Fix gnat.dg/opt39.adb on hppa.
[official-gcc.git] / gcc / tree-ssa-dom.cc
blob32769c2068a7c94aa1b747d3e1d7ba1c5596ae43
1 /* SSA Dominator optimizations for trees
2 Copyright (C) 2001-2023 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
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 "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 "cfganal.h"
32 #include "cfgloop.h"
33 #include "gimple-iterator.h"
34 #include "gimple-fold.h"
35 #include "tree-eh.h"
36 #include "tree-inline.h"
37 #include "tree-cfg.h"
38 #include "tree-into-ssa.h"
39 #include "domwalk.h"
40 #include "tree-ssa-propagate.h"
41 #include "tree-ssa-threadupdate.h"
42 #include "tree-ssa-scopedtables.h"
43 #include "tree-ssa-threadedge.h"
44 #include "tree-ssa-dom.h"
45 #include "gimplify.h"
46 #include "tree-cfgcleanup.h"
47 #include "dbgcnt.h"
48 #include "alloc-pool.h"
49 #include "tree-vrp.h"
50 #include "vr-values.h"
51 #include "gimple-range.h"
52 #include "gimple-range-path.h"
53 #include "alias.h"
55 /* This file implements optimizations on the dominator tree. */
57 /* Structure for recording edge equivalences.
59 Computing and storing the edge equivalences instead of creating
60 them on-demand can save significant amounts of time, particularly
61 for pathological cases involving switch statements.
63 These structures live for a single iteration of the dominator
64 optimizer in the edge's AUX field. At the end of an iteration we
65 free each of these structures. */
66 class edge_info
68 public:
69 typedef std::pair <tree, tree> equiv_pair;
70 edge_info (edge);
71 ~edge_info ();
73 /* Record a simple LHS = RHS equivalence. This may trigger
74 calls to derive_equivalences. */
75 void record_simple_equiv (tree, tree);
77 /* If traversing this edge creates simple equivalences, we store
78 them as LHS/RHS pairs within this vector. */
79 vec<equiv_pair> simple_equivalences;
81 /* Traversing an edge may also indicate one or more particular conditions
82 are true or false. */
83 vec<cond_equivalence> cond_equivalences;
85 private:
86 /* Derive equivalences by walking the use-def chains. */
87 void derive_equivalences (tree, tree, int);
90 /* Track whether or not we have changed the control flow graph. */
91 static bool cfg_altered;
93 /* Bitmap of blocks that have had EH statements cleaned. We should
94 remove their dead edges eventually. */
95 static bitmap need_eh_cleanup;
96 static vec<gimple *> need_noreturn_fixup;
98 /* Statistics for dominator optimizations. */
99 struct opt_stats_d
101 long num_stmts;
102 long num_exprs_considered;
103 long num_re;
104 long num_const_prop;
105 long num_copy_prop;
108 static struct opt_stats_d opt_stats;
110 /* Local functions. */
111 static void record_equality (tree, tree, class const_and_copies *);
112 static void record_equivalences_from_phis (basic_block);
113 static void record_equivalences_from_incoming_edge (basic_block,
114 class const_and_copies *,
115 class avail_exprs_stack *,
116 bitmap blocks_on_stack);
117 static void eliminate_redundant_computations (gimple_stmt_iterator *,
118 class const_and_copies *,
119 class avail_exprs_stack *);
120 static void record_equivalences_from_stmt (gimple *, int,
121 class avail_exprs_stack *);
122 static void dump_dominator_optimization_stats (FILE *file,
123 hash_table<expr_elt_hasher> *);
124 static void record_temporary_equivalences (edge, class const_and_copies *,
125 class avail_exprs_stack *, bitmap);
127 /* Constructor for EDGE_INFO. An EDGE_INFO instance is always
128 associated with an edge E. */
130 edge_info::edge_info (edge e)
132 /* Free the old one associated with E, if it exists and
133 associate our new object with E. */
134 free_dom_edge_info (e);
135 e->aux = this;
137 /* And initialize the embedded vectors. */
138 simple_equivalences = vNULL;
139 cond_equivalences = vNULL;
142 /* Destructor just needs to release the vectors. */
144 edge_info::~edge_info (void)
146 this->cond_equivalences.release ();
147 this->simple_equivalences.release ();
150 /* NAME is known to have the value VALUE, which must be a constant.
152 Walk through its use-def chain to see if there are other equivalences
153 we might be able to derive.
155 RECURSION_LIMIT controls how far back we recurse through the use-def
156 chains. */
158 void
159 edge_info::derive_equivalences (tree name, tree value, int recursion_limit)
161 if (TREE_CODE (name) != SSA_NAME || TREE_CODE (value) != INTEGER_CST)
162 return;
164 /* This records the equivalence for the toplevel object. Do
165 this before checking the recursion limit. */
166 simple_equivalences.safe_push (equiv_pair (name, value));
168 /* Limit how far up the use-def chains we are willing to walk. */
169 if (recursion_limit == 0)
170 return;
172 /* We can walk up the use-def chains to potentially find more
173 equivalences. */
174 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
175 if (is_gimple_assign (def_stmt))
177 enum tree_code code = gimple_assign_rhs_code (def_stmt);
178 switch (code)
180 /* If the result of an OR is zero, then its operands are, too. */
181 case BIT_IOR_EXPR:
182 if (integer_zerop (value))
184 tree rhs1 = gimple_assign_rhs1 (def_stmt);
185 tree rhs2 = gimple_assign_rhs2 (def_stmt);
187 value = build_zero_cst (TREE_TYPE (rhs1));
188 derive_equivalences (rhs1, value, recursion_limit - 1);
189 value = build_zero_cst (TREE_TYPE (rhs2));
190 derive_equivalences (rhs2, value, recursion_limit - 1);
192 break;
194 /* If the result of an AND is nonzero, then its operands are, too. */
195 case BIT_AND_EXPR:
196 if (!integer_zerop (value))
198 tree rhs1 = gimple_assign_rhs1 (def_stmt);
199 tree rhs2 = gimple_assign_rhs2 (def_stmt);
201 /* If either operand has a boolean range, then we
202 know its value must be one, otherwise we just know it
203 is nonzero. The former is clearly useful, I haven't
204 seen cases where the latter is helpful yet. */
205 if (TREE_CODE (rhs1) == SSA_NAME)
207 if (ssa_name_has_boolean_range (rhs1))
209 value = build_one_cst (TREE_TYPE (rhs1));
210 derive_equivalences (rhs1, value, recursion_limit - 1);
213 if (TREE_CODE (rhs2) == SSA_NAME)
215 if (ssa_name_has_boolean_range (rhs2))
217 value = build_one_cst (TREE_TYPE (rhs2));
218 derive_equivalences (rhs2, value, recursion_limit - 1);
222 break;
224 /* If LHS is an SSA_NAME and RHS is a constant integer and LHS was
225 set via a widening type conversion, then we may be able to record
226 additional equivalences. */
227 CASE_CONVERT:
229 tree rhs = gimple_assign_rhs1 (def_stmt);
230 tree rhs_type = TREE_TYPE (rhs);
231 if (INTEGRAL_TYPE_P (rhs_type)
232 && (TYPE_PRECISION (TREE_TYPE (name))
233 >= TYPE_PRECISION (rhs_type))
234 && int_fits_type_p (value, rhs_type))
235 derive_equivalences (rhs,
236 fold_convert (rhs_type, value),
237 recursion_limit - 1);
238 break;
241 /* We can invert the operation of these codes trivially if
242 one of the RHS operands is a constant to produce a known
243 value for the other RHS operand. */
244 case POINTER_PLUS_EXPR:
245 case PLUS_EXPR:
247 tree rhs1 = gimple_assign_rhs1 (def_stmt);
248 tree rhs2 = gimple_assign_rhs2 (def_stmt);
250 /* If either argument is a constant, then we can compute
251 a constant value for the nonconstant argument. */
252 if (TREE_CODE (rhs1) == INTEGER_CST
253 && TREE_CODE (rhs2) == SSA_NAME)
254 derive_equivalences (rhs2,
255 fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
256 value, rhs1),
257 recursion_limit - 1);
258 else if (TREE_CODE (rhs2) == INTEGER_CST
259 && TREE_CODE (rhs1) == SSA_NAME)
260 derive_equivalences (rhs1,
261 fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
262 value, rhs2),
263 recursion_limit - 1);
264 break;
267 /* If one of the operands is a constant, then we can compute
268 the value of the other operand. If both operands are
269 SSA_NAMEs, then they must be equal if the result is zero. */
270 case MINUS_EXPR:
272 tree rhs1 = gimple_assign_rhs1 (def_stmt);
273 tree rhs2 = gimple_assign_rhs2 (def_stmt);
275 /* If either argument is a constant, then we can compute
276 a constant value for the nonconstant argument. */
277 if (TREE_CODE (rhs1) == INTEGER_CST
278 && TREE_CODE (rhs2) == SSA_NAME)
279 derive_equivalences (rhs2,
280 fold_binary (MINUS_EXPR, TREE_TYPE (rhs1),
281 rhs1, value),
282 recursion_limit - 1);
283 else if (TREE_CODE (rhs2) == INTEGER_CST
284 && TREE_CODE (rhs1) == SSA_NAME)
285 derive_equivalences (rhs1,
286 fold_binary (PLUS_EXPR, TREE_TYPE (rhs1),
287 value, rhs2),
288 recursion_limit - 1);
289 else if (integer_zerop (value))
291 tree cond = build2 (EQ_EXPR, boolean_type_node,
292 gimple_assign_rhs1 (def_stmt),
293 gimple_assign_rhs2 (def_stmt));
294 tree inverted = invert_truthvalue (cond);
295 record_conditions (&this->cond_equivalences, cond, inverted);
297 break;
300 case EQ_EXPR:
301 case NE_EXPR:
303 if ((code == EQ_EXPR && integer_onep (value))
304 || (code == NE_EXPR && integer_zerop (value)))
306 tree rhs1 = gimple_assign_rhs1 (def_stmt);
307 tree rhs2 = gimple_assign_rhs2 (def_stmt);
309 /* If either argument is a constant, then record the
310 other argument as being the same as that constant.
312 If neither operand is a constant, then we have a
313 conditional name == name equivalence. */
314 if (TREE_CODE (rhs1) == INTEGER_CST)
315 derive_equivalences (rhs2, rhs1, recursion_limit - 1);
316 else if (TREE_CODE (rhs2) == INTEGER_CST)
317 derive_equivalences (rhs1, rhs2, recursion_limit - 1);
319 else
321 tree cond = build2 (code, boolean_type_node,
322 gimple_assign_rhs1 (def_stmt),
323 gimple_assign_rhs2 (def_stmt));
324 tree inverted = invert_truthvalue (cond);
325 if (integer_zerop (value))
326 std::swap (cond, inverted);
327 record_conditions (&this->cond_equivalences, cond, inverted);
329 break;
332 /* For BIT_NOT and NEGATE, we can just apply the operation to the
333 VALUE to get the new equivalence. It will always be a constant
334 so we can recurse. */
335 case BIT_NOT_EXPR:
336 case NEGATE_EXPR:
338 tree rhs = gimple_assign_rhs1 (def_stmt);
339 tree res;
340 /* If this is a NOT and the operand has a boolean range, then we
341 know its value must be zero or one. We are not supposed to
342 have a BIT_NOT_EXPR for boolean types with precision > 1 in
343 the general case, see e.g. the handling of TRUTH_NOT_EXPR in
344 the gimplifier, but it can be generated by match.pd out of
345 a BIT_XOR_EXPR wrapped in a BIT_AND_EXPR. Now the handling
346 of BIT_AND_EXPR above already forces a specific semantics for
347 boolean types with precision > 1 so we must do the same here,
348 otherwise we could change the semantics of TRUTH_NOT_EXPR for
349 boolean types with precision > 1. */
350 if (code == BIT_NOT_EXPR
351 && TREE_CODE (rhs) == SSA_NAME
352 && ssa_name_has_boolean_range (rhs))
354 if ((TREE_INT_CST_LOW (value) & 1) == 0)
355 res = build_one_cst (TREE_TYPE (rhs));
356 else
357 res = build_zero_cst (TREE_TYPE (rhs));
359 else
360 res = fold_build1 (code, TREE_TYPE (rhs), value);
361 derive_equivalences (rhs, res, recursion_limit - 1);
362 break;
365 default:
367 if (TREE_CODE_CLASS (code) == tcc_comparison)
369 tree cond = build2 (code, boolean_type_node,
370 gimple_assign_rhs1 (def_stmt),
371 gimple_assign_rhs2 (def_stmt));
372 tree inverted = invert_truthvalue (cond);
373 if (integer_zerop (value))
374 std::swap (cond, inverted);
375 record_conditions (&this->cond_equivalences, cond, inverted);
376 break;
378 break;
384 void
385 edge_info::record_simple_equiv (tree lhs, tree rhs)
387 /* If the RHS is a constant, then we may be able to derive
388 further equivalences. Else just record the name = name
389 equivalence. */
390 if (TREE_CODE (rhs) == INTEGER_CST)
391 derive_equivalences (lhs, rhs, 4);
392 else
393 simple_equivalences.safe_push (equiv_pair (lhs, rhs));
396 /* Free the edge_info data attached to E, if it exists and
397 clear e->aux. */
399 void
400 free_dom_edge_info (edge e)
402 class edge_info *edge_info = (class edge_info *)e->aux;
404 if (edge_info)
405 delete edge_info;
406 e->aux = NULL;
409 /* Free all EDGE_INFO structures associated with edges in the CFG.
410 If a particular edge can be threaded, copy the redirection
411 target from the EDGE_INFO structure into the edge's AUX field
412 as required by code to update the CFG and SSA graph for
413 jump threading. */
415 static void
416 free_all_edge_infos (void)
418 basic_block bb;
419 edge_iterator ei;
420 edge e;
422 FOR_EACH_BB_FN (bb, cfun)
424 FOR_EACH_EDGE (e, ei, bb->preds)
425 free_dom_edge_info (e);
429 /* Return TRUE if BB has precisely two preds, one of which
430 is a backedge from a forwarder block where the forwarder
431 block is a direct successor of BB. Being a forwarder
432 block, it has no side effects other than transfer of
433 control. Otherwise return FALSE. */
435 static bool
436 single_block_loop_p (basic_block bb)
438 /* Two preds. */
439 if (EDGE_COUNT (bb->preds) != 2)
440 return false;
442 /* One and only one of the edges must be marked with
443 EDGE_DFS_BACK. */
444 basic_block pred = NULL;
445 unsigned int count = 0;
446 if (EDGE_PRED (bb, 0)->flags & EDGE_DFS_BACK)
448 pred = EDGE_PRED (bb, 0)->src;
449 count++;
451 if (EDGE_PRED (bb, 1)->flags & EDGE_DFS_BACK)
453 pred = EDGE_PRED (bb, 1)->src;
454 count++;
457 if (count != 1)
458 return false;
460 /* Now examine PRED. It should have a single predecessor which
461 is BB and a single successor that is also BB. */
462 if (EDGE_COUNT (pred->preds) != 1
463 || EDGE_COUNT (pred->succs) != 1
464 || EDGE_PRED (pred, 0)->src != bb
465 || EDGE_SUCC (pred, 0)->dest != bb)
466 return false;
468 /* This looks good from a CFG standpoint. Now look at the guts
469 of PRED. Basically we want to verify there are no PHI nodes
470 and no real statements. */
471 if (! gimple_seq_empty_p (phi_nodes (pred)))
472 return false;
474 gimple_stmt_iterator gsi;
475 for (gsi = gsi_last_bb (pred); !gsi_end_p (gsi); gsi_prev (&gsi))
477 gimple *stmt = gsi_stmt (gsi);
479 switch (gimple_code (stmt))
481 case GIMPLE_LABEL:
482 if (DECL_NONLOCAL (gimple_label_label (as_a <glabel *> (stmt))))
483 return false;
484 break;
486 case GIMPLE_DEBUG:
487 break;
489 default:
490 return false;
494 return true;
497 /* We have finished optimizing BB, record any information implied by
498 taking a specific outgoing edge from BB. */
500 static void
501 record_edge_info (basic_block bb)
503 gimple_stmt_iterator gsi = gsi_last_bb (bb);
504 class edge_info *edge_info;
506 /* Free all the outgoing edge info data associated with
507 BB's outgoing edges. */
508 edge e;
509 edge_iterator ei;
510 FOR_EACH_EDGE (e, ei, bb->succs)
511 free_dom_edge_info (e);
513 if (! gsi_end_p (gsi))
515 gimple *stmt = gsi_stmt (gsi);
516 location_t loc = gimple_location (stmt);
518 if (gimple_code (stmt) == GIMPLE_SWITCH)
520 gswitch *switch_stmt = as_a <gswitch *> (stmt);
521 tree index = gimple_switch_index (switch_stmt);
523 if (TREE_CODE (index) == SSA_NAME)
525 int i;
526 int n_labels = gimple_switch_num_labels (switch_stmt);
527 tree *info = XCNEWVEC (tree, last_basic_block_for_fn (cfun));
529 for (i = 0; i < n_labels; i++)
531 tree label = gimple_switch_label (switch_stmt, i);
532 basic_block target_bb
533 = label_to_block (cfun, CASE_LABEL (label));
534 if (CASE_HIGH (label)
535 || !CASE_LOW (label)
536 || info[target_bb->index])
537 info[target_bb->index] = error_mark_node;
538 else
539 info[target_bb->index] = label;
542 FOR_EACH_EDGE (e, ei, bb->succs)
544 basic_block target_bb = e->dest;
545 tree label = info[target_bb->index];
547 if (label != NULL && label != error_mark_node)
549 tree x = fold_convert_loc (loc, TREE_TYPE (index),
550 CASE_LOW (label));
551 edge_info = new class edge_info (e);
552 edge_info->record_simple_equiv (index, x);
555 free (info);
559 /* A COND_EXPR may create equivalences too. */
560 if (gimple_code (stmt) == GIMPLE_COND)
562 edge true_edge;
563 edge false_edge;
565 tree op0 = gimple_cond_lhs (stmt);
566 tree op1 = gimple_cond_rhs (stmt);
567 enum tree_code code = gimple_cond_code (stmt);
569 extract_true_false_edges_from_block (bb, &true_edge, &false_edge);
571 /* Special case comparing booleans against a constant as we
572 know the value of OP0 on both arms of the branch. i.e., we
573 can record an equivalence for OP0 rather than COND.
575 However, don't do this if the constant isn't zero or one.
576 Such conditionals will get optimized more thoroughly during
577 the domwalk. */
578 if ((code == EQ_EXPR || code == NE_EXPR)
579 && TREE_CODE (op0) == SSA_NAME
580 && ssa_name_has_boolean_range (op0)
581 && is_gimple_min_invariant (op1)
582 && (integer_zerop (op1) || integer_onep (op1)))
584 tree true_val = constant_boolean_node (true, TREE_TYPE (op0));
585 tree false_val = constant_boolean_node (false, TREE_TYPE (op0));
587 if (code == EQ_EXPR)
589 edge_info = new class edge_info (true_edge);
590 edge_info->record_simple_equiv (op0,
591 (integer_zerop (op1)
592 ? false_val : true_val));
593 edge_info = new class edge_info (false_edge);
594 edge_info->record_simple_equiv (op0,
595 (integer_zerop (op1)
596 ? true_val : false_val));
598 else
600 edge_info = new class edge_info (true_edge);
601 edge_info->record_simple_equiv (op0,
602 (integer_zerop (op1)
603 ? true_val : false_val));
604 edge_info = new class edge_info (false_edge);
605 edge_info->record_simple_equiv (op0,
606 (integer_zerop (op1)
607 ? false_val : true_val));
610 /* This can show up in the IL as a result of copy propagation
611 it will eventually be canonicalized, but we have to cope
612 with this case within the pass. */
613 else if (is_gimple_min_invariant (op0)
614 && TREE_CODE (op1) == SSA_NAME)
616 tree cond = build2 (code, boolean_type_node, op0, op1);
617 tree inverted = invert_truthvalue_loc (loc, cond);
618 bool can_infer_simple_equiv
619 = !(HONOR_SIGNED_ZEROS (op0) && real_maybe_zerop (op0))
620 && !DECIMAL_FLOAT_MODE_P (element_mode (TREE_TYPE (op0)));
621 class edge_info *edge_info;
623 edge_info = new class edge_info (true_edge);
624 record_conditions (&edge_info->cond_equivalences, cond, inverted);
626 if (can_infer_simple_equiv && code == EQ_EXPR)
627 edge_info->record_simple_equiv (op1, op0);
629 edge_info = new class edge_info (false_edge);
630 record_conditions (&edge_info->cond_equivalences, inverted, cond);
632 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
633 edge_info->record_simple_equiv (op1, op0);
636 else if (TREE_CODE (op0) == SSA_NAME
637 && (TREE_CODE (op1) == SSA_NAME
638 || is_gimple_min_invariant (op1)))
640 tree cond = build2 (code, boolean_type_node, op0, op1);
641 tree inverted = invert_truthvalue_loc (loc, cond);
642 bool can_infer_simple_equiv
643 = !(HONOR_SIGNED_ZEROS (op1) && real_maybe_zerop (op1))
644 && !DECIMAL_FLOAT_MODE_P (element_mode (TREE_TYPE (op1)));
645 class edge_info *edge_info;
647 edge_info = new class edge_info (true_edge);
648 record_conditions (&edge_info->cond_equivalences, cond, inverted);
650 if (can_infer_simple_equiv && code == EQ_EXPR)
651 edge_info->record_simple_equiv (op0, op1);
653 edge_info = new class edge_info (false_edge);
654 record_conditions (&edge_info->cond_equivalences, inverted, cond);
656 if (can_infer_simple_equiv && TREE_CODE (inverted) == EQ_EXPR)
657 edge_info->record_simple_equiv (op0, op1);
660 /* If this block is a single block loop, then we may be able to
661 record some equivalences on the loop's exit edge. */
662 if (single_block_loop_p (bb))
664 /* We know it's a single block loop. Now look at the loop
665 exit condition. What we're looking for is whether or not
666 the exit condition is loop invariant which we can detect
667 by checking if all the SSA_NAMEs referenced are defined
668 outside the loop. */
669 if ((TREE_CODE (op0) != SSA_NAME
670 || gimple_bb (SSA_NAME_DEF_STMT (op0)) != bb)
671 && (TREE_CODE (op1) != SSA_NAME
672 || gimple_bb (SSA_NAME_DEF_STMT (op1)) != bb))
674 /* At this point we know the exit condition is loop
675 invariant. The only way to get out of the loop is
676 if it never traverses the backedge to begin with. This
677 implies that any PHI nodes create equivalances that we
678 can attach to the loop exit edge. */
679 bool alternative
680 = (EDGE_PRED (bb, 0)->flags & EDGE_DFS_BACK) ? 1 : 0;
682 gphi_iterator gsi;
683 for (gsi = gsi_start_phis (bb);
684 !gsi_end_p (gsi);
685 gsi_next (&gsi))
687 /* Now get the EDGE_INFO class so we can append
688 it to our list. We want the successor edge
689 where the destination is not the source of
690 an incoming edge. */
691 gphi *phi = gsi.phi ();
692 tree src = PHI_ARG_DEF (phi, alternative);
693 tree dst = PHI_RESULT (phi);
695 /* If the other alternative is the same as the result,
696 then this is a degenerate and can be ignored. */
697 if (dst == PHI_ARG_DEF (phi, !alternative))
698 continue;
700 if (EDGE_SUCC (bb, 0)->dest
701 != EDGE_PRED (bb, !alternative)->src)
702 edge_info = (class edge_info *)EDGE_SUCC (bb, 0)->aux;
703 else
704 edge_info = (class edge_info *)EDGE_SUCC (bb, 1)->aux;
706 /* Note that since this processing is done independently
707 of other edge equivalency processing, we may not
708 have an EDGE_INFO structure set up yet. */
709 if (edge_info == NULL)
710 edge_info = new class edge_info (false_edge);
711 edge_info->record_simple_equiv (dst, src);
719 class dom_jt_state : public jt_state
721 public:
722 dom_jt_state (const_and_copies *copies, avail_exprs_stack *avails)
723 : m_copies (copies), m_avails (avails)
725 bitmap_tree_view (m_blocks_on_stack);
727 void push (edge e) override
729 m_copies->push_marker ();
730 m_avails->push_marker ();
731 jt_state::push (e);
733 void pop () override
735 m_copies->pop_to_marker ();
736 m_avails->pop_to_marker ();
737 jt_state::pop ();
739 void register_equivs_edge (edge e) override
741 record_temporary_equivalences (e, m_copies, m_avails, m_blocks_on_stack);
743 void register_equiv (tree dest, tree src, bool update) override;
744 bitmap get_blocks_on_stack () { return m_blocks_on_stack; }
745 private:
746 const_and_copies *m_copies;
747 avail_exprs_stack *m_avails;
748 /* Set of blocks on the stack, to be used for medium-fast
749 dominance queries in back_propagate_equivalences. */
750 auto_bitmap m_blocks_on_stack;
753 void
754 dom_jt_state::register_equiv (tree dest, tree src, bool)
756 m_copies->record_const_or_copy (dest, src);
759 class dom_jt_simplifier : public hybrid_jt_simplifier
761 public:
762 dom_jt_simplifier (avail_exprs_stack *avails, gimple_ranger *ranger,
763 path_range_query *query)
764 : hybrid_jt_simplifier (ranger, query), m_avails (avails) { }
766 private:
767 tree simplify (gimple *, gimple *, basic_block, jt_state *) override;
768 avail_exprs_stack *m_avails;
771 tree
772 dom_jt_simplifier::simplify (gimple *stmt, gimple *within_stmt,
773 basic_block bb, jt_state *state)
775 /* First see if the conditional is in the hash table. */
776 tree cached_lhs = m_avails->lookup_avail_expr (stmt, false, true);
777 if (cached_lhs)
778 return cached_lhs;
780 /* Otherwise call the ranger if possible. */
781 if (state)
782 return hybrid_jt_simplifier::simplify (stmt, within_stmt, bb, state);
784 return NULL;
787 class dom_opt_dom_walker : public dom_walker
789 public:
790 dom_opt_dom_walker (cdi_direction direction,
791 jump_threader *threader,
792 dom_jt_state *state,
793 gimple_ranger *ranger,
794 const_and_copies *const_and_copies,
795 avail_exprs_stack *avail_exprs_stack)
796 : dom_walker (direction, REACHABLE_BLOCKS)
798 m_ranger = ranger;
799 m_state = state;
800 m_dummy_cond = gimple_build_cond (NE_EXPR, integer_zero_node,
801 integer_zero_node, NULL, NULL);
802 m_const_and_copies = const_and_copies;
803 m_avail_exprs_stack = avail_exprs_stack;
804 m_threader = threader;
807 edge before_dom_children (basic_block) final override;
808 void after_dom_children (basic_block) final override;
810 private:
812 /* Unwindable equivalences, both const/copy and expression varieties. */
813 class const_and_copies *m_const_and_copies;
814 class avail_exprs_stack *m_avail_exprs_stack;
816 /* Dummy condition to avoid creating lots of throw away statements. */
817 gcond *m_dummy_cond;
819 /* Optimize a single statement within a basic block using the
820 various tables mantained by DOM. Returns the taken edge if
821 the statement is a conditional with a statically determined
822 value. */
823 edge optimize_stmt (basic_block, gimple_stmt_iterator *, bool *);
825 void set_global_ranges_from_unreachable_edges (basic_block);
827 void test_for_singularity (gimple *, avail_exprs_stack *);
828 edge fold_cond (gcond *cond);
830 jump_threader *m_threader;
831 gimple_ranger *m_ranger;
832 dom_jt_state *m_state;
835 /* Jump threading, redundancy elimination and const/copy propagation.
837 This pass may expose new symbols that need to be renamed into SSA. For
838 every new symbol exposed, its corresponding bit will be set in
839 VARS_TO_RENAME. */
841 namespace {
843 const pass_data pass_data_dominator =
845 GIMPLE_PASS, /* type */
846 "dom", /* name */
847 OPTGROUP_NONE, /* optinfo_flags */
848 TV_TREE_SSA_DOMINATOR_OPTS, /* tv_id */
849 ( PROP_cfg | PROP_ssa ), /* properties_required */
850 0, /* properties_provided */
851 0, /* properties_destroyed */
852 0, /* todo_flags_start */
853 ( TODO_cleanup_cfg | TODO_update_ssa ), /* todo_flags_finish */
856 class pass_dominator : public gimple_opt_pass
858 public:
859 pass_dominator (gcc::context *ctxt)
860 : gimple_opt_pass (pass_data_dominator, ctxt),
861 may_peel_loop_headers_p (false)
864 /* opt_pass methods: */
865 opt_pass * clone () final override { return new pass_dominator (m_ctxt); }
866 void set_pass_param (unsigned int n, bool param) final override
868 gcc_assert (n == 0);
869 may_peel_loop_headers_p = param;
871 bool gate (function *) final override { return flag_tree_dom != 0; }
872 unsigned int execute (function *) final override;
874 private:
875 /* This flag is used to prevent loops from being peeled repeatedly in jump
876 threading; it will be removed once we preserve loop structures throughout
877 the compilation -- we will be able to mark the affected loops directly in
878 jump threading, and avoid peeling them next time. */
879 bool may_peel_loop_headers_p;
880 }; // class pass_dominator
882 unsigned int
883 pass_dominator::execute (function *fun)
885 memset (&opt_stats, 0, sizeof (opt_stats));
887 /* Create our hash tables. */
888 hash_table<expr_elt_hasher> *avail_exprs
889 = new hash_table<expr_elt_hasher> (1024);
890 class avail_exprs_stack *avail_exprs_stack
891 = new class avail_exprs_stack (avail_exprs);
892 class const_and_copies *const_and_copies = new class const_and_copies ();
893 need_eh_cleanup = BITMAP_ALLOC (NULL);
894 need_noreturn_fixup.create (0);
896 calculate_dominance_info (CDI_DOMINATORS);
897 cfg_altered = false;
899 /* We need to know loop structures in order to avoid destroying them
900 in jump threading. Note that we still can e.g. thread through loop
901 headers to an exit edge, or through loop header to the loop body, assuming
902 that we update the loop info.
904 TODO: We don't need to set LOOPS_HAVE_PREHEADERS generally, but due
905 to several overly conservative bail-outs in jump threading, case
906 gcc.dg/tree-ssa/pr21417.c can't be threaded if loop preheader is
907 missing. We should improve jump threading in future then
908 LOOPS_HAVE_PREHEADERS won't be needed here. */
909 loop_optimizer_init (LOOPS_HAVE_PREHEADERS | LOOPS_HAVE_SIMPLE_LATCHES
910 | LOOPS_HAVE_MARKED_IRREDUCIBLE_REGIONS);
912 /* We need accurate information regarding back edges in the CFG
913 for jump threading; this may include back edges that are not part of
914 a single loop. */
915 mark_dfs_back_edges ();
917 /* We want to create the edge info structures before the dominator walk
918 so that they'll be in place for the jump threader, particularly when
919 threading through a join block.
921 The conditions will be lazily updated with global equivalences as
922 we reach them during the dominator walk. */
923 basic_block bb;
924 FOR_EACH_BB_FN (bb, fun)
925 record_edge_info (bb);
927 /* Recursively walk the dominator tree optimizing statements. */
928 gimple_ranger *ranger = enable_ranger (fun);
929 path_range_query path_query (*ranger);
930 dom_jt_simplifier simplifier (avail_exprs_stack, ranger, &path_query);
931 dom_jt_state state (const_and_copies, avail_exprs_stack);
932 jump_threader threader (&simplifier, &state);
933 dom_opt_dom_walker walker (CDI_DOMINATORS,
934 &threader,
935 &state,
936 ranger,
937 const_and_copies,
938 avail_exprs_stack);
939 walker.walk (fun->cfg->x_entry_block_ptr);
941 ranger->export_global_ranges ();
942 disable_ranger (fun);
944 /* Look for blocks where we cleared EDGE_EXECUTABLE on an outgoing
945 edge. When found, remove jump threads which contain any outgoing
946 edge from the affected block. */
947 if (cfg_altered)
949 FOR_EACH_BB_FN (bb, fun)
951 edge_iterator ei;
952 edge e;
954 /* First see if there are any edges without EDGE_EXECUTABLE
955 set. */
956 bool found = false;
957 FOR_EACH_EDGE (e, ei, bb->succs)
959 if ((e->flags & EDGE_EXECUTABLE) == 0)
961 found = true;
962 break;
966 /* If there were any such edges found, then remove jump threads
967 containing any edge leaving BB. */
968 if (found)
969 FOR_EACH_EDGE (e, ei, bb->succs)
970 threader.remove_jump_threads_including (e);
975 gimple_stmt_iterator gsi;
976 basic_block bb;
977 FOR_EACH_BB_FN (bb, fun)
979 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
980 update_stmt_if_modified (gsi_stmt (gsi));
984 /* If we exposed any new variables, go ahead and put them into
985 SSA form now, before we handle jump threading. This simplifies
986 interactions between rewriting of _DECL nodes into SSA form
987 and rewriting SSA_NAME nodes into SSA form after block
988 duplication and CFG manipulation. */
989 update_ssa (TODO_update_ssa);
991 free_all_edge_infos ();
993 /* Thread jumps, creating duplicate blocks as needed. */
994 cfg_altered |= threader.thread_through_all_blocks (may_peel_loop_headers_p);
996 if (cfg_altered)
997 free_dominance_info (CDI_DOMINATORS);
999 /* Removal of statements may make some EH edges dead. Purge
1000 such edges from the CFG as needed. */
1001 if (!bitmap_empty_p (need_eh_cleanup))
1003 unsigned i;
1004 bitmap_iterator bi;
1006 /* Jump threading may have created forwarder blocks from blocks
1007 needing EH cleanup; the new successor of these blocks, which
1008 has inherited from the original block, needs the cleanup.
1009 Don't clear bits in the bitmap, as that can break the bitmap
1010 iterator. */
1011 EXECUTE_IF_SET_IN_BITMAP (need_eh_cleanup, 0, i, bi)
1013 basic_block bb = BASIC_BLOCK_FOR_FN (fun, i);
1014 if (bb == NULL)
1015 continue;
1016 while (single_succ_p (bb)
1017 && (single_succ_edge (bb)->flags
1018 & (EDGE_EH|EDGE_DFS_BACK)) == 0)
1019 bb = single_succ (bb);
1020 if (bb == EXIT_BLOCK_PTR_FOR_FN (fun))
1021 continue;
1022 if ((unsigned) bb->index != i)
1023 bitmap_set_bit (need_eh_cleanup, bb->index);
1026 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
1027 bitmap_clear (need_eh_cleanup);
1030 /* Fixup stmts that became noreturn calls. This may require splitting
1031 blocks and thus isn't possible during the dominator walk or before
1032 jump threading finished. Do this in reverse order so we don't
1033 inadvertedly remove a stmt we want to fixup by visiting a dominating
1034 now noreturn call first. */
1035 while (!need_noreturn_fixup.is_empty ())
1037 gimple *stmt = need_noreturn_fixup.pop ();
1038 if (dump_file && dump_flags & TDF_DETAILS)
1040 fprintf (dump_file, "Fixing up noreturn call ");
1041 print_gimple_stmt (dump_file, stmt, 0);
1042 fprintf (dump_file, "\n");
1044 fixup_noreturn_call (stmt);
1047 statistics_counter_event (fun, "Redundant expressions eliminated",
1048 opt_stats.num_re);
1049 statistics_counter_event (fun, "Constants propagated",
1050 opt_stats.num_const_prop);
1051 statistics_counter_event (fun, "Copies propagated",
1052 opt_stats.num_copy_prop);
1054 /* Debugging dumps. */
1055 if (dump_file && (dump_flags & TDF_STATS))
1056 dump_dominator_optimization_stats (dump_file, avail_exprs);
1058 loop_optimizer_finalize ();
1060 /* Delete our main hashtable. */
1061 delete avail_exprs;
1062 avail_exprs = NULL;
1064 /* Free asserted bitmaps and stacks. */
1065 BITMAP_FREE (need_eh_cleanup);
1066 need_noreturn_fixup.release ();
1067 delete avail_exprs_stack;
1068 delete const_and_copies;
1070 return 0;
1073 } // anon namespace
1075 gimple_opt_pass *
1076 make_pass_dominator (gcc::context *ctxt)
1078 return new pass_dominator (ctxt);
1081 /* Valueize hook for gimple_fold_stmt_to_constant_1. */
1083 static tree
1084 dom_valueize (tree t)
1086 if (TREE_CODE (t) == SSA_NAME)
1088 tree tem = SSA_NAME_VALUE (t);
1089 if (tem)
1090 return tem;
1092 return t;
1095 /* We have just found an equivalence for LHS on an edge E.
1096 Look backwards to other uses of LHS and see if we can derive
1097 additional equivalences that are valid on edge E. */
1098 static void
1099 back_propagate_equivalences (tree lhs, edge e,
1100 class const_and_copies *const_and_copies,
1101 bitmap domby)
1103 use_operand_p use_p;
1104 imm_use_iterator iter;
1105 basic_block dest = e->dest;
1106 bool domok = (dom_info_state (CDI_DOMINATORS) == DOM_OK);
1108 /* Iterate over the uses of LHS to see if any dominate E->dest.
1109 If so, they may create useful equivalences too.
1111 ??? If the code gets re-organized to a worklist to catch more
1112 indirect opportunities and it is made to handle PHIs then this
1113 should only consider use_stmts in basic-blocks we have already visited. */
1114 FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
1116 gimple *use_stmt = USE_STMT (use_p);
1118 /* Often the use is in DEST, which we trivially know we can't use.
1119 This is cheaper than the dominator set tests below. */
1120 if (dest == gimple_bb (use_stmt))
1121 continue;
1123 /* Filter out statements that can never produce a useful
1124 equivalence. */
1125 tree lhs2 = gimple_get_lhs (use_stmt);
1126 if (!lhs2 || TREE_CODE (lhs2) != SSA_NAME)
1127 continue;
1129 if (domok)
1131 if (!dominated_by_p (CDI_DOMINATORS, dest, gimple_bb (use_stmt)))
1132 continue;
1134 else
1136 /* We can use the set of BBs on the stack from a domwalk
1137 for a medium fast way to query dominance. Profiling
1138 has shown non-fast query dominance tests here can be fairly
1139 expensive. */
1140 /* This tests if USE_STMT does not dominate DEST. */
1141 if (!bitmap_bit_p (domby, gimple_bb (use_stmt)->index))
1142 continue;
1145 /* At this point USE_STMT dominates DEST and may result in a
1146 useful equivalence. Try to simplify its RHS to a constant
1147 or SSA_NAME. */
1148 tree res = gimple_fold_stmt_to_constant_1 (use_stmt, dom_valueize,
1149 no_follow_ssa_edges);
1150 if (res && (TREE_CODE (res) == SSA_NAME || is_gimple_min_invariant (res)))
1151 record_equality (lhs2, res, const_and_copies);
1155 /* Record into CONST_AND_COPIES and AVAIL_EXPRS_STACK any equivalences implied
1156 by traversing edge E (which are cached in E->aux).
1158 Callers are responsible for managing the unwinding markers. */
1159 static void
1160 record_temporary_equivalences (edge e,
1161 class const_and_copies *const_and_copies,
1162 class avail_exprs_stack *avail_exprs_stack,
1163 bitmap blocks_on_stack)
1165 int i;
1166 class edge_info *edge_info = (class edge_info *) e->aux;
1168 /* If we have info associated with this edge, record it into
1169 our equivalence tables. */
1170 if (edge_info)
1172 cond_equivalence *eq;
1173 /* If we have 0 = COND or 1 = COND equivalences, record them
1174 into our expression hash tables. */
1175 for (i = 0; edge_info->cond_equivalences.iterate (i, &eq); ++i)
1176 avail_exprs_stack->record_cond (eq);
1178 edge_info::equiv_pair *seq;
1179 for (i = 0; edge_info->simple_equivalences.iterate (i, &seq); ++i)
1181 tree lhs = seq->first;
1182 if (!lhs || TREE_CODE (lhs) != SSA_NAME)
1183 continue;
1185 /* Record the simple NAME = VALUE equivalence. */
1186 tree rhs = seq->second;
1188 /* If this is a SSA_NAME = SSA_NAME equivalence and one operand is
1189 cheaper to compute than the other, then set up the equivalence
1190 such that we replace the expensive one with the cheap one.
1192 If they are the same cost to compute, then do not record
1193 anything. */
1194 if (TREE_CODE (lhs) == SSA_NAME && TREE_CODE (rhs) == SSA_NAME)
1196 gimple *rhs_def = SSA_NAME_DEF_STMT (rhs);
1197 int rhs_cost = estimate_num_insns (rhs_def, &eni_size_weights);
1199 gimple *lhs_def = SSA_NAME_DEF_STMT (lhs);
1200 int lhs_cost = estimate_num_insns (lhs_def, &eni_size_weights);
1202 if (rhs_cost > lhs_cost)
1203 record_equality (rhs, lhs, const_and_copies);
1204 else if (rhs_cost < lhs_cost)
1205 record_equality (lhs, rhs, const_and_copies);
1207 else
1208 record_equality (lhs, rhs, const_and_copies);
1211 /* Any equivalence found for LHS may result in additional
1212 equivalences for other uses of LHS that we have already
1213 processed. */
1214 back_propagate_equivalences (lhs, e, const_and_copies,
1215 blocks_on_stack);
1220 /* PHI nodes can create equivalences too.
1222 Ignoring any alternatives which are the same as the result, if
1223 all the alternatives are equal, then the PHI node creates an
1224 equivalence. */
1226 static void
1227 record_equivalences_from_phis (basic_block bb)
1229 gphi_iterator gsi;
1231 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); )
1233 gphi *phi = gsi.phi ();
1235 /* We might eliminate the PHI, so advance GSI now. */
1236 gsi_next (&gsi);
1238 tree lhs = gimple_phi_result (phi);
1239 tree rhs = NULL;
1240 size_t i;
1242 for (i = 0; i < gimple_phi_num_args (phi); i++)
1244 tree t = gimple_phi_arg_def (phi, i);
1246 /* Ignore alternatives which are the same as our LHS. Since
1247 LHS is a PHI_RESULT, it is known to be a SSA_NAME, so we
1248 can simply compare pointers. */
1249 if (lhs == t)
1250 continue;
1252 /* If the associated edge is not marked as executable, then it
1253 can be ignored. */
1254 if ((gimple_phi_arg_edge (phi, i)->flags & EDGE_EXECUTABLE) == 0)
1255 continue;
1257 t = dom_valueize (t);
1259 /* If T is an SSA_NAME and its associated edge is a backedge,
1260 then quit as we cannot utilize this equivalence. */
1261 if (TREE_CODE (t) == SSA_NAME
1262 && (gimple_phi_arg_edge (phi, i)->flags & EDGE_DFS_BACK))
1263 break;
1265 /* If we have not processed an alternative yet, then set
1266 RHS to this alternative. */
1267 if (rhs == NULL)
1268 rhs = t;
1269 /* If we have processed an alternative (stored in RHS), then
1270 see if it is equal to this one. If it isn't, then stop
1271 the search. */
1272 else if (! operand_equal_for_phi_arg_p (rhs, t))
1273 break;
1276 /* If we had no interesting alternatives, then all the RHS alternatives
1277 must have been the same as LHS. */
1278 if (!rhs)
1279 rhs = lhs;
1281 /* If we managed to iterate through each PHI alternative without
1282 breaking out of the loop, then we have a PHI which may create
1283 a useful equivalence. We do not need to record unwind data for
1284 this, since this is a true assignment and not an equivalence
1285 inferred from a comparison. All uses of this ssa name are dominated
1286 by this assignment, so unwinding just costs time and space. */
1287 if (i == gimple_phi_num_args (phi))
1289 if (may_propagate_copy (lhs, rhs))
1290 set_ssa_name_value (lhs, rhs);
1291 else if (virtual_operand_p (lhs))
1293 gimple *use_stmt;
1294 imm_use_iterator iter;
1295 use_operand_p use_p;
1296 /* For virtual operands we have to propagate into all uses as
1297 otherwise we will create overlapping life-ranges. */
1298 FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
1299 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
1300 SET_USE (use_p, rhs);
1301 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs))
1302 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs) = 1;
1303 gimple_stmt_iterator tmp_gsi = gsi_for_stmt (phi);
1304 remove_phi_node (&tmp_gsi, true);
1310 /* Return true if all uses of NAME are dominated by STMT or feed STMT
1311 via a chain of single immediate uses. */
1313 static bool
1314 all_uses_feed_or_dominated_by_stmt (tree name, gimple *stmt)
1316 use_operand_p use_p, use2_p;
1317 imm_use_iterator iter;
1318 basic_block stmt_bb = gimple_bb (stmt);
1320 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
1322 gimple *use_stmt = USE_STMT (use_p), *use_stmt2;
1323 if (use_stmt == stmt
1324 || is_gimple_debug (use_stmt)
1325 || (gimple_bb (use_stmt) != stmt_bb
1326 && dominated_by_p (CDI_DOMINATORS,
1327 gimple_bb (use_stmt), stmt_bb)))
1328 continue;
1329 while (use_stmt != stmt
1330 && is_gimple_assign (use_stmt)
1331 && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
1332 && single_imm_use (gimple_assign_lhs (use_stmt),
1333 &use2_p, &use_stmt2))
1334 use_stmt = use_stmt2;
1335 if (use_stmt != stmt)
1336 return false;
1338 return true;
1341 /* Set global ranges that can be determined from the C->M edge:
1343 <bb C>:
1345 if (something)
1346 goto <bb N>;
1347 else
1348 goto <bb M>;
1349 <bb N>:
1350 __builtin_unreachable ();
1351 <bb M>:
1354 void
1355 dom_opt_dom_walker::set_global_ranges_from_unreachable_edges (basic_block bb)
1357 edge pred_e = single_pred_edge_ignoring_loop_edges (bb, false);
1358 if (!pred_e)
1359 return;
1361 gimple *stmt = last_stmt (pred_e->src);
1362 if (!stmt
1363 || gimple_code (stmt) != GIMPLE_COND
1364 || !assert_unreachable_fallthru_edge_p (pred_e))
1365 return;
1367 tree name;
1368 gori_compute &gori = m_ranger->gori ();
1369 FOR_EACH_GORI_EXPORT_NAME (gori, pred_e->src, name)
1370 if (all_uses_feed_or_dominated_by_stmt (name, stmt)
1371 // The condition must post-dominate the definition point.
1372 && (SSA_NAME_IS_DEFAULT_DEF (name)
1373 || (gimple_bb (SSA_NAME_DEF_STMT (name))
1374 == pred_e->src)))
1376 Value_Range r (TREE_TYPE (name));
1378 if (m_ranger->range_on_edge (r, pred_e, name)
1379 && !r.varying_p ()
1380 && !r.undefined_p ())
1382 set_range_info (name, r);
1383 maybe_set_nonzero_bits (pred_e, name);
1388 /* Record any equivalences created by the incoming edge to BB into
1389 CONST_AND_COPIES and AVAIL_EXPRS_STACK. If BB has more than one
1390 incoming edge, then no equivalence is created. */
1392 static void
1393 record_equivalences_from_incoming_edge (basic_block bb,
1394 class const_and_copies *const_and_copies,
1395 class avail_exprs_stack *avail_exprs_stack,
1396 bitmap blocks_on_stack)
1398 edge e;
1399 basic_block parent;
1401 /* If our parent block ended with a control statement, then we may be
1402 able to record some equivalences based on which outgoing edge from
1403 the parent was followed. */
1404 parent = get_immediate_dominator (CDI_DOMINATORS, bb);
1406 e = single_pred_edge_ignoring_loop_edges (bb, true);
1408 /* If we had a single incoming edge from our parent block, then enter
1409 any data associated with the edge into our tables. */
1410 if (e && e->src == parent)
1411 record_temporary_equivalences (e, const_and_copies, avail_exprs_stack,
1412 blocks_on_stack);
1415 /* Dump statistics for the hash table HTAB. */
1417 static void
1418 htab_statistics (FILE *file, const hash_table<expr_elt_hasher> &htab)
1420 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1421 (long) htab.size (),
1422 (long) htab.elements (),
1423 htab.collisions ());
1426 /* Dump SSA statistics on FILE. */
1428 static void
1429 dump_dominator_optimization_stats (FILE *file,
1430 hash_table<expr_elt_hasher> *avail_exprs)
1432 fprintf (file, "Total number of statements: %6ld\n\n",
1433 opt_stats.num_stmts);
1434 fprintf (file, "Exprs considered for dominator optimizations: %6ld\n",
1435 opt_stats.num_exprs_considered);
1437 fprintf (file, "\nHash table statistics:\n");
1439 fprintf (file, " avail_exprs: ");
1440 htab_statistics (file, *avail_exprs);
1444 /* Similarly, but assume that X and Y are the two operands of an EQ_EXPR.
1445 This constrains the cases in which we may treat this as assignment. */
1447 static void
1448 record_equality (tree x, tree y, class const_and_copies *const_and_copies)
1450 tree prev_x = NULL, prev_y = NULL;
1452 if (tree_swap_operands_p (x, y))
1453 std::swap (x, y);
1455 /* Most of the time tree_swap_operands_p does what we want. But there
1456 are cases where we know one operand is better for copy propagation than
1457 the other. Given no other code cares about ordering of equality
1458 comparison operators for that purpose, we just handle the special cases
1459 here. */
1460 if (TREE_CODE (x) == SSA_NAME && TREE_CODE (y) == SSA_NAME)
1462 /* If one operand is a single use operand, then make it
1463 X. This will preserve its single use properly and if this
1464 conditional is eliminated, the computation of X can be
1465 eliminated as well. */
1466 if (has_single_use (y) && ! has_single_use (x))
1467 std::swap (x, y);
1469 if (TREE_CODE (x) == SSA_NAME)
1470 prev_x = SSA_NAME_VALUE (x);
1471 if (TREE_CODE (y) == SSA_NAME)
1472 prev_y = SSA_NAME_VALUE (y);
1474 /* If one of the previous values is invariant, or invariant in more loops
1475 (by depth), then use that.
1476 Otherwise it doesn't matter which value we choose, just so
1477 long as we canonicalize on one value. */
1478 if (is_gimple_min_invariant (y))
1480 else if (is_gimple_min_invariant (x))
1481 prev_x = x, x = y, y = prev_x, prev_x = prev_y;
1482 else if (prev_x && is_gimple_min_invariant (prev_x))
1483 x = y, y = prev_x, prev_x = prev_y;
1484 else if (prev_y)
1485 y = prev_y;
1487 /* After the swapping, we must have one SSA_NAME. */
1488 if (TREE_CODE (x) != SSA_NAME)
1489 return;
1491 /* For IEEE, -0.0 == 0.0, so we don't necessarily know the sign of a
1492 variable compared against zero. If we're honoring signed zeros,
1493 then we cannot record this value unless we know that the value is
1494 nonzero. */
1495 if (HONOR_SIGNED_ZEROS (x)
1496 && (TREE_CODE (y) != REAL_CST
1497 || real_equal (&dconst0, &TREE_REAL_CST (y))))
1498 return;
1500 const_and_copies->record_const_or_copy (x, y, prev_x);
1503 /* Returns true when STMT is a simple iv increment. It detects the
1504 following situation:
1506 i_1 = phi (..., i_k)
1507 [...]
1508 i_j = i_{j-1} for each j : 2 <= j <= k-1
1509 [...]
1510 i_k = i_{k-1} +/- ... */
1512 bool
1513 simple_iv_increment_p (gimple *stmt)
1515 enum tree_code code;
1516 tree lhs, preinc;
1517 gimple *phi;
1518 size_t i;
1520 if (gimple_code (stmt) != GIMPLE_ASSIGN)
1521 return false;
1523 lhs = gimple_assign_lhs (stmt);
1524 if (TREE_CODE (lhs) != SSA_NAME)
1525 return false;
1527 code = gimple_assign_rhs_code (stmt);
1528 if (code != PLUS_EXPR
1529 && code != MINUS_EXPR
1530 && code != POINTER_PLUS_EXPR)
1531 return false;
1533 preinc = gimple_assign_rhs1 (stmt);
1534 if (TREE_CODE (preinc) != SSA_NAME)
1535 return false;
1537 phi = SSA_NAME_DEF_STMT (preinc);
1538 while (gimple_code (phi) != GIMPLE_PHI)
1540 /* Follow trivial copies, but not the DEF used in a back edge,
1541 so that we don't prevent coalescing. */
1542 if (!gimple_assign_ssa_name_copy_p (phi))
1543 return false;
1544 preinc = gimple_assign_rhs1 (phi);
1545 phi = SSA_NAME_DEF_STMT (preinc);
1548 for (i = 0; i < gimple_phi_num_args (phi); i++)
1549 if (gimple_phi_arg_def (phi, i) == lhs)
1550 return true;
1552 return false;
1555 /* Propagate know values from SSA_NAME_VALUE into the PHI nodes of the
1556 successors of BB. */
1558 static void
1559 cprop_into_successor_phis (basic_block bb,
1560 class const_and_copies *const_and_copies)
1562 edge e;
1563 edge_iterator ei;
1565 FOR_EACH_EDGE (e, ei, bb->succs)
1567 int indx;
1568 gphi_iterator gsi;
1570 /* If this is an abnormal edge, then we do not want to copy propagate
1571 into the PHI alternative associated with this edge. */
1572 if (e->flags & EDGE_ABNORMAL)
1573 continue;
1575 gsi = gsi_start_phis (e->dest);
1576 if (gsi_end_p (gsi))
1577 continue;
1579 /* We may have an equivalence associated with this edge. While
1580 we cannot propagate it into non-dominated blocks, we can
1581 propagate them into PHIs in non-dominated blocks. */
1583 /* Push the unwind marker so we can reset the const and copies
1584 table back to its original state after processing this edge. */
1585 const_and_copies->push_marker ();
1587 /* Extract and record any simple NAME = VALUE equivalences.
1589 Don't bother with [01] = COND equivalences, they're not useful
1590 here. */
1591 class edge_info *edge_info = (class edge_info *) e->aux;
1593 if (edge_info)
1595 edge_info::equiv_pair *seq;
1596 for (int i = 0; edge_info->simple_equivalences.iterate (i, &seq); ++i)
1598 tree lhs = seq->first;
1599 tree rhs = seq->second;
1601 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1602 const_and_copies->record_const_or_copy (lhs, rhs);
1607 indx = e->dest_idx;
1608 for ( ; !gsi_end_p (gsi); gsi_next (&gsi))
1610 tree new_val;
1611 use_operand_p orig_p;
1612 tree orig_val;
1613 gphi *phi = gsi.phi ();
1615 /* The alternative may be associated with a constant, so verify
1616 it is an SSA_NAME before doing anything with it. */
1617 orig_p = gimple_phi_arg_imm_use_ptr (phi, indx);
1618 orig_val = get_use_from_ptr (orig_p);
1619 if (TREE_CODE (orig_val) != SSA_NAME)
1620 continue;
1622 /* If we have *ORIG_P in our constant/copy table, then replace
1623 ORIG_P with its value in our constant/copy table. */
1624 new_val = SSA_NAME_VALUE (orig_val);
1625 if (new_val
1626 && new_val != orig_val
1627 && may_propagate_copy (orig_val, new_val))
1628 propagate_value (orig_p, new_val);
1631 const_and_copies->pop_to_marker ();
1635 edge
1636 dom_opt_dom_walker::before_dom_children (basic_block bb)
1638 gimple_stmt_iterator gsi;
1640 if (dump_file && (dump_flags & TDF_DETAILS))
1641 fprintf (dump_file, "\n\nOptimizing block #%d\n\n", bb->index);
1643 /* Push a marker on the stacks of local information so that we know how
1644 far to unwind when we finalize this block. */
1645 m_avail_exprs_stack->push_marker ();
1646 m_const_and_copies->push_marker ();
1647 bitmap_set_bit (m_state->get_blocks_on_stack (), bb->index);
1649 record_equivalences_from_incoming_edge (bb, m_const_and_copies,
1650 m_avail_exprs_stack,
1651 m_state->get_blocks_on_stack ());
1652 set_global_ranges_from_unreachable_edges (bb);
1654 /* PHI nodes can create equivalences too. */
1655 record_equivalences_from_phis (bb);
1657 /* Create equivalences from redundant PHIs. PHIs are only truly
1658 redundant when they exist in the same block, so push another
1659 marker and unwind right afterwards. */
1660 m_avail_exprs_stack->push_marker ();
1661 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1662 eliminate_redundant_computations (&gsi, m_const_and_copies,
1663 m_avail_exprs_stack);
1664 m_avail_exprs_stack->pop_to_marker ();
1666 edge taken_edge = NULL;
1667 /* Initialize visited flag ahead of us, it has undefined state on
1668 pass entry. */
1669 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1670 gimple_set_visited (gsi_stmt (gsi), false);
1671 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi);)
1673 /* Do not optimize a stmt twice, substitution might end up with
1674 _3 = _3 which is not valid. */
1675 if (gimple_visited_p (gsi_stmt (gsi)))
1677 gsi_next (&gsi);
1678 continue;
1681 bool removed_p = false;
1682 taken_edge = this->optimize_stmt (bb, &gsi, &removed_p);
1683 if (!removed_p)
1684 gimple_set_visited (gsi_stmt (gsi), true);
1686 /* Go back and visit stmts inserted by folding after substituting
1687 into the stmt at gsi. */
1688 if (gsi_end_p (gsi))
1690 gcc_checking_assert (removed_p);
1691 gsi = gsi_last_bb (bb);
1692 while (!gsi_end_p (gsi) && !gimple_visited_p (gsi_stmt (gsi)))
1693 gsi_prev (&gsi);
1695 else
1699 gsi_prev (&gsi);
1701 while (!gsi_end_p (gsi) && !gimple_visited_p (gsi_stmt (gsi)));
1703 if (gsi_end_p (gsi))
1704 gsi = gsi_start_bb (bb);
1705 else
1706 gsi_next (&gsi);
1709 /* Now prepare to process dominated blocks. */
1710 record_edge_info (bb);
1711 cprop_into_successor_phis (bb, m_const_and_copies);
1712 if (taken_edge && !dbg_cnt (dom_unreachable_edges))
1713 return NULL;
1715 return taken_edge;
1718 /* We have finished processing the dominator children of BB, perform
1719 any finalization actions in preparation for leaving this node in
1720 the dominator tree. */
1722 void
1723 dom_opt_dom_walker::after_dom_children (basic_block bb)
1725 m_threader->thread_outgoing_edges (bb);
1726 bitmap_clear_bit (m_state->get_blocks_on_stack (), bb->index);
1727 m_avail_exprs_stack->pop_to_marker ();
1728 m_const_and_copies->pop_to_marker ();
1731 /* Search for redundant computations in STMT. If any are found, then
1732 replace them with the variable holding the result of the computation.
1734 If safe, record this expression into AVAIL_EXPRS_STACK and
1735 CONST_AND_COPIES. */
1737 static void
1738 eliminate_redundant_computations (gimple_stmt_iterator* gsi,
1739 class const_and_copies *const_and_copies,
1740 class avail_exprs_stack *avail_exprs_stack)
1742 tree expr_type;
1743 tree cached_lhs;
1744 tree def;
1745 bool insert = true;
1746 bool assigns_var_p = false;
1748 gimple *stmt = gsi_stmt (*gsi);
1750 if (gimple_code (stmt) == GIMPLE_PHI)
1751 def = gimple_phi_result (stmt);
1752 else
1753 def = gimple_get_lhs (stmt);
1755 /* Certain expressions on the RHS can be optimized away, but cannot
1756 themselves be entered into the hash tables. */
1757 if (! def
1758 || TREE_CODE (def) != SSA_NAME
1759 || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (def)
1760 || gimple_vdef (stmt)
1761 /* Do not record equivalences for increments of ivs. This would create
1762 overlapping live ranges for a very questionable gain. */
1763 || simple_iv_increment_p (stmt))
1764 insert = false;
1766 /* Check if the expression has been computed before. */
1767 cached_lhs = avail_exprs_stack->lookup_avail_expr (stmt, insert, true);
1769 opt_stats.num_exprs_considered++;
1771 /* Get the type of the expression we are trying to optimize. */
1772 if (is_gimple_assign (stmt))
1774 expr_type = TREE_TYPE (gimple_assign_lhs (stmt));
1775 assigns_var_p = true;
1777 else if (gimple_code (stmt) == GIMPLE_COND)
1778 expr_type = boolean_type_node;
1779 else if (is_gimple_call (stmt))
1781 gcc_assert (gimple_call_lhs (stmt));
1782 expr_type = TREE_TYPE (gimple_call_lhs (stmt));
1783 assigns_var_p = true;
1785 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
1786 expr_type = TREE_TYPE (gimple_switch_index (swtch_stmt));
1787 else if (gimple_code (stmt) == GIMPLE_PHI)
1788 /* We can't propagate into a phi, so the logic below doesn't apply.
1789 Instead record an equivalence between the cached LHS and the
1790 PHI result of this statement, provided they are in the same block.
1791 This should be sufficient to kill the redundant phi. */
1793 if (def && cached_lhs)
1794 const_and_copies->record_const_or_copy (def, cached_lhs);
1795 return;
1797 else
1798 gcc_unreachable ();
1800 if (!cached_lhs)
1801 return;
1803 /* It is safe to ignore types here since we have already done
1804 type checking in the hashing and equality routines. In fact
1805 type checking here merely gets in the way of constant
1806 propagation. Also, make sure that it is safe to propagate
1807 CACHED_LHS into the expression in STMT. */
1808 if ((TREE_CODE (cached_lhs) != SSA_NAME
1809 && (assigns_var_p
1810 || useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs))))
1811 || may_propagate_copy_into_stmt (stmt, cached_lhs))
1813 gcc_checking_assert (TREE_CODE (cached_lhs) == SSA_NAME
1814 || is_gimple_min_invariant (cached_lhs));
1816 if (dump_file && (dump_flags & TDF_DETAILS))
1818 fprintf (dump_file, " Replaced redundant expr '");
1819 print_gimple_expr (dump_file, stmt, 0, dump_flags);
1820 fprintf (dump_file, "' with '");
1821 print_generic_expr (dump_file, cached_lhs, dump_flags);
1822 fprintf (dump_file, "'\n");
1825 opt_stats.num_re++;
1827 if (assigns_var_p
1828 && !useless_type_conversion_p (expr_type, TREE_TYPE (cached_lhs)))
1829 cached_lhs = fold_convert (expr_type, cached_lhs);
1831 propagate_tree_value_into_stmt (gsi, cached_lhs);
1833 /* Since it is always necessary to mark the result as modified,
1834 perhaps we should move this into propagate_tree_value_into_stmt
1835 itself. */
1836 gimple_set_modified (gsi_stmt (*gsi), true);
1840 /* STMT, a GIMPLE_ASSIGN, may create certain equivalences, in either
1841 the available expressions table or the const_and_copies table.
1842 Detect and record those equivalences into AVAIL_EXPRS_STACK.
1844 We handle only very simple copy equivalences here. The heavy
1845 lifing is done by eliminate_redundant_computations. */
1847 static void
1848 record_equivalences_from_stmt (gimple *stmt, int may_optimize_p,
1849 class avail_exprs_stack *avail_exprs_stack)
1851 tree lhs;
1852 enum tree_code lhs_code;
1854 gcc_assert (is_gimple_assign (stmt));
1856 lhs = gimple_assign_lhs (stmt);
1857 lhs_code = TREE_CODE (lhs);
1859 if (lhs_code == SSA_NAME
1860 && gimple_assign_single_p (stmt))
1862 tree rhs = gimple_assign_rhs1 (stmt);
1864 /* If the RHS of the assignment is a constant or another variable that
1865 may be propagated, register it in the CONST_AND_COPIES table. We
1866 do not need to record unwind data for this, since this is a true
1867 assignment and not an equivalence inferred from a comparison. All
1868 uses of this ssa name are dominated by this assignment, so unwinding
1869 just costs time and space. */
1870 if (may_optimize_p
1871 && (TREE_CODE (rhs) == SSA_NAME
1872 || is_gimple_min_invariant (rhs)))
1874 rhs = dom_valueize (rhs);
1876 if (dump_file && (dump_flags & TDF_DETAILS))
1878 fprintf (dump_file, "==== ASGN ");
1879 print_generic_expr (dump_file, lhs);
1880 fprintf (dump_file, " = ");
1881 print_generic_expr (dump_file, rhs);
1882 fprintf (dump_file, "\n");
1885 set_ssa_name_value (lhs, rhs);
1889 /* Make sure we can propagate &x + CST. */
1890 if (lhs_code == SSA_NAME
1891 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
1892 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR
1893 && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
1895 tree op0 = gimple_assign_rhs1 (stmt);
1896 tree op1 = gimple_assign_rhs2 (stmt);
1897 tree new_rhs
1898 = build1 (ADDR_EXPR, TREE_TYPE (op0),
1899 fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (op0)),
1900 unshare_expr (op0), fold_convert (ptr_type_node,
1901 op1)));
1902 if (dump_file && (dump_flags & TDF_DETAILS))
1904 fprintf (dump_file, "==== ASGN ");
1905 print_generic_expr (dump_file, lhs);
1906 fprintf (dump_file, " = ");
1907 print_generic_expr (dump_file, new_rhs);
1908 fprintf (dump_file, "\n");
1911 set_ssa_name_value (lhs, new_rhs);
1914 /* A memory store, even an aliased store, creates a useful
1915 equivalence. By exchanging the LHS and RHS, creating suitable
1916 vops and recording the result in the available expression table,
1917 we may be able to expose more redundant loads. */
1918 if (!gimple_has_volatile_ops (stmt)
1919 && gimple_references_memory_p (stmt)
1920 && gimple_assign_single_p (stmt)
1921 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
1922 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt)))
1923 && !is_gimple_reg (lhs))
1925 tree rhs = gimple_assign_rhs1 (stmt);
1926 gassign *new_stmt;
1928 /* Build a new statement with the RHS and LHS exchanged. */
1929 if (TREE_CODE (rhs) == SSA_NAME)
1931 /* NOTE tuples. The call to gimple_build_assign below replaced
1932 a call to build_gimple_modify_stmt, which did not set the
1933 SSA_NAME_DEF_STMT on the LHS of the assignment. Doing so
1934 may cause an SSA validation failure, as the LHS may be a
1935 default-initialized name and should have no definition. I'm
1936 a bit dubious of this, as the artificial statement that we
1937 generate here may in fact be ill-formed, but it is simply
1938 used as an internal device in this pass, and never becomes
1939 part of the CFG. */
1940 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
1941 new_stmt = gimple_build_assign (rhs, lhs);
1942 SSA_NAME_DEF_STMT (rhs) = defstmt;
1944 else
1945 new_stmt = gimple_build_assign (rhs, lhs);
1947 gimple_set_vuse (new_stmt, gimple_vdef (stmt));
1949 /* Finally enter the statement into the available expression
1950 table. */
1951 avail_exprs_stack->lookup_avail_expr (new_stmt, true, true);
1955 /* Replace *OP_P in STMT with any known equivalent value for *OP_P from
1956 CONST_AND_COPIES. */
1958 static void
1959 cprop_operand (gimple *stmt, use_operand_p op_p, range_query *query)
1961 tree val;
1962 tree op = USE_FROM_PTR (op_p);
1964 /* If the operand has a known constant value or it is known to be a
1965 copy of some other variable, use the value or copy stored in
1966 CONST_AND_COPIES. */
1967 val = SSA_NAME_VALUE (op);
1968 if (!val)
1970 Value_Range r (TREE_TYPE (op));
1971 tree single;
1972 if (query->range_of_expr (r, op, stmt) && r.singleton_p (&single))
1973 val = single;
1976 if (val && val != op)
1978 /* Do not replace hard register operands in asm statements. */
1979 if (gimple_code (stmt) == GIMPLE_ASM
1980 && !may_propagate_copy_into_asm (op))
1981 return;
1983 /* Certain operands are not allowed to be copy propagated due
1984 to their interaction with exception handling and some GCC
1985 extensions. */
1986 if (!may_propagate_copy (op, val))
1987 return;
1989 /* Do not propagate copies into BIVs.
1990 See PR23821 and PR62217 for how this can disturb IV and
1991 number of iteration analysis. */
1992 if (TREE_CODE (val) != INTEGER_CST)
1994 gimple *def = SSA_NAME_DEF_STMT (op);
1995 if (gimple_code (def) == GIMPLE_PHI
1996 && gimple_bb (def)->loop_father->header == gimple_bb (def))
1997 return;
2000 /* Dump details. */
2001 if (dump_file && (dump_flags & TDF_DETAILS))
2003 fprintf (dump_file, " Replaced '");
2004 print_generic_expr (dump_file, op, dump_flags);
2005 fprintf (dump_file, "' with %s '",
2006 (TREE_CODE (val) != SSA_NAME ? "constant" : "variable"));
2007 print_generic_expr (dump_file, val, dump_flags);
2008 fprintf (dump_file, "'\n");
2011 if (TREE_CODE (val) != SSA_NAME)
2012 opt_stats.num_const_prop++;
2013 else
2014 opt_stats.num_copy_prop++;
2016 propagate_value (op_p, val);
2018 /* And note that we modified this statement. This is now
2019 safe, even if we changed virtual operands since we will
2020 rescan the statement and rewrite its operands again. */
2021 gimple_set_modified (stmt, true);
2025 /* CONST_AND_COPIES is a table which maps an SSA_NAME to the current
2026 known value for that SSA_NAME (or NULL if no value is known).
2028 Propagate values from CONST_AND_COPIES into the uses, vuses and
2029 vdef_ops of STMT. */
2031 static void
2032 cprop_into_stmt (gimple *stmt, range_query *query)
2034 use_operand_p op_p;
2035 ssa_op_iter iter;
2036 tree last_copy_propagated_op = NULL;
2038 FOR_EACH_SSA_USE_OPERAND (op_p, stmt, iter, SSA_OP_USE)
2040 tree old_op = USE_FROM_PTR (op_p);
2042 /* If we have A = B and B = A in the copy propagation tables
2043 (due to an equality comparison), avoid substituting B for A
2044 then A for B in the trivially discovered cases. This allows
2045 optimization of statements were A and B appear as input
2046 operands. */
2047 if (old_op != last_copy_propagated_op)
2049 cprop_operand (stmt, op_p, query);
2051 tree new_op = USE_FROM_PTR (op_p);
2052 if (new_op != old_op && TREE_CODE (new_op) == SSA_NAME)
2053 last_copy_propagated_op = new_op;
2058 /* If STMT contains a relational test, try to convert it into an
2059 equality test if there is only a single value which can ever
2060 make the test true.
2062 For example, if the expression hash table contains:
2064 TRUE = (i <= 1)
2066 And we have a test within statement of i >= 1, then we can safely
2067 rewrite the test as i == 1 since there only a single value where
2068 the test is true.
2070 This is similar to code in VRP. */
2072 void
2073 dom_opt_dom_walker::test_for_singularity (gimple *stmt,
2074 avail_exprs_stack *avail_exprs_stack)
2076 /* We want to support gimple conditionals as well as assignments
2077 where the RHS contains a conditional. */
2078 if (is_gimple_assign (stmt) || gimple_code (stmt) == GIMPLE_COND)
2080 enum tree_code code = ERROR_MARK;
2081 tree lhs, rhs;
2083 /* Extract the condition of interest from both forms we support. */
2084 if (is_gimple_assign (stmt))
2086 code = gimple_assign_rhs_code (stmt);
2087 lhs = gimple_assign_rhs1 (stmt);
2088 rhs = gimple_assign_rhs2 (stmt);
2090 else if (gimple_code (stmt) == GIMPLE_COND)
2092 code = gimple_cond_code (as_a <gcond *> (stmt));
2093 lhs = gimple_cond_lhs (as_a <gcond *> (stmt));
2094 rhs = gimple_cond_rhs (as_a <gcond *> (stmt));
2097 /* We're looking for a relational test using LE/GE. Also note we can
2098 canonicalize LT/GT tests against constants into LE/GT tests. */
2099 if (code == LE_EXPR || code == GE_EXPR
2100 || ((code == LT_EXPR || code == GT_EXPR)
2101 && TREE_CODE (rhs) == INTEGER_CST))
2103 /* For LT_EXPR and GT_EXPR, canonicalize to LE_EXPR and GE_EXPR. */
2104 if (code == LT_EXPR)
2105 rhs = fold_build2 (MINUS_EXPR, TREE_TYPE (rhs),
2106 rhs, build_int_cst (TREE_TYPE (rhs), 1));
2108 if (code == GT_EXPR)
2109 rhs = fold_build2 (PLUS_EXPR, TREE_TYPE (rhs),
2110 rhs, build_int_cst (TREE_TYPE (rhs), 1));
2112 /* Determine the code we want to check for in the hash table. */
2113 enum tree_code test_code;
2114 if (code == GE_EXPR || code == GT_EXPR)
2115 test_code = LE_EXPR;
2116 else
2117 test_code = GE_EXPR;
2119 /* Update the dummy statement so we can query the hash tables. */
2120 gimple_cond_set_code (m_dummy_cond, test_code);
2121 gimple_cond_set_lhs (m_dummy_cond, lhs);
2122 gimple_cond_set_rhs (m_dummy_cond, rhs);
2123 tree cached_lhs
2124 = avail_exprs_stack->lookup_avail_expr (m_dummy_cond,
2125 false, false);
2127 /* If the lookup returned 1 (true), then the expression we
2128 queried was in the hash table. As a result there is only
2129 one value that makes the original conditional true. Update
2130 STMT accordingly. */
2131 if (cached_lhs && integer_onep (cached_lhs))
2133 if (is_gimple_assign (stmt))
2135 gimple_assign_set_rhs_code (stmt, EQ_EXPR);
2136 gimple_assign_set_rhs2 (stmt, rhs);
2137 gimple_set_modified (stmt, true);
2139 else
2141 gimple_set_modified (stmt, true);
2142 gimple_cond_set_code (as_a <gcond *> (stmt), EQ_EXPR);
2143 gimple_cond_set_rhs (as_a <gcond *> (stmt), rhs);
2144 gimple_set_modified (stmt, true);
2151 /* If STMT is a comparison of two uniform vectors reduce it to a comparison
2152 of scalar objects, otherwise leave STMT unchanged. */
2154 static void
2155 reduce_vector_comparison_to_scalar_comparison (gimple *stmt)
2157 if (gimple_code (stmt) == GIMPLE_COND)
2159 tree lhs = gimple_cond_lhs (stmt);
2160 tree rhs = gimple_cond_rhs (stmt);
2162 /* We may have a vector comparison where both arms are uniform
2163 vectors. If so, we can simplify the vector comparison down
2164 to a scalar comparison. */
2165 if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
2166 && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE)
2168 /* If either operand is an SSA_NAME, then look back to its
2169 defining statement to try and get at a suitable source. */
2170 if (TREE_CODE (rhs) == SSA_NAME)
2172 gimple *def_stmt = SSA_NAME_DEF_STMT (rhs);
2173 if (gimple_assign_single_p (def_stmt))
2174 rhs = gimple_assign_rhs1 (def_stmt);
2177 if (TREE_CODE (lhs) == SSA_NAME)
2179 gimple *def_stmt = SSA_NAME_DEF_STMT (lhs);
2180 if (gimple_assign_single_p (def_stmt))
2181 lhs = gimple_assign_rhs1 (def_stmt);
2184 /* Now see if they are both uniform vectors and if so replace
2185 the vector comparison with a scalar comparison. */
2186 tree rhs_elem = rhs ? uniform_vector_p (rhs) : NULL_TREE;
2187 tree lhs_elem = lhs ? uniform_vector_p (lhs) : NULL_TREE;
2188 if (rhs_elem && lhs_elem)
2190 if (dump_file && dump_flags & TDF_DETAILS)
2192 fprintf (dump_file, "Reducing vector comparison: ");
2193 print_gimple_stmt (dump_file, stmt, 0);
2196 gimple_cond_set_rhs (as_a <gcond *>(stmt), rhs_elem);
2197 gimple_cond_set_lhs (as_a <gcond *>(stmt), lhs_elem);
2198 gimple_set_modified (stmt, true);
2200 if (dump_file && dump_flags & TDF_DETAILS)
2202 fprintf (dump_file, "To scalar equivalent: ");
2203 print_gimple_stmt (dump_file, stmt, 0);
2204 fprintf (dump_file, "\n");
2211 /* If possible, rewrite the conditional as TRUE or FALSE, and return
2212 the taken edge. Otherwise, return NULL. */
2214 edge
2215 dom_opt_dom_walker::fold_cond (gcond *cond)
2217 simplify_using_ranges simplify (m_ranger);
2218 if (simplify.fold_cond (cond))
2220 basic_block bb = gimple_bb (cond);
2221 if (gimple_cond_true_p (cond))
2222 return find_taken_edge (bb, boolean_true_node);
2223 if (gimple_cond_false_p (cond))
2224 return find_taken_edge (bb, boolean_false_node);
2226 return NULL;
2229 /* Optimize the statement in block BB pointed to by iterator SI.
2231 We try to perform some simplistic global redundancy elimination and
2232 constant propagation:
2234 1- To detect global redundancy, we keep track of expressions that have
2235 been computed in this block and its dominators. If we find that the
2236 same expression is computed more than once, we eliminate repeated
2237 computations by using the target of the first one.
2239 2- Constant values and copy assignments. This is used to do very
2240 simplistic constant and copy propagation. When a constant or copy
2241 assignment is found, we map the value on the RHS of the assignment to
2242 the variable in the LHS in the CONST_AND_COPIES table.
2244 3- Very simple redundant store elimination is performed.
2246 4- We can simplify a condition to a constant or from a relational
2247 condition to an equality condition. */
2249 edge
2250 dom_opt_dom_walker::optimize_stmt (basic_block bb, gimple_stmt_iterator *si,
2251 bool *removed_p)
2253 gimple *stmt, *old_stmt;
2254 bool may_optimize_p;
2255 bool modified_p = false;
2256 bool was_noreturn;
2257 edge retval = NULL;
2259 old_stmt = stmt = gsi_stmt (*si);
2260 was_noreturn = is_gimple_call (stmt) && gimple_call_noreturn_p (stmt);
2262 if (dump_file && (dump_flags & TDF_DETAILS))
2264 fprintf (dump_file, "Optimizing statement ");
2265 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2268 /* STMT may be a comparison of uniform vectors that we can simplify
2269 down to a comparison of scalars. Do that transformation first
2270 so that all the scalar optimizations from here onward apply. */
2271 reduce_vector_comparison_to_scalar_comparison (stmt);
2273 update_stmt_if_modified (stmt);
2274 opt_stats.num_stmts++;
2276 /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */
2277 cprop_into_stmt (stmt, m_ranger);
2279 /* If the statement has been modified with constant replacements,
2280 fold its RHS before checking for redundant computations. */
2281 if (gimple_modified_p (stmt))
2283 tree rhs = NULL;
2285 /* Try to fold the statement making sure that STMT is kept
2286 up to date. */
2287 if (fold_stmt (si))
2289 stmt = gsi_stmt (*si);
2290 gimple_set_modified (stmt, true);
2292 if (dump_file && (dump_flags & TDF_DETAILS))
2294 fprintf (dump_file, " Folded to: ");
2295 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2299 /* We only need to consider cases that can yield a gimple operand. */
2300 if (gimple_assign_single_p (stmt))
2301 rhs = gimple_assign_rhs1 (stmt);
2302 else if (gimple_code (stmt) == GIMPLE_GOTO)
2303 rhs = gimple_goto_dest (stmt);
2304 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2305 /* This should never be an ADDR_EXPR. */
2306 rhs = gimple_switch_index (swtch_stmt);
2308 if (rhs && TREE_CODE (rhs) == ADDR_EXPR)
2309 recompute_tree_invariant_for_addr_expr (rhs);
2311 /* Indicate that maybe_clean_or_replace_eh_stmt needs to be called,
2312 even if fold_stmt updated the stmt already and thus cleared
2313 gimple_modified_p flag on it. */
2314 modified_p = true;
2317 /* Check for redundant computations. Do this optimization only
2318 for assignments that have no volatile ops and conditionals. */
2319 may_optimize_p = (!gimple_has_side_effects (stmt)
2320 && (is_gimple_assign (stmt)
2321 || (is_gimple_call (stmt)
2322 && gimple_call_lhs (stmt) != NULL_TREE)
2323 || gimple_code (stmt) == GIMPLE_COND
2324 || gimple_code (stmt) == GIMPLE_SWITCH));
2326 if (may_optimize_p)
2328 if (gimple_code (stmt) == GIMPLE_CALL)
2330 /* Resolve __builtin_constant_p. If it hasn't been
2331 folded to integer_one_node by now, it's fairly
2332 certain that the value simply isn't constant. */
2333 tree callee = gimple_call_fndecl (stmt);
2334 if (callee
2335 && fndecl_built_in_p (callee, BUILT_IN_CONSTANT_P))
2337 propagate_tree_value_into_stmt (si, integer_zero_node);
2338 stmt = gsi_stmt (*si);
2342 if (gimple_code (stmt) == GIMPLE_COND)
2344 tree lhs = gimple_cond_lhs (stmt);
2345 tree rhs = gimple_cond_rhs (stmt);
2347 /* If the LHS has a range [0..1] and the RHS has a range ~[0..1],
2348 then this conditional is computable at compile time. We can just
2349 shove either 0 or 1 into the LHS, mark the statement as modified
2350 and all the right things will just happen below.
2352 Note this would apply to any case where LHS has a range
2353 narrower than its type implies and RHS is outside that
2354 narrower range. Future work. */
2355 if (TREE_CODE (lhs) == SSA_NAME
2356 && ssa_name_has_boolean_range (lhs)
2357 && TREE_CODE (rhs) == INTEGER_CST
2358 && ! (integer_zerop (rhs) || integer_onep (rhs)))
2360 gimple_cond_set_lhs (as_a <gcond *> (stmt),
2361 fold_convert (TREE_TYPE (lhs),
2362 integer_zero_node));
2363 gimple_set_modified (stmt, true);
2365 else if (TREE_CODE (lhs) == SSA_NAME)
2367 /* Exploiting EVRP data is not yet fully integrated into DOM
2368 but we need to do something for this case to avoid regressing
2369 udr4.f90 and new1.C which have unexecutable blocks with
2370 undefined behavior that get diagnosed if they're left in the
2371 IL because we've attached range information to new
2372 SSA_NAMES. */
2373 update_stmt_if_modified (stmt);
2374 edge taken_edge = fold_cond (as_a <gcond *> (stmt));
2375 if (taken_edge)
2377 gimple_set_modified (stmt, true);
2378 update_stmt (stmt);
2379 cfg_altered = true;
2380 return taken_edge;
2385 update_stmt_if_modified (stmt);
2386 eliminate_redundant_computations (si, m_const_and_copies,
2387 m_avail_exprs_stack);
2388 stmt = gsi_stmt (*si);
2390 /* Perform simple redundant store elimination. */
2391 if (gimple_assign_single_p (stmt)
2392 && TREE_CODE (gimple_assign_lhs (stmt)) != SSA_NAME)
2394 tree lhs = gimple_assign_lhs (stmt);
2395 tree rhs = gimple_assign_rhs1 (stmt);
2396 tree cached_lhs;
2397 gassign *new_stmt;
2398 rhs = dom_valueize (rhs);
2399 /* Build a new statement with the RHS and LHS exchanged. */
2400 if (TREE_CODE (rhs) == SSA_NAME)
2402 gimple *defstmt = SSA_NAME_DEF_STMT (rhs);
2403 new_stmt = gimple_build_assign (rhs, lhs);
2404 SSA_NAME_DEF_STMT (rhs) = defstmt;
2406 else
2407 new_stmt = gimple_build_assign (rhs, lhs);
2408 gimple_set_vuse (new_stmt, gimple_vuse (stmt));
2409 expr_hash_elt *elt = NULL;
2410 cached_lhs = m_avail_exprs_stack->lookup_avail_expr (new_stmt, false,
2411 false, &elt);
2412 if (cached_lhs
2413 && operand_equal_p (rhs, cached_lhs, 0)
2414 && refs_same_for_tbaa_p (elt->expr ()->kind == EXPR_SINGLE
2415 ? elt->expr ()->ops.single.rhs
2416 : NULL_TREE, lhs))
2418 basic_block bb = gimple_bb (stmt);
2419 unlink_stmt_vdef (stmt);
2420 if (gsi_remove (si, true))
2422 bitmap_set_bit (need_eh_cleanup, bb->index);
2423 if (dump_file && (dump_flags & TDF_DETAILS))
2424 fprintf (dump_file, " Flagged to clear EH edges.\n");
2426 release_defs (stmt);
2427 *removed_p = true;
2428 return retval;
2432 /* If this statement was not redundant, we may still be able to simplify
2433 it, which may in turn allow other part of DOM or other passes to do
2434 a better job. */
2435 test_for_singularity (stmt, m_avail_exprs_stack);
2438 /* Record any additional equivalences created by this statement. */
2439 if (is_gimple_assign (stmt))
2440 record_equivalences_from_stmt (stmt, may_optimize_p, m_avail_exprs_stack);
2442 /* If STMT is a COND_EXPR or SWITCH_EXPR and it was modified, then we may
2443 know where it goes. */
2444 if (gimple_modified_p (stmt) || modified_p)
2446 tree val = NULL;
2448 if (gimple_code (stmt) == GIMPLE_COND)
2449 val = fold_binary_loc (gimple_location (stmt),
2450 gimple_cond_code (stmt), boolean_type_node,
2451 gimple_cond_lhs (stmt),
2452 gimple_cond_rhs (stmt));
2453 else if (gswitch *swtch_stmt = dyn_cast <gswitch *> (stmt))
2454 val = gimple_switch_index (swtch_stmt);
2456 if (val && TREE_CODE (val) == INTEGER_CST)
2458 retval = find_taken_edge (bb, val);
2459 if (retval)
2461 /* Fix the condition to be either true or false. */
2462 if (gimple_code (stmt) == GIMPLE_COND)
2464 if (integer_zerop (val))
2465 gimple_cond_make_false (as_a <gcond *> (stmt));
2466 else if (integer_onep (val))
2467 gimple_cond_make_true (as_a <gcond *> (stmt));
2468 else
2469 gcc_unreachable ();
2471 gimple_set_modified (stmt, true);
2474 /* Further simplifications may be possible. */
2475 cfg_altered = true;
2479 update_stmt_if_modified (stmt);
2481 /* If we simplified a statement in such a way as to be shown that it
2482 cannot trap, update the eh information and the cfg to match. */
2483 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
2485 bitmap_set_bit (need_eh_cleanup, bb->index);
2486 if (dump_file && (dump_flags & TDF_DETAILS))
2487 fprintf (dump_file, " Flagged to clear EH edges.\n");
2490 if (!was_noreturn
2491 && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
2492 need_noreturn_fixup.safe_push (stmt);
2494 return retval;