OSDN Git Service

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