1 /* Analyze loop dependencies
2 Copyright (C) 2000, 2002 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 2, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING. If not, write to the Free
18 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
22 Practical Dependence Testing, Goff, Kennedy, Tseng, PLDI, 1991
23 High Performance Compilers for Parallel Computing, Wolfe
36 #define MAX_SUBSCRIPTS 13
39 We perform the following steps:
41 Build the data structures def_use_chain, loop_chain, and induction_chain.
43 Determine if a loop index is a normalized induction variable.
44 A loop is currently considered to be a for loop having an index set to an
45 initial value, conditional check of the index, and increment/decrement of
48 Determine the distance and direction vectors. Both are two dimensioned
49 arrays where the first dimension represents a loop and the second
50 dimension represents a subscript. Dependencies are actually per loop, not
51 per subscript. So for:
52 for (i = 0; i < 10; i++)
53 for (j = 0; j < 10; j++)
54 array [i][j] = array[i][j-1]
55 We find the dependencies: loop1/sub_i, loop1/sub_j, loop2/sub_i, loop2/sub_j
56 and then intersect loop1/sub_i V loop2/sub_i and loop1/sub_i V loop2/sub_j
57 We determine the type of dependence, which determines which test we use.
58 We then try to refine the type of dependence we have and add the
59 dependence to the dep_chain
62 enum dependence_type
{dt_flow
, dt_anti
, dt_output
, dt_none
};
64 static const char *const dependence_string
[] = {"flow", "anti", "output", "none"};
66 enum direction_type
{lt
, le
, eq
, gt
, ge
, star
, independent
, undef
};
68 static const char *const direction_string
[] = {"<", "<=", "=", ">", ">=", "*",
69 "INDEPENDENT", "UNDEFINED"};
71 enum def_use_type
{def
, use
, init_def_use
};
73 enum du_status_type
{seen
, unseen
};
75 enum loop_status_type
{normal
, unnormal
};
77 enum complexity_type
{ziv
, strong_siv
, weak_siv
, weak_zero_siv
,
78 weak_crossing_siv
, miv
};
80 /* Given a def/use one can chase the next chain to follow the def/use
81 for that variable. Alternately one can sequentially follow each
82 element of def_use_chain. */
84 typedef struct def_use
88 /* loop containing this def/use */
95 enum def_use_type type
;
97 enum du_status_type status
;
98 /* next def/use for this same name */
100 /* dependencies for this def */
101 struct dependence
*dep
;
104 /* Given a loop* one can chase the next_nest chain to follow the nested
105 loops for that loop. Alternately one can sequentially follow each
106 element of loop_chain and check outer_loop to get all loops
107 contained within a certain loop. */
111 /* outermost loop containing this loop */
114 tree containing_loop
;
115 /* nest level for this loop */
117 /* can loop be normalized? */
118 enum loop_status_type status
;
119 /* loop* for loop contained in this loop */
120 struct loop
*next_nest
;
121 /* induction variables for this loop. Currently only the index variable. */
122 struct induction
*ind
;
125 /* Pointed to by loop. One per induction variable. */
127 typedef struct induction
130 const char *variable
;
131 /* increment. Currently only +1 or -1 */
137 /* next induction variable for this loop. Currently null. */
138 struct induction
*next
;
141 /* Pointed to by def/use. One per dependence. */
143 typedef struct dependence
147 enum dependence_type dependence
;
148 enum direction_type direction
[MAX_SUBSCRIPTS
];
149 int distance
[MAX_SUBSCRIPTS
];
150 struct dependence
*next
;
153 /* subscripts are represented by an array of these. Each reflects one
154 X * i + Y term, where X and Y are constants. */
156 typedef struct subscript
158 /* ordinal subscript number */
165 const char *variable
;
166 /* next subscript term. Currently null. */
167 struct subscript
*next
;
170 /* Remember the destination the front end encountered. */
172 static tree dest_to_remember
;
174 /* Chain for def_use */
175 static varray_type def_use_chain
;
177 /* Chain for dependence */
178 static varray_type dep_chain
;
181 static varray_type loop_chain
;
183 /* Chain for induction */
184 static varray_type induction_chain
;
186 void init_dependence_analysis
PARAMS ((tree
));
187 static void build_def_use
PARAMS ((tree
, enum def_use_type
));
188 static loop
* add_loop
PARAMS ((tree
, tree
, int));
189 static int find_induction_variable
PARAMS ((tree
, tree
, tree
, loop
*));
190 static int get_low_bound
PARAMS ((tree
, const char*));
191 static int have_induction_variable
PARAMS ((tree
, const char*));
192 static void link_loops
PARAMS ((void));
193 static void get_node_dependence
PARAMS ((void));
194 static void check_node_dependence
PARAMS ((def_use
*));
195 static int get_coefficients
PARAMS ((def_use
*, subscript
[]));
196 static int get_one_coefficient
PARAMS ((tree
, subscript
*, def_use
*, enum tree_code
*));
197 static void normalize_coefficients
PARAMS ((subscript
[], loop
*, int));
198 static void classify_dependence
PARAMS ((subscript
[], subscript
[],
199 enum complexity_type
[], int*, int));
200 static void ziv_test
PARAMS ((subscript
[], subscript
[],
201 enum direction_type
[][MAX_SUBSCRIPTS
],
202 int[][MAX_SUBSCRIPTS
], loop
*, int));
203 static void siv_test
PARAMS ((subscript
[], subscript
[],
204 enum direction_type
[][MAX_SUBSCRIPTS
],
205 int[][MAX_SUBSCRIPTS
], loop
*, int));
206 static int check_subscript_induction
PARAMS ((subscript
*, subscript
*, loop
*));
207 static void gcd_test
PARAMS ((subscript
[], subscript
[], enum
208 direction_type
[][MAX_SUBSCRIPTS
],
209 int[][MAX_SUBSCRIPTS
], loop
*, int));
210 static int find_gcd
PARAMS ((int, int));
211 static void merge_dependencies
PARAMS ((enum direction_type
[][MAX_SUBSCRIPTS
],
212 int[][MAX_SUBSCRIPTS
], int, int));
213 static void dump_array_ref
PARAMS ((tree
));
215 static void dump_one_node
PARAMS ((def_use
*, varray_type
*));
216 static void dump_node_dependence
PARAMS ((void));
218 int search_dependence
PARAMS ((tree
));
219 void remember_dest_for_dependence
PARAMS ((tree
));
220 int have_dependence_p
PARAMS ((rtx
, rtx
, enum direction_type
[], int[]));
221 void end_dependence_analysis
PARAMS ((void));
223 /* Build dependence chain 'dep_chain', which is used by have_dependence_p,
224 for the function given by EXP. */
227 init_dependence_analysis (exp
)
232 VARRAY_GENERIC_PTR_INIT (def_use_chain
, 50, "def_use_chain");
233 VARRAY_GENERIC_PTR_INIT (dep_chain
, 50, "dep_chain");
234 VARRAY_GENERIC_PTR_INIT (loop_chain
, 50, "loop_chain");
235 VARRAY_GENERIC_PTR_INIT (induction_chain
, 50, "induction_chain");
237 build_def_use (exp
, init_def_use
);
241 get_node_dependence ();
243 /* dump_node_dependence (&def_use_chain);*/
245 for (du_ptr
= VARRAY_TOP (def_use_chain
, generic
);
246 VARRAY_POP (def_use_chain
);
247 du_ptr
= VARRAY_TOP (def_use_chain
, generic
))
252 VARRAY_FREE (def_use_chain
);
253 VARRAY_FREE (loop_chain
);
254 VARRAY_FREE (induction_chain
);
257 /* Build ARRAY_REF def/use info 'def_use_chain' starting at EXP which is a def
261 build_def_use (exp
, du_type
)
263 enum def_use_type du_type
;
265 static tree outer_loop
;
267 static tree current_loop
;
269 static loop
*loop_def
;
274 if (du_type
== init_def_use
)
282 switch (TREE_CODE (node
))
285 node
= TREE_OPERAND (node
, 0);
288 build_def_use (TREE_VALUE (node
), 0);
289 node
= TREE_CHAIN (node
);
292 node
= TREE_CHAIN (node
);
295 if (! nloop
) outer_loop
= node
;
298 loop_def
= add_loop (node
, outer_loop
, nloop
);
299 if (find_induction_variable (TREE_OPERAND (node
, 0),
300 TREE_OPERAND (node
, 1),
301 TREE_OPERAND (node
, 2), loop_def
)
303 loop_def
->status
= unnormal
;
305 build_def_use (TREE_OPERAND (node
, 3), 0);
308 node
= TREE_CHAIN (node
);
311 /* Is an induction variable modified? */
313 && TREE_CODE (TREE_OPERAND (node
, 0)) == VAR_DECL
314 && have_induction_variable
315 (loop_def
->outer_loop
,
316 IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (node
, 0)))) >= 0)
317 loop_def
->status
= unnormal
;
319 if (TREE_CODE (TREE_OPERAND (node
, 0)) == ARRAY_REF
320 || TREE_CODE (TREE_OPERAND (node
, 0)) == INDIRECT_REF
)
321 build_def_use (TREE_OPERAND (node
, 0), def
);
323 build_def_use (TREE_OPERAND (node
, 1), use
);
324 node
= TREE_CHAIN (node
);
327 if (! TREE_OPERAND (node
, 1)
328 || TREE_CODE (TREE_OPERAND (node
, 1)) != ARRAY_REF
)
333 node
= TREE_OPERAND (node
, 1);
338 char null_string
= '\0';
340 VARRAY_PUSH_GENERIC_PTR (def_use_chain
, xmalloc (sizeof (def_use
)));
341 du_ptr
= VARRAY_GENERIC_PTR (def_use_chain
, du_idx
++);
342 du_ptr
->type
= du_type
;
343 du_ptr
->status
= unseen
;
344 du_ptr
->outer_loop
= outer_loop
;
345 du_ptr
->containing_loop
= current_loop
;
346 du_ptr
->expression
= node
;
347 du_ptr
->variable
= &null_string
;
350 for (array_ref
= node
;
351 TREE_CODE (array_ref
) == ARRAY_REF
;
352 array_ref
= TREE_OPERAND (array_ref
, 0))
355 if (TREE_CODE (array_ref
) == COMPONENT_REF
)
357 array_ref
= TREE_OPERAND (array_ref
, 1);
358 if (! (TREE_CODE (array_ref
) == FIELD_DECL
359 && TREE_CODE (TREE_TYPE (array_ref
)) == ARRAY_TYPE
))
368 && strcmp (IDENTIFIER_POINTER (DECL_NAME (array_ref
)),
369 ((def_use
*) (VARRAY_GENERIC_PTR
370 (def_use_chain
, i
)))->variable
);
376 for (tmp_duc
= ((def_use
*) (VARRAY_GENERIC_PTR (def_use_chain
, i
)));
378 tmp_duc
= ((def_use
*)tmp_duc
->next
));
379 tmp_duc
->next
= du_ptr
;
381 else du_ptr
->next
= 0;
382 du_ptr
->variable
= IDENTIFIER_POINTER (DECL_NAME (array_ref
));
389 node
= TREE_CHAIN (node
);
393 if (TREE_CODE (TREE_OPERAND (node
, 0)) == MODIFY_EXPR
)
394 build_def_use (TREE_OPERAND (node
, 0), def
);
395 node
= TREE_CHAIN (node
);
399 if (TREE_CODE_CLASS (TREE_CODE (node
)) == '2')
401 build_def_use (TREE_OPERAND (node
, 0), use
);
402 build_def_use (TREE_OPERAND (node
, 1), use
);
403 node
= TREE_CHAIN (node
);
410 /* Add a loop to 'loop_chain' corresponding to for loop LOOP_NODE at depth
411 NLOOP, whose outermost loop is OUTER_LOOP */
414 add_loop (loop_node
, outer_loop
, nloop
)
421 VARRAY_PUSH_GENERIC_PTR (loop_chain
, xmalloc (sizeof (loop
)));
422 loop_ptr
= VARRAY_TOP (loop_chain
, generic
);
423 loop_ptr
->outer_loop
= outer_loop
;
424 loop_ptr
->containing_loop
= loop_node
;
425 loop_ptr
->depth
= nloop
;
426 loop_ptr
->status
= normal
;
427 loop_ptr
->next_nest
= 0;
432 /* Update LOOP_DEF if for loop's COND_NODE and INCR_NODE define an index that
433 is a normalized induction variable. */
436 find_induction_variable (init_node
, cond_node
, incr_node
, loop_def
)
443 enum tree_code incr_code
;
446 if (! init_node
|| ! incr_node
|| ! cond_node
)
448 /* Allow for ',' operator in increment expression of FOR */
451 while (TREE_CODE (incr
) == COMPOUND_EXPR
)
453 incr_code
= TREE_CODE (TREE_OPERAND (incr
, 0));
454 if (incr_code
== PREDECREMENT_EXPR
|| incr_code
== POSTDECREMENT_EXPR
455 || incr_code
== PREINCREMENT_EXPR
|| incr_code
== POSTINCREMENT_EXPR
)
457 incr_node
= TREE_OPERAND (incr
, 0);
460 incr_code
= TREE_CODE (TREE_OPERAND (incr
, 1));
461 if (incr_code
== PREDECREMENT_EXPR
|| incr_code
== POSTDECREMENT_EXPR
462 || incr_code
== PREINCREMENT_EXPR
|| incr_code
== POSTINCREMENT_EXPR
)
464 incr_node
= TREE_OPERAND (incr
, 1);
467 incr
= TREE_OPERAND (incr
, 1);
470 /* Allow index condition to be part of logical expression */
471 cond_node
= TREE_VALUE (cond_node
);
474 #define INDEX_LIMIT_CHECK(NODE) \
475 (TREE_CODE_CLASS (TREE_CODE (NODE)) == '<') \
476 && (TREE_CODE (TREE_OPERAND (NODE, 0)) == VAR_DECL \
477 && (IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (NODE, 0))) \
478 == IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (incr_node, 0))))) \
481 while (TREE_CODE (incr
) == TRUTH_ANDIF_EXPR
482 || TREE_CODE (incr
) == TRUTH_ORIF_EXPR
)
484 if (INDEX_LIMIT_CHECK (TREE_OPERAND (incr
, 0)))
486 cond_node
= TREE_OPERAND (incr
, 0);
489 if (INDEX_LIMIT_CHECK (TREE_OPERAND (incr
, 1)))
491 cond_node
= TREE_OPERAND (incr
, 1);
494 incr
= TREE_OPERAND (incr
, 0);
497 incr_code
= TREE_CODE (incr_node
);
498 if ((incr_code
== PREDECREMENT_EXPR
|| incr_code
== POSTDECREMENT_EXPR
499 || incr_code
== PREINCREMENT_EXPR
|| incr_code
== POSTINCREMENT_EXPR
)
500 && TREE_CODE_CLASS (TREE_CODE (cond_node
)) == '<')
502 if (!INDEX_LIMIT_CHECK (cond_node
))
505 VARRAY_PUSH_GENERIC_PTR (induction_chain
, xmalloc (sizeof (induction
)));
506 ind_ptr
= VARRAY_TOP (induction_chain
, generic
);
507 loop_def
->ind
= ind_ptr
;
508 ind_ptr
->variable
= IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND
510 ind_ptr
->increment
= TREE_INT_CST_LOW (TREE_OPERAND (incr_node
, 1));
511 if (TREE_CODE (incr_node
) == PREDECREMENT_EXPR
512 || TREE_CODE (incr_node
) == POSTDECREMENT_EXPR
)
513 ind_ptr
->increment
= -ind_ptr
->increment
;
515 ind_ptr
->low_bound
= get_low_bound (init_node
, ind_ptr
->variable
);
516 if (TREE_CODE (TREE_OPERAND (cond_node
, 0)) == VAR_DECL
517 && IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (cond_node
, 0)))
518 == ind_ptr
->variable
)
520 if (TREE_CODE (TREE_OPERAND (cond_node
, 1)) == INTEGER_CST
)
521 ind_ptr
->high_bound
=
522 TREE_INT_CST_LOW (TREE_OPERAND (cond_node
, 1));
524 ind_ptr
->high_bound
= ind_ptr
->increment
< 0 ? INT_MIN
: INT_MAX
;
526 else if (TREE_CODE (TREE_OPERAND (cond_node
, 1)) == VAR_DECL
527 && IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (cond_node
, 1)))
528 == ind_ptr
->variable
)
530 if (TREE_CODE (TREE_OPERAND (cond_node
, 0)) == INTEGER_CST
)
531 ind_ptr
->high_bound
=
532 TREE_INT_CST_LOW (TREE_OPERAND (cond_node
, 0));
534 ind_ptr
->high_bound
= ind_ptr
->increment
< 0 ? INT_MIN
: INT_MAX
;
542 /* Return the low bound for induction VARIABLE in NODE */
545 get_low_bound (node
, variable
)
547 const char *variable
;
550 if (TREE_CODE (node
) == SCOPE_STMT
)
551 node
= TREE_CHAIN (node
);
556 while (TREE_CODE (node
) == COMPOUND_EXPR
)
558 if (TREE_CODE (TREE_OPERAND (node
, 0)) == MODIFY_EXPR
559 && (TREE_CODE (TREE_OPERAND (node
, 0)) == VAR_DECL
560 && IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (node
, 0)))
565 if (TREE_CODE (node
) == EXPR_STMT
)
566 node
= TREE_OPERAND (node
, 0);
567 if (TREE_CODE (node
) == MODIFY_EXPR
568 && (TREE_CODE (TREE_OPERAND (node
, 0)) == VAR_DECL
569 && IDENTIFIER_POINTER (DECL_NAME (TREE_OPERAND (node
, 0)))
572 return TREE_INT_CST_LOW (TREE_OPERAND (node
, 1));
578 /* Return the ordinal subscript position for IND_VAR if it is an induction
579 variable contained in OUTER_LOOP, otherwise return -1. */
582 have_induction_variable (outer_loop
, ind_var
)
588 unsigned int ind_idx
= 0;
589 unsigned int loop_idx
= 0;
591 for (loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, loop_idx
);
592 loop_ptr
&& loop_idx
< VARRAY_SIZE (loop_chain
);
593 loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, ++loop_idx
))
594 if (loop_ptr
->outer_loop
== outer_loop
)
595 for (ind_ptr
= loop_ptr
->ind
;
596 ind_ptr
&& ind_idx
< VARRAY_SIZE (induction_chain
);
597 ind_ptr
= ind_ptr
->next
)
599 if (! strcmp (ind_ptr
->variable
, ind_var
))
605 /* Chain the nodes of 'loop_chain'. */
610 unsigned int loop_idx
= 0;
611 loop
*loop_ptr
, *prev_loop_ptr
= 0;
613 prev_loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, loop_idx
);
614 for (loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, ++loop_idx
);
615 loop_ptr
&& loop_idx
< VARRAY_SIZE (loop_chain
);
616 loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, ++loop_idx
))
618 if (prev_loop_ptr
->outer_loop
== loop_ptr
->outer_loop
)
620 if (prev_loop_ptr
->depth
== loop_ptr
->depth
- 1)
621 prev_loop_ptr
->next_nest
= loop_ptr
;
622 prev_loop_ptr
= loop_ptr
;
627 /* Check the dependence for each member of 'def_use_chain'. */
630 get_node_dependence ()
636 for (du_ptr
= VARRAY_GENERIC_PTR (def_use_chain
, du_idx
);
637 du_ptr
&& du_idx
< VARRAY_SIZE (def_use_chain
);
638 du_ptr
= VARRAY_GENERIC_PTR (def_use_chain
, du_idx
++))
640 if (du_ptr
->status
== unseen
)
641 check_node_dependence (du_ptr
);
645 /* Check the dependence for definition DU. */
648 check_node_dependence (du
)
651 def_use
*def_ptr
, *use_ptr
;
652 dependence
*dep_ptr
, *dep_list
;
653 subscript icoefficients
[MAX_SUBSCRIPTS
];
654 subscript ocoefficients
[MAX_SUBSCRIPTS
];
655 loop
*loop_ptr
, *ck_loop_ptr
;
656 unsigned int loop_idx
= 0;
657 int distance
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
661 enum direction_type direction
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
662 enum complexity_type complexity
[MAX_SUBSCRIPTS
];
666 for (j
= 1 ; j
< MAX_SUBSCRIPTS
; j
++)
668 direction
[j
][0] = undef
;
672 for (def_ptr
= du
; def_ptr
; def_ptr
= def_ptr
->next
)
674 if (def_ptr
->type
!= def
)
676 subscript_count
= get_coefficients (def_ptr
, ocoefficients
);
677 if (subscript_count
< 0)
681 for (loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, loop_idx
);
682 loop_ptr
&& loop_idx
< VARRAY_SIZE (loop_chain
);
683 loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, ++loop_idx
))
685 if (loop_ptr
->outer_loop
== def_ptr
->outer_loop
)
690 for (ck_loop_ptr
= loop_ptr
;
691 ck_loop_ptr
&& loop_idx
< VARRAY_SIZE (loop_chain
);
692 ck_loop_ptr
= VARRAY_GENERIC_PTR (loop_chain
, ++loop_idx
))
694 if (ck_loop_ptr
->outer_loop
== def_ptr
->outer_loop
695 && ck_loop_ptr
->status
== unnormal
)
701 normalize_coefficients (ocoefficients
, loop_ptr
, subscript_count
);
703 for (use_ptr
= du
; use_ptr
; use_ptr
= use_ptr
->next
)
705 if (def_ptr
== use_ptr
706 || def_ptr
->outer_loop
!= use_ptr
->outer_loop
)
708 def_ptr
->status
= seen
;
709 use_ptr
->status
= seen
;
710 subscript_count
= get_coefficients (use_ptr
, icoefficients
);
711 normalize_coefficients (icoefficients
, loop_ptr
, subscript_count
);
712 classify_dependence (icoefficients
, ocoefficients
, complexity
,
713 &separability
, subscript_count
);
715 for (i
= 1, ck_loop_ptr
= loop_ptr
; ck_loop_ptr
; i
++)
717 for (j
= 1; j
<= subscript_count
; j
++)
719 direction
[i
][j
] = star
;
720 distance
[i
][j
] = INT_MAX
;
721 if (separability
&& complexity
[j
] == ziv
)
722 ziv_test (icoefficients
, ocoefficients
, direction
, distance
,
724 else if (separability
725 && (complexity
[j
] == strong_siv
726 || complexity
[j
] == weak_zero_siv
727 || complexity
[j
] == weak_crossing_siv
))
728 siv_test (icoefficients
, ocoefficients
, direction
, distance
,
731 gcd_test (icoefficients
, ocoefficients
, direction
, distance
,
733 /* ?? Add other tests: single variable exact test, banerjee */
736 ck_loop_ptr
= ck_loop_ptr
->next_nest
;
739 merge_dependencies (direction
, distance
, i
- 1, j
- 1);
742 for (j
= 1; j
<= i
- 1; j
++)
744 if (direction
[j
][0] != independent
)
747 if (! have_dependence
)
750 VARRAY_PUSH_GENERIC_PTR (dep_chain
, xmalloc (sizeof (dependence
)));
751 dep_ptr
= VARRAY_TOP (dep_chain
, generic
);
752 dep_ptr
->source
= use_ptr
->expression
;
753 dep_ptr
->destination
= def_ptr
->expression
;
756 if (def_ptr
< use_ptr
&& use_ptr
->type
== use
)
757 dep_ptr
->dependence
= dt_flow
;
758 else if (def_ptr
> use_ptr
&& use_ptr
->type
== use
)
759 dep_ptr
->dependence
= dt_anti
;
760 else dep_ptr
->dependence
= dt_output
;
762 for (j
= 1 ; j
<= i
- 1 ; j
++)
764 if (direction
[j
][0] == gt
)
766 dep_ptr
->dependence
= dt_anti
;
767 direction
[j
][0] = lt
;
768 distance
[j
][0] = -distance
[j
][0];
771 else if (direction
[j
][0] == lt
)
773 dep_ptr
->dependence
= dt_flow
;
777 for (j
= 1 ; j
< MAX_SUBSCRIPTS
; j
++)
779 dep_ptr
->direction
[j
] = direction
[j
][0];
780 dep_ptr
->distance
[j
] = distance
[j
][0];
783 for (dep_list
= def_ptr
->dep
;
784 dep_list
&& dep_list
->next
;
785 dep_list
= dep_list
->next
)
790 /* Dummy for rtl interface */
791 dependence
*dep_root_ptr
;
793 VARRAY_PUSH_GENERIC_PTR (dep_chain
, xmalloc (sizeof (dependence
)));
794 dep_root_ptr
= VARRAY_TOP (dep_chain
, generic
);
795 dep_root_ptr
->source
= 0;
796 dep_root_ptr
->destination
= def_ptr
->expression
;
797 dep_root_ptr
->dependence
= dt_none
;
798 dep_root_ptr
->next
= dep_ptr
;
799 def_ptr
->dep
= dep_ptr
;
802 dep_list
->next
= dep_ptr
;
807 /* Get the COEFFICIENTS and offset for def/use DU. */
810 get_coefficients (du
, coefficients
)
812 subscript coefficients
[MAX_SUBSCRIPTS
];
820 for (array_ref
= du
->expression
;
821 TREE_CODE (array_ref
) == ARRAY_REF
;
822 array_ref
= TREE_OPERAND (array_ref
, 0))
827 for (i
= 0; i
< MAX_SUBSCRIPTS
; i
++)
829 coefficients
[i
].position
= 0;
830 coefficients
[i
].coefficient
= INT_MIN
;
831 coefficients
[i
].offset
= INT_MIN
;
832 coefficients
[i
].variable
= 0;
833 coefficients
[i
].next
= 0;
836 for (array_ref
= du
->expression
;
837 TREE_CODE (array_ref
) == ARRAY_REF
;
838 array_ref
= TREE_OPERAND (array_ref
, 0))
840 if (TREE_CODE (TREE_OPERAND (array_ref
, 1)) == INTEGER_CST
)
841 coefficients
[idx
].offset
= TREE_INT_CST_LOW (TREE_OPERAND (array_ref
, 1));
843 if (get_one_coefficient (TREE_OPERAND (array_ref
, 1),
844 &coefficients
[idx
], du
, 0) < 0)
851 /* Get the COEFFICIENTS and offset for NODE having TYPE and defined in DU. */
854 get_one_coefficient (node
, coefficients
, du
, type
)
856 subscript
*coefficients
;
858 enum tree_code
*type
;
860 enum tree_code tree_op
, tree_op_code
;
863 tree_op
= TREE_CODE (node
);
867 if (tree_op
== VAR_DECL
)
869 index
= have_induction_variable (du
->outer_loop
,
870 IDENTIFIER_POINTER (DECL_NAME (node
)));
873 coefficients
->position
= index
;
874 coefficients
->variable
= IDENTIFIER_POINTER (DECL_NAME (node
));
875 coefficients
->coefficient
= 1;
876 if (coefficients
->offset
== INT_MIN
)
877 coefficients
->offset
= 0;
881 else if (tree_op
== INTEGER_CST
)
883 return TREE_INT_CST_LOW (node
);
885 else if (tree_op
== NON_LVALUE_EXPR
)
887 return get_one_coefficient (TREE_OPERAND (node
, 0), coefficients
, du
,
890 else if (tree_op
== PLUS_EXPR
)
892 value
= get_one_coefficient (TREE_OPERAND (node
, 0), coefficients
, du
,
894 if (tree_op_code
== INTEGER_CST
)
895 coefficients
->offset
= value
;
897 value
= get_one_coefficient (TREE_OPERAND (node
, 1), coefficients
, du
,
899 if (tree_op_code
== INTEGER_CST
)
900 coefficients
->offset
= value
;
904 else if (tree_op
== MINUS_EXPR
)
906 value
= get_one_coefficient (TREE_OPERAND (node
, 0), coefficients
, du
,
908 if (tree_op_code
== INTEGER_CST
)
909 coefficients
->offset
= value
;
911 value
= get_one_coefficient (TREE_OPERAND (node
, 1), coefficients
, du
,
913 if (tree_op_code
== INTEGER_CST
)
914 coefficients
->offset
= -value
;
918 else if (tree_op
== MULT_EXPR
)
920 int value0
, value1
, value0_is_idx
= 0, value1_is_idx
= 0;
922 value0
= get_one_coefficient (TREE_OPERAND (node
, 0), coefficients
, du
,
924 if (tree_op_code
== VAR_DECL
)
927 value1
= get_one_coefficient (TREE_OPERAND (node
, 1), coefficients
, du
,
929 if (tree_op_code
== VAR_DECL
)
933 coefficients
->coefficient
= value1
;
934 else if (value1_is_idx
)
935 coefficients
->coefficient
= value0
;
940 /* Adjust the COEFFICIENTS as if loop LOOP_PTR were normalized to start at 0. */
943 normalize_coefficients (coefficients
, loop_ptr
, count
)
944 subscript coefficients
[MAX_SUBSCRIPTS
];
952 for (i
= 1; i
<= count
; i
++)
954 for (ck_loop_ptr
= loop_ptr
; ck_loop_ptr
;
955 ck_loop_ptr
= ck_loop_ptr
->next_nest
)
956 for (ind_ptr
= ck_loop_ptr
->ind
; ind_ptr
; ind_ptr
= ind_ptr
->next
)
958 if (coefficients
[i
].variable
== ind_ptr
->variable
)
960 if (ind_ptr
->low_bound
< ind_ptr
->high_bound
)
961 coefficients
[i
].offset
+= coefficients
[i
].coefficient
962 * ind_ptr
->low_bound
;
963 else if (ind_ptr
->high_bound
!= INT_MIN
)
965 coefficients
[i
].offset
= coefficients
[i
].coefficient
966 * ind_ptr
->high_bound
;
967 coefficients
[i
].coefficient
= coefficients
[i
].coefficient
976 /* Determine the COMPLEXITY and SEPARABILITY for COUNT subscripts of
977 inputs ICOEFFICIENTS and outputs OCOEFFICIENTS */
980 classify_dependence (icoefficients
, ocoefficients
, complexity
, separability
,
982 subscript icoefficients
[MAX_SUBSCRIPTS
];
983 subscript ocoefficients
[MAX_SUBSCRIPTS
];
984 enum complexity_type complexity
[MAX_SUBSCRIPTS
];
988 const char *iiv_used
[MAX_SUBSCRIPTS
];
989 const char *oiv_used
[MAX_SUBSCRIPTS
];
990 int ocoeff
[MAX_SUBSCRIPTS
];
991 int icoeff
[MAX_SUBSCRIPTS
];
994 memset (iiv_used
, 0, sizeof (tree
) * MAX_SUBSCRIPTS
);
995 memset (oiv_used
, 0, sizeof (tree
) * MAX_SUBSCRIPTS
);
996 memset (icoeff
, 0, sizeof (int) * MAX_SUBSCRIPTS
);
997 memset (ocoeff
, 0, sizeof (int) * MAX_SUBSCRIPTS
);
998 for (idx
= 1; idx
<= count
; idx
++)
1000 if (icoefficients
[idx
].variable
!= 0)
1002 if (! iiv_used
[idx
])
1004 iiv_used
[idx
] = icoefficients
[idx
].variable
;
1005 icoeff
[idx
] = icoefficients
[idx
].coefficient
;
1008 if (ocoefficients
[idx
].variable
!= 0)
1010 if (! oiv_used
[idx
])
1012 oiv_used
[idx
] = ocoefficients
[idx
].variable
;
1013 ocoeff
[idx
] = ocoefficients
[idx
].coefficient
;
1018 for (idx
= 1; idx
<= count
; idx
++)
1020 if (iiv_used
[idx
] == 0 && oiv_used
[idx
] == 0)
1021 complexity
[idx
] = ziv
;
1022 else if (iiv_used
[idx
] == oiv_used
[idx
])
1024 if (icoeff
[idx
] == ocoeff
[idx
])
1025 complexity
[idx
] = strong_siv
;
1026 else if (icoeff
[idx
] == -1 * ocoeff
[idx
])
1027 complexity
[idx
] = weak_crossing_siv
;
1029 complexity
[idx
] = weak_siv
;
1031 else if (icoeff
[idx
] == 0 || ocoeff
[idx
] == 0)
1032 complexity
[idx
] = weak_zero_siv
;
1033 else complexity
[idx
] = miv
;
1037 for (idx
= 1; idx
<= count
; idx
++)
1039 for (cidx
= 1; cidx
<= count
; cidx
++)
1042 && iiv_used
[idx
] && oiv_used
[cidx
]
1043 && iiv_used
[idx
] == oiv_used
[cidx
])
1049 /* Determine the DIRECTION and DISTANCE dependency for subscript SUB of
1050 inputs ICOEFFICIENTS and outputs OCOEFFICIENTS of loop LOOP_PTR using
1051 the zero induction variable test */
1054 ziv_test (icoefficients
, ocoefficients
, direction
, distance
, loop_ptr
, sub
)
1055 subscript icoefficients
[MAX_SUBSCRIPTS
];
1056 subscript ocoefficients
[MAX_SUBSCRIPTS
];
1057 enum direction_type direction
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1058 int distance
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
] ATTRIBUTE_UNUSED
;
1062 if (ocoefficients
[sub
].offset
!=
1063 icoefficients
[sub
].offset
)
1064 direction
[loop_ptr
->depth
][sub
] = independent
;
1067 /* Determine the DIRECTION and DISTANCE dependency for subscript SUB of
1068 inputs ICOEFFICIENTS and outputs OCOEFFICIENTS of loop LOOP_PTR using
1069 the single induction variable test */
1072 siv_test (icoefficients
, ocoefficients
, direction
, distance
, loop_ptr
, sub
)
1073 subscript icoefficients
[MAX_SUBSCRIPTS
];
1074 subscript ocoefficients
[MAX_SUBSCRIPTS
];
1075 enum direction_type direction
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1076 int distance
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1084 if (! check_subscript_induction (&icoefficients
[sub
], &ocoefficients
[sub
],
1088 coef_diff
= icoefficients
[sub
].offset
- ocoefficients
[sub
].offset
;
1089 /* strong_siv requires equal coefficients. weak_crossing_siv requires
1090 coefficients to have equal absolute value. weak_zero_siv uses the
1091 nonzero coefficient. */
1093 if (ocoefficients
[sub
].coefficient
== INT_MIN
)
1094 coef
= icoefficients
[sub
].coefficient
;
1095 else if (icoefficients
[sub
].coefficient
== INT_MIN
)
1096 coef
= ocoefficients
[sub
].coefficient
;
1097 else if (ocoefficients
[sub
].coefficient
==
1098 -1 * icoefficients
[sub
].coefficient
)
1099 coef
= 2 * abs (ocoefficients
[sub
].coefficient
);
1101 coef
= icoefficients
[sub
].coefficient
;
1103 gcd
= -coef_diff
/ coef
;
1104 if (gcd
* coef
!= -coef_diff
)
1106 direction
[loop_ptr
->depth
][sub
] = independent
;
1110 distance
[loop_ptr
->depth
][sub
] = gcd
;
1112 direction
[loop_ptr
->depth
][sub
] = gt
;
1114 direction
[loop_ptr
->depth
][sub
] = lt
;
1116 direction
[loop_ptr
->depth
][sub
] = eq
;
1120 /* Return 1 if an induction variable of LOOP_PTR is used by either
1121 input ICOEFFICIENT or output OCOEFFICIENT */
1124 check_subscript_induction (icoefficient
, ocoefficient
, loop_ptr
)
1125 subscript
*icoefficient
;
1126 subscript
*ocoefficient
;
1130 int sub_ind_input
= 0;
1131 int sub_ind_output
= 0;
1133 for (ind_ptr
= loop_ptr
->ind
; ind_ptr
; ind_ptr
= ind_ptr
->next
)
1135 if (icoefficient
->variable
== ind_ptr
->variable
)
1137 if (ocoefficient
->variable
== ind_ptr
->variable
)
1140 if (sub_ind_input
|| sub_ind_output
)
1146 #define abs(N) ((N) < 0 ? -(N) : (N))
1148 /* Determine the DIRECTION and DISTANCE dependency for subscript SUB of
1149 inputs ICOEFFICIENTS and outputs OCOEFFICIENTS of loop LOOP_PTR using
1150 the greatest common denominator test */
1153 gcd_test (icoefficients
, ocoefficients
, direction
, distance
, loop_ptr
, sub
)
1154 subscript icoefficients
[MAX_SUBSCRIPTS
];
1155 subscript ocoefficients
[MAX_SUBSCRIPTS
];
1156 enum direction_type direction
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1157 int distance
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
] ATTRIBUTE_UNUSED
;
1164 if (! check_subscript_induction (&icoefficients
[sub
], &ocoefficients
[sub
],
1168 g
= find_gcd (icoefficients
[sub
].coefficient
,
1169 ocoefficients
[sub
].coefficient
);
1172 coef_diff
= icoefficients
[sub
].offset
- ocoefficients
[sub
].offset
;
1174 if (gg
* g
!= coef_diff
)
1176 direction
[loop_ptr
->depth
][sub
] = independent
;
1179 /* ?? gcd does not yield direction and distance. Wolfe's direction
1180 vector hierarchy can be used to give this. */
1183 /* Find the gcd of X and Y using Euclid's algorithm */
1215 /* Merge SUBSCRIPT_COUNT DIRECTIONs and DISTANCEs for LOOP_COUNT loops.
1216 We use a predefined array to handle the direction merge.
1217 The distance merge makes use of the fact that distances default to
1218 INT_MAX. Distances are '&' together. Watch out for a negative distance.
1222 merge_dependencies (direction
, distance
, loop_count
, subscript_count
)
1223 enum direction_type direction
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1224 int distance
[MAX_SUBSCRIPTS
][MAX_SUBSCRIPTS
];
1226 int subscript_count
;
1231 static const enum direction_type direction_merge
[8][8] =
1232 {{lt
, le
, le
, star
, star
, lt
, independent
, lt
},
1233 {le
, le
, le
, star
, star
, le
, independent
, le
},
1234 {le
, le
, eq
, ge
, ge
, eq
, independent
, eq
},
1235 {star
, star
, ge
, gt
, ge
, gt
, independent
, ge
},
1236 {star
, star
, ge
, ge
, ge
, ge
, independent
, ge
},
1237 {lt
, le
, eq
, gt
, ge
, star
, independent
, star
},
1238 {independent
, independent
, independent
, independent
, independent
},
1239 {independent
, independent
, independent
}
1242 for (i
= 1; i
<= loop_count
; i
++)
1244 distance
[i
][0] = INT_MAX
;
1245 direction
[i
][0] = star
;
1247 for (j
= 1; j
<= subscript_count
; j
++)
1249 if (distance
[i
][j
] < 0)
1251 distance
[i
][0] = distance
[i
][0] & abs (distance
[i
][j
]);
1255 distance
[i
][0] = distance
[i
][0] & distance
[i
][j
];
1256 direction
[i
][0] = direction_merge
[(int)direction
[i
][0]]
1257 [(int)direction
[i
][j
]];
1259 distance
[i
][0] = sign
* distance
[i
][0];
1263 /* Dump ARRAY_REF NODE. */
1266 dump_array_ref (node
)
1269 enum tree_code tree_op
= TREE_CODE (node
);
1271 if (tree_op
== VAR_DECL
)
1273 printf ("%s", IDENTIFIER_POINTER (DECL_NAME (node
)));
1275 else if (tree_op
== INTEGER_CST
)
1277 printf ("%d", (int)TREE_INT_CST_LOW (node
));
1279 else if (tree_op
== PLUS_EXPR
)
1281 dump_array_ref (TREE_OPERAND (node
, 0));
1283 dump_array_ref (TREE_OPERAND (node
, 1));
1285 else if (tree_op
== MINUS_EXPR
)
1287 dump_array_ref (TREE_OPERAND (node
, 0));
1289 dump_array_ref (TREE_OPERAND (node
, 1));
1291 else if (tree_op
== MULT_EXPR
)
1293 dump_array_ref (TREE_OPERAND (node
, 0));
1295 dump_array_ref (TREE_OPERAND (node
, 1));
1299 /* Dump def/use DU. */
1303 dump_one_node (du
, seen
)
1308 dependence
*dep_ptr
;
1311 for (du_ptr
= du
; du_ptr
; du_ptr
= du_ptr
->next
)
1313 printf ("%s ", du_ptr
->variable
);
1314 for (array_ref
= du_ptr
->expression
;
1315 TREE_CODE (array_ref
) == ARRAY_REF
;
1316 array_ref
= TREE_OPERAND (array_ref
, 0))
1319 dump_array_ref (TREE_OPERAND (array_ref
, 1));
1323 printf (" Outer Loop %x Containing Loop %x Expression %x %s\n",
1324 (int)du_ptr
->outer_loop
,
1325 (int)du_ptr
->containing_loop
,
1326 (int)du_ptr
->expression
, du_ptr
->type
== def
? "Def" : "Use");
1327 VARRAY_PUSH_GENERIC_PTR (*seen
, du_ptr
);
1329 for (dep_ptr
= du_ptr
->dep
; dep_ptr
; dep_ptr
= dep_ptr
->next
)
1332 printf ("%s Dependence with %x ",
1333 dependence_string
[(int)dep_ptr
->dependence
],
1334 (int)dep_ptr
->source
);
1335 printf ("Dir/Dist ");
1336 for (i
= 1 ; i
< MAX_SUBSCRIPTS
; i
++)
1337 if (dep_ptr
->direction
[i
] != undef
)
1338 printf ("[%d] %s/%d ", i
,
1339 direction_string
[(int)dep_ptr
->direction
[i
]],
1340 dep_ptr
->distance
[i
]);
1346 /* Dump dependence info. */
1349 dump_node_dependence (void)
1352 unsigned int du_idx
, seen_idx
, i
;
1355 VARRAY_GENERIC_PTR_INIT (seen
, 20, "seen");
1358 for (du_ptr
= VARRAY_GENERIC_PTR (def_use_chain
, du_idx
);
1359 du_idx
< VARRAY_SIZE (def_use_chain
);
1360 du_ptr
= VARRAY_GENERIC_PTR (def_use_chain
, du_idx
++))
1362 for (i
= 0; i
< VARRAY_SIZE (seen
) && VARRAY_GENERIC_PTR (seen
, i
)
1364 if (i
>= VARRAY_SIZE (seen
))
1365 dump_one_node (du_ptr
, &seen
);
1371 /* Return the index into 'dep_chain' if there is a dependency for destination
1372 dest_to_remember (set by remember_dest_for_dependence) and source node.
1373 Called by the front end, which adds the index onto a MEM rtx. */
1376 search_dependence (node
)
1379 dependence
*dep_ptr
;
1385 if (TREE_CODE (node
) == INDIRECT_REF
&& TREE_OPERAND (node
, 1)
1386 && TREE_CODE (TREE_OPERAND (node
, 1)) == ARRAY_REF
)
1387 node
= TREE_OPERAND (node
, 1);
1389 for (dep_ptr
= VARRAY_GENERIC_PTR (dep_chain
, 0);
1390 dep_ptr
; dep_ptr
= VARRAY_GENERIC_PTR (dep_chain
, dep_idx
++))
1392 if ((node
== dep_ptr
->source
1393 && dest_to_remember
== dep_ptr
->destination
)
1394 || (! dep_ptr
->source
&& node
== dep_ptr
->destination
))
1402 /* Remember a destination NODE for search_dependence. */
1405 remember_dest_for_dependence (node
)
1410 if (TREE_CODE (node
) == INDIRECT_REF
&& TREE_OPERAND (node
, 1)
1411 && TREE_CODE (TREE_OPERAND (node
, 1)) == ARRAY_REF
)
1412 node
= TREE_OPERAND (node
, 1);
1413 dest_to_remember
= node
;
1417 #ifndef MEM_DEPENDENCY
1418 #define MEM_DEPENDENCY(RTX) XCWINT (RTX, 2, MEM)
1421 /* Return 1 along with the dependence DIRECTION and DISTANCE if there is a
1422 dependence from dest_rtx to src_rtx. */
1425 have_dependence_p (dest_rtx
, src_rtx
, direction
, distance
)
1428 enum direction_type direction
[MAX_SUBSCRIPTS
];
1429 int distance
[MAX_SUBSCRIPTS
];
1431 int dest_idx
= 0, src_idx
= 0;
1433 dependence
*dep_ptr
;
1435 if (GET_CODE (SET_DEST (PATTERN (dest_rtx
))) == MEM
)
1437 dest
= SET_DEST (PATTERN (dest_rtx
));
1438 dest_idx
= MEM_DEPENDENCY (dest
) - 1;
1440 if (GET_CODE (SET_SRC (PATTERN (src_rtx
))) == MEM
)
1442 src
= SET_SRC (PATTERN (src_rtx
));
1443 src_idx
= MEM_DEPENDENCY (src
) - 1;
1445 if (dest_idx
>= 0 || src_idx
>= 0)
1448 for (dep_ptr
= VARRAY_GENERIC_PTR (dep_chain
, -dest_idx
);
1449 dep_ptr
; dep_ptr
= dep_ptr
->next
)
1451 if (dep_ptr
== VARRAY_GENERIC_PTR (dep_chain
, -src_idx
))
1453 direction
= (enum direction_type
*) &dep_ptr
->direction
;
1454 distance
= (int*) &dep_ptr
->distance
;
1461 /* Cleanup when dependency analysis is complete. */
1464 end_dependence_analysis ()
1466 VARRAY_FREE (dep_chain
);