Remove a layer of indirection from hash_table
[gcc.git] / gcc / tree-ssa-copy.c
1 /* Copy propagation and SSA_NAME replacement support routines.
2 Copyright (C) 2004-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
9 any later version.
10
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
19
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "tree.h"
25 #include "flags.h"
26 #include "tm_p.h"
27 #include "basic-block.h"
28 #include "function.h"
29 #include "gimple-pretty-print.h"
30 #include "tree-ssa-alias.h"
31 #include "internal-fn.h"
32 #include "gimple-expr.h"
33 #include "is-a.h"
34 #include "gimple.h"
35 #include "gimple-iterator.h"
36 #include "gimple-ssa.h"
37 #include "tree-cfg.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 "tree-ssa-propagate.h"
44 #include "langhooks.h"
45 #include "cfgloop.h"
46 #include "tree-scalar-evolution.h"
47 #include "tree-ssa-dom.h"
48 #include "tree-ssa-loop-niter.h"
49
50
51 /* This file implements the copy propagation pass and provides a
52 handful of interfaces for performing const/copy propagation and
53 simple expression replacement which keep variable annotations
54 up-to-date.
55
56 We require that for any copy operation where the RHS and LHS have
57 a non-null memory tag the memory tag be the same. It is OK
58 for one or both of the memory tags to be NULL.
59
60 We also require tracking if a variable is dereferenced in a load or
61 store operation.
62
63 We enforce these requirements by having all copy propagation and
64 replacements of one SSA_NAME with a different SSA_NAME to use the
65 APIs defined in this file. */
66
67 /*---------------------------------------------------------------------------
68 Copy propagation
69 ---------------------------------------------------------------------------*/
70 /* Lattice for copy-propagation. The lattice is initialized to
71 UNDEFINED (value == NULL) for SSA names that can become a copy
72 of something or VARYING (value == self) if not (see get_copy_of_val
73 and stmt_may_generate_copy). Other values make the name a COPY
74 of that value.
75
76 When visiting a statement or PHI node the lattice value for an
77 SSA name can transition from UNDEFINED to COPY to VARYING. */
78
79 struct prop_value_d {
80 /* Copy-of value. */
81 tree value;
82 };
83 typedef struct prop_value_d prop_value_t;
84
85 static prop_value_t *copy_of;
86 static unsigned n_copy_of;
87
88
89 /* Return true if this statement may generate a useful copy. */
90
91 static bool
92 stmt_may_generate_copy (gimple stmt)
93 {
94 if (gimple_code (stmt) == GIMPLE_PHI)
95 return !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_phi_result (stmt));
96
97 if (gimple_code (stmt) != GIMPLE_ASSIGN)
98 return false;
99
100 /* If the statement has volatile operands, it won't generate a
101 useful copy. */
102 if (gimple_has_volatile_ops (stmt))
103 return false;
104
105 /* Statements with loads and/or stores will never generate a useful copy. */
106 if (gimple_vuse (stmt))
107 return false;
108
109 /* Otherwise, the only statements that generate useful copies are
110 assignments whose RHS is just an SSA name that doesn't flow
111 through abnormal edges. */
112 return ((gimple_assign_rhs_code (stmt) == SSA_NAME
113 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (gimple_assign_rhs1 (stmt)))
114 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)));
115 }
116
117
118 /* Return the copy-of value for VAR. */
119
120 static inline prop_value_t *
121 get_copy_of_val (tree var)
122 {
123 prop_value_t *val = &copy_of[SSA_NAME_VERSION (var)];
124
125 if (val->value == NULL_TREE
126 && !stmt_may_generate_copy (SSA_NAME_DEF_STMT (var)))
127 {
128 /* If the variable will never generate a useful copy relation,
129 make it its own copy. */
130 val->value = var;
131 }
132
133 return val;
134 }
135
136 /* Return the variable VAR is a copy of or VAR if VAR isn't the result
137 of a copy. */
138
139 static inline tree
140 valueize_val (tree var)
141 {
142 if (TREE_CODE (var) == SSA_NAME)
143 {
144 tree val = get_copy_of_val (var)->value;
145 if (val)
146 return val;
147 }
148 return var;
149 }
150
151 /* Set VAL to be the copy of VAR. If that changed return true. */
152
153 static inline bool
154 set_copy_of_val (tree var, tree val)
155 {
156 unsigned int ver = SSA_NAME_VERSION (var);
157 tree old;
158
159 /* Set FIRST to be the first link in COPY_OF[DEST]. If that
160 changed, return true. */
161 old = copy_of[ver].value;
162 copy_of[ver].value = val;
163
164 if (old != val
165 || (val && !operand_equal_p (old, val, 0)))
166 return true;
167
168 return false;
169 }
170
171
172 /* Dump the copy-of value for variable VAR to FILE. */
173
174 static void
175 dump_copy_of (FILE *file, tree var)
176 {
177 tree val;
178
179 print_generic_expr (file, var, dump_flags);
180 if (TREE_CODE (var) != SSA_NAME)
181 return;
182
183 val = copy_of[SSA_NAME_VERSION (var)].value;
184 fprintf (file, " copy-of chain: ");
185 print_generic_expr (file, var, 0);
186 fprintf (file, " ");
187 if (!val)
188 fprintf (file, "[UNDEFINED]");
189 else if (val == var)
190 fprintf (file, "[NOT A COPY]");
191 else
192 {
193 fprintf (file, "-> ");
194 print_generic_expr (file, val, 0);
195 fprintf (file, " ");
196 fprintf (file, "[COPY]");
197 }
198 }
199
200
201 /* Evaluate the RHS of STMT. If it produces a valid copy, set the LHS
202 value and store the LHS into *RESULT_P. */
203
204 static enum ssa_prop_result
205 copy_prop_visit_assignment (gimple stmt, tree *result_p)
206 {
207 tree lhs, rhs;
208
209 lhs = gimple_assign_lhs (stmt);
210 rhs = valueize_val (gimple_assign_rhs1 (stmt));
211
212 if (TREE_CODE (lhs) == SSA_NAME)
213 {
214 /* Straight copy between two SSA names. First, make sure that
215 we can propagate the RHS into uses of LHS. */
216 if (!may_propagate_copy (lhs, rhs))
217 return SSA_PROP_VARYING;
218
219 *result_p = lhs;
220 if (set_copy_of_val (*result_p, rhs))
221 return SSA_PROP_INTERESTING;
222 else
223 return SSA_PROP_NOT_INTERESTING;
224 }
225
226 return SSA_PROP_VARYING;
227 }
228
229
230 /* Visit the GIMPLE_COND STMT. Return SSA_PROP_INTERESTING
231 if it can determine which edge will be taken. Otherwise, return
232 SSA_PROP_VARYING. */
233
234 static enum ssa_prop_result
235 copy_prop_visit_cond_stmt (gimple stmt, edge *taken_edge_p)
236 {
237 enum ssa_prop_result retval = SSA_PROP_VARYING;
238 location_t loc = gimple_location (stmt);
239
240 tree op0 = gimple_cond_lhs (stmt);
241 tree op1 = gimple_cond_rhs (stmt);
242
243 /* The only conditionals that we may be able to compute statically
244 are predicates involving two SSA_NAMEs. */
245 if (TREE_CODE (op0) == SSA_NAME && TREE_CODE (op1) == SSA_NAME)
246 {
247 op0 = valueize_val (op0);
248 op1 = valueize_val (op1);
249
250 /* See if we can determine the predicate's value. */
251 if (dump_file && (dump_flags & TDF_DETAILS))
252 {
253 fprintf (dump_file, "Trying to determine truth value of ");
254 fprintf (dump_file, "predicate ");
255 print_gimple_stmt (dump_file, stmt, 0, 0);
256 }
257
258 /* We can fold COND and get a useful result only when we have
259 the same SSA_NAME on both sides of a comparison operator. */
260 if (op0 == op1)
261 {
262 tree folded_cond = fold_binary_loc (loc, gimple_cond_code (stmt),
263 boolean_type_node, op0, op1);
264 if (folded_cond)
265 {
266 basic_block bb = gimple_bb (stmt);
267 *taken_edge_p = find_taken_edge (bb, folded_cond);
268 if (*taken_edge_p)
269 retval = SSA_PROP_INTERESTING;
270 }
271 }
272 }
273
274 if (dump_file && (dump_flags & TDF_DETAILS) && *taken_edge_p)
275 fprintf (dump_file, "\nConditional will always take edge %d->%d\n",
276 (*taken_edge_p)->src->index, (*taken_edge_p)->dest->index);
277
278 return retval;
279 }
280
281
282 /* Evaluate statement STMT. If the statement produces a new output
283 value, return SSA_PROP_INTERESTING and store the SSA_NAME holding
284 the new value in *RESULT_P.
285
286 If STMT is a conditional branch and we can determine its truth
287 value, set *TAKEN_EDGE_P accordingly.
288
289 If the new value produced by STMT is varying, return
290 SSA_PROP_VARYING. */
291
292 static enum ssa_prop_result
293 copy_prop_visit_stmt (gimple stmt, edge *taken_edge_p, tree *result_p)
294 {
295 enum ssa_prop_result retval;
296
297 if (dump_file && (dump_flags & TDF_DETAILS))
298 {
299 fprintf (dump_file, "\nVisiting statement:\n");
300 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
301 fprintf (dump_file, "\n");
302 }
303
304 if (gimple_assign_single_p (stmt)
305 && TREE_CODE (gimple_assign_lhs (stmt)) == SSA_NAME
306 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
307 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
308 {
309 /* If the statement is a copy assignment, evaluate its RHS to
310 see if the lattice value of its output has changed. */
311 retval = copy_prop_visit_assignment (stmt, result_p);
312 }
313 else if (gimple_code (stmt) == GIMPLE_COND)
314 {
315 /* See if we can determine which edge goes out of a conditional
316 jump. */
317 retval = copy_prop_visit_cond_stmt (stmt, taken_edge_p);
318 }
319 else
320 retval = SSA_PROP_VARYING;
321
322 if (retval == SSA_PROP_VARYING)
323 {
324 tree def;
325 ssa_op_iter i;
326
327 /* Any other kind of statement is not interesting for constant
328 propagation and, therefore, not worth simulating. */
329 if (dump_file && (dump_flags & TDF_DETAILS))
330 fprintf (dump_file, "No interesting values produced.\n");
331
332 /* The assignment is not a copy operation. Don't visit this
333 statement again and mark all the definitions in the statement
334 to be copies of nothing. */
335 FOR_EACH_SSA_TREE_OPERAND (def, stmt, i, SSA_OP_ALL_DEFS)
336 set_copy_of_val (def, def);
337 }
338
339 return retval;
340 }
341
342
343 /* Visit PHI node PHI. If all the arguments produce the same value,
344 set it to be the value of the LHS of PHI. */
345
346 static enum ssa_prop_result
347 copy_prop_visit_phi_node (gimple phi)
348 {
349 enum ssa_prop_result retval;
350 unsigned i;
351 prop_value_t phi_val = { NULL_TREE };
352
353 tree lhs = gimple_phi_result (phi);
354
355 if (dump_file && (dump_flags & TDF_DETAILS))
356 {
357 fprintf (dump_file, "\nVisiting PHI node: ");
358 print_gimple_stmt (dump_file, phi, 0, dump_flags);
359 }
360
361 for (i = 0; i < gimple_phi_num_args (phi); i++)
362 {
363 prop_value_t *arg_val;
364 tree arg_value;
365 tree arg = gimple_phi_arg_def (phi, i);
366 edge e = gimple_phi_arg_edge (phi, i);
367
368 /* We don't care about values flowing through non-executable
369 edges. */
370 if (!(e->flags & EDGE_EXECUTABLE))
371 continue;
372
373 /* Names that flow through abnormal edges cannot be used to
374 derive copies. */
375 if (TREE_CODE (arg) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg))
376 {
377 phi_val.value = lhs;
378 break;
379 }
380
381 if (dump_file && (dump_flags & TDF_DETAILS))
382 {
383 fprintf (dump_file, "\tArgument #%d: ", i);
384 dump_copy_of (dump_file, arg);
385 fprintf (dump_file, "\n");
386 }
387
388 if (TREE_CODE (arg) == SSA_NAME)
389 {
390 arg_val = get_copy_of_val (arg);
391
392 /* If we didn't visit the definition of arg yet treat it as
393 UNDEFINED. This also handles PHI arguments that are the
394 same as lhs. We'll come here again. */
395 if (!arg_val->value)
396 continue;
397
398 arg_value = arg_val->value;
399 }
400 else
401 arg_value = valueize_val (arg);
402
403 /* Avoid copy propagation from an inner into an outer loop.
404 Otherwise, this may move loop variant variables outside of
405 their loops and prevent coalescing opportunities. If the
406 value was loop invariant, it will be hoisted by LICM and
407 exposed for copy propagation.
408 ??? The value will be always loop invariant.
409 In loop-closed SSA form do not copy-propagate through
410 PHI nodes in blocks with a loop exit edge predecessor. */
411 if (TREE_CODE (arg_value) == SSA_NAME
412 && (loop_depth_of_name (arg_value) > loop_depth_of_name (lhs)
413 || (loops_state_satisfies_p (LOOP_CLOSED_SSA)
414 && loop_exit_edge_p (e->src->loop_father, e))))
415 {
416 phi_val.value = lhs;
417 break;
418 }
419
420 /* If the LHS didn't have a value yet, make it a copy of the
421 first argument we find. */
422 if (phi_val.value == NULL_TREE)
423 {
424 phi_val.value = arg_value;
425 continue;
426 }
427
428 /* If PHI_VAL and ARG don't have a common copy-of chain, then
429 this PHI node cannot be a copy operation. */
430 if (phi_val.value != arg_value
431 && !operand_equal_p (phi_val.value, arg_value, 0))
432 {
433 phi_val.value = lhs;
434 break;
435 }
436 }
437
438 if (phi_val.value
439 && may_propagate_copy (lhs, phi_val.value)
440 && set_copy_of_val (lhs, phi_val.value))
441 retval = (phi_val.value != lhs) ? SSA_PROP_INTERESTING : SSA_PROP_VARYING;
442 else
443 retval = SSA_PROP_NOT_INTERESTING;
444
445 if (dump_file && (dump_flags & TDF_DETAILS))
446 {
447 fprintf (dump_file, "PHI node ");
448 dump_copy_of (dump_file, lhs);
449 fprintf (dump_file, "\nTelling the propagator to ");
450 if (retval == SSA_PROP_INTERESTING)
451 fprintf (dump_file, "add SSA edges out of this PHI and continue.");
452 else if (retval == SSA_PROP_VARYING)
453 fprintf (dump_file, "add SSA edges out of this PHI and never visit again.");
454 else
455 fprintf (dump_file, "do nothing with SSA edges and keep iterating.");
456 fprintf (dump_file, "\n\n");
457 }
458
459 return retval;
460 }
461
462
463 /* Initialize structures used for copy propagation. */
464
465 static void
466 init_copy_prop (void)
467 {
468 basic_block bb;
469
470 n_copy_of = num_ssa_names;
471 copy_of = XCNEWVEC (prop_value_t, n_copy_of);
472
473 FOR_EACH_BB_FN (bb, cfun)
474 {
475 gimple_stmt_iterator si;
476 int depth = bb_loop_depth (bb);
477
478 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
479 {
480 gimple stmt = gsi_stmt (si);
481 ssa_op_iter iter;
482 tree def;
483
484 /* The only statements that we care about are those that may
485 generate useful copies. We also need to mark conditional
486 jumps so that their outgoing edges are added to the work
487 lists of the propagator.
488
489 Avoid copy propagation from an inner into an outer loop.
490 Otherwise, this may move loop variant variables outside of
491 their loops and prevent coalescing opportunities. If the
492 value was loop invariant, it will be hoisted by LICM and
493 exposed for copy propagation.
494 ??? This doesn't make sense. */
495 if (stmt_ends_bb_p (stmt))
496 prop_set_simulate_again (stmt, true);
497 else if (stmt_may_generate_copy (stmt)
498 /* Since we are iterating over the statements in
499 BB, not the phi nodes, STMT will always be an
500 assignment. */
501 && loop_depth_of_name (gimple_assign_rhs1 (stmt)) <= depth)
502 prop_set_simulate_again (stmt, true);
503 else
504 prop_set_simulate_again (stmt, false);
505
506 /* Mark all the outputs of this statement as not being
507 the copy of anything. */
508 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
509 if (!prop_simulate_again_p (stmt))
510 set_copy_of_val (def, def);
511 }
512
513 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
514 {
515 gimple phi = gsi_stmt (si);
516 tree def;
517
518 def = gimple_phi_result (phi);
519 if (virtual_operand_p (def))
520 prop_set_simulate_again (phi, false);
521 else
522 prop_set_simulate_again (phi, true);
523
524 if (!prop_simulate_again_p (phi))
525 set_copy_of_val (def, def);
526 }
527 }
528 }
529
530 /* Callback for substitute_and_fold to get at the final copy-of values. */
531
532 static tree
533 get_value (tree name)
534 {
535 tree val;
536 if (SSA_NAME_VERSION (name) >= n_copy_of)
537 return NULL_TREE;
538 val = copy_of[SSA_NAME_VERSION (name)].value;
539 if (val && val != name)
540 return val;
541 return NULL_TREE;
542 }
543
544 /* Deallocate memory used in copy propagation and do final
545 substitution. */
546
547 static bool
548 fini_copy_prop (void)
549 {
550 unsigned i;
551
552 /* Set the final copy-of value for each variable by traversing the
553 copy-of chains. */
554 for (i = 1; i < num_ssa_names; i++)
555 {
556 tree var = ssa_name (i);
557 if (!var
558 || !copy_of[i].value
559 || copy_of[i].value == var)
560 continue;
561
562 /* In theory the points-to solution of all members of the
563 copy chain is their intersection. For now we do not bother
564 to compute this but only make sure we do not lose points-to
565 information completely by setting the points-to solution
566 of the representative to the first solution we find if
567 it doesn't have one already. */
568 if (copy_of[i].value != var
569 && TREE_CODE (copy_of[i].value) == SSA_NAME)
570 {
571 basic_block copy_of_bb
572 = gimple_bb (SSA_NAME_DEF_STMT (copy_of[i].value));
573 basic_block var_bb = gimple_bb (SSA_NAME_DEF_STMT (var));
574 if (POINTER_TYPE_P (TREE_TYPE (var))
575 && SSA_NAME_PTR_INFO (var)
576 && !SSA_NAME_PTR_INFO (copy_of[i].value))
577 {
578 duplicate_ssa_name_ptr_info (copy_of[i].value,
579 SSA_NAME_PTR_INFO (var));
580 /* Points-to information is cfg insensitive,
581 but alignment info might be cfg sensitive, if it
582 e.g. is derived from VRP derived non-zero bits.
583 So, do not copy alignment info if the two SSA_NAMEs
584 aren't defined in the same basic block. */
585 if (var_bb != copy_of_bb)
586 mark_ptr_info_alignment_unknown
587 (SSA_NAME_PTR_INFO (copy_of[i].value));
588 }
589 else if (!POINTER_TYPE_P (TREE_TYPE (var))
590 && SSA_NAME_RANGE_INFO (var)
591 && !SSA_NAME_RANGE_INFO (copy_of[i].value)
592 && var_bb == copy_of_bb)
593 duplicate_ssa_name_range_info (copy_of[i].value,
594 SSA_NAME_RANGE_TYPE (var),
595 SSA_NAME_RANGE_INFO (var));
596 }
597 }
598
599 bool changed = substitute_and_fold (get_value, NULL, true);
600 if (changed)
601 {
602 free_numbers_of_iterations_estimates ();
603 if (scev_initialized_p ())
604 scev_reset ();
605 }
606
607 free (copy_of);
608
609 return changed;
610 }
611
612
613 /* Main entry point to the copy propagator.
614
615 PHIS_ONLY is true if we should only consider PHI nodes as generating
616 copy propagation opportunities.
617
618 The algorithm propagates the value COPY-OF using ssa_propagate. For
619 every variable X_i, COPY-OF(X_i) indicates which variable is X_i created
620 from. The following example shows how the algorithm proceeds at a
621 high level:
622
623 1 a_24 = x_1
624 2 a_2 = PHI <a_24, x_1>
625 3 a_5 = PHI <a_2>
626 4 x_1 = PHI <x_298, a_5, a_2>
627
628 The end result should be that a_2, a_5, a_24 and x_1 are a copy of
629 x_298. Propagation proceeds as follows.
630
631 Visit #1: a_24 is copy-of x_1. Value changed.
632 Visit #2: a_2 is copy-of x_1. Value changed.
633 Visit #3: a_5 is copy-of x_1. Value changed.
634 Visit #4: x_1 is copy-of x_298. Value changed.
635 Visit #1: a_24 is copy-of x_298. Value changed.
636 Visit #2: a_2 is copy-of x_298. Value changed.
637 Visit #3: a_5 is copy-of x_298. Value changed.
638 Visit #4: x_1 is copy-of x_298. Stable state reached.
639
640 When visiting PHI nodes, we only consider arguments that flow
641 through edges marked executable by the propagation engine. So,
642 when visiting statement #2 for the first time, we will only look at
643 the first argument (a_24) and optimistically assume that its value
644 is the copy of a_24 (x_1). */
645
646 static unsigned int
647 execute_copy_prop (void)
648 {
649 init_copy_prop ();
650 ssa_propagate (copy_prop_visit_stmt, copy_prop_visit_phi_node);
651 if (fini_copy_prop ())
652 return TODO_cleanup_cfg;
653 return 0;
654 }
655
656 namespace {
657
658 const pass_data pass_data_copy_prop =
659 {
660 GIMPLE_PASS, /* type */
661 "copyprop", /* name */
662 OPTGROUP_NONE, /* optinfo_flags */
663 true, /* has_execute */
664 TV_TREE_COPY_PROP, /* tv_id */
665 ( PROP_ssa | PROP_cfg ), /* properties_required */
666 0, /* properties_provided */
667 0, /* properties_destroyed */
668 0, /* todo_flags_start */
669 0, /* todo_flags_finish */
670 };
671
672 class pass_copy_prop : public gimple_opt_pass
673 {
674 public:
675 pass_copy_prop (gcc::context *ctxt)
676 : gimple_opt_pass (pass_data_copy_prop, ctxt)
677 {}
678
679 /* opt_pass methods: */
680 opt_pass * clone () { return new pass_copy_prop (m_ctxt); }
681 virtual bool gate (function *) { return flag_tree_copy_prop != 0; }
682 virtual unsigned int execute (function *) { return execute_copy_prop (); }
683
684 }; // class pass_copy_prop
685
686 } // anon namespace
687
688 gimple_opt_pass *
689 make_pass_copy_prop (gcc::context *ctxt)
690 {
691 return new pass_copy_prop (ctxt);
692 }