PR middle-end/59175
[official-gcc.git] / gcc / tree-ssa-phiprop.c
blob070b8ed3f0bf773dd8c126d04be9a5b2d6f25106
1 /* Backward propagation of indirect loads through PHIs.
2 Copyright (C) 2007-2013 Free Software Foundation, Inc.
3 Contributed by Richard Guenther <rguenther@suse.de>
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
29 #include "gimple.h"
30 #include "gimplify.h"
31 #include "gimple-iterator.h"
32 #include "gimple-ssa.h"
33 #include "tree-phinodes.h"
34 #include "ssa-iterators.h"
35 #include "tree-ssanames.h"
36 #include "tree-pass.h"
37 #include "langhooks.h"
38 #include "flags.h"
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.
44 For example the pass changes
46 # addr_1 = PHI <&a, &b>
47 tmp_1 = *addr_1;
51 # tmp_1 = PHI <a, b>
53 but also handles more complex scenarios like
55 D.2077_2 = &this_1(D)->a1;
56 ...
58 # b_12 = PHI <&c(2), D.2077_2(3)>
59 D.2114_13 = *b_12;
60 ...
62 # b_15 = PHI <b_12(4), &b(5)>
63 D.2080_5 = &this_1(D)->a0;
64 ...
66 # b_18 = PHI <D.2080_5(6), &c(7)>
67 ...
69 # b_21 = PHI <b_15(8), b_18(9)>
70 D.2076_8 = *b_21;
72 where the addresses loaded are defined by PHIs itself.
73 The above happens for
75 std::max(std::min(a0, c), std::min(std::max(a1, c), b))
77 where this pass transforms it to a form later PHI optimization
78 recognizes and transforms it to the simple
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>;
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. */
94 /* Structure to keep track of the value of a dereferenced PHI result
95 and the virtual operand used for that dereference. */
97 struct phiprop_d
99 tree value;
100 tree vuse;
103 /* Verify if the value recorded for NAME in PHIVN is still valid at
104 the start of basic block BB. */
106 static bool
107 phivn_valid_p (struct phiprop_d *phivn, tree name, basic_block bb)
109 tree vuse = phivn[SSA_NAME_VERSION (name)].vuse;
110 gimple use_stmt;
111 imm_use_iterator ui2;
112 bool ok = true;
114 /* The def stmts of the virtual uses need to be dominated by bb. */
115 gcc_assert (vuse != NULL_TREE);
117 FOR_EACH_IMM_USE_STMT (use_stmt, ui2, vuse)
119 /* If BB does not dominate a VDEF, the value is invalid. */
120 if ((gimple_vdef (use_stmt) != NULL_TREE
121 || gimple_code (use_stmt) == GIMPLE_PHI)
122 && !dominated_by_p (CDI_DOMINATORS, gimple_bb (use_stmt), bb))
124 ok = false;
125 BREAK_FROM_IMM_USE_STMT (ui2);
129 return ok;
132 /* Insert a new phi node for the dereference of PHI at basic_block
133 BB with the virtual operands from USE_STMT. */
135 static tree
136 phiprop_insert_phi (basic_block bb, gimple phi, gimple use_stmt,
137 struct phiprop_d *phivn, size_t n)
139 tree res;
140 gimple new_phi;
141 edge_iterator ei;
142 edge e;
144 gcc_assert (is_gimple_assign (use_stmt)
145 && gimple_assign_rhs_code (use_stmt) == MEM_REF);
147 /* Build a new PHI node to replace the definition of
148 the indirect reference lhs. */
149 res = gimple_assign_lhs (use_stmt);
150 new_phi = create_phi_node (res, bb);
152 if (dump_file && (dump_flags & TDF_DETAILS))
154 fprintf (dump_file, "Inserting PHI for result of load ");
155 print_gimple_stmt (dump_file, use_stmt, 0, 0);
158 /* Add PHI arguments for each edge inserting loads of the
159 addressable operands. */
160 FOR_EACH_EDGE (e, ei, bb->preds)
162 tree old_arg, new_var;
163 gimple tmp;
164 source_location locus;
166 old_arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
167 locus = gimple_phi_arg_location_from_edge (phi, e);
168 while (TREE_CODE (old_arg) == SSA_NAME
169 && (SSA_NAME_VERSION (old_arg) >= n
170 || phivn[SSA_NAME_VERSION (old_arg)].value == NULL_TREE))
172 gimple def_stmt = SSA_NAME_DEF_STMT (old_arg);
173 old_arg = gimple_assign_rhs1 (def_stmt);
174 locus = gimple_location (def_stmt);
177 if (TREE_CODE (old_arg) == SSA_NAME)
179 if (dump_file && (dump_flags & TDF_DETAILS))
181 fprintf (dump_file, " for edge defining ");
182 print_generic_expr (dump_file, PHI_ARG_DEF_FROM_EDGE (phi, e), 0);
183 fprintf (dump_file, " reusing PHI result ");
184 print_generic_expr (dump_file,
185 phivn[SSA_NAME_VERSION (old_arg)].value, 0);
186 fprintf (dump_file, "\n");
188 /* Reuse a formerly created dereference. */
189 new_var = phivn[SSA_NAME_VERSION (old_arg)].value;
191 else
193 tree rhs = gimple_assign_rhs1 (use_stmt);
194 gcc_assert (TREE_CODE (old_arg) == ADDR_EXPR);
195 new_var = make_ssa_name (TREE_TYPE (rhs), NULL);
196 if (!is_gimple_min_invariant (old_arg))
197 old_arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
198 else
199 old_arg = unshare_expr (old_arg);
200 tmp = gimple_build_assign (new_var,
201 fold_build2 (MEM_REF, TREE_TYPE (rhs),
202 old_arg,
203 TREE_OPERAND (rhs, 1)));
204 gimple_set_location (tmp, locus);
206 gsi_insert_on_edge (e, tmp);
207 update_stmt (tmp);
209 if (dump_file && (dump_flags & TDF_DETAILS))
211 fprintf (dump_file, " for edge defining ");
212 print_generic_expr (dump_file, PHI_ARG_DEF_FROM_EDGE (phi, e), 0);
213 fprintf (dump_file, " inserting load ");
214 print_gimple_stmt (dump_file, tmp, 0, 0);
218 add_phi_arg (new_phi, new_var, e, locus);
221 update_stmt (new_phi);
223 if (dump_file && (dump_flags & TDF_DETAILS))
224 print_gimple_stmt (dump_file, new_phi, 0, 0);
226 return res;
229 /* Propagate between the phi node arguments of PHI in BB and phi result
230 users. For now this matches
231 # p_2 = PHI <&x, &y>
232 <Lx>:;
233 p_3 = p_2;
234 z_2 = *p_3;
235 and converts it to
236 # z_2 = PHI <x, y>
237 <Lx>:;
238 Returns true if a transformation was done and edge insertions
239 need to be committed. Global data PHIVN and N is used to track
240 past transformation results. We need to be especially careful here
241 with aliasing issues as we are moving memory reads. */
243 static bool
244 propagate_with_phi (basic_block bb, gimple phi, struct phiprop_d *phivn,
245 size_t n)
247 tree ptr = PHI_RESULT (phi);
248 gimple use_stmt;
249 tree res = NULL_TREE;
250 gimple_stmt_iterator gsi;
251 imm_use_iterator ui;
252 use_operand_p arg_p, use;
253 ssa_op_iter i;
254 bool phi_inserted;
255 tree type = NULL_TREE;
257 if (!POINTER_TYPE_P (TREE_TYPE (ptr))
258 || !is_gimple_reg_type (TREE_TYPE (TREE_TYPE (ptr))))
259 return false;
261 /* Check if we can "cheaply" dereference all phi arguments. */
262 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
264 tree arg = USE_FROM_PTR (arg_p);
265 /* Walk the ssa chain until we reach a ssa name we already
266 created a value for or we reach a definition of the form
267 ssa_name_n = &var; */
268 while (TREE_CODE (arg) == SSA_NAME
269 && !SSA_NAME_IS_DEFAULT_DEF (arg)
270 && (SSA_NAME_VERSION (arg) >= n
271 || phivn[SSA_NAME_VERSION (arg)].value == NULL_TREE))
273 gimple def_stmt = SSA_NAME_DEF_STMT (arg);
274 if (!gimple_assign_single_p (def_stmt))
275 return false;
276 arg = gimple_assign_rhs1 (def_stmt);
278 if (TREE_CODE (arg) != ADDR_EXPR
279 && !(TREE_CODE (arg) == SSA_NAME
280 && SSA_NAME_VERSION (arg) < n
281 && phivn[SSA_NAME_VERSION (arg)].value != NULL_TREE
282 && (!type
283 || types_compatible_p
284 (type, TREE_TYPE (phivn[SSA_NAME_VERSION (arg)].value)))
285 && phivn_valid_p (phivn, arg, bb)))
286 return false;
287 if (!type
288 && TREE_CODE (arg) == SSA_NAME)
289 type = TREE_TYPE (phivn[SSA_NAME_VERSION (arg)].value);
292 /* Find a dereferencing use. First follow (single use) ssa
293 copy chains for ptr. */
294 while (single_imm_use (ptr, &use, &use_stmt)
295 && gimple_assign_ssa_name_copy_p (use_stmt))
296 ptr = gimple_assign_lhs (use_stmt);
298 /* Replace the first dereference of *ptr if there is one and if we
299 can move the loads to the place of the ptr phi node. */
300 phi_inserted = false;
301 FOR_EACH_IMM_USE_STMT (use_stmt, ui, ptr)
303 gimple def_stmt;
304 tree vuse;
306 /* Check whether this is a load of *ptr. */
307 if (!(is_gimple_assign (use_stmt)
308 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
309 && gimple_assign_rhs_code (use_stmt) == MEM_REF
310 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == ptr
311 && integer_zerop (TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 1))
312 && (!type
313 || types_compatible_p
314 (TREE_TYPE (gimple_assign_lhs (use_stmt)), type))
315 /* We cannot replace a load that may throw or is volatile. */
316 && !stmt_can_throw_internal (use_stmt)))
317 continue;
319 /* Check if we can move the loads. The def stmt of the virtual use
320 needs to be in a different basic block dominating bb. */
321 vuse = gimple_vuse (use_stmt);
322 def_stmt = SSA_NAME_DEF_STMT (vuse);
323 if (!SSA_NAME_IS_DEFAULT_DEF (vuse)
324 && (gimple_bb (def_stmt) == bb
325 || !dominated_by_p (CDI_DOMINATORS,
326 bb, gimple_bb (def_stmt))))
327 goto next;
329 /* Found a proper dereference. Insert a phi node if this
330 is the first load transformation. */
331 if (!phi_inserted)
333 res = phiprop_insert_phi (bb, phi, use_stmt, phivn, n);
334 type = TREE_TYPE (res);
336 /* Remember the value we created for *ptr. */
337 phivn[SSA_NAME_VERSION (ptr)].value = res;
338 phivn[SSA_NAME_VERSION (ptr)].vuse = vuse;
340 /* Remove old stmt. The phi is taken care of by DCE, if we
341 want to delete it here we also have to delete all intermediate
342 copies. */
343 gsi = gsi_for_stmt (use_stmt);
344 gsi_remove (&gsi, true);
346 phi_inserted = true;
348 else
350 /* Further replacements are easy, just make a copy out of the
351 load. */
352 gimple_assign_set_rhs1 (use_stmt, res);
353 update_stmt (use_stmt);
356 next:;
357 /* Continue searching for a proper dereference. */
360 return phi_inserted;
363 /* Main entry for phiprop pass. */
365 static unsigned int
366 tree_ssa_phiprop (void)
368 vec<basic_block> bbs;
369 struct phiprop_d *phivn;
370 bool did_something = false;
371 basic_block bb;
372 gimple_stmt_iterator gsi;
373 unsigned i;
374 size_t n;
376 calculate_dominance_info (CDI_DOMINATORS);
378 n = num_ssa_names;
379 phivn = XCNEWVEC (struct phiprop_d, n);
381 /* Walk the dominator tree in preorder. */
382 bbs = get_all_dominated_blocks (CDI_DOMINATORS,
383 single_succ (ENTRY_BLOCK_PTR));
384 FOR_EACH_VEC_ELT (bbs, i, bb)
385 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
386 did_something |= propagate_with_phi (bb, gsi_stmt (gsi), phivn, n);
388 if (did_something)
389 gsi_commit_edge_inserts ();
391 bbs.release ();
392 free (phivn);
394 return 0;
397 static bool
398 gate_phiprop (void)
400 return flag_tree_phiprop;
403 namespace {
405 const pass_data pass_data_phiprop =
407 GIMPLE_PASS, /* type */
408 "phiprop", /* name */
409 OPTGROUP_NONE, /* optinfo_flags */
410 true, /* has_gate */
411 true, /* has_execute */
412 TV_TREE_PHIPROP, /* tv_id */
413 ( PROP_cfg | PROP_ssa ), /* properties_required */
414 0, /* properties_provided */
415 0, /* properties_destroyed */
416 0, /* todo_flags_start */
417 ( TODO_update_ssa | TODO_verify_ssa ), /* todo_flags_finish */
420 class pass_phiprop : public gimple_opt_pass
422 public:
423 pass_phiprop (gcc::context *ctxt)
424 : gimple_opt_pass (pass_data_phiprop, ctxt)
427 /* opt_pass methods: */
428 bool gate () { return gate_phiprop (); }
429 unsigned int execute () { return tree_ssa_phiprop (); }
431 }; // class pass_phiprop
433 } // anon namespace
435 gimple_opt_pass *
436 make_pass_phiprop (gcc::context *ctxt)
438 return new pass_phiprop (ctxt);