OSDN Git Service

b1d160545bb33cc1251112018a253f8b57a2c00b
[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, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, 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 "errors.h"
82 #include "ggc.h"
83 #include "tree.h"
84
85 /* These RTL headers are needed for basic-block.h.  */
86 #include "rtl.h"
87 #include "basic-block.h"
88 #include "diagnostic.h"
89 #include "tree-flow.h"
90 #include "tree-dump.h"
91 #include "timevar.h"
92 #include "cfgloop.h"
93 #include "tree-chrec.h"
94 #include "tree-data-ref.h"
95 #include "tree-scalar-evolution.h"
96 #include "tree-pass.h"
97
98 /* This is the simplest data dependence test: determines whether the
99    data references A and B access the same array/region.  Returns
100    false when the property is not computable at compile time.
101    Otherwise return true, and DIFFER_P will record the result. This
102    utility will not be necessary when alias_sets_conflict_p will be
103    less conservative.  */
104
105 bool
106 array_base_name_differ_p (struct data_reference *a,
107                           struct data_reference *b,
108                           bool *differ_p)
109 {
110   tree base_a = DR_BASE_NAME (a);
111   tree base_b = DR_BASE_NAME (b);
112
113   if (!base_a || !base_b)
114     return false;
115
116   /* Determine if same base.  Example: for the array accesses
117      a[i], b[i] or pointer accesses *a, *b, bases are a, b.  */
118   if (base_a == base_b)
119     {
120       *differ_p = false;
121       return true;
122     }
123
124   /* For pointer based accesses, (*p)[i], (*q)[j], the bases are (*p)
125      and (*q)  */
126   if (TREE_CODE (base_a) == INDIRECT_REF && TREE_CODE (base_b) == INDIRECT_REF
127       && TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0))
128     {
129       *differ_p = false;
130       return true;
131     }
132
133   /* Record/union based accesses - s.a[i], t.b[j]. bases are s.a,t.b.  */ 
134   if (TREE_CODE (base_a) == COMPONENT_REF && TREE_CODE (base_b) == COMPONENT_REF
135       && TREE_OPERAND (base_a, 0) == TREE_OPERAND (base_b, 0)
136       && TREE_OPERAND (base_a, 1) == TREE_OPERAND (base_b, 1))
137     {
138       *differ_p = false;
139       return true;
140     }
141
142
143   /* Determine if different bases.  */
144
145   /* At this point we know that base_a != base_b.  However, pointer
146      accesses of the form x=(*p) and y=(*q), whose bases are p and q,
147      may still be pointing to the same base. In SSAed GIMPLE p and q will
148      be SSA_NAMES in this case.  Therefore, here we check if they are
149      really two different declarations.  */
150   if (TREE_CODE (base_a) == VAR_DECL && TREE_CODE (base_b) == VAR_DECL)
151     {
152       *differ_p = true;
153       return true;
154     }
155
156   /* Compare two record/union bases s.a and t.b: s != t or (a != b and
157      s and t are not unions).  */
158   if (TREE_CODE (base_a) == COMPONENT_REF && TREE_CODE (base_b) == COMPONENT_REF
159       && ((TREE_CODE (TREE_OPERAND (base_a, 0)) == VAR_DECL
160            && TREE_CODE (TREE_OPERAND (base_b, 0)) == VAR_DECL
161            && TREE_OPERAND (base_a, 0) != TREE_OPERAND (base_b, 0))
162           || (TREE_CODE (TREE_TYPE (TREE_OPERAND (base_a, 0))) == RECORD_TYPE 
163               && TREE_CODE (TREE_TYPE (TREE_OPERAND (base_b, 0))) == RECORD_TYPE
164               && TREE_OPERAND (base_a, 1) != TREE_OPERAND (base_b, 1))))
165     {
166       *differ_p = true;
167       return true;
168     }
169
170   /* Compare a record/union access and an array access.  */ 
171   if ((TREE_CODE (base_a) == VAR_DECL
172        && (TREE_CODE (base_b) == COMPONENT_REF
173            && TREE_CODE (TREE_OPERAND (base_b, 0)) == VAR_DECL))
174       || (TREE_CODE (base_b) == VAR_DECL
175        && (TREE_CODE (base_a) == COMPONENT_REF
176            && TREE_CODE (TREE_OPERAND (base_a, 0)) == VAR_DECL)))
177     {
178       *differ_p = true;
179       return true;
180     }
181
182   return false;
183 }
184
185 /* Returns true iff A divides B.  */
186
187 static inline bool 
188 tree_fold_divides_p (tree type, 
189                      tree a, 
190                      tree b)
191 {
192   /* Determines whether (A == gcd (A, B)).  */
193   return integer_zerop 
194     (fold (build (MINUS_EXPR, type, a, tree_fold_gcd (a, b))));
195 }
196
197 /* Compute the greatest common denominator of two numbers using
198    Euclid's algorithm.  */
199
200 static int 
201 gcd (int a, int b)
202 {
203   
204   int x, y, z;
205   
206   x = abs (a);
207   y = abs (b);
208
209   while (x>0)
210     {
211       z = y % x;
212       y = x;
213       x = z;
214     }
215
216   return (y);
217 }
218
219 /* Returns true iff A divides B.  */
220
221 static inline bool 
222 int_divides_p (int a, int b)
223 {
224   return ((b % a) == 0);
225 }
226
227 \f
228
229 /* Dump into FILE all the data references from DATAREFS.  */ 
230
231 void 
232 dump_data_references (FILE *file, 
233                       varray_type datarefs)
234 {
235   unsigned int i;
236   
237   for (i = 0; i < VARRAY_ACTIVE_SIZE (datarefs); i++)
238     dump_data_reference (file, VARRAY_GENERIC_PTR (datarefs, i));
239 }
240
241 /* Dump into FILE all the dependence relations from DDR.  */ 
242
243 void 
244 dump_data_dependence_relations (FILE *file, 
245                                 varray_type ddr)
246 {
247   unsigned int i;
248   
249   for (i = 0; i < VARRAY_ACTIVE_SIZE (ddr); i++)
250     dump_data_dependence_relation (file, VARRAY_GENERIC_PTR (ddr, i));
251 }
252
253 /* Dump function for a DATA_REFERENCE structure.  */
254
255 void 
256 dump_data_reference (FILE *outf, 
257                      struct data_reference *dr)
258 {
259   unsigned int i;
260   
261   fprintf (outf, "(Data Ref: \n  stmt: ");
262   print_generic_stmt (outf, DR_STMT (dr), 0);
263   fprintf (outf, "  ref: ");
264   print_generic_stmt (outf, DR_REF (dr), 0);
265   fprintf (outf, "  base_name: ");
266   print_generic_stmt (outf, DR_BASE_NAME (dr), 0);
267   
268   for (i = 0; i < DR_NUM_DIMENSIONS (dr); i++)
269     {
270       fprintf (outf, "  Access function %d: ", i);
271       print_generic_stmt (outf, DR_ACCESS_FN (dr, i), 0);
272     }
273   fprintf (outf, ")\n");
274 }
275
276 /* Dump function for a SUBSCRIPT structure.  */
277
278 void 
279 dump_subscript (FILE *outf, struct subscript *subscript)
280 {
281   tree chrec = SUB_CONFLICTS_IN_A (subscript);
282
283   fprintf (outf, "\n (subscript \n");
284   fprintf (outf, "  iterations_that_access_an_element_twice_in_A: ");
285   print_generic_stmt (outf, chrec, 0);
286   if (chrec == chrec_known)
287     fprintf (outf, "    (no dependence)\n");
288   else if (chrec_contains_undetermined (chrec))
289     fprintf (outf, "    (don't know)\n");
290   else
291     {
292       tree last_iteration = SUB_LAST_CONFLICT (subscript);
293       fprintf (outf, "  last_conflict: ");
294       print_generic_stmt (outf, last_iteration, 0);
295     }
296           
297   chrec = SUB_CONFLICTS_IN_B (subscript);
298   fprintf (outf, "  iterations_that_access_an_element_twice_in_B: ");
299   print_generic_stmt (outf, chrec, 0);
300   if (chrec == chrec_known)
301     fprintf (outf, "    (no dependence)\n");
302   else if (chrec_contains_undetermined (chrec))
303     fprintf (outf, "    (don't know)\n");
304   else
305     {
306       tree last_iteration = SUB_LAST_CONFLICT (subscript);
307       fprintf (outf, "  last_conflict: ");
308       print_generic_stmt (outf, last_iteration, 0);
309     }
310
311   fprintf (outf, "  (Subscript distance: ");
312   print_generic_stmt (outf, SUB_DISTANCE (subscript), 0);
313   fprintf (outf, "  )\n");
314   fprintf (outf, " )\n");
315 }
316
317 /* Dump function for a DATA_DEPENDENCE_RELATION structure.  */
318
319 void 
320 dump_data_dependence_relation (FILE *outf, 
321                                struct data_dependence_relation *ddr)
322 {
323   struct data_reference *dra, *drb;
324
325   dra = DDR_A (ddr);
326   drb = DDR_B (ddr);
327   fprintf (outf, "(Data Dep: \n");
328   if (DDR_ARE_DEPENDENT (ddr) == chrec_dont_know)
329     fprintf (outf, "    (don't know)\n");
330   
331   else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
332     fprintf (outf, "    (no dependence)\n");
333   
334   else if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
335     {
336       unsigned int i;
337       for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
338         {
339           fprintf (outf, "  access_fn_A: ");
340           print_generic_stmt (outf, DR_ACCESS_FN (dra, i), 0);
341           fprintf (outf, "  access_fn_B: ");
342           print_generic_stmt (outf, DR_ACCESS_FN (drb, i), 0);
343           dump_subscript (outf, DDR_SUBSCRIPT (ddr, i));
344         }
345       if (DDR_DIST_VECT (ddr))
346         {
347           fprintf (outf, "  distance_vect: ");
348           print_lambda_vector (outf, DDR_DIST_VECT (ddr), DDR_SIZE_VECT (ddr));
349         }
350       if (DDR_DIR_VECT (ddr))
351         {
352           fprintf (outf, "  direction_vect: ");
353           print_lambda_vector (outf, DDR_DIR_VECT (ddr), DDR_SIZE_VECT (ddr));
354         }
355     }
356
357   fprintf (outf, ")\n");
358 }
359
360
361
362 /* Dump function for a DATA_DEPENDENCE_DIRECTION structure.  */
363
364 void
365 dump_data_dependence_direction (FILE *file, 
366                                 enum data_dependence_direction dir)
367 {
368   switch (dir)
369     {
370     case dir_positive: 
371       fprintf (file, "+");
372       break;
373       
374     case dir_negative:
375       fprintf (file, "-");
376       break;
377       
378     case dir_equal:
379       fprintf (file, "=");
380       break;
381       
382     case dir_positive_or_negative:
383       fprintf (file, "+-");
384       break;
385       
386     case dir_positive_or_equal: 
387       fprintf (file, "+=");
388       break;
389       
390     case dir_negative_or_equal: 
391       fprintf (file, "-=");
392       break;
393       
394     case dir_star: 
395       fprintf (file, "*"); 
396       break;
397       
398     default: 
399       break;
400     }
401 }
402
403 /* Dumps the distance and direction vectors in FILE.  DDRS contains
404    the dependence relations, and VECT_SIZE is the size of the
405    dependence vectors, or in other words the number of loops in the
406    considered nest.  */
407
408 void 
409 dump_dist_dir_vectors (FILE *file, varray_type ddrs)
410 {
411   unsigned int i;
412
413   for (i = 0; i < VARRAY_ACTIVE_SIZE (ddrs); i++)
414     {
415       struct data_dependence_relation *ddr = 
416         (struct data_dependence_relation *) 
417         VARRAY_GENERIC_PTR (ddrs, i);
418       if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE
419           && DDR_AFFINE_P (ddr))
420         {
421           fprintf (file, "DISTANCE_V (");
422           print_lambda_vector (file, DDR_DIST_VECT (ddr), DDR_SIZE_VECT (ddr));
423           fprintf (file, ")\n");
424           fprintf (file, "DIRECTION_V (");
425           print_lambda_vector (file, DDR_DIR_VECT (ddr), DDR_SIZE_VECT (ddr));
426           fprintf (file, ")\n");
427         }
428     }
429   fprintf (file, "\n\n");
430 }
431
432 /* Dumps the data dependence relations DDRS in FILE.  */
433
434 void 
435 dump_ddrs (FILE *file, varray_type ddrs)
436 {
437   unsigned int i;
438
439   for (i = 0; i < VARRAY_ACTIVE_SIZE (ddrs); i++)
440     {
441       struct data_dependence_relation *ddr = 
442         (struct data_dependence_relation *) 
443         VARRAY_GENERIC_PTR (ddrs, i);
444       dump_data_dependence_relation (file, ddr);
445     }
446   fprintf (file, "\n\n");
447 }
448
449 \f
450
451 /* Compute the lowest iteration bound for LOOP.  It is an
452    INTEGER_CST.  */
453
454 static void
455 compute_estimated_nb_iterations (struct loop *loop)
456 {
457   tree estimation;
458   struct nb_iter_bound *bound, *next;
459   
460   for (bound = loop->bounds; bound; bound = next)
461     {
462       next = bound->next;
463       estimation = bound->bound;
464
465       if (TREE_CODE (estimation) != INTEGER_CST)
466         continue;
467
468       if (loop->estimated_nb_iterations)
469         {
470           /* Update only if estimation is smaller.  */
471           if (tree_int_cst_lt (estimation, loop->estimated_nb_iterations))
472             loop->estimated_nb_iterations = estimation;
473         }
474       else
475         loop->estimated_nb_iterations = estimation;
476     }
477 }
478
479 /* Estimate the number of iterations from the size of the data and the
480    access functions.  */
481
482 static void
483 estimate_niter_from_size_of_data (struct loop *loop, 
484                                   tree opnd0, 
485                                   tree access_fn, 
486                                   tree stmt)
487 {
488   tree estimation;
489   tree array_size, data_size, element_size;
490   tree init, step;
491
492   init = initial_condition (access_fn);
493   step = evolution_part_in_loop_num (access_fn, loop->num);
494
495   array_size = TYPE_SIZE (TREE_TYPE (opnd0));
496   element_size = TYPE_SIZE (TREE_TYPE (TREE_TYPE (opnd0)));
497   if (array_size == NULL_TREE 
498       || TREE_CODE (array_size) != INTEGER_CST
499       || TREE_CODE (element_size) != INTEGER_CST)
500     return;
501
502   data_size = fold (build2 (EXACT_DIV_EXPR, integer_type_node, 
503                             array_size, element_size));
504
505   if (init != NULL_TREE
506       && step != NULL_TREE
507       && TREE_CODE (init) == INTEGER_CST
508       && TREE_CODE (step) == INTEGER_CST)
509     {
510       estimation = fold (build2 (CEIL_DIV_EXPR, integer_type_node, 
511                                  fold (build2 (MINUS_EXPR, integer_type_node, 
512                                                data_size, init)), step));
513
514       record_estimate (loop, estimation, boolean_true_node, stmt);
515     }
516 }
517
518 /* Given an ARRAY_REF node REF, records its access functions.
519    Example: given A[i][3], record in ACCESS_FNS the opnd1 function,
520    i.e. the constant "3", then recursively call the function on opnd0,
521    i.e. the ARRAY_REF "A[i]".  The function returns the base name:
522    "A".  */
523
524 static tree
525 analyze_array_indexes (struct loop *loop,
526                        VEC(tree,heap) **access_fns, 
527                        tree ref, tree stmt)
528 {
529   tree opnd0, opnd1;
530   tree access_fn;
531   
532   opnd0 = TREE_OPERAND (ref, 0);
533   opnd1 = TREE_OPERAND (ref, 1);
534   
535   /* The detection of the evolution function for this data access is
536      postponed until the dependence test.  This lazy strategy avoids
537      the computation of access functions that are of no interest for
538      the optimizers.  */
539   access_fn = instantiate_parameters 
540     (loop, analyze_scalar_evolution (loop, opnd1));
541
542   if (loop->estimated_nb_iterations == NULL_TREE)
543     estimate_niter_from_size_of_data (loop, opnd0, access_fn, stmt);
544   
545   VEC_safe_push (tree, heap, *access_fns, access_fn);
546   
547   /* Recursively record other array access functions.  */
548   if (TREE_CODE (opnd0) == ARRAY_REF)
549     return analyze_array_indexes (loop, access_fns, opnd0, stmt);
550   
551   /* Return the base name of the data access.  */
552   else
553     return opnd0;
554 }
555
556 /* For a data reference REF contained in the statement STMT, initialize
557    a DATA_REFERENCE structure, and return it.  IS_READ flag has to be
558    set to true when REF is in the right hand side of an
559    assignment.  */
560
561 struct data_reference *
562 analyze_array (tree stmt, tree ref, bool is_read)
563 {
564   struct data_reference *res;
565
566   if (dump_file && (dump_flags & TDF_DETAILS))
567     {
568       fprintf (dump_file, "(analyze_array \n");
569       fprintf (dump_file, "  (ref = ");
570       print_generic_stmt (dump_file, ref, 0);
571       fprintf (dump_file, ")\n");
572     }
573   
574   res = xmalloc (sizeof (struct data_reference));
575   
576   DR_STMT (res) = stmt;
577   DR_REF (res) = ref;
578   DR_ACCESS_FNS (res) = VEC_alloc (tree, heap, 3);
579   DR_BASE_NAME (res) = analyze_array_indexes 
580     (loop_containing_stmt (stmt), &(DR_ACCESS_FNS (res)), ref, stmt);
581   DR_IS_READ (res) = is_read;
582   
583   if (dump_file && (dump_flags & TDF_DETAILS))
584     fprintf (dump_file, ")\n");
585   
586   return res;
587 }
588
589 /* For a data reference REF contained in the statement STMT, initialize
590    a DATA_REFERENCE structure, and return it.  */
591
592 struct data_reference *
593 init_data_ref (tree stmt, 
594                tree ref,
595                tree base,
596                tree access_fn,
597                bool is_read)
598 {
599   struct data_reference *res;
600
601   if (dump_file && (dump_flags & TDF_DETAILS))
602     {
603       fprintf (dump_file, "(init_data_ref \n");
604       fprintf (dump_file, "  (ref = ");
605       print_generic_stmt (dump_file, ref, 0);
606       fprintf (dump_file, ")\n");
607     }
608   
609   res = xmalloc (sizeof (struct data_reference));
610   
611   DR_STMT (res) = stmt;
612   DR_REF (res) = ref;
613   DR_ACCESS_FNS (res) = VEC_alloc (tree, heap, 5);
614   DR_BASE_NAME (res) = base;
615   VEC_quick_push (tree, DR_ACCESS_FNS (res), access_fn);
616   DR_IS_READ (res) = is_read;
617   
618   if (dump_file && (dump_flags & TDF_DETAILS))
619     fprintf (dump_file, ")\n");
620   
621   return res;
622 }
623
624 \f
625
626 /* Returns true when all the functions of a tree_vec CHREC are the
627    same.  */
628
629 static bool 
630 all_chrecs_equal_p (tree chrec)
631 {
632   int j;
633
634   for (j = 0; j < TREE_VEC_LENGTH (chrec) - 1; j++)
635     {
636       tree chrec_j = TREE_VEC_ELT (chrec, j);
637       tree chrec_j_1 = TREE_VEC_ELT (chrec, j + 1);
638       if (!integer_zerop 
639           (chrec_fold_minus 
640            (integer_type_node, chrec_j, chrec_j_1)))
641         return false;
642     }
643   return true;
644 }
645
646 /* Determine for each subscript in the data dependence relation DDR
647    the distance.  */
648
649 void
650 compute_subscript_distance (struct data_dependence_relation *ddr)
651 {
652   if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
653     {
654       unsigned int i;
655       
656       for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
657         {
658           tree conflicts_a, conflicts_b, difference;
659           struct subscript *subscript;
660           
661           subscript = DDR_SUBSCRIPT (ddr, i);
662           conflicts_a = SUB_CONFLICTS_IN_A (subscript);
663           conflicts_b = SUB_CONFLICTS_IN_B (subscript);
664
665           if (TREE_CODE (conflicts_a) == TREE_VEC)
666             {
667               if (!all_chrecs_equal_p (conflicts_a))
668                 {
669                   SUB_DISTANCE (subscript) = chrec_dont_know;
670                   return;
671                 }
672               else
673                 conflicts_a = TREE_VEC_ELT (conflicts_a, 0);
674             }
675
676           if (TREE_CODE (conflicts_b) == TREE_VEC)
677             {
678               if (!all_chrecs_equal_p (conflicts_b))
679                 {
680                   SUB_DISTANCE (subscript) = chrec_dont_know;
681                   return;
682                 }
683               else
684                 conflicts_b = TREE_VEC_ELT (conflicts_b, 0);
685             }
686
687           difference = chrec_fold_minus 
688             (integer_type_node, conflicts_b, conflicts_a);
689           
690           if (evolution_function_is_constant_p (difference))
691             SUB_DISTANCE (subscript) = difference;
692           
693           else
694             SUB_DISTANCE (subscript) = chrec_dont_know;
695         }
696     }
697 }
698
699 /* Initialize a ddr.  */
700
701 struct data_dependence_relation *
702 initialize_data_dependence_relation (struct data_reference *a, 
703                                      struct data_reference *b)
704 {
705   struct data_dependence_relation *res;
706   bool differ_p;
707   
708   res = xmalloc (sizeof (struct data_dependence_relation));
709   DDR_A (res) = a;
710   DDR_B (res) = b;
711
712   if (a == NULL || b == NULL 
713       || DR_BASE_NAME (a) == NULL_TREE
714       || DR_BASE_NAME (b) == NULL_TREE)
715     DDR_ARE_DEPENDENT (res) = chrec_dont_know;    
716
717   /* When the dimensions of A and B differ, we directly initialize
718      the relation to "there is no dependence": chrec_known.  */
719   else if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b)
720            || (array_base_name_differ_p (a, b, &differ_p) && differ_p))
721     DDR_ARE_DEPENDENT (res) = chrec_known;
722   
723   else
724     {
725       unsigned int i;
726       DDR_AFFINE_P (res) = true;
727       DDR_ARE_DEPENDENT (res) = NULL_TREE;
728       DDR_SUBSCRIPTS_VECTOR_INIT (res, DR_NUM_DIMENSIONS (a));
729       DDR_SIZE_VECT (res) = 0;
730       DDR_DIST_VECT (res) = NULL;
731       DDR_DIR_VECT (res) = NULL;
732       
733       for (i = 0; i < DR_NUM_DIMENSIONS (a); i++)
734         {
735           struct subscript *subscript;
736           
737           subscript = xmalloc (sizeof (struct subscript));
738           SUB_CONFLICTS_IN_A (subscript) = chrec_dont_know;
739           SUB_CONFLICTS_IN_B (subscript) = chrec_dont_know;
740           SUB_LAST_CONFLICT (subscript) = chrec_dont_know;
741           SUB_DISTANCE (subscript) = chrec_dont_know;
742           VARRAY_PUSH_GENERIC_PTR (DDR_SUBSCRIPTS (res), subscript);
743         }
744     }
745   
746   return res;
747 }
748
749 /* Set DDR_ARE_DEPENDENT to CHREC and finalize the subscript overlap
750    description.  */
751
752 static inline void
753 finalize_ddr_dependent (struct data_dependence_relation *ddr, 
754                         tree chrec)
755 {
756   if (dump_file && (dump_flags & TDF_DETAILS))
757     {
758       fprintf (dump_file, "(dependence classified: ");
759       print_generic_expr (dump_file, chrec, 0);
760       fprintf (dump_file, ")\n");
761     }
762
763   DDR_ARE_DEPENDENT (ddr) = chrec;  
764   varray_clear (DDR_SUBSCRIPTS (ddr));
765 }
766
767 /* The dependence relation DDR cannot be represented by a distance
768    vector.  */
769
770 static inline void
771 non_affine_dependence_relation (struct data_dependence_relation *ddr)
772 {
773   if (dump_file && (dump_flags & TDF_DETAILS))
774     fprintf (dump_file, "(Dependence relation cannot be represented by distance vector.) \n");
775
776   DDR_AFFINE_P (ddr) = false;
777 }
778
779 \f
780
781 /* This section contains the classic Banerjee tests.  */
782
783 /* Returns true iff CHREC_A and CHREC_B are not dependent on any index
784    variables, i.e., if the ZIV (Zero Index Variable) test is true.  */
785
786 static inline bool
787 ziv_subscript_p (tree chrec_a, 
788                  tree chrec_b)
789 {
790   return (evolution_function_is_constant_p (chrec_a)
791           && evolution_function_is_constant_p (chrec_b));
792 }
793
794 /* Returns true iff CHREC_A and CHREC_B are dependent on an index
795    variable, i.e., if the SIV (Single Index Variable) test is true.  */
796
797 static bool
798 siv_subscript_p (tree chrec_a,
799                  tree chrec_b)
800 {
801   if ((evolution_function_is_constant_p (chrec_a)
802        && evolution_function_is_univariate_p (chrec_b))
803       || (evolution_function_is_constant_p (chrec_b)
804           && evolution_function_is_univariate_p (chrec_a)))
805     return true;
806   
807   if (evolution_function_is_univariate_p (chrec_a)
808       && evolution_function_is_univariate_p (chrec_b))
809     {
810       switch (TREE_CODE (chrec_a))
811         {
812         case POLYNOMIAL_CHREC:
813           switch (TREE_CODE (chrec_b))
814             {
815             case POLYNOMIAL_CHREC:
816               if (CHREC_VARIABLE (chrec_a) != CHREC_VARIABLE (chrec_b))
817                 return false;
818               
819             default:
820               return true;
821             }
822           
823         default:
824           return true;
825         }
826     }
827   
828   return false;
829 }
830
831 /* Analyze a ZIV (Zero Index Variable) subscript.  *OVERLAPS_A and
832    *OVERLAPS_B are initialized to the functions that describe the
833    relation between the elements accessed twice by CHREC_A and
834    CHREC_B.  For k >= 0, the following property is verified:
835
836    CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */
837
838 static void 
839 analyze_ziv_subscript (tree chrec_a, 
840                        tree chrec_b, 
841                        tree *overlaps_a,
842                        tree *overlaps_b, 
843                        tree *last_conflicts)
844 {
845   tree difference;
846   
847   if (dump_file && (dump_flags & TDF_DETAILS))
848     fprintf (dump_file, "(analyze_ziv_subscript \n");
849   
850   difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
851   
852   switch (TREE_CODE (difference))
853     {
854     case INTEGER_CST:
855       if (integer_zerop (difference))
856         {
857           /* The difference is equal to zero: the accessed index
858              overlaps for each iteration in the loop.  */
859           *overlaps_a = integer_zero_node;
860           *overlaps_b = integer_zero_node;
861           *last_conflicts = chrec_dont_know;
862         }
863       else
864         {
865           /* The accesses do not overlap.  */
866           *overlaps_a = chrec_known;
867           *overlaps_b = chrec_known;
868           *last_conflicts = integer_zero_node;
869         }
870       break;
871       
872     default:
873       /* We're not sure whether the indexes overlap.  For the moment, 
874          conservatively answer "don't know".  */
875       *overlaps_a = chrec_dont_know;
876       *overlaps_b = chrec_dont_know;
877       *last_conflicts = chrec_dont_know;
878       break;
879     }
880   
881   if (dump_file && (dump_flags & TDF_DETAILS))
882     fprintf (dump_file, ")\n");
883 }
884
885 /* Analyze a SIV (Single Index Variable) subscript where CHREC_A is a
886    constant, and CHREC_B is an affine function.  *OVERLAPS_A and
887    *OVERLAPS_B are initialized to the functions that describe the
888    relation between the elements accessed twice by CHREC_A and
889    CHREC_B.  For k >= 0, the following property is verified:
890
891    CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */
892
893 static void
894 analyze_siv_subscript_cst_affine (tree chrec_a, 
895                                   tree chrec_b,
896                                   tree *overlaps_a, 
897                                   tree *overlaps_b, 
898                                   tree *last_conflicts)
899 {
900   bool value0, value1, value2;
901   tree difference = chrec_fold_minus 
902     (integer_type_node, CHREC_LEFT (chrec_b), chrec_a);
903   
904   if (!chrec_is_positive (initial_condition (difference), &value0))
905     {
906       *overlaps_a = chrec_dont_know;
907       *overlaps_b = chrec_dont_know;
908       *last_conflicts = chrec_dont_know;
909       return;
910     }
911   else
912     {
913       if (value0 == false)
914         {
915           if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value1))
916             {
917               *overlaps_a = chrec_dont_know;
918               *overlaps_b = chrec_dont_know;      
919               *last_conflicts = chrec_dont_know;
920               return;
921             }
922           else
923             {
924               if (value1 == true)
925                 {
926                   /* Example:  
927                      chrec_a = 12
928                      chrec_b = {10, +, 1}
929                   */
930                   
931                   if (tree_fold_divides_p 
932                       (integer_type_node, CHREC_RIGHT (chrec_b), difference))
933                     {
934                       *overlaps_a = integer_zero_node;
935                       *overlaps_b = fold 
936                         (build (EXACT_DIV_EXPR, integer_type_node, 
937                                 fold (build1 (ABS_EXPR, integer_type_node, difference)), 
938                                 CHREC_RIGHT (chrec_b)));
939                       *last_conflicts = integer_one_node;
940                       return;
941                     }
942                   
943                   /* When the step does not divides the difference, there are
944                      no overlaps.  */
945                   else
946                     {
947                       *overlaps_a = chrec_known;
948                       *overlaps_b = chrec_known;      
949                       *last_conflicts = integer_zero_node;
950                       return;
951                     }
952                 }
953               
954               else
955                 {
956                   /* Example:  
957                      chrec_a = 12
958                      chrec_b = {10, +, -1}
959                      
960                      In this case, chrec_a will not overlap with chrec_b.  */
961                   *overlaps_a = chrec_known;
962                   *overlaps_b = chrec_known;
963                   *last_conflicts = integer_zero_node;
964                   return;
965                 }
966             }
967         }
968       else 
969         {
970           if (!chrec_is_positive (CHREC_RIGHT (chrec_b), &value2))
971             {
972               *overlaps_a = chrec_dont_know;
973               *overlaps_b = chrec_dont_know;      
974               *last_conflicts = chrec_dont_know;
975               return;
976             }
977           else
978             {
979               if (value2 == false)
980                 {
981                   /* Example:  
982                      chrec_a = 3
983                      chrec_b = {10, +, -1}
984                   */
985                   if (tree_fold_divides_p 
986                       (integer_type_node, CHREC_RIGHT (chrec_b), difference))
987                     {
988                       *overlaps_a = integer_zero_node;
989                       *overlaps_b = fold 
990                         (build (EXACT_DIV_EXPR, integer_type_node, difference, 
991                                 CHREC_RIGHT (chrec_b)));
992                       *last_conflicts = integer_one_node;
993                       return;
994                     }
995                   
996                   /* When the step does not divides the difference, there
997                      are no overlaps.  */
998                   else
999                     {
1000                       *overlaps_a = chrec_known;
1001                       *overlaps_b = chrec_known;      
1002                       *last_conflicts = integer_zero_node;
1003                       return;
1004                     }
1005                 }
1006               else
1007                 {
1008                   /* Example:  
1009                      chrec_a = 3  
1010                      chrec_b = {4, +, 1}
1011                  
1012                      In this case, chrec_a will not overlap with chrec_b.  */
1013                   *overlaps_a = chrec_known;
1014                   *overlaps_b = chrec_known;
1015                   *last_conflicts = integer_zero_node;
1016                   return;
1017                 }
1018             }
1019         }
1020     }
1021 }
1022
1023 /* Helper recursive function for initializing the matrix A.  Returns
1024    the initial value of CHREC.  */
1025
1026 static int
1027 initialize_matrix_A (lambda_matrix A, tree chrec, unsigned index, int mult)
1028 {
1029   gcc_assert (chrec);
1030
1031   if (TREE_CODE (chrec) != POLYNOMIAL_CHREC)
1032     return int_cst_value (chrec);
1033
1034   A[index][0] = mult * int_cst_value (CHREC_RIGHT (chrec));
1035   return initialize_matrix_A (A, CHREC_LEFT (chrec), index + 1, mult);
1036 }
1037
1038 #define FLOOR_DIV(x,y) ((x) / (y))
1039
1040 /* Solves the special case of the Diophantine equation: 
1041    | {0, +, STEP_A}_x (OVERLAPS_A) = {0, +, STEP_B}_y (OVERLAPS_B)
1042
1043    Computes the descriptions OVERLAPS_A and OVERLAPS_B.  NITER is the
1044    number of iterations that loops X and Y run.  The overlaps will be
1045    constructed as evolutions in dimension DIM.  */
1046
1047 static void
1048 compute_overlap_steps_for_affine_univar (int niter, int step_a, int step_b, 
1049                                          tree *overlaps_a, tree *overlaps_b, 
1050                                          tree *last_conflicts, int dim)
1051 {
1052   if (((step_a > 0 && step_b > 0)
1053        || (step_a < 0 && step_b < 0)))
1054     {
1055       int step_overlaps_a, step_overlaps_b;
1056       int gcd_steps_a_b, last_conflict, tau2;
1057
1058       gcd_steps_a_b = gcd (step_a, step_b);
1059       step_overlaps_a = step_b / gcd_steps_a_b;
1060       step_overlaps_b = step_a / gcd_steps_a_b;
1061
1062       tau2 = FLOOR_DIV (niter, step_overlaps_a);
1063       tau2 = MIN (tau2, FLOOR_DIV (niter, step_overlaps_b));
1064       last_conflict = tau2;
1065
1066       *overlaps_a = build_polynomial_chrec
1067         (dim, integer_zero_node,
1068          build_int_cst (NULL_TREE, step_overlaps_a));
1069       *overlaps_b = build_polynomial_chrec
1070         (dim, integer_zero_node,
1071          build_int_cst (NULL_TREE, step_overlaps_b));
1072       *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
1073     }
1074
1075   else
1076     {
1077       *overlaps_a = integer_zero_node;
1078       *overlaps_b = integer_zero_node;
1079       *last_conflicts = integer_zero_node;
1080     }
1081 }
1082
1083
1084 /* Solves the special case of a Diophantine equation where CHREC_A is
1085    an affine bivariate function, and CHREC_B is an affine univariate
1086    function.  For example, 
1087
1088    | {{0, +, 1}_x, +, 1335}_y = {0, +, 1336}_z
1089    
1090    has the following overlapping functions: 
1091
1092    | x (t, u, v) = {{0, +, 1336}_t, +, 1}_v
1093    | y (t, u, v) = {{0, +, 1336}_u, +, 1}_v
1094    | z (t, u, v) = {{{0, +, 1}_t, +, 1335}_u, +, 1}_v
1095
1096    FORNOW: This is a specialized implementation for a case occurring in
1097    a common benchmark.  Implement the general algorithm.  */
1098
1099 static void
1100 compute_overlap_steps_for_affine_1_2 (tree chrec_a, tree chrec_b, 
1101                                       tree *overlaps_a, tree *overlaps_b, 
1102                                       tree *last_conflicts)
1103 {
1104   bool xz_p, yz_p, xyz_p;
1105   int step_x, step_y, step_z;
1106   int niter_x, niter_y, niter_z, niter;
1107   tree numiter_x, numiter_y, numiter_z;
1108   tree overlaps_a_xz, overlaps_b_xz, last_conflicts_xz;
1109   tree overlaps_a_yz, overlaps_b_yz, last_conflicts_yz;
1110   tree overlaps_a_xyz, overlaps_b_xyz, last_conflicts_xyz;
1111
1112   step_x = int_cst_value (CHREC_RIGHT (CHREC_LEFT (chrec_a)));
1113   step_y = int_cst_value (CHREC_RIGHT (chrec_a));
1114   step_z = int_cst_value (CHREC_RIGHT (chrec_b));
1115
1116   numiter_x = number_of_iterations_in_loop 
1117     (current_loops->parray[CHREC_VARIABLE (CHREC_LEFT (chrec_a))]);
1118   numiter_y = number_of_iterations_in_loop 
1119     (current_loops->parray[CHREC_VARIABLE (chrec_a)]);
1120   numiter_z = number_of_iterations_in_loop 
1121     (current_loops->parray[CHREC_VARIABLE (chrec_b)]);
1122
1123   if (TREE_CODE (numiter_x) != INTEGER_CST)
1124     numiter_x = current_loops->parray[CHREC_VARIABLE (CHREC_LEFT (chrec_a))]
1125       ->estimated_nb_iterations;
1126   if (TREE_CODE (numiter_y) != INTEGER_CST)
1127     numiter_y = current_loops->parray[CHREC_VARIABLE (chrec_a)]
1128       ->estimated_nb_iterations;
1129   if (TREE_CODE (numiter_z) != INTEGER_CST)
1130     numiter_z = current_loops->parray[CHREC_VARIABLE (chrec_b)]
1131       ->estimated_nb_iterations;
1132
1133   if (numiter_x == NULL_TREE || numiter_y == NULL_TREE 
1134       || numiter_z == NULL_TREE)
1135     {
1136       *overlaps_a = chrec_dont_know;
1137       *overlaps_b = chrec_dont_know;
1138       *last_conflicts = chrec_dont_know;
1139       return;
1140     }
1141
1142   niter_x = int_cst_value (numiter_x);
1143   niter_y = int_cst_value (numiter_y);
1144   niter_z = int_cst_value (numiter_z);
1145
1146   niter = MIN (niter_x, niter_z);
1147   compute_overlap_steps_for_affine_univar (niter, step_x, step_z,
1148                                            &overlaps_a_xz,
1149                                            &overlaps_b_xz,
1150                                            &last_conflicts_xz, 1);
1151   niter = MIN (niter_y, niter_z);
1152   compute_overlap_steps_for_affine_univar (niter, step_y, step_z,
1153                                            &overlaps_a_yz,
1154                                            &overlaps_b_yz,
1155                                            &last_conflicts_yz, 2);
1156   niter = MIN (niter_x, niter_z);
1157   niter = MIN (niter_y, niter);
1158   compute_overlap_steps_for_affine_univar (niter, step_x + step_y, step_z,
1159                                            &overlaps_a_xyz,
1160                                            &overlaps_b_xyz,
1161                                            &last_conflicts_xyz, 3);
1162
1163   xz_p = !integer_zerop (last_conflicts_xz);
1164   yz_p = !integer_zerop (last_conflicts_yz);
1165   xyz_p = !integer_zerop (last_conflicts_xyz);
1166
1167   if (xz_p || yz_p || xyz_p)
1168     {
1169       *overlaps_a = make_tree_vec (2);
1170       TREE_VEC_ELT (*overlaps_a, 0) = integer_zero_node;
1171       TREE_VEC_ELT (*overlaps_a, 1) = integer_zero_node;
1172       *overlaps_b = integer_zero_node;
1173       if (xz_p)
1174         {
1175           TREE_VEC_ELT (*overlaps_a, 0) = 
1176             chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 0),
1177                              overlaps_a_xz);
1178           *overlaps_b = 
1179             chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_xz);
1180           *last_conflicts = last_conflicts_xz;
1181         }
1182       if (yz_p)
1183         {
1184           TREE_VEC_ELT (*overlaps_a, 1) = 
1185             chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 1),
1186                              overlaps_a_yz);
1187           *overlaps_b = 
1188             chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_yz);
1189           *last_conflicts = last_conflicts_yz;
1190         }
1191       if (xyz_p)
1192         {
1193           TREE_VEC_ELT (*overlaps_a, 0) = 
1194             chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 0),
1195                              overlaps_a_xyz);
1196           TREE_VEC_ELT (*overlaps_a, 1) = 
1197             chrec_fold_plus (integer_type_node, TREE_VEC_ELT (*overlaps_a, 1),
1198                              overlaps_a_xyz);
1199           *overlaps_b = 
1200             chrec_fold_plus (integer_type_node, *overlaps_b, overlaps_b_xyz);
1201           *last_conflicts = last_conflicts_xyz;
1202         }
1203     }
1204   else
1205     {
1206       *overlaps_a = integer_zero_node;
1207       *overlaps_b = integer_zero_node;
1208       *last_conflicts = integer_zero_node;
1209     }
1210 }
1211
1212 /* Determines the overlapping elements due to accesses CHREC_A and
1213    CHREC_B, that are affine functions.  This is a part of the
1214    subscript analyzer.  */
1215
1216 static void
1217 analyze_subscript_affine_affine (tree chrec_a, 
1218                                  tree chrec_b,
1219                                  tree *overlaps_a, 
1220                                  tree *overlaps_b, 
1221                                  tree *last_conflicts)
1222 {
1223   unsigned nb_vars_a, nb_vars_b, dim;
1224   int init_a, init_b, gamma, gcd_alpha_beta;
1225   int tau1, tau2;
1226   lambda_matrix A, U, S;
1227
1228   if (dump_file && (dump_flags & TDF_DETAILS))
1229     fprintf (dump_file, "(analyze_subscript_affine_affine \n");
1230   
1231   /* For determining the initial intersection, we have to solve a
1232      Diophantine equation.  This is the most time consuming part.
1233      
1234      For answering to the question: "Is there a dependence?" we have
1235      to prove that there exists a solution to the Diophantine
1236      equation, and that the solution is in the iteration domain,
1237      i.e. the solution is positive or zero, and that the solution
1238      happens before the upper bound loop.nb_iterations.  Otherwise
1239      there is no dependence.  This function outputs a description of
1240      the iterations that hold the intersections.  */
1241
1242   
1243   nb_vars_a = nb_vars_in_chrec (chrec_a);
1244   nb_vars_b = nb_vars_in_chrec (chrec_b);
1245
1246   dim = nb_vars_a + nb_vars_b;
1247   U = lambda_matrix_new (dim, dim);
1248   A = lambda_matrix_new (dim, 1);
1249   S = lambda_matrix_new (dim, 1);
1250
1251   init_a = initialize_matrix_A (A, chrec_a, 0, 1);
1252   init_b = initialize_matrix_A (A, chrec_b, nb_vars_a, -1);
1253   gamma = init_b - init_a;
1254
1255   /* Don't do all the hard work of solving the Diophantine equation
1256      when we already know the solution: for example, 
1257      | {3, +, 1}_1
1258      | {3, +, 4}_2
1259      | gamma = 3 - 3 = 0.
1260      Then the first overlap occurs during the first iterations: 
1261      | {3, +, 1}_1 ({0, +, 4}_x) = {3, +, 4}_2 ({0, +, 1}_x)
1262   */
1263   if (gamma == 0)
1264     {
1265       if (nb_vars_a == 1 && nb_vars_b == 1)
1266         {
1267           int step_a, step_b;
1268           int niter, niter_a, niter_b;
1269           tree numiter_a, numiter_b;
1270
1271           numiter_a = number_of_iterations_in_loop 
1272             (current_loops->parray[CHREC_VARIABLE (chrec_a)]);
1273           numiter_b = number_of_iterations_in_loop 
1274             (current_loops->parray[CHREC_VARIABLE (chrec_b)]);
1275
1276           if (TREE_CODE (numiter_a) != INTEGER_CST)
1277             numiter_a = current_loops->parray[CHREC_VARIABLE (chrec_a)]
1278               ->estimated_nb_iterations;
1279           if (TREE_CODE (numiter_b) != INTEGER_CST)
1280             numiter_b = current_loops->parray[CHREC_VARIABLE (chrec_b)]
1281               ->estimated_nb_iterations;
1282           if (numiter_a == NULL_TREE || numiter_b == NULL_TREE)
1283             {
1284               *overlaps_a = chrec_dont_know;
1285               *overlaps_b = chrec_dont_know;
1286               *last_conflicts = chrec_dont_know;
1287               return;
1288             }
1289
1290           niter_a = int_cst_value (numiter_a);
1291           niter_b = int_cst_value (numiter_b);
1292           niter = MIN (niter_a, niter_b);
1293
1294           step_a = int_cst_value (CHREC_RIGHT (chrec_a));
1295           step_b = int_cst_value (CHREC_RIGHT (chrec_b));
1296
1297           compute_overlap_steps_for_affine_univar (niter, step_a, step_b, 
1298                                                    overlaps_a, overlaps_b, 
1299                                                    last_conflicts, 1);
1300         }
1301
1302       else if (nb_vars_a == 2 && nb_vars_b == 1)
1303         compute_overlap_steps_for_affine_1_2
1304           (chrec_a, chrec_b, overlaps_a, overlaps_b, last_conflicts);
1305
1306       else if (nb_vars_a == 1 && nb_vars_b == 2)
1307         compute_overlap_steps_for_affine_1_2
1308           (chrec_b, chrec_a, overlaps_b, overlaps_a, last_conflicts);
1309
1310       else
1311         {
1312           *overlaps_a = chrec_dont_know;
1313           *overlaps_b = chrec_dont_know;
1314           *last_conflicts = chrec_dont_know;
1315         }
1316       return;
1317     }
1318
1319   /* U.A = S */
1320   lambda_matrix_right_hermite (A, dim, 1, S, U);
1321
1322   if (S[0][0] < 0)
1323     {
1324       S[0][0] *= -1;
1325       lambda_matrix_row_negate (U, dim, 0);
1326     }
1327   gcd_alpha_beta = S[0][0];
1328
1329   /* The classic "gcd-test".  */
1330   if (!int_divides_p (gcd_alpha_beta, gamma))
1331     {
1332       /* The "gcd-test" has determined that there is no integer
1333          solution, i.e. there is no dependence.  */
1334       *overlaps_a = chrec_known;
1335       *overlaps_b = chrec_known;
1336       *last_conflicts = integer_zero_node;
1337     }
1338
1339   /* Both access functions are univariate.  This includes SIV and MIV cases.  */
1340   else if (nb_vars_a == 1 && nb_vars_b == 1)
1341     {
1342       /* Both functions should have the same evolution sign.  */
1343       if (((A[0][0] > 0 && -A[1][0] > 0)
1344            || (A[0][0] < 0 && -A[1][0] < 0)))
1345         {
1346           /* The solutions are given by:
1347              | 
1348              | [GAMMA/GCD_ALPHA_BETA  t].[u11 u12]  = [x0]
1349              |                           [u21 u22]    [y0]
1350          
1351              For a given integer t.  Using the following variables,
1352          
1353              | i0 = u11 * gamma / gcd_alpha_beta
1354              | j0 = u12 * gamma / gcd_alpha_beta
1355              | i1 = u21
1356              | j1 = u22
1357          
1358              the solutions are:
1359          
1360              | x0 = i0 + i1 * t, 
1361              | y0 = j0 + j1 * t.  */
1362       
1363           int i0, j0, i1, j1;
1364
1365           /* X0 and Y0 are the first iterations for which there is a
1366              dependence.  X0, Y0 are two solutions of the Diophantine
1367              equation: chrec_a (X0) = chrec_b (Y0).  */
1368           int x0, y0;
1369           int niter, niter_a, niter_b;
1370           tree numiter_a, numiter_b;
1371
1372           numiter_a = number_of_iterations_in_loop 
1373             (current_loops->parray[CHREC_VARIABLE (chrec_a)]);
1374           numiter_b = number_of_iterations_in_loop 
1375             (current_loops->parray[CHREC_VARIABLE (chrec_b)]);
1376
1377           if (TREE_CODE (numiter_a) != INTEGER_CST)
1378             numiter_a = current_loops->parray[CHREC_VARIABLE (chrec_a)]
1379               ->estimated_nb_iterations;
1380           if (TREE_CODE (numiter_b) != INTEGER_CST)
1381             numiter_b = current_loops->parray[CHREC_VARIABLE (chrec_b)]
1382               ->estimated_nb_iterations;
1383           if (numiter_a == NULL_TREE || numiter_b == NULL_TREE)
1384             {
1385               *overlaps_a = chrec_dont_know;
1386               *overlaps_b = chrec_dont_know;
1387               *last_conflicts = chrec_dont_know;
1388               return;
1389             }
1390
1391           niter_a = int_cst_value (numiter_a);
1392           niter_b = int_cst_value (numiter_b);
1393           niter = MIN (niter_a, niter_b);
1394
1395           i0 = U[0][0] * gamma / gcd_alpha_beta;
1396           j0 = U[0][1] * gamma / gcd_alpha_beta;
1397           i1 = U[1][0];
1398           j1 = U[1][1];
1399
1400           if ((i1 == 0 && i0 < 0)
1401               || (j1 == 0 && j0 < 0))
1402             {
1403               /* There is no solution.  
1404                  FIXME: The case "i0 > nb_iterations, j0 > nb_iterations" 
1405                  falls in here, but for the moment we don't look at the 
1406                  upper bound of the iteration domain.  */
1407               *overlaps_a = chrec_known;
1408               *overlaps_b = chrec_known;
1409               *last_conflicts = integer_zero_node;
1410             }
1411
1412           else 
1413             {
1414               if (i1 > 0)
1415                 {
1416                   tau1 = CEIL (-i0, i1);
1417                   tau2 = FLOOR_DIV (niter - i0, i1);
1418
1419                   if (j1 > 0)
1420                     {
1421                       int last_conflict, min_multiple;
1422                       tau1 = MAX (tau1, CEIL (-j0, j1));
1423                       tau2 = MIN (tau2, FLOOR_DIV (niter - j0, j1));
1424
1425                       x0 = i1 * tau1 + i0;
1426                       y0 = j1 * tau1 + j0;
1427
1428                       /* At this point (x0, y0) is one of the
1429                          solutions to the Diophantine equation.  The
1430                          next step has to compute the smallest
1431                          positive solution: the first conflicts.  */
1432                       min_multiple = MIN (x0 / i1, y0 / j1);
1433                       x0 -= i1 * min_multiple;
1434                       y0 -= j1 * min_multiple;
1435
1436                       tau1 = (x0 - i0)/i1;
1437                       last_conflict = tau2 - tau1;
1438
1439                       *overlaps_a = build_polynomial_chrec
1440                         (1,
1441                          build_int_cst (NULL_TREE, x0),
1442                          build_int_cst (NULL_TREE, i1));
1443                       *overlaps_b = build_polynomial_chrec
1444                         (1,
1445                          build_int_cst (NULL_TREE, y0),
1446                          build_int_cst (NULL_TREE, j1));
1447                       *last_conflicts = build_int_cst (NULL_TREE, last_conflict);
1448                     }
1449                   else
1450                     {
1451                       /* FIXME: For the moment, the upper bound of the
1452                          iteration domain for j is not checked.  */
1453                       *overlaps_a = chrec_dont_know;
1454                       *overlaps_b = chrec_dont_know;
1455                       *last_conflicts = chrec_dont_know;
1456                     }
1457                 }
1458           
1459               else
1460                 {
1461                   /* FIXME: For the moment, the upper bound of the
1462                      iteration domain for i is not checked.  */
1463                   *overlaps_a = chrec_dont_know;
1464                   *overlaps_b = chrec_dont_know;
1465                   *last_conflicts = chrec_dont_know;
1466                 }
1467             }
1468         }
1469       else
1470         {
1471           *overlaps_a = chrec_dont_know;
1472           *overlaps_b = chrec_dont_know;
1473           *last_conflicts = chrec_dont_know;
1474         }
1475     }
1476
1477   else
1478     {
1479       *overlaps_a = chrec_dont_know;
1480       *overlaps_b = chrec_dont_know;
1481       *last_conflicts = chrec_dont_know;
1482     }
1483
1484
1485   if (dump_file && (dump_flags & TDF_DETAILS))
1486     {
1487       fprintf (dump_file, "  (overlaps_a = ");
1488       print_generic_expr (dump_file, *overlaps_a, 0);
1489       fprintf (dump_file, ")\n  (overlaps_b = ");
1490       print_generic_expr (dump_file, *overlaps_b, 0);
1491       fprintf (dump_file, ")\n");
1492     }
1493   
1494   if (dump_file && (dump_flags & TDF_DETAILS))
1495     fprintf (dump_file, ")\n");
1496 }
1497
1498 /* Analyze a SIV (Single Index Variable) subscript.  *OVERLAPS_A and
1499    *OVERLAPS_B are initialized to the functions that describe the
1500    relation between the elements accessed twice by CHREC_A and
1501    CHREC_B.  For k >= 0, the following property is verified:
1502
1503    CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */
1504
1505 static void
1506 analyze_siv_subscript (tree chrec_a, 
1507                        tree chrec_b,
1508                        tree *overlaps_a, 
1509                        tree *overlaps_b, 
1510                        tree *last_conflicts)
1511 {
1512   if (dump_file && (dump_flags & TDF_DETAILS))
1513     fprintf (dump_file, "(analyze_siv_subscript \n");
1514   
1515   if (evolution_function_is_constant_p (chrec_a)
1516       && evolution_function_is_affine_p (chrec_b))
1517     analyze_siv_subscript_cst_affine (chrec_a, chrec_b, 
1518                                       overlaps_a, overlaps_b, last_conflicts);
1519   
1520   else if (evolution_function_is_affine_p (chrec_a)
1521            && evolution_function_is_constant_p (chrec_b))
1522     analyze_siv_subscript_cst_affine (chrec_b, chrec_a, 
1523                                       overlaps_b, overlaps_a, last_conflicts);
1524   
1525   else if (evolution_function_is_affine_p (chrec_a)
1526            && evolution_function_is_affine_p (chrec_b))
1527     analyze_subscript_affine_affine (chrec_a, chrec_b, 
1528                                      overlaps_a, overlaps_b, last_conflicts);
1529   else
1530     {
1531       *overlaps_a = chrec_dont_know;
1532       *overlaps_b = chrec_dont_know;
1533       *last_conflicts = chrec_dont_know;
1534     }
1535   
1536   if (dump_file && (dump_flags & TDF_DETAILS))
1537     fprintf (dump_file, ")\n");
1538 }
1539
1540 /* Return true when the evolution steps of an affine CHREC divide the
1541    constant CST.  */
1542
1543 static bool
1544 chrec_steps_divide_constant_p (tree chrec, 
1545                                tree cst)
1546 {
1547   switch (TREE_CODE (chrec))
1548     {
1549     case POLYNOMIAL_CHREC:
1550       return (tree_fold_divides_p (integer_type_node, CHREC_RIGHT (chrec), cst)
1551               && chrec_steps_divide_constant_p (CHREC_LEFT (chrec), cst));
1552       
1553     default:
1554       /* On the initial condition, return true.  */
1555       return true;
1556     }
1557 }
1558
1559 /* Analyze a MIV (Multiple Index Variable) subscript.  *OVERLAPS_A and
1560    *OVERLAPS_B are initialized to the functions that describe the
1561    relation between the elements accessed twice by CHREC_A and
1562    CHREC_B.  For k >= 0, the following property is verified:
1563
1564    CHREC_A (*OVERLAPS_A (k)) = CHREC_B (*OVERLAPS_B (k)).  */
1565
1566 static void
1567 analyze_miv_subscript (tree chrec_a, 
1568                        tree chrec_b, 
1569                        tree *overlaps_a, 
1570                        tree *overlaps_b, 
1571                        tree *last_conflicts)
1572 {
1573   /* FIXME:  This is a MIV subscript, not yet handled.
1574      Example: (A[{1, +, 1}_1] vs. A[{1, +, 1}_2]) that comes from 
1575      (A[i] vs. A[j]).  
1576      
1577      In the SIV test we had to solve a Diophantine equation with two
1578      variables.  In the MIV case we have to solve a Diophantine
1579      equation with 2*n variables (if the subscript uses n IVs).
1580   */
1581   tree difference;
1582   
1583   if (dump_file && (dump_flags & TDF_DETAILS))
1584     fprintf (dump_file, "(analyze_miv_subscript \n");
1585   
1586   difference = chrec_fold_minus (integer_type_node, chrec_a, chrec_b);
1587   
1588   if (chrec_zerop (difference))
1589     {
1590       /* Access functions are the same: all the elements are accessed
1591          in the same order.  */
1592       *overlaps_a = integer_zero_node;
1593       *overlaps_b = integer_zero_node;
1594       *last_conflicts = number_of_iterations_in_loop 
1595         (current_loops->parray[CHREC_VARIABLE (chrec_a)]);
1596     }
1597   
1598   else if (evolution_function_is_constant_p (difference)
1599            /* For the moment, the following is verified:
1600               evolution_function_is_affine_multivariate_p (chrec_a) */
1601            && !chrec_steps_divide_constant_p (chrec_a, difference))
1602     {
1603       /* testsuite/.../ssa-chrec-33.c
1604          {{21, +, 2}_1, +, -2}_2  vs.  {{20, +, 2}_1, +, -2}_2 
1605         
1606          The difference is 1, and the evolution steps are equal to 2,
1607          consequently there are no overlapping elements.  */
1608       *overlaps_a = chrec_known;
1609       *overlaps_b = chrec_known;
1610       *last_conflicts = integer_zero_node;
1611     }
1612   
1613   else if (evolution_function_is_affine_multivariate_p (chrec_a)
1614            && evolution_function_is_affine_multivariate_p (chrec_b))
1615     {
1616       /* testsuite/.../ssa-chrec-35.c
1617          {0, +, 1}_2  vs.  {0, +, 1}_3
1618          the overlapping elements are respectively located at iterations:
1619          {0, +, 1}_x and {0, +, 1}_x, 
1620          in other words, we have the equality: 
1621          {0, +, 1}_2 ({0, +, 1}_x) = {0, +, 1}_3 ({0, +, 1}_x)
1622          
1623          Other examples: 
1624          {{0, +, 1}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y) = 
1625          {0, +, 1}_1 ({{0, +, 1}_x, +, 2}_y)
1626
1627          {{0, +, 2}_1, +, 3}_2 ({0, +, 1}_y, {0, +, 1}_x) = 
1628          {{0, +, 3}_1, +, 2}_2 ({0, +, 1}_x, {0, +, 1}_y)
1629       */
1630       analyze_subscript_affine_affine (chrec_a, chrec_b, 
1631                                        overlaps_a, overlaps_b, last_conflicts);
1632     }
1633   
1634   else
1635     {
1636       /* When the analysis is too difficult, answer "don't know".  */
1637       *overlaps_a = chrec_dont_know;
1638       *overlaps_b = chrec_dont_know;
1639       *last_conflicts = chrec_dont_know;
1640     }
1641   
1642   if (dump_file && (dump_flags & TDF_DETAILS))
1643     fprintf (dump_file, ")\n");
1644 }
1645
1646 /* Determines the iterations for which CHREC_A is equal to CHREC_B.
1647    OVERLAP_ITERATIONS_A and OVERLAP_ITERATIONS_B are initialized with
1648    two functions that describe the iterations that contain conflicting
1649    elements.
1650    
1651    Remark: For an integer k >= 0, the following equality is true:
1652    
1653    CHREC_A (OVERLAP_ITERATIONS_A (k)) == CHREC_B (OVERLAP_ITERATIONS_B (k)).
1654 */
1655
1656 static void 
1657 analyze_overlapping_iterations (tree chrec_a, 
1658                                 tree chrec_b, 
1659                                 tree *overlap_iterations_a, 
1660                                 tree *overlap_iterations_b, 
1661                                 tree *last_conflicts)
1662 {
1663   if (dump_file && (dump_flags & TDF_DETAILS))
1664     {
1665       fprintf (dump_file, "(analyze_overlapping_iterations \n");
1666       fprintf (dump_file, "  (chrec_a = ");
1667       print_generic_expr (dump_file, chrec_a, 0);
1668       fprintf (dump_file, ")\n  chrec_b = ");
1669       print_generic_expr (dump_file, chrec_b, 0);
1670       fprintf (dump_file, ")\n");
1671     }
1672   
1673   if (chrec_a == NULL_TREE
1674       || chrec_b == NULL_TREE
1675       || chrec_contains_undetermined (chrec_a)
1676       || chrec_contains_undetermined (chrec_b)
1677       || chrec_contains_symbols (chrec_a)
1678       || chrec_contains_symbols (chrec_b))
1679     {
1680       *overlap_iterations_a = chrec_dont_know;
1681       *overlap_iterations_b = chrec_dont_know;
1682     }
1683   
1684   else if (ziv_subscript_p (chrec_a, chrec_b))
1685     analyze_ziv_subscript (chrec_a, chrec_b, 
1686                            overlap_iterations_a, overlap_iterations_b,
1687                            last_conflicts);
1688   
1689   else if (siv_subscript_p (chrec_a, chrec_b))
1690     analyze_siv_subscript (chrec_a, chrec_b, 
1691                            overlap_iterations_a, overlap_iterations_b, 
1692                            last_conflicts);
1693   
1694   else
1695     analyze_miv_subscript (chrec_a, chrec_b, 
1696                            overlap_iterations_a, overlap_iterations_b,
1697                            last_conflicts);
1698   
1699   if (dump_file && (dump_flags & TDF_DETAILS))
1700     {
1701       fprintf (dump_file, "  (overlap_iterations_a = ");
1702       print_generic_expr (dump_file, *overlap_iterations_a, 0);
1703       fprintf (dump_file, ")\n  (overlap_iterations_b = ");
1704       print_generic_expr (dump_file, *overlap_iterations_b, 0);
1705       fprintf (dump_file, ")\n");
1706     }
1707 }
1708
1709 \f
1710
1711 /* This section contains the affine functions dependences detector.  */
1712
1713 /* Computes the conflicting iterations, and initialize DDR.  */
1714
1715 static void
1716 subscript_dependence_tester (struct data_dependence_relation *ddr)
1717 {
1718   unsigned int i;
1719   struct data_reference *dra = DDR_A (ddr);
1720   struct data_reference *drb = DDR_B (ddr);
1721   tree last_conflicts;
1722   
1723   if (dump_file && (dump_flags & TDF_DETAILS))
1724     fprintf (dump_file, "(subscript_dependence_tester \n");
1725   
1726   for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1727     {
1728       tree overlaps_a, overlaps_b;
1729       struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
1730       
1731       analyze_overlapping_iterations (DR_ACCESS_FN (dra, i), 
1732                                       DR_ACCESS_FN (drb, i),
1733                                       &overlaps_a, &overlaps_b, 
1734                                       &last_conflicts);
1735       
1736       if (chrec_contains_undetermined (overlaps_a)
1737           || chrec_contains_undetermined (overlaps_b))
1738         {
1739           finalize_ddr_dependent (ddr, chrec_dont_know);
1740           break;
1741         }
1742       
1743       else if (overlaps_a == chrec_known
1744                || overlaps_b == chrec_known)
1745         {
1746           finalize_ddr_dependent (ddr, chrec_known);
1747           break;
1748         }
1749       
1750       else
1751         {
1752           SUB_CONFLICTS_IN_A (subscript) = overlaps_a;
1753           SUB_CONFLICTS_IN_B (subscript) = overlaps_b;
1754           SUB_LAST_CONFLICT (subscript) = last_conflicts;
1755         }
1756     }
1757   
1758   if (dump_file && (dump_flags & TDF_DETAILS))
1759     fprintf (dump_file, ")\n");
1760 }
1761
1762 /* Compute the classic per loop distance vector.
1763
1764    DDR is the data dependence relation to build a vector from.
1765    NB_LOOPS is the total number of loops we are considering.
1766    FIRST_LOOP_DEPTH is the loop->depth of the first loop in the analyzed
1767    loop nest.  
1768    Return FALSE if the dependence relation is outside of the loop nest
1769    starting at FIRST_LOOP_DEPTH. 
1770    Return TRUE otherwise.  */
1771
1772 bool
1773 build_classic_dist_vector (struct data_dependence_relation *ddr, 
1774                            int nb_loops, int first_loop_depth)
1775 {
1776   unsigned i;
1777   lambda_vector dist_v, init_v;
1778   
1779   dist_v = lambda_vector_new (nb_loops);
1780   init_v = lambda_vector_new (nb_loops);
1781   lambda_vector_clear (dist_v, nb_loops);
1782   lambda_vector_clear (init_v, nb_loops);
1783   
1784   if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
1785     return true;
1786   
1787   for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1788     {
1789       tree access_fn_a, access_fn_b;
1790       struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
1791
1792       if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
1793         {
1794           non_affine_dependence_relation (ddr);
1795           return true;
1796         }
1797
1798       access_fn_a = DR_ACCESS_FN (DDR_A (ddr), i);
1799       access_fn_b = DR_ACCESS_FN (DDR_B (ddr), i);
1800
1801       if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC 
1802           && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
1803         {
1804           int dist, loop_nb, loop_depth;
1805           int loop_nb_a = CHREC_VARIABLE (access_fn_a);
1806           int loop_nb_b = CHREC_VARIABLE (access_fn_b);
1807           struct loop *loop_a = current_loops->parray[loop_nb_a];
1808           struct loop *loop_b = current_loops->parray[loop_nb_b];
1809
1810           /* If the loop for either variable is at a lower depth than 
1811              the first_loop's depth, then we can't possibly have a
1812              dependency at this level of the loop.  */
1813              
1814           if (loop_a->depth < first_loop_depth
1815               || loop_b->depth < first_loop_depth)
1816             return false;
1817
1818           if (loop_nb_a != loop_nb_b
1819               && !flow_loop_nested_p (loop_a, loop_b)
1820               && !flow_loop_nested_p (loop_b, loop_a))
1821             {
1822               /* Example: when there are two consecutive loops,
1823
1824                  | loop_1
1825                  |   A[{0, +, 1}_1]
1826                  | endloop_1
1827                  | loop_2
1828                  |   A[{0, +, 1}_2]
1829                  | endloop_2
1830
1831                  the dependence relation cannot be captured by the
1832                  distance abstraction.  */
1833               non_affine_dependence_relation (ddr);
1834               return true;
1835             }
1836
1837           /* The dependence is carried by the outermost loop.  Example:
1838              | loop_1
1839              |   A[{4, +, 1}_1]
1840              |   loop_2
1841              |     A[{5, +, 1}_2]
1842              |   endloop_2
1843              | endloop_1
1844              In this case, the dependence is carried by loop_1.  */
1845           loop_nb = loop_nb_a < loop_nb_b ? loop_nb_a : loop_nb_b;
1846           loop_depth = current_loops->parray[loop_nb]->depth - first_loop_depth;
1847
1848           /* If the loop number is still greater than the number of
1849              loops we've been asked to analyze, or negative,
1850              something is borked.  */
1851           gcc_assert (loop_depth >= 0);
1852           gcc_assert (loop_depth < nb_loops);
1853           if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
1854             {
1855               non_affine_dependence_relation (ddr);
1856               return true;
1857             }
1858           
1859           dist = int_cst_value (SUB_DISTANCE (subscript));
1860
1861           /* This is the subscript coupling test.  
1862              | loop i = 0, N, 1
1863              |   T[i+1][i] = ...
1864              |   ... = T[i][i]
1865              | endloop
1866              There is no dependence.  */
1867           if (init_v[loop_depth] != 0
1868               && dist_v[loop_depth] != dist)
1869             {
1870               finalize_ddr_dependent (ddr, chrec_known);
1871               return true;
1872             }
1873
1874           dist_v[loop_depth] = dist;
1875           init_v[loop_depth] = 1;
1876         }
1877     }
1878   
1879   /* There is a distance of 1 on all the outer loops: 
1880      
1881      Example: there is a dependence of distance 1 on loop_1 for the array A.
1882      | loop_1
1883      |   A[5] = ...
1884      | endloop
1885   */
1886   {
1887     struct loop *lca, *loop_a, *loop_b;
1888     struct data_reference *a = DDR_A (ddr);
1889     struct data_reference *b = DDR_B (ddr);
1890     int lca_depth;
1891     loop_a = loop_containing_stmt (DR_STMT (a));
1892     loop_b = loop_containing_stmt (DR_STMT (b));
1893     
1894     /* Get the common ancestor loop.  */
1895     lca = find_common_loop (loop_a, loop_b); 
1896     
1897     lca_depth = lca->depth;
1898     lca_depth -= first_loop_depth;
1899     gcc_assert (lca_depth >= 0);
1900     gcc_assert (lca_depth < nb_loops);
1901
1902     /* For each outer loop where init_v is not set, the accesses are
1903        in dependence of distance 1 in the loop.  */
1904     if (lca != loop_a
1905         && lca != loop_b
1906         && init_v[lca_depth] == 0)
1907       dist_v[lca_depth] = 1;
1908     
1909     lca = lca->outer;
1910     
1911     if (lca)
1912       {
1913         lca_depth = lca->depth - first_loop_depth;
1914         while (lca->depth != 0)
1915           {
1916             /* If we're considering just a sub-nest, then don't record
1917                any information on the outer loops.  */
1918             if (lca_depth < 0)
1919               break;
1920
1921             gcc_assert (lca_depth < nb_loops);
1922
1923             if (init_v[lca_depth] == 0)
1924               dist_v[lca_depth] = 1;
1925             lca = lca->outer;
1926             lca_depth = lca->depth - first_loop_depth;
1927           
1928           }
1929       }
1930   }
1931   
1932   DDR_DIST_VECT (ddr) = dist_v;
1933   DDR_SIZE_VECT (ddr) = nb_loops;
1934   return true;
1935 }
1936
1937 /* Compute the classic per loop direction vector.  
1938
1939    DDR is the data dependence relation to build a vector from.
1940    NB_LOOPS is the total number of loops we are considering.
1941    FIRST_LOOP_DEPTH is the loop->depth of the first loop in the analyzed 
1942    loop nest.
1943    Return FALSE if the dependence relation is outside of the loop nest
1944    at FIRST_LOOP_DEPTH. 
1945    Return TRUE otherwise.  */
1946
1947 static bool
1948 build_classic_dir_vector (struct data_dependence_relation *ddr, 
1949                           int nb_loops, int first_loop_depth)
1950 {
1951   unsigned i;
1952   lambda_vector dir_v, init_v;
1953   
1954   dir_v = lambda_vector_new (nb_loops);
1955   init_v = lambda_vector_new (nb_loops);
1956   lambda_vector_clear (dir_v, nb_loops);
1957   lambda_vector_clear (init_v, nb_loops);
1958   
1959   if (DDR_ARE_DEPENDENT (ddr) != NULL_TREE)
1960     return true;
1961   
1962   for (i = 0; i < DDR_NUM_SUBSCRIPTS (ddr); i++)
1963     {
1964       tree access_fn_a, access_fn_b;
1965       struct subscript *subscript = DDR_SUBSCRIPT (ddr, i);
1966
1967       if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
1968         {
1969           non_affine_dependence_relation (ddr);
1970           return true;
1971         }
1972
1973       access_fn_a = DR_ACCESS_FN (DDR_A (ddr), i);
1974       access_fn_b = DR_ACCESS_FN (DDR_B (ddr), i);
1975       if (TREE_CODE (access_fn_a) == POLYNOMIAL_CHREC
1976           && TREE_CODE (access_fn_b) == POLYNOMIAL_CHREC)
1977         {
1978           int dist, loop_nb, loop_depth;
1979           enum data_dependence_direction dir = dir_star;
1980           int loop_nb_a = CHREC_VARIABLE (access_fn_a);
1981           int loop_nb_b = CHREC_VARIABLE (access_fn_b);
1982           struct loop *loop_a = current_loops->parray[loop_nb_a];
1983           struct loop *loop_b = current_loops->parray[loop_nb_b];
1984  
1985           /* If the loop for either variable is at a lower depth than 
1986              the first_loop's depth, then we can't possibly have a
1987              dependency at this level of the loop.  */
1988              
1989           if (loop_a->depth < first_loop_depth
1990               || loop_b->depth < first_loop_depth)
1991             return false;
1992
1993           if (loop_nb_a != loop_nb_b
1994               && !flow_loop_nested_p (loop_a, loop_b)
1995               && !flow_loop_nested_p (loop_b, loop_a))
1996             {
1997               /* Example: when there are two consecutive loops,
1998
1999                  | loop_1
2000                  |   A[{0, +, 1}_1]
2001                  | endloop_1
2002                  | loop_2
2003                  |   A[{0, +, 1}_2]
2004                  | endloop_2
2005
2006                  the dependence relation cannot be captured by the
2007                  distance abstraction.  */
2008               non_affine_dependence_relation (ddr);
2009               return true;
2010             }
2011
2012           /* The dependence is carried by the outermost loop.  Example:
2013              | loop_1
2014              |   A[{4, +, 1}_1]
2015              |   loop_2
2016              |     A[{5, +, 1}_2]
2017              |   endloop_2
2018              | endloop_1
2019              In this case, the dependence is carried by loop_1.  */
2020           loop_nb = loop_nb_a < loop_nb_b ? loop_nb_a : loop_nb_b;
2021           loop_depth = current_loops->parray[loop_nb]->depth - first_loop_depth;
2022
2023           /* If the loop number is still greater than the number of
2024              loops we've been asked to analyze, or negative,
2025              something is borked.  */
2026           gcc_assert (loop_depth >= 0);
2027           gcc_assert (loop_depth < nb_loops);
2028
2029           if (chrec_contains_undetermined (SUB_DISTANCE (subscript)))
2030             {
2031               non_affine_dependence_relation (ddr);
2032               return true;
2033             }
2034
2035           dist = int_cst_value (SUB_DISTANCE (subscript));
2036
2037           if (dist == 0)
2038             dir = dir_equal;
2039           else if (dist > 0)
2040             dir = dir_positive;
2041           else if (dist < 0)
2042             dir = dir_negative;
2043           
2044           /* This is the subscript coupling test.  
2045              | loop i = 0, N, 1
2046              |   T[i+1][i] = ...
2047              |   ... = T[i][i]
2048              | endloop
2049              There is no dependence.  */
2050           if (init_v[loop_depth] != 0
2051               && dir != dir_star
2052               && (enum data_dependence_direction) dir_v[loop_depth] != dir
2053               && (enum data_dependence_direction) dir_v[loop_depth] != dir_star)
2054             {
2055               finalize_ddr_dependent (ddr, chrec_known);
2056               return true;
2057             }
2058           
2059           dir_v[loop_depth] = dir;
2060           init_v[loop_depth] = 1;
2061         }
2062     }
2063   
2064   /* There is a distance of 1 on all the outer loops: 
2065      
2066      Example: there is a dependence of distance 1 on loop_1 for the array A.
2067      | loop_1
2068      |   A[5] = ...
2069      | endloop
2070   */
2071   {
2072     struct loop *lca, *loop_a, *loop_b;
2073     struct data_reference *a = DDR_A (ddr);
2074     struct data_reference *b = DDR_B (ddr);
2075     int lca_depth;
2076     loop_a = loop_containing_stmt (DR_STMT (a));
2077     loop_b = loop_containing_stmt (DR_STMT (b));
2078     
2079     /* Get the common ancestor loop.  */
2080     lca = find_common_loop (loop_a, loop_b); 
2081     lca_depth = lca->depth - first_loop_depth;
2082
2083     gcc_assert (lca_depth >= 0);
2084     gcc_assert (lca_depth < nb_loops);
2085
2086     /* For each outer loop where init_v is not set, the accesses are
2087        in dependence of distance 1 in the loop.  */
2088     if (lca != loop_a
2089         && lca != loop_b
2090         && init_v[lca_depth] == 0)
2091       dir_v[lca_depth] = dir_positive;
2092     
2093     lca = lca->outer;
2094     if (lca)
2095       {
2096         lca_depth = lca->depth - first_loop_depth;
2097         while (lca->depth != 0)
2098           {
2099             /* If we're considering just a sub-nest, then don't record
2100                any information on the outer loops.  */
2101             if (lca_depth < 0)
2102               break;
2103
2104             gcc_assert (lca_depth < nb_loops);
2105
2106             if (init_v[lca_depth] == 0)
2107               dir_v[lca_depth] = dir_positive;
2108             lca = lca->outer;
2109             lca_depth = lca->depth - first_loop_depth;
2110            
2111           }
2112       }
2113   }
2114   
2115   DDR_DIR_VECT (ddr) = dir_v;
2116   DDR_SIZE_VECT (ddr) = nb_loops;
2117   return true;
2118 }
2119
2120 /* Returns true when all the access functions of A are affine or
2121    constant.  */
2122
2123 static bool 
2124 access_functions_are_affine_or_constant_p (struct data_reference *a)
2125 {
2126   unsigned int i;
2127   VEC(tree,heap) **fns = &DR_ACCESS_FNS (a);
2128   tree t;
2129   
2130   for (i = 0; VEC_iterate (tree, *fns, i, t); i++)
2131     if (!evolution_function_is_constant_p (t)
2132         && !evolution_function_is_affine_multivariate_p (t))
2133       return false;
2134   
2135   return true;
2136 }
2137
2138 /* This computes the affine dependence relation between A and B.
2139    CHREC_KNOWN is used for representing the independence between two
2140    accesses, while CHREC_DONT_KNOW is used for representing the unknown
2141    relation.
2142    
2143    Note that it is possible to stop the computation of the dependence
2144    relation the first time we detect a CHREC_KNOWN element for a given
2145    subscript.  */
2146
2147 void
2148 compute_affine_dependence (struct data_dependence_relation *ddr)
2149 {
2150   struct data_reference *dra = DDR_A (ddr);
2151   struct data_reference *drb = DDR_B (ddr);
2152   
2153   if (dump_file && (dump_flags & TDF_DETAILS))
2154     {
2155       fprintf (dump_file, "(compute_affine_dependence\n");
2156       fprintf (dump_file, "  (stmt_a = \n");
2157       print_generic_expr (dump_file, DR_STMT (dra), 0);
2158       fprintf (dump_file, ")\n  (stmt_b = \n");
2159       print_generic_expr (dump_file, DR_STMT (drb), 0);
2160       fprintf (dump_file, ")\n");
2161     }
2162   
2163   /* Analyze only when the dependence relation is not yet known.  */
2164   if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE)
2165     {
2166       if (access_functions_are_affine_or_constant_p (dra)
2167           && access_functions_are_affine_or_constant_p (drb))
2168         subscript_dependence_tester (ddr);
2169       
2170       /* As a last case, if the dependence cannot be determined, or if
2171          the dependence is considered too difficult to determine, answer
2172          "don't know".  */
2173       else
2174         finalize_ddr_dependent (ddr, chrec_dont_know);
2175     }
2176   
2177   if (dump_file && (dump_flags & TDF_DETAILS))
2178     fprintf (dump_file, ")\n");
2179 }
2180
2181
2182 typedef struct data_dependence_relation *ddr_p;
2183 DEF_VEC_P(ddr_p);
2184 DEF_VEC_ALLOC_P(ddr_p,heap);
2185
2186 /* Compute a subset of the data dependence relation graph.  Don't
2187    compute read-read relations, and avoid the computation of the
2188    opposite relation, i.e. when AB has been computed, don't compute BA.
2189    DATAREFS contains a list of data references, and the result is set
2190    in DEPENDENCE_RELATIONS.  */
2191
2192 static void 
2193 compute_all_dependences (varray_type datarefs, 
2194                          VEC(ddr_p,heap) **dependence_relations)
2195 {
2196   unsigned int i, j, N;
2197
2198   N = VARRAY_ACTIVE_SIZE (datarefs);
2199
2200   for (i = 0; i < N; i++)
2201     for (j = i; j < N; j++)
2202       {
2203         struct data_reference *a, *b;
2204         struct data_dependence_relation *ddr;
2205
2206         a = VARRAY_GENERIC_PTR (datarefs, i);
2207         b = VARRAY_GENERIC_PTR (datarefs, j);
2208         ddr = initialize_data_dependence_relation (a, b);
2209
2210         VEC_safe_push (ddr_p, heap, *dependence_relations, ddr);
2211         compute_affine_dependence (ddr);
2212         compute_subscript_distance (ddr);
2213       }
2214 }
2215
2216 /* Search the data references in LOOP, and record the information into
2217    DATAREFS.  Returns chrec_dont_know when failing to analyze a
2218    difficult case, returns NULL_TREE otherwise.
2219    
2220    TODO: This function should be made smarter so that it can handle address
2221    arithmetic as if they were array accesses, etc.  */
2222
2223 tree 
2224 find_data_references_in_loop (struct loop *loop, varray_type *datarefs)
2225 {
2226   basic_block bb, *bbs;
2227   unsigned int i;
2228   block_stmt_iterator bsi;
2229
2230   bbs = get_loop_body (loop);
2231
2232   for (i = 0; i < loop->num_nodes; i++)
2233     {
2234       bb = bbs[i];
2235
2236       for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2237         {
2238           tree stmt = bsi_stmt (bsi);
2239
2240           /* ASM_EXPR and CALL_EXPR may embed arbitrary side effects.
2241              Calls have side-effects, except those to const or pure
2242              functions.  */
2243           if ((TREE_CODE (stmt) == CALL_EXPR
2244                && !(call_expr_flags (stmt) & (ECF_CONST | ECF_PURE)))
2245               || (TREE_CODE (stmt) == ASM_EXPR
2246                   && ASM_VOLATILE_P (stmt)))
2247             goto insert_dont_know_node;
2248
2249           if (ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
2250             continue;
2251
2252           switch (TREE_CODE (stmt))
2253             {
2254             case MODIFY_EXPR:
2255               if (TREE_CODE (TREE_OPERAND (stmt, 0)) == ARRAY_REF)
2256                 VARRAY_PUSH_GENERIC_PTR 
2257                   (*datarefs, analyze_array (stmt, TREE_OPERAND (stmt, 0),
2258                                              false));
2259
2260               if (TREE_CODE (TREE_OPERAND (stmt, 1)) == ARRAY_REF)
2261                 VARRAY_PUSH_GENERIC_PTR 
2262                   (*datarefs, analyze_array (stmt, TREE_OPERAND (stmt, 1),
2263                                              true));
2264
2265               if (TREE_CODE (TREE_OPERAND (stmt, 0)) != ARRAY_REF
2266                   && TREE_CODE (TREE_OPERAND (stmt, 1)) != ARRAY_REF)
2267                 goto insert_dont_know_node;
2268
2269               break;
2270
2271             case CALL_EXPR:
2272               {
2273                 tree args;
2274                 bool one_inserted = false;
2275
2276                 for (args = TREE_OPERAND (stmt, 1); args; args = TREE_CHAIN (args))
2277                   if (TREE_CODE (TREE_VALUE (args)) == ARRAY_REF)
2278                     {
2279                       VARRAY_PUSH_GENERIC_PTR 
2280                         (*datarefs, analyze_array (stmt, TREE_VALUE (args), true));
2281                       one_inserted = true;
2282                     }
2283
2284                 if (!one_inserted)
2285                   goto insert_dont_know_node;
2286
2287                 break;
2288               }
2289
2290             default:
2291                 {
2292                   struct data_reference *res;
2293
2294                 insert_dont_know_node:;
2295                   res = xmalloc (sizeof (struct data_reference));
2296                   DR_STMT (res) = NULL_TREE;
2297                   DR_REF (res) = NULL_TREE;
2298                   DR_ACCESS_FNS (res) = NULL;
2299                   DR_BASE_NAME (res) = NULL;
2300                   DR_IS_READ (res) = false;
2301                   VARRAY_PUSH_GENERIC_PTR (*datarefs, res);
2302
2303                   free (bbs);
2304                   return chrec_dont_know;
2305                 }
2306             }
2307
2308           /* When there are no defs in the loop, the loop is parallel.  */
2309           if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_DEFS))
2310             bb->loop_father->parallel_p = false;
2311         }
2312
2313       if (bb->loop_father->estimated_nb_iterations == NULL_TREE)
2314         compute_estimated_nb_iterations (bb->loop_father);
2315     }
2316
2317   free (bbs);
2318
2319   return NULL_TREE;
2320 }
2321
2322 \f
2323
2324 /* This section contains all the entry points.  */
2325
2326 /* Given a loop nest LOOP, the following vectors are returned:
2327    *DATAREFS is initialized to all the array elements contained in this loop, 
2328    *DEPENDENCE_RELATIONS contains the relations between the data references.  */
2329
2330 void
2331 compute_data_dependences_for_loop (unsigned nb_loops, 
2332                                    struct loop *loop,
2333                                    varray_type *datarefs,
2334                                    varray_type *dependence_relations)
2335 {
2336   unsigned int i;
2337   VEC(ddr_p,heap) *allrelations;
2338   struct data_dependence_relation *ddr;
2339
2340   /* If one of the data references is not computable, give up without
2341      spending time to compute other dependences.  */
2342   if (find_data_references_in_loop (loop, datarefs) == chrec_dont_know)
2343     {
2344       struct data_dependence_relation *ddr;
2345
2346       /* Insert a single relation into dependence_relations:
2347          chrec_dont_know.  */
2348       ddr = initialize_data_dependence_relation (NULL, NULL);
2349       VARRAY_PUSH_GENERIC_PTR (*dependence_relations, ddr);
2350       build_classic_dist_vector (ddr, nb_loops, loop->depth);
2351       build_classic_dir_vector (ddr, nb_loops, loop->depth);
2352       return;
2353     }
2354
2355   allrelations = NULL;
2356   compute_all_dependences (*datarefs, &allrelations);
2357
2358   for (i = 0; VEC_iterate (ddr_p, allrelations, i, ddr); i++)
2359     {
2360       if (build_classic_dist_vector (ddr, nb_loops, loop->depth))
2361         {
2362           VARRAY_PUSH_GENERIC_PTR (*dependence_relations, ddr);
2363           build_classic_dir_vector (ddr, nb_loops, loop->depth);
2364         }
2365     }
2366 }
2367
2368 /* Entry point (for testing only).  Analyze all the data references
2369    and the dependence relations.
2370
2371    The data references are computed first.  
2372    
2373    A relation on these nodes is represented by a complete graph.  Some
2374    of the relations could be of no interest, thus the relations can be
2375    computed on demand.
2376    
2377    In the following function we compute all the relations.  This is
2378    just a first implementation that is here for:
2379    - for showing how to ask for the dependence relations, 
2380    - for the debugging the whole dependence graph,
2381    - for the dejagnu testcases and maintenance.
2382    
2383    It is possible to ask only for a part of the graph, avoiding to
2384    compute the whole dependence graph.  The computed dependences are
2385    stored in a knowledge base (KB) such that later queries don't
2386    recompute the same information.  The implementation of this KB is
2387    transparent to the optimizer, and thus the KB can be changed with a
2388    more efficient implementation, or the KB could be disabled.  */
2389
2390 void 
2391 analyze_all_data_dependences (struct loops *loops)
2392 {
2393   unsigned int i;
2394   varray_type datarefs;
2395   varray_type dependence_relations;
2396   int nb_data_refs = 10;
2397
2398   VARRAY_GENERIC_PTR_INIT (datarefs, nb_data_refs, "datarefs");
2399   VARRAY_GENERIC_PTR_INIT (dependence_relations, 
2400                            nb_data_refs * nb_data_refs,
2401                            "dependence_relations");
2402
2403   /* Compute DDs on the whole function.  */
2404   compute_data_dependences_for_loop (loops->num, loops->parray[0], 
2405                                      &datarefs, &dependence_relations);
2406
2407   if (dump_file)
2408     {
2409       dump_data_dependence_relations (dump_file, dependence_relations);
2410       fprintf (dump_file, "\n\n");
2411
2412       if (dump_flags & TDF_DETAILS)
2413         dump_dist_dir_vectors (dump_file, dependence_relations);
2414
2415       if (dump_flags & TDF_STATS)
2416         {
2417           unsigned nb_top_relations = 0;
2418           unsigned nb_bot_relations = 0;
2419           unsigned nb_basename_differ = 0;
2420           unsigned nb_chrec_relations = 0;
2421
2422           for (i = 0; i < VARRAY_ACTIVE_SIZE (dependence_relations); i++)
2423             {
2424               struct data_dependence_relation *ddr;
2425               ddr = VARRAY_GENERIC_PTR (dependence_relations, i);
2426           
2427               if (chrec_contains_undetermined (DDR_ARE_DEPENDENT (ddr)))
2428                 nb_top_relations++;
2429           
2430               else if (DDR_ARE_DEPENDENT (ddr) == chrec_known)
2431                 {
2432                   struct data_reference *a = DDR_A (ddr);
2433                   struct data_reference *b = DDR_B (ddr);
2434                   bool differ_p;        
2435               
2436                   if (DR_NUM_DIMENSIONS (a) != DR_NUM_DIMENSIONS (b)
2437                       || (array_base_name_differ_p (a, b, &differ_p) && differ_p))
2438                     nb_basename_differ++;
2439                   else
2440                     nb_bot_relations++;
2441                 }
2442           
2443               else 
2444                 nb_chrec_relations++;
2445             }
2446       
2447           gather_stats_on_scev_database ();
2448         }
2449     }
2450
2451   free_dependence_relations (dependence_relations);
2452   free_data_refs (datarefs);
2453 }
2454
2455 /* Free the memory used by a data dependence relation DDR.  */
2456
2457 void
2458 free_dependence_relation (struct data_dependence_relation *ddr)
2459 {
2460   if (ddr == NULL)
2461     return;
2462
2463   if (DDR_ARE_DEPENDENT (ddr) == NULL_TREE && DDR_SUBSCRIPTS (ddr))
2464     varray_clear (DDR_SUBSCRIPTS (ddr));
2465   free (ddr);
2466 }
2467
2468 /* Free the memory used by the data dependence relations from
2469    DEPENDENCE_RELATIONS.  */
2470
2471 void 
2472 free_dependence_relations (varray_type dependence_relations)
2473 {
2474   unsigned int i;
2475   if (dependence_relations == NULL)
2476     return;
2477
2478   for (i = 0; i < VARRAY_ACTIVE_SIZE (dependence_relations); i++)
2479     free_dependence_relation (VARRAY_GENERIC_PTR (dependence_relations, i));
2480   varray_clear (dependence_relations);
2481 }
2482
2483 /* Free the memory used by the data references from DATAREFS.  */
2484
2485 void
2486 free_data_refs (varray_type datarefs)
2487 {
2488   unsigned int i;
2489   
2490   if (datarefs == NULL)
2491     return;
2492
2493   for (i = 0; i < VARRAY_ACTIVE_SIZE (datarefs); i++)
2494     {
2495       struct data_reference *dr = (struct data_reference *) 
2496         VARRAY_GENERIC_PTR (datarefs, i);
2497       if (dr)
2498         {
2499           VEC_free (tree, heap, DR_ACCESS_FNS (dr));
2500           free (dr);
2501         }
2502     }
2503   varray_clear (datarefs);
2504 }
2505