optabs-query.h (get_vcond_mask_icode): New.
[gcc.git] / gcc / tree-call-cdce.c
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>
4
5 This file is part of GCC.
6
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.
11
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.
16
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/>. */
20
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 "cfghooks.h"
28 #include "tree-pass.h"
29 #include "ssa.h"
30 #include "gimple-pretty-print.h"
31 #include "fold-const.h"
32 #include "stor-layout.h"
33 #include "gimple-iterator.h"
34 #include "tree-cfg.h"
35 #include "tree-into-ssa.h"
36 \f
37
38 /* Conditional dead call elimination
39
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:
47
48 built_in_call (args)
49 ==>
50 if (error_cond (args))
51 built_in_call (args)
52
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.
62
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).
66
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). */
75
76
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. */
84
85 struct inp_domain
86 {
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 };
94
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. */
102
103 static inp_domain
104 get_domain (int lb, bool has_lb, bool lb_inclusive,
105 int ub, bool has_ub, bool ub_inclusive)
106 {
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;
115 }
116
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. */
125
126 static bool
127 check_target_format (tree arg)
128 {
129 tree type;
130 machine_mode mode;
131 const struct real_format *rfmt;
132
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;
155
156 return false;
157 }
158
159 \f
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. */
165
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
169
170 static bool
171 check_pow (gcall *pow_call)
172 {
173 tree base, expn;
174 enum tree_code bc, ec;
175
176 if (gimple_call_num_args (pow_call) != 2)
177 return false;
178
179 base = gimple_call_arg (pow_call, 0);
180 expn = gimple_call_arg (pow_call, 1);
181
182 if (!check_target_format (expn))
183 return false;
184
185 bc = TREE_CODE (base);
186 ec = TREE_CODE (expn);
187
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;
192
193 if (bc == REAL_CST)
194 {
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_equal (&bcv, &dconst1))
199 return false;
200 if (real_less (&bcv, &dconst1))
201 return false;
202 real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, UNSIGNED);
203 if (real_less (&mv, &bcv))
204 return false;
205 return true;
206 }
207 else if (bc == SSA_NAME)
208 {
209 tree base_val0, type;
210 gimple *base_def;
211 int bit_sz;
212
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;
218
219 if (gimple_assign_rhs_code (base_def) != FLOAT_EXPR)
220 return false;
221 base_val0 = gimple_assign_rhs1 (base_def);
222
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;
232
233 return true;
234 }
235 else
236 return false;
237 }
238
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. */
243
244 static bool
245 check_builtin_call (gcall *bcall)
246 {
247 tree arg;
248
249 arg = gimple_call_arg (bcall, 0);
250 return check_target_format (arg);
251 }
252
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. */
256
257 static bool
258 is_call_dce_candidate (gcall *call)
259 {
260 tree fn;
261 enum built_in_function fnc;
262
263 /* Only potentially dead calls are considered. */
264 if (gimple_call_lhs (call))
265 return false;
266
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;
272
273 fnc = DECL_FUNCTION_CODE (fn);
274 switch (fnc)
275 {
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;
303 }
304
305 return false;
306 }
307
308 \f
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. */
318
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)
326 {
327 tree lbub_real_cst, lbub_cst, float_type;
328 tree temp, tempn, tempc, tempcn;
329 gassign *stmt1;
330 gassign *stmt2;
331 gcond *stmt3;
332
333 float_type = TREE_TYPE (arg);
334 lbub_cst = build_int_cst (integer_type_node, lbub);
335 lbub_real_cst = build_real_from_int_cst (float_type, lbub_cst);
336
337 temp = create_tmp_var (float_type, temp_name1);
338 stmt1 = gimple_build_assign (temp, arg);
339 tempn = make_ssa_name (temp, stmt1);
340 gimple_assign_set_lhs (stmt1, tempn);
341
342 tempc = create_tmp_var (boolean_type_node, temp_name2);
343 stmt2 = gimple_build_assign (tempc,
344 fold_build2 (tcode,
345 boolean_type_node,
346 tempn, lbub_real_cst));
347 tempcn = make_ssa_name (tempc, stmt2);
348 gimple_assign_set_lhs (stmt2, tempcn);
349
350 stmt3 = gimple_build_cond_from_tree (tempcn, NULL_TREE, NULL_TREE);
351 conds.quick_push (stmt1);
352 conds.quick_push (stmt2);
353 conds.quick_push (stmt3);
354 (*nconds)++;
355 }
356
357 /* A helper function to generate GIMPLE statements for
358 out of input domain check. ARG is the call argument
359 to be runtime checked, DOMAIN holds the valid domain
360 for the given function, CONDS points to the vector
361 holding the result GIMPLE statements. *NCONDS is
362 the number of logical comparisons. This function
363 produces no more than two logical comparisons, one
364 for lower bound check, one for upper bound check. */
365
366 static void
367 gen_conditions_for_domain (tree arg, inp_domain domain,
368 vec<gimple *> conds,
369 unsigned *nconds)
370 {
371 if (domain.has_lb)
372 gen_one_condition (arg, domain.lb,
373 (domain.is_lb_inclusive
374 ? LT_EXPR : LE_EXPR),
375 "DCE_COND_LB", "DCE_COND_LB_TEST",
376 conds, nconds);
377
378 if (domain.has_ub)
379 {
380 /* Now push a separator. */
381 if (domain.has_lb)
382 conds.quick_push (NULL);
383
384 gen_one_condition (arg, domain.ub,
385 (domain.is_ub_inclusive
386 ? GT_EXPR : GE_EXPR),
387 "DCE_COND_UB", "DCE_COND_UB_TEST",
388 conds, nconds);
389 }
390 }
391
392
393 /* A helper function to generate condition
394 code for the y argument in call pow (some_const, y).
395 See candidate selection in check_pow. Since the
396 candidates' base values have a limited range,
397 the guarded code generated for y are simple:
398 if (y > max_y)
399 pow (const, y);
400 Note max_y can be computed separately for each
401 const base, but in this implementation, we
402 choose to compute it using the max base
403 in the allowed range for the purpose of
404 simplicity. BASE is the constant base value,
405 EXPN is the expression for the exponent argument,
406 *CONDS is the vector to hold resulting statements,
407 and *NCONDS is the number of logical conditions. */
408
409 static void
410 gen_conditions_for_pow_cst_base (tree base, tree expn,
411 vec<gimple *> conds,
412 unsigned *nconds)
413 {
414 inp_domain exp_domain;
415 /* Validate the range of the base constant to make
416 sure it is consistent with check_pow. */
417 REAL_VALUE_TYPE mv;
418 REAL_VALUE_TYPE bcv = TREE_REAL_CST (base);
419 gcc_assert (!real_equal (&bcv, &dconst1)
420 && !real_less (&bcv, &dconst1));
421 real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, UNSIGNED);
422 gcc_assert (!real_less (&mv, &bcv));
423
424 exp_domain = get_domain (0, false, false,
425 127, true, false);
426
427 gen_conditions_for_domain (expn, exp_domain,
428 conds, nconds);
429 }
430
431 /* Generate error condition code for pow calls with
432 non constant base values. The candidates selected
433 have their base argument value converted from
434 integer (see check_pow) value (1, 2, 4 bytes), and
435 the max exp value is computed based on the size
436 of the integer type (i.e. max possible base value).
437 The resulting input domain for exp argument is thus
438 conservative (smaller than the max value allowed by
439 the runtime value of the base). BASE is the integer
440 base value, EXPN is the expression for the exponent
441 argument, *CONDS is the vector to hold resulting
442 statements, and *NCONDS is the number of logical
443 conditions. */
444
445 static void
446 gen_conditions_for_pow_int_base (tree base, tree expn,
447 vec<gimple *> conds,
448 unsigned *nconds)
449 {
450 gimple *base_def;
451 tree base_val0;
452 tree int_type;
453 tree temp, tempn;
454 tree cst0;
455 gimple *stmt1, *stmt2;
456 int bit_sz, max_exp;
457 inp_domain exp_domain;
458
459 base_def = SSA_NAME_DEF_STMT (base);
460 base_val0 = gimple_assign_rhs1 (base_def);
461 int_type = TREE_TYPE (base_val0);
462 bit_sz = TYPE_PRECISION (int_type);
463 gcc_assert (bit_sz > 0
464 && bit_sz <= MAX_BASE_INT_BIT_SIZE);
465
466 /* Determine the max exp argument value according to
467 the size of the base integer. The max exp value
468 is conservatively estimated assuming IEEE754 double
469 precision format. */
470 if (bit_sz == 8)
471 max_exp = 128;
472 else if (bit_sz == 16)
473 max_exp = 64;
474 else
475 {
476 gcc_assert (bit_sz == MAX_BASE_INT_BIT_SIZE);
477 max_exp = 32;
478 }
479
480 /* For pow ((double)x, y), generate the following conditions:
481 cond 1:
482 temp1 = x;
483 if (temp1 <= 0)
484
485 cond 2:
486 temp2 = y;
487 if (temp2 > max_exp_real_cst) */
488
489 /* Generate condition in reverse order -- first
490 the condition for the exp argument. */
491
492 exp_domain = get_domain (0, false, false,
493 max_exp, true, true);
494
495 gen_conditions_for_domain (expn, exp_domain,
496 conds, nconds);
497
498 /* Now generate condition for the base argument.
499 Note it does not use the helper function
500 gen_conditions_for_domain because the base
501 type is integer. */
502
503 /* Push a separator. */
504 conds.quick_push (NULL);
505
506 temp = create_tmp_var (int_type, "DCE_COND1");
507 cst0 = build_int_cst (int_type, 0);
508 stmt1 = gimple_build_assign (temp, base_val0);
509 tempn = make_ssa_name (temp, stmt1);
510 gimple_assign_set_lhs (stmt1, tempn);
511 stmt2 = gimple_build_cond (LE_EXPR, tempn, cst0, NULL_TREE, NULL_TREE);
512
513 conds.quick_push (stmt1);
514 conds.quick_push (stmt2);
515 (*nconds)++;
516 }
517
518 /* Method to generate conditional statements for guarding conditionally
519 dead calls to pow. One or more statements can be generated for
520 each logical condition. Statement groups of different conditions
521 are separated by a NULL tree and they are stored in the vec
522 conds. The number of logical conditions are stored in *nconds.
523
524 See C99 standard, 7.12.7.4:2, for description of pow (x, y).
525 The precise condition for domain errors are complex. In this
526 implementation, a simplified (but conservative) valid domain
527 for x and y are used: x is positive to avoid dom errors, while
528 y is smaller than a upper bound (depending on x) to avoid range
529 errors. Runtime code is generated to check x (if not constant)
530 and y against the valid domain. If it is out, jump to the call,
531 otherwise the call is bypassed. POW_CALL is the call statement,
532 *CONDS is a vector holding the resulting condition statements,
533 and *NCONDS is the number of logical conditions. */
534
535 static void
536 gen_conditions_for_pow (gcall *pow_call, vec<gimple *> conds,
537 unsigned *nconds)
538 {
539 tree base, expn;
540 enum tree_code bc;
541
542 gcc_checking_assert (check_pow (pow_call));
543
544 *nconds = 0;
545
546 base = gimple_call_arg (pow_call, 0);
547 expn = gimple_call_arg (pow_call, 1);
548
549 bc = TREE_CODE (base);
550
551 if (bc == REAL_CST)
552 gen_conditions_for_pow_cst_base (base, expn, conds, nconds);
553 else if (bc == SSA_NAME)
554 gen_conditions_for_pow_int_base (base, expn, conds, nconds);
555 else
556 gcc_unreachable ();
557 }
558
559 /* A helper routine to help computing the valid input domain
560 for a builtin function. See C99 7.12.7 for details. In this
561 implementation, we only handle single region domain. The
562 resulting region can be conservative (smaller) than the actual
563 one and rounded to integers. Some of the bounds are documented
564 in the standard, while other limit constants are computed
565 assuming IEEE floating point format (for SF and DF modes).
566 Since IEEE only sets minimum requirements for long double format,
567 different long double formats exist under different implementations
568 (e.g, 64 bit double precision (DF), 80 bit double-extended
569 precision (XF), and 128 bit quad precision (QF) ). For simplicity,
570 in this implementation, the computed bounds for long double assume
571 64 bit format (DF), and are therefore conservative. Another
572 assumption is that single precision float type is always SF mode,
573 and double type is DF mode. This function is quite
574 implementation specific, so it may not be suitable to be part of
575 builtins.c. This needs to be revisited later to see if it can
576 be leveraged in x87 assembly expansion. */
577
578 static inp_domain
579 get_no_error_domain (enum built_in_function fnc)
580 {
581 switch (fnc)
582 {
583 /* Trig functions: return [-1, +1] */
584 CASE_FLT_FN (BUILT_IN_ACOS):
585 CASE_FLT_FN (BUILT_IN_ASIN):
586 return get_domain (-1, true, true,
587 1, true, true);
588 /* Hyperbolic functions. */
589 CASE_FLT_FN (BUILT_IN_ACOSH):
590 /* acosh: [1, +inf) */
591 return get_domain (1, true, true,
592 1, false, false);
593 CASE_FLT_FN (BUILT_IN_ATANH):
594 /* atanh: (-1, +1) */
595 return get_domain (-1, true, false,
596 1, true, false);
597 case BUILT_IN_COSHF:
598 case BUILT_IN_SINHF:
599 /* coshf: (-89, +89) */
600 return get_domain (-89, true, false,
601 89, true, false);
602 case BUILT_IN_COSH:
603 case BUILT_IN_SINH:
604 case BUILT_IN_COSHL:
605 case BUILT_IN_SINHL:
606 /* cosh: (-710, +710) */
607 return get_domain (-710, true, false,
608 710, true, false);
609 /* Log functions: (0, +inf) */
610 CASE_FLT_FN (BUILT_IN_LOG):
611 CASE_FLT_FN (BUILT_IN_LOG2):
612 CASE_FLT_FN (BUILT_IN_LOG10):
613 return get_domain (0, true, false,
614 0, false, false);
615 CASE_FLT_FN (BUILT_IN_LOG1P):
616 return get_domain (-1, true, false,
617 0, false, false);
618 /* Exp functions. */
619 case BUILT_IN_EXPF:
620 case BUILT_IN_EXPM1F:
621 /* expf: (-inf, 88) */
622 return get_domain (-1, false, false,
623 88, true, false);
624 case BUILT_IN_EXP:
625 case BUILT_IN_EXPM1:
626 case BUILT_IN_EXPL:
627 case BUILT_IN_EXPM1L:
628 /* exp: (-inf, 709) */
629 return get_domain (-1, false, false,
630 709, true, false);
631 case BUILT_IN_EXP2F:
632 /* exp2f: (-inf, 128) */
633 return get_domain (-1, false, false,
634 128, true, false);
635 case BUILT_IN_EXP2:
636 case BUILT_IN_EXP2L:
637 /* exp2: (-inf, 1024) */
638 return get_domain (-1, false, false,
639 1024, true, false);
640 case BUILT_IN_EXP10F:
641 case BUILT_IN_POW10F:
642 /* exp10f: (-inf, 38) */
643 return get_domain (-1, false, false,
644 38, true, false);
645 case BUILT_IN_EXP10:
646 case BUILT_IN_POW10:
647 case BUILT_IN_EXP10L:
648 case BUILT_IN_POW10L:
649 /* exp10: (-inf, 308) */
650 return get_domain (-1, false, false,
651 308, true, false);
652 /* sqrt: [0, +inf) */
653 CASE_FLT_FN (BUILT_IN_SQRT):
654 return get_domain (0, true, true,
655 0, false, false);
656 default:
657 gcc_unreachable ();
658 }
659
660 gcc_unreachable ();
661 }
662
663 /* The function to generate shrink wrap conditions for a partially
664 dead builtin call whose return value is not used anywhere,
665 but has to be kept live due to potential error condition.
666 BI_CALL is the builtin call, CONDS is the vector of statements
667 for condition code, NCODES is the pointer to the number of
668 logical conditions. Statements belonging to different logical
669 condition are separated by NULL tree in the vector. */
670
671 static void
672 gen_shrink_wrap_conditions (gcall *bi_call, vec<gimple *> conds,
673 unsigned int *nconds)
674 {
675 gcall *call;
676 tree fn;
677 enum built_in_function fnc;
678
679 gcc_assert (nconds && conds.exists ());
680 gcc_assert (conds.length () == 0);
681 gcc_assert (is_gimple_call (bi_call));
682
683 call = bi_call;
684 fn = gimple_call_fndecl (call);
685 gcc_assert (fn && DECL_BUILT_IN (fn));
686 fnc = DECL_FUNCTION_CODE (fn);
687 *nconds = 0;
688
689 if (fnc == BUILT_IN_POW)
690 gen_conditions_for_pow (call, conds, nconds);
691 else
692 {
693 tree arg;
694 inp_domain domain = get_no_error_domain (fnc);
695 *nconds = 0;
696 arg = gimple_call_arg (bi_call, 0);
697 gen_conditions_for_domain (arg, domain, conds, nconds);
698 }
699
700 return;
701 }
702
703
704 /* Probability of the branch (to the call) is taken. */
705 #define ERR_PROB 0.01
706
707 /* The function to shrink wrap a partially dead builtin call
708 whose return value is not used anywhere, but has to be kept
709 live due to potential error condition. Returns true if the
710 transformation actually happens. */
711
712 static bool
713 shrink_wrap_one_built_in_call (gcall *bi_call)
714 {
715 gimple_stmt_iterator bi_call_bsi;
716 basic_block bi_call_bb, join_tgt_bb, guard_bb;
717 edge join_tgt_in_edge_from_call, join_tgt_in_edge_fall_thru;
718 edge bi_call_in_edge0, guard_bb_in_edge;
719 unsigned tn_cond_stmts, nconds;
720 unsigned ci;
721 gimple *cond_expr = NULL;
722 gimple *cond_expr_start;
723
724 auto_vec<gimple *, 12> conds;
725 gen_shrink_wrap_conditions (bi_call, conds, &nconds);
726
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;
733
734 /* The cfg we want to create looks like this:
735
736 [guard n-1] <- guard_bb (old block)
737 | \
738 | [guard n-2] }
739 | / \ }
740 | / ... } new blocks
741 | / [guard 0] }
742 | / / | }
743 [ call ] | <- bi_call_bb }
744 | \ |
745 | \ |
746 | [ join ] <- join_tgt_bb (old iff call must end bb)
747 |
748 possible EH edges (only if [join] is old)
749
750 When [join] is new, the immediate dominators for these blocks are:
751
752 1. [guard n-1]: unchanged
753 2. [call]: [guard n-1]
754 3. [guard m]: [guard m+1] for 0 <= m <= n-2
755 4. [join]: [guard n-1]
756
757 We punt for the more complex case case of [join] being old and
758 simply free the dominance info. We also punt on postdominators,
759 which aren't expected to be available at this point anyway. */
760 bi_call_bb = gimple_bb (bi_call);
761
762 /* Now find the join target bb -- split bi_call_bb if needed. */
763 if (stmt_ends_bb_p (bi_call))
764 {
765 /* If the call must be the last in the bb, don't split the block,
766 it could e.g. have EH edges. */
767 join_tgt_in_edge_from_call = find_fallthru_edge (bi_call_bb->succs);
768 if (join_tgt_in_edge_from_call == NULL)
769 return false;
770 free_dominance_info (CDI_DOMINATORS);
771 }
772 else
773 join_tgt_in_edge_from_call = split_block (bi_call_bb, bi_call);
774
775 bi_call_bsi = gsi_for_stmt (bi_call);
776
777 join_tgt_bb = join_tgt_in_edge_from_call->dest;
778
779 /* Now it is time to insert the first conditional expression
780 into bi_call_bb and split this bb so that bi_call is
781 shrink-wrapped. */
782 tn_cond_stmts = conds.length ();
783 cond_expr = NULL;
784 cond_expr_start = conds[0];
785 for (ci = 0; ci < tn_cond_stmts; ci++)
786 {
787 gimple *c = conds[ci];
788 gcc_assert (c || ci != 0);
789 if (!c)
790 break;
791 gsi_insert_before (&bi_call_bsi, c, GSI_SAME_STMT);
792 cond_expr = c;
793 }
794 nconds--;
795 ci++;
796 gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND);
797
798 bi_call_in_edge0 = split_block (bi_call_bb, cond_expr);
799 bi_call_in_edge0->flags &= ~EDGE_FALLTHRU;
800 bi_call_in_edge0->flags |= EDGE_TRUE_VALUE;
801 guard_bb = bi_call_bb;
802 bi_call_bb = bi_call_in_edge0->dest;
803 join_tgt_in_edge_fall_thru = make_edge (guard_bb, join_tgt_bb,
804 EDGE_FALSE_VALUE);
805
806 bi_call_in_edge0->probability = REG_BR_PROB_BASE * ERR_PROB;
807 bi_call_in_edge0->count =
808 apply_probability (guard_bb->count,
809 bi_call_in_edge0->probability);
810 join_tgt_in_edge_fall_thru->probability =
811 inverse_probability (bi_call_in_edge0->probability);
812 join_tgt_in_edge_fall_thru->count =
813 guard_bb->count - bi_call_in_edge0->count;
814
815 /* Code generation for the rest of the conditions */
816 while (nconds > 0)
817 {
818 unsigned ci0;
819 edge bi_call_in_edge;
820 gimple_stmt_iterator guard_bsi = gsi_for_stmt (cond_expr_start);
821 ci0 = ci;
822 cond_expr_start = conds[ci0];
823 for (; ci < tn_cond_stmts; ci++)
824 {
825 gimple *c = conds[ci];
826 gcc_assert (c || ci != ci0);
827 if (!c)
828 break;
829 gsi_insert_before (&guard_bsi, c, GSI_SAME_STMT);
830 cond_expr = c;
831 }
832 nconds--;
833 ci++;
834 gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND);
835 guard_bb_in_edge = split_block (guard_bb, cond_expr);
836 guard_bb_in_edge->flags &= ~EDGE_FALLTHRU;
837 guard_bb_in_edge->flags |= EDGE_FALSE_VALUE;
838
839 bi_call_in_edge = make_edge (guard_bb, bi_call_bb, EDGE_TRUE_VALUE);
840
841 bi_call_in_edge->probability = REG_BR_PROB_BASE * ERR_PROB;
842 bi_call_in_edge->count =
843 apply_probability (guard_bb->count,
844 bi_call_in_edge->probability);
845 guard_bb_in_edge->probability =
846 inverse_probability (bi_call_in_edge->probability);
847 guard_bb_in_edge->count = guard_bb->count - bi_call_in_edge->count;
848 }
849
850 if (dom_info_available_p (CDI_DOMINATORS))
851 {
852 /* The split_blocks leave [guard 0] as the immediate dominator
853 of [call] and [call] as the immediate dominator of [join].
854 Fix them up. */
855 set_immediate_dominator (CDI_DOMINATORS, bi_call_bb, guard_bb);
856 set_immediate_dominator (CDI_DOMINATORS, join_tgt_bb, guard_bb);
857 }
858
859 if (dump_file && (dump_flags & TDF_DETAILS))
860 {
861 location_t loc;
862 loc = gimple_location (bi_call);
863 fprintf (dump_file,
864 "%s:%d: note: function call is shrink-wrapped"
865 " into error conditions.\n",
866 LOCATION_FILE (loc), LOCATION_LINE (loc));
867 }
868
869 return true;
870 }
871
872 /* The top level function for conditional dead code shrink
873 wrapping transformation. */
874
875 static bool
876 shrink_wrap_conditional_dead_built_in_calls (vec<gcall *> calls)
877 {
878 bool changed = false;
879 unsigned i = 0;
880
881 unsigned n = calls.length ();
882 if (n == 0)
883 return false;
884
885 for (; i < n ; i++)
886 {
887 gcall *bi_call = calls[i];
888 changed |= shrink_wrap_one_built_in_call (bi_call);
889 }
890
891 return changed;
892 }
893
894 namespace {
895
896 const pass_data pass_data_call_cdce =
897 {
898 GIMPLE_PASS, /* type */
899 "cdce", /* name */
900 OPTGROUP_NONE, /* optinfo_flags */
901 TV_TREE_CALL_CDCE, /* tv_id */
902 ( PROP_cfg | PROP_ssa ), /* properties_required */
903 0, /* properties_provided */
904 0, /* properties_destroyed */
905 0, /* todo_flags_start */
906 0, /* todo_flags_finish */
907 };
908
909 class pass_call_cdce : public gimple_opt_pass
910 {
911 public:
912 pass_call_cdce (gcc::context *ctxt)
913 : gimple_opt_pass (pass_data_call_cdce, ctxt)
914 {}
915
916 /* opt_pass methods: */
917 virtual bool gate (function *fun)
918 {
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
923 && optimize_function_for_speed_p (fun);
924 }
925
926 virtual unsigned int execute (function *);
927
928 }; // class pass_call_cdce
929
930 unsigned int
931 pass_call_cdce::execute (function *fun)
932 {
933 basic_block bb;
934 gimple_stmt_iterator i;
935 bool something_changed = false;
936 auto_vec<gcall *> cond_dead_built_in_calls;
937 FOR_EACH_BB_FN (bb, fun)
938 {
939 /* Collect dead call candidates. */
940 for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
941 {
942 gcall *stmt = dyn_cast <gcall *> (gsi_stmt (i));
943 if (stmt && is_call_dce_candidate (stmt))
944 {
945 if (dump_file && (dump_flags & TDF_DETAILS))
946 {
947 fprintf (dump_file, "Found conditional dead call: ");
948 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
949 fprintf (dump_file, "\n");
950 }
951 if (!cond_dead_built_in_calls.exists ())
952 cond_dead_built_in_calls.create (64);
953 cond_dead_built_in_calls.safe_push (stmt);
954 }
955 }
956 }
957
958 if (!cond_dead_built_in_calls.exists ())
959 return 0;
960
961 something_changed
962 = shrink_wrap_conditional_dead_built_in_calls (cond_dead_built_in_calls);
963
964 if (something_changed)
965 {
966 free_dominance_info (CDI_POST_DOMINATORS);
967 /* As we introduced new control-flow we need to insert PHI-nodes
968 for the call-clobbers of the remaining call. */
969 mark_virtual_operands_for_renaming (fun);
970 return TODO_update_ssa;
971 }
972
973 return 0;
974 }
975
976 } // anon namespace
977
978 gimple_opt_pass *
979 make_pass_call_cdce (gcc::context *ctxt)
980 {
981 return new pass_call_cdce (ctxt);
982 }