SSAWLF.c 68.7 KB
Newer Older
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
1
2
/*******************************************************************************

3
4
 $Id$

5
6
 This file realizes the withlop folding for a code in ssa form. it uses no
 masks anymore. Most code is unchanged from the original implementation in
7
 WLF.c . Due to the ssaform, we can simplify some task in WLF concerning
8
 renaming operations.
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
9
10
11
12
13
14
15

 *******************************************************************************

 Usage of arg_info:

 wlfm_search_WL  :
 wlfm_search_ref :
Stephan Herhut's avatar
Stephan Herhut committed
16
17
18
19
 - NEXT   : store old information in nested WLs
 - WL     : reference to base node of current WL (N_Nwith)
 - FLAG   : collects information in the wlfm_search phase if there is
            at least one foldable reference in the WL.
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
20
21
22

 wlfm_replace    :
 wlfm_rename     :
Stephan Herhut's avatar
Stephan Herhut committed
23
24
25
26
27
28
 - SUBST  : Pointer to subst N_Ncode/ pointer to copy of subst N_Ncode
 - NEW_ID : pointer to N_id which shall replace the prf sel()
            in the target Code.
 - ID     : pointer to original N_id node in target WL to replace
            by subst WL.
 - NCA    : points to the assignment of the subst WL. Here new
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
29
30
31
                     assignments are inserted to define new variables.

 all wlfm phases :
Stephan Herhut's avatar
Stephan Herhut committed
32
33
 - ASSIGN : always the last N_assign node
 - FUNDEF : pointer to last fundef node. Needed to access vardecs
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
34
35
36
37
38
39
40
41
42
43
44
45
46

 ******************************************************************************

 Usage of ID_WL:

 ID_WL is used in 2 ways:
 (1) in WLI phase we store a pointer to (assign node of) a WL in ID_WL if
     the Id references a new WL. This pointer is used in WLF.
 (2) When the decision to fold the Id has been made it is necessary to
     create new code blocks. In this new code blocks ID_WL is used to
     point to it's original Id. So we have to distinguish between the Ids
     which are in the original code (1) and those in newly created code
     blocks (2).
Kai Trojahner's avatar
Kai Trojahner committed
47
     Creation of new code is initiated in CreateCode() and the resulting
48
     N_code nodes are collected in new_codes until they are inserted into the
Kai Trojahner's avatar
Kai Trojahner committed
49
     syntax tree in WLFNwith(). While a code is in new_codes, ID_WL of every
50
     Id inside the code points to its original Id (this is the Id which was
Kai Trojahner's avatar
Kai Trojahner committed
51
52
53
     copied by DUPdoDupTree()). DUPdoDupTree() sets this pointer and, if the
     argument of DUPdoDupTree() is a code inside new_codes, copies ID_WL of
     this code (which is a pointer to the original).
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
54
55
56
57
58

 ******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
59
#include <string.h>
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
60
61
62
63
64
#include <limits.h>

#include "types.h"
#include "tree_basic.h"
#include "tree_compound.h"
65
66
#include "str.h"
#include "memory.h"
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
67
68
69
#include "free.h"
#include "DupTree.h"
#include "globals.h"
70
#include "ctinfo.h"
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
71
72
73
74
75
#include "dbug.h"
#include "traverse.h"
#include "SSAWithloopFolding.h"
#include "SSAWLF.h"
#include "shape.h"
76
77
#include "type_utils.h"
#include "new_types.h"
78
#include "new_typecheck.h"
79
#include "constants.h"
80
#include "math_utils.h"
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
81

Stephan Herhut's avatar
Stephan Herhut committed
82
83
84
85
/*
 * INFO structure
 */
struct INFO {
Sven-Bodo Scholz's avatar
Sven-Bodo Scholz committed
86
    bool onefundef;
Stephan Herhut's avatar
Stephan Herhut committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
    info *next;
    node *subst;
    node *wl;
    node *new_id;
    node *assign;
    node *fundef;
    node *id;
    node *nca;
    int flag;
    int mystery;
};

/*
 * INFO macros
 */
Sven-Bodo Scholz's avatar
Sven-Bodo Scholz committed
102
#define INFO_ONEFUNDEF(n) (n->onefundef)
Kai Trojahner's avatar
Kai Trojahner committed
103
104
105
106
107
108
109
110
111
112
#define INFO_NEXT(n) (n->next)
#define INFO_SUBST(n) (n->subst)
#define INFO_WL(n) (n->wl)
#define INFO_NEW_ID(n) (n->new_id)
#define INFO_ASSIGN(n) (n->assign)
#define INFO_FUNDEF(n) (n->fundef)
#define INFO_ID(n) (n->id)
#define INFO_NCA(n) (n->nca)
#define INFO_FLAG(n) (n->flag)
#define INFO_MYSTERY(n) (n->mystery)
Stephan Herhut's avatar
Stephan Herhut committed
113
114
115
116
117
118
119
120
121
122
123

/*
 * INFO functions
 */
static info *
MakeInfo ()
{
    info *result;

    DBUG_ENTER ("MakeInfo");

124
    result = MEMmalloc (sizeof (info));
Stephan Herhut's avatar
Stephan Herhut committed
125

Sven-Bodo Scholz's avatar
Sven-Bodo Scholz committed
126
    INFO_ONEFUNDEF (result) = FALSE;
Kai Trojahner's avatar
Kai Trojahner committed
127
128
129
130
131
132
133
134
135
136
    INFO_NEXT (result) = NULL;
    INFO_SUBST (result) = NULL;
    INFO_WL (result) = NULL;
    INFO_NEW_ID (result) = NULL;
    INFO_ASSIGN (result) = NULL;
    INFO_FUNDEF (result) = NULL;
    INFO_ID (result) = NULL;
    INFO_NCA (result) = NULL;
    INFO_FLAG (result) = 0;
    INFO_MYSTERY (result) = 0;
Stephan Herhut's avatar
Stephan Herhut committed
137
138
139
140
141
142
143
144
145

    DBUG_RETURN (result);
}

static info *
FreeInfo (info *info)
{
    DBUG_ENTER ("FreeInfo");

146
    info = MEMfree (info);
Stephan Herhut's avatar
Stephan Herhut committed
147
148
149
150

    DBUG_RETURN (info);
}

Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/******************************************************************************
 *
 *   typedefs
 *
 ******************************************************************************/

/* Several traverse functions of this file are traversed for different
   purposes. This enum determines ths function. */
typedef enum {
    wlfm_search_WL,  /* find new WLs */
    wlfm_search_ref, /* find references within WL to fold. */
    wlfm_rename,     /* traverse copied body of substWL and
                        rename variables to avoid name clashes. */
    wlfm_replace     /* traverse copied body of targetWL and
                        replace substituted reference. */
    /* wlfm_search_WL and wlm_search_ref both traverse the original target code
       block. The first phase searches for other WLs inside the current WL
       body. The most cascated WLs are folded first. The second phase again
       traverses the target code and, if a foldable reference is found, initiates
       folding.  wlfm_replace traverses the copied target code block and tries to
       find the same point (Id which should be folded) here. Then the subst code
       is inserted at this point.  wlfm_rename traverses the subst code and renames
       variables which would lead to name clashes. */
} wlf_mode_type;

typedef struct CODE_CONSTRUCTION {
    node *target;
    node *subst;
    node *new;
    struct CODE_CONSTRUCTION *next;
} code_constr_type;

/******************************************************************************
 *
 *   global variables
 *
 ******************************************************************************/

Dietmar Kreye's avatar
Dietmar Kreye committed
189
190
191
192
193
194
static wlf_mode_type wlf_mode;

static code_constr_type *code_constr; /* list of combinded code blocks */

static node *new_codes; /* list of created N_Ncode nodes */

Stephan Herhut's avatar
Stephan Herhut committed
195
static info *ref_mode_arg_info; /* saves arg_info for CreateCode(). Else
Dietmar Kreye's avatar
Dietmar Kreye committed
196
197
198
199
200
                                   we would have to pass arg_info through
                                   many functions (which costs time) */

static intern_gen *new_ig; /* new generators derived by a traversel
                              of one Ncode block. */
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
201

Dietmar Kreye's avatar
Dietmar Kreye committed
202
203
static intern_gen *all_new_ig; /* new generators derived from all Ncode
                                  nodes. */
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
204
205
206

/* global vars to speed up function call of IntersectGrids(). They are only used
   to transfer information between IntersectGrids() and IntersectInternGen(). */
Dietmar Kreye's avatar
Dietmar Kreye committed
207
208
209
210
211
212
213
214

static int *intersect_grids_ot; /* grid offsets used by IntersectGrids() */

static int *intersect_grids_os;

static intern_gen *intersect_grids_tig, *intersect_grids_sig, *intersect_grids_baseig;

static intern_gen *intersect_intern_gen; /* resulting igs of IntersectInternGen. */
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
215
216
217
218
219
220
221
222
223
224

/******************************************************************************
 *
 * function:
 *   void AddCC(node *targetn, node *substn, node *resultn)
 *
 * description:
 *   adds entry to the global code_constr list.
 *
 ******************************************************************************/
Dietmar Kreye's avatar
Dietmar Kreye committed
225

Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
226
227
228
229
230
231
232
static void
AddCC (node *targetn, node *substn, node *resultn)
{
    code_constr_type *cc;

    DBUG_ENTER ("AddCC");

233
    cc = MEMmalloc (sizeof (code_constr_type));
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
    cc->target = targetn;
    cc->subst = substn;
    cc->new = resultn;
    cc->next = code_constr;
    code_constr = cc;

    DBUG_VOID_RETURN;
}

/******************************************************************************
 *
 * function:
 *   code_constr_type *SearchCC(node *targetn, node *substn)
 *
 * description:
 *   Searches for an existing entry of targetn and substn and returns it
 *   or NULL if not found
 *
 ******************************************************************************/
Dietmar Kreye's avatar
Dietmar Kreye committed
253

Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
static code_constr_type *
SearchCC (node *targetn, node *substn)
{
    code_constr_type *cc;

    DBUG_ENTER ("SearchCC");

    cc = code_constr;
    while (cc && (cc->target != targetn || cc->subst != substn)) {
        cc = cc->next;
    }

    DBUG_RETURN (cc);
}

/******************************************************************************
 *
 * function:
 *   void FreeCC(code_constr_type *cc)
 *
 * description:
 *   sets whole cc list free
 *
 ******************************************************************************/
Dietmar Kreye's avatar
Dietmar Kreye committed
278

Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
279
280
281
282
283
284
285
286
287
288
static void
FreeCC (code_constr_type *cc)
{
    code_constr_type *tmpcc;

    DBUG_ENTER ("FreeCC");

    while (cc) {
        tmpcc = cc;
        cc = cc->next;
289
        tmpcc = MEMfree (tmpcc);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
290
291
292
293
294
295
296
297
298
299
300
301
302
    }

    DBUG_VOID_RETURN;
}

/******************************************************************************
 *
 * function:
 *   intern_gen *MergeGenerators(intern_gen *ig)
 *
 * description:
 *   tries to merge gererators (grids) to reduce effort of further
 *   intersections.
303
 *   This optimization can result in better code production of compile
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
304
305
306
307
 *   (wltransform.c, OptimizeWL does something similar but it not that
 *    powerful).
 *
 ******************************************************************************/
Dietmar Kreye's avatar
Dietmar Kreye committed
308

Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
static intern_gen *
MergeGenerators (intern_gen *ig)
{
    DBUG_ENTER ("MergeGenerators");

    DBUG_RETURN (ig);
}

/******************************************************************************
 *
 * function:
 *   intern_gen *LinearTransformationsHelp(intern_gen *ig, int dim, prf prf,
 *                                         int arg_no, int constval)
 *
 * description:
 *   Executes transformation (prf const) on the given ig in dimension dim.
 *   More complex transformations require to split up generators in two new
 *   generators (original ig and newig). If a newig is created, it is returned.
 *
 ******************************************************************************/
Dietmar Kreye's avatar
Dietmar Kreye committed
329

Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
330
331
332
333
334
335
336
337
338
339
static intern_gen *
LinearTransformationsHelp (intern_gen *ig, int dim, prf prf, int arg_no, int constval)
{
    int lbuf, ubuf, cut, buf;
    intern_gen *newig = NULL;

    DBUG_ENTER ("LinearTransformationsHelp");
    DBUG_ASSERT (((1 == arg_no) || (2 == arg_no)), "wrong parameters");

    switch (prf) {
340
    case F_add_SxS:
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
341
342
343
344
345
        /* addition is commutable, grids are not affected. */
        ig->l[dim] -= constval;
        ig->u[dim] -= constval;
        break;

346
    case F_sub_SxS:
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
        if (2 == arg_no) {
            /* index - scalar, grids are not affected */
            ig->l[dim] += constval;
            ig->u[dim] += constval;
        } else { /* arg_no == 1 */
            /* scalar - index */
            lbuf = constval - ig->u[dim] + 1; /* +1 to transform < in <= */
            ubuf = constval - ig->l[dim] + 1; /* +1 to transform <= in < */

            if (ig->step) {
                /*
                 * reverse the grid.
                 * Example:
                 * "xxx  xxx  xx" => "xx  xxx  xxx"
                 * done with 2 gens: "xx   xx   xx" &&
                 *                       "x    x  "
                 */
                cut = (ig->u[dim] - ig->l[dim]) % ig->step[dim];
                if (cut == 0) {
                    lbuf += ig->step[dim] - ig->width[dim];
                } else if (cut > ig->width[dim]) {
                    lbuf += cut - ig->width[dim];
                } else if (cut < ig->width[dim]) {
                    /* make newig */
371
                    newig = WLFcopyInternGen (ig);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
372
373
374
375
376
                    newig->l[dim] = ig->l[dim] + cut;
                    newig->width[dim] = ig->width[dim] - cut;
                    ig->width[dim] = cut;
                    /* if u - l is smaller than width, the newig is empty */
                    if (newig->u[dim] <= newig->l[dim]) {
377
                        newig = WLFfreeInternGen (newig);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
378
379
380
381
382
383
384
385
386
387
388
389
390
391
                    } else {
                        buf = (constval - newig->u[dim] + 1 + newig->step[dim]
                               - newig->width[dim]);
                        newig->u[dim] = constval - newig->l[dim] + 1;
                        newig->l[dim] = buf;
                    }
                }
            }

            ig->l[dim] = lbuf;
            ig->u[dim] = ubuf;
        }
        break;

392
    case F_mul_SxS:
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
        /*
         * first try  (srs):  ig->l[dim] = (ig->l[dim] + constval - 1) / constval;
         *                    ig->u[dim] = (ig->u[dim] - 1) / constval +1;
         *
         * second try (sbs):  ig->l[dim] = (ig->l[dim] + constval - 1) / constval;
         *                    ig->u[dim] = (ig->u[dim] + constval - 1) / constval;
         *
         * But this is still incorrect in some cases, e.g. if (constval < 0) ...
         *
         * These conditions must hold for l_neu and u_neu:
         *
         *   constval > 0:      l  >  constval * ( l_neu - 1 )
         *                  &&  l  <= constval * l_neu
         *                  &&  u  >  constval * ( u_neu - 1 )
         *                  &&  u  <= constval * u_neu
         *
         *             ==>      l / constvec  <=  l_neu  <  l / constvec + 1
         *                  &&  u / constvec  <=  u_neu  <  u / constvec + 1
         *
         *   constval < 0:      u  >  constval * l_neu           // bounds reflected, ...
         *                  &&  u  <= constval * ( l_neu - 1 )   // ... <= and < reversed
         *                  &&  l  >  constval * u_neu
         *                  &&  l  <= constval * ( u_neu - 1 )
         *
         *             ==>      u / constvec  <  l_neu  <=  u / constval + 1
         *                  &&  l / constvec  <  u_neu  <=  l / constval + 1
         *
         * Therefore the following code should work correctly:
         */
        if (constval > 0) {
            ig->l[dim] = ((ig->l[dim] % constval == 0) || (ig->l[dim] < 0))
                           ? (ig->l[dim] / constval)
                           : (ig->l[dim] / constval + 1);
            ig->u[dim] = ((ig->u[dim] % constval == 0) || (ig->u[dim] < 0))
                           ? (ig->u[dim] / constval)
                           : (ig->u[dim] / constval + 1);
        }
        if (constval < 0) {
            ig->l[dim] = ((ig->u[dim] % constval == 0) || (ig->u[dim] < 0))
                           ? (ig->u[dim] / constval + 1)
                           : (ig->u[dim] / constval);
            ig->u[dim] = ((ig->l[dim] % constval == 0) || (ig->l[dim] < 0))
                           ? (ig->l[dim] / constval + 1)
                           : (ig->l[dim] / constval);
        }

        if (ig->step) {
            DBUG_ASSERT (0, ("WL folding with transformed index variables "
                             "by multiplication and grids not supported right now."));
        }
        break;

445
    case F_div_SxS:
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
        DBUG_ASSERT ((arg_no == 2),
                     "WLF transformation (scalar / index) not yet implemented!");

        /*
         * first try  (srs):  ig->l[dim] = ig->l[dim] * constval;
         *                    ig->u[dim] = ig->u[dim] * constval;
         *
         * But this is incorrect in some cases, e.g. if (constval < 0) ...
         *
         * These conditions must hold for l_neu and u_neu:
         *
         *   constval > 0:      l  >  ( l_neu - 1 ) / constval
         *                  &&  l  <= l_neu / constval
         *                  &&  u  >  ( u_neu - 1 ) / constval
         *                  &&  u  <= u_neu / constval
         *
         *             ==>      l * constvec  <=  l_neu  <  l * constvec + 1
         *                  &&  u * constvec  <=  u_neu  <  u * constvec + 1
         *
         *   constval < 0:      u  >  l_neu / constval           // bounds reflected, ...
         *                  &&  u  <= ( l_neu - 1 ) / constval   // ... <= and < reversed
         *                  &&  l  >  u_neu / constval
         *                  &&  l  <= ( u_neu - 1 ) / constval
         *
         *             ==>      u * constvec  <  l_neu  <=  u * constval + 1
         *                  &&  l * constvec  <  u_neu  <=  l * constval + 1
         *
         * Therefore the following code should work correctly:
         */
        if (constval > 0) {
            ig->l[dim] = ig->l[dim] * constval;
            ig->u[dim] = ig->u[dim] * constval;
        }
        if (constval < 0) {
            ig->l[dim] = ig->u[dim] * constval + 1;
            ig->u[dim] = ig->l[dim] * constval + 1;
        }

        if (ig->step) {
            DBUG_ASSERT (0, ("WL folding with transformed index variables "
                             "by division and grids not supported right now."));
        }
        break;

    default:
        DBUG_ASSERT (0, ("Wrong transformation function"));
    }

    DBUG_RETURN (newig);
}

/******************************************************************************
 *
 * function:
 *   void LinearTransformationsScalar( intern_gen *ig,
 *                                     index_info *transformations, int dim)
 *
 * description:
 *   like LinearTransformationsVector(), but only transforms one dimension of ig.
 *
 *
 ******************************************************************************/
Dietmar Kreye's avatar
Dietmar Kreye committed
508

Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
static intern_gen *
LinearTransformationsScalar (intern_gen *ig, index_info *transformations, int dim)
{
    intern_gen *actig, *newig;

    DBUG_ENTER ("LinearTransformationsScalar");
    DBUG_ASSERT (0 == transformations->vector, ("wrong parameters"));
    DBUG_ASSERT (!transformations->last[0] || !transformations->last[0]->vector,
                 ("scalar points to vector"));
    DBUG_ASSERT (transformations->permutation[0], ("scalar constant???"));

    actig = ig;
    if (transformations->arg_no)
        /* valid prf. */
        while (actig) { /* all igs */
            newig = LinearTransformationsHelp (actig, dim, transformations->prf,
                                               transformations->arg_no,
                                               transformations->const_arg[0]);

            if (newig) {
                newig->next = ig; /* insert new element before whole list. */
                ig = newig;       /* set new root */
            }
            actig = actig->next;
        }

    if (transformations->last[0]) {
        ig = LinearTransformationsScalar (ig, transformations->last[0], dim);
    }

    DBUG_RETURN (ig);
}

/******************************************************************************
 *
 * function:
Kai Trojahner's avatar
Kai Trojahner committed
545
546
 *   void LinearTransformationsVector(intern_gen *ig,
 *                                    index_info *transformations)
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
547
548
549
550
551
552
 *
 * description:
 *   realizes transformations on the given list of intern gens.
 *
 *
 ******************************************************************************/
Dietmar Kreye's avatar
Dietmar Kreye committed
553

Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
554
555
556
557
558
559
560
561
static intern_gen *
LinearTransformationsVector (intern_gen *ig, index_info *transformations)
{
    int dim, act_dim;
    intern_gen *actig, *newig;

    DBUG_ENTER ("LinearTransformationsVector");
    DBUG_ASSERT (transformations->vector == ig->shape,
562
                 ("Transformations do not fit to generators"));
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611

    dim = ig->shape;

    if (transformations->vector && transformations->arg_no) {
        /* vector transformation and a valid prf. */
        for (act_dim = 0; act_dim < dim; act_dim++) { /* all dimensions */
            actig = ig;
            if (transformations->permutation[act_dim]) {
                while (actig) { /* all igs */
                    newig
                      = LinearTransformationsHelp (actig, act_dim, transformations->prf,
                                                   transformations->arg_no,
                                                   transformations->const_arg[act_dim]);
                    if (newig) {
                        newig->next = ig; /* insert new element before whole list. */
                        ig = newig;       /* set new root */
                    }
                    actig = actig->next;
                }
            }
        }
    }

    if (transformations->last[0] && transformations->last[0]->vector) {
        ig = LinearTransformationsVector (ig, transformations->last[0]);
    } else { /* maybe additional scalar transformations */
        for (act_dim = 0; act_dim < dim; act_dim++) {
            if (transformations->last[act_dim]) {
                ig = LinearTransformationsScalar (ig, transformations->last[act_dim],
                                                  act_dim);
            }
        }
    }

    DBUG_RETURN (ig);
}

/******************************************************************************
 *
 * function:
 *   intern_gen *FinalTransformations( intern_gen *ig,
 *                                     index_info *transformations,
 *                                     int target_dim);
 *
 * description:
 *   transforms list of ig into cuboids of dimension target_dim.
 *
 *
 ******************************************************************************/
Dietmar Kreye's avatar
Dietmar Kreye committed
612

Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
static intern_gen *
FinalTransformations (intern_gen *substig, index_info *transformations, int target_dim)
{
    intern_gen *tmpig, *newig, *rootig;
    int ok, i, *help;

    DBUG_ENTER ("FinalTransformations");
    DBUG_ASSERT (transformations->vector == substig->shape, ("wrong parameters"));

    /* create array to speed up later transformations.
       help[i] is
       - 0 if the ith index scalar is not used to index the subst array
       - d if the ith index scalar addresses the dth component of the subst array.

       Example:
       iv=[i,j,k]
       subst array A[42,i,k]
       help: [2,0,3] */
    i = sizeof (int) * target_dim;
632
    help = MEMmalloc (i);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
    help = memset (help, 0, i);
    for (i = 0; i < transformations->vector; i++) {
        if (transformations->permutation[i]) {
            help[transformations->permutation[i] - 1] = i + 1;
        }
    }

    /* now process all substig and create new ig list rootig */
    tmpig = substig;
    newig = rootig = NULL;

    while (tmpig) {
        /* Check whether constants in *transformations disqualify this tmpig */
        ok = 1;
        for (i = 0; i < tmpig->shape && ok; i++)
            ok = (transformations->permutation[i] ||                /* not a constant */
                  ((tmpig->l[i] <= transformations->const_arg[i] && /* in range */
                    transformations->const_arg[i] < tmpig->u[i])
                   && (!tmpig->step || /* and no grid */
                       (transformations->const_arg[i] - tmpig->l[i])
                           % /* or in this grid */
                           tmpig->step[i]
                         < tmpig->width[i])));

        if (ok) {
            /* start transformations */
659
            newig = WLFcreateInternGen (target_dim, NULL != tmpig->step);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
            for (i = 0; i < target_dim; i++) {
                if (help[i]) {
                    newig->l[i] = tmpig->l[help[i] - 1];
                    newig->u[i] = tmpig->u[help[i] - 1];
                    if (tmpig->step) {
                        newig->step[i] = tmpig->step[help[i] - 1];
                        newig->width[i] = tmpig->width[help[i] - 1];
                    }
                } else {
                    newig->l[i] = 0;
                    newig->u[i] = INT_MAX;
                    if (tmpig->step) {
                        newig->step[i] = 1;
                        newig->width[i] = 1;
                    }
                }
            }
677
            DBUG_ASSERT (0 == WLFnormalizeInternGen (newig),
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
678
679
680
681
682
683
684
685
686
687
688
                         ("Error while normalizing ig"));

            newig->code = tmpig->code;
            newig->next = rootig;
            rootig = newig;
        }

        /* go to next substig */
        tmpig = tmpig->next;
    }

689
    help = MEMfree (help);
690
    WLFfreeInternGenChain (substig);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
691
692
693
694
695
696
697
698
699
700

    DBUG_RETURN (rootig);
}

/******************************************************************************
 *
 * function:
 *   node *CreateCode(node *target, node *subst)
 *
 * description:
Kai Trojahner's avatar
Kai Trojahner committed
701
 *   substitutes the code block subst into target. INFO_ID points
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
702
 *   to the N_id node in the target WL which shall be replaced.
703
 *   New vardecs and assignments are introduced and an N_code node is returned.
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
704
705
706
707
 *   Most of the work is done in the usual traversal steps (with mode
 *   wlfm_replace and wlfm_rename).
 *
 ******************************************************************************/
Dietmar Kreye's avatar
Dietmar Kreye committed
708

Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
709
710
711
static node *
CreateCode (node *target, node *subst)
{
Stephan Herhut's avatar
Stephan Herhut committed
712
713
    node *coden;
    info *new_arg_info;
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
714
715
716

    DBUG_ENTER ("CreateCode");

717
718
    DBUG_ASSERT ((N_code == NODE_TYPE (target)), "wrong target Parameter");
    DBUG_ASSERT ((N_code == NODE_TYPE (subst)), "wrong subst Parameter");
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
719
720
721
722
723
724

    wlf_mode = wlfm_replace;

    /* create new arg_info to avoid modifications of the old one and copy
       all relevant information */
    new_arg_info = MakeInfo ();
Kai Trojahner's avatar
Kai Trojahner committed
725
726
727
728
729
    INFO_FUNDEF (new_arg_info) = INFO_FUNDEF (ref_mode_arg_info);
    INFO_ID (new_arg_info) = INFO_ID (ref_mode_arg_info);
    INFO_NCA (new_arg_info) = INFO_NCA (ref_mode_arg_info);
    INFO_SUBST (new_arg_info) = subst;
    INFO_NEW_ID (new_arg_info) = NULL;
Stephan Herhut's avatar
Stephan Herhut committed
730
731

    /* WHY is this copied? */
Kai Trojahner's avatar
Kai Trojahner committed
732
    INFO_MYSTERY (new_arg_info) = INFO_MYSTERY (ref_mode_arg_info);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
733
734

    /*
Sven-Bodo Scholz's avatar
Sven-Bodo Scholz committed
735
736
     * DUPdoDupTree() shall fill ID_WL of Id nodes with special information.
     * So we have to call DUPdoDupTree() with DUP_WLF.
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
737
     */
738
    coden = DUPdoDupTreeType (CODE_CBLOCK (target), DUP_WLF);
Sven-Bodo Scholz's avatar
Sven-Bodo Scholz committed
739
    coden = TRAVdo (coden, new_arg_info);
740
    coden = TBmakeCode (coden, DUPdoDupTreeType (CODE_CEXPRS (target), DUP_WLF));
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
741

Stephan Herhut's avatar
Stephan Herhut committed
742
    new_arg_info = FreeInfo (new_arg_info);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758

    wlf_mode = wlfm_search_ref;

    DBUG_RETURN (coden);
}

/******************************************************************************
 *
 * function:
 *   void IntersectGrids(int dim, intern_gen *ig)
 *
 * description:
 *   recursive function to intersect two grids. Information are made available
 *   in global variables intersect_grids* to speed up recursive function calls.
 *
 ******************************************************************************/
Dietmar Kreye's avatar
Dietmar Kreye committed
759

Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
static void
IntersectGrids (int dim)
{
    int dc, first, last, d;
    intern_gen *ig;
    code_constr_type *cc;
    node *coden;

    DBUG_ENTER ("IntersectGrids");

    dc = 0;

    while (dc < intersect_grids_baseig->step[dim]) {
        /* search common dc */
        if (((!intersect_grids_tig->step)
             || ((dc + intersect_grids_ot[dim]) % intersect_grids_tig->step[dim]
                 < intersect_grids_tig->width[dim]))
            && ((!intersect_grids_sig->step)
                || ((dc + intersect_grids_os[dim]) % intersect_grids_sig->step[dim]
                    < intersect_grids_sig->width[dim]))) {
            first = dc;
            /* search first dc where either in tig or sig the element is
               not present. */
            do {
                dc++;
            } while (
              ((!intersect_grids_tig->step)
               || ((dc + intersect_grids_ot[dim]) % intersect_grids_tig->step[dim]
                   < intersect_grids_tig->width[dim]))
              && ((!intersect_grids_sig->step)
                  || ((dc + intersect_grids_os[dim]) % intersect_grids_sig->step[dim]
                      < intersect_grids_sig->width[dim]))
              && (dc < intersect_grids_baseig->step[dim]));
            last = dc;

            if (dim < intersect_grids_baseig->shape - 1) {
                /* process inner dimensions if lower bound is less then upper bound. */
                if (intersect_grids_baseig->l[dim] + first
                    < intersect_grids_baseig->u[dim]) {
                    intersect_grids_baseig->l[dim] += first;
                    intersect_grids_baseig->width[dim] = last - first;
                    IntersectGrids (dim + 1);
                    intersect_grids_baseig->l[dim] -= first; /* restore old value */
                } else {
                    dc = intersect_grids_baseig->step[dim]; /* stop further search */
                }
            } else {
                /* create new ig and add it to structures
                   if lower bound is less then upper bound. */
                if (intersect_grids_baseig->l[dim] + first
                    < intersect_grids_baseig->u[dim]) {
811
                    ig = WLFcreateInternGen (intersect_grids_baseig->shape, 1);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
                    for (d = 0; d < intersect_grids_baseig->shape; d++) {
                        ig->l[d] = intersect_grids_baseig->l[d];
                        ig->u[d] = intersect_grids_baseig->u[d];
                        ig->step[d] = intersect_grids_baseig->step[d];
                        ig->width[d] = intersect_grids_baseig->width[d];
                    }
                    ig->l[dim] += first;
                    ig->width[dim] = last - first;

                    /* add craetes code to code_constr list and to new_codes. */
                    cc = SearchCC (intersect_grids_tig->code, intersect_grids_sig->code);
                    if (cc) {
                        ig->code = cc->new;
                    } else {
                        coden = CreateCode (intersect_grids_tig->code,
                                            intersect_grids_sig->code);
                        ig->code = coden;
                        AddCC (intersect_grids_tig->code, intersect_grids_sig->code,
                               coden);
Sven-Bodo Scholz's avatar
Sven-Bodo Scholz committed
831
                        CODE_NEXT (coden) = new_codes;
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
                        new_codes = coden;
                    }

                    /* add new generator to intersect_intern_gen list. */
                    ig->next = intersect_intern_gen;
                    intersect_intern_gen = ig;
                } else {
                    dc = intersect_grids_baseig->step[dim]; /* stop further search */
                }
            }
        }

        dc++;
    }

    DBUG_VOID_RETURN;
}

/******************************************************************************
 *
 * function:
 *   intern_gen *IntersectInternGen(intern_gen *tig, intern_gen *sig)
 *
 * description:
 *   Intersects the generators of the currently processed code (tig) and all
 *   generators of the subst WL (sig). Appends new codes to code_constr.
 *   Returns intern_gen list of new intersections.
 *
 ******************************************************************************/
Dietmar Kreye's avatar
Dietmar Kreye committed
861

Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
static intern_gen *
IntersectInternGen (intern_gen *target_ig, intern_gen *subst_ig)
{
    intern_gen *sig, *new_gen, *new_gen_step, *new_gen_nostep;
    int d, max_dim, l, u, create_steps;
    code_constr_type *cc;
    node *coden;

    DBUG_ENTER ("IntersectInternGen");
    DBUG_ASSERT (target_ig->shape == subst_ig->shape, ("wrong parameters"));

    max_dim = target_ig->shape;
    new_gen_step = NULL;
    new_gen_nostep = NULL;
    intersect_intern_gen = NULL;

    while (target_ig) {
        sig = subst_ig;
        while (sig) {
            if (!new_gen_step) {
882
                new_gen_step = WLFcreateInternGen (target_ig->shape, 1);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
883
884
            }
            if (!new_gen_nostep) {
885
                new_gen_nostep = WLFcreateInternGen (target_ig->shape, 0);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
886
887
888
889
890
891
892
893
894
895
896
897
898
899
            }

            create_steps = target_ig->step || sig->step;
            if (create_steps) {
                new_gen = new_gen_step;
                new_gen_step = NULL;
            } else {
                new_gen = new_gen_nostep;
                new_gen_nostep = NULL;
            }

            /* here we have to intersect target_ig and sig */
            /* first the bounds, ignore step/width */
            for (d = 0; d < max_dim; d++) {
900
901
                l = MATHmax (target_ig->l[d], sig->l[d]);
                u = MATHmin (target_ig->u[d], sig->u[d]);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
                if (l >= u) {
                    break; /* empty intersection */
                } else {
                    new_gen->l[d] = l;
                    new_gen->u[d] = u;
                }
            }

            if (d == max_dim) {
                /* we have a new ig. Maybe. Because if both generators are
                   grids they may still be disjuct. */
                if (create_steps) {
                    /* prepare parameters for recursive function IntersectGrids().
                       Global pointers are used to speed up function call. */
                    /* new_gen will be the reference structure for new igs
                       created within IntersectGrids(). */
                    if (!target_ig->step) {
                        for (d = 0; d < max_dim; d++) {
                            new_gen->step[d] = sig->step[d];
                        }
                    } else if (!sig->step) {
                        for (d = 0; d < max_dim; d++) {
                            new_gen->step[d] = target_ig->step[d];
                        }
                    } else {
                        for (d = 0; d < max_dim; d++) {
928
                            new_gen->step[d] = MATHlcm (target_ig->step[d], sig->step[d]);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
                        }
                    }

                    /* compute offsets */
                    if (target_ig->step) {
                        for (d = 0; d < max_dim; d++) {
                            intersect_grids_ot[d]
                              = (new_gen->l[d] - target_ig->l[d]) % target_ig->step[d];
                        }
                    }
                    if (sig->step) {
                        for (d = 0; d < max_dim; d++) {
                            intersect_grids_os[d]
                              = (new_gen->l[d] - sig->l[d]) % sig->step[d];
                        }
                    }

                    intersect_grids_baseig = new_gen;
                    intersect_grids_tig = target_ig;
                    intersect_grids_sig = sig;

                    IntersectGrids (0);
                    /* the mem of new_gen has not been erased in IntersectGrids(). It can
                       be used for the next intersection. Don't free it. */
                    new_gen_step = new_gen;
                } else {
                    /* craete new code and add it to code_constr list and to new_codes. */
                    cc = SearchCC (target_ig->code, sig->code);
                    if (cc) {
                        new_gen->code = cc->new;
                    } else {
                        coden = CreateCode (target_ig->code, sig->code);
                        new_gen->code = coden;
                        AddCC (target_ig->code, sig->code, coden);
Sven-Bodo Scholz's avatar
Sven-Bodo Scholz committed
963
                        CODE_NEXT (coden) = new_codes;
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
                        new_codes = coden;
                    }

                    /* add new generator to intersect_intern_gen list. */
                    new_gen->next = intersect_intern_gen;
                    intersect_intern_gen = new_gen;
                    new_gen = NULL;
                }
            }

            sig = sig->next; /* go on with next subst ig */
        }
        target_ig = target_ig->next; /* go on with next target ig */
    }

    if (new_gen_step) {
980
        new_gen_step = WLFfreeInternGen (new_gen_step);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
981
982
    }
    if (new_gen_nostep) {
983
        new_gen_nostep = WLFfreeInternGen (new_gen_nostep);
Nico Marcussen-Wulff's avatar
Nico Marcussen-Wulff committed
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
    }

    DBUG_RETURN (intersect_intern_gen);
}

/******************************************************************************
 *
 * function:
 *   intern_gen *RemoveDoubleIndexVectors( intern_gen *subst_ig,
 *                                         index_info *transformations)
 *
 * description:
 *   only used if permutations of index scalar variables are enabled.
 *   This is a transformation to reduce the number of components of the ig.
 *
 ******************************************************************************/
Dietmar Kreye's avatar
Dietmar Kreye committed
1000