• Main Page
  • Modules
  • Data Types
  • Files

osprey/kg++fe/gnu/gcse.c

Go to the documentation of this file.
00001 /* Global common subexpression elimination/Partial redundancy elimination
00002    and global constant/copy propagation for GNU compiler.
00003    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
00004    Free Software Foundation, Inc.
00005 
00006 This file is part of GCC.
00007 
00008 GCC is free software; you can redistribute it and/or modify it under
00009 the terms of the GNU General Public License as published by the Free
00010 Software Foundation; either version 2, or (at your option) any later
00011 version.
00012 
00013 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
00014 WARRANTY; without even the implied warranty of MERCHANTABILITY or
00015 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00016 for more details.
00017 
00018 You should have received a copy of the GNU General Public License
00019 along with GCC; see the file COPYING.  If not, write to the Free
00020 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
00021 02111-1307, USA.  */
00022 
00023 /* TODO
00024    - reordering of memory allocation and freeing to be more space efficient
00025    - do rough calc of how many regs are needed in each block, and a rough
00026      calc of how many regs are available in each class and use that to
00027      throttle back the code in cases where RTX_COST is minimal.
00028    - a store to the same address as a load does not kill the load if the
00029      source of the store is also the destination of the load.  Handling this
00030      allows more load motion, particularly out of loops.
00031    - ability to realloc sbitmap vectors would allow one initial computation
00032      of reg_set_in_block with only subsequent additions, rather than
00033      recomputing it for each pass
00034 
00035 */
00036 
00037 /* References searched while implementing this.
00038 
00039    Compilers Principles, Techniques and Tools
00040    Aho, Sethi, Ullman
00041    Addison-Wesley, 1988
00042 
00043    Global Optimization by Suppression of Partial Redundancies
00044    E. Morel, C. Renvoise
00045    communications of the acm, Vol. 22, Num. 2, Feb. 1979
00046 
00047    A Portable Machine-Independent Global Optimizer - Design and Measurements
00048    Frederick Chow
00049    Stanford Ph.D. thesis, Dec. 1983
00050 
00051    A Fast Algorithm for Code Movement Optimization
00052    D.M. Dhamdhere
00053    SIGPLAN Notices, Vol. 23, Num. 10, Oct. 1988
00054 
00055    A Solution to a Problem with Morel and Renvoise's
00056    Global Optimization by Suppression of Partial Redundancies
00057    K-H Drechsler, M.P. Stadel
00058    ACM TOPLAS, Vol. 10, Num. 4, Oct. 1988
00059 
00060    Practical Adaptation of the Global Optimization
00061    Algorithm of Morel and Renvoise
00062    D.M. Dhamdhere
00063    ACM TOPLAS, Vol. 13, Num. 2. Apr. 1991
00064 
00065    Efficiently Computing Static Single Assignment Form and the Control
00066    Dependence Graph
00067    R. Cytron, J. Ferrante, B.K. Rosen, M.N. Wegman, and F.K. Zadeck
00068    ACM TOPLAS, Vol. 13, Num. 4, Oct. 1991
00069 
00070    Lazy Code Motion
00071    J. Knoop, O. Ruthing, B. Steffen
00072    ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
00073 
00074    What's In a Region?  Or Computing Control Dependence Regions in Near-Linear
00075    Time for Reducible Flow Control
00076    Thomas Ball
00077    ACM Letters on Programming Languages and Systems,
00078    Vol. 2, Num. 1-4, Mar-Dec 1993
00079 
00080    An Efficient Representation for Sparse Sets
00081    Preston Briggs, Linda Torczon
00082    ACM Letters on Programming Languages and Systems,
00083    Vol. 2, Num. 1-4, Mar-Dec 1993
00084 
00085    A Variation of Knoop, Ruthing, and Steffen's Lazy Code Motion
00086    K-H Drechsler, M.P. Stadel
00087    ACM SIGPLAN Notices, Vol. 28, Num. 5, May 1993
00088 
00089    Partial Dead Code Elimination
00090    J. Knoop, O. Ruthing, B. Steffen
00091    ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
00092 
00093    Effective Partial Redundancy Elimination
00094    P. Briggs, K.D. Cooper
00095    ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
00096 
00097    The Program Structure Tree: Computing Control Regions in Linear Time
00098    R. Johnson, D. Pearson, K. Pingali
00099    ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
00100 
00101    Optimal Code Motion: Theory and Practice
00102    J. Knoop, O. Ruthing, B. Steffen
00103    ACM TOPLAS, Vol. 16, Num. 4, Jul. 1994
00104 
00105    The power of assignment motion
00106    J. Knoop, O. Ruthing, B. Steffen
00107    ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
00108 
00109    Global code motion / global value numbering
00110    C. Click
00111    ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
00112 
00113    Value Driven Redundancy Elimination
00114    L.T. Simpson
00115    Rice University Ph.D. thesis, Apr. 1996
00116 
00117    Value Numbering
00118    L.T. Simpson
00119    Massively Scalar Compiler Project, Rice University, Sep. 1996
00120 
00121    High Performance Compilers for Parallel Computing
00122    Michael Wolfe
00123    Addison-Wesley, 1996
00124 
00125    Advanced Compiler Design and Implementation
00126    Steven Muchnick
00127    Morgan Kaufmann, 1997
00128 
00129    Building an Optimizing Compiler
00130    Robert Morgan
00131    Digital Press, 1998
00132 
00133    People wishing to speed up the code here should read:
00134      Elimination Algorithms for Data Flow Analysis
00135      B.G. Ryder, M.C. Paull
00136      ACM Computing Surveys, Vol. 18, Num. 3, Sep. 1986
00137 
00138      How to Analyze Large Programs Efficiently and Informatively
00139      D.M. Dhamdhere, B.K. Rosen, F.K. Zadeck
00140      ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
00141 
00142    People wishing to do something different can find various possibilities
00143    in the above papers and elsewhere.
00144 */
00145 
00146 #include "config.h"
00147 #include "system.h"
00148 #include "toplev.h"
00149 
00150 #include "rtl.h"
00151 #include "tm_p.h"
00152 #include "regs.h"
00153 #include "hard-reg-set.h"
00154 #include "flags.h"
00155 #include "real.h"
00156 #include "insn-config.h"
00157 #include "recog.h"
00158 #include "basic-block.h"
00159 #include "output.h"
00160 #include "function.h"
00161 #include "expr.h"
00162 #include "except.h"
00163 #include "ggc.h"
00164 #include "params.h"
00165 #include "cselib.h"
00166 
00167 #include "obstack.h"
00168 
00169 /* Propagate flow information through back edges and thus enable PRE's
00170    moving loop invariant calculations out of loops.
00171 
00172    Originally this tended to create worse overall code, but several
00173    improvements during the development of PRE seem to have made following
00174    back edges generally a win.
00175 
00176    Note much of the loop invariant code motion done here would normally
00177    be done by loop.c, which has more heuristics for when to move invariants
00178    out of loops.  At some point we might need to move some of those
00179    heuristics into gcse.c.  */
00180 
00181 /* We support GCSE via Partial Redundancy Elimination.  PRE optimizations
00182    are a superset of those done by GCSE.
00183 
00184    We perform the following steps:
00185 
00186    1) Compute basic block information.
00187 
00188    2) Compute table of places where registers are set.
00189 
00190    3) Perform copy/constant propagation.
00191 
00192    4) Perform global cse.
00193 
00194    5) Perform another pass of copy/constant propagation.
00195 
00196    Two passes of copy/constant propagation are done because the first one
00197    enables more GCSE and the second one helps to clean up the copies that
00198    GCSE creates.  This is needed more for PRE than for Classic because Classic
00199    GCSE will try to use an existing register containing the common
00200    subexpression rather than create a new one.  This is harder to do for PRE
00201    because of the code motion (which Classic GCSE doesn't do).
00202 
00203    Expressions we are interested in GCSE-ing are of the form
00204    (set (pseudo-reg) (expression)).
00205    Function want_to_gcse_p says what these are.
00206 
00207    PRE handles moving invariant expressions out of loops (by treating them as
00208    partially redundant).
00209 
00210    Eventually it would be nice to replace cse.c/gcse.c with SSA (static single
00211    assignment) based GVN (global value numbering).  L. T. Simpson's paper
00212    (Rice University) on value numbering is a useful reference for this.
00213 
00214    **********************
00215 
00216    We used to support multiple passes but there are diminishing returns in
00217    doing so.  The first pass usually makes 90% of the changes that are doable.
00218    A second pass can make a few more changes made possible by the first pass.
00219    Experiments show any further passes don't make enough changes to justify
00220    the expense.
00221 
00222    A study of spec92 using an unlimited number of passes:
00223    [1 pass] = 1208 substitutions, [2] = 577, [3] = 202, [4] = 192, [5] = 83,
00224    [6] = 34, [7] = 17, [8] = 9, [9] = 4, [10] = 4, [11] = 2,
00225    [12] = 2, [13] = 1, [15] = 1, [16] = 2, [41] = 1
00226 
00227    It was found doing copy propagation between each pass enables further
00228    substitutions.
00229 
00230    PRE is quite expensive in complicated functions because the DFA can take
00231    awhile to converge.  Hence we only perform one pass.  The parameter max-gcse-passes can
00232    be modified if one wants to experiment.
00233 
00234    **********************
00235 
00236    The steps for PRE are:
00237 
00238    1) Build the hash table of expressions we wish to GCSE (expr_hash_table).
00239 
00240    2) Perform the data flow analysis for PRE.
00241 
00242    3) Delete the redundant instructions
00243 
00244    4) Insert the required copies [if any] that make the partially
00245       redundant instructions fully redundant.
00246 
00247    5) For other reaching expressions, insert an instruction to copy the value
00248       to a newly created pseudo that will reach the redundant instruction.
00249 
00250    The deletion is done first so that when we do insertions we
00251    know which pseudo reg to use.
00252 
00253    Various papers have argued that PRE DFA is expensive (O(n^2)) and others
00254    argue it is not.  The number of iterations for the algorithm to converge
00255    is typically 2-4 so I don't view it as that expensive (relatively speaking).
00256 
00257    PRE GCSE depends heavily on the second CSE pass to clean up the copies
00258    we create.  To make an expression reach the place where it's redundant,
00259    the result of the expression is copied to a new register, and the redundant
00260    expression is deleted by replacing it with this new register.  Classic GCSE
00261    doesn't have this problem as much as it computes the reaching defs of
00262    each register in each block and thus can try to use an existing register.
00263 
00264    **********************
00265 
00266    A fair bit of simplicity is created by creating small functions for simple
00267    tasks, even when the function is only called in one place.  This may
00268    measurably slow things down [or may not] by creating more function call
00269    overhead than is necessary.  The source is laid out so that it's trivial
00270    to make the affected functions inline so that one can measure what speed
00271    up, if any, can be achieved, and maybe later when things settle things can
00272    be rearranged.
00273 
00274    Help stamp out big monolithic functions!  */
00275 
00276 /* GCSE global vars.  */
00277 
00278 /* -dG dump file.  */
00279 static FILE *gcse_file;
00280 
00281 /* Note whether or not we should run jump optimization after gcse.  We
00282    want to do this for two cases.
00283 
00284     * If we changed any jumps via cprop.
00285 
00286     * If we added any labels via edge splitting.  */
00287 
00288 static int run_jump_opt_after_gcse;
00289 
00290 /* Bitmaps are normally not included in debugging dumps.
00291    However it's useful to be able to print them from GDB.
00292    We could create special functions for this, but it's simpler to
00293    just allow passing stderr to the dump_foo fns.  Since stderr can
00294    be a macro, we store a copy here.  */
00295 static FILE *debug_stderr;
00296 
00297 /* An obstack for our working variables.  */
00298 static struct obstack gcse_obstack;
00299 
00300 /* Nonzero for each mode that supports (set (reg) (reg)).
00301    This is trivially true for integer and floating point values.
00302    It may or may not be true for condition codes.  */
00303 static char can_copy_p[(int) NUM_MACHINE_MODES];
00304 
00305 /* Nonzero if can_copy_p has been initialized.  */
00306 static int can_copy_init_p;
00307 
00308 struct reg_use {rtx reg_rtx; };
00309 
00310 /* Hash table of expressions.  */
00311 
00312 struct expr
00313 {
00314   /* The expression (SET_SRC for expressions, PATTERN for assignments).  */
00315   rtx expr;
00316   /* Index in the available expression bitmaps.  */
00317   int bitmap_index;
00318   /* Next entry with the same hash.  */
00319   struct expr *next_same_hash;
00320   /* List of anticipatable occurrences in basic blocks in the function.
00321      An "anticipatable occurrence" is one that is the first occurrence in the
00322      basic block, the operands are not modified in the basic block prior
00323      to the occurrence and the output is not used between the start of
00324      the block and the occurrence.  */
00325   struct occr *antic_occr;
00326   /* List of available occurrence in basic blocks in the function.
00327      An "available occurrence" is one that is the last occurrence in the
00328      basic block and the operands are not modified by following statements in
00329      the basic block [including this insn].  */
00330   struct occr *avail_occr;
00331   /* Non-null if the computation is PRE redundant.
00332      The value is the newly created pseudo-reg to record a copy of the
00333      expression in all the places that reach the redundant copy.  */
00334   rtx reaching_reg;
00335 };
00336 
00337 /* Occurrence of an expression.
00338    There is one per basic block.  If a pattern appears more than once the
00339    last appearance is used [or first for anticipatable expressions].  */
00340 
00341 struct occr
00342 {
00343   /* Next occurrence of this expression.  */
00344   struct occr *next;
00345   /* The insn that computes the expression.  */
00346   rtx insn;
00347   /* Nonzero if this [anticipatable] occurrence has been deleted.  */
00348   char deleted_p;
00349   /* Nonzero if this [available] occurrence has been copied to
00350      reaching_reg.  */
00351   /* ??? This is mutually exclusive with deleted_p, so they could share
00352      the same byte.  */
00353   char copied_p;
00354 };
00355 
00356 /* Expression and copy propagation hash tables.
00357    Each hash table is an array of buckets.
00358    ??? It is known that if it were an array of entries, structure elements
00359    `next_same_hash' and `bitmap_index' wouldn't be necessary.  However, it is
00360    not clear whether in the final analysis a sufficient amount of memory would
00361    be saved as the size of the available expression bitmaps would be larger
00362    [one could build a mapping table without holes afterwards though].
00363    Someday I'll perform the computation and figure it out.  */
00364 
00365 struct hash_table
00366 {
00367   /* The table itself.
00368      This is an array of `expr_hash_table_size' elements.  */
00369   struct expr **table;
00370 
00371   /* Size of the hash table, in elements.  */
00372   unsigned int size;
00373 
00374   /* Number of hash table elements.  */
00375   unsigned int n_elems;
00376 
00377   /* Whether the table is expression of copy propagation one.  */
00378   int set_p;
00379 };
00380 
00381 /* Expression hash table.  */
00382 static struct hash_table expr_hash_table;
00383 
00384 /* Copy propagation hash table.  */
00385 static struct hash_table set_hash_table;
00386 
00387 /* Mapping of uids to cuids.
00388    Only real insns get cuids.  */
00389 static int *uid_cuid;
00390 
00391 /* Highest UID in UID_CUID.  */
00392 static int max_uid;
00393 
00394 /* Get the cuid of an insn.  */
00395 #ifdef ENABLE_CHECKING
00396 #define INSN_CUID(INSN) (INSN_UID (INSN) > max_uid ? (abort (), 0) : uid_cuid[INSN_UID (INSN)])
00397 #else
00398 #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
00399 #endif
00400 
00401 /* Number of cuids.  */
00402 static int max_cuid;
00403 
00404 /* Mapping of cuids to insns.  */
00405 static rtx *cuid_insn;
00406 
00407 /* Get insn from cuid.  */
00408 #define CUID_INSN(CUID) (cuid_insn[CUID])
00409 
00410 /* Maximum register number in function prior to doing gcse + 1.
00411    Registers created during this pass have regno >= max_gcse_regno.
00412    This is named with "gcse" to not collide with global of same name.  */
00413 static unsigned int max_gcse_regno;
00414 
00415 /* Table of registers that are modified.
00416 
00417    For each register, each element is a list of places where the pseudo-reg
00418    is set.
00419 
00420    For simplicity, GCSE is done on sets of pseudo-regs only.  PRE GCSE only
00421    requires knowledge of which blocks kill which regs [and thus could use
00422    a bitmap instead of the lists `reg_set_table' uses].
00423 
00424    `reg_set_table' and could be turned into an array of bitmaps (num-bbs x
00425    num-regs) [however perhaps it may be useful to keep the data as is].  One
00426    advantage of recording things this way is that `reg_set_table' is fairly
00427    sparse with respect to pseudo regs but for hard regs could be fairly dense
00428    [relatively speaking].  And recording sets of pseudo-regs in lists speeds
00429    up functions like compute_transp since in the case of pseudo-regs we only
00430    need to iterate over the number of times a pseudo-reg is set, not over the
00431    number of basic blocks [clearly there is a bit of a slow down in the cases
00432    where a pseudo is set more than once in a block, however it is believed
00433    that the net effect is to speed things up].  This isn't done for hard-regs
00434    because recording call-clobbered hard-regs in `reg_set_table' at each
00435    function call can consume a fair bit of memory, and iterating over
00436    hard-regs stored this way in compute_transp will be more expensive.  */
00437 
00438 typedef struct reg_set
00439 {
00440   /* The next setting of this register.  */
00441   struct reg_set *next;
00442   /* The insn where it was set.  */
00443   rtx insn;
00444 } reg_set;
00445 
00446 static reg_set **reg_set_table;
00447 
00448 /* Size of `reg_set_table'.
00449    The table starts out at max_gcse_regno + slop, and is enlarged as
00450    necessary.  */
00451 static int reg_set_table_size;
00452 
00453 /* Amount to grow `reg_set_table' by when it's full.  */
00454 #define REG_SET_TABLE_SLOP 100
00455 
00456 /* This is a list of expressions which are MEMs and will be used by load
00457    or store motion.
00458    Load motion tracks MEMs which aren't killed by
00459    anything except itself. (ie, loads and stores to a single location).
00460    We can then allow movement of these MEM refs with a little special
00461    allowance. (all stores copy the same value to the reaching reg used
00462    for the loads).  This means all values used to store into memory must have
00463    no side effects so we can re-issue the setter value.
00464    Store Motion uses this structure as an expression table to track stores
00465    which look interesting, and might be moveable towards the exit block.  */
00466 
00467 struct ls_expr
00468 {
00469   struct expr * expr;   /* Gcse expression reference for LM.  */
00470   rtx pattern;      /* Pattern of this mem.  */
00471   rtx loads;      /* INSN list of loads seen.  */
00472   rtx stores;     /* INSN list of stores seen.  */
00473   struct ls_expr * next;  /* Next in the list.  */
00474   int invalid;      /* Invalid for some reason.  */
00475   int index;      /* If it maps to a bitmap index.  */
00476   int hash_index;   /* Index when in a hash table.  */
00477   rtx reaching_reg;   /* Register to use when re-writing.  */
00478 };
00479 
00480 /* Head of the list of load/store memory refs.  */
00481 static struct ls_expr * pre_ldst_mems = NULL;
00482 
00483 /* Bitmap containing one bit for each register in the program.
00484    Used when performing GCSE to track which registers have been set since
00485    the start of the basic block.  */
00486 static regset reg_set_bitmap;
00487 
00488 /* For each block, a bitmap of registers set in the block.
00489    This is used by expr_killed_p and compute_transp.
00490    It is computed during hash table computation and not by compute_sets
00491    as it includes registers added since the last pass (or between cprop and
00492    gcse) and it's currently not easy to realloc sbitmap vectors.  */
00493 static sbitmap *reg_set_in_block;
00494 
00495 /* Array, indexed by basic block number for a list of insns which modify
00496    memory within that block.  */
00497 static rtx * modify_mem_list;
00498 bitmap modify_mem_list_set;
00499 
00500 /* This array parallels modify_mem_list, but is kept canonicalized.  */
00501 static rtx * canon_modify_mem_list;
00502 bitmap canon_modify_mem_list_set;
00503 /* Various variables for statistics gathering.  */
00504 
00505 /* Memory used in a pass.
00506    This isn't intended to be absolutely precise.  Its intent is only
00507    to keep an eye on memory usage.  */
00508 static int bytes_used;
00509 
00510 /* GCSE substitutions made.  */
00511 static int gcse_subst_count;
00512 /* Number of copy instructions created.  */
00513 static int gcse_create_count;
00514 /* Number of constants propagated.  */
00515 static int const_prop_count;
00516 /* Number of copys propagated.  */
00517 static int copy_prop_count;
00518 
00519 /* These variables are used by classic GCSE.
00520    Normally they'd be defined a bit later, but `rd_gen' needs to
00521    be declared sooner.  */
00522 
00523 /* Each block has a bitmap of each type.
00524    The length of each blocks bitmap is:
00525 
00526        max_cuid  - for reaching definitions
00527        n_exprs - for available expressions
00528 
00529    Thus we view the bitmaps as 2 dimensional arrays.  i.e.
00530    rd_kill[block_num][cuid_num]
00531    ae_kill[block_num][expr_num]      */
00532 
00533 /* For reaching defs */
00534 static sbitmap *rd_kill, *rd_gen, *reaching_defs, *rd_out;
00535 
00536 /* for available exprs */
00537 static sbitmap *ae_kill, *ae_gen, *ae_in, *ae_out;
00538 
00539 /* Objects of this type are passed around by the null-pointer check
00540    removal routines.  */
00541 struct null_pointer_info
00542 {
00543   /* The basic block being processed.  */
00544   basic_block current_block;
00545   /* The first register to be handled in this pass.  */
00546   unsigned int min_reg;
00547   /* One greater than the last register to be handled in this pass.  */
00548   unsigned int max_reg;
00549   sbitmap *nonnull_local;
00550   sbitmap *nonnull_killed;
00551 };
00552 
00553 static void compute_can_copy  PARAMS ((void));
00554 static char *gmalloc    PARAMS ((unsigned int));
00555 static char *grealloc   PARAMS ((char *, unsigned int));
00556 static char *gcse_alloc   PARAMS ((unsigned long));
00557 static void alloc_gcse_mem  PARAMS ((rtx));
00558 static void free_gcse_mem PARAMS ((void));
00559 static void alloc_reg_set_mem PARAMS ((int));
00560 static void free_reg_set_mem  PARAMS ((void));
00561 static int get_bitmap_width     PARAMS ((int, int, int));
00562 static void record_one_set  PARAMS ((int, rtx));
00563 static void record_set_info PARAMS ((rtx, rtx, void *));
00564 static void compute_sets  PARAMS ((rtx));
00565 static void hash_scan_insn  PARAMS ((rtx, struct hash_table *, int));
00566 static void hash_scan_set PARAMS ((rtx, rtx, struct hash_table *));
00567 static void hash_scan_clobber PARAMS ((rtx, rtx, struct hash_table *));
00568 static void hash_scan_call  PARAMS ((rtx, rtx, struct hash_table *));
00569 static int want_to_gcse_p PARAMS ((rtx));
00570 static int oprs_unchanged_p PARAMS ((rtx, rtx, int));
00571 static int oprs_anticipatable_p PARAMS ((rtx, rtx));
00572 static int oprs_available_p PARAMS ((rtx, rtx));
00573 static void insert_expr_in_table PARAMS ((rtx, enum machine_mode, rtx,
00574             int, int, struct hash_table *));
00575 static void insert_set_in_table PARAMS ((rtx, rtx, struct hash_table *));
00576 static unsigned int hash_expr PARAMS ((rtx, enum machine_mode, int *, int));
00577 static unsigned int hash_expr_1 PARAMS ((rtx, enum machine_mode, int *));
00578 static unsigned int hash_string_1 PARAMS ((const char *));
00579 static unsigned int hash_set  PARAMS ((int, int));
00580 static int expr_equiv_p         PARAMS ((rtx, rtx));
00581 static void record_last_reg_set_info PARAMS ((rtx, int));
00582 static void record_last_mem_set_info PARAMS ((rtx));
00583 static void record_last_set_info PARAMS ((rtx, rtx, void *));
00584 static void compute_hash_table  PARAMS ((struct hash_table *));
00585 static void alloc_hash_table PARAMS ((int, struct hash_table *, int));
00586 static void free_hash_table PARAMS ((struct hash_table *));
00587 static void compute_hash_table_work PARAMS ((struct hash_table *));
00588 static void dump_hash_table PARAMS ((FILE *, const char *,
00589           struct hash_table *));
00590 static struct expr *lookup_expr PARAMS ((rtx, struct hash_table *));
00591 static struct expr *lookup_set  PARAMS ((unsigned int, rtx, struct hash_table *));
00592 static struct expr *next_set  PARAMS ((unsigned int, struct expr *));
00593 static void reset_opr_set_tables PARAMS ((void));
00594 static int oprs_not_set_p PARAMS ((rtx, rtx));
00595 static void mark_call   PARAMS ((rtx));
00596 static void mark_set    PARAMS ((rtx, rtx));
00597 static void mark_clobber  PARAMS ((rtx, rtx));
00598 static void mark_oprs_set PARAMS ((rtx));
00599 static void alloc_cprop_mem PARAMS ((int, int));
00600 static void free_cprop_mem  PARAMS ((void));
00601 static void compute_transp  PARAMS ((rtx, int, sbitmap *, int));
00602 static void compute_transpout PARAMS ((void));
00603 static void compute_local_properties PARAMS ((sbitmap *, sbitmap *, sbitmap *,
00604                 struct hash_table *));
00605 static void compute_cprop_data  PARAMS ((void));
00606 static void find_used_regs  PARAMS ((rtx *, void *));
00607 static int try_replace_reg  PARAMS ((rtx, rtx, rtx));
00608 static struct expr *find_avail_set PARAMS ((int, rtx));
00609 static int cprop_jump   PARAMS ((basic_block, rtx, rtx, rtx, rtx));
00610 static void mems_conflict_for_gcse_p PARAMS ((rtx, rtx, void *));
00611 static int load_killed_in_block_p    PARAMS ((basic_block, int, rtx, int));
00612 static void canon_list_insert        PARAMS ((rtx, rtx, void *));
00613 static int cprop_insn   PARAMS ((rtx, int));
00614 static int cprop    PARAMS ((int));
00615 static int one_cprop_pass PARAMS ((int, int));
00616 static bool constprop_register  PARAMS ((rtx, rtx, rtx, int));
00617 static struct expr *find_bypass_set PARAMS ((int, int));
00618 static bool reg_killed_on_edge      PARAMS ((rtx, edge));
00619 static int bypass_block       PARAMS ((basic_block, rtx, rtx));
00620 static int bypass_conditional_jumps PARAMS ((void));
00621 static void alloc_pre_mem PARAMS ((int, int));
00622 static void free_pre_mem  PARAMS ((void));
00623 static void compute_pre_data  PARAMS ((void));
00624 static int pre_expr_reaches_here_p PARAMS ((basic_block, struct expr *,
00625               basic_block));
00626 static void insert_insn_end_bb  PARAMS ((struct expr *, basic_block, int));
00627 static void pre_insert_copy_insn PARAMS ((struct expr *, rtx));
00628 static void pre_insert_copies PARAMS ((void));
00629 static int pre_delete   PARAMS ((void));
00630 static int pre_gcse   PARAMS ((void));
00631 static int one_pre_gcse_pass  PARAMS ((int));
00632 static void add_label_notes PARAMS ((rtx, rtx));
00633 static void alloc_code_hoist_mem PARAMS ((int, int));
00634 static void free_code_hoist_mem PARAMS ((void));
00635 static void compute_code_hoist_vbeinout PARAMS ((void));
00636 static void compute_code_hoist_data PARAMS ((void));
00637 static int hoist_expr_reaches_here_p PARAMS ((basic_block, int, basic_block,
00638                 char *));
00639 static void hoist_code    PARAMS ((void));
00640 static int one_code_hoisting_pass PARAMS ((void));
00641 static void alloc_rd_mem  PARAMS ((int, int));
00642 static void free_rd_mem   PARAMS ((void));
00643 static void handle_rd_kill_set  PARAMS ((rtx, int, basic_block));
00644 static void compute_kill_rd PARAMS ((void));
00645 static void compute_rd    PARAMS ((void));
00646 static void alloc_avail_expr_mem PARAMS ((int, int));
00647 static void free_avail_expr_mem PARAMS ((void));
00648 static void compute_ae_gen  PARAMS ((struct hash_table *));
00649 static int expr_killed_p  PARAMS ((rtx, basic_block));
00650 static void compute_ae_kill PARAMS ((sbitmap *, sbitmap *, struct hash_table *));
00651 static int expr_reaches_here_p  PARAMS ((struct occr *, struct expr *,
00652            basic_block, int));
00653 static rtx computing_insn PARAMS ((struct expr *, rtx));
00654 static int def_reaches_here_p PARAMS ((rtx, rtx));
00655 static int can_disregard_other_sets PARAMS ((struct reg_set **, rtx, int));
00656 static int handle_avail_expr  PARAMS ((rtx, struct expr *));
00657 static int classic_gcse   PARAMS ((void));
00658 static int one_classic_gcse_pass PARAMS ((int));
00659 static void invalidate_nonnull_info PARAMS ((rtx, rtx, void *));
00660 static int delete_null_pointer_checks_1 PARAMS ((unsigned int *,
00661               sbitmap *, sbitmap *,
00662               struct null_pointer_info *));
00663 static rtx process_insert_insn  PARAMS ((struct expr *));
00664 static int pre_edge_insert  PARAMS ((struct edge_list *, struct expr **));
00665 static int expr_reaches_here_p_work PARAMS ((struct occr *, struct expr *,
00666                basic_block, int, char *));
00667 static int pre_expr_reaches_here_p_work PARAMS ((basic_block, struct expr *,
00668              basic_block, char *));
00669 static struct ls_expr * ldst_entry  PARAMS ((rtx));
00670 static void free_ldst_entry     PARAMS ((struct ls_expr *));
00671 static void free_ldst_mems    PARAMS ((void));
00672 static void print_ldst_list     PARAMS ((FILE *));
00673 static struct ls_expr * find_rtx_in_ldst PARAMS ((rtx));
00674 static int enumerate_ldsts    PARAMS ((void));
00675 static inline struct ls_expr * first_ls_expr PARAMS ((void));
00676 static inline struct ls_expr * next_ls_expr  PARAMS ((struct ls_expr *));
00677 static int simple_mem     PARAMS ((rtx));
00678 static void invalidate_any_buried_refs  PARAMS ((rtx));
00679 static void compute_ld_motion_mems  PARAMS ((void));
00680 static void trim_ld_motion_mems   PARAMS ((void));
00681 static void update_ld_motion_stores PARAMS ((struct expr *));
00682 static void reg_set_info    PARAMS ((rtx, rtx, void *));
00683 static int store_ops_ok     PARAMS ((rtx, basic_block));
00684 static void find_moveable_store   PARAMS ((rtx));
00685 static int compute_store_table    PARAMS ((void));
00686 static int load_kills_store   PARAMS ((rtx, rtx));
00687 static int find_loads     PARAMS ((rtx, rtx));
00688 static int store_killed_in_insn   PARAMS ((rtx, rtx));
00689 static int store_killed_after   PARAMS ((rtx, rtx, basic_block));
00690 static int store_killed_before    PARAMS ((rtx, rtx, basic_block));
00691 static void build_store_vectors   PARAMS ((void));
00692 static void insert_insn_start_bb  PARAMS ((rtx, basic_block));
00693 static int insert_store     PARAMS ((struct ls_expr *, edge));
00694 static void replace_store_insn    PARAMS ((rtx, rtx, basic_block));
00695 static void delete_store    PARAMS ((struct ls_expr *,
00696              basic_block));
00697 static void free_store_memory   PARAMS ((void));
00698 static void store_motion    PARAMS ((void));
00699 static void free_insn_expr_list_list  PARAMS ((rtx *));
00700 static void clear_modify_mem_tables PARAMS ((void));
00701 static void free_modify_mem_tables  PARAMS ((void));
00702 static rtx gcse_emit_move_after   PARAMS ((rtx, rtx, rtx));
00703 static void local_cprop_find_used_regs  PARAMS ((rtx *, void *));
00704 static bool do_local_cprop    PARAMS ((rtx, rtx, int, rtx*));
00705 static bool adjust_libcall_notes  PARAMS ((rtx, rtx, rtx, rtx*));
00706 static void local_cprop_pass    PARAMS ((int));
00707 
00708 /* Entry point for global common subexpression elimination.
00709    F is the first instruction in the function.  */
00710 
00711 int
00712 gcse_main (f, file)
00713      rtx f;
00714      FILE *file;
00715 {
00716   int changed, pass;
00717   /* Bytes used at start of pass.  */
00718   int initial_bytes_used;
00719   /* Maximum number of bytes used by a pass.  */
00720   int max_pass_bytes;
00721   /* Point to release obstack data from for each pass.  */
00722   char *gcse_obstack_bottom;
00723 
00724   /* Insertion of instructions on edges can create new basic blocks; we
00725      need the original basic block count so that we can properly deallocate
00726      arrays sized on the number of basic blocks originally in the cfg.  */
00727   int orig_bb_count;
00728   /* We do not construct an accurate cfg in functions which call
00729      setjmp, so just punt to be safe.  */
00730   if (current_function_calls_setjmp)
00731     return 0;
00732 
00733   /* Assume that we do not need to run jump optimizations after gcse.  */
00734   run_jump_opt_after_gcse = 0;
00735 
00736   /* For calling dump_foo fns from gdb.  */
00737   debug_stderr = stderr;
00738   gcse_file = file;
00739 
00740   /* Identify the basic block information for this function, including
00741      successors and predecessors.  */
00742   max_gcse_regno = max_reg_num ();
00743 
00744   if (file)
00745     dump_flow_info (file);
00746 
00747   orig_bb_count = n_basic_blocks;
00748   /* Return if there's nothing to do.  */
00749   if (n_basic_blocks <= 1)
00750     return 0;
00751 
00752   /* Trying to perform global optimizations on flow graphs which have
00753      a high connectivity will take a long time and is unlikely to be
00754      particularly useful.
00755 
00756      In normal circumstances a cfg should have about twice as many edges
00757      as blocks.  But we do not want to punish small functions which have
00758      a couple switch statements.  So we require a relatively large number
00759      of basic blocks and the ratio of edges to blocks to be high.  */
00760   if (n_basic_blocks > 1000 && n_edges / n_basic_blocks >= 20)
00761     {
00762       if (warn_disabled_optimization)
00763   warning ("GCSE disabled: %d > 1000 basic blocks and %d >= 20 edges/basic block",
00764      n_basic_blocks, n_edges / n_basic_blocks);
00765       return 0;
00766     }
00767 
00768   /* If allocating memory for the cprop bitmap would take up too much
00769      storage it's better just to disable the optimization.  */
00770   if ((n_basic_blocks
00771        * SBITMAP_SET_SIZE (max_gcse_regno)
00772        * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
00773     {
00774       if (warn_disabled_optimization)
00775   warning ("GCSE disabled: %d basic blocks and %d registers",
00776      n_basic_blocks, max_gcse_regno);
00777 
00778       return 0;
00779     }
00780 
00781   /* See what modes support reg/reg copy operations.  */
00782   if (! can_copy_init_p)
00783     {
00784       compute_can_copy ();
00785       can_copy_init_p = 1;
00786     }
00787 
00788   gcc_obstack_init (&gcse_obstack);
00789   bytes_used = 0;
00790 
00791   /* We need alias.  */
00792   init_alias_analysis ();
00793   /* Record where pseudo-registers are set.  This data is kept accurate
00794      during each pass.  ??? We could also record hard-reg information here
00795      [since it's unchanging], however it is currently done during hash table
00796      computation.
00797 
00798      It may be tempting to compute MEM set information here too, but MEM sets
00799      will be subject to code motion one day and thus we need to compute
00800      information about memory sets when we build the hash tables.  */
00801 
00802   alloc_reg_set_mem (max_gcse_regno);
00803   compute_sets (f);
00804 
00805   pass = 0;
00806   initial_bytes_used = bytes_used;
00807   max_pass_bytes = 0;
00808   gcse_obstack_bottom = gcse_alloc (1);
00809   changed = 1;
00810   while (changed && pass < MAX_GCSE_PASSES)
00811     {
00812       changed = 0;
00813       if (file)
00814   fprintf (file, "GCSE pass %d\n\n", pass + 1);
00815 
00816       /* Initialize bytes_used to the space for the pred/succ lists,
00817    and the reg_set_table data.  */
00818       bytes_used = initial_bytes_used;
00819 
00820       /* Each pass may create new registers, so recalculate each time.  */
00821       max_gcse_regno = max_reg_num ();
00822 
00823       alloc_gcse_mem (f);
00824 
00825       /* Don't allow constant propagation to modify jumps
00826    during this pass.  */
00827       changed = one_cprop_pass (pass + 1, 0);
00828 
00829       if (optimize_size)
00830   changed |= one_classic_gcse_pass (pass + 1);
00831       else
00832   {
00833     changed |= one_pre_gcse_pass (pass + 1);
00834     /* We may have just created new basic blocks.  Release and
00835        recompute various things which are sized on the number of
00836        basic blocks.  */
00837     if (changed)
00838       {
00839         free_modify_mem_tables ();
00840         modify_mem_list
00841     = (rtx *) gmalloc (last_basic_block * sizeof (rtx));
00842         canon_modify_mem_list
00843     = (rtx *) gmalloc (last_basic_block * sizeof (rtx));
00844         memset ((char *) modify_mem_list, 0, last_basic_block * sizeof (rtx));
00845         memset ((char *) canon_modify_mem_list, 0, last_basic_block * sizeof (rtx));
00846         orig_bb_count = n_basic_blocks;
00847       }
00848     free_reg_set_mem ();
00849     alloc_reg_set_mem (max_reg_num ());
00850     compute_sets (f);
00851     run_jump_opt_after_gcse = 1;
00852   }
00853 
00854       if (max_pass_bytes < bytes_used)
00855   max_pass_bytes = bytes_used;
00856 
00857       /* Free up memory, then reallocate for code hoisting.  We can
00858    not re-use the existing allocated memory because the tables
00859    will not have info for the insns or registers created by
00860    partial redundancy elimination.  */
00861       free_gcse_mem ();
00862 
00863       /* It does not make sense to run code hoisting unless we optimizing
00864    for code size -- it rarely makes programs faster, and can make
00865    them bigger if we did partial redundancy elimination (when optimizing
00866    for space, we use a classic gcse algorithm instead of partial
00867    redundancy algorithms).  */
00868       if (optimize_size)
00869   {
00870     max_gcse_regno = max_reg_num ();
00871     alloc_gcse_mem (f);
00872     changed |= one_code_hoisting_pass ();
00873     free_gcse_mem ();
00874 
00875     if (max_pass_bytes < bytes_used)
00876       max_pass_bytes = bytes_used;
00877   }
00878 
00879       if (file)
00880   {
00881     fprintf (file, "\n");
00882     fflush (file);
00883   }
00884 
00885       obstack_free (&gcse_obstack, gcse_obstack_bottom);
00886       pass++;
00887     }
00888 
00889   /* Do one last pass of copy propagation, including cprop into
00890      conditional jumps.  */
00891 
00892   max_gcse_regno = max_reg_num ();
00893   alloc_gcse_mem (f);
00894   /* This time, go ahead and allow cprop to alter jumps.  */
00895   one_cprop_pass (pass + 1, 1);
00896   free_gcse_mem ();
00897 
00898   if (file)
00899     {
00900       fprintf (file, "GCSE of %s: %d basic blocks, ",
00901          current_function_name, n_basic_blocks);
00902       fprintf (file, "%d pass%s, %d bytes\n\n",
00903          pass, pass > 1 ? "es" : "", max_pass_bytes);
00904     }
00905 
00906   obstack_free (&gcse_obstack, NULL);
00907   free_reg_set_mem ();
00908   /* We are finished with alias.  */
00909   end_alias_analysis ();
00910   allocate_reg_info (max_reg_num (), FALSE, FALSE);
00911 
00912   /* Store motion disabled until it is fixed.  */
00913   if (0 && !optimize_size && flag_gcse_sm)
00914     store_motion ();
00915   /* Record where pseudo-registers are set.  */
00916   return run_jump_opt_after_gcse;
00917 }
00918 
00919 /* Misc. utilities.  */
00920 
00921 /* Compute which modes support reg/reg copy operations.  */
00922 
00923 static void
00924 compute_can_copy ()
00925 {
00926   int i;
00927 #ifndef AVOID_CCMODE_COPIES
00928   rtx reg, insn;
00929 #endif
00930   memset (can_copy_p, 0, NUM_MACHINE_MODES);
00931 
00932   start_sequence ();
00933   for (i = 0; i < NUM_MACHINE_MODES; i++)
00934     if (GET_MODE_CLASS (i) == MODE_CC)
00935       {
00936 #ifdef AVOID_CCMODE_COPIES
00937   can_copy_p[i] = 0;
00938 #else
00939   reg = gen_rtx_REG ((enum machine_mode) i, LAST_VIRTUAL_REGISTER + 1);
00940   insn = emit_insn (gen_rtx_SET (VOIDmode, reg, reg));
00941   if (recog (PATTERN (insn), insn, NULL) >= 0)
00942     can_copy_p[i] = 1;
00943 #endif
00944       }
00945     else
00946       can_copy_p[i] = 1;
00947 
00948   end_sequence ();
00949 }
00950 
00951 /* Cover function to xmalloc to record bytes allocated.  */
00952 
00953 static char *
00954 gmalloc (size)
00955      unsigned int size;
00956 {
00957   bytes_used += size;
00958   return xmalloc (size);
00959 }
00960 
00961 /* Cover function to xrealloc.
00962    We don't record the additional size since we don't know it.
00963    It won't affect memory usage stats much anyway.  */
00964 
00965 static char *
00966 grealloc (ptr, size)
00967      char *ptr;
00968      unsigned int size;
00969 {
00970   return xrealloc (ptr, size);
00971 }
00972 
00973 /* Cover function to obstack_alloc.  */
00974 
00975 static char *
00976 gcse_alloc (size)
00977      unsigned long size;
00978 {
00979   bytes_used += size;
00980   return (char *) obstack_alloc (&gcse_obstack, size);
00981 }
00982 
00983 /* Allocate memory for the cuid mapping array,
00984    and reg/memory set tracking tables.
00985 
00986    This is called at the start of each pass.  */
00987 
00988 static void
00989 alloc_gcse_mem (f)
00990      rtx f;
00991 {
00992   int i, n;
00993   rtx insn;
00994 
00995   /* Find the largest UID and create a mapping from UIDs to CUIDs.
00996      CUIDs are like UIDs except they increase monotonically, have no gaps,
00997      and only apply to real insns.  */
00998 
00999   max_uid = get_max_uid ();
01000   n = (max_uid + 1) * sizeof (int);
01001   uid_cuid = (int *) gmalloc (n);
01002   memset ((char *) uid_cuid, 0, n);
01003   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
01004     {
01005       if (INSN_P (insn))
01006   uid_cuid[INSN_UID (insn)] = i++;
01007       else
01008   uid_cuid[INSN_UID (insn)] = i;
01009     }
01010 
01011   /* Create a table mapping cuids to insns.  */
01012 
01013   max_cuid = i;
01014   n = (max_cuid + 1) * sizeof (rtx);
01015   cuid_insn = (rtx *) gmalloc (n);
01016   memset ((char *) cuid_insn, 0, n);
01017   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
01018     if (INSN_P (insn))
01019       CUID_INSN (i++) = insn;
01020 
01021   /* Allocate vars to track sets of regs.  */
01022   reg_set_bitmap = BITMAP_XMALLOC ();
01023 
01024   /* Allocate vars to track sets of regs, memory per block.  */
01025   reg_set_in_block = (sbitmap *) sbitmap_vector_alloc (last_basic_block,
01026                    max_gcse_regno);
01027   /* Allocate array to keep a list of insns which modify memory in each
01028      basic block.  */
01029   modify_mem_list = (rtx *) gmalloc (last_basic_block * sizeof (rtx));
01030   canon_modify_mem_list = (rtx *) gmalloc (last_basic_block * sizeof (rtx));
01031   memset ((char *) modify_mem_list, 0, last_basic_block * sizeof (rtx));
01032   memset ((char *) canon_modify_mem_list, 0, last_basic_block * sizeof (rtx));
01033   modify_mem_list_set = BITMAP_XMALLOC ();
01034   canon_modify_mem_list_set = BITMAP_XMALLOC ();
01035 }
01036 
01037 /* Free memory allocated by alloc_gcse_mem.  */
01038 
01039 static void
01040 free_gcse_mem ()
01041 {
01042   free (uid_cuid);
01043   free (cuid_insn);
01044 
01045   BITMAP_XFREE (reg_set_bitmap);
01046 
01047   sbitmap_vector_free (reg_set_in_block);
01048   free_modify_mem_tables ();
01049   BITMAP_XFREE (modify_mem_list_set);
01050   BITMAP_XFREE (canon_modify_mem_list_set);
01051 }
01052 
01053 /* Many of the global optimization algorithms work by solving dataflow
01054    equations for various expressions.  Initially, some local value is
01055    computed for each expression in each block.  Then, the values across the
01056    various blocks are combined (by following flow graph edges) to arrive at
01057    global values.  Conceptually, each set of equations is independent.  We
01058    may therefore solve all the equations in parallel, solve them one at a
01059    time, or pick any intermediate approach.
01060 
01061    When you're going to need N two-dimensional bitmaps, each X (say, the
01062    number of blocks) by Y (say, the number of expressions), call this
01063    function.  It's not important what X and Y represent; only that Y
01064    correspond to the things that can be done in parallel.  This function will
01065    return an appropriate chunking factor C; you should solve C sets of
01066    equations in parallel.  By going through this function, we can easily
01067    trade space against time; by solving fewer equations in parallel we use
01068    less space.  */
01069 
01070 static int
01071 get_bitmap_width (n, x, y)
01072      int n;
01073      int x;
01074      int y;
01075 {
01076   /* It's not really worth figuring out *exactly* how much memory will
01077      be used by a particular choice.  The important thing is to get
01078      something approximately right.  */
01079   size_t max_bitmap_memory = 10 * 1024 * 1024;
01080 
01081   /* The number of bytes we'd use for a single column of minimum
01082      width.  */
01083   size_t column_size = n * x * sizeof (SBITMAP_ELT_TYPE);
01084 
01085   /* Often, it's reasonable just to solve all the equations in
01086      parallel.  */
01087   if (column_size * SBITMAP_SET_SIZE (y) <= max_bitmap_memory)
01088     return y;
01089 
01090   /* Otherwise, pick the largest width we can, without going over the
01091      limit.  */
01092   return SBITMAP_ELT_BITS * ((max_bitmap_memory + column_size - 1)
01093            / column_size);
01094 }
01095 
01096 /* Compute the local properties of each recorded expression.
01097 
01098    Local properties are those that are defined by the block, irrespective of
01099    other blocks.
01100 
01101    An expression is transparent in a block if its operands are not modified
01102    in the block.
01103 
01104    An expression is computed (locally available) in a block if it is computed
01105    at least once and expression would contain the same value if the
01106    computation was moved to the end of the block.
01107 
01108    An expression is locally anticipatable in a block if it is computed at
01109    least once and expression would contain the same value if the computation
01110    was moved to the beginning of the block.
01111 
01112    We call this routine for cprop, pre and code hoisting.  They all compute
01113    basically the same information and thus can easily share this code.
01114 
01115    TRANSP, COMP, and ANTLOC are destination sbitmaps for recording local
01116    properties.  If NULL, then it is not necessary to compute or record that
01117    particular property.
01118 
01119    TABLE controls which hash table to look at.  If it is  set hash table,
01120    additionally, TRANSP is computed as ~TRANSP, since this is really cprop's
01121    ABSALTERED.  */
01122 
01123 static void
01124 compute_local_properties (transp, comp, antloc, table)
01125      sbitmap *transp;
01126      sbitmap *comp;
01127      sbitmap *antloc;
01128      struct hash_table *table;
01129 {
01130   unsigned int i;
01131 
01132   /* Initialize any bitmaps that were passed in.  */
01133   if (transp)
01134     {
01135       if (table->set_p)
01136   sbitmap_vector_zero (transp, last_basic_block);
01137       else
01138   sbitmap_vector_ones (transp, last_basic_block);
01139     }
01140 
01141   if (comp)
01142     sbitmap_vector_zero (comp, last_basic_block);
01143   if (antloc)
01144     sbitmap_vector_zero (antloc, last_basic_block);
01145 
01146   for (i = 0; i < table->size; i++)
01147     {
01148       struct expr *expr;
01149 
01150       for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
01151   {
01152     int indx = expr->bitmap_index;
01153     struct occr *occr;
01154 
01155     /* The expression is transparent in this block if it is not killed.
01156        We start by assuming all are transparent [none are killed], and
01157        then reset the bits for those that are.  */
01158     if (transp)
01159       compute_transp (expr->expr, indx, transp, table->set_p);
01160 
01161     /* The occurrences recorded in antic_occr are exactly those that
01162        we want to set to nonzero in ANTLOC.  */
01163     if (antloc)
01164       for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
01165         {
01166     SET_BIT (antloc[BLOCK_NUM (occr->insn)], indx);
01167 
01168     /* While we're scanning the table, this is a good place to
01169        initialize this.  */
01170     occr->deleted_p = 0;
01171         }
01172 
01173     /* The occurrences recorded in avail_occr are exactly those that
01174        we want to set to nonzero in COMP.  */
01175     if (comp)
01176       for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
01177         {
01178     SET_BIT (comp[BLOCK_NUM (occr->insn)], indx);
01179 
01180     /* While we're scanning the table, this is a good place to
01181        initialize this.  */
01182     occr->copied_p = 0;
01183         }
01184 
01185     /* While we're scanning the table, this is a good place to
01186        initialize this.  */
01187     expr->reaching_reg = 0;
01188   }
01189     }
01190 }
01191 
01192 /* Register set information.
01193 
01194    `reg_set_table' records where each register is set or otherwise
01195    modified.  */
01196 
01197 static struct obstack reg_set_obstack;
01198 
01199 static void
01200 alloc_reg_set_mem (n_regs)
01201      int n_regs;
01202 {
01203   unsigned int n;
01204 
01205   reg_set_table_size = n_regs + REG_SET_TABLE_SLOP;
01206   n = reg_set_table_size * sizeof (struct reg_set *);
01207   reg_set_table = (struct reg_set **) gmalloc (n);
01208   memset ((char *) reg_set_table, 0, n);
01209 
01210   gcc_obstack_init (&reg_set_obstack);
01211 }
01212 
01213 static void
01214 free_reg_set_mem ()
01215 {
01216   free (reg_set_table);
01217   obstack_free (&reg_set_obstack, NULL);
01218 }
01219 
01220 /* Record REGNO in the reg_set table.  */
01221 
01222 static void
01223 record_one_set (regno, insn)
01224      int regno;
01225      rtx insn;
01226 {
01227   /* Allocate a new reg_set element and link it onto the list.  */
01228   struct reg_set *new_reg_info;
01229 
01230   /* If the table isn't big enough, enlarge it.  */
01231   if (regno >= reg_set_table_size)
01232     {
01233       int new_size = regno + REG_SET_TABLE_SLOP;
01234 
01235       reg_set_table
01236   = (struct reg_set **) grealloc ((char *) reg_set_table,
01237           new_size * sizeof (struct reg_set *));
01238       memset ((char *) (reg_set_table + reg_set_table_size), 0,
01239         (new_size - reg_set_table_size) * sizeof (struct reg_set *));
01240       reg_set_table_size = new_size;
01241     }
01242 
01243   new_reg_info = (struct reg_set *) obstack_alloc (&reg_set_obstack,
01244                sizeof (struct reg_set));
01245   bytes_used += sizeof (struct reg_set);
01246   new_reg_info->insn = insn;
01247   new_reg_info->next = reg_set_table[regno];
01248   reg_set_table[regno] = new_reg_info;
01249 }
01250 
01251 /* Called from compute_sets via note_stores to handle one SET or CLOBBER in
01252    an insn.  The DATA is really the instruction in which the SET is
01253    occurring.  */
01254 
01255 static void
01256 record_set_info (dest, setter, data)
01257      rtx dest, setter ATTRIBUTE_UNUSED;
01258      void *data;
01259 {
01260   rtx record_set_insn = (rtx) data;
01261 
01262   if (GET_CODE (dest) == REG && REGNO (dest) >= FIRST_PSEUDO_REGISTER)
01263     record_one_set (REGNO (dest), record_set_insn);
01264 }
01265 
01266 /* Scan the function and record each set of each pseudo-register.
01267 
01268    This is called once, at the start of the gcse pass.  See the comments for
01269    `reg_set_table' for further documenation.  */
01270 
01271 static void
01272 compute_sets (f)
01273      rtx f;
01274 {
01275   rtx insn;
01276 
01277   for (insn = f; insn != 0; insn = NEXT_INSN (insn))
01278     if (INSN_P (insn))
01279       note_stores (PATTERN (insn), record_set_info, insn);
01280 }
01281 
01282 /* Hash table support.  */
01283 
01284 struct reg_avail_info
01285 {
01286   basic_block last_bb;
01287   int first_set;
01288   int last_set;
01289 };
01290 
01291 static struct reg_avail_info *reg_avail_info;
01292 static basic_block current_bb;
01293 
01294 
01295 /* See whether X, the source of a set, is something we want to consider for
01296    GCSE.  */
01297 
01298 static GTY(()) rtx test_insn;
01299 static int
01300 want_to_gcse_p (x)
01301      rtx x;
01302 {
01303   int num_clobbers = 0;
01304   int icode;
01305 
01306   switch (GET_CODE (x))
01307     {
01308     case REG:
01309     case SUBREG:
01310     case CONST_INT:
01311     case CONST_DOUBLE:
01312     case CONST_VECTOR:
01313     case CALL:
01314       return 0;
01315 
01316     default:
01317       break;
01318     }
01319 
01320   /* If this is a valid operand, we are OK.  If it's VOIDmode, we aren't.  */
01321   if (general_operand (x, GET_MODE (x)))
01322     return 1;
01323   else if (GET_MODE (x) == VOIDmode)
01324     return 0;
01325 
01326   /* Otherwise, check if we can make a valid insn from it.  First initialize
01327      our test insn if we haven't already.  */
01328   if (test_insn == 0)
01329     {
01330       test_insn
01331   = make_insn_raw (gen_rtx_SET (VOIDmode,
01332               gen_rtx_REG (word_mode,
01333                FIRST_PSEUDO_REGISTER * 2),
01334               const0_rtx));
01335       NEXT_INSN (test_insn) = PREV_INSN (test_insn) = 0;
01336     }
01337 
01338   /* Now make an insn like the one we would make when GCSE'ing and see if
01339      valid.  */
01340   PUT_MODE (SET_DEST (PATTERN (test_insn)), GET_MODE (x));
01341   SET_SRC (PATTERN (test_insn)) = x;
01342   return ((icode = recog (PATTERN (test_insn), test_insn, &num_clobbers)) >= 0
01343     && (num_clobbers == 0 || ! added_clobbers_hard_reg_p (icode)));
01344 }
01345 
01346 /* Return nonzero if the operands of expression X are unchanged from the
01347    start of INSN's basic block up to but not including INSN (if AVAIL_P == 0),
01348    or from INSN to the end of INSN's basic block (if AVAIL_P != 0).  */
01349 
01350 static int
01351 oprs_unchanged_p (x, insn, avail_p)
01352      rtx x, insn;
01353      int avail_p;
01354 {
01355   int i, j;
01356   enum rtx_code code;
01357   const char *fmt;
01358 
01359   if (x == 0)
01360     return 1;
01361 
01362   code = GET_CODE (x);
01363   switch (code)
01364     {
01365     case REG:
01366       {
01367   struct reg_avail_info *info = &reg_avail_info[REGNO (x)];
01368 
01369   if (info->last_bb != current_bb)
01370     return 1;
01371   if (avail_p)
01372     return info->last_set < INSN_CUID (insn);
01373   else
01374     return info->first_set >= INSN_CUID (insn);
01375       }
01376 
01377     case MEM:
01378       if (load_killed_in_block_p (current_bb, INSN_CUID (insn),
01379           x, avail_p))
01380   return 0;
01381       else
01382   return oprs_unchanged_p (XEXP (x, 0), insn, avail_p);
01383 
01384     case PRE_DEC:
01385     case PRE_INC:
01386     case POST_DEC:
01387     case POST_INC:
01388     case PRE_MODIFY:
01389     case POST_MODIFY:
01390       return 0;
01391 
01392     case PC:
01393     case CC0: /*FIXME*/
01394     case CONST:
01395     case CONST_INT:
01396     case CONST_DOUBLE:
01397     case CONST_VECTOR:
01398     case SYMBOL_REF:
01399     case LABEL_REF:
01400     case ADDR_VEC:
01401     case ADDR_DIFF_VEC:
01402       return 1;
01403 
01404     default:
01405       break;
01406     }
01407 
01408   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
01409     {
01410       if (fmt[i] == 'e')
01411   {
01412     /* If we are about to do the last recursive call needed at this
01413        level, change it into iteration.  This function is called enough
01414        to be worth it.  */
01415     if (i == 0)
01416       return oprs_unchanged_p (XEXP (x, i), insn, avail_p);
01417 
01418     else if (! oprs_unchanged_p (XEXP (x, i), insn, avail_p))
01419       return 0;
01420   }
01421       else if (fmt[i] == 'E')
01422   for (j = 0; j < XVECLEN (x, i); j++)
01423     if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, avail_p))
01424       return 0;
01425     }
01426 
01427   return 1;
01428 }
01429 
01430 /* Used for communication between mems_conflict_for_gcse_p and
01431    load_killed_in_block_p.  Nonzero if mems_conflict_for_gcse_p finds a
01432    conflict between two memory references.  */
01433 static int gcse_mems_conflict_p;
01434 
01435 /* Used for communication between mems_conflict_for_gcse_p and
01436    load_killed_in_block_p.  A memory reference for a load instruction,
01437    mems_conflict_for_gcse_p will see if a memory store conflicts with
01438    this memory load.  */
01439 static rtx gcse_mem_operand;
01440 
01441 /* DEST is the output of an instruction.  If it is a memory reference, and
01442    possibly conflicts with the load found in gcse_mem_operand, then set
01443    gcse_mems_conflict_p to a nonzero value.  */
01444 
01445 static void
01446 mems_conflict_for_gcse_p (dest, setter, data)
01447      rtx dest, setter ATTRIBUTE_UNUSED;
01448      void *data ATTRIBUTE_UNUSED;
01449 {
01450   while (GET_CODE (dest) == SUBREG
01451    || GET_CODE (dest) == ZERO_EXTRACT
01452    || GET_CODE (dest) == SIGN_EXTRACT
01453    || GET_CODE (dest) == STRICT_LOW_PART)
01454     dest = XEXP (dest, 0);
01455 
01456   /* If DEST is not a MEM, then it will not conflict with the load.  Note
01457      that function calls are assumed to clobber memory, but are handled
01458      elsewhere.  */
01459   if (GET_CODE (dest) != MEM)
01460     return;
01461 
01462   /* If we are setting a MEM in our list of specially recognized MEMs,
01463      don't mark as killed this time.  */
01464 
01465   if (dest == gcse_mem_operand && pre_ldst_mems != NULL)
01466     {
01467       if (!find_rtx_in_ldst (dest))
01468   gcse_mems_conflict_p = 1;
01469       return;
01470     }
01471 
01472   if (true_dependence (dest, GET_MODE (dest), gcse_mem_operand,
01473            rtx_addr_varies_p))
01474     gcse_mems_conflict_p = 1;
01475 }
01476 
01477 /* Return nonzero if the expression in X (a memory reference) is killed
01478    in block BB before or after the insn with the CUID in UID_LIMIT.
01479    AVAIL_P is nonzero for kills after UID_LIMIT, and zero for kills
01480    before UID_LIMIT.
01481 
01482    To check the entire block, set UID_LIMIT to max_uid + 1 and
01483    AVAIL_P to 0.  */
01484 
01485 static int
01486 load_killed_in_block_p (bb, uid_limit, x, avail_p)
01487      basic_block bb;
01488      int uid_limit;
01489      rtx x;
01490      int avail_p;
01491 {
01492   rtx list_entry = modify_mem_list[bb->index];
01493   while (list_entry)
01494     {
01495       rtx setter;
01496       /* Ignore entries in the list that do not apply.  */
01497       if ((avail_p
01498      && INSN_CUID (XEXP (list_entry, 0)) < uid_limit)
01499     || (! avail_p
01500         && INSN_CUID (XEXP (list_entry, 0)) > uid_limit))
01501   {
01502     list_entry = XEXP (list_entry, 1);
01503     continue;
01504   }
01505 
01506       setter = XEXP (list_entry, 0);
01507 
01508       /* If SETTER is a call everything is clobbered.  Note that calls
01509    to pure functions are never put on the list, so we need not
01510    worry about them.  */
01511       if (GET_CODE (setter) == CALL_INSN)
01512   return 1;
01513 
01514       /* SETTER must be an INSN of some kind that sets memory.  Call
01515    note_stores to examine each hunk of memory that is modified.
01516 
01517    The note_stores interface is pretty limited, so we have to
01518    communicate via global variables.  Yuk.  */
01519       gcse_mem_operand = x;
01520       gcse_mems_conflict_p = 0;
01521       note_stores (PATTERN (setter), mems_conflict_for_gcse_p, NULL);
01522       if (gcse_mems_conflict_p)
01523   return 1;
01524       list_entry = XEXP (list_entry, 1);
01525     }
01526   return 0;
01527 }
01528 
01529 /* Return nonzero if the operands of expression X are unchanged from
01530    the start of INSN's basic block up to but not including INSN.  */
01531 
01532 static int
01533 oprs_anticipatable_p (x, insn)
01534      rtx x, insn;
01535 {
01536   return oprs_unchanged_p (x, insn, 0);
01537 }
01538 
01539 /* Return nonzero if the operands of expression X are unchanged from
01540    INSN to the end of INSN's basic block.  */
01541 
01542 static int
01543 oprs_available_p (x, insn)
01544      rtx x, insn;
01545 {
01546   return oprs_unchanged_p (x, insn, 1);
01547 }
01548 
01549 /* Hash expression X.
01550 
01551    MODE is only used if X is a CONST_INT.  DO_NOT_RECORD_P is a boolean
01552    indicating if a volatile operand is found or if the expression contains
01553    something we don't want to insert in the table.
01554 
01555    ??? One might want to merge this with canon_hash.  Later.  */
01556 
01557 static unsigned int
01558 hash_expr (x, mode, do_not_record_p, hash_table_size)
01559      rtx x;
01560      enum machine_mode mode;
01561      int *do_not_record_p;
01562      int hash_table_size;
01563 {
01564   unsigned int hash;
01565 
01566   *do_not_record_p = 0;
01567 
01568   hash = hash_expr_1 (x, mode, do_not_record_p);
01569   return hash % hash_table_size;
01570 }
01571 
01572 /* Hash a string.  Just add its bytes up.  */
01573 
01574 static inline unsigned
01575 hash_string_1 (ps)
01576      const char *ps;
01577 {
01578   unsigned hash = 0;
01579   const unsigned char *p = (const unsigned char *) ps;
01580 
01581   if (p)
01582     while (*p)
01583       hash += *p++;
01584 
01585   return hash;
01586 }
01587 
01588 /* Subroutine of hash_expr to do the actual work.  */
01589 
01590 static unsigned int
01591 hash_expr_1 (x, mode, do_not_record_p)
01592      rtx x;
01593      enum machine_mode mode;
01594      int *do_not_record_p;
01595 {
01596   int i, j;
01597   unsigned hash = 0;
01598   enum rtx_code code;
01599   const char *fmt;
01600 
01601   /* Used to turn recursion into iteration.  We can't rely on GCC's
01602      tail-recursion eliminatio since we need to keep accumulating values
01603      in HASH.  */
01604 
01605   if (x == 0)
01606     return hash;
01607 
01608  repeat:
01609   code = GET_CODE (x);
01610   switch (code)
01611     {
01612     case REG:
01613       hash += ((unsigned int) REG << 7) + REGNO (x);
01614       return hash;
01615 
01616     case CONST_INT:
01617       hash += (((unsigned int) CONST_INT << 7) + (unsigned int) mode
01618          + (unsigned int) INTVAL (x));
01619       return hash;
01620 
01621     case CONST_DOUBLE:
01622       /* This is like the general case, except that it only counts
01623    the integers representing the constant.  */
01624       hash += (unsigned int) code + (unsigned int) GET_MODE (x);
01625       if (GET_MODE (x) != VOIDmode)
01626   for (i = 2; i < GET_RTX_LENGTH (CONST_DOUBLE); i++)
01627     hash += (unsigned int) XWINT (x, i);
01628       else
01629   hash += ((unsigned int) CONST_DOUBLE_LOW (x)
01630      + (unsigned int) CONST_DOUBLE_HIGH (x));
01631       return hash;
01632 
01633     case CONST_VECTOR:
01634       {
01635   int units;
01636   rtx elt;
01637 
01638   units = CONST_VECTOR_NUNITS (x);
01639 
01640   for (i = 0; i < units; ++i)
01641     {
01642       elt = CONST_VECTOR_ELT (x, i);
01643       hash += hash_expr_1 (elt, GET_MODE (elt), do_not_record_p);
01644     }
01645 
01646   return hash;
01647       }
01648 
01649       /* Assume there is only one rtx object for any given label.  */
01650     case LABEL_REF:
01651       /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
01652    differences and differences between each stage's debugging dumps.  */
01653       hash += (((unsigned int) LABEL_REF << 7)
01654          + CODE_LABEL_NUMBER (XEXP (x, 0)));
01655       return hash;
01656 
01657     case SYMBOL_REF:
01658       {
01659   /* Don't hash on the symbol's address to avoid bootstrap differences.
01660      Different hash values may cause expressions to be recorded in
01661      different orders and thus different registers to be used in the
01662      final assembler.  This also avoids differences in the dump files
01663      between various stages.  */
01664   unsigned int h = 0;
01665   const unsigned char *p = (const unsigned char *) XSTR (x, 0);
01666 
01667   while (*p)
01668     h += (h << 7) + *p++; /* ??? revisit */
01669 
01670   hash += ((unsigned int) SYMBOL_REF << 7) + h;
01671   return hash;
01672       }
01673 
01674     case MEM:
01675       if (MEM_VOLATILE_P (x))
01676   {
01677     *do_not_record_p = 1;
01678     return 0;
01679   }
01680 
01681       hash += (unsigned int) MEM;
01682       /* We used alias set for hashing, but this is not good, since the alias
01683    set may differ in -fprofile-arcs and -fbranch-probabilities compilation
01684    causing the profiles to fail to match.  */
01685       x = XEXP (x, 0);
01686       goto repeat;
01687 
01688     case PRE_DEC:
01689     case PRE_INC:
01690     case POST_DEC:
01691     case POST_INC:
01692     case PC:
01693     case CC0:
01694     case CALL:
01695     case UNSPEC_VOLATILE:
01696       *do_not_record_p = 1;
01697       return 0;
01698 
01699     case ASM_OPERANDS:
01700       if (MEM_VOLATILE_P (x))
01701   {
01702     *do_not_record_p = 1;
01703     return 0;
01704   }
01705       else
01706   {
01707     /* We don't want to take the filename and line into account.  */
01708     hash += (unsigned) code + (unsigned) GET_MODE (x)
01709       + hash_string_1 (ASM_OPERANDS_TEMPLATE (x))
01710       + hash_string_1 (ASM_OPERANDS_OUTPUT_CONSTRAINT (x))
01711       + (unsigned) ASM_OPERANDS_OUTPUT_IDX (x);
01712 
01713     if (ASM_OPERANDS_INPUT_LENGTH (x))
01714       {
01715         for (i = 1; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
01716     {
01717       hash += (hash_expr_1 (ASM_OPERANDS_INPUT (x, i),
01718           GET_MODE (ASM_OPERANDS_INPUT (x, i)),
01719           do_not_record_p)
01720          + hash_string_1 (ASM_OPERANDS_INPUT_CONSTRAINT
01721               (x, i)));
01722     }
01723 
01724         hash += hash_string_1 (ASM_OPERANDS_INPUT_CONSTRAINT (x, 0));
01725         x = ASM_OPERANDS_INPUT (x, 0);
01726         mode = GET_MODE (x);
01727         goto repeat;
01728       }
01729     return hash;
01730   }
01731 
01732     default:
01733       break;
01734     }
01735 
01736   hash += (unsigned) code + (unsigned) GET_MODE (x);
01737   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
01738     {
01739       if (fmt[i] == 'e')
01740   {
01741     /* If we are about to do the last recursive call
01742        needed at this level, change it into iteration.
01743        This function is called enough to be worth it.  */
01744     if (i == 0)
01745       {
01746         x = XEXP (x, i);
01747         goto repeat;
01748       }
01749 
01750     hash += hash_expr_1 (XEXP (x, i), 0, do_not_record_p);
01751     if (*do_not_record_p)
01752       return 0;
01753   }
01754 
01755       else if (fmt[i] == 'E')
01756   for (j = 0; j < XVECLEN (x, i); j++)
01757     {
01758       hash += hash_expr_1 (XVECEXP (x, i, j), 0, do_not_record_p);
01759       if (*do_not_record_p)
01760         return 0;
01761     }
01762 
01763       else if (fmt[i] == 's')
01764   hash += hash_string_1 (XSTR (x, i));
01765       else if (fmt[i] == 'i')
01766   hash += (unsigned int) XINT (x, i);
01767       else
01768   abort ();
01769     }
01770 
01771   return hash;
01772 }
01773 
01774 /* Hash a set of register REGNO.
01775 
01776    Sets are hashed on the register that is set.  This simplifies the PRE copy
01777    propagation code.
01778 
01779    ??? May need to make things more elaborate.  Later, as necessary.  */
01780 
01781 static unsigned int
01782 hash_set (regno, hash_table_size)
01783      int regno;
01784      int hash_table_size;
01785 {
01786   unsigned int hash;
01787 
01788   hash = regno;
01789   return hash % hash_table_size;
01790 }
01791 
01792 /* Return nonzero if exp1 is equivalent to exp2.
01793    ??? Borrowed from cse.c.  Might want to remerge with cse.c.  Later.  */
01794 
01795 static int
01796 expr_equiv_p (x, y)
01797      rtx x, y;
01798 {
01799   int i, j;
01800   enum rtx_code code;
01801   const char *fmt;
01802 
01803   if (x == y)
01804     return 1;
01805 
01806   if (x == 0 || y == 0)
01807     return x == y;
01808 
01809   code = GET_CODE (x);
01810   if (code != GET_CODE (y))
01811     return 0;
01812 
01813   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
01814   if (GET_MODE (x) != GET_MODE (y))
01815     return 0;
01816 
01817   switch (code)
01818     {
01819     case PC:
01820     case CC0:
01821       return x == y;
01822 
01823     case CONST_INT:
01824       return INTVAL (x) == INTVAL (y);
01825 
01826     case LABEL_REF:
01827       return XEXP (x, 0) == XEXP (y, 0);
01828 
01829     case SYMBOL_REF:
01830       return XSTR (x, 0) == XSTR (y, 0);
01831 
01832     case REG:
01833       return REGNO (x) == REGNO (y);
01834 
01835     case MEM:
01836       /* Can't merge two expressions in different alias sets, since we can
01837    decide that the expression is transparent in a block when it isn't,
01838    due to it being set with the different alias set.  */
01839       if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
01840   return 0;
01841       break;
01842 
01843     /*  For commutative operations, check both orders.  */
01844     case PLUS:
01845     case MULT:
01846     case AND:
01847     case IOR:
01848     case XOR:
01849     case NE:
01850     case EQ:
01851       return ((expr_equiv_p (XEXP (x, 0), XEXP (y, 0))
01852          && expr_equiv_p (XEXP (x, 1), XEXP (y, 1)))
01853         || (expr_equiv_p (XEXP (x, 0), XEXP (y, 1))
01854       && expr_equiv_p (XEXP (x, 1), XEXP (y, 0))));
01855 
01856     case ASM_OPERANDS:
01857       /* We don't use the generic code below because we want to
01858    disregard filename and line numbers.  */
01859 
01860       /* A volatile asm isn't equivalent to any other.  */
01861       if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
01862   return 0;
01863 
01864       if (GET_MODE (x) != GET_MODE (y)
01865     || strcmp (ASM_OPERANDS_TEMPLATE (x), ASM_OPERANDS_TEMPLATE (y))
01866     || strcmp (ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
01867          ASM_OPERANDS_OUTPUT_CONSTRAINT (y))
01868     || ASM_OPERANDS_OUTPUT_IDX (x) != ASM_OPERANDS_OUTPUT_IDX (y)
01869     || ASM_OPERANDS_INPUT_LENGTH (x) != ASM_OPERANDS_INPUT_LENGTH (y))
01870   return 0;
01871 
01872       if (ASM_OPERANDS_INPUT_LENGTH (x))
01873   {
01874     for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; i--)
01875       if (! expr_equiv_p (ASM_OPERANDS_INPUT (x, i),
01876         ASM_OPERANDS_INPUT (y, i))
01877     || strcmp (ASM_OPERANDS_INPUT_CONSTRAINT (x, i),
01878          ASM_OPERANDS_INPUT_CONSTRAINT (y, i)))
01879         return 0;
01880   }
01881 
01882       return 1;
01883 
01884     default:
01885       break;
01886     }
01887 
01888   /* Compare the elements.  If any pair of corresponding elements
01889      fail to match, return 0 for the whole thing.  */
01890 
01891   fmt = GET_RTX_FORMAT (code);
01892   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
01893     {
01894       switch (fmt[i])
01895   {
01896   case 'e':
01897     if (! expr_equiv_p (XEXP (x, i), XEXP (y, i)))
01898       return 0;
01899     break;
01900 
01901   case 'E':
01902     if (XVECLEN (x, i) != XVECLEN (y, i))
01903       return 0;
01904     for (j = 0; j < XVECLEN (x, i); j++)
01905       if (! expr_equiv_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
01906         return 0;
01907     break;
01908 
01909   case 's':
01910     if (strcmp (XSTR (x, i), XSTR (y, i)))
01911       return 0;
01912     break;
01913 
01914   case 'i':
01915     if (XINT (x, i) != XINT (y, i))
01916       return 0;
01917     break;
01918 
01919   case 'w':
01920     if (XWINT (x, i) != XWINT (y, i))
01921       return 0;
01922   break;
01923 
01924   case '0':
01925     break;
01926 
01927   default:
01928     abort ();
01929   }
01930     }
01931 
01932   return 1;
01933 }
01934 
01935 /* Insert expression X in INSN in the hash TABLE.
01936    If it is already present, record it as the last occurrence in INSN's
01937    basic block.
01938 
01939    MODE is the mode of the value X is being stored into.
01940    It is only used if X is a CONST_INT.
01941 
01942    ANTIC_P is nonzero if X is an anticipatable expression.
01943    AVAIL_P is nonzero if X is an available expression.  */
01944 
01945 static void
01946 insert_expr_in_table (x, mode, insn, antic_p, avail_p, table)
01947      rtx x;
01948      enum machine_mode mode;
01949      rtx insn;
01950      int antic_p, avail_p;
01951      struct hash_table *table;
01952 {
01953   int found, do_not_record_p;
01954   unsigned int hash;
01955   struct expr *cur_expr, *last_expr = NULL;
01956   struct occr *antic_occr, *avail_occr;
01957   struct occr *last_occr = NULL;
01958 
01959   hash = hash_expr (x, mode, &do_not_record_p, table->size);
01960 
01961   /* Do not insert expression in table if it contains volatile operands,
01962      or if hash_expr determines the expression is something we don't want
01963      to or can't handle.  */
01964   if (do_not_record_p)
01965     return;
01966 
01967   cur_expr = table->table[hash];
01968   found = 0;
01969 
01970   while (cur_expr && 0 == (found = expr_equiv_p (cur_expr->expr, x)))
01971     {
01972       /* If the expression isn't found, save a pointer to the end of
01973    the list.  */
01974       last_expr = cur_expr;
01975       cur_expr = cur_expr->next_same_hash;
01976     }
01977 
01978   if (! found)
01979     {
01980       cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
01981       bytes_used += sizeof (struct expr);
01982       if (table->table[hash] == NULL)
01983   /* This is the first pattern that hashed to this index.  */
01984   table->table[hash] = cur_expr;
01985       else
01986   /* Add EXPR to end of this hash chain.  */
01987   last_expr->next_same_hash = cur_expr;
01988 
01989       /* Set the fields of the expr element.  */
01990       cur_expr->expr = x;
01991       cur_expr->bitmap_index = table->n_elems++;
01992       cur_expr->next_same_hash = NULL;
01993       cur_expr->antic_occr = NULL;
01994       cur_expr->avail_occr = NULL;
01995     }
01996 
01997   /* Now record the occurrence(s).  */
01998   if (antic_p)
01999     {
02000       antic_occr = cur_expr->antic_occr;
02001 
02002       /* Search for another occurrence in the same basic block.  */
02003       while (antic_occr && BLOCK_NUM (antic_occr->insn) != BLOCK_NUM (insn))
02004   {
02005     /* If an occurrence isn't found, save a pointer to the end of
02006        the list.  */
02007     last_occr = antic_occr;
02008     antic_occr = antic_occr->next;
02009   }
02010 
02011       if (antic_occr)
02012   /* Found another instance of the expression in the same basic block.
02013      Prefer the currently recorded one.  We want the first one in the
02014      block and the block is scanned from start to end.  */
02015   ; /* nothing to do */
02016       else
02017   {
02018     /* First occurrence of this expression in this basic block.  */
02019     antic_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
02020     bytes_used += sizeof (struct occr);
02021     /* First occurrence of this expression in any block?  */
02022     if (cur_expr->antic_occr == NULL)
02023       cur_expr->antic_occr = antic_occr;
02024     else
02025       last_occr->next = antic_occr;
02026 
02027     antic_occr->insn = insn;
02028     antic_occr->next = NULL;
02029   }
02030     }
02031 
02032   if (avail_p)
02033     {
02034       avail_occr = cur_expr->avail_occr;
02035 
02036       /* Search for another occurrence in the same basic block.  */
02037       while (avail_occr && BLOCK_NUM (avail_occr->insn) != BLOCK_NUM (insn))
02038   {
02039     /* If an occurrence isn't found, save a pointer to the end of
02040        the list.  */
02041     last_occr = avail_occr;
02042     avail_occr = avail_occr->next;
02043   }
02044 
02045       if (avail_occr)
02046   /* Found another instance of the expression in the same basic block.
02047      Prefer this occurrence to the currently recorded one.  We want
02048      the last one in the block and the block is scanned from start
02049      to end.  */
02050   avail_occr->insn = insn;
02051       else
02052   {
02053     /* First occurrence of this expression in this basic block.  */
02054     avail_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
02055     bytes_used += sizeof (struct occr);
02056 
02057     /* First occurrence of this expression in any block?  */
02058     if (cur_expr->avail_occr == NULL)
02059       cur_expr->avail_occr = avail_occr;
02060     else
02061       last_occr->next = avail_occr;
02062 
02063     avail_occr->insn = insn;
02064     avail_occr->next = NULL;
02065   }
02066     }
02067 }
02068 
02069 /* Insert pattern X in INSN in the hash table.
02070    X is a SET of a reg to either another reg or a constant.
02071    If it is already present, record it as the last occurrence in INSN's
02072    basic block.  */
02073 
02074 static void
02075 insert_set_in_table (x, insn, table)
02076      rtx x;
02077      rtx insn;
02078      struct hash_table *table;
02079 {
02080   int found;
02081   unsigned int hash;
02082   struct expr *cur_expr, *last_expr = NULL;
02083   struct occr *cur_occr, *last_occr = NULL;
02084 
02085   if (GET_CODE (x) != SET
02086       || GET_CODE (SET_DEST (x)) != REG)
02087     abort ();
02088 
02089   hash = hash_set (REGNO (SET_DEST (x)), table->size);
02090 
02091   cur_expr = table->table[hash];
02092   found = 0;
02093 
02094   while (cur_expr && 0 == (found = expr_equiv_p (cur_expr->expr, x)))
02095     {
02096       /* If the expression isn't found, save a pointer to the end of
02097    the list.  */
02098       last_expr = cur_expr;
02099       cur_expr = cur_expr->next_same_hash;
02100     }
02101 
02102   if (! found)
02103     {
02104       cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
02105       bytes_used += sizeof (struct expr);
02106       if (table->table[hash] == NULL)
02107   /* This is the first pattern that hashed to this index.  */
02108   table->table[hash] = cur_expr;
02109       else
02110   /* Add EXPR to end of this hash chain.  */
02111   last_expr->next_same_hash = cur_expr;
02112 
02113       /* Set the fields of the expr element.
02114    We must copy X because it can be modified when copy propagation is
02115    performed on its operands.  */
02116       cur_expr->expr = copy_rtx (x);
02117       cur_expr->bitmap_index = table->n_elems++;
02118       cur_expr->next_same_hash = NULL;
02119       cur_expr->antic_occr = NULL;
02120       cur_expr->avail_occr = NULL;
02121     }
02122 
02123   /* Now record the occurrence.  */
02124   cur_occr = cur_expr->avail_occr;
02125 
02126   /* Search for another occurrence in the same basic block.  */
02127   while (cur_occr && BLOCK_NUM (cur_occr->insn) != BLOCK_NUM (insn))
02128     {
02129       /* If an occurrence isn't found, save a pointer to the end of
02130    the list.  */
02131       last_occr = cur_occr;
02132       cur_occr = cur_occr->next;
02133     }
02134 
02135   if (cur_occr)
02136     /* Found another instance of the expression in the same basic block.
02137        Prefer this occurrence to the currently recorded one.  We want the
02138        last one in the block and the block is scanned from start to end.  */
02139     cur_occr->insn = insn;
02140   else
02141     {
02142       /* First occurrence of this expression in this basic block.  */
02143       cur_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
02144       bytes_used += sizeof (struct occr);
02145 
02146       /* First occurrence of this expression in any block?  */
02147       if (cur_expr->avail_occr == NULL)
02148   cur_expr->avail_occr = cur_occr;
02149       else
02150   last_occr->next = cur_occr;
02151 
02152       cur_occr->insn = insn;
02153       cur_occr->next = NULL;
02154     }
02155 }
02156 
02157 /* Scan pattern PAT of INSN and add an entry to the hash TABLE (set or
02158    expression one).  */
02159 
02160 static void
02161 hash_scan_set (pat, insn, table)
02162      rtx pat, insn;
02163      struct hash_table *table;
02164 {
02165   rtx src = SET_SRC (pat);
02166   rtx dest = SET_DEST (pat);
02167   rtx note;
02168 
02169   if (GET_CODE (src) == CALL)
02170     hash_scan_call (src, insn, table);
02171 
02172   else if (GET_CODE (dest) == REG)
02173     {
02174       unsigned int regno = REGNO (dest);
02175       rtx tmp;
02176 
02177       /* If this is a single set and we are doing constant propagation,
02178    see if a REG_NOTE shows this equivalent to a constant.  */
02179       if (table->set_p && (note = find_reg_equal_equiv_note (insn)) != 0
02180     && CONSTANT_P (XEXP (note, 0)))
02181   src = XEXP (note, 0), pat = gen_rtx_SET (VOIDmode, dest, src);
02182 
02183       /* Only record sets of pseudo-regs in the hash table.  */
02184       if (! table->set_p
02185     && regno >= FIRST_PSEUDO_REGISTER
02186     /* Don't GCSE something if we can't do a reg/reg copy.  */
02187     && can_copy_p [GET_MODE (dest)]
02188     /* GCSE commonly inserts instruction after the insn.  We can't
02189        do that easily for EH_REGION notes so disable GCSE on these
02190        for now.  */
02191     && !find_reg_note (insn, REG_EH_REGION, NULL_RTX)
02192     /* Is SET_SRC something we want to gcse?  */
02193     && want_to_gcse_p (src)
02194     /* Don't CSE a nop.  */
02195     && ! set_noop_p (pat)
02196     /* Don't GCSE if it has attached REG_EQUIV note.
02197        At this point this only function parameters should have
02198        REG_EQUIV notes and if the argument slot is used somewhere
02199        explicitly, it means address of parameter has been taken,
02200        so we should not extend the lifetime of the pseudo.  */
02201     && ((note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) == 0
02202         || GET_CODE (XEXP (note, 0)) != MEM))
02203   {
02204     /* An expression is not anticipatable if its operands are
02205        modified before this insn or if this is not the only SET in
02206        this insn.  */
02207     int antic_p = oprs_anticipatable_p (src, insn) && single_set (insn);
02208     /* An expression is not available if its operands are
02209        subsequently modified, including this insn.  It's also not
02210        available if this is a branch, because we can't insert
02211        a set after the branch.  */
02212     int avail_p = (oprs_available_p (src, insn)
02213        && ! JUMP_P (insn));
02214 
02215     insert_expr_in_table (src, GET_MODE (dest), insn, antic_p, avail_p, table);
02216   }
02217 
02218       /* Record sets for constant/copy propagation.  */
02219       else if (table->set_p
02220          && regno >= FIRST_PSEUDO_REGISTER
02221          && ((GET_CODE (src) == REG
02222         && REGNO (src) >= FIRST_PSEUDO_REGISTER
02223         && can_copy_p [GET_MODE (dest)]
02224         && REGNO (src) != regno)
02225        || CONSTANT_P (src))
02226          /* A copy is not available if its src or dest is subsequently
02227       modified.  Here we want to search from INSN+1 on, but
02228       oprs_available_p searches from INSN on.  */
02229          && (insn == BLOCK_END (BLOCK_NUM (insn))
02230        || ((tmp = next_nonnote_insn (insn)) != NULL_RTX
02231            && oprs_available_p (pat, tmp))))
02232   insert_set_in_table (pat, insn, table);
02233     }
02234 }
02235 
02236 static void
02237 hash_scan_clobber (x, insn, table)
02238      rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
02239      struct hash_table *table ATTRIBUTE_UNUSED;
02240 {
02241   /* Currently nothing to do.  */
02242 }
02243 
02244 static void
02245 hash_scan_call (x, insn, table)
02246      rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
02247      struct hash_table *table ATTRIBUTE_UNUSED;
02248 {
02249   /* Currently nothing to do.  */
02250 }
02251 
02252 /* Process INSN and add hash table entries as appropriate.
02253 
02254    Only available expressions that set a single pseudo-reg are recorded.
02255 
02256    Single sets in a PARALLEL could be handled, but it's an extra complication
02257    that isn't dealt with right now.  The trick is handling the CLOBBERs that
02258    are also in the PARALLEL.  Later.
02259 
02260    If SET_P is nonzero, this is for the assignment hash table,
02261    otherwise it is for the expression hash table.
02262    If IN_LIBCALL_BLOCK nonzero, we are in a libcall block, and should
02263    not record any expressions.  */
02264 
02265 static void
02266 hash_scan_insn (insn, table, in_libcall_block)
02267      rtx insn;
02268      struct hash_table *table;
02269      int in_libcall_block;
02270 {
02271   rtx pat = PATTERN (insn);
02272   int i;
02273 
02274   if (in_libcall_block)
02275     return;
02276 
02277   /* Pick out the sets of INSN and for other forms of instructions record
02278      what's been modified.  */
02279 
02280   if (GET_CODE (pat) == SET)
02281     hash_scan_set (pat, insn, table);
02282   else if (GET_CODE (pat) == PARALLEL)
02283     for (i = 0; i < XVECLEN (pat, 0); i++)
02284       {
02285   rtx x = XVECEXP (pat, 0, i);
02286 
02287   if (GET_CODE (x) == SET)
02288     hash_scan_set (x, insn, table);
02289   else if (GET_CODE (x) == CLOBBER)
02290     hash_scan_clobber (x, insn, table);
02291   else if (GET_CODE (x) == CALL)
02292     hash_scan_call (x, insn, table);
02293       }
02294 
02295   else if (GET_CODE (pat) == CLOBBER)
02296     hash_scan_clobber (pat, insn, table);
02297   else if (GET_CODE (pat) == CALL)
02298     hash_scan_call (pat, insn, table);
02299 }
02300 
02301 static void
02302 dump_hash_table (file, name, table)
02303      FILE *file;
02304      const char *name;
02305      struct hash_table *table;
02306 {
02307   int i;
02308   /* Flattened out table, so it's printed in proper order.  */
02309   struct expr **flat_table;
02310   unsigned int *hash_val;
02311   struct expr *expr;
02312 
02313   flat_table
02314     = (struct expr **) xcalloc (table->n_elems, sizeof (struct expr *));
02315   hash_val = (unsigned int *) xmalloc (table->n_elems * sizeof (unsigned int));
02316 
02317   for (i = 0; i < (int) table->size; i++)
02318     for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
02319       {
02320   flat_table[expr->bitmap_index] = expr;
02321   hash_val[expr->bitmap_index] = i;
02322       }
02323 
02324   fprintf (file, "%s hash table (%d buckets, %d entries)\n",
02325      name, table->size, table->n_elems);
02326 
02327   for (i = 0; i < (int) table->n_elems; i++)
02328     if (flat_table[i] != 0)
02329       {
02330   expr = flat_table[i];
02331   fprintf (file, "Index %d (hash value %d)\n  ",
02332      expr->bitmap_index, hash_val[i]);
02333   print_rtl (file, expr->expr);
02334   fprintf (file, "\n");
02335       }
02336 
02337   fprintf (file, "\n");
02338 
02339   free (flat_table);
02340   free (hash_val);
02341 }
02342 
02343 /* Record register first/last/block set information for REGNO in INSN.
02344 
02345    first_set records the first place in the block where the register
02346    is set and is used to compute "anticipatability".
02347 
02348    last_set records the last place in the block where the register
02349    is set and is used to compute "availability".
02350 
02351    last_bb records the block for which first_set and last_set are
02352    valid, as a quick test to invalidate them.
02353 
02354    reg_set_in_block records whether the register is set in the block
02355    and is used to compute "transparency".  */
02356 
02357 static void
02358 record_last_reg_set_info (insn, regno)
02359      rtx insn;
02360      int regno;
02361 {
02362   struct reg_avail_info *info = &reg_avail_info[regno];
02363   int cuid = INSN_CUID (insn);
02364 
02365   info->last_set = cuid;
02366   if (info->last_bb != current_bb)
02367     {
02368       info->last_bb = current_bb;
02369       info->first_set = cuid;
02370       SET_BIT (reg_set_in_block[current_bb->index], regno);
02371     }
02372 }
02373 
02374 
02375 /* Record all of the canonicalized MEMs of record_last_mem_set_info's insn.
02376    Note we store a pair of elements in the list, so they have to be
02377    taken off pairwise.  */
02378 
02379 static void
02380 canon_list_insert (dest, unused1, v_insn)
02381      rtx    dest ATTRIBUTE_UNUSED;
02382      rtx    unused1 ATTRIBUTE_UNUSED;
02383      void * v_insn;
02384 {
02385   rtx dest_addr, insn;
02386   int bb;
02387 
02388   while (GET_CODE (dest) == SUBREG
02389       || GET_CODE (dest) == ZERO_EXTRACT
02390       || GET_CODE (dest) == SIGN_EXTRACT
02391       || GET_CODE (dest) == STRICT_LOW_PART)
02392     dest = XEXP (dest, 0);
02393 
02394   /* If DEST is not a MEM, then it will not conflict with a load.  Note
02395      that function calls are assumed to clobber memory, but are handled
02396      elsewhere.  */
02397 
02398   if (GET_CODE (dest) != MEM)
02399     return;
02400 
02401   dest_addr = get_addr (XEXP (dest, 0));
02402   dest_addr = canon_rtx (dest_addr);
02403   insn = (rtx) v_insn;
02404   bb = BLOCK_NUM (insn);
02405 
02406   canon_modify_mem_list[bb] =
02407     alloc_EXPR_LIST (VOIDmode, dest_addr, canon_modify_mem_list[bb]);
02408   canon_modify_mem_list[bb] =
02409     alloc_EXPR_LIST (VOIDmode, dest, canon_modify_mem_list[bb]);
02410   bitmap_set_bit (canon_modify_mem_list_set, bb);
02411 }
02412 
02413 /* Record memory modification information for INSN.  We do not actually care
02414    about the memory location(s) that are set, or even how they are set (consider
02415    a CALL_INSN).  We merely need to record which insns modify memory.  */
02416 
02417 static void
02418 record_last_mem_set_info (insn)
02419      rtx insn;
02420 {
02421   int bb = BLOCK_NUM (insn);
02422 
02423   /* load_killed_in_block_p will handle the case of calls clobbering
02424      everything.  */
02425   modify_mem_list[bb] = alloc_INSN_LIST (insn, modify_mem_list[bb]);
02426   bitmap_set_bit (modify_mem_list_set, bb);
02427 
02428   if (GET_CODE (insn) == CALL_INSN)
02429     {
02430       /* Note that traversals of this loop (other than for free-ing)
02431    will break after encountering a CALL_INSN.  So, there's no
02432    need to insert a pair of items, as canon_list_insert does.  */
02433       canon_modify_mem_list[bb] =
02434   alloc_INSN_LIST (insn, canon_modify_mem_list[bb]);
02435       bitmap_set_bit (canon_modify_mem_list_set, bb);
02436     }
02437   else
02438     note_stores (PATTERN (insn), canon_list_insert, (void*) insn);
02439 }
02440 
02441 /* Called from compute_hash_table via note_stores to handle one
02442    SET or CLOBBER in an insn.  DATA is really the instruction in which
02443    the SET is taking place.  */
02444 
02445 static void
02446 record_last_set_info (dest, setter, data)
02447      rtx dest, setter ATTRIBUTE_UNUSED;
02448      void *data;
02449 {
02450   rtx last_set_insn = (rtx) data;
02451 
02452   if (GET_CODE (dest) == SUBREG)
02453     dest = SUBREG_REG (dest);
02454 
02455   if (GET_CODE (dest) == REG)
02456     record_last_reg_set_info (last_set_insn, REGNO (dest));
02457   else if (GET_CODE (dest) == MEM
02458      /* Ignore pushes, they clobber nothing.  */
02459      && ! push_operand (dest, GET_MODE (dest)))
02460     record_last_mem_set_info (last_set_insn);
02461 }
02462 
02463 /* Top level function to create an expression or assignment hash table.
02464 
02465    Expression entries are placed in the hash table if
02466    - they are of the form (set (pseudo-reg) src),
02467    - src is something we want to perform GCSE on,
02468    - none of the operands are subsequently modified in the block
02469 
02470    Assignment entries are placed in the hash table if
02471    - they are of the form (set (pseudo-reg) src),
02472    - src is something we want to perform const/copy propagation on,
02473    - none of the operands or target are subsequently modified in the block
02474 
02475    Currently src must be a pseudo-reg or a const_int.
02476 
02477    F is the first insn.
02478    TABLE is the table computed.  */
02479 
02480 static void
02481 compute_hash_table_work (table)
02482      struct hash_table *table;
02483 {
02484   unsigned int i;
02485 
02486   /* While we compute the hash table we also compute a bit array of which
02487      registers are set in which blocks.
02488      ??? This isn't needed during const/copy propagation, but it's cheap to
02489      compute.  Later.  */
02490   sbitmap_vector_zero (reg_set_in_block, last_basic_block);
02491 
02492   /* re-Cache any INSN_LIST nodes we have allocated.  */
02493   clear_modify_mem_tables ();
02494   /* Some working arrays used to track first and last set in each block.  */
02495   reg_avail_info = (struct reg_avail_info*)
02496     gmalloc (max_gcse_regno * sizeof (struct reg_avail_info));
02497 
02498   for (i = 0; i < max_gcse_regno; ++i)
02499     reg_avail_info[i].last_bb = NULL;
02500 
02501   FOR_EACH_BB (current_bb)
02502     {
02503       rtx insn;
02504       unsigned int regno;
02505       int in_libcall_block;
02506 
02507       /* First pass over the instructions records information used to
02508    determine when registers and memory are first and last set.
02509    ??? hard-reg reg_set_in_block computation
02510    could be moved to compute_sets since they currently don't change.  */
02511 
02512       for (insn = current_bb->head;
02513      insn && insn != NEXT_INSN (current_bb->end);
02514      insn = NEXT_INSN (insn))
02515   {
02516     if (! INSN_P (insn))
02517       continue;
02518 
02519     if (GET_CODE (insn) == CALL_INSN)
02520       {
02521         bool clobbers_all = false;
02522 #ifdef NON_SAVING_SETJMP
02523         if (NON_SAVING_SETJMP
02524       && find_reg_note (insn, REG_SETJMP, NULL_RTX))
02525     clobbers_all = true;
02526 #endif
02527 
02528         for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
02529     if (clobbers_all
02530         || TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
02531       record_last_reg_set_info (insn, regno);
02532 
02533         mark_call (insn);
02534       }
02535 
02536     note_stores (PATTERN (insn), record_last_set_info, insn);
02537   }
02538 
02539       /* The next pass builds the hash table.  */
02540 
02541       for (insn = current_bb->head, in_libcall_block = 0;
02542      insn && insn != NEXT_INSN (current_bb->end);
02543      insn = NEXT_INSN (insn))
02544   if (INSN_P (insn))
02545     {
02546       if (find_reg_note (insn, REG_LIBCALL, NULL_RTX))
02547         in_libcall_block = 1;
02548       else if (table->set_p && find_reg_note (insn, REG_RETVAL, NULL_RTX))
02549         in_libcall_block = 0;
02550       hash_scan_insn (insn, table, in_libcall_block);
02551       if (!table->set_p && find_reg_note (insn, REG_RETVAL, NULL_RTX))
02552         in_libcall_block = 0;
02553     }
02554     }
02555 
02556   free (reg_avail_info);
02557   reg_avail_info = NULL;
02558 }
02559 
02560 /* Allocate space for the set/expr hash TABLE.
02561    N_INSNS is the number of instructions in the function.
02562    It is used to determine the number of buckets to use.
02563    SET_P determines whether set or expression table will
02564    be created.  */
02565 
02566 static void
02567 alloc_hash_table (n_insns, table, set_p)
02568      int n_insns;
02569      struct hash_table *table;
02570      int set_p;
02571 {
02572   int n;
02573 
02574   table->size = n_insns / 4;
02575   if (table->size < 11)
02576     table->size = 11;
02577 
02578   /* Attempt to maintain efficient use of hash table.
02579      Making it an odd number is simplest for now.
02580      ??? Later take some measurements.  */
02581   table->size |= 1;
02582   n = table->size * sizeof (struct expr *);
02583   table->table = (struct expr **) gmalloc (n);
02584   table->set_p = set_p;
02585 }
02586 
02587 /* Free things allocated by alloc_hash_table.  */
02588 
02589 static void
02590 free_hash_table (table)
02591      struct hash_table *table;
02592 {
02593   free (table->table);
02594 }
02595 
02596 /* Compute the hash TABLE for doing copy/const propagation or
02597    expression hash table.  */
02598 
02599 static void
02600 compute_hash_table (table)
02601     struct hash_table *table;
02602 {
02603   /* Initialize count of number of entries in hash table.  */
02604   table->n_elems = 0;
02605   memset ((char *) table->table, 0,
02606     table->size * sizeof (struct expr *));
02607 
02608   compute_hash_table_work (table);
02609 }
02610 
02611 /* Expression tracking support.  */
02612 
02613 /* Lookup pattern PAT in the expression TABLE.
02614    The result is a pointer to the table entry, or NULL if not found.  */
02615 
02616 static struct expr *
02617 lookup_expr (pat, table)
02618      rtx pat;
02619      struct hash_table *table;
02620 {
02621   int do_not_record_p;
02622   unsigned int hash = hash_expr (pat, GET_MODE (pat), &do_not_record_p,
02623          table->size);
02624   struct expr *expr;
02625 
02626   if (do_not_record_p)
02627     return NULL;
02628 
02629   expr = table->table[hash];
02630 
02631   while (expr && ! expr_equiv_p (expr->expr, pat))
02632     expr = expr->next_same_hash;
02633 
02634   return expr;
02635 }
02636 
02637 /* Lookup REGNO in the set TABLE.  If PAT is non-NULL look for the entry that
02638    matches it, otherwise return the first entry for REGNO.  The result is a
02639    pointer to the table entry, or NULL if not found.  */
02640 
02641 static struct expr *
02642 lookup_set (regno, pat, table)
02643      unsigned int regno;
02644      rtx pat;
02645      struct hash_table *table;
02646 {
02647   unsigned int hash = hash_set (regno, table->size);
02648   struct expr *expr;
02649 
02650   expr = table->table[hash];
02651 
02652   if (pat)
02653     {
02654       while (expr && ! expr_equiv_p (expr->expr, pat))
02655   expr = expr->next_same_hash;
02656     }
02657   else
02658     {
02659       while (expr && REGNO (SET_DEST (expr->expr)) != regno)
02660   expr = expr->next_same_hash;
02661     }
02662 
02663   return expr;
02664 }
02665 
02666 /* Return the next entry for REGNO in list EXPR.  */
02667 
02668 static struct expr *
02669 next_set (regno, expr)
02670      unsigned int regno;
02671      struct expr *expr;
02672 {
02673   do
02674     expr = expr->next_same_hash;
02675   while (expr && REGNO (SET_DEST (expr->expr)) != regno);
02676 
02677   return expr;
02678 }
02679 
02680 /* Like free_INSN_LIST_list or free_EXPR_LIST_list, except that the node
02681    types may be mixed.  */
02682 
02683 static void
02684 free_insn_expr_list_list (listp)
02685      rtx *listp;
02686 {
02687   rtx list, next;
02688 
02689   for (list = *listp; list ; list = next)
02690     {
02691       next = XEXP (list, 1);
02692       if (GET_CODE (list) == EXPR_LIST)
02693   free_EXPR_LIST_node (list);
02694       else
02695   free_INSN_LIST_node (list);
02696     }
02697 
02698   *listp = NULL;
02699 }
02700 
02701 /* Clear canon_modify_mem_list and modify_mem_list tables.  */
02702 static void
02703 clear_modify_mem_tables ()
02704 {
02705   int i;
02706 
02707   EXECUTE_IF_SET_IN_BITMAP
02708     (modify_mem_list_set, 0, i, free_INSN_LIST_list (modify_mem_list + i));
02709   bitmap_clear (modify_mem_list_set);
02710 
02711   EXECUTE_IF_SET_IN_BITMAP
02712     (canon_modify_mem_list_set, 0, i,
02713      free_insn_expr_list_list (canon_modify_mem_list + i));
02714   bitmap_clear (canon_modify_mem_list_set);
02715 }
02716 
02717 /* Release memory used by modify_mem_list_set and canon_modify_mem_list_set.  */
02718 
02719 static void
02720 free_modify_mem_tables ()
02721 {
02722   clear_modify_mem_tables ();
02723   free (modify_mem_list);
02724   free (canon_modify_mem_list);
02725   modify_mem_list = 0;
02726   canon_modify_mem_list = 0;
02727 }
02728 
02729 /* Reset tables used to keep track of what's still available [since the
02730    start of the block].  */
02731 
02732 static void
02733 reset_opr_set_tables ()
02734 {
02735   /* Maintain a bitmap of which regs have been set since beginning of
02736      the block.  */
02737   CLEAR_REG_SET (reg_set_bitmap);
02738 
02739   /* Also keep a record of the last instruction to modify memory.
02740      For now this is very trivial, we only record whether any memory
02741      location has been modified.  */
02742   clear_modify_mem_tables ();
02743 }
02744 
02745 /* Return nonzero if the operands of X are not set before INSN in
02746    INSN's basic block.  */
02747 
02748 static int
02749 oprs_not_set_p (x, insn)
02750      rtx x, insn;
02751 {
02752   int i, j;
02753   enum rtx_code code;
02754   const char *fmt;
02755 
02756   if (x == 0)
02757     return 1;
02758 
02759   code = GET_CODE (x);
02760   switch (code)
02761     {
02762     case PC:
02763     case CC0:
02764     case CONST:
02765     case CONST_INT:
02766     case CONST_DOUBLE:
02767     case CONST_VECTOR:
02768     case SYMBOL_REF:
02769     case LABEL_REF:
02770     case ADDR_VEC:
02771     case ADDR_DIFF_VEC:
02772       return 1;
02773 
02774     case MEM:
02775       if (load_killed_in_block_p (BLOCK_FOR_INSN (insn),
02776           INSN_CUID (insn), x, 0))
02777   return 0;
02778       else
02779   return oprs_not_set_p (XEXP (x, 0), insn);
02780 
02781     case REG:
02782       return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
02783 
02784     default:
02785       break;
02786     }
02787 
02788   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
02789     {
02790       if (fmt[i] == 'e')
02791   {
02792     /* If we are about to do the last recursive call
02793        needed at this level, change it into iteration.
02794        This function is called enough to be worth it.  */
02795     if (i == 0)
02796       return oprs_not_set_p (XEXP (x, i), insn);
02797 
02798     if (! oprs_not_set_p (XEXP (x, i), insn))
02799       return 0;
02800   }
02801       else if (fmt[i] == 'E')
02802   for (j = 0; j < XVECLEN (x, i); j++)
02803     if (! oprs_not_set_p (XVECEXP (x, i, j), insn))
02804       return 0;
02805     }
02806 
02807   return 1;
02808 }
02809 
02810 /* Mark things set by a CALL.  */
02811 
02812 static void
02813 mark_call (insn)
02814      rtx insn;
02815 {
02816   if (! CONST_OR_PURE_CALL_P (insn))
02817     record_last_mem_set_info (insn);
02818 }
02819 
02820 /* Mark things set by a SET.  */
02821 
02822 static void
02823 mark_set (pat, insn)
02824      rtx pat, insn;
02825 {
02826   rtx dest = SET_DEST (pat);
02827 
02828   while (GET_CODE (dest) == SUBREG
02829    || GET_CODE (dest) == ZERO_EXTRACT
02830    || GET_CODE (dest) == SIGN_EXTRACT
02831    || GET_CODE (dest) == STRICT_LOW_PART)
02832     dest = XEXP (dest, 0);
02833 
02834   if (GET_CODE (dest) == REG)
02835     SET_REGNO_REG_SET (reg_set_bitmap, REGNO (dest));
02836   else if (GET_CODE (dest) == MEM)
02837     record_last_mem_set_info (insn);
02838 
02839   if (GET_CODE (SET_SRC (pat)) == CALL)
02840     mark_call (insn);
02841 }
02842 
02843 /* Record things set by a CLOBBER.  */
02844 
02845 static void
02846 mark_clobber (pat, insn)
02847      rtx pat, insn;
02848 {
02849   rtx clob = XEXP (pat, 0);
02850 
02851   while (GET_CODE (clob) == SUBREG || GET_CODE (clob) == STRICT_LOW_PART)
02852     clob = XEXP (clob, 0);
02853 
02854   if (GET_CODE (clob) == REG)
02855     SET_REGNO_REG_SET (reg_set_bitmap, REGNO (clob));
02856   else
02857     record_last_mem_set_info (insn);
02858 }
02859 
02860 /* Record things set by INSN.
02861    This data is used by oprs_not_set_p.  */
02862 
02863 static void
02864 mark_oprs_set (insn)
02865      rtx insn;
02866 {
02867   rtx pat = PATTERN (insn);
02868   int i;
02869 
02870   if (GET_CODE (pat) == SET)
02871     mark_set (pat, insn);
02872   else if (GET_CODE (pat) == PARALLEL)
02873     for (i = 0; i < XVECLEN (pat, 0); i++)
02874       {
02875   rtx x = XVECEXP (pat, 0, i);
02876 
02877   if (GET_CODE (x) == SET)
02878     mark_set (x, insn);
02879   else if (GET_CODE (x) == CLOBBER)
02880     mark_clobber (x, insn);
02881   else if (GET_CODE (x) == CALL)
02882     mark_call (insn);
02883       }
02884 
02885   else if (GET_CODE (pat) == CLOBBER)
02886     mark_clobber (pat, insn);
02887   else if (GET_CODE (pat) == CALL)
02888     mark_call (insn);
02889 }
02890 
02891 
02892 /* Classic GCSE reaching definition support.  */
02893 
02894 /* Allocate reaching def variables.  */
02895 
02896 static void
02897 alloc_rd_mem (n_blocks, n_insns)
02898      int n_blocks, n_insns;
02899 {
02900   rd_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
02901   sbitmap_vector_zero (rd_kill, n_blocks);
02902 
02903   rd_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
02904   sbitmap_vector_zero (rd_gen, n_blocks);
02905 
02906   reaching_defs = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
02907   sbitmap_vector_zero (reaching_defs, n_blocks);
02908 
02909   rd_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
02910   sbitmap_vector_zero (rd_out, n_blocks);
02911 }
02912 
02913 /* Free reaching def variables.  */
02914 
02915 static void
02916 free_rd_mem ()
02917 {
02918   sbitmap_vector_free (rd_kill);
02919   sbitmap_vector_free (rd_gen);
02920   sbitmap_vector_free (reaching_defs);
02921   sbitmap_vector_free (rd_out);
02922 }
02923 
02924 /* Add INSN to the kills of BB.  REGNO, set in BB, is killed by INSN.  */
02925 
02926 static void
02927 handle_rd_kill_set (insn, regno, bb)
02928      rtx insn;
02929      int regno;
02930      basic_block bb;
02931 {
02932   struct reg_set *this_reg;
02933 
02934   for (this_reg = reg_set_table[regno]; this_reg; this_reg = this_reg ->next)
02935     if (BLOCK_NUM (this_reg->insn) != BLOCK_NUM (insn))
02936       SET_BIT (rd_kill[bb->index], INSN_CUID (this_reg->insn));
02937 }
02938 
02939 /* Compute the set of kill's for reaching definitions.  */
02940 
02941 static void
02942 compute_kill_rd ()
02943 {
02944   int cuid;
02945   unsigned int regno;
02946   int i;
02947   basic_block bb;
02948 
02949   /* For each block
02950        For each set bit in `gen' of the block (i.e each insn which
02951      generates a definition in the block)
02952    Call the reg set by the insn corresponding to that bit regx
02953    Look at the linked list starting at reg_set_table[regx]
02954    For each setting of regx in the linked list, which is not in
02955        this block
02956      Set the bit in `kill' corresponding to that insn.  */
02957   FOR_EACH_BB (bb)
02958     for (cuid = 0; cuid < max_cuid; cuid++)
02959       if (TEST_BIT (rd_gen[bb->index], cuid))
02960   {
02961     rtx insn = CUID_INSN (cuid);
02962     rtx pat = PATTERN (insn);
02963 
02964     if (GET_CODE (insn) == CALL_INSN)
02965       {
02966         for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
02967     if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
02968       handle_rd_kill_set (insn, regno, bb);
02969       }
02970 
02971     if (GET_CODE (pat) == PARALLEL)
02972       {
02973         for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
02974     {
02975       enum rtx_code code = GET_CODE (XVECEXP (pat, 0, i));
02976 
02977       if ((code == SET || code == CLOBBER)
02978           && GET_CODE (XEXP (XVECEXP (pat, 0, i), 0)) == REG)
02979         handle_rd_kill_set (insn,
02980           REGNO (XEXP (XVECEXP (pat, 0, i), 0)),
02981           bb);
02982     }
02983       }
02984     else if (GET_CODE (pat) == SET && GET_CODE (SET_DEST (pat)) == REG)
02985       /* Each setting of this register outside of this block
02986          must be marked in the set of kills in this block.  */
02987       handle_rd_kill_set (insn, REGNO (SET_DEST (pat)), bb);
02988   }
02989 }
02990 
02991 /* Compute the reaching definitions as in
02992    Compilers Principles, Techniques, and Tools. Aho, Sethi, Ullman,
02993    Chapter 10.  It is the same algorithm as used for computing available
02994    expressions but applied to the gens and kills of reaching definitions.  */
02995 
02996 static void
02997 compute_rd ()
02998 {
02999   int changed, passes;
03000   basic_block bb;
03001 
03002   FOR_EACH_BB (bb)
03003     sbitmap_copy (rd_out[bb->index] /*dst*/, rd_gen[bb->index] /*src*/);
03004 
03005   passes = 0;
03006   changed = 1;
03007   while (changed)
03008     {
03009       changed = 0;
03010       FOR_EACH_BB (bb)
03011   {
03012     sbitmap_union_of_preds (reaching_defs[bb->index], rd_out, bb->index);
03013     changed |= sbitmap_union_of_diff_cg (rd_out[bb->index], rd_gen[bb->index],
03014                  reaching_defs[bb->index], rd_kill[bb->index]);
03015   }
03016       passes++;
03017     }
03018 
03019   if (gcse_file)
03020     fprintf (gcse_file, "reaching def computation: %d passes\n", passes);
03021 }
03022 
03023 /* Classic GCSE available expression support.  */
03024 
03025 /* Allocate memory for available expression computation.  */
03026 
03027 static void
03028 alloc_avail_expr_mem (n_blocks, n_exprs)
03029      int n_blocks, n_exprs;
03030 {
03031   ae_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
03032   sbitmap_vector_zero (ae_kill, n_blocks);
03033 
03034   ae_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
03035   sbitmap_vector_zero (ae_gen, n_blocks);
03036 
03037   ae_in = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
03038   sbitmap_vector_zero (ae_in, n_blocks);
03039 
03040   ae_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
03041   sbitmap_vector_zero (ae_out, n_blocks);
03042 }
03043 
03044 static void
03045 free_avail_expr_mem ()
03046 {
03047   sbitmap_vector_free (ae_kill);
03048   sbitmap_vector_free (ae_gen);
03049   sbitmap_vector_free (ae_in);
03050   sbitmap_vector_free (ae_out);
03051 }
03052 
03053 /* Compute the set of available expressions generated in each basic block.  */
03054 
03055 static void
03056 compute_ae_gen (expr_hash_table)
03057      struct hash_table *expr_hash_table;
03058 {
03059   unsigned int i;
03060   struct expr *expr;
03061   struct occr *occr;
03062 
03063   /* For each recorded occurrence of each expression, set ae_gen[bb][expr].
03064      This is all we have to do because an expression is not recorded if it
03065      is not available, and the only expressions we want to work with are the
03066      ones that are recorded.  */
03067   for (i = 0; i < expr_hash_table->size; i++)
03068     for (expr = expr_hash_table->table[i]; expr != 0; expr = expr->next_same_hash)
03069       for (occr = expr->avail_occr; occr != 0; occr = occr->next)
03070   SET_BIT (ae_gen[BLOCK_NUM (occr->insn)], expr->bitmap_index);
03071 }
03072 
03073 /* Return nonzero if expression X is killed in BB.  */
03074 
03075 static int
03076 expr_killed_p (x, bb)
03077      rtx x;
03078      basic_block bb;
03079 {
03080   int i, j;
03081   enum rtx_code code;
03082   const char *fmt;
03083 
03084   if (x == 0)
03085     return 1;
03086 
03087   code = GET_CODE (x);
03088   switch (code)
03089     {
03090     case REG:
03091       return TEST_BIT (reg_set_in_block[bb->index], REGNO (x));
03092 
03093     case MEM:
03094       if (load_killed_in_block_p (bb, get_max_uid () + 1, x, 0))
03095   return 1;
03096       else
03097   return expr_killed_p (XEXP (x, 0), bb);
03098 
03099     case PC:
03100     case CC0: /*FIXME*/
03101     case CONST:
03102     case CONST_INT:
03103     case CONST_DOUBLE:
03104     case CONST_VECTOR:
03105     case SYMBOL_REF:
03106     case LABEL_REF:
03107     case ADDR_VEC:
03108     case ADDR_DIFF_VEC:
03109       return 0;
03110 
03111     default:
03112       break;
03113     }
03114 
03115   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
03116     {
03117       if (fmt[i] == 'e')
03118   {
03119     /* If we are about to do the last recursive call
03120        needed at this level, change it into iteration.
03121        This function is called enough to be worth it.  */
03122     if (i == 0)
03123       return expr_killed_p (XEXP (x, i), bb);
03124     else if (expr_killed_p (XEXP (x, i), bb))
03125       return 1;
03126   }
03127       else if (fmt[i] == 'E')
03128   for (j = 0; j < XVECLEN (x, i); j++)
03129     if (expr_killed_p (XVECEXP (x, i, j), bb))
03130       return 1;
03131     }
03132 
03133   return 0;
03134 }
03135 
03136 /* Compute the set of available expressions killed in each basic block.  */
03137 
03138 static void
03139 compute_ae_kill (ae_gen, ae_kill, expr_hash_table)
03140      sbitmap *ae_gen, *ae_kill;
03141      struct hash_table *expr_hash_table;
03142 {
03143   basic_block bb;
03144   unsigned int i;
03145   struct expr *expr;
03146 
03147   FOR_EACH_BB (bb)
03148     for (i = 0; i < expr_hash_table->size; i++)
03149       for (expr = expr_hash_table->table[i]; expr; expr = expr->next_same_hash)
03150   {
03151     /* Skip EXPR if generated in this block.  */
03152     if (TEST_BIT (ae_gen[bb->index], expr->bitmap_index))
03153       continue;
03154 
03155     if (expr_killed_p (expr->expr, bb))
03156       SET_BIT (ae_kill[bb->index], expr->bitmap_index);
03157   }
03158 }
03159 
03160 /* Actually perform the Classic GCSE optimizations.  */
03161 
03162 /* Return nonzero if occurrence OCCR of expression EXPR reaches block BB.
03163 
03164    CHECK_SELF_LOOP is nonzero if we should consider a block reaching itself
03165    as a positive reach.  We want to do this when there are two computations
03166    of the expression in the block.
03167 
03168    VISITED is a pointer to a working buffer for tracking which BB's have
03169    been visited.  It is NULL for the top-level call.
03170 
03171    We treat reaching expressions that go through blocks containing the same
03172    reaching expression as "not reaching".  E.g. if EXPR is generated in blocks
03173    2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
03174    2 as not reaching.  The intent is to improve the probability of finding
03175    only one reaching expression and to reduce register lifetimes by picking
03176    the closest such expression.  */
03177 
03178 static int
03179 expr_reaches_here_p_work (occr, expr, bb, check_self_loop, visited)
03180      struct occr *occr;
03181      struct expr *expr;
03182      basic_block bb;
03183      int check_self_loop;
03184      char *visited;
03185 {
03186   edge pred;
03187 
03188   for (pred = bb->pred; pred != NULL; pred = pred->pred_next)
03189     {
03190       basic_block pred_bb = pred->src;
03191 
03192       if (visited[pred_bb->index])
03193   /* This predecessor has already been visited. Nothing to do.  */
03194     ;
03195       else if (pred_bb == bb)
03196   {
03197     /* BB loops on itself.  */
03198     if (check_self_loop
03199         && TEST_BIT (ae_gen[pred_bb->index], expr->bitmap_index)
03200         && BLOCK_NUM (occr->insn) == pred_bb->index)
03201       return 1;
03202 
03203     visited[pred_bb->index] = 1;
03204   }
03205 
03206       /* Ignore this predecessor if it kills the expression.  */
03207       else if (TEST_BIT (ae_kill[pred_bb->index], expr->bitmap_index))
03208   visited[pred_bb->index] = 1;
03209 
03210       /* Does this predecessor generate this expression?  */
03211       else if (TEST_BIT (ae_gen[pred_bb->index], expr->bitmap_index))
03212   {
03213     /* Is this the occurrence we're looking for?
03214        Note that there's only one generating occurrence per block
03215        so we just need to check the block number.  */
03216     if (BLOCK_NUM (occr->insn) == pred_bb->index)
03217       return 1;
03218 
03219     visited[pred_bb->index] = 1;
03220   }
03221 
03222       /* Neither gen nor kill.  */
03223       else
03224   {
03225     visited[pred_bb->index] = 1;
03226     if (expr_reaches_here_p_work (occr, expr, pred_bb, check_self_loop,
03227         visited))
03228 
03229       return 1;
03230   }
03231     }
03232 
03233   /* All paths have been checked.  */
03234   return 0;
03235 }
03236 
03237 /* This wrapper for expr_reaches_here_p_work() is to ensure that any
03238    memory allocated for that function is returned.  */
03239 
03240 static int
03241 expr_reaches_here_p (occr, expr, bb, check_self_loop)
03242      struct occr *occr;
03243      struct expr *expr;
03244      basic_block bb;
03245      int check_self_loop;
03246 {
03247   int rval;
03248   char *visited = (char *) xcalloc (last_basic_block, 1);
03249 
03250   rval = expr_reaches_here_p_work (occr, expr, bb, check_self_loop, visited);
03251 
03252   free (visited);
03253   return rval;
03254 }
03255 
03256 /* Return the instruction that computes EXPR that reaches INSN's basic block.
03257    If there is more than one such instruction, return NULL.
03258 
03259    Called only by handle_avail_expr.  */
03260 
03261 static rtx
03262 computing_insn (expr, insn)
03263      struct expr *expr;
03264      rtx insn;
03265 {
03266   basic_block bb = BLOCK_FOR_INSN (insn);
03267 
03268   if (expr->avail_occr->next == NULL)
03269     {
03270       if (BLOCK_FOR_INSN (expr->avail_occr->insn) == bb)
03271   /* The available expression is actually itself
03272      (i.e. a loop in the flow graph) so do nothing.  */
03273   return NULL;
03274 
03275       /* (FIXME) Case that we found a pattern that was created by
03276    a substitution that took place.  */
03277       return expr->avail_occr->insn;
03278     }
03279   else
03280     {
03281       /* Pattern is computed more than once.
03282    Search backwards from this insn to see how many of these
03283    computations actually reach this insn.  */
03284       struct occr *occr;
03285       rtx insn_computes_expr = NULL;
03286       int can_reach = 0;
03287 
03288       for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
03289   {
03290     if (BLOCK_FOR_INSN (occr->insn) == bb)
03291       {
03292         /* The expression is generated in this block.
03293      The only time we care about this is when the expression
03294      is generated later in the block [and thus there's a loop].
03295      We let the normal cse pass handle the other cases.  */
03296         if (INSN_CUID (insn) < INSN_CUID (occr->insn)
03297       && expr_reaches_here_p (occr, expr, bb, 1))
03298     {
03299       can_reach++;
03300       if (can_reach > 1)
03301         return NULL;
03302 
03303       insn_computes_expr = occr->insn;
03304     }
03305       }
03306     else if (expr_reaches_here_p (occr, expr, bb, 0))
03307       {
03308         can_reach++;
03309         if (can_reach > 1)
03310     return NULL;
03311 
03312         insn_computes_expr = occr->insn;
03313       }
03314   }
03315 
03316       if (insn_computes_expr == NULL)
03317   abort ();
03318 
03319       return insn_computes_expr;
03320     }
03321 }
03322 
03323 /* Return nonzero if the definition in DEF_INSN can reach INSN.
03324    Only called by can_disregard_other_sets.  */
03325 
03326 static int
03327 def_reaches_here_p (insn, def_insn)
03328      rtx insn, def_insn;
03329 {
03330   rtx reg;
03331 
03332   if (TEST_BIT (reaching_defs[BLOCK_NUM (insn)], INSN_CUID (def_insn)))
03333     return 1;
03334 
03335   if (BLOCK_NUM (insn) == BLOCK_NUM (def_insn))
03336     {
03337       if (INSN_CUID (def_insn) < INSN_CUID (insn))
03338   {
03339     if (GET_CODE (PATTERN (def_insn)) == PARALLEL)
03340       return 1;
03341     else if (GET_CODE (PATTERN (def_insn)) == CLOBBER)
03342       reg = XEXP (PATTERN (def_insn), 0);
03343     else if (GET_CODE (PATTERN (def_insn)) == SET)
03344       reg = SET_DEST (PATTERN (def_insn));
03345     else
03346       abort ();
03347 
03348     return ! reg_set_between_p (reg, NEXT_INSN (def_insn), insn);
03349   }
03350       else
03351   return 0;
03352     }
03353 
03354   return 0;
03355 }
03356 
03357 /* Return nonzero if *ADDR_THIS_REG can only have one value at INSN.  The
03358    value returned is the number of definitions that reach INSN.  Returning a
03359    value of zero means that [maybe] more than one definition reaches INSN and
03360    the caller can't perform whatever optimization it is trying.  i.e. it is
03361    always safe to return zero.  */
03362 
03363 static int
03364 can_disregard_other_sets (addr_this_reg, insn, for_combine)
03365      struct reg_set **addr_this_reg;
03366      rtx insn;
03367      int for_combine;
03368 {
03369   int number_of_reaching_defs = 0;
03370   struct reg_set *this_reg;
03371 
03372   for (this_reg = *addr_this_reg; this_reg != 0; this_reg = this_reg->next)
03373     if (def_reaches_here_p (insn, this_reg->insn))
03374       {
03375   number_of_reaching_defs++;
03376   /* Ignore parallels for now.  */
03377   if (GET_CODE (PATTERN (this_reg->insn)) == PARALLEL)
03378     return 0;
03379 
03380   if (!for_combine
03381       && (GET_CODE (PATTERN (this_reg->insn)) == CLOBBER
03382     || ! rtx_equal_p (SET_SRC (PATTERN (this_reg->insn)),
03383           SET_SRC (PATTERN (insn)))))
03384     /* A setting of the reg to a different value reaches INSN.  */
03385     return 0;
03386 
03387   if (number_of_reaching_defs > 1)
03388     {
03389       /* If in this setting the value the register is being set to is
03390          equal to the previous value the register was set to and this
03391          setting reaches the insn we are trying to do the substitution
03392          on then we are ok.  */
03393       if (GET_CODE (PATTERN (this_reg->insn)) == CLOBBER)
03394         return 0;
03395       else if (! rtx_equal_p (SET_SRC (PATTERN (this_reg->insn)),
03396             SET_SRC (PATTERN (insn))))
03397         return 0;
03398     }
03399 
03400   *addr_this_reg = this_reg;
03401       }
03402 
03403   return number_of_reaching_defs;
03404 }
03405 
03406 /* Expression computed by insn is available and the substitution is legal,
03407    so try to perform the substitution.
03408 
03409    The result is nonzero if any changes were made.  */
03410 
03411 static int
03412 handle_avail_expr (insn, expr)
03413      rtx insn;
03414      struct expr *expr;
03415 {
03416   rtx pat, insn_computes_expr, expr_set;
03417   rtx to;
03418   struct reg_set *this_reg;
03419   int found_setting, use_src;
03420   int changed = 0;
03421 
03422   /* We only handle the case where one computation of the expression
03423      reaches this instruction.  */
03424   insn_computes_expr = computing_insn (expr, insn);
03425   if (insn_computes_expr == NULL)
03426     return 0;
03427   expr_set = single_set (insn_computes_expr);
03428   if (!expr_set)
03429     abort ();
03430 
03431   found_setting = 0;
03432   use_src = 0;
03433 
03434   /* At this point we know only one computation of EXPR outside of this
03435      block reaches this insn.  Now try to find a register that the
03436      expression is computed into.  */
03437   if (GET_CODE (SET_SRC (expr_set)) == REG)
03438     {
03439       /* This is the case when the available expression that reaches
03440    here has already been handled as an available expression.  */
03441       unsigned int regnum_for_replacing
03442   = REGNO (SET_SRC (expr_set));
03443 
03444       /* If the register was created by GCSE we can't use `reg_set_table',
03445    however we know it's set only once.  */
03446       if (regnum_for_replacing >= max_gcse_regno
03447     /* If the register the expression is computed into is set only once,
03448        or only one set reaches this insn, we can use it.  */
03449     || (((this_reg = reg_set_table[regnum_for_replacing]),
03450          this_reg->next == NULL)
03451         || can_disregard_other_sets (&this_reg, insn, 0)))
03452   {
03453     use_src = 1;
03454     found_setting = 1;
03455   }
03456     }
03457 
03458   if (!found_setting)
03459     {
03460       unsigned int regnum_for_replacing
03461   = REGNO (SET_DEST (expr_set));
03462 
03463       /* This shouldn't happen.  */
03464       if (regnum_for_replacing >= max_gcse_regno)
03465   abort ();
03466 
03467       this_reg = reg_set_table[regnum_for_replacing];
03468 
03469       /* If the register the expression is computed into is set only once,
03470    or only one set reaches this insn, use it.  */
03471       if (this_reg->next == NULL
03472     || can_disregard_other_sets (&this_reg, insn, 0))
03473   found_setting = 1;
03474     }
03475 
03476   if (found_setting)
03477     {
03478       pat = PATTERN (insn);
03479       if (use_src)
03480   to = SET_SRC (expr_set);
03481       else
03482   to = SET_DEST (expr_set);
03483       changed = validate_change (insn, &SET_SRC (pat), to, 0);
03484 
03485       /* We should be able to ignore the return code from validate_change but
03486    to play it safe we check.  */
03487       if (changed)
03488   {
03489     gcse_subst_count++;
03490     if (gcse_file != NULL)
03491       {
03492         fprintf (gcse_file, "GCSE: Replacing the source in insn %d with",
03493            INSN_UID (insn));
03494         fprintf (gcse_file, " reg %d %s insn %d\n",
03495            REGNO (to), use_src ? "from" : "set in",
03496            INSN_UID (insn_computes_expr));
03497       }
03498   }
03499     }
03500 
03501   /* The register that the expr is computed into is set more than once.  */
03502   else if (1 /*expensive_op(this_pattrn->op) && do_expensive_gcse)*/)
03503     {
03504       /* Insert an insn after insnx that copies the reg set in insnx
03505    into a new pseudo register call this new register REGN.
03506    From insnb until end of basic block or until REGB is set
03507    replace all uses of REGB with REGN.  */
03508       rtx new_insn;
03509 
03510       to = gen_reg_rtx (GET_MODE (SET_DEST (expr_set)));
03511 
03512       /* Generate the new insn.  */
03513       /* ??? If the change fails, we return 0, even though we created
03514    an insn.  I think this is ok.  */
03515       new_insn
03516   = emit_insn_after (gen_rtx_SET (VOIDmode, to,
03517           SET_DEST (expr_set)),
03518          insn_computes_expr);
03519 
03520       /* Keep register set table up to date.  */
03521       record_one_set (REGNO (to), new_insn);
03522 
03523       gcse_create_count++;
03524       if (gcse_file != NULL)
03525   {
03526     fprintf (gcse_file, "GCSE: Creating insn %d to copy value of reg %d",
03527        INSN_UID (NEXT_INSN (insn_computes_expr)),
03528        REGNO (SET_SRC (PATTERN (NEXT_INSN (insn_computes_expr)))));
03529     fprintf (gcse_file, ", computed in insn %d,\n",
03530        INSN_UID (insn_computes_expr));
03531     fprintf (gcse_file, "      into newly allocated reg %d\n",
03532        REGNO (to));
03533   }
03534 
03535       pat = PATTERN (insn);
03536 
03537       /* Do register replacement for INSN.  */
03538       changed = validate_change (insn, &SET_SRC (pat),
03539          SET_DEST (PATTERN
03540              (NEXT_INSN (insn_computes_expr))),
03541          0);
03542 
03543       /* We should be able to ignore the return code from validate_change but
03544    to play it safe we check.  */
03545       if (changed)
03546   {
03547     gcse_subst_count++;
03548     if (gcse_file != NULL)
03549       {
03550         fprintf (gcse_file,
03551            "GCSE: Replacing the source in insn %d with reg %d ",
03552            INSN_UID (insn),
03553            REGNO (SET_DEST (PATTERN (NEXT_INSN
03554              (insn_computes_expr)))));
03555         fprintf (gcse_file, "set in insn %d\n",
03556            INSN_UID (insn_computes_expr));
03557       }
03558   }
03559     }
03560 
03561   return changed;
03562 }
03563 
03564 /* Perform classic GCSE.  This is called by one_classic_gcse_pass after all
03565    the dataflow analysis has been done.
03566 
03567    The result is nonzero if a change was made.  */
03568 
03569 static int
03570 classic_gcse ()
03571 {
03572   int changed;
03573   rtx insn;
03574   basic_block bb;
03575 
03576   /* Note we start at block 1.  */
03577 
03578   if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
03579     return 0;
03580 
03581   changed = 0;
03582   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb, EXIT_BLOCK_PTR, next_bb)
03583     {
03584       /* Reset tables used to keep track of what's still valid [since the
03585    start of the block].  */
03586       reset_opr_set_tables ();
03587 
03588       for (insn = bb->head;
03589      insn != NULL && insn != NEXT_INSN (bb->end);
03590      insn = NEXT_INSN (insn))
03591   {
03592     /* Is insn of form (set (pseudo-reg) ...)?  */
03593     if (GET_CODE (insn) == INSN
03594         && GET_CODE (PATTERN (insn)) == SET
03595         && GET_CODE (SET_DEST (PATTERN (insn))) == REG
03596         && REGNO (SET_DEST (PATTERN (insn))) >= FIRST_PSEUDO_REGISTER)
03597       {
03598         rtx pat = PATTERN (insn);
03599         rtx src = SET_SRC (pat);
03600         struct expr *expr;
03601 
03602         if (want_to_gcse_p (src)
03603       /* Is the expression recorded?  */
03604       && ((expr = lookup_expr (src, &expr_hash_table)) != NULL)
03605       /* Is the expression available [at the start of the
03606          block]?  */
03607       && TEST_BIT (ae_in[bb->index], expr->bitmap_index)
03608       /* Are the operands unchanged since the start of the
03609          block?  */
03610       && oprs_not_set_p (src, insn))
03611     changed |= handle_avail_expr (insn, expr);
03612       }
03613 
03614     /* Keep track of everything modified by this insn.  */
03615     /* ??? Need to be careful w.r.t. mods done to INSN.  */
03616     if (INSN_P (insn))
03617       mark_oprs_set (insn);
03618   }
03619     }
03620 
03621   return changed;
03622 }
03623 
03624 /* Top level routine to perform one classic GCSE pass.
03625 
03626    Return nonzero if a change was made.  */
03627 
03628 static int
03629 one_classic_gcse_pass (pass)
03630      int pass;
03631 {
03632   int changed = 0;
03633 
03634   gcse_subst_count = 0;
03635   gcse_create_count = 0;
03636 
03637   alloc_hash_table (max_cuid, &expr_hash_table, 0);
03638   alloc_rd_mem (last_basic_block, max_cuid);
03639   compute_hash_table (&expr_hash_table);
03640   if (gcse_file)
03641     dump_hash_table (gcse_file, "Expression", &expr_hash_table);
03642 
03643   if (expr_hash_table.n_elems > 0)
03644     {
03645       compute_kill_rd ();
03646       compute_rd ();
03647       alloc_avail_expr_mem (last_basic_block, expr_hash_table.n_elems);
03648       compute_ae_gen (&expr_hash_table);
03649       compute_ae_kill (ae_gen, ae_kill, &expr_hash_table);
03650       compute_available (ae_gen, ae_kill, ae_out, ae_in);
03651       changed = classic_gcse ();
03652       free_avail_expr_mem ();
03653     }
03654 
03655   free_rd_mem ();
03656   free_hash_table (&expr_hash_table);
03657 
03658   if (gcse_file)
03659     {
03660       fprintf (gcse_file, "\n");
03661       fprintf (gcse_file, "GCSE of %s, pass %d: %d bytes needed, %d substs,",
03662          current_function_name, pass, bytes_used, gcse_subst_count);
03663       fprintf (gcse_file, "%d insns created\n", gcse_create_count);
03664     }
03665 
03666   return changed;
03667 }
03668 
03669 /* Compute copy/constant propagation working variables.  */
03670 
03671 /* Local properties of assignments.  */
03672 static sbitmap *cprop_pavloc;
03673 static sbitmap *cprop_absaltered;
03674 
03675 /* Global properties of assignments (computed from the local properties).  */
03676 static sbitmap *cprop_avin;
03677 static sbitmap *cprop_avout;
03678 
03679 /* Allocate vars used for copy/const propagation.  N_BLOCKS is the number of
03680    basic blocks.  N_SETS is the number of sets.  */
03681 
03682 static void
03683 alloc_cprop_mem (n_blocks, n_sets)
03684      int n_blocks, n_sets;
03685 {
03686   cprop_pavloc = sbitmap_vector_alloc (n_blocks, n_sets);
03687   cprop_absaltered = sbitmap_vector_alloc (n_blocks, n_sets);
03688 
03689   cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
03690   cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
03691 }
03692 
03693 /* Free vars used by copy/const propagation.  */
03694 
03695 static void
03696 free_cprop_mem ()
03697 {
03698   sbitmap_vector_free (cprop_pavloc);
03699   sbitmap_vector_free (cprop_absaltered);
03700   sbitmap_vector_free (cprop_avin);
03701   sbitmap_vector_free (cprop_avout);
03702 }
03703 
03704 /* For each block, compute whether X is transparent.  X is either an
03705    expression or an assignment [though we don't care which, for this context
03706    an assignment is treated as an expression].  For each block where an
03707    element of X is modified, set (SET_P == 1) or reset (SET_P == 0) the INDX
03708    bit in BMAP.  */
03709 
03710 static void
03711 compute_transp (x, indx, bmap, set_p)
03712      rtx x;
03713      int indx;
03714      sbitmap *bmap;
03715      int set_p;
03716 {
03717   int i, j;
03718   basic_block bb;
03719   enum rtx_code code;
03720   reg_set *r;
03721   const char *fmt;
03722 
03723   /* repeat is used to turn tail-recursion into iteration since GCC
03724      can't do it when there's no return value.  */
03725  repeat:
03726 
03727   if (x == 0)
03728     return;
03729 
03730   code = GET_CODE (x);
03731   switch (code)
03732     {
03733     case REG:
03734       if (set_p)
03735   {
03736     if (REGNO (x) < FIRST_PSEUDO_REGISTER)
03737       {
03738         FOR_EACH_BB (bb)
03739     if (TEST_BIT (reg_set_in_block[bb->index], REGNO (x)))
03740       SET_BIT (bmap[bb->index], indx);
03741       }
03742     else
03743       {
03744         for (r = reg_set_table[REGNO (x)]; r != NULL; r = r->next)
03745     SET_BIT (bmap[BLOCK_NUM (r->insn)], indx);
03746       }
03747   }
03748       else
03749   {
03750     if (REGNO (x) < FIRST_PSEUDO_REGISTER)
03751       {
03752         FOR_EACH_BB (bb)
03753     if (TEST_BIT (reg_set_in_block[bb->index], REGNO (x)))
03754       RESET_BIT (bmap[bb->index], indx);
03755       }
03756     else
03757       {
03758         for (r = reg_set_table[REGNO (x)]; r != NULL; r = r->next)
03759     RESET_BIT (bmap[BLOCK_NUM (r->insn)], indx);
03760       }
03761   }
03762 
03763       return;
03764 
03765     case MEM:
03766       FOR_EACH_BB (bb)
03767   {
03768     rtx list_entry = canon_modify_mem_list[bb->index];
03769 
03770     while (list_entry)
03771       {
03772         rtx dest, dest_addr;
03773 
03774         if (GET_CODE (XEXP (list_entry, 0)) == CALL_INSN)
03775     {
03776       if (set_p)
03777         SET_BIT (bmap[bb->index], indx);
03778       else
03779         RESET_BIT (bmap[bb->index], indx);
03780       break;
03781     }
03782         /* LIST_ENTRY must be an INSN of some kind that sets memory.
03783      Examine each hunk of memory that is modified.  */
03784 
03785         dest = XEXP (list_entry, 0);
03786         list_entry = XEXP (list_entry, 1);
03787         dest_addr = XEXP (list_entry, 0);
03788 
03789         if (canon_true_dependence (dest, GET_MODE (dest), dest_addr,
03790            x, rtx_addr_varies_p))
03791     {
03792       if (set_p)
03793         SET_BIT (bmap[bb->index], indx);
03794       else
03795         RESET_BIT (bmap[bb->index], indx);
03796       break;
03797     }
03798         list_entry = XEXP (list_entry, 1);
03799       }
03800   }
03801 
03802       x = XEXP (x, 0);
03803       goto repeat;
03804 
03805     case PC:
03806     case CC0: /*FIXME*/
03807     case CONST:
03808     case CONST_INT:
03809     case CONST_DOUBLE:
03810     case CONST_VECTOR:
03811     case SYMBOL_REF:
03812     case LABEL_REF:
03813     case ADDR_VEC:
03814     case ADDR_DIFF_VEC:
03815       return;
03816 
03817     default:
03818       break;
03819     }
03820 
03821   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
03822     {
03823       if (fmt[i] == 'e')
03824   {
03825     /* If we are about to do the last recursive call
03826        needed at this level, change it into iteration.
03827        This function is called enough to be worth it.  */
03828     if (i == 0)
03829       {
03830         x = XEXP (x, i);
03831         goto repeat;
03832       }
03833 
03834     compute_transp (XEXP (x, i), indx, bmap, set_p);
03835   }
03836       else if (fmt[i] == 'E')
03837   for (j = 0; j < XVECLEN (x, i); j++)
03838     compute_transp (XVECEXP (x, i, j), indx, bmap, set_p);
03839     }
03840 }
03841 
03842 /* Top level routine to do the dataflow analysis needed by copy/const
03843    propagation.  */
03844 
03845 static void
03846 compute_cprop_data ()
03847 {
03848   compute_local_properties (cprop_absaltered, cprop_pavloc, NULL, &set_hash_table);
03849   compute_available (cprop_pavloc, cprop_absaltered,
03850          cprop_avout, cprop_avin);
03851 }
03852 
03853 /* Copy/constant propagation.  */
03854 
03855 /* Maximum number of register uses in an insn that we handle.  */
03856 #define MAX_USES 8
03857 
03858 /* Table of uses found in an insn.
03859    Allocated statically to avoid alloc/free complexity and overhead.  */
03860 static struct reg_use reg_use_table[MAX_USES];
03861 
03862 /* Index into `reg_use_table' while building it.  */
03863 static int reg_use_count;
03864 
03865 /* Set up a list of register numbers used in INSN.  The found uses are stored
03866    in `reg_use_table'.  `reg_use_count' is initialized to zero before entry,
03867    and contains the number of uses in the table upon exit.
03868 
03869    ??? If a register appears multiple times we will record it multiple times.
03870    This doesn't hurt anything but it will slow things down.  */
03871 
03872 static void
03873 find_used_regs (xptr, data)
03874      rtx *xptr;
03875      void *data ATTRIBUTE_UNUSED;
03876 {
03877   int i, j;
03878   enum rtx_code code;
03879   const char *fmt;
03880   rtx x = *xptr;
03881 
03882   /* repeat is used to turn tail-recursion into iteration since GCC
03883      can't do it when there's no return value.  */
03884  repeat:
03885   if (x == 0)
03886     return;
03887 
03888   code = GET_CODE (x);
03889   if (REG_P (x))
03890     {
03891       if (reg_use_count == MAX_USES)
03892   return;
03893 
03894       reg_use_table[reg_use_count].reg_rtx = x;
03895       reg_use_count++;
03896     }
03897 
03898   /* Recursively scan the operands of this expression.  */
03899 
03900   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
03901     {
03902       if (fmt[i] == 'e')
03903   {
03904     /* If we are about to do the last recursive call
03905        needed at this level, change it into iteration.
03906        This function is called enough to be worth it.  */
03907     if (i == 0)
03908       {
03909         x = XEXP (x, 0);
03910         goto repeat;
03911       }
03912 
03913     find_used_regs (&XEXP (x, i), data);
03914   }
03915       else if (fmt[i] == 'E')
03916   for (j = 0; j < XVECLEN (x, i); j++)
03917     find_used_regs (&XVECEXP (x, i, j), data);
03918     }
03919 }
03920 
03921 /* Try to replace all non-SET_DEST occurrences of FROM in INSN with TO.
03922    Returns nonzero is successful.  */
03923 
03924 static int
03925 try_replace_reg (from, to, insn)
03926      rtx from, to, insn;
03927 {
03928   rtx note = find_reg_equal_equiv_note (insn);
03929   rtx src = 0;
03930   int success = 0;
03931   rtx set = single_set (insn);
03932 
03933   validate_replace_src_group (from, to, insn);
03934   if (num_changes_pending () && apply_change_group ())
03935     success = 1;
03936 
03937   /* Try to simplify SET_SRC if we have substituted a constant.  */
03938   if (success && set && CONSTANT_P (to))
03939     {
03940       src = simplify_rtx (SET_SRC (set));
03941 
03942       if (src)
03943   validate_change (insn, &SET_SRC (set), src, 0);
03944     }
03945 
03946   if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
03947     {
03948       /* If above failed and this is a single set, try to simplify the source of
03949    the set given our substitution.  We could perhaps try this for multiple
03950    SETs, but it probably won't buy us anything.  */
03951       src = simplify_replace_rtx (SET_SRC (set), from, to);
03952 
03953       if (!rtx_equal_p (src, SET_SRC (set))
03954     && validate_change (insn, &SET_SRC (set), src, 0))
03955   success = 1;
03956 
03957       /* If we've failed to do replacement, have a single SET, don't already
03958    have a note, and have no special SET, add a REG_EQUAL note to not
03959    lose information.  */
03960       if (!success && note == 0 && set != 0
03961     && GET_CODE (XEXP (set, 0)) != ZERO_EXTRACT
03962     && GET_CODE (XEXP (set, 0)) != SIGN_EXTRACT)
03963   note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
03964     }
03965 
03966   /* If there is already a NOTE, update the expression in it with our
03967      replacement.  */
03968   else if (note != 0)
03969     XEXP (note, 0) = simplify_replace_rtx (XEXP (note, 0), from, to);
03970 
03971   /* REG_EQUAL may get simplified into register.
03972      We don't allow that. Remove that note. This code ought
03973      not to hapen, because previous code ought to syntetize
03974      reg-reg move, but be on the safe side.  */
03975   if (note && REG_P (XEXP (note, 0)))
03976     remove_note (insn, note);
03977 
03978   return success;
03979 }
03980 
03981 /* Find a set of REGNOs that are available on entry to INSN's block.  Returns
03982    NULL no such set is found.  */
03983 
03984 static struct expr *
03985 find_avail_set (regno, insn)
03986      int regno;
03987      rtx insn;
03988 {
03989   /* SET1 contains the last set found that can be returned to the caller for
03990      use in a substitution.  */
03991   struct expr *set1 = 0;
03992 
03993   /* Loops are not possible here.  To get a loop we would need two sets
03994      available at the start of the block containing INSN.  ie we would
03995      need two sets like this available at the start of the block:
03996 
03997        (set (reg X) (reg Y))
03998        (set (reg Y) (reg X))
03999 
04000      This can not happen since the set of (reg Y) would have killed the
04001      set of (reg X) making it unavailable at the start of this block.  */
04002   while (1)
04003     {
04004       rtx src;
04005       struct expr *set = lookup_set (regno, NULL_RTX, &set_hash_table);
04006 
04007       /* Find a set that is available at the start of the block
04008    which contains INSN.  */
04009       while (set)
04010   {
04011     if (TEST_BIT (cprop_avin[BLOCK_NUM (insn)], set->bitmap_index))
04012       break;
04013     set = next_set (regno, set);
04014   }
04015 
04016       /* If no available set was found we've reached the end of the
04017    (possibly empty) copy chain.  */
04018       if (set == 0)
04019   break;
04020 
04021       if (GET_CODE (set->expr) != SET)
04022   abort ();
04023 
04024       src = SET_SRC (set->expr);
04025 
04026       /* We know the set is available.
04027    Now check that SRC is ANTLOC (i.e. none of the source operands
04028    have changed since the start of the block).
04029 
04030          If the source operand changed, we may still use it for the next
04031          iteration of this loop, but we may not use it for substitutions.  */
04032 
04033       if (CONSTANT_P (src) || oprs_not_set_p (src, insn))
04034   set1 = set;
04035 
04036       /* If the source of the set is anything except a register, then
04037    we have reached the end of the copy chain.  */
04038       if (GET_CODE (src) != REG)
04039   break;
04040 
04041       /* Follow the copy chain, ie start another iteration of the loop
04042    and see if we have an available copy into SRC.  */
04043       regno = REGNO (src);
04044     }
04045 
04046   /* SET1 holds the last set that was available and anticipatable at
04047      INSN.  */
04048   return set1;
04049 }
04050 
04051 /* Subroutine of cprop_insn that tries to propagate constants into
04052    JUMP_INSNS.  JUMP must be a conditional jump.  If SETCC is non-NULL
04053    it is the instruction that immediately preceeds JUMP, and must be a
04054    single SET of a register.  FROM is what we will try to replace,
04055    SRC is the constant we will try to substitute for it.  Returns nonzero
04056    if a change was made.  */
04057 
04058 static int
04059 cprop_jump (bb, setcc, jump, from, src)
04060      basic_block bb;
04061      rtx setcc;
04062      rtx jump;
04063      rtx from;
04064      rtx src;
04065 {
04066   rtx new, new_set;
04067   rtx set = pc_set (jump);
04068 
04069   /* First substitute in the INSN condition as the SET_SRC of the JUMP,
04070      then substitute that given values in this expanded JUMP.  */
04071   if (setcc != NULL
04072       && !modified_between_p (from, setcc, jump)
04073       && !modified_between_p (src, setcc, jump))
04074     {
04075       rtx setcc_set = single_set (setcc);
04076       new_set = simplify_replace_rtx (SET_SRC (set),
04077               SET_DEST (setcc_set),
04078               SET_SRC (setcc_set));
04079     }
04080   else
04081     new_set = set;
04082 
04083   new = simplify_replace_rtx (new_set, from, src);
04084 
04085   /* If no simplification can be made, then try the next
04086      register.  */
04087   if (rtx_equal_p (new, new_set) || rtx_equal_p (new, SET_SRC (set)))
04088     return 0;
04089 
04090   /* If this is now a no-op delete it, otherwise this must be a valid insn.  */
04091   if (new == pc_rtx)
04092     delete_insn (jump);
04093   else
04094     {
04095       /* Ensure the value computed inside the jump insn to be equivalent
04096          to one computed by setcc.  */
04097       if (setcc 
04098     && modified_in_p (new, setcc))
04099   return 0;
04100       if (! validate_change (jump, &SET_SRC (set), new, 0))
04101   return 0;
04102 
04103       /* If this has turned into an unconditional jump,
04104    then put a barrier after it so that the unreachable
04105    code will be deleted.  */
04106       if (GET_CODE (SET_SRC (set)) == LABEL_REF)
04107   emit_barrier_after (jump);
04108      }
04109 
04110 #ifdef HAVE_cc0
04111   /* Delete the cc0 setter.  */
04112   if (setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
04113     delete_insn (setcc);
04114 #endif
04115 
04116   run_jump_opt_after_gcse = 1;
04117 
04118   const_prop_count++;
04119   if (gcse_file != NULL)
04120     {
04121       fprintf (gcse_file,
04122          "CONST-PROP: Replacing reg %d in jump_insn %d with constant ",
04123          REGNO (from), INSN_UID (jump));
04124       print_rtl (gcse_file, src);
04125       fprintf (gcse_file, "\n");
04126     }
04127   purge_dead_edges (bb);
04128 
04129   return 1;
04130 }
04131 
04132 static bool
04133 constprop_register (insn, from, to, alter_jumps)
04134      rtx insn;
04135      rtx from;
04136      rtx to;
04137      int alter_jumps;
04138 {
04139   rtx sset;
04140 
04141   /* Check for reg or cc0 setting instructions followed by
04142      conditional branch instructions first.  */
04143   if (alter_jumps
04144       && (sset = single_set (insn)) != NULL
04145       && NEXT_INSN (insn)
04146       && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
04147     {
04148       rtx dest = SET_DEST (sset);
04149       if ((REG_P (dest) || CC0_P (dest))
04150     && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn), from, to))
04151   return 1;
04152     }
04153 
04154   /* Handle normal insns next.  */
04155   if (GET_CODE (insn) == INSN
04156       && try_replace_reg (from, to, insn))
04157     return 1;
04158 
04159   /* Try to propagate a CONST_INT into a conditional jump.
04160      We're pretty specific about what we will handle in this
04161      code, we can extend this as necessary over time.
04162 
04163      Right now the insn in question must look like
04164      (set (pc) (if_then_else ...))  */
04165   else if (alter_jumps && any_condjump_p (insn) && onlyjump_p (insn))
04166     return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, to);
04167   return 0;
04168 }
04169 
04170 /* Perform constant and copy propagation on INSN.
04171    The result is nonzero if a change was made.  */
04172 
04173 static int
04174 cprop_insn (insn, alter_jumps)
04175      rtx insn;
04176      int alter_jumps;
04177 {
04178   struct reg_use *reg_used;
04179   int changed = 0;
04180   rtx note;
04181 
04182   if (!INSN_P (insn))
04183     return 0;
04184 
04185   reg_use_count = 0;
04186   note_uses (&PATTERN (insn), find_used_regs, NULL);
04187 
04188   note = find_reg_equal_equiv_note (insn);
04189 
04190   /* We may win even when propagating constants into notes.  */
04191   if (note)
04192     find_used_regs (&XEXP (note, 0), NULL);
04193 
04194   for (reg_used = &reg_use_table[0]; reg_use_count > 0;
04195        reg_used++, reg_use_count--)
04196     {
04197       unsigned int regno = REGNO (reg_used->reg_rtx);
04198       rtx pat, src;
04199       struct expr *set;
04200 
04201       /* Ignore registers created by GCSE.
04202    We do this because ...  */
04203       if (regno >= max_gcse_regno)
04204   continue;
04205 
04206       /* If the register has already been set in this block, there's
04207    nothing we can do.  */
04208       if (! oprs_not_set_p (reg_used->reg_rtx, insn))
04209   continue;
04210 
04211       /* Find an assignment that sets reg_used and is available
04212    at the start of the block.  */
04213       set = find_avail_set (regno, insn);
04214       if (! set)
04215   continue;
04216 
04217       pat = set->expr;
04218       /* ??? We might be able to handle PARALLELs.  Later.  */
04219       if (GET_CODE (pat) != SET)
04220   abort ();
04221 
04222       src = SET_SRC (pat);
04223 
04224       /* Constant propagation.  */
04225       if (CONSTANT_P (src))
04226   {
04227           if (constprop_register (insn, reg_used->reg_rtx, src, alter_jumps))
04228       {
04229         changed = 1;
04230         const_prop_count++;
04231         if (gcse_file != NULL)
04232     {
04233       fprintf (gcse_file, "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
04234       fprintf (gcse_file, "insn %d with constant ", INSN_UID (insn));
04235       print_rtl (gcse_file, src);
04236       fprintf (gcse_file, "\n");
04237     }
04238       }
04239   }
04240       else if (GET_CODE (src) == REG
04241          && REGNO (src) >= FIRST_PSEUDO_REGISTER
04242          && REGNO (src) != regno)
04243   {
04244     if (try_replace_reg (reg_used->reg_rtx, src, insn))
04245       {
04246         changed = 1;
04247         copy_prop_count++;
04248         if (gcse_file != NULL)
04249     {
04250       fprintf (gcse_file, "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
04251          regno, INSN_UID (insn));
04252       fprintf (gcse_file, " with reg %d\n", REGNO (src));
04253     }
04254 
04255         /* The original insn setting reg_used may or may not now be
04256      deletable.  We leave the deletion to flow.  */
04257         /* FIXME: If it turns out that the insn isn't deletable,
04258      then we may have unnecessarily extended register lifetimes
04259      and made things worse.  */
04260       }
04261   }
04262     }
04263 
04264   return changed;
04265 }
04266 
04267 /* Like find_used_regs, but avoid recording uses that appear in
04268    input-output contexts such as zero_extract or pre_dec.  This
04269    restricts the cases we consider to those for which local cprop
04270    can legitimately make replacements.  */
04271 
04272 static void
04273 local_cprop_find_used_regs (xptr, data)
04274      rtx *xptr;
04275      void *data;
04276 {
04277   rtx x = *xptr;
04278 
04279   if (x == 0)
04280     return;
04281 
04282   switch (GET_CODE (x))
04283     {
04284     case ZERO_EXTRACT:
04285     case SIGN_EXTRACT:
04286     case STRICT_LOW_PART:
04287       return;
04288 
04289     case PRE_DEC:
04290     case PRE_INC:
04291     case POST_DEC:
04292     case POST_INC:
04293     case PRE_MODIFY:
04294     case POST_MODIFY:
04295       /* Can only legitimately appear this early in the context of
04296    stack pushes for function arguments, but handle all of the
04297    codes nonetheless.  */
04298       return;
04299 
04300     case SUBREG:
04301       /* Setting a subreg of a register larger than word_mode leaves
04302    the non-written words unchanged.  */
04303       if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) > BITS_PER_WORD)
04304   return;
04305       break;
04306 
04307     default:
04308       break;
04309     }
04310 
04311   find_used_regs (xptr, data);
04312 }
04313   
04314 /* LIBCALL_SP is a zero-terminated array of insns at the end of a libcall;
04315    their REG_EQUAL notes need updating.  */
04316 
04317 static bool
04318 do_local_cprop (x, insn, alter_jumps, libcall_sp)
04319      rtx x;
04320      rtx insn;
04321      int alter_jumps;
04322      rtx *libcall_sp;
04323 {
04324   rtx newreg = NULL, newcnst = NULL;
04325 
04326   /* Rule out USE instructions and ASM statements as we don't want to
04327      change the hard registers mentioned.  */
04328   if (GET_CODE (x) == REG
04329       && (REGNO (x) >= FIRST_PSEUDO_REGISTER
04330           || (GET_CODE (PATTERN (insn)) != USE
04331         && asm_noperands (PATTERN (insn)) < 0)))
04332     {
04333       cselib_val *val = cselib_lookup (x, GET_MODE (x), 0);
04334       struct elt_loc_list *l;
04335 
04336       if (!val)
04337   return false;
04338       for (l = val->locs; l; l = l->next)
04339   {
04340     rtx this_rtx = l->loc;
04341     rtx note;
04342 
04343     if (l->in_libcall)
04344       continue;
04345 
04346     if (CONSTANT_P (this_rtx))
04347       newcnst = this_rtx;
04348     if (REG_P (this_rtx) && REGNO (this_rtx) >= FIRST_PSEUDO_REGISTER
04349         /* Don't copy propagate if it has attached REG_EQUIV note.
04350      At this point this only function parameters should have
04351      REG_EQUIV notes and if the argument slot is used somewhere
04352      explicitly, it means address of parameter has been taken,
04353      so we should not extend the lifetime of the pseudo.  */
04354         && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
04355       || GET_CODE (XEXP (note, 0)) != MEM))
04356       newreg = this_rtx;
04357   }
04358       if (newcnst && constprop_register (insn, x, newcnst, alter_jumps))
04359   {
04360     /* If we find a case where we can't fix the retval REG_EQUAL notes
04361        match the new register, we either have to abandom this replacement
04362        or fix delete_trivially_dead_insns to preserve the setting insn,
04363        or make it delete the REG_EUAQL note, and fix up all passes that
04364        require the REG_EQUAL note there.  */
04365     if (!adjust_libcall_notes (x, newcnst, insn, libcall_sp))
04366       abort ();
04367     if (gcse_file != NULL)
04368       {
04369         fprintf (gcse_file, "LOCAL CONST-PROP: Replacing reg %d in ",
04370            REGNO (x));
04371         fprintf (gcse_file, "insn %d with constant ",
04372            INSN_UID (insn));
04373         print_rtl (gcse_file, newcnst);
04374         fprintf (gcse_file, "\n");
04375       }
04376     const_prop_count++;
04377     return true;
04378   }
04379       else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
04380   {
04381     adjust_libcall_notes (x, newreg, insn, libcall_sp);
04382     if (gcse_file != NULL)
04383       {
04384         fprintf (gcse_file,
04385            "LOCAL COPY-PROP: Replacing reg %d in insn %d",
04386            REGNO (x), INSN_UID (insn));
04387         fprintf (gcse_file, " with reg %d\n", REGNO (newreg));
04388       }
04389     copy_prop_count++;
04390     return true;
04391   }
04392     }
04393   return false;
04394 }
04395 
04396 /* LIBCALL_SP is a zero-terminated array of insns at the end of a libcall;
04397    their REG_EQUAL notes need updating to reflect that OLDREG has been
04398    replaced with NEWVAL in INSN.  Return true if all substitutions could
04399    be made.  */
04400 static bool
04401 adjust_libcall_notes (oldreg, newval, insn, libcall_sp)
04402      rtx oldreg, newval, insn, *libcall_sp;
04403 {
04404   rtx end;
04405 
04406   while ((end = *libcall_sp++))
04407     {
04408       rtx note = find_reg_equal_equiv_note (end);
04409 
04410       if (! note)
04411   continue;
04412 
04413       if (REG_P (newval))
04414   {
04415     if (reg_set_between_p (newval, PREV_INSN (insn), end))
04416       {
04417         do
04418     {
04419       note = find_reg_equal_equiv_note (end);
04420       if (! note)
04421         continue;
04422       if (reg_mentioned_p (newval, XEXP (note, 0)))
04423         return false;
04424     }
04425         while ((end = *libcall_sp++));
04426         return true;
04427       }
04428   }
04429       XEXP (note, 0) = replace_rtx (XEXP (note, 0), oldreg, newval);
04430       insn = end;
04431     }
04432   return true;
04433 }
04434 
04435 #define MAX_NESTED_LIBCALLS 9
04436 
04437 static void
04438 local_cprop_pass (alter_jumps)
04439      int alter_jumps;
04440 {
04441   rtx insn;
04442   struct reg_use *reg_used;
04443   rtx libcall_stack[MAX_NESTED_LIBCALLS + 1], *libcall_sp;
04444   bool changed = false;
04445 
04446   cselib_init ();
04447   libcall_sp = &libcall_stack[MAX_NESTED_LIBCALLS];
04448   *libcall_sp = 0;
04449   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
04450     {
04451       if (INSN_P (insn))
04452   {
04453     rtx note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
04454 
04455     if (note)
04456       {
04457         if (libcall_sp == libcall_stack)
04458     abort ();
04459         *--libcall_sp = XEXP (note, 0);
04460       }
04461     note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
04462     if (note)
04463       libcall_sp++;
04464     note = find_reg_equal_equiv_note (insn);
04465     do
04466       {
04467         reg_use_count = 0;
04468         note_uses (&PATTERN (insn), local_cprop_find_used_regs, NULL);
04469         if (note)
04470     local_cprop_find_used_regs (&XEXP (note, 0), NULL);
04471 
04472         for (reg_used = &reg_use_table[0]; reg_use_count > 0;
04473        reg_used++, reg_use_count--)
04474     if (do_local_cprop (reg_used->reg_rtx, insn, alter_jumps,
04475         libcall_sp))
04476       {
04477         changed = true;
04478         break;
04479       }
04480       }
04481     while (reg_use_count);
04482   }
04483       cselib_process_insn (insn);
04484     }
04485   cselib_finish ();
04486   /* Global analysis may get into infinite loops for unreachable blocks.  */
04487   if (changed && alter_jumps)
04488     {
04489       delete_unreachable_blocks ();
04490       free_reg_set_mem ();
04491       alloc_reg_set_mem (max_reg_num ());
04492       compute_sets (get_insns ());
04493     }
04494 }
04495 
04496 /* Forward propagate copies.  This includes copies and constants.  Return
04497    nonzero if a change was made.  */
04498 
04499 static int
04500 cprop (alter_jumps)
04501      int alter_jumps;
04502 {
04503   int changed;
04504   basic_block bb;
04505   rtx insn;
04506 
04507   /* Note we start at block 1.  */
04508   if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
04509     {
04510       if (gcse_file != NULL)
04511   fprintf (gcse_file, "\n");
04512       return 0;
04513     }
04514 
04515   changed = 0;
04516   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb, EXIT_BLOCK_PTR, next_bb)
04517     {
04518       /* Reset tables used to keep track of what's still valid [since the
04519    start of the block].  */
04520       reset_opr_set_tables ();
04521 
04522       for (insn = bb->head;
04523      insn != NULL && insn != NEXT_INSN (bb->end);
04524      insn = NEXT_INSN (insn))
04525   if (INSN_P (insn))
04526     {
04527       changed |= cprop_insn (insn, alter_jumps);
04528 
04529       /* Keep track of everything modified by this insn.  */
04530       /* ??? Need to be careful w.r.t. mods done to INSN.  Don't
04531          call mark_oprs_set if we turned the insn into a NOTE.  */
04532       if (GET_CODE (insn) != NOTE)
04533         mark_oprs_set (insn);
04534     }
04535     }
04536 
04537   if (gcse_file != NULL)
04538     fprintf (gcse_file, "\n");
04539 
04540   return changed;
04541 }
04542 
04543 /* Perform one copy/constant propagation pass.
04544    F is the first insn in the function.
04545    PASS is the pass count.  */
04546 
04547 static int
04548 one_cprop_pass (pass, alter_jumps)
04549      int pass;
04550      int alter_jumps;
04551 {
04552   int changed = 0;
04553 
04554   const_prop_count = 0;
04555   copy_prop_count = 0;
04556 
04557   local_cprop_pass (alter_jumps);
04558 
04559   alloc_hash_table (max_cuid, &set_hash_table, 1);
04560   compute_hash_table (&set_hash_table);
04561   if (gcse_file)
04562     dump_hash_table (gcse_file, "SET", &set_hash_table);
04563   if (set_hash_table.n_elems > 0)
04564     {
04565       alloc_cprop_mem (last_basic_block, set_hash_table.n_elems);
04566       compute_cprop_data ();
04567       changed = cprop (alter_jumps);
04568       if (alter_jumps)
04569   changed |= bypass_conditional_jumps ();
04570       free_cprop_mem ();
04571     }
04572 
04573   free_hash_table (&set_hash_table);
04574 
04575   if (gcse_file)
04576     {
04577       fprintf (gcse_file, "CPROP of %s, pass %d: %d bytes needed, ",
04578          current_function_name, pass, bytes_used);
04579       fprintf (gcse_file, "%d const props, %d copy props\n\n",
04580          const_prop_count, copy_prop_count);
04581     }
04582   /* Global analysis may get into infinite loops for unreachable blocks.  */
04583   if (changed && alter_jumps)
04584     delete_unreachable_blocks ();
04585 
04586   return changed;
04587 }
04588 
04589 /* Bypass conditional jumps.  */
04590 
04591 /* Find a set of REGNO to a constant that is available at the end of basic
04592    block BB.  Returns NULL if no such set is found.  Based heavily upon
04593    find_avail_set.  */
04594 
04595 static struct expr *
04596 find_bypass_set (regno, bb)
04597      int regno;
04598      int bb;
04599 {
04600   struct expr *result = 0;
04601 
04602   for (;;)
04603     {
04604       rtx src;
04605       struct expr *set = lookup_set (regno, NULL_RTX, &set_hash_table);
04606 
04607       while (set)
04608   {
04609     if (TEST_BIT (cprop_avout[bb], set->bitmap_index))
04610       break;
04611     set = next_set (regno, set);
04612   }
04613 
04614       if (set == 0)
04615   break;
04616 
04617       if (GET_CODE (set->expr) != SET)
04618   abort ();
04619 
04620       src = SET_SRC (set->expr);
04621       if (CONSTANT_P (src))
04622   result = set;
04623 
04624       if (GET_CODE (src) != REG)
04625   break;
04626 
04627       regno = REGNO (src);
04628     }
04629   return result;
04630 }
04631 
04632 
04633 /* Subroutine of bypass_block that checks whether a pseudo is killed by
04634    any of the instructions inserted on an edge.  Jump bypassing places
04635    condition code setters on CFG edges using insert_insn_on_edge.  This
04636    function is required to check that our data flow analysis is still
04637    valid prior to commit_edge_insertions.  */
04638 
04639 static bool
04640 reg_killed_on_edge (reg, e)
04641      rtx reg;
04642      edge e;
04643 {
04644   rtx insn;
04645 
04646   for (insn = e->insns; insn; insn = NEXT_INSN (insn))
04647     if (INSN_P (insn) && reg_set_p (reg, insn))
04648       return true;
04649 
04650   return false;
04651 }
04652 
04653 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
04654    basic block BB which has more than one predecessor.  If not NULL, SETCC
04655    is the first instruction of BB, which is immediately followed by JUMP_INSN
04656    JUMP.  Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
04657    Returns nonzero if a change was made.
04658 
04659    During the jump bypassing pass, we may place copies of SETCC instuctions
04660    on CFG edges.  The following routine must be careful to pay attention to
04661    these inserted insns when performing its transformations.  */
04662 
04663 static int
04664 bypass_block (bb, setcc, jump)
04665      basic_block bb;
04666      rtx setcc, jump;
04667 {
04668   rtx insn, note;
04669   edge e, enext, edest;
04670   int i, change;
04671 
04672   insn = (setcc != NULL) ? setcc : jump;
04673 
04674   /* Determine set of register uses in INSN.  */
04675   reg_use_count = 0;
04676   note_uses (&PATTERN (insn), find_used_regs, NULL);
04677   note = find_reg_equal_equiv_note (insn);
04678   if (note)
04679     find_used_regs (&XEXP (note, 0), NULL);
04680 
04681   change = 0;
04682   for (e = bb->pred; e; e = enext)
04683     {
04684       enext = e->pred_next;
04685       for (i = 0; i < reg_use_count; i++)
04686   {
04687     struct reg_use *reg_used = &reg_use_table[i];
04688     unsigned int regno = REGNO (reg_used->reg_rtx);
04689     basic_block dest, old_dest;
04690     struct expr *set;
04691     rtx src, new;
04692 
04693     if (regno >= max_gcse_regno)
04694       continue;
04695 
04696     set = find_bypass_set (regno, e->src->index);
04697 
04698     if (! set)
04699       continue;
04700 
04701     /* Check the data flow is valid after edge insertions.  */
04702     if (e->insns && reg_killed_on_edge (reg_used->reg_rtx, e))
04703       continue;
04704 
04705     src = SET_SRC (pc_set (jump));
04706 
04707     if (setcc != NULL)
04708         src = simplify_replace_rtx (src,
04709             SET_DEST (PATTERN (setcc)),
04710             SET_SRC (PATTERN (setcc)));
04711 
04712     new = simplify_replace_rtx (src, reg_used->reg_rtx,
04713               SET_SRC (set->expr));
04714 
04715     /* Jump bypassing may have already placed instructions on 
04716        edges of the CFG.  We can't bypass an outgoing edge that
04717        has instructions associated with it, as these insns won't
04718        get executed if the incoming edge is redirected.  */
04719 
04720     if (new == pc_rtx)
04721       {
04722         edest = FALLTHRU_EDGE (bb);
04723         dest = edest->insns ? NULL : edest->dest;
04724       }
04725     else if (GET_CODE (new) == LABEL_REF)
04726       {
04727         dest = BLOCK_FOR_INSN (XEXP (new, 0));
04728         /* Don't bypass edges containing instructions.  */
04729         for (edest = bb->succ; edest; edest = edest->succ_next)
04730     if (edest->dest == dest && edest->insns)
04731       {
04732         dest = NULL;
04733         break;
04734       }
04735       }
04736     else
04737       dest = NULL;
04738 
04739     /* Once basic block indices are stable, we should be able
04740        to use redirect_edge_and_branch_force instead.  */
04741     old_dest = e->dest;
04742     if (dest != NULL && dest != old_dest
04743         && redirect_edge_and_branch (e, dest))
04744       {
04745         /* Copy the register setter to the redirected edge.
04746      Don't copy CC0 setters, as CC0 is dead after jump.  */
04747         if (setcc)
04748     {
04749       rtx pat = PATTERN (setcc);
04750       if (!CC0_P (SET_DEST (pat)))
04751         insert_insn_on_edge (copy_insn (pat), e);
04752     }
04753 
04754         if (gcse_file != NULL)
04755     {
04756       fprintf (gcse_file, "JUMP-BYPASS: Proved reg %d in jump_insn %d equals constant ",
04757          regno, INSN_UID (jump));
04758       print_rtl (gcse_file, SET_SRC (set->expr));
04759       fprintf (gcse_file, "\nBypass edge from %d->%d to %d\n",
04760          e->src->index, old_dest->index, dest->index);
04761     }
04762         change = 1;
04763         break;
04764       }
04765   }
04766     }
04767   return change;
04768 }
04769 
04770 /* Find basic blocks with more than one predecessor that only contain a
04771    single conditional jump.  If the result of the comparison is known at
04772    compile-time from any incoming edge, redirect that edge to the
04773    appropriate target.  Returns nonzero if a change was made.  */
04774 
04775 static int
04776 bypass_conditional_jumps ()
04777 {
04778   basic_block bb;
04779   int changed;
04780   rtx setcc;
04781   rtx insn;
04782   rtx dest;
04783 
04784   /* Note we start at block 1.  */
04785   if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
04786     return 0;
04787 
04788   changed = 0;
04789   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb,
04790       EXIT_BLOCK_PTR, next_bb)
04791     {
04792       /* Check for more than one predecessor.  */
04793       if (bb->pred && bb->pred->pred_next)
04794   {
04795     setcc = NULL_RTX;
04796     for (insn = bb->head;
04797          insn != NULL && insn != NEXT_INSN (bb->end);
04798          insn = NEXT_INSN (insn))
04799       if (GET_CODE (insn) == INSN)
04800         {
04801     if (setcc)
04802       break;
04803     if (GET_CODE (PATTERN (insn)) != SET)
04804       break;
04805 
04806     dest = SET_DEST (PATTERN (insn));
04807     if (REG_P (dest) || CC0_P (dest))
04808       setcc = insn;
04809     else
04810       break;
04811         }
04812       else if (GET_CODE (insn) == JUMP_INSN)
04813         {
04814     if (any_condjump_p (insn) && onlyjump_p (insn))
04815       changed |= bypass_block (bb, setcc, insn);
04816     break;
04817         }
04818       else if (INSN_P (insn))
04819         break;
04820   }
04821     }
04822 
04823   /* If we bypassed any register setting insns, we inserted a
04824      copy on the redirected edge.  These need to be commited.  */
04825   if (changed)
04826     commit_edge_insertions();
04827 
04828   return changed;
04829 }
04830 
04831 /* Compute PRE+LCM working variables.  */
04832 
04833 /* Local properties of expressions.  */
04834 /* Nonzero for expressions that are transparent in the block.  */
04835 static sbitmap *transp;
04836 
04837 /* Nonzero for expressions that are transparent at the end of the block.
04838    This is only zero for expressions killed by abnormal critical edge
04839    created by a calls.  */
04840 static sbitmap *transpout;
04841 
04842 /* Nonzero for expressions that are computed (available) in the block.  */
04843 static sbitmap *comp;
04844 
04845 /* Nonzero for expressions that are locally anticipatable in the block.  */
04846 static sbitmap *antloc;
04847 
04848 /* Nonzero for expressions where this block is an optimal computation
04849    point.  */
04850 static sbitmap *pre_optimal;
04851 
04852 /* Nonzero for expressions which are redundant in a particular block.  */
04853 static sbitmap *pre_redundant;
04854 
04855 /* Nonzero for expressions which should be inserted on a specific edge.  */
04856 static sbitmap *pre_insert_map;
04857 
04858 /* Nonzero for expressions which should be deleted in a specific block.  */
04859 static sbitmap *pre_delete_map;
04860 
04861 /* Contains the edge_list returned by pre_edge_lcm.  */
04862 static struct edge_list *edge_list;
04863 
04864 /* Redundant insns.  */
04865 static sbitmap pre_redundant_insns;
04866 
04867 /* Allocate vars used for PRE analysis.  */
04868 
04869 static void
04870 alloc_pre_mem (n_blocks, n_exprs)
04871      int n_blocks, n_exprs;
04872 {
04873   transp = sbitmap_vector_alloc (n_blocks, n_exprs);
04874   comp = sbitmap_vector_alloc (n_blocks, n_exprs);
04875   antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
04876 
04877   pre_optimal = NULL;
04878   pre_redundant = NULL;
04879   pre_insert_map = NULL;
04880   pre_delete_map = NULL;
04881   ae_in = NULL;
04882   ae_out = NULL;
04883   ae_kill = sbitmap_vector_alloc (n_blocks, n_exprs);
04884 
04885   /* pre_insert and pre_delete are allocated later.  */
04886 }
04887 
04888 /* Free vars used for PRE analysis.  */
04889 
04890 static void
04891 free_pre_mem ()
04892 {
04893   sbitmap_vector_free (transp);
04894   sbitmap_vector_free (comp);
04895 
04896   /* ANTLOC and AE_KILL are freed just after pre_lcm finishes.  */
04897 
04898   if (pre_optimal)
04899     sbitmap_vector_free (pre_optimal);
04900   if (pre_redundant)
04901     sbitmap_vector_free (pre_redundant);
04902   if (pre_insert_map)
04903     sbitmap_vector_free (pre_insert_map);
04904   if (pre_delete_map)
04905     sbitmap_vector_free (pre_delete_map);
04906   if (ae_in)
04907     sbitmap_vector_free (ae_in);
04908   if (ae_out)
04909     sbitmap_vector_free (ae_out);
04910 
04911   transp = comp = NULL;
04912   pre_optimal = pre_redundant = pre_insert_map = pre_delete_map = NULL;
04913   ae_in = ae_out = NULL;
04914 }
04915 
04916 /* Top level routine to do the dataflow analysis needed by PRE.  */
04917 
04918 static void
04919 compute_pre_data ()
04920 {
04921   sbitmap trapping_expr;
04922   basic_block bb;
04923   unsigned int ui;
04924 
04925   compute_local_properties (transp, comp, antloc, &expr_hash_table);
04926   sbitmap_vector_zero (ae_kill, last_basic_block);
04927 
04928   /* Collect expressions which might trap.  */
04929   trapping_expr = sbitmap_alloc (expr_hash_table.n_elems);
04930   sbitmap_zero (trapping_expr);
04931   for (ui = 0; ui < expr_hash_table.size; ui++)
04932     {
04933       struct expr *e;
04934       for (e = expr_hash_table.table[ui]; e != NULL; e = e->next_same_hash)
04935   if (may_trap_p (e->expr))
04936     SET_BIT (trapping_expr, e->bitmap_index);
04937     }
04938 
04939   /* Compute ae_kill for each basic block using:
04940 
04941      ~(TRANSP | COMP)
04942 
04943      This is significantly faster than compute_ae_kill.  */
04944 
04945   FOR_EACH_BB (bb)
04946     {
04947       edge e;
04948 
04949       /* If the current block is the destination of an abnormal edge, we
04950    kill all trapping expressions because we won't be able to properly
04951    place the instruction on the edge.  So make them neither
04952    anticipatable nor transparent.  This is fairly conservative.  */
04953       for (e = bb->pred; e ; e = e->pred_next)
04954   if (e->flags & EDGE_ABNORMAL)
04955     {
04956       sbitmap_difference (antloc[bb->index], antloc[bb->index], trapping_expr);
04957       sbitmap_difference (transp[bb->index], transp[bb->index], trapping_expr);
04958       break;
04959     }
04960 
04961       sbitmap_a_or_b (ae_kill[bb->index], transp[bb->index], comp[bb->index]);
04962       sbitmap_not (ae_kill[bb->index], ae_kill[bb->index]);
04963     }
04964 
04965   edge_list = pre_edge_lcm (gcse_file, expr_hash_table.n_elems, transp, comp, antloc,
04966           ae_kill, &pre_insert_map, &pre_delete_map);
04967   sbitmap_vector_free (antloc);
04968   antloc = NULL;
04969   sbitmap_vector_free (ae_kill);
04970   ae_kill = NULL;
04971   sbitmap_free (trapping_expr);
04972 }
04973 
04974 /* PRE utilities */
04975 
04976 /* Return nonzero if an occurrence of expression EXPR in OCCR_BB would reach
04977    block BB.
04978 
04979    VISITED is a pointer to a working buffer for tracking which BB's have
04980    been visited.  It is NULL for the top-level call.
04981 
04982    We treat reaching expressions that go through blocks containing the same
04983    reaching expression as "not reaching".  E.g. if EXPR is generated in blocks
04984    2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
04985    2 as not reaching.  The intent is to improve the probability of finding
04986    only one reaching expression and to reduce register lifetimes by picking
04987    the closest such expression.  */
04988 
04989 static int
04990 pre_expr_reaches_here_p_work (occr_bb, expr, bb, visited)
04991      basic_block occr_bb;
04992      struct expr *expr;
04993      basic_block bb;
04994      char *visited;
04995 {
04996   edge pred;
04997 
04998   for (pred = bb->pred; pred != NULL; pred = pred->pred_next)
04999     {
05000       basic_block pred_bb = pred->src;
05001 
05002       if (pred->src == ENTRY_BLOCK_PTR
05003     /* Has predecessor has already been visited?  */
05004     || visited[pred_bb->index])
05005   ;/* Nothing to do.  */
05006 
05007       /* Does this predecessor generate this expression?  */
05008       else if (TEST_BIT (comp[pred_bb->index], expr->bitmap_index))
05009   {
05010     /* Is this the occurrence we're looking for?
05011        Note that there's only one generating occurrence per block
05012        so we just need to check the block number.  */
05013     if (occr_bb == pred_bb)
05014       return 1;
05015 
05016     visited[pred_bb->index] = 1;
05017   }
05018       /* Ignore this predecessor if it kills the expression.  */
05019       else if (! TEST_BIT (transp[pred_bb->index], expr->bitmap_index))
05020   visited[pred_bb->index] = 1;
05021 
05022       /* Neither gen nor kill.  */
05023       else
05024   {
05025     visited[pred_bb->index] = 1;
05026     if (pre_expr_reaches_here_p_work (occr_bb, expr, pred_bb, visited))
05027       return 1;
05028   }
05029     }
05030 
05031   /* All paths have been checked.  */
05032   return 0;
05033 }
05034 
05035 /* The wrapper for pre_expr_reaches_here_work that ensures that any
05036    memory allocated for that function is returned.  */
05037 
05038 static int
05039 pre_expr_reaches_here_p (occr_bb, expr, bb)
05040      basic_block occr_bb;
05041      struct expr *expr;
05042      basic_block bb;
05043 {
05044   int rval;
05045   char *visited = (char *) xcalloc (last_basic_block, 1);
05046 
05047   rval = pre_expr_reaches_here_p_work (occr_bb, expr, bb, visited);
05048 
05049   free (visited);
05050   return rval;
05051 }
05052 
05053 
05054 /* Given an expr, generate RTL which we can insert at the end of a BB,
05055    or on an edge.  Set the block number of any insns generated to
05056    the value of BB.  */
05057 
05058 static rtx
05059 process_insert_insn (expr)
05060      struct expr *expr;
05061 {
05062   rtx reg = expr->reaching_reg;
05063   rtx exp = copy_rtx (expr->expr);
05064   rtx pat;
05065 
05066   start_sequence ();
05067 
05068   /* If the expression is something that's an operand, like a constant,
05069      just copy it to a register.  */
05070   if (general_operand (exp, GET_MODE (reg)))
05071     emit_move_insn (reg, exp);
05072 
05073   /* Otherwise, make a new insn to compute this expression and make sure the
05074      insn will be recognized (this also adds any needed CLOBBERs).  Copy the
05075      expression to make sure we don't have any sharing issues.  */
05076   else if (insn_invalid_p (emit_insn (gen_rtx_SET (VOIDmode, reg, exp))))
05077     abort ();
05078 
05079   pat = get_insns ();
05080   end_sequence ();
05081 
05082   return pat;
05083 }
05084 
05085 /* Add EXPR to the end of basic block BB.
05086 
05087    This is used by both the PRE and code hoisting.
05088 
05089    For PRE, we want to verify that the expr is either transparent
05090    or locally anticipatable in the target block.  This check makes
05091    no sense for code hoisting.  */
05092 
05093 static void
05094 insert_insn_end_bb (expr, bb, pre)
05095      struct expr *expr;
05096      basic_block bb;
05097      int pre;
05098 {
05099   rtx insn = bb->end;
05100   rtx new_insn;
05101   rtx reg = expr->reaching_reg;
05102   int regno = REGNO (reg);
05103   rtx pat, pat_end;
05104 
05105   pat = process_insert_insn (expr);
05106   if (pat == NULL_RTX || ! INSN_P (pat))
05107     abort ();
05108 
05109   pat_end = pat;
05110   while (NEXT_INSN (pat_end) != NULL_RTX)
05111     pat_end = NEXT_INSN (pat_end);
05112 
05113   /* If the last insn is a jump, insert EXPR in front [taking care to
05114      handle cc0, etc. properly].  Similary we need to care trapping
05115      instructions in presence of non-call exceptions.  */
05116 
05117   if (GET_CODE (insn) == JUMP_INSN
05118       || (GET_CODE (insn) == INSN
05119     && (bb->succ->succ_next || (bb->succ->flags & EDGE_ABNORMAL))))
05120     {
05121 #ifdef HAVE_cc0
05122       rtx note;
05123 #endif
05124       /* It should always be the case that we can put these instructions
05125    anywhere in the basic block with performing PRE optimizations.
05126    Check this.  */
05127       if (GET_CODE (insn) == INSN && pre
05128     && !TEST_BIT (antloc[bb->index], expr->bitmap_index)
05129     && !TEST_BIT (transp[bb->index], expr->bitmap_index))
05130   abort ();
05131 
05132       /* If this is a jump table, then we can't insert stuff here.  Since
05133    we know the previous real insn must be the tablejump, we insert
05134    the new instruction just before the tablejump.  */
05135       if (GET_CODE (PATTERN (insn)) == ADDR_VEC
05136     || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
05137   insn = prev_real_insn (insn);
05138 
05139 #ifdef HAVE_cc0
05140       /* FIXME: 'twould be nice to call prev_cc0_setter here but it aborts
05141    if cc0 isn't set.  */
05142       note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
05143       if (note)
05144   insn = XEXP (note, 0);
05145       else
05146   {
05147     rtx maybe_cc0_setter = prev_nonnote_insn (insn);
05148     if (maybe_cc0_setter
05149         && INSN_P (maybe_cc0_setter)
05150         && sets_cc0_p (PATTERN (maybe_cc0_setter)))
05151       insn = maybe_cc0_setter;
05152   }
05153 #endif
05154       /* FIXME: What if something in cc0/jump uses value set in new insn?  */
05155       new_insn = emit_insn_before (pat, insn);
05156     }
05157 
05158   /* Likewise if the last insn is a call, as will happen in the presence
05159      of exception handling.  */
05160   else if (GET_CODE (insn) == CALL_INSN
05161      && (bb->succ->succ_next || (bb->succ->flags & EDGE_ABNORMAL)))
05162     {
05163       /* Keeping in mind SMALL_REGISTER_CLASSES and parameters in registers,
05164    we search backward and place the instructions before the first
05165    parameter is loaded.  Do this for everyone for consistency and a
05166    presumtion that we'll get better code elsewhere as well.
05167 
05168    It should always be the case that we can put these instructions
05169    anywhere in the basic block with performing PRE optimizations.
05170    Check this.  */
05171 
05172       if (pre
05173     && !TEST_BIT (antloc[bb->index], expr->bitmap_index)
05174     && !TEST_BIT (transp[bb->index], expr->bitmap_index))
05175   abort ();
05176 
05177       /* Since different machines initialize their parameter registers
05178    in different orders, assume nothing.  Collect the set of all
05179    parameter registers.  */
05180       insn = find_first_parameter_load (insn, bb->head);
05181 
05182       /* If we found all the parameter loads, then we want to insert
05183    before the first parameter load.
05184 
05185    If we did not find all the parameter loads, then we might have
05186    stopped on the head of the block, which could be a CODE_LABEL.
05187    If we inserted before the CODE_LABEL, then we would be putting
05188    the insn in the wrong basic block.  In that case, put the insn
05189    after the CODE_LABEL.  Also, respect NOTE_INSN_BASIC_BLOCK.  */
05190       while (GET_CODE (insn) == CODE_LABEL
05191        || NOTE_INSN_BASIC_BLOCK_P (insn))
05192   insn = NEXT_INSN (insn);
05193 
05194       new_insn = emit_insn_before (pat, insn);
05195     }
05196   else
05197     new_insn = emit_insn_after (pat, insn);
05198 
05199   while (1)
05200     {
05201       if (INSN_P (pat))
05202   {
05203     add_label_notes (PATTERN (pat), new_insn);
05204     note_stores (PATTERN (pat), record_set_info, pat);
05205   }
05206       if (pat == pat_end)
05207   break;
05208       pat = NEXT_INSN (pat);
05209     }
05210 
05211   gcse_create_count++;
05212 
05213   if (gcse_file)
05214     {
05215       fprintf (gcse_file, "PRE/HOIST: end of bb %d, insn %d, ",
05216          bb->index, INSN_UID (new_insn));
05217       fprintf (gcse_file, "copying expression %d to reg %d\n",
05218          expr->bitmap_index, regno);
05219     }
05220 }
05221 
05222 /* Insert partially redundant expressions on edges in the CFG to make
05223    the expressions fully redundant.  */
05224 
05225 static int
05226 pre_edge_insert (edge_list, index_map)
05227      struct edge_list *edge_list;
05228      struct expr **index_map;
05229 {
05230   int e, i, j, num_edges, set_size, did_insert = 0;
05231   sbitmap *inserted;
05232 
05233   /* Where PRE_INSERT_MAP is nonzero, we add the expression on that edge
05234      if it reaches any of the deleted expressions.  */
05235 
05236   set_size = pre_insert_map[0]->size;
05237   num_edges = NUM_EDGES (edge_list);
05238   inserted = sbitmap_vector_alloc (num_edges, expr_hash_table.n_elems);
05239   sbitmap_vector_zero (inserted, num_edges);
05240 
05241   for (e = 0; e < num_edges; e++)
05242     {
05243       int indx;
05244       basic_block bb = INDEX_EDGE_PRED_BB (edge_list, e);
05245 
05246       for (i = indx = 0; i < set_size; i++, indx += SBITMAP_ELT_BITS)
05247   {
05248     SBITMAP_ELT_TYPE insert = pre_insert_map[e]->elms[i];
05249 
05250     for (j = indx; insert && j < (int) expr_hash_table.n_elems; j++, insert >>= 1)
05251       if ((insert & 1) != 0 && index_map[j]->reaching_reg != NULL_RTX)
05252         {
05253     struct expr *expr = index_map[j];
05254     struct occr *occr;
05255 
05256     /* Now look at each deleted occurrence of this expression.  */
05257     for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
05258       {
05259         if (! occr->deleted_p)
05260           continue;
05261 
05262         /* Insert this expression on this edge if if it would
05263            reach the deleted occurrence in BB.  */
05264         if (!TEST_BIT (inserted[e], j))
05265           {
05266       rtx insn;
05267       edge eg = INDEX_EDGE (edge_list, e);
05268 
05269       /* We can't insert anything on an abnormal and
05270          critical edge, so we insert the insn at the end of
05271          the previous block. There are several alternatives
05272          detailed in Morgans book P277 (sec 10.5) for
05273          handling this situation.  This one is easiest for
05274          now.  */
05275 
05276       if ((eg->flags & EDGE_ABNORMAL) == EDGE_ABNORMAL)
05277         insert_insn_end_bb (index_map[j], bb, 0);
05278       else
05279         {
05280           insn = process_insert_insn (index_map[j]);
05281           insert_insn_on_edge (insn, eg);
05282         }
05283 
05284       if (gcse_file)
05285         {
05286           fprintf (gcse_file, "PRE/HOIST: edge (%d,%d), ",
05287              bb->index,
05288              INDEX_EDGE_SUCC_BB (edge_list, e)->index);
05289           fprintf (gcse_file, "copy expression %d\n",
05290              expr->bitmap_index);
05291         }
05292 
05293       update_ld_motion_stores (expr);
05294       SET_BIT (inserted[e], j);
05295       did_insert = 1;
05296       gcse_create_count++;
05297           }
05298       }
05299         }
05300   }
05301     }
05302 
05303   sbitmap_vector_free (inserted);
05304   return did_insert;
05305 }
05306 
05307 /* Copy the result of INSN to REG.  INDX is the expression number.  */
05308 
05309 static void
05310 pre_insert_copy_insn (expr, insn)
05311      struct expr *expr;
05312      rtx insn;
05313 {
05314   rtx reg = expr->reaching_reg;
05315   int regno = REGNO (reg);
05316   int indx = expr->bitmap_index;
05317   rtx set = single_set (insn);
05318   rtx new_insn;
05319 
05320   if (!set)
05321     abort ();
05322 
05323   new_insn = emit_insn_after (gen_move_insn (reg, SET_DEST (set)), insn);
05324 
05325   /* Keep register set table up to date.  */
05326   record_one_set (regno, new_insn);
05327 
05328   gcse_create_count++;
05329 
05330   if (gcse_file)
05331     fprintf (gcse_file,
05332        "PRE: bb %d, insn %d, copy expression %d in insn %d to reg %d\n",
05333         BLOCK_NUM (insn), INSN_UID (new_insn), indx,
05334         INSN_UID (insn), regno);
05335   update_ld_motion_stores (expr);
05336 }
05337 
05338 /* Copy available expressions that reach the redundant expression
05339    to `reaching_reg'.  */
05340 
05341 static void
05342 pre_insert_copies ()
05343 {
05344   unsigned int i;
05345   struct expr *expr;
05346   struct occr *occr;
05347   struct occr *avail;
05348 
05349   /* For each available expression in the table, copy the result to
05350      `reaching_reg' if the expression reaches a deleted one.
05351 
05352      ??? The current algorithm is rather brute force.
05353      Need to do some profiling.  */
05354 
05355   for (i = 0; i < expr_hash_table.size; i++)
05356     for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
05357       {
05358   /* If the basic block isn't reachable, PPOUT will be TRUE.  However,
05359      we don't want to insert a copy here because the expression may not
05360      really be redundant.  So only insert an insn if the expression was
05361      deleted.  This test also avoids further processing if the
05362      expression wasn't deleted anywhere.  */
05363   if (expr->reaching_reg == NULL)
05364     continue;
05365 
05366   for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
05367     {
05368       if (! occr->deleted_p)
05369         continue;
05370 
05371       for (avail = expr->avail_occr; avail != NULL; avail = avail->next)
05372         {
05373     rtx insn = avail->insn;
05374 
05375     /* No need to handle this one if handled already.  */
05376     if (avail->copied_p)
05377       continue;
05378 
05379     /* Don't handle this one if it's a redundant one.  */
05380     if (TEST_BIT (pre_redundant_insns, INSN_CUID (insn)))
05381       continue;
05382 
05383     /* Or if the expression doesn't reach the deleted one.  */
05384     if (! pre_expr_reaches_here_p (BLOCK_FOR_INSN (avail->insn),
05385                  expr,
05386                  BLOCK_FOR_INSN (occr->insn)))
05387       continue;
05388 
05389     /* Copy the result of avail to reaching_reg.  */
05390     pre_insert_copy_insn (expr, insn);
05391     avail->copied_p = 1;
05392         }
05393     }
05394       }
05395 }
05396 
05397 /* Emit move from SRC to DEST noting the equivalence with expression computed
05398    in INSN.  */
05399 static rtx
05400 gcse_emit_move_after (src, dest, insn)
05401      rtx src, dest, insn;
05402 {
05403   rtx new;
05404   rtx set = single_set (insn), set2;
05405   rtx note;
05406   rtx eqv;
05407 
05408   /* This should never fail since we're creating a reg->reg copy
05409      we've verified to be valid.  */
05410 
05411   new = emit_insn_after (gen_move_insn (dest, src), insn);
05412 
05413   /* Note the equivalence for local CSE pass.  */
05414   set2 = single_set (new);
05415   if (!set2 || !rtx_equal_p (SET_DEST (set2), dest))
05416     return new;
05417   if ((note = find_reg_equal_equiv_note (insn)))
05418     eqv = XEXP (note, 0);
05419   else
05420     eqv = SET_SRC (set);
05421 
05422   set_unique_reg_note (new, REG_EQUAL, copy_insn_1 (eqv));
05423 
05424   return new;
05425 }
05426 
05427 /* Delete redundant computations.
05428    Deletion is done by changing the insn to copy the `reaching_reg' of
05429    the expression into the result of the SET.  It is left to later passes
05430    (cprop, cse2, flow, combine, regmove) to propagate the copy or eliminate it.
05431 
05432    Returns nonzero if a change is made.  */
05433 
05434 static int
05435 pre_delete ()
05436 {
05437   unsigned int i;
05438   int changed;
05439   struct expr *expr;
05440   struct occr *occr;
05441 
05442   changed = 0;
05443   for (i = 0; i < expr_hash_table.size; i++)
05444     for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
05445       {
05446   int indx = expr->bitmap_index;
05447 
05448   /* We only need to search antic_occr since we require
05449      ANTLOC != 0.  */
05450 
05451   for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
05452     {
05453       rtx insn = occr->insn;
05454       rtx set;
05455       basic_block bb = BLOCK_FOR_INSN (insn);
05456 
05457       if (TEST_BIT (pre_delete_map[bb->index], indx))
05458         {
05459     set = single_set (insn);
05460     if (! set)
05461       abort ();
05462 
05463     /* Create a pseudo-reg to store the result of reaching
05464        expressions into.  Get the mode for the new pseudo from
05465        the mode of the original destination pseudo.  */
05466     if (expr->reaching_reg == NULL)
05467       expr->reaching_reg
05468         = gen_reg_rtx (GET_MODE (SET_DEST (set)));
05469 
05470     gcse_emit_move_after (expr->reaching_reg, SET_DEST (set), insn);
05471     delete_insn (insn);
05472     occr->deleted_p = 1;
05473     SET_BIT (pre_redundant_insns, INSN_CUID (insn));
05474     changed = 1;
05475     gcse_subst_count++;
05476 
05477     if (gcse_file)
05478       {
05479         fprintf (gcse_file,
05480            "PRE: redundant insn %d (expression %d) in ",
05481              INSN_UID (insn), indx);
05482         fprintf (gcse_file, "bb %d, reaching reg is %d\n",
05483            bb->index, REGNO (expr->reaching_reg));
05484       }
05485         }
05486     }
05487       }
05488 
05489   return changed;
05490 }
05491 
05492 /* Perform GCSE optimizations using PRE.
05493    This is called by one_pre_gcse_pass after all the dataflow analysis
05494    has been done.
05495 
05496    This is based on the original Morel-Renvoise paper Fred Chow's thesis, and
05497    lazy code motion from Knoop, Ruthing and Steffen as described in Advanced
05498    Compiler Design and Implementation.
05499 
05500    ??? A new pseudo reg is created to hold the reaching expression.  The nice
05501    thing about the classical approach is that it would try to use an existing
05502    reg.  If the register can't be adequately optimized [i.e. we introduce
05503    reload problems], one could add a pass here to propagate the new register
05504    through the block.
05505 
05506    ??? We don't handle single sets in PARALLELs because we're [currently] not
05507    able to copy the rest of the parallel when we insert copies to create full
05508    redundancies from partial redundancies.  However, there's no reason why we
05509    can't handle PARALLELs in the cases where there are no partial
05510    redundancies.  */
05511 
05512 static int
05513 pre_gcse ()
05514 {
05515   unsigned int i;
05516   int did_insert, changed;
05517   struct expr **index_map;
05518   struct expr *expr;
05519 
05520   /* Compute a mapping from expression number (`bitmap_index') to
05521      hash table entry.  */
05522 
05523   index_map = (struct expr **) xcalloc (expr_hash_table.n_elems, sizeof (struct expr *));
05524   for (i = 0; i < expr_hash_table.size; i++)
05525     for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
05526       index_map[expr->bitmap_index] = expr;
05527 
05528   /* Reset bitmap used to track which insns are redundant.  */
05529   pre_redundant_insns = sbitmap_alloc (max_cuid);
05530   sbitmap_zero (pre_redundant_insns);
05531 
05532   /* Delete the redundant insns first so that
05533      - we know what register to use for the new insns and for the other
05534        ones with reaching expressions
05535      - we know which insns are redundant when we go to create copies  */
05536 
05537   changed = pre_delete ();
05538 
05539   did_insert = pre_edge_insert (edge_list, index_map);
05540 
05541   /* In other places with reaching expressions, copy the expression to the
05542      specially allocated pseudo-reg that reaches the redundant expr.  */
05543   pre_insert_copies ();
05544   if (did_insert)
05545     {
05546       commit_edge_insertions ();
05547       changed = 1;
05548     }
05549 
05550   free (index_map);
05551   sbitmap_free (pre_redundant_insns);
05552   return changed;
05553 }
05554 
05555 /* Top level routine to perform one PRE GCSE pass.
05556 
05557    Return nonzero if a change was made.  */
05558 
05559 static int
05560 one_pre_gcse_pass (pass)
05561      int pass;
05562 {
05563   int changed = 0;
05564 
05565   gcse_subst_count = 0;
05566   gcse_create_count = 0;
05567 
05568   alloc_hash_table (max_cuid, &expr_hash_table, 0);
05569   add_noreturn_fake_exit_edges ();
05570   if (flag_gcse_lm)
05571     compute_ld_motion_mems ();
05572 
05573   compute_hash_table (&expr_hash_table);
05574   trim_ld_motion_mems ();
05575   if (gcse_file)
05576     dump_hash_table (gcse_file, "Expression", &expr_hash_table);
05577 
05578   if (expr_hash_table.n_elems > 0)
05579     {
05580       alloc_pre_mem (last_basic_block, expr_hash_table.n_elems);
05581       compute_pre_data ();
05582       changed |= pre_gcse ();
05583       free_edge_list (edge_list);
05584       free_pre_mem ();
05585     }
05586 
05587   free_ldst_mems ();
05588   remove_fake_edges ();
05589   free_hash_table (&expr_hash_table);
05590 
05591   if (gcse_file)
05592     {
05593       fprintf (gcse_file, "\nPRE GCSE of %s, pass %d: %d bytes needed, ",
05594          current_function_name, pass, bytes_used);
05595       fprintf (gcse_file, "%d substs, %d insns created\n",
05596          gcse_subst_count, gcse_create_count);
05597     }
05598 
05599   return changed;
05600 }
05601 
05602 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to INSN.
05603    If notes are added to an insn which references a CODE_LABEL, the
05604    LABEL_NUSES count is incremented.  We have to add REG_LABEL notes,
05605    because the following loop optimization pass requires them.  */
05606 
05607 /* ??? This is very similar to the loop.c add_label_notes function.  We
05608    could probably share code here.  */
05609 
05610 /* ??? If there was a jump optimization pass after gcse and before loop,
05611    then we would not need to do this here, because jump would add the
05612    necessary REG_LABEL notes.  */
05613 
05614 static void
05615 add_label_notes (x, insn)
05616      rtx x;
05617      rtx insn;
05618 {
05619   enum rtx_code code = GET_CODE (x);
05620   int i, j;
05621   const char *fmt;
05622 
05623   if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
05624     {
05625       /* This code used to ignore labels that referred to dispatch tables to
05626    avoid flow generating (slighly) worse code.
05627 
05628    We no longer ignore such label references (see LABEL_REF handling in
05629    mark_jump_label for additional information).  */
05630 
05631       REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, XEXP (x, 0),
05632               REG_NOTES (insn));
05633       if (LABEL_P (XEXP (x, 0)))
05634   LABEL_NUSES (XEXP (x, 0))++;
05635       return;
05636     }
05637 
05638   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
05639     {
05640       if (fmt[i] == 'e')
05641   add_label_notes (XEXP (x, i), insn);
05642       else if (fmt[i] == 'E')
05643   for (j = XVECLEN (x, i) - 1; j >= 0; j--)
05644     add_label_notes (XVECEXP (x, i, j), insn);
05645     }
05646 }
05647 
05648 /* Compute transparent outgoing information for each block.
05649 
05650    An expression is transparent to an edge unless it is killed by
05651    the edge itself.  This can only happen with abnormal control flow,
05652    when the edge is traversed through a call.  This happens with
05653    non-local labels and exceptions.
05654 
05655    This would not be necessary if we split the edge.  While this is
05656    normally impossible for abnormal critical edges, with some effort
05657    it should be possible with exception handling, since we still have
05658    control over which handler should be invoked.  But due to increased
05659    EH table sizes, this may not be worthwhile.  */
05660 
05661 static void
05662 compute_transpout ()
05663 {
05664   basic_block bb;
05665   unsigned int i;
05666   struct expr *expr;
05667 
05668   sbitmap_vector_ones (transpout, last_basic_block);
05669 
05670   FOR_EACH_BB (bb)
05671     {
05672       /* Note that flow inserted a nop a the end of basic blocks that
05673    end in call instructions for reasons other than abnormal
05674    control flow.  */
05675       if (GET_CODE (bb->end) != CALL_INSN)
05676   continue;
05677 
05678       for (i = 0; i < expr_hash_table.size; i++)
05679   for (expr = expr_hash_table.table[i]; expr ; expr = expr->next_same_hash)
05680     if (GET_CODE (expr->expr) == MEM)
05681       {
05682         if (GET_CODE (XEXP (expr->expr, 0)) == SYMBOL_REF
05683       && CONSTANT_POOL_ADDRESS_P (XEXP (expr->expr, 0)))
05684     continue;
05685 
05686         /* ??? Optimally, we would use interprocedural alias
05687      analysis to determine if this mem is actually killed
05688      by this call.  */
05689         RESET_BIT (transpout[bb->index], expr->bitmap_index);
05690       }
05691     }
05692 }
05693 
05694 /* Removal of useless null pointer checks */
05695 
05696 /* Called via note_stores.  X is set by SETTER.  If X is a register we must
05697    invalidate nonnull_local and set nonnull_killed.  DATA is really a
05698    `null_pointer_info *'.
05699 
05700    We ignore hard registers.  */
05701 
05702 static void
05703 invalidate_nonnull_info (x, setter, data)
05704      rtx x;
05705      rtx setter ATTRIBUTE_UNUSED;
05706      void *data;
05707 {
05708   unsigned int regno;
05709   struct null_pointer_info *npi = (struct null_pointer_info *) data;
05710 
05711   while (GET_CODE (x) == SUBREG)
05712     x = SUBREG_REG (x);
05713 
05714   /* Ignore anything that is not a register or is a hard register.  */
05715   if (GET_CODE (x) != REG
05716       || REGNO (x) < npi->min_reg
05717       || REGNO (x) >= npi->max_reg)
05718     return;
05719 
05720   regno = REGNO (x) - npi->min_reg;
05721 
05722   RESET_BIT (npi->nonnull_local[npi->current_block->index], regno);
05723   SET_BIT (npi->nonnull_killed[npi->current_block->index], regno);
05724 }
05725 
05726 /* Do null-pointer check elimination for the registers indicated in
05727    NPI.  NONNULL_AVIN and NONNULL_AVOUT are pre-allocated sbitmaps;
05728    they are not our responsibility to free.  */
05729 
05730 static int
05731 delete_null_pointer_checks_1 (block_reg, nonnull_avin,
05732             nonnull_avout, npi)
05733      unsigned int *block_reg;
05734      sbitmap *nonnull_avin;
05735      sbitmap *nonnull_avout;
05736      struct null_pointer_info *npi;
05737 {
05738   basic_block bb, current_block;
05739   sbitmap *nonnull_local = npi->nonnull_local;
05740   sbitmap *nonnull_killed = npi->nonnull_killed;
05741   int something_changed = 0;
05742 
05743   /* Compute local properties, nonnull and killed.  A register will have
05744      the nonnull property if at the end of the current block its value is
05745      known to be nonnull.  The killed property indicates that somewhere in
05746      the block any information we had about the register is killed.
05747 
05748      Note that a register can have both properties in a single block.  That
05749      indicates that it's killed, then later in the block a new value is
05750      computed.  */
05751   sbitmap_vector_zero (nonnull_local, last_basic_block);
05752   sbitmap_vector_zero (nonnull_killed, last_basic_block);
05753 
05754   FOR_EACH_BB (current_block)
05755     {
05756       rtx insn, stop_insn;
05757 
05758       /* Set the current block for invalidate_nonnull_info.  */
05759       npi->current_block = current_block;
05760 
05761       /* Scan each insn in the basic block looking for memory references and
05762    register sets.  */
05763       stop_insn = NEXT_INSN (current_block->end);
05764       for (insn = current_block->head;
05765      insn != stop_insn;
05766      insn = NEXT_INSN (insn))
05767   {
05768     rtx set;
05769     rtx reg;
05770 
05771     /* Ignore anything that is not a normal insn.  */
05772     if (! INSN_P (insn))
05773       continue;
05774 
05775     /* Basically ignore anything that is not a simple SET.  We do have
05776        to make sure to invalidate nonnull_local and set nonnull_killed
05777        for such insns though.  */
05778     set = single_set (insn);
05779     if (!set)
05780       {
05781         note_stores (PATTERN (insn), invalidate_nonnull_info, npi);
05782         continue;
05783       }
05784 
05785     /* See if we've got a usable memory load.  We handle it first
05786        in case it uses its address register as a dest (which kills
05787        the nonnull property).  */
05788     if (GET_CODE (SET_SRC (set)) == MEM
05789         && GET_CODE ((reg = XEXP (SET_SRC (set), 0))) == REG
05790         && REGNO (reg) >= npi->min_reg
05791         && REGNO (reg) < npi->max_reg)
05792       SET_BIT (nonnull_local[current_block->index],
05793          REGNO (reg) - npi->min_reg);
05794 
05795     /* Now invalidate stuff clobbered by this insn.  */
05796     note_stores (PATTERN (insn), invalidate_nonnull_info, npi);
05797 
05798     /* And handle stores, we do these last since any sets in INSN can
05799        not kill the nonnull property if it is derived from a MEM
05800        appearing in a SET_DEST.  */
05801     if (GET_CODE (SET_DEST (set)) == MEM
05802         && GET_CODE ((reg = XEXP (SET_DEST (set), 0))) == REG
05803         && REGNO (reg) >= npi->min_reg
05804         && REGNO (reg) < npi->max_reg)
05805       SET_BIT (nonnull_local[current_block->index],
05806          REGNO (reg) - npi->min_reg);
05807   }
05808     }
05809 
05810   /* Now compute global properties based on the local properties.   This
05811      is a classic global availablity algorithm.  */
05812   compute_available (nonnull_local, nonnull_killed,
05813          nonnull_avout, nonnull_avin);
05814 
05815   /* Now look at each bb and see if it ends with a compare of a value
05816      against zero.  */
05817   FOR_EACH_BB (bb)
05818     {
05819       rtx last_insn = bb->end;
05820       rtx condition, earliest;
05821       int compare_and_branch;
05822 
05823       /* Since MIN_REG is always at least FIRST_PSEUDO_REGISTER, and
05824    since BLOCK_REG[BB] is zero if this block did not end with a
05825    comparison against zero, this condition works.  */
05826       if (block_reg[bb->index] < npi->min_reg
05827     || block_reg[bb->index] >= npi->max_reg)
05828   continue;
05829 
05830       /* LAST_INSN is a conditional jump.  Get its condition.  */
05831       condition = get_condition (last_insn, &earliest);
05832 
05833       /* If we can't determine the condition then skip.  */
05834       if (! condition)
05835   continue;
05836 
05837       /* Is the register known to have a nonzero value?  */
05838       if (!TEST_BIT (nonnull_avout[bb->index], block_reg[bb->index] - npi->min_reg))
05839   continue;
05840 
05841       /* Try to compute whether the compare/branch at the loop end is one or
05842    two instructions.  */
05843       if (earliest == last_insn)
05844   compare_and_branch = 1;
05845       else if (earliest == prev_nonnote_insn (last_insn))
05846   compare_and_branch = 2;
05847       else
05848   continue;
05849 
05850       /* We know the register in this comparison is nonnull at exit from
05851    this block.  We can optimize this comparison.  */
05852       if (GET_CODE (condition) == NE)
05853   {
05854     rtx new_jump;
05855 
05856     new_jump = emit_jump_insn_after (gen_jump (JUMP_LABEL (last_insn)),
05857              last_insn);
05858     JUMP_LABEL (new_jump) = JUMP_LABEL (last_insn);
05859     LABEL_NUSES (JUMP_LABEL (new_jump))++;
05860     emit_barrier_after (new_jump);
05861   }
05862 
05863       something_changed = 1;
05864       delete_insn (last_insn);
05865       if (compare_and_branch == 2)
05866   delete_insn (earliest);
05867       purge_dead_edges (bb);
05868 
05869       /* Don't check this block again.  (Note that BLOCK_END is
05870    invalid here; we deleted the last instruction in the
05871    block.)  */
05872       block_reg[bb->index] = 0;
05873     }
05874 
05875   return something_changed;
05876 }
05877 
05878 /* Find EQ/NE comparisons against zero which can be (indirectly) evaluated
05879    at compile time.
05880 
05881    This is conceptually similar to global constant/copy propagation and
05882    classic global CSE (it even uses the same dataflow equations as cprop).
05883 
05884    If a register is used as memory address with the form (mem (reg)), then we
05885    know that REG can not be zero at that point in the program.  Any instruction
05886    which sets REG "kills" this property.
05887 
05888    So, if every path leading to a conditional branch has an available memory
05889    reference of that form, then we know the register can not have the value
05890    zero at the conditional branch.
05891 
05892    So we merely need to compute the local properies and propagate that data
05893    around the cfg, then optimize where possible.
05894 
05895    We run this pass two times.  Once before CSE, then again after CSE.  This
05896    has proven to be the most profitable approach.  It is rare for new
05897    optimization opportunities of this nature to appear after the first CSE
05898    pass.
05899 
05900    This could probably be integrated with global cprop with a little work.  */
05901 
05902 int
05903 delete_null_pointer_checks (f)
05904      rtx f ATTRIBUTE_UNUSED;
05905 {
05906   sbitmap *nonnull_avin, *nonnull_avout;
05907   unsigned int *block_reg;
05908   basic_block bb;
05909   int reg;
05910   int regs_per_pass;
05911   int max_reg;
05912   struct null_pointer_info npi;
05913   int something_changed = 0;
05914 
05915   /* If we have only a single block, then there's nothing to do.  */
05916   if (n_basic_blocks <= 1)
05917     return 0;
05918 
05919   /* Trying to perform global optimizations on flow graphs which have
05920      a high connectivity will take a long time and is unlikely to be
05921      particularly useful.
05922 
05923      In normal circumstances a cfg should have about twice as many edges
05924      as blocks.  But we do not want to punish small functions which have
05925      a couple switch statements.  So we require a relatively large number
05926      of basic blocks and the ratio of edges to blocks to be high.  */
05927   if (n_basic_blocks > 1000 && n_edges / n_basic_blocks >= 20)
05928     return 0;
05929 
05930   /* We need four bitmaps, each with a bit for each register in each
05931      basic block.  */
05932   max_reg = max_reg_num ();
05933   regs_per_pass = get_bitmap_width (4, last_basic_block, max_reg);
05934 
05935   /* Allocate bitmaps to hold local and global properties.  */
05936   npi.nonnull_local = sbitmap_vector_alloc (last_basic_block, regs_per_pass);
05937   npi.nonnull_killed = sbitmap_vector_alloc (last_basic_block, regs_per_pass);
05938   nonnull_avin = sbitmap_vector_alloc (last_basic_block, regs_per_pass);
05939   nonnull_avout = sbitmap_vector_alloc (last_basic_block, regs_per_pass);
05940 
05941   /* Go through the basic blocks, seeing whether or not each block
05942      ends with a conditional branch whose condition is a comparison
05943      against zero.  Record the register compared in BLOCK_REG.  */
05944   block_reg = (unsigned int *) xcalloc (last_basic_block, sizeof (int));
05945   FOR_EACH_BB (bb)
05946     {
05947       rtx last_insn = bb->end;
05948       rtx condition, earliest, reg;
05949 
05950       /* We only want conditional branches.  */
05951       if (GET_CODE (last_insn) != JUMP_INSN
05952     || !any_condjump_p (last_insn)
05953     || !onlyjump_p (last_insn))
05954   continue;
05955 
05956       /* LAST_INSN is a conditional jump.  Get its condition.  */
05957       condition = get_condition (last_insn, &earliest);
05958 
05959       /* If we were unable to get the condition, or it is not an equality
05960    comparison against zero then there's nothing we can do.  */
05961       if (!condition
05962     || (GET_CODE (condition) != NE && GET_CODE (condition) != EQ)
05963     || GET_CODE (XEXP (condition, 1)) != CONST_INT
05964     || (XEXP (condition, 1)
05965         != CONST0_RTX (GET_MODE (XEXP (condition, 0)))))
05966   continue;
05967 
05968       /* We must be checking a register against zero.  */
05969       reg = XEXP (condition, 0);
05970       if (GET_CODE (reg) != REG)
05971   continue;
05972 
05973       block_reg[bb->index] = REGNO (reg);
05974     }
05975 
05976   /* Go through the algorithm for each block of registers.  */
05977   for (reg = FIRST_PSEUDO_REGISTER; reg < max_reg; reg += regs_per_pass)
05978     {
05979       npi.min_reg = reg;
05980       npi.max_reg = MIN (reg + regs_per_pass, max_reg);
05981       something_changed |= delete_null_pointer_checks_1 (block_reg,
05982                nonnull_avin,
05983                nonnull_avout,
05984                &npi);
05985     }
05986 
05987   /* Free the table of registers compared at the end of every block.  */
05988   free (block_reg);
05989 
05990   /* Free bitmaps.  */
05991   sbitmap_vector_free (npi.nonnull_local);
05992   sbitmap_vector_free (npi.nonnull_killed);
05993   sbitmap_vector_free (nonnull_avin);
05994   sbitmap_vector_free (nonnull_avout);
05995 
05996   return something_changed;
05997 }
05998 
05999 /* Code Hoisting variables and subroutines.  */
06000 
06001 /* Very busy expressions.  */
06002 static sbitmap *hoist_vbein;
06003 static sbitmap *hoist_vbeout;
06004 
06005 /* Hoistable expressions.  */
06006 static sbitmap *hoist_exprs;
06007 
06008 /* Dominator bitmaps.  */
06009 dominance_info dominators;
06010 
06011 /* ??? We could compute post dominators and run this algorithm in
06012    reverse to perform tail merging, doing so would probably be
06013    more effective than the tail merging code in jump.c.
06014 
06015    It's unclear if tail merging could be run in parallel with
06016    code hoisting.  It would be nice.  */
06017 
06018 /* Allocate vars used for code hoisting analysis.  */
06019 
06020 static void
06021 alloc_code_hoist_mem (n_blocks, n_exprs)
06022      int n_blocks, n_exprs;
06023 {
06024   antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
06025   transp = sbitmap_vector_alloc (n_blocks, n_exprs);
06026   comp = sbitmap_vector_alloc (n_blocks, n_exprs);
06027 
06028   hoist_vbein = sbitmap_vector_alloc (n_blocks, n_exprs);
06029   hoist_vbeout = sbitmap_vector_alloc (n_blocks, n_exprs);
06030   hoist_exprs = sbitmap_vector_alloc (n_blocks, n_exprs);
06031   transpout = sbitmap_vector_alloc (n_blocks, n_exprs);
06032 }
06033 
06034 /* Free vars used for code hoisting analysis.  */
06035 
06036 static void
06037 free_code_hoist_mem ()
06038 {
06039   sbitmap_vector_free (antloc);
06040   sbitmap_vector_free (transp);
06041   sbitmap_vector_free (comp);
06042 
06043   sbitmap_vector_free (hoist_vbein);
06044   sbitmap_vector_free (hoist_vbeout);
06045   sbitmap_vector_free (hoist_exprs);
06046   sbitmap_vector_free (transpout);
06047 
06048   free_dominance_info (dominators);
06049 }
06050 
06051 /* Compute the very busy expressions at entry/exit from each block.
06052 
06053    An expression is very busy if all paths from a given point
06054    compute the expression.  */
06055 
06056 static void
06057 compute_code_hoist_vbeinout ()
06058 {
06059   int changed, passes;
06060   basic_block bb;
06061 
06062   sbitmap_vector_zero (hoist_vbeout, last_basic_block);
06063   sbitmap_vector_zero (hoist_vbein, last_basic_block);
06064 
06065   passes = 0;
06066   changed = 1;
06067 
06068   while (changed)
06069     {
06070       changed = 0;
06071 
06072       /* We scan the blocks in the reverse order to speed up
06073    the convergence.  */
06074       FOR_EACH_BB_REVERSE (bb)
06075   {
06076     changed |= sbitmap_a_or_b_and_c_cg (hoist_vbein[bb->index], antloc[bb->index],
06077                 hoist_vbeout[bb->index], transp[bb->index]);
06078     if (bb->next_bb != EXIT_BLOCK_PTR)
06079       sbitmap_intersection_of_succs (hoist_vbeout[bb->index], hoist_vbein, bb->index);
06080   }
06081 
06082       passes++;
06083     }
06084 
06085   if (gcse_file)
06086     fprintf (gcse_file, "hoisting vbeinout computation: %d passes\n", passes);
06087 }
06088 
06089 /* Top level routine to do the dataflow analysis needed by code hoisting.  */
06090 
06091 static void
06092 compute_code_hoist_data ()
06093 {
06094   compute_local_properties (transp, comp, antloc, &expr_hash_table);
06095   compute_transpout ();
06096   compute_code_hoist_vbeinout ();
06097   dominators = calculate_dominance_info (CDI_DOMINATORS);
06098   if (gcse_file)
06099     fprintf (gcse_file, "\n");
06100 }
06101 
06102 /* Determine if the expression identified by EXPR_INDEX would
06103    reach BB unimpared if it was placed at the end of EXPR_BB.
06104 
06105    It's unclear exactly what Muchnick meant by "unimpared".  It seems
06106    to me that the expression must either be computed or transparent in
06107    *every* block in the path(s) from EXPR_BB to BB.  Any other definition
06108    would allow the expression to be hoisted out of loops, even if
06109    the expression wasn't a loop invariant.
06110 
06111    Contrast this to reachability for PRE where an expression is
06112    considered reachable if *any* path reaches instead of *all*
06113    paths.  */
06114 
06115 static int
06116 hoist_expr_reaches_here_p (expr_bb, expr_index, bb, visited)
06117      basic_block expr_bb;
06118      int expr_index;
06119      basic_block bb;
06120      char *visited;
06121 {
06122   edge pred;
06123   int visited_allocated_locally = 0;
06124 
06125 
06126   if (visited == NULL)
06127     {
06128       visited_allocated_locally = 1;
06129       visited = xcalloc (last_basic_block, 1);
06130     }
06131 
06132   for (pred = bb->pred; pred != NULL; pred = pred->pred_next)
06133     {
06134       basic_block pred_bb = pred->src;
06135 
06136       if (pred->src == ENTRY_BLOCK_PTR)
06137   break;
06138       else if (pred_bb == expr_bb)
06139   continue;
06140       else if (visited[pred_bb->index])
06141   continue;
06142 
06143       /* Does this predecessor generate this expression?  */
06144       else if (TEST_BIT (comp[pred_bb->index], expr_index))
06145   break;
06146       else if (! TEST_BIT (transp[pred_bb->index], expr_index))
06147   break;
06148 
06149       /* Not killed.  */
06150       else
06151   {
06152     visited[pred_bb->index] = 1;
06153     if (! hoist_expr_reaches_here_p (expr_bb, expr_index,
06154              pred_bb, visited))
06155       break;
06156   }
06157     }
06158   if (visited_allocated_locally)
06159     free (visited);
06160 
06161   return (pred == NULL);
06162 }
06163 
06164 /* Actually perform code hoisting.  */
06165 
06166 static void
06167 hoist_code ()
06168 {
06169   basic_block bb, dominated;
06170   basic_block *domby;
06171   unsigned int domby_len;
06172   unsigned int i,j;
06173   struct expr **index_map;
06174   struct expr *expr;
06175 
06176   sbitmap_vector_zero (hoist_exprs, last_basic_block);
06177 
06178   /* Compute a mapping from expression number (`bitmap_index') to
06179      hash table entry.  */
06180 
06181   index_map = (struct expr **) xcalloc (expr_hash_table.n_elems, sizeof (struct expr *));
06182   for (i = 0; i < expr_hash_table.size; i++)
06183     for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
06184       index_map[expr->bitmap_index] = expr;
06185 
06186   /* Walk over each basic block looking for potentially hoistable
06187      expressions, nothing gets hoisted from the entry block.  */
06188   FOR_EACH_BB (bb)
06189     {
06190       int found = 0;
06191       int insn_inserted_p;
06192 
06193       domby_len = get_dominated_by (dominators, bb, &domby);
06194       /* Examine each expression that is very busy at the exit of this
06195    block.  These are the potentially hoistable expressions.  */
06196       for (i = 0; i < hoist_vbeout[bb->index]->n_bits; i++)
06197   {
06198     int hoistable = 0;
06199 
06200     if (TEST_BIT (hoist_vbeout[bb->index], i)
06201         && TEST_BIT (transpout[bb->index], i))
06202       {
06203         /* We've found a potentially hoistable expression, now
06204      we look at every block BB dominates to see if it
06205      computes the expression.  */
06206         for (j = 0; j < domby_len; j++)
06207     {
06208       dominated = domby[j];
06209       /* Ignore self dominance.  */
06210       if (bb == dominated)
06211         continue;
06212       /* We've found a dominated block, now see if it computes
06213          the busy expression and whether or not moving that
06214          expression to the "beginning" of that block is safe.  */
06215       if (!TEST_BIT (antloc[dominated->index], i))
06216         continue;
06217 
06218       /* Note if the expression would reach the dominated block
06219          unimpared if it was placed at the end of BB.
06220 
06221          Keep track of how many times this expression is hoistable
06222          from a dominated block into BB.  */
06223       if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
06224         hoistable++;
06225     }
06226 
06227         /* If we found more than one hoistable occurrence of this
06228      expression, then note it in the bitmap of expressions to
06229      hoist.  It makes no sense to hoist things which are computed
06230      in only one BB, and doing so tends to pessimize register
06231      allocation.  One could increase this value to try harder
06232      to avoid any possible code expansion due to register
06233      allocation issues; however experiments have shown that
06234      the vast majority of hoistable expressions are only movable
06235      from two successors, so raising this threshhold is likely
06236      to nullify any benefit we get from code hoisting.  */
06237         if (hoistable > 1)
06238     {
06239       SET_BIT (hoist_exprs[bb->index], i);
06240       found = 1;
06241     }
06242       }
06243   }
06244       /* If we found nothing to hoist, then quit now.  */
06245       if (! found)
06246         {
06247       free (domby);
06248   continue;
06249   }
06250 
06251       /* Loop over all the hoistable expressions.  */
06252       for (i = 0; i < hoist_exprs[bb->index]->n_bits; i++)
06253   {
06254     /* We want to insert the expression into BB only once, so
06255        note when we've inserted it.  */
06256     insn_inserted_p = 0;
06257 
06258     /* These tests should be the same as the tests above.  */
06259     if (TEST_BIT (hoist_vbeout[bb->index], i))
06260       {
06261         /* We've found a potentially hoistable expression, now
06262      we look at every block BB dominates to see if it
06263      computes the expression.  */
06264         for (j = 0; j < domby_len; j++)
06265     {
06266       dominated = domby[j];
06267       /* Ignore self dominance.  */
06268       if (bb == dominated)
06269         continue;
06270 
06271       /* We've found a dominated block, now see if it computes
06272          the busy expression and whether or not moving that
06273          expression to the "beginning" of that block is safe.  */
06274       if (!TEST_BIT (antloc[dominated->index], i))
06275         continue;
06276 
06277       /* The expression is computed in the dominated block and
06278          it would be safe to compute it at the start of the
06279          dominated block.  Now we have to determine if the
06280          expression would reach the dominated block if it was
06281          placed at the end of BB.  */
06282       if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
06283         {
06284           struct expr *expr = index_map[i];
06285           struct occr *occr = expr->antic_occr;
06286           rtx insn;
06287           rtx set;
06288 
06289           /* Find the right occurrence of this expression.  */
06290           while (BLOCK_FOR_INSN (occr->insn) != dominated && occr)
06291       occr = occr->next;
06292 
06293           /* Should never happen.  */
06294           if (!occr)
06295       abort ();
06296 
06297           insn = occr->insn;
06298 
06299           set = single_set (insn);
06300           if (! set)
06301       abort ();
06302 
06303           /* Create a pseudo-reg to store the result of reaching
06304        expressions into.  Get the mode for the new pseudo
06305        from the mode of the original destination pseudo.  */
06306           if (expr->reaching_reg == NULL)
06307       expr->reaching_reg
06308         = gen_reg_rtx (GET_MODE (SET_DEST (set)));
06309 
06310           gcse_emit_move_after (expr->reaching_reg, SET_DEST (set), insn);
06311           delete_insn (insn);
06312           occr->deleted_p = 1;
06313           if (!insn_inserted_p)
06314       {
06315         insert_insn_end_bb (index_map[i], bb, 0);
06316         insn_inserted_p = 1;
06317       }
06318         }
06319     }
06320       }
06321   }
06322       free (domby);
06323     }
06324 
06325   free (index_map);
06326 }
06327 
06328 /* Top level routine to perform one code hoisting (aka unification) pass
06329 
06330    Return nonzero if a change was made.  */
06331 
06332 static int
06333 one_code_hoisting_pass ()
06334 {
06335   int changed = 0;
06336 
06337   alloc_hash_table (max_cuid, &expr_hash_table, 0);
06338   compute_hash_table (&expr_hash_table);
06339   if (gcse_file)
06340     dump_hash_table (gcse_file, "Code Hosting Expressions", &expr_hash_table);
06341 
06342   if (expr_hash_table.n_elems > 0)
06343     {
06344       alloc_code_hoist_mem (last_basic_block, expr_hash_table.n_elems);
06345       compute_code_hoist_data ();
06346       hoist_code ();
06347       free_code_hoist_mem ();
06348     }
06349 
06350   free_hash_table (&expr_hash_table);
06351 
06352   return changed;
06353 }
06354 
06355 /*  Here we provide the things required to do store motion towards
06356     the exit. In order for this to be effective, gcse also needed to
06357     be taught how to move a load when it is kill only by a store to itself.
06358 
06359       int i;
06360       float a[10];
06361 
06362       void foo(float scale)
06363       {
06364         for (i=0; i<10; i++)
06365     a[i] *= scale;
06366       }
06367 
06368     'i' is both loaded and stored to in the loop. Normally, gcse cannot move
06369     the load out since its live around the loop, and stored at the bottom
06370     of the loop.
06371 
06372       The 'Load Motion' referred to and implemented in this file is
06373     an enhancement to gcse which when using edge based lcm, recognizes
06374     this situation and allows gcse to move the load out of the loop.
06375 
06376       Once gcse has hoisted the load, store motion can then push this
06377     load towards the exit, and we end up with no loads or stores of 'i'
06378     in the loop.  */
06379 
06380 /* This will search the ldst list for a matching expression. If it
06381    doesn't find one, we create one and initialize it.  */
06382 
06383 static struct ls_expr *
06384 ldst_entry (x)
06385      rtx x;
06386 {
06387   struct ls_expr * ptr;
06388 
06389   for (ptr = first_ls_expr(); ptr != NULL; ptr = next_ls_expr (ptr))
06390     if (expr_equiv_p (ptr->pattern, x))
06391       break;
06392 
06393   if (!ptr)
06394     {
06395       ptr = (struct ls_expr *) xmalloc (sizeof (struct ls_expr));
06396 
06397       ptr->next         = pre_ldst_mems;
06398       ptr->expr         = NULL;
06399       ptr->pattern      = x;
06400       ptr->loads        = NULL_RTX;
06401       ptr->stores       = NULL_RTX;
06402       ptr->reaching_reg = NULL_RTX;
06403       ptr->invalid      = 0;
06404       ptr->index        = 0;
06405       ptr->hash_index   = 0;
06406       pre_ldst_mems     = ptr;
06407     }
06408 
06409   return ptr;
06410 }
06411 
06412 /* Free up an individual ldst entry.  */
06413 
06414 static void
06415 free_ldst_entry (ptr)
06416      struct ls_expr * ptr;
06417 {
06418   free_INSN_LIST_list (& ptr->loads);
06419   free_INSN_LIST_list (& ptr->stores);
06420 
06421   free (ptr);
06422 }
06423 
06424 /* Free up all memory associated with the ldst list.  */
06425 
06426 static void
06427 free_ldst_mems ()
06428 {
06429   while (pre_ldst_mems)
06430     {
06431       struct ls_expr * tmp = pre_ldst_mems;
06432 
06433       pre_ldst_mems = pre_ldst_mems->next;
06434 
06435       free_ldst_entry (tmp);
06436     }
06437 
06438   pre_ldst_mems = NULL;
06439 }
06440 
06441 /* Dump debugging info about the ldst list.  */
06442 
06443 static void
06444 print_ldst_list (file)
06445      FILE * file;
06446 {
06447   struct ls_expr * ptr;
06448 
06449   fprintf (file, "LDST list: \n");
06450 
06451   for (ptr = first_ls_expr(); ptr != NULL; ptr = next_ls_expr (ptr))
06452     {
06453       fprintf (file, "  Pattern (%3d): ", ptr->index);
06454 
06455       print_rtl (file, ptr->pattern);
06456 
06457       fprintf (file, "\n   Loads : ");
06458 
06459       if (ptr->loads)
06460   print_rtl (file, ptr->loads);
06461       else
06462   fprintf (file, "(nil)");
06463 
06464       fprintf (file, "\n  Stores : ");
06465 
06466       if (ptr->stores)
06467   print_rtl (file, ptr->stores);
06468       else
06469   fprintf (file, "(nil)");
06470 
06471       fprintf (file, "\n\n");
06472     }
06473 
06474   fprintf (file, "\n");
06475 }
06476 
06477 /* Returns 1 if X is in the list of ldst only expressions.  */
06478 
06479 static struct ls_expr *
06480 find_rtx_in_ldst (x)
06481      rtx x;
06482 {
06483   struct ls_expr * ptr;
06484 
06485   for (ptr = pre_ldst_mems; ptr != NULL; ptr = ptr->next)
06486     if (expr_equiv_p (ptr->pattern, x) && ! ptr->invalid)
06487       return ptr;
06488 
06489   return NULL;
06490 }
06491 
06492 /* Assign each element of the list of mems a monotonically increasing value.  */
06493 
06494 static int
06495 enumerate_ldsts ()
06496 {
06497   struct ls_expr * ptr;
06498   int n = 0;
06499 
06500   for (ptr = pre_ldst_mems; ptr != NULL; ptr = ptr->next)
06501     ptr->index = n++;
06502 
06503   return n;
06504 }
06505 
06506 /* Return first item in the list.  */
06507 
06508 static inline struct ls_expr *
06509 first_ls_expr ()
06510 {
06511   return pre_ldst_mems;
06512 }
06513 
06514 /* Return the next item in ther list after the specified one.  */
06515 
06516 static inline struct ls_expr *
06517 next_ls_expr (ptr)
06518      struct ls_expr * ptr;
06519 {
06520   return ptr->next;
06521 }
06522 
06523 /* Load Motion for loads which only kill themselves.  */
06524 
06525 /* Return true if x is a simple MEM operation, with no registers or
06526    side effects. These are the types of loads we consider for the
06527    ld_motion list, otherwise we let the usual aliasing take care of it.  */
06528 
06529 static int
06530 simple_mem (x)
06531      rtx x;
06532 {
06533   if (GET_CODE (x) != MEM)
06534     return 0;
06535 
06536   if (MEM_VOLATILE_P (x))
06537     return 0;
06538 
06539   if (GET_MODE (x) == BLKmode)
06540     return 0;
06541 
06542   if (!rtx_varies_p (XEXP (x, 0), 0))
06543     return 1;
06544 
06545   return 0;
06546 }
06547 
06548 /* Make sure there isn't a buried reference in this pattern anywhere.
06549    If there is, invalidate the entry for it since we're not capable
06550    of fixing it up just yet.. We have to be sure we know about ALL
06551    loads since the aliasing code will allow all entries in the
06552    ld_motion list to not-alias itself.  If we miss a load, we will get
06553    the wrong value since gcse might common it and we won't know to
06554    fix it up.  */
06555 
06556 static void
06557 invalidate_any_buried_refs (x)
06558      rtx x;
06559 {
06560   const char * fmt;
06561   int i, j;
06562   struct ls_expr * ptr;
06563 
06564   /* Invalidate it in the list.  */
06565   if (GET_CODE (x) == MEM && simple_mem (x))
06566     {
06567       ptr = ldst_entry (x);
06568       ptr->invalid = 1;
06569     }
06570 
06571   /* Recursively process the insn.  */
06572   fmt = GET_RTX_FORMAT (GET_CODE (x));
06573 
06574   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
06575     {
06576       if (fmt[i] == 'e')
06577   invalidate_any_buried_refs (XEXP (x, i));
06578       else if (fmt[i] == 'E')
06579   for (j = XVECLEN (x, i) - 1; j >= 0; j--)
06580     invalidate_any_buried_refs (XVECEXP (x, i, j));
06581     }
06582 }
06583 
06584 /* Find all the 'simple' MEMs which are used in LOADs and STORES. Simple
06585    being defined as MEM loads and stores to symbols, with no
06586    side effects and no registers in the expression. If there are any
06587    uses/defs which don't match this criteria, it is invalidated and
06588    trimmed out later.  */
06589 
06590 static void
06591 compute_ld_motion_mems ()
06592 {
06593   struct ls_expr * ptr;
06594   basic_block bb;
06595   rtx insn;
06596 
06597   pre_ldst_mems = NULL;
06598 
06599   FOR_EACH_BB (bb)
06600     {
06601       for (insn = bb->head;
06602      insn && insn != NEXT_INSN (bb->end);
06603      insn = NEXT_INSN (insn))
06604   {
06605     if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
06606       {
06607         if (GET_CODE (PATTERN (insn)) == SET)
06608     {
06609       rtx src = SET_SRC (PATTERN (insn));
06610       rtx dest = SET_DEST (PATTERN (insn));
06611 
06612       /* Check for a simple LOAD...  */
06613       if (GET_CODE (src) == MEM && simple_mem (src))
06614         {
06615           ptr = ldst_entry (src);
06616           if (GET_CODE (dest) == REG)
06617       ptr->loads = alloc_INSN_LIST (insn, ptr->loads);
06618           else
06619       ptr->invalid = 1;
06620         }
06621       else
06622         {
06623           /* Make sure there isn't a buried load somewhere.  */
06624           invalidate_any_buried_refs (src);
06625         }
06626 
06627       /* Check for stores. Don't worry about aliased ones, they
06628          will block any movement we might do later. We only care
06629          about this exact pattern since those are the only
06630          circumstance that we will ignore the aliasing info.  */
06631       if (GET_CODE (dest) == MEM && simple_mem (dest))
06632         {
06633           ptr = ldst_entry (dest);
06634 
06635           if (GET_CODE (src) != MEM
06636         && GET_CODE (src) != ASM_OPERANDS)
06637       ptr->stores = alloc_INSN_LIST (insn, ptr->stores);
06638           else
06639       ptr->invalid = 1;
06640         }
06641     }
06642         else
06643     invalidate_any_buried_refs (PATTERN (insn));
06644       }
06645   }
06646     }
06647 }
06648 
06649 /* Remove any references that have been either invalidated or are not in the
06650    expression list for pre gcse.  */
06651 
06652 static void
06653 trim_ld_motion_mems ()
06654 {
06655   struct ls_expr * last = NULL;
06656   struct ls_expr * ptr = first_ls_expr ();
06657 
06658   while (ptr != NULL)
06659     {
06660       int del = ptr->invalid;
06661       struct expr * expr = NULL;
06662 
06663       /* Delete if entry has been made invalid.  */
06664       if (!del)
06665   {
06666     unsigned int i;
06667 
06668     del = 1;
06669     /* Delete if we cannot find this mem in the expression list.  */
06670     for (i = 0; i < expr_hash_table.size && del; i++)
06671       {
06672         for (expr = expr_hash_table.table[i];
06673        expr != NULL;
06674        expr = expr->next_same_hash)
06675     if (expr_equiv_p (expr->expr, ptr->pattern))
06676       {
06677         del = 0;
06678         break;
06679       }
06680       }
06681   }
06682 
06683       if (del)
06684   {
06685     if (last != NULL)
06686       {
06687         last->next = ptr->next;
06688         free_ldst_entry (ptr);
06689         ptr = last->next;
06690       }
06691     else
06692       {
06693         pre_ldst_mems = pre_ldst_mems->next;
06694         free_ldst_entry (ptr);
06695         ptr = pre_ldst_mems;
06696       }
06697   }
06698       else
06699   {
06700     /* Set the expression field if we are keeping it.  */
06701     last = ptr;
06702     ptr->expr = expr;
06703     ptr = ptr->next;
06704   }
06705     }
06706 
06707   /* Show the world what we've found.  */
06708   if (gcse_file && pre_ldst_mems != NULL)
06709     print_ldst_list (gcse_file);
06710 }
06711 
06712 /* This routine will take an expression which we are replacing with
06713    a reaching register, and update any stores that are needed if
06714    that expression is in the ld_motion list.  Stores are updated by
06715    copying their SRC to the reaching register, and then storeing
06716    the reaching register into the store location. These keeps the
06717    correct value in the reaching register for the loads.  */
06718 
06719 static void
06720 update_ld_motion_stores (expr)
06721      struct expr * expr;
06722 {
06723   struct ls_expr * mem_ptr;
06724 
06725   if ((mem_ptr = find_rtx_in_ldst (expr->expr)))
06726     {
06727       /* We can try to find just the REACHED stores, but is shouldn't
06728    matter to set the reaching reg everywhere...  some might be
06729    dead and should be eliminated later.  */
06730 
06731       /* We replace  SET mem = expr   with
06732      SET reg = expr
06733      SET mem = reg , where reg is the
06734      reaching reg used in the load.  */
06735       rtx list = mem_ptr->stores;
06736 
06737       for ( ; list != NULL_RTX; list = XEXP (list, 1))
06738   {
06739     rtx insn = XEXP (list, 0);
06740     rtx pat = PATTERN (insn);
06741     rtx src = SET_SRC (pat);
06742     rtx reg = expr->reaching_reg;
06743     rtx copy, new;
06744 
06745     /* If we've already copied it, continue.  */
06746     if (expr->reaching_reg == src)
06747       continue;
06748 
06749     if (gcse_file)
06750       {
06751         fprintf (gcse_file, "PRE:  store updated with reaching reg ");
06752         print_rtl (gcse_file, expr->reaching_reg);
06753         fprintf (gcse_file, ":\n  ");
06754         print_inline_rtx (gcse_file, insn, 8);
06755         fprintf (gcse_file, "\n");
06756       }
06757 
06758     copy = gen_move_insn ( reg, SET_SRC (pat));
06759     new = emit_insn_before (copy, insn);
06760     record_one_set (REGNO (reg), new);
06761     SET_SRC (pat) = reg;
06762 
06763     /* un-recognize this pattern since it's probably different now.  */
06764     INSN_CODE (insn) = -1;
06765     gcse_create_count++;
06766   }
06767     }
06768 }
06769 
06770 /* Store motion code.  */
06771 
06772 /* This is used to communicate the target bitvector we want to use in the
06773    reg_set_info routine when called via the note_stores mechanism.  */
06774 static sbitmap * regvec;
06775 
06776 /* Used in computing the reverse edge graph bit vectors.  */
06777 static sbitmap * st_antloc;
06778 
06779 /* Global holding the number of store expressions we are dealing with.  */
06780 static int num_stores;
06781 
06782 /* Checks to set if we need to mark a register set. Called from note_stores.  */
06783 
06784 static void
06785 reg_set_info (dest, setter, data)
06786      rtx dest, setter ATTRIBUTE_UNUSED;
06787      void * data ATTRIBUTE_UNUSED;
06788 {
06789   if (GET_CODE (dest) == SUBREG)
06790     dest = SUBREG_REG (dest);
06791 
06792   if (GET_CODE (dest) == REG)
06793     SET_BIT (*regvec, REGNO (dest));
06794 }
06795 
06796 /* Return nonzero if the register operands of expression X are killed
06797    anywhere in basic block BB.  */
06798 
06799 static int
06800 store_ops_ok (x, bb)
06801      rtx x;
06802      basic_block bb;
06803 {
06804   int i;
06805   enum rtx_code code;
06806   const char * fmt;
06807 
06808   /* Repeat is used to turn tail-recursion into iteration.  */
06809  repeat:
06810 
06811   if (x == 0)
06812     return 1;
06813 
06814   code = GET_CODE (x);
06815   switch (code)
06816     {
06817     case REG:
06818   /* If a reg has changed after us in this
06819      block, the operand has been killed.  */
06820   return TEST_BIT (reg_set_in_block[bb->index], REGNO (x));
06821 
06822     case MEM:
06823       x = XEXP (x, 0);
06824       goto repeat;
06825 
06826     case PRE_DEC:
06827     case PRE_INC:
06828     case POST_DEC:
06829     case POST_INC:
06830       return 0;
06831 
06832     case PC:
06833     case CC0: /*FIXME*/
06834     case CONST:
06835     case CONST_INT:
06836     case CONST_DOUBLE:
06837     case CONST_VECTOR:
06838     case SYMBOL_REF:
06839     case LABEL_REF:
06840     case ADDR_VEC:
06841     case ADDR_DIFF_VEC:
06842       return 1;
06843 
06844     default:
06845       break;
06846     }
06847 
06848   i = GET_RTX_LENGTH (code) - 1;
06849   fmt = GET_RTX_FORMAT (code);
06850 
06851   for (; i >= 0; i--)
06852     {
06853       if (fmt[i] == 'e')
06854   {
06855     rtx tem = XEXP (x, i);
06856 
06857     /* If we are about to do the last recursive call
06858        needed at this level, change it into iteration.
06859        This function is called enough to be worth it.  */
06860     if (i == 0)
06861       {
06862         x = tem;
06863         goto repeat;
06864       }
06865 
06866     if (! store_ops_ok (tem, bb))
06867       return 0;
06868   }
06869       else if (fmt[i] == 'E')
06870   {
06871     int j;
06872 
06873     for (j = 0; j < XVECLEN (x, i); j++)
06874       {
06875         if (! store_ops_ok (XVECEXP (x, i, j), bb))
06876     return 0;
06877       }
06878   }
06879     }
06880 
06881   return 1;
06882 }
06883 
06884 /* Determine whether insn is MEM store pattern that we will consider moving.  */
06885 
06886 static void
06887 find_moveable_store (insn)
06888      rtx insn;
06889 {
06890   struct ls_expr * ptr;
06891   rtx dest = PATTERN (insn);
06892 
06893   if (GET_CODE (dest) != SET
06894       || GET_CODE (SET_SRC (dest)) == ASM_OPERANDS)
06895     return;
06896 
06897   dest = SET_DEST (dest);
06898 
06899   if (GET_CODE (dest) != MEM || MEM_VOLATILE_P (dest)
06900       || GET_MODE (dest) == BLKmode)
06901     return;
06902 
06903   if (GET_CODE (XEXP (dest, 0)) != SYMBOL_REF)
06904       return;
06905 
06906   if (rtx_varies_p (XEXP (dest, 0), 0))
06907     return;
06908 
06909   ptr = ldst_entry (dest);
06910   ptr->stores = alloc_INSN_LIST (insn, ptr->stores);
06911 }
06912 
06913 /* Perform store motion. Much like gcse, except we move expressions the
06914    other way by looking at the flowgraph in reverse.  */
06915 
06916 static int
06917 compute_store_table ()
06918 {
06919   int ret;
06920   basic_block bb;
06921   unsigned regno;
06922   rtx insn, pat;
06923 
06924   max_gcse_regno = max_reg_num ();
06925 
06926   reg_set_in_block = (sbitmap *) sbitmap_vector_alloc (last_basic_block,
06927                    max_gcse_regno);
06928   sbitmap_vector_zero (reg_set_in_block, last_basic_block);
06929   pre_ldst_mems = 0;
06930 
06931   /* Find all the stores we care about.  */
06932   FOR_EACH_BB (bb)
06933     {
06934       regvec = & (reg_set_in_block[bb->index]);
06935       for (insn = bb->end;
06936      insn && insn != PREV_INSN (bb->end);
06937      insn = PREV_INSN (insn))
06938   {
06939     /* Ignore anything that is not a normal insn.  */
06940     if (! INSN_P (insn))
06941       continue;
06942 
06943     if (GET_CODE (insn) == CALL_INSN)
06944       {
06945         bool clobbers_all = false;
06946 #ifdef NON_SAVING_SETJMP
06947         if (NON_SAVING_SETJMP
06948       && find_reg_note (insn, REG_SETJMP, NULL_RTX))
06949     clobbers_all = true;
06950 #endif
06951 
06952         for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
06953     if (clobbers_all
06954         || TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
06955       SET_BIT (reg_set_in_block[bb->index], regno);
06956       }
06957 
06958     pat = PATTERN (insn);
06959     note_stores (pat, reg_set_info, NULL);
06960 
06961     /* Now that we've marked regs, look for stores.  */
06962     if (GET_CODE (pat) == SET)
06963       find_moveable_store (insn);
06964   }
06965     }
06966 
06967   ret = enumerate_ldsts ();
06968 
06969   if (gcse_file)
06970     {
06971       fprintf (gcse_file, "Store Motion Expressions.\n");
06972       print_ldst_list (gcse_file);
06973     }
06974 
06975   return ret;
06976 }
06977 
06978 /* Check to see if the load X is aliased with STORE_PATTERN.  */
06979 
06980 static int
06981 load_kills_store (x, store_pattern)
06982      rtx x, store_pattern;
06983 {
06984   if (true_dependence (x, GET_MODE (x), store_pattern, rtx_addr_varies_p))
06985     return 1;
06986   return 0;
06987 }
06988 
06989 /* Go through the entire insn X, looking for any loads which might alias
06990    STORE_PATTERN.  Return 1 if found.  */
06991 
06992 static int
06993 find_loads (x, store_pattern)
06994      rtx x, store_pattern;
06995 {
06996   const char * fmt;
06997   int i, j;
06998   int ret = 0;
06999 
07000   if (!x)
07001     return 0;
07002 
07003   if (GET_CODE (x) == SET)
07004     x = SET_SRC (x);
07005 
07006   if (GET_CODE (x) == MEM)
07007     {
07008       if (load_kills_store (x, store_pattern))
07009   return 1;
07010     }
07011 
07012   /* Recursively process the insn.  */
07013   fmt = GET_RTX_FORMAT (GET_CODE (x));
07014 
07015   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0 && !ret; i--)
07016     {
07017       if (fmt[i] == 'e')
07018   ret |= find_loads (XEXP (x, i), store_pattern);
07019       else if (fmt[i] == 'E')
07020   for (j = XVECLEN (x, i) - 1; j >= 0; j--)
07021     ret |= find_loads (XVECEXP (x, i, j), store_pattern);
07022     }
07023   return ret;
07024 }
07025 
07026 /* Check if INSN kills the store pattern X (is aliased with it).
07027    Return 1 if it it does.  */
07028 
07029 static int
07030 store_killed_in_insn (x, insn)
07031      rtx x, insn;
07032 {
07033   if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
07034     return 0;
07035 
07036   if (GET_CODE (insn) == CALL_INSN)
07037     {
07038       /* A normal or pure call might read from pattern,
07039    but a const call will not.  */
07040       return ! CONST_OR_PURE_CALL_P (insn) || pure_call_p (insn);
07041     }
07042 
07043   if (GET_CODE (PATTERN (insn)) == SET)
07044     {
07045       rtx pat = PATTERN (insn);
07046       /* Check for memory stores to aliased objects.  */
07047       if (GET_CODE (SET_DEST (pat)) == MEM && !expr_equiv_p (SET_DEST (pat), x))
07048   /* pretend its a load and check for aliasing.  */
07049   if (find_loads (SET_DEST (pat), x))
07050     return 1;
07051       return find_loads (SET_SRC (pat), x);
07052     }
07053   else
07054     return find_loads (PATTERN (insn), x);
07055 }
07056 
07057 /* Returns 1 if the expression X is loaded or clobbered on or after INSN
07058    within basic block BB.  */
07059 
07060 static int
07061 store_killed_after (x, insn, bb)
07062      rtx x, insn;
07063      basic_block bb;
07064 {
07065   rtx last = bb->end;
07066 
07067   if (insn == last)
07068     return 0;
07069 
07070   /* Check if the register operands of the store are OK in this block.
07071      Note that if registers are changed ANYWHERE in the block, we'll
07072      decide we can't move it, regardless of whether it changed above
07073      or below the store. This could be improved by checking the register
07074      operands while lookinng for aliasing in each insn.  */
07075   if (!store_ops_ok (XEXP (x, 0), bb))
07076     return 1;
07077 
07078   for ( ; insn && insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
07079     if (store_killed_in_insn (x, insn))
07080       return 1;
07081 
07082   return 0;
07083 }
07084 
07085 /* Returns 1 if the expression X is loaded or clobbered on or before INSN
07086    within basic block BB.  */
07087 static int
07088 store_killed_before (x, insn, bb)
07089      rtx x, insn;
07090      basic_block bb;
07091 {
07092   rtx first = bb->head;
07093 
07094   if (insn == first)
07095     return store_killed_in_insn (x, insn);
07096 
07097   /* Check if the register operands of the store are OK in this block.
07098      Note that if registers are changed ANYWHERE in the block, we'll
07099      decide we can't move it, regardless of whether it changed above
07100      or below the store. This could be improved by checking the register
07101      operands while lookinng for aliasing in each insn.  */
07102   if (!store_ops_ok (XEXP (x, 0), bb))
07103     return 1;
07104 
07105   for ( ; insn && insn != PREV_INSN (first); insn = PREV_INSN (insn))
07106     if (store_killed_in_insn (x, insn))
07107       return 1;
07108 
07109   return 0;
07110 }
07111 
07112 #define ANTIC_STORE_LIST(x) ((x)->loads)
07113 #define AVAIL_STORE_LIST(x) ((x)->stores)
07114 
07115 /* Given the table of available store insns at the end of blocks,
07116    determine which ones are not killed by aliasing, and generate
07117    the appropriate vectors for gen and killed.  */
07118 static void
07119 build_store_vectors ()
07120 {
07121   basic_block bb, b;
07122   rtx insn, st;
07123   struct ls_expr * ptr;
07124 
07125   /* Build the gen_vector. This is any store in the table which is not killed
07126      by aliasing later in its block.  */
07127   ae_gen = (sbitmap *) sbitmap_vector_alloc (last_basic_block, num_stores);
07128   sbitmap_vector_zero (ae_gen, last_basic_block);
07129 
07130   st_antloc = (sbitmap *) sbitmap_vector_alloc (last_basic_block, num_stores);
07131   sbitmap_vector_zero (st_antloc, last_basic_block);
07132 
07133   for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
07134     {
07135       /* Put all the stores into either the antic list, or the avail list,
07136    or both.  */
07137       rtx store_list = ptr->stores;
07138       ptr->stores = NULL_RTX;
07139 
07140       for (st = store_list; st != NULL; st = XEXP (st, 1))
07141   {
07142     insn = XEXP (st, 0);
07143     bb = BLOCK_FOR_INSN (insn);
07144 
07145     if (!store_killed_after (ptr->pattern, insn, bb))
07146       {
07147         /* If we've already seen an availale expression in this block,
07148      we can delete the one we saw already (It occurs earlier in
07149      the block), and replace it with this one). We'll copy the
07150      old SRC expression to an unused register in case there
07151      are any side effects.  */
07152         if (TEST_BIT (ae_gen[bb->index], ptr->index))
07153     {
07154       /* Find previous store.  */
07155       rtx st;
07156       for (st = AVAIL_STORE_LIST (ptr); st ; st = XEXP (st, 1))
07157         if (BLOCK_FOR_INSN (XEXP (st, 0)) == bb)
07158           break;
07159       if (st)
07160         {
07161           rtx r = gen_reg_rtx (GET_MODE (ptr->pattern));
07162           if (gcse_file)
07163       fprintf (gcse_file, "Removing redundant store:\n");
07164           replace_store_insn (r, XEXP (st, 0), bb);
07165           XEXP (st, 0) = insn;
07166           continue;
07167         }
07168     }
07169         SET_BIT (ae_gen[bb->index], ptr->index);
07170         AVAIL_STORE_LIST (ptr) = alloc_INSN_LIST (insn,
07171               AVAIL_STORE_LIST (ptr));
07172       }
07173 
07174     if (!store_killed_before (ptr->pattern, insn, bb))
07175       {
07176         SET_BIT (st_antloc[BLOCK_NUM (insn)], ptr->index);
07177         ANTIC_STORE_LIST (ptr) = alloc_INSN_LIST (insn,
07178               ANTIC_STORE_LIST (ptr));
07179       }
07180   }
07181 
07182       /* Free the original list of store insns.  */
07183       free_INSN_LIST_list (&store_list);
07184     }
07185 
07186   ae_kill = (sbitmap *) sbitmap_vector_alloc (last_basic_block, num_stores);
07187   sbitmap_vector_zero (ae_kill, last_basic_block);
07188 
07189   transp = (sbitmap *) sbitmap_vector_alloc (last_basic_block, num_stores);
07190   sbitmap_vector_zero (transp, last_basic_block);
07191 
07192   for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
07193     FOR_EACH_BB (b)
07194       {
07195   if (store_killed_after (ptr->pattern, b->head, b))
07196     {
07197       /* The anticipatable expression is not killed if it's gen'd.  */
07198       /*
07199         We leave this check out for now. If we have a code sequence
07200         in a block which looks like:
07201       ST MEMa = x
07202       L     y = MEMa
07203       ST MEMa = z
07204         We should flag this as having an ANTIC expression, NOT
07205         transparent, NOT killed, and AVAIL.
07206         Unfortunately, since we haven't re-written all loads to
07207         use the reaching reg, we'll end up doing an incorrect
07208         Load in the middle here if we push the store down. It happens in
07209         gcc.c-torture/execute/960311-1.c with -O3
07210         If we always kill it in this case, we'll sometimes do
07211         uneccessary work, but it shouldn't actually hurt anything.
07212       if (!TEST_BIT (ae_gen[b], ptr->index)).  */
07213       SET_BIT (ae_kill[b->index], ptr->index);
07214     }
07215   else
07216     SET_BIT (transp[b->index], ptr->index);
07217       }
07218 
07219   /* Any block with no exits calls some non-returning function, so
07220      we better mark the store killed here, or we might not store to
07221      it at all.  If we knew it was abort, we wouldn't have to store,
07222      but we don't know that for sure.  */
07223   if (gcse_file)
07224     {
07225       fprintf (gcse_file, "ST_avail and ST_antic (shown under loads..)\n");
07226       print_ldst_list (gcse_file);
07227       dump_sbitmap_vector (gcse_file, "st_antloc", "", st_antloc, last_basic_block);
07228       dump_sbitmap_vector (gcse_file, "st_kill", "", ae_kill, last_basic_block);
07229       dump_sbitmap_vector (gcse_file, "Transpt", "", transp, last_basic_block);
07230       dump_sbitmap_vector (gcse_file, "st_avloc", "", ae_gen, last_basic_block);
07231     }
07232 }
07233 
07234 /* Insert an instruction at the begining of a basic block, and update
07235    the BLOCK_HEAD if needed.  */
07236 
07237 static void
07238 insert_insn_start_bb (insn, bb)
07239      rtx insn;
07240      basic_block bb;
07241 {
07242   /* Insert at start of successor block.  */
07243   rtx prev = PREV_INSN (bb->head);
07244   rtx before = bb->head;
07245   while (before != 0)
07246     {
07247       if (GET_CODE (before) != CODE_LABEL
07248     && (GET_CODE (before) != NOTE
07249         || NOTE_LINE_NUMBER (before) != NOTE_INSN_BASIC_BLOCK))
07250   break;
07251       prev = before;
07252       if (prev == bb->end)
07253   break;
07254       before = NEXT_INSN (before);
07255     }
07256 
07257   insn = emit_insn_after (insn, prev);
07258 
07259   if (gcse_file)
07260     {
07261       fprintf (gcse_file, "STORE_MOTION  insert store at start of BB %d:\n",
07262          bb->index);
07263       print_inline_rtx (gcse_file, insn, 6);
07264       fprintf (gcse_file, "\n");
07265     }
07266 }
07267 
07268 /* This routine will insert a store on an edge. EXPR is the ldst entry for
07269    the memory reference, and E is the edge to insert it on.  Returns nonzero
07270    if an edge insertion was performed.  */
07271 
07272 static int
07273 insert_store (expr, e)
07274      struct ls_expr * expr;
07275      edge e;
07276 {
07277   rtx reg, insn;
07278   basic_block bb;
07279   edge tmp;
07280 
07281   /* We did all the deleted before this insert, so if we didn't delete a
07282      store, then we haven't set the reaching reg yet either.  */
07283   if (expr->reaching_reg == NULL_RTX)
07284     return 0;
07285 
07286   reg = expr->reaching_reg;
07287   insn = gen_move_insn (expr->pattern, reg);
07288 
07289   /* If we are inserting this expression on ALL predecessor edges of a BB,
07290      insert it at the start of the BB, and reset the insert bits on the other
07291      edges so we don't try to insert it on the other edges.  */
07292   bb = e->dest;
07293   for (tmp = e->dest->pred; tmp ; tmp = tmp->pred_next)
07294     {
07295       int index = EDGE_INDEX (edge_list, tmp->src, tmp->dest);
07296       if (index == EDGE_INDEX_NO_EDGE)
07297   abort ();
07298       if (! TEST_BIT (pre_insert_map[index], expr->index))
07299   break;
07300     }
07301 
07302   /* If tmp is NULL, we found an insertion on every edge, blank the
07303      insertion vector for these edges, and insert at the start of the BB.  */
07304   if (!tmp && bb != EXIT_BLOCK_PTR)
07305     {
07306       for (tmp = e->dest->pred; tmp ; tmp = tmp->pred_next)
07307   {
07308     int index = EDGE_INDEX (edge_list, tmp->src, tmp->dest);
07309     RESET_BIT (pre_insert_map[index], expr->index);
07310   }
07311       insert_insn_start_bb (insn, bb);
07312       return 0;
07313     }
07314 
07315   /* We can't insert on this edge, so we'll insert at the head of the
07316      successors block.  See Morgan, sec 10.5.  */
07317   if ((e->flags & EDGE_ABNORMAL) == EDGE_ABNORMAL)
07318     {
07319       insert_insn_start_bb (insn, bb);
07320       return 0;
07321     }
07322 
07323   insert_insn_on_edge (insn, e);
07324 
07325   if (gcse_file)
07326     {
07327       fprintf (gcse_file, "STORE_MOTION  insert insn on edge (%d, %d):\n",
07328          e->src->index, e->dest->index);
07329       print_inline_rtx (gcse_file, insn, 6);
07330       fprintf (gcse_file, "\n");
07331     }
07332 
07333   return 1;
07334 }
07335 
07336 /* This routine will replace a store with a SET to a specified register.  */
07337 
07338 static void
07339 replace_store_insn (reg, del, bb)
07340      rtx reg, del;
07341      basic_block bb;
07342 {
07343   rtx insn;
07344 
07345   insn = gen_move_insn (reg, SET_SRC (PATTERN (del)));
07346   insn = emit_insn_after (insn, del);
07347 
07348   if (gcse_file)
07349     {
07350       fprintf (gcse_file,
07351          "STORE_MOTION  delete insn in BB %d:\n      ", bb->index);
07352       print_inline_rtx (gcse_file, del, 6);
07353       fprintf (gcse_file, "\nSTORE MOTION  replaced with insn:\n      ");
07354       print_inline_rtx (gcse_file, insn, 6);
07355       fprintf (gcse_file, "\n");
07356     }
07357 
07358   delete_insn (del);
07359 }
07360 
07361 
07362 /* Delete a store, but copy the value that would have been stored into
07363    the reaching_reg for later storing.  */
07364 
07365 static void
07366 delete_store (expr, bb)
07367      struct ls_expr * expr;
07368      basic_block bb;
07369 {
07370   rtx reg, i, del;
07371 
07372   if (expr->reaching_reg == NULL_RTX)
07373     expr->reaching_reg = gen_reg_rtx (GET_MODE (expr->pattern));
07374 
07375 
07376   /* If there is more than 1 store, the earlier ones will be dead,
07377      but it doesn't hurt to replace them here.  */
07378   reg = expr->reaching_reg;
07379 
07380   for (i = AVAIL_STORE_LIST (expr); i; i = XEXP (i, 1))
07381     {
07382       del = XEXP (i, 0);
07383       if (BLOCK_FOR_INSN (del) == bb)
07384   {
07385     /* We know there is only one since we deleted redundant
07386        ones during the available computation.  */
07387     replace_store_insn (reg, del, bb);
07388     break;
07389   }
07390     }
07391 }
07392 
07393 /* Free memory used by store motion.  */
07394 
07395 static void
07396 free_store_memory ()
07397 {
07398   free_ldst_mems ();
07399 
07400   if (ae_gen)
07401     sbitmap_vector_free (ae_gen);
07402   if (ae_kill)
07403     sbitmap_vector_free (ae_kill);
07404   if (transp)
07405     sbitmap_vector_free (transp);
07406   if (st_antloc)
07407     sbitmap_vector_free (st_antloc);
07408   if (pre_insert_map)
07409     sbitmap_vector_free (pre_insert_map);
07410   if (pre_delete_map)
07411     sbitmap_vector_free (pre_delete_map);
07412   if (reg_set_in_block)
07413     sbitmap_vector_free (reg_set_in_block);
07414 
07415   ae_gen = ae_kill = transp = st_antloc = NULL;
07416   pre_insert_map = pre_delete_map = reg_set_in_block = NULL;
07417 }
07418 
07419 /* Perform store motion. Much like gcse, except we move expressions the
07420    other way by looking at the flowgraph in reverse.  */
07421 
07422 static void
07423 store_motion ()
07424 {
07425   basic_block bb;
07426   int x;
07427   struct ls_expr * ptr;
07428   int update_flow = 0;
07429 
07430   if (gcse_file)
07431     {
07432       fprintf (gcse_file, "before store motion\n");
07433       print_rtl (gcse_file, get_insns ());
07434     }
07435 
07436 
07437   init_alias_analysis ();
07438 
07439   /* Find all the stores that are live to the end of their block.  */
07440   num_stores = compute_store_table ();
07441   if (num_stores == 0)
07442     {
07443       sbitmap_vector_free (reg_set_in_block);
07444       end_alias_analysis ();
07445       return;
07446     }
07447 
07448   /* Now compute whats actually available to move.  */
07449   add_noreturn_fake_exit_edges ();
07450   build_store_vectors ();
07451 
07452   edge_list = pre_edge_rev_lcm (gcse_file, num_stores, transp, ae_gen,
07453         st_antloc, ae_kill, &pre_insert_map,
07454         &pre_delete_map);
07455 
07456   /* Now we want to insert the new stores which are going to be needed.  */
07457   for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
07458     {
07459       FOR_EACH_BB (bb)
07460   if (TEST_BIT (pre_delete_map[bb->index], ptr->index))
07461     delete_store (ptr, bb);
07462 
07463       for (x = 0; x < NUM_EDGES (edge_list); x++)
07464   if (TEST_BIT (pre_insert_map[x], ptr->index))
07465     update_flow |= insert_store (ptr, INDEX_EDGE (edge_list, x));
07466     }
07467 
07468   if (update_flow)
07469     commit_edge_insertions ();
07470 
07471   free_store_memory ();
07472   free_edge_list (edge_list);
07473   remove_fake_edg