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