1 Index: tree-loop-linear.c
2 ===================================================================
3 *** tree-loop-linear.c (revision 113884)
4 --- tree-loop-linear.c (working copy)
7 /* Linear Loop transforms
8 ! Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
9 Contributed by Daniel Berlin <dberlin@dberlin.org>.
11 This file is part of GCC.
13 /* Linear Loop transforms
14 ! Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
15 Contributed by Daniel Berlin <dberlin@dberlin.org>.
17 This file is part of GCC.
18 *************** try_interchange_loops (lambda_trans_matr
22 linear_transform_loops (struct loops *loops)
24 + bool modified = false;
26 VEC(tree,heap) *oldivs = NULL;
27 VEC(tree,heap) *invariants = NULL;
28 *************** linear_transform_loops (struct loops *lo
30 lambda_loopnest before, after;
31 lambda_trans_matrix trans;
33 - bool need_perfect_nest = false;
34 /* If it's not a loop nest, we don't want it.
35 We also don't handle sibling loops properly,
36 which are loops of the following form:
38 *************** linear_transform_loops (struct loops *lo
40 fprintf (dump_file, "Can't transform loop, transform is illegal:\n");
43 ! if (!perfect_nest_p (loop_nest))
44 ! need_perfect_nest = true;
45 ! before = gcc_loopnest_to_lambda_loopnest (loops,
53 fprintf (dump_file, "Can't transform loop, transform is illegal:\n");
57 ! before = gcc_loopnest_to_lambda_loopnest (loops, loop_nest, &oldivs,
62 *************** linear_transform_loops (struct loops *lo
66 lambda_loopnest_to_gcc_loopnest (loop_nest, oldivs, invariants,
71 fprintf (dump_file, "Successfully transformed loop.\n");
72 free_dependence_relations (dependence_relations);
73 *************** linear_transform_loops (struct loops *lo
75 VEC_free (tree, heap, oldivs);
76 VEC_free (tree, heap, invariants);
78 ! rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_full_phi);
81 VEC_free (tree, heap, oldivs);
82 VEC_free (tree, heap, invariants);
86 ! rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa_full_phi);
89 ===================================================================
90 *** lambda.h (revision 113884)
91 --- lambda.h (working copy)
94 /* Lambda matrix and vector interface.
95 ! Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
96 Contributed by Daniel Berlin <dberlin@dberlin.org>
98 This file is part of GCC.
100 /* Lambda matrix and vector interface.
101 ! Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
102 Contributed by Daniel Berlin <dberlin@dberlin.org>
104 This file is part of GCC.
105 *************** void print_lambda_body_vector (FILE *, l
107 lambda_loopnest gcc_loopnest_to_lambda_loopnest (struct loops *,
112 void lambda_loopnest_to_gcc_loopnest (struct loop *,
113 VEC(tree,heap) *, VEC(tree,heap) *,
114 lambda_loopnest, lambda_trans_matrix);
116 lambda_loopnest gcc_loopnest_to_lambda_loopnest (struct loops *,
119 ! VEC(tree,heap) **);
120 void lambda_loopnest_to_gcc_loopnest (struct loop *,
121 VEC(tree,heap) *, VEC(tree,heap) *,
122 lambda_loopnest, lambda_trans_matrix);
124 ===================================================================
125 *** lambda-code.c (revision 113884)
126 --- lambda-code.c (working copy)
129 /* Loop transformation code generation
130 ! Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
131 Contributed by Daniel Berlin <dberlin@dberlin.org>
133 This file is part of GCC.
135 /* Loop transformation code generation
136 ! Copyright (C) 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
137 Contributed by Daniel Berlin <dberlin@dberlin.org>
139 This file is part of GCC.
140 *************** static lambda_lattice lambda_lattice_new
143 static lambda_lattice lambda_lattice_compute_base (lambda_loopnest);
145 static tree find_induction_var_from_exit_cond (struct loop *);
146 + static bool can_convert_to_perfect_nest (struct loop *);
148 /* Create a new lambda body vector. */
150 *************** DEF_VEC_ALLOC_P(lambda_loop,heap);
154 gcc_loopnest_to_lambda_loopnest (struct loops *currloops,
155 ! struct loop * loop_nest,
156 VEC(tree,heap) **inductionvars,
157 ! VEC(tree,heap) **invariants,
158 ! bool need_perfect_nest)
160 lambda_loopnest ret = NULL;
164 VEC(lambda_loop,heap) *loops = NULL;
165 VEC(tree,heap) *uboundvars = NULL;
169 gcc_loopnest_to_lambda_loopnest (struct loops *currloops,
170 ! struct loop *loop_nest,
171 VEC(tree,heap) **inductionvars,
172 ! VEC(tree,heap) **invariants)
174 lambda_loopnest ret = NULL;
175 ! struct loop *temp = loop_nest;
176 ! int depth = depth_of_nest (loop_nest);
178 VEC(lambda_loop,heap) *loops = NULL;
179 VEC(tree,heap) *uboundvars = NULL;
180 *************** gcc_loopnest_to_lambda_loopnest (struct
182 VEC(int,heap) *steps = NULL;
184 tree inductionvar = NULL;
186 ! depth = depth_of_nest (loop_nest);
190 newloop = gcc_loop_to_lambda_loop (temp, depth, invariants,
192 VEC(int,heap) *steps = NULL;
194 tree inductionvar = NULL;
195 ! bool perfect_nest = perfect_nest_p (loop_nest);
197 ! if (!perfect_nest && !can_convert_to_perfect_nest (loop_nest))
202 newloop = gcc_loop_to_lambda_loop (temp, depth, invariants,
203 *************** gcc_loopnest_to_lambda_loopnest (struct
205 &lboundvars, &uboundvars,
209 VEC_safe_push (tree, heap, *inductionvars, inductionvar);
210 VEC_safe_push (lambda_loop, heap, loops, newloop);
213 ! if (need_perfect_nest)
215 if (!perfect_nestify (currloops, loop_nest,
216 lboundvars, uboundvars, steps, *inductionvars))
218 &lboundvars, &uboundvars,
223 VEC_safe_push (tree, heap, *inductionvars, inductionvar);
224 VEC_safe_push (lambda_loop, heap, loops, newloop);
230 if (!perfect_nestify (currloops, loop_nest,
231 lboundvars, uboundvars, steps, *inductionvars))
232 *************** gcc_loopnest_to_lambda_loopnest (struct
236 "Successfully converted loop nest to perfect loop nest.\n");
239 ret = lambda_loopnest_new (depth, 2 * depth);
241 for (i = 0; VEC_iterate (lambda_loop, loops, i, newloop); i++)
242 LN_LOOPS (ret)[i] = newloop;
245 VEC_free (lambda_loop, heap, loops);
246 VEC_free (tree, heap, uboundvars);
247 *************** replace_uses_equiv_to_x_with_y (struct l
250 tree use = USE_FROM_PTR (use_p);
251 tree step = NULL_TREE;
252 ! tree access_fn = NULL_TREE;
255 ! access_fn = instantiate_parameters
256 ! (loop, analyze_scalar_evolution (loop, use));
257 ! if (access_fn != NULL_TREE && access_fn != chrec_dont_know)
258 ! step = evolution_part_in_loop_num (access_fn, loop->num);
259 if ((step && step != chrec_dont_know
260 && TREE_CODE (step) == INTEGER_CST
261 && int_cst_value (step) == xstep)
264 tree use = USE_FROM_PTR (use_p);
265 tree step = NULL_TREE;
266 ! tree scev = instantiate_parameters (loop,
267 ! analyze_scalar_evolution (loop, use));
269 ! if (scev != NULL_TREE && scev != chrec_dont_know)
270 ! step = evolution_part_in_loop_num (scev, loop->num);
272 if ((step && step != chrec_dont_know
273 && TREE_CODE (step) == INTEGER_CST
274 && int_cst_value (step) == xstep)
275 *************** replace_uses_equiv_to_x_with_y (struct l
280 - /* Return TRUE if STMT uses tree OP in it's uses. */
283 - stmt_uses_op (tree stmt, tree op)
288 - FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_USE)
296 /* Return true if STMT is an exit PHI for LOOP */
300 *************** can_put_in_inner_loop (struct loop *inne
306 ! /* Return TRUE if LOOP is an imperfect nest that we can convert to a perfect
307 ! one. LOOPIVS is a vector of induction variables, one per loop.
308 ! ATM, we only handle imperfect nests of depth 2, where all of the statements
309 ! occur after the inner loop. */
312 ! can_convert_to_perfect_nest (struct loop *loop,
313 ! VEC(tree,heap) *loopivs)
316 tree exit_condition, phi;
321 + /* Return true if STMT can be put *after* the inner loop of LOOP. */
324 + can_put_after_inner_loop (struct loop *loop, tree stmt)
326 + imm_use_iterator imm_iter;
327 + use_operand_p use_p;
329 + if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
332 + FOR_EACH_IMM_USE_FAST (use_p, imm_iter, TREE_OPERAND (stmt, 0))
334 + if (!exit_phi_for_loop_p (loop, USE_STMT (use_p)))
336 + basic_block immbb = bb_for_stmt (USE_STMT (use_p));
338 + if (!dominated_by_p (CDI_DOMINATORS,
340 + loop->inner->header)
341 + && !can_put_in_inner_loop (loop->inner, stmt))
348 ! /* Return TRUE if LOOP is an imperfect nest that we can convert to a
349 ! perfect one. At the moment, we only handle imperfect nests of
350 ! depth 2, where all of the statements occur after the inner loop. */
353 ! can_convert_to_perfect_nest (struct loop *loop)
356 tree exit_condition, phi;
357 *************** can_convert_to_perfect_nest (struct loop
360 for (bsi = bsi_start (bbs[i]); !bsi_end_p (bsi); bsi_next (&bsi))
363 tree stmt = bsi_stmt (bsi);
366 if (stmt == exit_condition
367 || not_interesting_stmt (stmt)
368 || stmt_is_bumper_for_loop (loop, stmt))
370 ! /* If the statement uses inner loop ivs, we == screwed. */
371 ! for (j = 1; VEC_iterate (tree, loopivs, j, iv); j++)
372 ! if (stmt_uses_op (stmt, iv))
375 /* If this is a simple operation like a cast that is invariant
376 in the inner loop, only used there, and we can place it
377 there, then it's not going to hurt us.
380 for (bsi = bsi_start (bbs[i]); !bsi_end_p (bsi); bsi_next (&bsi))
382 tree stmt = bsi_stmt (bsi);
384 if (stmt == exit_condition
385 || not_interesting_stmt (stmt)
386 || stmt_is_bumper_for_loop (loop, stmt))
389 /* If this is a simple operation like a cast that is invariant
390 in the inner loop, only used there, and we can place it
391 there, then it's not going to hurt us.
392 *************** can_convert_to_perfect_nest (struct loop
394 theory that we are going to gain a lot more by interchanging
395 the loop than we are by leaving some invariant code there for
396 some other pass to clean up. */
397 ! if (TREE_CODE (stmt) == MODIFY_EXPR
398 ! && is_gimple_cast (TREE_OPERAND (stmt, 1))
399 ! && can_put_in_inner_loop (loop->inner, stmt))
402 /* Otherwise, if the bb of a statement we care about isn't
403 dominated by the header of the inner loop, then we can't
405 theory that we are going to gain a lot more by interchanging
406 the loop than we are by leaving some invariant code there for
407 some other pass to clean up. */
408 ! if (TREE_CODE (stmt) == MODIFY_EXPR)
410 ! use_operand_p use_a, use_b;
411 ! imm_use_iterator imm_iter;
412 ! ssa_op_iter op_iter, op_iter1;
413 ! tree op0 = TREE_OPERAND (stmt, 0);
414 ! tree scev = instantiate_parameters
415 ! (loop, analyze_scalar_evolution (loop, op0));
417 ! /* If the IV is simple, it can be duplicated. */
418 ! if (!automatically_generated_chrec_p (scev))
420 ! tree step = evolution_part_in_loop_num (scev, loop->num);
421 ! if (step && step != chrec_dont_know
422 ! && TREE_CODE (step) == INTEGER_CST)
426 ! /* The statement should not define a variable used
427 ! in the inner loop. */
428 ! if (TREE_CODE (op0) == SSA_NAME)
429 ! FOR_EACH_IMM_USE_FAST (use_a, imm_iter, op0)
430 ! if (bb_for_stmt (USE_STMT (use_a))->loop_father
434 ! FOR_EACH_SSA_USE_OPERAND (use_a, stmt, op_iter, SSA_OP_USE)
436 ! tree node, op = USE_FROM_PTR (use_a);
438 ! /* The variables should not be used in both loops. */
439 ! FOR_EACH_IMM_USE_FAST (use_b, imm_iter, op)
440 ! if (bb_for_stmt (USE_STMT (use_b))->loop_father
444 ! /* The statement should not use the value of a
445 ! scalar that was modified in the loop. */
446 ! node = SSA_NAME_DEF_STMT (op);
447 ! if (TREE_CODE (node) == PHI_NODE)
448 ! FOR_EACH_PHI_ARG (use_b, node, op_iter1, SSA_OP_USE)
450 ! tree arg = USE_FROM_PTR (use_b);
452 ! if (TREE_CODE (arg) == SSA_NAME)
454 ! tree arg_stmt = SSA_NAME_DEF_STMT (arg);
456 ! if (bb_for_stmt (arg_stmt)->loop_father
463 ! if (can_put_in_inner_loop (loop->inner, stmt)
464 ! || can_put_after_inner_loop (loop, stmt))
468 /* Otherwise, if the bb of a statement we care about isn't
469 dominated by the header of the inner loop, then we can't
470 *************** perfect_nestify (struct loops *loops,
473 tree oldivvar, ivvar, ivvarinced;
474 VEC(tree,heap) *phis = NULL;
476 ! if (!can_convert_to_perfect_nest (loop, loopivs))
479 ! /* Create the new loop */
481 olddest = loop->single_exit->dest;
482 ! preheaderbb = loop_split_edge_with (loop->single_exit, NULL);
483 headerbb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
485 /* Push the exit phi nodes that we are moving. */
488 tree oldivvar, ivvar, ivvarinced;
489 VEC(tree,heap) *phis = NULL;
491 ! /* Create the new loop. */
492 olddest = loop->single_exit->dest;
493 ! preheaderbb = loop_split_edge_with (loop->single_exit, NULL);
494 headerbb = create_empty_bb (EXIT_BLOCK_PTR->prev_bb);
496 /* Push the exit phi nodes that we are moving. */
497 *************** perfect_nestify (struct loops *loops,
502 ! replace_uses_equiv_to_x_with_y (loop, stmt,
504 ! VEC_index (int, steps, 0),
506 bsi_move_before (&bsi, &tobsi);
508 /* If the statement has any virtual operands, they may
513 ! replace_uses_equiv_to_x_with_y
514 ! (loop, stmt, oldivvar, VEC_index (int, steps, 0), ivvar);
516 bsi_move_before (&bsi, &tobsi);
518 /* If the statement has any virtual operands, they may