Line data Source code
1 : /* Backward propagation of indirect loads through PHIs.
2 : Copyright (C) 2007-2026 Free Software Foundation, Inc.
3 : Contributed by Richard Guenther <rguenther@suse.de>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3, or (at your option)
10 : any later version.
11 :
12 : GCC is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "tree.h"
26 : #include "gimple.h"
27 : #include "tree-pass.h"
28 : #include "ssa.h"
29 : #include "gimple-pretty-print.h"
30 : #include "fold-const.h"
31 : #include "tree-eh.h"
32 : #include "gimple-iterator.h"
33 : #include "stor-layout.h"
34 : #include "tree-ssa-loop.h"
35 : #include "tree-cfg.h"
36 : #include "tree-ssa-dce.h"
37 : #include "cfgloop.h"
38 :
39 : /* This pass propagates indirect loads through the PHI node for its
40 : address to make the load source possibly non-addressable and to
41 : allow for PHI optimization to trigger.
42 :
43 : For example the pass changes
44 :
45 : # addr_1 = PHI <&a, &b>
46 : tmp_1 = *addr_1;
47 :
48 : to
49 :
50 : # tmp_1 = PHI <a, b>
51 :
52 : but also handles more complex scenarios like
53 :
54 : D.2077_2 = &this_1(D)->a1;
55 : ...
56 :
57 : # b_12 = PHI <&c(2), D.2077_2(3)>
58 : D.2114_13 = *b_12;
59 : ...
60 :
61 : # b_15 = PHI <b_12(4), &b(5)>
62 : D.2080_5 = &this_1(D)->a0;
63 : ...
64 :
65 : # b_18 = PHI <D.2080_5(6), &c(7)>
66 : ...
67 :
68 : # b_21 = PHI <b_15(8), b_18(9)>
69 : D.2076_8 = *b_21;
70 :
71 : where the addresses loaded are defined by PHIs itself.
72 : The above happens for
73 :
74 : std::max(std::min(a0, c), std::min(std::max(a1, c), b))
75 :
76 : where this pass transforms it to a form later PHI optimization
77 : recognizes and transforms it to the simple
78 :
79 : D.2109_10 = this_1(D)->a1;
80 : D.2110_11 = c;
81 : D.2114_31 = MAX_EXPR <D.2109_10, D.2110_11>;
82 : D.2115_14 = b;
83 : D.2125_17 = MIN_EXPR <D.2115_14, D.2114_31>;
84 : D.2119_16 = this_1(D)->a0;
85 : D.2124_32 = MIN_EXPR <D.2110_11, D.2119_16>;
86 : D.2076_33 = MAX_EXPR <D.2125_17, D.2124_32>;
87 :
88 : The pass does a dominator walk processing loads using a basic-block
89 : local analysis and stores the result for use by transformations on
90 : dominated basic-blocks. */
91 :
92 :
93 : /* Structure to keep track of the value of a dereferenced PHI result
94 : and the virtual operand used for that dereference. */
95 :
96 : struct phiprop_d
97 : {
98 : tree value;
99 : tree vuse;
100 : };
101 :
102 : /* Insert a new phi node for the dereference of PHI at basic_block
103 : BB with the virtual operands from USE_STMT. The vuse for
104 : the load will be set to OTHER_VUSE unless there is virtual op
105 : phi for BB. */
106 :
107 : static tree
108 8206 : phiprop_insert_phi (basic_block bb, gphi *phi, gimple *use_stmt,
109 : struct phiprop_d *phivn, size_t n,
110 : bitmap dce_ssa_names, tree other_vuse)
111 : {
112 8206 : tree res;
113 8206 : gphi *new_phi = NULL;
114 8206 : edge_iterator ei;
115 8206 : edge e;
116 8206 : tree phi_result = PHI_RESULT (phi);
117 8206 : bitmap_set_bit (dce_ssa_names, SSA_NAME_VERSION (phi_result));
118 :
119 8206 : gcc_assert (is_gimple_assign (use_stmt)
120 : && gimple_assign_rhs_code (use_stmt) == MEM_REF);
121 :
122 : /* Build a new PHI node to replace the definition of
123 : the indirect reference lhs. */
124 8206 : res = gimple_assign_lhs (use_stmt);
125 8206 : if (TREE_CODE (res) == SSA_NAME)
126 7961 : new_phi = create_phi_node (res, bb);
127 :
128 8206 : if (dump_file && (dump_flags & TDF_DETAILS))
129 : {
130 26 : fprintf (dump_file, "Inserting PHI for result of load ");
131 26 : print_gimple_stmt (dump_file, use_stmt, 0);
132 : }
133 :
134 8206 : gphi *vphi = get_virtual_phi (bb);
135 :
136 : /* Add PHI arguments for each edge inserting loads of the
137 : addressable operands. */
138 23136 : FOR_EACH_EDGE (e, ei, bb->preds)
139 : {
140 14930 : tree old_arg, new_var;
141 14930 : gassign *tmp;
142 14930 : location_t locus;
143 :
144 14930 : old_arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
145 14930 : locus = gimple_phi_arg_location_from_edge (phi, e);
146 14930 : while (TREE_CODE (old_arg) == SSA_NAME
147 16308 : && (SSA_NAME_VERSION (old_arg) >= n
148 1481 : || phivn[SSA_NAME_VERSION (old_arg)].value == NULL_TREE))
149 : {
150 1378 : gimple *def_stmt = SSA_NAME_DEF_STMT (old_arg);
151 1378 : old_arg = gimple_assign_rhs1 (def_stmt);
152 1378 : locus = gimple_location (def_stmt);
153 : }
154 :
155 14930 : if (TREE_CODE (old_arg) == SSA_NAME)
156 : {
157 103 : if (dump_file && (dump_flags & TDF_DETAILS))
158 : {
159 6 : fprintf (dump_file, " for edge defining ");
160 6 : print_generic_expr (dump_file, PHI_ARG_DEF_FROM_EDGE (phi, e));
161 6 : fprintf (dump_file, " reusing PHI result ");
162 12 : print_generic_expr (dump_file,
163 6 : phivn[SSA_NAME_VERSION (old_arg)].value);
164 6 : fprintf (dump_file, "\n");
165 : }
166 : /* Reuse a formerly created dereference. */
167 103 : new_var = phivn[SSA_NAME_VERSION (old_arg)].value;
168 : }
169 : else
170 : {
171 14827 : tree rhs = gimple_assign_rhs1 (use_stmt);
172 14827 : gcc_assert (TREE_CODE (old_arg) == ADDR_EXPR);
173 14827 : tree vuse = NULL_TREE;
174 14827 : if (TREE_CODE (res) == SSA_NAME)
175 : {
176 14275 : new_var = make_ssa_name (TREE_TYPE (rhs));
177 14275 : if (vphi)
178 132 : vuse = PHI_ARG_DEF_FROM_EDGE (vphi, e);
179 : else
180 : vuse = other_vuse;
181 : }
182 : else
183 : /* For the aggregate copy case updating virtual operands
184 : we'd have to possibly insert a virtual PHI and we have
185 : to split the existing VUSE lifetime. Leave that to
186 : the generic SSA updating. */
187 552 : new_var = unshare_expr (res);
188 14827 : if (!is_gimple_min_invariant (old_arg))
189 1355 : old_arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
190 : else
191 13472 : old_arg = unshare_expr (old_arg);
192 14827 : tmp = gimple_build_assign (new_var,
193 14827 : fold_build2 (MEM_REF, TREE_TYPE (rhs),
194 : old_arg,
195 : TREE_OPERAND (rhs, 1)));
196 14827 : gimple_set_location (tmp, locus);
197 14827 : if (vuse)
198 14275 : gimple_set_vuse (tmp, vuse);
199 :
200 14827 : gsi_insert_on_edge (e, tmp);
201 14827 : update_stmt (tmp);
202 :
203 14827 : if (dump_file && (dump_flags & TDF_DETAILS))
204 : {
205 46 : fprintf (dump_file, " for edge defining ");
206 46 : print_generic_expr (dump_file, PHI_ARG_DEF_FROM_EDGE (phi, e));
207 46 : fprintf (dump_file, " inserting load ");
208 46 : print_gimple_stmt (dump_file, tmp, 0);
209 : }
210 : }
211 :
212 14930 : if (new_phi)
213 14378 : add_phi_arg (new_phi, new_var, e, locus);
214 : }
215 :
216 8206 : if (new_phi)
217 : {
218 7961 : update_stmt (new_phi);
219 :
220 7961 : if (dump_file && (dump_flags & TDF_DETAILS))
221 26 : print_gimple_stmt (dump_file, new_phi, 0);
222 : }
223 :
224 8206 : return res;
225 : }
226 :
227 : /* Verify if *idx is available at *DATA. */
228 :
229 : static bool
230 50 : chk_uses (tree, tree *idx, void *data)
231 : {
232 50 : basic_block dom = (basic_block) data;
233 50 : if (TREE_CODE (*idx) == SSA_NAME)
234 32 : return (SSA_NAME_IS_DEFAULT_DEF (*idx)
235 32 : || ! dominated_by_p (CDI_DOMINATORS,
236 18 : gimple_bb (SSA_NAME_DEF_STMT (*idx)), dom));
237 : return true;
238 : }
239 :
240 : /* Check if we can move the loads from LOAD_STMT.
241 : This is when the virtual use is the same as the
242 : one active at the start of BB which we know either
243 : from its virtual PHI def (VPHI) or from the common
244 : incoming VUSE (up_vuse). If neither is present
245 : make sure the def stmt of the virtual use is in a
246 : different basic block dominating BB. When the def
247 : is an edge-inserted one we know it dominates us.
248 : Returns the vuse to use for the inserting. NULL_TREE
249 : is returned when we can't do the insert. */
250 :
251 : static tree
252 9215 : can_handle_load (gimple *load_stmt,
253 : basic_block bb,
254 : gphi *vphi, tree up_vuse, bool aggregate)
255 : {
256 9215 : tree vuse = gimple_vuse (load_stmt);
257 : /* If the load does not have a store beforehand,
258 : then we can do the load in conditional. */
259 9215 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
260 : {
261 : /* For loads that have no stores before, there should be no
262 : vphi. */
263 1338 : gcc_checking_assert (!vphi);
264 : /* The common vuse is the same as the default or there is none. */
265 1338 : gcc_checking_assert (!up_vuse || up_vuse == vuse);
266 : return vuse;
267 : }
268 :
269 : /* If we have a vphi, then that needs to be end point.
270 : If we have a common incoming vuse, that needs to be the end point. */
271 7877 : tree expected_vuse = NULL_TREE;
272 7877 : if (vphi)
273 331 : expected_vuse = gimple_phi_result (vphi);
274 : else if (up_vuse)
275 : expected_vuse = up_vuse;
276 : /* Try to see if the store does not effect the load. */
277 7877 : gimple *other_store = SSA_NAME_DEF_STMT (vuse);
278 : /* For aggregates, skipping the store is too
279 : hard to handle as you need to check for loads
280 : and it is not worth the extra checks so just handle expected vuse
281 : and the dominated by case. */
282 7877 : if (aggregate)
283 : {
284 : /* If the vuse on the load is the same as the expected vuse,
285 : there are no stores in between. */
286 264 : if (vuse == expected_vuse)
287 : return vuse;
288 236 : if (expected_vuse)
289 : return NULL_TREE;
290 236 : if (gimple_bb (other_store) != bb
291 404 : && dominated_by_p (CDI_DOMINATORS,
292 168 : bb, gimple_bb (other_store)))
293 : return vuse;
294 74 : return NULL_TREE;
295 : }
296 :
297 : /* Skip over clobbers in the same bb as the use
298 : as they don't interfere with loads. */
299 7631 : while (!SSA_NAME_IS_DEFAULT_DEF (vuse)
300 7631 : && gimple_clobber_p (other_store)
301 7877 : && gimple_bb (other_store) == bb)
302 : {
303 18 : vuse = gimple_vuse (other_store);
304 18 : other_store = SSA_NAME_DEF_STMT (vuse);
305 : }
306 : /* If the load does not have a store beforehand,
307 : then we can do the load in conditional. */
308 7613 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
309 : {
310 : /* For loads that have no stores before, there should be no
311 : vphi. */
312 0 : gcc_checking_assert (!vphi);
313 : /* The common vuse is the same as the default or there is none. */
314 0 : gcc_checking_assert (!up_vuse || up_vuse == vuse);
315 : return vuse;
316 : }
317 :
318 : /* If the vuse on the load is the same as the expected vuse,
319 : there are no stores in between. */
320 7613 : if (vuse == expected_vuse)
321 : return vuse;
322 :
323 : /* Only handling the case where the store is in the same
324 : bb as the phi. */
325 7397 : if (gimple_bb (other_store) == bb)
326 : {
327 223 : tree src = gimple_assign_rhs1 (load_stmt);
328 223 : ao_ref read;
329 223 : ao_ref_init (&read, src);
330 223 : if (stmt_may_clobber_ref_p_1 (other_store, &read, false))
331 184 : return NULL_TREE;
332 119 : vuse = gimple_vuse (other_store);
333 : /* If that skipped store was the first store in program,
334 : then we can do the load conditional. */
335 119 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
336 : {
337 : /* For loads that have no stores before, there should be no
338 : vphi. */
339 4 : gcc_checking_assert (!vphi);
340 : /* The common vuse is the same as the default or there is none. */
341 4 : gcc_checking_assert (!up_vuse || up_vuse == vuse);
342 : return vuse;
343 : }
344 115 : other_store = SSA_NAME_DEF_STMT (vuse);
345 : /* If the new vuse (after skipping) is the same as expected
346 : then that is the vuse to return. */
347 115 : if (vuse == expected_vuse)
348 : return vuse;
349 108 : if (gimple_bb (other_store) == bb)
350 : return NULL_TREE;
351 : }
352 :
353 : /* If there was no an expected vuse then see if the vuse dominates the phi of
354 : the address. */
355 7213 : if (!expected_vuse
356 14232 : && dominated_by_p (CDI_DOMINATORS,
357 7019 : bb, gimple_bb (other_store)))
358 6816 : return vuse;
359 :
360 : return NULL_TREE;
361 : }
362 :
363 : /* Propagate between the phi node arguments of PHI in BB and phi result
364 : users. For now this matches
365 : # p_2 = PHI <&x, &y>
366 : <Lx>:;
367 : p_3 = p_2;
368 : z_2 = *p_3;
369 : and converts it to
370 : # z_2 = PHI <x, y>
371 : <Lx>:;
372 : Returns true if a transformation was done and edge insertions
373 : need to be committed. Global data PHIVN and N is used to track
374 : past transformation results. VPHI is the virtual PHI node in BB
375 : if there is one. We need to be especially careful here
376 : with aliasing issues as we are moving memory reads. */
377 :
378 : static bool
379 7950616 : propagate_with_phi (basic_block bb, gphi *vphi, gphi *phi,
380 : struct phiprop_d *phivn, size_t n, bitmap dce_ssa_names)
381 : {
382 7950616 : tree ptr = PHI_RESULT (phi);
383 7950616 : gimple *use_stmt;
384 7950616 : tree res = NULL_TREE;
385 7950616 : gimple_stmt_iterator gsi;
386 7950616 : imm_use_iterator ui;
387 7950616 : use_operand_p arg_p, use;
388 7950616 : ssa_op_iter i;
389 7950616 : bool phi_inserted;
390 7950616 : bool changed;
391 7950616 : tree type = NULL_TREE;
392 :
393 15147409 : if (!POINTER_TYPE_P (TREE_TYPE (ptr))
394 7968097 : || (!is_gimple_reg_type (TREE_TYPE (TREE_TYPE (ptr)))
395 362337 : && TYPE_MODE (TREE_TYPE (TREE_TYPE (ptr))) == BLKmode))
396 : return false;
397 :
398 503988 : tree up_vuse = NULL_TREE;
399 503988 : bool canpossible_trap = false;
400 : /* Check if we can "cheaply" dereference all phi arguments. */
401 605020 : FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
402 : {
403 581675 : tree arg = USE_FROM_PTR (arg_p);
404 : /* Walk the ssa chain until we reach a ssa name we already
405 : created a value for or we reach a definition of the form
406 : ssa_name_n = &var; */
407 581675 : while (TREE_CODE (arg) == SSA_NAME
408 496151 : && !SSA_NAME_IS_DEFAULT_DEF (arg)
409 1242825 : && (SSA_NAME_VERSION (arg) >= n
410 444893 : || phivn[SSA_NAME_VERSION (arg)].value == NULL_TREE))
411 : {
412 444785 : gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
413 444785 : if (!gimple_assign_single_p (def_stmt))
414 : return false;
415 216247 : arg = gimple_assign_rhs1 (def_stmt);
416 : }
417 353137 : if (TREE_CODE (arg) == ADDR_EXPR)
418 : {
419 100915 : tree decl = TREE_OPERAND (arg, 0);
420 100915 : if (!canpossible_trap)
421 97967 : canpossible_trap = tree_could_trap_p (decl);
422 : }
423 : /* When we have an SSA name see if we previously encountered a
424 : dereference of it. */
425 252222 : else if (TREE_CODE (arg) == SSA_NAME
426 51366 : && SSA_NAME_VERSION (arg) < n
427 51366 : && phivn[SSA_NAME_VERSION (arg)].value != NULL_TREE
428 252340 : && (!type
429 3 : || types_compatible_p
430 3 : (type, TREE_TYPE (phivn[SSA_NAME_VERSION (arg)].value))))
431 : {
432 : /* The dereference should be under the VUSE that's active in BB.
433 : If the BB has no virtual PHI then record the common "incoming"
434 : vuse. */
435 118 : if (vphi)
436 2 : up_vuse = gimple_phi_arg_def (vphi, phi_arg_index_from_use (arg_p));
437 118 : if (!up_vuse)
438 113 : up_vuse = phivn[SSA_NAME_VERSION (arg)].vuse;
439 5 : else if (up_vuse != phivn[SSA_NAME_VERSION (arg)].vuse)
440 : return false;
441 : }
442 : else
443 : return false;
444 101032 : if (!type
445 100970 : && TREE_CODE (arg) == SSA_NAME)
446 114 : type = TREE_TYPE (phivn[SSA_NAME_VERSION (arg)].value);
447 : }
448 :
449 : /* Find a dereferencing use. First follow (single use) ssa
450 : copy chains for ptr. */
451 24443 : while (single_imm_use (ptr, &use, &use_stmt)
452 24443 : && gimple_assign_ssa_name_copy_p (use_stmt))
453 1098 : ptr = gimple_assign_lhs (use_stmt);
454 :
455 : /* Replace the first dereference of *ptr if there is one and if we
456 : can move the loads to the place of the ptr phi node. */
457 23345 : phi_inserted = false;
458 23345 : changed = false;
459 23345 : auto_vec<gimple*> delayed_uses;
460 60323 : FOR_EACH_IMM_USE_STMT (use_stmt, ui, ptr)
461 : {
462 36978 : bool delay = false;
463 :
464 : /* Check whether this is a load of *ptr. */
465 36978 : auto assign = dyn_cast<gassign *> (use_stmt);
466 16123 : if (!(assign
467 16123 : && gimple_assign_rhs_code (assign) == MEM_REF
468 9477 : && TREE_OPERAND (gimple_assign_rhs1 (assign), 0) == ptr
469 9477 : && integer_zerop (TREE_OPERAND (gimple_assign_rhs1 (assign), 1))
470 9461 : && (!type
471 128 : || types_compatible_p
472 128 : (TREE_TYPE (gimple_assign_lhs (assign)), type))
473 : /* We cannot replace a load that may throw or is volatile.
474 : For volatiles the transform can change the number of
475 : executions if the load is inside a loop but the address
476 : computations outside (PR91812). We could relax this
477 : if we guard against that appropriately. For loads that can
478 : throw we could relax things if the moved loads all are
479 : known to not throw. */
480 9453 : && !stmt_can_throw_internal (cfun, assign)
481 9227 : && !gimple_has_volatile_ops (assign)))
482 27763 : continue;
483 :
484 9215 : bool aggregate = false;
485 9215 : if (!is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (assign))))
486 367 : aggregate = true;
487 :
488 9215 : tree other_vuse;
489 9215 : other_vuse = can_handle_load (use_stmt, bb, vphi, up_vuse, aggregate);
490 9215 : if (!other_vuse)
491 644 : continue;
492 :
493 8571 : if ((canpossible_trap || aggregate)
494 8571 : && !dom_info_available_p (cfun, CDI_POST_DOMINATORS))
495 605 : calculate_dominance_info (CDI_POST_DOMINATORS);
496 :
497 : /* Only replace loads in blocks that post-dominate the PHI node. That
498 : makes sure we don't end up speculating trapping loads or
499 : aggregate stores won't happen speculating. */
500 8571 : if ((canpossible_trap || aggregate)
501 8571 : && !dominated_by_p (CDI_POST_DOMINATORS,
502 2548 : bb, gimple_bb (use_stmt)))
503 : delay = true;
504 :
505 : /* Amend the post-dominance check for SSA cycles, we need to
506 : make sure each PHI result value is dereferenced.
507 : We only want to delay this if we don't insert a phi. */
508 8571 : if (!(gimple_bb (use_stmt) == bb
509 339 : || (!(bb->flags & BB_IRREDUCIBLE_LOOP)
510 339 : && !(gimple_bb (use_stmt)->flags & BB_IRREDUCIBLE_LOOP)
511 339 : && (bb->loop_father == gimple_bb (use_stmt)->loop_father
512 89 : || flow_loop_nested_p (bb->loop_father,
513 89 : gimple_bb (use_stmt)->loop_father)))))
514 : delay = true;
515 :
516 : /* Found a proper dereference with an aggregate copy. Just
517 : insert aggregate copies on the edges instead. */
518 8571 : if (aggregate)
519 : {
520 : /* aggregate copies are too hard to handled if delayed. */
521 293 : if (delay)
522 48 : goto next;
523 554 : if (!gimple_vdef (use_stmt))
524 0 : goto next;
525 :
526 : /* As we replicate the lhs on each incoming edge all
527 : used SSA names have to be available there. */
528 277 : if (! for_each_index (gimple_assign_lhs_ptr (use_stmt),
529 : chk_uses,
530 277 : get_immediate_dominator (CDI_DOMINATORS,
531 : gimple_bb (phi))))
532 18 : goto next;
533 :
534 259 : gimple *vuse_stmt;
535 259 : imm_use_iterator vui;
536 259 : use_operand_p vuse_p;
537 259 : tree vuse = gimple_vuse (use_stmt);
538 : /* In order to move the aggregate copies earlier, make sure
539 : there are no statements that could read from memory
540 : aliasing the lhs in between the start of bb and use_stmt.
541 : As we require use_stmt to have a VDEF above, loads after
542 : use_stmt will use a different virtual SSA_NAME. When
543 : we reach an edge inserted load the constraints we place
544 : on processing guarantees that program order is preserved
545 : so we can avoid checking those. */
546 1171 : FOR_EACH_IMM_USE_FAST (vuse_p, vui, vuse)
547 : {
548 926 : vuse_stmt = USE_STMT (vuse_p);
549 926 : if (vuse_stmt == use_stmt)
550 253 : continue;
551 673 : if (!gimple_bb (vuse_stmt)
552 1322 : || !dominated_by_p (CDI_DOMINATORS,
553 649 : gimple_bb (vuse_stmt), bb))
554 659 : continue;
555 14 : if (ref_maybe_used_by_stmt_p (vuse_stmt,
556 : gimple_assign_lhs (use_stmt)))
557 14 : goto next;
558 14 : }
559 :
560 245 : phiprop_insert_phi (bb, phi, use_stmt, phivn, n,
561 : dce_ssa_names, other_vuse);
562 :
563 : /* Remove old stmt. The phi and all of maybe its dependencies
564 : will be removed later via simple_dce_from_worklist. */
565 245 : gsi = gsi_for_stmt (use_stmt);
566 : /* Unlinking the VDEF here is fine as we are sure that we process
567 : stmts in execution order due to aggregate copies having VDEFs
568 : and we emit loads on the edges in the very same order.
569 : We get multiple copies (or intermediate register loads) handled
570 : only by walking PHIs or immediate uses in a lucky order though,
571 : so we could signal the caller to re-start iterating over PHIs
572 : when we come here which would make it quadratic in the number
573 : of PHIs. */
574 245 : unlink_stmt_vdef (use_stmt);
575 245 : gsi_remove (&gsi, true);
576 :
577 245 : changed = true;
578 : }
579 : /* Further replacements are easy, just make a copy out of the
580 : load. */
581 8278 : else if (phi_inserted)
582 : {
583 0 : gimple_assign_set_rhs1 (use_stmt, res);
584 0 : update_stmt (use_stmt);
585 0 : changed = true;
586 : }
587 8278 : else if (delay)
588 317 : delayed_uses.safe_push (use_stmt);
589 : /* Found a proper dereference. Insert a phi node if this
590 : is the first load transformation. */
591 : else
592 : {
593 7961 : tree vuse = gimple_vuse (use_stmt);
594 7961 : res = phiprop_insert_phi (bb, phi, use_stmt, phivn, n,
595 : dce_ssa_names, other_vuse);
596 7961 : type = TREE_TYPE (res);
597 :
598 : /* Remember the value we created for *ptr. */
599 7961 : phivn[SSA_NAME_VERSION (ptr)].value = res;
600 7961 : phivn[SSA_NAME_VERSION (ptr)].vuse = vuse;
601 :
602 : /* Remove old stmt. The phi and all of maybe its dependencies
603 : will be removed later via simple_dce_from_worklist. */
604 7961 : gsi = gsi_for_stmt (use_stmt);
605 7961 : gsi_remove (&gsi, true);
606 :
607 7961 : phi_inserted = true;
608 7961 : changed = true;
609 : }
610 :
611 36978 : next:;
612 : /* Continue searching for a proper dereference. */
613 23345 : }
614 :
615 : /* Update the delayed uses if there is any
616 : as now we know this is safe to do. */
617 23345 : if (phi_inserted)
618 8501 : for (auto use_stmt : delayed_uses)
619 : {
620 : /* The types must match of the inserted phi. */
621 250 : if (!types_compatible_p (type, TREE_TYPE (gimple_assign_lhs (use_stmt))))
622 6 : continue;
623 244 : gimple_assign_set_rhs1 (use_stmt, res);
624 244 : update_stmt (use_stmt);
625 : }
626 :
627 23345 : return changed;
628 23345 : }
629 :
630 : /* Main entry for phiprop pass. */
631 :
632 : namespace {
633 :
634 : const pass_data pass_data_phiprop =
635 : {
636 : GIMPLE_PASS, /* type */
637 : "phiprop", /* name */
638 : OPTGROUP_NONE, /* optinfo_flags */
639 : TV_TREE_PHIPROP, /* tv_id */
640 : ( PROP_cfg | PROP_ssa ), /* properties_required */
641 : 0, /* properties_provided */
642 : 0, /* properties_destroyed */
643 : 0, /* todo_flags_start */
644 : 0, /* todo_flags_finish */
645 : };
646 :
647 : class pass_phiprop : public gimple_opt_pass
648 : {
649 : public:
650 605970 : pass_phiprop (gcc::context *ctxt)
651 1211940 : : gimple_opt_pass (pass_data_phiprop, ctxt)
652 : {}
653 :
654 : /* opt_pass methods: */
655 302985 : opt_pass * clone () final override { return new pass_phiprop (m_ctxt); }
656 3595533 : bool gate (function *) final override { return flag_tree_phiprop; }
657 : unsigned int execute (function *) final override;
658 :
659 : }; // class pass_phiprop
660 :
661 : unsigned int
662 3595162 : pass_phiprop::execute (function *fun)
663 : {
664 3595162 : struct phiprop_d *phivn;
665 3595162 : bool did_something = false;
666 3595162 : basic_block bb;
667 3595162 : gphi_iterator gsi;
668 3595162 : unsigned i;
669 3595162 : size_t n;
670 3595162 : auto_bitmap dce_ssa_names;
671 :
672 3595162 : calculate_dominance_info (CDI_DOMINATORS);
673 :
674 3595162 : n = num_ssa_names;
675 3595162 : phivn = XCNEWVEC (struct phiprop_d, n);
676 :
677 : /* Walk the dominator tree in preorder. */
678 3595162 : auto_vec<basic_block> bbs
679 : = get_all_dominated_blocks (CDI_DOMINATORS,
680 3595162 : single_succ (ENTRY_BLOCK_PTR_FOR_FN (fun)));
681 33244629 : FOR_EACH_VEC_ELT (bbs, i, bb)
682 : {
683 : /* Since we're going to move dereferences across predecessor
684 : edges avoid blocks with abnormal predecessors. */
685 26054305 : if (bb_has_abnormal_pred (bb))
686 9322 : continue;
687 26044983 : gphi *vphi = get_virtual_phi (bb);
688 33995599 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
689 7950616 : did_something |= propagate_with_phi (bb, vphi, gsi.phi (),
690 : phivn, n, dce_ssa_names);
691 : }
692 :
693 3595162 : if (did_something)
694 : {
695 5615 : gsi_commit_edge_inserts ();
696 5615 : simple_dce_from_worklist (dce_ssa_names);
697 : }
698 :
699 3595162 : free (phivn);
700 :
701 3595162 : free_dominance_info (CDI_POST_DOMINATORS);
702 :
703 3595162 : return did_something ? TODO_update_ssa_only_virtuals : 0;
704 3595162 : }
705 :
706 : } // anon namespace
707 :
708 : gimple_opt_pass *
709 302985 : make_pass_phiprop (gcc::context *ctxt)
710 : {
711 302985 : return new pass_phiprop (ctxt);
712 : }
|