LCOV - code coverage report
Current view: top level - gcc - tree-ssa-reassoc.cc (source / functions) Coverage Total Hit
Test: gcc.info Lines: 91.9 % 3608 3316
Test Date: 2024-04-13 14:00:49 Functions: 96.0 % 100 96
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: - 0 0

             Branch data     Line data    Source code
       1                 :             : /* Reassociation for trees.
       2                 :             :    Copyright (C) 2005-2024 Free Software Foundation, Inc.
       3                 :             :    Contributed by Daniel Berlin <dan@dberlin.org>
       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 "target.h"
      26                 :             : #include "rtl.h"
      27                 :             : #include "tree.h"
      28                 :             : #include "gimple.h"
      29                 :             : #include "cfghooks.h"
      30                 :             : #include "alloc-pool.h"
      31                 :             : #include "tree-pass.h"
      32                 :             : #include "memmodel.h"
      33                 :             : #include "tm_p.h"
      34                 :             : #include "ssa.h"
      35                 :             : #include "optabs-tree.h"
      36                 :             : #include "gimple-pretty-print.h"
      37                 :             : #include "diagnostic-core.h"
      38                 :             : #include "fold-const.h"
      39                 :             : #include "stor-layout.h"
      40                 :             : #include "cfganal.h"
      41                 :             : #include "gimple-iterator.h"
      42                 :             : #include "gimple-fold.h"
      43                 :             : #include "tree-eh.h"
      44                 :             : #include "gimplify-me.h"
      45                 :             : #include "tree-cfg.h"
      46                 :             : #include "tree-ssa-loop.h"
      47                 :             : #include "flags.h"
      48                 :             : #include "tree-ssa.h"
      49                 :             : #include "langhooks.h"
      50                 :             : #include "cfgloop.h"
      51                 :             : #include "builtins.h"
      52                 :             : #include "gimplify.h"
      53                 :             : #include "case-cfn-macros.h"
      54                 :             : #include "tree-ssa-reassoc.h"
      55                 :             : #include "tree-ssa-math-opts.h"
      56                 :             : #include "gimple-range.h"
      57                 :             : #include "internal-fn.h"
      58                 :             : 
      59                 :             : /*  This is a simple global reassociation pass.  It is, in part, based
      60                 :             :     on the LLVM pass of the same name (They do some things more/less
      61                 :             :     than we do, in different orders, etc).
      62                 :             : 
      63                 :             :     It consists of five steps:
      64                 :             : 
      65                 :             :     1. Breaking up subtract operations into addition + negate, where
      66                 :             :     it would promote the reassociation of adds.
      67                 :             : 
      68                 :             :     2. Left linearization of the expression trees, so that (A+B)+(C+D)
      69                 :             :     becomes (((A+B)+C)+D), which is easier for us to rewrite later.
      70                 :             :     During linearization, we place the operands of the binary
      71                 :             :     expressions into a vector of operand_entry_*
      72                 :             : 
      73                 :             :     3. Optimization of the operand lists, eliminating things like a +
      74                 :             :     -a, a & a, etc.
      75                 :             : 
      76                 :             :     3a. Combine repeated factors with the same occurrence counts
      77                 :             :     into a __builtin_powi call that will later be optimized into
      78                 :             :     an optimal number of multiplies.
      79                 :             : 
      80                 :             :     4. Rewrite the expression trees we linearized and optimized so
      81                 :             :     they are in proper rank order.
      82                 :             : 
      83                 :             :     5. Repropagate negates, as nothing else will clean it up ATM.
      84                 :             : 
      85                 :             :     A bit of theory on #4, since nobody seems to write anything down
      86                 :             :     about why it makes sense to do it the way they do it:
      87                 :             : 
      88                 :             :     We could do this much nicer theoretically, but don't (for reasons
      89                 :             :     explained after how to do it theoretically nice :P).
      90                 :             : 
      91                 :             :     In order to promote the most redundancy elimination, you want
      92                 :             :     binary expressions whose operands are the same rank (or
      93                 :             :     preferably, the same value) exposed to the redundancy eliminator,
      94                 :             :     for possible elimination.
      95                 :             : 
      96                 :             :     So the way to do this if we really cared, is to build the new op
      97                 :             :     tree from the leaves to the roots, merging as you go, and putting the
      98                 :             :     new op on the end of the worklist, until you are left with one
      99                 :             :     thing on the worklist.
     100                 :             : 
     101                 :             :     IE if you have to rewrite the following set of operands (listed with
     102                 :             :     rank in parentheses), with opcode PLUS_EXPR:
     103                 :             : 
     104                 :             :     a (1),  b (1),  c (1),  d (2), e (2)
     105                 :             : 
     106                 :             : 
     107                 :             :     We start with our merge worklist empty, and the ops list with all of
     108                 :             :     those on it.
     109                 :             : 
     110                 :             :     You want to first merge all leaves of the same rank, as much as
     111                 :             :     possible.
     112                 :             : 
     113                 :             :     So first build a binary op of
     114                 :             : 
     115                 :             :     mergetmp = a + b, and put "mergetmp" on the merge worklist.
     116                 :             : 
     117                 :             :     Because there is no three operand form of PLUS_EXPR, c is not going to
     118                 :             :     be exposed to redundancy elimination as a rank 1 operand.
     119                 :             : 
     120                 :             :     So you might as well throw it on the merge worklist (you could also
     121                 :             :     consider it to now be a rank two operand, and merge it with d and e,
     122                 :             :     but in this case, you then have evicted e from a binary op. So at
     123                 :             :     least in this situation, you can't win.)
     124                 :             : 
     125                 :             :     Then build a binary op of d + e
     126                 :             :     mergetmp2 = d + e
     127                 :             : 
     128                 :             :     and put mergetmp2 on the merge worklist.
     129                 :             : 
     130                 :             :     so merge worklist = {mergetmp, c, mergetmp2}
     131                 :             : 
     132                 :             :     Continue building binary ops of these operations until you have only
     133                 :             :     one operation left on the worklist.
     134                 :             : 
     135                 :             :     So we have
     136                 :             : 
     137                 :             :     build binary op
     138                 :             :     mergetmp3 = mergetmp + c
     139                 :             : 
     140                 :             :     worklist = {mergetmp2, mergetmp3}
     141                 :             : 
     142                 :             :     mergetmp4 = mergetmp2 + mergetmp3
     143                 :             : 
     144                 :             :     worklist = {mergetmp4}
     145                 :             : 
     146                 :             :     because we have one operation left, we can now just set the original
     147                 :             :     statement equal to the result of that operation.
     148                 :             : 
     149                 :             :     This will at least expose a + b  and d + e to redundancy elimination
     150                 :             :     as binary operations.
     151                 :             : 
     152                 :             :     For extra points, you can reuse the old statements to build the
     153                 :             :     mergetmps, since you shouldn't run out.
     154                 :             : 
     155                 :             :     So why don't we do this?
     156                 :             : 
     157                 :             :     Because it's expensive, and rarely will help.  Most trees we are
     158                 :             :     reassociating have 3 or less ops.  If they have 2 ops, they already
     159                 :             :     will be written into a nice single binary op.  If you have 3 ops, a
     160                 :             :     single simple check suffices to tell you whether the first two are of the
     161                 :             :     same rank.  If so, you know to order it
     162                 :             : 
     163                 :             :     mergetmp = op1 + op2
     164                 :             :     newstmt = mergetmp + op3
     165                 :             : 
     166                 :             :     instead of
     167                 :             :     mergetmp = op2 + op3
     168                 :             :     newstmt = mergetmp + op1
     169                 :             : 
     170                 :             :     If all three are of the same rank, you can't expose them all in a
     171                 :             :     single binary operator anyway, so the above is *still* the best you
     172                 :             :     can do.
     173                 :             : 
     174                 :             :     Thus, this is what we do.  When we have three ops left, we check to see
     175                 :             :     what order to put them in, and call it a day.  As a nod to vector sum
     176                 :             :     reduction, we check if any of the ops are really a phi node that is a
     177                 :             :     destructive update for the associating op, and keep the destructive
     178                 :             :     update together for vector sum reduction recognition.  */
     179                 :             : 
     180                 :             : /* Enable insertion of __builtin_powi calls during execute_reassoc.  See
     181                 :             :    point 3a in the pass header comment.  */
     182                 :             : static bool reassoc_insert_powi_p;
     183                 :             : 
     184                 :             : /* Enable biasing ranks of loop accumulators.  We don't want this before
     185                 :             :    vectorization, since it interferes with reduction chains.  */
     186                 :             : static bool reassoc_bias_loop_carried_phi_ranks_p;
     187                 :             : 
     188                 :             : /* Statistics */
     189                 :             : static struct
     190                 :             : {
     191                 :             :   int linearized;
     192                 :             :   int constants_eliminated;
     193                 :             :   int ops_eliminated;
     194                 :             :   int rewritten;
     195                 :             :   int pows_encountered;
     196                 :             :   int pows_created;
     197                 :             : } reassociate_stats;
     198                 :             : 
     199                 :             : 
     200                 :             : static object_allocator<operand_entry> operand_entry_pool
     201                 :             :   ("operand entry pool");
     202                 :             : 
     203                 :             : /* This is used to assign a unique ID to each struct operand_entry
     204                 :             :    so that qsort results are identical on different hosts.  */
     205                 :             : static unsigned int next_operand_entry_id;
     206                 :             : 
     207                 :             : /* Starting rank number for a given basic block, so that we can rank
     208                 :             :    operations using unmovable instructions in that BB based on the bb
     209                 :             :    depth.  */
     210                 :             : static int64_t *bb_rank;
     211                 :             : 
     212                 :             : /* Operand->rank hashtable.  */
     213                 :             : static hash_map<tree, int64_t> *operand_rank;
     214                 :             : 
     215                 :             : /* SSA_NAMEs that are forms of loop accumulators and whose ranks need to be
     216                 :             :    biased.  */
     217                 :             : static auto_bitmap biased_names;
     218                 :             : 
     219                 :             : /* Vector of SSA_NAMEs on which after reassociate_bb is done with
     220                 :             :    all basic blocks the CFG should be adjusted - basic blocks
     221                 :             :    split right after that SSA_NAME's definition statement and before
     222                 :             :    the only use, which must be a bit ior.  */
     223                 :             : static vec<tree> reassoc_branch_fixups;
     224                 :             : 
     225                 :             : /* Forward decls.  */
     226                 :             : static int64_t get_rank (tree);
     227                 :             : static bool reassoc_stmt_dominates_stmt_p (gimple *, gimple *);
     228                 :             : 
     229                 :             : /* Wrapper around gsi_remove, which adjusts gimple_uid of debug stmts
     230                 :             :    possibly added by gsi_remove.  */
     231                 :             : 
     232                 :             : static bool
     233                 :      132462 : reassoc_remove_stmt (gimple_stmt_iterator *gsi)
     234                 :             : {
     235                 :      132462 :   gimple *stmt = gsi_stmt (*gsi);
     236                 :             : 
     237                 :      132462 :   if (!MAY_HAVE_DEBUG_BIND_STMTS || gimple_code (stmt) == GIMPLE_PHI)
     238                 :       60686 :     return gsi_remove (gsi, true);
     239                 :             : 
     240                 :       71776 :   gimple_stmt_iterator prev = *gsi;
     241                 :       71776 :   gsi_prev (&prev);
     242                 :       71776 :   unsigned uid = gimple_uid (stmt);
     243                 :       71776 :   basic_block bb = gimple_bb (stmt);
     244                 :       71776 :   bool ret = gsi_remove (gsi, true);
     245                 :       71776 :   if (!gsi_end_p (prev))
     246                 :       71567 :     gsi_next (&prev);
     247                 :             :   else
     248                 :         418 :     prev = gsi_start_bb (bb);
     249                 :       71776 :   gimple *end_stmt = gsi_stmt (*gsi);
     250                 :       75965 :   while ((stmt = gsi_stmt (prev)) != end_stmt)
     251                 :             :     {
     252                 :        4189 :       gcc_assert (stmt && is_gimple_debug (stmt) && gimple_uid (stmt) == 0);
     253                 :        4189 :       gimple_set_uid (stmt, uid);
     254                 :        4189 :       gsi_next (&prev);
     255                 :             :     }
     256                 :             :   return ret;
     257                 :             : }
     258                 :             : 
     259                 :             : /* Bias amount for loop-carried phis.  We want this to be larger than
     260                 :             :    the depth of any reassociation tree we can see, but not larger than
     261                 :             :    the rank difference between two blocks.  */
     262                 :             : #define PHI_LOOP_BIAS (1 << 15)
     263                 :             : 
     264                 :             : /* Return TRUE iff PHI_LOOP_BIAS should be propagated from one of the STMT's
     265                 :             :    operands to the STMT's left-hand side.  The goal is to preserve bias in code
     266                 :             :    like this:
     267                 :             : 
     268                 :             :      x_1 = phi(x_0, x_2)
     269                 :             :      a = x_1 | 1
     270                 :             :      b = a ^ 2
     271                 :             :      .MEM = b
     272                 :             :      c = b + d
     273                 :             :      x_2 = c + e
     274                 :             : 
     275                 :             :    That is, we need to preserve bias along single-use chains originating from
     276                 :             :    loop-carried phis.  Only GIMPLE_ASSIGNs to SSA_NAMEs are considered to be
     277                 :             :    uses, because only they participate in rank propagation.  */
     278                 :             : static bool
     279                 :     5709013 : propagate_bias_p (gimple *stmt)
     280                 :             : {
     281                 :     5709013 :   use_operand_p use;
     282                 :     5709013 :   imm_use_iterator use_iter;
     283                 :     5709013 :   gimple *single_use_stmt = NULL;
     284                 :             : 
     285                 :     5709013 :   if (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt)) == tcc_reference)
     286                 :             :     return false;
     287                 :             : 
     288                 :     9990953 :   FOR_EACH_IMM_USE_FAST (use, use_iter, gimple_assign_lhs (stmt))
     289                 :             :     {
     290                 :     6284525 :       gimple *current_use_stmt = USE_STMT (use);
     291                 :             : 
     292                 :     6284525 :       if (is_gimple_assign (current_use_stmt)
     293                 :     6284525 :           && TREE_CODE (gimple_assign_lhs (current_use_stmt)) == SSA_NAME)
     294                 :             :         {
     295                 :     4961068 :           if (single_use_stmt != NULL && single_use_stmt != current_use_stmt)
     296                 :             :             return false;
     297                 :             :           single_use_stmt = current_use_stmt;
     298                 :             :         }
     299                 :             :     }
     300                 :             : 
     301                 :     3706428 :   if (single_use_stmt == NULL)
     302                 :             :     return false;
     303                 :             : 
     304                 :     3706165 :   if (gimple_bb (stmt)->loop_father
     305                 :     3706165 :       != gimple_bb (single_use_stmt)->loop_father)
     306                 :             :     return false;
     307                 :             : 
     308                 :             :   return true;
     309                 :             : }
     310                 :             : 
     311                 :             : /* Rank assigned to a phi statement.  If STMT is a loop-carried phi of
     312                 :             :    an innermost loop, and the phi has only a single use which is inside
     313                 :             :    the loop, then the rank is the block rank of the loop latch plus an
     314                 :             :    extra bias for the loop-carried dependence.  This causes expressions
     315                 :             :    calculated into an accumulator variable to be independent for each
     316                 :             :    iteration of the loop.  If STMT is some other phi, the rank is the
     317                 :             :    block rank of its containing block.  */
     318                 :             : static int64_t
     319                 :     1200021 : phi_rank (gimple *stmt)
     320                 :             : {
     321                 :     1200021 :   basic_block bb = gimple_bb (stmt);
     322                 :     1200021 :   class loop *father = bb->loop_father;
     323                 :     1200021 :   tree res;
     324                 :     1200021 :   unsigned i;
     325                 :     1200021 :   use_operand_p use;
     326                 :     1200021 :   gimple *use_stmt;
     327                 :             : 
     328                 :     1200021 :   if (!reassoc_bias_loop_carried_phi_ranks_p)
     329                 :      470781 :     return bb_rank[bb->index];
     330                 :             : 
     331                 :             :   /* We only care about real loops (those with a latch).  */
     332                 :      729240 :   if (!father->latch)
     333                 :          93 :     return bb_rank[bb->index];
     334                 :             : 
     335                 :             :   /* Interesting phis must be in headers of innermost loops.  */
     336                 :      729147 :   if (bb != father->header
     337                 :      579342 :       || father->inner)
     338                 :      278810 :     return bb_rank[bb->index];
     339                 :             : 
     340                 :             :   /* Ignore virtual SSA_NAMEs.  */
     341                 :      450337 :   res = gimple_phi_result (stmt);
     342                 :      900674 :   if (virtual_operand_p (res))
     343                 :           0 :     return bb_rank[bb->index];
     344                 :             : 
     345                 :             :   /* The phi definition must have a single use, and that use must be
     346                 :             :      within the loop.  Otherwise this isn't an accumulator pattern.  */
     347                 :      450337 :   if (!single_imm_use (res, &use, &use_stmt)
     348                 :      450337 :       || gimple_bb (use_stmt)->loop_father != father)
     349                 :      392606 :     return bb_rank[bb->index];
     350                 :             : 
     351                 :             :   /* Look for phi arguments from within the loop.  If found, bias this phi.  */
     352                 :       67811 :   for (i = 0; i < gimple_phi_num_args (stmt); i++)
     353                 :             :     {
     354                 :       67629 :       tree arg = gimple_phi_arg_def (stmt, i);
     355                 :       67629 :       if (TREE_CODE (arg) == SSA_NAME
     356                 :       67629 :           && !SSA_NAME_IS_DEFAULT_DEF (arg))
     357                 :             :         {
     358                 :       62807 :           gimple *def_stmt = SSA_NAME_DEF_STMT (arg);
     359                 :       62807 :           if (gimple_bb (def_stmt)->loop_father == father)
     360                 :       57549 :             return bb_rank[father->latch->index] + PHI_LOOP_BIAS;
     361                 :             :         }
     362                 :             :     }
     363                 :             : 
     364                 :             :   /* Must be an uninteresting phi.  */
     365                 :         182 :   return bb_rank[bb->index];
     366                 :             : }
     367                 :             : 
     368                 :             : /* Return the maximum of RANK and the rank that should be propagated
     369                 :             :    from expression OP.  For most operands, this is just the rank of OP.
     370                 :             :    For loop-carried phis, the value is zero to avoid undoing the bias
     371                 :             :    in favor of the phi.  */
     372                 :             : static int64_t
     373                 :     6216238 : propagate_rank (int64_t rank, tree op, bool *maybe_biased_p)
     374                 :             : {
     375                 :     6216238 :   int64_t op_rank;
     376                 :             : 
     377                 :     6216238 :   op_rank = get_rank (op);
     378                 :             : 
     379                 :             :   /* Check whether op is biased after the get_rank () call, since it might have
     380                 :             :      updated biased_names.  */
     381                 :     6216238 :   if (TREE_CODE (op) == SSA_NAME
     382                 :     6216238 :       && bitmap_bit_p (biased_names, SSA_NAME_VERSION (op)))
     383                 :             :     {
     384                 :       44664 :       if (maybe_biased_p == NULL)
     385                 :             :         return rank;
     386                 :       32941 :       *maybe_biased_p = true;
     387                 :             :     }
     388                 :             : 
     389                 :     6204515 :   return MAX (rank, op_rank);
     390                 :             : }
     391                 :             : 
     392                 :             : /* Look up the operand rank structure for expression E.  */
     393                 :             : 
     394                 :             : static inline int64_t
     395                 :    11445304 : find_operand_rank (tree e)
     396                 :             : {
     397                 :    11445304 :   int64_t *slot = operand_rank->get (e);
     398                 :    11445304 :   return slot ? *slot : -1;
     399                 :             : }
     400                 :             : 
     401                 :             : /* Insert {E,RANK} into the operand rank hashtable.  */
     402                 :             : 
     403                 :             : static inline void
     404                 :    12879661 : insert_operand_rank (tree e, int64_t rank)
     405                 :             : {
     406                 :    12879661 :   gcc_assert (rank > 0);
     407                 :    12879661 :   gcc_assert (!operand_rank->put (e, rank));
     408                 :    12879661 : }
     409                 :             : 
     410                 :             : /* Given an expression E, return the rank of the expression.  */
     411                 :             : 
     412                 :             : static int64_t
     413                 :    14207567 : get_rank (tree e)
     414                 :             : {
     415                 :             :   /* SSA_NAME's have the rank of the expression they are the result
     416                 :             :      of.
     417                 :             :      For globals and uninitialized values, the rank is 0.
     418                 :             :      For function arguments, use the pre-setup rank.
     419                 :             :      For PHI nodes, stores, asm statements, etc, we use the rank of
     420                 :             :      the BB.
     421                 :             :      For simple operations, the rank is the maximum rank of any of
     422                 :             :      its operands, or the bb_rank, whichever is less.
     423                 :             :      I make no claims that this is optimal, however, it gives good
     424                 :             :      results.  */
     425                 :             : 
     426                 :             :   /* We make an exception to the normal ranking system to break
     427                 :             :      dependences of accumulator variables in loops.  Suppose we
     428                 :             :      have a simple one-block loop containing:
     429                 :             : 
     430                 :             :        x_1 = phi(x_0, x_2)
     431                 :             :        b = a + x_1
     432                 :             :        c = b + d
     433                 :             :        x_2 = c + e
     434                 :             : 
     435                 :             :      As shown, each iteration of the calculation into x is fully
     436                 :             :      dependent upon the iteration before it.  We would prefer to
     437                 :             :      see this in the form:
     438                 :             : 
     439                 :             :        x_1 = phi(x_0, x_2)
     440                 :             :        b = a + d
     441                 :             :        c = b + e
     442                 :             :        x_2 = c + x_1
     443                 :             : 
     444                 :             :      If the loop is unrolled, the calculations of b and c from
     445                 :             :      different iterations can be interleaved.
     446                 :             : 
     447                 :             :      To obtain this result during reassociation, we bias the rank
     448                 :             :      of the phi definition x_1 upward, when it is recognized as an
     449                 :             :      accumulator pattern.  The artificial rank causes it to be 
     450                 :             :      added last, providing the desired independence.  */
     451                 :             : 
     452                 :    14207567 :   if (TREE_CODE (e) == SSA_NAME)
     453                 :             :     {
     454                 :    11445304 :       ssa_op_iter iter;
     455                 :    11445304 :       gimple *stmt;
     456                 :    11445304 :       int64_t rank;
     457                 :    11445304 :       tree op;
     458                 :             : 
     459                 :             :       /* If we already have a rank for this expression, use that.  */
     460                 :    11445304 :       rank = find_operand_rank (e);
     461                 :    11445304 :       if (rank != -1)
     462                 :             :         return rank;
     463                 :             : 
     464                 :     7162706 :       stmt = SSA_NAME_DEF_STMT (e);
     465                 :     7162706 :       if (gimple_code (stmt) == GIMPLE_PHI)
     466                 :             :         {
     467                 :     1200021 :           rank = phi_rank (stmt);
     468                 :     1200021 :           if (rank != bb_rank[gimple_bb (stmt)->index])
     469                 :       57549 :             bitmap_set_bit (biased_names, SSA_NAME_VERSION (e));
     470                 :             :         }
     471                 :             : 
     472                 :     5962685 :       else if (!is_gimple_assign (stmt))
     473                 :      253672 :         rank = bb_rank[gimple_bb (stmt)->index];
     474                 :             : 
     475                 :             :       else
     476                 :             :         {
     477                 :     5709013 :           bool biased_p = false;
     478                 :     5709013 :           bool *maybe_biased_p = propagate_bias_p (stmt) ? &biased_p : NULL;
     479                 :             : 
     480                 :             :           /* Otherwise, find the maximum rank for the operands.  As an
     481                 :             :              exception, remove the bias from loop-carried phis when propagating
     482                 :             :              the rank so that dependent operations are not also biased.  */
     483                 :             :           /* Simply walk over all SSA uses - this takes advatage of the
     484                 :             :              fact that non-SSA operands are is_gimple_min_invariant and
     485                 :             :              thus have rank 0.  */
     486                 :     5709013 :           rank = 0;
     487                 :    11925251 :           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
     488                 :     6216238 :             rank = propagate_rank (rank, op, maybe_biased_p);
     489                 :             : 
     490                 :     5709013 :           rank += 1;
     491                 :     5709013 :           if (biased_p)
     492                 :       31596 :             bitmap_set_bit (biased_names, SSA_NAME_VERSION (e));
     493                 :             :         }
     494                 :             : 
     495                 :     7162706 :       if (dump_file && (dump_flags & TDF_DETAILS))
     496                 :             :         {
     497                 :         194 :           fprintf (dump_file, "Rank for ");
     498                 :         194 :           print_generic_expr (dump_file, e);
     499                 :         194 :           fprintf (dump_file, " is %" PRId64 "\n", rank);
     500                 :             :         }
     501                 :             : 
     502                 :             :       /* Note the rank in the hashtable so we don't recompute it.  */
     503                 :     7162706 :       insert_operand_rank (e, rank);
     504                 :     7162706 :       return rank;
     505                 :             :     }
     506                 :             : 
     507                 :             :   /* Constants, globals, etc., are rank 0 */
     508                 :             :   return 0;
     509                 :             : }
     510                 :             : 
     511                 :             : 
     512                 :             : /* We want integer ones to end up last no matter what, since they are
     513                 :             :    the ones we can do the most with.  */
     514                 :             : #define INTEGER_CONST_TYPE 1 << 4
     515                 :             : #define FLOAT_ONE_CONST_TYPE 1 << 3
     516                 :             : #define FLOAT_CONST_TYPE 1 << 2
     517                 :             : #define OTHER_CONST_TYPE 1 << 1
     518                 :             : 
     519                 :             : /* Classify an invariant tree into integer, float, or other, so that
     520                 :             :    we can sort them to be near other constants of the same type.  */
     521                 :             : static inline int
     522                 :      247404 : constant_type (tree t)
     523                 :             : {
     524                 :      247404 :   if (INTEGRAL_TYPE_P (TREE_TYPE (t)))
     525                 :             :     return INTEGER_CONST_TYPE;
     526                 :        7096 :   else if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (t)))
     527                 :             :     {
     528                 :             :       /* Sort -1.0 and 1.0 constants last, while in some cases
     529                 :             :          const_binop can't optimize some inexact operations, multiplication
     530                 :             :          by -1.0 or 1.0 can be always merged with others.  */
     531                 :        6632 :       if (real_onep (t) || real_minus_onep (t))
     532                 :         824 :         return FLOAT_ONE_CONST_TYPE;
     533                 :             :       return FLOAT_CONST_TYPE;
     534                 :             :     }
     535                 :             :   else
     536                 :             :     return OTHER_CONST_TYPE;
     537                 :             : }
     538                 :             : 
     539                 :             : /* qsort comparison function to sort operand entries PA and PB by rank
     540                 :             :    so that the sorted array is ordered by rank in decreasing order.  */
     541                 :             : static int
     542                 :    19628201 : sort_by_operand_rank (const void *pa, const void *pb)
     543                 :             : {
     544                 :    19628201 :   const operand_entry *oea = *(const operand_entry *const *)pa;
     545                 :    19628201 :   const operand_entry *oeb = *(const operand_entry *const *)pb;
     546                 :             : 
     547                 :    19628201 :   if (oeb->rank != oea->rank)
     548                 :    28888767 :     return oeb->rank > oea->rank ? 1 : -1;
     549                 :             : 
     550                 :             :   /* It's nicer for optimize_expression if constants that are likely
     551                 :             :      to fold when added/multiplied/whatever are put next to each
     552                 :             :      other.  Since all constants have rank 0, order them by type.  */
     553                 :     2484236 :   if (oea->rank == 0)
     554                 :             :     {
     555                 :      123640 :       if (constant_type (oeb->op) != constant_type (oea->op))
     556                 :          62 :         return constant_type (oea->op) - constant_type (oeb->op);
     557                 :             :       else
     558                 :             :         /* To make sorting result stable, we use unique IDs to determine
     559                 :             :            order.  */
     560                 :      196763 :         return oeb->id > oea->id ? 1 : -1;
     561                 :             :     }
     562                 :             : 
     563                 :     2360596 :   if (TREE_CODE (oea->op) != SSA_NAME)
     564                 :             :     {
     565                 :           0 :       if (TREE_CODE (oeb->op) != SSA_NAME)
     566                 :           0 :         return oeb->id > oea->id ? 1 : -1;
     567                 :             :       else
     568                 :             :         return 1;
     569                 :             :     }
     570                 :     2360596 :   else if (TREE_CODE (oeb->op) != SSA_NAME)
     571                 :             :     return -1;
     572                 :             : 
     573                 :             :   /* Lastly, make sure the versions that are the same go next to each
     574                 :             :      other.  */
     575                 :     2360596 :   if (SSA_NAME_VERSION (oeb->op) != SSA_NAME_VERSION (oea->op))
     576                 :             :     {
     577                 :             :       /* As SSA_NAME_VERSION is assigned pretty randomly, because we reuse
     578                 :             :          versions of removed SSA_NAMEs, so if possible, prefer to sort
     579                 :             :          based on basic block and gimple_uid of the SSA_NAME_DEF_STMT.
     580                 :             :          See PR60418.  */
     581                 :     2311867 :       gimple *stmta = SSA_NAME_DEF_STMT (oea->op);
     582                 :     2311867 :       gimple *stmtb = SSA_NAME_DEF_STMT (oeb->op);
     583                 :     2311867 :       basic_block bba = gimple_bb (stmta);
     584                 :     2311867 :       basic_block bbb = gimple_bb (stmtb);
     585                 :     2311867 :       if (bbb != bba)
     586                 :             :         {
     587                 :             :           /* One of the SSA_NAMEs can be defined in oeN->stmt_to_insert
     588                 :             :              but the other might not.  */
     589                 :      160514 :           if (!bba)
     590                 :             :             return 1;
     591                 :      156157 :           if (!bbb)
     592                 :             :             return -1;
     593                 :             :           /* If neither is, compare bb_rank.  */
     594                 :      150753 :           if (bb_rank[bbb->index] != bb_rank[bba->index])
     595                 :      150753 :             return (bb_rank[bbb->index] >> 16) - (bb_rank[bba->index] >> 16);
     596                 :             :         }
     597                 :             : 
     598                 :     2151353 :       bool da = reassoc_stmt_dominates_stmt_p (stmta, stmtb);
     599                 :     2151353 :       bool db = reassoc_stmt_dominates_stmt_p (stmtb, stmta);
     600                 :     2151353 :       if (da != db)
     601                 :     3271929 :         return da ? 1 : -1;
     602                 :             : 
     603                 :       56525 :       return SSA_NAME_VERSION (oeb->op) > SSA_NAME_VERSION (oea->op) ? 1 : -1;
     604                 :             :     }
     605                 :             : 
     606                 :       48729 :   return oeb->id > oea->id ? 1 : -1;
     607                 :             : }
     608                 :             : 
     609                 :             : /* Add an operand entry to *OPS for the tree operand OP.  */
     610                 :             : 
     611                 :             : static void
     612                 :     7991015 : add_to_ops_vec (vec<operand_entry *> *ops, tree op, gimple *stmt_to_insert = NULL)
     613                 :             : {
     614                 :     7991015 :   operand_entry *oe = operand_entry_pool.allocate ();
     615                 :             : 
     616                 :     7991015 :   oe->op = op;
     617                 :     7991015 :   oe->rank = get_rank (op);
     618                 :     7991015 :   oe->id = next_operand_entry_id++;
     619                 :     7991015 :   oe->count = 1;
     620                 :     7991015 :   oe->stmt_to_insert = stmt_to_insert;
     621                 :     7991015 :   ops->safe_push (oe);
     622                 :     7991015 : }
     623                 :             : 
     624                 :             : /* Add an operand entry to *OPS for the tree operand OP with repeat
     625                 :             :    count REPEAT.  */
     626                 :             : 
     627                 :             : static void
     628                 :          15 : add_repeat_to_ops_vec (vec<operand_entry *> *ops, tree op,
     629                 :             :                        HOST_WIDE_INT repeat)
     630                 :             : {
     631                 :          15 :   operand_entry *oe = operand_entry_pool.allocate ();
     632                 :             : 
     633                 :          15 :   oe->op = op;
     634                 :          15 :   oe->rank = get_rank (op);
     635                 :          15 :   oe->id = next_operand_entry_id++;
     636                 :          15 :   oe->count = repeat;
     637                 :          15 :   oe->stmt_to_insert = NULL;
     638                 :          15 :   ops->safe_push (oe);
     639                 :             : 
     640                 :          15 :   reassociate_stats.pows_encountered++;
     641                 :          15 : }
     642                 :             : 
     643                 :             : /* Returns true if we can associate the SSA def OP.  */
     644                 :             : 
     645                 :             : static bool
     646                 :    27715624 : can_reassociate_op_p (tree op)
     647                 :             : {
     648                 :    27715624 :   if (TREE_CODE (op) == SSA_NAME && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op))
     649                 :             :     return false;
     650                 :             :   /* Uninitialized variables can't participate in reassociation. */
     651                 :    27714814 :   if (TREE_CODE (op) == SSA_NAME && ssa_name_maybe_undef_p (op))
     652                 :             :     return false;
     653                 :             :   /* Make sure asm goto outputs do not participate in reassociation since
     654                 :             :      we have no way to find an insertion place after asm goto.  */
     655                 :    27710511 :   if (TREE_CODE (op) == SSA_NAME
     656                 :    20503543 :       && gimple_code (SSA_NAME_DEF_STMT (op)) == GIMPLE_ASM
     657                 :    27740572 :       && gimple_asm_nlabels (as_a <gasm *> (SSA_NAME_DEF_STMT (op))) != 0)
     658                 :             :     return false;
     659                 :             :   return true;
     660                 :             : }
     661                 :             : 
     662                 :             : /* Returns true if we can reassociate operations of TYPE.
     663                 :             :    That is for integral or non-saturating fixed-point types, and for
     664                 :             :    floating point type when associative-math is enabled.  */
     665                 :             : 
     666                 :             : static bool
     667                 :    51374988 : can_reassociate_type_p (tree type)
     668                 :             : {
     669                 :    51374988 :   if ((ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_WRAPS (type))
     670                 :    31986898 :       || NON_SAT_FIXED_POINT_TYPE_P (type)
     671                 :    83361886 :       || (flag_associative_math && FLOAT_TYPE_P (type)))
     672                 :    19764841 :     return true;
     673                 :             :   return false;
     674                 :             : }
     675                 :             : 
     676                 :             : /* Return true if STMT is reassociable operation containing a binary
     677                 :             :    operation with tree code CODE, and is inside LOOP.  */
     678                 :             : 
     679                 :             : static bool
     680                 :     6521571 : is_reassociable_op (gimple *stmt, enum tree_code code, class loop *loop)
     681                 :             : {
     682                 :     6521571 :   basic_block bb = gimple_bb (stmt);
     683                 :             : 
     684                 :     6521571 :   if (gimple_bb (stmt) == NULL)
     685                 :             :     return false;
     686                 :             : 
     687                 :     6371653 :   if (!flow_bb_inside_loop_p (loop, bb))
     688                 :             :     return false;
     689                 :             : 
     690                 :     6184022 :   if (is_gimple_assign (stmt)
     691                 :     4901617 :       && gimple_assign_rhs_code (stmt) == code
     692                 :     6896206 :       && has_single_use (gimple_assign_lhs (stmt)))
     693                 :             :     {
     694                 :      522795 :       tree rhs1 = gimple_assign_rhs1 (stmt);
     695                 :      522795 :       tree rhs2 = gimple_assign_rhs2 (stmt);
     696                 :      522795 :       if (!can_reassociate_op_p (rhs1)
     697                 :      522795 :           || (rhs2 && !can_reassociate_op_p (rhs2)))
     698                 :             :         return false;
     699                 :             :       return true;
     700                 :             :     }
     701                 :             : 
     702                 :             :   return false;
     703                 :             : }
     704                 :             : 
     705                 :             : 
     706                 :             : /* Return true if STMT is a nop-conversion.  */
     707                 :             : 
     708                 :             : static bool
     709                 :     6383794 : gimple_nop_conversion_p (gimple *stmt)
     710                 :             : {
     711                 :     6383794 :   if (gassign *ass = dyn_cast <gassign *> (stmt))
     712                 :             :     {
     713                 :     7599582 :       if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (ass))
     714                 :     5527485 :           && tree_nop_conversion_p (TREE_TYPE (gimple_assign_lhs (ass)),
     715                 :     1151796 :                                     TREE_TYPE (gimple_assign_rhs1 (ass))))
     716                 :             :         return true;
     717                 :             :     }
     718                 :             :   return false;
     719                 :             : }
     720                 :             : 
     721                 :             : /* Given NAME, if NAME is defined by a unary operation OPCODE, return the
     722                 :             :    operand of the negate operation.  Otherwise, return NULL.  */
     723                 :             : 
     724                 :             : static tree
     725                 :     6311640 : get_unary_op (tree name, enum tree_code opcode)
     726                 :             : {
     727                 :     6311640 :   gimple *stmt = SSA_NAME_DEF_STMT (name);
     728                 :             : 
     729                 :             :   /* Look through nop conversions (sign changes).  */
     730                 :     6311640 :   if (gimple_nop_conversion_p (stmt)
     731                 :     6311640 :       && TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME)
     732                 :      628385 :     stmt = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
     733                 :             : 
     734                 :     6311640 :   if (!is_gimple_assign (stmt))
     735                 :             :     return NULL_TREE;
     736                 :             : 
     737                 :     4038873 :   if (gimple_assign_rhs_code (stmt) == opcode)
     738                 :      105359 :     return gimple_assign_rhs1 (stmt);
     739                 :             :   return NULL_TREE;
     740                 :             : }
     741                 :             : 
     742                 :             : /* Return true if OP1 and OP2 have the same value if casted to either type.  */
     743                 :             : 
     744                 :             : static bool
     745                 :       37705 : ops_equal_values_p (tree op1, tree op2)
     746                 :             : {
     747                 :       37705 :   if (op1 == op2)
     748                 :             :     return true;
     749                 :             : 
     750                 :       36584 :   tree orig_op1 = op1;
     751                 :       36584 :   if (TREE_CODE (op1) == SSA_NAME)
     752                 :             :     {
     753                 :       36584 :       gimple *stmt = SSA_NAME_DEF_STMT (op1);
     754                 :       36584 :       if (gimple_nop_conversion_p (stmt))
     755                 :             :         {
     756                 :       12711 :           op1 = gimple_assign_rhs1 (stmt);
     757                 :       12711 :           if (op1 == op2)
     758                 :             :             return true;
     759                 :             :         }
     760                 :             :     }
     761                 :             : 
     762                 :       35570 :   if (TREE_CODE (op2) == SSA_NAME)
     763                 :             :     {
     764                 :       35570 :       gimple *stmt = SSA_NAME_DEF_STMT (op2);
     765                 :       35570 :       if (gimple_nop_conversion_p (stmt))
     766                 :             :         {
     767                 :       11861 :           op2 = gimple_assign_rhs1 (stmt);
     768                 :       11861 :           if (op1 == op2
     769                 :       11861 :               || orig_op1 == op2)
     770                 :             :             return true;
     771                 :             :         }
     772                 :             :     }
     773                 :             : 
     774                 :             :   return false;
     775                 :             : }
     776                 :             : 
     777                 :             : 
     778                 :             : /* If CURR and LAST are a pair of ops that OPCODE allows us to
     779                 :             :    eliminate through equivalences, do so, remove them from OPS, and
     780                 :             :    return true.  Otherwise, return false.  */
     781                 :             : 
     782                 :             : static bool
     783                 :     7895912 : eliminate_duplicate_pair (enum tree_code opcode,
     784                 :             :                           vec<operand_entry *> *ops,
     785                 :             :                           bool *all_done,
     786                 :             :                           unsigned int i,
     787                 :             :                           operand_entry *curr,
     788                 :             :                           operand_entry *last)
     789                 :             : {
     790                 :             : 
     791                 :             :   /* If we have two of the same op, and the opcode is & |, min, or max,
     792                 :             :      we can eliminate one of them.
     793                 :             :      If we have two of the same op, and the opcode is ^, we can
     794                 :             :      eliminate both of them.  */
     795                 :             : 
     796                 :     7895912 :   if (last && last->op == curr->op)
     797                 :             :     {
     798                 :        4751 :       switch (opcode)
     799                 :             :         {
     800                 :          20 :         case MAX_EXPR:
     801                 :          20 :         case MIN_EXPR:
     802                 :          20 :         case BIT_IOR_EXPR:
     803                 :          20 :         case BIT_AND_EXPR:
     804                 :          20 :           if (dump_file && (dump_flags & TDF_DETAILS))
     805                 :             :             {
     806                 :           1 :               fprintf (dump_file, "Equivalence: ");
     807                 :           1 :               print_generic_expr (dump_file, curr->op);
     808                 :           1 :               fprintf (dump_file, " [&|minmax] ");
     809                 :           1 :               print_generic_expr (dump_file, last->op);
     810                 :           1 :               fprintf (dump_file, " -> ");
     811                 :           1 :               print_generic_stmt (dump_file, last->op);
     812                 :             :             }
     813                 :             : 
     814                 :          20 :           ops->ordered_remove (i);
     815                 :          20 :           reassociate_stats.ops_eliminated ++;
     816                 :             : 
     817                 :          20 :           return true;
     818                 :             : 
     819                 :          14 :         case BIT_XOR_EXPR:
     820                 :          14 :           if (dump_file && (dump_flags & TDF_DETAILS))
     821                 :             :             {
     822                 :           0 :               fprintf (dump_file, "Equivalence: ");
     823                 :           0 :               print_generic_expr (dump_file, curr->op);
     824                 :           0 :               fprintf (dump_file, " ^ ");
     825                 :           0 :               print_generic_expr (dump_file, last->op);
     826                 :           0 :               fprintf (dump_file, " -> nothing\n");
     827                 :             :             }
     828                 :             : 
     829                 :          14 :           reassociate_stats.ops_eliminated += 2;
     830                 :             : 
     831                 :          14 :           if (ops->length () == 2)
     832                 :             :             {
     833                 :           1 :               ops->truncate (0);
     834                 :           1 :               add_to_ops_vec (ops, build_zero_cst (TREE_TYPE (last->op)));
     835                 :           1 :               *all_done = true;
     836                 :             :             }
     837                 :             :           else
     838                 :             :             {
     839                 :          13 :               ops->ordered_remove (i-1);
     840                 :          13 :               ops->ordered_remove (i-1);
     841                 :             :             }
     842                 :             : 
     843                 :          14 :           return true;
     844                 :             : 
     845                 :             :         default:
     846                 :             :           break;
     847                 :             :         }
     848                 :             :     }
     849                 :             :   return false;
     850                 :             : }
     851                 :             : 
     852                 :             : static vec<tree> plus_negates;
     853                 :             : 
     854                 :             : /* If OPCODE is PLUS_EXPR, CURR->OP is a negate expression or a bitwise not
     855                 :             :    expression, look in OPS for a corresponding positive operation to cancel
     856                 :             :    it out.  If we find one, remove the other from OPS, replace
     857                 :             :    OPS[CURRINDEX] with 0 or -1, respectively, and return true.  Otherwise,
     858                 :             :    return false. */
     859                 :             : 
     860                 :             : static bool
     861                 :     7895878 : eliminate_plus_minus_pair (enum tree_code opcode,
     862                 :             :                            vec<operand_entry *> *ops,
     863                 :             :                            unsigned int currindex,
     864                 :             :                            operand_entry *curr)
     865                 :             : {
     866                 :     7895878 :   tree negateop;
     867                 :     7895878 :   tree notop;
     868                 :     7895878 :   unsigned int i;
     869                 :     7895878 :   operand_entry *oe;
     870                 :             : 
     871                 :     7895878 :   if (opcode != PLUS_EXPR || TREE_CODE (curr->op) != SSA_NAME)
     872                 :             :     return false;
     873                 :             : 
     874                 :     2459869 :   negateop = get_unary_op (curr->op, NEGATE_EXPR);
     875                 :     2459869 :   notop = get_unary_op (curr->op, BIT_NOT_EXPR);
     876                 :     2459869 :   if (negateop == NULL_TREE && notop == NULL_TREE)
     877                 :             :     return false;
     878                 :             : 
     879                 :             :   /* Any non-negated version will have a rank that is one less than
     880                 :             :      the current rank.  So once we hit those ranks, if we don't find
     881                 :             :      one, we can stop.  */
     882                 :             : 
     883                 :      108504 :   for (i = currindex + 1;
     884                 :      156900 :        ops->iterate (i, &oe)
     885                 :      194605 :        && oe->rank >= curr->rank - 1 ;
     886                 :             :        i++)
     887                 :             :     {
     888                 :       37705 :       if (negateop
     889                 :       37705 :           && ops_equal_values_p (oe->op, negateop))
     890                 :             :         {
     891                 :        2076 :           if (dump_file && (dump_flags & TDF_DETAILS))
     892                 :             :             {
     893                 :           0 :               fprintf (dump_file, "Equivalence: ");
     894                 :           0 :               print_generic_expr (dump_file, negateop);
     895                 :           0 :               fprintf (dump_file, " + -");
     896                 :           0 :               print_generic_expr (dump_file, oe->op);
     897                 :           0 :               fprintf (dump_file, " -> 0\n");
     898                 :             :             }
     899                 :             : 
     900                 :        2076 :           ops->ordered_remove (i);
     901                 :        2076 :           add_to_ops_vec (ops, build_zero_cst (TREE_TYPE (oe->op)));
     902                 :        2076 :           ops->ordered_remove (currindex);
     903                 :        2076 :           reassociate_stats.ops_eliminated ++;
     904                 :             : 
     905                 :        2076 :           return true;
     906                 :             :         }
     907                 :       35629 :       else if (notop
     908                 :       35629 :                && ops_equal_values_p (oe->op, notop))
     909                 :             :         {
     910                 :        1010 :           tree op_type = TREE_TYPE (oe->op);
     911                 :             : 
     912                 :        1010 :           if (dump_file && (dump_flags & TDF_DETAILS))
     913                 :             :             {
     914                 :           0 :               fprintf (dump_file, "Equivalence: ");
     915                 :           0 :               print_generic_expr (dump_file, notop);
     916                 :           0 :               fprintf (dump_file, " + ~");
     917                 :           0 :               print_generic_expr (dump_file, oe->op);
     918                 :           0 :               fprintf (dump_file, " -> -1\n");
     919                 :             :             }
     920                 :             : 
     921                 :        1010 :           ops->ordered_remove (i);
     922                 :        1010 :           add_to_ops_vec (ops, build_all_ones_cst (op_type));
     923                 :        1010 :           ops->ordered_remove (currindex);
     924                 :        1010 :           reassociate_stats.ops_eliminated ++;
     925                 :             : 
     926                 :        1010 :           return true;
     927                 :             :         }
     928                 :             :     }
     929                 :             : 
     930                 :             :   /* If CURR->OP is a negate expr without nop conversion in a plus expr: 
     931                 :             :      save it for later inspection in repropagate_negates().  */
     932                 :       70799 :   if (negateop != NULL_TREE
     933                 :       70799 :       && gimple_assign_rhs_code (SSA_NAME_DEF_STMT (curr->op)) == NEGATE_EXPR)
     934                 :       70347 :     plus_negates.safe_push (curr->op);
     935                 :             : 
     936                 :             :   return false;
     937                 :             : }
     938                 :             : 
     939                 :             : /* If OPCODE is BIT_IOR_EXPR, BIT_AND_EXPR, and, CURR->OP is really a
     940                 :             :    bitwise not expression, look in OPS for a corresponding operand to
     941                 :             :    cancel it out.  If we find one, remove the other from OPS, replace
     942                 :             :    OPS[CURRINDEX] with 0, and return true.  Otherwise, return
     943                 :             :    false. */
     944                 :             : 
     945                 :             : static bool
     946                 :     7895913 : eliminate_not_pairs (enum tree_code opcode,
     947                 :             :                      vec<operand_entry *> *ops,
     948                 :             :                      unsigned int currindex,
     949                 :             :                      operand_entry *curr)
     950                 :             : {
     951                 :     7895913 :   tree notop;
     952                 :     7895913 :   unsigned int i;
     953                 :     7895913 :   operand_entry *oe;
     954                 :             : 
     955                 :     7895913 :   if ((opcode != BIT_IOR_EXPR && opcode != BIT_AND_EXPR)
     956                 :     1867339 :       || TREE_CODE (curr->op) != SSA_NAME)
     957                 :             :     return false;
     958                 :             : 
     959                 :     1391902 :   notop = get_unary_op (curr->op, BIT_NOT_EXPR);
     960                 :     1391902 :   if (notop == NULL_TREE)
     961                 :             :     return false;
     962                 :             : 
     963                 :             :   /* Any non-not version will have a rank that is one less than
     964                 :             :      the current rank.  So once we hit those ranks, if we don't find
     965                 :             :      one, we can stop.  */
     966                 :             : 
     967                 :       36977 :   for (i = currindex + 1;
     968                 :     7919850 :        ops->iterate (i, &oe)
     969                 :       60914 :        && oe->rank >= curr->rank - 1;
     970                 :             :        i++)
     971                 :             :     {
     972                 :        5504 :       if (oe->op == notop)
     973                 :             :         {
     974                 :           1 :           if (dump_file && (dump_flags & TDF_DETAILS))
     975                 :             :             {
     976                 :           0 :               fprintf (dump_file, "Equivalence: ");
     977                 :           0 :               print_generic_expr (dump_file, notop);
     978                 :           0 :               if (opcode == BIT_AND_EXPR)
     979                 :           0 :                 fprintf (dump_file, " & ~");
     980                 :           0 :               else if (opcode == BIT_IOR_EXPR)
     981                 :           0 :                 fprintf (dump_file, " | ~");
     982                 :           0 :               print_generic_expr (dump_file, oe->op);
     983                 :           0 :               if (opcode == BIT_AND_EXPR)
     984                 :           0 :                 fprintf (dump_file, " -> 0\n");
     985                 :           0 :               else if (opcode == BIT_IOR_EXPR)
     986                 :           0 :                 fprintf (dump_file, " -> -1\n");
     987                 :             :             }
     988                 :             : 
     989                 :           1 :           if (opcode == BIT_AND_EXPR)
     990                 :           1 :             oe->op = build_zero_cst (TREE_TYPE (oe->op));
     991                 :           0 :           else if (opcode == BIT_IOR_EXPR)
     992                 :           0 :             oe->op = build_all_ones_cst (TREE_TYPE (oe->op));
     993                 :             : 
     994                 :           1 :           reassociate_stats.ops_eliminated += ops->length () - 1;
     995                 :           1 :           ops->truncate (0);
     996                 :           1 :           ops->quick_push (oe);
     997                 :           1 :           return true;
     998                 :             :         }
     999                 :             :     }
    1000                 :             : 
    1001                 :             :   return false;
    1002                 :             : }
    1003                 :             : 
    1004                 :             : /* Use constant value that may be present in OPS to try to eliminate
    1005                 :             :    operands.  Note that this function is only really used when we've
    1006                 :             :    eliminated ops for other reasons, or merged constants.  Across
    1007                 :             :    single statements, fold already does all of this, plus more.  There
    1008                 :             :    is little point in duplicating logic, so I've only included the
    1009                 :             :    identities that I could ever construct testcases to trigger.  */
    1010                 :             : 
    1011                 :             : static void
    1012                 :     3836071 : eliminate_using_constants (enum tree_code opcode,
    1013                 :             :                            vec<operand_entry *> *ops)
    1014                 :             : {
    1015                 :     3836071 :   operand_entry *oelast = ops->last ();
    1016                 :     3836071 :   tree type = TREE_TYPE (oelast->op);
    1017                 :             : 
    1018                 :     3836071 :   if (oelast->rank == 0
    1019                 :     3836071 :       && (ANY_INTEGRAL_TYPE_P (type) || FLOAT_TYPE_P (type)))
    1020                 :             :     {
    1021                 :     2730061 :       switch (opcode)
    1022                 :             :         {
    1023                 :      403345 :         case BIT_AND_EXPR:
    1024                 :      403345 :           if (integer_zerop (oelast->op))
    1025                 :             :             {
    1026                 :           0 :               if (ops->length () != 1)
    1027                 :             :                 {
    1028                 :           0 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    1029                 :           0 :                     fprintf (dump_file, "Found & 0, removing all other ops\n");
    1030                 :             : 
    1031                 :           0 :                   reassociate_stats.ops_eliminated += ops->length () - 1;
    1032                 :             : 
    1033                 :           0 :                   ops->truncate (0);
    1034                 :           0 :                   ops->quick_push (oelast);
    1035                 :        2001 :                   return;
    1036                 :             :                 }
    1037                 :             :             }
    1038                 :      403345 :           else if (integer_all_onesp (oelast->op))
    1039                 :             :             {
    1040                 :          61 :               if (ops->length () != 1)
    1041                 :             :                 {
    1042                 :          61 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    1043                 :           0 :                     fprintf (dump_file, "Found & -1, removing\n");
    1044                 :          61 :                   ops->pop ();
    1045                 :          61 :                   reassociate_stats.ops_eliminated++;
    1046                 :             :                 }
    1047                 :             :             }
    1048                 :             :           break;
    1049                 :       71644 :         case BIT_IOR_EXPR:
    1050                 :       71644 :           if (integer_all_onesp (oelast->op))
    1051                 :             :             {
    1052                 :           0 :               if (ops->length () != 1)
    1053                 :             :                 {
    1054                 :           0 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    1055                 :           0 :                     fprintf (dump_file, "Found | -1, removing all other ops\n");
    1056                 :             : 
    1057                 :           0 :                   reassociate_stats.ops_eliminated += ops->length () - 1;
    1058                 :             : 
    1059                 :           0 :                   ops->truncate (0);
    1060                 :           0 :                   ops->quick_push (oelast);
    1061                 :           0 :                   return;
    1062                 :             :                 }
    1063                 :             :             }
    1064                 :       71644 :           else if (integer_zerop (oelast->op))
    1065                 :             :             {
    1066                 :           6 :               if (ops->length () != 1)
    1067                 :             :                 {
    1068                 :           6 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    1069                 :           0 :                     fprintf (dump_file, "Found | 0, removing\n");
    1070                 :           6 :                   ops->pop ();
    1071                 :           6 :                   reassociate_stats.ops_eliminated++;
    1072                 :             :                 }
    1073                 :             :             }
    1074                 :             :           break;
    1075                 :      757271 :         case MULT_EXPR:
    1076                 :      757271 :           if (integer_zerop (oelast->op)
    1077                 :      757271 :               || (FLOAT_TYPE_P (type)
    1078                 :        1568 :                   && !HONOR_NANS (type)
    1079                 :        1325 :                   && !HONOR_SIGNED_ZEROS (type)
    1080                 :        1325 :                   && real_zerop (oelast->op)))
    1081                 :             :             {
    1082                 :           0 :               if (ops->length () != 1)
    1083                 :             :                 {
    1084                 :           0 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    1085                 :           0 :                     fprintf (dump_file, "Found * 0, removing all other ops\n");
    1086                 :             : 
    1087                 :           0 :                   reassociate_stats.ops_eliminated += ops->length () - 1;
    1088                 :           0 :                   ops->truncate (0);
    1089                 :           0 :                   ops->quick_push (oelast);
    1090                 :           0 :                   return;
    1091                 :             :                 }
    1092                 :             :             }
    1093                 :      757271 :           else if (integer_onep (oelast->op)
    1094                 :      757271 :                    || (FLOAT_TYPE_P (type)
    1095                 :        1568 :                        && !HONOR_SNANS (type)
    1096                 :        1568 :                        && real_onep (oelast->op)))
    1097                 :             :             {
    1098                 :           4 :               if (ops->length () != 1)
    1099                 :             :                 {
    1100                 :           4 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    1101                 :           0 :                     fprintf (dump_file, "Found * 1, removing\n");
    1102                 :           4 :                   ops->pop ();
    1103                 :           4 :                   reassociate_stats.ops_eliminated++;
    1104                 :           4 :                   return;
    1105                 :             :                 }
    1106                 :             :             }
    1107                 :             :           break;
    1108                 :     1412119 :         case BIT_XOR_EXPR:
    1109                 :     1412119 :         case PLUS_EXPR:
    1110                 :     1412119 :         case MINUS_EXPR:
    1111                 :     1412119 :           if (integer_zerop (oelast->op)
    1112                 :     1412119 :               || (FLOAT_TYPE_P (type)
    1113                 :         771 :                   && (opcode == PLUS_EXPR || opcode == MINUS_EXPR)
    1114                 :         771 :                   && fold_real_zero_addition_p (type, 0, oelast->op,
    1115                 :             :                                                 opcode == MINUS_EXPR)))
    1116                 :             :             {
    1117                 :        1997 :               if (ops->length () != 1)
    1118                 :             :                 {
    1119                 :        1997 :                   if (dump_file && (dump_flags & TDF_DETAILS))
    1120                 :           0 :                     fprintf (dump_file, "Found [|^+] 0, removing\n");
    1121                 :        1997 :                   ops->pop ();
    1122                 :        1997 :                   reassociate_stats.ops_eliminated++;
    1123                 :        1997 :                   return;
    1124                 :             :                 }
    1125                 :             :             }
    1126                 :             :           break;
    1127                 :             :         default:
    1128                 :             :           break;
    1129                 :             :         }
    1130                 :             :     }
    1131                 :             : }
    1132                 :             : 
    1133                 :             : 
    1134                 :             : static void linearize_expr_tree (vec<operand_entry *> *, gimple *,
    1135                 :             :                                  bool, bool);
    1136                 :             : 
    1137                 :             : /* Structure for tracking and counting operands.  */
    1138                 :             : struct oecount {
    1139                 :             :   unsigned int cnt;
    1140                 :             :   unsigned int id;
    1141                 :             :   enum tree_code oecode;
    1142                 :             :   tree op;
    1143                 :             : };
    1144                 :             : 
    1145                 :             : 
    1146                 :             : /* The heap for the oecount hashtable and the sorted list of operands.  */
    1147                 :             : static vec<oecount> cvec;
    1148                 :             : 
    1149                 :             : 
    1150                 :             : /* Oecount hashtable helpers.  */
    1151                 :             : 
    1152                 :             : struct oecount_hasher : int_hash <int, 0, 1>
    1153                 :             : {
    1154                 :             :   static inline hashval_t hash (int);
    1155                 :             :   static inline bool equal (int, int);
    1156                 :             : };
    1157                 :             : 
    1158                 :             : /* Hash function for oecount.  */
    1159                 :             : 
    1160                 :             : inline hashval_t
    1161                 :      145937 : oecount_hasher::hash (int p)
    1162                 :             : {
    1163                 :      145937 :   const oecount *c = &cvec[p - 42];
    1164                 :      145937 :   return htab_hash_pointer (c->op) ^ (hashval_t)c->oecode;
    1165                 :             : }
    1166                 :             : 
    1167                 :             : /* Comparison function for oecount.  */
    1168                 :             : 
    1169                 :             : inline bool
    1170                 :       78697 : oecount_hasher::equal (int p1, int p2)
    1171                 :             : {
    1172                 :       78697 :   const oecount *c1 = &cvec[p1 - 42];
    1173                 :       78697 :   const oecount *c2 = &cvec[p2 - 42];
    1174                 :       78697 :   return c1->oecode == c2->oecode && c1->op == c2->op;
    1175                 :             : }
    1176                 :             : 
    1177                 :             : /* Comparison function for qsort sorting oecount elements by count.  */
    1178                 :             : 
    1179                 :             : static int
    1180                 :      575971 : oecount_cmp (const void *p1, const void *p2)
    1181                 :             : {
    1182                 :      575971 :   const oecount *c1 = (const oecount *)p1;
    1183                 :      575971 :   const oecount *c2 = (const oecount *)p2;
    1184                 :      575971 :   if (c1->cnt != c2->cnt)
    1185                 :       11892 :     return c1->cnt > c2->cnt ? 1 : -1;
    1186                 :             :   else
    1187                 :             :     /* If counts are identical, use unique IDs to stabilize qsort.  */
    1188                 :      832377 :     return c1->id > c2->id ? 1 : -1;
    1189                 :             : }
    1190                 :             : 
    1191                 :             : /* Return TRUE iff STMT represents a builtin call that raises OP
    1192                 :             :    to some exponent.  */
    1193                 :             : 
    1194                 :             : static bool
    1195                 :         795 : stmt_is_power_of_op (gimple *stmt, tree op)
    1196                 :             : {
    1197                 :         795 :   if (!is_gimple_call (stmt))
    1198                 :             :     return false;
    1199                 :             : 
    1200                 :          11 :   switch (gimple_call_combined_fn (stmt))
    1201                 :             :     {
    1202                 :           6 :     CASE_CFN_POW:
    1203                 :           6 :     CASE_CFN_POWI:
    1204                 :           6 :       return (operand_equal_p (gimple_call_arg (stmt, 0), op, 0));
    1205                 :             :       
    1206                 :             :     default:
    1207                 :             :       return false;
    1208                 :             :     }
    1209                 :             : }
    1210                 :             : 
    1211                 :             : /* Given STMT which is a __builtin_pow* call, decrement its exponent
    1212                 :             :    in place and return the result.  Assumes that stmt_is_power_of_op
    1213                 :             :    was previously called for STMT and returned TRUE.  */
    1214                 :             : 
    1215                 :             : static HOST_WIDE_INT
    1216                 :           6 : decrement_power (gimple *stmt)
    1217                 :             : {
    1218                 :           6 :   REAL_VALUE_TYPE c, cint;
    1219                 :           6 :   HOST_WIDE_INT power;
    1220                 :           6 :   tree arg1;
    1221                 :             : 
    1222                 :           6 :   switch (gimple_call_combined_fn (stmt))
    1223                 :             :     {
    1224                 :           0 :     CASE_CFN_POW:
    1225                 :           0 :       arg1 = gimple_call_arg (stmt, 1);
    1226                 :           0 :       c = TREE_REAL_CST (arg1);
    1227                 :           0 :       power = real_to_integer (&c) - 1;
    1228                 :           0 :       real_from_integer (&cint, VOIDmode, power, SIGNED);
    1229                 :           0 :       gimple_call_set_arg (stmt, 1, build_real (TREE_TYPE (arg1), cint));
    1230                 :           0 :       return power;
    1231                 :             : 
    1232                 :           6 :     CASE_CFN_POWI:
    1233                 :           6 :       arg1 = gimple_call_arg (stmt, 1);
    1234                 :           6 :       power = TREE_INT_CST_LOW (arg1) - 1;
    1235                 :           6 :       gimple_call_set_arg (stmt, 1, build_int_cst (TREE_TYPE (arg1), power));
    1236                 :           6 :       return power;
    1237                 :             : 
    1238                 :           0 :     default:
    1239                 :           0 :       gcc_unreachable ();
    1240                 :             :     }
    1241                 :             : }
    1242                 :             : 
    1243                 :             : /* Replace SSA defined by STMT and replace all its uses with new
    1244                 :             :    SSA.  Also return the new SSA.  */
    1245                 :             : 
    1246                 :             : static tree
    1247                 :         237 : make_new_ssa_for_def (gimple *stmt, enum tree_code opcode, tree op)
    1248                 :             : {
    1249                 :         237 :   gimple *use_stmt;
    1250                 :         237 :   use_operand_p use;
    1251                 :         237 :   imm_use_iterator iter;
    1252                 :         237 :   tree new_lhs, new_debug_lhs = NULL_TREE;
    1253                 :         237 :   tree lhs = gimple_get_lhs (stmt);
    1254                 :             : 
    1255                 :         237 :   new_lhs = make_ssa_name (TREE_TYPE (lhs));
    1256                 :         237 :   gimple_set_lhs (stmt, new_lhs);
    1257                 :             : 
    1258                 :             :   /* Also need to update GIMPLE_DEBUGs.  */
    1259                 :         486 :   FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    1260                 :             :     {
    1261                 :         249 :       tree repl = new_lhs;
    1262                 :         249 :       if (is_gimple_debug (use_stmt))
    1263                 :             :         {
    1264                 :          12 :           if (new_debug_lhs == NULL_TREE)
    1265                 :             :             {
    1266                 :           6 :               new_debug_lhs = build_debug_expr_decl (TREE_TYPE (lhs));
    1267                 :           6 :               gdebug *def_temp
    1268                 :           6 :                 = gimple_build_debug_bind (new_debug_lhs,
    1269                 :           6 :                                            build2 (opcode, TREE_TYPE (lhs),
    1270                 :             :                                                    new_lhs, op),
    1271                 :             :                                            stmt);
    1272                 :           6 :               gimple_set_uid (def_temp, gimple_uid (stmt));
    1273                 :           6 :               gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    1274                 :           6 :               gsi_insert_after (&gsi, def_temp, GSI_SAME_STMT);
    1275                 :             :             }
    1276                 :             :           repl = new_debug_lhs;
    1277                 :             :         }
    1278                 :         747 :       FOR_EACH_IMM_USE_ON_STMT (use, iter)
    1279                 :         249 :         SET_USE (use, repl);
    1280                 :         249 :       update_stmt (use_stmt);
    1281                 :         237 :     }
    1282                 :         237 :   return new_lhs;
    1283                 :             : }
    1284                 :             : 
    1285                 :             : /* Replace all SSAs defined in STMTS_TO_FIX and replace its
    1286                 :             :    uses with new SSAs.  Also do this for the stmt that defines DEF
    1287                 :             :    if *DEF is not OP.  */
    1288                 :             : 
    1289                 :             : static void
    1290                 :         170 : make_new_ssa_for_all_defs (tree *def, enum tree_code opcode, tree op,
    1291                 :             :                            vec<gimple *> &stmts_to_fix)
    1292                 :             : {
    1293                 :         170 :   unsigned i;
    1294                 :         170 :   gimple *stmt;
    1295                 :             : 
    1296                 :         170 :   if (*def != op
    1297                 :         170 :       && TREE_CODE (*def) == SSA_NAME
    1298                 :         170 :       && (stmt = SSA_NAME_DEF_STMT (*def))
    1299                 :         340 :       && gimple_code (stmt) != GIMPLE_NOP)
    1300                 :         170 :     *def = make_new_ssa_for_def (stmt, opcode, op);
    1301                 :             : 
    1302                 :         237 :   FOR_EACH_VEC_ELT (stmts_to_fix, i, stmt)
    1303                 :          67 :     make_new_ssa_for_def (stmt, opcode, op);
    1304                 :         170 : }
    1305                 :             : 
    1306                 :             : /* Find the single immediate use of STMT's LHS, and replace it
    1307                 :             :    with OP.  Remove STMT.  If STMT's LHS is the same as *DEF,
    1308                 :             :    replace *DEF with OP as well.  */
    1309                 :             : 
    1310                 :             : static void
    1311                 :         534 : propagate_op_to_single_use (tree op, gimple *stmt, tree *def)
    1312                 :             : {
    1313                 :         534 :   tree lhs;
    1314                 :         534 :   gimple *use_stmt;
    1315                 :         534 :   use_operand_p use;
    1316                 :         534 :   gimple_stmt_iterator gsi;
    1317                 :             : 
    1318                 :         534 :   if (is_gimple_call (stmt))
    1319                 :           1 :     lhs = gimple_call_lhs (stmt);
    1320                 :             :   else
    1321                 :         533 :     lhs = gimple_assign_lhs (stmt);
    1322                 :             : 
    1323                 :         534 :   gcc_assert (has_single_use (lhs));
    1324                 :         534 :   single_imm_use (lhs, &use, &use_stmt);
    1325                 :         534 :   if (lhs == *def)
    1326                 :         373 :     *def = op;
    1327                 :         534 :   SET_USE (use, op);
    1328                 :         534 :   if (TREE_CODE (op) != SSA_NAME)
    1329                 :          39 :     update_stmt (use_stmt);
    1330                 :         534 :   gsi = gsi_for_stmt (stmt);
    1331                 :         534 :   unlink_stmt_vdef (stmt);
    1332                 :         534 :   reassoc_remove_stmt (&gsi);
    1333                 :         534 :   release_defs (stmt);
    1334                 :         534 : }
    1335                 :             : 
    1336                 :             : /* Walks the linear chain with result *DEF searching for an operation
    1337                 :             :    with operand OP and code OPCODE removing that from the chain.  *DEF
    1338                 :             :    is updated if there is only one operand but no operation left.  */
    1339                 :             : 
    1340                 :             : static void
    1341                 :         543 : zero_one_operation (tree *def, enum tree_code opcode, tree op)
    1342                 :             : {
    1343                 :         543 :   tree orig_def = *def;
    1344                 :         543 :   gimple *stmt = SSA_NAME_DEF_STMT (*def);
    1345                 :             :   /* PR72835 - Record the stmt chain that has to be updated such that
    1346                 :             :      we dont use the same LHS when the values computed are different.  */
    1347                 :         543 :   auto_vec<gimple *, 64> stmts_to_fix;
    1348                 :             : 
    1349                 :         999 :   do
    1350                 :             :     {
    1351                 :         771 :       tree name;
    1352                 :             : 
    1353                 :         771 :       if (opcode == MULT_EXPR)
    1354                 :             :         {
    1355                 :         769 :           if (stmt_is_power_of_op (stmt, op))
    1356                 :             :             {
    1357                 :           6 :               if (decrement_power (stmt) == 1)
    1358                 :             :                 {
    1359                 :           1 :                   if (stmts_to_fix.length () > 0)
    1360                 :           1 :                     stmts_to_fix.pop ();
    1361                 :           1 :                   propagate_op_to_single_use (op, stmt, def);
    1362                 :             :                 }
    1363                 :             :               break;
    1364                 :             :             }
    1365                 :         763 :           else if (gimple_assign_rhs_code (stmt) == NEGATE_EXPR)
    1366                 :             :             {
    1367                 :          15 :               if (gimple_assign_rhs1 (stmt) == op)
    1368                 :             :                 {
    1369                 :          11 :                   tree cst = build_minus_one_cst (TREE_TYPE (op));
    1370                 :          11 :                   if (stmts_to_fix.length () > 0)
    1371                 :          11 :                     stmts_to_fix.pop ();
    1372                 :          11 :                   propagate_op_to_single_use (cst, stmt, def);
    1373                 :          11 :                   break;
    1374                 :             :                 }
    1375                 :           4 :               else if (integer_minus_onep (op)
    1376                 :           4 :                        || real_minus_onep (op))
    1377                 :             :                 {
    1378                 :           4 :                   gimple_assign_set_rhs_code
    1379                 :           4 :                     (stmt, TREE_CODE (gimple_assign_rhs1 (stmt)));
    1380                 :           4 :                   break;
    1381                 :             :                 }
    1382                 :             :             }
    1383                 :             :         }
    1384                 :             : 
    1385                 :         750 :       name = gimple_assign_rhs1 (stmt);
    1386                 :             : 
    1387                 :             :       /* If this is the operation we look for and one of the operands
    1388                 :             :          is ours simply propagate the other operand into the stmts
    1389                 :             :          single use.  */
    1390                 :         750 :       if (gimple_assign_rhs_code (stmt) == opcode
    1391                 :         750 :           && (name == op
    1392                 :         628 :               || gimple_assign_rhs2 (stmt) == op))
    1393                 :             :         {
    1394                 :         522 :           if (name == op)
    1395                 :         122 :             name = gimple_assign_rhs2 (stmt);
    1396                 :         522 :           if (stmts_to_fix.length () > 0)
    1397                 :         149 :             stmts_to_fix.pop ();
    1398                 :         522 :           propagate_op_to_single_use (name, stmt, def);
    1399                 :         522 :           break;
    1400                 :             :         }
    1401                 :             : 
    1402                 :             :       /* We might have a multiply of two __builtin_pow* calls, and
    1403                 :             :          the operand might be hiding in the rightmost one.  Likewise
    1404                 :             :          this can happen for a negate.  */
    1405                 :         228 :       if (opcode == MULT_EXPR
    1406                 :         228 :           && gimple_assign_rhs_code (stmt) == opcode
    1407                 :         228 :           && TREE_CODE (gimple_assign_rhs2 (stmt)) == SSA_NAME
    1408                 :         386 :           && has_single_use (gimple_assign_rhs2 (stmt)))
    1409                 :             :         {
    1410                 :          26 :           gimple *stmt2 = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt));
    1411                 :          26 :           if (stmt_is_power_of_op (stmt2, op))
    1412                 :             :             {
    1413                 :           0 :               if (decrement_power (stmt2) == 1)
    1414                 :           0 :                 propagate_op_to_single_use (op, stmt2, def);
    1415                 :             :               else
    1416                 :           0 :                 stmts_to_fix.safe_push (stmt2);
    1417                 :           0 :               break;
    1418                 :             :             }
    1419                 :          26 :           else if (is_gimple_assign (stmt2)
    1420                 :          26 :                    && gimple_assign_rhs_code (stmt2) == NEGATE_EXPR)
    1421                 :             :             {
    1422                 :           0 :               if (gimple_assign_rhs1 (stmt2) == op)
    1423                 :             :                 {
    1424                 :           0 :                   tree cst = build_minus_one_cst (TREE_TYPE (op));
    1425                 :           0 :                   propagate_op_to_single_use (cst, stmt2, def);
    1426                 :           0 :                   break;
    1427                 :             :                 }
    1428                 :           0 :               else if (integer_minus_onep (op)
    1429                 :           0 :                        || real_minus_onep (op))
    1430                 :             :                 {
    1431                 :           0 :                   stmts_to_fix.safe_push (stmt2);
    1432                 :           0 :                   gimple_assign_set_rhs_code
    1433                 :           0 :                     (stmt2, TREE_CODE (gimple_assign_rhs1 (stmt2)));
    1434                 :           0 :                   break;
    1435                 :             :                 }
    1436                 :             :             }
    1437                 :             :         }
    1438                 :             : 
    1439                 :             :       /* Continue walking the chain.  */
    1440                 :         228 :       gcc_assert (name != op
    1441                 :             :                   && TREE_CODE (name) == SSA_NAME);
    1442                 :         228 :       stmt = SSA_NAME_DEF_STMT (name);
    1443                 :         228 :       stmts_to_fix.safe_push (stmt);
    1444                 :         228 :     }
    1445                 :             :   while (1);
    1446                 :             : 
    1447                 :         543 :   if (stmts_to_fix.length () > 0 || *def == orig_def)
    1448                 :         170 :     make_new_ssa_for_all_defs (def, opcode, op, stmts_to_fix);
    1449                 :         543 : }
    1450                 :             : 
    1451                 :             : /* Returns true if statement S1 dominates statement S2.  Like
    1452                 :             :    stmt_dominates_stmt_p, but uses stmt UIDs to optimize.  */
    1453                 :             : 
    1454                 :             : static bool
    1455                 :     5902349 : reassoc_stmt_dominates_stmt_p (gimple *s1, gimple *s2)
    1456                 :             : {
    1457                 :     5902349 :   basic_block bb1 = gimple_bb (s1), bb2 = gimple_bb (s2);
    1458                 :             : 
    1459                 :             :   /* If bb1 is NULL, it should be a GIMPLE_NOP def stmt of an (D)
    1460                 :             :      SSA_NAME.  Assume it lives at the beginning of function and
    1461                 :             :      thus dominates everything.  */
    1462                 :     5902349 :   if (!bb1 || s1 == s2)
    1463                 :             :     return true;
    1464                 :             : 
    1465                 :             :   /* If bb2 is NULL, it doesn't dominate any stmt with a bb.  */
    1466                 :     5899333 :   if (!bb2)
    1467                 :             :     return false;
    1468                 :             : 
    1469                 :     5880109 :   if (bb1 == bb2)
    1470                 :             :     {
    1471                 :             :       /* PHIs in the same basic block are assumed to be
    1472                 :             :          executed all in parallel, if only one stmt is a PHI,
    1473                 :             :          it dominates the other stmt in the same basic block.  */
    1474                 :     5647599 :       if (gimple_code (s1) == GIMPLE_PHI)
    1475                 :             :         return true;
    1476                 :             : 
    1477                 :     5529697 :       if (gimple_code (s2) == GIMPLE_PHI)
    1478                 :             :         return false;
    1479                 :             : 
    1480                 :     5457326 :       gcc_assert (gimple_uid (s1) && gimple_uid (s2));
    1481                 :             : 
    1482                 :     5457326 :       if (gimple_uid (s1) < gimple_uid (s2))
    1483                 :             :         return true;
    1484                 :             : 
    1485                 :     3341151 :       if (gimple_uid (s1) > gimple_uid (s2))
    1486                 :             :         return false;
    1487                 :             : 
    1488                 :       41791 :       gimple_stmt_iterator gsi = gsi_for_stmt (s1);
    1489                 :       41791 :       unsigned int uid = gimple_uid (s1);
    1490                 :      121216 :       for (gsi_next (&gsi); !gsi_end_p (gsi); gsi_next (&gsi))
    1491                 :             :         {
    1492                 :      117750 :           gimple *s = gsi_stmt (gsi);
    1493                 :      117750 :           if (gimple_uid (s) != uid)
    1494                 :             :             break;
    1495                 :       82738 :           if (s == s2)
    1496                 :             :             return true;
    1497                 :             :         }
    1498                 :             : 
    1499                 :       38478 :       return false;
    1500                 :             :     }
    1501                 :             : 
    1502                 :      232510 :   return dominated_by_p (CDI_DOMINATORS, bb2, bb1);
    1503                 :             : }
    1504                 :             : 
    1505                 :             : /* Insert STMT after INSERT_POINT.  */
    1506                 :             : 
    1507                 :             : static void
    1508                 :       33645 : insert_stmt_after (gimple *stmt, gimple *insert_point)
    1509                 :             : {
    1510                 :       33645 :   gimple_stmt_iterator gsi;
    1511                 :       33645 :   basic_block bb;
    1512                 :             : 
    1513                 :       33645 :   if (gimple_code (insert_point) == GIMPLE_PHI)
    1514                 :          24 :     bb = gimple_bb (insert_point);
    1515                 :       33621 :   else if (!stmt_ends_bb_p (insert_point))
    1516                 :             :     {
    1517                 :       33611 :       gsi = gsi_for_stmt (insert_point);
    1518                 :       33611 :       gimple_set_uid (stmt, gimple_uid (insert_point));
    1519                 :       33611 :       gsi_insert_after (&gsi, stmt, GSI_NEW_STMT);
    1520                 :       33611 :       return;
    1521                 :             :     }
    1522                 :          10 :   else if (gimple_code (insert_point) == GIMPLE_ASM
    1523                 :          10 :            && gimple_asm_nlabels (as_a <gasm *> (insert_point)) != 0)
    1524                 :             :     /* We have no idea where to insert - it depends on where the
    1525                 :             :        uses will be placed.  */
    1526                 :           0 :     gcc_unreachable ();
    1527                 :             :   else
    1528                 :             :     /* We assume INSERT_POINT is a SSA_NAME_DEF_STMT of some SSA_NAME,
    1529                 :             :        thus if it must end a basic block, it should be a call that can
    1530                 :             :        throw, or some assignment that can throw.  If it throws, the LHS
    1531                 :             :        of it will not be initialized though, so only valid places using
    1532                 :             :        the SSA_NAME should be dominated by the fallthru edge.  */
    1533                 :          10 :     bb = find_fallthru_edge (gimple_bb (insert_point)->succs)->dest;
    1534                 :          34 :   gsi = gsi_after_labels (bb);
    1535                 :          34 :   if (gsi_end_p (gsi))
    1536                 :             :     {
    1537                 :           0 :       gimple_stmt_iterator gsi2 = gsi_last_bb (bb);
    1538                 :           0 :       gimple_set_uid (stmt,
    1539                 :           0 :                       gsi_end_p (gsi2) ? 1 : gimple_uid (gsi_stmt (gsi2)));
    1540                 :             :     }
    1541                 :             :   else
    1542                 :          34 :     gimple_set_uid (stmt, gimple_uid (gsi_stmt (gsi)));
    1543                 :          34 :   gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
    1544                 :             : }
    1545                 :             : 
    1546                 :             : /* Builds one statement performing OP1 OPCODE OP2 using TMPVAR for
    1547                 :             :    the result.  Places the statement after the definition of either
    1548                 :             :    OP1 or OP2.  Returns the new statement.  */
    1549                 :             : 
    1550                 :             : static gimple *
    1551                 :        6652 : build_and_add_sum (tree type, tree op1, tree op2, enum tree_code opcode)
    1552                 :             : {
    1553                 :        6652 :   gimple *op1def = NULL, *op2def = NULL;
    1554                 :        6652 :   gimple_stmt_iterator gsi;
    1555                 :        6652 :   tree op;
    1556                 :        6652 :   gassign *sum;
    1557                 :             : 
    1558                 :             :   /* Create the addition statement.  */
    1559                 :        6652 :   op = make_ssa_name (type);
    1560                 :        6652 :   sum = gimple_build_assign (op, opcode, op1, op2);
    1561                 :             : 
    1562                 :             :   /* Find an insertion place and insert.  */
    1563                 :        6652 :   if (TREE_CODE (op1) == SSA_NAME)
    1564                 :        6652 :     op1def = SSA_NAME_DEF_STMT (op1);
    1565                 :        6652 :   if (TREE_CODE (op2) == SSA_NAME)
    1566                 :        6376 :     op2def = SSA_NAME_DEF_STMT (op2);
    1567                 :        6652 :   if ((!op1def || gimple_nop_p (op1def))
    1568                 :        6731 :       && (!op2def || gimple_nop_p (op2def)))
    1569                 :             :     {
    1570                 :          79 :       gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
    1571                 :          79 :       if (!gsi_end_p (gsi)
    1572                 :          79 :           && is_gimple_call (gsi_stmt (gsi))
    1573                 :          85 :           && (gimple_call_flags (gsi_stmt (gsi)) & ECF_RETURNS_TWICE))
    1574                 :             :         {
    1575                 :             :           /* Don't add statements before a returns_twice call at the start
    1576                 :             :              of a function.  */
    1577                 :           1 :           split_edge (single_succ_edge (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
    1578                 :           1 :           gsi = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
    1579                 :             :         }
    1580                 :          79 :       if (gsi_end_p (gsi))
    1581                 :             :         {
    1582                 :           1 :           gimple_stmt_iterator gsi2
    1583                 :           1 :             = gsi_last_bb (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
    1584                 :           1 :           gimple_set_uid (sum,
    1585                 :           1 :                           gsi_end_p (gsi2) ? 1 : gimple_uid (gsi_stmt (gsi2)));
    1586                 :             :         }
    1587                 :             :       else
    1588                 :          78 :         gimple_set_uid (sum, gimple_uid (gsi_stmt (gsi)));
    1589                 :          79 :       gsi_insert_before (&gsi, sum, GSI_NEW_STMT);
    1590                 :             :     }
    1591                 :             :   else
    1592                 :             :     {
    1593                 :        6573 :       gimple *insert_point;
    1594                 :        6573 :       if ((!op1def || gimple_nop_p (op1def))
    1595                 :       13146 :            || (op2def && !gimple_nop_p (op2def)
    1596                 :        6269 :                && reassoc_stmt_dominates_stmt_p (op1def, op2def)))
    1597                 :             :         insert_point = op2def;
    1598                 :             :       else
    1599                 :             :         insert_point = op1def;
    1600                 :        6573 :       insert_stmt_after (sum, insert_point);
    1601                 :             :     }
    1602                 :        6652 :   update_stmt (sum);
    1603                 :             : 
    1604                 :        6652 :   return sum;
    1605                 :             : }
    1606                 :             : 
    1607                 :             : /* Perform un-distribution of divisions and multiplications.
    1608                 :             :    A * X + B * X is transformed into (A + B) * X and A / X + B / X
    1609                 :             :    to (A + B) / X for real X.
    1610                 :             : 
    1611                 :             :    The algorithm is organized as follows.
    1612                 :             : 
    1613                 :             :     - First we walk the addition chain *OPS looking for summands that
    1614                 :             :       are defined by a multiplication or a real division.  This results
    1615                 :             :       in the candidates bitmap with relevant indices into *OPS.
    1616                 :             : 
    1617                 :             :     - Second we build the chains of multiplications or divisions for
    1618                 :             :       these candidates, counting the number of occurrences of (operand, code)
    1619                 :             :       pairs in all of the candidates chains.
    1620                 :             : 
    1621                 :             :     - Third we sort the (operand, code) pairs by number of occurrence and
    1622                 :             :       process them starting with the pair with the most uses.
    1623                 :             : 
    1624                 :             :       * For each such pair we walk the candidates again to build a
    1625                 :             :         second candidate bitmap noting all multiplication/division chains
    1626                 :             :         that have at least one occurrence of (operand, code).
    1627                 :             : 
    1628                 :             :       * We build an alternate addition chain only covering these
    1629                 :             :         candidates with one (operand, code) operation removed from their
    1630                 :             :         multiplication/division chain.
    1631                 :             : 
    1632                 :             :       * The first candidate gets replaced by the alternate addition chain
    1633                 :             :         multiplied/divided by the operand.
    1634                 :             : 
    1635                 :             :       * All candidate chains get disabled for further processing and
    1636                 :             :         processing of (operand, code) pairs continues.
    1637                 :             : 
    1638                 :             :   The alternate addition chains built are re-processed by the main
    1639                 :             :   reassociation algorithm which allows optimizing a * x * y + b * y * x
    1640                 :             :   to (a + b ) * x * y in one invocation of the reassociation pass.  */
    1641                 :             : 
    1642                 :             : static bool
    1643                 :     3833718 : undistribute_ops_list (enum tree_code opcode,
    1644                 :             :                        vec<operand_entry *> *ops, class loop *loop)
    1645                 :             : {
    1646                 :     3833718 :   unsigned int length = ops->length ();
    1647                 :     3833718 :   operand_entry *oe1;
    1648                 :     3833718 :   unsigned i, j;
    1649                 :     3833718 :   unsigned nr_candidates, nr_candidates2;
    1650                 :     3833718 :   sbitmap_iterator sbi0;
    1651                 :     3833718 :   vec<operand_entry *> *subops;
    1652                 :     3833718 :   bool changed = false;
    1653                 :     3833718 :   unsigned int next_oecount_id = 0;
    1654                 :             : 
    1655                 :     3833718 :   if (length <= 1
    1656                 :     3833718 :       || opcode != PLUS_EXPR)
    1657                 :             :     return false;
    1658                 :             : 
    1659                 :             :   /* Build a list of candidates to process.  */
    1660                 :     1849293 :   auto_sbitmap candidates (length);
    1661                 :     1849293 :   bitmap_clear (candidates);
    1662                 :     1849293 :   nr_candidates = 0;
    1663                 :     5702875 :   FOR_EACH_VEC_ELT (*ops, i, oe1)
    1664                 :             :     {
    1665                 :     3853582 :       enum tree_code dcode;
    1666                 :     3853582 :       gimple *oe1def;
    1667                 :             : 
    1668                 :     3853582 :       if (TREE_CODE (oe1->op) != SSA_NAME)
    1669                 :     1401526 :         continue;
    1670                 :     2452056 :       oe1def = SSA_NAME_DEF_STMT (oe1->op);
    1671                 :     2452056 :       if (!is_gimple_assign (oe1def))
    1672                 :      908112 :         continue;
    1673                 :     1543944 :       dcode = gimple_assign_rhs_code (oe1def);
    1674                 :     2907529 :       if ((dcode != MULT_EXPR
    1675                 :     1543944 :            && dcode != RDIV_EXPR)
    1676                 :     1543944 :           || !is_reassociable_op (oe1def, dcode, loop))
    1677                 :     1363585 :         continue;
    1678                 :             : 
    1679                 :      180359 :       bitmap_set_bit (candidates, i);
    1680                 :      180359 :       nr_candidates++;
    1681                 :             :     }
    1682                 :             : 
    1683                 :     1849293 :   if (nr_candidates < 2)
    1684                 :             :     return false;
    1685                 :             : 
    1686                 :       15337 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1687                 :             :     {
    1688                 :           1 :       fprintf (dump_file, "searching for un-distribute opportunities ");
    1689                 :           2 :       print_generic_expr (dump_file,
    1690                 :           1 :         (*ops)[bitmap_first_set_bit (candidates)]->op, TDF_NONE);
    1691                 :           1 :       fprintf (dump_file, " %d\n", nr_candidates);
    1692                 :             :     }
    1693                 :             : 
    1694                 :             :   /* Build linearized sub-operand lists and the counting table.  */
    1695                 :       15337 :   cvec.create (0);
    1696                 :             : 
    1697                 :       15337 :   hash_table<oecount_hasher> ctable (15);
    1698                 :             : 
    1699                 :             :   /* ??? Macro arguments cannot have multi-argument template types in
    1700                 :             :      them.  This typedef is needed to workaround that limitation.  */
    1701                 :       15337 :   typedef vec<operand_entry *> vec_operand_entry_t_heap;
    1702                 :       30674 :   subops = XCNEWVEC (vec_operand_entry_t_heap, ops->length ());
    1703                 :       68385 :   EXECUTE_IF_SET_IN_BITMAP (candidates, 0, i, sbi0)
    1704                 :             :     {
    1705                 :       37711 :       gimple *oedef;
    1706                 :       37711 :       enum tree_code oecode;
    1707                 :       37711 :       unsigned j;
    1708                 :             : 
    1709                 :       37711 :       oedef = SSA_NAME_DEF_STMT ((*ops)[i]->op);
    1710                 :       37711 :       oecode = gimple_assign_rhs_code (oedef);
    1711                 :       75422 :       linearize_expr_tree (&subops[i], oedef,
    1712                 :       37711 :                            associative_tree_code (oecode), false);
    1713                 :             : 
    1714                 :      151534 :       FOR_EACH_VEC_ELT (subops[i], j, oe1)
    1715                 :             :         {
    1716                 :       76112 :           oecount c;
    1717                 :       76112 :           int *slot;
    1718                 :       76112 :           int idx;
    1719                 :       76112 :           c.oecode = oecode;
    1720                 :       76112 :           c.cnt = 1;
    1721                 :       76112 :           c.id = next_oecount_id++;
    1722                 :       76112 :           c.op = oe1->op;
    1723                 :       76112 :           cvec.safe_push (c);
    1724                 :       76112 :           idx = cvec.length () + 41;
    1725                 :       76112 :           slot = ctable.find_slot (idx, INSERT);
    1726                 :       76112 :           if (!*slot)
    1727                 :             :             {
    1728                 :       75274 :               *slot = idx;
    1729                 :             :             }
    1730                 :             :           else
    1731                 :             :             {
    1732                 :         838 :               cvec.pop ();
    1733                 :         838 :               cvec[*slot - 42].cnt++;
    1734                 :             :             }
    1735                 :             :         }
    1736                 :             :     }
    1737                 :             : 
    1738                 :             :   /* Sort the counting table.  */
    1739                 :       15337 :   cvec.qsort (oecount_cmp);
    1740                 :             : 
    1741                 :       15337 :   if (dump_file && (dump_flags & TDF_DETAILS))
    1742                 :             :     {
    1743                 :           1 :       oecount *c;
    1744                 :           1 :       fprintf (dump_file, "Candidates:\n");
    1745                 :           5 :       FOR_EACH_VEC_ELT (cvec, j, c)
    1746                 :             :         {
    1747                 :           3 :           fprintf (dump_file, "  %u %s: ", c->cnt,
    1748                 :           3 :                    c->oecode == MULT_EXPR
    1749                 :             :                    ? "*" : c->oecode == RDIV_EXPR ? "/" : "?");
    1750                 :           3 :           print_generic_expr (dump_file, c->op);
    1751                 :           3 :           fprintf (dump_file, "\n");
    1752                 :             :         }
    1753                 :             :     }
    1754                 :             : 
    1755                 :             :   /* Process the (operand, code) pairs in order of most occurrence.  */
    1756                 :       15337 :   auto_sbitmap candidates2 (length);
    1757                 :       15865 :   while (!cvec.is_empty ())
    1758                 :             :     {
    1759                 :       15800 :       oecount *c = &cvec.last ();
    1760                 :       15800 :       if (c->cnt < 2)
    1761                 :             :         break;
    1762                 :             : 
    1763                 :             :       /* Now collect the operands in the outer chain that contain
    1764                 :             :          the common operand in their inner chain.  */
    1765                 :         528 :       bitmap_clear (candidates2);
    1766                 :         528 :       nr_candidates2 = 0;
    1767                 :        4026 :       EXECUTE_IF_SET_IN_BITMAP (candidates, 0, i, sbi0)
    1768                 :             :         {
    1769                 :        2970 :           gimple *oedef;
    1770                 :        2970 :           enum tree_code oecode;
    1771                 :        2970 :           unsigned j;
    1772                 :        2970 :           tree op = (*ops)[i]->op;
    1773                 :             : 
    1774                 :             :           /* If we undistributed in this chain already this may be
    1775                 :             :              a constant.  */
    1776                 :        2970 :           if (TREE_CODE (op) != SSA_NAME)
    1777                 :         775 :             continue;
    1778                 :             : 
    1779                 :        2195 :           oedef = SSA_NAME_DEF_STMT (op);
    1780                 :        2195 :           oecode = gimple_assign_rhs_code (oedef);
    1781                 :        2195 :           if (oecode != c->oecode)
    1782                 :           0 :             continue;
    1783                 :             : 
    1784                 :        8404 :           FOR_EACH_VEC_ELT (subops[i], j, oe1)
    1785                 :             :             {
    1786                 :        4102 :               if (oe1->op == c->op)
    1787                 :             :                 {
    1788                 :         863 :                   bitmap_set_bit (candidates2, i);
    1789                 :         863 :                   ++nr_candidates2;
    1790                 :         863 :                   break;
    1791                 :             :                 }
    1792                 :             :             }
    1793                 :             :         }
    1794                 :             : 
    1795                 :         528 :       if (nr_candidates2 >= 2)
    1796                 :             :         {
    1797                 :         159 :           operand_entry *oe1, *oe2;
    1798                 :         159 :           gimple *prod;
    1799                 :         159 :           int first = bitmap_first_set_bit (candidates2);
    1800                 :             : 
    1801                 :             :           /* Build the new addition chain.  */
    1802                 :         159 :           oe1 = (*ops)[first];
    1803                 :         159 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1804                 :             :             {
    1805                 :           0 :               fprintf (dump_file, "Building (");
    1806                 :           0 :               print_generic_expr (dump_file, oe1->op);
    1807                 :             :             }
    1808                 :         159 :           zero_one_operation (&oe1->op, c->oecode, c->op);
    1809                 :         543 :           EXECUTE_IF_SET_IN_BITMAP (candidates2, first+1, i, sbi0)
    1810                 :             :             {
    1811                 :         384 :               gimple *sum;
    1812                 :         384 :               oe2 = (*ops)[i];
    1813                 :         384 :               if (dump_file && (dump_flags & TDF_DETAILS))
    1814                 :             :                 {
    1815                 :           0 :                   fprintf (dump_file, " + ");
    1816                 :           0 :                   print_generic_expr (dump_file, oe2->op);
    1817                 :             :                 }
    1818                 :         384 :               zero_one_operation (&oe2->op, c->oecode, c->op);
    1819                 :         384 :               sum = build_and_add_sum (TREE_TYPE (oe1->op),
    1820                 :             :                                        oe1->op, oe2->op, opcode);
    1821                 :         384 :               oe2->op = build_zero_cst (TREE_TYPE (oe2->op));
    1822                 :         384 :               oe2->rank = 0;
    1823                 :         384 :               oe1->op = gimple_get_lhs (sum);
    1824                 :             :             }
    1825                 :             : 
    1826                 :             :           /* Apply the multiplication/division.  */
    1827                 :         159 :           prod = build_and_add_sum (TREE_TYPE (oe1->op),
    1828                 :             :                                     oe1->op, c->op, c->oecode);
    1829                 :         159 :           if (dump_file && (dump_flags & TDF_DETAILS))
    1830                 :             :             {
    1831                 :           0 :               fprintf (dump_file, ") %s ", c->oecode == MULT_EXPR ? "*" : "/");
    1832                 :           0 :               print_generic_expr (dump_file, c->op);
    1833                 :           0 :               fprintf (dump_file, "\n");
    1834                 :             :             }
    1835                 :             : 
    1836                 :             :           /* Record it in the addition chain and disable further
    1837                 :             :              undistribution with this op.  */
    1838                 :         159 :           oe1->op = gimple_assign_lhs (prod);
    1839                 :         159 :           oe1->rank = get_rank (oe1->op);
    1840                 :         159 :           subops[first].release ();
    1841                 :             : 
    1842                 :         159 :           changed = true;
    1843                 :             :         }
    1844                 :             : 
    1845                 :         528 :       cvec.pop ();
    1846                 :             :     }
    1847                 :             : 
    1848                 :      129498 :   for (i = 0; i < ops->length (); ++i)
    1849                 :       49412 :     subops[i].release ();
    1850                 :       15337 :   free (subops);
    1851                 :       15337 :   cvec.release ();
    1852                 :             : 
    1853                 :       15337 :   return changed;
    1854                 :     1849293 : }
    1855                 :             : 
    1856                 :             : /* Pair to hold the information of one specific VECTOR_TYPE SSA_NAME:
    1857                 :             :    first: element index for each relevant BIT_FIELD_REF.
    1858                 :             :    second: the index of vec ops* for each relevant BIT_FIELD_REF.  */
    1859                 :             : typedef std::pair<unsigned, unsigned> v_info_elem;
    1860                 :        6360 : struct v_info {
    1861                 :             :   tree vec_type;
    1862                 :             :   auto_vec<v_info_elem, 32> vec;
    1863                 :             : };
    1864                 :             : typedef v_info *v_info_ptr;
    1865                 :             : 
    1866                 :             : /* Comparison function for qsort on VECTOR SSA_NAME trees by machine mode.  */
    1867                 :             : static int
    1868                 :       10560 : sort_by_mach_mode (const void *p_i, const void *p_j)
    1869                 :             : {
    1870                 :       10560 :   const tree tr1 = *((const tree *) p_i);
    1871                 :       10560 :   const tree tr2 = *((const tree *) p_j);
    1872                 :       10560 :   unsigned int mode1 = TYPE_MODE (TREE_TYPE (tr1));
    1873                 :       10560 :   unsigned int mode2 = TYPE_MODE (TREE_TYPE (tr2));
    1874                 :       10560 :   if (mode1 > mode2)
    1875                 :             :     return 1;
    1876                 :       10522 :   else if (mode1 < mode2)
    1877                 :             :     return -1;
    1878                 :       10475 :   if (SSA_NAME_VERSION (tr1) < SSA_NAME_VERSION (tr2))
    1879                 :             :     return -1;
    1880                 :        5102 :   else if (SSA_NAME_VERSION (tr1) > SSA_NAME_VERSION (tr2))
    1881                 :        5102 :     return 1;
    1882                 :             :   return 0;
    1883                 :             : }
    1884                 :             : 
    1885                 :             : /* Cleanup hash map for VECTOR information.  */
    1886                 :             : static void
    1887                 :     3701136 : cleanup_vinfo_map (hash_map<tree, v_info_ptr> &info_map)
    1888                 :             : {
    1889                 :     3707496 :   for (hash_map<tree, v_info_ptr>::iterator it = info_map.begin ();
    1890                 :     3713856 :        it != info_map.end (); ++it)
    1891                 :             :     {
    1892                 :        6360 :       v_info_ptr info = (*it).second;
    1893                 :        6360 :       delete info;
    1894                 :        6360 :       (*it).second = NULL;
    1895                 :             :     }
    1896                 :     3701136 : }
    1897                 :             : 
    1898                 :             : /* Perform un-distribution of BIT_FIELD_REF on VECTOR_TYPE.
    1899                 :             :      V1[0] + V1[1] + ... + V1[k] + V2[0] + V2[1] + ... + V2[k] + ... Vn[k]
    1900                 :             :    is transformed to
    1901                 :             :      Vs = (V1 + V2 + ... + Vn)
    1902                 :             :      Vs[0] + Vs[1] + ... + Vs[k]
    1903                 :             : 
    1904                 :             :    The basic steps are listed below:
    1905                 :             : 
    1906                 :             :     1) Check the addition chain *OPS by looking those summands coming from
    1907                 :             :        VECTOR bit_field_ref on VECTOR type.  Put the information into
    1908                 :             :        v_info_map for each satisfied summand, using VECTOR SSA_NAME as key.
    1909                 :             : 
    1910                 :             :     2) For each key (VECTOR SSA_NAME), validate all its BIT_FIELD_REFs are
    1911                 :             :        continuous, they can cover the whole VECTOR perfectly without any holes.
    1912                 :             :        Obtain one VECTOR list which contain candidates to be transformed.
    1913                 :             : 
    1914                 :             :     3) Sort the VECTOR list by machine mode of VECTOR type, for each group of
    1915                 :             :        candidates with same mode, build the addition statements for them and
    1916                 :             :        generate BIT_FIELD_REFs accordingly.
    1917                 :             : 
    1918                 :             :    TODO:
    1919                 :             :        The current implementation requires the whole VECTORs should be fully
    1920                 :             :        covered, but it can be extended to support partial, checking adjacent
    1921                 :             :        but not fill the whole, it may need some cost model to define the
    1922                 :             :        boundary to do or not.
    1923                 :             : */
    1924                 :             : static bool
    1925                 :     3833718 : undistribute_bitref_for_vector (enum tree_code opcode,
    1926                 :             :                                 vec<operand_entry *> *ops, struct loop *loop)
    1927                 :             : {
    1928                 :     3833718 :   if (ops->length () <= 1)
    1929                 :             :     return false;
    1930                 :             : 
    1931                 :     3830798 :   if (opcode != PLUS_EXPR
    1932                 :     3830798 :       && opcode != MULT_EXPR
    1933                 :             :       && opcode != BIT_XOR_EXPR
    1934                 :     1077477 :       && opcode != BIT_IOR_EXPR
    1935                 :      730542 :       && opcode != BIT_AND_EXPR)
    1936                 :             :     return false;
    1937                 :             : 
    1938                 :     3701136 :   hash_map<tree, v_info_ptr> v_info_map;
    1939                 :     3701136 :   operand_entry *oe1;
    1940                 :     3701136 :   unsigned i;
    1941                 :             : 
    1942                 :             :   /* Find those summands from VECTOR BIT_FIELD_REF in addition chain, put the
    1943                 :             :      information into map.  */
    1944                 :    11323715 :   FOR_EACH_VEC_ELT (*ops, i, oe1)
    1945                 :             :     {
    1946                 :     7622579 :       enum tree_code dcode;
    1947                 :     7622579 :       gimple *oe1def;
    1948                 :             : 
    1949                 :     7622579 :       if (TREE_CODE (oe1->op) != SSA_NAME)
    1950                 :     7614624 :         continue;
    1951                 :     4980206 :       oe1def = SSA_NAME_DEF_STMT (oe1->op);
    1952                 :     4980206 :       if (!is_gimple_assign (oe1def))
    1953                 :     1256484 :         continue;
    1954                 :     3723722 :       dcode = gimple_assign_rhs_code (oe1def);
    1955                 :     3723722 :       if (dcode != BIT_FIELD_REF || !is_reassociable_op (oe1def, dcode, loop))
    1956                 :     3673835 :         continue;
    1957                 :             : 
    1958                 :       49887 :       tree rhs = gimple_assign_rhs1 (oe1def);
    1959                 :       49887 :       tree vec = TREE_OPERAND (rhs, 0);
    1960                 :       49887 :       tree vec_type = TREE_TYPE (vec);
    1961                 :             : 
    1962                 :       49887 :       if (TREE_CODE (vec) != SSA_NAME || !VECTOR_TYPE_P (vec_type))
    1963                 :       31731 :         continue;
    1964                 :             : 
    1965                 :             :       /* Ignore it if target machine can't support this VECTOR type.  */
    1966                 :       18156 :       if (!VECTOR_MODE_P (TYPE_MODE (vec_type)))
    1967                 :        4754 :         continue;
    1968                 :             : 
    1969                 :             :       /* Check const vector type, constrain BIT_FIELD_REF offset and size.  */
    1970                 :       13402 :       if (!TYPE_VECTOR_SUBPARTS (vec_type).is_constant ())
    1971                 :           0 :         continue;
    1972                 :             : 
    1973                 :       13402 :       if (VECTOR_TYPE_P (TREE_TYPE (rhs))
    1974                 :       13402 :           || !is_a <scalar_mode> (TYPE_MODE (TREE_TYPE (rhs))))
    1975                 :        4947 :         continue;
    1976                 :             : 
    1977                 :             :       /* The type of BIT_FIELD_REF might not be equal to the element type of
    1978                 :             :          the vector.  We want to use a vector type with element type the
    1979                 :             :          same as the BIT_FIELD_REF and size the same as TREE_TYPE (vec).  */
    1980                 :        8455 :       if (!useless_type_conversion_p (TREE_TYPE (rhs), TREE_TYPE (vec_type)))
    1981                 :             :         {
    1982                 :        1293 :           machine_mode simd_mode;
    1983                 :        1293 :           unsigned HOST_WIDE_INT size, nunits;
    1984                 :        1293 :           unsigned HOST_WIDE_INT elem_size
    1985                 :        1293 :             = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
    1986                 :        2586 :           if (!GET_MODE_BITSIZE (TYPE_MODE (vec_type)).is_constant (&size))
    1987                 :           0 :             continue;
    1988                 :        1293 :           if (size <= elem_size || (size % elem_size) != 0)
    1989                 :           0 :             continue;
    1990                 :        1293 :           nunits = size / elem_size;
    1991                 :        2586 :           if (!mode_for_vector (SCALAR_TYPE_MODE (TREE_TYPE (rhs)),
    1992                 :        1293 :                                 nunits).exists (&simd_mode))
    1993                 :           0 :             continue;
    1994                 :        1293 :           vec_type = build_vector_type_for_mode (TREE_TYPE (rhs), simd_mode);
    1995                 :             : 
    1996                 :             :           /* Ignore it if target machine can't support this VECTOR type.  */
    1997                 :        1293 :           if (!VECTOR_MODE_P (TYPE_MODE (vec_type)))
    1998                 :           0 :             continue;
    1999                 :             : 
    2000                 :             :           /* Check const vector type, constrain BIT_FIELD_REF offset and
    2001                 :             :              size.  */
    2002                 :        1293 :           if (!TYPE_VECTOR_SUBPARTS (vec_type).is_constant ())
    2003                 :           0 :             continue;
    2004                 :             : 
    2005                 :        1293 :           if (maybe_ne (GET_MODE_SIZE (TYPE_MODE (vec_type)),
    2006                 :        3879 :                         GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (vec)))))
    2007                 :           0 :             continue;
    2008                 :             :         }
    2009                 :             : 
    2010                 :        8455 :       tree elem_type = TREE_TYPE (vec_type);
    2011                 :        8455 :       unsigned HOST_WIDE_INT elem_size = tree_to_uhwi (TYPE_SIZE (elem_type));
    2012                 :        8455 :       if (maybe_ne (bit_field_size (rhs), elem_size))
    2013                 :           0 :         continue;
    2014                 :             : 
    2015                 :        8455 :       unsigned idx;
    2016                 :        8455 :       if (!constant_multiple_p (bit_field_offset (rhs), elem_size, &idx))
    2017                 :           0 :         continue;
    2018                 :             : 
    2019                 :             :       /* Ignore it if target machine can't support this type of VECTOR
    2020                 :             :          operation.  */
    2021                 :        8455 :       optab op_tab = optab_for_tree_code (opcode, vec_type, optab_vector);
    2022                 :        8455 :       if (optab_handler (op_tab, TYPE_MODE (vec_type)) == CODE_FOR_nothing)
    2023                 :         500 :         continue;
    2024                 :             : 
    2025                 :        7955 :       bool existed;
    2026                 :        7955 :       v_info_ptr &info = v_info_map.get_or_insert (vec, &existed);
    2027                 :        7955 :       if (!existed)
    2028                 :             :         {
    2029                 :        6360 :           info = new v_info;
    2030                 :        6360 :           info->vec_type = vec_type;
    2031                 :             :         }
    2032                 :        1595 :       else if (!types_compatible_p (vec_type, info->vec_type))
    2033                 :           0 :         continue;
    2034                 :        7955 :       info->vec.safe_push (std::make_pair (idx, i));
    2035                 :             :     }
    2036                 :             : 
    2037                 :             :   /* At least two VECTOR to combine.  */
    2038                 :     3701136 :   if (v_info_map.elements () <= 1)
    2039                 :             :     {
    2040                 :     3700919 :       cleanup_vinfo_map (v_info_map);
    2041                 :     3700919 :       return false;
    2042                 :             :     }
    2043                 :             : 
    2044                 :             :   /* Verify all VECTOR candidates by checking two conditions:
    2045                 :             :        1) sorted offsets are adjacent, no holes.
    2046                 :             :        2) can fill the whole VECTOR perfectly.
    2047                 :             :      And add the valid candidates to a vector for further handling.  */
    2048                 :         217 :   auto_vec<tree> valid_vecs (v_info_map.elements ());
    2049                 :         217 :   for (hash_map<tree, v_info_ptr>::iterator it = v_info_map.begin ();
    2050                 :         959 :        it != v_info_map.end (); ++it)
    2051                 :             :     {
    2052                 :         742 :       tree cand_vec = (*it).first;
    2053                 :         742 :       v_info_ptr cand_info = (*it).second;
    2054                 :         742 :       unsigned int num_elems
    2055                 :         742 :         = TYPE_VECTOR_SUBPARTS (cand_info->vec_type).to_constant ();
    2056                 :         742 :       if (cand_info->vec.length () != num_elems)
    2057                 :         375 :         continue;
    2058                 :         367 :       sbitmap holes = sbitmap_alloc (num_elems);
    2059                 :         367 :       bitmap_ones (holes);
    2060                 :         367 :       bool valid = true;
    2061                 :         367 :       v_info_elem *curr;
    2062                 :        1809 :       FOR_EACH_VEC_ELT (cand_info->vec, i, curr)
    2063                 :             :         {
    2064                 :        1442 :           if (!bitmap_bit_p (holes, curr->first))
    2065                 :             :             {
    2066                 :             :               valid = false;
    2067                 :             :               break;
    2068                 :             :             }
    2069                 :             :           else
    2070                 :        1442 :             bitmap_clear_bit (holes, curr->first);
    2071                 :             :         }
    2072                 :         367 :       if (valid && bitmap_empty_p (holes))
    2073                 :         367 :         valid_vecs.quick_push (cand_vec);
    2074                 :         367 :       sbitmap_free (holes);
    2075                 :             :     }
    2076                 :             : 
    2077                 :             :   /* At least two VECTOR to combine.  */
    2078                 :         217 :   if (valid_vecs.length () <= 1)
    2079                 :             :     {
    2080                 :         181 :       cleanup_vinfo_map (v_info_map);
    2081                 :         181 :       return false;
    2082                 :             :     }
    2083                 :             : 
    2084                 :          36 :   valid_vecs.qsort (sort_by_mach_mode);
    2085                 :             :   /* Go through all candidates by machine mode order, query the mode_to_total
    2086                 :             :      to get the total number for each mode and skip the single one.  */
    2087                 :          78 :   for (unsigned i = 0; i < valid_vecs.length () - 1; ++i)
    2088                 :             :     {
    2089                 :          42 :       tree tvec = valid_vecs[i];
    2090                 :          42 :       enum machine_mode mode = TYPE_MODE (TREE_TYPE (tvec));
    2091                 :             : 
    2092                 :             :       /* Skip modes with only a single candidate.  */
    2093                 :          42 :       if (TYPE_MODE (TREE_TYPE (valid_vecs[i + 1])) != mode)
    2094                 :           4 :         continue;
    2095                 :             : 
    2096                 :          38 :       unsigned int idx, j;
    2097                 :          38 :       gimple *sum = NULL;
    2098                 :          38 :       tree sum_vec = tvec;
    2099                 :          38 :       v_info_ptr info_ptr = *(v_info_map.get (tvec));
    2100                 :          38 :       v_info_elem *elem;
    2101                 :          38 :       tree vec_type = info_ptr->vec_type;
    2102                 :             : 
    2103                 :             :       /* Build the sum for all candidates with same mode.  */
    2104                 :         321 :       do
    2105                 :             :         {
    2106                 :         321 :           sum = build_and_add_sum (vec_type, sum_vec,
    2107                 :         321 :                                    valid_vecs[i + 1], opcode);
    2108                 :             :           /* Update the operands only after build_and_add_sum,
    2109                 :             :              so that we don't have to repeat the placement algorithm
    2110                 :             :              of build_and_add_sum.  */
    2111                 :         321 :           if (sum_vec == tvec
    2112                 :         321 :               && !useless_type_conversion_p (vec_type, TREE_TYPE (sum_vec)))
    2113                 :             :             {
    2114                 :          14 :               gimple_stmt_iterator gsi = gsi_for_stmt (sum);
    2115                 :          14 :               tree vce = build1 (VIEW_CONVERT_EXPR, vec_type, sum_vec);
    2116                 :          14 :               tree lhs = make_ssa_name (vec_type);
    2117                 :          14 :               gimple *g = gimple_build_assign (lhs, VIEW_CONVERT_EXPR, vce);
    2118                 :          14 :               gimple_set_uid (g, gimple_uid (sum));
    2119                 :          14 :               gsi_insert_before (&gsi, g, GSI_NEW_STMT);
    2120                 :          14 :               gimple_assign_set_rhs1 (sum, lhs);
    2121                 :          14 :               update_stmt (sum);
    2122                 :             :             }
    2123                 :         321 :           if (!useless_type_conversion_p (vec_type,
    2124                 :         321 :                                           TREE_TYPE (valid_vecs[i + 1])))
    2125                 :             :             {
    2126                 :         266 :               gimple_stmt_iterator gsi = gsi_for_stmt (sum);
    2127                 :         266 :               tree vce = build1 (VIEW_CONVERT_EXPR, vec_type,
    2128                 :         266 :                                  valid_vecs[i + 1]);
    2129                 :         266 :               tree lhs = make_ssa_name (vec_type);
    2130                 :         266 :               gimple *g = gimple_build_assign (lhs, VIEW_CONVERT_EXPR, vce);
    2131                 :         266 :               gimple_set_uid (g, gimple_uid (sum));
    2132                 :         266 :               gsi_insert_before (&gsi, g, GSI_NEW_STMT);
    2133                 :         266 :               gimple_assign_set_rhs2 (sum, lhs);
    2134                 :         266 :               update_stmt (sum);
    2135                 :             :             }
    2136                 :         321 :           sum_vec = gimple_get_lhs (sum);
    2137                 :         321 :           info_ptr = *(v_info_map.get (valid_vecs[i + 1]));
    2138                 :         321 :           gcc_assert (types_compatible_p (vec_type, info_ptr->vec_type));
    2139                 :             :           /* Update those related ops of current candidate VECTOR.  */
    2140                 :        1563 :           FOR_EACH_VEC_ELT (info_ptr->vec, j, elem)
    2141                 :             :             {
    2142                 :        1242 :               idx = elem->second;
    2143                 :        1242 :               gimple *def = SSA_NAME_DEF_STMT ((*ops)[idx]->op);
    2144                 :             :               /* Set this then op definition will get DCEd later.  */
    2145                 :        1242 :               gimple_set_visited (def, true);
    2146                 :        1242 :               if (opcode == PLUS_EXPR
    2147                 :        1242 :                   || opcode == BIT_XOR_EXPR
    2148                 :         100 :                   || opcode == BIT_IOR_EXPR)
    2149                 :        1182 :                 (*ops)[idx]->op = build_zero_cst (TREE_TYPE ((*ops)[idx]->op));
    2150                 :          60 :               else if (opcode == MULT_EXPR)
    2151                 :          24 :                 (*ops)[idx]->op = build_one_cst (TREE_TYPE ((*ops)[idx]->op));
    2152                 :             :               else
    2153                 :             :                 {
    2154                 :          36 :                   gcc_assert (opcode == BIT_AND_EXPR);
    2155                 :          36 :                   (*ops)[idx]->op
    2156                 :          72 :                     = build_all_ones_cst (TREE_TYPE ((*ops)[idx]->op));
    2157                 :             :                 }
    2158                 :        1242 :               (*ops)[idx]->rank = 0;
    2159                 :             :             }
    2160                 :         321 :           if (dump_file && (dump_flags & TDF_DETAILS))
    2161                 :             :             {
    2162                 :           0 :               fprintf (dump_file, "Generating addition -> ");
    2163                 :           0 :               print_gimple_stmt (dump_file, sum, 0);
    2164                 :             :             }
    2165                 :         321 :           i++;
    2166                 :             :         }
    2167                 :         321 :       while ((i < valid_vecs.length () - 1)
    2168                 :         359 :              && TYPE_MODE (TREE_TYPE (valid_vecs[i + 1])) == mode);
    2169                 :             : 
    2170                 :             :       /* Referring to first valid VECTOR with this mode, generate the
    2171                 :             :          BIT_FIELD_REF statements accordingly.  */
    2172                 :          38 :       info_ptr = *(v_info_map.get (tvec));
    2173                 :          38 :       gcc_assert (sum);
    2174                 :          38 :       tree elem_type = TREE_TYPE (vec_type);
    2175                 :         178 :       FOR_EACH_VEC_ELT (info_ptr->vec, j, elem)
    2176                 :             :         {
    2177                 :         140 :           idx = elem->second;
    2178                 :         140 :           tree dst = make_ssa_name (elem_type);
    2179                 :         140 :           tree pos = bitsize_int (elem->first
    2180                 :             :                                   * tree_to_uhwi (TYPE_SIZE (elem_type)));
    2181                 :         140 :           tree bfr = build3 (BIT_FIELD_REF, elem_type, sum_vec,
    2182                 :         140 :                              TYPE_SIZE (elem_type), pos);
    2183                 :         140 :           gimple *gs = gimple_build_assign (dst, BIT_FIELD_REF, bfr);
    2184                 :         140 :           insert_stmt_after (gs, sum);
    2185                 :         140 :           gimple *def = SSA_NAME_DEF_STMT ((*ops)[idx]->op);
    2186                 :             :           /* Set this then op definition will get DCEd later.  */
    2187                 :         140 :           gimple_set_visited (def, true);
    2188                 :         140 :           (*ops)[idx]->op = gimple_assign_lhs (gs);
    2189                 :         140 :           (*ops)[idx]->rank = get_rank ((*ops)[idx]->op);
    2190                 :         140 :           if (dump_file && (dump_flags & TDF_DETAILS))
    2191                 :             :             {
    2192                 :           0 :               fprintf (dump_file, "Generating bit_field_ref -> ");
    2193                 :           0 :               print_gimple_stmt (dump_file, gs, 0);
    2194                 :             :             }
    2195                 :             :         }
    2196                 :             :     }
    2197                 :             : 
    2198                 :          36 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2199                 :           0 :     fprintf (dump_file, "undistributiong bit_field_ref for vector done.\n");
    2200                 :             : 
    2201                 :          36 :   cleanup_vinfo_map (v_info_map);
    2202                 :             : 
    2203                 :          36 :   return true;
    2204                 :     3701136 : }
    2205                 :             : 
    2206                 :             : /* If OPCODE is BIT_IOR_EXPR or BIT_AND_EXPR and CURR is a comparison
    2207                 :             :    expression, examine the other OPS to see if any of them are comparisons
    2208                 :             :    of the same values, which we may be able to combine or eliminate.
    2209                 :             :    For example, we can rewrite (a < b) | (a == b) as (a <= b).  */
    2210                 :             : 
    2211                 :             : static bool
    2212                 :     7892792 : eliminate_redundant_comparison (enum tree_code opcode,
    2213                 :             :                                 vec<operand_entry *> *ops,
    2214                 :             :                                 unsigned int currindex,
    2215                 :             :                                 operand_entry *curr)
    2216                 :             : {
    2217                 :     7892792 :   tree op1, op2;
    2218                 :     7892792 :   enum tree_code lcode, rcode;
    2219                 :     7892792 :   gimple *def1, *def2;
    2220                 :     7892792 :   int i;
    2221                 :     7892792 :   operand_entry *oe;
    2222                 :             : 
    2223                 :     7892792 :   if (opcode != BIT_IOR_EXPR && opcode != BIT_AND_EXPR)
    2224                 :             :     return false;
    2225                 :             : 
    2226                 :             :   /* Check that CURR is a comparison.  */
    2227                 :     1867319 :   if (TREE_CODE (curr->op) != SSA_NAME)
    2228                 :             :     return false;
    2229                 :     1391882 :   def1 = SSA_NAME_DEF_STMT (curr->op);
    2230                 :     1391882 :   if (!is_gimple_assign (def1))
    2231                 :             :     return false;
    2232                 :     1205180 :   lcode = gimple_assign_rhs_code (def1);
    2233                 :     1205180 :   if (TREE_CODE_CLASS (lcode) != tcc_comparison)
    2234                 :             :     return false;
    2235                 :      477490 :   op1 = gimple_assign_rhs1 (def1);
    2236                 :      477490 :   op2 = gimple_assign_rhs2 (def1);
    2237                 :             : 
    2238                 :             :   /* Now look for a similar comparison in the remaining OPS.  */
    2239                 :     1085103 :   for (i = currindex + 1; ops->iterate (i, &oe); i++)
    2240                 :             :     {
    2241                 :      607772 :       tree t;
    2242                 :             : 
    2243                 :      607772 :       if (TREE_CODE (oe->op) != SSA_NAME)
    2244                 :          37 :         continue;
    2245                 :      607735 :       def2 = SSA_NAME_DEF_STMT (oe->op);
    2246                 :      607735 :       if (!is_gimple_assign (def2))
    2247                 :        6830 :         continue;
    2248                 :      600905 :       rcode = gimple_assign_rhs_code (def2);
    2249                 :      600905 :       if (TREE_CODE_CLASS (rcode) != tcc_comparison)
    2250                 :        6987 :         continue;
    2251                 :             : 
    2252                 :             :       /* If we got here, we have a match.  See if we can combine the
    2253                 :             :          two comparisons.  */
    2254                 :      593918 :       tree type = TREE_TYPE (gimple_assign_lhs (def1));
    2255                 :      593918 :       if (opcode == BIT_IOR_EXPR)
    2256                 :      473287 :         t = maybe_fold_or_comparisons (type,
    2257                 :             :                                        lcode, op1, op2,
    2258                 :             :                                        rcode, gimple_assign_rhs1 (def2),
    2259                 :             :                                        gimple_assign_rhs2 (def2));
    2260                 :             :       else
    2261                 :      120631 :         t = maybe_fold_and_comparisons (type,
    2262                 :             :                                         lcode, op1, op2,
    2263                 :             :                                         rcode, gimple_assign_rhs1 (def2),
    2264                 :             :                                         gimple_assign_rhs2 (def2));
    2265                 :      593918 :       if (!t)
    2266                 :      593729 :         continue;
    2267                 :             : 
    2268                 :             :       /* maybe_fold_and_comparisons and maybe_fold_or_comparisons
    2269                 :             :          always give us a boolean_type_node value back.  If the original
    2270                 :             :          BIT_AND_EXPR or BIT_IOR_EXPR was of a wider integer type,
    2271                 :             :          we need to convert.  */
    2272                 :         189 :       if (!useless_type_conversion_p (TREE_TYPE (curr->op), TREE_TYPE (t)))
    2273                 :             :         {
    2274                 :           1 :           if (!fold_convertible_p (TREE_TYPE (curr->op), t))
    2275                 :           0 :             continue;
    2276                 :           1 :           t = fold_convert (TREE_TYPE (curr->op), t);
    2277                 :             :         }
    2278                 :             : 
    2279                 :         189 :       if (TREE_CODE (t) != INTEGER_CST
    2280                 :         189 :           && !operand_equal_p (t, curr->op, 0))
    2281                 :             :         {
    2282                 :         185 :           enum tree_code subcode;
    2283                 :         185 :           tree newop1, newop2;
    2284                 :         185 :           if (!COMPARISON_CLASS_P (t))
    2285                 :          30 :             continue;
    2286                 :         175 :           extract_ops_from_tree (t, &subcode, &newop1, &newop2);
    2287                 :         175 :           STRIP_USELESS_TYPE_CONVERSION (newop1);
    2288                 :         175 :           STRIP_USELESS_TYPE_CONVERSION (newop2);
    2289                 :         175 :           if (!is_gimple_val (newop1) || !is_gimple_val (newop2))
    2290                 :           0 :             continue;
    2291                 :         175 :           if (lcode == TREE_CODE (t)
    2292                 :         112 :               && operand_equal_p (op1, newop1, 0)
    2293                 :         287 :               && operand_equal_p (op2, newop2, 0))
    2294                 :          79 :             t = curr->op;
    2295                 :         116 :           else if ((TREE_CODE (newop1) == SSA_NAME
    2296                 :          96 :                     && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (newop1))
    2297                 :         172 :                    || (TREE_CODE (newop2) == SSA_NAME
    2298                 :          50 :                        && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (newop2)))
    2299                 :          20 :             continue;
    2300                 :             :         }
    2301                 :             : 
    2302                 :         159 :       if (dump_file && (dump_flags & TDF_DETAILS))
    2303                 :             :         {
    2304                 :           6 :           fprintf (dump_file, "Equivalence: ");
    2305                 :           6 :           print_generic_expr (dump_file, curr->op);
    2306                 :           6 :           fprintf (dump_file, " %s ", op_symbol_code (opcode));
    2307                 :           6 :           print_generic_expr (dump_file, oe->op);
    2308                 :           6 :           fprintf (dump_file, " -> ");
    2309                 :           6 :           print_generic_expr (dump_file, t);
    2310                 :           6 :           fprintf (dump_file, "\n");
    2311                 :             :         }
    2312                 :             : 
    2313                 :             :       /* Now we can delete oe, as it has been subsumed by the new combined
    2314                 :             :          expression t.  */
    2315                 :         159 :       ops->ordered_remove (i);
    2316                 :         159 :       reassociate_stats.ops_eliminated ++;
    2317                 :             : 
    2318                 :             :       /* If t is the same as curr->op, we're done.  Otherwise we must
    2319                 :             :          replace curr->op with t.  Special case is if we got a constant
    2320                 :             :          back, in which case we add it to the end instead of in place of
    2321                 :             :          the current entry.  */
    2322                 :         159 :       if (TREE_CODE (t) == INTEGER_CST)
    2323                 :             :         {
    2324                 :           4 :           ops->ordered_remove (currindex);
    2325                 :           4 :           add_to_ops_vec (ops, t);
    2326                 :             :         }
    2327                 :         155 :       else if (!operand_equal_p (t, curr->op, 0))
    2328                 :             :         {
    2329                 :          76 :           gimple *sum;
    2330                 :          76 :           enum tree_code subcode;
    2331                 :          76 :           tree newop1;
    2332                 :          76 :           tree newop2;
    2333                 :          76 :           gcc_assert (COMPARISON_CLASS_P (t));
    2334                 :          76 :           extract_ops_from_tree (t, &subcode, &newop1, &newop2);
    2335                 :          76 :           STRIP_USELESS_TYPE_CONVERSION (newop1);
    2336                 :          76 :           STRIP_USELESS_TYPE_CONVERSION (newop2);
    2337                 :          76 :           gcc_checking_assert (is_gimple_val (newop1)
    2338                 :             :                                && is_gimple_val (newop2));
    2339                 :          76 :           sum = build_and_add_sum (TREE_TYPE (t), newop1, newop2, subcode);
    2340                 :          76 :           curr->op = gimple_get_lhs (sum);
    2341                 :             :         }
    2342                 :             :       return true;
    2343                 :             :     }
    2344                 :             : 
    2345                 :             :   return false;
    2346                 :             : }
    2347                 :             : 
    2348                 :             : 
    2349                 :             : /* Transform repeated addition of same values into multiply with
    2350                 :             :    constant.  */
    2351                 :             : static bool
    2352                 :     1852012 : transform_add_to_multiply (vec<operand_entry *> *ops)
    2353                 :             : {
    2354                 :     1852012 :   operand_entry *oe;
    2355                 :     1852012 :   tree op = NULL_TREE;
    2356                 :     1852012 :   int j;
    2357                 :     1852012 :   int i, start = -1, end = 0, count = 0;
    2358                 :     1852012 :   auto_vec<std::pair <int, int> > indxs;
    2359                 :     1852012 :   bool changed = false;
    2360                 :             : 
    2361                 :     1852012 :   if (!INTEGRAL_TYPE_P (TREE_TYPE ((*ops)[0]->op))
    2362                 :       66145 :       && (!SCALAR_FLOAT_TYPE_P (TREE_TYPE ((*ops)[0]->op))
    2363                 :       32707 :           || !flag_unsafe_math_optimizations))
    2364                 :             :     return false;
    2365                 :             : 
    2366                 :             :   /* Look for repeated operands.  */
    2367                 :     5598860 :   FOR_EACH_VEC_ELT (*ops, i, oe)
    2368                 :             :     {
    2369                 :     3780455 :       if (start == -1)
    2370                 :             :         {
    2371                 :     1818405 :           count = 1;
    2372                 :     1818405 :           op = oe->op;
    2373                 :     1818405 :           start = i;
    2374                 :             :         }
    2375                 :     1962050 :       else if (operand_equal_p (oe->op, op, 0))
    2376                 :             :         {
    2377                 :         178 :           count++;
    2378                 :         178 :           end = i;
    2379                 :             :         }
    2380                 :             :       else
    2381                 :             :         {
    2382                 :     1961872 :           if (count > 1)
    2383                 :          65 :             indxs.safe_push (std::make_pair (start, end));
    2384                 :     1961872 :           count = 1;
    2385                 :     1961872 :           op = oe->op;
    2386                 :     1961872 :           start = i;
    2387                 :             :         }
    2388                 :             :     }
    2389                 :             : 
    2390                 :     1818405 :   if (count > 1)
    2391                 :          39 :     indxs.safe_push (std::make_pair (start, end));
    2392                 :             : 
    2393                 :     1818600 :   for (j = indxs.length () - 1; j >= 0; --j)
    2394                 :             :     {
    2395                 :             :       /* Convert repeated operand addition to multiplication.  */
    2396                 :         104 :       start = indxs[j].first;
    2397                 :         104 :       end = indxs[j].second;
    2398                 :         104 :       op = (*ops)[start]->op;
    2399                 :         104 :       count = end - start + 1;
    2400                 :         386 :       for (i = end; i >= start; --i)
    2401                 :         282 :         ops->unordered_remove (i);
    2402                 :         104 :       tree tmp = make_ssa_name (TREE_TYPE (op));
    2403                 :         104 :       tree cst = build_int_cst (integer_type_node, count);
    2404                 :         104 :       gassign *mul_stmt
    2405                 :         104 :         = gimple_build_assign (tmp, MULT_EXPR,
    2406                 :         104 :                                op, fold_convert (TREE_TYPE (op), cst));
    2407                 :         104 :       gimple_set_visited (mul_stmt, true);
    2408                 :         104 :       add_to_ops_vec (ops, tmp, mul_stmt);
    2409                 :         104 :       changed = true;
    2410                 :             :     }
    2411                 :             : 
    2412                 :             :   return changed;
    2413                 :     1852012 : }
    2414                 :             : 
    2415                 :             : 
    2416                 :             : /* Perform various identities and other optimizations on the list of
    2417                 :             :    operand entries, stored in OPS.  The tree code for the binary
    2418                 :             :    operation between all the operands is OPCODE.  */
    2419                 :             : 
    2420                 :             : static void
    2421                 :     3833890 : optimize_ops_list (enum tree_code opcode,
    2422                 :             :                    vec<operand_entry *> *ops)
    2423                 :             : {
    2424                 :     3847717 :   unsigned int length = ops->length ();
    2425                 :     3847717 :   unsigned int i;
    2426                 :     3847717 :   operand_entry *oe;
    2427                 :     7694395 :   operand_entry *oelast = NULL;
    2428                 :     7694395 :   bool iterate = false;
    2429                 :             : 
    2430                 :     3847717 :   if (length == 1)
    2431                 :     3833890 :     return;
    2432                 :             : 
    2433                 :     3846678 :   oelast = ops->last ();
    2434                 :             : 
    2435                 :             :   /* If the last two are constants, pop the constants off, merge them
    2436                 :             :      and try the next two.  */
    2437                 :     3846678 :   if (oelast->rank == 0 && is_gimple_min_invariant (oelast->op))
    2438                 :             :     {
    2439                 :     2741276 :       operand_entry *oelm1 = (*ops)[length - 2];
    2440                 :             : 
    2441                 :     2741276 :       if (oelm1->rank == 0
    2442                 :       10625 :           && is_gimple_min_invariant (oelm1->op)
    2443                 :     2751901 :           && useless_type_conversion_p (TREE_TYPE (oelm1->op),
    2444                 :       10625 :                                        TREE_TYPE (oelast->op)))
    2445                 :             :         {
    2446                 :       10625 :           tree folded = fold_binary (opcode, TREE_TYPE (oelm1->op),
    2447                 :             :                                      oelm1->op, oelast->op);
    2448                 :             : 
    2449                 :       10625 :           if (folded && is_gimple_min_invariant (folded))
    2450                 :             :             {
    2451                 :       10607 :               if (dump_file && (dump_flags & TDF_DETAILS))
    2452                 :           0 :                 fprintf (dump_file, "Merging constants\n");
    2453                 :             : 
    2454                 :       10607 :               ops->pop ();
    2455                 :       10607 :               ops->pop ();
    2456                 :             : 
    2457                 :       10607 :               add_to_ops_vec (ops, folded);
    2458                 :       10607 :               reassociate_stats.constants_eliminated++;
    2459                 :             : 
    2460                 :       10607 :               optimize_ops_list (opcode, ops);
    2461                 :       10607 :               return;
    2462                 :             :             }
    2463                 :             :         }
    2464                 :             :     }
    2465                 :             : 
    2466                 :     3836071 :   eliminate_using_constants (opcode, ops);
    2467                 :     3836071 :   oelast = NULL;
    2468                 :             : 
    2469                 :    11731982 :   for (i = 0; ops->iterate (i, &oe);)
    2470                 :             :     {
    2471                 :     7895913 :       bool done = false;
    2472                 :             : 
    2473                 :     7895913 :       if (eliminate_not_pairs (opcode, ops, i, oe))
    2474                 :           2 :         return;
    2475                 :     7895912 :       if (eliminate_duplicate_pair (opcode, ops, &done, i, oe, oelast)
    2476                 :     7895878 :           || (!done && eliminate_plus_minus_pair (opcode, ops, i, oe))
    2477                 :    15788704 :           || (!done && eliminate_redundant_comparison (opcode, ops, i, oe)))
    2478                 :             :         {
    2479                 :        3279 :           if (done)
    2480                 :             :             return;
    2481                 :        3278 :           iterate = true;
    2482                 :        3278 :           oelast = NULL;
    2483                 :        3278 :           continue;
    2484                 :             :         }
    2485                 :     7892633 :       oelast = oe;
    2486                 :     7892633 :       i++;
    2487                 :             :     }
    2488                 :             : 
    2489                 :     3836069 :   if (iterate)
    2490                 :             :     optimize_ops_list (opcode, ops);
    2491                 :             : }
    2492                 :             : 
    2493                 :             : /* The following functions are subroutines to optimize_range_tests and allow
    2494                 :             :    it to try to change a logical combination of comparisons into a range
    2495                 :             :    test.
    2496                 :             : 
    2497                 :             :    For example, both
    2498                 :             :         X == 2 || X == 5 || X == 3 || X == 4
    2499                 :             :    and
    2500                 :             :         X >= 2 && X <= 5
    2501                 :             :    are converted to
    2502                 :             :         (unsigned) (X - 2) <= 3
    2503                 :             : 
    2504                 :             :    For more information see comments above fold_test_range in fold-const.cc,
    2505                 :             :    this implementation is for GIMPLE.  */
    2506                 :             : 
    2507                 :             : 
    2508                 :             : 
    2509                 :             : /* Dump the range entry R to FILE, skipping its expression if SKIP_EXP.  */
    2510                 :             : 
    2511                 :             : void
    2512                 :         141 : dump_range_entry (FILE *file, struct range_entry *r, bool skip_exp)
    2513                 :             : {
    2514                 :         141 :   if (!skip_exp)
    2515                 :          59 :     print_generic_expr (file, r->exp);
    2516                 :         251 :   fprintf (file, " %c[", r->in_p ? '+' : '-');
    2517                 :         141 :   print_generic_expr (file, r->low);
    2518                 :         141 :   fputs (", ", file);
    2519                 :         141 :   print_generic_expr (file, r->high);
    2520                 :         141 :   fputc (']', file);
    2521                 :         141 : }
    2522                 :             : 
    2523                 :             : /* Dump the range entry R to STDERR.  */
    2524                 :             : 
    2525                 :             : DEBUG_FUNCTION void
    2526                 :           0 : debug_range_entry (struct range_entry *r)
    2527                 :             : {
    2528                 :           0 :   dump_range_entry (stderr, r, false);
    2529                 :           0 :   fputc ('\n', stderr);
    2530                 :           0 : }
    2531                 :             : 
    2532                 :             : /* This is similar to make_range in fold-const.cc, but on top of
    2533                 :             :    GIMPLE instead of trees.  If EXP is non-NULL, it should be
    2534                 :             :    an SSA_NAME and STMT argument is ignored, otherwise STMT
    2535                 :             :    argument should be a GIMPLE_COND.  */
    2536                 :             : 
    2537                 :             : void
    2538                 :     5031662 : init_range_entry (struct range_entry *r, tree exp, gimple *stmt)
    2539                 :             : {
    2540                 :     5031662 :   int in_p;
    2541                 :     5031662 :   tree low, high;
    2542                 :     5031662 :   bool is_bool, strict_overflow_p;
    2543                 :             : 
    2544                 :     5031662 :   r->exp = NULL_TREE;
    2545                 :     5031662 :   r->in_p = false;
    2546                 :     5031662 :   r->strict_overflow_p = false;
    2547                 :     5031662 :   r->low = NULL_TREE;
    2548                 :     5031662 :   r->high = NULL_TREE;
    2549                 :     5031662 :   if (exp != NULL_TREE
    2550                 :     5031662 :       && (TREE_CODE (exp) != SSA_NAME || !INTEGRAL_TYPE_P (TREE_TYPE (exp))))
    2551                 :      764277 :     return;
    2552                 :             : 
    2553                 :             :   /* Start with simply saying "EXP != 0" and then look at the code of EXP
    2554                 :             :      and see if we can refine the range.  Some of the cases below may not
    2555                 :             :      happen, but it doesn't seem worth worrying about this.  We "continue"
    2556                 :             :      the outer loop when we've changed something; otherwise we "break"
    2557                 :             :      the switch, which will "break" the while.  */
    2558                 :     4559266 :   low = exp ? build_int_cst (TREE_TYPE (exp), 0) : boolean_false_node;
    2559                 :     4559266 :   high = low;
    2560                 :     4559266 :   in_p = 0;
    2561                 :     4559266 :   strict_overflow_p = false;
    2562                 :     4559266 :   is_bool = false;
    2563                 :     4559266 :   if (exp == NULL_TREE)
    2564                 :             :     is_bool = true;
    2565                 :     1481127 :   else if (TYPE_PRECISION (TREE_TYPE (exp)) == 1)
    2566                 :             :     {
    2567                 :      619911 :       if (TYPE_UNSIGNED (TREE_TYPE (exp)))
    2568                 :             :         is_bool = true;
    2569                 :             :       else
    2570                 :             :         return;
    2571                 :             :     }
    2572                 :      861216 :   else if (TREE_CODE (TREE_TYPE (exp)) == BOOLEAN_TYPE)
    2573                 :           8 :     is_bool = true;
    2574                 :             : 
    2575                 :     7163875 :   while (1)
    2576                 :             :     {
    2577                 :     7163875 :       enum tree_code code;
    2578                 :     7163875 :       tree arg0, arg1, exp_type;
    2579                 :     7163875 :       tree nexp;
    2580                 :     7163875 :       location_t loc;
    2581                 :             : 
    2582                 :     7163875 :       if (exp != NULL_TREE)
    2583                 :             :         {
    2584                 :     4085736 :           if (TREE_CODE (exp) != SSA_NAME
    2585                 :     4085736 :               || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (exp))
    2586                 :             :             break;
    2587                 :             : 
    2588                 :     4085736 :           stmt = SSA_NAME_DEF_STMT (exp);
    2589                 :     4085736 :           if (!is_gimple_assign (stmt))
    2590                 :             :             break;
    2591                 :             : 
    2592                 :     2483388 :           code = gimple_assign_rhs_code (stmt);
    2593                 :     2483388 :           arg0 = gimple_assign_rhs1 (stmt);
    2594                 :     2483388 :           arg1 = gimple_assign_rhs2 (stmt);
    2595                 :     2483388 :           exp_type = TREE_TYPE (exp);
    2596                 :             :         }
    2597                 :             :       else
    2598                 :             :         {
    2599                 :     3078139 :           code = gimple_cond_code (stmt);
    2600                 :     3078139 :           arg0 = gimple_cond_lhs (stmt);
    2601                 :     3078139 :           arg1 = gimple_cond_rhs (stmt);
    2602                 :     3078139 :           exp_type = boolean_type_node;
    2603                 :             :         }
    2604                 :             : 
    2605                 :     5561527 :       if (TREE_CODE (arg0) != SSA_NAME
    2606                 :     4444218 :           || SSA_NAME_OCCURS_IN_ABNORMAL_PHI (arg0)
    2607                 :    10005365 :           || ssa_name_maybe_undef_p (arg0))
    2608                 :             :         break;
    2609                 :     4437891 :       loc = gimple_location (stmt);
    2610                 :     4437891 :       switch (code)
    2611                 :             :         {
    2612                 :       31995 :         case BIT_NOT_EXPR:
    2613                 :       31995 :           if (TREE_CODE (TREE_TYPE (exp)) == BOOLEAN_TYPE
    2614                 :             :               /* Ensure the range is either +[-,0], +[0,0],
    2615                 :             :                  -[-,0], -[0,0] or +[1,-], +[1,1], -[1,-] or
    2616                 :             :                  -[1,1].  If it is e.g. +[-,-] or -[-,-]
    2617                 :             :                  or similar expression of unconditional true or
    2618                 :             :                  false, it should not be negated.  */
    2619                 :       31995 :               && ((high && integer_zerop (high))
    2620                 :           0 :                   || (low && integer_onep (low))))
    2621                 :             :             {
    2622                 :        6679 :               in_p = !in_p;
    2623                 :        6679 :               exp = arg0;
    2624                 :        6679 :               continue;
    2625                 :             :             }
    2626                 :             :           break;
    2627                 :        3009 :         case SSA_NAME:
    2628                 :        3009 :           exp = arg0;
    2629                 :        3009 :           continue;
    2630                 :      216818 :         CASE_CONVERT:
    2631                 :      216818 :           if (is_bool)
    2632                 :             :             {
    2633                 :      113455 :               if ((TYPE_PRECISION (exp_type) == 1
    2634                 :      107496 :                    || TREE_CODE (exp_type) == BOOLEAN_TYPE)
    2635                 :      113455 :                   && TYPE_PRECISION (TREE_TYPE (arg0)) > 1)
    2636                 :             :                 return;
    2637                 :             :             }
    2638                 :      103363 :           else if (TYPE_PRECISION (TREE_TYPE (arg0)) == 1)
    2639                 :             :             {
    2640                 :        3984 :               if (TYPE_UNSIGNED (TREE_TYPE (arg0)))
    2641                 :             :                 is_bool = true;
    2642                 :             :               else
    2643                 :             :                 return;
    2644                 :             :             }
    2645                 :       99379 :           else if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE)
    2646                 :      114654 :             is_bool = true;
    2647                 :      214033 :           goto do_default;
    2648                 :             :         case EQ_EXPR:
    2649                 :             :         case NE_EXPR:
    2650                 :             :         case LT_EXPR:
    2651                 :             :         case LE_EXPR:
    2652                 :             :         case GE_EXPR:
    2653                 :             :         case GT_EXPR:
    2654                 :             :           is_bool = true;
    2655                 :             :           /* FALLTHRU */
    2656                 :      589621 :         default:
    2657                 :      589621 :           if (!is_bool)
    2658                 :             :             return;
    2659                 :      300525 :         do_default:
    2660                 :     4111006 :           nexp = make_range_step (loc, code, arg0, arg1, exp_type,
    2661                 :             :                                   &low, &high, &in_p,
    2662                 :             :                                   &strict_overflow_p);
    2663                 :     4111006 :           if (nexp != NULL_TREE)
    2664                 :             :             {
    2665                 :     2594921 :               exp = nexp;
    2666                 :     2594921 :               gcc_assert (TREE_CODE (exp) == SSA_NAME);
    2667                 :     2594921 :               continue;
    2668                 :             :             }
    2669                 :             :           break;
    2670                 :             :         }
    2671                 :             :       break;
    2672                 :             :     }
    2673                 :     4267385 :   if (is_bool)
    2674                 :             :     {
    2675                 :     3699257 :       r->exp = exp;
    2676                 :     3699257 :       r->in_p = in_p;
    2677                 :     3699257 :       r->low = low;
    2678                 :     3699257 :       r->high = high;
    2679                 :     3699257 :       r->strict_overflow_p = strict_overflow_p;
    2680                 :             :     }
    2681                 :             : }
    2682                 :             : 
    2683                 :             : /* Comparison function for qsort.  Sort entries
    2684                 :             :    without SSA_NAME exp first, then with SSA_NAMEs sorted
    2685                 :             :    by increasing SSA_NAME_VERSION, and for the same SSA_NAMEs
    2686                 :             :    by increasing ->low and if ->low is the same, by increasing
    2687                 :             :    ->high.  ->low == NULL_TREE means minimum, ->high == NULL_TREE
    2688                 :             :    maximum.  */
    2689                 :             : 
    2690                 :             : static int
    2691                 :     6383470 : range_entry_cmp (const void *a, const void *b)
    2692                 :             : {
    2693                 :     6383470 :   const struct range_entry *p = (const struct range_entry *) a;
    2694                 :     6383470 :   const struct range_entry *q = (const struct range_entry *) b;
    2695                 :             : 
    2696                 :     6383470 :   if (p->exp != NULL_TREE && TREE_CODE (p->exp) == SSA_NAME)
    2697                 :             :     {
    2698                 :     2979843 :       if (q->exp != NULL_TREE && TREE_CODE (q->exp) == SSA_NAME)
    2699                 :             :         {
    2700                 :             :           /* Group range_entries for the same SSA_NAME together.  */
    2701                 :     2920368 :           if (SSA_NAME_VERSION (p->exp) < SSA_NAME_VERSION (q->exp))
    2702                 :             :             return -1;
    2703                 :     1280239 :           else if (SSA_NAME_VERSION (p->exp) > SSA_NAME_VERSION (q->exp))
    2704                 :             :             return 1;
    2705                 :             :           /* If ->low is different, NULL low goes first, then by
    2706                 :             :              ascending low.  */
    2707                 :      132321 :           if (p->low != NULL_TREE)
    2708                 :             :             {
    2709                 :      117152 :               if (q->low != NULL_TREE)
    2710                 :             :                 {
    2711                 :      110281 :                   tree tem = fold_binary (LT_EXPR, boolean_type_node,
    2712                 :             :                                           p->low, q->low);
    2713                 :      110281 :                   if (tem && integer_onep (tem))
    2714                 :             :                     return -1;
    2715                 :       47032 :                   tem = fold_binary (GT_EXPR, boolean_type_node,
    2716                 :             :                                      p->low, q->low);
    2717                 :       47032 :                   if (tem && integer_onep (tem))
    2718                 :             :                     return 1;
    2719                 :             :                 }
    2720                 :             :               else
    2721                 :             :                 return 1;
    2722                 :             :             }
    2723                 :       15169 :           else if (q->low != NULL_TREE)
    2724                 :             :             return -1;
    2725                 :             :           /* If ->high is different, NULL high goes last, before that by
    2726                 :             :              ascending high.  */
    2727                 :       13212 :           if (p->high != NULL_TREE)
    2728                 :             :             {
    2729                 :       13080 :               if (q->high != NULL_TREE)
    2730                 :             :                 {
    2731                 :       12824 :                   tree tem = fold_binary (LT_EXPR, boolean_type_node,
    2732                 :             :                                           p->high, q->high);
    2733                 :       12824 :                   if (tem && integer_onep (tem))
    2734                 :             :                     return -1;
    2735                 :        4565 :                   tem = fold_binary (GT_EXPR, boolean_type_node,
    2736                 :             :                                      p->high, q->high);
    2737                 :        4565 :                   if (tem && integer_onep (tem))
    2738                 :             :                     return 1;
    2739                 :             :                 }
    2740                 :             :               else
    2741                 :             :                 return -1;
    2742                 :             :             }
    2743                 :         132 :           else if (q->high != NULL_TREE)
    2744                 :             :             return 1;
    2745                 :             :           /* If both ranges are the same, sort below by ascending idx.  */
    2746                 :             :         }
    2747                 :             :       else
    2748                 :             :         return 1;
    2749                 :             :     }
    2750                 :     3403627 :   else if (q->exp != NULL_TREE && TREE_CODE (q->exp) == SSA_NAME)
    2751                 :             :     return -1;
    2752                 :             : 
    2753                 :     3321811 :   if (p->idx < q->idx)
    2754                 :             :     return -1;
    2755                 :             :   else
    2756                 :             :     {
    2757                 :     1676383 :       gcc_checking_assert (p->idx > q->idx);
    2758                 :             :       return 1;
    2759                 :             :     }
    2760                 :             : }
    2761                 :             : 
    2762                 :             : /* Helper function for update_range_test.  Force EXPR into an SSA_NAME,
    2763                 :             :    insert needed statements BEFORE or after GSI.  */
    2764                 :             : 
    2765                 :             : static tree
    2766                 :       19584 : force_into_ssa_name (gimple_stmt_iterator *gsi, tree expr, bool before)
    2767                 :             : {
    2768                 :       19584 :   enum gsi_iterator_update m = before ? GSI_SAME_STMT : GSI_CONTINUE_LINKING;
    2769                 :       19584 :   tree ret = force_gimple_operand_gsi (gsi, expr, true, NULL_TREE, before, m);
    2770                 :       19584 :   if (TREE_CODE (ret) != SSA_NAME)
    2771                 :             :     {
    2772                 :          30 :       gimple *g = gimple_build_assign (make_ssa_name (TREE_TYPE (ret)), ret);
    2773                 :          30 :       if (before)
    2774                 :          30 :         gsi_insert_before (gsi, g, GSI_SAME_STMT);
    2775                 :             :       else
    2776                 :           0 :         gsi_insert_after (gsi, g, GSI_CONTINUE_LINKING);
    2777                 :          30 :       ret = gimple_assign_lhs (g);
    2778                 :             :     }
    2779                 :       19584 :   return ret;
    2780                 :             : }
    2781                 :             : 
    2782                 :             : /* Helper routine of optimize_range_test.
    2783                 :             :    [EXP, IN_P, LOW, HIGH, STRICT_OVERFLOW_P] is a merged range for
    2784                 :             :    RANGE and OTHERRANGE through OTHERRANGE + COUNT - 1 ranges,
    2785                 :             :    OPCODE and OPS are arguments of optimize_range_tests.  If OTHERRANGE
    2786                 :             :    is NULL, OTHERRANGEP should not be and then OTHERRANGEP points to
    2787                 :             :    an array of COUNT pointers to other ranges.  Return
    2788                 :             :    true if the range merge has been successful.
    2789                 :             :    If OPCODE is ERROR_MARK, this is called from within
    2790                 :             :    maybe_optimize_range_tests and is performing inter-bb range optimization.
    2791                 :             :    In that case, whether an op is BIT_AND_EXPR or BIT_IOR_EXPR is found in
    2792                 :             :    oe->rank.  */
    2793                 :             : 
    2794                 :             : static bool
    2795                 :       19584 : update_range_test (struct range_entry *range, struct range_entry *otherrange,
    2796                 :             :                    struct range_entry **otherrangep,
    2797                 :             :                    unsigned int count, enum tree_code opcode,
    2798                 :             :                    vec<operand_entry *> *ops, tree exp, gimple_seq seq,
    2799                 :             :                    bool in_p, tree low, tree high, bool strict_overflow_p)
    2800                 :             : {
    2801                 :       19584 :   unsigned int idx = range->idx;
    2802                 :       19584 :   struct range_entry *swap_with = NULL;
    2803                 :       19584 :   basic_block rewrite_bb_first = NULL, rewrite_bb_last = NULL;
    2804                 :       19584 :   if (opcode == ERROR_MARK)
    2805                 :             :     {
    2806                 :             :       /* For inter-bb range test optimization, pick from the range tests
    2807                 :             :          the one which is tested in the earliest condition (one dominating
    2808                 :             :          the others), because otherwise there could be some UB (e.g. signed
    2809                 :             :          overflow) in following bbs that we'd expose which wasn't there in
    2810                 :             :          the original program.  See PR104196.  */
    2811                 :        8733 :       basic_block orig_range_bb = BASIC_BLOCK_FOR_FN (cfun, (*ops)[idx]->id);
    2812                 :        8733 :       basic_block range_bb = orig_range_bb;
    2813                 :       22182 :       for (unsigned int i = 0; i < count; i++)
    2814                 :             :         {
    2815                 :       13449 :           struct range_entry *this_range;
    2816                 :       13449 :           if (otherrange)
    2817                 :        5215 :             this_range = otherrange + i;
    2818                 :             :           else
    2819                 :        8234 :             this_range = otherrangep[i];
    2820                 :       13449 :           operand_entry *oe = (*ops)[this_range->idx];
    2821                 :       13449 :           basic_block this_bb = BASIC_BLOCK_FOR_FN (cfun, oe->id);
    2822                 :       13449 :           if (range_bb != this_bb
    2823                 :       13449 :               && dominated_by_p (CDI_DOMINATORS, range_bb, this_bb))
    2824                 :             :             {
    2825                 :        7486 :               swap_with = this_range;
    2826                 :        7486 :               range_bb = this_bb;
    2827                 :        7486 :               idx = this_range->idx;
    2828                 :             :             }
    2829                 :             :         }
    2830                 :             :       /* If seq is non-NULL, it can contain statements that use SSA_NAMEs
    2831                 :             :          only defined in later blocks.  In this case we can't move the
    2832                 :             :          merged comparison earlier, so instead check if there are any stmts
    2833                 :             :          that might trigger signed integer overflow in between and rewrite
    2834                 :             :          them.  But only after we check if the optimization is possible.  */
    2835                 :        8733 :       if (seq && swap_with)
    2836                 :             :         {
    2837                 :        3172 :           rewrite_bb_first = range_bb;
    2838                 :        3172 :           rewrite_bb_last = orig_range_bb;
    2839                 :        3172 :           idx = range->idx;
    2840                 :        3172 :           swap_with = NULL;
    2841                 :             :         }
    2842                 :             :     }
    2843                 :       19584 :   operand_entry *oe = (*ops)[idx];
    2844                 :       19584 :   tree op = oe->op;
    2845                 :       32001 :   gimple *stmt = op ? SSA_NAME_DEF_STMT (op)
    2846                 :        7167 :                     : last_nondebug_stmt (BASIC_BLOCK_FOR_FN (cfun, oe->id));
    2847                 :       19584 :   location_t loc = gimple_location (stmt);
    2848                 :       19584 :   tree optype = op ? TREE_TYPE (op) : boolean_type_node;
    2849                 :       19584 :   tree tem = build_range_check (loc, optype, unshare_expr (exp),
    2850                 :             :                                 in_p, low, high);
    2851                 :       19584 :   enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
    2852                 :       19584 :   gimple_stmt_iterator gsi;
    2853                 :       19584 :   unsigned int i, uid;
    2854                 :             : 
    2855                 :       19584 :   if (tem == NULL_TREE)
    2856                 :             :     return false;
    2857                 :             : 
    2858                 :             :   /* If op is default def SSA_NAME, there is no place to insert the
    2859                 :             :      new comparison.  Give up, unless we can use OP itself as the
    2860                 :             :      range test.  */
    2861                 :       32001 :   if (op && SSA_NAME_IS_DEFAULT_DEF (op))
    2862                 :             :     {
    2863                 :           0 :       if (op == range->exp
    2864                 :           0 :           && ((TYPE_PRECISION (optype) == 1 && TYPE_UNSIGNED (optype))
    2865                 :           0 :               || TREE_CODE (optype) == BOOLEAN_TYPE)
    2866                 :           0 :           && (op == tem
    2867                 :           0 :               || (TREE_CODE (tem) == EQ_EXPR
    2868                 :           0 :                   && TREE_OPERAND (tem, 0) == op
    2869                 :           0 :                   && integer_onep (TREE_OPERAND (tem, 1))))
    2870                 :           0 :           && opcode != BIT_IOR_EXPR
    2871                 :           0 :           && (opcode != ERROR_MARK || oe->rank != BIT_IOR_EXPR))
    2872                 :             :         {
    2873                 :             :           stmt = NULL;
    2874                 :             :           tem = op;
    2875                 :             :         }
    2876                 :             :       else
    2877                 :           0 :         return false;
    2878                 :             :     }
    2879                 :             : 
    2880                 :       19584 :   if (swap_with)
    2881                 :        1055 :     std::swap (range->idx, swap_with->idx);
    2882                 :             : 
    2883                 :       19584 :   if (strict_overflow_p && issue_strict_overflow_warning (wc))
    2884                 :           0 :     warning_at (loc, OPT_Wstrict_overflow,
    2885                 :             :                 "assuming signed overflow does not occur "
    2886                 :             :                 "when simplifying range test");
    2887                 :             : 
    2888                 :       19584 :   if (dump_file && (dump_flags & TDF_DETAILS))
    2889                 :             :     {
    2890                 :          39 :       struct range_entry *r;
    2891                 :          39 :       fprintf (dump_file, "Optimizing range tests ");
    2892                 :          39 :       dump_range_entry (dump_file, range, false);
    2893                 :         180 :       for (i = 0; i < count; i++)
    2894                 :             :         {
    2895                 :         102 :           if (otherrange)
    2896                 :          82 :             r = otherrange + i;
    2897                 :             :           else
    2898                 :          20 :             r = otherrangep[i];
    2899                 :         102 :           if (r->exp
    2900                 :         102 :               && r->exp != range->exp
    2901                 :          20 :               && TREE_CODE (r->exp) == SSA_NAME)
    2902                 :             :             {
    2903                 :          20 :               fprintf (dump_file, " and ");
    2904                 :          20 :               dump_range_entry (dump_file, r, false);
    2905                 :             :             }
    2906                 :             :           else
    2907                 :             :             {
    2908                 :          82 :               fprintf (dump_file, " and");
    2909                 :          82 :               dump_range_entry (dump_file, r, true);
    2910                 :             :             }
    2911                 :             :         }
    2912                 :          39 :       fprintf (dump_file, "\n into ");
    2913                 :          39 :       print_generic_expr (dump_file, tem);
    2914                 :          39 :       fprintf (dump_file, "\n");
    2915                 :             :     }
    2916                 :             : 
    2917                 :             :   /* In inter-bb range optimization mode, if we have a seq, we can't
    2918                 :             :      move the merged comparison to the earliest bb from the comparisons
    2919                 :             :      being replaced, so instead rewrite stmts that could trigger signed
    2920                 :             :      integer overflow.  */
    2921                 :        7954 :   for (basic_block bb = rewrite_bb_last;
    2922                 :       27538 :        bb != rewrite_bb_first; bb = single_pred (bb))
    2923                 :       15908 :     for (gimple_stmt_iterator gsi = gsi_start_bb (bb);
    2924                 :       26794 :          !gsi_end_p (gsi); gsi_next (&gsi))
    2925                 :             :       {
    2926                 :       18840 :         gimple *stmt = gsi_stmt (gsi);
    2927                 :       18840 :         if (is_gimple_assign (stmt))
    2928                 :        7245 :           if (tree lhs = gimple_assign_lhs (stmt))
    2929                 :       14488 :             if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
    2930                 :         133 :                  || POINTER_TYPE_P (TREE_TYPE (lhs)))
    2931                 :       14481 :                 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (lhs)))
    2932                 :             :               {
    2933                 :        2466 :                 enum tree_code code = gimple_assign_rhs_code (stmt);
    2934                 :        2466 :                 if (arith_code_with_undefined_signed_overflow (code))
    2935                 :             :                   {
    2936                 :          22 :                     gimple_stmt_iterator gsip = gsi;
    2937                 :          22 :                     gimple_stmt_iterator gsin = gsi;
    2938                 :          22 :                     gsi_prev (&gsip);
    2939                 :          22 :                     gsi_next (&gsin);
    2940                 :          22 :                     rewrite_to_defined_overflow (&gsi);
    2941                 :          22 :                     unsigned uid = gimple_uid (stmt);
    2942                 :          22 :                     if (gsi_end_p (gsip))
    2943                 :          20 :                       gsip = gsi_after_labels (bb);
    2944                 :             :                     else
    2945                 :           2 :                       gsi_next (&gsip);
    2946                 :          99 :                     for (; gsi_stmt (gsip) != gsi_stmt (gsin);
    2947                 :          77 :                          gsi_next (&gsip))
    2948                 :          77 :                       gimple_set_uid (gsi_stmt (gsip), uid);
    2949                 :             :                   }
    2950                 :             :               }
    2951                 :             :       }
    2952                 :             : 
    2953                 :       19584 :   if (opcode == BIT_IOR_EXPR
    2954                 :       13450 :       || (opcode == ERROR_MARK && oe->rank == BIT_IOR_EXPR))
    2955                 :       12953 :     tem = invert_truthvalue_loc (loc, tem);
    2956                 :             : 
    2957                 :       19584 :   tem = fold_convert_loc (loc, optype, tem);
    2958                 :       19584 :   if (stmt)
    2959                 :             :     {
    2960                 :       19584 :       gsi = gsi_for_stmt (stmt);
    2961                 :       19584 :       uid = gimple_uid (stmt);
    2962                 :             :     }
    2963                 :             :   else
    2964                 :             :     {
    2965                 :           0 :       gsi = gsi_none ();
    2966                 :           0 :       uid = 0;
    2967                 :             :     }
    2968                 :       19584 :   if (stmt == NULL)
    2969                 :           0 :     gcc_checking_assert (tem == op);
    2970                 :             :   /* In rare cases range->exp can be equal to lhs of stmt.
    2971                 :             :      In that case we have to insert after the stmt rather then before
    2972                 :             :      it.  If stmt is a PHI, insert it at the start of the basic block.  */
    2973                 :       19584 :   else if (op != range->exp)
    2974                 :             :     {
    2975                 :       19584 :       gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
    2976                 :       19584 :       tem = force_into_ssa_name (&gsi, tem, true);
    2977                 :       19584 :       gsi_prev (&gsi);
    2978                 :             :     }
    2979                 :           0 :   else if (gimple_code (stmt) != GIMPLE_PHI)
    2980                 :             :     {
    2981                 :           0 :       gsi_insert_seq_after (&gsi, seq, GSI_CONTINUE_LINKING);
    2982                 :           0 :       tem = force_into_ssa_name (&gsi, tem, false);
    2983                 :             :     }
    2984                 :             :   else
    2985                 :             :     {
    2986                 :           0 :       gsi = gsi_after_labels (gimple_bb (stmt));
    2987                 :           0 :       if (!gsi_end_p (gsi))
    2988                 :           0 :         uid = gimple_uid (gsi_stmt (gsi));
    2989                 :             :       else
    2990                 :             :         {
    2991                 :           0 :           gsi = gsi_start_bb (gimple_bb (stmt));
    2992                 :           0 :           uid = 1;
    2993                 :           0 :           while (!gsi_end_p (gsi))
    2994                 :             :             {
    2995                 :           0 :               uid = gimple_uid (gsi_stmt (gsi));
    2996                 :           0 :               gsi_next (&gsi);
    2997                 :             :             }
    2998                 :             :         }
    2999                 :           0 :       gsi_insert_seq_before (&gsi, seq, GSI_SAME_STMT);
    3000                 :           0 :       tem = force_into_ssa_name (&gsi, tem, true);
    3001                 :           0 :       if (gsi_end_p (gsi))
    3002                 :           0 :         gsi = gsi_last_bb (gimple_bb (stmt));
    3003                 :             :       else
    3004                 :       19584 :         gsi_prev (&gsi);
    3005                 :             :     }
    3006                 :      133096 :   for (; !gsi_end_p (gsi); gsi_prev (&gsi))
    3007                 :       71928 :     if (gimple_uid (gsi_stmt (gsi)))
    3008                 :             :       break;
    3009                 :             :     else
    3010                 :       56756 :       gimple_set_uid (gsi_stmt (gsi), uid);
    3011                 :             : 
    3012                 :       19584 :   oe->op = tem;
    3013                 :       19584 :   range->exp = exp;
    3014                 :       19584 :   range->low = low;
    3015                 :       19584 :   range->high = high;
    3016                 :       19584 :   range->in_p = in_p;
    3017                 :       19584 :   range->strict_overflow_p = false;
    3018                 :             : 
    3019                 :       44970 :   for (i = 0; i < count; i++)
    3020                 :             :     {
    3021                 :       25386 :       if (otherrange)
    3022                 :       13353 :         range = otherrange + i;
    3023                 :             :       else
    3024                 :       12033 :         range = otherrangep[i];
    3025                 :       25386 :       oe = (*ops)[range->idx];
    3026                 :             :       /* Now change all the other range test immediate uses, so that
    3027                 :             :          those tests will be optimized away.  */
    3028                 :       25386 :       if (opcode == ERROR_MARK)
    3029                 :             :         {
    3030                 :       13449 :           if (oe->op)
    3031                 :        1913 :             oe->op = build_int_cst (TREE_TYPE (oe->op),
    3032                 :        1913 :                                     oe->rank == BIT_IOR_EXPR ? 0 : 1);
    3033                 :             :           else
    3034                 :       11536 :             oe->op = (oe->rank == BIT_IOR_EXPR
    3035                 :       11536 :                       ? boolean_false_node : boolean_true_node);
    3036                 :             :         }
    3037                 :             :       else
    3038                 :       11937 :         oe->op = error_mark_node;
    3039                 :       25386 :       range->exp = NULL_TREE;
    3040                 :       25386 :       range->low = NULL_TREE;
    3041                 :       25386 :       range->high = NULL_TREE;
    3042                 :             :     }
    3043                 :             :   return true;
    3044                 :             : }
    3045                 :             : 
    3046                 :             : /* Optimize X == CST1 || X == CST2
    3047                 :             :    if popcount (CST1 ^ CST2) == 1 into
    3048                 :             :    (X & ~(CST1 ^ CST2)) == (CST1 & ~(CST1 ^ CST2)).
    3049                 :             :    Similarly for ranges.  E.g.
    3050                 :             :    X != 2 && X != 3 && X != 10 && X != 11
    3051                 :             :    will be transformed by the previous optimization into
    3052                 :             :    !((X - 2U) <= 1U || (X - 10U) <= 1U)
    3053                 :             :    and this loop can transform that into
    3054                 :             :    !(((X & ~8) - 2U) <= 1U).  */
    3055                 :             : 
    3056                 :             : static bool
    3057                 :       21280 : optimize_range_tests_xor (enum tree_code opcode, tree type,
    3058                 :             :                           tree lowi, tree lowj, tree highi, tree highj,
    3059                 :             :                           vec<operand_entry *> *ops,
    3060                 :             :                           struct range_entry *rangei,
    3061                 :             :                           struct range_entry *rangej)
    3062                 :             : {
    3063                 :       21280 :   tree lowxor, highxor, tem, exp;
    3064                 :             :   /* Check lowi ^ lowj == highi ^ highj and
    3065                 :             :      popcount (lowi ^ lowj) == 1.  */
    3066                 :       21280 :   lowxor = fold_binary (BIT_XOR_EXPR, type, lowi, lowj);
    3067                 :       21280 :   if (lowxor == NULL_TREE || TREE_CODE (lowxor) != INTEGER_CST)
    3068                 :             :     return false;
    3069                 :       21280 :   if (!integer_pow2p (lowxor))
    3070                 :             :     return false;
    3071                 :        3183 :   highxor = fold_binary (BIT_XOR_EXPR, type, highi, highj);
    3072                 :        3183 :   if (!tree_int_cst_equal (lowxor, highxor))
    3073                 :             :     return false;
    3074                 :             : 
    3075                 :        2774 :   exp = rangei->exp;
    3076                 :        2774 :   scalar_int_mode mode = as_a <scalar_int_mode> (TYPE_MODE (type));
    3077                 :        2774 :   int prec = GET_MODE_PRECISION (mode);
    3078                 :        2774 :   if (TYPE_PRECISION (type) < prec
    3079                 :        2773 :       || (wi::to_wide (TYPE_MIN_VALUE (type))
    3080                 :        8320 :           != wi::min_value (prec, TYPE_SIGN (type)))
    3081                 :        5547 :       || (wi::to_wide (TYPE_MAX_VALUE (type))
    3082                 :        8320 :           != wi::max_value (prec, TYPE_SIGN (type))))
    3083                 :             :     {
    3084                 :           1 :       type = build_nonstandard_integer_type (prec, TYPE_UNSIGNED (type));
    3085                 :           1 :       exp = fold_convert (type, exp);
    3086                 :           1 :       lowxor = fold_convert (type, lowxor);
    3087                 :           1 :       lowi = fold_convert (type, lowi);
    3088                 :           1 :       highi = fold_convert (type, highi);
    3089                 :             :     }
    3090                 :        2774 :   tem = fold_build1 (BIT_NOT_EXPR, type, lowxor);
    3091                 :        2774 :   exp = fold_build2 (BIT_AND_EXPR, type, exp, tem);
    3092                 :        2774 :   lowj = fold_build2 (BIT_AND_EXPR, type, lowi, tem);
    3093                 :        2774 :   highj = fold_build2 (BIT_AND_EXPR, type, highi, tem);
    3094                 :        2774 :   if (update_range_test (rangei, rangej, NULL, 1, opcode, ops, exp,
    3095                 :        2774 :                          NULL, rangei->in_p, lowj, highj,
    3096                 :        2774 :                          rangei->strict_overflow_p
    3097                 :        2774 :                          || rangej->strict_overflow_p))
    3098                 :             :     return true;
    3099                 :             :   return false;
    3100                 :             : }
    3101                 :             : 
    3102                 :             : /* Optimize X == CST1 || X == CST2
    3103                 :             :    if popcount (CST2 - CST1) == 1 into
    3104                 :             :    ((X - CST1) & ~(CST2 - CST1)) == 0.
    3105                 :             :    Similarly for ranges.  E.g.
    3106                 :             :    X == 43 || X == 76 || X == 44 || X == 78 || X == 77 || X == 46
    3107                 :             :    || X == 75 || X == 45
    3108                 :             :    will be transformed by the previous optimization into
    3109                 :             :    (X - 43U) <= 3U || (X - 75U) <= 3U
    3110                 :             :    and this loop can transform that into
    3111                 :             :    ((X - 43U) & ~(75U - 43U)) <= 3U.  */
    3112                 :             : static bool
    3113                 :       16029 : optimize_range_tests_diff (enum tree_code opcode, tree type,
    3114                 :             :                            tree lowi, tree lowj, tree highi, tree highj,
    3115                 :             :                            vec<operand_entry *> *ops,
    3116                 :             :                            struct range_entry *rangei,
    3117                 :             :                            struct range_entry *rangej)
    3118                 :             : {
    3119                 :       16029 :   tree tem1, tem2, mask;
    3120                 :             :   /* Check highi - lowi == highj - lowj.  */
    3121                 :       16029 :   tem1 = fold_binary (MINUS_EXPR, type, highi, lowi);
    3122                 :       16029 :   if (tem1 == NULL_TREE || TREE_CODE (tem1) != INTEGER_CST)
    3123                 :             :     return false;
    3124                 :       16029 :   tem2 = fold_binary (MINUS_EXPR, type, highj, lowj);
    3125                 :       16029 :   if (!tree_int_cst_equal (tem1, tem2))
    3126                 :             :     return false;
    3127                 :             :   /* Check popcount (lowj - lowi) == 1.  */
    3128                 :       11589 :   tem1 = fold_binary (MINUS_EXPR, type, lowj, lowi);
    3129                 :       11589 :   if (tem1 == NULL_TREE || TREE_CODE (tem1) != INTEGER_CST)
    3130                 :             :     return false;
    3131                 :       11589 :   if (!integer_pow2p (tem1))
    3132                 :             :     return false;
    3133                 :             : 
    3134                 :        1928 :   scalar_int_mode mode = as_a <scalar_int_mode> (TYPE_MODE (type));
    3135                 :        1928 :   int prec = GET_MODE_PRECISION (mode);
    3136                 :        1928 :   if (TYPE_PRECISION (type) < prec
    3137                 :        1924 :       || (wi::to_wide (TYPE_MIN_VALUE (type))
    3138                 :        5776 :           != wi::min_value (prec, TYPE_SIGN (type)))
    3139                 :        3852 :       || (wi::to_wide (TYPE_MAX_VALUE (type))
    3140                 :        5776 :           != wi::max_value (prec, TYPE_SIGN (type))))
    3141                 :           4 :     type = build_nonstandard_integer_type (prec, 1);
    3142                 :             :   else
    3143                 :        1924 :     type = unsigned_type_for (type);
    3144                 :        1928 :   tem1 = fold_convert (type, tem1);
    3145                 :        1928 :   tem2 = fold_convert (type, tem2);
    3146                 :        1928 :   lowi = fold_convert (type, lowi);
    3147                 :        1928 :   mask = fold_build1 (BIT_NOT_EXPR, type, tem1);
    3148                 :        1928 :   tem1 = fold_build2 (MINUS_EXPR, type,
    3149                 :             :                       fold_convert (type, rangei->exp), lowi);
    3150                 :        1928 :   tem1 = fold_build2 (BIT_AND_EXPR, type, tem1, mask);
    3151                 :        1928 :   lowj = build_int_cst (type, 0);
    3152                 :        1928 :   if (update_range_test (rangei, rangej, NULL, 1, opcode, ops, tem1,
    3153                 :        1928 :                          NULL, rangei->in_p, lowj, tem2,
    3154                 :        1928 :                          rangei->strict_overflow_p
    3155                 :        1928 :                          || rangej->strict_overflow_p))
    3156                 :             :     return true;
    3157                 :             :   return false;
    3158                 :             : }
    3159                 :             : 
    3160                 :             : /* It does some common checks for function optimize_range_tests_xor and
    3161                 :             :    optimize_range_tests_diff.
    3162                 :             :    If OPTIMIZE_XOR is TRUE, it calls optimize_range_tests_xor.
    3163                 :             :    Else it calls optimize_range_tests_diff.  */
    3164                 :             : 
    3165                 :             : static bool
    3166                 :     1991460 : optimize_range_tests_1 (enum tree_code opcode, int first, int length,
    3167                 :             :                         bool optimize_xor, vec<operand_entry *> *ops,
    3168                 :             :                         struct range_entry *ranges)
    3169                 :             : {
    3170                 :     1991460 :   int i, j;
    3171                 :     1991460 :   bool any_changes = false;
    3172                 :     3470108 :   for (i = first; i < length; i++)
    3173                 :             :     {
    3174                 :     1478648 :       tree lowi, highi, lowj, highj, type, tem;
    3175                 :             : 
    3176                 :     1478648 :       if (ranges[i].exp == NULL_TREE || ranges[i].in_p)
    3177                 :      912188 :         continue;
    3178                 :      566460 :       type = TREE_TYPE (ranges[i].exp);
    3179                 :      566460 :       if (!INTEGRAL_TYPE_P (type))
    3180                 :       43858 :         continue;
    3181                 :      522602 :       lowi = ranges[i].low;
    3182                 :      522602 :       if (lowi == NULL_TREE)
    3183                 :       34156 :         lowi = TYPE_MIN_VALUE (type);
    3184                 :      522602 :       highi = ranges[i].high;
    3185                 :      522602 :       if (highi == NULL_TREE)
    3186                 :        5990 :         continue;
    3187                 :      868795 :       for (j = i + 1; j < length && j < i + 64; j++)
    3188                 :             :         {
    3189                 :      356885 :           bool changes;
    3190                 :      356885 :           if (ranges[i].exp != ranges[j].exp || ranges[j].in_p)
    3191                 :      319576 :             continue;
    3192                 :       37309 :           lowj = ranges[j].low;
    3193                 :       37309 :           if (lowj == NULL_TREE)
    3194                 :           0 :             continue;
    3195                 :       37309 :           highj = ranges[j].high;
    3196                 :       37309 :           if (highj == NULL_TREE)
    3197                 :         120 :             highj = TYPE_MAX_VALUE (type);
    3198                 :             :           /* Check lowj > highi.  */
    3199                 :       37309 :           tem = fold_binary (GT_EXPR, boolean_type_node,
    3200                 :             :                              lowj, highi);
    3201                 :       37309 :           if (tem == NULL_TREE || !integer_onep (tem))
    3202                 :           0 :             continue;
    3203                 :       37309 :           if (optimize_xor)
    3204                 :       21280 :             changes = optimize_range_tests_xor (opcode, type, lowi, lowj,
    3205                 :             :                                                 highi, highj, ops,
    3206                 :             :                                                 ranges + i, ranges + j);
    3207                 :             :           else
    3208                 :       16029 :             changes = optimize_range_tests_diff (opcode, type, lowi, lowj,
    3209                 :             :                                                  highi, highj, ops,
    3210                 :             :                                                  ranges + i, ranges + j);
    3211                 :       37309 :           if (changes)
    3212                 :             :             {
    3213                 :             :               any_changes = true;
    3214                 :             :               break;
    3215                 :             :             }
    3216                 :             :         }
    3217                 :             :     }
    3218                 :     1991460 :   return any_changes;
    3219                 :             : }
    3220                 :             : 
    3221                 :             : /* Helper function of optimize_range_tests_to_bit_test.  Handle a single
    3222                 :             :    range, EXP, LOW, HIGH, compute bit mask of bits to test and return
    3223                 :             :    EXP on success, NULL otherwise.  */
    3224                 :             : 
    3225                 :             : static tree
    3226                 :      159552 : extract_bit_test_mask (tree exp, int prec, tree totallow, tree low, tree high,
    3227                 :             :                        wide_int *mask, tree *totallowp)
    3228                 :             : {
    3229                 :      159552 :   tree tem = int_const_binop (MINUS_EXPR, high, low);
    3230                 :      159552 :   if (tem == NULL_TREE
    3231                 :      159552 :       || TREE_CODE (tem) != INTEGER_CST
    3232                 :      159552 :       || TREE_OVERFLOW (tem)
    3233                 :      148984 :       || tree_int_cst_sgn (tem) == -1
    3234                 :      308536 :       || compare_tree_int (tem, prec) != -1)
    3235                 :       12671 :     return NULL_TREE;
    3236                 :             : 
    3237                 :      146881 :   unsigned HOST_WIDE_INT max = tree_to_uhwi (tem) + 1;
    3238                 :      146881 :   *mask = wi::shifted_mask (0, max, false, prec);
    3239                 :      146881 :   if (TREE_CODE (exp) == BIT_AND_EXPR
    3240                 :      146881 :       && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
    3241                 :             :     {
    3242                 :        4898 :       widest_int msk = wi::to_widest (TREE_OPERAND (exp, 1));
    3243                 :        4898 :       msk = wi::zext (~msk, TYPE_PRECISION (TREE_TYPE (exp)));
    3244                 :        4898 :       if (wi::popcount (msk) == 1
    3245                 :        4898 :           && wi::ltu_p (msk, prec - max))
    3246                 :             :         {
    3247                 :        3863 :           *mask |= wi::shifted_mask (msk.to_uhwi (), max, false, prec);
    3248                 :        3863 :           max += msk.to_uhwi ();
    3249                 :        3863 :           exp = TREE_OPERAND (exp, 0);
    3250                 :        3863 :           if (integer_zerop (low)
    3251                 :        1895 :               && TREE_CODE (exp) == PLUS_EXPR
    3252                 :        5521 :               && TREE_CODE (TREE_OPERAND (exp, 1)) == INTEGER_CST)
    3253                 :             :             {
    3254                 :        1658 :               tree ret = TREE_OPERAND (exp, 0);
    3255                 :        1658 :               STRIP_NOPS (ret);
    3256                 :        1658 :               widest_int bias
    3257                 :        1658 :                 = wi::neg (wi::sext (wi::to_widest (TREE_OPERAND (exp, 1)),
    3258                 :        3316 :                                      TYPE_PRECISION (TREE_TYPE (low))));
    3259                 :        1658 :               tree tbias = wide_int_to_tree (TREE_TYPE (ret), bias);
    3260                 :        1658 :               if (totallowp)
    3261                 :             :                 {
    3262                 :        1624 :                   *totallowp = tbias;
    3263                 :        1624 :                   return ret;
    3264                 :             :                 }
    3265                 :          34 :               else if (!tree_int_cst_lt (totallow, tbias))
    3266                 :             :                 return NULL_TREE;
    3267                 :          34 :               bias = wi::to_widest (tbias);
    3268                 :          34 :               bias -= wi::to_widest (totallow);
    3269                 :          34 :               if (bias >= 0 && bias < prec - max)
    3270                 :             :                 {
    3271                 :          24 :                   *mask = wi::lshift (*mask, bias);
    3272                 :          24 :                   return ret;
    3273                 :             :                 }
    3274                 :        1658 :             }
    3275                 :             :         }
    3276                 :        4898 :     }
    3277                 :      145233 :   if (totallowp)
    3278                 :             :     return exp;
    3279                 :       13000 :   if (!tree_int_cst_lt (totallow, low))
    3280                 :             :     return exp;
    3281                 :       12978 :   tem = int_const_binop (MINUS_EXPR, low, totallow);
    3282                 :       12978 :   if (tem == NULL_TREE
    3283                 :       12978 :       || TREE_CODE (tem) != INTEGER_CST
    3284                 :       12978 :       || TREE_OVERFLOW (tem)
    3285                 :       25847 :       || compare_tree_int (tem, prec - max) == 1)
    3286                 :        3136 :     return NULL_TREE;
    3287                 :             : 
    3288                 :        9842 :   *mask = wi::lshift (*mask, wi::to_widest (tem));
    3289                 :        9842 :   return exp;
    3290                 :             : }
    3291                 :             : 
    3292                 :             : /* Attempt to optimize small range tests using bit test.
    3293                 :             :    E.g.
    3294                 :             :    X != 43 && X != 76 && X != 44 && X != 78 && X != 49
    3295                 :             :    && X != 77 && X != 46 && X != 75 && X != 45 && X != 82
    3296                 :             :    has been by earlier optimizations optimized into:
    3297                 :             :    ((X - 43U) & ~32U) > 3U && X != 49 && X != 82
    3298                 :             :    As all the 43 through 82 range is less than 64 numbers,
    3299                 :             :    for 64-bit word targets optimize that into:
    3300                 :             :    (X - 43U) > 40U && ((1 << (X - 43U)) & 0x8F0000004FULL) == 0  */
    3301                 :             : 
    3302                 :             : static bool
    3303                 :      995736 : optimize_range_tests_to_bit_test (enum tree_code opcode, int first, int length,
    3304                 :             :                                   vec<operand_entry *> *ops,
    3305                 :             :                                   struct range_entry *ranges)
    3306                 :             : {
    3307                 :      995736 :   int i, j;
    3308                 :      995736 :   bool any_changes = false;
    3309                 :      995736 :   int prec = GET_MODE_BITSIZE (word_mode);
    3310                 :      995736 :   auto_vec<struct range_entry *, 64> candidates;
    3311                 :             : 
    3312                 :     1399478 :   for (i = first; i < length - 1; i++)
    3313                 :             :     {
    3314                 :      403742 :       tree lowi, highi, lowj, highj, type;
    3315                 :             : 
    3316                 :      403742 :       if (ranges[i].exp == NULL_TREE || ranges[i].in_p)
    3317                 :      269885 :         continue;
    3318                 :      162442 :       type = TREE_TYPE (ranges[i].exp);
    3319                 :      162442 :       if (!INTEGRAL_TYPE_P (type))
    3320                 :       13907 :         continue;
    3321                 :      148535 :       lowi = ranges[i].low;
    3322                 :      148535 :       if (lowi == NULL_TREE)
    3323                 :       10653 :         lowi = TYPE_MIN_VALUE (type);
    3324                 :      148535 :       highi = ranges[i].high;
    3325                 :      148535 :       if (highi == NULL_TREE)
    3326                 :        2123 :         continue;
    3327                 :      146412 :       wide_int mask;
    3328                 :      146412 :       tree exp = extract_bit_test_mask (ranges[i].exp, prec, lowi, lowi,
    3329                 :             :                                         highi, &mask, &lowi);
    3330                 :      146412 :       if (exp == NULL_TREE)
    3331                 :       12555 :         continue;
    3332                 :      133857 :       bool strict_overflow_p = ranges[i].strict_overflow_p;
    3333                 :      133857 :       candidates.truncate (0);
    3334                 :      133857 :       int end = MIN (i + 64, length);
    3335                 :      291218 :       for (j = i + 1; j < end; j++)
    3336                 :             :         {
    3337                 :      157361 :           tree exp2;
    3338                 :      157361 :           if (ranges[j].exp == NULL_TREE || ranges[j].in_p)
    3339                 :      147507 :             continue;
    3340                 :       94764 :           if (ranges[j].exp == exp)
    3341                 :             :             ;
    3342                 :       81904 :           else if (TREE_CODE (ranges[j].exp) == BIT_AND_EXPR)
    3343                 :             :             {
    3344                 :        1354 :               exp2 = TREE_OPERAND (ranges[j].exp, 0);
    3345                 :        1354 :               if (exp2 == exp)
    3346                 :             :                 ;
    3347                 :        1120 :               else if (TREE_CODE (exp2) == PLUS_EXPR)
    3348                 :             :                 {
    3349                 :         770 :                   exp2 = TREE_OPERAND (exp2, 0);
    3350                 :         770 :                   STRIP_NOPS (exp2);
    3351                 :         770 :                   if (exp2 != exp)
    3352                 :         724 :                     continue;
    3353                 :             :                 }
    3354                 :             :               else
    3355                 :         350 :                 continue;
    3356                 :             :             }
    3357                 :             :           else
    3358                 :       80550 :             continue;
    3359                 :       13140 :           lowj = ranges[j].low;
    3360                 :       13140 :           if (lowj == NULL_TREE)
    3361                 :           0 :             continue;
    3362                 :       13140 :           highj = ranges[j].high;
    3363                 :       13140 :           if (highj == NULL_TREE)
    3364                 :          60 :             highj = TYPE_MAX_VALUE (type);
    3365                 :       13140 :           wide_int mask2;
    3366                 :       13140 :           exp2 = extract_bit_test_mask (ranges[j].exp, prec, lowi, lowj,
    3367                 :             :                                         highj, &mask2, NULL);
    3368                 :       13140 :           if (exp2 != exp)
    3369                 :        3286 :             continue;
    3370                 :        9854 :           mask |= mask2;
    3371                 :        9854 :           strict_overflow_p |= ranges[j].strict_overflow_p;
    3372                 :        9854 :           candidates.safe_push (&ranges[j]);
    3373                 :       13140 :         }
    3374                 :             : 
    3375                 :             :       /* If every possible relative value of the expression is a valid shift
    3376                 :             :          amount, then we can merge the entry test in the bit test.  In this
    3377                 :             :          case, if we would need otherwise 2 or more comparisons, then use
    3378                 :             :          the bit test; in the other cases, the threshold is 3 comparisons.  */
    3379                 :      133857 :       bool entry_test_needed;
    3380                 :      133857 :       value_range r;
    3381                 :      267714 :       if (TREE_CODE (exp) == SSA_NAME
    3382                 :      265752 :           && get_range_query (cfun)->range_of_expr (r, exp)
    3383                 :      132876 :           && !r.undefined_p ()
    3384                 :      132876 :           && !r.varying_p ()
    3385                 :      326212 :           && wi::leu_p (r.upper_bound () - r.lower_bound (), prec - 1))
    3386                 :             :         {
    3387                 :        5226 :           wide_int min = r.lower_bound ();
    3388                 :        5226 :           wide_int ilowi = wi::to_wide (lowi);
    3389                 :        5226 :           if (wi::lt_p (min, ilowi, TYPE_SIGN (TREE_TYPE (lowi))))
    3390                 :             :             {
    3391                 :         608 :               lowi = wide_int_to_tree (TREE_TYPE (lowi), min);
    3392                 :         608 :               mask = wi::lshift (mask, ilowi - min);
    3393                 :             :             }
    3394                 :        4618 :           else if (wi::gt_p (min, ilowi, TYPE_SIGN (TREE_TYPE (lowi))))
    3395                 :             :             {
    3396                 :           1 :               lowi = wide_int_to_tree (TREE_TYPE (lowi), min);
    3397                 :           1 :               mask = wi::lrshift (mask, min - ilowi);
    3398                 :             :             }
    3399                 :        5226 :           entry_test_needed = false;
    3400                 :        5226 :         }
    3401                 :             :       else
    3402                 :             :         entry_test_needed = true;
    3403                 :      272940 :       if (candidates.length () >= (entry_test_needed ? 2 : 1))
    3404                 :             :         {
    3405                 :         906 :           tree high = wide_int_to_tree (TREE_TYPE (lowi),
    3406                 :         453 :                                         wi::to_widest (lowi)
    3407                 :        1359 :                                         + prec - 1 - wi::clz (mask));
    3408                 :         453 :           operand_entry *oe = (*ops)[ranges[i].idx];
    3409                 :         453 :           tree op = oe->op;
    3410                 :         875 :           gimple *stmt = op ? SSA_NAME_DEF_STMT (op)
    3411                 :          31 :                             : last_nondebug_stmt (BASIC_BLOCK_FOR_FN
    3412                 :         453 :                                                           (cfun, oe->id));
    3413                 :         453 :           location_t loc = gimple_location (stmt);
    3414                 :         453 :           tree optype = op ? TREE_TYPE (op) : boolean_type_node;
    3415                 :             : 
    3416                 :             :           /* See if it isn't cheaper to pretend the minimum value of the
    3417                 :             :              range is 0, if maximum value is small enough.
    3418                 :             :              We can avoid then subtraction of the minimum value, but the
    3419                 :             :              mask constant could be perhaps more expensive.  */
    3420                 :         453 :           if (compare_tree_int (lowi, 0) > 0
    3421                 :         453 :               && compare_tree_int (high, prec) < 0)
    3422                 :             :             {
    3423                 :         139 :               int cost_diff;
    3424                 :         139 :               HOST_WIDE_INT m = tree_to_uhwi (lowi);
    3425                 :         139 :               rtx reg = gen_raw_REG (word_mode, 10000);
    3426                 :         139 :               bool speed_p = optimize_bb_for_speed_p (gimple_bb (stmt));
    3427                 :         139 :               cost_diff = set_src_cost (gen_rtx_PLUS (word_mode, reg,
    3428                 :             :                                                       GEN_INT (-m)),
    3429                 :             :                                         word_mode, speed_p);
    3430                 :         139 :               rtx r = immed_wide_int_const (mask, word_mode);
    3431                 :         139 :               cost_diff += set_src_cost (gen_rtx_AND (word_mode, reg, r),
    3432                 :             :                                          word_mode, speed_p);
    3433                 :         139 :               r = immed_wide_int_const (wi::lshift (mask, m), word_mode);
    3434                 :         139 :               cost_diff -= set_src_cost (gen_rtx_AND (word_mode, reg, r),
    3435                 :             :                                          word_mode, speed_p);
    3436                 :         139 :               if (cost_diff > 0)
    3437                 :             :                 {
    3438                 :         139 :                   mask = wi::lshift (mask, m);
    3439                 :         139 :                   lowi = build_zero_cst (TREE_TYPE (lowi));
    3440                 :             :                 }
    3441                 :             :             }
    3442                 :             : 
    3443                 :         453 :           tree tem;
    3444                 :         453 :           if (entry_test_needed)
    3445                 :             :             {
    3446                 :         399 :               tem = build_range_check (loc, optype, unshare_expr (exp),
    3447                 :             :                                        false, lowi, high);
    3448                 :         399 :               if (tem == NULL_TREE || is_gimple_val (tem))
    3449                 :           0 :                 continue;
    3450                 :             :             }
    3451                 :             :           else
    3452                 :          54 :             tem = NULL_TREE;
    3453                 :         453 :           tree etype = unsigned_type_for (TREE_TYPE (exp));
    3454                 :         453 :           exp = fold_build2_loc (loc, MINUS_EXPR, etype,
    3455                 :             :                                  fold_convert_loc (loc, etype, exp),
    3456                 :             :                                  fold_convert_loc (loc, etype, lowi));
    3457                 :         453 :           exp = fold_convert_loc (loc, integer_type_node, exp);
    3458                 :         453 :           tree word_type = lang_hooks.types.type_for_mode (word_mode, 1);
    3459                 :         453 :           exp = fold_build2_loc (loc, LSHIFT_EXPR, word_type,
    3460                 :         453 :                                  build_int_cst (word_type, 1), exp);
    3461                 :         453 :           exp = fold_build2_loc (loc, BIT_AND_EXPR, word_type, exp,
    3462                 :             :                                  wide_int_to_tree (word_type, mask));
    3463                 :         453 :           exp = fold_build2_loc (loc, EQ_EXPR, optype, exp,
    3464                 :             :                                  build_zero_cst (word_type));
    3465                 :         453 :           if (is_gimple_val (exp))
    3466                 :           0 :             continue;
    3467                 :             : 
    3468                 :             :           /* The shift might have undefined behavior if TEM is true,
    3469                 :             :              but reassociate_bb isn't prepared to have basic blocks
    3470                 :             :              split when it is running.  So, temporarily emit a code
    3471                 :             :              with BIT_IOR_EXPR instead of &&, and fix it up in
    3472                 :             :              branch_fixup.  */
    3473                 :         453 :           gimple_seq seq = NULL;
    3474                 :         453 :           if (tem)
    3475                 :             :             {
    3476                 :         399 :               tem = force_gimple_operand (tem, &seq, true, NULL_TREE);
    3477                 :         399 :               gcc_assert (TREE_CODE (tem) == SSA_NAME);
    3478                 :         399 :               gimple_set_visited (SSA_NAME_DEF_STMT (tem), true);
    3479                 :             :             }
    3480                 :         453 :           gimple_seq seq2;
    3481                 :         453 :           exp = force_gimple_operand (exp, &seq2, true, NULL_TREE);
    3482                 :         453 :           gimple_seq_add_seq_without_update (&seq, seq2);
    3483                 :         453 :           gcc_assert (TREE_CODE (exp) == SSA_NAME);
    3484                 :         453 :           gimple_set_visited (SSA_NAME_DEF_STMT (exp), true);
    3485                 :         453 :           if (tem)
    3486                 :             :             {
    3487                 :         399 :               gimple *g = gimple_build_assign (make_ssa_name (optype),
    3488                 :             :                                                BIT_IOR_EXPR, tem, exp);
    3489                 :         399 :               gimple_set_location (g, loc);
    3490                 :         399 :               gimple_seq_add_stmt_without_update (&seq, g);
    3491                 :         399 :               exp = gimple_assign_lhs (g);
    3492                 :             :             }
    3493                 :         453 :           tree val = build_zero_cst (optype);
    3494                 :        1359 :           if (update_range_test (&ranges[i], NULL, candidates.address (),
    3495                 :             :                                  candidates.length (), opcode, ops, exp,
    3496                 :             :                                  seq, false, val, val, strict_overflow_p))
    3497                 :             :             {
    3498                 :         453 :               any_changes = true;
    3499                 :         453 :               if (tem)
    3500                 :         399 :                 reassoc_branch_fixups.safe_push (tem);
    3501                 :             :             }
    3502                 :             :           else
    3503                 :           0 :             gimple_seq_discard (seq);
    3504                 :             :         }
    3505                 :      146412 :     }
    3506                 :      995736 :   return any_changes;
    3507                 :      995736 : }
    3508                 :             : 
    3509                 :             : /* Optimize x != 0 && y != 0 && z != 0 into (x | y | z) != 0
    3510                 :             :    and similarly x != -1 && y != -1 && y != -1 into (x & y & z) != -1.
    3511                 :             :    Also, handle x < C && y < C && z < C where C is power of two as
    3512                 :             :    (x | y | z) < C.  And also handle signed x < 0 && y < 0 && z < 0
    3513                 :             :    as (x | y | z) < 0.  */
    3514                 :             : 
    3515                 :             : static bool
    3516                 :      995736 : optimize_range_tests_cmp_bitwise (enum tree_code opcode, int first, int length,
    3517                 :             :                                   vec<operand_entry *> *ops,
    3518                 :             :                                   struct range_entry *ranges)
    3519                 :             : {
    3520                 :      995736 :   int i;
    3521                 :      995736 :   unsigned int b;
    3522                 :      995736 :   bool any_changes = false;
    3523                 :      995736 :   auto_vec<int, 128> buckets;
    3524                 :      995736 :   auto_vec<int, 32> chains;
    3525                 :      995736 :   auto_vec<struct range_entry *, 32> candidates;
    3526                 :             : 
    3527                 :     1735065 :   for (i = first; i < length; i++)
    3528                 :             :     {
    3529                 :      739329 :       int idx;
    3530                 :             : 
    3531                 :     1086897 :       if (ranges[i].exp == NULL_TREE
    3532                 :      724452 :           || TREE_CODE (ranges[i].exp) != SSA_NAME
    3533                 :      719881 :           || TYPE_PRECISION (TREE_TYPE (ranges[i].exp)) <= 1
    3534                 :     1131098 :           || TREE_CODE (TREE_TYPE (ranges[i].exp)) == BOOLEAN_TYPE)
    3535                 :      347568 :         continue;
    3536                 :             : 
    3537                 :      391761 :       if (ranges[i].low != NULL_TREE
    3538                 :      366487 :           && ranges[i].high != NULL_TREE
    3539                 :      314453 :           && ranges[i].in_p
    3540                 :      567380 :           && tree_int_cst_equal (ranges[i].low, ranges[i].high))
    3541                 :             :         {
    3542                 :      153413 :           idx = !integer_zerop (ranges[i].low);
    3543                 :      153413 :           if (idx && !integer_all_onesp (ranges[i].low))
    3544                 :       81362 :             continue;
    3545                 :             :         }
    3546                 :      238348 :       else if (ranges[i].high != NULL_TREE
    3547                 :      186284 :                && TREE_CODE (ranges[i].high) == INTEGER_CST
    3548                 :      186284 :                && ranges[i].in_p)
    3549                 :             :         {
    3550                 :       30402 :           wide_int w = wi::to_wide (ranges[i].high);
    3551                 :       30402 :           int prec = TYPE_PRECISION (TREE_TYPE (ranges[i].exp));
    3552                 :       30402 :           int l = wi::clz (w);
    3553                 :       30402 :           idx = 2;
    3554                 :       81199 :           if (l <= 0
    3555                 :       30402 :               || l >= prec
    3556                 :       54761 :               || w != wi::mask (prec - l, false, prec))
    3557                 :       20395 :             continue;
    3558                 :       10007 :           if (!((TYPE_UNSIGNED (TREE_TYPE (ranges[i].exp))
    3559                 :        7027 :                  && ranges[i].low == NULL_TREE)
    3560                 :       10007 :                 || (ranges[i].low
    3561                 :        8954 :                     && integer_zerop (ranges[i].low))))
    3562                 :        2639 :             continue;
    3563                 :       30402 :         }
    3564                 :      392733 :       else if (ranges[i].high == NULL_TREE
    3565                 :       52064 :                && ranges[i].low != NULL_TREE
    3566                 :             :                /* Perform this optimization only in the last
    3567                 :             :                   reassoc pass, as it interferes with the reassociation
    3568                 :             :                   itself or could also with VRP etc. which might not
    3569                 :             :                   be able to virtually undo the optimization.  */
    3570                 :       52034 :                && !reassoc_insert_powi_p
    3571                 :       25735 :                && !TYPE_UNSIGNED (TREE_TYPE (ranges[i].exp))
    3572                 :      233680 :                && integer_zerop (ranges[i].low))
    3573                 :             :         idx = 3;
    3574                 :             :       else
    3575                 :      184787 :         continue;
    3576                 :             : 
    3577                 :      102578 :       b = TYPE_PRECISION (TREE_TYPE (ranges[i].exp)) * 4 + idx;
    3578                 :      102578 :       if (buckets.length () <= b)
    3579                 :       85671 :         buckets.safe_grow_cleared (b + 1, true);
    3580                 :      205156 :       if (chains.length () <= (unsigned) i)
    3581                 :      102578 :         chains.safe_grow (i + 1, true);
    3582                 :      102578 :       chains[i] = buckets[b];
    3583                 :      102578 :       buckets[b] = i + 1;
    3584                 :             :     }
    3585                 :             : 
    3586                 :    15664599 :   FOR_EACH_VEC_ELT (buckets, b, i)
    3587                 :    14668863 :     if (i && chains[i - 1])
    3588                 :             :       {
    3589                 :        6317 :         int j, k = i;
    3590                 :        6317 :         if ((b % 4) == 2)
    3591                 :             :           {
    3592                 :             :             /* When ranges[X - 1].high + 1 is a power of two,
    3593                 :             :                we need to process the same bucket up to
    3594                 :             :                precision - 1 times, each time split the entries
    3595                 :             :                with the same high bound into one chain and the
    3596                 :             :                rest into another one to be processed later.  */
    3597                 :             :             int this_prev = i;
    3598                 :             :             int other_prev = 0;
    3599                 :         176 :             for (j = chains[i - 1]; j; j = chains[j - 1])
    3600                 :             :               {
    3601                 :          94 :                 if (tree_int_cst_equal (ranges[i - 1].high,
    3602                 :          94 :                                         ranges[j - 1].high))
    3603                 :             :                   {
    3604                 :          73 :                     chains[this_prev - 1] = j;
    3605                 :          73 :                     this_prev = j;
    3606                 :             :                   }
    3607                 :          21 :                 else if (other_prev == 0)
    3608                 :             :                   {
    3609                 :          19 :                     buckets[b] = j;
    3610                 :          19 :                     other_prev = j;
    3611                 :             :                   }
    3612                 :             :                 else
    3613                 :             :                   {
    3614                 :           2 :                     chains[other_prev - 1] = j;
    3615                 :           2 :                     other_prev = j;
    3616                 :             :                   }
    3617                 :             :               }
    3618                 :          82 :             chains[this_prev - 1] = 0;
    3619                 :          82 :             if (other_prev)
    3620                 :          19 :               chains[other_prev - 1] = 0;
    3621                 :          82 :             if (chains[i - 1] == 0)
    3622                 :             :               {
    3623                 :          17 :                 if (other_prev)
    3624                 :          17 :                   b--;
    3625                 :          17 :                 continue;
    3626                 :             :               }
    3627                 :             :           }
    3628                 :       17323 :         for (j = chains[i - 1]; j; j = chains[j - 1])
    3629                 :             :           {
    3630                 :       11023 :             gimple *gk = SSA_NAME_DEF_STMT (ranges[k - 1].exp);
    3631                 :       11023 :             gimple *gj = SSA_NAME_DEF_STMT (ranges[j - 1].exp);
    3632                 :       11023 :             if (reassoc_stmt_dominates_stmt_p (gk, gj))
    3633                 :        2823 :               k = j;
    3634                 :             :           }
    3635                 :        6300 :         tree type1 = TREE_TYPE (ranges[k - 1].exp);
    3636                 :        6300 :         tree type2 = NULL_TREE;
    3637                 :        6300 :         bool strict_overflow_p = false;
    3638                 :        6300 :         candidates.truncate (0);
    3639                 :        6300 :         if (POINTER_TYPE_P (type1) || TREE_CODE (type1) == OFFSET_TYPE)
    3640                 :         578 :           type1 = pointer_sized_int_node;
    3641                 :       23623 :         for (j = i; j; j = chains[j - 1])
    3642                 :             :           {
    3643                 :       17323 :             tree type = TREE_TYPE (ranges[j - 1].exp);
    3644                 :       17323 :             strict_overflow_p |= ranges[j - 1].strict_overflow_p;
    3645                 :       17323 :             if (POINTER_TYPE_P (type) || TREE_CODE (type) == OFFSET_TYPE)
    3646                 :        1184 :               type = pointer_sized_int_node;
    3647                 :       17323 :             if ((b % 4) == 3)
    3648                 :             :               {
    3649                 :             :                 /* For the signed < 0 cases, the types should be
    3650                 :             :                    really compatible (all signed with the same precision,
    3651                 :             :                    instead put ranges that have different in_p from
    3652                 :             :                    k first.  */
    3653                 :        3263 :                 if (!useless_type_conversion_p (type1, type))
    3654                 :           0 :                   continue;
    3655                 :        3263 :                 if (ranges[j - 1].in_p != ranges[k - 1].in_p)
    3656                 :        1010 :                   candidates.safe_push (&ranges[j - 1]);
    3657                 :        3263 :                 type2 = type1;
    3658                 :        3263 :                 continue;
    3659                 :             :               }
    3660                 :       14060 :             if (j == k
    3661                 :       14060 :                 || useless_type_conversion_p (type1, type))
    3662                 :             :               ;
    3663                 :         373 :             else if (type2 == NULL_TREE
    3664                 :         373 :                      || useless_type_conversion_p (type2, type))
    3665                 :             :               {
    3666                 :         373 :                 if (type2 == NULL_TREE)
    3667                 :         359 :                   type2 = type;
    3668                 :         373 :                 candidates.safe_push (&ranges[j - 1]);
    3669                 :             :               }
    3670                 :             :           }
    3671                 :        6300 :         unsigned l = candidates.length ();
    3672                 :       23623 :         for (j = i; j; j = chains[j - 1])
    3673                 :             :           {
    3674                 :       17323 :             tree type = TREE_TYPE (ranges[j - 1].exp);
    3675                 :       17323 :             if (j == k)
    3676                 :        6300 :               continue;
    3677                 :       11023 :             if (POINTER_TYPE_P (type) || TREE_CODE (type) == OFFSET_TYPE)
    3678                 :         606 :               type = pointer_sized_int_node;
    3679                 :       11023 :             if ((b % 4) == 3)
    3680                 :             :               {
    3681                 :        1798 :                 if (!useless_type_conversion_p (type1, type))
    3682                 :           0 :                   continue;
    3683                 :        1798 :                 if (ranges[j - 1].in_p == ranges[k - 1].in_p)
    3684                 :         788 :                   candidates.safe_push (&ranges[j - 1]);
    3685                 :        1798 :                 continue;
    3686                 :             :               }
    3687                 :        9225 :             if (useless_type_conversion_p (type1, type))
    3688                 :             :               ;
    3689                 :         746 :             else if (type2 == NULL_TREE
    3690                 :         373 :                      || useless_type_conversion_p (type2, type))
    3691                 :         373 :               continue;
    3692                 :        8852 :             candidates.safe_push (&ranges[j - 1]);
    3693                 :             :           }
    3694                 :        6300 :         gimple_seq seq = NULL;
    3695                 :        6300 :         tree op = NULL_TREE;
    3696                 :        6300 :         unsigned int id;
    3697                 :        6300 :         struct range_entry *r;
    3698                 :        6300 :         candidates.safe_push (&ranges[k - 1]);
    3699                 :       23623 :         FOR_EACH_VEC_ELT (candidates, id, r)
    3700                 :             :           {
    3701                 :       17323 :             gimple *g;
    3702                 :       17323 :             enum tree_code code;
    3703                 :       17323 :             if (id == 0)
    3704                 :             :               {
    3705                 :        6300 :                 op = r->exp;
    3706                 :        6300 :                 continue;
    3707                 :             :               }
    3708                 :       11023 :             if (id == l
    3709                 :        9654 :                 || POINTER_TYPE_P (TREE_TYPE (op))
    3710                 :       20180 :                 || TREE_CODE (TREE_TYPE (op)) == OFFSET_TYPE)
    3711                 :             :               {
    3712                 :        1871 :                 code = (b % 4) == 3 ? BIT_NOT_EXPR : NOP_EXPR;
    3713                 :        1871 :                 tree type3 = id >= l ? type1 : pointer_sized_int_node;
    3714                 :        1871 :                 if (code == BIT_NOT_EXPR
    3715                 :        1871 :                     && TREE_CODE (TREE_TYPE (op)) == OFFSET_TYPE)
    3716                 :             :                   {
    3717                 :           0 :                     g = gimple_build_assign (make_ssa_name (type3),
    3718                 :             :                                              NOP_EXPR, op);
    3719                 :           0 :                     gimple_seq_add_stmt_without_update (&seq, g);
    3720                 :           0 :                     op = gimple_assign_lhs (g);
    3721                 :             :                   }
    3722                 :        1871 :                 g = gimple_build_assign (make_ssa_name (type3), code, op);
    3723                 :        1871 :                 gimple_seq_add_stmt_without_update (&seq, g);
    3724                 :        1871 :                 op = gimple_assign_lhs (g);
    3725                 :             :               }
    3726                 :       11023 :             tree type = TREE_TYPE (r->exp);
    3727                 :       11023 :             tree exp = r->exp;
    3728                 :       11023 :             if (POINTER_TYPE_P (type)
    3729                 :       10422 :                 || TREE_CODE (type) == OFFSET_TYPE
    3730                 :       21440 :                 || (id >= l && !useless_type_conversion_p (type1, type)))
    3731                 :             :               {
    3732                 :         606 :                 tree type3 = id >= l ? type1 : pointer_sized_int_node;
    3733                 :         606 :                 g = gimple_build_assign (make_ssa_name (type3), NOP_EXPR, exp);
    3734                 :         606 :                 gimple_seq_add_stmt_without_update (&seq, g);
    3735                 :         606 :                 exp = gimple_assign_lhs (g);
    3736                 :             :               }
    3737                 :       11023 :             if ((b % 4) == 3)
    3738                 :        1798 :               code = r->in_p ? BIT_IOR_EXPR : BIT_AND_EXPR;
    3739                 :             :             else
    3740                 :        9225 :               code = (b % 4) == 1 ? BIT_AND_EXPR : BIT_IOR_EXPR;
    3741                 :       22046 :             g = gimple_build_assign (make_ssa_name (id >= l ? type1 : type2),
    3742                 :             :                                      code, op, exp);
    3743                 :       11023 :             gimple_seq_add_stmt_without_update (&seq, g);
    3744                 :       11023 :             op = gimple_assign_lhs (g);
    3745                 :             :           }
    3746                 :        6300 :         type1 = TREE_TYPE (ranges[k - 1].exp);
    3747                 :        6300 :         if (POINTER_TYPE_P (type1) || TREE_CODE (type1) == OFFSET_TYPE)
    3748                 :             :           {
    3749                 :         578 :             gimple *g
    3750                 :         578 :               = gimple_build_assign (make_ssa_name (type1), NOP_EXPR, op);
    3751                 :         578 :             gimple_seq_add_stmt_without_update (&seq, g);
    3752                 :         578 :             op = gimple_assign_lhs (g);
    3753                 :             :           }
    3754                 :        6300 :         candidates.pop ();
    3755                 :        6300 :         if (update_range_test (&ranges[k - 1], NULL, candidates.address (),
    3756                 :             :                                candidates.length (), opcode, ops, op,
    3757                 :        6300 :                                seq, ranges[k - 1].in_p, ranges[k - 1].low,
    3758                 :             :                                ranges[k - 1].high, strict_overflow_p))
    3759                 :             :           any_changes = true;
    3760                 :             :         else
    3761                 :           0 :           gimple_seq_discard (seq);
    3762                 :        6365 :         if ((b % 4) == 2 && buckets[b] != i)
    3763                 :             :           /* There is more work to do for this bucket.  */
    3764                 :           2 :           b--;
    3765                 :             :       }
    3766                 :             : 
    3767                 :      995736 :   return any_changes;
    3768                 :      995736 : }
    3769                 :             : 
    3770                 :             : /* Attempt to optimize for signed a and b where b is known to be >= 0:
    3771                 :             :    a >= 0 && a < b into (unsigned) a < (unsigned) b
    3772                 :             :    a >= 0 && a <= b into (unsigned) a <= (unsigned) b  */
    3773                 :             : 
    3774                 :             : static bool
    3775                 :      995736 : optimize_range_tests_var_bound (enum tree_code opcode, int first, int length,
    3776                 :             :                                 vec<operand_entry *> *ops,
    3777                 :             :                                 struct range_entry *ranges,
    3778                 :             :                                 basic_block first_bb)
    3779                 :             : {
    3780                 :      995736 :   int i;
    3781                 :      995736 :   bool any_changes = false;
    3782                 :      995736 :   hash_map<tree, int> *map = NULL;
    3783                 :             : 
    3784                 :     1735065 :   for (i = first; i < length; i++)
    3785                 :             :     {
    3786                 :      739329 :       if (ranges[i].exp == NULL_TREE
    3787                 :      724966 :           || TREE_CODE (ranges[i].exp) != SSA_NAME
    3788                 :      720395 :           || !ranges[i].in_p)
    3789                 :      295623 :         continue;
    3790                 :             : 
    3791                 :      443706 :       tree type = TREE_TYPE (ranges[i].exp);
    3792                 :      842789 :       if (!INTEGRAL_TYPE_P (type)
    3793                 :      435102 :           || TYPE_UNSIGNED (type)
    3794                 :      160992 :           || ranges[i].low == NULL_TREE
    3795                 :      152796 :           || !integer_zerop (ranges[i].low)
    3796                 :      512659 :           || ranges[i].high != NULL_TREE)
    3797                 :      399083 :         continue;
    3798                 :             :       /* EXP >= 0 here.  */
    3799                 :       44623 :       if (map == NULL)
    3800                 :       43279 :         map = new hash_map <tree, int>;
    3801                 :       44623 :       map->put (ranges[i].exp, i);
    3802                 :             :     }
    3803                 :             : 
    3804                 :      995736 :   if (map == NULL)
    3805                 :             :     return false;
    3806                 :             : 
    3807                 :      132749 :   for (i = 0; i < length; i++)
    3808                 :             :     {
    3809                 :       89470 :       bool in_p = ranges[i].in_p;
    3810                 :       89470 :       if (ranges[i].low == NULL_TREE
    3811                 :       88833 :           || ranges[i].high == NULL_TREE)
    3812                 :       88956 :         continue;
    3813                 :       42429 :       if (!integer_zerop (ranges[i].low)
    3814                 :       42429 :           || !integer_zerop (ranges[i].high))
    3815                 :             :         {
    3816                 :        7266 :           if (ranges[i].exp
    3817                 :        3633 :               && TYPE_PRECISION (TREE_TYPE (ranges[i].exp)) == 1
    3818                 :           0 :               && TYPE_UNSIGNED (TREE_TYPE (ranges[i].exp))
    3819                 :           0 :               && integer_onep (ranges[i].low)
    3820                 :        3633 :               && integer_onep (ranges[i].high))
    3821                 :           0 :             in_p = !in_p;
    3822                 :             :           else
    3823                 :        3633 :             continue;
    3824                 :             :         }
    3825                 :             : 
    3826                 :       38796 :       gimple *stmt;
    3827                 :       38796 :       tree_code ccode;
    3828                 :       38796 :       tree rhs1, rhs2;
    3829                 :       38796 :       if (ranges[i].exp)
    3830                 :             :         {
    3831                 :       37971 :           if (TREE_CODE (ranges[i].exp) != SSA_NAME)
    3832                 :           5 :             continue;
    3833                 :       37966 :           stmt = SSA_NAME_DEF_STMT (ranges[i].exp);
    3834                 :       37966 :           if (!is_gimple_assign (stmt))
    3835                 :         886 :             continue;
    3836                 :       37080 :           ccode = gimple_assign_rhs_code (stmt);
    3837                 :       37080 :           rhs1 = gimple_assign_rhs1 (stmt);
    3838                 :       37080 :           rhs2 = gimple_assign_rhs2 (stmt);
    3839                 :             :         }
    3840                 :             :       else
    3841                 :             :         {
    3842                 :         825 :           operand_entry *oe = (*ops)[ranges[i].idx];
    3843                 :         825 :           stmt = last_nondebug_stmt (BASIC_BLOCK_FOR_FN (cfun, oe->id));
    3844                 :         825 :           if (gimple_code (stmt) != GIMPLE_COND)
    3845                 :           0 :             continue;
    3846                 :         825 :           ccode = gimple_cond_code (stmt);
    3847                 :         825 :           rhs1 = gimple_cond_lhs (stmt);
    3848                 :         825 :           rhs2 = gimple_cond_rhs (stmt);
    3849                 :             :         }
    3850                 :             : 
    3851                 :       37905 :       if (TREE_CODE (rhs1) != SSA_NAME
    3852                 :       37356 :           || rhs2 == NULL_TREE
    3853                 :       37300 :           || TREE_CODE (rhs2) != SSA_NAME)
    3854                 :         677 :         continue;
    3855                 :             : 
    3856                 :       37228 :       switch (ccode)
    3857                 :             :         {
    3858                 :       36484 :         case GT_EXPR:
    3859                 :       36484 :         case GE_EXPR:
    3860                 :       36484 :         case LT_EXPR:
    3861                 :       36484 :         case LE_EXPR:
    3862                 :       36484 :           break;
    3863                 :         744 :         default:
    3864                 :         744 :           continue;
    3865                 :             :         }
    3866                 :       36484 :       if (in_p)
    3867                 :         573 :         ccode = invert_tree_comparison (ccode, false);
    3868                 :       36484 :       switch (ccode)
    3869                 :             :         {
    3870                 :       14420 :         case GT_EXPR:
    3871                 :       14420 :         case GE_EXPR:
    3872                 :       14420 :           std::swap (rhs1, rhs2);
    3873                 :       14420 :           ccode = swap_tree_comparison (ccode);
    3874                 :       14420 :           break;
    3875                 :             :         case LT_EXPR:
    3876                 :             :         case LE_EXPR:
    3877                 :             :           break;
    3878                 :           0 :         default:
    3879                 :           0 :           gcc_unreachable ();
    3880                 :             :         }
    3881                 :             : 
    3882                 :       36484 :       int *idx = map->get (rhs1);
    3883                 :       36484 :       if (idx == NULL)
    3884                 :         663 :         continue;
    3885                 :             : 
    3886                 :             :       /* maybe_optimize_range_tests allows statements without side-effects
    3887                 :             :          in the basic blocks as long as they are consumed in the same bb.
    3888                 :             :          Make sure rhs2's def stmt is not among them, otherwise we can't
    3889                 :             :          use safely get_nonzero_bits on it.  E.g. in:
    3890                 :             :           # RANGE [-83, 1] NONZERO 173
    3891                 :             :           # k_32 = PHI <k_47(13), k_12(9)>
    3892                 :             :          ...
    3893                 :             :           if (k_32 >= 0)
    3894                 :             :             goto <bb 5>; [26.46%]
    3895                 :             :           else
    3896                 :             :             goto <bb 9>; [73.54%]
    3897                 :             : 
    3898                 :             :           <bb 5> [local count: 140323371]:
    3899                 :             :           # RANGE [0, 1] NONZERO 1
    3900                 :             :           _5 = (int) k_32;
    3901                 :             :           # RANGE [0, 4] NONZERO 4
    3902                 :             :           _21 = _5 << 2;
    3903                 :             :           # RANGE [0, 4] NONZERO 4
    3904                 :             :           iftmp.0_44 = (char) _21;
    3905                 :             :           if (k_32 < iftmp.0_44)
    3906                 :             :             goto <bb 6>; [84.48%]
    3907                 :             :           else
    3908                 :             :             goto <bb 9>; [15.52%]
    3909                 :             :          the ranges on _5/_21/iftmp.0_44 are flow sensitive, assume that
    3910                 :             :          k_32 >= 0.  If we'd optimize k_32 >= 0 to true and k_32 < iftmp.0_44
    3911                 :             :          to (unsigned) k_32 < (unsigned) iftmp.0_44, then we would execute
    3912                 :             :          those stmts even for negative k_32 and the value ranges would be no
    3913                 :             :          longer guaranteed and so the optimization would be invalid.  */
    3914                 :       35822 :       while (opcode == ERROR_MARK)
    3915                 :             :         {
    3916                 :         321 :           gimple *g = SSA_NAME_DEF_STMT (rhs2);
    3917                 :         321 :           basic_block bb2 = gimple_bb (g);
    3918                 :         321 :           if (bb2
    3919                 :         321 :               && bb2 != first_bb
    3920                 :         321 :               && dominated_by_p (CDI_DOMINATORS, bb2, first_bb))
    3921                 :             :             {
    3922                 :             :               /* As an exception, handle a few common cases.  */
    3923                 :         258 :               if (gimple_assign_cast_p (g)
    3924                 :         258 :                   && INTEGRAL_TYPE_P (TREE_TYPE (gimple_assign_rhs1 (g))))
    3925                 :             :                 {
    3926                 :          31 :                   tree op0 = gimple_assign_rhs1 (g);
    3927                 :          31 :                   if (TYPE_UNSIGNED (TREE_TYPE (op0))
    3928                 :          31 :                       && (TYPE_PRECISION (TREE_TYPE (rhs2))
    3929                 :          17 :                           > TYPE_PRECISION (TREE_TYPE (op0))))
    3930                 :             :                     /* Zero-extension is always ok.  */
    3931                 :             :                     break;
    3932                 :          15 :                   else if (TYPE_PRECISION (TREE_TYPE (rhs2))
    3933                 :          15 :                            == TYPE_PRECISION (TREE_TYPE (op0))
    3934                 :          15 :                            && TREE_CODE (op0) == SSA_NAME)
    3935                 :             :                     {
    3936                 :             :                       /* Cast from signed to unsigned or vice versa.  Retry
    3937                 :             :                          with the op0 as new rhs2.  */
    3938                 :           1 :                       rhs2 = op0;
    3939                 :           1 :                       continue;
    3940                 :             :                     }
    3941                 :             :                 }
    3942                 :         227 :               else if (is_gimple_assign (g)
    3943                 :         227 :                        && gimple_assign_rhs_code (g) == BIT_AND_EXPR
    3944                 :           0 :                        && TREE_CODE (gimple_assign_rhs2 (g)) == INTEGER_CST
    3945                 :         454 :                        && !wi::neg_p (wi::to_wide (gimple_assign_rhs2 (g))))
    3946                 :             :                 /* Masking with INTEGER_CST with MSB clear is always ok
    3947                 :             :                    too.  */
    3948                 :             :                 break;
    3949                 :             :               rhs2 = NULL_TREE;
    3950                 :             :             }
    3951                 :             :           break;
    3952                 :             :         }
    3953                 :       35580 :       if (rhs2 == NULL_TREE)
    3954                 :         241 :         continue;
    3955                 :             : 
    3956                 :       36094 :       wide_int nz = get_nonzero_bits (rhs2);
    3957                 :       35580 :       if (wi::neg_p (nz))
    3958                 :       35066 :         continue;
    3959                 :             : 
    3960                 :             :       /* We have EXP < RHS2 or EXP <= RHS2 where EXP >= 0
    3961                 :             :          and RHS2 is known to be RHS2 >= 0.  */
    3962                 :         514 :       tree utype = unsigned_type_for (TREE_TYPE (rhs1));
    3963                 :             : 
    3964                 :         514 :       enum warn_strict_overflow_code wc = WARN_STRICT_OVERFLOW_COMPARISON;
    3965                 :         514 :       if ((ranges[*idx].strict_overflow_p
    3966                 :         514 :            || ranges[i].strict_overflow_p)
    3967                 :           0 :           && issue_strict_overflow_warning (wc))
    3968                 :           0 :         warning_at (gimple_location (stmt), OPT_Wstrict_overflow,
    3969                 :             :                     "assuming signed overflow does not occur "
    3970                 :             :                     "when simplifying range test");
    3971                 :             : 
    3972                 :         514 :       if (dump_file && (dump_flags & TDF_DETAILS))
    3973                 :             :         {
    3974                 :           7 :           struct range_entry *r = &ranges[*idx];
    3975                 :           7 :           fprintf (dump_file, "Optimizing range test ");
    3976                 :           7 :           print_generic_expr (dump_file, r->exp);
    3977                 :           7 :           fprintf (dump_file, " +[");
    3978                 :           7 :           print_generic_expr (dump_file, r->low);
    3979                 :           7 :           fprintf (dump_file, ", ");
    3980                 :           7 :           print_generic_expr (dump_file, r->high);
    3981                 :           7 :           fprintf (dump_file, "] and comparison ");
    3982                 :           7 :           print_generic_expr (dump_file, rhs1);
    3983                 :           7 :           fprintf (dump_file, " %s ", op_symbol_code (ccode));
    3984                 :           7 :           print_generic_expr (dump_file, rhs2);
    3985                 :           7 :           fprintf (dump_file, "\n into (");
    3986                 :           7 :           print_generic_expr (dump_file, utype);
    3987                 :           7 :           fprintf (dump_file, ") ");
    3988                 :           7 :           print_generic_expr (dump_file, rhs1);
    3989                 :           7 :           fprintf (dump_file, " %s (", op_symbol_code (ccode));
    3990                 :           7 :           print_generic_expr (dump_file, utype);
    3991                 :           7 :           fprintf (dump_file, ") ");
    3992                 :           7 :           print_generic_expr (dump_file, rhs2);
    3993                 :           7 :           fprintf (dump_file, "\n");
    3994                 :             :         }
    3995                 :             : 
    3996                 :         514 :       operand_entry *oe = (*ops)[ranges[i].idx];
    3997                 :         514 :       ranges[i].in_p = 0;
    3998                 :         514 :       if (opcode == BIT_IOR_EXPR
    3999                 :         504 :           || (opcode == ERROR_MARK && oe->rank == BIT_IOR_EXPR))
    4000                 :             :         {
    4001                 :          14 :           ranges[i].in_p = 1;
    4002                 :          14 :           ccode = invert_tree_comparison (ccode, false);
    4003                 :             :         }
    4004                 :             : 
    4005                 :         514 :       unsigned int uid = gimple_uid (stmt);
    4006                 :         514 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    4007                 :         514 :       gimple *g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, rhs1);
    4008                 :         514 :       gimple_set_uid (g, uid);
    4009                 :         514 :       rhs1 = gimple_assign_lhs (g);
    4010                 :         514 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    4011                 :         514 :       if (!useless_type_conversion_p (utype, TREE_TYPE (rhs2)))
    4012                 :             :         {
    4013                 :         513 :           g = gimple_build_assign (make_ssa_name (utype), NOP_EXPR, rhs2);
    4014                 :         513 :           gimple_set_uid (g, uid);
    4015                 :         513 :           rhs2 = gimple_assign_lhs (g);
    4016                 :         513 :           gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    4017                 :             :         }
    4018                 :         514 :       if (tree_swap_operands_p (rhs1, rhs2))
    4019                 :             :         {
    4020                 :         480 :           std::swap (rhs1, rhs2);
    4021                 :         480 :           ccode = swap_tree_comparison (ccode);
    4022                 :             :         }
    4023                 :         514 :       if (gimple_code (stmt) == GIMPLE_COND)
    4024                 :             :         {
    4025                 :          14 :           gcond *c = as_a <gcond *> (stmt);
    4026                 :          14 :           gimple_cond_set_code (c, ccode);
    4027                 :          14 :           gimple_cond_set_lhs (c, rhs1);
    4028                 :          14 :           gimple_cond_set_rhs (c, rhs2);
    4029                 :          14 :           update_stmt (stmt);
    4030                 :             :         }
    4031                 :             :       else
    4032                 :             :         {
    4033                 :         500 :           tree ctype = oe->op ? TREE_TYPE (oe->op) : boolean_type_node;
    4034                 :         500 :           if (!INTEGRAL_TYPE_P (ctype)
    4035                 :         500 :               || (TREE_CODE (ctype) != BOOLEAN_TYPE
    4036                 :           2 :                   && TYPE_PRECISION (ctype) != 1))
    4037                 :           2 :             ctype = boolean_type_node;
    4038                 :         500 :           g = gimple_build_assign (make_ssa_name (ctype), ccode, rhs1, rhs2);
    4039                 :         500 :           gimple_set_uid (g, uid);
    4040                 :         500 :           gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    4041                 :         500 :           if (oe->op && ctype != TREE_TYPE (oe->op))
    4042                 :             :             {
    4043                 :           2 :               g = gimple_build_assign (make_ssa_name (TREE_TYPE (oe->op)),
    4044                 :             :                                        NOP_EXPR, gimple_assign_lhs (g));
    4045                 :           2 :               gimple_set_uid (g, uid);
    4046                 :           2 :               gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    4047                 :             :             }
    4048                 :         500 :           ranges[i].exp = gimple_assign_lhs (g);
    4049                 :         500 :           oe->op = ranges[i].exp;
    4050                 :         500 :           ranges[i].low = build_zero_cst (TREE_TYPE (ranges[i].exp));
    4051                 :         500 :           ranges[i].high = ranges[i].low;
    4052                 :             :         }
    4053                 :         514 :       ranges[i].strict_overflow_p = false;
    4054                 :         514 :       oe = (*ops)[ranges[*idx].idx];
    4055                 :             :       /* Now change all the other range test immediate uses, so that
    4056                 :             :          those tests will be optimized away.  */
    4057                 :         514 :       if (opcode == ERROR_MARK)
    4058                 :             :         {
    4059                 :          17 :           if (oe->op)
    4060                 :           7 :             oe->op = build_int_cst (TREE_TYPE (oe->op),
    4061                 :           7 :                                     oe->rank == BIT_IOR_EXPR ? 0 : 1);
    4062                 :             :           else
    4063                 :          10 :             oe->op = (oe->rank == BIT_IOR_EXPR
    4064                 :          10 :                       ? boolean_false_node : boolean_true_node);
    4065                 :             :         }
    4066                 :             :       else
    4067                 :         497 :         oe->op = error_mark_node;
    4068                 :         514 :       ranges[*idx].exp = NULL_TREE;
    4069                 :         514 :       ranges[*idx].low = NULL_TREE;
    4070                 :         514 :       ranges[*idx].high = NULL_TREE;
    4071                 :         514 :       any_changes = true;
    4072                 :             :     }
    4073                 :             : 
    4074                 :       43279 :   delete map;
    4075                 :       43279 :   return any_changes;
    4076                 :             : }
    4077                 :             : 
    4078                 :             : /* Optimize range tests, similarly how fold_range_test optimizes
    4079                 :             :    it on trees.  The tree code for the binary
    4080                 :             :    operation between all the operands is OPCODE.
    4081                 :             :    If OPCODE is ERROR_MARK, optimize_range_tests is called from within
    4082                 :             :    maybe_optimize_range_tests for inter-bb range optimization.
    4083                 :             :    In that case if oe->op is NULL, oe->id is bb->index whose
    4084                 :             :    GIMPLE_COND is && or ||ed into the test, and oe->rank says
    4085                 :             :    the actual opcode.
    4086                 :             :    FIRST_BB is the first basic block if OPCODE is ERROR_MARK.  */
    4087                 :             : 
    4088                 :             : static bool
    4089                 :      995823 : optimize_range_tests (enum tree_code opcode,
    4090                 :             :                       vec<operand_entry *> *ops, basic_block first_bb)
    4091                 :             : {
    4092                 :      995823 :   unsigned int length = ops->length (), i, j, first;
    4093                 :      995823 :   operand_entry *oe;
    4094                 :      995823 :   struct range_entry *ranges;
    4095                 :     1991559 :   bool any_changes = false;
    4096                 :             : 
    4097                 :      995823 :   if (length == 1)
    4098                 :             :     return false;
    4099                 :             : 
    4100                 :      995736 :   ranges = XNEWVEC (struct range_entry, length);
    4101                 :     4113194 :   for (i = 0; i < length; i++)
    4102                 :             :     {
    4103                 :     2121722 :       oe = (*ops)[i];
    4104                 :     2121722 :       ranges[i].idx = i;
    4105                 :     2121722 :       init_range_entry (ranges + i, oe->op,
    4106                 :     2121722 :                         oe->op
    4107                 :             :                         ? NULL
    4108                 :      226195 :                         : last_nondebug_stmt (BASIC_BLOCK_FOR_FN (cfun, oe->id)));
    4109                 :             :       /* For | invert it now, we will invert it again before emitting
    4110                 :             :          the optimized expression.  */
    4111                 :     2121722 :       if (opcode == BIT_IOR_EXPR
    4112                 :     1474852 :           || (opcode == ERROR_MARK && oe->rank == BIT_IOR_EXPR))
    4113                 :      820743 :         ranges[i].in_p = !ranges[i].in_p;
    4114                 :             :     }
    4115                 :             : 
    4116                 :      995736 :   qsort (ranges, length, sizeof (*ranges), range_entry_cmp);
    4117                 :     3373865 :   for (i = 0; i < length; i++)
    4118                 :     1717980 :     if (ranges[i].exp != NULL_TREE && TREE_CODE (ranges[i].exp) == SSA_NAME)
    4119                 :             :       break;
    4120                 :             : 
    4121                 :             :   /* Try to merge ranges.  */
    4122                 :     1726414 :   for (first = i; i < length; i++)
    4123                 :             :     {
    4124                 :      730678 :       tree low = ranges[i].low;
    4125                 :      730678 :       tree high = ranges[i].high;
    4126                 :      730678 :       int in_p = ranges[i].in_p;
    4127                 :      730678 :       bool strict_overflow_p = ranges[i].strict_overflow_p;
    4128                 :      730678 :       int update_fail_count = 0;
    4129                 :             : 
    4130                 :      739329 :       for (j = i + 1; j < length; j++)
    4131                 :             :         {
    4132                 :      403742 :           if (ranges[i].exp != ranges[j].exp)
    4133                 :             :             break;
    4134                 :       28054 :           if (!merge_ranges (&in_p, &low, &high, in_p, low, high,
    4135                 :       28054 :                              ranges[j].in_p, ranges[j].low, ranges[j].high))
    4136                 :             :             break;
    4137                 :        8651 :           strict_overflow_p |= ranges[j].strict_overflow_p;
    4138                 :             :         }
    4139                 :             : 
    4140                 :      730678 :       if (j == i + 1)
    4141                 :      722549 :         continue;
    4142                 :             : 
    4143                 :        8129 :       if (update_range_test (ranges + i, ranges + i + 1, NULL, j - i - 1,
    4144                 :             :                              opcode, ops, ranges[i].exp, NULL, in_p,
    4145                 :             :                              low, high, strict_overflow_p))
    4146                 :             :         {
    4147                 :        8129 :           i = j - 1;
    4148                 :        8129 :           any_changes = true;
    4149                 :             :         }
    4150                 :             :       /* Avoid quadratic complexity if all merge_ranges calls would succeed,
    4151                 :             :          while update_range_test would fail.  */
    4152                 :             :       else if (update_fail_count == 64)
    4153                 :             :         i = j - 1;
    4154                 :             :       else
    4155                 :        8129 :         ++update_fail_count;
    4156                 :             :     }
    4157                 :             : 
    4158                 :      995736 :   any_changes |= optimize_range_tests_1 (opcode, first, length, true,
    4159                 :             :                                          ops, ranges);
    4160                 :             : 
    4161                 :      995736 :   if (BRANCH_COST (optimize_function_for_speed_p (cfun), false) >= 2)
    4162                 :      995724 :     any_changes |= optimize_range_tests_1 (opcode, first, length, false,
    4163                 :             :                                            ops, ranges);
    4164                 :      995736 :   if (lshift_cheap_p (optimize_function_for_speed_p (cfun)))
    4165                 :      995736 :     any_changes |= optimize_range_tests_to_bit_test (opcode, first, length,
    4166                 :             :                                                      ops, ranges);
    4167                 :      995736 :   any_changes |= optimize_range_tests_var_bound (opcode, first, length, ops,
    4168                 :             :                                                  ranges, first_bb);
    4169                 :      995736 :   any_changes |= optimize_range_tests_cmp_bitwise (opcode, first, length,
    4170                 :             :                                                    ops, ranges);
    4171                 :             : 
    4172                 :      995736 :   if (any_changes && opcode != ERROR_MARK)
    4173                 :             :     {
    4174                 :             :       j = 0;
    4175                 :       36655 :       FOR_EACH_VEC_ELT (*ops, i, oe)
    4176                 :             :         {
    4177                 :       25603 :           if (oe->op == error_mark_node)
    4178                 :       12434 :             continue;
    4179                 :       13169 :           else if (i != j)
    4180                 :        5418 :             (*ops)[j] = oe;
    4181                 :       13169 :           j++;
    4182                 :             :         }
    4183                 :       11052 :       ops->truncate (j);
    4184                 :             :     }
    4185                 :             : 
    4186                 :      995736 :   XDELETEVEC (ranges);
    4187                 :      995736 :   return any_changes;
    4188                 :             : }
    4189                 :             : 
    4190                 :             : /* A subroutine of optimize_vec_cond_expr to extract and canonicalize
    4191                 :             :    the operands of the VEC_COND_EXPR.  Returns ERROR_MARK on failure,
    4192                 :             :    otherwise the comparison code.  TYPE is a return value that is set
    4193                 :             :    to type of comparison.  */
    4194                 :             : 
    4195                 :             : static tree_code
    4196                 :       24064 : ovce_extract_ops (tree var, gassign **rets, bool *reti, tree *type,
    4197                 :             :                   tree *lhs, tree *rhs, gassign **vcond)
    4198                 :             : {
    4199                 :       24064 :   if (TREE_CODE (var) != SSA_NAME)
    4200                 :             :     return ERROR_MARK;
    4201                 :             : 
    4202                 :       20522 :   gassign *stmt = dyn_cast <gassign *> (SSA_NAME_DEF_STMT (var));
    4203                 :       19204 :   if (stmt == NULL)
    4204                 :             :     return ERROR_MARK;
    4205                 :       19204 :   if (vcond)
    4206                 :       19204 :     *vcond = stmt;
    4207                 :             : 
    4208                 :             :   /* ??? If we start creating more COND_EXPR, we could perform
    4209                 :             :      this same optimization with them.  For now, simplify.  */
    4210                 :       24667 :   if (gimple_assign_rhs_code (stmt) != VEC_COND_EXPR)
    4211                 :             :     return ERROR_MARK;
    4212                 :             : 
    4213                 :        1126 :   tree cond = gimple_assign_rhs1 (stmt);
    4214                 :        1126 :   tree_code cmp = TREE_CODE (cond);
    4215                 :        1126 :   if (cmp != SSA_NAME)
    4216                 :             :     return ERROR_MARK;
    4217                 :             : 
    4218                 :       25188 :   gassign *assign = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (cond));
    4219                 :        1124 :   if (assign == NULL
    4220                 :        1124 :       || TREE_CODE_CLASS (gimple_assign_rhs_code (assign)) != tcc_comparison)
    4221                 :             :     return ERROR_MARK;
    4222                 :             : 
    4223                 :        1059 :   cmp = gimple_assign_rhs_code (assign);
    4224                 :        1059 :   if (lhs)
    4225                 :        1059 :     *lhs = gimple_assign_rhs1 (assign);
    4226                 :        1059 :   if (rhs)
    4227                 :        2118 :     *rhs = gimple_assign_rhs2 (assign);
    4228                 :             : 
    4229                 :             :   /* ??? For now, allow only canonical true and false result vectors.
    4230                 :             :      We could expand this to other constants should the need arise,
    4231                 :             :      but at the moment we don't create them.  */
    4232                 :        1059 :   tree t = gimple_assign_rhs2 (stmt);
    4233                 :        1059 :   tree f = gimple_assign_rhs3 (stmt);
    4234                 :        1059 :   bool inv;
    4235                 :        1059 :   if (integer_all_onesp (t))
    4236                 :             :     inv = false;
    4237                 :        1059 :   else if (integer_all_onesp (f))
    4238                 :             :     {
    4239                 :          16 :       cmp = invert_tree_comparison (cmp, false);
    4240                 :          16 :       inv = true;
    4241                 :             :     }
    4242                 :             :   else
    4243                 :             :     return ERROR_MARK;
    4244                 :          16 :   if (!integer_zerop (f))
    4245                 :             :     return ERROR_MARK;
    4246                 :             : 
    4247                 :             :   /* Success!  */
    4248                 :           0 :   if (rets)
    4249                 :           0 :     *rets = assign;
    4250                 :           0 :   if (reti)
    4251                 :           0 :     *reti = inv;
    4252                 :           0 :   if (type)
    4253                 :           0 :     *type = TREE_TYPE (cond);
    4254                 :             :   return cmp;
    4255                 :             : }
    4256                 :             : 
    4257                 :             : /* Optimize the condition of VEC_COND_EXPRs which have been combined
    4258                 :             :    with OPCODE (either BIT_AND_EXPR or BIT_IOR_EXPR).  */
    4259                 :             : 
    4260                 :             : static bool
    4261                 :       10674 : optimize_vec_cond_expr (tree_code opcode, vec<operand_entry *> *ops)
    4262                 :             : {
    4263                 :       10674 :   unsigned int length = ops->length (), i, j;
    4264                 :       10674 :   bool any_changes = false;
    4265                 :             : 
    4266                 :       10674 :   if (length == 1)
    4267                 :             :     return false;
    4268                 :             : 
    4269                 :       34672 :   for (i = 0; i < length; ++i)
    4270                 :             :     {
    4271                 :       24064 :       tree elt0 = (*ops)[i]->op;
    4272                 :             : 
    4273                 :       24064 :       gassign *stmt0, *vcond0;
    4274                 :       24064 :       bool invert;
    4275                 :       24064 :       tree type, lhs0, rhs0;
    4276                 :       24064 :       tree_code cmp0 = ovce_extract_ops (elt0, &stmt0, &invert, &type, &lhs0,
    4277                 :             :                                          &rhs0, &vcond0);
    4278                 :       24064 :       if (cmp0 == ERROR_MARK)
    4279                 :       24064 :         continue;
    4280                 :             : 
    4281                 :           0 :       for (j = i + 1; j < length; ++j)
    4282                 :             :         {
    4283                 :           0 :           tree &elt1 = (*ops)[j]->op;
    4284                 :             : 
    4285                 :           0 :           gassign *stmt1, *vcond1;
    4286                 :           0 :           tree lhs1, rhs1;
    4287                 :           0 :           tree_code cmp1 = ovce_extract_ops (elt1, &stmt1, NULL, NULL, &lhs1,
    4288                 :             :                                              &rhs1, &vcond1);
    4289                 :           0 :           if (cmp1 == ERROR_MARK)
    4290                 :           0 :             continue;
    4291                 :             : 
    4292                 :           0 :           tree comb;
    4293                 :           0 :           if (opcode == BIT_AND_EXPR)
    4294                 :           0 :             comb = maybe_fold_and_comparisons (type, cmp0, lhs0, rhs0,
    4295                 :             :                                                cmp1, lhs1, rhs1);
    4296                 :           0 :           else if (opcode == BIT_IOR_EXPR)
    4297                 :           0 :             comb = maybe_fold_or_comparisons (type, cmp0, lhs0, rhs0,
    4298                 :             :                                               cmp1, lhs1, rhs1);
    4299                 :             :           else
    4300                 :           0 :             gcc_unreachable ();
    4301                 :           0 :           if (comb == NULL)
    4302                 :           0 :             continue;
    4303                 :             : 
    4304                 :             :           /* Success! */
    4305                 :           0 :           if (dump_file && (dump_flags & TDF_DETAILS))
    4306                 :             :             {
    4307                 :           0 :               fprintf (dump_file, "Transforming ");
    4308                 :           0 :               print_generic_expr (dump_file, gimple_assign_lhs (stmt0));
    4309                 :           0 :               fprintf (dump_file, " %c ", opcode == BIT_AND_EXPR ? '&' : '|');
    4310                 :           0 :               print_generic_expr (dump_file, gimple_assign_lhs (stmt1));
    4311                 :           0 :               fprintf (dump_file, " into ");
    4312                 :           0 :               print_generic_expr (dump_file, comb);
    4313                 :           0 :               fputc ('\n', dump_file);
    4314                 :             :             }
    4315                 :             : 
    4316                 :           0 :           gimple_stmt_iterator gsi = gsi_for_stmt (vcond0);
    4317                 :           0 :           tree exp = force_gimple_operand_gsi (&gsi, comb, true, NULL_TREE,
    4318                 :             :                                                true, GSI_SAME_STMT);
    4319                 :           0 :           if (invert)
    4320                 :           0 :             swap_ssa_operands (vcond0, gimple_assign_rhs2_ptr (vcond0),
    4321                 :             :                                gimple_assign_rhs3_ptr (vcond0));
    4322                 :           0 :           gimple_assign_set_rhs1 (vcond0, exp);
    4323                 :           0 :           update_stmt (vcond0);
    4324                 :             : 
    4325                 :           0 :           elt1 = error_mark_node;
    4326                 :           0 :           any_changes = true;
    4327                 :             :         }
    4328                 :             :     }
    4329                 :             : 
    4330                 :       10608 :   if (any_changes)
    4331                 :             :     {
    4332                 :             :       operand_entry *oe;
    4333                 :             :       j = 0;
    4334                 :           0 :       FOR_EACH_VEC_ELT (*ops, i, oe)
    4335                 :             :         {
    4336                 :           0 :           if (oe->op == error_mark_node)
    4337                 :           0 :             continue;
    4338                 :           0 :           else if (i != j)
    4339                 :           0 :             (*ops)[j] = oe;
    4340                 :           0 :           j++;
    4341                 :             :         }
    4342                 :           0 :       ops->truncate (j);
    4343                 :             :     }
    4344                 :             : 
    4345                 :             :   return any_changes;
    4346                 :             : }
    4347                 :             : 
    4348                 :             : /* Return true if STMT is a cast like:
    4349                 :             :    <bb N>:
    4350                 :             :    ...
    4351                 :             :    _123 = (int) _234;
    4352                 :             : 
    4353                 :             :    <bb M>:
    4354                 :             :    # _345 = PHI <_123(N), 1(...), 1(...)>
    4355                 :             :    where _234 has bool type, _123 has single use and
    4356                 :             :    bb N has a single successor M.  This is commonly used in
    4357                 :             :    the last block of a range test.
    4358                 :             : 
    4359                 :             :    Also Return true if STMT is tcc_compare like:
    4360                 :             :    <bb N>:
    4361                 :             :    ...
    4362                 :             :    _234 = a_2(D) == 2;
    4363                 :             : 
    4364                 :             :    <bb M>:
    4365                 :             :    # _345 = PHI <_234(N), 1(...), 1(...)>
    4366                 :             :    _346 = (int) _345;
    4367                 :             :    where _234 has booltype, single use and
    4368                 :             :    bb N has a single successor M.  This is commonly used in
    4369                 :             :    the last block of a range test.  */
    4370                 :             : 
    4371                 :             : static bool
    4372                 :    13941722 : final_range_test_p (gimple *stmt)
    4373                 :             : {
    4374                 :    13941722 :   basic_block bb, rhs_bb, lhs_bb;
    4375                 :    13941722 :   edge e;
    4376                 :    13941722 :   tree lhs, rhs;
    4377                 :    13941722 :   use_operand_p use_p;
    4378                 :    13941722 :   gimple *use_stmt;
    4379                 :             : 
    4380                 :    13941722 :   if (!gimple_assign_cast_p (stmt)
    4381                 :    13941722 :       && (!is_gimple_assign (stmt)
    4382                 :     4362845 :           || (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
    4383                 :             :               != tcc_comparison)))
    4384                 :             :     return false;
    4385                 :      533593 :   bb = gimple_bb (stmt);
    4386                 :     1067186 :   if (!single_succ_p (bb))
    4387                 :             :     return false;
    4388                 :      533191 :   e = single_succ_edge (bb);
    4389                 :      533191 :   if (e->flags & EDGE_COMPLEX)
    4390                 :             :     return false;
    4391                 :             : 
    4392                 :      533191 :   lhs = gimple_assign_lhs (stmt);
    4393                 :      533191 :   rhs = gimple_assign_rhs1 (stmt);
    4394                 :      533191 :   if (gimple_assign_cast_p (stmt)
    4395                 :      533191 :       && (!INTEGRAL_TYPE_P (TREE_TYPE (lhs))
    4396                 :      381596 :           || TREE_CODE (rhs) != SSA_NAME
    4397                 :      369971 :           || TREE_CODE (TREE_TYPE (rhs)) != BOOLEAN_TYPE))
    4398                 :             :     return false;
    4399                 :             : 
    4400                 :      194315 :   if (!gimple_assign_cast_p (stmt)
    4401                 :      194315 :       && (TREE_CODE (TREE_TYPE (lhs)) != BOOLEAN_TYPE))
    4402                 :             :       return false;
    4403                 :             : 
    4404                 :             :   /* Test whether lhs is consumed only by a PHI in the only successor bb.  */
    4405                 :      194309 :   if (!single_imm_use (lhs, &use_p, &use_stmt))
    4406                 :             :     return false;
    4407                 :             : 
    4408                 :      188827 :   if (gimple_code (use_stmt) != GIMPLE_PHI
    4409                 :      188827 :       || gimple_bb (use_stmt) != e->dest)
    4410                 :             :     return false;
    4411                 :             : 
    4412                 :             :   /* And that the rhs is defined in the same loop.  */
    4413                 :      187301 :   if (gimple_assign_cast_p (stmt))
    4414                 :             :     {
    4415                 :       64595 :       if (TREE_CODE (rhs) != SSA_NAME
    4416                 :       64595 :           || !(rhs_bb = gimple_bb (SSA_NAME_DEF_STMT (rhs)))
    4417                 :      129184 :           || !flow_bb_inside_loop_p (loop_containing_stmt (stmt), rhs_bb))
    4418                 :          11 :         return false;
    4419                 :             :     }
    4420                 :             :   else
    4421                 :             :     {
    4422                 :      122706 :       if (TREE_CODE (lhs) != SSA_NAME
    4423                 :      122706 :           || !(lhs_bb = gimple_bb (SSA_NAME_DEF_STMT (lhs)))
    4424                 :      245412 :           || !flow_bb_inside_loop_p (loop_containing_stmt (stmt), lhs_bb))
    4425                 :           0 :         return false;
    4426                 :             :     }
    4427                 :             : 
    4428                 :             :   return true;
    4429                 :             : }
    4430                 :             : 
    4431                 :             : /* Return true if BB is suitable basic block for inter-bb range test
    4432                 :             :    optimization.  If BACKWARD is true, BB should be the only predecessor
    4433                 :             :    of TEST_BB, and *OTHER_BB is either NULL and filled by the routine,
    4434                 :             :    or compared with to find a common basic block to which all conditions
    4435                 :             :    branch to if true resp. false.  If BACKWARD is false, TEST_BB should
    4436                 :             :    be the only predecessor of BB.  *TEST_SWAPPED_P is set to true if
    4437                 :             :    TEST_BB is a bb ending in condition where the edge to non-*OTHER_BB
    4438                 :             :    block points to an empty block that falls through into *OTHER_BB and
    4439                 :             :    the phi args match that path.  */
    4440                 :             : 
    4441                 :             : static bool
    4442                 :     9701697 : suitable_cond_bb (basic_block bb, basic_block test_bb, basic_block *other_bb,
    4443                 :             :                   bool *test_swapped_p, bool backward)
    4444                 :             : {
    4445                 :     9701697 :   edge_iterator ei, ei2;
    4446                 :     9701697 :   edge e, e2;
    4447                 :     9701697 :   gimple *stmt;
    4448                 :     9701697 :   gphi_iterator gsi;
    4449                 :     9701697 :   bool other_edge_seen = false;
    4450                 :     9701697 :   bool is_cond;
    4451                 :             : 
    4452                 :     9701697 :   if (test_bb == bb)
    4453                 :             :     return false;
    4454                 :             :   /* Check last stmt first.  */
    4455                 :     9701697 :   stmt = last_nondebug_stmt (bb);
    4456                 :     9701697 :   if (stmt == NULL
    4457                 :     8897146 :       || (gimple_code (stmt) != GIMPLE_COND
    4458                 :      483918 :           && (backward || !final_range_test_p (stmt)))
    4459                 :     8444933 :       || gimple_visited_p (stmt)
    4460                 :     8421696 :       || stmt_could_throw_p (cfun, stmt)
    4461                 :    18123281 :       || *other_bb == bb)
    4462                 :     1280120 :     return false;
    4463                 :     8421577 :   is_cond = gimple_code (stmt) == GIMPLE_COND;
    4464                 :     8421577 :   if (is_cond)
    4465                 :             :     {
    4466                 :             :       /* If last stmt is GIMPLE_COND, verify that one of the succ edges
    4467                 :             :          goes to the next bb (if BACKWARD, it is TEST_BB), and the other
    4468                 :             :          to *OTHER_BB (if not set yet, try to find it out).  */
    4469                 :    16059357 :       if (EDGE_COUNT (bb->succs) != 2)
    4470                 :             :         return false;
    4471                 :    16596185 :       FOR_EACH_EDGE (e, ei, bb->succs)
    4472                 :             :         {
    4473                 :    13639377 :           if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
    4474                 :             :             return false;
    4475                 :    13639377 :           if (e->dest == test_bb)
    4476                 :             :             {
    4477                 :     4665424 :               if (backward)
    4478                 :     4663223 :                 continue;
    4479                 :             :               else
    4480                 :             :                 return false;
    4481                 :             :             }
    4482                 :     8973953 :           if (e->dest == bb)
    4483                 :             :             return false;
    4484                 :     8848838 :           if (*other_bb == NULL)
    4485                 :             :             {
    4486                 :    22078710 :               FOR_EACH_EDGE (e2, ei2, test_bb->succs)
    4487                 :    14719140 :                 if (!(e2->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE)))
    4488                 :             :                   return false;
    4489                 :    14719140 :                 else if (e->dest == e2->dest)
    4490                 :     2143493 :                   *other_bb = e->dest;
    4491                 :     7359570 :               if (*other_bb == NULL)
    4492                 :             :                 return false;
    4493                 :             :             }
    4494                 :     3632761 :           if (e->dest == *other_bb)
    4495                 :             :             other_edge_seen = true;
    4496                 :      702439 :           else if (backward)
    4497                 :             :             return false;
    4498                 :             :         }
    4499                 :     2956808 :       if (*other_bb == NULL || !other_edge_seen)
    4500                 :             :         return false;
    4501                 :             :     }
    4502                 :       31593 :   else if (single_succ (bb) != *other_bb)
    4503                 :             :     return false;
    4504                 :             : 
    4505                 :             :   /* Now check all PHIs of *OTHER_BB.  */
    4506                 :     2961457 :   e = find_edge (bb, *other_bb);
    4507                 :     2961457 :   e2 = find_edge (test_bb, *other_bb);
    4508                 :     2970228 :  retry:;
    4509                 :     4236246 :   for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); gsi_next (&gsi))
    4510                 :             :     {
    4511                 :     2203922 :       gphi *phi = gsi.phi ();
    4512                 :             :       /* If both BB and TEST_BB end with GIMPLE_COND, all PHI arguments
    4513                 :             :          corresponding to BB and TEST_BB predecessor must be the same.  */
    4514                 :     2203922 :       if (!operand_equal_p (gimple_phi_arg_def (phi, e->dest_idx),
    4515                 :     2203922 :                             gimple_phi_arg_def (phi, e2->dest_idx), 0))
    4516                 :             :         {
    4517                 :             :           /* Otherwise, if one of the blocks doesn't end with GIMPLE_COND,
    4518                 :             :              one of the PHIs should have the lhs of the last stmt in
    4519                 :             :              that block as PHI arg and that PHI should have 0 or 1
    4520                 :             :              corresponding to it in all other range test basic blocks
    4521                 :             :              considered.  */
    4522                 :     1001902 :           if (!is_cond)
    4523                 :             :             {
    4524                 :       33165 :               if (gimple_phi_arg_def (phi, e->dest_idx)
    4525                 :       33165 :                   == gimple_assign_lhs (stmt)
    4526                 :       33165 :                   && (integer_zerop (gimple_phi_arg_def (phi, e2->dest_idx))
    4527                 :       14328 :                       || integer_onep (gimple_phi_arg_def (phi,
    4528                 :       14328 :                                                            e2->dest_idx))))
    4529                 :       29827 :                 continue;
    4530                 :             :             }
    4531                 :             :           else
    4532                 :             :             {
    4533                 :      968737 :               gimple *test_last = last_nondebug_stmt (test_bb);
    4534                 :      968737 :               if (gimple_code (test_last) == GIMPLE_COND)
    4535                 :             :                 {
    4536                 :      931231 :                   if (backward ? e2->src != test_bb : e->src != bb)
    4537                 :             :                     return false;
    4538                 :             : 
    4539                 :             :                   /* For last_bb, handle also:
    4540                 :             :                      if (x_3(D) == 3)
    4541                 :             :                        goto <bb 6>; [34.00%]
    4542                 :             :                      else
    4543                 :             :                        goto <bb 7>; [66.00%]
    4544                 :             : 
    4545                 :             :                      <bb 6> [local count: 79512730]:
    4546                 :             : 
    4547                 :             :                      <bb 7> [local count: 1073741824]:
    4548                 :             :                      # prephitmp_7 = PHI <1(3), 1(4), 0(5), 1(2), 1(6)>
    4549                 :             :                      where bb 7 is *OTHER_BB, but the PHI values from the
    4550                 :             :                      earlier bbs match the path through the empty bb
    4551                 :             :                      in between.  */
    4552                 :      926765 :                   edge e3;
    4553                 :      926765 :                   if (backward)
    4554                 :     1206818 :                     e3 = EDGE_SUCC (test_bb,
    4555                 :             :                                     e2 == EDGE_SUCC (test_bb, 0) ? 1 : 0);
    4556                 :             :                   else
    4557                 :        6360 :                     e3 = EDGE_SUCC (bb,
    4558                 :             :                                     e == EDGE_SUCC (bb, 0) ? 1 : 0);
    4559                 :      926765 :                   if (empty_block_p (e3->dest)
    4560                 :       31065 :                       && single_succ_p (e3->dest)
    4561                 :       31065 :                       && single_succ (e3->dest) == *other_bb
    4562                 :      955012 :                       && single_pred_p (e3->dest)
    4563                 :      957110 :                       && single_succ_edge (e3->dest)->flags == EDGE_FALLTHRU)
    4564                 :             :                     {
    4565                 :        8771 :                       if (backward)
    4566                 :        7572 :                         e2 = single_succ_edge (e3->dest);
    4567                 :             :                       else
    4568                 :        1199 :                         e = single_succ_edge (e3->dest);
    4569                 :        8771 :                       if (test_swapped_p)
    4570                 :         384 :                         *test_swapped_p = true;
    4571                 :        8771 :                       goto retry;
    4572                 :             :                     }
    4573                 :             :                 }
    4574                 :       37506 :               else if (gimple_phi_arg_def (phi, e2->dest_idx)
    4575                 :       37506 :                        == gimple_assign_lhs (test_last)
    4576                 :       72717 :                        && (integer_zerop (gimple_phi_arg_def (phi,
    4577                 :       35211 :                                                               e->dest_idx))
    4578                 :       16357 :                            || integer_onep (gimple_phi_arg_def (phi,
    4579                 :       16357 :                                                                 e->dest_idx))))
    4580                 :       34171 :                 continue;
    4581                 :             :             }
    4582                 :             : 
    4583                 :      924667 :           return false;
    4584                 :             :         }
    4585                 :             :     }
    4586                 :             :   return true;
    4587                 :             : }
    4588                 :             : 
    4589                 :             : /* Return true if BB doesn't have side-effects that would disallow
    4590                 :             :    range test optimization, all SSA_NAMEs set in the bb are consumed
    4591                 :             :    in the bb and there are no PHIs.  */
    4592                 :             : 
    4593                 :             : bool
    4594                 :     4728478 : no_side_effect_bb (basic_block bb)
    4595                 :             : {
    4596                 :     4728478 :   gimple_stmt_iterator gsi;
    4597                 :     4728478 :   gimple *last;
    4598                 :             : 
    4599                 :     4728478 :   if (!gimple_seq_empty_p (phi_nodes (bb)))
    4600                 :             :     return false;
    4601                 :     3768862 :   last = last_nondebug_stmt (bb);
    4602                 :    11932834 :   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    4603                 :             :     {
    4604                 :     8163972 :       gimple *stmt = gsi_stmt (gsi);
    4605                 :     8163972 :       tree lhs;
    4606                 :     8163972 :       imm_use_iterator imm_iter;
    4607                 :     8163972 :       use_operand_p use_p;
    4608                 :             : 
    4609                 :     8163972 :       if (is_gimple_debug (stmt))
    4610                 :     3017859 :         continue;
    4611                 :     5146113 :       if (gimple_has_side_effects (stmt))
    4612                 :     3768862 :         return false;
    4613                 :     4335932 :       if (stmt == last)
    4614                 :             :         return true;
    4615                 :     3394704 :       if (!is_gimple_assign (stmt))
    4616                 :             :         return false;
    4617                 :     2788896 :       lhs = gimple_assign_lhs (stmt);
    4618                 :     2788896 :       if (TREE_CODE (lhs) != SSA_NAME)
    4619                 :             :         return false;
    4620                 :     2535434 :       if (gimple_assign_rhs_could_trap_p (stmt))
    4621                 :             :         return false;
    4622                 :     3210508 :       FOR_EACH_IMM_USE_FAST (use_p, imm_iter, lhs)
    4623                 :             :         {
    4624                 :     1833257 :           gimple *use_stmt = USE_STMT (use_p);
    4625                 :     1833257 :           if (is_gimple_debug (use_stmt))
    4626                 :       68994 :             continue;
    4627                 :     1764263 :           if (gimple_bb (use_stmt) != bb)
    4628                 :             :             return false;
    4629                 :             :         }
    4630                 :             :     }
    4631                 :             :   return false;
    4632                 :             : }
    4633                 :             : 
    4634                 :             : /* If VAR is set by CODE (BIT_{AND,IOR}_EXPR) which is reassociable,
    4635                 :             :    return true and fill in *OPS recursively.  */
    4636                 :             : 
    4637                 :             : static bool
    4638                 :      113213 : get_ops (tree var, enum tree_code code, vec<operand_entry *> *ops,
    4639                 :             :          class loop *loop)
    4640                 :             : {
    4641                 :      113213 :   gimple *stmt = SSA_NAME_DEF_STMT (var);
    4642                 :      113213 :   tree rhs[2];
    4643                 :      113213 :   int i;
    4644                 :             : 
    4645                 :      113213 :   if (!is_reassociable_op (stmt, code, loop))
    4646                 :             :     return false;
    4647                 :             : 
    4648                 :       34007 :   rhs[0] = gimple_assign_rhs1 (stmt);
    4649                 :       34007 :   rhs[1] = gimple_assign_rhs2 (stmt);
    4650                 :       34007 :   gimple_set_visited (stmt, true);
    4651                 :      102021 :   for (i = 0; i < 2; i++)
    4652                 :       68014 :     if (TREE_CODE (rhs[i]) == SSA_NAME
    4653                 :       68014 :         && !get_ops (rhs[i], code, ops, loop)
    4654                 :      117394 :         && has_single_use (rhs[i]))
    4655                 :             :       {
    4656                 :       48877 :         operand_entry *oe = operand_entry_pool.allocate ();
    4657                 :             : 
    4658                 :       48877 :         oe->op = rhs[i];
    4659                 :       48877 :         oe->rank = code;
    4660                 :       48877 :         oe->id = 0;
    4661                 :       48877 :         oe->count = 1;
    4662                 :       48877 :         oe->stmt_to_insert = NULL;
    4663                 :       48877 :         ops->safe_push (oe);
    4664                 :             :       }
    4665                 :             :   return true;
    4666                 :             : }
    4667                 :             : 
    4668                 :             : /* Find the ops that were added by get_ops starting from VAR, see if
    4669                 :             :    they were changed during update_range_test and if yes, create new
    4670                 :             :    stmts.  */
    4671                 :             : 
    4672                 :             : static tree
    4673                 :        8053 : update_ops (tree var, enum tree_code code, const vec<operand_entry *> &ops,
    4674                 :             :             unsigned int *pidx, class loop *loop)
    4675                 :             : {
    4676                 :        8053 :   gimple *stmt = SSA_NAME_DEF_STMT (var);
    4677                 :        8053 :   tree rhs[4];
    4678                 :        8053 :   int i;
    4679                 :             : 
    4680                 :        8053 :   if (!is_reassociable_op (stmt, code, loop))
    4681                 :             :     return NULL;
    4682                 :             : 
    4683                 :        2645 :   rhs[0] = gimple_assign_rhs1 (stmt);
    4684                 :        2645 :   rhs[1] = gimple_assign_rhs2 (stmt);
    4685                 :        2645 :   rhs[2] = rhs[0];
    4686                 :        2645 :   rhs[3] = rhs[1];
    4687                 :        7935 :   for (i = 0; i < 2; i++)
    4688                 :        5290 :     if (TREE_CODE (rhs[i]) == SSA_NAME)
    4689                 :             :       {
    4690                 :        5290 :         rhs[2 + i] = update_ops (rhs[i], code, ops, pidx, loop);
    4691                 :        5290 :         if (rhs[2 + i] == NULL_TREE)
    4692                 :             :           {
    4693                 :        5138 :             if (has_single_use (rhs[i]))
    4694                 :        5126 :               rhs[2 + i] = ops[(*pidx)++]->op;
    4695                 :             :             else
    4696                 :          12 :               rhs[2 + i] = rhs[i];
    4697                 :             :           }
    4698                 :             :       }
    4699                 :        2645 :   if ((rhs[2] != rhs[0] || rhs[3] != rhs[1])
    4700                 :        2450 :       && (rhs[2] != rhs[1] || rhs[3] != rhs[0]))
    4701                 :             :     {
    4702                 :        2450 :       gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    4703                 :        2450 :       var = make_ssa_name (TREE_TYPE (var));
    4704                 :        2450 :       gassign *g = gimple_build_assign (var, gimple_assign_rhs_code (stmt),
    4705                 :             :                                         rhs[2], rhs[3]);
    4706                 :        2450 :       gimple_set_uid (g, gimple_uid (stmt));
    4707                 :        2450 :       gimple_set_visited (g, true);
    4708                 :        2450 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    4709                 :        2450 :       gimple_stmt_iterator gsi2 = gsi_for_stmt (g);
    4710                 :        2450 :       if (fold_stmt_inplace (&gsi2))
    4711                 :        1720 :         update_stmt (g);
    4712                 :             :     }
    4713                 :             :   return var;
    4714                 :             : }
    4715                 :             : 
    4716                 :             : /* Structure to track the initial value passed to get_ops and
    4717                 :             :    the range in the ops vector for each basic block.  */
    4718                 :             : 
    4719                 :             : struct inter_bb_range_test_entry
    4720                 :             : {
    4721                 :             :   tree op;
    4722                 :             :   unsigned int first_idx, last_idx;
    4723                 :             : };
    4724                 :             : 
    4725                 :             : /* Inter-bb range test optimization.
    4726                 :             : 
    4727                 :             :    Returns TRUE if a gimple conditional is optimized to a true/false,
    4728                 :             :    otherwise return FALSE.
    4729                 :             : 
    4730                 :             :    This indicates to the caller that it should run a CFG cleanup pass
    4731                 :             :    once reassociation is completed.  */
    4732                 :             : 
    4733                 :             : static bool
    4734                 :    16709987 : maybe_optimize_range_tests (gimple *stmt)
    4735                 :             : {
    4736                 :    16709987 :   basic_block first_bb = gimple_bb (stmt);
    4737                 :    16709987 :   basic_block last_bb = first_bb;
    4738                 :    16709987 :   basic_block other_bb = NULL;
    4739                 :    16709987 :   basic_block bb;
    4740                 :    16709987 :   edge_iterator ei;
    4741                 :    16709987 :   edge e;
    4742                 :    16709987 :   auto_vec<operand_entry *> ops;
    4743                 :    16709987 :   auto_vec<inter_bb_range_test_entry> bbinfo;
    4744                 :    16709987 :   bool any_changes = false;
    4745                 :    16709987 :   bool cfg_cleanup_needed = false;
    4746                 :             : 
    4747                 :             :   /* Consider only basic blocks that end with GIMPLE_COND or
    4748                 :             :      a cast statement satisfying final_range_test_p.  All
    4749                 :             :      but the last bb in the first_bb .. last_bb range
    4750                 :             :      should end with GIMPLE_COND.  */
    4751                 :    16709987 :   if (gimple_code (stmt) == GIMPLE_COND)
    4752                 :             :     {
    4753                 :    24190941 :       if (EDGE_COUNT (first_bb->succs) != 2)
    4754                 :             :         return cfg_cleanup_needed;
    4755                 :             :     }
    4756                 :     9220786 :   else if (final_range_test_p (stmt))
    4757                 :       86104 :     other_bb = single_succ (first_bb);
    4758                 :             :   else
    4759                 :             :     return cfg_cleanup_needed;
    4760                 :             : 
    4761                 :     7575305 :   if (stmt_could_throw_p (cfun, stmt))
    4762                 :             :     return cfg_cleanup_needed;
    4763                 :             : 
    4764                 :             :   /* As relative ordering of post-dominator sons isn't fixed,
    4765                 :             :      maybe_optimize_range_tests can be called first on any
    4766                 :             :      bb in the range we want to optimize.  So, start searching
    4767                 :             :      backwards, if first_bb can be set to a predecessor.  */
    4768                 :     7754278 :   while (single_pred_p (first_bb))
    4769                 :             :     {
    4770                 :     5241447 :       basic_block pred_bb = single_pred (first_bb);
    4771                 :     5241447 :       if (!suitable_cond_bb (pred_bb, first_bb, &other_bb, NULL, true))
    4772                 :             :         break;
    4773                 :      772225 :       if (!no_side_effect_bb (first_bb))
    4774                 :             :         break;
    4775                 :             :       first_bb = pred_bb;
    4776                 :             :     }
    4777                 :             :   /* If first_bb is last_bb, other_bb hasn't been computed yet.
    4778                 :             :      Before starting forward search in last_bb successors, find
    4779                 :             :      out the other_bb.  */
    4780                 :     7575139 :   if (first_bb == last_bb)
    4781                 :             :     {
    4782                 :     7452383 :       other_bb = NULL;
    4783                 :             :       /* As non-GIMPLE_COND last stmt always terminates the range,
    4784                 :             :          if forward search didn't discover anything, just give up.  */
    4785                 :     7452383 :       if (gimple_code (stmt) != GIMPLE_COND)
    4786                 :             :         return cfg_cleanup_needed;
    4787                 :             :       /* Look at both successors.  Either it ends with a GIMPLE_COND
    4788                 :             :          and satisfies suitable_cond_bb, or ends with a cast and
    4789                 :             :          other_bb is that cast's successor.  */
    4790                 :    20612107 :       FOR_EACH_EDGE (e, ei, first_bb->succs)
    4791                 :    14243434 :         if (!(e->flags & (EDGE_TRUE_VALUE | EDGE_FALSE_VALUE))
    4792                 :    14243434 :             || e->dest == first_bb)
    4793                 :             :           return cfg_cleanup_needed;
    4794                 :    21724946 :         else if (single_pred_p (e->dest))
    4795                 :             :           {
    4796                 :     8483784 :             stmt = last_nondebug_stmt (e->dest);
    4797                 :     8483784 :             if (stmt
    4798                 :     8316052 :                 && gimple_code (stmt) == GIMPLE_COND
    4799                 :    12145683 :                 && EDGE_COUNT (e->dest->succs) == 2)
    4800                 :             :               {
    4801                 :     3661899 :                 if (suitable_cond_bb (first_bb, e->dest, &other_bb,
    4802                 :             :                                       NULL, true))
    4803                 :             :                   break;
    4804                 :             :                 else
    4805                 :     3104266 :                   other_bb = NULL;
    4806                 :             :               }
    4807                 :     4821885 :             else if (stmt
    4808                 :     4654153 :                      && final_range_test_p (stmt)
    4809                 :     4891366 :                      && find_edge (first_bb, single_succ (e->dest)))
    4810                 :             :               {
    4811                 :       30994 :                 other_bb = single_succ (e->dest);
    4812                 :       30994 :                 if (other_bb == first_bb)
    4813                 :           0 :                   other_bb = NULL;
    4814                 :             :               }
    4815                 :             :           }
    4816                 :     6926306 :       if (other_bb == NULL)
    4817                 :             :         return cfg_cleanup_needed;
    4818                 :             :     }
    4819                 :             :   /* Now do the forward search, moving last_bb to successor bbs
    4820                 :             :      that aren't other_bb.  */
    4821                 :      712640 :   while (EDGE_COUNT (last_bb->succs) == 2)
    4822                 :             :     {
    4823                 :     1219550 :       FOR_EACH_EDGE (e, ei, last_bb->succs)
    4824                 :     1219550 :         if (e->dest != other_bb)
    4825                 :             :           break;
    4826                 :      708009 :       if (e == NULL)
    4827                 :             :         break;
    4828                 :     1416018 :       if (!single_pred_p (e->dest))
    4829                 :             :         break;
    4830                 :      679706 :       if (!suitable_cond_bb (e->dest, last_bb, &other_bb, NULL, false))
    4831                 :             :         break;
    4832                 :      583821 :       if (!no_side_effect_bb (e->dest))
    4833                 :             :         break;
    4834                 :        1257 :       last_bb = e->dest;
    4835                 :             :     }
    4836                 :      711383 :   if (first_bb == last_bb)
    4837                 :             :     return cfg_cleanup_needed;
    4838                 :             :   /* Here basic blocks first_bb through last_bb's predecessor
    4839                 :             :      end with GIMPLE_COND, all of them have one of the edges to
    4840                 :             :      other_bb and another to another block in the range,
    4841                 :             :      all blocks except first_bb don't have side-effects and
    4842                 :             :      last_bb ends with either GIMPLE_COND, or cast satisfying
    4843                 :             :      final_range_test_p.  */
    4844                 :      180396 :   for (bb = last_bb; ; bb = single_pred (bb))
    4845                 :             :     {
    4846                 :      303672 :       enum tree_code code;
    4847                 :      303672 :       tree lhs, rhs;
    4848                 :      303672 :       inter_bb_range_test_entry bb_ent;
    4849                 :             : 
    4850                 :      303672 :       bb_ent.op = NULL_TREE;
    4851                 :      303672 :       bb_ent.first_idx = ops.length ();
    4852                 :      303672 :       bb_ent.last_idx = bb_ent.first_idx;
    4853                 :      303672 :       e = find_edge (bb, other_bb);
    4854                 :      303672 :       stmt = last_nondebug_stmt (bb);
    4855                 :      303672 :       gimple_set_visited (stmt, true);
    4856                 :      303672 :       if (gimple_code (stmt) != GIMPLE_COND)
    4857                 :             :         {
    4858                 :        4631 :           use_operand_p use_p;
    4859                 :        4631 :           gimple *phi;
    4860                 :        4631 :           edge e2;
    4861                 :        4631 :           unsigned int d;
    4862                 :             : 
    4863                 :        4631 :           lhs = gimple_assign_lhs (stmt);
    4864                 :        4631 :           rhs = gimple_assign_rhs1 (stmt);
    4865                 :        4631 :           gcc_assert (bb == last_bb);
    4866                 :             : 
    4867                 :             :           /* stmt is
    4868                 :             :              _123 = (int) _234;
    4869                 :             :              OR
    4870                 :             :              _234 = a_2(D) == 2;
    4871                 :             : 
    4872                 :             :              followed by:
    4873                 :             :              <bb M>:
    4874                 :             :              # _345 = PHI <_123(N), 1(...), 1(...)>
    4875                 :             : 
    4876                 :             :              or 0 instead of 1.  If it is 0, the _234
    4877                 :             :              range test is anded together with all the
    4878                 :             :              other range tests, if it is 1, it is ored with
    4879                 :             :              them.  */
    4880                 :        4631 :           single_imm_use (lhs, &use_p, &phi);
    4881                 :        4631 :           gcc_assert (gimple_code (phi) == GIMPLE_PHI);
    4882                 :        4631 :           e2 = find_edge (first_bb, other_bb);
    4883                 :        4631 :           d = e2->dest_idx;
    4884                 :        4631 :           gcc_assert (gimple_phi_arg_def (phi, e->dest_idx) == lhs);
    4885                 :        4631 :           if (integer_zerop (gimple_phi_arg_def (phi, d)))
    4886                 :             :             code = BIT_AND_EXPR;
    4887                 :             :           else
    4888                 :             :             {
    4889                 :        2312 :               gcc_checking_assert (integer_onep (gimple_phi_arg_def (phi, d)));
    4890                 :             :               code = BIT_IOR_EXPR;
    4891                 :             :             }
    4892                 :             : 
    4893                 :             :           /* If _234 SSA_NAME_DEF_STMT is
    4894                 :             :              _234 = _567 | _789;
    4895                 :             :              (or &, corresponding to 1/0 in the phi arguments,
    4896                 :             :              push into ops the individual range test arguments
    4897                 :             :              of the bitwise or resp. and, recursively.  */
    4898                 :        4631 :           if (TREE_CODE (rhs) == SSA_NAME
    4899                 :        4631 :               && (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
    4900                 :             :                   != tcc_comparison)
    4901                 :        2772 :               && !get_ops (rhs, code, &ops,
    4902                 :             :                            loop_containing_stmt (stmt))
    4903                 :        7248 :               && has_single_use (rhs))
    4904                 :             :             {
    4905                 :             :               /* Otherwise, push the _234 range test itself.  */
    4906                 :        2617 :               operand_entry *oe = operand_entry_pool.allocate ();
    4907                 :             : 
    4908                 :        2617 :               oe->op = rhs;
    4909                 :        2617 :               oe->rank = code;
    4910                 :        2617 :               oe->id = 0;
    4911                 :        2617 :               oe->count = 1;
    4912                 :        2617 :               oe->stmt_to_insert = NULL;
    4913                 :        2617 :               ops.safe_push (oe);
    4914                 :        2617 :               bb_ent.last_idx++;
    4915                 :        2617 :               bb_ent.op = rhs;
    4916                 :             :             }
    4917                 :        2014 :           else if (is_gimple_assign (stmt)
    4918                 :        2014 :                    && (TREE_CODE_CLASS (gimple_assign_rhs_code (stmt))
    4919                 :             :                        == tcc_comparison)
    4920                 :        1859 :                    && !get_ops (lhs, code, &ops,
    4921                 :             :                                 loop_containing_stmt (stmt))
    4922                 :        3873 :                    && has_single_use (lhs))
    4923                 :             :             {
    4924                 :        1859 :               operand_entry *oe = operand_entry_pool.allocate ();
    4925                 :        1859 :               oe->op = lhs;
    4926                 :        1859 :               oe->rank = code;
    4927                 :        1859 :               oe->id = 0;
    4928                 :        1859 :               oe->count = 1;
    4929                 :        1859 :               ops.safe_push (oe);
    4930                 :        1859 :               bb_ent.last_idx++;
    4931                 :        1859 :               bb_ent.op = lhs;
    4932                 :             :             }
    4933                 :             :           else
    4934                 :             :             {
    4935                 :         155 :               bb_ent.last_idx = ops.length ();
    4936                 :         155 :               bb_ent.op = rhs;
    4937                 :             :             }
    4938                 :        4631 :           bbinfo.safe_push (bb_ent);
    4939                 :        9426 :           for (unsigned int i = bb_ent.first_idx; i < bb_ent.last_idx; ++i)
    4940                 :        4795 :             ops[i]->id = bb->index;
    4941                 :        4631 :           continue;
    4942                 :        4631 :         }
    4943                 :      299041 :       else if (bb == last_bb)
    4944                 :             :         {
    4945                 :             :           /* For last_bb, handle also:
    4946                 :             :              if (x_3(D) == 3)
    4947                 :             :                goto <bb 6>; [34.00%]
    4948                 :             :              else
    4949                 :             :                goto <bb 7>; [66.00%]
    4950                 :             : 
    4951                 :             :              <bb 6> [local count: 79512730]:
    4952                 :             : 
    4953                 :             :              <bb 7> [local count: 1073741824]:
    4954                 :             :              # prephitmp_7 = PHI <1(3), 1(4), 0(5), 1(2), 1(6)>
    4955                 :             :              where bb 7 is OTHER_BB, but the PHI values from the
    4956                 :             :              earlier bbs match the path through the empty bb
    4957                 :             :              in between.  */
    4958                 :      118645 :           bool test_swapped_p = false;
    4959                 :      118645 :           bool ok = suitable_cond_bb (single_pred (last_bb), last_bb,
    4960                 :             :                                       &other_bb, &test_swapped_p, true);
    4961                 :      118645 :           gcc_assert (ok);
    4962                 :      118645 :           if (test_swapped_p)
    4963                 :         612 :             e = EDGE_SUCC (bb, e == EDGE_SUCC (bb, 0) ? 1 : 0);
    4964                 :             :         }
    4965                 :             :       /* Otherwise stmt is GIMPLE_COND.  */
    4966                 :      299041 :       code = gimple_cond_code (stmt);
    4967                 :      299041 :       lhs = gimple_cond_lhs (stmt);
    4968                 :      299041 :       rhs = gimple_cond_rhs (stmt);
    4969                 :      299041 :       if (TREE_CODE (lhs) == SSA_NAME
    4970                 :      297823 :           && INTEGRAL_TYPE_P (TREE_TYPE (lhs))
    4971                 :      552674 :           && ((code != EQ_EXPR && code != NE_EXPR)
    4972                 :      201701 :               || rhs != boolean_false_node
    4973                 :             :                  /* Either push into ops the individual bitwise
    4974                 :             :                     or resp. and operands, depending on which
    4975                 :             :                     edge is other_bb.  */
    4976                 :       40568 :               || !get_ops (lhs, (((e->flags & EDGE_TRUE_VALUE) == 0)
    4977                 :       40568 :                                  ^ (code == EQ_EXPR))
    4978                 :             :                                 ? BIT_AND_EXPR : BIT_IOR_EXPR, &ops,
    4979                 :             :                            loop_containing_stmt (stmt))))
    4980                 :             :         {
    4981                 :             :           /* Or push the GIMPLE_COND stmt itself.  */
    4982                 :      238415 :           operand_entry *oe = operand_entry_pool.allocate ();
    4983                 :             : 
    4984                 :      238415 :           oe->op = NULL;
    4985                 :      476830 :           oe->rank = (e->flags & EDGE_TRUE_VALUE)
    4986                 :      238415 :                      ? BIT_IOR_EXPR : BIT_AND_EXPR;
    4987                 :             :           /* oe->op = NULL signs that there is no SSA_NAME
    4988                 :             :              for the range test, and oe->id instead is the
    4989                 :             :              basic block number, at which's end the GIMPLE_COND
    4990                 :             :              is.  */
    4991                 :      238415 :           oe->id = bb->index;
    4992                 :      238415 :           oe->count = 1;
    4993                 :      238415 :           oe->stmt_to_insert = NULL;
    4994                 :      238415 :           ops.safe_push (oe);
    4995                 :      238415 :           bb_ent.op = NULL;
    4996                 :      238415 :           bb_ent.last_idx++;
    4997                 :             :         }
    4998                 :       83826 :       else if (ops.length () > bb_ent.first_idx)
    4999                 :             :         {
    5000                 :       15186 :           bb_ent.op = lhs;
    5001                 :       15186 :           bb_ent.last_idx = ops.length ();
    5002                 :             :         }
    5003                 :      299041 :       bbinfo.safe_push (bb_ent);
    5004                 :      586014 :       for (unsigned int i = bb_ent.first_idx; i < bb_ent.last_idx; ++i)
    5005                 :      286973 :         ops[i]->id = bb->index;
    5006                 :      299041 :       if (bb == first_bb)
    5007                 :             :         break;
    5008                 :      180396 :     }
    5009                 :    16833263 :   if (ops.length () > 1)
    5010                 :       99473 :     any_changes = optimize_range_tests (ERROR_MARK, &ops, first_bb);
    5011                 :       99473 :   if (any_changes)
    5012                 :             :     {
    5013                 :             :       unsigned int idx, max_idx = 0;
    5014                 :             :       /* update_ops relies on has_single_use predicates returning the
    5015                 :             :          same values as it did during get_ops earlier.  Additionally it
    5016                 :             :          never removes statements, only adds new ones and it should walk
    5017                 :             :          from the single imm use and check the predicate already before
    5018                 :             :          making those changes.
    5019                 :             :          On the other side, the handling of GIMPLE_COND directly can turn
    5020                 :             :          previously multiply used SSA_NAMEs into single use SSA_NAMEs, so
    5021                 :             :          it needs to be done in a separate loop afterwards.  */
    5022                 :       16972 :       for (bb = last_bb, idx = 0; ; bb = single_pred (bb), idx++)
    5023                 :             :         {
    5024                 :       25219 :           if (bbinfo[idx].first_idx < bbinfo[idx].last_idx
    5025                 :       25219 :               && bbinfo[idx].op != NULL_TREE)
    5026                 :             :             {
    5027                 :        2763 :               tree new_op;
    5028                 :             : 
    5029                 :        2763 :               max_idx = idx;
    5030                 :        2763 :               stmt = last_nondebug_stmt (bb);
    5031                 :        5526 :               new_op = update_ops (bbinfo[idx].op,
    5032                 :             :                                    (enum tree_code)
    5033                 :        2763 :                                    ops[bbinfo[idx].first_idx]->rank,
    5034                 :        2763 :                                    ops, &bbinfo[idx].first_idx,
    5035                 :             :                                    loop_containing_stmt (stmt));
    5036                 :        2763 :               if (new_op == NULL_TREE)
    5037                 :             :                 {
    5038                 :         270 :                   gcc_assert (bb == last_bb);
    5039                 :         270 :                   new_op = ops[bbinfo[idx].first_idx++]->op;
    5040                 :             :                 }
    5041                 :        2763 :               if (bbinfo[idx].op != new_op)
    5042                 :             :                 {
    5043                 :        2557 :                   imm_use_iterator iter;
    5044                 :        2557 :                   use_operand_p use_p;
    5045                 :        2557 :                   gimple *use_stmt, *cast_or_tcc_cmp_stmt = NULL;
    5046                 :             : 
    5047                 :        5139 :                   FOR_EACH_IMM_USE_STMT (use_stmt, iter, bbinfo[idx].op)
    5048                 :        2582 :                     if (is_gimple_debug (use_stmt))
    5049                 :          25 :                       continue;
    5050                 :        2557 :                     else if (gimple_code (use_stmt) == GIMPLE_COND
    5051                 :        2557 :                              || gimple_code (use_stmt) == GIMPLE_PHI)
    5052                 :        6984 :                       FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
    5053                 :        2328 :                         SET_USE (use_p, new_op);
    5054                 :         229 :                     else if ((is_gimple_assign (use_stmt)
    5055                 :         229 :                               && (TREE_CODE_CLASS
    5056                 :             :                                   (gimple_assign_rhs_code (use_stmt))
    5057                 :             :                                   == tcc_comparison)))
    5058                 :             :                       cast_or_tcc_cmp_stmt = use_stmt;
    5059                 :         229 :                     else if (gimple_assign_cast_p (use_stmt))
    5060                 :             :                       cast_or_tcc_cmp_stmt = use_stmt;
    5061                 :             :                     else
    5062                 :        2557 :                       gcc_unreachable ();
    5063                 :             : 
    5064                 :        2557 :                   if (cast_or_tcc_cmp_stmt)
    5065                 :             :                     {
    5066                 :         229 :                       gcc_assert (bb == last_bb);
    5067                 :         229 :                       tree lhs = gimple_assign_lhs (cast_or_tcc_cmp_stmt);
    5068                 :         229 :                       tree new_lhs = make_ssa_name (TREE_TYPE (lhs));
    5069                 :         229 :                       enum tree_code rhs_code
    5070                 :         229 :                         = gimple_assign_cast_p (cast_or_tcc_cmp_stmt)
    5071                 :         229 :                         ? gimple_assign_rhs_code (cast_or_tcc_cmp_stmt)
    5072                 :         229 :                         : CONVERT_EXPR;
    5073                 :         229 :                       gassign *g;
    5074                 :         229 :                       if (is_gimple_min_invariant (new_op))
    5075                 :             :                         {
    5076                 :          62 :                           new_op = fold_convert (TREE_TYPE (lhs), new_op);
    5077                 :          62 :                           g = gimple_build_assign (new_lhs, new_op);
    5078                 :             :                         }
    5079                 :             :                       else
    5080                 :         167 :                         g = gimple_build_assign (new_lhs, rhs_code, new_op);
    5081                 :         229 :                       gimple_stmt_iterator gsi
    5082                 :         229 :                         = gsi_for_stmt (cast_or_tcc_cmp_stmt);
    5083                 :         229 :                       gimple_set_uid (g, gimple_uid (cast_or_tcc_cmp_stmt));
    5084                 :         229 :                       gimple_set_visited (g, true);
    5085                 :         229 :                       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    5086                 :         463 :                       FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
    5087                 :         234 :                         if (is_gimple_debug (use_stmt))
    5088                 :           5 :                           continue;
    5089                 :         229 :                         else if (gimple_code (use_stmt) == GIMPLE_COND
    5090                 :         229 :                                  || gimple_code (use_stmt) == GIMPLE_PHI)
    5091                 :         687 :                           FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
    5092                 :         229 :                             SET_USE (use_p, new_lhs);
    5093                 :             :                         else
    5094                 :         229 :                           gcc_unreachable ();
    5095                 :             :                     }
    5096                 :             :                 }
    5097                 :             :             }
    5098                 :       25219 :           if (bb == first_bb)
    5099                 :             :             break;
    5100                 :       16972 :         }
    5101                 :       16972 :       for (bb = last_bb, idx = 0; ; bb = single_pred (bb), idx++)
    5102                 :             :         {
    5103                 :       25219 :           if (bbinfo[idx].first_idx < bbinfo[idx].last_idx
    5104                 :       22173 :               && bbinfo[idx].op == NULL_TREE
    5105                 :       47392 :               && ops[bbinfo[idx].first_idx]->op != NULL_TREE)
    5106                 :             :             {
    5107                 :       37426 :               gcond *cond_stmt = as_a <gcond *> (*gsi_last_bb (bb));
    5108                 :             : 
    5109                 :       18713 :               if (idx > max_idx)
    5110                 :             :                 max_idx = idx;
    5111                 :             : 
    5112                 :             :               /* If we collapse the conditional to a true/false
    5113                 :             :                  condition, then bubble that knowledge up to our caller.  */
    5114                 :       18713 :               if (integer_zerop (ops[bbinfo[idx].first_idx]->op))
    5115                 :             :                 {
    5116                 :        9336 :                   gimple_cond_make_false (cond_stmt);
    5117                 :        9336 :                   cfg_cleanup_needed = true;
    5118                 :             :                 }
    5119                 :        9377 :               else if (integer_onep (ops[bbinfo[idx].first_idx]->op))
    5120                 :             :                 {
    5121                 :        2214 :                   gimple_cond_make_true (cond_stmt);
    5122                 :        2214 :                   cfg_cleanup_needed = true;
    5123                 :             :                 }
    5124                 :             :               else
    5125                 :             :                 {
    5126                 :        7163 :                   gimple_cond_set_code (cond_stmt, NE_EXPR);
    5127                 :        7163 :                   gimple_cond_set_lhs (cond_stmt,
    5128                 :        7163 :                                        ops[bbinfo[idx].first_idx]->op);
    5129                 :        7163 :                   gimple_cond_set_rhs (cond_stmt, boolean_false_node);
    5130                 :             :                 }
    5131                 :       18713 :               update_stmt (cond_stmt);
    5132                 :             :             }
    5133                 :       25219 :           if (bb == first_bb)
    5134                 :             :             break;
    5135                 :       16972 :         }
    5136                 :             : 
    5137                 :             :       /* The above changes could result in basic blocks after the first
    5138                 :             :          modified one, up to and including last_bb, to be executed even if
    5139                 :             :          they would not be in the original program.  If the value ranges of
    5140                 :             :          assignment lhs' in those bbs were dependent on the conditions
    5141                 :             :          guarding those basic blocks which now can change, the VRs might
    5142                 :             :          be incorrect.  As no_side_effect_bb should ensure those SSA_NAMEs
    5143                 :             :          are only used within the same bb, it should be not a big deal if
    5144                 :             :          we just reset all the VRs in those bbs.  See PR68671.  */
    5145                 :       24131 :       for (bb = last_bb, idx = 0; idx < max_idx; bb = single_pred (bb), idx++)
    5146                 :       15884 :         reset_flow_sensitive_info_in_bb (bb);
    5147                 :             :     }
    5148                 :             :   return cfg_cleanup_needed;
    5149                 :    16709987 : }
    5150                 :             : 
    5151                 :             : /* Remove def stmt of VAR if VAR has zero uses and recurse
    5152                 :             :    on rhs1 operand if so.  */
    5153                 :             : 
    5154                 :             : static void
    5155                 :       53600 : remove_visited_stmt_chain (tree var)
    5156                 :             : {
    5157                 :       73676 :   gimple *stmt;
    5158                 :       73676 :   gimple_stmt_iterator gsi;
    5159                 :             : 
    5160                 :       93752 :   while (1)
    5161                 :             :     {
    5162                 :       73676 :       if (TREE_CODE (var) != SSA_NAME || !has_zero_uses (var))
    5163                 :             :         return;
    5164                 :       30439 :       stmt = SSA_NAME_DEF_STMT (var);
    5165                 :       30439 :       if (is_gimple_assign (stmt) && gimple_visited_p (stmt))
    5166                 :             :         {
    5167                 :       20076 :           var = gimple_assign_rhs1 (stmt);
    5168                 :       20076 :           gsi = gsi_for_stmt (stmt);
    5169                 :       20076 :           reassoc_remove_stmt (&gsi);
    5170                 :       20076 :           release_defs (stmt);
    5171                 :             :         }
    5172                 :             :       else
    5173                 :             :         return;
    5174                 :             :     }
    5175                 :             : }
    5176                 :             : 
    5177                 :             : /* This function checks three consequtive operands in
    5178                 :             :    passed operands vector OPS starting from OPINDEX and
    5179                 :             :    swaps two operands if it is profitable for binary operation
    5180                 :             :    consuming OPINDEX + 1 abnd OPINDEX + 2 operands.
    5181                 :             : 
    5182                 :             :    We pair ops with the same rank if possible.  */
    5183                 :             : 
    5184                 :             : static void
    5185                 :      148737 : swap_ops_for_binary_stmt (const vec<operand_entry *> &ops,
    5186                 :             :                           unsigned int opindex)
    5187                 :             : {
    5188                 :      148737 :   operand_entry *oe1, *oe2, *oe3;
    5189                 :             : 
    5190                 :      148737 :   oe1 = ops[opindex];
    5191                 :      148737 :   oe2 = ops[opindex + 1];
    5192                 :      148737 :   oe3 = ops[opindex + 2];
    5193                 :             : 
    5194                 :      148737 :   if (oe1->rank == oe2->rank && oe2->rank != oe3->rank)
    5195                 :       20768 :     std::swap (*oe1, *oe3);
    5196                 :      127969 :   else if (oe1->rank == oe3->rank && oe2->rank != oe3->rank)
    5197                 :         254 :     std::swap (*oe1, *oe2);
    5198                 :      148737 : }
    5199                 :             : 
    5200                 :             : /* If definition of RHS1 or RHS2 dominates STMT, return the later of those
    5201                 :             :    two definitions, otherwise return STMT.  Sets INSERT_BEFORE to indicate
    5202                 :             :    whether RHS1 op RHS2 can be inserted before or needs to be inserted
    5203                 :             :    after the returned stmt.  */
    5204                 :             : 
    5205                 :             : static inline gimple *
    5206                 :      817108 : find_insert_point (gimple *stmt, tree rhs1, tree rhs2, bool &insert_before)
    5207                 :             : {
    5208                 :      817108 :   insert_before = true;
    5209                 :      817108 :   if (TREE_CODE (rhs1) == SSA_NAME
    5210                 :      817108 :       && reassoc_stmt_dominates_stmt_p (stmt, SSA_NAME_DEF_STMT (rhs1)))
    5211                 :             :     {
    5212                 :       11909 :       stmt = SSA_NAME_DEF_STMT (rhs1);
    5213                 :       11909 :       insert_before = false;
    5214                 :             :     }
    5215                 :      817108 :   if (TREE_CODE (rhs2) == SSA_NAME
    5216                 :      817108 :       && reassoc_stmt_dominates_stmt_p (stmt, SSA_NAME_DEF_STMT (rhs2)))
    5217                 :             :     {
    5218                 :       15778 :       stmt = SSA_NAME_DEF_STMT (rhs2);
    5219                 :       15778 :       insert_before = false;
    5220                 :             :     }
    5221                 :      817108 :   return stmt;
    5222                 :             : }
    5223                 :             : 
    5224                 :             : /* If the stmt that defines operand has to be inserted, insert it
    5225                 :             :    before the use.  */
    5226                 :             : static void
    5227                 :         104 : insert_stmt_before_use (gimple *stmt, gimple *stmt_to_insert)
    5228                 :             : {
    5229                 :         104 :   gcc_assert (is_gimple_assign (stmt_to_insert));
    5230                 :         104 :   tree rhs1 = gimple_assign_rhs1 (stmt_to_insert);
    5231                 :         104 :   tree rhs2 = gimple_assign_rhs2 (stmt_to_insert);
    5232                 :         104 :   bool insert_before;
    5233                 :         104 :   gimple *insert_point = find_insert_point (stmt, rhs1, rhs2, insert_before);
    5234                 :         104 :   gimple_stmt_iterator gsi = gsi_for_stmt (insert_point);
    5235                 :         104 :   gimple_set_uid (stmt_to_insert, gimple_uid (insert_point));
    5236                 :             : 
    5237                 :             :   /* If the insert point is not stmt, then insert_point would be
    5238                 :             :      the point where operand rhs1 or rhs2 is defined. In this case,
    5239                 :             :      stmt_to_insert has to be inserted afterwards. This would
    5240                 :             :      only happen when the stmt insertion point is flexible. */
    5241                 :         104 :   if (insert_before)
    5242                 :         103 :     gsi_insert_before (&gsi, stmt_to_insert, GSI_NEW_STMT);
    5243                 :             :   else
    5244                 :           1 :     insert_stmt_after (stmt_to_insert, insert_point);
    5245                 :         104 : }
    5246                 :             : 
    5247                 :             : 
    5248                 :             : /* Recursively rewrite our linearized statements so that the operators
    5249                 :             :    match those in OPS[OPINDEX], putting the computation in rank
    5250                 :             :    order.  Return new lhs.
    5251                 :             :    CHANGED is true if we shouldn't reuse the lhs SSA_NAME both in
    5252                 :             :    the current stmt and during recursive invocations.
    5253                 :             :    NEXT_CHANGED is true if we shouldn't reuse the lhs SSA_NAME in
    5254                 :             :    recursive invocations.  */
    5255                 :             : 
    5256                 :             : static tree
    5257                 :     4028612 : rewrite_expr_tree (gimple *stmt, enum tree_code rhs_code, unsigned int opindex,
    5258                 :             :                    const vec<operand_entry *> &ops, bool changed,
    5259                 :             :                    bool next_changed)
    5260                 :             : {
    5261                 :     4028612 :   tree rhs1 = gimple_assign_rhs1 (stmt);
    5262                 :     4028612 :   tree rhs2 = gimple_assign_rhs2 (stmt);
    5263                 :     4028612 :   tree lhs = gimple_assign_lhs (stmt);
    5264                 :     4028612 :   operand_entry *oe;
    5265                 :             : 
    5266                 :             :   /* The final recursion case for this function is that you have
    5267                 :             :      exactly two operations left.
    5268                 :             :      If we had exactly one op in the entire list to start with, we
    5269                 :             :      would have never called this function, and the tail recursion
    5270                 :             :      rewrites them one at a time.  */
    5271                 :     8057224 :   if (opindex + 2 == ops.length ())
    5272                 :             :     {
    5273                 :     3818459 :       operand_entry *oe1, *oe2;
    5274                 :             : 
    5275                 :     3818459 :       oe1 = ops[opindex];
    5276                 :     3818459 :       oe2 = ops[opindex + 1];
    5277                 :             : 
    5278                 :     3818459 :       if (rhs1 != oe1->op || rhs2 != oe2->op)
    5279                 :             :         {
    5280                 :      717256 :           gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    5281                 :      717256 :           unsigned int uid = gimple_uid (stmt);
    5282                 :             : 
    5283                 :      717256 :           if (dump_file && (dump_flags & TDF_DETAILS))
    5284                 :             :             {
    5285                 :          29 :               fprintf (dump_file, "Transforming ");
    5286                 :          29 :               print_gimple_stmt (dump_file, stmt, 0);
    5287                 :             :             }
    5288                 :             : 
    5289                 :             :           /* If the stmt that defines operand has to be inserted, insert it
    5290                 :             :              before the use.  */
    5291                 :      717256 :           if (oe1->stmt_to_insert)
    5292                 :          47 :             insert_stmt_before_use (stmt, oe1->stmt_to_insert);
    5293                 :      717256 :           if (oe2->stmt_to_insert)
    5294                 :          55 :             insert_stmt_before_use (stmt, oe2->stmt_to_insert);
    5295                 :             :           /* Even when changed is false, reassociation could have e.g. removed
    5296                 :             :              some redundant operations, so unless we are just swapping the
    5297                 :             :              arguments or unless there is no change at all (then we just
    5298                 :             :              return lhs), force creation of a new SSA_NAME.  */
    5299                 :      717256 :           if (changed || ((rhs1 != oe2->op || rhs2 != oe1->op) && opindex))
    5300                 :             :             {
    5301                 :       74451 :               bool insert_before;
    5302                 :       74451 :               gimple *insert_point
    5303                 :       74451 :                 = find_insert_point (stmt, oe1->op, oe2->op, insert_before);
    5304                 :       74451 :               lhs = make_ssa_name (TREE_TYPE (lhs));
    5305                 :       74451 :               stmt
    5306                 :       74451 :                 = gimple_build_assign (lhs, rhs_code,
    5307                 :             :                                        oe1->op, oe2->op);
    5308                 :       74451 :               gimple_set_uid (stmt, uid);
    5309                 :       74451 :               gimple_set_visited (stmt, true);
    5310                 :       74451 :               if (insert_before)
    5311                 :       58063 :                 gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
    5312                 :             :               else
    5313                 :       16388 :                 insert_stmt_after (stmt, insert_point);
    5314                 :       74451 :             }
    5315                 :             :           else
    5316                 :             :             {
    5317                 :      642805 :               bool insert_before;
    5318                 :      642805 :               gcc_checking_assert (find_insert_point (stmt, oe1->op, oe2->op,
    5319                 :             :                                                       insert_before)
    5320                 :             :                                    == stmt);
    5321                 :      642805 :               gimple_assign_set_rhs1 (stmt, oe1->op);
    5322                 :      642805 :               gimple_assign_set_rhs2 (stmt, oe2->op);
    5323                 :      642805 :               update_stmt (stmt);
    5324                 :             :             }
    5325                 :             : 
    5326                 :      717256 :           if (rhs1 != oe1->op && rhs1 != oe2->op)
    5327                 :       38341 :             remove_visited_stmt_chain (rhs1);
    5328                 :             : 
    5329                 :      717256 :           if (dump_file && (dump_flags & TDF_DETAILS))
    5330                 :             :             {
    5331                 :          29 :               fprintf (dump_file, " into ");
    5332                 :          29 :               print_gimple_stmt (dump_file, stmt, 0);
    5333                 :             :             }
    5334                 :             :         }
    5335                 :     3818459 :       return lhs;
    5336                 :             :     }
    5337                 :             : 
    5338                 :             :   /* If we hit here, we should have 3 or more ops left.  */
    5339                 :      210153 :   gcc_assert (opindex + 2 < ops.length ());
    5340                 :             : 
    5341                 :             :   /* Rewrite the next operator.  */
    5342                 :      210153 :   oe = ops[opindex];
    5343                 :             : 
    5344                 :             :   /* If the stmt that defines operand has to be inserted, insert it
    5345                 :             :      before the use.  */
    5346                 :      210153 :   if (oe->stmt_to_insert)
    5347                 :           2 :     insert_stmt_before_use (stmt, oe->stmt_to_insert);
    5348                 :             : 
    5349                 :             :   /* Recurse on the LHS of the binary operator, which is guaranteed to
    5350                 :             :      be the non-leaf side.  */
    5351                 :      210153 :   tree new_rhs1
    5352                 :      210153 :     = rewrite_expr_tree (SSA_NAME_DEF_STMT (rhs1), rhs_code, opindex + 1, ops,
    5353                 :      210153 :                          changed || oe->op != rhs2 || next_changed,
    5354                 :             :                          false);
    5355                 :             : 
    5356                 :      210153 :   if (oe->op != rhs2 || new_rhs1 != rhs1)
    5357                 :             :     {
    5358                 :       99748 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5359                 :             :         {
    5360                 :           6 :           fprintf (dump_file, "Transforming ");
    5361                 :           6 :           print_gimple_stmt (dump_file, stmt, 0);
    5362                 :             :         }
    5363                 :             : 
    5364                 :             :       /* If changed is false, this is either opindex == 0
    5365                 :             :          or all outer rhs2's were equal to corresponding oe->op,
    5366                 :             :          and powi_result is NULL.
    5367                 :             :          That means lhs is equivalent before and after reassociation.
    5368                 :             :          Otherwise ensure the old lhs SSA_NAME is not reused and
    5369                 :             :          create a new stmt as well, so that any debug stmts will be
    5370                 :             :          properly adjusted.  */
    5371                 :       99748 :       if (changed)
    5372                 :             :         {
    5373                 :       24764 :           gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    5374                 :       24764 :           unsigned int uid = gimple_uid (stmt);
    5375                 :       24764 :           bool insert_before;
    5376                 :       24764 :           gimple *insert_point = find_insert_point (stmt, new_rhs1, oe->op,
    5377                 :             :                                                     insert_before);
    5378                 :             : 
    5379                 :       24764 :           lhs = make_ssa_name (TREE_TYPE (lhs));
    5380                 :       24764 :           stmt = gimple_build_assign (lhs, rhs_code,
    5381                 :             :                                       new_rhs1, oe->op);
    5382                 :       24764 :           gimple_set_uid (stmt, uid);
    5383                 :       24764 :           gimple_set_visited (stmt, true);
    5384                 :       24764 :           if (insert_before)
    5385                 :       14244 :             gsi_insert_before (&gsi, stmt, GSI_SAME_STMT);
    5386                 :             :           else
    5387                 :       10520 :             insert_stmt_after (stmt, insert_point);
    5388                 :             :         }
    5389                 :             :       else
    5390                 :             :         {
    5391                 :       74984 :           bool insert_before;
    5392                 :       74984 :           gcc_checking_assert (find_insert_point (stmt, new_rhs1, oe->op,
    5393                 :             :                                                   insert_before)
    5394                 :             :                                == stmt);
    5395                 :       74984 :           gimple_assign_set_rhs1 (stmt, new_rhs1);
    5396                 :       74984 :           gimple_assign_set_rhs2 (stmt, oe->op);
    5397                 :       74984 :           update_stmt (stmt);
    5398                 :             :         }
    5399                 :             : 
    5400                 :       99748 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5401                 :             :         {
    5402                 :           6 :           fprintf (dump_file, " into ");
    5403                 :           6 :           print_gimple_stmt (dump_file, stmt, 0);
    5404                 :             :         }
    5405                 :             :     }
    5406                 :             :   return lhs;
    5407                 :             : }
    5408                 :             : 
    5409                 :             : /* Find out how many cycles we need to compute statements chain.
    5410                 :             :    OPS_NUM holds number os statements in a chain.  CPU_WIDTH is a
    5411                 :             :    maximum number of independent statements we may execute per cycle.  */
    5412                 :             : 
    5413                 :             : static int
    5414                 :       15327 : get_required_cycles (int ops_num, int cpu_width)
    5415                 :             : {
    5416                 :       15327 :   int res;
    5417                 :       15327 :   int elog;
    5418                 :       15327 :   unsigned int rest;
    5419                 :             : 
    5420                 :             :   /* While we have more than 2 * cpu_width operands
    5421                 :             :      we may reduce number of operands by cpu_width
    5422                 :             :      per cycle.  */
    5423                 :       15327 :   res = ops_num / (2 * cpu_width);
    5424                 :             : 
    5425                 :             :   /* Remained operands count may be reduced twice per cycle
    5426                 :             :      until we have only one operand.  */
    5427                 :       15327 :   rest = (unsigned)(ops_num - res * cpu_width);
    5428                 :       15327 :   elog = exact_log2 (rest);
    5429                 :        7217 :   if (elog >= 0)
    5430                 :        7217 :     res += elog;
    5431                 :             :   else
    5432                 :       16220 :     res += floor_log2 (rest) + 1;
    5433                 :             : 
    5434                 :       15327 :   return res;
    5435                 :             : }
    5436                 :             : 
    5437                 :             : /* Given that the target fully pipelines FMA instructions, return the latency
    5438                 :             :    of MULT_EXPRs that can't be hidden by the FMAs.  WIDTH is the number of
    5439                 :             :    pipes.  */
    5440                 :             : 
    5441                 :             : static inline int
    5442                 :           0 : get_mult_latency_consider_fma (int ops_num, int mult_num, int width)
    5443                 :             : {
    5444                 :           0 :   gcc_checking_assert (mult_num && mult_num <= ops_num);
    5445                 :             : 
    5446                 :             :   /* For each partition, if mult_num == ops_num, there's latency(MULT)*2.
    5447                 :             :      e.g:
    5448                 :             : 
    5449                 :             :         A * B + C * D
    5450                 :             :         =>
    5451                 :             :         _1 = A * B;
    5452                 :             :         _2 = .FMA (C, D, _1);
    5453                 :             : 
    5454                 :             :       Otherwise there's latency(MULT)*1 in the first FMA.  */
    5455                 :           0 :   return CEIL (ops_num, width) == CEIL (mult_num, width) ? 2 : 1;
    5456                 :             : }
    5457                 :             : 
    5458                 :             : /* Returns an optimal number of registers to use for computation of
    5459                 :             :    given statements.
    5460                 :             : 
    5461                 :             :    LHS is the result ssa name of OPS.  MULT_NUM is number of sub-expressions
    5462                 :             :    that are MULT_EXPRs, when OPS are PLUS_EXPRs or MINUS_EXPRs.  */
    5463                 :             : 
    5464                 :             : static int
    5465                 :       19113 : get_reassociation_width (vec<operand_entry *> *ops, int mult_num, tree lhs,
    5466                 :             :                          enum tree_code opc, machine_mode mode)
    5467                 :             : {
    5468                 :       19113 :   int param_width = param_tree_reassoc_width;
    5469                 :       19113 :   int width;
    5470                 :       19113 :   int width_min;
    5471                 :       19113 :   int cycles_best;
    5472                 :       19113 :   int ops_num = ops->length ();
    5473                 :             : 
    5474                 :       19113 :   if (param_width > 0)
    5475                 :             :     width = param_width;
    5476                 :             :   else
    5477                 :       19064 :     width = targetm.sched.reassociation_width (opc, mode);
    5478                 :             : 
    5479                 :       19113 :   if (width == 1)
    5480                 :             :     return width;
    5481                 :             : 
    5482                 :             :   /* Get the minimal time required for sequence computation.  */
    5483                 :        5840 :   cycles_best = get_required_cycles (ops_num, width);
    5484                 :             : 
    5485                 :             :   /* Check if we may use less width and still compute sequence for
    5486                 :             :      the same time.  It will allow us to reduce registers usage.
    5487                 :             :      get_required_cycles is monotonically increasing with lower width
    5488                 :             :      so we can perform a binary search for the minimal width that still
    5489                 :             :      results in the optimal cycle count.  */
    5490                 :        5840 :   width_min = 1;
    5491                 :             : 
    5492                 :             :   /* If the target fully pipelines FMA instruction, the multiply part can start
    5493                 :             :      already if its operands are ready.  Assuming symmetric pipes are used for
    5494                 :             :      FMUL/FADD/FMA, then for a sequence of FMA like:
    5495                 :             : 
    5496                 :             :         _8 = .FMA (_2, _3, _1);
    5497                 :             :         _9 = .FMA (_5, _4, _8);
    5498                 :             :         _10 = .FMA (_7, _6, _9);
    5499                 :             : 
    5500                 :             :      , if width=1, the latency is latency(MULT) + latency(ADD)*3.
    5501                 :             :      While with width=2:
    5502                 :             : 
    5503                 :             :         _8 = _4 * _5;
    5504                 :             :         _9 = .FMA (_2, _3, _1);
    5505                 :             :         _10 = .FMA (_6, _7, _8);
    5506                 :             :         _11 = _9 + _10;
    5507                 :             : 
    5508                 :             :      , it is latency(MULT)*2 + latency(ADD)*2.  Assuming latency(MULT) >=
    5509                 :             :      latency(ADD), the first variant is preferred.
    5510                 :             : 
    5511                 :             :      Find out if we can get a smaller width considering FMA.  */
    5512                 :        5840 :   if (width > 1 && mult_num && param_fully_pipelined_fma)
    5513                 :             :     {
    5514                 :             :       /* When param_fully_pipelined_fma is set, assume FMUL and FMA use the
    5515                 :             :          same units that can also do FADD.  For other scenarios, such as when
    5516                 :             :          FMUL and FADD are using separated units, the following code may not
    5517                 :             :          appy.  */
    5518                 :           0 :       int width_mult = targetm.sched.reassociation_width (MULT_EXPR, mode);
    5519                 :           0 :       gcc_checking_assert (width_mult <= width);
    5520                 :             : 
    5521                 :             :       /* Latency of MULT_EXPRs.  */
    5522                 :           0 :       int lat_mul
    5523                 :           0 :         = get_mult_latency_consider_fma (ops_num, mult_num, width_mult);
    5524                 :             : 
    5525                 :             :       /* Quick search might not apply.  So start from 1.  */
    5526                 :           0 :       for (int i = 1; i < width_mult; i++)
    5527                 :             :         {
    5528                 :           0 :           int lat_mul_new
    5529                 :           0 :             = get_mult_latency_consider_fma (ops_num, mult_num, i);
    5530                 :           0 :           int lat_add_new = get_required_cycles (ops_num, i);
    5531                 :             : 
    5532                 :             :           /* Assume latency(MULT) >= latency(ADD).  */
    5533                 :           0 :           if (lat_mul - lat_mul_new >= lat_add_new - cycles_best)
    5534                 :             :             {
    5535                 :             :               width = i;
    5536                 :             :               break;
    5537                 :             :             }
    5538                 :             :         }
    5539                 :             :     }
    5540                 :             :   else
    5541                 :             :     {
    5542                 :       13831 :       while (width > width_min)
    5543                 :             :         {
    5544                 :        9487 :           int width_mid = (width + width_min) / 2;
    5545                 :             : 
    5546                 :        9487 :           if (get_required_cycles (ops_num, width_mid) == cycles_best)
    5547                 :             :             width = width_mid;
    5548                 :        1632 :           else if (width_min < width_mid)
    5549                 :             :             width_min = width_mid;
    5550                 :             :           else
    5551                 :             :             break;
    5552                 :             :         }
    5553                 :             :     }
    5554                 :             : 
    5555                 :             :   /* If there's loop dependent FMA result, return width=2 to avoid it.  This is
    5556                 :             :      better than skipping these FMA candidates in widening_mul.  */
    5557                 :        5840 :   if (width == 1
    5558                 :        5840 :       && maybe_le (tree_to_poly_int64 (TYPE_SIZE (TREE_TYPE (lhs))),
    5559                 :             :                    param_avoid_fma_max_bits))
    5560                 :             :     {
    5561                 :             :       /* Look for cross backedge dependency:
    5562                 :             :         1. LHS is a phi argument in the same basic block it is defined.
    5563                 :             :         2. And the result of the phi node is used in OPS.  */
    5564                 :        3960 :       basic_block bb = gimple_bb (SSA_NAME_DEF_STMT (lhs));
    5565                 :             : 
    5566                 :        3960 :       use_operand_p use_p;
    5567                 :        3960 :       imm_use_iterator iter;
    5568                 :        8234 :       FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
    5569                 :        6136 :         if (gphi *phi = dyn_cast<gphi *> (USE_STMT (use_p)))
    5570                 :             :           {
    5571                 :        3342 :             if (gimple_phi_arg_edge (phi, phi_arg_index_from_use (use_p))->src
    5572                 :             :                 != bb)
    5573                 :           0 :               continue;
    5574                 :        3342 :             tree phi_result = gimple_phi_result (phi);
    5575                 :        3342 :             operand_entry *oe;
    5576                 :        3342 :             unsigned int j;
    5577                 :       15780 :             FOR_EACH_VEC_ELT (*ops, j, oe)
    5578                 :             :               {
    5579                 :       10026 :                 if (TREE_CODE (oe->op) != SSA_NAME)
    5580                 :           0 :                   continue;
    5581                 :             : 
    5582                 :             :                 /* Result of phi is operand of PLUS_EXPR.  */
    5583                 :       10026 :                 if (oe->op == phi_result)
    5584                 :        1862 :                   return 2;
    5585                 :             : 
    5586                 :             :                 /* Check is result of phi is operand of MULT_EXPR.  */
    5587                 :        8164 :                 gimple *def_stmt = SSA_NAME_DEF_STMT (oe->op);
    5588                 :        8164 :                 if (is_gimple_assign (def_stmt)
    5589                 :        8164 :                     && gimple_assign_rhs_code (def_stmt) == NEGATE_EXPR)
    5590                 :             :                   {
    5591                 :        1668 :                     tree rhs = gimple_assign_rhs1 (def_stmt);
    5592                 :        1668 :                     if (TREE_CODE (rhs) == SSA_NAME)
    5593                 :             :                       {
    5594                 :        1668 :                         if (rhs == phi_result)
    5595                 :             :                           return 2;
    5596                 :        1668 :                         def_stmt = SSA_NAME_DEF_STMT (rhs);
    5597                 :             :                       }
    5598                 :             :                   }
    5599                 :        8164 :                 if (is_gimple_assign (def_stmt)
    5600                 :        8164 :                     && gimple_assign_rhs_code (def_stmt) == MULT_EXPR)
    5601                 :             :                   {
    5602                 :        6684 :                     if (gimple_assign_rhs1 (def_stmt) == phi_result
    5603                 :        6684 :                         || gimple_assign_rhs2 (def_stmt) == phi_result)
    5604                 :             :                       return 2;
    5605                 :             :                   }
    5606                 :             :               }
    5607                 :             :           }
    5608                 :             :     }
    5609                 :             : 
    5610                 :             :   return width;
    5611                 :             : }
    5612                 :             : 
    5613                 :             : #define SPECIAL_BIASED_END_STMT 0 /* It is the end stmt of all ops.  */
    5614                 :             : #define BIASED_END_STMT 1 /* It is the end stmt of normal or biased ops.  */
    5615                 :             : #define NORMAL_END_STMT 2 /* It is the end stmt of normal ops.  */
    5616                 :             : 
    5617                 :             : /* Rewrite statements with dependency chain with regard the chance to generate
    5618                 :             :    FMA.
    5619                 :             :    For the chain with FMA: Try to keep fma opportunity as much as possible.
    5620                 :             :    For the chain without FMA: Putting the computation in rank order and trying
    5621                 :             :    to allow operations to be executed in parallel.
    5622                 :             :    E.g.
    5623                 :             :    e + f + a * b + c * d;
    5624                 :             : 
    5625                 :             :    ssa1 = e + a * b;
    5626                 :             :    ssa2 = f + c * d;
    5627                 :             :    ssa3 = ssa1 + ssa2;
    5628                 :             : 
    5629                 :             :    This reassociation approach preserves the chance of fma generation as much
    5630                 :             :    as possible.
    5631                 :             : 
    5632                 :             :    Another thing is to avoid adding loop-carried ops to long chains, otherwise
    5633                 :             :    the whole chain will have dependencies across the loop iteration.  Just keep
    5634                 :             :    loop-carried ops in a separate chain.
    5635                 :             :    E.g.
    5636                 :             :    x_1 = phi (x_0, x_2)
    5637                 :             :    y_1 = phi (y_0, y_2)
    5638                 :             : 
    5639                 :             :    a + b + c + d + e + x1 + y1
    5640                 :             : 
    5641                 :             :    SSA1 = a + b;
    5642                 :             :    SSA2 = c + d;
    5643                 :             :    SSA3 = SSA1 + e;
    5644                 :             :    SSA4 = SSA3 + SSA2;
    5645                 :             :    SSA5 = x1 + y1;
    5646                 :             :    SSA6 = SSA4 + SSA5;
    5647                 :             :  */
    5648                 :             : static void
    5649                 :        1492 : rewrite_expr_tree_parallel (gassign *stmt, int width, bool has_fma,
    5650                 :             :                             const vec<operand_entry *> &ops)
    5651                 :             : {
    5652                 :        1492 :   enum tree_code opcode = gimple_assign_rhs_code (stmt);
    5653                 :        1492 :   int op_num = ops.length ();
    5654                 :        1492 :   int op_normal_num = op_num;
    5655                 :        1492 :   gcc_assert (op_num > 0);
    5656                 :        1492 :   int stmt_num = op_num - 1;
    5657                 :        1492 :   gimple **stmts = XALLOCAVEC (gimple *, stmt_num);
    5658                 :        1492 :   int i = 0, j = 0;
    5659                 :        1492 :   tree tmp_op[2], op1;
    5660                 :        1492 :   operand_entry *oe;
    5661                 :        1492 :   gimple *stmt1 = NULL;
    5662                 :        1492 :   tree last_rhs1 = gimple_assign_rhs1 (stmt);
    5663                 :        1492 :   int last_rhs1_stmt_index = 0, last_rhs2_stmt_index = 0;
    5664                 :        1492 :   int width_active = 0, width_count = 0;
    5665                 :        1492 :   bool has_biased = false, ops_changed = false;
    5666                 :        1492 :   auto_vec<operand_entry *> ops_normal;
    5667                 :        1492 :   auto_vec<operand_entry *> ops_biased;
    5668                 :        1492 :   vec<operand_entry *> *ops1;
    5669                 :             : 
    5670                 :             :   /* We start expression rewriting from the top statements.
    5671                 :             :      So, in this loop we create a full list of statements
    5672                 :             :      we will work with.  */
    5673                 :        1492 :   stmts[stmt_num - 1] = stmt;
    5674                 :        7204 :   for (i = stmt_num - 2; i >= 0; i--)
    5675                 :        5712 :     stmts[i] = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmts[i+1]));
    5676                 :             : 
    5677                 :             :   /* Avoid adding loop-carried ops to long chains, first filter out the
    5678                 :             :      loop-carried.  But we need to make sure that the length of the remainder
    5679                 :             :      is not less than 4, which is the smallest ops length we can break the
    5680                 :             :      dependency.  */
    5681                 :       10188 :   FOR_EACH_VEC_ELT (ops, i, oe)
    5682                 :             :     {
    5683                 :        8696 :       if (TREE_CODE (oe->op) == SSA_NAME
    5684                 :        8511 :           && bitmap_bit_p (biased_names, SSA_NAME_VERSION (oe->op))
    5685                 :        8844 :           && op_normal_num > 4)
    5686                 :             :         {
    5687                 :         125 :           ops_biased.safe_push (oe);
    5688                 :         125 :           has_biased = true;
    5689                 :         125 :           op_normal_num --;
    5690                 :             :         }
    5691                 :             :       else
    5692                 :        8571 :         ops_normal.safe_push (oe);
    5693                 :             :     }
    5694                 :             : 
    5695                 :             :   /* Width should not be larger than ops length / 2, since we can not create
    5696                 :             :      more parallel dependency chains that exceeds such value.  */
    5697                 :        1492 :   int width_normal = op_normal_num / 2;
    5698                 :        1492 :   int width_biased = (op_num - op_normal_num) / 2;
    5699                 :        1492 :   width_normal = width <= width_normal ? width : width_normal;
    5700                 :        1492 :   width_biased = width <= width_biased ? width : width_biased;
    5701                 :             : 
    5702                 :        1492 :   ops1 = &ops_normal;
    5703                 :        1492 :   width_count = width_active = width_normal;
    5704                 :             : 
    5705                 :             :   /* Build parallel dependency chain according to width.  */
    5706                 :        8696 :   for (i = 0; i < stmt_num; i++)
    5707                 :             :     {
    5708                 :        7204 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5709                 :             :         {
    5710                 :           6 :           fprintf (dump_file, "Transforming ");
    5711                 :           6 :           print_gimple_stmt (dump_file, stmts[i], 0);
    5712                 :             :         }
    5713                 :             : 
    5714                 :             :       /* When the work of normal ops is over, but the loop is not over,
    5715                 :             :          continue to do biased ops.  */
    5716                 :        7204 :       if (width_count == 0 && ops1 == &ops_normal)
    5717                 :             :         {
    5718                 :         121 :           ops1 = &ops_biased;
    5719                 :         121 :           width_count = width_active = width_biased;
    5720                 :         121 :           ops_changed = true;
    5721                 :             :         }
    5722                 :             : 
    5723                 :             :       /* Swap the operands if no FMA in the chain.  */
    5724                 :        7204 :       if (ops1->length () > 2 && !has_fma)
    5725                 :        3549 :         swap_ops_for_binary_stmt (*ops1, ops1->length () - 3);
    5726                 :             : 
    5727                 :        7204 :       if (i < width_active
    5728                 :        4088 :           || (ops_changed && i <= (last_rhs1_stmt_index + width_active)))
    5729                 :             :         {
    5730                 :        9357 :           for (j = 0; j < 2; j++)
    5731                 :             :             {
    5732                 :        6238 :               oe = ops1->pop ();
    5733                 :        6238 :               tmp_op[j] = oe->op;
    5734                 :             :               /* If the stmt that defines operand has to be inserted, insert it
    5735                 :             :                  before the use.  */
    5736                 :        6238 :               stmt1 = oe->stmt_to_insert;
    5737                 :        6238 :               if (stmt1)
    5738                 :           0 :                 insert_stmt_before_use (stmts[i], stmt1);
    5739                 :        6238 :               stmt1 = NULL;
    5740                 :             :             }
    5741                 :        3119 :           stmts[i] = build_and_add_sum (TREE_TYPE (last_rhs1),
    5742                 :             :                                         tmp_op[1],
    5743                 :             :                                         tmp_op[0],
    5744                 :             :                                         opcode);
    5745                 :        3119 :           gimple_set_visited (stmts[i], true);
    5746                 :             : 
    5747                 :             :         }
    5748                 :             :       else
    5749                 :             :         {
    5750                 :             :           /* We keep original statement only for the last one.  All others are
    5751                 :             :              recreated.  */
    5752                 :        4085 :           if (!ops1->length ())
    5753                 :             :             {
    5754                 :             :               /* For biased length equal to 2.  */
    5755                 :        1627 :               if (width_count == BIASED_END_STMT && !last_rhs2_stmt_index)
    5756                 :           1 :                 last_rhs2_stmt_index = i - 1;
    5757                 :             : 
    5758                 :             :               /* When width_count == 2 and there is no biased, just finish.  */
    5759                 :        1627 :               if (width_count == NORMAL_END_STMT && !has_biased)
    5760                 :             :                 {
    5761                 :        1371 :                   last_rhs1_stmt_index = i - 1;
    5762                 :        1371 :                   last_rhs2_stmt_index = i - 2;
    5763                 :             :                 }
    5764                 :        1627 :               if (last_rhs1_stmt_index && (last_rhs2_stmt_index || !has_biased))
    5765                 :             :                 {
    5766                 :             :                   /* We keep original statement only for the last one.  All
    5767                 :             :                      others are recreated.  */
    5768                 :        1373 :                   gimple_assign_set_rhs1 (stmts[i], gimple_assign_lhs
    5769                 :        1373 :                                           (stmts[last_rhs1_stmt_index]));
    5770                 :        1373 :                   gimple_assign_set_rhs2 (stmts[i], gimple_assign_lhs
    5771                 :        1373 :                                           (stmts[last_rhs2_stmt_index]));
    5772                 :        1373 :                   update_stmt (stmts[i]);
    5773                 :             :                 }
    5774                 :             :               else
    5775                 :             :                 {
    5776                 :         762 :                   stmts[i] =
    5777                 :         254 :                     build_and_add_sum (TREE_TYPE (last_rhs1),
    5778                 :         254 :                                        gimple_assign_lhs (stmts[i-width_count]),
    5779                 :             :                                        gimple_assign_lhs
    5780                 :         254 :                                        (stmts[i-width_count+1]),
    5781                 :             :                                        opcode);
    5782                 :         254 :                   gimple_set_visited (stmts[i], true);
    5783                 :         254 :                   width_count--;
    5784                 :             : 
    5785                 :             :                   /* It is the end of normal or biased ops.
    5786                 :             :                      last_rhs1_stmt_index used to record the last stmt index
    5787                 :             :                      for normal ops.  last_rhs2_stmt_index used to record the
    5788                 :             :                      last stmt index for biased ops.  */
    5789                 :         254 :                   if (width_count == BIASED_END_STMT)
    5790                 :             :                     {
    5791                 :         122 :                       gcc_assert (has_biased);
    5792                 :         122 :                       if (ops_biased.length ())
    5793                 :             :                         last_rhs1_stmt_index = i;
    5794                 :             :                       else
    5795                 :           1 :                         last_rhs2_stmt_index = i;
    5796                 :             :                       width_count--;
    5797                 :             :                     }
    5798                 :             :                 }
    5799                 :             :             }
    5800                 :             :           else
    5801                 :             :             {
    5802                 :             :               /* Attach the rest ops to the parallel dependency chain.  */
    5803                 :        2458 :               oe = ops1->pop ();
    5804                 :        2458 :               op1 = oe->op;
    5805                 :        2458 :               stmt1 = oe->stmt_to_insert;
    5806                 :        2458 :               if (stmt1)
    5807                 :           0 :                 insert_stmt_before_use (stmts[i], stmt1);
    5808                 :        2458 :               stmt1 = NULL;
    5809                 :             : 
    5810                 :             :               /* For only one biased ops.  */
    5811                 :        2458 :               if (width_count == SPECIAL_BIASED_END_STMT)
    5812                 :             :                 {
    5813                 :             :                   /* We keep original statement only for the last one.  All
    5814                 :             :                      others are recreated.  */
    5815                 :         119 :                   gcc_assert (has_biased);
    5816                 :         119 :                   gimple_assign_set_rhs1 (stmts[i], gimple_assign_lhs
    5817                 :         119 :                                           (stmts[last_rhs1_stmt_index]));
    5818                 :         119 :                   gimple_assign_set_rhs2 (stmts[i], op1);
    5819                 :         119 :                   update_stmt (stmts[i]);
    5820                 :             :                 }
    5821                 :             :               else
    5822                 :             :                 {
    5823                 :        2339 :                   stmts[i] = build_and_add_sum (TREE_TYPE (last_rhs1),
    5824                 :             :                                                 gimple_assign_lhs
    5825                 :        2339 :                                                 (stmts[i-width_active]),
    5826                 :             :                                                 op1,
    5827                 :             :                                                 opcode);
    5828                 :        2339 :                   gimple_set_visited (stmts[i], true);
    5829                 :             :                 }
    5830                 :             :             }
    5831                 :             :         }
    5832                 :             : 
    5833                 :        7204 :       if (dump_file && (dump_flags & TDF_DETAILS))
    5834                 :             :         {
    5835                 :           6 :           fprintf (dump_file, " into ");
    5836                 :           6 :           print_gimple_stmt (dump_file, stmts[i], 0);
    5837                 :             :         }
    5838                 :             :     }
    5839                 :             : 
    5840                 :        1492 :   remove_visited_stmt_chain (last_rhs1);
    5841                 :        1492 : }
    5842                 :             : 
    5843                 :             : /* Transform STMT, which is really (A +B) + (C + D) into the left
    5844                 :             :    linear form, ((A+B)+C)+D.
    5845                 :             :    Recurse on D if necessary.  */
    5846                 :             : 
    5847                 :             : static void
    5848                 :        2449 : linearize_expr (gimple *stmt)
    5849                 :             : {
    5850                 :        2449 :   gimple_stmt_iterator gsi;
    5851                 :        2449 :   gimple *binlhs = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (stmt));
    5852                 :        2449 :   gimple *binrhs = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt));
    5853                 :        2449 :   gimple *oldbinrhs = binrhs;
    5854                 :        2449 :   enum tree_code rhscode = gimple_assign_rhs_code (stmt);
    5855                 :        2449 :   gimple *newbinrhs = NULL;
    5856                 :        2449 :   class loop *loop = loop_containing_stmt (stmt);
    5857                 :        2449 :   tree lhs = gimple_assign_lhs (stmt);
    5858                 :             : 
    5859                 :        2449 :   gcc_assert (is_reassociable_op (binlhs, rhscode, loop)
    5860                 :             :               && is_reassociable_op (binrhs, rhscode, loop));
    5861                 :             : 
    5862                 :        2449 :   gsi = gsi_for_stmt (stmt);
    5863                 :             : 
    5864                 :        2449 :   gimple_assign_set_rhs2 (stmt, gimple_assign_rhs1 (binrhs));
    5865                 :        2449 :   binrhs = gimple_build_assign (make_ssa_name (TREE_TYPE (lhs)),
    5866                 :             :                                 gimple_assign_rhs_code (binrhs),
    5867                 :             :                                 gimple_assign_lhs (binlhs),
    5868                 :             :                                 gimple_assign_rhs2 (binrhs));
    5869                 :        2449 :   gimple_assign_set_rhs1 (stmt, gimple_assign_lhs (binrhs));
    5870                 :        2449 :   gsi_insert_before (&gsi, binrhs, GSI_SAME_STMT);
    5871                 :        2449 :   gimple_set_uid (binrhs, gimple_uid (stmt));
    5872                 :             : 
    5873                 :        2449 :   if (TREE_CODE (gimple_assign_rhs2 (stmt)) == SSA_NAME)
    5874                 :        2444 :     newbinrhs = SSA_NAME_DEF_STMT (gimple_assign_rhs2 (stmt));
    5875                 :             : 
    5876                 :        2449 :   if (dump_file && (dump_flags & TDF_DETAILS))
    5877                 :             :     {
    5878                 :           0 :       fprintf (dump_file, "Linearized: ");
    5879                 :           0 :       print_gimple_stmt (dump_file, stmt, 0);
    5880                 :             :     }
    5881                 :             : 
    5882                 :        2449 :   reassociate_stats.linearized++;
    5883                 :        2449 :   update_stmt (stmt);
    5884                 :             : 
    5885                 :        2449 :   gsi = gsi_for_stmt (oldbinrhs);
    5886                 :        2449 :   reassoc_remove_stmt (&gsi);
    5887                 :        2449 :   release_defs (oldbinrhs);
    5888                 :             : 
    5889                 :        2449 :   gimple_set_visited (stmt, true);
    5890                 :        2449 :   gimple_set_visited (binlhs, true);
    5891                 :        2449 :   gimple_set_visited (binrhs, true);
    5892                 :             : 
    5893                 :             :   /* Tail recurse on the new rhs if it still needs reassociation.  */
    5894                 :        2449 :   if (newbinrhs && is_reassociable_op (newbinrhs, rhscode, loop))
    5895                 :             :     /* ??? This should probably be linearize_expr (newbinrhs) but I don't
    5896                 :             :            want to change the algorithm while converting to tuples.  */
    5897                 :         662 :     linearize_expr (stmt);
    5898                 :        2449 : }
    5899                 :             : 
    5900                 :             : /* If LHS has a single immediate use that is a GIMPLE_ASSIGN statement, return
    5901                 :             :    it.  Otherwise, return NULL.  */
    5902                 :             : 
    5903                 :             : static gimple *
    5904                 :      356159 : get_single_immediate_use (tree lhs)
    5905                 :             : {
    5906                 :      356159 :   use_operand_p immuse;
    5907                 :      356159 :   gimple *immusestmt;
    5908                 :             : 
    5909                 :      356159 :   if (TREE_CODE (lhs) == SSA_NAME
    5910                 :      356159 :       && single_imm_use (lhs, &immuse, &immusestmt)
    5911                 :      620990 :       && is_gimple_assign (immusestmt))
    5912                 :             :     return immusestmt;
    5913                 :             : 
    5914                 :             :   return NULL;
    5915                 :             : }
    5916                 :             : 
    5917                 :             : /* Recursively negate the value of TONEGATE, and return the SSA_NAME
    5918                 :             :    representing the negated value.  Insertions of any necessary
    5919                 :             :    instructions go before GSI.
    5920                 :             :    This function is recursive in that, if you hand it "a_5" as the
    5921                 :             :    value to negate, and a_5 is defined by "a_5 = b_3 + b_4", it will
    5922                 :             :    transform b_3 + b_4 into a_5 = -b_3 + -b_4.  */
    5923                 :             : 
    5924                 :             : static tree
    5925                 :       64406 : negate_value (tree tonegate, gimple_stmt_iterator *gsip)
    5926                 :             : {
    5927                 :       64406 :   gimple *negatedefstmt = NULL;
    5928                 :       64406 :   tree resultofnegate;
    5929                 :       64406 :   gimple_stmt_iterator gsi;
    5930                 :       64406 :   unsigned int uid;
    5931                 :             : 
    5932                 :             :   /* If we are trying to negate a name, defined by an add, negate the
    5933                 :             :      add operands instead.  */
    5934                 :       64406 :   if (TREE_CODE (tonegate) == SSA_NAME)
    5935                 :       62937 :     negatedefstmt = SSA_NAME_DEF_STMT (tonegate);
    5936                 :       64406 :   if (TREE_CODE (tonegate) == SSA_NAME
    5937                 :       62937 :       && is_gimple_assign (negatedefstmt)
    5938                 :       51868 :       && TREE_CODE (gimple_assign_lhs (negatedefstmt)) == SSA_NAME
    5939                 :       51868 :       && has_single_use (gimple_assign_lhs (negatedefstmt))
    5940                 :      103900 :       && gimple_assign_rhs_code (negatedefstmt) == PLUS_EXPR)
    5941                 :             :     {
    5942                 :         820 :       tree rhs1 = gimple_assign_rhs1 (negatedefstmt);
    5943                 :         820 :       tree rhs2 = gimple_assign_rhs2 (negatedefstmt);
    5944                 :         820 :       tree lhs = gimple_assign_lhs (negatedefstmt);
    5945                 :         820 :       gimple *g;
    5946                 :             : 
    5947                 :         820 :       gsi = gsi_for_stmt (negatedefstmt);
    5948                 :         820 :       rhs1 = negate_value (rhs1, &gsi);
    5949                 :             : 
    5950                 :         820 :       gsi = gsi_for_stmt (negatedefstmt);
    5951                 :         820 :       rhs2 = negate_value (rhs2, &gsi);
    5952                 :             : 
    5953                 :         820 :       gsi = gsi_for_stmt (negatedefstmt);
    5954                 :         820 :       lhs = make_ssa_name (TREE_TYPE (lhs));
    5955                 :         820 :       gimple_set_visited (negatedefstmt, true);
    5956                 :         820 :       g = gimple_build_assign (lhs, PLUS_EXPR, rhs1, rhs2);
    5957                 :         820 :       gimple_set_uid (g, gimple_uid (negatedefstmt));
    5958                 :         820 :       gsi_insert_before (&gsi, g, GSI_SAME_STMT);
    5959                 :         820 :       return lhs;
    5960                 :             :     }
    5961                 :             : 
    5962                 :       63586 :   tonegate = fold_build1 (NEGATE_EXPR, TREE_TYPE (tonegate), tonegate);
    5963                 :       63586 :   resultofnegate = force_gimple_operand_gsi (gsip, tonegate, true,
    5964                 :             :                                              NULL_TREE, true, GSI_SAME_STMT);
    5965                 :       63586 :   gsi = *gsip;
    5966                 :       63586 :   uid = gimple_uid (gsi_stmt (gsi));
    5967                 :      251406 :   for (gsi_prev (&gsi); !gsi_end_p (gsi); gsi_prev (&gsi))
    5968                 :             :     {
    5969                 :      120064 :       gimple *stmt = gsi_stmt (gsi);
    5970                 :      120064 :       if (gimple_uid (stmt) != 0)
    5971                 :             :         break;
    5972                 :       62117 :       gimple_set_uid (stmt, uid);
    5973                 :             :     }
    5974                 :             :   return resultofnegate;
    5975                 :             : }
    5976                 :             : 
    5977                 :             : /* Return true if we should break up the subtract in STMT into an add
    5978                 :             :    with negate.  This is true when we the subtract operands are really
    5979                 :             :    adds, or the subtract itself is used in an add expression.  In
    5980                 :             :    either case, breaking up the subtract into an add with negate
    5981                 :             :    exposes the adds to reassociation.  */
    5982                 :             : 
    5983                 :             : static bool
    5984                 :      255212 : should_break_up_subtract (gimple *stmt)
    5985                 :             : {
    5986                 :      255212 :   tree lhs = gimple_assign_lhs (stmt);
    5987                 :      255212 :   tree binlhs = gimple_assign_rhs1 (stmt);
    5988                 :      255212 :   tree binrhs = gimple_assign_rhs2 (stmt);
    5989                 :      255212 :   gimple *immusestmt;
    5990                 :      255212 :   class loop *loop = loop_containing_stmt (stmt);
    5991                 :             : 
    5992                 :      255212 :   if (TREE_CODE (binlhs) == SSA_NAME
    5993                 :      255212 :       && is_reassociable_op (SSA_NAME_DEF_STMT (binlhs), PLUS_EXPR, loop))
    5994                 :             :     return true;
    5995                 :             : 
    5996                 :      242103 :   if (TREE_CODE (binrhs) == SSA_NAME
    5997                 :      242103 :       && is_reassociable_op (SSA_NAME_DEF_STMT (binrhs), PLUS_EXPR, loop))
    5998                 :             :     return true;
    5999                 :             : 
    6000                 :      241375 :   if (TREE_CODE (lhs) == SSA_NAME
    6001                 :      241375 :       && (immusestmt = get_single_immediate_use (lhs))
    6002                 :      104068 :       && is_gimple_assign (immusestmt)
    6003                 :      345443 :       && (gimple_assign_rhs_code (immusestmt) == PLUS_EXPR
    6004                 :       67042 :           || (gimple_assign_rhs_code (immusestmt) == MINUS_EXPR
    6005                 :        2081 :               && gimple_assign_rhs1 (immusestmt) == lhs)
    6006                 :       64967 :           || gimple_assign_rhs_code (immusestmt) == MULT_EXPR))
    6007                 :             :     return true;
    6008                 :             :   return false;
    6009                 :             : }
    6010                 :             : 
    6011                 :             : /* Transform STMT from A - B into A + -B.  */
    6012                 :             : 
    6013                 :             : static void
    6014                 :       62766 : break_up_subtract (gimple *stmt, gimple_stmt_iterator *gsip)
    6015                 :             : {
    6016                 :       62766 :   tree rhs1 = gimple_assign_rhs1 (stmt);
    6017                 :       62766 :   tree rhs2 = gimple_assign_rhs2 (stmt);
    6018                 :             : 
    6019                 :       62766 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6020                 :             :     {
    6021                 :           0 :       fprintf (dump_file, "Breaking up subtract ");
    6022                 :           0 :       print_gimple_stmt (dump_file, stmt, 0);
    6023                 :             :     }
    6024                 :             : 
    6025                 :       62766 :   rhs2 = negate_value (rhs2, gsip);
    6026                 :       62766 :   gimple_assign_set_rhs_with_ops (gsip, PLUS_EXPR, rhs1, rhs2);
    6027                 :       62766 :   update_stmt (stmt);
    6028                 :       62766 : }
    6029                 :             : 
    6030                 :             : /* Determine whether STMT is a builtin call that raises an SSA name
    6031                 :             :    to an integer power and has only one use.  If so, and this is early
    6032                 :             :    reassociation and unsafe math optimizations are permitted, place
    6033                 :             :    the SSA name in *BASE and the exponent in *EXPONENT, and return TRUE.
    6034                 :             :    If any of these conditions does not hold, return FALSE.  */
    6035                 :             : 
    6036                 :             : static bool
    6037                 :         120 : acceptable_pow_call (gcall *stmt, tree *base, HOST_WIDE_INT *exponent)
    6038                 :             : {
    6039                 :         120 :   tree arg1;
    6040                 :         120 :   REAL_VALUE_TYPE c, cint;
    6041                 :             : 
    6042                 :         120 :   switch (gimple_call_combined_fn (stmt))
    6043                 :             :     {
    6044                 :          16 :     CASE_CFN_POW:
    6045                 :          16 :       if (flag_errno_math)
    6046                 :             :         return false;
    6047                 :             : 
    6048                 :          16 :       *base = gimple_call_arg (stmt, 0);
    6049                 :          16 :       arg1 = gimple_call_arg (stmt, 1);
    6050                 :             : 
    6051                 :          16 :       if (TREE_CODE (arg1) != REAL_CST)
    6052                 :             :         return false;
    6053                 :             : 
    6054                 :          16 :       c = TREE_REAL_CST (arg1);
    6055                 :             : 
    6056                 :          16 :       if (REAL_EXP (&c) > HOST_BITS_PER_WIDE_INT)
    6057                 :             :         return false;
    6058                 :             : 
    6059                 :          16 :       *exponent = real_to_integer (&c);
    6060                 :          16 :       real_from_integer (&cint, VOIDmode, *exponent, SIGNED);
    6061                 :          16 :       if (!real_identical (&c, &cint))
    6062                 :             :         return false;
    6063                 :             : 
    6064                 :             :       break;
    6065                 :             : 
    6066                 :           7 :     CASE_CFN_POWI:
    6067                 :           7 :       *base = gimple_call_arg (stmt, 0);
    6068                 :           7 :       arg1 = gimple_call_arg (stmt, 1);
    6069                 :             : 
    6070                 :           7 :       if (!tree_fits_shwi_p (arg1))
    6071                 :             :         return false;
    6072                 :             : 
    6073                 :           7 :       *exponent = tree_to_shwi (arg1);
    6074                 :           7 :       break;
    6075                 :             : 
    6076                 :             :     default:
    6077                 :             :       return false;
    6078                 :             :     }
    6079                 :             : 
    6080                 :             :   /* Expanding negative exponents is generally unproductive, so we don't
    6081                 :             :      complicate matters with those.  Exponents of zero and one should
    6082                 :             :      have been handled by expression folding.  */
    6083                 :          15 :   if (*exponent < 2 || TREE_CODE (*base) != SSA_NAME)
    6084                 :             :     return false;
    6085                 :             : 
    6086                 :             :   return true;
    6087                 :             : }
    6088                 :             : 
    6089                 :             : /* Try to derive and add operand entry for OP to *OPS.  Return false if
    6090                 :             :    unsuccessful.  */
    6091                 :             : 
    6092                 :             : static bool
    6093                 :     7977046 : try_special_add_to_ops (vec<operand_entry *> *ops,
    6094                 :             :                         enum tree_code code,
    6095                 :             :                         tree op, gimple* def_stmt)
    6096                 :             : {
    6097                 :     7977046 :   tree base = NULL_TREE;
    6098                 :     7977046 :   HOST_WIDE_INT exponent = 0;
    6099                 :             : 
    6100                 :     7977046 :   if (TREE_CODE (op) != SSA_NAME
    6101                 :     7977046 :       || ! has_single_use (op))
    6102                 :             :     return false;
    6103                 :             : 
    6104                 :     3040259 :   if (code == MULT_EXPR
    6105                 :      649218 :       && reassoc_insert_powi_p
    6106                 :      315584 :       && flag_unsafe_math_optimizations
    6107                 :       29146 :       && is_gimple_call (def_stmt)
    6108                 :     3040379 :       && acceptable_pow_call (as_a <gcall *> (def_stmt), &base, &exponent))
    6109                 :             :     {
    6110                 :          15 :       add_repeat_to_ops_vec (ops, base, exponent);
    6111                 :          15 :       gimple_set_visited (def_stmt, true);
    6112                 :          15 :       return true;
    6113                 :             :     }
    6114                 :     3040244 :   else if (code == MULT_EXPR
    6115                 :      649203 :            && is_gimple_assign (def_stmt)
    6116                 :      613149 :            && gimple_assign_rhs_code (def_stmt) == NEGATE_EXPR
    6117                 :         174 :            && !HONOR_SNANS (TREE_TYPE (op))
    6118                 :         174 :            && (!HONOR_SIGNED_ZEROS (TREE_TYPE (op))
    6119                 :           0 :                || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (op)))
    6120                 :     3040418 :            && (!FLOAT_TYPE_P (TREE_TYPE (op))
    6121                 :          50 :                || !DECIMAL_FLOAT_MODE_P (element_mode (op))))
    6122                 :             :     {
    6123                 :         167 :       tree rhs1 = gimple_assign_rhs1 (def_stmt);
    6124                 :         167 :       tree cst = build_minus_one_cst (TREE_TYPE (op));
    6125                 :         167 :       add_to_ops_vec (ops, rhs1);
    6126                 :         167 :       add_to_ops_vec (ops, cst);
    6127                 :         167 :       gimple_set_visited (def_stmt, true);
    6128                 :         167 :       return true;
    6129                 :             :     }
    6130                 :             : 
    6131                 :             :   return false;
    6132                 :             : }
    6133                 :             : 
    6134                 :             : /* Recursively linearize a binary expression that is the RHS of STMT.
    6135                 :             :    Place the operands of the expression tree in the vector named OPS.  */
    6136                 :             : 
    6137                 :             : static void
    6138                 :     4105647 : linearize_expr_tree (vec<operand_entry *> *ops, gimple *stmt,
    6139                 :             :                      bool is_associative, bool set_visited)
    6140                 :             : {
    6141                 :     4105647 :   tree binlhs = gimple_assign_rhs1 (stmt);
    6142                 :     4105647 :   tree binrhs = gimple_assign_rhs2 (stmt);
    6143                 :     4105647 :   gimple *binlhsdef = NULL, *binrhsdef = NULL;
    6144                 :     4105647 :   bool binlhsisreassoc = false;
    6145                 :     4105647 :   bool binrhsisreassoc = false;
    6146                 :     4105647 :   enum tree_code rhscode = gimple_assign_rhs_code (stmt);
    6147                 :     4105647 :   class loop *loop = loop_containing_stmt (stmt);
    6148                 :             : 
    6149                 :     4105647 :   if (set_visited)
    6150                 :     4067246 :     gimple_set_visited (stmt, true);
    6151                 :             : 
    6152                 :     4105647 :   if (TREE_CODE (binlhs) == SSA_NAME)
    6153                 :             :     {
    6154                 :     4103153 :       binlhsdef = SSA_NAME_DEF_STMT (binlhs);
    6155                 :     4103153 :       binlhsisreassoc = (is_reassociable_op (binlhsdef, rhscode, loop)
    6156                 :     4103153 :                          && !stmt_could_throw_p (cfun, binlhsdef));
    6157                 :             :     }
    6158                 :             : 
    6159                 :     4105647 :   if (TREE_CODE (binrhs) == SSA_NAME)
    6160                 :             :     {
    6161                 :     1359744 :       binrhsdef = SSA_NAME_DEF_STMT (binrhs);
    6162                 :     1359744 :       binrhsisreassoc = (is_reassociable_op (binrhsdef, rhscode, loop)
    6163                 :     1359744 :                          && !stmt_could_throw_p (cfun, binrhsdef));
    6164                 :             :     }
    6165                 :             : 
    6166                 :             :   /* If the LHS is not reassociable, but the RHS is, we need to swap
    6167                 :             :      them.  If neither is reassociable, there is nothing we can do, so
    6168                 :             :      just put them in the ops vector.  If the LHS is reassociable,
    6169                 :             :      linearize it.  If both are reassociable, then linearize the RHS
    6170                 :             :      and the LHS.  */
    6171                 :             : 
    6172                 :     4105647 :   if (!binlhsisreassoc)
    6173                 :             :     {
    6174                 :             :       /* If this is not a associative operation like division, give up.  */
    6175                 :     3945523 :       if (!is_associative)
    6176                 :             :         {
    6177                 :          15 :           add_to_ops_vec (ops, binrhs);
    6178                 :          15 :           return;
    6179                 :             :         }
    6180                 :             : 
    6181                 :     3945508 :       if (!binrhsisreassoc)
    6182                 :             :         {
    6183                 :     3871414 :           bool swap = false;
    6184                 :     3871414 :           if (try_special_add_to_ops (ops, rhscode, binrhs, binrhsdef))
    6185                 :             :             /* If we add ops for the rhs we expect to be able to recurse
    6186                 :             :                to it via the lhs during expression rewrite so swap
    6187                 :             :                operands.  */
    6188                 :             :             swap = true;
    6189                 :             :           else
    6190                 :     3871336 :             add_to_ops_vec (ops, binrhs);
    6191                 :             : 
    6192                 :     3871414 :           if (!try_special_add_to_ops (ops, rhscode, binlhs, binlhsdef))
    6193                 :     3871314 :             add_to_ops_vec (ops, binlhs);
    6194                 :             : 
    6195                 :     3871414 :           if (!swap)
    6196                 :             :             return;
    6197                 :             :         }
    6198                 :             : 
    6199                 :       74172 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6200                 :             :         {
    6201                 :           8 :           fprintf (dump_file, "swapping operands of ");
    6202                 :           8 :           print_gimple_stmt (dump_file, stmt, 0);
    6203                 :             :         }
    6204                 :             : 
    6205                 :       74172 :       swap_ssa_operands (stmt,
    6206                 :             :                          gimple_assign_rhs1_ptr (stmt),
    6207                 :             :                          gimple_assign_rhs2_ptr (stmt));
    6208                 :       74172 :       update_stmt (stmt);
    6209                 :             : 
    6210                 :       74172 :       if (dump_file && (dump_flags & TDF_DETAILS))
    6211                 :             :         {
    6212                 :           8 :           fprintf (dump_file, " is now ");
    6213                 :           8 :           print_gimple_stmt (dump_file, stmt, 0);
    6214                 :             :         }
    6215                 :       74172 :       if (!binrhsisreassoc)
    6216                 :             :         return;
    6217                 :             : 
    6218                 :             :       /* We want to make it so the lhs is always the reassociative op,
    6219                 :             :          so swap.  */
    6220                 :             :       std::swap (binlhs, binrhs);
    6221                 :             :     }
    6222                 :      160124 :   else if (binrhsisreassoc)
    6223                 :             :     {
    6224                 :        1787 :       linearize_expr (stmt);
    6225                 :        1787 :       binlhs = gimple_assign_rhs1 (stmt);
    6226                 :        1787 :       binrhs = gimple_assign_rhs2 (stmt);
    6227                 :             :     }
    6228                 :             : 
    6229                 :      234218 :   gcc_assert (TREE_CODE (binrhs) != SSA_NAME
    6230                 :             :               || !is_reassociable_op (SSA_NAME_DEF_STMT (binrhs),
    6231                 :             :                                       rhscode, loop));
    6232                 :      234218 :   linearize_expr_tree (ops, SSA_NAME_DEF_STMT (binlhs),
    6233                 :             :                        is_associative, set_visited);
    6234                 :             : 
    6235                 :      234218 :   if (!try_special_add_to_ops (ops, rhscode, binrhs, binrhsdef))
    6236                 :      234214 :     add_to_ops_vec (ops, binrhs);
    6237                 :             : }
    6238                 :             : 
    6239                 :             : /* Repropagate the negates back into subtracts, since no other pass
    6240                 :             :    currently does it.  */
    6241                 :             : 
    6242                 :             : static void
    6243                 :     1944013 : repropagate_negates (void)
    6244                 :             : {
    6245                 :     1944013 :   unsigned int i = 0;
    6246                 :     1944013 :   tree negate;
    6247                 :             : 
    6248                 :     2058797 :   FOR_EACH_VEC_ELT (plus_negates, i, negate)
    6249                 :             :     {
    6250                 :      114784 :       gimple *user = get_single_immediate_use (negate);
    6251                 :      114784 :       if (!user || !is_gimple_assign (user))
    6252                 :       19410 :         continue;
    6253                 :             : 
    6254                 :       95374 :       tree negateop = gimple_assign_rhs1 (SSA_NAME_DEF_STMT (negate));
    6255                 :       95384 :       if (TREE_CODE (negateop) == SSA_NAME
    6256                 :       95374 :           && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (negateop))
    6257                 :          10 :         continue;
    6258                 :             : 
    6259                 :             :       /* The negate operand can be either operand of a PLUS_EXPR
    6260                 :             :          (it can be the LHS if the RHS is a constant for example).
    6261                 :             : 
    6262                 :             :          Force the negate operand to the RHS of the PLUS_EXPR, then
    6263                 :             :          transform the PLUS_EXPR into a MINUS_EXPR.  */
    6264                 :       95364 :       if (gimple_assign_rhs_code (user) == PLUS_EXPR)
    6265                 :             :         {
    6266                 :             :           /* If the negated operand appears on the LHS of the
    6267                 :             :              PLUS_EXPR, exchange the operands of the PLUS_EXPR
    6268                 :             :              to force the negated operand to the RHS of the PLUS_EXPR.  */
    6269                 :       67760 :           if (gimple_assign_rhs1 (user) == negate)
    6270                 :             :             {
    6271                 :       30019 :               swap_ssa_operands (user,
    6272                 :             :                                  gimple_assign_rhs1_ptr (user),
    6273                 :             :                                  gimple_assign_rhs2_ptr (user));
    6274                 :             :             }
    6275                 :             : 
    6276                 :             :           /* Now transform the PLUS_EXPR into a MINUS_EXPR and replace
    6277                 :             :              the RHS of the PLUS_EXPR with the operand of the NEGATE_EXPR.  */
    6278                 :       67760 :           if (gimple_assign_rhs2 (user) == negate)
    6279                 :             :             {
    6280                 :       67760 :               tree rhs1 = gimple_assign_rhs1 (user);
    6281                 :       67760 :               gimple_stmt_iterator gsi = gsi_for_stmt (user);
    6282                 :       67760 :               gimple_assign_set_rhs_with_ops (&gsi, MINUS_EXPR, rhs1,
    6283                 :             :                                               negateop);
    6284                 :       67760 :               update_stmt (user);
    6285                 :             :             }
    6286                 :             :         }
    6287                 :       27604 :       else if (gimple_assign_rhs_code (user) == MINUS_EXPR)
    6288                 :             :         {
    6289                 :        1778 :           if (gimple_assign_rhs1 (user) == negate)
    6290                 :             :             {
    6291                 :             :               /* We have
    6292                 :             :                    x = -negateop
    6293                 :             :                    y = x - b
    6294                 :             :                  which we transform into
    6295                 :             :                    x = negateop + b
    6296                 :             :                    y = -x .
    6297                 :             :                  This pushes down the negate which we possibly can merge
    6298                 :             :                  into some other operation, hence insert it into the
    6299                 :             :                  plus_negates vector.  */
    6300                 :        1778 :               gimple *feed = SSA_NAME_DEF_STMT (negate);
    6301                 :        1778 :               tree b = gimple_assign_rhs2 (user);
    6302                 :        1778 :               gimple_stmt_iterator gsi = gsi_for_stmt (feed);
    6303                 :        1778 :               gimple_stmt_iterator gsi2 = gsi_for_stmt (user);
    6304                 :        1778 :               tree x = make_ssa_name (TREE_TYPE (gimple_assign_lhs (feed)));
    6305                 :        1778 :               gimple *g = gimple_build_assign (x, PLUS_EXPR, negateop, b);
    6306                 :        1778 :               gsi_insert_before (&gsi2, g, GSI_SAME_STMT);
    6307                 :        1778 :               gimple_assign_set_rhs_with_ops (&gsi2, NEGATE_EXPR, x);
    6308                 :        1778 :               user = gsi_stmt (gsi2);
    6309                 :        1778 :               update_stmt (user);
    6310                 :        1778 :               reassoc_remove_stmt (&gsi);
    6311                 :        1778 :               release_defs (feed);
    6312                 :        1778 :               plus_negates.safe_push (gimple_assign_lhs (user));
    6313                 :             :             }
    6314                 :             :           else
    6315                 :             :             {
    6316                 :             :               /* Transform "x = -negateop; y = b - x" into "y = b + negateop",
    6317                 :             :                  getting rid of one operation.  */
    6318                 :           0 :               tree rhs1 = gimple_assign_rhs1 (user);
    6319                 :           0 :               gimple_stmt_iterator gsi = gsi_for_stmt (user);
    6320                 :           0 :               gimple_assign_set_rhs_with_ops (&gsi, PLUS_EXPR, rhs1, negateop);
    6321                 :           0 :               update_stmt (gsi_stmt (gsi));
    6322                 :             :             }
    6323                 :             :         }
    6324                 :             :     }
    6325                 :     1944013 : }
    6326                 :             : 
    6327                 :             : /* Break up subtract operations in block BB.
    6328                 :             : 
    6329                 :             :    We do this top down because we don't know whether the subtract is
    6330                 :             :    part of a possible chain of reassociation except at the top.
    6331                 :             : 
    6332                 :             :    IE given
    6333                 :             :    d = f + g
    6334                 :             :    c = a + e
    6335                 :             :    b = c - d
    6336                 :             :    q = b - r
    6337                 :             :    k = t - q
    6338                 :             : 
    6339                 :             :    we want to break up k = t - q, but we won't until we've transformed q
    6340                 :             :    = b - r, which won't be broken up until we transform b = c - d.
    6341                 :             : 
    6342                 :             :    En passant, clear the GIMPLE visited flag on every statement
    6343                 :             :    and set UIDs within each basic block.  */
    6344                 :             : 
    6345                 :             : static void
    6346                 :    19380648 : break_up_subtract_bb (basic_block bb)
    6347                 :             : {
    6348                 :    19380648 :   gimple_stmt_iterator gsi;
    6349                 :    19380648 :   basic_block son;
    6350                 :    19380648 :   unsigned int uid = 1;
    6351                 :             : 
    6352                 :   174637469 :   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    6353                 :             :     {
    6354                 :   135876173 :       gimple *stmt = gsi_stmt (gsi);
    6355                 :   135876173 :       gimple_set_visited (stmt, false);
    6356                 :   135876173 :       gimple_set_uid (stmt, uid++);
    6357                 :             : 
    6358                 :   135876173 :       if (!is_gimple_assign (stmt)
    6359                 :    42416138 :           || !can_reassociate_type_p (TREE_TYPE (gimple_assign_lhs (stmt)))
    6360                 :   151407818 :           || !can_reassociate_op_p (gimple_assign_lhs (stmt)))
    6361                 :   120344858 :         continue;
    6362                 :             : 
    6363                 :             :       /* Look for simple gimple subtract operations.  */
    6364                 :    15531315 :       if (gimple_assign_rhs_code (stmt) == MINUS_EXPR)
    6365                 :             :         {
    6366                 :      255443 :           if (!can_reassociate_op_p (gimple_assign_rhs1 (stmt))
    6367                 :      255443 :               || !can_reassociate_op_p (gimple_assign_rhs2 (stmt)))
    6368                 :         231 :             continue;
    6369                 :             : 
    6370                 :             :           /* Check for a subtract used only in an addition.  If this
    6371                 :             :              is the case, transform it into add of a negate for better
    6372                 :             :              reassociation.  IE transform C = A-B into C = A + -B if C
    6373                 :             :              is only used in an addition.  */
    6374                 :      255212 :           if (should_break_up_subtract (stmt))
    6375                 :       62766 :             break_up_subtract (stmt, &gsi);
    6376                 :             :         }
    6377                 :    15275872 :       else if (gimple_assign_rhs_code (stmt) == NEGATE_EXPR
    6378                 :    15275872 :                && can_reassociate_op_p (gimple_assign_rhs1 (stmt)))
    6379                 :       42659 :         plus_negates.safe_push (gimple_assign_lhs (stmt));
    6380                 :             :     }
    6381                 :    19380648 :   for (son = first_dom_son (CDI_DOMINATORS, bb);
    6382                 :    36817283 :        son;
    6383                 :    17436635 :        son = next_dom_son (CDI_DOMINATORS, son))
    6384                 :    17436635 :     break_up_subtract_bb (son);
    6385                 :    19380648 : }
    6386                 :             : 
    6387                 :             : /* Used for repeated factor analysis.  */
    6388                 :             : struct repeat_factor
    6389                 :             : {
    6390                 :             :   /* An SSA name that occurs in a multiply chain.  */
    6391                 :             :   tree factor;
    6392                 :             : 
    6393                 :             :   /* Cached rank of the factor.  */
    6394                 :             :   unsigned rank;
    6395                 :             : 
    6396                 :             :   /* Number of occurrences of the factor in the chain.  */
    6397                 :             :   HOST_WIDE_INT count;
    6398                 :             : 
    6399                 :             :   /* An SSA name representing the product of this factor and
    6400                 :             :      all factors appearing later in the repeated factor vector.  */
    6401                 :             :   tree repr;
    6402                 :             : };
    6403                 :             : 
    6404                 :             : 
    6405                 :             : static vec<repeat_factor> repeat_factor_vec;
    6406                 :             : 
    6407                 :             : /* Used for sorting the repeat factor vector.  Sort primarily by
    6408                 :             :    ascending occurrence count, secondarily by descending rank.  */
    6409                 :             : 
    6410                 :             : static int
    6411                 :      226061 : compare_repeat_factors (const void *x1, const void *x2)
    6412                 :             : {
    6413                 :      226061 :   const repeat_factor *rf1 = (const repeat_factor *) x1;
    6414                 :      226061 :   const repeat_factor *rf2 = (const repeat_factor *) x2;
    6415                 :             : 
    6416                 :      226061 :   if (rf1->count != rf2->count)
    6417                 :        1075 :     return rf1->count - rf2->count;
    6418                 :             : 
    6419                 :      224986 :   return rf2->rank - rf1->rank;
    6420                 :             : }
    6421                 :             : 
    6422                 :             : /* Look for repeated operands in OPS in the multiply tree rooted at
    6423                 :             :    STMT.  Replace them with an optimal sequence of multiplies and powi
    6424                 :             :    builtin calls, and remove the used operands from OPS.  Return an
    6425                 :             :    SSA name representing the value of the replacement sequence.  */
    6426                 :             : 
    6427                 :             : static tree
    6428                 :      413719 : attempt_builtin_powi (gimple *stmt, vec<operand_entry *> *ops)
    6429                 :             : {
    6430                 :      413719 :   unsigned i, j, vec_len;
    6431                 :      413719 :   int ii;
    6432                 :      413719 :   operand_entry *oe;
    6433                 :      413719 :   repeat_factor *rf1, *rf2;
    6434                 :      413719 :   repeat_factor rfnew;
    6435                 :      413719 :   tree result = NULL_TREE;
    6436                 :      413719 :   tree target_ssa, iter_result;
    6437                 :      413719 :   tree type = TREE_TYPE (gimple_get_lhs (stmt));
    6438                 :      413719 :   tree powi_fndecl = mathfn_built_in (type, BUILT_IN_POWI);
    6439                 :      413719 :   gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
    6440                 :      413719 :   gimple *mul_stmt, *pow_stmt;
    6441                 :             : 
    6442                 :             :   /* Nothing to do if BUILT_IN_POWI doesn't exist for this type and
    6443                 :             :      target, unless type is integral.  */
    6444                 :      413719 :   if (!powi_fndecl && !INTEGRAL_TYPE_P (type))
    6445                 :             :     return NULL_TREE;
    6446                 :             : 
    6447                 :             :   /* Allocate the repeated factor vector.  */
    6448                 :      410063 :   repeat_factor_vec.create (10);
    6449                 :             : 
    6450                 :             :   /* Scan the OPS vector for all SSA names in the product and build
    6451                 :             :      up a vector of occurrence counts for each factor.  */
    6452                 :     1644133 :   FOR_EACH_VEC_ELT (*ops, i, oe)
    6453                 :             :     {
    6454                 :      824007 :       if (TREE_CODE (oe->op) == SSA_NAME)
    6455                 :             :         {
    6456                 :      528303 :           FOR_EACH_VEC_ELT (repeat_factor_vec, j, rf1)
    6457                 :             :             {
    6458                 :       61355 :               if (rf1->factor == oe->op)
    6459                 :             :                 {
    6460                 :        2518 :                   rf1->count += oe->count;
    6461                 :        2518 :                   break;
    6462                 :             :                 }
    6463                 :             :             }
    6464                 :             : 
    6465                 :      938932 :           if (j >= repeat_factor_vec.length ())
    6466                 :             :             {
    6467                 :      466948 :               rfnew.factor = oe->op;
    6468                 :      466948 :               rfnew.rank = oe->rank;
    6469                 :      466948 :               rfnew.count = oe->count;
    6470                 :      466948 :               rfnew.repr = NULL_TREE;
    6471                 :      466948 :               repeat_factor_vec.safe_push (rfnew);
    6472                 :             :             }
    6473                 :             :         }
    6474                 :             :     }
    6475                 :             : 
    6476                 :             :   /* Sort the repeated factor vector by (a) increasing occurrence count,
    6477                 :             :      and (b) decreasing rank.  */
    6478                 :      410063 :   repeat_factor_vec.qsort (compare_repeat_factors);
    6479                 :             : 
    6480                 :             :   /* It is generally best to combine as many base factors as possible
    6481                 :             :      into a product before applying __builtin_powi to the result.
    6482                 :             :      However, the sort order chosen for the repeated factor vector
    6483                 :             :      allows us to cache partial results for the product of the base
    6484                 :             :      factors for subsequent use.  When we already have a cached partial
    6485                 :             :      result from a previous iteration, it is best to make use of it
    6486                 :             :      before looking for another __builtin_pow opportunity.
    6487                 :             : 
    6488                 :             :      As an example, consider x * x * y * y * y * z * z * z * z.
    6489                 :             :      We want to first compose the product x * y * z, raise it to the
    6490                 :             :      second power, then multiply this by y * z, and finally multiply
    6491                 :             :      by z.  This can be done in 5 multiplies provided we cache y * z
    6492                 :             :      for use in both expressions:
    6493                 :             : 
    6494                 :             :         t1 = y * z
    6495                 :             :         t2 = t1 * x
    6496                 :             :         t3 = t2 * t2
    6497                 :             :         t4 = t1 * t3
    6498                 :             :         result = t4 * z
    6499                 :             : 
    6500                 :             :      If we instead ignored the cached y * z and first multiplied by
    6501                 :             :      the __builtin_pow opportunity z * z, we would get the inferior:
    6502                 :             : 
    6503                 :             :         t1 = y * z
    6504                 :             :         t2 = t1 * x
    6505                 :             :         t3 = t2 * t2
    6506                 :             :         t4 = z * z
    6507                 :             :         t5 = t3 * t4
    6508                 :             :         result = t5 * y  */
    6509                 :             : 
    6510                 :      820126 :   vec_len = repeat_factor_vec.length ();
    6511                 :             :   
    6512                 :             :   /* Repeatedly look for opportunities to create a builtin_powi call.  */
    6513                 :      411547 :   while (true)
    6514                 :             :     {
    6515                 :      411547 :       HOST_WIDE_INT power;
    6516                 :             : 
    6517                 :             :       /* First look for the largest cached product of factors from
    6518                 :             :          preceding iterations.  If found, create a builtin_powi for
    6519                 :             :          it if the minimum occurrence count for its factors is at
    6520                 :             :          least 2, or just use this cached product as our next 
    6521                 :             :          multiplicand if the minimum occurrence count is 1.  */
    6522                 :      880267 :       FOR_EACH_VEC_ELT (repeat_factor_vec, j, rf1)
    6523                 :             :         {
    6524                 :      468729 :           if (rf1->repr && rf1->count > 0)
    6525                 :             :             break;
    6526                 :             :         }
    6527                 :             : 
    6528                 :      411547 :       if (j < vec_len)
    6529                 :             :         {
    6530                 :           9 :           power = rf1->count;
    6531                 :             : 
    6532                 :           9 :           if (power == 1)
    6533                 :             :             {
    6534                 :           7 :               iter_result = rf1->repr;
    6535                 :             : 
    6536                 :           7 :               if (dump_file && (dump_flags & TDF_DETAILS))
    6537                 :             :                 {
    6538                 :           0 :                   unsigned elt;
    6539                 :           0 :                   repeat_factor *rf;
    6540                 :           0 :                   fputs ("Multiplying by cached product ", dump_file);
    6541                 :           0 :                   for (elt = j; elt < vec_len; elt++)
    6542                 :             :                     {
    6543                 :           0 :                       rf = &repeat_factor_vec[elt];
    6544                 :           0 :                       print_generic_expr (dump_file, rf->factor);
    6545                 :           0 :                       if (elt < vec_len - 1)
    6546                 :           0 :                         fputs (" * ", dump_file);
    6547                 :             :                     }
    6548                 :           0 :                   fputs ("\n", dump_file);
    6549                 :             :                 }
    6550                 :             :             }
    6551                 :             :           else
    6552                 :             :             {
    6553                 :           2 :               if (INTEGRAL_TYPE_P (type))
    6554                 :             :                 {
    6555                 :           0 :                   gcc_assert (power > 1);
    6556                 :           0 :                   gimple_stmt_iterator gsip = gsi;
    6557                 :           0 :                   gsi_prev (&gsip);
    6558                 :           0 :                   iter_result = powi_as_mults (&gsi, gimple_location (stmt),
    6559                 :             :                                                rf1->repr, power);
    6560                 :           0 :                   gimple_stmt_iterator gsic = gsi;
    6561                 :           0 :                   while (gsi_stmt (gsic) != gsi_stmt (gsip))
    6562                 :             :                     {
    6563                 :           0 :                       gimple_set_uid (gsi_stmt (gsic), gimple_uid (stmt));
    6564                 :           0 :                       gimple_set_visited (gsi_stmt (gsic), true);
    6565                 :           0 :                       gsi_prev (&gsic);
    6566                 :             :                     }
    6567                 :             :                 }
    6568                 :             :               else
    6569                 :             :                 {
    6570                 :           2 :                   iter_result = make_temp_ssa_name (type, NULL, "reassocpow");
    6571                 :           2 :                   pow_stmt
    6572                 :           2 :                     = gimple_build_call (powi_fndecl, 2, rf1->repr,
    6573                 :             :                                          build_int_cst (integer_type_node,
    6574                 :             :                                                         power));
    6575                 :           2 :                   gimple_call_set_lhs (pow_stmt, iter_result);
    6576                 :           2 :                   gimple_set_location (pow_stmt, gimple_location (stmt));
    6577                 :           2 :                   gimple_set_uid (pow_stmt, gimple_uid (stmt));
    6578                 :           2 :                   gsi_insert_before (&gsi, pow_stmt, GSI_SAME_STMT);
    6579                 :             :                 }
    6580                 :             : 
    6581                 :           2 :               if (dump_file && (dump_flags & TDF_DETAILS))
    6582                 :             :                 {
    6583                 :           0 :                   unsigned elt;
    6584                 :           0 :                   repeat_factor *rf;
    6585                 :           0 :                   fputs ("Building __builtin_pow call for cached product (",
    6586                 :             :                          dump_file);
    6587                 :           0 :                   for (elt = j; elt < vec_len; elt++)
    6588                 :             :                     {
    6589                 :           0 :                       rf = &repeat_factor_vec[elt];
    6590                 :           0 :                       print_generic_expr (dump_file, rf->factor);
    6591                 :           0 :                       if (elt < vec_len - 1)
    6592                 :           0 :                         fputs (" * ", dump_file);
    6593                 :             :                     }
    6594                 :           0 :                   fprintf (dump_file, ")^" HOST_WIDE_INT_PRINT_DEC"\n",
    6595                 :             :                            power);
    6596                 :             :                 }
    6597                 :             :             }
    6598                 :             :         }
    6599                 :             :       else
    6600                 :             :         {
    6601                 :             :           /* Otherwise, find the first factor in the repeated factor
    6602                 :             :              vector whose occurrence count is at least 2.  If no such
    6603                 :             :              factor exists, there are no builtin_powi opportunities
    6604                 :             :              remaining.  */
    6605                 :      878734 :           FOR_EACH_VEC_ELT (repeat_factor_vec, j, rf1)
    6606                 :             :             {
    6607                 :      468671 :               if (rf1->count >= 2)
    6608                 :             :                 break;
    6609                 :             :             }
    6610                 :             : 
    6611                 :      411538 :           if (j >= vec_len)
    6612                 :             :             break;
    6613                 :             : 
    6614                 :        1475 :           power = rf1->count;
    6615                 :             : 
    6616                 :        1475 :           if (dump_file && (dump_flags & TDF_DETAILS))
    6617                 :             :             {
    6618                 :           0 :               unsigned elt;
    6619                 :           0 :               repeat_factor *rf;
    6620                 :           0 :               fputs ("Building __builtin_pow call for (", dump_file);
    6621                 :           0 :               for (elt = j; elt < vec_len; elt++)
    6622                 :             :                 {
    6623                 :           0 :                   rf = &repeat_factor_vec[elt];
    6624                 :           0 :                   print_generic_expr (dump_file, rf->factor);
    6625                 :           0 :                   if (elt < vec_len - 1)
    6626                 :           0 :                     fputs (" * ", dump_file);
    6627                 :             :                 }
    6628                 :           0 :               fprintf (dump_file, ")^" HOST_WIDE_INT_PRINT_DEC"\n", power);
    6629                 :             :             }
    6630                 :             : 
    6631                 :        1475 :           reassociate_stats.pows_created++;
    6632                 :             : 
    6633                 :             :           /* Visit each element of the vector in reverse order (so that
    6634                 :             :              high-occurrence elements are visited first, and within the
    6635                 :             :              same occurrence count, lower-ranked elements are visited
    6636                 :             :              first).  Form a linear product of all elements in this order
    6637                 :             :              whose occurrencce count is at least that of element J.
    6638                 :             :              Record the SSA name representing the product of each element
    6639                 :             :              with all subsequent elements in the vector.  */
    6640                 :        1475 :           if (j == vec_len - 1)
    6641                 :        1449 :             rf1->repr = rf1->factor;
    6642                 :             :           else
    6643                 :             :             {
    6644                 :          60 :               for (ii = vec_len - 2; ii >= (int)j; ii--)
    6645                 :             :                 {
    6646                 :          34 :                   tree op1, op2;
    6647                 :             : 
    6648                 :          34 :                   rf1 = &repeat_factor_vec[ii];
    6649                 :          34 :                   rf2 = &repeat_factor_vec[ii + 1];
    6650                 :             : 
    6651                 :             :                   /* Init the last factor's representative to be itself.  */
    6652                 :          34 :                   if (!rf2->repr)
    6653                 :          26 :                     rf2->repr = rf2->factor;
    6654                 :             : 
    6655                 :          34 :                   op1 = rf1->factor;
    6656                 :          34 :                   op2 = rf2->repr;
    6657                 :             : 
    6658                 :          34 :                   target_ssa = make_temp_ssa_name (type, NULL, "reassocpow");
    6659                 :          34 :                   mul_stmt = gimple_build_assign (target_ssa, MULT_EXPR,
    6660                 :             :                                                   op1, op2);
    6661                 :          34 :                   gimple_set_location (mul_stmt, gimple_location (stmt));
    6662                 :          34 :                   gimple_set_uid (mul_stmt, gimple_uid (stmt));
    6663                 :          34 :                   gsi_insert_before (&gsi, mul_stmt, GSI_SAME_STMT);
    6664                 :          34 :                   rf1->repr = target_ssa;
    6665                 :             : 
    6666                 :             :                   /* Don't reprocess the multiply we just introduced.  */
    6667                 :          34 :                   gimple_set_visited (mul_stmt, true);
    6668                 :             :                 }
    6669                 :             :             }
    6670                 :             : 
    6671                 :             :           /* Form a call to __builtin_powi for the maximum product
    6672                 :             :              just formed, raised to the power obtained earlier.  */
    6673                 :        1475 :           rf1 = &repeat_factor_vec[j];
    6674                 :        1475 :           if (INTEGRAL_TYPE_P (type))
    6675                 :             :             {
    6676                 :         912 :               gcc_assert (power > 1);
    6677                 :         912 :               gimple_stmt_iterator gsip = gsi;
    6678                 :         912 :               gsi_prev (&gsip);
    6679                 :         912 :               iter_result = powi_as_mults (&gsi, gimple_location (stmt),
    6680                 :             :                                            rf1->repr, power);
    6681                 :         912 :               gimple_stmt_iterator gsic = gsi;
    6682                 :         912 :               while (gsi_stmt (gsic) != gsi_stmt (gsip))
    6683                 :             :                 {
    6684                 :        1860 :                   gimple_set_uid (gsi_stmt (gsic), gimple_uid (stmt));
    6685                 :        1860 :                   gimple_set_visited (gsi_stmt (gsic), true);
    6686                 :        2772 :                   gsi_prev (&gsic);
    6687                 :             :                 }
    6688                 :             :             }
    6689                 :             :           else
    6690                 :             :             {
    6691                 :         563 :               iter_result = make_temp_ssa_name (type, NULL, "reassocpow");
    6692                 :         563 :               pow_stmt = gimple_build_call (powi_fndecl, 2, rf1->repr,
    6693                 :             :                                             build_int_cst (integer_type_node,
    6694                 :             :                                                            power));
    6695                 :         563 :               gimple_call_set_lhs (pow_stmt, iter_result);
    6696                 :         563 :               gimple_set_location (pow_stmt, gimple_location (stmt));
    6697                 :         563 :               gimple_set_uid (pow_stmt, gimple_uid (stmt));
    6698                 :         563 :               gsi_insert_before (&gsi, pow_stmt, GSI_SAME_STMT);
    6699                 :             :             }
    6700                 :             :         }
    6701                 :             : 
    6702                 :             :       /* If we previously formed at least one other builtin_powi call,
    6703                 :             :          form the product of this one and those others.  */
    6704                 :        1484 :       if (result)
    6705                 :             :         {
    6706                 :           9 :           tree new_result = make_temp_ssa_name (type, NULL, "reassocpow");
    6707                 :           9 :           mul_stmt = gimple_build_assign (new_result, MULT_EXPR,
    6708                 :             :                                           result, iter_result);
    6709                 :           9 :           gimple_set_location (mul_stmt, gimple_location (stmt));
    6710                 :           9 :           gimple_set_uid (mul_stmt, gimple_uid (stmt));
    6711                 :           9 :           gsi_insert_before (&gsi, mul_stmt, GSI_SAME_STMT);
    6712                 :           9 :           gimple_set_visited (mul_stmt, true);
    6713                 :           9 :           result = new_result;
    6714                 :             :         }
    6715                 :             :       else
    6716                 :             :         result = iter_result;
    6717                 :             : 
    6718                 :             :       /* Decrement the occurrence count of each element in the product
    6719                 :             :          by the count found above, and remove this many copies of each
    6720                 :             :          factor from OPS.  */
    6721                 :        3007 :       for (i = j; i < vec_len; i++)
    6722                 :             :         {
    6723                 :        1523 :           unsigned k = power;
    6724                 :        1523 :           unsigned n;
    6725                 :             : 
    6726                 :        1523 :           rf1 = &repeat_factor_vec[i];
    6727                 :        1523 :           rf1->count -= power;
    6728                 :             :           
    6729                 :        7477 :           FOR_EACH_VEC_ELT_REVERSE (*ops, n, oe)
    6730                 :             :             {
    6731                 :        4431 :               if (oe->op == rf1->factor)
    6732                 :             :                 {
    6733                 :        4033 :                   if (oe->count <= k)
    6734                 :             :                     {
    6735                 :        4027 :                       ops->ordered_remove (n);
    6736                 :        4027 :                       k -= oe->count;
    6737                 :             : 
    6738                 :        4027 :                       if (k == 0)
    6739                 :             :                         break;
    6740                 :             :                     }
    6741                 :             :                   else
    6742                 :             :                     {
    6743                 :           6 :                       oe->count -= k;
    6744                 :           6 :                       break;
    6745                 :             :                     }
    6746                 :             :                 }
    6747                 :             :             }
    6748                 :             :         }
    6749                 :             :     }
    6750                 :             : 
    6751                 :             :   /* At this point all elements in the repeated factor vector have a
    6752                 :             :      remaining occurrence count of 0 or 1, and those with a count of 1
    6753                 :             :      don't have cached representatives.  Re-sort the ops vector and
    6754                 :             :      clean up.  */
    6755                 :      410063 :   ops->qsort (sort_by_operand_rank);
    6756                 :      410063 :   repeat_factor_vec.release ();
    6757                 :             : 
    6758                 :             :   /* Return the final product computed herein.  Note that there may
    6759                 :             :      still be some elements with single occurrence count left in OPS;
    6760                 :             :      those will be handled by the normal reassociation logic.  */
    6761                 :      410063 :   return result;
    6762                 :             : }
    6763                 :             : 
    6764                 :             : /* Attempt to optimize
    6765                 :             :    CST1 * copysign (CST2, y) -> copysign (CST1 * CST2, y) if CST1 > 0, or
    6766                 :             :    CST1 * copysign (CST2, y) -> -copysign (CST1 * CST2, y) if CST1 < 0.  */
    6767                 :             : 
    6768                 :             : static void
    6769                 :      884321 : attempt_builtin_copysign (vec<operand_entry *> *ops)
    6770                 :             : {
    6771                 :      884321 :   operand_entry *oe;
    6772                 :      884321 :   unsigned int i;
    6773                 :      884321 :   unsigned int length = ops->length ();
    6774                 :      884321 :   tree cst = ops->last ()->op;
    6775                 :             : 
    6776                 :      884321 :   if (length == 1 || TREE_CODE (cst) != REAL_CST)
    6777                 :             :     return;
    6778                 :             : 
    6779                 :        4566 :   FOR_EACH_VEC_ELT (*ops, i, oe)
    6780                 :             :     {
    6781                 :        3255 :       if (TREE_CODE (oe->op) == SSA_NAME
    6782                 :        3255 :           && has_single_use (oe->op))
    6783                 :             :         {
    6784                 :         954 :           gimple *def_stmt = SSA_NAME_DEF_STMT (oe->op);
    6785                 :        3292 :           if (gcall *old_call = dyn_cast <gcall *> (def_stmt))
    6786                 :             :             {
    6787                 :          53 :               tree arg0, arg1;
    6788                 :          53 :               switch (gimple_call_combined_fn (old_call))
    6789                 :             :                 {
    6790                 :          20 :                 CASE_CFN_COPYSIGN:
    6791                 :          20 :                 CASE_CFN_COPYSIGN_FN:
    6792                 :          20 :                   arg0 = gimple_call_arg (old_call, 0);
    6793                 :          20 :                   arg1 = gimple_call_arg (old_call, 1);
    6794                 :             :                   /* The first argument of copysign must be a constant,
    6795                 :             :                      otherwise there's nothing to do.  */
    6796                 :          20 :                   if (TREE_CODE (arg0) == REAL_CST)
    6797                 :             :                     {
    6798                 :          20 :                       tree type = TREE_TYPE (arg0);
    6799                 :          20 :                       tree mul = const_binop (MULT_EXPR, type, cst, arg0);
    6800                 :             :                       /* If we couldn't fold to a single constant, skip it.
    6801                 :             :                          That happens e.g. for inexact multiplication when
    6802                 :             :                          -frounding-math.  */
    6803                 :          20 :                       if (mul == NULL_TREE)
    6804                 :             :                         break;
    6805                 :             :                       /* Instead of adjusting OLD_CALL, let's build a new
    6806                 :             :                          call to not leak the LHS and prevent keeping bogus
    6807                 :             :                          debug statements.  DCE will clean up the old call.  */
    6808                 :          16 :                       gcall *new_call;
    6809                 :          16 :                       if (gimple_call_internal_p (old_call))
    6810                 :           0 :                         new_call = gimple_build_call_internal
    6811                 :           0 :                           (IFN_COPYSIGN, 2, mul, arg1);
    6812                 :             :                       else
    6813                 :          16 :                         new_call = gimple_build_call
    6814                 :          16 :                           (gimple_call_fndecl (old_call), 2, mul, arg1);
    6815                 :          16 :                       tree lhs = make_ssa_name (type);
    6816                 :          16 :                       gimple_call_set_lhs (new_call, lhs);
    6817                 :          16 :                       gimple_set_location (new_call,
    6818                 :             :                                            gimple_location (old_call));
    6819                 :          16 :                       insert_stmt_after (new_call, old_call);
    6820                 :             :                       /* We've used the constant, get rid of it.  */
    6821                 :          16 :                       ops->pop ();
    6822                 :          16 :                       bool cst1_neg = real_isneg (TREE_REAL_CST_PTR (cst));
    6823                 :             :                       /* Handle the CST1 < 0 case by negating the result.  */
    6824                 :          16 :                       if (cst1_neg)
    6825                 :             :                         {
    6826                 :           7 :                           tree negrhs = make_ssa_name (TREE_TYPE (lhs));
    6827                 :           7 :                           gimple *negate_stmt
    6828                 :           7 :                             = gimple_build_assign (negrhs, NEGATE_EXPR, lhs);
    6829                 :           7 :                           insert_stmt_after (negate_stmt, new_call);
    6830                 :           7 :                           oe->op = negrhs;
    6831                 :             :                         }
    6832                 :             :                       else
    6833                 :           9 :                         oe->op = lhs;
    6834                 :          16 :                       if (dump_file && (dump_flags & TDF_DETAILS))
    6835                 :             :                         {
    6836                 :          14 :                           fprintf (dump_file, "Optimizing copysign: ");
    6837                 :          14 :                           print_generic_expr (dump_file, cst);
    6838                 :          14 :                           fprintf (dump_file, " * COPYSIGN (");
    6839                 :          14 :                           print_generic_expr (dump_file, arg0);
    6840                 :          14 :                           fprintf (dump_file, ", ");
    6841                 :          14 :                           print_generic_expr (dump_file, arg1);
    6842                 :          23 :                           fprintf (dump_file, ") into %sCOPYSIGN (",
    6843                 :             :                                    cst1_neg ? "-" : "");
    6844                 :          14 :                           print_generic_expr (dump_file, mul);
    6845                 :          14 :                           fprintf (dump_file, ", ");
    6846                 :          14 :                           print_generic_expr (dump_file, arg1);
    6847                 :          14 :                           fprintf (dump_file, "\n");
    6848                 :             :                         }
    6849                 :          16 :                       return;
    6850                 :             :                     }
    6851                 :             :                   break;
    6852                 :             :                 default:
    6853                 :             :                   break;
    6854                 :             :                 }
    6855                 :             :             }
    6856                 :             :         }
    6857                 :             :     }
    6858                 :             : }
    6859                 :             : 
    6860                 :             : /* Transform STMT at *GSI into a copy by replacing its rhs with NEW_RHS.  */
    6861                 :             : 
    6862                 :             : static void
    6863                 :       13596 : transform_stmt_to_copy (gimple_stmt_iterator *gsi, gimple *stmt, tree new_rhs)
    6864                 :             : {
    6865                 :       13596 :   tree rhs1;
    6866                 :             : 
    6867                 :       13596 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6868                 :             :     {
    6869                 :          27 :       fprintf (dump_file, "Transforming ");
    6870                 :          27 :       print_gimple_stmt (dump_file, stmt, 0);
    6871                 :             :     }
    6872                 :             : 
    6873                 :       13596 :   rhs1 = gimple_assign_rhs1 (stmt);
    6874                 :       13596 :   gimple_assign_set_rhs_from_tree (gsi, new_rhs);
    6875                 :       13596 :   update_stmt (stmt);
    6876                 :       13596 :   remove_visited_stmt_chain (rhs1);
    6877                 :             : 
    6878                 :       13596 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6879                 :             :     {
    6880                 :          27 :       fprintf (dump_file, " into ");
    6881                 :          27 :       print_gimple_stmt (dump_file, stmt, 0);
    6882                 :             :     }
    6883                 :       13596 : }
    6884                 :             : 
    6885                 :             : /* Transform STMT at *GSI into a multiply of RHS1 and RHS2.  */
    6886                 :             : 
    6887                 :             : static void
    6888                 :         171 : transform_stmt_to_multiply (gimple_stmt_iterator *gsi, gimple *stmt,
    6889                 :             :                             tree rhs1, tree rhs2)
    6890                 :             : {
    6891                 :         171 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6892                 :             :     {
    6893                 :           0 :       fprintf (dump_file, "Transforming ");
    6894                 :           0 :       print_gimple_stmt (dump_file, stmt, 0);
    6895                 :             :     }
    6896                 :             : 
    6897                 :         171 :   gimple_assign_set_rhs_with_ops (gsi, MULT_EXPR, rhs1, rhs2);
    6898                 :         171 :   update_stmt (gsi_stmt (*gsi));
    6899                 :         171 :   remove_visited_stmt_chain (rhs1);
    6900                 :             : 
    6901                 :         171 :   if (dump_file && (dump_flags & TDF_DETAILS))
    6902                 :             :     {
    6903                 :           0 :       fprintf (dump_file, " into ");
    6904                 :           0 :       print_gimple_stmt (dump_file, stmt, 0);
    6905                 :             :     }
    6906                 :         171 : }
    6907                 :             : 
    6908                 :             : /* Rearrange ops may have more FMA when the chain may has more than 2 FMAs.
    6909                 :             :    Put no-mult ops and mult ops alternately at the end of the queue, which is
    6910                 :             :    conducive to generating more FMA and reducing the loss of FMA when breaking
    6911                 :             :    the chain.
    6912                 :             :    E.g.
    6913                 :             :    a * b + c * d + e generates:
    6914                 :             : 
    6915                 :             :    _4  = c_9(D) * d_10(D);
    6916                 :             :    _12 = .FMA (a_7(D), b_8(D), _4);
    6917                 :             :    _11 = e_6(D) + _12;
    6918                 :             : 
    6919                 :             :    Rearrange ops to -> e + a * b + c * d generates:
    6920                 :             : 
    6921                 :             :    _4  = .FMA (c_7(D), d_8(D), _3);
    6922                 :             :    _11 = .FMA (a_5(D), b_6(D), _4);
    6923                 :             : 
    6924                 :             :    Return the number of MULT_EXPRs in the chain.  */
    6925                 :             : static int
    6926                 :       18647 : rank_ops_for_fma (vec<operand_entry *> *ops)
    6927                 :             : {
    6928                 :       18647 :   operand_entry *oe;
    6929                 :       18647 :   unsigned int i;
    6930                 :       18647 :   unsigned int ops_length = ops->length ();
    6931                 :       18647 :   auto_vec<operand_entry *> ops_mult;
    6932                 :       18647 :   auto_vec<operand_entry *> ops_others;
    6933                 :             : 
    6934                 :       60578 :   FOR_EACH_VEC_ELT (*ops, i, oe)
    6935                 :             :     {
    6936                 :       41931 :       if (TREE_CODE (oe->op) == SSA_NAME)
    6937                 :             :         {
    6938                 :       41887 :           gimple *def_stmt = SSA_NAME_DEF_STMT (oe->op);
    6939                 :       41887 :           if (is_gimple_assign (def_stmt))
    6940                 :             :             {
    6941                 :       28448 :               if (gimple_assign_rhs_code (def_stmt) == MULT_EXPR)
    6942                 :       13641 :                 ops_mult.safe_push (oe);
    6943                 :             :               /* A negate on the multiplication leads to FNMA.  */
    6944                 :       14807 :               else if (gimple_assign_rhs_code (def_stmt) == NEGATE_EXPR
    6945                 :       14807 :                        && TREE_CODE (gimple_assign_rhs1 (def_stmt)) == SSA_NAME)
    6946                 :             :                 {
    6947                 :        2322 :                   gimple *neg_def_stmt
    6948                 :        2322 :                     = SSA_NAME_DEF_STMT (gimple_assign_rhs1 (def_stmt));
    6949                 :        2322 :                   if (is_gimple_assign (neg_def_stmt)
    6950                 :        2319 :                       && gimple_bb (neg_def_stmt) == gimple_bb (def_stmt)
    6951                 :        4630 :                       && gimple_assign_rhs_code (neg_def_stmt) == MULT_EXPR)
    6952                 :        2246 :                     ops_mult.safe_push (oe);
    6953                 :             :                   else
    6954                 :          76 :                     ops_others.safe_push (oe);
    6955                 :             :                 }
    6956                 :             :               else
    6957                 :       12485 :                 ops_others.safe_push (oe);
    6958                 :             :             }
    6959                 :             :           else
    6960                 :       13439 :             ops_others.safe_push (oe);
    6961                 :             :         }
    6962                 :             :       else
    6963                 :          44 :         ops_others.safe_push (oe);
    6964                 :             :     }
    6965                 :             :   /* 1. When ops_mult.length == 2, like the following case,
    6966                 :             : 
    6967                 :             :      a * b + c * d + e.
    6968                 :             : 
    6969                 :             :      we need to rearrange the ops.
    6970                 :             : 
    6971                 :             :      Putting ops that not def from mult in front can generate more FMAs.
    6972                 :             : 
    6973                 :             :      2. If all ops are defined with mult, we don't need to rearrange them.  */
    6974                 :       18647 :   unsigned mult_num = ops_mult.length ();
    6975                 :       18647 :   if (mult_num >= 2 && mult_num != ops_length)
    6976                 :             :     {
    6977                 :             :       /* Put no-mult ops and mult ops alternately at the end of the
    6978                 :             :          queue, which is conducive to generating more FMA and reducing the
    6979                 :             :          loss of FMA when breaking the chain.  */
    6980                 :        4352 :       ops->truncate (0);
    6981                 :        4352 :       ops->splice (ops_mult);
    6982                 :        4352 :       int j, opindex = ops->length ();
    6983                 :        4352 :       int others_length = ops_others.length ();
    6984                 :        8713 :       for (j = 0; j < others_length; j++)
    6985                 :             :         {
    6986                 :        4361 :           oe = ops_others.pop ();
    6987                 :        4361 :           ops->quick_insert (opindex, oe);
    6988                 :        4361 :           if (opindex > 0)
    6989                 :        4359 :             opindex--;
    6990                 :             :         }
    6991                 :             :     }
    6992                 :       18647 :   return mult_num;
    6993                 :       18647 : }
    6994                 :             : /* Reassociate expressions in basic block BB and its post-dominator as
    6995                 :             :    children.
    6996                 :             : 
    6997                 :             :    Bubble up return status from maybe_optimize_range_tests.  */
    6998                 :             : 
    6999                 :             : static bool
    7000                 :    19380622 : reassociate_bb (basic_block bb)
    7001                 :             : {
    7002                 :    19380622 :   gimple_stmt_iterator gsi;
    7003                 :    19380622 :   basic_block son;
    7004                 :    19380622 :   gimple *stmt = last_nondebug_stmt (bb);
    7005                 :    19380622 :   bool cfg_cleanup_needed = false;
    7006                 :             : 
    7007                 :    19380622 :   if (stmt && !gimple_visited_p (stmt))
    7008                 :    16709987 :     cfg_cleanup_needed |= maybe_optimize_range_tests (stmt);
    7009                 :             : 
    7010                 :    19380622 :   bool do_prev = false;
    7011                 :    38761244 :   for (gsi = gsi_last_bb (bb);
    7012                 :   155472551 :        !gsi_end_p (gsi); do_prev ? gsi_prev (&gsi) : (void) 0)
    7013                 :             :     {
    7014                 :   136091929 :       do_prev = true;
    7015                 :   136091929 :       stmt = gsi_stmt (gsi);
    7016                 :             : 
    7017                 :   136091929 :       if (is_gimple_assign (stmt)
    7018                 :   136091929 :           && !stmt_could_throw_p (cfun, stmt))
    7019                 :             :         {
    7020                 :    40538037 :           tree lhs, rhs1, rhs2;
    7021                 :    40538037 :           enum tree_code rhs_code = gimple_assign_rhs_code (stmt);
    7022                 :             : 
    7023                 :             :           /* If this was part of an already processed statement,
    7024                 :             :              we don't need to touch it again. */
    7025                 :    40538037 :           if (gimple_visited_p (stmt))
    7026                 :             :             {
    7027                 :             :               /* This statement might have become dead because of previous
    7028                 :             :                  reassociations.  */
    7029                 :      364258 :               if (has_zero_uses (gimple_get_lhs (stmt)))
    7030                 :             :                 {
    7031                 :      107625 :                   reassoc_remove_stmt (&gsi);
    7032                 :      107625 :                   release_defs (stmt);
    7033                 :             :                   /* We might end up removing the last stmt above which
    7034                 :             :                      places the iterator to the end of the sequence.
    7035                 :             :                      Reset it to the last stmt in this case and make sure
    7036                 :             :                      we don't do gsi_prev in that case.  */
    7037                 :      107625 :                   if (gsi_end_p (gsi))
    7038                 :             :                     {
    7039                 :         345 :                       gsi = gsi_last_bb (bb);
    7040                 :         345 :                       do_prev = false;
    7041                 :             :                     }
    7042                 :             :                 }
    7043                 :      364258 :               continue;
    7044                 :             :             }
    7045                 :             : 
    7046                 :             :           /* If this is not a gimple binary expression, there is
    7047                 :             :              nothing for us to do with it.  */
    7048                 :    40173779 :           if (get_gimple_rhs_class (rhs_code) != GIMPLE_BINARY_RHS)
    7049                 :    30129254 :             continue;
    7050                 :             : 
    7051                 :    10044525 :           lhs = gimple_assign_lhs (stmt);
    7052                 :    10044525 :           rhs1 = gimple_assign_rhs1 (stmt);
    7053                 :    10044525 :           rhs2 = gimple_assign_rhs2 (stmt);
    7054                 :             : 
    7055                 :             :           /* For non-bit or min/max operations we can't associate
    7056                 :             :              all types.  Verify that here.  */
    7057                 :    14774290 :           if ((rhs_code != BIT_IOR_EXPR
    7058                 :    10044525 :                && rhs_code != BIT_AND_EXPR
    7059                 :     9130391 :                && rhs_code != BIT_XOR_EXPR
    7060                 :     9130391 :                && rhs_code != MIN_EXPR
    7061                 :     9032761 :                && rhs_code != MAX_EXPR
    7062                 :     8958850 :                && !can_reassociate_type_p (TREE_TYPE (lhs)))
    7063                 :     5318871 :               || !can_reassociate_op_p (rhs1)
    7064                 :    15360657 :               || !can_reassociate_op_p (rhs2))
    7065                 :     4729765 :             continue;
    7066                 :             : 
    7067                 :     5314760 :           if (associative_tree_code (rhs_code))
    7068                 :             :             {
    7069                 :     3869198 :               auto_vec<operand_entry *> ops;
    7070                 :     3869198 :               tree powi_result = NULL_TREE;
    7071                 :     3869198 :               bool is_vector = VECTOR_TYPE_P (TREE_TYPE (lhs));
    7072                 :             : 
    7073                 :             :               /* There may be no immediate uses left by the time we
    7074                 :             :                  get here because we may have eliminated them all.  */
    7075                 :     3869198 :               if (TREE_CODE (lhs) == SSA_NAME && has_zero_uses (lhs))
    7076                 :       35480 :                 continue;
    7077                 :             : 
    7078                 :     3833718 :               gimple_set_visited (stmt, true);
    7079                 :     3833718 :               linearize_expr_tree (&ops, stmt, true, true);
    7080                 :     3833718 :               ops.qsort (sort_by_operand_rank);
    7081                 :     3833718 :               int orig_len = ops.length ();
    7082                 :     3833718 :               optimize_ops_list (rhs_code, &ops);
    7083                 :     7667436 :               if (undistribute_ops_list (rhs_code, &ops,
    7084                 :             :                                          loop_containing_stmt (stmt)))
    7085                 :             :                 {
    7086                 :         136 :                   ops.qsort (sort_by_operand_rank);
    7087                 :         136 :                   optimize_ops_list (rhs_code, &ops);
    7088                 :             :                 }
    7089                 :     7667436 :               if (undistribute_bitref_for_vector (rhs_code, &ops,
    7090                 :             :                                                   loop_containing_stmt (stmt)))
    7091                 :             :                 {
    7092                 :          36 :                   ops.qsort (sort_by_operand_rank);
    7093                 :          36 :                   optimize_ops_list (rhs_code, &ops);
    7094                 :             :                 }
    7095                 :     3833718 :               if (rhs_code == PLUS_EXPR
    7096                 :     3833718 :                   && transform_add_to_multiply (&ops))
    7097                 :          91 :                 ops.qsort (sort_by_operand_rank);
    7098                 :             : 
    7099                 :     3833718 :               if (rhs_code == BIT_IOR_EXPR || rhs_code == BIT_AND_EXPR)
    7100                 :             :                 {
    7101                 :      907024 :                   if (is_vector)
    7102                 :       10674 :                     optimize_vec_cond_expr (rhs_code, &ops);
    7103                 :             :                   else
    7104                 :      896350 :                     optimize_range_tests (rhs_code, &ops, NULL);
    7105                 :             :                 }
    7106                 :             : 
    7107                 :     3833718 :               if (rhs_code == MULT_EXPR && !is_vector)
    7108                 :             :                 {
    7109                 :      884321 :                   attempt_builtin_copysign (&ops);
    7110                 :             : 
    7111                 :      884321 :                   if (reassoc_insert_powi_p
    7112                 :      884321 :                       && (flag_unsafe_math_optimizations
    7113                 :      371747 :                           || (INTEGRAL_TYPE_P (TREE_TYPE (lhs)))))
    7114                 :      413719 :                     powi_result = attempt_builtin_powi (stmt, &ops);
    7115                 :             :                 }
    7116                 :             : 
    7117                 :     3833718 :               operand_entry *last;
    7118                 :     3833718 :               bool negate_result = false;
    7119                 :     3833718 :               if (ops.length () > 1
    7120                 :     3833718 :                   && rhs_code == MULT_EXPR)
    7121                 :             :                 {
    7122                 :      902698 :                   last = ops.last ();
    7123                 :      902698 :                   if ((integer_minus_onep (last->op)
    7124                 :      902578 :                        || real_minus_onep (last->op))
    7125                 :         147 :                       && !HONOR_SNANS (TREE_TYPE (lhs))
    7126                 :      902845 :                       && (!HONOR_SIGNED_ZEROS (TREE_TYPE (lhs))
    7127                 :           0 :                           || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (lhs))))
    7128                 :             :                     {
    7129                 :         147 :                       ops.pop ();
    7130                 :         147 :                       negate_result = true;
    7131                 :             :                     }
    7132                 :             :                 }
    7133                 :             : 
    7134                 :     3833718 :               tree new_lhs = lhs;
    7135                 :             :               /* If the operand vector is now empty, all operands were 
    7136                 :             :                  consumed by the __builtin_powi optimization.  */
    7137                 :     3833718 :               if (ops.length () == 0)
    7138                 :        1194 :                 transform_stmt_to_copy (&gsi, stmt, powi_result);
    7139                 :     3832524 :               else if (ops.length () == 1)
    7140                 :             :                 {
    7141                 :       12573 :                   tree last_op = ops.last ()->op;
    7142                 :             : 
    7143                 :             :                   /* If the stmt that defines operand has to be inserted, insert it
    7144                 :             :                      before the use.  */
    7145                 :       12573 :                   if (ops.last ()->stmt_to_insert)
    7146                 :           0 :                     insert_stmt_before_use (stmt, ops.last ()->stmt_to_insert);
    7147                 :       12573 :                   if (powi_result)
    7148                 :         171 :                     transform_stmt_to_multiply (&gsi, stmt, last_op,
    7149                 :             :                                                 powi_result);
    7150                 :             :                   else
    7151                 :       12402 :                     transform_stmt_to_copy (&gsi, stmt, last_op);
    7152                 :             :                 }
    7153                 :             :               else
    7154                 :             :                 {
    7155                 :     3819951 :                   machine_mode mode = TYPE_MODE (TREE_TYPE (lhs));
    7156                 :     3819951 :                   int ops_num = ops.length ();
    7157                 :     3819951 :                   int width = 0;
    7158                 :     3819951 :                   int mult_num = 0;
    7159                 :             : 
    7160                 :             :                   /* For binary bit operations, if there are at least 3
    7161                 :             :                      operands and the last operand in OPS is a constant,
    7162                 :             :                      move it to the front.  This helps ensure that we generate
    7163                 :             :                      (X & Y) & C rather than (X & C) & Y.  The former will
    7164                 :             :                      often match a canonical bit test when we get to RTL.  */
    7165                 :     3819951 :                   if (ops.length () > 2
    7166                 :      149162 :                       && (rhs_code == BIT_AND_EXPR
    7167                 :             :                           || rhs_code == BIT_IOR_EXPR
    7168                 :      131403 :                           || rhs_code == BIT_XOR_EXPR)
    7169                 :     3840622 :                       && TREE_CODE (ops.last ()->op) == INTEGER_CST)
    7170                 :        1472 :                     std::swap (*ops[0], *ops[ops_num - 1]);
    7171                 :             : 
    7172                 :     3819951 :                   optimization_type opt_type = bb_optimization_type (bb);
    7173                 :             : 
    7174                 :             :                   /* If the target support FMA, rank_ops_for_fma will detect if
    7175                 :             :                      the chain has fmas and rearrange the ops if so.  */
    7176                 :     3819951 :                   if (direct_internal_fn_supported_p (IFN_FMA,
    7177                 :     3819951 :                                                       TREE_TYPE (lhs),
    7178                 :             :                                                       opt_type)
    7179                 :     3819951 :                       && (rhs_code == PLUS_EXPR || rhs_code == MINUS_EXPR))
    7180                 :             :                     {
    7181                 :       18647 :                       mult_num = rank_ops_for_fma (&ops);
    7182                 :             :                     }
    7183                 :             : 
    7184                 :             :                   /* Only rewrite the expression tree to parallel in the
    7185                 :             :                      last reassoc pass to avoid useless work back-and-forth
    7186                 :             :                      with initial linearization.  */
    7187                 :     3819951 :                   bool has_fma = mult_num >= 2 && mult_num != ops_num;
    7188                 :     3819951 :                   if (!reassoc_insert_powi_p
    7189                 :     2284721 :                       && ops.length () > 3
    7190                 :     3834716 :                       && (width = get_reassociation_width (&ops, mult_num, lhs,
    7191                 :             :                                                            rhs_code, mode))
    7192                 :             :                            > 1)
    7193                 :             :                     {
    7194                 :        1492 :                       if (dump_file && (dump_flags & TDF_DETAILS))
    7195                 :           2 :                         fprintf (dump_file,
    7196                 :             :                                  "Width = %d was chosen for reassociation\n",
    7197                 :             :                                  width);
    7198                 :        1492 :                       rewrite_expr_tree_parallel (as_a <gassign *> (stmt),
    7199                 :             :                                                   width,
    7200                 :             :                                                   has_fma,
    7201                 :             :                                                   ops);
    7202                 :             :                     }
    7203                 :             :                   else
    7204                 :             :                     {
    7205                 :             :                       /* When there are three operands left, we want
    7206                 :             :                          to make sure the ones that get the double
    7207                 :             :                          binary op are chosen wisely.  */
    7208                 :     3818459 :                       int len = ops.length ();
    7209                 :     3818459 :                       if (len >= 3
    7210                 :     3818459 :                           && (!has_fma
    7211                 :             :                               /* width > 1 means ranking ops results in better
    7212                 :             :                                  parallelism.  Check current value to avoid
    7213                 :             :                                  calling get_reassociation_width again.  */
    7214                 :        4348 :                               || (width != 1
    7215                 :        4348 :                                   && get_reassociation_width (
    7216                 :             :                                        &ops, mult_num, lhs, rhs_code, mode)
    7217                 :             :                                        > 1)))
    7218                 :      145188 :                         swap_ops_for_binary_stmt (ops, len - 3);
    7219                 :             : 
    7220                 :     3818459 :                       new_lhs = rewrite_expr_tree (stmt, rhs_code, 0, ops,
    7221                 :     3818459 :                                                    powi_result != NULL
    7222                 :     3818459 :                                                    || negate_result,
    7223                 :             :                                                    len != orig_len);
    7224                 :             :                     }
    7225                 :             : 
    7226                 :             :                   /* If we combined some repeated factors into a 
    7227                 :             :                      __builtin_powi call, multiply that result by the
    7228                 :             :                      reassociated operands.  */
    7229                 :     3819951 :                   if (powi_result)
    7230                 :             :                     {
    7231                 :         110 :                       gimple *mul_stmt, *lhs_stmt = SSA_NAME_DEF_STMT (lhs);
    7232                 :         110 :                       tree type = TREE_TYPE (lhs);
    7233                 :         110 :                       tree target_ssa = make_temp_ssa_name (type, NULL,
    7234                 :             :                                                             "reassocpow");
    7235                 :         110 :                       gimple_set_lhs (lhs_stmt, target_ssa);
    7236                 :         110 :                       update_stmt (lhs_stmt);
    7237                 :         110 :                       if (lhs != new_lhs)
    7238                 :             :                         {
    7239                 :         110 :                           target_ssa = new_lhs;
    7240                 :         110 :                           new_lhs = lhs;
    7241                 :             :                         }
    7242                 :         110 :                       mul_stmt = gimple_build_assign (lhs, MULT_EXPR,
    7243                 :             :                                                       powi_result, target_ssa);
    7244                 :         110 :                       gimple_set_location (mul_stmt, gimple_location (stmt));
    7245                 :         110 :                       gimple_set_uid (mul_stmt, gimple_uid (stmt));
    7246                 :         110 :                       gsi_insert_after (&gsi, mul_stmt, GSI_NEW_STMT);
    7247                 :             :                     }
    7248                 :             :                 }
    7249                 :             : 
    7250                 :     3833718 :               if (negate_result)
    7251                 :             :                 {
    7252                 :         147 :                   stmt = SSA_NAME_DEF_STMT (lhs);
    7253                 :         147 :                   tree tmp = make_ssa_name (TREE_TYPE (lhs));
    7254                 :         147 :                   gimple_set_lhs (stmt, tmp);
    7255                 :         147 :                   if (lhs != new_lhs)
    7256                 :         137 :                     tmp = new_lhs;
    7257                 :         147 :                   gassign *neg_stmt = gimple_build_assign (lhs, NEGATE_EXPR,
    7258                 :             :                                                            tmp);
    7259                 :         147 :                   gimple_set_uid (neg_stmt, gimple_uid (stmt));
    7260                 :         147 :                   gsi_insert_after (&gsi, neg_stmt, GSI_NEW_STMT);
    7261                 :         147 :                   update_stmt (stmt);
    7262                 :             :                 }
    7263                 :     3869198 :             }
    7264                 :             :         }
    7265                 :             :     }
    7266                 :    19380622 :   for (son = first_dom_son (CDI_POST_DOMINATORS, bb);
    7267                 :    36817231 :        son;
    7268                 :    17436609 :        son = next_dom_son (CDI_POST_DOMINATORS, son))
    7269                 :    17436609 :     cfg_cleanup_needed |= reassociate_bb (son);
    7270                 :             : 
    7271                 :    19380622 :   return cfg_cleanup_needed;
    7272                 :             : }
    7273                 :             : 
    7274                 :             : /* Add jumps around shifts for range tests turned into bit tests.
    7275                 :             :    For each SSA_NAME VAR we have code like:
    7276                 :             :    VAR = ...; // final stmt of range comparison
    7277                 :             :    // bit test here...;
    7278                 :             :    OTHERVAR = ...; // final stmt of the bit test sequence
    7279                 :             :    RES = VAR | OTHERVAR;
    7280                 :             :    Turn the above into:
    7281                 :             :    VAR = ...;
    7282                 :             :    if (VAR != 0)
    7283                 :             :      goto <l3>;
    7284                 :             :    else
    7285                 :             :      goto <l2>;
    7286                 :             :    <l2>:
    7287                 :             :    // bit test here...;
    7288                 :             :    OTHERVAR = ...;
    7289                 :             :    <l3>:
    7290                 :             :    # RES = PHI<1(l1), OTHERVAR(l2)>;  */
    7291                 :             : 
    7292                 :             : static void
    7293                 :     1944013 : branch_fixup (void)
    7294                 :             : {
    7295                 :     1944013 :   tree var;
    7296                 :     1944013 :   unsigned int i;
    7297                 :             : 
    7298                 :     1944412 :   FOR_EACH_VEC_ELT (reassoc_branch_fixups, i, var)
    7299                 :             :     {
    7300                 :         399 :       gimple *def_stmt = SSA_NAME_DEF_STMT (var);
    7301                 :         399 :       gimple *use_stmt;
    7302                 :         399 :       use_operand_p use;
    7303                 :         399 :       bool ok = single_imm_use (var, &use, &use_stmt);
    7304                 :         399 :       gcc_assert (ok
    7305                 :             :                   && is_gimple_assign (use_stmt)
    7306                 :             :                   && gimple_assign_rhs_code (use_stmt) == BIT_IOR_EXPR
    7307                 :             :                   && gimple_bb (def_stmt) == gimple_bb (use_stmt));
    7308                 :             : 
    7309                 :         399 :       basic_block cond_bb = gimple_bb (def_stmt);
    7310                 :         399 :       basic_block then_bb = split_block (cond_bb, def_stmt)->dest;
    7311                 :         399 :       basic_block merge_bb = split_block (then_bb, use_stmt)->dest;
    7312                 :             : 
    7313                 :         399 :       gimple_stmt_iterator gsi = gsi_for_stmt (def_stmt);
    7314                 :         399 :       gimple *g = gimple_build_cond (NE_EXPR, var,
    7315                 :         399 :                                      build_zero_cst (TREE_TYPE (var)),
    7316                 :             :                                      NULL_TREE, NULL_TREE);
    7317                 :         399 :       location_t loc = gimple_location (use_stmt);
    7318                 :         399 :       gimple_set_location (g, loc);
    7319                 :         399 :       gsi_insert_after (&gsi, g, GSI_NEW_STMT);
    7320                 :             : 
    7321                 :         399 :       edge etrue = make_edge (cond_bb, merge_bb, EDGE_TRUE_VALUE);
    7322                 :         399 :       etrue->probability = profile_probability::even ();
    7323                 :         399 :       edge efalse = find_edge (cond_bb, then_bb);
    7324                 :         399 :       efalse->flags = EDGE_FALSE_VALUE;
    7325                 :         399 :       efalse->probability -= etrue->probability;
    7326                 :         399 :       then_bb->count -= etrue->count ();
    7327                 :             : 
    7328                 :         399 :       tree othervar = NULL_TREE;
    7329                 :         399 :       if (gimple_assign_rhs1 (use_stmt) == var)
    7330                 :           0 :         othervar = gimple_assign_rhs2 (use_stmt);
    7331                 :         399 :       else if (gimple_assign_rhs2 (use_stmt) == var)
    7332                 :             :         othervar = gimple_assign_rhs1 (use_stmt);
    7333                 :             :       else
    7334                 :           0 :         gcc_unreachable ();
    7335                 :         399 :       tree lhs = gimple_assign_lhs (use_stmt);
    7336                 :         399 :       gphi *phi = create_phi_node (lhs, merge_bb);
    7337                 :         399 :       add_phi_arg (phi, build_one_cst (TREE_TYPE (lhs)), etrue, loc);
    7338                 :         399 :       add_phi_arg (phi, othervar, single_succ_edge (then_bb), loc);
    7339                 :         399 :       gsi = gsi_for_stmt (use_stmt);
    7340                 :         399 :       gsi_remove (&gsi, true);
    7341                 :             : 
    7342                 :         399 :       set_immediate_dominator (CDI_DOMINATORS, merge_bb, cond_bb);
    7343                 :         399 :       set_immediate_dominator (CDI_POST_DOMINATORS, cond_bb, merge_bb);
    7344                 :             :     }
    7345                 :     1944013 :   reassoc_branch_fixups.release ();
    7346                 :     1944013 : }
    7347                 :             : 
    7348                 :             : void dump_ops_vector (FILE *file, vec<operand_entry *> ops);
    7349                 :             : void debug_ops_vector (vec<operand_entry *> ops);
    7350                 :             : 
    7351                 :             : /* Dump the operand entry vector OPS to FILE.  */
    7352                 :             : 
    7353                 :             : void
    7354                 :           0 : dump_ops_vector (FILE *file, vec<operand_entry *> ops)
    7355                 :             : {
    7356                 :           0 :   operand_entry *oe;
    7357                 :           0 :   unsigned int i;
    7358                 :             : 
    7359                 :           0 :   FOR_EACH_VEC_ELT (ops, i, oe)
    7360                 :             :     {
    7361                 :           0 :       fprintf (file, "Op %d -> rank: %d, tree: ", i, oe->rank);
    7362                 :           0 :       print_generic_expr (file, oe->op);
    7363                 :           0 :       fprintf (file, "\n");
    7364                 :             :     }
    7365                 :           0 : }
    7366                 :             : 
    7367                 :             : /* Dump the operand entry vector OPS to STDERR.  */
    7368                 :             : 
    7369                 :             : DEBUG_FUNCTION void
    7370                 :           0 : debug_ops_vector (vec<operand_entry *> ops)
    7371                 :             : {
    7372                 :           0 :   dump_ops_vector (stderr, ops);
    7373                 :           0 : }
    7374                 :             : 
    7375                 :             : /* Bubble up return status from reassociate_bb.  */
    7376                 :             : 
    7377                 :             : static bool
    7378                 :     1944013 : do_reassoc (void)
    7379                 :             : {
    7380                 :     1944013 :   break_up_subtract_bb (ENTRY_BLOCK_PTR_FOR_FN (cfun));
    7381                 :     1944013 :   return reassociate_bb (EXIT_BLOCK_PTR_FOR_FN (cfun));
    7382                 :             : }
    7383                 :             : 
    7384                 :             : /* Initialize the reassociation pass.  */
    7385                 :             : 
    7386                 :             : static void
    7387                 :     1944013 : init_reassoc (void)
    7388                 :             : {
    7389                 :     1944013 :   int i;
    7390                 :     1944013 :   int64_t rank = 2;
    7391                 :     1944013 :   int *bbs = XNEWVEC (int, n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS);
    7392                 :             : 
    7393                 :             :   /* Find the loops, so that we can prevent moving calculations in
    7394                 :             :      them.  */
    7395                 :     1944013 :   loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
    7396                 :             : 
    7397                 :     1944013 :   memset (&reassociate_stats, 0, sizeof (reassociate_stats));
    7398                 :             : 
    7399                 :     1944013 :   next_operand_entry_id = 0;
    7400                 :             : 
    7401                 :             :   /* Reverse RPO (Reverse Post Order) will give us something where
    7402                 :             :      deeper loops come later.  */
    7403                 :     1944013 :   pre_and_rev_post_order_compute (NULL, bbs, false);
    7404                 :     1944013 :   bb_rank = XCNEWVEC (int64_t, last_basic_block_for_fn (cfun));
    7405                 :     1944013 :   operand_rank = new hash_map<tree, int64_t>;
    7406                 :             : 
    7407                 :             :   /* Give each default definition a distinct rank.  This includes
    7408                 :             :      parameters and the static chain.  Walk backwards over all
    7409                 :             :      SSA names so that we get proper rank ordering according
    7410                 :             :      to tree_swap_operands_p.  */
    7411                 :    95633009 :   for (i = num_ssa_names - 1; i > 0; --i)
    7412                 :             :     {
    7413                 :    91744983 :       tree name = ssa_name (i);
    7414                 :   159250732 :       if (name && SSA_NAME_IS_DEFAULT_DEF (name))
    7415                 :     5716955 :         insert_operand_rank (name, ++rank);
    7416                 :             :     }
    7417                 :             : 
    7418                 :             :   /* Set up rank for each BB  */
    7419                 :    19380622 :   for (i = 0; i < n_basic_blocks_for_fn (cfun) - NUM_FIXED_BLOCKS; i++)
    7420                 :    17436609 :     bb_rank[bbs[i]] = ++rank << 16;
    7421                 :             : 
    7422                 :     1944013 :   free (bbs);
    7423                 :     1944013 :   calculate_dominance_info (CDI_POST_DOMINATORS);
    7424                 :     1944013 :   plus_negates = vNULL;
    7425                 :     1944013 :   mark_ssa_maybe_undefs ();
    7426                 :     1944013 : }
    7427                 :             : 
    7428                 :             : /* Cleanup after the reassociation pass, and print stats if
    7429                 :             :    requested.  */
    7430                 :             : 
    7431                 :             : static void
    7432                 :     1944013 : fini_reassoc (void)
    7433                 :             : {
    7434                 :     1944013 :   statistics_counter_event (cfun, "Linearized",
    7435                 :             :                             reassociate_stats.linearized);
    7436                 :     1944013 :   statistics_counter_event (cfun, "Constants eliminated",
    7437                 :             :                             reassociate_stats.constants_eliminated);
    7438                 :     1944013 :   statistics_counter_event (cfun, "Ops eliminated",
    7439                 :             :                             reassociate_stats.ops_eliminated);
    7440                 :     1944013 :   statistics_counter_event (cfun, "Statements rewritten",
    7441                 :             :                             reassociate_stats.rewritten);
    7442                 :     1944013 :   statistics_counter_event (cfun, "Built-in pow[i] calls encountered",
    7443                 :             :                             reassociate_stats.pows_encountered);
    7444                 :     1944013 :   statistics_counter_event (cfun, "Built-in powi calls created",
    7445                 :             :                             reassociate_stats.pows_created);
    7446                 :             : 
    7447                 :     3888026 :   delete operand_rank;
    7448                 :     1944013 :   bitmap_clear (biased_names);
    7449                 :     1944013 :   operand_entry_pool.release ();
    7450                 :     1944013 :   free (bb_rank);
    7451                 :     1944013 :   plus_negates.release ();
    7452                 :     1944013 :   free_dominance_info (CDI_POST_DOMINATORS);
    7453                 :     1944013 :   loop_optimizer_finalize ();
    7454                 :     1944013 : }
    7455                 :             : 
    7456                 :             : /* Gate and execute functions for Reassociation.  If INSERT_POWI_P, enable
    7457                 :             :    insertion of __builtin_powi calls.
    7458                 :             : 
    7459                 :             :    Returns TODO_cfg_cleanup if a CFG cleanup pass is desired due to
    7460                 :             :    optimization of a gimple conditional.  Otherwise returns zero.  */
    7461                 :             : 
    7462                 :             : static unsigned int
    7463                 :     1944013 : execute_reassoc (bool insert_powi_p, bool bias_loop_carried_phi_ranks_p)
    7464                 :             : {
    7465                 :     1944013 :   reassoc_insert_powi_p = insert_powi_p;
    7466                 :     1944013 :   reassoc_bias_loop_carried_phi_ranks_p = bias_loop_carried_phi_ranks_p;
    7467                 :             : 
    7468                 :     1944013 :   init_reassoc ();
    7469                 :             : 
    7470                 :     1944013 :   bool cfg_cleanup_needed;
    7471                 :     1944013 :   cfg_cleanup_needed = do_reassoc ();
    7472                 :     1944013 :   repropagate_negates ();
    7473                 :     1944013 :   branch_fixup ();
    7474                 :             : 
    7475                 :     1944013 :   fini_reassoc ();
    7476                 :     1944013 :   return cfg_cleanup_needed ? TODO_cleanup_cfg : 0;
    7477                 :             : }
    7478                 :             : 
    7479                 :             : namespace {
    7480                 :             : 
    7481                 :             : const pass_data pass_data_reassoc =
    7482                 :             : {
    7483                 :             :   GIMPLE_PASS, /* type */
    7484                 :             :   "reassoc", /* name */
    7485                 :             :   OPTGROUP_NONE, /* optinfo_flags */
    7486                 :             :   TV_TREE_REASSOC, /* tv_id */
    7487                 :             :   ( PROP_cfg | PROP_ssa ), /* properties_required */
    7488                 :             :   0, /* properties_provided */
    7489                 :             :   0, /* properties_destroyed */
    7490                 :             :   0, /* todo_flags_start */
    7491                 :             :   TODO_update_ssa_only_virtuals, /* todo_flags_finish */
    7492                 :             : };
    7493                 :             : 
    7494                 :             : class pass_reassoc : public gimple_opt_pass
    7495                 :             : {
    7496                 :             : public:
    7497                 :      560910 :   pass_reassoc (gcc::context *ctxt)
    7498                 :     1121820 :     : gimple_opt_pass (pass_data_reassoc, ctxt), insert_powi_p (false)
    7499                 :             :   {}
    7500                 :             : 
    7501                 :             :   /* opt_pass methods: */
    7502                 :      280455 :   opt_pass * clone () final override { return new pass_reassoc (m_ctxt); }
    7503                 :      560910 :   void set_pass_param (unsigned int n, bool param) final override
    7504                 :             :     {
    7505                 :      560910 :       gcc_assert (n == 0);
    7506                 :      560910 :       insert_powi_p = param;
    7507                 :      560910 :       bias_loop_carried_phi_ranks_p = !param;
    7508                 :      560910 :     }
    7509                 :     1944178 :   bool gate (function *) final override { return flag_tree_reassoc != 0; }
    7510                 :     1944013 :   unsigned int execute (function *) final override
    7511                 :             :   {
    7512                 :     1944013 :     return execute_reassoc (insert_powi_p, bias_loop_carried_phi_ranks_p);
    7513                 :             :   }
    7514                 :             : 
    7515                 :             :  private:
    7516                 :             :   /* Enable insertion of __builtin_powi calls during execute_reassoc.  See
    7517                 :             :      point 3a in the pass header comment.  */
    7518                 :             :   bool insert_powi_p;
    7519                 :             :   bool bias_loop_carried_phi_ranks_p;
    7520                 :             : }; // class pass_reassoc
    7521                 :             : 
    7522                 :             : } // anon namespace
    7523                 :             : 
    7524                 :             : gimple_opt_pass *
    7525                 :      280455 : make_pass_reassoc (gcc::context *ctxt)
    7526                 :             : {
    7527                 :      280455 :   return new pass_reassoc (ctxt);
    7528                 :             : }
        

Generated by: LCOV version 2.1-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.