re PR bootstrap/61084 (wide-int merge broke Solaris/SPARC bootstrap)
[platform/upstream/gcc.git] / gcc / tree-ssa-phiprop.c
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>
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 "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"
45
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.
49
50    For example the pass changes
51
52      # addr_1 = PHI <&a, &b>
53      tmp_1 = *addr_1;
54
55    to
56
57      # tmp_1 = PHI <a, b>
58
59    but also handles more complex scenarios like
60
61      D.2077_2 = &this_1(D)->a1;
62      ...
63
64      # b_12 = PHI <&c(2), D.2077_2(3)>
65      D.2114_13 = *b_12;
66      ...
67
68      # b_15 = PHI <b_12(4), &b(5)>
69      D.2080_5 = &this_1(D)->a0;
70      ...
71
72      # b_18 = PHI <D.2080_5(6), &c(7)>
73      ...
74
75      # b_21 = PHI <b_15(8), b_18(9)>
76      D.2076_8 = *b_21;
77
78    where the addresses loaded are defined by PHIs itself.
79    The above happens for
80
81      std::max(std::min(a0, c), std::min(std::max(a1, c), b))
82
83    where this pass transforms it to a form later PHI optimization
84    recognizes and transforms it to the simple
85
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>;
94
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.  */
98
99
100 /* Structure to keep track of the value of a dereferenced PHI result
101    and the virtual operand used for that dereference.  */
102
103 struct phiprop_d
104 {
105   tree value;
106   tree vuse;
107 };
108
109 /* Verify if the value recorded for NAME in PHIVN is still valid at
110    the start of basic block BB.  */
111
112 static bool
113 phivn_valid_p (struct phiprop_d *phivn, tree name, basic_block bb)
114 {
115   tree vuse = phivn[SSA_NAME_VERSION (name)].vuse;
116   gimple use_stmt;
117   imm_use_iterator ui2;
118   bool ok = true;
119
120   /* The def stmts of the virtual uses need to be dominated by bb.  */
121   gcc_assert (vuse != NULL_TREE);
122
123   FOR_EACH_IMM_USE_STMT (use_stmt, ui2, vuse)
124     {
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))
129         {
130           ok = false;
131           BREAK_FROM_IMM_USE_STMT (ui2);
132         }
133     }
134
135   return ok;
136 }
137
138 /* Insert a new phi node for the dereference of PHI at basic_block
139    BB with the virtual operands from USE_STMT.  */
140
141 static tree
142 phiprop_insert_phi (basic_block bb, gimple phi, gimple use_stmt,
143                     struct phiprop_d *phivn, size_t n)
144 {
145   tree res;
146   gimple new_phi;
147   edge_iterator ei;
148   edge e;
149
150   gcc_assert (is_gimple_assign (use_stmt)
151               && gimple_assign_rhs_code (use_stmt) == MEM_REF);
152
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);
157
158   if (dump_file && (dump_flags & TDF_DETAILS))
159     {
160       fprintf (dump_file, "Inserting PHI for result of load ");
161       print_gimple_stmt (dump_file, use_stmt, 0, 0);
162     }
163
164   /* Add PHI arguments for each edge inserting loads of the
165      addressable operands.  */
166   FOR_EACH_EDGE (e, ei, bb->preds)
167     {
168       tree old_arg, new_var;
169       gimple tmp;
170       source_location locus;
171
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))
177         {
178           gimple def_stmt = SSA_NAME_DEF_STMT (old_arg);
179           old_arg = gimple_assign_rhs1 (def_stmt);
180           locus = gimple_location (def_stmt);
181         }
182
183       if (TREE_CODE (old_arg) == SSA_NAME)
184         {
185           if (dump_file && (dump_flags & TDF_DETAILS))
186             {
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");
193             }
194           /* Reuse a formerly created dereference.  */
195           new_var = phivn[SSA_NAME_VERSION (old_arg)].value;
196         }
197       else
198         {
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);
211
212           gsi_insert_on_edge (e, tmp);
213           update_stmt (tmp);
214
215           if (dump_file && (dump_flags & TDF_DETAILS))
216             {
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);
221             }
222         }
223
224       add_phi_arg (new_phi, new_var, e, locus);
225     }
226
227   update_stmt (new_phi);
228
229   if (dump_file && (dump_flags & TDF_DETAILS))
230     print_gimple_stmt (dump_file, new_phi, 0, 0);
231
232   return res;
233 }
234
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.  */
248
249 static bool
250 propagate_with_phi (basic_block bb, gimple phi, struct phiprop_d *phivn,
251                     size_t n)
252 {
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;
262
263   if (!POINTER_TYPE_P (TREE_TYPE (ptr))
264       || !is_gimple_reg_type (TREE_TYPE (TREE_TYPE (ptr))))
265     return false;
266
267   /* Check if we can "cheaply" dereference all phi arguments.  */
268   FOR_EACH_PHI_ARG (arg_p, phi, i, SSA_OP_USE)
269     {
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))
278         {
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);
283         }
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);
296     }
297
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);
303
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)
308     {
309       gimple def_stmt;
310       tree vuse;
311
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;
317          
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;
330
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;
340
341       /* Found a proper dereference.  Insert a phi node if this
342          is the first load transformation.  */
343       if (!phi_inserted)
344         {
345           res = phiprop_insert_phi (bb, phi, use_stmt, phivn, n);
346           type = TREE_TYPE (res);
347
348           /* Remember the value we created for *ptr.  */
349           phivn[SSA_NAME_VERSION (ptr)].value = res;
350           phivn[SSA_NAME_VERSION (ptr)].vuse = vuse;
351
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);
357
358           phi_inserted = true;
359         }
360       else
361         {
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);
366         }
367
368 next:;
369       /* Continue searching for a proper dereference.  */
370     }
371
372   return phi_inserted;
373 }
374
375 /* Main entry for phiprop pass.  */
376
377 namespace {
378
379 const pass_data pass_data_phiprop =
380 {
381   GIMPLE_PASS, /* type */
382   "phiprop", /* name */
383   OPTGROUP_NONE, /* optinfo_flags */
384   true, /* has_execute */
385   TV_TREE_PHIPROP, /* tv_id */
386   ( PROP_cfg | PROP_ssa ), /* properties_required */
387   0, /* properties_provided */
388   0, /* properties_destroyed */
389   0, /* todo_flags_start */
390   TODO_update_ssa, /* todo_flags_finish */
391 };
392
393 class pass_phiprop : public gimple_opt_pass
394 {
395 public:
396   pass_phiprop (gcc::context *ctxt)
397     : gimple_opt_pass (pass_data_phiprop, ctxt)
398   {}
399
400   /* opt_pass methods: */
401   virtual bool gate (function *) { return flag_tree_phiprop; }
402   virtual unsigned int execute (function *);
403
404 }; // class pass_phiprop
405
406 unsigned int
407 pass_phiprop::execute (function *fun)
408 {
409   vec<basic_block> bbs;
410   struct phiprop_d *phivn;
411   bool did_something = false;
412   basic_block bb;
413   gimple_stmt_iterator gsi;
414   unsigned i;
415   size_t n;
416
417   calculate_dominance_info (CDI_DOMINATORS);
418   calculate_dominance_info (CDI_POST_DOMINATORS);
419
420   n = num_ssa_names;
421   phivn = XCNEWVEC (struct phiprop_d, n);
422
423   /* Walk the dominator tree in preorder.  */
424   bbs = get_all_dominated_blocks (CDI_DOMINATORS,
425                                   single_succ (ENTRY_BLOCK_PTR_FOR_FN (fun)));
426   FOR_EACH_VEC_ELT (bbs, i, bb)
427     for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
428       did_something |= propagate_with_phi (bb, gsi_stmt (gsi), phivn, n);
429
430   if (did_something)
431     gsi_commit_edge_inserts ();
432
433   bbs.release ();
434   free (phivn);
435
436   free_dominance_info (CDI_POST_DOMINATORS);
437
438   return 0;
439 }
440
441 } // anon namespace
442
443 gimple_opt_pass *
444 make_pass_phiprop (gcc::context *ctxt)
445 {
446   return new pass_phiprop (ctxt);
447 }