1 /* Optimization of PHI nodes by converting them into straightline code.
2 Copyright (C) 2004-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 3, or (at your option) any
11 GCC is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
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/>. */
22 #include "coretypes.h"
23 #include "hash-table.h"
32 #include "fold-const.h"
33 #include "stor-layout.h"
37 #include "hard-reg-set.h"
39 #include "dominance.h"
42 #include "basic-block.h"
43 #include "tree-ssa-alias.h"
44 #include "internal-fn.h"
45 #include "gimple-expr.h"
49 #include "gimple-iterator.h"
50 #include "gimplify-me.h"
51 #include "gimple-ssa.h"
53 #include "tree-phinodes.h"
54 #include "ssa-iterators.h"
55 #include "stringpool.h"
56 #include "tree-ssanames.h"
59 #include "statistics.h"
60 #include "insn-config.h"
70 #include "tree-pass.h"
71 #include "langhooks.h"
74 #include "tree-data-ref.h"
75 #include "gimple-pretty-print.h"
76 #include "insn-codes.h"
78 #include "tree-scalar-evolution.h"
79 #include "tree-inline.h"
81 static unsigned int tree_ssa_phiopt_worker (bool, bool);
82 static bool conditional_replacement (basic_block
, basic_block
,
83 edge
, edge
, gphi
*, tree
, tree
);
84 static int value_replacement (basic_block
, basic_block
,
85 edge
, edge
, gimple
, tree
, tree
);
86 static bool minmax_replacement (basic_block
, basic_block
,
87 edge
, edge
, gimple
, tree
, tree
);
88 static bool abs_replacement (basic_block
, basic_block
,
89 edge
, edge
, gimple
, tree
, tree
);
90 static bool cond_store_replacement (basic_block
, basic_block
, edge
, edge
,
92 static bool cond_if_else_store_replacement (basic_block
, basic_block
, basic_block
);
93 static hash_set
<tree
> * get_non_trapping ();
94 static void replace_phi_edge_with_variable (basic_block
, edge
, gimple
, tree
);
95 static void hoist_adjacent_loads (basic_block
, basic_block
,
96 basic_block
, basic_block
);
97 static bool gate_hoist_loads (void);
99 /* This pass tries to transform conditional stores into unconditional
100 ones, enabling further simplifications with the simpler then and else
101 blocks. In particular it replaces this:
104 if (cond) goto bb2; else goto bb1;
112 if (cond) goto bb1; else goto bb2;
116 condtmp = PHI <RHS, condtmp'>
119 This transformation can only be done under several constraints,
120 documented below. It also replaces:
123 if (cond) goto bb2; else goto bb1;
134 if (cond) goto bb3; else goto bb1;
137 condtmp = PHI <RHS1, RHS2>
141 tree_ssa_cs_elim (void)
144 /* ??? We are not interested in loop related info, but the following
145 will create it, ICEing as we didn't init loops with pre-headers.
146 An interfacing issue of find_data_references_in_bb. */
147 loop_optimizer_init (LOOPS_NORMAL
);
149 todo
= tree_ssa_phiopt_worker (true, false);
151 loop_optimizer_finalize ();
155 /* Return the singleton PHI in the SEQ of PHIs for edges E0 and E1. */
158 single_non_singleton_phi_for_edges (gimple_seq seq
, edge e0
, edge e1
)
160 gimple_stmt_iterator i
;
162 if (gimple_seq_singleton_p (seq
))
163 return as_a
<gphi
*> (gsi_stmt (gsi_start (seq
)));
164 for (i
= gsi_start (seq
); !gsi_end_p (i
); gsi_next (&i
))
166 gphi
*p
= as_a
<gphi
*> (gsi_stmt (i
));
167 /* If the PHI arguments are equal then we can skip this PHI. */
168 if (operand_equal_for_phi_arg_p (gimple_phi_arg_def (p
, e0
->dest_idx
),
169 gimple_phi_arg_def (p
, e1
->dest_idx
)))
172 /* If we already have a PHI that has the two edge arguments are
173 different, then return it is not a singleton for these PHIs. */
182 /* The core routine of conditional store replacement and normal
183 phi optimizations. Both share much of the infrastructure in how
184 to match applicable basic block patterns. DO_STORE_ELIM is true
185 when we want to do conditional store replacement, false otherwise.
186 DO_HOIST_LOADS is true when we want to hoist adjacent loads out
187 of diamond control flow patterns, false otherwise. */
189 tree_ssa_phiopt_worker (bool do_store_elim
, bool do_hoist_loads
)
192 basic_block
*bb_order
;
194 bool cfgchanged
= false;
195 hash_set
<tree
> *nontrap
= 0;
198 /* Calculate the set of non-trapping memory accesses. */
199 nontrap
= get_non_trapping ();
201 /* Search every basic block for COND_EXPR we may be able to optimize.
203 We walk the blocks in order that guarantees that a block with
204 a single predecessor is processed before the predecessor.
205 This ensures that we collapse inner ifs before visiting the
206 outer ones, and also that we do not try to visit a removed
208 bb_order
= single_pred_before_succ_order ();
209 n
= n_basic_blocks_for_fn (cfun
) - NUM_FIXED_BLOCKS
;
211 for (i
= 0; i
< n
; i
++)
215 basic_block bb1
, bb2
;
221 cond_stmt
= last_stmt (bb
);
222 /* Check to see if the last statement is a GIMPLE_COND. */
224 || gimple_code (cond_stmt
) != GIMPLE_COND
)
227 e1
= EDGE_SUCC (bb
, 0);
229 e2
= EDGE_SUCC (bb
, 1);
232 /* We cannot do the optimization on abnormal edges. */
233 if ((e1
->flags
& EDGE_ABNORMAL
) != 0
234 || (e2
->flags
& EDGE_ABNORMAL
) != 0)
237 /* If either bb1's succ or bb2 or bb2's succ is non NULL. */
238 if (EDGE_COUNT (bb1
->succs
) == 0
240 || EDGE_COUNT (bb2
->succs
) == 0)
243 /* Find the bb which is the fall through to the other. */
244 if (EDGE_SUCC (bb1
, 0)->dest
== bb2
)
246 else if (EDGE_SUCC (bb2
, 0)->dest
== bb1
)
248 basic_block bb_tmp
= bb1
;
255 else if (do_store_elim
256 && EDGE_SUCC (bb1
, 0)->dest
== EDGE_SUCC (bb2
, 0)->dest
)
258 basic_block bb3
= EDGE_SUCC (bb1
, 0)->dest
;
260 if (!single_succ_p (bb1
)
261 || (EDGE_SUCC (bb1
, 0)->flags
& EDGE_FALLTHRU
) == 0
262 || !single_succ_p (bb2
)
263 || (EDGE_SUCC (bb2
, 0)->flags
& EDGE_FALLTHRU
) == 0
264 || EDGE_COUNT (bb3
->preds
) != 2)
266 if (cond_if_else_store_replacement (bb1
, bb2
, bb3
))
270 else if (do_hoist_loads
271 && EDGE_SUCC (bb1
, 0)->dest
== EDGE_SUCC (bb2
, 0)->dest
)
273 basic_block bb3
= EDGE_SUCC (bb1
, 0)->dest
;
275 if (!FLOAT_TYPE_P (TREE_TYPE (gimple_cond_lhs (cond_stmt
)))
276 && single_succ_p (bb1
)
277 && single_succ_p (bb2
)
278 && single_pred_p (bb1
)
279 && single_pred_p (bb2
)
280 && EDGE_COUNT (bb
->succs
) == 2
281 && EDGE_COUNT (bb3
->preds
) == 2
282 /* If one edge or the other is dominant, a conditional move
283 is likely to perform worse than the well-predicted branch. */
284 && !predictable_edge_p (EDGE_SUCC (bb
, 0))
285 && !predictable_edge_p (EDGE_SUCC (bb
, 1)))
286 hoist_adjacent_loads (bb
, bb1
, bb2
, bb3
);
292 e1
= EDGE_SUCC (bb1
, 0);
294 /* Make sure that bb1 is just a fall through. */
295 if (!single_succ_p (bb1
)
296 || (e1
->flags
& EDGE_FALLTHRU
) == 0)
299 /* Also make sure that bb1 only have one predecessor and that it
301 if (!single_pred_p (bb1
)
302 || single_pred (bb1
) != bb
)
307 /* bb1 is the middle block, bb2 the join block, bb the split block,
308 e1 the fallthrough edge from bb1 to bb2. We can't do the
309 optimization if the join block has more than two predecessors. */
310 if (EDGE_COUNT (bb2
->preds
) > 2)
312 if (cond_store_replacement (bb1
, bb2
, e1
, e2
, nontrap
))
317 gimple_seq phis
= phi_nodes (bb2
);
318 gimple_stmt_iterator gsi
;
319 bool candorest
= true;
321 /* Value replacement can work with more than one PHI
322 so try that first. */
323 for (gsi
= gsi_start (phis
); !gsi_end_p (gsi
); gsi_next (&gsi
))
325 phi
= as_a
<gphi
*> (gsi_stmt (gsi
));
326 arg0
= gimple_phi_arg_def (phi
, e1
->dest_idx
);
327 arg1
= gimple_phi_arg_def (phi
, e2
->dest_idx
);
328 if (value_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
) == 2)
339 phi
= single_non_singleton_phi_for_edges (phis
, e1
, e2
);
343 arg0
= gimple_phi_arg_def (phi
, e1
->dest_idx
);
344 arg1
= gimple_phi_arg_def (phi
, e2
->dest_idx
);
346 /* Something is wrong if we cannot find the arguments in the PHI
348 gcc_assert (arg0
!= NULL
&& arg1
!= NULL
);
350 /* Do the replacement of conditional if it can be done. */
351 if (conditional_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
353 else if (abs_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
355 else if (minmax_replacement (bb
, bb1
, e1
, e2
, phi
, arg0
, arg1
))
364 /* If the CFG has changed, we should cleanup the CFG. */
365 if (cfgchanged
&& do_store_elim
)
367 /* In cond-store replacement we have added some loads on edges
368 and new VOPS (as we moved the store, and created a load). */
369 gsi_commit_edge_inserts ();
370 return TODO_cleanup_cfg
| TODO_update_ssa_only_virtuals
;
373 return TODO_cleanup_cfg
;
377 /* Replace PHI node element whose edge is E in block BB with variable NEW.
378 Remove the edge from COND_BLOCK which does not lead to BB (COND_BLOCK
379 is known to have two edges, one of which must reach BB). */
382 replace_phi_edge_with_variable (basic_block cond_block
,
383 edge e
, gimple phi
, tree new_tree
)
385 basic_block bb
= gimple_bb (phi
);
386 basic_block block_to_remove
;
387 gimple_stmt_iterator gsi
;
389 /* Change the PHI argument to new. */
390 SET_USE (PHI_ARG_DEF_PTR (phi
, e
->dest_idx
), new_tree
);
392 /* Remove the empty basic block. */
393 if (EDGE_SUCC (cond_block
, 0)->dest
== bb
)
395 EDGE_SUCC (cond_block
, 0)->flags
|= EDGE_FALLTHRU
;
396 EDGE_SUCC (cond_block
, 0)->flags
&= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
397 EDGE_SUCC (cond_block
, 0)->probability
= REG_BR_PROB_BASE
;
398 EDGE_SUCC (cond_block
, 0)->count
+= EDGE_SUCC (cond_block
, 1)->count
;
400 block_to_remove
= EDGE_SUCC (cond_block
, 1)->dest
;
404 EDGE_SUCC (cond_block
, 1)->flags
|= EDGE_FALLTHRU
;
405 EDGE_SUCC (cond_block
, 1)->flags
406 &= ~(EDGE_TRUE_VALUE
| EDGE_FALSE_VALUE
);
407 EDGE_SUCC (cond_block
, 1)->probability
= REG_BR_PROB_BASE
;
408 EDGE_SUCC (cond_block
, 1)->count
+= EDGE_SUCC (cond_block
, 0)->count
;
410 block_to_remove
= EDGE_SUCC (cond_block
, 0)->dest
;
412 delete_basic_block (block_to_remove
);
414 /* Eliminate the COND_EXPR at the end of COND_BLOCK. */
415 gsi
= gsi_last_bb (cond_block
);
416 gsi_remove (&gsi
, true);
418 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
420 "COND_EXPR in block %d and PHI in block %d converted to straightline code.\n",
425 /* The function conditional_replacement does the main work of doing the
426 conditional replacement. Return true if the replacement is done.
427 Otherwise return false.
428 BB is the basic block where the replacement is going to be done on. ARG0
429 is argument 0 from PHI. Likewise for ARG1. */
432 conditional_replacement (basic_block cond_bb
, basic_block middle_bb
,
433 edge e0
, edge e1
, gphi
*phi
,
434 tree arg0
, tree arg1
)
440 gimple_stmt_iterator gsi
;
441 edge true_edge
, false_edge
;
442 tree new_var
, new_var2
;
445 /* FIXME: Gimplification of complex type is too hard for now. */
446 /* We aren't prepared to handle vectors either (and it is a question
447 if it would be worthwhile anyway). */
448 if (!(INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
449 || POINTER_TYPE_P (TREE_TYPE (arg0
)))
450 || !(INTEGRAL_TYPE_P (TREE_TYPE (arg1
))
451 || POINTER_TYPE_P (TREE_TYPE (arg1
))))
454 /* The PHI arguments have the constants 0 and 1, or 0 and -1, then
455 convert it to the conditional. */
456 if ((integer_zerop (arg0
) && integer_onep (arg1
))
457 || (integer_zerop (arg1
) && integer_onep (arg0
)))
459 else if ((integer_zerop (arg0
) && integer_all_onesp (arg1
))
460 || (integer_zerop (arg1
) && integer_all_onesp (arg0
)))
465 if (!empty_block_p (middle_bb
))
468 /* At this point we know we have a GIMPLE_COND with two successors.
469 One successor is BB, the other successor is an empty block which
470 falls through into BB.
472 There is a single PHI node at the join point (BB) and its arguments
473 are constants (0, 1) or (0, -1).
475 So, given the condition COND, and the two PHI arguments, we can
476 rewrite this PHI into non-branching code:
478 dest = (COND) or dest = COND'
480 We use the condition as-is if the argument associated with the
481 true edge has the value one or the argument associated with the
482 false edge as the value zero. Note that those conditions are not
483 the same since only one of the outgoing edges from the GIMPLE_COND
484 will directly reach BB and thus be associated with an argument. */
486 stmt
= last_stmt (cond_bb
);
487 result
= PHI_RESULT (phi
);
489 /* To handle special cases like floating point comparison, it is easier and
490 less error-prone to build a tree and gimplify it on the fly though it is
492 cond
= fold_build2_loc (gimple_location (stmt
),
493 gimple_cond_code (stmt
), boolean_type_node
,
494 gimple_cond_lhs (stmt
), gimple_cond_rhs (stmt
));
496 /* We need to know which is the true edge and which is the false
497 edge so that we know when to invert the condition below. */
498 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
499 if ((e0
== true_edge
&& integer_zerop (arg0
))
500 || (e0
== false_edge
&& !integer_zerop (arg0
))
501 || (e1
== true_edge
&& integer_zerop (arg1
))
502 || (e1
== false_edge
&& !integer_zerop (arg1
)))
503 cond
= fold_build1_loc (gimple_location (stmt
),
504 TRUTH_NOT_EXPR
, TREE_TYPE (cond
), cond
);
508 cond
= fold_convert_loc (gimple_location (stmt
),
509 TREE_TYPE (result
), cond
);
510 cond
= fold_build1_loc (gimple_location (stmt
),
511 NEGATE_EXPR
, TREE_TYPE (cond
), cond
);
514 /* Insert our new statements at the end of conditional block before the
516 gsi
= gsi_for_stmt (stmt
);
517 new_var
= force_gimple_operand_gsi (&gsi
, cond
, true, NULL
, true,
520 if (!useless_type_conversion_p (TREE_TYPE (result
), TREE_TYPE (new_var
)))
522 source_location locus_0
, locus_1
;
524 new_var2
= make_ssa_name (TREE_TYPE (result
));
525 new_stmt
= gimple_build_assign (new_var2
, CONVERT_EXPR
, new_var
);
526 gsi_insert_before (&gsi
, new_stmt
, GSI_SAME_STMT
);
529 /* Set the locus to the first argument, unless is doesn't have one. */
530 locus_0
= gimple_phi_arg_location (phi
, 0);
531 locus_1
= gimple_phi_arg_location (phi
, 1);
532 if (locus_0
== UNKNOWN_LOCATION
)
534 gimple_set_location (new_stmt
, locus_0
);
537 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, new_var
);
539 /* Note that we optimized this PHI. */
543 /* Update *ARG which is defined in STMT so that it contains the
544 computed value if that seems profitable. Return true if the
545 statement is made dead by that rewriting. */
548 jump_function_from_stmt (tree
*arg
, gimple stmt
)
550 enum tree_code code
= gimple_assign_rhs_code (stmt
);
551 if (code
== ADDR_EXPR
)
553 /* For arg = &p->i transform it to p, if possible. */
554 tree rhs1
= gimple_assign_rhs1 (stmt
);
555 HOST_WIDE_INT offset
;
556 tree tem
= get_addr_base_and_unit_offset (TREE_OPERAND (rhs1
, 0),
559 && TREE_CODE (tem
) == MEM_REF
560 && (mem_ref_offset (tem
) + offset
) == 0)
562 *arg
= TREE_OPERAND (tem
, 0);
566 /* TODO: Much like IPA-CP jump-functions we want to handle constant
567 additions symbolically here, and we'd need to update the comparison
568 code that compares the arg + cst tuples in our caller. For now the
569 code above exactly handles the VEC_BASE pattern from vec.h. */
573 /* RHS is a source argument in a BIT_AND_EXPR which feeds a conditional
574 of the form SSA_NAME NE 0.
576 If RHS is fed by a simple EQ_EXPR comparison of two values, see if
577 the two input values of the EQ_EXPR match arg0 and arg1.
579 If so update *code and return TRUE. Otherwise return FALSE. */
582 rhs_is_fed_for_value_replacement (const_tree arg0
, const_tree arg1
,
583 enum tree_code
*code
, const_tree rhs
)
585 /* Obviously if RHS is not an SSA_NAME, we can't look at the defining
587 if (TREE_CODE (rhs
) == SSA_NAME
)
589 gimple def1
= SSA_NAME_DEF_STMT (rhs
);
591 /* Verify the defining statement has an EQ_EXPR on the RHS. */
592 if (is_gimple_assign (def1
) && gimple_assign_rhs_code (def1
) == EQ_EXPR
)
594 /* Finally verify the source operands of the EQ_EXPR are equal
596 tree op0
= gimple_assign_rhs1 (def1
);
597 tree op1
= gimple_assign_rhs2 (def1
);
598 if ((operand_equal_for_phi_arg_p (arg0
, op0
)
599 && operand_equal_for_phi_arg_p (arg1
, op1
))
600 || (operand_equal_for_phi_arg_p (arg0
, op1
)
601 && operand_equal_for_phi_arg_p (arg1
, op0
)))
603 /* We will perform the optimization. */
604 *code
= gimple_assign_rhs_code (def1
);
612 /* Return TRUE if arg0/arg1 are equal to the rhs/lhs or lhs/rhs of COND.
614 Also return TRUE if arg0/arg1 are equal to the source arguments of a
615 an EQ comparison feeding a BIT_AND_EXPR which feeds COND.
617 Return FALSE otherwise. */
620 operand_equal_for_value_replacement (const_tree arg0
, const_tree arg1
,
621 enum tree_code
*code
, gimple cond
)
624 tree lhs
= gimple_cond_lhs (cond
);
625 tree rhs
= gimple_cond_rhs (cond
);
627 if ((operand_equal_for_phi_arg_p (arg0
, lhs
)
628 && operand_equal_for_phi_arg_p (arg1
, rhs
))
629 || (operand_equal_for_phi_arg_p (arg1
, lhs
)
630 && operand_equal_for_phi_arg_p (arg0
, rhs
)))
633 /* Now handle more complex case where we have an EQ comparison
634 which feeds a BIT_AND_EXPR which feeds COND.
636 First verify that COND is of the form SSA_NAME NE 0. */
637 if (*code
!= NE_EXPR
|| !integer_zerop (rhs
)
638 || TREE_CODE (lhs
) != SSA_NAME
)
641 /* Now ensure that SSA_NAME is set by a BIT_AND_EXPR. */
642 def
= SSA_NAME_DEF_STMT (lhs
);
643 if (!is_gimple_assign (def
) || gimple_assign_rhs_code (def
) != BIT_AND_EXPR
)
646 /* Now verify arg0/arg1 correspond to the source arguments of an
647 EQ comparison feeding the BIT_AND_EXPR. */
649 tree tmp
= gimple_assign_rhs1 (def
);
650 if (rhs_is_fed_for_value_replacement (arg0
, arg1
, code
, tmp
))
653 tmp
= gimple_assign_rhs2 (def
);
654 if (rhs_is_fed_for_value_replacement (arg0
, arg1
, code
, tmp
))
660 /* Returns true if ARG is a neutral element for operation CODE
661 on the RIGHT side. */
664 neutral_element_p (tree_code code
, tree arg
, bool right
)
671 return integer_zerop (arg
);
678 case POINTER_PLUS_EXPR
:
679 return right
&& integer_zerop (arg
);
682 return integer_onep (arg
);
689 return right
&& integer_onep (arg
);
692 return integer_all_onesp (arg
);
699 /* Returns true if ARG is an absorbing element for operation CODE. */
702 absorbing_element_p (tree_code code
, tree arg
)
707 return integer_all_onesp (arg
);
711 return integer_zerop (arg
);
718 /* The function value_replacement does the main work of doing the value
719 replacement. Return non-zero if the replacement is done. Otherwise return
720 0. If we remove the middle basic block, return 2.
721 BB is the basic block where the replacement is going to be done on. ARG0
722 is argument 0 from the PHI. Likewise for ARG1. */
725 value_replacement (basic_block cond_bb
, basic_block middle_bb
,
726 edge e0
, edge e1
, gimple phi
,
727 tree arg0
, tree arg1
)
729 gimple_stmt_iterator gsi
;
731 edge true_edge
, false_edge
;
733 bool emtpy_or_with_defined_p
= true;
735 /* If the type says honor signed zeros we cannot do this
737 if (HONOR_SIGNED_ZEROS (arg1
))
740 /* If there is a statement in MIDDLE_BB that defines one of the PHI
741 arguments, then adjust arg0 or arg1. */
742 gsi
= gsi_start_nondebug_after_labels_bb (middle_bb
);
743 while (!gsi_end_p (gsi
))
745 gimple stmt
= gsi_stmt (gsi
);
747 gsi_next_nondebug (&gsi
);
748 if (!is_gimple_assign (stmt
))
750 emtpy_or_with_defined_p
= false;
753 /* Now try to adjust arg0 or arg1 according to the computation
755 lhs
= gimple_assign_lhs (stmt
);
757 && jump_function_from_stmt (&arg0
, stmt
))
759 && jump_function_from_stmt (&arg1
, stmt
)))
760 emtpy_or_with_defined_p
= false;
763 cond
= last_stmt (cond_bb
);
764 code
= gimple_cond_code (cond
);
766 /* This transformation is only valid for equality comparisons. */
767 if (code
!= NE_EXPR
&& code
!= EQ_EXPR
)
770 /* We need to know which is the true edge and which is the false
771 edge so that we know if have abs or negative abs. */
772 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
774 /* At this point we know we have a COND_EXPR with two successors.
775 One successor is BB, the other successor is an empty block which
776 falls through into BB.
778 The condition for the COND_EXPR is known to be NE_EXPR or EQ_EXPR.
780 There is a single PHI node at the join point (BB) with two arguments.
782 We now need to verify that the two arguments in the PHI node match
783 the two arguments to the equality comparison. */
785 if (operand_equal_for_value_replacement (arg0
, arg1
, &code
, cond
))
790 /* For NE_EXPR, we want to build an assignment result = arg where
791 arg is the PHI argument associated with the true edge. For
792 EQ_EXPR we want the PHI argument associated with the false edge. */
793 e
= (code
== NE_EXPR
? true_edge
: false_edge
);
795 /* Unfortunately, E may not reach BB (it may instead have gone to
796 OTHER_BLOCK). If that is the case, then we want the single outgoing
797 edge from OTHER_BLOCK which reaches BB and represents the desired
798 path from COND_BLOCK. */
799 if (e
->dest
== middle_bb
)
800 e
= single_succ_edge (e
->dest
);
802 /* Now we know the incoming edge to BB that has the argument for the
803 RHS of our new assignment statement. */
809 /* If the middle basic block was empty or is defining the
810 PHI arguments and this is a single phi where the args are different
811 for the edges e0 and e1 then we can remove the middle basic block. */
812 if (emtpy_or_with_defined_p
813 && single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi
)),
816 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, arg
);
817 /* Note that we optimized this PHI. */
822 /* Replace the PHI arguments with arg. */
823 SET_PHI_ARG_DEF (phi
, e0
->dest_idx
, arg
);
824 SET_PHI_ARG_DEF (phi
, e1
->dest_idx
, arg
);
825 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
827 fprintf (dump_file
, "PHI ");
828 print_generic_expr (dump_file
, gimple_phi_result (phi
), 0);
829 fprintf (dump_file
, " reduced for COND_EXPR in block %d to ",
831 print_generic_expr (dump_file
, arg
, 0);
832 fprintf (dump_file
, ".\n");
839 /* Now optimize (x != 0) ? x + y : y to just y.
840 The following condition is too restrictive, there can easily be another
841 stmt in middle_bb, for instance a CONVERT_EXPR for the second argument. */
842 gimple assign
= last_and_only_stmt (middle_bb
);
843 if (!assign
|| gimple_code (assign
) != GIMPLE_ASSIGN
844 || gimple_assign_rhs_class (assign
) != GIMPLE_BINARY_RHS
845 || (!INTEGRAL_TYPE_P (TREE_TYPE (arg0
))
846 && !POINTER_TYPE_P (TREE_TYPE (arg0
))))
849 /* Punt if there are (degenerate) PHIs in middle_bb, there should not be. */
850 if (!gimple_seq_empty_p (phi_nodes (middle_bb
)))
853 /* Only transform if it removes the condition. */
854 if (!single_non_singleton_phi_for_edges (phi_nodes (gimple_bb (phi
)), e0
, e1
))
857 /* Size-wise, this is always profitable. */
858 if (optimize_bb_for_speed_p (cond_bb
)
859 /* The special case is useless if it has a low probability. */
860 && profile_status_for_fn (cfun
) != PROFILE_ABSENT
861 && EDGE_PRED (middle_bb
, 0)->probability
< PROB_EVEN
862 /* If assign is cheap, there is no point avoiding it. */
863 && estimate_num_insns (assign
, &eni_time_weights
)
864 >= 3 * estimate_num_insns (cond
, &eni_time_weights
))
867 tree lhs
= gimple_assign_lhs (assign
);
868 tree rhs1
= gimple_assign_rhs1 (assign
);
869 tree rhs2
= gimple_assign_rhs2 (assign
);
870 enum tree_code code_def
= gimple_assign_rhs_code (assign
);
871 tree cond_lhs
= gimple_cond_lhs (cond
);
872 tree cond_rhs
= gimple_cond_rhs (cond
);
874 if (((code
== NE_EXPR
&& e1
== false_edge
)
875 || (code
== EQ_EXPR
&& e1
== true_edge
))
878 && operand_equal_for_phi_arg_p (rhs2
, cond_lhs
)
879 && neutral_element_p (code_def
, cond_rhs
, true))
881 && operand_equal_for_phi_arg_p (rhs1
, cond_lhs
)
882 && neutral_element_p (code_def
, cond_rhs
, false))
883 || (operand_equal_for_phi_arg_p (arg1
, cond_rhs
)
884 && (operand_equal_for_phi_arg_p (rhs2
, cond_lhs
)
885 || operand_equal_for_phi_arg_p (rhs1
, cond_lhs
))
886 && absorbing_element_p (code_def
, cond_rhs
))))
888 gsi
= gsi_for_stmt (cond
);
889 if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
)))
891 /* Moving ASSIGN might change VR of lhs, e.g. when moving u_6
899 # RANGE [0, 4294967294]
900 u_6 = n_5 + 4294967295;
903 # u_3 = PHI <u_6(3), 4294967295(2)> */
904 SSA_NAME_RANGE_INFO (lhs
) = NULL
;
905 SSA_NAME_ANTI_RANGE_P (lhs
) = 0;
906 /* If available, we can use VR of phi result at least. */
907 tree phires
= gimple_phi_result (phi
);
908 struct range_info_def
*phires_range_info
909 = SSA_NAME_RANGE_INFO (phires
);
910 if (phires_range_info
)
911 duplicate_ssa_name_range_info (lhs
, SSA_NAME_RANGE_TYPE (phires
),
914 gimple_stmt_iterator gsi_from
= gsi_for_stmt (assign
);
915 gsi_move_before (&gsi_from
, &gsi
);
916 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, lhs
);
923 /* The function minmax_replacement does the main work of doing the minmax
924 replacement. Return true if the replacement is done. Otherwise return
926 BB is the basic block where the replacement is going to be done on. ARG0
927 is argument 0 from the PHI. Likewise for ARG1. */
930 minmax_replacement (basic_block cond_bb
, basic_block middle_bb
,
931 edge e0
, edge e1
, gimple phi
,
932 tree arg0
, tree arg1
)
937 edge true_edge
, false_edge
;
938 enum tree_code cmp
, minmax
, ass_code
;
939 tree smaller
, larger
, arg_true
, arg_false
;
940 gimple_stmt_iterator gsi
, gsi_from
;
942 type
= TREE_TYPE (PHI_RESULT (phi
));
944 /* The optimization may be unsafe due to NaNs. */
945 if (HONOR_NANS (type
))
948 cond
= as_a
<gcond
*> (last_stmt (cond_bb
));
949 cmp
= gimple_cond_code (cond
);
951 /* This transformation is only valid for order comparisons. Record which
952 operand is smaller/larger if the result of the comparison is true. */
953 if (cmp
== LT_EXPR
|| cmp
== LE_EXPR
)
955 smaller
= gimple_cond_lhs (cond
);
956 larger
= gimple_cond_rhs (cond
);
958 else if (cmp
== GT_EXPR
|| cmp
== GE_EXPR
)
960 smaller
= gimple_cond_rhs (cond
);
961 larger
= gimple_cond_lhs (cond
);
966 /* We need to know which is the true edge and which is the false
967 edge so that we know if have abs or negative abs. */
968 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
970 /* Forward the edges over the middle basic block. */
971 if (true_edge
->dest
== middle_bb
)
972 true_edge
= EDGE_SUCC (true_edge
->dest
, 0);
973 if (false_edge
->dest
== middle_bb
)
974 false_edge
= EDGE_SUCC (false_edge
->dest
, 0);
978 gcc_assert (false_edge
== e1
);
984 gcc_assert (false_edge
== e0
);
985 gcc_assert (true_edge
== e1
);
990 if (empty_block_p (middle_bb
))
992 if (operand_equal_for_phi_arg_p (arg_true
, smaller
)
993 && operand_equal_for_phi_arg_p (arg_false
, larger
))
997 if (smaller < larger)
1003 else if (operand_equal_for_phi_arg_p (arg_false
, smaller
)
1004 && operand_equal_for_phi_arg_p (arg_true
, larger
))
1011 /* Recognize the following case, assuming d <= u:
1017 This is equivalent to
1022 gimple assign
= last_and_only_stmt (middle_bb
);
1023 tree lhs
, op0
, op1
, bound
;
1026 || gimple_code (assign
) != GIMPLE_ASSIGN
)
1029 lhs
= gimple_assign_lhs (assign
);
1030 ass_code
= gimple_assign_rhs_code (assign
);
1031 if (ass_code
!= MAX_EXPR
&& ass_code
!= MIN_EXPR
)
1033 op0
= gimple_assign_rhs1 (assign
);
1034 op1
= gimple_assign_rhs2 (assign
);
1036 if (true_edge
->src
== middle_bb
)
1038 /* We got here if the condition is true, i.e., SMALLER < LARGER. */
1039 if (!operand_equal_for_phi_arg_p (lhs
, arg_true
))
1042 if (operand_equal_for_phi_arg_p (arg_false
, larger
))
1046 if (smaller < larger)
1048 r' = MAX_EXPR (smaller, bound)
1050 r = PHI <r', larger> --> to be turned to MIN_EXPR. */
1051 if (ass_code
!= MAX_EXPR
)
1055 if (operand_equal_for_phi_arg_p (op0
, smaller
))
1057 else if (operand_equal_for_phi_arg_p (op1
, smaller
))
1062 /* We need BOUND <= LARGER. */
1063 if (!integer_nonzerop (fold_build2 (LE_EXPR
, boolean_type_node
,
1067 else if (operand_equal_for_phi_arg_p (arg_false
, smaller
))
1071 if (smaller < larger)
1073 r' = MIN_EXPR (larger, bound)
1075 r = PHI <r', smaller> --> to be turned to MAX_EXPR. */
1076 if (ass_code
!= MIN_EXPR
)
1080 if (operand_equal_for_phi_arg_p (op0
, larger
))
1082 else if (operand_equal_for_phi_arg_p (op1
, larger
))
1087 /* We need BOUND >= SMALLER. */
1088 if (!integer_nonzerop (fold_build2 (GE_EXPR
, boolean_type_node
,
1097 /* We got here if the condition is false, i.e., SMALLER > LARGER. */
1098 if (!operand_equal_for_phi_arg_p (lhs
, arg_false
))
1101 if (operand_equal_for_phi_arg_p (arg_true
, larger
))
1105 if (smaller > larger)
1107 r' = MIN_EXPR (smaller, bound)
1109 r = PHI <r', larger> --> to be turned to MAX_EXPR. */
1110 if (ass_code
!= MIN_EXPR
)
1114 if (operand_equal_for_phi_arg_p (op0
, smaller
))
1116 else if (operand_equal_for_phi_arg_p (op1
, smaller
))
1121 /* We need BOUND >= LARGER. */
1122 if (!integer_nonzerop (fold_build2 (GE_EXPR
, boolean_type_node
,
1126 else if (operand_equal_for_phi_arg_p (arg_true
, smaller
))
1130 if (smaller > larger)
1132 r' = MAX_EXPR (larger, bound)
1134 r = PHI <r', smaller> --> to be turned to MIN_EXPR. */
1135 if (ass_code
!= MAX_EXPR
)
1139 if (operand_equal_for_phi_arg_p (op0
, larger
))
1141 else if (operand_equal_for_phi_arg_p (op1
, larger
))
1146 /* We need BOUND <= SMALLER. */
1147 if (!integer_nonzerop (fold_build2 (LE_EXPR
, boolean_type_node
,
1155 /* Move the statement from the middle block. */
1156 gsi
= gsi_last_bb (cond_bb
);
1157 gsi_from
= gsi_last_nondebug_bb (middle_bb
);
1158 gsi_move_before (&gsi_from
, &gsi
);
1161 /* Emit the statement to compute min/max. */
1162 result
= duplicate_ssa_name (PHI_RESULT (phi
), NULL
);
1163 new_stmt
= gimple_build_assign (result
, minmax
, arg0
, arg1
);
1164 gsi
= gsi_last_bb (cond_bb
);
1165 gsi_insert_before (&gsi
, new_stmt
, GSI_NEW_STMT
);
1167 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, result
);
1171 /* The function absolute_replacement does the main work of doing the absolute
1172 replacement. Return true if the replacement is done. Otherwise return
1174 bb is the basic block where the replacement is going to be done on. arg0
1175 is argument 0 from the phi. Likewise for arg1. */
1178 abs_replacement (basic_block cond_bb
, basic_block middle_bb
,
1179 edge e0 ATTRIBUTE_UNUSED
, edge e1
,
1180 gimple phi
, tree arg0
, tree arg1
)
1185 gimple_stmt_iterator gsi
;
1186 edge true_edge
, false_edge
;
1191 enum tree_code cond_code
;
1193 /* If the type says honor signed zeros we cannot do this
1195 if (HONOR_SIGNED_ZEROS (arg1
))
1198 /* OTHER_BLOCK must have only one executable statement which must have the
1199 form arg0 = -arg1 or arg1 = -arg0. */
1201 assign
= last_and_only_stmt (middle_bb
);
1202 /* If we did not find the proper negation assignment, then we can not
1207 /* If we got here, then we have found the only executable statement
1208 in OTHER_BLOCK. If it is anything other than arg = -arg1 or
1209 arg1 = -arg0, then we can not optimize. */
1210 if (gimple_code (assign
) != GIMPLE_ASSIGN
)
1213 lhs
= gimple_assign_lhs (assign
);
1215 if (gimple_assign_rhs_code (assign
) != NEGATE_EXPR
)
1218 rhs
= gimple_assign_rhs1 (assign
);
1220 /* The assignment has to be arg0 = -arg1 or arg1 = -arg0. */
1221 if (!(lhs
== arg0
&& rhs
== arg1
)
1222 && !(lhs
== arg1
&& rhs
== arg0
))
1225 cond
= last_stmt (cond_bb
);
1226 result
= PHI_RESULT (phi
);
1228 /* Only relationals comparing arg[01] against zero are interesting. */
1229 cond_code
= gimple_cond_code (cond
);
1230 if (cond_code
!= GT_EXPR
&& cond_code
!= GE_EXPR
1231 && cond_code
!= LT_EXPR
&& cond_code
!= LE_EXPR
)
1234 /* Make sure the conditional is arg[01] OP y. */
1235 if (gimple_cond_lhs (cond
) != rhs
)
1238 if (FLOAT_TYPE_P (TREE_TYPE (gimple_cond_rhs (cond
)))
1239 ? real_zerop (gimple_cond_rhs (cond
))
1240 : integer_zerop (gimple_cond_rhs (cond
)))
1245 /* We need to know which is the true edge and which is the false
1246 edge so that we know if have abs or negative abs. */
1247 extract_true_false_edges_from_block (cond_bb
, &true_edge
, &false_edge
);
1249 /* For GT_EXPR/GE_EXPR, if the true edge goes to OTHER_BLOCK, then we
1250 will need to negate the result. Similarly for LT_EXPR/LE_EXPR if
1251 the false edge goes to OTHER_BLOCK. */
1252 if (cond_code
== GT_EXPR
|| cond_code
== GE_EXPR
)
1257 if (e
->dest
== middle_bb
)
1262 result
= duplicate_ssa_name (result
, NULL
);
1265 lhs
= make_ssa_name (TREE_TYPE (result
));
1269 /* Build the modify expression with abs expression. */
1270 new_stmt
= gimple_build_assign (lhs
, ABS_EXPR
, rhs
);
1272 gsi
= gsi_last_bb (cond_bb
);
1273 gsi_insert_before (&gsi
, new_stmt
, GSI_NEW_STMT
);
1277 /* Get the right GSI. We want to insert after the recently
1278 added ABS_EXPR statement (which we know is the first statement
1280 new_stmt
= gimple_build_assign (result
, NEGATE_EXPR
, lhs
);
1282 gsi_insert_after (&gsi
, new_stmt
, GSI_NEW_STMT
);
1285 replace_phi_edge_with_variable (cond_bb
, e1
, phi
, result
);
1287 /* Note that we optimized this PHI. */
1291 /* Auxiliary functions to determine the set of memory accesses which
1292 can't trap because they are preceded by accesses to the same memory
1293 portion. We do that for MEM_REFs, so we only need to track
1294 the SSA_NAME of the pointer indirectly referenced. The algorithm
1295 simply is a walk over all instructions in dominator order. When
1296 we see an MEM_REF we determine if we've already seen a same
1297 ref anywhere up to the root of the dominator tree. If we do the
1298 current access can't trap. If we don't see any dominating access
1299 the current access might trap, but might also make later accesses
1300 non-trapping, so we remember it. We need to be careful with loads
1301 or stores, for instance a load might not trap, while a store would,
1302 so if we see a dominating read access this doesn't mean that a later
1303 write access would not trap. Hence we also need to differentiate the
1304 type of access(es) seen.
1306 ??? We currently are very conservative and assume that a load might
1307 trap even if a store doesn't (write-only memory). This probably is
1308 overly conservative. */
1310 /* A hash-table of SSA_NAMEs, and in which basic block an MEM_REF
1311 through it was seen, which would constitute a no-trap region for
1315 unsigned int ssa_name_ver
;
1318 HOST_WIDE_INT offset
, size
;
1322 /* Hashtable helpers. */
1324 struct ssa_names_hasher
: typed_free_remove
<name_to_bb
>
1326 typedef name_to_bb
*value_type
;
1327 typedef name_to_bb
*compare_type
;
1328 static inline hashval_t
hash (const name_to_bb
*);
1329 static inline bool equal (const name_to_bb
*, const name_to_bb
*);
1332 /* Used for quick clearing of the hash-table when we see calls.
1333 Hash entries with phase < nt_call_phase are invalid. */
1334 static unsigned int nt_call_phase
;
1336 /* The hash function. */
1339 ssa_names_hasher::hash (const name_to_bb
*n
)
1341 return n
->ssa_name_ver
^ (((hashval_t
) n
->store
) << 31)
1342 ^ (n
->offset
<< 6) ^ (n
->size
<< 3);
1345 /* The equality function of *P1 and *P2. */
1348 ssa_names_hasher::equal (const name_to_bb
*n1
, const name_to_bb
*n2
)
1350 return n1
->ssa_name_ver
== n2
->ssa_name_ver
1351 && n1
->store
== n2
->store
1352 && n1
->offset
== n2
->offset
1353 && n1
->size
== n2
->size
;
1356 class nontrapping_dom_walker
: public dom_walker
1359 nontrapping_dom_walker (cdi_direction direction
, hash_set
<tree
> *ps
)
1360 : dom_walker (direction
), m_nontrapping (ps
), m_seen_ssa_names (128) {}
1362 virtual void before_dom_children (basic_block
);
1363 virtual void after_dom_children (basic_block
);
1367 /* We see the expression EXP in basic block BB. If it's an interesting
1368 expression (an MEM_REF through an SSA_NAME) possibly insert the
1369 expression into the set NONTRAP or the hash table of seen expressions.
1370 STORE is true if this expression is on the LHS, otherwise it's on
1372 void add_or_mark_expr (basic_block
, tree
, bool);
1374 hash_set
<tree
> *m_nontrapping
;
1376 /* The hash table for remembering what we've seen. */
1377 hash_table
<ssa_names_hasher
> m_seen_ssa_names
;
1380 /* Called by walk_dominator_tree, when entering the block BB. */
1382 nontrapping_dom_walker::before_dom_children (basic_block bb
)
1386 gimple_stmt_iterator gsi
;
1388 /* If we haven't seen all our predecessors, clear the hash-table. */
1389 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1390 if ((((size_t)e
->src
->aux
) & 2) == 0)
1396 /* Mark this BB as being on the path to dominator root and as visited. */
1397 bb
->aux
= (void*)(1 | 2);
1399 /* And walk the statements in order. */
1400 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1402 gimple stmt
= gsi_stmt (gsi
);
1404 if (is_gimple_call (stmt
) && !nonfreeing_call_p (stmt
))
1406 else if (gimple_assign_single_p (stmt
) && !gimple_has_volatile_ops (stmt
))
1408 add_or_mark_expr (bb
, gimple_assign_lhs (stmt
), true);
1409 add_or_mark_expr (bb
, gimple_assign_rhs1 (stmt
), false);
1414 /* Called by walk_dominator_tree, when basic block BB is exited. */
1416 nontrapping_dom_walker::after_dom_children (basic_block bb
)
1418 /* This BB isn't on the path to dominator root anymore. */
1422 /* We see the expression EXP in basic block BB. If it's an interesting
1423 expression (an MEM_REF through an SSA_NAME) possibly insert the
1424 expression into the set NONTRAP or the hash table of seen expressions.
1425 STORE is true if this expression is on the LHS, otherwise it's on
1428 nontrapping_dom_walker::add_or_mark_expr (basic_block bb
, tree exp
, bool store
)
1432 if (TREE_CODE (exp
) == MEM_REF
1433 && TREE_CODE (TREE_OPERAND (exp
, 0)) == SSA_NAME
1434 && tree_fits_shwi_p (TREE_OPERAND (exp
, 1))
1435 && (size
= int_size_in_bytes (TREE_TYPE (exp
))) > 0)
1437 tree name
= TREE_OPERAND (exp
, 0);
1438 struct name_to_bb map
;
1440 struct name_to_bb
*n2bb
;
1441 basic_block found_bb
= 0;
1443 /* Try to find the last seen MEM_REF through the same
1444 SSA_NAME, which can trap. */
1445 map
.ssa_name_ver
= SSA_NAME_VERSION (name
);
1449 map
.offset
= tree_to_shwi (TREE_OPERAND (exp
, 1));
1452 slot
= m_seen_ssa_names
.find_slot (&map
, INSERT
);
1454 if (n2bb
&& n2bb
->phase
>= nt_call_phase
)
1455 found_bb
= n2bb
->bb
;
1457 /* If we've found a trapping MEM_REF, _and_ it dominates EXP
1458 (it's in a basic block on the path from us to the dominator root)
1459 then we can't trap. */
1460 if (found_bb
&& (((size_t)found_bb
->aux
) & 1) == 1)
1462 m_nontrapping
->add (exp
);
1466 /* EXP might trap, so insert it into the hash table. */
1469 n2bb
->phase
= nt_call_phase
;
1474 n2bb
= XNEW (struct name_to_bb
);
1475 n2bb
->ssa_name_ver
= SSA_NAME_VERSION (name
);
1476 n2bb
->phase
= nt_call_phase
;
1478 n2bb
->store
= store
;
1479 n2bb
->offset
= map
.offset
;
1487 /* This is the entry point of gathering non trapping memory accesses.
1488 It will do a dominator walk over the whole function, and it will
1489 make use of the bb->aux pointers. It returns a set of trees
1490 (the MEM_REFs itself) which can't trap. */
1491 static hash_set
<tree
> *
1492 get_non_trapping (void)
1495 hash_set
<tree
> *nontrap
= new hash_set
<tree
>;
1496 /* We're going to do a dominator walk, so ensure that we have
1497 dominance information. */
1498 calculate_dominance_info (CDI_DOMINATORS
);
1500 nontrapping_dom_walker (CDI_DOMINATORS
, nontrap
)
1501 .walk (cfun
->cfg
->x_entry_block_ptr
);
1503 clear_aux_for_blocks ();
1507 /* Do the main work of conditional store replacement. We already know
1508 that the recognized pattern looks like so:
1511 if (cond) goto MIDDLE_BB; else goto JOIN_BB (edge E1)
1514 fallthrough (edge E0)
1518 We check that MIDDLE_BB contains only one store, that that store
1519 doesn't trap (not via NOTRAP, but via checking if an access to the same
1520 memory location dominates us) and that the store has a "simple" RHS. */
1523 cond_store_replacement (basic_block middle_bb
, basic_block join_bb
,
1524 edge e0
, edge e1
, hash_set
<tree
> *nontrap
)
1526 gimple assign
= last_and_only_stmt (middle_bb
);
1527 tree lhs
, rhs
, name
, name2
;
1530 gimple_stmt_iterator gsi
;
1531 source_location locus
;
1533 /* Check if middle_bb contains of only one store. */
1535 || !gimple_assign_single_p (assign
)
1536 || gimple_has_volatile_ops (assign
))
1539 locus
= gimple_location (assign
);
1540 lhs
= gimple_assign_lhs (assign
);
1541 rhs
= gimple_assign_rhs1 (assign
);
1542 if (TREE_CODE (lhs
) != MEM_REF
1543 || TREE_CODE (TREE_OPERAND (lhs
, 0)) != SSA_NAME
1544 || !is_gimple_reg_type (TREE_TYPE (lhs
)))
1547 /* Prove that we can move the store down. We could also check
1548 TREE_THIS_NOTRAP here, but in that case we also could move stores,
1549 whose value is not available readily, which we want to avoid. */
1550 if (!nontrap
->contains (lhs
))
1553 /* Now we've checked the constraints, so do the transformation:
1554 1) Remove the single store. */
1555 gsi
= gsi_for_stmt (assign
);
1556 unlink_stmt_vdef (assign
);
1557 gsi_remove (&gsi
, true);
1558 release_defs (assign
);
1560 /* 2) Insert a load from the memory of the store to the temporary
1561 on the edge which did not contain the store. */
1562 lhs
= unshare_expr (lhs
);
1563 name
= make_temp_ssa_name (TREE_TYPE (lhs
), NULL
, "cstore");
1564 new_stmt
= gimple_build_assign (name
, lhs
);
1565 gimple_set_location (new_stmt
, locus
);
1566 gsi_insert_on_edge (e1
, new_stmt
);
1568 /* 3) Create a PHI node at the join block, with one argument
1569 holding the old RHS, and the other holding the temporary
1570 where we stored the old memory contents. */
1571 name2
= make_temp_ssa_name (TREE_TYPE (lhs
), NULL
, "cstore");
1572 newphi
= create_phi_node (name2
, join_bb
);
1573 add_phi_arg (newphi
, rhs
, e0
, locus
);
1574 add_phi_arg (newphi
, name
, e1
, locus
);
1576 lhs
= unshare_expr (lhs
);
1577 new_stmt
= gimple_build_assign (lhs
, PHI_RESULT (newphi
));
1579 /* 4) Insert that PHI node. */
1580 gsi
= gsi_after_labels (join_bb
);
1581 if (gsi_end_p (gsi
))
1583 gsi
= gsi_last_bb (join_bb
);
1584 gsi_insert_after (&gsi
, new_stmt
, GSI_NEW_STMT
);
1587 gsi_insert_before (&gsi
, new_stmt
, GSI_NEW_STMT
);
1592 /* Do the main work of conditional store replacement. */
1595 cond_if_else_store_replacement_1 (basic_block then_bb
, basic_block else_bb
,
1596 basic_block join_bb
, gimple then_assign
,
1599 tree lhs_base
, lhs
, then_rhs
, else_rhs
, name
;
1600 source_location then_locus
, else_locus
;
1601 gimple_stmt_iterator gsi
;
1605 if (then_assign
== NULL
1606 || !gimple_assign_single_p (then_assign
)
1607 || gimple_clobber_p (then_assign
)
1608 || gimple_has_volatile_ops (then_assign
)
1609 || else_assign
== NULL
1610 || !gimple_assign_single_p (else_assign
)
1611 || gimple_clobber_p (else_assign
)
1612 || gimple_has_volatile_ops (else_assign
))
1615 lhs
= gimple_assign_lhs (then_assign
);
1616 if (!is_gimple_reg_type (TREE_TYPE (lhs
))
1617 || !operand_equal_p (lhs
, gimple_assign_lhs (else_assign
), 0))
1620 lhs_base
= get_base_address (lhs
);
1621 if (lhs_base
== NULL_TREE
1622 || (!DECL_P (lhs_base
) && TREE_CODE (lhs_base
) != MEM_REF
))
1625 then_rhs
= gimple_assign_rhs1 (then_assign
);
1626 else_rhs
= gimple_assign_rhs1 (else_assign
);
1627 then_locus
= gimple_location (then_assign
);
1628 else_locus
= gimple_location (else_assign
);
1630 /* Now we've checked the constraints, so do the transformation:
1631 1) Remove the stores. */
1632 gsi
= gsi_for_stmt (then_assign
);
1633 unlink_stmt_vdef (then_assign
);
1634 gsi_remove (&gsi
, true);
1635 release_defs (then_assign
);
1637 gsi
= gsi_for_stmt (else_assign
);
1638 unlink_stmt_vdef (else_assign
);
1639 gsi_remove (&gsi
, true);
1640 release_defs (else_assign
);
1642 /* 2) Create a PHI node at the join block, with one argument
1643 holding the old RHS, and the other holding the temporary
1644 where we stored the old memory contents. */
1645 name
= make_temp_ssa_name (TREE_TYPE (lhs
), NULL
, "cstore");
1646 newphi
= create_phi_node (name
, join_bb
);
1647 add_phi_arg (newphi
, then_rhs
, EDGE_SUCC (then_bb
, 0), then_locus
);
1648 add_phi_arg (newphi
, else_rhs
, EDGE_SUCC (else_bb
, 0), else_locus
);
1650 new_stmt
= gimple_build_assign (lhs
, PHI_RESULT (newphi
));
1652 /* 3) Insert that PHI node. */
1653 gsi
= gsi_after_labels (join_bb
);
1654 if (gsi_end_p (gsi
))
1656 gsi
= gsi_last_bb (join_bb
);
1657 gsi_insert_after (&gsi
, new_stmt
, GSI_NEW_STMT
);
1660 gsi_insert_before (&gsi
, new_stmt
, GSI_NEW_STMT
);
1665 /* Conditional store replacement. We already know
1666 that the recognized pattern looks like so:
1669 if (cond) goto THEN_BB; else goto ELSE_BB (edge E1)
1679 fallthrough (edge E0)
1683 We check that it is safe to sink the store to JOIN_BB by verifying that
1684 there are no read-after-write or write-after-write dependencies in
1685 THEN_BB and ELSE_BB. */
1688 cond_if_else_store_replacement (basic_block then_bb
, basic_block else_bb
,
1689 basic_block join_bb
)
1691 gimple then_assign
= last_and_only_stmt (then_bb
);
1692 gimple else_assign
= last_and_only_stmt (else_bb
);
1693 vec
<data_reference_p
> then_datarefs
, else_datarefs
;
1694 vec
<ddr_p
> then_ddrs
, else_ddrs
;
1695 gimple then_store
, else_store
;
1696 bool found
, ok
= false, res
;
1697 struct data_dependence_relation
*ddr
;
1698 data_reference_p then_dr
, else_dr
;
1700 tree then_lhs
, else_lhs
;
1701 basic_block blocks
[3];
1703 if (MAX_STORES_TO_SINK
== 0)
1706 /* Handle the case with single statement in THEN_BB and ELSE_BB. */
1707 if (then_assign
&& else_assign
)
1708 return cond_if_else_store_replacement_1 (then_bb
, else_bb
, join_bb
,
1709 then_assign
, else_assign
);
1711 /* Find data references. */
1712 then_datarefs
.create (1);
1713 else_datarefs
.create (1);
1714 if ((find_data_references_in_bb (NULL
, then_bb
, &then_datarefs
)
1716 || !then_datarefs
.length ()
1717 || (find_data_references_in_bb (NULL
, else_bb
, &else_datarefs
)
1719 || !else_datarefs
.length ())
1721 free_data_refs (then_datarefs
);
1722 free_data_refs (else_datarefs
);
1726 /* Find pairs of stores with equal LHS. */
1727 auto_vec
<gimple
, 1> then_stores
, else_stores
;
1728 FOR_EACH_VEC_ELT (then_datarefs
, i
, then_dr
)
1730 if (DR_IS_READ (then_dr
))
1733 then_store
= DR_STMT (then_dr
);
1734 then_lhs
= gimple_get_lhs (then_store
);
1735 if (then_lhs
== NULL_TREE
)
1739 FOR_EACH_VEC_ELT (else_datarefs
, j
, else_dr
)
1741 if (DR_IS_READ (else_dr
))
1744 else_store
= DR_STMT (else_dr
);
1745 else_lhs
= gimple_get_lhs (else_store
);
1746 if (else_lhs
== NULL_TREE
)
1749 if (operand_equal_p (then_lhs
, else_lhs
, 0))
1759 then_stores
.safe_push (then_store
);
1760 else_stores
.safe_push (else_store
);
1763 /* No pairs of stores found. */
1764 if (!then_stores
.length ()
1765 || then_stores
.length () > (unsigned) MAX_STORES_TO_SINK
)
1767 free_data_refs (then_datarefs
);
1768 free_data_refs (else_datarefs
);
1772 /* Compute and check data dependencies in both basic blocks. */
1773 then_ddrs
.create (1);
1774 else_ddrs
.create (1);
1775 if (!compute_all_dependences (then_datarefs
, &then_ddrs
,
1777 || !compute_all_dependences (else_datarefs
, &else_ddrs
,
1780 free_dependence_relations (then_ddrs
);
1781 free_dependence_relations (else_ddrs
);
1782 free_data_refs (then_datarefs
);
1783 free_data_refs (else_datarefs
);
1786 blocks
[0] = then_bb
;
1787 blocks
[1] = else_bb
;
1788 blocks
[2] = join_bb
;
1789 renumber_gimple_stmt_uids_in_blocks (blocks
, 3);
1791 /* Check that there are no read-after-write or write-after-write dependencies
1793 FOR_EACH_VEC_ELT (then_ddrs
, i
, ddr
)
1795 struct data_reference
*dra
= DDR_A (ddr
);
1796 struct data_reference
*drb
= DDR_B (ddr
);
1798 if (DDR_ARE_DEPENDENT (ddr
) != chrec_known
1799 && ((DR_IS_READ (dra
) && DR_IS_WRITE (drb
)
1800 && gimple_uid (DR_STMT (dra
)) > gimple_uid (DR_STMT (drb
)))
1801 || (DR_IS_READ (drb
) && DR_IS_WRITE (dra
)
1802 && gimple_uid (DR_STMT (drb
)) > gimple_uid (DR_STMT (dra
)))
1803 || (DR_IS_WRITE (dra
) && DR_IS_WRITE (drb
))))
1805 free_dependence_relations (then_ddrs
);
1806 free_dependence_relations (else_ddrs
);
1807 free_data_refs (then_datarefs
);
1808 free_data_refs (else_datarefs
);
1813 /* Check that there are no read-after-write or write-after-write dependencies
1815 FOR_EACH_VEC_ELT (else_ddrs
, i
, ddr
)
1817 struct data_reference
*dra
= DDR_A (ddr
);
1818 struct data_reference
*drb
= DDR_B (ddr
);
1820 if (DDR_ARE_DEPENDENT (ddr
) != chrec_known
1821 && ((DR_IS_READ (dra
) && DR_IS_WRITE (drb
)
1822 && gimple_uid (DR_STMT (dra
)) > gimple_uid (DR_STMT (drb
)))
1823 || (DR_IS_READ (drb
) && DR_IS_WRITE (dra
)
1824 && gimple_uid (DR_STMT (drb
)) > gimple_uid (DR_STMT (dra
)))
1825 || (DR_IS_WRITE (dra
) && DR_IS_WRITE (drb
))))
1827 free_dependence_relations (then_ddrs
);
1828 free_dependence_relations (else_ddrs
);
1829 free_data_refs (then_datarefs
);
1830 free_data_refs (else_datarefs
);
1835 /* Sink stores with same LHS. */
1836 FOR_EACH_VEC_ELT (then_stores
, i
, then_store
)
1838 else_store
= else_stores
[i
];
1839 res
= cond_if_else_store_replacement_1 (then_bb
, else_bb
, join_bb
,
1840 then_store
, else_store
);
1844 free_dependence_relations (then_ddrs
);
1845 free_dependence_relations (else_ddrs
);
1846 free_data_refs (then_datarefs
);
1847 free_data_refs (else_datarefs
);
1852 /* Return TRUE if STMT has a VUSE whose corresponding VDEF is in BB. */
1855 local_mem_dependence (gimple stmt
, basic_block bb
)
1857 tree vuse
= gimple_vuse (stmt
);
1863 def
= SSA_NAME_DEF_STMT (vuse
);
1864 return (def
&& gimple_bb (def
) == bb
);
1867 /* Given a "diamond" control-flow pattern where BB0 tests a condition,
1868 BB1 and BB2 are "then" and "else" blocks dependent on this test,
1869 and BB3 rejoins control flow following BB1 and BB2, look for
1870 opportunities to hoist loads as follows. If BB3 contains a PHI of
1871 two loads, one each occurring in BB1 and BB2, and the loads are
1872 provably of adjacent fields in the same structure, then move both
1873 loads into BB0. Of course this can only be done if there are no
1874 dependencies preventing such motion.
1876 One of the hoisted loads will always be speculative, so the
1877 transformation is currently conservative:
1879 - The fields must be strictly adjacent.
1880 - The two fields must occupy a single memory block that is
1881 guaranteed to not cross a page boundary.
1883 The last is difficult to prove, as such memory blocks should be
1884 aligned on the minimum of the stack alignment boundary and the
1885 alignment guaranteed by heap allocation interfaces. Thus we rely
1886 on a parameter for the alignment value.
1888 Provided a good value is used for the last case, the first
1889 restriction could possibly be relaxed. */
1892 hoist_adjacent_loads (basic_block bb0
, basic_block bb1
,
1893 basic_block bb2
, basic_block bb3
)
1895 int param_align
= PARAM_VALUE (PARAM_L1_CACHE_LINE_SIZE
);
1896 unsigned param_align_bits
= (unsigned) (param_align
* BITS_PER_UNIT
);
1899 /* Walk the phis in bb3 looking for an opportunity. We are looking
1900 for phis of two SSA names, one each of which is defined in bb1 and
1902 for (gsi
= gsi_start_phis (bb3
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1904 gphi
*phi_stmt
= gsi
.phi ();
1906 tree arg1
, arg2
, ref1
, ref2
, field1
, field2
;
1907 tree tree_offset1
, tree_offset2
, tree_size2
, next
;
1908 int offset1
, offset2
, size2
;
1910 gimple_stmt_iterator gsi2
;
1911 basic_block bb_for_def1
, bb_for_def2
;
1913 if (gimple_phi_num_args (phi_stmt
) != 2
1914 || virtual_operand_p (gimple_phi_result (phi_stmt
)))
1917 arg1
= gimple_phi_arg_def (phi_stmt
, 0);
1918 arg2
= gimple_phi_arg_def (phi_stmt
, 1);
1920 if (TREE_CODE (arg1
) != SSA_NAME
1921 || TREE_CODE (arg2
) != SSA_NAME
1922 || SSA_NAME_IS_DEFAULT_DEF (arg1
)
1923 || SSA_NAME_IS_DEFAULT_DEF (arg2
))
1926 def1
= SSA_NAME_DEF_STMT (arg1
);
1927 def2
= SSA_NAME_DEF_STMT (arg2
);
1929 if ((gimple_bb (def1
) != bb1
|| gimple_bb (def2
) != bb2
)
1930 && (gimple_bb (def2
) != bb1
|| gimple_bb (def1
) != bb2
))
1933 /* Check the mode of the arguments to be sure a conditional move
1934 can be generated for it. */
1935 if (optab_handler (movcc_optab
, TYPE_MODE (TREE_TYPE (arg1
)))
1936 == CODE_FOR_nothing
)
1939 /* Both statements must be assignments whose RHS is a COMPONENT_REF. */
1940 if (!gimple_assign_single_p (def1
)
1941 || !gimple_assign_single_p (def2
)
1942 || gimple_has_volatile_ops (def1
)
1943 || gimple_has_volatile_ops (def2
))
1946 ref1
= gimple_assign_rhs1 (def1
);
1947 ref2
= gimple_assign_rhs1 (def2
);
1949 if (TREE_CODE (ref1
) != COMPONENT_REF
1950 || TREE_CODE (ref2
) != COMPONENT_REF
)
1953 /* The zeroth operand of the two component references must be
1954 identical. It is not sufficient to compare get_base_address of
1955 the two references, because this could allow for different
1956 elements of the same array in the two trees. It is not safe to
1957 assume that the existence of one array element implies the
1958 existence of a different one. */
1959 if (!operand_equal_p (TREE_OPERAND (ref1
, 0), TREE_OPERAND (ref2
, 0), 0))
1962 field1
= TREE_OPERAND (ref1
, 1);
1963 field2
= TREE_OPERAND (ref2
, 1);
1965 /* Check for field adjacency, and ensure field1 comes first. */
1966 for (next
= DECL_CHAIN (field1
);
1967 next
&& TREE_CODE (next
) != FIELD_DECL
;
1968 next
= DECL_CHAIN (next
))
1973 for (next
= DECL_CHAIN (field2
);
1974 next
&& TREE_CODE (next
) != FIELD_DECL
;
1975 next
= DECL_CHAIN (next
))
1981 std::swap (field1
, field2
);
1982 std::swap (def1
, def2
);
1985 bb_for_def1
= gimple_bb (def1
);
1986 bb_for_def2
= gimple_bb (def2
);
1988 /* Check for proper alignment of the first field. */
1989 tree_offset1
= bit_position (field1
);
1990 tree_offset2
= bit_position (field2
);
1991 tree_size2
= DECL_SIZE (field2
);
1993 if (!tree_fits_uhwi_p (tree_offset1
)
1994 || !tree_fits_uhwi_p (tree_offset2
)
1995 || !tree_fits_uhwi_p (tree_size2
))
1998 offset1
= tree_to_uhwi (tree_offset1
);
1999 offset2
= tree_to_uhwi (tree_offset2
);
2000 size2
= tree_to_uhwi (tree_size2
);
2001 align1
= DECL_ALIGN (field1
) % param_align_bits
;
2003 if (offset1
% BITS_PER_UNIT
!= 0)
2006 /* For profitability, the two field references should fit within
2007 a single cache line. */
2008 if (align1
+ offset2
- offset1
+ size2
> param_align_bits
)
2011 /* The two expressions cannot be dependent upon vdefs defined
2013 if (local_mem_dependence (def1
, bb_for_def1
)
2014 || local_mem_dependence (def2
, bb_for_def2
))
2017 /* The conditions are satisfied; hoist the loads from bb1 and bb2 into
2018 bb0. We hoist the first one first so that a cache miss is handled
2019 efficiently regardless of hardware cache-fill policy. */
2020 gsi2
= gsi_for_stmt (def1
);
2021 gsi_move_to_bb_end (&gsi2
, bb0
);
2022 gsi2
= gsi_for_stmt (def2
);
2023 gsi_move_to_bb_end (&gsi2
, bb0
);
2025 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2028 "\nHoisting adjacent loads from %d and %d into %d: \n",
2029 bb_for_def1
->index
, bb_for_def2
->index
, bb0
->index
);
2030 print_gimple_stmt (dump_file
, def1
, 0, TDF_VOPS
|TDF_MEMSYMS
);
2031 print_gimple_stmt (dump_file
, def2
, 0, TDF_VOPS
|TDF_MEMSYMS
);
2036 /* Determine whether we should attempt to hoist adjacent loads out of
2037 diamond patterns in pass_phiopt. Always hoist loads if
2038 -fhoist-adjacent-loads is specified and the target machine has
2039 both a conditional move instruction and a defined cache line size. */
2042 gate_hoist_loads (void)
2044 return (flag_hoist_adjacent_loads
== 1
2045 && PARAM_VALUE (PARAM_L1_CACHE_LINE_SIZE
)
2046 && HAVE_conditional_move
);
2049 /* This pass tries to replaces an if-then-else block with an
2050 assignment. We have four kinds of transformations. Some of these
2051 transformations are also performed by the ifcvt RTL optimizer.
2053 Conditional Replacement
2054 -----------------------
2056 This transformation, implemented in conditional_replacement,
2060 if (cond) goto bb2; else goto bb1;
2063 x = PHI <0 (bb1), 1 (bb0), ...>;
2071 x = PHI <x' (bb0), ...>;
2073 We remove bb1 as it becomes unreachable. This occurs often due to
2074 gimplification of conditionals.
2079 This transformation, implemented in value_replacement, replaces
2082 if (a != b) goto bb2; else goto bb1;
2085 x = PHI <a (bb1), b (bb0), ...>;
2091 x = PHI <b (bb0), ...>;
2093 This opportunity can sometimes occur as a result of other
2097 Another case caught by value replacement looks like this:
2103 if (t3 != 0) goto bb1; else goto bb2;
2119 This transformation, implemented in abs_replacement, replaces
2122 if (a >= 0) goto bb2; else goto bb1;
2126 x = PHI <x (bb1), a (bb0), ...>;
2133 x = PHI <x' (bb0), ...>;
2138 This transformation, minmax_replacement replaces
2141 if (a <= b) goto bb2; else goto bb1;
2144 x = PHI <b (bb1), a (bb0), ...>;
2149 x' = MIN_EXPR (a, b)
2151 x = PHI <x' (bb0), ...>;
2153 A similar transformation is done for MAX_EXPR.
2156 This pass also performs a fifth transformation of a slightly different
2159 Adjacent Load Hoisting
2160 ----------------------
2162 This transformation replaces
2165 if (...) goto bb2; else goto bb1;
2167 x1 = (<expr>).field1;
2170 x2 = (<expr>).field2;
2177 x1 = (<expr>).field1;
2178 x2 = (<expr>).field2;
2179 if (...) goto bb2; else goto bb1;
2186 The purpose of this transformation is to enable generation of conditional
2187 move instructions such as Intel CMOVE or PowerPC ISEL. Because one of
2188 the loads is speculative, the transformation is restricted to very
2189 specific cases to avoid introducing a page fault. We are looking for
2197 where left and right are typically adjacent pointers in a tree structure. */
2201 const pass_data pass_data_phiopt
=
2203 GIMPLE_PASS
, /* type */
2204 "phiopt", /* name */
2205 OPTGROUP_NONE
, /* optinfo_flags */
2206 TV_TREE_PHIOPT
, /* tv_id */
2207 ( PROP_cfg
| PROP_ssa
), /* properties_required */
2208 0, /* properties_provided */
2209 0, /* properties_destroyed */
2210 0, /* todo_flags_start */
2211 0, /* todo_flags_finish */
2214 class pass_phiopt
: public gimple_opt_pass
2217 pass_phiopt (gcc::context
*ctxt
)
2218 : gimple_opt_pass (pass_data_phiopt
, ctxt
)
2221 /* opt_pass methods: */
2222 opt_pass
* clone () { return new pass_phiopt (m_ctxt
); }
2223 virtual bool gate (function
*) { return flag_ssa_phiopt
; }
2224 virtual unsigned int execute (function
*)
2226 return tree_ssa_phiopt_worker (false, gate_hoist_loads ());
2229 }; // class pass_phiopt
2234 make_pass_phiopt (gcc::context
*ctxt
)
2236 return new pass_phiopt (ctxt
);
2241 const pass_data pass_data_cselim
=
2243 GIMPLE_PASS
, /* type */
2244 "cselim", /* name */
2245 OPTGROUP_NONE
, /* optinfo_flags */
2246 TV_TREE_PHIOPT
, /* tv_id */
2247 ( PROP_cfg
| PROP_ssa
), /* properties_required */
2248 0, /* properties_provided */
2249 0, /* properties_destroyed */
2250 0, /* todo_flags_start */
2251 0, /* todo_flags_finish */
2254 class pass_cselim
: public gimple_opt_pass
2257 pass_cselim (gcc::context
*ctxt
)
2258 : gimple_opt_pass (pass_data_cselim
, ctxt
)
2261 /* opt_pass methods: */
2262 virtual bool gate (function
*) { return flag_tree_cselim
; }
2263 virtual unsigned int execute (function
*) { return tree_ssa_cs_elim (); }
2265 }; // class pass_cselim
2270 make_pass_cselim (gcc::context
*ctxt
)
2272 return new pass_cselim (ctxt
);