]>
Commit | Line | Data |
---|---|---|
e6a23add | 1 | /* Conditional Dead Call Elimination pass for the GNU compiler. |
d353bf18 | 2 | Copyright (C) 2008-2015 Free Software Foundation, Inc. |
e6a23add | 3 | Contributed by Xinliang David Li <davidxl@google.com> |
4 | ||
5 | This file is part of GCC. | |
48e1416a | 6 | |
e6a23add | 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. | |
48e1416a | 11 | |
e6a23add | 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. | |
48e1416a | 16 | |
e6a23add | 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 "tm.h" | |
94ea8568 | 25 | #include "predict.h" |
94ea8568 | 26 | #include "hard-reg-set.h" |
94ea8568 | 27 | #include "function.h" |
28 | #include "dominance.h" | |
29 | #include "cfg.h" | |
e6a23add | 30 | #include "basic-block.h" |
b20a8bb4 | 31 | #include "symtab.h" |
32 | #include "alias.h" | |
e6a23add | 33 | #include "tree.h" |
b20a8bb4 | 34 | #include "fold-const.h" |
9ed99284 | 35 | #include "stor-layout.h" |
ce084dfc | 36 | #include "gimple-pretty-print.h" |
bc61cadb | 37 | #include "tree-ssa-alias.h" |
38 | #include "internal-fn.h" | |
39 | #include "gimple-expr.h" | |
75a70cf9 | 40 | #include "gimple.h" |
dcf1a1ec | 41 | #include "gimple-iterator.h" |
073c1fd5 | 42 | #include "gimple-ssa.h" |
43 | #include "tree-cfg.h" | |
9ed99284 | 44 | #include "stringpool.h" |
073c1fd5 | 45 | #include "tree-ssanames.h" |
46 | #include "tree-into-ssa.h" | |
e6a23add | 47 | #include "tree-pass.h" |
e6a23add | 48 | #include "flags.h" |
49 | \f | |
50 | ||
51 | /* Conditional dead call elimination | |
52 | ||
53 | Some builtin functions can set errno on error conditions, but they | |
54 | are otherwise pure. If the result of a call to such a function is | |
55 | not used, the compiler can still not eliminate the call without | |
56 | powerful interprocedural analysis to prove that the errno is not | |
57 | checked. However, if the conditions under which the error occurs | |
48e1416a | 58 | are known, the compiler can conditionally dead code eliminate the |
e6a23add | 59 | calls by shrink-wrapping the semi-dead calls into the error condition: |
60 | ||
61 | built_in_call (args) | |
62 | ==> | |
63 | if (error_cond (args)) | |
64 | built_in_call (args) | |
65 | ||
66 | An actual simple example is : | |
67 | log (x); // Mostly dead call | |
68 | ==> | |
1f5fe264 | 69 | if (x <= 0) |
e6a23add | 70 | log (x); |
71 | With this change, call to log (x) is effectively eliminated, as | |
72 | in majority of the cases, log won't be called with x out of | |
73 | range. The branch is totally predictable, so the branch cost | |
48e1416a | 74 | is low. |
e6a23add | 75 | |
76 | Note that library functions are not supposed to clear errno to zero without | |
77 | error. See IEEE Std 1003.1, section 2.3 Error Numbers, and section 7.5:3 of | |
78 | ISO/IEC 9899 (C99). | |
79 | ||
80 | The condition wrapping the builtin call is conservatively set to avoid too | |
81 | aggressive (wrong) shrink wrapping. The optimization is called conditional | |
82 | dead call elimination because the call is eliminated under the condition | |
83 | that the input arguments would not lead to domain or range error (for | |
84 | instance when x <= 0 for a log (x) call), however the chances that the error | |
85 | condition is hit is very low (those builtin calls which are conditionally | |
86 | dead are usually part of the C++ abstraction penalty exposed after | |
87 | inlining). */ | |
88 | ||
89 | ||
48e1416a | 90 | /* A structure for representing input domain of |
e6a23add | 91 | a function argument in integer. If the lower |
48e1416a | 92 | bound is -inf, has_lb is set to false. If the |
93 | upper bound is +inf, has_ub is false. | |
94 | is_lb_inclusive and is_ub_inclusive are flags | |
95 | to indicate if lb and ub value are inclusive | |
e6a23add | 96 | respectively. */ |
97 | ||
98 | typedef struct input_domain | |
99 | { | |
100 | int lb; | |
101 | int ub; | |
102 | bool has_lb; | |
103 | bool has_ub; | |
104 | bool is_lb_inclusive; | |
105 | bool is_ub_inclusive; | |
106 | } inp_domain; | |
107 | ||
e6a23add | 108 | /* A helper function to construct and return an input |
48e1416a | 109 | domain object. LB is the lower bound, HAS_LB is |
e6a23add | 110 | a boolean flag indicating if the lower bound exists, |
111 | and LB_INCLUSIVE is a boolean flag indicating if the | |
112 | lower bound is inclusive or not. UB, HAS_UB, and | |
48e1416a | 113 | UB_INCLUSIVE have the same meaning, but for upper |
e6a23add | 114 | bound of the domain. */ |
115 | ||
116 | static inp_domain | |
117 | get_domain (int lb, bool has_lb, bool lb_inclusive, | |
118 | int ub, bool has_ub, bool ub_inclusive) | |
119 | { | |
120 | inp_domain domain; | |
121 | domain.lb = lb; | |
122 | domain.has_lb = has_lb; | |
123 | domain.is_lb_inclusive = lb_inclusive; | |
124 | domain.ub = ub; | |
125 | domain.has_ub = has_ub; | |
126 | domain.is_ub_inclusive = ub_inclusive; | |
127 | return domain; | |
128 | } | |
129 | ||
48e1416a | 130 | /* A helper function to check the target format for the |
e6a23add | 131 | argument type. In this implementation, only IEEE formats |
48e1416a | 132 | are supported. ARG is the call argument to be checked. |
e6a23add | 133 | Returns true if the format is supported. To support other |
134 | target formats, function get_no_error_domain needs to be | |
48e1416a | 135 | enhanced to have range bounds properly computed. Since |
136 | the check is cheap (very small number of candidates | |
e6a23add | 137 | to be checked), the result is not cached for each float type. */ |
138 | ||
139 | static bool | |
140 | check_target_format (tree arg) | |
141 | { | |
142 | tree type; | |
3754d046 | 143 | machine_mode mode; |
e6a23add | 144 | const struct real_format *rfmt; |
48e1416a | 145 | |
e6a23add | 146 | type = TREE_TYPE (arg); |
147 | mode = TYPE_MODE (type); | |
148 | rfmt = REAL_MODE_FORMAT (mode); | |
defc07a6 | 149 | if ((mode == SFmode |
b161ca01 | 150 | && (rfmt == &ieee_single_format || rfmt == &mips_single_format |
151 | || rfmt == &motorola_single_format)) | |
defc07a6 | 152 | || (mode == DFmode |
b161ca01 | 153 | && (rfmt == &ieee_double_format || rfmt == &mips_double_format |
154 | || rfmt == &motorola_double_format)) | |
e6a23add | 155 | /* For long double, we can not really check XFmode |
48e1416a | 156 | which is only defined on intel platforms. |
157 | Candidate pre-selection using builtin function | |
158 | code guarantees that we are checking formats | |
e6a23add | 159 | for long double modes: double, quad, and extended. */ |
48e1416a | 160 | || (mode != SFmode && mode != DFmode |
e6a23add | 161 | && (rfmt == &ieee_quad_format |
defc07a6 | 162 | || rfmt == &mips_quad_format |
b161ca01 | 163 | || rfmt == &ieee_extended_motorola_format |
48e1416a | 164 | || rfmt == &ieee_extended_intel_96_format |
165 | || rfmt == &ieee_extended_intel_128_format | |
e6a23add | 166 | || rfmt == &ieee_extended_intel_96_round_53_format))) |
167 | return true; | |
168 | ||
169 | return false; | |
170 | } | |
171 | ||
172 | \f | |
173 | /* A helper function to help select calls to pow that are suitable for | |
174 | conditional DCE transformation. It looks for pow calls that can be | |
175 | guided with simple conditions. Such calls either have constant base | |
48e1416a | 176 | values or base values converted from integers. Returns true if |
e6a23add | 177 | the pow call POW_CALL is a candidate. */ |
178 | ||
179 | /* The maximum integer bit size for base argument of a pow call | |
180 | that is suitable for shrink-wrapping transformation. */ | |
181 | #define MAX_BASE_INT_BIT_SIZE 32 | |
182 | ||
183 | static bool | |
1a91d914 | 184 | check_pow (gcall *pow_call) |
e6a23add | 185 | { |
186 | tree base, expn; | |
187 | enum tree_code bc, ec; | |
188 | ||
75a70cf9 | 189 | if (gimple_call_num_args (pow_call) != 2) |
e6a23add | 190 | return false; |
191 | ||
75a70cf9 | 192 | base = gimple_call_arg (pow_call, 0); |
193 | expn = gimple_call_arg (pow_call, 1); | |
e6a23add | 194 | |
195 | if (!check_target_format (expn)) | |
196 | return false; | |
197 | ||
198 | bc = TREE_CODE (base); | |
199 | ec = TREE_CODE (expn); | |
200 | ||
201 | /* Folding candidates are not interesting. | |
202 | Can actually assert that it is already folded. */ | |
203 | if (ec == REAL_CST && bc == REAL_CST) | |
204 | return false; | |
205 | ||
206 | if (bc == REAL_CST) | |
207 | { | |
208 | /* Only handle a fixed range of constant. */ | |
209 | REAL_VALUE_TYPE mv; | |
210 | REAL_VALUE_TYPE bcv = TREE_REAL_CST (base); | |
211 | if (REAL_VALUES_EQUAL (bcv, dconst1)) | |
212 | return false; | |
213 | if (REAL_VALUES_LESS (bcv, dconst1)) | |
214 | return false; | |
e913b5cd | 215 | real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, UNSIGNED); |
e6a23add | 216 | if (REAL_VALUES_LESS (mv, bcv)) |
217 | return false; | |
218 | return true; | |
219 | } | |
220 | else if (bc == SSA_NAME) | |
221 | { | |
7ecda5e8 | 222 | tree base_val0, type; |
75a70cf9 | 223 | gimple base_def; |
e6a23add | 224 | int bit_sz; |
225 | ||
226 | /* Only handles cases where base value is converted | |
48e1416a | 227 | from integer values. */ |
e6a23add | 228 | base_def = SSA_NAME_DEF_STMT (base); |
75a70cf9 | 229 | if (gimple_code (base_def) != GIMPLE_ASSIGN) |
e6a23add | 230 | return false; |
231 | ||
75a70cf9 | 232 | if (gimple_assign_rhs_code (base_def) != FLOAT_EXPR) |
e6a23add | 233 | return false; |
75a70cf9 | 234 | base_val0 = gimple_assign_rhs1 (base_def); |
e6a23add | 235 | |
7ecda5e8 | 236 | type = TREE_TYPE (base_val0); |
e6a23add | 237 | if (TREE_CODE (type) != INTEGER_TYPE) |
238 | return false; | |
239 | bit_sz = TYPE_PRECISION (type); | |
240 | /* If the type of the base is too wide, | |
241 | the resulting shrink wrapping condition | |
242 | will be too conservative. */ | |
243 | if (bit_sz > MAX_BASE_INT_BIT_SIZE) | |
244 | return false; | |
245 | ||
246 | return true; | |
247 | } | |
248 | else | |
249 | return false; | |
250 | } | |
251 | ||
252 | /* A helper function to help select candidate function calls that are | |
253 | suitable for conditional DCE. Candidate functions must have single | |
254 | valid input domain in this implementation except for pow (see check_pow). | |
255 | Returns true if the function call is a candidate. */ | |
256 | ||
257 | static bool | |
1a91d914 | 258 | check_builtin_call (gcall *bcall) |
e6a23add | 259 | { |
260 | tree arg; | |
261 | ||
75a70cf9 | 262 | arg = gimple_call_arg (bcall, 0); |
e6a23add | 263 | return check_target_format (arg); |
264 | } | |
265 | ||
266 | /* A helper function to determine if a builtin function call is a | |
267 | candidate for conditional DCE. Returns true if the builtin call | |
268 | is a candidate. */ | |
269 | ||
270 | static bool | |
1a91d914 | 271 | is_call_dce_candidate (gcall *call) |
e6a23add | 272 | { |
273 | tree fn; | |
274 | enum built_in_function fnc; | |
275 | ||
75a70cf9 | 276 | /* Only potentially dead calls are considered. */ |
277 | if (gimple_call_lhs (call)) | |
e6a23add | 278 | return false; |
279 | ||
75a70cf9 | 280 | fn = gimple_call_fndecl (call); |
281 | if (!fn | |
48e1416a | 282 | || !DECL_BUILT_IN (fn) |
e6a23add | 283 | || (DECL_BUILT_IN_CLASS (fn) != BUILT_IN_NORMAL)) |
284 | return false; | |
285 | ||
286 | fnc = DECL_FUNCTION_CODE (fn); | |
287 | switch (fnc) | |
288 | { | |
289 | /* Trig functions. */ | |
290 | CASE_FLT_FN (BUILT_IN_ACOS): | |
291 | CASE_FLT_FN (BUILT_IN_ASIN): | |
292 | /* Hyperbolic functions. */ | |
293 | CASE_FLT_FN (BUILT_IN_ACOSH): | |
294 | CASE_FLT_FN (BUILT_IN_ATANH): | |
295 | CASE_FLT_FN (BUILT_IN_COSH): | |
296 | CASE_FLT_FN (BUILT_IN_SINH): | |
297 | /* Log functions. */ | |
298 | CASE_FLT_FN (BUILT_IN_LOG): | |
299 | CASE_FLT_FN (BUILT_IN_LOG2): | |
300 | CASE_FLT_FN (BUILT_IN_LOG10): | |
301 | CASE_FLT_FN (BUILT_IN_LOG1P): | |
302 | /* Exp functions. */ | |
303 | CASE_FLT_FN (BUILT_IN_EXP): | |
304 | CASE_FLT_FN (BUILT_IN_EXP2): | |
305 | CASE_FLT_FN (BUILT_IN_EXP10): | |
306 | CASE_FLT_FN (BUILT_IN_EXPM1): | |
307 | CASE_FLT_FN (BUILT_IN_POW10): | |
308 | /* Sqrt. */ | |
309 | CASE_FLT_FN (BUILT_IN_SQRT): | |
310 | return check_builtin_call (call); | |
311 | /* Special one: two argument pow. */ | |
312 | case BUILT_IN_POW: | |
313 | return check_pow (call); | |
314 | default: | |
315 | break; | |
316 | } | |
317 | ||
318 | return false; | |
319 | } | |
320 | ||
321 | \f | |
322 | /* A helper function to generate gimple statements for | |
323 | one bound comparison. ARG is the call argument to | |
324 | be compared with the bound, LBUB is the bound value | |
325 | in integer, TCODE is the tree_code of the comparison, | |
326 | TEMP_NAME1/TEMP_NAME2 are names of the temporaries, | |
327 | CONDS is a vector holding the produced GIMPLE statements, | |
328 | and NCONDS points to the variable holding the number | |
48e1416a | 329 | of logical comparisons. CONDS is either empty or |
e6a23add | 330 | a list ended with a null tree. */ |
331 | ||
332 | static void | |
48e1416a | 333 | gen_one_condition (tree arg, int lbub, |
e6a23add | 334 | enum tree_code tcode, |
335 | const char *temp_name1, | |
75a70cf9 | 336 | const char *temp_name2, |
f1f41a6c | 337 | vec<gimple> conds, |
e6a23add | 338 | unsigned *nconds) |
339 | { | |
340 | tree lbub_real_cst, lbub_cst, float_type; | |
341 | tree temp, tempn, tempc, tempcn; | |
1a91d914 | 342 | gassign *stmt1; |
343 | gassign *stmt2; | |
344 | gcond *stmt3; | |
e6a23add | 345 | |
346 | float_type = TREE_TYPE (arg); | |
347 | lbub_cst = build_int_cst (integer_type_node, lbub); | |
348 | lbub_real_cst = build_real_from_int_cst (float_type, lbub_cst); | |
349 | ||
350 | temp = create_tmp_var (float_type, temp_name1); | |
75a70cf9 | 351 | stmt1 = gimple_build_assign (temp, arg); |
e6a23add | 352 | tempn = make_ssa_name (temp, stmt1); |
75a70cf9 | 353 | gimple_assign_set_lhs (stmt1, tempn); |
e6a23add | 354 | |
355 | tempc = create_tmp_var (boolean_type_node, temp_name2); | |
75a70cf9 | 356 | stmt2 = gimple_build_assign (tempc, |
357 | fold_build2 (tcode, | |
358 | boolean_type_node, | |
359 | tempn, lbub_real_cst)); | |
e6a23add | 360 | tempcn = make_ssa_name (tempc, stmt2); |
75a70cf9 | 361 | gimple_assign_set_lhs (stmt2, tempcn); |
362 | ||
363 | stmt3 = gimple_build_cond_from_tree (tempcn, NULL_TREE, NULL_TREE); | |
f1f41a6c | 364 | conds.quick_push (stmt1); |
365 | conds.quick_push (stmt2); | |
366 | conds.quick_push (stmt3); | |
e6a23add | 367 | (*nconds)++; |
368 | } | |
369 | ||
370 | /* A helper function to generate GIMPLE statements for | |
371 | out of input domain check. ARG is the call argument | |
372 | to be runtime checked, DOMAIN holds the valid domain | |
373 | for the given function, CONDS points to the vector | |
48e1416a | 374 | holding the result GIMPLE statements. *NCONDS is |
375 | the number of logical comparisons. This function | |
e6a23add | 376 | produces no more than two logical comparisons, one |
377 | for lower bound check, one for upper bound check. */ | |
378 | ||
379 | static void | |
380 | gen_conditions_for_domain (tree arg, inp_domain domain, | |
f1f41a6c | 381 | vec<gimple> conds, |
e6a23add | 382 | unsigned *nconds) |
383 | { | |
384 | if (domain.has_lb) | |
385 | gen_one_condition (arg, domain.lb, | |
386 | (domain.is_lb_inclusive | |
387 | ? LT_EXPR : LE_EXPR), | |
388 | "DCE_COND_LB", "DCE_COND_LB_TEST", | |
389 | conds, nconds); | |
390 | ||
391 | if (domain.has_ub) | |
392 | { | |
393 | /* Now push a separator. */ | |
394 | if (domain.has_lb) | |
f1f41a6c | 395 | conds.quick_push (NULL); |
e6a23add | 396 | |
397 | gen_one_condition (arg, domain.ub, | |
398 | (domain.is_ub_inclusive | |
399 | ? GT_EXPR : GE_EXPR), | |
400 | "DCE_COND_UB", "DCE_COND_UB_TEST", | |
401 | conds, nconds); | |
402 | } | |
403 | } | |
404 | ||
405 | ||
406 | /* A helper function to generate condition | |
407 | code for the y argument in call pow (some_const, y). | |
48e1416a | 408 | See candidate selection in check_pow. Since the |
e6a23add | 409 | candidates' base values have a limited range, |
410 | the guarded code generated for y are simple: | |
411 | if (y > max_y) | |
412 | pow (const, y); | |
413 | Note max_y can be computed separately for each | |
414 | const base, but in this implementation, we | |
415 | choose to compute it using the max base | |
416 | in the allowed range for the purpose of | |
417 | simplicity. BASE is the constant base value, | |
418 | EXPN is the expression for the exponent argument, | |
419 | *CONDS is the vector to hold resulting statements, | |
420 | and *NCONDS is the number of logical conditions. */ | |
421 | ||
422 | static void | |
423 | gen_conditions_for_pow_cst_base (tree base, tree expn, | |
f1f41a6c | 424 | vec<gimple> conds, |
e6a23add | 425 | unsigned *nconds) |
426 | { | |
48e1416a | 427 | inp_domain exp_domain; |
428 | /* Validate the range of the base constant to make | |
e6a23add | 429 | sure it is consistent with check_pow. */ |
430 | REAL_VALUE_TYPE mv; | |
431 | REAL_VALUE_TYPE bcv = TREE_REAL_CST (base); | |
432 | gcc_assert (!REAL_VALUES_EQUAL (bcv, dconst1) | |
433 | && !REAL_VALUES_LESS (bcv, dconst1)); | |
e913b5cd | 434 | real_from_integer (&mv, TYPE_MODE (TREE_TYPE (base)), 256, UNSIGNED); |
e6a23add | 435 | gcc_assert (!REAL_VALUES_LESS (mv, bcv)); |
436 | ||
437 | exp_domain = get_domain (0, false, false, | |
438 | 127, true, false); | |
439 | ||
440 | gen_conditions_for_domain (expn, exp_domain, | |
441 | conds, nconds); | |
442 | } | |
443 | ||
444 | /* Generate error condition code for pow calls with | |
445 | non constant base values. The candidates selected | |
446 | have their base argument value converted from | |
447 | integer (see check_pow) value (1, 2, 4 bytes), and | |
448 | the max exp value is computed based on the size | |
449 | of the integer type (i.e. max possible base value). | |
450 | The resulting input domain for exp argument is thus | |
48e1416a | 451 | conservative (smaller than the max value allowed by |
452 | the runtime value of the base). BASE is the integer | |
453 | base value, EXPN is the expression for the exponent | |
454 | argument, *CONDS is the vector to hold resulting | |
455 | statements, and *NCONDS is the number of logical | |
e6a23add | 456 | conditions. */ |
457 | ||
458 | static void | |
459 | gen_conditions_for_pow_int_base (tree base, tree expn, | |
f1f41a6c | 460 | vec<gimple> conds, |
e6a23add | 461 | unsigned *nconds) |
462 | { | |
75a70cf9 | 463 | gimple base_def; |
f018d957 | 464 | tree base_val0; |
7ecda5e8 | 465 | tree int_type; |
e6a23add | 466 | tree temp, tempn; |
75a70cf9 | 467 | tree cst0; |
468 | gimple stmt1, stmt2; | |
e6a23add | 469 | int bit_sz, max_exp; |
470 | inp_domain exp_domain; | |
471 | ||
472 | base_def = SSA_NAME_DEF_STMT (base); | |
75a70cf9 | 473 | base_val0 = gimple_assign_rhs1 (base_def); |
7ecda5e8 | 474 | int_type = TREE_TYPE (base_val0); |
e6a23add | 475 | bit_sz = TYPE_PRECISION (int_type); |
48e1416a | 476 | gcc_assert (bit_sz > 0 |
e6a23add | 477 | && bit_sz <= MAX_BASE_INT_BIT_SIZE); |
478 | ||
479 | /* Determine the max exp argument value according to | |
480 | the size of the base integer. The max exp value | |
481 | is conservatively estimated assuming IEEE754 double | |
482 | precision format. */ | |
483 | if (bit_sz == 8) | |
484 | max_exp = 128; | |
485 | else if (bit_sz == 16) | |
486 | max_exp = 64; | |
487 | else | |
f018d957 | 488 | { |
489 | gcc_assert (bit_sz == MAX_BASE_INT_BIT_SIZE); | |
490 | max_exp = 32; | |
491 | } | |
e6a23add | 492 | |
493 | /* For pow ((double)x, y), generate the following conditions: | |
494 | cond 1: | |
495 | temp1 = x; | |
496 | if (temp1 <= 0) | |
497 | ||
498 | cond 2: | |
499 | temp2 = y; | |
500 | if (temp2 > max_exp_real_cst) */ | |
501 | ||
502 | /* Generate condition in reverse order -- first | |
503 | the condition for the exp argument. */ | |
504 | ||
505 | exp_domain = get_domain (0, false, false, | |
506 | max_exp, true, true); | |
507 | ||
508 | gen_conditions_for_domain (expn, exp_domain, | |
509 | conds, nconds); | |
510 | ||
511 | /* Now generate condition for the base argument. | |
512 | Note it does not use the helper function | |
513 | gen_conditions_for_domain because the base | |
514 | type is integer. */ | |
515 | ||
516 | /* Push a separator. */ | |
f1f41a6c | 517 | conds.quick_push (NULL); |
e6a23add | 518 | |
519 | temp = create_tmp_var (int_type, "DCE_COND1"); | |
520 | cst0 = build_int_cst (int_type, 0); | |
75a70cf9 | 521 | stmt1 = gimple_build_assign (temp, base_val0); |
e6a23add | 522 | tempn = make_ssa_name (temp, stmt1); |
75a70cf9 | 523 | gimple_assign_set_lhs (stmt1, tempn); |
524 | stmt2 = gimple_build_cond (LE_EXPR, tempn, cst0, NULL_TREE, NULL_TREE); | |
e6a23add | 525 | |
f1f41a6c | 526 | conds.quick_push (stmt1); |
527 | conds.quick_push (stmt2); | |
e6a23add | 528 | (*nconds)++; |
529 | } | |
530 | ||
531 | /* Method to generate conditional statements for guarding conditionally | |
532 | dead calls to pow. One or more statements can be generated for | |
533 | each logical condition. Statement groups of different conditions | |
f1f41a6c | 534 | are separated by a NULL tree and they are stored in the vec |
e6a23add | 535 | conds. The number of logical conditions are stored in *nconds. |
536 | ||
537 | See C99 standard, 7.12.7.4:2, for description of pow (x, y). | |
538 | The precise condition for domain errors are complex. In this | |
539 | implementation, a simplified (but conservative) valid domain | |
540 | for x and y are used: x is positive to avoid dom errors, while | |
541 | y is smaller than a upper bound (depending on x) to avoid range | |
542 | errors. Runtime code is generated to check x (if not constant) | |
543 | and y against the valid domain. If it is out, jump to the call, | |
544 | otherwise the call is bypassed. POW_CALL is the call statement, | |
545 | *CONDS is a vector holding the resulting condition statements, | |
546 | and *NCONDS is the number of logical conditions. */ | |
547 | ||
548 | static void | |
1a91d914 | 549 | gen_conditions_for_pow (gcall *pow_call, vec<gimple> conds, |
e6a23add | 550 | unsigned *nconds) |
551 | { | |
552 | tree base, expn; | |
f018d957 | 553 | enum tree_code bc; |
e6a23add | 554 | |
1b4345f7 | 555 | gcc_checking_assert (check_pow (pow_call)); |
e6a23add | 556 | |
557 | *nconds = 0; | |
558 | ||
75a70cf9 | 559 | base = gimple_call_arg (pow_call, 0); |
560 | expn = gimple_call_arg (pow_call, 1); | |
e6a23add | 561 | |
562 | bc = TREE_CODE (base); | |
e6a23add | 563 | |
564 | if (bc == REAL_CST) | |
f018d957 | 565 | gen_conditions_for_pow_cst_base (base, expn, conds, nconds); |
e6a23add | 566 | else if (bc == SSA_NAME) |
f018d957 | 567 | gen_conditions_for_pow_int_base (base, expn, conds, nconds); |
e6a23add | 568 | else |
569 | gcc_unreachable (); | |
570 | } | |
571 | ||
572 | /* A helper routine to help computing the valid input domain | |
573 | for a builtin function. See C99 7.12.7 for details. In this | |
574 | implementation, we only handle single region domain. The | |
575 | resulting region can be conservative (smaller) than the actual | |
576 | one and rounded to integers. Some of the bounds are documented | |
577 | in the standard, while other limit constants are computed | |
48e1416a | 578 | assuming IEEE floating point format (for SF and DF modes). |
579 | Since IEEE only sets minimum requirements for long double format, | |
580 | different long double formats exist under different implementations | |
581 | (e.g, 64 bit double precision (DF), 80 bit double-extended | |
582 | precision (XF), and 128 bit quad precision (QF) ). For simplicity, | |
583 | in this implementation, the computed bounds for long double assume | |
584 | 64 bit format (DF), and are therefore conservative. Another | |
e6a23add | 585 | assumption is that single precision float type is always SF mode, |
48e1416a | 586 | and double type is DF mode. This function is quite |
e6a23add | 587 | implementation specific, so it may not be suitable to be part of |
588 | builtins.c. This needs to be revisited later to see if it can | |
589 | be leveraged in x87 assembly expansion. */ | |
590 | ||
591 | static inp_domain | |
592 | get_no_error_domain (enum built_in_function fnc) | |
593 | { | |
594 | switch (fnc) | |
595 | { | |
596 | /* Trig functions: return [-1, +1] */ | |
597 | CASE_FLT_FN (BUILT_IN_ACOS): | |
598 | CASE_FLT_FN (BUILT_IN_ASIN): | |
599 | return get_domain (-1, true, true, | |
600 | 1, true, true); | |
601 | /* Hyperbolic functions. */ | |
602 | CASE_FLT_FN (BUILT_IN_ACOSH): | |
603 | /* acosh: [1, +inf) */ | |
604 | return get_domain (1, true, true, | |
605 | 1, false, false); | |
606 | CASE_FLT_FN (BUILT_IN_ATANH): | |
607 | /* atanh: (-1, +1) */ | |
608 | return get_domain (-1, true, false, | |
609 | 1, true, false); | |
610 | case BUILT_IN_COSHF: | |
611 | case BUILT_IN_SINHF: | |
612 | /* coshf: (-89, +89) */ | |
613 | return get_domain (-89, true, false, | |
614 | 89, true, false); | |
615 | case BUILT_IN_COSH: | |
616 | case BUILT_IN_SINH: | |
617 | case BUILT_IN_COSHL: | |
618 | case BUILT_IN_SINHL: | |
619 | /* cosh: (-710, +710) */ | |
620 | return get_domain (-710, true, false, | |
621 | 710, true, false); | |
622 | /* Log functions: (0, +inf) */ | |
623 | CASE_FLT_FN (BUILT_IN_LOG): | |
624 | CASE_FLT_FN (BUILT_IN_LOG2): | |
625 | CASE_FLT_FN (BUILT_IN_LOG10): | |
626 | return get_domain (0, true, false, | |
627 | 0, false, false); | |
628 | CASE_FLT_FN (BUILT_IN_LOG1P): | |
629 | return get_domain (-1, true, false, | |
630 | 0, false, false); | |
631 | /* Exp functions. */ | |
632 | case BUILT_IN_EXPF: | |
633 | case BUILT_IN_EXPM1F: | |
634 | /* expf: (-inf, 88) */ | |
635 | return get_domain (-1, false, false, | |
636 | 88, true, false); | |
637 | case BUILT_IN_EXP: | |
638 | case BUILT_IN_EXPM1: | |
639 | case BUILT_IN_EXPL: | |
640 | case BUILT_IN_EXPM1L: | |
641 | /* exp: (-inf, 709) */ | |
642 | return get_domain (-1, false, false, | |
643 | 709, true, false); | |
644 | case BUILT_IN_EXP2F: | |
645 | /* exp2f: (-inf, 128) */ | |
646 | return get_domain (-1, false, false, | |
647 | 128, true, false); | |
648 | case BUILT_IN_EXP2: | |
649 | case BUILT_IN_EXP2L: | |
650 | /* exp2: (-inf, 1024) */ | |
651 | return get_domain (-1, false, false, | |
652 | 1024, true, false); | |
653 | case BUILT_IN_EXP10F: | |
654 | case BUILT_IN_POW10F: | |
655 | /* exp10f: (-inf, 38) */ | |
656 | return get_domain (-1, false, false, | |
657 | 38, true, false); | |
658 | case BUILT_IN_EXP10: | |
659 | case BUILT_IN_POW10: | |
660 | case BUILT_IN_EXP10L: | |
661 | case BUILT_IN_POW10L: | |
662 | /* exp10: (-inf, 308) */ | |
663 | return get_domain (-1, false, false, | |
664 | 308, true, false); | |
665 | /* sqrt: [0, +inf) */ | |
666 | CASE_FLT_FN (BUILT_IN_SQRT): | |
667 | return get_domain (0, true, true, | |
668 | 0, false, false); | |
669 | default: | |
48e1416a | 670 | gcc_unreachable (); |
e6a23add | 671 | } |
672 | ||
48e1416a | 673 | gcc_unreachable (); |
e6a23add | 674 | } |
675 | ||
676 | /* The function to generate shrink wrap conditions for a partially | |
677 | dead builtin call whose return value is not used anywhere, | |
678 | but has to be kept live due to potential error condition. | |
48e1416a | 679 | BI_CALL is the builtin call, CONDS is the vector of statements |
680 | for condition code, NCODES is the pointer to the number of | |
e6a23add | 681 | logical conditions. Statements belonging to different logical |
682 | condition are separated by NULL tree in the vector. */ | |
683 | ||
684 | static void | |
1a91d914 | 685 | gen_shrink_wrap_conditions (gcall *bi_call, vec<gimple> conds, |
e6a23add | 686 | unsigned int *nconds) |
687 | { | |
1a91d914 | 688 | gcall *call; |
75a70cf9 | 689 | tree fn; |
e6a23add | 690 | enum built_in_function fnc; |
691 | ||
f1f41a6c | 692 | gcc_assert (nconds && conds.exists ()); |
693 | gcc_assert (conds.length () == 0); | |
75a70cf9 | 694 | gcc_assert (is_gimple_call (bi_call)); |
e6a23add | 695 | |
696 | call = bi_call; | |
75a70cf9 | 697 | fn = gimple_call_fndecl (call); |
e6a23add | 698 | gcc_assert (fn && DECL_BUILT_IN (fn)); |
699 | fnc = DECL_FUNCTION_CODE (fn); | |
700 | *nconds = 0; | |
701 | ||
702 | if (fnc == BUILT_IN_POW) | |
703 | gen_conditions_for_pow (call, conds, nconds); | |
704 | else | |
705 | { | |
706 | tree arg; | |
707 | inp_domain domain = get_no_error_domain (fnc); | |
708 | *nconds = 0; | |
75a70cf9 | 709 | arg = gimple_call_arg (bi_call, 0); |
e6a23add | 710 | gen_conditions_for_domain (arg, domain, conds, nconds); |
711 | } | |
712 | ||
713 | return; | |
714 | } | |
715 | ||
716 | ||
717 | /* Probability of the branch (to the call) is taken. */ | |
718 | #define ERR_PROB 0.01 | |
719 | ||
48e1416a | 720 | /* The function to shrink wrap a partially dead builtin call |
721 | whose return value is not used anywhere, but has to be kept | |
e6a23add | 722 | live due to potential error condition. Returns true if the |
723 | transformation actually happens. */ | |
724 | ||
48e1416a | 725 | static bool |
1a91d914 | 726 | shrink_wrap_one_built_in_call (gcall *bi_call) |
e6a23add | 727 | { |
75a70cf9 | 728 | gimple_stmt_iterator bi_call_bsi; |
e6a23add | 729 | basic_block bi_call_bb, join_tgt_bb, guard_bb, guard_bb0; |
730 | edge join_tgt_in_edge_from_call, join_tgt_in_edge_fall_thru; | |
731 | edge bi_call_in_edge0, guard_bb_in_edge; | |
e6a23add | 732 | unsigned tn_cond_stmts, nconds; |
733 | unsigned ci; | |
75a70cf9 | 734 | gimple cond_expr = NULL; |
735 | gimple cond_expr_start; | |
e6a23add | 736 | tree bi_call_label_decl; |
75a70cf9 | 737 | gimple bi_call_label; |
e6a23add | 738 | |
4997014d | 739 | auto_vec<gimple, 12> conds; |
e6a23add | 740 | gen_shrink_wrap_conditions (bi_call, conds, &nconds); |
741 | ||
742 | /* This can happen if the condition generator decides | |
743 | it is not beneficial to do the transformation. Just | |
48e1416a | 744 | return false and do not do any transformation for |
e6a23add | 745 | the call. */ |
746 | if (nconds == 0) | |
e85cf4e5 | 747 | return false; |
e6a23add | 748 | |
75a70cf9 | 749 | bi_call_bb = gimple_bb (bi_call); |
e6a23add | 750 | |
36ee0f30 | 751 | /* Now find the join target bb -- split bi_call_bb if needed. */ |
752 | if (stmt_ends_bb_p (bi_call)) | |
753 | { | |
754 | /* If the call must be the last in the bb, don't split the block, | |
755 | it could e.g. have EH edges. */ | |
756 | join_tgt_in_edge_from_call = find_fallthru_edge (bi_call_bb->succs); | |
757 | if (join_tgt_in_edge_from_call == NULL) | |
e85cf4e5 | 758 | return false; |
36ee0f30 | 759 | } |
760 | else | |
761 | join_tgt_in_edge_from_call = split_block (bi_call_bb, bi_call); | |
e6a23add | 762 | |
75a70cf9 | 763 | bi_call_bsi = gsi_for_stmt (bi_call); |
e6a23add | 764 | |
765 | join_tgt_bb = join_tgt_in_edge_from_call->dest; | |
766 | ||
767 | /* Now it is time to insert the first conditional expression | |
768 | into bi_call_bb and split this bb so that bi_call is | |
769 | shrink-wrapped. */ | |
f1f41a6c | 770 | tn_cond_stmts = conds.length (); |
e6a23add | 771 | cond_expr = NULL; |
f1f41a6c | 772 | cond_expr_start = conds[0]; |
e6a23add | 773 | for (ci = 0; ci < tn_cond_stmts; ci++) |
774 | { | |
f1f41a6c | 775 | gimple c = conds[ci]; |
e6a23add | 776 | gcc_assert (c || ci != 0); |
777 | if (!c) | |
778 | break; | |
75a70cf9 | 779 | gsi_insert_before (&bi_call_bsi, c, GSI_SAME_STMT); |
e6a23add | 780 | cond_expr = c; |
781 | } | |
782 | nconds--; | |
783 | ci++; | |
75a70cf9 | 784 | gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND); |
e6a23add | 785 | |
786 | /* Now the label. */ | |
e60a6f7b | 787 | bi_call_label_decl = create_artificial_label (gimple_location (bi_call)); |
75a70cf9 | 788 | bi_call_label = gimple_build_label (bi_call_label_decl); |
789 | gsi_insert_before (&bi_call_bsi, bi_call_label, GSI_SAME_STMT); | |
e6a23add | 790 | |
791 | bi_call_in_edge0 = split_block (bi_call_bb, cond_expr); | |
792 | bi_call_in_edge0->flags &= ~EDGE_FALLTHRU; | |
793 | bi_call_in_edge0->flags |= EDGE_TRUE_VALUE; | |
794 | guard_bb0 = bi_call_bb; | |
795 | bi_call_bb = bi_call_in_edge0->dest; | |
48e1416a | 796 | join_tgt_in_edge_fall_thru = make_edge (guard_bb0, join_tgt_bb, |
e6a23add | 797 | EDGE_FALSE_VALUE); |
798 | ||
799 | bi_call_in_edge0->probability = REG_BR_PROB_BASE * ERR_PROB; | |
9231bca5 | 800 | bi_call_in_edge0->count = |
801 | apply_probability (guard_bb0->count, | |
802 | bi_call_in_edge0->probability); | |
e6a23add | 803 | join_tgt_in_edge_fall_thru->probability = |
9231bca5 | 804 | inverse_probability (bi_call_in_edge0->probability); |
805 | join_tgt_in_edge_fall_thru->count = | |
806 | guard_bb0->count - bi_call_in_edge0->count; | |
e6a23add | 807 | |
808 | /* Code generation for the rest of the conditions */ | |
809 | guard_bb = guard_bb0; | |
810 | while (nconds > 0) | |
811 | { | |
812 | unsigned ci0; | |
813 | edge bi_call_in_edge; | |
75a70cf9 | 814 | gimple_stmt_iterator guard_bsi = gsi_for_stmt (cond_expr_start); |
e6a23add | 815 | ci0 = ci; |
f1f41a6c | 816 | cond_expr_start = conds[ci0]; |
e6a23add | 817 | for (; ci < tn_cond_stmts; ci++) |
818 | { | |
f1f41a6c | 819 | gimple c = conds[ci]; |
e6a23add | 820 | gcc_assert (c || ci != ci0); |
821 | if (!c) | |
822 | break; | |
75a70cf9 | 823 | gsi_insert_before (&guard_bsi, c, GSI_SAME_STMT); |
e6a23add | 824 | cond_expr = c; |
825 | } | |
826 | nconds--; | |
827 | ci++; | |
75a70cf9 | 828 | gcc_assert (cond_expr && gimple_code (cond_expr) == GIMPLE_COND); |
e6a23add | 829 | guard_bb_in_edge = split_block (guard_bb, cond_expr); |
830 | guard_bb_in_edge->flags &= ~EDGE_FALLTHRU; | |
831 | guard_bb_in_edge->flags |= EDGE_FALSE_VALUE; | |
832 | ||
833 | bi_call_in_edge = make_edge (guard_bb, bi_call_bb, EDGE_TRUE_VALUE); | |
834 | ||
835 | bi_call_in_edge->probability = REG_BR_PROB_BASE * ERR_PROB; | |
9231bca5 | 836 | bi_call_in_edge->count = |
837 | apply_probability (guard_bb->count, | |
838 | bi_call_in_edge->probability); | |
e6a23add | 839 | guard_bb_in_edge->probability = |
9231bca5 | 840 | inverse_probability (bi_call_in_edge->probability); |
841 | guard_bb_in_edge->count = guard_bb->count - bi_call_in_edge->count; | |
e6a23add | 842 | } |
843 | ||
e6a23add | 844 | if (dump_file && (dump_flags & TDF_DETAILS)) |
845 | { | |
846 | location_t loc; | |
75a70cf9 | 847 | loc = gimple_location (bi_call); |
e6a23add | 848 | fprintf (dump_file, |
849 | "%s:%d: note: function call is shrink-wrapped" | |
850 | " into error conditions.\n", | |
851 | LOCATION_FILE (loc), LOCATION_LINE (loc)); | |
852 | } | |
853 | ||
854 | return true; | |
855 | } | |
856 | ||
857 | /* The top level function for conditional dead code shrink | |
858 | wrapping transformation. */ | |
859 | ||
860 | static bool | |
1a91d914 | 861 | shrink_wrap_conditional_dead_built_in_calls (vec<gcall *> calls) |
e6a23add | 862 | { |
863 | bool changed = false; | |
864 | unsigned i = 0; | |
865 | ||
f1f41a6c | 866 | unsigned n = calls.length (); |
48e1416a | 867 | if (n == 0) |
e6a23add | 868 | return false; |
869 | ||
870 | for (; i < n ; i++) | |
871 | { | |
1a91d914 | 872 | gcall *bi_call = calls[i]; |
e6a23add | 873 | changed |= shrink_wrap_one_built_in_call (bi_call); |
874 | } | |
875 | ||
876 | return changed; | |
877 | } | |
878 | ||
65b0537f | 879 | namespace { |
880 | ||
881 | const pass_data pass_data_call_cdce = | |
882 | { | |
883 | GIMPLE_PASS, /* type */ | |
884 | "cdce", /* name */ | |
885 | OPTGROUP_NONE, /* optinfo_flags */ | |
65b0537f | 886 | TV_TREE_CALL_CDCE, /* tv_id */ |
887 | ( PROP_cfg | PROP_ssa ), /* properties_required */ | |
888 | 0, /* properties_provided */ | |
889 | 0, /* properties_destroyed */ | |
890 | 0, /* todo_flags_start */ | |
8b88439e | 891 | 0, /* todo_flags_finish */ |
65b0537f | 892 | }; |
e6a23add | 893 | |
65b0537f | 894 | class pass_call_cdce : public gimple_opt_pass |
895 | { | |
896 | public: | |
897 | pass_call_cdce (gcc::context *ctxt) | |
898 | : gimple_opt_pass (pass_data_call_cdce, ctxt) | |
899 | {} | |
900 | ||
901 | /* opt_pass methods: */ | |
902 | virtual bool gate (function *fun) | |
903 | { | |
904 | /* The limit constants used in the implementation | |
905 | assume IEEE floating point format. Other formats | |
906 | can be supported in the future if needed. */ | |
907 | return flag_tree_builtin_call_dce != 0 | |
908 | && optimize_function_for_speed_p (fun); | |
909 | } | |
e6a23add | 910 | |
65b0537f | 911 | virtual unsigned int execute (function *); |
912 | ||
913 | }; // class pass_call_cdce | |
914 | ||
915 | unsigned int | |
916 | pass_call_cdce::execute (function *fun) | |
e6a23add | 917 | { |
918 | basic_block bb; | |
75a70cf9 | 919 | gimple_stmt_iterator i; |
e6a23add | 920 | bool something_changed = false; |
1a91d914 | 921 | auto_vec<gcall *> cond_dead_built_in_calls; |
65b0537f | 922 | FOR_EACH_BB_FN (bb, fun) |
e6a23add | 923 | { |
924 | /* Collect dead call candidates. */ | |
75a70cf9 | 925 | for (i = gsi_start_bb (bb); !gsi_end_p (i); gsi_next (&i)) |
e6a23add | 926 | { |
1a91d914 | 927 | gcall *stmt = dyn_cast <gcall *> (gsi_stmt (i)); |
928 | if (stmt && is_call_dce_candidate (stmt)) | |
e6a23add | 929 | { |
930 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
931 | { | |
932 | fprintf (dump_file, "Found conditional dead call: "); | |
75a70cf9 | 933 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); |
e6a23add | 934 | fprintf (dump_file, "\n"); |
935 | } | |
f1f41a6c | 936 | if (!cond_dead_built_in_calls.exists ()) |
937 | cond_dead_built_in_calls.create (64); | |
938 | cond_dead_built_in_calls.safe_push (stmt); | |
e6a23add | 939 | } |
940 | } | |
941 | } | |
942 | ||
f1f41a6c | 943 | if (!cond_dead_built_in_calls.exists ()) |
86ed2c77 | 944 | return 0; |
945 | ||
946 | something_changed | |
947 | = shrink_wrap_conditional_dead_built_in_calls (cond_dead_built_in_calls); | |
e6a23add | 948 | |
e6a23add | 949 | if (something_changed) |
950 | { | |
951 | free_dominance_info (CDI_DOMINATORS); | |
952 | free_dominance_info (CDI_POST_DOMINATORS); | |
dd277d48 | 953 | /* As we introduced new control-flow we need to insert PHI-nodes |
954 | for the call-clobbers of the remaining call. */ | |
65b0537f | 955 | mark_virtual_operands_for_renaming (fun); |
560965e9 | 956 | return TODO_update_ssa; |
e6a23add | 957 | } |
560965e9 | 958 | |
959 | return 0; | |
e6a23add | 960 | } |
961 | ||
cbe8bda8 | 962 | } // anon namespace |
963 | ||
964 | gimple_opt_pass * | |
965 | make_pass_call_cdce (gcc::context *ctxt) | |
966 | { | |
967 | return new pass_call_cdce (ctxt); | |
968 | } |