OpenCores
URL https://opencores.org/ocsvn/scarts/scarts/trunk

Subversion Repositories scarts

[/] [scarts/] [trunk/] [toolchain/] [scarts-gcc/] [gcc-4.1.1/] [gcc/] [tree-data-ref.c] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jlechner
/* Data references and dependences detectors.
2
   Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
3
   Contributed by Sebastian Pop <s.pop@laposte.net>
4
 
5
This file is part of GCC.
6
 
7
GCC is free software; you can redistribute it and/or modify it under
8
the terms of the GNU General Public License as published by the Free
9
Software Foundation; either version 2, or (at your option) any later
10
version.
11
 
12
GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13
WARRANTY; without even the implied warranty of MERCHANTABILITY or
14
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15
for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with GCC; see the file COPYING.  If not, write to the Free
19
Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20
02110-1301, USA.  */
21
 
22
/* This pass walks a given loop structure searching for array
23
   references.  The information about the array accesses is recorded
24
   in DATA_REFERENCE structures.
25
 
26
   The basic test for determining the dependences is:
27
   given two access functions chrec1 and chrec2 to a same array, and
28
   x and y two vectors from the iteration domain, the same element of
29
   the array is accessed twice at iterations x and y if and only if:
30
   |             chrec1 (x) == chrec2 (y).
31
 
32
   The goals of this analysis are:
33
 
34
   - to determine the independence: the relation between two
35
     independent accesses is qualified with the chrec_known (this
36
     information allows a loop parallelization),
37
 
38
   - when two data references access the same data, to qualify the
39
     dependence relation with classic dependence representations:
40
 
41
       - distance vectors
42
       - direction vectors
43
       - loop carried level dependence
44
       - polyhedron dependence
45
     or with the chains of recurrences based representation,
46
 
47
   - to define a knowledge base for storing the data dependence
48
     information,
49
 
50
   - to define an interface to access this data.
51
 
52
 
53
   Definitions:
54
 
55
   - subscript: given two array accesses a subscript is the tuple
56
   composed of the access functions for a given dimension.  Example:
57
   Given A[f1][f2][f3] and B[g1][g2][g3], there are three subscripts:
58
   (f1, g1), (f2, g2), (f3, g3).
59
 
60
   - Diophantine equation: an equation whose coefficients and
61
   solutions are integer constants, for example the equation
62
   |   3*x + 2*y = 1
63
   has an integer solution x = 1 and y = -1.
64
 
65
   References:
66
 
67
   - "Advanced Compilation for High Performance Computing" by Randy
68
   Allen and Ken Kennedy.
69
   http://citeseer.ist.psu.edu/goff91practical.html
70
 
71
   - "Loop Transformations for Restructuring Compilers - The Foundations"
72
   by Utpal Banerjee.
73
 
74
 
75
*/
76
 
77
#include "config.h"
78
#include "system.h"
79
#include "coretypes.h"
80
#include "tm.h"
81
#include "ggc.h"
82
#include "tree.h"
83
 
84
/* These RTL headers are needed for basic-block.h.  */
85
#include "rtl.h"
86
#include "basic-block.h"
87
#include "diagnostic.h"
88
#include "tree-flow.h"
89
#include "tree-dump.h"
90
#include "timevar.h"
91
#include "cfgloop.h"
92
#include "tree-chrec.h"
93
#include "tree-data-ref.h"
94
#include "tree-scalar-evolution.h"
95
#include "tree-pass.h"
96
 
97
static tree object_analysis (tree, tree, bool, struct data_reference **,
98
                             tree *, tree *, tree *, tree *, tree *,
99
                             struct ptr_info_def **, subvar_t *);
100
static struct data_reference * init_data_ref (tree, tree, tree, tree, bool,
101
                                              tree, tree, tree, tree, tree,
102
                                              struct ptr_info_def *,
103
                                              enum  data_ref_type);
104
 
105
/* Determine if PTR and DECL may alias, the result is put in ALIASED.
106
   Return FALSE if there is no type memory tag for PTR.
107
*/
108
static bool
109
ptr_decl_may_alias_p (tree ptr, tree decl,
110
                      struct data_reference *ptr_dr,
111
                      bool *aliased)
112
{
113
  tree tag;
114
 
115
  gcc_assert (TREE_CODE (ptr) == SSA_NAME && DECL_P (decl));
116
 
117
  tag = get_var_ann (SSA_NAME_VAR (ptr))->type_mem_tag;
118
  if (!tag)
119
    tag = DR_MEMTAG (ptr_dr);
120
  if (!tag)
121
    return false;
122
 
123
  *aliased = is_aliased_with (tag, decl);
124
  return true;
125
}
126
 
127
 
128
/* Determine if two pointers may alias, the result is put in ALIASED.
129
   Return FALSE if there is no type memory tag for one of the pointers.
130
*/
131
static bool
132
ptr_ptr_may_alias_p (tree ptr_a, tree ptr_b,
133
                     struct data_reference *dra,
134
                     struct data_reference *drb,
135
                     bool *aliased)
136
{
137
  tree tag_a, tag_b;
138
 
139
  tag_a = get_var_ann (SSA_NAME_VAR (ptr_a))->type_mem_tag;
140
  if (!tag_a)
141
    tag_a = DR_MEMTAG (dra);
142
  if (!tag_a)
143
    return false;
144
  tag_b = get_var_ann (SSA_NAME_VAR (ptr_b))->type_mem_tag;
145
  if (!tag_b)
146
    tag_b = DR_MEMTAG (drb);
147
  if (!tag_b)
148
    return false;
149
  *aliased = (tag_a == tag_b);
150
  return true;
151
}
152
 
153
 
154
/* Determine if BASE_A and BASE_B may alias, the result is put in ALIASED.
155
   Return FALSE if there is no type memory tag for one of the symbols.
156
*/
157
static bool
158
may_alias_p (tree base_a, tree base_b,
159
             struct data_reference *dra,
160
             struct data_reference *drb,
161
             bool *aliased)
162
{
163
  if (TREE_CODE (base_a) == ADDR_EXPR || TREE_CODE (base_b) == ADDR_EXPR)
164
    {
165
      if (TREE_CODE (base_a) == ADDR_EXPR && TREE_CODE (base_b) == ADDR_EXPR)
166
        {
167
         *aliased = (TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0));
168
         return true;
169
        }
170
      if (TREE_CODE (base_a) == ADDR_EXPR)
171
        return ptr_decl_may_alias_p (base_b, TREE_OPERAND (base_a, 0), drb,
172
                                     aliased);
173
      else
174
        return ptr_decl_may_alias_p (base_a, TREE_OPERAND (base_b, 0), dra,
175
                                     aliased);
176
    }
177
 
178
  return ptr_ptr_may_alias_p (base_a, base_b, dra, drb, aliased);
179
}
180
 
181
 
182
/* Determine if a pointer (BASE_A) and a record/union access (BASE_B)
183
   are not aliased. Return TRUE if they differ.  */
184
static bool
185
record_ptr_differ_p (struct data_reference *dra,
186
                     struct data_reference *drb)
187
{
188
  bool aliased;
189
  tree base_a = DR_BASE_OBJECT (dra);
190
  tree base_b = DR_BASE_OBJECT (drb);
191
 
192
  if (TREE_CODE (base_b) != COMPONENT_REF)
193
    return false;
194
 
195
  /* Peel COMPONENT_REFs to get to the base. Do not peel INDIRECT_REFs.
196
     For a.b.c.d[i] we will get a, and for a.b->c.d[i] we will get a.b.
197
     Probably will be unnecessary with struct alias analysis.  */
198
  while (TREE_CODE (base_b) == COMPONENT_REF)
199
     base_b = TREE_OPERAND (base_b, 0);
200
  /* Compare a record/union access (b.c[i] or p->c[i]) and a pointer
201
     ((*q)[i]).  */
202
  if (TREE_CODE (base_a) == INDIRECT_REF
203
      && ((TREE_CODE (base_b) == VAR_DECL
204
           && (ptr_decl_may_alias_p (TREE_OPERAND (base_a, 0), base_b, dra,
205
                                     &aliased)
206
               && !aliased))
207
          || (TREE_CODE (base_b) == INDIRECT_REF
208
              && (ptr_ptr_may_alias_p (TREE_OPERAND (base_a, 0),
209
                                       TREE_OPERAND (base_b, 0), dra, drb,
210
                                       &aliased)
211
                  && !aliased))))
212
    return true;
213
  else
214
    return false;
215
}
216
 
217
 
218
/* Determine if an array access (BASE_A) and a record/union access (BASE_B)
219
   are not aliased. Return TRUE if they differ.  */
220
static bool
221
record_array_differ_p (struct data_reference *dra,
222
                       struct data_reference *drb)
223
{
224
  bool aliased;
225
  tree base_a = DR_BASE_OBJECT (dra);
226
  tree base_b = DR_BASE_OBJECT (drb);
227
 
228
  if (TREE_CODE (base_b) != COMPONENT_REF)
229
    return false;
230
 
231
  /* Peel COMPONENT_REFs to get to the base. Do not peel INDIRECT_REFs.
232
     For a.b.c.d[i] we will get a, and for a.b->c.d[i] we will get a.b.
233
     Probably will be unnecessary with struct alias analysis.  */
234
  while (TREE_CODE (base_b) == COMPONENT_REF)
235
     base_b = TREE_OPERAND (base_b, 0);
236
 
237
  /* Compare a record/union access (b.c[i] or p->c[i]) and an array access
238
     (a[i]). In case of p->c[i] use alias analysis to verify that p is not
239
     pointing to a.  */
240
  if (TREE_CODE (base_a) == VAR_DECL
241
      && (TREE_CODE (base_b) == VAR_DECL
242
          || (TREE_CODE (base_b) == INDIRECT_REF
243
              && (ptr_decl_may_alias_p (TREE_OPERAND (base_b, 0), base_a, drb,
244
                                        &aliased)
245
                  && !aliased))))
246
    return true;
247
  else
248
    return false;
249
}
250
 
251
 
252
/* Determine if an array access (BASE_A) and a pointer (BASE_B)
253
   are not aliased. Return TRUE if they differ.  */
254
static bool
255
array_ptr_differ_p (tree base_a, tree base_b,
256
                    struct data_reference *drb)
257
{
258
  bool aliased;
259
 
260
  /* In case one of the bases is a pointer (a[i] and (*p)[i]), we check with the
261
     help of alias analysis that p is not pointing to a.  */
262
  if (TREE_CODE (base_a) == VAR_DECL && TREE_CODE (base_b) == INDIRECT_REF
263
      && (ptr_decl_may_alias_p (TREE_OPERAND (base_b, 0), base_a, drb, &aliased)
264
          && !aliased))
265
    return true;
266
  else
267
    return false;
268
}
269
 
270
 
271
/* This is the simplest data dependence test: determines whether the
272
   data references A and B access the same array/region.  Returns
273
   false when the property is not computable at compile time.
274
   Otherwise return true, and DIFFER_P will record the result. This
275
   utility will not be necessary when alias_sets_conflict_p will be
276
   less conservative.  */
277
 
278
static bool
279
base_object_differ_p (struct data_reference *a,
280
                      struct data_reference *b,
281
                      bool *differ_p)
282
{
283
  tree base_a = DR_BASE_OBJECT (a);
284
  tree base_b = DR_BASE_OBJECT (b);
285
  bool aliased;
286
 
287
  if (!base_a || !base_b)
288
    return false;
289
 
290
  /* Determine if same base.  Example: for the array accesses
291
     a[i], b[i] or pointer accesses *a, *b, bases are a, b.  */
292
  if (base_a == base_b)
293
    {
294
      *differ_p = false;
295
      return true;
296
    }
297
 
298
  /* For pointer based accesses, (*p)[i], (*q)[j], the bases are (*p)
299
     and (*q)  */
300
  if (TREE_CODE (base_a) == INDIRECT_REF && TREE_CODE (base_b) == INDIRECT_REF
301
      && TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0))
302
    {
303
      *differ_p = false;
304
      return true;
305
    }
306
 
307
  /* Record/union based accesses - s.a[i], t.b[j]. bases are s.a,t.b.  */
308
  if (TREE_CODE (base_a) == COMPONENT_REF && TREE_CODE (base_b) == COMPONENT_REF
309
      && TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0)
310
      && TREE_OPERAND (base_a, 1) == TREE_OPERAND (base_b, 1))
311
    {
312
      *differ_p = false;
313
      return true;
314
    }
315
 
316
 
317
  /* Determine if different bases.  */
318
 
319
  /* At this point we know that base_a != base_b.  However, pointer
320
     accesses of the form x=(*p) and y=(*q), whose bases are p and q,
321
     may still be pointing to the same base. In SSAed GIMPLE p and q will
322
     be SSA_NAMES in this case.  Therefore, here we check if they are
323
     really two different declarations.  */
324
  if (TREE_CODE (base_a) == VAR_DECL && TREE_CODE (base_b) == VAR_DECL)
325
    {
326
      *differ_p = true;
327
      return true;
328
    }
329
 
330
  /* In case one of the bases is a pointer (a[i] and (*p)[i]), we check with the
331
     help of alias analysis that p is not pointing to a.  */
332
  if (array_ptr_differ_p (base_a, base_b, b)
333
      || array_ptr_differ_p (base_b, base_a, a))
334
    {
335
      *differ_p = true;
336
      return true;
337
    }
338
 
339
  /* If the bases are pointers ((*q)[i] and (*p)[i]), we check with the
340
     help of alias analysis they don't point to the same bases.  */
341
  if (TREE_CODE (base_a) == INDIRECT_REF && TREE_CODE (base_b) == INDIRECT_REF
342
      && (may_alias_p (TREE_OPERAND (base_a, 0), TREE_OPERAND (base_b, 0), a, b,
343
                       &aliased)
344
          && !aliased))
345
    {
346
      *differ_p = true;
347
      return true;
348
    }
349
 
350
  /* Compare two record/union bases s.a and t.b: s != t or (a != b and
351
     s and t are not unions).  */
352
  if (TREE_CODE (base_a) == COMPONENT_REF && TREE_CODE (base_b) == COMPONENT_REF
353
      && ((TREE_CODE (TREE_OPERAND (base_a, 0)) == VAR_DECL
354
           && TREE_CODE (TREE_OPERAND (base_b, 0)) == VAR_DECL
355
           && TREE_OPERAND (base_a, 0) != TREE_OPERAND (base_b, 0))
356
          || (TREE_CODE (TREE_TYPE (TREE_OPERAND (base_a, 0))) == RECORD_TYPE
357
              && TREE_CODE (TREE_TYPE (TREE_OPERAND (base_b, 0))) == RECORD_TYPE
358
              && TREE_OPERAND (base_a, 1) != TREE_OPERAND (base_b, 1))))
359
    {
360
      *differ_p = true;
361
      return true;
362
    }
363
 
364
  /* Compare a record/union access (b.c[i] or p->c[i]) and a pointer
365
     ((*q)[i]).  */
366
  if (record_ptr_differ_p (a, b) || record_ptr_differ_p (b, a))
367
    {
368
      *differ_p = true;
369
      return true;
370
    }
371
 
372
  /* Compare a record/union access (b.c[i] or p->c[i]) and an array access
373
     (a[i]). In case of p->c[i] use alias analysis to verify that p is not
374
     pointing to a.  */
375
  if (record_array_differ_p (a, b) || record_array_differ_p (b, a))
376
    {
377
      *differ_p = true;
378
      return true;
379
    }
380
 
381
  return false;
382
}
383
 
384
/* Function base_addr_differ_p.
385
 
386
   This is the simplest data dependence test: determines whether the
387
   data references DRA and DRB access the same array/region.  Returns
388
   false when the property is not computable at compile time.
389
   Otherwise return true, and DIFFER_P will record the result.
390
 
391
   The algorithm:
392
   1. if (both DRA and DRB are represented as arrays)
393
          compare DRA.BASE_OBJECT and DRB.BASE_OBJECT
394
   2. else if (both DRA and DRB are represented as pointers)
395
          try to prove that DRA.FIRST_LOCATION == DRB.FIRST_LOCATION
396
   3. else if (DRA and DRB are represented differently or 2. fails)
397
          only try to prove that the bases are surely different
398
*/
399
 
400
 
401
static bool
402
base_addr_differ_p (struct data_reference *dra,
403
                    struct data_reference *drb,
404
                    bool *differ_p)
405
{
406
  tree addr_a = DR_BASE_ADDRESS (dra);
407
  tree addr_b = DR_BASE_ADDRESS (drb);
408
  tree type_a, type_b;
409
  bool aliased;
410
 
411
  if (!addr_a || !addr_b)
412
    return false;
413
 
414
  type_a = TREE_TYPE (addr_a);
415
  type_b = TREE_TYPE (addr_b);
416
 
417
  gcc_assert (POINTER_TYPE_P (type_a) &&  POINTER_TYPE_P (type_b));
418
 
419
  /* 1. if (both DRA and DRB are represented as arrays)
420
            compare DRA.BASE_OBJECT and DRB.BASE_OBJECT.  */
421
  if (DR_TYPE (dra) == ARRAY_REF_TYPE && DR_TYPE (drb) == ARRAY_REF_TYPE)
422
    return base_object_differ_p (dra, drb, differ_p);
423
 
424
 
425
  /* 2. else if (both DRA and DRB are represented as pointers)
426
            try to prove that DRA.FIRST_LOCATION == DRB.FIRST_LOCATION.  */
427
  /* If base addresses are the same, we check the offsets, since the access of
428
     the data-ref is described by {base addr + offset} and its access function,
429
     i.e., in order to decide whether the bases of data-refs are the same we
430
     compare both base addresses and offsets.  */
431
  if (DR_TYPE (dra) == POINTER_REF_TYPE && DR_TYPE (drb) == POINTER_REF_TYPE
432
      && (addr_a == addr_b
433
          || (TREE_CODE (addr_a) == ADDR_EXPR && TREE_CODE (addr_b) == ADDR_EXPR
434
              && TREE_OPERAND (addr_a, 0) == TREE_OPERAND (addr_b, 0))))
435
    {
436
      /* Compare offsets.  */
437
      tree offset_a = DR_OFFSET (dra);
438
      tree offset_b = DR_OFFSET (drb);
439
 
440
      STRIP_NOPS (offset_a);
441
      STRIP_NOPS (offset_b);
442
 
443
      /* FORNOW: we only compare offsets that are MULT_EXPR, i.e., we don't handle
444
         PLUS_EXPR.  */
445
      if ((offset_a == offset_b)
446
          || (TREE_CODE (offset_a) == MULT_EXPR
447
              && TREE_CODE (offset_b) == MULT_EXPR
448
              && TREE_OPERAND (offset_a, 0) == TREE_OPERAND (offset_b, 0)
449
              && TREE_OPERAND (offset_a, 1) == TREE_OPERAND (offset_b, 1)))
450
        {
451
          *differ_p = false;
452
          return true;
453
        }
454
    }
455
 
456
  /*  3. else if (DRA and DRB are represented differently or 2. fails)
457
              only try to prove that the bases are surely different.  */
458
 
459
  /* Apply alias analysis.  */
460
  if (may_alias_p (addr_a, addr_b, dra, drb, &aliased) && !aliased)
461
    {
462
      *differ_p = true;
463
      return true;
464
    }
465
 
466
  /* An instruction writing through a restricted pointer is "independent" of any
467
     instruction reading or writing through a different pointer, in the same
468
     block/scope.  */
469
  else if ((TYPE_RESTRICT (type_a) && !DR_IS_READ (dra))
470
      || (TYPE_RESTRICT (type_b) && !DR_IS_READ (drb)))
471
    {
472
      *differ_p = true;
473
      return true;
474
    }
475
  return false;
476
}
477
 
478
 
479
/* Returns true iff A divides B.  */
480
 
481
static inline bool
482
tree_fold_divides_p (tree a,
483
                     tree b)
484
{
485
  /* Determines whether (A == gcd (A, B)).  */
486
  return tree_int_cst_equal (a, tree_fold_gcd (a, b));
487
}
488
 
489
/* Compute the greatest common denominator of two numbers using
490
   Euclid's algorithm.  */
491
 
492
static int
493
gcd (int a, int b)
494
{
495
 
496
  int x, y, z;
497
 
498
  x = abs (a);
499
  y = abs (b);
500
 
501
  while (x>0)
502
    {
503
      z = y % x;
504
      y = x;
505
      x = z;
506
    }
507
 
508
  return (y);
509
}
510
 
511
/* Returns true iff A divides B.  */
512
 
513
static inline bool
514
int_divides_p (int a, int b)
515
{
516
  return ((b % a) == 0);
517
}
518
 
519
 
520
 
521
/* Dump into FILE all the data references from DATAREFS.  */
522
 
523
void
524
dump_data_references (FILE *file,
525
                      varray_type datarefs)
526
{
527
  unsigned int i;
528
 
529
  for (i = 0; i < VARRAY_ACTIVE_SIZE (datarefs); i++)
530
    dump_data_reference (file, VARRAY_GENERIC_PTR (datarefs, i));
531
}
532
 
533
/* Dump into FILE all the dependence relations from DDR.  */
534
 
535
void
536
dump_data_dependence_relations (FILE *file,
537
                                varray_type ddr)
538
{
539
  unsigned int i;
540
 
541
  for (i = 0; i < VARRAY_ACTIVE_SIZE (ddr); i++)
542
    dump_data_dependence_relation (file, VARRAY_GENERIC_PTR (ddr, i));
543
}
544
 
545
/* Dump function for a DATA_REFERENCE structure.  */
546
 
547
void
548
dump_data_reference (FILE *outf,
549
                     struct data_reference *dr)
550
{
551
  unsigned int i;
552
 
553
  fprintf (outf, "(Data Ref: \n  stmt: ");
554
  print_generic_stmt (outf, DR_STMT (dr), 0);
555
  fprintf (outf, "  ref: ");
556
  print_generic_stmt (outf, DR_REF (dr), 0);
557
  fprintf (outf, "  base_name: ");
558
  print_generic_stmt (outf, DR_BASE_OBJECT (dr), 0);
559
 
560
  for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
561
    {
562
      fprintf (outf, "  Access function %d: ", i);
563
      print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
564
    }
565
  fprintf (outf, ")\n");
566
}
567
 
568
/* Dump function for a SUBSCRIPT structure.  */
569
 
570
void
571
dump_subscript (FILE *outf, struct subscript *subscript)
572
{
573
  tree chrec = SUB_CONFLICTS_IN_A (subscript);
574
 
575
  fprintf (outf, "\n (subscript \n");
576
  fprintf (outf, "  iterations_that_access_an_element_twice_in_A: ");
577
  print_generic_stmt (outf, chrec, 0);
578
  if (chrec == chrec_known)
579
    fprintf (outf, "    (no dependence)\n");
580
  else if (chrec_contains_undetermined (chrec))
581
    fprintf (outf, "    (don't know)\n");
582
  else
583
    {
584
      tree last_iteration = SUB_LAST_CONFLICT (subscript);
585
      fprintf (outf, "  last_conflict: ");
586
      print_generic_stmt (outf, last_iteration, 0);
587
    }
588
 
589
  chrec = SUB_CONFLICTS_IN_B (subscript);
590
  fprintf (outf, "  iterations_that_access_an_element_twice_in_B: ");
591
  print_generic_stmt (outf, chrec, 0);
592
  if (chrec == chrec_known)
593
    fprintf (outf, "    (no dependence)\n");
594
  else if (chrec_contains_undetermined (chrec))
595
    fprintf (outf, "    (don't know)\n");
596
  else
597
    {
598
      tree last_iteration = SUB_LAST_CONFLICT (subscript);
599
      fprintf (outf, "  last_conflict: ");
600
      print_generic_stmt (outf, last_iteration, 0);
601
    }
602
 
603
  fprintf (outf, "  (Subscript distance: ");
604
  print_generic_stmt (outf, SUB_DISTANCE (subscript), 0);
605
  fprintf (outf, "  )\n");
606
  fprintf (outf, " )\n");
607
}
608
 
609
/* Dump function for a DATA_DEPENDENCE_RELATION structure.  */
610
 
611
void
612
dump_data_dependence_relation (FILE *outf,
613
                               struct data_dependence_relation *ddr)
614
{
615
  struct data_reference *dra, *drb;
616
 
617
  dra = DDR_A (ddr);
618
  drb = DDR_B (ddr);
619
  fprintf (outf, "(Data Dep: \n");
620
  if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
621
    fprintf (outf, "    (don't know)\n");
622
 
623
  else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
624
    fprintf (outf, "    (no dependence)\n");
625
 
626
  else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
627
    {
628
      unsigned int i;
629
 
630
      for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
631
        {
632
          fprintf (outf, "  access_fn_A: ");
633
          print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
634
          fprintf (outf, "  access_fn_B: ");
635
          print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
636
          dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
637
        }
638
 
639
      for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
640
        {
641
          fprintf (outf, "  distance_vector: ");
642
          print_lambda_vector (outf, DDR_DIST_VECT (ddr, i),
643
                               DDR_SIZE_VECT (ddr));
644
        }
645
 
646
      for (i = 0; i < DDR_NUM_DIR_VECTS (ddr); i++)
647
        {
648
          fprintf (outf, "  direction_vector: ");
649
          print_lambda_vector (outf, DDR_DIR_VECT (ddr, i),
650
                               DDR_SIZE_VECT (ddr));
651
        }
652
    }
653
 
654
  fprintf (outf, ")\n");
655
}
656
 
657
 
658
 
659
/* Dump function for a DATA_DEPENDENCE_DIRECTION structure.  */
660
 
661
void
662
dump_data_dependence_direction (FILE *file,
663
                                enum data_dependence_direction dir)
664
{
665
  switch (dir)
666
    {
667
    case dir_positive:
668
      fprintf (file, "+");
669
      break;
670
 
671
    case dir_negative:
672
      fprintf (file, "-");
673
      break;
674
 
675
    case dir_equal:
676
      fprintf (file, "=");
677
      break;
678
 
679
    case dir_positive_or_negative:
680
      fprintf (file, "+-");
681
      break;
682
 
683
    case dir_positive_or_equal:
684
      fprintf (file, "+=");
685
      break;
686
 
687
    case dir_negative_or_equal:
688
      fprintf (file, "-=");
689
      break;
690
 
691
    case dir_star:
692
      fprintf (file, "*");
693
      break;
694
 
695
    default:
696
      break;
697
    }
698
}
699
 
700
/* Dumps the distance and direction vectors in FILE.  DDRS contains
701
   the dependence relations, and VECT_SIZE is the size of the
702
   dependence vectors, or in other words the number of loops in the
703
   considered nest.  */
704
 
705
void
706
dump_dist_dir_vectors (FILE *file, varray_type ddrs)
707
{
708
  unsigned int i, j;
709
 
710
  for (i = 0; i < VARRAY_ACTIVE_SIZE (ddrs); i++)
711
    {
712
      struct data_dependence_relation *ddr =
713
        (struct data_dependence_relation *)
714
        VARRAY_GENERIC_PTR (ddrs, i);
715
      if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE
716
          && DDR_AFFINE_P (ddr))
717
        {
718
          for (j = 0; j < DDR_NUM_DIST_VECTS (ddr); j++)
719
            {
720
              fprintf (file, "DISTANCE_V (");
721
              print_lambda_vector (file, DDR_DIST_VECT (ddr, j),
722
                                   DDR_SIZE_VECT (ddr));
723
              fprintf (file, ")\n");
724
            }
725
 
726
          for (j = 0; j < DDR_NUM_DIR_VECTS (ddr); j++)
727
            {
728
              fprintf (file, "DIRECTION_V (");
729
              print_lambda_vector (file, DDR_DIR_VECT (ddr, j),
730
                                   DDR_SIZE_VECT (ddr));
731
              fprintf (file, ")\n");
732
            }
733
        }
734
    }
735
  fprintf (file, "\n\n");
736
}
737
 
738
/* Dumps the data dependence relations DDRS in FILE.  */
739
 
740
void
741
dump_ddrs (FILE *file, varray_type ddrs)
742
{
743
  unsigned int i;
744
 
745
  for (i = 0; i < VARRAY_ACTIVE_SIZE (ddrs); i++)
746
    {
747
      struct data_dependence_relation *ddr =
748
        (struct data_dependence_relation *)
749
        VARRAY_GENERIC_PTR (ddrs, i);
750
      dump_data_dependence_relation (file, ddr);
751
    }
752
  fprintf (file, "\n\n");
753
}
754
 
755
 
756
 
757
/* Estimate the number of iterations from the size of the data and the
758
   access functions.  */
759
 
760
static void
761
estimate_niter_from_size_of_data (struct loop *loop,
762
                                  tree opnd0,
763
                                  tree access_fn,
764
                                  tree stmt)
765
{
766
  tree estimation = NULL_TREE;
767
  tree array_size, data_size, element_size;
768
  tree init, step;
769
 
770
  init = initial_condition (access_fn);
771
  step = evolution_part_in_loop_num (access_fn, loop->num);
772
 
773
  array_size = TYPE_SIZE (TREE_TYPE (opnd0));
774
  element_size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (opnd0)));
775
  if (array_size == NULL_TREE
776
      || TREE_CODE (array_size) != INTEGER_CST
777
      || TREE_CODE (element_size) != INTEGER_CST)
778
    return;
779
 
780
  data_size = fold_build2 (EXACT_DIV_EXPR, integer_type_node,
781
                           array_size, element_size);
782
 
783
  if (init != NULL_TREE
784
      && step != NULL_TREE
785
      && TREE_CODE (init) == INTEGER_CST
786
      && TREE_CODE (step) == INTEGER_CST)
787
    {
788
      tree i_plus_s = fold_build2 (PLUS_EXPR, integer_type_node, init, step);
789
      tree sign = fold_build2 (GT_EXPR, boolean_type_node, i_plus_s, init);
790
 
791
      if (sign == boolean_true_node)
792
        estimation = fold_build2 (CEIL_DIV_EXPR, integer_type_node,
793
                                  fold_build2 (MINUS_EXPR, integer_type_node,
794
                                               data_size, init), step);
795
 
796
      /* When the step is negative, as in PR23386: (init = 3, step =
797
         0ffffffff, data_size = 100), we have to compute the
798
         estimation as ceil_div (init, 0 - step) + 1.  */
799
      else if (sign == boolean_false_node)
800
        estimation =
801
          fold_build2 (PLUS_EXPR, integer_type_node,
802
                       fold_build2 (CEIL_DIV_EXPR, integer_type_node,
803
                                    init,
804
                                    fold_build2 (MINUS_EXPR, unsigned_type_node,
805
                                                 integer_zero_node, step)),
806
                       integer_one_node);
807
 
808
      if (estimation)
809
        record_estimate (loop, estimation, boolean_true_node, stmt);
810
    }
811
}
812
 
813
/* Given an ARRAY_REF node REF, records its access functions.
814
   Example: given A[i][3], record in ACCESS_FNS the opnd1 function,
815
   i.e. the constant "3", then recursively call the function on opnd0,
816
   i.e. the ARRAY_REF "A[i]".
817
   If ESTIMATE_ONLY is true, we just set the estimated number of loop
818
   iterations, we don't store the access function.
819
   The function returns the base name: "A".  */
820
 
821
static tree
822
analyze_array_indexes (struct loop *loop,
823
                       VEC(tree,heap) **access_fns,
824
                       tree ref, tree stmt,
825
                       bool estimate_only)
826
{
827
  tree opnd0, opnd1;
828
  tree access_fn;
829
 
830
  opnd0 = TREE_OPERAND (ref, 0);
831
  opnd1 = TREE_OPERAND (ref, 1);
832
 
833
  /* The detection of the evolution function for this data access is
834
     postponed until the dependence test.  This lazy strategy avoids
835
     the computation of access functions that are of no interest for
836
     the optimizers.  */
837
  access_fn = instantiate_parameters
838
    (loop, analyze_scalar_evolution (loop, opnd1));
839
 
840
  if (estimate_only
841
      && chrec_contains_undetermined (loop->estimated_nb_iterations))
842
    estimate_niter_from_size_of_data (loop, opnd0, access_fn, stmt);
843
 
844
  if (!estimate_only)
845
    VEC_safe_push (tree, heap, *access_fns, access_fn);
846
 
847
  /* Recursively record other array access functions.  */
848
  if (TREE_CODE (opnd0) == ARRAY_REF)
849
    return analyze_array_indexes (loop, access_fns, opnd0, stmt, estimate_only);
850
 
851
  /* Return the base name of the data access.  */
852
  else
853
    return opnd0;
854
}
855
 
856
/* For an array reference REF contained in STMT, attempt to bound the
857
   number of iterations in the loop containing STMT  */
858
 
859
void
860
estimate_iters_using_array (tree stmt, tree ref)
861
{
862
  analyze_array_indexes (loop_containing_stmt (stmt), NULL, ref, stmt,
863
                         true);
864
}
865
 
866
/* For a data reference REF contained in the statement STMT, initialize
867
   a DATA_REFERENCE structure, and return it.  IS_READ flag has to be
868
   set to true when REF is in the right hand side of an
869
   assignment.  */
870
 
871
struct data_reference *
872
analyze_array (tree stmt, tree ref, bool is_read)
873
{
874
  struct data_reference *res;
875
  VEC(tree,heap) *acc_fns;
876
 
877
  if (dump_file && (dump_flags & TDF_DETAILS))
878
    {
879
      fprintf (dump_file, "(analyze_array \n");
880
      fprintf (dump_file, "  (ref = ");
881
      print_generic_stmt (dump_file, ref, 0);
882
      fprintf (dump_file, ")\n");
883
    }
884
 
885
  res = xmalloc (sizeof (struct data_reference));
886
 
887
  DR_STMT (res) = stmt;
888
  DR_REF (res) = ref;
889
  acc_fns = VEC_alloc (tree, heap, 3);
890
  DR_BASE_OBJECT (res) = analyze_array_indexes
891
    (loop_containing_stmt (stmt), &acc_fns, ref, stmt, false);
892
  DR_TYPE (res) = ARRAY_REF_TYPE;
893
  DR_SET_ACCESS_FNS (res, acc_fns);
894
  DR_IS_READ (res) = is_read;
895
  DR_BASE_ADDRESS (res) = NULL_TREE;
896
  DR_OFFSET (res) = NULL_TREE;
897
  DR_INIT (res) = NULL_TREE;
898
  DR_STEP (res) = NULL_TREE;
899
  DR_OFFSET_MISALIGNMENT (res) = NULL_TREE;
900
  DR_MEMTAG (res) = NULL_TREE;
901
  DR_PTR_INFO (res) = NULL;
902
 
903
  if (dump_file && (dump_flags & TDF_DETAILS))
904
    fprintf (dump_file, ")\n");
905
 
906
  return res;
907
}
908
 
909
 
910
/* Analyze an indirect memory reference, REF, that comes from STMT.
911
   IS_READ is true if this is an indirect load, and false if it is
912
   an indirect store.
913
   Return a new data reference structure representing the indirect_ref, or
914
   NULL if we cannot describe the access function.  */
915
 
916
static struct data_reference *
917
analyze_indirect_ref (tree stmt, tree ref, bool is_read)
918
{
919
  struct loop *loop = loop_containing_stmt (stmt);
920
  tree ptr_ref = TREE_OPERAND (ref, 0);
921
  tree access_fn = analyze_scalar_evolution (loop, ptr_ref);
922
  tree init = initial_condition_in_loop_num (access_fn, loop->num);
923
  tree base_address = NULL_TREE, evolution, step = NULL_TREE;
924
  struct ptr_info_def *ptr_info = NULL;
925
 
926
  if (TREE_CODE (ptr_ref) == SSA_NAME)
927
    ptr_info = SSA_NAME_PTR_INFO (ptr_ref);
928
 
929
  STRIP_NOPS (init);
930
  if (access_fn == chrec_dont_know || !init || init == chrec_dont_know)
931
    {
932
      if (dump_file && (dump_flags & TDF_DETAILS))
933
        {
934
          fprintf (dump_file, "\nBad access function of ptr: ");
935
          print_generic_expr (dump_file, ref, TDF_SLIM);
936
          fprintf (dump_file, "\n");
937
        }
938
      return NULL;
939
    }
940
 
941
  if (dump_file && (dump_flags & TDF_DETAILS))
942
    {
943
      fprintf (dump_file, "\nAccess function of ptr: ");
944
      print_generic_expr (dump_file, access_fn, TDF_SLIM);
945
      fprintf (dump_file, "\n");
946
    }
947
 
948
  if (!expr_invariant_in_loop_p (loop, init))
949
    {
950
    if (dump_file && (dump_flags & TDF_DETAILS))
951
        fprintf (dump_file, "\ninitial condition is not loop invariant.\n");
952
    }
953
  else
954
    {
955
      base_address = init;
956
      evolution = evolution_part_in_loop_num (access_fn, loop->num);
957
      if (evolution != chrec_dont_know)
958
        {
959
          if (!evolution)
960
            step = ssize_int (0);
961
          else
962
            {
963
              if (TREE_CODE (evolution) == INTEGER_CST)
964
                step = fold_convert (ssizetype, evolution);
965
              else
966
                if (dump_file && (dump_flags & TDF_DETAILS))
967
                  fprintf (dump_file, "\nnon constant step for ptr access.\n");
968
            }
969
        }
970
      else
971
        if (dump_file && (dump_flags & TDF_DETAILS))
972
          fprintf (dump_file, "\nunknown evolution of ptr.\n");
973
    }
974
  return init_data_ref (stmt, ref, NULL_TREE, access_fn, is_read, base_address,
975
                        NULL_TREE, step, NULL_TREE, NULL_TREE,
976
                        ptr_info, POINTER_REF_TYPE);
977
}
978
 
979
/* For a data reference REF contained in the statement STMT, initialize
980
   a DATA_REFERENCE structure, and return it.  */
981
 
982
struct data_reference *
983
init_data_ref (tree stmt,
984
               tree ref,
985
               tree base,
986
               tree access_fn,
987
               bool is_read,
988
               tree base_address,
989
               tree init_offset,
990
               tree step,
991
               tree misalign,
992
               tree memtag,
993
               struct ptr_info_def *ptr_info,
994
               enum data_ref_type type)
995
{
996
  struct data_reference *res;
997
  VEC(tree,heap) *acc_fns;
998
 
999
  if (dump_file && (dump_flags & TDF_DETAILS))
1000
    {
1001
      fprintf (dump_file, "(init_data_ref \n");
1002
      fprintf (dump_file, "  (ref = ");
1003
      print_generic_stmt (dump_file, ref, 0);
1004
      fprintf (dump_file, ")\n");
1005
    }
1006
 
1007
  res = xmalloc (sizeof (struct data_reference));
1008
 
1009
  DR_STMT (res) = stmt;
1010
  DR_REF (res) = ref;
1011
  DR_BASE_OBJECT (res) = base;
1012
  DR_TYPE (res) = type;
1013
  acc_fns = VEC_alloc (tree, heap, 3);
1014
  DR_SET_ACCESS_FNS (res, acc_fns);
1015
  VEC_quick_push (tree, DR_ACCESS_FNS (res), access_fn);
1016
  DR_IS_READ (res) = is_read;
1017
  DR_BASE_ADDRESS (res) = base_address;
1018
  DR_OFFSET (res) = init_offset;
1019
  DR_INIT (res) = NULL_TREE;
1020
  DR_STEP (res) = step;
1021
  DR_OFFSET_MISALIGNMENT (res) = misalign;
1022
  DR_MEMTAG (res) = memtag;
1023
  DR_PTR_INFO (res) = ptr_info;
1024
 
1025
  if (dump_file && (dump_flags & TDF_DETAILS))
1026
    fprintf (dump_file, ")\n");
1027
 
1028
  return res;
1029
}
1030
 
1031
 
1032
 
1033
/* Function strip_conversions
1034
 
1035
   Strip conversions that don't narrow the mode.  */
1036
 
1037
static tree
1038
strip_conversion (tree expr)
1039
{
1040
  tree to, ti, oprnd0;
1041
 
1042
  while (TREE_CODE (expr) == NOP_EXPR || TREE_CODE (expr) == CONVERT_EXPR)
1043
    {
1044
      to = TREE_TYPE (expr);
1045
      oprnd0 = TREE_OPERAND (expr, 0);
1046
      ti = TREE_TYPE (oprnd0);
1047
 
1048
      if (!INTEGRAL_TYPE_P (to) || !INTEGRAL_TYPE_P (ti))
1049
        return NULL_TREE;
1050
      if (GET_MODE_SIZE (TYPE_MODE (to)) < GET_MODE_SIZE (TYPE_MODE (ti)))
1051
        return NULL_TREE;
1052
 
1053
      expr = oprnd0;
1054
    }
1055
  return expr;
1056
}
1057
 
1058
 
1059
/* Function analyze_offset_expr
1060
 
1061
   Given an offset expression EXPR received from get_inner_reference, analyze
1062
   it and create an expression for INITIAL_OFFSET by substituting the variables
1063
   of EXPR with initial_condition of the corresponding access_fn in the loop.
1064
   E.g.,
1065
      for i
1066
         for (j = 3; j < N; j++)
1067
            a[j].b[i][j] = 0;
1068
 
1069
   For a[j].b[i][j], EXPR will be 'i * C_i + j * C_j + C'. 'i' cannot be
1070
   substituted, since its access_fn in the inner loop is i. 'j' will be
1071
   substituted with 3. An INITIAL_OFFSET will be 'i * C_i + C`', where
1072
   C` =  3 * C_j + C.
1073
 
1074
   Compute MISALIGN (the misalignment of the data reference initial access from
1075
   its base). Misalignment can be calculated only if all the variables can be
1076
   substituted with constants, otherwise, we record maximum possible alignment
1077
   in ALIGNED_TO. In the above example, since 'i' cannot be substituted, MISALIGN
1078
   will be NULL_TREE, and the biggest divider of C_i (a power of 2) will be
1079
   recorded in ALIGNED_TO.
1080
 
1081
   STEP is an evolution of the data reference in this loop in bytes.
1082
   In the above example, STEP is C_j.
1083
 
1084
   Return FALSE, if the analysis fails, e.g., there is no access_fn for a
1085
   variable. In this case, all the outputs (INITIAL_OFFSET, MISALIGN, ALIGNED_TO
1086
   and STEP) are NULL_TREEs. Otherwise, return TRUE.
1087
 
1088
*/
1089
 
1090
static bool
1091
analyze_offset_expr (tree expr,
1092
                     struct loop *loop,
1093
                     tree *initial_offset,
1094
                     tree *misalign,
1095
                     tree *aligned_to,
1096
                     tree *step)
1097
{
1098
  tree oprnd0;
1099
  tree oprnd1;
1100
  tree left_offset = ssize_int (0);
1101
  tree right_offset = ssize_int (0);
1102
  tree left_misalign = ssize_int (0);
1103
  tree right_misalign = ssize_int (0);
1104
  tree left_step = ssize_int (0);
1105
  tree right_step = ssize_int (0);
1106
  enum tree_code code;
1107
  tree init, evolution;
1108
  tree left_aligned_to = NULL_TREE, right_aligned_to = NULL_TREE;
1109
 
1110
  *step = NULL_TREE;
1111
  *misalign = NULL_TREE;
1112
  *aligned_to = NULL_TREE;
1113
  *initial_offset = NULL_TREE;
1114
 
1115
  /* Strip conversions that don't narrow the mode.  */
1116
  expr = strip_conversion (expr);
1117
  if (!expr)
1118
    return false;
1119
 
1120
  /* Stop conditions:
1121
     1. Constant.  */
1122
  if (TREE_CODE (expr) == INTEGER_CST)
1123
    {
1124
      *initial_offset = fold_convert (ssizetype, expr);
1125
      *misalign = fold_convert (ssizetype, expr);
1126
      *step = ssize_int (0);
1127
      return true;
1128
    }
1129
 
1130
  /* 2. Variable. Try to substitute with initial_condition of the corresponding
1131
     access_fn in the current loop.  */
1132
  if (SSA_VAR_P (expr))
1133
    {
1134
      tree access_fn = analyze_scalar_evolution (loop, expr);
1135
 
1136
      if (access_fn == chrec_dont_know)
1137
        /* No access_fn.  */
1138
        return false;
1139
 
1140
      init = initial_condition_in_loop_num (access_fn, loop->num);
1141
      if (!expr_invariant_in_loop_p (loop, init))
1142
        /* Not enough information: may be not loop invariant.
1143
           E.g., for a[b[i]], we get a[D], where D=b[i]. EXPR is D, its
1144
           initial_condition is D, but it depends on i - loop's induction
1145
           variable.  */
1146
        return false;
1147
 
1148
      evolution = evolution_part_in_loop_num (access_fn, loop->num);
1149
      if (evolution && TREE_CODE (evolution) != INTEGER_CST)
1150
        /* Evolution is not constant.  */
1151
        return false;
1152
 
1153
      if (TREE_CODE (init) == INTEGER_CST)
1154
        *misalign = fold_convert (ssizetype, init);
1155
      else
1156
        /* Not constant, misalignment cannot be calculated.  */
1157
        *misalign = NULL_TREE;
1158
 
1159
      *initial_offset = fold_convert (ssizetype, init);
1160
 
1161
      *step = evolution ? fold_convert (ssizetype, evolution) : ssize_int (0);
1162
      return true;
1163
    }
1164
 
1165
  /* Recursive computation.  */
1166
  if (!BINARY_CLASS_P (expr))
1167
    {
1168
      /* We expect to get binary expressions (PLUS/MINUS and MULT).  */
1169
      if (dump_file && (dump_flags & TDF_DETAILS))
1170
        {
1171
          fprintf (dump_file, "\nNot binary expression ");
1172
          print_generic_expr (dump_file, expr, TDF_SLIM);
1173
          fprintf (dump_file, "\n");
1174
        }
1175
      return false;
1176
    }
1177
  oprnd0 = TREE_OPERAND (expr, 0);
1178
  oprnd1 = TREE_OPERAND (expr, 1);
1179
 
1180
  if (!analyze_offset_expr (oprnd0, loop, &left_offset, &left_misalign,
1181
                            &left_aligned_to, &left_step)
1182
      || !analyze_offset_expr (oprnd1, loop, &right_offset, &right_misalign,
1183
                               &right_aligned_to, &right_step))
1184
    return false;
1185
 
1186
  /* The type of the operation: plus, minus or mult.  */
1187
  code = TREE_CODE (expr);
1188
  switch (code)
1189
    {
1190
    case MULT_EXPR:
1191
      if (TREE_CODE (right_offset) != INTEGER_CST)
1192
        /* RIGHT_OFFSET can be not constant. For example, for arrays of variable
1193
           sized types.
1194
           FORNOW: We don't support such cases.  */
1195
        return false;
1196
 
1197
      /* Strip conversions that don't narrow the mode.  */
1198
      left_offset = strip_conversion (left_offset);
1199
      if (!left_offset)
1200
        return false;
1201
      /* Misalignment computation.  */
1202
      if (SSA_VAR_P (left_offset))
1203
        {
1204
          /* If the left side contains variables that can't be substituted with
1205
             constants, the misalignment is unknown. However, if the right side
1206
             is a multiple of some alignment, we know that the expression is
1207
             aligned to it. Therefore, we record such maximum possible value.
1208
           */
1209
          *misalign = NULL_TREE;
1210
          *aligned_to = ssize_int (highest_pow2_factor (right_offset));
1211
        }
1212
      else
1213
        {
1214
          /* The left operand was successfully substituted with constant.  */
1215
          if (left_misalign)
1216
            {
1217
              /* In case of EXPR '(i * C1 + j) * C2', LEFT_MISALIGN is
1218
                 NULL_TREE.  */
1219
              *misalign  = size_binop (code, left_misalign, right_misalign);
1220
              if (left_aligned_to && right_aligned_to)
1221
                *aligned_to = size_binop (MIN_EXPR, left_aligned_to,
1222
                                          right_aligned_to);
1223
              else
1224
                *aligned_to = left_aligned_to ?
1225
                  left_aligned_to : right_aligned_to;
1226
            }
1227
          else
1228
            *misalign = NULL_TREE;
1229
        }
1230
 
1231
      /* Step calculation.  */
1232
      /* Multiply the step by the right operand.  */
1233
      *step  = size_binop (MULT_EXPR, left_step, right_offset);
1234
      break;
1235
 
1236
    case PLUS_EXPR:
1237
    case MINUS_EXPR:
1238
      /* Combine the recursive calculations for step and misalignment.  */
1239
      *step = size_binop (code, left_step, right_step);
1240
 
1241
      /* Unknown alignment.  */
1242
      if ((!left_misalign && !left_aligned_to)
1243
          || (!right_misalign && !right_aligned_to))
1244
        {
1245
          *misalign = NULL_TREE;
1246
          *aligned_to = NULL_TREE;
1247
          break;
1248
        }
1249
 
1250
      if (left_misalign && right_misalign)
1251
        *misalign = size_binop (code, left_misalign, right_misalign);
1252
      else
1253
        *misalign = left_misalign ? left_misalign : right_misalign;
1254
 
1255
      if (left_aligned_to && right_aligned_to)
1256
        *aligned_to = size_binop (MIN_EXPR, left_aligned_to, right_aligned_to);
1257
      else
1258
        *aligned_to = left_aligned_to ? left_aligned_to : right_aligned_to;
1259
 
1260
      break;
1261
 
1262
    default:
1263
      gcc_unreachable ();
1264
    }
1265
 
1266
  /* Compute offset.  */
1267
  *initial_offset = fold_convert (ssizetype,
1268
                                  fold_build2 (code, TREE_TYPE (left_offset),
1269
                                               left_offset,
1270
                                               right_offset));
1271
  return true;
1272
}
1273
 
1274
/* Function address_analysis
1275
 
1276
   Return the BASE of the address expression EXPR.
1277
   Also compute the OFFSET from BASE, MISALIGN and STEP.
1278
 
1279
   Input:
1280
   EXPR - the address expression that is being analyzed
1281
   STMT - the statement that contains EXPR or its original memory reference
1282
   IS_READ - TRUE if STMT reads from EXPR, FALSE if writes to EXPR
1283
   DR - data_reference struct for the original memory reference
1284
 
1285
   Output:
1286
   BASE (returned value) - the base of the data reference EXPR.
1287
   INITIAL_OFFSET - initial offset of EXPR from BASE (an expression)
1288
   MISALIGN - offset of EXPR from BASE in bytes (a constant) or NULL_TREE if the
1289
              computation is impossible
1290
   ALIGNED_TO - maximum alignment of EXPR or NULL_TREE if MISALIGN can be
1291
                calculated (doesn't depend on variables)
1292
   STEP - evolution of EXPR in the loop
1293
 
1294
   If something unexpected is encountered (an unsupported form of data-ref),
1295
   then NULL_TREE is returned.
1296
 */
1297
 
1298
static tree
1299
address_analysis (tree expr, tree stmt, bool is_read, struct data_reference *dr,
1300
                  tree *offset, tree *misalign, tree *aligned_to, tree *step)
1301
{
1302
  tree oprnd0, oprnd1, base_address, offset_expr, base_addr0, base_addr1;
1303
  tree address_offset = ssize_int (0), address_misalign = ssize_int (0);
1304
  tree dummy, address_aligned_to = NULL_TREE;
1305
  struct ptr_info_def *dummy1;
1306
  subvar_t dummy2;
1307
 
1308
  switch (TREE_CODE (expr))
1309
    {
1310
    case PLUS_EXPR:
1311
    case MINUS_EXPR:
1312
      /* EXPR is of form {base +/- offset} (or {offset +/- base}).  */
1313
      oprnd0 = TREE_OPERAND (expr, 0);
1314
      oprnd1 = TREE_OPERAND (expr, 1);
1315
 
1316
      STRIP_NOPS (oprnd0);
1317
      STRIP_NOPS (oprnd1);
1318
 
1319
      /* Recursively try to find the base of the address contained in EXPR.
1320
         For offset, the returned base will be NULL.  */
1321
      base_addr0 = address_analysis (oprnd0, stmt, is_read, dr, &address_offset,
1322
                                     &address_misalign, &address_aligned_to,
1323
                                     step);
1324
 
1325
      base_addr1 = address_analysis (oprnd1, stmt, is_read,  dr, &address_offset,
1326
                                     &address_misalign, &address_aligned_to,
1327
                                     step);
1328
 
1329
      /* We support cases where only one of the operands contains an
1330
         address.  */
1331
      if ((base_addr0 && base_addr1) || (!base_addr0 && !base_addr1))
1332
        {
1333
          if (dump_file && (dump_flags & TDF_DETAILS))
1334
            {
1335
              fprintf (dump_file,
1336
                    "\neither more than one address or no addresses in expr ");
1337
              print_generic_expr (dump_file, expr, TDF_SLIM);
1338
              fprintf (dump_file, "\n");
1339
            }
1340
          return NULL_TREE;
1341
        }
1342
 
1343
      /* To revert STRIP_NOPS.  */
1344
      oprnd0 = TREE_OPERAND (expr, 0);
1345
      oprnd1 = TREE_OPERAND (expr, 1);
1346
 
1347
      offset_expr = base_addr0 ?
1348
        fold_convert (ssizetype, oprnd1) : fold_convert (ssizetype, oprnd0);
1349
 
1350
      /* EXPR is of form {base +/- offset} (or {offset +/- base}). If offset is
1351
         a number, we can add it to the misalignment value calculated for base,
1352
         otherwise, misalignment is NULL.  */
1353
      if (TREE_CODE (offset_expr) == INTEGER_CST && address_misalign)
1354
        {
1355
          *misalign = size_binop (TREE_CODE (expr), address_misalign,
1356
                                  offset_expr);
1357
          *aligned_to = address_aligned_to;
1358
        }
1359
      else
1360
        {
1361
          *misalign = NULL_TREE;
1362
          *aligned_to = NULL_TREE;
1363
        }
1364
 
1365
      /* Combine offset (from EXPR {base + offset}) with the offset calculated
1366
         for base.  */
1367
      *offset = size_binop (TREE_CODE (expr), address_offset, offset_expr);
1368
      return base_addr0 ? base_addr0 : base_addr1;
1369
 
1370
    case ADDR_EXPR:
1371
      base_address = object_analysis (TREE_OPERAND (expr, 0), stmt, is_read,
1372
                                      &dr, offset, misalign, aligned_to, step,
1373
                                      &dummy, &dummy1, &dummy2);
1374
      return base_address;
1375
 
1376
    case SSA_NAME:
1377
      if (!POINTER_TYPE_P (TREE_TYPE (expr)))
1378
        {
1379
          if (dump_file && (dump_flags & TDF_DETAILS))
1380
            {
1381
              fprintf (dump_file, "\nnot pointer SSA_NAME ");
1382
              print_generic_expr (dump_file, expr, TDF_SLIM);
1383
              fprintf (dump_file, "\n");
1384
            }
1385
          return NULL_TREE;
1386
        }
1387
      *aligned_to = ssize_int (TYPE_ALIGN_UNIT (TREE_TYPE (TREE_TYPE (expr))));
1388
      *misalign = ssize_int (0);
1389
      *offset = ssize_int (0);
1390
      *step = ssize_int (0);
1391
      return expr;
1392
 
1393
    default:
1394
      return NULL_TREE;
1395
    }
1396
}
1397
 
1398
 
1399
/* Function object_analysis
1400
 
1401
   Create a data-reference structure DR for MEMREF.
1402
   Return the BASE of the data reference MEMREF if the analysis is possible.
1403
   Also compute the INITIAL_OFFSET from BASE, MISALIGN and STEP.
1404
   E.g., for EXPR a.b[i] + 4B, BASE is a, and OFFSET is the overall offset
1405
   'a.b[i] + 4B' from a (can be an expression), MISALIGN is an OFFSET
1406
   instantiated with initial_conditions of access_functions of variables,
1407
   and STEP is the evolution of the DR_REF in this loop.
1408
 
1409
   Function get_inner_reference is used for the above in case of ARRAY_REF and
1410
   COMPONENT_REF.
1411
 
1412
   The structure of the function is as follows:
1413
   Part 1:
1414
   Case 1. For handled_component_p refs
1415
          1.1 build data-reference structure for MEMREF
1416
          1.2 call get_inner_reference
1417
            1.2.1 analyze offset expr received from get_inner_reference
1418
          (fall through with BASE)
1419
   Case 2. For declarations
1420
          2.1 set MEMTAG
1421
   Case 3. For INDIRECT_REFs
1422
          3.1 build data-reference structure for MEMREF
1423
          3.2 analyze evolution and initial condition of MEMREF
1424
          3.3 set data-reference structure for MEMREF
1425
          3.4 call address_analysis to analyze INIT of the access function
1426
          3.5 extract memory tag
1427
 
1428
   Part 2:
1429
   Combine the results of object and address analysis to calculate
1430
   INITIAL_OFFSET, STEP and misalignment info.
1431
 
1432
   Input:
1433
   MEMREF - the memory reference that is being analyzed
1434
   STMT - the statement that contains MEMREF
1435
   IS_READ - TRUE if STMT reads from MEMREF, FALSE if writes to MEMREF
1436
 
1437
   Output:
1438
   BASE_ADDRESS (returned value) - the base address of the data reference MEMREF
1439
                                   E.g, if MEMREF is a.b[k].c[i][j] the returned
1440
                                   base is &a.
1441
   DR - data_reference struct for MEMREF
1442
   INITIAL_OFFSET - initial offset of MEMREF from BASE (an expression)
1443
   MISALIGN - offset of MEMREF from BASE in bytes (a constant) modulo alignment of
1444
              ALIGNMENT or NULL_TREE if the computation is impossible
1445
   ALIGNED_TO - maximum alignment of EXPR or NULL_TREE if MISALIGN can be
1446
                calculated (doesn't depend on variables)
1447
   STEP - evolution of the DR_REF in the loop
1448
   MEMTAG - memory tag for aliasing purposes
1449
   PTR_INFO - NULL or points-to aliasing info from a pointer SSA_NAME
1450
   SUBVARS - Sub-variables of the variable
1451
 
1452
   If the analysis of MEMREF evolution in the loop fails, NULL_TREE is returned,
1453
   but DR can be created anyway.
1454
 
1455
*/
1456
 
1457
static tree
1458
object_analysis (tree memref, tree stmt, bool is_read,
1459
                 struct data_reference **dr, tree *offset, tree *misalign,
1460
                 tree *aligned_to, tree *step, tree *memtag,
1461
                 struct ptr_info_def **ptr_info, subvar_t *subvars)
1462
{
1463
  tree base = NULL_TREE, base_address = NULL_TREE;
1464
  tree object_offset = ssize_int (0), object_misalign = ssize_int (0);
1465
  tree object_step = ssize_int (0), address_step = ssize_int (0);
1466
  tree address_offset = ssize_int (0), address_misalign = ssize_int (0);
1467
  HOST_WIDE_INT pbitsize, pbitpos;
1468
  tree poffset, bit_pos_in_bytes;
1469
  enum machine_mode pmode;
1470
  int punsignedp, pvolatilep;
1471
  tree ptr_step = ssize_int (0), ptr_init = NULL_TREE;
1472
  struct loop *loop = loop_containing_stmt (stmt);
1473
  struct data_reference *ptr_dr = NULL;
1474
  tree object_aligned_to = NULL_TREE, address_aligned_to = NULL_TREE;
1475
 
1476
 *ptr_info = NULL;
1477
 
1478
  /* Part 1:  */
1479
  /* Case 1. handled_component_p refs.  */
1480
  if (handled_component_p (memref))
1481
    {
1482
      /* 1.1 build data-reference structure for MEMREF.  */
1483
      /* TODO: handle COMPONENT_REFs.  */
1484
      if (!(*dr))
1485
        {
1486
          if (TREE_CODE (memref) == ARRAY_REF)
1487
            *dr = analyze_array (stmt, memref, is_read);
1488
          else
1489
            {
1490
              /* FORNOW.  */
1491
              if (dump_file && (dump_flags & TDF_DETAILS))
1492
                {
1493
                  fprintf (dump_file, "\ndata-ref of unsupported type ");
1494
                  print_generic_expr (dump_file, memref, TDF_SLIM);
1495
                  fprintf (dump_file, "\n");
1496
                }
1497
              return NULL_TREE;
1498
            }
1499
        }
1500
 
1501
      /* 1.2 call get_inner_reference.  */
1502
      /* Find the base and the offset from it.  */
1503
      base = get_inner_reference (memref, &pbitsize, &pbitpos, &poffset,
1504
                                  &pmode, &punsignedp, &pvolatilep, false);
1505
      if (!base)
1506
        {
1507
          if (dump_file && (dump_flags & TDF_DETAILS))
1508
            {
1509
              fprintf (dump_file, "\nfailed to get inner ref for ");
1510
              print_generic_expr (dump_file, memref, TDF_SLIM);
1511
              fprintf (dump_file, "\n");
1512
            }
1513
          return NULL_TREE;
1514
        }
1515
 
1516
      /* 1.2.1 analyze offset expr received from get_inner_reference.  */
1517
      if (poffset
1518
          && !analyze_offset_expr (poffset, loop, &object_offset,
1519
                                   &object_misalign, &object_aligned_to,
1520
                                   &object_step))
1521
        {
1522
          if (dump_file && (dump_flags & TDF_DETAILS))
1523
            {
1524
              fprintf (dump_file, "\nfailed to compute offset or step for ");
1525
              print_generic_expr (dump_file, memref, TDF_SLIM);
1526
              fprintf (dump_file, "\n");
1527
            }
1528
          return NULL_TREE;
1529
        }
1530
 
1531
      /* Add bit position to OFFSET and MISALIGN.  */
1532
 
1533
      bit_pos_in_bytes = ssize_int (pbitpos/BITS_PER_UNIT);
1534
      /* Check that there is no remainder in bits.  */
1535
      if (pbitpos%BITS_PER_UNIT)
1536
        {
1537
          if (dump_file && (dump_flags & TDF_DETAILS))
1538
            fprintf (dump_file, "\nbit offset alignment.\n");
1539
          return NULL_TREE;
1540
        }
1541
      object_offset = size_binop (PLUS_EXPR, bit_pos_in_bytes, object_offset);
1542
      if (object_misalign)
1543
        object_misalign = size_binop (PLUS_EXPR, object_misalign,
1544
                                      bit_pos_in_bytes);
1545
 
1546
      memref = base; /* To continue analysis of BASE.  */
1547
      /* fall through  */
1548
    }
1549
 
1550
  /*  Part 1: Case 2. Declarations.  */
1551
  if (DECL_P (memref))
1552
    {
1553
      /* We expect to get a decl only if we already have a DR.  */
1554
      if (!(*dr))
1555
        {
1556
          if (dump_file && (dump_flags & TDF_DETAILS))
1557
            {
1558
              fprintf (dump_file, "\nunhandled decl ");
1559
              print_generic_expr (dump_file, memref, TDF_SLIM);
1560
              fprintf (dump_file, "\n");
1561
            }
1562
          return NULL_TREE;
1563
        }
1564
 
1565
      /* TODO: if during the analysis of INDIRECT_REF we get to an object, put
1566
         the object in BASE_OBJECT field if we can prove that this is O.K.,
1567
         i.e., the data-ref access is bounded by the bounds of the BASE_OBJECT.
1568
         (e.g., if the object is an array base 'a', where 'a[N]', we must prove
1569
         that every access with 'p' (the original INDIRECT_REF based on '&a')
1570
         in the loop is within the array boundaries - from a[0] to a[N-1]).
1571
         Otherwise, our alias analysis can be incorrect.
1572
         Even if an access function based on BASE_OBJECT can't be build, update
1573
         BASE_OBJECT field to enable us to prove that two data-refs are
1574
         different (without access function, distance analysis is impossible).
1575
      */
1576
     if (SSA_VAR_P (memref) && var_can_have_subvars (memref))
1577
        *subvars = get_subvars_for_var (memref);
1578
      base_address = build_fold_addr_expr (memref);
1579
      /* 2.1 set MEMTAG.  */
1580
      *memtag = memref;
1581
    }
1582
 
1583
  /* Part 1:  Case 3. INDIRECT_REFs.  */
1584
  else if (TREE_CODE (memref) == INDIRECT_REF)
1585
    {
1586
      tree ptr_ref = TREE_OPERAND (memref, 0);
1587
      if (TREE_CODE (ptr_ref) == SSA_NAME)
1588
        *ptr_info = SSA_NAME_PTR_INFO (ptr_ref);
1589
 
1590
      /* 3.1 build data-reference structure for MEMREF.  */
1591
      ptr_dr = analyze_indirect_ref (stmt, memref, is_read);
1592
      if (!ptr_dr)
1593
        {
1594
          if (dump_file && (dump_flags & TDF_DETAILS))
1595
            {
1596
              fprintf (dump_file, "\nfailed to create dr for ");
1597
              print_generic_expr (dump_file, memref, TDF_SLIM);
1598
              fprintf (dump_file, "\n");
1599
            }
1600
          return NULL_TREE;
1601
        }
1602
 
1603
      /* 3.2 analyze evolution and initial condition of MEMREF.  */
1604
      ptr_step = DR_STEP (ptr_dr);
1605
      ptr_init = DR_BASE_ADDRESS (ptr_dr);
1606
      if (!ptr_init || !ptr_step || !POINTER_TYPE_P (TREE_TYPE (ptr_init)))
1607
        {
1608
          *dr = (*dr) ? *dr : ptr_dr;
1609
          if (dump_file && (dump_flags & TDF_DETAILS))
1610
            {
1611
              fprintf (dump_file, "\nbad pointer access ");
1612
              print_generic_expr (dump_file, memref, TDF_SLIM);
1613
              fprintf (dump_file, "\n");
1614
            }
1615
          return NULL_TREE;
1616
        }
1617
 
1618
      if (integer_zerop (ptr_step) && !(*dr))
1619
        {
1620
          if (dump_file && (dump_flags & TDF_DETAILS))
1621
            fprintf (dump_file, "\nptr is loop invariant.\n");
1622
          *dr = ptr_dr;
1623
          return NULL_TREE;
1624
 
1625
          /* If there exists DR for MEMREF, we are analyzing the base of
1626
             handled component (PTR_INIT), which not necessary has evolution in
1627
             the loop.  */
1628
        }
1629
      object_step = size_binop (PLUS_EXPR, object_step, ptr_step);
1630
 
1631
      /* 3.3 set data-reference structure for MEMREF.  */
1632
      if (!*dr)
1633
        *dr = ptr_dr;
1634
 
1635
      /* 3.4 call address_analysis to analyze INIT of the access
1636
         function.  */
1637
      base_address = address_analysis (ptr_init, stmt, is_read, *dr,
1638
                                       &address_offset, &address_misalign,
1639
                                       &address_aligned_to, &address_step);
1640
      if (!base_address)
1641
        {
1642
          if (dump_file && (dump_flags & TDF_DETAILS))
1643
            {
1644
              fprintf (dump_file, "\nfailed to analyze address ");
1645
              print_generic_expr (dump_file, ptr_init, TDF_SLIM);
1646
              fprintf (dump_file, "\n");
1647
            }
1648
          return NULL_TREE;
1649
        }
1650
 
1651
      /* 3.5 extract memory tag.  */
1652
      switch (TREE_CODE (base_address))
1653
        {
1654
        case SSA_NAME:
1655
          *memtag = get_var_ann (SSA_NAME_VAR (base_address))->type_mem_tag;
1656
          if (!(*memtag) && TREE_CODE (TREE_OPERAND (memref, 0)) == SSA_NAME)
1657
            *memtag = get_var_ann (
1658
                      SSA_NAME_VAR (TREE_OPERAND (memref, 0)))->type_mem_tag;
1659
          break;
1660
        case ADDR_EXPR:
1661
          *memtag = TREE_OPERAND (base_address, 0);
1662
          break;
1663
        default:
1664
          if (dump_file && (dump_flags & TDF_DETAILS))
1665
            {
1666
              fprintf (dump_file, "\nno memtag for ");
1667
              print_generic_expr (dump_file, memref, TDF_SLIM);
1668
              fprintf (dump_file, "\n");
1669
            }
1670
          *memtag = NULL_TREE;
1671
          break;
1672
        }
1673
    }
1674
 
1675
  if (!base_address)
1676
    {
1677
      /* MEMREF cannot be analyzed.  */
1678
      if (dump_file && (dump_flags & TDF_DETAILS))
1679
        {
1680
          fprintf (dump_file, "\ndata-ref of unsupported type ");
1681
          print_generic_expr (dump_file, memref, TDF_SLIM);
1682
          fprintf (dump_file, "\n");
1683
        }
1684
      return NULL_TREE;
1685
    }
1686
 
1687
  if (SSA_VAR_P (*memtag) && var_can_have_subvars (*memtag))
1688
    *subvars = get_subvars_for_var (*memtag);
1689
 
1690
  /* Part 2: Combine the results of object and address analysis to calculate
1691
     INITIAL_OFFSET, STEP and misalignment info.  */
1692
  *offset = size_binop (PLUS_EXPR, object_offset, address_offset);
1693
 
1694
  if ((!object_misalign && !object_aligned_to)
1695
      || (!address_misalign && !address_aligned_to))
1696
    {
1697
      *misalign = NULL_TREE;
1698
      *aligned_to = NULL_TREE;
1699
    }
1700
  else
1701
    {
1702
      if (object_misalign && address_misalign)
1703
        *misalign = size_binop (PLUS_EXPR, object_misalign, address_misalign);
1704
      else
1705
        *misalign = object_misalign ? object_misalign : address_misalign;
1706
      if (object_aligned_to && address_aligned_to)
1707
        *aligned_to = size_binop (MIN_EXPR, object_aligned_to,
1708
                                  address_aligned_to);
1709
      else
1710
        *aligned_to = object_aligned_to ?
1711
          object_aligned_to : address_aligned_to;
1712
    }
1713
  *step = size_binop (PLUS_EXPR, object_step, address_step);
1714
 
1715
  return base_address;
1716
}
1717
 
1718
/* Function analyze_offset.
1719
 
1720
   Extract INVARIANT and CONSTANT parts from OFFSET.
1721
 
1722
*/
1723
static void
1724
analyze_offset (tree offset, tree *invariant, tree *constant)
1725
{
1726
  tree op0, op1, constant_0, constant_1, invariant_0, invariant_1;
1727
  enum tree_code code = TREE_CODE (offset);
1728
 
1729
  *invariant = NULL_TREE;
1730
  *constant = NULL_TREE;
1731
 
1732
  /* Not PLUS/MINUS expression - recursion stop condition.  */
1733
  if (code != PLUS_EXPR && code != MINUS_EXPR)
1734
    {
1735
      if (TREE_CODE (offset) == INTEGER_CST)
1736
        *constant = offset;
1737
      else
1738
        *invariant = offset;
1739
      return;
1740
    }
1741
 
1742
  op0 = TREE_OPERAND (offset, 0);
1743
  op1 = TREE_OPERAND (offset, 1);
1744
 
1745
  /* Recursive call with the operands.  */
1746
  analyze_offset (op0, &invariant_0, &constant_0);
1747
  analyze_offset (op1, &invariant_1, &constant_1);
1748
 
1749
  /* Combine the results.  */
1750
  *constant = constant_0 ? constant_0 : constant_1;
1751
  if (invariant_0 && invariant_1)
1752
    *invariant =
1753
      fold_build2 (code, TREE_TYPE (invariant_0), invariant_0, invariant_1);
1754
  else
1755
    *invariant = invariant_0 ? invariant_0 : invariant_1;
1756
}
1757
 
1758
 
1759
/* Function create_data_ref.
1760
 
1761
   Create a data-reference structure for MEMREF. Set its DR_BASE_ADDRESS,
1762
   DR_OFFSET, DR_INIT, DR_STEP, DR_OFFSET_MISALIGNMENT, DR_ALIGNED_TO,
1763
   DR_MEMTAG, and DR_POINTSTO_INFO fields.
1764
 
1765
   Input:
1766
   MEMREF - the memory reference that is being analyzed
1767
   STMT - the statement that contains MEMREF
1768
   IS_READ - TRUE if STMT reads from MEMREF, FALSE if writes to MEMREF
1769
 
1770
   Output:
1771
   DR (returned value) - data_reference struct for MEMREF
1772
*/
1773
 
1774
static struct data_reference *
1775
create_data_ref (tree memref, tree stmt, bool is_read)
1776
{
1777
  struct data_reference *dr = NULL;
1778
  tree base_address, offset, step, misalign, memtag;
1779
  struct loop *loop = loop_containing_stmt (stmt);
1780
  tree invariant = NULL_TREE, constant = NULL_TREE;
1781
  tree type_size, init_cond;
1782
  struct ptr_info_def *ptr_info;
1783
  subvar_t subvars = NULL;
1784
  tree aligned_to;
1785
 
1786
  if (!memref)
1787
    return NULL;
1788
 
1789
  base_address = object_analysis (memref, stmt, is_read, &dr, &offset,
1790
                                  &misalign, &aligned_to, &step, &memtag,
1791
                                  &ptr_info, &subvars);
1792
  if (!dr || !base_address)
1793
    {
1794
      if (dump_file && (dump_flags & TDF_DETAILS))
1795
        {
1796
          fprintf (dump_file, "\ncreate_data_ref: failed to create a dr for ");
1797
          print_generic_expr (dump_file, memref, TDF_SLIM);
1798
          fprintf (dump_file, "\n");
1799
        }
1800
      return NULL;
1801
    }
1802
 
1803
  DR_BASE_ADDRESS (dr) = base_address;
1804
  DR_OFFSET (dr) = offset;
1805
  DR_INIT (dr) = ssize_int (0);
1806
  DR_STEP (dr) = step;
1807
  DR_OFFSET_MISALIGNMENT (dr) = misalign;
1808
  DR_ALIGNED_TO (dr) = aligned_to;
1809
  DR_MEMTAG (dr) = memtag;
1810
  DR_PTR_INFO (dr) = ptr_info;
1811
  DR_SUBVARS (dr) = subvars;
1812
 
1813
  type_size = fold_convert (ssizetype, TYPE_SIZE_UNIT (TREE_TYPE (DR_REF (dr))));
1814
 
1815
  /* Change the access function for INIDIRECT_REFs, according to
1816
     DR_BASE_ADDRESS.  Analyze OFFSET calculated in object_analysis. OFFSET is
1817
     an expression that can contain loop invariant expressions and constants.
1818
     We put the constant part in the initial condition of the access function
1819
     (for data dependence tests), and in DR_INIT of the data-ref. The loop
1820
     invariant part is put in DR_OFFSET.
1821
     The evolution part of the access function is STEP calculated in
1822
     object_analysis divided by the size of data type.
1823
  */
1824
  if (!DR_BASE_OBJECT (dr))
1825
    {
1826
      tree access_fn;
1827
      tree new_step;
1828
 
1829
      /* Extract CONSTANT and INVARIANT from OFFSET, and put them in DR_INIT and
1830
         DR_OFFSET fields of DR.  */
1831
      analyze_offset (offset, &invariant, &constant);
1832
      if (constant)
1833
        {
1834
          DR_INIT (dr) = fold_convert (ssizetype, constant);
1835
          init_cond = fold_build2 (TRUNC_DIV_EXPR, TREE_TYPE (constant),
1836
                                   constant, type_size);
1837
        }
1838
      else
1839
        DR_INIT (dr) = init_cond = ssize_int (0);;
1840
 
1841
      if (invariant)
1842
        DR_OFFSET (dr) = invariant;
1843
      else
1844
        DR_OFFSET (dr) = ssize_int (0);
1845
 
1846
      /* Update access function.  */
1847
      access_fn = DR_ACCESS_FN (dr, 0);
1848
      new_step = size_binop (TRUNC_DIV_EXPR,
1849
                             fold_convert (ssizetype, step), type_size);
1850
 
1851
      access_fn = chrec_replace_initial_condition (access_fn, init_cond);
1852
      access_fn = reset_evolution_in_loop (loop->num, access_fn, new_step);
1853
 
1854
      VEC_replace (tree, DR_ACCESS_FNS (dr), 0, access_fn);
1855
    }
1856
 
1857
  if (dump_file && (dump_flags & TDF_DETAILS))
1858
    {
1859
      struct ptr_info_def *pi = DR_PTR_INFO (dr);
1860
 
1861
      fprintf (dump_file, "\nCreated dr for ");
1862
      print_generic_expr (dump_file, memref, TDF_SLIM);
1863
      fprintf (dump_file, "\n\tbase_address: ");
1864
      print_generic_expr (dump_file, DR_BASE_ADDRESS (dr), TDF_SLIM);
1865
      fprintf (dump_file, "\n\toffset from base address: ");
1866
      print_generic_expr (dump_file, DR_OFFSET (dr), TDF_SLIM);
1867
      fprintf (dump_file, "\n\tconstant offset from base address: ");
1868
      print_generic_expr (dump_file, DR_INIT (dr), TDF_SLIM);
1869
      fprintf (dump_file, "\n\tbase_object: ");
1870
      print_generic_expr (dump_file, DR_BASE_OBJECT (dr), TDF_SLIM);
1871
      fprintf (dump_file, "\n\tstep: ");
1872
      print_generic_expr (dump_file, DR_STEP (dr), TDF_SLIM);
1873
      fprintf (dump_file, "B\n\tmisalignment from base: ");
1874
      print_generic_expr (dump_file, DR_OFFSET_MISALIGNMENT (dr), TDF_SLIM);
1875
      if (DR_OFFSET_MISALIGNMENT (dr))
1876
        fprintf (dump_file, "B");
1877
      if (DR_ALIGNED_TO (dr))
1878
        {
1879
          fprintf (dump_file, "\n\taligned to: ");
1880
          print_generic_expr (dump_file, DR_ALIGNED_TO (dr), TDF_SLIM);
1881
        }
1882
      fprintf (dump_file, "\n\tmemtag: ");
1883
      print_generic_expr (dump_file, DR_MEMTAG (dr), TDF_SLIM);
1884
      fprintf (dump_file, "\n");
1885
      if (pi && pi->name_mem_tag)
1886
        {
1887
          fprintf (dump_file, "\n\tnametag: ");
1888
          print_generic_expr (dump_file, pi->name_mem_tag, TDF_SLIM);
1889
          fprintf (dump_file, "\n");
1890
        }
1891
    }
1892
  return dr;
1893
}
1894
 
1895
 
1896
/* Returns true when all the functions of a tree_vec CHREC are the
1897
   same.  */
1898
 
1899
static bool
1900
all_chrecs_equal_p (tree chrec)
1901
{
1902
  int j;
1903
 
1904
  for (j = 0; j < TREE_VEC_LENGTH (chrec) - 1; j++)
1905
    {
1906
      tree chrec_j = TREE_VEC_ELT (chrec, j);
1907
      tree chrec_j_1 = TREE_VEC_ELT (chrec, j + 1);
1908
      if (!integer_zerop
1909
          (chrec_fold_minus
1910
           (integer_type_node, chrec_j, chrec_j_1)))
1911
        return false;
1912
    }
1913
  return true;
1914
}
1915
 
1916
/* Determine for each subscript in the data dependence relation DDR
1917
   the distance.  */
1918
 
1919
void
1920
compute_subscript_distance (struct data_dependence_relation *ddr)
1921
{
1922
  if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
1923
    {
1924
      unsigned int i;
1925
 
1926
      for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1927
        {
1928
          tree conflicts_a, conflicts_b, difference;
1929
          struct subscript *subscript;
1930
 
1931
          subscript = DDR_SUBSCRIPT (ddr, i);
1932
          conflicts_a = SUB_CONFLICTS_IN_A (subscript);
1933
          conflicts_b = SUB_CONFLICTS_IN_B (subscript);
1934
 
1935
          if (TREE_CODE (conflicts_a) == TREE_VEC)
1936
            {
1937
              if (!all_chrecs_equal_p (conflicts_a))
1938
                {
1939
                  SUB_DISTANCE (subscript) = chrec_dont_know;
1940
                  return;
1941
                }
1942
              else
1943
                conflicts_a = TREE_VEC_ELT (conflicts_a, 0);
1944
            }
1945
 
1946
          if (TREE_CODE (conflicts_b) == TREE_VEC)
1947
            {
1948
              if (!all_chrecs_equal_p (conflicts_b))
1949
                {
1950
                  SUB_DISTANCE (subscript) = chrec_dont_know;
1951
                  return;
1952
                }
1953
              else
1954
                conflicts_b = TREE_VEC_ELT (conflicts_b, 0);
1955
            }
1956
 
1957
          difference = chrec_fold_minus
1958
            (integer_type_node, conflicts_b, conflicts_a);
1959
 
1960
          if (evolution_function_is_constant_p (difference))
1961
            SUB_DISTANCE (subscript) = difference;
1962
 
1963
          else
1964
            SUB_DISTANCE (subscript) = chrec_dont_know;
1965
        }
1966
    }
1967
}
1968
 
1969
/* Initialize a ddr.  */
1970
 
1971
struct data_dependence_relation *
1972
initialize_data_dependence_relation (struct data_reference *a,
1973
                                     struct data_reference *b)
1974
{
1975
  struct data_dependence_relation *res;
1976
  bool differ_p;
1977
  unsigned int i;
1978
 
1979
  res = xmalloc (sizeof (struct data_dependence_relation));
1980
  DDR_A (res) = a;
1981
  DDR_B (res) = b;
1982
 
1983
  if (a == NULL || b == NULL)
1984
    {
1985
      DDR_ARE_DEPENDENT (res) = chrec_dont_know;
1986
      return res;
1987
    }
1988
 
1989
  /* When A and B are arrays and their dimensions differ, we directly
1990
     initialize the relation to "there is no dependence": chrec_known.  */
1991
  if (DR_BASE_OBJECT (a) && DR_BASE_OBJECT (b)
1992
      && DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
1993
    {
1994
      DDR_ARE_DEPENDENT (res) = chrec_known;
1995
      return res;
1996
    }
1997
 
1998
    /* Compare the bases of the data-refs.  */
1999
  if (!base_addr_differ_p (a, b, &differ_p))
2000
    {
2001
      /* Can't determine whether the data-refs access the same memory
2002
         region.  */
2003
      DDR_ARE_DEPENDENT (res) = chrec_dont_know;
2004
      return res;
2005
    }
2006
  if (differ_p)
2007
    {
2008
      DDR_ARE_DEPENDENT (res) = chrec_known;
2009
      return res;
2010
    }
2011
 
2012
  DDR_AFFINE_P (res) = true;
2013
  DDR_ARE_DEPENDENT (res) = NULL_TREE;
2014
  DDR_SUBSCRIPTS_VECTOR_INIT (res, DR_NUM_DIMENSIONS (a));
2015
  DDR_SIZE_VECT (res) = 0;
2016
  DDR_DIR_VECTS (res) = NULL;
2017
  DDR_DIST_VECTS (res) = NULL;
2018
 
2019
  for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
2020
    {
2021
      struct subscript *subscript;
2022
 
2023
      subscript = xmalloc (sizeof (struct subscript));
2024
      SUB_CONFLICTS_IN_A (subscript) = chrec_dont_know;
2025
      SUB_CONFLICTS_IN_B (subscript) = chrec_dont_know;
2026
      SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
2027
      SUB_DISTANCE (subscript) = chrec_dont_know;
2028
      VARRAY_PUSH_GENERIC_PTR (DDR_SUBSCRIPTS (res), subscript);
2029
    }
2030
 
2031
  return res;
2032
}
2033
 
2034
/* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
2035
   description.  */
2036
 
2037
static inline void
2038
finalize_ddr_dependent (struct data_dependence_relation *ddr,
2039
                        tree chrec)
2040
{
2041
  if (dump_file && (dump_flags & TDF_DETAILS))
2042
    {
2043
      fprintf (dump_file, "(dependence classified: ");
2044
      print_generic_expr (dump_file, chrec, 0);
2045
      fprintf (dump_file, ")\n");
2046
    }
2047
 
2048
  DDR_ARE_DEPENDENT (ddr) = chrec;
2049
  varray_clear (DDR_SUBSCRIPTS (ddr));
2050
}
2051
 
2052
/* The dependence relation DDR cannot be represented by a distance
2053
   vector.  */
2054
 
2055
static inline void
2056
non_affine_dependence_relation (struct data_dependence_relation *ddr)
2057
{
2058
  if (dump_file && (dump_flags & TDF_DETAILS))
2059
    fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
2060
 
2061
  DDR_AFFINE_P (ddr) = false;
2062
}
2063
 
2064
 
2065
 
2066
/* This section contains the classic Banerjee tests.  */
2067
 
2068
/* Returns true iff CHREC_A and CHREC_B are not dependent on any index
2069
   variables, i.e., if the ZIV (Zero Index Variable) test is true.  */
2070
 
2071
static inline bool
2072
ziv_subscript_p (tree chrec_a,
2073
                 tree chrec_b)
2074
{
2075
  return (evolution_function_is_constant_p (chrec_a)
2076
          && evolution_function_is_constant_p (chrec_b));
2077
}
2078
 
2079
/* Returns true iff CHREC_A and CHREC_B are dependent on an index
2080
   variable, i.e., if the SIV (Single Index Variable) test is true.  */
2081
 
2082
static bool
2083
siv_subscript_p (tree chrec_a,
2084
                 tree chrec_b)
2085
{
2086
  if ((evolution_function_is_constant_p (chrec_a)
2087
       && evolution_function_is_univariate_p (chrec_b))
2088
      || (evolution_function_is_constant_p (chrec_b)
2089
          && evolution_function_is_univariate_p (chrec_a)))
2090
    return true;
2091
 
2092
  if (evolution_function_is_univariate_p (chrec_a)
2093
      && evolution_function_is_univariate_p (chrec_b))
2094
    {
2095
      switch (TREE_CODE (chrec_a))
2096
        {
2097
        case POLYNOMIAL_CHREC:
2098
          switch (TREE_CODE (chrec_b))
2099
            {
2100
            case POLYNOMIAL_CHREC:
2101
              if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
2102
                return false;
2103
 
2104
            default:
2105
              return true;
2106
            }
2107
 
2108
        default:
2109
          return true;
2110
        }
2111
    }
2112
 
2113
  return false;
2114
}
2115
 
2116
/* Analyze a ZIV (Zero Index Variable) subscript.  *OVERLAPS_A and
2117
   *OVERLAPS_B are initialized to the functions that describe the
2118
   relation between the elements accessed twice by CHREC_A and
2119
   CHREC_B.  For k >= 0, the following property is verified:
2120
 
2121
   CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */
2122
 
2123
static void
2124
analyze_ziv_subscript (tree chrec_a,
2125
                       tree chrec_b,
2126
                       tree *overlaps_a,
2127
                       tree *overlaps_b,
2128
                       tree *last_conflicts)
2129
{
2130
  tree difference;
2131
 
2132
  if (dump_file && (dump_flags & TDF_DETAILS))
2133
    fprintf (dump_file, "(analyze_ziv_subscript \n");
2134
 
2135
  difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
2136
 
2137
  switch (TREE_CODE (difference))
2138
    {
2139
    case INTEGER_CST:
2140
      if (integer_zerop (difference))
2141
        {
2142
          /* The difference is equal to zero: the accessed index
2143
             overlaps for each iteration in the loop.  */
2144
          *overlaps_a = integer_zero_node;
2145
          *overlaps_b = integer_zero_node;
2146
          *last_conflicts = chrec_dont_know;
2147
        }
2148
      else
2149
        {
2150
          /* The accesses do not overlap.  */
2151
          *overlaps_a = chrec_known;
2152
          *overlaps_b = chrec_known;
2153
          *last_conflicts = integer_zero_node;
2154
        }
2155
      break;
2156
 
2157
    default:
2158
      /* We're not sure whether the indexes overlap.  For the moment,
2159
         conservatively answer "don't know".  */
2160
      *overlaps_a = chrec_dont_know;
2161
      *overlaps_b = chrec_dont_know;
2162
      *last_conflicts = chrec_dont_know;
2163
      break;
2164
    }
2165
 
2166
  if (dump_file && (dump_flags & TDF_DETAILS))
2167
    fprintf (dump_file, ")\n");
2168
}
2169
 
2170
/* Get the real or estimated number of iterations for LOOPNUM, whichever is
2171
   available. Return the number of iterations as a tree, or NULL_TREE if
2172
   we don't know.  */
2173
 
2174
static tree
2175
get_number_of_iters_for_loop (int loopnum)
2176
{
2177
  tree numiter = number_of_iterations_in_loop (current_loops->parray[loopnum]);
2178
 
2179
  if (TREE_CODE (numiter) != INTEGER_CST)
2180
    numiter = current_loops->parray[loopnum]->estimated_nb_iterations;
2181
  if (chrec_contains_undetermined (numiter))
2182
    return NULL_TREE;
2183
  return numiter;
2184
}
2185
 
2186
/* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
2187
   constant, and CHREC_B is an affine function.  *OVERLAPS_A and
2188
   *OVERLAPS_B are initialized to the functions that describe the
2189
   relation between the elements accessed twice by CHREC_A and
2190
   CHREC_B.  For k >= 0, the following property is verified:
2191
 
2192
   CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */
2193
 
2194
static void
2195
analyze_siv_subscript_cst_affine (tree chrec_a,
2196
                                  tree chrec_b,
2197
                                  tree *overlaps_a,
2198
                                  tree *overlaps_b,
2199
                                  tree *last_conflicts)
2200
{
2201
  bool value0, value1, value2;
2202
  tree difference = chrec_fold_minus
2203
    (integer_type_node, CHREC_LEFT (chrec_b), chrec_a);
2204
 
2205
  if (!chrec_is_positive (initial_condition (difference), &value0))
2206
    {
2207
      *overlaps_a = chrec_dont_know;
2208
      *overlaps_b = chrec_dont_know;
2209
      *last_conflicts = chrec_dont_know;
2210
      return;
2211
    }
2212
  else
2213
    {
2214
      if (value0 == false)
2215
        {
2216
          if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
2217
            {
2218
              *overlaps_a = chrec_dont_know;
2219
              *overlaps_b = chrec_dont_know;
2220
              *last_conflicts = chrec_dont_know;
2221
              return;
2222
            }
2223
          else
2224
            {
2225
              if (value1 == true)
2226
                {
2227
                  /* Example:
2228
                     chrec_a = 12
2229
                     chrec_b = {10, +, 1}
2230
                  */
2231
 
2232
                  if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2233
                    {
2234
                      tree numiter;
2235
                      int loopnum = CHREC_VARIABLE (chrec_b);
2236
 
2237
                      *overlaps_a = integer_zero_node;
2238
                      *overlaps_b = fold_build2 (EXACT_DIV_EXPR, integer_type_node,
2239
                                                 fold_build1 (ABS_EXPR,
2240
                                                              integer_type_node,
2241
                                                              difference),
2242
                                                 CHREC_RIGHT (chrec_b));
2243
                      *last_conflicts = integer_one_node;
2244
 
2245
 
2246
                      /* Perform weak-zero siv test to see if overlap is
2247
                         outside the loop bounds.  */
2248
                      numiter = get_number_of_iters_for_loop (loopnum);
2249
 
2250
                      if (numiter != NULL_TREE
2251
                          && TREE_CODE (*overlaps_b) == INTEGER_CST
2252
                          && tree_int_cst_lt (numiter, *overlaps_b))
2253
                        {
2254
                          *overlaps_a = chrec_known;
2255
                          *overlaps_b = chrec_known;
2256
                          *last_conflicts = integer_zero_node;
2257
                          return;
2258
                        }
2259
                      return;
2260
                    }
2261
 
2262
                  /* When the step does not divide the difference, there are
2263
                     no overlaps.  */
2264
                  else
2265
                    {
2266
                      *overlaps_a = chrec_known;
2267
                      *overlaps_b = chrec_known;
2268
                      *last_conflicts = integer_zero_node;
2269
                      return;
2270
                    }
2271
                }
2272
 
2273
              else
2274
                {
2275
                  /* Example:
2276
                     chrec_a = 12
2277
                     chrec_b = {10, +, -1}
2278
 
2279
                     In this case, chrec_a will not overlap with chrec_b.  */
2280
                  *overlaps_a = chrec_known;
2281
                  *overlaps_b = chrec_known;
2282
                  *last_conflicts = integer_zero_node;
2283
                  return;
2284
                }
2285
            }
2286
        }
2287
      else
2288
        {
2289
          if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
2290
            {
2291
              *overlaps_a = chrec_dont_know;
2292
              *overlaps_b = chrec_dont_know;
2293
              *last_conflicts = chrec_dont_know;
2294
              return;
2295
            }
2296
          else
2297
            {
2298
              if (value2 == false)
2299
                {
2300
                  /* Example:
2301
                     chrec_a = 3
2302
                     chrec_b = {10, +, -1}
2303
                  */
2304
                  if (tree_fold_divides_p (CHREC_RIGHT (chrec_b), difference))
2305
                    {
2306
                      tree numiter;
2307
                      int loopnum = CHREC_VARIABLE (chrec_b);
2308
 
2309
                      *overlaps_a = integer_zero_node;
2310
                      *overlaps_b = fold_build2 (EXACT_DIV_EXPR,
2311
                                                 integer_type_node, difference,
2312
                                                 CHREC_RIGHT (chrec_b));
2313
                      *last_conflicts = integer_one_node;
2314
 
2315
                      /* Perform weak-zero siv test to see if overlap is
2316
                         outside the loop bounds.  */
2317
                      numiter = get_number_of_iters_for_loop (loopnum);
2318
 
2319
                      if (numiter != NULL_TREE
2320
                          && TREE_CODE (*overlaps_b) == INTEGER_CST
2321
                          && tree_int_cst_lt (numiter, *overlaps_b))
2322
                        {
2323
                          *overlaps_a = chrec_known;
2324
                          *overlaps_b = chrec_known;
2325
                          *last_conflicts = integer_zero_node;
2326
                          return;
2327
                        }
2328
                      return;
2329
                    }
2330
 
2331
                  /* When the step does not divide the difference, there
2332
                     are no overlaps.  */
2333
                  else
2334
                    {
2335
                      *overlaps_a = chrec_known;
2336
                      *overlaps_b = chrec_known;
2337
                      *last_conflicts = integer_zero_node;
2338
                      return;
2339
                    }
2340
                }
2341
              else
2342
                {
2343
                  /* Example:
2344
                     chrec_a = 3
2345
                     chrec_b = {4, +, 1}
2346
 
2347
                     In this case, chrec_a will not overlap with chrec_b.  */
2348
                  *overlaps_a = chrec_known;
2349
                  *overlaps_b = chrec_known;
2350
                  *last_conflicts = integer_zero_node;
2351
                  return;
2352
                }
2353
            }
2354
        }
2355
    }
2356
}
2357
 
2358
/* Helper recursive function for initializing the matrix A.  Returns
2359
   the initial value of CHREC.  */
2360
 
2361
static int
2362
initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
2363
{
2364
  gcc_assert (chrec);
2365
 
2366
  if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
2367
    return int_cst_value (chrec);
2368
 
2369
  A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
2370
  return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
2371
}
2372
 
2373
#define FLOOR_DIV(x,y) ((x) / (y))
2374
 
2375
/* Solves the special case of the Diophantine equation:
2376
   | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
2377
 
2378
   Computes the descriptions OVERLAPS_A and OVERLAPS_B.  NITER is the
2379
   number of iterations that loops X and Y run.  The overlaps will be
2380
   constructed as evolutions in dimension DIM.  */
2381
 
2382
static void
2383
compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b,
2384
                                         tree *overlaps_a, tree *overlaps_b,
2385
                                         tree *last_conflicts, int dim)
2386
{
2387
  if (((step_a > 0 && step_b > 0)
2388
       || (step_a < 0 && step_b < 0)))
2389
    {
2390
      int step_overlaps_a, step_overlaps_b;
2391
      int gcd_steps_a_b, last_conflict, tau2;
2392
 
2393
      gcd_steps_a_b = gcd (step_a, step_b);
2394
      step_overlaps_a = step_b / gcd_steps_a_b;
2395
      step_overlaps_b = step_a / gcd_steps_a_b;
2396
 
2397
      tau2 = FLOOR_DIV (niter, step_overlaps_a);
2398
      tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
2399
      last_conflict = tau2;
2400
 
2401
      *overlaps_a = build_polynomial_chrec
2402
        (dim, integer_zero_node,
2403
         build_int_cst (NULL_TREE, step_overlaps_a));
2404
      *overlaps_b = build_polynomial_chrec
2405
        (dim, integer_zero_node,
2406
         build_int_cst (NULL_TREE, step_overlaps_b));
2407
      *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2408
    }
2409
 
2410
  else
2411
    {
2412
      *overlaps_a = integer_zero_node;
2413
      *overlaps_b = integer_zero_node;
2414
      *last_conflicts = integer_zero_node;
2415
    }
2416
}
2417
 
2418
 
2419
/* Solves the special case of a Diophantine equation where CHREC_A is
2420
   an affine bivariate function, and CHREC_B is an affine univariate
2421
   function.  For example,
2422
 
2423
   | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
2424
 
2425
   has the following overlapping functions:
2426
 
2427
   | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
2428
   | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
2429
   | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
2430
 
2431
   FORNOW: This is a specialized implementation for a case occurring in
2432
   a common benchmark.  Implement the general algorithm.  */
2433
 
2434
static void
2435
compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b,
2436
                                      tree *overlaps_a, tree *overlaps_b,
2437
                                      tree *last_conflicts)
2438
{
2439
  bool xz_p, yz_p, xyz_p;
2440
  int step_x, step_y, step_z;
2441
  int niter_x, niter_y, niter_z, niter;
2442
  tree numiter_x, numiter_y, numiter_z;
2443
  tree overlaps_a_xz, overlaps_b_xz, last_conflicts_xz;
2444
  tree overlaps_a_yz, overlaps_b_yz, last_conflicts_yz;
2445
  tree overlaps_a_xyz, overlaps_b_xyz, last_conflicts_xyz;
2446
 
2447
  step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
2448
  step_y = int_cst_value (CHREC_RIGHT (chrec_a));
2449
  step_z = int_cst_value (CHREC_RIGHT (chrec_b));
2450
 
2451
  numiter_x = get_number_of_iters_for_loop (CHREC_VARIABLE (CHREC_LEFT (chrec_a)));
2452
  numiter_y = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
2453
  numiter_z = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b));
2454
 
2455
  if (numiter_x == NULL_TREE || numiter_y == NULL_TREE
2456
      || numiter_z == NULL_TREE)
2457
    {
2458
      *overlaps_a = chrec_dont_know;
2459
      *overlaps_b = chrec_dont_know;
2460
      *last_conflicts = chrec_dont_know;
2461
      return;
2462
    }
2463
 
2464
  niter_x = int_cst_value (numiter_x);
2465
  niter_y = int_cst_value (numiter_y);
2466
  niter_z = int_cst_value (numiter_z);
2467
 
2468
  niter = MIN (niter_x, niter_z);
2469
  compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
2470
                                           &overlaps_a_xz,
2471
                                           &overlaps_b_xz,
2472
                                           &last_conflicts_xz, 1);
2473
  niter = MIN (niter_y, niter_z);
2474
  compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
2475
                                           &overlaps_a_yz,
2476
                                           &overlaps_b_yz,
2477
                                           &last_conflicts_yz, 2);
2478
  niter = MIN (niter_x, niter_z);
2479
  niter = MIN (niter_y, niter);
2480
  compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
2481
                                           &overlaps_a_xyz,
2482
                                           &overlaps_b_xyz,
2483
                                           &last_conflicts_xyz, 3);
2484
 
2485
  xz_p = !integer_zerop (last_conflicts_xz);
2486
  yz_p = !integer_zerop (last_conflicts_yz);
2487
  xyz_p = !integer_zerop (last_conflicts_xyz);
2488
 
2489
  if (xz_p || yz_p || xyz_p)
2490
    {
2491
      *overlaps_a = make_tree_vec (2);
2492
      TREE_VEC_ELT (*overlaps_a, 0) = integer_zero_node;
2493
      TREE_VEC_ELT (*overlaps_a, 1) = integer_zero_node;
2494
      *overlaps_b = integer_zero_node;
2495
      if (xz_p)
2496
        {
2497
          TREE_VEC_ELT (*overlaps_a, 0) =
2498
            chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 0),
2499
                             overlaps_a_xz);
2500
          *overlaps_b =
2501
            chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_xz);
2502
          *last_conflicts = last_conflicts_xz;
2503
        }
2504
      if (yz_p)
2505
        {
2506
          TREE_VEC_ELT (*overlaps_a, 1) =
2507
            chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 1),
2508
                             overlaps_a_yz);
2509
          *overlaps_b =
2510
            chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_yz);
2511
          *last_conflicts = last_conflicts_yz;
2512
        }
2513
      if (xyz_p)
2514
        {
2515
          TREE_VEC_ELT (*overlaps_a, 0) =
2516
            chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 0),
2517
                             overlaps_a_xyz);
2518
          TREE_VEC_ELT (*overlaps_a, 1) =
2519
            chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 1),
2520
                             overlaps_a_xyz);
2521
          *overlaps_b =
2522
            chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_xyz);
2523
          *last_conflicts = last_conflicts_xyz;
2524
        }
2525
    }
2526
  else
2527
    {
2528
      *overlaps_a = integer_zero_node;
2529
      *overlaps_b = integer_zero_node;
2530
      *last_conflicts = integer_zero_node;
2531
    }
2532
}
2533
 
2534
/* Determines the overlapping elements due to accesses CHREC_A and
2535
   CHREC_B, that are affine functions.  This is a part of the
2536
   subscript analyzer.  */
2537
 
2538
static void
2539
analyze_subscript_affine_affine (tree chrec_a,
2540
                                 tree chrec_b,
2541
                                 tree *overlaps_a,
2542
                                 tree *overlaps_b,
2543
                                 tree *last_conflicts)
2544
{
2545
  unsigned nb_vars_a, nb_vars_b, dim;
2546
  int init_a, init_b, gamma, gcd_alpha_beta;
2547
  int tau1, tau2;
2548
  lambda_matrix A, U, S;
2549
  tree difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
2550
 
2551
  if (integer_zerop (difference))
2552
    {
2553
      /* The difference is equal to zero: the accessed index
2554
         overlaps for each iteration in the loop.  */
2555
      *overlaps_a = integer_zero_node;
2556
      *overlaps_b = integer_zero_node;
2557
      *last_conflicts = chrec_dont_know;
2558
      return;
2559
    }
2560
  if (dump_file && (dump_flags & TDF_DETAILS))
2561
    fprintf (dump_file, "(analyze_subscript_affine_affine \n");
2562
 
2563
  /* For determining the initial intersection, we have to solve a
2564
     Diophantine equation.  This is the most time consuming part.
2565
 
2566
     For answering to the question: "Is there a dependence?" we have
2567
     to prove that there exists a solution to the Diophantine
2568
     equation, and that the solution is in the iteration domain,
2569
     i.e. the solution is positive or zero, and that the solution
2570
     happens before the upper bound loop.nb_iterations.  Otherwise
2571
     there is no dependence.  This function outputs a description of
2572
     the iterations that hold the intersections.  */
2573
 
2574
 
2575
  nb_vars_a = nb_vars_in_chrec (chrec_a);
2576
  nb_vars_b = nb_vars_in_chrec (chrec_b);
2577
 
2578
  dim = nb_vars_a + nb_vars_b;
2579
  U = lambda_matrix_new (dim, dim);
2580
  A = lambda_matrix_new (dim, 1);
2581
  S = lambda_matrix_new (dim, 1);
2582
 
2583
  init_a = initialize_matrix_A (A, chrec_a, 0, 1);
2584
  init_b = initialize_matrix_A (A, chrec_b, nb_vars_a, -1);
2585
  gamma = init_b - init_a;
2586
 
2587
  /* Don't do all the hard work of solving the Diophantine equation
2588
     when we already know the solution: for example,
2589
     | {3, +, 1}_1
2590
     | {3, +, 4}_2
2591
     | gamma = 3 - 3 = 0.
2592
     Then the first overlap occurs during the first iterations:
2593
     | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
2594
  */
2595
  if (gamma == 0)
2596
    {
2597
      if (nb_vars_a == 1 && nb_vars_b == 1)
2598
        {
2599
          int step_a, step_b;
2600
          int niter, niter_a, niter_b;
2601
          tree numiter_a, numiter_b;
2602
 
2603
          numiter_a = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
2604
          numiter_b = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b));
2605
          if (numiter_a == NULL_TREE || numiter_b == NULL_TREE)
2606
            {
2607
              *overlaps_a = chrec_dont_know;
2608
              *overlaps_b = chrec_dont_know;
2609
              *last_conflicts = chrec_dont_know;
2610
              return;
2611
            }
2612
 
2613
          niter_a = int_cst_value (numiter_a);
2614
          niter_b = int_cst_value (numiter_b);
2615
          niter = MIN (niter_a, niter_b);
2616
 
2617
          step_a = int_cst_value (CHREC_RIGHT (chrec_a));
2618
          step_b = int_cst_value (CHREC_RIGHT (chrec_b));
2619
 
2620
          compute_overlap_steps_for_affine_univar (niter, step_a, step_b,
2621
                                                   overlaps_a, overlaps_b,
2622
                                                   last_conflicts, 1);
2623
        }
2624
 
2625
      else if (nb_vars_a == 2 && nb_vars_b == 1)
2626
        compute_overlap_steps_for_affine_1_2
2627
          (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
2628
 
2629
      else if (nb_vars_a == 1 && nb_vars_b == 2)
2630
        compute_overlap_steps_for_affine_1_2
2631
          (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
2632
 
2633
      else
2634
        {
2635
          *overlaps_a = chrec_dont_know;
2636
          *overlaps_b = chrec_dont_know;
2637
          *last_conflicts = chrec_dont_know;
2638
        }
2639
      return;
2640
    }
2641
 
2642
  /* U.A = S */
2643
  lambda_matrix_right_hermite (A, dim, 1, S, U);
2644
 
2645
  if (S[0][0] < 0)
2646
    {
2647
      S[0][0] *= -1;
2648
      lambda_matrix_row_negate (U, dim, 0);
2649
    }
2650
  gcd_alpha_beta = S[0][0];
2651
 
2652
  /* The classic "gcd-test".  */
2653
  if (!int_divides_p (gcd_alpha_beta, gamma))
2654
    {
2655
      /* The "gcd-test" has determined that there is no integer
2656
         solution, i.e. there is no dependence.  */
2657
      *overlaps_a = chrec_known;
2658
      *overlaps_b = chrec_known;
2659
      *last_conflicts = integer_zero_node;
2660
    }
2661
 
2662
  /* Both access functions are univariate.  This includes SIV and MIV cases.  */
2663
  else if (nb_vars_a == 1 && nb_vars_b == 1)
2664
    {
2665
      /* Both functions should have the same evolution sign.  */
2666
      if (((A[0][0] > 0 && -A[1][0] > 0)
2667
           || (A[0][0] < 0 && -A[1][0] < 0)))
2668
        {
2669
          /* The solutions are given by:
2670
             |
2671
             | [GAMMA/GCD_ALPHA_BETA  t].[u11 u12]  = [x0]
2672
             |                           [u21 u22]    [y0]
2673
 
2674
             For a given integer t.  Using the following variables,
2675
 
2676
             | i0 = u11 * gamma / gcd_alpha_beta
2677
             | j0 = u12 * gamma / gcd_alpha_beta
2678
             | i1 = u21
2679
             | j1 = u22
2680
 
2681
             the solutions are:
2682
 
2683
             | x0 = i0 + i1 * t,
2684
             | y0 = j0 + j1 * t.  */
2685
 
2686
          int i0, j0, i1, j1;
2687
 
2688
          /* X0 and Y0 are the first iterations for which there is a
2689
             dependence.  X0, Y0 are two solutions of the Diophantine
2690
             equation: chrec_a (X0) = chrec_b (Y0).  */
2691
          int x0, y0;
2692
          int niter, niter_a, niter_b;
2693
          tree numiter_a, numiter_b;
2694
 
2695
          numiter_a = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
2696
          numiter_b = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_b));
2697
 
2698
          if (numiter_a == NULL_TREE || numiter_b == NULL_TREE)
2699
            {
2700
              *overlaps_a = chrec_dont_know;
2701
              *overlaps_b = chrec_dont_know;
2702
              *last_conflicts = chrec_dont_know;
2703
              return;
2704
            }
2705
 
2706
          niter_a = int_cst_value (numiter_a);
2707
          niter_b = int_cst_value (numiter_b);
2708
          niter = MIN (niter_a, niter_b);
2709
 
2710
          i0 = U[0][0] * gamma / gcd_alpha_beta;
2711
          j0 = U[0][1] * gamma / gcd_alpha_beta;
2712
          i1 = U[1][0];
2713
          j1 = U[1][1];
2714
 
2715
          if ((i1 == 0 && i0 < 0)
2716
              || (j1 == 0 && j0 < 0))
2717
            {
2718
              /* There is no solution.
2719
                 FIXME: The case "i0 > nb_iterations, j0 > nb_iterations"
2720
                 falls in here, but for the moment we don't look at the
2721
                 upper bound of the iteration domain.  */
2722
              *overlaps_a = chrec_known;
2723
              *overlaps_b = chrec_known;
2724
              *last_conflicts = integer_zero_node;
2725
            }
2726
 
2727
          else
2728
            {
2729
              if (i1 > 0)
2730
                {
2731
                  tau1 = CEIL (-i0, i1);
2732
                  tau2 = FLOOR_DIV (niter - i0, i1);
2733
 
2734
                  if (j1 > 0)
2735
                    {
2736
                      int last_conflict, min_multiple;
2737
                      tau1 = MAX (tau1, CEIL (-j0, j1));
2738
                      tau2 = MIN (tau2, FLOOR_DIV (niter - j0, j1));
2739
 
2740
                      x0 = i1 * tau1 + i0;
2741
                      y0 = j1 * tau1 + j0;
2742
 
2743
                      /* At this point (x0, y0) is one of the
2744
                         solutions to the Diophantine equation.  The
2745
                         next step has to compute the smallest
2746
                         positive solution: the first conflicts.  */
2747
                      min_multiple = MIN (x0 / i1, y0 / j1);
2748
                      x0 -= i1 * min_multiple;
2749
                      y0 -= j1 * min_multiple;
2750
 
2751
                      tau1 = (x0 - i0)/i1;
2752
                      last_conflict = tau2 - tau1;
2753
 
2754
                      /* If the overlap occurs outside of the bounds of the
2755
                         loop, there is no dependence.  */
2756
                      if (x0 > niter || y0  > niter)
2757
 
2758
                        {
2759
                          *overlaps_a = chrec_known;
2760
                          *overlaps_b = chrec_known;
2761
                          *last_conflicts = integer_zero_node;
2762
                        }
2763
                      else
2764
                        {
2765
                          *overlaps_a = build_polynomial_chrec
2766
                            (1,
2767
                             build_int_cst (NULL_TREE, x0),
2768
                             build_int_cst (NULL_TREE, i1));
2769
                          *overlaps_b = build_polynomial_chrec
2770
                            (1,
2771
                             build_int_cst (NULL_TREE, y0),
2772
                             build_int_cst (NULL_TREE, j1));
2773
                          *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
2774
                        }
2775
                    }
2776
                  else
2777
                    {
2778
                      /* FIXME: For the moment, the upper bound of the
2779
                         iteration domain for j is not checked.  */
2780
                      *overlaps_a = chrec_dont_know;
2781
                      *overlaps_b = chrec_dont_know;
2782
                      *last_conflicts = chrec_dont_know;
2783
                    }
2784
                }
2785
 
2786
              else
2787
                {
2788
                  /* FIXME: For the moment, the upper bound of the
2789
                     iteration domain for i is not checked.  */
2790
                  *overlaps_a = chrec_dont_know;
2791
                  *overlaps_b = chrec_dont_know;
2792
                  *last_conflicts = chrec_dont_know;
2793
                }
2794
            }
2795
        }
2796
      else
2797
        {
2798
          *overlaps_a = chrec_dont_know;
2799
          *overlaps_b = chrec_dont_know;
2800
          *last_conflicts = chrec_dont_know;
2801
        }
2802
    }
2803
 
2804
  else
2805
    {
2806
      *overlaps_a = chrec_dont_know;
2807
      *overlaps_b = chrec_dont_know;
2808
      *last_conflicts = chrec_dont_know;
2809
    }
2810
 
2811
 
2812
  if (dump_file && (dump_flags & TDF_DETAILS))
2813
    {
2814
      fprintf (dump_file, "  (overlaps_a = ");
2815
      print_generic_expr (dump_file, *overlaps_a, 0);
2816
      fprintf (dump_file, ")\n  (overlaps_b = ");
2817
      print_generic_expr (dump_file, *overlaps_b, 0);
2818
      fprintf (dump_file, ")\n");
2819
    }
2820
 
2821
  if (dump_file && (dump_flags & TDF_DETAILS))
2822
    fprintf (dump_file, ")\n");
2823
}
2824
 
2825
/* Analyze a SIV (Single Index Variable) subscript.  *OVERLAPS_A and
2826
   *OVERLAPS_B are initialized to the functions that describe the
2827
   relation between the elements accessed twice by CHREC_A and
2828
   CHREC_B.  For k >= 0, the following property is verified:
2829
 
2830
   CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */
2831
 
2832
static void
2833
analyze_siv_subscript (tree chrec_a,
2834
                       tree chrec_b,
2835
                       tree *overlaps_a,
2836
                       tree *overlaps_b,
2837
                       tree *last_conflicts)
2838
{
2839
  if (dump_file && (dump_flags & TDF_DETAILS))
2840
    fprintf (dump_file, "(analyze_siv_subscript \n");
2841
 
2842
  if (evolution_function_is_constant_p (chrec_a)
2843
      && evolution_function_is_affine_p (chrec_b))
2844
    analyze_siv_subscript_cst_affine (chrec_a, chrec_b,
2845
                                      overlaps_a, overlaps_b, last_conflicts);
2846
 
2847
  else if (evolution_function_is_affine_p (chrec_a)
2848
           && evolution_function_is_constant_p (chrec_b))
2849
    analyze_siv_subscript_cst_affine (chrec_b, chrec_a,
2850
                                      overlaps_b, overlaps_a, last_conflicts);
2851
 
2852
  else if (evolution_function_is_affine_p (chrec_a)
2853
           && evolution_function_is_affine_p (chrec_b))
2854
    analyze_subscript_affine_affine (chrec_a, chrec_b,
2855
                                     overlaps_a, overlaps_b, last_conflicts);
2856
  else
2857
    {
2858
      *overlaps_a = chrec_dont_know;
2859
      *overlaps_b = chrec_dont_know;
2860
      *last_conflicts = chrec_dont_know;
2861
    }
2862
 
2863
  if (dump_file && (dump_flags & TDF_DETAILS))
2864
    fprintf (dump_file, ")\n");
2865
}
2866
 
2867
/* Return true when the evolution steps of an affine CHREC divide the
2868
   constant CST.  */
2869
 
2870
static bool
2871
chrec_steps_divide_constant_p (tree chrec,
2872
                               tree cst)
2873
{
2874
  switch (TREE_CODE (chrec))
2875
    {
2876
    case POLYNOMIAL_CHREC:
2877
      return (tree_fold_divides_p (CHREC_RIGHT (chrec), cst)
2878
              && chrec_steps_divide_constant_p (CHREC_LEFT (chrec), cst));
2879
 
2880
    default:
2881
      /* On the initial condition, return true.  */
2882
      return true;
2883
    }
2884
}
2885
 
2886
/* Analyze a MIV (Multiple Index Variable) subscript.  *OVERLAPS_A and
2887
   *OVERLAPS_B are initialized to the functions that describe the
2888
   relation between the elements accessed twice by CHREC_A and
2889
   CHREC_B.  For k >= 0, the following property is verified:
2890
 
2891
   CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */
2892
 
2893
static void
2894
analyze_miv_subscript (tree chrec_a,
2895
                       tree chrec_b,
2896
                       tree *overlaps_a,
2897
                       tree *overlaps_b,
2898
                       tree *last_conflicts)
2899
{
2900
  /* FIXME:  This is a MIV subscript, not yet handled.
2901
     Example: (A[{1, +, 1}_1] vs. A[{1, +, 1}_2]) that comes from
2902
     (A[i] vs. A[j]).
2903
 
2904
     In the SIV test we had to solve a Diophantine equation with two
2905
     variables.  In the MIV case we have to solve a Diophantine
2906
     equation with 2*n variables (if the subscript uses n IVs).
2907
  */
2908
  tree difference;
2909
 
2910
  if (dump_file && (dump_flags & TDF_DETAILS))
2911
    fprintf (dump_file, "(analyze_miv_subscript \n");
2912
 
2913
  difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
2914
 
2915
  if (chrec_zerop (difference))
2916
    {
2917
      /* Access functions are the same: all the elements are accessed
2918
         in the same order.  */
2919
      *overlaps_a = integer_zero_node;
2920
      *overlaps_b = integer_zero_node;
2921
      *last_conflicts = get_number_of_iters_for_loop (CHREC_VARIABLE (chrec_a));
2922
 
2923
    }
2924
 
2925
  else if (evolution_function_is_constant_p (difference)
2926
           /* For the moment, the following is verified:
2927
              evolution_function_is_affine_multivariate_p (chrec_a) */
2928
           && !chrec_steps_divide_constant_p (chrec_a, difference))
2929
    {
2930
      /* testsuite/.../ssa-chrec-33.c
2931
         {{21, +, 2}_1, +, -2}_2  vs.  {{20, +, 2}_1, +, -2}_2
2932
 
2933
         The difference is 1, and the evolution steps are equal to 2,
2934
         consequently there are no overlapping elements.  */
2935
      *overlaps_a = chrec_known;
2936
      *overlaps_b = chrec_known;
2937
      *last_conflicts = integer_zero_node;
2938
    }
2939
 
2940
  else if (evolution_function_is_affine_multivariate_p (chrec_a)
2941
           && evolution_function_is_affine_multivariate_p (chrec_b))
2942
    {
2943
      /* testsuite/.../ssa-chrec-35.c
2944
         {0, +, 1}_2  vs.  {0, +, 1}_3
2945
         the overlapping elements are respectively located at iterations:
2946
         {0, +, 1}_x and {0, +, 1}_x,
2947
         in other words, we have the equality:
2948
         {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
2949
 
2950
         Other examples:
2951
         {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) =
2952
         {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
2953
 
2954
         {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) =
2955
         {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
2956
      */
2957
      analyze_subscript_affine_affine (chrec_a, chrec_b,
2958
                                       overlaps_a, overlaps_b, last_conflicts);
2959
    }
2960
 
2961
  else
2962
    {
2963
      /* When the analysis is too difficult, answer "don't know".  */
2964
      *overlaps_a = chrec_dont_know;
2965
      *overlaps_b = chrec_dont_know;
2966
      *last_conflicts = chrec_dont_know;
2967
    }
2968
 
2969
  if (dump_file && (dump_flags & TDF_DETAILS))
2970
    fprintf (dump_file, ")\n");
2971
}
2972
 
2973
/* Determines the iterations for which CHREC_A is equal to CHREC_B.
2974
   OVERLAP_ITERATIONS_A and OVERLAP_ITERATIONS_B are initialized with
2975
   two functions that describe the iterations that contain conflicting
2976
   elements.
2977
 
2978
   Remark: For an integer k >= 0, the following equality is true:
2979
 
2980
   CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
2981
*/
2982
 
2983
static void
2984
analyze_overlapping_iterations (tree chrec_a,
2985
                                tree chrec_b,
2986
                                tree *overlap_iterations_a,
2987
                                tree *overlap_iterations_b,
2988
                                tree *last_conflicts)
2989
{
2990
  if (dump_file && (dump_flags & TDF_DETAILS))
2991
    {
2992
      fprintf (dump_file, "(analyze_overlapping_iterations \n");
2993
      fprintf (dump_file, "  (chrec_a = ");
2994
      print_generic_expr (dump_file, chrec_a, 0);
2995
      fprintf (dump_file, ")\n  chrec_b = ");
2996
      print_generic_expr (dump_file, chrec_b, 0);
2997
      fprintf (dump_file, ")\n");
2998
    }
2999
 
3000
  if (chrec_a == NULL_TREE
3001
      || chrec_b == NULL_TREE
3002
      || chrec_contains_undetermined (chrec_a)
3003
      || chrec_contains_undetermined (chrec_b)
3004
      || chrec_contains_symbols (chrec_a)
3005
      || chrec_contains_symbols (chrec_b))
3006
    {
3007
      *overlap_iterations_a = chrec_dont_know;
3008
      *overlap_iterations_b = chrec_dont_know;
3009
    }
3010
 
3011
  else if (ziv_subscript_p (chrec_a, chrec_b))
3012
    analyze_ziv_subscript (chrec_a, chrec_b,
3013
                           overlap_iterations_a, overlap_iterations_b,
3014
                           last_conflicts);
3015
 
3016
  else if (siv_subscript_p (chrec_a, chrec_b))
3017
    analyze_siv_subscript (chrec_a, chrec_b,
3018
                           overlap_iterations_a, overlap_iterations_b,
3019
                           last_conflicts);
3020
 
3021
  else
3022
    analyze_miv_subscript (chrec_a, chrec_b,
3023
                           overlap_iterations_a, overlap_iterations_b,
3024
                           last_conflicts);
3025
 
3026
  if (dump_file && (dump_flags & TDF_DETAILS))
3027
    {
3028
      fprintf (dump_file, "  (overlap_iterations_a = ");
3029
      print_generic_expr (dump_file, *overlap_iterations_a, 0);
3030
      fprintf (dump_file, ")\n  (overlap_iterations_b = ");
3031
      print_generic_expr (dump_file, *overlap_iterations_b, 0);
3032
      fprintf (dump_file, ")\n");
3033
    }
3034
}
3035
 
3036
 
3037
 
3038
/* This section contains the affine functions dependences detector.  */
3039
 
3040
/* Computes the conflicting iterations, and initialize DDR.  */
3041
 
3042
static void
3043
subscript_dependence_tester (struct data_dependence_relation *ddr)
3044
{
3045
  unsigned int i;
3046
  struct data_reference *dra = DDR_A (ddr);
3047
  struct data_reference *drb = DDR_B (ddr);
3048
  tree last_conflicts;
3049
 
3050
  if (dump_file && (dump_flags & TDF_DETAILS))
3051
    fprintf (dump_file, "(subscript_dependence_tester \n");
3052
 
3053
  for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3054
    {
3055
      tree overlaps_a, overlaps_b;
3056
      struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3057
 
3058
      analyze_overlapping_iterations (DR_ACCESS_FN (dra, i),
3059
                                      DR_ACCESS_FN (drb, i),
3060
                                      &overlaps_a, &overlaps_b,
3061
                                      &last_conflicts);
3062
 
3063
      if (chrec_contains_undetermined (overlaps_a)
3064
          || chrec_contains_undetermined (overlaps_b))
3065
        {
3066
          finalize_ddr_dependent (ddr, chrec_dont_know);
3067
          break;
3068
        }
3069
 
3070
      else if (overlaps_a == chrec_known
3071
               || overlaps_b == chrec_known)
3072
        {
3073
          finalize_ddr_dependent (ddr, chrec_known);
3074
          break;
3075
        }
3076
 
3077
      else
3078
        {
3079
          SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
3080
          SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
3081
          SUB_LAST_CONFLICT (subscript) = last_conflicts;
3082
        }
3083
    }
3084
 
3085
  if (dump_file && (dump_flags & TDF_DETAILS))
3086
    fprintf (dump_file, ")\n");
3087
}
3088
 
3089
/* Compute the classic per loop distance vector.
3090
 
3091
   DDR is the data dependence relation to build a vector from.
3092
   NB_LOOPS is the total number of loops we are considering.
3093
   FIRST_LOOP_DEPTH is the loop->depth of the first loop in the analyzed
3094
   loop nest.
3095
   Return FALSE when fail to represent the data dependence as a distance
3096
   vector.
3097
   Return TRUE otherwise.  */
3098
 
3099
static bool
3100
build_classic_dist_vector (struct data_dependence_relation *ddr,
3101
                           int nb_loops, int first_loop_depth)
3102
{
3103
  unsigned i;
3104
  lambda_vector dist_v, init_v;
3105
  bool init_b = false;
3106
 
3107
  DDR_SIZE_VECT (ddr) = nb_loops;
3108
  dist_v = lambda_vector_new (nb_loops);
3109
  init_v = lambda_vector_new (nb_loops);
3110
  lambda_vector_clear (dist_v, nb_loops);
3111
  lambda_vector_clear (init_v, nb_loops);
3112
 
3113
  if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3114
    return true;
3115
 
3116
  for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3117
    {
3118
      tree access_fn_a, access_fn_b;
3119
      struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3120
 
3121
      if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3122
        {
3123
          non_affine_dependence_relation (ddr);
3124
          return true;
3125
        }
3126
 
3127
      access_fn_a = DR_ACCESS_FN (DDR_A (ddr), i);
3128
      access_fn_b = DR_ACCESS_FN (DDR_B (ddr), i);
3129
 
3130
      if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3131
          && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3132
        {
3133
          int dist, loop_nb, loop_depth;
3134
          int loop_nb_a = CHREC_VARIABLE (access_fn_a);
3135
          int loop_nb_b = CHREC_VARIABLE (access_fn_b);
3136
          struct loop *loop_a = current_loops->parray[loop_nb_a];
3137
          struct loop *loop_b = current_loops->parray[loop_nb_b];
3138
 
3139
          /* If the loop for either variable is at a lower depth than
3140
             the first_loop's depth, then we can't possibly have a
3141
             dependency at this level of the loop.  */
3142
 
3143
          if (loop_a->depth < first_loop_depth
3144
              || loop_b->depth < first_loop_depth)
3145
            return false;
3146
 
3147
          if (loop_nb_a != loop_nb_b
3148
              && !flow_loop_nested_p (loop_a, loop_b)
3149
              && !flow_loop_nested_p (loop_b, loop_a))
3150
            {
3151
              /* Example: when there are two consecutive loops,
3152
 
3153
                 | loop_1
3154
                 |   A[{0, +, 1}_1]
3155
                 | endloop_1
3156
                 | loop_2
3157
                 |   A[{0, +, 1}_2]
3158
                 | endloop_2
3159
 
3160
                 the dependence relation cannot be captured by the
3161
                 distance abstraction.  */
3162
              non_affine_dependence_relation (ddr);
3163
              return true;
3164
            }
3165
 
3166
          /* The dependence is carried by the outermost loop.  Example:
3167
             | loop_1
3168
             |   A[{4, +, 1}_1]
3169
             |   loop_2
3170
             |     A[{5, +, 1}_2]
3171
             |   endloop_2
3172
             | endloop_1
3173
             In this case, the dependence is carried by loop_1.  */
3174
          loop_nb = loop_nb_a < loop_nb_b ? loop_nb_a : loop_nb_b;
3175
          loop_depth = current_loops->parray[loop_nb]->depth - first_loop_depth;
3176
 
3177
          /* If the loop number is still greater than the number of
3178
             loops we've been asked to analyze, or negative,
3179
             something is borked.  */
3180
          gcc_assert (loop_depth >= 0);
3181
          gcc_assert (loop_depth < nb_loops);
3182
          if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3183
            {
3184
              non_affine_dependence_relation (ddr);
3185
              return true;
3186
            }
3187
 
3188
          dist = int_cst_value (SUB_DISTANCE (subscript));
3189
 
3190
          /* This is the subscript coupling test.
3191
             | loop i = 0, N, 1
3192
             |   T[i+1][i] = ...
3193
             |   ... = T[i][i]
3194
             | endloop
3195
             There is no dependence.  */
3196
          if (init_v[loop_depth] != 0
3197
              && dist_v[loop_depth] != dist)
3198
            {
3199
              finalize_ddr_dependent (ddr, chrec_known);
3200
              return true;
3201
            }
3202
 
3203
          dist_v[loop_depth] = dist;
3204
          init_v[loop_depth] = 1;
3205
          init_b = true;
3206
        }
3207
    }
3208
 
3209
  /* Save the distance vector if we initialized one.  */
3210
  if (init_b)
3211
    {
3212
      lambda_vector save_v;
3213
 
3214
      /* Verify a basic constraint: classic distance vectors should always
3215
         be lexicographically positive.  */
3216
      if (!lambda_vector_lexico_pos (dist_v, DDR_SIZE_VECT (ddr)))
3217
        {
3218
          if (DDR_SIZE_VECT (ddr) == 1)
3219
            /* This one is simple to fix, and can be fixed.
3220
               Multidimensional arrays cannot be fixed that simply.  */
3221
            lambda_vector_negate (dist_v, dist_v, DDR_SIZE_VECT (ddr));
3222
          else
3223
            /* This is not valid: we need the delta test for properly
3224
               fixing all this.  */
3225
            return false;
3226
        }
3227
 
3228
      save_v = lambda_vector_new (DDR_SIZE_VECT (ddr));
3229
      lambda_vector_copy (dist_v, save_v, DDR_SIZE_VECT (ddr));
3230
      VEC_safe_push (lambda_vector, heap, DDR_DIST_VECTS (ddr), save_v);
3231
 
3232
      /* There is nothing more to do when there are no outer loops.  */
3233
      if (DDR_SIZE_VECT (ddr) == 1)
3234
        goto classic_dist_done;
3235
    }
3236
 
3237
  /* There is a distance of 1 on all the outer loops:
3238
 
3239
     Example: there is a dependence of distance 1 on loop_1 for the array A.
3240
     | loop_1
3241
     |   A[5] = ...
3242
     | endloop
3243
  */
3244
  {
3245
    struct loop *lca, *loop_a, *loop_b;
3246
    struct data_reference *a = DDR_A (ddr);
3247
    struct data_reference *b = DDR_B (ddr);
3248
    int lca_depth;
3249
    loop_a = loop_containing_stmt (DR_STMT (a));
3250
    loop_b = loop_containing_stmt (DR_STMT (b));
3251
 
3252
    /* Get the common ancestor loop.  */
3253
    lca = find_common_loop (loop_a, loop_b);
3254
    lca_depth = lca->depth - first_loop_depth;
3255
 
3256
    gcc_assert (lca_depth >= 0);
3257
    gcc_assert (lca_depth < nb_loops);
3258
 
3259
    /* For each outer loop where init_v is not set, the accesses are
3260
       in dependence of distance 1 in the loop.  */
3261
    while (lca->depth != 0)
3262
      {
3263
        /* If we're considering just a sub-nest, then don't record
3264
           any information on the outer loops.  */
3265
        if (lca_depth < 0)
3266
          break;
3267
 
3268
        gcc_assert (lca_depth < nb_loops);
3269
 
3270
        /* If we haven't yet determined a distance for this outer
3271
           loop, push a new distance vector composed of the previous
3272
           distance, and a distance of 1 for this outer loop.
3273
           Example:
3274
 
3275
           | loop_1
3276
           |   loop_2
3277
           |     A[10]
3278
           |   endloop_2
3279
           | endloop_1
3280
 
3281
           Saved vectors are of the form (dist_in_1, dist_in_2).
3282
           First, we save (0, 1), then we have to save (1, 0).  */
3283
        if (init_v[lca_depth] == 0)
3284
          {
3285
            lambda_vector save_v = lambda_vector_new (DDR_SIZE_VECT (ddr));
3286
 
3287
            lambda_vector_copy (dist_v, save_v, DDR_SIZE_VECT (ddr));
3288
            save_v[lca_depth] = 1;
3289
            VEC_safe_push (lambda_vector, heap, DDR_DIST_VECTS (ddr), save_v);
3290
          }
3291
 
3292
        lca = lca->outer;
3293
        lca_depth = lca->depth - first_loop_depth;
3294
      }
3295
  }
3296
 
3297
 classic_dist_done:;
3298
 
3299
  if (dump_file && (dump_flags & TDF_DETAILS))
3300
    {
3301
      fprintf (dump_file, "(build_classic_dist_vector\n");
3302
 
3303
      for (i = 0; i < DDR_NUM_DIST_VECTS (ddr); i++)
3304
        {
3305
          fprintf (dump_file, "  dist_vector = (");
3306
          print_lambda_vector (dump_file, DDR_DIST_VECT (ddr, i),
3307
                               DDR_SIZE_VECT (ddr));
3308
          fprintf (dump_file, "  )\n");
3309
        }
3310
      fprintf (dump_file, ")\n");
3311
    }
3312
 
3313
  return true;
3314
}
3315
 
3316
/* Compute the classic per loop direction vector.
3317
 
3318
   DDR is the data dependence relation to build a vector from.
3319
   NB_LOOPS is the total number of loops we are considering.
3320
   FIRST_LOOP_DEPTH is the loop->depth of the first loop in the analyzed
3321
   loop nest.
3322
   Return FALSE if the dependence relation is outside of the loop nest
3323
   at FIRST_LOOP_DEPTH.
3324
   Return TRUE otherwise.  */
3325
 
3326
static bool
3327
build_classic_dir_vector (struct data_dependence_relation *ddr,
3328
                          int nb_loops, int first_loop_depth)
3329
{
3330
  unsigned i;
3331
  lambda_vector dir_v, init_v;
3332
  bool init_b = false;
3333
 
3334
  dir_v = lambda_vector_new (nb_loops);
3335
  init_v = lambda_vector_new (nb_loops);
3336
  lambda_vector_clear (dir_v, nb_loops);
3337
  lambda_vector_clear (init_v, nb_loops);
3338
 
3339
  DDR_SIZE_VECT (ddr) = nb_loops;
3340
 
3341
  if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
3342
    return true;
3343
 
3344
  for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3345
    {
3346
      tree access_fn_a, access_fn_b;
3347
      struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3348
 
3349
      if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3350
        {
3351
          non_affine_dependence_relation (ddr);
3352
          return true;
3353
        }
3354
 
3355
      access_fn_a = DR_ACCESS_FN (DDR_A (ddr), i);
3356
      access_fn_b = DR_ACCESS_FN (DDR_B (ddr), i);
3357
      if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
3358
          && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
3359
        {
3360
          int dist, loop_nb, loop_depth;
3361
          enum data_dependence_direction dir = dir_star;
3362
          int loop_nb_a = CHREC_VARIABLE (access_fn_a);
3363
          int loop_nb_b = CHREC_VARIABLE (access_fn_b);
3364
          struct loop *loop_a = current_loops->parray[loop_nb_a];
3365
          struct loop *loop_b = current_loops->parray[loop_nb_b];
3366
 
3367
          /* If the loop for either variable is at a lower depth than
3368
             the first_loop's depth, then we can't possibly have a
3369
             dependency at this level of the loop.  */
3370
 
3371
          if (loop_a->depth < first_loop_depth
3372
              || loop_b->depth < first_loop_depth)
3373
            return false;
3374
 
3375
          if (loop_nb_a != loop_nb_b
3376
              && !flow_loop_nested_p (loop_a, loop_b)
3377
              && !flow_loop_nested_p (loop_b, loop_a))
3378
            {
3379
              /* Example: when there are two consecutive loops,
3380
 
3381
                 | loop_1
3382
                 |   A[{0, +, 1}_1]
3383
                 | endloop_1
3384
                 | loop_2
3385
                 |   A[{0, +, 1}_2]
3386
                 | endloop_2
3387
 
3388
                 the dependence relation cannot be captured by the
3389
                 distance abstraction.  */
3390
              non_affine_dependence_relation (ddr);
3391
              return true;
3392
            }
3393
 
3394
          /* The dependence is carried by the outermost loop.  Example:
3395
             | loop_1
3396
             |   A[{4, +, 1}_1]
3397
             |   loop_2
3398
             |     A[{5, +, 1}_2]
3399
             |   endloop_2
3400
             | endloop_1
3401
             In this case, the dependence is carried by loop_1.  */
3402
          loop_nb = loop_nb_a < loop_nb_b ? loop_nb_a : loop_nb_b;
3403
          loop_depth = current_loops->parray[loop_nb]->depth - first_loop_depth;
3404
 
3405
          /* If the loop number is still greater than the number of
3406
             loops we've been asked to analyze, or negative,
3407
             something is borked.  */
3408
          gcc_assert (loop_depth >= 0);
3409
          gcc_assert (loop_depth < nb_loops);
3410
 
3411
          if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
3412
            {
3413
              non_affine_dependence_relation (ddr);
3414
              return true;
3415
            }
3416
 
3417
          dist = int_cst_value (SUB_DISTANCE (subscript));
3418
 
3419
          if (dist == 0)
3420
            dir = dir_equal;
3421
          else if (dist > 0)
3422
            dir = dir_positive;
3423
          else if (dist < 0)
3424
            dir = dir_negative;
3425
 
3426
          /* This is the subscript coupling test.
3427
             | loop i = 0, N, 1
3428
             |   T[i+1][i] = ...
3429
             |   ... = T[i][i]
3430
             | endloop
3431
             There is no dependence.  */
3432
          if (init_v[loop_depth] != 0
3433
              && dir != dir_star
3434
              && (enum data_dependence_direction) dir_v[loop_depth] != dir
3435
              && (enum data_dependence_direction) dir_v[loop_depth] != dir_star)
3436
            {
3437
              finalize_ddr_dependent (ddr, chrec_known);
3438
              return true;
3439
            }
3440
 
3441
          dir_v[loop_depth] = dir;
3442
          init_v[loop_depth] = 1;
3443
          init_b = true;
3444
        }
3445
    }
3446
 
3447
  /* Save the direction vector if we initialized one.  */
3448
  if (init_b)
3449
    {
3450
      lambda_vector save_v = lambda_vector_new (DDR_SIZE_VECT (ddr));
3451
 
3452
      lambda_vector_copy (dir_v, save_v, DDR_SIZE_VECT (ddr));
3453
      VEC_safe_push (lambda_vector, heap, DDR_DIR_VECTS (ddr), save_v);
3454
    }
3455
 
3456
  /* There is a distance of 1 on all the outer loops:
3457
 
3458
     Example: there is a dependence of distance 1 on loop_1 for the array A.
3459
     | loop_1
3460
     |   A[5] = ...
3461
     | endloop
3462
  */
3463
  {
3464
    struct loop *lca, *loop_a, *loop_b;
3465
    struct data_reference *a = DDR_A (ddr);
3466
    struct data_reference *b = DDR_B (ddr);
3467
    int lca_depth;
3468
    loop_a = loop_containing_stmt (DR_STMT (a));
3469
    loop_b = loop_containing_stmt (DR_STMT (b));
3470
 
3471
    /* Get the common ancestor loop.  */
3472
    lca = find_common_loop (loop_a, loop_b);
3473
    lca_depth = lca->depth - first_loop_depth;
3474
 
3475
    gcc_assert (lca_depth >= 0);
3476
    gcc_assert (lca_depth < nb_loops);
3477
 
3478
    while (lca->depth != 0)
3479
      {
3480
        /* If we're considering just a sub-nest, then don't record
3481
           any information on the outer loops.  */
3482
        if (lca_depth < 0)
3483
          break;
3484
 
3485
        gcc_assert (lca_depth < nb_loops);
3486
 
3487
        if (init_v[lca_depth] == 0)
3488
          {
3489
            lambda_vector save_v = lambda_vector_new (DDR_SIZE_VECT (ddr));
3490
 
3491
            lambda_vector_copy (dir_v, save_v, DDR_SIZE_VECT (ddr));
3492
            save_v[lca_depth] = dir_positive;
3493
            VEC_safe_push (lambda_vector, heap, DDR_DIR_VECTS (ddr), save_v);
3494
          }
3495
 
3496
        lca = lca->outer;
3497
        lca_depth = lca->depth - first_loop_depth;
3498
 
3499
      }
3500
  }
3501
 
3502
  return true;
3503
}
3504
 
3505
/* Returns true when all the access functions of A are affine or
3506
   constant.  */
3507
 
3508
static bool
3509
access_functions_are_affine_or_constant_p (struct data_reference *a)
3510
{
3511
  unsigned int i;
3512
  VEC(tree,heap) **fns = DR_ACCESS_FNS_ADDR (a);
3513
  tree t;
3514
 
3515
  for (i = 0; VEC_iterate (tree, *fns, i, t); i++)
3516
    if (!evolution_function_is_constant_p (t)
3517
        && !evolution_function_is_affine_multivariate_p (t))
3518
      return false;
3519
 
3520
  return true;
3521
}
3522
 
3523
/* This computes the affine dependence relation between A and B.
3524
   CHREC_KNOWN is used for representing the independence between two
3525
   accesses, while CHREC_DONT_KNOW is used for representing the unknown
3526
   relation.
3527
 
3528
   Note that it is possible to stop the computation of the dependence
3529
   relation the first time we detect a CHREC_KNOWN element for a given
3530
   subscript.  */
3531
 
3532
void
3533
compute_affine_dependence (struct data_dependence_relation *ddr)
3534
{
3535
  struct data_reference *dra = DDR_A (ddr);
3536
  struct data_reference *drb = DDR_B (ddr);
3537
 
3538
  if (dump_file && (dump_flags & TDF_DETAILS))
3539
    {
3540
      fprintf (dump_file, "(compute_affine_dependence\n");
3541
      fprintf (dump_file, "  (stmt_a = \n");
3542
      print_generic_expr (dump_file, DR_STMT (dra), 0);
3543
      fprintf (dump_file, ")\n  (stmt_b = \n");
3544
      print_generic_expr (dump_file, DR_STMT (drb), 0);
3545
      fprintf (dump_file, ")\n");
3546
    }
3547
 
3548
  /* Analyze only when the dependence relation is not yet known.  */
3549
  if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
3550
    {
3551
      if (access_functions_are_affine_or_constant_p (dra)
3552
          && access_functions_are_affine_or_constant_p (drb))
3553
        subscript_dependence_tester (ddr);
3554
 
3555
      /* As a last case, if the dependence cannot be determined, or if
3556
         the dependence is considered too difficult to determine, answer
3557
         "don't know".  */
3558
      else
3559
        finalize_ddr_dependent (ddr, chrec_dont_know);
3560
    }
3561
 
3562
  if (dump_file && (dump_flags & TDF_DETAILS))
3563
    fprintf (dump_file, ")\n");
3564
}
3565
 
3566
/* This computes the dependence relation for the same data
3567
   reference into DDR.  */
3568
 
3569
static void
3570
compute_self_dependence (struct data_dependence_relation *ddr)
3571
{
3572
  unsigned int i;
3573
 
3574
  for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
3575
    {
3576
      struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
3577
 
3578
      /* The accessed index overlaps for each iteration.  */
3579
      SUB_CONFLICTS_IN_A (subscript) = integer_zero_node;
3580
      SUB_CONFLICTS_IN_B (subscript) = integer_zero_node;
3581
      SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
3582
    }
3583
}
3584
 
3585
 
3586
typedef struct data_dependence_relation *ddr_p;
3587
DEF_VEC_P(ddr_p);
3588
DEF_VEC_ALLOC_P(ddr_p,heap);
3589
 
3590
/* Compute a subset of the data dependence relation graph.  Don't
3591
   compute read-read and self relations if
3592
   COMPUTE_SELF_AND_READ_READ_DEPENDENCES is FALSE, and avoid the computation
3593
   of the opposite relation, i.e. when AB has been computed, don't compute BA.
3594
   DATAREFS contains a list of data references, and the result is set
3595
   in DEPENDENCE_RELATIONS.  */
3596
 
3597
static void
3598
compute_all_dependences (varray_type datarefs,
3599
                         bool compute_self_and_read_read_dependences,
3600
                         VEC(ddr_p,heap) **dependence_relations)
3601
{
3602
  unsigned int i, j, N;
3603
 
3604
  N = VARRAY_ACTIVE_SIZE (datarefs);
3605
 
3606
  /* Note that we specifically skip i == j because it's a self dependence, and
3607
     use compute_self_dependence below.  */
3608
 
3609
  for (i = 0; i < N; i++)
3610
    for (j = i + 1; j < N; j++)
3611
      {
3612
        struct data_reference *a, *b;
3613
        struct data_dependence_relation *ddr;
3614
 
3615
        a = VARRAY_GENERIC_PTR (datarefs, i);
3616
        b = VARRAY_GENERIC_PTR (datarefs, j);
3617
        if (DR_IS_READ (a) && DR_IS_READ (b)
3618
            && !compute_self_and_read_read_dependences)
3619
          continue;
3620
        ddr = initialize_data_dependence_relation (a, b);
3621
 
3622
        VEC_safe_push (ddr_p, heap, *dependence_relations, ddr);
3623
        compute_affine_dependence (ddr);
3624
        compute_subscript_distance (ddr);
3625
      }
3626
  if (!compute_self_and_read_read_dependences)
3627
    return;
3628
 
3629
  /* Compute self dependence relation of each dataref to itself.  */
3630
 
3631
  for (i = 0; i < N; i++)
3632
    {
3633
      struct data_reference *a, *b;
3634
      struct data_dependence_relation *ddr;
3635
 
3636
      a = VARRAY_GENERIC_PTR (datarefs, i);
3637
      b = VARRAY_GENERIC_PTR (datarefs, i);
3638
      ddr = initialize_data_dependence_relation (a, b);
3639
 
3640
      VEC_safe_push (ddr_p, heap, *dependence_relations, ddr);
3641
      compute_self_dependence (ddr);
3642
      compute_subscript_distance (ddr);
3643
    }
3644
}
3645
 
3646
/* Search the data references in LOOP, and record the information into
3647
   DATAREFS.  Returns chrec_dont_know when failing to analyze a
3648
   difficult case, returns NULL_TREE otherwise.
3649
 
3650
   TODO: This function should be made smarter so that it can handle address
3651
   arithmetic as if they were array accesses, etc.  */
3652
 
3653
tree
3654
find_data_references_in_loop (struct loop *loop, varray_type *datarefs)
3655
{
3656
  basic_block bb, *bbs;
3657
  unsigned int i;
3658
  block_stmt_iterator bsi;
3659
  struct data_reference *dr;
3660
 
3661
  bbs = get_loop_body (loop);
3662
 
3663
  for (i = 0; i < loop->num_nodes; i++)
3664
    {
3665
      bb = bbs[i];
3666
 
3667
      for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
3668
        {
3669
          tree stmt = bsi_stmt (bsi);
3670
 
3671
          /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
3672
             Calls have side-effects, except those to const or pure
3673
             functions.  */
3674
          if ((TREE_CODE (stmt) == CALL_EXPR
3675
               && !(call_expr_flags (stmt) & (ECF_CONST | ECF_PURE)))
3676
              || (TREE_CODE (stmt) == ASM_EXPR
3677
                  && ASM_VOLATILE_P (stmt)))
3678
            goto insert_dont_know_node;
3679
 
3680
          if (ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
3681
            continue;
3682
 
3683
          switch (TREE_CODE (stmt))
3684
            {
3685
            case MODIFY_EXPR:
3686
              {
3687
                bool one_inserted = false;
3688
                tree opnd0 = TREE_OPERAND (stmt, 0);
3689
                tree opnd1 = TREE_OPERAND (stmt, 1);
3690
 
3691
                if (TREE_CODE (opnd0) == ARRAY_REF
3692
                    || TREE_CODE (opnd0) == INDIRECT_REF)
3693
                  {
3694
                    dr = create_data_ref (opnd0, stmt, false);
3695
                    if (dr)
3696
                      {
3697
                        VARRAY_PUSH_GENERIC_PTR (*datarefs, dr);
3698
                        one_inserted = true;
3699
                      }
3700
                  }
3701
 
3702
                if (TREE_CODE (opnd1) == ARRAY_REF
3703
                    || TREE_CODE (opnd1) == INDIRECT_REF)
3704
                  {
3705
                    dr = create_data_ref (opnd1, stmt, true);
3706
                    if (dr)
3707
                      {
3708
                        VARRAY_PUSH_GENERIC_PTR (*datarefs, dr);
3709
                        one_inserted = true;
3710
                      }
3711
                  }
3712
 
3713
                if (!one_inserted)
3714
                  goto insert_dont_know_node;
3715
 
3716
                break;
3717
              }
3718
 
3719
            case CALL_EXPR:
3720
              {
3721
                tree args;
3722
                bool one_inserted = false;
3723
 
3724
                for (args = TREE_OPERAND (stmt, 1); args;
3725
                     args = TREE_CHAIN (args))
3726
                  if (TREE_CODE (TREE_VALUE (args)) == ARRAY_REF
3727
                      || TREE_CODE (TREE_VALUE (args)) == INDIRECT_REF)
3728
                    {
3729
                      dr = create_data_ref (TREE_VALUE (args), stmt, true);
3730
                      if (dr)
3731
                        {
3732
                          VARRAY_PUSH_GENERIC_PTR (*datarefs, dr);
3733
                          one_inserted = true;
3734
                        }
3735
                    }
3736
 
3737
                if (!one_inserted)
3738
                  goto insert_dont_know_node;
3739
 
3740
                break;
3741
              }
3742
 
3743
            default:
3744
                {
3745
                  struct data_reference *res;
3746
 
3747
                insert_dont_know_node:;
3748
                  res = xmalloc (sizeof (struct data_reference));
3749
                  DR_STMT (res) = NULL_TREE;
3750
                  DR_REF (res) = NULL_TREE;
3751
                  DR_BASE_OBJECT (res) = NULL;
3752
                  DR_TYPE (res) = ARRAY_REF_TYPE;
3753
                  DR_SET_ACCESS_FNS (res, NULL);
3754
                  DR_BASE_OBJECT (res) = NULL;
3755
                  DR_IS_READ (res) = false;
3756
                  DR_BASE_ADDRESS (res) = NULL_TREE;
3757
                  DR_OFFSET (res) = NULL_TREE;
3758
                  DR_INIT (res) = NULL_TREE;
3759
                  DR_STEP (res) = NULL_TREE;
3760
                  DR_OFFSET_MISALIGNMENT (res) = NULL_TREE;
3761
                  DR_MEMTAG (res) = NULL_TREE;
3762
                  DR_PTR_INFO (res) = NULL;
3763
                  VARRAY_PUSH_GENERIC_PTR (*datarefs, res);
3764
 
3765
                  free (bbs);
3766
                  return chrec_dont_know;
3767
                }
3768
            }
3769
 
3770
          /* When there are no defs in the loop, the loop is parallel.  */
3771
          if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_DEFS))
3772
            loop->parallel_p = false;
3773
        }
3774
    }
3775
 
3776
  free (bbs);
3777
 
3778
  return NULL_TREE;
3779
}
3780
 
3781
 
3782
 
3783
/* This section contains all the entry points.  */
3784
 
3785
/* Given a loop nest LOOP, the following vectors are returned:
3786
   *DATAREFS is initialized to all the array elements contained in this loop,
3787
   *DEPENDENCE_RELATIONS contains the relations between the data references.
3788
   Compute read-read and self relations if
3789
   COMPUTE_SELF_AND_READ_READ_DEPENDENCES is TRUE.  */
3790
 
3791
void
3792
compute_data_dependences_for_loop (struct loop *loop,
3793
                                   bool compute_self_and_read_read_dependences,
3794
                                   varray_type *datarefs,
3795
                                   varray_type *dependence_relations)
3796
{
3797
  unsigned int i, nb_loops;
3798
  VEC(ddr_p,heap) *allrelations;
3799
  struct data_dependence_relation *ddr;
3800
  struct loop *loop_nest = loop;
3801
 
3802
  while (loop_nest && loop_nest->outer && loop_nest->outer->outer)
3803
    loop_nest = loop_nest->outer;
3804
 
3805
  nb_loops = loop_nest->level;
3806
 
3807
  /* If one of the data references is not computable, give up without
3808
     spending time to compute other dependences.  */
3809
  if (find_data_references_in_loop (loop, datarefs) == chrec_dont_know)
3810
    {
3811
      struct data_dependence_relation *ddr;
3812
 
3813
      /* Insert a single relation into dependence_relations:
3814
         chrec_dont_know.  */
3815
      ddr = initialize_data_dependence_relation (NULL, NULL);
3816
      VARRAY_PUSH_GENERIC_PTR (*dependence_relations, ddr);
3817
      build_classic_dist_vector (ddr, nb_loops, loop->depth);
3818
      build_classic_dir_vector (ddr, nb_loops, loop->depth);
3819
      return;
3820
    }
3821
 
3822
  allrelations = NULL;
3823
  compute_all_dependences (*datarefs, compute_self_and_read_read_dependences,
3824
                           &allrelations);
3825
 
3826
  for (i = 0; VEC_iterate (ddr_p, allrelations, i, ddr); i++)
3827
    {
3828
      if (build_classic_dist_vector (ddr, nb_loops, loop_nest->depth))
3829
        {
3830
          VARRAY_PUSH_GENERIC_PTR (*dependence_relations, ddr);
3831
          build_classic_dir_vector (ddr, nb_loops, loop_nest->depth);
3832
        }
3833
    }
3834
}
3835
 
3836
/* Entry point (for testing only).  Analyze all the data references
3837
   and the dependence relations.
3838
 
3839
   The data references are computed first.
3840
 
3841
   A relation on these nodes is represented by a complete graph.  Some
3842
   of the relations could be of no interest, thus the relations can be
3843
   computed on demand.
3844
 
3845
   In the following function we compute all the relations.  This is
3846
   just a first implementation that is here for:
3847
   - for showing how to ask for the dependence relations,
3848
   - for the debugging the whole dependence graph,
3849
   - for the dejagnu testcases and maintenance.
3850
 
3851
   It is possible to ask only for a part of the graph, avoiding to
3852
   compute the whole dependence graph.  The computed dependences are
3853
   stored in a knowledge base (KB) such that later queries don't
3854
   recompute the same information.  The implementation of this KB is
3855
   transparent to the optimizer, and thus the KB can be changed with a
3856
   more efficient implementation, or the KB could be disabled.  */
3857
 
3858
void
3859
analyze_all_data_dependences (struct loops *loops)
3860
{
3861
  unsigned int i;
3862
  varray_type datarefs;
3863
  varray_type dependence_relations;
3864
  int nb_data_refs = 10;
3865
 
3866
  VARRAY_GENERIC_PTR_INIT (datarefs, nb_data_refs, "datarefs");
3867
  VARRAY_GENERIC_PTR_INIT (dependence_relations,
3868
                           nb_data_refs * nb_data_refs,
3869
                           "dependence_relations");
3870
 
3871
  /* Compute DDs on the whole function.  */
3872
  compute_data_dependences_for_loop (loops->parray[0], false,
3873
                                     &datarefs, &dependence_relations);
3874
 
3875
  if (dump_file)
3876
    {
3877
      dump_data_dependence_relations (dump_file, dependence_relations);
3878
      fprintf (dump_file, "\n\n");
3879
 
3880
      if (dump_flags & TDF_DETAILS)
3881
        dump_dist_dir_vectors (dump_file, dependence_relations);
3882
 
3883
      if (dump_flags & TDF_STATS)
3884
        {
3885
          unsigned nb_top_relations = 0;
3886
          unsigned nb_bot_relations = 0;
3887
          unsigned nb_basename_differ = 0;
3888
          unsigned nb_chrec_relations = 0;
3889
 
3890
          for (i = 0; i < VARRAY_ACTIVE_SIZE (dependence_relations); i++)
3891
            {
3892
              struct data_dependence_relation *ddr;
3893
              ddr = VARRAY_GENERIC_PTR (dependence_relations, i);
3894
 
3895
              if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
3896
                nb_top_relations++;
3897
 
3898
              else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
3899
                {
3900
                  struct data_reference *a = DDR_A (ddr);
3901
                  struct data_reference *b = DDR_B (ddr);
3902
                  bool differ_p;
3903
 
3904
                  if ((DR_BASE_OBJECT (a) && DR_BASE_OBJECT (b)
3905
                       && DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b))
3906
                      || (base_object_differ_p (a, b, &differ_p)
3907
                          && differ_p))
3908
                    nb_basename_differ++;
3909
                  else
3910
                    nb_bot_relations++;
3911
                }
3912
 
3913
              else
3914
                nb_chrec_relations++;
3915
            }
3916
 
3917
          gather_stats_on_scev_database ();
3918
        }
3919
    }
3920
 
3921
  free_dependence_relations (dependence_relations);
3922
  free_data_refs (datarefs);
3923
}
3924
 
3925
/* Free the memory used by a data dependence relation DDR.  */
3926
 
3927
void
3928
free_dependence_relation (struct data_dependence_relation *ddr)
3929
{
3930
  if (ddr == NULL)
3931
    return;
3932
 
3933
  if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_SUBSCRIPTS (ddr))
3934
    varray_clear (DDR_SUBSCRIPTS (ddr));
3935
  free (ddr);
3936
}
3937
 
3938
/* Free the memory used by the data dependence relations from
3939
   DEPENDENCE_RELATIONS.  */
3940
 
3941
void
3942
free_dependence_relations (varray_type dependence_relations)
3943
{
3944
  unsigned int i;
3945
  if (dependence_relations == NULL)
3946
    return;
3947
 
3948
  for (i = 0; i < VARRAY_ACTIVE_SIZE (dependence_relations); i++)
3949
    free_dependence_relation (VARRAY_GENERIC_PTR (dependence_relations, i));
3950
  varray_clear (dependence_relations);
3951
}
3952
 
3953
/* Free the memory used by the data references from DATAREFS.  */
3954
 
3955
void
3956
free_data_refs (varray_type datarefs)
3957
{
3958
  unsigned int i;
3959
 
3960
  if (datarefs == NULL)
3961
    return;
3962
 
3963
  for (i = 0; i < VARRAY_ACTIVE_SIZE (datarefs); i++)
3964
    {
3965
      struct data_reference *dr = (struct data_reference *)
3966
        VARRAY_GENERIC_PTR (datarefs, i);
3967
      if (dr)
3968
        {
3969
          DR_FREE_ACCESS_FNS (dr);
3970
          free (dr);
3971
        }
3972
    }
3973
  varray_clear (datarefs);
3974
}
3975
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.