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