* lib/ubsan-dg.exp (check_effective_target_fsanitize_undefined):
[official-gcc.git] / gcc / tree-call-cdce.c
blob188a29553fa56fa7e0d4c5b6e6838f8e418631df
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 "predict.h"
26 #include "vec.h"
27 #include "hashtab.h"
28 #include "hash-set.h"
29 #include "machmode.h"
30 #include "hard-reg-set.h"
31 #include "input.h"
32 #include "function.h"
33 #include "dominance.h"
34 #include "cfg.h"
35 #include "basic-block.h"
36 #include "tree.h"
37 #include "stor-layout.h"
38 #include "gimple-pretty-print.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-expr.h"
42 #include "is-a.h"
43 #include "gimple.h"
44 #include "gimple-iterator.h"
45 #include "gimple-ssa.h"
46 #include "tree-cfg.h"
47 #include "stringpool.h"
48 #include "tree-ssanames.h"
49 #include "tree-into-ssa.h"
50 #include "tree-pass.h"
51 #include "flags.h"
54 /* Conditional dead call elimination
56 Some builtin functions can set errno on error conditions, but they
57 are otherwise pure. If the result of a call to such a function is
58 not used, the compiler can still not eliminate the call without
59 powerful interprocedural analysis to prove that the errno is not
60 checked. However, if the conditions under which the error occurs
61 are known, the compiler can conditionally dead code eliminate the
62 calls by shrink-wrapping the semi-dead calls into the error condition:
64 built_in_call (args)
65 ==>
66 if (error_cond (args))
67 built_in_call (args)
69 An actual simple example is :
70 log (x); // Mostly dead call
71 ==>
72 if (x < 0)
73 log (x);
74 With this change, call to log (x) is effectively eliminated, as
75 in majority of the cases, log won't be called with x out of
76 range. The branch is totally predictable, so the branch cost
77 is low.
79 Note that library functions are not supposed to clear errno to zero without
80 error. See IEEE Std 1003.1, section 2.3 Error Numbers, and section 7.5:3 of
81 ISO/IEC 9899 (C99).
83 The condition wrapping the builtin call is conservatively set to avoid too
84 aggressive (wrong) shrink wrapping. The optimization is called conditional
85 dead call elimination because the call is eliminated under the condition
86 that the input arguments would not lead to domain or range error (for
87 instance when x <= 0 for a log (x) call), however the chances that the error
88 condition is hit is very low (those builtin calls which are conditionally
89 dead are usually part of the C++ abstraction penalty exposed after
90 inlining). */
93 /* A structure for representing input domain of
94 a function argument in integer. If the lower
95 bound is -inf, has_lb is set to false. If the
96 upper bound is +inf, has_ub is false.
97 is_lb_inclusive and is_ub_inclusive are flags
98 to indicate if lb and ub value are inclusive
99 respectively. */
101 typedef struct input_domain
103 int lb;
104 int ub;
105 bool has_lb;
106 bool has_ub;
107 bool is_lb_inclusive;
108 bool is_ub_inclusive;
109 } inp_domain;
111 /* A helper function to construct and return an input
112 domain object. LB is the lower bound, HAS_LB is
113 a boolean flag indicating if the lower bound exists,
114 and LB_INCLUSIVE is a boolean flag indicating if the
115 lower bound is inclusive or not. UB, HAS_UB, and
116 UB_INCLUSIVE have the same meaning, but for upper
117 bound of the domain. */
119 static inp_domain
120 get_domain (int lb, bool has_lb, bool lb_inclusive,
121 int ub, bool has_ub, bool ub_inclusive)
123 inp_domain domain;
124 domain.lb = lb;
125 domain.has_lb = has_lb;
126 domain.is_lb_inclusive = lb_inclusive;
127 domain.ub = ub;
128 domain.has_ub = has_ub;
129 domain.is_ub_inclusive = ub_inclusive;
130 return domain;
133 /* A helper function to check the target format for the
134 argument type. In this implementation, only IEEE formats
135 are supported. ARG is the call argument to be checked.
136 Returns true if the format is supported. To support other
137 target formats, function get_no_error_domain needs to be
138 enhanced to have range bounds properly computed. Since
139 the check is cheap (very small number of candidates
140 to be checked), the result is not cached for each float type. */
142 static bool
143 check_target_format (tree arg)
145 tree type;
146 machine_mode mode;
147 const struct real_format *rfmt;
149 type = TREE_TYPE (arg);
150 mode = TYPE_MODE (type);
151 rfmt = REAL_MODE_FORMAT (mode);
152 if ((mode == SFmode
153 && (rfmt == &ieee_single_format || rfmt == &mips_single_format
154 || rfmt == &motorola_single_format))
155 || (mode == DFmode
156 && (rfmt == &ieee_double_format || rfmt == &mips_double_format
157 || rfmt == &motorola_double_format))
158 /* For long double, we can not really check XFmode
159 which is only defined on intel platforms.
160 Candidate pre-selection using builtin function
161 code guarantees that we are checking formats
162 for long double modes: double, quad, and extended. */
163 || (mode != SFmode && mode != DFmode
164 && (rfmt == &ieee_quad_format
165 || rfmt == &mips_quad_format
166 || rfmt == &ieee_extended_motorola_format
167 || rfmt == &ieee_extended_intel_96_format
168 || rfmt == &ieee_extended_intel_128_format
169 || rfmt == &ieee_extended_intel_96_round_53_format)))
170 return true;
172 return false;
176 /* A helper function to help select calls to pow that are suitable for
177 conditional DCE transformation. It looks for pow calls that can be
178 guided with simple conditions. Such calls either have constant base
179 values or base values converted from integers. Returns true if
180 the pow call POW_CALL is a candidate. */
182 /* The maximum integer bit size for base argument of a pow call
183 that is suitable for shrink-wrapping transformation. */
184 #define MAX_BASE_INT_BIT_SIZE 32
186 static bool
187 check_pow (gcall *pow_call)
189 tree base, expn;
190 enum tree_code bc, ec;
192 if (gimple_call_num_args (pow_call) != 2)
193 return false;
195 base = gimple_call_arg (pow_call, 0);
196 expn = gimple_call_arg (pow_call, 1);
198 if (!check_target_format (expn))
199 return false;
201 bc = TREE_CODE (base);
202 ec = TREE_CODE (expn);
204 /* Folding candidates are not interesting.
205 Can actually assert that it is already folded. */
206 if (ec == REAL_CST && bc == REAL_CST)
207 return false;
209 if (bc == REAL_CST)
211 /* Only handle a fixed range of constant. */
212 REAL_VALUE_TYPE mv;
213 REAL_VALUE_TYPE bcv = TREE_REAL_CST (base);
214 if (REAL_VALUES_EQUAL (bcv, dconst1))
215 return false;
216 if (REAL_VALUES_LESS (bcv, dconst1))
217 return false;
218 real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, UNSIGNED);
219 if (REAL_VALUES_LESS (mv, bcv))
220 return false;
221 return true;
223 else if (bc == SSA_NAME)
225 tree base_val0, type;
226 gimple base_def;
227 int bit_sz;
229 /* Only handles cases where base value is converted
230 from integer values. */
231 base_def = SSA_NAME_DEF_STMT (base);
232 if (gimple_code (base_def) != GIMPLE_ASSIGN)
233 return false;
235 if (gimple_assign_rhs_code (base_def) != FLOAT_EXPR)
236 return false;
237 base_val0 = gimple_assign_rhs1 (base_def);
239 type = TREE_TYPE (base_val0);
240 if (TREE_CODE (type) != INTEGER_TYPE)
241 return false;
242 bit_sz = TYPE_PRECISION (type);
243 /* If the type of the base is too wide,
244 the resulting shrink wrapping condition
245 will be too conservative. */
246 if (bit_sz > MAX_BASE_INT_BIT_SIZE)
247 return false;
249 return true;
251 else
252 return false;
255 /* A helper function to help select candidate function calls that are
256 suitable for conditional DCE. Candidate functions must have single
257 valid input domain in this implementation except for pow (see check_pow).
258 Returns true if the function call is a candidate. */
260 static bool
261 check_builtin_call (gcall *bcall)
263 tree arg;
265 arg = gimple_call_arg (bcall, 0);
266 return check_target_format (arg);
269 /* A helper function to determine if a builtin function call is a
270 candidate for conditional DCE. Returns true if the builtin call
271 is a candidate. */
273 static bool
274 is_call_dce_candidate (gcall *call)
276 tree fn;
277 enum built_in_function fnc;
279 /* Only potentially dead calls are considered. */
280 if (gimple_call_lhs (call))
281 return false;
283 fn = gimple_call_fndecl (call);
284 if (!fn
285 || !DECL_BUILT_IN (fn)
286 || (DECL_BUILT_IN_CLASS (fn) != BUILT_IN_NORMAL))
287 return false;
289 fnc = DECL_FUNCTION_CODE (fn);
290 switch (fnc)
292 /* Trig functions. */
293 CASE_FLT_FN (BUILT_IN_ACOS):
294 CASE_FLT_FN (BUILT_IN_ASIN):
295 /* Hyperbolic functions. */
296 CASE_FLT_FN (BUILT_IN_ACOSH):
297 CASE_FLT_FN (BUILT_IN_ATANH):
298 CASE_FLT_FN (BUILT_IN_COSH):
299 CASE_FLT_FN (BUILT_IN_SINH):
300 /* Log functions. */
301 CASE_FLT_FN (BUILT_IN_LOG):
302 CASE_FLT_FN (BUILT_IN_LOG2):
303 CASE_FLT_FN (BUILT_IN_LOG10):
304 CASE_FLT_FN (BUILT_IN_LOG1P):
305 /* Exp functions. */
306 CASE_FLT_FN (BUILT_IN_EXP):
307 CASE_FLT_FN (BUILT_IN_EXP2):
308 CASE_FLT_FN (BUILT_IN_EXP10):
309 CASE_FLT_FN (BUILT_IN_EXPM1):
310 CASE_FLT_FN (BUILT_IN_POW10):
311 /* Sqrt. */
312 CASE_FLT_FN (BUILT_IN_SQRT):
313 return check_builtin_call (call);
314 /* Special one: two argument pow. */
315 case BUILT_IN_POW:
316 return check_pow (call);
317 default:
318 break;
321 return false;
325 /* A helper function to generate gimple statements for
326 one bound comparison. ARG is the call argument to
327 be compared with the bound, LBUB is the bound value
328 in integer, TCODE is the tree_code of the comparison,
329 TEMP_NAME1/TEMP_NAME2 are names of the temporaries,
330 CONDS is a vector holding the produced GIMPLE statements,
331 and NCONDS points to the variable holding the number
332 of logical comparisons. CONDS is either empty or
333 a list ended with a null tree. */
335 static void
336 gen_one_condition (tree arg, int lbub,
337 enum tree_code tcode,
338 const char *temp_name1,
339 const char *temp_name2,
340 vec<gimple> conds,
341 unsigned *nconds)
343 tree lbub_real_cst, lbub_cst, float_type;
344 tree temp, tempn, tempc, tempcn;
345 gassign *stmt1;
346 gassign *stmt2;
347 gcond *stmt3;
349 float_type = TREE_TYPE (arg);
350 lbub_cst = build_int_cst (integer_type_node, lbub);
351 lbub_real_cst = build_real_from_int_cst (float_type, lbub_cst);
353 temp = create_tmp_var (float_type, temp_name1);
354 stmt1 = gimple_build_assign (temp, arg);
355 tempn = make_ssa_name (temp, stmt1);
356 gimple_assign_set_lhs (stmt1, tempn);
358 tempc = create_tmp_var (boolean_type_node, temp_name2);
359 stmt2 = gimple_build_assign (tempc,
360 fold_build2 (tcode,
361 boolean_type_node,
362 tempn, lbub_real_cst));
363 tempcn = make_ssa_name (tempc, stmt2);
364 gimple_assign_set_lhs (stmt2, tempcn);
366 stmt3 = gimple_build_cond_from_tree (tempcn, NULL_TREE, NULL_TREE);
367 conds.quick_push (stmt1);
368 conds.quick_push (stmt2);
369 conds.quick_push (stmt3);
370 (*nconds)++;
373 /* A helper function to generate GIMPLE statements for
374 out of input domain check. ARG is the call argument
375 to be runtime checked, DOMAIN holds the valid domain
376 for the given function, CONDS points to the vector
377 holding the result GIMPLE statements. *NCONDS is
378 the number of logical comparisons. This function
379 produces no more than two logical comparisons, one
380 for lower bound check, one for upper bound check. */
382 static void
383 gen_conditions_for_domain (tree arg, inp_domain domain,
384 vec<gimple> conds,
385 unsigned *nconds)
387 if (domain.has_lb)
388 gen_one_condition (arg, domain.lb,
389 (domain.is_lb_inclusive
390 ? LT_EXPR : LE_EXPR),
391 "DCE_COND_LB", "DCE_COND_LB_TEST",
392 conds, nconds);
394 if (domain.has_ub)
396 /* Now push a separator. */
397 if (domain.has_lb)
398 conds.quick_push (NULL);
400 gen_one_condition (arg, domain.ub,
401 (domain.is_ub_inclusive
402 ? GT_EXPR : GE_EXPR),
403 "DCE_COND_UB", "DCE_COND_UB_TEST",
404 conds, nconds);
409 /* A helper function to generate condition
410 code for the y argument in call pow (some_const, y).
411 See candidate selection in check_pow. Since the
412 candidates' base values have a limited range,
413 the guarded code generated for y are simple:
414 if (y > max_y)
415 pow (const, y);
416 Note max_y can be computed separately for each
417 const base, but in this implementation, we
418 choose to compute it using the max base
419 in the allowed range for the purpose of
420 simplicity. BASE is the constant base value,
421 EXPN is the expression for the exponent argument,
422 *CONDS is the vector to hold resulting statements,
423 and *NCONDS is the number of logical conditions. */
425 static void
426 gen_conditions_for_pow_cst_base (tree base, tree expn,
427 vec<gimple> conds,
428 unsigned *nconds)
430 inp_domain exp_domain;
431 /* Validate the range of the base constant to make
432 sure it is consistent with check_pow. */
433 REAL_VALUE_TYPE mv;
434 REAL_VALUE_TYPE bcv = TREE_REAL_CST (base);
435 gcc_assert (!REAL_VALUES_EQUAL (bcv, dconst1)
436 && !REAL_VALUES_LESS (bcv, dconst1));
437 real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, UNSIGNED);
438 gcc_assert (!REAL_VALUES_LESS (mv, bcv));
440 exp_domain = get_domain (0, false, false,
441 127, true, false);
443 gen_conditions_for_domain (expn, exp_domain,
444 conds, nconds);
447 /* Generate error condition code for pow calls with
448 non constant base values. The candidates selected
449 have their base argument value converted from
450 integer (see check_pow) value (1, 2, 4 bytes), and
451 the max exp value is computed based on the size
452 of the integer type (i.e. max possible base value).
453 The resulting input domain for exp argument is thus
454 conservative (smaller than the max value allowed by
455 the runtime value of the base). BASE is the integer
456 base value, EXPN is the expression for the exponent
457 argument, *CONDS is the vector to hold resulting
458 statements, and *NCONDS is the number of logical
459 conditions. */
461 static void
462 gen_conditions_for_pow_int_base (tree base, tree expn,
463 vec<gimple> conds,
464 unsigned *nconds)
466 gimple base_def;
467 tree base_val0;
468 tree int_type;
469 tree temp, tempn;
470 tree cst0;
471 gimple stmt1, stmt2;
472 int bit_sz, max_exp;
473 inp_domain exp_domain;
475 base_def = SSA_NAME_DEF_STMT (base);
476 base_val0 = gimple_assign_rhs1 (base_def);
477 int_type = TREE_TYPE (base_val0);
478 bit_sz = TYPE_PRECISION (int_type);
479 gcc_assert (bit_sz > 0
480 && bit_sz <= MAX_BASE_INT_BIT_SIZE);
482 /* Determine the max exp argument value according to
483 the size of the base integer. The max exp value
484 is conservatively estimated assuming IEEE754 double
485 precision format. */
486 if (bit_sz == 8)
487 max_exp = 128;
488 else if (bit_sz == 16)
489 max_exp = 64;
490 else
492 gcc_assert (bit_sz == MAX_BASE_INT_BIT_SIZE);
493 max_exp = 32;
496 /* For pow ((double)x, y), generate the following conditions:
497 cond 1:
498 temp1 = x;
499 if (temp1 <= 0)
501 cond 2:
502 temp2 = y;
503 if (temp2 > max_exp_real_cst) */
505 /* Generate condition in reverse order -- first
506 the condition for the exp argument. */
508 exp_domain = get_domain (0, false, false,
509 max_exp, true, true);
511 gen_conditions_for_domain (expn, exp_domain,
512 conds, nconds);
514 /* Now generate condition for the base argument.
515 Note it does not use the helper function
516 gen_conditions_for_domain because the base
517 type is integer. */
519 /* Push a separator. */
520 conds.quick_push (NULL);
522 temp = create_tmp_var (int_type, "DCE_COND1");
523 cst0 = build_int_cst (int_type, 0);
524 stmt1 = gimple_build_assign (temp, base_val0);
525 tempn = make_ssa_name (temp, stmt1);
526 gimple_assign_set_lhs (stmt1, tempn);
527 stmt2 = gimple_build_cond (LE_EXPR, tempn, cst0, NULL_TREE, NULL_TREE);
529 conds.quick_push (stmt1);
530 conds.quick_push (stmt2);
531 (*nconds)++;
534 /* Method to generate conditional statements for guarding conditionally
535 dead calls to pow. One or more statements can be generated for
536 each logical condition. Statement groups of different conditions
537 are separated by a NULL tree and they are stored in the vec
538 conds. The number of logical conditions are stored in *nconds.
540 See C99 standard, 7.12.7.4:2, for description of pow (x, y).
541 The precise condition for domain errors are complex. In this
542 implementation, a simplified (but conservative) valid domain
543 for x and y are used: x is positive to avoid dom errors, while
544 y is smaller than a upper bound (depending on x) to avoid range
545 errors. Runtime code is generated to check x (if not constant)
546 and y against the valid domain. If it is out, jump to the call,
547 otherwise the call is bypassed. POW_CALL is the call statement,
548 *CONDS is a vector holding the resulting condition statements,
549 and *NCONDS is the number of logical conditions. */
551 static void
552 gen_conditions_for_pow (gcall *pow_call, vec<gimple> conds,
553 unsigned *nconds)
555 tree base, expn;
556 enum tree_code bc;
558 gcc_checking_assert (check_pow (pow_call));
560 *nconds = 0;
562 base = gimple_call_arg (pow_call, 0);
563 expn = gimple_call_arg (pow_call, 1);
565 bc = TREE_CODE (base);
567 if (bc == REAL_CST)
568 gen_conditions_for_pow_cst_base (base, expn, conds, nconds);
569 else if (bc == SSA_NAME)
570 gen_conditions_for_pow_int_base (base, expn, conds, nconds);
571 else
572 gcc_unreachable ();
575 /* A helper routine to help computing the valid input domain
576 for a builtin function. See C99 7.12.7 for details. In this
577 implementation, we only handle single region domain. The
578 resulting region can be conservative (smaller) than the actual
579 one and rounded to integers. Some of the bounds are documented
580 in the standard, while other limit constants are computed
581 assuming IEEE floating point format (for SF and DF modes).
582 Since IEEE only sets minimum requirements for long double format,
583 different long double formats exist under different implementations
584 (e.g, 64 bit double precision (DF), 80 bit double-extended
585 precision (XF), and 128 bit quad precision (QF) ). For simplicity,
586 in this implementation, the computed bounds for long double assume
587 64 bit format (DF), and are therefore conservative. Another
588 assumption is that single precision float type is always SF mode,
589 and double type is DF mode. This function is quite
590 implementation specific, so it may not be suitable to be part of
591 builtins.c. This needs to be revisited later to see if it can
592 be leveraged in x87 assembly expansion. */
594 static inp_domain
595 get_no_error_domain (enum built_in_function fnc)
597 switch (fnc)
599 /* Trig functions: return [-1, +1] */
600 CASE_FLT_FN (BUILT_IN_ACOS):
601 CASE_FLT_FN (BUILT_IN_ASIN):
602 return get_domain (-1, true, true,
603 1, true, true);
604 /* Hyperbolic functions. */
605 CASE_FLT_FN (BUILT_IN_ACOSH):
606 /* acosh: [1, +inf) */
607 return get_domain (1, true, true,
608 1, false, false);
609 CASE_FLT_FN (BUILT_IN_ATANH):
610 /* atanh: (-1, +1) */
611 return get_domain (-1, true, false,
612 1, true, false);
613 case BUILT_IN_COSHF:
614 case BUILT_IN_SINHF:
615 /* coshf: (-89, +89) */
616 return get_domain (-89, true, false,
617 89, true, false);
618 case BUILT_IN_COSH:
619 case BUILT_IN_SINH:
620 case BUILT_IN_COSHL:
621 case BUILT_IN_SINHL:
622 /* cosh: (-710, +710) */
623 return get_domain (-710, true, false,
624 710, true, false);
625 /* Log functions: (0, +inf) */
626 CASE_FLT_FN (BUILT_IN_LOG):
627 CASE_FLT_FN (BUILT_IN_LOG2):
628 CASE_FLT_FN (BUILT_IN_LOG10):
629 return get_domain (0, true, false,
630 0, false, false);
631 CASE_FLT_FN (BUILT_IN_LOG1P):
632 return get_domain (-1, true, false,
633 0, false, false);
634 /* Exp functions. */
635 case BUILT_IN_EXPF:
636 case BUILT_IN_EXPM1F:
637 /* expf: (-inf, 88) */
638 return get_domain (-1, false, false,
639 88, true, false);
640 case BUILT_IN_EXP:
641 case BUILT_IN_EXPM1:
642 case BUILT_IN_EXPL:
643 case BUILT_IN_EXPM1L:
644 /* exp: (-inf, 709) */
645 return get_domain (-1, false, false,
646 709, true, false);
647 case BUILT_IN_EXP2F:
648 /* exp2f: (-inf, 128) */
649 return get_domain (-1, false, false,
650 128, true, false);
651 case BUILT_IN_EXP2:
652 case BUILT_IN_EXP2L:
653 /* exp2: (-inf, 1024) */
654 return get_domain (-1, false, false,
655 1024, true, false);
656 case BUILT_IN_EXP10F:
657 case BUILT_IN_POW10F:
658 /* exp10f: (-inf, 38) */
659 return get_domain (-1, false, false,
660 38, true, false);
661 case BUILT_IN_EXP10:
662 case BUILT_IN_POW10:
663 case BUILT_IN_EXP10L:
664 case BUILT_IN_POW10L:
665 /* exp10: (-inf, 308) */
666 return get_domain (-1, false, false,
667 308, true, false);
668 /* sqrt: [0, +inf) */
669 CASE_FLT_FN (BUILT_IN_SQRT):
670 return get_domain (0, true, true,
671 0, false, false);
672 default:
673 gcc_unreachable ();
676 gcc_unreachable ();
679 /* The function to generate shrink wrap conditions for a partially
680 dead builtin call whose return value is not used anywhere,
681 but has to be kept live due to potential error condition.
682 BI_CALL is the builtin call, CONDS is the vector of statements
683 for condition code, NCODES is the pointer to the number of
684 logical conditions. Statements belonging to different logical
685 condition are separated by NULL tree in the vector. */
687 static void
688 gen_shrink_wrap_conditions (gcall *bi_call, vec<gimple> conds,
689 unsigned int *nconds)
691 gcall *call;
692 tree fn;
693 enum built_in_function fnc;
695 gcc_assert (nconds && conds.exists ());
696 gcc_assert (conds.length () == 0);
697 gcc_assert (is_gimple_call (bi_call));
699 call = bi_call;
700 fn = gimple_call_fndecl (call);
701 gcc_assert (fn && DECL_BUILT_IN (fn));
702 fnc = DECL_FUNCTION_CODE (fn);
703 *nconds = 0;
705 if (fnc == BUILT_IN_POW)
706 gen_conditions_for_pow (call, conds, nconds);
707 else
709 tree arg;
710 inp_domain domain = get_no_error_domain (fnc);
711 *nconds = 0;
712 arg = gimple_call_arg (bi_call, 0);
713 gen_conditions_for_domain (arg, domain, conds, nconds);
716 return;
720 /* Probability of the branch (to the call) is taken. */
721 #define ERR_PROB 0.01
723 /* The function to shrink wrap a partially dead builtin call
724 whose return value is not used anywhere, but has to be kept
725 live due to potential error condition. Returns true if the
726 transformation actually happens. */
728 static bool
729 shrink_wrap_one_built_in_call (gcall *bi_call)
731 gimple_stmt_iterator bi_call_bsi;
732 basic_block bi_call_bb, join_tgt_bb, guard_bb, guard_bb0;
733 edge join_tgt_in_edge_from_call, join_tgt_in_edge_fall_thru;
734 edge bi_call_in_edge0, guard_bb_in_edge;
735 unsigned tn_cond_stmts, nconds;
736 unsigned ci;
737 gimple cond_expr = NULL;
738 gimple cond_expr_start;
739 tree bi_call_label_decl;
740 gimple bi_call_label;
742 auto_vec<gimple, 12> conds;
743 gen_shrink_wrap_conditions (bi_call, conds, &nconds);
745 /* This can happen if the condition generator decides
746 it is not beneficial to do the transformation. Just
747 return false and do not do any transformation for
748 the call. */
749 if (nconds == 0)
750 return false;
752 bi_call_bb = gimple_bb (bi_call);
754 /* Now find the join target bb -- split bi_call_bb if needed. */
755 if (stmt_ends_bb_p (bi_call))
757 /* If the call must be the last in the bb, don't split the block,
758 it could e.g. have EH edges. */
759 join_tgt_in_edge_from_call = find_fallthru_edge (bi_call_bb->succs);
760 if (join_tgt_in_edge_from_call == NULL)
761 return false;
763 else
764 join_tgt_in_edge_from_call = split_block (bi_call_bb, bi_call);
766 bi_call_bsi = gsi_for_stmt (bi_call);
768 join_tgt_bb = join_tgt_in_edge_from_call->dest;
770 /* Now it is time to insert the first conditional expression
771 into bi_call_bb and split this bb so that bi_call is
772 shrink-wrapped. */
773 tn_cond_stmts = conds.length ();
774 cond_expr = NULL;
775 cond_expr_start = conds[0];
776 for (ci = 0; ci < tn_cond_stmts; ci++)
778 gimple c = conds[ci];
779 gcc_assert (c || ci != 0);
780 if (!c)
781 break;
782 gsi_insert_before (&bi_call_bsi, c, GSI_SAME_STMT);
783 cond_expr = c;
785 nconds--;
786 ci++;
787 gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND);
789 /* Now the label. */
790 bi_call_label_decl = create_artificial_label (gimple_location (bi_call));
791 bi_call_label = gimple_build_label (bi_call_label_decl);
792 gsi_insert_before (&bi_call_bsi, bi_call_label, GSI_SAME_STMT);
794 bi_call_in_edge0 = split_block (bi_call_bb, cond_expr);
795 bi_call_in_edge0->flags &= ~EDGE_FALLTHRU;
796 bi_call_in_edge0->flags |= EDGE_TRUE_VALUE;
797 guard_bb0 = bi_call_bb;
798 bi_call_bb = bi_call_in_edge0->dest;
799 join_tgt_in_edge_fall_thru = make_edge (guard_bb0, join_tgt_bb,
800 EDGE_FALSE_VALUE);
802 bi_call_in_edge0->probability = REG_BR_PROB_BASE * ERR_PROB;
803 bi_call_in_edge0->count =
804 apply_probability (guard_bb0->count,
805 bi_call_in_edge0->probability);
806 join_tgt_in_edge_fall_thru->probability =
807 inverse_probability (bi_call_in_edge0->probability);
808 join_tgt_in_edge_fall_thru->count =
809 guard_bb0->count - bi_call_in_edge0->count;
811 /* Code generation for the rest of the conditions */
812 guard_bb = guard_bb0;
813 while (nconds > 0)
815 unsigned ci0;
816 edge bi_call_in_edge;
817 gimple_stmt_iterator guard_bsi = gsi_for_stmt (cond_expr_start);
818 ci0 = ci;
819 cond_expr_start = conds[ci0];
820 for (; ci < tn_cond_stmts; ci++)
822 gimple c = conds[ci];
823 gcc_assert (c || ci != ci0);
824 if (!c)
825 break;
826 gsi_insert_before (&guard_bsi, c, GSI_SAME_STMT);
827 cond_expr = c;
829 nconds--;
830 ci++;
831 gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND);
832 guard_bb_in_edge = split_block (guard_bb, cond_expr);
833 guard_bb_in_edge->flags &= ~EDGE_FALLTHRU;
834 guard_bb_in_edge->flags |= EDGE_FALSE_VALUE;
836 bi_call_in_edge = make_edge (guard_bb, bi_call_bb, EDGE_TRUE_VALUE);
838 bi_call_in_edge->probability = REG_BR_PROB_BASE * ERR_PROB;
839 bi_call_in_edge->count =
840 apply_probability (guard_bb->count,
841 bi_call_in_edge->probability);
842 guard_bb_in_edge->probability =
843 inverse_probability (bi_call_in_edge->probability);
844 guard_bb_in_edge->count = guard_bb->count - bi_call_in_edge->count;
847 if (dump_file && (dump_flags & TDF_DETAILS))
849 location_t loc;
850 loc = gimple_location (bi_call);
851 fprintf (dump_file,
852 "%s:%d: note: function call is shrink-wrapped"
853 " into error conditions.\n",
854 LOCATION_FILE (loc), LOCATION_LINE (loc));
857 return true;
860 /* The top level function for conditional dead code shrink
861 wrapping transformation. */
863 static bool
864 shrink_wrap_conditional_dead_built_in_calls (vec<gcall *> calls)
866 bool changed = false;
867 unsigned i = 0;
869 unsigned n = calls.length ();
870 if (n == 0)
871 return false;
873 for (; i < n ; i++)
875 gcall *bi_call = calls[i];
876 changed |= shrink_wrap_one_built_in_call (bi_call);
879 return changed;
882 namespace {
884 const pass_data pass_data_call_cdce =
886 GIMPLE_PASS, /* type */
887 "cdce", /* name */
888 OPTGROUP_NONE, /* optinfo_flags */
889 TV_TREE_CALL_CDCE, /* tv_id */
890 ( PROP_cfg | PROP_ssa ), /* properties_required */
891 0, /* properties_provided */
892 0, /* properties_destroyed */
893 0, /* todo_flags_start */
894 0, /* todo_flags_finish */
897 class pass_call_cdce : public gimple_opt_pass
899 public:
900 pass_call_cdce (gcc::context *ctxt)
901 : gimple_opt_pass (pass_data_call_cdce, ctxt)
904 /* opt_pass methods: */
905 virtual bool gate (function *fun)
907 /* The limit constants used in the implementation
908 assume IEEE floating point format. Other formats
909 can be supported in the future if needed. */
910 return flag_tree_builtin_call_dce != 0
911 && optimize_function_for_speed_p (fun);
914 virtual unsigned int execute (function *);
916 }; // class pass_call_cdce
918 unsigned int
919 pass_call_cdce::execute (function *fun)
921 basic_block bb;
922 gimple_stmt_iterator i;
923 bool something_changed = false;
924 auto_vec<gcall *> cond_dead_built_in_calls;
925 FOR_EACH_BB_FN (bb, fun)
927 /* Collect dead call candidates. */
928 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
930 gcall *stmt = dyn_cast <gcall *> (gsi_stmt (i));
931 if (stmt && is_call_dce_candidate (stmt))
933 if (dump_file && (dump_flags & TDF_DETAILS))
935 fprintf (dump_file, "Found conditional dead call: ");
936 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
937 fprintf (dump_file, "\n");
939 if (!cond_dead_built_in_calls.exists ())
940 cond_dead_built_in_calls.create (64);
941 cond_dead_built_in_calls.safe_push (stmt);
946 if (!cond_dead_built_in_calls.exists ())
947 return 0;
949 something_changed
950 = shrink_wrap_conditional_dead_built_in_calls (cond_dead_built_in_calls);
952 if (something_changed)
954 free_dominance_info (CDI_DOMINATORS);
955 free_dominance_info (CDI_POST_DOMINATORS);
956 /* As we introduced new control-flow we need to insert PHI-nodes
957 for the call-clobbers of the remaining call. */
958 mark_virtual_operands_for_renaming (fun);
959 return TODO_update_ssa;
962 return 0;
965 } // anon namespace
967 gimple_opt_pass *
968 make_pass_call_cdce (gcc::context *ctxt)
970 return new pass_call_cdce (ctxt);