Daily bump.
[official-gcc.git] / gcc / tree-ssa-loop-im.c
blob6402a8e62443527c14e7909e93338929a017a1a4
1 /* Loop invariant motion.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
9 later version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "hard-reg-set.h"
28 #include "basic-block.h"
29 #include "output.h"
30 #include "diagnostic.h"
31 #include "tree-flow.h"
32 #include "tree-dump.h"
33 #include "timevar.h"
34 #include "cfgloop.h"
35 #include "domwalk.h"
36 #include "params.h"
37 #include "tree-pass.h"
38 #include "flags.h"
39 #include "real.h"
40 #include "hashtab.h"
41 #include "tree-affine.h"
42 #include "pointer-set.h"
44 /* TODO: Support for predicated code motion. I.e.
46 while (1)
48 if (cond)
50 a = inv;
51 something;
55 Where COND and INV are is invariants, but evaluating INV may trap or be
56 invalid from some other reason if !COND. This may be transformed to
58 if (cond)
59 a = inv;
60 while (1)
62 if (cond)
63 something;
64 } */
66 /* A type for the list of statements that have to be moved in order to be able
67 to hoist an invariant computation. */
69 struct depend
71 tree stmt;
72 struct depend *next;
75 /* The auxiliary data kept for each statement. */
77 struct lim_aux_data
79 struct loop *max_loop; /* The outermost loop in that the statement
80 is invariant. */
82 struct loop *tgt_loop; /* The loop out of that we want to move the
83 invariant. */
85 struct loop *always_executed_in;
86 /* The outermost loop for that we are sure
87 the statement is executed if the loop
88 is entered. */
90 unsigned cost; /* Cost of the computation performed by the
91 statement. */
93 struct depend *depends; /* List of statements that must be also hoisted
94 out of the loop when this statement is
95 hoisted; i.e. those that define the operands
96 of the statement and are inside of the
97 MAX_LOOP loop. */
100 #define LIM_DATA(STMT) (TREE_CODE (STMT) == PHI_NODE \
101 ? NULL \
102 : (struct lim_aux_data *) (stmt_ann (STMT)->common.aux))
104 /* Description of a memory reference location. */
106 typedef struct mem_ref_loc
108 tree *ref; /* The reference itself. */
109 tree stmt; /* The statement in that it occurs. */
110 } *mem_ref_loc_p;
112 DEF_VEC_P(mem_ref_loc_p);
113 DEF_VEC_ALLOC_P(mem_ref_loc_p, heap);
115 /* The list of memory reference locations in a loop. */
117 typedef struct mem_ref_locs
119 VEC (mem_ref_loc_p, heap) *locs;
120 } *mem_ref_locs_p;
122 DEF_VEC_P(mem_ref_locs_p);
123 DEF_VEC_ALLOC_P(mem_ref_locs_p, heap);
125 /* Description of a memory reference. */
127 typedef struct mem_ref
129 tree mem; /* The memory itself. */
130 unsigned id; /* ID assigned to the memory reference
131 (its index in memory_accesses.refs_list) */
132 hashval_t hash; /* Its hash value. */
133 bitmap stored; /* The set of loops in that this memory locatio
134 is stored to. */
135 VEC (mem_ref_locs_p, heap) *accesses_in_loop;
136 /* The locations of the accesses. Vector
137 indexed by the loop number. */
138 bitmap vops; /* Vops corresponding to this memory
139 location. */
141 /* The following sets are computed on demand. We keep both set and
142 its complement, so that we know whether the information was
143 already computed or not. */
144 bitmap indep_loop; /* The set of loops in that the memory
145 reference is independent, meaning:
146 If it is stored in the loop, this store
147 is independent on all other loads and
148 stores.
149 If it is only loaded, then it is independent
150 on all stores in the loop. */
151 bitmap dep_loop; /* The complement of INDEP_LOOP. */
153 bitmap indep_ref; /* The set of memory references on that
154 this reference is independent. */
155 bitmap dep_ref; /* The complement of DEP_REF. */
156 } *mem_ref_p;
158 DEF_VEC_P(mem_ref_p);
159 DEF_VEC_ALLOC_P(mem_ref_p, heap);
161 DEF_VEC_P(bitmap);
162 DEF_VEC_ALLOC_P(bitmap, heap);
164 DEF_VEC_P(htab_t);
165 DEF_VEC_ALLOC_P(htab_t, heap);
167 /* Description of memory accesses in loops. */
169 static struct
171 /* The hash table of memory references accessed in loops. */
172 htab_t refs;
174 /* The list of memory references. */
175 VEC (mem_ref_p, heap) *refs_list;
177 /* The set of memory references accessed in each loop. */
178 VEC (bitmap, heap) *refs_in_loop;
180 /* The set of memory references accessed in each loop, including
181 subloops. */
182 VEC (bitmap, heap) *all_refs_in_loop;
184 /* The set of virtual operands clobbered in a given loop. */
185 VEC (bitmap, heap) *clobbered_vops;
187 /* Map from the pair (loop, virtual operand) to the set of refs that
188 touch the virtual operand in the loop. */
189 VEC (htab_t, heap) *vop_ref_map;
191 /* Cache for expanding memory addresses. */
192 struct pointer_map_t *ttae_cache;
193 } memory_accesses;
195 static bool ref_indep_loop_p (struct loop *, mem_ref_p);
197 /* Minimum cost of an expensive expression. */
198 #define LIM_EXPENSIVE ((unsigned) PARAM_VALUE (PARAM_LIM_EXPENSIVE))
200 /* The outermost loop for that execution of the header guarantees that the
201 block will be executed. */
202 #define ALWAYS_EXECUTED_IN(BB) ((struct loop *) (BB)->aux)
204 /* Calls CBCK for each index in memory reference ADDR_P. There are two
205 kinds situations handled; in each of these cases, the memory reference
206 and DATA are passed to the callback:
208 Access to an array: ARRAY_{RANGE_}REF (base, index). In this case we also
209 pass the pointer to the index to the callback.
211 Pointer dereference: INDIRECT_REF (addr). In this case we also pass the
212 pointer to addr to the callback.
214 If the callback returns false, the whole search stops and false is returned.
215 Otherwise the function returns true after traversing through the whole
216 reference *ADDR_P. */
218 bool
219 for_each_index (tree *addr_p, bool (*cbck) (tree, tree *, void *), void *data)
221 tree *nxt, *idx;
223 for (; ; addr_p = nxt)
225 switch (TREE_CODE (*addr_p))
227 case SSA_NAME:
228 return cbck (*addr_p, addr_p, data);
230 case MISALIGNED_INDIRECT_REF:
231 case ALIGN_INDIRECT_REF:
232 case INDIRECT_REF:
233 nxt = &TREE_OPERAND (*addr_p, 0);
234 return cbck (*addr_p, nxt, data);
236 case BIT_FIELD_REF:
237 case VIEW_CONVERT_EXPR:
238 case REALPART_EXPR:
239 case IMAGPART_EXPR:
240 nxt = &TREE_OPERAND (*addr_p, 0);
241 break;
243 case COMPONENT_REF:
244 /* If the component has varying offset, it behaves like index
245 as well. */
246 idx = &TREE_OPERAND (*addr_p, 2);
247 if (*idx
248 && !cbck (*addr_p, idx, data))
249 return false;
251 nxt = &TREE_OPERAND (*addr_p, 0);
252 break;
254 case ARRAY_REF:
255 case ARRAY_RANGE_REF:
256 nxt = &TREE_OPERAND (*addr_p, 0);
257 if (!cbck (*addr_p, &TREE_OPERAND (*addr_p, 1), data))
258 return false;
259 break;
261 case VAR_DECL:
262 case PARM_DECL:
263 case STRING_CST:
264 case RESULT_DECL:
265 case VECTOR_CST:
266 case COMPLEX_CST:
267 case INTEGER_CST:
268 case REAL_CST:
269 case FIXED_CST:
270 case CONSTRUCTOR:
271 return true;
273 case ADDR_EXPR:
274 gcc_assert (is_gimple_min_invariant (*addr_p));
275 return true;
277 case TARGET_MEM_REF:
278 idx = &TMR_BASE (*addr_p);
279 if (*idx
280 && !cbck (*addr_p, idx, data))
281 return false;
282 idx = &TMR_INDEX (*addr_p);
283 if (*idx
284 && !cbck (*addr_p, idx, data))
285 return false;
286 return true;
288 default:
289 gcc_unreachable ();
294 /* If it is possible to hoist the statement STMT unconditionally,
295 returns MOVE_POSSIBLE.
296 If it is possible to hoist the statement STMT, but we must avoid making
297 it executed if it would not be executed in the original program (e.g.
298 because it may trap), return MOVE_PRESERVE_EXECUTION.
299 Otherwise return MOVE_IMPOSSIBLE. */
301 enum move_pos
302 movement_possibility (tree stmt)
304 tree lhs, rhs;
306 if (flag_unswitch_loops
307 && TREE_CODE (stmt) == COND_EXPR)
309 /* If we perform unswitching, force the operands of the invariant
310 condition to be moved out of the loop. */
311 return MOVE_POSSIBLE;
314 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
315 return MOVE_IMPOSSIBLE;
317 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_DEFS))
318 return MOVE_IMPOSSIBLE;
320 if (stmt_ends_bb_p (stmt))
321 return MOVE_IMPOSSIBLE;
323 if (stmt_ann (stmt)->has_volatile_ops)
324 return MOVE_IMPOSSIBLE;
326 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
327 if (TREE_CODE (lhs) == SSA_NAME
328 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
329 return MOVE_IMPOSSIBLE;
331 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
333 if (TREE_SIDE_EFFECTS (rhs)
334 || tree_could_throw_p (rhs))
335 return MOVE_IMPOSSIBLE;
337 if (TREE_CODE (lhs) != SSA_NAME
338 || tree_could_trap_p (rhs))
339 return MOVE_PRESERVE_EXECUTION;
341 if (get_call_expr_in (stmt))
343 /* While pure or const call is guaranteed to have no side effects, we
344 cannot move it arbitrarily. Consider code like
346 char *s = something ();
348 while (1)
350 if (s)
351 t = strlen (s);
352 else
353 t = 0;
356 Here the strlen call cannot be moved out of the loop, even though
357 s is invariant. In addition to possibly creating a call with
358 invalid arguments, moving out a function call that is not executed
359 may cause performance regressions in case the call is costly and
360 not executed at all. */
361 return MOVE_PRESERVE_EXECUTION;
363 return MOVE_POSSIBLE;
366 /* Suppose that operand DEF is used inside the LOOP. Returns the outermost
367 loop to that we could move the expression using DEF if it did not have
368 other operands, i.e. the outermost loop enclosing LOOP in that the value
369 of DEF is invariant. */
371 static struct loop *
372 outermost_invariant_loop (tree def, struct loop *loop)
374 tree def_stmt;
375 basic_block def_bb;
376 struct loop *max_loop;
378 if (TREE_CODE (def) != SSA_NAME)
379 return superloop_at_depth (loop, 1);
381 def_stmt = SSA_NAME_DEF_STMT (def);
382 def_bb = bb_for_stmt (def_stmt);
383 if (!def_bb)
384 return superloop_at_depth (loop, 1);
386 max_loop = find_common_loop (loop, def_bb->loop_father);
388 if (LIM_DATA (def_stmt) && LIM_DATA (def_stmt)->max_loop)
389 max_loop = find_common_loop (max_loop,
390 loop_outer (LIM_DATA (def_stmt)->max_loop));
391 if (max_loop == loop)
392 return NULL;
393 max_loop = superloop_at_depth (loop, loop_depth (max_loop) + 1);
395 return max_loop;
398 /* Returns the outermost superloop of LOOP in that the expression EXPR is
399 invariant. */
401 static struct loop *
402 outermost_invariant_loop_expr (tree expr, struct loop *loop)
404 enum tree_code_class codeclass = TREE_CODE_CLASS (TREE_CODE (expr));
405 unsigned i, nops;
406 struct loop *max_loop = superloop_at_depth (loop, 1), *aloop;
408 if (TREE_CODE (expr) == SSA_NAME
409 || TREE_CODE (expr) == INTEGER_CST
410 || is_gimple_min_invariant (expr))
411 return outermost_invariant_loop (expr, loop);
413 if (codeclass != tcc_unary
414 && codeclass != tcc_binary
415 && codeclass != tcc_expression
416 && codeclass != tcc_vl_exp
417 && codeclass != tcc_comparison)
418 return NULL;
420 nops = TREE_OPERAND_LENGTH (expr);
421 for (i = 0; i < nops; i++)
423 aloop = outermost_invariant_loop_expr (TREE_OPERAND (expr, i), loop);
424 if (!aloop)
425 return NULL;
427 if (flow_loop_nested_p (max_loop, aloop))
428 max_loop = aloop;
431 return max_loop;
434 /* DATA is a structure containing information associated with a statement
435 inside LOOP. DEF is one of the operands of this statement.
437 Find the outermost loop enclosing LOOP in that value of DEF is invariant
438 and record this in DATA->max_loop field. If DEF itself is defined inside
439 this loop as well (i.e. we need to hoist it out of the loop if we want
440 to hoist the statement represented by DATA), record the statement in that
441 DEF is defined to the DATA->depends list. Additionally if ADD_COST is true,
442 add the cost of the computation of DEF to the DATA->cost.
444 If DEF is not invariant in LOOP, return false. Otherwise return TRUE. */
446 static bool
447 add_dependency (tree def, struct lim_aux_data *data, struct loop *loop,
448 bool add_cost)
450 tree def_stmt = SSA_NAME_DEF_STMT (def);
451 basic_block def_bb = bb_for_stmt (def_stmt);
452 struct loop *max_loop;
453 struct depend *dep;
455 if (!def_bb)
456 return true;
458 max_loop = outermost_invariant_loop (def, loop);
459 if (!max_loop)
460 return false;
462 if (flow_loop_nested_p (data->max_loop, max_loop))
463 data->max_loop = max_loop;
465 if (!LIM_DATA (def_stmt))
466 return true;
468 if (add_cost
469 /* Only add the cost if the statement defining DEF is inside LOOP,
470 i.e. if it is likely that by moving the invariants dependent
471 on it, we will be able to avoid creating a new register for
472 it (since it will be only used in these dependent invariants). */
473 && def_bb->loop_father == loop)
474 data->cost += LIM_DATA (def_stmt)->cost;
476 dep = XNEW (struct depend);
477 dep->stmt = def_stmt;
478 dep->next = data->depends;
479 data->depends = dep;
481 return true;
484 /* Returns an estimate for a cost of statement STMT. TODO -- the values here
485 are just ad-hoc constants. The estimates should be based on target-specific
486 values. */
488 static unsigned
489 stmt_cost (tree stmt)
491 tree rhs;
492 unsigned cost = 1;
494 /* Always try to create possibilities for unswitching. */
495 if (TREE_CODE (stmt) == COND_EXPR)
496 return LIM_EXPENSIVE;
498 rhs = GENERIC_TREE_OPERAND (stmt, 1);
500 /* Hoisting memory references out should almost surely be a win. */
501 if (stmt_references_memory_p (stmt))
502 cost += 20;
504 switch (TREE_CODE (rhs))
506 case CALL_EXPR:
507 /* We should be hoisting calls if possible. */
509 /* Unless the call is a builtin_constant_p; this always folds to a
510 constant, so moving it is useless. */
511 rhs = get_callee_fndecl (rhs);
512 if (DECL_BUILT_IN_CLASS (rhs) == BUILT_IN_NORMAL
513 && DECL_FUNCTION_CODE (rhs) == BUILT_IN_CONSTANT_P)
514 return 0;
516 cost += 20;
517 break;
519 case MULT_EXPR:
520 case TRUNC_DIV_EXPR:
521 case CEIL_DIV_EXPR:
522 case FLOOR_DIV_EXPR:
523 case ROUND_DIV_EXPR:
524 case EXACT_DIV_EXPR:
525 case CEIL_MOD_EXPR:
526 case FLOOR_MOD_EXPR:
527 case ROUND_MOD_EXPR:
528 case TRUNC_MOD_EXPR:
529 case RDIV_EXPR:
530 /* Division and multiplication are usually expensive. */
531 cost += 20;
532 break;
534 case LSHIFT_EXPR:
535 case RSHIFT_EXPR:
536 cost += 20;
537 break;
539 default:
540 break;
543 return cost;
546 /* Finds the outermost loop between OUTER and LOOP in that the memory reference
547 REF is independent. If REF is not independent in LOOP, NULL is returned
548 instead. */
550 static struct loop *
551 outermost_indep_loop (struct loop *outer, struct loop *loop, mem_ref_p ref)
553 struct loop *aloop;
555 if (bitmap_bit_p (ref->stored, loop->num))
556 return NULL;
558 for (aloop = outer;
559 aloop != loop;
560 aloop = superloop_at_depth (loop, loop_depth (aloop) + 1))
561 if (!bitmap_bit_p (ref->stored, aloop->num)
562 && ref_indep_loop_p (aloop, ref))
563 return aloop;
565 if (ref_indep_loop_p (loop, ref))
566 return loop;
567 else
568 return NULL;
571 /* If there is a simple load or store to a memory reference in STMT, returns
572 the location of the memory reference, and sets IS_STORE accoring to whether
573 it is a store or load. Otherwise, returns NULL. */
575 static tree *
576 simple_mem_ref_in_stmt (tree stmt, bool *is_store)
578 tree *lhs, *rhs;
580 /* Recognize MEM = (SSA_NAME | invariant) and SSA_NAME = MEM patterns. */
581 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
582 return NULL;
584 lhs = &GIMPLE_STMT_OPERAND (stmt, 0);
585 rhs = &GIMPLE_STMT_OPERAND (stmt, 1);
587 if (TREE_CODE (*lhs) == SSA_NAME)
589 if (!is_gimple_addressable (*rhs))
590 return NULL;
592 *is_store = false;
593 return rhs;
595 else if (TREE_CODE (*rhs) == SSA_NAME
596 || is_gimple_min_invariant (*rhs))
598 *is_store = true;
599 return lhs;
601 else
602 return NULL;
605 /* Returns the memory reference contained in STMT. */
607 static mem_ref_p
608 mem_ref_in_stmt (tree stmt)
610 bool store;
611 tree *mem = simple_mem_ref_in_stmt (stmt, &store);
612 hashval_t hash;
613 mem_ref_p ref;
615 if (!mem)
616 return NULL;
617 gcc_assert (!store);
619 hash = iterative_hash_expr (*mem, 0);
620 ref = htab_find_with_hash (memory_accesses.refs, *mem, hash);
622 gcc_assert (ref != NULL);
623 return ref;
626 /* Determine the outermost loop to that it is possible to hoist a statement
627 STMT and store it to LIM_DATA (STMT)->max_loop. To do this we determine
628 the outermost loop in that the value computed by STMT is invariant.
629 If MUST_PRESERVE_EXEC is true, additionally choose such a loop that
630 we preserve the fact whether STMT is executed. It also fills other related
631 information to LIM_DATA (STMT).
633 The function returns false if STMT cannot be hoisted outside of the loop it
634 is defined in, and true otherwise. */
636 static bool
637 determine_max_movement (tree stmt, bool must_preserve_exec)
639 basic_block bb = bb_for_stmt (stmt);
640 struct loop *loop = bb->loop_father;
641 struct loop *level;
642 struct lim_aux_data *lim_data = LIM_DATA (stmt);
643 tree val;
644 ssa_op_iter iter;
646 if (must_preserve_exec)
647 level = ALWAYS_EXECUTED_IN (bb);
648 else
649 level = superloop_at_depth (loop, 1);
650 lim_data->max_loop = level;
652 FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_USE)
653 if (!add_dependency (val, lim_data, loop, true))
654 return false;
656 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_USES))
658 mem_ref_p ref = mem_ref_in_stmt (stmt);
660 if (ref)
662 lim_data->max_loop
663 = outermost_indep_loop (lim_data->max_loop, loop, ref);
664 if (!lim_data->max_loop)
665 return false;
667 else
669 FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_VIRTUAL_USES)
671 if (!add_dependency (val, lim_data, loop, false))
672 return false;
677 lim_data->cost += stmt_cost (stmt);
679 return true;
682 /* Suppose that some statement in ORIG_LOOP is hoisted to the loop LEVEL,
683 and that one of the operands of this statement is computed by STMT.
684 Ensure that STMT (together with all the statements that define its
685 operands) is hoisted at least out of the loop LEVEL. */
687 static void
688 set_level (tree stmt, struct loop *orig_loop, struct loop *level)
690 struct loop *stmt_loop = bb_for_stmt (stmt)->loop_father;
691 struct depend *dep;
693 stmt_loop = find_common_loop (orig_loop, stmt_loop);
694 if (LIM_DATA (stmt) && LIM_DATA (stmt)->tgt_loop)
695 stmt_loop = find_common_loop (stmt_loop,
696 loop_outer (LIM_DATA (stmt)->tgt_loop));
697 if (flow_loop_nested_p (stmt_loop, level))
698 return;
700 gcc_assert (LIM_DATA (stmt));
701 gcc_assert (level == LIM_DATA (stmt)->max_loop
702 || flow_loop_nested_p (LIM_DATA (stmt)->max_loop, level));
704 LIM_DATA (stmt)->tgt_loop = level;
705 for (dep = LIM_DATA (stmt)->depends; dep; dep = dep->next)
706 set_level (dep->stmt, orig_loop, level);
709 /* Determines an outermost loop from that we want to hoist the statement STMT.
710 For now we chose the outermost possible loop. TODO -- use profiling
711 information to set it more sanely. */
713 static void
714 set_profitable_level (tree stmt)
716 set_level (stmt, bb_for_stmt (stmt)->loop_father, LIM_DATA (stmt)->max_loop);
719 /* Returns true if STMT is not a pure call. */
721 static bool
722 nonpure_call_p (tree stmt)
724 tree call = get_call_expr_in (stmt);
726 if (!call)
727 return false;
729 return TREE_SIDE_EFFECTS (call) != 0;
732 /* Releases the memory occupied by DATA. */
734 static void
735 free_lim_aux_data (struct lim_aux_data *data)
737 struct depend *dep, *next;
739 for (dep = data->depends; dep; dep = next)
741 next = dep->next;
742 free (dep);
744 free (data);
747 /* Rewrite a/b to a*(1/b). Return the invariant stmt to process. */
749 static tree
750 rewrite_reciprocal (block_stmt_iterator *bsi)
752 tree stmt, lhs, rhs, stmt1, stmt2, var, name, tmp;
754 stmt = bsi_stmt (*bsi);
755 lhs = GENERIC_TREE_OPERAND (stmt, 0);
756 rhs = GENERIC_TREE_OPERAND (stmt, 1);
758 /* stmt must be GIMPLE_MODIFY_STMT. */
759 var = create_tmp_var (TREE_TYPE (rhs), "reciptmp");
760 add_referenced_var (var);
762 tmp = build2 (RDIV_EXPR, TREE_TYPE (rhs),
763 build_real (TREE_TYPE (rhs), dconst1),
764 TREE_OPERAND (rhs, 1));
765 stmt1 = build_gimple_modify_stmt (var, tmp);
766 name = make_ssa_name (var, stmt1);
767 GIMPLE_STMT_OPERAND (stmt1, 0) = name;
768 tmp = build2 (MULT_EXPR, TREE_TYPE (rhs),
769 name, TREE_OPERAND (rhs, 0));
770 stmt2 = build_gimple_modify_stmt (lhs, tmp);
772 /* Replace division stmt with reciprocal and multiply stmts.
773 The multiply stmt is not invariant, so update iterator
774 and avoid rescanning. */
775 bsi_replace (bsi, stmt1, true);
776 bsi_insert_after (bsi, stmt2, BSI_NEW_STMT);
777 SSA_NAME_DEF_STMT (lhs) = stmt2;
779 /* Continue processing with invariant reciprocal statement. */
780 return stmt1;
783 /* Check if the pattern at *BSI is a bittest of the form
784 (A >> B) & 1 != 0 and in this case rewrite it to A & (1 << B) != 0. */
786 static tree
787 rewrite_bittest (block_stmt_iterator *bsi)
789 tree stmt, lhs, rhs, var, name, use_stmt, stmt1, stmt2, t;
790 use_operand_p use;
792 stmt = bsi_stmt (*bsi);
793 lhs = GENERIC_TREE_OPERAND (stmt, 0);
794 rhs = GENERIC_TREE_OPERAND (stmt, 1);
796 /* Verify that the single use of lhs is a comparison against zero. */
797 if (TREE_CODE (lhs) != SSA_NAME
798 || !single_imm_use (lhs, &use, &use_stmt)
799 || TREE_CODE (use_stmt) != COND_EXPR)
800 return stmt;
801 t = COND_EXPR_COND (use_stmt);
802 if (TREE_OPERAND (t, 0) != lhs
803 || (TREE_CODE (t) != NE_EXPR
804 && TREE_CODE (t) != EQ_EXPR)
805 || !integer_zerop (TREE_OPERAND (t, 1)))
806 return stmt;
808 /* Get at the operands of the shift. The rhs is TMP1 & 1. */
809 stmt1 = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
810 if (TREE_CODE (stmt1) != GIMPLE_MODIFY_STMT)
811 return stmt;
813 /* There is a conversion in between possibly inserted by fold. */
814 t = GIMPLE_STMT_OPERAND (stmt1, 1);
815 if (TREE_CODE (t) == NOP_EXPR
816 || TREE_CODE (t) == CONVERT_EXPR)
818 t = TREE_OPERAND (t, 0);
819 if (TREE_CODE (t) != SSA_NAME
820 || !has_single_use (t))
821 return stmt;
822 stmt1 = SSA_NAME_DEF_STMT (t);
823 if (TREE_CODE (stmt1) != GIMPLE_MODIFY_STMT)
824 return stmt;
825 t = GIMPLE_STMT_OPERAND (stmt1, 1);
828 /* Verify that B is loop invariant but A is not. Verify that with
829 all the stmt walking we are still in the same loop. */
830 if (TREE_CODE (t) == RSHIFT_EXPR
831 && loop_containing_stmt (stmt1) == loop_containing_stmt (stmt)
832 && outermost_invariant_loop_expr (TREE_OPERAND (t, 1),
833 loop_containing_stmt (stmt1)) != NULL
834 && outermost_invariant_loop_expr (TREE_OPERAND (t, 0),
835 loop_containing_stmt (stmt1)) == NULL)
837 tree a = TREE_OPERAND (t, 0);
838 tree b = TREE_OPERAND (t, 1);
840 /* 1 << B */
841 var = create_tmp_var (TREE_TYPE (a), "shifttmp");
842 add_referenced_var (var);
843 t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (a),
844 build_int_cst (TREE_TYPE (a), 1), b);
845 stmt1 = build_gimple_modify_stmt (var, t);
846 name = make_ssa_name (var, stmt1);
847 GIMPLE_STMT_OPERAND (stmt1, 0) = name;
849 /* A & (1 << B) */
850 t = fold_build2 (BIT_AND_EXPR, TREE_TYPE (a), a, name);
851 stmt2 = build_gimple_modify_stmt (var, t);
852 name = make_ssa_name (var, stmt2);
853 GIMPLE_STMT_OPERAND (stmt2, 0) = name;
855 /* Replace the SSA_NAME we compare against zero. Adjust
856 the type of zero accordingly. */
857 SET_USE (use, name);
858 TREE_OPERAND (COND_EXPR_COND (use_stmt), 1)
859 = build_int_cst_type (TREE_TYPE (name), 0);
861 bsi_insert_before (bsi, stmt1, BSI_SAME_STMT);
862 bsi_replace (bsi, stmt2, true);
864 return stmt1;
867 return stmt;
871 /* Determine the outermost loops in that statements in basic block BB are
872 invariant, and record them to the LIM_DATA associated with the statements.
873 Callback for walk_dominator_tree. */
875 static void
876 determine_invariantness_stmt (struct dom_walk_data *dw_data ATTRIBUTE_UNUSED,
877 basic_block bb)
879 enum move_pos pos;
880 block_stmt_iterator bsi;
881 tree stmt, rhs;
882 bool maybe_never = ALWAYS_EXECUTED_IN (bb) == NULL;
883 struct loop *outermost = ALWAYS_EXECUTED_IN (bb);
885 if (!loop_outer (bb->loop_father))
886 return;
888 if (dump_file && (dump_flags & TDF_DETAILS))
889 fprintf (dump_file, "Basic block %d (loop %d -- depth %d):\n\n",
890 bb->index, bb->loop_father->num, loop_depth (bb->loop_father));
892 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
894 stmt = bsi_stmt (bsi);
896 pos = movement_possibility (stmt);
897 if (pos == MOVE_IMPOSSIBLE)
899 if (nonpure_call_p (stmt))
901 maybe_never = true;
902 outermost = NULL;
904 continue;
907 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
909 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
911 /* If divisor is invariant, convert a/b to a*(1/b), allowing reciprocal
912 to be hoisted out of loop, saving expensive divide. */
913 if (pos == MOVE_POSSIBLE
914 && TREE_CODE (rhs) == RDIV_EXPR
915 && flag_unsafe_math_optimizations
916 && !flag_trapping_math
917 && outermost_invariant_loop_expr (TREE_OPERAND (rhs, 1),
918 loop_containing_stmt (stmt)) != NULL
919 && outermost_invariant_loop_expr (rhs,
920 loop_containing_stmt (stmt)) == NULL)
921 stmt = rewrite_reciprocal (&bsi);
923 /* If the shift count is invariant, convert (A >> B) & 1 to
924 A & (1 << B) allowing the bit mask to be hoisted out of the loop
925 saving an expensive shift. */
926 if (pos == MOVE_POSSIBLE
927 && TREE_CODE (rhs) == BIT_AND_EXPR
928 && integer_onep (TREE_OPERAND (rhs, 1))
929 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
930 && has_single_use (TREE_OPERAND (rhs, 0)))
931 stmt = rewrite_bittest (&bsi);
934 stmt_ann (stmt)->common.aux = xcalloc (1, sizeof (struct lim_aux_data));
935 LIM_DATA (stmt)->always_executed_in = outermost;
937 if (maybe_never && pos == MOVE_PRESERVE_EXECUTION)
938 continue;
940 if (!determine_max_movement (stmt, pos == MOVE_PRESERVE_EXECUTION))
942 LIM_DATA (stmt)->max_loop = NULL;
943 continue;
946 if (dump_file && (dump_flags & TDF_DETAILS))
948 print_generic_stmt_indented (dump_file, stmt, 0, 2);
949 fprintf (dump_file, " invariant up to level %d, cost %d.\n\n",
950 loop_depth (LIM_DATA (stmt)->max_loop),
951 LIM_DATA (stmt)->cost);
954 if (LIM_DATA (stmt)->cost >= LIM_EXPENSIVE)
955 set_profitable_level (stmt);
959 /* For each statement determines the outermost loop in that it is invariant,
960 statements on whose motion it depends and the cost of the computation.
961 This information is stored to the LIM_DATA structure associated with
962 each statement. */
964 static void
965 determine_invariantness (void)
967 struct dom_walk_data walk_data;
969 memset (&walk_data, 0, sizeof (struct dom_walk_data));
970 walk_data.dom_direction = CDI_DOMINATORS;
971 walk_data.before_dom_children_before_stmts = determine_invariantness_stmt;
973 init_walk_dominator_tree (&walk_data);
974 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
975 fini_walk_dominator_tree (&walk_data);
978 /* Hoist the statements in basic block BB out of the loops prescribed by
979 data stored in LIM_DATA structures associated with each statement. Callback
980 for walk_dominator_tree. */
982 static void
983 move_computations_stmt (struct dom_walk_data *dw_data ATTRIBUTE_UNUSED,
984 basic_block bb)
986 struct loop *level;
987 block_stmt_iterator bsi;
988 tree stmt;
989 unsigned cost = 0;
991 if (!loop_outer (bb->loop_father))
992 return;
994 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
996 stmt = bsi_stmt (bsi);
998 if (!LIM_DATA (stmt))
1000 bsi_next (&bsi);
1001 continue;
1004 cost = LIM_DATA (stmt)->cost;
1005 level = LIM_DATA (stmt)->tgt_loop;
1006 free_lim_aux_data (LIM_DATA (stmt));
1007 stmt_ann (stmt)->common.aux = NULL;
1009 if (!level)
1011 bsi_next (&bsi);
1012 continue;
1015 /* We do not really want to move conditionals out of the loop; we just
1016 placed it here to force its operands to be moved if necessary. */
1017 if (TREE_CODE (stmt) == COND_EXPR)
1018 continue;
1020 if (dump_file && (dump_flags & TDF_DETAILS))
1022 fprintf (dump_file, "Moving statement\n");
1023 print_generic_stmt (dump_file, stmt, 0);
1024 fprintf (dump_file, "(cost %u) out of loop %d.\n\n",
1025 cost, level->num);
1028 mark_virtual_ops_for_renaming (stmt);
1029 bsi_insert_on_edge (loop_preheader_edge (level), stmt);
1030 bsi_remove (&bsi, false);
1034 /* Hoist the statements out of the loops prescribed by data stored in
1035 LIM_DATA structures associated with each statement.*/
1037 static void
1038 move_computations (void)
1040 struct dom_walk_data walk_data;
1042 memset (&walk_data, 0, sizeof (struct dom_walk_data));
1043 walk_data.dom_direction = CDI_DOMINATORS;
1044 walk_data.before_dom_children_before_stmts = move_computations_stmt;
1046 init_walk_dominator_tree (&walk_data);
1047 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1048 fini_walk_dominator_tree (&walk_data);
1050 bsi_commit_edge_inserts ();
1051 if (need_ssa_update_p ())
1052 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1055 /* Checks whether the statement defining variable *INDEX can be hoisted
1056 out of the loop passed in DATA. Callback for for_each_index. */
1058 static bool
1059 may_move_till (tree ref, tree *index, void *data)
1061 struct loop *loop = (struct loop*) data, *max_loop;
1063 /* If REF is an array reference, check also that the step and the lower
1064 bound is invariant in LOOP. */
1065 if (TREE_CODE (ref) == ARRAY_REF)
1067 tree step = array_ref_element_size (ref);
1068 tree lbound = array_ref_low_bound (ref);
1070 max_loop = outermost_invariant_loop_expr (step, loop);
1071 if (!max_loop)
1072 return false;
1074 max_loop = outermost_invariant_loop_expr (lbound, loop);
1075 if (!max_loop)
1076 return false;
1079 max_loop = outermost_invariant_loop (*index, loop);
1080 if (!max_loop)
1081 return false;
1083 return true;
1086 /* Forces statements defining (invariant) SSA names in expression EXPR to be
1087 moved out of the LOOP. ORIG_LOOP is the loop in that EXPR is used. */
1089 static void
1090 force_move_till_expr (tree expr, struct loop *orig_loop, struct loop *loop)
1092 enum tree_code_class codeclass = TREE_CODE_CLASS (TREE_CODE (expr));
1093 unsigned i, nops;
1095 if (TREE_CODE (expr) == SSA_NAME)
1097 tree stmt = SSA_NAME_DEF_STMT (expr);
1098 if (IS_EMPTY_STMT (stmt))
1099 return;
1101 set_level (stmt, orig_loop, loop);
1102 return;
1105 if (codeclass != tcc_unary
1106 && codeclass != tcc_binary
1107 && codeclass != tcc_expression
1108 && codeclass != tcc_vl_exp
1109 && codeclass != tcc_comparison)
1110 return;
1112 nops = TREE_OPERAND_LENGTH (expr);
1113 for (i = 0; i < nops; i++)
1114 force_move_till_expr (TREE_OPERAND (expr, i), orig_loop, loop);
1117 /* Forces statement defining invariants in REF (and *INDEX) to be moved out of
1118 the LOOP. The reference REF is used in the loop ORIG_LOOP. Callback for
1119 for_each_index. */
1121 struct fmt_data
1123 struct loop *loop;
1124 struct loop *orig_loop;
1127 static bool
1128 force_move_till (tree ref, tree *index, void *data)
1130 tree stmt;
1131 struct fmt_data *fmt_data = (struct fmt_data *) data;
1133 if (TREE_CODE (ref) == ARRAY_REF)
1135 tree step = array_ref_element_size (ref);
1136 tree lbound = array_ref_low_bound (ref);
1138 force_move_till_expr (step, fmt_data->orig_loop, fmt_data->loop);
1139 force_move_till_expr (lbound, fmt_data->orig_loop, fmt_data->loop);
1142 if (TREE_CODE (*index) != SSA_NAME)
1143 return true;
1145 stmt = SSA_NAME_DEF_STMT (*index);
1146 if (IS_EMPTY_STMT (stmt))
1147 return true;
1149 set_level (stmt, fmt_data->orig_loop, fmt_data->loop);
1151 return true;
1154 /* A hash function for struct mem_ref object OBJ. */
1156 static hashval_t
1157 memref_hash (const void *obj)
1159 const struct mem_ref *mem = obj;
1161 return mem->hash;
1164 /* An equality function for struct mem_ref object OBJ1 with
1165 memory reference OBJ2. */
1167 static int
1168 memref_eq (const void *obj1, const void *obj2)
1170 const struct mem_ref *mem1 = obj1;
1172 return operand_equal_p (mem1->mem, (tree) obj2, 0);
1175 /* Releases list of memory reference locations ACCS. */
1177 static void
1178 free_mem_ref_locs (mem_ref_locs_p accs)
1180 unsigned i;
1181 mem_ref_loc_p loc;
1183 if (!accs)
1184 return;
1186 for (i = 0; VEC_iterate (mem_ref_loc_p, accs->locs, i, loc); i++)
1187 free (loc);
1188 VEC_free (mem_ref_loc_p, heap, accs->locs);
1189 free (accs);
1192 /* A function to free the mem_ref object OBJ. */
1194 static void
1195 memref_free (void *obj)
1197 struct mem_ref *mem = obj;
1198 unsigned i;
1199 mem_ref_locs_p accs;
1201 BITMAP_FREE (mem->stored);
1202 BITMAP_FREE (mem->indep_loop);
1203 BITMAP_FREE (mem->dep_loop);
1204 BITMAP_FREE (mem->indep_ref);
1205 BITMAP_FREE (mem->dep_ref);
1207 for (i = 0; VEC_iterate (mem_ref_locs_p, mem->accesses_in_loop, i, accs); i++)
1208 free_mem_ref_locs (accs);
1209 VEC_free (mem_ref_locs_p, heap, mem->accesses_in_loop);
1211 BITMAP_FREE (mem->vops);
1212 free (mem);
1215 /* Allocates and returns a memory reference description for MEM whose hash
1216 value is HASH and id is ID. */
1218 static mem_ref_p
1219 mem_ref_alloc (tree mem, unsigned hash, unsigned id)
1221 mem_ref_p ref = XNEW (struct mem_ref);
1222 ref->mem = mem;
1223 ref->id = id;
1224 ref->hash = hash;
1225 ref->stored = BITMAP_ALLOC (NULL);
1226 ref->indep_loop = BITMAP_ALLOC (NULL);
1227 ref->dep_loop = BITMAP_ALLOC (NULL);
1228 ref->indep_ref = BITMAP_ALLOC (NULL);
1229 ref->dep_ref = BITMAP_ALLOC (NULL);
1230 ref->accesses_in_loop = NULL;
1231 ref->vops = BITMAP_ALLOC (NULL);
1233 return ref;
1236 /* Allocates and returns the new list of locations. */
1238 static mem_ref_locs_p
1239 mem_ref_locs_alloc (void)
1241 mem_ref_locs_p accs = XNEW (struct mem_ref_locs);
1242 accs->locs = NULL;
1243 return accs;
1246 /* Records memory reference location *LOC in LOOP to the memory reference
1247 description REF. The reference occurs in statement STMT. */
1249 static void
1250 record_mem_ref_loc (mem_ref_p ref, struct loop *loop, tree stmt, tree *loc)
1252 mem_ref_loc_p aref = XNEW (struct mem_ref_loc);
1253 mem_ref_locs_p accs;
1254 bitmap ril = VEC_index (bitmap, memory_accesses.refs_in_loop, loop->num);
1256 if (VEC_length (mem_ref_locs_p, ref->accesses_in_loop)
1257 <= (unsigned) loop->num)
1258 VEC_safe_grow_cleared (mem_ref_locs_p, heap, ref->accesses_in_loop,
1259 loop->num + 1);
1260 accs = VEC_index (mem_ref_locs_p, ref->accesses_in_loop, loop->num);
1261 if (!accs)
1263 accs = mem_ref_locs_alloc ();
1264 VEC_replace (mem_ref_locs_p, ref->accesses_in_loop, loop->num, accs);
1267 aref->stmt = stmt;
1268 aref->ref = loc;
1270 VEC_safe_push (mem_ref_loc_p, heap, accs->locs, aref);
1271 bitmap_set_bit (ril, ref->id);
1274 /* Marks reference REF as stored in LOOP. */
1276 static void
1277 mark_ref_stored (mem_ref_p ref, struct loop *loop)
1279 for (;
1280 loop != current_loops->tree_root
1281 && !bitmap_bit_p (ref->stored, loop->num);
1282 loop = loop_outer (loop))
1283 bitmap_set_bit (ref->stored, loop->num);
1286 /* Gathers memory references in statement STMT in LOOP, storing the
1287 information about them in the memory_accesses structure. Marks
1288 the vops accessed through unrecognized statements there as
1289 well. */
1291 static void
1292 gather_mem_refs_stmt (struct loop *loop, tree stmt)
1294 tree *mem = NULL;
1295 hashval_t hash;
1296 PTR *slot;
1297 mem_ref_p ref;
1298 ssa_op_iter oi;
1299 tree vname;
1300 bool is_stored;
1301 bitmap clvops;
1302 unsigned id;
1304 if (ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
1305 return;
1307 mem = simple_mem_ref_in_stmt (stmt, &is_stored);
1308 if (!mem)
1309 goto fail;
1311 hash = iterative_hash_expr (*mem, 0);
1312 slot = htab_find_slot_with_hash (memory_accesses.refs, *mem, hash, INSERT);
1314 if (*slot)
1316 ref = *slot;
1317 id = ref->id;
1319 else
1321 id = VEC_length (mem_ref_p, memory_accesses.refs_list);
1322 ref = mem_ref_alloc (*mem, hash, id);
1323 VEC_safe_push (mem_ref_p, heap, memory_accesses.refs_list, ref);
1324 *slot = ref;
1326 if (dump_file && (dump_flags & TDF_DETAILS))
1328 fprintf (dump_file, "Memory reference %u: ", id);
1329 print_generic_expr (dump_file, ref->mem, TDF_SLIM);
1330 fprintf (dump_file, "\n");
1333 if (is_stored)
1334 mark_ref_stored (ref, loop);
1336 FOR_EACH_SSA_TREE_OPERAND (vname, stmt, oi, SSA_OP_VIRTUAL_USES)
1337 bitmap_set_bit (ref->vops, DECL_UID (SSA_NAME_VAR (vname)));
1338 record_mem_ref_loc (ref, loop, stmt, mem);
1339 return;
1341 fail:
1342 clvops = VEC_index (bitmap, memory_accesses.clobbered_vops, loop->num);
1343 FOR_EACH_SSA_TREE_OPERAND (vname, stmt, oi, SSA_OP_VIRTUAL_USES)
1344 bitmap_set_bit (clvops, DECL_UID (SSA_NAME_VAR (vname)));
1347 /* Gathers memory references in loops. */
1349 static void
1350 gather_mem_refs_in_loops (void)
1352 block_stmt_iterator bsi;
1353 basic_block bb;
1354 struct loop *loop;
1355 loop_iterator li;
1356 bitmap clvo, clvi;
1357 bitmap lrefs, alrefs, alrefso;
1359 FOR_EACH_BB (bb)
1361 loop = bb->loop_father;
1362 if (loop == current_loops->tree_root)
1363 continue;
1365 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1366 gather_mem_refs_stmt (loop, bsi_stmt (bsi));
1369 /* Propagate the information about clobbered vops and accessed memory
1370 references up the loop hierarchy. */
1371 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
1373 lrefs = VEC_index (bitmap, memory_accesses.refs_in_loop, loop->num);
1374 alrefs = VEC_index (bitmap, memory_accesses.all_refs_in_loop, loop->num);
1375 bitmap_ior_into (alrefs, lrefs);
1377 if (loop_outer (loop) == current_loops->tree_root)
1378 continue;
1380 clvi = VEC_index (bitmap, memory_accesses.clobbered_vops, loop->num);
1381 clvo = VEC_index (bitmap, memory_accesses.clobbered_vops,
1382 loop_outer (loop)->num);
1383 bitmap_ior_into (clvo, clvi);
1385 alrefso = VEC_index (bitmap, memory_accesses.all_refs_in_loop,
1386 loop_outer (loop)->num);
1387 bitmap_ior_into (alrefso, alrefs);
1391 /* Element of the hash table that maps vops to memory references. */
1393 struct vop_to_refs_elt
1395 /* DECL_UID of the vop. */
1396 unsigned uid;
1398 /* List of the all references. */
1399 bitmap refs_all;
1401 /* List of stored references. */
1402 bitmap refs_stored;
1405 /* A hash function for struct vop_to_refs_elt object OBJ. */
1407 static hashval_t
1408 vtoe_hash (const void *obj)
1410 const struct vop_to_refs_elt *vtoe = obj;
1412 return vtoe->uid;
1415 /* An equality function for struct vop_to_refs_elt object OBJ1 with
1416 uid of a vop OBJ2. */
1418 static int
1419 vtoe_eq (const void *obj1, const void *obj2)
1421 const struct vop_to_refs_elt *vtoe = obj1;
1422 const unsigned *uid = obj2;
1424 return vtoe->uid == *uid;
1427 /* A function to free the struct vop_to_refs_elt object. */
1429 static void
1430 vtoe_free (void *obj)
1432 struct vop_to_refs_elt *vtoe = obj;
1434 BITMAP_FREE (vtoe->refs_all);
1435 BITMAP_FREE (vtoe->refs_stored);
1436 free (vtoe);
1439 /* Records REF to hashtable VOP_TO_REFS for the index VOP. STORED is true
1440 if the reference REF is stored. */
1442 static void
1443 record_vop_access (htab_t vop_to_refs, unsigned vop, unsigned ref, bool stored)
1445 void **slot = htab_find_slot_with_hash (vop_to_refs, &vop, vop, INSERT);
1446 struct vop_to_refs_elt *vtoe;
1448 if (!*slot)
1450 vtoe = XNEW (struct vop_to_refs_elt);
1451 vtoe->uid = vop;
1452 vtoe->refs_all = BITMAP_ALLOC (NULL);
1453 vtoe->refs_stored = BITMAP_ALLOC (NULL);
1454 *slot = vtoe;
1456 else
1457 vtoe = *slot;
1459 bitmap_set_bit (vtoe->refs_all, ref);
1460 if (stored)
1461 bitmap_set_bit (vtoe->refs_stored, ref);
1464 /* Returns the set of references that access VOP according to the table
1465 VOP_TO_REFS. */
1467 static bitmap
1468 get_vop_accesses (htab_t vop_to_refs, unsigned vop)
1470 struct vop_to_refs_elt *vtoe = htab_find_with_hash (vop_to_refs, &vop, vop);
1471 return vtoe->refs_all;
1474 /* Returns the set of stores that access VOP according to the table
1475 VOP_TO_REFS. */
1477 static bitmap
1478 get_vop_stores (htab_t vop_to_refs, unsigned vop)
1480 struct vop_to_refs_elt *vtoe = htab_find_with_hash (vop_to_refs, &vop, vop);
1481 return vtoe->refs_stored;
1484 /* Adds REF to mapping from virtual operands to references in LOOP. */
1486 static void
1487 add_vop_ref_mapping (struct loop *loop, mem_ref_p ref)
1489 htab_t map = VEC_index (htab_t, memory_accesses.vop_ref_map, loop->num);
1490 bool stored = bitmap_bit_p (ref->stored, loop->num);
1491 bitmap clobbers = VEC_index (bitmap, memory_accesses.clobbered_vops,
1492 loop->num);
1493 bitmap_iterator bi;
1494 unsigned vop;
1496 EXECUTE_IF_AND_COMPL_IN_BITMAP (ref->vops, clobbers, 0, vop, bi)
1498 record_vop_access (map, vop, ref->id, stored);
1502 /* Create a mapping from virtual operands to references that touch them
1503 in LOOP. */
1505 static void
1506 create_vop_ref_mapping_loop (struct loop *loop)
1508 bitmap refs = VEC_index (bitmap, memory_accesses.refs_in_loop, loop->num);
1509 struct loop *sloop;
1510 bitmap_iterator bi;
1511 unsigned i;
1512 mem_ref_p ref;
1514 EXECUTE_IF_SET_IN_BITMAP (refs, 0, i, bi)
1516 ref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
1517 for (sloop = loop; sloop != current_loops->tree_root; sloop = loop_outer (sloop))
1518 add_vop_ref_mapping (sloop, ref);
1522 /* For each non-clobbered virtual operand and each loop, record the memory
1523 references in this loop that touch the operand. */
1525 static void
1526 create_vop_ref_mapping (void)
1528 loop_iterator li;
1529 struct loop *loop;
1531 FOR_EACH_LOOP (li, loop, 0)
1533 create_vop_ref_mapping_loop (loop);
1537 /* Gathers information about memory accesses in the loops. */
1539 static void
1540 analyze_memory_references (void)
1542 unsigned i;
1543 bitmap empty;
1544 htab_t hempty;
1546 memory_accesses.refs
1547 = htab_create (100, memref_hash, memref_eq, memref_free);
1548 memory_accesses.refs_list = NULL;
1549 memory_accesses.refs_in_loop = VEC_alloc (bitmap, heap,
1550 number_of_loops ());
1551 memory_accesses.all_refs_in_loop = VEC_alloc (bitmap, heap,
1552 number_of_loops ());
1553 memory_accesses.clobbered_vops = VEC_alloc (bitmap, heap,
1554 number_of_loops ());
1555 memory_accesses.vop_ref_map = VEC_alloc (htab_t, heap,
1556 number_of_loops ());
1558 for (i = 0; i < number_of_loops (); i++)
1560 empty = BITMAP_ALLOC (NULL);
1561 VEC_quick_push (bitmap, memory_accesses.refs_in_loop, empty);
1562 empty = BITMAP_ALLOC (NULL);
1563 VEC_quick_push (bitmap, memory_accesses.all_refs_in_loop, empty);
1564 empty = BITMAP_ALLOC (NULL);
1565 VEC_quick_push (bitmap, memory_accesses.clobbered_vops, empty);
1566 hempty = htab_create (10, vtoe_hash, vtoe_eq, vtoe_free);
1567 VEC_quick_push (htab_t, memory_accesses.vop_ref_map, hempty);
1570 memory_accesses.ttae_cache = NULL;
1572 gather_mem_refs_in_loops ();
1573 create_vop_ref_mapping ();
1576 /* Returns true if a region of size SIZE1 at position 0 and a region of
1577 size SIZE2 at position DIFF cannot overlap. */
1579 static bool
1580 cannot_overlap_p (aff_tree *diff, double_int size1, double_int size2)
1582 double_int d, bound;
1584 /* Unless the difference is a constant, we fail. */
1585 if (diff->n != 0)
1586 return false;
1588 d = diff->offset;
1589 if (double_int_negative_p (d))
1591 /* The second object is before the first one, we succeed if the last
1592 element of the second object is before the start of the first one. */
1593 bound = double_int_add (d, double_int_add (size2, double_int_minus_one));
1594 return double_int_negative_p (bound);
1596 else
1598 /* We succeed if the second object starts after the first one ends. */
1599 return double_int_scmp (size1, d) <= 0;
1603 /* Returns true if MEM1 and MEM2 may alias. TTAE_CACHE is used as a cache in
1604 tree_to_aff_combination_expand. */
1606 static bool
1607 mem_refs_may_alias_p (tree mem1, tree mem2, struct pointer_map_t **ttae_cache)
1609 /* Perform BASE + OFFSET analysis -- if MEM1 and MEM2 are based on the same
1610 object and their offset differ in such a way that the locations cannot
1611 overlap, then they cannot alias. */
1612 aff_tree off1, off2;
1613 double_int size1, size2;
1614 tree base1, base2;
1616 /* If MEM1 and MEM2 are based on different variables, they cannot alias. */
1617 base1 = get_base_address (mem1);
1618 base2 = get_base_address (mem2);
1620 if (base1
1621 && !INDIRECT_REF_P (base1)
1622 && base2
1623 && !INDIRECT_REF_P (base2)
1624 && !operand_equal_p (base1, base2, 0))
1625 return false;
1627 /* With strict aliasing, it is impossible to access a scalar variable through
1628 anything but a pointer dereference or through a union (gcc extension). */
1629 if (flag_strict_aliasing)
1631 if (!INDIRECT_REF_P (mem1)
1632 && base1
1633 && TREE_CODE (TREE_TYPE (base1)) != UNION_TYPE
1634 && SSA_VAR_P (mem2)
1635 && !AGGREGATE_TYPE_P (TREE_TYPE (mem2)))
1636 return false;
1637 if (!INDIRECT_REF_P (mem2)
1638 && base2
1639 && TREE_CODE (TREE_TYPE (base2)) != UNION_TYPE
1640 && SSA_VAR_P (mem1)
1641 && !AGGREGATE_TYPE_P (TREE_TYPE (mem1)))
1642 return false;
1645 /* The expansion of addresses may be a bit expensive, thus we only do
1646 the check at -O2 and higher optimization levels. */
1647 if (optimize < 2)
1648 return true;
1650 get_inner_reference_aff (mem1, &off1, &size1);
1651 get_inner_reference_aff (mem2, &off2, &size2);
1652 aff_combination_expand (&off1, ttae_cache);
1653 aff_combination_expand (&off2, ttae_cache);
1654 aff_combination_scale (&off1, double_int_minus_one);
1655 aff_combination_add (&off2, &off1);
1657 if (cannot_overlap_p (&off2, size1, size2))
1658 return false;
1660 return true;
1663 /* Rewrites location LOC by TMP_VAR. */
1665 static void
1666 rewrite_mem_ref_loc (mem_ref_loc_p loc, tree tmp_var)
1668 mark_virtual_ops_for_renaming (loc->stmt);
1669 *loc->ref = tmp_var;
1670 update_stmt (loc->stmt);
1673 /* Adds all locations of REF in LOOP and its subloops to LOCS. */
1675 static void
1676 get_all_locs_in_loop (struct loop *loop, mem_ref_p ref,
1677 VEC (mem_ref_loc_p, heap) **locs)
1679 mem_ref_locs_p accs;
1680 unsigned i;
1681 mem_ref_loc_p loc;
1682 bitmap refs = VEC_index (bitmap, memory_accesses.all_refs_in_loop,
1683 loop->num);
1684 struct loop *subloop;
1686 if (!bitmap_bit_p (refs, ref->id))
1687 return;
1689 if (VEC_length (mem_ref_locs_p, ref->accesses_in_loop)
1690 > (unsigned) loop->num)
1692 accs = VEC_index (mem_ref_locs_p, ref->accesses_in_loop, loop->num);
1693 if (accs)
1695 for (i = 0; VEC_iterate (mem_ref_loc_p, accs->locs, i, loc); i++)
1696 VEC_safe_push (mem_ref_loc_p, heap, *locs, loc);
1700 for (subloop = loop->inner; subloop != NULL; subloop = subloop->next)
1701 get_all_locs_in_loop (subloop, ref, locs);
1704 /* Rewrites all references to REF in LOOP by variable TMP_VAR. */
1706 static void
1707 rewrite_mem_refs (struct loop *loop, mem_ref_p ref, tree tmp_var)
1709 unsigned i;
1710 mem_ref_loc_p loc;
1711 VEC (mem_ref_loc_p, heap) *locs = NULL;
1713 get_all_locs_in_loop (loop, ref, &locs);
1714 for (i = 0; VEC_iterate (mem_ref_loc_p, locs, i, loc); i++)
1715 rewrite_mem_ref_loc (loc, tmp_var);
1716 VEC_free (mem_ref_loc_p, heap, locs);
1719 /* The name and the length of the currently generated variable
1720 for lsm. */
1721 #define MAX_LSM_NAME_LENGTH 40
1722 static char lsm_tmp_name[MAX_LSM_NAME_LENGTH + 1];
1723 static int lsm_tmp_name_length;
1725 /* Adds S to lsm_tmp_name. */
1727 static void
1728 lsm_tmp_name_add (const char *s)
1730 int l = strlen (s) + lsm_tmp_name_length;
1731 if (l > MAX_LSM_NAME_LENGTH)
1732 return;
1734 strcpy (lsm_tmp_name + lsm_tmp_name_length, s);
1735 lsm_tmp_name_length = l;
1738 /* Stores the name for temporary variable that replaces REF to
1739 lsm_tmp_name. */
1741 static void
1742 gen_lsm_tmp_name (tree ref)
1744 const char *name;
1746 switch (TREE_CODE (ref))
1748 case MISALIGNED_INDIRECT_REF:
1749 case ALIGN_INDIRECT_REF:
1750 case INDIRECT_REF:
1751 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1752 lsm_tmp_name_add ("_");
1753 break;
1755 case BIT_FIELD_REF:
1756 case VIEW_CONVERT_EXPR:
1757 case ARRAY_RANGE_REF:
1758 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1759 break;
1761 case REALPART_EXPR:
1762 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1763 lsm_tmp_name_add ("_RE");
1764 break;
1766 case IMAGPART_EXPR:
1767 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1768 lsm_tmp_name_add ("_IM");
1769 break;
1771 case COMPONENT_REF:
1772 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1773 lsm_tmp_name_add ("_");
1774 name = get_name (TREE_OPERAND (ref, 1));
1775 if (!name)
1776 name = "F";
1777 lsm_tmp_name_add ("_");
1778 lsm_tmp_name_add (name);
1780 case ARRAY_REF:
1781 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1782 lsm_tmp_name_add ("_I");
1783 break;
1785 case SSA_NAME:
1786 ref = SSA_NAME_VAR (ref);
1787 /* Fallthru. */
1789 case VAR_DECL:
1790 case PARM_DECL:
1791 name = get_name (ref);
1792 if (!name)
1793 name = "D";
1794 lsm_tmp_name_add (name);
1795 break;
1797 case STRING_CST:
1798 lsm_tmp_name_add ("S");
1799 break;
1801 case RESULT_DECL:
1802 lsm_tmp_name_add ("R");
1803 break;
1805 default:
1806 gcc_unreachable ();
1810 /* Determines name for temporary variable that replaces REF.
1811 The name is accumulated into the lsm_tmp_name variable.
1812 N is added to the name of the temporary. */
1814 char *
1815 get_lsm_tmp_name (tree ref, unsigned n)
1817 char ns[2];
1819 lsm_tmp_name_length = 0;
1820 gen_lsm_tmp_name (ref);
1821 lsm_tmp_name_add ("_lsm");
1822 if (n < 10)
1824 ns[0] = '0' + n;
1825 ns[1] = 0;
1826 lsm_tmp_name_add (ns);
1828 return lsm_tmp_name;
1831 /* Executes store motion of memory reference REF from LOOP.
1832 Exits from the LOOP are stored in EXITS. The initialization of the
1833 temporary variable is put to the preheader of the loop, and assignments
1834 to the reference from the temporary variable are emitted to exits. */
1836 static void
1837 execute_sm (struct loop *loop, VEC (edge, heap) *exits, mem_ref_p ref)
1839 tree tmp_var;
1840 unsigned i;
1841 tree load, store;
1842 struct fmt_data fmt_data;
1843 edge ex;
1845 if (dump_file && (dump_flags & TDF_DETAILS))
1847 fprintf (dump_file, "Executing store motion of ");
1848 print_generic_expr (dump_file, ref->mem, 0);
1849 fprintf (dump_file, " from loop %d\n", loop->num);
1852 tmp_var = make_rename_temp (TREE_TYPE (ref->mem),
1853 get_lsm_tmp_name (ref->mem, ~0));
1855 fmt_data.loop = loop;
1856 fmt_data.orig_loop = loop;
1857 for_each_index (&ref->mem, force_move_till, &fmt_data);
1859 rewrite_mem_refs (loop, ref, tmp_var);
1861 /* Emit the load & stores. */
1862 load = build_gimple_modify_stmt (tmp_var, unshare_expr (ref->mem));
1863 get_stmt_ann (load)->common.aux = xcalloc (1, sizeof (struct lim_aux_data));
1864 LIM_DATA (load)->max_loop = loop;
1865 LIM_DATA (load)->tgt_loop = loop;
1867 /* Put this into the latch, so that we are sure it will be processed after
1868 all dependencies. */
1869 bsi_insert_on_edge (loop_latch_edge (loop), load);
1871 for (i = 0; VEC_iterate (edge, exits, i, ex); i++)
1873 store = build_gimple_modify_stmt (unshare_expr (ref->mem), tmp_var);
1874 bsi_insert_on_edge (ex, store);
1878 /* Hoists memory references MEM_REFS out of LOOP. EXITS is the list of exit
1879 edges of the LOOP. */
1881 static void
1882 hoist_memory_references (struct loop *loop, bitmap mem_refs,
1883 VEC (edge, heap) *exits)
1885 mem_ref_p ref;
1886 unsigned i;
1887 bitmap_iterator bi;
1889 EXECUTE_IF_SET_IN_BITMAP (mem_refs, 0, i, bi)
1891 ref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
1892 execute_sm (loop, exits, ref);
1896 /* Returns true if REF is always accessed in LOOP. */
1898 static bool
1899 ref_always_accessed_p (struct loop *loop, mem_ref_p ref)
1901 VEC (mem_ref_loc_p, heap) *locs = NULL;
1902 unsigned i;
1903 mem_ref_loc_p loc;
1904 bool ret = false;
1905 struct loop *must_exec;
1907 get_all_locs_in_loop (loop, ref, &locs);
1908 for (i = 0; VEC_iterate (mem_ref_loc_p, locs, i, loc); i++)
1910 if (!LIM_DATA (loc->stmt))
1911 continue;
1913 must_exec = LIM_DATA (loc->stmt)->always_executed_in;
1914 if (!must_exec)
1915 continue;
1917 if (must_exec == loop
1918 || flow_loop_nested_p (must_exec, loop))
1920 ret = true;
1921 break;
1924 VEC_free (mem_ref_loc_p, heap, locs);
1926 return ret;
1929 /* Returns true if REF1 and REF2 are independent. */
1931 static bool
1932 refs_independent_p (mem_ref_p ref1, mem_ref_p ref2)
1934 if (ref1 == ref2
1935 || bitmap_bit_p (ref1->indep_ref, ref2->id))
1936 return true;
1937 if (bitmap_bit_p (ref1->dep_ref, ref2->id))
1938 return false;
1940 if (dump_file && (dump_flags & TDF_DETAILS))
1941 fprintf (dump_file, "Querying dependency of refs %u and %u: ",
1942 ref1->id, ref2->id);
1944 if (mem_refs_may_alias_p (ref1->mem, ref2->mem,
1945 &memory_accesses.ttae_cache))
1947 bitmap_set_bit (ref1->dep_ref, ref2->id);
1948 bitmap_set_bit (ref2->dep_ref, ref1->id);
1949 if (dump_file && (dump_flags & TDF_DETAILS))
1950 fprintf (dump_file, "dependent.\n");
1951 return false;
1953 else
1955 bitmap_set_bit (ref1->indep_ref, ref2->id);
1956 bitmap_set_bit (ref2->indep_ref, ref1->id);
1957 if (dump_file && (dump_flags & TDF_DETAILS))
1958 fprintf (dump_file, "independent.\n");
1959 return true;
1963 /* Records the information whether REF is independent in LOOP (according
1964 to INDEP). */
1966 static void
1967 record_indep_loop (struct loop *loop, mem_ref_p ref, bool indep)
1969 if (indep)
1970 bitmap_set_bit (ref->indep_loop, loop->num);
1971 else
1972 bitmap_set_bit (ref->dep_loop, loop->num);
1975 /* Returns true if REF is independent on all other memory references in
1976 LOOP. */
1978 static bool
1979 ref_indep_loop_p_1 (struct loop *loop, mem_ref_p ref)
1981 bitmap clobbers, refs_to_check, refs;
1982 unsigned i;
1983 bitmap_iterator bi;
1984 bool ret = true, stored = bitmap_bit_p (ref->stored, loop->num);
1985 htab_t map;
1986 mem_ref_p aref;
1988 /* If the reference is clobbered, it is not independent. */
1989 clobbers = VEC_index (bitmap, memory_accesses.clobbered_vops, loop->num);
1990 if (bitmap_intersect_p (ref->vops, clobbers))
1991 return false;
1993 refs_to_check = BITMAP_ALLOC (NULL);
1995 map = VEC_index (htab_t, memory_accesses.vop_ref_map, loop->num);
1996 EXECUTE_IF_AND_COMPL_IN_BITMAP (ref->vops, clobbers, 0, i, bi)
1998 if (stored)
1999 refs = get_vop_accesses (map, i);
2000 else
2001 refs = get_vop_stores (map, i);
2003 bitmap_ior_into (refs_to_check, refs);
2006 EXECUTE_IF_SET_IN_BITMAP (refs_to_check, 0, i, bi)
2008 aref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
2009 if (!refs_independent_p (ref, aref))
2011 ret = false;
2012 record_indep_loop (loop, aref, false);
2013 break;
2017 BITMAP_FREE (refs_to_check);
2018 return ret;
2021 /* Returns true if REF is independent on all other memory references in
2022 LOOP. Wrapper over ref_indep_loop_p_1, caching its results. */
2024 static bool
2025 ref_indep_loop_p (struct loop *loop, mem_ref_p ref)
2027 bool ret;
2029 if (bitmap_bit_p (ref->indep_loop, loop->num))
2030 return true;
2031 if (bitmap_bit_p (ref->dep_loop, loop->num))
2032 return false;
2034 ret = ref_indep_loop_p_1 (loop, ref);
2036 if (dump_file && (dump_flags & TDF_DETAILS))
2037 fprintf (dump_file, "Querying dependencies of ref %u in loop %d: %s\n",
2038 ref->id, loop->num, ret ? "independent" : "dependent");
2040 record_indep_loop (loop, ref, ret);
2042 return ret;
2045 /* Returns true if we can perform store motion of REF from LOOP. */
2047 static bool
2048 can_sm_ref_p (struct loop *loop, mem_ref_p ref)
2050 /* Unless the reference is stored in the loop, there is nothing to do. */
2051 if (!bitmap_bit_p (ref->stored, loop->num))
2052 return false;
2054 /* It should be movable. */
2055 if (!is_gimple_reg_type (TREE_TYPE (ref->mem))
2056 || TREE_THIS_VOLATILE (ref->mem)
2057 || !for_each_index (&ref->mem, may_move_till, loop))
2058 return false;
2060 /* If it can trap, it must be always executed in LOOP. */
2061 if (tree_could_trap_p (ref->mem)
2062 && !ref_always_accessed_p (loop, ref))
2063 return false;
2065 /* And it must be independent on all other memory references
2066 in LOOP. */
2067 if (!ref_indep_loop_p (loop, ref))
2068 return false;
2070 return true;
2073 /* Marks the references in LOOP for that store motion should be performed
2074 in REFS_TO_SM. SM_EXECUTED is the set of references for that store
2075 motion was performed in one of the outer loops. */
2077 static void
2078 find_refs_for_sm (struct loop *loop, bitmap sm_executed, bitmap refs_to_sm)
2080 bitmap refs = VEC_index (bitmap, memory_accesses.all_refs_in_loop,
2081 loop->num);
2082 unsigned i;
2083 bitmap_iterator bi;
2084 mem_ref_p ref;
2086 EXECUTE_IF_AND_COMPL_IN_BITMAP (refs, sm_executed, 0, i, bi)
2088 ref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
2089 if (can_sm_ref_p (loop, ref))
2090 bitmap_set_bit (refs_to_sm, i);
2094 /* Checks whether LOOP (with exits stored in EXITS array) is suitable
2095 for a store motion optimization (i.e. whether we can insert statement
2096 on its exits). */
2098 static bool
2099 loop_suitable_for_sm (struct loop *loop ATTRIBUTE_UNUSED,
2100 VEC (edge, heap) *exits)
2102 unsigned i;
2103 edge ex;
2105 for (i = 0; VEC_iterate (edge, exits, i, ex); i++)
2106 if (ex->flags & EDGE_ABNORMAL)
2107 return false;
2109 return true;
2112 /* Try to perform store motion for all memory references modified inside
2113 LOOP. SM_EXECUTED is the bitmap of the memory references for that
2114 store motion was executed in one of the outer loops. */
2116 static void
2117 store_motion_loop (struct loop *loop, bitmap sm_executed)
2119 VEC (edge, heap) *exits = get_loop_exit_edges (loop);
2120 struct loop *subloop;
2121 bitmap sm_in_loop = BITMAP_ALLOC (NULL);
2123 if (loop_suitable_for_sm (loop, exits))
2125 find_refs_for_sm (loop, sm_executed, sm_in_loop);
2126 hoist_memory_references (loop, sm_in_loop, exits);
2128 VEC_free (edge, heap, exits);
2130 bitmap_ior_into (sm_executed, sm_in_loop);
2131 for (subloop = loop->inner; subloop != NULL; subloop = subloop->next)
2132 store_motion_loop (subloop, sm_executed);
2133 bitmap_and_compl_into (sm_executed, sm_in_loop);
2134 BITMAP_FREE (sm_in_loop);
2137 /* Try to perform store motion for all memory references modified inside
2138 loops. */
2140 static void
2141 store_motion (void)
2143 struct loop *loop;
2144 bitmap sm_executed = BITMAP_ALLOC (NULL);
2146 for (loop = current_loops->tree_root->inner; loop != NULL; loop = loop->next)
2147 store_motion_loop (loop, sm_executed);
2149 BITMAP_FREE (sm_executed);
2150 bsi_commit_edge_inserts ();
2153 /* Fills ALWAYS_EXECUTED_IN information for basic blocks of LOOP, i.e.
2154 for each such basic block bb records the outermost loop for that execution
2155 of its header implies execution of bb. CONTAINS_CALL is the bitmap of
2156 blocks that contain a nonpure call. */
2158 static void
2159 fill_always_executed_in (struct loop *loop, sbitmap contains_call)
2161 basic_block bb = NULL, *bbs, last = NULL;
2162 unsigned i;
2163 edge e;
2164 struct loop *inn_loop = loop;
2166 if (!loop->header->aux)
2168 bbs = get_loop_body_in_dom_order (loop);
2170 for (i = 0; i < loop->num_nodes; i++)
2172 edge_iterator ei;
2173 bb = bbs[i];
2175 if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
2176 last = bb;
2178 if (TEST_BIT (contains_call, bb->index))
2179 break;
2181 FOR_EACH_EDGE (e, ei, bb->succs)
2182 if (!flow_bb_inside_loop_p (loop, e->dest))
2183 break;
2184 if (e)
2185 break;
2187 /* A loop might be infinite (TODO use simple loop analysis
2188 to disprove this if possible). */
2189 if (bb->flags & BB_IRREDUCIBLE_LOOP)
2190 break;
2192 if (!flow_bb_inside_loop_p (inn_loop, bb))
2193 break;
2195 if (bb->loop_father->header == bb)
2197 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
2198 break;
2200 /* In a loop that is always entered we may proceed anyway.
2201 But record that we entered it and stop once we leave it. */
2202 inn_loop = bb->loop_father;
2206 while (1)
2208 last->aux = loop;
2209 if (last == loop->header)
2210 break;
2211 last = get_immediate_dominator (CDI_DOMINATORS, last);
2214 free (bbs);
2217 for (loop = loop->inner; loop; loop = loop->next)
2218 fill_always_executed_in (loop, contains_call);
2221 /* Compute the global information needed by the loop invariant motion pass. */
2223 static void
2224 tree_ssa_lim_initialize (void)
2226 sbitmap contains_call = sbitmap_alloc (last_basic_block);
2227 block_stmt_iterator bsi;
2228 struct loop *loop;
2229 basic_block bb;
2231 sbitmap_zero (contains_call);
2232 FOR_EACH_BB (bb)
2234 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2236 if (nonpure_call_p (bsi_stmt (bsi)))
2237 break;
2240 if (!bsi_end_p (bsi))
2241 SET_BIT (contains_call, bb->index);
2244 for (loop = current_loops->tree_root->inner; loop; loop = loop->next)
2245 fill_always_executed_in (loop, contains_call);
2247 sbitmap_free (contains_call);
2250 /* Cleans up after the invariant motion pass. */
2252 static void
2253 tree_ssa_lim_finalize (void)
2255 basic_block bb;
2256 unsigned i;
2257 bitmap b;
2258 htab_t h;
2260 FOR_EACH_BB (bb)
2262 bb->aux = NULL;
2265 VEC_free (mem_ref_p, heap, memory_accesses.refs_list);
2266 htab_delete (memory_accesses.refs);
2268 for (i = 0; VEC_iterate (bitmap, memory_accesses.refs_in_loop, i, b); i++)
2269 BITMAP_FREE (b);
2270 VEC_free (bitmap, heap, memory_accesses.refs_in_loop);
2272 for (i = 0; VEC_iterate (bitmap, memory_accesses.all_refs_in_loop, i, b); i++)
2273 BITMAP_FREE (b);
2274 VEC_free (bitmap, heap, memory_accesses.all_refs_in_loop);
2276 for (i = 0; VEC_iterate (bitmap, memory_accesses.clobbered_vops, i, b); i++)
2277 BITMAP_FREE (b);
2278 VEC_free (bitmap, heap, memory_accesses.clobbered_vops);
2280 for (i = 0; VEC_iterate (htab_t, memory_accesses.vop_ref_map, i, h); i++)
2281 htab_delete (h);
2282 VEC_free (htab_t, heap, memory_accesses.vop_ref_map);
2284 if (memory_accesses.ttae_cache)
2285 pointer_map_destroy (memory_accesses.ttae_cache);
2288 /* Moves invariants from loops. Only "expensive" invariants are moved out --
2289 i.e. those that are likely to be win regardless of the register pressure. */
2291 void
2292 tree_ssa_lim (void)
2294 tree_ssa_lim_initialize ();
2296 /* Gathers information about memory accesses in the loops. */
2297 analyze_memory_references ();
2299 /* For each statement determine the outermost loop in that it is
2300 invariant and cost for computing the invariant. */
2301 determine_invariantness ();
2303 /* Execute store motion. Force the necessary invariants to be moved
2304 out of the loops as well. */
2305 store_motion ();
2307 /* Move the expressions that are expensive enough. */
2308 move_computations ();
2310 tree_ssa_lim_finalize ();