LCOV - code coverage report
Current view: top level - gcc - tree-call-cdce.c (source / functions) Hit Total Coverage
Test: gcc.info Lines: 447 491 91.0 %
Date: 2020-04-04 11:58:09 Functions: 22 23 95.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 0 0 -

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

Generated by: LCOV version 1.0

LCOV profile is generated on x86_64 machine using following configure options: configure --disable-bootstrap --enable-coverage=opt --enable-languages=c,c++,fortran,go,jit,lto --enable-host-shared. GCC test suite is run with the built compiler.