1 /* Analyze loop dependencies
2 Copyright (C) 2000 Free Software Foundation, Inc.
4 This file is part of GNU CC.
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
22 Practical Dependence Testing, Goff, Kennedy, Tseng, PLDI, 1991
23 High Performance Compilers for Parallel Computing, Wolfe
35 #define MAX_SUBSCRIPTS 13
38 We perform the following steps:
40 Build the data structures def_use_chain, loop_chain, and induction_chain.
42 Determine if a loop index is a normalized induction variable.
43 A loop is currently considered to be a for loop having an index set to an
44 initial value, conditional check of the index, and increment/decrement of
47 Determine the distance and direction vectors. Both are two dimensioned
48 arrays where the first dimension represents a loop and the second
49 dimension represents a subscript. Dependencies are actually per loop, not
50 per subscript. So for:
51 for (i = 0; i < 10; i++)
52 for (j = 0; j < 10; j++)
53 array [i][j] = array[i][j-1]
54 We find the dependencies: loop1/sub_i, loop1/sub_j, loop2/sub_i, loop2/sub_j
55 and then intersect loop1/sub_i V loop2/sub_i and loop1/sub_i V loop2/sub_j
56 We determine the type of dependence, which determines which test we use.
57 We then try to refine the type of dependence we have and add the
58 dependence to the dep_chain
61 enum dependence_type
{flow
, anti
, output
, none
};
62 static const char * dependence_string
[] = {"flow", "anti", "output", "none"};
64 enum direction_type
{lt
, le
, eq
, gt
, ge
, star
, independent
, undef
};
65 static const char * direction_string
[] = {"<", "<=", "=", ">", ">=", "*",
66 "INDEPENDENT", "UNDEFINED"};
68 enum def_use_type
{def
, use
, init_def_use
};
70 enum du_status_type
{seen
, unseen
};
72 enum loop_status_type
{normal
, unnormal
};
74 enum complexity_type
{ziv
, strong_siv
, weak_siv
, weak_zero_siv
,
75 weak_crossing_siv
, miv
};
77 /* Given a def/use one can chase the next chain to follow the def/use
78 for that variable. Alternately one can sequentially follow each
79 element of def_use_chain. */
81 typedef struct def_use
85 /* loop containing this def/use */
92 enum def_use_type type
;
94 enum du_status_type status
;
95 /* next def/use for this same name */
97 /* dependencies for this def */
98 struct dependence
*dep
;
101 /* Given a loop* one can chase the next_nest chain to follow the nested
102 loops for that loop. Alternately one can sequentially follow each
103 element of loop_chain and check outer_loop to get all loops
104 contained within a certain loop. */
108 /* outermost loop containing this loop */
111 tree containing_loop
;
112 /* nest level for this loop */
114 /* can loop be normalized? */
115 enum loop_status_type status
;
116 /* loop* for loop contained in this loop */
117 struct loop
*next_nest
;
118 /* induction variables for this loop. Currently only the index variable. */
119 struct induction
*ind
;
122 /* Pointed to by loop. One per induction variable. */
124 typedef struct induction
128 /* increment. Currently only +1 or -1 */
134 /* next induction variable for this loop. Currently null. */
135 struct induction
*next
;
138 /* Pointed to by def/use. One per dependence. */
140 typedef struct dependence
144 enum dependence_type dependence
;
145 enum direction_type direction
[MAX_SUBSCRIPTS
];
146 int distance
[MAX_SUBSCRIPTS
];
147 struct dependence
*next
;
150 /* subscripts are represented by an array of these. Each reflects one
151 X * i + Y term, where X and Y are constants. */
153 typedef struct subscript
155 /* ordinal subscript number */
163 /* next subscript term. Currently null. */
164 struct subscript
*next
;
167 /* Remember the destination the front end encountered. */
169 static tree dest_to_remember
;
171 /* Chain for def_use */
172 static varray_type def_use_chain
;
174 /* Chain for dependence */
175 static varray_type dep_chain
;
178 static varray_type loop_chain
;
180 /* Chain for induction */
181 static varray_type induction_chain
;
183 void init_dependence_analysis
PARAMS ((tree
));
184 static void build_def_use
PARAMS ((tree
, enum def_use_type
));
185 static loop
* add_loop
PARAMS ((tree
, tree
, int));
186 static int find_induction_variable
PARAMS ((tree
, tree
, tree
, loop
*));
187 static int get_low_bound
PARAMS ((tree
, char*));
188 static int have_induction_variable
PARAMS ((tree
, char*));
189 static void link_loops
PARAMS ((void));
190 static void get_node_dependence
PARAMS ((void));
191 static void check_node_dependence
PARAMS ((def_use
*));
192 static int get_coefficients
PARAMS ((def_use
*, subscript
[]));
193 static int get_one_coefficient
PARAMS ((tree
, subscript
*, def_use
*, enum tree_code
*));
194 static void normalize_coefficients
PARAMS ((subscript
[], loop
*, int));
195 static void classify_dependence
PARAMS ((subscript
[], subscript
[],
196 enum complexity_type
[], int*, int));
197 static void ziv_test
PARAMS ((subscript
[], subscript
[],
198 enum direction_type
[][MAX_SUBSCRIPTS
],
199 int[][MAX_SUBSCRIPTS
], loop
*, int));
200 static void siv_test
PARAMS ((subscript
[], subscript
[],
201 enum direction_type
[][MAX_SUBSCRIPTS
],
202 int[][MAX_SUBSCRIPTS
], loop
*, int));
203 static int check_subscript_induction
PARAMS ((subscript
*, subscript
*, loop
*));
204 static void gcd_test
PARAMS ((subscript
[], subscript
[], enum
205 direction_type
[][MAX_SUBSCRIPTS
],
206 int[][MAX_SUBSCRIPTS
], loop
*, int));
207 static int find_gcd
PARAMS ((int, int));
208 static void merge_dependencies
PARAMS ((enum direction_type
[][MAX_SUBSCRIPTS
],
209 int[][MAX_SUBSCRIPTS
], int, int));
210 static void dump_array_ref
PARAMS ((tree
));
211 static void dump_one_node
PARAMS ((def_use
*, varray_type
*));
212 static void dump_node_dependence
PARAMS ((void));
213 int search_dependence
PARAMS ((tree
));
214 void remember_dest_for_dependence
PARAMS ((tree
));
215 int have_dependence_p
PARAMS ((rtx
, rtx
, enum direction_type
[], int[]));
216 void end_dependence_analysis
PARAMS ((void));
218 /* Build dependence chain 'dep_chain', which is used by have_dependence_p,
219 for the function given by EXP. */
222 init_dependence_analysis (exp
)
227 VARRAY_GENERIC_PTR_INIT (def_use_chain
, 50, "def_use_chain");
228 VARRAY_GENERIC_PTR_INIT (dep_chain
, 50, "dep_chain");
229 VARRAY_GENERIC_PTR_INIT (loop_chain
, 50, "loop_chain");
230 VARRAY_GENERIC_PTR_INIT (induction_chain
, 50, "induction_chain");
232 build_def_use (exp
, init_def_use
);
236 get_node_dependence ();
238 /* dump_node_dependence (&def_use_chain);*/
240 for (du_ptr
= VARRAY_TOP (def_use_chain
, generic
);
241 VARRAY_POP (def_use_chain
);
242 du_ptr
= VARRAY_TOP (def_use_chain
, generic
))
247 VARRAY_FREE (def_use_chain
);
248 VARRAY_FREE (loop_chain
);
249 VARRAY_FREE (induction_chain
);
252 /* Build ARRAY_REF def/use info 'def_use_chain' starting at EXP which is a def
256 build_def_use (exp
, du_type
)
258 enum def_use_type du_type
;
260 static tree outer_loop
;
262 static tree current_loop
;
264 static loop
*loop_def
;
270 if (du_type
== init_def_use
)
278 switch (TREE_CODE (node
))
281 node
= TREE_OPERAND (node
, 0);
284 build_def_use (TREE_VALUE (node
), 0);
285 node
= TREE_CHAIN (node
);
288 node
= TREE_CHAIN (node
);
291 if (! nloop
) outer_loop
= node
;
294 loop_def
= add_loop (node
, outer_loop
, nloop
);
295 if (find_induction_variable (TREE_OPERAND (node
, 0),
296 TREE_OPERAND (node
, 1),
297 TREE_OPERAND (node
, 2), loop_def
)
299 loop_def
->status
= unnormal
;
301 build_def_use (TREE_OPERAND (node
, 3), 0);
304 node
= TREE_CHAIN (node
);
307 /* Is an induction variable modified? */
309 && TREE_CODE (TREE_OPERAND (node
, 0)) == VAR_DECL
310 && have_induction_variable
311 (loop_def
->outer_loop
,
312 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (node
, 0)))) >= 0)
313 loop_def
->status
= unnormal
;
315 if (TREE_CODE (TREE_OPERAND (node
, 0)) == ARRAY_REF
316 || TREE_CODE (TREE_OPERAND (node
, 0)) == INDIRECT_REF
)
317 build_def_use (TREE_OPERAND (node
, 0), def
);
319 build_def_use (TREE_OPERAND (node
, 1), use
);
320 node
= TREE_CHAIN (node
);
323 if (! TREE_OPERAND (node
, 1)
324 || TREE_CODE (TREE_OPERAND (node
, 1)) != ARRAY_REF
)
329 node
= TREE_OPERAND (node
, 1);
334 char null_string
= '\0';
336 VARRAY_PUSH_GENERIC_PTR (def_use_chain
, xmalloc (sizeof (def_use
)));
337 du_ptr
= VARRAY_GENERIC_PTR (def_use_chain
, du_idx
++);
338 du_ptr
->type
= du_type
;
339 du_ptr
->status
= unseen
;
340 du_ptr
->outer_loop
= outer_loop
;
341 du_ptr
->containing_loop
= current_loop
;
342 du_ptr
->expression
= node
;
343 du_ptr
->variable
= &null_string
;
346 for (array_ref
= node
;
347 TREE_CODE (array_ref
) == ARRAY_REF
;
348 array_ref
= TREE_OPERAND (array_ref
, 0))
351 if (TREE_CODE (array_ref
) == COMPONENT_REF
)
353 array_ref
= TREE_OPERAND (array_ref
, 1);
354 if (! (TREE_CODE (array_ref
) == FIELD_DECL
355 && TREE_CODE (TREE_TYPE (array_ref
)) == ARRAY_TYPE
))
366 && strcmp (IDENTIFIER_POINTER (DECL_NAME (array_ref
)),
367 ((def_use
*) (VARRAY_GENERIC_PTR
368 (def_use_chain
, i
)))->variable
);
374 for (tmp_duc
= ((def_use
*) (VARRAY_GENERIC_PTR (def_use_chain
, i
)));
376 tmp_duc
= ((def_use
*)tmp_duc
->next
));
377 tmp_duc
->next
= du_ptr
;
379 else du_ptr
->next
= 0;
380 du_ptr
->variable
= IDENTIFIER_POINTER (DECL_NAME (array_ref
));
387 node
= TREE_CHAIN (node
);
391 if (TREE_CODE (TREE_OPERAND (node
, 0)) == MODIFY_EXPR
)
392 build_def_use (TREE_OPERAND (node
, 0), def
);
393 node
= TREE_CHAIN (node
);
397 if (TREE_CODE_CLASS (TREE_CODE (node
)) == '2')
399 build_def_use (TREE_OPERAND (node
, 0), use
);
400 build_def_use (TREE_OPERAND (node
, 1), use
);
401 node
= TREE_CHAIN (node
);
408 /* Add a loop to 'loop_chain' corresponding to for loop LOOP_NODE at depth
409 NLOOP, whose outermost loop is OUTER_LOOP */
412 add_loop (loop_node
, outer_loop
, nloop
)
419 VARRAY_PUSH_GENERIC_PTR (loop_chain
, xmalloc (sizeof (loop
)));
420 loop_ptr
= VARRAY_TOP (loop_chain
, generic
);
421 loop_ptr
->outer_loop
= outer_loop
;
422 loop_ptr
->containing_loop
= loop_node
;
423 loop_ptr
->depth
= nloop
;
424 loop_ptr
->status
= normal
;
425 loop_ptr
->next_nest
= 0;
430 /* Update LOOP_DEF if for loop's COND_NODE and INCR_NODE define an index that
431 is a normalized induction variable. */
434 find_induction_variable (init_node
, cond_node
, incr_node
, loop_def
)
441 enum tree_code incr_code
;
444 if (! init_node
|| ! incr_node
|| ! cond_node
)
446 /* Allow for ',' operator in increment expression of FOR */
449 while (TREE_CODE (incr
) == COMPOUND_EXPR
)
451 incr_code
= TREE_CODE (TREE_OPERAND (incr
, 0));
452 if (incr_code
== PREDECREMENT_EXPR
|| incr_code
== POSTDECREMENT_EXPR
453 || incr_code
== PREINCREMENT_EXPR
|| incr_code
== POSTINCREMENT_EXPR
)
455 incr_node
= TREE_OPERAND (incr
, 0);
458 incr_code
= TREE_CODE (TREE_OPERAND (incr
, 1));
459 if (incr_code
== PREDECREMENT_EXPR
|| incr_code
== POSTDECREMENT_EXPR
460 || incr_code
== PREINCREMENT_EXPR
|| incr_code
== POSTINCREMENT_EXPR
)
462 incr_node
= TREE_OPERAND (incr
, 1);
465 incr
= TREE_OPERAND (incr
, 1);
468 /* Allow index condition to be part of logical expression */
469 cond_node
= TREE_VALUE (cond_node
);
472 #define INDEX_LIMIT_CHECK(node) \
473 (TREE_CODE_CLASS (TREE_CODE (node)) == '<') \
474 && (TREE_CODE (TREE_OPERAND (node, 0)) == VAR_DECL \
475 && (IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (node, 0))) \
476 == IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (incr_node, 0))))) \
479 while (TREE_CODE (incr
) == TRUTH_ANDIF_EXPR
480 || TREE_CODE (incr
) == TRUTH_ORIF_EXPR
)
482 if (INDEX_LIMIT_CHECK (TREE_OPERAND (incr
, 0)))
484 cond_node
= TREE_OPERAND (incr
, 0);
487 if (INDEX_LIMIT_CHECK (TREE_OPERAND (incr
, 1)))
489 cond_node
= TREE_OPERAND (incr
, 1);
492 incr
= TREE_OPERAND (incr
, 0);
495 incr_code
= TREE_CODE (incr_node
);
496 if ((incr_code
== PREDECREMENT_EXPR
|| incr_code
== POSTDECREMENT_EXPR
497 || incr_code
== PREINCREMENT_EXPR
|| incr_code
== POSTINCREMENT_EXPR
)
498 && TREE_CODE_CLASS (TREE_CODE (cond_node
)) == '<')
500 if (!INDEX_LIMIT_CHECK (cond_node
))
503 VARRAY_PUSH_GENERIC_PTR (induction_chain
, xmalloc (sizeof (induction
)));
504 ind_ptr
= VARRAY_TOP (induction_chain
, generic
);
505 loop_def
->ind
= ind_ptr
;
506 ind_ptr
->variable
= IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND
508 ind_ptr
->increment
= TREE_INT_CST_LOW (TREE_OPERAND (incr_node
, 1));
509 if (TREE_CODE (incr_node
) == PREDECREMENT_EXPR
510 || TREE_CODE (incr_node
) == POSTDECREMENT_EXPR
)
511 ind_ptr
->increment
= -ind_ptr
->increment
;
513 ind_ptr
->low_bound
= get_low_bound (init_node
, ind_ptr
->variable
);
514 if (TREE_CODE (TREE_OPERAND (cond_node
, 0)) == VAR_DECL
515 && IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (cond_node
, 0)))
516 == ind_ptr
->variable
)
517 if (TREE_CODE (TREE_OPERAND (cond_node
, 1)) == INTEGER_CST
)
518 ind_ptr
->high_bound
= TREE_INT_CST_LOW (TREE_OPERAND (cond_node
, 1));
520 ind_ptr
->high_bound
= ind_ptr
->increment
< 0 ? INT_MIN
: INT_MAX
;
521 else if (TREE_CODE (TREE_OPERAND (cond_node
, 1)) == VAR_DECL
522 && IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (cond_node
, 1)))
523 == ind_ptr
->variable
)
524 if (TREE_CODE (TREE_OPERAND (cond_node
, 0)) == INTEGER_CST
)
525 ind_ptr
->high_bound
= TREE_INT_CST_LOW (TREE_OPERAND (cond_node
, 0));
527 ind_ptr
->high_bound
= ind_ptr
->increment
< 0 ? INT_MIN
: INT_MAX
;
534 /* Return the low bound for induction VARIABLE in NODE */
537 get_low_bound (node
, variable
)
542 if (TREE_CODE (node
) == SCOPE_STMT
)
543 node
= TREE_CHAIN (node
);
548 while (TREE_CODE (node
) == COMPOUND_EXPR
)
550 if (TREE_CODE (TREE_OPERAND (node
, 0)) == MODIFY_EXPR
551 && (TREE_CODE (TREE_OPERAND (node
, 0)) == VAR_DECL
552 && IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (node
, 0)))
557 if (TREE_CODE (node
) == EXPR_STMT
)
558 node
= TREE_OPERAND (node
, 0);
559 if (TREE_CODE (node
) == MODIFY_EXPR
560 && (TREE_CODE (TREE_OPERAND (node
, 0)) == VAR_DECL
561 && IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (node
, 0)))
564 return TREE_INT_CST_LOW (TREE_OPERAND (node
, 1));
570 /* Return the ordinal subscript position for IND_VAR if it is an induction
571 variable contained in OUTER_LOOP, otherwise return -1. */
574 have_induction_variable (outer_loop
, ind_var
)
580 unsigned int ind_idx
= 0;
581 unsigned int loop_idx
= 0;
583 for (loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, loop_idx
);
584 loop_ptr
&& loop_idx
< VARRAY_SIZE (loop_chain
);
585 loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, ++loop_idx
))
586 if (loop_ptr
->outer_loop
== outer_loop
)
587 for (ind_ptr
= loop_ptr
->ind
;
588 ind_ptr
&& ind_idx
< VARRAY_SIZE (induction_chain
);
589 ind_ptr
= ind_ptr
->next
)
591 if (! strcmp (ind_ptr
->variable
, ind_var
))
597 /* Chain the nodes of 'loop_chain'. */
602 unsigned int loop_idx
= 0;
603 loop
*loop_ptr
, *prev_loop_ptr
= 0;
605 prev_loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, loop_idx
);
606 for (loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, ++loop_idx
);
607 loop_ptr
&& loop_idx
< VARRAY_SIZE (loop_chain
);
608 loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, ++loop_idx
))
610 if (prev_loop_ptr
->outer_loop
== loop_ptr
->outer_loop
)
612 if (prev_loop_ptr
->depth
== loop_ptr
->depth
- 1)
613 prev_loop_ptr
->next_nest
= loop_ptr
;
614 prev_loop_ptr
= loop_ptr
;
619 /* Check the dependence for each member of 'def_use_chain'. */
622 get_node_dependence ()
628 for (du_ptr
= VARRAY_GENERIC_PTR (def_use_chain
, du_idx
);
629 du_ptr
&& du_idx
< VARRAY_SIZE (def_use_chain
);
630 du_ptr
= VARRAY_GENERIC_PTR (def_use_chain
, du_idx
++))
632 if (du_ptr
->status
== unseen
)
633 check_node_dependence (du_ptr
);
637 /* Check the dependence for definition DU. */
640 check_node_dependence (du
)
643 def_use
*def_ptr
, *use_ptr
;
644 dependence
*dep_ptr
, *dep_list
;
645 subscript icoefficients
[MAX_SUBSCRIPTS
];
646 subscript ocoefficients
[MAX_SUBSCRIPTS
];
647 loop
*loop_ptr
, *ck_loop_ptr
;
648 unsigned int loop_idx
= 0;
649 int distance
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
653 enum direction_type direction
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
654 enum complexity_type complexity
[MAX_SUBSCRIPTS
];
658 for (j
= 1 ; j
< MAX_SUBSCRIPTS
; j
++)
660 direction
[j
][0] = undef
;
664 for (def_ptr
= du
; def_ptr
; def_ptr
= def_ptr
->next
)
666 if (def_ptr
->type
!= def
)
668 subscript_count
= get_coefficients (def_ptr
, ocoefficients
);
669 if (subscript_count
< 0)
673 for (loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, loop_idx
);
674 loop_ptr
&& loop_idx
< VARRAY_SIZE (loop_chain
);
675 loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, ++loop_idx
))
677 if (loop_ptr
->outer_loop
== def_ptr
->outer_loop
)
682 for (ck_loop_ptr
= loop_ptr
;
683 ck_loop_ptr
&& loop_idx
< VARRAY_SIZE (loop_chain
);
684 ck_loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, ++loop_idx
))
686 if (ck_loop_ptr
->outer_loop
== def_ptr
->outer_loop
687 && ck_loop_ptr
->status
== unnormal
)
693 normalize_coefficients (ocoefficients
, loop_ptr
, subscript_count
);
695 for (use_ptr
= du
; use_ptr
; use_ptr
= use_ptr
->next
)
697 if (def_ptr
== use_ptr
698 || def_ptr
->outer_loop
!= use_ptr
->outer_loop
)
700 def_ptr
->status
= seen
;
701 use_ptr
->status
= seen
;
702 subscript_count
= get_coefficients (use_ptr
, icoefficients
);
703 normalize_coefficients (icoefficients
, loop_ptr
, subscript_count
);
704 classify_dependence (icoefficients
, ocoefficients
, complexity
,
705 &separability
, subscript_count
);
707 for (i
= 1, ck_loop_ptr
= loop_ptr
; ck_loop_ptr
; i
++)
709 for (j
= 1; j
<= subscript_count
; j
++)
711 direction
[i
][j
] = star
;
712 distance
[i
][j
] = INT_MAX
;
713 if (separability
&& complexity
[j
] == ziv
)
714 ziv_test (icoefficients
, ocoefficients
, direction
, distance
,
716 else if (separability
717 && (complexity
[j
] == strong_siv
718 || complexity
[j
] == weak_zero_siv
719 || complexity
[j
] == weak_crossing_siv
))
720 siv_test (icoefficients
, ocoefficients
, direction
, distance
,
723 gcd_test (icoefficients
, ocoefficients
, direction
, distance
,
725 /* ?? Add other tests: single variable exact test, banerjee */
728 ck_loop_ptr
= ck_loop_ptr
->next_nest
;
731 merge_dependencies (direction
, distance
, i
- 1, j
- 1);
734 for (j
= 1; j
<= i
- 1; j
++)
736 if (direction
[j
][0] != independent
)
739 if (! have_dependence
)
742 VARRAY_PUSH_GENERIC_PTR (dep_chain
, xmalloc (sizeof (dependence
)));
743 dep_ptr
= VARRAY_TOP (dep_chain
, generic
);
744 dep_ptr
->source
= use_ptr
->expression
;
745 dep_ptr
->destination
= def_ptr
->expression
;
748 if (def_ptr
< use_ptr
&& use_ptr
->type
== use
)
749 dep_ptr
->dependence
= flow
;
750 else if (def_ptr
> use_ptr
&& use_ptr
->type
== use
)
751 dep_ptr
->dependence
= anti
;
752 else dep_ptr
->dependence
= output
;
754 for (j
= 1 ; j
<= i
- 1 ; j
++)
756 if (direction
[j
][0] == gt
)
758 dep_ptr
->dependence
= anti
;
759 direction
[j
][0] = lt
;
760 distance
[j
][0] = -distance
[j
][0];
763 else if (direction
[j
][0] == lt
)
765 dep_ptr
->dependence
= flow
;
769 for (j
= 1 ; j
< MAX_SUBSCRIPTS
; j
++)
771 dep_ptr
->direction
[j
] = direction
[j
][0];
772 dep_ptr
->distance
[j
] = distance
[j
][0];
775 for (dep_list
= def_ptr
->dep
;
776 dep_list
&& dep_list
->next
;
777 dep_list
= dep_list
->next
)
782 /* Dummy for rtl interface */
783 dependence
*dep_root_ptr
;
785 VARRAY_PUSH_GENERIC_PTR (dep_chain
, xmalloc (sizeof (dependence
)));
786 dep_root_ptr
= VARRAY_TOP (dep_chain
, generic
);
787 dep_root_ptr
->source
= 0;
788 dep_root_ptr
->destination
= def_ptr
->expression
;
789 dep_root_ptr
->dependence
= none
;
790 dep_root_ptr
->next
= dep_ptr
;
791 def_ptr
->dep
= dep_ptr
;
794 dep_list
->next
= dep_ptr
;
799 /* Get the COEFFICIENTS and offset for def/use DU. */
802 get_coefficients (du
, coefficients
)
804 subscript coefficients
[MAX_SUBSCRIPTS
];
812 for (array_ref
= du
->expression
;
813 TREE_CODE (array_ref
) == ARRAY_REF
;
814 array_ref
= TREE_OPERAND (array_ref
, 0))
819 for (i
= 0; i
< MAX_SUBSCRIPTS
; i
++)
821 coefficients
[i
].position
= 0;
822 coefficients
[i
].coefficient
= INT_MIN
;
823 coefficients
[i
].offset
= INT_MIN
;
824 coefficients
[i
].variable
= 0;
825 coefficients
[i
].next
= 0;
828 for (array_ref
= du
->expression
;
829 TREE_CODE (array_ref
) == ARRAY_REF
;
830 array_ref
= TREE_OPERAND (array_ref
, 0))
832 if (TREE_CODE (TREE_OPERAND (array_ref
, 1)) == INTEGER_CST
)
833 coefficients
[idx
].offset
= TREE_INT_CST_LOW (TREE_OPERAND (array_ref
, 1));
835 if (get_one_coefficient (TREE_OPERAND (array_ref
, 1),
836 &coefficients
[idx
], du
, 0) < 0)
843 /* Get the COEFFICIENTS and offset for NODE having TYPE and defined in DU. */
846 get_one_coefficient (node
, coefficients
, du
, type
)
848 subscript
*coefficients
;
850 enum tree_code
*type
;
852 enum tree_code tree_op
, tree_op_code
;
855 tree_op
= TREE_CODE (node
);
859 if (tree_op
== VAR_DECL
)
861 index
= have_induction_variable (du
->outer_loop
,
862 IDENTIFIER_POINTER (DECL_NAME (node
)));
865 coefficients
->position
= index
;
866 coefficients
->variable
= IDENTIFIER_POINTER (DECL_NAME (node
));
867 coefficients
->coefficient
= 1;
868 if (coefficients
->offset
== INT_MIN
)
869 coefficients
->offset
= 0;
873 else if (tree_op
== INTEGER_CST
)
875 return TREE_INT_CST_LOW (node
);
877 else if (tree_op
== NON_LVALUE_EXPR
)
879 return get_one_coefficient (TREE_OPERAND (node
, 0), coefficients
, du
,
882 else if (tree_op
== PLUS_EXPR
)
884 value
= get_one_coefficient (TREE_OPERAND (node
, 0), coefficients
, du
,
886 if (tree_op_code
== INTEGER_CST
)
887 coefficients
->offset
= value
;
889 value
= get_one_coefficient (TREE_OPERAND (node
, 1), coefficients
, du
,
891 if (tree_op_code
== INTEGER_CST
)
892 coefficients
->offset
= value
;
896 else if (tree_op
== MINUS_EXPR
)
898 value
= get_one_coefficient (TREE_OPERAND (node
, 0), coefficients
, du
,
900 if (tree_op_code
== INTEGER_CST
)
901 coefficients
->offset
= value
;
903 value
= get_one_coefficient (TREE_OPERAND (node
, 1), coefficients
, du
,
905 if (tree_op_code
== INTEGER_CST
)
906 coefficients
->offset
= -value
;
910 else if (tree_op
== MULT_EXPR
)
912 int value0
, value1
, value0_is_idx
, value1_is_idx
;
914 value0
= get_one_coefficient (TREE_OPERAND (node
, 0), coefficients
, du
,
916 if (tree_op_code
== VAR_DECL
)
919 value1
= get_one_coefficient (TREE_OPERAND (node
, 1), coefficients
, du
,
921 if (tree_op_code
== VAR_DECL
)
925 coefficients
->coefficient
= value1
;
926 else if (value1_is_idx
)
927 coefficients
->coefficient
= value0
;
932 /* Adjust the COEFFICIENTS as if loop LOOP_PTR were normalized to start at 0. */
935 normalize_coefficients (coefficients
, loop_ptr
, count
)
936 subscript coefficients
[MAX_SUBSCRIPTS
];
944 for (i
= 1; i
<= count
; i
++)
946 for (ck_loop_ptr
= loop_ptr
; ck_loop_ptr
;
947 ck_loop_ptr
= ck_loop_ptr
->next_nest
)
948 for (ind_ptr
= ck_loop_ptr
->ind
; ind_ptr
; ind_ptr
= ind_ptr
->next
)
950 if (coefficients
[i
].variable
== ind_ptr
->variable
)
952 if (ind_ptr
->low_bound
< ind_ptr
->high_bound
)
953 coefficients
[i
].offset
+= coefficients
[i
].coefficient
954 * ind_ptr
->low_bound
;
955 else if (ind_ptr
->high_bound
!= INT_MIN
)
957 coefficients
[i
].offset
= coefficients
[i
].coefficient
958 * ind_ptr
->high_bound
;
959 coefficients
[i
].coefficient
= coefficients
[i
].coefficient
968 /* Determine the COMPLEXITY and SEPARABILITY for COUNT subscripts of
969 inputs ICOEFFICIENTS and outputs OCOEFFICIENTS */
972 classify_dependence (icoefficients
, ocoefficients
, complexity
, separability
,
974 subscript icoefficients
[MAX_SUBSCRIPTS
];
975 subscript ocoefficients
[MAX_SUBSCRIPTS
];
976 enum complexity_type complexity
[MAX_SUBSCRIPTS
];
980 char *iiv_used
[MAX_SUBSCRIPTS
];
981 char *oiv_used
[MAX_SUBSCRIPTS
];
982 int ocoeff
[MAX_SUBSCRIPTS
];
983 int icoeff
[MAX_SUBSCRIPTS
];
986 memset (iiv_used
, 0, sizeof (tree
) * MAX_SUBSCRIPTS
);
987 memset (oiv_used
, 0, sizeof (tree
) * MAX_SUBSCRIPTS
);
988 memset (icoeff
, 0, sizeof (int) * MAX_SUBSCRIPTS
);
989 memset (ocoeff
, 0, sizeof (int) * MAX_SUBSCRIPTS
);
990 for (idx
= 1; idx
<= count
; idx
++)
992 if (icoefficients
[idx
].variable
!= 0)
996 iiv_used
[idx
] = icoefficients
[idx
].variable
;
997 icoeff
[idx
] = icoefficients
[idx
].coefficient
;
1000 if (ocoefficients
[idx
].variable
!= 0)
1002 if (! oiv_used
[idx
])
1004 oiv_used
[idx
] = ocoefficients
[idx
].variable
;
1005 ocoeff
[idx
] = ocoefficients
[idx
].coefficient
;
1010 for (idx
= 1; idx
<= count
; idx
++)
1012 if (iiv_used
[idx
] == 0 && oiv_used
[idx
] == 0)
1013 complexity
[idx
] = ziv
;
1014 else if (iiv_used
[idx
] == oiv_used
[idx
])
1016 if (icoeff
[idx
] == ocoeff
[idx
])
1017 complexity
[idx
] = strong_siv
;
1018 else if (icoeff
[idx
] == -1 * ocoeff
[idx
])
1019 complexity
[idx
] = weak_crossing_siv
;
1021 complexity
[idx
] = weak_siv
;
1023 else if (icoeff
[idx
] == 0 || ocoeff
[idx
] == 0)
1024 complexity
[idx
] = weak_zero_siv
;
1025 else complexity
[idx
] = miv
;
1029 for (idx
= 1; idx
<= count
; idx
++)
1031 for (cidx
= 1; cidx
<= count
; cidx
++)
1034 && iiv_used
[idx
] && oiv_used
[cidx
]
1035 && iiv_used
[idx
] == oiv_used
[cidx
])
1041 /* Determine the DIRECTION and DISTANCE dependency for subscript SUB of
1042 inputs ICOEFFICIENTS and outputs OCOEFFICIENTS of loop LOOP_PTR using
1043 the zero induction variable test */
1046 ziv_test (icoefficients
, ocoefficients
, direction
, distance
, loop_ptr
, sub
)
1047 subscript icoefficients
[MAX_SUBSCRIPTS
];
1048 subscript ocoefficients
[MAX_SUBSCRIPTS
];
1049 enum direction_type direction
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1050 int distance
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1056 if (ocoefficients
[sub
].offset
!=
1057 icoefficients
[sub
].offset
)
1058 direction
[loop_ptr
->depth
][idx
] = independent
;
1061 /* Determine the DIRECTION and DISTANCE dependency for subscript SUB of
1062 inputs ICOEFFICIENTS and outputs OCOEFFICIENTS of loop LOOP_PTR using
1063 the single induction variable test */
1066 siv_test (icoefficients
, ocoefficients
, direction
, distance
, loop_ptr
, sub
)
1067 subscript icoefficients
[MAX_SUBSCRIPTS
];
1068 subscript ocoefficients
[MAX_SUBSCRIPTS
];
1069 enum direction_type direction
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1070 int distance
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1078 if (! check_subscript_induction (&icoefficients
[sub
], &ocoefficients
[sub
],
1082 coef_diff
= icoefficients
[sub
].offset
- ocoefficients
[sub
].offset
;
1083 /* strong_siv requires equal coefficients. weak_crossing_siv requires
1084 coefficients to have equal absolute value. weak_zero_siv uses the
1085 nonzero coefficient. */
1087 if (ocoefficients
[sub
].coefficient
== INT_MIN
)
1088 coef
= icoefficients
[sub
].coefficient
;
1089 else if (icoefficients
[sub
].coefficient
== INT_MIN
)
1090 coef
= ocoefficients
[sub
].coefficient
;
1091 else if (ocoefficients
[sub
].coefficient
==
1092 -1 * icoefficients
[sub
].coefficient
)
1093 coef
= 2 * abs (ocoefficients
[sub
].coefficient
);
1095 coef
= icoefficients
[sub
].coefficient
;
1097 gcd
= -coef_diff
/ coef
;
1098 if (gcd
* coef
!= -coef_diff
)
1100 direction
[loop_ptr
->depth
][sub
] = independent
;
1104 distance
[loop_ptr
->depth
][sub
] = gcd
;
1106 direction
[loop_ptr
->depth
][sub
] = gt
;
1108 direction
[loop_ptr
->depth
][sub
] = lt
;
1110 direction
[loop_ptr
->depth
][sub
] = eq
;
1114 /* Return 1 if an induction variable of LOOP_PTR is used by either
1115 input ICOEFFICIENT or output OCOEFFICIENT */
1118 check_subscript_induction (icoefficient
, ocoefficient
, loop_ptr
)
1119 subscript
*icoefficient
;
1120 subscript
*ocoefficient
;
1124 int sub_ind_input
= 0;
1125 int sub_ind_output
= 0;
1127 for (ind_ptr
= loop_ptr
->ind
; ind_ptr
; ind_ptr
= ind_ptr
->next
)
1129 if (icoefficient
->variable
== ind_ptr
->variable
)
1131 if (ocoefficient
->variable
== ind_ptr
->variable
)
1134 if (sub_ind_input
|| sub_ind_output
)
1140 #define abs(n) (n < 0 ? -n : n)
1142 /* Determine the DIRECTION and DISTANCE dependency for subscript SUB of
1143 inputs ICOEFFICIENTS and outputs OCOEFFICIENTS of loop LOOP_PTR using
1144 the greatest common denominator test */
1147 gcd_test (icoefficients
, ocoefficients
, direction
, distance
, loop_ptr
, sub
)
1148 subscript icoefficients
[MAX_SUBSCRIPTS
];
1149 subscript ocoefficients
[MAX_SUBSCRIPTS
];
1150 enum direction_type direction
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1151 int distance
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1158 if (! check_subscript_induction (&icoefficients
[sub
], &ocoefficients
[sub
],
1162 g
= find_gcd (icoefficients
[sub
].coefficient
,
1163 ocoefficients
[sub
].coefficient
);
1166 coef_diff
= icoefficients
[sub
].offset
- ocoefficients
[sub
].offset
;
1168 if (gg
* g
!= coef_diff
)
1170 direction
[loop_ptr
->depth
][sub
] = independent
;
1173 /* ?? gcd does not yield direction and distance. Wolfe's direction
1174 vector hierarchy can be used to give this. */
1177 /* Find the gcd of X and Y using Euclid's algorithm */
1209 /* Merge SUBSCRIPT_COUNT DIRECTIONs and DISTANCEs for LOOP_COUNT loops.
1210 We use a predefined array to handle the direction merge.
1211 The distance merge makes use of the fact that distances default to
1212 INT_MAX. Distances are '&' together. Watch out for a negative distance.
1216 merge_dependencies (direction
, distance
, loop_count
, subscript_count
)
1217 enum direction_type direction
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1218 int distance
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1220 int subscript_count
;
1225 enum direction_type direction_merge
[8][8] =
1226 {{lt
, le
, le
, star
, star
, lt
, independent
, lt
},
1227 {le
, le
, le
, star
, star
, le
, independent
, le
},
1228 {le
, le
, eq
, ge
, ge
, eq
, independent
, eq
},
1229 {star
, star
, ge
, gt
, ge
, gt
, independent
, ge
},
1230 {star
, star
, ge
, ge
, ge
, ge
, independent
, ge
},
1231 {lt
, le
, eq
, gt
, ge
, star
, independent
, star
},
1232 {independent
, independent
, independent
, independent
, independent
},
1233 {independent
, independent
, independent
}
1236 for (i
= 1; i
<= loop_count
; i
++)
1238 distance
[i
][0] = INT_MAX
;
1239 direction
[i
][0] = star
;
1241 for (j
= 1; j
<= subscript_count
; j
++)
1243 if (distance
[i
][j
] < 0)
1245 distance
[i
][0] = distance
[i
][0] & abs (distance
[i
][j
]);
1249 distance
[i
][0] = distance
[i
][0] & distance
[i
][j
];
1250 direction
[i
][0] = direction_merge
[(int)direction
[i
][0]]
1251 [(int)direction
[i
][j
]];
1253 distance
[i
][0] = sign
* distance
[i
][0];
1257 /* Dump ARRAY_REF NODE. */
1260 dump_array_ref (node
)
1263 enum tree_code tree_op
= TREE_CODE (node
);
1265 if (tree_op
== VAR_DECL
)
1267 printf ("%s", IDENTIFIER_POINTER (DECL_NAME (node
)));
1269 else if (tree_op
== INTEGER_CST
)
1271 printf ("%d", (int)TREE_INT_CST_LOW (node
));
1273 else if (tree_op
== PLUS_EXPR
)
1275 dump_array_ref (TREE_OPERAND (node
, 0));
1277 dump_array_ref (TREE_OPERAND (node
, 1));
1279 else if (tree_op
== MINUS_EXPR
)
1281 dump_array_ref (TREE_OPERAND (node
, 0));
1283 dump_array_ref (TREE_OPERAND (node
, 1));
1285 else if (tree_op
== MULT_EXPR
)
1287 dump_array_ref (TREE_OPERAND (node
, 0));
1289 dump_array_ref (TREE_OPERAND (node
, 1));
1293 /* Dump def/use DU. */
1296 dump_one_node (du
, seen
)
1301 dependence
*dep_ptr
;
1304 for (du_ptr
= du
; du_ptr
; du_ptr
= du_ptr
->next
)
1306 printf ("%s ", du_ptr
->variable
);
1307 for (array_ref
= du_ptr
->expression
;
1308 TREE_CODE (array_ref
) == ARRAY_REF
;
1309 array_ref
= TREE_OPERAND (array_ref
, 0))
1312 dump_array_ref (TREE_OPERAND (array_ref
, 1));
1316 printf (" Outer Loop %x Containing Loop %x Expression %x %s\n",
1317 (int)du_ptr
->outer_loop
,
1318 (int)du_ptr
->containing_loop
,
1319 (int)du_ptr
->expression
, du_ptr
->type
== def
? "Def" : "Use");
1320 VARRAY_PUSH_GENERIC_PTR (*seen
, du_ptr
);
1322 for (dep_ptr
= du_ptr
->dep
; dep_ptr
; dep_ptr
= dep_ptr
->next
)
1325 printf ("%s Dependence with %x ",
1326 dependence_string
[(int)dep_ptr
->dependence
],
1327 (int)dep_ptr
->source
);
1328 printf ("Dir/Dist ");
1329 for (i
= 1 ; i
< MAX_SUBSCRIPTS
; i
++)
1330 if (dep_ptr
->direction
[i
] != undef
)
1331 printf ("[%d] %s/%d ", i
,
1332 direction_string
[(int)dep_ptr
->direction
[i
]],
1333 dep_ptr
->distance
[i
]);
1339 /* Dump dependence info. */
1342 dump_node_dependence (void)
1345 unsigned int du_idx
, seen_idx
, i
;
1348 VARRAY_GENERIC_PTR_INIT (seen
, 20, "seen");
1351 for (du_ptr
= VARRAY_GENERIC_PTR (def_use_chain
, du_idx
);
1352 du_idx
< VARRAY_SIZE (def_use_chain
);
1353 du_ptr
= VARRAY_GENERIC_PTR (def_use_chain
, du_idx
++))
1355 for (i
= 0; i
< VARRAY_SIZE (seen
) && VARRAY_GENERIC_PTR (seen
, i
)
1357 if (i
>= VARRAY_SIZE (seen
))
1358 dump_one_node (du_ptr
, &seen
);
1363 /* Return the index into 'dep_chain' if there is a dependency for destination
1364 dest_to_remember (set by remember_dest_for_dependence) and source node.
1365 Called by the front end, which adds the index onto a MEM rtx. */
1368 search_dependence (node
)
1371 dependence
*dep_ptr
;
1377 if (TREE_CODE (node
) == INDIRECT_REF
&& TREE_OPERAND (node
, 1)
1378 && TREE_CODE (TREE_OPERAND (node
, 1)) == ARRAY_REF
)
1379 node
= TREE_OPERAND (node
, 1);
1381 for (dep_ptr
= VARRAY_GENERIC_PTR (dep_chain
, 0);
1382 dep_ptr
; dep_ptr
= VARRAY_GENERIC_PTR (dep_chain
, dep_idx
++))
1384 if ((node
== dep_ptr
->source
1385 && dest_to_remember
== dep_ptr
->destination
)
1386 || (! dep_ptr
->source
&& node
== dep_ptr
->destination
))
1394 /* Remember a destination NODE for search_dependence. */
1397 remember_dest_for_dependence (node
)
1402 if (TREE_CODE (node
) == INDIRECT_REF
&& TREE_OPERAND (node
, 1)
1403 && TREE_CODE (TREE_OPERAND (node
, 1)) == ARRAY_REF
)
1404 node
= TREE_OPERAND (node
, 1);
1405 dest_to_remember
= node
;
1409 #ifndef MEM_DEPENDENCY
1410 #define MEM_DEPENDENCY(RTX) XCWINT(RTX, 2, MEM)
1413 /* Return 1 along with the dependence DIRECTION and DISTANCE if there is a
1414 dependence from dest_rtx to src_rtx. */
1417 have_dependence_p (dest_rtx
, src_rtx
, direction
, distance
)
1420 enum direction_type direction
[MAX_SUBSCRIPTS
];
1421 int distance
[MAX_SUBSCRIPTS
];
1423 int dest_idx
, src_idx
;
1425 dependence
*dep_ptr
;
1427 if (GET_CODE (SET_DEST (PATTERN (dest_rtx
))) == MEM
)
1429 dest
= SET_DEST (PATTERN (dest_rtx
));
1430 dest_idx
= MEM_DEPENDENCY (dest
) - 1;
1432 if (GET_CODE (SET_SRC (PATTERN (src_rtx
))) == MEM
)
1434 src
= SET_SRC (PATTERN (src_rtx
));
1435 src_idx
= MEM_DEPENDENCY (dest
) - 1;
1437 if (dest_idx
>= 0 || src_idx
>= 0)
1440 for (dep_ptr
= VARRAY_GENERIC_PTR (dep_chain
, -dest_idx
);
1441 dep_ptr
; dep_ptr
= dep_ptr
->next
)
1443 if (dep_ptr
== VARRAY_GENERIC_PTR (dep_chain
, -src_idx
))
1445 direction
= (enum direction_type
*) &dep_ptr
->direction
;
1446 distance
= (int*) &dep_ptr
->distance
;
1453 /* Cleanup when dependency analysis is complete. */
1456 end_dependence_analysis (void)
1458 VARRAY_FREE (dep_chain
);