Branch data Line data Source code
1 : : /* Loop invariant motion.
2 : : Copyright (C) 2003-2025 Free Software Foundation, Inc.
3 : :
4 : : This file is part of GCC.
5 : :
6 : : GCC is free software; you can redistribute it and/or modify it
7 : : under the terms of the GNU General Public License as published by the
8 : : Free Software Foundation; either version 3, or (at your option) any
9 : : later version.
10 : :
11 : : GCC is distributed in the hope that it will be useful, but WITHOUT
12 : : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 : : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 : : for more details.
15 : :
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/>. */
19 : :
20 : : #include "config.h"
21 : : #include "system.h"
22 : : #include "coretypes.h"
23 : : #include "backend.h"
24 : : #include "tree.h"
25 : : #include "gimple.h"
26 : : #include "cfghooks.h"
27 : : #include "tree-pass.h"
28 : : #include "ssa.h"
29 : : #include "gimple-pretty-print.h"
30 : : #include "fold-const.h"
31 : : #include "cfganal.h"
32 : : #include "tree-eh.h"
33 : : #include "gimplify.h"
34 : : #include "gimple-iterator.h"
35 : : #include "tree-cfg.h"
36 : : #include "tree-ssa-loop-manip.h"
37 : : #include "tree-ssa-loop.h"
38 : : #include "tree-into-ssa.h"
39 : : #include "cfgloop.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"
46 : : #include "alias.h"
47 : : #include "builtins.h"
48 : : #include "tree-dfa.h"
49 : : #include "tree-ssa.h"
50 : : #include "dbgcnt.h"
51 : : #include "insn-codes.h"
52 : : #include "optabs-tree.h"
53 : :
54 : : /* TODO: Support for predicated code motion. I.e.
55 : :
56 : : while (1)
57 : : {
58 : : if (cond)
59 : : {
60 : : a = inv;
61 : : something;
62 : : }
63 : : }
64 : :
65 : : Where COND and INV are invariants, but evaluating INV may trap or be
66 : : invalid from some other reason if !COND. This may be transformed to
67 : :
68 : : if (cond)
69 : : a = inv;
70 : : while (1)
71 : : {
72 : : if (cond)
73 : : something;
74 : : } */
75 : :
76 : : /* The auxiliary data kept for each statement. */
77 : :
78 : : struct lim_aux_data
79 : : {
80 : : class loop *max_loop; /* The outermost loop in that the statement
81 : : is invariant. */
82 : :
83 : : class loop *tgt_loop; /* The loop out of that we want to move the
84 : : invariant. */
85 : :
86 : : class loop *always_executed_in;
87 : : /* The outermost loop for that we are sure
88 : : the statement is executed if the loop
89 : : is entered. */
90 : :
91 : : unsigned cost; /* Cost of the computation performed by the
92 : : statement. */
93 : :
94 : : unsigned ref; /* The simple_mem_ref in this stmt or 0. */
95 : :
96 : : vec<gimple *> depends; /* Vector of statements that must be also
97 : : hoisted out of the loop when this statement
98 : : is hoisted; i.e. those that define the
99 : : operands of the statement and are inside of
100 : : the MAX_LOOP loop. */
101 : : };
102 : :
103 : : /* Maps statements to their lim_aux_data. */
104 : :
105 : : static hash_map<gimple *, lim_aux_data *> *lim_aux_data_map;
106 : :
107 : : /* Description of a memory reference location. */
108 : :
109 : : struct mem_ref_loc
110 : : {
111 : : tree *ref; /* The reference itself. */
112 : : gimple *stmt; /* The statement in that it occurs. */
113 : : };
114 : :
115 : :
116 : : /* Description of a memory reference. */
117 : :
118 : : class im_mem_ref
119 : : {
120 : : public:
121 : : unsigned id : 30; /* ID assigned to the memory reference
122 : : (its index in memory_accesses.refs_list) */
123 : : unsigned ref_canonical : 1; /* Whether mem.ref was canonicalized. */
124 : : unsigned ref_decomposed : 1; /* Whether the ref was hashed from mem. */
125 : : hashval_t hash; /* Its hash value. */
126 : :
127 : : /* The memory access itself and associated caching of alias-oracle
128 : : query meta-data. We are using mem.ref == error_mark_node for the
129 : : case the reference is represented by its single access stmt
130 : : in accesses_in_loop[0]. */
131 : : ao_ref mem;
132 : :
133 : : bitmap stored; /* The set of loops in that this memory location
134 : : is stored to. */
135 : : bitmap loaded; /* The set of loops in that this memory location
136 : : is loaded from. */
137 : : vec<mem_ref_loc> accesses_in_loop;
138 : : /* The locations of the accesses. */
139 : :
140 : : /* The following set is computed on demand. */
141 : : bitmap_head dep_loop; /* The set of loops in that the memory
142 : : reference is {in,}dependent in
143 : : different modes. */
144 : : };
145 : :
146 : : static bool in_loop_pipeline;
147 : :
148 : : /* We use six bits per loop in the ref->dep_loop bitmap to record
149 : : the dep_kind x dep_state combinations. */
150 : :
151 : : enum dep_kind { lim_raw, sm_war, sm_waw };
152 : : enum dep_state { dep_unknown, dep_independent, dep_dependent };
153 : :
154 : : /* coldest outermost loop for given loop. */
155 : : vec<class loop *> coldest_outermost_loop;
156 : : /* hotter outer loop nearest to given loop. */
157 : : vec<class loop *> hotter_than_inner_loop;
158 : :
159 : : /* Populate the loop dependence cache of REF for LOOP, KIND with STATE. */
160 : :
161 : : static void
162 : 1941801 : record_loop_dependence (class loop *loop, im_mem_ref *ref,
163 : : dep_kind kind, dep_state state)
164 : : {
165 : 1941801 : gcc_assert (state != dep_unknown);
166 : 1941801 : unsigned bit = 6 * loop->num + kind * 2 + state == dep_dependent ? 1 : 0;
167 : 1941801 : bitmap_set_bit (&ref->dep_loop, bit);
168 : 1941801 : }
169 : :
170 : : /* Query the loop dependence cache of REF for LOOP, KIND. */
171 : :
172 : : static dep_state
173 : 626165 : query_loop_dependence (class loop *loop, im_mem_ref *ref, dep_kind kind)
174 : : {
175 : 626165 : unsigned first_bit = 6 * loop->num + kind * 2;
176 : 626165 : if (bitmap_bit_p (&ref->dep_loop, first_bit))
177 : : return dep_independent;
178 : 626165 : else if (bitmap_bit_p (&ref->dep_loop, first_bit + 1))
179 : 0 : return dep_dependent;
180 : : return dep_unknown;
181 : : }
182 : :
183 : : /* Mem_ref hashtable helpers. */
184 : :
185 : : struct mem_ref_hasher : nofree_ptr_hash <im_mem_ref>
186 : : {
187 : : typedef ao_ref *compare_type;
188 : : static inline hashval_t hash (const im_mem_ref *);
189 : : static inline bool equal (const im_mem_ref *, const ao_ref *);
190 : : };
191 : :
192 : : /* A hash function for class im_mem_ref object OBJ. */
193 : :
194 : : inline hashval_t
195 : 11145374 : mem_ref_hasher::hash (const im_mem_ref *mem)
196 : : {
197 : 11145374 : return mem->hash;
198 : : }
199 : :
200 : : /* An equality function for class im_mem_ref object MEM1 with
201 : : memory reference OBJ2. */
202 : :
203 : : inline bool
204 : 12978592 : mem_ref_hasher::equal (const im_mem_ref *mem1, const ao_ref *obj2)
205 : : {
206 : 12978592 : if (obj2->max_size_known_p ())
207 : 11492844 : return (mem1->ref_decomposed
208 : 10992872 : && ((TREE_CODE (mem1->mem.base) == MEM_REF
209 : 4556151 : && TREE_CODE (obj2->base) == MEM_REF
210 : 2901857 : && operand_equal_p (TREE_OPERAND (mem1->mem.base, 0),
211 : 2901857 : TREE_OPERAND (obj2->base, 0), 0)
212 : 13279377 : && known_eq (mem_ref_offset (mem1->mem.base) * BITS_PER_UNIT + mem1->mem.offset,
213 : : mem_ref_offset (obj2->base) * BITS_PER_UNIT + obj2->offset))
214 : 10565946 : || (operand_equal_p (mem1->mem.base, obj2->base, 0)
215 : 858042 : && known_eq (mem1->mem.offset, obj2->offset)))
216 : 936996 : && known_eq (mem1->mem.size, obj2->size)
217 : 934507 : && known_eq (mem1->mem.max_size, obj2->max_size)
218 : 934507 : && mem1->mem.volatile_p == obj2->volatile_p
219 : 934491 : && (mem1->mem.ref_alias_set == obj2->ref_alias_set
220 : : /* We are not canonicalizing alias-sets but for the
221 : : special-case we didn't canonicalize yet and the
222 : : incoming ref is a alias-set zero MEM we pick
223 : : the correct one already. */
224 : 11750 : || (!mem1->ref_canonical
225 : 11339 : && (TREE_CODE (obj2->ref) == MEM_REF
226 : 11339 : || TREE_CODE (obj2->ref) == TARGET_MEM_REF)
227 : 5057 : && obj2->ref_alias_set == 0)
228 : : /* Likewise if there's a canonical ref with alias-set zero. */
229 : 10577 : || (mem1->ref_canonical && mem1->mem.ref_alias_set == 0))
230 : 12416808 : && types_compatible_p (TREE_TYPE (mem1->mem.ref),
231 : 923964 : TREE_TYPE (obj2->ref)));
232 : : else
233 : 1485748 : return operand_equal_p (mem1->mem.ref, obj2->ref, 0);
234 : : }
235 : :
236 : :
237 : : /* Description of memory accesses in loops. */
238 : :
239 : : static struct
240 : : {
241 : : /* The hash table of memory references accessed in loops. */
242 : : hash_table<mem_ref_hasher> *refs;
243 : :
244 : : /* The list of memory references. */
245 : : vec<im_mem_ref *> refs_list;
246 : :
247 : : /* The set of memory references accessed in each loop. */
248 : : vec<bitmap_head> refs_loaded_in_loop;
249 : :
250 : : /* The set of memory references stored in each loop. */
251 : : vec<bitmap_head> refs_stored_in_loop;
252 : :
253 : : /* The set of memory references stored in each loop, including subloops . */
254 : : vec<bitmap_head> all_refs_stored_in_loop;
255 : :
256 : : /* Cache for expanding memory addresses. */
257 : : hash_map<tree, name_expansion *> *ttae_cache;
258 : : } memory_accesses;
259 : :
260 : : /* Obstack for the bitmaps in the above data structures. */
261 : : static bitmap_obstack lim_bitmap_obstack;
262 : : static obstack mem_ref_obstack;
263 : :
264 : : static bool ref_indep_loop_p (class loop *, im_mem_ref *, dep_kind);
265 : : static bool ref_always_accessed_p (class loop *, im_mem_ref *, bool);
266 : : static bool refs_independent_p (im_mem_ref *, im_mem_ref *, bool = true);
267 : :
268 : : /* Minimum cost of an expensive expression. */
269 : : #define LIM_EXPENSIVE ((unsigned) param_lim_expensive)
270 : :
271 : : /* The outermost loop for which execution of the header guarantees that the
272 : : block will be executed. */
273 : : #define ALWAYS_EXECUTED_IN(BB) ((class loop *) (BB)->aux)
274 : : #define SET_ALWAYS_EXECUTED_IN(BB, VAL) ((BB)->aux = (void *) (VAL))
275 : :
276 : : /* ID of the shared unanalyzable mem. */
277 : : #define UNANALYZABLE_MEM_ID 0
278 : :
279 : : /* Whether the reference was analyzable. */
280 : : #define MEM_ANALYZABLE(REF) ((REF)->id != UNANALYZABLE_MEM_ID)
281 : :
282 : : static struct lim_aux_data *
283 : 17773861 : init_lim_data (gimple *stmt)
284 : : {
285 : 17773861 : lim_aux_data *p = XCNEW (struct lim_aux_data);
286 : 17773861 : lim_aux_data_map->put (stmt, p);
287 : :
288 : 17773861 : return p;
289 : : }
290 : :
291 : : static struct lim_aux_data *
292 : 94537642 : get_lim_data (gimple *stmt)
293 : : {
294 : 94537642 : lim_aux_data **p = lim_aux_data_map->get (stmt);
295 : 94537642 : if (!p)
296 : : return NULL;
297 : :
298 : 48142358 : return *p;
299 : : }
300 : :
301 : : /* Releases the memory occupied by DATA. */
302 : :
303 : : static void
304 : 17773713 : free_lim_aux_data (struct lim_aux_data *data)
305 : : {
306 : 0 : data->depends.release ();
307 : 17773713 : free (data);
308 : 0 : }
309 : :
310 : : static void
311 : 17773713 : clear_lim_data (gimple *stmt)
312 : : {
313 : 17773713 : lim_aux_data **p = lim_aux_data_map->get (stmt);
314 : 17773713 : if (!p)
315 : : return;
316 : :
317 : 17773713 : free_lim_aux_data (*p);
318 : 17773713 : *p = NULL;
319 : : }
320 : :
321 : :
322 : : /* The possibilities of statement movement. */
323 : : enum move_pos
324 : : {
325 : : MOVE_IMPOSSIBLE, /* No movement -- side effect expression. */
326 : : MOVE_PRESERVE_EXECUTION, /* Must not cause the non-executed statement
327 : : become executed -- memory accesses, ... */
328 : : MOVE_POSSIBLE /* Unlimited movement. */
329 : : };
330 : :
331 : :
332 : : /* If it is possible to hoist the statement STMT unconditionally,
333 : : returns MOVE_POSSIBLE.
334 : : If it is possible to hoist the statement STMT, but we must avoid making
335 : : it executed if it would not be executed in the original program (e.g.
336 : : because it may trap), return MOVE_PRESERVE_EXECUTION.
337 : : Otherwise return MOVE_IMPOSSIBLE. */
338 : :
339 : : static enum move_pos
340 : 46847462 : movement_possibility_1 (gimple *stmt)
341 : : {
342 : 46847462 : tree lhs;
343 : 46847462 : enum move_pos ret = MOVE_POSSIBLE;
344 : :
345 : 46847462 : if (flag_unswitch_loops
346 : 46847462 : && gimple_code (stmt) == GIMPLE_COND)
347 : : {
348 : : /* If we perform unswitching, force the operands of the invariant
349 : : condition to be moved out of the loop. */
350 : : return MOVE_POSSIBLE;
351 : : }
352 : :
353 : 46512446 : if (gimple_code (stmt) == GIMPLE_PHI
354 : 2655234 : && gimple_phi_num_args (stmt) <= 2
355 : 5027992 : && !virtual_operand_p (gimple_phi_result (stmt))
356 : 47985445 : && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt)))
357 : : return MOVE_POSSIBLE;
358 : :
359 : 45039668 : if (gimple_get_lhs (stmt) == NULL_TREE)
360 : : return MOVE_IMPOSSIBLE;
361 : :
362 : 31652284 : if (gimple_vdef (stmt))
363 : : return MOVE_IMPOSSIBLE;
364 : :
365 : 13327665 : if (stmt_ends_bb_p (stmt)
366 : 12103062 : || gimple_has_volatile_ops (stmt)
367 : 13259641 : || gimple_has_side_effects (stmt)
368 : 26581547 : || stmt_could_throw_p (cfun, stmt))
369 : 441760 : return MOVE_IMPOSSIBLE;
370 : :
371 : 12885905 : if (is_gimple_call (stmt))
372 : : {
373 : : /* While pure or const call is guaranteed to have no side effects, we
374 : : cannot move it arbitrarily. Consider code like
375 : :
376 : : char *s = something ();
377 : :
378 : : while (1)
379 : : {
380 : : if (s)
381 : : t = strlen (s);
382 : : else
383 : : t = 0;
384 : : }
385 : :
386 : : Here the strlen call cannot be moved out of the loop, even though
387 : : s is invariant. In addition to possibly creating a call with
388 : : invalid arguments, moving out a function call that is not executed
389 : : may cause performance regressions in case the call is costly and
390 : : not executed at all. */
391 : 127844 : ret = MOVE_PRESERVE_EXECUTION;
392 : 127844 : lhs = gimple_call_lhs (stmt);
393 : : }
394 : 12758061 : else if (is_gimple_assign (stmt))
395 : 11575605 : lhs = gimple_assign_lhs (stmt);
396 : : else
397 : : return MOVE_IMPOSSIBLE;
398 : :
399 : 11703449 : if (TREE_CODE (lhs) == SSA_NAME
400 : 11703449 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
401 : : return MOVE_IMPOSSIBLE;
402 : :
403 : 11703239 : if (TREE_CODE (lhs) != SSA_NAME
404 : 11703239 : || gimple_could_trap_p (stmt))
405 : 2435322 : return MOVE_PRESERVE_EXECUTION;
406 : :
407 : 9267917 : if (is_gimple_assign (stmt))
408 : : {
409 : 9141159 : auto code = gimple_assign_rhs_code (stmt);
410 : 9141159 : tree type = TREE_TYPE (gimple_assign_rhs1 (stmt));
411 : : /* For shifts and rotates and possibly out-of-bound shift operands
412 : : we currently cannot rewrite them into something unconditionally
413 : : well-defined. */
414 : 9141159 : if ((code == LSHIFT_EXPR
415 : : || code == RSHIFT_EXPR
416 : : || code == LROTATE_EXPR
417 : 9141159 : || code == RROTATE_EXPR)
418 : 9141159 : && (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST
419 : : /* We cannot use ranges at 'stmt' here. */
420 : 124649 : || wi::ltu_p (wi::to_wide (gimple_assign_rhs2 (stmt)),
421 : 9100340 : element_precision (type))))
422 : 165468 : ret = MOVE_PRESERVE_EXECUTION;
423 : : }
424 : :
425 : : /* Non local loads in a transaction cannot be hoisted out. Well,
426 : : unless the load happens on every path out of the loop, but we
427 : : don't take this into account yet. */
428 : 9267917 : if (flag_tm
429 : 434 : && gimple_in_transaction (stmt)
430 : 9268020 : && gimple_assign_single_p (stmt))
431 : : {
432 : 21 : tree rhs = gimple_assign_rhs1 (stmt);
433 : 21 : if (DECL_P (rhs) && is_global_var (rhs))
434 : : {
435 : 18 : if (dump_file)
436 : : {
437 : 2 : fprintf (dump_file, "Cannot hoist conditional load of ");
438 : 2 : print_generic_expr (dump_file, rhs, TDF_SLIM);
439 : 2 : fprintf (dump_file, " because it is in a transaction.\n");
440 : : }
441 : 18 : return MOVE_IMPOSSIBLE;
442 : : }
443 : : }
444 : :
445 : : return ret;
446 : : }
447 : :
448 : : static enum move_pos
449 : 46847462 : movement_possibility (gimple *stmt)
450 : : {
451 : 46847462 : enum move_pos pos = movement_possibility_1 (stmt);
452 : 46847462 : if (pos == MOVE_POSSIBLE)
453 : : {
454 : 10783467 : use_operand_p use_p;
455 : 10783467 : ssa_op_iter ssa_iter;
456 : 34928543 : FOR_EACH_PHI_OR_STMT_USE (use_p, stmt, ssa_iter, SSA_OP_USE)
457 : 13379567 : if (TREE_CODE (USE_FROM_PTR (use_p)) == SSA_NAME
458 : 13379567 : && ssa_name_maybe_undef_p (USE_FROM_PTR (use_p)))
459 : 17958 : return MOVE_PRESERVE_EXECUTION;
460 : : }
461 : : return pos;
462 : : }
463 : :
464 : :
465 : : /* Compare the profile count inequality of bb and loop's preheader, it is
466 : : three-state as stated in profile-count.h, FALSE is returned if inequality
467 : : cannot be decided. */
468 : : bool
469 : 13947142 : bb_colder_than_loop_preheader (basic_block bb, class loop *loop)
470 : : {
471 : 13947142 : gcc_assert (bb && loop);
472 : 13947142 : return bb->count < loop_preheader_edge (loop)->src->count;
473 : : }
474 : :
475 : : /* Check coldest loop between OUTERMOST_LOOP and LOOP by comparing profile
476 : : count.
477 : : It does three steps check:
478 : : 1) Check whether CURR_BB is cold in it's own loop_father, if it is cold, just
479 : : return NULL which means it should not be moved out at all;
480 : : 2) CURR_BB is NOT cold, check if pre-computed COLDEST_LOOP is outside of
481 : : OUTERMOST_LOOP, if it is inside of OUTERMOST_LOOP, return the COLDEST_LOOP;
482 : : 3) If COLDEST_LOOP is outside of OUTERMOST_LOOP, check whether there is a
483 : : hotter loop between OUTERMOST_LOOP and loop in pre-computed
484 : : HOTTER_THAN_INNER_LOOP, return it's nested inner loop, otherwise return
485 : : OUTERMOST_LOOP.
486 : : At last, the coldest_loop is inside of OUTERMOST_LOOP, just return it as
487 : : the hoist target. */
488 : :
489 : : static class loop *
490 : 12302689 : get_coldest_out_loop (class loop *outermost_loop, class loop *loop,
491 : : basic_block curr_bb)
492 : : {
493 : 12302689 : gcc_assert (outermost_loop == loop
494 : : || flow_loop_nested_p (outermost_loop, loop));
495 : :
496 : : /* If bb_colder_than_loop_preheader returns false due to three-state
497 : : comparision, OUTERMOST_LOOP is returned finally to preserve the behavior.
498 : : Otherwise, return the coldest loop between OUTERMOST_LOOP and LOOP. */
499 : 12302689 : if (curr_bb && bb_colder_than_loop_preheader (curr_bb, loop))
500 : : return NULL;
501 : :
502 : 11321743 : class loop *coldest_loop = coldest_outermost_loop[loop->num];
503 : 22643486 : if (loop_depth (coldest_loop) < loop_depth (outermost_loop))
504 : : {
505 : 238200 : class loop *hotter_loop = hotter_than_inner_loop[loop->num];
506 : 238200 : if (!hotter_loop
507 : 244766 : || loop_depth (hotter_loop) < loop_depth (outermost_loop))
508 : : return outermost_loop;
509 : :
510 : : /* hotter_loop is between OUTERMOST_LOOP and LOOP like:
511 : : [loop tree root, ..., coldest_loop, ..., outermost_loop, ...,
512 : : hotter_loop, second_coldest_loop, ..., loop]
513 : : return second_coldest_loop to be the hoist target. */
514 : 3 : class loop *aloop;
515 : 3 : for (aloop = hotter_loop->inner; aloop; aloop = aloop->next)
516 : 3 : if (aloop == loop || flow_loop_nested_p (aloop, loop))
517 : 3 : return aloop;
518 : : }
519 : : return coldest_loop;
520 : : }
521 : :
522 : : /* Suppose that operand DEF is used inside the LOOP. Returns the outermost
523 : : loop to that we could move the expression using DEF if it did not have
524 : : other operands, i.e. the outermost loop enclosing LOOP in that the value
525 : : of DEF is invariant. */
526 : :
527 : : static class loop *
528 : 19475278 : outermost_invariant_loop (tree def, class loop *loop)
529 : : {
530 : 19475278 : gimple *def_stmt;
531 : 19475278 : basic_block def_bb;
532 : 19475278 : class loop *max_loop;
533 : 19475278 : struct lim_aux_data *lim_data;
534 : :
535 : 19475278 : if (!def)
536 : 883175 : return superloop_at_depth (loop, 1);
537 : :
538 : 18592103 : if (TREE_CODE (def) != SSA_NAME)
539 : : {
540 : 3849628 : gcc_assert (is_gimple_min_invariant (def));
541 : 3849628 : return superloop_at_depth (loop, 1);
542 : : }
543 : :
544 : 14742475 : def_stmt = SSA_NAME_DEF_STMT (def);
545 : 14742475 : def_bb = gimple_bb (def_stmt);
546 : 14742475 : if (!def_bb)
547 : 71151 : return superloop_at_depth (loop, 1);
548 : :
549 : 14671324 : max_loop = find_common_loop (loop, def_bb->loop_father);
550 : :
551 : 14671324 : lim_data = get_lim_data (def_stmt);
552 : 14671324 : if (lim_data != NULL && lim_data->max_loop != NULL)
553 : 681126 : max_loop = find_common_loop (max_loop,
554 : : loop_outer (lim_data->max_loop));
555 : 14671324 : if (max_loop == loop)
556 : : return NULL;
557 : 1842620 : max_loop = superloop_at_depth (loop, loop_depth (max_loop) + 1);
558 : :
559 : 1842620 : return max_loop;
560 : : }
561 : :
562 : : /* DATA is a structure containing information associated with a statement
563 : : inside LOOP. DEF is one of the operands of this statement.
564 : :
565 : : Find the outermost loop enclosing LOOP in that value of DEF is invariant
566 : : and record this in DATA->max_loop field. If DEF itself is defined inside
567 : : this loop as well (i.e. we need to hoist it out of the loop if we want
568 : : to hoist the statement represented by DATA), record the statement in that
569 : : DEF is defined to the DATA->depends list. Additionally if ADD_COST is true,
570 : : add the cost of the computation of DEF to the DATA->cost.
571 : :
572 : : If DEF is not invariant in LOOP, return false. Otherwise return TRUE. */
573 : :
574 : : static bool
575 : 11479648 : add_dependency (tree def, struct lim_aux_data *data, class loop *loop,
576 : : bool add_cost)
577 : : {
578 : 11479648 : gimple *def_stmt = SSA_NAME_DEF_STMT (def);
579 : 11479648 : basic_block def_bb = gimple_bb (def_stmt);
580 : 11479648 : class loop *max_loop;
581 : 11479648 : struct lim_aux_data *def_data;
582 : :
583 : 11479648 : if (!def_bb)
584 : : return true;
585 : :
586 : 11167464 : max_loop = outermost_invariant_loop (def, loop);
587 : 11167464 : if (!max_loop)
588 : : return false;
589 : :
590 : 1390287 : if (flow_loop_nested_p (data->max_loop, max_loop))
591 : 434781 : data->max_loop = max_loop;
592 : :
593 : 1390287 : def_data = get_lim_data (def_stmt);
594 : 1390287 : if (!def_data)
595 : : return true;
596 : :
597 : 672658 : if (add_cost
598 : : /* Only add the cost if the statement defining DEF is inside LOOP,
599 : : i.e. if it is likely that by moving the invariants dependent
600 : : on it, we will be able to avoid creating a new register for
601 : : it (since it will be only used in these dependent invariants). */
602 : 648777 : && def_bb->loop_father == loop)
603 : 455809 : data->cost += def_data->cost;
604 : :
605 : 672658 : data->depends.safe_push (def_stmt);
606 : :
607 : 672658 : return true;
608 : : }
609 : :
610 : : /* Returns an estimate for a cost of statement STMT. The values here
611 : : are just ad-hoc constants, similar to costs for inlining. */
612 : :
613 : : static unsigned
614 : 763505 : stmt_cost (gimple *stmt)
615 : : {
616 : : /* Always try to create possibilities for unswitching. */
617 : 763505 : if (gimple_code (stmt) == GIMPLE_COND
618 : 763505 : || gimple_code (stmt) == GIMPLE_PHI)
619 : 12322 : return LIM_EXPENSIVE;
620 : :
621 : : /* We should be hoisting calls if possible. */
622 : 751183 : if (is_gimple_call (stmt))
623 : : {
624 : 671 : tree fndecl;
625 : :
626 : : /* Unless the call is a builtin_constant_p; this always folds to a
627 : : constant, so moving it is useless. */
628 : 671 : fndecl = gimple_call_fndecl (stmt);
629 : 671 : if (fndecl && fndecl_built_in_p (fndecl, BUILT_IN_CONSTANT_P))
630 : : return 0;
631 : :
632 : 671 : return LIM_EXPENSIVE;
633 : : }
634 : :
635 : : /* Hoisting memory references out should almost surely be a win. */
636 : 750512 : if (gimple_references_memory_p (stmt))
637 : 65436 : return LIM_EXPENSIVE;
638 : :
639 : 685076 : if (gimple_code (stmt) != GIMPLE_ASSIGN)
640 : : return 1;
641 : :
642 : 685076 : enum tree_code code = gimple_assign_rhs_code (stmt);
643 : 685076 : switch (code)
644 : : {
645 : 117237 : case MULT_EXPR:
646 : 117237 : case WIDEN_MULT_EXPR:
647 : 117237 : case WIDEN_MULT_PLUS_EXPR:
648 : 117237 : case WIDEN_MULT_MINUS_EXPR:
649 : 117237 : case DOT_PROD_EXPR:
650 : 117237 : case TRUNC_DIV_EXPR:
651 : 117237 : case CEIL_DIV_EXPR:
652 : 117237 : case FLOOR_DIV_EXPR:
653 : 117237 : case ROUND_DIV_EXPR:
654 : 117237 : case EXACT_DIV_EXPR:
655 : 117237 : case CEIL_MOD_EXPR:
656 : 117237 : case FLOOR_MOD_EXPR:
657 : 117237 : case ROUND_MOD_EXPR:
658 : 117237 : case TRUNC_MOD_EXPR:
659 : 117237 : case RDIV_EXPR:
660 : : /* Division and multiplication are usually expensive. */
661 : 117237 : return LIM_EXPENSIVE;
662 : :
663 : 4133 : case LSHIFT_EXPR:
664 : 4133 : case RSHIFT_EXPR:
665 : 4133 : case WIDEN_LSHIFT_EXPR:
666 : 4133 : case LROTATE_EXPR:
667 : 4133 : case RROTATE_EXPR:
668 : : /* Shifts and rotates are usually expensive. */
669 : 4133 : return LIM_EXPENSIVE;
670 : :
671 : 534 : case COND_EXPR:
672 : 534 : case VEC_COND_EXPR:
673 : : /* Conditionals are expensive. */
674 : 534 : return LIM_EXPENSIVE;
675 : :
676 : 1560 : case CONSTRUCTOR:
677 : : /* Make vector construction cost proportional to the number
678 : : of elements. */
679 : 1560 : return CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt));
680 : :
681 : : case SSA_NAME:
682 : : case PAREN_EXPR:
683 : : /* Whether or not something is wrapped inside a PAREN_EXPR
684 : : should not change move cost. Nor should an intermediate
685 : : unpropagated SSA name copy. */
686 : : return 0;
687 : :
688 : 523198 : default:
689 : : /* Comparisons are usually expensive. */
690 : 523198 : if (TREE_CODE_CLASS (code) == tcc_comparison)
691 : 8011 : return LIM_EXPENSIVE;
692 : : return 1;
693 : : }
694 : : }
695 : :
696 : : /* Finds the outermost loop between OUTER and LOOP in that the memory reference
697 : : REF is independent. If REF is not independent in LOOP, NULL is returned
698 : : instead. */
699 : :
700 : : static class loop *
701 : 786775 : outermost_indep_loop (class loop *outer, class loop *loop, im_mem_ref *ref)
702 : : {
703 : 786775 : class loop *aloop;
704 : :
705 : 786775 : if (ref->stored && bitmap_bit_p (ref->stored, loop->num))
706 : : return NULL;
707 : :
708 : 89494 : for (aloop = outer;
709 : 716191 : aloop != loop;
710 : 89494 : aloop = superloop_at_depth (loop, loop_depth (aloop) + 1))
711 : 9243 : if ((!ref->stored || !bitmap_bit_p (ref->stored, aloop->num))
712 : 105353 : && ref_indep_loop_p (aloop, ref, lim_raw))
713 : : return aloop;
714 : :
715 : 612545 : if (ref_indep_loop_p (loop, ref, lim_raw))
716 : : return loop;
717 : : else
718 : : return NULL;
719 : : }
720 : :
721 : : /* If there is a simple load or store to a memory reference in STMT, returns
722 : : the location of the memory reference, and sets IS_STORE according to whether
723 : : it is a store or load. Otherwise, returns NULL. */
724 : :
725 : : static tree *
726 : 7145817 : simple_mem_ref_in_stmt (gimple *stmt, bool *is_store)
727 : : {
728 : 7145817 : tree *lhs, *rhs;
729 : :
730 : : /* Recognize SSA_NAME = MEM and MEM = (SSA_NAME | invariant) patterns. */
731 : 7145817 : if (!gimple_assign_single_p (stmt))
732 : : return NULL;
733 : :
734 : 5881944 : lhs = gimple_assign_lhs_ptr (stmt);
735 : 5881944 : rhs = gimple_assign_rhs1_ptr (stmt);
736 : :
737 : 9158253 : if (TREE_CODE (*lhs) == SSA_NAME && gimple_vuse (stmt))
738 : : {
739 : 3276309 : *is_store = false;
740 : 3276309 : return rhs;
741 : : }
742 : 2605635 : else if (gimple_vdef (stmt)
743 : 2605635 : && (TREE_CODE (*rhs) == SSA_NAME || is_gimple_min_invariant (*rhs)))
744 : : {
745 : 2073699 : *is_store = true;
746 : 2073699 : return lhs;
747 : : }
748 : : else
749 : 531936 : return NULL;
750 : : }
751 : :
752 : : /* From a controlling predicate in DOM determine the arguments from
753 : : the PHI node PHI that are chosen if the predicate evaluates to
754 : : true and false and store them to *TRUE_ARG_P and *FALSE_ARG_P if
755 : : they are non-NULL. Returns true if the arguments can be determined,
756 : : else return false. */
757 : :
758 : : static bool
759 : 13591 : extract_true_false_args_from_phi (basic_block dom, gphi *phi,
760 : : tree *true_arg_p, tree *false_arg_p)
761 : : {
762 : 13591 : edge te, fe;
763 : 13591 : if (! extract_true_false_controlled_edges (dom, gimple_bb (phi),
764 : : &te, &fe))
765 : : return false;
766 : :
767 : 11673 : if (true_arg_p)
768 : 895 : *true_arg_p = PHI_ARG_DEF (phi, te->dest_idx);
769 : 11673 : if (false_arg_p)
770 : 895 : *false_arg_p = PHI_ARG_DEF (phi, fe->dest_idx);
771 : :
772 : : return true;
773 : : }
774 : :
775 : : /* Determine the outermost loop to that it is possible to hoist a statement
776 : : STMT and store it to LIM_DATA (STMT)->max_loop. To do this we determine
777 : : the outermost loop in that the value computed by STMT is invariant.
778 : : If MUST_PRESERVE_EXEC is true, additionally choose such a loop that
779 : : we preserve the fact whether STMT is executed. It also fills other related
780 : : information to LIM_DATA (STMT).
781 : :
782 : : The function returns false if STMT cannot be hoisted outside of the loop it
783 : : is defined in, and true otherwise. */
784 : :
785 : : static bool
786 : 12259569 : determine_max_movement (gimple *stmt, bool must_preserve_exec)
787 : : {
788 : 12259569 : basic_block bb = gimple_bb (stmt);
789 : 12259569 : class loop *loop = bb->loop_father;
790 : 12259569 : class loop *level;
791 : 12259569 : struct lim_aux_data *lim_data = get_lim_data (stmt);
792 : 12259569 : tree val;
793 : 12259569 : ssa_op_iter iter;
794 : :
795 : 12259569 : if (must_preserve_exec)
796 : 1488308 : level = ALWAYS_EXECUTED_IN (bb);
797 : : else
798 : 10771261 : level = superloop_at_depth (loop, 1);
799 : 12259569 : lim_data->max_loop = get_coldest_out_loop (level, loop, bb);
800 : 12259569 : if (!lim_data->max_loop)
801 : : return false;
802 : :
803 : 11280326 : if (gphi *phi = dyn_cast <gphi *> (stmt))
804 : : {
805 : 1412563 : use_operand_p use_p;
806 : 1412563 : unsigned min_cost = UINT_MAX;
807 : 1412563 : unsigned total_cost = 0;
808 : 1412563 : struct lim_aux_data *def_data;
809 : :
810 : : /* We will end up promoting dependencies to be unconditionally
811 : : evaluated. For this reason the PHI cost (and thus the
812 : : cost we remove from the loop by doing the invariant motion)
813 : : is that of the cheapest PHI argument dependency chain. */
814 : 1647303 : FOR_EACH_PHI_ARG (use_p, phi, iter, SSA_OP_USE)
815 : : {
816 : 1618328 : val = USE_FROM_PTR (use_p);
817 : :
818 : 1618328 : if (TREE_CODE (val) != SSA_NAME)
819 : : {
820 : : /* Assign const 1 to constants. */
821 : 140893 : min_cost = MIN (min_cost, 1);
822 : 140893 : total_cost += 1;
823 : 140893 : continue;
824 : : }
825 : 1477435 : if (!add_dependency (val, lim_data, loop, false))
826 : : return false;
827 : :
828 : 93847 : gimple *def_stmt = SSA_NAME_DEF_STMT (val);
829 : 93847 : if (gimple_bb (def_stmt)
830 : 93847 : && gimple_bb (def_stmt)->loop_father == loop)
831 : : {
832 : 1386 : def_data = get_lim_data (def_stmt);
833 : 1386 : if (def_data)
834 : : {
835 : 1386 : min_cost = MIN (min_cost, def_data->cost);
836 : 1386 : total_cost += def_data->cost;
837 : : }
838 : : }
839 : : }
840 : :
841 : 28975 : min_cost = MIN (min_cost, total_cost);
842 : 28975 : lim_data->cost += min_cost;
843 : :
844 : 28975 : if (gimple_phi_num_args (phi) > 1)
845 : : {
846 : 28771 : basic_block dom = get_immediate_dominator (CDI_DOMINATORS, bb);
847 : 28771 : gimple *cond;
848 : 57542 : if (gsi_end_p (gsi_last_bb (dom)))
849 : : return false;
850 : 17773 : cond = gsi_stmt (gsi_last_bb (dom));
851 : 17773 : if (gimple_code (cond) != GIMPLE_COND)
852 : : return false;
853 : : /* Verify that this is an extended form of a diamond and
854 : : the PHI arguments are completely controlled by the
855 : : predicate in DOM. */
856 : 12696 : if (!extract_true_false_args_from_phi (dom, phi, NULL, NULL))
857 : : return false;
858 : :
859 : : /* Check if one of the depedent statement is a vector compare whether
860 : : the target supports it, otherwise it's invalid to hoist it out of
861 : : the gcond it belonged to. */
862 : 10778 : if (VECTOR_TYPE_P (TREE_TYPE (gimple_cond_lhs (cond))))
863 : : {
864 : 2 : tree type = TREE_TYPE (gimple_cond_lhs (cond));
865 : 2 : auto code = gimple_cond_code (cond);
866 : 2 : if (!target_supports_op_p (type, code, optab_vector))
867 : : return false;
868 : : }
869 : :
870 : : /* Fold in dependencies and cost of the condition. */
871 : 12713 : FOR_EACH_SSA_TREE_OPERAND (val, cond, iter, SSA_OP_USE)
872 : : {
873 : 11714 : if (!add_dependency (val, lim_data, loop, false))
874 : : return false;
875 : 1937 : def_data = get_lim_data (SSA_NAME_DEF_STMT (val));
876 : 1937 : if (def_data)
877 : 908 : lim_data->cost += def_data->cost;
878 : : }
879 : :
880 : : /* We want to avoid unconditionally executing very expensive
881 : : operations. As costs for our dependencies cannot be
882 : : negative just claim we are not invariand for this case.
883 : : We also are not sure whether the control-flow inside the
884 : : loop will vanish. */
885 : 999 : if (total_cost - min_cost >= 2 * LIM_EXPENSIVE
886 : 125 : && !(min_cost != 0
887 : 125 : && total_cost / min_cost <= 2))
888 : : return false;
889 : :
890 : : /* Assume that the control-flow in the loop will vanish.
891 : : ??? We should verify this and not artificially increase
892 : : the cost if that is not the case. */
893 : 895 : lim_data->cost += stmt_cost (stmt);
894 : : }
895 : :
896 : 1099 : return true;
897 : : }
898 : :
899 : : /* A stmt that receives abnormal edges cannot be hoisted. */
900 : 9867763 : if (is_a <gcall *> (stmt)
901 : 9867763 : && (gimple_call_flags (stmt) & ECF_RETURNS_TWICE))
902 : : return false;
903 : :
904 : 11474425 : FOR_EACH_SSA_TREE_OPERAND (val, stmt, iter, SSA_OP_USE)
905 : 9989538 : if (!add_dependency (val, lim_data, loop, true))
906 : : return false;
907 : :
908 : 2958347 : if (gimple_vuse (stmt))
909 : : {
910 : 787736 : im_mem_ref *ref
911 : 787736 : = lim_data ? memory_accesses.refs_list[lim_data->ref] : NULL;
912 : 787736 : if (ref
913 : 787736 : && MEM_ANALYZABLE (ref))
914 : : {
915 : 786775 : lim_data->max_loop = outermost_indep_loop (lim_data->max_loop,
916 : : loop, ref);
917 : 786775 : if (!lim_data->max_loop)
918 : : return false;
919 : : }
920 : 961 : else if (! add_dependency (gimple_vuse (stmt), lim_data, loop, false))
921 : : return false;
922 : : }
923 : :
924 : 762610 : lim_data->cost += stmt_cost (stmt);
925 : :
926 : 762610 : return true;
927 : : }
928 : :
929 : : /* Suppose that some statement in ORIG_LOOP is hoisted to the loop LEVEL,
930 : : and that one of the operands of this statement is computed by STMT.
931 : : Ensure that STMT (together with all the statements that define its
932 : : operands) is hoisted at least out of the loop LEVEL. */
933 : :
934 : : static void
935 : 685754 : set_level (gimple *stmt, class loop *orig_loop, class loop *level)
936 : : {
937 : 685754 : class loop *stmt_loop = gimple_bb (stmt)->loop_father;
938 : 685754 : struct lim_aux_data *lim_data;
939 : 685754 : gimple *dep_stmt;
940 : 685754 : unsigned i;
941 : :
942 : 685754 : stmt_loop = find_common_loop (orig_loop, stmt_loop);
943 : 685754 : lim_data = get_lim_data (stmt);
944 : 685754 : if (lim_data != NULL && lim_data->tgt_loop != NULL)
945 : 170351 : stmt_loop = find_common_loop (stmt_loop,
946 : : loop_outer (lim_data->tgt_loop));
947 : 685754 : if (flow_loop_nested_p (stmt_loop, level))
948 : 685754 : return;
949 : :
950 : 455235 : gcc_assert (level == lim_data->max_loop
951 : : || flow_loop_nested_p (lim_data->max_loop, level));
952 : :
953 : 455235 : lim_data->tgt_loop = level;
954 : 785895 : FOR_EACH_VEC_ELT (lim_data->depends, i, dep_stmt)
955 : 330660 : set_level (dep_stmt, orig_loop, level);
956 : : }
957 : :
958 : : /* Determines an outermost loop from that we want to hoist the statement STMT.
959 : : For now we chose the outermost possible loop. TODO -- use profiling
960 : : information to set it more sanely. */
961 : :
962 : : static void
963 : 292924 : set_profitable_level (gimple *stmt)
964 : : {
965 : 292924 : set_level (stmt, gimple_bb (stmt)->loop_father, get_lim_data (stmt)->max_loop);
966 : 292924 : }
967 : :
968 : : /* Returns true if STMT is a call that has side effects. */
969 : :
970 : : static bool
971 : 127971237 : nonpure_call_p (gimple *stmt)
972 : : {
973 : 0 : if (gimple_code (stmt) != GIMPLE_CALL)
974 : : return false;
975 : :
976 : 5439533 : return gimple_has_side_effects (stmt);
977 : : }
978 : :
979 : : /* Rewrite a/b to a*(1/b). Return the invariant stmt to process. */
980 : :
981 : : static gimple *
982 : 35 : rewrite_reciprocal (gimple_stmt_iterator *bsi)
983 : : {
984 : 35 : gassign *stmt, *stmt1, *stmt2;
985 : 35 : tree name, lhs, type;
986 : 35 : tree real_one;
987 : 35 : gimple_stmt_iterator gsi;
988 : :
989 : 35 : stmt = as_a <gassign *> (gsi_stmt (*bsi));
990 : 35 : lhs = gimple_assign_lhs (stmt);
991 : 35 : type = TREE_TYPE (lhs);
992 : :
993 : 35 : real_one = build_one_cst (type);
994 : :
995 : 35 : name = make_temp_ssa_name (type, NULL, "reciptmp");
996 : 70 : stmt1 = gimple_build_assign (name, RDIV_EXPR, real_one,
997 : : gimple_assign_rhs2 (stmt));
998 : 35 : stmt2 = gimple_build_assign (lhs, MULT_EXPR, name,
999 : : gimple_assign_rhs1 (stmt));
1000 : :
1001 : : /* Replace division stmt with reciprocal and multiply stmts.
1002 : : The multiply stmt is not invariant, so update iterator
1003 : : and avoid rescanning. */
1004 : 35 : gsi = *bsi;
1005 : 35 : gsi_insert_before (bsi, stmt1, GSI_NEW_STMT);
1006 : 35 : gsi_replace (&gsi, stmt2, true);
1007 : :
1008 : : /* Continue processing with invariant reciprocal statement. */
1009 : 35 : return stmt1;
1010 : : }
1011 : :
1012 : : /* Check if the pattern at *BSI is a bittest of the form
1013 : : (A >> B) & 1 != 0 and in this case rewrite it to A & (1 << B) != 0. */
1014 : :
1015 : : static gimple *
1016 : 11096 : rewrite_bittest (gimple_stmt_iterator *bsi)
1017 : : {
1018 : 11096 : gassign *stmt;
1019 : 11096 : gimple *stmt1;
1020 : 11096 : gassign *stmt2;
1021 : 11096 : gimple *use_stmt;
1022 : 11096 : gcond *cond_stmt;
1023 : 11096 : tree lhs, name, t, a, b;
1024 : 11096 : use_operand_p use;
1025 : :
1026 : 11096 : stmt = as_a <gassign *> (gsi_stmt (*bsi));
1027 : 11096 : lhs = gimple_assign_lhs (stmt);
1028 : :
1029 : : /* Verify that the single use of lhs is a comparison against zero. */
1030 : 11096 : if (TREE_CODE (lhs) != SSA_NAME
1031 : 11096 : || !single_imm_use (lhs, &use, &use_stmt))
1032 : 302 : return stmt;
1033 : 16568 : cond_stmt = dyn_cast <gcond *> (use_stmt);
1034 : 5524 : if (!cond_stmt)
1035 : : return stmt;
1036 : 5524 : if (gimple_cond_lhs (cond_stmt) != lhs
1037 : 5372 : || (gimple_cond_code (cond_stmt) != NE_EXPR
1038 : 2323 : && gimple_cond_code (cond_stmt) != EQ_EXPR)
1039 : 10896 : || !integer_zerop (gimple_cond_rhs (cond_stmt)))
1040 : 182 : return stmt;
1041 : :
1042 : : /* Get at the operands of the shift. The rhs is TMP1 & 1. */
1043 : 5342 : stmt1 = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
1044 : 5342 : if (gimple_code (stmt1) != GIMPLE_ASSIGN)
1045 : : return stmt;
1046 : :
1047 : : /* There is a conversion in between possibly inserted by fold. */
1048 : 5271 : if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt1)))
1049 : : {
1050 : 249 : t = gimple_assign_rhs1 (stmt1);
1051 : 249 : if (TREE_CODE (t) != SSA_NAME
1052 : 249 : || !has_single_use (t))
1053 : : return stmt;
1054 : 173 : stmt1 = SSA_NAME_DEF_STMT (t);
1055 : 173 : if (gimple_code (stmt1) != GIMPLE_ASSIGN)
1056 : : return stmt;
1057 : : }
1058 : :
1059 : : /* Verify that B is loop invariant but A is not. Verify that with
1060 : : all the stmt walking we are still in the same loop. */
1061 : 5153 : if (gimple_assign_rhs_code (stmt1) != RSHIFT_EXPR
1062 : 11571 : || loop_containing_stmt (stmt1) != loop_containing_stmt (stmt))
1063 : : return stmt;
1064 : :
1065 : 3209 : a = gimple_assign_rhs1 (stmt1);
1066 : 3209 : b = gimple_assign_rhs2 (stmt1);
1067 : :
1068 : 3209 : if (outermost_invariant_loop (b, loop_containing_stmt (stmt1)) != NULL
1069 : 3261 : && outermost_invariant_loop (a, loop_containing_stmt (stmt1)) == NULL)
1070 : : {
1071 : 52 : gimple_stmt_iterator rsi;
1072 : :
1073 : : /* 1 << B */
1074 : 52 : t = fold_build2 (LSHIFT_EXPR, TREE_TYPE (a),
1075 : : build_int_cst (TREE_TYPE (a), 1), b);
1076 : 52 : name = make_temp_ssa_name (TREE_TYPE (a), NULL, "shifttmp");
1077 : 52 : stmt1 = gimple_build_assign (name, t);
1078 : :
1079 : : /* A & (1 << B) */
1080 : 52 : t = fold_build2 (BIT_AND_EXPR, TREE_TYPE (a), a, name);
1081 : 52 : name = make_temp_ssa_name (TREE_TYPE (a), NULL, "shifttmp");
1082 : 52 : stmt2 = gimple_build_assign (name, t);
1083 : :
1084 : : /* Replace the SSA_NAME we compare against zero. Adjust
1085 : : the type of zero accordingly. */
1086 : 52 : SET_USE (use, name);
1087 : 52 : gimple_cond_set_rhs (cond_stmt,
1088 : 52 : build_int_cst_type (TREE_TYPE (name),
1089 : : 0));
1090 : :
1091 : : /* Don't use gsi_replace here, none of the new assignments sets
1092 : : the variable originally set in stmt. Move bsi to stmt1, and
1093 : : then remove the original stmt, so that we get a chance to
1094 : : retain debug info for it. */
1095 : 52 : rsi = *bsi;
1096 : 52 : gsi_insert_before (bsi, stmt1, GSI_NEW_STMT);
1097 : 52 : gsi_insert_before (&rsi, stmt2, GSI_SAME_STMT);
1098 : 52 : gimple *to_release = gsi_stmt (rsi);
1099 : 52 : gsi_remove (&rsi, true);
1100 : 52 : release_defs (to_release);
1101 : :
1102 : 52 : return stmt1;
1103 : : }
1104 : :
1105 : : return stmt;
1106 : : }
1107 : :
1108 : : /* Determine the outermost loops in that statements in basic block BB are
1109 : : invariant, and record them to the LIM_DATA associated with the
1110 : : statements. */
1111 : :
1112 : : static void
1113 : 15058916 : compute_invariantness (basic_block bb)
1114 : : {
1115 : 15058916 : enum move_pos pos;
1116 : 15058916 : gimple_stmt_iterator bsi;
1117 : 15058916 : gimple *stmt;
1118 : 15058916 : bool maybe_never = ALWAYS_EXECUTED_IN (bb) == NULL;
1119 : 15058916 : class loop *outermost = ALWAYS_EXECUTED_IN (bb);
1120 : 15058916 : struct lim_aux_data *lim_data;
1121 : :
1122 : 15058916 : if (!loop_outer (bb->loop_father))
1123 : 8851087 : return;
1124 : :
1125 : 6207829 : if (dump_file && (dump_flags & TDF_DETAILS))
1126 : 350 : fprintf (dump_file, "Basic block %d (loop %d -- depth %d):\n\n",
1127 : 350 : bb->index, bb->loop_father->num, loop_depth (bb->loop_father));
1128 : :
1129 : : /* Look at PHI nodes, but only if there is at most two.
1130 : : ??? We could relax this further by post-processing the inserted
1131 : : code and transforming adjacent cond-exprs with the same predicate
1132 : : to control flow again. */
1133 : 6207829 : bsi = gsi_start_phis (bb);
1134 : 6207829 : if (!gsi_end_p (bsi)
1135 : 6207829 : && ((gsi_next (&bsi), gsi_end_p (bsi))
1136 : 1385150 : || (gsi_next (&bsi), gsi_end_p (bsi))))
1137 : 4428389 : for (bsi = gsi_start_phis (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1138 : : {
1139 : 2655234 : stmt = gsi_stmt (bsi);
1140 : :
1141 : 2655234 : pos = movement_possibility (stmt);
1142 : 2655234 : if (pos == MOVE_IMPOSSIBLE)
1143 : 1182456 : continue;
1144 : :
1145 : 1472778 : lim_data = get_lim_data (stmt);
1146 : 1472778 : if (! lim_data)
1147 : 1472778 : lim_data = init_lim_data (stmt);
1148 : 1472778 : lim_data->always_executed_in = outermost;
1149 : :
1150 : 1472778 : if (!determine_max_movement (stmt, false))
1151 : : {
1152 : 1471679 : lim_data->max_loop = NULL;
1153 : 1471679 : continue;
1154 : : }
1155 : :
1156 : 1099 : if (dump_file && (dump_flags & TDF_DETAILS))
1157 : : {
1158 : 2 : print_gimple_stmt (dump_file, stmt, 2);
1159 : 2 : fprintf (dump_file, " invariant up to level %d, cost %d.\n\n",
1160 : 2 : loop_depth (lim_data->max_loop),
1161 : : lim_data->cost);
1162 : : }
1163 : :
1164 : 1099 : if (lim_data->cost >= LIM_EXPENSIVE)
1165 : 901 : set_profitable_level (stmt);
1166 : : }
1167 : :
1168 : 56607886 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1169 : : {
1170 : 44192228 : stmt = gsi_stmt (bsi);
1171 : :
1172 : 44192228 : pos = movement_possibility (stmt);
1173 : 44192228 : if (pos == MOVE_IMPOSSIBLE)
1174 : : {
1175 : 33348556 : if (nonpure_call_p (stmt))
1176 : : {
1177 : : maybe_never = true;
1178 : : outermost = NULL;
1179 : : }
1180 : : /* Make sure to note always_executed_in for stores to make
1181 : : store-motion work. */
1182 : 30962365 : else if (stmt_makes_single_store (stmt))
1183 : : {
1184 : 2593079 : struct lim_aux_data *lim_data = get_lim_data (stmt);
1185 : 2593079 : if (! lim_data)
1186 : 0 : lim_data = init_lim_data (stmt);
1187 : 2593079 : lim_data->always_executed_in = outermost;
1188 : : }
1189 : 32153991 : continue;
1190 : 32153991 : }
1191 : :
1192 : 12038237 : if (is_gimple_assign (stmt)
1193 : 12038237 : && (get_gimple_rhs_class (gimple_assign_rhs_code (stmt))
1194 : : == GIMPLE_BINARY_RHS))
1195 : : {
1196 : 5750996 : tree op0 = gimple_assign_rhs1 (stmt);
1197 : 5750996 : tree op1 = gimple_assign_rhs2 (stmt);
1198 : 11501992 : class loop *ol1 = outermost_invariant_loop (op1,
1199 : : loop_containing_stmt (stmt));
1200 : :
1201 : : /* If divisor is invariant, convert a/b to a*(1/b), allowing reciprocal
1202 : : to be hoisted out of loop, saving expensive divide. */
1203 : 5750996 : if (pos == MOVE_POSSIBLE
1204 : 5177895 : && gimple_assign_rhs_code (stmt) == RDIV_EXPR
1205 : 549 : && flag_unsafe_math_optimizations
1206 : 367 : && !flag_trapping_math
1207 : 367 : && ol1 != NULL
1208 : 5751034 : && outermost_invariant_loop (op0, ol1) == NULL)
1209 : 35 : stmt = rewrite_reciprocal (&bsi);
1210 : :
1211 : : /* If the shift count is invariant, convert (A >> B) & 1 to
1212 : : A & (1 << B) allowing the bit mask to be hoisted out of the loop
1213 : : saving an expensive shift. */
1214 : 5750996 : if (pos == MOVE_POSSIBLE
1215 : 5177895 : && gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
1216 : 209668 : && integer_onep (op1)
1217 : 19862 : && TREE_CODE (op0) == SSA_NAME
1218 : 5770858 : && has_single_use (op0))
1219 : 11096 : stmt = rewrite_bittest (&bsi);
1220 : : }
1221 : :
1222 : 12038237 : lim_data = get_lim_data (stmt);
1223 : 12038237 : if (! lim_data)
1224 : 9112872 : lim_data = init_lim_data (stmt);
1225 : 12038237 : lim_data->always_executed_in = outermost;
1226 : :
1227 : 12038237 : if (maybe_never && pos == MOVE_PRESERVE_EXECUTION)
1228 : 1251446 : continue;
1229 : :
1230 : 10786791 : if (!determine_max_movement (stmt, pos == MOVE_PRESERVE_EXECUTION))
1231 : : {
1232 : 10024181 : lim_data->max_loop = NULL;
1233 : 10024181 : continue;
1234 : : }
1235 : :
1236 : 762610 : if (dump_file && (dump_flags & TDF_DETAILS))
1237 : : {
1238 : 266 : print_gimple_stmt (dump_file, stmt, 2);
1239 : 266 : fprintf (dump_file, " invariant up to level %d, cost %d.\n\n",
1240 : 266 : loop_depth (lim_data->max_loop),
1241 : : lim_data->cost);
1242 : : }
1243 : :
1244 : 762610 : if (lim_data->cost >= LIM_EXPENSIVE)
1245 : 292023 : set_profitable_level (stmt);
1246 : : /* When we run before PRE and PRE is active hoist all expressions
1247 : : to the always executed loop since PRE would do so anyway
1248 : : and we can preserve range info while PRE cannot. */
1249 : 470587 : else if (flag_tree_pre && !in_loop_pipeline
1250 : 100962 : && outermost)
1251 : : {
1252 : 59299 : class loop *mloop = lim_data->max_loop;
1253 : 177897 : if (loop_depth (outermost) > loop_depth (mloop))
1254 : : {
1255 : 8440 : mloop = outermost;
1256 : 8440 : if (dump_file && (dump_flags & TDF_DETAILS))
1257 : 1 : fprintf (dump_file, " constraining to loop depth %d\n\n\n",
1258 : : loop_depth (mloop));
1259 : : }
1260 : 59299 : set_level (stmt, bb->loop_father, mloop);
1261 : : }
1262 : : }
1263 : : }
1264 : :
1265 : : /* Hoist the statements in basic block BB out of the loops prescribed by
1266 : : data stored in LIM_DATA structures associated with each statement. Callback
1267 : : for walk_dominator_tree. */
1268 : :
1269 : : unsigned int
1270 : 15105303 : move_computations_worker (basic_block bb)
1271 : : {
1272 : 15105303 : class loop *level;
1273 : 15105303 : unsigned cost = 0;
1274 : 15105303 : struct lim_aux_data *lim_data;
1275 : 15105303 : unsigned int todo = 0;
1276 : :
1277 : 15105303 : if (!loop_outer (bb->loop_father))
1278 : : return todo;
1279 : :
1280 : 10883184 : for (gphi_iterator bsi = gsi_start_phis (bb); !gsi_end_p (bsi); )
1281 : : {
1282 : 4669135 : gassign *new_stmt;
1283 : 4669135 : gphi *stmt = bsi.phi ();
1284 : :
1285 : 4669135 : lim_data = get_lim_data (stmt);
1286 : 4669135 : if (lim_data == NULL)
1287 : : {
1288 : 3196357 : gsi_next (&bsi);
1289 : 3196357 : continue;
1290 : : }
1291 : :
1292 : 1472778 : cost = lim_data->cost;
1293 : 1472778 : level = lim_data->tgt_loop;
1294 : 1472778 : clear_lim_data (stmt);
1295 : :
1296 : 1472778 : if (!level)
1297 : : {
1298 : 1471873 : gsi_next (&bsi);
1299 : 1471873 : continue;
1300 : : }
1301 : :
1302 : 905 : if (dump_file && (dump_flags & TDF_DETAILS))
1303 : : {
1304 : 2 : fprintf (dump_file, "Moving PHI node\n");
1305 : 2 : print_gimple_stmt (dump_file, stmt, 0);
1306 : 2 : fprintf (dump_file, "(cost %u) out of loop %d.\n\n",
1307 : : cost, level->num);
1308 : : }
1309 : :
1310 : 905 : if (gimple_phi_num_args (stmt) == 1)
1311 : : {
1312 : 10 : tree arg = PHI_ARG_DEF (stmt, 0);
1313 : 10 : new_stmt = gimple_build_assign (gimple_phi_result (stmt),
1314 : 10 : TREE_CODE (arg), arg);
1315 : : }
1316 : : else
1317 : : {
1318 : 895 : basic_block dom = get_immediate_dominator (CDI_DOMINATORS, bb);
1319 : 895 : gimple *cond = gsi_stmt (gsi_last_bb (dom));
1320 : 895 : tree arg0 = NULL_TREE, arg1 = NULL_TREE, t;
1321 : : /* Get the PHI arguments corresponding to the true and false
1322 : : edges of COND. */
1323 : 895 : extract_true_false_args_from_phi (dom, stmt, &arg0, &arg1);
1324 : 895 : gcc_assert (arg0 && arg1);
1325 : : /* For `bool_val != 0`, reuse bool_val. */
1326 : 895 : if (gimple_cond_code (cond) == NE_EXPR
1327 : 524 : && integer_zerop (gimple_cond_rhs (cond))
1328 : 1349 : && types_compatible_p (TREE_TYPE (gimple_cond_lhs (cond)),
1329 : : boolean_type_node))
1330 : : {
1331 : 55 : t = gimple_cond_lhs (cond);
1332 : : }
1333 : : else
1334 : : {
1335 : 840 : t = make_ssa_name (boolean_type_node);
1336 : 840 : new_stmt = gimple_build_assign (t, gimple_cond_code (cond),
1337 : : gimple_cond_lhs (cond),
1338 : : gimple_cond_rhs (cond));
1339 : 840 : gsi_insert_on_edge (loop_preheader_edge (level), new_stmt);
1340 : : }
1341 : 895 : new_stmt = gimple_build_assign (gimple_phi_result (stmt),
1342 : : COND_EXPR, t, arg0, arg1);
1343 : 895 : todo |= TODO_cleanup_cfg;
1344 : : }
1345 : 905 : if (!ALWAYS_EXECUTED_IN (bb)
1346 : 905 : || (ALWAYS_EXECUTED_IN (bb) != level
1347 : 96 : && !flow_loop_nested_p (ALWAYS_EXECUTED_IN (bb), level)))
1348 : 445 : reset_flow_sensitive_info (gimple_assign_lhs (new_stmt));
1349 : 905 : gsi_insert_on_edge (loop_preheader_edge (level), new_stmt);
1350 : 905 : remove_phi_node (&bsi, false);
1351 : : }
1352 : :
1353 : 56685722 : for (gimple_stmt_iterator bsi = gsi_start_bb (bb); !gsi_end_p (bsi); )
1354 : : {
1355 : 44257624 : edge e;
1356 : :
1357 : 44257624 : gimple *stmt = gsi_stmt (bsi);
1358 : :
1359 : 44257624 : lim_data = get_lim_data (stmt);
1360 : 44257624 : if (lim_data == NULL)
1361 : : {
1362 : 27956689 : gsi_next (&bsi);
1363 : 27956689 : continue;
1364 : : }
1365 : :
1366 : 16300935 : cost = lim_data->cost;
1367 : 16300935 : level = lim_data->tgt_loop;
1368 : 16300935 : clear_lim_data (stmt);
1369 : :
1370 : 16300935 : if (!level)
1371 : : {
1372 : 15807321 : gsi_next (&bsi);
1373 : 15807321 : continue;
1374 : : }
1375 : :
1376 : : /* We do not really want to move conditionals out of the loop; we just
1377 : : placed it here to force its operands to be moved if necessary. */
1378 : 493614 : if (gimple_code (stmt) == GIMPLE_COND)
1379 : : {
1380 : 11427 : gsi_next (&bsi);
1381 : 11427 : continue;
1382 : : }
1383 : :
1384 : 482187 : if (dump_file && (dump_flags & TDF_DETAILS))
1385 : : {
1386 : 336 : fprintf (dump_file, "Moving statement\n");
1387 : 336 : print_gimple_stmt (dump_file, stmt, 0);
1388 : 336 : fprintf (dump_file, "(cost %u) out of loop %d.\n\n",
1389 : : cost, level->num);
1390 : : }
1391 : :
1392 : 482187 : e = loop_preheader_edge (level);
1393 : 964374 : gcc_assert (!gimple_vdef (stmt));
1394 : 964374 : if (gimple_vuse (stmt))
1395 : : {
1396 : : /* The new VUSE is the one from the virtual PHI in the loop
1397 : : header or the one already present. */
1398 : 83122 : gphi_iterator gsi2;
1399 : 83122 : for (gsi2 = gsi_start_phis (e->dest);
1400 : 194616 : !gsi_end_p (gsi2); gsi_next (&gsi2))
1401 : : {
1402 : 177013 : gphi *phi = gsi2.phi ();
1403 : 354026 : if (virtual_operand_p (gimple_phi_result (phi)))
1404 : : {
1405 : 65519 : SET_USE (gimple_vuse_op (stmt),
1406 : : PHI_ARG_DEF_FROM_EDGE (phi, e));
1407 : 65519 : break;
1408 : : }
1409 : : }
1410 : : }
1411 : 482187 : gsi_remove (&bsi, false);
1412 : 482187 : if (gimple_has_lhs (stmt)
1413 : 482187 : && TREE_CODE (gimple_get_lhs (stmt)) == SSA_NAME
1414 : 440464 : && (!ALWAYS_EXECUTED_IN (bb)
1415 : 372304 : || !(ALWAYS_EXECUTED_IN (bb) == level
1416 : 97759 : || flow_loop_nested_p (ALWAYS_EXECUTED_IN (bb), level))))
1417 : 206308 : reset_flow_sensitive_info (gimple_get_lhs (stmt));
1418 : : /* In case this is a stmt that is not unconditionally executed
1419 : : when the target loop header is executed and the stmt may
1420 : : invoke undefined integer or pointer overflow rewrite it to
1421 : : unsigned arithmetic. */
1422 : 482187 : if (gimple_needing_rewrite_undefined (stmt)
1423 : 482187 : && (!ALWAYS_EXECUTED_IN (bb)
1424 : 79003 : || !(ALWAYS_EXECUTED_IN (bb) == level
1425 : 18619 : || flow_loop_nested_p (ALWAYS_EXECUTED_IN (bb), level))))
1426 : 19881 : gsi_insert_seq_on_edge (e, rewrite_to_defined_unconditional (stmt));
1427 : : else
1428 : 462306 : gsi_insert_on_edge (e, stmt);
1429 : : }
1430 : :
1431 : 6214049 : return todo;
1432 : : }
1433 : :
1434 : : /* Checks whether the statement defining variable *INDEX can be hoisted
1435 : : out of the loop passed in DATA. Callback for for_each_index. */
1436 : :
1437 : : static bool
1438 : 1667639 : may_move_till (tree ref, tree *index, void *data)
1439 : : {
1440 : 1667639 : class loop *loop = (class loop *) data, *max_loop;
1441 : :
1442 : : /* If REF is an array reference, check also that the step and the lower
1443 : : bound is invariant in LOOP. */
1444 : 1667639 : if (TREE_CODE (ref) == ARRAY_REF)
1445 : : {
1446 : 442940 : tree step = TREE_OPERAND (ref, 3);
1447 : 442940 : tree lbound = TREE_OPERAND (ref, 2);
1448 : :
1449 : 442940 : max_loop = outermost_invariant_loop (step, loop);
1450 : 442940 : if (!max_loop)
1451 : : return false;
1452 : :
1453 : 442940 : max_loop = outermost_invariant_loop (lbound, loop);
1454 : 442940 : if (!max_loop)
1455 : : return false;
1456 : : }
1457 : :
1458 : 1667639 : max_loop = outermost_invariant_loop (*index, loop);
1459 : 1667639 : if (!max_loop)
1460 : : return false;
1461 : :
1462 : : return true;
1463 : : }
1464 : :
1465 : : /* If OP is SSA NAME, force the statement that defines it to be
1466 : : moved out of the LOOP. ORIG_LOOP is the loop in that EXPR is used. */
1467 : :
1468 : : static void
1469 : 37956 : force_move_till_op (tree op, class loop *orig_loop, class loop *loop)
1470 : : {
1471 : 37956 : gimple *stmt;
1472 : :
1473 : 37956 : if (!op
1474 : 37956 : || is_gimple_min_invariant (op))
1475 : 33406 : return;
1476 : :
1477 : 4550 : gcc_assert (TREE_CODE (op) == SSA_NAME);
1478 : :
1479 : 4550 : stmt = SSA_NAME_DEF_STMT (op);
1480 : 4550 : if (gimple_nop_p (stmt))
1481 : : return;
1482 : :
1483 : 2871 : set_level (stmt, orig_loop, loop);
1484 : : }
1485 : :
1486 : : /* Forces statement defining invariants in REF (and *INDEX) to be moved out of
1487 : : the LOOP. The reference REF is used in the loop ORIG_LOOP. Callback for
1488 : : for_each_index. */
1489 : :
1490 : : struct fmt_data
1491 : : {
1492 : : class loop *loop;
1493 : : class loop *orig_loop;
1494 : : };
1495 : :
1496 : : static bool
1497 : 20074 : force_move_till (tree ref, tree *index, void *data)
1498 : : {
1499 : 20074 : struct fmt_data *fmt_data = (struct fmt_data *) data;
1500 : :
1501 : 20074 : if (TREE_CODE (ref) == ARRAY_REF)
1502 : : {
1503 : 8941 : tree step = TREE_OPERAND (ref, 3);
1504 : 8941 : tree lbound = TREE_OPERAND (ref, 2);
1505 : :
1506 : 8941 : force_move_till_op (step, fmt_data->orig_loop, fmt_data->loop);
1507 : 8941 : force_move_till_op (lbound, fmt_data->orig_loop, fmt_data->loop);
1508 : : }
1509 : :
1510 : 20074 : force_move_till_op (*index, fmt_data->orig_loop, fmt_data->loop);
1511 : :
1512 : 20074 : return true;
1513 : : }
1514 : :
1515 : : /* A function to free the mem_ref object OBJ. */
1516 : :
1517 : : static void
1518 : 5373827 : memref_free (class im_mem_ref *mem)
1519 : : {
1520 : 0 : mem->accesses_in_loop.release ();
1521 : 0 : }
1522 : :
1523 : : /* Allocates and returns a memory reference description for MEM whose hash
1524 : : value is HASH and id is ID. */
1525 : :
1526 : : static im_mem_ref *
1527 : 5373827 : mem_ref_alloc (ao_ref *mem, unsigned hash, unsigned id)
1528 : : {
1529 : 5373827 : im_mem_ref *ref = XOBNEW (&mem_ref_obstack, class im_mem_ref);
1530 : 5373827 : if (mem)
1531 : 4356997 : ref->mem = *mem;
1532 : : else
1533 : 1016830 : ao_ref_init (&ref->mem, error_mark_node);
1534 : 5373827 : ref->id = id;
1535 : 5373827 : ref->ref_canonical = false;
1536 : 5373827 : ref->ref_decomposed = false;
1537 : 5373827 : ref->hash = hash;
1538 : 5373827 : ref->stored = NULL;
1539 : 5373827 : ref->loaded = NULL;
1540 : 5373827 : bitmap_initialize (&ref->dep_loop, &lim_bitmap_obstack);
1541 : 5373827 : ref->accesses_in_loop.create (1);
1542 : :
1543 : 5373827 : return ref;
1544 : : }
1545 : :
1546 : : /* Records memory reference location *LOC in LOOP to the memory reference
1547 : : description REF. The reference occurs in statement STMT. */
1548 : :
1549 : : static void
1550 : 5881944 : record_mem_ref_loc (im_mem_ref *ref, gimple *stmt, tree *loc)
1551 : : {
1552 : 5881944 : mem_ref_loc aref;
1553 : 5881944 : aref.stmt = stmt;
1554 : 5881944 : aref.ref = loc;
1555 : 0 : ref->accesses_in_loop.safe_push (aref);
1556 : 0 : }
1557 : :
1558 : : /* Set the LOOP bit in REF stored bitmap and allocate that if
1559 : : necessary. Return whether a bit was changed. */
1560 : :
1561 : : static bool
1562 : 4533837 : set_ref_stored_in_loop (im_mem_ref *ref, class loop *loop)
1563 : : {
1564 : 4533837 : if (!ref->stored)
1565 : 2607924 : ref->stored = BITMAP_ALLOC (&lim_bitmap_obstack);
1566 : 4533837 : return bitmap_set_bit (ref->stored, loop->num);
1567 : : }
1568 : :
1569 : : /* Marks reference REF as stored in LOOP. */
1570 : :
1571 : : static void
1572 : 3789740 : mark_ref_stored (im_mem_ref *ref, class loop *loop)
1573 : : {
1574 : 3789740 : while (loop != current_loops->tree_root
1575 : 7311296 : && set_ref_stored_in_loop (ref, loop))
1576 : 3521556 : loop = loop_outer (loop);
1577 : 3789740 : }
1578 : :
1579 : : /* Set the LOOP bit in REF loaded bitmap and allocate that if
1580 : : necessary. Return whether a bit was changed. */
1581 : :
1582 : : static bool
1583 : 6354792 : set_ref_loaded_in_loop (im_mem_ref *ref, class loop *loop)
1584 : : {
1585 : 6354792 : if (!ref->loaded)
1586 : 3515385 : ref->loaded = BITMAP_ALLOC (&lim_bitmap_obstack);
1587 : 6354792 : return bitmap_set_bit (ref->loaded, loop->num);
1588 : : }
1589 : :
1590 : : /* Marks reference REF as loaded in LOOP. */
1591 : :
1592 : : static void
1593 : 5072118 : mark_ref_loaded (im_mem_ref *ref, class loop *loop)
1594 : : {
1595 : 5072118 : while (loop != current_loops->tree_root
1596 : 10094741 : && set_ref_loaded_in_loop (ref, loop))
1597 : 5022623 : loop = loop_outer (loop);
1598 : 5072118 : }
1599 : :
1600 : : /* Gathers memory references in statement STMT in LOOP, storing the
1601 : : information about them in the memory_accesses structure. Marks
1602 : : the vops accessed through unrecognized statements there as
1603 : : well. */
1604 : :
1605 : : static void
1606 : 44192141 : gather_mem_refs_stmt (class loop *loop, gimple *stmt)
1607 : : {
1608 : 44192141 : tree *mem = NULL;
1609 : 44192141 : hashval_t hash;
1610 : 44192141 : im_mem_ref **slot;
1611 : 44192141 : im_mem_ref *ref;
1612 : 44192141 : bool is_stored;
1613 : 44192141 : unsigned id;
1614 : :
1615 : 60139141 : if (!gimple_vuse (stmt))
1616 : : return;
1617 : :
1618 : 7145817 : mem = simple_mem_ref_in_stmt (stmt, &is_stored);
1619 : 7145817 : if (!mem && is_gimple_assign (stmt))
1620 : : {
1621 : : /* For aggregate copies record distinct references but use them
1622 : : only for disambiguation purposes. */
1623 : 531936 : id = memory_accesses.refs_list.length ();
1624 : 531936 : ref = mem_ref_alloc (NULL, 0, id);
1625 : 531936 : memory_accesses.refs_list.safe_push (ref);
1626 : 531936 : if (dump_file && (dump_flags & TDF_DETAILS))
1627 : : {
1628 : 24 : fprintf (dump_file, "Unhandled memory reference %u: ", id);
1629 : 24 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1630 : : }
1631 : 531936 : record_mem_ref_loc (ref, stmt, mem);
1632 : 1063872 : is_stored = gimple_vdef (stmt);
1633 : : }
1634 : 6613881 : else if (!mem)
1635 : : {
1636 : : /* We use the shared mem_ref for all unanalyzable refs. */
1637 : 1263873 : id = UNANALYZABLE_MEM_ID;
1638 : 1263873 : ref = memory_accesses.refs_list[id];
1639 : 1263873 : if (dump_file && (dump_flags & TDF_DETAILS))
1640 : : {
1641 : 9 : fprintf (dump_file, "Unanalyzed memory reference %u: ", id);
1642 : 9 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1643 : : }
1644 : 2527746 : is_stored = gimple_vdef (stmt);
1645 : : }
1646 : : else
1647 : : {
1648 : : /* We are looking for equal refs that might differ in structure
1649 : : such as a.b vs. MEM[&a + 4]. So we key off the ao_ref but
1650 : : make sure we can canonicalize the ref in the hashtable if
1651 : : non-operand_equal_p refs are found. For the lookup we mark
1652 : : the case we want strict equality with aor.max_size == -1. */
1653 : 5350008 : ao_ref aor;
1654 : 5350008 : ao_ref_init (&aor, *mem);
1655 : 5350008 : ao_ref_base (&aor);
1656 : 5350008 : ao_ref_alias_set (&aor);
1657 : 5350008 : HOST_WIDE_INT offset, size, max_size;
1658 : 5350008 : poly_int64 saved_maxsize = aor.max_size, mem_off;
1659 : 5350008 : tree mem_base;
1660 : 5350008 : bool ref_decomposed;
1661 : 5350008 : if (aor.max_size_known_p ()
1662 : 5178753 : && aor.offset.is_constant (&offset)
1663 : 5178753 : && aor.size.is_constant (&size)
1664 : 5178753 : && aor.max_size.is_constant (&max_size)
1665 : 5178753 : && size == max_size
1666 : 4572683 : && (size % BITS_PER_UNIT) == 0
1667 : : /* We're canonicalizing to a MEM where TYPE_SIZE specifies the
1668 : : size. Make sure this is consistent with the extraction. */
1669 : 4560134 : && poly_int_tree_p (TYPE_SIZE (TREE_TYPE (*mem)))
1670 : 4560134 : && known_eq (wi::to_poly_offset (TYPE_SIZE (TREE_TYPE (*mem))),
1671 : : aor.size)
1672 : 9910050 : && (mem_base = get_addr_base_and_unit_offset (aor.ref, &mem_off)))
1673 : : {
1674 : 4558999 : ref_decomposed = true;
1675 : 4558999 : tree base = ao_ref_base (&aor);
1676 : 4558999 : poly_int64 moffset;
1677 : 4558999 : HOST_WIDE_INT mcoffset;
1678 : 4558999 : if (TREE_CODE (base) == MEM_REF
1679 : 2035556 : && (mem_ref_offset (base) * BITS_PER_UNIT + offset).to_shwi (&moffset)
1680 : 4558999 : && moffset.is_constant (&mcoffset))
1681 : : {
1682 : 2035556 : hash = iterative_hash_expr (TREE_OPERAND (base, 0), 0);
1683 : 2035556 : hash = iterative_hash_host_wide_int (mcoffset, hash);
1684 : : }
1685 : : else
1686 : : {
1687 : 2523443 : hash = iterative_hash_expr (base, 0);
1688 : 2523443 : hash = iterative_hash_host_wide_int (offset, hash);
1689 : : }
1690 : 4558999 : hash = iterative_hash_host_wide_int (size, hash);
1691 : : }
1692 : : else
1693 : : {
1694 : 791009 : ref_decomposed = false;
1695 : 791009 : hash = iterative_hash_expr (aor.ref, 0);
1696 : 791009 : aor.max_size = -1;
1697 : : }
1698 : 5350008 : slot = memory_accesses.refs->find_slot_with_hash (&aor, hash, INSERT);
1699 : 5350008 : aor.max_size = saved_maxsize;
1700 : 5350008 : if (*slot)
1701 : : {
1702 : 993011 : if (!(*slot)->ref_canonical
1703 : 993011 : && !operand_equal_p (*mem, (*slot)->mem.ref, 0))
1704 : : {
1705 : : /* If we didn't yet canonicalize the hashtable ref (which
1706 : : we'll end up using for code insertion) and hit a second
1707 : : equal ref that is not structurally equivalent create
1708 : : a canonical ref which is a bare MEM_REF. */
1709 : 91785 : if (TREE_CODE (*mem) == MEM_REF
1710 : 91785 : || TREE_CODE (*mem) == TARGET_MEM_REF)
1711 : : {
1712 : 29153 : (*slot)->mem.ref = *mem;
1713 : 29153 : (*slot)->mem.base_alias_set = ao_ref_base_alias_set (&aor);
1714 : : }
1715 : : else
1716 : : {
1717 : 62632 : tree ref_alias_type = reference_alias_ptr_type (*mem);
1718 : 62632 : unsigned int ref_align = get_object_alignment (*mem);
1719 : 62632 : tree ref_type = TREE_TYPE (*mem);
1720 : 62632 : tree tmp = build1 (ADDR_EXPR, ptr_type_node,
1721 : : unshare_expr (mem_base));
1722 : 62632 : if (TYPE_ALIGN (ref_type) != ref_align)
1723 : 23220 : ref_type = build_aligned_type (ref_type, ref_align);
1724 : 62632 : tree new_ref
1725 : 62632 : = fold_build2 (MEM_REF, ref_type, tmp,
1726 : : build_int_cst (ref_alias_type, mem_off));
1727 : 62632 : if ((*slot)->mem.volatile_p)
1728 : 14 : TREE_THIS_VOLATILE (new_ref) = 1;
1729 : 62632 : (*slot)->mem.ref = new_ref;
1730 : : /* Make sure the recorded base and offset are consistent
1731 : : with the newly built ref. */
1732 : 62632 : if (TREE_CODE (TREE_OPERAND (new_ref, 0)) == ADDR_EXPR)
1733 : : ;
1734 : : else
1735 : : {
1736 : 29438 : (*slot)->mem.base = new_ref;
1737 : 29438 : (*slot)->mem.offset = 0;
1738 : : }
1739 : 62632 : gcc_checking_assert (TREE_CODE ((*slot)->mem.ref) == MEM_REF
1740 : : && is_gimple_mem_ref_addr
1741 : : (TREE_OPERAND ((*slot)->mem.ref,
1742 : : 0)));
1743 : 62632 : (*slot)->mem.base_alias_set = (*slot)->mem.ref_alias_set;
1744 : : }
1745 : 91785 : (*slot)->ref_canonical = true;
1746 : : }
1747 : 993011 : ref = *slot;
1748 : 993011 : id = ref->id;
1749 : : }
1750 : : else
1751 : : {
1752 : 4356997 : id = memory_accesses.refs_list.length ();
1753 : 4356997 : ref = mem_ref_alloc (&aor, hash, id);
1754 : 4356997 : ref->ref_decomposed = ref_decomposed;
1755 : 4356997 : memory_accesses.refs_list.safe_push (ref);
1756 : 4356997 : *slot = ref;
1757 : :
1758 : 4356997 : if (dump_file && (dump_flags & TDF_DETAILS))
1759 : : {
1760 : 390 : fprintf (dump_file, "Memory reference %u: ", id);
1761 : 390 : print_generic_expr (dump_file, ref->mem.ref, TDF_SLIM);
1762 : 390 : fprintf (dump_file, "\n");
1763 : : }
1764 : : }
1765 : :
1766 : 5350008 : record_mem_ref_loc (ref, stmt, mem);
1767 : : }
1768 : 7145817 : if (is_stored)
1769 : : {
1770 : 3789740 : bitmap_set_bit (&memory_accesses.refs_stored_in_loop[loop->num], ref->id);
1771 : 3789740 : mark_ref_stored (ref, loop);
1772 : : }
1773 : : /* A not simple memory op is also a read when it is a write. */
1774 : 7145817 : if (!is_stored || id == UNANALYZABLE_MEM_ID
1775 : 2605635 : || ref->mem.ref == error_mark_node)
1776 : : {
1777 : 5072118 : bitmap_set_bit (&memory_accesses.refs_loaded_in_loop[loop->num], ref->id);
1778 : 5072118 : mark_ref_loaded (ref, loop);
1779 : : }
1780 : 7145817 : init_lim_data (stmt)->ref = ref->id;
1781 : 7145817 : return;
1782 : : }
1783 : :
1784 : : static unsigned *bb_loop_postorder;
1785 : :
1786 : : /* qsort sort function to sort blocks after their loop fathers postorder. */
1787 : :
1788 : : static int
1789 : 163666461 : sort_bbs_in_loop_postorder_cmp (const void *bb1_, const void *bb2_,
1790 : : void *bb_loop_postorder_)
1791 : : {
1792 : 163666461 : unsigned *bb_loop_postorder = (unsigned *)bb_loop_postorder_;
1793 : 163666461 : basic_block bb1 = *(const basic_block *)bb1_;
1794 : 163666461 : basic_block bb2 = *(const basic_block *)bb2_;
1795 : 163666461 : class loop *loop1 = bb1->loop_father;
1796 : 163666461 : class loop *loop2 = bb2->loop_father;
1797 : 163666461 : if (loop1->num == loop2->num)
1798 : 78607961 : return bb1->index - bb2->index;
1799 : 85058500 : return bb_loop_postorder[loop1->num] < bb_loop_postorder[loop2->num] ? -1 : 1;
1800 : : }
1801 : :
1802 : : /* qsort sort function to sort ref locs after their loop fathers postorder. */
1803 : :
1804 : : static int
1805 : 992996 : sort_locs_in_loop_postorder_cmp (const void *loc1_, const void *loc2_,
1806 : : void *bb_loop_postorder_)
1807 : : {
1808 : 992996 : unsigned *bb_loop_postorder = (unsigned *)bb_loop_postorder_;
1809 : 992996 : const mem_ref_loc *loc1 = (const mem_ref_loc *)loc1_;
1810 : 992996 : const mem_ref_loc *loc2 = (const mem_ref_loc *)loc2_;
1811 : 992996 : class loop *loop1 = gimple_bb (loc1->stmt)->loop_father;
1812 : 992996 : class loop *loop2 = gimple_bb (loc2->stmt)->loop_father;
1813 : 992996 : if (loop1->num == loop2->num)
1814 : : return 0;
1815 : 147212 : return bb_loop_postorder[loop1->num] < bb_loop_postorder[loop2->num] ? -1 : 1;
1816 : : }
1817 : :
1818 : : /* Gathers memory references in loops. */
1819 : :
1820 : : static void
1821 : 484894 : analyze_memory_references (bool store_motion)
1822 : : {
1823 : 484894 : gimple_stmt_iterator bsi;
1824 : 484894 : basic_block bb, *bbs;
1825 : 484894 : class loop *outer;
1826 : 484894 : unsigned i, n;
1827 : :
1828 : : /* Collect all basic-blocks in loops and sort them after their
1829 : : loops postorder. */
1830 : 484894 : i = 0;
1831 : 484894 : bbs = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
1832 : 15543810 : FOR_EACH_BB_FN (bb, cfun)
1833 : 15058916 : if (bb->loop_father != current_loops->tree_root)
1834 : 6207829 : bbs[i++] = bb;
1835 : 484894 : n = i;
1836 : 484894 : gcc_sort_r (bbs, n, sizeof (basic_block), sort_bbs_in_loop_postorder_cmp,
1837 : : bb_loop_postorder);
1838 : :
1839 : : /* Visit blocks in loop postorder and assign mem-ref IDs in that order.
1840 : : That results in better locality for all the bitmaps. It also
1841 : : automatically sorts the location list of gathered memory references
1842 : : after their loop postorder number allowing to binary-search it. */
1843 : 6692723 : for (i = 0; i < n; ++i)
1844 : : {
1845 : 6207829 : basic_block bb = bbs[i];
1846 : 56607799 : for (bsi = gsi_start_bb (bb); !gsi_end_p (bsi); gsi_next (&bsi))
1847 : 44192141 : gather_mem_refs_stmt (bb->loop_father, gsi_stmt (bsi));
1848 : : }
1849 : :
1850 : : /* Verify the list of gathered memory references is sorted after their
1851 : : loop postorder number. */
1852 : 484894 : if (flag_checking)
1853 : : {
1854 : : im_mem_ref *ref;
1855 : 11232476 : FOR_EACH_VEC_ELT (memory_accesses.refs_list, i, ref)
1856 : 6366793 : for (unsigned j = 1; j < ref->accesses_in_loop.length (); ++j)
1857 : 992996 : gcc_assert (sort_locs_in_loop_postorder_cmp
1858 : : (&ref->accesses_in_loop[j-1], &ref->accesses_in_loop[j],
1859 : : bb_loop_postorder) <= 0);
1860 : : }
1861 : :
1862 : 484894 : free (bbs);
1863 : :
1864 : 484894 : if (!store_motion)
1865 : 484894 : return;
1866 : :
1867 : : /* Propagate the information about accessed memory references up
1868 : : the loop hierarchy. */
1869 : 2784748 : for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))
1870 : : {
1871 : : /* Finalize the overall touched references (including subloops). */
1872 : 1330249 : bitmap_ior_into (&memory_accesses.all_refs_stored_in_loop[loop->num],
1873 : 1330249 : &memory_accesses.refs_stored_in_loop[loop->num]);
1874 : :
1875 : : /* Propagate the information about accessed memory references up
1876 : : the loop hierarchy. */
1877 : 1330249 : outer = loop_outer (loop);
1878 : 1330249 : if (outer == current_loops->tree_root)
1879 : 1022363 : continue;
1880 : :
1881 : 307886 : bitmap_ior_into (&memory_accesses.all_refs_stored_in_loop[outer->num],
1882 : 307886 : &memory_accesses.all_refs_stored_in_loop[loop->num]);
1883 : 484833 : }
1884 : : }
1885 : :
1886 : : /* Returns true if MEM1 and MEM2 may alias. TTAE_CACHE is used as a cache in
1887 : : tree_to_aff_combination_expand. */
1888 : :
1889 : : static bool
1890 : 552567 : mem_refs_may_alias_p (im_mem_ref *mem1, im_mem_ref *mem2,
1891 : : hash_map<tree, name_expansion *> **ttae_cache,
1892 : : bool tbaa_p)
1893 : : {
1894 : 552567 : gcc_checking_assert (mem1->mem.ref != error_mark_node
1895 : : && mem2->mem.ref != error_mark_node);
1896 : :
1897 : : /* Perform BASE + OFFSET analysis -- if MEM1 and MEM2 are based on the same
1898 : : object and their offset differ in such a way that the locations cannot
1899 : : overlap, then they cannot alias. */
1900 : : poly_widest_int size1, size2;
1901 : 1105134 : aff_tree off1, off2;
1902 : :
1903 : : /* Perform basic offset and type-based disambiguation. */
1904 : 552567 : if (!refs_may_alias_p_1 (&mem1->mem, &mem2->mem, tbaa_p))
1905 : : return false;
1906 : :
1907 : : /* The expansion of addresses may be a bit expensive, thus we only do
1908 : : the check at -O2 and higher optimization levels. */
1909 : 68420 : if (optimize < 2)
1910 : : return true;
1911 : :
1912 : 58959 : get_inner_reference_aff (mem1->mem.ref, &off1, &size1);
1913 : 58959 : get_inner_reference_aff (mem2->mem.ref, &off2, &size2);
1914 : 58959 : aff_combination_expand (&off1, ttae_cache);
1915 : 58959 : aff_combination_expand (&off2, ttae_cache);
1916 : 58959 : aff_combination_scale (&off1, -1);
1917 : 58959 : aff_combination_add (&off2, &off1);
1918 : :
1919 : 58959 : if (aff_comb_cannot_overlap_p (&off2, size1, size2))
1920 : : return false;
1921 : :
1922 : : return true;
1923 : 552567 : }
1924 : :
1925 : : /* Compare function for bsearch searching for reference locations
1926 : : in a loop. */
1927 : :
1928 : : static int
1929 : 262822 : find_ref_loc_in_loop_cmp (const void *loop_, const void *loc_,
1930 : : void *bb_loop_postorder_)
1931 : : {
1932 : 262822 : unsigned *bb_loop_postorder = (unsigned *)bb_loop_postorder_;
1933 : 262822 : class loop *loop = (class loop *)const_cast<void *>(loop_);
1934 : 262822 : mem_ref_loc *loc = (mem_ref_loc *)const_cast<void *>(loc_);
1935 : 262822 : class loop *loc_loop = gimple_bb (loc->stmt)->loop_father;
1936 : 262822 : if (loop->num == loc_loop->num
1937 : 262822 : || flow_loop_nested_p (loop, loc_loop))
1938 : 201736 : return 0;
1939 : 61086 : return (bb_loop_postorder[loop->num] < bb_loop_postorder[loc_loop->num]
1940 : 61086 : ? -1 : 1);
1941 : : }
1942 : :
1943 : : /* Iterates over all locations of REF in LOOP and its subloops calling
1944 : : fn.operator() with the location as argument. When that operator
1945 : : returns true the iteration is stopped and true is returned.
1946 : : Otherwise false is returned. */
1947 : :
1948 : : template <typename FN>
1949 : : static bool
1950 : 201736 : for_all_locs_in_loop (class loop *loop, im_mem_ref *ref, FN fn)
1951 : : {
1952 : : unsigned i;
1953 : : mem_ref_loc *loc;
1954 : :
1955 : : /* Search for the cluster of locs in the accesses_in_loop vector
1956 : : which is sorted after postorder index of the loop father. */
1957 : 201736 : loc = ref->accesses_in_loop.bsearch (loop, find_ref_loc_in_loop_cmp,
1958 : : bb_loop_postorder);
1959 : 201736 : if (!loc)
1960 : 0 : return false;
1961 : :
1962 : : /* We have found one location inside loop or its sub-loops. Iterate
1963 : : both forward and backward to cover the whole cluster. */
1964 : 201736 : i = loc - ref->accesses_in_loop.address ();
1965 : 278238 : while (i > 0)
1966 : : {
1967 : 135410 : --i;
1968 : 135410 : mem_ref_loc *l = &ref->accesses_in_loop[i];
1969 : 135410 : if (!flow_bb_inside_loop_p (loop, gimple_bb (l->stmt)))
1970 : : break;
1971 : 111493 : if (fn (l))
1972 : : return true;
1973 : : }
1974 : 456461 : for (i = loc - ref->accesses_in_loop.address ();
1975 : 289716 : i < ref->accesses_in_loop.length (); ++i)
1976 : : {
1977 : 216209 : mem_ref_loc *l = &ref->accesses_in_loop[i];
1978 : 216209 : if (!flow_bb_inside_loop_p (loop, gimple_bb (l->stmt)))
1979 : : break;
1980 : 196295 : if (fn (l))
1981 : : return true;
1982 : : }
1983 : :
1984 : : return false;
1985 : : }
1986 : :
1987 : : /* Rewrites location LOC by TMP_VAR. */
1988 : :
1989 : : class rewrite_mem_ref_loc
1990 : : {
1991 : : public:
1992 : 34437 : rewrite_mem_ref_loc (tree tmp_var_) : tmp_var (tmp_var_) {}
1993 : : bool operator () (mem_ref_loc *loc);
1994 : : tree tmp_var;
1995 : : };
1996 : :
1997 : : bool
1998 : 55007 : rewrite_mem_ref_loc::operator () (mem_ref_loc *loc)
1999 : : {
2000 : 55007 : *loc->ref = tmp_var;
2001 : 55007 : update_stmt (loc->stmt);
2002 : 55007 : return false;
2003 : : }
2004 : :
2005 : : /* Rewrites all references to REF in LOOP by variable TMP_VAR. */
2006 : :
2007 : : static void
2008 : 34437 : rewrite_mem_refs (class loop *loop, im_mem_ref *ref, tree tmp_var)
2009 : : {
2010 : 0 : for_all_locs_in_loop (loop, ref, rewrite_mem_ref_loc (tmp_var));
2011 : 0 : }
2012 : :
2013 : : /* Stores the first reference location in LOCP. */
2014 : :
2015 : : class first_mem_ref_loc_1
2016 : : {
2017 : : public:
2018 : 34437 : first_mem_ref_loc_1 (mem_ref_loc **locp_) : locp (locp_) {}
2019 : : bool operator () (mem_ref_loc *loc);
2020 : : mem_ref_loc **locp;
2021 : : };
2022 : :
2023 : : bool
2024 : 34437 : first_mem_ref_loc_1::operator () (mem_ref_loc *loc)
2025 : : {
2026 : 34437 : *locp = loc;
2027 : 34437 : return true;
2028 : : }
2029 : :
2030 : : /* Returns the first reference location to REF in LOOP. */
2031 : :
2032 : : static mem_ref_loc *
2033 : 34437 : first_mem_ref_loc (class loop *loop, im_mem_ref *ref)
2034 : : {
2035 : 34437 : mem_ref_loc *locp = NULL;
2036 : 0 : for_all_locs_in_loop (loop, ref, first_mem_ref_loc_1 (&locp));
2037 : 34437 : return locp;
2038 : : }
2039 : :
2040 : : /* Helper function for execute_sm. Emit code to store TMP_VAR into
2041 : : MEM along edge EX.
2042 : :
2043 : : The store is only done if MEM has changed. We do this so no
2044 : : changes to MEM occur on code paths that did not originally store
2045 : : into it.
2046 : :
2047 : : The common case for execute_sm will transform:
2048 : :
2049 : : for (...) {
2050 : : if (foo)
2051 : : stuff;
2052 : : else
2053 : : MEM = TMP_VAR;
2054 : : }
2055 : :
2056 : : into:
2057 : :
2058 : : lsm = MEM;
2059 : : for (...) {
2060 : : if (foo)
2061 : : stuff;
2062 : : else
2063 : : lsm = TMP_VAR;
2064 : : }
2065 : : MEM = lsm;
2066 : :
2067 : : This function will generate:
2068 : :
2069 : : lsm = MEM;
2070 : :
2071 : : lsm_flag = false;
2072 : : ...
2073 : : for (...) {
2074 : : if (foo)
2075 : : stuff;
2076 : : else {
2077 : : lsm = TMP_VAR;
2078 : : lsm_flag = true;
2079 : : }
2080 : : }
2081 : : if (lsm_flag) <--
2082 : : MEM = lsm; <-- (X)
2083 : :
2084 : : In case MEM and TMP_VAR are NULL the function will return the then
2085 : : block so the caller can insert (X) and other related stmts.
2086 : : */
2087 : :
2088 : : static basic_block
2089 : 15122 : execute_sm_if_changed (edge ex, tree mem, tree tmp_var, tree flag,
2090 : : edge preheader, hash_set <basic_block> *flag_bbs,
2091 : : edge &append_cond_position, edge &last_cond_fallthru)
2092 : : {
2093 : 15122 : basic_block new_bb, then_bb, old_dest;
2094 : 15122 : bool loop_has_only_one_exit;
2095 : 15122 : edge then_old_edge;
2096 : 15122 : gimple_stmt_iterator gsi;
2097 : 15122 : gimple *stmt;
2098 : 15122 : bool irr = ex->flags & EDGE_IRREDUCIBLE_LOOP;
2099 : :
2100 : 15122 : profile_count count_sum = profile_count::zero ();
2101 : 15122 : int nbbs = 0, ncount = 0;
2102 : 15122 : profile_probability flag_probability = profile_probability::uninitialized ();
2103 : :
2104 : : /* Flag is set in FLAG_BBS. Determine probability that flag will be true
2105 : : at loop exit.
2106 : :
2107 : : This code may look fancy, but it cannot update profile very realistically
2108 : : because we do not know the probability that flag will be true at given
2109 : : loop exit.
2110 : :
2111 : : We look for two interesting extremes
2112 : : - when exit is dominated by block setting the flag, we know it will
2113 : : always be true. This is a common case.
2114 : : - when all blocks setting the flag have very low frequency we know
2115 : : it will likely be false.
2116 : : In all other cases we default to 2/3 for flag being true. */
2117 : :
2118 : 15122 : for (hash_set<basic_block>::iterator it = flag_bbs->begin ();
2119 : 50254 : it != flag_bbs->end (); ++it)
2120 : : {
2121 : 17566 : if ((*it)->count.initialized_p ())
2122 : 17553 : count_sum += (*it)->count, ncount ++;
2123 : 17566 : if (dominated_by_p (CDI_DOMINATORS, ex->src, *it))
2124 : 2258 : flag_probability = profile_probability::always ();
2125 : 17566 : nbbs++;
2126 : : }
2127 : :
2128 : 15122 : profile_probability cap
2129 : 15122 : = profile_probability::guessed_always ().apply_scale (2, 3);
2130 : :
2131 : 15122 : if (flag_probability.initialized_p ())
2132 : : ;
2133 : 12907 : else if (ncount == nbbs
2134 : 26176 : && preheader->count () >= count_sum && preheader->count ().nonzero_p ())
2135 : : {
2136 : 191 : flag_probability = count_sum.probability_in (preheader->count ());
2137 : 191 : if (flag_probability > cap)
2138 : 64 : flag_probability = cap;
2139 : : }
2140 : :
2141 : 15122 : if (!flag_probability.initialized_p ())
2142 : 12716 : flag_probability = cap;
2143 : :
2144 : : /* ?? Insert store after previous store if applicable. See note
2145 : : below. */
2146 : 15122 : if (append_cond_position)
2147 : 6068 : ex = append_cond_position;
2148 : :
2149 : 15122 : loop_has_only_one_exit = single_pred_p (ex->dest);
2150 : :
2151 : 15122 : if (loop_has_only_one_exit)
2152 : 6298 : ex = split_block_after_labels (ex->dest);
2153 : : else
2154 : : {
2155 : 8824 : for (gphi_iterator gpi = gsi_start_phis (ex->dest);
2156 : 12530 : !gsi_end_p (gpi); gsi_next (&gpi))
2157 : : {
2158 : 4425 : gphi *phi = gpi.phi ();
2159 : 8850 : if (virtual_operand_p (gimple_phi_result (phi)))
2160 : 3706 : continue;
2161 : :
2162 : : /* When the destination has a non-virtual PHI node with multiple
2163 : : predecessors make sure we preserve the PHI structure by
2164 : : forcing a forwarder block so that hoisting of that PHI will
2165 : : still work. */
2166 : 719 : split_edge (ex);
2167 : 719 : break;
2168 : : }
2169 : : }
2170 : :
2171 : 15122 : old_dest = ex->dest;
2172 : 15122 : new_bb = split_edge (ex);
2173 : 15122 : if (append_cond_position)
2174 : 6068 : new_bb->count += last_cond_fallthru->count ();
2175 : 15122 : then_bb = create_empty_bb (new_bb);
2176 : 15122 : then_bb->count = new_bb->count.apply_probability (flag_probability);
2177 : 15122 : if (irr)
2178 : 49 : then_bb->flags = BB_IRREDUCIBLE_LOOP;
2179 : 15122 : add_bb_to_loop (then_bb, new_bb->loop_father);
2180 : :
2181 : 15122 : gsi = gsi_start_bb (new_bb);
2182 : 15122 : stmt = gimple_build_cond (NE_EXPR, flag, boolean_false_node,
2183 : : NULL_TREE, NULL_TREE);
2184 : 15122 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
2185 : :
2186 : : /* Insert actual store. */
2187 : 15122 : if (mem)
2188 : : {
2189 : 12731 : gsi = gsi_start_bb (then_bb);
2190 : 12731 : stmt = gimple_build_assign (unshare_expr (mem), tmp_var);
2191 : 12731 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
2192 : : }
2193 : :
2194 : 15122 : edge e1 = single_succ_edge (new_bb);
2195 : 30195 : edge e2 = make_edge (new_bb, then_bb,
2196 : : EDGE_TRUE_VALUE | (irr ? EDGE_IRREDUCIBLE_LOOP : 0));
2197 : 15122 : e2->probability = flag_probability;
2198 : :
2199 : 15122 : e1->flags |= EDGE_FALSE_VALUE | (irr ? EDGE_IRREDUCIBLE_LOOP : 0);
2200 : 15122 : e1->flags &= ~EDGE_FALLTHRU;
2201 : :
2202 : 15122 : e1->probability = flag_probability.invert ();
2203 : :
2204 : 30195 : then_old_edge = make_single_succ_edge (then_bb, old_dest,
2205 : : EDGE_FALLTHRU | (irr ? EDGE_IRREDUCIBLE_LOOP : 0));
2206 : :
2207 : 15122 : set_immediate_dominator (CDI_DOMINATORS, then_bb, new_bb);
2208 : :
2209 : 15122 : if (append_cond_position)
2210 : : {
2211 : 6068 : basic_block prevbb = last_cond_fallthru->src;
2212 : 6068 : redirect_edge_succ (last_cond_fallthru, new_bb);
2213 : 6068 : set_immediate_dominator (CDI_DOMINATORS, new_bb, prevbb);
2214 : 6068 : set_immediate_dominator (CDI_DOMINATORS, old_dest,
2215 : : recompute_dominator (CDI_DOMINATORS, old_dest));
2216 : : }
2217 : :
2218 : : /* ?? Because stores may alias, they must happen in the exact
2219 : : sequence they originally happened. Save the position right after
2220 : : the (_lsm) store we just created so we can continue appending after
2221 : : it and maintain the original order. */
2222 : 15122 : append_cond_position = then_old_edge;
2223 : 15122 : last_cond_fallthru = find_edge (new_bb, old_dest);
2224 : :
2225 : 15122 : if (!loop_has_only_one_exit)
2226 : 8824 : for (gphi_iterator gpi = gsi_start_phis (old_dest);
2227 : 12430 : !gsi_end_p (gpi); gsi_next (&gpi))
2228 : : {
2229 : 3606 : gphi *phi = gpi.phi ();
2230 : 3606 : unsigned i;
2231 : :
2232 : 20118 : for (i = 0; i < gimple_phi_num_args (phi); i++)
2233 : 16512 : if (gimple_phi_arg_edge (phi, i)->src == new_bb)
2234 : : {
2235 : 3606 : tree arg = gimple_phi_arg_def (phi, i);
2236 : 3606 : add_phi_arg (phi, arg, then_old_edge, UNKNOWN_LOCATION);
2237 : 3606 : update_stmt (phi);
2238 : : }
2239 : : }
2240 : :
2241 : 15122 : return then_bb;
2242 : : }
2243 : :
2244 : : /* When REF is set on the location, set flag indicating the store. */
2245 : :
2246 : : class sm_set_flag_if_changed
2247 : : {
2248 : : public:
2249 : 7957 : sm_set_flag_if_changed (tree flag_, hash_set <basic_block> *bbs_)
2250 : 7957 : : flag (flag_), bbs (bbs_) {}
2251 : : bool operator () (mem_ref_loc *loc);
2252 : : tree flag;
2253 : : hash_set <basic_block> *bbs;
2254 : : };
2255 : :
2256 : : bool
2257 : 15150 : sm_set_flag_if_changed::operator () (mem_ref_loc *loc)
2258 : : {
2259 : : /* Only set the flag for writes. */
2260 : 15150 : if (is_gimple_assign (loc->stmt)
2261 : 15150 : && gimple_assign_lhs_ptr (loc->stmt) == loc->ref)
2262 : : {
2263 : 9132 : gimple_stmt_iterator gsi = gsi_for_stmt (loc->stmt);
2264 : 9132 : gimple *stmt = gimple_build_assign (flag, boolean_true_node);
2265 : 9132 : gsi_insert_after (&gsi, stmt, GSI_CONTINUE_LINKING);
2266 : 9132 : bbs->add (gimple_bb (stmt));
2267 : : }
2268 : 15150 : return false;
2269 : : }
2270 : :
2271 : : /* Helper function for execute_sm. On every location where REF is
2272 : : set, set an appropriate flag indicating the store. */
2273 : :
2274 : : static tree
2275 : 7957 : execute_sm_if_changed_flag_set (class loop *loop, im_mem_ref *ref,
2276 : : hash_set <basic_block> *bbs)
2277 : : {
2278 : 7957 : tree flag;
2279 : 7957 : char *str = get_lsm_tmp_name (ref->mem.ref, ~0, "_flag");
2280 : 7957 : flag = create_tmp_reg (boolean_type_node, str);
2281 : 7957 : for_all_locs_in_loop (loop, ref, sm_set_flag_if_changed (flag, bbs));
2282 : 7957 : return flag;
2283 : : }
2284 : :
2285 : 34437 : struct sm_aux
2286 : : {
2287 : : tree tmp_var;
2288 : : tree store_flag;
2289 : : hash_set <basic_block> flag_bbs;
2290 : : };
2291 : :
2292 : : /* Executes store motion of memory reference REF from LOOP.
2293 : : Exits from the LOOP are stored in EXITS. The initialization of the
2294 : : temporary variable is put to the preheader of the loop, and assignments
2295 : : to the reference from the temporary variable are emitted to exits. */
2296 : :
2297 : : static sm_aux *
2298 : 34437 : execute_sm (class loop *loop, im_mem_ref *ref,
2299 : : hash_map<im_mem_ref *, sm_aux *> &aux_map, bool maybe_mt,
2300 : : bool use_other_flag_var)
2301 : : {
2302 : 34437 : gassign *load;
2303 : 34437 : struct fmt_data fmt_data;
2304 : 34437 : struct lim_aux_data *lim_data;
2305 : 34437 : bool multi_threaded_model_p = false;
2306 : 34437 : gimple_stmt_iterator gsi;
2307 : 34437 : sm_aux *aux = new sm_aux;
2308 : :
2309 : 34437 : if (dump_file && (dump_flags & TDF_DETAILS))
2310 : : {
2311 : 82 : fprintf (dump_file, "Executing store motion of ");
2312 : 82 : print_generic_expr (dump_file, ref->mem.ref);
2313 : 82 : fprintf (dump_file, " from loop %d\n", loop->num);
2314 : : }
2315 : :
2316 : 34437 : aux->tmp_var = create_tmp_reg (TREE_TYPE (ref->mem.ref),
2317 : 34437 : get_lsm_tmp_name (ref->mem.ref, ~0));
2318 : :
2319 : 34437 : fmt_data.loop = loop;
2320 : 34437 : fmt_data.orig_loop = loop;
2321 : 34437 : for_each_index (&ref->mem.ref, force_move_till, &fmt_data);
2322 : :
2323 : 68874 : bool always_stored = ref_always_accessed_p (loop, ref, true);
2324 : 34437 : if (maybe_mt
2325 : 11734 : && (bb_in_transaction (loop_preheader_edge (loop)->src)
2326 : 11733 : || (ref_can_have_store_data_races (ref->mem.ref) && ! always_stored)))
2327 : : multi_threaded_model_p = true;
2328 : :
2329 : 34437 : if (multi_threaded_model_p && !use_other_flag_var)
2330 : 7957 : aux->store_flag
2331 : 7957 : = execute_sm_if_changed_flag_set (loop, ref, &aux->flag_bbs);
2332 : : else
2333 : 26480 : aux->store_flag = NULL_TREE;
2334 : :
2335 : : /* Remember variable setup. */
2336 : 34437 : aux_map.put (ref, aux);
2337 : :
2338 : 34437 : rewrite_mem_refs (loop, ref, aux->tmp_var);
2339 : :
2340 : : /* Emit the load code on a random exit edge or into the latch if
2341 : : the loop does not exit, so that we are sure it will be processed
2342 : : by move_computations after all dependencies. */
2343 : 34437 : gsi = gsi_for_stmt (first_mem_ref_loc (loop, ref)->stmt);
2344 : :
2345 : : /* Avoid doing a load if there was no load of the ref in the loop.
2346 : : Esp. when the ref is not always stored we cannot optimize it
2347 : : away later. But when it is not always stored we must use a conditional
2348 : : store then. */
2349 : 34437 : if ((!always_stored && !multi_threaded_model_p)
2350 : 34437 : || (ref->loaded && bitmap_bit_p (ref->loaded, loop->num)))
2351 : 17969 : load = gimple_build_assign (aux->tmp_var, unshare_expr (ref->mem.ref));
2352 : : else
2353 : : {
2354 : : /* If not emitting a load mark the uninitialized state on the
2355 : : loop entry as not to be warned for. */
2356 : 16468 : tree uninit = create_tmp_reg (TREE_TYPE (aux->tmp_var));
2357 : 16468 : suppress_warning (uninit, OPT_Wuninitialized);
2358 : 16468 : uninit = get_or_create_ssa_default_def (cfun, uninit);
2359 : 16468 : load = gimple_build_assign (aux->tmp_var, uninit);
2360 : : }
2361 : 34437 : lim_data = init_lim_data (load);
2362 : 34437 : lim_data->max_loop = loop;
2363 : 34437 : lim_data->tgt_loop = loop;
2364 : 34437 : gsi_insert_before (&gsi, load, GSI_SAME_STMT);
2365 : :
2366 : 34437 : if (aux->store_flag)
2367 : : {
2368 : 7957 : load = gimple_build_assign (aux->store_flag, boolean_false_node);
2369 : 7957 : lim_data = init_lim_data (load);
2370 : 7957 : lim_data->max_loop = loop;
2371 : 7957 : lim_data->tgt_loop = loop;
2372 : 7957 : gsi_insert_before (&gsi, load, GSI_SAME_STMT);
2373 : : }
2374 : :
2375 : 34437 : return aux;
2376 : : }
2377 : :
2378 : : /* sm_ord is used for ordinary stores we can retain order with respect
2379 : : to other stores
2380 : : sm_unord is used for conditional executed stores which need to be
2381 : : able to execute in arbitrary order with respect to other stores
2382 : : sm_other is used for stores we do not try to apply store motion to. */
2383 : : enum sm_kind { sm_ord, sm_unord, sm_other };
2384 : : struct seq_entry
2385 : : {
2386 : : seq_entry () = default;
2387 : 52516 : seq_entry (unsigned f, sm_kind k, tree fr = NULL)
2388 : 52516 : : first (f), second (k), from (fr) {}
2389 : : unsigned first;
2390 : : sm_kind second;
2391 : : tree from;
2392 : : };
2393 : :
2394 : : static void
2395 : 33206 : execute_sm_exit (class loop *loop, edge ex, vec<seq_entry> &seq,
2396 : : hash_map<im_mem_ref *, sm_aux *> &aux_map, sm_kind kind,
2397 : : edge &append_cond_position, edge &last_cond_fallthru,
2398 : : bitmap clobbers_to_prune)
2399 : : {
2400 : : /* Sink the stores to exit from the loop. */
2401 : 116103 : for (unsigned i = seq.length (); i > 0; --i)
2402 : : {
2403 : 49691 : im_mem_ref *ref = memory_accesses.refs_list[seq[i-1].first];
2404 : 49691 : if (seq[i-1].second == sm_other)
2405 : : {
2406 : 4978 : gcc_assert (kind == sm_ord && seq[i-1].from != NULL_TREE);
2407 : 4978 : gassign *store;
2408 : 4978 : if (ref->mem.ref == error_mark_node)
2409 : : {
2410 : 148 : tree lhs = gimple_assign_lhs (ref->accesses_in_loop[0].stmt);
2411 : 148 : if (dump_file && (dump_flags & TDF_DETAILS))
2412 : : {
2413 : 3 : fprintf (dump_file, "Re-issueing dependent ");
2414 : 3 : print_generic_expr (dump_file, unshare_expr (seq[i-1].from));
2415 : 3 : fprintf (dump_file, " of ");
2416 : 3 : print_generic_expr (dump_file, lhs);
2417 : 3 : fprintf (dump_file, " from loop %d on exit %d -> %d\n",
2418 : 3 : loop->num, ex->src->index, ex->dest->index);
2419 : : }
2420 : 148 : store = gimple_build_assign (unshare_expr (lhs),
2421 : 148 : unshare_expr (seq[i-1].from));
2422 : 148 : bitmap_set_bit (clobbers_to_prune, seq[i-1].first);
2423 : : }
2424 : : else
2425 : : {
2426 : 4830 : if (dump_file && (dump_flags & TDF_DETAILS))
2427 : : {
2428 : 0 : fprintf (dump_file, "Re-issueing dependent store of ");
2429 : 0 : print_generic_expr (dump_file, ref->mem.ref);
2430 : 0 : fprintf (dump_file, " from loop %d on exit %d -> %d\n",
2431 : 0 : loop->num, ex->src->index, ex->dest->index);
2432 : : }
2433 : 4830 : store = gimple_build_assign (unshare_expr (ref->mem.ref),
2434 : 4830 : seq[i-1].from);
2435 : : }
2436 : 4978 : gsi_insert_on_edge (ex, store);
2437 : : }
2438 : : else
2439 : : {
2440 : 44713 : sm_aux *aux = *aux_map.get (ref);
2441 : 44713 : if (!aux->store_flag || kind == sm_ord)
2442 : : {
2443 : 31982 : gassign *store;
2444 : 31982 : store = gimple_build_assign (unshare_expr (ref->mem.ref),
2445 : : aux->tmp_var);
2446 : 31982 : gsi_insert_on_edge (ex, store);
2447 : 31982 : }
2448 : : else
2449 : 12731 : execute_sm_if_changed (ex, ref->mem.ref, aux->tmp_var,
2450 : : aux->store_flag,
2451 : : loop_preheader_edge (loop), &aux->flag_bbs,
2452 : : append_cond_position, last_cond_fallthru);
2453 : : }
2454 : : }
2455 : 33206 : }
2456 : :
2457 : : /* Push the SM candidate at index PTR in the sequence SEQ down until
2458 : : we hit the next SM candidate. Return true if that went OK and
2459 : : false if we could not disambiguate agains another unrelated ref.
2460 : : Update *AT to the index where the candidate now resides. */
2461 : :
2462 : : static bool
2463 : 36629 : sm_seq_push_down (vec<seq_entry> &seq, unsigned ptr, unsigned *at)
2464 : : {
2465 : 36629 : *at = ptr;
2466 : 37481 : for (; ptr > 0; --ptr)
2467 : : {
2468 : 16990 : seq_entry &new_cand = seq[ptr];
2469 : 16990 : seq_entry &against = seq[ptr-1];
2470 : 16990 : if (against.second == sm_ord
2471 : 5967 : || (against.second == sm_other && against.from != NULL_TREE))
2472 : : /* Found the tail of the sequence. */
2473 : : break;
2474 : : /* We may not ignore self-dependences here. */
2475 : 1195 : if (new_cand.first == against.first
2476 : : /* ??? We could actually handle clobbers here, but not easily
2477 : : with LIMs dependence analysis. */
2478 : 979 : || (memory_accesses.refs_list[new_cand.first]->mem.ref
2479 : 979 : == error_mark_node)
2480 : 974 : || (memory_accesses.refs_list[against.first]->mem.ref
2481 : : == error_mark_node)
2482 : 2097 : || !refs_independent_p (memory_accesses.refs_list[new_cand.first],
2483 : 902 : memory_accesses.refs_list[against.first],
2484 : : false))
2485 : : /* ??? Prune new_cand from the list of refs to apply SM to. */
2486 : 343 : return false;
2487 : 852 : std::swap (new_cand, against);
2488 : 852 : *at = ptr - 1;
2489 : : }
2490 : : return true;
2491 : : }
2492 : :
2493 : : /* Computes the sequence of stores from candidates in REFS_NOT_IN_SEQ to SEQ
2494 : : walking backwards from VDEF (or the end of BB if VDEF is NULL). */
2495 : :
2496 : : static int
2497 : 35640 : sm_seq_valid_bb (class loop *loop, basic_block bb, tree vdef,
2498 : : vec<seq_entry> &seq, bitmap refs_not_in_seq,
2499 : : bitmap refs_not_supported, bool forked,
2500 : : bitmap fully_visited)
2501 : : {
2502 : 35640 : if (!vdef)
2503 : 26373 : for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);
2504 : 116617 : gsi_prev (&gsi))
2505 : : {
2506 : 142919 : vdef = gimple_vdef (gsi_stmt (gsi));
2507 : 52675 : if (vdef)
2508 : : break;
2509 : : }
2510 : 26373 : if (!vdef)
2511 : : {
2512 : 8576 : gphi *vphi = get_virtual_phi (bb);
2513 : 8576 : if (vphi)
2514 : 4344 : vdef = gimple_phi_result (vphi);
2515 : : }
2516 : 35640 : if (!vdef)
2517 : : {
2518 : 4232 : if (single_pred_p (bb))
2519 : : /* This handles the perfect nest case. */
2520 : 3449 : return sm_seq_valid_bb (loop, single_pred (bb), vdef,
2521 : : seq, refs_not_in_seq, refs_not_supported,
2522 : 3449 : forked, fully_visited);
2523 : : return 0;
2524 : : }
2525 : 56051 : do
2526 : : {
2527 : 112102 : gimple *def = SSA_NAME_DEF_STMT (vdef);
2528 : 56051 : if (gimple_bb (def) != bb)
2529 : : {
2530 : : /* If we forked by processing a PHI do not allow our walk to
2531 : : merge again until we handle that robustly. */
2532 : 3199 : if (forked)
2533 : : {
2534 : : /* Mark refs_not_in_seq as unsupported. */
2535 : 1482 : bitmap_ior_into (refs_not_supported, refs_not_in_seq);
2536 : 1482 : return 1;
2537 : : }
2538 : : /* Otherwise it doesn't really matter if we end up in different
2539 : : BBs. */
2540 : : bb = gimple_bb (def);
2541 : : }
2542 : 54569 : if (gphi *phi = dyn_cast <gphi *> (def))
2543 : : {
2544 : : /* Handle CFG merges. Until we handle forks (gimple_bb (def) != bb)
2545 : : this is still linear.
2546 : : Eventually we want to cache intermediate results per BB
2547 : : (but we can't easily cache for different exits?). */
2548 : : /* Stop at PHIs with possible backedges. */
2549 : 11035 : if (bb == bb->loop_father->header
2550 : 5677 : || bb->flags & BB_IRREDUCIBLE_LOOP)
2551 : : {
2552 : : /* Mark refs_not_in_seq as unsupported. */
2553 : 5363 : bitmap_ior_into (refs_not_supported, refs_not_in_seq);
2554 : 5363 : return 1;
2555 : : }
2556 : 5672 : if (gimple_phi_num_args (phi) == 1)
2557 : 1578 : return sm_seq_valid_bb (loop, gimple_phi_arg_edge (phi, 0)->src,
2558 : : gimple_phi_arg_def (phi, 0), seq,
2559 : : refs_not_in_seq, refs_not_supported,
2560 : 1578 : false, fully_visited);
2561 : 8188 : if (bitmap_bit_p (fully_visited,
2562 : 4094 : SSA_NAME_VERSION (gimple_phi_result (phi))))
2563 : : return 1;
2564 : 3990 : auto_vec<seq_entry> first_edge_seq;
2565 : 3990 : auto_bitmap tem_refs_not_in_seq (&lim_bitmap_obstack);
2566 : 3990 : int eret;
2567 : 3990 : bitmap_copy (tem_refs_not_in_seq, refs_not_in_seq);
2568 : 3990 : eret = sm_seq_valid_bb (loop, gimple_phi_arg_edge (phi, 0)->src,
2569 : : gimple_phi_arg_def (phi, 0),
2570 : : first_edge_seq,
2571 : : tem_refs_not_in_seq, refs_not_supported,
2572 : : true, fully_visited);
2573 : 3990 : if (eret != 1)
2574 : : return -1;
2575 : : /* Simplify our lives by pruning the sequence of !sm_ord. */
2576 : 4906 : while (!first_edge_seq.is_empty ()
2577 : 11609 : && first_edge_seq.last ().second != sm_ord)
2578 : 2713 : first_edge_seq.pop ();
2579 : 7689 : for (unsigned int i = 1; i < gimple_phi_num_args (phi); ++i)
2580 : : {
2581 : 5710 : tree vuse = gimple_phi_arg_def (phi, i);
2582 : 5710 : edge e = gimple_phi_arg_edge (phi, i);
2583 : 5710 : auto_vec<seq_entry> edge_seq;
2584 : 5710 : bitmap_and_compl (tem_refs_not_in_seq,
2585 : : refs_not_in_seq, refs_not_supported);
2586 : : /* If we've marked all refs we search for as unsupported
2587 : : we can stop processing and use the sequence as before
2588 : : the PHI. */
2589 : 5710 : if (bitmap_empty_p (tem_refs_not_in_seq))
2590 : : return 1;
2591 : 3699 : eret = sm_seq_valid_bb (loop, e->src, vuse, edge_seq,
2592 : : tem_refs_not_in_seq, refs_not_supported,
2593 : : true, fully_visited);
2594 : 3699 : if (eret != 1)
2595 : : return -1;
2596 : : /* Simplify our lives by pruning the sequence of !sm_ord. */
2597 : 4826 : while (!edge_seq.is_empty ()
2598 : 11297 : && edge_seq.last ().second != sm_ord)
2599 : 2772 : edge_seq.pop ();
2600 : 7398 : unsigned min_len = MIN(first_edge_seq.length (),
2601 : : edge_seq.length ());
2602 : : /* Incrementally merge seqs into first_edge_seq. */
2603 : 3699 : int first_uneq = -1;
2604 : 3699 : auto_vec<seq_entry, 2> extra_refs;
2605 : 5826 : for (unsigned int i = 0; i < min_len; ++i)
2606 : : {
2607 : : /* ??? We can more intelligently merge when we face different
2608 : : order by additional sinking operations in one sequence.
2609 : : For now we simply mark them as to be processed by the
2610 : : not order-preserving SM code. */
2611 : 2127 : if (first_edge_seq[i].first != edge_seq[i].first)
2612 : : {
2613 : 44 : if (first_edge_seq[i].second == sm_ord)
2614 : 26 : bitmap_set_bit (refs_not_supported,
2615 : 26 : first_edge_seq[i].first);
2616 : 44 : if (edge_seq[i].second == sm_ord)
2617 : 29 : bitmap_set_bit (refs_not_supported, edge_seq[i].first);
2618 : 44 : first_edge_seq[i].second = sm_other;
2619 : 44 : first_edge_seq[i].from = NULL_TREE;
2620 : : /* Record the dropped refs for later processing. */
2621 : 44 : if (first_uneq == -1)
2622 : 40 : first_uneq = i;
2623 : 88 : extra_refs.safe_push (seq_entry (edge_seq[i].first,
2624 : 44 : sm_other, NULL_TREE));
2625 : : }
2626 : : /* sm_other prevails. */
2627 : 2083 : else if (first_edge_seq[i].second != edge_seq[i].second)
2628 : : {
2629 : : /* Make sure the ref is marked as not supported. */
2630 : 0 : bitmap_set_bit (refs_not_supported,
2631 : 0 : first_edge_seq[i].first);
2632 : 0 : first_edge_seq[i].second = sm_other;
2633 : 0 : first_edge_seq[i].from = NULL_TREE;
2634 : : }
2635 : 2083 : else if (first_edge_seq[i].second == sm_other
2636 : 21 : && first_edge_seq[i].from != NULL_TREE
2637 : 2104 : && (edge_seq[i].from == NULL_TREE
2638 : 21 : || !operand_equal_p (first_edge_seq[i].from,
2639 : 21 : edge_seq[i].from, 0)))
2640 : 15 : first_edge_seq[i].from = NULL_TREE;
2641 : : }
2642 : : /* Any excess elements become sm_other since they are now
2643 : : coonditionally executed. */
2644 : 10491 : if (first_edge_seq.length () > edge_seq.length ())
2645 : : {
2646 : 5784 : for (unsigned i = edge_seq.length ();
2647 : 7654 : i < first_edge_seq.length (); ++i)
2648 : : {
2649 : 5784 : if (first_edge_seq[i].second == sm_ord)
2650 : 2814 : bitmap_set_bit (refs_not_supported,
2651 : 2814 : first_edge_seq[i].first);
2652 : 5784 : first_edge_seq[i].second = sm_other;
2653 : : }
2654 : : }
2655 : 1829 : else if (edge_seq.length () > first_edge_seq.length ())
2656 : : {
2657 : 12 : if (first_uneq == -1)
2658 : 0 : first_uneq = first_edge_seq.length ();
2659 : 12 : for (unsigned i = first_edge_seq.length ();
2660 : 36 : i < edge_seq.length (); ++i)
2661 : : {
2662 : 24 : if (edge_seq[i].second == sm_ord)
2663 : 12 : bitmap_set_bit (refs_not_supported, edge_seq[i].first);
2664 : 48 : extra_refs.safe_push (seq_entry (edge_seq[i].first,
2665 : 24 : sm_other, NULL_TREE));
2666 : : }
2667 : : }
2668 : : /* Put unmerged refs at first_uneq to force dependence checking
2669 : : on them. */
2670 : 3699 : if (first_uneq != -1)
2671 : : {
2672 : : /* Missing ordered_splice_at. */
2673 : 80 : if ((unsigned)first_uneq == first_edge_seq.length ())
2674 : 0 : first_edge_seq.safe_splice (extra_refs);
2675 : : else
2676 : : {
2677 : 40 : unsigned fes_length = first_edge_seq.length ();
2678 : 40 : first_edge_seq.safe_grow (fes_length
2679 : 40 : + extra_refs.length ());
2680 : 40 : memmove (&first_edge_seq[first_uneq + extra_refs.length ()],
2681 : 40 : &first_edge_seq[first_uneq],
2682 : 40 : (fes_length - first_uneq) * sizeof (seq_entry));
2683 : 80 : memcpy (&first_edge_seq[first_uneq],
2684 : 40 : extra_refs.address (),
2685 : 40 : extra_refs.length () * sizeof (seq_entry));
2686 : : }
2687 : : }
2688 : 5710 : }
2689 : : /* Use the sequence from the first edge and push SMs down. */
2690 : 5766 : for (unsigned i = 0; i < first_edge_seq.length (); ++i)
2691 : : {
2692 : 3787 : unsigned id = first_edge_seq[i].first;
2693 : 3787 : seq.safe_push (first_edge_seq[i]);
2694 : 3787 : unsigned new_idx;
2695 : 3787 : if ((first_edge_seq[i].second == sm_ord
2696 : 3231 : || (first_edge_seq[i].second == sm_other
2697 : 3231 : && first_edge_seq[i].from != NULL_TREE))
2698 : 5012 : && !sm_seq_push_down (seq, seq.length () - 1, &new_idx))
2699 : : {
2700 : 125 : if (first_edge_seq[i].second == sm_ord)
2701 : 0 : bitmap_set_bit (refs_not_supported, id);
2702 : : /* Mark it sm_other. */
2703 : 125 : seq[new_idx].second = sm_other;
2704 : 125 : seq[new_idx].from = NULL_TREE;
2705 : : }
2706 : : }
2707 : 1979 : bitmap_set_bit (fully_visited,
2708 : 1979 : SSA_NAME_VERSION (gimple_phi_result (phi)));
2709 : 1979 : return 1;
2710 : 3990 : }
2711 : 43534 : lim_aux_data *data = get_lim_data (def);
2712 : 43534 : im_mem_ref *ref = memory_accesses.refs_list[data->ref];
2713 : 43534 : if (data->ref == UNANALYZABLE_MEM_ID)
2714 : : return -1;
2715 : : /* Stop at memory references which we can't move. */
2716 : 43534 : else if ((ref->mem.ref == error_mark_node
2717 : : /* We can move end-of-storage/object down. */
2718 : 515 : && !gimple_clobber_p (ref->accesses_in_loop[0].stmt,
2719 : : CLOBBER_STORAGE_END)
2720 : 375 : && !gimple_clobber_p (ref->accesses_in_loop[0].stmt,
2721 : : CLOBBER_OBJECT_END))
2722 : 43892 : || TREE_THIS_VOLATILE (ref->mem.ref))
2723 : : {
2724 : : /* Mark refs_not_in_seq as unsupported. */
2725 : 186 : bitmap_ior_into (refs_not_supported, refs_not_in_seq);
2726 : 186 : return 1;
2727 : : }
2728 : : /* One of the stores we want to apply SM to and we've not yet seen. */
2729 : 43348 : else if (bitmap_clear_bit (refs_not_in_seq, data->ref))
2730 : : {
2731 : 32319 : seq.safe_push (seq_entry (data->ref, sm_ord));
2732 : :
2733 : : /* 1) push it down the queue until a SMed
2734 : : and not ignored ref is reached, skipping all not SMed refs
2735 : : and ignored refs via non-TBAA disambiguation. */
2736 : 32319 : unsigned new_idx;
2737 : 64638 : if (!sm_seq_push_down (seq, seq.length () - 1, &new_idx)
2738 : : /* If that fails but we did not fork yet continue, we'll see
2739 : : to re-materialize all of the stores in the sequence then.
2740 : : Further stores will only be pushed up to this one. */
2741 : 32319 : && forked)
2742 : : {
2743 : 0 : bitmap_set_bit (refs_not_supported, data->ref);
2744 : : /* Mark it sm_other. */
2745 : 0 : seq[new_idx].second = sm_other;
2746 : : }
2747 : :
2748 : : /* 2) check whether we've seen all refs we want to SM and if so
2749 : : declare success for the active exit */
2750 : 32319 : if (bitmap_empty_p (refs_not_in_seq))
2751 : 18705 : return 1;
2752 : : }
2753 : : else
2754 : : /* Another store not part of the final sequence. Simply push it. */
2755 : 11029 : seq.safe_push (seq_entry (data->ref, sm_other,
2756 : 11029 : gimple_assign_rhs1 (def)));
2757 : :
2758 : 80694 : vdef = gimple_vuse (def);
2759 : : }
2760 : : while (1);
2761 : : }
2762 : :
2763 : : /* Hoists memory references MEM_REFS out of LOOP. EXITS is the list of exit
2764 : : edges of the LOOP. */
2765 : :
2766 : : static void
2767 : 21349 : hoist_memory_references (class loop *loop, bitmap mem_refs,
2768 : : const vec<edge> &exits)
2769 : : {
2770 : 21349 : im_mem_ref *ref;
2771 : 21349 : unsigned i;
2772 : 21349 : bitmap_iterator bi;
2773 : :
2774 : : /* There's a special case we can use ordered re-materialization for
2775 : : conditionally excuted stores which is when all stores in the loop
2776 : : happen in the same basic-block. In that case we know we'll reach
2777 : : all stores and thus can simply process that BB and emit a single
2778 : : conditional block of ordered materializations. See PR102436. */
2779 : 21349 : basic_block single_store_bb = NULL;
2780 : 31697 : EXECUTE_IF_SET_IN_BITMAP (&memory_accesses.all_refs_stored_in_loop[loop->num],
2781 : : 0, i, bi)
2782 : : {
2783 : 29472 : bool fail = false;
2784 : 29472 : ref = memory_accesses.refs_list[i];
2785 : 135181 : for (auto loc : ref->accesses_in_loop)
2786 : 145097 : if (!gimple_vdef (loc.stmt))
2787 : : ;
2788 : 32443 : else if (!single_store_bb)
2789 : : {
2790 : 21349 : single_store_bb = gimple_bb (loc.stmt);
2791 : 21349 : bool conditional = false;
2792 : 76782 : for (edge e : exits)
2793 : 23059 : if (!dominated_by_p (CDI_DOMINATORS, e->src, single_store_bb))
2794 : : {
2795 : : /* Conditional as seen from e. */
2796 : : conditional = true;
2797 : : break;
2798 : : }
2799 : 21349 : if (!conditional)
2800 : : {
2801 : : fail = true;
2802 : : break;
2803 : : }
2804 : : }
2805 : 11094 : else if (single_store_bb != gimple_bb (loc.stmt))
2806 : : {
2807 : : fail = true;
2808 : : break;
2809 : : }
2810 : 29472 : if (fail)
2811 : : {
2812 : : single_store_bb = NULL;
2813 : : break;
2814 : : }
2815 : : }
2816 : 21349 : if (single_store_bb)
2817 : : {
2818 : : /* Analyze the single block with stores. */
2819 : 2225 : auto_bitmap fully_visited;
2820 : 2225 : auto_bitmap refs_not_supported;
2821 : 2225 : auto_bitmap refs_not_in_seq;
2822 : 2225 : auto_vec<seq_entry> seq;
2823 : 2225 : bitmap_copy (refs_not_in_seq, mem_refs);
2824 : 2225 : int res = sm_seq_valid_bb (loop, single_store_bb, NULL_TREE,
2825 : : seq, refs_not_in_seq, refs_not_supported,
2826 : : false, fully_visited);
2827 : 2225 : if (res != 1)
2828 : : {
2829 : : /* Unhandled refs can still fail this. */
2830 : 0 : bitmap_clear (mem_refs);
2831 : 0 : return;
2832 : : }
2833 : :
2834 : : /* We cannot handle sm_other since we neither remember the
2835 : : stored location nor the value at the point we execute them. */
2836 : 5453 : for (unsigned i = 0; i < seq.length (); ++i)
2837 : : {
2838 : 3228 : unsigned new_i;
2839 : 3228 : if (seq[i].second == sm_other
2840 : 3228 : && seq[i].from != NULL_TREE)
2841 : 483 : seq[i].from = NULL_TREE;
2842 : 2745 : else if ((seq[i].second == sm_ord
2843 : 0 : || (seq[i].second == sm_other
2844 : 0 : && seq[i].from != NULL_TREE))
2845 : 2745 : && !sm_seq_push_down (seq, i, &new_i))
2846 : : {
2847 : 111 : bitmap_set_bit (refs_not_supported, seq[new_i].first);
2848 : 111 : seq[new_i].second = sm_other;
2849 : 111 : seq[new_i].from = NULL_TREE;
2850 : : }
2851 : : }
2852 : 2225 : bitmap_and_compl_into (mem_refs, refs_not_supported);
2853 : 2225 : if (bitmap_empty_p (mem_refs))
2854 : : return;
2855 : :
2856 : : /* Prune seq. */
2857 : 2526 : while (seq.last ().second == sm_other
2858 : 2526 : && seq.last ().from == NULL_TREE)
2859 : 397 : seq.pop ();
2860 : :
2861 : 2129 : hash_map<im_mem_ref *, sm_aux *> aux_map;
2862 : :
2863 : : /* Execute SM but delay the store materialization for ordered
2864 : : sequences on exit. Remember a created flag var and make
2865 : : sure to re-use it. */
2866 : 2129 : sm_aux *flag_var_aux = nullptr;
2867 : 4763 : EXECUTE_IF_SET_IN_BITMAP (mem_refs, 0, i, bi)
2868 : : {
2869 : 2634 : ref = memory_accesses.refs_list[i];
2870 : 2634 : sm_aux *aux = execute_sm (loop, ref, aux_map, true,
2871 : : flag_var_aux != nullptr);
2872 : 2634 : if (aux->store_flag)
2873 : 1506 : flag_var_aux = aux;
2874 : : }
2875 : :
2876 : : /* Materialize ordered store sequences on exits. */
2877 : 2129 : edge e;
2878 : 2129 : auto_bitmap clobbers_to_prune;
2879 : 5806 : FOR_EACH_VEC_ELT (exits, i, e)
2880 : : {
2881 : 3677 : edge append_cond_position = NULL;
2882 : 3677 : edge last_cond_fallthru = NULL;
2883 : 3677 : edge insert_e = e;
2884 : : /* Construct the single flag variable control flow and insert
2885 : : the ordered seq of stores in the then block. With
2886 : : -fstore-data-races we can do the stores unconditionally. */
2887 : 3677 : if (flag_var_aux)
2888 : 2391 : insert_e
2889 : : = single_pred_edge
2890 : 2391 : (execute_sm_if_changed (e, NULL_TREE, NULL_TREE,
2891 : : flag_var_aux->store_flag,
2892 : : loop_preheader_edge (loop),
2893 : : &flag_var_aux->flag_bbs,
2894 : : append_cond_position,
2895 : : last_cond_fallthru));
2896 : 3677 : execute_sm_exit (loop, insert_e, seq, aux_map, sm_ord,
2897 : : append_cond_position, last_cond_fallthru,
2898 : : clobbers_to_prune);
2899 : 3677 : gsi_commit_one_edge_insert (insert_e, NULL);
2900 : : }
2901 : :
2902 : : /* Remove clobbers inside the loop we re-materialized on exits. */
2903 : 2129 : EXECUTE_IF_SET_IN_BITMAP (clobbers_to_prune, 0, i, bi)
2904 : : {
2905 : 0 : gimple *stmt = memory_accesses.refs_list[i]->accesses_in_loop[0].stmt;
2906 : 0 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
2907 : 0 : unlink_stmt_vdef (stmt);
2908 : 0 : release_defs (stmt);
2909 : 0 : gimple_set_vdef (stmt, NULL_TREE);
2910 : 0 : gsi_remove (&gsi, true);
2911 : : }
2912 : :
2913 : 4763 : for (hash_map<im_mem_ref *, sm_aux *>::iterator iter = aux_map.begin ();
2914 : 7397 : iter != aux_map.end (); ++iter)
2915 : 5268 : delete (*iter).second;
2916 : :
2917 : 2129 : return;
2918 : 4354 : }
2919 : :
2920 : : /* To address PR57359 before actually applying store-motion check
2921 : : the candidates found for validity with regards to reordering
2922 : : relative to other stores which we until here disambiguated using
2923 : : TBAA which isn't valid.
2924 : : What matters is the order of the last stores to the mem_refs
2925 : : with respect to the other stores of the loop at the point of the
2926 : : loop exits. */
2927 : :
2928 : : /* For each exit compute the store order, pruning from mem_refs
2929 : : on the fly. */
2930 : : /* The complexity of this is at least
2931 : : O(number of exits * number of SM refs) but more approaching
2932 : : O(number of exits * number of SM refs * number of stores). */
2933 : : /* ??? Somehow do this in a single sweep over the loop body. */
2934 : 19124 : auto_vec<std::pair<edge, vec<seq_entry> > > sms;
2935 : 19124 : auto_bitmap refs_not_supported (&lim_bitmap_obstack);
2936 : 19124 : edge e;
2937 : 39040 : FOR_EACH_VEC_ELT (exits, i, e)
2938 : : {
2939 : 22330 : vec<seq_entry> seq;
2940 : 22330 : seq.create (4);
2941 : 22330 : auto_bitmap refs_not_in_seq (&lim_bitmap_obstack);
2942 : 22330 : bitmap_and_compl (refs_not_in_seq, mem_refs, refs_not_supported);
2943 : 22330 : if (bitmap_empty_p (refs_not_in_seq))
2944 : : {
2945 : 1631 : seq.release ();
2946 : 1631 : break;
2947 : : }
2948 : 20699 : auto_bitmap fully_visited;
2949 : 20699 : int res = sm_seq_valid_bb (loop, e->src, NULL_TREE,
2950 : : seq, refs_not_in_seq,
2951 : : refs_not_supported, false,
2952 : : fully_visited);
2953 : 20699 : if (res != 1)
2954 : : {
2955 : 783 : bitmap_copy (refs_not_supported, mem_refs);
2956 : 783 : seq.release ();
2957 : 783 : break;
2958 : : }
2959 : 19916 : sms.safe_push (std::make_pair (e, seq));
2960 : 23113 : }
2961 : :
2962 : : /* Prune pruned mem_refs from earlier processed exits. */
2963 : 19124 : bool changed = !bitmap_empty_p (refs_not_supported);
2964 : 19124 : while (changed)
2965 : : {
2966 : 6275 : changed = false;
2967 : 6275 : std::pair<edge, vec<seq_entry> > *seq;
2968 : 40509 : FOR_EACH_VEC_ELT (sms, i, seq)
2969 : : {
2970 : : bool need_to_push = false;
2971 : 13661 : for (unsigned i = 0; i < seq->second.length (); ++i)
2972 : : {
2973 : 6106 : sm_kind kind = seq->second[i].second;
2974 : 6106 : if (kind == sm_other && seq->second[i].from == NULL_TREE)
2975 : : break;
2976 : 5203 : unsigned id = seq->second[i].first;
2977 : 5203 : unsigned new_idx;
2978 : 5203 : if (kind == sm_ord
2979 : 5203 : && bitmap_bit_p (refs_not_supported, id))
2980 : : {
2981 : 2474 : seq->second[i].second = sm_other;
2982 : 2474 : gcc_assert (seq->second[i].from == NULL_TREE);
2983 : : need_to_push = true;
2984 : : }
2985 : 2729 : else if (need_to_push
2986 : 2729 : && !sm_seq_push_down (seq->second, i, &new_idx))
2987 : : {
2988 : : /* We need to push down both sm_ord and sm_other
2989 : : but for the latter we need to disqualify all
2990 : : following refs. */
2991 : 107 : if (kind == sm_ord)
2992 : : {
2993 : 7 : if (bitmap_set_bit (refs_not_supported, id))
2994 : 7 : changed = true;
2995 : 7 : seq->second[new_idx].second = sm_other;
2996 : : }
2997 : : else
2998 : : {
2999 : 379 : for (unsigned j = seq->second.length () - 1;
3000 : 279 : j > new_idx; --j)
3001 : 179 : if (seq->second[j].second == sm_ord
3002 : 285 : && bitmap_set_bit (refs_not_supported,
3003 : 106 : seq->second[j].first))
3004 : : changed = true;
3005 : 100 : seq->second.truncate (new_idx);
3006 : 100 : break;
3007 : : }
3008 : : }
3009 : : }
3010 : : }
3011 : : }
3012 : 19124 : std::pair<edge, vec<seq_entry> > *seq;
3013 : 58956 : FOR_EACH_VEC_ELT (sms, i, seq)
3014 : : {
3015 : : /* Prune sm_other from the end. */
3016 : 62430 : while (!seq->second.is_empty ()
3017 : 42514 : && seq->second.last ().second == sm_other)
3018 : 4616 : seq->second.pop ();
3019 : : /* Prune duplicates from the start. */
3020 : 19916 : auto_bitmap seen (&lim_bitmap_obstack);
3021 : 19916 : unsigned j, k;
3022 : 47049 : for (j = k = 0; j < seq->second.length (); ++j)
3023 : 27133 : if (bitmap_set_bit (seen, seq->second[j].first))
3024 : : {
3025 : 27022 : if (k != j)
3026 : 79 : seq->second[k] = seq->second[j];
3027 : 27022 : ++k;
3028 : : }
3029 : 19916 : seq->second.truncate (k);
3030 : : /* And verify. */
3031 : 19916 : seq_entry *e;
3032 : 86770 : FOR_EACH_VEC_ELT (seq->second, j, e)
3033 : 27022 : gcc_assert (e->second == sm_ord
3034 : : || (e->second == sm_other && e->from != NULL_TREE));
3035 : 19916 : }
3036 : :
3037 : : /* Verify dependence for refs we cannot handle with the order preserving
3038 : : code (refs_not_supported) or prune them from mem_refs. */
3039 : 19124 : auto_vec<seq_entry> unord_refs;
3040 : 35089 : EXECUTE_IF_SET_IN_BITMAP (refs_not_supported, 0, i, bi)
3041 : : {
3042 : 15965 : ref = memory_accesses.refs_list[i];
3043 : 15965 : if (!ref_indep_loop_p (loop, ref, sm_waw))
3044 : 6865 : bitmap_clear_bit (mem_refs, i);
3045 : : /* We've now verified store order for ref with respect to all other
3046 : : stores in the loop does not matter. */
3047 : : else
3048 : 9100 : unord_refs.safe_push (seq_entry (i, sm_unord));
3049 : : }
3050 : :
3051 : 19124 : hash_map<im_mem_ref *, sm_aux *> aux_map;
3052 : :
3053 : : /* Execute SM but delay the store materialization for ordered
3054 : : sequences on exit. */
3055 : 50927 : EXECUTE_IF_SET_IN_BITMAP (mem_refs, 0, i, bi)
3056 : : {
3057 : 31803 : ref = memory_accesses.refs_list[i];
3058 : 31803 : execute_sm (loop, ref, aux_map, bitmap_bit_p (refs_not_supported, i),
3059 : : false);
3060 : : }
3061 : :
3062 : : /* Materialize ordered store sequences on exits. */
3063 : 19124 : auto_bitmap clobbers_to_prune;
3064 : 43235 : FOR_EACH_VEC_ELT (exits, i, e)
3065 : : {
3066 : 24111 : edge append_cond_position = NULL;
3067 : 24111 : edge last_cond_fallthru = NULL;
3068 : 24111 : if (i < sms.length ())
3069 : : {
3070 : 19916 : gcc_assert (sms[i].first == e);
3071 : 19916 : execute_sm_exit (loop, e, sms[i].second, aux_map, sm_ord,
3072 : : append_cond_position, last_cond_fallthru,
3073 : : clobbers_to_prune);
3074 : 19916 : sms[i].second.release ();
3075 : : }
3076 : 24111 : if (!unord_refs.is_empty ())
3077 : 9613 : execute_sm_exit (loop, e, unord_refs, aux_map, sm_unord,
3078 : : append_cond_position, last_cond_fallthru,
3079 : : clobbers_to_prune);
3080 : : /* Commit edge inserts here to preserve the order of stores
3081 : : when an exit exits multiple loops. */
3082 : 24111 : gsi_commit_one_edge_insert (e, NULL);
3083 : : }
3084 : :
3085 : : /* Remove clobbers inside the loop we re-materialized on exits. */
3086 : 19272 : EXECUTE_IF_SET_IN_BITMAP (clobbers_to_prune, 0, i, bi)
3087 : : {
3088 : 148 : gimple *stmt = memory_accesses.refs_list[i]->accesses_in_loop[0].stmt;
3089 : 148 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
3090 : 148 : unlink_stmt_vdef (stmt);
3091 : 148 : release_defs (stmt);
3092 : 148 : gimple_set_vdef (stmt, NULL_TREE);
3093 : 148 : gsi_remove (&gsi, true);
3094 : : }
3095 : :
3096 : 50927 : for (hash_map<im_mem_ref *, sm_aux *>::iterator iter = aux_map.begin ();
3097 : 101854 : iter != aux_map.end (); ++iter)
3098 : 63606 : delete (*iter).second;
3099 : 19124 : }
3100 : :
3101 : : class ref_always_accessed
3102 : : {
3103 : : public:
3104 : 82577 : ref_always_accessed (class loop *loop_, bool stored_p_)
3105 : 82577 : : loop (loop_), stored_p (stored_p_) {}
3106 : : bool operator () (mem_ref_loc *loc);
3107 : : class loop *loop;
3108 : : bool stored_p;
3109 : : };
3110 : :
3111 : : bool
3112 : 160074 : ref_always_accessed::operator () (mem_ref_loc *loc)
3113 : : {
3114 : 160074 : class loop *must_exec;
3115 : :
3116 : 160074 : struct lim_aux_data *lim_data = get_lim_data (loc->stmt);
3117 : 160074 : if (!lim_data)
3118 : : return false;
3119 : :
3120 : : /* If we require an always executed store make sure the statement
3121 : : is a store. */
3122 : 160074 : if (stored_p)
3123 : : {
3124 : 160074 : tree lhs = gimple_get_lhs (loc->stmt);
3125 : 160074 : if (!lhs
3126 : 160074 : || !(DECL_P (lhs) || REFERENCE_CLASS_P (lhs)))
3127 : : return false;
3128 : : }
3129 : :
3130 : 98331 : must_exec = lim_data->always_executed_in;
3131 : 98331 : if (!must_exec)
3132 : : return false;
3133 : :
3134 : 35788 : if (must_exec == loop
3135 : 35788 : || flow_loop_nested_p (must_exec, loop))
3136 : 32461 : return true;
3137 : :
3138 : : return false;
3139 : : }
3140 : :
3141 : : /* Returns true if REF is always accessed in LOOP. If STORED_P is true
3142 : : make sure REF is always stored to in LOOP. */
3143 : :
3144 : : static bool
3145 : 82577 : ref_always_accessed_p (class loop *loop, im_mem_ref *ref, bool stored_p)
3146 : : {
3147 : 34437 : return for_all_locs_in_loop (loop, ref,
3148 : 82577 : ref_always_accessed (loop, stored_p));
3149 : : }
3150 : :
3151 : : /* Returns true if REF1 and REF2 are independent. */
3152 : :
3153 : : static bool
3154 : 615563 : refs_independent_p (im_mem_ref *ref1, im_mem_ref *ref2, bool tbaa_p)
3155 : : {
3156 : 615563 : if (ref1 == ref2)
3157 : : return true;
3158 : :
3159 : 552567 : if (dump_file && (dump_flags & TDF_DETAILS))
3160 : 3031 : fprintf (dump_file, "Querying dependency of refs %u and %u: ",
3161 : 3031 : ref1->id, ref2->id);
3162 : :
3163 : 552567 : if (mem_refs_may_alias_p (ref1, ref2, &memory_accesses.ttae_cache, tbaa_p))
3164 : : {
3165 : 65940 : if (dump_file && (dump_flags & TDF_DETAILS))
3166 : 94 : fprintf (dump_file, "dependent.\n");
3167 : 65940 : return false;
3168 : : }
3169 : : else
3170 : : {
3171 : 486627 : if (dump_file && (dump_flags & TDF_DETAILS))
3172 : 2937 : fprintf (dump_file, "independent.\n");
3173 : 486627 : return true;
3174 : : }
3175 : : }
3176 : :
3177 : : /* Returns true if REF is independent on all other accessess in LOOP.
3178 : : KIND specifies the kind of dependence to consider.
3179 : : lim_raw assumes REF is not stored in LOOP and disambiguates RAW
3180 : : dependences so if true REF can be hoisted out of LOOP
3181 : : sm_war disambiguates a store REF against all other loads to see
3182 : : whether the store can be sunk across loads out of LOOP
3183 : : sm_waw disambiguates a store REF against all other stores to see
3184 : : whether the store can be sunk across stores out of LOOP. */
3185 : :
3186 : : static bool
3187 : 1941801 : ref_indep_loop_p (class loop *loop, im_mem_ref *ref, dep_kind kind)
3188 : : {
3189 : 1941801 : bool indep_p = true;
3190 : 1941801 : bitmap refs_to_check;
3191 : :
3192 : 1941801 : if (kind == sm_war)
3193 : 843787 : refs_to_check = &memory_accesses.refs_loaded_in_loop[loop->num];
3194 : : else
3195 : 1098014 : refs_to_check = &memory_accesses.refs_stored_in_loop[loop->num];
3196 : :
3197 : 1941801 : if (bitmap_bit_p (refs_to_check, UNANALYZABLE_MEM_ID)
3198 : 1941801 : || ref->mem.ref == error_mark_node)
3199 : : indep_p = false;
3200 : : else
3201 : : {
3202 : : /* tri-state, { unknown, independent, dependent } */
3203 : 626165 : dep_state state = query_loop_dependence (loop, ref, kind);
3204 : 626165 : if (state != dep_unknown)
3205 : 0 : return state == dep_independent ? true : false;
3206 : :
3207 : 626165 : class loop *inner = loop->inner;
3208 : 784583 : while (inner)
3209 : : {
3210 : 413428 : if (!ref_indep_loop_p (inner, ref, kind))
3211 : : {
3212 : : indep_p = false;
3213 : : break;
3214 : : }
3215 : 158418 : inner = inner->next;
3216 : : }
3217 : :
3218 : 626165 : if (indep_p)
3219 : : {
3220 : 371155 : unsigned i;
3221 : 371155 : bitmap_iterator bi;
3222 : 957991 : EXECUTE_IF_SET_IN_BITMAP (refs_to_check, 0, i, bi)
3223 : : {
3224 : 657903 : im_mem_ref *aref = memory_accesses.refs_list[i];
3225 : 657903 : if (aref->mem.ref == error_mark_node)
3226 : : {
3227 : 43242 : gimple *stmt = aref->accesses_in_loop[0].stmt;
3228 : 43242 : if ((kind == sm_war
3229 : 17015 : && ref_maybe_used_by_stmt_p (stmt, &ref->mem,
3230 : : kind != sm_waw))
3231 : 59865 : || stmt_may_clobber_ref_p_1 (stmt, &ref->mem,
3232 : : kind != sm_waw))
3233 : : {
3234 : : indep_p = false;
3235 : : break;
3236 : : }
3237 : : }
3238 : 614661 : else if (!refs_independent_p (ref, aref, kind != sm_waw))
3239 : : {
3240 : : indep_p = false;
3241 : : break;
3242 : : }
3243 : : }
3244 : : }
3245 : : }
3246 : :
3247 : 1941801 : if (dump_file && (dump_flags & TDF_DETAILS))
3248 : 735 : fprintf (dump_file, "Querying %s dependencies of ref %u in loop %d: %s\n",
3249 : : kind == lim_raw ? "RAW" : (kind == sm_war ? "SM WAR" : "SM WAW"),
3250 : 357 : ref->id, loop->num, indep_p ? "independent" : "dependent");
3251 : :
3252 : : /* Record the computed result in the cache. */
3253 : 1941801 : record_loop_dependence (loop, ref, kind,
3254 : : indep_p ? dep_independent : dep_dependent);
3255 : :
3256 : 1941801 : return indep_p;
3257 : : }
3258 : :
3259 : : class ref_in_loop_hot_body
3260 : : {
3261 : : public:
3262 : 42328 : ref_in_loop_hot_body (class loop *loop_) : l (loop_) {}
3263 : : bool operator () (mem_ref_loc *loc);
3264 : : class loop *l;
3265 : : };
3266 : :
3267 : : /* Check the coldest loop between loop L and innermost loop. If there is one
3268 : : cold loop between L and INNER_LOOP, store motion can be performed, otherwise
3269 : : no cold loop means no store motion. get_coldest_out_loop also handles cases
3270 : : when l is inner_loop. */
3271 : : bool
3272 : 43120 : ref_in_loop_hot_body::operator () (mem_ref_loc *loc)
3273 : : {
3274 : 43120 : basic_block curr_bb = gimple_bb (loc->stmt);
3275 : 43120 : class loop *inner_loop = curr_bb->loop_father;
3276 : 43120 : return get_coldest_out_loop (l, inner_loop, curr_bb);
3277 : : }
3278 : :
3279 : :
3280 : : /* Returns true if we can perform store motion of REF from LOOP. */
3281 : :
3282 : : static bool
3283 : 2689549 : can_sm_ref_p (class loop *loop, im_mem_ref *ref)
3284 : : {
3285 : 2689549 : tree base;
3286 : :
3287 : : /* Can't hoist unanalyzable refs. */
3288 : 2689549 : if (!MEM_ANALYZABLE (ref))
3289 : : return false;
3290 : :
3291 : : /* Can't hoist/sink aggregate copies. */
3292 : 2334531 : if (ref->mem.ref == error_mark_node)
3293 : : return false;
3294 : :
3295 : : /* It should be movable. */
3296 : 1942394 : if (!is_gimple_reg_type (TREE_TYPE (ref->mem.ref))
3297 : 1942182 : || TREE_THIS_VOLATILE (ref->mem.ref)
3298 : 3867754 : || !for_each_index (&ref->mem.ref, may_move_till, loop))
3299 : 1104255 : return false;
3300 : :
3301 : : /* If it can throw fail, we do not properly update EH info. */
3302 : 838139 : if (tree_could_throw_p (ref->mem.ref))
3303 : : return false;
3304 : :
3305 : : /* If it can trap, it must be always executed in LOOP.
3306 : : Readonly memory locations may trap when storing to them, but
3307 : : tree_could_trap_p is a predicate for rvalues, so check that
3308 : : explicitly. */
3309 : 817247 : base = get_base_address (ref->mem.ref);
3310 : 817247 : if ((tree_could_trap_p (ref->mem.ref)
3311 : 769347 : || (DECL_P (base) && TREE_READONLY (base))
3312 : 769115 : || TREE_CODE (base) == STRING_CST)
3313 : : /* ??? We can at least use false here, allowing loads? We
3314 : : are forcing conditional stores if the ref is not always
3315 : : stored to later anyway. So this would only guard
3316 : : the load we need to emit. Thus when the ref is not
3317 : : loaded we can elide this completely? */
3318 : 865387 : && !ref_always_accessed_p (loop, ref, true))
3319 : : return false;
3320 : :
3321 : : /* Verify all loads of ref can be hoisted. */
3322 : 778947 : if (ref->loaded
3323 : 99567 : && bitmap_bit_p (ref->loaded, loop->num)
3324 : 873185 : && !ref_indep_loop_p (loop, ref, lim_raw))
3325 : : return false;
3326 : :
3327 : : /* Verify the candidate can be disambiguated against all loads,
3328 : : that is, we can elide all in-loop stores. Disambiguation
3329 : : against stores is done later when we cannot guarantee preserving
3330 : : the order of stores. */
3331 : 709515 : if (!ref_indep_loop_p (loop, ref, sm_war))
3332 : : return false;
3333 : :
3334 : : /* Verify whether the candidate is hot for LOOP. Only do store motion if the
3335 : : candidate's profile count is hot. Statement in cold BB shouldn't be moved
3336 : : out of it's loop_father. */
3337 : 42328 : if (!for_all_locs_in_loop (loop, ref, ref_in_loop_hot_body (loop)))
3338 : : return false;
3339 : :
3340 : : return true;
3341 : : }
3342 : :
3343 : : /* Marks the references in LOOP for that store motion should be performed
3344 : : in REFS_TO_SM. SM_EXECUTED is the set of references for that store
3345 : : motion was performed in one of the outer loops. */
3346 : :
3347 : : static void
3348 : 1274471 : find_refs_for_sm (class loop *loop, bitmap sm_executed, bitmap refs_to_sm)
3349 : : {
3350 : 1274471 : bitmap refs = &memory_accesses.all_refs_stored_in_loop[loop->num];
3351 : 1274471 : unsigned i;
3352 : 1274471 : bitmap_iterator bi;
3353 : 1274471 : im_mem_ref *ref;
3354 : :
3355 : 3964020 : EXECUTE_IF_AND_COMPL_IN_BITMAP (refs, sm_executed, 0, i, bi)
3356 : : {
3357 : 2689549 : ref = memory_accesses.refs_list[i];
3358 : 2689549 : if (can_sm_ref_p (loop, ref) && dbg_cnt (lim))
3359 : 41417 : bitmap_set_bit (refs_to_sm, i);
3360 : : }
3361 : 1274471 : }
3362 : :
3363 : : /* Checks whether LOOP (with exits stored in EXITS array) is suitable
3364 : : for a store motion optimization (i.e. whether we can insert statement
3365 : : on its exits). */
3366 : :
3367 : : static bool
3368 : 1330249 : loop_suitable_for_sm (class loop *loop ATTRIBUTE_UNUSED,
3369 : : const vec<edge> &exits)
3370 : : {
3371 : 1330249 : unsigned i;
3372 : 1330249 : edge ex;
3373 : :
3374 : 3431580 : FOR_EACH_VEC_ELT (exits, i, ex)
3375 : 2157109 : if (ex->flags & (EDGE_ABNORMAL | EDGE_EH))
3376 : : return false;
3377 : :
3378 : : return true;
3379 : : }
3380 : :
3381 : : /* Try to perform store motion for all memory references modified inside
3382 : : LOOP. SM_EXECUTED is the bitmap of the memory references for that
3383 : : store motion was executed in one of the outer loops. */
3384 : :
3385 : : static void
3386 : 1330249 : store_motion_loop (class loop *loop, bitmap sm_executed)
3387 : : {
3388 : 1330249 : auto_vec<edge> exits = get_loop_exit_edges (loop);
3389 : 1330249 : class loop *subloop;
3390 : 1330249 : bitmap sm_in_loop = BITMAP_ALLOC (&lim_bitmap_obstack);
3391 : :
3392 : 1330249 : if (loop_suitable_for_sm (loop, exits))
3393 : : {
3394 : 1274471 : find_refs_for_sm (loop, sm_executed, sm_in_loop);
3395 : 1274471 : if (!bitmap_empty_p (sm_in_loop))
3396 : 21349 : hoist_memory_references (loop, sm_in_loop, exits);
3397 : : }
3398 : :
3399 : 1330249 : bitmap_ior_into (sm_executed, sm_in_loop);
3400 : 1638135 : for (subloop = loop->inner; subloop != NULL; subloop = subloop->next)
3401 : 307886 : store_motion_loop (subloop, sm_executed);
3402 : 1330249 : bitmap_and_compl_into (sm_executed, sm_in_loop);
3403 : 1330249 : BITMAP_FREE (sm_in_loop);
3404 : 1330249 : }
3405 : :
3406 : : /* Try to perform store motion for all memory references modified inside
3407 : : loops. */
3408 : :
3409 : : static void
3410 : 484833 : do_store_motion (void)
3411 : : {
3412 : 484833 : class loop *loop;
3413 : 484833 : bitmap sm_executed = BITMAP_ALLOC (&lim_bitmap_obstack);
3414 : :
3415 : 1507196 : for (loop = current_loops->tree_root->inner; loop != NULL; loop = loop->next)
3416 : 1022363 : store_motion_loop (loop, sm_executed);
3417 : :
3418 : 484833 : BITMAP_FREE (sm_executed);
3419 : 484833 : }
3420 : :
3421 : : /* Fills ALWAYS_EXECUTED_IN information for basic blocks of LOOP, i.e.
3422 : : for each such basic block bb records the outermost loop for that execution
3423 : : of its header implies execution of bb. CONTAINS_CALL is the bitmap of
3424 : : blocks that contain a nonpure call. */
3425 : :
3426 : : static void
3427 : 1330450 : fill_always_executed_in_1 (class loop *loop, sbitmap contains_call)
3428 : : {
3429 : 1330450 : basic_block bb = NULL, last = NULL;
3430 : 1330450 : edge e;
3431 : 1330450 : class loop *inn_loop = loop;
3432 : :
3433 : 1330450 : if (ALWAYS_EXECUTED_IN (loop->header) == NULL)
3434 : : {
3435 : 1211492 : auto_vec<basic_block, 64> worklist;
3436 : 1211492 : worklist.reserve_exact (loop->num_nodes);
3437 : 1211492 : worklist.quick_push (loop->header);
3438 : 2670969 : do
3439 : : {
3440 : 2670969 : edge_iterator ei;
3441 : 2670969 : bb = worklist.pop ();
3442 : :
3443 : 2670969 : if (!flow_bb_inside_loop_p (inn_loop, bb))
3444 : : {
3445 : : /* When we are leaving a possibly infinite inner loop
3446 : : we have to stop processing. */
3447 : 158025 : if (!finite_loop_p (inn_loop))
3448 : : break;
3449 : : /* If the loop was finite we can continue with processing
3450 : : the loop we exited to. */
3451 : 149359 : inn_loop = bb->loop_father;
3452 : : }
3453 : :
3454 : 2662303 : if (dominated_by_p (CDI_DOMINATORS, loop->latch, bb))
3455 : 1555315 : last = bb;
3456 : :
3457 : 2662303 : if (bitmap_bit_p (contains_call, bb->index))
3458 : : break;
3459 : :
3460 : : /* If LOOP exits from this BB stop processing. */
3461 : 5102495 : FOR_EACH_EDGE (e, ei, bb->succs)
3462 : 3633439 : if (!flow_bb_inside_loop_p (loop, e->dest))
3463 : : break;
3464 : 2351564 : if (e)
3465 : : break;
3466 : :
3467 : : /* A loop might be infinite (TODO use simple loop analysis
3468 : : to disprove this if possible). */
3469 : 1469056 : if (bb->flags & BB_IRREDUCIBLE_LOOP)
3470 : : break;
3471 : :
3472 : 1468967 : if (bb->loop_father->header == bb)
3473 : : /* Record that we enter into a subloop since it might not
3474 : : be finite. */
3475 : : /* ??? Entering into a not always executed subloop makes
3476 : : fill_always_executed_in quadratic in loop depth since
3477 : : we walk those loops N times. This is not a problem
3478 : : in practice though, see PR102253 for a worst-case testcase. */
3479 : 553537 : inn_loop = bb->loop_father;
3480 : :
3481 : : /* Walk the body of LOOP sorted by dominance relation. Additionally,
3482 : : if a basic block S dominates the latch, then only blocks dominated
3483 : : by S are after it.
3484 : : This is get_loop_body_in_dom_order using a worklist algorithm and
3485 : : stopping once we are no longer interested in visiting further
3486 : : blocks. */
3487 : 1468967 : unsigned old_len = worklist.length ();
3488 : 1468967 : unsigned postpone = 0;
3489 : 1468967 : for (basic_block son = first_dom_son (CDI_DOMINATORS, bb);
3490 : 3187357 : son;
3491 : 1718390 : son = next_dom_son (CDI_DOMINATORS, son))
3492 : : {
3493 : 1718390 : if (!flow_bb_inside_loop_p (loop, son))
3494 : 20260 : continue;
3495 : 1698130 : if (dominated_by_p (CDI_DOMINATORS, loop->latch, son))
3496 : 477246 : postpone = worklist.length ();
3497 : 1698130 : worklist.quick_push (son);
3498 : : }
3499 : 1468967 : if (postpone)
3500 : : /* Postponing the block that dominates the latch means
3501 : : processing it last and thus putting it earliest in the
3502 : : worklist. */
3503 : 143630 : std::swap (worklist[old_len], worklist[postpone]);
3504 : : }
3505 : 2937934 : while (!worklist.is_empty ());
3506 : :
3507 : 1899138 : while (1)
3508 : : {
3509 : 1555315 : if (dump_enabled_p ())
3510 : 1695 : dump_printf (MSG_NOTE, "BB %d is always executed in loop %d\n",
3511 : : last->index, loop->num);
3512 : 1555315 : SET_ALWAYS_EXECUTED_IN (last, loop);
3513 : 1555315 : if (last == loop->header)
3514 : : break;
3515 : 343823 : last = get_immediate_dominator (CDI_DOMINATORS, last);
3516 : : }
3517 : 1211492 : }
3518 : :
3519 : 1638447 : for (loop = loop->inner; loop; loop = loop->next)
3520 : 307997 : fill_always_executed_in_1 (loop, contains_call);
3521 : 1330450 : }
3522 : :
3523 : : /* Fills ALWAYS_EXECUTED_IN information for basic blocks, i.e.
3524 : : for each such basic block bb records the outermost loop for that execution
3525 : : of its header implies execution of bb. */
3526 : :
3527 : : static void
3528 : 484894 : fill_always_executed_in (void)
3529 : : {
3530 : 484894 : basic_block bb;
3531 : 484894 : class loop *loop;
3532 : :
3533 : 484894 : auto_sbitmap contains_call (last_basic_block_for_fn (cfun));
3534 : 484894 : bitmap_clear (contains_call);
3535 : 15543810 : FOR_EACH_BB_FN (bb, cfun)
3536 : : {
3537 : 15058916 : gimple_stmt_iterator gsi;
3538 : 122170957 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3539 : : {
3540 : 100062214 : if (nonpure_call_p (gsi_stmt (gsi)))
3541 : : break;
3542 : : }
3543 : :
3544 : 15058916 : if (!gsi_end_p (gsi))
3545 : 3764121 : bitmap_set_bit (contains_call, bb->index);
3546 : : }
3547 : :
3548 : 1507347 : for (loop = current_loops->tree_root->inner; loop; loop = loop->next)
3549 : 1022453 : fill_always_executed_in_1 (loop, contains_call);
3550 : 484894 : }
3551 : :
3552 : : /* Find the coldest loop preheader for LOOP, also find the nearest hotter loop
3553 : : to LOOP. Then recursively iterate each inner loop. */
3554 : :
3555 : : void
3556 : 1330450 : fill_coldest_and_hotter_out_loop (class loop *coldest_loop,
3557 : : class loop *hotter_loop, class loop *loop)
3558 : : {
3559 : 1330450 : if (bb_colder_than_loop_preheader (loop_preheader_edge (loop)->src,
3560 : : coldest_loop))
3561 : 25285 : coldest_loop = loop;
3562 : :
3563 : 1330450 : coldest_outermost_loop[loop->num] = coldest_loop;
3564 : :
3565 : 1330450 : hotter_than_inner_loop[loop->num] = NULL;
3566 : 1330450 : class loop *outer_loop = loop_outer (loop);
3567 : 1330450 : if (hotter_loop
3568 : 1330450 : && bb_colder_than_loop_preheader (loop_preheader_edge (loop)->src,
3569 : : hotter_loop))
3570 : 2900 : hotter_than_inner_loop[loop->num] = hotter_loop;
3571 : :
3572 : 1330450 : if (outer_loop && outer_loop != current_loops->tree_root
3573 : 1638447 : && bb_colder_than_loop_preheader (loop_preheader_edge (loop)->src,
3574 : : outer_loop))
3575 : 31460 : hotter_than_inner_loop[loop->num] = outer_loop;
3576 : :
3577 : 1330450 : if (dump_enabled_p ())
3578 : : {
3579 : 1434 : dump_printf (MSG_NOTE, "loop %d's coldest_outermost_loop is %d, ",
3580 : : loop->num, coldest_loop->num);
3581 : 1434 : if (hotter_than_inner_loop[loop->num])
3582 : 137 : dump_printf (MSG_NOTE, "hotter_than_inner_loop is %d\n",
3583 : 137 : hotter_than_inner_loop[loop->num]->num);
3584 : : else
3585 : 1297 : dump_printf (MSG_NOTE, "hotter_than_inner_loop is NULL\n");
3586 : : }
3587 : :
3588 : 1330450 : class loop *inner_loop;
3589 : 1638447 : for (inner_loop = loop->inner; inner_loop; inner_loop = inner_loop->next)
3590 : 615994 : fill_coldest_and_hotter_out_loop (coldest_loop,
3591 : 307997 : hotter_than_inner_loop[loop->num],
3592 : : inner_loop);
3593 : 1330450 : }
3594 : :
3595 : : /* Compute the global information needed by the loop invariant motion pass. */
3596 : :
3597 : : static void
3598 : 484894 : tree_ssa_lim_initialize (bool store_motion)
3599 : : {
3600 : 484894 : unsigned i;
3601 : :
3602 : 484894 : bitmap_obstack_initialize (&lim_bitmap_obstack);
3603 : 484894 : gcc_obstack_init (&mem_ref_obstack);
3604 : 484894 : lim_aux_data_map = new hash_map<gimple *, lim_aux_data *>;
3605 : :
3606 : 484894 : if (flag_tm)
3607 : 104 : compute_transaction_bits ();
3608 : :
3609 : 484894 : memory_accesses.refs = new hash_table<mem_ref_hasher> (100);
3610 : 484894 : memory_accesses.refs_list.create (100);
3611 : : /* Allocate a special, unanalyzable mem-ref with ID zero. */
3612 : 484894 : memory_accesses.refs_list.quick_push
3613 : 484894 : (mem_ref_alloc (NULL, 0, UNANALYZABLE_MEM_ID));
3614 : :
3615 : 969788 : memory_accesses.refs_loaded_in_loop.create (number_of_loops (cfun));
3616 : 969788 : memory_accesses.refs_loaded_in_loop.quick_grow_cleared (number_of_loops (cfun));
3617 : 969788 : memory_accesses.refs_stored_in_loop.create (number_of_loops (cfun));
3618 : 969788 : memory_accesses.refs_stored_in_loop.quick_grow_cleared (number_of_loops (cfun));
3619 : 484894 : if (store_motion)
3620 : : {
3621 : 969666 : memory_accesses.all_refs_stored_in_loop.create (number_of_loops (cfun));
3622 : 484833 : memory_accesses.all_refs_stored_in_loop.quick_grow_cleared
3623 : 969666 : (number_of_loops (cfun));
3624 : : }
3625 : :
3626 : 6071664 : for (i = 0; i < number_of_loops (cfun); i++)
3627 : : {
3628 : 2550938 : bitmap_initialize (&memory_accesses.refs_loaded_in_loop[i],
3629 : : &lim_bitmap_obstack);
3630 : 2550938 : bitmap_initialize (&memory_accesses.refs_stored_in_loop[i],
3631 : : &lim_bitmap_obstack);
3632 : 2550938 : if (store_motion)
3633 : 2550646 : bitmap_initialize (&memory_accesses.all_refs_stored_in_loop[i],
3634 : : &lim_bitmap_obstack);
3635 : : }
3636 : :
3637 : 484894 : memory_accesses.ttae_cache = NULL;
3638 : :
3639 : : /* Initialize bb_loop_postorder with a mapping from loop->num to
3640 : : its postorder index. */
3641 : 484894 : i = 0;
3642 : 969788 : bb_loop_postorder = XNEWVEC (unsigned, number_of_loops (cfun));
3643 : 2785132 : for (auto loop : loops_list (cfun, LI_FROM_INNERMOST))
3644 : 1330450 : bb_loop_postorder[loop->num] = i++;
3645 : 484894 : }
3646 : :
3647 : : /* Cleans up after the invariant motion pass. */
3648 : :
3649 : : static void
3650 : 484894 : tree_ssa_lim_finalize (void)
3651 : : {
3652 : 484894 : basic_block bb;
3653 : 484894 : unsigned i;
3654 : 484894 : im_mem_ref *ref;
3655 : :
3656 : 15590197 : FOR_EACH_BB_FN (bb, cfun)
3657 : 15105303 : SET_ALWAYS_EXECUTED_IN (bb, NULL);
3658 : :
3659 : 484894 : bitmap_obstack_release (&lim_bitmap_obstack);
3660 : 969788 : delete lim_aux_data_map;
3661 : :
3662 : 484894 : delete memory_accesses.refs;
3663 : 484894 : memory_accesses.refs = NULL;
3664 : :
3665 : 5858721 : FOR_EACH_VEC_ELT (memory_accesses.refs_list, i, ref)
3666 : 5373827 : memref_free (ref);
3667 : 484894 : memory_accesses.refs_list.release ();
3668 : 484894 : obstack_free (&mem_ref_obstack, NULL);
3669 : :
3670 : 484894 : memory_accesses.refs_loaded_in_loop.release ();
3671 : 484894 : memory_accesses.refs_stored_in_loop.release ();
3672 : 484894 : memory_accesses.all_refs_stored_in_loop.release ();
3673 : :
3674 : 484894 : if (memory_accesses.ttae_cache)
3675 : 4090 : free_affine_expand_cache (&memory_accesses.ttae_cache);
3676 : :
3677 : 484894 : free (bb_loop_postorder);
3678 : :
3679 : 484894 : coldest_outermost_loop.release ();
3680 : 484894 : hotter_than_inner_loop.release ();
3681 : 484894 : }
3682 : :
3683 : : /* Moves invariants from loops. Only "expensive" invariants are moved out --
3684 : : i.e. those that are likely to be win regardless of the register pressure.
3685 : : Only perform store motion if STORE_MOTION is true. */
3686 : :
3687 : : unsigned int
3688 : 484894 : loop_invariant_motion_in_fun (function *fun, bool store_motion)
3689 : : {
3690 : 484894 : unsigned int todo = 0;
3691 : :
3692 : 484894 : tree_ssa_lim_initialize (store_motion);
3693 : :
3694 : 484894 : mark_ssa_maybe_undefs ();
3695 : :
3696 : : /* Gathers information about memory accesses in the loops. */
3697 : 484894 : analyze_memory_references (store_motion);
3698 : :
3699 : : /* Fills ALWAYS_EXECUTED_IN information for basic blocks. */
3700 : 484894 : fill_always_executed_in ();
3701 : :
3702 : : /* Pre-compute coldest outermost loop and nearest hotter loop of each loop.
3703 : : */
3704 : 484894 : class loop *loop;
3705 : 969788 : coldest_outermost_loop.create (number_of_loops (cfun));
3706 : 969788 : coldest_outermost_loop.safe_grow_cleared (number_of_loops (cfun));
3707 : 969788 : hotter_than_inner_loop.create (number_of_loops (cfun));
3708 : 969788 : hotter_than_inner_loop.safe_grow_cleared (number_of_loops (cfun));
3709 : 1507347 : for (loop = current_loops->tree_root->inner; loop != NULL; loop = loop->next)
3710 : 1022453 : fill_coldest_and_hotter_out_loop (loop, NULL, loop);
3711 : :
3712 : 484894 : int *rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
3713 : 484894 : int n = pre_and_rev_post_order_compute_fn (fun, NULL, rpo, false);
3714 : :
3715 : : /* For each statement determine the outermost loop in that it is
3716 : : invariant and cost for computing the invariant. */
3717 : 15543810 : for (int i = 0; i < n; ++i)
3718 : 15058916 : compute_invariantness (BASIC_BLOCK_FOR_FN (fun, rpo[i]));
3719 : :
3720 : : /* Execute store motion. Force the necessary invariants to be moved
3721 : : out of the loops as well. */
3722 : 484894 : if (store_motion)
3723 : 484833 : do_store_motion ();
3724 : :
3725 : 484894 : free (rpo);
3726 : 484894 : rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
3727 : 484894 : n = pre_and_rev_post_order_compute_fn (fun, NULL, rpo, false);
3728 : :
3729 : : /* Move the expressions that are expensive enough. */
3730 : 15590197 : for (int i = 0; i < n; ++i)
3731 : 15105303 : todo |= move_computations_worker (BASIC_BLOCK_FOR_FN (fun, rpo[i]));
3732 : :
3733 : 484894 : free (rpo);
3734 : :
3735 : 484894 : gsi_commit_edge_inserts ();
3736 : 484894 : if (need_ssa_update_p (fun))
3737 : 14739 : rewrite_into_loop_closed_ssa (NULL, TODO_update_ssa);
3738 : :
3739 : 484894 : tree_ssa_lim_finalize ();
3740 : :
3741 : 484894 : return todo;
3742 : : }
3743 : :
3744 : : /* Loop invariant motion pass. */
3745 : :
3746 : : namespace {
3747 : :
3748 : : const pass_data pass_data_lim =
3749 : : {
3750 : : GIMPLE_PASS, /* type */
3751 : : "lim", /* name */
3752 : : OPTGROUP_LOOP, /* optinfo_flags */
3753 : : TV_LIM, /* tv_id */
3754 : : PROP_cfg, /* properties_required */
3755 : : 0, /* properties_provided */
3756 : : 0, /* properties_destroyed */
3757 : : 0, /* todo_flags_start */
3758 : : 0, /* todo_flags_finish */
3759 : : };
3760 : :
3761 : : class pass_lim : public gimple_opt_pass
3762 : : {
3763 : : public:
3764 : 1142756 : pass_lim (gcc::context *ctxt)
3765 : 2285512 : : gimple_opt_pass (pass_data_lim, ctxt)
3766 : : {}
3767 : :
3768 : : /* opt_pass methods: */
3769 : 857067 : opt_pass * clone () final override { return new pass_lim (m_ctxt); }
3770 : 1286157 : bool gate (function *) final override { return flag_tree_loop_im != 0; }
3771 : : unsigned int execute (function *) final override;
3772 : :
3773 : : }; // class pass_lim
3774 : :
3775 : : unsigned int
3776 : 1285846 : pass_lim::execute (function *fun)
3777 : : {
3778 : 1285846 : in_loop_pipeline = scev_initialized_p ();
3779 : 1285846 : if (!in_loop_pipeline)
3780 : 1043644 : loop_optimizer_init (LOOPS_NORMAL | LOOPS_HAVE_RECORDED_EXITS);
3781 : :
3782 : 2571692 : if (number_of_loops (fun) <= 1)
3783 : : return 0;
3784 : 484849 : unsigned int todo = loop_invariant_motion_in_fun (fun, flag_move_loop_stores);
3785 : :
3786 : 484849 : if (!in_loop_pipeline)
3787 : 242647 : loop_optimizer_finalize ();
3788 : : else
3789 : 242202 : scev_reset ();
3790 : : return todo;
3791 : : }
3792 : :
3793 : : } // anon namespace
3794 : :
3795 : : gimple_opt_pass *
3796 : 285689 : make_pass_lim (gcc::context *ctxt)
3797 : : {
3798 : 285689 : return new pass_lim (ctxt);
3799 : : }
3800 : :
3801 : :
|