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