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