OSDN Git Service

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