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 "gimplify.h"
33 : #include "gimple-iterator.h"
34 : #include "stor-layout.h"
35 : #include "tree-ssa-loop.h"
36 : #include "tree-cfg.h"
37 : #include "tree-ssa-dce.h"
38 : #include "cfgloop.h"
39 :
40 : /* This pass propagates indirect loads through the PHI node for its
41 : address to make the load source possibly non-addressable and to
42 : allow for PHI optimization to trigger.
43 :
44 : For example the pass changes
45 :
46 : # addr_1 = PHI <&a, &b>
47 : tmp_1 = *addr_1;
48 :
49 : to
50 :
51 : # tmp_1 = PHI <a, b>
52 :
53 : but also handles more complex scenarios like
54 :
55 : D.2077_2 = &this_1(D)->a1;
56 : ...
57 :
58 : # b_12 = PHI <&c(2), D.2077_2(3)>
59 : D.2114_13 = *b_12;
60 : ...
61 :
62 : # b_15 = PHI <b_12(4), &b(5)>
63 : D.2080_5 = &this_1(D)->a0;
64 : ...
65 :
66 : # b_18 = PHI <D.2080_5(6), &c(7)>
67 : ...
68 :
69 : # b_21 = PHI <b_15(8), b_18(9)>
70 : D.2076_8 = *b_21;
71 :
72 : where the addresses loaded are defined by PHIs itself.
73 : The above happens for
74 :
75 : std::max(std::min(a0, c), std::min(std::max(a1, c), b))
76 :
77 : where this pass transforms it to a form later PHI optimization
78 : recognizes and transforms it to the simple
79 :
80 : D.2109_10 = this_1(D)->a1;
81 : D.2110_11 = c;
82 : D.2114_31 = MAX_EXPR <D.2109_10, D.2110_11>;
83 : D.2115_14 = b;
84 : D.2125_17 = MIN_EXPR <D.2115_14, D.2114_31>;
85 : D.2119_16 = this_1(D)->a0;
86 : D.2124_32 = MIN_EXPR <D.2110_11, D.2119_16>;
87 : D.2076_33 = MAX_EXPR <D.2125_17, D.2124_32>;
88 :
89 : The pass does a dominator walk processing loads using a basic-block
90 : local analysis and stores the result for use by transformations on
91 : dominated basic-blocks. */
92 :
93 :
94 : /* Structure to keep track of the value of a dereferenced PHI result
95 : and the virtual operand used for that dereference. */
96 :
97 : struct phiprop_d
98 : {
99 : tree value;
100 : tree vuse;
101 : };
102 :
103 : /* Insert a new phi node for the dereference of PHI at basic_block
104 : BB with the virtual operands from USE_STMT. The vuse for
105 : the load will be set to OTHER_VUSE unless there is virtual op
106 : phi for BB. */
107 :
108 : static tree
109 15653 : phiprop_insert_phi (basic_block bb, gphi *phi, gimple *use_stmt,
110 : struct phiprop_d *phivn, size_t n,
111 : bitmap dce_ssa_names, tree other_vuse)
112 : {
113 15653 : tree res;
114 15653 : gphi *new_phi = NULL;
115 15653 : edge_iterator ei;
116 15653 : edge e;
117 15653 : tree phi_result = PHI_RESULT (phi);
118 15653 : bitmap_set_bit (dce_ssa_names, SSA_NAME_VERSION (phi_result));
119 :
120 15653 : gcc_assert (is_gimple_assign (use_stmt)
121 : && gimple_assign_rhs_code (use_stmt) == MEM_REF);
122 :
123 : /* Build a new PHI node to replace the definition of
124 : the indirect reference lhs. */
125 15653 : res = gimple_assign_lhs (use_stmt);
126 15653 : if (TREE_CODE (res) == SSA_NAME)
127 15512 : new_phi = create_phi_node (res, bb);
128 :
129 15653 : if (dump_file && (dump_flags & TDF_DETAILS))
130 : {
131 26 : fprintf (dump_file, "Inserting PHI for result of load ");
132 26 : print_gimple_stmt (dump_file, use_stmt, 0);
133 : }
134 :
135 15653 : gphi *vphi = get_virtual_phi (bb);
136 :
137 : /* Add PHI arguments for each edge inserting loads of the
138 : addressable operands. */
139 45493 : FOR_EACH_EDGE (e, ei, bb->preds)
140 : {
141 29840 : tree old_arg, new_var;
142 29840 : gassign *tmp;
143 29840 : location_t locus;
144 :
145 29840 : old_arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
146 29840 : locus = gimple_phi_arg_location_from_edge (phi, e);
147 29840 : while (TREE_CODE (old_arg) == SSA_NAME
148 31149 : && (SSA_NAME_VERSION (old_arg) >= n
149 1411 : || phivn[SSA_NAME_VERSION (old_arg)].value == NULL_TREE))
150 : {
151 1309 : gimple *def_stmt = SSA_NAME_DEF_STMT (old_arg);
152 1309 : old_arg = gimple_assign_rhs1 (def_stmt);
153 1309 : locus = gimple_location (def_stmt);
154 : }
155 :
156 29840 : if (TREE_CODE (old_arg) == SSA_NAME)
157 : {
158 102 : if (dump_file && (dump_flags & TDF_DETAILS))
159 : {
160 6 : fprintf (dump_file, " for edge defining ");
161 6 : print_generic_expr (dump_file, PHI_ARG_DEF_FROM_EDGE (phi, e));
162 6 : fprintf (dump_file, " reusing PHI result ");
163 12 : print_generic_expr (dump_file,
164 6 : phivn[SSA_NAME_VERSION (old_arg)].value);
165 6 : fprintf (dump_file, "\n");
166 : }
167 : /* Reuse a formerly created dereference. */
168 102 : new_var = phivn[SSA_NAME_VERSION (old_arg)].value;
169 : }
170 : else
171 : {
172 29738 : tree rhs = gimple_assign_rhs1 (use_stmt);
173 29738 : gcc_assert (TREE_CODE (old_arg) == ADDR_EXPR);
174 29738 : tree vuse = NULL_TREE;
175 29738 : if (TREE_CODE (res) == SSA_NAME)
176 : {
177 29394 : new_var = make_ssa_name (TREE_TYPE (rhs));
178 29394 : if (vphi)
179 120 : vuse = PHI_ARG_DEF_FROM_EDGE (vphi, e);
180 : else
181 : vuse = other_vuse;
182 : }
183 : else
184 : /* For the aggregate copy case updating virtual operands
185 : we'd have to possibly insert a virtual PHI and we have
186 : to split the existing VUSE lifetime. Leave that to
187 : the generic SSA updating. */
188 344 : new_var = unshare_expr (res);
189 29738 : if (!is_gimple_min_invariant (old_arg))
190 1286 : old_arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
191 : else
192 28452 : old_arg = unshare_expr (old_arg);
193 29738 : tmp = gimple_build_assign (new_var,
194 29738 : fold_build2 (MEM_REF, TREE_TYPE (rhs),
195 : old_arg,
196 : TREE_OPERAND (rhs, 1)));
197 29738 : gimple_set_location (tmp, locus);
198 29738 : if (vuse)
199 29394 : gimple_set_vuse (tmp, vuse);
200 :
201 29738 : gsi_insert_on_edge (e, tmp);
202 29738 : update_stmt (tmp);
203 :
204 29738 : if (dump_file && (dump_flags & TDF_DETAILS))
205 : {
206 46 : fprintf (dump_file, " for edge defining ");
207 46 : print_generic_expr (dump_file, PHI_ARG_DEF_FROM_EDGE (phi, e));
208 46 : fprintf (dump_file, " inserting load ");
209 46 : print_gimple_stmt (dump_file, tmp, 0);
210 : }
211 : }
212 :
213 29840 : if (new_phi)
214 29496 : add_phi_arg (new_phi, new_var, e, locus);
215 : }
216 :
217 15653 : if (new_phi)
218 : {
219 15512 : update_stmt (new_phi);
220 :
221 15512 : if (dump_file && (dump_flags & TDF_DETAILS))
222 26 : print_gimple_stmt (dump_file, new_phi, 0);
223 : }
224 :
225 15653 : return res;
226 : }
227 :
228 : /* Verify if *idx is available at *DATA. */
229 :
230 : static bool
231 50 : chk_uses (tree, tree *idx, void *data)
232 : {
233 50 : basic_block dom = (basic_block) data;
234 50 : if (TREE_CODE (*idx) == SSA_NAME)
235 32 : return (SSA_NAME_IS_DEFAULT_DEF (*idx)
236 32 : || ! dominated_by_p (CDI_DOMINATORS,
237 18 : gimple_bb (SSA_NAME_DEF_STMT (*idx)), dom));
238 : return true;
239 : }
240 :
241 : /* Check if we can move the loads from LOAD_STMT.
242 : This is when the virtual use is the same as the
243 : one active at the start of BB which we know either
244 : from its virtual PHI def (VPHI) or from the common
245 : incoming VUSE (up_vuse). If neither is present
246 : make sure the def stmt of the virtual use is in a
247 : different basic block dominating BB. When the def
248 : is an edge-inserted one we know it dominates us.
249 : Returns the vuse to use for the inserting. NULL_TREE
250 : is returned when we can't do the insert. */
251 :
252 : static tree
253 16524 : can_handle_load (gimple *load_stmt,
254 : basic_block bb,
255 : gphi *vphi, tree up_vuse, bool aggregate)
256 : {
257 16524 : tree vuse = gimple_vuse (load_stmt);
258 : /* If the load does not have a store beforehand,
259 : then we can do the load in conditional. */
260 16524 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
261 : {
262 : /* For loads that have no stores before, there should be no
263 : vphi. */
264 1286 : gcc_checking_assert (!vphi);
265 : /* The common vuse is the same as the default or there is none. */
266 1286 : gcc_checking_assert (!up_vuse || up_vuse == vuse);
267 : return vuse;
268 : }
269 :
270 : /* If we have a vphi, then that needs to be end point.
271 : If we have a common incoming vuse, that needs to be the end point. */
272 15238 : tree expected_vuse = NULL_TREE;
273 15238 : if (vphi)
274 249 : expected_vuse = gimple_phi_result (vphi);
275 : else if (up_vuse)
276 : expected_vuse = up_vuse;
277 : /* Try to see if the store does not effect the load. */
278 15238 : gimple *other_store = SSA_NAME_DEF_STMT (vuse);
279 : /* For aggregates, skipping the store is too
280 : hard to handle as you need to check for loads
281 : and it is not worth the extra checks so just handle expected vuse
282 : and the dominated by case. */
283 15238 : if (aggregate)
284 : {
285 : /* If the vuse on the load is the same as the expected vuse,
286 : there are no stores inbetween. */
287 116 : if (vuse == expected_vuse)
288 : return vuse;
289 88 : if (expected_vuse)
290 : return NULL_TREE;
291 88 : if (gimple_bb (other_store) != bb
292 156 : && dominated_by_p (CDI_DOMINATORS,
293 68 : bb, gimple_bb (other_store)))
294 : return vuse;
295 20 : return NULL_TREE;
296 : }
297 :
298 : /* Skip over clobbers in the same bb as the use
299 : as they don't interfere with loads. */
300 15140 : while (!SSA_NAME_IS_DEFAULT_DEF (vuse)
301 15140 : && gimple_clobber_p (other_store)
302 15336 : && gimple_bb (other_store) == bb)
303 : {
304 18 : vuse = gimple_vuse (other_store);
305 18 : other_store = SSA_NAME_DEF_STMT (vuse);
306 : }
307 : /* If the load does not have a store beforehand,
308 : then we can do the load in conditional. */
309 15122 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
310 : {
311 : /* For loads that have no stores before, there should be no
312 : vphi. */
313 0 : gcc_checking_assert (!vphi);
314 : /* The common vuse is the same as the default or there is none. */
315 0 : gcc_checking_assert (!up_vuse || up_vuse == vuse);
316 : return vuse;
317 : }
318 :
319 : /* If the vuse on the load is the same as the expected vuse,
320 : there are no stores inbetween. */
321 15122 : if (vuse == expected_vuse)
322 : return vuse;
323 :
324 : /* Only handling the case where the store is in the same
325 : bb as the phi. */
326 14949 : if (gimple_bb (other_store) == bb)
327 : {
328 212 : tree src = gimple_assign_rhs1 (load_stmt);
329 212 : ao_ref read;
330 212 : ao_ref_init (&read, src);
331 212 : if (stmt_may_clobber_ref_p_1 (other_store, &read, false))
332 178 : return NULL_TREE;
333 108 : vuse = gimple_vuse (other_store);
334 : /* If that skipped store was the first store in program,
335 : then we can do the load conditional. */
336 108 : if (SSA_NAME_IS_DEFAULT_DEF (vuse))
337 : {
338 : /* For loads that have no stores before, there should be no
339 : vphi. */
340 0 : gcc_checking_assert (!vphi);
341 : /* The common vuse is the same as the default or there is none. */
342 0 : gcc_checking_assert (!up_vuse || up_vuse == vuse);
343 : return vuse;
344 : }
345 108 : other_store = SSA_NAME_DEF_STMT (vuse);
346 : /* If the new vuse (after skipping) is the same as expected
347 : then that is the vuse to return. */
348 108 : if (vuse == expected_vuse)
349 : return vuse;
350 101 : if (gimple_bb (other_store) == bb)
351 : return NULL_TREE;
352 : }
353 :
354 : /* If there was no an expected vuse then see if the vuse dominates the phi of
355 : the address. */
356 14771 : if (!expected_vuse
357 29388 : && dominated_by_p (CDI_DOMINATORS,
358 14617 : bb, gimple_bb (other_store)))
359 : return vuse;
360 :
361 : return NULL_TREE;
362 : }
363 :
364 : /* Propagate between the phi node arguments of PHI in BB and phi result
365 : users. For now this matches
366 : # p_2 = PHI <&x, &y>
367 : <Lx>:;
368 : p_3 = p_2;
369 : z_2 = *p_3;
370 : and converts it to
371 : # z_2 = PHI <x, y>
372 : <Lx>:;
373 : Returns true if a transformation was done and edge insertions
374 : need to be committed. Global data PHIVN and N is used to track
375 : past transformation results. VPHI is the virtual PHI node in BB
376 : if there is one. We need to be especially careful here
377 : with aliasing issues as we are moving memory reads. */
378 :
379 : static bool
380 7577869 : propagate_with_phi (basic_block bb, gphi *vphi, gphi *phi,
381 : struct phiprop_d *phivn, size_t n, bitmap dce_ssa_names)
382 : {
383 7577869 : tree ptr = PHI_RESULT (phi);
384 7577869 : gimple *use_stmt;
385 7577869 : tree res = NULL_TREE;
386 7577869 : gimple_stmt_iterator gsi;
387 7577869 : imm_use_iterator ui;
388 7577869 : use_operand_p arg_p, use;
389 7577869 : ssa_op_iter i;
390 7577869 : bool phi_inserted;
391 7577869 : bool changed;
392 7577869 : tree type = NULL_TREE;
393 :
394 14444576 : if (!POINTER_TYPE_P (TREE_TYPE (ptr))
395 7604558 : || (!is_gimple_reg_type (TREE_TYPE (TREE_TYPE (ptr)))
396 348428 : && TYPE_MODE (TREE_TYPE (TREE_TYPE (ptr))) == BLKmode))
397 7096112 : return false;
398 :
399 481757 : tree up_vuse = NULL_TREE;
400 481757 : bool canpossible_trap = false;
401 : /* Check if we can "cheaply" dereference all phi arguments. */
402 592743 : FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
403 : {
404 562234 : tree arg = USE_FROM_PTR (arg_p);
405 : /* Walk the ssa chain until we reach a ssa name we already
406 : created a value for or we reach a definition of the form
407 : ssa_name_n = &var; */
408 562234 : while (TREE_CODE (arg) == SSA_NAME
409 460207 : && !SSA_NAME_IS_DEFAULT_DEF (arg)
410 1172244 : && (SSA_NAME_VERSION (arg) >= n
411 406598 : || phivn[SSA_NAME_VERSION (arg)].value == NULL_TREE))
412 : {
413 406491 : gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
414 406491 : if (!gimple_assign_single_p (def_stmt))
415 : return false;
416 203402 : arg = gimple_assign_rhs1 (def_stmt);
417 : }
418 359145 : if (TREE_CODE (arg) == ADDR_EXPR)
419 : {
420 110870 : tree decl = TREE_OPERAND (arg, 0);
421 110870 : if (!canpossible_trap)
422 107991 : canpossible_trap = tree_could_trap_p (decl);
423 : }
424 : /* When we have an SSA name see if we previously encountered a
425 : dereference of it. */
426 248275 : else if (TREE_CODE (arg) == SSA_NAME
427 53716 : && SSA_NAME_VERSION (arg) < n
428 53716 : && phivn[SSA_NAME_VERSION (arg)].value != NULL_TREE
429 248392 : && (!type
430 3 : || types_compatible_p
431 3 : (type, TREE_TYPE (phivn[SSA_NAME_VERSION (arg)].value))))
432 : {
433 : /* The dereference should be under the VUSE that's active in BB.
434 : If the BB has no virtual PHI then record the common "incoming"
435 : vuse. */
436 117 : if (vphi)
437 2 : up_vuse = gimple_phi_arg_def (vphi, phi_arg_index_from_use (arg_p));
438 117 : if (!up_vuse)
439 112 : up_vuse = phivn[SSA_NAME_VERSION (arg)].vuse;
440 5 : else if (up_vuse != phivn[SSA_NAME_VERSION (arg)].vuse)
441 : return false;
442 : }
443 : else
444 248158 : return false;
445 110986 : if (!type
446 110924 : && TREE_CODE (arg) == SSA_NAME)
447 113 : type = TREE_TYPE (phivn[SSA_NAME_VERSION (arg)].value);
448 : }
449 :
450 : /* Find a dereferencing use. First follow (single use) ssa
451 : copy chains for ptr. */
452 31605 : while (single_imm_use (ptr, &use, &use_stmt)
453 31605 : && gimple_assign_ssa_name_copy_p (use_stmt))
454 1096 : ptr = gimple_assign_lhs (use_stmt);
455 :
456 : /* Replace the first dereference of *ptr if there is one and if we
457 : can move the loads to the place of the ptr phi node. */
458 30509 : phi_inserted = false;
459 30509 : changed = false;
460 30509 : auto_vec<gimple*> delayed_uses;
461 73981 : FOR_EACH_IMM_USE_STMT (use_stmt, ui, ptr)
462 : {
463 43472 : bool delay = false;
464 :
465 : /* Check whether this is a load of *ptr. */
466 43472 : if (!(is_gimple_assign (use_stmt)
467 23246 : && gimple_assign_rhs_code (use_stmt) == MEM_REF
468 16778 : && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == ptr
469 16778 : && integer_zerop (TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 1))
470 16762 : && (!type
471 119 : || types_compatible_p
472 119 : (TREE_TYPE (gimple_assign_lhs (use_stmt)), 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 16762 : && !stmt_can_throw_internal (cfun, use_stmt)
481 33072 : && !gimple_has_volatile_ops (use_stmt)))
482 26948 : continue;
483 :
484 16524 : bool aggregate = false;
485 16524 : if (!is_gimple_reg_type (TREE_TYPE (gimple_assign_lhs (use_stmt))))
486 209 : aggregate = true;
487 :
488 16524 : tree other_vuse;
489 16524 : other_vuse = can_handle_load (use_stmt, bb, vphi, up_vuse, aggregate);
490 16524 : if (!other_vuse)
491 548 : continue;
492 :
493 15976 : if ((canpossible_trap || aggregate)
494 15976 : && !dom_info_available_p (cfun, CDI_POST_DOMINATORS))
495 545 : 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 15976 : if ((canpossible_trap || aggregate)
501 15976 : && !dominated_by_p (CDI_POST_DOMINATORS,
502 2386 : 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 15976 : if (!(gimple_bb (use_stmt) == bb
509 297 : || (!(bb->flags & BB_IRREDUCIBLE_LOOP)
510 297 : && !(gimple_bb (use_stmt)->flags & BB_IRREDUCIBLE_LOOP)
511 297 : && (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 15976 : if (aggregate)
519 : {
520 : /* aggregate copies are too hard to handled if delayed. */
521 189 : if (delay)
522 48 : goto next;
523 346 : 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 173 : if (! for_each_index (gimple_assign_lhs_ptr (use_stmt),
529 : chk_uses,
530 173 : get_immediate_dominator (CDI_DOMINATORS,
531 : gimple_bb (phi))))
532 18 : goto next;
533 :
534 155 : gimple *vuse_stmt;
535 155 : imm_use_iterator vui;
536 155 : use_operand_p vuse_p;
537 155 : 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 914 : FOR_EACH_IMM_USE_FAST (vuse_p, vui, vuse)
547 : {
548 618 : vuse_stmt = USE_STMT (vuse_p);
549 618 : if (vuse_stmt == use_stmt)
550 149 : continue;
551 469 : if (!gimple_bb (vuse_stmt)
552 914 : || !dominated_by_p (CDI_DOMINATORS,
553 445 : gimple_bb (vuse_stmt), bb))
554 455 : 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 141 : 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 depedencies
564 : will be removed later via simple_dce_from_worklist. */
565 141 : 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 141 : unlink_stmt_vdef (use_stmt);
575 141 : gsi_remove (&gsi, true);
576 :
577 141 : changed = true;
578 : }
579 : /* Further replacements are easy, just make a copy out of the
580 : load. */
581 15787 : 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 15787 : else if (delay)
588 275 : 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 15512 : tree vuse = gimple_vuse (use_stmt);
594 15512 : res = phiprop_insert_phi (bb, phi, use_stmt, phivn, n,
595 : dce_ssa_names, other_vuse);
596 15512 : type = TREE_TYPE (res);
597 :
598 : /* Remember the value we created for *ptr. */
599 15512 : phivn[SSA_NAME_VERSION (ptr)].value = res;
600 15512 : phivn[SSA_NAME_VERSION (ptr)].vuse = vuse;
601 :
602 : /* Remove old stmt. The phi and all of maybe its depedencies
603 : will be removed later via simple_dce_from_worklist. */
604 15512 : gsi = gsi_for_stmt (use_stmt);
605 15512 : gsi_remove (&gsi, true);
606 :
607 15512 : phi_inserted = true;
608 15512 : changed = true;
609 : }
610 :
611 43472 : next:;
612 : /* Continue searching for a proper dereference. */
613 30509 : }
614 :
615 : /* Update the delayed uses if there is any
616 : as now we know this is safe to do. */
617 30509 : if (phi_inserted)
618 16052 : 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 30509 : return changed;
628 30509 : }
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 576094 : pass_phiprop (gcc::context *ctxt)
651 1152188 : : gimple_opt_pass (pass_data_phiprop, ctxt)
652 : {}
653 :
654 : /* opt_pass methods: */
655 288047 : opt_pass * clone () final override { return new pass_phiprop (m_ctxt); }
656 3451265 : 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 3451058 : pass_phiprop::execute (function *fun)
663 : {
664 3451058 : struct phiprop_d *phivn;
665 3451058 : bool did_something = false;
666 3451058 : basic_block bb;
667 3451058 : gphi_iterator gsi;
668 3451058 : unsigned i;
669 3451058 : size_t n;
670 3451058 : auto_bitmap dce_ssa_names;
671 :
672 3451058 : calculate_dominance_info (CDI_DOMINATORS);
673 :
674 3451058 : n = num_ssa_names;
675 3451058 : phivn = XCNEWVEC (struct phiprop_d, n);
676 :
677 : /* Walk the dominator tree in preorder. */
678 3451058 : auto_vec<basic_block> bbs
679 : = get_all_dominated_blocks (CDI_DOMINATORS,
680 3451058 : single_succ (ENTRY_BLOCK_PTR_FOR_FN (fun)));
681 28360720 : 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 24909662 : if (bb_has_abnormal_pred (bb))
686 8588 : continue;
687 24901074 : gphi *vphi = get_virtual_phi (bb);
688 32478943 : for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
689 7577869 : did_something |= propagate_with_phi (bb, vphi, gsi.phi (),
690 : phivn, n, dce_ssa_names);
691 : }
692 :
693 3451058 : if (did_something)
694 : {
695 13274 : gsi_commit_edge_inserts ();
696 13274 : simple_dce_from_worklist (dce_ssa_names);
697 : }
698 :
699 3451058 : free (phivn);
700 :
701 3451058 : free_dominance_info (CDI_POST_DOMINATORS);
702 :
703 3451058 : return did_something ? TODO_update_ssa_only_virtuals : 0;
704 3451058 : }
705 :
706 : } // anon namespace
707 :
708 : gimple_opt_pass *
709 288047 : make_pass_phiprop (gcc::context *ctxt)
710 : {
711 288047 : return new pass_phiprop (ctxt);
712 : }
|