1 /* Conditional Dead Call Elimination pass for the GNU compiler.
3 Free Software Foundation, Inc.
4 Contributed by Xinliang David Li <davidxl@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by the
10 Free Software Foundation; either version 3, or (at your option) any
13 GCC is distributed in the hope that it will be useful, but WITHOUT
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
28 /* These RTL headers are needed for basic-block.h. */
31 #include "hard-reg-set.h"
33 #include "basic-block.h"
36 #include "diagnostic.h"
37 #include "tree-flow.h"
38 #include "tree-gimple.h"
39 #include "tree-dump.h"
40 #include "tree-pass.h"
45 /* Conditional dead call elimination
47 Some builtin functions can set errno on error conditions, but they
48 are otherwise pure. If the result of a call to such a function is
49 not used, the compiler can still not eliminate the call without
50 powerful interprocedural analysis to prove that the errno is not
51 checked. However, if the conditions under which the error occurs
52 are known, the compiler can conditionally dead code eliminate the
53 calls by shrink-wrapping the semi-dead calls into the error condition:
57 if (error_cond (args))
60 An actual simple example is :
61 log (x); // Mostly dead call
65 With this change, call to log (x) is effectively eliminated, as
66 in majority of the cases, log won't be called with x out of
67 range. The branch is totally predictable, so the branch cost
70 Note that library functions are not supposed to clear errno to zero without
71 error. See IEEE Std 1003.1, section 2.3 Error Numbers, and section 7.5:3 of
74 The condition wrapping the builtin call is conservatively set to avoid too
75 aggressive (wrong) shrink wrapping. The optimization is called conditional
76 dead call elimination because the call is eliminated under the condition
77 that the input arguments would not lead to domain or range error (for
78 instance when x <= 0 for a log (x) call), however the chances that the error
79 condition is hit is very low (those builtin calls which are conditionally
80 dead are usually part of the C++ abstraction penalty exposed after
84 /* A structure for representing input domain of
85 a function argument in integer. If the lower
86 bound is -inf, has_lb is set to false. If the
87 upper bound is +inf, has_ub is false.
88 is_lb_inclusive and is_ub_inclusive are flags
89 to indicate if lb and ub value are inclusive
92 typedef struct input_domain
102 static VEC (tree, heap) *cond_dead_built_in_calls;
104 /* A helper function to construct and return an input
105 domain object. LB is the lower bound, HAS_LB is
106 a boolean flag indicating if the lower bound exists,
107 and LB_INCLUSIVE is a boolean flag indicating if the
108 lower bound is inclusive or not. UB, HAS_UB, and
109 UB_INCLUSIVE have the same meaning, but for upper
110 bound of the domain. */
113 get_domain (int lb, bool has_lb, bool lb_inclusive,
114 int ub, bool has_ub, bool ub_inclusive)
118 domain.has_lb = has_lb;
119 domain.is_lb_inclusive = lb_inclusive;
121 domain.has_ub = has_ub;
122 domain.is_ub_inclusive = ub_inclusive;
126 /* A helper function to check the target format for the
127 argument type. In this implementation, only IEEE formats
128 are supported. ARG is the call argument to be checked.
129 Returns true if the format is supported. To support other
130 target formats, function get_no_error_domain needs to be
131 enhanced to have range bounds properly computed. Since
132 the check is cheap (very small number of candidates
133 to be checked), the result is not cached for each float type. */
136 check_target_format (tree arg)
139 enum machine_mode mode;
140 const struct real_format *rfmt;
142 type = TREE_TYPE (arg);
143 mode = TYPE_MODE (type);
144 rfmt = REAL_MODE_FORMAT (mode);
146 && (rfmt == &ieee_single_format || rfmt == &mips_single_format))
148 && (rfmt == &ieee_double_format || rfmt == &mips_double_format))
149 /* For long double, we can not really check XFmode
150 which is only defined on intel platforms.
151 Candidate pre-selection using builtin function
152 code guarantees that we are checking formats
153 for long double modes: double, quad, and extended. */
154 || (mode != SFmode && mode != DFmode
155 && (rfmt == &ieee_quad_format
156 || rfmt == &mips_quad_format
157 || rfmt == &ieee_extended_intel_96_format
158 || rfmt == &ieee_extended_intel_128_format
159 || rfmt == &ieee_extended_intel_96_round_53_format)))
166 /* A helper function to help select calls to pow that are suitable for
167 conditional DCE transformation. It looks for pow calls that can be
168 guided with simple conditions. Such calls either have constant base
169 values or base values converted from integers. Returns true if
170 the pow call POW_CALL is a candidate. */
172 /* The maximum integer bit size for base argument of a pow call
173 that is suitable for shrink-wrapping transformation. */
174 #define MAX_BASE_INT_BIT_SIZE 32
177 check_pow (tree pow_call)
180 enum tree_code bc, ec;
182 if (call_expr_nargs (pow_call) != 2)
185 base = CALL_EXPR_ARG (pow_call, 0);
186 expn = CALL_EXPR_ARG (pow_call, 1);
188 if (!check_target_format (expn))
191 bc = TREE_CODE (base);
192 ec = TREE_CODE (expn);
194 /* Folding candidates are not interesting.
195 Can actually assert that it is already folded. */
196 if (ec == REAL_CST && bc == REAL_CST)
201 /* Only handle a fixed range of constant. */
203 REAL_VALUE_TYPE bcv = TREE_REAL_CST (base);
204 if (REAL_VALUES_EQUAL (bcv, dconst1))
206 if (REAL_VALUES_LESS (bcv, dconst1))
208 real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, 0, 1);
209 if (REAL_VALUES_LESS (mv, bcv))
213 else if (bc == SSA_NAME)
215 tree base_def, base_val, base_val0, base_var, type;
218 /* Only handles cases where base value is converted
219 from integer values. */
220 base_def = SSA_NAME_DEF_STMT (base);
221 if (TREE_CODE (base_def) != GIMPLE_MODIFY_STMT)
224 base_val = GIMPLE_STMT_OPERAND (base_def, 1);
226 if (TREE_CODE (base_val) != FLOAT_EXPR)
228 base_val0 = TREE_OPERAND (base_val, 0);
230 base_var = SSA_NAME_VAR (base_val0);
231 if (!DECL_P (base_var))
234 type = TREE_TYPE (base_var);
235 if (TREE_CODE (type) != INTEGER_TYPE)
237 bit_sz = TYPE_PRECISION (type);
238 /* If the type of the base is too wide,
239 the resulting shrink wrapping condition
240 will be too conservative. */
241 if (bit_sz > MAX_BASE_INT_BIT_SIZE)
250 /* A helper function to help select candidate function calls that are
251 suitable for conditional DCE. Candidate functions must have single
252 valid input domain in this implementation except for pow (see check_pow).
253 Returns true if the function call is a candidate. */
256 check_builtin_call (tree bcall)
260 arg = CALL_EXPR_ARG (bcall, 0);
261 return check_target_format (arg);
264 /* A helper function to determine if a builtin function call is a
265 candidate for conditional DCE. Returns true if the builtin call
269 is_call_dce_candidate (tree call)
272 enum built_in_function fnc;
274 if (!flag_tree_builtin_call_dce)
277 gcc_assert (call && TREE_CODE (call) == CALL_EXPR);
279 fn = get_callee_fndecl (call);
280 if (!fn || !DECL_BUILT_IN (fn)
281 || (DECL_BUILT_IN_CLASS (fn) != BUILT_IN_NORMAL))
284 fnc = DECL_FUNCTION_CODE (fn);
287 /* Trig functions. */
288 CASE_FLT_FN (BUILT_IN_ACOS):
289 CASE_FLT_FN (BUILT_IN_ASIN):
290 /* Hyperbolic functions. */
291 CASE_FLT_FN (BUILT_IN_ACOSH):
292 CASE_FLT_FN (BUILT_IN_ATANH):
293 CASE_FLT_FN (BUILT_IN_COSH):
294 CASE_FLT_FN (BUILT_IN_SINH):
296 CASE_FLT_FN (BUILT_IN_LOG):
297 CASE_FLT_FN (BUILT_IN_LOG2):
298 CASE_FLT_FN (BUILT_IN_LOG10):
299 CASE_FLT_FN (BUILT_IN_LOG1P):
301 CASE_FLT_FN (BUILT_IN_EXP):
302 CASE_FLT_FN (BUILT_IN_EXP2):
303 CASE_FLT_FN (BUILT_IN_EXP10):
304 CASE_FLT_FN (BUILT_IN_EXPM1):
305 CASE_FLT_FN (BUILT_IN_POW10):
307 CASE_FLT_FN (BUILT_IN_SQRT):
308 return check_builtin_call (call);
309 /* Special one: two argument pow. */
311 return check_pow (call);
320 /* A helper function to generate gimple statements for
321 one bound comparison. ARG is the call argument to
322 be compared with the bound, LBUB is the bound value
323 in integer, TCODE is the tree_code of the comparison,
324 TEMP_NAME1/TEMP_NAME2 are names of the temporaries,
325 CONDS is a vector holding the produced GIMPLE statements,
326 and NCONDS points to the variable holding the number
327 of logical comparisons. CONDS is either empty or
328 a list ended with a null tree. */
331 gen_one_condition (tree arg, int lbub,
332 enum tree_code tcode,
333 const char *temp_name1,
334 const char *temp_name2,
335 VEC (tree, heap) *conds,
338 tree lbub_real_cst, lbub_cst, float_type;
339 tree temp, tempn, tempc, tempcn;
340 tree stmt1, stmt2, stmt3;
342 float_type = TREE_TYPE (arg);
343 lbub_cst = build_int_cst (integer_type_node, lbub);
344 lbub_real_cst = build_real_from_int_cst (float_type, lbub_cst);
346 temp = create_tmp_var (float_type, temp_name1);
347 stmt1 = build_gimple_modify_stmt (temp, arg);
348 tempn = make_ssa_name (temp, stmt1);
349 GIMPLE_STMT_OPERAND (stmt1, 0) = tempn;
351 tempc = create_tmp_var (boolean_type_node, temp_name2);
352 stmt2 = build_gimple_modify_stmt (tempc,
355 tempn, lbub_real_cst));
356 tempcn = make_ssa_name (tempc, stmt2);
357 GIMPLE_STMT_OPERAND (stmt2, 0) = tempcn;
359 /* fold_built3 not used for gimple statement here,
360 as it will hit assertion. */
361 stmt3 = build3 (COND_EXPR, void_type_node,
362 tempcn, NULL_TREE, NULL_TREE);
363 VEC_quick_push (tree, conds, stmt1);
364 VEC_quick_push (tree, conds, stmt2);
365 VEC_quick_push (tree, conds, stmt3);
369 /* A helper function to generate GIMPLE statements for
370 out of input domain check. ARG is the call argument
371 to be runtime checked, DOMAIN holds the valid domain
372 for the given function, CONDS points to the vector
373 holding the result GIMPLE statements. *NCONDS is
374 the number of logical comparisons. This function
375 produces no more than two logical comparisons, one
376 for lower bound check, one for upper bound check. */
379 gen_conditions_for_domain (tree arg, inp_domain domain,
380 VEC (tree, heap) *conds,
384 gen_one_condition (arg, domain.lb,
385 (domain.is_lb_inclusive
386 ? LT_EXPR : LE_EXPR),
387 "DCE_COND_LB", "DCE_COND_LB_TEST",
392 /* Now push a separator. */
394 VEC_quick_push (tree, conds, NULL);
396 gen_one_condition (arg, domain.ub,
397 (domain.is_ub_inclusive
398 ? GT_EXPR : GE_EXPR),
399 "DCE_COND_UB", "DCE_COND_UB_TEST",
405 /* A helper function to generate condition
406 code for the y argument in call pow (some_const, y).
407 See candidate selection in check_pow. Since the
408 candidates' base values have a limited range,
409 the guarded code generated for y are simple:
412 Note max_y can be computed separately for each
413 const base, but in this implementation, we
414 choose to compute it using the max base
415 in the allowed range for the purpose of
416 simplicity. BASE is the constant base value,
417 EXPN is the expression for the exponent argument,
418 *CONDS is the vector to hold resulting statements,
419 and *NCONDS is the number of logical conditions. */
422 gen_conditions_for_pow_cst_base (tree base, tree expn,
423 VEC (tree, heap) *conds,
426 inp_domain exp_domain;
427 /* Validate the range of the base constant to make
428 sure it is consistent with check_pow. */
430 REAL_VALUE_TYPE bcv = TREE_REAL_CST (base);
431 gcc_assert (!REAL_VALUES_EQUAL (bcv, dconst1)
432 && !REAL_VALUES_LESS (bcv, dconst1));
433 real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, 0, 1);
434 gcc_assert (!REAL_VALUES_LESS (mv, bcv));
436 exp_domain = get_domain (0, false, false,
439 gen_conditions_for_domain (expn, exp_domain,
443 /* Generate error condition code for pow calls with
444 non constant base values. The candidates selected
445 have their base argument value converted from
446 integer (see check_pow) value (1, 2, 4 bytes), and
447 the max exp value is computed based on the size
448 of the integer type (i.e. max possible base value).
449 The resulting input domain for exp argument is thus
450 conservative (smaller than the max value allowed by
451 the runtime value of the base). BASE is the integer
452 base value, EXPN is the expression for the exponent
453 argument, *CONDS is the vector to hold resulting
454 statements, and *NCONDS is the number of logical
458 gen_conditions_for_pow_int_base (tree base, tree expn,
459 VEC (tree, heap) *conds,
462 tree base_def, base_nm, base_val, base_val0;
463 tree base_var, int_type;
465 tree cst0, stmt1, stmt2;
467 inp_domain exp_domain;
469 base_def = SSA_NAME_DEF_STMT (base);
470 base_nm = GIMPLE_STMT_OPERAND (base_def, 0);
471 base_val = GIMPLE_STMT_OPERAND (base_def, 1);
472 base_val0 = TREE_OPERAND (base_val, 0);
473 base_var = SSA_NAME_VAR (base_val0);
474 int_type = TREE_TYPE (base_var);
475 bit_sz = TYPE_PRECISION (int_type);
476 gcc_assert (bit_sz > 0
477 && bit_sz <= MAX_BASE_INT_BIT_SIZE);
479 /* Determine the max exp argument value according to
480 the size of the base integer. The max exp value
481 is conservatively estimated assuming IEEE754 double
485 else if (bit_sz == 16)
489 gcc_assert (bit_sz == MAX_BASE_INT_BIT_SIZE);
493 /* For pow ((double)x, y), generate the following conditions:
500 if (temp2 > max_exp_real_cst) */
502 /* Generate condition in reverse order -- first
503 the condition for the exp argument. */
505 exp_domain = get_domain (0, false, false,
506 max_exp, true, true);
508 gen_conditions_for_domain (expn, exp_domain,
511 /* Now generate condition for the base argument.
512 Note it does not use the helper function
513 gen_conditions_for_domain because the base
516 /* Push a separator. */
517 VEC_quick_push (tree, conds, NULL);
519 temp = create_tmp_var (int_type, "DCE_COND1");
520 cst0 = build_int_cst (int_type, 0);
521 stmt1 = build_gimple_modify_stmt (temp, base_val0);
522 tempn = make_ssa_name (temp, stmt1);
523 GIMPLE_STMT_OPERAND (stmt1, 0) = tempn;
524 stmt2 = build3 (COND_EXPR, void_type_node,
525 fold_build2 (LE_EXPR, boolean_type_node, tempn, cst0),
526 NULL_TREE, NULL_TREE);
528 VEC_quick_push (tree, conds, stmt1);
529 VEC_quick_push (tree, conds, stmt2);
533 /* Method to generate conditional statements for guarding conditionally
534 dead calls to pow. One or more statements can be generated for
535 each logical condition. Statement groups of different conditions
536 are separated by a NULL tree and they are stored in the VEC
537 conds. The number of logical conditions are stored in *nconds.
539 See C99 standard, 7.12.7.4:2, for description of pow (x, y).
540 The precise condition for domain errors are complex. In this
541 implementation, a simplified (but conservative) valid domain
542 for x and y are used: x is positive to avoid dom errors, while
543 y is smaller than a upper bound (depending on x) to avoid range
544 errors. Runtime code is generated to check x (if not constant)
545 and y against the valid domain. If it is out, jump to the call,
546 otherwise the call is bypassed. POW_CALL is the call statement,
547 *CONDS is a vector holding the resulting condition statements,
548 and *NCONDS is the number of logical conditions. */
551 gen_conditions_for_pow (tree pow_call, VEC (tree, heap) *conds,
555 enum tree_code bc, ec;
557 #ifdef ENABLE_CHECKING
558 gcc_assert (check_pow (pow_call));
563 base = CALL_EXPR_ARG (pow_call, 0);
564 expn = CALL_EXPR_ARG (pow_call, 1);
566 bc = TREE_CODE (base);
567 ec = TREE_CODE (expn);
570 gen_conditions_for_pow_cst_base (base, expn,
572 else if (bc == SSA_NAME)
573 gen_conditions_for_pow_int_base (base, expn,
579 /* A helper routine to help computing the valid input domain
580 for a builtin function. See C99 7.12.7 for details. In this
581 implementation, we only handle single region domain. The
582 resulting region can be conservative (smaller) than the actual
583 one and rounded to integers. Some of the bounds are documented
584 in the standard, while other limit constants are computed
585 assuming IEEE floating point format (for SF and DF modes).
586 Since IEEE only sets minimum requirements for long double format,
587 different long double formats exist under different implementations
588 (e.g, 64 bit double precision (DF), 80 bit double-extended
589 precision (XF), and 128 bit quad precision (QF) ). For simplicity,
590 in this implementation, the computed bounds for long double assume
591 64 bit format (DF), and are therefore conservative. Another
592 assumption is that single precision float type is always SF mode,
593 and double type is DF mode. This function is quite
594 implementation specific, so it may not be suitable to be part of
595 builtins.c. This needs to be revisited later to see if it can
596 be leveraged in x87 assembly expansion. */
599 get_no_error_domain (enum built_in_function fnc)
603 /* Trig functions: return [-1, +1] */
604 CASE_FLT_FN (BUILT_IN_ACOS):
605 CASE_FLT_FN (BUILT_IN_ASIN):
606 return get_domain (-1, true, true,
608 /* Hyperbolic functions. */
609 CASE_FLT_FN (BUILT_IN_ACOSH):
610 /* acosh: [1, +inf) */
611 return get_domain (1, true, true,
613 CASE_FLT_FN (BUILT_IN_ATANH):
614 /* atanh: (-1, +1) */
615 return get_domain (-1, true, false,
619 /* coshf: (-89, +89) */
620 return get_domain (-89, true, false,
626 /* cosh: (-710, +710) */
627 return get_domain (-710, true, false,
629 /* Log functions: (0, +inf) */
630 CASE_FLT_FN (BUILT_IN_LOG):
631 CASE_FLT_FN (BUILT_IN_LOG2):
632 CASE_FLT_FN (BUILT_IN_LOG10):
633 return get_domain (0, true, false,
635 CASE_FLT_FN (BUILT_IN_LOG1P):
636 return get_domain (-1, true, false,
640 case BUILT_IN_EXPM1F:
641 /* expf: (-inf, 88) */
642 return get_domain (-1, false, false,
647 case BUILT_IN_EXPM1L:
648 /* exp: (-inf, 709) */
649 return get_domain (-1, false, false,
652 /* exp2f: (-inf, 128) */
653 return get_domain (-1, false, false,
657 /* exp2: (-inf, 1024) */
658 return get_domain (-1, false, false,
660 case BUILT_IN_EXP10F:
661 case BUILT_IN_POW10F:
662 /* exp10f: (-inf, 38) */
663 return get_domain (-1, false, false,
667 case BUILT_IN_EXP10L:
668 case BUILT_IN_POW10L:
669 /* exp10: (-inf, 308) */
670 return get_domain (-1, false, false,
672 /* sqrt: [0, +inf) */
673 CASE_FLT_FN (BUILT_IN_SQRT):
674 return get_domain (0, true, true,
683 /* The function to generate shrink wrap conditions for a partially
684 dead builtin call whose return value is not used anywhere,
685 but has to be kept live due to potential error condition.
686 BI_CALL is the builtin call, CONDS is the vector of statements
687 for condition code, NCODES is the pointer to the number of
688 logical conditions. Statements belonging to different logical
689 condition are separated by NULL tree in the vector. */
692 gen_shrink_wrap_conditions (tree bi_call, VEC (tree, heap) *conds,
693 unsigned int *nconds)
696 enum built_in_function fnc;
698 gcc_assert (nconds && conds);
699 gcc_assert (VEC_length (tree, conds) == 0);
700 gcc_assert (TREE_CODE (bi_call) == GIMPLE_MODIFY_STMT
701 || TREE_CODE (bi_call) == CALL_EXPR);
704 if (TREE_CODE (call) == GIMPLE_MODIFY_STMT)
705 call = get_call_expr_in (bi_call);
707 fn = get_callee_fndecl (call);
708 gcc_assert (fn && DECL_BUILT_IN (fn));
709 fnc = DECL_FUNCTION_CODE (fn);
712 if (fnc == BUILT_IN_POW)
713 gen_conditions_for_pow (call, conds, nconds);
717 inp_domain domain = get_no_error_domain (fnc);
719 arg = CALL_EXPR_ARG (bi_call, 0);
720 gen_conditions_for_domain (arg, domain, conds, nconds);
727 /* Probability of the branch (to the call) is taken. */
728 #define ERR_PROB 0.01
730 /* The function to shrink wrap a partially dead builtin call
731 whose return value is not used anywhere, but has to be kept
732 live due to potential error condition. Returns true if the
733 transformation actually happens. */
736 shrink_wrap_one_built_in_call (tree bi_call)
738 block_stmt_iterator bi_call_bsi;
739 basic_block bi_call_bb, join_tgt_bb, guard_bb, guard_bb0;
740 edge join_tgt_in_edge_from_call, join_tgt_in_edge_fall_thru;
741 edge bi_call_in_edge0, guard_bb_in_edge;
742 VEC (tree, heap) *conds;
743 unsigned tn_cond_stmts, nconds;
745 tree cond_expr = NULL;
746 tree cond_expr_start;
747 tree bi_call_label_decl;
750 conds = VEC_alloc (tree, heap, 12);
751 gen_shrink_wrap_conditions (bi_call, conds, &nconds);
753 /* This can happen if the condition generator decides
754 it is not beneficial to do the transformation. Just
755 return false and do not do any transformation for
760 bi_call_bb = bb_for_stmt (bi_call);
762 /* Now find the join target bb -- split
763 bi_call_bb if needed. */
764 bi_call_bsi = bsi_for_stmt (bi_call);
766 join_tgt_in_edge_from_call = split_block (bi_call_bb, bi_call);
767 bi_call_bsi = bsi_for_stmt (bi_call);
769 join_tgt_bb = join_tgt_in_edge_from_call->dest;
771 /* Now it is time to insert the first conditional expression
772 into bi_call_bb and split this bb so that bi_call is
774 tn_cond_stmts = VEC_length (tree, conds);
776 cond_expr_start = VEC_index (tree, conds, 0);
777 for (ci = 0; ci < tn_cond_stmts; ci++)
779 tree c = VEC_index (tree, conds, ci);
780 gcc_assert (c || ci != 0);
783 bsi_insert_before (&bi_call_bsi, c, BSI_SAME_STMT);
788 gcc_assert (cond_expr && TREE_CODE (cond_expr) == COND_EXPR);
791 bi_call_label_decl = create_artificial_label ();
792 bi_call_label = build1 (LABEL_EXPR, void_type_node, bi_call_label_decl);
793 bsi_insert_before (&bi_call_bsi, bi_call_label, BSI_SAME_STMT);
795 bi_call_in_edge0 = split_block (bi_call_bb, cond_expr);
796 bi_call_in_edge0->flags &= ~EDGE_FALLTHRU;
797 bi_call_in_edge0->flags |= EDGE_TRUE_VALUE;
798 guard_bb0 = bi_call_bb;
799 bi_call_bb = bi_call_in_edge0->dest;
800 join_tgt_in_edge_fall_thru = make_edge (guard_bb0, join_tgt_bb,
803 bi_call_in_edge0->probability = REG_BR_PROB_BASE * ERR_PROB;
804 join_tgt_in_edge_fall_thru->probability =
805 REG_BR_PROB_BASE - bi_call_in_edge0->probability;
807 /* Code generation for the rest of the conditions */
808 guard_bb = guard_bb0;
812 edge bi_call_in_edge;
813 block_stmt_iterator guard_bsi = bsi_for_stmt (cond_expr_start);
815 cond_expr_start = VEC_index (tree, conds, ci0);
816 for (; ci < tn_cond_stmts; ci++)
818 tree c = VEC_index (tree, conds, ci);
819 gcc_assert (c || ci != ci0);
822 bsi_insert_before (&guard_bsi, c, BSI_SAME_STMT);
827 gcc_assert (cond_expr && TREE_CODE (cond_expr) == COND_EXPR);
828 guard_bb_in_edge = split_block (guard_bb, cond_expr);
829 guard_bb_in_edge->flags &= ~EDGE_FALLTHRU;
830 guard_bb_in_edge->flags |= EDGE_FALSE_VALUE;
832 bi_call_in_edge = make_edge (guard_bb, bi_call_bb, EDGE_TRUE_VALUE);
834 bi_call_in_edge->probability = REG_BR_PROB_BASE * ERR_PROB;
835 guard_bb_in_edge->probability =
836 REG_BR_PROB_BASE - bi_call_in_edge->probability;
839 VEC_free (tree, heap, conds);
840 if (dump_file && (dump_flags & TDF_DETAILS))
843 loc = EXPR_LOCATION (bi_call);
845 "%s:%d: note: function call is shrink-wrapped"
846 " into error conditions.\n",
847 LOCATION_FILE (loc), LOCATION_LINE (loc));
853 /* The top level function for conditional dead code shrink
854 wrapping transformation. */
857 shrink_wrap_conditional_dead_built_in_calls (void)
859 bool changed = false;
862 unsigned n = VEC_length (tree, cond_dead_built_in_calls);
868 tree bi_call = VEC_index (tree, cond_dead_built_in_calls, i);
869 changed |= shrink_wrap_one_built_in_call (bi_call);
875 /* Pass entry points. */
878 tree_call_cdce (void)
881 block_stmt_iterator i;
882 bool something_changed = false;
883 cond_dead_built_in_calls = VEC_alloc (tree, heap, 64);
887 /* Collect dead call candidates. */
888 for (i = bsi_start (bb); ! bsi_end_p (i); bsi_next (&i))
890 tree stmt = bsi_stmt (i);
891 if (TREE_CODE (stmt) == CALL_EXPR
892 && is_call_dce_candidate (stmt))
894 if (dump_file && (dump_flags & TDF_DETAILS))
896 fprintf (dump_file, "Found conditional dead call: ");
897 print_generic_stmt (dump_file, stmt, TDF_SLIM);
898 fprintf (dump_file, "\n");
900 VEC_quick_push (tree, cond_dead_built_in_calls, stmt);
906 shrink_wrap_conditional_dead_built_in_calls ();
908 VEC_free (tree, heap, cond_dead_built_in_calls);
910 if (something_changed)
912 free_dominance_info (CDI_DOMINATORS);
913 free_dominance_info (CDI_POST_DOMINATORS);
914 return (TODO_update_ssa | TODO_cleanup_cfg | TODO_ggc_collect
915 | TODO_remove_unused_locals);
922 gate_call_cdce (void)
924 /* The limit constants used in the implementation
925 assume IEEE floating point format. Other formats
926 can be supported in the future if needed. */
927 return flag_tree_builtin_call_dce != 0;
930 struct gimple_opt_pass pass_call_cdce =
935 gate_call_cdce, /* gate */
936 tree_call_cdce, /* execute */
939 0, /* static_pass_number */
940 TV_TREE_CALL_CDCE, /* tv_id */
941 PROP_cfg | PROP_ssa, /* properties_required */
942 0, /* properties_provided */
943 0, /* properties_destroyed */
944 0, /* todo_flags_start */
945 TODO_dump_func | TODO_verify_ssa /* todo_flags_finish */