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