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