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