Revert r135493 & r135463
[official-gcc.git] / gcc / tree-ssa-loop-im.c
blob8740030397f48bcbb7f01a74c39394827b72439e
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"
43 #include "tree-ssa-propagate.h"
45 /* TODO: Support for predicated code motion. I.e.
47 while (1)
49 if (cond)
51 a = inv;
52 something;
56 Where COND and INV are is invariants, but evaluating INV may trap or be
57 invalid from some other reason if !COND. This may be transformed to
59 if (cond)
60 a = inv;
61 while (1)
63 if (cond)
64 something;
65 } */
67 /* A type for the list of statements that have to be moved in order to be able
68 to hoist an invariant computation. */
70 struct depend
72 tree stmt;
73 struct depend *next;
76 /* The auxiliary data kept for each statement. */
78 struct lim_aux_data
80 struct loop *max_loop; /* The outermost loop in that the statement
81 is invariant. */
83 struct loop *tgt_loop; /* The loop out of that we want to move the
84 invariant. */
86 struct loop *always_executed_in;
87 /* The outermost loop for that we are sure
88 the statement is executed if the loop
89 is entered. */
91 unsigned cost; /* Cost of the computation performed by the
92 statement. */
94 struct depend *depends; /* List of statements that must be also hoisted
95 out of the loop when this statement is
96 hoisted; i.e. those that define the operands
97 of the statement and are inside of the
98 MAX_LOOP loop. */
101 #define LIM_DATA(STMT) (TREE_CODE (STMT) == PHI_NODE \
102 ? NULL \
103 : (struct lim_aux_data *) (stmt_ann (STMT)->common.aux))
105 /* Description of a memory reference location. */
107 typedef struct mem_ref_loc
109 tree *ref; /* The reference itself. */
110 tree stmt; /* The statement in that it occurs. */
111 } *mem_ref_loc_p;
113 DEF_VEC_P(mem_ref_loc_p);
114 DEF_VEC_ALLOC_P(mem_ref_loc_p, heap);
116 /* The list of memory reference locations in a loop. */
118 typedef struct mem_ref_locs
120 VEC (mem_ref_loc_p, heap) *locs;
121 } *mem_ref_locs_p;
123 DEF_VEC_P(mem_ref_locs_p);
124 DEF_VEC_ALLOC_P(mem_ref_locs_p, heap);
126 /* Description of a memory reference. */
128 typedef struct mem_ref
130 tree mem; /* The memory itself. */
131 unsigned id; /* ID assigned to the memory reference
132 (its index in memory_accesses.refs_list) */
133 hashval_t hash; /* Its hash value. */
134 bitmap stored; /* The set of loops in that this memory locatio
135 is stored to. */
136 VEC (mem_ref_locs_p, heap) *accesses_in_loop;
137 /* The locations of the accesses. Vector
138 indexed by the loop number. */
139 bitmap vops; /* Vops corresponding to this memory
140 location. */
142 /* The following sets are computed on demand. We keep both set and
143 its complement, so that we know whether the information was
144 already computed or not. */
145 bitmap indep_loop; /* The set of loops in that the memory
146 reference is independent, meaning:
147 If it is stored in the loop, this store
148 is independent on all other loads and
149 stores.
150 If it is only loaded, then it is independent
151 on all stores in the loop. */
152 bitmap dep_loop; /* The complement of INDEP_LOOP. */
154 bitmap indep_ref; /* The set of memory references on that
155 this reference is independent. */
156 bitmap dep_ref; /* The complement of DEP_REF. */
157 } *mem_ref_p;
159 DEF_VEC_P(mem_ref_p);
160 DEF_VEC_ALLOC_P(mem_ref_p, heap);
162 DEF_VEC_P(bitmap);
163 DEF_VEC_ALLOC_P(bitmap, heap);
165 DEF_VEC_P(htab_t);
166 DEF_VEC_ALLOC_P(htab_t, heap);
168 /* Description of memory accesses in loops. */
170 static struct
172 /* The hash table of memory references accessed in loops. */
173 htab_t refs;
175 /* The list of memory references. */
176 VEC (mem_ref_p, heap) *refs_list;
178 /* The set of memory references accessed in each loop. */
179 VEC (bitmap, heap) *refs_in_loop;
181 /* The set of memory references accessed in each loop, including
182 subloops. */
183 VEC (bitmap, heap) *all_refs_in_loop;
185 /* The set of virtual operands clobbered in a given loop. */
186 VEC (bitmap, heap) *clobbered_vops;
188 /* Map from the pair (loop, virtual operand) to the set of refs that
189 touch the virtual operand in the loop. */
190 VEC (htab_t, heap) *vop_ref_map;
192 /* Cache for expanding memory addresses. */
193 struct pointer_map_t *ttae_cache;
194 } memory_accesses;
196 static bool ref_indep_loop_p (struct loop *, mem_ref_p);
198 /* Minimum cost of an expensive expression. */
199 #define LIM_EXPENSIVE ((unsigned) PARAM_VALUE (PARAM_LIM_EXPENSIVE))
201 /* The outermost loop for that execution of the header guarantees that the
202 block will be executed. */
203 #define ALWAYS_EXECUTED_IN(BB) ((struct loop *) (BB)->aux)
205 /* Calls CBCK for each index in memory reference ADDR_P. There are two
206 kinds situations handled; in each of these cases, the memory reference
207 and DATA are passed to the callback:
209 Access to an array: ARRAY_{RANGE_}REF (base, index). In this case we also
210 pass the pointer to the index to the callback.
212 Pointer dereference: INDIRECT_REF (addr). In this case we also pass the
213 pointer to addr to the callback.
215 If the callback returns false, the whole search stops and false is returned.
216 Otherwise the function returns true after traversing through the whole
217 reference *ADDR_P. */
219 bool
220 for_each_index (tree *addr_p, bool (*cbck) (tree, tree *, void *), void *data)
222 tree *nxt, *idx;
224 for (; ; addr_p = nxt)
226 switch (TREE_CODE (*addr_p))
228 case SSA_NAME:
229 return cbck (*addr_p, addr_p, data);
231 case MISALIGNED_INDIRECT_REF:
232 case ALIGN_INDIRECT_REF:
233 case INDIRECT_REF:
234 nxt = &TREE_OPERAND (*addr_p, 0);
235 return cbck (*addr_p, nxt, data);
237 case BIT_FIELD_REF:
238 case VIEW_CONVERT_EXPR:
239 case REALPART_EXPR:
240 case IMAGPART_EXPR:
241 nxt = &TREE_OPERAND (*addr_p, 0);
242 break;
244 case COMPONENT_REF:
245 /* If the component has varying offset, it behaves like index
246 as well. */
247 idx = &TREE_OPERAND (*addr_p, 2);
248 if (*idx
249 && !cbck (*addr_p, idx, data))
250 return false;
252 nxt = &TREE_OPERAND (*addr_p, 0);
253 break;
255 case ARRAY_REF:
256 case ARRAY_RANGE_REF:
257 nxt = &TREE_OPERAND (*addr_p, 0);
258 if (!cbck (*addr_p, &TREE_OPERAND (*addr_p, 1), data))
259 return false;
260 break;
262 case VAR_DECL:
263 case PARM_DECL:
264 case STRING_CST:
265 case RESULT_DECL:
266 case VECTOR_CST:
267 case COMPLEX_CST:
268 case INTEGER_CST:
269 case REAL_CST:
270 case FIXED_CST:
271 case CONSTRUCTOR:
272 return true;
274 case ADDR_EXPR:
275 gcc_assert (is_gimple_min_invariant (*addr_p));
276 return true;
278 case TARGET_MEM_REF:
279 idx = &TMR_BASE (*addr_p);
280 if (*idx
281 && !cbck (*addr_p, idx, data))
282 return false;
283 idx = &TMR_INDEX (*addr_p);
284 if (*idx
285 && !cbck (*addr_p, idx, data))
286 return false;
287 return true;
289 default:
290 gcc_unreachable ();
295 /* If it is possible to hoist the statement STMT unconditionally,
296 returns MOVE_POSSIBLE.
297 If it is possible to hoist the statement STMT, but we must avoid making
298 it executed if it would not be executed in the original program (e.g.
299 because it may trap), return MOVE_PRESERVE_EXECUTION.
300 Otherwise return MOVE_IMPOSSIBLE. */
302 enum move_pos
303 movement_possibility (tree stmt)
305 tree lhs, rhs;
307 if (flag_unswitch_loops
308 && TREE_CODE (stmt) == COND_EXPR)
310 /* If we perform unswitching, force the operands of the invariant
311 condition to be moved out of the loop. */
312 return MOVE_POSSIBLE;
315 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
316 return MOVE_IMPOSSIBLE;
318 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_DEFS))
319 return MOVE_IMPOSSIBLE;
321 if (stmt_ends_bb_p (stmt))
322 return MOVE_IMPOSSIBLE;
324 if (stmt_ann (stmt)->has_volatile_ops)
325 return MOVE_IMPOSSIBLE;
327 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
328 if (TREE_CODE (lhs) == SSA_NAME
329 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
330 return MOVE_IMPOSSIBLE;
332 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
334 if (TREE_SIDE_EFFECTS (rhs)
335 || tree_could_throw_p (rhs))
336 return MOVE_IMPOSSIBLE;
338 if (TREE_CODE (lhs) != SSA_NAME
339 || tree_could_trap_p (rhs))
340 return MOVE_PRESERVE_EXECUTION;
342 if (get_call_expr_in (stmt))
344 /* While pure or const call is guaranteed to have no side effects, we
345 cannot move it arbitrarily. Consider code like
347 char *s = something ();
349 while (1)
351 if (s)
352 t = strlen (s);
353 else
354 t = 0;
357 Here the strlen call cannot be moved out of the loop, even though
358 s is invariant. In addition to possibly creating a call with
359 invalid arguments, moving out a function call that is not executed
360 may cause performance regressions in case the call is costly and
361 not executed at all. */
362 return MOVE_PRESERVE_EXECUTION;
364 return MOVE_POSSIBLE;
367 /* Suppose that operand DEF is used inside the LOOP. Returns the outermost
368 loop to that we could move the expression using DEF if it did not have
369 other operands, i.e. the outermost loop enclosing LOOP in that the value
370 of DEF is invariant. */
372 static struct loop *
373 outermost_invariant_loop (tree def, struct loop *loop)
375 tree def_stmt;
376 basic_block def_bb;
377 struct loop *max_loop;
379 if (TREE_CODE (def) != SSA_NAME)
380 return superloop_at_depth (loop, 1);
382 def_stmt = SSA_NAME_DEF_STMT (def);
383 def_bb = bb_for_stmt (def_stmt);
384 if (!def_bb)
385 return superloop_at_depth (loop, 1);
387 max_loop = find_common_loop (loop, def_bb->loop_father);
389 if (LIM_DATA (def_stmt) && LIM_DATA (def_stmt)->max_loop)
390 max_loop = find_common_loop (max_loop,
391 loop_outer (LIM_DATA (def_stmt)->max_loop));
392 if (max_loop == loop)
393 return NULL;
394 max_loop = superloop_at_depth (loop, loop_depth (max_loop) + 1);
396 return max_loop;
399 /* Returns the outermost superloop of LOOP in that the expression EXPR is
400 invariant. */
402 static struct loop *
403 outermost_invariant_loop_expr (tree expr, struct loop *loop)
405 enum tree_code_class codeclass = TREE_CODE_CLASS (TREE_CODE (expr));
406 unsigned i, nops;
407 struct loop *max_loop = superloop_at_depth (loop, 1), *aloop;
409 if (TREE_CODE (expr) == SSA_NAME
410 || TREE_CODE (expr) == INTEGER_CST
411 || is_gimple_min_invariant (expr))
412 return outermost_invariant_loop (expr, loop);
414 if (codeclass != tcc_unary
415 && codeclass != tcc_binary
416 && codeclass != tcc_expression
417 && codeclass != tcc_vl_exp
418 && codeclass != tcc_comparison)
419 return NULL;
421 nops = TREE_OPERAND_LENGTH (expr);
422 for (i = 0; i < nops; i++)
424 aloop = outermost_invariant_loop_expr (TREE_OPERAND (expr, i), loop);
425 if (!aloop)
426 return NULL;
428 if (flow_loop_nested_p (max_loop, aloop))
429 max_loop = aloop;
432 return max_loop;
435 /* DATA is a structure containing information associated with a statement
436 inside LOOP. DEF is one of the operands of this statement.
438 Find the outermost loop enclosing LOOP in that value of DEF is invariant
439 and record this in DATA->max_loop field. If DEF itself is defined inside
440 this loop as well (i.e. we need to hoist it out of the loop if we want
441 to hoist the statement represented by DATA), record the statement in that
442 DEF is defined to the DATA->depends list. Additionally if ADD_COST is true,
443 add the cost of the computation of DEF to the DATA->cost.
445 If DEF is not invariant in LOOP, return false. Otherwise return TRUE. */
447 static bool
448 add_dependency (tree def, struct lim_aux_data *data, struct loop *loop,
449 bool add_cost)
451 tree def_stmt = SSA_NAME_DEF_STMT (def);
452 basic_block def_bb = bb_for_stmt (def_stmt);
453 struct loop *max_loop;
454 struct depend *dep;
456 if (!def_bb)
457 return true;
459 max_loop = outermost_invariant_loop (def, loop);
460 if (!max_loop)
461 return false;
463 if (flow_loop_nested_p (data->max_loop, max_loop))
464 data->max_loop = max_loop;
466 if (!LIM_DATA (def_stmt))
467 return true;
469 if (add_cost
470 /* Only add the cost if the statement defining DEF is inside LOOP,
471 i.e. if it is likely that by moving the invariants dependent
472 on it, we will be able to avoid creating a new register for
473 it (since it will be only used in these dependent invariants). */
474 && def_bb->loop_father == loop)
475 data->cost += LIM_DATA (def_stmt)->cost;
477 dep = XNEW (struct depend);
478 dep->stmt = def_stmt;
479 dep->next = data->depends;
480 data->depends = dep;
482 return true;
485 /* Returns an estimate for a cost of statement STMT. TODO -- the values here
486 are just ad-hoc constants. The estimates should be based on target-specific
487 values. */
489 static unsigned
490 stmt_cost (tree stmt)
492 tree rhs;
493 unsigned cost = 1;
495 /* Always try to create possibilities for unswitching. */
496 if (TREE_CODE (stmt) == COND_EXPR)
497 return LIM_EXPENSIVE;
499 rhs = GENERIC_TREE_OPERAND (stmt, 1);
501 /* Hoisting memory references out should almost surely be a win. */
502 if (stmt_references_memory_p (stmt))
503 cost += 20;
505 switch (TREE_CODE (rhs))
507 case CALL_EXPR:
508 /* We should be hoisting calls if possible. */
510 /* Unless the call is a builtin_constant_p; this always folds to a
511 constant, so moving it is useless. */
512 rhs = get_callee_fndecl (rhs);
513 if (DECL_BUILT_IN_CLASS (rhs) == BUILT_IN_NORMAL
514 && DECL_FUNCTION_CODE (rhs) == BUILT_IN_CONSTANT_P)
515 return 0;
517 cost += 20;
518 break;
520 case MULT_EXPR:
521 case TRUNC_DIV_EXPR:
522 case CEIL_DIV_EXPR:
523 case FLOOR_DIV_EXPR:
524 case ROUND_DIV_EXPR:
525 case EXACT_DIV_EXPR:
526 case CEIL_MOD_EXPR:
527 case FLOOR_MOD_EXPR:
528 case ROUND_MOD_EXPR:
529 case TRUNC_MOD_EXPR:
530 case RDIV_EXPR:
531 /* Division and multiplication are usually expensive. */
532 cost += 20;
533 break;
535 case LSHIFT_EXPR:
536 case RSHIFT_EXPR:
537 cost += 20;
538 break;
540 default:
541 break;
544 return cost;
547 /* Finds the outermost loop between OUTER and LOOP in that the memory reference
548 REF is independent. If REF is not independent in LOOP, NULL is returned
549 instead. */
551 static struct loop *
552 outermost_indep_loop (struct loop *outer, struct loop *loop, mem_ref_p ref)
554 struct loop *aloop;
556 if (bitmap_bit_p (ref->stored, loop->num))
557 return NULL;
559 for (aloop = outer;
560 aloop != loop;
561 aloop = superloop_at_depth (loop, loop_depth (aloop) + 1))
562 if (!bitmap_bit_p (ref->stored, aloop->num)
563 && ref_indep_loop_p (aloop, ref))
564 return aloop;
566 if (ref_indep_loop_p (loop, ref))
567 return loop;
568 else
569 return NULL;
572 /* If there is a simple load or store to a memory reference in STMT, returns
573 the location of the memory reference, and sets IS_STORE accoring to whether
574 it is a store or load. Otherwise, returns NULL. */
576 static tree *
577 simple_mem_ref_in_stmt (tree stmt, bool *is_store)
579 tree *lhs, *rhs;
581 /* Recognize MEM = (SSA_NAME | invariant) and SSA_NAME = MEM patterns. */
582 if (TREE_CODE (stmt) != GIMPLE_MODIFY_STMT)
583 return NULL;
585 lhs = &GIMPLE_STMT_OPERAND (stmt, 0);
586 rhs = &GIMPLE_STMT_OPERAND (stmt, 1);
588 if (TREE_CODE (*lhs) == SSA_NAME)
590 if (!is_gimple_addressable (*rhs))
591 return NULL;
593 *is_store = false;
594 return rhs;
596 else if (TREE_CODE (*rhs) == SSA_NAME
597 || is_gimple_min_invariant (*rhs))
599 *is_store = true;
600 return lhs;
602 else
603 return NULL;
606 /* Returns the memory reference contained in STMT. */
608 static mem_ref_p
609 mem_ref_in_stmt (tree stmt)
611 bool store;
612 tree *mem = simple_mem_ref_in_stmt (stmt, &store);
613 hashval_t hash;
614 mem_ref_p ref;
616 if (!mem)
617 return NULL;
618 gcc_assert (!store);
620 hash = iterative_hash_expr (*mem, 0);
621 ref = htab_find_with_hash (memory_accesses.refs, *mem, hash);
623 gcc_assert (ref != NULL);
624 return ref;
627 /* Determine the outermost loop to that it is possible to hoist a statement
628 STMT and store it to LIM_DATA (STMT)->max_loop. To do this we determine
629 the outermost loop in that the value computed by STMT is invariant.
630 If MUST_PRESERVE_EXEC is true, additionally choose such a loop that
631 we preserve the fact whether STMT is executed. It also fills other related
632 information to LIM_DATA (STMT).
634 The function returns false if STMT cannot be hoisted outside of the loop it
635 is defined in, and true otherwise. */
637 static bool
638 determine_max_movement (tree stmt, bool must_preserve_exec)
640 basic_block bb = bb_for_stmt (stmt);
641 struct loop *loop = bb->loop_father;
642 struct loop *level;
643 struct lim_aux_data *lim_data = LIM_DATA (stmt);
644 tree val;
645 ssa_op_iter iter;
647 if (must_preserve_exec)
648 level = ALWAYS_EXECUTED_IN (bb);
649 else
650 level = superloop_at_depth (loop, 1);
651 lim_data->max_loop = level;
653 FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_USE)
654 if (!add_dependency (val, lim_data, loop, true))
655 return false;
657 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_USES))
659 mem_ref_p ref = mem_ref_in_stmt (stmt);
661 if (ref)
663 lim_data->max_loop
664 = outermost_indep_loop (lim_data->max_loop, loop, ref);
665 if (!lim_data->max_loop)
666 return false;
668 else
670 FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_VIRTUAL_USES)
672 if (!add_dependency (val, lim_data, loop, false))
673 return false;
678 lim_data->cost += stmt_cost (stmt);
680 return true;
683 /* Suppose that some statement in ORIG_LOOP is hoisted to the loop LEVEL,
684 and that one of the operands of this statement is computed by STMT.
685 Ensure that STMT (together with all the statements that define its
686 operands) is hoisted at least out of the loop LEVEL. */
688 static void
689 set_level (tree stmt, struct loop *orig_loop, struct loop *level)
691 struct loop *stmt_loop = bb_for_stmt (stmt)->loop_father;
692 struct depend *dep;
694 stmt_loop = find_common_loop (orig_loop, stmt_loop);
695 if (LIM_DATA (stmt) && LIM_DATA (stmt)->tgt_loop)
696 stmt_loop = find_common_loop (stmt_loop,
697 loop_outer (LIM_DATA (stmt)->tgt_loop));
698 if (flow_loop_nested_p (stmt_loop, level))
699 return;
701 gcc_assert (LIM_DATA (stmt));
702 gcc_assert (level == LIM_DATA (stmt)->max_loop
703 || flow_loop_nested_p (LIM_DATA (stmt)->max_loop, level));
705 LIM_DATA (stmt)->tgt_loop = level;
706 for (dep = LIM_DATA (stmt)->depends; dep; dep = dep->next)
707 set_level (dep->stmt, orig_loop, level);
710 /* Determines an outermost loop from that we want to hoist the statement STMT.
711 For now we chose the outermost possible loop. TODO -- use profiling
712 information to set it more sanely. */
714 static void
715 set_profitable_level (tree stmt)
717 set_level (stmt, bb_for_stmt (stmt)->loop_father, LIM_DATA (stmt)->max_loop);
720 /* Returns true if STMT is not a pure call. */
722 static bool
723 nonpure_call_p (tree stmt)
725 tree call = get_call_expr_in (stmt);
727 if (!call)
728 return false;
730 return TREE_SIDE_EFFECTS (call) != 0;
733 /* Releases the memory occupied by DATA. */
735 static void
736 free_lim_aux_data (struct lim_aux_data *data)
738 struct depend *dep, *next;
740 for (dep = data->depends; dep; dep = next)
742 next = dep->next;
743 free (dep);
745 free (data);
748 /* Rewrite a/b to a*(1/b). Return the invariant stmt to process. */
750 static tree
751 rewrite_reciprocal (block_stmt_iterator *bsi)
753 tree stmt, lhs, rhs, stmt1, stmt2, var, name, tmp;
755 stmt = bsi_stmt (*bsi);
756 lhs = GENERIC_TREE_OPERAND (stmt, 0);
757 rhs = GENERIC_TREE_OPERAND (stmt, 1);
759 /* stmt must be GIMPLE_MODIFY_STMT. */
760 var = create_tmp_var (TREE_TYPE (rhs), "reciptmp");
761 add_referenced_var (var);
763 tmp = build2 (RDIV_EXPR, TREE_TYPE (rhs),
764 build_real (TREE_TYPE (rhs), dconst1),
765 TREE_OPERAND (rhs, 1));
766 stmt1 = build_gimple_modify_stmt (var, tmp);
767 name = make_ssa_name (var, stmt1);
768 GIMPLE_STMT_OPERAND (stmt1, 0) = name;
769 tmp = build2 (MULT_EXPR, TREE_TYPE (rhs),
770 name, TREE_OPERAND (rhs, 0));
771 stmt2 = build_gimple_modify_stmt (lhs, tmp);
773 /* Replace division stmt with reciprocal and multiply stmts.
774 The multiply stmt is not invariant, so update iterator
775 and avoid rescanning. */
776 bsi_replace (bsi, stmt1, true);
777 bsi_insert_after (bsi, stmt2, BSI_NEW_STMT);
778 SSA_NAME_DEF_STMT (lhs) = stmt2;
780 /* Continue processing with invariant reciprocal statement. */
781 return stmt1;
784 /* Check if the pattern at *BSI is a bittest of the form
785 (A >> B) & 1 != 0 and in this case rewrite it to A & (1 << B) != 0. */
787 static tree
788 rewrite_bittest (block_stmt_iterator *bsi)
790 tree stmt, lhs, rhs, var, name, use_stmt, stmt1, stmt2, t;
791 use_operand_p use;
793 stmt = bsi_stmt (*bsi);
794 lhs = GENERIC_TREE_OPERAND (stmt, 0);
795 rhs = GENERIC_TREE_OPERAND (stmt, 1);
797 /* Verify that the single use of lhs is a comparison against zero. */
798 if (TREE_CODE (lhs) != SSA_NAME
799 || !single_imm_use (lhs, &use, &use_stmt)
800 || TREE_CODE (use_stmt) != COND_EXPR)
801 return stmt;
802 t = COND_EXPR_COND (use_stmt);
803 if (TREE_OPERAND (t, 0) != lhs
804 || (TREE_CODE (t) != NE_EXPR
805 && TREE_CODE (t) != EQ_EXPR)
806 || !integer_zerop (TREE_OPERAND (t, 1)))
807 return stmt;
809 /* Get at the operands of the shift. The rhs is TMP1 & 1. */
810 stmt1 = SSA_NAME_DEF_STMT (TREE_OPERAND (rhs, 0));
811 if (TREE_CODE (stmt1) != GIMPLE_MODIFY_STMT)
812 return stmt;
814 /* There is a conversion in between possibly inserted by fold. */
815 t = GIMPLE_STMT_OPERAND (stmt1, 1);
816 if (CONVERT_EXPR_P (t))
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 /* Make sure to note always_executed_in for stores to make
905 store-motion work. */
906 else if (stmt_makes_single_store (stmt))
908 stmt_ann (stmt)->common.aux
909 = xcalloc (1, sizeof (struct lim_aux_data));
910 LIM_DATA (stmt)->always_executed_in = outermost;
912 continue;
915 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
917 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
919 /* If divisor is invariant, convert a/b to a*(1/b), allowing reciprocal
920 to be hoisted out of loop, saving expensive divide. */
921 if (pos == MOVE_POSSIBLE
922 && TREE_CODE (rhs) == RDIV_EXPR
923 && flag_unsafe_math_optimizations
924 && !flag_trapping_math
925 && outermost_invariant_loop_expr (TREE_OPERAND (rhs, 1),
926 loop_containing_stmt (stmt)) != NULL
927 && outermost_invariant_loop_expr (rhs,
928 loop_containing_stmt (stmt)) == NULL)
929 stmt = rewrite_reciprocal (&bsi);
931 /* If the shift count is invariant, convert (A >> B) & 1 to
932 A & (1 << B) allowing the bit mask to be hoisted out of the loop
933 saving an expensive shift. */
934 if (pos == MOVE_POSSIBLE
935 && TREE_CODE (rhs) == BIT_AND_EXPR
936 && integer_onep (TREE_OPERAND (rhs, 1))
937 && TREE_CODE (TREE_OPERAND (rhs, 0)) == SSA_NAME
938 && has_single_use (TREE_OPERAND (rhs, 0)))
939 stmt = rewrite_bittest (&bsi);
942 stmt_ann (stmt)->common.aux = xcalloc (1, sizeof (struct lim_aux_data));
943 LIM_DATA (stmt)->always_executed_in = outermost;
945 if (maybe_never && pos == MOVE_PRESERVE_EXECUTION)
946 continue;
948 if (!determine_max_movement (stmt, pos == MOVE_PRESERVE_EXECUTION))
950 LIM_DATA (stmt)->max_loop = NULL;
951 continue;
954 if (dump_file && (dump_flags & TDF_DETAILS))
956 print_generic_stmt_indented (dump_file, stmt, 0, 2);
957 fprintf (dump_file, " invariant up to level %d, cost %d.\n\n",
958 loop_depth (LIM_DATA (stmt)->max_loop),
959 LIM_DATA (stmt)->cost);
962 if (LIM_DATA (stmt)->cost >= LIM_EXPENSIVE)
963 set_profitable_level (stmt);
967 /* For each statement determines the outermost loop in that it is invariant,
968 statements on whose motion it depends and the cost of the computation.
969 This information is stored to the LIM_DATA structure associated with
970 each statement. */
972 static void
973 determine_invariantness (void)
975 struct dom_walk_data walk_data;
977 memset (&walk_data, 0, sizeof (struct dom_walk_data));
978 walk_data.dom_direction = CDI_DOMINATORS;
979 walk_data.before_dom_children_before_stmts = determine_invariantness_stmt;
981 init_walk_dominator_tree (&walk_data);
982 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
983 fini_walk_dominator_tree (&walk_data);
986 /* Hoist the statements in basic block BB out of the loops prescribed by
987 data stored in LIM_DATA structures associated with each statement. Callback
988 for walk_dominator_tree. */
990 static void
991 move_computations_stmt (struct dom_walk_data *dw_data ATTRIBUTE_UNUSED,
992 basic_block bb)
994 struct loop *level;
995 block_stmt_iterator bsi;
996 tree stmt;
997 unsigned cost = 0;
999 if (!loop_outer (bb->loop_father))
1000 return;
1002 for (bsi = bsi_start (bb); !bsi_end_p (bsi); )
1004 stmt = bsi_stmt (bsi);
1006 if (!LIM_DATA (stmt))
1008 bsi_next (&bsi);
1009 continue;
1012 cost = LIM_DATA (stmt)->cost;
1013 level = LIM_DATA (stmt)->tgt_loop;
1014 free_lim_aux_data (LIM_DATA (stmt));
1015 stmt_ann (stmt)->common.aux = NULL;
1017 if (!level)
1019 bsi_next (&bsi);
1020 continue;
1023 /* We do not really want to move conditionals out of the loop; we just
1024 placed it here to force its operands to be moved if necessary. */
1025 if (TREE_CODE (stmt) == COND_EXPR)
1026 continue;
1028 if (dump_file && (dump_flags & TDF_DETAILS))
1030 fprintf (dump_file, "Moving statement\n");
1031 print_generic_stmt (dump_file, stmt, 0);
1032 fprintf (dump_file, "(cost %u) out of loop %d.\n\n",
1033 cost, level->num);
1036 mark_virtual_ops_for_renaming (stmt);
1037 bsi_insert_on_edge (loop_preheader_edge (level), stmt);
1038 bsi_remove (&bsi, false);
1042 /* Hoist the statements out of the loops prescribed by data stored in
1043 LIM_DATA structures associated with each statement.*/
1045 static void
1046 move_computations (void)
1048 struct dom_walk_data walk_data;
1050 memset (&walk_data, 0, sizeof (struct dom_walk_data));
1051 walk_data.dom_direction = CDI_DOMINATORS;
1052 walk_data.before_dom_children_before_stmts = move_computations_stmt;
1054 init_walk_dominator_tree (&walk_data);
1055 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1056 fini_walk_dominator_tree (&walk_data);
1058 bsi_commit_edge_inserts ();
1059 if (need_ssa_update_p ())
1060 rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
1063 /* Checks whether the statement defining variable *INDEX can be hoisted
1064 out of the loop passed in DATA. Callback for for_each_index. */
1066 static bool
1067 may_move_till (tree ref, tree *index, void *data)
1069 struct loop *loop = (struct loop*) data, *max_loop;
1071 /* If REF is an array reference, check also that the step and the lower
1072 bound is invariant in LOOP. */
1073 if (TREE_CODE (ref) == ARRAY_REF)
1075 tree step = array_ref_element_size (ref);
1076 tree lbound = array_ref_low_bound (ref);
1078 max_loop = outermost_invariant_loop_expr (step, loop);
1079 if (!max_loop)
1080 return false;
1082 max_loop = outermost_invariant_loop_expr (lbound, loop);
1083 if (!max_loop)
1084 return false;
1087 max_loop = outermost_invariant_loop (*index, loop);
1088 if (!max_loop)
1089 return false;
1091 return true;
1094 /* Forces statements defining (invariant) SSA names in expression EXPR to be
1095 moved out of the LOOP. ORIG_LOOP is the loop in that EXPR is used. */
1097 static void
1098 force_move_till_expr (tree expr, struct loop *orig_loop, struct loop *loop)
1100 enum tree_code_class codeclass = TREE_CODE_CLASS (TREE_CODE (expr));
1101 unsigned i, nops;
1103 if (TREE_CODE (expr) == SSA_NAME)
1105 tree stmt = SSA_NAME_DEF_STMT (expr);
1106 if (IS_EMPTY_STMT (stmt))
1107 return;
1109 set_level (stmt, orig_loop, loop);
1110 return;
1113 if (codeclass != tcc_unary
1114 && codeclass != tcc_binary
1115 && codeclass != tcc_expression
1116 && codeclass != tcc_vl_exp
1117 && codeclass != tcc_comparison)
1118 return;
1120 nops = TREE_OPERAND_LENGTH (expr);
1121 for (i = 0; i < nops; i++)
1122 force_move_till_expr (TREE_OPERAND (expr, i), orig_loop, loop);
1125 /* Forces statement defining invariants in REF (and *INDEX) to be moved out of
1126 the LOOP. The reference REF is used in the loop ORIG_LOOP. Callback for
1127 for_each_index. */
1129 struct fmt_data
1131 struct loop *loop;
1132 struct loop *orig_loop;
1135 static bool
1136 force_move_till (tree ref, tree *index, void *data)
1138 tree stmt;
1139 struct fmt_data *fmt_data = (struct fmt_data *) data;
1141 if (TREE_CODE (ref) == ARRAY_REF)
1143 tree step = array_ref_element_size (ref);
1144 tree lbound = array_ref_low_bound (ref);
1146 force_move_till_expr (step, fmt_data->orig_loop, fmt_data->loop);
1147 force_move_till_expr (lbound, fmt_data->orig_loop, fmt_data->loop);
1150 if (TREE_CODE (*index) != SSA_NAME)
1151 return true;
1153 stmt = SSA_NAME_DEF_STMT (*index);
1154 if (IS_EMPTY_STMT (stmt))
1155 return true;
1157 set_level (stmt, fmt_data->orig_loop, fmt_data->loop);
1159 return true;
1162 /* A hash function for struct mem_ref object OBJ. */
1164 static hashval_t
1165 memref_hash (const void *obj)
1167 const struct mem_ref *mem = obj;
1169 return mem->hash;
1172 /* An equality function for struct mem_ref object OBJ1 with
1173 memory reference OBJ2. */
1175 static int
1176 memref_eq (const void *obj1, const void *obj2)
1178 const struct mem_ref *mem1 = obj1;
1180 return operand_equal_p (mem1->mem, (tree) obj2, 0);
1183 /* Releases list of memory reference locations ACCS. */
1185 static void
1186 free_mem_ref_locs (mem_ref_locs_p accs)
1188 unsigned i;
1189 mem_ref_loc_p loc;
1191 if (!accs)
1192 return;
1194 for (i = 0; VEC_iterate (mem_ref_loc_p, accs->locs, i, loc); i++)
1195 free (loc);
1196 VEC_free (mem_ref_loc_p, heap, accs->locs);
1197 free (accs);
1200 /* A function to free the mem_ref object OBJ. */
1202 static void
1203 memref_free (void *obj)
1205 struct mem_ref *mem = obj;
1206 unsigned i;
1207 mem_ref_locs_p accs;
1209 BITMAP_FREE (mem->stored);
1210 BITMAP_FREE (mem->indep_loop);
1211 BITMAP_FREE (mem->dep_loop);
1212 BITMAP_FREE (mem->indep_ref);
1213 BITMAP_FREE (mem->dep_ref);
1215 for (i = 0; VEC_iterate (mem_ref_locs_p, mem->accesses_in_loop, i, accs); i++)
1216 free_mem_ref_locs (accs);
1217 VEC_free (mem_ref_locs_p, heap, mem->accesses_in_loop);
1219 BITMAP_FREE (mem->vops);
1220 free (mem);
1223 /* Allocates and returns a memory reference description for MEM whose hash
1224 value is HASH and id is ID. */
1226 static mem_ref_p
1227 mem_ref_alloc (tree mem, unsigned hash, unsigned id)
1229 mem_ref_p ref = XNEW (struct mem_ref);
1230 ref->mem = mem;
1231 ref->id = id;
1232 ref->hash = hash;
1233 ref->stored = BITMAP_ALLOC (NULL);
1234 ref->indep_loop = BITMAP_ALLOC (NULL);
1235 ref->dep_loop = BITMAP_ALLOC (NULL);
1236 ref->indep_ref = BITMAP_ALLOC (NULL);
1237 ref->dep_ref = BITMAP_ALLOC (NULL);
1238 ref->accesses_in_loop = NULL;
1239 ref->vops = BITMAP_ALLOC (NULL);
1241 return ref;
1244 /* Allocates and returns the new list of locations. */
1246 static mem_ref_locs_p
1247 mem_ref_locs_alloc (void)
1249 mem_ref_locs_p accs = XNEW (struct mem_ref_locs);
1250 accs->locs = NULL;
1251 return accs;
1254 /* Records memory reference location *LOC in LOOP to the memory reference
1255 description REF. The reference occurs in statement STMT. */
1257 static void
1258 record_mem_ref_loc (mem_ref_p ref, struct loop *loop, tree stmt, tree *loc)
1260 mem_ref_loc_p aref = XNEW (struct mem_ref_loc);
1261 mem_ref_locs_p accs;
1262 bitmap ril = VEC_index (bitmap, memory_accesses.refs_in_loop, loop->num);
1264 if (VEC_length (mem_ref_locs_p, ref->accesses_in_loop)
1265 <= (unsigned) loop->num)
1266 VEC_safe_grow_cleared (mem_ref_locs_p, heap, ref->accesses_in_loop,
1267 loop->num + 1);
1268 accs = VEC_index (mem_ref_locs_p, ref->accesses_in_loop, loop->num);
1269 if (!accs)
1271 accs = mem_ref_locs_alloc ();
1272 VEC_replace (mem_ref_locs_p, ref->accesses_in_loop, loop->num, accs);
1275 aref->stmt = stmt;
1276 aref->ref = loc;
1278 VEC_safe_push (mem_ref_loc_p, heap, accs->locs, aref);
1279 bitmap_set_bit (ril, ref->id);
1282 /* Marks reference REF as stored in LOOP. */
1284 static void
1285 mark_ref_stored (mem_ref_p ref, struct loop *loop)
1287 for (;
1288 loop != current_loops->tree_root
1289 && !bitmap_bit_p (ref->stored, loop->num);
1290 loop = loop_outer (loop))
1291 bitmap_set_bit (ref->stored, loop->num);
1294 /* Gathers memory references in statement STMT in LOOP, storing the
1295 information about them in the memory_accesses structure. Marks
1296 the vops accessed through unrecognized statements there as
1297 well. */
1299 static void
1300 gather_mem_refs_stmt (struct loop *loop, tree stmt)
1302 tree *mem = NULL;
1303 hashval_t hash;
1304 PTR *slot;
1305 mem_ref_p ref;
1306 ssa_op_iter oi;
1307 tree vname;
1308 bool is_stored;
1309 bitmap clvops;
1310 unsigned id;
1312 if (ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
1313 return;
1315 mem = simple_mem_ref_in_stmt (stmt, &is_stored);
1316 if (!mem)
1317 goto fail;
1319 hash = iterative_hash_expr (*mem, 0);
1320 slot = htab_find_slot_with_hash (memory_accesses.refs, *mem, hash, INSERT);
1322 if (*slot)
1324 ref = *slot;
1325 id = ref->id;
1327 else
1329 id = VEC_length (mem_ref_p, memory_accesses.refs_list);
1330 ref = mem_ref_alloc (*mem, hash, id);
1331 VEC_safe_push (mem_ref_p, heap, memory_accesses.refs_list, ref);
1332 *slot = ref;
1334 if (dump_file && (dump_flags & TDF_DETAILS))
1336 fprintf (dump_file, "Memory reference %u: ", id);
1337 print_generic_expr (dump_file, ref->mem, TDF_SLIM);
1338 fprintf (dump_file, "\n");
1341 if (is_stored)
1342 mark_ref_stored (ref, loop);
1344 FOR_EACH_SSA_TREE_OPERAND (vname, stmt, oi, SSA_OP_VIRTUAL_USES)
1345 bitmap_set_bit (ref->vops, DECL_UID (SSA_NAME_VAR (vname)));
1346 record_mem_ref_loc (ref, loop, stmt, mem);
1347 return;
1349 fail:
1350 clvops = VEC_index (bitmap, memory_accesses.clobbered_vops, loop->num);
1351 FOR_EACH_SSA_TREE_OPERAND (vname, stmt, oi, SSA_OP_VIRTUAL_USES)
1352 bitmap_set_bit (clvops, DECL_UID (SSA_NAME_VAR (vname)));
1355 /* Gathers memory references in loops. */
1357 static void
1358 gather_mem_refs_in_loops (void)
1360 block_stmt_iterator bsi;
1361 basic_block bb;
1362 struct loop *loop;
1363 loop_iterator li;
1364 bitmap clvo, clvi;
1365 bitmap lrefs, alrefs, alrefso;
1367 FOR_EACH_BB (bb)
1369 loop = bb->loop_father;
1370 if (loop == current_loops->tree_root)
1371 continue;
1373 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
1374 gather_mem_refs_stmt (loop, bsi_stmt (bsi));
1377 /* Propagate the information about clobbered vops and accessed memory
1378 references up the loop hierarchy. */
1379 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
1381 lrefs = VEC_index (bitmap, memory_accesses.refs_in_loop, loop->num);
1382 alrefs = VEC_index (bitmap, memory_accesses.all_refs_in_loop, loop->num);
1383 bitmap_ior_into (alrefs, lrefs);
1385 if (loop_outer (loop) == current_loops->tree_root)
1386 continue;
1388 clvi = VEC_index (bitmap, memory_accesses.clobbered_vops, loop->num);
1389 clvo = VEC_index (bitmap, memory_accesses.clobbered_vops,
1390 loop_outer (loop)->num);
1391 bitmap_ior_into (clvo, clvi);
1393 alrefso = VEC_index (bitmap, memory_accesses.all_refs_in_loop,
1394 loop_outer (loop)->num);
1395 bitmap_ior_into (alrefso, alrefs);
1399 /* Element of the hash table that maps vops to memory references. */
1401 struct vop_to_refs_elt
1403 /* DECL_UID of the vop. */
1404 unsigned uid;
1406 /* List of the all references. */
1407 bitmap refs_all;
1409 /* List of stored references. */
1410 bitmap refs_stored;
1413 /* A hash function for struct vop_to_refs_elt object OBJ. */
1415 static hashval_t
1416 vtoe_hash (const void *obj)
1418 const struct vop_to_refs_elt *vtoe = obj;
1420 return vtoe->uid;
1423 /* An equality function for struct vop_to_refs_elt object OBJ1 with
1424 uid of a vop OBJ2. */
1426 static int
1427 vtoe_eq (const void *obj1, const void *obj2)
1429 const struct vop_to_refs_elt *vtoe = obj1;
1430 const unsigned *uid = obj2;
1432 return vtoe->uid == *uid;
1435 /* A function to free the struct vop_to_refs_elt object. */
1437 static void
1438 vtoe_free (void *obj)
1440 struct vop_to_refs_elt *vtoe = obj;
1442 BITMAP_FREE (vtoe->refs_all);
1443 BITMAP_FREE (vtoe->refs_stored);
1444 free (vtoe);
1447 /* Records REF to hashtable VOP_TO_REFS for the index VOP. STORED is true
1448 if the reference REF is stored. */
1450 static void
1451 record_vop_access (htab_t vop_to_refs, unsigned vop, unsigned ref, bool stored)
1453 void **slot = htab_find_slot_with_hash (vop_to_refs, &vop, vop, INSERT);
1454 struct vop_to_refs_elt *vtoe;
1456 if (!*slot)
1458 vtoe = XNEW (struct vop_to_refs_elt);
1459 vtoe->uid = vop;
1460 vtoe->refs_all = BITMAP_ALLOC (NULL);
1461 vtoe->refs_stored = BITMAP_ALLOC (NULL);
1462 *slot = vtoe;
1464 else
1465 vtoe = *slot;
1467 bitmap_set_bit (vtoe->refs_all, ref);
1468 if (stored)
1469 bitmap_set_bit (vtoe->refs_stored, ref);
1472 /* Returns the set of references that access VOP according to the table
1473 VOP_TO_REFS. */
1475 static bitmap
1476 get_vop_accesses (htab_t vop_to_refs, unsigned vop)
1478 struct vop_to_refs_elt *vtoe = htab_find_with_hash (vop_to_refs, &vop, vop);
1479 return vtoe->refs_all;
1482 /* Returns the set of stores that access VOP according to the table
1483 VOP_TO_REFS. */
1485 static bitmap
1486 get_vop_stores (htab_t vop_to_refs, unsigned vop)
1488 struct vop_to_refs_elt *vtoe = htab_find_with_hash (vop_to_refs, &vop, vop);
1489 return vtoe->refs_stored;
1492 /* Adds REF to mapping from virtual operands to references in LOOP. */
1494 static void
1495 add_vop_ref_mapping (struct loop *loop, mem_ref_p ref)
1497 htab_t map = VEC_index (htab_t, memory_accesses.vop_ref_map, loop->num);
1498 bool stored = bitmap_bit_p (ref->stored, loop->num);
1499 bitmap clobbers = VEC_index (bitmap, memory_accesses.clobbered_vops,
1500 loop->num);
1501 bitmap_iterator bi;
1502 unsigned vop;
1504 EXECUTE_IF_AND_COMPL_IN_BITMAP (ref->vops, clobbers, 0, vop, bi)
1506 record_vop_access (map, vop, ref->id, stored);
1510 /* Create a mapping from virtual operands to references that touch them
1511 in LOOP. */
1513 static void
1514 create_vop_ref_mapping_loop (struct loop *loop)
1516 bitmap refs = VEC_index (bitmap, memory_accesses.refs_in_loop, loop->num);
1517 struct loop *sloop;
1518 bitmap_iterator bi;
1519 unsigned i;
1520 mem_ref_p ref;
1522 EXECUTE_IF_SET_IN_BITMAP (refs, 0, i, bi)
1524 ref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
1525 for (sloop = loop; sloop != current_loops->tree_root; sloop = loop_outer (sloop))
1526 add_vop_ref_mapping (sloop, ref);
1530 /* For each non-clobbered virtual operand and each loop, record the memory
1531 references in this loop that touch the operand. */
1533 static void
1534 create_vop_ref_mapping (void)
1536 loop_iterator li;
1537 struct loop *loop;
1539 FOR_EACH_LOOP (li, loop, 0)
1541 create_vop_ref_mapping_loop (loop);
1545 /* Gathers information about memory accesses in the loops. */
1547 static void
1548 analyze_memory_references (void)
1550 unsigned i;
1551 bitmap empty;
1552 htab_t hempty;
1554 memory_accesses.refs
1555 = htab_create (100, memref_hash, memref_eq, memref_free);
1556 memory_accesses.refs_list = NULL;
1557 memory_accesses.refs_in_loop = VEC_alloc (bitmap, heap,
1558 number_of_loops ());
1559 memory_accesses.all_refs_in_loop = VEC_alloc (bitmap, heap,
1560 number_of_loops ());
1561 memory_accesses.clobbered_vops = VEC_alloc (bitmap, heap,
1562 number_of_loops ());
1563 memory_accesses.vop_ref_map = VEC_alloc (htab_t, heap,
1564 number_of_loops ());
1566 for (i = 0; i < number_of_loops (); i++)
1568 empty = BITMAP_ALLOC (NULL);
1569 VEC_quick_push (bitmap, memory_accesses.refs_in_loop, empty);
1570 empty = BITMAP_ALLOC (NULL);
1571 VEC_quick_push (bitmap, memory_accesses.all_refs_in_loop, empty);
1572 empty = BITMAP_ALLOC (NULL);
1573 VEC_quick_push (bitmap, memory_accesses.clobbered_vops, empty);
1574 hempty = htab_create (10, vtoe_hash, vtoe_eq, vtoe_free);
1575 VEC_quick_push (htab_t, memory_accesses.vop_ref_map, hempty);
1578 memory_accesses.ttae_cache = NULL;
1580 gather_mem_refs_in_loops ();
1581 create_vop_ref_mapping ();
1584 /* Returns true if a region of size SIZE1 at position 0 and a region of
1585 size SIZE2 at position DIFF cannot overlap. */
1587 static bool
1588 cannot_overlap_p (aff_tree *diff, double_int size1, double_int size2)
1590 double_int d, bound;
1592 /* Unless the difference is a constant, we fail. */
1593 if (diff->n != 0)
1594 return false;
1596 d = diff->offset;
1597 if (double_int_negative_p (d))
1599 /* The second object is before the first one, we succeed if the last
1600 element of the second object is before the start of the first one. */
1601 bound = double_int_add (d, double_int_add (size2, double_int_minus_one));
1602 return double_int_negative_p (bound);
1604 else
1606 /* We succeed if the second object starts after the first one ends. */
1607 return double_int_scmp (size1, d) <= 0;
1611 /* Returns true if MEM1 and MEM2 may alias. TTAE_CACHE is used as a cache in
1612 tree_to_aff_combination_expand. */
1614 static bool
1615 mem_refs_may_alias_p (tree mem1, tree mem2, struct pointer_map_t **ttae_cache)
1617 /* Perform BASE + OFFSET analysis -- if MEM1 and MEM2 are based on the same
1618 object and their offset differ in such a way that the locations cannot
1619 overlap, then they cannot alias. */
1620 aff_tree off1, off2;
1621 double_int size1, size2;
1622 tree base1, base2;
1624 /* If MEM1 and MEM2 are based on different variables, they cannot alias. */
1625 base1 = get_base_address (mem1);
1626 base2 = get_base_address (mem2);
1628 if (base1
1629 && !INDIRECT_REF_P (base1)
1630 && base2
1631 && !INDIRECT_REF_P (base2)
1632 && !operand_equal_p (base1, base2, 0))
1633 return false;
1635 /* With strict aliasing, it is impossible to access a scalar variable through
1636 anything but a pointer dereference or through a union (gcc extension). */
1637 if (flag_strict_aliasing)
1639 if (!INDIRECT_REF_P (mem1)
1640 && base1
1641 && TREE_CODE (TREE_TYPE (base1)) != UNION_TYPE
1642 && SSA_VAR_P (mem2)
1643 && !AGGREGATE_TYPE_P (TREE_TYPE (mem2)))
1644 return false;
1645 if (!INDIRECT_REF_P (mem2)
1646 && base2
1647 && TREE_CODE (TREE_TYPE (base2)) != UNION_TYPE
1648 && SSA_VAR_P (mem1)
1649 && !AGGREGATE_TYPE_P (TREE_TYPE (mem1)))
1650 return false;
1651 if (!alias_sets_conflict_p (get_alias_set (mem1), get_alias_set (mem2)))
1652 return false;
1655 /* The expansion of addresses may be a bit expensive, thus we only do
1656 the check at -O2 and higher optimization levels. */
1657 if (optimize < 2)
1658 return true;
1660 get_inner_reference_aff (mem1, &off1, &size1);
1661 get_inner_reference_aff (mem2, &off2, &size2);
1662 aff_combination_expand (&off1, ttae_cache);
1663 aff_combination_expand (&off2, ttae_cache);
1664 aff_combination_scale (&off1, double_int_minus_one);
1665 aff_combination_add (&off2, &off1);
1667 if (cannot_overlap_p (&off2, size1, size2))
1668 return false;
1670 return true;
1673 /* Rewrites location LOC by TMP_VAR. */
1675 static void
1676 rewrite_mem_ref_loc (mem_ref_loc_p loc, tree tmp_var)
1678 mark_virtual_ops_for_renaming (loc->stmt);
1679 *loc->ref = tmp_var;
1680 update_stmt (loc->stmt);
1683 /* Adds all locations of REF in LOOP and its subloops to LOCS. */
1685 static void
1686 get_all_locs_in_loop (struct loop *loop, mem_ref_p ref,
1687 VEC (mem_ref_loc_p, heap) **locs)
1689 mem_ref_locs_p accs;
1690 unsigned i;
1691 mem_ref_loc_p loc;
1692 bitmap refs = VEC_index (bitmap, memory_accesses.all_refs_in_loop,
1693 loop->num);
1694 struct loop *subloop;
1696 if (!bitmap_bit_p (refs, ref->id))
1697 return;
1699 if (VEC_length (mem_ref_locs_p, ref->accesses_in_loop)
1700 > (unsigned) loop->num)
1702 accs = VEC_index (mem_ref_locs_p, ref->accesses_in_loop, loop->num);
1703 if (accs)
1705 for (i = 0; VEC_iterate (mem_ref_loc_p, accs->locs, i, loc); i++)
1706 VEC_safe_push (mem_ref_loc_p, heap, *locs, loc);
1710 for (subloop = loop->inner; subloop != NULL; subloop = subloop->next)
1711 get_all_locs_in_loop (subloop, ref, locs);
1714 /* Rewrites all references to REF in LOOP by variable TMP_VAR. */
1716 static void
1717 rewrite_mem_refs (struct loop *loop, mem_ref_p ref, tree tmp_var)
1719 unsigned i;
1720 mem_ref_loc_p loc;
1721 VEC (mem_ref_loc_p, heap) *locs = NULL;
1723 get_all_locs_in_loop (loop, ref, &locs);
1724 for (i = 0; VEC_iterate (mem_ref_loc_p, locs, i, loc); i++)
1725 rewrite_mem_ref_loc (loc, tmp_var);
1726 VEC_free (mem_ref_loc_p, heap, locs);
1729 /* The name and the length of the currently generated variable
1730 for lsm. */
1731 #define MAX_LSM_NAME_LENGTH 40
1732 static char lsm_tmp_name[MAX_LSM_NAME_LENGTH + 1];
1733 static int lsm_tmp_name_length;
1735 /* Adds S to lsm_tmp_name. */
1737 static void
1738 lsm_tmp_name_add (const char *s)
1740 int l = strlen (s) + lsm_tmp_name_length;
1741 if (l > MAX_LSM_NAME_LENGTH)
1742 return;
1744 strcpy (lsm_tmp_name + lsm_tmp_name_length, s);
1745 lsm_tmp_name_length = l;
1748 /* Stores the name for temporary variable that replaces REF to
1749 lsm_tmp_name. */
1751 static void
1752 gen_lsm_tmp_name (tree ref)
1754 const char *name;
1756 switch (TREE_CODE (ref))
1758 case MISALIGNED_INDIRECT_REF:
1759 case ALIGN_INDIRECT_REF:
1760 case INDIRECT_REF:
1761 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1762 lsm_tmp_name_add ("_");
1763 break;
1765 case BIT_FIELD_REF:
1766 case VIEW_CONVERT_EXPR:
1767 case ARRAY_RANGE_REF:
1768 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1769 break;
1771 case REALPART_EXPR:
1772 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1773 lsm_tmp_name_add ("_RE");
1774 break;
1776 case IMAGPART_EXPR:
1777 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1778 lsm_tmp_name_add ("_IM");
1779 break;
1781 case COMPONENT_REF:
1782 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1783 lsm_tmp_name_add ("_");
1784 name = get_name (TREE_OPERAND (ref, 1));
1785 if (!name)
1786 name = "F";
1787 lsm_tmp_name_add ("_");
1788 lsm_tmp_name_add (name);
1790 case ARRAY_REF:
1791 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1792 lsm_tmp_name_add ("_I");
1793 break;
1795 case SSA_NAME:
1796 ref = SSA_NAME_VAR (ref);
1797 /* Fallthru. */
1799 case VAR_DECL:
1800 case PARM_DECL:
1801 name = get_name (ref);
1802 if (!name)
1803 name = "D";
1804 lsm_tmp_name_add (name);
1805 break;
1807 case STRING_CST:
1808 lsm_tmp_name_add ("S");
1809 break;
1811 case RESULT_DECL:
1812 lsm_tmp_name_add ("R");
1813 break;
1815 default:
1816 gcc_unreachable ();
1820 /* Determines name for temporary variable that replaces REF.
1821 The name is accumulated into the lsm_tmp_name variable.
1822 N is added to the name of the temporary. */
1824 char *
1825 get_lsm_tmp_name (tree ref, unsigned n)
1827 char ns[2];
1829 lsm_tmp_name_length = 0;
1830 gen_lsm_tmp_name (ref);
1831 lsm_tmp_name_add ("_lsm");
1832 if (n < 10)
1834 ns[0] = '0' + n;
1835 ns[1] = 0;
1836 lsm_tmp_name_add (ns);
1838 return lsm_tmp_name;
1841 /* Executes store motion of memory reference REF from LOOP.
1842 Exits from the LOOP are stored in EXITS. The initialization of the
1843 temporary variable is put to the preheader of the loop, and assignments
1844 to the reference from the temporary variable are emitted to exits. */
1846 static void
1847 execute_sm (struct loop *loop, VEC (edge, heap) *exits, mem_ref_p ref)
1849 tree tmp_var;
1850 unsigned i;
1851 tree load, store;
1852 struct fmt_data fmt_data;
1853 edge ex;
1855 if (dump_file && (dump_flags & TDF_DETAILS))
1857 fprintf (dump_file, "Executing store motion of ");
1858 print_generic_expr (dump_file, ref->mem, 0);
1859 fprintf (dump_file, " from loop %d\n", loop->num);
1862 tmp_var = make_rename_temp (TREE_TYPE (ref->mem),
1863 get_lsm_tmp_name (ref->mem, ~0));
1865 fmt_data.loop = loop;
1866 fmt_data.orig_loop = loop;
1867 for_each_index (&ref->mem, force_move_till, &fmt_data);
1869 rewrite_mem_refs (loop, ref, tmp_var);
1871 /* Emit the load & stores. */
1872 load = build_gimple_modify_stmt (tmp_var, unshare_expr (ref->mem));
1873 get_stmt_ann (load)->common.aux = xcalloc (1, sizeof (struct lim_aux_data));
1874 LIM_DATA (load)->max_loop = loop;
1875 LIM_DATA (load)->tgt_loop = loop;
1877 /* Put this into the latch, so that we are sure it will be processed after
1878 all dependencies. */
1879 bsi_insert_on_edge (loop_latch_edge (loop), load);
1881 for (i = 0; VEC_iterate (edge, exits, i, ex); i++)
1883 store = build_gimple_modify_stmt (unshare_expr (ref->mem), tmp_var);
1884 bsi_insert_on_edge (ex, store);
1888 /* Hoists memory references MEM_REFS out of LOOP. EXITS is the list of exit
1889 edges of the LOOP. */
1891 static void
1892 hoist_memory_references (struct loop *loop, bitmap mem_refs,
1893 VEC (edge, heap) *exits)
1895 mem_ref_p ref;
1896 unsigned i;
1897 bitmap_iterator bi;
1899 EXECUTE_IF_SET_IN_BITMAP (mem_refs, 0, i, bi)
1901 ref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
1902 execute_sm (loop, exits, ref);
1906 /* Returns true if REF is always accessed in LOOP. */
1908 static bool
1909 ref_always_accessed_p (struct loop *loop, mem_ref_p ref)
1911 VEC (mem_ref_loc_p, heap) *locs = NULL;
1912 unsigned i;
1913 mem_ref_loc_p loc;
1914 bool ret = false;
1915 struct loop *must_exec;
1917 get_all_locs_in_loop (loop, ref, &locs);
1918 for (i = 0; VEC_iterate (mem_ref_loc_p, locs, i, loc); i++)
1920 if (!LIM_DATA (loc->stmt))
1921 continue;
1923 must_exec = LIM_DATA (loc->stmt)->always_executed_in;
1924 if (!must_exec)
1925 continue;
1927 if (must_exec == loop
1928 || flow_loop_nested_p (must_exec, loop))
1930 ret = true;
1931 break;
1934 VEC_free (mem_ref_loc_p, heap, locs);
1936 return ret;
1939 /* Returns true if REF1 and REF2 are independent. */
1941 static bool
1942 refs_independent_p (mem_ref_p ref1, mem_ref_p ref2)
1944 if (ref1 == ref2
1945 || bitmap_bit_p (ref1->indep_ref, ref2->id))
1946 return true;
1947 if (bitmap_bit_p (ref1->dep_ref, ref2->id))
1948 return false;
1950 if (dump_file && (dump_flags & TDF_DETAILS))
1951 fprintf (dump_file, "Querying dependency of refs %u and %u: ",
1952 ref1->id, ref2->id);
1954 if (mem_refs_may_alias_p (ref1->mem, ref2->mem,
1955 &memory_accesses.ttae_cache))
1957 bitmap_set_bit (ref1->dep_ref, ref2->id);
1958 bitmap_set_bit (ref2->dep_ref, ref1->id);
1959 if (dump_file && (dump_flags & TDF_DETAILS))
1960 fprintf (dump_file, "dependent.\n");
1961 return false;
1963 else
1965 bitmap_set_bit (ref1->indep_ref, ref2->id);
1966 bitmap_set_bit (ref2->indep_ref, ref1->id);
1967 if (dump_file && (dump_flags & TDF_DETAILS))
1968 fprintf (dump_file, "independent.\n");
1969 return true;
1973 /* Records the information whether REF is independent in LOOP (according
1974 to INDEP). */
1976 static void
1977 record_indep_loop (struct loop *loop, mem_ref_p ref, bool indep)
1979 if (indep)
1980 bitmap_set_bit (ref->indep_loop, loop->num);
1981 else
1982 bitmap_set_bit (ref->dep_loop, loop->num);
1985 /* Returns true if REF is independent on all other memory references in
1986 LOOP. */
1988 static bool
1989 ref_indep_loop_p_1 (struct loop *loop, mem_ref_p ref)
1991 bitmap clobbers, refs_to_check, refs;
1992 unsigned i;
1993 bitmap_iterator bi;
1994 bool ret = true, stored = bitmap_bit_p (ref->stored, loop->num);
1995 htab_t map;
1996 mem_ref_p aref;
1998 /* If the reference is clobbered, it is not independent. */
1999 clobbers = VEC_index (bitmap, memory_accesses.clobbered_vops, loop->num);
2000 if (bitmap_intersect_p (ref->vops, clobbers))
2001 return false;
2003 refs_to_check = BITMAP_ALLOC (NULL);
2005 map = VEC_index (htab_t, memory_accesses.vop_ref_map, loop->num);
2006 EXECUTE_IF_AND_COMPL_IN_BITMAP (ref->vops, clobbers, 0, i, bi)
2008 if (stored)
2009 refs = get_vop_accesses (map, i);
2010 else
2011 refs = get_vop_stores (map, i);
2013 bitmap_ior_into (refs_to_check, refs);
2016 EXECUTE_IF_SET_IN_BITMAP (refs_to_check, 0, i, bi)
2018 aref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
2019 if (!refs_independent_p (ref, aref))
2021 ret = false;
2022 record_indep_loop (loop, aref, false);
2023 break;
2027 BITMAP_FREE (refs_to_check);
2028 return ret;
2031 /* Returns true if REF is independent on all other memory references in
2032 LOOP. Wrapper over ref_indep_loop_p_1, caching its results. */
2034 static bool
2035 ref_indep_loop_p (struct loop *loop, mem_ref_p ref)
2037 bool ret;
2039 if (bitmap_bit_p (ref->indep_loop, loop->num))
2040 return true;
2041 if (bitmap_bit_p (ref->dep_loop, loop->num))
2042 return false;
2044 ret = ref_indep_loop_p_1 (loop, ref);
2046 if (dump_file && (dump_flags & TDF_DETAILS))
2047 fprintf (dump_file, "Querying dependencies of ref %u in loop %d: %s\n",
2048 ref->id, loop->num, ret ? "independent" : "dependent");
2050 record_indep_loop (loop, ref, ret);
2052 return ret;
2055 /* Returns true if we can perform store motion of REF from LOOP. */
2057 static bool
2058 can_sm_ref_p (struct loop *loop, mem_ref_p ref)
2060 /* Unless the reference is stored in the loop, there is nothing to do. */
2061 if (!bitmap_bit_p (ref->stored, loop->num))
2062 return false;
2064 /* It should be movable. */
2065 if (!is_gimple_reg_type (TREE_TYPE (ref->mem))
2066 || TREE_THIS_VOLATILE (ref->mem)
2067 || !for_each_index (&ref->mem, may_move_till, loop))
2068 return false;
2070 /* If it can trap, it must be always executed in LOOP. */
2071 if (tree_could_trap_p (ref->mem)
2072 && !ref_always_accessed_p (loop, ref))
2073 return false;
2075 /* And it must be independent on all other memory references
2076 in LOOP. */
2077 if (!ref_indep_loop_p (loop, ref))
2078 return false;
2080 return true;
2083 /* Marks the references in LOOP for that store motion should be performed
2084 in REFS_TO_SM. SM_EXECUTED is the set of references for that store
2085 motion was performed in one of the outer loops. */
2087 static void
2088 find_refs_for_sm (struct loop *loop, bitmap sm_executed, bitmap refs_to_sm)
2090 bitmap refs = VEC_index (bitmap, memory_accesses.all_refs_in_loop,
2091 loop->num);
2092 unsigned i;
2093 bitmap_iterator bi;
2094 mem_ref_p ref;
2096 EXECUTE_IF_AND_COMPL_IN_BITMAP (refs, sm_executed, 0, i, bi)
2098 ref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
2099 if (can_sm_ref_p (loop, ref))
2100 bitmap_set_bit (refs_to_sm, i);
2104 /* Checks whether LOOP (with exits stored in EXITS array) is suitable
2105 for a store motion optimization (i.e. whether we can insert statement
2106 on its exits). */
2108 static bool
2109 loop_suitable_for_sm (struct loop *loop ATTRIBUTE_UNUSED,
2110 VEC (edge, heap) *exits)
2112 unsigned i;
2113 edge ex;
2115 for (i = 0; VEC_iterate (edge, exits, i, ex); i++)
2116 if (ex->flags & EDGE_ABNORMAL)
2117 return false;
2119 return true;
2122 /* Try to perform store motion for all memory references modified inside
2123 LOOP. SM_EXECUTED is the bitmap of the memory references for that
2124 store motion was executed in one of the outer loops. */
2126 static void
2127 store_motion_loop (struct loop *loop, bitmap sm_executed)
2129 VEC (edge, heap) *exits = get_loop_exit_edges (loop);
2130 struct loop *subloop;
2131 bitmap sm_in_loop = BITMAP_ALLOC (NULL);
2133 if (loop_suitable_for_sm (loop, exits))
2135 find_refs_for_sm (loop, sm_executed, sm_in_loop);
2136 hoist_memory_references (loop, sm_in_loop, exits);
2138 VEC_free (edge, heap, exits);
2140 bitmap_ior_into (sm_executed, sm_in_loop);
2141 for (subloop = loop->inner; subloop != NULL; subloop = subloop->next)
2142 store_motion_loop (subloop, sm_executed);
2143 bitmap_and_compl_into (sm_executed, sm_in_loop);
2144 BITMAP_FREE (sm_in_loop);
2147 /* Try to perform store motion for all memory references modified inside
2148 loops. */
2150 static void
2151 store_motion (void)
2153 struct loop *loop;
2154 bitmap sm_executed = BITMAP_ALLOC (NULL);
2156 for (loop = current_loops->tree_root->inner; loop != NULL; loop = loop->next)
2157 store_motion_loop (loop, sm_executed);
2159 BITMAP_FREE (sm_executed);
2160 bsi_commit_edge_inserts ();
2163 /* Fills ALWAYS_EXECUTED_IN information for basic blocks of LOOP, i.e.
2164 for each such basic block bb records the outermost loop for that execution
2165 of its header implies execution of bb. CONTAINS_CALL is the bitmap of
2166 blocks that contain a nonpure call. */
2168 static void
2169 fill_always_executed_in (struct loop *loop, sbitmap contains_call)
2171 basic_block bb = NULL, *bbs, last = NULL;
2172 unsigned i;
2173 edge e;
2174 struct loop *inn_loop = loop;
2176 if (!loop->header->aux)
2178 bbs = get_loop_body_in_dom_order (loop);
2180 for (i = 0; i < loop->num_nodes; i++)
2182 edge_iterator ei;
2183 bb = bbs[i];
2185 if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
2186 last = bb;
2188 if (TEST_BIT (contains_call, bb->index))
2189 break;
2191 FOR_EACH_EDGE (e, ei, bb->succs)
2192 if (!flow_bb_inside_loop_p (loop, e->dest))
2193 break;
2194 if (e)
2195 break;
2197 /* A loop might be infinite (TODO use simple loop analysis
2198 to disprove this if possible). */
2199 if (bb->flags & BB_IRREDUCIBLE_LOOP)
2200 break;
2202 if (!flow_bb_inside_loop_p (inn_loop, bb))
2203 break;
2205 if (bb->loop_father->header == bb)
2207 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
2208 break;
2210 /* In a loop that is always entered we may proceed anyway.
2211 But record that we entered it and stop once we leave it. */
2212 inn_loop = bb->loop_father;
2216 while (1)
2218 last->aux = loop;
2219 if (last == loop->header)
2220 break;
2221 last = get_immediate_dominator (CDI_DOMINATORS, last);
2224 free (bbs);
2227 for (loop = loop->inner; loop; loop = loop->next)
2228 fill_always_executed_in (loop, contains_call);
2231 /* Compute the global information needed by the loop invariant motion pass. */
2233 static void
2234 tree_ssa_lim_initialize (void)
2236 sbitmap contains_call = sbitmap_alloc (last_basic_block);
2237 block_stmt_iterator bsi;
2238 struct loop *loop;
2239 basic_block bb;
2241 sbitmap_zero (contains_call);
2242 FOR_EACH_BB (bb)
2244 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2246 if (nonpure_call_p (bsi_stmt (bsi)))
2247 break;
2250 if (!bsi_end_p (bsi))
2251 SET_BIT (contains_call, bb->index);
2254 for (loop = current_loops->tree_root->inner; loop; loop = loop->next)
2255 fill_always_executed_in (loop, contains_call);
2257 sbitmap_free (contains_call);
2260 /* Cleans up after the invariant motion pass. */
2262 static void
2263 tree_ssa_lim_finalize (void)
2265 basic_block bb;
2266 unsigned i;
2267 bitmap b;
2268 htab_t h;
2270 FOR_EACH_BB (bb)
2272 bb->aux = NULL;
2275 VEC_free (mem_ref_p, heap, memory_accesses.refs_list);
2276 htab_delete (memory_accesses.refs);
2278 for (i = 0; VEC_iterate (bitmap, memory_accesses.refs_in_loop, i, b); i++)
2279 BITMAP_FREE (b);
2280 VEC_free (bitmap, heap, memory_accesses.refs_in_loop);
2282 for (i = 0; VEC_iterate (bitmap, memory_accesses.all_refs_in_loop, i, b); i++)
2283 BITMAP_FREE (b);
2284 VEC_free (bitmap, heap, memory_accesses.all_refs_in_loop);
2286 for (i = 0; VEC_iterate (bitmap, memory_accesses.clobbered_vops, i, b); i++)
2287 BITMAP_FREE (b);
2288 VEC_free (bitmap, heap, memory_accesses.clobbered_vops);
2290 for (i = 0; VEC_iterate (htab_t, memory_accesses.vop_ref_map, i, h); i++)
2291 htab_delete (h);
2292 VEC_free (htab_t, heap, memory_accesses.vop_ref_map);
2294 if (memory_accesses.ttae_cache)
2295 pointer_map_destroy (memory_accesses.ttae_cache);
2298 /* Moves invariants from loops. Only "expensive" invariants are moved out --
2299 i.e. those that are likely to be win regardless of the register pressure. */
2301 void
2302 tree_ssa_lim (void)
2304 tree_ssa_lim_initialize ();
2306 /* Gathers information about memory accesses in the loops. */
2307 analyze_memory_references ();
2309 /* For each statement determine the outermost loop in that it is
2310 invariant and cost for computing the invariant. */
2311 determine_invariantness ();
2313 /* Execute store motion. Force the necessary invariants to be moved
2314 out of the loops as well. */
2315 store_motion ();
2317 /* Move the expressions that are expensive enough. */
2318 move_computations ();
2320 tree_ssa_lim_finalize ();