LCOV - code coverage report
Current view: top level - gcc - tree-ssa-ifcombine.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 96.4 % 578 557
Test Date: 2026-12-05 16:10:02 Functions: 100.0 % 20 20
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /* Combining of if-expressions on trees.
       2              :    Copyright (C) 2007-2026 Free Software Foundation, Inc.
       3              :    Contributed by Richard Guenther <rguenther@suse.de>
       4              : 
       5              : This file is part of GCC.
       6              : 
       7              : GCC is free software; you can redistribute it and/or modify
       8              : it under the terms of the GNU General Public License as published by
       9              : the Free Software Foundation; either version 3, or (at your option)
      10              : any later version.
      11              : 
      12              : GCC is distributed in the hope that it will be useful,
      13              : but WITHOUT ANY WARRANTY; without even the implied warranty of
      14              : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15              : GNU General Public License 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 "rtl.h"
      26              : #include "tree.h"
      27              : #include "gimple.h"
      28              : #include "cfghooks.h"
      29              : #include "tree-pass.h"
      30              : #include "memmodel.h"
      31              : #include "tm_p.h"
      32              : #include "ssa.h"
      33              : #include "tree-pretty-print.h"
      34              : /* rtl is needed only because arm back-end requires it for
      35              :    BRANCH_COST.  */
      36              : #include "fold-const.h"
      37              : #include "cfganal.h"
      38              : #include "gimple-iterator.h"
      39              : #include "gimple-fold.h"
      40              : #include "gimplify-me.h"
      41              : #include "tree-cfg.h"
      42              : #include "tree-ssa.h"
      43              : #include "tree-ssa-ifcombine.h"
      44              : #include "attribs.h"
      45              : #include "asan.h"
      46              : #include "bitmap.h"
      47              : #include "cfgloop.h"
      48              : 
      49              : #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
      50              : #define LOGICAL_OP_NON_SHORT_CIRCUIT \
      51              :   (BRANCH_COST (optimize_function_for_speed_p (cfun), \
      52              :                 false) >= 2)
      53              : #endif
      54              : 
      55              : /* Return FALSE iff the COND_BB ends with a conditional whose result is not a
      56              :    known constant.  */
      57              : 
      58              : static bool
      59     32649872 : known_succ_p (basic_block cond_bb)
      60              : {
      61     67286823 :   gcond *cond = safe_dyn_cast <gcond *> (*gsi_last_bb (cond_bb));
      62              : 
      63     30957231 :   if (!cond)
      64              :     return true;
      65              : 
      66     30957231 :   return (CONSTANT_CLASS_P (gimple_cond_lhs (cond))
      67     30957231 :           && CONSTANT_CLASS_P (gimple_cond_rhs (cond)));
      68              : }
      69              : 
      70              : /* This pass combines COND_EXPRs to simplify control flow.  It
      71              :    currently recognizes bit tests and comparisons in chains that
      72              :    represent logical and or logical or of two COND_EXPRs.
      73              : 
      74              :    It does so by walking basic blocks in a approximate reverse
      75              :    post-dominator order and trying to match CFG patterns that
      76              :    represent logical and or logical or of two COND_EXPRs.
      77              :    Transformations are done if the COND_EXPR conditions match
      78              :    either
      79              : 
      80              :      1. two single bit tests X & (1 << Yn) (for logical and)
      81              : 
      82              :      2. two bit tests X & Yn (for logical or)
      83              : 
      84              :      3. two comparisons X OPn Y (for logical or)
      85              : 
      86              :    To simplify this pass, removing basic blocks and dead code
      87              :    is left to CFG cleanup and DCE.  */
      88              : 
      89              : 
      90              : /* Recognize a if-then-else CFG pattern starting to match with the COND_BB
      91              :    basic-block containing the COND_EXPR.  If !SUCCS_ANY, the condition must not
      92              :    resolve to a constant for a match.  Returns true if the pattern matched,
      93              :    false otherwise.  In case of a !SUCCS_ANY match, the recognized then end
      94              :    else blocks are stored to *THEN_BB and *ELSE_BB.  If *THEN_BB and/or
      95              :    *ELSE_BB are already set, they are required to match the then and else
      96              :    basic-blocks to make the pattern match.  If SUCCS_ANY, *THEN_BB and *ELSE_BB
      97              :    will not be filled in, and they will be found to match even if reversed.  */
      98              : 
      99              : bool
     100     36039041 : recognize_if_then_else (basic_block cond_bb,
     101              :                         basic_block *then_bb, basic_block *else_bb,
     102              :                         bool succs_any)
     103              : {
     104     36039041 :   edge t, e;
     105              : 
     106     36039041 :   if (EDGE_COUNT (cond_bb->succs) != 2
     107     36039041 :       || (!succs_any && known_succ_p (cond_bb)))
     108              :     return false;
     109              : 
     110              :   /* Find the then/else edges.  */
     111     31148203 :   t = EDGE_SUCC (cond_bb, 0);
     112     31148203 :   e = EDGE_SUCC (cond_bb, 1);
     113              : 
     114     31148203 :   if (succs_any)
     115       606249 :     return ((t->dest == *then_bb && e->dest == *else_bb)
     116      2493406 :             || (t->dest == *else_bb && e->dest == *then_bb));
     117              : 
     118     28984115 :   if (!(t->flags & EDGE_TRUE_VALUE))
     119      2687007 :     std::swap (t, e);
     120     28984115 :   if (!(t->flags & EDGE_TRUE_VALUE)
     121     28984115 :       || !(e->flags & EDGE_FALSE_VALUE))
     122              :     return false;
     123              : 
     124              :   /* Check if the edge destinations point to the required block.  */
     125     28984115 :   if (*then_bb
     126     22469058 :       && t->dest != *then_bb)
     127              :     return false;
     128     24046194 :   if (*else_bb
     129      2518359 :       && e->dest != *else_bb)
     130              :     return false;
     131              : 
     132     23094459 :   if (!*then_bb)
     133      6515057 :     *then_bb = t->dest;
     134     23094459 :   if (!*else_bb)
     135     21527835 :     *else_bb = e->dest;
     136              : 
     137              :   return true;
     138              : }
     139              : 
     140              : /* Verify if the basic block BB does not have side-effects.  Return
     141              :    true in this case, else false.  */
     142              : 
     143              : static bool
     144      4514474 : bb_no_side_effects_p (basic_block bb)
     145              : {
     146      4514474 :   gimple_stmt_iterator gsi;
     147              : 
     148     25604485 :   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
     149              :     {
     150     18998212 :       gimple *stmt = gsi_stmt (gsi);
     151              : 
     152     18998212 :       if (is_gimple_debug (stmt))
     153     11166361 :         continue;
     154              : 
     155      7831851 :       gassign *ass;
     156      7831851 :       enum tree_code rhs_code;
     157      7831851 :       if (gimple_has_side_effects (stmt)
     158              :           /* Ignore GIMPLE_COND for trapping.  */
     159      6964438 :           || (!is_a<gcond*>(stmt)
     160      4872487 :               && gimple_could_trap_p (stmt))
     161      6082533 :           || gimple_vdef (stmt)
     162              :           /* We need to rewrite stmts with undefined overflow to use
     163              :              unsigned arithmetic but cannot do so for signed division.  */
     164      9059032 :           || ((ass = dyn_cast <gassign *> (stmt))
     165      3247036 :               && INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_lhs (ass)))
     166      4902552 :               && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (gimple_assign_lhs (ass)))
     167       849884 :               && ((rhs_code = gimple_assign_rhs_code (ass)), true)
     168       849884 :               && (rhs_code == TRUNC_DIV_EXPR
     169              :                   || rhs_code == CEIL_DIV_EXPR
     170              :                   || rhs_code == FLOOR_DIV_EXPR
     171       849884 :                   || rhs_code == ROUND_DIV_EXPR)
     172              :               /* We cannot use expr_not_equal_to since we'd have to restrict
     173              :                  flow-sensitive info to whats known at the outer if.  */
     174         1883 :               && (TREE_CODE (gimple_assign_rhs2 (ass)) != INTEGER_CST
     175         1883 :                   || !integer_minus_onep (gimple_assign_rhs2 (ass))))
     176              :           /* const calls don't match any of the above, yet they could
     177              :              still have some side-effects - they could contain
     178              :              gimple_could_trap_p statements, like floating point
     179              :              exceptions or integer division by zero.  See PR70586.
     180              :              FIXME: perhaps gimple_has_side_effects or gimple_could_trap_p
     181              :              should handle this.  */
     182     16095123 :           || is_gimple_call (stmt))
     183      2422675 :         return false;
     184              : 
     185      5413806 :       ssa_op_iter it;
     186      5413806 :       tree use;
     187     10797216 :       FOR_EACH_SSA_TREE_OPERAND (use, stmt, it, SSA_OP_USE)
     188      5388040 :         if (ssa_name_maybe_undef_p (use))
     189              :           return false;
     190              :     }
     191              : 
     192              :   return true;
     193              : }
     194              : 
     195              : /* Return true if BB is an empty forwarder block to TO_BB.  */
     196              : 
     197              : static bool
     198      2528396 : forwarder_block_to (basic_block bb, basic_block to_bb)
     199              : {
     200      2528396 :   return empty_block_p (bb)
     201       360062 :          && single_succ_p (bb)
     202      2888458 :          && single_succ (bb) == to_bb;
     203              : }
     204              : 
     205              : /* Verify if all PHI node arguments in DEST for edges from BB1 or
     206              :    BB2 to DEST are the same.  This makes the CFG merge point
     207              :    free from side-effects.  Return true in this case, else false.  */
     208              : 
     209              : static bool
     210      1136919 : same_phi_args_p (basic_block bb1, basic_block bb2, basic_block dest)
     211              : {
     212      1136919 :   edge e1 = find_edge (bb1, dest);
     213      1136919 :   edge e2 = find_edge (bb2, dest);
     214      1136919 :   gphi_iterator gsi;
     215      1136919 :   gphi *phi;
     216              : 
     217      1398711 :   for (gsi = gsi_start_phis (dest); !gsi_end_p (gsi); gsi_next (&gsi))
     218              :     {
     219       350203 :       phi = gsi.phi ();
     220       350203 :       if (!operand_equal_p (PHI_ARG_DEF_FROM_EDGE (phi, e1),
     221       350203 :                             PHI_ARG_DEF_FROM_EDGE (phi, e2), 0))
     222              :         return false;
     223              :     }
     224              : 
     225              :   return true;
     226              : }
     227              : 
     228              : /* Return the best representative SSA name for CANDIDATE which is used
     229              :    in a bit test.  */
     230              : 
     231              : static tree
     232         7703 : get_name_for_bit_test (tree candidate)
     233              : {
     234              :   /* Skip single-use names in favor of using the name from a
     235              :      non-widening conversion definition.  */
     236         7703 :   if (TREE_CODE (candidate) == SSA_NAME
     237         7703 :       && has_single_use (candidate))
     238              :     {
     239         7055 :       auto *def_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (candidate));
     240         7267 :       if (def_stmt && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
     241              :         {
     242           70 :           if (TYPE_PRECISION (TREE_TYPE (candidate))
     243           70 :               <= TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (def_stmt))))
     244              :             return gimple_assign_rhs1 (def_stmt);
     245              :         }
     246              :     }
     247              : 
     248              :   return candidate;
     249              : }
     250              : 
     251              : /* Recognize a single bit test pattern in GIMPLE_COND and its defining
     252              :    statements.  Store the name being tested in *NAME and the bit
     253              :    in *BIT.  The GIMPLE_COND computes *NAME & (1 << *BIT).
     254              :    Returns true if the pattern matched, false otherwise.  */
     255              : 
     256              : static bool
     257       455083 : recognize_single_bit_test (gcond *cond, tree *name, tree *bit, bool inv)
     258              : {
     259       455083 :   gimple *stmt;
     260              : 
     261              :   /* Get at the definition of the result of the bit test.  */
     262       455083 :   if (gimple_cond_code (cond) != (inv ? EQ_EXPR : NE_EXPR)
     263       115534 :       || TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
     264       570602 :       || !integer_zerop (gimple_cond_rhs (cond)))
     265              :     return false;
     266        89166 :   stmt = SSA_NAME_DEF_STMT (gimple_cond_lhs (cond));
     267        89166 :   if (!is_gimple_assign (stmt))
     268              :     return false;
     269              : 
     270              :   /* Look at which bit is tested.  One form to recognize is
     271              :      D.1985_5 = state_3(D) >> control1_4(D);
     272              :      D.1986_6 = (int) D.1985_5;
     273              :      D.1987_7 = op0 & 1;
     274              :      if (D.1987_7 != 0)  */
     275        84773 :   if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
     276        11297 :       && integer_onep (gimple_assign_rhs2 (stmt))
     277        85269 :       && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
     278              :     {
     279          496 :       tree orig_name = gimple_assign_rhs1 (stmt);
     280              : 
     281              :       /* Look through copies and conversions to eventually
     282              :          find the stmt that computes the shift.  */
     283          496 :       stmt = SSA_NAME_DEF_STMT (orig_name);
     284              : 
     285          497 :       while (is_gimple_assign (stmt)
     286          497 :              && ((CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
     287            1 :                   && (TYPE_PRECISION (TREE_TYPE (gimple_assign_lhs (stmt)))
     288            1 :                       <= TYPE_PRECISION (TREE_TYPE (gimple_assign_rhs1 (stmt))))
     289            1 :                   && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
     290          378 :                  || gimple_assign_ssa_name_copy_p (stmt)))
     291            1 :         stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
     292              : 
     293              :       /* If we found such, decompose it.  */
     294          496 :       if (is_gimple_assign (stmt)
     295          496 :           && gimple_assign_rhs_code (stmt) == RSHIFT_EXPR)
     296              :         {
     297              :           /* op0 & (1 << op1) */
     298          119 :           *bit = gimple_assign_rhs2 (stmt);
     299          119 :           *name = gimple_assign_rhs1 (stmt);
     300              :         }
     301              :       else
     302              :         {
     303              :           /* t & 1 */
     304          377 :           *bit = integer_zero_node;
     305          377 :           *name = get_name_for_bit_test (orig_name);
     306              :         }
     307              : 
     308              :       return true;
     309              :     }
     310              : 
     311              :   /* Another form is
     312              :      D.1987_7 = op0 & (1 << CST)
     313              :      if (D.1987_7 != 0)  */
     314        84277 :   if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
     315        10801 :       && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
     316        95078 :       && integer_pow2p (gimple_assign_rhs2 (stmt)))
     317              :     {
     318         5615 :       *name = gimple_assign_rhs1 (stmt);
     319         5615 :       *bit = build_int_cst (integer_type_node,
     320         5615 :                             tree_log2 (gimple_assign_rhs2 (stmt)));
     321         5615 :       return true;
     322              :     }
     323              : 
     324              :   /* Another form is
     325              :      D.1986_6 = 1 << control1_4(D)
     326              :      D.1987_7 = op0 & D.1986_6
     327              :      if (D.1987_7 != 0)  */
     328        78662 :   if (gimple_assign_rhs_code (stmt) == BIT_AND_EXPR
     329         5186 :       && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
     330        83848 :       && TREE_CODE (gimple_assign_rhs2 (stmt)) == SSA_NAME)
     331              :     {
     332         4069 :       gimple *tmp;
     333              : 
     334              :       /* Both arguments of the BIT_AND_EXPR can be the single-bit
     335              :          specifying expression.  */
     336         4069 :       tmp = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt)));
     337         3928 :       if (tmp
     338         3928 :           && gimple_assign_rhs_code (tmp) == LSHIFT_EXPR
     339           19 :           && integer_onep (gimple_assign_rhs1 (tmp)))
     340              :         {
     341           19 :           *name = gimple_assign_rhs2 (stmt);
     342           19 :           *bit = gimple_assign_rhs2 (tmp);
     343           19 :           return true;
     344              :         }
     345              : 
     346         4050 :       tmp = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt)));
     347         3959 :       if (tmp
     348         3959 :           && gimple_assign_rhs_code (tmp) == LSHIFT_EXPR
     349           48 :           && integer_onep (gimple_assign_rhs1 (tmp)))
     350              :         {
     351           48 :           *name = gimple_assign_rhs1 (stmt);
     352           48 :           *bit = gimple_assign_rhs2 (tmp);
     353           48 :           return true;
     354              :         }
     355              :     }
     356              : 
     357              :   return false;
     358              : }
     359              : 
     360              : /* Recognize a bit test pattern in a GIMPLE_COND and its defining
     361              :    statements.  Store the name being tested in *NAME and the bits
     362              :    in *BITS.  The COND_EXPR computes *NAME & *BITS.
     363              :    Returns true if the pattern matched, false otherwise.  */
     364              : 
     365              : static bool
     366       460671 : recognize_bits_test (gcond *cond, tree *name, tree *bits, bool inv)
     367              : {
     368              :   /* Get at the definition of the result of the bit test.  */
     369       460671 :   if (gimple_cond_code (cond) != (inv ? EQ_EXPR : NE_EXPR)
     370       280610 :       || TREE_CODE (gimple_cond_lhs (cond)) != SSA_NAME
     371       741268 :       || !integer_zerop (gimple_cond_rhs (cond)))
     372              :     return false;
     373        56898 :   auto stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (gimple_cond_lhs (cond)));
     374        78356 :   if (!stmt || gimple_assign_rhs_code (stmt) != BIT_AND_EXPR)
     375              :     return false;
     376              : 
     377        16010 :   *name = gimple_assign_rhs1 (stmt);
     378        16010 :   *bits = gimple_assign_rhs2 (stmt);
     379              : 
     380        16010 :   return true;
     381              : }
     382              : 
     383              : 
     384              : /* Update profile after code in either outer_cond_bb or inner_cond_bb was
     385              :    adjusted so that it has no condition.  */
     386              : 
     387              : static void
     388        94499 : update_profile_after_ifcombine (basic_block inner_cond_bb,
     389              :                                 basic_block outer_cond_bb)
     390              : {
     391              :   /* In the following we assume that inner_cond_bb has single predecessor.  */
     392        94499 :   gcc_assert (single_pred_p (inner_cond_bb));
     393              : 
     394        94499 :   basic_block outer_to_inner_bb = inner_cond_bb;
     395        94499 :   profile_probability prob = profile_probability::always ();
     396        94762 :   for (;;)
     397              :     {
     398        94762 :       basic_block parent = single_pred (outer_to_inner_bb);
     399        94762 :       prob *= find_edge (parent, outer_to_inner_bb)->probability;
     400        94762 :       if (parent == outer_cond_bb)
     401              :         break;
     402              :       outer_to_inner_bb = parent;
     403              :     }
     404              : 
     405        94499 :   edge outer_to_inner = find_edge (outer_cond_bb, outer_to_inner_bb);
     406        94499 :   edge outer2 = (EDGE_SUCC (outer_cond_bb, 0) == outer_to_inner
     407        42800 :                  ? EDGE_SUCC (outer_cond_bb, 1)
     408       137299 :                  : EDGE_SUCC (outer_cond_bb, 0));
     409        94499 :   edge inner_taken = EDGE_SUCC (inner_cond_bb, 0);
     410        94499 :   edge inner_not_taken = EDGE_SUCC (inner_cond_bb, 1);
     411              : 
     412        94499 :   if (inner_taken->dest != outer2->dest)
     413        36355 :     std::swap (inner_taken, inner_not_taken);
     414        94499 :   gcc_assert (inner_taken->dest == outer2->dest);
     415              : 
     416        94499 :   if (outer_to_inner_bb == inner_cond_bb
     417        94499 :       && known_succ_p (outer_cond_bb))
     418              :     {
     419              :       /* Path outer_cond_bb->(outer2) needs to be merged into path
     420              :          outer_cond_bb->(outer_to_inner)->inner_cond_bb->(inner_taken)
     421              :          and probability of inner_not_taken updated.  */
     422              : 
     423        94166 :       inner_cond_bb->count = outer_cond_bb->count;
     424              : 
     425              :       /* Handle special case where inner_taken probability is always. In this
     426              :          case we know that the overall outcome will be always as well, but
     427              :          combining probabilities will be conservative because it does not know
     428              :          that outer2->probability is inverse of
     429              :          outer_to_inner->probability.  */
     430        94166 :       if (inner_taken->probability == profile_probability::always ())
     431              :         ;
     432              :       else
     433        90996 :         inner_taken->probability = outer2->probability
     434        90996 :           + outer_to_inner->probability * inner_taken->probability;
     435        94166 :       inner_not_taken->probability = profile_probability::always ()
     436        94166 :         - inner_taken->probability;
     437              : 
     438        94166 :       outer_to_inner->probability = profile_probability::always ();
     439        94166 :       outer2->probability = profile_probability::never ();
     440              :     }
     441          333 :   else if (known_succ_p (inner_cond_bb))
     442              :     {
     443              :       /* Path inner_cond_bb->(inner_taken) needs to be merged into path
     444              :          outer_cond_bb->(outer2).  We've accumulated the probabilities from
     445              :          outer_cond_bb->(outer)->...->inner_cond_bb in prob, so we have to
     446              :          adjust that by inner_taken, and make inner unconditional.  */
     447              : 
     448          212 :       prob *= inner_taken->probability;
     449          212 :       outer2->probability += prob;
     450          212 :       outer_to_inner->probability = profile_probability::always ()
     451          212 :         - outer2->probability;
     452              : 
     453          212 :       inner_taken->probability = profile_probability::never ();
     454          212 :       inner_not_taken->probability = profile_probability::always ();
     455              :     }
     456              :   else
     457              :     {
     458              :       /* We've moved part of the inner cond to outer, but we don't know the
     459              :          probabilities for each part, so estimate the effects by moving half of
     460              :          the odds of inner_taken to outer.  */
     461              : 
     462          121 :       inner_taken->probability *= profile_probability::even ();
     463          121 :       inner_not_taken->probability = profile_probability::always ()
     464          121 :         - inner_taken->probability;
     465              : 
     466          121 :       prob *= inner_taken->probability;
     467          121 :       outer2->probability += prob;
     468          121 :       outer_to_inner->probability = profile_probability::always ()
     469          121 :         - outer2->probability;
     470              :     }
     471        94499 : }
     472              : 
     473              : /* Set NAME's bit in USED if OUTER dominates it.  */
     474              : 
     475              : static void
     476         1416 : ifcombine_mark_ssa_name (bitmap used, tree name, basic_block outer)
     477              : {
     478         1416 :   if (!name || TREE_CODE (name) != SSA_NAME || SSA_NAME_IS_DEFAULT_DEF (name))
     479              :     return;
     480              : 
     481          670 :   gimple *def = SSA_NAME_DEF_STMT (name);
     482          670 :   basic_block bb = gimple_bb (def);
     483          670 :   if (!dominated_by_p (CDI_DOMINATORS, bb, outer))
     484              :     return;
     485              : 
     486          463 :   bitmap_set_bit (used, SSA_NAME_VERSION (name));
     487              : }
     488              : 
     489              : /* Data structure passed to ifcombine_mark_ssa_name.  */
     490              : struct ifcombine_mark_ssa_name_t
     491              : {
     492              :   /* SSA_NAMEs that have been referenced.  */
     493              :   bitmap used;
     494              :   /* Dominating block of DEFs that might need moving.  */
     495              :   basic_block outer;
     496              : };
     497              : 
     498              : /* Mark in DATA->used any SSA_NAMEs used in *t.  */
     499              : 
     500              : static tree
     501         1392 : ifcombine_mark_ssa_name_walk (tree *t, int *, void *data_)
     502              : {
     503         1392 :   ifcombine_mark_ssa_name_t *data = (ifcombine_mark_ssa_name_t *)data_;
     504              : 
     505         1392 :   ifcombine_mark_ssa_name (data->used, *t, data->outer);
     506              : 
     507         1392 :   return NULL;
     508              : }
     509              : 
     510              : /* Rewrite a stmt, that presumably used to be guarded by conditions that could
     511              :    avoid undefined overflow, into one that has well-defined overflow, so that
     512              :    it won't invoke undefined behavior once the guarding conditions change.  */
     513              : 
     514              : static inline void
     515       427962 : ifcombine_rewrite_to_defined_overflow (gimple_stmt_iterator gsi)
     516              : {
     517       427962 :   if (!gimple_needing_rewrite_undefined (gsi_stmt (gsi)))
     518              :     return;
     519           36 :   rewrite_to_defined_unconditional (&gsi);
     520              : }
     521              : 
     522              : 
     523              : /* Replace the conditions in INNER_COND and OUTER_COND with COND and COND2.
     524              :    COND and COND2 are computed for insertion at INNER_COND, with OUTER_COND
     525              :    replaced with a constant, but if there are intervening blocks, it's best to
     526              :    adjust COND for insertion at OUTER_COND, placing COND2 at INNER_COND.  */
     527              : 
     528              : static bool
     529        94615 : ifcombine_replace_cond (gcond *inner_cond, bool inner_inv,
     530              :                         gcond *outer_cond, bool outer_inv,
     531              :                         tree cond, bool must_canon, tree cond2)
     532              : {
     533        94615 :   bool split_single_cond = false;
     534              :   /* Split cond into cond2 if they're contiguous.  ??? We might be able to
     535              :      handle ORIF as well, inverting both conditions, but it's not clear that
     536              :      this would be enough, and it never comes up.  */
     537        94615 :   if (!cond2
     538        94609 :       && TREE_CODE (cond) == TRUTH_ANDIF_EXPR
     539        94751 :       && single_pred (gimple_bb (inner_cond)) == gimple_bb (outer_cond))
     540              :     {
     541          115 :       cond2 = TREE_OPERAND (cond, 1);
     542          115 :       cond = TREE_OPERAND (cond, 0);
     543          115 :       split_single_cond = true;
     544              :     }
     545              : 
     546        94615 :   bool outer_p = cond2 || (single_pred (gimple_bb (inner_cond))
     547        94494 :                            != gimple_bb (outer_cond));
     548              :   bool result_inv = outer_p ? outer_inv : inner_inv;
     549        94615 :   bool strictening_outer_cond = !split_single_cond && outer_p;
     550              : 
     551        94615 :   if (result_inv)
     552        59569 :     cond = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (cond), cond);
     553              : 
     554        94615 :   if (tree tcanon = canonicalize_cond_expr_cond (cond))
     555         6103 :     cond = tcanon;
     556        88512 :   else if (must_canon)
     557              :     return false;
     558              : 
     559        94500 :   if (outer_p)
     560              :     {
     561          334 :       {
     562          334 :         auto_bitmap used;
     563          334 :         basic_block outer_bb = gimple_bb (outer_cond);
     564              : 
     565          334 :         bitmap_tree_view (used);
     566              : 
     567              :         /* Mark SSA DEFs that are referenced by cond and may thus need to be
     568              :            moved to outer.  */
     569          334 :         {
     570          334 :           ifcombine_mark_ssa_name_t data = { used, outer_bb };
     571          334 :           walk_tree (&cond, ifcombine_mark_ssa_name_walk, &data, NULL);
     572              :         }
     573              : 
     574          334 :         if (!bitmap_empty_p (used))
     575              :           {
     576          241 :             const int max_stmts = 6;
     577          241 :             auto_vec<gimple *, max_stmts> stmts;
     578              : 
     579              :             /* Iterate up from inner_cond, moving DEFs identified as used by
     580              :                cond, and marking USEs in the DEFs for moving as well.  */
     581          651 :             for (basic_block bb = gimple_bb (inner_cond);
     582          651 :                  bb != outer_bb; bb = single_pred (bb))
     583              :               {
     584          822 :                 for (gimple_stmt_iterator gsitr = gsi_last_bb (bb);
     585         2748 :                      !gsi_end_p (gsitr); gsi_prev (&gsitr))
     586              :                   {
     587         1926 :                     gimple *stmt = gsi_stmt (gsitr);
     588         1926 :                     bool move = false;
     589         1926 :                     tree t;
     590         1926 :                     ssa_op_iter it;
     591              : 
     592         2927 :                     FOR_EACH_SSA_TREE_OPERAND (t, stmt, it, SSA_OP_DEF)
     593         1143 :                       if (bitmap_bit_p (used, SSA_NAME_VERSION (t)))
     594              :                         {
     595              :                           move = true;
     596              :                           break;
     597              :                         }
     598              : 
     599         1926 :                     if (!move)
     600         1784 :                       continue;
     601              : 
     602          142 :                     if (stmts.length () < max_stmts)
     603          142 :                       stmts.quick_push (stmt);
     604              :                     else
     605            0 :                       return false;
     606              : 
     607              :                     /* Mark uses in STMT before moving it.  */
     608          162 :                     FOR_EACH_SSA_TREE_OPERAND (t, stmt, it, SSA_OP_USE)
     609           20 :                       ifcombine_mark_ssa_name (used, t, outer_bb);
     610              :                   }
     611              : 
     612              :                 /* Surprisingly, there may be PHI nodes in single-predecessor
     613              :                    bocks, as in pr50682.C.  Fortunately, since they can't
     614              :                    involve back edges, there won't be references to parallel
     615              :                    nodes that we'd have to pay special attention to to keep
     616              :                    them parallel.  We can't move the PHI nodes, but we can turn
     617              :                    them into assignments.  */
     618          411 :                 for (gphi_iterator gsi = gsi_start_phis (bb);
     619          415 :                      !gsi_end_p (gsi);)
     620              :                   {
     621            5 :                     gphi *phi = gsi.phi ();
     622              : 
     623            5 :                     gcc_assert (gimple_phi_num_args (phi) == 1);
     624            5 :                     tree def = gimple_phi_result (phi);
     625              : 
     626            5 :                     if (!bitmap_bit_p (used, SSA_NAME_VERSION (def)))
     627              :                       {
     628            0 :                         gsi_next (&gsi);
     629            0 :                         continue;
     630              :                       }
     631              : 
     632            5 :                     if (stmts.length () < max_stmts)
     633            4 :                       stmts.quick_push (phi);
     634              :                     else
     635            1 :                       return false;
     636              : 
     637              :                     /* Mark uses in STMT before moving it.  */
     638            4 :                     use_operand_p use_p;
     639            4 :                     ssa_op_iter it;
     640            8 :                     FOR_EACH_PHI_ARG (use_p, phi, it, SSA_OP_USE)
     641            4 :                       ifcombine_mark_ssa_name (used, USE_FROM_PTR (use_p),
     642              :                                                outer_bb);
     643              :                   }
     644              :               }
     645              : 
     646              :             /* ??? Test whether it makes sense to move STMTS.  */
     647              : 
     648              :             /* Move the STMTS that need moving.  From this point on, we're
     649              :                committing to the attempted ifcombine.  */
     650          240 :             gimple_stmt_iterator gsins = gsi_for_stmt (outer_cond);
     651          240 :             unsigned i;
     652          240 :             gimple *stmt;
     653          620 :             FOR_EACH_VEC_ELT (stmts, i, stmt)
     654              :               {
     655          140 :                 if (gphi *phi = dyn_cast <gphi *> (stmt))
     656              :                   {
     657            0 :                     tree def = gimple_phi_result (phi);
     658            0 :                     tree use = gimple_phi_arg_def (phi, 0);
     659            0 :                     location_t loc = gimple_phi_arg_location (phi, 0);
     660              : 
     661            0 :                     gphi_iterator gsi = gsi_for_phi (phi);
     662            0 :                     remove_phi_node (&gsi, false);
     663              : 
     664            0 :                     gassign *a = gimple_build_assign (def, use);
     665            0 :                     gimple_set_location (a, loc);
     666            0 :                     gsi_insert_before (&gsins, a, GSI_NEW_STMT);
     667              :                   }
     668              :                 else
     669              :                   {
     670          140 :                     gimple_stmt_iterator gsitr = gsi_for_stmt (stmt);
     671          140 :                     gsi_move_before (&gsitr, &gsins, GSI_NEW_STMT);
     672              :                   }
     673              :               }
     674              : 
     675          380 :             for (; gsi_stmt (gsins) != outer_cond; gsi_next (&gsins))
     676              :               {
     677              :                 /* Clear range info from all defs we've moved from under
     678              :                    conditions.  */
     679          140 :                 tree t;
     680          140 :                 ssa_op_iter it;
     681          280 :                 FOR_EACH_SSA_TREE_OPERAND (t, gsi_stmt (gsins), it, SSA_OP_DEF)
     682          140 :                   reset_flow_sensitive_info (t);
     683              :                 /* Avoid introducing undefined overflows while at that.  */
     684          140 :                 ifcombine_rewrite_to_defined_overflow (gsins);
     685              :               }
     686          241 :           }
     687            1 :       }
     688              : 
     689          333 :       if (!is_gimple_condexpr_for_cond (cond))
     690              :         {
     691           99 :           gimple_stmt_iterator gsi = gsi_for_stmt (outer_cond);
     692           99 :           cond = force_gimple_operand_gsi_1 (&gsi, cond,
     693              :                                              is_gimple_condexpr_for_cond,
     694              :                                              NULL, true, GSI_SAME_STMT);
     695              :         }
     696              : 
     697              :       /* Leave CFG optimization to cfg_cleanup.  */
     698          333 :       gimple_cond_set_condition_from_tree (outer_cond, cond);
     699          333 :       update_stmt (outer_cond);
     700              : 
     701          333 :       if (cond2)
     702              :         {
     703          121 :           if (inner_inv)
     704          115 :             cond2 = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (cond2), cond2);
     705              : 
     706          121 :           if (tree tcanon = canonicalize_cond_expr_cond (cond2))
     707           49 :             cond2 = tcanon;
     708          121 :           if (!is_gimple_condexpr_for_cond (cond2))
     709              :             {
     710           72 :               gimple_stmt_iterator gsi = gsi_for_stmt (inner_cond);
     711           72 :               cond2 = force_gimple_operand_gsi_1 (&gsi, cond2,
     712              :                                                   is_gimple_condexpr_for_cond,
     713              :                                                   NULL, true, GSI_SAME_STMT);
     714              :             }
     715          121 :           gimple_cond_set_condition_from_tree (inner_cond, cond2);
     716              :         }
     717              :       else
     718          212 :         gimple_cond_set_condition_from_tree (inner_cond,
     719              :                                              inner_inv
     720              :                                              ? boolean_false_node
     721              :                                              : boolean_true_node);
     722          333 :       update_stmt (inner_cond);
     723              :     }
     724              :   else
     725              :     {
     726        94166 :       if (!is_gimple_condexpr_for_cond (cond))
     727              :         {
     728        88298 :           gimple_stmt_iterator gsi = gsi_for_stmt (inner_cond);
     729        88298 :           cond = force_gimple_operand_gsi_1 (&gsi, cond,
     730              :                                              is_gimple_condexpr_for_cond,
     731              :                                              NULL, true, GSI_SAME_STMT);
     732              :         }
     733        94166 :       gimple_cond_set_condition_from_tree (inner_cond, cond);
     734        94166 :       update_stmt (inner_cond);
     735              : 
     736              :       /* Leave CFG optimization to cfg_cleanup.  */
     737        94166 :       gimple_cond_set_condition_from_tree (outer_cond,
     738              :                                            outer_inv
     739              :                                            ? boolean_false_node
     740              :                                            : boolean_true_node);
     741        94166 :       update_stmt (outer_cond);
     742              :     }
     743              : 
     744              :   /* We're changing conditions that guard inner blocks, so reset flow sensitive
     745              :      info and avoid introducing undefined behavior.  */
     746       189261 :   for (basic_block bb = gimple_bb (inner_cond), end = gimple_bb (outer_cond);
     747       189261 :        bb != end; bb = single_pred (bb))
     748              :     {
     749              :       /* Clear range info from all stmts in BB which is now guarded by
     750              :          different conditionals.  */
     751        94762 :       reset_flow_sensitive_info_in_bb (gimple_bb (inner_cond));
     752              : 
     753              :       /* We only need to worry about introducing undefined behavior if we've
     754              :          relaxed the outer condition.  */
     755        94762 :       if (strictening_outer_cond)
     756          481 :         continue;
     757              : 
     758              :       /* Avoid introducing undefined behavior as we move stmts that used to be
     759              :          guarded by OUTER_COND.  */
     760       188562 :       for (gimple_stmt_iterator gsi = gsi_start_bb (gimple_bb (inner_cond));
     761       522103 :            !gsi_end_p (gsi); gsi_next (&gsi))
     762       427822 :         ifcombine_rewrite_to_defined_overflow (gsi);
     763              :     }
     764              : 
     765        94499 :   update_profile_after_ifcombine (gimple_bb (inner_cond),
     766              :                                   gimple_bb (outer_cond));
     767              : 
     768        94499 :   return true;
     769              : }
     770              : 
     771              : /* Returns true if inner_cond_bb contains just the condition or 1/2 statements
     772              :    that define lhs or rhs with an integer conversion. */
     773              : 
     774              : static bool
     775       226938 : can_combine_bbs_with_short_circuit (basic_block inner_cond_bb, tree lhs, tree rhs)
     776              : {
     777       226938 :   gimple_stmt_iterator gsi;
     778       226938 :   gsi = gsi_start_nondebug_after_labels_bb (inner_cond_bb);
     779              :   /* If only the condition, this should be allowed. */
     780       226938 :   if (gsi_one_before_end_p (gsi))
     781              :     return true;
     782              :   /* Can have up to 2 statements defining each of lhs/rhs. */
     783       146650 :   for (int i = 0; i < 2; i++)
     784              :     {
     785       146650 :       gimple *stmt = gsi_stmt (gsi);
     786       146650 :       if (!is_gimple_assign (stmt)
     787       146650 :           || !CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt)))
     788              :         return false;
     789              :       /* The defining statement needs to match either the lhs or rhs of
     790              :          the condition. */
     791        11673 :       if (lhs != gimple_assign_lhs (stmt)
     792        11673 :           && rhs != gimple_assign_lhs (stmt))
     793              :         return false;
     794         4684 :       gsi_next_nondebug (&gsi);
     795         5326 :       if (gsi_one_before_end_p (gsi))
     796              :         return true;
     797              :     }
     798              :   return false;
     799              : }
     800              : 
     801              : /* Return true if BB guards entry to a loop: a successor edge reaches a loop
     802              :    header BB does not belong to, directly or through a single-successor
     803              :    preheader.  Combining the scalar conditions guarding a loop into a single
     804              :    boolean leaves the number-of-iterations analysis unable to prove the loop
     805              :    runs at least once, pessimizing later loop passes such as ivopts.  */
     806              : 
     807              : static bool
     808       473992 : bb_guards_loop_p (basic_block bb)
     809              : {
     810       473992 :   edge e;
     811       473992 :   edge_iterator ei;
     812      1382790 :   FOR_EACH_EDGE (e, ei, bb->succs)
     813              :     {
     814       937534 :       basic_block h = e->dest;
     815       937534 :       if (single_succ_p (h) && !bb_loop_header_p (h))
     816       376033 :         h = single_succ (h);
     817       937534 :       if (bb_loop_header_p (h) && !dominated_by_p (CDI_DOMINATORS, bb, h))
     818              :         return true;
     819              :     }
     820              :   return false;
     821              : }
     822              : 
     823              : /* If-convert on a and pattern with a common else block.  The inner
     824              :    if is specified by its INNER_COND_BB, the outer by OUTER_COND_BB.
     825              :    inner_inv, outer_inv indicate whether the conditions are inverted.
     826              :    Returns true if the edges to the common else basic-block were merged.  */
     827              : 
     828              : static bool
     829       535581 : ifcombine_ifandif (basic_block inner_cond_bb, bool inner_inv,
     830              :                    basic_block outer_cond_bb, bool outer_inv)
     831              : {
     832       535581 :   gimple_stmt_iterator gsi;
     833       535581 :   tree name1, name2, bit1, bit2, bits1, bits2;
     834              : 
     835      1071162 :   gcond *inner_cond = safe_dyn_cast <gcond *> (*gsi_last_bb (inner_cond_bb));
     836       535581 :   if (!inner_cond)
     837              :     return false;
     838              : 
     839      1071162 :   gcond *outer_cond = safe_dyn_cast <gcond *> (*gsi_last_bb (outer_cond_bb));
     840       535581 :   if (!outer_cond)
     841              :     return false;
     842              : 
     843              :   /* If the inner condition can trap, there is no combining unless
     844              :      the operands are the same.  */
     845       535581 :   if (gimple_could_trap_p (inner_cond))
     846              :     {
     847         6420 :       if (!operand_equal_p (gimple_cond_lhs (inner_cond),
     848         6420 :                             gimple_cond_lhs (outer_cond))
     849         9249 :           || !operand_equal_p (gimple_cond_rhs (inner_cond),
     850         2829 :                                gimple_cond_rhs (outer_cond)))
     851              :         return false;
     852              :      // We don't check if the outer will cause a trap as combine_comparisons
     853              :      // will take care if the combining happens or not. Specifically in the
     854              :      // case of losing a trap or cause a trap that was not there before.
     855          679 :      tree res = NULL_TREE;
     856          679 :      tree_code outer_cond_code = gimple_cond_code (outer_cond);
     857          679 :      tree_code inner_cond_code = gimple_cond_code (inner_cond);
     858          679 :      tree larg = gimple_cond_lhs (inner_cond);
     859          679 :      tree rarg = gimple_cond_rhs (inner_cond);
     860              :      // Handle `(a && b)`, no inverse
     861          679 :      if (!inner_inv && !outer_inv)
     862          256 :         res = combine_comparisons (UNKNOWN_LOCATION, TRUTH_ANDIF_EXPR,
     863              :                                    outer_cond_code, inner_cond_code,
     864              :                                    boolean_type_node, boolean_type_node,
     865              :                                    larg, rarg);
     866              :       // If both are inverse, `!a && !b`, then handle it as `!(a || b)`
     867              :       // As that !a or !b are most likely not producing a comparison code.
     868          423 :       else if (inner_inv && outer_inv)
     869              :         {
     870          132 :           res = combine_comparisons (UNKNOWN_LOCATION, TRUTH_ORIF_EXPR,
     871              :                                      outer_cond_code, inner_cond_code,
     872              :                                      boolean_type_node, boolean_type_node,
     873              :                                      larg, rarg);
     874          132 :           if (res)
     875          120 :             res = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (res), res);
     876              :         }
     877              :       else
     878              :         {
     879              :           // Handles the case where one is inverted and the other is not.
     880          291 :           tree_code inner_cond_code1 = inner_cond_code;
     881          291 :           tree_code outer_cond_code1 = outer_cond_code;
     882              :           // Try first `!a && b` and `a && !b`, those might be invertable.
     883          291 :           if (inner_inv)
     884          236 :             inner_cond_code1 = invert_tree_comparison (inner_cond_code1,
     885              :                                                        HONOR_NANS (larg));
     886           55 :           else if (outer_inv)
     887           55 :             outer_cond_code1 = invert_tree_comparison (outer_cond_code1,
     888              :                                                        HONOR_NANS (larg));
     889          291 :           if (inner_cond_code1 != ERROR_MARK && outer_cond_code1 != ERROR_MARK)
     890          224 :             res = combine_comparisons (UNKNOWN_LOCATION, TRUTH_ANDIF_EXPR,
     891              :                                        outer_cond_code1, inner_cond_code1,
     892              :                                        boolean_type_node, boolean_type_node,
     893              :                                        larg, rarg);
     894              :           // Otherwise, we need to try `!(!a || b)
     895           67 :           else if (inner_cond_code1 == ERROR_MARK)
     896              :             {
     897              :               // a && !b -> !(!a || b)
     898           32 :               outer_cond_code1 = invert_tree_comparison (outer_cond_code,
     899              :                                                          HONOR_NANS (larg));
     900           32 :               if (outer_cond_code1 != ERROR_MARK)
     901           12 :                 res = combine_comparisons (UNKNOWN_LOCATION, TRUTH_ORIF_EXPR,
     902              :                                            outer_cond_code1, inner_cond_code,
     903              :                                            boolean_type_node, boolean_type_node,
     904              :                                            larg, rarg);
     905           12 :               if (res)
     906           12 :                 res = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (res), res);
     907              :             }
     908              :           // Or `!(a || !b)`
     909              :           else
     910              :             {
     911              :               // !a && b -> !(a || !b)
     912           35 :               inner_cond_code1 = invert_tree_comparison (inner_cond_code,
     913              :                                                          HONOR_NANS (larg));
     914           35 :               if (inner_cond_code1 != ERROR_MARK)
     915            0 :                 res = combine_comparisons (UNKNOWN_LOCATION, TRUTH_ORIF_EXPR,
     916              :                                            outer_cond_code, inner_cond_code1,
     917              :                                            boolean_type_node, boolean_type_node,
     918              :                                            larg, rarg);
     919            0 :               if (res)
     920            0 :                 res = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (res), res);
     921              :             }
     922              :         }
     923          612 :       if (res)
     924              :         {
     925          493 :           if (!ifcombine_replace_cond (inner_cond, inner_inv,
     926              :                                        outer_cond, outer_inv,
     927              :                                        res, true, NULL_TREE))
     928              :             return false;
     929              : 
     930          378 :           if (dump_file)
     931              :             {
     932            5 :               fprintf (dump_file, "optimizing trapping cond to ");
     933            5 :               print_generic_expr (dump_file, res);
     934            5 :               fprintf (dump_file, "\n");
     935              :             }
     936              :           return true;
     937              :         }
     938              :       return false;
     939              :     }
     940              : 
     941              :   /* niter analysis does not cope with boolean typed loop exit conditions, nor
     942              :      with boolean loop guards.  Avoid turning an analyzable loop exit or guard
     943              :      into an unanalyzable one.  */
     944       529161 :   if ((inner_cond_bb->loop_father == outer_cond_bb->loop_father
     945       529161 :        && loop_exits_from_bb_p (inner_cond_bb->loop_father, inner_cond_bb)
     946        61916 :        && loop_exits_from_bb_p (outer_cond_bb->loop_father, outer_cond_bb))
     947      1003153 :       || bb_guards_loop_p (inner_cond_bb))
     948              :     {
     949        83905 :       tree outer_type = TREE_TYPE (gimple_cond_lhs (outer_cond));
     950        83905 :       tree inner_type = TREE_TYPE (gimple_cond_lhs (inner_cond));
     951        83905 :       if (TREE_CODE (outer_type) == INTEGER_TYPE
     952        21287 :           || POINTER_TYPE_P (outer_type)
     953        11436 :           || TREE_CODE (inner_type) == INTEGER_TYPE
     954         6656 :           || POINTER_TYPE_P (inner_type))
     955              :         return false;
     956              :     }
     957              : 
     958              :   /* See if we test a single bit of the same name in both tests.  In
     959              :      that case remove the outer test, merging both else edges,
     960              :      and change the inner one to test for
     961              :      name & (bit1 | bit2) == (bit1 | bit2).  */
     962       450985 :   if (recognize_single_bit_test (inner_cond, &name1, &bit1, inner_inv)
     963         4098 :       && recognize_single_bit_test (outer_cond, &name2, &bit2, outer_inv)
     964       453065 :       && name1 == name2)
     965              :     {
     966         1019 :       tree t, t2;
     967              : 
     968         1019 :       if (TREE_CODE (name1) == SSA_NAME
     969         1019 :           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name1))
     970              :         return false;
     971              : 
     972              :       /* Do it.  */
     973         1019 :       gsi = gsi_for_stmt (inner_cond);
     974         1019 :       location_t loc1 = gimple_location (inner_cond);
     975         1019 :       location_t loc2 = gimple_location (outer_cond);
     976         2038 :       t = gimple_build (&gsi, true, GSI_SAME_STMT, loc1, LSHIFT_EXPR,
     977         1019 :                         TREE_TYPE (name1),
     978         1019 :                         build_int_cst (TREE_TYPE (name1), 1), bit1);
     979         2038 :       t2 = gimple_build (&gsi, true, GSI_SAME_STMT, loc2, LSHIFT_EXPR,
     980         1019 :                          TREE_TYPE (name1),
     981         1019 :                          build_int_cst (TREE_TYPE (name1), 1), bit2);
     982         1019 :       t = gimple_build (&gsi, true, GSI_SAME_STMT, loc1, BIT_IOR_EXPR,
     983         1019 :                         TREE_TYPE (name1), t, t2);
     984         1019 :       t2 = gimple_build (&gsi, true, GSI_SAME_STMT, loc1, BIT_AND_EXPR,
     985         1019 :                          TREE_TYPE (name1), name1, t);
     986              : 
     987         1019 :       t = fold_build2 (EQ_EXPR, boolean_type_node, t2, t);
     988              : 
     989         1019 :       if (!ifcombine_replace_cond (inner_cond, inner_inv,
     990              :                                    outer_cond, outer_inv,
     991              :                                    t, true, NULL_TREE))
     992              :         return false;
     993              : 
     994         1019 :       if (dump_file)
     995              :         {
     996            1 :           fprintf (dump_file, "optimizing double bit test to ");
     997            1 :           print_generic_expr (dump_file, name1);
     998            1 :           fprintf (dump_file, " & T == T\nwith temporary T = (1 << ");
     999            1 :           print_generic_expr (dump_file, bit1);
    1000            1 :           fprintf (dump_file, ") | (1 << ");
    1001            1 :           print_generic_expr (dump_file, bit2);
    1002            1 :           fprintf (dump_file, ")\n");
    1003              :         }
    1004              : 
    1005              :       return true;
    1006              :     }
    1007              : 
    1008              :   /* See if we have two bit tests of the same name in both tests.
    1009              :      In that case remove the outer test and change the inner one to
    1010              :      test for name & (bits1 | bits2) != 0.  */
    1011       449966 :   else if (recognize_bits_test (inner_cond, &name1, &bits1, !inner_inv)
    1012       449966 :            && recognize_bits_test (outer_cond, &name2, &bits2, !outer_inv))
    1013              :     {
    1014         5305 :       tree t;
    1015              : 
    1016         5305 :       if ((TREE_CODE (name1) == SSA_NAME
    1017         5305 :            && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name1))
    1018         5305 :           || (TREE_CODE (name2) == SSA_NAME
    1019         5305 :               && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name2))
    1020         5305 :           || (TREE_CODE (bits1) == SSA_NAME
    1021         3279 :               && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (bits1))
    1022        10610 :           || (TREE_CODE (bits2) == SSA_NAME
    1023         3299 :               && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (bits2)))
    1024              :         return false;
    1025              : 
    1026              :       /* Find the common name which is bit-tested.  */
    1027         5305 :       if (name1 == name2)
    1028              :         ;
    1029         3755 :       else if (bits1 == bits2)
    1030              :         {
    1031           86 :           std::swap (name2, bits2);
    1032           86 :           std::swap (name1, bits1);
    1033              :         }
    1034         3669 :       else if (name1 == bits2)
    1035            6 :         std::swap (name2, bits2);
    1036         3663 :       else if (bits1 == name2)
    1037            0 :         std::swap (name1, bits1);
    1038              :       else
    1039              :         {
    1040              :           /* Look through non-widening conversions, but only on the names
    1041              :              so that a stripped operand never becomes part of the mask.  */
    1042         3663 :           tree t1 = get_name_for_bit_test (name1);
    1043         3663 :           tree t2 = get_name_for_bit_test (name2);
    1044         3663 :           if (t1 != t2)
    1045         3652 :             goto bits_test_failed;
    1046           11 :           name1 = name2 = t1;
    1047              :         }
    1048              : 
    1049         1653 :       if (TREE_CODE (name1) == SSA_NAME
    1050         1653 :           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name1))
    1051              :         return false;
    1052              : 
    1053              :       /* As we strip non-widening conversions in finding a common
    1054              :          name that is tested make sure to end up with an integral
    1055              :          type for building the bit operations.  */
    1056         1653 :       if (TYPE_PRECISION (TREE_TYPE (bits1))
    1057         1653 :           >= TYPE_PRECISION (TREE_TYPE (bits2)))
    1058              :         {
    1059         1653 :           bits1 = fold_convert (unsigned_type_for (TREE_TYPE (bits1)), bits1);
    1060         1653 :           name1 = fold_convert (TREE_TYPE (bits1), name1);
    1061         1653 :           bits2 = fold_convert (unsigned_type_for (TREE_TYPE (bits2)), bits2);
    1062         1653 :           bits2 = fold_convert (TREE_TYPE (bits1), bits2);
    1063              :         }
    1064              :       else
    1065              :         {
    1066            0 :           bits2 = fold_convert (unsigned_type_for (TREE_TYPE (bits2)), bits2);
    1067            0 :           name1 = fold_convert (TREE_TYPE (bits2), name1);
    1068            0 :           bits1 = fold_convert (unsigned_type_for (TREE_TYPE (bits1)), bits1);
    1069            0 :           bits1 = fold_convert (TREE_TYPE (bits2), bits1);
    1070              :         }
    1071              : 
    1072         1653 :       t = fold_build2 (BIT_IOR_EXPR, TREE_TYPE (name1), bits1, bits2);
    1073         1653 :       t = fold_build2 (BIT_AND_EXPR, TREE_TYPE (name1), name1, t);
    1074         1653 :       t = fold_build2 (EQ_EXPR, boolean_type_node, t,
    1075              :                        build_int_cst (TREE_TYPE (t), 0));
    1076         1653 :       if (!ifcombine_replace_cond (inner_cond, inner_inv,
    1077              :                                    outer_cond, outer_inv,
    1078              :                                    t, false, NULL_TREE))
    1079              :         return false;
    1080              : 
    1081         1653 :       if (dump_file)
    1082              :         {
    1083            3 :           fprintf (dump_file, "optimizing bits or bits test to ");
    1084            3 :           print_generic_expr (dump_file, name1);
    1085            3 :           fprintf (dump_file, " & T != 0\nwith temporary T = ");
    1086            3 :           print_generic_expr (dump_file, bits1);
    1087            3 :           fprintf (dump_file, " | ");
    1088            3 :           print_generic_expr (dump_file, bits2);
    1089            3 :           fprintf (dump_file, "\n");
    1090              :         }
    1091              : 
    1092              :       return true;
    1093              :     }
    1094              : 
    1095              :   /* See if we have two comparisons that we can merge into one.  */
    1096              :   else
    1097              :     {
    1098       448313 :     bits_test_failed:
    1099       448313 :       tree t, ts = NULL_TREE;
    1100       448313 :       enum tree_code inner_cond_code = gimple_cond_code (inner_cond);
    1101       448313 :       enum tree_code outer_cond_code = gimple_cond_code (outer_cond);
    1102              : 
    1103              :       /* Invert comparisons if necessary (and possible).  */
    1104       448313 :       if (inner_inv)
    1105       285970 :         inner_cond_code = invert_tree_comparison (inner_cond_code,
    1106       285970 :           HONOR_NANS (gimple_cond_lhs (inner_cond)));
    1107       448313 :       if (inner_cond_code == ERROR_MARK)
    1108              :         return false;
    1109       446633 :       if (outer_inv)
    1110       278736 :         outer_cond_code = invert_tree_comparison (outer_cond_code,
    1111       278736 :           HONOR_NANS (gimple_cond_lhs (outer_cond)));
    1112       446633 :       if (outer_cond_code == ERROR_MARK)
    1113              :         return false;
    1114              :       /* Don't return false so fast, try maybe_fold_or_comparisons?  */
    1115              : 
    1116       445930 :       if (!(t = maybe_fold_and_comparisons (boolean_type_node,
    1117              :                                             TRUTH_ANDIF_EXPR, outer_cond_code,
    1118              :                                             gimple_cond_lhs (outer_cond),
    1119              :                                             gimple_cond_rhs (outer_cond),
    1120              :                                             inner_cond_code,
    1121              :                                             gimple_cond_lhs (inner_cond),
    1122              :                                             gimple_cond_rhs (inner_cond),
    1123              :                                             gimple_bb (outer_cond)))
    1124       445930 :           && !(t = (fold_truth_andor_for_ifcombine
    1125       442844 :                     (TRUTH_ANDIF_EXPR, boolean_type_node,
    1126              :                      gimple_location (outer_cond),
    1127              :                      outer_cond_code,
    1128              :                      gimple_cond_lhs (outer_cond),
    1129              :                      gimple_cond_rhs (outer_cond),
    1130              :                      gimple_location (inner_cond),
    1131              :                      inner_cond_code,
    1132              :                      gimple_cond_lhs (inner_cond),
    1133              :                      gimple_cond_rhs (inner_cond),
    1134       442844 :                      single_pred (inner_cond_bb) != outer_cond_bb
    1135              :                      ? &ts : 0))))
    1136              :         {
    1137              :           /* Only combine conditions in this fallback case if the blocks are
    1138              :              neighbors.  */
    1139       439452 :           if (single_pred (inner_cond_bb) != outer_cond_bb)
    1140              :             return false;
    1141       227086 :           tree t1, t2;
    1142       227086 :           bool logical_op_non_short_circuit = LOGICAL_OP_NON_SHORT_CIRCUIT;
    1143       227086 :           if (param_logical_op_non_short_circuit != -1)
    1144          175 :             logical_op_non_short_circuit
    1145          175 :               = param_logical_op_non_short_circuit;
    1146       227086 :           if (!logical_op_non_short_circuit || sanitize_coverage_p ())
    1147              :             return false;
    1148              :           /* Only do this optimization if the inner bb contains only the conditional
    1149              :              or there is one or 2 statements which are nop conversion for the comparison. */
    1150       226938 :           if (!can_combine_bbs_with_short_circuit (inner_cond_bb,
    1151              :                                                    gimple_cond_lhs (inner_cond),
    1152              :                                                    gimple_cond_rhs (inner_cond)))
    1153              :             return false;
    1154        84972 :           t1 = fold_build2_loc (gimple_location (inner_cond),
    1155              :                                 inner_cond_code,
    1156              :                                 boolean_type_node,
    1157              :                                 gimple_cond_lhs (inner_cond),
    1158              :                                 gimple_cond_rhs (inner_cond));
    1159        84972 :           t2 = fold_build2_loc (gimple_location (outer_cond),
    1160              :                                 outer_cond_code,
    1161              :                                 boolean_type_node,
    1162              :                                 gimple_cond_lhs (outer_cond),
    1163              :                                 gimple_cond_rhs (outer_cond));
    1164        84972 :           t = fold_build2_loc (gimple_location (inner_cond),
    1165              :                                TRUTH_AND_EXPR, boolean_type_node, t1, t2);
    1166              :         }
    1167              : 
    1168        91450 :       if (!ifcombine_replace_cond (inner_cond, inner_inv,
    1169              :                                    outer_cond, outer_inv,
    1170              :                                    t, false, ts))
    1171              :         return false;
    1172              : 
    1173        91449 :       if (dump_file)
    1174              :         {
    1175           49 :           fprintf (dump_file, "optimizing two comparisons to ");
    1176           49 :           print_generic_expr (dump_file, t);
    1177           49 :           if (ts)
    1178              :             {
    1179            0 :               fprintf (dump_file, " and ");
    1180            0 :               print_generic_expr (dump_file, ts);
    1181              :             }
    1182           49 :           fprintf (dump_file, "\n");
    1183              :         }
    1184              : 
    1185              :       return true;
    1186              :     }
    1187              : 
    1188              :   return false;
    1189              : }
    1190              : 
    1191              : /* Helper function for tree_ssa_ifcombine_bb.  Recognize a CFG pattern and
    1192              :    dispatch to the appropriate if-conversion helper for a particular
    1193              :    set of INNER_COND_BB, OUTER_COND_BB, THEN_BB and ELSE_BB.
    1194              :    PHI_PRED_BB should be one of INNER_COND_BB, THEN_BB or ELSE_BB.
    1195              :    OUTER_SUCC_BB is the successor of OUTER_COND_BB on the path towards
    1196              :    INNER_COND_BB.  */
    1197              : 
    1198              : static bool
    1199      1621956 : tree_ssa_ifcombine_bb_1 (basic_block inner_cond_bb, basic_block outer_cond_bb,
    1200              :                          basic_block then_bb, basic_block else_bb,
    1201              :                          basic_block phi_pred_bb, basic_block outer_succ_bb)
    1202              : {
    1203              :   /* The && form is characterized by a common else_bb with
    1204              :      the two edges leading to it mergeable.  The latter is
    1205              :      guaranteed by matching PHI arguments in the else_bb and
    1206              :      the inner cond_bb having no side-effects.  */
    1207      1621956 :   if (phi_pred_bb != else_bb
    1208      1598450 :       && recognize_if_then_else (outer_cond_bb, &outer_succ_bb, &else_bb)
    1209      1827853 :       && same_phi_args_p (outer_cond_bb, phi_pred_bb, else_bb))
    1210              :     {
    1211              :       /* We have
    1212              :            <outer_cond_bb>
    1213              :              if (q) goto inner_cond_bb; else goto else_bb;
    1214              :            <inner_cond_bb>
    1215              :              if (p) goto ...; else goto else_bb;
    1216              :              ...
    1217              :            <else_bb>
    1218              :              ...
    1219              :        */
    1220       195939 :       return ifcombine_ifandif (inner_cond_bb, false, outer_cond_bb, false);
    1221              :     }
    1222              : 
    1223              :   /* And a version where the outer condition is negated.  */
    1224      1426017 :   if (phi_pred_bb != else_bb
    1225      1402511 :       && recognize_if_then_else (outer_cond_bb, &else_bb, &outer_succ_bb)
    1226      1445384 :       && same_phi_args_p (outer_cond_bb, phi_pred_bb, else_bb))
    1227              :     {
    1228              :       /* We have
    1229              :            <outer_cond_bb>
    1230              :              if (q) goto else_bb; else goto inner_cond_bb;
    1231              :            <inner_cond_bb>
    1232              :              if (p) goto ...; else goto else_bb;
    1233              :              ...
    1234              :            <else_bb>
    1235              :              ...
    1236              :        */
    1237        10983 :       return ifcombine_ifandif (inner_cond_bb, false, outer_cond_bb, true);
    1238              :     }
    1239              : 
    1240              :   /* The || form is characterized by a common then_bb with the
    1241              :      two edges leading to it mergeable.  The latter is guaranteed
    1242              :      by matching PHI arguments in the then_bb and the inner cond_bb
    1243              :      having no side-effects.  */
    1244      1415034 :   if (phi_pred_bb != then_bb
    1245      1400894 :       && recognize_if_then_else (outer_cond_bb, &then_bb, &outer_succ_bb)
    1246      1747815 :       && same_phi_args_p (outer_cond_bb, phi_pred_bb, then_bb))
    1247              :     {
    1248              :       /* We have
    1249              :            <outer_cond_bb>
    1250              :              if (q) goto then_bb; else goto inner_cond_bb;
    1251              :            <inner_cond_bb>
    1252              :              if (p) goto then_bb; else goto ...;
    1253              :            <then_bb>
    1254              :              ...
    1255              :        */
    1256       308857 :       return ifcombine_ifandif (inner_cond_bb, true, outer_cond_bb, true);
    1257              :     }
    1258              : 
    1259              :   /* And a version where the outer condition is negated.  */
    1260      1106177 :   if (phi_pred_bb != then_bb
    1261      1092037 :       && recognize_if_then_else (outer_cond_bb, &outer_succ_bb, &then_bb)
    1262      1133562 :       && same_phi_args_p (outer_cond_bb, phi_pred_bb, then_bb))
    1263              :     {
    1264              :       /* We have
    1265              :            <outer_cond_bb>
    1266              :              if (q) goto inner_cond_bb; else goto then_bb;
    1267              :            <inner_cond_bb>
    1268              :              if (p) goto then_bb; else goto ...;
    1269              :            <then_bb>
    1270              :              ...
    1271              :        */
    1272        19802 :       return ifcombine_ifandif (inner_cond_bb, true, outer_cond_bb, false);
    1273              :     }
    1274              : 
    1275              :   return false;
    1276              : }
    1277              : 
    1278              : /* Recognize a CFG pattern and dispatch to the appropriate
    1279              :    if-conversion helper.  We start with BB as the innermost
    1280              :    worker basic-block.  Returns true if a transformation was done.  */
    1281              : 
    1282              : bool
    1283      5534114 : tree_ssa_ifcombine_bb (basic_block inner_cond_bb)
    1284              : {
    1285      5534114 :   bool ret = false;
    1286      5534114 :   basic_block then_bb = NULL, else_bb = NULL;
    1287              : 
    1288      5534114 :   if (!recognize_if_then_else (inner_cond_bb, &then_bb, &else_bb))
    1289              :     return ret;
    1290              : 
    1291              :   /* Recognize && and || of two conditions with a common
    1292              :      then/else block which entry edges we can merge.  That is:
    1293              :        if (a || b)
    1294              :          ;
    1295              :      and
    1296              :        if (a && b)
    1297              :          ;
    1298              :      This requires a single predecessor of the inner cond_bb.
    1299              : 
    1300              :      Look for an OUTER_COND_BBs to combine with INNER_COND_BB.  They need not
    1301              :      be contiguous, as long as inner and intervening blocks have no side
    1302              :      effects, and are either single-entry-single-exit or conditionals choosing
    1303              :      between the same EXIT_BB with the same PHI args, possibly through an
    1304              :      EXIT_PRED, and the path leading to INNER_COND_BB.  EXIT_PRED will be set
    1305              :      just before (along with a successful combination) or just after setting
    1306              :      EXIT_BB, to either THEN_BB, ELSE_BB, or INNER_COND_BB.  ??? We could
    1307              :      potentially handle multi-block single-entry-single-exit regions, but the
    1308              :      loop below only deals with single-entry-single-exit individual intervening
    1309              :      blocks.  Larger regions without side effects are presumably rare, so it's
    1310              :      probably not worth the effort.  */
    1311      6315085 :   for (basic_block bb = inner_cond_bb, outer_cond_bb, exit_bb = NULL,
    1312              :          /* This initialization shouldn't be needed, but in case the compiler
    1313              :             is not smart enough to tell, make it harmless.  */
    1314      5533863 :          exit_pred = NULL;
    1315      6315085 :        single_pred_p (bb) && bb_no_side_effects_p (bb);
    1316       781222 :        bb = outer_cond_bb)
    1317              :     {
    1318      2091799 :       bool changed = false;
    1319              : 
    1320      2091799 :       outer_cond_bb = single_pred (bb);
    1321              : 
    1322              :       /* Skip blocks without conditions.  */
    1323      2091799 :       if (single_succ_p (outer_cond_bb))
    1324       189882 :         continue;
    1325              : 
    1326              :       /* When considering noncontiguous conditions, make sure that all
    1327              :          non-final conditions lead to the same successor of the final
    1328              :          condition, when not taking the path to inner_bb, so that we can
    1329              :          combine C into A, both in A && (B && C), and in A || (B || C), but
    1330              :          neither in A && (B || C), nor A || (B && C).  Say, if C goes to
    1331              :          THEN_BB or ELSE_BB, then B must go to either of these, say X, besides
    1332              :          C (whether C is then or else), and A must go to X and B (whether then
    1333              :          or else).
    1334              : 
    1335              :          We test for this, while allowing intervening nonconditional blocks, by
    1336              :          first taking note of which of the successors of the inner conditional
    1337              :          block is the exit path taken by the first considered outer conditional
    1338              :          block.
    1339              : 
    1340              :          Having identified and saved the exit block in EXIT_BB at the end of
    1341              :          the loop, here we test that subsequent conditional blocks under
    1342              :          consideration also use the exit block as a successor, besides the
    1343              :          block that leads to inner_cond_bb, and that the edges to exit share
    1344              :          the same phi values.  */
    1345      1901917 :       if (exit_bb
    1346      1901917 :           && !recognize_if_then_else (outer_cond_bb, &bb, &exit_bb, true))
    1347              :         break;
    1348              : 
    1349              :       /* After checking dests and phi args, we can also skip blocks whose
    1350              :          conditions have been optimized down to a constant, without trying to
    1351              :          combine them, but we must not skip the computation of EXIT_BB and the
    1352              :          checking of same phi args.  */
    1353      1835200 :       if (known_succ_p (outer_cond_bb))
    1354              :         changed = false;
    1355       233286 :       else if ((!exit_bb || exit_pred == inner_cond_bb)
    1356      1817284 :                && tree_ssa_ifcombine_bb_1 (inner_cond_bb, outer_cond_bb,
    1357              :                                            then_bb, else_bb, inner_cond_bb, bb))
    1358              :         changed = true, exit_pred = inner_cond_bb;
    1359      1489939 :       else if (exit_bb
    1360      1489939 :                ? exit_pred == else_bb
    1361      1256787 :                : forwarder_block_to (else_bb, then_bb))
    1362              :         {
    1363              :           /* Other possibilities for the && form, if else_bb is
    1364              :              empty forwarder block to then_bb.  Compared to the above simpler
    1365              :              forms this can be treated as if then_bb and else_bb were swapped,
    1366              :              and the corresponding inner_cond_bb not inverted because of that.
    1367              :              For same_phi_args_p we look at equality of arguments between
    1368              :              edge from outer_cond_bb and the forwarder block.  */
    1369        14452 :           if (tree_ssa_ifcombine_bb_1 (inner_cond_bb, outer_cond_bb, else_bb,
    1370              :                                        then_bb, else_bb, bb))
    1371          189 :             changed = true, exit_pred = else_bb;
    1372              :         }
    1373      1475487 :       else if (exit_bb
    1374      1475487 :                ? exit_pred == then_bb
    1375      1242358 :                : forwarder_block_to (then_bb, else_bb))
    1376              :         {
    1377              :           /* Other possibilities for the || form, if then_bb is
    1378              :              empty forwarder block to else_bb.  Compared to the above simpler
    1379              :              forms this can be treated as if then_bb and else_bb were swapped,
    1380              :              and the corresponding inner_cond_bb not inverted because of that.
    1381              :              For same_phi_args_p we look at equality of arguments between
    1382              :              edge from outer_cond_bb and the forwarder block.  */
    1383        23506 :           if (tree_ssa_ifcombine_bb_1 (inner_cond_bb, outer_cond_bb, else_bb,
    1384              :                                        then_bb, then_bb, bb))
    1385          188 :             changed = true, exit_pred = then_bb;
    1386              :         }
    1387              : 
    1388          377 :       if (changed)
    1389        94499 :         ret = changed;
    1390              : 
    1391              :       /* If the inner condition is gone, there's no point in attempting to
    1392              :          combine it any further.  */
    1393        94499 :       if (changed && known_succ_p (inner_cond_bb))
    1394              :         break;
    1395              : 
    1396              :       /* Starting at this point in the loop, we start preparing to attempt
    1397              :          combinations in which OUTER_COND_BB will be an intervening block.
    1398              :          Checking that it has a single predecessor is a very cheap test, unlike
    1399              :          the PHI args tests below, so test it early and hopefully save the more
    1400              :          expensive tests in case we won't be able to try other blocks.  */
    1401      1834970 :       if (!single_pred_p (outer_cond_bb))
    1402              :         break;
    1403              : 
    1404              :       /* Record the exit path taken by the outer condition.  */
    1405      1365419 :       if (!exit_bb)
    1406              :         {
    1407              :           /* If we have removed the outer condition entirely, we need not
    1408              :              commit to an exit block yet, it's as if we'd merged the blocks and
    1409              :              were starting afresh.  This is sound as long as we never replace
    1410              :              the outer condition with a constant that leads away from the inner
    1411              :              block.  Here's why we never do: when combining contiguous
    1412              :              conditions, we replace the inner cond, and replace the outer cond
    1413              :              with a constant that leads to inner, so this case is good.  When
    1414              :              combining noncontiguous blocks, we normally modify outer, and
    1415              :              replace inner with a constant or remainders of the original
    1416              :              condition that couldn't be combined.  This test would normally not
    1417              :              hit with noncontiguous blocks, because we'd have computed EXIT_BB
    1418              :              before reaching the noncontiguous outer block.  However, if all
    1419              :              intervening blocks are unconditional, including those just made
    1420              :              unconditional, we may replace outer instead of inner with the
    1421              :              combined condition.  If the combined noncontiguous conditions are
    1422              :              mutually exclusive, we could end up with a constant outer
    1423              :              condition, but then, the inner condition would also be a constant,
    1424              :              and then we'd stop iterating because of the known_succ_p
    1425              :              (inner_cond_bb) test above.  */
    1426      1036276 :           if (changed && known_succ_p (outer_cond_bb))
    1427        78295 :             continue;
    1428              : 
    1429       957981 :           if (recognize_if_then_else (outer_cond_bb, &then_bb, &bb, true))
    1430       128160 :             exit_bb = then_bb;
    1431       829821 :           else if (recognize_if_then_else (outer_cond_bb, &bb, &else_bb, true))
    1432        90346 :             exit_bb = else_bb;
    1433              :           else
    1434              :             break;
    1435              : 
    1436              :           /* Find out which path from INNER_COND_BB shares PHI args with the
    1437              :              edge (OUTER_COND_BB->EXIT_BB).  That path may involve a forwarder
    1438              :              block, whether THEN_BB or ELSE_BB, and we need to know which one
    1439              :              satisfies the condition to avoid combinations that could use
    1440              :              different forwarding arrangements, because they would be unsound.
    1441              :              E.g., given (a ? 0 : b ? 1 : c ? 1 : 0), after trying to merge b
    1442              :              and c, we test that both share the same exit block, with the same
    1443              :              value 1.  Whether or not that involves a forwarder block, if we
    1444              :              don't go through the same (possibly absent) forwarder block in
    1445              :              subsequent attempted combinations, e.g. a with c, we could find
    1446              :              that a and inverted c share the same exit block with a different
    1447              :              value, namely 0, which would enable an unsound merge.  We need all
    1448              :              of inner, intervening and outer blocks to reach the same exit with
    1449              :              the same value for the transformation to be sound.  So here we
    1450              :              determine how to get to EXIT_BB from outer and inner with the same
    1451              :              PHI values, record that in EXIT_PRED, and then subsequent
    1452              :              combination attempts that have OUTER_COND_BB as an intervening
    1453              :              block will ensure the same path to exit is taken, skipping unsound
    1454              :              transformations.  */
    1455       218506 :           if (changed)
    1456              :             /* EXIT_PRED was set along with CHANGED, and the successful
    1457              :                combination already checked for the same PHI args.  */;
    1458       218394 :           else if (same_phi_args_p (outer_cond_bb, inner_cond_bb, exit_bb))
    1459              :             exit_pred = inner_cond_bb;
    1460        29251 :           else if (then_bb == exit_bb
    1461        20319 :                    && forwarder_block_to (else_bb, then_bb)
    1462        31948 :                    && same_phi_args_p (outer_cond_bb, else_bb, exit_bb))
    1463           79 :             exit_pred = else_bb;
    1464        29172 :           else if (else_bb == exit_bb
    1465         8932 :                    && forwarder_block_to (then_bb, else_bb)
    1466        30433 :                    && same_phi_args_p (outer_cond_bb, then_bb, exit_bb))
    1467          176 :             exit_pred = then_bb;
    1468              :           else
    1469              :             /* If none of the paths share the same PHI args, no combination is
    1470              :                viable.  */
    1471              :             break;
    1472              :           /* Skip the PHI args test below, it's redundant with the tests we've
    1473              :              just performed.  */
    1474       189510 :           continue;
    1475              :         }
    1476              : 
    1477              :       /* Before trying an earlier block, make sure INNER_COND_BB and the
    1478              :          current OUTER_COND_BB share the same PHI args at EXIT_BB.  We don't
    1479              :          need to check if the latest attempt at combining succeeded, because
    1480              :          that means we'll have already checked.  But we can't only check outer
    1481              :          and inner, we have to check that all intervening blocks also get to
    1482              :          exit with the same result, otherwise the transformation may change the
    1483              :          final result.  Consider (a ? 0 : b ? 1 : c ? 0 : -1).  If we combine
    1484              :          (a | c), yielding ((a | c) ? 0 : b ? 1 : [0 ? 0 :] -1), we'd get 0
    1485              :          rather than 1 when (!a&&b).  And if we were to replace inner instead
    1486              :          of outer, we'd get ([1 ? 0 :] b ? 1 : (a | c) ? 0 : -1), which would
    1487              :          yield 1 rather than 0 when (a).  */
    1488       329143 :       if (!changed
    1489       329143 :           && !same_phi_args_p (outer_cond_bb, exit_pred, exit_bb))
    1490              :         break;
    1491              :     }
    1492              : 
    1493      5533863 :   return ret;
    1494              : }
    1495              : 
    1496              : /* Main entry for the tree if-conversion pass.  */
    1497              : 
    1498              : namespace {
    1499              : 
    1500              : const pass_data pass_data_tree_ifcombine =
    1501              : {
    1502              :   GIMPLE_PASS, /* type */
    1503              :   "ifcombine", /* name */
    1504              :   OPTGROUP_NONE, /* optinfo_flags */
    1505              :   TV_TREE_IFCOMBINE, /* tv_id */
    1506              :   ( PROP_cfg | PROP_ssa ), /* properties_required */
    1507              :   0, /* properties_provided */
    1508              :   0, /* properties_destroyed */
    1509              :   0, /* todo_flags_start */
    1510              :   TODO_update_ssa, /* todo_flags_finish */
    1511              : };
    1512              : 
    1513              : class pass_tree_ifcombine : public gimple_opt_pass
    1514              : {
    1515              : public:
    1516       302985 :   pass_tree_ifcombine (gcc::context *ctxt)
    1517       605970 :     : gimple_opt_pass (pass_data_tree_ifcombine, ctxt)
    1518              :   {}
    1519              : 
    1520              :   /* opt_pass methods: */
    1521              :   unsigned int execute (function *) final override;
    1522              : 
    1523              : }; // class pass_tree_ifcombine
    1524              : 
    1525              : unsigned int
    1526      1068169 : pass_tree_ifcombine::execute (function *fun)
    1527              : {
    1528      1068169 :   basic_block *bbs;
    1529      1068169 :   bool cfg_changed = false;
    1530      1068169 :   int i;
    1531              : 
    1532      1068169 :   bbs = single_pred_before_succ_order ();
    1533      1068169 :   calculate_dominance_info (CDI_DOMINATORS);
    1534      1068169 :   mark_ssa_maybe_undefs ();
    1535              : 
    1536              :   /* Search every basic block for COND_EXPR we may be able to optimize.
    1537              : 
    1538              :      We walk the blocks in order that guarantees that a block with
    1539              :      a single predecessor is processed after the predecessor.
    1540              :      This ensures that we collapse outer ifs before visiting the
    1541              :      inner ones, and also that we do not try to visit a removed
    1542              :      block.  This is opposite of PHI-OPT, because we cascade the
    1543              :      combining rather than cascading PHIs. */
    1544     11717333 :   for (i = n_basic_blocks_for_fn (fun) - NUM_FIXED_BLOCKS - 1; i >= 0; i--)
    1545              :     {
    1546     10649164 :       basic_block bb = bbs[i];
    1547              : 
    1548     25665028 :       if (safe_is_a <gcond *> (*gsi_last_bb (bb)))
    1549      4446479 :         if (tree_ssa_ifcombine_bb (bb))
    1550     10649164 :           cfg_changed |= true;
    1551              :     }
    1552              : 
    1553      1068169 :   free (bbs);
    1554              : 
    1555      1068169 :   return cfg_changed ? TODO_cleanup_cfg : 0;
    1556              : }
    1557              : 
    1558              : } // anon namespace
    1559              : 
    1560              : gimple_opt_pass *
    1561       302985 : make_pass_tree_ifcombine (gcc::context *ctxt)
    1562              : {
    1563       302985 :   return new pass_tree_ifcombine (ctxt);
    1564              : }
        

Generated by: LCOV version 2.6-beta

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,rust,m2 --enable-host-shared. GCC test suite is run with the built compiler.