OSDN Git Service

* df-scan.c (df_insn_rescan): Salvage insn's LUID if the insn is
[pf3gnuchains/gcc-fork.git] / gcc / gcse.c
1 /* Global common subexpression elimination/Partial redundancy elimination
2    and global constant/copy propagation for GNU compiler.
3    Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4    2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* TODO
23    - reordering of memory allocation and freeing to be more space efficient
24    - do rough calc of how many regs are needed in each block, and a rough
25      calc of how many regs are available in each class and use that to
26      throttle back the code in cases where RTX_COST is minimal.
27    - a store to the same address as a load does not kill the load if the
28      source of the store is also the destination of the load.  Handling this
29      allows more load motion, particularly out of loops.
30
31 */
32
33 /* References searched while implementing this.
34
35    Compilers Principles, Techniques and Tools
36    Aho, Sethi, Ullman
37    Addison-Wesley, 1988
38
39    Global Optimization by Suppression of Partial Redundancies
40    E. Morel, C. Renvoise
41    communications of the acm, Vol. 22, Num. 2, Feb. 1979
42
43    A Portable Machine-Independent Global Optimizer - Design and Measurements
44    Frederick Chow
45    Stanford Ph.D. thesis, Dec. 1983
46
47    A Fast Algorithm for Code Movement Optimization
48    D.M. Dhamdhere
49    SIGPLAN Notices, Vol. 23, Num. 10, Oct. 1988
50
51    A Solution to a Problem with Morel and Renvoise's
52    Global Optimization by Suppression of Partial Redundancies
53    K-H Drechsler, M.P. Stadel
54    ACM TOPLAS, Vol. 10, Num. 4, Oct. 1988
55
56    Practical Adaptation of the Global Optimization
57    Algorithm of Morel and Renvoise
58    D.M. Dhamdhere
59    ACM TOPLAS, Vol. 13, Num. 2. Apr. 1991
60
61    Efficiently Computing Static Single Assignment Form and the Control
62    Dependence Graph
63    R. Cytron, J. Ferrante, B.K. Rosen, M.N. Wegman, and F.K. Zadeck
64    ACM TOPLAS, Vol. 13, Num. 4, Oct. 1991
65
66    Lazy Code Motion
67    J. Knoop, O. Ruthing, B. Steffen
68    ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
69
70    What's In a Region?  Or Computing Control Dependence Regions in Near-Linear
71    Time for Reducible Flow Control
72    Thomas Ball
73    ACM Letters on Programming Languages and Systems,
74    Vol. 2, Num. 1-4, Mar-Dec 1993
75
76    An Efficient Representation for Sparse Sets
77    Preston Briggs, Linda Torczon
78    ACM Letters on Programming Languages and Systems,
79    Vol. 2, Num. 1-4, Mar-Dec 1993
80
81    A Variation of Knoop, Ruthing, and Steffen's Lazy Code Motion
82    K-H Drechsler, M.P. Stadel
83    ACM SIGPLAN Notices, Vol. 28, Num. 5, May 1993
84
85    Partial Dead Code Elimination
86    J. Knoop, O. Ruthing, B. Steffen
87    ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
88
89    Effective Partial Redundancy Elimination
90    P. Briggs, K.D. Cooper
91    ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
92
93    The Program Structure Tree: Computing Control Regions in Linear Time
94    R. Johnson, D. Pearson, K. Pingali
95    ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
96
97    Optimal Code Motion: Theory and Practice
98    J. Knoop, O. Ruthing, B. Steffen
99    ACM TOPLAS, Vol. 16, Num. 4, Jul. 1994
100
101    The power of assignment motion
102    J. Knoop, O. Ruthing, B. Steffen
103    ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
104
105    Global code motion / global value numbering
106    C. Click
107    ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
108
109    Value Driven Redundancy Elimination
110    L.T. Simpson
111    Rice University Ph.D. thesis, Apr. 1996
112
113    Value Numbering
114    L.T. Simpson
115    Massively Scalar Compiler Project, Rice University, Sep. 1996
116
117    High Performance Compilers for Parallel Computing
118    Michael Wolfe
119    Addison-Wesley, 1996
120
121    Advanced Compiler Design and Implementation
122    Steven Muchnick
123    Morgan Kaufmann, 1997
124
125    Building an Optimizing Compiler
126    Robert Morgan
127    Digital Press, 1998
128
129    People wishing to speed up the code here should read:
130      Elimination Algorithms for Data Flow Analysis
131      B.G. Ryder, M.C. Paull
132      ACM Computing Surveys, Vol. 18, Num. 3, Sep. 1986
133
134      How to Analyze Large Programs Efficiently and Informatively
135      D.M. Dhamdhere, B.K. Rosen, F.K. Zadeck
136      ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
137
138    People wishing to do something different can find various possibilities
139    in the above papers and elsewhere.
140 */
141
142 #include "config.h"
143 #include "system.h"
144 #include "coretypes.h"
145 #include "tm.h"
146 #include "toplev.h"
147
148 #include "rtl.h"
149 #include "tree.h"
150 #include "tm_p.h"
151 #include "regs.h"
152 #include "hard-reg-set.h"
153 #include "flags.h"
154 #include "real.h"
155 #include "insn-config.h"
156 #include "recog.h"
157 #include "basic-block.h"
158 #include "output.h"
159 #include "function.h"
160 #include "expr.h"
161 #include "except.h"
162 #include "ggc.h"
163 #include "params.h"
164 #include "cselib.h"
165 #include "intl.h"
166 #include "obstack.h"
167 #include "timevar.h"
168 #include "tree-pass.h"
169 #include "hashtab.h"
170 #include "df.h"
171 #include "dbgcnt.h"
172
173 /* Propagate flow information through back edges and thus enable PRE's
174    moving loop invariant calculations out of loops.
175
176    Originally this tended to create worse overall code, but several
177    improvements during the development of PRE seem to have made following
178    back edges generally a win.
179
180    Note much of the loop invariant code motion done here would normally
181    be done by loop.c, which has more heuristics for when to move invariants
182    out of loops.  At some point we might need to move some of those
183    heuristics into gcse.c.  */
184
185 /* We support GCSE via Partial Redundancy Elimination.  PRE optimizations
186    are a superset of those done by GCSE.
187
188    We perform the following steps:
189
190    1) Compute table of places where registers are set.
191
192    2) Perform copy/constant propagation.
193
194    3) Perform global cse using lazy code motion if not optimizing
195       for size, or code hoisting if we are.
196
197    4) Perform another pass of copy/constant propagation.  Try to bypass
198       conditional jumps if the condition can be computed from a value of
199       an incoming edge.
200
201    5) Perform store motion.
202
203    Two passes of copy/constant propagation are done because the first one
204    enables more GCSE and the second one helps to clean up the copies that
205    GCSE creates.  This is needed more for PRE than for Classic because Classic
206    GCSE will try to use an existing register containing the common
207    subexpression rather than create a new one.  This is harder to do for PRE
208    because of the code motion (which Classic GCSE doesn't do).
209
210    Expressions we are interested in GCSE-ing are of the form
211    (set (pseudo-reg) (expression)).
212    Function want_to_gcse_p says what these are.
213
214    In addition, expressions in REG_EQUAL notes are candidates for GXSE-ing.
215    This allows PRE to hoist expressions that are expressed in multiple insns,
216    such as comprex address calculations (e.g. for PIC code, or loads with a 
217    high part and as lowe part).
218
219    PRE handles moving invariant expressions out of loops (by treating them as
220    partially redundant).
221
222    Eventually it would be nice to replace cse.c/gcse.c with SSA (static single
223    assignment) based GVN (global value numbering).  L. T. Simpson's paper
224    (Rice University) on value numbering is a useful reference for this.
225
226    **********************
227
228    We used to support multiple passes but there are diminishing returns in
229    doing so.  The first pass usually makes 90% of the changes that are doable.
230    A second pass can make a few more changes made possible by the first pass.
231    Experiments show any further passes don't make enough changes to justify
232    the expense.
233
234    A study of spec92 using an unlimited number of passes:
235    [1 pass] = 1208 substitutions, [2] = 577, [3] = 202, [4] = 192, [5] = 83,
236    [6] = 34, [7] = 17, [8] = 9, [9] = 4, [10] = 4, [11] = 2,
237    [12] = 2, [13] = 1, [15] = 1, [16] = 2, [41] = 1
238
239    It was found doing copy propagation between each pass enables further
240    substitutions.
241
242    This study was done before expressions in REG_EQUAL notes were added as
243    candidate expressions for optimization, and before the GIMPLE optimizers
244    were added.  Probably, multiple passes is even less efficient now than
245    at the time when the study was conducted.
246
247    PRE is quite expensive in complicated functions because the DFA can take
248    a while to converge.  Hence we only perform one pass.
249
250    **********************
251
252    The steps for PRE are:
253
254    1) Build the hash table of expressions we wish to GCSE (expr_hash_table).
255
256    2) Perform the data flow analysis for PRE.
257
258    3) Delete the redundant instructions
259
260    4) Insert the required copies [if any] that make the partially
261       redundant instructions fully redundant.
262
263    5) For other reaching expressions, insert an instruction to copy the value
264       to a newly created pseudo that will reach the redundant instruction.
265
266    The deletion is done first so that when we do insertions we
267    know which pseudo reg to use.
268
269    Various papers have argued that PRE DFA is expensive (O(n^2)) and others
270    argue it is not.  The number of iterations for the algorithm to converge
271    is typically 2-4 so I don't view it as that expensive (relatively speaking).
272
273    PRE GCSE depends heavily on the second CSE pass to clean up the copies
274    we create.  To make an expression reach the place where it's redundant,
275    the result of the expression is copied to a new register, and the redundant
276    expression is deleted by replacing it with this new register.  Classic GCSE
277    doesn't have this problem as much as it computes the reaching defs of
278    each register in each block and thus can try to use an existing
279    register.  */
280 \f
281 /* GCSE global vars.  */
282
283 /* Note whether or not we should run jump optimization after gcse.  We
284    want to do this for two cases.
285
286     * If we changed any jumps via cprop.
287
288     * If we added any labels via edge splitting.  */
289 static int run_jump_opt_after_gcse;
290
291 /* An obstack for our working variables.  */
292 static struct obstack gcse_obstack;
293
294 struct reg_use {rtx reg_rtx; };
295
296 /* Hash table of expressions.  */
297
298 struct expr
299 {
300   /* The expression (SET_SRC for expressions, PATTERN for assignments).  */
301   rtx expr;
302   /* Index in the available expression bitmaps.  */
303   int bitmap_index;
304   /* Next entry with the same hash.  */
305   struct expr *next_same_hash;
306   /* List of anticipatable occurrences in basic blocks in the function.
307      An "anticipatable occurrence" is one that is the first occurrence in the
308      basic block, the operands are not modified in the basic block prior
309      to the occurrence and the output is not used between the start of
310      the block and the occurrence.  */
311   struct occr *antic_occr;
312   /* List of available occurrence in basic blocks in the function.
313      An "available occurrence" is one that is the last occurrence in the
314      basic block and the operands are not modified by following statements in
315      the basic block [including this insn].  */
316   struct occr *avail_occr;
317   /* Non-null if the computation is PRE redundant.
318      The value is the newly created pseudo-reg to record a copy of the
319      expression in all the places that reach the redundant copy.  */
320   rtx reaching_reg;
321 };
322
323 /* Occurrence of an expression.
324    There is one per basic block.  If a pattern appears more than once the
325    last appearance is used [or first for anticipatable expressions].  */
326
327 struct occr
328 {
329   /* Next occurrence of this expression.  */
330   struct occr *next;
331   /* The insn that computes the expression.  */
332   rtx insn;
333   /* Nonzero if this [anticipatable] occurrence has been deleted.  */
334   char deleted_p;
335   /* Nonzero if this [available] occurrence has been copied to
336      reaching_reg.  */
337   /* ??? This is mutually exclusive with deleted_p, so they could share
338      the same byte.  */
339   char copied_p;
340 };
341
342 /* Expression and copy propagation hash tables.
343    Each hash table is an array of buckets.
344    ??? It is known that if it were an array of entries, structure elements
345    `next_same_hash' and `bitmap_index' wouldn't be necessary.  However, it is
346    not clear whether in the final analysis a sufficient amount of memory would
347    be saved as the size of the available expression bitmaps would be larger
348    [one could build a mapping table without holes afterwards though].
349    Someday I'll perform the computation and figure it out.  */
350
351 struct hash_table
352 {
353   /* The table itself.
354      This is an array of `expr_hash_table_size' elements.  */
355   struct expr **table;
356
357   /* Size of the hash table, in elements.  */
358   unsigned int size;
359
360   /* Number of hash table elements.  */
361   unsigned int n_elems;
362
363   /* Whether the table is expression of copy propagation one.  */
364   int set_p;
365 };
366
367 /* Expression hash table.  */
368 static struct hash_table expr_hash_table;
369
370 /* Copy propagation hash table.  */
371 static struct hash_table set_hash_table;
372
373 /* Maximum register number in function prior to doing gcse + 1.
374    Registers created during this pass have regno >= max_gcse_regno.
375    This is named with "gcse" to not collide with global of same name.  */
376 static unsigned int max_gcse_regno;
377
378 /* This is a list of expressions which are MEMs and will be used by load
379    or store motion.
380    Load motion tracks MEMs which aren't killed by
381    anything except itself. (i.e., loads and stores to a single location).
382    We can then allow movement of these MEM refs with a little special
383    allowance. (all stores copy the same value to the reaching reg used
384    for the loads).  This means all values used to store into memory must have
385    no side effects so we can re-issue the setter value.
386    Store Motion uses this structure as an expression table to track stores
387    which look interesting, and might be moveable towards the exit block.  */
388
389 struct ls_expr
390 {
391   struct expr * expr;           /* Gcse expression reference for LM.  */
392   rtx pattern;                  /* Pattern of this mem.  */
393   rtx pattern_regs;             /* List of registers mentioned by the mem.  */
394   rtx loads;                    /* INSN list of loads seen.  */
395   rtx stores;                   /* INSN list of stores seen.  */
396   struct ls_expr * next;        /* Next in the list.  */
397   int invalid;                  /* Invalid for some reason.  */
398   int index;                    /* If it maps to a bitmap index.  */
399   unsigned int hash_index;      /* Index when in a hash table.  */
400   rtx reaching_reg;             /* Register to use when re-writing.  */
401 };
402
403 /* Array of implicit set patterns indexed by basic block index.  */
404 static rtx *implicit_sets;
405
406 /* Head of the list of load/store memory refs.  */
407 static struct ls_expr * pre_ldst_mems = NULL;
408
409 /* Hashtable for the load/store memory refs.  */
410 static htab_t pre_ldst_table = NULL;
411
412 /* Bitmap containing one bit for each register in the program.
413    Used when performing GCSE to track which registers have been set since
414    the start of the basic block.  */
415 static regset reg_set_bitmap;
416
417 /* Array, indexed by basic block number for a list of insns which modify
418    memory within that block.  */
419 static rtx * modify_mem_list;
420 static bitmap modify_mem_list_set;
421
422 /* This array parallels modify_mem_list, but is kept canonicalized.  */
423 static rtx * canon_modify_mem_list;
424
425 /* Bitmap indexed by block numbers to record which blocks contain
426    function calls.  */
427 static bitmap blocks_with_calls;
428
429 /* Various variables for statistics gathering.  */
430
431 /* Memory used in a pass.
432    This isn't intended to be absolutely precise.  Its intent is only
433    to keep an eye on memory usage.  */
434 static int bytes_used;
435
436 /* GCSE substitutions made.  */
437 static int gcse_subst_count;
438 /* Number of copy instructions created.  */
439 static int gcse_create_count;
440 /* Number of local constants propagated.  */
441 static int local_const_prop_count;
442 /* Number of local copies propagated.  */
443 static int local_copy_prop_count;
444 /* Number of global constants propagated.  */
445 static int global_const_prop_count;
446 /* Number of global copies propagated.  */
447 static int global_copy_prop_count;
448 \f
449 /* For available exprs */
450 static sbitmap *ae_kill, *ae_gen;
451 \f
452 static void compute_can_copy (void);
453 static void recompute_all_luids (void);
454 static void *gmalloc (size_t) ATTRIBUTE_MALLOC;
455 static void *gcalloc (size_t, size_t) ATTRIBUTE_MALLOC;
456 static void *gcse_alloc (unsigned long);
457 static void alloc_gcse_mem (void);
458 static void free_gcse_mem (void);
459 static void hash_scan_insn (rtx, struct hash_table *);
460 static void hash_scan_set (rtx, rtx, struct hash_table *);
461 static void hash_scan_clobber (rtx, rtx, struct hash_table *);
462 static void hash_scan_call (rtx, rtx, struct hash_table *);
463 static int want_to_gcse_p (rtx);
464 static bool can_assign_to_reg_p (rtx);
465 static bool gcse_constant_p (const_rtx);
466 static int oprs_unchanged_p (const_rtx, const_rtx, int);
467 static int oprs_anticipatable_p (const_rtx, const_rtx);
468 static int oprs_available_p (const_rtx, const_rtx);
469 static void insert_expr_in_table (rtx, enum machine_mode, rtx, int, int,
470                                   struct hash_table *);
471 static void insert_set_in_table (rtx, rtx, struct hash_table *);
472 static unsigned int hash_expr (const_rtx, enum machine_mode, int *, int);
473 static unsigned int hash_set (int, int);
474 static int expr_equiv_p (const_rtx, const_rtx);
475 static void record_last_reg_set_info (rtx, int);
476 static void record_last_mem_set_info (rtx);
477 static void record_last_set_info (rtx, const_rtx, void *);
478 static void compute_hash_table (struct hash_table *);
479 static void alloc_hash_table (int, struct hash_table *, int);
480 static void free_hash_table (struct hash_table *);
481 static void compute_hash_table_work (struct hash_table *);
482 static void dump_hash_table (FILE *, const char *, struct hash_table *);
483 static struct expr *lookup_set (unsigned int, struct hash_table *);
484 static struct expr *next_set (unsigned int, struct expr *);
485 static void reset_opr_set_tables (void);
486 static int oprs_not_set_p (const_rtx, const_rtx);
487 static void mark_call (rtx);
488 static void mark_set (rtx, rtx);
489 static void mark_clobber (rtx, rtx);
490 static void mark_oprs_set (rtx);
491 static void alloc_cprop_mem (int, int);
492 static void free_cprop_mem (void);
493 static void compute_transp (const_rtx, int, sbitmap *, int);
494 static void compute_transpout (void);
495 static void compute_local_properties (sbitmap *, sbitmap *, sbitmap *,
496                                       struct hash_table *);
497 static void compute_cprop_data (void);
498 static void find_used_regs (rtx *, void *);
499 static int try_replace_reg (rtx, rtx, rtx);
500 static struct expr *find_avail_set (int, rtx);
501 static int cprop_jump (basic_block, rtx, rtx, rtx, rtx);
502 static void mems_conflict_for_gcse_p (rtx, const_rtx, void *);
503 static int load_killed_in_block_p (const_basic_block, int, const_rtx, int);
504 static void canon_list_insert (rtx, const_rtx, void *);
505 static int cprop_insn (rtx, int);
506 static int cprop (int);
507 static void find_implicit_sets (void);
508 static int one_cprop_pass (int, bool, bool);
509 static bool constprop_register (rtx, rtx, rtx, bool);
510 static struct expr *find_bypass_set (int, int);
511 static bool reg_killed_on_edge (const_rtx, const_edge);
512 static int bypass_block (basic_block, rtx, rtx);
513 static int bypass_conditional_jumps (void);
514 static void alloc_pre_mem (int, int);
515 static void free_pre_mem (void);
516 static void compute_pre_data (void);
517 static int pre_expr_reaches_here_p (basic_block, struct expr *,
518                                     basic_block);
519 static void insert_insn_end_basic_block (struct expr *, basic_block, int);
520 static void pre_insert_copy_insn (struct expr *, rtx);
521 static void pre_insert_copies (void);
522 static int pre_delete (void);
523 static int pre_gcse (void);
524 static int one_pre_gcse_pass (int);
525 static void add_label_notes (rtx, rtx);
526 static void alloc_code_hoist_mem (int, int);
527 static void free_code_hoist_mem (void);
528 static void compute_code_hoist_vbeinout (void);
529 static void compute_code_hoist_data (void);
530 static int hoist_expr_reaches_here_p (basic_block, int, basic_block, char *);
531 static void hoist_code (void);
532 static int one_code_hoisting_pass (void);
533 static rtx process_insert_insn (struct expr *);
534 static int pre_edge_insert (struct edge_list *, struct expr **);
535 static int pre_expr_reaches_here_p_work (basic_block, struct expr *,
536                                          basic_block, char *);
537 static struct ls_expr * ldst_entry (rtx);
538 static void free_ldst_entry (struct ls_expr *);
539 static void free_ldst_mems (void);
540 static void print_ldst_list (FILE *);
541 static struct ls_expr * find_rtx_in_ldst (rtx);
542 static int enumerate_ldsts (void);
543 static inline struct ls_expr * first_ls_expr (void);
544 static inline struct ls_expr * next_ls_expr (struct ls_expr *);
545 static int simple_mem (const_rtx);
546 static void invalidate_any_buried_refs (rtx);
547 static void compute_ld_motion_mems (void);
548 static void trim_ld_motion_mems (void);
549 static void update_ld_motion_stores (struct expr *);
550 static void reg_set_info (rtx, const_rtx, void *);
551 static void reg_clear_last_set (rtx, const_rtx, void *);
552 static bool store_ops_ok (const_rtx, int *);
553 static rtx extract_mentioned_regs (rtx);
554 static rtx extract_mentioned_regs_helper (rtx, rtx);
555 static void find_moveable_store (rtx, int *, int *);
556 static int compute_store_table (void);
557 static bool load_kills_store (const_rtx, const_rtx, int);
558 static bool find_loads (const_rtx, const_rtx, int);
559 static bool store_killed_in_insn (const_rtx, const_rtx, const_rtx, int);
560 static bool store_killed_after (const_rtx, const_rtx, const_rtx, const_basic_block, int *, rtx *);
561 static bool store_killed_before (const_rtx, const_rtx, const_rtx, const_basic_block, int *);
562 static void build_store_vectors (void);
563 static void insert_insn_start_basic_block (rtx, basic_block);
564 static int insert_store (struct ls_expr *, edge);
565 static void remove_reachable_equiv_notes (basic_block, struct ls_expr *);
566 static void replace_store_insn (rtx, rtx, basic_block, struct ls_expr *);
567 static void delete_store (struct ls_expr *, basic_block);
568 static void free_store_memory (void);
569 static void store_motion (void);
570 static void free_insn_expr_list_list (rtx *);
571 static void clear_modify_mem_tables (void);
572 static void free_modify_mem_tables (void);
573 static rtx gcse_emit_move_after (rtx, rtx, rtx);
574 static void local_cprop_find_used_regs (rtx *, void *);
575 static bool do_local_cprop (rtx, rtx, bool);
576 static void local_cprop_pass (bool);
577 static bool is_too_expensive (const char *);
578
579 #define GNEW(T)                 ((T *) gmalloc (sizeof (T)))
580 #define GCNEW(T)                ((T *) gcalloc (1, sizeof (T)))
581
582 #define GNEWVEC(T, N)           ((T *) gmalloc (sizeof (T) * (N)))
583 #define GCNEWVEC(T, N)          ((T *) gcalloc ((N), sizeof (T)))
584
585 #define GNEWVAR(T, S)           ((T *) gmalloc ((S)))
586 #define GCNEWVAR(T, S)          ((T *) gcalloc (1, (S)))
587
588 #define GOBNEW(T)               ((T *) gcse_alloc (sizeof (T)))
589 #define GOBNEWVAR(T, S)         ((T *) gcse_alloc ((S)))
590 \f
591
592 /* Entry point for global common subexpression elimination.
593    F is the first instruction in the function.  Return nonzero if a
594    change is mode.  */
595
596 static int
597 gcse_main (rtx f ATTRIBUTE_UNUSED)
598 {
599   int changed;
600   /* Point to release obstack data from for each pass.  */
601   char *gcse_obstack_bottom;
602
603   /* We do not construct an accurate cfg in functions which call
604      setjmp, so just punt to be safe.  */
605   if (cfun->calls_setjmp)
606     return 0;
607
608   /* Assume that we do not need to run jump optimizations after gcse.  */
609   run_jump_opt_after_gcse = 0;
610
611   /* Identify the basic block information for this function, including
612      successors and predecessors.  */
613   max_gcse_regno = max_reg_num ();
614
615   df_note_add_problem ();
616   df_analyze ();
617
618   if (dump_file)
619     dump_flow_info (dump_file, dump_flags);
620
621   /* Return if there's nothing to do, or it is too expensive.  */
622   if (n_basic_blocks <= NUM_FIXED_BLOCKS + 1
623       || is_too_expensive (_("GCSE disabled")))
624     return 0;
625
626   gcc_obstack_init (&gcse_obstack);
627   bytes_used = 0;
628
629   /* We need alias.  */
630   init_alias_analysis ();
631
632   gcse_obstack_bottom = GOBNEWVAR (char, 1);
633   changed = 0;
634  
635   if (dump_file)
636     fprintf (dump_file, "GCSE pass\n\n");
637
638   max_gcse_regno = max_reg_num ();
639
640   alloc_gcse_mem ();
641
642   /* Don't allow constant propagation to modify jumps
643      during this pass.  */
644   if (dbg_cnt (cprop1))
645     {
646       timevar_push (TV_CPROP1);
647       changed = one_cprop_pass (1, false, false);
648       if (changed)
649         recompute_all_luids ();
650       timevar_pop (TV_CPROP1);
651     }
652
653   if (optimize_function_for_speed_p (cfun))
654     {
655       timevar_push (TV_PRE);
656       changed |= one_pre_gcse_pass (1);
657       /* We may have just created new basic blocks.  Release and
658          recompute various things which are sized on the number of
659          basic blocks.
660          ??? There would be no need for this if we used a block
661          based Lazy Code Motion variant, with all (or selected)
662          edges split before running the pass.  That would also
663          help find_implicit_sets for cprop.  FIXME.  */
664       if (changed)
665         {
666           free_modify_mem_tables ();
667           modify_mem_list = GCNEWVEC (rtx, last_basic_block);
668           canon_modify_mem_list = GCNEWVEC (rtx, last_basic_block);
669         }
670
671       df_analyze ();
672       run_jump_opt_after_gcse = 1;
673       timevar_pop (TV_PRE);
674     }
675   else
676     {
677       /* This function is being optimized for code size.
678          It does not make sense to run code hoisting unless we are optimizing
679          for code size -- it rarely makes programs faster, and can make
680          them bigger if we did partial redundancy elimination (when optimizing
681          for space, we don't run the partial redundancy algorithms).  */
682       timevar_push (TV_HOIST);
683       max_gcse_regno = max_reg_num ();
684       alloc_gcse_mem ();
685       one_code_hoisting_pass ();
686       timevar_pop (TV_HOIST);
687     }
688
689   free_gcse_mem ();
690
691   if (dump_file)
692     {
693       fprintf (dump_file, "\n");
694       fflush (dump_file);
695     }
696
697   obstack_free (&gcse_obstack, gcse_obstack_bottom);
698
699   /* Do the second const/copy propagation pass, including cprop into
700      conditional jumps.  */
701   if (dbg_cnt (cprop2))
702     {
703       max_gcse_regno = max_reg_num ();
704       alloc_gcse_mem ();
705
706       /* This time, go ahead and allow cprop to alter jumps.  */
707       timevar_push (TV_CPROP2);
708       changed = one_cprop_pass (2, true, true);
709       if (changed)
710         recompute_all_luids ();
711       timevar_pop (TV_CPROP2);
712       free_gcse_mem ();
713     }
714
715   if (dump_file)
716     {
717       fprintf (dump_file, "GCSE of %s: %d basic blocks, ",
718                current_function_name (), n_basic_blocks);
719       fprintf (dump_file, "pass 1, %d bytes\n\n", bytes_used);
720     }
721
722   obstack_free (&gcse_obstack, NULL);
723
724   /* We are finished with alias.
725      ??? Actually we recompute alias in store_motion.  */
726   end_alias_analysis ();
727
728   /* Run store motion.  */
729   if (optimize_function_for_speed_p (cfun) && flag_gcse_sm)
730     {
731       timevar_push (TV_LSM);
732       store_motion ();
733       timevar_pop (TV_LSM);
734     }
735
736   /* Record where pseudo-registers are set.  */
737   return run_jump_opt_after_gcse;
738 }
739 \f
740 /* Misc. utilities.  */
741
742 /* Nonzero for each mode that supports (set (reg) (reg)).
743    This is trivially true for integer and floating point values.
744    It may or may not be true for condition codes.  */
745 static char can_copy[(int) NUM_MACHINE_MODES];
746
747 /* Compute which modes support reg/reg copy operations.  */
748
749 static void
750 compute_can_copy (void)
751 {
752   int i;
753 #ifndef AVOID_CCMODE_COPIES
754   rtx reg, insn;
755 #endif
756   memset (can_copy, 0, NUM_MACHINE_MODES);
757
758   start_sequence ();
759   for (i = 0; i < NUM_MACHINE_MODES; i++)
760     if (GET_MODE_CLASS (i) == MODE_CC)
761       {
762 #ifdef AVOID_CCMODE_COPIES
763         can_copy[i] = 0;
764 #else
765         reg = gen_rtx_REG ((enum machine_mode) i, LAST_VIRTUAL_REGISTER + 1);
766         insn = emit_insn (gen_rtx_SET (VOIDmode, reg, reg));
767         if (recog (PATTERN (insn), insn, NULL) >= 0)
768           can_copy[i] = 1;
769 #endif
770       }
771     else
772       can_copy[i] = 1;
773
774   end_sequence ();
775 }
776
777 /* Returns whether the mode supports reg/reg copy operations.  */
778
779 bool
780 can_copy_p (enum machine_mode mode)
781 {
782   static bool can_copy_init_p = false;
783
784   if (! can_copy_init_p)
785     {
786       compute_can_copy ();
787       can_copy_init_p = true;
788     }
789
790   return can_copy[mode] != 0;
791 }
792
793 /* Recompute the DF LUIDs for all basic blocks.  If a sub-pass in this
794    file changes something, we have to recompute them for the next pass.
795    FIXME: If we would track which basic blocks we touch, we could
796           update LUIDs in only those basic blocks.  */
797
798 static void
799 recompute_all_luids (void)
800 {
801   basic_block bb;
802   FOR_EACH_BB (bb)
803     df_recompute_luids (bb);
804 }
805
806 \f
807 /* Cover function to xmalloc to record bytes allocated.  */
808
809 static void *
810 gmalloc (size_t size)
811 {
812   bytes_used += size;
813   return xmalloc (size);
814 }
815
816 /* Cover function to xcalloc to record bytes allocated.  */
817
818 static void *
819 gcalloc (size_t nelem, size_t elsize)
820 {
821   bytes_used += nelem * elsize;
822   return xcalloc (nelem, elsize);
823 }
824
825 /* Cover function to obstack_alloc.  */
826
827 static void *
828 gcse_alloc (unsigned long size)
829 {
830   bytes_used += size;
831   return obstack_alloc (&gcse_obstack, size);
832 }
833
834 /* Allocate memory for the reg/memory set tracking tables.
835    This is called at the start of each pass.  */
836
837 static void
838 alloc_gcse_mem (void)
839 {
840   /* Allocate vars to track sets of regs.  */
841   reg_set_bitmap = BITMAP_ALLOC (NULL);
842
843   /* Allocate array to keep a list of insns which modify memory in each
844      basic block.  */
845   modify_mem_list = GCNEWVEC (rtx, last_basic_block);
846   canon_modify_mem_list = GCNEWVEC (rtx, last_basic_block);
847   modify_mem_list_set = BITMAP_ALLOC (NULL);
848   blocks_with_calls = BITMAP_ALLOC (NULL);
849 }
850
851 /* Free memory allocated by alloc_gcse_mem.  */
852
853 static void
854 free_gcse_mem (void)
855 {
856   free_modify_mem_tables ();
857   BITMAP_FREE (modify_mem_list_set);
858   BITMAP_FREE (blocks_with_calls);
859 }
860 \f
861 /* Compute the local properties of each recorded expression.
862
863    Local properties are those that are defined by the block, irrespective of
864    other blocks.
865
866    An expression is transparent in a block if its operands are not modified
867    in the block.
868
869    An expression is computed (locally available) in a block if it is computed
870    at least once and expression would contain the same value if the
871    computation was moved to the end of the block.
872
873    An expression is locally anticipatable in a block if it is computed at
874    least once and expression would contain the same value if the computation
875    was moved to the beginning of the block.
876
877    We call this routine for cprop, pre and code hoisting.  They all compute
878    basically the same information and thus can easily share this code.
879
880    TRANSP, COMP, and ANTLOC are destination sbitmaps for recording local
881    properties.  If NULL, then it is not necessary to compute or record that
882    particular property.
883
884    TABLE controls which hash table to look at.  If it is  set hash table,
885    additionally, TRANSP is computed as ~TRANSP, since this is really cprop's
886    ABSALTERED.  */
887
888 static void
889 compute_local_properties (sbitmap *transp, sbitmap *comp, sbitmap *antloc,
890                           struct hash_table *table)
891 {
892   unsigned int i;
893
894   /* Initialize any bitmaps that were passed in.  */
895   if (transp)
896     {
897       if (table->set_p)
898         sbitmap_vector_zero (transp, last_basic_block);
899       else
900         sbitmap_vector_ones (transp, last_basic_block);
901     }
902
903   if (comp)
904     sbitmap_vector_zero (comp, last_basic_block);
905   if (antloc)
906     sbitmap_vector_zero (antloc, last_basic_block);
907
908   for (i = 0; i < table->size; i++)
909     {
910       struct expr *expr;
911
912       for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
913         {
914           int indx = expr->bitmap_index;
915           struct occr *occr;
916
917           /* The expression is transparent in this block if it is not killed.
918              We start by assuming all are transparent [none are killed], and
919              then reset the bits for those that are.  */
920           if (transp)
921             compute_transp (expr->expr, indx, transp, table->set_p);
922
923           /* The occurrences recorded in antic_occr are exactly those that
924              we want to set to nonzero in ANTLOC.  */
925           if (antloc)
926             for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
927               {
928                 SET_BIT (antloc[BLOCK_NUM (occr->insn)], indx);
929
930                 /* While we're scanning the table, this is a good place to
931                    initialize this.  */
932                 occr->deleted_p = 0;
933               }
934
935           /* The occurrences recorded in avail_occr are exactly those that
936              we want to set to nonzero in COMP.  */
937           if (comp)
938             for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
939               {
940                 SET_BIT (comp[BLOCK_NUM (occr->insn)], indx);
941
942                 /* While we're scanning the table, this is a good place to
943                    initialize this.  */
944                 occr->copied_p = 0;
945               }
946
947           /* While we're scanning the table, this is a good place to
948              initialize this.  */
949           expr->reaching_reg = 0;
950         }
951     }
952 }
953 \f
954 /* Hash table support.  */
955
956 struct reg_avail_info
957 {
958   basic_block last_bb;
959   int first_set;
960   int last_set;
961 };
962
963 static struct reg_avail_info *reg_avail_info;
964 static basic_block current_bb;
965
966
967 /* See whether X, the source of a set, is something we want to consider for
968    GCSE.  */
969
970 static int
971 want_to_gcse_p (rtx x)
972 {
973 #ifdef STACK_REGS
974   /* On register stack architectures, don't GCSE constants from the
975      constant pool, as the benefits are often swamped by the overhead
976      of shuffling the register stack between basic blocks.  */
977   if (IS_STACK_MODE (GET_MODE (x)))
978     x = avoid_constant_pool_reference (x);
979 #endif
980
981   switch (GET_CODE (x))
982     {
983     case REG:
984     case SUBREG:
985     case CONST_INT:
986     case CONST_DOUBLE:
987     case CONST_FIXED:
988     case CONST_VECTOR:
989     case CALL:
990       return 0;
991
992     default:
993       return can_assign_to_reg_p (x);
994     }
995 }
996
997 /* Used internally by can_assign_to_reg_p.  */
998
999 static GTY(()) rtx test_insn;
1000
1001 /* Return true if we can assign X to a pseudo register.  */
1002
1003 static bool
1004 can_assign_to_reg_p (rtx x)
1005 {
1006   int num_clobbers = 0;
1007   int icode;
1008
1009   /* If this is a valid operand, we are OK.  If it's VOIDmode, we aren't.  */
1010   if (general_operand (x, GET_MODE (x)))
1011     return 1;
1012   else if (GET_MODE (x) == VOIDmode)
1013     return 0;
1014
1015   /* Otherwise, check if we can make a valid insn from it.  First initialize
1016      our test insn if we haven't already.  */
1017   if (test_insn == 0)
1018     {
1019       test_insn
1020         = make_insn_raw (gen_rtx_SET (VOIDmode,
1021                                       gen_rtx_REG (word_mode,
1022                                                    FIRST_PSEUDO_REGISTER * 2),
1023                                       const0_rtx));
1024       NEXT_INSN (test_insn) = PREV_INSN (test_insn) = 0;
1025     }
1026
1027   /* Now make an insn like the one we would make when GCSE'ing and see if
1028      valid.  */
1029   PUT_MODE (SET_DEST (PATTERN (test_insn)), GET_MODE (x));
1030   SET_SRC (PATTERN (test_insn)) = x;
1031   return ((icode = recog (PATTERN (test_insn), test_insn, &num_clobbers)) >= 0
1032           && (num_clobbers == 0 || ! added_clobbers_hard_reg_p (icode)));
1033 }
1034
1035 /* Return nonzero if the operands of expression X are unchanged from the
1036    start of INSN's basic block up to but not including INSN (if AVAIL_P == 0),
1037    or from INSN to the end of INSN's basic block (if AVAIL_P != 0).  */
1038
1039 static int
1040 oprs_unchanged_p (const_rtx x, const_rtx insn, int avail_p)
1041 {
1042   int i, j;
1043   enum rtx_code code;
1044   const char *fmt;
1045
1046   if (x == 0)
1047     return 1;
1048
1049   code = GET_CODE (x);
1050   switch (code)
1051     {
1052     case REG:
1053       {
1054         struct reg_avail_info *info = &reg_avail_info[REGNO (x)];
1055
1056         if (info->last_bb != current_bb)
1057           return 1;
1058         if (avail_p)
1059           return info->last_set < DF_INSN_LUID (insn);
1060         else
1061           return info->first_set >= DF_INSN_LUID (insn);
1062       }
1063
1064     case MEM:
1065       if (load_killed_in_block_p (current_bb, DF_INSN_LUID (insn),
1066                                   x, avail_p))
1067         return 0;
1068       else
1069         return oprs_unchanged_p (XEXP (x, 0), insn, avail_p);
1070
1071     case PRE_DEC:
1072     case PRE_INC:
1073     case POST_DEC:
1074     case POST_INC:
1075     case PRE_MODIFY:
1076     case POST_MODIFY:
1077       return 0;
1078
1079     case PC:
1080     case CC0: /*FIXME*/
1081     case CONST:
1082     case CONST_INT:
1083     case CONST_DOUBLE:
1084     case CONST_FIXED:
1085     case CONST_VECTOR:
1086     case SYMBOL_REF:
1087     case LABEL_REF:
1088     case ADDR_VEC:
1089     case ADDR_DIFF_VEC:
1090       return 1;
1091
1092     default:
1093       break;
1094     }
1095
1096   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
1097     {
1098       if (fmt[i] == 'e')
1099         {
1100           /* If we are about to do the last recursive call needed at this
1101              level, change it into iteration.  This function is called enough
1102              to be worth it.  */
1103           if (i == 0)
1104             return oprs_unchanged_p (XEXP (x, i), insn, avail_p);
1105
1106           else if (! oprs_unchanged_p (XEXP (x, i), insn, avail_p))
1107             return 0;
1108         }
1109       else if (fmt[i] == 'E')
1110         for (j = 0; j < XVECLEN (x, i); j++)
1111           if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, avail_p))
1112             return 0;
1113     }
1114
1115   return 1;
1116 }
1117
1118 /* Used for communication between mems_conflict_for_gcse_p and
1119    load_killed_in_block_p.  Nonzero if mems_conflict_for_gcse_p finds a
1120    conflict between two memory references.  */
1121 static int gcse_mems_conflict_p;
1122
1123 /* Used for communication between mems_conflict_for_gcse_p and
1124    load_killed_in_block_p.  A memory reference for a load instruction,
1125    mems_conflict_for_gcse_p will see if a memory store conflicts with
1126    this memory load.  */
1127 static const_rtx gcse_mem_operand;
1128
1129 /* DEST is the output of an instruction.  If it is a memory reference, and
1130    possibly conflicts with the load found in gcse_mem_operand, then set
1131    gcse_mems_conflict_p to a nonzero value.  */
1132
1133 static void
1134 mems_conflict_for_gcse_p (rtx dest, const_rtx setter ATTRIBUTE_UNUSED,
1135                           void *data ATTRIBUTE_UNUSED)
1136 {
1137   while (GET_CODE (dest) == SUBREG
1138          || GET_CODE (dest) == ZERO_EXTRACT
1139          || GET_CODE (dest) == STRICT_LOW_PART)
1140     dest = XEXP (dest, 0);
1141
1142   /* If DEST is not a MEM, then it will not conflict with the load.  Note
1143      that function calls are assumed to clobber memory, but are handled
1144      elsewhere.  */
1145   if (! MEM_P (dest))
1146     return;
1147
1148   /* If we are setting a MEM in our list of specially recognized MEMs,
1149      don't mark as killed this time.  */
1150
1151   if (expr_equiv_p (dest, gcse_mem_operand) && pre_ldst_mems != NULL)
1152     {
1153       if (!find_rtx_in_ldst (dest))
1154         gcse_mems_conflict_p = 1;
1155       return;
1156     }
1157
1158   if (true_dependence (dest, GET_MODE (dest), gcse_mem_operand,
1159                        rtx_addr_varies_p))
1160     gcse_mems_conflict_p = 1;
1161 }
1162
1163 /* Return nonzero if the expression in X (a memory reference) is killed
1164    in block BB before or after the insn with the LUID in UID_LIMIT.
1165    AVAIL_P is nonzero for kills after UID_LIMIT, and zero for kills
1166    before UID_LIMIT.
1167
1168    To check the entire block, set UID_LIMIT to max_uid + 1 and
1169    AVAIL_P to 0.  */
1170
1171 static int
1172 load_killed_in_block_p (const_basic_block bb, int uid_limit, const_rtx x, int avail_p)
1173 {
1174   rtx list_entry = modify_mem_list[bb->index];
1175
1176   /* If this is a readonly then we aren't going to be changing it.  */
1177   if (MEM_READONLY_P (x))
1178     return 0;
1179
1180   while (list_entry)
1181     {
1182       rtx setter;
1183       /* Ignore entries in the list that do not apply.  */
1184       if ((avail_p
1185            && DF_INSN_LUID (XEXP (list_entry, 0)) < uid_limit)
1186           || (! avail_p
1187               && DF_INSN_LUID (XEXP (list_entry, 0)) > uid_limit))
1188         {
1189           list_entry = XEXP (list_entry, 1);
1190           continue;
1191         }
1192
1193       setter = XEXP (list_entry, 0);
1194
1195       /* If SETTER is a call everything is clobbered.  Note that calls
1196          to pure functions are never put on the list, so we need not
1197          worry about them.  */
1198       if (CALL_P (setter))
1199         return 1;
1200
1201       /* SETTER must be an INSN of some kind that sets memory.  Call
1202          note_stores to examine each hunk of memory that is modified.
1203
1204          The note_stores interface is pretty limited, so we have to
1205          communicate via global variables.  Yuk.  */
1206       gcse_mem_operand = x;
1207       gcse_mems_conflict_p = 0;
1208       note_stores (PATTERN (setter), mems_conflict_for_gcse_p, NULL);
1209       if (gcse_mems_conflict_p)
1210         return 1;
1211       list_entry = XEXP (list_entry, 1);
1212     }
1213   return 0;
1214 }
1215
1216 /* Return nonzero if the operands of expression X are unchanged from
1217    the start of INSN's basic block up to but not including INSN.  */
1218
1219 static int
1220 oprs_anticipatable_p (const_rtx x, const_rtx insn)
1221 {
1222   return oprs_unchanged_p (x, insn, 0);
1223 }
1224
1225 /* Return nonzero if the operands of expression X are unchanged from
1226    INSN to the end of INSN's basic block.  */
1227
1228 static int
1229 oprs_available_p (const_rtx x, const_rtx insn)
1230 {
1231   return oprs_unchanged_p (x, insn, 1);
1232 }
1233
1234 /* Hash expression X.
1235
1236    MODE is only used if X is a CONST_INT.  DO_NOT_RECORD_P is a boolean
1237    indicating if a volatile operand is found or if the expression contains
1238    something we don't want to insert in the table.  HASH_TABLE_SIZE is
1239    the current size of the hash table to be probed.  */
1240
1241 static unsigned int
1242 hash_expr (const_rtx x, enum machine_mode mode, int *do_not_record_p,
1243            int hash_table_size)
1244 {
1245   unsigned int hash;
1246
1247   *do_not_record_p = 0;
1248
1249   hash = hash_rtx (x, mode, do_not_record_p,
1250                    NULL,  /*have_reg_qty=*/false);
1251   return hash % hash_table_size;
1252 }
1253
1254 /* Hash a set of register REGNO.
1255
1256    Sets are hashed on the register that is set.  This simplifies the PRE copy
1257    propagation code.
1258
1259    ??? May need to make things more elaborate.  Later, as necessary.  */
1260
1261 static unsigned int
1262 hash_set (int regno, int hash_table_size)
1263 {
1264   unsigned int hash;
1265
1266   hash = regno;
1267   return hash % hash_table_size;
1268 }
1269
1270 /* Return nonzero if exp1 is equivalent to exp2.  */
1271
1272 static int
1273 expr_equiv_p (const_rtx x, const_rtx y)
1274 {
1275   return exp_equiv_p (x, y, 0, true);
1276 }
1277
1278 /* Insert expression X in INSN in the hash TABLE.
1279    If it is already present, record it as the last occurrence in INSN's
1280    basic block.
1281
1282    MODE is the mode of the value X is being stored into.
1283    It is only used if X is a CONST_INT.
1284
1285    ANTIC_P is nonzero if X is an anticipatable expression.
1286    AVAIL_P is nonzero if X is an available expression.  */
1287
1288 static void
1289 insert_expr_in_table (rtx x, enum machine_mode mode, rtx insn, int antic_p,
1290                       int avail_p, struct hash_table *table)
1291 {
1292   int found, do_not_record_p;
1293   unsigned int hash;
1294   struct expr *cur_expr, *last_expr = NULL;
1295   struct occr *antic_occr, *avail_occr;
1296
1297   hash = hash_expr (x, mode, &do_not_record_p, table->size);
1298
1299   /* Do not insert expression in table if it contains volatile operands,
1300      or if hash_expr determines the expression is something we don't want
1301      to or can't handle.  */
1302   if (do_not_record_p)
1303     return;
1304
1305   cur_expr = table->table[hash];
1306   found = 0;
1307
1308   while (cur_expr && 0 == (found = expr_equiv_p (cur_expr->expr, x)))
1309     {
1310       /* If the expression isn't found, save a pointer to the end of
1311          the list.  */
1312       last_expr = cur_expr;
1313       cur_expr = cur_expr->next_same_hash;
1314     }
1315
1316   if (! found)
1317     {
1318       cur_expr = GOBNEW (struct expr);
1319       bytes_used += sizeof (struct expr);
1320       if (table->table[hash] == NULL)
1321         /* This is the first pattern that hashed to this index.  */
1322         table->table[hash] = cur_expr;
1323       else
1324         /* Add EXPR to end of this hash chain.  */
1325         last_expr->next_same_hash = cur_expr;
1326
1327       /* Set the fields of the expr element.  */
1328       cur_expr->expr = x;
1329       cur_expr->bitmap_index = table->n_elems++;
1330       cur_expr->next_same_hash = NULL;
1331       cur_expr->antic_occr = NULL;
1332       cur_expr->avail_occr = NULL;
1333     }
1334
1335   /* Now record the occurrence(s).  */
1336   if (antic_p)
1337     {
1338       antic_occr = cur_expr->antic_occr;
1339
1340       if (antic_occr && BLOCK_NUM (antic_occr->insn) != BLOCK_NUM (insn))
1341         antic_occr = NULL;
1342
1343       if (antic_occr)
1344         /* Found another instance of the expression in the same basic block.
1345            Prefer the currently recorded one.  We want the first one in the
1346            block and the block is scanned from start to end.  */
1347         ; /* nothing to do */
1348       else
1349         {
1350           /* First occurrence of this expression in this basic block.  */
1351           antic_occr = GOBNEW (struct occr);
1352           bytes_used += sizeof (struct occr);
1353           antic_occr->insn = insn;
1354           antic_occr->next = cur_expr->antic_occr;
1355           antic_occr->deleted_p = 0;
1356           cur_expr->antic_occr = antic_occr;
1357         }
1358     }
1359
1360   if (avail_p)
1361     {
1362       avail_occr = cur_expr->avail_occr;
1363
1364       if (avail_occr && BLOCK_NUM (avail_occr->insn) == BLOCK_NUM (insn))
1365         {
1366           /* Found another instance of the expression in the same basic block.
1367              Prefer this occurrence to the currently recorded one.  We want
1368              the last one in the block and the block is scanned from start
1369              to end.  */
1370           avail_occr->insn = insn;
1371         }
1372       else
1373         {
1374           /* First occurrence of this expression in this basic block.  */
1375           avail_occr = GOBNEW (struct occr);
1376           bytes_used += sizeof (struct occr);
1377           avail_occr->insn = insn;
1378           avail_occr->next = cur_expr->avail_occr;
1379           avail_occr->deleted_p = 0;
1380           cur_expr->avail_occr = avail_occr;
1381         }
1382     }
1383 }
1384
1385 /* Insert pattern X in INSN in the hash table.
1386    X is a SET of a reg to either another reg or a constant.
1387    If it is already present, record it as the last occurrence in INSN's
1388    basic block.  */
1389
1390 static void
1391 insert_set_in_table (rtx x, rtx insn, struct hash_table *table)
1392 {
1393   int found;
1394   unsigned int hash;
1395   struct expr *cur_expr, *last_expr = NULL;
1396   struct occr *cur_occr;
1397
1398   gcc_assert (GET_CODE (x) == SET && REG_P (SET_DEST (x)));
1399
1400   hash = hash_set (REGNO (SET_DEST (x)), table->size);
1401
1402   cur_expr = table->table[hash];
1403   found = 0;
1404
1405   while (cur_expr && 0 == (found = expr_equiv_p (cur_expr->expr, x)))
1406     {
1407       /* If the expression isn't found, save a pointer to the end of
1408          the list.  */
1409       last_expr = cur_expr;
1410       cur_expr = cur_expr->next_same_hash;
1411     }
1412
1413   if (! found)
1414     {
1415       cur_expr = GOBNEW (struct expr);
1416       bytes_used += sizeof (struct expr);
1417       if (table->table[hash] == NULL)
1418         /* This is the first pattern that hashed to this index.  */
1419         table->table[hash] = cur_expr;
1420       else
1421         /* Add EXPR to end of this hash chain.  */
1422         last_expr->next_same_hash = cur_expr;
1423
1424       /* Set the fields of the expr element.
1425          We must copy X because it can be modified when copy propagation is
1426          performed on its operands.  */
1427       cur_expr->expr = copy_rtx (x);
1428       cur_expr->bitmap_index = table->n_elems++;
1429       cur_expr->next_same_hash = NULL;
1430       cur_expr->antic_occr = NULL;
1431       cur_expr->avail_occr = NULL;
1432     }
1433
1434   /* Now record the occurrence.  */
1435   cur_occr = cur_expr->avail_occr;
1436
1437   if (cur_occr && BLOCK_NUM (cur_occr->insn) == BLOCK_NUM (insn))
1438     {
1439       /* Found another instance of the expression in the same basic block.
1440          Prefer this occurrence to the currently recorded one.  We want
1441          the last one in the block and the block is scanned from start
1442          to end.  */
1443       cur_occr->insn = insn;
1444     }
1445   else
1446     {
1447       /* First occurrence of this expression in this basic block.  */
1448       cur_occr = GOBNEW (struct occr);
1449       bytes_used += sizeof (struct occr);
1450
1451           cur_occr->insn = insn;
1452           cur_occr->next = cur_expr->avail_occr;
1453           cur_occr->deleted_p = 0;
1454           cur_expr->avail_occr = cur_occr;
1455     }
1456 }
1457
1458 /* Determine whether the rtx X should be treated as a constant for
1459    the purposes of GCSE's constant propagation.  */
1460
1461 static bool
1462 gcse_constant_p (const_rtx x)
1463 {
1464   /* Consider a COMPARE of two integers constant.  */
1465   if (GET_CODE (x) == COMPARE
1466       && GET_CODE (XEXP (x, 0)) == CONST_INT
1467       && GET_CODE (XEXP (x, 1)) == CONST_INT)
1468     return true;
1469
1470   /* Consider a COMPARE of the same registers is a constant
1471      if they are not floating point registers.  */
1472   if (GET_CODE(x) == COMPARE
1473       && REG_P (XEXP (x, 0)) && REG_P (XEXP (x, 1))
1474       && REGNO (XEXP (x, 0)) == REGNO (XEXP (x, 1))
1475       && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
1476       && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 1))))
1477     return true;
1478
1479   return CONSTANT_P (x);
1480 }
1481
1482 /* Scan pattern PAT of INSN and add an entry to the hash TABLE (set or
1483    expression one).  */
1484
1485 static void
1486 hash_scan_set (rtx pat, rtx insn, struct hash_table *table)
1487 {
1488   rtx src = SET_SRC (pat);
1489   rtx dest = SET_DEST (pat);
1490   rtx note;
1491
1492   if (GET_CODE (src) == CALL)
1493     hash_scan_call (src, insn, table);
1494
1495   else if (REG_P (dest))
1496     {
1497       unsigned int regno = REGNO (dest);
1498       rtx tmp;
1499
1500       /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
1501
1502          This allows us to do a single GCSE pass and still eliminate
1503          redundant constants, addresses or other expressions that are
1504          constructed with multiple instructions.
1505
1506          However, keep the original SRC if INSN is a simple reg-reg move.  In
1507          In this case, there will almost always be a REG_EQUAL note on the
1508          insn that sets SRC.  By recording the REG_EQUAL value here as SRC
1509          for INSN, we miss copy propagation opportunities and we perform the
1510          same PRE GCSE operation repeatedly on the same REG_EQUAL value if we
1511          do more than one PRE GCSE pass.
1512
1513          Note that this does not impede profitable constant propagations.  We
1514          "look through" reg-reg sets in lookup_avail_set.  */
1515       note = find_reg_equal_equiv_note (insn);
1516       if (note != 0
1517           && REG_NOTE_KIND (note) == REG_EQUAL
1518           && !REG_P (src)
1519           && (table->set_p
1520               ? gcse_constant_p (XEXP (note, 0))
1521               : want_to_gcse_p (XEXP (note, 0))))
1522         src = XEXP (note, 0), pat = gen_rtx_SET (VOIDmode, dest, src);
1523
1524       /* Only record sets of pseudo-regs in the hash table.  */
1525       if (! table->set_p
1526           && regno >= FIRST_PSEUDO_REGISTER
1527           /* Don't GCSE something if we can't do a reg/reg copy.  */
1528           && can_copy_p (GET_MODE (dest))
1529           /* GCSE commonly inserts instruction after the insn.  We can't
1530              do that easily for EH_REGION notes so disable GCSE on these
1531              for now.  */
1532           && !find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1533           /* Is SET_SRC something we want to gcse?  */
1534           && want_to_gcse_p (src)
1535           /* Don't CSE a nop.  */
1536           && ! set_noop_p (pat)
1537           /* Don't GCSE if it has attached REG_EQUIV note.
1538              At this point this only function parameters should have
1539              REG_EQUIV notes and if the argument slot is used somewhere
1540              explicitly, it means address of parameter has been taken,
1541              so we should not extend the lifetime of the pseudo.  */
1542           && (note == NULL_RTX || ! MEM_P (XEXP (note, 0))))
1543         {
1544           /* An expression is not anticipatable if its operands are
1545              modified before this insn or if this is not the only SET in
1546              this insn.  The latter condition does not have to mean that
1547              SRC itself is not anticipatable, but we just will not be
1548              able to handle code motion of insns with multiple sets.  */
1549           int antic_p = oprs_anticipatable_p (src, insn)
1550                         && !multiple_sets (insn);
1551           /* An expression is not available if its operands are
1552              subsequently modified, including this insn.  It's also not
1553              available if this is a branch, because we can't insert
1554              a set after the branch.  */
1555           int avail_p = (oprs_available_p (src, insn)
1556                          && ! JUMP_P (insn));
1557
1558           insert_expr_in_table (src, GET_MODE (dest), insn, antic_p, avail_p, table);
1559         }
1560
1561       /* Record sets for constant/copy propagation.  */
1562       else if (table->set_p
1563                && regno >= FIRST_PSEUDO_REGISTER
1564                && ((REG_P (src)
1565                     && REGNO (src) >= FIRST_PSEUDO_REGISTER
1566                     && can_copy_p (GET_MODE (dest))
1567                     && REGNO (src) != regno)
1568                    || gcse_constant_p (src))
1569                /* A copy is not available if its src or dest is subsequently
1570                   modified.  Here we want to search from INSN+1 on, but
1571                   oprs_available_p searches from INSN on.  */
1572                && (insn == BB_END (BLOCK_FOR_INSN (insn))
1573                    || (tmp = next_nonnote_insn (insn)) == NULL_RTX
1574                    || BLOCK_FOR_INSN (tmp) != BLOCK_FOR_INSN (insn)
1575                    || oprs_available_p (pat, tmp)))
1576         insert_set_in_table (pat, insn, table);
1577     }
1578   /* In case of store we want to consider the memory value as available in
1579      the REG stored in that memory. This makes it possible to remove
1580      redundant loads from due to stores to the same location.  */
1581   else if (flag_gcse_las && REG_P (src) && MEM_P (dest))
1582       {
1583         unsigned int regno = REGNO (src);
1584
1585         /* Do not do this for constant/copy propagation.  */
1586         if (! table->set_p
1587             /* Only record sets of pseudo-regs in the hash table.  */
1588             && regno >= FIRST_PSEUDO_REGISTER
1589            /* Don't GCSE something if we can't do a reg/reg copy.  */
1590            && can_copy_p (GET_MODE (src))
1591            /* GCSE commonly inserts instruction after the insn.  We can't
1592               do that easily for EH_REGION notes so disable GCSE on these
1593               for now.  */
1594            && ! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1595            /* Is SET_DEST something we want to gcse?  */
1596            && want_to_gcse_p (dest)
1597            /* Don't CSE a nop.  */
1598            && ! set_noop_p (pat)
1599            /* Don't GCSE if it has attached REG_EQUIV note.
1600               At this point this only function parameters should have
1601               REG_EQUIV notes and if the argument slot is used somewhere
1602               explicitly, it means address of parameter has been taken,
1603               so we should not extend the lifetime of the pseudo.  */
1604            && ((note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) == 0
1605                || ! MEM_P (XEXP (note, 0))))
1606              {
1607                /* Stores are never anticipatable.  */
1608                int antic_p = 0;
1609                /* An expression is not available if its operands are
1610                   subsequently modified, including this insn.  It's also not
1611                   available if this is a branch, because we can't insert
1612                   a set after the branch.  */
1613                int avail_p = oprs_available_p (dest, insn)
1614                              && ! JUMP_P (insn);
1615
1616                /* Record the memory expression (DEST) in the hash table.  */
1617                insert_expr_in_table (dest, GET_MODE (dest), insn,
1618                                      antic_p, avail_p, table);
1619              }
1620       }
1621 }
1622
1623 static void
1624 hash_scan_clobber (rtx x ATTRIBUTE_UNUSED, rtx insn ATTRIBUTE_UNUSED,
1625                    struct hash_table *table ATTRIBUTE_UNUSED)
1626 {
1627   /* Currently nothing to do.  */
1628 }
1629
1630 static void
1631 hash_scan_call (rtx x ATTRIBUTE_UNUSED, rtx insn ATTRIBUTE_UNUSED,
1632                 struct hash_table *table ATTRIBUTE_UNUSED)
1633 {
1634   /* Currently nothing to do.  */
1635 }
1636
1637 /* Process INSN and add hash table entries as appropriate.
1638
1639    Only available expressions that set a single pseudo-reg are recorded.
1640
1641    Single sets in a PARALLEL could be handled, but it's an extra complication
1642    that isn't dealt with right now.  The trick is handling the CLOBBERs that
1643    are also in the PARALLEL.  Later.
1644
1645    If SET_P is nonzero, this is for the assignment hash table,
1646    otherwise it is for the expression hash table.  */
1647
1648 static void
1649 hash_scan_insn (rtx insn, struct hash_table *table)
1650 {
1651   rtx pat = PATTERN (insn);
1652   int i;
1653
1654   /* Pick out the sets of INSN and for other forms of instructions record
1655      what's been modified.  */
1656
1657   if (GET_CODE (pat) == SET)
1658     hash_scan_set (pat, insn, table);
1659   else if (GET_CODE (pat) == PARALLEL)
1660     for (i = 0; i < XVECLEN (pat, 0); i++)
1661       {
1662         rtx x = XVECEXP (pat, 0, i);
1663
1664         if (GET_CODE (x) == SET)
1665           hash_scan_set (x, insn, table);
1666         else if (GET_CODE (x) == CLOBBER)
1667           hash_scan_clobber (x, insn, table);
1668         else if (GET_CODE (x) == CALL)
1669           hash_scan_call (x, insn, table);
1670       }
1671
1672   else if (GET_CODE (pat) == CLOBBER)
1673     hash_scan_clobber (pat, insn, table);
1674   else if (GET_CODE (pat) == CALL)
1675     hash_scan_call (pat, insn, table);
1676 }
1677
1678 static void
1679 dump_hash_table (FILE *file, const char *name, struct hash_table *table)
1680 {
1681   int i;
1682   /* Flattened out table, so it's printed in proper order.  */
1683   struct expr **flat_table;
1684   unsigned int *hash_val;
1685   struct expr *expr;
1686
1687   flat_table = XCNEWVEC (struct expr *, table->n_elems);
1688   hash_val = XNEWVEC (unsigned int, table->n_elems);
1689
1690   for (i = 0; i < (int) table->size; i++)
1691     for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
1692       {
1693         flat_table[expr->bitmap_index] = expr;
1694         hash_val[expr->bitmap_index] = i;
1695       }
1696
1697   fprintf (file, "%s hash table (%d buckets, %d entries)\n",
1698            name, table->size, table->n_elems);
1699
1700   for (i = 0; i < (int) table->n_elems; i++)
1701     if (flat_table[i] != 0)
1702       {
1703         expr = flat_table[i];
1704         fprintf (file, "Index %d (hash value %d)\n  ",
1705                  expr->bitmap_index, hash_val[i]);
1706         print_rtl (file, expr->expr);
1707         fprintf (file, "\n");
1708       }
1709
1710   fprintf (file, "\n");
1711
1712   free (flat_table);
1713   free (hash_val);
1714 }
1715
1716 /* Record register first/last/block set information for REGNO in INSN.
1717
1718    first_set records the first place in the block where the register
1719    is set and is used to compute "anticipatability".
1720
1721    last_set records the last place in the block where the register
1722    is set and is used to compute "availability".
1723
1724    last_bb records the block for which first_set and last_set are
1725    valid, as a quick test to invalidate them.  */
1726
1727 static void
1728 record_last_reg_set_info (rtx insn, int regno)
1729 {
1730   struct reg_avail_info *info = &reg_avail_info[regno];
1731   int luid = DF_INSN_LUID (insn);
1732
1733   info->last_set = luid;
1734   if (info->last_bb != current_bb)
1735     {
1736       info->last_bb = current_bb;
1737       info->first_set = luid;
1738     }
1739 }
1740
1741
1742 /* Record all of the canonicalized MEMs of record_last_mem_set_info's insn.
1743    Note we store a pair of elements in the list, so they have to be
1744    taken off pairwise.  */
1745
1746 static void
1747 canon_list_insert (rtx dest ATTRIBUTE_UNUSED, const_rtx unused1 ATTRIBUTE_UNUSED,
1748                    void * v_insn)
1749 {
1750   rtx dest_addr, insn;
1751   int bb;
1752
1753   while (GET_CODE (dest) == SUBREG
1754       || GET_CODE (dest) == ZERO_EXTRACT
1755       || GET_CODE (dest) == STRICT_LOW_PART)
1756     dest = XEXP (dest, 0);
1757
1758   /* If DEST is not a MEM, then it will not conflict with a load.  Note
1759      that function calls are assumed to clobber memory, but are handled
1760      elsewhere.  */
1761
1762   if (! MEM_P (dest))
1763     return;
1764
1765   dest_addr = get_addr (XEXP (dest, 0));
1766   dest_addr = canon_rtx (dest_addr);
1767   insn = (rtx) v_insn;
1768   bb = BLOCK_NUM (insn);
1769
1770   canon_modify_mem_list[bb] =
1771     alloc_EXPR_LIST (VOIDmode, dest_addr, canon_modify_mem_list[bb]);
1772   canon_modify_mem_list[bb] =
1773     alloc_EXPR_LIST (VOIDmode, dest, canon_modify_mem_list[bb]);
1774 }
1775
1776 /* Record memory modification information for INSN.  We do not actually care
1777    about the memory location(s) that are set, or even how they are set (consider
1778    a CALL_INSN).  We merely need to record which insns modify memory.  */
1779
1780 static void
1781 record_last_mem_set_info (rtx insn)
1782 {
1783   int bb = BLOCK_NUM (insn);
1784
1785   /* load_killed_in_block_p will handle the case of calls clobbering
1786      everything.  */
1787   modify_mem_list[bb] = alloc_INSN_LIST (insn, modify_mem_list[bb]);
1788   bitmap_set_bit (modify_mem_list_set, bb);
1789
1790   if (CALL_P (insn))
1791     {
1792       /* Note that traversals of this loop (other than for free-ing)
1793          will break after encountering a CALL_INSN.  So, there's no
1794          need to insert a pair of items, as canon_list_insert does.  */
1795       canon_modify_mem_list[bb] =
1796         alloc_INSN_LIST (insn, canon_modify_mem_list[bb]);
1797       bitmap_set_bit (blocks_with_calls, bb);
1798     }
1799   else
1800     note_stores (PATTERN (insn), canon_list_insert, (void*) insn);
1801 }
1802
1803 /* Called from compute_hash_table via note_stores to handle one
1804    SET or CLOBBER in an insn.  DATA is really the instruction in which
1805    the SET is taking place.  */
1806
1807 static void
1808 record_last_set_info (rtx dest, const_rtx setter ATTRIBUTE_UNUSED, void *data)
1809 {
1810   rtx last_set_insn = (rtx) data;
1811
1812   if (GET_CODE (dest) == SUBREG)
1813     dest = SUBREG_REG (dest);
1814
1815   if (REG_P (dest))
1816     record_last_reg_set_info (last_set_insn, REGNO (dest));
1817   else if (MEM_P (dest)
1818            /* Ignore pushes, they clobber nothing.  */
1819            && ! push_operand (dest, GET_MODE (dest)))
1820     record_last_mem_set_info (last_set_insn);
1821 }
1822
1823 /* Top level function to create an expression or assignment hash table.
1824
1825    Expression entries are placed in the hash table if
1826    - they are of the form (set (pseudo-reg) src),
1827    - src is something we want to perform GCSE on,
1828    - none of the operands are subsequently modified in the block
1829
1830    Assignment entries are placed in the hash table if
1831    - they are of the form (set (pseudo-reg) src),
1832    - src is something we want to perform const/copy propagation on,
1833    - none of the operands or target are subsequently modified in the block
1834
1835    Currently src must be a pseudo-reg or a const_int.
1836
1837    TABLE is the table computed.  */
1838
1839 static void
1840 compute_hash_table_work (struct hash_table *table)
1841 {
1842   unsigned int i;
1843
1844   /* re-Cache any INSN_LIST nodes we have allocated.  */
1845   clear_modify_mem_tables ();
1846   /* Some working arrays used to track first and last set in each block.  */
1847   reg_avail_info = GNEWVEC (struct reg_avail_info, max_gcse_regno);
1848
1849   for (i = 0; i < max_gcse_regno; ++i)
1850     reg_avail_info[i].last_bb = NULL;
1851
1852   FOR_EACH_BB (current_bb)
1853     {
1854       rtx insn;
1855       unsigned int regno;
1856
1857       /* First pass over the instructions records information used to
1858          determine when registers and memory are first and last set.  */
1859       FOR_BB_INSNS (current_bb, insn)
1860         {
1861           if (! INSN_P (insn))
1862             continue;
1863
1864           if (CALL_P (insn))
1865             {
1866               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1867                 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
1868                   record_last_reg_set_info (insn, regno);
1869
1870               mark_call (insn);
1871             }
1872
1873           note_stores (PATTERN (insn), record_last_set_info, insn);
1874         }
1875
1876       /* Insert implicit sets in the hash table.  */
1877       if (table->set_p
1878           && implicit_sets[current_bb->index] != NULL_RTX)
1879         hash_scan_set (implicit_sets[current_bb->index],
1880                        BB_HEAD (current_bb), table);
1881
1882       /* The next pass builds the hash table.  */
1883       FOR_BB_INSNS (current_bb, insn)
1884         if (INSN_P (insn))
1885           hash_scan_insn (insn, table);
1886     }
1887
1888   free (reg_avail_info);
1889   reg_avail_info = NULL;
1890 }
1891
1892 /* Allocate space for the set/expr hash TABLE.
1893    N_INSNS is the number of instructions in the function.
1894    It is used to determine the number of buckets to use.
1895    SET_P determines whether set or expression table will
1896    be created.  */
1897
1898 static void
1899 alloc_hash_table (int n_insns, struct hash_table *table, int set_p)
1900 {
1901   int n;
1902
1903   table->size = n_insns / 4;
1904   if (table->size < 11)
1905     table->size = 11;
1906
1907   /* Attempt to maintain efficient use of hash table.
1908      Making it an odd number is simplest for now.
1909      ??? Later take some measurements.  */
1910   table->size |= 1;
1911   n = table->size * sizeof (struct expr *);
1912   table->table = GNEWVAR (struct expr *, n);
1913   table->set_p = set_p;
1914 }
1915
1916 /* Free things allocated by alloc_hash_table.  */
1917
1918 static void
1919 free_hash_table (struct hash_table *table)
1920 {
1921   free (table->table);
1922 }
1923
1924 /* Compute the hash TABLE for doing copy/const propagation or
1925    expression hash table.  */
1926
1927 static void
1928 compute_hash_table (struct hash_table *table)
1929 {
1930   /* Initialize count of number of entries in hash table.  */
1931   table->n_elems = 0;
1932   memset (table->table, 0, table->size * sizeof (struct expr *));
1933
1934   compute_hash_table_work (table);
1935 }
1936 \f
1937 /* Expression tracking support.  */
1938
1939 /* Lookup REGNO in the set TABLE.  The result is a pointer to the
1940    table entry, or NULL if not found.  */
1941
1942 static struct expr *
1943 lookup_set (unsigned int regno, struct hash_table *table)
1944 {
1945   unsigned int hash = hash_set (regno, table->size);
1946   struct expr *expr;
1947
1948   expr = table->table[hash];
1949
1950   while (expr && REGNO (SET_DEST (expr->expr)) != regno)
1951     expr = expr->next_same_hash;
1952
1953   return expr;
1954 }
1955
1956 /* Return the next entry for REGNO in list EXPR.  */
1957
1958 static struct expr *
1959 next_set (unsigned int regno, struct expr *expr)
1960 {
1961   do
1962     expr = expr->next_same_hash;
1963   while (expr && REGNO (SET_DEST (expr->expr)) != regno);
1964
1965   return expr;
1966 }
1967
1968 /* Like free_INSN_LIST_list or free_EXPR_LIST_list, except that the node
1969    types may be mixed.  */
1970
1971 static void
1972 free_insn_expr_list_list (rtx *listp)
1973 {
1974   rtx list, next;
1975
1976   for (list = *listp; list ; list = next)
1977     {
1978       next = XEXP (list, 1);
1979       if (GET_CODE (list) == EXPR_LIST)
1980         free_EXPR_LIST_node (list);
1981       else
1982         free_INSN_LIST_node (list);
1983     }
1984
1985   *listp = NULL;
1986 }
1987
1988 /* Clear canon_modify_mem_list and modify_mem_list tables.  */
1989 static void
1990 clear_modify_mem_tables (void)
1991 {
1992   unsigned i;
1993   bitmap_iterator bi;
1994
1995   EXECUTE_IF_SET_IN_BITMAP (modify_mem_list_set, 0, i, bi)
1996     {
1997       free_INSN_LIST_list (modify_mem_list + i);
1998       free_insn_expr_list_list (canon_modify_mem_list + i);
1999     }
2000   bitmap_clear (modify_mem_list_set);
2001   bitmap_clear (blocks_with_calls);
2002 }
2003
2004 /* Release memory used by modify_mem_list_set.  */
2005
2006 static void
2007 free_modify_mem_tables (void)
2008 {
2009   clear_modify_mem_tables ();
2010   free (modify_mem_list);
2011   free (canon_modify_mem_list);
2012   modify_mem_list = 0;
2013   canon_modify_mem_list = 0;
2014 }
2015
2016 /* Reset tables used to keep track of what's still available [since the
2017    start of the block].  */
2018
2019 static void
2020 reset_opr_set_tables (void)
2021 {
2022   /* Maintain a bitmap of which regs have been set since beginning of
2023      the block.  */
2024   CLEAR_REG_SET (reg_set_bitmap);
2025
2026   /* Also keep a record of the last instruction to modify memory.
2027      For now this is very trivial, we only record whether any memory
2028      location has been modified.  */
2029   clear_modify_mem_tables ();
2030 }
2031
2032 /* Return nonzero if the operands of X are not set before INSN in
2033    INSN's basic block.  */
2034
2035 static int
2036 oprs_not_set_p (const_rtx x, const_rtx insn)
2037 {
2038   int i, j;
2039   enum rtx_code code;
2040   const char *fmt;
2041
2042   if (x == 0)
2043     return 1;
2044
2045   code = GET_CODE (x);
2046   switch (code)
2047     {
2048     case PC:
2049     case CC0:
2050     case CONST:
2051     case CONST_INT:
2052     case CONST_DOUBLE:
2053     case CONST_FIXED:
2054     case CONST_VECTOR:
2055     case SYMBOL_REF:
2056     case LABEL_REF:
2057     case ADDR_VEC:
2058     case ADDR_DIFF_VEC:
2059       return 1;
2060
2061     case MEM:
2062       if (load_killed_in_block_p (BLOCK_FOR_INSN (insn),
2063                                   DF_INSN_LUID (insn), x, 0))
2064         return 0;
2065       else
2066         return oprs_not_set_p (XEXP (x, 0), insn);
2067
2068     case REG:
2069       return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
2070
2071     default:
2072       break;
2073     }
2074
2075   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
2076     {
2077       if (fmt[i] == 'e')
2078         {
2079           /* If we are about to do the last recursive call
2080              needed at this level, change it into iteration.
2081              This function is called enough to be worth it.  */
2082           if (i == 0)
2083             return oprs_not_set_p (XEXP (x, i), insn);
2084
2085           if (! oprs_not_set_p (XEXP (x, i), insn))
2086             return 0;
2087         }
2088       else if (fmt[i] == 'E')
2089         for (j = 0; j < XVECLEN (x, i); j++)
2090           if (! oprs_not_set_p (XVECEXP (x, i, j), insn))
2091             return 0;
2092     }
2093
2094   return 1;
2095 }
2096
2097 /* Mark things set by a CALL.  */
2098
2099 static void
2100 mark_call (rtx insn)
2101 {
2102   if (! RTL_CONST_OR_PURE_CALL_P (insn))
2103     record_last_mem_set_info (insn);
2104 }
2105
2106 /* Mark things set by a SET.  */
2107
2108 static void
2109 mark_set (rtx pat, rtx insn)
2110 {
2111   rtx dest = SET_DEST (pat);
2112
2113   while (GET_CODE (dest) == SUBREG
2114          || GET_CODE (dest) == ZERO_EXTRACT
2115          || GET_CODE (dest) == STRICT_LOW_PART)
2116     dest = XEXP (dest, 0);
2117
2118   if (REG_P (dest))
2119     SET_REGNO_REG_SET (reg_set_bitmap, REGNO (dest));
2120   else if (MEM_P (dest))
2121     record_last_mem_set_info (insn);
2122
2123   if (GET_CODE (SET_SRC (pat)) == CALL)
2124     mark_call (insn);
2125 }
2126
2127 /* Record things set by a CLOBBER.  */
2128
2129 static void
2130 mark_clobber (rtx pat, rtx insn)
2131 {
2132   rtx clob = XEXP (pat, 0);
2133
2134   while (GET_CODE (clob) == SUBREG || GET_CODE (clob) == STRICT_LOW_PART)
2135     clob = XEXP (clob, 0);
2136
2137   if (REG_P (clob))
2138     SET_REGNO_REG_SET (reg_set_bitmap, REGNO (clob));
2139   else
2140     record_last_mem_set_info (insn);
2141 }
2142
2143 /* Record things set by INSN.
2144    This data is used by oprs_not_set_p.  */
2145
2146 static void
2147 mark_oprs_set (rtx insn)
2148 {
2149   rtx pat = PATTERN (insn);
2150   int i;
2151
2152   if (GET_CODE (pat) == SET)
2153     mark_set (pat, insn);
2154   else if (GET_CODE (pat) == PARALLEL)
2155     for (i = 0; i < XVECLEN (pat, 0); i++)
2156       {
2157         rtx x = XVECEXP (pat, 0, i);
2158
2159         if (GET_CODE (x) == SET)
2160           mark_set (x, insn);
2161         else if (GET_CODE (x) == CLOBBER)
2162           mark_clobber (x, insn);
2163         else if (GET_CODE (x) == CALL)
2164           mark_call (insn);
2165       }
2166
2167   else if (GET_CODE (pat) == CLOBBER)
2168     mark_clobber (pat, insn);
2169   else if (GET_CODE (pat) == CALL)
2170     mark_call (insn);
2171 }
2172
2173 \f
2174 /* Compute copy/constant propagation working variables.  */
2175
2176 /* Local properties of assignments.  */
2177 static sbitmap *cprop_pavloc;
2178 static sbitmap *cprop_absaltered;
2179
2180 /* Global properties of assignments (computed from the local properties).  */
2181 static sbitmap *cprop_avin;
2182 static sbitmap *cprop_avout;
2183
2184 /* Allocate vars used for copy/const propagation.  N_BLOCKS is the number of
2185    basic blocks.  N_SETS is the number of sets.  */
2186
2187 static void
2188 alloc_cprop_mem (int n_blocks, int n_sets)
2189 {
2190   cprop_pavloc = sbitmap_vector_alloc (n_blocks, n_sets);
2191   cprop_absaltered = sbitmap_vector_alloc (n_blocks, n_sets);
2192
2193   cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
2194   cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
2195 }
2196
2197 /* Free vars used by copy/const propagation.  */
2198
2199 static void
2200 free_cprop_mem (void)
2201 {
2202   sbitmap_vector_free (cprop_pavloc);
2203   sbitmap_vector_free (cprop_absaltered);
2204   sbitmap_vector_free (cprop_avin);
2205   sbitmap_vector_free (cprop_avout);
2206 }
2207
2208 /* For each block, compute whether X is transparent.  X is either an
2209    expression or an assignment [though we don't care which, for this context
2210    an assignment is treated as an expression].  For each block where an
2211    element of X is modified, set (SET_P == 1) or reset (SET_P == 0) the INDX
2212    bit in BMAP.  */
2213
2214 static void
2215 compute_transp (const_rtx x, int indx, sbitmap *bmap, int set_p)
2216 {
2217   int i, j;
2218   enum rtx_code code;
2219   const char *fmt;
2220
2221   /* repeat is used to turn tail-recursion into iteration since GCC
2222      can't do it when there's no return value.  */
2223  repeat:
2224
2225   if (x == 0)
2226     return;
2227
2228   code = GET_CODE (x);
2229   switch (code)
2230     {
2231     case REG:
2232       if (set_p)
2233         {
2234           df_ref def;
2235           for (def = DF_REG_DEF_CHAIN (REGNO (x));
2236                def;
2237                def = DF_REF_NEXT_REG (def))
2238             SET_BIT (bmap[DF_REF_BB (def)->index], indx);
2239         }
2240       else
2241         {
2242           df_ref def;
2243           for (def = DF_REG_DEF_CHAIN (REGNO (x));
2244                def;
2245                def = DF_REF_NEXT_REG (def))
2246             RESET_BIT (bmap[DF_REF_BB (def)->index], indx);
2247         }
2248
2249       return;
2250
2251     case MEM:
2252       if (! MEM_READONLY_P (x))
2253         {
2254           bitmap_iterator bi;
2255           unsigned bb_index;
2256
2257           /* First handle all the blocks with calls.  We don't need to
2258              do any list walking for them.  */
2259           EXECUTE_IF_SET_IN_BITMAP (blocks_with_calls, 0, bb_index, bi)
2260             {
2261               if (set_p)
2262                 SET_BIT (bmap[bb_index], indx);
2263               else
2264                 RESET_BIT (bmap[bb_index], indx);
2265             }
2266
2267             /* Now iterate over the blocks which have memory modifications
2268                but which do not have any calls.  */
2269             EXECUTE_IF_AND_COMPL_IN_BITMAP (modify_mem_list_set, 
2270                                             blocks_with_calls,
2271                                             0, bb_index, bi)
2272               {
2273                 rtx list_entry = canon_modify_mem_list[bb_index];
2274
2275                 while (list_entry)
2276                   {
2277                     rtx dest, dest_addr;
2278
2279                     /* LIST_ENTRY must be an INSN of some kind that sets memory.
2280                        Examine each hunk of memory that is modified.  */
2281
2282                     dest = XEXP (list_entry, 0);
2283                     list_entry = XEXP (list_entry, 1);
2284                     dest_addr = XEXP (list_entry, 0);
2285
2286                     if (canon_true_dependence (dest, GET_MODE (dest), dest_addr,
2287                                                x, NULL_RTX, rtx_addr_varies_p))
2288                       {
2289                         if (set_p)
2290                           SET_BIT (bmap[bb_index], indx);
2291                         else
2292                           RESET_BIT (bmap[bb_index], indx);
2293                         break;
2294                       }
2295                     list_entry = XEXP (list_entry, 1);
2296                   }
2297               }
2298         }
2299
2300       x = XEXP (x, 0);
2301       goto repeat;
2302
2303     case PC:
2304     case CC0: /*FIXME*/
2305     case CONST:
2306     case CONST_INT:
2307     case CONST_DOUBLE:
2308     case CONST_FIXED:
2309     case CONST_VECTOR:
2310     case SYMBOL_REF:
2311     case LABEL_REF:
2312     case ADDR_VEC:
2313     case ADDR_DIFF_VEC:
2314       return;
2315
2316     default:
2317       break;
2318     }
2319
2320   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
2321     {
2322       if (fmt[i] == 'e')
2323         {
2324           /* If we are about to do the last recursive call
2325              needed at this level, change it into iteration.
2326              This function is called enough to be worth it.  */
2327           if (i == 0)
2328             {
2329               x = XEXP (x, i);
2330               goto repeat;
2331             }
2332
2333           compute_transp (XEXP (x, i), indx, bmap, set_p);
2334         }
2335       else if (fmt[i] == 'E')
2336         for (j = 0; j < XVECLEN (x, i); j++)
2337           compute_transp (XVECEXP (x, i, j), indx, bmap, set_p);
2338     }
2339 }
2340
2341 /* Top level routine to do the dataflow analysis needed by copy/const
2342    propagation.  */
2343
2344 static void
2345 compute_cprop_data (void)
2346 {
2347   compute_local_properties (cprop_absaltered, cprop_pavloc, NULL, &set_hash_table);
2348   compute_available (cprop_pavloc, cprop_absaltered,
2349                      cprop_avout, cprop_avin);
2350 }
2351 \f
2352 /* Copy/constant propagation.  */
2353
2354 /* Maximum number of register uses in an insn that we handle.  */
2355 #define MAX_USES 8
2356
2357 /* Table of uses found in an insn.
2358    Allocated statically to avoid alloc/free complexity and overhead.  */
2359 static struct reg_use reg_use_table[MAX_USES];
2360
2361 /* Index into `reg_use_table' while building it.  */
2362 static int reg_use_count;
2363
2364 /* Set up a list of register numbers used in INSN.  The found uses are stored
2365    in `reg_use_table'.  `reg_use_count' is initialized to zero before entry,
2366    and contains the number of uses in the table upon exit.
2367
2368    ??? If a register appears multiple times we will record it multiple times.
2369    This doesn't hurt anything but it will slow things down.  */
2370
2371 static void
2372 find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
2373 {
2374   int i, j;
2375   enum rtx_code code;
2376   const char *fmt;
2377   rtx x = *xptr;
2378
2379   /* repeat is used to turn tail-recursion into iteration since GCC
2380      can't do it when there's no return value.  */
2381  repeat:
2382   if (x == 0)
2383     return;
2384
2385   code = GET_CODE (x);
2386   if (REG_P (x))
2387     {
2388       if (reg_use_count == MAX_USES)
2389         return;
2390
2391       reg_use_table[reg_use_count].reg_rtx = x;
2392       reg_use_count++;
2393     }
2394
2395   /* Recursively scan the operands of this expression.  */
2396
2397   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
2398     {
2399       if (fmt[i] == 'e')
2400         {
2401           /* If we are about to do the last recursive call
2402              needed at this level, change it into iteration.
2403              This function is called enough to be worth it.  */
2404           if (i == 0)
2405             {
2406               x = XEXP (x, 0);
2407               goto repeat;
2408             }
2409
2410           find_used_regs (&XEXP (x, i), data);
2411         }
2412       else if (fmt[i] == 'E')
2413         for (j = 0; j < XVECLEN (x, i); j++)
2414           find_used_regs (&XVECEXP (x, i, j), data);
2415     }
2416 }
2417
2418 /* Try to replace all non-SET_DEST occurrences of FROM in INSN with TO.
2419    Returns nonzero is successful.  */
2420
2421 static int
2422 try_replace_reg (rtx from, rtx to, rtx insn)
2423 {
2424   rtx note = find_reg_equal_equiv_note (insn);
2425   rtx src = 0;
2426   int success = 0;
2427   rtx set = single_set (insn);
2428
2429   /* Usually we substitute easy stuff, so we won't copy everything.
2430      We however need to take care to not duplicate non-trivial CONST
2431      expressions.  */
2432   to = copy_rtx (to);
2433
2434   validate_replace_src_group (from, to, insn);
2435   if (num_changes_pending () && apply_change_group ())
2436     success = 1;
2437
2438   /* Try to simplify SET_SRC if we have substituted a constant.  */
2439   if (success && set && CONSTANT_P (to))
2440     {
2441       src = simplify_rtx (SET_SRC (set));
2442
2443       if (src)
2444         validate_change (insn, &SET_SRC (set), src, 0);
2445     }
2446
2447   /* If there is already a REG_EQUAL note, update the expression in it
2448      with our replacement.  */
2449   if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
2450     set_unique_reg_note (insn, REG_EQUAL,
2451                          simplify_replace_rtx (XEXP (note, 0), from,
2452                          copy_rtx (to)));
2453   if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
2454     {
2455       /* If above failed and this is a single set, try to simplify the source of
2456          the set given our substitution.  We could perhaps try this for multiple
2457          SETs, but it probably won't buy us anything.  */
2458       src = simplify_replace_rtx (SET_SRC (set), from, to);
2459
2460       if (!rtx_equal_p (src, SET_SRC (set))
2461           && validate_change (insn, &SET_SRC (set), src, 0))
2462         success = 1;
2463
2464       /* If we've failed to do replacement, have a single SET, don't already
2465          have a note, and have no special SET, add a REG_EQUAL note to not
2466          lose information.  */
2467       if (!success && note == 0 && set != 0
2468           && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
2469           && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
2470         note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
2471     }
2472
2473   /* REG_EQUAL may get simplified into register.
2474      We don't allow that. Remove that note. This code ought
2475      not to happen, because previous code ought to synthesize
2476      reg-reg move, but be on the safe side.  */
2477   if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
2478     remove_note (insn, note);
2479
2480   return success;
2481 }
2482
2483 /* Find a set of REGNOs that are available on entry to INSN's block.  Returns
2484    NULL no such set is found.  */
2485
2486 static struct expr *
2487 find_avail_set (int regno, rtx insn)
2488 {
2489   /* SET1 contains the last set found that can be returned to the caller for
2490      use in a substitution.  */
2491   struct expr *set1 = 0;
2492
2493   /* Loops are not possible here.  To get a loop we would need two sets
2494      available at the start of the block containing INSN.  i.e. we would
2495      need two sets like this available at the start of the block:
2496
2497        (set (reg X) (reg Y))
2498        (set (reg Y) (reg X))
2499
2500      This can not happen since the set of (reg Y) would have killed the
2501      set of (reg X) making it unavailable at the start of this block.  */
2502   while (1)
2503     {
2504       rtx src;
2505       struct expr *set = lookup_set (regno, &set_hash_table);
2506
2507       /* Find a set that is available at the start of the block
2508          which contains INSN.  */
2509       while (set)
2510         {
2511           if (TEST_BIT (cprop_avin[BLOCK_NUM (insn)], set->bitmap_index))
2512             break;
2513           set = next_set (regno, set);
2514         }
2515
2516       /* If no available set was found we've reached the end of the
2517          (possibly empty) copy chain.  */
2518       if (set == 0)
2519         break;
2520
2521       gcc_assert (GET_CODE (set->expr) == SET);
2522
2523       src = SET_SRC (set->expr);
2524
2525       /* We know the set is available.
2526          Now check that SRC is ANTLOC (i.e. none of the source operands
2527          have changed since the start of the block).
2528
2529          If the source operand changed, we may still use it for the next
2530          iteration of this loop, but we may not use it for substitutions.  */
2531
2532       if (gcse_constant_p (src) || oprs_not_set_p (src, insn))
2533         set1 = set;
2534
2535       /* If the source of the set is anything except a register, then
2536          we have reached the end of the copy chain.  */
2537       if (! REG_P (src))
2538         break;
2539
2540       /* Follow the copy chain, i.e. start another iteration of the loop
2541          and see if we have an available copy into SRC.  */
2542       regno = REGNO (src);
2543     }
2544
2545   /* SET1 holds the last set that was available and anticipatable at
2546      INSN.  */
2547   return set1;
2548 }
2549
2550 /* Subroutine of cprop_insn that tries to propagate constants into
2551    JUMP_INSNS.  JUMP must be a conditional jump.  If SETCC is non-NULL
2552    it is the instruction that immediately precedes JUMP, and must be a
2553    single SET of a register.  FROM is what we will try to replace,
2554    SRC is the constant we will try to substitute for it.  Returns nonzero
2555    if a change was made.  */
2556
2557 static int
2558 cprop_jump (basic_block bb, rtx setcc, rtx jump, rtx from, rtx src)
2559 {
2560   rtx new_rtx, set_src, note_src;
2561   rtx set = pc_set (jump);
2562   rtx note = find_reg_equal_equiv_note (jump);
2563
2564   if (note)
2565     {
2566       note_src = XEXP (note, 0);
2567       if (GET_CODE (note_src) == EXPR_LIST)
2568         note_src = NULL_RTX;
2569     }
2570   else note_src = NULL_RTX;
2571
2572   /* Prefer REG_EQUAL notes except those containing EXPR_LISTs.  */
2573   set_src = note_src ? note_src : SET_SRC (set);
2574
2575   /* First substitute the SETCC condition into the JUMP instruction,
2576      then substitute that given values into this expanded JUMP.  */
2577   if (setcc != NULL_RTX
2578       && !modified_between_p (from, setcc, jump)
2579       && !modified_between_p (src, setcc, jump))
2580     {
2581       rtx setcc_src;
2582       rtx setcc_set = single_set (setcc);
2583       rtx setcc_note = find_reg_equal_equiv_note (setcc);
2584       setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
2585                 ? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
2586       set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
2587                                       setcc_src);
2588     }
2589   else
2590     setcc = NULL_RTX;
2591
2592   new_rtx = simplify_replace_rtx (set_src, from, src);
2593
2594   /* If no simplification can be made, then try the next register.  */
2595   if (rtx_equal_p (new_rtx, SET_SRC (set)))
2596     return 0;
2597
2598   /* If this is now a no-op delete it, otherwise this must be a valid insn.  */
2599   if (new_rtx == pc_rtx)
2600     delete_insn (jump);
2601   else
2602     {
2603       /* Ensure the value computed inside the jump insn to be equivalent
2604          to one computed by setcc.  */
2605       if (setcc && modified_in_p (new_rtx, setcc))
2606         return 0;
2607       if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
2608         {
2609           /* When (some) constants are not valid in a comparison, and there
2610              are two registers to be replaced by constants before the entire
2611              comparison can be folded into a constant, we need to keep
2612              intermediate information in REG_EQUAL notes.  For targets with
2613              separate compare insns, such notes are added by try_replace_reg.
2614              When we have a combined compare-and-branch instruction, however,
2615              we need to attach a note to the branch itself to make this
2616              optimization work.  */
2617
2618           if (!rtx_equal_p (new_rtx, note_src))
2619             set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
2620           return 0;
2621         }
2622
2623       /* Remove REG_EQUAL note after simplification.  */
2624       if (note_src)
2625         remove_note (jump, note);
2626      }
2627
2628 #ifdef HAVE_cc0
2629   /* Delete the cc0 setter.  */
2630   if (setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
2631     delete_insn (setcc);
2632 #endif
2633
2634   run_jump_opt_after_gcse = 1;
2635
2636   global_const_prop_count++;
2637   if (dump_file != NULL)
2638     {
2639       fprintf (dump_file,
2640                "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with constant ",
2641                REGNO (from), INSN_UID (jump));
2642       print_rtl (dump_file, src);
2643       fprintf (dump_file, "\n");
2644     }
2645   purge_dead_edges (bb);
2646
2647   /* If a conditional jump has been changed into unconditional jump, remove
2648      the jump and make the edge fallthru - this is always called in
2649      cfglayout mode.  */
2650   if (new_rtx != pc_rtx && simplejump_p (jump))
2651     {
2652       edge e;
2653       edge_iterator ei;
2654
2655       for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ei_next (&ei))
2656         if (e->dest != EXIT_BLOCK_PTR
2657             && BB_HEAD (e->dest) == JUMP_LABEL (jump))
2658           {
2659             e->flags |= EDGE_FALLTHRU;
2660             break;
2661           }
2662       delete_insn (jump);
2663     }
2664
2665   return 1;
2666 }
2667
2668 static bool
2669 constprop_register (rtx insn, rtx from, rtx to, bool alter_jumps)
2670 {
2671   rtx sset;
2672
2673   /* Check for reg or cc0 setting instructions followed by
2674      conditional branch instructions first.  */
2675   if (alter_jumps
2676       && (sset = single_set (insn)) != NULL
2677       && NEXT_INSN (insn)
2678       && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
2679     {
2680       rtx dest = SET_DEST (sset);
2681       if ((REG_P (dest) || CC0_P (dest))
2682           && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn), from, to))
2683         return 1;
2684     }
2685
2686   /* Handle normal insns next.  */
2687   if (NONJUMP_INSN_P (insn)
2688       && try_replace_reg (from, to, insn))
2689     return 1;
2690
2691   /* Try to propagate a CONST_INT into a conditional jump.
2692      We're pretty specific about what we will handle in this
2693      code, we can extend this as necessary over time.
2694
2695      Right now the insn in question must look like
2696      (set (pc) (if_then_else ...))  */
2697   else if (alter_jumps && any_condjump_p (insn) && onlyjump_p (insn))
2698     return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, to);
2699   return 0;
2700 }
2701
2702 /* Perform constant and copy propagation on INSN.
2703    The result is nonzero if a change was made.  */
2704
2705 static int
2706 cprop_insn (rtx insn, int alter_jumps)
2707 {
2708   struct reg_use *reg_used;
2709   int changed = 0;
2710   rtx note;
2711
2712   if (!INSN_P (insn))
2713     return 0;
2714
2715   reg_use_count = 0;
2716   note_uses (&PATTERN (insn), find_used_regs, NULL);
2717
2718   note = find_reg_equal_equiv_note (insn);
2719
2720   /* We may win even when propagating constants into notes.  */
2721   if (note)
2722     find_used_regs (&XEXP (note, 0), NULL);
2723
2724   for (reg_used = &reg_use_table[0]; reg_use_count > 0;
2725        reg_used++, reg_use_count--)
2726     {
2727       unsigned int regno = REGNO (reg_used->reg_rtx);
2728       rtx pat, src;
2729       struct expr *set;
2730
2731       /* Ignore registers created by GCSE.
2732          We do this because ...  */
2733       if (regno >= max_gcse_regno)
2734         continue;
2735
2736       /* If the register has already been set in this block, there's
2737          nothing we can do.  */
2738       if (! oprs_not_set_p (reg_used->reg_rtx, insn))
2739         continue;
2740
2741       /* Find an assignment that sets reg_used and is available
2742          at the start of the block.  */
2743       set = find_avail_set (regno, insn);
2744       if (! set)
2745         continue;
2746
2747       pat = set->expr;
2748       /* ??? We might be able to handle PARALLELs.  Later.  */
2749       gcc_assert (GET_CODE (pat) == SET);
2750
2751       src = SET_SRC (pat);
2752
2753       /* Constant propagation.  */
2754       if (gcse_constant_p (src))
2755         {
2756           if (constprop_register (insn, reg_used->reg_rtx, src, alter_jumps))
2757             {
2758               changed = 1;
2759               global_const_prop_count++;
2760               if (dump_file != NULL)
2761                 {
2762                   fprintf (dump_file, "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
2763                   fprintf (dump_file, "insn %d with constant ", INSN_UID (insn));
2764                   print_rtl (dump_file, src);
2765                   fprintf (dump_file, "\n");
2766                 }
2767               if (INSN_DELETED_P (insn))
2768                 return 1;
2769             }
2770         }
2771       else if (REG_P (src)
2772                && REGNO (src) >= FIRST_PSEUDO_REGISTER
2773                && REGNO (src) != regno)
2774         {
2775           if (try_replace_reg (reg_used->reg_rtx, src, insn))
2776             {
2777               changed = 1;
2778               global_copy_prop_count++;
2779               if (dump_file != NULL)
2780                 {
2781                   fprintf (dump_file, "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
2782                            regno, INSN_UID (insn));
2783                   fprintf (dump_file, " with reg %d\n", REGNO (src));
2784                 }
2785
2786               /* The original insn setting reg_used may or may not now be
2787                  deletable.  We leave the deletion to flow.  */
2788               /* FIXME: If it turns out that the insn isn't deletable,
2789                  then we may have unnecessarily extended register lifetimes
2790                  and made things worse.  */
2791             }
2792         }
2793     }
2794
2795   return changed;
2796 }
2797
2798 /* Like find_used_regs, but avoid recording uses that appear in
2799    input-output contexts such as zero_extract or pre_dec.  This
2800    restricts the cases we consider to those for which local cprop
2801    can legitimately make replacements.  */
2802
2803 static void
2804 local_cprop_find_used_regs (rtx *xptr, void *data)
2805 {
2806   rtx x = *xptr;
2807
2808   if (x == 0)
2809     return;
2810
2811   switch (GET_CODE (x))
2812     {
2813     case ZERO_EXTRACT:
2814     case SIGN_EXTRACT:
2815     case STRICT_LOW_PART:
2816       return;
2817
2818     case PRE_DEC:
2819     case PRE_INC:
2820     case POST_DEC:
2821     case POST_INC:
2822     case PRE_MODIFY:
2823     case POST_MODIFY:
2824       /* Can only legitimately appear this early in the context of
2825          stack pushes for function arguments, but handle all of the
2826          codes nonetheless.  */
2827       return;
2828
2829     case SUBREG:
2830       /* Setting a subreg of a register larger than word_mode leaves
2831          the non-written words unchanged.  */
2832       if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) > BITS_PER_WORD)
2833         return;
2834       break;
2835
2836     default:
2837       break;
2838     }
2839
2840   find_used_regs (xptr, data);
2841 }
2842
2843 /* Try to perform local const/copy propagation on X in INSN.
2844    If ALTER_JUMPS is false, changing jump insns is not allowed.  */
2845
2846 static bool
2847 do_local_cprop (rtx x, rtx insn, bool alter_jumps)
2848 {
2849   rtx newreg = NULL, newcnst = NULL;
2850
2851   /* Rule out USE instructions and ASM statements as we don't want to
2852      change the hard registers mentioned.  */
2853   if (REG_P (x)
2854       && (REGNO (x) >= FIRST_PSEUDO_REGISTER
2855           || (GET_CODE (PATTERN (insn)) != USE
2856               && asm_noperands (PATTERN (insn)) < 0)))
2857     {
2858       cselib_val *val = cselib_lookup (x, GET_MODE (x), 0);
2859       struct elt_loc_list *l;
2860
2861       if (!val)
2862         return false;
2863       for (l = val->locs; l; l = l->next)
2864         {
2865           rtx this_rtx = l->loc;
2866           rtx note;
2867
2868           if (gcse_constant_p (this_rtx))
2869             newcnst = this_rtx;
2870           if (REG_P (this_rtx) && REGNO (this_rtx) >= FIRST_PSEUDO_REGISTER
2871               /* Don't copy propagate if it has attached REG_EQUIV note.
2872                  At this point this only function parameters should have
2873                  REG_EQUIV notes and if the argument slot is used somewhere
2874                  explicitly, it means address of parameter has been taken,
2875                  so we should not extend the lifetime of the pseudo.  */
2876               && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
2877                   || ! MEM_P (XEXP (note, 0))))
2878             newreg = this_rtx;
2879         }
2880       if (newcnst && constprop_register (insn, x, newcnst, alter_jumps))
2881         {
2882           if (dump_file != NULL)
2883             {
2884               fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
2885                        REGNO (x));
2886               fprintf (dump_file, "insn %d with constant ",
2887                        INSN_UID (insn));
2888               print_rtl (dump_file, newcnst);
2889               fprintf (dump_file, "\n");
2890             }
2891           local_const_prop_count++;
2892           return true;
2893         }
2894       else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
2895         {
2896           if (dump_file != NULL)
2897             {
2898               fprintf (dump_file,
2899                        "LOCAL COPY-PROP: Replacing reg %d in insn %d",
2900                        REGNO (x), INSN_UID (insn));
2901               fprintf (dump_file, " with reg %d\n", REGNO (newreg));
2902             }
2903           local_copy_prop_count++;
2904           return true;
2905         }
2906     }
2907   return false;
2908 }
2909
2910 /* Do local const/copy propagation (i.e. within each basic block).
2911    If ALTER_JUMPS is true, allow propagating into jump insns, which
2912    could modify the CFG.  */
2913
2914 static void
2915 local_cprop_pass (bool alter_jumps)
2916 {
2917   basic_block bb;
2918   rtx insn;
2919   struct reg_use *reg_used;
2920   bool changed = false;
2921
2922   cselib_init (false);
2923   FOR_EACH_BB (bb)
2924     {
2925       FOR_BB_INSNS (bb, insn)
2926         {
2927           if (INSN_P (insn))
2928             {
2929               rtx note = find_reg_equal_equiv_note (insn);
2930               do
2931                 {
2932                   reg_use_count = 0;
2933                   note_uses (&PATTERN (insn), local_cprop_find_used_regs,
2934                              NULL);
2935                   if (note)
2936                     local_cprop_find_used_regs (&XEXP (note, 0), NULL);
2937
2938                   for (reg_used = &reg_use_table[0]; reg_use_count > 0;
2939                        reg_used++, reg_use_count--)
2940                     {
2941                       if (do_local_cprop (reg_used->reg_rtx, insn, alter_jumps))
2942                         {
2943                           changed = true;
2944                           break;
2945                         }
2946                     }
2947                   if (INSN_DELETED_P (insn))
2948                     break;
2949                 }
2950               while (reg_use_count);
2951             }
2952           cselib_process_insn (insn);
2953         }
2954
2955       /* Forget everything at the end of a basic block.  */
2956       cselib_clear_table ();
2957     }
2958
2959   cselib_finish ();
2960
2961   /* Global analysis may get into infinite loops for unreachable blocks.  */
2962   if (changed && alter_jumps)
2963     delete_unreachable_blocks ();
2964 }
2965
2966 /* Forward propagate copies.  This includes copies and constants.  Return
2967    nonzero if a change was made.  */
2968
2969 static int
2970 cprop (int alter_jumps)
2971 {
2972   int changed;
2973   basic_block bb;
2974   rtx insn;
2975
2976   /* Note we start at block 1.  */
2977   if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
2978     {
2979       if (dump_file != NULL)
2980         fprintf (dump_file, "\n");
2981       return 0;
2982     }
2983
2984   changed = 0;
2985   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb, EXIT_BLOCK_PTR, next_bb)
2986     {
2987       /* Reset tables used to keep track of what's still valid [since the
2988          start of the block].  */
2989       reset_opr_set_tables ();
2990
2991       FOR_BB_INSNS (bb, insn)
2992         if (INSN_P (insn))
2993           {
2994             changed |= cprop_insn (insn, alter_jumps);
2995
2996             /* Keep track of everything modified by this insn.  */
2997             /* ??? Need to be careful w.r.t. mods done to INSN.  Don't
2998                call mark_oprs_set if we turned the insn into a NOTE.  */
2999             if (! NOTE_P (insn))
3000               mark_oprs_set (insn);
3001           }
3002     }
3003
3004   if (dump_file != NULL)
3005     fprintf (dump_file, "\n");
3006
3007   return changed;
3008 }
3009
3010 /* Similar to get_condition, only the resulting condition must be
3011    valid at JUMP, instead of at EARLIEST.
3012
3013    This differs from noce_get_condition in ifcvt.c in that we prefer not to
3014    settle for the condition variable in the jump instruction being integral.
3015    We prefer to be able to record the value of a user variable, rather than
3016    the value of a temporary used in a condition.  This could be solved by
3017    recording the value of *every* register scanned by canonicalize_condition,
3018    but this would require some code reorganization.  */
3019
3020 rtx
3021 fis_get_condition (rtx jump)
3022 {
3023   return get_condition (jump, NULL, false, true);
3024 }
3025
3026 /* Check the comparison COND to see if we can safely form an implicit set from
3027    it.  COND is either an EQ or NE comparison.  */
3028
3029 static bool
3030 implicit_set_cond_p (const_rtx cond)
3031 {
3032   const enum machine_mode mode = GET_MODE (XEXP (cond, 0));
3033   const_rtx cst = XEXP (cond, 1);
3034
3035   /* We can't perform this optimization if either operand might be or might
3036      contain a signed zero.  */
3037   if (HONOR_SIGNED_ZEROS (mode))
3038     {
3039       /* It is sufficient to check if CST is or contains a zero.  We must
3040          handle float, complex, and vector.  If any subpart is a zero, then
3041          the optimization can't be performed.  */
3042       /* ??? The complex and vector checks are not implemented yet.  We just
3043          always return zero for them.  */
3044       if (GET_CODE (cst) == CONST_DOUBLE)
3045         {
3046           REAL_VALUE_TYPE d;
3047           REAL_VALUE_FROM_CONST_DOUBLE (d, cst);
3048           if (REAL_VALUES_EQUAL (d, dconst0))
3049             return 0;
3050         }
3051       else
3052         return 0;
3053     }
3054
3055   return gcse_constant_p (cst);
3056 }
3057
3058 /* Find the implicit sets of a function.  An "implicit set" is a constraint
3059    on the value of a variable, implied by a conditional jump.  For example,
3060    following "if (x == 2)", the then branch may be optimized as though the
3061    conditional performed an "explicit set", in this example, "x = 2".  This
3062    function records the set patterns that are implicit at the start of each
3063    basic block.  */
3064
3065 static void
3066 find_implicit_sets (void)
3067 {
3068   basic_block bb, dest;
3069   unsigned int count;
3070   rtx cond, new_rtx;
3071
3072   count = 0;
3073   FOR_EACH_BB (bb)
3074     /* Check for more than one successor.  */
3075     if (EDGE_COUNT (bb->succs) > 1)
3076       {
3077         cond = fis_get_condition (BB_END (bb));
3078
3079         if (cond
3080             && (GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
3081             && REG_P (XEXP (cond, 0))
3082             && REGNO (XEXP (cond, 0)) >= FIRST_PSEUDO_REGISTER
3083             && implicit_set_cond_p (cond))
3084           {
3085             dest = GET_CODE (cond) == EQ ? BRANCH_EDGE (bb)->dest
3086                                          : FALLTHRU_EDGE (bb)->dest;
3087
3088             if (dest && single_pred_p (dest)
3089                 && dest != EXIT_BLOCK_PTR)
3090               {
3091                 new_rtx = gen_rtx_SET (VOIDmode, XEXP (cond, 0),
3092                                              XEXP (cond, 1));
3093                 implicit_sets[dest->index] = new_rtx;
3094                 if (dump_file)
3095                   {
3096                     fprintf(dump_file, "Implicit set of reg %d in ",
3097                             REGNO (XEXP (cond, 0)));
3098                     fprintf(dump_file, "basic block %d\n", dest->index);
3099                   }
3100                 count++;
3101               }
3102           }
3103       }
3104
3105   if (dump_file)
3106     fprintf (dump_file, "Found %d implicit sets\n", count);
3107 }
3108
3109 /* Perform one copy/constant propagation pass.
3110    PASS is the pass count.  If CPROP_JUMPS is true, perform constant
3111    propagation into conditional jumps.  If BYPASS_JUMPS is true,
3112    perform conditional jump bypassing optimizations.  */
3113
3114 static int
3115 one_cprop_pass (int pass, bool cprop_jumps, bool bypass_jumps)
3116 {
3117   int changed = 0;
3118
3119   global_const_prop_count = local_const_prop_count = 0;
3120   global_copy_prop_count = local_copy_prop_count = 0;
3121
3122   if (cprop_jumps)
3123     local_cprop_pass (cprop_jumps);
3124
3125   /* Determine implicit sets.  */
3126   implicit_sets = XCNEWVEC (rtx, last_basic_block);
3127   find_implicit_sets ();
3128
3129   alloc_hash_table (get_max_uid (), &set_hash_table, 1);
3130   compute_hash_table (&set_hash_table);
3131
3132   /* Free implicit_sets before peak usage.  */
3133   free (implicit_sets);
3134   implicit_sets = NULL;
3135
3136   if (dump_file)
3137     dump_hash_table (dump_file, "SET", &set_hash_table);
3138   if (set_hash_table.n_elems > 0)
3139     {
3140       alloc_cprop_mem (last_basic_block, set_hash_table.n_elems);
3141       compute_cprop_data ();
3142       changed = cprop (cprop_jumps);
3143       if (bypass_jumps)
3144         changed |= bypass_conditional_jumps ();
3145       free_cprop_mem ();
3146     }
3147
3148   free_hash_table (&set_hash_table);
3149
3150   if (dump_file)
3151     {
3152       fprintf (dump_file, "CPROP of %s, pass %d: %d bytes needed, ",
3153                current_function_name (), pass, bytes_used);
3154       fprintf (dump_file, "%d local const props, %d local copy props, ",
3155                local_const_prop_count, local_copy_prop_count);
3156       fprintf (dump_file, "%d global const props, %d global copy props\n\n",
3157                global_const_prop_count, global_copy_prop_count);
3158     }
3159   /* Global analysis may get into infinite loops for unreachable blocks.  */
3160   if (changed && cprop_jumps)
3161     delete_unreachable_blocks ();
3162
3163   return changed;
3164 }
3165 \f
3166 /* Bypass conditional jumps.  */
3167
3168 /* The value of last_basic_block at the beginning of the jump_bypass
3169    pass.  The use of redirect_edge_and_branch_force may introduce new
3170    basic blocks, but the data flow analysis is only valid for basic
3171    block indices less than bypass_last_basic_block.  */
3172
3173 static int bypass_last_basic_block;
3174
3175 /* Find a set of REGNO to a constant that is available at the end of basic
3176    block BB.  Returns NULL if no such set is found.  Based heavily upon
3177    find_avail_set.  */
3178
3179 static struct expr *
3180 find_bypass_set (int regno, int bb)
3181 {
3182   struct expr *result = 0;
3183
3184   for (;;)
3185     {
3186       rtx src;
3187       struct expr *set = lookup_set (regno, &set_hash_table);
3188
3189       while (set)
3190         {
3191           if (TEST_BIT (cprop_avout[bb], set->bitmap_index))
3192             break;
3193           set = next_set (regno, set);
3194         }
3195
3196       if (set == 0)
3197         break;
3198
3199       gcc_assert (GET_CODE (set->expr) == SET);
3200
3201       src = SET_SRC (set->expr);
3202       if (gcse_constant_p (src))
3203         result = set;
3204
3205       if (! REG_P (src))
3206         break;
3207
3208       regno = REGNO (src);
3209     }
3210   return result;
3211 }
3212
3213
3214 /* Subroutine of bypass_block that checks whether a pseudo is killed by
3215    any of the instructions inserted on an edge.  Jump bypassing places
3216    condition code setters on CFG edges using insert_insn_on_edge.  This
3217    function is required to check that our data flow analysis is still
3218    valid prior to commit_edge_insertions.  */
3219
3220 static bool
3221 reg_killed_on_edge (const_rtx reg, const_edge e)
3222 {
3223   rtx insn;
3224
3225   for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
3226     if (INSN_P (insn) && reg_set_p (reg, insn))
3227       return true;
3228
3229   return false;
3230 }
3231
3232 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
3233    basic block BB which has more than one predecessor.  If not NULL, SETCC
3234    is the first instruction of BB, which is immediately followed by JUMP_INSN
3235    JUMP.  Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
3236    Returns nonzero if a change was made.
3237
3238    During the jump bypassing pass, we may place copies of SETCC instructions
3239    on CFG edges.  The following routine must be careful to pay attention to
3240    these inserted insns when performing its transformations.  */
3241
3242 static int
3243 bypass_block (basic_block bb, rtx setcc, rtx jump)
3244 {
3245   rtx insn, note;
3246   edge e, edest;
3247   int i, change;
3248   int may_be_loop_header;
3249   unsigned removed_p;
3250   edge_iterator ei;
3251
3252   insn = (setcc != NULL) ? setcc : jump;
3253
3254   /* Determine set of register uses in INSN.  */
3255   reg_use_count = 0;
3256   note_uses (&PATTERN (insn), find_used_regs, NULL);
3257   note = find_reg_equal_equiv_note (insn);
3258   if (note)
3259     find_used_regs (&XEXP (note, 0), NULL);
3260
3261   may_be_loop_header = false;
3262   FOR_EACH_EDGE (e, ei, bb->preds)
3263     if (e->flags & EDGE_DFS_BACK)
3264       {
3265         may_be_loop_header = true;
3266         break;
3267       }
3268
3269   change = 0;
3270   for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
3271     {
3272       removed_p = 0;
3273           
3274       if (e->flags & EDGE_COMPLEX)
3275         {
3276           ei_next (&ei);
3277           continue;
3278         }
3279
3280       /* We can't redirect edges from new basic blocks.  */
3281       if (e->src->index >= bypass_last_basic_block)
3282         {
3283           ei_next (&ei);
3284           continue;
3285         }
3286
3287       /* The irreducible loops created by redirecting of edges entering the
3288          loop from outside would decrease effectiveness of some of the following
3289          optimizations, so prevent this.  */
3290       if (may_be_loop_header
3291           && !(e->flags & EDGE_DFS_BACK))
3292         {
3293           ei_next (&ei);
3294           continue;
3295         }
3296
3297       for (i = 0; i < reg_use_count; i++)
3298         {
3299           struct reg_use *reg_used = &reg_use_table[i];
3300           unsigned int regno = REGNO (reg_used->reg_rtx);
3301           basic_block dest, old_dest;
3302           struct expr *set;
3303           rtx src, new_rtx;
3304
3305           if (regno >= max_gcse_regno)
3306             continue;
3307
3308           set = find_bypass_set (regno, e->src->index);
3309
3310           if (! set)
3311             continue;
3312
3313           /* Check the data flow is valid after edge insertions.  */
3314           if (e->insns.r && reg_killed_on_edge (reg_used->reg_rtx, e))
3315             continue;
3316
3317           src = SET_SRC (pc_set (jump));
3318
3319           if (setcc != NULL)
3320               src = simplify_replace_rtx (src,
3321                                           SET_DEST (PATTERN (setcc)),
3322                                           SET_SRC (PATTERN (setcc)));
3323
3324           new_rtx = simplify_replace_rtx (src, reg_used->reg_rtx,
3325                                       SET_SRC (set->expr));
3326
3327           /* Jump bypassing may have already placed instructions on
3328              edges of the CFG.  We can't bypass an outgoing edge that
3329              has instructions associated with it, as these insns won't
3330              get executed if the incoming edge is redirected.  */
3331
3332           if (new_rtx == pc_rtx)
3333             {
3334               edest = FALLTHRU_EDGE (bb);
3335               dest = edest->insns.r ? NULL : edest->dest;
3336             }
3337           else if (GET_CODE (new_rtx) == LABEL_REF)
3338             {
3339               dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
3340               /* Don't bypass edges containing instructions.  */
3341               edest = find_edge (bb, dest);
3342               if (edest && edest->insns.r)
3343                 dest = NULL;
3344             }
3345           else
3346             dest = NULL;
3347
3348           /* Avoid unification of the edge with other edges from original
3349              branch.  We would end up emitting the instruction on "both"
3350              edges.  */
3351
3352           if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
3353               && find_edge (e->src, dest))
3354             dest = NULL;
3355
3356           old_dest = e->dest;
3357           if (dest != NULL
3358               && dest != old_dest
3359               && dest != EXIT_BLOCK_PTR)
3360             {
3361               redirect_edge_and_branch_force (e, dest);
3362
3363               /* Copy the register setter to the redirected edge.
3364                  Don't copy CC0 setters, as CC0 is dead after jump.  */
3365               if (setcc)
3366                 {
3367                   rtx pat = PATTERN (setcc);
3368                   if (!CC0_P (SET_DEST (pat)))
3369                     insert_insn_on_edge (copy_insn (pat), e);
3370                 }
3371
3372               if (dump_file != NULL)
3373                 {
3374                   fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
3375                                       "in jump_insn %d equals constant ",
3376                            regno, INSN_UID (jump));
3377                   print_rtl (dump_file, SET_SRC (set->expr));
3378                   fprintf (dump_file, "\nBypass edge from %d->%d to %d\n",
3379                            e->src->index, old_dest->index, dest->index);
3380                 }
3381               change = 1;
3382               removed_p = 1;
3383               break;
3384             }
3385         }
3386       if (!removed_p)
3387         ei_next (&ei);
3388     }
3389   return change;
3390 }
3391
3392 /* Find basic blocks with more than one predecessor that only contain a
3393    single conditional jump.  If the result of the comparison is known at
3394    compile-time from any incoming edge, redirect that edge to the
3395    appropriate target.  Returns nonzero if a change was made.
3396
3397    This function is now mis-named, because we also handle indirect jumps.  */
3398
3399 static int
3400 bypass_conditional_jumps (void)
3401 {
3402   basic_block bb;
3403   int changed;
3404   rtx setcc;
3405   rtx insn;
3406   rtx dest;
3407
3408   /* Note we start at block 1.  */
3409   if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
3410     return 0;
3411
3412   bypass_last_basic_block = last_basic_block;
3413   mark_dfs_back_edges ();
3414
3415   changed = 0;
3416   FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb,
3417                   EXIT_BLOCK_PTR, next_bb)
3418     {
3419       /* Check for more than one predecessor.  */
3420       if (!single_pred_p (bb))
3421         {
3422           setcc = NULL_RTX;
3423           FOR_BB_INSNS (bb, insn)
3424             if (NONJUMP_INSN_P (insn))
3425               {
3426                 if (setcc)
3427                   break;
3428                 if (GET_CODE (PATTERN (insn)) != SET)
3429                   break;
3430
3431                 dest = SET_DEST (PATTERN (insn));
3432                 if (REG_P (dest) || CC0_P (dest))
3433                   setcc = insn;
3434                 else
3435                   break;
3436               }
3437             else if (JUMP_P (insn))
3438               {
3439                 if ((any_condjump_p (insn) || computed_jump_p (insn))
3440                     && onlyjump_p (insn))
3441                   changed |= bypass_block (bb, setcc, insn);
3442                 break;
3443               }
3444             else if (INSN_P (insn))
3445               break;
3446         }
3447     }
3448
3449   /* If we bypassed any register setting insns, we inserted a
3450      copy on the redirected edge.  These need to be committed.  */
3451   if (changed)
3452     commit_edge_insertions ();
3453
3454   return changed;
3455 }
3456 \f
3457 /* Compute PRE+LCM working variables.  */
3458
3459 /* Local properties of expressions.  */
3460 /* Nonzero for expressions that are transparent in the block.  */
3461 static sbitmap *transp;
3462
3463 /* Nonzero for expressions that are transparent at the end of the block.
3464    This is only zero for expressions killed by abnormal critical edge
3465    created by a calls.  */
3466 static sbitmap *transpout;
3467
3468 /* Nonzero for expressions that are computed (available) in the block.  */
3469 static sbitmap *comp;
3470
3471 /* Nonzero for expressions that are locally anticipatable in the block.  */
3472 static sbitmap *antloc;
3473
3474 /* Nonzero for expressions where this block is an optimal computation
3475    point.  */
3476 static sbitmap *pre_optimal;
3477
3478 /* Nonzero for expressions which are redundant in a particular block.  */
3479 static sbitmap *pre_redundant;
3480
3481 /* Nonzero for expressions which should be inserted on a specific edge.  */
3482 static sbitmap *pre_insert_map;
3483
3484 /* Nonzero for expressions which should be deleted in a specific block.  */
3485 static sbitmap *pre_delete_map;
3486
3487 /* Contains the edge_list returned by pre_edge_lcm.  */
3488 static struct edge_list *edge_list;
3489
3490 /* Allocate vars used for PRE analysis.  */
3491
3492 static void
3493 alloc_pre_mem (int n_blocks, int n_exprs)
3494 {
3495   transp = sbitmap_vector_alloc (n_blocks, n_exprs);
3496   comp = sbitmap_vector_alloc (n_blocks, n_exprs);
3497   antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
3498
3499   pre_optimal = NULL;
3500   pre_redundant = NULL;
3501   pre_insert_map = NULL;
3502   pre_delete_map = NULL;
3503   ae_kill = sbitmap_vector_alloc (n_blocks, n_exprs);
3504
3505   /* pre_insert and pre_delete are allocated later.  */
3506 }
3507
3508 /* Free vars used for PRE analysis.  */
3509
3510 static void
3511 free_pre_mem (void)
3512 {
3513   sbitmap_vector_free (transp);
3514   sbitmap_vector_free (comp);
3515
3516   /* ANTLOC and AE_KILL are freed just after pre_lcm finishes.  */
3517
3518   if (pre_optimal)
3519     sbitmap_vector_free (pre_optimal);
3520   if (pre_redundant)
3521     sbitmap_vector_free (pre_redundant);
3522   if (pre_insert_map)
3523     sbitmap_vector_free (pre_insert_map);
3524   if (pre_delete_map)
3525     sbitmap_vector_free (pre_delete_map);
3526
3527   transp = comp = NULL;
3528   pre_optimal = pre_redundant = pre_insert_map = pre_delete_map = NULL;
3529 }
3530
3531 /* Top level routine to do the dataflow analysis needed by PRE.  */
3532
3533 static void
3534 compute_pre_data (void)
3535 {
3536   sbitmap trapping_expr;
3537   basic_block bb;
3538   unsigned int ui;
3539
3540   compute_local_properties (transp, comp, antloc, &expr_hash_table);
3541   sbitmap_vector_zero (ae_kill, last_basic_block);
3542
3543   /* Collect expressions which might trap.  */
3544   trapping_expr = sbitmap_alloc (expr_hash_table.n_elems);
3545   sbitmap_zero (trapping_expr);
3546   for (ui = 0; ui < expr_hash_table.size; ui++)
3547     {
3548       struct expr *e;
3549       for (e = expr_hash_table.table[ui]; e != NULL; e = e->next_same_hash)
3550         if (may_trap_p (e->expr))
3551           SET_BIT (trapping_expr, e->bitmap_index);
3552     }
3553
3554   /* Compute ae_kill for each basic block using:
3555
3556      ~(TRANSP | COMP)
3557   */
3558
3559   FOR_EACH_BB (bb)
3560     {
3561       edge e;
3562       edge_iterator ei;
3563
3564       /* If the current block is the destination of an abnormal edge, we
3565          kill all trapping expressions because we won't be able to properly
3566          place the instruction on the edge.  So make them neither
3567          anticipatable nor transparent.  This is fairly conservative.  */
3568       FOR_EACH_EDGE (e, ei, bb->preds)
3569         if (e->flags & EDGE_ABNORMAL)
3570           {
3571             sbitmap_difference (antloc[bb->index], antloc[bb->index], trapping_expr);
3572             sbitmap_difference (transp[bb->index], transp[bb->index], trapping_expr);
3573             break;
3574           }
3575
3576       sbitmap_a_or_b (ae_kill[bb->index], transp[bb->index], comp[bb->index]);
3577       sbitmap_not (ae_kill[bb->index], ae_kill[bb->index]);
3578     }
3579
3580   edge_list = pre_edge_lcm (expr_hash_table.n_elems, transp, comp, antloc,
3581                             ae_kill, &pre_insert_map, &pre_delete_map);
3582   sbitmap_vector_free (antloc);
3583   antloc = NULL;
3584   sbitmap_vector_free (ae_kill);
3585   ae_kill = NULL;
3586   sbitmap_free (trapping_expr);
3587 }
3588 \f
3589 /* PRE utilities */
3590
3591 /* Return nonzero if an occurrence of expression EXPR in OCCR_BB would reach
3592    block BB.
3593
3594    VISITED is a pointer to a working buffer for tracking which BB's have
3595    been visited.  It is NULL for the top-level call.
3596
3597    We treat reaching expressions that go through blocks containing the same
3598    reaching expression as "not reaching".  E.g. if EXPR is generated in blocks
3599    2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
3600    2 as not reaching.  The intent is to improve the probability of finding
3601    only one reaching expression and to reduce register lifetimes by picking
3602    the closest such expression.  */
3603
3604 static int
3605 pre_expr_reaches_here_p_work (basic_block occr_bb, struct expr *expr, basic_block bb, char *visited)
3606 {
3607   edge pred;
3608   edge_iterator ei;
3609   
3610   FOR_EACH_EDGE (pred, ei, bb->preds)
3611     {
3612       basic_block pred_bb = pred->src;
3613
3614       if (pred->src == ENTRY_BLOCK_PTR
3615           /* Has predecessor has already been visited?  */
3616           || visited[pred_bb->index])
3617         ;/* Nothing to do.  */
3618
3619       /* Does this predecessor generate this expression?  */
3620       else if (TEST_BIT (comp[pred_bb->index], expr->bitmap_index))
3621         {
3622           /* Is this the occurrence we're looking for?
3623              Note that there's only one generating occurrence per block
3624              so we just need to check the block number.  */
3625           if (occr_bb == pred_bb)
3626             return 1;
3627
3628           visited[pred_bb->index] = 1;
3629         }
3630       /* Ignore this predecessor if it kills the expression.  */
3631       else if (! TEST_BIT (transp[pred_bb->index], expr->bitmap_index))
3632         visited[pred_bb->index] = 1;
3633
3634       /* Neither gen nor kill.  */
3635       else
3636         {
3637           visited[pred_bb->index] = 1;
3638           if (pre_expr_reaches_here_p_work (occr_bb, expr, pred_bb, visited))
3639             return 1;
3640         }
3641     }
3642
3643   /* All paths have been checked.  */
3644   return 0;
3645 }
3646
3647 /* The wrapper for pre_expr_reaches_here_work that ensures that any
3648    memory allocated for that function is returned.  */
3649
3650 static int
3651 pre_expr_reaches_here_p (basic_block occr_bb, struct expr *expr, basic_block bb)
3652 {
3653   int rval;
3654   char *visited = XCNEWVEC (char, last_basic_block);
3655
3656   rval = pre_expr_reaches_here_p_work (occr_bb, expr, bb, visited);
3657
3658   free (visited);
3659   return rval;
3660 }
3661 \f
3662
3663 /* Given an expr, generate RTL which we can insert at the end of a BB,
3664    or on an edge.  Set the block number of any insns generated to
3665    the value of BB.  */
3666
3667 static rtx
3668 process_insert_insn (struct expr *expr)
3669 {
3670   rtx reg = expr->reaching_reg;
3671   rtx exp = copy_rtx (expr->expr);
3672   rtx pat;
3673
3674   start_sequence ();
3675
3676   /* If the expression is something that's an operand, like a constant,
3677      just copy it to a register.  */
3678   if (general_operand (exp, GET_MODE (reg)))
3679     emit_move_insn (reg, exp);
3680
3681   /* Otherwise, make a new insn to compute this expression and make sure the
3682      insn will be recognized (this also adds any needed CLOBBERs).  Copy the
3683      expression to make sure we don't have any sharing issues.  */
3684   else
3685     {
3686       rtx insn = emit_insn (gen_rtx_SET (VOIDmode, reg, exp));
3687
3688       if (insn_invalid_p (insn))
3689         gcc_unreachable ();
3690     }
3691   
3692
3693   pat = get_insns ();
3694   end_sequence ();
3695
3696   return pat;
3697 }
3698
3699 /* Add EXPR to the end of basic block BB.
3700
3701    This is used by both the PRE and code hoisting.
3702
3703    For PRE, we want to verify that the expr is either transparent
3704    or locally anticipatable in the target block.  This check makes
3705    no sense for code hoisting.  */
3706
3707 static void
3708 insert_insn_end_basic_block (struct expr *expr, basic_block bb, int pre)
3709 {
3710   rtx insn = BB_END (bb);
3711   rtx new_insn;
3712   rtx reg = expr->reaching_reg;
3713   int regno = REGNO (reg);
3714   rtx pat, pat_end;
3715
3716   pat = process_insert_insn (expr);
3717   gcc_assert (pat && INSN_P (pat));
3718
3719   pat_end = pat;
3720   while (NEXT_INSN (pat_end) != NULL_RTX)
3721     pat_end = NEXT_INSN (pat_end);
3722
3723   /* If the last insn is a jump, insert EXPR in front [taking care to
3724      handle cc0, etc. properly].  Similarly we need to care trapping
3725      instructions in presence of non-call exceptions.  */
3726
3727   if (JUMP_P (insn)
3728       || (NONJUMP_INSN_P (insn)
3729           && (!single_succ_p (bb)
3730               || single_succ_edge (bb)->flags & EDGE_ABNORMAL)))
3731     {
3732 #ifdef HAVE_cc0
3733       rtx note;
3734 #endif
3735       /* It should always be the case that we can put these instructions
3736          anywhere in the basic block with performing PRE optimizations.
3737          Check this.  */
3738       gcc_assert (!NONJUMP_INSN_P (insn) || !pre
3739                   || TEST_BIT (antloc[bb->index], expr->bitmap_index)
3740                   || TEST_BIT (transp[bb->index], expr->bitmap_index));
3741
3742       /* If this is a jump table, then we can't insert stuff here.  Since
3743          we know the previous real insn must be the tablejump, we insert
3744          the new instruction just before the tablejump.  */
3745       if (GET_CODE (PATTERN (insn)) == ADDR_VEC
3746           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
3747         insn = prev_real_insn (insn);
3748
3749 #ifdef HAVE_cc0
3750       /* FIXME: 'twould be nice to call prev_cc0_setter here but it aborts
3751          if cc0 isn't set.  */
3752       note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
3753       if (note)
3754         insn = XEXP (note, 0);
3755       else
3756         {
3757           rtx maybe_cc0_setter = prev_nonnote_insn (insn);
3758           if (maybe_cc0_setter
3759               && INSN_P (maybe_cc0_setter)
3760               && sets_cc0_p (PATTERN (maybe_cc0_setter)))
3761             insn = maybe_cc0_setter;
3762         }
3763 #endif
3764       /* FIXME: What if something in cc0/jump uses value set in new insn?  */
3765       new_insn = emit_insn_before_noloc (pat, insn, bb);
3766     }
3767
3768   /* Likewise if the last insn is a call, as will happen in the presence
3769      of exception handling.  */
3770   else if (CALL_P (insn)
3771            && (!single_succ_p (bb)
3772                || single_succ_edge (bb)->flags & EDGE_ABNORMAL))
3773     {
3774       /* Keeping in mind SMALL_REGISTER_CLASSES and parameters in registers,
3775          we search backward and place the instructions before the first
3776          parameter is loaded.  Do this for everyone for consistency and a
3777          presumption that we'll get better code elsewhere as well.
3778
3779          It should always be the case that we can put these instructions
3780          anywhere in the basic block with performing PRE optimizations.
3781          Check this.  */
3782
3783       gcc_assert (!pre
3784                   || TEST_BIT (antloc[bb->index], expr->bitmap_index)
3785                   || TEST_BIT (transp[bb->index], expr->bitmap_index));
3786
3787       /* Since different machines initialize their parameter registers
3788          in different orders, assume nothing.  Collect the set of all
3789          parameter registers.  */
3790       insn = find_first_parameter_load (insn, BB_HEAD (bb));
3791
3792       /* If we found all the parameter loads, then we want to insert
3793          before the first parameter load.
3794
3795          If we did not find all the parameter loads, then we might have
3796          stopped on the head of the block, which could be a CODE_LABEL.
3797          If we inserted before the CODE_LABEL, then we would be putting
3798          the insn in the wrong basic block.  In that case, put the insn
3799          after the CODE_LABEL.  Also, respect NOTE_INSN_BASIC_BLOCK.  */
3800       while (LABEL_P (insn)
3801              || NOTE_INSN_BASIC_BLOCK_P (insn))
3802         insn = NEXT_INSN (insn);
3803
3804       new_insn = emit_insn_before_noloc (pat, insn, bb);
3805     }
3806   else
3807     new_insn = emit_insn_after_noloc (pat, insn, bb);
3808
3809   while (1)
3810     {
3811       if (INSN_P (pat))
3812         add_label_notes (PATTERN (pat), new_insn);
3813       if (pat == pat_end)
3814         break;
3815       pat = NEXT_INSN (pat);
3816     }
3817
3818   gcse_create_count++;
3819
3820   if (dump_file)
3821     {
3822       fprintf (dump_file, "PRE/HOIST: end of bb %d, insn %d, ",
3823                bb->index, INSN_UID (new_insn));
3824       fprintf (dump_file, "copying expression %d to reg %d\n",
3825                expr->bitmap_index, regno);
3826     }
3827 }
3828
3829 /* Insert partially redundant expressions on edges in the CFG to make
3830    the expressions fully redundant.  */
3831
3832 static int
3833 pre_edge_insert (struct edge_list *edge_list, struct expr **index_map)
3834 {
3835   int e, i, j, num_edges, set_size, did_insert = 0;
3836   sbitmap *inserted;
3837
3838   /* Where PRE_INSERT_MAP is nonzero, we add the expression on that edge
3839      if it reaches any of the deleted expressions.  */
3840
3841   set_size = pre_insert_map[0]->size;
3842   num_edges = NUM_EDGES (edge_list);
3843   inserted = sbitmap_vector_alloc (num_edges, expr_hash_table.n_elems);
3844   sbitmap_vector_zero (inserted, num_edges);
3845
3846   for (e = 0; e < num_edges; e++)
3847     {
3848       int indx;
3849       basic_block bb = INDEX_EDGE_PRED_BB (edge_list, e);
3850
3851       for (i = indx = 0; i < set_size; i++, indx += SBITMAP_ELT_BITS)
3852         {
3853           SBITMAP_ELT_TYPE insert = pre_insert_map[e]->elms[i];
3854
3855           for (j = indx; insert && j < (int) expr_hash_table.n_elems; j++, insert >>= 1)
3856             if ((insert & 1) != 0 && index_map[j]->reaching_reg != NULL_RTX)
3857               {
3858                 struct expr *expr = index_map[j];
3859                 struct occr *occr;
3860
3861                 /* Now look at each deleted occurrence of this expression.  */
3862                 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
3863                   {
3864                     if (! occr->deleted_p)
3865                       continue;
3866
3867                     /* Insert this expression on this edge if it would
3868                        reach the deleted occurrence in BB.  */
3869                     if (!TEST_BIT (inserted[e], j))
3870                       {
3871                         rtx insn;
3872                         edge eg = INDEX_EDGE (edge_list, e);
3873
3874                         /* We can't insert anything on an abnormal and
3875                            critical edge, so we insert the insn at the end of
3876                            the previous block. There are several alternatives
3877                            detailed in Morgans book P277 (sec 10.5) for
3878                            handling this situation.  This one is easiest for
3879                            now.  */
3880
3881                         if (eg->flags & EDGE_ABNORMAL)
3882                           insert_insn_end_basic_block (index_map[j], bb, 0);
3883                         else
3884                           {
3885                             insn = process_insert_insn (index_map[j]);
3886                             insert_insn_on_edge (insn, eg);
3887                           }
3888
3889                         if (dump_file)
3890                           {
3891                             fprintf (dump_file, "PRE/HOIST: edge (%d,%d), ",
3892                                      bb->index,
3893                                      INDEX_EDGE_SUCC_BB (edge_list, e)->index);
3894                             fprintf (dump_file, "copy expression %d\n",
3895                                      expr->bitmap_index);
3896                           }
3897
3898                         update_ld_motion_stores (expr);
3899                         SET_BIT (inserted[e], j);
3900                         did_insert = 1;
3901                         gcse_create_count++;
3902                       }
3903                   }
3904               }
3905         }
3906     }
3907
3908   sbitmap_vector_free (inserted);
3909   return did_insert;
3910 }
3911
3912 /* Copy the result of EXPR->EXPR generated by INSN to EXPR->REACHING_REG.
3913    Given "old_reg <- expr" (INSN), instead of adding after it
3914      reaching_reg <- old_reg
3915    it's better to do the following:
3916      reaching_reg <- expr
3917      old_reg      <- reaching_reg
3918    because this way copy propagation can discover additional PRE
3919    opportunities.  But if this fails, we try the old way.
3920    When "expr" is a store, i.e.
3921    given "MEM <- old_reg", instead of adding after it
3922      reaching_reg <- old_reg
3923    it's better to add it before as follows:
3924      reaching_reg <- old_reg
3925      MEM          <- reaching_reg.  */
3926
3927 static void
3928 pre_insert_copy_insn (struct expr *expr, rtx insn)
3929 {
3930   rtx reg = expr->reaching_reg;
3931   int regno = REGNO (reg);
3932   int indx = expr->bitmap_index;
3933   rtx pat = PATTERN (insn);
3934   rtx set, first_set, new_insn;
3935   rtx old_reg;
3936   int i;
3937
3938   /* This block matches the logic in hash_scan_insn.  */
3939   switch (GET_CODE (pat))
3940     {
3941     case SET:
3942       set = pat;
3943       break;
3944
3945     case PARALLEL:
3946       /* Search through the parallel looking for the set whose
3947          source was the expression that we're interested in.  */
3948       first_set = NULL_RTX;
3949       set = NULL_RTX;
3950       for (i = 0; i < XVECLEN (pat, 0); i++)
3951         {
3952           rtx x = XVECEXP (pat, 0, i);
3953           if (GET_CODE (x) == SET)
3954             {
3955               /* If the source was a REG_EQUAL or REG_EQUIV note, we
3956                  may not find an equivalent expression, but in this
3957                  case the PARALLEL will have a single set.  */
3958               if (first_set == NULL_RTX)
3959                 first_set = x;
3960               if (expr_equiv_p (SET_SRC (x), expr->expr))
3961                 {
3962                   set = x;
3963                   break;
3964                 }
3965             }
3966         }
3967
3968       gcc_assert (first_set);
3969       if (set == NULL_RTX)
3970         set = first_set;
3971       break;
3972
3973     default:
3974       gcc_unreachable ();
3975     }
3976
3977   if (REG_P (SET_DEST (set)))
3978     {
3979       old_reg = SET_DEST (set);
3980       /* Check if we can modify the set destination in the original insn.  */
3981       if (validate_change (insn, &SET_DEST (set), reg, 0))
3982         {
3983           new_insn = gen_move_insn (old_reg, reg);
3984           new_insn = emit_insn_after (new_insn, insn);
3985         }
3986       else
3987         {
3988           new_insn = gen_move_insn (reg, old_reg);
3989           new_insn = emit_insn_after (new_insn, insn);
3990         }
3991     }
3992   else /* This is possible only in case of a store to memory.  */
3993     {
3994       old_reg = SET_SRC (set);
3995       new_insn = gen_move_insn (reg, old_reg);
3996
3997       /* Check if we can modify the set source in the original insn.  */
3998       if (validate_change (insn, &SET_SRC (set), reg, 0))
3999         new_insn = emit_insn_before (new_insn, insn);
4000       else
4001         new_insn = emit_insn_after (new_insn, insn);
4002     }
4003
4004   gcse_create_count++;
4005
4006   if (dump_file)
4007     fprintf (dump_file,
4008              "PRE: bb %d, insn %d, copy expression %d in insn %d to reg %d\n",
4009               BLOCK_NUM (insn), INSN_UID (new_insn), indx,
4010               INSN_UID (insn), regno);
4011 }
4012
4013 /* Copy available expressions that reach the redundant expression
4014    to `reaching_reg'.  */
4015
4016 static void
4017 pre_insert_copies (void)
4018 {
4019   unsigned int i, added_copy;
4020   struct expr *expr;
4021   struct occr *occr;
4022   struct occr *avail;
4023
4024   /* For each available expression in the table, copy the result to
4025      `reaching_reg' if the expression reaches a deleted one.
4026
4027      ??? The current algorithm is rather brute force.
4028      Need to do some profiling.  */
4029
4030   for (i = 0; i < expr_hash_table.size; i++)
4031     for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
4032       {
4033         /* If the basic block isn't reachable, PPOUT will be TRUE.  However,
4034            we don't want to insert a copy here because the expression may not
4035            really be redundant.  So only insert an insn if the expression was
4036            deleted.  This test also avoids further processing if the
4037            expression wasn't deleted anywhere.  */
4038         if (expr->reaching_reg == NULL)
4039           continue;
4040
4041         /* Set when we add a copy for that expression.  */
4042         added_copy = 0;
4043
4044         for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
4045           {
4046             if (! occr->deleted_p)
4047               continue;
4048
4049             for (avail = expr->avail_occr; avail != NULL; avail = avail->next)
4050               {
4051                 rtx insn = avail->insn;
4052
4053                 /* No need to handle this one if handled already.  */
4054                 if (avail->copied_p)
4055                   continue;
4056
4057                 /* Don't handle this one if it's a redundant one.  */
4058                 if (INSN_DELETED_P (insn))
4059                   continue;
4060
4061                 /* Or if the expression doesn't reach the deleted one.  */
4062                 if (! pre_expr_reaches_here_p (BLOCK_FOR_INSN (avail->insn),
4063                                                expr,
4064                                                BLOCK_FOR_INSN (occr->insn)))
4065                   continue;
4066
4067                 added_copy = 1;
4068
4069                 /* Copy the result of avail to reaching_reg.  */
4070                 pre_insert_copy_insn (expr, insn);
4071                 avail->copied_p = 1;
4072               }
4073           }
4074
4075           if (added_copy)
4076             update_ld_motion_stores (expr);
4077       }
4078 }
4079
4080 /* Emit move from SRC to DEST noting the equivalence with expression computed
4081    in INSN.  */
4082 static rtx
4083 gcse_emit_move_after (rtx src, rtx dest, rtx insn)
4084 {
4085   rtx new_rtx;
4086   rtx set = single_set (insn), set2;
4087   rtx note;
4088   rtx eqv;
4089
4090   /* This should never fail since we're creating a reg->reg copy
4091      we've verified to be valid.  */
4092
4093   new_rtx = emit_insn_after (gen_move_insn (dest, src), insn);
4094
4095   /* Note the equivalence for local CSE pass.  */
4096   set2 = single_set (new_rtx);
4097   if (!set2 || !rtx_equal_p (SET_DEST (set2), dest))
4098     return new_rtx;
4099   if ((note = find_reg_equal_equiv_note (insn)))
4100     eqv = XEXP (note, 0);
4101   else
4102     eqv = SET_SRC (set);
4103
4104   set_unique_reg_note (new_rtx, REG_EQUAL, copy_insn_1 (eqv));
4105
4106   return new_rtx;
4107 }
4108
4109 /* Delete redundant computations.
4110    Deletion is done by changing the insn to copy the `reaching_reg' of
4111    the expression into the result of the SET.  It is left to later passes
4112    (cprop, cse2, flow, combine, regmove) to propagate the copy or eliminate it.
4113
4114    Returns nonzero if a change is made.  */
4115
4116 static int
4117 pre_delete (void)
4118 {
4119   unsigned int i;
4120   int changed;
4121   struct expr *expr;
4122   struct occr *occr;
4123
4124   changed = 0;
4125   for (i = 0; i < expr_hash_table.size; i++)
4126     for (expr = expr_hash_table.table[i];
4127          expr != NULL;
4128          expr = expr->next_same_hash)
4129       {
4130         int indx = expr->bitmap_index;
4131
4132         /* We only need to search antic_occr since we require
4133            ANTLOC != 0.  */
4134
4135         for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
4136           {
4137             rtx insn = occr->insn;
4138             rtx set;
4139             basic_block bb = BLOCK_FOR_INSN (insn);
4140
4141             /* We only delete insns that have a single_set.  */
4142             if (TEST_BIT (pre_delete_map[bb->index], indx)
4143                 && (set = single_set (insn)) != 0
4144                 && dbg_cnt (pre_insn))
4145               {
4146                 /* Create a pseudo-reg to store the result of reaching
4147                    expressions into.  Get the mode for the new pseudo from
4148                    the mode of the original destination pseudo.  */
4149                 if (expr->reaching_reg == NULL)
4150                   expr->reaching_reg = gen_reg_rtx_and_attrs (SET_DEST (set));
4151
4152                 gcse_emit_move_after (expr->reaching_reg, SET_DEST (set), insn);
4153                 delete_insn (insn);
4154                 occr->deleted_p = 1;
4155                 changed = 1;
4156                 gcse_subst_count++;
4157
4158                 if (dump_file)
4159                   {
4160                     fprintf (dump_file,
4161                              "PRE: redundant insn %d (expression %d) in ",
4162                                INSN_UID (insn), indx);
4163                     fprintf (dump_file, "bb %d, reaching reg is %d\n",
4164                              bb->index, REGNO (expr->reaching_reg));
4165                   }
4166               }
4167           }
4168       }
4169
4170   return changed;
4171 }
4172
4173 /* Perform GCSE optimizations using PRE.
4174    This is called by one_pre_gcse_pass after all the dataflow analysis
4175    has been done.
4176
4177    This is based on the original Morel-Renvoise paper Fred Chow's thesis, and
4178    lazy code motion from Knoop, Ruthing and Steffen as described in Advanced
4179    Compiler Design and Implementation.
4180
4181    ??? A new pseudo reg is created to hold the reaching expression.  The nice
4182    thing about the classical approach is that it would try to use an existing
4183    reg.  If the register can't be adequately optimized [i.e. we introduce
4184    reload problems], one could add a pass here to propagate the new register
4185    through the block.
4186
4187    ??? We don't handle single sets in PARALLELs because we're [currently] not
4188    able to copy the rest of the parallel when we insert copies to create full
4189    redundancies from partial redundancies.  However, there's no reason why we
4190    can't handle PARALLELs in the cases where there are no partial
4191    redundancies.  */
4192
4193 static int
4194 pre_gcse (void)
4195 {
4196   unsigned int i;
4197   int did_insert, changed;
4198   struct expr **index_map;
4199   struct expr *expr;
4200
4201   /* Compute a mapping from expression number (`bitmap_index') to
4202      hash table entry.  */
4203
4204   index_map = XCNEWVEC (struct expr *, expr_hash_table.n_elems);
4205   for (i = 0; i < expr_hash_table.size; i++)
4206     for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
4207       index_map[expr->bitmap_index] = expr;
4208
4209   /* Delete the redundant insns first so that
4210      - we know what register to use for the new insns and for the other
4211        ones with reaching expressions
4212      - we know which insns are redundant when we go to create copies  */
4213
4214   changed = pre_delete ();
4215   did_insert = pre_edge_insert (edge_list, index_map);
4216
4217   /* In other places with reaching expressions, copy the expression to the
4218      specially allocated pseudo-reg that reaches the redundant expr.  */
4219   pre_insert_copies ();
4220   if (did_insert)
4221     {
4222       commit_edge_insertions ();
4223       changed = 1;
4224     }
4225
4226   free (index_map);
4227   return changed;
4228 }
4229
4230 /* Top level routine to perform one PRE GCSE pass.
4231
4232    Return nonzero if a change was made.  */
4233
4234 static int
4235 one_pre_gcse_pass (int pass)
4236 {
4237   int changed = 0;
4238
4239   gcse_subst_count = 0;
4240   gcse_create_count = 0;
4241
4242   alloc_hash_table (get_max_uid (), &expr_hash_table, 0);
4243   add_noreturn_fake_exit_edges ();
4244   if (flag_gcse_lm)
4245     compute_ld_motion_mems ();
4246
4247   compute_hash_table (&expr_hash_table);
4248   trim_ld_motion_mems ();
4249   if (dump_file)
4250     dump_hash_table (dump_file, "Expression", &expr_hash_table);
4251
4252   if (expr_hash_table.n_elems > 0)
4253     {
4254       alloc_pre_mem (last_basic_block, expr_hash_table.n_elems);
4255       compute_pre_data ();
4256       changed |= pre_gcse ();
4257       free_edge_list (edge_list);
4258       free_pre_mem ();
4259     }
4260
4261   free_ldst_mems ();
4262   remove_fake_exit_edges ();
4263   free_hash_table (&expr_hash_table);
4264
4265   if (dump_file)
4266     {
4267       fprintf (dump_file, "\nPRE GCSE of %s, pass %d: %d bytes needed, ",
4268                current_function_name (), pass, bytes_used);
4269       fprintf (dump_file, "%d substs, %d insns created\n",
4270                gcse_subst_count, gcse_create_count);
4271     }
4272
4273   return changed;
4274 }
4275 \f
4276 /* If X contains any LABEL_REF's, add REG_LABEL_OPERAND notes for them
4277    to INSN.  If such notes are added to an insn which references a
4278    CODE_LABEL, the LABEL_NUSES count is incremented.  We have to add
4279    that note, because the following loop optimization pass requires
4280    them.  */
4281
4282 /* ??? If there was a jump optimization pass after gcse and before loop,
4283    then we would not need to do this here, because jump would add the
4284    necessary REG_LABEL_OPERAND and REG_LABEL_TARGET notes.  */
4285
4286 static void
4287 add_label_notes (rtx x, rtx insn)
4288 {
4289   enum rtx_code code = GET_CODE (x);
4290   int i, j;
4291   const char *fmt;
4292
4293   if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
4294     {
4295       /* This code used to ignore labels that referred to dispatch tables to
4296          avoid flow generating (slightly) worse code.
4297
4298          We no longer ignore such label references (see LABEL_REF handling in
4299          mark_jump_label for additional information).  */
4300
4301       /* There's no reason for current users to emit jump-insns with
4302          such a LABEL_REF, so we don't have to handle REG_LABEL_TARGET
4303          notes.  */
4304       gcc_assert (!JUMP_P (insn));
4305       add_reg_note (insn, REG_LABEL_OPERAND, XEXP (x, 0));
4306
4307       if (LABEL_P (XEXP (x, 0)))
4308         LABEL_NUSES (XEXP (x, 0))++;
4309
4310       return;
4311     }
4312
4313   for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
4314     {
4315       if (fmt[i] == 'e')
4316         add_label_notes (XEXP (x, i), insn);
4317       else if (fmt[i] == 'E')
4318         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4319           add_label_notes (XVECEXP (x, i, j), insn);
4320     }
4321 }
4322
4323 /* Compute transparent outgoing information for each block.
4324
4325    An expression is transparent to an edge unless it is killed by
4326    the edge itself.  This can only happen with abnormal control flow,
4327    when the edge is traversed through a call.  This happens with
4328    non-local labels and exceptions.
4329
4330    This would not be necessary if we split the edge.  While this is
4331    normally impossible for abnormal critical edges, with some effort
4332    it should be possible with exception handling, since we still have
4333    control over which handler should be invoked.  But due to increased
4334    EH table sizes, this may not be worthwhile.  */
4335
4336 static void
4337 compute_transpout (void)
4338 {
4339   basic_block bb;
4340   unsigned int i;
4341   struct expr *expr;
4342
4343   sbitmap_vector_ones (transpout, last_basic_block);
4344
4345   FOR_EACH_BB (bb)
4346     {
4347       /* Note that flow inserted a nop at the end of basic blocks that
4348          end in call instructions for reasons other than abnormal
4349          control flow.  */
4350       if (! CALL_P (BB_END (bb)))
4351         continue;
4352
4353       for (i = 0; i < expr_hash_table.size; i++)
4354         for (expr = expr_hash_table.table[i]; expr ; expr = expr->next_same_hash)
4355           if (MEM_P (expr->expr))
4356             {
4357               if (GET_CODE (XEXP (expr->expr, 0)) == SYMBOL_REF
4358                   && CONSTANT_POOL_ADDRESS_P (XEXP (expr->expr, 0)))
4359                 continue;
4360
4361               /* ??? Optimally, we would use interprocedural alias
4362                  analysis to determine if this mem is actually killed
4363                  by this call.  */
4364               RESET_BIT (transpout[bb->index], expr->bitmap_index);
4365             }
4366     }
4367 }
4368
4369 /* Code Hoisting variables and subroutines.  */
4370
4371 /* Very busy expressions.  */
4372 static sbitmap *hoist_vbein;
4373 static sbitmap *hoist_vbeout;
4374
4375 /* Hoistable expressions.  */
4376 static sbitmap *hoist_exprs;
4377
4378 /* ??? We could compute post dominators and run this algorithm in
4379    reverse to perform tail merging, doing so would probably be
4380    more effective than the tail merging code in jump.c.
4381
4382    It's unclear if tail merging could be run in parallel with
4383    code hoisting.  It would be nice.  */
4384
4385 /* Allocate vars used for code hoisting analysis.  */
4386
4387 static void
4388 alloc_code_hoist_mem (int n_blocks, int n_exprs)
4389 {
4390   antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
4391   transp = sbitmap_vector_alloc (n_blocks, n_exprs);
4392   comp = sbitmap_vector_alloc (n_blocks, n_exprs);
4393
4394   hoist_vbein = sbitmap_vector_alloc (n_blocks, n_exprs);
4395   hoist_vbeout = sbitmap_vector_alloc (n_blocks, n_exprs);
4396   hoist_exprs = sbitmap_vector_alloc (n_blocks, n_exprs);
4397   transpout = sbitmap_vector_alloc (n_blocks, n_exprs);
4398 }
4399
4400 /* Free vars used for code hoisting analysis.  */
4401
4402 static void
4403 free_code_hoist_mem (void)
4404 {
4405   sbitmap_vector_free (antloc);
4406   sbitmap_vector_free (transp);
4407   sbitmap_vector_free (comp);
4408
4409   sbitmap_vector_free (hoist_vbein);
4410   sbitmap_vector_free (hoist_vbeout);
4411   sbitmap_vector_free (hoist_exprs);
4412   sbitmap_vector_free (transpout);
4413
4414   free_dominance_info (CDI_DOMINATORS);
4415 }
4416
4417 /* Compute the very busy expressions at entry/exit from each block.
4418
4419    An expression is very busy if all paths from a given point
4420    compute the expression.  */
4421
4422 static void
4423 compute_code_hoist_vbeinout (void)
4424 {
4425   int changed, passes;
4426   basic_block bb;
4427
4428   sbitmap_vector_zero (hoist_vbeout, last_basic_block);
4429   sbitmap_vector_zero (hoist_vbein, last_basic_block);
4430
4431   passes = 0;
4432   changed = 1;
4433
4434   while (changed)
4435     {
4436       changed = 0;
4437
4438       /* We scan the blocks in the reverse order to speed up
4439          the convergence.  */
4440       FOR_EACH_BB_REVERSE (bb)
4441         {
4442           if (bb->next_bb != EXIT_BLOCK_PTR)
4443             sbitmap_intersection_of_succs (hoist_vbeout[bb->index],
4444                                            hoist_vbein, bb->index);
4445
4446           changed |= sbitmap_a_or_b_and_c_cg (hoist_vbein[bb->index],
4447                                               antloc[bb->index],
4448                                               hoist_vbeout[bb->index],
4449                                               transp[bb->index]);
4450         }
4451
4452       passes++;
4453     }
4454
4455   if (dump_file)
4456     fprintf (dump_file, "hoisting vbeinout computation: %d passes\n", passes);
4457 }
4458
4459 /* Top level routine to do the dataflow analysis needed by code hoisting.  */
4460
4461 static void
4462 compute_code_hoist_data (void)
4463 {
4464   compute_local_properties (transp, comp, antloc, &expr_hash_table);
4465   compute_transpout ();
4466   compute_code_hoist_vbeinout ();
4467   calculate_dominance_info (CDI_DOMINATORS);
4468   if (dump_file)
4469     fprintf (dump_file, "\n");
4470 }
4471
4472 /* Determine if the expression identified by EXPR_INDEX would
4473    reach BB unimpared if it was placed at the end of EXPR_BB.
4474
4475    It's unclear exactly what Muchnick meant by "unimpared".  It seems
4476    to me that the expression must either be computed or transparent in
4477    *every* block in the path(s) from EXPR_BB to BB.  Any other definition
4478    would allow the expression to be hoisted out of loops, even if
4479    the expression wasn't a loop invariant.
4480
4481    Contrast this to reachability for PRE where an expression is
4482    considered reachable if *any* path reaches instead of *all*
4483    paths.  */
4484
4485 static int
4486 hoist_expr_reaches_here_p (basic_block expr_bb, int expr_index, basic_block bb, char *visited)
4487 {
4488   edge pred;
4489   edge_iterator ei;
4490   int visited_allocated_locally = 0;
4491
4492
4493   if (visited == NULL)
4494     {
4495       visited_allocated_locally = 1;
4496       visited = XCNEWVEC (char, last_basic_block);
4497     }
4498
4499   FOR_EACH_EDGE (pred, ei, bb->preds)
4500     {
4501       basic_block pred_bb = pred->src;
4502
4503       if (pred->src == ENTRY_BLOCK_PTR)
4504         break;
4505       else if (pred_bb == expr_bb)
4506         continue;
4507       else if (visited[pred_bb->index])
4508         continue;
4509
4510       /* Does this predecessor generate this expression?  */
4511       else if (TEST_BIT (comp[pred_bb->index], expr_index))
4512         break;
4513       else if (! TEST_BIT (transp[pred_bb->index], expr_index))
4514         break;
4515
4516       /* Not killed.  */
4517       else
4518         {
4519           visited[pred_bb->index] = 1;
4520           if (! hoist_expr_reaches_here_p (expr_bb, expr_index,
4521                                            pred_bb, visited))
4522             break;
4523         }
4524     }
4525   if (visited_allocated_locally)
4526     free (visited);
4527
4528   return (pred == NULL);
4529 }
4530 \f
4531 /* Actually perform code hoisting.  */
4532
4533 static void
4534 hoist_code (void)
4535 {
4536   basic_block bb, dominated;
4537   VEC (basic_block, heap) *domby;
4538   unsigned int i,j;
4539   struct expr **index_map;
4540   struct expr *expr;
4541
4542   sbitmap_vector_zero (hoist_exprs, last_basic_block);
4543
4544   /* Compute a mapping from expression number (`bitmap_index') to
4545      hash table entry.  */
4546
4547   index_map = XCNEWVEC (struct expr *, expr_hash_table.n_elems);
4548   for (i = 0; i < expr_hash_table.size; i++)
4549     for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
4550       index_map[expr->bitmap_index] = expr;
4551
4552   /* Walk over each basic block looking for potentially hoistable
4553      expressions, nothing gets hoisted from the entry block.  */
4554   FOR_EACH_BB (bb)
4555     {
4556       int found = 0;
4557       int insn_inserted_p;
4558
4559       domby = get_dominated_by (CDI_DOMINATORS, bb);
4560       /* Examine each expression that is very busy at the exit of this
4561          block.  These are the potentially hoistable expressions.  */
4562       for (i = 0; i < hoist_vbeout[bb->index]->n_bits; i++)
4563         {
4564           int hoistable = 0;
4565
4566           if (TEST_BIT (hoist_vbeout[bb->index], i)
4567               && TEST_BIT (transpout[bb->index], i))
4568             {
4569               /* We've found a potentially hoistable expression, now
4570                  we look at every block BB dominates to see if it
4571                  computes the expression.  */
4572               for (j = 0; VEC_iterate (basic_block, domby, j, dominated); j++)
4573                 {
4574                   /* Ignore self dominance.  */
4575                   if (bb == dominated)
4576                     continue;
4577                   /* We've found a dominated block, now see if it computes
4578                      the busy expression and whether or not moving that
4579                      expression to the "beginning" of that block is safe.  */
4580                   if (!TEST_BIT (antloc[dominated->index], i))
4581                     continue;
4582
4583                   /* Note if the expression would reach the dominated block
4584                      unimpared if it was placed at the end of BB.
4585
4586                      Keep track of how many times this expression is hoistable
4587                      from a dominated block into BB.  */
4588                   if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
4589                     hoistable++;
4590                 }
4591
4592               /* If we found more than one hoistable occurrence of this
4593                  expression, then note it in the bitmap of expressions to
4594                  hoist.  It makes no sense to hoist things which are computed
4595                  in only one BB, and doing so tends to pessimize register
4596                  allocation.  One could increase this value to try harder
4597                  to avoid any possible code expansion due to register
4598                  allocation issues; however experiments have shown that
4599                  the vast majority of hoistable expressions are only movable
4600                  from two successors, so raising this threshold is likely
4601                  to nullify any benefit we get from code hoisting.  */
4602               if (hoistable > 1)
4603                 {
4604                   SET_BIT (hoist_exprs[bb->index], i);
4605                   found = 1;
4606                 }
4607             }
4608         }
4609       /* If we found nothing to hoist, then quit now.  */
4610       if (! found)
4611         {
4612           VEC_free (basic_block, heap, domby);
4613           continue;
4614         }
4615
4616       /* Loop over all the hoistable expressions.  */
4617       for (i = 0; i < hoist_exprs[bb->index]->n_bits; i++)
4618         {
4619           /* We want to insert the expression into BB only once, so
4620              note when we've inserted it.  */
4621           insn_inserted_p = 0;
4622
4623           /* These tests should be the same as the tests above.  */
4624           if (TEST_BIT (hoist_exprs[bb->index], i))
4625             {
4626               /* We've found a potentially hoistable expression, now
4627                  we look at every block BB dominates to see if it
4628                  computes the expression.  */
4629               for (j = 0; VEC_iterate (basic_block, domby, j, dominated); j++)
4630                 {
4631                   /* Ignore self dominance.  */
4632                   if (bb == dominated)
4633                     continue;
4634
4635                   /* We've found a dominated block, now see if it computes
4636                      the busy expression and whether or not moving that
4637                      expression to the "beginning" of that block is safe.  */
4638                   if (!TEST_BIT (antloc[dominated->index], i))
4639                     continue;
4640
4641                   /* The expression is computed in the dominated block and
4642                      it would be safe to compute it at the start of the
4643                      dominated block.  Now we have to determine if the
4644                      expression would reach the dominated block if it was
4645                      placed at the end of BB.  */
4646                   if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
4647                     {
4648                       struct expr *expr = index_map[i];
4649                       struct occr *occr = expr->antic_occr;
4650                       rtx insn;
4651                       rtx set;
4652
4653                       /* Find the right occurrence of this expression.  */
4654                       while (BLOCK_FOR_INSN (occr->insn) != dominated && occr)
4655                         occr = occr->next;
4656
4657                       gcc_assert (occr);
4658                       insn = occr->insn;
4659                       set = single_set (insn);
4660                       gcc_assert (set);
4661
4662                       /* Create a pseudo-reg to store the result of reaching
4663                          expressions into.  Get the mode for the new pseudo
4664                          from the mode of the original destination pseudo.  */
4665                       if (expr->reaching_reg == NULL)
4666                         expr->reaching_reg
4667                           = gen_reg_rtx_and_attrs (SET_DEST (set));
4668
4669                       gcse_emit_move_after (expr->reaching_reg, SET_DEST (set), insn);
4670                       delete_insn (insn);
4671                       occr->deleted_p = 1;
4672                       if (!insn_inserted_p)
4673                         {
4674                           insert_insn_end_basic_block (index_map[i], bb, 0);
4675                           insn_inserted_p = 1;
4676                         }
4677                     }
4678                 }
4679             }
4680         }
4681       VEC_free (basic_block, heap, domby);
4682     }
4683
4684   free (index_map);
4685 }
4686
4687 /* Top level routine to perform one code hoisting (aka unification) pass
4688
4689    Return nonzero if a change was made.  */
4690
4691 static int
4692 one_code_hoisting_pass (void)
4693 {
4694   int changed = 0;
4695
4696   alloc_hash_table (get_max_uid (), &expr_hash_table, 0);
4697   compute_hash_table (&expr_hash_table);
4698   if (dump_file)
4699     dump_hash_table (dump_file, "Code Hosting Expressions", &expr_hash_table);
4700
4701   if (expr_hash_table.n_elems > 0)
4702     {
4703       alloc_code_hoist_mem (last_basic_block, expr_hash_table.n_elems);
4704       compute_code_hoist_data ();
4705       hoist_code ();
4706       free_code_hoist_mem ();
4707     }
4708
4709   free_hash_table (&expr_hash_table);
4710
4711   return changed;
4712 }
4713 \f
4714 /*  Here we provide the things required to do store motion towards
4715     the exit. In order for this to be effective, gcse also needed to
4716     be taught how to move a load when it is kill only by a store to itself.
4717
4718             int i;
4719             float a[10];
4720
4721             void foo(float scale)
4722             {
4723               for (i=0; i<10; i++)
4724                 a[i] *= scale;
4725             }
4726
4727     'i' is both loaded and stored to in the loop. Normally, gcse cannot move
4728     the load out since its live around the loop, and stored at the bottom
4729     of the loop.
4730
4731       The 'Load Motion' referred to and implemented in this file is
4732     an enhancement to gcse which when using edge based lcm, recognizes
4733     this situation and allows gcse to move the load out of the loop.
4734
4735       Once gcse has hoisted the load, store motion can then push this
4736     load towards the exit, and we end up with no loads or stores of 'i'
4737     in the loop.  */
4738
4739 static hashval_t
4740 pre_ldst_expr_hash (const void *p)
4741 {
4742   int do_not_record_p = 0;
4743   const struct ls_expr *const x = (const struct ls_expr *) p;
4744   return hash_rtx (x->pattern, GET_MODE (x->pattern), &do_not_record_p, NULL, false);
4745 }
4746
4747 static int
4748 pre_ldst_expr_eq (const void *p1, const void *p2)
4749 {
4750   const struct ls_expr *const ptr1 = (const struct ls_expr *) p1,
4751     *const ptr2 = (const struct ls_expr *) p2;
4752   return expr_equiv_p (ptr1->pattern, ptr2->pattern);
4753 }
4754
4755 /* This will search the ldst list for a matching expression. If it
4756    doesn't find one, we create one and initialize it.  */
4757
4758 static struct ls_expr *
4759 ldst_entry (rtx x)
4760 {
4761   int do_not_record_p = 0;
4762   struct ls_expr * ptr;
4763   unsigned int hash;
4764   void **slot;
4765   struct ls_expr e;
4766
4767   hash = hash_rtx (x, GET_MODE (x), &do_not_record_p,
4768                    NULL,  /*have_reg_qty=*/false);
4769
4770   e.pattern = x;
4771   slot = htab_find_slot_with_hash (pre_ldst_table, &e, hash, INSERT);
4772   if (*slot)
4773     return (struct ls_expr *)*slot;
4774
4775   ptr = XNEW (struct ls_expr);
4776
4777   ptr->next         = pre_ldst_mems;
4778   ptr->expr         = NULL;
4779   ptr->pattern      = x;
4780   ptr->pattern_regs = NULL_RTX;
4781   ptr->loads        = NULL_RTX;
4782   ptr->stores       = NULL_RTX;
4783   ptr->reaching_reg = NULL_RTX;
4784   ptr->invalid      = 0;
4785   ptr->index        = 0;
4786   ptr->hash_index   = hash;
4787   pre_ldst_mems     = ptr;
4788   *slot = ptr;
4789
4790   return ptr;
4791 }
4792
4793 /* Free up an individual ldst entry.  */
4794
4795 static void
4796 free_ldst_entry (struct ls_expr * ptr)
4797 {
4798   free_INSN_LIST_list (& ptr->loads);
4799   free_INSN_LIST_list (& ptr->stores);
4800
4801   free (ptr);
4802 }
4803
4804 /* Free up all memory associated with the ldst list.  */
4805
4806 static void
4807 free_ldst_mems (void)
4808 {
4809   if (pre_ldst_table)
4810     htab_delete (pre_ldst_table);
4811   pre_ldst_table = NULL;
4812
4813   while (pre_ldst_mems)
4814     {
4815       struct ls_expr * tmp = pre_ldst_mems;
4816
4817       pre_ldst_mems = pre_ldst_mems->next;
4818
4819       free_ldst_entry (tmp);
4820     }
4821
4822   pre_ldst_mems = NULL;
4823 }
4824
4825 /* Dump debugging info about the ldst list.  */
4826
4827 static void
4828 print_ldst_list (FILE * file)
4829 {
4830   struct ls_expr * ptr;
4831
4832   fprintf (file, "LDST list: \n");
4833
4834   for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
4835     {
4836       fprintf (file, "  Pattern (%3d): ", ptr->index);
4837
4838       print_rtl (file, ptr->pattern);
4839
4840       fprintf (file, "\n         Loads : ");
4841
4842       if (ptr->loads)
4843         print_rtl (file, ptr->loads);
4844       else
4845         fprintf (file, "(nil)");
4846
4847       fprintf (file, "\n        Stores : ");
4848
4849       if (ptr->stores)
4850         print_rtl (file, ptr->stores);
4851       else
4852         fprintf (file, "(nil)");
4853
4854       fprintf (file, "\n\n");
4855     }
4856
4857   fprintf (file, "\n");
4858 }
4859
4860 /* Returns 1 if X is in the list of ldst only expressions.  */
4861
4862 static struct ls_expr *
4863 find_rtx_in_ldst (rtx x)
4864 {
4865   struct ls_expr e;
4866   void **slot;
4867   if (!pre_ldst_table)
4868     return NULL;
4869   e.pattern = x;
4870   slot = htab_find_slot (pre_ldst_table, &e, NO_INSERT);
4871   if (!slot || ((struct ls_expr *)*slot)->invalid)
4872     return NULL;
4873   return (struct ls_expr *) *slot;
4874 }
4875
4876 /* Assign each element of the list of mems a monotonically increasing value.  */
4877
4878 static int
4879 enumerate_ldsts (void)
4880 {
4881   struct ls_expr * ptr;
4882   int n = 0;
4883
4884   for (ptr = pre_ldst_mems; ptr != NULL; ptr = ptr->next)
4885     ptr->index = n++;
4886
4887   return n;
4888 }
4889
4890 /* Return first item in the list.  */
4891
4892 static inline struct ls_expr *
4893 first_ls_expr (void)
4894 {
4895   return pre_ldst_mems;
4896 }
4897
4898 /* Return the next item in the list after the specified one.  */
4899
4900 static inline struct ls_expr *
4901 next_ls_expr (struct ls_expr * ptr)
4902 {
4903   return ptr->next;
4904 }
4905 \f
4906 /* Load Motion for loads which only kill themselves.  */
4907
4908 /* Return true if x is a simple MEM operation, with no registers or
4909    side effects. These are the types of loads we consider for the
4910    ld_motion list, otherwise we let the usual aliasing take care of it.  */
4911
4912 static int
4913 simple_mem (const_rtx x)
4914 {
4915   if (! MEM_P (x))
4916     return 0;
4917
4918   if (MEM_VOLATILE_P (x))
4919     return 0;
4920
4921   if (GET_MODE (x) == BLKmode)
4922     return 0;
4923
4924   /* If we are handling exceptions, we must be careful with memory references
4925      that may trap. If we are not, the behavior is undefined, so we may just
4926      continue.  */
4927   if (flag_non_call_exceptions && may_trap_p (x))
4928     return 0;
4929
4930   if (side_effects_p (x))
4931     return 0;
4932
4933   /* Do not consider function arguments passed on stack.  */
4934   if (reg_mentioned_p (stack_pointer_rtx, x))
4935     return 0;
4936
4937   if (flag_float_store && FLOAT_MODE_P (GET_MODE (x)))
4938     return 0;
4939
4940   return 1;
4941 }
4942
4943 /* Make sure there isn't a buried reference in this pattern anywhere.
4944    If there is, invalidate the entry for it since we're not capable
4945    of fixing it up just yet.. We have to be sure we know about ALL
4946    loads since the aliasing code will allow all entries in the
4947    ld_motion list to not-alias itself.  If we miss a load, we will get
4948    the wrong value since gcse might common it and we won't know to
4949    fix it up.  */
4950
4951 static void
4952 invalidate_any_buried_refs (rtx x)
4953 {
4954   const char * fmt;
4955   int i, j;
4956   struct ls_expr * ptr;
4957
4958   /* Invalidate it in the list.  */
4959   if (MEM_P (x) && simple_mem (x))
4960     {
4961       ptr = ldst_entry (x);
4962       ptr->invalid = 1;
4963     }
4964
4965   /* Recursively process the insn.  */
4966   fmt = GET_RTX_FORMAT (GET_CODE (x));
4967
4968   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4969     {
4970       if (fmt[i] == 'e')
4971         invalidate_any_buried_refs (XEXP (x, i));
4972       else if (fmt[i] == 'E')
4973         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4974           invalidate_any_buried_refs (XVECEXP (x, i, j));
4975     }
4976 }
4977
4978 /* Find all the 'simple' MEMs which are used in LOADs and STORES.  Simple
4979    being defined as MEM loads and stores to symbols, with no side effects
4980    and no registers in the expression.  For a MEM destination, we also
4981    check that the insn is still valid if we replace the destination with a
4982    REG, as is done in update_ld_motion_stores.  If there are any uses/defs
4983    which don't match this criteria, they are invalidated and trimmed out
4984    later.  */
4985
4986 static void
4987 compute_ld_motion_mems (void)
4988 {
4989   struct ls_expr * ptr;
4990   basic_block bb;
4991   rtx insn;
4992
4993   pre_ldst_mems = NULL;
4994   pre_ldst_table = htab_create (13, pre_ldst_expr_hash,
4995                                 pre_ldst_expr_eq, NULL);
4996
4997   FOR_EACH_BB (bb)
4998     {
4999       FOR_BB_INSNS (bb, insn)
5000         {
5001           if (INSN_P (insn))
5002             {
5003               if (GET_CODE (PATTERN (insn)) == SET)
5004                 {
5005                   rtx src = SET_SRC (PATTERN (insn));
5006                   rtx dest = SET_DEST (PATTERN (insn));
5007
5008                   /* Check for a simple LOAD...  */
5009                   if (MEM_P (src) && simple_mem (src))
5010                     {
5011                       ptr = ldst_entry (src);
5012                       if (REG_P (dest))
5013                         ptr->loads = alloc_INSN_LIST (insn, ptr->loads);
5014                       else
5015                         ptr->invalid = 1;
5016                     }
5017                   else
5018                     {
5019                       /* Make sure there isn't a buried load somewhere.  */
5020                       invalidate_any_buried_refs (src);
5021                     }
5022
5023                   /* Check for stores. Don't worry about aliased ones, they
5024                      will block any movement we might do later. We only care
5025                      about this exact pattern since those are the only
5026                      circumstance that we will ignore the aliasing info.  */
5027                   if (MEM_P (dest) && simple_mem (dest))
5028                     {
5029                       ptr = ldst_entry (dest);
5030
5031                       if (! MEM_P (src)
5032                           && GET_CODE (src) != ASM_OPERANDS
5033                           /* Check for REG manually since want_to_gcse_p
5034                              returns 0 for all REGs.  */
5035                           && can_assign_to_reg_p (src))
5036                         ptr->stores = alloc_INSN_LIST (insn, ptr->stores);
5037                       else
5038                         ptr->invalid = 1;
5039                     }
5040                 }
5041               else
5042                 invalidate_any_buried_refs (PATTERN (insn));
5043             }
5044         }
5045     }
5046 }
5047
5048 /* Remove any references that have been either invalidated or are not in the
5049    expression list for pre gcse.  */
5050
5051 static void
5052 trim_ld_motion_mems (void)
5053 {
5054   struct ls_expr * * last = & pre_ldst_mems;
5055   struct ls_expr * ptr = pre_ldst_mems;
5056
5057   while (ptr != NULL)
5058     {
5059       struct expr * expr;
5060
5061       /* Delete if entry has been made invalid.  */
5062       if (! ptr->invalid)
5063         {
5064           /* Delete if we cannot find this mem in the expression list.  */
5065           unsigned int hash = ptr->hash_index % expr_hash_table.size;
5066
5067           for (expr = expr_hash_table.table[hash];
5068                expr != NULL;
5069                expr = expr->next_same_hash)
5070             if (expr_equiv_p (expr->expr, ptr->pattern))
5071               break;
5072         }
5073       else
5074         expr = (struct expr *) 0;
5075
5076       if (expr)
5077         {
5078           /* Set the expression field if we are keeping it.  */
5079           ptr->expr = expr;
5080           last = & ptr->next;
5081           ptr = ptr->next;
5082         }
5083       else
5084         {
5085           *last = ptr->next;
5086           htab_remove_elt_with_hash (pre_ldst_table, ptr, ptr->hash_index);
5087           free_ldst_entry (ptr);
5088           ptr = * last;
5089         }
5090     }
5091
5092   /* Show the world what we've found.  */
5093   if (dump_file && pre_ldst_mems != NULL)
5094     print_ldst_list (dump_file);
5095 }
5096
5097 /* This routine will take an expression which we are replacing with
5098    a reaching register, and update any stores that are needed if
5099    that expression is in the ld_motion list.  Stores are updated by
5100    copying their SRC to the reaching register, and then storing
5101    the reaching register into the store location. These keeps the
5102    correct value in the reaching register for the loads.  */
5103
5104 static void
5105 update_ld_motion_stores (struct expr * expr)
5106 {
5107   struct ls_expr * mem_ptr;
5108
5109   if ((mem_ptr = find_rtx_in_ldst (expr->expr)))
5110     {
5111       /* We can try to find just the REACHED stores, but is shouldn't
5112          matter to set the reaching reg everywhere...  some might be
5113          dead and should be eliminated later.  */
5114
5115       /* We replace (set mem expr) with (set reg expr) (set mem reg)
5116          where reg is the reaching reg used in the load.  We checked in
5117          compute_ld_motion_mems that we can replace (set mem expr) with
5118          (set reg expr) in that insn.  */
5119       rtx list = mem_ptr->stores;
5120
5121       for ( ; list != NULL_RTX; list = XEXP (list, 1))
5122         {
5123           rtx insn = XEXP (list, 0);
5124           rtx pat = PATTERN (insn);
5125           rtx src = SET_SRC (pat);
5126           rtx reg = expr->reaching_reg;
5127           rtx copy, new_rtx;
5128
5129           /* If we've already copied it, continue.  */
5130           if (expr->reaching_reg == src)
5131             continue;
5132
5133           if (dump_file)
5134             {
5135               fprintf (dump_file, "PRE:  store updated with reaching reg ");
5136               print_rtl (dump_file, expr->reaching_reg);
5137               fprintf (dump_file, ":\n  ");
5138               print_inline_rtx (dump_file, insn, 8);
5139               fprintf (dump_file, "\n");
5140             }
5141
5142           copy = gen_move_insn (reg, copy_rtx (SET_SRC (pat)));
5143           new_rtx = emit_insn_before (copy, insn);
5144           SET_SRC (pat) = reg;
5145           df_insn_rescan (insn);
5146
5147           /* un-recognize this pattern since it's probably different now.  */
5148           INSN_CODE (insn) = -1;
5149           gcse_create_count++;
5150         }
5151     }
5152 }
5153 \f
5154 /* Store motion code.  */
5155
5156 #define ANTIC_STORE_LIST(x)             ((x)->loads)
5157 #define AVAIL_STORE_LIST(x)             ((x)->stores)
5158 #define LAST_AVAIL_CHECK_FAILURE(x)     ((x)->reaching_reg)
5159
5160 /* This is used to communicate the target bitvector we want to use in the
5161    reg_set_info routine when called via the note_stores mechanism.  */
5162 static int * regvec;
5163
5164 /* And current insn, for the same routine.  */
5165 static rtx compute_store_table_current_insn;
5166
5167 /* Used in computing the reverse edge graph bit vectors.  */
5168 static sbitmap * st_antloc;
5169
5170 /* Global holding the number of store expressions we are dealing with.  */
5171 static int num_stores;
5172
5173 /* Checks to set if we need to mark a register set.  Called from
5174    note_stores.  */
5175
5176 static void
5177 reg_set_info (rtx dest, const_rtx setter ATTRIBUTE_UNUSED,
5178               void *data ATTRIBUTE_UNUSED)
5179 {
5180   if (GET_CODE (dest) == SUBREG)
5181     dest = SUBREG_REG (dest);
5182
5183   if (REG_P (dest))
5184     regvec[REGNO (dest)] = INSN_UID (compute_store_table_current_insn);
5185 }
5186
5187 /* Clear any mark that says that this insn sets dest.  Called from
5188    note_stores.  */
5189
5190 static void
5191 reg_clear_last_set (rtx dest, const_rtx setter ATTRIBUTE_UNUSED,
5192               void *data)
5193 {
5194   int *dead_vec = (int *) data;
5195
5196   if (GET_CODE (dest) == SUBREG)
5197     dest = SUBREG_REG (dest);
5198
5199   if (REG_P (dest) &&
5200       dead_vec[REGNO (dest)] == INSN_UID (compute_store_table_current_insn))
5201     dead_vec[REGNO (dest)] = 0;
5202 }
5203
5204 /* Return zero if some of the registers in list X are killed
5205    due to set of registers in bitmap REGS_SET.  */
5206
5207 static bool
5208 store_ops_ok (const_rtx x, int *regs_set)
5209 {
5210   const_rtx reg;
5211
5212   for (; x; x = XEXP (x, 1))
5213     {
5214       reg = XEXP (x, 0);
5215       if (regs_set[REGNO(reg)])
5216         return false;
5217     }
5218
5219   return true;
5220 }
5221
5222 /* Returns a list of registers mentioned in X.  */
5223 static rtx
5224 extract_mentioned_regs (rtx x)
5225 {
5226   return extract_mentioned_regs_helper (x, NULL_RTX);
5227 }
5228
5229 /* Helper for extract_mentioned_regs; ACCUM is used to accumulate used
5230    registers.  */
5231 static rtx
5232 extract_mentioned_regs_helper (rtx x, rtx accum)
5233 {
5234   int i;
5235   enum rtx_code code;
5236   const char * fmt;
5237
5238   /* Repeat is used to turn tail-recursion into iteration.  */
5239  repeat:
5240
5241   if (x == 0)
5242     return accum;
5243
5244   code = GET_CODE (x);
5245   switch (code)
5246     {
5247     case REG:
5248       return alloc_EXPR_LIST (0, x, accum);
5249
5250     case MEM:
5251       x = XEXP (x, 0);
5252       goto repeat;
5253
5254     case PRE_DEC:
5255     case PRE_INC:
5256     case PRE_MODIFY:
5257     case POST_DEC:
5258     case POST_INC:
5259     case POST_MODIFY:
5260       /* We do not run this function with arguments having side effects.  */
5261       gcc_unreachable ();
5262
5263     case PC:
5264     case CC0: /*FIXME*/
5265     case CONST:
5266     case CONST_INT:
5267     case CONST_DOUBLE:
5268     case CONST_FIXED:
5269     case CONST_VECTOR:
5270     case SYMBOL_REF:
5271     case LABEL_REF:
5272     case ADDR_VEC:
5273     case ADDR_DIFF_VEC:
5274       return accum;
5275
5276     default:
5277       break;
5278     }
5279
5280   i = GET_RTX_LENGTH (code) - 1;
5281   fmt = GET_RTX_FORMAT (code);
5282
5283   for (; i >= 0; i--)
5284     {
5285       if (fmt[i] == 'e')
5286         {
5287           rtx tem = XEXP (x, i);
5288
5289           /* If we are about to do the last recursive call
5290              needed at this level, change it into iteration.  */
5291           if (i == 0)
5292             {
5293               x = tem;
5294               goto repeat;
5295             }
5296
5297           accum = extract_mentioned_regs_helper (tem, accum);
5298         }
5299       else if (fmt[i] == 'E')
5300         {
5301           int j;
5302
5303           for (j = 0; j < XVECLEN (x, i); j++)
5304             accum = extract_mentioned_regs_helper (XVECEXP (x, i, j), accum);
5305         }
5306     }
5307
5308   return accum;
5309 }
5310
5311 /* Determine whether INSN is MEM store pattern that we will consider moving.
5312    REGS_SET_BEFORE is bitmap of registers set before (and including) the
5313    current insn, REGS_SET_AFTER is bitmap of registers set after (and
5314    including) the insn in this basic block.  We must be passing through BB from
5315    head to end, as we are using this fact to speed things up.
5316
5317    The results are stored this way:
5318
5319    -- the first anticipatable expression is added into ANTIC_STORE_LIST
5320    -- if the processed expression is not anticipatable, NULL_RTX is added
5321       there instead, so that we can use it as indicator that no further
5322       expression of this type may be anticipatable
5323    -- if the expression is available, it is added as head of AVAIL_STORE_LIST;
5324       consequently, all of them but this head are dead and may be deleted.
5325    -- if the expression is not available, the insn due to that it fails to be
5326       available is stored in reaching_reg.
5327
5328    The things are complicated a bit by fact that there already may be stores
5329    to the same MEM from other blocks; also caller must take care of the
5330    necessary cleanup of the temporary markers after end of the basic block.
5331    */
5332
5333 static void
5334 find_moveable_store (rtx insn, int *regs_set_before, int *regs_set_after)
5335 {
5336   struct ls_expr * ptr;
5337   rtx dest, set, tmp;
5338   int check_anticipatable, check_available;
5339   basic_block bb = BLOCK_FOR_INSN (insn);
5340
5341   set = single_set (insn);
5342   if (!set)
5343     return;
5344
5345   dest = SET_DEST (set);
5346
5347   if (! MEM_P (dest) || MEM_VOLATILE_P (dest)
5348       || GET_MODE (dest) == BLKmode)
5349     return;
5350
5351   if (side_effects_p (dest))
5352     return;
5353
5354   /* If we are handling exceptions, we must be careful with memory references
5355      that may trap. If we are not, the behavior is undefined, so we may just
5356      continue.  */
5357   if (flag_non_call_exceptions && may_trap_p (dest))
5358     return;
5359
5360   /* Even if the destination cannot trap, the source may.  In this case we'd
5361      need to handle updating the REG_EH_REGION note.  */
5362   if (find_reg_note (insn, REG_EH_REGION, NULL_RTX))
5363     return;
5364
5365   /* Make sure that the SET_SRC of this store insns can be assigned to
5366      a register, or we will fail later on in replace_store_insn, which
5367      assumes that we can do this.  But sometimes the target machine has
5368      oddities like MEM read-modify-write instruction.  See for example
5369      PR24257.  */
5370   if (!can_assign_to_reg_p (SET_SRC (set)))
5371     return;
5372
5373   ptr = ldst_entry (dest);
5374   if (!ptr->pattern_regs)
5375     ptr->pattern_regs = extract_mentioned_regs (dest);
5376
5377   /* Do not check for anticipatability if we either found one anticipatable
5378      store already, or tested for one and found out that it was killed.  */
5379   check_anticipatable = 0;
5380   if (!ANTIC_STORE_LIST (ptr))
5381     check_anticipatable = 1;
5382   else
5383     {
5384       tmp = XEXP (ANTIC_STORE_LIST (ptr), 0);
5385       if (tmp != NULL_RTX
5386           && BLOCK_FOR_INSN (tmp) != bb)
5387         check_anticipatable = 1;
5388     }
5389   if (check_anticipatable)
5390     {
5391       if (store_killed_before (dest, ptr->pattern_regs, insn, bb, regs_set_before))
5392         tmp = NULL_RTX;
5393       else
5394         tmp = insn;
5395       ANTIC_STORE_LIST (ptr) = alloc_INSN_LIST (tmp,
5396                                                 ANTIC_STORE_LIST (ptr));
5397     }
5398
5399   /* It is not necessary to check whether store is available if we did
5400      it successfully before; if we failed before, do not bother to check
5401      until we reach the insn that caused us to fail.  */
5402   check_available = 0;
5403   if (!AVAIL_STORE_LIST (ptr))
5404     check_available = 1;
5405   else
5406     {
5407       tmp = XEXP (AVAIL_STORE_LIST (ptr), 0);
5408       if (BLOCK_FOR_INSN (tmp) != bb)
5409         check_available = 1;
5410     }
5411   if (check_available)
5412     {
5413       /* Check that we have already reached the insn at that the check
5414          failed last time.  */
5415       if (LAST_AVAIL_CHECK_FAILURE (ptr))
5416         {
5417           for (tmp = BB_END (bb);
5418                tmp != insn && tmp != LAST_AVAIL_CHECK_FAILURE (ptr);
5419                tmp = PREV_INSN (tmp))
5420             continue;
5421           if (tmp == insn)
5422             check_available = 0;
5423         }
5424       else
5425         check_available = store_killed_after (dest, ptr->pattern_regs, insn,
5426                                               bb, regs_set_after,
5427                                               &LAST_AVAIL_CHECK_FAILURE (ptr));
5428     }
5429   if (!check_available)
5430     AVAIL_STORE_LIST (ptr) = alloc_INSN_LIST (insn, AVAIL_STORE_LIST (ptr));
5431 }
5432
5433 /* Find available and anticipatable stores.  */
5434
5435 static int
5436 compute_store_table (void)
5437 {
5438   int ret;
5439   basic_block bb;
5440   unsigned regno;
5441   rtx insn, pat, tmp;
5442   int *last_set_in, *already_set;
5443   struct ls_expr * ptr, **prev_next_ptr_ptr;
5444
5445   max_gcse_regno = max_reg_num ();
5446
5447   pre_ldst_mems = 0;
5448   pre_ldst_table = htab_create (13, pre_ldst_expr_hash,
5449                                 pre_ldst_expr_eq, NULL);
5450   last_set_in = XCNEWVEC (int, max_gcse_regno);
5451   already_set = XNEWVEC (int, max_gcse_regno);
5452
5453   /* Find all the stores we care about.  */
5454   FOR_EACH_BB (bb)
5455     {
5456       /* First compute the registers set in this block.  */
5457       regvec = last_set_in;
5458
5459       FOR_BB_INSNS (bb, insn)
5460         {
5461           if (! INSN_P (insn))
5462             continue;
5463
5464           if (CALL_P (insn))
5465             {
5466               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5467                 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
5468                   last_set_in[regno] = INSN_UID (insn);
5469             }
5470
5471           pat = PATTERN (insn);
5472           compute_store_table_current_insn = insn;
5473           note_stores (pat, reg_set_info, NULL);
5474         }
5475
5476       /* Now find the stores.  */
5477       memset (already_set, 0, sizeof (int) * max_gcse_regno);
5478       regvec = already_set;
5479       FOR_BB_INSNS (bb, insn)
5480         {
5481           if (! INSN_P (insn))
5482             continue;
5483
5484           if (CALL_P (insn))
5485             {
5486               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5487                 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
5488                   already_set[regno] = 1;
5489             }
5490
5491           pat = PATTERN (insn);
5492           note_stores (pat, reg_set_info, NULL);
5493
5494           /* Now that we've marked regs, look for stores.  */
5495           find_moveable_store (insn, already_set, last_set_in);
5496
5497           /* Unmark regs that are no longer set.  */
5498           compute_store_table_current_insn = insn;
5499           note_stores (pat, reg_clear_last_set, last_set_in);
5500           if (CALL_P (insn))
5501             {
5502               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
5503                 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno)
5504                     && last_set_in[regno] == INSN_UID (insn))
5505                   last_set_in[regno] = 0;
5506             }
5507         }
5508
5509 #ifdef ENABLE_CHECKING
5510       /* last_set_in should now be all-zero.  */
5511       for (regno = 0; regno < max_gcse_regno; regno++)
5512         gcc_assert (!last_set_in[regno]);
5513 #endif
5514
5515       /* Clear temporary marks.  */
5516       for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
5517         {
5518           LAST_AVAIL_CHECK_FAILURE(ptr) = NULL_RTX;
5519           if (ANTIC_STORE_LIST (ptr)
5520               && (tmp = XEXP (ANTIC_STORE_LIST (ptr), 0)) == NULL_RTX)
5521             ANTIC_STORE_LIST (ptr) = XEXP (ANTIC_STORE_LIST (ptr), 1);
5522         }
5523     }
5524
5525   /* Remove the stores that are not available anywhere, as there will
5526      be no opportunity to optimize them.  */
5527   for (ptr = pre_ldst_mems, prev_next_ptr_ptr = &pre_ldst_mems;
5528        ptr != NULL;
5529        ptr = *prev_next_ptr_ptr)
5530     {
5531       if (!AVAIL_STORE_LIST (ptr))
5532         {
5533           *prev_next_ptr_ptr = ptr->next;
5534           htab_remove_elt_with_hash (pre_ldst_table, ptr, ptr->hash_index);
5535           free_ldst_entry (ptr);
5536         }
5537       else
5538         prev_next_ptr_ptr = &ptr->next;
5539     }
5540
5541   ret = enumerate_ldsts ();
5542
5543   if (dump_file)
5544     {
5545       fprintf (dump_file, "ST_avail and ST_antic (shown under loads..)\n");
5546       print_ldst_list (dump_file);
5547     }
5548
5549   free (last_set_in);
5550   free (already_set);
5551   return ret;
5552 }
5553
5554 /* Check to see if the load X is aliased with STORE_PATTERN.
5555    AFTER is true if we are checking the case when STORE_PATTERN occurs
5556    after the X.  */
5557
5558 static bool
5559 load_kills_store (const_rtx x, const_rtx store_pattern, int after)
5560 {
5561   if (after)
5562     return anti_dependence (x, store_pattern);
5563   else
5564     return true_dependence (store_pattern, GET_MODE (store_pattern), x,
5565                             rtx_addr_varies_p);
5566 }
5567
5568 /* Go through the entire insn X, looking for any loads which might alias
5569    STORE_PATTERN.  Return true if found.
5570    AFTER is true if we are checking the case when STORE_PATTERN occurs
5571    after the insn X.  */
5572
5573 static bool
5574 find_loads (const_rtx x, const_rtx store_pattern, int after)
5575 {
5576   const char * fmt;
5577   int i, j;
5578   int ret = false;
5579
5580   if (!x)
5581     return false;
5582
5583   if (GET_CODE (x) == SET)
5584     x = SET_SRC (x);
5585
5586   if (MEM_P (x))
5587     {
5588       if (load_kills_store (x, store_pattern, after))
5589         return true;
5590     }
5591
5592   /* Recursively process the insn.  */
5593   fmt = GET_RTX_FORMAT (GET_CODE (x));
5594
5595   for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0 && !ret; i--)
5596     {
5597       if (fmt[i] == 'e')
5598         ret |= find_loads (XEXP (x, i), store_pattern, after);
5599       else if (fmt[i] == 'E')
5600         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5601           ret |= find_loads (XVECEXP (x, i, j), store_pattern, after);
5602     }
5603   return ret;
5604 }
5605
5606 static inline bool
5607 store_killed_in_pat (const_rtx x, const_rtx pat, int after)
5608 {
5609   if (GET_CODE (pat) == SET)
5610     {
5611       rtx dest = SET_DEST (pat);
5612
5613       if (GET_CODE (dest) == ZERO_EXTRACT)
5614         dest = XEXP (dest, 0);
5615
5616       /* Check for memory stores to aliased objects.  */
5617       if (MEM_P (dest)
5618           && !expr_equiv_p (dest, x))
5619         {
5620           if (after)
5621             {
5622               if (output_dependence (dest, x))
5623                 return true;
5624             }
5625           else
5626             {
5627               if (output_dependence (x, dest))
5628                 return true;
5629             }
5630         }
5631     }
5632
5633   if (find_loads (pat, x, after))
5634     return true;
5635
5636   return false;
5637 }
5638
5639 /* Check if INSN kills the store pattern X (is aliased with it).
5640    AFTER is true if we are checking the case when store X occurs
5641    after the insn.  Return true if it does.  */
5642
5643 static bool
5644 store_killed_in_insn (const_rtx x, const_rtx x_regs, const_rtx insn, int after)
5645 {
5646   const_rtx reg, base, note, pat;
5647
5648   if (!INSN_P (insn))
5649     return false;
5650
5651   if (CALL_P (insn))
5652     {
5653       /* A normal or pure call might read from pattern,
5654          but a const call will not.  */
5655       if (!RTL_CONST_CALL_P (insn))
5656         return true;
5657
5658       /* But even a const call reads its parameters.  Check whether the
5659          base of some of registers used in mem is stack pointer.  */
5660       for (reg = x_regs; reg; reg = XEXP (reg, 1))
5661         {
5662           base = find_base_term (XEXP (reg, 0));
5663           if (!base
5664               || (GET_CODE (base) == ADDRESS
5665                   && GET_MODE (base) == Pmode
5666                   && XEXP (base, 0) == stack_pointer_rtx))
5667             return true;
5668         }
5669
5670       return false;
5671     }
5672
5673   pat = PATTERN (insn);
5674   if (GET_CODE (pat) == SET)
5675     {
5676       if (store_killed_in_pat (x, pat, after))
5677         return true;
5678     }
5679   else if (GET_CODE (pat) == PARALLEL)
5680     {
5681       int i;
5682
5683       for (i = 0; i < XVECLEN (pat, 0); i++)
5684         if (store_killed_in_pat (x, XVECEXP (pat, 0, i), after))
5685           return true;
5686     }
5687   else if (find_loads (PATTERN (insn), x, after))
5688     return true;
5689
5690   /* If this insn has a REG_EQUAL or REG_EQUIV note referencing a memory
5691      location aliased with X, then this insn kills X.  */
5692   note = find_reg_equal_equiv_note (insn);
5693   if (! note)
5694     return false;
5695   note = XEXP (note, 0);
5696
5697   /* However, if the note represents a must alias rather than a may
5698      alias relationship, then it does not kill X.  */
5699   if (expr_equiv_p (note, x))
5700     return false;
5701
5702   /* See if there are any aliased loads in the note.  */
5703   return find_loads (note, x, after);
5704 }
5705
5706 /* Returns true if the expression X is loaded or clobbered on or after INSN
5707    within basic block BB.  REGS_SET_AFTER is bitmap of registers set in
5708    or after the insn.  X_REGS is list of registers mentioned in X. If the store
5709    is killed, return the last insn in that it occurs in FAIL_INSN.  */
5710
5711 static bool
5712 store_killed_after (const_rtx x, const_rtx x_regs, const_rtx insn, const_basic_block bb,
5713                     int *regs_set_after, rtx *fail_insn)
5714 {
5715   rtx last = BB_END (bb), act;
5716
5717   if (!store_ops_ok (x_regs, regs_set_after))
5718     {
5719       /* We do not know where it will happen.  */
5720       if (fail_insn)
5721         *fail_insn = NULL_RTX;
5722       return true;
5723     }
5724
5725   /* Scan from the end, so that fail_insn is determined correctly.  */
5726   for (act = last; act != PREV_INSN (insn); act = PREV_INSN (act))
5727     if (store_killed_in_insn (x, x_regs, act, false))
5728       {
5729         if (fail_insn)
5730           *fail_insn = act;
5731         return true;
5732       }
5733
5734   return false;
5735 }
5736
5737 /* Returns true if the expression X is loaded or clobbered on or before INSN
5738    within basic block BB. X_REGS is list of registers mentioned in X.
5739    REGS_SET_BEFORE is bitmap of registers set before or in this insn.  */
5740 static bool
5741 store_killed_before (const_rtx x, const_rtx x_regs, const_rtx insn, const_basic_block bb,
5742                      int *regs_set_before)
5743 {
5744   rtx first = BB_HEAD (bb);
5745
5746   if (!store_ops_ok (x_regs, regs_set_before))
5747     return true;
5748
5749   for ( ; insn != PREV_INSN (first); insn = PREV_INSN (insn))
5750     if (store_killed_in_insn (x, x_regs, insn, true))
5751       return true;
5752
5753   return false;
5754 }
5755
5756 /* Fill in available, anticipatable, transparent and kill vectors in
5757    STORE_DATA, based on lists of available and anticipatable stores.  */
5758 static void
5759 build_store_vectors (void)
5760 {
5761   basic_block bb;
5762   int *regs_set_in_block;
5763   rtx insn, st;
5764   struct ls_expr * ptr;
5765
5766   /* Build the gen_vector. This is any store in the table which is not killed
5767      by aliasing later in its block.  */
5768   ae_gen = sbitmap_vector_alloc (last_basic_block, num_stores);
5769   sbitmap_vector_zero (ae_gen, last_basic_block);
5770
5771   st_antloc = sbitmap_vector_alloc (last_basic_block, num_stores);
5772   sbitmap_vector_zero (st_antloc, last_basic_block);
5773
5774   for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
5775     {
5776       for (st = AVAIL_STORE_LIST (ptr); st != NULL; st = XEXP (st, 1))
5777         {
5778           insn = XEXP (st, 0);
5779           bb = BLOCK_FOR_INSN (insn);
5780
5781           /* If we've already seen an available expression in this block,
5782              we can delete this one (It occurs earlier in the block). We'll
5783              copy the SRC expression to an unused register in case there
5784              are any side effects.  */
5785           if (TEST_BIT (ae_gen[bb->index], ptr->index))
5786             {
5787               rtx r = gen_reg_rtx_and_attrs (ptr->pattern);
5788               if (dump_file)
5789                 fprintf (dump_file, "Removing redundant store:\n");
5790               replace_store_insn (r, XEXP (st, 0), bb, ptr);
5791               continue;
5792             }
5793           SET_BIT (ae_gen[bb->index], ptr->index);
5794         }
5795
5796       for (st = ANTIC_STORE_LIST (ptr); st != NULL; st = XEXP (st, 1))
5797         {
5798           insn = XEXP (st, 0);
5799           bb = BLOCK_FOR_INSN (insn);
5800           SET_BIT (st_antloc[bb->index], ptr->index);
5801         }
5802     }
5803
5804   ae_kill = sbitmap_vector_alloc (last_basic_block, num_stores);
5805   sbitmap_vector_zero (ae_kill, last_basic_block);
5806
5807   transp = sbitmap_vector_alloc (last_basic_block, num_stores);
5808   sbitmap_vector_zero (transp, last_basic_block);
5809   regs_set_in_block = XNEWVEC (int, max_gcse_regno);
5810
5811   FOR_EACH_BB (bb)
5812     {
5813       FOR_BB_INSNS (bb, insn)
5814         if (INSN_P (insn))
5815           {
5816             df_ref *def_rec;
5817             for (def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++)
5818               {
5819                 unsigned int ref_regno = DF_REF_REGNO (*def_rec);
5820                 if (ref_regno < max_gcse_regno)
5821                   regs_set_in_block[DF_REF_REGNO (*def_rec)] = 1;
5822               }
5823           }
5824
5825       for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
5826         {
5827           if (store_killed_after (ptr->pattern, ptr->pattern_regs, BB_HEAD (bb),
5828                                   bb, regs_set_in_block, NULL))
5829             {
5830               /* It should not be necessary to consider the expression
5831                  killed if it is both anticipatable and available.  */
5832               if (!TEST_BIT (st_antloc[bb->index], ptr->index)
5833                   || !TEST_BIT (ae_gen[bb->index], ptr->index))
5834                 SET_BIT (ae_kill[bb->index], ptr->index);
5835             }
5836           else
5837             SET_BIT (transp[bb->index], ptr->index);
5838         }
5839     }
5840
5841   free (regs_set_in_block);
5842
5843   if (dump_file)
5844     {
5845       dump_sbitmap_vector (dump_file, "st_antloc", "", st_antloc, last_basic_block);
5846       dump_sbitmap_vector (dump_file, "st_kill", "", ae_kill, last_basic_block);
5847       dump_sbitmap_vector (dump_file, "Transpt", "", transp, last_basic_block);
5848       dump_sbitmap_vector (dump_file, "st_avloc", "", ae_gen, last_basic_block);
5849     }
5850 }
5851
5852 /* Insert an instruction at the beginning of a basic block, and update
5853    the BB_HEAD if needed.  */
5854
5855 static void
5856 insert_insn_start_basic_block (rtx insn, basic_block bb)
5857 {
5858   /* Insert at start of successor block.  */
5859   rtx prev = PREV_INSN (BB_HEAD (bb));
5860   rtx before = BB_HEAD (bb);
5861   while (before != 0)
5862     {
5863       if (! LABEL_P (before)
5864           && !NOTE_INSN_BASIC_BLOCK_P (before))
5865         break;
5866       prev = before;
5867       if (prev == BB_END (bb))
5868         break;
5869       before = NEXT_INSN (before);
5870     }
5871
5872   insn = emit_insn_after_noloc (insn, prev, bb);
5873
5874   if (dump_file)
5875     {
5876       fprintf (dump_file, "STORE_MOTION  insert store at start of BB %d:\n",
5877                bb->index);
5878       print_inline_rtx (dump_file, insn, 6);
5879       fprintf (dump_file, "\n");
5880     }
5881 }
5882
5883 /* This routine will insert a store on an edge. EXPR is the ldst entry for
5884    the memory reference, and E is the edge to insert it on.  Returns nonzero
5885    if an edge insertion was performed.  */
5886
5887 static int
5888 insert_store (struct ls_expr * expr, edge e)
5889 {
5890   rtx reg, insn;
5891   basic_block bb;
5892   edge tmp;
5893   edge_iterator ei;
5894
5895   /* We did all the deleted before this insert, so if we didn't delete a
5896      store, then we haven't set the reaching reg yet either.  */
5897   if (expr->reaching_reg == NULL_RTX)
5898     return 0;
5899
5900   if (e->flags & EDGE_FAKE)
5901     return 0;
5902
5903   reg = expr->reaching_reg;
5904   insn = gen_move_insn (copy_rtx (expr->pattern), reg);
5905
5906   /* If we are inserting this expression on ALL predecessor edges of a BB,
5907      insert it at the start of the BB, and reset the insert bits on the other
5908      edges so we don't try to insert it on the other edges.  */
5909   bb = e->dest;
5910   FOR_EACH_EDGE (tmp, ei, e->dest->preds)
5911     if (!(tmp->flags & EDGE_FAKE))
5912       {
5913         int index = EDGE_INDEX (edge_list, tmp->src, tmp->dest);
5914         
5915         gcc_assert (index != EDGE_INDEX_NO_EDGE);
5916         if (! TEST_BIT (pre_insert_map[index], expr->index))
5917           break;
5918       }
5919
5920   /* If tmp is NULL, we found an insertion on every edge, blank the
5921      insertion vector for these edges, and insert at the start of the BB.  */
5922   if (!tmp && bb != EXIT_BLOCK_PTR)
5923     {
5924       FOR_EACH_EDGE (tmp, ei, e->dest->preds)
5925         {
5926           int index = EDGE_INDEX (edge_list, tmp->src, tmp->dest);
5927           RESET_BIT (pre_insert_map[index], expr->index);
5928         }
5929       insert_insn_start_basic_block (insn, bb);
5930       return 0;
5931     }
5932
5933   /* We can't put stores in the front of blocks pointed to by abnormal
5934      edges since that may put a store where one didn't used to be.  */
5935   gcc_assert (!(e->flags & EDGE_ABNORMAL));
5936
5937   insert_insn_on_edge (insn, e);
5938
5939   if (dump_file)
5940     {
5941       fprintf (dump_file, "STORE_MOTION  insert insn on edge (%d, %d):\n",
5942                e->src->index, e->dest->index);
5943       print_inline_rtx (dump_file, insn, 6);
5944       fprintf (dump_file, "\n");
5945     }
5946
5947   return 1;
5948 }
5949
5950 /* Remove any REG_EQUAL or REG_EQUIV notes containing a reference to the
5951    memory location in SMEXPR set in basic block BB.
5952
5953    This could be rather expensive.  */
5954
5955 static void
5956 remove_reachable_equiv_notes (basic_block bb, struct ls_expr *smexpr)
5957 {
5958   edge_iterator *stack, ei;
5959   int sp;
5960   edge act;
5961   sbitmap visited = sbitmap_alloc (last_basic_block);
5962   rtx last, insn, note;
5963   rtx mem = smexpr->pattern;
5964
5965   stack = XNEWVEC (edge_iterator, n_basic_blocks);
5966   sp = 0;
5967   ei = ei_start (bb->succs);
5968
5969   sbitmap_zero (visited);
5970
5971   act = (EDGE_COUNT (ei_container (ei)) > 0 ? EDGE_I (ei_container (ei), 0) : NULL);
5972   while (1)
5973     {
5974       if (!act)
5975         {
5976           if (!sp)
5977             {
5978               free (stack);
5979               sbitmap_free (visited);
5980               return;
5981             }
5982           act = ei_edge (stack[--sp]);
5983         }
5984       bb = act->dest;
5985
5986       if (bb == EXIT_BLOCK_PTR
5987           || TEST_BIT (visited, bb->index))
5988         {
5989           if (!ei_end_p (ei))
5990               ei_next (&ei);
5991           act = (! ei_end_p (ei)) ? ei_edge (ei) : NULL;
5992           continue;
5993         }
5994       SET_BIT (visited, bb->index);
5995
5996       if (TEST_BIT (st_antloc[bb->index], smexpr->index))
5997         {
5998           for (last = ANTIC_STORE_LIST (smexpr);
5999                BLOCK_FOR_INSN (XEXP (last, 0)) != bb;
6000                last = XEXP (last, 1))
6001             continue;
6002           last = XEXP (last, 0);
6003         }
6004       else
6005         last = NEXT_INSN (BB_END (bb));
6006
6007       for (insn = BB_HEAD (bb); insn != last; insn = NEXT_INSN (insn))
6008         if (INSN_P (insn))
6009           {
6010             note = find_reg_equal_equiv_note (insn);
6011             if (!note || !expr_equiv_p (XEXP (note, 0), mem))
6012               continue;
6013
6014             if (dump_file)
6015               fprintf (dump_file, "STORE_MOTION  drop REG_EQUAL note at insn %d:\n",
6016                        INSN_UID (insn));
6017             remove_note (insn, note);
6018           }
6019
6020       if (!ei_end_p (ei))
6021         ei_next (&ei);
6022       act = (! ei_end_p (ei)) ? ei_edge (ei) : NULL;
6023
6024       if (EDGE_COUNT (bb->succs) > 0)
6025         {
6026           if (act)
6027             stack[sp++] = ei;
6028           ei = ei_start (bb->succs);
6029           act = (EDGE_COUNT (ei_container (ei)) > 0 ? EDGE_I (ei_container (ei), 0) : NULL);
6030         }
6031     }
6032 }
6033
6034 /* This routine will replace a store with a SET to a specified register.  */
6035
6036 static void
6037 replace_store_insn (rtx reg, rtx del, basic_block bb, struct ls_expr *smexpr)
6038 {
6039   rtx insn, mem, note, set, ptr;
6040
6041   mem = smexpr->pattern;
6042   insn = gen_move_insn (reg, SET_SRC (single_set (del)));
6043
6044   for (ptr = ANTIC_STORE_LIST (smexpr); ptr; ptr = XEXP (ptr, 1))
6045     if (XEXP (ptr, 0) == del)
6046       {
6047         XEXP (ptr, 0) = insn;
6048         break;
6049       }
6050
6051   /* Move the notes from the deleted insn to its replacement.  */
6052   REG_NOTES (insn) = REG_NOTES (del);
6053
6054   /* Emit the insn AFTER all the notes are transferred.
6055      This is cheaper since we avoid df rescanning for the note change.  */
6056   insn = emit_insn_after (insn, del);
6057
6058   if (dump_file)
6059     {
6060       fprintf (dump_file,
6061                "STORE_MOTION  delete insn in BB %d:\n      ", bb->index);
6062       print_inline_rtx (dump_file, del, 6);
6063       fprintf (dump_file, "\nSTORE MOTION  replaced with insn:\n      ");
6064       print_inline_rtx (dump_file, insn, 6);
6065       fprintf (dump_file, "\n");
6066     }
6067
6068   delete_insn (del);
6069
6070   /* Now we must handle REG_EQUAL notes whose contents is equal to the mem;
6071      they are no longer accurate provided that they are reached by this
6072      definition, so drop them.  */
6073   for (; insn != NEXT_INSN (BB_END (bb)); insn = NEXT_INSN (insn))
6074     if (INSN_P (insn))
6075       {
6076         set = single_set (insn);
6077         if (!set)
6078           continue;
6079         if (expr_equiv_p (SET_DEST (set), mem))
6080           return;
6081         note = find_reg_equal_equiv_note (insn);
6082         if (!note || !expr_equiv_p (XEXP (note, 0), mem))
6083           continue;
6084
6085         if (dump_file)
6086           fprintf (dump_file, "STORE_MOTION  drop REG_EQUAL note at insn %d:\n",
6087                    INSN_UID (insn));
6088         remove_note (insn, note);
6089       }
6090   remove_reachable_equiv_notes (bb, smexpr);
6091 }
6092
6093
6094 /* Delete a store, but copy the value that would have been stored into
6095    the reaching_reg for later storing.  */
6096
6097 static void
6098 delete_store (struct ls_expr * expr, basic_block bb)
6099 {
6100   rtx reg, i, del;
6101
6102   if (expr->reaching_reg == NULL_RTX)
6103     expr->reaching_reg = gen_reg_rtx_and_attrs (expr->pattern);
6104
6105   reg = expr->reaching_reg;
6106
6107   for (i = AVAIL_STORE_LIST (expr); i; i = XEXP (i, 1))
6108     {
6109       del = XEXP (i, 0);
6110       if (BLOCK_FOR_INSN (del) == bb)
6111         {
6112           /* We know there is only one since we deleted redundant
6113              ones during the available computation.  */
6114           replace_store_insn (reg, del, bb, expr);
6115           break;
6116         }
6117     }
6118 }
6119
6120 /* Free memory used by store motion.  */
6121
6122 static void
6123 free_store_memory (void)
6124 {
6125   free_ldst_mems ();
6126
6127   if (ae_gen)
6128     sbitmap_vector_free (ae_gen);
6129   if (ae_kill)
6130     sbitmap_vector_free (ae_kill);
6131   if (transp)
6132     sbitmap_vector_free (transp);
6133   if (st_antloc)
6134     sbitmap_vector_free (st_antloc);
6135   if (pre_insert_map)
6136     sbitmap_vector_free (pre_insert_map);
6137   if (pre_delete_map)
6138     sbitmap_vector_free (pre_delete_map);
6139
6140   ae_gen = ae_kill = transp = st_antloc = NULL;
6141   pre_insert_map = pre_delete_map = NULL;
6142 }
6143
6144 /* Perform store motion. Much like gcse, except we move expressions the
6145    other way by looking at the flowgraph in reverse.  */
6146
6147 static void
6148 store_motion (void)
6149 {
6150   basic_block bb;
6151   int x;
6152   struct ls_expr * ptr;
6153   int update_flow = 0;
6154
6155   if (dump_file)
6156     {
6157       fprintf (dump_file, "before store motion\n");
6158       print_rtl (dump_file, get_insns ());
6159     }
6160
6161   init_alias_analysis ();
6162
6163   /* Find all the available and anticipatable stores.  */
6164   num_stores = compute_store_table ();
6165   if (num_stores == 0)
6166     {
6167       htab_delete (pre_ldst_table);
6168       pre_ldst_table = NULL;
6169       end_alias_analysis ();
6170       return;
6171     }
6172
6173   /* Now compute kill & transp vectors.  */
6174   build_store_vectors ();
6175   add_noreturn_fake_exit_edges ();
6176   connect_infinite_loops_to_exit ();
6177
6178   edge_list = pre_edge_rev_lcm (num_stores, transp, ae_gen,
6179                                 st_antloc, ae_kill, &pre_insert_map,
6180                                 &pre_delete_map);
6181
6182   /* Now we want to insert the new stores which are going to be needed.  */
6183   for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
6184     {
6185       /* If any of the edges we have above are abnormal, we can't move this
6186          store.  */
6187       for (x = NUM_EDGES (edge_list) - 1; x >= 0; x--)
6188         if (TEST_BIT (pre_insert_map[x], ptr->index)
6189             && (INDEX_EDGE (edge_list, x)->flags & EDGE_ABNORMAL))
6190           break;
6191
6192       if (x >= 0)
6193         {
6194           if (dump_file != NULL)
6195             fprintf (dump_file,
6196                      "Can't replace store %d: abnormal edge from %d to %d\n",
6197                      ptr->index, INDEX_EDGE (edge_list, x)->src->index,
6198                      INDEX_EDGE (edge_list, x)->dest->index);
6199           continue;
6200         }
6201                       
6202       /* Now we want to insert the new stores which are going to be needed.  */
6203
6204       FOR_EACH_BB (bb)
6205         if (TEST_BIT (pre_delete_map[bb->index], ptr->index))
6206           delete_store (ptr, bb);
6207
6208       for (x = 0; x < NUM_EDGES (edge_list); x++)
6209         if (TEST_BIT (pre_insert_map[x], ptr->index))
6210           update_flow |= insert_store (ptr, INDEX_EDGE (edge_list, x));
6211     }
6212
6213   if (update_flow)
6214     commit_edge_insertions ();
6215
6216   free_store_memory ();
6217   free_edge_list (edge_list);
6218   remove_fake_exit_edges ();
6219   end_alias_analysis ();
6220 }
6221
6222 \f
6223 /* Entry point for jump bypassing optimization pass.  */
6224
6225 static int
6226 bypass_jumps (void)
6227 {
6228   int changed;
6229
6230   /* We do not construct an accurate cfg in functions which call
6231      setjmp, so just punt to be safe.  */
6232   if (cfun->calls_setjmp)
6233     return 0;
6234
6235   /* Identify the basic block information for this function, including
6236      successors and predecessors.  */
6237   max_gcse_regno = max_reg_num ();
6238
6239   if (dump_file)
6240     dump_flow_info (dump_file, dump_flags);
6241
6242   /* Return if there's nothing to do, or it is too expensive.  */
6243   if (n_basic_blocks <= NUM_FIXED_BLOCKS + 1
6244       || is_too_expensive (_ ("jump bypassing disabled")))
6245     return 0;
6246
6247   gcc_obstack_init (&gcse_obstack);
6248   bytes_used = 0;
6249
6250   /* We need alias.  */
6251   init_alias_analysis ();
6252
6253   max_gcse_regno = max_reg_num ();
6254   alloc_gcse_mem ();
6255   changed = one_cprop_pass (3, true, true);
6256   free_gcse_mem ();
6257
6258   if (dump_file)
6259     {
6260       fprintf (dump_file, "BYPASS of %s: %d basic blocks, ",
6261                current_function_name (), n_basic_blocks);
6262       fprintf (dump_file, "%d bytes\n\n", bytes_used);
6263     }
6264
6265   obstack_free (&gcse_obstack, NULL);
6266
6267   /* We are finished with alias.  */
6268   end_alias_analysis ();
6269
6270   return changed;
6271 }
6272
6273 /* Return true if the graph is too expensive to optimize. PASS is the
6274    optimization about to be performed.  */
6275
6276 static bool
6277 is_too_expensive (const char *pass)
6278 {
6279   /* Trying to perform global optimizations on flow graphs which have
6280      a high connectivity will take a long time and is unlikely to be
6281      particularly useful.
6282
6283      In normal circumstances a cfg should have about twice as many
6284      edges as blocks.  But we do not want to punish small functions
6285      which have a couple switch statements.  Rather than simply
6286      threshold the number of blocks, uses something with a more
6287      graceful degradation.  */
6288   if (n_edges > 20000 + n_basic_blocks * 4)
6289     {
6290       warning (OPT_Wdisabled_optimization,
6291                "%s: %d basic blocks and %d edges/basic block",
6292                pass, n_basic_blocks, n_edges / n_basic_blocks);
6293
6294       return true;
6295     }
6296
6297   /* If allocating memory for the cprop bitmap would take up too much
6298      storage it's better just to disable the optimization.  */
6299   if ((n_basic_blocks
6300        * SBITMAP_SET_SIZE (max_reg_num ())
6301        * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
6302     {
6303       warning (OPT_Wdisabled_optimization,
6304                "%s: %d basic blocks and %d registers",
6305                pass, n_basic_blocks, max_reg_num ());
6306
6307       return true;
6308     }
6309
6310   return false;
6311 }
6312 \f
6313 static bool
6314 gate_handle_jump_bypass (void)
6315 {
6316   return optimize > 0 && flag_gcse
6317     && dbg_cnt (jump_bypass);
6318 }
6319
6320 /* Perform jump bypassing and control flow optimizations.  */
6321 static unsigned int
6322 rest_of_handle_jump_bypass (void)
6323 {
6324   delete_unreachable_blocks ();
6325   if (bypass_jumps ())
6326     {
6327       delete_trivially_dead_insns (get_insns (), max_reg_num ());
6328       rebuild_jump_labels (get_insns ());
6329       cleanup_cfg (0);
6330     }
6331   return 0;
6332 }
6333
6334 struct rtl_opt_pass pass_jump_bypass =
6335 {
6336  {
6337   RTL_PASS,
6338   "bypass",                             /* name */
6339   gate_handle_jump_bypass,              /* gate */   
6340   rest_of_handle_jump_bypass,           /* execute */       
6341   NULL,                                 /* sub */
6342   NULL,                                 /* next */
6343   0,                                    /* static_pass_number */
6344   TV_BYPASS,                            /* tv_id */
6345   PROP_cfglayout,                       /* properties_required */
6346   0,                                    /* properties_provided */
6347   0,                                    /* properties_destroyed */
6348   0,                                    /* todo_flags_start */
6349   TODO_dump_func |
6350   TODO_ggc_collect | TODO_verify_flow   /* todo_flags_finish */
6351  }
6352 };
6353
6354
6355 static bool
6356 gate_handle_gcse (void)
6357 {
6358   return optimize > 0 && flag_gcse
6359     && dbg_cnt (gcse);
6360 }
6361
6362
6363 static unsigned int
6364 rest_of_handle_gcse (void)
6365 {
6366   int save_csb, save_cfj;
6367   int tem2 = 0, tem;
6368   tem = gcse_main (get_insns ());
6369   delete_trivially_dead_insns (get_insns (), max_reg_num ());
6370   rebuild_jump_labels (get_insns ());
6371   save_csb = flag_cse_skip_blocks;
6372   save_cfj = flag_cse_follow_jumps;
6373   flag_cse_skip_blocks = flag_cse_follow_jumps = 0;
6374
6375   /* If -fexpensive-optimizations, re-run CSE to clean up things done
6376      by gcse.  */
6377   if (flag_expensive_optimizations)
6378     {
6379       timevar_push (TV_CSE);
6380       tem2 = cse_main (get_insns (), max_reg_num ());
6381       df_finish_pass (false);
6382       purge_all_dead_edges ();
6383       delete_trivially_dead_insns (get_insns (), max_reg_num ());
6384       timevar_pop (TV_CSE);
6385       cse_not_expected = !flag_rerun_cse_after_loop;
6386     }
6387
6388   /* If gcse or cse altered any jumps, rerun jump optimizations to clean
6389      things up.  */
6390   if (tem || tem2 == 2)
6391     {
6392       timevar_push (TV_JUMP);
6393       rebuild_jump_labels (get_insns ());
6394       cleanup_cfg (0);
6395       timevar_pop (TV_JUMP);
6396     }
6397   else if (tem2 == 1)
6398     cleanup_cfg (0);
6399
6400   flag_cse_skip_blocks = save_csb;
6401   flag_cse_follow_jumps = save_cfj;
6402   return 0;
6403 }
6404
6405 struct rtl_opt_pass pass_gcse =
6406 {
6407  {
6408   RTL_PASS,
6409   "gcse1",                              /* name */
6410   gate_handle_gcse,                     /* gate */   
6411   rest_of_handle_gcse,                  /* execute */       
6412   NULL,                                 /* sub */
6413   NULL,                                 /* next */
6414   0,                                    /* static_pass_number */
6415   TV_GCSE,                              /* tv_id */
6416   PROP_cfglayout,                       /* properties_required */
6417   0,                                    /* properties_provided */
6418   0,                                    /* properties_destroyed */
6419   0,                                    /* todo_flags_start */
6420   TODO_df_finish | TODO_verify_rtl_sharing |
6421   TODO_dump_func |
6422   TODO_verify_flow | TODO_ggc_collect   /* todo_flags_finish */
6423  }
6424 };
6425
6426
6427 #include "gt-gcse.h"