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