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