2015-06-11 Paul Thomas <pault@gcc.gnu.org>
[official-gcc.git] / gcc / tree-call-cdce.c
blobb07fbd6d0132e8c506dafff2259092da408b3efa
1 /* Conditional Dead Call Elimination pass for the GNU compiler.
2 Copyright (C) 2008-2015 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 "predict.h"
26 #include "hard-reg-set.h"
27 #include "input.h"
28 #include "function.h"
29 #include "dominance.h"
30 #include "cfg.h"
31 #include "basic-block.h"
32 #include "symtab.h"
33 #include "alias.h"
34 #include "tree.h"
35 #include "fold-const.h"
36 #include "stor-layout.h"
37 #include "gimple-pretty-print.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "gimple-expr.h"
41 #include "is-a.h"
42 #include "gimple.h"
43 #include "gimple-iterator.h"
44 #include "gimple-ssa.h"
45 #include "tree-cfg.h"
46 #include "stringpool.h"
47 #include "tree-ssanames.h"
48 #include "tree-into-ssa.h"
49 #include "tree-pass.h"
50 #include "flags.h"
53 /* Conditional dead call elimination
55 Some builtin functions can set errno on error conditions, but they
56 are otherwise pure. If the result of a call to such a function is
57 not used, the compiler can still not eliminate the call without
58 powerful interprocedural analysis to prove that the errno is not
59 checked. However, if the conditions under which the error occurs
60 are known, the compiler can conditionally dead code eliminate the
61 calls by shrink-wrapping the semi-dead calls into the error condition:
63 built_in_call (args)
64 ==>
65 if (error_cond (args))
66 built_in_call (args)
68 An actual simple example is :
69 log (x); // Mostly dead call
70 ==>
71 if (x <= 0)
72 log (x);
73 With this change, call to log (x) is effectively eliminated, as
74 in majority of the cases, log won't be called with x out of
75 range. The branch is totally predictable, so the branch cost
76 is low.
78 Note that library functions are not supposed to clear errno to zero without
79 error. See IEEE Std 1003.1, section 2.3 Error Numbers, and section 7.5:3 of
80 ISO/IEC 9899 (C99).
82 The condition wrapping the builtin call is conservatively set to avoid too
83 aggressive (wrong) shrink wrapping. The optimization is called conditional
84 dead call elimination because the call is eliminated under the condition
85 that the input arguments would not lead to domain or range error (for
86 instance when x <= 0 for a log (x) call), however the chances that the error
87 condition is hit is very low (those builtin calls which are conditionally
88 dead are usually part of the C++ abstraction penalty exposed after
89 inlining). */
92 /* A structure for representing input domain of
93 a function argument in integer. If the lower
94 bound is -inf, has_lb is set to false. If the
95 upper bound is +inf, has_ub is false.
96 is_lb_inclusive and is_ub_inclusive are flags
97 to indicate if lb and ub value are inclusive
98 respectively. */
100 typedef struct input_domain
102 int lb;
103 int ub;
104 bool has_lb;
105 bool has_ub;
106 bool is_lb_inclusive;
107 bool is_ub_inclusive;
108 } inp_domain;
110 /* A helper function to construct and return an input
111 domain object. LB is the lower bound, HAS_LB is
112 a boolean flag indicating if the lower bound exists,
113 and LB_INCLUSIVE is a boolean flag indicating if the
114 lower bound is inclusive or not. UB, HAS_UB, and
115 UB_INCLUSIVE have the same meaning, but for upper
116 bound of the domain. */
118 static inp_domain
119 get_domain (int lb, bool has_lb, bool lb_inclusive,
120 int ub, bool has_ub, bool ub_inclusive)
122 inp_domain domain;
123 domain.lb = lb;
124 domain.has_lb = has_lb;
125 domain.is_lb_inclusive = lb_inclusive;
126 domain.ub = ub;
127 domain.has_ub = has_ub;
128 domain.is_ub_inclusive = ub_inclusive;
129 return domain;
132 /* A helper function to check the target format for the
133 argument type. In this implementation, only IEEE formats
134 are supported. ARG is the call argument to be checked.
135 Returns true if the format is supported. To support other
136 target formats, function get_no_error_domain needs to be
137 enhanced to have range bounds properly computed. Since
138 the check is cheap (very small number of candidates
139 to be checked), the result is not cached for each float type. */
141 static bool
142 check_target_format (tree arg)
144 tree type;
145 machine_mode mode;
146 const struct real_format *rfmt;
148 type = TREE_TYPE (arg);
149 mode = TYPE_MODE (type);
150 rfmt = REAL_MODE_FORMAT (mode);
151 if ((mode == SFmode
152 && (rfmt == &ieee_single_format || rfmt == &mips_single_format
153 || rfmt == &motorola_single_format))
154 || (mode == DFmode
155 && (rfmt == &ieee_double_format || rfmt == &mips_double_format
156 || rfmt == &motorola_double_format))
157 /* For long double, we can not really check XFmode
158 which is only defined on intel platforms.
159 Candidate pre-selection using builtin function
160 code guarantees that we are checking formats
161 for long double modes: double, quad, and extended. */
162 || (mode != SFmode && mode != DFmode
163 && (rfmt == &ieee_quad_format
164 || rfmt == &mips_quad_format
165 || rfmt == &ieee_extended_motorola_format
166 || rfmt == &ieee_extended_intel_96_format
167 || rfmt == &ieee_extended_intel_128_format
168 || rfmt == &ieee_extended_intel_96_round_53_format)))
169 return true;
171 return false;
175 /* A helper function to help select calls to pow that are suitable for
176 conditional DCE transformation. It looks for pow calls that can be
177 guided with simple conditions. Such calls either have constant base
178 values or base values converted from integers. Returns true if
179 the pow call POW_CALL is a candidate. */
181 /* The maximum integer bit size for base argument of a pow call
182 that is suitable for shrink-wrapping transformation. */
183 #define MAX_BASE_INT_BIT_SIZE 32
185 static bool
186 check_pow (gcall *pow_call)
188 tree base, expn;
189 enum tree_code bc, ec;
191 if (gimple_call_num_args (pow_call) != 2)
192 return false;
194 base = gimple_call_arg (pow_call, 0);
195 expn = gimple_call_arg (pow_call, 1);
197 if (!check_target_format (expn))
198 return false;
200 bc = TREE_CODE (base);
201 ec = TREE_CODE (expn);
203 /* Folding candidates are not interesting.
204 Can actually assert that it is already folded. */
205 if (ec == REAL_CST && bc == REAL_CST)
206 return false;
208 if (bc == REAL_CST)
210 /* Only handle a fixed range of constant. */
211 REAL_VALUE_TYPE mv;
212 REAL_VALUE_TYPE bcv = TREE_REAL_CST (base);
213 if (REAL_VALUES_EQUAL (bcv, dconst1))
214 return false;
215 if (REAL_VALUES_LESS (bcv, dconst1))
216 return false;
217 real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, UNSIGNED);
218 if (REAL_VALUES_LESS (mv, bcv))
219 return false;
220 return true;
222 else if (bc == SSA_NAME)
224 tree base_val0, type;
225 gimple base_def;
226 int bit_sz;
228 /* Only handles cases where base value is converted
229 from integer values. */
230 base_def = SSA_NAME_DEF_STMT (base);
231 if (gimple_code (base_def) != GIMPLE_ASSIGN)
232 return false;
234 if (gimple_assign_rhs_code (base_def) != FLOAT_EXPR)
235 return false;
236 base_val0 = gimple_assign_rhs1 (base_def);
238 type = TREE_TYPE (base_val0);
239 if (TREE_CODE (type) != INTEGER_TYPE)
240 return false;
241 bit_sz = TYPE_PRECISION (type);
242 /* If the type of the base is too wide,
243 the resulting shrink wrapping condition
244 will be too conservative. */
245 if (bit_sz > MAX_BASE_INT_BIT_SIZE)
246 return false;
248 return true;
250 else
251 return false;
254 /* A helper function to help select candidate function calls that are
255 suitable for conditional DCE. Candidate functions must have single
256 valid input domain in this implementation except for pow (see check_pow).
257 Returns true if the function call is a candidate. */
259 static bool
260 check_builtin_call (gcall *bcall)
262 tree arg;
264 arg = gimple_call_arg (bcall, 0);
265 return check_target_format (arg);
268 /* A helper function to determine if a builtin function call is a
269 candidate for conditional DCE. Returns true if the builtin call
270 is a candidate. */
272 static bool
273 is_call_dce_candidate (gcall *call)
275 tree fn;
276 enum built_in_function fnc;
278 /* Only potentially dead calls are considered. */
279 if (gimple_call_lhs (call))
280 return false;
282 fn = gimple_call_fndecl (call);
283 if (!fn
284 || !DECL_BUILT_IN (fn)
285 || (DECL_BUILT_IN_CLASS (fn) != BUILT_IN_NORMAL))
286 return false;
288 fnc = DECL_FUNCTION_CODE (fn);
289 switch (fnc)
291 /* Trig functions. */
292 CASE_FLT_FN (BUILT_IN_ACOS):
293 CASE_FLT_FN (BUILT_IN_ASIN):
294 /* Hyperbolic functions. */
295 CASE_FLT_FN (BUILT_IN_ACOSH):
296 CASE_FLT_FN (BUILT_IN_ATANH):
297 CASE_FLT_FN (BUILT_IN_COSH):
298 CASE_FLT_FN (BUILT_IN_SINH):
299 /* Log functions. */
300 CASE_FLT_FN (BUILT_IN_LOG):
301 CASE_FLT_FN (BUILT_IN_LOG2):
302 CASE_FLT_FN (BUILT_IN_LOG10):
303 CASE_FLT_FN (BUILT_IN_LOG1P):
304 /* Exp functions. */
305 CASE_FLT_FN (BUILT_IN_EXP):
306 CASE_FLT_FN (BUILT_IN_EXP2):
307 CASE_FLT_FN (BUILT_IN_EXP10):
308 CASE_FLT_FN (BUILT_IN_EXPM1):
309 CASE_FLT_FN (BUILT_IN_POW10):
310 /* Sqrt. */
311 CASE_FLT_FN (BUILT_IN_SQRT):
312 return check_builtin_call (call);
313 /* Special one: two argument pow. */
314 case BUILT_IN_POW:
315 return check_pow (call);
316 default:
317 break;
320 return false;
324 /* A helper function to generate gimple statements for
325 one bound comparison. ARG is the call argument to
326 be compared with the bound, LBUB is the bound value
327 in integer, TCODE is the tree_code of the comparison,
328 TEMP_NAME1/TEMP_NAME2 are names of the temporaries,
329 CONDS is a vector holding the produced GIMPLE statements,
330 and NCONDS points to the variable holding the number
331 of logical comparisons. CONDS is either empty or
332 a list ended with a null tree. */
334 static void
335 gen_one_condition (tree arg, int lbub,
336 enum tree_code tcode,
337 const char *temp_name1,
338 const char *temp_name2,
339 vec<gimple> conds,
340 unsigned *nconds)
342 tree lbub_real_cst, lbub_cst, float_type;
343 tree temp, tempn, tempc, tempcn;
344 gassign *stmt1;
345 gassign *stmt2;
346 gcond *stmt3;
348 float_type = TREE_TYPE (arg);
349 lbub_cst = build_int_cst (integer_type_node, lbub);
350 lbub_real_cst = build_real_from_int_cst (float_type, lbub_cst);
352 temp = create_tmp_var (float_type, temp_name1);
353 stmt1 = gimple_build_assign (temp, arg);
354 tempn = make_ssa_name (temp, stmt1);
355 gimple_assign_set_lhs (stmt1, tempn);
357 tempc = create_tmp_var (boolean_type_node, temp_name2);
358 stmt2 = gimple_build_assign (tempc,
359 fold_build2 (tcode,
360 boolean_type_node,
361 tempn, lbub_real_cst));
362 tempcn = make_ssa_name (tempc, stmt2);
363 gimple_assign_set_lhs (stmt2, tempcn);
365 stmt3 = gimple_build_cond_from_tree (tempcn, NULL_TREE, NULL_TREE);
366 conds.quick_push (stmt1);
367 conds.quick_push (stmt2);
368 conds.quick_push (stmt3);
369 (*nconds)++;
372 /* A helper function to generate GIMPLE statements for
373 out of input domain check. ARG is the call argument
374 to be runtime checked, DOMAIN holds the valid domain
375 for the given function, CONDS points to the vector
376 holding the result GIMPLE statements. *NCONDS is
377 the number of logical comparisons. This function
378 produces no more than two logical comparisons, one
379 for lower bound check, one for upper bound check. */
381 static void
382 gen_conditions_for_domain (tree arg, inp_domain domain,
383 vec<gimple> conds,
384 unsigned *nconds)
386 if (domain.has_lb)
387 gen_one_condition (arg, domain.lb,
388 (domain.is_lb_inclusive
389 ? LT_EXPR : LE_EXPR),
390 "DCE_COND_LB", "DCE_COND_LB_TEST",
391 conds, nconds);
393 if (domain.has_ub)
395 /* Now push a separator. */
396 if (domain.has_lb)
397 conds.quick_push (NULL);
399 gen_one_condition (arg, domain.ub,
400 (domain.is_ub_inclusive
401 ? GT_EXPR : GE_EXPR),
402 "DCE_COND_UB", "DCE_COND_UB_TEST",
403 conds, nconds);
408 /* A helper function to generate condition
409 code for the y argument in call pow (some_const, y).
410 See candidate selection in check_pow. Since the
411 candidates' base values have a limited range,
412 the guarded code generated for y are simple:
413 if (y > max_y)
414 pow (const, y);
415 Note max_y can be computed separately for each
416 const base, but in this implementation, we
417 choose to compute it using the max base
418 in the allowed range for the purpose of
419 simplicity. BASE is the constant base value,
420 EXPN is the expression for the exponent argument,
421 *CONDS is the vector to hold resulting statements,
422 and *NCONDS is the number of logical conditions. */
424 static void
425 gen_conditions_for_pow_cst_base (tree base, tree expn,
426 vec<gimple> conds,
427 unsigned *nconds)
429 inp_domain exp_domain;
430 /* Validate the range of the base constant to make
431 sure it is consistent with check_pow. */
432 REAL_VALUE_TYPE mv;
433 REAL_VALUE_TYPE bcv = TREE_REAL_CST (base);
434 gcc_assert (!REAL_VALUES_EQUAL (bcv, dconst1)
435 && !REAL_VALUES_LESS (bcv, dconst1));
436 real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, UNSIGNED);
437 gcc_assert (!REAL_VALUES_LESS (mv, bcv));
439 exp_domain = get_domain (0, false, false,
440 127, true, false);
442 gen_conditions_for_domain (expn, exp_domain,
443 conds, nconds);
446 /* Generate error condition code for pow calls with
447 non constant base values. The candidates selected
448 have their base argument value converted from
449 integer (see check_pow) value (1, 2, 4 bytes), and
450 the max exp value is computed based on the size
451 of the integer type (i.e. max possible base value).
452 The resulting input domain for exp argument is thus
453 conservative (smaller than the max value allowed by
454 the runtime value of the base). BASE is the integer
455 base value, EXPN is the expression for the exponent
456 argument, *CONDS is the vector to hold resulting
457 statements, and *NCONDS is the number of logical
458 conditions. */
460 static void
461 gen_conditions_for_pow_int_base (tree base, tree expn,
462 vec<gimple> conds,
463 unsigned *nconds)
465 gimple base_def;
466 tree base_val0;
467 tree int_type;
468 tree temp, tempn;
469 tree cst0;
470 gimple stmt1, stmt2;
471 int bit_sz, max_exp;
472 inp_domain exp_domain;
474 base_def = SSA_NAME_DEF_STMT (base);
475 base_val0 = gimple_assign_rhs1 (base_def);
476 int_type = TREE_TYPE (base_val0);
477 bit_sz = TYPE_PRECISION (int_type);
478 gcc_assert (bit_sz > 0
479 && bit_sz <= MAX_BASE_INT_BIT_SIZE);
481 /* Determine the max exp argument value according to
482 the size of the base integer. The max exp value
483 is conservatively estimated assuming IEEE754 double
484 precision format. */
485 if (bit_sz == 8)
486 max_exp = 128;
487 else if (bit_sz == 16)
488 max_exp = 64;
489 else
491 gcc_assert (bit_sz == MAX_BASE_INT_BIT_SIZE);
492 max_exp = 32;
495 /* For pow ((double)x, y), generate the following conditions:
496 cond 1:
497 temp1 = x;
498 if (temp1 <= 0)
500 cond 2:
501 temp2 = y;
502 if (temp2 > max_exp_real_cst) */
504 /* Generate condition in reverse order -- first
505 the condition for the exp argument. */
507 exp_domain = get_domain (0, false, false,
508 max_exp, true, true);
510 gen_conditions_for_domain (expn, exp_domain,
511 conds, nconds);
513 /* Now generate condition for the base argument.
514 Note it does not use the helper function
515 gen_conditions_for_domain because the base
516 type is integer. */
518 /* Push a separator. */
519 conds.quick_push (NULL);
521 temp = create_tmp_var (int_type, "DCE_COND1");
522 cst0 = build_int_cst (int_type, 0);
523 stmt1 = gimple_build_assign (temp, base_val0);
524 tempn = make_ssa_name (temp, stmt1);
525 gimple_assign_set_lhs (stmt1, tempn);
526 stmt2 = gimple_build_cond (LE_EXPR, tempn, cst0, NULL_TREE, NULL_TREE);
528 conds.quick_push (stmt1);
529 conds.quick_push (stmt2);
530 (*nconds)++;
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. */
550 static void
551 gen_conditions_for_pow (gcall *pow_call, vec<gimple> conds,
552 unsigned *nconds)
554 tree base, expn;
555 enum tree_code bc;
557 gcc_checking_assert (check_pow (pow_call));
559 *nconds = 0;
561 base = gimple_call_arg (pow_call, 0);
562 expn = gimple_call_arg (pow_call, 1);
564 bc = TREE_CODE (base);
566 if (bc == REAL_CST)
567 gen_conditions_for_pow_cst_base (base, expn, conds, nconds);
568 else if (bc == SSA_NAME)
569 gen_conditions_for_pow_int_base (base, expn, conds, nconds);
570 else
571 gcc_unreachable ();
574 /* A helper routine to help computing the valid input domain
575 for a builtin function. See C99 7.12.7 for details. In this
576 implementation, we only handle single region domain. The
577 resulting region can be conservative (smaller) than the actual
578 one and rounded to integers. Some of the bounds are documented
579 in the standard, while other limit constants are computed
580 assuming IEEE floating point format (for SF and DF modes).
581 Since IEEE only sets minimum requirements for long double format,
582 different long double formats exist under different implementations
583 (e.g, 64 bit double precision (DF), 80 bit double-extended
584 precision (XF), and 128 bit quad precision (QF) ). For simplicity,
585 in this implementation, the computed bounds for long double assume
586 64 bit format (DF), and are therefore conservative. Another
587 assumption is that single precision float type is always SF mode,
588 and double type is DF mode. This function is quite
589 implementation specific, so it may not be suitable to be part of
590 builtins.c. This needs to be revisited later to see if it can
591 be leveraged in x87 assembly expansion. */
593 static inp_domain
594 get_no_error_domain (enum built_in_function fnc)
596 switch (fnc)
598 /* Trig functions: return [-1, +1] */
599 CASE_FLT_FN (BUILT_IN_ACOS):
600 CASE_FLT_FN (BUILT_IN_ASIN):
601 return get_domain (-1, true, true,
602 1, true, true);
603 /* Hyperbolic functions. */
604 CASE_FLT_FN (BUILT_IN_ACOSH):
605 /* acosh: [1, +inf) */
606 return get_domain (1, true, true,
607 1, false, false);
608 CASE_FLT_FN (BUILT_IN_ATANH):
609 /* atanh: (-1, +1) */
610 return get_domain (-1, true, false,
611 1, true, false);
612 case BUILT_IN_COSHF:
613 case BUILT_IN_SINHF:
614 /* coshf: (-89, +89) */
615 return get_domain (-89, true, false,
616 89, true, false);
617 case BUILT_IN_COSH:
618 case BUILT_IN_SINH:
619 case BUILT_IN_COSHL:
620 case BUILT_IN_SINHL:
621 /* cosh: (-710, +710) */
622 return get_domain (-710, true, false,
623 710, true, false);
624 /* Log functions: (0, +inf) */
625 CASE_FLT_FN (BUILT_IN_LOG):
626 CASE_FLT_FN (BUILT_IN_LOG2):
627 CASE_FLT_FN (BUILT_IN_LOG10):
628 return get_domain (0, true, false,
629 0, false, false);
630 CASE_FLT_FN (BUILT_IN_LOG1P):
631 return get_domain (-1, true, false,
632 0, false, false);
633 /* Exp functions. */
634 case BUILT_IN_EXPF:
635 case BUILT_IN_EXPM1F:
636 /* expf: (-inf, 88) */
637 return get_domain (-1, false, false,
638 88, true, false);
639 case BUILT_IN_EXP:
640 case BUILT_IN_EXPM1:
641 case BUILT_IN_EXPL:
642 case BUILT_IN_EXPM1L:
643 /* exp: (-inf, 709) */
644 return get_domain (-1, false, false,
645 709, true, false);
646 case BUILT_IN_EXP2F:
647 /* exp2f: (-inf, 128) */
648 return get_domain (-1, false, false,
649 128, true, false);
650 case BUILT_IN_EXP2:
651 case BUILT_IN_EXP2L:
652 /* exp2: (-inf, 1024) */
653 return get_domain (-1, false, false,
654 1024, true, false);
655 case BUILT_IN_EXP10F:
656 case BUILT_IN_POW10F:
657 /* exp10f: (-inf, 38) */
658 return get_domain (-1, false, false,
659 38, true, false);
660 case BUILT_IN_EXP10:
661 case BUILT_IN_POW10:
662 case BUILT_IN_EXP10L:
663 case BUILT_IN_POW10L:
664 /* exp10: (-inf, 308) */
665 return get_domain (-1, false, false,
666 308, true, false);
667 /* sqrt: [0, +inf) */
668 CASE_FLT_FN (BUILT_IN_SQRT):
669 return get_domain (0, true, true,
670 0, false, false);
671 default:
672 gcc_unreachable ();
675 gcc_unreachable ();
678 /* The function to generate shrink wrap conditions for a partially
679 dead builtin call whose return value is not used anywhere,
680 but has to be kept live due to potential error condition.
681 BI_CALL is the builtin call, CONDS is the vector of statements
682 for condition code, NCODES is the pointer to the number of
683 logical conditions. Statements belonging to different logical
684 condition are separated by NULL tree in the vector. */
686 static void
687 gen_shrink_wrap_conditions (gcall *bi_call, vec<gimple> conds,
688 unsigned int *nconds)
690 gcall *call;
691 tree fn;
692 enum built_in_function fnc;
694 gcc_assert (nconds && conds.exists ());
695 gcc_assert (conds.length () == 0);
696 gcc_assert (is_gimple_call (bi_call));
698 call = bi_call;
699 fn = gimple_call_fndecl (call);
700 gcc_assert (fn && DECL_BUILT_IN (fn));
701 fnc = DECL_FUNCTION_CODE (fn);
702 *nconds = 0;
704 if (fnc == BUILT_IN_POW)
705 gen_conditions_for_pow (call, conds, nconds);
706 else
708 tree arg;
709 inp_domain domain = get_no_error_domain (fnc);
710 *nconds = 0;
711 arg = gimple_call_arg (bi_call, 0);
712 gen_conditions_for_domain (arg, domain, conds, nconds);
715 return;
719 /* Probability of the branch (to the call) is taken. */
720 #define ERR_PROB 0.01
722 /* The function to shrink wrap a partially dead builtin call
723 whose return value is not used anywhere, but has to be kept
724 live due to potential error condition. Returns true if the
725 transformation actually happens. */
727 static bool
728 shrink_wrap_one_built_in_call (gcall *bi_call)
730 gimple_stmt_iterator bi_call_bsi;
731 basic_block bi_call_bb, join_tgt_bb, guard_bb, guard_bb0;
732 edge join_tgt_in_edge_from_call, join_tgt_in_edge_fall_thru;
733 edge bi_call_in_edge0, guard_bb_in_edge;
734 unsigned tn_cond_stmts, nconds;
735 unsigned ci;
736 gimple cond_expr = NULL;
737 gimple cond_expr_start;
738 tree bi_call_label_decl;
739 gimple bi_call_label;
741 auto_vec<gimple, 12> conds;
742 gen_shrink_wrap_conditions (bi_call, conds, &nconds);
744 /* This can happen if the condition generator decides
745 it is not beneficial to do the transformation. Just
746 return false and do not do any transformation for
747 the call. */
748 if (nconds == 0)
749 return false;
751 bi_call_bb = gimple_bb (bi_call);
753 /* Now find the join target bb -- split bi_call_bb if needed. */
754 if (stmt_ends_bb_p (bi_call))
756 /* If the call must be the last in the bb, don't split the block,
757 it could e.g. have EH edges. */
758 join_tgt_in_edge_from_call = find_fallthru_edge (bi_call_bb->succs);
759 if (join_tgt_in_edge_from_call == NULL)
760 return false;
762 else
763 join_tgt_in_edge_from_call = split_block (bi_call_bb, bi_call);
765 bi_call_bsi = gsi_for_stmt (bi_call);
767 join_tgt_bb = join_tgt_in_edge_from_call->dest;
769 /* Now it is time to insert the first conditional expression
770 into bi_call_bb and split this bb so that bi_call is
771 shrink-wrapped. */
772 tn_cond_stmts = conds.length ();
773 cond_expr = NULL;
774 cond_expr_start = conds[0];
775 for (ci = 0; ci < tn_cond_stmts; ci++)
777 gimple c = conds[ci];
778 gcc_assert (c || ci != 0);
779 if (!c)
780 break;
781 gsi_insert_before (&bi_call_bsi, c, GSI_SAME_STMT);
782 cond_expr = c;
784 nconds--;
785 ci++;
786 gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND);
788 /* Now the label. */
789 bi_call_label_decl = create_artificial_label (gimple_location (bi_call));
790 bi_call_label = gimple_build_label (bi_call_label_decl);
791 gsi_insert_before (&bi_call_bsi, bi_call_label, GSI_SAME_STMT);
793 bi_call_in_edge0 = split_block (bi_call_bb, cond_expr);
794 bi_call_in_edge0->flags &= ~EDGE_FALLTHRU;
795 bi_call_in_edge0->flags |= EDGE_TRUE_VALUE;
796 guard_bb0 = bi_call_bb;
797 bi_call_bb = bi_call_in_edge0->dest;
798 join_tgt_in_edge_fall_thru = make_edge (guard_bb0, join_tgt_bb,
799 EDGE_FALSE_VALUE);
801 bi_call_in_edge0->probability = REG_BR_PROB_BASE * ERR_PROB;
802 bi_call_in_edge0->count =
803 apply_probability (guard_bb0->count,
804 bi_call_in_edge0->probability);
805 join_tgt_in_edge_fall_thru->probability =
806 inverse_probability (bi_call_in_edge0->probability);
807 join_tgt_in_edge_fall_thru->count =
808 guard_bb0->count - bi_call_in_edge0->count;
810 /* Code generation for the rest of the conditions */
811 guard_bb = guard_bb0;
812 while (nconds > 0)
814 unsigned ci0;
815 edge bi_call_in_edge;
816 gimple_stmt_iterator guard_bsi = gsi_for_stmt (cond_expr_start);
817 ci0 = ci;
818 cond_expr_start = conds[ci0];
819 for (; ci < tn_cond_stmts; ci++)
821 gimple c = conds[ci];
822 gcc_assert (c || ci != ci0);
823 if (!c)
824 break;
825 gsi_insert_before (&guard_bsi, c, GSI_SAME_STMT);
826 cond_expr = c;
828 nconds--;
829 ci++;
830 gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND);
831 guard_bb_in_edge = split_block (guard_bb, cond_expr);
832 guard_bb_in_edge->flags &= ~EDGE_FALLTHRU;
833 guard_bb_in_edge->flags |= EDGE_FALSE_VALUE;
835 bi_call_in_edge = make_edge (guard_bb, bi_call_bb, EDGE_TRUE_VALUE);
837 bi_call_in_edge->probability = REG_BR_PROB_BASE * ERR_PROB;
838 bi_call_in_edge->count =
839 apply_probability (guard_bb->count,
840 bi_call_in_edge->probability);
841 guard_bb_in_edge->probability =
842 inverse_probability (bi_call_in_edge->probability);
843 guard_bb_in_edge->count = guard_bb->count - bi_call_in_edge->count;
846 if (dump_file && (dump_flags & TDF_DETAILS))
848 location_t loc;
849 loc = gimple_location (bi_call);
850 fprintf (dump_file,
851 "%s:%d: note: function call is shrink-wrapped"
852 " into error conditions.\n",
853 LOCATION_FILE (loc), LOCATION_LINE (loc));
856 return true;
859 /* The top level function for conditional dead code shrink
860 wrapping transformation. */
862 static bool
863 shrink_wrap_conditional_dead_built_in_calls (vec<gcall *> calls)
865 bool changed = false;
866 unsigned i = 0;
868 unsigned n = calls.length ();
869 if (n == 0)
870 return false;
872 for (; i < n ; i++)
874 gcall *bi_call = calls[i];
875 changed |= shrink_wrap_one_built_in_call (bi_call);
878 return changed;
881 namespace {
883 const pass_data pass_data_call_cdce =
885 GIMPLE_PASS, /* type */
886 "cdce", /* name */
887 OPTGROUP_NONE, /* optinfo_flags */
888 TV_TREE_CALL_CDCE, /* tv_id */
889 ( PROP_cfg | PROP_ssa ), /* properties_required */
890 0, /* properties_provided */
891 0, /* properties_destroyed */
892 0, /* todo_flags_start */
893 0, /* todo_flags_finish */
896 class pass_call_cdce : public gimple_opt_pass
898 public:
899 pass_call_cdce (gcc::context *ctxt)
900 : gimple_opt_pass (pass_data_call_cdce, ctxt)
903 /* opt_pass methods: */
904 virtual bool gate (function *fun)
906 /* The limit constants used in the implementation
907 assume IEEE floating point format. Other formats
908 can be supported in the future if needed. */
909 return flag_tree_builtin_call_dce != 0
910 && optimize_function_for_speed_p (fun);
913 virtual unsigned int execute (function *);
915 }; // class pass_call_cdce
917 unsigned int
918 pass_call_cdce::execute (function *fun)
920 basic_block bb;
921 gimple_stmt_iterator i;
922 bool something_changed = false;
923 auto_vec<gcall *> cond_dead_built_in_calls;
924 FOR_EACH_BB_FN (bb, fun)
926 /* Collect dead call candidates. */
927 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
929 gcall *stmt = dyn_cast <gcall *> (gsi_stmt (i));
930 if (stmt && is_call_dce_candidate (stmt))
932 if (dump_file && (dump_flags & TDF_DETAILS))
934 fprintf (dump_file, "Found conditional dead call: ");
935 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
936 fprintf (dump_file, "\n");
938 if (!cond_dead_built_in_calls.exists ())
939 cond_dead_built_in_calls.create (64);
940 cond_dead_built_in_calls.safe_push (stmt);
945 if (!cond_dead_built_in_calls.exists ())
946 return 0;
948 something_changed
949 = shrink_wrap_conditional_dead_built_in_calls (cond_dead_built_in_calls);
951 if (something_changed)
953 free_dominance_info (CDI_DOMINATORS);
954 free_dominance_info (CDI_POST_DOMINATORS);
955 /* As we introduced new control-flow we need to insert PHI-nodes
956 for the call-clobbers of the remaining call. */
957 mark_virtual_operands_for_renaming (fun);
958 return TODO_update_ssa;
961 return 0;
964 } // anon namespace
966 gimple_opt_pass *
967 make_pass_call_cdce (gcc::context *ctxt)
969 return new pass_call_cdce (ctxt);