PR c++/53989
[official-gcc.git] / gcc / tree-call-cdce.c
blob3ad241cf29ddd47ad8384e8e7a98a2bfb858c96e
1 /* Conditional Dead Call Elimination pass for the GNU compiler.
2 Copyright (C) 2008, 2009, 2010
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
11 later version.
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
16 for more details.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "basic-block.h"
27 #include "tree.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-flow.h"
30 #include "gimple.h"
31 #include "tree-pass.h"
32 #include "flags.h"
35 /* Conditional dead call elimination
37 Some builtin functions can set errno on error conditions, but they
38 are otherwise pure. If the result of a call to such a function is
39 not used, the compiler can still not eliminate the call without
40 powerful interprocedural analysis to prove that the errno is not
41 checked. However, if the conditions under which the error occurs
42 are known, the compiler can conditionally dead code eliminate the
43 calls by shrink-wrapping the semi-dead calls into the error condition:
45 built_in_call (args)
46 ==>
47 if (error_cond (args))
48 built_in_call (args)
50 An actual simple example is :
51 log (x); // Mostly dead call
52 ==>
53 if (x < 0)
54 log (x);
55 With this change, call to log (x) is effectively eliminated, as
56 in majority of the cases, log won't be called with x out of
57 range. The branch is totally predictable, so the branch cost
58 is low.
60 Note that library functions are not supposed to clear errno to zero without
61 error. See IEEE Std 1003.1, section 2.3 Error Numbers, and section 7.5:3 of
62 ISO/IEC 9899 (C99).
64 The condition wrapping the builtin call is conservatively set to avoid too
65 aggressive (wrong) shrink wrapping. The optimization is called conditional
66 dead call elimination because the call is eliminated under the condition
67 that the input arguments would not lead to domain or range error (for
68 instance when x <= 0 for a log (x) call), however the chances that the error
69 condition is hit is very low (those builtin calls which are conditionally
70 dead are usually part of the C++ abstraction penalty exposed after
71 inlining). */
74 /* A structure for representing input domain of
75 a function argument in integer. If the lower
76 bound is -inf, has_lb is set to false. If the
77 upper bound is +inf, has_ub is false.
78 is_lb_inclusive and is_ub_inclusive are flags
79 to indicate if lb and ub value are inclusive
80 respectively. */
82 typedef struct input_domain
84 int lb;
85 int ub;
86 bool has_lb;
87 bool has_ub;
88 bool is_lb_inclusive;
89 bool is_ub_inclusive;
90 } inp_domain;
92 /* A helper function to construct and return an input
93 domain object. LB is the lower bound, HAS_LB is
94 a boolean flag indicating if the lower bound exists,
95 and LB_INCLUSIVE is a boolean flag indicating if the
96 lower bound is inclusive or not. UB, HAS_UB, and
97 UB_INCLUSIVE have the same meaning, but for upper
98 bound of the domain. */
100 static inp_domain
101 get_domain (int lb, bool has_lb, bool lb_inclusive,
102 int ub, bool has_ub, bool ub_inclusive)
104 inp_domain domain;
105 domain.lb = lb;
106 domain.has_lb = has_lb;
107 domain.is_lb_inclusive = lb_inclusive;
108 domain.ub = ub;
109 domain.has_ub = has_ub;
110 domain.is_ub_inclusive = ub_inclusive;
111 return domain;
114 /* A helper function to check the target format for the
115 argument type. In this implementation, only IEEE formats
116 are supported. ARG is the call argument to be checked.
117 Returns true if the format is supported. To support other
118 target formats, function get_no_error_domain needs to be
119 enhanced to have range bounds properly computed. Since
120 the check is cheap (very small number of candidates
121 to be checked), the result is not cached for each float type. */
123 static bool
124 check_target_format (tree arg)
126 tree type;
127 enum machine_mode mode;
128 const struct real_format *rfmt;
130 type = TREE_TYPE (arg);
131 mode = TYPE_MODE (type);
132 rfmt = REAL_MODE_FORMAT (mode);
133 if ((mode == SFmode
134 && (rfmt == &ieee_single_format || rfmt == &mips_single_format
135 || rfmt == &motorola_single_format))
136 || (mode == DFmode
137 && (rfmt == &ieee_double_format || rfmt == &mips_double_format
138 || rfmt == &motorola_double_format))
139 /* For long double, we can not really check XFmode
140 which is only defined on intel platforms.
141 Candidate pre-selection using builtin function
142 code guarantees that we are checking formats
143 for long double modes: double, quad, and extended. */
144 || (mode != SFmode && mode != DFmode
145 && (rfmt == &ieee_quad_format
146 || rfmt == &mips_quad_format
147 || rfmt == &ieee_extended_motorola_format
148 || rfmt == &ieee_extended_intel_96_format
149 || rfmt == &ieee_extended_intel_128_format
150 || rfmt == &ieee_extended_intel_96_round_53_format)))
151 return true;
153 return false;
157 /* A helper function to help select calls to pow that are suitable for
158 conditional DCE transformation. It looks for pow calls that can be
159 guided with simple conditions. Such calls either have constant base
160 values or base values converted from integers. Returns true if
161 the pow call POW_CALL is a candidate. */
163 /* The maximum integer bit size for base argument of a pow call
164 that is suitable for shrink-wrapping transformation. */
165 #define MAX_BASE_INT_BIT_SIZE 32
167 static bool
168 check_pow (gimple pow_call)
170 tree base, expn;
171 enum tree_code bc, ec;
173 if (gimple_call_num_args (pow_call) != 2)
174 return false;
176 base = gimple_call_arg (pow_call, 0);
177 expn = gimple_call_arg (pow_call, 1);
179 if (!check_target_format (expn))
180 return false;
182 bc = TREE_CODE (base);
183 ec = TREE_CODE (expn);
185 /* Folding candidates are not interesting.
186 Can actually assert that it is already folded. */
187 if (ec == REAL_CST && bc == REAL_CST)
188 return false;
190 if (bc == REAL_CST)
192 /* Only handle a fixed range of constant. */
193 REAL_VALUE_TYPE mv;
194 REAL_VALUE_TYPE bcv = TREE_REAL_CST (base);
195 if (REAL_VALUES_EQUAL (bcv, dconst1))
196 return false;
197 if (REAL_VALUES_LESS (bcv, dconst1))
198 return false;
199 real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, 0, 1);
200 if (REAL_VALUES_LESS (mv, bcv))
201 return false;
202 return true;
204 else if (bc == SSA_NAME)
206 tree base_val0, base_var, type;
207 gimple base_def;
208 int bit_sz;
210 /* Only handles cases where base value is converted
211 from integer values. */
212 base_def = SSA_NAME_DEF_STMT (base);
213 if (gimple_code (base_def) != GIMPLE_ASSIGN)
214 return false;
216 if (gimple_assign_rhs_code (base_def) != FLOAT_EXPR)
217 return false;
218 base_val0 = gimple_assign_rhs1 (base_def);
220 base_var = SSA_NAME_VAR (base_val0);
221 if (!DECL_P (base_var))
222 return false;
224 type = TREE_TYPE (base_var);
225 if (TREE_CODE (type) != INTEGER_TYPE)
226 return false;
227 bit_sz = TYPE_PRECISION (type);
228 /* If the type of the base is too wide,
229 the resulting shrink wrapping condition
230 will be too conservative. */
231 if (bit_sz > MAX_BASE_INT_BIT_SIZE)
232 return false;
234 return true;
236 else
237 return false;
240 /* A helper function to help select candidate function calls that are
241 suitable for conditional DCE. Candidate functions must have single
242 valid input domain in this implementation except for pow (see check_pow).
243 Returns true if the function call is a candidate. */
245 static bool
246 check_builtin_call (gimple bcall)
248 tree arg;
250 arg = gimple_call_arg (bcall, 0);
251 return check_target_format (arg);
254 /* A helper function to determine if a builtin function call is a
255 candidate for conditional DCE. Returns true if the builtin call
256 is a candidate. */
258 static bool
259 is_call_dce_candidate (gimple call)
261 tree fn;
262 enum built_in_function fnc;
264 /* Only potentially dead calls are considered. */
265 if (gimple_call_lhs (call))
266 return false;
268 fn = gimple_call_fndecl (call);
269 if (!fn
270 || !DECL_BUILT_IN (fn)
271 || (DECL_BUILT_IN_CLASS (fn) != BUILT_IN_NORMAL))
272 return false;
274 fnc = DECL_FUNCTION_CODE (fn);
275 switch (fnc)
277 /* Trig functions. */
278 CASE_FLT_FN (BUILT_IN_ACOS):
279 CASE_FLT_FN (BUILT_IN_ASIN):
280 /* Hyperbolic functions. */
281 CASE_FLT_FN (BUILT_IN_ACOSH):
282 CASE_FLT_FN (BUILT_IN_ATANH):
283 CASE_FLT_FN (BUILT_IN_COSH):
284 CASE_FLT_FN (BUILT_IN_SINH):
285 /* Log functions. */
286 CASE_FLT_FN (BUILT_IN_LOG):
287 CASE_FLT_FN (BUILT_IN_LOG2):
288 CASE_FLT_FN (BUILT_IN_LOG10):
289 CASE_FLT_FN (BUILT_IN_LOG1P):
290 /* Exp functions. */
291 CASE_FLT_FN (BUILT_IN_EXP):
292 CASE_FLT_FN (BUILT_IN_EXP2):
293 CASE_FLT_FN (BUILT_IN_EXP10):
294 CASE_FLT_FN (BUILT_IN_EXPM1):
295 CASE_FLT_FN (BUILT_IN_POW10):
296 /* Sqrt. */
297 CASE_FLT_FN (BUILT_IN_SQRT):
298 return check_builtin_call (call);
299 /* Special one: two argument pow. */
300 case BUILT_IN_POW:
301 return check_pow (call);
302 default:
303 break;
306 return false;
310 /* A helper function to generate gimple statements for
311 one bound comparison. ARG is the call argument to
312 be compared with the bound, LBUB is the bound value
313 in integer, TCODE is the tree_code of the comparison,
314 TEMP_NAME1/TEMP_NAME2 are names of the temporaries,
315 CONDS is a vector holding the produced GIMPLE statements,
316 and NCONDS points to the variable holding the number
317 of logical comparisons. CONDS is either empty or
318 a list ended with a null tree. */
320 static void
321 gen_one_condition (tree arg, int lbub,
322 enum tree_code tcode,
323 const char *temp_name1,
324 const char *temp_name2,
325 VEC (gimple, heap) *conds,
326 unsigned *nconds)
328 tree lbub_real_cst, lbub_cst, float_type;
329 tree temp, tempn, tempc, tempcn;
330 gimple stmt1, stmt2, stmt3;
332 float_type = TREE_TYPE (arg);
333 lbub_cst = build_int_cst (integer_type_node, lbub);
334 lbub_real_cst = build_real_from_int_cst (float_type, lbub_cst);
336 temp = create_tmp_var (float_type, temp_name1);
337 stmt1 = gimple_build_assign (temp, arg);
338 tempn = make_ssa_name (temp, stmt1);
339 gimple_assign_set_lhs (stmt1, tempn);
341 tempc = create_tmp_var (boolean_type_node, temp_name2);
342 stmt2 = gimple_build_assign (tempc,
343 fold_build2 (tcode,
344 boolean_type_node,
345 tempn, lbub_real_cst));
346 tempcn = make_ssa_name (tempc, stmt2);
347 gimple_assign_set_lhs (stmt2, tempcn);
349 stmt3 = gimple_build_cond_from_tree (tempcn, NULL_TREE, NULL_TREE);
350 VEC_quick_push (gimple, conds, stmt1);
351 VEC_quick_push (gimple, conds, stmt2);
352 VEC_quick_push (gimple, conds, stmt3);
353 (*nconds)++;
356 /* A helper function to generate GIMPLE statements for
357 out of input domain check. ARG is the call argument
358 to be runtime checked, DOMAIN holds the valid domain
359 for the given function, CONDS points to the vector
360 holding the result GIMPLE statements. *NCONDS is
361 the number of logical comparisons. This function
362 produces no more than two logical comparisons, one
363 for lower bound check, one for upper bound check. */
365 static void
366 gen_conditions_for_domain (tree arg, inp_domain domain,
367 VEC (gimple, heap) *conds,
368 unsigned *nconds)
370 if (domain.has_lb)
371 gen_one_condition (arg, domain.lb,
372 (domain.is_lb_inclusive
373 ? LT_EXPR : LE_EXPR),
374 "DCE_COND_LB", "DCE_COND_LB_TEST",
375 conds, nconds);
377 if (domain.has_ub)
379 /* Now push a separator. */
380 if (domain.has_lb)
381 VEC_quick_push (gimple, conds, NULL);
383 gen_one_condition (arg, domain.ub,
384 (domain.is_ub_inclusive
385 ? GT_EXPR : GE_EXPR),
386 "DCE_COND_UB", "DCE_COND_UB_TEST",
387 conds, nconds);
392 /* A helper function to generate condition
393 code for the y argument in call pow (some_const, y).
394 See candidate selection in check_pow. Since the
395 candidates' base values have a limited range,
396 the guarded code generated for y are simple:
397 if (y > max_y)
398 pow (const, y);
399 Note max_y can be computed separately for each
400 const base, but in this implementation, we
401 choose to compute it using the max base
402 in the allowed range for the purpose of
403 simplicity. BASE is the constant base value,
404 EXPN is the expression for the exponent argument,
405 *CONDS is the vector to hold resulting statements,
406 and *NCONDS is the number of logical conditions. */
408 static void
409 gen_conditions_for_pow_cst_base (tree base, tree expn,
410 VEC (gimple, heap) *conds,
411 unsigned *nconds)
413 inp_domain exp_domain;
414 /* Validate the range of the base constant to make
415 sure it is consistent with check_pow. */
416 REAL_VALUE_TYPE mv;
417 REAL_VALUE_TYPE bcv = TREE_REAL_CST (base);
418 gcc_assert (!REAL_VALUES_EQUAL (bcv, dconst1)
419 && !REAL_VALUES_LESS (bcv, dconst1));
420 real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, 0, 1);
421 gcc_assert (!REAL_VALUES_LESS (mv, bcv));
423 exp_domain = get_domain (0, false, false,
424 127, true, false);
426 gen_conditions_for_domain (expn, exp_domain,
427 conds, nconds);
430 /* Generate error condition code for pow calls with
431 non constant base values. The candidates selected
432 have their base argument value converted from
433 integer (see check_pow) value (1, 2, 4 bytes), and
434 the max exp value is computed based on the size
435 of the integer type (i.e. max possible base value).
436 The resulting input domain for exp argument is thus
437 conservative (smaller than the max value allowed by
438 the runtime value of the base). BASE is the integer
439 base value, EXPN is the expression for the exponent
440 argument, *CONDS is the vector to hold resulting
441 statements, and *NCONDS is the number of logical
442 conditions. */
444 static void
445 gen_conditions_for_pow_int_base (tree base, tree expn,
446 VEC (gimple, heap) *conds,
447 unsigned *nconds)
449 gimple base_def;
450 tree base_val0;
451 tree base_var, int_type;
452 tree temp, tempn;
453 tree cst0;
454 gimple stmt1, stmt2;
455 int bit_sz, max_exp;
456 inp_domain exp_domain;
458 base_def = SSA_NAME_DEF_STMT (base);
459 base_val0 = gimple_assign_rhs1 (base_def);
460 base_var = SSA_NAME_VAR (base_val0);
461 int_type = TREE_TYPE (base_var);
462 bit_sz = TYPE_PRECISION (int_type);
463 gcc_assert (bit_sz > 0
464 && bit_sz <= MAX_BASE_INT_BIT_SIZE);
466 /* Determine the max exp argument value according to
467 the size of the base integer. The max exp value
468 is conservatively estimated assuming IEEE754 double
469 precision format. */
470 if (bit_sz == 8)
471 max_exp = 128;
472 else if (bit_sz == 16)
473 max_exp = 64;
474 else
476 gcc_assert (bit_sz == MAX_BASE_INT_BIT_SIZE);
477 max_exp = 32;
480 /* For pow ((double)x, y), generate the following conditions:
481 cond 1:
482 temp1 = x;
483 if (temp1 <= 0)
485 cond 2:
486 temp2 = y;
487 if (temp2 > max_exp_real_cst) */
489 /* Generate condition in reverse order -- first
490 the condition for the exp argument. */
492 exp_domain = get_domain (0, false, false,
493 max_exp, true, true);
495 gen_conditions_for_domain (expn, exp_domain,
496 conds, nconds);
498 /* Now generate condition for the base argument.
499 Note it does not use the helper function
500 gen_conditions_for_domain because the base
501 type is integer. */
503 /* Push a separator. */
504 VEC_quick_push (gimple, conds, NULL);
506 temp = create_tmp_var (int_type, "DCE_COND1");
507 cst0 = build_int_cst (int_type, 0);
508 stmt1 = gimple_build_assign (temp, base_val0);
509 tempn = make_ssa_name (temp, stmt1);
510 gimple_assign_set_lhs (stmt1, tempn);
511 stmt2 = gimple_build_cond (LE_EXPR, tempn, cst0, NULL_TREE, NULL_TREE);
513 VEC_quick_push (gimple, conds, stmt1);
514 VEC_quick_push (gimple, conds, stmt2);
515 (*nconds)++;
518 /* Method to generate conditional statements for guarding conditionally
519 dead calls to pow. One or more statements can be generated for
520 each logical condition. Statement groups of different conditions
521 are separated by a NULL tree and they are stored in the VEC
522 conds. The number of logical conditions are stored in *nconds.
524 See C99 standard, 7.12.7.4:2, for description of pow (x, y).
525 The precise condition for domain errors are complex. In this
526 implementation, a simplified (but conservative) valid domain
527 for x and y are used: x is positive to avoid dom errors, while
528 y is smaller than a upper bound (depending on x) to avoid range
529 errors. Runtime code is generated to check x (if not constant)
530 and y against the valid domain. If it is out, jump to the call,
531 otherwise the call is bypassed. POW_CALL is the call statement,
532 *CONDS is a vector holding the resulting condition statements,
533 and *NCONDS is the number of logical conditions. */
535 static void
536 gen_conditions_for_pow (gimple pow_call, VEC (gimple, heap) *conds,
537 unsigned *nconds)
539 tree base, expn;
540 enum tree_code bc;
542 gcc_checking_assert (check_pow (pow_call));
544 *nconds = 0;
546 base = gimple_call_arg (pow_call, 0);
547 expn = gimple_call_arg (pow_call, 1);
549 bc = TREE_CODE (base);
551 if (bc == REAL_CST)
552 gen_conditions_for_pow_cst_base (base, expn, conds, nconds);
553 else if (bc == SSA_NAME)
554 gen_conditions_for_pow_int_base (base, expn, conds, nconds);
555 else
556 gcc_unreachable ();
559 /* A helper routine to help computing the valid input domain
560 for a builtin function. See C99 7.12.7 for details. In this
561 implementation, we only handle single region domain. The
562 resulting region can be conservative (smaller) than the actual
563 one and rounded to integers. Some of the bounds are documented
564 in the standard, while other limit constants are computed
565 assuming IEEE floating point format (for SF and DF modes).
566 Since IEEE only sets minimum requirements for long double format,
567 different long double formats exist under different implementations
568 (e.g, 64 bit double precision (DF), 80 bit double-extended
569 precision (XF), and 128 bit quad precision (QF) ). For simplicity,
570 in this implementation, the computed bounds for long double assume
571 64 bit format (DF), and are therefore conservative. Another
572 assumption is that single precision float type is always SF mode,
573 and double type is DF mode. This function is quite
574 implementation specific, so it may not be suitable to be part of
575 builtins.c. This needs to be revisited later to see if it can
576 be leveraged in x87 assembly expansion. */
578 static inp_domain
579 get_no_error_domain (enum built_in_function fnc)
581 switch (fnc)
583 /* Trig functions: return [-1, +1] */
584 CASE_FLT_FN (BUILT_IN_ACOS):
585 CASE_FLT_FN (BUILT_IN_ASIN):
586 return get_domain (-1, true, true,
587 1, true, true);
588 /* Hyperbolic functions. */
589 CASE_FLT_FN (BUILT_IN_ACOSH):
590 /* acosh: [1, +inf) */
591 return get_domain (1, true, true,
592 1, false, false);
593 CASE_FLT_FN (BUILT_IN_ATANH):
594 /* atanh: (-1, +1) */
595 return get_domain (-1, true, false,
596 1, true, false);
597 case BUILT_IN_COSHF:
598 case BUILT_IN_SINHF:
599 /* coshf: (-89, +89) */
600 return get_domain (-89, true, false,
601 89, true, false);
602 case BUILT_IN_COSH:
603 case BUILT_IN_SINH:
604 case BUILT_IN_COSHL:
605 case BUILT_IN_SINHL:
606 /* cosh: (-710, +710) */
607 return get_domain (-710, true, false,
608 710, true, false);
609 /* Log functions: (0, +inf) */
610 CASE_FLT_FN (BUILT_IN_LOG):
611 CASE_FLT_FN (BUILT_IN_LOG2):
612 CASE_FLT_FN (BUILT_IN_LOG10):
613 return get_domain (0, true, false,
614 0, false, false);
615 CASE_FLT_FN (BUILT_IN_LOG1P):
616 return get_domain (-1, true, false,
617 0, false, false);
618 /* Exp functions. */
619 case BUILT_IN_EXPF:
620 case BUILT_IN_EXPM1F:
621 /* expf: (-inf, 88) */
622 return get_domain (-1, false, false,
623 88, true, false);
624 case BUILT_IN_EXP:
625 case BUILT_IN_EXPM1:
626 case BUILT_IN_EXPL:
627 case BUILT_IN_EXPM1L:
628 /* exp: (-inf, 709) */
629 return get_domain (-1, false, false,
630 709, true, false);
631 case BUILT_IN_EXP2F:
632 /* exp2f: (-inf, 128) */
633 return get_domain (-1, false, false,
634 128, true, false);
635 case BUILT_IN_EXP2:
636 case BUILT_IN_EXP2L:
637 /* exp2: (-inf, 1024) */
638 return get_domain (-1, false, false,
639 1024, true, false);
640 case BUILT_IN_EXP10F:
641 case BUILT_IN_POW10F:
642 /* exp10f: (-inf, 38) */
643 return get_domain (-1, false, false,
644 38, true, false);
645 case BUILT_IN_EXP10:
646 case BUILT_IN_POW10:
647 case BUILT_IN_EXP10L:
648 case BUILT_IN_POW10L:
649 /* exp10: (-inf, 308) */
650 return get_domain (-1, false, false,
651 308, true, false);
652 /* sqrt: [0, +inf) */
653 CASE_FLT_FN (BUILT_IN_SQRT):
654 return get_domain (0, true, true,
655 0, false, false);
656 default:
657 gcc_unreachable ();
660 gcc_unreachable ();
663 /* The function to generate shrink wrap conditions for a partially
664 dead builtin call whose return value is not used anywhere,
665 but has to be kept live due to potential error condition.
666 BI_CALL is the builtin call, CONDS is the vector of statements
667 for condition code, NCODES is the pointer to the number of
668 logical conditions. Statements belonging to different logical
669 condition are separated by NULL tree in the vector. */
671 static void
672 gen_shrink_wrap_conditions (gimple bi_call, VEC (gimple, heap) *conds,
673 unsigned int *nconds)
675 gimple call;
676 tree fn;
677 enum built_in_function fnc;
679 gcc_assert (nconds && conds);
680 gcc_assert (VEC_length (gimple, conds) == 0);
681 gcc_assert (is_gimple_call (bi_call));
683 call = bi_call;
684 fn = gimple_call_fndecl (call);
685 gcc_assert (fn && DECL_BUILT_IN (fn));
686 fnc = DECL_FUNCTION_CODE (fn);
687 *nconds = 0;
689 if (fnc == BUILT_IN_POW)
690 gen_conditions_for_pow (call, conds, nconds);
691 else
693 tree arg;
694 inp_domain domain = get_no_error_domain (fnc);
695 *nconds = 0;
696 arg = gimple_call_arg (bi_call, 0);
697 gen_conditions_for_domain (arg, domain, conds, nconds);
700 return;
704 /* Probability of the branch (to the call) is taken. */
705 #define ERR_PROB 0.01
707 /* The function to shrink wrap a partially dead builtin call
708 whose return value is not used anywhere, but has to be kept
709 live due to potential error condition. Returns true if the
710 transformation actually happens. */
712 static bool
713 shrink_wrap_one_built_in_call (gimple bi_call)
715 gimple_stmt_iterator bi_call_bsi;
716 basic_block bi_call_bb, join_tgt_bb, guard_bb, guard_bb0;
717 edge join_tgt_in_edge_from_call, join_tgt_in_edge_fall_thru;
718 edge bi_call_in_edge0, guard_bb_in_edge;
719 VEC (gimple, heap) *conds;
720 unsigned tn_cond_stmts, nconds;
721 unsigned ci;
722 gimple cond_expr = NULL;
723 gimple cond_expr_start;
724 tree bi_call_label_decl;
725 gimple bi_call_label;
727 conds = VEC_alloc (gimple, heap, 12);
728 gen_shrink_wrap_conditions (bi_call, conds, &nconds);
730 /* This can happen if the condition generator decides
731 it is not beneficial to do the transformation. Just
732 return false and do not do any transformation for
733 the call. */
734 if (nconds == 0)
735 return false;
737 bi_call_bb = gimple_bb (bi_call);
739 /* Now find the join target bb -- split
740 bi_call_bb if needed. */
741 bi_call_bsi = gsi_for_stmt (bi_call);
743 join_tgt_in_edge_from_call = split_block (bi_call_bb, bi_call);
744 bi_call_bsi = gsi_for_stmt (bi_call);
746 join_tgt_bb = join_tgt_in_edge_from_call->dest;
748 /* Now it is time to insert the first conditional expression
749 into bi_call_bb and split this bb so that bi_call is
750 shrink-wrapped. */
751 tn_cond_stmts = VEC_length (gimple, conds);
752 cond_expr = NULL;
753 cond_expr_start = VEC_index (gimple, conds, 0);
754 for (ci = 0; ci < tn_cond_stmts; ci++)
756 gimple c = VEC_index (gimple, conds, ci);
757 gcc_assert (c || ci != 0);
758 if (!c)
759 break;
760 gsi_insert_before (&bi_call_bsi, c, GSI_SAME_STMT);
761 cond_expr = c;
763 nconds--;
764 ci++;
765 gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND);
767 /* Now the label. */
768 bi_call_label_decl = create_artificial_label (gimple_location (bi_call));
769 bi_call_label = gimple_build_label (bi_call_label_decl);
770 gsi_insert_before (&bi_call_bsi, bi_call_label, GSI_SAME_STMT);
772 bi_call_in_edge0 = split_block (bi_call_bb, cond_expr);
773 bi_call_in_edge0->flags &= ~EDGE_FALLTHRU;
774 bi_call_in_edge0->flags |= EDGE_TRUE_VALUE;
775 guard_bb0 = bi_call_bb;
776 bi_call_bb = bi_call_in_edge0->dest;
777 join_tgt_in_edge_fall_thru = make_edge (guard_bb0, join_tgt_bb,
778 EDGE_FALSE_VALUE);
780 bi_call_in_edge0->probability = REG_BR_PROB_BASE * ERR_PROB;
781 join_tgt_in_edge_fall_thru->probability =
782 REG_BR_PROB_BASE - bi_call_in_edge0->probability;
784 /* Code generation for the rest of the conditions */
785 guard_bb = guard_bb0;
786 while (nconds > 0)
788 unsigned ci0;
789 edge bi_call_in_edge;
790 gimple_stmt_iterator guard_bsi = gsi_for_stmt (cond_expr_start);
791 ci0 = ci;
792 cond_expr_start = VEC_index (gimple, conds, ci0);
793 for (; ci < tn_cond_stmts; ci++)
795 gimple c = VEC_index (gimple, conds, ci);
796 gcc_assert (c || ci != ci0);
797 if (!c)
798 break;
799 gsi_insert_before (&guard_bsi, c, GSI_SAME_STMT);
800 cond_expr = c;
802 nconds--;
803 ci++;
804 gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND);
805 guard_bb_in_edge = split_block (guard_bb, cond_expr);
806 guard_bb_in_edge->flags &= ~EDGE_FALLTHRU;
807 guard_bb_in_edge->flags |= EDGE_FALSE_VALUE;
809 bi_call_in_edge = make_edge (guard_bb, bi_call_bb, EDGE_TRUE_VALUE);
811 bi_call_in_edge->probability = REG_BR_PROB_BASE * ERR_PROB;
812 guard_bb_in_edge->probability =
813 REG_BR_PROB_BASE - bi_call_in_edge->probability;
816 VEC_free (gimple, heap, conds);
817 if (dump_file && (dump_flags & TDF_DETAILS))
819 location_t loc;
820 loc = gimple_location (bi_call);
821 fprintf (dump_file,
822 "%s:%d: note: function call is shrink-wrapped"
823 " into error conditions.\n",
824 LOCATION_FILE (loc), LOCATION_LINE (loc));
827 return true;
830 /* The top level function for conditional dead code shrink
831 wrapping transformation. */
833 static bool
834 shrink_wrap_conditional_dead_built_in_calls (VEC (gimple, heap) *calls)
836 bool changed = false;
837 unsigned i = 0;
839 unsigned n = VEC_length (gimple, calls);
840 if (n == 0)
841 return false;
843 for (; i < n ; i++)
845 gimple bi_call = VEC_index (gimple, calls, i);
846 changed |= shrink_wrap_one_built_in_call (bi_call);
849 return changed;
852 /* Pass entry points. */
854 static unsigned int
855 tree_call_cdce (void)
857 basic_block bb;
858 gimple_stmt_iterator i;
859 bool something_changed = false;
860 VEC (gimple, heap) *cond_dead_built_in_calls = NULL;
861 FOR_EACH_BB (bb)
863 /* Collect dead call candidates. */
864 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
866 gimple stmt = gsi_stmt (i);
867 if (is_gimple_call (stmt)
868 && is_call_dce_candidate (stmt))
870 if (dump_file && (dump_flags & TDF_DETAILS))
872 fprintf (dump_file, "Found conditional dead call: ");
873 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
874 fprintf (dump_file, "\n");
876 if (cond_dead_built_in_calls == NULL)
877 cond_dead_built_in_calls = VEC_alloc (gimple, heap, 64);
878 VEC_safe_push (gimple, heap, cond_dead_built_in_calls, stmt);
883 if (cond_dead_built_in_calls == NULL)
884 return 0;
886 something_changed
887 = shrink_wrap_conditional_dead_built_in_calls (cond_dead_built_in_calls);
889 VEC_free (gimple, heap, cond_dead_built_in_calls);
891 if (something_changed)
893 free_dominance_info (CDI_DOMINATORS);
894 free_dominance_info (CDI_POST_DOMINATORS);
895 /* As we introduced new control-flow we need to insert PHI-nodes
896 for the call-clobbers of the remaining call. */
897 mark_sym_for_renaming (gimple_vop (cfun));
898 return (TODO_update_ssa | TODO_cleanup_cfg | TODO_ggc_collect
899 | TODO_remove_unused_locals);
901 else
902 return 0;
905 static bool
906 gate_call_cdce (void)
908 /* The limit constants used in the implementation
909 assume IEEE floating point format. Other formats
910 can be supported in the future if needed. */
911 return flag_tree_builtin_call_dce != 0 && optimize_function_for_speed_p (cfun);
914 struct gimple_opt_pass pass_call_cdce =
917 GIMPLE_PASS,
918 "cdce", /* name */
919 gate_call_cdce, /* gate */
920 tree_call_cdce, /* execute */
921 NULL, /* sub */
922 NULL, /* next */
923 0, /* static_pass_number */
924 TV_TREE_CALL_CDCE, /* tv_id */
925 PROP_cfg | PROP_ssa, /* properties_required */
926 0, /* properties_provided */
927 0, /* properties_destroyed */
928 0, /* todo_flags_start */
929 TODO_verify_ssa /* todo_flags_finish */