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