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