Home | History | Annotate | Line # | Download | only in gcc
      1  1.1  mrg /* Rewrite a program in Normal form into SSA.
      2  1.1  mrg    Copyright (C) 2001-2022 Free Software Foundation, Inc.
      3  1.1  mrg    Contributed by Diego Novillo <dnovillo (at) redhat.com>
      4  1.1  mrg 
      5  1.1  mrg This file is part of GCC.
      6  1.1  mrg 
      7  1.1  mrg GCC is free software; you can redistribute it and/or modify
      8  1.1  mrg it under the terms of the GNU General Public License as published by
      9  1.1  mrg the Free Software Foundation; either version 3, or (at your option)
     10  1.1  mrg any later version.
     11  1.1  mrg 
     12  1.1  mrg GCC is distributed in the hope that it will be useful,
     13  1.1  mrg but WITHOUT ANY WARRANTY; without even the implied warranty of
     14  1.1  mrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     15  1.1  mrg GNU General Public License for more details.
     16  1.1  mrg 
     17  1.1  mrg You should have received a copy of the GNU General Public License
     18  1.1  mrg along with GCC; see the file COPYING3.  If not see
     19  1.1  mrg <http://www.gnu.org/licenses/>.  */
     20  1.1  mrg 
     21  1.1  mrg #include "config.h"
     22  1.1  mrg #include "system.h"
     23  1.1  mrg #include "coretypes.h"
     24  1.1  mrg #include "backend.h"
     25  1.1  mrg #include "rtl.h"
     26  1.1  mrg #include "tree.h"
     27  1.1  mrg #include "gimple.h"
     28  1.1  mrg #include "tree-pass.h"
     29  1.1  mrg #include "ssa.h"
     30  1.1  mrg #include "gimple-pretty-print.h"
     31  1.1  mrg #include "diagnostic-core.h"
     32  1.1  mrg #include "langhooks.h"
     33  1.1  mrg #include "cfganal.h"
     34  1.1  mrg #include "gimple-iterator.h"
     35  1.1  mrg #include "tree-cfg.h"
     36  1.1  mrg #include "tree-into-ssa.h"
     37  1.1  mrg #include "tree-dfa.h"
     38  1.1  mrg #include "tree-ssa.h"
     39  1.1  mrg #include "domwalk.h"
     40  1.1  mrg #include "statistics.h"
     41  1.1  mrg #include "stringpool.h"
     42  1.1  mrg #include "attribs.h"
     43  1.1  mrg #include "asan.h"
     44  1.1  mrg #include "attr-fnspec.h"
     45  1.1  mrg 
     46  1.1  mrg #define PERCENT(x,y) ((float)(x) * 100.0 / (float)(y))
     47  1.1  mrg 
     48  1.1  mrg /* This file builds the SSA form for a function as described in:
     49  1.1  mrg    R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
     50  1.1  mrg    Computing Static Single Assignment Form and the Control Dependence
     51  1.1  mrg    Graph. ACM Transactions on Programming Languages and Systems,
     52  1.1  mrg    13(4):451-490, October 1991.  */
     53  1.1  mrg 
     54  1.1  mrg /* Structure to map a variable VAR to the set of blocks that contain
     55  1.1  mrg    definitions for VAR.  */
     56  1.1  mrg struct def_blocks
     57  1.1  mrg {
     58  1.1  mrg   /* Blocks that contain definitions of VAR.  Bit I will be set if the
     59  1.1  mrg      Ith block contains a definition of VAR.  */
     60  1.1  mrg   bitmap def_blocks;
     61  1.1  mrg 
     62  1.1  mrg   /* Blocks that contain a PHI node for VAR.  */
     63  1.1  mrg   bitmap phi_blocks;
     64  1.1  mrg 
     65  1.1  mrg   /* Blocks where VAR is live-on-entry.  Similar semantics as
     66  1.1  mrg      DEF_BLOCKS.  */
     67  1.1  mrg   bitmap livein_blocks;
     68  1.1  mrg };
     69  1.1  mrg 
     70  1.1  mrg /* Stack of trees used to restore the global currdefs to its original
     71  1.1  mrg    state after completing rewriting of a block and its dominator
     72  1.1  mrg    children.  Its elements have the following properties:
     73  1.1  mrg 
     74  1.1  mrg    - An SSA_NAME (N) indicates that the current definition of the
     75  1.1  mrg      underlying variable should be set to the given SSA_NAME.  If the
     76  1.1  mrg      symbol associated with the SSA_NAME is not a GIMPLE register, the
     77  1.1  mrg      next slot in the stack must be a _DECL node (SYM).  In this case,
     78  1.1  mrg      the name N in the previous slot is the current reaching
     79  1.1  mrg      definition for SYM.
     80  1.1  mrg 
     81  1.1  mrg    - A _DECL node indicates that the underlying variable has no
     82  1.1  mrg      current definition.
     83  1.1  mrg 
     84  1.1  mrg    - A NULL node at the top entry is used to mark the last slot
     85  1.1  mrg      associated with the current block.  */
     86  1.1  mrg static vec<tree> block_defs_stack;
     87  1.1  mrg 
     88  1.1  mrg 
     89  1.1  mrg /* Set of existing SSA names being replaced by update_ssa.  */
     90  1.1  mrg static sbitmap old_ssa_names;
     91  1.1  mrg 
     92  1.1  mrg /* Set of new SSA names being added by update_ssa.  Note that both
     93  1.1  mrg    NEW_SSA_NAMES and OLD_SSA_NAMES are dense bitmaps because most of
     94  1.1  mrg    the operations done on them are presence tests.  */
     95  1.1  mrg static sbitmap new_ssa_names;
     96  1.1  mrg 
     97  1.1  mrg static sbitmap interesting_blocks;
     98  1.1  mrg 
     99  1.1  mrg /* Set of SSA names that have been marked to be released after they
    100  1.1  mrg    were registered in the replacement table.  They will be finally
    101  1.1  mrg    released after we finish updating the SSA web.  */
    102  1.1  mrg bitmap names_to_release;
    103  1.1  mrg 
    104  1.1  mrg /* vec of vec of PHIs to rewrite in a basic block.  Element I corresponds
    105  1.1  mrg    the to basic block with index I.  Allocated once per compilation, *not*
    106  1.1  mrg    released between different functions.  */
    107  1.1  mrg static vec< vec<gphi *> > phis_to_rewrite;
    108  1.1  mrg 
    109  1.1  mrg /* The bitmap of non-NULL elements of PHIS_TO_REWRITE.  */
    110  1.1  mrg static bitmap blocks_with_phis_to_rewrite;
    111  1.1  mrg 
    112  1.1  mrg /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES.  These sets need
    113  1.1  mrg    to grow as the callers to create_new_def_for will create new names on
    114  1.1  mrg    the fly.
    115  1.1  mrg    FIXME.  Currently set to 1/3 to avoid frequent reallocations but still
    116  1.1  mrg    need to find a reasonable growth strategy.  */
    117  1.1  mrg #define NAME_SETS_GROWTH_FACTOR	(MAX (3, num_ssa_names / 3))
    118  1.1  mrg 
    119  1.1  mrg 
    120  1.1  mrg /* The function the SSA updating data structures have been initialized for.
    121  1.1  mrg    NULL if they need to be initialized by create_new_def_for.  */
    122  1.1  mrg static struct function *update_ssa_initialized_fn = NULL;
    123  1.1  mrg 
    124  1.1  mrg /* Global data to attach to the main dominator walk structure.  */
    125  1.1  mrg struct mark_def_sites_global_data
    126  1.1  mrg {
    127  1.1  mrg   /* This bitmap contains the variables which are set before they
    128  1.1  mrg      are used in a basic block.  */
    129  1.1  mrg   bitmap kills;
    130  1.1  mrg };
    131  1.1  mrg 
    132  1.1  mrg /* It is advantageous to avoid things like life analysis for variables which
    133  1.1  mrg    do not need PHI nodes.  This enum describes whether or not a particular
    134  1.1  mrg    variable may need a PHI node.  */
    135  1.1  mrg 
    136  1.1  mrg enum need_phi_state {
    137  1.1  mrg   /* This is the default.  If we are still in this state after finding
    138  1.1  mrg      all the definition and use sites, then we will assume the variable
    139  1.1  mrg      needs PHI nodes.  This is probably an overly conservative assumption.  */
    140  1.1  mrg   NEED_PHI_STATE_UNKNOWN,
    141  1.1  mrg 
    142  1.1  mrg   /* This state indicates that we have seen one or more sets of the
    143  1.1  mrg      variable in a single basic block and that the sets dominate all
    144  1.1  mrg      uses seen so far.  If after finding all definition and use sites
    145  1.1  mrg      we are still in this state, then the variable does not need any
    146  1.1  mrg      PHI nodes.  */
    147  1.1  mrg   NEED_PHI_STATE_NO,
    148  1.1  mrg 
    149  1.1  mrg   /* This state indicates that we have either seen multiple definitions of
    150  1.1  mrg      the variable in multiple blocks, or that we encountered a use in a
    151  1.1  mrg      block that was not dominated by the block containing the set(s) of
    152  1.1  mrg      this variable.  This variable is assumed to need PHI nodes.  */
    153  1.1  mrg   NEED_PHI_STATE_MAYBE
    154  1.1  mrg };
    155  1.1  mrg 
    156  1.1  mrg /* Information stored for both SSA names and decls.  */
    157  1.1  mrg struct common_info
    158  1.1  mrg {
    159  1.1  mrg   /* This field indicates whether or not the variable may need PHI nodes.
    160  1.1  mrg      See the enum's definition for more detailed information about the
    161  1.1  mrg      states.  */
    162  1.1  mrg   ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
    163  1.1  mrg 
    164  1.1  mrg   /* The current reaching definition replacing this var.  */
    165  1.1  mrg   tree current_def;
    166  1.1  mrg 
    167  1.1  mrg   /* Definitions for this var.  */
    168  1.1  mrg   struct def_blocks def_blocks;
    169  1.1  mrg };
    170  1.1  mrg 
    171  1.1  mrg /* Information stored for decls.  */
    172  1.1  mrg struct var_info
    173  1.1  mrg {
    174  1.1  mrg   /* The variable.  */
    175  1.1  mrg   tree var;
    176  1.1  mrg 
    177  1.1  mrg   /* Information stored for both SSA names and decls.  */
    178  1.1  mrg   common_info info;
    179  1.1  mrg };
    180  1.1  mrg 
    181  1.1  mrg 
    182  1.1  mrg /* VAR_INFOS hashtable helpers.  */
    183  1.1  mrg 
    184  1.1  mrg struct var_info_hasher : free_ptr_hash <var_info>
    185  1.1  mrg {
    186  1.1  mrg   static inline hashval_t hash (const value_type &);
    187  1.1  mrg   static inline bool equal (const value_type &, const compare_type &);
    188  1.1  mrg };
    189  1.1  mrg 
    190  1.1  mrg inline hashval_t
    191  1.1  mrg var_info_hasher::hash (const value_type &p)
    192  1.1  mrg {
    193  1.1  mrg   return DECL_UID (p->var);
    194  1.1  mrg }
    195  1.1  mrg 
    196  1.1  mrg inline bool
    197  1.1  mrg var_info_hasher::equal (const value_type &p1, const compare_type &p2)
    198  1.1  mrg {
    199  1.1  mrg   return p1->var == p2->var;
    200  1.1  mrg }
    201  1.1  mrg 
    202  1.1  mrg 
    203  1.1  mrg /* Each entry in VAR_INFOS contains an element of type STRUCT
    204  1.1  mrg    VAR_INFO_D.  */
    205  1.1  mrg static hash_table<var_info_hasher> *var_infos;
    206  1.1  mrg 
    207  1.1  mrg 
    208  1.1  mrg /* Information stored for SSA names.  */
    209  1.1  mrg struct ssa_name_info
    210  1.1  mrg {
    211  1.1  mrg   /* Age of this record (so that info_for_ssa_name table can be cleared
    212  1.1  mrg      quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
    213  1.1  mrg      are assumed to be null.  */
    214  1.1  mrg   unsigned age;
    215  1.1  mrg 
    216  1.1  mrg   /* Replacement mappings, allocated from update_ssa_obstack.  */
    217  1.1  mrg   bitmap repl_set;
    218  1.1  mrg 
    219  1.1  mrg   /* Information stored for both SSA names and decls.  */
    220  1.1  mrg   common_info info;
    221  1.1  mrg };
    222  1.1  mrg 
    223  1.1  mrg static vec<ssa_name_info *> info_for_ssa_name;
    224  1.1  mrg static unsigned current_info_for_ssa_name_age;
    225  1.1  mrg 
    226  1.1  mrg static bitmap_obstack update_ssa_obstack;
    227  1.1  mrg 
    228  1.1  mrg /* The set of blocks affected by update_ssa.  */
    229  1.1  mrg static bitmap blocks_to_update;
    230  1.1  mrg 
    231  1.1  mrg /* The main entry point to the SSA renamer (rewrite_blocks) may be
    232  1.1  mrg    called several times to do different, but related, tasks.
    233  1.1  mrg    Initially, we need it to rename the whole program into SSA form.
    234  1.1  mrg    At other times, we may need it to only rename into SSA newly
    235  1.1  mrg    exposed symbols.  Finally, we can also call it to incrementally fix
    236  1.1  mrg    an already built SSA web.  */
    237  1.1  mrg enum rewrite_mode {
    238  1.1  mrg     /* Convert the whole function into SSA form.  */
    239  1.1  mrg     REWRITE_ALL,
    240  1.1  mrg 
    241  1.1  mrg     /* Incrementally update the SSA web by replacing existing SSA
    242  1.1  mrg        names with new ones.  See update_ssa for details.  */
    243  1.1  mrg     REWRITE_UPDATE
    244  1.1  mrg };
    245  1.1  mrg 
    246  1.1  mrg /* The set of symbols we ought to re-write into SSA form in update_ssa.  */
    247  1.1  mrg static bitmap symbols_to_rename_set;
    248  1.1  mrg static vec<tree> symbols_to_rename;
    249  1.1  mrg 
    250  1.1  mrg /* Mark SYM for renaming.  */
    251  1.1  mrg 
    252  1.1  mrg static void
    253  1.1  mrg mark_for_renaming (tree sym)
    254  1.1  mrg {
    255  1.1  mrg   if (!symbols_to_rename_set)
    256  1.1  mrg     symbols_to_rename_set = BITMAP_ALLOC (NULL);
    257  1.1  mrg   if (bitmap_set_bit (symbols_to_rename_set, DECL_UID (sym)))
    258  1.1  mrg     symbols_to_rename.safe_push (sym);
    259  1.1  mrg }
    260  1.1  mrg 
    261  1.1  mrg /* Return true if SYM is marked for renaming.  */
    262  1.1  mrg 
    263  1.1  mrg static bool
    264  1.1  mrg marked_for_renaming (tree sym)
    265  1.1  mrg {
    266  1.1  mrg   if (!symbols_to_rename_set || sym == NULL_TREE)
    267  1.1  mrg     return false;
    268  1.1  mrg   return bitmap_bit_p (symbols_to_rename_set, DECL_UID (sym));
    269  1.1  mrg }
    270  1.1  mrg 
    271  1.1  mrg 
    272  1.1  mrg /* Return true if STMT needs to be rewritten.  When renaming a subset
    273  1.1  mrg    of the variables, not all statements will be processed.  This is
    274  1.1  mrg    decided in mark_def_sites.  */
    275  1.1  mrg 
    276  1.1  mrg static inline bool
    277  1.1  mrg rewrite_uses_p (gimple *stmt)
    278  1.1  mrg {
    279  1.1  mrg   return gimple_visited_p (stmt);
    280  1.1  mrg }
    281  1.1  mrg 
    282  1.1  mrg 
    283  1.1  mrg /* Set the rewrite marker on STMT to the value given by REWRITE_P.  */
    284  1.1  mrg 
    285  1.1  mrg static inline void
    286  1.1  mrg set_rewrite_uses (gimple *stmt, bool rewrite_p)
    287  1.1  mrg {
    288  1.1  mrg   gimple_set_visited (stmt, rewrite_p);
    289  1.1  mrg }
    290  1.1  mrg 
    291  1.1  mrg 
    292  1.1  mrg /* Return true if the DEFs created by statement STMT should be
    293  1.1  mrg    registered when marking new definition sites.  This is slightly
    294  1.1  mrg    different than rewrite_uses_p: it's used by update_ssa to
    295  1.1  mrg    distinguish statements that need to have both uses and defs
    296  1.1  mrg    processed from those that only need to have their defs processed.
    297  1.1  mrg    Statements that define new SSA names only need to have their defs
    298  1.1  mrg    registered, but they don't need to have their uses renamed.  */
    299  1.1  mrg 
    300  1.1  mrg static inline bool
    301  1.1  mrg register_defs_p (gimple *stmt)
    302  1.1  mrg {
    303  1.1  mrg   return gimple_plf (stmt, GF_PLF_1) != 0;
    304  1.1  mrg }
    305  1.1  mrg 
    306  1.1  mrg 
    307  1.1  mrg /* If REGISTER_DEFS_P is true, mark STMT to have its DEFs registered.  */
    308  1.1  mrg 
    309  1.1  mrg static inline void
    310  1.1  mrg set_register_defs (gimple *stmt, bool register_defs_p)
    311  1.1  mrg {
    312  1.1  mrg   gimple_set_plf (stmt, GF_PLF_1, register_defs_p);
    313  1.1  mrg }
    314  1.1  mrg 
    315  1.1  mrg 
    316  1.1  mrg /* Get the information associated with NAME.  */
    317  1.1  mrg 
    318  1.1  mrg static inline ssa_name_info *
    319  1.1  mrg get_ssa_name_ann (tree name)
    320  1.1  mrg {
    321  1.1  mrg   unsigned ver = SSA_NAME_VERSION (name);
    322  1.1  mrg   unsigned len = info_for_ssa_name.length ();
    323  1.1  mrg   struct ssa_name_info *info;
    324  1.1  mrg 
    325  1.1  mrg   /* Re-allocate the vector at most once per update/into-SSA.  */
    326  1.1  mrg   if (ver >= len)
    327  1.1  mrg     info_for_ssa_name.safe_grow_cleared (num_ssa_names, true);
    328  1.1  mrg 
    329  1.1  mrg   /* But allocate infos lazily.  */
    330  1.1  mrg   info = info_for_ssa_name[ver];
    331  1.1  mrg   if (!info)
    332  1.1  mrg     {
    333  1.1  mrg       info = XCNEW (struct ssa_name_info);
    334  1.1  mrg       info->age = current_info_for_ssa_name_age;
    335  1.1  mrg       info->info.need_phi_state = NEED_PHI_STATE_UNKNOWN;
    336  1.1  mrg       info_for_ssa_name[ver] = info;
    337  1.1  mrg     }
    338  1.1  mrg 
    339  1.1  mrg   if (info->age < current_info_for_ssa_name_age)
    340  1.1  mrg     {
    341  1.1  mrg       info->age = current_info_for_ssa_name_age;
    342  1.1  mrg       info->repl_set = NULL;
    343  1.1  mrg       info->info.need_phi_state = NEED_PHI_STATE_UNKNOWN;
    344  1.1  mrg       info->info.current_def = NULL_TREE;
    345  1.1  mrg       info->info.def_blocks.def_blocks = NULL;
    346  1.1  mrg       info->info.def_blocks.phi_blocks = NULL;
    347  1.1  mrg       info->info.def_blocks.livein_blocks = NULL;
    348  1.1  mrg     }
    349  1.1  mrg 
    350  1.1  mrg   return info;
    351  1.1  mrg }
    352  1.1  mrg 
    353  1.1  mrg /* Return and allocate the auxiliar information for DECL.  */
    354  1.1  mrg 
    355  1.1  mrg static inline var_info *
    356  1.1  mrg get_var_info (tree decl)
    357  1.1  mrg {
    358  1.1  mrg   var_info vi;
    359  1.1  mrg   var_info **slot;
    360  1.1  mrg   vi.var = decl;
    361  1.1  mrg   slot = var_infos->find_slot_with_hash (&vi, DECL_UID (decl), INSERT);
    362  1.1  mrg   if (*slot == NULL)
    363  1.1  mrg     {
    364  1.1  mrg       var_info *v = XCNEW (var_info);
    365  1.1  mrg       v->var = decl;
    366  1.1  mrg       *slot = v;
    367  1.1  mrg       return v;
    368  1.1  mrg     }
    369  1.1  mrg   return *slot;
    370  1.1  mrg }
    371  1.1  mrg 
    372  1.1  mrg 
    373  1.1  mrg /* Clears info for SSA names.  */
    374  1.1  mrg 
    375  1.1  mrg static void
    376  1.1  mrg clear_ssa_name_info (void)
    377  1.1  mrg {
    378  1.1  mrg   current_info_for_ssa_name_age++;
    379  1.1  mrg 
    380  1.1  mrg   /* If current_info_for_ssa_name_age wraps we use stale information.
    381  1.1  mrg      Asser that this does not happen.  */
    382  1.1  mrg   gcc_assert (current_info_for_ssa_name_age != 0);
    383  1.1  mrg }
    384  1.1  mrg 
    385  1.1  mrg 
    386  1.1  mrg /* Get access to the auxiliar information stored per SSA name or decl.  */
    387  1.1  mrg 
    388  1.1  mrg static inline common_info *
    389  1.1  mrg get_common_info (tree var)
    390  1.1  mrg {
    391  1.1  mrg   if (TREE_CODE (var) == SSA_NAME)
    392  1.1  mrg     return &get_ssa_name_ann (var)->info;
    393  1.1  mrg   else
    394  1.1  mrg     return &get_var_info (var)->info;
    395  1.1  mrg }
    396  1.1  mrg 
    397  1.1  mrg 
    398  1.1  mrg /* Return the current definition for VAR.  */
    399  1.1  mrg 
    400  1.1  mrg tree
    401  1.1  mrg get_current_def (tree var)
    402  1.1  mrg {
    403  1.1  mrg   return get_common_info (var)->current_def;
    404  1.1  mrg }
    405  1.1  mrg 
    406  1.1  mrg 
    407  1.1  mrg /* Sets current definition of VAR to DEF.  */
    408  1.1  mrg 
    409  1.1  mrg void
    410  1.1  mrg set_current_def (tree var, tree def)
    411  1.1  mrg {
    412  1.1  mrg   get_common_info (var)->current_def = def;
    413  1.1  mrg }
    414  1.1  mrg 
    415  1.1  mrg /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
    416  1.1  mrg    all statements in basic block BB.  */
    417  1.1  mrg 
    418  1.1  mrg static void
    419  1.1  mrg initialize_flags_in_bb (basic_block bb)
    420  1.1  mrg {
    421  1.1  mrg   gimple *stmt;
    422  1.1  mrg   gimple_stmt_iterator gsi;
    423  1.1  mrg 
    424  1.1  mrg   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    425  1.1  mrg     {
    426  1.1  mrg       gimple *phi = gsi_stmt (gsi);
    427  1.1  mrg       set_rewrite_uses (phi, false);
    428  1.1  mrg       set_register_defs (phi, false);
    429  1.1  mrg     }
    430  1.1  mrg 
    431  1.1  mrg   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
    432  1.1  mrg     {
    433  1.1  mrg       stmt = gsi_stmt (gsi);
    434  1.1  mrg 
    435  1.1  mrg       /* We are going to use the operand cache API, such as
    436  1.1  mrg 	 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST.  The operand
    437  1.1  mrg 	 cache for each statement should be up-to-date.  */
    438  1.1  mrg       gcc_checking_assert (!gimple_modified_p (stmt));
    439  1.1  mrg       set_rewrite_uses (stmt, false);
    440  1.1  mrg       set_register_defs (stmt, false);
    441  1.1  mrg     }
    442  1.1  mrg }
    443  1.1  mrg 
    444  1.1  mrg /* Mark block BB as interesting for update_ssa.  */
    445  1.1  mrg 
    446  1.1  mrg static void
    447  1.1  mrg mark_block_for_update (basic_block bb)
    448  1.1  mrg {
    449  1.1  mrg   gcc_checking_assert (blocks_to_update != NULL);
    450  1.1  mrg   if (!bitmap_set_bit (blocks_to_update, bb->index))
    451  1.1  mrg     return;
    452  1.1  mrg   initialize_flags_in_bb (bb);
    453  1.1  mrg }
    454  1.1  mrg 
    455  1.1  mrg /* Return the set of blocks where variable VAR is defined and the blocks
    456  1.1  mrg    where VAR is live on entry (livein).  If no entry is found in
    457  1.1  mrg    DEF_BLOCKS, a new one is created and returned.  */
    458  1.1  mrg 
    459  1.1  mrg static inline def_blocks *
    460  1.1  mrg get_def_blocks_for (common_info *info)
    461  1.1  mrg {
    462  1.1  mrg   def_blocks *db_p = &info->def_blocks;
    463  1.1  mrg   if (!db_p->def_blocks)
    464  1.1  mrg     {
    465  1.1  mrg       db_p->def_blocks = BITMAP_ALLOC (&update_ssa_obstack);
    466  1.1  mrg       db_p->phi_blocks = BITMAP_ALLOC (&update_ssa_obstack);
    467  1.1  mrg       db_p->livein_blocks = BITMAP_ALLOC (&update_ssa_obstack);
    468  1.1  mrg     }
    469  1.1  mrg 
    470  1.1  mrg   return db_p;
    471  1.1  mrg }
    472  1.1  mrg 
    473  1.1  mrg 
    474  1.1  mrg /* Mark block BB as the definition site for variable VAR.  PHI_P is true if
    475  1.1  mrg    VAR is defined by a PHI node.  */
    476  1.1  mrg 
    477  1.1  mrg static void
    478  1.1  mrg set_def_block (tree var, basic_block bb, bool phi_p)
    479  1.1  mrg {
    480  1.1  mrg   def_blocks *db_p;
    481  1.1  mrg   common_info *info;
    482  1.1  mrg 
    483  1.1  mrg   info = get_common_info (var);
    484  1.1  mrg   db_p = get_def_blocks_for (info);
    485  1.1  mrg 
    486  1.1  mrg   /* Set the bit corresponding to the block where VAR is defined.  */
    487  1.1  mrg   bitmap_set_bit (db_p->def_blocks, bb->index);
    488  1.1  mrg   if (phi_p)
    489  1.1  mrg     bitmap_set_bit (db_p->phi_blocks, bb->index);
    490  1.1  mrg 
    491  1.1  mrg   /* Keep track of whether or not we may need to insert PHI nodes.
    492  1.1  mrg 
    493  1.1  mrg      If we are in the UNKNOWN state, then this is the first definition
    494  1.1  mrg      of VAR.  Additionally, we have not seen any uses of VAR yet, so
    495  1.1  mrg      we do not need a PHI node for this variable at this time (i.e.,
    496  1.1  mrg      transition to NEED_PHI_STATE_NO).
    497  1.1  mrg 
    498  1.1  mrg      If we are in any other state, then we either have multiple definitions
    499  1.1  mrg      of this variable occurring in different blocks or we saw a use of the
    500  1.1  mrg      variable which was not dominated by the block containing the
    501  1.1  mrg      definition(s).  In this case we may need a PHI node, so enter
    502  1.1  mrg      state NEED_PHI_STATE_MAYBE.  */
    503  1.1  mrg   if (info->need_phi_state == NEED_PHI_STATE_UNKNOWN)
    504  1.1  mrg     info->need_phi_state = NEED_PHI_STATE_NO;
    505  1.1  mrg   else
    506  1.1  mrg     info->need_phi_state = NEED_PHI_STATE_MAYBE;
    507  1.1  mrg }
    508  1.1  mrg 
    509  1.1  mrg 
    510  1.1  mrg /* Mark block BB as having VAR live at the entry to BB.  */
    511  1.1  mrg 
    512  1.1  mrg static void
    513  1.1  mrg set_livein_block (tree var, basic_block bb)
    514  1.1  mrg {
    515  1.1  mrg   common_info *info;
    516  1.1  mrg   def_blocks *db_p;
    517  1.1  mrg 
    518  1.1  mrg   info = get_common_info (var);
    519  1.1  mrg   db_p = get_def_blocks_for (info);
    520  1.1  mrg 
    521  1.1  mrg   /* Set the bit corresponding to the block where VAR is live in.  */
    522  1.1  mrg   bitmap_set_bit (db_p->livein_blocks, bb->index);
    523  1.1  mrg 
    524  1.1  mrg   /* Keep track of whether or not we may need to insert PHI nodes.
    525  1.1  mrg 
    526  1.1  mrg      If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
    527  1.1  mrg      by the single block containing the definition(s) of this variable.  If
    528  1.1  mrg      it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
    529  1.1  mrg      NEED_PHI_STATE_MAYBE.  */
    530  1.1  mrg   if (info->need_phi_state == NEED_PHI_STATE_NO)
    531  1.1  mrg     {
    532  1.1  mrg       int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
    533  1.1  mrg 
    534  1.1  mrg       if (def_block_index == -1
    535  1.1  mrg 	  || ! dominated_by_p (CDI_DOMINATORS, bb,
    536  1.1  mrg 	                       BASIC_BLOCK_FOR_FN (cfun, def_block_index)))
    537  1.1  mrg 	info->need_phi_state = NEED_PHI_STATE_MAYBE;
    538  1.1  mrg     }
    539  1.1  mrg   else
    540  1.1  mrg     info->need_phi_state = NEED_PHI_STATE_MAYBE;
    541  1.1  mrg }
    542  1.1  mrg 
    543  1.1  mrg 
    544  1.1  mrg /* Return true if NAME is in OLD_SSA_NAMES.  */
    545  1.1  mrg 
    546  1.1  mrg static inline bool
    547  1.1  mrg is_old_name (tree name)
    548  1.1  mrg {
    549  1.1  mrg   unsigned ver = SSA_NAME_VERSION (name);
    550  1.1  mrg   if (!old_ssa_names)
    551  1.1  mrg     return false;
    552  1.1  mrg   return (ver < SBITMAP_SIZE (old_ssa_names)
    553  1.1  mrg 	  && bitmap_bit_p (old_ssa_names, ver));
    554  1.1  mrg }
    555  1.1  mrg 
    556  1.1  mrg 
    557  1.1  mrg /* Return true if NAME is in NEW_SSA_NAMES.  */
    558  1.1  mrg 
    559  1.1  mrg static inline bool
    560  1.1  mrg is_new_name (tree name)
    561  1.1  mrg {
    562  1.1  mrg   unsigned ver = SSA_NAME_VERSION (name);
    563  1.1  mrg   if (!new_ssa_names)
    564  1.1  mrg     return false;
    565  1.1  mrg   return (ver < SBITMAP_SIZE (new_ssa_names)
    566  1.1  mrg 	  && bitmap_bit_p (new_ssa_names, ver));
    567  1.1  mrg }
    568  1.1  mrg 
    569  1.1  mrg 
    570  1.1  mrg /* Return the names replaced by NEW_TREE (i.e., REPL_TBL[NEW_TREE].SET).  */
    571  1.1  mrg 
    572  1.1  mrg static inline bitmap
    573  1.1  mrg names_replaced_by (tree new_tree)
    574  1.1  mrg {
    575  1.1  mrg   return get_ssa_name_ann (new_tree)->repl_set;
    576  1.1  mrg }
    577  1.1  mrg 
    578  1.1  mrg 
    579  1.1  mrg /* Add OLD to REPL_TBL[NEW_TREE].SET.  */
    580  1.1  mrg 
    581  1.1  mrg static inline void
    582  1.1  mrg add_to_repl_tbl (tree new_tree, tree old)
    583  1.1  mrg {
    584  1.1  mrg   bitmap *set = &get_ssa_name_ann (new_tree)->repl_set;
    585  1.1  mrg   if (!*set)
    586  1.1  mrg     *set = BITMAP_ALLOC (&update_ssa_obstack);
    587  1.1  mrg   bitmap_set_bit (*set, SSA_NAME_VERSION (old));
    588  1.1  mrg }
    589  1.1  mrg 
    590  1.1  mrg 
    591  1.1  mrg /* Add a new mapping NEW_TREE -> OLD REPL_TBL.  Every entry N_i in REPL_TBL
    592  1.1  mrg    represents the set of names O_1 ... O_j replaced by N_i.  This is
    593  1.1  mrg    used by update_ssa and its helpers to introduce new SSA names in an
    594  1.1  mrg    already formed SSA web.  */
    595  1.1  mrg 
    596  1.1  mrg static void
    597  1.1  mrg add_new_name_mapping (tree new_tree, tree old)
    598  1.1  mrg {
    599  1.1  mrg   /* OLD and NEW_TREE must be different SSA names for the same symbol.  */
    600  1.1  mrg   gcc_checking_assert (new_tree != old
    601  1.1  mrg 		       && SSA_NAME_VAR (new_tree) == SSA_NAME_VAR (old));
    602  1.1  mrg 
    603  1.1  mrg   /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
    604  1.1  mrg      caller may have created new names since the set was created.  */
    605  1.1  mrg   if (SBITMAP_SIZE (new_ssa_names) <= num_ssa_names - 1)
    606  1.1  mrg     {
    607  1.1  mrg       unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
    608  1.1  mrg       new_ssa_names = sbitmap_resize (new_ssa_names, new_sz, 0);
    609  1.1  mrg       old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0);
    610  1.1  mrg     }
    611  1.1  mrg 
    612  1.1  mrg   /* Update the REPL_TBL table.  */
    613  1.1  mrg   add_to_repl_tbl (new_tree, old);
    614  1.1  mrg 
    615  1.1  mrg   /* If OLD had already been registered as a new name, then all the
    616  1.1  mrg      names that OLD replaces should also be replaced by NEW_TREE.  */
    617  1.1  mrg   if (is_new_name (old))
    618  1.1  mrg     bitmap_ior_into (names_replaced_by (new_tree), names_replaced_by (old));
    619  1.1  mrg 
    620  1.1  mrg   /* Register NEW_TREE and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
    621  1.1  mrg      respectively.  */
    622  1.1  mrg   bitmap_set_bit (new_ssa_names, SSA_NAME_VERSION (new_tree));
    623  1.1  mrg   bitmap_set_bit (old_ssa_names, SSA_NAME_VERSION (old));
    624  1.1  mrg }
    625  1.1  mrg 
    626  1.1  mrg 
    627  1.1  mrg /* Call back for walk_dominator_tree used to collect definition sites
    628  1.1  mrg    for every variable in the function.  For every statement S in block
    629  1.1  mrg    BB:
    630  1.1  mrg 
    631  1.1  mrg    1- Variables defined by S in the DEFS of S are marked in the bitmap
    632  1.1  mrg       KILLS.
    633  1.1  mrg 
    634  1.1  mrg    2- If S uses a variable VAR and there is no preceding kill of VAR,
    635  1.1  mrg       then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
    636  1.1  mrg 
    637  1.1  mrg    This information is used to determine which variables are live
    638  1.1  mrg    across block boundaries to reduce the number of PHI nodes
    639  1.1  mrg    we create.  */
    640  1.1  mrg 
    641  1.1  mrg static void
    642  1.1  mrg mark_def_sites (basic_block bb, gimple *stmt, bitmap kills)
    643  1.1  mrg {
    644  1.1  mrg   tree def;
    645  1.1  mrg   use_operand_p use_p;
    646  1.1  mrg   ssa_op_iter iter;
    647  1.1  mrg 
    648  1.1  mrg   /* Since this is the first time that we rewrite the program into SSA
    649  1.1  mrg      form, force an operand scan on every statement.  */
    650  1.1  mrg   update_stmt (stmt);
    651  1.1  mrg 
    652  1.1  mrg   gcc_checking_assert (blocks_to_update == NULL);
    653  1.1  mrg   set_register_defs (stmt, false);
    654  1.1  mrg   set_rewrite_uses (stmt, false);
    655  1.1  mrg 
    656  1.1  mrg   if (is_gimple_debug (stmt))
    657  1.1  mrg     {
    658  1.1  mrg       FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
    659  1.1  mrg 	{
    660  1.1  mrg 	  tree sym = USE_FROM_PTR (use_p);
    661  1.1  mrg 	  gcc_checking_assert (DECL_P (sym));
    662  1.1  mrg 	  set_rewrite_uses (stmt, true);
    663  1.1  mrg 	}
    664  1.1  mrg       if (rewrite_uses_p (stmt))
    665  1.1  mrg 	bitmap_set_bit (interesting_blocks, bb->index);
    666  1.1  mrg       return;
    667  1.1  mrg     }
    668  1.1  mrg 
    669  1.1  mrg   /* If a variable is used before being set, then the variable is live
    670  1.1  mrg      across a block boundary, so mark it live-on-entry to BB.  */
    671  1.1  mrg   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
    672  1.1  mrg     {
    673  1.1  mrg       tree sym = USE_FROM_PTR (use_p);
    674  1.1  mrg       if (TREE_CODE (sym) == SSA_NAME)
    675  1.1  mrg 	continue;
    676  1.1  mrg       gcc_checking_assert (DECL_P (sym));
    677  1.1  mrg       if (!bitmap_bit_p (kills, DECL_UID (sym)))
    678  1.1  mrg 	set_livein_block (sym, bb);
    679  1.1  mrg       set_rewrite_uses (stmt, true);
    680  1.1  mrg     }
    681  1.1  mrg 
    682  1.1  mrg   /* Now process the defs.  Mark BB as the definition block and add
    683  1.1  mrg      each def to the set of killed symbols.  */
    684  1.1  mrg   FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
    685  1.1  mrg     {
    686  1.1  mrg       if (TREE_CODE (def) == SSA_NAME)
    687  1.1  mrg 	continue;
    688  1.1  mrg       gcc_checking_assert (DECL_P (def));
    689  1.1  mrg       set_def_block (def, bb, false);
    690  1.1  mrg       bitmap_set_bit (kills, DECL_UID (def));
    691  1.1  mrg       set_register_defs (stmt, true);
    692  1.1  mrg     }
    693  1.1  mrg 
    694  1.1  mrg   /* If we found the statement interesting then also mark the block BB
    695  1.1  mrg      as interesting.  */
    696  1.1  mrg   if (rewrite_uses_p (stmt) || register_defs_p (stmt))
    697  1.1  mrg     bitmap_set_bit (interesting_blocks, bb->index);
    698  1.1  mrg }
    699  1.1  mrg 
    700  1.1  mrg /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
    701  1.1  mrg    in the dfs numbering of the dominance tree.  */
    702  1.1  mrg 
    703  1.1  mrg struct dom_dfsnum
    704  1.1  mrg {
    705  1.1  mrg   /* Basic block whose index this entry corresponds to.  */
    706  1.1  mrg   unsigned bb_index;
    707  1.1  mrg 
    708  1.1  mrg   /* The dfs number of this node.  */
    709  1.1  mrg   unsigned dfs_num;
    710  1.1  mrg };
    711  1.1  mrg 
    712  1.1  mrg /* Compares two entries of type struct dom_dfsnum by dfs_num field.  Callback
    713  1.1  mrg    for qsort.  */
    714  1.1  mrg 
    715  1.1  mrg static int
    716  1.1  mrg cmp_dfsnum (const void *a, const void *b)
    717  1.1  mrg {
    718  1.1  mrg   const struct dom_dfsnum *const da = (const struct dom_dfsnum *) a;
    719  1.1  mrg   const struct dom_dfsnum *const db = (const struct dom_dfsnum *) b;
    720  1.1  mrg 
    721  1.1  mrg   return (int) da->dfs_num - (int) db->dfs_num;
    722  1.1  mrg }
    723  1.1  mrg 
    724  1.1  mrg /* Among the intervals starting at the N points specified in DEFS, find
    725  1.1  mrg    the one that contains S, and return its bb_index.  */
    726  1.1  mrg 
    727  1.1  mrg static unsigned
    728  1.1  mrg find_dfsnum_interval (struct dom_dfsnum *defs, unsigned n, unsigned s)
    729  1.1  mrg {
    730  1.1  mrg   unsigned f = 0, t = n, m;
    731  1.1  mrg 
    732  1.1  mrg   while (t > f + 1)
    733  1.1  mrg     {
    734  1.1  mrg       m = (f + t) / 2;
    735  1.1  mrg       if (defs[m].dfs_num <= s)
    736  1.1  mrg 	f = m;
    737  1.1  mrg       else
    738  1.1  mrg 	t = m;
    739  1.1  mrg     }
    740  1.1  mrg 
    741  1.1  mrg   return defs[f].bb_index;
    742  1.1  mrg }
    743  1.1  mrg 
    744  1.1  mrg /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
    745  1.1  mrg    KILLS is a bitmap of blocks where the value is defined before any use.  */
    746  1.1  mrg 
    747  1.1  mrg static void
    748  1.1  mrg prune_unused_phi_nodes (bitmap phis, bitmap kills, bitmap uses)
    749  1.1  mrg {
    750  1.1  mrg   bitmap_iterator bi;
    751  1.1  mrg   unsigned i, b, p, u, top;
    752  1.1  mrg   bitmap live_phis;
    753  1.1  mrg   basic_block def_bb, use_bb;
    754  1.1  mrg   edge e;
    755  1.1  mrg   edge_iterator ei;
    756  1.1  mrg   bitmap to_remove;
    757  1.1  mrg   struct dom_dfsnum *defs;
    758  1.1  mrg   unsigned n_defs, adef;
    759  1.1  mrg 
    760  1.1  mrg   if (bitmap_empty_p (uses))
    761  1.1  mrg     {
    762  1.1  mrg       bitmap_clear (phis);
    763  1.1  mrg       return;
    764  1.1  mrg     }
    765  1.1  mrg 
    766  1.1  mrg   /* The phi must dominate a use, or an argument of a live phi.  Also, we
    767  1.1  mrg      do not create any phi nodes in def blocks, unless they are also livein.  */
    768  1.1  mrg   to_remove = BITMAP_ALLOC (NULL);
    769  1.1  mrg   bitmap_and_compl (to_remove, kills, uses);
    770  1.1  mrg   bitmap_and_compl_into (phis, to_remove);
    771  1.1  mrg   if (bitmap_empty_p (phis))
    772  1.1  mrg     {
    773  1.1  mrg       BITMAP_FREE (to_remove);
    774  1.1  mrg       return;
    775  1.1  mrg     }
    776  1.1  mrg 
    777  1.1  mrg   /* We want to remove the unnecessary phi nodes, but we do not want to compute
    778  1.1  mrg      liveness information, as that may be linear in the size of CFG, and if
    779  1.1  mrg      there are lot of different variables to rewrite, this may lead to quadratic
    780  1.1  mrg      behavior.
    781  1.1  mrg 
    782  1.1  mrg      Instead, we basically emulate standard dce.  We put all uses to worklist,
    783  1.1  mrg      then for each of them find the nearest def that dominates them.  If this
    784  1.1  mrg      def is a phi node, we mark it live, and if it was not live before, we
    785  1.1  mrg      add the predecessors of its basic block to the worklist.
    786  1.1  mrg 
    787  1.1  mrg      To quickly locate the nearest def that dominates use, we use dfs numbering
    788  1.1  mrg      of the dominance tree (that is already available in order to speed up
    789  1.1  mrg      queries).  For each def, we have the interval given by the dfs number on
    790  1.1  mrg      entry to and on exit from the corresponding subtree in the dominance tree.
    791  1.1  mrg      The nearest dominator for a given use is the smallest of these intervals
    792  1.1  mrg      that contains entry and exit dfs numbers for the basic block with the use.
    793  1.1  mrg      If we store the bounds for all the uses to an array and sort it, we can
    794  1.1  mrg      locate the nearest dominating def in logarithmic time by binary search.*/
    795  1.1  mrg   bitmap_ior (to_remove, kills, phis);
    796  1.1  mrg   n_defs = bitmap_count_bits (to_remove);
    797  1.1  mrg   defs = XNEWVEC (struct dom_dfsnum, 2 * n_defs + 1);
    798  1.1  mrg   defs[0].bb_index = 1;
    799  1.1  mrg   defs[0].dfs_num = 0;
    800  1.1  mrg   adef = 1;
    801  1.1  mrg   EXECUTE_IF_SET_IN_BITMAP (to_remove, 0, i, bi)
    802  1.1  mrg     {
    803  1.1  mrg       def_bb = BASIC_BLOCK_FOR_FN (cfun, i);
    804  1.1  mrg       defs[adef].bb_index = i;
    805  1.1  mrg       defs[adef].dfs_num = bb_dom_dfs_in (CDI_DOMINATORS, def_bb);
    806  1.1  mrg       defs[adef + 1].bb_index = i;
    807  1.1  mrg       defs[adef + 1].dfs_num = bb_dom_dfs_out (CDI_DOMINATORS, def_bb);
    808  1.1  mrg       adef += 2;
    809  1.1  mrg     }
    810  1.1  mrg   BITMAP_FREE (to_remove);
    811  1.1  mrg   gcc_assert (adef == 2 * n_defs + 1);
    812  1.1  mrg   qsort (defs, adef, sizeof (struct dom_dfsnum), cmp_dfsnum);
    813  1.1  mrg   gcc_assert (defs[0].bb_index == 1);
    814  1.1  mrg 
    815  1.1  mrg   /* Now each DEFS entry contains the number of the basic block to that the
    816  1.1  mrg      dfs number corresponds.  Change them to the number of basic block that
    817  1.1  mrg      corresponds to the interval following the dfs number.  Also, for the
    818  1.1  mrg      dfs_out numbers, increase the dfs number by one (so that it corresponds
    819  1.1  mrg      to the start of the following interval, not to the end of the current
    820  1.1  mrg      one).  We use WORKLIST as a stack.  */
    821  1.1  mrg   auto_vec<int> worklist (n_defs + 1);
    822  1.1  mrg   worklist.quick_push (1);
    823  1.1  mrg   top = 1;
    824  1.1  mrg   n_defs = 1;
    825  1.1  mrg   for (i = 1; i < adef; i++)
    826  1.1  mrg     {
    827  1.1  mrg       b = defs[i].bb_index;
    828  1.1  mrg       if (b == top)
    829  1.1  mrg 	{
    830  1.1  mrg 	  /* This is a closing element.  Interval corresponding to the top
    831  1.1  mrg 	     of the stack after removing it follows.  */
    832  1.1  mrg 	  worklist.pop ();
    833  1.1  mrg 	  top = worklist[worklist.length () - 1];
    834  1.1  mrg 	  defs[n_defs].bb_index = top;
    835  1.1  mrg 	  defs[n_defs].dfs_num = defs[i].dfs_num + 1;
    836  1.1  mrg 	}
    837  1.1  mrg       else
    838  1.1  mrg 	{
    839  1.1  mrg 	  /* Opening element.  Nothing to do, just push it to the stack and move
    840  1.1  mrg 	     it to the correct position.  */
    841  1.1  mrg 	  defs[n_defs].bb_index = defs[i].bb_index;
    842  1.1  mrg 	  defs[n_defs].dfs_num = defs[i].dfs_num;
    843  1.1  mrg 	  worklist.quick_push (b);
    844  1.1  mrg 	  top = b;
    845  1.1  mrg 	}
    846  1.1  mrg 
    847  1.1  mrg       /* If this interval starts at the same point as the previous one, cancel
    848  1.1  mrg 	 the previous one.  */
    849  1.1  mrg       if (defs[n_defs].dfs_num == defs[n_defs - 1].dfs_num)
    850  1.1  mrg 	defs[n_defs - 1].bb_index = defs[n_defs].bb_index;
    851  1.1  mrg       else
    852  1.1  mrg 	n_defs++;
    853  1.1  mrg     }
    854  1.1  mrg   worklist.pop ();
    855  1.1  mrg   gcc_assert (worklist.is_empty ());
    856  1.1  mrg 
    857  1.1  mrg   /* Now process the uses.  */
    858  1.1  mrg   live_phis = BITMAP_ALLOC (NULL);
    859  1.1  mrg   EXECUTE_IF_SET_IN_BITMAP (uses, 0, i, bi)
    860  1.1  mrg     {
    861  1.1  mrg       worklist.safe_push (i);
    862  1.1  mrg     }
    863  1.1  mrg 
    864  1.1  mrg   while (!worklist.is_empty ())
    865  1.1  mrg     {
    866  1.1  mrg       b = worklist.pop ();
    867  1.1  mrg       if (b == ENTRY_BLOCK)
    868  1.1  mrg 	continue;
    869  1.1  mrg 
    870  1.1  mrg       /* If there is a phi node in USE_BB, it is made live.  Otherwise,
    871  1.1  mrg 	 find the def that dominates the immediate dominator of USE_BB
    872  1.1  mrg 	 (the kill in USE_BB does not dominate the use).  */
    873  1.1  mrg       if (bitmap_bit_p (phis, b))
    874  1.1  mrg 	p = b;
    875  1.1  mrg       else
    876  1.1  mrg 	{
    877  1.1  mrg 	  use_bb = get_immediate_dominator (CDI_DOMINATORS,
    878  1.1  mrg 					    BASIC_BLOCK_FOR_FN (cfun, b));
    879  1.1  mrg 	  p = find_dfsnum_interval (defs, n_defs,
    880  1.1  mrg 				    bb_dom_dfs_in (CDI_DOMINATORS, use_bb));
    881  1.1  mrg 	  if (!bitmap_bit_p (phis, p))
    882  1.1  mrg 	    continue;
    883  1.1  mrg 	}
    884  1.1  mrg 
    885  1.1  mrg       /* If the phi node is already live, there is nothing to do.  */
    886  1.1  mrg       if (!bitmap_set_bit (live_phis, p))
    887  1.1  mrg 	continue;
    888  1.1  mrg 
    889  1.1  mrg       /* Add the new uses to the worklist.  */
    890  1.1  mrg       def_bb = BASIC_BLOCK_FOR_FN (cfun, p);
    891  1.1  mrg       FOR_EACH_EDGE (e, ei, def_bb->preds)
    892  1.1  mrg 	{
    893  1.1  mrg 	  u = e->src->index;
    894  1.1  mrg 	  if (bitmap_bit_p (uses, u))
    895  1.1  mrg 	    continue;
    896  1.1  mrg 
    897  1.1  mrg 	  /* In case there is a kill directly in the use block, do not record
    898  1.1  mrg 	     the use (this is also necessary for correctness, as we assume that
    899  1.1  mrg 	     uses dominated by a def directly in their block have been filtered
    900  1.1  mrg 	     out before).  */
    901  1.1  mrg 	  if (bitmap_bit_p (kills, u))
    902  1.1  mrg 	    continue;
    903  1.1  mrg 
    904  1.1  mrg 	  bitmap_set_bit (uses, u);
    905  1.1  mrg 	  worklist.safe_push (u);
    906  1.1  mrg 	}
    907  1.1  mrg     }
    908  1.1  mrg 
    909  1.1  mrg   bitmap_copy (phis, live_phis);
    910  1.1  mrg   BITMAP_FREE (live_phis);
    911  1.1  mrg   free (defs);
    912  1.1  mrg }
    913  1.1  mrg 
    914  1.1  mrg /* Return the set of blocks where variable VAR is defined and the blocks
    915  1.1  mrg    where VAR is live on entry (livein).  Return NULL, if no entry is
    916  1.1  mrg    found in DEF_BLOCKS.  */
    917  1.1  mrg 
    918  1.1  mrg static inline def_blocks *
    919  1.1  mrg find_def_blocks_for (tree var)
    920  1.1  mrg {
    921  1.1  mrg   def_blocks *p = &get_common_info (var)->def_blocks;
    922  1.1  mrg   if (!p->def_blocks)
    923  1.1  mrg     return NULL;
    924  1.1  mrg   return p;
    925  1.1  mrg }
    926  1.1  mrg 
    927  1.1  mrg 
    928  1.1  mrg /* Marks phi node PHI in basic block BB for rewrite.  */
    929  1.1  mrg 
    930  1.1  mrg static void
    931  1.1  mrg mark_phi_for_rewrite (basic_block bb, gphi *phi)
    932  1.1  mrg {
    933  1.1  mrg   vec<gphi *> phis;
    934  1.1  mrg   unsigned n, idx = bb->index;
    935  1.1  mrg 
    936  1.1  mrg   if (rewrite_uses_p (phi))
    937  1.1  mrg     return;
    938  1.1  mrg 
    939  1.1  mrg   set_rewrite_uses (phi, true);
    940  1.1  mrg 
    941  1.1  mrg   if (!blocks_with_phis_to_rewrite)
    942  1.1  mrg     return;
    943  1.1  mrg 
    944  1.1  mrg   if (bitmap_set_bit (blocks_with_phis_to_rewrite, idx))
    945  1.1  mrg     {
    946  1.1  mrg       n = (unsigned) last_basic_block_for_fn (cfun) + 1;
    947  1.1  mrg       if (phis_to_rewrite.length () < n)
    948  1.1  mrg 	phis_to_rewrite.safe_grow_cleared (n, true);
    949  1.1  mrg 
    950  1.1  mrg       phis = phis_to_rewrite[idx];
    951  1.1  mrg       gcc_assert (!phis.exists ());
    952  1.1  mrg       phis.create (10);
    953  1.1  mrg     }
    954  1.1  mrg   else
    955  1.1  mrg     phis = phis_to_rewrite[idx];
    956  1.1  mrg 
    957  1.1  mrg   phis.safe_push (phi);
    958  1.1  mrg   phis_to_rewrite[idx] = phis;
    959  1.1  mrg }
    960  1.1  mrg 
    961  1.1  mrg /* Insert PHI nodes for variable VAR using the iterated dominance
    962  1.1  mrg    frontier given in PHI_INSERTION_POINTS.  If UPDATE_P is true, this
    963  1.1  mrg    function assumes that the caller is incrementally updating the
    964  1.1  mrg    existing SSA form, in which case VAR may be an SSA name instead of
    965  1.1  mrg    a symbol.
    966  1.1  mrg 
    967  1.1  mrg    PHI_INSERTION_POINTS is updated to reflect nodes that already had a
    968  1.1  mrg    PHI node for VAR.  On exit, only the nodes that received a PHI node
    969  1.1  mrg    for VAR will be present in PHI_INSERTION_POINTS.  */
    970  1.1  mrg 
    971  1.1  mrg static void
    972  1.1  mrg insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p)
    973  1.1  mrg {
    974  1.1  mrg   unsigned bb_index;
    975  1.1  mrg   edge e;
    976  1.1  mrg   gphi *phi;
    977  1.1  mrg   basic_block bb;
    978  1.1  mrg   bitmap_iterator bi;
    979  1.1  mrg   def_blocks *def_map = find_def_blocks_for (var);
    980  1.1  mrg 
    981  1.1  mrg   /* Remove the blocks where we already have PHI nodes for VAR.  */
    982  1.1  mrg   bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
    983  1.1  mrg 
    984  1.1  mrg   /* Remove obviously useless phi nodes.  */
    985  1.1  mrg   prune_unused_phi_nodes (phi_insertion_points, def_map->def_blocks,
    986  1.1  mrg 			  def_map->livein_blocks);
    987  1.1  mrg 
    988  1.1  mrg   /* And insert the PHI nodes.  */
    989  1.1  mrg   EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points, 0, bb_index, bi)
    990  1.1  mrg     {
    991  1.1  mrg       bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
    992  1.1  mrg       if (update_p)
    993  1.1  mrg 	mark_block_for_update (bb);
    994  1.1  mrg 
    995  1.1  mrg       if (dump_file && (dump_flags & TDF_DETAILS))
    996  1.1  mrg 	{
    997  1.1  mrg 	  fprintf (dump_file, "creating PHI node in block #%d for ", bb_index);
    998  1.1  mrg 	  print_generic_expr (dump_file, var, TDF_SLIM);
    999  1.1  mrg 	  fprintf (dump_file, "\n");
   1000  1.1  mrg 	}
   1001  1.1  mrg       phi = NULL;
   1002  1.1  mrg 
   1003  1.1  mrg       if (TREE_CODE (var) == SSA_NAME)
   1004  1.1  mrg 	{
   1005  1.1  mrg 	  /* If we are rewriting SSA names, create the LHS of the PHI
   1006  1.1  mrg 	     node by duplicating VAR.  This is useful in the case of
   1007  1.1  mrg 	     pointers, to also duplicate pointer attributes (alias
   1008  1.1  mrg 	     information, in particular).  */
   1009  1.1  mrg 	  edge_iterator ei;
   1010  1.1  mrg 	  tree new_lhs;
   1011  1.1  mrg 
   1012  1.1  mrg 	  gcc_checking_assert (update_p);
   1013  1.1  mrg 	  new_lhs = duplicate_ssa_name (var, NULL);
   1014  1.1  mrg 	  phi = create_phi_node (new_lhs, bb);
   1015  1.1  mrg 	  add_new_name_mapping (new_lhs, var);
   1016  1.1  mrg 
   1017  1.1  mrg 	  /* Add VAR to every argument slot of PHI.  We need VAR in
   1018  1.1  mrg 	     every argument so that rewrite_update_phi_arguments knows
   1019  1.1  mrg 	     which name is this PHI node replacing.  If VAR is a
   1020  1.1  mrg 	     symbol marked for renaming, this is not necessary, the
   1021  1.1  mrg 	     renamer will use the symbol on the LHS to get its
   1022  1.1  mrg 	     reaching definition.  */
   1023  1.1  mrg 	  FOR_EACH_EDGE (e, ei, bb->preds)
   1024  1.1  mrg 	    add_phi_arg (phi, var, e, UNKNOWN_LOCATION);
   1025  1.1  mrg 	}
   1026  1.1  mrg       else
   1027  1.1  mrg 	{
   1028  1.1  mrg 	  tree tracked_var;
   1029  1.1  mrg 
   1030  1.1  mrg 	  gcc_checking_assert (DECL_P (var));
   1031  1.1  mrg 	  phi = create_phi_node (var, bb);
   1032  1.1  mrg 
   1033  1.1  mrg 	  tracked_var = target_for_debug_bind (var);
   1034  1.1  mrg 	  if (tracked_var)
   1035  1.1  mrg 	    {
   1036  1.1  mrg 	      gimple *note = gimple_build_debug_bind (tracked_var,
   1037  1.1  mrg 						      PHI_RESULT (phi),
   1038  1.1  mrg 						     phi);
   1039  1.1  mrg 	      gimple_stmt_iterator si = gsi_after_labels (bb);
   1040  1.1  mrg 	      gsi_insert_before (&si, note, GSI_SAME_STMT);
   1041  1.1  mrg 	    }
   1042  1.1  mrg 	}
   1043  1.1  mrg 
   1044  1.1  mrg       /* Mark this PHI node as interesting for update_ssa.  */
   1045  1.1  mrg       set_register_defs (phi, true);
   1046  1.1  mrg       mark_phi_for_rewrite (bb, phi);
   1047  1.1  mrg     }
   1048  1.1  mrg }
   1049  1.1  mrg 
   1050  1.1  mrg /* Sort var_infos after DECL_UID of their var.  */
   1051  1.1  mrg 
   1052  1.1  mrg static int
   1053  1.1  mrg insert_phi_nodes_compare_var_infos (const void *a, const void *b)
   1054  1.1  mrg {
   1055  1.1  mrg   const var_info *defa = *(var_info * const *)a;
   1056  1.1  mrg   const var_info *defb = *(var_info * const *)b;
   1057  1.1  mrg   if (DECL_UID (defa->var) < DECL_UID (defb->var))
   1058  1.1  mrg     return -1;
   1059  1.1  mrg   else
   1060  1.1  mrg     return 1;
   1061  1.1  mrg }
   1062  1.1  mrg 
   1063  1.1  mrg /* Insert PHI nodes at the dominance frontier of blocks with variable
   1064  1.1  mrg    definitions.  DFS contains the dominance frontier information for
   1065  1.1  mrg    the flowgraph.  */
   1066  1.1  mrg 
   1067  1.1  mrg static void
   1068  1.1  mrg insert_phi_nodes (bitmap_head *dfs)
   1069  1.1  mrg {
   1070  1.1  mrg   hash_table<var_info_hasher>::iterator hi;
   1071  1.1  mrg   unsigned i;
   1072  1.1  mrg   var_info *info;
   1073  1.1  mrg 
   1074  1.1  mrg   /* When the gimplifier introduces SSA names it cannot easily avoid
   1075  1.1  mrg      situations where abnormal edges added by CFG construction break
   1076  1.1  mrg      the use-def dominance requirement.  For this case rewrite SSA
   1077  1.1  mrg      names with broken use-def dominance out-of-SSA and register them
   1078  1.1  mrg      for PHI insertion.  We only need to do this if abnormal edges
   1079  1.1  mrg      can appear in the function.  */
   1080  1.1  mrg   tree name;
   1081  1.1  mrg   if (cfun->calls_setjmp
   1082  1.1  mrg       || cfun->has_nonlocal_label)
   1083  1.1  mrg     FOR_EACH_SSA_NAME (i, name, cfun)
   1084  1.1  mrg       {
   1085  1.1  mrg 	gimple *def_stmt = SSA_NAME_DEF_STMT (name);
   1086  1.1  mrg 	if (SSA_NAME_IS_DEFAULT_DEF (name))
   1087  1.1  mrg 	  continue;
   1088  1.1  mrg 
   1089  1.1  mrg 	basic_block def_bb = gimple_bb (def_stmt);
   1090  1.1  mrg 	imm_use_iterator it;
   1091  1.1  mrg 	gimple *use_stmt;
   1092  1.1  mrg 	bool need_phis = false;
   1093  1.1  mrg 	FOR_EACH_IMM_USE_STMT (use_stmt, it, name)
   1094  1.1  mrg 	  {
   1095  1.1  mrg 	    basic_block use_bb = gimple_bb (use_stmt);
   1096  1.1  mrg 	    if (use_bb != def_bb
   1097  1.1  mrg 		&& ! dominated_by_p (CDI_DOMINATORS, use_bb, def_bb))
   1098  1.1  mrg 	      need_phis = true;
   1099  1.1  mrg 	  }
   1100  1.1  mrg 	if (need_phis)
   1101  1.1  mrg 	  {
   1102  1.1  mrg 	    tree var = create_tmp_reg (TREE_TYPE (name));
   1103  1.1  mrg 	    use_operand_p use_p;
   1104  1.1  mrg 	    FOR_EACH_IMM_USE_STMT (use_stmt, it, name)
   1105  1.1  mrg 	      {
   1106  1.1  mrg 		basic_block use_bb = gimple_bb (use_stmt);
   1107  1.1  mrg 		FOR_EACH_IMM_USE_ON_STMT (use_p, it)
   1108  1.1  mrg 		    SET_USE (use_p, var);
   1109  1.1  mrg 		update_stmt (use_stmt);
   1110  1.1  mrg 		set_livein_block (var, use_bb);
   1111  1.1  mrg 		set_rewrite_uses (use_stmt, true);
   1112  1.1  mrg 		bitmap_set_bit (interesting_blocks, use_bb->index);
   1113  1.1  mrg 	      }
   1114  1.1  mrg 	    def_operand_p def_p;
   1115  1.1  mrg 	    ssa_op_iter dit;
   1116  1.1  mrg 	    FOR_EACH_SSA_DEF_OPERAND (def_p, def_stmt, dit, SSA_OP_DEF)
   1117  1.1  mrg 	      if (DEF_FROM_PTR (def_p) == name)
   1118  1.1  mrg 		SET_DEF (def_p, var);
   1119  1.1  mrg 	    update_stmt (def_stmt);
   1120  1.1  mrg 	    set_def_block (var, def_bb, false);
   1121  1.1  mrg 	    set_register_defs (def_stmt, true);
   1122  1.1  mrg 	    bitmap_set_bit (interesting_blocks, def_bb->index);
   1123  1.1  mrg 	    release_ssa_name (name);
   1124  1.1  mrg 	  }
   1125  1.1  mrg       }
   1126  1.1  mrg 
   1127  1.1  mrg   auto_vec<var_info *> vars (var_infos->elements ());
   1128  1.1  mrg   FOR_EACH_HASH_TABLE_ELEMENT (*var_infos, info, var_info_p, hi)
   1129  1.1  mrg     if (info->info.need_phi_state != NEED_PHI_STATE_NO)
   1130  1.1  mrg       vars.quick_push (info);
   1131  1.1  mrg 
   1132  1.1  mrg   /* Do two stages to avoid code generation differences for UID
   1133  1.1  mrg      differences but no UID ordering differences.  */
   1134  1.1  mrg   vars.qsort (insert_phi_nodes_compare_var_infos);
   1135  1.1  mrg 
   1136  1.1  mrg   FOR_EACH_VEC_ELT (vars, i, info)
   1137  1.1  mrg     {
   1138  1.1  mrg       bitmap idf = compute_idf (info->info.def_blocks.def_blocks, dfs);
   1139  1.1  mrg       insert_phi_nodes_for (info->var, idf, false);
   1140  1.1  mrg       BITMAP_FREE (idf);
   1141  1.1  mrg     }
   1142  1.1  mrg }
   1143  1.1  mrg 
   1144  1.1  mrg 
   1145  1.1  mrg /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
   1146  1.1  mrg    register DEF (an SSA_NAME) to be a new definition for SYM.  */
   1147  1.1  mrg 
   1148  1.1  mrg static void
   1149  1.1  mrg register_new_def (tree def, tree sym)
   1150  1.1  mrg {
   1151  1.1  mrg   common_info *info = get_common_info (sym);
   1152  1.1  mrg   tree currdef;
   1153  1.1  mrg 
   1154  1.1  mrg   /* If this variable is set in a single basic block and all uses are
   1155  1.1  mrg      dominated by the set(s) in that single basic block, then there is
   1156  1.1  mrg      no reason to record anything for this variable in the block local
   1157  1.1  mrg      definition stacks.  Doing so just wastes time and memory.
   1158  1.1  mrg 
   1159  1.1  mrg      This is the same test to prune the set of variables which may
   1160  1.1  mrg      need PHI nodes.  So we just use that information since it's already
   1161  1.1  mrg      computed and available for us to use.  */
   1162  1.1  mrg   if (info->need_phi_state == NEED_PHI_STATE_NO)
   1163  1.1  mrg     {
   1164  1.1  mrg       info->current_def = def;
   1165  1.1  mrg       return;
   1166  1.1  mrg     }
   1167  1.1  mrg 
   1168  1.1  mrg   currdef = info->current_def;
   1169  1.1  mrg 
   1170  1.1  mrg   /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
   1171  1.1  mrg      SSA_NAME_VAR is not necessarily SYM.  In this case, also push SYM
   1172  1.1  mrg      in the stack so that we know which symbol is being defined by
   1173  1.1  mrg      this SSA name when we unwind the stack.  */
   1174  1.1  mrg   if (currdef && !is_gimple_reg (sym))
   1175  1.1  mrg     block_defs_stack.safe_push (sym);
   1176  1.1  mrg 
   1177  1.1  mrg   /* Push the current reaching definition into BLOCK_DEFS_STACK.  This
   1178  1.1  mrg      stack is later used by the dominator tree callbacks to restore
   1179  1.1  mrg      the reaching definitions for all the variables defined in the
   1180  1.1  mrg      block after a recursive visit to all its immediately dominated
   1181  1.1  mrg      blocks.  If there is no current reaching definition, then just
   1182  1.1  mrg      record the underlying _DECL node.  */
   1183  1.1  mrg   block_defs_stack.safe_push (currdef ? currdef : sym);
   1184  1.1  mrg 
   1185  1.1  mrg   /* Set the current reaching definition for SYM to be DEF.  */
   1186  1.1  mrg   info->current_def = def;
   1187  1.1  mrg }
   1188  1.1  mrg 
   1189  1.1  mrg 
   1190  1.1  mrg /* Perform a depth-first traversal of the dominator tree looking for
   1191  1.1  mrg    variables to rename.  BB is the block where to start searching.
   1192  1.1  mrg    Renaming is a five step process:
   1193  1.1  mrg 
   1194  1.1  mrg    1- Every definition made by PHI nodes at the start of the blocks is
   1195  1.1  mrg       registered as the current definition for the corresponding variable.
   1196  1.1  mrg 
   1197  1.1  mrg    2- Every statement in BB is rewritten.  USE and VUSE operands are
   1198  1.1  mrg       rewritten with their corresponding reaching definition.  DEF and
   1199  1.1  mrg       VDEF targets are registered as new definitions.
   1200  1.1  mrg 
   1201  1.1  mrg    3- All the PHI nodes in successor blocks of BB are visited.  The
   1202  1.1  mrg       argument corresponding to BB is replaced with its current reaching
   1203  1.1  mrg       definition.
   1204  1.1  mrg 
   1205  1.1  mrg    4- Recursively rewrite every dominator child block of BB.
   1206  1.1  mrg 
   1207  1.1  mrg    5- Restore (in reverse order) the current reaching definition for every
   1208  1.1  mrg       new definition introduced in this block.  This is done so that when
   1209  1.1  mrg       we return from the recursive call, all the current reaching
   1210  1.1  mrg       definitions are restored to the names that were valid in the
   1211  1.1  mrg       dominator parent of BB.  */
   1212  1.1  mrg 
   1213  1.1  mrg /* Return the current definition for variable VAR.  If none is found,
   1214  1.1  mrg    create a new SSA name to act as the zeroth definition for VAR.  */
   1215  1.1  mrg 
   1216  1.1  mrg static tree
   1217  1.1  mrg get_reaching_def (tree var)
   1218  1.1  mrg {
   1219  1.1  mrg   common_info *info = get_common_info (var);
   1220  1.1  mrg   tree currdef;
   1221  1.1  mrg 
   1222  1.1  mrg   /* Lookup the current reaching definition for VAR.  */
   1223  1.1  mrg   currdef = info->current_def;
   1224  1.1  mrg 
   1225  1.1  mrg   /* If there is no reaching definition for VAR, create and register a
   1226  1.1  mrg      default definition for it (if needed).  */
   1227  1.1  mrg   if (currdef == NULL_TREE)
   1228  1.1  mrg     {
   1229  1.1  mrg       tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
   1230  1.1  mrg       if (! sym)
   1231  1.1  mrg 	sym = create_tmp_reg (TREE_TYPE (var));
   1232  1.1  mrg       currdef = get_or_create_ssa_default_def (cfun, sym);
   1233  1.1  mrg     }
   1234  1.1  mrg 
   1235  1.1  mrg   /* Return the current reaching definition for VAR, or the default
   1236  1.1  mrg      definition, if we had to create one.  */
   1237  1.1  mrg   return currdef;
   1238  1.1  mrg }
   1239  1.1  mrg 
   1240  1.1  mrg 
   1241  1.1  mrg /* Helper function for rewrite_stmt.  Rewrite uses in a debug stmt.  */
   1242  1.1  mrg 
   1243  1.1  mrg static void
   1244  1.1  mrg rewrite_debug_stmt_uses (gimple *stmt)
   1245  1.1  mrg {
   1246  1.1  mrg   use_operand_p use_p;
   1247  1.1  mrg   ssa_op_iter iter;
   1248  1.1  mrg   bool update = false;
   1249  1.1  mrg 
   1250  1.1  mrg   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
   1251  1.1  mrg     {
   1252  1.1  mrg       tree var = USE_FROM_PTR (use_p), def;
   1253  1.1  mrg       common_info *info = get_common_info (var);
   1254  1.1  mrg       gcc_checking_assert (DECL_P (var));
   1255  1.1  mrg       def = info->current_def;
   1256  1.1  mrg       if (!def)
   1257  1.1  mrg 	{
   1258  1.1  mrg 	  if (TREE_CODE (var) == PARM_DECL
   1259  1.1  mrg 	      && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
   1260  1.1  mrg 	    {
   1261  1.1  mrg 	      gimple_stmt_iterator gsi
   1262  1.1  mrg 		=
   1263  1.1  mrg 	     gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
   1264  1.1  mrg 	      int lim;
   1265  1.1  mrg 	      /* Search a few source bind stmts at the start of first bb to
   1266  1.1  mrg 		 see if a DEBUG_EXPR_DECL can't be reused.  */
   1267  1.1  mrg 	      for (lim = 32;
   1268  1.1  mrg 		   !gsi_end_p (gsi) && lim > 0;
   1269  1.1  mrg 		   gsi_next (&gsi), lim--)
   1270  1.1  mrg 		{
   1271  1.1  mrg 		  gimple *gstmt = gsi_stmt (gsi);
   1272  1.1  mrg 		  if (!gimple_debug_source_bind_p (gstmt))
   1273  1.1  mrg 		    break;
   1274  1.1  mrg 		  if (gimple_debug_source_bind_get_value (gstmt) == var)
   1275  1.1  mrg 		    {
   1276  1.1  mrg 		      def = gimple_debug_source_bind_get_var (gstmt);
   1277  1.1  mrg 		      if (TREE_CODE (def) == DEBUG_EXPR_DECL)
   1278  1.1  mrg 			break;
   1279  1.1  mrg 		      else
   1280  1.1  mrg 			def = NULL_TREE;
   1281  1.1  mrg 		    }
   1282  1.1  mrg 		}
   1283  1.1  mrg 	      /* If not, add a new source bind stmt.  */
   1284  1.1  mrg 	      if (def == NULL_TREE)
   1285  1.1  mrg 		{
   1286  1.1  mrg 		  gimple *def_temp;
   1287  1.1  mrg 		  def = build_debug_expr_decl (TREE_TYPE (var));
   1288  1.1  mrg 		  /* FIXME: Is setting the mode really necessary? */
   1289  1.1  mrg 		  SET_DECL_MODE (def, DECL_MODE (var));
   1290  1.1  mrg 		  def_temp = gimple_build_debug_source_bind (def, var, NULL);
   1291  1.1  mrg 		  gsi =
   1292  1.1  mrg 		 gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
   1293  1.1  mrg 		  gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
   1294  1.1  mrg 		}
   1295  1.1  mrg 	      update = true;
   1296  1.1  mrg 	    }
   1297  1.1  mrg 	}
   1298  1.1  mrg       else
   1299  1.1  mrg 	{
   1300  1.1  mrg 	  /* Check if info->current_def can be trusted.  */
   1301  1.1  mrg 	  basic_block bb = gimple_bb (stmt);
   1302  1.1  mrg 	  basic_block def_bb
   1303  1.1  mrg 	      = SSA_NAME_IS_DEFAULT_DEF (def)
   1304  1.1  mrg 	      ? NULL : gimple_bb (SSA_NAME_DEF_STMT (def));
   1305  1.1  mrg 
   1306  1.1  mrg 	  /* If definition is in current bb, it is fine.  */
   1307  1.1  mrg 	  if (bb == def_bb)
   1308  1.1  mrg 	    ;
   1309  1.1  mrg 	  /* If definition bb doesn't dominate the current bb,
   1310  1.1  mrg 	     it can't be used.  */
   1311  1.1  mrg 	  else if (def_bb && !dominated_by_p (CDI_DOMINATORS, bb, def_bb))
   1312  1.1  mrg 	    def = NULL;
   1313  1.1  mrg 	  /* If there is just one definition and dominates the current
   1314  1.1  mrg 	     bb, it is fine.  */
   1315  1.1  mrg 	  else if (info->need_phi_state == NEED_PHI_STATE_NO)
   1316  1.1  mrg 	    ;
   1317  1.1  mrg 	  else
   1318  1.1  mrg 	    {
   1319  1.1  mrg 	      def_blocks *db_p = get_def_blocks_for (info);
   1320  1.1  mrg 
   1321  1.1  mrg 	      /* If there are some non-debug uses in the current bb,
   1322  1.1  mrg 		 it is fine.  */
   1323  1.1  mrg 	      if (bitmap_bit_p (db_p->livein_blocks, bb->index))
   1324  1.1  mrg 		;
   1325  1.1  mrg 	      /* Otherwise give up for now.  */
   1326  1.1  mrg 	      else
   1327  1.1  mrg 		def = NULL;
   1328  1.1  mrg 	    }
   1329  1.1  mrg 	}
   1330  1.1  mrg       if (def == NULL)
   1331  1.1  mrg 	{
   1332  1.1  mrg 	  gimple_debug_bind_reset_value (stmt);
   1333  1.1  mrg 	  update_stmt (stmt);
   1334  1.1  mrg 	  return;
   1335  1.1  mrg 	}
   1336  1.1  mrg       SET_USE (use_p, def);
   1337  1.1  mrg     }
   1338  1.1  mrg   if (update)
   1339  1.1  mrg     update_stmt (stmt);
   1340  1.1  mrg }
   1341  1.1  mrg 
   1342  1.1  mrg /* SSA Rewriting Step 2.  Rewrite every variable used in each statement in
   1343  1.1  mrg    the block with its immediate reaching definitions.  Update the current
   1344  1.1  mrg    definition of a variable when a new real or virtual definition is found.  */
   1345  1.1  mrg 
   1346  1.1  mrg static void
   1347  1.1  mrg rewrite_stmt (gimple_stmt_iterator *si)
   1348  1.1  mrg {
   1349  1.1  mrg   use_operand_p use_p;
   1350  1.1  mrg   def_operand_p def_p;
   1351  1.1  mrg   ssa_op_iter iter;
   1352  1.1  mrg   gimple *stmt = gsi_stmt (*si);
   1353  1.1  mrg 
   1354  1.1  mrg   /* If mark_def_sites decided that we don't need to rewrite this
   1355  1.1  mrg      statement, ignore it.  */
   1356  1.1  mrg   gcc_assert (blocks_to_update == NULL);
   1357  1.1  mrg   if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
   1358  1.1  mrg     return;
   1359  1.1  mrg 
   1360  1.1  mrg   if (dump_file && (dump_flags & TDF_DETAILS))
   1361  1.1  mrg     {
   1362  1.1  mrg       fprintf (dump_file, "Renaming statement ");
   1363  1.1  mrg       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
   1364  1.1  mrg       fprintf (dump_file, "\n");
   1365  1.1  mrg     }
   1366  1.1  mrg 
   1367  1.1  mrg   /* Step 1.  Rewrite USES in the statement.  */
   1368  1.1  mrg   if (rewrite_uses_p (stmt))
   1369  1.1  mrg     {
   1370  1.1  mrg       if (is_gimple_debug (stmt))
   1371  1.1  mrg 	rewrite_debug_stmt_uses (stmt);
   1372  1.1  mrg       else
   1373  1.1  mrg 	FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
   1374  1.1  mrg 	  {
   1375  1.1  mrg 	    tree var = USE_FROM_PTR (use_p);
   1376  1.1  mrg 	    if (TREE_CODE (var) == SSA_NAME)
   1377  1.1  mrg 	      continue;
   1378  1.1  mrg 	    gcc_checking_assert (DECL_P (var));
   1379  1.1  mrg 	    SET_USE (use_p, get_reaching_def (var));
   1380  1.1  mrg 	  }
   1381  1.1  mrg     }
   1382  1.1  mrg 
   1383  1.1  mrg   /* Step 2.  Register the statement's DEF operands.  */
   1384  1.1  mrg   if (register_defs_p (stmt))
   1385  1.1  mrg     FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
   1386  1.1  mrg       {
   1387  1.1  mrg 	tree var = DEF_FROM_PTR (def_p);
   1388  1.1  mrg 	tree name;
   1389  1.1  mrg 	tree tracked_var;
   1390  1.1  mrg 
   1391  1.1  mrg 	if (TREE_CODE (var) == SSA_NAME)
   1392  1.1  mrg 	  continue;
   1393  1.1  mrg 	gcc_checking_assert (DECL_P (var));
   1394  1.1  mrg 
   1395  1.1  mrg 	if (gimple_clobber_p (stmt)
   1396  1.1  mrg 	    && is_gimple_reg (var))
   1397  1.1  mrg 	  {
   1398  1.1  mrg 	    /* If we rewrite a DECL into SSA form then drop its
   1399  1.1  mrg 	       clobber stmts and replace uses with a new default def.  */
   1400  1.1  mrg 	    gcc_checking_assert (VAR_P (var) && !gimple_vdef (stmt));
   1401  1.1  mrg 	    gsi_replace (si, gimple_build_nop (), true);
   1402  1.1  mrg 	    register_new_def (get_or_create_ssa_default_def (cfun, var), var);
   1403  1.1  mrg 	    break;
   1404  1.1  mrg 	  }
   1405  1.1  mrg 
   1406  1.1  mrg 	name = make_ssa_name (var, stmt);
   1407  1.1  mrg 	SET_DEF (def_p, name);
   1408  1.1  mrg 	register_new_def (DEF_FROM_PTR (def_p), var);
   1409  1.1  mrg 
   1410  1.1  mrg 	/* Do not insert debug stmts if the stmt ends the BB.  */
   1411  1.1  mrg 	if (stmt_ends_bb_p (stmt))
   1412  1.1  mrg 	  continue;
   1413  1.1  mrg 
   1414  1.1  mrg 	tracked_var = target_for_debug_bind (var);
   1415  1.1  mrg 	if (tracked_var)
   1416  1.1  mrg 	  {
   1417  1.1  mrg 	    gimple *note = gimple_build_debug_bind (tracked_var, name, stmt);
   1418  1.1  mrg 	    gsi_insert_after (si, note, GSI_SAME_STMT);
   1419  1.1  mrg 	  }
   1420  1.1  mrg       }
   1421  1.1  mrg }
   1422  1.1  mrg 
   1423  1.1  mrg 
   1424  1.1  mrg /* SSA Rewriting Step 3.  Visit all the successor blocks of BB looking for
   1425  1.1  mrg    PHI nodes.  For every PHI node found, add a new argument containing the
   1426  1.1  mrg    current reaching definition for the variable and the edge through which
   1427  1.1  mrg    that definition is reaching the PHI node.  */
   1428  1.1  mrg 
   1429  1.1  mrg static void
   1430  1.1  mrg rewrite_add_phi_arguments (basic_block bb)
   1431  1.1  mrg {
   1432  1.1  mrg   edge e;
   1433  1.1  mrg   edge_iterator ei;
   1434  1.1  mrg 
   1435  1.1  mrg   FOR_EACH_EDGE (e, ei, bb->succs)
   1436  1.1  mrg     {
   1437  1.1  mrg       gphi *phi;
   1438  1.1  mrg       gphi_iterator gsi;
   1439  1.1  mrg 
   1440  1.1  mrg       for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);
   1441  1.1  mrg 	   gsi_next (&gsi))
   1442  1.1  mrg 	{
   1443  1.1  mrg 	  tree currdef, res;
   1444  1.1  mrg 	  location_t loc;
   1445  1.1  mrg 
   1446  1.1  mrg 	  phi = gsi.phi ();
   1447  1.1  mrg 	  res = gimple_phi_result (phi);
   1448  1.1  mrg 	  currdef = get_reaching_def (SSA_NAME_VAR (res));
   1449  1.1  mrg 	  /* Virtual operand PHI args do not need a location.  */
   1450  1.1  mrg 	  if (virtual_operand_p (res))
   1451  1.1  mrg 	    loc = UNKNOWN_LOCATION;
   1452  1.1  mrg 	  else
   1453  1.1  mrg 	    loc = gimple_location (SSA_NAME_DEF_STMT (currdef));
   1454  1.1  mrg 	  add_phi_arg (phi, currdef, e, loc);
   1455  1.1  mrg 	}
   1456  1.1  mrg     }
   1457  1.1  mrg }
   1458  1.1  mrg 
   1459  1.1  mrg class rewrite_dom_walker : public dom_walker
   1460  1.1  mrg {
   1461  1.1  mrg public:
   1462  1.1  mrg   rewrite_dom_walker (cdi_direction direction)
   1463  1.1  mrg     : dom_walker (direction, ALL_BLOCKS, NULL) {}
   1464  1.1  mrg 
   1465  1.1  mrg   virtual edge before_dom_children (basic_block);
   1466  1.1  mrg   virtual void after_dom_children (basic_block);
   1467  1.1  mrg };
   1468  1.1  mrg 
   1469  1.1  mrg /* SSA Rewriting Step 1.  Initialization, create a block local stack
   1470  1.1  mrg    of reaching definitions for new SSA names produced in this block
   1471  1.1  mrg    (BLOCK_DEFS).  Register new definitions for every PHI node in the
   1472  1.1  mrg    block.  */
   1473  1.1  mrg 
   1474  1.1  mrg edge
   1475  1.1  mrg rewrite_dom_walker::before_dom_children (basic_block bb)
   1476  1.1  mrg {
   1477  1.1  mrg   if (dump_file && (dump_flags & TDF_DETAILS))
   1478  1.1  mrg     fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
   1479  1.1  mrg 
   1480  1.1  mrg   /* Mark the unwind point for this block.  */
   1481  1.1  mrg   block_defs_stack.safe_push (NULL_TREE);
   1482  1.1  mrg 
   1483  1.1  mrg   /* Step 1.  Register new definitions for every PHI node in the block.
   1484  1.1  mrg      Conceptually, all the PHI nodes are executed in parallel and each PHI
   1485  1.1  mrg      node introduces a new version for the associated variable.  */
   1486  1.1  mrg   for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
   1487  1.1  mrg        gsi_next (&gsi))
   1488  1.1  mrg     {
   1489  1.1  mrg       tree result = gimple_phi_result (gsi_stmt (gsi));
   1490  1.1  mrg       register_new_def (result, SSA_NAME_VAR (result));
   1491  1.1  mrg     }
   1492  1.1  mrg 
   1493  1.1  mrg   /* Step 2.  Rewrite every variable used in each statement in the block
   1494  1.1  mrg      with its immediate reaching definitions.  Update the current definition
   1495  1.1  mrg      of a variable when a new real or virtual definition is found.  */
   1496  1.1  mrg   if (bitmap_bit_p (interesting_blocks, bb->index))
   1497  1.1  mrg     for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
   1498  1.1  mrg 	 gsi_next (&gsi))
   1499  1.1  mrg       rewrite_stmt (&gsi);
   1500  1.1  mrg 
   1501  1.1  mrg   /* Step 3.  Visit all the successor blocks of BB looking for PHI nodes.
   1502  1.1  mrg      For every PHI node found, add a new argument containing the current
   1503  1.1  mrg      reaching definition for the variable and the edge through which that
   1504  1.1  mrg      definition is reaching the PHI node.  */
   1505  1.1  mrg   rewrite_add_phi_arguments (bb);
   1506  1.1  mrg 
   1507  1.1  mrg   return NULL;
   1508  1.1  mrg }
   1509  1.1  mrg 
   1510  1.1  mrg 
   1511  1.1  mrg 
   1512  1.1  mrg /* Called after visiting all the statements in basic block BB and all
   1513  1.1  mrg    of its dominator children.  Restore CURRDEFS to its original value.  */
   1514  1.1  mrg 
   1515  1.1  mrg void
   1516  1.1  mrg rewrite_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
   1517  1.1  mrg {
   1518  1.1  mrg   /* Restore CURRDEFS to its original state.  */
   1519  1.1  mrg   while (block_defs_stack.length () > 0)
   1520  1.1  mrg     {
   1521  1.1  mrg       tree tmp = block_defs_stack.pop ();
   1522  1.1  mrg       tree saved_def, var;
   1523  1.1  mrg 
   1524  1.1  mrg       if (tmp == NULL_TREE)
   1525  1.1  mrg 	break;
   1526  1.1  mrg 
   1527  1.1  mrg       if (TREE_CODE (tmp) == SSA_NAME)
   1528  1.1  mrg 	{
   1529  1.1  mrg 	  /* If we recorded an SSA_NAME, then make the SSA_NAME the
   1530  1.1  mrg 	     current definition of its underlying variable.  Note that
   1531  1.1  mrg 	     if the SSA_NAME is not for a GIMPLE register, the symbol
   1532  1.1  mrg 	     being defined is stored in the next slot in the stack.
   1533  1.1  mrg 	     This mechanism is needed because an SSA name for a
   1534  1.1  mrg 	     non-register symbol may be the definition for more than
   1535  1.1  mrg 	     one symbol (e.g., SFTs, aliased variables, etc).  */
   1536  1.1  mrg 	  saved_def = tmp;
   1537  1.1  mrg 	  var = SSA_NAME_VAR (saved_def);
   1538  1.1  mrg 	  if (!is_gimple_reg (var))
   1539  1.1  mrg 	    var = block_defs_stack.pop ();
   1540  1.1  mrg 	}
   1541  1.1  mrg       else
   1542  1.1  mrg 	{
   1543  1.1  mrg 	  /* If we recorded anything else, it must have been a _DECL
   1544  1.1  mrg 	     node and its current reaching definition must have been
   1545  1.1  mrg 	     NULL.  */
   1546  1.1  mrg 	  saved_def = NULL;
   1547  1.1  mrg 	  var = tmp;
   1548  1.1  mrg 	}
   1549  1.1  mrg 
   1550  1.1  mrg       get_common_info (var)->current_def = saved_def;
   1551  1.1  mrg     }
   1552  1.1  mrg }
   1553  1.1  mrg 
   1554  1.1  mrg 
   1555  1.1  mrg /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE.  */
   1556  1.1  mrg 
   1557  1.1  mrg DEBUG_FUNCTION void
   1558  1.1  mrg debug_decl_set (bitmap set)
   1559  1.1  mrg {
   1560  1.1  mrg   dump_decl_set (stderr, set);
   1561  1.1  mrg   fprintf (stderr, "\n");
   1562  1.1  mrg }
   1563  1.1  mrg 
   1564  1.1  mrg 
   1565  1.1  mrg /* Dump the renaming stack (block_defs_stack) to FILE.  Traverse the
   1566  1.1  mrg    stack up to a maximum of N levels.  If N is -1, the whole stack is
   1567  1.1  mrg    dumped.  New levels are created when the dominator tree traversal
   1568  1.1  mrg    used for renaming enters a new sub-tree.  */
   1569  1.1  mrg 
   1570  1.1  mrg void
   1571  1.1  mrg dump_defs_stack (FILE *file, int n)
   1572  1.1  mrg {
   1573  1.1  mrg   int i, j;
   1574  1.1  mrg 
   1575  1.1  mrg   fprintf (file, "\n\nRenaming stack");
   1576  1.1  mrg   if (n > 0)
   1577  1.1  mrg     fprintf (file, " (up to %d levels)", n);
   1578  1.1  mrg   fprintf (file, "\n\n");
   1579  1.1  mrg 
   1580  1.1  mrg   i = 1;
   1581  1.1  mrg   fprintf (file, "Level %d (current level)\n", i);
   1582  1.1  mrg   for (j = (int) block_defs_stack.length () - 1; j >= 0; j--)
   1583  1.1  mrg     {
   1584  1.1  mrg       tree name, var;
   1585  1.1  mrg 
   1586  1.1  mrg       name = block_defs_stack[j];
   1587  1.1  mrg       if (name == NULL_TREE)
   1588  1.1  mrg 	{
   1589  1.1  mrg 	  i++;
   1590  1.1  mrg 	  if (n > 0 && i > n)
   1591  1.1  mrg 	    break;
   1592  1.1  mrg 	  fprintf (file, "\nLevel %d\n", i);
   1593  1.1  mrg 	  continue;
   1594  1.1  mrg 	}
   1595  1.1  mrg 
   1596  1.1  mrg       if (DECL_P (name))
   1597  1.1  mrg 	{
   1598  1.1  mrg 	  var = name;
   1599  1.1  mrg 	  name = NULL_TREE;
   1600  1.1  mrg 	}
   1601  1.1  mrg       else
   1602  1.1  mrg 	{
   1603  1.1  mrg 	  var = SSA_NAME_VAR (name);
   1604  1.1  mrg 	  if (!is_gimple_reg (var))
   1605  1.1  mrg 	    {
   1606  1.1  mrg 	      j--;
   1607  1.1  mrg 	      var = block_defs_stack[j];
   1608  1.1  mrg 	    }
   1609  1.1  mrg 	}
   1610  1.1  mrg 
   1611  1.1  mrg       fprintf (file, "    Previous CURRDEF (");
   1612  1.1  mrg       print_generic_expr (file, var);
   1613  1.1  mrg       fprintf (file, ") = ");
   1614  1.1  mrg       if (name)
   1615  1.1  mrg 	print_generic_expr (file, name);
   1616  1.1  mrg       else
   1617  1.1  mrg 	fprintf (file, "<NIL>");
   1618  1.1  mrg       fprintf (file, "\n");
   1619  1.1  mrg     }
   1620  1.1  mrg }
   1621  1.1  mrg 
   1622  1.1  mrg 
   1623  1.1  mrg /* Dump the renaming stack (block_defs_stack) to stderr.  Traverse the
   1624  1.1  mrg    stack up to a maximum of N levels.  If N is -1, the whole stack is
   1625  1.1  mrg    dumped.  New levels are created when the dominator tree traversal
   1626  1.1  mrg    used for renaming enters a new sub-tree.  */
   1627  1.1  mrg 
   1628  1.1  mrg DEBUG_FUNCTION void
   1629  1.1  mrg debug_defs_stack (int n)
   1630  1.1  mrg {
   1631  1.1  mrg   dump_defs_stack (stderr, n);
   1632  1.1  mrg }
   1633  1.1  mrg 
   1634  1.1  mrg 
   1635  1.1  mrg /* Dump the current reaching definition of every symbol to FILE.  */
   1636  1.1  mrg 
   1637  1.1  mrg void
   1638  1.1  mrg dump_currdefs (FILE *file)
   1639  1.1  mrg {
   1640  1.1  mrg   if (symbols_to_rename.is_empty ())
   1641  1.1  mrg     return;
   1642  1.1  mrg 
   1643  1.1  mrg   fprintf (file, "\n\nCurrent reaching definitions\n\n");
   1644  1.1  mrg   for (tree var : symbols_to_rename)
   1645  1.1  mrg     {
   1646  1.1  mrg       common_info *info = get_common_info (var);
   1647  1.1  mrg       fprintf (file, "CURRDEF (");
   1648  1.1  mrg       print_generic_expr (file, var);
   1649  1.1  mrg       fprintf (file, ") = ");
   1650  1.1  mrg       if (info->current_def)
   1651  1.1  mrg 	print_generic_expr (file, info->current_def);
   1652  1.1  mrg       else
   1653  1.1  mrg 	fprintf (file, "<NIL>");
   1654  1.1  mrg       fprintf (file, "\n");
   1655  1.1  mrg     }
   1656  1.1  mrg }
   1657  1.1  mrg 
   1658  1.1  mrg 
   1659  1.1  mrg /* Dump the current reaching definition of every symbol to stderr.  */
   1660  1.1  mrg 
   1661  1.1  mrg DEBUG_FUNCTION void
   1662  1.1  mrg debug_currdefs (void)
   1663  1.1  mrg {
   1664  1.1  mrg   dump_currdefs (stderr);
   1665  1.1  mrg }
   1666  1.1  mrg 
   1667  1.1  mrg 
   1668  1.1  mrg /* Dump SSA information to FILE.  */
   1669  1.1  mrg 
   1670  1.1  mrg void
   1671  1.1  mrg dump_tree_ssa (FILE *file)
   1672  1.1  mrg {
   1673  1.1  mrg   const char *funcname
   1674  1.1  mrg     = lang_hooks.decl_printable_name (current_function_decl, 2);
   1675  1.1  mrg 
   1676  1.1  mrg   fprintf (file, "SSA renaming information for %s\n\n", funcname);
   1677  1.1  mrg 
   1678  1.1  mrg   dump_var_infos (file);
   1679  1.1  mrg   dump_defs_stack (file, -1);
   1680  1.1  mrg   dump_currdefs (file);
   1681  1.1  mrg   dump_tree_ssa_stats (file);
   1682  1.1  mrg }
   1683  1.1  mrg 
   1684  1.1  mrg 
   1685  1.1  mrg /* Dump SSA information to stderr.  */
   1686  1.1  mrg 
   1687  1.1  mrg DEBUG_FUNCTION void
   1688  1.1  mrg debug_tree_ssa (void)
   1689  1.1  mrg {
   1690  1.1  mrg   dump_tree_ssa (stderr);
   1691  1.1  mrg }
   1692  1.1  mrg 
   1693  1.1  mrg 
   1694  1.1  mrg /* Dump statistics for the hash table HTAB.  */
   1695  1.1  mrg 
   1696  1.1  mrg static void
   1697  1.1  mrg htab_statistics (FILE *file, const hash_table<var_info_hasher> &htab)
   1698  1.1  mrg {
   1699  1.1  mrg   fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
   1700  1.1  mrg 	   (long) htab.size (),
   1701  1.1  mrg 	   (long) htab.elements (),
   1702  1.1  mrg 	   htab.collisions ());
   1703  1.1  mrg }
   1704  1.1  mrg 
   1705  1.1  mrg 
   1706  1.1  mrg /* Dump SSA statistics on FILE.  */
   1707  1.1  mrg 
   1708  1.1  mrg void
   1709  1.1  mrg dump_tree_ssa_stats (FILE *file)
   1710  1.1  mrg {
   1711  1.1  mrg   if (var_infos)
   1712  1.1  mrg     {
   1713  1.1  mrg       fprintf (file, "\nHash table statistics:\n");
   1714  1.1  mrg       fprintf (file, "    var_infos:   ");
   1715  1.1  mrg       htab_statistics (file, *var_infos);
   1716  1.1  mrg       fprintf (file, "\n");
   1717  1.1  mrg     }
   1718  1.1  mrg }
   1719  1.1  mrg 
   1720  1.1  mrg 
   1721  1.1  mrg /* Dump SSA statistics on stderr.  */
   1722  1.1  mrg 
   1723  1.1  mrg DEBUG_FUNCTION void
   1724  1.1  mrg debug_tree_ssa_stats (void)
   1725  1.1  mrg {
   1726  1.1  mrg   dump_tree_ssa_stats (stderr);
   1727  1.1  mrg }
   1728  1.1  mrg 
   1729  1.1  mrg 
   1730  1.1  mrg /* Callback for htab_traverse to dump the VAR_INFOS hash table.  */
   1731  1.1  mrg 
   1732  1.1  mrg int
   1733  1.1  mrg debug_var_infos_r (var_info **slot, FILE *file)
   1734  1.1  mrg {
   1735  1.1  mrg   var_info *info = *slot;
   1736  1.1  mrg 
   1737  1.1  mrg   fprintf (file, "VAR: ");
   1738  1.1  mrg   print_generic_expr (file, info->var, dump_flags);
   1739  1.1  mrg   bitmap_print (file, info->info.def_blocks.def_blocks,
   1740  1.1  mrg 		", DEF_BLOCKS: { ", "}");
   1741  1.1  mrg   bitmap_print (file, info->info.def_blocks.livein_blocks,
   1742  1.1  mrg 		", LIVEIN_BLOCKS: { ", "}");
   1743  1.1  mrg   bitmap_print (file, info->info.def_blocks.phi_blocks,
   1744  1.1  mrg 		", PHI_BLOCKS: { ", "}\n");
   1745  1.1  mrg 
   1746  1.1  mrg   return 1;
   1747  1.1  mrg }
   1748  1.1  mrg 
   1749  1.1  mrg 
   1750  1.1  mrg /* Dump the VAR_INFOS hash table on FILE.  */
   1751  1.1  mrg 
   1752  1.1  mrg void
   1753  1.1  mrg dump_var_infos (FILE *file)
   1754  1.1  mrg {
   1755  1.1  mrg   fprintf (file, "\n\nDefinition and live-in blocks:\n\n");
   1756  1.1  mrg   if (var_infos)
   1757  1.1  mrg     var_infos->traverse <FILE *, debug_var_infos_r> (file);
   1758  1.1  mrg }
   1759  1.1  mrg 
   1760  1.1  mrg 
   1761  1.1  mrg /* Dump the VAR_INFOS hash table on stderr.  */
   1762  1.1  mrg 
   1763  1.1  mrg DEBUG_FUNCTION void
   1764  1.1  mrg debug_var_infos (void)
   1765  1.1  mrg {
   1766  1.1  mrg   dump_var_infos (stderr);
   1767  1.1  mrg }
   1768  1.1  mrg 
   1769  1.1  mrg 
   1770  1.1  mrg /* Register NEW_NAME to be the new reaching definition for OLD_NAME.  */
   1771  1.1  mrg 
   1772  1.1  mrg static inline void
   1773  1.1  mrg register_new_update_single (tree new_name, tree old_name)
   1774  1.1  mrg {
   1775  1.1  mrg   common_info *info = get_common_info (old_name);
   1776  1.1  mrg   tree currdef = info->current_def;
   1777  1.1  mrg 
   1778  1.1  mrg   /* Push the current reaching definition into BLOCK_DEFS_STACK.
   1779  1.1  mrg      This stack is later used by the dominator tree callbacks to
   1780  1.1  mrg      restore the reaching definitions for all the variables
   1781  1.1  mrg      defined in the block after a recursive visit to all its
   1782  1.1  mrg      immediately dominated blocks.  */
   1783  1.1  mrg   block_defs_stack.reserve (2);
   1784  1.1  mrg   block_defs_stack.quick_push (currdef);
   1785  1.1  mrg   block_defs_stack.quick_push (old_name);
   1786  1.1  mrg 
   1787  1.1  mrg   /* Set the current reaching definition for OLD_NAME to be
   1788  1.1  mrg      NEW_NAME.  */
   1789  1.1  mrg   info->current_def = new_name;
   1790  1.1  mrg }
   1791  1.1  mrg 
   1792  1.1  mrg 
   1793  1.1  mrg /* Register NEW_NAME to be the new reaching definition for all the
   1794  1.1  mrg    names in OLD_NAMES.  Used by the incremental SSA update routines to
   1795  1.1  mrg    replace old SSA names with new ones.  */
   1796  1.1  mrg 
   1797  1.1  mrg static inline void
   1798  1.1  mrg register_new_update_set (tree new_name, bitmap old_names)
   1799  1.1  mrg {
   1800  1.1  mrg   bitmap_iterator bi;
   1801  1.1  mrg   unsigned i;
   1802  1.1  mrg 
   1803  1.1  mrg   EXECUTE_IF_SET_IN_BITMAP (old_names, 0, i, bi)
   1804  1.1  mrg     register_new_update_single (new_name, ssa_name (i));
   1805  1.1  mrg }
   1806  1.1  mrg 
   1807  1.1  mrg 
   1808  1.1  mrg 
   1809  1.1  mrg /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
   1810  1.1  mrg    it is a symbol marked for renaming, replace it with USE_P's current
   1811  1.1  mrg    reaching definition.  */
   1812  1.1  mrg 
   1813  1.1  mrg static inline void
   1814  1.1  mrg maybe_replace_use (use_operand_p use_p)
   1815  1.1  mrg {
   1816  1.1  mrg   tree rdef = NULL_TREE;
   1817  1.1  mrg   tree use = USE_FROM_PTR (use_p);
   1818  1.1  mrg   tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
   1819  1.1  mrg 
   1820  1.1  mrg   if (marked_for_renaming (sym))
   1821  1.1  mrg     rdef = get_reaching_def (sym);
   1822  1.1  mrg   else if (is_old_name (use))
   1823  1.1  mrg     rdef = get_reaching_def (use);
   1824  1.1  mrg 
   1825  1.1  mrg   if (rdef && rdef != use)
   1826  1.1  mrg     SET_USE (use_p, rdef);
   1827  1.1  mrg }
   1828  1.1  mrg 
   1829  1.1  mrg 
   1830  1.1  mrg /* Same as maybe_replace_use, but without introducing default stmts,
   1831  1.1  mrg    returning false to indicate a need to do so.  */
   1832  1.1  mrg 
   1833  1.1  mrg static inline bool
   1834  1.1  mrg maybe_replace_use_in_debug_stmt (use_operand_p use_p)
   1835  1.1  mrg {
   1836  1.1  mrg   tree rdef = NULL_TREE;
   1837  1.1  mrg   tree use = USE_FROM_PTR (use_p);
   1838  1.1  mrg   tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
   1839  1.1  mrg 
   1840  1.1  mrg   if (marked_for_renaming (sym))
   1841  1.1  mrg     rdef = get_var_info (sym)->info.current_def;
   1842  1.1  mrg   else if (is_old_name (use))
   1843  1.1  mrg     {
   1844  1.1  mrg       rdef = get_ssa_name_ann (use)->info.current_def;
   1845  1.1  mrg       /* We can't assume that, if there's no current definition, the
   1846  1.1  mrg 	 default one should be used.  It could be the case that we've
   1847  1.1  mrg 	 rearranged blocks so that the earlier definition no longer
   1848  1.1  mrg 	 dominates the use.  */
   1849  1.1  mrg       if (!rdef && SSA_NAME_IS_DEFAULT_DEF (use))
   1850  1.1  mrg 	rdef = use;
   1851  1.1  mrg     }
   1852  1.1  mrg   else
   1853  1.1  mrg     rdef = use;
   1854  1.1  mrg 
   1855  1.1  mrg   if (rdef && rdef != use)
   1856  1.1  mrg     SET_USE (use_p, rdef);
   1857  1.1  mrg 
   1858  1.1  mrg   return rdef != NULL_TREE;
   1859  1.1  mrg }
   1860  1.1  mrg 
   1861  1.1  mrg 
   1862  1.1  mrg /* If DEF has x_5 = ASAN_POISON () as its current def, add
   1863  1.1  mrg    ASAN_POISON_USE (x_5) stmt before GSI to denote the stmt writes into
   1864  1.1  mrg    a poisoned (out of scope) variable.  */
   1865  1.1  mrg 
   1866  1.1  mrg static void
   1867  1.1  mrg maybe_add_asan_poison_write (tree def, gimple_stmt_iterator *gsi)
   1868  1.1  mrg {
   1869  1.1  mrg   tree cdef = get_current_def (def);
   1870  1.1  mrg   if (cdef != NULL
   1871  1.1  mrg       && TREE_CODE (cdef) == SSA_NAME
   1872  1.1  mrg       && gimple_call_internal_p (SSA_NAME_DEF_STMT (cdef), IFN_ASAN_POISON))
   1873  1.1  mrg     {
   1874  1.1  mrg       gcall *call
   1875  1.1  mrg 	= gimple_build_call_internal (IFN_ASAN_POISON_USE, 1, cdef);
   1876  1.1  mrg       gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
   1877  1.1  mrg       gsi_insert_before (gsi, call, GSI_SAME_STMT);
   1878  1.1  mrg     }
   1879  1.1  mrg }
   1880  1.1  mrg 
   1881  1.1  mrg 
   1882  1.1  mrg /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
   1883  1.1  mrg    or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
   1884  1.1  mrg    register it as the current definition for the names replaced by
   1885  1.1  mrg    DEF_P.  Returns whether the statement should be removed.  */
   1886  1.1  mrg 
   1887  1.1  mrg static inline bool
   1888  1.1  mrg maybe_register_def (def_operand_p def_p, gimple *stmt,
   1889  1.1  mrg 		    gimple_stmt_iterator gsi)
   1890  1.1  mrg {
   1891  1.1  mrg   tree def = DEF_FROM_PTR (def_p);
   1892  1.1  mrg   tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
   1893  1.1  mrg   bool to_delete = false;
   1894  1.1  mrg 
   1895  1.1  mrg   /* If DEF is a naked symbol that needs renaming, create a new
   1896  1.1  mrg      name for it.  */
   1897  1.1  mrg   if (marked_for_renaming (sym))
   1898  1.1  mrg     {
   1899  1.1  mrg       if (DECL_P (def))
   1900  1.1  mrg 	{
   1901  1.1  mrg 	  if (gimple_clobber_p (stmt) && is_gimple_reg (sym))
   1902  1.1  mrg 	    {
   1903  1.1  mrg 	      gcc_checking_assert (VAR_P (sym));
   1904  1.1  mrg 	      /* Replace clobber stmts with a default def. This new use of a
   1905  1.1  mrg 		 default definition may make it look like SSA_NAMEs have
   1906  1.1  mrg 		 conflicting lifetimes, so we need special code to let them
   1907  1.1  mrg 		 coalesce properly.  */
   1908  1.1  mrg 	      to_delete = true;
   1909  1.1  mrg 	      def = get_or_create_ssa_default_def (cfun, sym);
   1910  1.1  mrg 	    }
   1911  1.1  mrg 	  else
   1912  1.1  mrg 	    {
   1913  1.1  mrg 	      if (asan_sanitize_use_after_scope ())
   1914  1.1  mrg 		maybe_add_asan_poison_write (def, &gsi);
   1915  1.1  mrg 	      def = make_ssa_name (def, stmt);
   1916  1.1  mrg 	    }
   1917  1.1  mrg 	  SET_DEF (def_p, def);
   1918  1.1  mrg 
   1919  1.1  mrg 	  tree tracked_var = target_for_debug_bind (sym);
   1920  1.1  mrg 	  if (tracked_var)
   1921  1.1  mrg 	    {
   1922  1.1  mrg 	      /* If stmt ends the bb, insert the debug stmt on the non-EH
   1923  1.1  mrg 		 edge(s) from the stmt.  */
   1924  1.1  mrg 	      if (gsi_one_before_end_p (gsi) && stmt_ends_bb_p (stmt))
   1925  1.1  mrg 		{
   1926  1.1  mrg 		  basic_block bb = gsi_bb (gsi);
   1927  1.1  mrg 		  edge_iterator ei;
   1928  1.1  mrg 		  edge e, ef = NULL;
   1929  1.1  mrg 		  FOR_EACH_EDGE (e, ei, bb->succs)
   1930  1.1  mrg 		    if (!(e->flags & EDGE_EH))
   1931  1.1  mrg 		      {
   1932  1.1  mrg 			/* asm goto can have multiple non-EH edges from the
   1933  1.1  mrg 			   stmt.  Insert on all of them where it is
   1934  1.1  mrg 			   possible.  */
   1935  1.1  mrg 			gcc_checking_assert (!ef || (gimple_code (stmt)
   1936  1.1  mrg 						     == GIMPLE_ASM));
   1937  1.1  mrg 			ef = e;
   1938  1.1  mrg 			/* If there are other predecessors to ef->dest, then
   1939  1.1  mrg 			   there must be PHI nodes for the modified
   1940  1.1  mrg 			   variable, and therefore there will be debug bind
   1941  1.1  mrg 			   stmts after the PHI nodes.  The debug bind notes
   1942  1.1  mrg 			   we'd insert would force the creation of a new
   1943  1.1  mrg 			   block (diverging codegen) and be redundant with
   1944  1.1  mrg 			   the post-PHI bind stmts, so don't add them.
   1945  1.1  mrg 
   1946  1.1  mrg 			   As for the exit edge, there wouldn't be redundant
   1947  1.1  mrg 			   bind stmts, but there wouldn't be a PC to bind
   1948  1.1  mrg 			   them to either, so avoid diverging the CFG.  */
   1949  1.1  mrg 			if (e
   1950  1.1  mrg 			    && single_pred_p (e->dest)
   1951  1.1  mrg 			    && gimple_seq_empty_p (phi_nodes (e->dest))
   1952  1.1  mrg 			    && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
   1953  1.1  mrg 			  {
   1954  1.1  mrg 			    /* If there were PHI nodes in the node, we'd
   1955  1.1  mrg 			       have to make sure the value we're binding
   1956  1.1  mrg 			       doesn't need rewriting.  But there shouldn't
   1957  1.1  mrg 			       be PHI nodes in a single-predecessor block,
   1958  1.1  mrg 			       so we just add the note.  */
   1959  1.1  mrg 			    gimple *note
   1960  1.1  mrg 			      = gimple_build_debug_bind (tracked_var, def,
   1961  1.1  mrg 							 stmt);
   1962  1.1  mrg 			    gsi_insert_on_edge_immediate (ef, note);
   1963  1.1  mrg 			  }
   1964  1.1  mrg 		      }
   1965  1.1  mrg 		}
   1966  1.1  mrg 	      else
   1967  1.1  mrg 		{
   1968  1.1  mrg 		  gimple *note
   1969  1.1  mrg 		    = gimple_build_debug_bind (tracked_var, def, stmt);
   1970  1.1  mrg 		  gsi_insert_after (&gsi, note, GSI_SAME_STMT);
   1971  1.1  mrg 		}
   1972  1.1  mrg 	    }
   1973  1.1  mrg 	}
   1974  1.1  mrg 
   1975  1.1  mrg       register_new_update_single (def, sym);
   1976  1.1  mrg     }
   1977  1.1  mrg   else
   1978  1.1  mrg     {
   1979  1.1  mrg       /* If DEF is a new name, register it as a new definition
   1980  1.1  mrg 	 for all the names replaced by DEF.  */
   1981  1.1  mrg       if (is_new_name (def))
   1982  1.1  mrg 	register_new_update_set (def, names_replaced_by (def));
   1983  1.1  mrg 
   1984  1.1  mrg       /* If DEF is an old name, register DEF as a new
   1985  1.1  mrg 	 definition for itself.  */
   1986  1.1  mrg       if (is_old_name (def))
   1987  1.1  mrg 	register_new_update_single (def, def);
   1988  1.1  mrg     }
   1989  1.1  mrg 
   1990  1.1  mrg   return to_delete;
   1991  1.1  mrg }
   1992  1.1  mrg 
   1993  1.1  mrg 
   1994  1.1  mrg /* Update every variable used in the statement pointed-to by SI.  The
   1995  1.1  mrg    statement is assumed to be in SSA form already.  Names in
   1996  1.1  mrg    OLD_SSA_NAMES used by SI will be updated to their current reaching
   1997  1.1  mrg    definition.  Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
   1998  1.1  mrg    will be registered as a new definition for their corresponding name
   1999  1.1  mrg    in OLD_SSA_NAMES.  Returns whether STMT should be removed.  */
   2000  1.1  mrg 
   2001  1.1  mrg static bool
   2002  1.1  mrg rewrite_update_stmt (gimple *stmt, gimple_stmt_iterator gsi)
   2003  1.1  mrg {
   2004  1.1  mrg   use_operand_p use_p;
   2005  1.1  mrg   def_operand_p def_p;
   2006  1.1  mrg   ssa_op_iter iter;
   2007  1.1  mrg 
   2008  1.1  mrg   /* Only update marked statements.  */
   2009  1.1  mrg   if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
   2010  1.1  mrg     return false;
   2011  1.1  mrg 
   2012  1.1  mrg   if (dump_file && (dump_flags & TDF_DETAILS))
   2013  1.1  mrg     {
   2014  1.1  mrg       fprintf (dump_file, "Updating SSA information for statement ");
   2015  1.1  mrg       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
   2016  1.1  mrg     }
   2017  1.1  mrg 
   2018  1.1  mrg   /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
   2019  1.1  mrg      symbol is marked for renaming.  */
   2020  1.1  mrg   if (rewrite_uses_p (stmt))
   2021  1.1  mrg     {
   2022  1.1  mrg       if (is_gimple_debug (stmt))
   2023  1.1  mrg 	{
   2024  1.1  mrg 	  bool failed = false;
   2025  1.1  mrg 
   2026  1.1  mrg 	  FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
   2027  1.1  mrg 	    if (!maybe_replace_use_in_debug_stmt (use_p))
   2028  1.1  mrg 	      {
   2029  1.1  mrg 		failed = true;
   2030  1.1  mrg 		break;
   2031  1.1  mrg 	      }
   2032  1.1  mrg 
   2033  1.1  mrg 	  if (failed)
   2034  1.1  mrg 	    {
   2035  1.1  mrg 	      /* DOM sometimes threads jumps in such a way that a
   2036  1.1  mrg 		 debug stmt ends up referencing a SSA variable that no
   2037  1.1  mrg 		 longer dominates the debug stmt, but such that all
   2038  1.1  mrg 		 incoming definitions refer to the same definition in
   2039  1.1  mrg 		 an earlier dominator.  We could try to recover that
   2040  1.1  mrg 		 definition somehow, but this will have to do for now.
   2041  1.1  mrg 
   2042  1.1  mrg 		 Introducing a default definition, which is what
   2043  1.1  mrg 		 maybe_replace_use() would do in such cases, may
   2044  1.1  mrg 		 modify code generation, for the otherwise-unused
   2045  1.1  mrg 		 default definition would never go away, modifying SSA
   2046  1.1  mrg 		 version numbers all over.  */
   2047  1.1  mrg 	      gimple_debug_bind_reset_value (stmt);
   2048  1.1  mrg 	      update_stmt (stmt);
   2049  1.1  mrg 	    }
   2050  1.1  mrg 	}
   2051  1.1  mrg       else
   2052  1.1  mrg 	{
   2053  1.1  mrg 	  FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
   2054  1.1  mrg 	    maybe_replace_use (use_p);
   2055  1.1  mrg 	}
   2056  1.1  mrg     }
   2057  1.1  mrg 
   2058  1.1  mrg   /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
   2059  1.1  mrg      Also register definitions for names whose underlying symbol is
   2060  1.1  mrg      marked for renaming.  */
   2061  1.1  mrg   bool to_delete = false;
   2062  1.1  mrg   if (register_defs_p (stmt))
   2063  1.1  mrg     FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
   2064  1.1  mrg       to_delete |= maybe_register_def (def_p, stmt, gsi);
   2065  1.1  mrg 
   2066  1.1  mrg   return to_delete;
   2067  1.1  mrg }
   2068  1.1  mrg 
   2069  1.1  mrg 
   2070  1.1  mrg /* Visit all the successor blocks of BB looking for PHI nodes.  For
   2071  1.1  mrg    every PHI node found, check if any of its arguments is in
   2072  1.1  mrg    OLD_SSA_NAMES.  If so, and if the argument has a current reaching
   2073  1.1  mrg    definition, replace it.  */
   2074  1.1  mrg 
   2075  1.1  mrg static void
   2076  1.1  mrg rewrite_update_phi_arguments (basic_block bb)
   2077  1.1  mrg {
   2078  1.1  mrg   edge e;
   2079  1.1  mrg   edge_iterator ei;
   2080  1.1  mrg 
   2081  1.1  mrg   FOR_EACH_EDGE (e, ei, bb->succs)
   2082  1.1  mrg     {
   2083  1.1  mrg       vec<gphi *> phis;
   2084  1.1  mrg 
   2085  1.1  mrg       if (!bitmap_bit_p (blocks_with_phis_to_rewrite, e->dest->index))
   2086  1.1  mrg 	continue;
   2087  1.1  mrg 
   2088  1.1  mrg       phis = phis_to_rewrite[e->dest->index];
   2089  1.1  mrg       for (gphi *phi : phis)
   2090  1.1  mrg 	{
   2091  1.1  mrg 	  tree arg, lhs_sym, reaching_def = NULL;
   2092  1.1  mrg 	  use_operand_p arg_p;
   2093  1.1  mrg 
   2094  1.1  mrg   	  gcc_checking_assert (rewrite_uses_p (phi));
   2095  1.1  mrg 
   2096  1.1  mrg 	  arg_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
   2097  1.1  mrg 	  arg = USE_FROM_PTR (arg_p);
   2098  1.1  mrg 
   2099  1.1  mrg 	  if (arg && !DECL_P (arg) && TREE_CODE (arg) != SSA_NAME)
   2100  1.1  mrg 	    continue;
   2101  1.1  mrg 
   2102  1.1  mrg 	  lhs_sym = SSA_NAME_VAR (gimple_phi_result (phi));
   2103  1.1  mrg 
   2104  1.1  mrg 	  if (arg == NULL_TREE)
   2105  1.1  mrg 	    {
   2106  1.1  mrg 	      /* When updating a PHI node for a recently introduced
   2107  1.1  mrg 		 symbol we may find NULL arguments.  That's why we
   2108  1.1  mrg 		 take the symbol from the LHS of the PHI node.  */
   2109  1.1  mrg 	      reaching_def = get_reaching_def (lhs_sym);
   2110  1.1  mrg 	    }
   2111  1.1  mrg 	  else
   2112  1.1  mrg 	    {
   2113  1.1  mrg 	      tree sym = DECL_P (arg) ? arg : SSA_NAME_VAR (arg);
   2114  1.1  mrg 
   2115  1.1  mrg 	      if (marked_for_renaming (sym))
   2116  1.1  mrg 		reaching_def = get_reaching_def (sym);
   2117  1.1  mrg 	      else if (is_old_name (arg))
   2118  1.1  mrg 		reaching_def = get_reaching_def (arg);
   2119  1.1  mrg 	    }
   2120  1.1  mrg 
   2121  1.1  mrg 	  /* Update the argument if there is a reaching def different
   2122  1.1  mrg 	     from arg.  */
   2123  1.1  mrg 	  if (reaching_def && reaching_def != arg)
   2124  1.1  mrg 	    {
   2125  1.1  mrg 	      location_t locus;
   2126  1.1  mrg 	      int arg_i = PHI_ARG_INDEX_FROM_USE (arg_p);
   2127  1.1  mrg 
   2128  1.1  mrg 	      SET_USE (arg_p, reaching_def);
   2129  1.1  mrg 
   2130  1.1  mrg 	      /* Virtual operands do not need a location.  */
   2131  1.1  mrg 	      if (virtual_operand_p (reaching_def))
   2132  1.1  mrg 		locus = UNKNOWN_LOCATION;
   2133  1.1  mrg 	      /* If SSA update didn't insert this PHI the argument
   2134  1.1  mrg 		 might have a location already, keep that.  */
   2135  1.1  mrg 	      else if (gimple_phi_arg_has_location (phi, arg_i))
   2136  1.1  mrg 		locus = gimple_phi_arg_location (phi, arg_i);
   2137  1.1  mrg 	      else
   2138  1.1  mrg 		{
   2139  1.1  mrg 		  gimple *stmt = SSA_NAME_DEF_STMT (reaching_def);
   2140  1.1  mrg 		  gphi *other_phi = dyn_cast <gphi *> (stmt);
   2141  1.1  mrg 
   2142  1.1  mrg 		  /* Single element PHI nodes  behave like copies, so get the
   2143  1.1  mrg 		     location from the phi argument.  */
   2144  1.1  mrg 		  if (other_phi
   2145  1.1  mrg 		      && gimple_phi_num_args (other_phi) == 1)
   2146  1.1  mrg 		    locus = gimple_phi_arg_location (other_phi, 0);
   2147  1.1  mrg 		  else
   2148  1.1  mrg 		    locus = gimple_location (stmt);
   2149  1.1  mrg 		}
   2150  1.1  mrg 
   2151  1.1  mrg 	      gimple_phi_arg_set_location (phi, arg_i, locus);
   2152  1.1  mrg 	    }
   2153  1.1  mrg 
   2154  1.1  mrg 	  if (e->flags & EDGE_ABNORMAL)
   2155  1.1  mrg 	    SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p)) = 1;
   2156  1.1  mrg 	}
   2157  1.1  mrg     }
   2158  1.1  mrg }
   2159  1.1  mrg 
   2160  1.1  mrg class rewrite_update_dom_walker : public dom_walker
   2161  1.1  mrg {
   2162  1.1  mrg public:
   2163  1.1  mrg   rewrite_update_dom_walker (cdi_direction direction)
   2164  1.1  mrg     : dom_walker (direction, ALL_BLOCKS, NULL) {}
   2165  1.1  mrg 
   2166  1.1  mrg   virtual edge before_dom_children (basic_block);
   2167  1.1  mrg   virtual void after_dom_children (basic_block);
   2168  1.1  mrg };
   2169  1.1  mrg 
   2170  1.1  mrg /* Initialization of block data structures for the incremental SSA
   2171  1.1  mrg    update pass.  Create a block local stack of reaching definitions
   2172  1.1  mrg    for new SSA names produced in this block (BLOCK_DEFS).  Register
   2173  1.1  mrg    new definitions for every PHI node in the block.  */
   2174  1.1  mrg 
   2175  1.1  mrg edge
   2176  1.1  mrg rewrite_update_dom_walker::before_dom_children (basic_block bb)
   2177  1.1  mrg {
   2178  1.1  mrg   bool is_abnormal_phi;
   2179  1.1  mrg 
   2180  1.1  mrg   if (dump_file && (dump_flags & TDF_DETAILS))
   2181  1.1  mrg     fprintf (dump_file, "Registering new PHI nodes in block #%d\n",
   2182  1.1  mrg 	     bb->index);
   2183  1.1  mrg 
   2184  1.1  mrg   /* Mark the unwind point for this block.  */
   2185  1.1  mrg   block_defs_stack.safe_push (NULL_TREE);
   2186  1.1  mrg 
   2187  1.1  mrg   if (!bitmap_bit_p (blocks_to_update, bb->index))
   2188  1.1  mrg     return NULL;
   2189  1.1  mrg 
   2190  1.1  mrg   /* Mark the LHS if any of the arguments flows through an abnormal
   2191  1.1  mrg      edge.  */
   2192  1.1  mrg   is_abnormal_phi = bb_has_abnormal_pred (bb);
   2193  1.1  mrg 
   2194  1.1  mrg   /* If any of the PHI nodes is a replacement for a name in
   2195  1.1  mrg      OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
   2196  1.1  mrg      register it as a new definition for its corresponding name.  Also
   2197  1.1  mrg      register definitions for names whose underlying symbols are
   2198  1.1  mrg      marked for renaming.  */
   2199  1.1  mrg   for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
   2200  1.1  mrg        gsi_next (&gsi))
   2201  1.1  mrg     {
   2202  1.1  mrg       tree lhs, lhs_sym;
   2203  1.1  mrg       gphi *phi = gsi.phi ();
   2204  1.1  mrg 
   2205  1.1  mrg       if (!register_defs_p (phi))
   2206  1.1  mrg 	continue;
   2207  1.1  mrg 
   2208  1.1  mrg       lhs = gimple_phi_result (phi);
   2209  1.1  mrg       lhs_sym = SSA_NAME_VAR (lhs);
   2210  1.1  mrg 
   2211  1.1  mrg       if (marked_for_renaming (lhs_sym))
   2212  1.1  mrg 	register_new_update_single (lhs, lhs_sym);
   2213  1.1  mrg       else
   2214  1.1  mrg 	{
   2215  1.1  mrg 
   2216  1.1  mrg 	  /* If LHS is a new name, register a new definition for all
   2217  1.1  mrg 	     the names replaced by LHS.  */
   2218  1.1  mrg 	  if (is_new_name (lhs))
   2219  1.1  mrg 	    register_new_update_set (lhs, names_replaced_by (lhs));
   2220  1.1  mrg 
   2221  1.1  mrg 	  /* If LHS is an OLD name, register it as a new definition
   2222  1.1  mrg 	     for itself.  */
   2223  1.1  mrg 	  if (is_old_name (lhs))
   2224  1.1  mrg 	    register_new_update_single (lhs, lhs);
   2225  1.1  mrg 	}
   2226  1.1  mrg 
   2227  1.1  mrg       if (is_abnormal_phi)
   2228  1.1  mrg 	SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) = 1;
   2229  1.1  mrg     }
   2230  1.1  mrg 
   2231  1.1  mrg   /* Step 2.  Rewrite every variable used in each statement in the block.  */
   2232  1.1  mrg   if (bitmap_bit_p (interesting_blocks, bb->index))
   2233  1.1  mrg     {
   2234  1.1  mrg       gcc_checking_assert (bitmap_bit_p (blocks_to_update, bb->index));
   2235  1.1  mrg       for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
   2236  1.1  mrg 	if (rewrite_update_stmt (gsi_stmt (gsi), gsi))
   2237  1.1  mrg 	  gsi_remove (&gsi, true);
   2238  1.1  mrg 	else
   2239  1.1  mrg 	  gsi_next (&gsi);
   2240  1.1  mrg     }
   2241  1.1  mrg 
   2242  1.1  mrg   /* Step 3.  Update PHI nodes.  */
   2243  1.1  mrg   rewrite_update_phi_arguments (bb);
   2244  1.1  mrg 
   2245  1.1  mrg   return NULL;
   2246  1.1  mrg }
   2247  1.1  mrg 
   2248  1.1  mrg /* Called after visiting block BB.  Unwind BLOCK_DEFS_STACK to restore
   2249  1.1  mrg    the current reaching definition of every name re-written in BB to
   2250  1.1  mrg    the original reaching definition before visiting BB.  This
   2251  1.1  mrg    unwinding must be done in the opposite order to what is done in
   2252  1.1  mrg    register_new_update_set.  */
   2253  1.1  mrg 
   2254  1.1  mrg void
   2255  1.1  mrg rewrite_update_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
   2256  1.1  mrg {
   2257  1.1  mrg   while (block_defs_stack.length () > 0)
   2258  1.1  mrg     {
   2259  1.1  mrg       tree var = block_defs_stack.pop ();
   2260  1.1  mrg       tree saved_def;
   2261  1.1  mrg 
   2262  1.1  mrg       /* NULL indicates the unwind stop point for this block (see
   2263  1.1  mrg 	 rewrite_update_enter_block).  */
   2264  1.1  mrg       if (var == NULL)
   2265  1.1  mrg 	return;
   2266  1.1  mrg 
   2267  1.1  mrg       saved_def = block_defs_stack.pop ();
   2268  1.1  mrg       get_common_info (var)->current_def = saved_def;
   2269  1.1  mrg     }
   2270  1.1  mrg }
   2271  1.1  mrg 
   2272  1.1  mrg 
   2273  1.1  mrg /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
   2274  1.1  mrg    form.
   2275  1.1  mrg 
   2276  1.1  mrg    ENTRY indicates the block where to start.  Every block dominated by
   2277  1.1  mrg       ENTRY will be rewritten.
   2278  1.1  mrg 
   2279  1.1  mrg    WHAT indicates what actions will be taken by the renamer (see enum
   2280  1.1  mrg       rewrite_mode).
   2281  1.1  mrg 
   2282  1.1  mrg    BLOCKS are the set of interesting blocks for the dominator walker
   2283  1.1  mrg       to process.  If this set is NULL, then all the nodes dominated
   2284  1.1  mrg       by ENTRY are walked.  Otherwise, blocks dominated by ENTRY that
   2285  1.1  mrg       are not present in BLOCKS are ignored.  */
   2286  1.1  mrg 
   2287  1.1  mrg static void
   2288  1.1  mrg rewrite_blocks (basic_block entry, enum rewrite_mode what)
   2289  1.1  mrg {
   2290  1.1  mrg   block_defs_stack.create (10);
   2291  1.1  mrg 
   2292  1.1  mrg   /* Recursively walk the dominator tree rewriting each statement in
   2293  1.1  mrg      each basic block.  */
   2294  1.1  mrg   if (what == REWRITE_ALL)
   2295  1.1  mrg       rewrite_dom_walker (CDI_DOMINATORS).walk (entry);
   2296  1.1  mrg   else if (what == REWRITE_UPDATE)
   2297  1.1  mrg       rewrite_update_dom_walker (CDI_DOMINATORS).walk (entry);
   2298  1.1  mrg   else
   2299  1.1  mrg     gcc_unreachable ();
   2300  1.1  mrg 
   2301  1.1  mrg   /* Debugging dumps.  */
   2302  1.1  mrg   if (dump_file && (dump_flags & TDF_STATS))
   2303  1.1  mrg     {
   2304  1.1  mrg       dump_dfa_stats (dump_file);
   2305  1.1  mrg       if (var_infos)
   2306  1.1  mrg 	dump_tree_ssa_stats (dump_file);
   2307  1.1  mrg     }
   2308  1.1  mrg 
   2309  1.1  mrg   block_defs_stack.release ();
   2310  1.1  mrg }
   2311  1.1  mrg 
   2312  1.1  mrg class mark_def_dom_walker : public dom_walker
   2313  1.1  mrg {
   2314  1.1  mrg public:
   2315  1.1  mrg   mark_def_dom_walker (cdi_direction direction);
   2316  1.1  mrg   ~mark_def_dom_walker ();
   2317  1.1  mrg 
   2318  1.1  mrg   virtual edge before_dom_children (basic_block);
   2319  1.1  mrg 
   2320  1.1  mrg private:
   2321  1.1  mrg   /* Notice that this bitmap is indexed using variable UIDs, so it must be
   2322  1.1  mrg      large enough to accommodate all the variables referenced in the
   2323  1.1  mrg      function, not just the ones we are renaming.  */
   2324  1.1  mrg   bitmap m_kills;
   2325  1.1  mrg };
   2326  1.1  mrg 
   2327  1.1  mrg mark_def_dom_walker::mark_def_dom_walker (cdi_direction direction)
   2328  1.1  mrg   : dom_walker (direction, ALL_BLOCKS, NULL), m_kills (BITMAP_ALLOC (NULL))
   2329  1.1  mrg {
   2330  1.1  mrg }
   2331  1.1  mrg 
   2332  1.1  mrg mark_def_dom_walker::~mark_def_dom_walker ()
   2333  1.1  mrg {
   2334  1.1  mrg   BITMAP_FREE (m_kills);
   2335  1.1  mrg }
   2336  1.1  mrg 
   2337  1.1  mrg /* Block processing routine for mark_def_sites.  Clear the KILLS bitmap
   2338  1.1  mrg    at the start of each block, and call mark_def_sites for each statement.  */
   2339  1.1  mrg 
   2340  1.1  mrg edge
   2341  1.1  mrg mark_def_dom_walker::before_dom_children (basic_block bb)
   2342  1.1  mrg {
   2343  1.1  mrg   gimple_stmt_iterator gsi;
   2344  1.1  mrg 
   2345  1.1  mrg   bitmap_clear (m_kills);
   2346  1.1  mrg   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
   2347  1.1  mrg     mark_def_sites (bb, gsi_stmt (gsi), m_kills);
   2348  1.1  mrg   return NULL;
   2349  1.1  mrg }
   2350  1.1  mrg 
   2351  1.1  mrg /* Initialize internal data needed during renaming.  */
   2352  1.1  mrg 
   2353  1.1  mrg static void
   2354  1.1  mrg init_ssa_renamer (void)
   2355  1.1  mrg {
   2356  1.1  mrg   cfun->gimple_df->in_ssa_p = false;
   2357  1.1  mrg 
   2358  1.1  mrg   /* Allocate memory for the DEF_BLOCKS hash table.  */
   2359  1.1  mrg   gcc_assert (!var_infos);
   2360  1.1  mrg   var_infos = new hash_table<var_info_hasher>
   2361  1.1  mrg     (vec_safe_length (cfun->local_decls));
   2362  1.1  mrg 
   2363  1.1  mrg   bitmap_obstack_initialize (&update_ssa_obstack);
   2364  1.1  mrg }
   2365  1.1  mrg 
   2366  1.1  mrg 
   2367  1.1  mrg /* Deallocate internal data structures used by the renamer.  */
   2368  1.1  mrg 
   2369  1.1  mrg static void
   2370  1.1  mrg fini_ssa_renamer (void)
   2371  1.1  mrg {
   2372  1.1  mrg   delete var_infos;
   2373  1.1  mrg     var_infos = NULL;
   2374  1.1  mrg 
   2375  1.1  mrg   bitmap_obstack_release (&update_ssa_obstack);
   2376  1.1  mrg 
   2377  1.1  mrg   cfun->gimple_df->ssa_renaming_needed = 0;
   2378  1.1  mrg   cfun->gimple_df->rename_vops = 0;
   2379  1.1  mrg   cfun->gimple_df->in_ssa_p = true;
   2380  1.1  mrg }
   2381  1.1  mrg 
   2382  1.1  mrg /* Main entry point into the SSA builder.  The renaming process
   2383  1.1  mrg    proceeds in four main phases:
   2384  1.1  mrg 
   2385  1.1  mrg    1- Compute dominance frontier and immediate dominators, needed to
   2386  1.1  mrg       insert PHI nodes and rename the function in dominator tree
   2387  1.1  mrg       order.
   2388  1.1  mrg 
   2389  1.1  mrg    2- Find and mark all the blocks that define variables.
   2390  1.1  mrg 
   2391  1.1  mrg    3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
   2392  1.1  mrg 
   2393  1.1  mrg    4- Rename all the blocks (rewrite_blocks) and statements in the program.
   2394  1.1  mrg 
   2395  1.1  mrg    Steps 3 and 4 are done using the dominator tree walker
   2396  1.1  mrg    (walk_dominator_tree).  */
   2397  1.1  mrg 
   2398  1.1  mrg namespace {
   2399  1.1  mrg 
   2400  1.1  mrg const pass_data pass_data_build_ssa =
   2401  1.1  mrg {
   2402  1.1  mrg   GIMPLE_PASS, /* type */
   2403  1.1  mrg   "ssa", /* name */
   2404  1.1  mrg   OPTGROUP_NONE, /* optinfo_flags */
   2405  1.1  mrg   TV_TREE_INTO_SSA, /* tv_id */
   2406  1.1  mrg   PROP_cfg, /* properties_required */
   2407  1.1  mrg   PROP_ssa, /* properties_provided */
   2408  1.1  mrg   0, /* properties_destroyed */
   2409  1.1  mrg   0, /* todo_flags_start */
   2410  1.1  mrg   TODO_remove_unused_locals, /* todo_flags_finish */
   2411  1.1  mrg };
   2412  1.1  mrg 
   2413  1.1  mrg class pass_build_ssa : public gimple_opt_pass
   2414  1.1  mrg {
   2415  1.1  mrg public:
   2416  1.1  mrg   pass_build_ssa (gcc::context *ctxt)
   2417  1.1  mrg     : gimple_opt_pass (pass_data_build_ssa, ctxt)
   2418  1.1  mrg   {}
   2419  1.1  mrg 
   2420  1.1  mrg   /* opt_pass methods: */
   2421  1.1  mrg   virtual bool gate (function *fun)
   2422  1.1  mrg     {
   2423  1.1  mrg       /* Do nothing for funcions that was produced already in SSA form.  */
   2424  1.1  mrg       return !(fun->curr_properties & PROP_ssa);
   2425  1.1  mrg     }
   2426  1.1  mrg 
   2427  1.1  mrg   virtual unsigned int execute (function *);
   2428  1.1  mrg 
   2429  1.1  mrg }; // class pass_build_ssa
   2430  1.1  mrg 
   2431  1.1  mrg unsigned int
   2432  1.1  mrg pass_build_ssa::execute (function *fun)
   2433  1.1  mrg {
   2434  1.1  mrg   bitmap_head *dfs;
   2435  1.1  mrg   basic_block bb;
   2436  1.1  mrg 
   2437  1.1  mrg   /* Increase the set of variables we can rewrite into SSA form
   2438  1.1  mrg      by clearing TREE_ADDRESSABLE and transform the IL to support this.  */
   2439  1.1  mrg   if (optimize)
   2440  1.1  mrg     execute_update_addresses_taken ();
   2441  1.1  mrg 
   2442  1.1  mrg   /* Initialize operand data structures.  */
   2443  1.1  mrg   init_ssa_operands (fun);
   2444  1.1  mrg 
   2445  1.1  mrg   /* Initialize internal data needed by the renamer.  */
   2446  1.1  mrg   init_ssa_renamer ();
   2447  1.1  mrg 
   2448  1.1  mrg   /* Initialize the set of interesting blocks.  The callback
   2449  1.1  mrg      mark_def_sites will add to this set those blocks that the renamer
   2450  1.1  mrg      should process.  */
   2451  1.1  mrg   interesting_blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
   2452  1.1  mrg   bitmap_clear (interesting_blocks);
   2453  1.1  mrg 
   2454  1.1  mrg   /* Initialize dominance frontier.  */
   2455  1.1  mrg   dfs = XNEWVEC (bitmap_head, last_basic_block_for_fn (fun));
   2456  1.1  mrg   FOR_EACH_BB_FN (bb, fun)
   2457  1.1  mrg     bitmap_initialize (&dfs[bb->index], &bitmap_default_obstack);
   2458  1.1  mrg 
   2459  1.1  mrg   /* 1- Compute dominance frontiers.  */
   2460  1.1  mrg   calculate_dominance_info (CDI_DOMINATORS);
   2461  1.1  mrg   compute_dominance_frontiers (dfs);
   2462  1.1  mrg 
   2463  1.1  mrg   /* 2- Find and mark definition sites.  */
   2464  1.1  mrg   mark_def_dom_walker (CDI_DOMINATORS).walk (fun->cfg->x_entry_block_ptr);
   2465  1.1  mrg 
   2466  1.1  mrg   /* 3- Insert PHI nodes at dominance frontiers of definition blocks.  */
   2467  1.1  mrg   insert_phi_nodes (dfs);
   2468  1.1  mrg 
   2469  1.1  mrg   /* 4- Rename all the blocks.  */
   2470  1.1  mrg   rewrite_blocks (ENTRY_BLOCK_PTR_FOR_FN (fun), REWRITE_ALL);
   2471  1.1  mrg 
   2472  1.1  mrg   /* Free allocated memory.  */
   2473  1.1  mrg   FOR_EACH_BB_FN (bb, fun)
   2474  1.1  mrg     bitmap_clear (&dfs[bb->index]);
   2475  1.1  mrg   free (dfs);
   2476  1.1  mrg 
   2477  1.1  mrg   sbitmap_free (interesting_blocks);
   2478  1.1  mrg 
   2479  1.1  mrg   fini_ssa_renamer ();
   2480  1.1  mrg 
   2481  1.1  mrg   /* Try to get rid of all gimplifier generated temporaries by making
   2482  1.1  mrg      its SSA names anonymous.  This way we can garbage collect them
   2483  1.1  mrg      all after removing unused locals which we do in our TODO.  */
   2484  1.1  mrg   unsigned i;
   2485  1.1  mrg   tree name;
   2486  1.1  mrg 
   2487  1.1  mrg   FOR_EACH_SSA_NAME (i, name, cfun)
   2488  1.1  mrg     {
   2489  1.1  mrg       if (SSA_NAME_IS_DEFAULT_DEF (name))
   2490  1.1  mrg 	continue;
   2491  1.1  mrg       tree decl = SSA_NAME_VAR (name);
   2492  1.1  mrg       if (decl
   2493  1.1  mrg 	  && VAR_P (decl)
   2494  1.1  mrg 	  && !VAR_DECL_IS_VIRTUAL_OPERAND (decl)
   2495  1.1  mrg 	  && DECL_IGNORED_P (decl))
   2496  1.1  mrg 	SET_SSA_NAME_VAR_OR_IDENTIFIER (name, DECL_NAME (decl));
   2497  1.1  mrg     }
   2498  1.1  mrg 
   2499  1.1  mrg   /* Initialize SSA_NAME_POINTS_TO_READONLY_MEMORY.  */
   2500  1.1  mrg   tree fnspec_tree
   2501  1.1  mrg 	 = lookup_attribute ("fn spec",
   2502  1.1  mrg 			     TYPE_ATTRIBUTES (TREE_TYPE (fun->decl)));
   2503  1.1  mrg   if (fnspec_tree)
   2504  1.1  mrg     {
   2505  1.1  mrg       attr_fnspec fnspec (TREE_VALUE (TREE_VALUE (fnspec_tree)));
   2506  1.1  mrg       unsigned i = 0;
   2507  1.1  mrg       for (tree arg = DECL_ARGUMENTS (cfun->decl);
   2508  1.1  mrg 	   arg; arg = DECL_CHAIN (arg), ++i)
   2509  1.1  mrg 	{
   2510  1.1  mrg 	  if (!fnspec.arg_specified_p (i))
   2511  1.1  mrg 	   break;
   2512  1.1  mrg 	  if (fnspec.arg_readonly_p (i))
   2513  1.1  mrg 	    {
   2514  1.1  mrg 	      tree name = ssa_default_def (fun, arg);
   2515  1.1  mrg 	      if (name)
   2516  1.1  mrg 		SSA_NAME_POINTS_TO_READONLY_MEMORY (name) = 1;
   2517  1.1  mrg 	    }
   2518  1.1  mrg 	}
   2519  1.1  mrg     }
   2520  1.1  mrg 
   2521  1.1  mrg   return 0;
   2522  1.1  mrg }
   2523  1.1  mrg 
   2524  1.1  mrg } // anon namespace
   2525  1.1  mrg 
   2526  1.1  mrg gimple_opt_pass *
   2527  1.1  mrg make_pass_build_ssa (gcc::context *ctxt)
   2528  1.1  mrg {
   2529  1.1  mrg   return new pass_build_ssa (ctxt);
   2530  1.1  mrg }
   2531  1.1  mrg 
   2532  1.1  mrg 
   2533  1.1  mrg /* Mark the definition of VAR at STMT and BB as interesting for the
   2534  1.1  mrg    renamer.  BLOCKS is the set of blocks that need updating.  */
   2535  1.1  mrg 
   2536  1.1  mrg static void
   2537  1.1  mrg mark_def_interesting (tree var, gimple *stmt, basic_block bb,
   2538  1.1  mrg 		      bool insert_phi_p)
   2539  1.1  mrg {
   2540  1.1  mrg   gcc_checking_assert (bitmap_bit_p (blocks_to_update, bb->index));
   2541  1.1  mrg   set_register_defs (stmt, true);
   2542  1.1  mrg 
   2543  1.1  mrg   if (insert_phi_p)
   2544  1.1  mrg     {
   2545  1.1  mrg       bool is_phi_p = gimple_code (stmt) == GIMPLE_PHI;
   2546  1.1  mrg 
   2547  1.1  mrg       set_def_block (var, bb, is_phi_p);
   2548  1.1  mrg 
   2549  1.1  mrg       /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
   2550  1.1  mrg 	 site for both itself and all the old names replaced by it.  */
   2551  1.1  mrg       if (TREE_CODE (var) == SSA_NAME && is_new_name (var))
   2552  1.1  mrg 	{
   2553  1.1  mrg 	  bitmap_iterator bi;
   2554  1.1  mrg 	  unsigned i;
   2555  1.1  mrg 	  bitmap set = names_replaced_by (var);
   2556  1.1  mrg 	  if (set)
   2557  1.1  mrg 	    EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
   2558  1.1  mrg 	      set_def_block (ssa_name (i), bb, is_phi_p);
   2559  1.1  mrg 	}
   2560  1.1  mrg     }
   2561  1.1  mrg }
   2562  1.1  mrg 
   2563  1.1  mrg 
   2564  1.1  mrg /* Mark the use of VAR at STMT and BB as interesting for the
   2565  1.1  mrg    renamer.  INSERT_PHI_P is true if we are going to insert new PHI
   2566  1.1  mrg    nodes.  */
   2567  1.1  mrg 
   2568  1.1  mrg static inline void
   2569  1.1  mrg mark_use_interesting (tree var, gimple *stmt, basic_block bb,
   2570  1.1  mrg 		      bool insert_phi_p)
   2571  1.1  mrg {
   2572  1.1  mrg   basic_block def_bb = gimple_bb (stmt);
   2573  1.1  mrg 
   2574  1.1  mrg   mark_block_for_update (def_bb);
   2575  1.1  mrg   mark_block_for_update (bb);
   2576  1.1  mrg 
   2577  1.1  mrg   if (gimple_code (stmt) == GIMPLE_PHI)
   2578  1.1  mrg     mark_phi_for_rewrite (def_bb, as_a <gphi *> (stmt));
   2579  1.1  mrg   else
   2580  1.1  mrg     {
   2581  1.1  mrg       set_rewrite_uses (stmt, true);
   2582  1.1  mrg 
   2583  1.1  mrg       if (is_gimple_debug (stmt))
   2584  1.1  mrg 	return;
   2585  1.1  mrg     }
   2586  1.1  mrg 
   2587  1.1  mrg   /* If VAR has not been defined in BB, then it is live-on-entry
   2588  1.1  mrg      to BB.  Note that we cannot just use the block holding VAR's
   2589  1.1  mrg      definition because if VAR is one of the names in OLD_SSA_NAMES,
   2590  1.1  mrg      it will have several definitions (itself and all the names that
   2591  1.1  mrg      replace it).  */
   2592  1.1  mrg   if (insert_phi_p)
   2593  1.1  mrg     {
   2594  1.1  mrg       def_blocks *db_p = get_def_blocks_for (get_common_info (var));
   2595  1.1  mrg       if (!bitmap_bit_p (db_p->def_blocks, bb->index))
   2596  1.1  mrg 	set_livein_block (var, bb);
   2597  1.1  mrg     }
   2598  1.1  mrg }
   2599  1.1  mrg 
   2600  1.1  mrg /* Processing statements in BB that reference symbols in SSA operands.
   2601  1.1  mrg    This is very similar to mark_def_sites, but the scan handles
   2602  1.1  mrg    statements whose operands may already be SSA names.
   2603  1.1  mrg 
   2604  1.1  mrg    If INSERT_PHI_P is true, mark those uses as live in the
   2605  1.1  mrg    corresponding block.  This is later used by the PHI placement
   2606  1.1  mrg    algorithm to make PHI pruning decisions.
   2607  1.1  mrg 
   2608  1.1  mrg    FIXME.  Most of this would be unnecessary if we could associate a
   2609  1.1  mrg 	   symbol to all the SSA names that reference it.  But that
   2610  1.1  mrg 	   sounds like it would be expensive to maintain.  Still, it
   2611  1.1  mrg 	   would be interesting to see if it makes better sense to do
   2612  1.1  mrg 	   that.  */
   2613  1.1  mrg 
   2614  1.1  mrg static void
   2615  1.1  mrg prepare_block_for_update_1 (basic_block bb, bool insert_phi_p)
   2616  1.1  mrg {
   2617  1.1  mrg   edge e;
   2618  1.1  mrg   edge_iterator ei;
   2619  1.1  mrg 
   2620  1.1  mrg   mark_block_for_update (bb);
   2621  1.1  mrg 
   2622  1.1  mrg   /* Process PHI nodes marking interesting those that define or use
   2623  1.1  mrg      the symbols that we are interested in.  */
   2624  1.1  mrg   for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
   2625  1.1  mrg        gsi_next (&si))
   2626  1.1  mrg     {
   2627  1.1  mrg       gphi *phi = si.phi ();
   2628  1.1  mrg       tree lhs_sym, lhs = gimple_phi_result (phi);
   2629  1.1  mrg 
   2630  1.1  mrg       if (TREE_CODE (lhs) == SSA_NAME
   2631  1.1  mrg 	  && (! virtual_operand_p (lhs)
   2632  1.1  mrg 	      || ! cfun->gimple_df->rename_vops))
   2633  1.1  mrg 	continue;
   2634  1.1  mrg 
   2635  1.1  mrg       lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs);
   2636  1.1  mrg       mark_for_renaming (lhs_sym);
   2637  1.1  mrg       mark_def_interesting (lhs_sym, phi, bb, insert_phi_p);
   2638  1.1  mrg 
   2639  1.1  mrg       /* Mark the uses in phi nodes as interesting.  It would be more correct
   2640  1.1  mrg 	 to process the arguments of the phi nodes of the successor edges of
   2641  1.1  mrg 	 BB at the end of prepare_block_for_update, however, that turns out
   2642  1.1  mrg 	 to be significantly more expensive.  Doing it here is conservatively
   2643  1.1  mrg 	 correct -- it may only cause us to believe a value to be live in a
   2644  1.1  mrg 	 block that also contains its definition, and thus insert a few more
   2645  1.1  mrg 	 phi nodes for it.  */
   2646  1.1  mrg       FOR_EACH_EDGE (e, ei, bb->preds)
   2647  1.1  mrg 	mark_use_interesting (lhs_sym, phi, e->src, insert_phi_p);
   2648  1.1  mrg     }
   2649  1.1  mrg 
   2650  1.1  mrg   /* Process the statements.  */
   2651  1.1  mrg   for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
   2652  1.1  mrg        gsi_next (&si))
   2653  1.1  mrg     {
   2654  1.1  mrg       gimple *stmt;
   2655  1.1  mrg       ssa_op_iter i;
   2656  1.1  mrg       use_operand_p use_p;
   2657  1.1  mrg       def_operand_p def_p;
   2658  1.1  mrg 
   2659  1.1  mrg       stmt = gsi_stmt (si);
   2660  1.1  mrg 
   2661  1.1  mrg       if (cfun->gimple_df->rename_vops
   2662  1.1  mrg 	  && gimple_vuse (stmt))
   2663  1.1  mrg 	{
   2664  1.1  mrg 	  tree use = gimple_vuse (stmt);
   2665  1.1  mrg 	  tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
   2666  1.1  mrg 	  mark_for_renaming (sym);
   2667  1.1  mrg 	  mark_use_interesting (sym, stmt, bb, insert_phi_p);
   2668  1.1  mrg 	}
   2669  1.1  mrg 
   2670  1.1  mrg       FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE)
   2671  1.1  mrg 	{
   2672  1.1  mrg 	  tree use = USE_FROM_PTR (use_p);
   2673  1.1  mrg 	  if (!DECL_P (use))
   2674  1.1  mrg 	    continue;
   2675  1.1  mrg 	  mark_for_renaming (use);
   2676  1.1  mrg 	  mark_use_interesting (use, stmt, bb, insert_phi_p);
   2677  1.1  mrg 	}
   2678  1.1  mrg 
   2679  1.1  mrg       if (cfun->gimple_df->rename_vops
   2680  1.1  mrg 	  && gimple_vdef (stmt))
   2681  1.1  mrg 	{
   2682  1.1  mrg 	  tree def = gimple_vdef (stmt);
   2683  1.1  mrg 	  tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
   2684  1.1  mrg 	  mark_for_renaming (sym);
   2685  1.1  mrg 	  mark_def_interesting (sym, stmt, bb, insert_phi_p);
   2686  1.1  mrg 	}
   2687  1.1  mrg 
   2688  1.1  mrg       FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_DEF)
   2689  1.1  mrg 	{
   2690  1.1  mrg 	  tree def = DEF_FROM_PTR (def_p);
   2691  1.1  mrg 	  if (!DECL_P (def))
   2692  1.1  mrg 	    continue;
   2693  1.1  mrg 	  mark_for_renaming (def);
   2694  1.1  mrg 	  mark_def_interesting (def, stmt, bb, insert_phi_p);
   2695  1.1  mrg 	}
   2696  1.1  mrg     }
   2697  1.1  mrg 
   2698  1.1  mrg }
   2699  1.1  mrg 
   2700  1.1  mrg /* Do a dominator walk starting at BB processing statements that
   2701  1.1  mrg    reference symbols in SSA operands.  This is very similar to
   2702  1.1  mrg    mark_def_sites, but the scan handles statements whose operands may
   2703  1.1  mrg    already be SSA names.
   2704  1.1  mrg 
   2705  1.1  mrg    If INSERT_PHI_P is true, mark those uses as live in the
   2706  1.1  mrg    corresponding block.  This is later used by the PHI placement
   2707  1.1  mrg    algorithm to make PHI pruning decisions.
   2708  1.1  mrg 
   2709  1.1  mrg    FIXME.  Most of this would be unnecessary if we could associate a
   2710  1.1  mrg 	   symbol to all the SSA names that reference it.  But that
   2711  1.1  mrg 	   sounds like it would be expensive to maintain.  Still, it
   2712  1.1  mrg 	   would be interesting to see if it makes better sense to do
   2713  1.1  mrg 	   that.  */
   2714  1.1  mrg static void
   2715  1.1  mrg prepare_block_for_update (basic_block bb, bool insert_phi_p)
   2716  1.1  mrg {
   2717  1.1  mrg   size_t sp = 0;
   2718  1.1  mrg   basic_block *worklist;
   2719  1.1  mrg 
   2720  1.1  mrg   /* Allocate the worklist.  */
   2721  1.1  mrg   worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
   2722  1.1  mrg   /* Add the BB to the worklist.  */
   2723  1.1  mrg   worklist[sp++] = bb;
   2724  1.1  mrg 
   2725  1.1  mrg   while (sp)
   2726  1.1  mrg     {
   2727  1.1  mrg       basic_block bb;
   2728  1.1  mrg       basic_block son;
   2729  1.1  mrg 
   2730  1.1  mrg       /* Pick a block from the worklist.  */
   2731  1.1  mrg       bb = worklist[--sp];
   2732  1.1  mrg 
   2733  1.1  mrg       prepare_block_for_update_1 (bb, insert_phi_p);
   2734  1.1  mrg 
   2735  1.1  mrg       /* Now add all the blocks dominated by BB to the worklist.  */
   2736  1.1  mrg       for (son = first_dom_son (CDI_DOMINATORS, bb);
   2737  1.1  mrg 	   son;
   2738  1.1  mrg 	   son = next_dom_son (CDI_DOMINATORS, son))
   2739  1.1  mrg 	worklist[sp++] = son;
   2740  1.1  mrg     }
   2741  1.1  mrg   free (worklist);
   2742  1.1  mrg }
   2743  1.1  mrg 
   2744  1.1  mrg /* Helper for prepare_names_to_update.  Mark all the use sites for
   2745  1.1  mrg    NAME as interesting.  BLOCKS and INSERT_PHI_P are as in
   2746  1.1  mrg    prepare_names_to_update.  */
   2747  1.1  mrg 
   2748  1.1  mrg static void
   2749  1.1  mrg prepare_use_sites_for (tree name, bool insert_phi_p)
   2750  1.1  mrg {
   2751  1.1  mrg   use_operand_p use_p;
   2752  1.1  mrg   imm_use_iterator iter;
   2753  1.1  mrg 
   2754  1.1  mrg   /* If we rename virtual operands do not update them.  */
   2755  1.1  mrg   if (virtual_operand_p (name)
   2756  1.1  mrg       && cfun->gimple_df->rename_vops)
   2757  1.1  mrg     return;
   2758  1.1  mrg 
   2759  1.1  mrg   FOR_EACH_IMM_USE_FAST (use_p, iter, name)
   2760  1.1  mrg     {
   2761  1.1  mrg       gimple *stmt = USE_STMT (use_p);
   2762  1.1  mrg       basic_block bb = gimple_bb (stmt);
   2763  1.1  mrg 
   2764  1.1  mrg       if (gimple_code (stmt) == GIMPLE_PHI)
   2765  1.1  mrg 	{
   2766  1.1  mrg 	  int ix = PHI_ARG_INDEX_FROM_USE (use_p);
   2767  1.1  mrg 	  edge e = gimple_phi_arg_edge (as_a <gphi *> (stmt), ix);
   2768  1.1  mrg 	  mark_use_interesting (name, stmt, e->src, insert_phi_p);
   2769  1.1  mrg 	}
   2770  1.1  mrg       else
   2771  1.1  mrg 	{
   2772  1.1  mrg 	  /* For regular statements, mark this as an interesting use
   2773  1.1  mrg 	     for NAME.  */
   2774  1.1  mrg 	  mark_use_interesting (name, stmt, bb, insert_phi_p);
   2775  1.1  mrg 	}
   2776  1.1  mrg     }
   2777  1.1  mrg }
   2778  1.1  mrg 
   2779  1.1  mrg 
   2780  1.1  mrg /* Helper for prepare_names_to_update.  Mark the definition site for
   2781  1.1  mrg    NAME as interesting.  BLOCKS and INSERT_PHI_P are as in
   2782  1.1  mrg    prepare_names_to_update.  */
   2783  1.1  mrg 
   2784  1.1  mrg static void
   2785  1.1  mrg prepare_def_site_for (tree name, bool insert_phi_p)
   2786  1.1  mrg {
   2787  1.1  mrg   gimple *stmt;
   2788  1.1  mrg   basic_block bb;
   2789  1.1  mrg 
   2790  1.1  mrg   gcc_checking_assert (names_to_release == NULL
   2791  1.1  mrg 		       || !bitmap_bit_p (names_to_release,
   2792  1.1  mrg 					 SSA_NAME_VERSION (name)));
   2793  1.1  mrg 
   2794  1.1  mrg   /* If we rename virtual operands do not update them.  */
   2795  1.1  mrg   if (virtual_operand_p (name)
   2796  1.1  mrg       && cfun->gimple_df->rename_vops)
   2797  1.1  mrg     return;
   2798  1.1  mrg 
   2799  1.1  mrg   stmt = SSA_NAME_DEF_STMT (name);
   2800  1.1  mrg   bb = gimple_bb (stmt);
   2801  1.1  mrg   if (bb)
   2802  1.1  mrg     {
   2803  1.1  mrg       gcc_checking_assert (bb->index < last_basic_block_for_fn (cfun));
   2804  1.1  mrg       mark_block_for_update (bb);
   2805  1.1  mrg       mark_def_interesting (name, stmt, bb, insert_phi_p);
   2806  1.1  mrg     }
   2807  1.1  mrg }
   2808  1.1  mrg 
   2809  1.1  mrg 
   2810  1.1  mrg /* Mark definition and use sites of names in NEW_SSA_NAMES and
   2811  1.1  mrg    OLD_SSA_NAMES.  INSERT_PHI_P is true if the caller wants to insert
   2812  1.1  mrg    PHI nodes for newly created names.  */
   2813  1.1  mrg 
   2814  1.1  mrg static void
   2815  1.1  mrg prepare_names_to_update (bool insert_phi_p)
   2816  1.1  mrg {
   2817  1.1  mrg   unsigned i = 0;
   2818  1.1  mrg   bitmap_iterator bi;
   2819  1.1  mrg   sbitmap_iterator sbi;
   2820  1.1  mrg 
   2821  1.1  mrg   /* If a name N from NEW_SSA_NAMES is also marked to be released,
   2822  1.1  mrg      remove it from NEW_SSA_NAMES so that we don't try to visit its
   2823  1.1  mrg      defining basic block (which most likely doesn't exist).  Notice
   2824  1.1  mrg      that we cannot do the same with names in OLD_SSA_NAMES because we
   2825  1.1  mrg      want to replace existing instances.  */
   2826  1.1  mrg   if (names_to_release)
   2827  1.1  mrg     EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
   2828  1.1  mrg       bitmap_clear_bit (new_ssa_names, i);
   2829  1.1  mrg 
   2830  1.1  mrg   /* First process names in NEW_SSA_NAMES.  Otherwise, uses of old
   2831  1.1  mrg      names may be considered to be live-in on blocks that contain
   2832  1.1  mrg      definitions for their replacements.  */
   2833  1.1  mrg   EXECUTE_IF_SET_IN_BITMAP (new_ssa_names, 0, i, sbi)
   2834  1.1  mrg     prepare_def_site_for (ssa_name (i), insert_phi_p);
   2835  1.1  mrg 
   2836  1.1  mrg   /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
   2837  1.1  mrg      OLD_SSA_NAMES, but we have to ignore its definition site.  */
   2838  1.1  mrg   EXECUTE_IF_SET_IN_BITMAP (old_ssa_names, 0, i, sbi)
   2839  1.1  mrg     {
   2840  1.1  mrg       if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i))
   2841  1.1  mrg 	prepare_def_site_for (ssa_name (i), insert_phi_p);
   2842  1.1  mrg       prepare_use_sites_for (ssa_name (i), insert_phi_p);
   2843  1.1  mrg     }
   2844  1.1  mrg }
   2845  1.1  mrg 
   2846  1.1  mrg 
   2847  1.1  mrg /* Dump all the names replaced by NAME to FILE.  */
   2848  1.1  mrg 
   2849  1.1  mrg void
   2850  1.1  mrg dump_names_replaced_by (FILE *file, tree name)
   2851  1.1  mrg {
   2852  1.1  mrg   unsigned i;
   2853  1.1  mrg   bitmap old_set;
   2854  1.1  mrg   bitmap_iterator bi;
   2855  1.1  mrg 
   2856  1.1  mrg   print_generic_expr (file, name);
   2857  1.1  mrg   fprintf (file, " -> { ");
   2858  1.1  mrg 
   2859  1.1  mrg   old_set = names_replaced_by (name);
   2860  1.1  mrg   EXECUTE_IF_SET_IN_BITMAP (old_set, 0, i, bi)
   2861  1.1  mrg     {
   2862  1.1  mrg       print_generic_expr (file, ssa_name (i));
   2863  1.1  mrg       fprintf (file, " ");
   2864  1.1  mrg     }
   2865  1.1  mrg 
   2866  1.1  mrg   fprintf (file, "}\n");
   2867  1.1  mrg }
   2868  1.1  mrg 
   2869  1.1  mrg 
   2870  1.1  mrg /* Dump all the names replaced by NAME to stderr.  */
   2871  1.1  mrg 
   2872  1.1  mrg DEBUG_FUNCTION void
   2873  1.1  mrg debug_names_replaced_by (tree name)
   2874  1.1  mrg {
   2875  1.1  mrg   dump_names_replaced_by (stderr, name);
   2876  1.1  mrg }
   2877  1.1  mrg 
   2878  1.1  mrg 
   2879  1.1  mrg /* Dump SSA update information to FILE.  */
   2880  1.1  mrg 
   2881  1.1  mrg void
   2882  1.1  mrg dump_update_ssa (FILE *file)
   2883  1.1  mrg {
   2884  1.1  mrg   unsigned i = 0;
   2885  1.1  mrg   bitmap_iterator bi;
   2886  1.1  mrg 
   2887  1.1  mrg   if (!need_ssa_update_p (cfun))
   2888  1.1  mrg     return;
   2889  1.1  mrg 
   2890  1.1  mrg   if (new_ssa_names && bitmap_first_set_bit (new_ssa_names) >= 0)
   2891  1.1  mrg     {
   2892  1.1  mrg       sbitmap_iterator sbi;
   2893  1.1  mrg 
   2894  1.1  mrg       fprintf (file, "\nSSA replacement table\n");
   2895  1.1  mrg       fprintf (file, "N_i -> { O_1 ... O_j } means that N_i replaces "
   2896  1.1  mrg 	             "O_1, ..., O_j\n\n");
   2897  1.1  mrg 
   2898  1.1  mrg       EXECUTE_IF_SET_IN_BITMAP (new_ssa_names, 0, i, sbi)
   2899  1.1  mrg 	dump_names_replaced_by (file, ssa_name (i));
   2900  1.1  mrg     }
   2901  1.1  mrg 
   2902  1.1  mrg   if (symbols_to_rename_set && !bitmap_empty_p (symbols_to_rename_set))
   2903  1.1  mrg     {
   2904  1.1  mrg       fprintf (file, "\nSymbols to be put in SSA form\n");
   2905  1.1  mrg       dump_decl_set (file, symbols_to_rename_set);
   2906  1.1  mrg       fprintf (file, "\n");
   2907  1.1  mrg     }
   2908  1.1  mrg 
   2909  1.1  mrg   if (names_to_release && !bitmap_empty_p (names_to_release))
   2910  1.1  mrg     {
   2911  1.1  mrg       fprintf (file, "\nSSA names to release after updating the SSA web\n\n");
   2912  1.1  mrg       EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
   2913  1.1  mrg 	{
   2914  1.1  mrg 	  print_generic_expr (file, ssa_name (i));
   2915  1.1  mrg 	  fprintf (file, " ");
   2916  1.1  mrg 	}
   2917  1.1  mrg       fprintf (file, "\n");
   2918  1.1  mrg     }
   2919  1.1  mrg }
   2920  1.1  mrg 
   2921  1.1  mrg 
   2922  1.1  mrg /* Dump SSA update information to stderr.  */
   2923  1.1  mrg 
   2924  1.1  mrg DEBUG_FUNCTION void
   2925  1.1  mrg debug_update_ssa (void)
   2926  1.1  mrg {
   2927  1.1  mrg   dump_update_ssa (stderr);
   2928  1.1  mrg }
   2929  1.1  mrg 
   2930  1.1  mrg 
   2931  1.1  mrg /* Initialize data structures used for incremental SSA updates.  */
   2932  1.1  mrg 
   2933  1.1  mrg static void
   2934  1.1  mrg init_update_ssa (struct function *fn)
   2935  1.1  mrg {
   2936  1.1  mrg   /* Reserve more space than the current number of names.  The calls to
   2937  1.1  mrg      add_new_name_mapping are typically done after creating new SSA
   2938  1.1  mrg      names, so we'll need to reallocate these arrays.  */
   2939  1.1  mrg   old_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
   2940  1.1  mrg   bitmap_clear (old_ssa_names);
   2941  1.1  mrg 
   2942  1.1  mrg   new_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
   2943  1.1  mrg   bitmap_clear (new_ssa_names);
   2944  1.1  mrg 
   2945  1.1  mrg   bitmap_obstack_initialize (&update_ssa_obstack);
   2946  1.1  mrg 
   2947  1.1  mrg   names_to_release = NULL;
   2948  1.1  mrg   update_ssa_initialized_fn = fn;
   2949  1.1  mrg }
   2950  1.1  mrg 
   2951  1.1  mrg 
   2952  1.1  mrg /* Deallocate data structures used for incremental SSA updates.  */
   2953  1.1  mrg 
   2954  1.1  mrg void
   2955  1.1  mrg delete_update_ssa (void)
   2956  1.1  mrg {
   2957  1.1  mrg   unsigned i;
   2958  1.1  mrg   bitmap_iterator bi;
   2959  1.1  mrg 
   2960  1.1  mrg   sbitmap_free (old_ssa_names);
   2961  1.1  mrg   old_ssa_names = NULL;
   2962  1.1  mrg 
   2963  1.1  mrg   sbitmap_free (new_ssa_names);
   2964  1.1  mrg   new_ssa_names = NULL;
   2965  1.1  mrg 
   2966  1.1  mrg   BITMAP_FREE (symbols_to_rename_set);
   2967  1.1  mrg   symbols_to_rename_set = NULL;
   2968  1.1  mrg   symbols_to_rename.release ();
   2969  1.1  mrg 
   2970  1.1  mrg   if (names_to_release)
   2971  1.1  mrg     {
   2972  1.1  mrg       EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
   2973  1.1  mrg 	release_ssa_name (ssa_name (i));
   2974  1.1  mrg       BITMAP_FREE (names_to_release);
   2975  1.1  mrg     }
   2976  1.1  mrg 
   2977  1.1  mrg   clear_ssa_name_info ();
   2978  1.1  mrg 
   2979  1.1  mrg   fini_ssa_renamer ();
   2980  1.1  mrg 
   2981  1.1  mrg   if (blocks_with_phis_to_rewrite)
   2982  1.1  mrg     EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite, 0, i, bi)
   2983  1.1  mrg       phis_to_rewrite[i].release ();
   2984  1.1  mrg 
   2985  1.1  mrg   BITMAP_FREE (blocks_with_phis_to_rewrite);
   2986  1.1  mrg   BITMAP_FREE (blocks_to_update);
   2987  1.1  mrg 
   2988  1.1  mrg   update_ssa_initialized_fn = NULL;
   2989  1.1  mrg }
   2990  1.1  mrg 
   2991  1.1  mrg 
   2992  1.1  mrg /* Create a new name for OLD_NAME in statement STMT and replace the
   2993  1.1  mrg    operand pointed to by DEF_P with the newly created name.  If DEF_P
   2994  1.1  mrg    is NULL then STMT should be a GIMPLE assignment.
   2995  1.1  mrg    Return the new name and register the replacement mapping <NEW, OLD> in
   2996  1.1  mrg    update_ssa's tables.  */
   2997  1.1  mrg 
   2998  1.1  mrg tree
   2999  1.1  mrg create_new_def_for (tree old_name, gimple *stmt, def_operand_p def)
   3000  1.1  mrg {
   3001  1.1  mrg   tree new_name;
   3002  1.1  mrg 
   3003  1.1  mrg   timevar_push (TV_TREE_SSA_INCREMENTAL);
   3004  1.1  mrg 
   3005  1.1  mrg   if (!update_ssa_initialized_fn)
   3006  1.1  mrg     init_update_ssa (cfun);
   3007  1.1  mrg 
   3008  1.1  mrg   gcc_assert (update_ssa_initialized_fn == cfun);
   3009  1.1  mrg 
   3010  1.1  mrg   new_name = duplicate_ssa_name (old_name, stmt);
   3011  1.1  mrg   if (def)
   3012  1.1  mrg     SET_DEF (def, new_name);
   3013  1.1  mrg   else
   3014  1.1  mrg     gimple_assign_set_lhs (stmt, new_name);
   3015  1.1  mrg 
   3016  1.1  mrg   if (gimple_code (stmt) == GIMPLE_PHI)
   3017  1.1  mrg     {
   3018  1.1  mrg       basic_block bb = gimple_bb (stmt);
   3019  1.1  mrg 
   3020  1.1  mrg       /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
   3021  1.1  mrg       SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = bb_has_abnormal_pred (bb);
   3022  1.1  mrg     }
   3023  1.1  mrg 
   3024  1.1  mrg   add_new_name_mapping (new_name, old_name);
   3025  1.1  mrg 
   3026  1.1  mrg   /* For the benefit of passes that will be updating the SSA form on
   3027  1.1  mrg      their own, set the current reaching definition of OLD_NAME to be
   3028  1.1  mrg      NEW_NAME.  */
   3029  1.1  mrg   get_ssa_name_ann (old_name)->info.current_def = new_name;
   3030  1.1  mrg 
   3031  1.1  mrg   timevar_pop (TV_TREE_SSA_INCREMENTAL);
   3032  1.1  mrg 
   3033  1.1  mrg   return new_name;
   3034  1.1  mrg }
   3035  1.1  mrg 
   3036  1.1  mrg 
   3037  1.1  mrg /* Mark virtual operands of FN for renaming by update_ssa.  */
   3038  1.1  mrg 
   3039  1.1  mrg void
   3040  1.1  mrg mark_virtual_operands_for_renaming (struct function *fn)
   3041  1.1  mrg {
   3042  1.1  mrg   fn->gimple_df->ssa_renaming_needed = 1;
   3043  1.1  mrg   fn->gimple_df->rename_vops = 1;
   3044  1.1  mrg }
   3045  1.1  mrg 
   3046  1.1  mrg /* Replace all uses of NAME by underlying variable and mark it
   3047  1.1  mrg    for renaming.  This assumes the defining statement of NAME is
   3048  1.1  mrg    going to be removed.  */
   3049  1.1  mrg 
   3050  1.1  mrg void
   3051  1.1  mrg mark_virtual_operand_for_renaming (tree name)
   3052  1.1  mrg {
   3053  1.1  mrg   tree name_var = SSA_NAME_VAR (name);
   3054  1.1  mrg   bool used = false;
   3055  1.1  mrg   imm_use_iterator iter;
   3056  1.1  mrg   use_operand_p use_p;
   3057  1.1  mrg   gimple *stmt;
   3058  1.1  mrg 
   3059  1.1  mrg   gcc_assert (VAR_DECL_IS_VIRTUAL_OPERAND (name_var));
   3060  1.1  mrg   FOR_EACH_IMM_USE_STMT (stmt, iter, name)
   3061  1.1  mrg     {
   3062  1.1  mrg       FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
   3063  1.1  mrg         SET_USE (use_p, name_var);
   3064  1.1  mrg       used = true;
   3065  1.1  mrg     }
   3066  1.1  mrg   if (used)
   3067  1.1  mrg     mark_virtual_operands_for_renaming (cfun);
   3068  1.1  mrg }
   3069  1.1  mrg 
   3070  1.1  mrg /* Replace all uses of the virtual PHI result by its underlying variable
   3071  1.1  mrg    and mark it for renaming.  This assumes the PHI node is going to be
   3072  1.1  mrg    removed.  */
   3073  1.1  mrg 
   3074  1.1  mrg void
   3075  1.1  mrg mark_virtual_phi_result_for_renaming (gphi *phi)
   3076  1.1  mrg {
   3077  1.1  mrg   if (dump_file && (dump_flags & TDF_DETAILS))
   3078  1.1  mrg     {
   3079  1.1  mrg       fprintf (dump_file, "Marking result for renaming : ");
   3080  1.1  mrg       print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
   3081  1.1  mrg       fprintf (dump_file, "\n");
   3082  1.1  mrg     }
   3083  1.1  mrg 
   3084  1.1  mrg   mark_virtual_operand_for_renaming (gimple_phi_result (phi));
   3085  1.1  mrg }
   3086  1.1  mrg 
   3087  1.1  mrg /* Return true if there is any work to be done by update_ssa
   3088  1.1  mrg    for function FN.  */
   3089  1.1  mrg 
   3090  1.1  mrg bool
   3091  1.1  mrg need_ssa_update_p (struct function *fn)
   3092  1.1  mrg {
   3093  1.1  mrg   gcc_assert (fn != NULL);
   3094  1.1  mrg   return (update_ssa_initialized_fn == fn
   3095  1.1  mrg 	  || (fn->gimple_df && fn->gimple_df->ssa_renaming_needed));
   3096  1.1  mrg }
   3097  1.1  mrg 
   3098  1.1  mrg /* Return true if name N has been registered in the replacement table.  */
   3099  1.1  mrg 
   3100  1.1  mrg bool
   3101  1.1  mrg name_registered_for_update_p (tree n ATTRIBUTE_UNUSED)
   3102  1.1  mrg {
   3103  1.1  mrg   if (!update_ssa_initialized_fn)
   3104  1.1  mrg     return false;
   3105  1.1  mrg 
   3106  1.1  mrg   gcc_assert (update_ssa_initialized_fn == cfun);
   3107  1.1  mrg 
   3108  1.1  mrg   return is_new_name (n) || is_old_name (n);
   3109  1.1  mrg }
   3110  1.1  mrg 
   3111  1.1  mrg 
   3112  1.1  mrg /* Mark NAME to be released after update_ssa has finished.  */
   3113  1.1  mrg 
   3114  1.1  mrg void
   3115  1.1  mrg release_ssa_name_after_update_ssa (tree name)
   3116  1.1  mrg {
   3117  1.1  mrg   gcc_assert (cfun && update_ssa_initialized_fn == cfun);
   3118  1.1  mrg 
   3119  1.1  mrg   if (names_to_release == NULL)
   3120  1.1  mrg     names_to_release = BITMAP_ALLOC (NULL);
   3121  1.1  mrg 
   3122  1.1  mrg   bitmap_set_bit (names_to_release, SSA_NAME_VERSION (name));
   3123  1.1  mrg }
   3124  1.1  mrg 
   3125  1.1  mrg 
   3126  1.1  mrg /* Insert new PHI nodes to replace VAR.  DFS contains dominance
   3127  1.1  mrg    frontier information.  BLOCKS is the set of blocks to be updated.
   3128  1.1  mrg 
   3129  1.1  mrg    This is slightly different than the regular PHI insertion
   3130  1.1  mrg    algorithm.  The value of UPDATE_FLAGS controls how PHI nodes for
   3131  1.1  mrg    real names (i.e., GIMPLE registers) are inserted:
   3132  1.1  mrg 
   3133  1.1  mrg    - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
   3134  1.1  mrg      nodes inside the region affected by the block that defines VAR
   3135  1.1  mrg      and the blocks that define all its replacements.  All these
   3136  1.1  mrg      definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
   3137  1.1  mrg 
   3138  1.1  mrg      First, we compute the entry point to the region (ENTRY).  This is
   3139  1.1  mrg      given by the nearest common dominator to all the definition
   3140  1.1  mrg      blocks. When computing the iterated dominance frontier (IDF), any
   3141  1.1  mrg      block not strictly dominated by ENTRY is ignored.
   3142  1.1  mrg 
   3143  1.1  mrg      We then call the standard PHI insertion algorithm with the pruned
   3144  1.1  mrg      IDF.
   3145  1.1  mrg 
   3146  1.1  mrg    - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
   3147  1.1  mrg      names is not pruned.  PHI nodes are inserted at every IDF block.  */
   3148  1.1  mrg 
   3149  1.1  mrg static void
   3150  1.1  mrg insert_updated_phi_nodes_for (tree var, bitmap_head *dfs, bitmap blocks,
   3151  1.1  mrg                               unsigned update_flags)
   3152  1.1  mrg {
   3153  1.1  mrg   basic_block entry;
   3154  1.1  mrg   def_blocks *db;
   3155  1.1  mrg   bitmap idf, pruned_idf;
   3156  1.1  mrg   bitmap_iterator bi;
   3157  1.1  mrg   unsigned i;
   3158  1.1  mrg 
   3159  1.1  mrg   if (TREE_CODE (var) == SSA_NAME)
   3160  1.1  mrg     gcc_checking_assert (is_old_name (var));
   3161  1.1  mrg   else
   3162  1.1  mrg     gcc_checking_assert (marked_for_renaming (var));
   3163  1.1  mrg 
   3164  1.1  mrg   /* Get all the definition sites for VAR.  */
   3165  1.1  mrg   db = find_def_blocks_for (var);
   3166  1.1  mrg 
   3167  1.1  mrg   /* No need to do anything if there were no definitions to VAR.  */
   3168  1.1  mrg   if (db == NULL || bitmap_empty_p (db->def_blocks))
   3169  1.1  mrg     return;
   3170  1.1  mrg 
   3171  1.1  mrg   /* Compute the initial iterated dominance frontier.  */
   3172  1.1  mrg   idf = compute_idf (db->def_blocks, dfs);
   3173  1.1  mrg   pruned_idf = BITMAP_ALLOC (NULL);
   3174  1.1  mrg 
   3175  1.1  mrg   if (TREE_CODE (var) == SSA_NAME)
   3176  1.1  mrg     {
   3177  1.1  mrg       if (update_flags == TODO_update_ssa)
   3178  1.1  mrg 	{
   3179  1.1  mrg 	  /* If doing regular SSA updates for GIMPLE registers, we are
   3180  1.1  mrg 	     only interested in IDF blocks dominated by the nearest
   3181  1.1  mrg 	     common dominator of all the definition blocks.  */
   3182  1.1  mrg 	  entry = nearest_common_dominator_for_set (CDI_DOMINATORS,
   3183  1.1  mrg 						    db->def_blocks);
   3184  1.1  mrg 	  if (entry != ENTRY_BLOCK_PTR_FOR_FN (cfun))
   3185  1.1  mrg 	    EXECUTE_IF_SET_IN_BITMAP (idf, 0, i, bi)
   3186  1.1  mrg 	      if (BASIC_BLOCK_FOR_FN (cfun, i) != entry
   3187  1.1  mrg 		  && dominated_by_p (CDI_DOMINATORS,
   3188  1.1  mrg 				     BASIC_BLOCK_FOR_FN (cfun, i), entry))
   3189  1.1  mrg 		bitmap_set_bit (pruned_idf, i);
   3190  1.1  mrg 	}
   3191  1.1  mrg       else
   3192  1.1  mrg 	{
   3193  1.1  mrg 	  /* Otherwise, do not prune the IDF for VAR.  */
   3194  1.1  mrg 	  gcc_checking_assert (update_flags == TODO_update_ssa_full_phi);
   3195  1.1  mrg 	  bitmap_copy (pruned_idf, idf);
   3196  1.1  mrg 	}
   3197  1.1  mrg     }
   3198  1.1  mrg   else
   3199  1.1  mrg     {
   3200  1.1  mrg       /* Otherwise, VAR is a symbol that needs to be put into SSA form
   3201  1.1  mrg 	 for the first time, so we need to compute the full IDF for
   3202  1.1  mrg 	 it.  */
   3203  1.1  mrg       bitmap_copy (pruned_idf, idf);
   3204  1.1  mrg     }
   3205  1.1  mrg 
   3206  1.1  mrg   if (!bitmap_empty_p (pruned_idf))
   3207  1.1  mrg     {
   3208  1.1  mrg       /* Make sure that PRUNED_IDF blocks and all their feeding blocks
   3209  1.1  mrg 	 are included in the region to be updated.  The feeding blocks
   3210  1.1  mrg 	 are important to guarantee that the PHI arguments are renamed
   3211  1.1  mrg 	 properly.  */
   3212  1.1  mrg 
   3213  1.1  mrg       /* FIXME, this is not needed if we are updating symbols.  We are
   3214  1.1  mrg 	 already starting at the ENTRY block anyway.  */
   3215  1.1  mrg       bitmap_ior_into (blocks, pruned_idf);
   3216  1.1  mrg       EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi)
   3217  1.1  mrg 	{
   3218  1.1  mrg 	  edge e;
   3219  1.1  mrg 	  edge_iterator ei;
   3220  1.1  mrg 	  basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
   3221  1.1  mrg 
   3222  1.1  mrg 	  FOR_EACH_EDGE (e, ei, bb->preds)
   3223  1.1  mrg 	    if (e->src->index >= 0)
   3224  1.1  mrg 	      bitmap_set_bit (blocks, e->src->index);
   3225  1.1  mrg 	}
   3226  1.1  mrg 
   3227  1.1  mrg       insert_phi_nodes_for (var, pruned_idf, true);
   3228  1.1  mrg     }
   3229  1.1  mrg 
   3230  1.1  mrg   BITMAP_FREE (pruned_idf);
   3231  1.1  mrg   BITMAP_FREE (idf);
   3232  1.1  mrg }
   3233  1.1  mrg 
   3234  1.1  mrg /* Sort symbols_to_rename after their DECL_UID.  */
   3235  1.1  mrg 
   3236  1.1  mrg static int
   3237  1.1  mrg insert_updated_phi_nodes_compare_uids (const void *a, const void *b)
   3238  1.1  mrg {
   3239  1.1  mrg   const_tree syma = *(const const_tree *)a;
   3240  1.1  mrg   const_tree symb = *(const const_tree *)b;
   3241  1.1  mrg   if (DECL_UID (syma) == DECL_UID (symb))
   3242  1.1  mrg     return 0;
   3243  1.1  mrg   return DECL_UID (syma) < DECL_UID (symb) ? -1 : 1;
   3244  1.1  mrg }
   3245  1.1  mrg 
   3246  1.1  mrg /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
   3247  1.1  mrg    existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
   3248  1.1  mrg 
   3249  1.1  mrg    1- The names in OLD_SSA_NAMES dominated by the definitions of
   3250  1.1  mrg       NEW_SSA_NAMES are all re-written to be reached by the
   3251  1.1  mrg       appropriate definition from NEW_SSA_NAMES.
   3252  1.1  mrg 
   3253  1.1  mrg    2- If needed, new PHI nodes are added to the iterated dominance
   3254  1.1  mrg       frontier of the blocks where each of NEW_SSA_NAMES are defined.
   3255  1.1  mrg 
   3256  1.1  mrg    The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
   3257  1.1  mrg    calling create_new_def_for to create new defs for names that the
   3258  1.1  mrg    caller wants to replace.
   3259  1.1  mrg 
   3260  1.1  mrg    The caller cretaes the new names to be inserted and the names that need
   3261  1.1  mrg    to be replaced by calling create_new_def_for for each old definition
   3262  1.1  mrg    to be replaced.  Note that the function assumes that the
   3263  1.1  mrg    new defining statement has already been inserted in the IL.
   3264  1.1  mrg 
   3265  1.1  mrg    For instance, given the following code:
   3266  1.1  mrg 
   3267  1.1  mrg      1	L0:
   3268  1.1  mrg      2	x_1 = PHI (0, x_5)
   3269  1.1  mrg      3	if (x_1 < 10)
   3270  1.1  mrg      4	  if (x_1 > 7)
   3271  1.1  mrg      5	    y_2 = 0
   3272  1.1  mrg      6	  else
   3273  1.1  mrg      7	    y_3 = x_1 + x_7
   3274  1.1  mrg      8	  endif
   3275  1.1  mrg      9	  x_5 = x_1 + 1
   3276  1.1  mrg      10   goto L0;
   3277  1.1  mrg      11	endif
   3278  1.1  mrg 
   3279  1.1  mrg    Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
   3280  1.1  mrg 
   3281  1.1  mrg      1	L0:
   3282  1.1  mrg      2	x_1 = PHI (0, x_5)
   3283  1.1  mrg      3	if (x_1 < 10)
   3284  1.1  mrg      4	  x_10 = ...
   3285  1.1  mrg      5	  if (x_1 > 7)
   3286  1.1  mrg      6	    y_2 = 0
   3287  1.1  mrg      7	  else
   3288  1.1  mrg      8	    x_11 = ...
   3289  1.1  mrg      9	    y_3 = x_1 + x_7
   3290  1.1  mrg      10	  endif
   3291  1.1  mrg      11	  x_5 = x_1 + 1
   3292  1.1  mrg      12	  goto L0;
   3293  1.1  mrg      13	endif
   3294  1.1  mrg 
   3295  1.1  mrg    We want to replace all the uses of x_1 with the new definitions of
   3296  1.1  mrg    x_10 and x_11.  Note that the only uses that should be replaced are
   3297  1.1  mrg    those at lines 5, 9 and 11.  Also, the use of x_7 at line 9 should
   3298  1.1  mrg    *not* be replaced (this is why we cannot just mark symbol 'x' for
   3299  1.1  mrg    renaming).
   3300  1.1  mrg 
   3301  1.1  mrg    Additionally, we may need to insert a PHI node at line 11 because
   3302  1.1  mrg    that is a merge point for x_10 and x_11.  So the use of x_1 at line
   3303  1.1  mrg    11 will be replaced with the new PHI node.  The insertion of PHI
   3304  1.1  mrg    nodes is optional.  They are not strictly necessary to preserve the
   3305  1.1  mrg    SSA form, and depending on what the caller inserted, they may not
   3306  1.1  mrg    even be useful for the optimizers.  UPDATE_FLAGS controls various
   3307  1.1  mrg    aspects of how update_ssa operates, see the documentation for
   3308  1.1  mrg    TODO_update_ssa*.  */
   3309  1.1  mrg 
   3310  1.1  mrg void
   3311  1.1  mrg update_ssa (unsigned update_flags)
   3312  1.1  mrg {
   3313  1.1  mrg   basic_block bb, start_bb;
   3314  1.1  mrg   bitmap_iterator bi;
   3315  1.1  mrg   unsigned i = 0;
   3316  1.1  mrg   bool insert_phi_p;
   3317  1.1  mrg   sbitmap_iterator sbi;
   3318  1.1  mrg   tree sym;
   3319  1.1  mrg 
   3320  1.1  mrg   /* Only one update flag should be set.  */
   3321  1.1  mrg   gcc_assert (update_flags == TODO_update_ssa
   3322  1.1  mrg               || update_flags == TODO_update_ssa_no_phi
   3323  1.1  mrg 	      || update_flags == TODO_update_ssa_full_phi
   3324  1.1  mrg 	      || update_flags == TODO_update_ssa_only_virtuals);
   3325  1.1  mrg 
   3326  1.1  mrg   if (!need_ssa_update_p (cfun))
   3327  1.1  mrg     return;
   3328  1.1  mrg 
   3329  1.1  mrg   if (flag_checking)
   3330  1.1  mrg     {
   3331  1.1  mrg       timevar_push (TV_TREE_STMT_VERIFY);
   3332  1.1  mrg 
   3333  1.1  mrg       bool err = false;
   3334  1.1  mrg 
   3335  1.1  mrg       FOR_EACH_BB_FN (bb, cfun)
   3336  1.1  mrg 	{
   3337  1.1  mrg 	  gimple_stmt_iterator gsi;
   3338  1.1  mrg 	  for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
   3339  1.1  mrg 	    {
   3340  1.1  mrg 	      gimple *stmt = gsi_stmt (gsi);
   3341  1.1  mrg 
   3342  1.1  mrg 	      ssa_op_iter i;
   3343  1.1  mrg 	      use_operand_p use_p;
   3344  1.1  mrg 	      FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_ALL_USES)
   3345  1.1  mrg 		{
   3346  1.1  mrg 		  tree use = USE_FROM_PTR (use_p);
   3347  1.1  mrg 		  if (TREE_CODE (use) != SSA_NAME)
   3348  1.1  mrg 		    continue;
   3349  1.1  mrg 
   3350  1.1  mrg 		  if (SSA_NAME_IN_FREE_LIST (use))
   3351  1.1  mrg 		    {
   3352  1.1  mrg 		      error ("statement uses released SSA name");
   3353  1.1  mrg 		      debug_gimple_stmt (stmt);
   3354  1.1  mrg 		      fprintf (stderr, "The use of ");
   3355  1.1  mrg 		      print_generic_expr (stderr, use);
   3356  1.1  mrg 		      fprintf (stderr," should have been replaced\n");
   3357  1.1  mrg 		      err = true;
   3358  1.1  mrg 		    }
   3359  1.1  mrg 		}
   3360  1.1  mrg 	    }
   3361  1.1  mrg 	}
   3362  1.1  mrg 
   3363  1.1  mrg       if (err)
   3364  1.1  mrg 	internal_error ("cannot update SSA form");
   3365  1.1  mrg 
   3366  1.1  mrg       timevar_pop (TV_TREE_STMT_VERIFY);
   3367  1.1  mrg     }
   3368  1.1  mrg 
   3369  1.1  mrg   timevar_push (TV_TREE_SSA_INCREMENTAL);
   3370  1.1  mrg 
   3371  1.1  mrg   if (dump_file && (dump_flags & TDF_DETAILS))
   3372  1.1  mrg     fprintf (dump_file, "\nUpdating SSA:\n");
   3373  1.1  mrg 
   3374  1.1  mrg   if (!update_ssa_initialized_fn)
   3375  1.1  mrg     init_update_ssa (cfun);
   3376  1.1  mrg   else if (update_flags == TODO_update_ssa_only_virtuals)
   3377  1.1  mrg     {
   3378  1.1  mrg       /* If we only need to update virtuals, remove all the mappings for
   3379  1.1  mrg 	 real names before proceeding.  The caller is responsible for
   3380  1.1  mrg 	 having dealt with the name mappings before calling update_ssa.  */
   3381  1.1  mrg       bitmap_clear (old_ssa_names);
   3382  1.1  mrg       bitmap_clear (new_ssa_names);
   3383  1.1  mrg     }
   3384  1.1  mrg 
   3385  1.1  mrg   gcc_assert (update_ssa_initialized_fn == cfun);
   3386  1.1  mrg 
   3387  1.1  mrg   blocks_with_phis_to_rewrite = BITMAP_ALLOC (NULL);
   3388  1.1  mrg   if (!phis_to_rewrite.exists ())
   3389  1.1  mrg     phis_to_rewrite.create (last_basic_block_for_fn (cfun) + 1);
   3390  1.1  mrg   blocks_to_update = BITMAP_ALLOC (NULL);
   3391  1.1  mrg 
   3392  1.1  mrg   /* Ensure that the dominance information is up-to-date.  */
   3393  1.1  mrg   calculate_dominance_info (CDI_DOMINATORS);
   3394  1.1  mrg 
   3395  1.1  mrg   insert_phi_p = (update_flags != TODO_update_ssa_no_phi);
   3396  1.1  mrg 
   3397  1.1  mrg   /* If there are names defined in the replacement table, prepare
   3398  1.1  mrg      definition and use sites for all the names in NEW_SSA_NAMES and
   3399  1.1  mrg      OLD_SSA_NAMES.  */
   3400  1.1  mrg   if (bitmap_first_set_bit (new_ssa_names) >= 0)
   3401  1.1  mrg     {
   3402  1.1  mrg       statistics_counter_event (cfun, "Incremental SSA update", 1);
   3403  1.1  mrg 
   3404  1.1  mrg       prepare_names_to_update (insert_phi_p);
   3405  1.1  mrg 
   3406  1.1  mrg       /* If all the names in NEW_SSA_NAMES had been marked for
   3407  1.1  mrg 	 removal, and there are no symbols to rename, then there's
   3408  1.1  mrg 	 nothing else to do.  */
   3409  1.1  mrg       if (bitmap_first_set_bit (new_ssa_names) < 0
   3410  1.1  mrg 	  && !cfun->gimple_df->ssa_renaming_needed)
   3411  1.1  mrg 	goto done;
   3412  1.1  mrg     }
   3413  1.1  mrg 
   3414  1.1  mrg   /* Next, determine the block at which to start the renaming process.  */
   3415  1.1  mrg   if (cfun->gimple_df->ssa_renaming_needed)
   3416  1.1  mrg     {
   3417  1.1  mrg       statistics_counter_event (cfun, "Symbol to SSA rewrite", 1);
   3418  1.1  mrg 
   3419  1.1  mrg       /* If we rename bare symbols initialize the mapping to
   3420  1.1  mrg          auxiliar info we need to keep track of.  */
   3421  1.1  mrg       var_infos = new hash_table<var_info_hasher> (47);
   3422  1.1  mrg 
   3423  1.1  mrg       /* If we have to rename some symbols from scratch, we need to
   3424  1.1  mrg 	 start the process at the root of the CFG.  FIXME, it should
   3425  1.1  mrg 	 be possible to determine the nearest block that had a
   3426  1.1  mrg 	 definition for each of the symbols that are marked for
   3427  1.1  mrg 	 updating.  For now this seems more work than it's worth.  */
   3428  1.1  mrg       start_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
   3429  1.1  mrg 
   3430  1.1  mrg       /* Traverse the CFG looking for existing definitions and uses of
   3431  1.1  mrg 	 symbols in SSA operands.  Mark interesting blocks and
   3432  1.1  mrg 	 statements and set local live-in information for the PHI
   3433  1.1  mrg 	 placement heuristics.  */
   3434  1.1  mrg       prepare_block_for_update (start_bb, insert_phi_p);
   3435  1.1  mrg 
   3436  1.1  mrg       tree name;
   3437  1.1  mrg 
   3438  1.1  mrg       if (flag_checking)
   3439  1.1  mrg 	FOR_EACH_SSA_NAME (i, name, cfun)
   3440  1.1  mrg 	  {
   3441  1.1  mrg 	    if (virtual_operand_p (name))
   3442  1.1  mrg 	      continue;
   3443  1.1  mrg 
   3444  1.1  mrg 	    /* For all but virtual operands, which do not have SSA names
   3445  1.1  mrg 	       with overlapping life ranges, ensure that symbols marked
   3446  1.1  mrg 	       for renaming do not have existing SSA names associated with
   3447  1.1  mrg 	       them as we do not re-write them out-of-SSA before going
   3448  1.1  mrg 	       into SSA for the remaining symbol uses.  */
   3449  1.1  mrg 	    if (marked_for_renaming (SSA_NAME_VAR (name)))
   3450  1.1  mrg 	      {
   3451  1.1  mrg 		fprintf (stderr, "Existing SSA name for symbol marked for "
   3452  1.1  mrg 			 "renaming: ");
   3453  1.1  mrg 		print_generic_expr (stderr, name, TDF_SLIM);
   3454  1.1  mrg 		fprintf (stderr, "\n");
   3455  1.1  mrg 		internal_error ("SSA corruption");
   3456  1.1  mrg 	      }
   3457  1.1  mrg 	  }
   3458  1.1  mrg     }
   3459  1.1  mrg   else
   3460  1.1  mrg     {
   3461  1.1  mrg       /* Otherwise, the entry block to the region is the nearest
   3462  1.1  mrg 	 common dominator for the blocks in BLOCKS.  */
   3463  1.1  mrg       start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
   3464  1.1  mrg 						   blocks_to_update);
   3465  1.1  mrg     }
   3466  1.1  mrg 
   3467  1.1  mrg   /* If requested, insert PHI nodes at the iterated dominance frontier
   3468  1.1  mrg      of every block, creating new definitions for names in OLD_SSA_NAMES
   3469  1.1  mrg      and for symbols found.  */
   3470  1.1  mrg   if (insert_phi_p)
   3471  1.1  mrg     {
   3472  1.1  mrg       bitmap_head *dfs;
   3473  1.1  mrg 
   3474  1.1  mrg       /* If the caller requested PHI nodes to be added, compute
   3475  1.1  mrg 	 dominance frontiers.  */
   3476  1.1  mrg       dfs = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
   3477  1.1  mrg       FOR_EACH_BB_FN (bb, cfun)
   3478  1.1  mrg 	bitmap_initialize (&dfs[bb->index], &bitmap_default_obstack);
   3479  1.1  mrg       compute_dominance_frontiers (dfs);
   3480  1.1  mrg 
   3481  1.1  mrg       if (bitmap_first_set_bit (old_ssa_names) >= 0)
   3482  1.1  mrg 	{
   3483  1.1  mrg 	  sbitmap_iterator sbi;
   3484  1.1  mrg 
   3485  1.1  mrg 	  /* insert_update_phi_nodes_for will call add_new_name_mapping
   3486  1.1  mrg 	     when inserting new PHI nodes, so the set OLD_SSA_NAMES
   3487  1.1  mrg 	     will grow while we are traversing it (but it will not
   3488  1.1  mrg 	     gain any new members).  Copy OLD_SSA_NAMES to a temporary
   3489  1.1  mrg 	     for traversal.  */
   3490  1.1  mrg 	  auto_sbitmap tmp (SBITMAP_SIZE (old_ssa_names));
   3491  1.1  mrg 	  bitmap_copy (tmp, old_ssa_names);
   3492  1.1  mrg 	  EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, sbi)
   3493  1.1  mrg 	    insert_updated_phi_nodes_for (ssa_name (i), dfs, blocks_to_update,
   3494  1.1  mrg 	                                  update_flags);
   3495  1.1  mrg 	}
   3496  1.1  mrg 
   3497  1.1  mrg       symbols_to_rename.qsort (insert_updated_phi_nodes_compare_uids);
   3498  1.1  mrg       FOR_EACH_VEC_ELT (symbols_to_rename, i, sym)
   3499  1.1  mrg 	insert_updated_phi_nodes_for (sym, dfs, blocks_to_update,
   3500  1.1  mrg 	                              update_flags);
   3501  1.1  mrg 
   3502  1.1  mrg       FOR_EACH_BB_FN (bb, cfun)
   3503  1.1  mrg 	bitmap_clear (&dfs[bb->index]);
   3504  1.1  mrg       free (dfs);
   3505  1.1  mrg 
   3506  1.1  mrg       /* Insertion of PHI nodes may have added blocks to the region.
   3507  1.1  mrg 	 We need to re-compute START_BB to include the newly added
   3508  1.1  mrg 	 blocks.  */
   3509  1.1  mrg       if (start_bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
   3510  1.1  mrg 	start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
   3511  1.1  mrg 						     blocks_to_update);
   3512  1.1  mrg     }
   3513  1.1  mrg 
   3514  1.1  mrg   /* Reset the current definition for name and symbol before renaming
   3515  1.1  mrg      the sub-graph.  */
   3516  1.1  mrg   EXECUTE_IF_SET_IN_BITMAP (old_ssa_names, 0, i, sbi)
   3517  1.1  mrg     get_ssa_name_ann (ssa_name (i))->info.current_def = NULL_TREE;
   3518  1.1  mrg 
   3519  1.1  mrg   FOR_EACH_VEC_ELT (symbols_to_rename, i, sym)
   3520  1.1  mrg     get_var_info (sym)->info.current_def = NULL_TREE;
   3521  1.1  mrg 
   3522  1.1  mrg   /* Now start the renaming process at START_BB.  */
   3523  1.1  mrg   interesting_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
   3524  1.1  mrg   bitmap_clear (interesting_blocks);
   3525  1.1  mrg   EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
   3526  1.1  mrg     bitmap_set_bit (interesting_blocks, i);
   3527  1.1  mrg 
   3528  1.1  mrg   rewrite_blocks (start_bb, REWRITE_UPDATE);
   3529  1.1  mrg 
   3530  1.1  mrg   sbitmap_free (interesting_blocks);
   3531  1.1  mrg 
   3532  1.1  mrg   /* Debugging dumps.  */
   3533  1.1  mrg   if (dump_file)
   3534  1.1  mrg     {
   3535  1.1  mrg       int c;
   3536  1.1  mrg       unsigned i;
   3537  1.1  mrg 
   3538  1.1  mrg       dump_update_ssa (dump_file);
   3539  1.1  mrg 
   3540  1.1  mrg       fprintf (dump_file, "Incremental SSA update started at block: %d\n",
   3541  1.1  mrg 	       start_bb->index);
   3542  1.1  mrg 
   3543  1.1  mrg       c = 0;
   3544  1.1  mrg       EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
   3545  1.1  mrg 	c++;
   3546  1.1  mrg       fprintf (dump_file, "Number of blocks in CFG: %d\n",
   3547  1.1  mrg 	       last_basic_block_for_fn (cfun));
   3548  1.1  mrg       fprintf (dump_file, "Number of blocks to update: %d (%3.0f%%)\n",
   3549  1.1  mrg 	       c, PERCENT (c, last_basic_block_for_fn (cfun)));
   3550  1.1  mrg 
   3551  1.1  mrg       if (dump_flags & TDF_DETAILS)
   3552  1.1  mrg 	{
   3553  1.1  mrg 	  fprintf (dump_file, "Affected blocks:");
   3554  1.1  mrg 	  EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
   3555  1.1  mrg 	    fprintf (dump_file, " %u", i);
   3556  1.1  mrg 	  fprintf (dump_file, "\n");
   3557  1.1  mrg 	}
   3558  1.1  mrg 
   3559  1.1  mrg       fprintf (dump_file, "\n\n");
   3560  1.1  mrg     }
   3561  1.1  mrg 
   3562  1.1  mrg   /* Free allocated memory.  */
   3563  1.1  mrg done:
   3564  1.1  mrg   delete_update_ssa ();
   3565  1.1  mrg 
   3566  1.1  mrg   timevar_pop (TV_TREE_SSA_INCREMENTAL);
   3567  1.1  mrg }
   3568