Line data Source code
1 : /* Conditional Dead Call Elimination pass for the GNU compiler.
2 : Copyright (C) 2008-2026 Free Software Foundation, Inc.
3 : Contributed by Xinliang David Li <davidxl@google.com>
4 :
5 : This file is part of GCC.
6 :
7 : GCC is free software; you can redistribute it and/or modify it
8 : under the terms of the GNU General Public License as published by the
9 : Free Software Foundation; either version 3, or (at your option) any
10 : later version.
11 :
12 : GCC is distributed in the hope that it will be useful, but WITHOUT
13 : ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 : FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 : for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with GCC; see the file COPYING3. If not see
19 : <http://www.gnu.org/licenses/>. */
20 :
21 : #include "config.h"
22 : #include "system.h"
23 : #include "coretypes.h"
24 : #include "backend.h"
25 : #include "tree.h"
26 : #include "gimple.h"
27 : #include "cfghooks.h"
28 : #include "tree-pass.h"
29 : #include "ssa.h"
30 : #include "gimple-pretty-print.h"
31 : #include "fold-const.h"
32 : #include "stor-layout.h"
33 : #include "gimple-iterator.h"
34 : #include "tree-cfg.h"
35 : #include "tree-into-ssa.h"
36 : #include "builtins.h"
37 : #include "internal-fn.h"
38 : #include "tree-dfa.h"
39 : #include "tree-eh.h"
40 :
41 :
42 : /* This pass serves two closely-related purposes:
43 :
44 : 1. It conditionally executes calls that set errno if (a) the result of
45 : the call is unused and (b) a simple range check on the arguments can
46 : detect most cases where errno does not need to be set.
47 :
48 : This is the "conditional dead-code elimination" that gave the pass
49 : its original name, since the call is dead for most argument values.
50 : The calls for which it helps are usually part of the C++ abstraction
51 : penalty exposed after inlining.
52 :
53 : 2. It looks for calls to built-in functions that set errno and whose
54 : result is used. It checks whether there is an associated internal
55 : function that doesn't set errno and whether the target supports
56 : that internal function. If so, the pass uses the internal function
57 : to compute the result of the built-in function but still arranges
58 : for errno to be set when necessary. There are two ways of setting
59 : errno:
60 :
61 : a. by protecting the original call with the same argument checks as (1)
62 :
63 : b. by protecting the original call with a check that the result
64 : of the internal function is not equal to itself (i.e. is NaN).
65 :
66 : (b) requires that NaNs are the only erroneous results. It is not
67 : appropriate for functions like log, which returns ERANGE for zero
68 : arguments. (b) is also likely to perform worse than (a) because it
69 : requires the result to be calculated first. The pass therefore uses
70 : (a) when it can and uses (b) as a fallback.
71 :
72 : For (b) the pass can replace the original call with a call to
73 : IFN_SET_EDOM, if the target supports direct assignments to errno.
74 :
75 : In both cases, arguments that require errno to be set should occur
76 : rarely in practice. Checks of the errno result should also be rare,
77 : but the compiler would need powerful interprocedural analysis to
78 : prove that errno is not checked. It's much easier to add argument
79 : checks or result checks instead.
80 :
81 : An example of (1) is:
82 :
83 : log (x); // Mostly dead call
84 : ==>
85 : if (__builtin_islessequal (x, 0))
86 : log (x);
87 :
88 : With this change, call to log (x) is effectively eliminated, as
89 : in the majority of the cases, log won't be called with x out of
90 : range. The branch is totally predictable, so the branch cost
91 : is low.
92 :
93 : An example of (2) is:
94 :
95 : y = sqrt (x);
96 : ==>
97 : if (__builtin_isless (x, 0))
98 : y = sqrt (x);
99 : else
100 : y = IFN_SQRT (x);
101 : In the vast majority of cases we should then never need to call sqrt.
102 :
103 : Note that library functions are not supposed to clear errno to zero without
104 : error. See IEEE Std 1003.1, section 2.3 Error Numbers, and section 7.5:3 of
105 : ISO/IEC 9899 (C99).
106 :
107 : The condition wrapping the builtin call is conservatively set to avoid too
108 : aggressive (wrong) shrink wrapping. */
109 :
110 :
111 : /* A structure for representing input domain of
112 : a function argument in integer. If the lower
113 : bound is -inf, has_lb is set to false. If the
114 : upper bound is +inf, has_ub is false.
115 : is_lb_inclusive and is_ub_inclusive are flags
116 : to indicate if lb and ub value are inclusive
117 : respectively. */
118 :
119 : struct inp_domain
120 : {
121 : int lb;
122 : int ub;
123 : bool has_lb;
124 : bool has_ub;
125 : bool is_lb_inclusive;
126 : bool is_ub_inclusive;
127 : };
128 :
129 : /* A helper function to construct and return an input
130 : domain object. LB is the lower bound, HAS_LB is
131 : a boolean flag indicating if the lower bound exists,
132 : and LB_INCLUSIVE is a boolean flag indicating if the
133 : lower bound is inclusive or not. UB, HAS_UB, and
134 : UB_INCLUSIVE have the same meaning, but for upper
135 : bound of the domain. */
136 :
137 : static inp_domain
138 2650 : get_domain (int lb, bool has_lb, bool lb_inclusive,
139 : int ub, bool has_ub, bool ub_inclusive)
140 : {
141 2650 : inp_domain domain;
142 2650 : domain.lb = lb;
143 2650 : domain.has_lb = has_lb;
144 2650 : domain.is_lb_inclusive = lb_inclusive;
145 2650 : domain.ub = ub;
146 2650 : domain.has_ub = has_ub;
147 2650 : domain.is_ub_inclusive = ub_inclusive;
148 2650 : return domain;
149 : }
150 :
151 : /* A helper function to check the target format for the
152 : argument type. In this implementation, only IEEE formats
153 : are supported. ARG is the call argument to be checked.
154 : Returns true if the format is supported. To support other
155 : target formats, function get_no_error_domain needs to be
156 : enhanced to have range bounds properly computed. Since
157 : the check is cheap (very small number of candidates
158 : to be checked), the result is not cached for each float type. */
159 :
160 : static bool
161 4371 : check_target_format (tree arg)
162 : {
163 4371 : tree type;
164 4371 : machine_mode mode;
165 4371 : const struct real_format *rfmt;
166 :
167 4371 : type = TREE_TYPE (arg);
168 4371 : mode = TYPE_MODE (type);
169 4371 : rfmt = REAL_MODE_FORMAT (mode);
170 4371 : if ((mode == SFmode
171 1335 : && (rfmt == &ieee_single_format || rfmt == &mips_single_format
172 0 : || rfmt == &motorola_single_format))
173 3036 : || (mode == DFmode
174 2104 : && (rfmt == &ieee_double_format || rfmt == &mips_double_format
175 0 : || rfmt == &motorola_double_format))
176 : /* For long double, we cannot really check XFmode
177 : which is only defined on intel platforms.
178 : Candidate pre-selection using builtin function
179 : code guarantees that we are checking formats
180 : for long double modes: double, quad, and extended. */
181 932 : || (mode != SFmode && mode != DFmode
182 932 : && (rfmt == &ieee_quad_format
183 905 : || rfmt == &mips_quad_format
184 905 : || rfmt == &ieee_extended_motorola_format
185 905 : || rfmt == &ieee_extended_intel_96_format
186 899 : || rfmt == &ieee_extended_intel_128_format
187 14 : || rfmt == &ieee_extended_intel_96_round_53_format)))
188 4357 : return true;
189 :
190 : return false;
191 : }
192 :
193 :
194 : /* A helper function to help select calls to pow that are suitable for
195 : conditional DCE transformation. It looks for pow calls that can be
196 : guided with simple conditions. Such calls either have constant base
197 : values or base values converted from integers. Returns true if
198 : the pow call POW_CALL is a candidate. */
199 :
200 : /* The maximum integer bit size for base argument of a pow call
201 : that is suitable for shrink-wrapping transformation. */
202 : #define MAX_BASE_INT_BIT_SIZE 32
203 :
204 : static bool
205 55 : check_pow (gcall *pow_call)
206 : {
207 55 : tree base, expn;
208 55 : enum tree_code bc, ec;
209 :
210 55 : if (gimple_call_num_args (pow_call) != 2)
211 : return false;
212 :
213 55 : base = gimple_call_arg (pow_call, 0);
214 55 : expn = gimple_call_arg (pow_call, 1);
215 :
216 55 : if (!check_target_format (expn))
217 : return false;
218 :
219 55 : bc = TREE_CODE (base);
220 55 : ec = TREE_CODE (expn);
221 :
222 : /* Folding candidates are not interesting.
223 : Can actually assert that it is already folded. */
224 55 : if (ec == REAL_CST && bc == REAL_CST)
225 : return false;
226 :
227 53 : if (bc == REAL_CST)
228 : {
229 : /* Only handle a fixed range of constant. */
230 28 : REAL_VALUE_TYPE mv;
231 28 : REAL_VALUE_TYPE bcv = TREE_REAL_CST (base);
232 28 : if (real_equal (&bcv, &dconst1))
233 : return false;
234 28 : if (real_less (&bcv, &dconst1))
235 : return false;
236 28 : real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, UNSIGNED);
237 28 : if (real_less (&mv, &bcv))
238 : return false;
239 : return true;
240 : }
241 25 : else if (bc == SSA_NAME)
242 : {
243 25 : tree base_val0, type;
244 25 : gimple *base_def;
245 25 : int bit_sz;
246 :
247 : /* Only handles cases where base value is converted
248 : from integer values. */
249 25 : base_def = SSA_NAME_DEF_STMT (base);
250 25 : if (gimple_code (base_def) != GIMPLE_ASSIGN)
251 : return false;
252 :
253 15 : if (gimple_assign_rhs_code (base_def) != FLOAT_EXPR)
254 : return false;
255 15 : base_val0 = gimple_assign_rhs1 (base_def);
256 :
257 15 : type = TREE_TYPE (base_val0);
258 15 : if (TREE_CODE (type) != INTEGER_TYPE)
259 : return false;
260 15 : bit_sz = TYPE_PRECISION (type);
261 : /* If the type of the base is too wide,
262 : the resulting shrink wrapping condition
263 : will be too conservative. */
264 15 : if (bit_sz != 8 && bit_sz != 16 && bit_sz != MAX_BASE_INT_BIT_SIZE)
265 : return false;
266 :
267 : return true;
268 : }
269 : else
270 : return false;
271 : }
272 :
273 : /* A helper function to help select candidate function calls that are
274 : suitable for conditional DCE. Candidate functions must have single
275 : valid input domain in this implementation except for pow (see check_pow).
276 : Returns true if the function call is a candidate. */
277 :
278 : static bool
279 4316 : check_builtin_call (gcall *bcall)
280 : {
281 4316 : tree arg;
282 :
283 4316 : arg = gimple_call_arg (bcall, 0);
284 4316 : return check_target_format (arg);
285 : }
286 :
287 : /* Return true if built-in function call CALL calls a math function
288 : and if we know how to test the range of its arguments to detect _most_
289 : situations in which errno is not set. The test must err on the side
290 : of treating non-erroneous values as potentially erroneous. */
291 :
292 : static bool
293 357028 : can_test_argument_range (gcall *call)
294 : {
295 357028 : switch (DECL_FUNCTION_CODE (gimple_call_fndecl (call)))
296 : {
297 : /* Trig functions. */
298 4316 : CASE_FLT_FN (BUILT_IN_ACOS):
299 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOS):
300 4316 : CASE_FLT_FN (BUILT_IN_ACOSPI):
301 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSPI):
302 4316 : CASE_FLT_FN (BUILT_IN_ASIN):
303 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASIN):
304 4316 : CASE_FLT_FN (BUILT_IN_ASINPI):
305 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASINPI):
306 : /* Hyperbolic functions. */
307 4316 : CASE_FLT_FN (BUILT_IN_ACOSH):
308 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSH):
309 4316 : CASE_FLT_FN (BUILT_IN_ATANH):
310 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATANH):
311 4316 : CASE_FLT_FN (BUILT_IN_COSH):
312 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COSH):
313 4316 : CASE_FLT_FN (BUILT_IN_SINH):
314 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SINH):
315 : /* Log functions. */
316 4316 : CASE_FLT_FN (BUILT_IN_LOG):
317 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG):
318 4316 : CASE_FLT_FN (BUILT_IN_LOG2):
319 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG2):
320 4316 : CASE_FLT_FN (BUILT_IN_LOG10):
321 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG10):
322 4316 : CASE_FLT_FN (BUILT_IN_LOG1P):
323 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG1P):
324 : /* Exp functions. */
325 4316 : CASE_FLT_FN (BUILT_IN_EXP):
326 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP):
327 4316 : CASE_FLT_FN (BUILT_IN_EXP2):
328 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXP2):
329 4316 : CASE_FLT_FN (BUILT_IN_EXP10):
330 4316 : CASE_FLT_FN (BUILT_IN_EXPM1):
331 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_EXPM1):
332 4316 : CASE_FLT_FN (BUILT_IN_POW10):
333 : /* Sqrt. */
334 4316 : CASE_FLT_FN (BUILT_IN_SQRT):
335 4316 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
336 4316 : return check_builtin_call (call);
337 : /* Special one: two argument pow. */
338 34 : case BUILT_IN_POW:
339 34 : return check_pow (call);
340 : default:
341 : break;
342 : }
343 :
344 : return false;
345 : }
346 :
347 : /* Return true if CALL can produce a domain error (EDOM) but can never
348 : produce a pole, range overflow or range underflow error (all ERANGE).
349 : This means that we can tell whether a function would have set errno
350 : by testing whether the result is a NaN. */
351 :
352 : static bool
353 373 : edom_only_function (gcall *call)
354 : {
355 373 : switch (DECL_FUNCTION_CODE (gimple_call_fndecl (call)))
356 : {
357 : CASE_FLT_FN (BUILT_IN_ACOS):
358 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOS):
359 : CASE_FLT_FN (BUILT_IN_ACOSPI):
360 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSPI):
361 : CASE_FLT_FN (BUILT_IN_ASIN):
362 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASIN):
363 : CASE_FLT_FN (BUILT_IN_ASINPI):
364 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASINPI):
365 : CASE_FLT_FN (BUILT_IN_COS):
366 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COS):
367 : CASE_FLT_FN (BUILT_IN_COSPI):
368 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_COSPI):
369 : CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
370 : CASE_FLT_FN (BUILT_IN_SIN):
371 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SIN):
372 : CASE_FLT_FN (BUILT_IN_SINPI):
373 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SINPI):
374 : CASE_FLT_FN (BUILT_IN_SQRT):
375 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
376 : CASE_FLT_FN (BUILT_IN_FMOD):
377 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_FMOD):
378 : CASE_FLT_FN (BUILT_IN_REMAINDER):
379 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_REMAINDER):
380 : return true;
381 :
382 95 : default:
383 95 : return false;
384 : }
385 : }
386 :
387 : /* Return true if it is structurally possible to guard CALL. */
388 :
389 : static bool
390 2789 : can_guard_call_p (gimple *call)
391 : {
392 2789 : return (!stmt_ends_bb_p (call)
393 2789 : || find_fallthru_edge (gimple_bb (call)->succs));
394 : }
395 :
396 : /* For a comparison code return the comparison code we should use if we don't
397 : HONOR_NANS. */
398 :
399 : static enum tree_code
400 8 : comparison_code_if_no_nans (tree_code code)
401 : {
402 8 : switch (code)
403 : {
404 : case UNLT_EXPR:
405 : return LT_EXPR;
406 4 : case UNGT_EXPR:
407 4 : return GT_EXPR;
408 0 : case UNLE_EXPR:
409 0 : return LE_EXPR;
410 0 : case UNGE_EXPR:
411 0 : return GE_EXPR;
412 0 : case UNEQ_EXPR:
413 0 : return EQ_EXPR;
414 0 : case LTGT_EXPR:
415 0 : return NE_EXPR;
416 :
417 0 : case LT_EXPR:
418 0 : case GT_EXPR:
419 0 : case LE_EXPR:
420 0 : case GE_EXPR:
421 0 : case EQ_EXPR:
422 0 : case NE_EXPR:
423 0 : return code;
424 :
425 0 : default:
426 0 : gcc_unreachable ();
427 : }
428 : }
429 :
430 : /* A helper function to generate gimple statements for one bound
431 : comparison, so that the built-in function is called whenever
432 : TCODE <ARG, LBUB> is *false*. TEMP_NAME1/TEMP_NAME2 are names
433 : of the temporaries, CONDS is a vector holding the produced GIMPLE
434 : statements, and NCONDS points to the variable holding the number of
435 : logical comparisons. CONDS is either empty or a list ended with a
436 : null tree. */
437 :
438 : static void
439 2822 : gen_one_condition (tree arg, int lbub,
440 : enum tree_code tcode,
441 : const char *temp_name1,
442 : const char *temp_name2,
443 : vec<gimple *> conds,
444 : unsigned *nconds)
445 : {
446 2822 : if (!HONOR_NANS (arg))
447 8 : tcode = comparison_code_if_no_nans (tcode);
448 :
449 2822 : tree lbub_real_cst, lbub_cst, float_type;
450 2822 : tree temp, tempn, tempc, tempcn;
451 2822 : gassign *stmt1;
452 2822 : gassign *stmt2;
453 2822 : gcond *stmt3;
454 :
455 2822 : float_type = TREE_TYPE (arg);
456 2822 : lbub_cst = build_int_cst (integer_type_node, lbub);
457 2822 : lbub_real_cst = build_real_from_int_cst (float_type, lbub_cst);
458 :
459 2822 : temp = create_tmp_var (float_type, temp_name1);
460 2822 : stmt1 = gimple_build_assign (temp, arg);
461 2822 : tempn = make_ssa_name (temp, stmt1);
462 2822 : gimple_assign_set_lhs (stmt1, tempn);
463 :
464 2822 : tempc = create_tmp_var (boolean_type_node, temp_name2);
465 2822 : stmt2 = gimple_build_assign (tempc,
466 : fold_build2 (tcode,
467 : boolean_type_node,
468 : tempn, lbub_real_cst));
469 2822 : tempcn = make_ssa_name (tempc, stmt2);
470 2822 : gimple_assign_set_lhs (stmt2, tempcn);
471 :
472 2822 : stmt3 = gimple_build_cond_from_tree (tempcn, NULL_TREE, NULL_TREE);
473 2822 : conds.quick_push (stmt1);
474 2822 : conds.quick_push (stmt2);
475 2822 : conds.quick_push (stmt3);
476 2822 : (*nconds)++;
477 2822 : }
478 :
479 : /* A helper function to generate GIMPLE statements for
480 : out of input domain check. ARG is the call argument
481 : to be runtime checked, DOMAIN holds the valid domain
482 : for the given function, CONDS points to the vector
483 : holding the result GIMPLE statements. *NCONDS is
484 : the number of logical comparisons. This function
485 : produces no more than two logical comparisons, one
486 : for lower bound check, one for upper bound check. */
487 :
488 : static void
489 2650 : gen_conditions_for_domain (tree arg, inp_domain domain,
490 : vec<gimple *> conds,
491 : unsigned *nconds)
492 : {
493 2650 : if (domain.has_lb)
494 2310 : gen_one_condition (arg, domain.lb,
495 2310 : (domain.is_lb_inclusive
496 : ? UNGE_EXPR : UNGT_EXPR),
497 : "DCE_COND_LB", "DCE_COND_LB_TEST",
498 : conds, nconds);
499 :
500 2650 : if (domain.has_ub)
501 : {
502 : /* Now push a separator. */
503 512 : if (domain.has_lb)
504 172 : conds.quick_push (NULL);
505 :
506 512 : gen_one_condition (arg, domain.ub,
507 512 : (domain.is_ub_inclusive
508 : ? UNLE_EXPR : UNLT_EXPR),
509 : "DCE_COND_UB", "DCE_COND_UB_TEST",
510 : conds, nconds);
511 : }
512 2650 : }
513 :
514 :
515 : /* A helper function to generate condition
516 : code for the y argument in call pow (some_const, y).
517 : See candidate selection in check_pow. Since the
518 : candidates' base values have a limited range,
519 : the guarded code generated for y are simple:
520 : if (__builtin_isgreater (y, max_y))
521 : pow (const, y);
522 : Note max_y can be computed separately for each
523 : const base, but in this implementation, we
524 : choose to compute it using the max base
525 : in the allowed range for the purpose of
526 : simplicity. BASE is the constant base value,
527 : EXPN is the expression for the exponent argument,
528 : *CONDS is the vector to hold resulting statements,
529 : and *NCONDS is the number of logical conditions. */
530 :
531 : static void
532 14 : gen_conditions_for_pow_cst_base (tree base, tree expn,
533 : vec<gimple *> conds,
534 : unsigned *nconds)
535 : {
536 14 : inp_domain exp_domain;
537 : /* Validate the range of the base constant to make
538 : sure it is consistent with check_pow. */
539 14 : REAL_VALUE_TYPE mv;
540 14 : REAL_VALUE_TYPE bcv = TREE_REAL_CST (base);
541 14 : gcc_assert (!real_equal (&bcv, &dconst1)
542 : && !real_less (&bcv, &dconst1));
543 14 : real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, UNSIGNED);
544 14 : gcc_assert (!real_less (&mv, &bcv));
545 :
546 14 : exp_domain = get_domain (0, false, false,
547 : 127, true, false);
548 :
549 14 : gen_conditions_for_domain (expn, exp_domain,
550 : conds, nconds);
551 14 : }
552 :
553 : /* Generate error condition code for pow calls with
554 : non constant base values. The candidates selected
555 : have their base argument value converted from
556 : integer (see check_pow) value (1, 2, 4 bytes), and
557 : the max exp value is computed based on the size
558 : of the integer type (i.e. max possible base value).
559 : The resulting input domain for exp argument is thus
560 : conservative (smaller than the max value allowed by
561 : the runtime value of the base). BASE is the integer
562 : base value, EXPN is the expression for the exponent
563 : argument, *CONDS is the vector to hold resulting
564 : statements, and *NCONDS is the number of logical
565 : conditions. */
566 :
567 : static void
568 7 : gen_conditions_for_pow_int_base (tree base, tree expn,
569 : vec<gimple *> conds,
570 : unsigned *nconds)
571 : {
572 7 : gimple *base_def;
573 7 : tree base_val0;
574 7 : tree int_type;
575 7 : tree temp, tempn;
576 7 : tree cst0;
577 7 : gimple *stmt1, *stmt2;
578 7 : int bit_sz, max_exp;
579 7 : inp_domain exp_domain;
580 :
581 7 : base_def = SSA_NAME_DEF_STMT (base);
582 7 : base_val0 = gimple_assign_rhs1 (base_def);
583 7 : int_type = TREE_TYPE (base_val0);
584 7 : bit_sz = TYPE_PRECISION (int_type);
585 7 : gcc_assert (bit_sz > 0
586 : && bit_sz <= MAX_BASE_INT_BIT_SIZE);
587 :
588 : /* Determine the max exp argument value according to
589 : the size of the base integer. The max exp value
590 : is conservatively estimated assuming IEEE754 double
591 : precision format. */
592 7 : if (bit_sz == 8)
593 : max_exp = 128;
594 : else if (bit_sz == 16)
595 : max_exp = 64;
596 : else
597 : {
598 0 : gcc_assert (bit_sz == MAX_BASE_INT_BIT_SIZE);
599 : max_exp = 32;
600 : }
601 :
602 : /* For pow ((double)x, y), generate the following conditions:
603 : cond 1:
604 : temp1 = x;
605 : if (__builtin_islessequal (temp1, 0))
606 :
607 : cond 2:
608 : temp2 = y;
609 : if (__builtin_isgreater (temp2, max_exp_real_cst)) */
610 :
611 : /* Generate condition in reverse order -- first
612 : the condition for the exp argument. */
613 :
614 7 : exp_domain = get_domain (0, false, false,
615 : max_exp, true, true);
616 :
617 7 : gen_conditions_for_domain (expn, exp_domain,
618 : conds, nconds);
619 :
620 : /* Now generate condition for the base argument.
621 : Note it does not use the helper function
622 : gen_conditions_for_domain because the base
623 : type is integer. */
624 :
625 : /* Push a separator. */
626 7 : conds.quick_push (NULL);
627 :
628 7 : temp = create_tmp_var (int_type, "DCE_COND1");
629 7 : cst0 = build_int_cst (int_type, 0);
630 7 : stmt1 = gimple_build_assign (temp, base_val0);
631 7 : tempn = make_ssa_name (temp, stmt1);
632 7 : gimple_assign_set_lhs (stmt1, tempn);
633 7 : stmt2 = gimple_build_cond (GT_EXPR, tempn, cst0, NULL_TREE, NULL_TREE);
634 :
635 7 : conds.quick_push (stmt1);
636 7 : conds.quick_push (stmt2);
637 7 : (*nconds)++;
638 7 : }
639 :
640 : /* Method to generate conditional statements for guarding conditionally
641 : dead calls to pow. One or more statements can be generated for
642 : each logical condition. Statement groups of different conditions
643 : are separated by a NULL tree and they are stored in the vec
644 : conds. The number of logical conditions are stored in *nconds.
645 :
646 : See C99 standard, 7.12.7.4:2, for description of pow (x, y).
647 : The precise condition for domain errors are complex. In this
648 : implementation, a simplified (but conservative) valid domain
649 : for x and y are used: x is positive to avoid dom errors, while
650 : y is smaller than a upper bound (depending on x) to avoid range
651 : errors. Runtime code is generated to check x (if not constant)
652 : and y against the valid domain. If it is out, jump to the call,
653 : otherwise the call is bypassed. POW_CALL is the call statement,
654 : *CONDS is a vector holding the resulting condition statements,
655 : and *NCONDS is the number of logical conditions. */
656 :
657 : static void
658 21 : gen_conditions_for_pow (gcall *pow_call, vec<gimple *> conds,
659 : unsigned *nconds)
660 : {
661 21 : tree base, expn;
662 21 : enum tree_code bc;
663 :
664 21 : gcc_checking_assert (check_pow (pow_call));
665 :
666 21 : *nconds = 0;
667 :
668 21 : base = gimple_call_arg (pow_call, 0);
669 21 : expn = gimple_call_arg (pow_call, 1);
670 :
671 21 : bc = TREE_CODE (base);
672 :
673 21 : if (bc == REAL_CST)
674 14 : gen_conditions_for_pow_cst_base (base, expn, conds, nconds);
675 7 : else if (bc == SSA_NAME)
676 7 : gen_conditions_for_pow_int_base (base, expn, conds, nconds);
677 : else
678 0 : gcc_unreachable ();
679 21 : }
680 :
681 : /* A helper routine to help computing the valid input domain
682 : for a builtin function. See C99 7.12.7 for details. In this
683 : implementation, we only handle single region domain. The
684 : resulting region can be conservative (smaller) than the actual
685 : one and rounded to integers. Some of the bounds are documented
686 : in the standard, while other limit constants are computed
687 : assuming IEEE floating point format (for SF and DF modes).
688 : Since IEEE only sets minimum requirements for long double format,
689 : different long double formats exist under different implementations
690 : (e.g, 64 bit double precision (DF), 80 bit double-extended
691 : precision (XF), and 128 bit quad precision (TF) ). For simplicity,
692 : in this implementation, the computed bounds for long double assume
693 : 64 bit format (DF) except when it is IEEE quad or extended with the same
694 : emax, and are therefore sometimes conservative. Another assumption is
695 : that single precision float type is always SF mode, and double type is DF
696 : mode. This function is quite implementation specific, so it may not be
697 : suitable to be part of builtins.cc. This needs to be revisited later
698 : to see if it can be leveraged in x87 assembly expansion. */
699 :
700 : static inp_domain
701 2629 : get_no_error_domain (enum built_in_function fnc)
702 : {
703 2707 : switch (fnc)
704 : {
705 : /* Trig functions: return [-1, +1] */
706 68 : CASE_FLT_FN (BUILT_IN_ACOS):
707 68 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOS):
708 68 : CASE_FLT_FN (BUILT_IN_ACOSPI):
709 68 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSPI):
710 68 : CASE_FLT_FN (BUILT_IN_ASIN):
711 68 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASIN):
712 68 : CASE_FLT_FN (BUILT_IN_ASINPI):
713 68 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ASINPI):
714 68 : return get_domain (-1, true, true,
715 : 1, true, true);
716 : /* Hyperbolic functions. */
717 42 : CASE_FLT_FN (BUILT_IN_ACOSH):
718 42 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ACOSH):
719 : /* acosh: [1, +inf) */
720 42 : return get_domain (1, true, true,
721 : 1, false, false);
722 36 : CASE_FLT_FN (BUILT_IN_ATANH):
723 36 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_ATANH):
724 : /* atanh: (-1, +1) */
725 36 : return get_domain (-1, true, false,
726 : 1, true, false);
727 0 : case BUILT_IN_COSHF16:
728 0 : case BUILT_IN_SINHF16:
729 : /* coshf16: (-11, +11) */
730 0 : return get_domain (-11, true, false,
731 : 11, true, false);
732 14 : case BUILT_IN_COSHF:
733 14 : case BUILT_IN_SINHF:
734 14 : case BUILT_IN_COSHF32:
735 14 : case BUILT_IN_SINHF32:
736 : /* coshf: (-89, +89) */
737 14 : return get_domain (-89, true, false,
738 : 89, true, false);
739 36 : case BUILT_IN_COSH:
740 36 : case BUILT_IN_SINH:
741 36 : case BUILT_IN_COSHF64:
742 36 : case BUILT_IN_SINHF64:
743 36 : case BUILT_IN_COSHF32X:
744 36 : case BUILT_IN_SINHF32X:
745 : /* cosh: (-710, +710) */
746 36 : return get_domain (-710, true, false,
747 : 710, true, false);
748 18 : case BUILT_IN_COSHF128:
749 18 : case BUILT_IN_SINHF128:
750 : /* coshf128: (-11357, +11357) */
751 18 : return get_domain (-11357, true, false,
752 : 11357, true, false);
753 12 : case BUILT_IN_COSHL:
754 12 : case BUILT_IN_SINHL:
755 12 : if (REAL_MODE_FORMAT (TYPE_MODE (long_double_type_node))->emax == 16384)
756 : return get_no_error_domain (BUILT_IN_COSHF128);
757 0 : return get_no_error_domain (BUILT_IN_COSH);
758 2 : case BUILT_IN_COSHF64X:
759 2 : case BUILT_IN_SINHF64X:
760 2 : if (REAL_MODE_FORMAT (TYPE_MODE (float64x_type_node))->emax == 16384)
761 : return get_no_error_domain (BUILT_IN_COSHF128);
762 0 : return get_no_error_domain (BUILT_IN_COSH);
763 : /* Log functions: (0, +inf) */
764 137 : CASE_FLT_FN (BUILT_IN_LOG):
765 137 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG):
766 137 : CASE_FLT_FN (BUILT_IN_LOG2):
767 137 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG2):
768 137 : CASE_FLT_FN (BUILT_IN_LOG10):
769 137 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG10):
770 137 : return get_domain (0, true, false,
771 : 0, false, false);
772 33 : CASE_FLT_FN (BUILT_IN_LOG1P):
773 33 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_LOG1P):
774 33 : return get_domain (-1, true, false,
775 : 0, false, false);
776 : /* Exp functions. */
777 0 : case BUILT_IN_EXPF16:
778 0 : case BUILT_IN_EXPM1F16:
779 : /* expf16: (-inf, 11) */
780 0 : return get_domain (-1, false, false,
781 : 11, true, false);
782 60 : case BUILT_IN_EXPF:
783 60 : case BUILT_IN_EXPM1F:
784 60 : case BUILT_IN_EXPF32:
785 60 : case BUILT_IN_EXPM1F32:
786 : /* expf: (-inf, 88) */
787 60 : return get_domain (-1, false, false,
788 : 88, true, false);
789 158 : case BUILT_IN_EXP:
790 158 : case BUILT_IN_EXPM1:
791 158 : case BUILT_IN_EXPF64:
792 158 : case BUILT_IN_EXPM1F64:
793 158 : case BUILT_IN_EXPF32X:
794 158 : case BUILT_IN_EXPM1F32X:
795 : /* exp: (-inf, 709) */
796 158 : return get_domain (-1, false, false,
797 : 709, true, false);
798 61 : case BUILT_IN_EXPF128:
799 61 : case BUILT_IN_EXPM1F128:
800 : /* expf128: (-inf, 11356) */
801 61 : return get_domain (-1, false, false,
802 : 11356, true, false);
803 57 : case BUILT_IN_EXPL:
804 57 : case BUILT_IN_EXPM1L:
805 57 : if (REAL_MODE_FORMAT (TYPE_MODE (long_double_type_node))->emax == 16384)
806 : return get_no_error_domain (BUILT_IN_EXPF128);
807 0 : return get_no_error_domain (BUILT_IN_EXP);
808 1 : case BUILT_IN_EXPF64X:
809 1 : case BUILT_IN_EXPM1F64X:
810 1 : if (REAL_MODE_FORMAT (TYPE_MODE (float64x_type_node))->emax == 16384)
811 : return get_no_error_domain (BUILT_IN_EXPF128);
812 0 : return get_no_error_domain (BUILT_IN_EXP);
813 0 : case BUILT_IN_EXP2F16:
814 : /* exp2f16: (-inf, 16) */
815 0 : return get_domain (-1, false, false,
816 : 16, true, false);
817 7 : case BUILT_IN_EXP2F:
818 7 : case BUILT_IN_EXP2F32:
819 : /* exp2f: (-inf, 128) */
820 7 : return get_domain (-1, false, false,
821 : 128, true, false);
822 18 : case BUILT_IN_EXP2:
823 18 : case BUILT_IN_EXP2F64:
824 18 : case BUILT_IN_EXP2F32X:
825 : /* exp2: (-inf, 1024) */
826 18 : return get_domain (-1, false, false,
827 : 1024, true, false);
828 8 : case BUILT_IN_EXP2F128:
829 : /* exp2f128: (-inf, 16384) */
830 8 : return get_domain (-1, false, false,
831 : 16384, true, false);
832 5 : case BUILT_IN_EXP2L:
833 5 : if (REAL_MODE_FORMAT (TYPE_MODE (long_double_type_node))->emax == 16384)
834 : return get_no_error_domain (BUILT_IN_EXP2F128);
835 0 : return get_no_error_domain (BUILT_IN_EXP2);
836 1 : case BUILT_IN_EXP2F64X:
837 1 : if (REAL_MODE_FORMAT (TYPE_MODE (float64x_type_node))->emax == 16384)
838 : return get_no_error_domain (BUILT_IN_EXP2F128);
839 0 : return get_no_error_domain (BUILT_IN_EXP2);
840 1 : case BUILT_IN_EXP10F:
841 1 : case BUILT_IN_POW10F:
842 : /* exp10f: (-inf, 38) */
843 1 : return get_domain (-1, false, false,
844 : 38, true, false);
845 5 : case BUILT_IN_EXP10:
846 5 : case BUILT_IN_POW10:
847 : /* exp10: (-inf, 308) */
848 5 : return get_domain (-1, false, false,
849 : 308, true, false);
850 1 : case BUILT_IN_EXP10L:
851 1 : case BUILT_IN_POW10L:
852 1 : if (REAL_MODE_FORMAT (TYPE_MODE (long_double_type_node))->emax == 16384)
853 : /* exp10l: (-inf, 4932) */
854 1 : return get_domain (-1, false, false,
855 : 4932, true, false);
856 : return get_no_error_domain (BUILT_IN_EXP10);
857 : /* sqrt: [0, +inf) */
858 1926 : CASE_FLT_FN (BUILT_IN_SQRT):
859 1926 : CASE_FLT_FN_FLOATN_NX (BUILT_IN_SQRT):
860 1926 : return get_domain (0, true, true,
861 : 0, false, false);
862 0 : default:
863 0 : gcc_unreachable ();
864 : }
865 :
866 : gcc_unreachable ();
867 : }
868 :
869 : /* The function to generate shrink wrap conditions for a partially
870 : dead builtin call whose return value is not used anywhere,
871 : but has to be kept live due to potential error condition.
872 : BI_CALL is the builtin call, CONDS is the vector of statements
873 : for condition code, NCODES is the pointer to the number of
874 : logical conditions. Statements belonging to different logical
875 : condition are separated by NULL tree in the vector. */
876 :
877 : static void
878 2650 : gen_shrink_wrap_conditions (gcall *bi_call, const vec<gimple *> &conds,
879 : unsigned int *nconds)
880 : {
881 2650 : gcall *call;
882 2650 : tree fn;
883 2650 : enum built_in_function fnc;
884 :
885 2650 : gcc_assert (nconds && conds.exists ());
886 2650 : gcc_assert (conds.length () == 0);
887 2650 : gcc_assert (is_gimple_call (bi_call));
888 :
889 2650 : call = bi_call;
890 2650 : fn = gimple_call_fndecl (call);
891 2650 : gcc_assert (fn && fndecl_built_in_p (fn));
892 2650 : fnc = DECL_FUNCTION_CODE (fn);
893 2650 : *nconds = 0;
894 :
895 2650 : if (fnc == BUILT_IN_POW)
896 21 : gen_conditions_for_pow (call, conds, nconds);
897 : else
898 : {
899 2629 : tree arg;
900 2629 : inp_domain domain = get_no_error_domain (fnc);
901 2629 : *nconds = 0;
902 2629 : arg = gimple_call_arg (bi_call, 0);
903 2629 : gen_conditions_for_domain (arg, domain, conds, nconds);
904 : }
905 :
906 2650 : return;
907 : }
908 :
909 : /* Shrink-wrap BI_CALL so that it is only called when one of the NCONDS
910 : conditions in CONDS is false. Also move BI_NEWCALL to a new basic block
911 : when it is non-null, it is called while all of the CONDS are true. */
912 :
913 : static void
914 2789 : shrink_wrap_one_built_in_call_with_conds (gcall *bi_call,
915 : const vec <gimple *> &conds,
916 : unsigned int nconds,
917 : gcall *bi_newcall = NULL)
918 : {
919 2789 : gimple_stmt_iterator bi_call_bsi;
920 2789 : basic_block bi_call_bb, bi_newcall_bb, join_tgt_bb, guard_bb;
921 2789 : edge join_tgt_in_edge_from_call, join_tgt_in_edge_fall_thru;
922 2789 : edge bi_call_in_edge0, guard_bb_in_edge;
923 2789 : unsigned tn_cond_stmts;
924 2789 : unsigned ci;
925 2789 : gimple *cond_expr = NULL;
926 2789 : gimple *cond_expr_start;
927 :
928 : /* The cfg we want to create looks like this:
929 : [guard n-1] <- guard_bb (old block)
930 : | \
931 : | [guard n-2] }
932 : | / \ }
933 : | / ... } new blocks
934 : | / [guard 0] }
935 : | / / | }
936 : [call] | <- bi_call_bb }
937 : \ [newcall] <-bi_newcall_bb}
938 : \ |
939 : [join] <- join_tgt_bb (old iff call must end bb)
940 : possible EH edges (only if [join] is old)
941 :
942 : When [join] is new, the immediate dominators for these blocks are:
943 :
944 : 1. [guard n-1]: unchanged
945 : 2. [call]: [guard n-1]
946 : 3. [newcall]: [guard 0]
947 : 4. [guard m]: [guard m+1] for 0 <= m <= n-2
948 : 5. [join]: [guard n-1]
949 :
950 : We punt for the more complex case of [join] being old and
951 : simply free the dominance info. We also punt on postdominators,
952 : which aren't expected to be available at this point anyway. */
953 2789 : bi_call_bb = gimple_bb (bi_call);
954 :
955 : /* Now find the join target bb -- split bi_call_bb if needed. */
956 2789 : if (stmt_ends_bb_p (bi_call))
957 : {
958 : /* We checked that there was a fallthrough edge in
959 : can_guard_call_p. */
960 0 : join_tgt_in_edge_from_call = find_fallthru_edge (bi_call_bb->succs);
961 0 : gcc_assert (join_tgt_in_edge_from_call);
962 : /* We don't want to handle PHIs. */
963 0 : if (EDGE_COUNT (join_tgt_in_edge_from_call->dest->preds) > 1)
964 0 : join_tgt_bb = split_edge (join_tgt_in_edge_from_call);
965 : else
966 : {
967 0 : join_tgt_bb = join_tgt_in_edge_from_call->dest;
968 : /* We may have degenerate PHIs in the destination. Propagate
969 : those out. */
970 0 : for (gphi_iterator i = gsi_start_phis (join_tgt_bb); !gsi_end_p (i);)
971 : {
972 0 : gphi *phi = i.phi ();
973 0 : replace_uses_by (gimple_phi_result (phi),
974 : gimple_phi_arg_def (phi, 0));
975 0 : remove_phi_node (&i, true);
976 : }
977 : }
978 : }
979 : else
980 : {
981 2789 : join_tgt_in_edge_from_call = split_block (bi_call_bb, bi_call);
982 2789 : join_tgt_bb = join_tgt_in_edge_from_call->dest;
983 : }
984 :
985 2789 : bi_call_bsi = gsi_for_stmt (bi_call);
986 :
987 : /* Now it is time to insert the first conditional expression
988 : into bi_call_bb and split this bb so that bi_call is
989 : shrink-wrapped. */
990 2789 : tn_cond_stmts = conds.length ();
991 2789 : cond_expr = NULL;
992 2789 : cond_expr_start = conds[0];
993 10882 : for (ci = 0; ci < tn_cond_stmts; ci++)
994 : {
995 8272 : gimple *c = conds[ci];
996 8272 : gcc_assert (c || ci != 0);
997 8272 : if (!c)
998 : break;
999 8093 : gsi_insert_before (&bi_call_bsi, c, GSI_SAME_STMT);
1000 8093 : cond_expr = c;
1001 : }
1002 2789 : ci++;
1003 2789 : gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND);
1004 :
1005 2789 : typedef std::pair<edge, edge> edge_pair;
1006 2789 : auto_vec<edge_pair, 8> edges;
1007 :
1008 2789 : bi_call_in_edge0 = split_block (bi_call_bb, cond_expr);
1009 2789 : bi_call_in_edge0->flags &= ~EDGE_FALLTHRU;
1010 2789 : bi_call_in_edge0->flags |= EDGE_FALSE_VALUE;
1011 2789 : guard_bb = bi_call_bb;
1012 2789 : bi_call_bb = bi_call_in_edge0->dest;
1013 2789 : join_tgt_in_edge_fall_thru = make_edge (guard_bb, join_tgt_bb,
1014 : EDGE_TRUE_VALUE);
1015 :
1016 2789 : edges.reserve (nconds);
1017 2789 : edges.quick_push (edge_pair (bi_call_in_edge0, join_tgt_in_edge_fall_thru));
1018 :
1019 : /* Code generation for the rest of the conditions */
1020 2968 : for (unsigned int i = 1; i < nconds; ++i)
1021 : {
1022 179 : unsigned ci0;
1023 179 : edge bi_call_in_edge;
1024 179 : gimple_stmt_iterator guard_bsi = gsi_for_stmt (cond_expr_start);
1025 179 : ci0 = ci;
1026 179 : cond_expr_start = conds[ci0];
1027 709 : for (; ci < tn_cond_stmts; ci++)
1028 : {
1029 530 : gimple *c = conds[ci];
1030 530 : gcc_assert (c || ci != ci0);
1031 530 : if (!c)
1032 : break;
1033 530 : gsi_insert_before (&guard_bsi, c, GSI_SAME_STMT);
1034 530 : cond_expr = c;
1035 : }
1036 179 : ci++;
1037 179 : gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND);
1038 179 : guard_bb_in_edge = split_block (guard_bb, cond_expr);
1039 179 : guard_bb_in_edge->flags &= ~EDGE_FALLTHRU;
1040 179 : guard_bb_in_edge->flags |= EDGE_TRUE_VALUE;
1041 :
1042 179 : bi_call_in_edge = make_edge (guard_bb, bi_call_bb, EDGE_FALSE_VALUE);
1043 179 : edges.quick_push (edge_pair (bi_call_in_edge, guard_bb_in_edge));
1044 : }
1045 :
1046 : /* Move BI_NEWCALL to new basic block when it is non-null. */
1047 2789 : if (bi_newcall)
1048 : {
1049 : /* Get bi_newcall_bb by split join_tgt_in_edge_fall_thru edge,
1050 : and move BI_NEWCALL to bi_newcall_bb. */
1051 1673 : bi_newcall_bb = split_edge (join_tgt_in_edge_fall_thru);
1052 1673 : gimple_stmt_iterator to_gsi = gsi_start_bb (bi_newcall_bb);
1053 1673 : gimple_stmt_iterator from_gsi = gsi_for_stmt (bi_newcall);
1054 1673 : gsi_move_before (&from_gsi, &to_gsi);
1055 1673 : join_tgt_in_edge_fall_thru = EDGE_SUCC (bi_newcall_bb, 0);
1056 1673 : join_tgt_bb = join_tgt_in_edge_fall_thru->dest;
1057 :
1058 1673 : tree bi_newcall_lhs = gimple_call_lhs (bi_newcall);
1059 1673 : tree bi_call_lhs = gimple_call_lhs (bi_call);
1060 1673 : if (!bi_call_lhs)
1061 : {
1062 1673 : bi_call_lhs = copy_ssa_name (bi_newcall_lhs);
1063 1673 : gimple_call_set_lhs (bi_call, bi_call_lhs);
1064 1673 : SSA_NAME_DEF_STMT (bi_call_lhs) = bi_call;
1065 : }
1066 :
1067 : /* Create phi node for lhs of BI_CALL and BI_NEWCALL. */
1068 1673 : gphi *new_phi = create_phi_node (copy_ssa_name (bi_newcall_lhs),
1069 : join_tgt_bb);
1070 1673 : SSA_NAME_OCCURS_IN_ABNORMAL_PHI (PHI_RESULT (new_phi))
1071 1673 : = SSA_NAME_OCCURS_IN_ABNORMAL_PHI (bi_newcall_lhs);
1072 1673 : add_phi_arg (new_phi, bi_call_lhs, join_tgt_in_edge_from_call,
1073 : gimple_location (bi_call));
1074 1673 : add_phi_arg (new_phi, bi_newcall_lhs, join_tgt_in_edge_fall_thru,
1075 : gimple_location (bi_newcall));
1076 :
1077 : /* Replace all use of original return value with result of phi node. */
1078 1673 : use_operand_p use_p;
1079 1673 : gimple *use_stmt;
1080 1673 : imm_use_iterator iterator;
1081 7546 : FOR_EACH_IMM_USE_STMT (use_stmt, iterator, bi_newcall_lhs)
1082 4200 : if (use_stmt != new_phi)
1083 7581 : FOR_EACH_IMM_USE_ON_STMT (use_p, iterator)
1084 4200 : SET_USE (use_p, PHI_RESULT (new_phi));
1085 : }
1086 :
1087 : /* Now update the probability and profile information, processing the
1088 : guards in order of execution.
1089 :
1090 : There are two approaches we could take here. On the one hand we
1091 : could assign a probability of X to the call block and distribute
1092 : that probability among its incoming edges. On the other hand we
1093 : could assign a probability of X to each individual call edge.
1094 :
1095 : The choice only affects calls that have more than one condition.
1096 : In those cases, the second approach would give the call block
1097 : a greater probability than the first. However, the difference
1098 : is only small, and our chosen X is a pure guess anyway.
1099 :
1100 : Here we take the second approach because it's slightly simpler
1101 : and because it's easy to see that it doesn't lose profile counts. */
1102 2789 : bi_call_bb->count = profile_count::zero ();
1103 8546 : while (!edges.is_empty ())
1104 : {
1105 2968 : edge_pair e = edges.pop ();
1106 2968 : edge call_edge = e.first;
1107 2968 : edge nocall_edge = e.second;
1108 2968 : basic_block src_bb = call_edge->src;
1109 2968 : gcc_assert (src_bb == nocall_edge->src);
1110 :
1111 2968 : call_edge->probability = profile_probability::very_unlikely ();
1112 5936 : nocall_edge->probability = profile_probability::always ()
1113 2968 : - call_edge->probability;
1114 :
1115 2968 : bi_call_bb->count += call_edge->count ();
1116 :
1117 2968 : if (nocall_edge->dest != join_tgt_bb)
1118 1852 : nocall_edge->dest->count = src_bb->count - bi_call_bb->count;
1119 : }
1120 :
1121 2789 : if (dom_info_available_p (CDI_DOMINATORS))
1122 : {
1123 : /* The split_blocks leave [guard 0] as the immediate dominator
1124 : of [call] and [call] as the immediate dominator of [join].
1125 : Fix them up. */
1126 2789 : set_immediate_dominator (CDI_DOMINATORS, bi_call_bb, guard_bb);
1127 2789 : set_immediate_dominator (CDI_DOMINATORS, join_tgt_bb, guard_bb);
1128 : }
1129 :
1130 2789 : if (dump_file && (dump_flags & TDF_DETAILS))
1131 : {
1132 138 : location_t loc;
1133 138 : loc = gimple_location (bi_call);
1134 138 : fprintf (dump_file,
1135 : "%s:%d: note: function call is shrink-wrapped"
1136 : " into error conditions.\n",
1137 276 : LOCATION_FILE (loc), LOCATION_LINE (loc));
1138 : }
1139 2789 : }
1140 :
1141 : /* Shrink-wrap BI_CALL so that it is only called when it might set errno
1142 : (but is always called if it would set errno). */
1143 :
1144 : static void
1145 977 : shrink_wrap_one_built_in_call (gcall *bi_call)
1146 : {
1147 977 : unsigned nconds = 0;
1148 977 : auto_vec<gimple *, 12> conds;
1149 977 : gen_shrink_wrap_conditions (bi_call, conds, &nconds);
1150 977 : gcc_assert (nconds != 0);
1151 977 : shrink_wrap_one_built_in_call_with_conds (bi_call, conds, nconds);
1152 977 : }
1153 :
1154 : /* Return true if built-in function call CALL could be implemented using
1155 : a combination of an internal function to compute the result and a
1156 : separate call to set errno. */
1157 :
1158 : static bool
1159 366402 : can_use_internal_fn (gcall *call)
1160 : {
1161 : /* Only replace calls that set errno. */
1162 905822 : if (!gimple_vdef (call))
1163 : return false;
1164 :
1165 : /* See whether there is an internal function for this built-in. */
1166 174830 : if (replacement_internal_fn (call) == IFN_LAST)
1167 : return false;
1168 :
1169 : /* See whether we can catch all cases where errno would be set,
1170 : while still avoiding the call in most cases. */
1171 1907 : if (!can_test_argument_range (call)
1172 1907 : && !edom_only_function (call))
1173 : return false;
1174 :
1175 : return true;
1176 : }
1177 :
1178 : /* Implement built-in function call CALL using an internal function. */
1179 :
1180 : static void
1181 1812 : use_internal_fn (gcall *call)
1182 : {
1183 : /* We'll be inserting another call with the same arguments after the
1184 : lhs has been set, so prevent any possible coalescing failure from
1185 : having both values live at once. See PR 71020. */
1186 1812 : replace_abnormal_ssa_names (call);
1187 :
1188 1812 : unsigned nconds = 0;
1189 1812 : auto_vec<gimple *, 12> conds;
1190 1812 : bool is_arg_conds = false;
1191 1812 : if (can_test_argument_range (call))
1192 : {
1193 1673 : gen_shrink_wrap_conditions (call, conds, &nconds);
1194 1673 : is_arg_conds = true;
1195 1673 : gcc_assert (nconds != 0);
1196 : }
1197 : else
1198 139 : gcc_assert (edom_only_function (call));
1199 :
1200 1812 : internal_fn ifn = replacement_internal_fn (call);
1201 1812 : gcc_assert (ifn != IFN_LAST);
1202 :
1203 : /* Construct the new call, with the same arguments as the original one. */
1204 1812 : auto_vec <tree, 16> args;
1205 1812 : unsigned int nargs = gimple_call_num_args (call);
1206 3762 : for (unsigned int i = 0; i < nargs; ++i)
1207 1950 : args.safe_push (gimple_call_arg (call, i));
1208 1812 : gcall *new_call = gimple_build_call_internal_vec (ifn, args);
1209 1812 : gimple_set_location (new_call, gimple_location (call));
1210 1812 : gimple_call_set_nothrow (new_call, gimple_call_nothrow_p (call));
1211 :
1212 : /* Transfer the LHS to the new call. */
1213 1812 : tree lhs = gimple_call_lhs (call);
1214 1812 : gimple_call_set_lhs (new_call, lhs);
1215 1812 : gimple_call_set_lhs (call, NULL_TREE);
1216 1812 : SSA_NAME_DEF_STMT (lhs) = new_call;
1217 :
1218 : /* Insert the new call. */
1219 1812 : gimple_stmt_iterator gsi = gsi_for_stmt (call);
1220 1812 : gsi_insert_before (&gsi, new_call, GSI_SAME_STMT);
1221 :
1222 1812 : if (nconds == 0)
1223 : {
1224 : /* Skip the call if LHS == LHS. If we reach here, EDOM is the only
1225 : valid errno value and it is used iff the result is NaN. */
1226 : /* In the case of non call exceptions, with signaling NaNs, EQ_EXPR
1227 : can throw an exception and that can't be part of the GIMPLE_COND. */
1228 139 : if (flag_exceptions
1229 51 : && cfun->can_throw_non_call_exceptions
1230 143 : && operation_could_trap_p (EQ_EXPR, true, false, NULL_TREE))
1231 : {
1232 4 : tree b = make_ssa_name (boolean_type_node);
1233 4 : conds.quick_push (gimple_build_assign (b, EQ_EXPR, lhs, lhs));
1234 4 : conds.quick_push (gimple_build_cond (NE_EXPR, b, boolean_false_node,
1235 : NULL_TREE, NULL_TREE));
1236 : }
1237 : else
1238 135 : conds.quick_push (gimple_build_cond (EQ_EXPR, lhs, lhs,
1239 : NULL_TREE, NULL_TREE));
1240 139 : nconds++;
1241 :
1242 : /* Try replacing the original call with a direct assignment to
1243 : errno, via an internal function. */
1244 139 : if (set_edom_supported_p () && !stmt_ends_bb_p (call))
1245 : {
1246 0 : gimple_stmt_iterator gsi = gsi_for_stmt (call);
1247 0 : gcall *new_call = gimple_build_call_internal (IFN_SET_EDOM, 0);
1248 0 : gimple_move_vops (new_call, call);
1249 0 : gimple_set_location (new_call, gimple_location (call));
1250 0 : gsi_replace (&gsi, new_call, false);
1251 0 : call = new_call;
1252 : }
1253 : }
1254 1951 : shrink_wrap_one_built_in_call_with_conds (call, conds, nconds,
1255 : is_arg_conds ? new_call : NULL);
1256 1812 : }
1257 :
1258 : /* The top level function for conditional dead code shrink
1259 : wrapping transformation. */
1260 :
1261 : static void
1262 1141 : shrink_wrap_conditional_dead_built_in_calls (const vec<gcall *> &calls)
1263 : {
1264 1141 : unsigned i = 0;
1265 :
1266 1141 : unsigned n = calls.length ();
1267 3930 : for (; i < n ; i++)
1268 : {
1269 2789 : gcall *bi_call = calls[i];
1270 2789 : if (gimple_call_lhs (bi_call))
1271 1812 : use_internal_fn (bi_call);
1272 : else
1273 977 : shrink_wrap_one_built_in_call (bi_call);
1274 : }
1275 1141 : }
1276 :
1277 : namespace {
1278 :
1279 : const pass_data pass_data_call_cdce =
1280 : {
1281 : GIMPLE_PASS, /* type */
1282 : "cdce", /* name */
1283 : OPTGROUP_NONE, /* optinfo_flags */
1284 : TV_TREE_CALL_CDCE, /* tv_id */
1285 : ( PROP_cfg | PROP_ssa ), /* properties_required */
1286 : 0, /* properties_provided */
1287 : 0, /* properties_destroyed */
1288 : 0, /* todo_flags_start */
1289 : 0, /* todo_flags_finish */
1290 : };
1291 :
1292 : class pass_call_cdce : public gimple_opt_pass
1293 : {
1294 : public:
1295 285722 : pass_call_cdce (gcc::context *ctxt)
1296 571444 : : gimple_opt_pass (pass_data_call_cdce, ctxt)
1297 : {}
1298 :
1299 : /* opt_pass methods: */
1300 1041484 : bool gate (function *) final override
1301 : {
1302 : /* The limit constants used in the implementation
1303 : assume IEEE floating point format. Other formats
1304 : can be supported in the future if needed. */
1305 1041484 : return flag_tree_builtin_call_dce != 0;
1306 : }
1307 :
1308 : unsigned int execute (function *) final override;
1309 :
1310 : }; // class pass_call_cdce
1311 :
1312 : unsigned int
1313 1041411 : pass_call_cdce::execute (function *fun)
1314 : {
1315 1041411 : basic_block bb;
1316 1041411 : gimple_stmt_iterator i;
1317 1041411 : auto_vec<gcall *> cond_dead_built_in_calls;
1318 11323562 : FOR_EACH_BB_FN (bb, fun)
1319 : {
1320 : /* Skip blocks that are being optimized for size, since our
1321 : transformation always increases code size. */
1322 10282151 : if (optimize_bb_for_size_p (bb))
1323 2141353 : continue;
1324 :
1325 : /* Collect dead call candidates. */
1326 86186551 : for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i))
1327 : {
1328 69904955 : gcall *stmt = dyn_cast <gcall *> (gsi_stmt (i));
1329 69904955 : if (stmt
1330 3720142 : && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL)
1331 1086113 : && (gimple_call_lhs (stmt)
1332 719711 : ? can_use_internal_fn (stmt)
1333 353309 : : can_test_argument_range (stmt))
1334 70627455 : && can_guard_call_p (stmt))
1335 : {
1336 2789 : if (dump_file && (dump_flags & TDF_DETAILS))
1337 : {
1338 138 : fprintf (dump_file, "Found conditional dead call: ");
1339 138 : print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1340 138 : fprintf (dump_file, "\n");
1341 : }
1342 2789 : if (!cond_dead_built_in_calls.exists ())
1343 1141 : cond_dead_built_in_calls.create (64);
1344 2789 : cond_dead_built_in_calls.safe_push (stmt);
1345 : }
1346 : }
1347 : }
1348 :
1349 1041411 : if (!cond_dead_built_in_calls.exists ())
1350 : return 0;
1351 :
1352 1141 : shrink_wrap_conditional_dead_built_in_calls (cond_dead_built_in_calls);
1353 1141 : free_dominance_info (CDI_POST_DOMINATORS);
1354 : /* As we introduced new control-flow we need to insert PHI-nodes
1355 : for the call-clobbers of the remaining call. */
1356 1141 : mark_virtual_operands_for_renaming (fun);
1357 1141 : return TODO_update_ssa;
1358 1041411 : }
1359 :
1360 : } // anon namespace
1361 :
1362 : gimple_opt_pass *
1363 285722 : make_pass_call_cdce (gcc::context *ctxt)
1364 : {
1365 285722 : return new pass_call_cdce (ctxt);
1366 : }
|