1 /* Loop invariant motion.
2 Copyright (C) 2003-2022 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
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
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/>. */
22 #include "coretypes.h"
27 #include "tree-pass.h"
29 #include "gimple-pretty-print.h"
30 #include "fold-const.h"
34 #include "gimple-iterator.h"
36 #include "tree-ssa-loop-manip.h"
37 #include "tree-ssa-loop.h"
38 #include "tree-into-ssa.h"
40 #include "tree-affine.h"
41 #include "tree-ssa-propagate.h"
42 #include "trans-mem.h"
43 #include "gimple-fold.h"
44 #include "tree-scalar-evolution.h"
45 #include "tree-ssa-loop-niter.h"
51 /* TODO: Support for predicated code motion. I.e.
62 Where COND and INV are invariants, but evaluating INV may trap or be
63 invalid from some other reason if !COND. This may be transformed to
73 /* The auxiliary data kept for each statement. */
77 class loop
*max_loop
; /* The outermost loop in that the statement
80 class loop
*tgt_loop
; /* The loop out of that we want to move the
83 class loop
*always_executed_in
;
84 /* The outermost loop for that we are sure
85 the statement is executed if the loop
88 unsigned cost
; /* Cost of the computation performed by the
91 unsigned ref
; /* The simple_mem_ref in this stmt or 0. */
93 vec
<gimple
*> depends
; /* Vector of statements that must be also
94 hoisted out of the loop when this statement
95 is hoisted; i.e. those that define the
96 operands of the statement and are inside of
100 /* Maps statements to their lim_aux_data. */
102 static hash_map
<gimple
*, lim_aux_data
*> *lim_aux_data_map
;
104 /* Description of a memory reference location. */
108 tree
*ref
; /* The reference itself. */
109 gimple
*stmt
; /* The statement in that it occurs. */
113 /* Description of a memory reference. */
118 unsigned id
: 30; /* ID assigned to the memory reference
119 (its index in memory_accesses.refs_list) */
120 unsigned ref_canonical
: 1; /* Whether mem.ref was canonicalized. */
121 unsigned ref_decomposed
: 1; /* Whether the ref was hashed from mem. */
122 hashval_t hash
; /* Its hash value. */
124 /* The memory access itself and associated caching of alias-oracle
125 query meta-data. We are using mem.ref == error_mark_node for the
126 case the reference is represented by its single access stmt
127 in accesses_in_loop[0]. */
130 bitmap stored
; /* The set of loops in that this memory location
132 bitmap loaded
; /* The set of loops in that this memory location
134 vec
<mem_ref_loc
> accesses_in_loop
;
135 /* The locations of the accesses. */
137 /* The following set is computed on demand. */
138 bitmap_head dep_loop
; /* The set of loops in that the memory
139 reference is {in,}dependent in
143 /* We use six bits per loop in the ref->dep_loop bitmap to record
144 the dep_kind x dep_state combinations. */
146 enum dep_kind
{ lim_raw
, sm_war
, sm_waw
};
147 enum dep_state
{ dep_unknown
, dep_independent
, dep_dependent
};
149 /* coldest outermost loop for given loop. */
150 vec
<class loop
*> coldest_outermost_loop
;
151 /* hotter outer loop nearest to given loop. */
152 vec
<class loop
*> hotter_than_inner_loop
;
154 /* Populate the loop dependence cache of REF for LOOP, KIND with STATE. */
157 record_loop_dependence (class loop
*loop
, im_mem_ref
*ref
,
158 dep_kind kind
, dep_state state
)
160 gcc_assert (state
!= dep_unknown
);
161 unsigned bit
= 6 * loop
->num
+ kind
* 2 + state
== dep_dependent
? 1 : 0;
162 bitmap_set_bit (&ref
->dep_loop
, bit
);
165 /* Query the loop dependence cache of REF for LOOP, KIND. */
168 query_loop_dependence (class loop
*loop
, im_mem_ref
*ref
, dep_kind kind
)
170 unsigned first_bit
= 6 * loop
->num
+ kind
* 2;
171 if (bitmap_bit_p (&ref
->dep_loop
, first_bit
))
172 return dep_independent
;
173 else if (bitmap_bit_p (&ref
->dep_loop
, first_bit
+ 1))
174 return dep_dependent
;
178 /* Mem_ref hashtable helpers. */
180 struct mem_ref_hasher
: nofree_ptr_hash
<im_mem_ref
>
182 typedef ao_ref
*compare_type
;
183 static inline hashval_t
hash (const im_mem_ref
*);
184 static inline bool equal (const im_mem_ref
*, const ao_ref
*);
187 /* A hash function for class im_mem_ref object OBJ. */
190 mem_ref_hasher::hash (const im_mem_ref
*mem
)
195 /* An equality function for class im_mem_ref object MEM1 with
196 memory reference OBJ2. */
199 mem_ref_hasher::equal (const im_mem_ref
*mem1
, const ao_ref
*obj2
)
201 if (obj2
->max_size_known_p ())
202 return (mem1
->ref_decomposed
203 && ((TREE_CODE (mem1
->mem
.base
) == MEM_REF
204 && TREE_CODE (obj2
->base
) == MEM_REF
205 && operand_equal_p (TREE_OPERAND (mem1
->mem
.base
, 0),
206 TREE_OPERAND (obj2
->base
, 0), 0)
207 && known_eq (mem_ref_offset (mem1
->mem
.base
) * BITS_PER_UNIT
+ mem1
->mem
.offset
,
208 mem_ref_offset (obj2
->base
) * BITS_PER_UNIT
+ obj2
->offset
))
209 || (operand_equal_p (mem1
->mem
.base
, obj2
->base
, 0)
210 && known_eq (mem1
->mem
.offset
, obj2
->offset
)))
211 && known_eq (mem1
->mem
.size
, obj2
->size
)
212 && known_eq (mem1
->mem
.max_size
, obj2
->max_size
)
213 && mem1
->mem
.volatile_p
== obj2
->volatile_p
214 && (mem1
->mem
.ref_alias_set
== obj2
->ref_alias_set
215 /* We are not canonicalizing alias-sets but for the
216 special-case we didn't canonicalize yet and the
217 incoming ref is a alias-set zero MEM we pick
218 the correct one already. */
219 || (!mem1
->ref_canonical
220 && (TREE_CODE (obj2
->ref
) == MEM_REF
221 || TREE_CODE (obj2
->ref
) == TARGET_MEM_REF
)
222 && obj2
->ref_alias_set
== 0)
223 /* Likewise if there's a canonical ref with alias-set zero. */
224 || (mem1
->ref_canonical
&& mem1
->mem
.ref_alias_set
== 0))
225 && types_compatible_p (TREE_TYPE (mem1
->mem
.ref
),
226 TREE_TYPE (obj2
->ref
)));
228 return operand_equal_p (mem1
->mem
.ref
, obj2
->ref
, 0);
232 /* Description of memory accesses in loops. */
236 /* The hash table of memory references accessed in loops. */
237 hash_table
<mem_ref_hasher
> *refs
;
239 /* The list of memory references. */
240 vec
<im_mem_ref
*> refs_list
;
242 /* The set of memory references accessed in each loop. */
243 vec
<bitmap_head
> refs_loaded_in_loop
;
245 /* The set of memory references stored in each loop. */
246 vec
<bitmap_head
> refs_stored_in_loop
;
248 /* The set of memory references stored in each loop, including subloops . */
249 vec
<bitmap_head
> all_refs_stored_in_loop
;
251 /* Cache for expanding memory addresses. */
252 hash_map
<tree
, name_expansion
*> *ttae_cache
;
255 /* Obstack for the bitmaps in the above data structures. */
256 static bitmap_obstack lim_bitmap_obstack
;
257 static obstack mem_ref_obstack
;
259 static bool ref_indep_loop_p (class loop
*, im_mem_ref
*, dep_kind
);
260 static bool ref_always_accessed_p (class loop
*, im_mem_ref
*, bool);
261 static bool refs_independent_p (im_mem_ref
*, im_mem_ref
*, bool = true);
263 /* Minimum cost of an expensive expression. */
264 #define LIM_EXPENSIVE ((unsigned) param_lim_expensive)
266 /* The outermost loop for which execution of the header guarantees that the
267 block will be executed. */
268 #define ALWAYS_EXECUTED_IN(BB) ((class loop *) (BB)->aux)
269 #define SET_ALWAYS_EXECUTED_IN(BB, VAL) ((BB)->aux = (void *) (VAL))
271 /* ID of the shared unanalyzable mem. */
272 #define UNANALYZABLE_MEM_ID 0
274 /* Whether the reference was analyzable. */
275 #define MEM_ANALYZABLE(REF) ((REF)->id != UNANALYZABLE_MEM_ID)
277 static struct lim_aux_data
*
278 init_lim_data (gimple
*stmt
)
280 lim_aux_data
*p
= XCNEW (struct lim_aux_data
);
281 lim_aux_data_map
->put (stmt
, p
);
286 static struct lim_aux_data
*
287 get_lim_data (gimple
*stmt
)
289 lim_aux_data
**p
= lim_aux_data_map
->get (stmt
);
296 /* Releases the memory occupied by DATA. */
299 free_lim_aux_data (struct lim_aux_data
*data
)
301 data
->depends
.release ();
306 clear_lim_data (gimple
*stmt
)
308 lim_aux_data
**p
= lim_aux_data_map
->get (stmt
);
312 free_lim_aux_data (*p
);
317 /* The possibilities of statement movement. */
320 MOVE_IMPOSSIBLE
, /* No movement -- side effect expression. */
321 MOVE_PRESERVE_EXECUTION
, /* Must not cause the non-executed statement
322 become executed -- memory accesses, ... */
323 MOVE_POSSIBLE
/* Unlimited movement. */
327 /* If it is possible to hoist the statement STMT unconditionally,
328 returns MOVE_POSSIBLE.
329 If it is possible to hoist the statement STMT, but we must avoid making
330 it executed if it would not be executed in the original program (e.g.
331 because it may trap), return MOVE_PRESERVE_EXECUTION.
332 Otherwise return MOVE_IMPOSSIBLE. */
335 movement_possibility (gimple
*stmt
)
338 enum move_pos ret
= MOVE_POSSIBLE
;
340 if (flag_unswitch_loops
341 && gimple_code (stmt
) == GIMPLE_COND
)
343 /* If we perform unswitching, force the operands of the invariant
344 condition to be moved out of the loop. */
345 return MOVE_POSSIBLE
;
348 if (gimple_code (stmt
) == GIMPLE_PHI
349 && gimple_phi_num_args (stmt
) <= 2
350 && !virtual_operand_p (gimple_phi_result (stmt
))
351 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt
)))
352 return MOVE_POSSIBLE
;
354 if (gimple_get_lhs (stmt
) == NULL_TREE
)
355 return MOVE_IMPOSSIBLE
;
357 if (gimple_vdef (stmt
))
358 return MOVE_IMPOSSIBLE
;
360 if (stmt_ends_bb_p (stmt
)
361 || gimple_has_volatile_ops (stmt
)
362 || gimple_has_side_effects (stmt
)
363 || stmt_could_throw_p (cfun
, stmt
))
364 return MOVE_IMPOSSIBLE
;
366 if (is_gimple_call (stmt
))
368 /* While pure or const call is guaranteed to have no side effects, we
369 cannot move it arbitrarily. Consider code like
371 char *s = something ();
381 Here the strlen call cannot be moved out of the loop, even though
382 s is invariant. In addition to possibly creating a call with
383 invalid arguments, moving out a function call that is not executed
384 may cause performance regressions in case the call is costly and
385 not executed at all. */
386 ret
= MOVE_PRESERVE_EXECUTION
;
387 lhs
= gimple_call_lhs (stmt
);
389 else if (is_gimple_assign (stmt
))
390 lhs
= gimple_assign_lhs (stmt
);
392 return MOVE_IMPOSSIBLE
;
394 if (TREE_CODE (lhs
) == SSA_NAME
395 && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
))
396 return MOVE_IMPOSSIBLE
;
398 if (TREE_CODE (lhs
) != SSA_NAME
399 || gimple_could_trap_p (stmt
))
400 return MOVE_PRESERVE_EXECUTION
;
402 /* Non local loads in a transaction cannot be hoisted out. Well,
403 unless the load happens on every path out of the loop, but we
404 don't take this into account yet. */
406 && gimple_in_transaction (stmt
)
407 && gimple_assign_single_p (stmt
))
409 tree rhs
= gimple_assign_rhs1 (stmt
);
410 if (DECL_P (rhs
) && is_global_var (rhs
))
414 fprintf (dump_file
, "Cannot hoist conditional load of ");
415 print_generic_expr (dump_file
, rhs
, TDF_SLIM
);
416 fprintf (dump_file
, " because it is in a transaction.\n");
418 return MOVE_IMPOSSIBLE
;
425 /* Compare the profile count inequality of bb and loop's preheader, it is
426 three-state as stated in profile-count.h, FALSE is returned if inequality
427 cannot be decided. */
429 bb_colder_than_loop_preheader (basic_block bb
, class loop
*loop
)
431 gcc_assert (bb
&& loop
);
432 return bb
->count
< loop_preheader_edge (loop
)->src
->count
;
435 /* Check coldest loop between OUTERMOST_LOOP and LOOP by comparing profile
437 It does three steps check:
438 1) Check whether CURR_BB is cold in it's own loop_father, if it is cold, just
439 return NULL which means it should not be moved out at all;
440 2) CURR_BB is NOT cold, check if pre-computed COLDEST_LOOP is outside of
441 OUTERMOST_LOOP, if it is inside of OUTERMOST_LOOP, return the COLDEST_LOOP;
442 3) If COLDEST_LOOP is outside of OUTERMOST_LOOP, check whether there is a
443 hotter loop between OUTERMOST_LOOP and loop in pre-computed
444 HOTTER_THAN_INNER_LOOP, return it's nested inner loop, otherwise return
446 At last, the coldest_loop is inside of OUTERMOST_LOOP, just return it as
450 get_coldest_out_loop (class loop
*outermost_loop
, class loop
*loop
,
453 gcc_assert (outermost_loop
== loop
454 || flow_loop_nested_p (outermost_loop
, loop
));
456 /* If bb_colder_than_loop_preheader returns false due to three-state
457 comparision, OUTERMOST_LOOP is returned finally to preserve the behavior.
458 Otherwise, return the coldest loop between OUTERMOST_LOOP and LOOP. */
459 if (curr_bb
&& bb_colder_than_loop_preheader (curr_bb
, loop
))
462 class loop
*coldest_loop
= coldest_outermost_loop
[loop
->num
];
463 if (loop_depth (coldest_loop
) < loop_depth (outermost_loop
))
465 class loop
*hotter_loop
= hotter_than_inner_loop
[loop
->num
];
467 || loop_depth (hotter_loop
) < loop_depth (outermost_loop
))
468 return outermost_loop
;
470 /* hotter_loop is between OUTERMOST_LOOP and LOOP like:
471 [loop tree root, ..., coldest_loop, ..., outermost_loop, ...,
472 hotter_loop, second_coldest_loop, ..., loop]
473 return second_coldest_loop to be the hoist target. */
475 for (aloop
= hotter_loop
->inner
; aloop
; aloop
= aloop
->next
)
476 if (aloop
== loop
|| flow_loop_nested_p (aloop
, loop
))
482 /* Suppose that operand DEF is used inside the LOOP. Returns the outermost
483 loop to that we could move the expression using DEF if it did not have
484 other operands, i.e. the outermost loop enclosing LOOP in that the value
485 of DEF is invariant. */
488 outermost_invariant_loop (tree def
, class loop
*loop
)
492 class loop
*max_loop
;
493 struct lim_aux_data
*lim_data
;
496 return superloop_at_depth (loop
, 1);
498 if (TREE_CODE (def
) != SSA_NAME
)
500 gcc_assert (is_gimple_min_invariant (def
));
501 return superloop_at_depth (loop
, 1);
504 def_stmt
= SSA_NAME_DEF_STMT (def
);
505 def_bb
= gimple_bb (def_stmt
);
507 return superloop_at_depth (loop
, 1);
509 max_loop
= find_common_loop (loop
, def_bb
->loop_father
);
511 lim_data
= get_lim_data (def_stmt
);
512 if (lim_data
!= NULL
&& lim_data
->max_loop
!= NULL
)
513 max_loop
= find_common_loop (max_loop
,
514 loop_outer (lim_data
->max_loop
));
515 if (max_loop
== loop
)
517 max_loop
= superloop_at_depth (loop
, loop_depth (max_loop
) + 1);
522 /* DATA is a structure containing information associated with a statement
523 inside LOOP. DEF is one of the operands of this statement.
525 Find the outermost loop enclosing LOOP in that value of DEF is invariant
526 and record this in DATA->max_loop field. If DEF itself is defined inside
527 this loop as well (i.e. we need to hoist it out of the loop if we want
528 to hoist the statement represented by DATA), record the statement in that
529 DEF is defined to the DATA->depends list. Additionally if ADD_COST is true,
530 add the cost of the computation of DEF to the DATA->cost.
532 If DEF is not invariant in LOOP, return false. Otherwise return TRUE. */
535 add_dependency (tree def
, struct lim_aux_data
*data
, class loop
*loop
,
538 gimple
*def_stmt
= SSA_NAME_DEF_STMT (def
);
539 basic_block def_bb
= gimple_bb (def_stmt
);
540 class loop
*max_loop
;
541 struct lim_aux_data
*def_data
;
546 max_loop
= outermost_invariant_loop (def
, loop
);
550 if (flow_loop_nested_p (data
->max_loop
, max_loop
))
551 data
->max_loop
= max_loop
;
553 def_data
= get_lim_data (def_stmt
);
558 /* Only add the cost if the statement defining DEF is inside LOOP,
559 i.e. if it is likely that by moving the invariants dependent
560 on it, we will be able to avoid creating a new register for
561 it (since it will be only used in these dependent invariants). */
562 && def_bb
->loop_father
== loop
)
563 data
->cost
+= def_data
->cost
;
565 data
->depends
.safe_push (def_stmt
);
570 /* Returns an estimate for a cost of statement STMT. The values here
571 are just ad-hoc constants, similar to costs for inlining. */
574 stmt_cost (gimple
*stmt
)
576 /* Always try to create possibilities for unswitching. */
577 if (gimple_code (stmt
) == GIMPLE_COND
578 || gimple_code (stmt
) == GIMPLE_PHI
)
579 return LIM_EXPENSIVE
;
581 /* We should be hoisting calls if possible. */
582 if (is_gimple_call (stmt
))
586 /* Unless the call is a builtin_constant_p; this always folds to a
587 constant, so moving it is useless. */
588 fndecl
= gimple_call_fndecl (stmt
);
589 if (fndecl
&& fndecl_built_in_p (fndecl
, BUILT_IN_CONSTANT_P
))
592 return LIM_EXPENSIVE
;
595 /* Hoisting memory references out should almost surely be a win. */
596 if (gimple_references_memory_p (stmt
))
597 return LIM_EXPENSIVE
;
599 if (gimple_code (stmt
) != GIMPLE_ASSIGN
)
602 switch (gimple_assign_rhs_code (stmt
))
605 case WIDEN_MULT_EXPR
:
606 case WIDEN_MULT_PLUS_EXPR
:
607 case WIDEN_MULT_MINUS_EXPR
:
619 /* Division and multiplication are usually expensive. */
620 return LIM_EXPENSIVE
;
624 case WIDEN_LSHIFT_EXPR
:
627 /* Shifts and rotates are usually expensive. */
628 return LIM_EXPENSIVE
;
631 /* Make vector construction cost proportional to the number
633 return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt
));
637 /* Whether or not something is wrapped inside a PAREN_EXPR
638 should not change move cost. Nor should an intermediate
639 unpropagated SSA name copy. */
647 /* Finds the outermost loop between OUTER and LOOP in that the memory reference
648 REF is independent. If REF is not independent in LOOP, NULL is returned
652 outermost_indep_loop (class loop
*outer
, class loop
*loop
, im_mem_ref
*ref
)
656 if (ref
->stored
&& bitmap_bit_p (ref
->stored
, loop
->num
))
661 aloop
= superloop_at_depth (loop
, loop_depth (aloop
) + 1))
662 if ((!ref
->stored
|| !bitmap_bit_p (ref
->stored
, aloop
->num
))
663 && ref_indep_loop_p (aloop
, ref
, lim_raw
))
666 if (ref_indep_loop_p (loop
, ref
, lim_raw
))
672 /* If there is a simple load or store to a memory reference in STMT, returns
673 the location of the memory reference, and sets IS_STORE according to whether
674 it is a store or load. Otherwise, returns NULL. */
677 simple_mem_ref_in_stmt (gimple
*stmt
, bool *is_store
)
681 /* Recognize SSA_NAME = MEM and MEM = (SSA_NAME | invariant) patterns. */
682 if (!gimple_assign_single_p (stmt
))
685 lhs
= gimple_assign_lhs_ptr (stmt
);
686 rhs
= gimple_assign_rhs1_ptr (stmt
);
688 if (TREE_CODE (*lhs
) == SSA_NAME
&& gimple_vuse (stmt
))
693 else if (gimple_vdef (stmt
)
694 && (TREE_CODE (*rhs
) == SSA_NAME
|| is_gimple_min_invariant (*rhs
)))
703 /* From a controlling predicate in DOM determine the arguments from
704 the PHI node PHI that are chosen if the predicate evaluates to
705 true and false and store them to *TRUE_ARG_P and *FALSE_ARG_P if
706 they are non-NULL. Returns true if the arguments can be determined,
707 else return false. */
710 extract_true_false_args_from_phi (basic_block dom
, gphi
*phi
,
711 tree
*true_arg_p
, tree
*false_arg_p
)
714 if (! extract_true_false_controlled_edges (dom
, gimple_bb (phi
),
719 *true_arg_p
= PHI_ARG_DEF (phi
, te
->dest_idx
);
721 *false_arg_p
= PHI_ARG_DEF (phi
, fe
->dest_idx
);
726 /* Determine the outermost loop to that it is possible to hoist a statement
727 STMT and store it to LIM_DATA (STMT)->max_loop. To do this we determine
728 the outermost loop in that the value computed by STMT is invariant.
729 If MUST_PRESERVE_EXEC is true, additionally choose such a loop that
730 we preserve the fact whether STMT is executed. It also fills other related
731 information to LIM_DATA (STMT).
733 The function returns false if STMT cannot be hoisted outside of the loop it
734 is defined in, and true otherwise. */
737 determine_max_movement (gimple
*stmt
, bool must_preserve_exec
)
739 basic_block bb
= gimple_bb (stmt
);
740 class loop
*loop
= bb
->loop_father
;
742 struct lim_aux_data
*lim_data
= get_lim_data (stmt
);
746 if (must_preserve_exec
)
747 level
= ALWAYS_EXECUTED_IN (bb
);
749 level
= superloop_at_depth (loop
, 1);
750 lim_data
->max_loop
= get_coldest_out_loop (level
, loop
, bb
);
751 if (!lim_data
->max_loop
)
754 if (gphi
*phi
= dyn_cast
<gphi
*> (stmt
))
757 unsigned min_cost
= UINT_MAX
;
758 unsigned total_cost
= 0;
759 struct lim_aux_data
*def_data
;
761 /* We will end up promoting dependencies to be unconditionally
762 evaluated. For this reason the PHI cost (and thus the
763 cost we remove from the loop by doing the invariant motion)
764 is that of the cheapest PHI argument dependency chain. */
765 FOR_EACH_PHI_ARG (use_p
, phi
, iter
, SSA_OP_USE
)
767 val
= USE_FROM_PTR (use_p
);
769 if (TREE_CODE (val
) != SSA_NAME
)
771 /* Assign const 1 to constants. */
772 min_cost
= MIN (min_cost
, 1);
776 if (!add_dependency (val
, lim_data
, loop
, false))
779 gimple
*def_stmt
= SSA_NAME_DEF_STMT (val
);
780 if (gimple_bb (def_stmt
)
781 && gimple_bb (def_stmt
)->loop_father
== loop
)
783 def_data
= get_lim_data (def_stmt
);
786 min_cost
= MIN (min_cost
, def_data
->cost
);
787 total_cost
+= def_data
->cost
;
792 min_cost
= MIN (min_cost
, total_cost
);
793 lim_data
->cost
+= min_cost
;
795 if (gimple_phi_num_args (phi
) > 1)
797 basic_block dom
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
799 if (gsi_end_p (gsi_last_bb (dom
)))
801 cond
= gsi_stmt (gsi_last_bb (dom
));
802 if (gimple_code (cond
) != GIMPLE_COND
)
804 /* Verify that this is an extended form of a diamond and
805 the PHI arguments are completely controlled by the
807 if (!extract_true_false_args_from_phi (dom
, phi
, NULL
, NULL
))
810 /* Fold in dependencies and cost of the condition. */
811 FOR_EACH_SSA_TREE_OPERAND (val
, cond
, iter
, SSA_OP_USE
)
813 if (!add_dependency (val
, lim_data
, loop
, false))
815 def_data
= get_lim_data (SSA_NAME_DEF_STMT (val
));
817 lim_data
->cost
+= def_data
->cost
;
820 /* We want to avoid unconditionally executing very expensive
821 operations. As costs for our dependencies cannot be
822 negative just claim we are not invariand for this case.
823 We also are not sure whether the control-flow inside the
825 if (total_cost
- min_cost
>= 2 * LIM_EXPENSIVE
827 && total_cost
/ min_cost
<= 2))
830 /* Assume that the control-flow in the loop will vanish.
831 ??? We should verify this and not artificially increase
832 the cost if that is not the case. */
833 lim_data
->cost
+= stmt_cost (stmt
);
839 /* A stmt that receives abnormal edges cannot be hoisted. */
840 if (is_a
<gcall
*> (stmt
)
841 && (gimple_call_flags (stmt
) & ECF_RETURNS_TWICE
))
844 FOR_EACH_SSA_TREE_OPERAND (val
, stmt
, iter
, SSA_OP_USE
)
845 if (!add_dependency (val
, lim_data
, loop
, true))
848 if (gimple_vuse (stmt
))
851 = lim_data
? memory_accesses
.refs_list
[lim_data
->ref
] : NULL
;
853 && MEM_ANALYZABLE (ref
))
855 lim_data
->max_loop
= outermost_indep_loop (lim_data
->max_loop
,
857 if (!lim_data
->max_loop
)
860 else if (! add_dependency (gimple_vuse (stmt
), lim_data
, loop
, false))
864 lim_data
->cost
+= stmt_cost (stmt
);
869 /* Suppose that some statement in ORIG_LOOP is hoisted to the loop LEVEL,
870 and that one of the operands of this statement is computed by STMT.
871 Ensure that STMT (together with all the statements that define its
872 operands) is hoisted at least out of the loop LEVEL. */
875 set_level (gimple
*stmt
, class loop
*orig_loop
, class loop
*level
)
877 class loop
*stmt_loop
= gimple_bb (stmt
)->loop_father
;
878 struct lim_aux_data
*lim_data
;
882 stmt_loop
= find_common_loop (orig_loop
, stmt_loop
);
883 lim_data
= get_lim_data (stmt
);
884 if (lim_data
!= NULL
&& lim_data
->tgt_loop
!= NULL
)
885 stmt_loop
= find_common_loop (stmt_loop
,
886 loop_outer (lim_data
->tgt_loop
));
887 if (flow_loop_nested_p (stmt_loop
, level
))
890 gcc_assert (level
== lim_data
->max_loop
891 || flow_loop_nested_p (lim_data
->max_loop
, level
));
893 lim_data
->tgt_loop
= level
;
894 FOR_EACH_VEC_ELT (lim_data
->depends
, i
, dep_stmt
)
895 set_level (dep_stmt
, orig_loop
, level
);
898 /* Determines an outermost loop from that we want to hoist the statement STMT.
899 For now we chose the outermost possible loop. TODO -- use profiling
900 information to set it more sanely. */
903 set_profitable_level (gimple
*stmt
)
905 set_level (stmt
, gimple_bb (stmt
)->loop_father
, get_lim_data (stmt
)->max_loop
);
908 /* Returns true if STMT is a call that has side effects. */
911 nonpure_call_p (gimple
*stmt
)
913 if (gimple_code (stmt
) != GIMPLE_CALL
)
916 return gimple_has_side_effects (stmt
);
919 /* Rewrite a/b to a*(1/b). Return the invariant stmt to process. */
922 rewrite_reciprocal (gimple_stmt_iterator
*bsi
)
924 gassign
*stmt
, *stmt1
, *stmt2
;
925 tree name
, lhs
, type
;
927 gimple_stmt_iterator gsi
;
929 stmt
= as_a
<gassign
*> (gsi_stmt (*bsi
));
930 lhs
= gimple_assign_lhs (stmt
);
931 type
= TREE_TYPE (lhs
);
933 real_one
= build_one_cst (type
);
935 name
= make_temp_ssa_name (type
, NULL
, "reciptmp");
936 stmt1
= gimple_build_assign (name
, RDIV_EXPR
, real_one
,
937 gimple_assign_rhs2 (stmt
));
938 stmt2
= gimple_build_assign (lhs
, MULT_EXPR
, name
,
939 gimple_assign_rhs1 (stmt
));
941 /* Replace division stmt with reciprocal and multiply stmts.
942 The multiply stmt is not invariant, so update iterator
943 and avoid rescanning. */
945 gsi_insert_before (bsi
, stmt1
, GSI_NEW_STMT
);
946 gsi_replace (&gsi
, stmt2
, true);
948 /* Continue processing with invariant reciprocal statement. */
952 /* Check if the pattern at *BSI is a bittest of the form
953 (A >> B) & 1 != 0 and in this case rewrite it to A & (1 << B) != 0. */
956 rewrite_bittest (gimple_stmt_iterator
*bsi
)
963 tree lhs
, name
, t
, a
, b
;
966 stmt
= as_a
<gassign
*> (gsi_stmt (*bsi
));
967 lhs
= gimple_assign_lhs (stmt
);
969 /* Verify that the single use of lhs is a comparison against zero. */
970 if (TREE_CODE (lhs
) != SSA_NAME
971 || !single_imm_use (lhs
, &use
, &use_stmt
))
973 cond_stmt
= dyn_cast
<gcond
*> (use_stmt
);
976 if (gimple_cond_lhs (cond_stmt
) != lhs
977 || (gimple_cond_code (cond_stmt
) != NE_EXPR
978 && gimple_cond_code (cond_stmt
) != EQ_EXPR
)
979 || !integer_zerop (gimple_cond_rhs (cond_stmt
)))
982 /* Get at the operands of the shift. The rhs is TMP1 & 1. */
983 stmt1
= SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt
));
984 if (gimple_code (stmt1
) != GIMPLE_ASSIGN
)
987 /* There is a conversion in between possibly inserted by fold. */
988 if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt1
)))
990 t
= gimple_assign_rhs1 (stmt1
);
991 if (TREE_CODE (t
) != SSA_NAME
992 || !has_single_use (t
))
994 stmt1
= SSA_NAME_DEF_STMT (t
);
995 if (gimple_code (stmt1
) != GIMPLE_ASSIGN
)
999 /* Verify that B is loop invariant but A is not. Verify that with
1000 all the stmt walking we are still in the same loop. */
1001 if (gimple_assign_rhs_code (stmt1
) != RSHIFT_EXPR
1002 || loop_containing_stmt (stmt1
) != loop_containing_stmt (stmt
))
1005 a
= gimple_assign_rhs1 (stmt1
);
1006 b
= gimple_assign_rhs2 (stmt1
);
1008 if (outermost_invariant_loop (b
, loop_containing_stmt (stmt1
)) != NULL
1009 && outermost_invariant_loop (a
, loop_containing_stmt (stmt1
)) == NULL
)
1011 gimple_stmt_iterator rsi
;
1014 t
= fold_build2 (LSHIFT_EXPR
, TREE_TYPE (a
),
1015 build_int_cst (TREE_TYPE (a
), 1), b
);
1016 name
= make_temp_ssa_name (TREE_TYPE (a
), NULL
, "shifttmp");
1017 stmt1
= gimple_build_assign (name
, t
);
1020 t
= fold_build2 (BIT_AND_EXPR
, TREE_TYPE (a
), a
, name
);
1021 name
= make_temp_ssa_name (TREE_TYPE (a
), NULL
, "shifttmp");
1022 stmt2
= gimple_build_assign (name
, t
);
1024 /* Replace the SSA_NAME we compare against zero. Adjust
1025 the type of zero accordingly. */
1026 SET_USE (use
, name
);
1027 gimple_cond_set_rhs (cond_stmt
,
1028 build_int_cst_type (TREE_TYPE (name
),
1031 /* Don't use gsi_replace here, none of the new assignments sets
1032 the variable originally set in stmt. Move bsi to stmt1, and
1033 then remove the original stmt, so that we get a chance to
1034 retain debug info for it. */
1036 gsi_insert_before (bsi
, stmt1
, GSI_NEW_STMT
);
1037 gsi_insert_before (&rsi
, stmt2
, GSI_SAME_STMT
);
1038 gimple
*to_release
= gsi_stmt (rsi
);
1039 gsi_remove (&rsi
, true);
1040 release_defs (to_release
);
1048 /* Determine the outermost loops in that statements in basic block BB are
1049 invariant, and record them to the LIM_DATA associated with the
1053 compute_invariantness (basic_block bb
)
1056 gimple_stmt_iterator bsi
;
1058 bool maybe_never
= ALWAYS_EXECUTED_IN (bb
) == NULL
;
1059 class loop
*outermost
= ALWAYS_EXECUTED_IN (bb
);
1060 struct lim_aux_data
*lim_data
;
1062 if (!loop_outer (bb
->loop_father
))
1065 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1066 fprintf (dump_file
, "Basic block %d (loop %d -- depth %d):\n\n",
1067 bb
->index
, bb
->loop_father
->num
, loop_depth (bb
->loop_father
));
1069 /* Look at PHI nodes, but only if there is at most two.
1070 ??? We could relax this further by post-processing the inserted
1071 code and transforming adjacent cond-exprs with the same predicate
1072 to control flow again. */
1073 bsi
= gsi_start_phis (bb
);
1074 if (!gsi_end_p (bsi
)
1075 && ((gsi_next (&bsi
), gsi_end_p (bsi
))
1076 || (gsi_next (&bsi
), gsi_end_p (bsi
))))
1077 for (bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
1079 stmt
= gsi_stmt (bsi
);
1081 pos
= movement_possibility (stmt
);
1082 if (pos
== MOVE_IMPOSSIBLE
)
1085 lim_data
= get_lim_data (stmt
);
1087 lim_data
= init_lim_data (stmt
);
1088 lim_data
->always_executed_in
= outermost
;
1090 if (!determine_max_movement (stmt
, false))
1092 lim_data
->max_loop
= NULL
;
1096 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1098 print_gimple_stmt (dump_file
, stmt
, 2);
1099 fprintf (dump_file
, " invariant up to level %d, cost %d.\n\n",
1100 loop_depth (lim_data
->max_loop
),
1104 if (lim_data
->cost
>= LIM_EXPENSIVE
)
1105 set_profitable_level (stmt
);
1108 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
1110 stmt
= gsi_stmt (bsi
);
1112 pos
= movement_possibility (stmt
);
1113 if (pos
== MOVE_IMPOSSIBLE
)
1115 if (nonpure_call_p (stmt
))
1120 /* Make sure to note always_executed_in for stores to make
1121 store-motion work. */
1122 else if (stmt_makes_single_store (stmt
))
1124 struct lim_aux_data
*lim_data
= get_lim_data (stmt
);
1126 lim_data
= init_lim_data (stmt
);
1127 lim_data
->always_executed_in
= outermost
;
1132 if (is_gimple_assign (stmt
)
1133 && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt
))
1134 == GIMPLE_BINARY_RHS
))
1136 tree op0
= gimple_assign_rhs1 (stmt
);
1137 tree op1
= gimple_assign_rhs2 (stmt
);
1138 class loop
*ol1
= outermost_invariant_loop (op1
,
1139 loop_containing_stmt (stmt
));
1141 /* If divisor is invariant, convert a/b to a*(1/b), allowing reciprocal
1142 to be hoisted out of loop, saving expensive divide. */
1143 if (pos
== MOVE_POSSIBLE
1144 && gimple_assign_rhs_code (stmt
) == RDIV_EXPR
1145 && flag_unsafe_math_optimizations
1146 && !flag_trapping_math
1148 && outermost_invariant_loop (op0
, ol1
) == NULL
)
1149 stmt
= rewrite_reciprocal (&bsi
);
1151 /* If the shift count is invariant, convert (A >> B) & 1 to
1152 A & (1 << B) allowing the bit mask to be hoisted out of the loop
1153 saving an expensive shift. */
1154 if (pos
== MOVE_POSSIBLE
1155 && gimple_assign_rhs_code (stmt
) == BIT_AND_EXPR
1156 && integer_onep (op1
)
1157 && TREE_CODE (op0
) == SSA_NAME
1158 && has_single_use (op0
))
1159 stmt
= rewrite_bittest (&bsi
);
1162 lim_data
= get_lim_data (stmt
);
1164 lim_data
= init_lim_data (stmt
);
1165 lim_data
->always_executed_in
= outermost
;
1167 if (maybe_never
&& pos
== MOVE_PRESERVE_EXECUTION
)
1170 if (!determine_max_movement (stmt
, pos
== MOVE_PRESERVE_EXECUTION
))
1172 lim_data
->max_loop
= NULL
;
1176 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1178 print_gimple_stmt (dump_file
, stmt
, 2);
1179 fprintf (dump_file
, " invariant up to level %d, cost %d.\n\n",
1180 loop_depth (lim_data
->max_loop
),
1184 if (lim_data
->cost
>= LIM_EXPENSIVE
)
1185 set_profitable_level (stmt
);
1189 /* Hoist the statements in basic block BB out of the loops prescribed by
1190 data stored in LIM_DATA structures associated with each statement. Callback
1191 for walk_dominator_tree. */
1194 move_computations_worker (basic_block bb
)
1198 struct lim_aux_data
*lim_data
;
1199 unsigned int todo
= 0;
1201 if (!loop_outer (bb
->loop_father
))
1204 for (gphi_iterator bsi
= gsi_start_phis (bb
); !gsi_end_p (bsi
); )
1207 gphi
*stmt
= bsi
.phi ();
1209 lim_data
= get_lim_data (stmt
);
1210 if (lim_data
== NULL
)
1216 cost
= lim_data
->cost
;
1217 level
= lim_data
->tgt_loop
;
1218 clear_lim_data (stmt
);
1226 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1228 fprintf (dump_file
, "Moving PHI node\n");
1229 print_gimple_stmt (dump_file
, stmt
, 0);
1230 fprintf (dump_file
, "(cost %u) out of loop %d.\n\n",
1234 if (gimple_phi_num_args (stmt
) == 1)
1236 tree arg
= PHI_ARG_DEF (stmt
, 0);
1237 new_stmt
= gimple_build_assign (gimple_phi_result (stmt
),
1238 TREE_CODE (arg
), arg
);
1242 basic_block dom
= get_immediate_dominator (CDI_DOMINATORS
, bb
);
1243 gimple
*cond
= gsi_stmt (gsi_last_bb (dom
));
1244 tree arg0
= NULL_TREE
, arg1
= NULL_TREE
, t
;
1245 /* Get the PHI arguments corresponding to the true and false
1247 extract_true_false_args_from_phi (dom
, stmt
, &arg0
, &arg1
);
1248 gcc_assert (arg0
&& arg1
);
1249 t
= make_ssa_name (boolean_type_node
);
1250 new_stmt
= gimple_build_assign (t
, gimple_cond_code (cond
),
1251 gimple_cond_lhs (cond
),
1252 gimple_cond_rhs (cond
));
1253 gsi_insert_on_edge (loop_preheader_edge (level
), new_stmt
);
1254 new_stmt
= gimple_build_assign (gimple_phi_result (stmt
),
1255 COND_EXPR
, t
, arg0
, arg1
);
1256 todo
|= TODO_cleanup_cfg
;
1258 if (!ALWAYS_EXECUTED_IN (bb
)
1259 || (ALWAYS_EXECUTED_IN (bb
) != level
1260 && !flow_loop_nested_p (ALWAYS_EXECUTED_IN (bb
), level
)))
1261 reset_flow_sensitive_info (gimple_assign_lhs (new_stmt
));
1262 gsi_insert_on_edge (loop_preheader_edge (level
), new_stmt
);
1263 remove_phi_node (&bsi
, false);
1266 for (gimple_stmt_iterator bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); )
1270 gimple
*stmt
= gsi_stmt (bsi
);
1272 lim_data
= get_lim_data (stmt
);
1273 if (lim_data
== NULL
)
1279 cost
= lim_data
->cost
;
1280 level
= lim_data
->tgt_loop
;
1281 clear_lim_data (stmt
);
1289 /* We do not really want to move conditionals out of the loop; we just
1290 placed it here to force its operands to be moved if necessary. */
1291 if (gimple_code (stmt
) == GIMPLE_COND
)
1297 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1299 fprintf (dump_file
, "Moving statement\n");
1300 print_gimple_stmt (dump_file
, stmt
, 0);
1301 fprintf (dump_file
, "(cost %u) out of loop %d.\n\n",
1305 e
= loop_preheader_edge (level
);
1306 gcc_assert (!gimple_vdef (stmt
));
1307 if (gimple_vuse (stmt
))
1309 /* The new VUSE is the one from the virtual PHI in the loop
1310 header or the one already present. */
1312 for (gsi2
= gsi_start_phis (e
->dest
);
1313 !gsi_end_p (gsi2
); gsi_next (&gsi2
))
1315 gphi
*phi
= gsi2
.phi ();
1316 if (virtual_operand_p (gimple_phi_result (phi
)))
1318 SET_USE (gimple_vuse_op (stmt
),
1319 PHI_ARG_DEF_FROM_EDGE (phi
, e
));
1324 gsi_remove (&bsi
, false);
1325 if (gimple_has_lhs (stmt
)
1326 && TREE_CODE (gimple_get_lhs (stmt
)) == SSA_NAME
1327 && (!ALWAYS_EXECUTED_IN (bb
)
1328 || !(ALWAYS_EXECUTED_IN (bb
) == level
1329 || flow_loop_nested_p (ALWAYS_EXECUTED_IN (bb
), level
))))
1330 reset_flow_sensitive_info (gimple_get_lhs (stmt
));
1331 /* In case this is a stmt that is not unconditionally executed
1332 when the target loop header is executed and the stmt may
1333 invoke undefined integer or pointer overflow rewrite it to
1334 unsigned arithmetic. */
1335 if (is_gimple_assign (stmt
)
1336 && INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt
)))
1337 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (gimple_assign_lhs (stmt
)))
1338 && arith_code_with_undefined_signed_overflow
1339 (gimple_assign_rhs_code (stmt
))
1340 && (!ALWAYS_EXECUTED_IN (bb
)
1341 || !(ALWAYS_EXECUTED_IN (bb
) == level
1342 || flow_loop_nested_p (ALWAYS_EXECUTED_IN (bb
), level
))))
1343 gsi_insert_seq_on_edge (e
, rewrite_to_defined_overflow (stmt
));
1345 gsi_insert_on_edge (e
, stmt
);
1351 /* Checks whether the statement defining variable *INDEX can be hoisted
1352 out of the loop passed in DATA. Callback for for_each_index. */
1355 may_move_till (tree ref
, tree
*index
, void *data
)
1357 class loop
*loop
= (class loop
*) data
, *max_loop
;
1359 /* If REF is an array reference, check also that the step and the lower
1360 bound is invariant in LOOP. */
1361 if (TREE_CODE (ref
) == ARRAY_REF
)
1363 tree step
= TREE_OPERAND (ref
, 3);
1364 tree lbound
= TREE_OPERAND (ref
, 2);
1366 max_loop
= outermost_invariant_loop (step
, loop
);
1370 max_loop
= outermost_invariant_loop (lbound
, loop
);
1375 max_loop
= outermost_invariant_loop (*index
, loop
);
1382 /* If OP is SSA NAME, force the statement that defines it to be
1383 moved out of the LOOP. ORIG_LOOP is the loop in that EXPR is used. */
1386 force_move_till_op (tree op
, class loop
*orig_loop
, class loop
*loop
)
1391 || is_gimple_min_invariant (op
))
1394 gcc_assert (TREE_CODE (op
) == SSA_NAME
);
1396 stmt
= SSA_NAME_DEF_STMT (op
);
1397 if (gimple_nop_p (stmt
))
1400 set_level (stmt
, orig_loop
, loop
);
1403 /* Forces statement defining invariants in REF (and *INDEX) to be moved out of
1404 the LOOP. The reference REF is used in the loop ORIG_LOOP. Callback for
1410 class loop
*orig_loop
;
1414 force_move_till (tree ref
, tree
*index
, void *data
)
1416 struct fmt_data
*fmt_data
= (struct fmt_data
*) data
;
1418 if (TREE_CODE (ref
) == ARRAY_REF
)
1420 tree step
= TREE_OPERAND (ref
, 3);
1421 tree lbound
= TREE_OPERAND (ref
, 2);
1423 force_move_till_op (step
, fmt_data
->orig_loop
, fmt_data
->loop
);
1424 force_move_till_op (lbound
, fmt_data
->orig_loop
, fmt_data
->loop
);
1427 force_move_till_op (*index
, fmt_data
->orig_loop
, fmt_data
->loop
);
1432 /* A function to free the mem_ref object OBJ. */
1435 memref_free (class im_mem_ref
*mem
)
1437 mem
->accesses_in_loop
.release ();
1440 /* Allocates and returns a memory reference description for MEM whose hash
1441 value is HASH and id is ID. */
1444 mem_ref_alloc (ao_ref
*mem
, unsigned hash
, unsigned id
)
1446 im_mem_ref
*ref
= XOBNEW (&mem_ref_obstack
, class im_mem_ref
);
1450 ao_ref_init (&ref
->mem
, error_mark_node
);
1452 ref
->ref_canonical
= false;
1453 ref
->ref_decomposed
= false;
1457 bitmap_initialize (&ref
->dep_loop
, &lim_bitmap_obstack
);
1458 ref
->accesses_in_loop
.create (1);
1463 /* Records memory reference location *LOC in LOOP to the memory reference
1464 description REF. The reference occurs in statement STMT. */
1467 record_mem_ref_loc (im_mem_ref
*ref
, gimple
*stmt
, tree
*loc
)
1472 ref
->accesses_in_loop
.safe_push (aref
);
1475 /* Set the LOOP bit in REF stored bitmap and allocate that if
1476 necessary. Return whether a bit was changed. */
1479 set_ref_stored_in_loop (im_mem_ref
*ref
, class loop
*loop
)
1482 ref
->stored
= BITMAP_ALLOC (&lim_bitmap_obstack
);
1483 return bitmap_set_bit (ref
->stored
, loop
->num
);
1486 /* Marks reference REF as stored in LOOP. */
1489 mark_ref_stored (im_mem_ref
*ref
, class loop
*loop
)
1491 while (loop
!= current_loops
->tree_root
1492 && set_ref_stored_in_loop (ref
, loop
))
1493 loop
= loop_outer (loop
);
1496 /* Set the LOOP bit in REF loaded bitmap and allocate that if
1497 necessary. Return whether a bit was changed. */
1500 set_ref_loaded_in_loop (im_mem_ref
*ref
, class loop
*loop
)
1503 ref
->loaded
= BITMAP_ALLOC (&lim_bitmap_obstack
);
1504 return bitmap_set_bit (ref
->loaded
, loop
->num
);
1507 /* Marks reference REF as loaded in LOOP. */
1510 mark_ref_loaded (im_mem_ref
*ref
, class loop
*loop
)
1512 while (loop
!= current_loops
->tree_root
1513 && set_ref_loaded_in_loop (ref
, loop
))
1514 loop
= loop_outer (loop
);
1517 /* Gathers memory references in statement STMT in LOOP, storing the
1518 information about them in the memory_accesses structure. Marks
1519 the vops accessed through unrecognized statements there as
1523 gather_mem_refs_stmt (class loop
*loop
, gimple
*stmt
)
1532 if (!gimple_vuse (stmt
))
1535 mem
= simple_mem_ref_in_stmt (stmt
, &is_stored
);
1536 if (!mem
&& is_gimple_assign (stmt
))
1538 /* For aggregate copies record distinct references but use them
1539 only for disambiguation purposes. */
1540 id
= memory_accesses
.refs_list
.length ();
1541 ref
= mem_ref_alloc (NULL
, 0, id
);
1542 memory_accesses
.refs_list
.safe_push (ref
);
1543 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1545 fprintf (dump_file
, "Unhandled memory reference %u: ", id
);
1546 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1548 record_mem_ref_loc (ref
, stmt
, mem
);
1549 is_stored
= gimple_vdef (stmt
);
1553 /* We use the shared mem_ref for all unanalyzable refs. */
1554 id
= UNANALYZABLE_MEM_ID
;
1555 ref
= memory_accesses
.refs_list
[id
];
1556 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1558 fprintf (dump_file
, "Unanalyzed memory reference %u: ", id
);
1559 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1561 is_stored
= gimple_vdef (stmt
);
1565 /* We are looking for equal refs that might differ in structure
1566 such as a.b vs. MEM[&a + 4]. So we key off the ao_ref but
1567 make sure we can canonicalize the ref in the hashtable if
1568 non-operand_equal_p refs are found. For the lookup we mark
1569 the case we want strict equality with aor.max_size == -1. */
1571 ao_ref_init (&aor
, *mem
);
1573 ao_ref_alias_set (&aor
);
1574 HOST_WIDE_INT offset
, size
, max_size
;
1575 poly_int64 saved_maxsize
= aor
.max_size
, mem_off
;
1577 bool ref_decomposed
;
1578 if (aor
.max_size_known_p ()
1579 && aor
.offset
.is_constant (&offset
)
1580 && aor
.size
.is_constant (&size
)
1581 && aor
.max_size
.is_constant (&max_size
)
1583 && (size
% BITS_PER_UNIT
) == 0
1584 /* We're canonicalizing to a MEM where TYPE_SIZE specifies the
1585 size. Make sure this is consistent with the extraction. */
1586 && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (*mem
)))
1587 && known_eq (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (*mem
))),
1589 && (mem_base
= get_addr_base_and_unit_offset (aor
.ref
, &mem_off
)))
1591 ref_decomposed
= true;
1592 tree base
= ao_ref_base (&aor
);
1594 HOST_WIDE_INT mcoffset
;
1595 if (TREE_CODE (base
) == MEM_REF
1596 && (mem_ref_offset (base
) * BITS_PER_UNIT
+ offset
).to_shwi (&moffset
)
1597 && moffset
.is_constant (&mcoffset
))
1599 hash
= iterative_hash_expr (TREE_OPERAND (base
, 0), 0);
1600 hash
= iterative_hash_host_wide_int (mcoffset
, hash
);
1604 hash
= iterative_hash_expr (base
, 0);
1605 hash
= iterative_hash_host_wide_int (offset
, hash
);
1607 hash
= iterative_hash_host_wide_int (size
, hash
);
1611 ref_decomposed
= false;
1612 hash
= iterative_hash_expr (aor
.ref
, 0);
1615 slot
= memory_accesses
.refs
->find_slot_with_hash (&aor
, hash
, INSERT
);
1616 aor
.max_size
= saved_maxsize
;
1619 if (!(*slot
)->ref_canonical
1620 && !operand_equal_p (*mem
, (*slot
)->mem
.ref
, 0))
1622 /* If we didn't yet canonicalize the hashtable ref (which
1623 we'll end up using for code insertion) and hit a second
1624 equal ref that is not structurally equivalent create
1625 a canonical ref which is a bare MEM_REF. */
1626 if (TREE_CODE (*mem
) == MEM_REF
1627 || TREE_CODE (*mem
) == TARGET_MEM_REF
)
1629 (*slot
)->mem
.ref
= *mem
;
1630 (*slot
)->mem
.base_alias_set
= ao_ref_base_alias_set (&aor
);
1634 tree ref_alias_type
= reference_alias_ptr_type (*mem
);
1635 unsigned int ref_align
= get_object_alignment (*mem
);
1636 tree ref_type
= TREE_TYPE (*mem
);
1637 tree tmp
= build1 (ADDR_EXPR
, ptr_type_node
,
1638 unshare_expr (mem_base
));
1639 if (TYPE_ALIGN (ref_type
) != ref_align
)
1640 ref_type
= build_aligned_type (ref_type
, ref_align
);
1642 = fold_build2 (MEM_REF
, ref_type
, tmp
,
1643 build_int_cst (ref_alias_type
, mem_off
));
1644 if ((*slot
)->mem
.volatile_p
)
1645 TREE_THIS_VOLATILE ((*slot
)->mem
.ref
) = 1;
1646 gcc_checking_assert (TREE_CODE ((*slot
)->mem
.ref
) == MEM_REF
1647 && is_gimple_mem_ref_addr
1648 (TREE_OPERAND ((*slot
)->mem
.ref
,
1650 (*slot
)->mem
.base_alias_set
= (*slot
)->mem
.ref_alias_set
;
1652 (*slot
)->ref_canonical
= true;
1659 id
= memory_accesses
.refs_list
.length ();
1660 ref
= mem_ref_alloc (&aor
, hash
, id
);
1661 ref
->ref_decomposed
= ref_decomposed
;
1662 memory_accesses
.refs_list
.safe_push (ref
);
1665 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1667 fprintf (dump_file
, "Memory reference %u: ", id
);
1668 print_generic_expr (dump_file
, ref
->mem
.ref
, TDF_SLIM
);
1669 fprintf (dump_file
, "\n");
1673 record_mem_ref_loc (ref
, stmt
, mem
);
1677 bitmap_set_bit (&memory_accesses
.refs_stored_in_loop
[loop
->num
], ref
->id
);
1678 mark_ref_stored (ref
, loop
);
1680 /* A not simple memory op is also a read when it is a write. */
1681 if (!is_stored
|| id
== UNANALYZABLE_MEM_ID
1682 || ref
->mem
.ref
== error_mark_node
)
1684 bitmap_set_bit (&memory_accesses
.refs_loaded_in_loop
[loop
->num
], ref
->id
);
1685 mark_ref_loaded (ref
, loop
);
1687 init_lim_data (stmt
)->ref
= ref
->id
;
1691 static unsigned *bb_loop_postorder
;
1693 /* qsort sort function to sort blocks after their loop fathers postorder. */
1696 sort_bbs_in_loop_postorder_cmp (const void *bb1_
, const void *bb2_
,
1697 void *bb_loop_postorder_
)
1699 unsigned *bb_loop_postorder
= (unsigned *)bb_loop_postorder_
;
1700 basic_block bb1
= *(const basic_block
*)bb1_
;
1701 basic_block bb2
= *(const basic_block
*)bb2_
;
1702 class loop
*loop1
= bb1
->loop_father
;
1703 class loop
*loop2
= bb2
->loop_father
;
1704 if (loop1
->num
== loop2
->num
)
1705 return bb1
->index
- bb2
->index
;
1706 return bb_loop_postorder
[loop1
->num
] < bb_loop_postorder
[loop2
->num
] ? -1 : 1;
1709 /* qsort sort function to sort ref locs after their loop fathers postorder. */
1712 sort_locs_in_loop_postorder_cmp (const void *loc1_
, const void *loc2_
,
1713 void *bb_loop_postorder_
)
1715 unsigned *bb_loop_postorder
= (unsigned *)bb_loop_postorder_
;
1716 const mem_ref_loc
*loc1
= (const mem_ref_loc
*)loc1_
;
1717 const mem_ref_loc
*loc2
= (const mem_ref_loc
*)loc2_
;
1718 class loop
*loop1
= gimple_bb (loc1
->stmt
)->loop_father
;
1719 class loop
*loop2
= gimple_bb (loc2
->stmt
)->loop_father
;
1720 if (loop1
->num
== loop2
->num
)
1722 return bb_loop_postorder
[loop1
->num
] < bb_loop_postorder
[loop2
->num
] ? -1 : 1;
1725 /* Gathers memory references in loops. */
1728 analyze_memory_references (bool store_motion
)
1730 gimple_stmt_iterator bsi
;
1731 basic_block bb
, *bbs
;
1735 /* Collect all basic-blocks in loops and sort them after their
1738 bbs
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (cfun
) - NUM_FIXED_BLOCKS
);
1739 FOR_EACH_BB_FN (bb
, cfun
)
1740 if (bb
->loop_father
!= current_loops
->tree_root
)
1743 gcc_sort_r (bbs
, n
, sizeof (basic_block
), sort_bbs_in_loop_postorder_cmp
,
1746 /* Visit blocks in loop postorder and assign mem-ref IDs in that order.
1747 That results in better locality for all the bitmaps. It also
1748 automatically sorts the location list of gathered memory references
1749 after their loop postorder number allowing to binary-search it. */
1750 for (i
= 0; i
< n
; ++i
)
1752 basic_block bb
= bbs
[i
];
1753 for (bsi
= gsi_start_bb (bb
); !gsi_end_p (bsi
); gsi_next (&bsi
))
1754 gather_mem_refs_stmt (bb
->loop_father
, gsi_stmt (bsi
));
1757 /* Verify the list of gathered memory references is sorted after their
1758 loop postorder number. */
1762 FOR_EACH_VEC_ELT (memory_accesses
.refs_list
, i
, ref
)
1763 for (unsigned j
= 1; j
< ref
->accesses_in_loop
.length (); ++j
)
1764 gcc_assert (sort_locs_in_loop_postorder_cmp
1765 (&ref
->accesses_in_loop
[j
-1], &ref
->accesses_in_loop
[j
],
1766 bb_loop_postorder
) <= 0);
1774 /* Propagate the information about accessed memory references up
1775 the loop hierarchy. */
1776 for (auto loop
: loops_list (cfun
, LI_FROM_INNERMOST
))
1778 /* Finalize the overall touched references (including subloops). */
1779 bitmap_ior_into (&memory_accesses
.all_refs_stored_in_loop
[loop
->num
],
1780 &memory_accesses
.refs_stored_in_loop
[loop
->num
]);
1782 /* Propagate the information about accessed memory references up
1783 the loop hierarchy. */
1784 outer
= loop_outer (loop
);
1785 if (outer
== current_loops
->tree_root
)
1788 bitmap_ior_into (&memory_accesses
.all_refs_stored_in_loop
[outer
->num
],
1789 &memory_accesses
.all_refs_stored_in_loop
[loop
->num
]);
1793 /* Returns true if MEM1 and MEM2 may alias. TTAE_CACHE is used as a cache in
1794 tree_to_aff_combination_expand. */
1797 mem_refs_may_alias_p (im_mem_ref
*mem1
, im_mem_ref
*mem2
,
1798 hash_map
<tree
, name_expansion
*> **ttae_cache
,
1801 gcc_checking_assert (mem1
->mem
.ref
!= error_mark_node
1802 && mem2
->mem
.ref
!= error_mark_node
);
1804 /* Perform BASE + OFFSET analysis -- if MEM1 and MEM2 are based on the same
1805 object and their offset differ in such a way that the locations cannot
1806 overlap, then they cannot alias. */
1807 poly_widest_int size1
, size2
;
1808 aff_tree off1
, off2
;
1810 /* Perform basic offset and type-based disambiguation. */
1811 if (!refs_may_alias_p_1 (&mem1
->mem
, &mem2
->mem
, tbaa_p
))
1814 /* The expansion of addresses may be a bit expensive, thus we only do
1815 the check at -O2 and higher optimization levels. */
1819 get_inner_reference_aff (mem1
->mem
.ref
, &off1
, &size1
);
1820 get_inner_reference_aff (mem2
->mem
.ref
, &off2
, &size2
);
1821 aff_combination_expand (&off1
, ttae_cache
);
1822 aff_combination_expand (&off2
, ttae_cache
);
1823 aff_combination_scale (&off1
, -1);
1824 aff_combination_add (&off2
, &off1
);
1826 if (aff_comb_cannot_overlap_p (&off2
, size1
, size2
))
1832 /* Compare function for bsearch searching for reference locations
1836 find_ref_loc_in_loop_cmp (const void *loop_
, const void *loc_
,
1837 void *bb_loop_postorder_
)
1839 unsigned *bb_loop_postorder
= (unsigned *)bb_loop_postorder_
;
1840 class loop
*loop
= (class loop
*)const_cast<void *>(loop_
);
1841 mem_ref_loc
*loc
= (mem_ref_loc
*)const_cast<void *>(loc_
);
1842 class loop
*loc_loop
= gimple_bb (loc
->stmt
)->loop_father
;
1843 if (loop
->num
== loc_loop
->num
1844 || flow_loop_nested_p (loop
, loc_loop
))
1846 return (bb_loop_postorder
[loop
->num
] < bb_loop_postorder
[loc_loop
->num
]
1850 /* Iterates over all locations of REF in LOOP and its subloops calling
1851 fn.operator() with the location as argument. When that operator
1852 returns true the iteration is stopped and true is returned.
1853 Otherwise false is returned. */
1855 template <typename FN
>
1857 for_all_locs_in_loop (class loop
*loop
, im_mem_ref
*ref
, FN fn
)
1862 /* Search for the cluster of locs in the accesses_in_loop vector
1863 which is sorted after postorder index of the loop father. */
1864 loc
= ref
->accesses_in_loop
.bsearch (loop
, find_ref_loc_in_loop_cmp
,
1869 /* We have found one location inside loop or its sub-loops. Iterate
1870 both forward and backward to cover the whole cluster. */
1871 i
= loc
- ref
->accesses_in_loop
.address ();
1875 mem_ref_loc
*l
= &ref
->accesses_in_loop
[i
];
1876 if (!flow_bb_inside_loop_p (loop
, gimple_bb (l
->stmt
)))
1881 for (i
= loc
- ref
->accesses_in_loop
.address ();
1882 i
< ref
->accesses_in_loop
.length (); ++i
)
1884 mem_ref_loc
*l
= &ref
->accesses_in_loop
[i
];
1885 if (!flow_bb_inside_loop_p (loop
, gimple_bb (l
->stmt
)))
1894 /* Rewrites location LOC by TMP_VAR. */
1896 class rewrite_mem_ref_loc
1899 rewrite_mem_ref_loc (tree tmp_var_
) : tmp_var (tmp_var_
) {}
1900 bool operator () (mem_ref_loc
*loc
);
1905 rewrite_mem_ref_loc::operator () (mem_ref_loc
*loc
)
1907 *loc
->ref
= tmp_var
;
1908 update_stmt (loc
->stmt
);
1912 /* Rewrites all references to REF in LOOP by variable TMP_VAR. */
1915 rewrite_mem_refs (class loop
*loop
, im_mem_ref
*ref
, tree tmp_var
)
1917 for_all_locs_in_loop (loop
, ref
, rewrite_mem_ref_loc (tmp_var
));
1920 /* Stores the first reference location in LOCP. */
1922 class first_mem_ref_loc_1
1925 first_mem_ref_loc_1 (mem_ref_loc
**locp_
) : locp (locp_
) {}
1926 bool operator () (mem_ref_loc
*loc
);
1931 first_mem_ref_loc_1::operator () (mem_ref_loc
*loc
)
1937 /* Returns the first reference location to REF in LOOP. */
1939 static mem_ref_loc
*
1940 first_mem_ref_loc (class loop
*loop
, im_mem_ref
*ref
)
1942 mem_ref_loc
*locp
= NULL
;
1943 for_all_locs_in_loop (loop
, ref
, first_mem_ref_loc_1 (&locp
));
1947 /* Helper function for execute_sm. Emit code to store TMP_VAR into
1950 The store is only done if MEM has changed. We do this so no
1951 changes to MEM occur on code paths that did not originally store
1954 The common case for execute_sm will transform:
1974 This function will generate:
1991 In case MEM and TMP_VAR are NULL the function will return the then
1992 block so the caller can insert (X) and other related stmts.
1996 execute_sm_if_changed (edge ex
, tree mem
, tree tmp_var
, tree flag
,
1997 edge preheader
, hash_set
<basic_block
> *flag_bbs
,
1998 edge
&append_cond_position
, edge
&last_cond_fallthru
)
2000 basic_block new_bb
, then_bb
, old_dest
;
2001 bool loop_has_only_one_exit
;
2003 gimple_stmt_iterator gsi
;
2005 bool irr
= ex
->flags
& EDGE_IRREDUCIBLE_LOOP
;
2007 profile_count count_sum
= profile_count::zero ();
2008 int nbbs
= 0, ncount
= 0;
2009 profile_probability flag_probability
= profile_probability::uninitialized ();
2011 /* Flag is set in FLAG_BBS. Determine probability that flag will be true
2014 This code may look fancy, but it cannot update profile very realistically
2015 because we do not know the probability that flag will be true at given
2018 We look for two interesting extremes
2019 - when exit is dominated by block setting the flag, we know it will
2020 always be true. This is a common case.
2021 - when all blocks setting the flag have very low frequency we know
2022 it will likely be false.
2023 In all other cases we default to 2/3 for flag being true. */
2025 for (hash_set
<basic_block
>::iterator it
= flag_bbs
->begin ();
2026 it
!= flag_bbs
->end (); ++it
)
2028 if ((*it
)->count
.initialized_p ())
2029 count_sum
+= (*it
)->count
, ncount
++;
2030 if (dominated_by_p (CDI_DOMINATORS
, ex
->src
, *it
))
2031 flag_probability
= profile_probability::always ();
2035 profile_probability cap
= profile_probability::always ().apply_scale (2, 3);
2037 if (flag_probability
.initialized_p ())
2039 else if (ncount
== nbbs
2040 && preheader
->count () >= count_sum
&& preheader
->count ().nonzero_p ())
2042 flag_probability
= count_sum
.probability_in (preheader
->count ());
2043 if (flag_probability
> cap
)
2044 flag_probability
= cap
;
2047 if (!flag_probability
.initialized_p ())
2048 flag_probability
= cap
;
2050 /* ?? Insert store after previous store if applicable. See note
2052 if (append_cond_position
)
2053 ex
= append_cond_position
;
2055 loop_has_only_one_exit
= single_pred_p (ex
->dest
);
2057 if (loop_has_only_one_exit
)
2058 ex
= split_block_after_labels (ex
->dest
);
2061 for (gphi_iterator gpi
= gsi_start_phis (ex
->dest
);
2062 !gsi_end_p (gpi
); gsi_next (&gpi
))
2064 gphi
*phi
= gpi
.phi ();
2065 if (virtual_operand_p (gimple_phi_result (phi
)))
2068 /* When the destination has a non-virtual PHI node with multiple
2069 predecessors make sure we preserve the PHI structure by
2070 forcing a forwarder block so that hoisting of that PHI will
2077 old_dest
= ex
->dest
;
2078 new_bb
= split_edge (ex
);
2079 then_bb
= create_empty_bb (new_bb
);
2080 then_bb
->count
= new_bb
->count
.apply_probability (flag_probability
);
2082 then_bb
->flags
= BB_IRREDUCIBLE_LOOP
;
2083 add_bb_to_loop (then_bb
, new_bb
->loop_father
);
2085 gsi
= gsi_start_bb (new_bb
);
2086 stmt
= gimple_build_cond (NE_EXPR
, flag
, boolean_false_node
,
2087 NULL_TREE
, NULL_TREE
);
2088 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2090 /* Insert actual store. */
2093 gsi
= gsi_start_bb (then_bb
);
2094 stmt
= gimple_build_assign (unshare_expr (mem
), tmp_var
);
2095 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2098 edge e1
= single_succ_edge (new_bb
);
2099 edge e2
= make_edge (new_bb
, then_bb
,
2100 EDGE_TRUE_VALUE
| (irr
? EDGE_IRREDUCIBLE_LOOP
: 0));
2101 e2
->probability
= flag_probability
;
2103 e1
->flags
|= EDGE_FALSE_VALUE
| (irr
? EDGE_IRREDUCIBLE_LOOP
: 0);
2104 e1
->flags
&= ~EDGE_FALLTHRU
;
2106 e1
->probability
= flag_probability
.invert ();
2108 then_old_edge
= make_single_succ_edge (then_bb
, old_dest
,
2109 EDGE_FALLTHRU
| (irr
? EDGE_IRREDUCIBLE_LOOP
: 0));
2111 set_immediate_dominator (CDI_DOMINATORS
, then_bb
, new_bb
);
2113 if (append_cond_position
)
2115 basic_block prevbb
= last_cond_fallthru
->src
;
2116 redirect_edge_succ (last_cond_fallthru
, new_bb
);
2117 set_immediate_dominator (CDI_DOMINATORS
, new_bb
, prevbb
);
2118 set_immediate_dominator (CDI_DOMINATORS
, old_dest
,
2119 recompute_dominator (CDI_DOMINATORS
, old_dest
));
2122 /* ?? Because stores may alias, they must happen in the exact
2123 sequence they originally happened. Save the position right after
2124 the (_lsm) store we just created so we can continue appending after
2125 it and maintain the original order. */
2126 append_cond_position
= then_old_edge
;
2127 last_cond_fallthru
= find_edge (new_bb
, old_dest
);
2129 if (!loop_has_only_one_exit
)
2130 for (gphi_iterator gpi
= gsi_start_phis (old_dest
);
2131 !gsi_end_p (gpi
); gsi_next (&gpi
))
2133 gphi
*phi
= gpi
.phi ();
2136 for (i
= 0; i
< gimple_phi_num_args (phi
); i
++)
2137 if (gimple_phi_arg_edge (phi
, i
)->src
== new_bb
)
2139 tree arg
= gimple_phi_arg_def (phi
, i
);
2140 add_phi_arg (phi
, arg
, then_old_edge
, UNKNOWN_LOCATION
);
2148 /* When REF is set on the location, set flag indicating the store. */
2150 class sm_set_flag_if_changed
2153 sm_set_flag_if_changed (tree flag_
, hash_set
<basic_block
> *bbs_
)
2154 : flag (flag_
), bbs (bbs_
) {}
2155 bool operator () (mem_ref_loc
*loc
);
2157 hash_set
<basic_block
> *bbs
;
2161 sm_set_flag_if_changed::operator () (mem_ref_loc
*loc
)
2163 /* Only set the flag for writes. */
2164 if (is_gimple_assign (loc
->stmt
)
2165 && gimple_assign_lhs_ptr (loc
->stmt
) == loc
->ref
)
2167 gimple_stmt_iterator gsi
= gsi_for_stmt (loc
->stmt
);
2168 gimple
*stmt
= gimple_build_assign (flag
, boolean_true_node
);
2169 gsi_insert_after (&gsi
, stmt
, GSI_CONTINUE_LINKING
);
2170 bbs
->add (gimple_bb (stmt
));
2175 /* Helper function for execute_sm. On every location where REF is
2176 set, set an appropriate flag indicating the store. */
2179 execute_sm_if_changed_flag_set (class loop
*loop
, im_mem_ref
*ref
,
2180 hash_set
<basic_block
> *bbs
)
2183 char *str
= get_lsm_tmp_name (ref
->mem
.ref
, ~0, "_flag");
2184 flag
= create_tmp_reg (boolean_type_node
, str
);
2185 for_all_locs_in_loop (loop
, ref
, sm_set_flag_if_changed (flag
, bbs
));
2193 hash_set
<basic_block
> flag_bbs
;
2196 /* Executes store motion of memory reference REF from LOOP.
2197 Exits from the LOOP are stored in EXITS. The initialization of the
2198 temporary variable is put to the preheader of the loop, and assignments
2199 to the reference from the temporary variable are emitted to exits. */
2202 execute_sm (class loop
*loop
, im_mem_ref
*ref
,
2203 hash_map
<im_mem_ref
*, sm_aux
*> &aux_map
, bool maybe_mt
,
2204 bool use_other_flag_var
)
2207 struct fmt_data fmt_data
;
2208 struct lim_aux_data
*lim_data
;
2209 bool multi_threaded_model_p
= false;
2210 gimple_stmt_iterator gsi
;
2211 sm_aux
*aux
= new sm_aux
;
2213 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2215 fprintf (dump_file
, "Executing store motion of ");
2216 print_generic_expr (dump_file
, ref
->mem
.ref
);
2217 fprintf (dump_file
, " from loop %d\n", loop
->num
);
2220 aux
->tmp_var
= create_tmp_reg (TREE_TYPE (ref
->mem
.ref
),
2221 get_lsm_tmp_name (ref
->mem
.ref
, ~0));
2223 fmt_data
.loop
= loop
;
2224 fmt_data
.orig_loop
= loop
;
2225 for_each_index (&ref
->mem
.ref
, force_move_till
, &fmt_data
);
2227 bool always_stored
= ref_always_accessed_p (loop
, ref
, true);
2229 && (bb_in_transaction (loop_preheader_edge (loop
)->src
)
2230 || (! flag_store_data_races
&& ! always_stored
)))
2231 multi_threaded_model_p
= true;
2233 if (multi_threaded_model_p
&& !use_other_flag_var
)
2235 = execute_sm_if_changed_flag_set (loop
, ref
, &aux
->flag_bbs
);
2237 aux
->store_flag
= NULL_TREE
;
2239 /* Remember variable setup. */
2240 aux_map
.put (ref
, aux
);
2242 rewrite_mem_refs (loop
, ref
, aux
->tmp_var
);
2244 /* Emit the load code on a random exit edge or into the latch if
2245 the loop does not exit, so that we are sure it will be processed
2246 by move_computations after all dependencies. */
2247 gsi
= gsi_for_stmt (first_mem_ref_loc (loop
, ref
)->stmt
);
2249 /* Avoid doing a load if there was no load of the ref in the loop.
2250 Esp. when the ref is not always stored we cannot optimize it
2251 away later. But when it is not always stored we must use a conditional
2253 if ((!always_stored
&& !multi_threaded_model_p
)
2254 || (ref
->loaded
&& bitmap_bit_p (ref
->loaded
, loop
->num
)))
2255 load
= gimple_build_assign (aux
->tmp_var
, unshare_expr (ref
->mem
.ref
));
2258 /* If not emitting a load mark the uninitialized state on the
2259 loop entry as not to be warned for. */
2260 tree uninit
= create_tmp_reg (TREE_TYPE (aux
->tmp_var
));
2261 suppress_warning (uninit
, OPT_Wuninitialized
);
2262 load
= gimple_build_assign (aux
->tmp_var
, uninit
);
2264 lim_data
= init_lim_data (load
);
2265 lim_data
->max_loop
= loop
;
2266 lim_data
->tgt_loop
= loop
;
2267 gsi_insert_before (&gsi
, load
, GSI_SAME_STMT
);
2269 if (aux
->store_flag
)
2271 load
= gimple_build_assign (aux
->store_flag
, boolean_false_node
);
2272 lim_data
= init_lim_data (load
);
2273 lim_data
->max_loop
= loop
;
2274 lim_data
->tgt_loop
= loop
;
2275 gsi_insert_before (&gsi
, load
, GSI_SAME_STMT
);
2279 /* sm_ord is used for ordinary stores we can retain order with respect
2281 sm_unord is used for conditional executed stores which need to be
2282 able to execute in arbitrary order with respect to other stores
2283 sm_other is used for stores we do not try to apply store motion to. */
2284 enum sm_kind
{ sm_ord
, sm_unord
, sm_other
};
2288 seq_entry (unsigned f
, sm_kind k
, tree fr
= NULL
)
2289 : first (f
), second (k
), from (fr
) {}
2296 execute_sm_exit (class loop
*loop
, edge ex
, vec
<seq_entry
> &seq
,
2297 hash_map
<im_mem_ref
*, sm_aux
*> &aux_map
, sm_kind kind
,
2298 edge
&append_cond_position
, edge
&last_cond_fallthru
)
2300 /* Sink the stores to exit from the loop. */
2301 for (unsigned i
= seq
.length (); i
> 0; --i
)
2303 im_mem_ref
*ref
= memory_accesses
.refs_list
[seq
[i
-1].first
];
2304 if (seq
[i
-1].second
== sm_other
)
2306 gcc_assert (kind
== sm_ord
&& seq
[i
-1].from
!= NULL_TREE
);
2307 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2309 fprintf (dump_file
, "Re-issueing dependent store of ");
2310 print_generic_expr (dump_file
, ref
->mem
.ref
);
2311 fprintf (dump_file
, " from loop %d on exit %d -> %d\n",
2312 loop
->num
, ex
->src
->index
, ex
->dest
->index
);
2314 gassign
*store
= gimple_build_assign (unshare_expr (ref
->mem
.ref
),
2316 gsi_insert_on_edge (ex
, store
);
2320 sm_aux
*aux
= *aux_map
.get (ref
);
2321 if (!aux
->store_flag
|| kind
== sm_ord
)
2324 store
= gimple_build_assign (unshare_expr (ref
->mem
.ref
),
2326 gsi_insert_on_edge (ex
, store
);
2329 execute_sm_if_changed (ex
, ref
->mem
.ref
, aux
->tmp_var
,
2331 loop_preheader_edge (loop
), &aux
->flag_bbs
,
2332 append_cond_position
, last_cond_fallthru
);
2337 /* Push the SM candidate at index PTR in the sequence SEQ down until
2338 we hit the next SM candidate. Return true if that went OK and
2339 false if we could not disambiguate agains another unrelated ref.
2340 Update *AT to the index where the candidate now resides. */
2343 sm_seq_push_down (vec
<seq_entry
> &seq
, unsigned ptr
, unsigned *at
)
2346 for (; ptr
> 0; --ptr
)
2348 seq_entry
&new_cand
= seq
[ptr
];
2349 seq_entry
&against
= seq
[ptr
-1];
2350 if (against
.second
== sm_ord
2351 || (against
.second
== sm_other
&& against
.from
!= NULL_TREE
))
2352 /* Found the tail of the sequence. */
2354 /* We may not ignore self-dependences here. */
2355 if (new_cand
.first
== against
.first
2356 || !refs_independent_p (memory_accesses
.refs_list
[new_cand
.first
],
2357 memory_accesses
.refs_list
[against
.first
],
2359 /* ??? Prune new_cand from the list of refs to apply SM to. */
2361 std::swap (new_cand
, against
);
2367 /* Computes the sequence of stores from candidates in REFS_NOT_IN_SEQ to SEQ
2368 walking backwards from VDEF (or the end of BB if VDEF is NULL). */
2371 sm_seq_valid_bb (class loop
*loop
, basic_block bb
, tree vdef
,
2372 vec
<seq_entry
> &seq
, bitmap refs_not_in_seq
,
2373 bitmap refs_not_supported
, bool forked
,
2374 bitmap fully_visited
)
2377 for (gimple_stmt_iterator gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
);
2380 vdef
= gimple_vdef (gsi_stmt (gsi
));
2386 gphi
*vphi
= get_virtual_phi (bb
);
2388 vdef
= gimple_phi_result (vphi
);
2392 if (single_pred_p (bb
))
2393 /* This handles the perfect nest case. */
2394 return sm_seq_valid_bb (loop
, single_pred (bb
), vdef
,
2395 seq
, refs_not_in_seq
, refs_not_supported
,
2396 forked
, fully_visited
);
2401 gimple
*def
= SSA_NAME_DEF_STMT (vdef
);
2402 if (gimple_bb (def
) != bb
)
2404 /* If we forked by processing a PHI do not allow our walk to
2405 merge again until we handle that robustly. */
2408 /* Mark refs_not_in_seq as unsupported. */
2409 bitmap_ior_into (refs_not_supported
, refs_not_in_seq
);
2412 /* Otherwise it doesn't really matter if we end up in different
2414 bb
= gimple_bb (def
);
2416 if (gphi
*phi
= dyn_cast
<gphi
*> (def
))
2418 /* Handle CFG merges. Until we handle forks (gimple_bb (def) != bb)
2419 this is still linear.
2420 Eventually we want to cache intermediate results per BB
2421 (but we can't easily cache for different exits?). */
2422 /* Stop at PHIs with possible backedges. */
2423 if (bb
== bb
->loop_father
->header
2424 || bb
->flags
& BB_IRREDUCIBLE_LOOP
)
2426 /* Mark refs_not_in_seq as unsupported. */
2427 bitmap_ior_into (refs_not_supported
, refs_not_in_seq
);
2430 if (gimple_phi_num_args (phi
) == 1)
2431 return sm_seq_valid_bb (loop
, gimple_phi_arg_edge (phi
, 0)->src
,
2432 gimple_phi_arg_def (phi
, 0), seq
,
2433 refs_not_in_seq
, refs_not_supported
,
2434 false, fully_visited
);
2435 if (bitmap_bit_p (fully_visited
,
2436 SSA_NAME_VERSION (gimple_phi_result (phi
))))
2438 auto_vec
<seq_entry
> first_edge_seq
;
2439 auto_bitmap
tem_refs_not_in_seq (&lim_bitmap_obstack
);
2441 bitmap_copy (tem_refs_not_in_seq
, refs_not_in_seq
);
2442 eret
= sm_seq_valid_bb (loop
, gimple_phi_arg_edge (phi
, 0)->src
,
2443 gimple_phi_arg_def (phi
, 0),
2445 tem_refs_not_in_seq
, refs_not_supported
,
2446 true, fully_visited
);
2449 /* Simplify our lives by pruning the sequence of !sm_ord. */
2450 while (!first_edge_seq
.is_empty ()
2451 && first_edge_seq
.last ().second
!= sm_ord
)
2452 first_edge_seq
.pop ();
2453 for (unsigned int i
= 1; i
< gimple_phi_num_args (phi
); ++i
)
2455 tree vuse
= gimple_phi_arg_def (phi
, i
);
2456 edge e
= gimple_phi_arg_edge (phi
, i
);
2457 auto_vec
<seq_entry
> edge_seq
;
2458 bitmap_and_compl (tem_refs_not_in_seq
,
2459 refs_not_in_seq
, refs_not_supported
);
2460 /* If we've marked all refs we search for as unsupported
2461 we can stop processing and use the sequence as before
2463 if (bitmap_empty_p (tem_refs_not_in_seq
))
2465 eret
= sm_seq_valid_bb (loop
, e
->src
, vuse
, edge_seq
,
2466 tem_refs_not_in_seq
, refs_not_supported
,
2467 true, fully_visited
);
2470 /* Simplify our lives by pruning the sequence of !sm_ord. */
2471 while (!edge_seq
.is_empty ()
2472 && edge_seq
.last ().second
!= sm_ord
)
2474 unsigned min_len
= MIN(first_edge_seq
.length (),
2475 edge_seq
.length ());
2476 /* Incrementally merge seqs into first_edge_seq. */
2477 int first_uneq
= -1;
2478 auto_vec
<seq_entry
, 2> extra_refs
;
2479 for (unsigned int i
= 0; i
< min_len
; ++i
)
2481 /* ??? We can more intelligently merge when we face different
2482 order by additional sinking operations in one sequence.
2483 For now we simply mark them as to be processed by the
2484 not order-preserving SM code. */
2485 if (first_edge_seq
[i
].first
!= edge_seq
[i
].first
)
2487 if (first_edge_seq
[i
].second
== sm_ord
)
2488 bitmap_set_bit (refs_not_supported
,
2489 first_edge_seq
[i
].first
);
2490 if (edge_seq
[i
].second
== sm_ord
)
2491 bitmap_set_bit (refs_not_supported
, edge_seq
[i
].first
);
2492 first_edge_seq
[i
].second
= sm_other
;
2493 first_edge_seq
[i
].from
= NULL_TREE
;
2494 /* Record the dropped refs for later processing. */
2495 if (first_uneq
== -1)
2497 extra_refs
.safe_push (seq_entry (edge_seq
[i
].first
,
2498 sm_other
, NULL_TREE
));
2500 /* sm_other prevails. */
2501 else if (first_edge_seq
[i
].second
!= edge_seq
[i
].second
)
2503 /* Make sure the ref is marked as not supported. */
2504 bitmap_set_bit (refs_not_supported
,
2505 first_edge_seq
[i
].first
);
2506 first_edge_seq
[i
].second
= sm_other
;
2507 first_edge_seq
[i
].from
= NULL_TREE
;
2509 else if (first_edge_seq
[i
].second
== sm_other
2510 && first_edge_seq
[i
].from
!= NULL_TREE
2511 && (edge_seq
[i
].from
== NULL_TREE
2512 || !operand_equal_p (first_edge_seq
[i
].from
,
2513 edge_seq
[i
].from
, 0)))
2514 first_edge_seq
[i
].from
= NULL_TREE
;
2516 /* Any excess elements become sm_other since they are now
2517 coonditionally executed. */
2518 if (first_edge_seq
.length () > edge_seq
.length ())
2520 for (unsigned i
= edge_seq
.length ();
2521 i
< first_edge_seq
.length (); ++i
)
2523 if (first_edge_seq
[i
].second
== sm_ord
)
2524 bitmap_set_bit (refs_not_supported
,
2525 first_edge_seq
[i
].first
);
2526 first_edge_seq
[i
].second
= sm_other
;
2529 else if (edge_seq
.length () > first_edge_seq
.length ())
2531 if (first_uneq
== -1)
2532 first_uneq
= first_edge_seq
.length ();
2533 for (unsigned i
= first_edge_seq
.length ();
2534 i
< edge_seq
.length (); ++i
)
2536 if (edge_seq
[i
].second
== sm_ord
)
2537 bitmap_set_bit (refs_not_supported
, edge_seq
[i
].first
);
2538 extra_refs
.safe_push (seq_entry (edge_seq
[i
].first
,
2539 sm_other
, NULL_TREE
));
2542 /* Put unmerged refs at first_uneq to force dependence checking
2544 if (first_uneq
!= -1)
2546 /* Missing ordered_splice_at. */
2547 if ((unsigned)first_uneq
== first_edge_seq
.length ())
2548 first_edge_seq
.safe_splice (extra_refs
);
2551 unsigned fes_length
= first_edge_seq
.length ();
2552 first_edge_seq
.safe_grow (fes_length
2553 + extra_refs
.length ());
2554 memmove (&first_edge_seq
[first_uneq
+ extra_refs
.length ()],
2555 &first_edge_seq
[first_uneq
],
2556 (fes_length
- first_uneq
) * sizeof (seq_entry
));
2557 memcpy (&first_edge_seq
[first_uneq
],
2558 extra_refs
.address (),
2559 extra_refs
.length () * sizeof (seq_entry
));
2563 /* Use the sequence from the first edge and push SMs down. */
2564 for (unsigned i
= 0; i
< first_edge_seq
.length (); ++i
)
2566 unsigned id
= first_edge_seq
[i
].first
;
2567 seq
.safe_push (first_edge_seq
[i
]);
2569 if ((first_edge_seq
[i
].second
== sm_ord
2570 || (first_edge_seq
[i
].second
== sm_other
2571 && first_edge_seq
[i
].from
!= NULL_TREE
))
2572 && !sm_seq_push_down (seq
, seq
.length () - 1, &new_idx
))
2574 if (first_edge_seq
[i
].second
== sm_ord
)
2575 bitmap_set_bit (refs_not_supported
, id
);
2576 /* Mark it sm_other. */
2577 seq
[new_idx
].second
= sm_other
;
2578 seq
[new_idx
].from
= NULL_TREE
;
2581 bitmap_set_bit (fully_visited
,
2582 SSA_NAME_VERSION (gimple_phi_result (phi
)));
2585 lim_aux_data
*data
= get_lim_data (def
);
2587 if (data
->ref
== UNANALYZABLE_MEM_ID
)
2589 /* Stop at memory references which we can't move. */
2590 else if (memory_accesses
.refs_list
[data
->ref
]->mem
.ref
== error_mark_node
)
2592 /* Mark refs_not_in_seq as unsupported. */
2593 bitmap_ior_into (refs_not_supported
, refs_not_in_seq
);
2596 /* One of the stores we want to apply SM to and we've not yet seen. */
2597 else if (bitmap_clear_bit (refs_not_in_seq
, data
->ref
))
2599 seq
.safe_push (seq_entry (data
->ref
, sm_ord
));
2601 /* 1) push it down the queue until a SMed
2602 and not ignored ref is reached, skipping all not SMed refs
2603 and ignored refs via non-TBAA disambiguation. */
2605 if (!sm_seq_push_down (seq
, seq
.length () - 1, &new_idx
)
2606 /* If that fails but we did not fork yet continue, we'll see
2607 to re-materialize all of the stores in the sequence then.
2608 Further stores will only be pushed up to this one. */
2611 bitmap_set_bit (refs_not_supported
, data
->ref
);
2612 /* Mark it sm_other. */
2613 seq
[new_idx
].second
= sm_other
;
2616 /* 2) check whether we've seen all refs we want to SM and if so
2617 declare success for the active exit */
2618 if (bitmap_empty_p (refs_not_in_seq
))
2622 /* Another store not part of the final sequence. Simply push it. */
2623 seq
.safe_push (seq_entry (data
->ref
, sm_other
,
2624 gimple_assign_rhs1 (def
)));
2626 vdef
= gimple_vuse (def
);
2631 /* Hoists memory references MEM_REFS out of LOOP. EXITS is the list of exit
2632 edges of the LOOP. */
2635 hoist_memory_references (class loop
*loop
, bitmap mem_refs
,
2636 const vec
<edge
> &exits
)
2642 /* There's a special case we can use ordered re-materialization for
2643 conditionally excuted stores which is when all stores in the loop
2644 happen in the same basic-block. In that case we know we'll reach
2645 all stores and thus can simply process that BB and emit a single
2646 conditional block of ordered materializations. See PR102436. */
2647 basic_block single_store_bb
= NULL
;
2648 EXECUTE_IF_SET_IN_BITMAP (&memory_accesses
.all_refs_stored_in_loop
[loop
->num
],
2652 ref
= memory_accesses
.refs_list
[i
];
2653 for (auto loc
: ref
->accesses_in_loop
)
2654 if (!gimple_vdef (loc
.stmt
))
2656 else if (!single_store_bb
)
2658 single_store_bb
= gimple_bb (loc
.stmt
);
2659 bool conditional
= false;
2660 for (edge e
: exits
)
2661 if (!dominated_by_p (CDI_DOMINATORS
, e
->src
, single_store_bb
))
2663 /* Conditional as seen from e. */
2673 else if (single_store_bb
!= gimple_bb (loc
.stmt
))
2680 single_store_bb
= NULL
;
2684 if (single_store_bb
)
2686 /* Analyze the single block with stores. */
2687 auto_bitmap fully_visited
;
2688 auto_bitmap refs_not_supported
;
2689 auto_bitmap refs_not_in_seq
;
2690 auto_vec
<seq_entry
> seq
;
2691 bitmap_copy (refs_not_in_seq
, mem_refs
);
2692 int res
= sm_seq_valid_bb (loop
, single_store_bb
, NULL_TREE
,
2693 seq
, refs_not_in_seq
, refs_not_supported
,
2694 false, fully_visited
);
2697 /* Unhandled refs can still fail this. */
2698 bitmap_clear (mem_refs
);
2702 /* We cannot handle sm_other since we neither remember the
2703 stored location nor the value at the point we execute them. */
2704 for (unsigned i
= 0; i
< seq
.length (); ++i
)
2707 if (seq
[i
].second
== sm_other
2708 && seq
[i
].from
!= NULL_TREE
)
2709 seq
[i
].from
= NULL_TREE
;
2710 else if ((seq
[i
].second
== sm_ord
2711 || (seq
[i
].second
== sm_other
2712 && seq
[i
].from
!= NULL_TREE
))
2713 && !sm_seq_push_down (seq
, i
, &new_i
))
2715 bitmap_set_bit (refs_not_supported
, seq
[new_i
].first
);
2716 seq
[new_i
].second
= sm_other
;
2717 seq
[new_i
].from
= NULL_TREE
;
2720 bitmap_and_compl_into (mem_refs
, refs_not_supported
);
2721 if (bitmap_empty_p (mem_refs
))
2725 while (seq
.last ().second
== sm_other
2726 && seq
.last ().from
== NULL_TREE
)
2729 hash_map
<im_mem_ref
*, sm_aux
*> aux_map
;
2731 /* Execute SM but delay the store materialization for ordered
2732 sequences on exit. */
2733 bool first_p
= true;
2734 EXECUTE_IF_SET_IN_BITMAP (mem_refs
, 0, i
, bi
)
2736 ref
= memory_accesses
.refs_list
[i
];
2737 execute_sm (loop
, ref
, aux_map
, true, !first_p
);
2741 /* Get at the single flag variable we eventually produced. */
2743 = memory_accesses
.refs_list
[bitmap_first_set_bit (mem_refs
)];
2744 sm_aux
*aux
= *aux_map
.get (ref
);
2746 /* Materialize ordered store sequences on exits. */
2748 FOR_EACH_VEC_ELT (exits
, i
, e
)
2750 edge append_cond_position
= NULL
;
2751 edge last_cond_fallthru
= NULL
;
2753 /* Construct the single flag variable control flow and insert
2754 the ordered seq of stores in the then block. With
2755 -fstore-data-races we can do the stores unconditionally. */
2756 if (aux
->store_flag
)
2759 (execute_sm_if_changed (e
, NULL_TREE
, NULL_TREE
,
2761 loop_preheader_edge (loop
),
2762 &aux
->flag_bbs
, append_cond_position
,
2763 last_cond_fallthru
));
2764 execute_sm_exit (loop
, insert_e
, seq
, aux_map
, sm_ord
,
2765 append_cond_position
, last_cond_fallthru
);
2766 gsi_commit_one_edge_insert (insert_e
, NULL
);
2769 for (hash_map
<im_mem_ref
*, sm_aux
*>::iterator iter
= aux_map
.begin ();
2770 iter
!= aux_map
.end (); ++iter
)
2771 delete (*iter
).second
;
2776 /* To address PR57359 before actually applying store-motion check
2777 the candidates found for validity with regards to reordering
2778 relative to other stores which we until here disambiguated using
2779 TBAA which isn't valid.
2780 What matters is the order of the last stores to the mem_refs
2781 with respect to the other stores of the loop at the point of the
2784 /* For each exit compute the store order, pruning from mem_refs
2786 /* The complexity of this is at least
2787 O(number of exits * number of SM refs) but more approaching
2788 O(number of exits * number of SM refs * number of stores). */
2789 /* ??? Somehow do this in a single sweep over the loop body. */
2790 auto_vec
<std::pair
<edge
, vec
<seq_entry
> > > sms
;
2791 auto_bitmap
refs_not_supported (&lim_bitmap_obstack
);
2793 FOR_EACH_VEC_ELT (exits
, i
, e
)
2797 auto_bitmap
refs_not_in_seq (&lim_bitmap_obstack
);
2798 bitmap_and_compl (refs_not_in_seq
, mem_refs
, refs_not_supported
);
2799 if (bitmap_empty_p (refs_not_in_seq
))
2804 auto_bitmap fully_visited
;
2805 int res
= sm_seq_valid_bb (loop
, e
->src
, NULL_TREE
,
2806 seq
, refs_not_in_seq
,
2807 refs_not_supported
, false,
2811 bitmap_copy (refs_not_supported
, mem_refs
);
2815 sms
.safe_push (std::make_pair (e
, seq
));
2818 /* Prune pruned mem_refs from earlier processed exits. */
2819 bool changed
= !bitmap_empty_p (refs_not_supported
);
2823 std::pair
<edge
, vec
<seq_entry
> > *seq
;
2824 FOR_EACH_VEC_ELT (sms
, i
, seq
)
2826 bool need_to_push
= false;
2827 for (unsigned i
= 0; i
< seq
->second
.length (); ++i
)
2829 sm_kind kind
= seq
->second
[i
].second
;
2830 if (kind
== sm_other
&& seq
->second
[i
].from
== NULL_TREE
)
2832 unsigned id
= seq
->second
[i
].first
;
2835 && bitmap_bit_p (refs_not_supported
, id
))
2837 seq
->second
[i
].second
= sm_other
;
2838 gcc_assert (seq
->second
[i
].from
== NULL_TREE
);
2839 need_to_push
= true;
2841 else if (need_to_push
2842 && !sm_seq_push_down (seq
->second
, i
, &new_idx
))
2844 /* We need to push down both sm_ord and sm_other
2845 but for the latter we need to disqualify all
2849 if (bitmap_set_bit (refs_not_supported
, id
))
2851 seq
->second
[new_idx
].second
= sm_other
;
2855 for (unsigned j
= seq
->second
.length () - 1;
2857 if (seq
->second
[j
].second
== sm_ord
2858 && bitmap_set_bit (refs_not_supported
,
2859 seq
->second
[j
].first
))
2861 seq
->second
.truncate (new_idx
);
2868 std::pair
<edge
, vec
<seq_entry
> > *seq
;
2869 FOR_EACH_VEC_ELT (sms
, i
, seq
)
2871 /* Prune sm_other from the end. */
2872 while (!seq
->second
.is_empty ()
2873 && seq
->second
.last ().second
== sm_other
)
2875 /* Prune duplicates from the start. */
2876 auto_bitmap
seen (&lim_bitmap_obstack
);
2878 for (j
= k
= 0; j
< seq
->second
.length (); ++j
)
2879 if (bitmap_set_bit (seen
, seq
->second
[j
].first
))
2882 seq
->second
[k
] = seq
->second
[j
];
2885 seq
->second
.truncate (k
);
2888 FOR_EACH_VEC_ELT (seq
->second
, j
, e
)
2889 gcc_assert (e
->second
== sm_ord
2890 || (e
->second
== sm_other
&& e
->from
!= NULL_TREE
));
2893 /* Verify dependence for refs we cannot handle with the order preserving
2894 code (refs_not_supported) or prune them from mem_refs. */
2895 auto_vec
<seq_entry
> unord_refs
;
2896 EXECUTE_IF_SET_IN_BITMAP (refs_not_supported
, 0, i
, bi
)
2898 ref
= memory_accesses
.refs_list
[i
];
2899 if (!ref_indep_loop_p (loop
, ref
, sm_waw
))
2900 bitmap_clear_bit (mem_refs
, i
);
2901 /* We've now verified store order for ref with respect to all other
2902 stores in the loop does not matter. */
2904 unord_refs
.safe_push (seq_entry (i
, sm_unord
));
2907 hash_map
<im_mem_ref
*, sm_aux
*> aux_map
;
2909 /* Execute SM but delay the store materialization for ordered
2910 sequences on exit. */
2911 EXECUTE_IF_SET_IN_BITMAP (mem_refs
, 0, i
, bi
)
2913 ref
= memory_accesses
.refs_list
[i
];
2914 execute_sm (loop
, ref
, aux_map
, bitmap_bit_p (refs_not_supported
, i
),
2918 /* Materialize ordered store sequences on exits. */
2919 FOR_EACH_VEC_ELT (exits
, i
, e
)
2921 edge append_cond_position
= NULL
;
2922 edge last_cond_fallthru
= NULL
;
2923 if (i
< sms
.length ())
2925 gcc_assert (sms
[i
].first
== e
);
2926 execute_sm_exit (loop
, e
, sms
[i
].second
, aux_map
, sm_ord
,
2927 append_cond_position
, last_cond_fallthru
);
2928 sms
[i
].second
.release ();
2930 if (!unord_refs
.is_empty ())
2931 execute_sm_exit (loop
, e
, unord_refs
, aux_map
, sm_unord
,
2932 append_cond_position
, last_cond_fallthru
);
2933 /* Commit edge inserts here to preserve the order of stores
2934 when an exit exits multiple loops. */
2935 gsi_commit_one_edge_insert (e
, NULL
);
2938 for (hash_map
<im_mem_ref
*, sm_aux
*>::iterator iter
= aux_map
.begin ();
2939 iter
!= aux_map
.end (); ++iter
)
2940 delete (*iter
).second
;
2943 class ref_always_accessed
2946 ref_always_accessed (class loop
*loop_
, bool stored_p_
)
2947 : loop (loop_
), stored_p (stored_p_
) {}
2948 bool operator () (mem_ref_loc
*loc
);
2954 ref_always_accessed::operator () (mem_ref_loc
*loc
)
2956 class loop
*must_exec
;
2958 struct lim_aux_data
*lim_data
= get_lim_data (loc
->stmt
);
2962 /* If we require an always executed store make sure the statement
2966 tree lhs
= gimple_get_lhs (loc
->stmt
);
2968 || !(DECL_P (lhs
) || REFERENCE_CLASS_P (lhs
)))
2972 must_exec
= lim_data
->always_executed_in
;
2976 if (must_exec
== loop
2977 || flow_loop_nested_p (must_exec
, loop
))
2983 /* Returns true if REF is always accessed in LOOP. If STORED_P is true
2984 make sure REF is always stored to in LOOP. */
2987 ref_always_accessed_p (class loop
*loop
, im_mem_ref
*ref
, bool stored_p
)
2989 return for_all_locs_in_loop (loop
, ref
,
2990 ref_always_accessed (loop
, stored_p
));
2993 /* Returns true if REF1 and REF2 are independent. */
2996 refs_independent_p (im_mem_ref
*ref1
, im_mem_ref
*ref2
, bool tbaa_p
)
3001 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3002 fprintf (dump_file
, "Querying dependency of refs %u and %u: ",
3003 ref1
->id
, ref2
->id
);
3005 if (mem_refs_may_alias_p (ref1
, ref2
, &memory_accesses
.ttae_cache
, tbaa_p
))
3007 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3008 fprintf (dump_file
, "dependent.\n");
3013 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3014 fprintf (dump_file
, "independent.\n");
3019 /* Returns true if REF is independent on all other accessess in LOOP.
3020 KIND specifies the kind of dependence to consider.
3021 lim_raw assumes REF is not stored in LOOP and disambiguates RAW
3022 dependences so if true REF can be hoisted out of LOOP
3023 sm_war disambiguates a store REF against all other loads to see
3024 whether the store can be sunk across loads out of LOOP
3025 sm_waw disambiguates a store REF against all other stores to see
3026 whether the store can be sunk across stores out of LOOP. */
3029 ref_indep_loop_p (class loop
*loop
, im_mem_ref
*ref
, dep_kind kind
)
3031 bool indep_p
= true;
3032 bitmap refs_to_check
;
3035 refs_to_check
= &memory_accesses
.refs_loaded_in_loop
[loop
->num
];
3037 refs_to_check
= &memory_accesses
.refs_stored_in_loop
[loop
->num
];
3039 if (bitmap_bit_p (refs_to_check
, UNANALYZABLE_MEM_ID
)
3040 || ref
->mem
.ref
== error_mark_node
)
3044 /* tri-state, { unknown, independent, dependent } */
3045 dep_state state
= query_loop_dependence (loop
, ref
, kind
);
3046 if (state
!= dep_unknown
)
3047 return state
== dep_independent
? true : false;
3049 class loop
*inner
= loop
->inner
;
3052 if (!ref_indep_loop_p (inner
, ref
, kind
))
3057 inner
= inner
->next
;
3064 EXECUTE_IF_SET_IN_BITMAP (refs_to_check
, 0, i
, bi
)
3066 im_mem_ref
*aref
= memory_accesses
.refs_list
[i
];
3067 if (aref
->mem
.ref
== error_mark_node
)
3069 gimple
*stmt
= aref
->accesses_in_loop
[0].stmt
;
3071 && ref_maybe_used_by_stmt_p (stmt
, &ref
->mem
,
3073 || stmt_may_clobber_ref_p_1 (stmt
, &ref
->mem
,
3080 else if (!refs_independent_p (ref
, aref
, kind
!= sm_waw
))
3089 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3090 fprintf (dump_file
, "Querying %s dependencies of ref %u in loop %d: %s\n",
3091 kind
== lim_raw
? "RAW" : (kind
== sm_war
? "SM WAR" : "SM WAW"),
3092 ref
->id
, loop
->num
, indep_p
? "independent" : "dependent");
3094 /* Record the computed result in the cache. */
3095 record_loop_dependence (loop
, ref
, kind
,
3096 indep_p
? dep_independent
: dep_dependent
);
3101 class ref_in_loop_hot_body
3104 ref_in_loop_hot_body (class loop
*loop_
) : l (loop_
) {}
3105 bool operator () (mem_ref_loc
*loc
);
3109 /* Check the coldest loop between loop L and innermost loop. If there is one
3110 cold loop between L and INNER_LOOP, store motion can be performed, otherwise
3111 no cold loop means no store motion. get_coldest_out_loop also handles cases
3112 when l is inner_loop. */
3114 ref_in_loop_hot_body::operator () (mem_ref_loc
*loc
)
3116 basic_block curr_bb
= gimple_bb (loc
->stmt
);
3117 class loop
*inner_loop
= curr_bb
->loop_father
;
3118 return get_coldest_out_loop (l
, inner_loop
, curr_bb
);
3122 /* Returns true if we can perform store motion of REF from LOOP. */
3125 can_sm_ref_p (class loop
*loop
, im_mem_ref
*ref
)
3129 /* Can't hoist unanalyzable refs. */
3130 if (!MEM_ANALYZABLE (ref
))
3133 /* Can't hoist/sink aggregate copies. */
3134 if (ref
->mem
.ref
== error_mark_node
)
3137 /* It should be movable. */
3138 if (!is_gimple_reg_type (TREE_TYPE (ref
->mem
.ref
))
3139 || TREE_THIS_VOLATILE (ref
->mem
.ref
)
3140 || !for_each_index (&ref
->mem
.ref
, may_move_till
, loop
))
3143 /* If it can throw fail, we do not properly update EH info. */
3144 if (tree_could_throw_p (ref
->mem
.ref
))
3147 /* If it can trap, it must be always executed in LOOP.
3148 Readonly memory locations may trap when storing to them, but
3149 tree_could_trap_p is a predicate for rvalues, so check that
3151 base
= get_base_address (ref
->mem
.ref
);
3152 if ((tree_could_trap_p (ref
->mem
.ref
)
3153 || (DECL_P (base
) && TREE_READONLY (base
)))
3154 /* ??? We can at least use false here, allowing loads? We
3155 are forcing conditional stores if the ref is not always
3156 stored to later anyway. So this would only guard
3157 the load we need to emit. Thus when the ref is not
3158 loaded we can elide this completely? */
3159 && !ref_always_accessed_p (loop
, ref
, true))
3162 /* Verify all loads of ref can be hoisted. */
3164 && bitmap_bit_p (ref
->loaded
, loop
->num
)
3165 && !ref_indep_loop_p (loop
, ref
, lim_raw
))
3168 /* Verify the candidate can be disambiguated against all loads,
3169 that is, we can elide all in-loop stores. Disambiguation
3170 against stores is done later when we cannot guarantee preserving
3171 the order of stores. */
3172 if (!ref_indep_loop_p (loop
, ref
, sm_war
))
3175 /* Verify whether the candidate is hot for LOOP. Only do store motion if the
3176 candidate's profile count is hot. Statement in cold BB shouldn't be moved
3177 out of it's loop_father. */
3178 if (!for_all_locs_in_loop (loop
, ref
, ref_in_loop_hot_body (loop
)))
3184 /* Marks the references in LOOP for that store motion should be performed
3185 in REFS_TO_SM. SM_EXECUTED is the set of references for that store
3186 motion was performed in one of the outer loops. */
3189 find_refs_for_sm (class loop
*loop
, bitmap sm_executed
, bitmap refs_to_sm
)
3191 bitmap refs
= &memory_accesses
.all_refs_stored_in_loop
[loop
->num
];
3196 EXECUTE_IF_AND_COMPL_IN_BITMAP (refs
, sm_executed
, 0, i
, bi
)
3198 ref
= memory_accesses
.refs_list
[i
];
3199 if (can_sm_ref_p (loop
, ref
) && dbg_cnt (lim
))
3200 bitmap_set_bit (refs_to_sm
, i
);
3204 /* Checks whether LOOP (with exits stored in EXITS array) is suitable
3205 for a store motion optimization (i.e. whether we can insert statement
3209 loop_suitable_for_sm (class loop
*loop ATTRIBUTE_UNUSED
,
3210 const vec
<edge
> &exits
)
3215 FOR_EACH_VEC_ELT (exits
, i
, ex
)
3216 if (ex
->flags
& (EDGE_ABNORMAL
| EDGE_EH
))
3222 /* Try to perform store motion for all memory references modified inside
3223 LOOP. SM_EXECUTED is the bitmap of the memory references for that
3224 store motion was executed in one of the outer loops. */
3227 store_motion_loop (class loop
*loop
, bitmap sm_executed
)
3229 auto_vec
<edge
> exits
= get_loop_exit_edges (loop
);
3230 class loop
*subloop
;
3231 bitmap sm_in_loop
= BITMAP_ALLOC (&lim_bitmap_obstack
);
3233 if (loop_suitable_for_sm (loop
, exits
))
3235 find_refs_for_sm (loop
, sm_executed
, sm_in_loop
);
3236 if (!bitmap_empty_p (sm_in_loop
))
3237 hoist_memory_references (loop
, sm_in_loop
, exits
);
3240 bitmap_ior_into (sm_executed
, sm_in_loop
);
3241 for (subloop
= loop
->inner
; subloop
!= NULL
; subloop
= subloop
->next
)
3242 store_motion_loop (subloop
, sm_executed
);
3243 bitmap_and_compl_into (sm_executed
, sm_in_loop
);
3244 BITMAP_FREE (sm_in_loop
);
3247 /* Try to perform store motion for all memory references modified inside
3251 do_store_motion (void)
3254 bitmap sm_executed
= BITMAP_ALLOC (&lim_bitmap_obstack
);
3256 for (loop
= current_loops
->tree_root
->inner
; loop
!= NULL
; loop
= loop
->next
)
3257 store_motion_loop (loop
, sm_executed
);
3259 BITMAP_FREE (sm_executed
);
3262 /* Fills ALWAYS_EXECUTED_IN information for basic blocks of LOOP, i.e.
3263 for each such basic block bb records the outermost loop for that execution
3264 of its header implies execution of bb. CONTAINS_CALL is the bitmap of
3265 blocks that contain a nonpure call. */
3268 fill_always_executed_in_1 (class loop
*loop
, sbitmap contains_call
)
3270 basic_block bb
= NULL
, last
= NULL
;
3272 class loop
*inn_loop
= loop
;
3274 if (ALWAYS_EXECUTED_IN (loop
->header
) == NULL
)
3276 auto_vec
<basic_block
, 64> worklist
;
3277 worklist
.reserve_exact (loop
->num_nodes
);
3278 worklist
.quick_push (loop
->header
);
3282 bb
= worklist
.pop ();
3284 if (!flow_bb_inside_loop_p (inn_loop
, bb
))
3286 /* When we are leaving a possibly infinite inner loop
3287 we have to stop processing. */
3288 if (!finite_loop_p (inn_loop
))
3290 /* If the loop was finite we can continue with processing
3291 the loop we exited to. */
3292 inn_loop
= bb
->loop_father
;
3295 if (dominated_by_p (CDI_DOMINATORS
, loop
->latch
, bb
))
3298 if (bitmap_bit_p (contains_call
, bb
->index
))
3301 /* If LOOP exits from this BB stop processing. */
3302 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
3303 if (!flow_bb_inside_loop_p (loop
, e
->dest
))
3308 /* A loop might be infinite (TODO use simple loop analysis
3309 to disprove this if possible). */
3310 if (bb
->flags
& BB_IRREDUCIBLE_LOOP
)
3313 if (bb
->loop_father
->header
== bb
)
3314 /* Record that we enter into a subloop since it might not
3316 /* ??? Entering into a not always executed subloop makes
3317 fill_always_executed_in quadratic in loop depth since
3318 we walk those loops N times. This is not a problem
3319 in practice though, see PR102253 for a worst-case testcase. */
3320 inn_loop
= bb
->loop_father
;
3322 /* Walk the body of LOOP sorted by dominance relation. Additionally,
3323 if a basic block S dominates the latch, then only blocks dominated
3325 This is get_loop_body_in_dom_order using a worklist algorithm and
3326 stopping once we are no longer interested in visiting further
3328 unsigned old_len
= worklist
.length ();
3329 unsigned postpone
= 0;
3330 for (basic_block son
= first_dom_son (CDI_DOMINATORS
, bb
);
3332 son
= next_dom_son (CDI_DOMINATORS
, son
))
3334 if (!flow_bb_inside_loop_p (loop
, son
))
3336 if (dominated_by_p (CDI_DOMINATORS
, loop
->latch
, son
))
3337 postpone
= worklist
.length ();
3338 worklist
.quick_push (son
);
3341 /* Postponing the block that dominates the latch means
3342 processing it last and thus putting it earliest in the
3344 std::swap (worklist
[old_len
], worklist
[postpone
]);
3346 while (!worklist
.is_empty ());
3350 if (dump_enabled_p ())
3351 dump_printf (MSG_NOTE
, "BB %d is always executed in loop %d\n",
3352 last
->index
, loop
->num
);
3353 SET_ALWAYS_EXECUTED_IN (last
, loop
);
3354 if (last
== loop
->header
)
3356 last
= get_immediate_dominator (CDI_DOMINATORS
, last
);
3360 for (loop
= loop
->inner
; loop
; loop
= loop
->next
)
3361 fill_always_executed_in_1 (loop
, contains_call
);
3364 /* Fills ALWAYS_EXECUTED_IN information for basic blocks, i.e.
3365 for each such basic block bb records the outermost loop for that execution
3366 of its header implies execution of bb. */
3369 fill_always_executed_in (void)
3374 auto_sbitmap
contains_call (last_basic_block_for_fn (cfun
));
3375 bitmap_clear (contains_call
);
3376 FOR_EACH_BB_FN (bb
, cfun
)
3378 gimple_stmt_iterator gsi
;
3379 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3381 if (nonpure_call_p (gsi_stmt (gsi
)))
3385 if (!gsi_end_p (gsi
))
3386 bitmap_set_bit (contains_call
, bb
->index
);
3389 for (loop
= current_loops
->tree_root
->inner
; loop
; loop
= loop
->next
)
3390 fill_always_executed_in_1 (loop
, contains_call
);
3393 /* Find the coldest loop preheader for LOOP, also find the nearest hotter loop
3394 to LOOP. Then recursively iterate each inner loop. */
3397 fill_coldest_and_hotter_out_loop (class loop
*coldest_loop
,
3398 class loop
*hotter_loop
, class loop
*loop
)
3400 if (bb_colder_than_loop_preheader (loop_preheader_edge (loop
)->src
,
3402 coldest_loop
= loop
;
3404 coldest_outermost_loop
[loop
->num
] = coldest_loop
;
3406 hotter_than_inner_loop
[loop
->num
] = NULL
;
3407 class loop
*outer_loop
= loop_outer (loop
);
3409 && bb_colder_than_loop_preheader (loop_preheader_edge (loop
)->src
,
3411 hotter_than_inner_loop
[loop
->num
] = hotter_loop
;
3413 if (outer_loop
&& outer_loop
!= current_loops
->tree_root
3414 && bb_colder_than_loop_preheader (loop_preheader_edge (loop
)->src
,
3416 hotter_than_inner_loop
[loop
->num
] = outer_loop
;
3418 if (dump_enabled_p ())
3420 dump_printf (MSG_NOTE
, "loop %d's coldest_outermost_loop is %d, ",
3421 loop
->num
, coldest_loop
->num
);
3422 if (hotter_than_inner_loop
[loop
->num
])
3423 dump_printf (MSG_NOTE
, "hotter_than_inner_loop is %d\n",
3424 hotter_than_inner_loop
[loop
->num
]->num
);
3426 dump_printf (MSG_NOTE
, "hotter_than_inner_loop is NULL\n");
3429 class loop
*inner_loop
;
3430 for (inner_loop
= loop
->inner
; inner_loop
; inner_loop
= inner_loop
->next
)
3431 fill_coldest_and_hotter_out_loop (coldest_loop
,
3432 hotter_than_inner_loop
[loop
->num
],
3436 /* Compute the global information needed by the loop invariant motion pass. */
3439 tree_ssa_lim_initialize (bool store_motion
)
3443 bitmap_obstack_initialize (&lim_bitmap_obstack
);
3444 gcc_obstack_init (&mem_ref_obstack
);
3445 lim_aux_data_map
= new hash_map
<gimple
*, lim_aux_data
*>;
3448 compute_transaction_bits ();
3450 memory_accesses
.refs
= new hash_table
<mem_ref_hasher
> (100);
3451 memory_accesses
.refs_list
.create (100);
3452 /* Allocate a special, unanalyzable mem-ref with ID zero. */
3453 memory_accesses
.refs_list
.quick_push
3454 (mem_ref_alloc (NULL
, 0, UNANALYZABLE_MEM_ID
));
3456 memory_accesses
.refs_loaded_in_loop
.create (number_of_loops (cfun
));
3457 memory_accesses
.refs_loaded_in_loop
.quick_grow (number_of_loops (cfun
));
3458 memory_accesses
.refs_stored_in_loop
.create (number_of_loops (cfun
));
3459 memory_accesses
.refs_stored_in_loop
.quick_grow (number_of_loops (cfun
));
3462 memory_accesses
.all_refs_stored_in_loop
.create (number_of_loops (cfun
));
3463 memory_accesses
.all_refs_stored_in_loop
.quick_grow
3464 (number_of_loops (cfun
));
3467 for (i
= 0; i
< number_of_loops (cfun
); i
++)
3469 bitmap_initialize (&memory_accesses
.refs_loaded_in_loop
[i
],
3470 &lim_bitmap_obstack
);
3471 bitmap_initialize (&memory_accesses
.refs_stored_in_loop
[i
],
3472 &lim_bitmap_obstack
);
3474 bitmap_initialize (&memory_accesses
.all_refs_stored_in_loop
[i
],
3475 &lim_bitmap_obstack
);
3478 memory_accesses
.ttae_cache
= NULL
;
3480 /* Initialize bb_loop_postorder with a mapping from loop->num to
3481 its postorder index. */
3483 bb_loop_postorder
= XNEWVEC (unsigned, number_of_loops (cfun
));
3484 for (auto loop
: loops_list (cfun
, LI_FROM_INNERMOST
))
3485 bb_loop_postorder
[loop
->num
] = i
++;
3488 /* Cleans up after the invariant motion pass. */
3491 tree_ssa_lim_finalize (void)
3497 FOR_EACH_BB_FN (bb
, cfun
)
3498 SET_ALWAYS_EXECUTED_IN (bb
, NULL
);
3500 bitmap_obstack_release (&lim_bitmap_obstack
);
3501 delete lim_aux_data_map
;
3503 delete memory_accesses
.refs
;
3504 memory_accesses
.refs
= NULL
;
3506 FOR_EACH_VEC_ELT (memory_accesses
.refs_list
, i
, ref
)
3508 memory_accesses
.refs_list
.release ();
3509 obstack_free (&mem_ref_obstack
, NULL
);
3511 memory_accesses
.refs_loaded_in_loop
.release ();
3512 memory_accesses
.refs_stored_in_loop
.release ();
3513 memory_accesses
.all_refs_stored_in_loop
.release ();
3515 if (memory_accesses
.ttae_cache
)
3516 free_affine_expand_cache (&memory_accesses
.ttae_cache
);
3518 free (bb_loop_postorder
);
3520 coldest_outermost_loop
.release ();
3521 hotter_than_inner_loop
.release ();
3524 /* Moves invariants from loops. Only "expensive" invariants are moved out --
3525 i.e. those that are likely to be win regardless of the register pressure.
3526 Only perform store motion if STORE_MOTION is true. */
3529 loop_invariant_motion_in_fun (function
*fun
, bool store_motion
)
3531 unsigned int todo
= 0;
3533 tree_ssa_lim_initialize (store_motion
);
3535 /* Gathers information about memory accesses in the loops. */
3536 analyze_memory_references (store_motion
);
3538 /* Fills ALWAYS_EXECUTED_IN information for basic blocks. */
3539 fill_always_executed_in ();
3541 /* Pre-compute coldest outermost loop and nearest hotter loop of each loop.
3544 coldest_outermost_loop
.create (number_of_loops (cfun
));
3545 coldest_outermost_loop
.safe_grow_cleared (number_of_loops (cfun
));
3546 hotter_than_inner_loop
.create (number_of_loops (cfun
));
3547 hotter_than_inner_loop
.safe_grow_cleared (number_of_loops (cfun
));
3548 for (loop
= current_loops
->tree_root
->inner
; loop
!= NULL
; loop
= loop
->next
)
3549 fill_coldest_and_hotter_out_loop (loop
, NULL
, loop
);
3551 int *rpo
= XNEWVEC (int, last_basic_block_for_fn (fun
));
3552 int n
= pre_and_rev_post_order_compute_fn (fun
, NULL
, rpo
, false);
3554 /* For each statement determine the outermost loop in that it is
3555 invariant and cost for computing the invariant. */
3556 for (int i
= 0; i
< n
; ++i
)
3557 compute_invariantness (BASIC_BLOCK_FOR_FN (fun
, rpo
[i
]));
3559 /* Execute store motion. Force the necessary invariants to be moved
3560 out of the loops as well. */
3565 rpo
= XNEWVEC (int, last_basic_block_for_fn (fun
));
3566 n
= pre_and_rev_post_order_compute_fn (fun
, NULL
, rpo
, false);
3568 /* Move the expressions that are expensive enough. */
3569 for (int i
= 0; i
< n
; ++i
)
3570 todo
|= move_computations_worker (BASIC_BLOCK_FOR_FN (fun
, rpo
[i
]));
3574 gsi_commit_edge_inserts ();
3575 if (need_ssa_update_p (fun
))
3576 rewrite_into_loop_closed_ssa (NULL
, TODO_update_ssa
);
3578 tree_ssa_lim_finalize ();
3583 /* Loop invariant motion pass. */
3587 const pass_data pass_data_lim
=
3589 GIMPLE_PASS
, /* type */
3591 OPTGROUP_LOOP
, /* optinfo_flags */
3593 PROP_cfg
, /* properties_required */
3594 0, /* properties_provided */
3595 0, /* properties_destroyed */
3596 0, /* todo_flags_start */
3597 0, /* todo_flags_finish */
3600 class pass_lim
: public gimple_opt_pass
3603 pass_lim (gcc::context
*ctxt
)
3604 : gimple_opt_pass (pass_data_lim
, ctxt
)
3607 /* opt_pass methods: */
3608 opt_pass
* clone () final override
{ return new pass_lim (m_ctxt
); }
3609 bool gate (function
*) final override
{ return flag_tree_loop_im
!= 0; }
3610 unsigned int execute (function
*) final override
;
3612 }; // class pass_lim
3615 pass_lim::execute (function
*fun
)
3617 bool in_loop_pipeline
= scev_initialized_p ();
3618 if (!in_loop_pipeline
)
3619 loop_optimizer_init (LOOPS_NORMAL
| LOOPS_HAVE_RECORDED_EXITS
);
3621 if (number_of_loops (fun
) <= 1)
3623 unsigned int todo
= loop_invariant_motion_in_fun (fun
, flag_move_loop_stores
);
3625 if (!in_loop_pipeline
)
3626 loop_optimizer_finalize ();
3635 make_pass_lim (gcc::context
*ctxt
)
3637 return new pass_lim (ctxt
);