Concretize gimple_call_set_fntype
[official-gcc.git] / gcc / tree-call-cdce.c
blob5dea8dbd49c338189b6aca3c7b72b049a0bce903
1 /* Conditional Dead Call Elimination pass for the GNU compiler.
2 Copyright (C) 2008-2014 Free Software Foundation, Inc.
3 Contributed by Xinliang David Li <davidxl@google.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3, or (at your option) any
10 later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "basic-block.h"
26 #include "tree.h"
27 #include "stor-layout.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-ssa-alias.h"
30 #include "internal-fn.h"
31 #include "gimple-expr.h"
32 #include "is-a.h"
33 #include "gimple.h"
34 #include "gimple-iterator.h"
35 #include "gimple-ssa.h"
36 #include "tree-cfg.h"
37 #include "stringpool.h"
38 #include "tree-ssanames.h"
39 #include "tree-into-ssa.h"
40 #include "tree-pass.h"
41 #include "flags.h"
44 /* Conditional dead call elimination
46 Some builtin functions can set errno on error conditions, but they
47 are otherwise pure. If the result of a call to such a function is
48 not used, the compiler can still not eliminate the call without
49 powerful interprocedural analysis to prove that the errno is not
50 checked. However, if the conditions under which the error occurs
51 are known, the compiler can conditionally dead code eliminate the
52 calls by shrink-wrapping the semi-dead calls into the error condition:
54 built_in_call (args)
55 ==>
56 if (error_cond (args))
57 built_in_call (args)
59 An actual simple example is :
60 log (x); // Mostly dead call
61 ==>
62 if (x < 0)
63 log (x);
64 With this change, call to log (x) is effectively eliminated, as
65 in majority of the cases, log won't be called with x out of
66 range. The branch is totally predictable, so the branch cost
67 is low.
69 Note that library functions are not supposed to clear errno to zero without
70 error. See IEEE Std 1003.1, section 2.3 Error Numbers, and section 7.5:3 of
71 ISO/IEC 9899 (C99).
73 The condition wrapping the builtin call is conservatively set to avoid too
74 aggressive (wrong) shrink wrapping. The optimization is called conditional
75 dead call elimination because the call is eliminated under the condition
76 that the input arguments would not lead to domain or range error (for
77 instance when x <= 0 for a log (x) call), however the chances that the error
78 condition is hit is very low (those builtin calls which are conditionally
79 dead are usually part of the C++ abstraction penalty exposed after
80 inlining). */
83 /* A structure for representing input domain of
84 a function argument in integer. If the lower
85 bound is -inf, has_lb is set to false. If the
86 upper bound is +inf, has_ub is false.
87 is_lb_inclusive and is_ub_inclusive are flags
88 to indicate if lb and ub value are inclusive
89 respectively. */
91 typedef struct input_domain
93 int lb;
94 int ub;
95 bool has_lb;
96 bool has_ub;
97 bool is_lb_inclusive;
98 bool is_ub_inclusive;
99 } inp_domain;
101 /* A helper function to construct and return an input
102 domain object. LB is the lower bound, HAS_LB is
103 a boolean flag indicating if the lower bound exists,
104 and LB_INCLUSIVE is a boolean flag indicating if the
105 lower bound is inclusive or not. UB, HAS_UB, and
106 UB_INCLUSIVE have the same meaning, but for upper
107 bound of the domain. */
109 static inp_domain
110 get_domain (int lb, bool has_lb, bool lb_inclusive,
111 int ub, bool has_ub, bool ub_inclusive)
113 inp_domain domain;
114 domain.lb = lb;
115 domain.has_lb = has_lb;
116 domain.is_lb_inclusive = lb_inclusive;
117 domain.ub = ub;
118 domain.has_ub = has_ub;
119 domain.is_ub_inclusive = ub_inclusive;
120 return domain;
123 /* A helper function to check the target format for the
124 argument type. In this implementation, only IEEE formats
125 are supported. ARG is the call argument to be checked.
126 Returns true if the format is supported. To support other
127 target formats, function get_no_error_domain needs to be
128 enhanced to have range bounds properly computed. Since
129 the check is cheap (very small number of candidates
130 to be checked), the result is not cached for each float type. */
132 static bool
133 check_target_format (tree arg)
135 tree type;
136 enum machine_mode mode;
137 const struct real_format *rfmt;
139 type = TREE_TYPE (arg);
140 mode = TYPE_MODE (type);
141 rfmt = REAL_MODE_FORMAT (mode);
142 if ((mode == SFmode
143 && (rfmt == &ieee_single_format || rfmt == &mips_single_format
144 || rfmt == &motorola_single_format))
145 || (mode == DFmode
146 && (rfmt == &ieee_double_format || rfmt == &mips_double_format
147 || rfmt == &motorola_double_format))
148 /* For long double, we can not really check XFmode
149 which is only defined on intel platforms.
150 Candidate pre-selection using builtin function
151 code guarantees that we are checking formats
152 for long double modes: double, quad, and extended. */
153 || (mode != SFmode && mode != DFmode
154 && (rfmt == &ieee_quad_format
155 || rfmt == &mips_quad_format
156 || rfmt == &ieee_extended_motorola_format
157 || rfmt == &ieee_extended_intel_96_format
158 || rfmt == &ieee_extended_intel_128_format
159 || rfmt == &ieee_extended_intel_96_round_53_format)))
160 return true;
162 return false;
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
176 static bool
177 check_pow (gimple_call pow_call)
179 tree base, expn;
180 enum tree_code bc, ec;
182 if (gimple_call_num_args (pow_call) != 2)
183 return false;
185 base = gimple_call_arg (pow_call, 0);
186 expn = gimple_call_arg (pow_call, 1);
188 if (!check_target_format (expn))
189 return false;
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)
197 return false;
199 if (bc == REAL_CST)
201 /* Only handle a fixed range of constant. */
202 REAL_VALUE_TYPE mv;
203 REAL_VALUE_TYPE bcv = TREE_REAL_CST (base);
204 if (REAL_VALUES_EQUAL (bcv, dconst1))
205 return false;
206 if (REAL_VALUES_LESS (bcv, dconst1))
207 return false;
208 real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, UNSIGNED);
209 if (REAL_VALUES_LESS (mv, bcv))
210 return false;
211 return true;
213 else if (bc == SSA_NAME)
215 tree base_val0, type;
216 gimple base_def;
217 int bit_sz;
219 /* Only handles cases where base value is converted
220 from integer values. */
221 base_def = SSA_NAME_DEF_STMT (base);
222 if (gimple_code (base_def) != GIMPLE_ASSIGN)
223 return false;
225 if (gimple_assign_rhs_code (base_def) != FLOAT_EXPR)
226 return false;
227 base_val0 = gimple_assign_rhs1 (base_def);
229 type = TREE_TYPE (base_val0);
230 if (TREE_CODE (type) != INTEGER_TYPE)
231 return false;
232 bit_sz = TYPE_PRECISION (type);
233 /* If the type of the base is too wide,
234 the resulting shrink wrapping condition
235 will be too conservative. */
236 if (bit_sz > MAX_BASE_INT_BIT_SIZE)
237 return false;
239 return true;
241 else
242 return false;
245 /* A helper function to help select candidate function calls that are
246 suitable for conditional DCE. Candidate functions must have single
247 valid input domain in this implementation except for pow (see check_pow).
248 Returns true if the function call is a candidate. */
250 static bool
251 check_builtin_call (gimple_call bcall)
253 tree arg;
255 arg = gimple_call_arg (bcall, 0);
256 return check_target_format (arg);
259 /* A helper function to determine if a builtin function call is a
260 candidate for conditional DCE. Returns true if the builtin call
261 is a candidate. */
263 static bool
264 is_call_dce_candidate (gimple_call call)
266 tree fn;
267 enum built_in_function fnc;
269 /* Only potentially dead calls are considered. */
270 if (gimple_call_lhs (call))
271 return false;
273 fn = gimple_call_fndecl (call);
274 if (!fn
275 || !DECL_BUILT_IN (fn)
276 || (DECL_BUILT_IN_CLASS (fn) != BUILT_IN_NORMAL))
277 return false;
279 fnc = DECL_FUNCTION_CODE (fn);
280 switch (fnc)
282 /* Trig functions. */
283 CASE_FLT_FN (BUILT_IN_ACOS):
284 CASE_FLT_FN (BUILT_IN_ASIN):
285 /* Hyperbolic functions. */
286 CASE_FLT_FN (BUILT_IN_ACOSH):
287 CASE_FLT_FN (BUILT_IN_ATANH):
288 CASE_FLT_FN (BUILT_IN_COSH):
289 CASE_FLT_FN (BUILT_IN_SINH):
290 /* Log functions. */
291 CASE_FLT_FN (BUILT_IN_LOG):
292 CASE_FLT_FN (BUILT_IN_LOG2):
293 CASE_FLT_FN (BUILT_IN_LOG10):
294 CASE_FLT_FN (BUILT_IN_LOG1P):
295 /* Exp functions. */
296 CASE_FLT_FN (BUILT_IN_EXP):
297 CASE_FLT_FN (BUILT_IN_EXP2):
298 CASE_FLT_FN (BUILT_IN_EXP10):
299 CASE_FLT_FN (BUILT_IN_EXPM1):
300 CASE_FLT_FN (BUILT_IN_POW10):
301 /* Sqrt. */
302 CASE_FLT_FN (BUILT_IN_SQRT):
303 return check_builtin_call (call);
304 /* Special one: two argument pow. */
305 case BUILT_IN_POW:
306 return check_pow (call);
307 default:
308 break;
311 return false;
315 /* A helper function to generate gimple statements for
316 one bound comparison. ARG is the call argument to
317 be compared with the bound, LBUB is the bound value
318 in integer, TCODE is the tree_code of the comparison,
319 TEMP_NAME1/TEMP_NAME2 are names of the temporaries,
320 CONDS is a vector holding the produced GIMPLE statements,
321 and NCONDS points to the variable holding the number
322 of logical comparisons. CONDS is either empty or
323 a list ended with a null tree. */
325 static void
326 gen_one_condition (tree arg, int lbub,
327 enum tree_code tcode,
328 const char *temp_name1,
329 const char *temp_name2,
330 vec<gimple> conds,
331 unsigned *nconds)
333 tree lbub_real_cst, lbub_cst, float_type;
334 tree temp, tempn, tempc, tempcn;
335 gimple_assign stmt1;
336 gimple_assign stmt2;
337 gimple_cond stmt3;
339 float_type = TREE_TYPE (arg);
340 lbub_cst = build_int_cst (integer_type_node, lbub);
341 lbub_real_cst = build_real_from_int_cst (float_type, lbub_cst);
343 temp = create_tmp_var (float_type, temp_name1);
344 stmt1 = gimple_build_assign (temp, arg);
345 tempn = make_ssa_name (temp, stmt1);
346 gimple_assign_set_lhs (stmt1, tempn);
348 tempc = create_tmp_var (boolean_type_node, temp_name2);
349 stmt2 = gimple_build_assign (tempc,
350 fold_build2 (tcode,
351 boolean_type_node,
352 tempn, lbub_real_cst));
353 tempcn = make_ssa_name (tempc, stmt2);
354 gimple_assign_set_lhs (stmt2, tempcn);
356 stmt3 = gimple_build_cond_from_tree (tempcn, NULL_TREE, NULL_TREE);
357 conds.quick_push (stmt1);
358 conds.quick_push (stmt2);
359 conds.quick_push (stmt3);
360 (*nconds)++;
363 /* A helper function to generate GIMPLE statements for
364 out of input domain check. ARG is the call argument
365 to be runtime checked, DOMAIN holds the valid domain
366 for the given function, CONDS points to the vector
367 holding the result GIMPLE statements. *NCONDS is
368 the number of logical comparisons. This function
369 produces no more than two logical comparisons, one
370 for lower bound check, one for upper bound check. */
372 static void
373 gen_conditions_for_domain (tree arg, inp_domain domain,
374 vec<gimple> conds,
375 unsigned *nconds)
377 if (domain.has_lb)
378 gen_one_condition (arg, domain.lb,
379 (domain.is_lb_inclusive
380 ? LT_EXPR : LE_EXPR),
381 "DCE_COND_LB", "DCE_COND_LB_TEST",
382 conds, nconds);
384 if (domain.has_ub)
386 /* Now push a separator. */
387 if (domain.has_lb)
388 conds.quick_push (NULL);
390 gen_one_condition (arg, domain.ub,
391 (domain.is_ub_inclusive
392 ? GT_EXPR : GE_EXPR),
393 "DCE_COND_UB", "DCE_COND_UB_TEST",
394 conds, nconds);
399 /* A helper function to generate condition
400 code for the y argument in call pow (some_const, y).
401 See candidate selection in check_pow. Since the
402 candidates' base values have a limited range,
403 the guarded code generated for y are simple:
404 if (y > max_y)
405 pow (const, y);
406 Note max_y can be computed separately for each
407 const base, but in this implementation, we
408 choose to compute it using the max base
409 in the allowed range for the purpose of
410 simplicity. BASE is the constant base value,
411 EXPN is the expression for the exponent argument,
412 *CONDS is the vector to hold resulting statements,
413 and *NCONDS is the number of logical conditions. */
415 static void
416 gen_conditions_for_pow_cst_base (tree base, tree expn,
417 vec<gimple> conds,
418 unsigned *nconds)
420 inp_domain exp_domain;
421 /* Validate the range of the base constant to make
422 sure it is consistent with check_pow. */
423 REAL_VALUE_TYPE mv;
424 REAL_VALUE_TYPE bcv = TREE_REAL_CST (base);
425 gcc_assert (!REAL_VALUES_EQUAL (bcv, dconst1)
426 && !REAL_VALUES_LESS (bcv, dconst1));
427 real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, UNSIGNED);
428 gcc_assert (!REAL_VALUES_LESS (mv, bcv));
430 exp_domain = get_domain (0, false, false,
431 127, true, false);
433 gen_conditions_for_domain (expn, exp_domain,
434 conds, nconds);
437 /* Generate error condition code for pow calls with
438 non constant base values. The candidates selected
439 have their base argument value converted from
440 integer (see check_pow) value (1, 2, 4 bytes), and
441 the max exp value is computed based on the size
442 of the integer type (i.e. max possible base value).
443 The resulting input domain for exp argument is thus
444 conservative (smaller than the max value allowed by
445 the runtime value of the base). BASE is the integer
446 base value, EXPN is the expression for the exponent
447 argument, *CONDS is the vector to hold resulting
448 statements, and *NCONDS is the number of logical
449 conditions. */
451 static void
452 gen_conditions_for_pow_int_base (tree base, tree expn,
453 vec<gimple> conds,
454 unsigned *nconds)
456 gimple base_def;
457 tree base_val0;
458 tree int_type;
459 tree temp, tempn;
460 tree cst0;
461 gimple stmt1, stmt2;
462 int bit_sz, max_exp;
463 inp_domain exp_domain;
465 base_def = SSA_NAME_DEF_STMT (base);
466 base_val0 = gimple_assign_rhs1 (base_def);
467 int_type = TREE_TYPE (base_val0);
468 bit_sz = TYPE_PRECISION (int_type);
469 gcc_assert (bit_sz > 0
470 && bit_sz <= MAX_BASE_INT_BIT_SIZE);
472 /* Determine the max exp argument value according to
473 the size of the base integer. The max exp value
474 is conservatively estimated assuming IEEE754 double
475 precision format. */
476 if (bit_sz == 8)
477 max_exp = 128;
478 else if (bit_sz == 16)
479 max_exp = 64;
480 else
482 gcc_assert (bit_sz == MAX_BASE_INT_BIT_SIZE);
483 max_exp = 32;
486 /* For pow ((double)x, y), generate the following conditions:
487 cond 1:
488 temp1 = x;
489 if (temp1 <= 0)
491 cond 2:
492 temp2 = y;
493 if (temp2 > max_exp_real_cst) */
495 /* Generate condition in reverse order -- first
496 the condition for the exp argument. */
498 exp_domain = get_domain (0, false, false,
499 max_exp, true, true);
501 gen_conditions_for_domain (expn, exp_domain,
502 conds, nconds);
504 /* Now generate condition for the base argument.
505 Note it does not use the helper function
506 gen_conditions_for_domain because the base
507 type is integer. */
509 /* Push a separator. */
510 conds.quick_push (NULL);
512 temp = create_tmp_var (int_type, "DCE_COND1");
513 cst0 = build_int_cst (int_type, 0);
514 stmt1 = gimple_build_assign (temp, base_val0);
515 tempn = make_ssa_name (temp, stmt1);
516 gimple_assign_set_lhs (stmt1, tempn);
517 stmt2 = gimple_build_cond (LE_EXPR, tempn, cst0, NULL_TREE, NULL_TREE);
519 conds.quick_push (stmt1);
520 conds.quick_push (stmt2);
521 (*nconds)++;
524 /* Method to generate conditional statements for guarding conditionally
525 dead calls to pow. One or more statements can be generated for
526 each logical condition. Statement groups of different conditions
527 are separated by a NULL tree and they are stored in the vec
528 conds. The number of logical conditions are stored in *nconds.
530 See C99 standard, 7.12.7.4:2, for description of pow (x, y).
531 The precise condition for domain errors are complex. In this
532 implementation, a simplified (but conservative) valid domain
533 for x and y are used: x is positive to avoid dom errors, while
534 y is smaller than a upper bound (depending on x) to avoid range
535 errors. Runtime code is generated to check x (if not constant)
536 and y against the valid domain. If it is out, jump to the call,
537 otherwise the call is bypassed. POW_CALL is the call statement,
538 *CONDS is a vector holding the resulting condition statements,
539 and *NCONDS is the number of logical conditions. */
541 static void
542 gen_conditions_for_pow (gimple_call pow_call, vec<gimple> conds,
543 unsigned *nconds)
545 tree base, expn;
546 enum tree_code bc;
548 gcc_checking_assert (check_pow (pow_call));
550 *nconds = 0;
552 base = gimple_call_arg (pow_call, 0);
553 expn = gimple_call_arg (pow_call, 1);
555 bc = TREE_CODE (base);
557 if (bc == REAL_CST)
558 gen_conditions_for_pow_cst_base (base, expn, conds, nconds);
559 else if (bc == SSA_NAME)
560 gen_conditions_for_pow_int_base (base, expn, conds, nconds);
561 else
562 gcc_unreachable ();
565 /* A helper routine to help computing the valid input domain
566 for a builtin function. See C99 7.12.7 for details. In this
567 implementation, we only handle single region domain. The
568 resulting region can be conservative (smaller) than the actual
569 one and rounded to integers. Some of the bounds are documented
570 in the standard, while other limit constants are computed
571 assuming IEEE floating point format (for SF and DF modes).
572 Since IEEE only sets minimum requirements for long double format,
573 different long double formats exist under different implementations
574 (e.g, 64 bit double precision (DF), 80 bit double-extended
575 precision (XF), and 128 bit quad precision (QF) ). For simplicity,
576 in this implementation, the computed bounds for long double assume
577 64 bit format (DF), and are therefore conservative. Another
578 assumption is that single precision float type is always SF mode,
579 and double type is DF mode. This function is quite
580 implementation specific, so it may not be suitable to be part of
581 builtins.c. This needs to be revisited later to see if it can
582 be leveraged in x87 assembly expansion. */
584 static inp_domain
585 get_no_error_domain (enum built_in_function fnc)
587 switch (fnc)
589 /* Trig functions: return [-1, +1] */
590 CASE_FLT_FN (BUILT_IN_ACOS):
591 CASE_FLT_FN (BUILT_IN_ASIN):
592 return get_domain (-1, true, true,
593 1, true, true);
594 /* Hyperbolic functions. */
595 CASE_FLT_FN (BUILT_IN_ACOSH):
596 /* acosh: [1, +inf) */
597 return get_domain (1, true, true,
598 1, false, false);
599 CASE_FLT_FN (BUILT_IN_ATANH):
600 /* atanh: (-1, +1) */
601 return get_domain (-1, true, false,
602 1, true, false);
603 case BUILT_IN_COSHF:
604 case BUILT_IN_SINHF:
605 /* coshf: (-89, +89) */
606 return get_domain (-89, true, false,
607 89, true, false);
608 case BUILT_IN_COSH:
609 case BUILT_IN_SINH:
610 case BUILT_IN_COSHL:
611 case BUILT_IN_SINHL:
612 /* cosh: (-710, +710) */
613 return get_domain (-710, true, false,
614 710, true, false);
615 /* Log functions: (0, +inf) */
616 CASE_FLT_FN (BUILT_IN_LOG):
617 CASE_FLT_FN (BUILT_IN_LOG2):
618 CASE_FLT_FN (BUILT_IN_LOG10):
619 return get_domain (0, true, false,
620 0, false, false);
621 CASE_FLT_FN (BUILT_IN_LOG1P):
622 return get_domain (-1, true, false,
623 0, false, false);
624 /* Exp functions. */
625 case BUILT_IN_EXPF:
626 case BUILT_IN_EXPM1F:
627 /* expf: (-inf, 88) */
628 return get_domain (-1, false, false,
629 88, true, false);
630 case BUILT_IN_EXP:
631 case BUILT_IN_EXPM1:
632 case BUILT_IN_EXPL:
633 case BUILT_IN_EXPM1L:
634 /* exp: (-inf, 709) */
635 return get_domain (-1, false, false,
636 709, true, false);
637 case BUILT_IN_EXP2F:
638 /* exp2f: (-inf, 128) */
639 return get_domain (-1, false, false,
640 128, true, false);
641 case BUILT_IN_EXP2:
642 case BUILT_IN_EXP2L:
643 /* exp2: (-inf, 1024) */
644 return get_domain (-1, false, false,
645 1024, true, false);
646 case BUILT_IN_EXP10F:
647 case BUILT_IN_POW10F:
648 /* exp10f: (-inf, 38) */
649 return get_domain (-1, false, false,
650 38, true, false);
651 case BUILT_IN_EXP10:
652 case BUILT_IN_POW10:
653 case BUILT_IN_EXP10L:
654 case BUILT_IN_POW10L:
655 /* exp10: (-inf, 308) */
656 return get_domain (-1, false, false,
657 308, true, false);
658 /* sqrt: [0, +inf) */
659 CASE_FLT_FN (BUILT_IN_SQRT):
660 return get_domain (0, true, true,
661 0, false, false);
662 default:
663 gcc_unreachable ();
666 gcc_unreachable ();
669 /* The function to generate shrink wrap conditions for a partially
670 dead builtin call whose return value is not used anywhere,
671 but has to be kept live due to potential error condition.
672 BI_CALL is the builtin call, CONDS is the vector of statements
673 for condition code, NCODES is the pointer to the number of
674 logical conditions. Statements belonging to different logical
675 condition are separated by NULL tree in the vector. */
677 static void
678 gen_shrink_wrap_conditions (gimple_call bi_call, vec<gimple> conds,
679 unsigned int *nconds)
681 gimple_call call;
682 tree fn;
683 enum built_in_function fnc;
685 gcc_assert (nconds && conds.exists ());
686 gcc_assert (conds.length () == 0);
687 gcc_assert (is_gimple_call (bi_call));
689 call = bi_call;
690 fn = gimple_call_fndecl (call);
691 gcc_assert (fn && DECL_BUILT_IN (fn));
692 fnc = DECL_FUNCTION_CODE (fn);
693 *nconds = 0;
695 if (fnc == BUILT_IN_POW)
696 gen_conditions_for_pow (call, conds, nconds);
697 else
699 tree arg;
700 inp_domain domain = get_no_error_domain (fnc);
701 *nconds = 0;
702 arg = gimple_call_arg (bi_call, 0);
703 gen_conditions_for_domain (arg, domain, conds, nconds);
706 return;
710 /* Probability of the branch (to the call) is taken. */
711 #define ERR_PROB 0.01
713 /* The function to shrink wrap a partially dead builtin call
714 whose return value is not used anywhere, but has to be kept
715 live due to potential error condition. Returns true if the
716 transformation actually happens. */
718 static bool
719 shrink_wrap_one_built_in_call (gimple_call bi_call)
721 gimple_stmt_iterator bi_call_bsi;
722 basic_block bi_call_bb, join_tgt_bb, guard_bb, guard_bb0;
723 edge join_tgt_in_edge_from_call, join_tgt_in_edge_fall_thru;
724 edge bi_call_in_edge0, guard_bb_in_edge;
725 unsigned tn_cond_stmts, nconds;
726 unsigned ci;
727 gimple cond_expr = NULL;
728 gimple cond_expr_start;
729 tree bi_call_label_decl;
730 gimple bi_call_label;
732 auto_vec<gimple, 12> conds;
733 gen_shrink_wrap_conditions (bi_call, conds, &nconds);
735 /* This can happen if the condition generator decides
736 it is not beneficial to do the transformation. Just
737 return false and do not do any transformation for
738 the call. */
739 if (nconds == 0)
740 return false;
742 bi_call_bb = gimple_bb (bi_call);
744 /* Now find the join target bb -- split bi_call_bb if needed. */
745 if (stmt_ends_bb_p (bi_call))
747 /* If the call must be the last in the bb, don't split the block,
748 it could e.g. have EH edges. */
749 join_tgt_in_edge_from_call = find_fallthru_edge (bi_call_bb->succs);
750 if (join_tgt_in_edge_from_call == NULL)
751 return false;
753 else
754 join_tgt_in_edge_from_call = split_block (bi_call_bb, bi_call);
756 bi_call_bsi = gsi_for_stmt (bi_call);
758 join_tgt_bb = join_tgt_in_edge_from_call->dest;
760 /* Now it is time to insert the first conditional expression
761 into bi_call_bb and split this bb so that bi_call is
762 shrink-wrapped. */
763 tn_cond_stmts = conds.length ();
764 cond_expr = NULL;
765 cond_expr_start = conds[0];
766 for (ci = 0; ci < tn_cond_stmts; ci++)
768 gimple c = conds[ci];
769 gcc_assert (c || ci != 0);
770 if (!c)
771 break;
772 gsi_insert_before (&bi_call_bsi, c, GSI_SAME_STMT);
773 cond_expr = c;
775 nconds--;
776 ci++;
777 gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND);
779 /* Now the label. */
780 bi_call_label_decl = create_artificial_label (gimple_location (bi_call));
781 bi_call_label = gimple_build_label (bi_call_label_decl);
782 gsi_insert_before (&bi_call_bsi, bi_call_label, GSI_SAME_STMT);
784 bi_call_in_edge0 = split_block (bi_call_bb, cond_expr);
785 bi_call_in_edge0->flags &= ~EDGE_FALLTHRU;
786 bi_call_in_edge0->flags |= EDGE_TRUE_VALUE;
787 guard_bb0 = bi_call_bb;
788 bi_call_bb = bi_call_in_edge0->dest;
789 join_tgt_in_edge_fall_thru = make_edge (guard_bb0, join_tgt_bb,
790 EDGE_FALSE_VALUE);
792 bi_call_in_edge0->probability = REG_BR_PROB_BASE * ERR_PROB;
793 bi_call_in_edge0->count =
794 apply_probability (guard_bb0->count,
795 bi_call_in_edge0->probability);
796 join_tgt_in_edge_fall_thru->probability =
797 inverse_probability (bi_call_in_edge0->probability);
798 join_tgt_in_edge_fall_thru->count =
799 guard_bb0->count - bi_call_in_edge0->count;
801 /* Code generation for the rest of the conditions */
802 guard_bb = guard_bb0;
803 while (nconds > 0)
805 unsigned ci0;
806 edge bi_call_in_edge;
807 gimple_stmt_iterator guard_bsi = gsi_for_stmt (cond_expr_start);
808 ci0 = ci;
809 cond_expr_start = conds[ci0];
810 for (; ci < tn_cond_stmts; ci++)
812 gimple c = conds[ci];
813 gcc_assert (c || ci != ci0);
814 if (!c)
815 break;
816 gsi_insert_before (&guard_bsi, c, GSI_SAME_STMT);
817 cond_expr = c;
819 nconds--;
820 ci++;
821 gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND);
822 guard_bb_in_edge = split_block (guard_bb, cond_expr);
823 guard_bb_in_edge->flags &= ~EDGE_FALLTHRU;
824 guard_bb_in_edge->flags |= EDGE_FALSE_VALUE;
826 bi_call_in_edge = make_edge (guard_bb, bi_call_bb, EDGE_TRUE_VALUE);
828 bi_call_in_edge->probability = REG_BR_PROB_BASE * ERR_PROB;
829 bi_call_in_edge->count =
830 apply_probability (guard_bb->count,
831 bi_call_in_edge->probability);
832 guard_bb_in_edge->probability =
833 inverse_probability (bi_call_in_edge->probability);
834 guard_bb_in_edge->count = guard_bb->count - bi_call_in_edge->count;
837 if (dump_file && (dump_flags & TDF_DETAILS))
839 location_t loc;
840 loc = gimple_location (bi_call);
841 fprintf (dump_file,
842 "%s:%d: note: function call is shrink-wrapped"
843 " into error conditions.\n",
844 LOCATION_FILE (loc), LOCATION_LINE (loc));
847 return true;
850 /* The top level function for conditional dead code shrink
851 wrapping transformation. */
853 static bool
854 shrink_wrap_conditional_dead_built_in_calls (vec<gimple_call> calls)
856 bool changed = false;
857 unsigned i = 0;
859 unsigned n = calls.length ();
860 if (n == 0)
861 return false;
863 for (; i < n ; i++)
865 gimple_call bi_call = calls[i];
866 changed |= shrink_wrap_one_built_in_call (bi_call);
869 return changed;
872 namespace {
874 const pass_data pass_data_call_cdce =
876 GIMPLE_PASS, /* type */
877 "cdce", /* name */
878 OPTGROUP_NONE, /* optinfo_flags */
879 TV_TREE_CALL_CDCE, /* tv_id */
880 ( PROP_cfg | PROP_ssa ), /* properties_required */
881 0, /* properties_provided */
882 0, /* properties_destroyed */
883 0, /* todo_flags_start */
884 0, /* todo_flags_finish */
887 class pass_call_cdce : public gimple_opt_pass
889 public:
890 pass_call_cdce (gcc::context *ctxt)
891 : gimple_opt_pass (pass_data_call_cdce, ctxt)
894 /* opt_pass methods: */
895 virtual bool gate (function *fun)
897 /* The limit constants used in the implementation
898 assume IEEE floating point format. Other formats
899 can be supported in the future if needed. */
900 return flag_tree_builtin_call_dce != 0
901 && optimize_function_for_speed_p (fun);
904 virtual unsigned int execute (function *);
906 }; // class pass_call_cdce
908 unsigned int
909 pass_call_cdce::execute (function *fun)
911 basic_block bb;
912 gimple_stmt_iterator i;
913 bool something_changed = false;
914 auto_vec<gimple_call> cond_dead_built_in_calls;
915 FOR_EACH_BB_FN (bb, fun)
917 /* Collect dead call candidates. */
918 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
920 gimple_call stmt = dyn_cast <gimple_call> (gsi_stmt (i));
921 if (stmt && is_call_dce_candidate (stmt))
923 if (dump_file && (dump_flags & TDF_DETAILS))
925 fprintf (dump_file, "Found conditional dead call: ");
926 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
927 fprintf (dump_file, "\n");
929 if (!cond_dead_built_in_calls.exists ())
930 cond_dead_built_in_calls.create (64);
931 cond_dead_built_in_calls.safe_push (stmt);
936 if (!cond_dead_built_in_calls.exists ())
937 return 0;
939 something_changed
940 = shrink_wrap_conditional_dead_built_in_calls (cond_dead_built_in_calls);
942 if (something_changed)
944 free_dominance_info (CDI_DOMINATORS);
945 free_dominance_info (CDI_POST_DOMINATORS);
946 /* As we introduced new control-flow we need to insert PHI-nodes
947 for the call-clobbers of the remaining call. */
948 mark_virtual_operands_for_renaming (fun);
949 return TODO_update_ssa;
952 return 0;
955 } // anon namespace
957 gimple_opt_pass *
958 make_pass_call_cdce (gcc::context *ctxt)
960 return new pass_call_cdce (ctxt);