OSDN Git Service

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