2008-04-30 Doug Kwan <dougkwan@google.com>
[official-gcc.git] / gcc / tree-ssa-loop-im.c
blob9a6455009ac3a098611dabf823cb023ecf804f79
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 gimple 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 /* Maps statements to their lim_aux_data. */
102 static struct pointer_map_t *lim_aux_data_map;
104 /* Description of a memory reference location. */
106 typedef struct mem_ref_loc
108 tree *ref; /* The reference itself. */
109 gimple 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 static struct lim_aux_data *
205 init_lim_data (gimple stmt)
207 void **p = pointer_map_insert (lim_aux_data_map, stmt);
209 *p = XCNEW (struct lim_aux_data);
210 return *p;
213 static struct lim_aux_data *
214 get_lim_data (gimple stmt)
216 void **p = pointer_map_contains (lim_aux_data_map, stmt);
217 if (!p)
218 return NULL;
220 return *p;
223 /* Releases the memory occupied by DATA. */
225 static void
226 free_lim_aux_data (struct lim_aux_data *data)
228 struct depend *dep, *next;
230 for (dep = data->depends; dep; dep = next)
232 next = dep->next;
233 free (dep);
235 free (data);
238 static void
239 clear_lim_data (gimple stmt)
241 void **p = pointer_map_contains (lim_aux_data_map, stmt);
242 if (!p)
243 return;
245 free_lim_aux_data (*p);
246 *p = NULL;
249 /* Calls CBCK for each index in memory reference ADDR_P. There are two
250 kinds situations handled; in each of these cases, the memory reference
251 and DATA are passed to the callback:
253 Access to an array: ARRAY_{RANGE_}REF (base, index). In this case we also
254 pass the pointer to the index to the callback.
256 Pointer dereference: INDIRECT_REF (addr). In this case we also pass the
257 pointer to addr to the callback.
259 If the callback returns false, the whole search stops and false is returned.
260 Otherwise the function returns true after traversing through the whole
261 reference *ADDR_P. */
263 bool
264 for_each_index (tree *addr_p, bool (*cbck) (tree, tree *, void *), void *data)
266 tree *nxt, *idx;
268 for (; ; addr_p = nxt)
270 switch (TREE_CODE (*addr_p))
272 case SSA_NAME:
273 return cbck (*addr_p, addr_p, data);
275 case MISALIGNED_INDIRECT_REF:
276 case ALIGN_INDIRECT_REF:
277 case INDIRECT_REF:
278 nxt = &TREE_OPERAND (*addr_p, 0);
279 return cbck (*addr_p, nxt, data);
281 case BIT_FIELD_REF:
282 case VIEW_CONVERT_EXPR:
283 case REALPART_EXPR:
284 case IMAGPART_EXPR:
285 nxt = &TREE_OPERAND (*addr_p, 0);
286 break;
288 case COMPONENT_REF:
289 /* If the component has varying offset, it behaves like index
290 as well. */
291 idx = &TREE_OPERAND (*addr_p, 2);
292 if (*idx
293 && !cbck (*addr_p, idx, data))
294 return false;
296 nxt = &TREE_OPERAND (*addr_p, 0);
297 break;
299 case ARRAY_REF:
300 case ARRAY_RANGE_REF:
301 nxt = &TREE_OPERAND (*addr_p, 0);
302 if (!cbck (*addr_p, &TREE_OPERAND (*addr_p, 1), data))
303 return false;
304 break;
306 case VAR_DECL:
307 case PARM_DECL:
308 case STRING_CST:
309 case RESULT_DECL:
310 case VECTOR_CST:
311 case COMPLEX_CST:
312 case INTEGER_CST:
313 case REAL_CST:
314 case FIXED_CST:
315 case CONSTRUCTOR:
316 return true;
318 case ADDR_EXPR:
319 gcc_assert (is_gimple_min_invariant (*addr_p));
320 return true;
322 case TARGET_MEM_REF:
323 idx = &TMR_BASE (*addr_p);
324 if (*idx
325 && !cbck (*addr_p, idx, data))
326 return false;
327 idx = &TMR_INDEX (*addr_p);
328 if (*idx
329 && !cbck (*addr_p, idx, data))
330 return false;
331 return true;
333 default:
334 gcc_unreachable ();
339 /* If it is possible to hoist the statement STMT unconditionally,
340 returns MOVE_POSSIBLE.
341 If it is possible to hoist the statement STMT, but we must avoid making
342 it executed if it would not be executed in the original program (e.g.
343 because it may trap), return MOVE_PRESERVE_EXECUTION.
344 Otherwise return MOVE_IMPOSSIBLE. */
346 enum move_pos
347 movement_possibility (gimple stmt)
349 tree lhs;
350 enum move_pos ret = MOVE_POSSIBLE;
352 if (flag_unswitch_loops
353 && gimple_code (stmt) == GIMPLE_COND)
355 /* If we perform unswitching, force the operands of the invariant
356 condition to be moved out of the loop. */
357 return MOVE_POSSIBLE;
360 if (gimple_get_lhs (stmt) == NULL_TREE)
361 return MOVE_IMPOSSIBLE;
363 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_DEFS))
364 return MOVE_IMPOSSIBLE;
366 if (stmt_ends_bb_p (stmt)
367 || gimple_has_volatile_ops (stmt)
368 || gimple_has_side_effects (stmt)
369 || stmt_could_throw_p (stmt))
370 return MOVE_IMPOSSIBLE;
372 if (gimple_code (stmt) == GIMPLE_CALL)
374 /* While pure or const call is guaranteed to have no side effects, we
375 cannot move it arbitrarily. Consider code like
377 char *s = something ();
379 while (1)
381 if (s)
382 t = strlen (s);
383 else
384 t = 0;
387 Here the strlen call cannot be moved out of the loop, even though
388 s is invariant. In addition to possibly creating a call with
389 invalid arguments, moving out a function call that is not executed
390 may cause performance regressions in case the call is costly and
391 not executed at all. */
392 ret = MOVE_PRESERVE_EXECUTION;
393 lhs = gimple_call_lhs (stmt);
395 else if (gimple_code (stmt) == GIMPLE_ASSIGN)
396 lhs = gimple_assign_lhs (stmt);
397 else
398 return MOVE_IMPOSSIBLE;
400 if (TREE_CODE (lhs) == SSA_NAME
401 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
402 return MOVE_IMPOSSIBLE;
404 if (TREE_CODE (lhs) != SSA_NAME
405 || gimple_could_trap_p (stmt))
406 return MOVE_PRESERVE_EXECUTION;
408 return ret;
411 /* Suppose that operand DEF is used inside the LOOP. Returns the outermost
412 loop to that we could move the expression using DEF if it did not have
413 other operands, i.e. the outermost loop enclosing LOOP in that the value
414 of DEF is invariant. */
416 static struct loop *
417 outermost_invariant_loop (tree def, struct loop *loop)
419 gimple def_stmt;
420 basic_block def_bb;
421 struct loop *max_loop;
422 struct lim_aux_data *lim_data;
424 if (!def)
425 return superloop_at_depth (loop, 1);
427 if (TREE_CODE (def) != SSA_NAME)
429 gcc_assert (is_gimple_min_invariant (def));
430 return superloop_at_depth (loop, 1);
433 def_stmt = SSA_NAME_DEF_STMT (def);
434 def_bb = gimple_bb (def_stmt);
435 if (!def_bb)
436 return superloop_at_depth (loop, 1);
438 max_loop = find_common_loop (loop, def_bb->loop_father);
440 lim_data = get_lim_data (def_stmt);
441 if (lim_data != NULL && lim_data->max_loop != NULL)
442 max_loop = find_common_loop (max_loop,
443 loop_outer (lim_data->max_loop));
444 if (max_loop == loop)
445 return NULL;
446 max_loop = superloop_at_depth (loop, loop_depth (max_loop) + 1);
448 return max_loop;
451 /* DATA is a structure containing information associated with a statement
452 inside LOOP. DEF is one of the operands of this statement.
454 Find the outermost loop enclosing LOOP in that value of DEF is invariant
455 and record this in DATA->max_loop field. If DEF itself is defined inside
456 this loop as well (i.e. we need to hoist it out of the loop if we want
457 to hoist the statement represented by DATA), record the statement in that
458 DEF is defined to the DATA->depends list. Additionally if ADD_COST is true,
459 add the cost of the computation of DEF to the DATA->cost.
461 If DEF is not invariant in LOOP, return false. Otherwise return TRUE. */
463 static bool
464 add_dependency (tree def, struct lim_aux_data *data, struct loop *loop,
465 bool add_cost)
467 gimple def_stmt = SSA_NAME_DEF_STMT (def);
468 basic_block def_bb = gimple_bb (def_stmt);
469 struct loop *max_loop;
470 struct depend *dep;
471 struct lim_aux_data *def_data;
473 if (!def_bb)
474 return true;
476 max_loop = outermost_invariant_loop (def, loop);
477 if (!max_loop)
478 return false;
480 if (flow_loop_nested_p (data->max_loop, max_loop))
481 data->max_loop = max_loop;
483 def_data = get_lim_data (def_stmt);
484 if (!def_data)
485 return true;
487 if (add_cost
488 /* Only add the cost if the statement defining DEF is inside LOOP,
489 i.e. if it is likely that by moving the invariants dependent
490 on it, we will be able to avoid creating a new register for
491 it (since it will be only used in these dependent invariants). */
492 && def_bb->loop_father == loop)
493 data->cost += def_data->cost;
495 dep = XNEW (struct depend);
496 dep->stmt = def_stmt;
497 dep->next = data->depends;
498 data->depends = dep;
500 return true;
503 /* Returns an estimate for a cost of statement STMT. TODO -- the values here
504 are just ad-hoc constants. The estimates should be based on target-specific
505 values. */
507 static unsigned
508 stmt_cost (gimple stmt)
510 tree fndecl;
511 unsigned cost = 1;
513 /* Always try to create possibilities for unswitching. */
514 if (gimple_code (stmt) == GIMPLE_COND)
515 return LIM_EXPENSIVE;
517 /* Hoisting memory references out should almost surely be a win. */
518 if (gimple_references_memory_p (stmt))
519 cost += 20;
521 if (gimple_code (stmt) == GIMPLE_CALL)
523 /* We should be hoisting calls if possible. */
525 /* Unless the call is a builtin_constant_p; this always folds to a
526 constant, so moving it is useless. */
527 fndecl = gimple_call_fndecl (stmt);
528 if (fndecl
529 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
530 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_CONSTANT_P)
531 return 0;
533 return cost + 20;
536 if (gimple_code (stmt) != GIMPLE_ASSIGN)
537 return cost;
539 switch (gimple_assign_rhs_code (stmt))
541 case MULT_EXPR:
542 case TRUNC_DIV_EXPR:
543 case CEIL_DIV_EXPR:
544 case FLOOR_DIV_EXPR:
545 case ROUND_DIV_EXPR:
546 case EXACT_DIV_EXPR:
547 case CEIL_MOD_EXPR:
548 case FLOOR_MOD_EXPR:
549 case ROUND_MOD_EXPR:
550 case TRUNC_MOD_EXPR:
551 case RDIV_EXPR:
552 /* Division and multiplication are usually expensive. */
553 cost += 20;
554 break;
556 case LSHIFT_EXPR:
557 case RSHIFT_EXPR:
558 cost += 20;
559 break;
561 default:
562 break;
565 return cost;
568 /* Finds the outermost loop between OUTER and LOOP in that the memory reference
569 REF is independent. If REF is not independent in LOOP, NULL is returned
570 instead. */
572 static struct loop *
573 outermost_indep_loop (struct loop *outer, struct loop *loop, mem_ref_p ref)
575 struct loop *aloop;
577 if (bitmap_bit_p (ref->stored, loop->num))
578 return NULL;
580 for (aloop = outer;
581 aloop != loop;
582 aloop = superloop_at_depth (loop, loop_depth (aloop) + 1))
583 if (!bitmap_bit_p (ref->stored, aloop->num)
584 && ref_indep_loop_p (aloop, ref))
585 return aloop;
587 if (ref_indep_loop_p (loop, ref))
588 return loop;
589 else
590 return NULL;
593 /* If there is a simple load or store to a memory reference in STMT, returns
594 the location of the memory reference, and sets IS_STORE accoring to whether
595 it is a store or load. Otherwise, returns NULL. */
597 static tree *
598 simple_mem_ref_in_stmt (gimple stmt, bool *is_store)
600 tree *lhs;
601 enum tree_code code;
603 /* Recognize MEM = (SSA_NAME | invariant) and SSA_NAME = MEM patterns. */
604 if (gimple_code (stmt) != GIMPLE_ASSIGN)
605 return NULL;
607 code = gimple_assign_rhs_code (stmt);
609 lhs = gimple_assign_lhs_ptr (stmt);
611 if (TREE_CODE (*lhs) == SSA_NAME)
613 if (get_gimple_rhs_class (code) != GIMPLE_SINGLE_RHS
614 || !is_gimple_addressable (gimple_assign_rhs1 (stmt)))
615 return NULL;
617 *is_store = false;
618 return gimple_assign_rhs1_ptr (stmt);
620 else if (code == SSA_NAME
621 || (get_gimple_rhs_class (code) == GIMPLE_SINGLE_RHS
622 && is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
624 *is_store = true;
625 return lhs;
627 else
628 return NULL;
631 /* Returns the memory reference contained in STMT. */
633 static mem_ref_p
634 mem_ref_in_stmt (gimple stmt)
636 bool store;
637 tree *mem = simple_mem_ref_in_stmt (stmt, &store);
638 hashval_t hash;
639 mem_ref_p ref;
641 if (!mem)
642 return NULL;
643 gcc_assert (!store);
645 hash = iterative_hash_expr (*mem, 0);
646 ref = htab_find_with_hash (memory_accesses.refs, *mem, hash);
648 gcc_assert (ref != NULL);
649 return ref;
652 /* Determine the outermost loop to that it is possible to hoist a statement
653 STMT and store it to LIM_DATA (STMT)->max_loop. To do this we determine
654 the outermost loop in that the value computed by STMT is invariant.
655 If MUST_PRESERVE_EXEC is true, additionally choose such a loop that
656 we preserve the fact whether STMT is executed. It also fills other related
657 information to LIM_DATA (STMT).
659 The function returns false if STMT cannot be hoisted outside of the loop it
660 is defined in, and true otherwise. */
662 static bool
663 determine_max_movement (gimple stmt, bool must_preserve_exec)
665 basic_block bb = gimple_bb (stmt);
666 struct loop *loop = bb->loop_father;
667 struct loop *level;
668 struct lim_aux_data *lim_data = get_lim_data (stmt);
669 tree val;
670 ssa_op_iter iter;
672 if (must_preserve_exec)
673 level = ALWAYS_EXECUTED_IN (bb);
674 else
675 level = superloop_at_depth (loop, 1);
676 lim_data->max_loop = level;
678 FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_USE)
679 if (!add_dependency (val, lim_data, loop, true))
680 return false;
682 if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_USES))
684 mem_ref_p ref = mem_ref_in_stmt (stmt);
686 if (ref)
688 lim_data->max_loop
689 = outermost_indep_loop (lim_data->max_loop, loop, ref);
690 if (!lim_data->max_loop)
691 return false;
693 else
695 FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_VIRTUAL_USES)
697 if (!add_dependency (val, lim_data, loop, false))
698 return false;
703 lim_data->cost += stmt_cost (stmt);
705 return true;
708 /* Suppose that some statement in ORIG_LOOP is hoisted to the loop LEVEL,
709 and that one of the operands of this statement is computed by STMT.
710 Ensure that STMT (together with all the statements that define its
711 operands) is hoisted at least out of the loop LEVEL. */
713 static void
714 set_level (gimple stmt, struct loop *orig_loop, struct loop *level)
716 struct loop *stmt_loop = gimple_bb (stmt)->loop_father;
717 struct depend *dep;
718 struct lim_aux_data *lim_data;
720 stmt_loop = find_common_loop (orig_loop, stmt_loop);
721 lim_data = get_lim_data (stmt);
722 if (lim_data != NULL && lim_data->tgt_loop != NULL)
723 stmt_loop = find_common_loop (stmt_loop,
724 loop_outer (lim_data->tgt_loop));
725 if (flow_loop_nested_p (stmt_loop, level))
726 return;
728 gcc_assert (level == lim_data->max_loop
729 || flow_loop_nested_p (lim_data->max_loop, level));
731 lim_data->tgt_loop = level;
732 for (dep = lim_data->depends; dep; dep = dep->next)
733 set_level (dep->stmt, orig_loop, level);
736 /* Determines an outermost loop from that we want to hoist the statement STMT.
737 For now we chose the outermost possible loop. TODO -- use profiling
738 information to set it more sanely. */
740 static void
741 set_profitable_level (gimple stmt)
743 set_level (stmt, gimple_bb (stmt)->loop_father, get_lim_data (stmt)->max_loop);
746 /* Returns true if STMT is a call that has side effects. */
748 static bool
749 nonpure_call_p (gimple stmt)
751 if (gimple_code (stmt) != GIMPLE_CALL)
752 return false;
754 return gimple_has_side_effects (stmt);
757 /* Rewrite a/b to a*(1/b). Return the invariant stmt to process. */
759 static gimple
760 rewrite_reciprocal (gimple_stmt_iterator *bsi)
762 gimple stmt, stmt1, stmt2;
763 tree var, name, lhs, type;
765 stmt = gsi_stmt (*bsi);
766 lhs = gimple_assign_lhs (stmt);
767 type = TREE_TYPE (lhs);
769 var = create_tmp_var (type, "reciptmp");
770 add_referenced_var (var);
772 stmt1 = gimple_build_assign_with_ops (RDIV_EXPR,
773 var, build_real (type, dconst1), gimple_assign_rhs2 (stmt));
774 name = make_ssa_name (var, stmt1);
775 gimple_assign_set_lhs (stmt1, name);
777 stmt2 = gimple_build_assign_with_ops (MULT_EXPR, lhs, name,
778 gimple_assign_rhs1 (stmt));
780 /* Replace division stmt with reciprocal and multiply stmts.
781 The multiply stmt is not invariant, so update iterator
782 and avoid rescanning. */
783 gsi_replace (bsi, stmt1, true);
784 gsi_insert_after (bsi, stmt2, GSI_NEW_STMT);
786 /* Continue processing with invariant reciprocal statement. */
787 return stmt1;
790 /* Check if the pattern at *BSI is a bittest of the form
791 (A >> B) & 1 != 0 and in this case rewrite it to A & (1 << B) != 0. */
793 static gimple
794 rewrite_bittest (gimple_stmt_iterator *bsi)
796 gimple stmt, use_stmt, stmt1, stmt2;
797 tree lhs, var, name, t, a, b;
798 use_operand_p use;
800 stmt = gsi_stmt (*bsi);
801 lhs = gimple_assign_lhs (stmt);
803 /* Verify that the single use of lhs is a comparison against zero. */
804 if (TREE_CODE (lhs) != SSA_NAME
805 || !single_imm_use (lhs, &use, &use_stmt)
806 || gimple_code (use_stmt) != GIMPLE_COND)
807 return stmt;
808 if (gimple_cond_lhs (use_stmt) != lhs
809 || (gimple_cond_code (use_stmt) != NE_EXPR
810 && gimple_cond_code (use_stmt) != EQ_EXPR)
811 || !integer_zerop (gimple_cond_rhs (use_stmt)))
812 return stmt;
814 /* Get at the operands of the shift. The rhs is TMP1 & 1. */
815 stmt1 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
816 if (gimple_code (stmt1) != GIMPLE_ASSIGN)
817 return stmt;
819 /* There is a conversion in between possibly inserted by fold. */
820 if (gimple_assign_rhs_code (stmt1) == NOP_EXPR
821 || gimple_assign_rhs_code (stmt1) == CONVERT_EXPR)
823 t = gimple_assign_rhs1 (stmt1);
824 if (TREE_CODE (t) != SSA_NAME
825 || !has_single_use (t))
826 return stmt;
827 stmt1 = SSA_NAME_DEF_STMT (t);
828 if (gimple_code (stmt1) != GIMPLE_ASSIGN)
829 return stmt;
832 /* Verify that B is loop invariant but A is not. Verify that with
833 all the stmt walking we are still in the same loop. */
834 if (gimple_assign_rhs_code (stmt1) != RSHIFT_EXPR
835 || loop_containing_stmt (stmt1) != loop_containing_stmt (stmt))
836 return stmt;
838 a = gimple_assign_rhs1 (stmt1);
839 b = gimple_assign_rhs2 (stmt1);
841 if (outermost_invariant_loop (b, loop_containing_stmt (stmt1)) != NULL
842 && outermost_invariant_loop (a, loop_containing_stmt (stmt1)) == NULL)
844 /* 1 << B */
845 var = create_tmp_var (TREE_TYPE (a), "shifttmp");
846 add_referenced_var (var);
847 t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (a),
848 build_int_cst (TREE_TYPE (a), 1), b);
849 stmt1 = gimple_build_assign (var, t);
850 name = make_ssa_name (var, stmt1);
851 gimple_assign_set_lhs (stmt1, name);
853 /* A & (1 << B) */
854 t = fold_build2 (BIT_AND_EXPR, TREE_TYPE (a), a, name);
855 stmt2 = gimple_build_assign (var, t);
856 name = make_ssa_name (var, stmt2);
857 gimple_assign_set_lhs (stmt2, name);
859 /* Replace the SSA_NAME we compare against zero. Adjust
860 the type of zero accordingly. */
861 SET_USE (use, name);
862 gimple_cond_set_rhs (use_stmt, build_int_cst_type (TREE_TYPE (name), 0));
864 gsi_insert_before (bsi, stmt1, GSI_SAME_STMT);
865 gsi_replace (bsi, stmt2, true);
867 return stmt1;
870 return stmt;
874 /* Determine the outermost loops in that statements in basic block BB are
875 invariant, and record them to the LIM_DATA associated with the statements.
876 Callback for walk_dominator_tree. */
878 static void
879 determine_invariantness_stmt (struct dom_walk_data *dw_data ATTRIBUTE_UNUSED,
880 basic_block bb)
882 enum move_pos pos;
883 gimple_stmt_iterator bsi;
884 gimple stmt;
885 bool maybe_never = ALWAYS_EXECUTED_IN (bb) == NULL;
886 struct loop *outermost = ALWAYS_EXECUTED_IN (bb);
887 struct lim_aux_data *lim_data;
889 if (!loop_outer (bb->loop_father))
890 return;
892 if (dump_file && (dump_flags & TDF_DETAILS))
893 fprintf (dump_file, "Basic block %d (loop %d -- depth %d):\n\n",
894 bb->index, bb->loop_father->num, loop_depth (bb->loop_father));
896 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
898 stmt = gsi_stmt (bsi);
900 pos = movement_possibility (stmt);
901 if (pos == MOVE_IMPOSSIBLE)
903 if (nonpure_call_p (stmt))
905 maybe_never = true;
906 outermost = NULL;
908 continue;
911 if (gimple_code (stmt) == GIMPLE_ASSIGN
912 && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
913 == GIMPLE_BINARY_RHS))
915 tree op0 = gimple_assign_rhs1 (stmt);
916 tree op1 = gimple_assign_rhs2 (stmt);
917 struct loop *ol1 = outermost_invariant_loop (op1,
918 loop_containing_stmt (stmt));
920 /* If divisor is invariant, convert a/b to a*(1/b), allowing reciprocal
921 to be hoisted out of loop, saving expensive divide. */
922 if (pos == MOVE_POSSIBLE
923 && gimple_assign_rhs_code (stmt) == RDIV_EXPR
924 && flag_unsafe_math_optimizations
925 && !flag_trapping_math
926 && ol1 != NULL
927 && outermost_invariant_loop (op0, ol1) == NULL)
928 stmt = rewrite_reciprocal (&bsi);
930 /* If the shift count is invariant, convert (A >> B) & 1 to
931 A & (1 << B) allowing the bit mask to be hoisted out of the loop
932 saving an expensive shift. */
933 if (pos == MOVE_POSSIBLE
934 && gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
935 && integer_onep (op1)
936 && TREE_CODE (op0) == SSA_NAME
937 && has_single_use (op0))
938 stmt = rewrite_bittest (&bsi);
941 lim_data = init_lim_data (stmt);
942 lim_data->always_executed_in = outermost;
944 if (maybe_never && pos == MOVE_PRESERVE_EXECUTION)
945 continue;
947 if (!determine_max_movement (stmt, pos == MOVE_PRESERVE_EXECUTION))
949 lim_data->max_loop = NULL;
950 continue;
953 if (dump_file && (dump_flags & TDF_DETAILS))
955 print_gimple_stmt (dump_file, stmt, 2, 0);
956 fprintf (dump_file, " invariant up to level %d, cost %d.\n\n",
957 loop_depth (lim_data->max_loop),
958 lim_data->cost);
961 if (lim_data->cost >= LIM_EXPENSIVE)
962 set_profitable_level (stmt);
966 /* For each statement determines the outermost loop in that it is invariant,
967 statements on whose motion it depends and the cost of the computation.
968 This information is stored to the LIM_DATA structure associated with
969 each statement. */
971 static void
972 determine_invariantness (void)
974 struct dom_walk_data walk_data;
976 memset (&walk_data, 0, sizeof (struct dom_walk_data));
977 walk_data.dom_direction = CDI_DOMINATORS;
978 walk_data.before_dom_children_before_stmts = determine_invariantness_stmt;
980 init_walk_dominator_tree (&walk_data);
981 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
982 fini_walk_dominator_tree (&walk_data);
985 /* Hoist the statements in basic block BB out of the loops prescribed by
986 data stored in LIM_DATA structures associated with each statement. Callback
987 for walk_dominator_tree. */
989 static void
990 move_computations_stmt (struct dom_walk_data *dw_data ATTRIBUTE_UNUSED,
991 basic_block bb)
993 struct loop *level;
994 gimple_stmt_iterator bsi;
995 gimple stmt;
996 unsigned cost = 0;
997 struct lim_aux_data *lim_data;
999 if (!loop_outer (bb->loop_father))
1000 return;
1002 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); )
1004 stmt = gsi_stmt (bsi);
1006 lim_data = get_lim_data (stmt);
1007 if (lim_data == NULL)
1009 gsi_next (&bsi);
1010 continue;
1013 cost = lim_data->cost;
1014 level = lim_data->tgt_loop;
1015 clear_lim_data (stmt);
1017 if (!level)
1019 gsi_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 (gimple_code (stmt) == GIMPLE_COND)
1026 continue;
1028 if (dump_file && (dump_flags & TDF_DETAILS))
1030 fprintf (dump_file, "Moving statement\n");
1031 print_gimple_stmt (dump_file, stmt, 0, 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 gsi_insert_on_edge (loop_preheader_edge (level), stmt);
1038 gsi_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 gsi_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 = TREE_OPERAND (ref, 3);
1076 tree lbound = TREE_OPERAND (ref, 2);
1078 max_loop = outermost_invariant_loop (step, loop);
1079 if (!max_loop)
1080 return false;
1082 max_loop = outermost_invariant_loop (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 /* If OP is SSA NAME, force the statement that defines it 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_op (tree op, struct loop *orig_loop, struct loop *loop)
1100 gimple stmt;
1102 if (!op
1103 || is_gimple_min_invariant (op))
1104 return;
1106 gcc_assert (TREE_CODE (op) == SSA_NAME);
1108 stmt = SSA_NAME_DEF_STMT (op);
1109 if (gimple_nop_p (stmt))
1110 return;
1112 set_level (stmt, orig_loop, loop);
1115 /* Forces statement defining invariants in REF (and *INDEX) to be moved out of
1116 the LOOP. The reference REF is used in the loop ORIG_LOOP. Callback for
1117 for_each_index. */
1119 struct fmt_data
1121 struct loop *loop;
1122 struct loop *orig_loop;
1125 static bool
1126 force_move_till (tree ref, tree *index, void *data)
1128 struct fmt_data *fmt_data = (struct fmt_data *) data;
1130 if (TREE_CODE (ref) == ARRAY_REF)
1132 tree step = TREE_OPERAND (ref, 3);
1133 tree lbound = TREE_OPERAND (ref, 2);
1135 force_move_till_op (step, fmt_data->orig_loop, fmt_data->loop);
1136 force_move_till_op (lbound, fmt_data->orig_loop, fmt_data->loop);
1139 force_move_till_op (*index, fmt_data->orig_loop, fmt_data->loop);
1141 return true;
1144 /* A hash function for struct mem_ref object OBJ. */
1146 static hashval_t
1147 memref_hash (const void *obj)
1149 const struct mem_ref *mem = obj;
1151 return mem->hash;
1154 /* An equality function for struct mem_ref object OBJ1 with
1155 memory reference OBJ2. */
1157 static int
1158 memref_eq (const void *obj1, const void *obj2)
1160 const struct mem_ref *mem1 = obj1;
1162 return operand_equal_p (mem1->mem, (tree) obj2, 0);
1165 /* Releases list of memory reference locations ACCS. */
1167 static void
1168 free_mem_ref_locs (mem_ref_locs_p accs)
1170 unsigned i;
1171 mem_ref_loc_p loc;
1173 if (!accs)
1174 return;
1176 for (i = 0; VEC_iterate (mem_ref_loc_p, accs->locs, i, loc); i++)
1177 free (loc);
1178 VEC_free (mem_ref_loc_p, heap, accs->locs);
1179 free (accs);
1182 /* A function to free the mem_ref object OBJ. */
1184 static void
1185 memref_free (void *obj)
1187 struct mem_ref *mem = obj;
1188 unsigned i;
1189 mem_ref_locs_p accs;
1191 BITMAP_FREE (mem->stored);
1192 BITMAP_FREE (mem->indep_loop);
1193 BITMAP_FREE (mem->dep_loop);
1194 BITMAP_FREE (mem->indep_ref);
1195 BITMAP_FREE (mem->dep_ref);
1197 for (i = 0; VEC_iterate (mem_ref_locs_p, mem->accesses_in_loop, i, accs); i++)
1198 free_mem_ref_locs (accs);
1199 VEC_free (mem_ref_locs_p, heap, mem->accesses_in_loop);
1201 BITMAP_FREE (mem->vops);
1202 free (mem);
1205 /* Allocates and returns a memory reference description for MEM whose hash
1206 value is HASH and id is ID. */
1208 static mem_ref_p
1209 mem_ref_alloc (tree mem, unsigned hash, unsigned id)
1211 mem_ref_p ref = XNEW (struct mem_ref);
1212 ref->mem = mem;
1213 ref->id = id;
1214 ref->hash = hash;
1215 ref->stored = BITMAP_ALLOC (NULL);
1216 ref->indep_loop = BITMAP_ALLOC (NULL);
1217 ref->dep_loop = BITMAP_ALLOC (NULL);
1218 ref->indep_ref = BITMAP_ALLOC (NULL);
1219 ref->dep_ref = BITMAP_ALLOC (NULL);
1220 ref->accesses_in_loop = NULL;
1221 ref->vops = BITMAP_ALLOC (NULL);
1223 return ref;
1226 /* Allocates and returns the new list of locations. */
1228 static mem_ref_locs_p
1229 mem_ref_locs_alloc (void)
1231 mem_ref_locs_p accs = XNEW (struct mem_ref_locs);
1232 accs->locs = NULL;
1233 return accs;
1236 /* Records memory reference location *LOC in LOOP to the memory reference
1237 description REF. The reference occurs in statement STMT. */
1239 static void
1240 record_mem_ref_loc (mem_ref_p ref, struct loop *loop, gimple stmt, tree *loc)
1242 mem_ref_loc_p aref = XNEW (struct mem_ref_loc);
1243 mem_ref_locs_p accs;
1244 bitmap ril = VEC_index (bitmap, memory_accesses.refs_in_loop, loop->num);
1246 if (VEC_length (mem_ref_locs_p, ref->accesses_in_loop)
1247 <= (unsigned) loop->num)
1248 VEC_safe_grow_cleared (mem_ref_locs_p, heap, ref->accesses_in_loop,
1249 loop->num + 1);
1250 accs = VEC_index (mem_ref_locs_p, ref->accesses_in_loop, loop->num);
1251 if (!accs)
1253 accs = mem_ref_locs_alloc ();
1254 VEC_replace (mem_ref_locs_p, ref->accesses_in_loop, loop->num, accs);
1257 aref->stmt = stmt;
1258 aref->ref = loc;
1260 VEC_safe_push (mem_ref_loc_p, heap, accs->locs, aref);
1261 bitmap_set_bit (ril, ref->id);
1264 /* Marks reference REF as stored in LOOP. */
1266 static void
1267 mark_ref_stored (mem_ref_p ref, struct loop *loop)
1269 for (;
1270 loop != current_loops->tree_root
1271 && !bitmap_bit_p (ref->stored, loop->num);
1272 loop = loop_outer (loop))
1273 bitmap_set_bit (ref->stored, loop->num);
1276 /* Gathers memory references in statement STMT in LOOP, storing the
1277 information about them in the memory_accesses structure. Marks
1278 the vops accessed through unrecognized statements there as
1279 well. */
1281 static void
1282 gather_mem_refs_stmt (struct loop *loop, gimple stmt)
1284 tree *mem = NULL;
1285 hashval_t hash;
1286 PTR *slot;
1287 mem_ref_p ref;
1288 ssa_op_iter oi;
1289 tree vname;
1290 bool is_stored;
1291 bitmap clvops;
1292 unsigned id;
1294 if (ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
1295 return;
1297 mem = simple_mem_ref_in_stmt (stmt, &is_stored);
1298 if (!mem)
1299 goto fail;
1301 hash = iterative_hash_expr (*mem, 0);
1302 slot = htab_find_slot_with_hash (memory_accesses.refs, *mem, hash, INSERT);
1304 if (*slot)
1306 ref = *slot;
1307 id = ref->id;
1309 else
1311 id = VEC_length (mem_ref_p, memory_accesses.refs_list);
1312 ref = mem_ref_alloc (*mem, hash, id);
1313 VEC_safe_push (mem_ref_p, heap, memory_accesses.refs_list, ref);
1314 *slot = ref;
1316 if (dump_file && (dump_flags & TDF_DETAILS))
1318 fprintf (dump_file, "Memory reference %u: ", id);
1319 print_generic_expr (dump_file, ref->mem, TDF_SLIM);
1320 fprintf (dump_file, "\n");
1323 if (is_stored)
1324 mark_ref_stored (ref, loop);
1326 FOR_EACH_SSA_TREE_OPERAND (vname, stmt, oi, SSA_OP_VIRTUAL_USES)
1327 bitmap_set_bit (ref->vops, DECL_UID (SSA_NAME_VAR (vname)));
1328 record_mem_ref_loc (ref, loop, stmt, mem);
1329 return;
1331 fail:
1332 clvops = VEC_index (bitmap, memory_accesses.clobbered_vops, loop->num);
1333 FOR_EACH_SSA_TREE_OPERAND (vname, stmt, oi, SSA_OP_VIRTUAL_USES)
1334 bitmap_set_bit (clvops, DECL_UID (SSA_NAME_VAR (vname)));
1337 /* Gathers memory references in loops. */
1339 static void
1340 gather_mem_refs_in_loops (void)
1342 gimple_stmt_iterator bsi;
1343 basic_block bb;
1344 struct loop *loop;
1345 loop_iterator li;
1346 bitmap clvo, clvi;
1347 bitmap lrefs, alrefs, alrefso;
1349 FOR_EACH_BB (bb)
1351 loop = bb->loop_father;
1352 if (loop == current_loops->tree_root)
1353 continue;
1355 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1356 gather_mem_refs_stmt (loop, gsi_stmt (bsi));
1359 /* Propagate the information about clobbered vops and accessed memory
1360 references up the loop hierarchy. */
1361 FOR_EACH_LOOP (li, loop, LI_FROM_INNERMOST)
1363 lrefs = VEC_index (bitmap, memory_accesses.refs_in_loop, loop->num);
1364 alrefs = VEC_index (bitmap, memory_accesses.all_refs_in_loop, loop->num);
1365 bitmap_ior_into (alrefs, lrefs);
1367 if (loop_outer (loop) == current_loops->tree_root)
1368 continue;
1370 clvi = VEC_index (bitmap, memory_accesses.clobbered_vops, loop->num);
1371 clvo = VEC_index (bitmap, memory_accesses.clobbered_vops,
1372 loop_outer (loop)->num);
1373 bitmap_ior_into (clvo, clvi);
1375 alrefso = VEC_index (bitmap, memory_accesses.all_refs_in_loop,
1376 loop_outer (loop)->num);
1377 bitmap_ior_into (alrefso, alrefs);
1381 /* Element of the hash table that maps vops to memory references. */
1383 struct vop_to_refs_elt
1385 /* DECL_UID of the vop. */
1386 unsigned uid;
1388 /* List of the all references. */
1389 bitmap refs_all;
1391 /* List of stored references. */
1392 bitmap refs_stored;
1395 /* A hash function for struct vop_to_refs_elt object OBJ. */
1397 static hashval_t
1398 vtoe_hash (const void *obj)
1400 const struct vop_to_refs_elt *vtoe = obj;
1402 return vtoe->uid;
1405 /* An equality function for struct vop_to_refs_elt object OBJ1 with
1406 uid of a vop OBJ2. */
1408 static int
1409 vtoe_eq (const void *obj1, const void *obj2)
1411 const struct vop_to_refs_elt *vtoe = obj1;
1412 const unsigned *uid = obj2;
1414 return vtoe->uid == *uid;
1417 /* A function to free the struct vop_to_refs_elt object. */
1419 static void
1420 vtoe_free (void *obj)
1422 struct vop_to_refs_elt *vtoe = obj;
1424 BITMAP_FREE (vtoe->refs_all);
1425 BITMAP_FREE (vtoe->refs_stored);
1426 free (vtoe);
1429 /* Records REF to hashtable VOP_TO_REFS for the index VOP. STORED is true
1430 if the reference REF is stored. */
1432 static void
1433 record_vop_access (htab_t vop_to_refs, unsigned vop, unsigned ref, bool stored)
1435 void **slot = htab_find_slot_with_hash (vop_to_refs, &vop, vop, INSERT);
1436 struct vop_to_refs_elt *vtoe;
1438 if (!*slot)
1440 vtoe = XNEW (struct vop_to_refs_elt);
1441 vtoe->uid = vop;
1442 vtoe->refs_all = BITMAP_ALLOC (NULL);
1443 vtoe->refs_stored = BITMAP_ALLOC (NULL);
1444 *slot = vtoe;
1446 else
1447 vtoe = *slot;
1449 bitmap_set_bit (vtoe->refs_all, ref);
1450 if (stored)
1451 bitmap_set_bit (vtoe->refs_stored, ref);
1454 /* Returns the set of references that access VOP according to the table
1455 VOP_TO_REFS. */
1457 static bitmap
1458 get_vop_accesses (htab_t vop_to_refs, unsigned vop)
1460 struct vop_to_refs_elt *vtoe = htab_find_with_hash (vop_to_refs, &vop, vop);
1461 return vtoe->refs_all;
1464 /* Returns the set of stores that access VOP according to the table
1465 VOP_TO_REFS. */
1467 static bitmap
1468 get_vop_stores (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_stored;
1474 /* Adds REF to mapping from virtual operands to references in LOOP. */
1476 static void
1477 add_vop_ref_mapping (struct loop *loop, mem_ref_p ref)
1479 htab_t map = VEC_index (htab_t, memory_accesses.vop_ref_map, loop->num);
1480 bool stored = bitmap_bit_p (ref->stored, loop->num);
1481 bitmap clobbers = VEC_index (bitmap, memory_accesses.clobbered_vops,
1482 loop->num);
1483 bitmap_iterator bi;
1484 unsigned vop;
1486 EXECUTE_IF_AND_COMPL_IN_BITMAP (ref->vops, clobbers, 0, vop, bi)
1488 record_vop_access (map, vop, ref->id, stored);
1492 /* Create a mapping from virtual operands to references that touch them
1493 in LOOP. */
1495 static void
1496 create_vop_ref_mapping_loop (struct loop *loop)
1498 bitmap refs = VEC_index (bitmap, memory_accesses.refs_in_loop, loop->num);
1499 struct loop *sloop;
1500 bitmap_iterator bi;
1501 unsigned i;
1502 mem_ref_p ref;
1504 EXECUTE_IF_SET_IN_BITMAP (refs, 0, i, bi)
1506 ref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
1507 for (sloop = loop; sloop != current_loops->tree_root; sloop = loop_outer (sloop))
1508 add_vop_ref_mapping (sloop, ref);
1512 /* For each non-clobbered virtual operand and each loop, record the memory
1513 references in this loop that touch the operand. */
1515 static void
1516 create_vop_ref_mapping (void)
1518 loop_iterator li;
1519 struct loop *loop;
1521 FOR_EACH_LOOP (li, loop, 0)
1523 create_vop_ref_mapping_loop (loop);
1527 /* Gathers information about memory accesses in the loops. */
1529 static void
1530 analyze_memory_references (void)
1532 unsigned i;
1533 bitmap empty;
1534 htab_t hempty;
1536 memory_accesses.refs
1537 = htab_create (100, memref_hash, memref_eq, memref_free);
1538 memory_accesses.refs_list = NULL;
1539 memory_accesses.refs_in_loop = VEC_alloc (bitmap, heap,
1540 number_of_loops ());
1541 memory_accesses.all_refs_in_loop = VEC_alloc (bitmap, heap,
1542 number_of_loops ());
1543 memory_accesses.clobbered_vops = VEC_alloc (bitmap, heap,
1544 number_of_loops ());
1545 memory_accesses.vop_ref_map = VEC_alloc (htab_t, heap,
1546 number_of_loops ());
1548 for (i = 0; i < number_of_loops (); i++)
1550 empty = BITMAP_ALLOC (NULL);
1551 VEC_quick_push (bitmap, memory_accesses.refs_in_loop, empty);
1552 empty = BITMAP_ALLOC (NULL);
1553 VEC_quick_push (bitmap, memory_accesses.all_refs_in_loop, empty);
1554 empty = BITMAP_ALLOC (NULL);
1555 VEC_quick_push (bitmap, memory_accesses.clobbered_vops, empty);
1556 hempty = htab_create (10, vtoe_hash, vtoe_eq, vtoe_free);
1557 VEC_quick_push (htab_t, memory_accesses.vop_ref_map, hempty);
1560 memory_accesses.ttae_cache = NULL;
1562 gather_mem_refs_in_loops ();
1563 create_vop_ref_mapping ();
1566 /* Returns true if a region of size SIZE1 at position 0 and a region of
1567 size SIZE2 at position DIFF cannot overlap. */
1569 static bool
1570 cannot_overlap_p (aff_tree *diff, double_int size1, double_int size2)
1572 double_int d, bound;
1574 /* Unless the difference is a constant, we fail. */
1575 if (diff->n != 0)
1576 return false;
1578 d = diff->offset;
1579 if (double_int_negative_p (d))
1581 /* The second object is before the first one, we succeed if the last
1582 element of the second object is before the start of the first one. */
1583 bound = double_int_add (d, double_int_add (size2, double_int_minus_one));
1584 return double_int_negative_p (bound);
1586 else
1588 /* We succeed if the second object starts after the first one ends. */
1589 return double_int_scmp (size1, d) <= 0;
1593 /* Returns true if MEM1 and MEM2 may alias. TTAE_CACHE is used as a cache in
1594 tree_to_aff_combination_expand. */
1596 static bool
1597 mem_refs_may_alias_p (tree mem1, tree mem2, struct pointer_map_t **ttae_cache)
1599 /* Perform BASE + OFFSET analysis -- if MEM1 and MEM2 are based on the same
1600 object and their offset differ in such a way that the locations cannot
1601 overlap, then they cannot alias. */
1602 aff_tree off1, off2;
1603 double_int size1, size2;
1604 tree base1, base2;
1606 /* If MEM1 and MEM2 are based on different variables, they cannot alias. */
1607 base1 = get_base_address (mem1);
1608 base2 = get_base_address (mem2);
1610 if (base1
1611 && !INDIRECT_REF_P (base1)
1612 && base2
1613 && !INDIRECT_REF_P (base2)
1614 && !operand_equal_p (base1, base2, 0))
1615 return false;
1617 /* With strict aliasing, it is impossible to access a scalar variable through
1618 anything but a pointer dereference or through a union (gcc extension). */
1619 if (flag_strict_aliasing)
1621 if (!INDIRECT_REF_P (mem1)
1622 && base1
1623 && TREE_CODE (TREE_TYPE (base1)) != UNION_TYPE
1624 && SSA_VAR_P (mem2)
1625 && !AGGREGATE_TYPE_P (TREE_TYPE (mem2)))
1626 return false;
1627 if (!INDIRECT_REF_P (mem2)
1628 && base2
1629 && TREE_CODE (TREE_TYPE (base2)) != UNION_TYPE
1630 && SSA_VAR_P (mem1)
1631 && !AGGREGATE_TYPE_P (TREE_TYPE (mem1)))
1632 return false;
1635 /* The expansion of addresses may be a bit expensive, thus we only do
1636 the check at -O2 and higher optimization levels. */
1637 if (optimize < 2)
1638 return true;
1640 get_inner_reference_aff (mem1, &off1, &size1);
1641 get_inner_reference_aff (mem2, &off2, &size2);
1642 aff_combination_expand (&off1, ttae_cache);
1643 aff_combination_expand (&off2, ttae_cache);
1644 aff_combination_scale (&off1, double_int_minus_one);
1645 aff_combination_add (&off2, &off1);
1647 if (cannot_overlap_p (&off2, size1, size2))
1648 return false;
1650 return true;
1653 /* Rewrites location LOC by TMP_VAR. */
1655 static void
1656 rewrite_mem_ref_loc (mem_ref_loc_p loc, tree tmp_var)
1658 mark_virtual_ops_for_renaming (loc->stmt);
1659 *loc->ref = tmp_var;
1660 update_stmt (loc->stmt);
1663 /* Adds all locations of REF in LOOP and its subloops to LOCS. */
1665 static void
1666 get_all_locs_in_loop (struct loop *loop, mem_ref_p ref,
1667 VEC (mem_ref_loc_p, heap) **locs)
1669 mem_ref_locs_p accs;
1670 unsigned i;
1671 mem_ref_loc_p loc;
1672 bitmap refs = VEC_index (bitmap, memory_accesses.all_refs_in_loop,
1673 loop->num);
1674 struct loop *subloop;
1676 if (!bitmap_bit_p (refs, ref->id))
1677 return;
1679 if (VEC_length (mem_ref_locs_p, ref->accesses_in_loop)
1680 > (unsigned) loop->num)
1682 accs = VEC_index (mem_ref_locs_p, ref->accesses_in_loop, loop->num);
1683 if (accs)
1685 for (i = 0; VEC_iterate (mem_ref_loc_p, accs->locs, i, loc); i++)
1686 VEC_safe_push (mem_ref_loc_p, heap, *locs, loc);
1690 for (subloop = loop->inner; subloop != NULL; subloop = subloop->next)
1691 get_all_locs_in_loop (subloop, ref, locs);
1694 /* Rewrites all references to REF in LOOP by variable TMP_VAR. */
1696 static void
1697 rewrite_mem_refs (struct loop *loop, mem_ref_p ref, tree tmp_var)
1699 unsigned i;
1700 mem_ref_loc_p loc;
1701 VEC (mem_ref_loc_p, heap) *locs = NULL;
1703 get_all_locs_in_loop (loop, ref, &locs);
1704 for (i = 0; VEC_iterate (mem_ref_loc_p, locs, i, loc); i++)
1705 rewrite_mem_ref_loc (loc, tmp_var);
1706 VEC_free (mem_ref_loc_p, heap, locs);
1709 /* The name and the length of the currently generated variable
1710 for lsm. */
1711 #define MAX_LSM_NAME_LENGTH 40
1712 static char lsm_tmp_name[MAX_LSM_NAME_LENGTH + 1];
1713 static int lsm_tmp_name_length;
1715 /* Adds S to lsm_tmp_name. */
1717 static void
1718 lsm_tmp_name_add (const char *s)
1720 int l = strlen (s) + lsm_tmp_name_length;
1721 if (l > MAX_LSM_NAME_LENGTH)
1722 return;
1724 strcpy (lsm_tmp_name + lsm_tmp_name_length, s);
1725 lsm_tmp_name_length = l;
1728 /* Stores the name for temporary variable that replaces REF to
1729 lsm_tmp_name. */
1731 static void
1732 gen_lsm_tmp_name (tree ref)
1734 const char *name;
1736 switch (TREE_CODE (ref))
1738 case MISALIGNED_INDIRECT_REF:
1739 case ALIGN_INDIRECT_REF:
1740 case INDIRECT_REF:
1741 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1742 lsm_tmp_name_add ("_");
1743 break;
1745 case BIT_FIELD_REF:
1746 case VIEW_CONVERT_EXPR:
1747 case ARRAY_RANGE_REF:
1748 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1749 break;
1751 case REALPART_EXPR:
1752 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1753 lsm_tmp_name_add ("_RE");
1754 break;
1756 case IMAGPART_EXPR:
1757 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1758 lsm_tmp_name_add ("_IM");
1759 break;
1761 case COMPONENT_REF:
1762 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1763 lsm_tmp_name_add ("_");
1764 name = get_name (TREE_OPERAND (ref, 1));
1765 if (!name)
1766 name = "F";
1767 lsm_tmp_name_add ("_");
1768 lsm_tmp_name_add (name);
1770 case ARRAY_REF:
1771 gen_lsm_tmp_name (TREE_OPERAND (ref, 0));
1772 lsm_tmp_name_add ("_I");
1773 break;
1775 case SSA_NAME:
1776 ref = SSA_NAME_VAR (ref);
1777 /* Fallthru. */
1779 case VAR_DECL:
1780 case PARM_DECL:
1781 name = get_name (ref);
1782 if (!name)
1783 name = "D";
1784 lsm_tmp_name_add (name);
1785 break;
1787 case STRING_CST:
1788 lsm_tmp_name_add ("S");
1789 break;
1791 case RESULT_DECL:
1792 lsm_tmp_name_add ("R");
1793 break;
1795 default:
1796 gcc_unreachable ();
1800 /* Determines name for temporary variable that replaces REF.
1801 The name is accumulated into the lsm_tmp_name variable.
1802 N is added to the name of the temporary. */
1804 char *
1805 get_lsm_tmp_name (tree ref, unsigned n)
1807 char ns[2];
1809 lsm_tmp_name_length = 0;
1810 gen_lsm_tmp_name (ref);
1811 lsm_tmp_name_add ("_lsm");
1812 if (n < 10)
1814 ns[0] = '0' + n;
1815 ns[1] = 0;
1816 lsm_tmp_name_add (ns);
1818 return lsm_tmp_name;
1821 /* Executes store motion of memory reference REF from LOOP.
1822 Exits from the LOOP are stored in EXITS. The initialization of the
1823 temporary variable is put to the preheader of the loop, and assignments
1824 to the reference from the temporary variable are emitted to exits. */
1826 static void
1827 execute_sm (struct loop *loop, VEC (edge, heap) *exits, mem_ref_p ref)
1829 tree tmp_var;
1830 unsigned i;
1831 gimple load, store;
1832 struct fmt_data fmt_data;
1833 edge ex;
1834 struct lim_aux_data *lim_data;
1836 if (dump_file && (dump_flags & TDF_DETAILS))
1838 fprintf (dump_file, "Executing store motion of ");
1839 print_generic_expr (dump_file, ref->mem, 0);
1840 fprintf (dump_file, " from loop %d\n", loop->num);
1843 tmp_var = make_rename_temp (TREE_TYPE (ref->mem),
1844 get_lsm_tmp_name (ref->mem, ~0));
1846 fmt_data.loop = loop;
1847 fmt_data.orig_loop = loop;
1848 for_each_index (&ref->mem, force_move_till, &fmt_data);
1850 rewrite_mem_refs (loop, ref, tmp_var);
1852 /* Emit the load & stores. */
1853 load = gimple_build_assign (tmp_var, unshare_expr (ref->mem));
1854 lim_data = init_lim_data (load);
1855 lim_data->max_loop = loop;
1856 lim_data->tgt_loop = loop;
1858 /* Put this into the latch, so that we are sure it will be processed after
1859 all dependencies. */
1860 gsi_insert_on_edge (loop_latch_edge (loop), load);
1862 for (i = 0; VEC_iterate (edge, exits, i, ex); i++)
1864 store = gimple_build_assign (unshare_expr (ref->mem), tmp_var);
1865 gsi_insert_on_edge (ex, store);
1869 /* Hoists memory references MEM_REFS out of LOOP. EXITS is the list of exit
1870 edges of the LOOP. */
1872 static void
1873 hoist_memory_references (struct loop *loop, bitmap mem_refs,
1874 VEC (edge, heap) *exits)
1876 mem_ref_p ref;
1877 unsigned i;
1878 bitmap_iterator bi;
1880 EXECUTE_IF_SET_IN_BITMAP (mem_refs, 0, i, bi)
1882 ref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
1883 execute_sm (loop, exits, ref);
1887 /* Returns true if REF is always accessed in LOOP. */
1889 static bool
1890 ref_always_accessed_p (struct loop *loop, mem_ref_p ref)
1892 VEC (mem_ref_loc_p, heap) *locs = NULL;
1893 unsigned i;
1894 mem_ref_loc_p loc;
1895 bool ret = false;
1896 struct loop *must_exec;
1898 get_all_locs_in_loop (loop, ref, &locs);
1899 for (i = 0; VEC_iterate (mem_ref_loc_p, locs, i, loc); i++)
1901 if (!get_lim_data (loc->stmt))
1902 continue;
1904 must_exec = get_lim_data (loc->stmt)->always_executed_in;
1905 if (!must_exec)
1906 continue;
1908 if (must_exec == loop
1909 || flow_loop_nested_p (must_exec, loop))
1911 ret = true;
1912 break;
1915 VEC_free (mem_ref_loc_p, heap, locs);
1917 return ret;
1920 /* Returns true if REF1 and REF2 are independent. */
1922 static bool
1923 refs_independent_p (mem_ref_p ref1, mem_ref_p ref2)
1925 if (ref1 == ref2
1926 || bitmap_bit_p (ref1->indep_ref, ref2->id))
1927 return true;
1928 if (bitmap_bit_p (ref1->dep_ref, ref2->id))
1929 return false;
1931 if (dump_file && (dump_flags & TDF_DETAILS))
1932 fprintf (dump_file, "Querying dependency of refs %u and %u: ",
1933 ref1->id, ref2->id);
1935 if (mem_refs_may_alias_p (ref1->mem, ref2->mem,
1936 &memory_accesses.ttae_cache))
1938 bitmap_set_bit (ref1->dep_ref, ref2->id);
1939 bitmap_set_bit (ref2->dep_ref, ref1->id);
1940 if (dump_file && (dump_flags & TDF_DETAILS))
1941 fprintf (dump_file, "dependent.\n");
1942 return false;
1944 else
1946 bitmap_set_bit (ref1->indep_ref, ref2->id);
1947 bitmap_set_bit (ref2->indep_ref, ref1->id);
1948 if (dump_file && (dump_flags & TDF_DETAILS))
1949 fprintf (dump_file, "independent.\n");
1950 return true;
1954 /* Records the information whether REF is independent in LOOP (according
1955 to INDEP). */
1957 static void
1958 record_indep_loop (struct loop *loop, mem_ref_p ref, bool indep)
1960 if (indep)
1961 bitmap_set_bit (ref->indep_loop, loop->num);
1962 else
1963 bitmap_set_bit (ref->dep_loop, loop->num);
1966 /* Returns true if REF is independent on all other memory references in
1967 LOOP. */
1969 static bool
1970 ref_indep_loop_p_1 (struct loop *loop, mem_ref_p ref)
1972 bitmap clobbers, refs_to_check, refs;
1973 unsigned i;
1974 bitmap_iterator bi;
1975 bool ret = true, stored = bitmap_bit_p (ref->stored, loop->num);
1976 htab_t map;
1977 mem_ref_p aref;
1979 /* If the reference is clobbered, it is not independent. */
1980 clobbers = VEC_index (bitmap, memory_accesses.clobbered_vops, loop->num);
1981 if (bitmap_intersect_p (ref->vops, clobbers))
1982 return false;
1984 refs_to_check = BITMAP_ALLOC (NULL);
1986 map = VEC_index (htab_t, memory_accesses.vop_ref_map, loop->num);
1987 EXECUTE_IF_AND_COMPL_IN_BITMAP (ref->vops, clobbers, 0, i, bi)
1989 if (stored)
1990 refs = get_vop_accesses (map, i);
1991 else
1992 refs = get_vop_stores (map, i);
1994 bitmap_ior_into (refs_to_check, refs);
1997 EXECUTE_IF_SET_IN_BITMAP (refs_to_check, 0, i, bi)
1999 aref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
2000 if (!refs_independent_p (ref, aref))
2002 ret = false;
2003 record_indep_loop (loop, aref, false);
2004 break;
2008 BITMAP_FREE (refs_to_check);
2009 return ret;
2012 /* Returns true if REF is independent on all other memory references in
2013 LOOP. Wrapper over ref_indep_loop_p_1, caching its results. */
2015 static bool
2016 ref_indep_loop_p (struct loop *loop, mem_ref_p ref)
2018 bool ret;
2020 if (bitmap_bit_p (ref->indep_loop, loop->num))
2021 return true;
2022 if (bitmap_bit_p (ref->dep_loop, loop->num))
2023 return false;
2025 ret = ref_indep_loop_p_1 (loop, ref);
2027 if (dump_file && (dump_flags & TDF_DETAILS))
2028 fprintf (dump_file, "Querying dependencies of ref %u in loop %d: %s\n",
2029 ref->id, loop->num, ret ? "independent" : "dependent");
2031 record_indep_loop (loop, ref, ret);
2033 return ret;
2036 /* Returns true if we can perform store motion of REF from LOOP. */
2038 static bool
2039 can_sm_ref_p (struct loop *loop, mem_ref_p ref)
2041 /* Unless the reference is stored in the loop, there is nothing to do. */
2042 if (!bitmap_bit_p (ref->stored, loop->num))
2043 return false;
2045 /* It should be movable. */
2046 if (!is_gimple_reg_type (TREE_TYPE (ref->mem))
2047 || TREE_THIS_VOLATILE (ref->mem)
2048 || !for_each_index (&ref->mem, may_move_till, loop))
2049 return false;
2051 /* If it can trap, it must be always executed in LOOP. */
2052 if (tree_could_trap_p (ref->mem)
2053 && !ref_always_accessed_p (loop, ref))
2054 return false;
2056 /* And it must be independent on all other memory references
2057 in LOOP. */
2058 if (!ref_indep_loop_p (loop, ref))
2059 return false;
2061 return true;
2064 /* Marks the references in LOOP for that store motion should be performed
2065 in REFS_TO_SM. SM_EXECUTED is the set of references for that store
2066 motion was performed in one of the outer loops. */
2068 static void
2069 find_refs_for_sm (struct loop *loop, bitmap sm_executed, bitmap refs_to_sm)
2071 bitmap refs = VEC_index (bitmap, memory_accesses.all_refs_in_loop,
2072 loop->num);
2073 unsigned i;
2074 bitmap_iterator bi;
2075 mem_ref_p ref;
2077 EXECUTE_IF_AND_COMPL_IN_BITMAP (refs, sm_executed, 0, i, bi)
2079 ref = VEC_index (mem_ref_p, memory_accesses.refs_list, i);
2080 if (can_sm_ref_p (loop, ref))
2081 bitmap_set_bit (refs_to_sm, i);
2085 /* Checks whether LOOP (with exits stored in EXITS array) is suitable
2086 for a store motion optimization (i.e. whether we can insert statement
2087 on its exits). */
2089 static bool
2090 loop_suitable_for_sm (struct loop *loop ATTRIBUTE_UNUSED,
2091 VEC (edge, heap) *exits)
2093 unsigned i;
2094 edge ex;
2096 for (i = 0; VEC_iterate (edge, exits, i, ex); i++)
2097 if (ex->flags & EDGE_ABNORMAL)
2098 return false;
2100 return true;
2103 /* Try to perform store motion for all memory references modified inside
2104 LOOP. SM_EXECUTED is the bitmap of the memory references for that
2105 store motion was executed in one of the outer loops. */
2107 static void
2108 store_motion_loop (struct loop *loop, bitmap sm_executed)
2110 VEC (edge, heap) *exits = get_loop_exit_edges (loop);
2111 struct loop *subloop;
2112 bitmap sm_in_loop = BITMAP_ALLOC (NULL);
2114 if (loop_suitable_for_sm (loop, exits))
2116 find_refs_for_sm (loop, sm_executed, sm_in_loop);
2117 hoist_memory_references (loop, sm_in_loop, exits);
2119 VEC_free (edge, heap, exits);
2121 bitmap_ior_into (sm_executed, sm_in_loop);
2122 for (subloop = loop->inner; subloop != NULL; subloop = subloop->next)
2123 store_motion_loop (subloop, sm_executed);
2124 bitmap_and_compl_into (sm_executed, sm_in_loop);
2125 BITMAP_FREE (sm_in_loop);
2128 /* Try to perform store motion for all memory references modified inside
2129 loops. */
2131 static void
2132 store_motion (void)
2134 struct loop *loop;
2135 bitmap sm_executed = BITMAP_ALLOC (NULL);
2137 for (loop = current_loops->tree_root->inner; loop != NULL; loop = loop->next)
2138 store_motion_loop (loop, sm_executed);
2140 BITMAP_FREE (sm_executed);
2141 gsi_commit_edge_inserts ();
2144 /* Fills ALWAYS_EXECUTED_IN information for basic blocks of LOOP, i.e.
2145 for each such basic block bb records the outermost loop for that execution
2146 of its header implies execution of bb. CONTAINS_CALL is the bitmap of
2147 blocks that contain a nonpure call. */
2149 static void
2150 fill_always_executed_in (struct loop *loop, sbitmap contains_call)
2152 basic_block bb = NULL, *bbs, last = NULL;
2153 unsigned i;
2154 edge e;
2155 struct loop *inn_loop = loop;
2157 if (!loop->header->aux)
2159 bbs = get_loop_body_in_dom_order (loop);
2161 for (i = 0; i < loop->num_nodes; i++)
2163 edge_iterator ei;
2164 bb = bbs[i];
2166 if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
2167 last = bb;
2169 if (TEST_BIT (contains_call, bb->index))
2170 break;
2172 FOR_EACH_EDGE (e, ei, bb->succs)
2173 if (!flow_bb_inside_loop_p (loop, e->dest))
2174 break;
2175 if (e)
2176 break;
2178 /* A loop might be infinite (TODO use simple loop analysis
2179 to disprove this if possible). */
2180 if (bb->flags & BB_IRREDUCIBLE_LOOP)
2181 break;
2183 if (!flow_bb_inside_loop_p (inn_loop, bb))
2184 break;
2186 if (bb->loop_father->header == bb)
2188 if (!dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
2189 break;
2191 /* In a loop that is always entered we may proceed anyway.
2192 But record that we entered it and stop once we leave it. */
2193 inn_loop = bb->loop_father;
2197 while (1)
2199 last->aux = loop;
2200 if (last == loop->header)
2201 break;
2202 last = get_immediate_dominator (CDI_DOMINATORS, last);
2205 free (bbs);
2208 for (loop = loop->inner; loop; loop = loop->next)
2209 fill_always_executed_in (loop, contains_call);
2212 /* Compute the global information needed by the loop invariant motion pass. */
2214 static void
2215 tree_ssa_lim_initialize (void)
2217 sbitmap contains_call = sbitmap_alloc (last_basic_block);
2218 gimple_stmt_iterator bsi;
2219 struct loop *loop;
2220 basic_block bb;
2222 sbitmap_zero (contains_call);
2223 FOR_EACH_BB (bb)
2225 for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
2227 if (nonpure_call_p (gsi_stmt (bsi)))
2228 break;
2231 if (!gsi_end_p (bsi))
2232 SET_BIT (contains_call, bb->index);
2235 for (loop = current_loops->tree_root->inner; loop; loop = loop->next)
2236 fill_always_executed_in (loop, contains_call);
2238 sbitmap_free (contains_call);
2240 lim_aux_data_map = pointer_map_create ();
2243 /* Cleans up after the invariant motion pass. */
2245 static void
2246 tree_ssa_lim_finalize (void)
2248 basic_block bb;
2249 unsigned i;
2250 bitmap b;
2251 htab_t h;
2253 FOR_EACH_BB (bb)
2255 bb->aux = NULL;
2258 pointer_map_destroy (lim_aux_data_map);
2260 VEC_free (mem_ref_p, heap, memory_accesses.refs_list);
2261 htab_delete (memory_accesses.refs);
2263 for (i = 0; VEC_iterate (bitmap, memory_accesses.refs_in_loop, i, b); i++)
2264 BITMAP_FREE (b);
2265 VEC_free (bitmap, heap, memory_accesses.refs_in_loop);
2267 for (i = 0; VEC_iterate (bitmap, memory_accesses.all_refs_in_loop, i, b); i++)
2268 BITMAP_FREE (b);
2269 VEC_free (bitmap, heap, memory_accesses.all_refs_in_loop);
2271 for (i = 0; VEC_iterate (bitmap, memory_accesses.clobbered_vops, i, b); i++)
2272 BITMAP_FREE (b);
2273 VEC_free (bitmap, heap, memory_accesses.clobbered_vops);
2275 for (i = 0; VEC_iterate (htab_t, memory_accesses.vop_ref_map, i, h); i++)
2276 htab_delete (h);
2277 VEC_free (htab_t, heap, memory_accesses.vop_ref_map);
2279 if (memory_accesses.ttae_cache)
2280 pointer_map_destroy (memory_accesses.ttae_cache);
2283 /* Moves invariants from loops. Only "expensive" invariants are moved out --
2284 i.e. those that are likely to be win regardless of the register pressure. */
2286 void
2287 tree_ssa_lim (void)
2289 tree_ssa_lim_initialize ();
2291 /* Gathers information about memory accesses in the loops. */
2292 analyze_memory_references ();
2294 /* For each statement determine the outermost loop in that it is
2295 invariant and cost for computing the invariant. */
2296 determine_invariantness ();
2298 /* Execute store motion. Force the necessary invariants to be moved
2299 out of the loops as well. */
2300 store_motion ();
2302 /* Move the expressions that are expensive enough. */
2303 move_computations ();
2305 tree_ssa_lim_finalize ();