Daily bump.
[official-gcc.git] / gcc / tree-ssa-phiprop.c
blob4d66c12a6062f13ec1fa9b7100774d7193d7f16b
1 /* Backward propagation of indirect loads through PHIs.
2 Copyright (C) 2007-2014 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 "tree-ssa-alias.h"
30 #include "internal-fn.h"
31 #include "tree-eh.h"
32 #include "gimple-expr.h"
33 #include "is-a.h"
34 #include "gimple.h"
35 #include "gimplify.h"
36 #include "gimple-iterator.h"
37 #include "gimple-ssa.h"
38 #include "tree-phinodes.h"
39 #include "ssa-iterators.h"
40 #include "stringpool.h"
41 #include "tree-ssanames.h"
42 #include "tree-pass.h"
43 #include "langhooks.h"
44 #include "flags.h"
46 /* This pass propagates indirect loads through the PHI node for its
47 address to make the load source possibly non-addressable and to
48 allow for PHI optimization to trigger.
50 For example the pass changes
52 # addr_1 = PHI <&a, &b>
53 tmp_1 = *addr_1;
57 # tmp_1 = PHI <a, b>
59 but also handles more complex scenarios like
61 D.2077_2 = &this_1(D)->a1;
62 ...
64 # b_12 = PHI <&c(2), D.2077_2(3)>
65 D.2114_13 = *b_12;
66 ...
68 # b_15 = PHI <b_12(4), &b(5)>
69 D.2080_5 = &this_1(D)->a0;
70 ...
72 # b_18 = PHI <D.2080_5(6), &c(7)>
73 ...
75 # b_21 = PHI <b_15(8), b_18(9)>
76 D.2076_8 = *b_21;
78 where the addresses loaded are defined by PHIs itself.
79 The above happens for
81 std::max(std::min(a0, c), std::min(std::max(a1, c), b))
83 where this pass transforms it to a form later PHI optimization
84 recognizes and transforms it to the simple
86 D.2109_10 = this_1(D)->a1;
87 D.2110_11 = c;
88 D.2114_31 = MAX_EXPR <D.2109_10, D.2110_11>;
89 D.2115_14 = b;
90 D.2125_17 = MIN_EXPR <D.2115_14, D.2114_31>;
91 D.2119_16 = this_1(D)->a0;
92 D.2124_32 = MIN_EXPR <D.2110_11, D.2119_16>;
93 D.2076_33 = MAX_EXPR <D.2125_17, D.2124_32>;
95 The pass does a dominator walk processing loads using a basic-block
96 local analysis and stores the result for use by transformations on
97 dominated basic-blocks. */
100 /* Structure to keep track of the value of a dereferenced PHI result
101 and the virtual operand used for that dereference. */
103 struct phiprop_d
105 tree value;
106 tree vuse;
109 /* Verify if the value recorded for NAME in PHIVN is still valid at
110 the start of basic block BB. */
112 static bool
113 phivn_valid_p (struct phiprop_d *phivn, tree name, basic_block bb)
115 tree vuse = phivn[SSA_NAME_VERSION (name)].vuse;
116 gimple use_stmt;
117 imm_use_iterator ui2;
118 bool ok = true;
120 /* The def stmts of the virtual uses need to be dominated by bb. */
121 gcc_assert (vuse != NULL_TREE);
123 FOR_EACH_IMM_USE_STMT (use_stmt, ui2, vuse)
125 /* If BB does not dominate a VDEF, the value is invalid. */
126 if ((gimple_vdef (use_stmt) != NULL_TREE
127 || gimple_code (use_stmt) == GIMPLE_PHI)
128 && !dominated_by_p (CDI_DOMINATORS, gimple_bb (use_stmt), bb))
130 ok = false;
131 BREAK_FROM_IMM_USE_STMT (ui2);
135 return ok;
138 /* Insert a new phi node for the dereference of PHI at basic_block
139 BB with the virtual operands from USE_STMT. */
141 static tree
142 phiprop_insert_phi (basic_block bb, gimple phi, gimple use_stmt,
143 struct phiprop_d *phivn, size_t n)
145 tree res;
146 gimple new_phi;
147 edge_iterator ei;
148 edge e;
150 gcc_assert (is_gimple_assign (use_stmt)
151 && gimple_assign_rhs_code (use_stmt) == MEM_REF);
153 /* Build a new PHI node to replace the definition of
154 the indirect reference lhs. */
155 res = gimple_assign_lhs (use_stmt);
156 new_phi = create_phi_node (res, bb);
158 if (dump_file && (dump_flags & TDF_DETAILS))
160 fprintf (dump_file, "Inserting PHI for result of load ");
161 print_gimple_stmt (dump_file, use_stmt, 0, 0);
164 /* Add PHI arguments for each edge inserting loads of the
165 addressable operands. */
166 FOR_EACH_EDGE (e, ei, bb->preds)
168 tree old_arg, new_var;
169 gimple tmp;
170 source_location locus;
172 old_arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
173 locus = gimple_phi_arg_location_from_edge (phi, e);
174 while (TREE_CODE (old_arg) == SSA_NAME
175 && (SSA_NAME_VERSION (old_arg) >= n
176 || phivn[SSA_NAME_VERSION (old_arg)].value == NULL_TREE))
178 gimple def_stmt = SSA_NAME_DEF_STMT (old_arg);
179 old_arg = gimple_assign_rhs1 (def_stmt);
180 locus = gimple_location (def_stmt);
183 if (TREE_CODE (old_arg) == SSA_NAME)
185 if (dump_file && (dump_flags & TDF_DETAILS))
187 fprintf (dump_file, " for edge defining ");
188 print_generic_expr (dump_file, PHI_ARG_DEF_FROM_EDGE (phi, e), 0);
189 fprintf (dump_file, " reusing PHI result ");
190 print_generic_expr (dump_file,
191 phivn[SSA_NAME_VERSION (old_arg)].value, 0);
192 fprintf (dump_file, "\n");
194 /* Reuse a formerly created dereference. */
195 new_var = phivn[SSA_NAME_VERSION (old_arg)].value;
197 else
199 tree rhs = gimple_assign_rhs1 (use_stmt);
200 gcc_assert (TREE_CODE (old_arg) == ADDR_EXPR);
201 new_var = make_ssa_name (TREE_TYPE (rhs), NULL);
202 if (!is_gimple_min_invariant (old_arg))
203 old_arg = PHI_ARG_DEF_FROM_EDGE (phi, e);
204 else
205 old_arg = unshare_expr (old_arg);
206 tmp = gimple_build_assign (new_var,
207 fold_build2 (MEM_REF, TREE_TYPE (rhs),
208 old_arg,
209 TREE_OPERAND (rhs, 1)));
210 gimple_set_location (tmp, locus);
212 gsi_insert_on_edge (e, tmp);
213 update_stmt (tmp);
215 if (dump_file && (dump_flags & TDF_DETAILS))
217 fprintf (dump_file, " for edge defining ");
218 print_generic_expr (dump_file, PHI_ARG_DEF_FROM_EDGE (phi, e), 0);
219 fprintf (dump_file, " inserting load ");
220 print_gimple_stmt (dump_file, tmp, 0, 0);
224 add_phi_arg (new_phi, new_var, e, locus);
227 update_stmt (new_phi);
229 if (dump_file && (dump_flags & TDF_DETAILS))
230 print_gimple_stmt (dump_file, new_phi, 0, 0);
232 return res;
235 /* Propagate between the phi node arguments of PHI in BB and phi result
236 users. For now this matches
237 # p_2 = PHI <&x, &y>
238 <Lx>:;
239 p_3 = p_2;
240 z_2 = *p_3;
241 and converts it to
242 # z_2 = PHI <x, y>
243 <Lx>:;
244 Returns true if a transformation was done and edge insertions
245 need to be committed. Global data PHIVN and N is used to track
246 past transformation results. We need to be especially careful here
247 with aliasing issues as we are moving memory reads. */
249 static bool
250 propagate_with_phi (basic_block bb, gimple phi, struct phiprop_d *phivn,
251 size_t n)
253 tree ptr = PHI_RESULT (phi);
254 gimple use_stmt;
255 tree res = NULL_TREE;
256 gimple_stmt_iterator gsi;
257 imm_use_iterator ui;
258 use_operand_p arg_p, use;
259 ssa_op_iter i;
260 bool phi_inserted;
261 tree type = NULL_TREE;
263 if (!POINTER_TYPE_P (TREE_TYPE (ptr))
264 || !is_gimple_reg_type (TREE_TYPE (TREE_TYPE (ptr))))
265 return false;
267 /* Check if we can "cheaply" dereference all phi arguments. */
268 FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
270 tree arg = USE_FROM_PTR (arg_p);
271 /* Walk the ssa chain until we reach a ssa name we already
272 created a value for or we reach a definition of the form
273 ssa_name_n = &var; */
274 while (TREE_CODE (arg) == SSA_NAME
275 && !SSA_NAME_IS_DEFAULT_DEF (arg)
276 && (SSA_NAME_VERSION (arg) >= n
277 || phivn[SSA_NAME_VERSION (arg)].value == NULL_TREE))
279 gimple def_stmt = SSA_NAME_DEF_STMT (arg);
280 if (!gimple_assign_single_p (def_stmt))
281 return false;
282 arg = gimple_assign_rhs1 (def_stmt);
284 if (TREE_CODE (arg) != ADDR_EXPR
285 && !(TREE_CODE (arg) == SSA_NAME
286 && SSA_NAME_VERSION (arg) < n
287 && phivn[SSA_NAME_VERSION (arg)].value != NULL_TREE
288 && (!type
289 || types_compatible_p
290 (type, TREE_TYPE (phivn[SSA_NAME_VERSION (arg)].value)))
291 && phivn_valid_p (phivn, arg, bb)))
292 return false;
293 if (!type
294 && TREE_CODE (arg) == SSA_NAME)
295 type = TREE_TYPE (phivn[SSA_NAME_VERSION (arg)].value);
298 /* Find a dereferencing use. First follow (single use) ssa
299 copy chains for ptr. */
300 while (single_imm_use (ptr, &use, &use_stmt)
301 && gimple_assign_ssa_name_copy_p (use_stmt))
302 ptr = gimple_assign_lhs (use_stmt);
304 /* Replace the first dereference of *ptr if there is one and if we
305 can move the loads to the place of the ptr phi node. */
306 phi_inserted = false;
307 FOR_EACH_IMM_USE_STMT (use_stmt, ui, ptr)
309 gimple def_stmt;
310 tree vuse;
312 /* Only replace loads in blocks that post-dominate the PHI node. That
313 makes sure we don't end up speculating loads. */
314 if (!dominated_by_p (CDI_POST_DOMINATORS,
315 bb, gimple_bb (use_stmt)))
316 continue;
318 /* Check whether this is a load of *ptr. */
319 if (!(is_gimple_assign (use_stmt)
320 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
321 && gimple_assign_rhs_code (use_stmt) == MEM_REF
322 && TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) == ptr
323 && integer_zerop (TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 1))
324 && (!type
325 || types_compatible_p
326 (TREE_TYPE (gimple_assign_lhs (use_stmt)), type))
327 /* We cannot replace a load that may throw or is volatile. */
328 && !stmt_can_throw_internal (use_stmt)))
329 continue;
331 /* Check if we can move the loads. The def stmt of the virtual use
332 needs to be in a different basic block dominating bb. */
333 vuse = gimple_vuse (use_stmt);
334 def_stmt = SSA_NAME_DEF_STMT (vuse);
335 if (!SSA_NAME_IS_DEFAULT_DEF (vuse)
336 && (gimple_bb (def_stmt) == bb
337 || !dominated_by_p (CDI_DOMINATORS,
338 bb, gimple_bb (def_stmt))))
339 goto next;
341 /* Found a proper dereference. Insert a phi node if this
342 is the first load transformation. */
343 if (!phi_inserted)
345 res = phiprop_insert_phi (bb, phi, use_stmt, phivn, n);
346 type = TREE_TYPE (res);
348 /* Remember the value we created for *ptr. */
349 phivn[SSA_NAME_VERSION (ptr)].value = res;
350 phivn[SSA_NAME_VERSION (ptr)].vuse = vuse;
352 /* Remove old stmt. The phi is taken care of by DCE, if we
353 want to delete it here we also have to delete all intermediate
354 copies. */
355 gsi = gsi_for_stmt (use_stmt);
356 gsi_remove (&gsi, true);
358 phi_inserted = true;
360 else
362 /* Further replacements are easy, just make a copy out of the
363 load. */
364 gimple_assign_set_rhs1 (use_stmt, res);
365 update_stmt (use_stmt);
368 next:;
369 /* Continue searching for a proper dereference. */
372 return phi_inserted;
375 /* Main entry for phiprop pass. */
377 static unsigned int
378 tree_ssa_phiprop (void)
380 vec<basic_block> bbs;
381 struct phiprop_d *phivn;
382 bool did_something = false;
383 basic_block bb;
384 gimple_stmt_iterator gsi;
385 unsigned i;
386 size_t n;
388 calculate_dominance_info (CDI_DOMINATORS);
389 calculate_dominance_info (CDI_POST_DOMINATORS);
391 n = num_ssa_names;
392 phivn = XCNEWVEC (struct phiprop_d, n);
394 /* Walk the dominator tree in preorder. */
395 bbs = get_all_dominated_blocks (CDI_DOMINATORS,
396 single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
397 FOR_EACH_VEC_ELT (bbs, i, bb)
398 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
399 did_something |= propagate_with_phi (bb, gsi_stmt (gsi), phivn, n);
401 if (did_something)
402 gsi_commit_edge_inserts ();
404 bbs.release ();
405 free (phivn);
407 free_dominance_info (CDI_POST_DOMINATORS);
409 return 0;
412 static bool
413 gate_phiprop (void)
415 return flag_tree_phiprop;
418 namespace {
420 const pass_data pass_data_phiprop =
422 GIMPLE_PASS, /* type */
423 "phiprop", /* name */
424 OPTGROUP_NONE, /* optinfo_flags */
425 true, /* has_gate */
426 true, /* has_execute */
427 TV_TREE_PHIPROP, /* tv_id */
428 ( PROP_cfg | PROP_ssa ), /* properties_required */
429 0, /* properties_provided */
430 0, /* properties_destroyed */
431 0, /* todo_flags_start */
432 ( TODO_update_ssa | TODO_verify_ssa ), /* todo_flags_finish */
435 class pass_phiprop : public gimple_opt_pass
437 public:
438 pass_phiprop (gcc::context *ctxt)
439 : gimple_opt_pass (pass_data_phiprop, ctxt)
442 /* opt_pass methods: */
443 bool gate () { return gate_phiprop (); }
444 unsigned int execute () { return tree_ssa_phiprop (); }
446 }; // class pass_phiprop
448 } // anon namespace
450 gimple_opt_pass *
451 make_pass_phiprop (gcc::context *ctxt)
453 return new pass_phiprop (ctxt);