OSDN Git Service

* calls.c (expand_call): Initialize "src" and "dest".
[pf3gnuchains/gcc-fork.git] / gcc / gcse.c
1 /* Global common subexpression elimination
2    and global constant/copy propagation for GNU compiler.
3    Copyright (C) 1997 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
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    - memory aliasing support
28    - ability to realloc sbitmap vectors would allow one initial computation
29      of reg_set_in_block with only subsequent additions, rather than
30      recomputing it for each pass
31
32    NOTES
33    - the classic gcse implementation is kept in for now for comparison
34 */
35
36 /* References searched while implementing this.
37    This list will eventually be deleted but I wanted to have a record of it
38    for now.
39
40    Compilers Principles, Techniques and Tools
41    Aho, Sethi, Ullman
42    Addison-Wesley, 1988
43
44    Global Optimization by Suppression of Partial Redundancies
45    E. Morel, C. Renvoise
46    communications of the acm, Vol. 22, Num. 2, Feb. 1979
47
48    A Portable Machine-Independent Global Optimizer - Design and Measurements
49    Frederick Chow
50    Stanford Ph.D. thesis, Dec. 1983
51
52 xxx
53    Elimination Algorithms for Data Flow Analysis
54    B.G. Ryder, M.C. Paull
55    ACM Computing Surveys, Vol. 18, Num. 3, Sep. 1986
56
57 reread
58    A Fast Algorithm for Code Movement Optimization
59    D.M. Dhamdhere
60    SIGPLAN Notices, Vol. 23, Num. 10, Oct. 1988
61
62    A Solution to a Problem with Morel and Renvoise's
63    Global Optimization by Suppression of Partial Redundancies
64    K-H Drechsler, M.P. Stadel
65    ACM TOPLAS, Vol. 10, Num. 4, Oct. 1988
66
67    Practical Adaptation of the Global Optimization
68    Algorithm of Morel and Renvoise
69    D.M. Dhamdhere
70    ACM TOPLAS, Vol. 13, Num. 2. Apr. 1991
71
72    Efficiently Computing Static Single Assignment Form and the Control
73    Dependence Graph
74    R. Cytron, J. Ferrante, B.K. Rosen, M.N. Wegman, and F.K. Zadeck
75    ACM TOPLAS, Vol. 13, Num. 4, Oct. 1991
76
77 yyy
78    How to Analyze Large Programs Efficiently and Informatively
79    D.M. Dhamdhere, B.K. Rosen, F.K. Zadeck
80    ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
81
82    Lazy Code Motion
83    J. Knoop, O. Ruthing, B. Steffen
84    ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
85
86    What's In a Region?  Or Computing Control Dependence Regions in Near-Linear
87    Time for Reducible Flow Control
88    Thomas Ball
89    ACM Letters on Programming Languages and Systems,
90    Vol. 2, Num. 1-4, Mar-Dec 1993
91
92    An Efficient Representation for Sparse Sets
93    Preston Briggs, Linda Torczon
94    ACM Letters on Programming Languages and Systems,
95    Vol. 2, Num. 1-4, Mar-Dec 1993
96
97    A Variation of Knoop, Ruthing, and Steffen's Lazy Code Motion
98    K-H Drechsler, M.P. Stadel
99    ACM SIGPLAN Notices, Vol. 28, Num. 5, May 1993
100
101    Partial Dead Code Elimination
102    J. Knoop, O. Ruthing, B. Steffen
103    ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
104
105    Effective Partial Redundancy Elimination
106    P. Briggs, K.D. Cooper
107    ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
108
109    The Program Structure Tree: Computing Control Regions in Linear Time
110    R. Johnson, D. Pearson, K. Pingali
111    ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
112
113    Optimal Code Motion: Theory and Practice
114    J. Knoop, O. Ruthing, B. Steffen
115    ACM TOPLAS, Vol. 16, Num. 4, Jul. 1994
116
117    The power of assignment motion
118    J. Knoop, O. Ruthing, B. Steffen
119    ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
120
121    Global code motion / global value numbering
122    C. Click
123    ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
124
125    Value Driven Redundancy Elimination
126    L.T. Simpson
127    Rice University Ph.D. thesis, Apr. 1996
128
129    Value Numbering
130    L.T. Simpson
131    Massively Scalar Compiler Project, Rice University, Sep. 1996
132
133    High Performance Compilers for Parallel Computing
134    Michael Wolfe
135    Addison-Wesley, 1996
136
137    People wishing to speed up the code here should read xxx, yyy.
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 /* Must precede rtl.h for FFS.  */
144 #include "system.h"
145
146 #include "rtl.h"
147 #include "regs.h"
148 #include "hard-reg-set.h"
149 #include "flags.h"
150 #include "real.h"
151 #include "insn-config.h"
152 #include "recog.h"
153 #include "basic-block.h"
154 #include "output.h"
155
156 #include "obstack.h"
157 #define obstack_chunk_alloc gmalloc
158 #define obstack_chunk_free free
159
160 /* Maximum number of passes to perform.  */
161 #define MAX_PASSES 1
162
163 /* Propagate flow information through back edges and thus enable PRE's
164    moving loop invariant calculations out of loops.
165
166    Originally this tended to create worse overall code, but several
167    improvements during the development of PRE seem to have made following
168    back edges generally a win.
169
170    Note much of the loop invariant code motion done here would normally
171    be done by loop.c, which has more heuristics for when to move invariants
172    out of loops.  At some point we might need to move some of those
173    heuristics into gcse.c.  */
174 #define FOLLOW_BACK_EDGES 1
175
176 /* We support two GCSE implementations: Classic GCSE (i.e. Dragon Book)
177    and PRE (Partial Redundancy Elimination) GCSE (based on Fred Chow's thesis).
178    The default is PRE.
179
180    In either case we perform the following steps:
181
182    1) Compute basic block information.
183
184    2) Compute table of places where registers are set.
185
186    3) Perform copy/constant propagation.
187
188    4) Perform global cse.
189
190    5) Perform another pass of copy/constant propagation [only if PRE].
191
192    Two passes of copy/constant propagation are done because the first one
193    enables more GCSE and the second one helps to clean up the copies that
194    GCSE creates.  This is needed more for PRE than for Classic because Classic
195    GCSE will try to use an existing register containing the common
196    subexpression rather than create a new one.  This is harder to do for PRE
197    because of the code motion (which Classic GCSE doesn't do).
198
199    Expressions we are interested in GCSE-ing are of the form
200    (set (pseudo-reg) (expression)).
201    Function want_to_gcse_p says what these are.
202
203    PRE handles moving invariant expressions out of loops (by treating them as
204    partially redundant).  This feature of PRE is disabled here (by not
205    propagating dataflow information along back edges) because loop.c has more
206    involved (and thus typically better) heuristics for what to move out of
207    loops.
208
209    Eventually it would be nice to replace cse.c/gcse.c with SSA (static single
210    assignment) based GVN (global value numbering).  L. T. Simpson's paper
211    (Rice University) on value numbering is a useful reference for this.
212
213    **********************
214
215    We used to support multiple passes but there are diminishing returns in
216    doing so.  The first pass usually makes 90% of the changes that are doable.
217    A second pass can make a few more changes made possible by the first pass.
218    Experiments show any further passes don't make enough changes to justify
219    the expense.
220
221    A study of spec92 using an unlimited number of passes:
222    [1 pass] = 1208 substitutions, [2] = 577, [3] = 202, [4] = 192, [5] = 83,
223    [6] = 34, [7] = 17, [8] = 9, [9] = 4, [10] = 4, [11] = 2,
224    [12] = 2, [13] = 1, [15] = 1, [16] = 2, [41] = 1
225
226    It was found doing copy propagation between each pass enables further
227    substitutions.
228
229    PRE is quite expensive in complicated functions because the DFA can take
230    awhile to converge.  Hence we only perform one pass.  Macro MAX_PASSES can
231    be modified if one wants to experiment.
232
233    **********************
234
235    The steps for PRE are:
236
237    1) Build the hash table of expressions we wish to GCSE (expr_hash_table).
238
239    2) Perform the data flow analysis for PRE.
240
241    3) Delete the redundant instructions
242
243    4) Insert the required copies [if any] that make the partially
244       redundant instructions fully redundant.
245
246    5) For other reaching expressions, insert an instruction to copy the value
247       to a newly created pseudo that will reach the redundant instruction.
248
249    The deletion is done first so that when we do insertions we
250    know which pseudo reg to use.
251
252    Various papers have argued that PRE DFA is expensive (O(n^2)) and others
253    argue it is not.  The number of iterations for the algorithm to converge
254    is typically 2-4 so I don't view it as that expensive (relatively speaking).
255
256    PRE GCSE depends heavily on the seconds CSE pass to clean up the copies
257    we create.  To make an expression reach the place where it's redundant,
258    the result of the expression is copied to a new register, and the redundant
259    expression is deleted by replacing it with this new register.  Classic GCSE
260    doesn't have this problem as much as it computes the reaching defs of
261    each register in each block and thus can try to use an existing register.
262
263    **********************
264
265    When -fclassic-gcse, we perform a classic global CSE pass.
266    It is based on the algorithms in the Dragon book, and is based on code
267    written by Devor Tevi at Intel.
268
269    The steps for Classic GCSE are:
270
271    1) Build the hash table of expressions we wish to GCSE (expr_hash_table).
272       Also recorded are reaching definition "gen" statements (rd_gen)
273
274    2) Compute the reaching definitions (reaching_defs).
275       This is a bitmap for each basic block indexed by INSN_CUID that is 1
276       for each statement containing a definition that reaches the block.
277
278    3) Compute the available expressions (ae_in).
279       This is a bitmap for each basic block indexed by expression number
280       that is 1 for each expression that is available at the beginning of
281       the block.
282
283    4) Perform GCSE.
284       This is done by scanning each instruction looking for sets of the form
285       (set (pseudo-reg) (expression)) and checking if `expression' is in the
286       hash table.  If it is, and if the expression is available, and if only
287       one computation of the expression reaches the instruction, we substitute
288       the expression for a register containing its value.  If there is no
289       such register, but the expression is expensive enough we create an
290       instruction to copy the result of the expression into and use that.
291
292    **********************
293
294    A fair bit of simplicity is created by creating small functions for simple
295    tasks, even when the function is only called in one place.  This may
296    measurably slow things down [or may not] by creating more function call
297    overhead than is necessary.  The source is laid out so that it's trivial
298    to make the affected functions inline so that one can measure what speed
299    up, if any, can be achieved, and maybe later when things settle things can
300    be rearranged.
301
302    Help stamp out big monolithic functions!  */
303 \f
304 /* GCSE global vars.  */
305
306 /* -dG dump file.  */
307 static FILE *gcse_file;
308
309 /* Bitmaps are normally not included in debugging dumps.
310    However it's useful to be able to print them from GDB.
311    We could create special functions for this, but it's simpler to
312    just allow passing stderr to the dump_foo fns.  Since stderr can
313    be a macro, we store a copy here.  */
314 static FILE *debug_stderr;
315
316 /* An obstack for our working variables.  */
317 static struct obstack gcse_obstack;
318
319 /* Non-zero for each mode that supports (set (reg) (reg)).
320    This is trivially true for integer and floating point values.
321    It may or may not be true for condition codes.  */
322 static char can_copy_p[(int) NUM_MACHINE_MODES];
323
324 /* Non-zero if can_copy_p has been initialized.  */
325 static int can_copy_init_p;
326
327 /* Element I is a list of I's predecessors/successors.  */
328 static int_list_ptr *s_preds;
329 static int_list_ptr *s_succs;
330
331 /* Element I is the number of predecessors/successors of basic block I.  */
332 static int *num_preds;
333 static int *num_succs;
334
335 /* Hash table of expressions.  */
336
337 struct expr
338 {
339   /* The expression (SET_SRC for expressions, PATTERN for assignments).  */
340   rtx expr;
341   /* Index in the available expression bitmaps.  */
342   int bitmap_index;
343   /* Next entry with the same hash.  */
344   struct expr *next_same_hash;
345   /* List of anticipatable occurrences in basic blocks in the function.
346      An "anticipatable occurrence" is one that is the first occurrence in the
347      basic block and the operands are not modified in the basic block prior
348      to the occurrence.  */
349   struct occr *antic_occr;
350   /* List of available occurrence in basic blocks in the function.
351      An "available occurrence" is one that is the last occurrence in the
352      basic block and the operands are not modified by following statements in
353      the basic block [including this insn].  */
354   struct occr *avail_occr;
355   /* Non-null if the computation is PRE redundant.
356      The value is the newly created pseudo-reg to record a copy of the
357      expression in all the places that reach the redundant copy.  */
358   rtx reaching_reg;
359 };
360
361 /* Occurrence of an expression.
362    There is one per basic block.  If a pattern appears more than once the
363    last appearance is used [or first for anticipatable expressions].  */
364
365 struct occr
366 {
367   /* Next occurrence of this expression.  */
368   struct occr *next;
369   /* The insn that computes the expression.  */
370   rtx insn;
371   /* Non-zero if this [anticipatable] occurrence has been deleted.  */
372   char deleted_p;
373   /* Non-zero if this [available] occurrence has been copied to
374      reaching_reg.  */
375   /* ??? This is mutually exclusive with deleted_p, so they could share
376      the same byte.  */
377   char copied_p;
378 };
379
380 /* Expression and copy propagation hash tables.
381    Each hash table is an array of buckets.
382    ??? It is known that if it were an array of entries, structure elements
383    `next_same_hash' and `bitmap_index' wouldn't be necessary.  However, it is
384    not clear whether in the final analysis a sufficient amount of memory would
385    be saved as the size of the available expression bitmaps would be larger
386    [one could build a mapping table without holes afterwards though].
387    Someday I'll perform the computation and figure it out.
388 */
389
390 /* Total size of the expression hash table, in elements.  */
391 static int expr_hash_table_size;
392 /* The table itself.
393    This is an array of `expr_hash_table_size' elements.  */
394 static struct expr **expr_hash_table;
395
396 /* Total size of the copy propagation hash table, in elements.  */
397 static int set_hash_table_size;
398 /* The table itself.
399    This is an array of `set_hash_table_size' elements.  */
400 static struct expr **set_hash_table;
401
402 /* Mapping of uids to cuids.
403    Only real insns get cuids.  */
404 static int *uid_cuid;
405
406 /* Highest UID in UID_CUID.  */
407 static int max_uid;
408
409 /* Get the cuid of an insn.  */
410 #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
411
412 /* Number of cuids.  */
413 static int max_cuid;
414
415 /* Mapping of cuids to insns.  */
416 static rtx *cuid_insn;
417
418 /* Get insn from cuid.  */
419 #define CUID_INSN(CUID) (cuid_insn[CUID])
420
421 /* Maximum register number in function prior to doing gcse + 1.
422    Registers created during this pass have regno >= max_gcse_regno.
423    This is named with "gcse" to not collide with global of same name.  */
424 static int max_gcse_regno;
425
426 /* Maximum number of cse-able expressions found.  */
427 static int n_exprs;
428 /* Maximum number of assignments for copy propagation found.  */
429 static int n_sets;
430
431 /* Table of registers that are modified.
432    For each register, each element is a list of places where the pseudo-reg
433    is set.
434
435    For simplicity, GCSE is done on sets of pseudo-regs only.  PRE GCSE only
436    requires knowledge of which blocks kill which regs [and thus could use
437    a bitmap instead of the lists `reg_set_table' uses].  The classic GCSE
438    uses the information in lists.
439
440    If the classic GCSE pass is deleted `reg_set_table' and could be turned
441    into an array of bitmaps (num-bbs x num-regs)
442    [however perhaps it may be useful to keep the data as is].
443    One advantage of recording things this way is that `reg_set_table' is
444    fairly sparse with respect to pseudo regs but for hard regs could be
445    fairly dense [relatively speaking].
446    And recording sets of pseudo-regs in lists speeds
447    up functions like compute_transp since in the case of pseudo-regs we only
448    need to iterate over the number of times a pseudo-reg is set, not over the
449    number of basic blocks [clearly there is a bit of a slow down in the cases
450    where a pseudo is set more than once in a block, however it is believed
451    that the net effect is to speed things up].  This isn't done for hard-regs
452    because recording call-clobbered hard-regs in `reg_set_table' at each
453    function call can consume a fair bit of memory, and iterating over hard-regs
454    stored this way in compute_transp will be more expensive.  */
455
456 typedef struct reg_set {
457   /* The next setting of this register.  */
458   struct reg_set *next;
459   /* The insn where it was set.  */
460   rtx insn;
461 } reg_set;
462 static reg_set **reg_set_table;
463 /* Size of `reg_set_table'.
464    The table starts out at max_gcse_regno + slop, and is enlarged as
465    necessary.  */
466 static int reg_set_table_size;
467 /* Amount to grow `reg_set_table' by when it's full.  */
468 #define REG_SET_TABLE_SLOP 100
469
470 /* Bitmap containing one bit for each register in the program.
471    Used when performing GCSE to track which registers have been set since
472    the start of the basic block.  */
473 static sbitmap reg_set_bitmap;
474
475 /* For each block, a bitmap of registers set in the block.
476    This is used by expr_killed_p and compute_transp.
477    It is computed during hash table computation and not by compute_sets
478    as it includes registers added since the last pass (or between cprop and
479    gcse) and it's currently not easy to realloc sbitmap vectors.  */
480 static sbitmap *reg_set_in_block;
481
482 /* For each block, non-zero if memory is set in that block.
483    This is computed during hash table computation and is used by
484    expr_killed_p and compute_transp.
485    ??? Handling of memory is very simple, we don't make any attempt
486    to optimize things (later).
487    ??? This can be computed by compute_sets since the information
488    doesn't change.  */
489 static char *mem_set_in_block;
490
491 /* Various variables for statistics gathering.  */
492
493 /* Memory used in a pass.
494    This isn't intended to be absolutely precise.  Its intent is only
495    to keep an eye on memory usage.  */
496 static int bytes_used;
497 /* GCSE substitutions made.  */
498 static int gcse_subst_count;
499 /* Number of copy instructions created.  */
500 static int gcse_create_count;
501 /* Number of constants propagated.  */
502 static int const_prop_count;
503 /* Number of copys propagated.  */
504 static int copy_prop_count;
505
506 extern char *current_function_name;
507 extern int current_function_calls_setjmp;
508 extern int current_function_calls_longjmp;
509 \f
510 /* These variables are used by classic GCSE.
511    Normally they'd be defined a bit later, but `rd_gen' needs to
512    be declared sooner.  */
513
514 /* A bitmap of all ones for implementing the algorithm for available
515    expressions and reaching definitions.  */
516 /* ??? Available expression bitmaps have a different size than reaching
517    definition bitmaps.  This should be the larger of the two, however, it
518    is not currently used for reaching definitions.  */
519 static sbitmap u_bitmap;
520
521 /* Each block has a bitmap of each type.
522    The length of each blocks bitmap is:
523
524        max_cuid  - for reaching definitions
525        n_exprs - for available expressions
526
527    Thus we view the bitmaps as 2 dimensional arrays.  i.e.
528    rd_kill[block_num][cuid_num]
529    ae_kill[block_num][expr_num]
530 */
531
532 /* For reaching defs */
533 static sbitmap *rd_kill, *rd_gen, *reaching_defs, *rd_out;
534
535 /* for available exprs */
536 static sbitmap *ae_kill, *ae_gen, *ae_in, *ae_out;
537 \f
538 static void compute_can_copy          PROTO ((void));
539
540 static char *gmalloc                  PROTO ((unsigned int));
541 static char *grealloc                 PROTO ((char *, unsigned int));
542 static char *gcse_alloc               PROTO ((unsigned long));
543 static void alloc_gcse_mem            PROTO ((rtx));
544 static void free_gcse_mem             PROTO ((void));
545 extern void dump_cuid_table           PROTO ((FILE *));
546
547 static void alloc_reg_set_mem         PROTO ((int));
548 static void free_reg_set_mem          PROTO ((void));
549 static void record_one_set            PROTO ((int, rtx));
550 static void record_set_info           PROTO ((rtx, rtx));
551 static void compute_sets              PROTO ((rtx));
552
553 static void hash_scan_insn            PROTO ((rtx, int));
554 static void hash_scan_set             PROTO ((rtx, rtx, int));
555 static void hash_scan_clobber         PROTO ((rtx, rtx));
556 static void hash_scan_call            PROTO ((rtx, rtx));
557 static void maybe_set_rd_gen          PROTO ((int, rtx));
558 static int want_to_gcse_p             PROTO ((rtx));
559 static int oprs_unchanged_p           PROTO ((rtx, rtx, int));
560 static int oprs_anticipatable_p       PROTO ((rtx, rtx));
561 static int oprs_available_p           PROTO ((rtx, rtx));
562 static void insert_expr_in_table      PROTO ((rtx, enum machine_mode, rtx, int, int));
563 static void insert_set_in_table       PROTO ((rtx, rtx));
564 static unsigned int hash_expr         PROTO ((rtx, enum machine_mode, int *, int));
565 static unsigned int hash_expr_1       PROTO ((rtx, enum machine_mode, int *));
566 static unsigned int hash_set          PROTO ((int, int));
567 static int expr_equiv_p               PROTO ((rtx, rtx));
568 static void record_last_reg_set_info  PROTO ((rtx, int));
569 static void record_last_mem_set_info  PROTO ((rtx));
570 static void record_last_set_info      PROTO ((rtx, rtx));
571 static void compute_hash_table        PROTO ((rtx, int));
572 static void alloc_set_hash_table      PROTO ((int));
573 static void free_set_hash_table       PROTO ((void));
574 static void compute_set_hash_table    PROTO ((rtx));
575 static void alloc_expr_hash_table     PROTO ((int));
576 static void free_expr_hash_table      PROTO ((void));
577 static void compute_expr_hash_table   PROTO ((rtx));
578 static void dump_hash_table           PROTO ((FILE *, char *, struct expr **, int, int));
579 static struct expr *lookup_expr       PROTO ((rtx));
580 static struct expr *lookup_set        PROTO ((int, rtx));
581 static struct expr *next_set          PROTO ((int, struct expr *));
582 static void reset_opr_set_tables      PROTO ((void));
583 static int oprs_not_set_p             PROTO ((rtx, rtx));
584 static void mark_call                 PROTO ((rtx, rtx));
585 static void mark_set                  PROTO ((rtx, rtx));
586 static void mark_clobber              PROTO ((rtx, rtx));
587 static void mark_oprs_set             PROTO ((rtx));
588
589 static void alloc_rd_mem              PROTO ((int, int));
590 static void free_rd_mem               PROTO ((void));
591 static void compute_kill_rd           PROTO ((void));
592 static void handle_rd_kill_set        PROTO ((rtx, int, int));
593 static void compute_rd                PROTO ((void));
594 extern void dump_rd_table             PROTO ((FILE *, char *, sbitmap *));
595
596 static void alloc_avail_expr_mem      PROTO ((int, int));
597 static void free_avail_expr_mem       PROTO ((void));
598 static void compute_ae_gen            PROTO ((void));
599 static void compute_ae_kill           PROTO ((void));
600 static int expr_killed_p              PROTO ((rtx, int));
601 static void compute_available         PROTO ((void));
602
603 static int expr_reaches_here_p        PROTO ((struct occr *, struct expr *,
604                                               int, int, char *));
605 static rtx computing_insn             PROTO ((struct expr *, rtx));
606 static int def_reaches_here_p         PROTO ((rtx, rtx));
607 static int can_disregard_other_sets   PROTO ((struct reg_set **, rtx, int));
608 static int handle_avail_expr          PROTO ((rtx, struct expr *));
609 static int classic_gcse               PROTO ((void));
610 static int one_classic_gcse_pass      PROTO ((rtx, int));
611
612 static void alloc_cprop_mem           PROTO ((int, int));
613 static void free_cprop_mem            PROTO ((void));
614 extern void dump_cprop_data           PROTO ((FILE *));
615 static void compute_transp            PROTO ((rtx, int, sbitmap *, int));
616 static void compute_cprop_local_properties PROTO ((void));
617 static void compute_cprop_avinout     PROTO ((void));
618 static void compute_cprop_data        PROTO ((void));
619 static void find_used_regs            PROTO ((rtx));
620 static int try_replace_reg            PROTO ((rtx, rtx, rtx));
621 static struct expr *find_avail_set    PROTO ((int, rtx));
622 static int cprop_insn                 PROTO ((rtx));
623 static int cprop                      PROTO ((void));
624 static int one_cprop_pass             PROTO ((rtx, int));
625
626 static void alloc_pre_mem             PROTO ((int, int));
627 static void free_pre_mem              PROTO ((void));
628 extern void dump_pre_data             PROTO ((FILE *));
629 static void compute_pre_local_properties PROTO ((void));
630 static void compute_pre_avinout       PROTO ((void));
631 static void compute_pre_antinout      PROTO ((void));
632 static void compute_pre_pavinout      PROTO ((void));
633 static void compute_pre_ppinout       PROTO ((void));
634 static void compute_pre_data          PROTO ((void));
635 static int pre_expr_reaches_here_p    PROTO ((struct occr *, struct expr *,
636                                               int, char *));
637 static void pre_insert_insn           PROTO ((struct expr *, int));
638 static void pre_insert                PROTO ((struct expr **));
639 static void pre_insert_copy_insn      PROTO ((struct expr *, rtx));
640 static void pre_insert_copies         PROTO ((void));
641 static int pre_delete                 PROTO ((void));
642 static int pre_gcse                   PROTO ((void));
643 static int one_pre_gcse_pass          PROTO ((rtx, int));
644 \f
645 /* Entry point for global common subexpression elimination.
646    F is the first instruction in the function.  */
647
648 void
649 gcse_main (f, file)
650      rtx f;
651      FILE *file;
652 {
653   int changed, pass;
654   /* Bytes used at start of pass.  */
655   int initial_bytes_used;
656   /* Maximum number of bytes used by a pass.  */
657   int max_pass_bytes;
658   /* Point to release obstack data from for each pass.  */
659   char *gcse_obstack_bottom;
660
661   /* It's impossible to construct a correct control flow graph in the
662      presense of setjmp, so just punt to be safe.  */
663   if (current_function_calls_setjmp)
664     return;
665    
666   /* For calling dump_foo fns from gdb.  */
667   debug_stderr = stderr;
668
669   max_gcse_regno = max_reg_num ();
670   find_basic_blocks (f, max_gcse_regno, file, 0);
671
672   /* Return if there's nothing to do.  */
673   if (n_basic_blocks <= 1)
674     {
675       /* Free storage allocated by find_basic_blocks.  */
676       free_basic_block_vars (0);
677       return;
678     }
679
680   /* See what modes support reg/reg copy operations.  */
681   if (! can_copy_init_p)
682     {
683       compute_can_copy ();
684       can_copy_init_p = 1;
685     }
686
687   gcc_obstack_init (&gcse_obstack);
688
689   gcse_file = file;
690
691   /* Allocate and compute predecessors/successors.  */
692
693   s_preds = (int_list_ptr *) alloca (n_basic_blocks * sizeof (int_list_ptr));
694   s_succs = (int_list_ptr *) alloca (n_basic_blocks * sizeof (int_list_ptr));
695   num_preds = (int *) alloca (n_basic_blocks * sizeof (int));
696   num_succs = (int *) alloca (n_basic_blocks * sizeof (int));
697   bytes_used = 4 * n_basic_blocks * sizeof (int_list_ptr);
698   compute_preds_succs (s_preds, s_succs, num_preds, num_succs);
699
700   if (file)
701     {
702       dump_bb_data (file, s_preds, s_succs);
703     }
704
705   /* Record where pseudo-registers are set.
706      This data is kept accurate during each pass.
707      ??? We could also record hard-reg and memory information here
708      [since it's unchanging], however it is currently done during
709      hash table computation.  */
710
711   alloc_reg_set_mem (max_gcse_regno);
712   compute_sets (f);
713
714   pass = 0;
715   initial_bytes_used = bytes_used;
716   max_pass_bytes = 0;
717   gcse_obstack_bottom = gcse_alloc (1);
718   changed = 1;
719   while (changed && pass < MAX_PASSES)
720     {
721       changed = 0;
722       if (file)
723         fprintf (file, "GCSE pass %d\n\n", pass + 1);
724
725       /* Initialize bytes_used to the space for the pred/succ lists,
726          and the reg_set_table data.  */
727       bytes_used = initial_bytes_used;
728
729       /* Each pass may create new registers, so recalculate each time.  */
730       max_gcse_regno = max_reg_num ();
731
732       alloc_gcse_mem (f);
733
734       changed = one_cprop_pass (f, pass + 1);
735
736       if (optimize_size)
737         changed |= one_classic_gcse_pass (f, pass + 1);
738       else
739         changed |= one_pre_gcse_pass (f, pass + 1);
740
741       if (max_pass_bytes < bytes_used)
742         max_pass_bytes = bytes_used;
743
744       free_gcse_mem ();
745
746       if (file)
747         {
748           fprintf (file, "\n");
749           fflush (file);
750         }
751       obstack_free (&gcse_obstack, gcse_obstack_bottom);
752       pass++;
753     }
754
755   /* If we're doing PRE, do one last pass of copy propagation.  */
756   if (! optimize_size)
757     {
758       max_gcse_regno = max_reg_num ();
759       alloc_gcse_mem (f);
760       one_cprop_pass (f, pass + 1);
761       free_gcse_mem ();
762     }
763
764   if (file)
765     {
766       fprintf (file, "GCSE of %s: %d basic blocks, ",
767                current_function_name, n_basic_blocks);
768       fprintf (file, "%d pass%s, %d bytes\n\n",
769                pass, pass > 1 ? "es" : "", max_pass_bytes);
770     }
771
772   /* Free our obstack.  */
773   obstack_free (&gcse_obstack, NULL_PTR);
774   /* Free reg_set_table.  */
775   free_reg_set_mem ();
776   /* Free storage used to record predecessor/successor data.  */
777   free_bb_mem ();
778   /* Free storage allocated by find_basic_blocks.  */
779   free_basic_block_vars (0);
780 }
781 \f
782 /* Misc. utilities.  */
783
784 /* Compute which modes support reg/reg copy operations.  */
785
786 static void
787 compute_can_copy ()
788 {
789   int i;
790 #ifndef AVOID_CCMODE_COPIES
791   rtx reg,insn;
792 #endif
793   char *free_point = (char *) oballoc (1);
794
795   bzero (can_copy_p, NUM_MACHINE_MODES);
796
797   start_sequence ();
798   for (i = 0; i < NUM_MACHINE_MODES; i++)
799     {
800       switch (GET_MODE_CLASS (i))
801         {
802         case MODE_CC :
803 #ifdef AVOID_CCMODE_COPIES
804           can_copy_p[i] = 0;
805 #else
806           reg = gen_rtx (REG, (enum machine_mode) i, LAST_VIRTUAL_REGISTER + 1);
807           insn = emit_insn (gen_rtx (SET, VOIDmode, reg, reg));
808           if (recog (PATTERN (insn), insn, NULL_PTR) >= 0)
809             can_copy_p[i] = 1;
810 #endif
811           break;
812         default :
813           can_copy_p[i] = 1;
814           break;
815         }
816     }
817   end_sequence ();
818
819   /* Free the objects we just allocated.  */
820   obfree (free_point);
821 }
822 \f
823 /* Cover function to xmalloc to record bytes allocated.  */
824
825 static char *
826 gmalloc (size)
827      unsigned int size;
828 {
829   bytes_used += size;
830   return xmalloc (size);
831 }
832
833 /* Cover function to xrealloc.
834    We don't record the additional size since we don't know it.
835    It won't affect memory usage stats much anyway.  */
836
837 static char *
838 grealloc (ptr, size)
839      char *ptr;
840      unsigned int size;
841 {
842   return xrealloc (ptr, size);
843 }
844
845 /* Cover function to obstack_alloc.
846    We don't need to record the bytes allocated here since
847    obstack_chunk_alloc is set to gmalloc.  */
848
849 static char *
850 gcse_alloc (size)
851      unsigned long size;
852 {
853   return (char *) obstack_alloc (&gcse_obstack, size);
854 }
855
856 /* Allocate memory for the cuid mapping array,
857    and reg/memory set tracking tables.
858
859    This is called at the start of each pass.  */
860
861 static void
862 alloc_gcse_mem (f)
863      rtx f;
864 {
865   int i,n;
866   rtx insn;
867
868   /* Find the largest UID and create a mapping from UIDs to CUIDs.
869      CUIDs are like UIDs except they increase monotonically, have no gaps,
870      and only apply to real insns.  */
871
872   max_uid = get_max_uid ();
873   n = (max_uid + 1) * sizeof (int);
874   uid_cuid = (int *) gmalloc (n);
875   bzero ((char *) uid_cuid, n);
876   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
877     {
878       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
879         INSN_CUID (insn) = i++;
880       else
881         INSN_CUID (insn) = i;
882     }
883
884   /* Create a table mapping cuids to insns.  */
885
886   max_cuid = i;
887   n = (max_cuid + 1) * sizeof (rtx);
888   cuid_insn = (rtx *) gmalloc (n);
889   bzero ((char *) cuid_insn, n);
890   for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
891     {
892       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
893         {
894           CUID_INSN (i) = insn;
895           i++;
896         }
897     }
898
899   /* Allocate vars to track sets of regs.  */
900
901   reg_set_bitmap = (sbitmap) sbitmap_alloc (max_gcse_regno);
902
903   /* Allocate vars to track sets of regs, memory per block.  */
904
905   reg_set_in_block = (sbitmap *) sbitmap_vector_alloc (n_basic_blocks,
906                                                        max_gcse_regno);
907   mem_set_in_block = (char *) gmalloc (n_basic_blocks);
908 }
909
910 /* Free memory allocated by alloc_gcse_mem.  */
911
912 static void
913 free_gcse_mem ()
914 {
915   free (uid_cuid);
916   free (cuid_insn);
917
918   free (reg_set_bitmap);
919
920   free (reg_set_in_block);
921   free (mem_set_in_block);
922 }
923
924 void
925 dump_cuid_table (file)
926      FILE *file;
927 {
928   int i,n;
929
930   fprintf (file, "CUID table\n");
931   for (i = n = 0; i < max_cuid; i++)
932     {
933       rtx insn = CUID_INSN (i);
934       if (n != 0 && n % 10 == 0)
935         fprintf (file, "\n");
936       if (insn != NULL)
937         fprintf (file, " %d", INSN_UID (insn));
938     }
939   fprintf (file, "\n\n");
940 }
941 \f
942 /* Register set information.
943
944    `reg_set_table' records where each register is set or otherwise
945    modified.  */
946
947 static struct obstack reg_set_obstack;
948
949 static void
950 alloc_reg_set_mem (n_regs)
951      int n_regs;
952 {
953   int n;
954
955   reg_set_table_size = n_regs + REG_SET_TABLE_SLOP;
956   n = reg_set_table_size * sizeof (struct reg_set *);
957   reg_set_table = (struct reg_set **) gmalloc (n);
958   bzero ((char *) reg_set_table, n);
959
960   gcc_obstack_init (&reg_set_obstack);
961 }
962
963 static void
964 free_reg_set_mem ()
965 {
966   free (reg_set_table);
967   obstack_free (&reg_set_obstack, NULL_PTR);
968 }
969
970 /* Record REGNO in the reg_set table.  */
971
972 static void
973 record_one_set (regno, insn)
974      int regno;
975      rtx insn;
976 {
977   /* allocate a new reg_set element and link it onto the list */
978   struct reg_set *new_reg_info, *reg_info_ptr1, *reg_info_ptr2;
979
980   /* If the table isn't big enough, enlarge it.  */
981   if (regno >= reg_set_table_size)
982     {
983       int new_size = regno + REG_SET_TABLE_SLOP;
984       reg_set_table = (struct reg_set **)
985         grealloc ((char *) reg_set_table,
986                   new_size * sizeof (struct reg_set *));
987       bzero ((char *) (reg_set_table + reg_set_table_size),
988              (new_size - reg_set_table_size) * sizeof (struct reg_set *));
989       reg_set_table_size = new_size;
990     }
991
992   new_reg_info = (struct reg_set *) obstack_alloc (&reg_set_obstack,
993                                                    sizeof (struct reg_set));
994   bytes_used += sizeof (struct reg_set);
995   new_reg_info->insn = insn;
996   new_reg_info->next = NULL;
997   if (reg_set_table[regno] == NULL)
998     reg_set_table[regno] = new_reg_info;
999   else
1000     {
1001       reg_info_ptr1 = reg_info_ptr2 = reg_set_table[regno];
1002       /* ??? One could keep a "last" pointer to speed this up.  */
1003       while (reg_info_ptr1 != NULL)
1004         {
1005           reg_info_ptr2 = reg_info_ptr1;
1006           reg_info_ptr1 = reg_info_ptr1->next;
1007         }
1008       reg_info_ptr2->next = new_reg_info;
1009     }
1010 }
1011
1012 /* For communication between next two functions (via note_stores).  */
1013 static rtx record_set_insn;
1014
1015 /* Called from compute_sets via note_stores to handle one
1016    SET or CLOBBER in an insn.  */
1017
1018 static void
1019 record_set_info (dest, setter)
1020      rtx dest, setter ATTRIBUTE_UNUSED;
1021 {
1022   if (GET_CODE (dest) == SUBREG)
1023     dest = SUBREG_REG (dest);
1024
1025   if (GET_CODE (dest) == REG)
1026     {
1027       if (REGNO (dest) >= FIRST_PSEUDO_REGISTER)
1028         record_one_set (REGNO (dest), record_set_insn);
1029     }
1030 }
1031
1032 /* Scan the function and record each set of each pseudo-register.
1033
1034    This is called once, at the start of the gcse pass.
1035    See the comments for `reg_set_table' for further docs.  */
1036
1037 static void
1038 compute_sets (f)
1039      rtx f;
1040 {
1041   rtx insn = f;
1042
1043   while (insn)
1044     {
1045       if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1046         {
1047           record_set_insn = insn;
1048           note_stores (PATTERN (insn), record_set_info);
1049         }
1050       insn = NEXT_INSN (insn);
1051     }
1052 }
1053 \f
1054 /* Hash table support.  */
1055
1056 /* For each register, the cuid of the first/last insn in the block to set it,
1057    or zero if not set.  */
1058 static int *reg_first_set;
1059 static int *reg_last_set;
1060
1061 /* While computing "first/last set" info, this is the CUID of first/last insn
1062    to set memory or zero if not set.  `mem_last_set' is also used when
1063    performing GCSE to record whether memory has been set since the beginning
1064    of the block.
1065    Note that handling of memory is very simple, we don't make any attempt
1066    to optimize things (later).  */
1067 static int mem_first_set;
1068 static int mem_last_set;
1069
1070 /* Set the appropriate bit in `rd_gen' [the gen for reaching defs] if the
1071    register set in this insn is not set after this insn in this block.  */
1072
1073 static void
1074 maybe_set_rd_gen (regno, insn)
1075      int regno;
1076      rtx insn;
1077 {
1078   if (reg_last_set[regno] <= INSN_CUID (insn))
1079     SET_BIT (rd_gen[BLOCK_NUM (insn)], INSN_CUID (insn));
1080 }
1081
1082 /* Perform a quick check whether X, the source of a set, is something
1083    we want to consider for GCSE.  */
1084
1085 static int
1086 want_to_gcse_p (x)
1087      rtx x;
1088 {
1089   enum rtx_code code = GET_CODE (x);
1090
1091   switch (code)
1092     {
1093     case REG:
1094     case SUBREG:
1095     case CONST_INT:
1096     case CONST_DOUBLE:
1097     case CALL:
1098       return 0;
1099
1100     default:
1101       break;
1102     }
1103
1104   return 1;
1105 }
1106
1107 /* Return non-zero if the operands of expression X are unchanged from the
1108    start of INSN's basic block up to but not including INSN (if AVAIL_P == 0),
1109    or from INSN to the end of INSN's basic block (if AVAIL_P != 0).  */
1110
1111 static int
1112 oprs_unchanged_p (x, insn, avail_p)
1113      rtx x, insn;
1114      int avail_p;
1115 {
1116   int i;
1117   enum rtx_code code;
1118   char *fmt;
1119
1120   /* repeat is used to turn tail-recursion into iteration.  */
1121  repeat:
1122
1123   if (x == 0)
1124     return 1;
1125
1126   code = GET_CODE (x);
1127   switch (code)
1128     {
1129     case REG:
1130       if (avail_p)
1131         return (reg_last_set[REGNO (x)] == 0
1132                 || reg_last_set[REGNO (x)] < INSN_CUID (insn));
1133       else
1134         return (reg_first_set[REGNO (x)] == 0
1135                 || reg_first_set[REGNO (x)] >= INSN_CUID (insn));
1136
1137     case MEM:
1138       if (avail_p)
1139         {
1140           if (mem_last_set != 0
1141               && mem_last_set >= INSN_CUID (insn))
1142             return 0;
1143         }
1144       else
1145         {
1146           if (mem_first_set != 0
1147               && mem_first_set < INSN_CUID (insn))
1148             return 0;
1149         }
1150       x = XEXP (x, 0);
1151       goto repeat;
1152
1153     case PRE_DEC:
1154     case PRE_INC:
1155     case POST_DEC:
1156     case POST_INC:
1157       return 0;
1158
1159     case PC:
1160     case CC0: /*FIXME*/
1161     case CONST:
1162     case CONST_INT:
1163     case CONST_DOUBLE:
1164     case SYMBOL_REF:
1165     case LABEL_REF:
1166     case ADDR_VEC:
1167     case ADDR_DIFF_VEC:
1168       return 1;
1169
1170     default:
1171       break;
1172     }
1173
1174   i = GET_RTX_LENGTH (code) - 1;
1175   fmt = GET_RTX_FORMAT (code);
1176   for (; i >= 0; i--)
1177     {
1178       if (fmt[i] == 'e')
1179         {
1180           rtx tem = XEXP (x, i);
1181
1182           /* If we are about to do the last recursive call
1183              needed at this level, change it into iteration.
1184              This function is called enough to be worth it.  */
1185           if (i == 0)
1186             {
1187               x = tem;
1188               goto repeat;
1189             }
1190           if (! oprs_unchanged_p (tem, insn, avail_p))
1191             return 0;
1192         }
1193       else if (fmt[i] == 'E')
1194         {
1195           int j;
1196           for (j = 0; j < XVECLEN (x, i); j++)
1197             {
1198               if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, avail_p))
1199                 return 0;
1200             }
1201         }
1202     }
1203
1204   return 1;
1205 }
1206
1207 /* Return non-zero if the operands of expression X are unchanged from
1208    the start of INSN's basic block up to but not including INSN.  */
1209
1210 static int
1211 oprs_anticipatable_p (x, insn)
1212      rtx x, insn;
1213 {
1214   return oprs_unchanged_p (x, insn, 0);
1215 }
1216
1217 /* Return non-zero if the operands of expression X are unchanged from
1218    INSN to the end of INSN's basic block.  */
1219
1220 static int
1221 oprs_available_p (x, insn)
1222      rtx x, insn;
1223 {
1224   return oprs_unchanged_p (x, insn, 1);
1225 }
1226
1227 /* Hash expression X.
1228    MODE is only used if X is a CONST_INT.
1229    A boolean indicating if a volatile operand is found or if the expression
1230    contains something we don't want to insert in the table is stored in
1231    DO_NOT_RECORD_P.
1232
1233    ??? One might want to merge this with canon_hash.  Later.  */
1234
1235 static unsigned int
1236 hash_expr (x, mode, do_not_record_p, hash_table_size)
1237      rtx x;
1238      enum machine_mode mode;
1239      int *do_not_record_p;
1240      int hash_table_size;
1241 {
1242   unsigned int hash;
1243
1244   *do_not_record_p = 0;
1245
1246   hash = hash_expr_1 (x, mode, do_not_record_p);
1247   return hash % hash_table_size;
1248 }
1249
1250 /* Subroutine of hash_expr to do the actual work.  */
1251
1252 static unsigned int
1253 hash_expr_1 (x, mode, do_not_record_p)
1254      rtx x;
1255      enum machine_mode mode;
1256      int *do_not_record_p;
1257 {
1258   int i, j;
1259   unsigned hash = 0;
1260   enum rtx_code code;
1261   char *fmt;
1262
1263   /* repeat is used to turn tail-recursion into iteration.  */
1264  repeat:
1265
1266   if (x == 0)
1267     return hash;
1268
1269   code = GET_CODE (x);
1270   switch (code)
1271     {
1272     case REG:
1273       {
1274         register int regno = REGNO (x);
1275         hash += ((unsigned) REG << 7) + regno;
1276         return hash;
1277       }
1278
1279     case CONST_INT:
1280       {
1281         unsigned HOST_WIDE_INT tem = INTVAL (x);
1282         hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + tem;
1283         return hash;
1284       }
1285
1286     case CONST_DOUBLE:
1287       /* This is like the general case, except that it only counts
1288          the integers representing the constant.  */
1289       hash += (unsigned) code + (unsigned) GET_MODE (x);
1290       if (GET_MODE (x) != VOIDmode)
1291         for (i = 2; i < GET_RTX_LENGTH (CONST_DOUBLE); i++)
1292           {
1293             unsigned tem = XINT (x, i);
1294             hash += tem;
1295           }
1296       else
1297         hash += ((unsigned) CONST_DOUBLE_LOW (x)
1298                  + (unsigned) CONST_DOUBLE_HIGH (x));
1299       return hash;
1300
1301       /* Assume there is only one rtx object for any given label.  */
1302     case LABEL_REF:
1303       /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1304          differences and differences between each stage's debugging dumps.  */
1305       hash += ((unsigned) LABEL_REF << 7) + CODE_LABEL_NUMBER (XEXP (x, 0));
1306       return hash;
1307
1308     case SYMBOL_REF:
1309       {
1310         /* Don't hash on the symbol's address to avoid bootstrap differences.
1311            Different hash values may cause expressions to be recorded in
1312            different orders and thus different registers to be used in the
1313            final assembler.  This also avoids differences in the dump files
1314            between various stages.  */
1315         unsigned int h = 0;
1316         unsigned char *p = (unsigned char *) XSTR (x, 0);
1317         while (*p)
1318           h += (h << 7) + *p++; /* ??? revisit */
1319         hash += ((unsigned) SYMBOL_REF << 7) + h;
1320         return hash;
1321       }
1322
1323     case MEM:
1324       if (MEM_VOLATILE_P (x))
1325         {
1326           *do_not_record_p = 1;
1327           return 0;
1328         }
1329       hash += (unsigned) MEM;
1330       x = XEXP (x, 0);
1331       goto repeat;
1332
1333     case PRE_DEC:
1334     case PRE_INC:
1335     case POST_DEC:
1336     case POST_INC:
1337     case PC:
1338     case CC0:
1339     case CALL:
1340     case UNSPEC_VOLATILE:
1341       *do_not_record_p = 1;
1342       return 0;
1343
1344     case ASM_OPERANDS:
1345       if (MEM_VOLATILE_P (x))
1346         {
1347           *do_not_record_p = 1;
1348           return 0;
1349         }
1350
1351     default:
1352       break;
1353     }
1354
1355   i = GET_RTX_LENGTH (code) - 1;
1356   hash += (unsigned) code + (unsigned) GET_MODE (x);
1357   fmt = GET_RTX_FORMAT (code);
1358   for (; i >= 0; i--)
1359     {
1360       if (fmt[i] == 'e')
1361         {
1362           rtx tem = XEXP (x, i);
1363
1364           /* If we are about to do the last recursive call
1365              needed at this level, change it into iteration.
1366              This function is called enough to be worth it.  */
1367           if (i == 0)
1368             {
1369               x = tem;
1370               goto repeat;
1371             }
1372           hash += hash_expr_1 (tem, 0, do_not_record_p);
1373           if (*do_not_record_p)
1374             return 0;
1375         }
1376       else if (fmt[i] == 'E')
1377         for (j = 0; j < XVECLEN (x, i); j++)
1378           {
1379             hash += hash_expr_1 (XVECEXP (x, i, j), 0, do_not_record_p);
1380             if (*do_not_record_p)
1381               return 0;
1382           }
1383       else if (fmt[i] == 's')
1384         {
1385           register unsigned char *p = (unsigned char *) XSTR (x, i);
1386           if (p)
1387             while (*p)
1388               hash += *p++;
1389         }
1390       else if (fmt[i] == 'i')
1391         {
1392           register unsigned tem = XINT (x, i);
1393           hash += tem;
1394         }
1395       else
1396         abort ();
1397     }
1398
1399   return hash;
1400 }
1401
1402 /* Hash a set of register REGNO.
1403
1404    Sets are hashed on the register that is set.
1405    This simplifies the PRE copy propagation code.
1406
1407    ??? May need to make things more elaborate.  Later, as necessary.  */
1408
1409 static unsigned int
1410 hash_set (regno, hash_table_size)
1411      int regno;
1412      int hash_table_size;
1413 {
1414   unsigned int hash;
1415
1416   hash = regno;
1417   return hash % hash_table_size;
1418 }
1419
1420 /* Return non-zero if exp1 is equivalent to exp2.
1421    ??? Borrowed from cse.c.  Might want to remerge with cse.c.  Later.  */
1422
1423 static int
1424 expr_equiv_p (x, y)
1425      rtx x, y;
1426 {
1427   register int i, j;
1428   register enum rtx_code code;
1429   register char *fmt;
1430
1431   if (x == y)
1432     return 1;
1433   if (x == 0 || y == 0)
1434     return x == y;
1435
1436   code = GET_CODE (x);
1437   if (code != GET_CODE (y))
1438     return 0;
1439
1440   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.  */
1441   if (GET_MODE (x) != GET_MODE (y))
1442     return 0;
1443
1444   switch (code)
1445     {
1446     case PC:
1447     case CC0:
1448       return x == y;
1449
1450     case CONST_INT:
1451       return INTVAL (x) == INTVAL (y);
1452
1453     case LABEL_REF:
1454       return XEXP (x, 0) == XEXP (y, 0);
1455
1456     case SYMBOL_REF:
1457       return XSTR (x, 0) == XSTR (y, 0);
1458
1459     case REG:
1460       return REGNO (x) == REGNO (y);
1461
1462     /*  For commutative operations, check both orders.  */
1463     case PLUS:
1464     case MULT:
1465     case AND:
1466     case IOR:
1467     case XOR:
1468     case NE:
1469     case EQ:
1470       return ((expr_equiv_p (XEXP (x, 0), XEXP (y, 0))
1471                && expr_equiv_p (XEXP (x, 1), XEXP (y, 1)))
1472               || (expr_equiv_p (XEXP (x, 0), XEXP (y, 1))
1473                   && expr_equiv_p (XEXP (x, 1), XEXP (y, 0))));
1474
1475     default:
1476       break;
1477     }
1478
1479   /* Compare the elements.  If any pair of corresponding elements
1480      fail to match, return 0 for the whole thing.  */
1481
1482   fmt = GET_RTX_FORMAT (code);
1483   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1484     {
1485       switch (fmt[i])
1486         {
1487         case 'e':
1488           if (! expr_equiv_p (XEXP (x, i), XEXP (y, i)))
1489             return 0;
1490           break;
1491
1492         case 'E':
1493           if (XVECLEN (x, i) != XVECLEN (y, i))
1494             return 0;
1495           for (j = 0; j < XVECLEN (x, i); j++)
1496             if (! expr_equiv_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
1497               return 0;
1498           break;
1499
1500         case 's':
1501           if (strcmp (XSTR (x, i), XSTR (y, i)))
1502             return 0;
1503           break;
1504
1505         case 'i':
1506           if (XINT (x, i) != XINT (y, i))
1507             return 0;
1508           break;
1509
1510         case 'w':
1511           if (XWINT (x, i) != XWINT (y, i))
1512             return 0;
1513         break;
1514
1515         case '0':
1516           break;
1517
1518         default:
1519           abort ();
1520         }
1521       }
1522
1523   return 1;
1524 }
1525
1526 /* Insert expression X in INSN in the hash table.
1527    If it is already present, record it as the last occurrence in INSN's
1528    basic block.
1529
1530    MODE is the mode of the value X is being stored into.
1531    It is only used if X is a CONST_INT.
1532
1533    ANTIC_P is non-zero if X is an anticipatable expression.
1534    AVAIL_P is non-zero if X is an available expression.  */
1535
1536 static void
1537 insert_expr_in_table (x, mode, insn, antic_p, avail_p)
1538      rtx x;
1539      enum machine_mode mode;
1540      rtx insn;
1541      int antic_p, avail_p;
1542 {
1543   int found, do_not_record_p;
1544   unsigned int hash;
1545   struct expr *cur_expr, *last_expr = NULL;
1546   struct occr *antic_occr, *avail_occr;
1547   struct occr *last_occr = NULL;
1548
1549   hash = hash_expr (x, mode, &do_not_record_p, expr_hash_table_size);
1550
1551   /* Do not insert expression in table if it contains volatile operands,
1552      or if hash_expr determines the expression is something we don't want
1553      to or can't handle.  */
1554   if (do_not_record_p)
1555     return;
1556
1557   cur_expr = expr_hash_table[hash];
1558   found = 0;
1559
1560   while (cur_expr && ! (found = expr_equiv_p (cur_expr->expr, x)))
1561     {
1562       /* If the expression isn't found, save a pointer to the end of
1563          the list.  */
1564       last_expr = cur_expr;
1565       cur_expr = cur_expr->next_same_hash;
1566     }
1567
1568   if (! found)
1569     {
1570       cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
1571       bytes_used += sizeof (struct expr);
1572       if (expr_hash_table[hash] == NULL)
1573         {
1574           /* This is the first pattern that hashed to this index.  */
1575           expr_hash_table[hash] = cur_expr;
1576         }
1577       else
1578         {
1579           /* Add EXPR to end of this hash chain.  */
1580           last_expr->next_same_hash = cur_expr;
1581         }
1582       /* Set the fields of the expr element.  */ 
1583       cur_expr->expr = x;
1584       cur_expr->bitmap_index = n_exprs++;
1585       cur_expr->next_same_hash = NULL;
1586       cur_expr->antic_occr = NULL;
1587       cur_expr->avail_occr = NULL;
1588     }
1589
1590   /* Now record the occurrence(s).  */
1591
1592   if (antic_p)
1593     {
1594       antic_occr = cur_expr->antic_occr;
1595
1596       /* Search for another occurrence in the same basic block.  */
1597       while (antic_occr && BLOCK_NUM (antic_occr->insn) != BLOCK_NUM (insn))
1598         {
1599           /* If an occurrence isn't found, save a pointer to the end of
1600              the list.  */
1601           last_occr = antic_occr;
1602           antic_occr = antic_occr->next;
1603         }
1604
1605       if (antic_occr)
1606         {
1607           /* Found another instance of the expression in the same basic block.
1608              Prefer the currently recorded one.  We want the first one in the
1609              block and the block is scanned from start to end.  */
1610           ; /* nothing to do */
1611         }
1612       else
1613         {
1614           /* First occurrence of this expression in this basic block.  */
1615           antic_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
1616           bytes_used += sizeof (struct occr);
1617           /* First occurrence of this expression in any block?  */
1618           if (cur_expr->antic_occr == NULL)
1619             cur_expr->antic_occr = antic_occr;
1620           else
1621             last_occr->next = antic_occr;
1622           antic_occr->insn = insn;
1623           antic_occr->next = NULL;
1624         }
1625     }
1626
1627   if (avail_p)
1628     {
1629       avail_occr = cur_expr->avail_occr;
1630
1631       /* Search for another occurrence in the same basic block.  */
1632       while (avail_occr && BLOCK_NUM (avail_occr->insn) != BLOCK_NUM (insn))
1633         {
1634           /* If an occurrence isn't found, save a pointer to the end of
1635              the list.  */
1636           last_occr = avail_occr;
1637           avail_occr = avail_occr->next;
1638         }
1639
1640       if (avail_occr)
1641         {
1642           /* Found another instance of the expression in the same basic block.
1643              Prefer this occurrence to the currently recorded one.  We want
1644              the last one in the block and the block is scanned from start
1645              to end.  */
1646           avail_occr->insn = insn;
1647         }
1648       else
1649         {
1650           /* First occurrence of this expression in this basic block.  */
1651           avail_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
1652           bytes_used += sizeof (struct occr);
1653           /* First occurrence of this expression in any block?  */
1654           if (cur_expr->avail_occr == NULL)
1655             cur_expr->avail_occr = avail_occr;
1656           else
1657             last_occr->next = avail_occr;
1658           avail_occr->insn = insn;
1659           avail_occr->next = NULL;
1660         }
1661     }
1662 }
1663
1664 /* Insert pattern X in INSN in the hash table.
1665    X is a SET of a reg to either another reg or a constant.
1666    If it is already present, record it as the last occurrence in INSN's
1667    basic block.  */
1668
1669 static void
1670 insert_set_in_table (x, insn)
1671      rtx x;
1672      rtx insn;
1673 {
1674   int found;
1675   unsigned int hash;
1676   struct expr *cur_expr, *last_expr = NULL;
1677   struct occr *cur_occr, *last_occr = NULL;
1678
1679   if (GET_CODE (x) != SET
1680       || GET_CODE (SET_DEST (x)) != REG)
1681     abort ();
1682
1683   hash = hash_set (REGNO (SET_DEST (x)), set_hash_table_size);
1684
1685   cur_expr = set_hash_table[hash];
1686   found = 0;
1687
1688   while (cur_expr && ! (found = expr_equiv_p (cur_expr->expr, x)))
1689     {
1690       /* If the expression isn't found, save a pointer to the end of
1691          the list.  */
1692       last_expr = cur_expr;
1693       cur_expr = cur_expr->next_same_hash;
1694     }
1695
1696   if (! found)
1697     {
1698       cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
1699       bytes_used += sizeof (struct expr);
1700       if (set_hash_table[hash] == NULL)
1701         {
1702           /* This is the first pattern that hashed to this index.  */
1703           set_hash_table[hash] = cur_expr;
1704         }
1705       else
1706         {
1707           /* Add EXPR to end of this hash chain.  */
1708           last_expr->next_same_hash = cur_expr;
1709         }
1710       /* Set the fields of the expr element.
1711          We must copy X because it can be modified when copy propagation is
1712          performed on its operands.  */
1713       /* ??? Should this go in a different obstack?  */
1714       cur_expr->expr = copy_rtx (x);
1715       cur_expr->bitmap_index = n_sets++;
1716       cur_expr->next_same_hash = NULL;
1717       cur_expr->antic_occr = NULL;
1718       cur_expr->avail_occr = NULL;
1719     }
1720
1721   /* Now record the occurrence.  */
1722
1723   cur_occr = cur_expr->avail_occr;
1724
1725   /* Search for another occurrence in the same basic block.  */
1726   while (cur_occr && BLOCK_NUM (cur_occr->insn) != BLOCK_NUM (insn))
1727     {
1728       /* If an occurrence isn't found, save a pointer to the end of
1729          the list.  */
1730       last_occr = cur_occr;
1731       cur_occr = cur_occr->next;
1732     }
1733
1734   if (cur_occr)
1735     {
1736       /* Found another instance of the expression in the same basic block.
1737          Prefer this occurrence to the currently recorded one.  We want
1738          the last one in the block and the block is scanned from start
1739          to end.  */
1740       cur_occr->insn = insn;
1741     }
1742   else
1743     {
1744       /* First occurrence of this expression in this basic block.  */
1745       cur_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
1746       bytes_used += sizeof (struct occr);
1747       /* First occurrence of this expression in any block?  */
1748       if (cur_expr->avail_occr == NULL)
1749         cur_expr->avail_occr = cur_occr;
1750       else
1751         last_occr->next = cur_occr;
1752       cur_occr->insn = insn;
1753       cur_occr->next = NULL;
1754     }
1755 }
1756
1757 /* Scan pattern PAT of INSN and add an entry to the hash table.
1758    If SET_P is non-zero, this is for the assignment hash table,
1759    otherwise it is for the expression hash table.  */
1760
1761 static void
1762 hash_scan_set (pat, insn, set_p)
1763      rtx pat, insn;
1764      int set_p;
1765 {
1766   rtx src = SET_SRC (pat);
1767   rtx dest = SET_DEST (pat);
1768
1769   if (GET_CODE (src) == CALL)
1770     hash_scan_call (src, insn);
1771
1772   if (GET_CODE (dest) == REG)
1773     {
1774       int regno = REGNO (dest);
1775       rtx tmp;
1776
1777       /* Only record sets of pseudo-regs in the hash table.  */
1778       if (! set_p
1779           && regno >= FIRST_PSEUDO_REGISTER
1780           /* Don't GCSE something if we can't do a reg/reg copy.  */
1781           && can_copy_p [GET_MODE (dest)]
1782           /* Is SET_SRC something we want to gcse?  */
1783           && want_to_gcse_p (src))
1784         {
1785           /* An expression is not anticipatable if its operands are
1786              modified before this insn.  */
1787           int antic_p = ! optimize_size && oprs_anticipatable_p (src, insn);
1788           /* An expression is not available if its operands are
1789              subsequently modified, including this insn.  */
1790           int avail_p = oprs_available_p (src, insn);
1791           insert_expr_in_table (src, GET_MODE (dest), insn, antic_p, avail_p);
1792         }
1793       /* Record sets for constant/copy propagation.  */
1794       else if (set_p
1795                && regno >= FIRST_PSEUDO_REGISTER
1796                && ((GET_CODE (src) == REG
1797                     && REGNO (src) >= FIRST_PSEUDO_REGISTER
1798                     && can_copy_p [GET_MODE (dest)])
1799                    /* ??? CONST_INT:wip */
1800                    || GET_CODE (src) == CONST_INT)
1801                /* A copy is not available if its src or dest is subsequently
1802                   modified.  Here we want to search from INSN+1 on, but
1803                   oprs_available_p searches from INSN on.  */
1804                && (insn == BLOCK_END (BLOCK_NUM (insn))
1805                    || ((tmp = next_nonnote_insn (insn)) != NULL_RTX
1806                        && oprs_available_p (pat, tmp))))
1807         insert_set_in_table (pat, insn);
1808     }
1809
1810   /* Check if first/last set in this block for classic gcse,
1811      but not for copy/constant propagation.  */
1812   if (optimize_size && !set_p)
1813       
1814     {
1815       rtx dest = SET_DEST (pat);
1816
1817       while (GET_CODE (dest) == SUBREG
1818              || GET_CODE (dest) == ZERO_EXTRACT
1819              || GET_CODE (dest) == SIGN_EXTRACT
1820              || GET_CODE (dest) == STRICT_LOW_PART)
1821         dest = XEXP (dest, 0);
1822       if (GET_CODE (dest) == REG)
1823         maybe_set_rd_gen (REGNO (dest), insn);
1824     }
1825 }
1826
1827 static void
1828 hash_scan_clobber (x, insn)
1829      rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
1830 {
1831   /* Currently nothing to do.  */
1832 }
1833
1834 static void
1835 hash_scan_call (x, insn)
1836      rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
1837 {
1838   /* Currently nothing to do.  */
1839 }
1840
1841 /* Process INSN and add hash table entries as appropriate.
1842
1843    Only available expressions that set a single pseudo-reg are recorded.
1844
1845    Single sets in a PARALLEL could be handled, but it's an extra complication
1846    that isn't dealt with right now.  The trick is handling the CLOBBERs that
1847    are also in the PARALLEL.  Later.
1848
1849    If SET_P is non-zero, this is for the assignment hash table,
1850    otherwise it is for the expression hash table.  */
1851
1852 static void
1853 hash_scan_insn (insn, set_p)
1854      rtx insn;
1855      int set_p;
1856 {
1857   rtx pat = PATTERN (insn);
1858
1859   /* Pick out the sets of INSN and for other forms of instructions record
1860      what's been modified.  */
1861
1862   if (GET_CODE (pat) == SET)
1863     hash_scan_set (pat, insn, set_p);
1864   else if (GET_CODE (pat) == PARALLEL)
1865     {
1866       int i;
1867
1868       for (i = 0; i < XVECLEN (pat, 0); i++)
1869         {
1870           rtx x = XVECEXP (pat, 0, i);
1871
1872           if (GET_CODE (x) == SET)
1873             {
1874               if (GET_CODE (SET_SRC (x)) == CALL)
1875                 hash_scan_call (SET_SRC (x), insn);
1876
1877               /* Check if first/last set in this block for classic
1878                  gcse, but not for constant/copy propagation.  */
1879               if (optimize_size && !set_p)
1880                 {
1881                   rtx dest = SET_DEST (x);
1882
1883                   while (GET_CODE (dest) == SUBREG
1884                          || GET_CODE (dest) == ZERO_EXTRACT
1885                          || GET_CODE (dest) == SIGN_EXTRACT
1886                          || GET_CODE (dest) == STRICT_LOW_PART)
1887                     dest = XEXP (dest, 0);
1888                   if (GET_CODE (dest) == REG)
1889                     maybe_set_rd_gen (REGNO (dest), insn);
1890                 }
1891             }
1892           else if (GET_CODE (x) == CLOBBER)
1893             hash_scan_clobber (x, insn);
1894           else if (GET_CODE (x) == CALL)
1895             hash_scan_call (x, insn);
1896         }
1897     }
1898   else if (GET_CODE (pat) == CLOBBER)
1899     hash_scan_clobber (pat, insn);
1900   else if (GET_CODE (pat) == CALL)
1901     hash_scan_call (pat, insn);
1902 }
1903
1904 static void
1905 dump_hash_table (file, name, table, table_size, total_size)
1906      FILE *file;
1907      char *name;
1908      struct expr **table;
1909      int table_size, total_size;
1910 {
1911   int i;
1912   /* Flattened out table, so it's printed in proper order.  */
1913   struct expr **flat_table = (struct expr **) alloca (total_size * sizeof (struct expr *));
1914   unsigned int *hash_val = (unsigned int *) alloca (total_size * sizeof (unsigned int));
1915
1916   bzero ((char *) flat_table, total_size * sizeof (struct expr *));
1917   for (i = 0; i < table_size; i++)
1918     {
1919       struct expr *expr;
1920
1921       for (expr = table[i]; expr != NULL; expr = expr->next_same_hash)
1922         {
1923           flat_table[expr->bitmap_index] = expr;
1924           hash_val[expr->bitmap_index] = i;
1925         }
1926     }
1927
1928   fprintf (file, "%s hash table (%d buckets, %d entries)\n",
1929            name, table_size, total_size);
1930
1931   for (i = 0; i < total_size; i++)
1932     {
1933       struct expr *expr = flat_table[i];
1934
1935       fprintf (file, "Index %d (hash value %d)\n  ",
1936                expr->bitmap_index, hash_val[i]);
1937       print_rtl (file, expr->expr);
1938       fprintf (file, "\n");
1939     }
1940
1941   fprintf (file, "\n");
1942 }
1943
1944 /* Record register first/last/block set information for REGNO in INSN.
1945    reg_first_set records the first place in the block where the register
1946    is set and is used to compute "anticipatability".
1947    reg_last_set records the last place in the block where the register
1948    is set and is used to compute "availability".
1949    reg_set_in_block records whether the register is set in the block
1950    and is used to compute "transparency".  */
1951
1952 static void
1953 record_last_reg_set_info (insn, regno)
1954      rtx insn;
1955      int regno;
1956 {
1957   if (reg_first_set[regno] == 0)
1958     reg_first_set[regno] = INSN_CUID (insn);
1959   reg_last_set[regno] = INSN_CUID (insn);
1960   SET_BIT (reg_set_in_block[BLOCK_NUM (insn)], regno);
1961 }
1962
1963 /* Record memory first/last/block set information for INSN.  */
1964
1965 static void
1966 record_last_mem_set_info (insn)
1967      rtx insn;
1968 {
1969   if (mem_first_set == 0)
1970     mem_first_set = INSN_CUID (insn);
1971   mem_last_set = INSN_CUID (insn);
1972   mem_set_in_block[BLOCK_NUM (insn)] = 1;
1973 }
1974
1975 /* Used for communicating between next two routines.  */
1976 static rtx last_set_insn;
1977
1978 /* Called from compute_hash_table via note_stores to handle one
1979    SET or CLOBBER in an insn.  */
1980
1981 static void
1982 record_last_set_info (dest, setter)
1983      rtx dest, setter ATTRIBUTE_UNUSED;
1984 {
1985   if (GET_CODE (dest) == SUBREG)
1986     dest = SUBREG_REG (dest);
1987
1988   if (GET_CODE (dest) == REG)
1989     record_last_reg_set_info (last_set_insn, REGNO (dest));
1990   else if (GET_CODE (dest) == MEM
1991            /* Ignore pushes, they clobber nothing.  */
1992            && ! push_operand (dest, GET_MODE (dest)))
1993     record_last_mem_set_info (last_set_insn);
1994 }
1995
1996 /* Top level function to create an expression or assignment hash table.
1997
1998    Expression entries are placed in the hash table if
1999    - they are of the form (set (pseudo-reg) src),
2000    - src is something we want to perform GCSE on,
2001    - none of the operands are subsequently modified in the block
2002
2003    Assignment entries are placed in the hash table if
2004    - they are of the form (set (pseudo-reg) src),
2005    - src is something we want to perform const/copy propagation on,
2006    - none of the operands or target are subsequently modified in the block
2007    Currently src must be a pseudo-reg or a const_int.
2008
2009    F is the first insn.
2010    SET_P is non-zero for computing the assignment hash table.  */
2011
2012 static void
2013 compute_hash_table (f, set_p)
2014      rtx f ATTRIBUTE_UNUSED;
2015      int set_p;
2016 {
2017   int bb;
2018
2019   /* While we compute the hash table we also compute a bit array of which
2020      registers are set in which blocks.
2021      We also compute which blocks set memory, in the absence of aliasing
2022      support [which is TODO].
2023      ??? This isn't needed during const/copy propagation, but it's cheap to
2024      compute.  Later.  */
2025   sbitmap_vector_zero (reg_set_in_block, n_basic_blocks);
2026   bzero ((char *) mem_set_in_block, n_basic_blocks);
2027
2028   /* Some working arrays used to track first and last set in each block.  */
2029   /* ??? One could use alloca here, but at some size a threshold is crossed
2030      beyond which one should use malloc.  Are we at that threshold here?  */
2031   reg_first_set = (int *) gmalloc (max_gcse_regno * sizeof (int));
2032   reg_last_set = (int *) gmalloc (max_gcse_regno * sizeof (int));
2033
2034   for (bb = 0; bb < n_basic_blocks; bb++)
2035     {
2036       rtx insn;
2037       int regno;
2038
2039       /* First pass over the instructions records information used to
2040          determine when registers and memory are first and last set.
2041          ??? The mem_set_in_block and hard-reg reg_set_in_block computation
2042          could be moved to compute_sets since they currently don't change.  */
2043
2044       bzero ((char *) reg_first_set, max_gcse_regno * sizeof (int));
2045       bzero ((char *) reg_last_set, max_gcse_regno * sizeof (int));
2046       mem_first_set = 0;
2047       mem_last_set = 0;
2048
2049       for (insn = basic_block_head[bb];
2050            insn && insn != NEXT_INSN (basic_block_end[bb]);
2051            insn = NEXT_INSN (insn))
2052         {
2053 #ifdef NON_SAVING_SETJMP 
2054           if (NON_SAVING_SETJMP && GET_CODE (insn) == NOTE
2055               && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
2056             {
2057               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2058                 record_last_reg_set_info (insn, regno);
2059               continue;
2060             }
2061 #endif
2062
2063           if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
2064             continue;
2065
2066           if (GET_CODE (insn) == CALL_INSN)
2067             {
2068               for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2069                 if (call_used_regs[regno])
2070                   record_last_reg_set_info (insn, regno);
2071               if (! CONST_CALL_P (insn))
2072                 record_last_mem_set_info (insn);
2073             }
2074
2075           last_set_insn = insn;
2076           note_stores (PATTERN (insn), record_last_set_info);
2077         }
2078
2079       /* The next pass builds the hash table.  */
2080
2081       for (insn = basic_block_head[bb];
2082            insn && insn != NEXT_INSN (basic_block_end[bb]);
2083            insn = NEXT_INSN (insn))
2084         {
2085           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2086             hash_scan_insn (insn, set_p);
2087         }
2088     }
2089
2090   free (reg_first_set);
2091   free (reg_last_set);
2092   /* Catch bugs early.  */
2093   reg_first_set = reg_last_set = 0;
2094 }
2095
2096 /* Allocate space for the set hash table.
2097    N_INSNS is the number of instructions in the function.
2098    It is used to determine the number of buckets to use.  */
2099
2100 static void
2101 alloc_set_hash_table (n_insns)
2102      int n_insns;
2103 {
2104   int n;
2105
2106   set_hash_table_size = n_insns / 4;
2107   if (set_hash_table_size < 11)
2108     set_hash_table_size = 11;
2109   /* Attempt to maintain efficient use of hash table.
2110      Making it an odd number is simplest for now.
2111      ??? Later take some measurements.  */
2112   set_hash_table_size |= 1;
2113   n = set_hash_table_size * sizeof (struct expr *);
2114   set_hash_table = (struct expr **) gmalloc (n);
2115 }
2116
2117 /* Free things allocated by alloc_set_hash_table.  */
2118
2119 static void
2120 free_set_hash_table ()
2121 {
2122   free (set_hash_table);
2123 }
2124
2125 /* Compute the hash table for doing copy/const propagation.  */
2126
2127 static void
2128 compute_set_hash_table (f)
2129      rtx f;
2130 {
2131   /* Initialize count of number of entries in hash table.  */
2132   n_sets = 0;
2133   bzero ((char *) set_hash_table, set_hash_table_size * sizeof (struct expr *));
2134
2135   compute_hash_table (f, 1);
2136 }
2137
2138 /* Allocate space for the expression hash table.
2139    N_INSNS is the number of instructions in the function.
2140    It is used to determine the number of buckets to use.  */
2141
2142 static void
2143 alloc_expr_hash_table (n_insns)
2144      int n_insns;
2145 {
2146   int n;
2147
2148   expr_hash_table_size = n_insns / 2;
2149   /* Make sure the amount is usable.  */
2150   if (expr_hash_table_size < 11)
2151     expr_hash_table_size = 11;
2152   /* Attempt to maintain efficient use of hash table.
2153      Making it an odd number is simplest for now.
2154      ??? Later take some measurements.  */
2155   expr_hash_table_size |= 1;
2156   n = expr_hash_table_size * sizeof (struct expr *);
2157   expr_hash_table = (struct expr **) gmalloc (n);
2158 }
2159
2160 /* Free things allocated by alloc_expr_hash_table.  */
2161
2162 static void
2163 free_expr_hash_table ()
2164 {
2165   free (expr_hash_table);
2166 }
2167
2168 /* Compute the hash table for doing GCSE.  */
2169
2170 static void
2171 compute_expr_hash_table (f)
2172      rtx f;
2173 {
2174   /* Initialize count of number of entries in hash table.  */
2175   n_exprs = 0;
2176   bzero ((char *) expr_hash_table, expr_hash_table_size * sizeof (struct expr *));
2177
2178   compute_hash_table (f, 0);
2179 }
2180 \f
2181 /* Expression tracking support.  */
2182
2183 /* Lookup pattern PAT in the expression table.
2184    The result is a pointer to the table entry, or NULL if not found.  */
2185
2186 static struct expr *
2187 lookup_expr (pat)
2188      rtx pat;
2189 {
2190   int do_not_record_p;
2191   unsigned int hash = hash_expr (pat, GET_MODE (pat), &do_not_record_p,
2192                                  expr_hash_table_size);
2193   struct expr *expr;
2194
2195   if (do_not_record_p)
2196     return NULL;
2197
2198   expr = expr_hash_table[hash];
2199
2200   while (expr && ! expr_equiv_p (expr->expr, pat))
2201     expr = expr->next_same_hash;
2202
2203   return expr;
2204 }
2205
2206 /* Lookup REGNO in the set table.
2207    If PAT is non-NULL look for the entry that matches it, otherwise return
2208    the first entry for REGNO.
2209    The result is a pointer to the table entry, or NULL if not found.  */
2210
2211 static struct expr *
2212 lookup_set (regno, pat)
2213      int regno;
2214      rtx pat;
2215 {
2216   unsigned int hash = hash_set (regno, set_hash_table_size);
2217   struct expr *expr;
2218
2219   expr = set_hash_table[hash];
2220
2221   if (pat)
2222     {
2223       while (expr && ! expr_equiv_p (expr->expr, pat))
2224         expr = expr->next_same_hash;
2225     }
2226   else
2227     {
2228       while (expr && REGNO (SET_DEST (expr->expr)) != regno)
2229         expr = expr->next_same_hash;
2230     }
2231
2232   return expr;
2233 }
2234
2235 /* Return the next entry for REGNO in list EXPR.  */
2236
2237 static struct expr *
2238 next_set (regno, expr)
2239      int regno;
2240      struct expr *expr;
2241 {
2242   do
2243     expr = expr->next_same_hash;
2244   while (expr && REGNO (SET_DEST (expr->expr)) != regno);
2245   return expr;
2246 }
2247
2248 /* Reset tables used to keep track of what's still available [since the
2249    start of the block].  */
2250
2251 static void
2252 reset_opr_set_tables ()
2253 {
2254   /* Maintain a bitmap of which regs have been set since beginning of
2255      the block.  */
2256   sbitmap_zero (reg_set_bitmap);
2257   /* Also keep a record of the last instruction to modify memory.
2258      For now this is very trivial, we only record whether any memory
2259      location has been modified.  */
2260   mem_last_set = 0;
2261 }
2262
2263 /* Return non-zero if the operands of X are not set before INSN in
2264    INSN's basic block.  */
2265
2266 static int
2267 oprs_not_set_p (x, insn)
2268      rtx x, insn;
2269 {
2270   int i;
2271   enum rtx_code code;
2272   char *fmt;
2273
2274   /* repeat is used to turn tail-recursion into iteration.  */
2275 repeat:
2276
2277   if (x == 0)
2278     return 1;
2279
2280   code = GET_CODE (x);
2281   switch (code)
2282     {
2283     case PC:
2284     case CC0:
2285     case CONST:
2286     case CONST_INT:
2287     case CONST_DOUBLE:
2288     case SYMBOL_REF:
2289     case LABEL_REF:
2290     case ADDR_VEC:
2291     case ADDR_DIFF_VEC:
2292       return 1;
2293
2294     case MEM:
2295       if (mem_last_set != 0)
2296         return 0;
2297       x = XEXP (x, 0);
2298       goto repeat;
2299
2300     case REG:
2301       return ! TEST_BIT (reg_set_bitmap, REGNO (x));
2302
2303     default:
2304       break;
2305     }
2306
2307   fmt = GET_RTX_FORMAT (code);
2308   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2309     {
2310       if (fmt[i] == 'e')
2311         {
2312           int not_set_p;
2313           /* If we are about to do the last recursive call
2314              needed at this level, change it into iteration.
2315              This function is called enough to be worth it.  */
2316           if (i == 0)
2317             {
2318               x = XEXP (x, 0);
2319               goto repeat;
2320             }
2321           not_set_p = oprs_not_set_p (XEXP (x, i), insn);
2322           if (! not_set_p)
2323             return 0;
2324         }
2325       else if (fmt[i] == 'E')
2326         {
2327           int j;
2328           for (j = 0; j < XVECLEN (x, i); j++)
2329             {
2330               int not_set_p = oprs_not_set_p (XVECEXP (x, i, j), insn);
2331               if (! not_set_p)
2332                 return 0;
2333             }
2334         }
2335     }
2336
2337   return 1;
2338 }
2339
2340 /* Mark things set by a CALL.  */
2341
2342 static void
2343 mark_call (pat, insn)
2344      rtx pat ATTRIBUTE_UNUSED, insn;
2345 {
2346   mem_last_set = INSN_CUID (insn);
2347 }
2348
2349 /* Mark things set by a SET.  */
2350
2351 static void
2352 mark_set (pat, insn)
2353      rtx pat, insn;
2354 {
2355   rtx dest = SET_DEST (pat);
2356
2357   while (GET_CODE (dest) == SUBREG
2358          || GET_CODE (dest) == ZERO_EXTRACT
2359          || GET_CODE (dest) == SIGN_EXTRACT
2360          || GET_CODE (dest) == STRICT_LOW_PART)
2361     dest = XEXP (dest, 0);
2362
2363   if (GET_CODE (dest) == REG)
2364     SET_BIT (reg_set_bitmap, REGNO (dest));
2365   else if (GET_CODE (dest) == MEM)
2366     mem_last_set = INSN_CUID (insn);
2367
2368   if (GET_CODE (SET_SRC (pat)) == CALL)
2369     mark_call (SET_SRC (pat), insn);
2370 }
2371
2372 /* Record things set by a CLOBBER.  */
2373
2374 static void
2375 mark_clobber (pat, insn)
2376      rtx pat, insn;
2377 {
2378   rtx clob = XEXP (pat, 0);
2379
2380   while (GET_CODE (clob) == SUBREG || GET_CODE (clob) == STRICT_LOW_PART)
2381     clob = XEXP (clob, 0);
2382
2383   if (GET_CODE (clob) == REG)
2384     SET_BIT (reg_set_bitmap, REGNO (clob));
2385   else
2386     mem_last_set = INSN_CUID (insn);
2387 }
2388
2389 /* Record things set by INSN.
2390    This data is used by oprs_not_set_p.  */
2391
2392 static void
2393 mark_oprs_set (insn)
2394      rtx insn;
2395 {
2396   rtx pat = PATTERN (insn);
2397
2398   if (GET_CODE (pat) == SET)
2399     mark_set (pat, insn);
2400   else if (GET_CODE (pat) == PARALLEL)
2401     {
2402       int i;
2403
2404       for (i = 0; i < XVECLEN (pat, 0); i++)
2405         {
2406           rtx x = XVECEXP (pat, 0, i);
2407
2408           if (GET_CODE (x) == SET)
2409             mark_set (x, insn);
2410           else if (GET_CODE (x) == CLOBBER)
2411             mark_clobber (x, insn);
2412           else if (GET_CODE (x) == CALL)
2413             mark_call (x, insn);
2414         }
2415     }
2416   else if (GET_CODE (pat) == CLOBBER)
2417     mark_clobber (pat, insn);
2418   else if (GET_CODE (pat) == CALL)
2419     mark_call (pat, insn);
2420 }
2421 \f
2422 /* Classic GCSE reaching definition support.  */
2423
2424 /* Allocate reaching def variables.  */
2425
2426 static void
2427 alloc_rd_mem (n_blocks, n_insns)
2428      int n_blocks, n_insns;
2429 {
2430   rd_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2431   sbitmap_vector_zero (rd_kill, n_basic_blocks);
2432
2433   rd_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2434   sbitmap_vector_zero (rd_gen, n_basic_blocks);
2435
2436   reaching_defs = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2437   sbitmap_vector_zero (reaching_defs, n_basic_blocks);
2438
2439   rd_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2440   sbitmap_vector_zero (rd_out, n_basic_blocks);
2441 }
2442
2443 /* Free reaching def variables.  */
2444
2445 static void
2446 free_rd_mem ()
2447 {
2448   free (rd_kill);
2449   free (rd_gen);
2450   free (reaching_defs);
2451   free (rd_out);
2452 }
2453
2454 /* Add INSN to the kills of BB.
2455    REGNO, set in BB, is killed by INSN.  */
2456
2457 static void
2458 handle_rd_kill_set (insn, regno, bb)
2459      rtx insn;
2460      int regno, bb;
2461 {
2462   struct reg_set *this_reg = reg_set_table[regno];
2463
2464   while (this_reg)
2465     {
2466       if (BLOCK_NUM (this_reg->insn) != BLOCK_NUM (insn))
2467         SET_BIT (rd_kill[bb], INSN_CUID (this_reg->insn));
2468       this_reg = this_reg->next;
2469     }
2470 }
2471
2472 void
2473 dump_rd_table (file, title, bmap)
2474      FILE *file;
2475      char *title;
2476      sbitmap *bmap;
2477 {
2478   int bb,cuid,i,j,n;
2479
2480   fprintf (file, "%s\n", title);
2481   for (bb = 0; bb < n_basic_blocks; bb++)
2482     {
2483       fprintf (file, "BB %d\n", bb);
2484       dump_sbitmap (file, bmap[bb]);
2485       for (i = n = cuid = 0; i < bmap[bb]->size; i++)
2486         {
2487           for (j = 0; j < SBITMAP_ELT_BITS; j++, cuid++)
2488             {
2489               if ((bmap[bb]->elms[i] & (1 << j)) != 0)
2490                 {
2491                   if (n % 10 == 0)
2492                     fprintf (file, " ");
2493                   fprintf (file, " %d", INSN_UID (CUID_INSN (cuid)));
2494                   n++;
2495                 }
2496             }
2497         }
2498       if (n != 0)
2499         fprintf (file, "\n");
2500     }
2501   fprintf (file, "\n");
2502 }
2503
2504 /* Compute the set of kill's for reaching definitions.  */
2505
2506 static void
2507 compute_kill_rd ()
2508 {
2509   int bb,cuid;
2510
2511   /* For each block
2512        For each set bit in `gen' of the block (i.e each insn which
2513            generates a definition in the block)
2514          Call the reg set by the insn corresponding to that bit regx
2515          Look at the linked list starting at reg_set_table[regx]
2516          For each setting of regx in the linked list, which is not in
2517              this block
2518            Set the bit in `kill' corresponding to that insn
2519     */
2520
2521   for (bb = 0; bb < n_basic_blocks; bb++)
2522     {
2523       for (cuid = 0; cuid < max_cuid; cuid++)
2524         {
2525           if (TEST_BIT (rd_gen[bb], cuid))
2526             {
2527               rtx insn = CUID_INSN (cuid);
2528               rtx pat = PATTERN (insn);
2529
2530               if (GET_CODE (insn) == CALL_INSN)
2531                 {
2532                   int regno;
2533
2534                   for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2535                     {
2536                       if (call_used_regs[regno])
2537                         handle_rd_kill_set (insn, regno, bb);
2538                     }
2539                 }
2540
2541               if (GET_CODE (pat) == PARALLEL)
2542                 {
2543                   int i;
2544
2545                   /* We work backwards because ... */
2546                   for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
2547                     {
2548                       enum rtx_code code = GET_CODE (XVECEXP (pat, 0, i));
2549                       if ((code == SET || code == CLOBBER)
2550                           && GET_CODE (XEXP (XVECEXP (pat, 0, i), 0)) == REG)
2551                         handle_rd_kill_set (insn,
2552                                             REGNO (XEXP (XVECEXP (pat, 0, i), 0)),
2553                                             bb);
2554                     }
2555                 }
2556               else if (GET_CODE (pat) == SET)
2557                 {
2558                   if (GET_CODE (SET_DEST (pat)) == REG)
2559                     {
2560                       /* Each setting of this register outside of this block
2561                          must be marked in the set of kills in this block.  */
2562                       handle_rd_kill_set (insn, REGNO (SET_DEST (pat)), bb);
2563                     }
2564                 }
2565               /* FIXME: CLOBBER? */
2566             }
2567         }
2568     }
2569 }
2570
2571 /* Compute the reaching definitions as in 
2572    Compilers Principles, Techniques, and Tools. Aho, Sethi, Ullman,
2573    Chapter 10.  It is the same algorithm as used for computing available
2574    expressions but applied to the gens and kills of reaching definitions.  */
2575
2576 static void
2577 compute_rd ()
2578 {
2579   int bb, changed, passes;
2580
2581   for (bb = 0; bb < n_basic_blocks; bb++)
2582     sbitmap_copy (rd_out[bb] /*dst*/, rd_gen[bb] /*src*/);
2583
2584   passes = 0;
2585   changed = 1;
2586   while (changed)
2587     {
2588       changed = 0;
2589       for (bb = 0; bb < n_basic_blocks; bb++)
2590         {
2591           sbitmap_union_of_predecessors (reaching_defs[bb], rd_out,
2592                                          bb, s_preds);
2593           changed |= sbitmap_union_of_diff (rd_out[bb], rd_gen[bb],
2594                                             reaching_defs[bb], rd_kill[bb]);
2595         }
2596       passes++;
2597     }
2598
2599   if (gcse_file)
2600     fprintf (gcse_file, "reaching def computation: %d passes\n", passes);
2601 }
2602 \f
2603 /* Classic GCSE available expression support.  */
2604
2605 /* Allocate memory for available expression computation.  */
2606
2607 static void
2608 alloc_avail_expr_mem (n_blocks, n_exprs)
2609      int n_blocks, n_exprs;
2610 {
2611   ae_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2612   sbitmap_vector_zero (ae_kill, n_basic_blocks);
2613
2614   ae_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2615   sbitmap_vector_zero (ae_gen, n_basic_blocks);
2616
2617   ae_in = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2618   sbitmap_vector_zero (ae_in, n_basic_blocks);
2619
2620   ae_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2621   sbitmap_vector_zero (ae_out, n_basic_blocks);
2622
2623   u_bitmap = (sbitmap) sbitmap_alloc (n_exprs);
2624   sbitmap_ones (u_bitmap);
2625 }
2626
2627 static void
2628 free_avail_expr_mem ()
2629 {
2630   free (ae_kill);
2631   free (ae_gen);
2632   free (ae_in);
2633   free (ae_out);
2634   free (u_bitmap);
2635 }
2636
2637 /* Compute the set of available expressions generated in each basic block.  */
2638
2639 static void
2640 compute_ae_gen ()
2641 {
2642   int i;
2643
2644   /* For each recorded occurrence of each expression, set ae_gen[bb][expr].
2645      This is all we have to do because an expression is not recorded if it
2646      is not available, and the only expressions we want to work with are the
2647      ones that are recorded.  */
2648
2649   for (i = 0; i < expr_hash_table_size; i++)
2650     {
2651       struct expr *expr = expr_hash_table[i];
2652       while (expr != NULL)
2653         {
2654           struct occr *occr = expr->avail_occr;
2655           while (occr != NULL)
2656             {
2657               SET_BIT (ae_gen[BLOCK_NUM (occr->insn)], expr->bitmap_index);
2658               occr = occr->next;
2659             }
2660           expr = expr->next_same_hash;
2661         }
2662     }
2663 }
2664
2665 /* Return non-zero if expression X is killed in BB.  */
2666
2667 static int
2668 expr_killed_p (x, bb)
2669      rtx x;
2670      int bb;
2671 {
2672   int i;
2673   enum rtx_code code;
2674   char *fmt;
2675
2676   /* repeat is used to turn tail-recursion into iteration.  */
2677  repeat:
2678
2679   if (x == 0)
2680     return 1;
2681
2682   code = GET_CODE (x);
2683   switch (code)
2684     {
2685     case REG:
2686       return TEST_BIT (reg_set_in_block[bb], REGNO (x));
2687
2688     case MEM:
2689       if (mem_set_in_block[bb])
2690         return 1;
2691       x = XEXP (x, 0);
2692       goto repeat;
2693
2694     case PC:
2695     case CC0: /*FIXME*/
2696     case CONST:
2697     case CONST_INT:
2698     case CONST_DOUBLE:
2699     case SYMBOL_REF:
2700     case LABEL_REF:
2701     case ADDR_VEC:
2702     case ADDR_DIFF_VEC:
2703       return 0;
2704
2705     default:
2706       break;
2707     }
2708
2709   i = GET_RTX_LENGTH (code) - 1;
2710   fmt = GET_RTX_FORMAT (code);
2711   for (; i >= 0; i--)
2712     {
2713       if (fmt[i] == 'e')
2714         {
2715           rtx tem = XEXP (x, i);
2716
2717           /* If we are about to do the last recursive call
2718              needed at this level, change it into iteration.
2719              This function is called enough to be worth it.  */
2720           if (i == 0)
2721             {
2722               x = tem;
2723               goto repeat;
2724             }
2725           if (expr_killed_p (tem, bb))
2726             return 1;
2727         }
2728       else if (fmt[i] == 'E')
2729         {
2730           int j;
2731           for (j = 0; j < XVECLEN (x, i); j++)
2732             {
2733               if (expr_killed_p (XVECEXP (x, i, j), bb))
2734                 return 1;
2735             }
2736         }
2737     }
2738
2739   return 0;
2740 }
2741
2742 /* Compute the set of available expressions killed in each basic block.  */
2743
2744 static void
2745 compute_ae_kill ()
2746 {
2747   int bb,i;
2748
2749   for (bb = 0; bb < n_basic_blocks; bb++)
2750     {
2751       for (i = 0; i < expr_hash_table_size; i++)
2752         {
2753           struct expr *expr = expr_hash_table[i];
2754
2755           for ( ; expr != NULL; expr = expr->next_same_hash)
2756             {
2757               /* Skip EXPR if generated in this block.  */
2758               if (TEST_BIT (ae_gen[bb], expr->bitmap_index))
2759                 continue;
2760
2761               if (expr_killed_p (expr->expr, bb))
2762                 SET_BIT (ae_kill[bb], expr->bitmap_index);
2763             }
2764         }
2765     }
2766 }
2767
2768 /* Compute available expressions.
2769
2770    Implement the algorithm to find available expressions
2771    as given in the Aho Sethi Ullman book, pages 627-631.  */
2772
2773 static void
2774 compute_available ()
2775 {
2776   int bb, changed, passes;
2777
2778   sbitmap_zero (ae_in[0]);
2779
2780   sbitmap_copy (ae_out[0] /*dst*/, ae_gen[0] /*src*/);
2781
2782   for (bb = 1; bb < n_basic_blocks; bb++)
2783     sbitmap_difference (ae_out[bb], u_bitmap, ae_kill[bb]);
2784     
2785   passes = 0;
2786   changed = 1;
2787   while (changed)
2788     {
2789       changed = 0;
2790       for (bb = 1; bb < n_basic_blocks; bb++)
2791         {
2792           sbitmap_intersect_of_predecessors (ae_in[bb], ae_out,
2793                                              bb, s_preds);
2794           changed |= sbitmap_union_of_diff (ae_out[bb], ae_gen[bb],
2795                                             ae_in[bb], ae_kill[bb]);
2796         }
2797       passes++;
2798     }
2799
2800   if (gcse_file)
2801     fprintf (gcse_file, "avail expr computation: %d passes\n", passes);
2802 }
2803 \f
2804 /* Actually perform the Classic GCSE optimizations.  */
2805
2806 /* Return non-zero if occurrence OCCR of expression EXPR reaches block BB.
2807
2808    CHECK_SELF_LOOP is non-zero if we should consider a block reaching itself
2809    as a positive reach.  We want to do this when there are two computations
2810    of the expression in the block.
2811
2812    VISITED is a pointer to a working buffer for tracking which BB's have
2813    been visited.  It is NULL for the top-level call.
2814
2815    We treat reaching expressions that go through blocks containing the same
2816    reaching expression as "not reaching".  E.g. if EXPR is generated in blocks
2817    2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
2818    2 as not reaching.  The intent is to improve the probability of finding
2819    only one reaching expression and to reduce register lifetimes by picking
2820    the closest such expression.  */
2821
2822 static int
2823 expr_reaches_here_p (occr, expr, bb, check_self_loop, visited)
2824      struct occr *occr;
2825      struct expr *expr;
2826      int bb;
2827      int check_self_loop;
2828      char *visited;
2829 {
2830   int_list_ptr pred;
2831
2832   if (visited == NULL)
2833     {
2834       visited = (char *) alloca (n_basic_blocks);
2835       bzero (visited, n_basic_blocks);
2836     }
2837
2838   for (pred = s_preds[bb]; pred != NULL; pred = pred->next)
2839     {
2840       int pred_bb = INT_LIST_VAL (pred);
2841
2842       if (visited[pred_bb])
2843         {
2844           /* This predecessor has already been visited.
2845              Nothing to do.  */
2846           ;
2847         }
2848       else if (pred_bb == bb)
2849         {
2850           /* BB loops on itself.  */
2851           if (check_self_loop
2852               && TEST_BIT (ae_gen[pred_bb], expr->bitmap_index)
2853               && BLOCK_NUM (occr->insn) == pred_bb)
2854             return 1;
2855           visited[pred_bb] = 1;
2856         }
2857       /* Ignore this predecessor if it kills the expression.  */
2858       else if (TEST_BIT (ae_kill[pred_bb], expr->bitmap_index))
2859         visited[pred_bb] = 1;
2860       /* Does this predecessor generate this expression?  */
2861       else if (TEST_BIT (ae_gen[pred_bb], expr->bitmap_index))
2862         {
2863           /* Is this the occurrence we're looking for?
2864              Note that there's only one generating occurrence per block
2865              so we just need to check the block number.  */
2866           if (BLOCK_NUM (occr->insn) == pred_bb)
2867             return 1;
2868           visited[pred_bb] = 1;
2869         }
2870       /* Neither gen nor kill.  */
2871       else
2872         {
2873           visited[pred_bb] = 1;
2874           if (expr_reaches_here_p (occr, expr, pred_bb, check_self_loop, visited))
2875             return 1;
2876         }
2877     }
2878
2879   /* All paths have been checked.  */
2880   return 0;
2881 }
2882
2883 /* Return the instruction that computes EXPR that reaches INSN's basic block.
2884    If there is more than one such instruction, return NULL.
2885
2886    Called only by handle_avail_expr.  */
2887
2888 static rtx
2889 computing_insn (expr, insn)
2890      struct expr *expr;
2891      rtx insn;
2892 {
2893   int bb = BLOCK_NUM (insn);
2894
2895   if (expr->avail_occr->next == NULL)
2896     {    
2897       if (BLOCK_NUM (expr->avail_occr->insn) == bb)
2898         {
2899           /* The available expression is actually itself
2900              (i.e. a loop in the flow graph) so do nothing.  */
2901           return NULL;
2902         }
2903       /* (FIXME) Case that we found a pattern that was created by
2904          a substitution that took place.  */
2905       return expr->avail_occr->insn;
2906     }
2907   else
2908     {
2909       /* Pattern is computed more than once.
2910          Search backwards from this insn to see how many of these 
2911          computations actually reach this insn.  */
2912       struct occr *occr;
2913       rtx insn_computes_expr = NULL;
2914       int can_reach = 0;
2915
2916       for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
2917         {
2918           if (BLOCK_NUM (occr->insn) == bb)
2919             {
2920               /* The expression is generated in this block.
2921                  The only time we care about this is when the expression
2922                  is generated later in the block [and thus there's a loop].
2923                  We let the normal cse pass handle the other cases.  */
2924               if (INSN_CUID (insn) < INSN_CUID (occr->insn))
2925                 {
2926                   if (expr_reaches_here_p (occr, expr, bb, 1, NULL))
2927                     {
2928                       can_reach++;
2929                       if (can_reach > 1)
2930                         return NULL;
2931                       insn_computes_expr = occr->insn;
2932                     }
2933                 }
2934             }
2935           else /* Computation of the pattern outside this block.  */
2936             {
2937               if (expr_reaches_here_p (occr, expr, bb, 0, NULL))
2938                 {
2939                   can_reach++;
2940                   if (can_reach > 1)
2941                     return NULL;
2942                   insn_computes_expr = occr->insn;
2943                 }
2944             }
2945         }
2946
2947       if (insn_computes_expr == NULL)
2948         abort ();
2949       return insn_computes_expr;
2950     }
2951 }
2952
2953 /* Return non-zero if the definition in DEF_INSN can reach INSN.
2954    Only called by can_disregard_other_sets.  */
2955
2956 static int
2957 def_reaches_here_p (insn, def_insn)
2958      rtx insn, def_insn;
2959 {
2960   rtx reg;
2961
2962   if (TEST_BIT (reaching_defs[BLOCK_NUM (insn)], INSN_CUID (def_insn)))
2963     return 1;
2964
2965   if (BLOCK_NUM (insn) == BLOCK_NUM (def_insn))
2966     {
2967       if (INSN_CUID (def_insn) < INSN_CUID (insn))
2968         {
2969           if (GET_CODE (PATTERN (def_insn)) == PARALLEL)
2970             return 1;
2971           if (GET_CODE (PATTERN (def_insn)) == CLOBBER)
2972             reg = XEXP (PATTERN (def_insn), 0);
2973           else if (GET_CODE (PATTERN (def_insn)) == SET)
2974             reg = SET_DEST (PATTERN (def_insn));
2975           else
2976             abort ();
2977           return ! reg_set_between_p (reg, NEXT_INSN (def_insn), insn);
2978         }
2979       else
2980         return 0;
2981     }
2982
2983   return 0;
2984 }
2985
2986 /* Return non-zero if *ADDR_THIS_REG can only have one value at INSN.
2987    The value returned is the number of definitions that reach INSN.
2988    Returning a value of zero means that [maybe] more than one definition
2989    reaches INSN and the caller can't perform whatever optimization it is
2990    trying.  i.e. it is always safe to return zero.  */
2991
2992 static int
2993 can_disregard_other_sets (addr_this_reg, insn, for_combine)
2994      struct reg_set **addr_this_reg;
2995      rtx insn;
2996      int for_combine;
2997 {
2998   int number_of_reaching_defs = 0;
2999   struct reg_set *this_reg = *addr_this_reg;
3000
3001   while (this_reg)
3002     {
3003       if (def_reaches_here_p (insn, this_reg->insn))
3004         {
3005           number_of_reaching_defs++;
3006           /* Ignore parallels for now.  */
3007           if (GET_CODE (PATTERN (this_reg->insn)) == PARALLEL)
3008             return 0;
3009           if (!for_combine
3010               && (GET_CODE (PATTERN (this_reg->insn)) == CLOBBER
3011                   || ! rtx_equal_p (SET_SRC (PATTERN (this_reg->insn)),
3012                                     SET_SRC (PATTERN (insn)))))
3013             {
3014               /* A setting of the reg to a different value reaches INSN.  */
3015               return 0;
3016             }
3017           if (number_of_reaching_defs > 1)
3018             {
3019               /* If in this setting the value the register is being
3020                  set to is equal to the previous value the register 
3021                  was set to and this setting reaches the insn we are
3022                  trying to do the substitution on then we are ok.  */
3023
3024               if (GET_CODE (PATTERN (this_reg->insn)) == CLOBBER)
3025                 return 0;
3026               if (! rtx_equal_p (SET_SRC (PATTERN (this_reg->insn)),
3027                                  SET_SRC (PATTERN (insn))))
3028                 return 0;
3029             }
3030           *addr_this_reg = this_reg; 
3031         }
3032
3033       /* prev_this_reg = this_reg; */
3034       this_reg = this_reg->next;
3035     }
3036
3037   return number_of_reaching_defs;
3038 }
3039
3040 /* Expression computed by insn is available and the substitution is legal,
3041    so try to perform the substitution.
3042
3043    The result is non-zero if any changes were made.  */
3044
3045 static int
3046 handle_avail_expr (insn, expr)
3047      rtx insn;
3048      struct expr *expr;
3049 {
3050   rtx pat, insn_computes_expr;
3051   rtx to;
3052   struct reg_set *this_reg;
3053   int found_setting, use_src;
3054   int changed = 0;
3055
3056   /* We only handle the case where one computation of the expression
3057      reaches this instruction.  */
3058   insn_computes_expr = computing_insn (expr, insn);
3059   if (insn_computes_expr == NULL)
3060     return 0;
3061
3062   found_setting = 0;
3063   use_src = 0;
3064
3065   /* At this point we know only one computation of EXPR outside of this
3066      block reaches this insn.  Now try to find a register that the
3067      expression is computed into.  */
3068
3069   if (GET_CODE (SET_SRC (PATTERN (insn_computes_expr))) == REG)
3070     {
3071       /* This is the case when the available expression that reaches
3072          here has already been handled as an available expression.  */
3073       int regnum_for_replacing = REGNO (SET_SRC (PATTERN (insn_computes_expr)));
3074       /* If the register was created by GCSE we can't use `reg_set_table',
3075          however we know it's set only once.  */
3076       if (regnum_for_replacing >= max_gcse_regno
3077           /* If the register the expression is computed into is set only once,
3078              or only one set reaches this insn, we can use it.  */
3079           || (((this_reg = reg_set_table[regnum_for_replacing]),
3080                this_reg->next == NULL)
3081               || can_disregard_other_sets (&this_reg, insn, 0)))
3082        {
3083          use_src = 1;
3084          found_setting = 1;
3085        }
3086     }
3087
3088   if (!found_setting)
3089     {
3090       int regnum_for_replacing = REGNO (SET_DEST (PATTERN (insn_computes_expr)));
3091       /* This shouldn't happen.  */
3092       if (regnum_for_replacing >= max_gcse_regno)
3093         abort ();
3094       this_reg = reg_set_table[regnum_for_replacing];
3095       /* If the register the expression is computed into is set only once,
3096          or only one set reaches this insn, use it.  */
3097       if (this_reg->next == NULL
3098           || can_disregard_other_sets (&this_reg, insn, 0))
3099         found_setting = 1;
3100     }
3101
3102   if (found_setting)
3103     {
3104       pat = PATTERN (insn);
3105       if (use_src)
3106         to = SET_SRC (PATTERN (insn_computes_expr));
3107       else
3108         to = SET_DEST (PATTERN (insn_computes_expr));
3109       changed = validate_change (insn, &SET_SRC (pat), to, 0);
3110
3111       /* We should be able to ignore the return code from validate_change but
3112          to play it safe we check.  */
3113       if (changed)
3114         {
3115           gcse_subst_count++;
3116           if (gcse_file != NULL)
3117             {
3118               fprintf (gcse_file, "GCSE: Replacing the source in insn %d with reg %d %s insn %d\n",
3119                        INSN_UID (insn), REGNO (to),
3120                        use_src ? "from" : "set in",
3121                        INSN_UID (insn_computes_expr));
3122             }
3123
3124         }
3125     }
3126   /* The register that the expr is computed into is set more than once.  */
3127   else if (1 /*expensive_op(this_pattrn->op) && do_expensive_gcse)*/)
3128     {
3129       /* Insert an insn after insnx that copies the reg set in insnx
3130          into a new pseudo register call this new register REGN.
3131          From insnb until end of basic block or until REGB is set
3132          replace all uses of REGB with REGN.  */
3133       rtx new_insn;
3134
3135       to = gen_reg_rtx (GET_MODE (SET_DEST (PATTERN (insn_computes_expr))));
3136
3137       /* Generate the new insn.  */
3138       /* ??? If the change fails, we return 0, even though we created
3139          an insn.  I think this is ok.  */
3140       new_insn = emit_insn_after (gen_rtx (SET, VOIDmode, to,
3141                                            SET_DEST (PATTERN (insn_computes_expr))),
3142                                   insn_computes_expr);
3143       /* Keep block number table up to date.  */
3144       set_block_num (new_insn, BLOCK_NUM (insn_computes_expr));
3145       /* Keep register set table up to date.  */
3146       record_one_set (REGNO (to), new_insn);
3147
3148       gcse_create_count++;
3149       if (gcse_file != NULL)
3150         {
3151           fprintf (gcse_file, "GCSE: Creating insn %d to copy value of reg %d, computed in insn %d,\n",
3152                    INSN_UID (NEXT_INSN (insn_computes_expr)),
3153                    REGNO (SET_SRC (PATTERN (NEXT_INSN (insn_computes_expr)))),
3154                    INSN_UID (insn_computes_expr));
3155           fprintf (gcse_file, "      into newly allocated reg %d\n", REGNO (to));
3156         }
3157
3158       pat = PATTERN (insn);
3159
3160       /* Do register replacement for INSN.  */
3161       changed = validate_change (insn, &SET_SRC (pat),
3162                                  SET_DEST (PATTERN (NEXT_INSN (insn_computes_expr))),
3163                                  0);
3164
3165       /* We should be able to ignore the return code from validate_change but
3166          to play it safe we check.  */
3167       if (changed)
3168         {
3169           gcse_subst_count++;
3170           if (gcse_file != NULL)
3171             {
3172               fprintf (gcse_file, "GCSE: Replacing the source in insn %d with reg %d set in insn %d\n",
3173                        INSN_UID (insn),
3174                        REGNO (SET_DEST (PATTERN (NEXT_INSN (insn_computes_expr)))),
3175                        INSN_UID (insn_computes_expr)); 
3176             }
3177
3178         }
3179     }
3180
3181   return changed;
3182 }
3183
3184 /* Perform classic GCSE.
3185    This is called by one_classic_gcse_pass after all the dataflow analysis
3186    has been done.
3187
3188    The result is non-zero if a change was made.  */
3189
3190 static int
3191 classic_gcse ()
3192 {
3193   int bb, changed;
3194   rtx insn;
3195
3196   /* Note we start at block 1.  */
3197
3198   changed = 0;
3199   for (bb = 1; bb < n_basic_blocks; bb++)
3200     {
3201       /* Reset tables used to keep track of what's still valid [since the
3202          start of the block].  */
3203       reset_opr_set_tables ();
3204
3205       for (insn = basic_block_head[bb];
3206            insn != NULL && insn != NEXT_INSN (basic_block_end[bb]);
3207            insn = NEXT_INSN (insn))
3208         {
3209           /* Is insn of form (set (pseudo-reg) ...)?  */
3210
3211           if (GET_CODE (insn) == INSN
3212               && GET_CODE (PATTERN (insn)) == SET
3213               && GET_CODE (SET_DEST (PATTERN (insn))) == REG
3214               && REGNO (SET_DEST (PATTERN (insn))) >= FIRST_PSEUDO_REGISTER)
3215             {
3216               rtx pat = PATTERN (insn);
3217               rtx src = SET_SRC (pat);
3218               struct expr *expr;
3219
3220               if (want_to_gcse_p (src)
3221                   /* Is the expression recorded?  */
3222                   && ((expr = lookup_expr (src)) != NULL)
3223                   /* Is the expression available [at the start of the
3224                      block]?  */
3225                   && TEST_BIT (ae_in[bb], expr->bitmap_index)
3226                   /* Are the operands unchanged since the start of the
3227                      block?  */
3228                   && oprs_not_set_p (src, insn))
3229                 changed |= handle_avail_expr (insn, expr);
3230             }
3231
3232           /* Keep track of everything modified by this insn.  */
3233           /* ??? Need to be careful w.r.t. mods done to INSN.  */
3234           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3235             mark_oprs_set (insn);
3236         }
3237     }
3238
3239   return changed;
3240 }
3241
3242 /* Top level routine to perform one classic GCSE pass.
3243
3244    Return non-zero if a change was made.  */
3245
3246 static int
3247 one_classic_gcse_pass (f, pass)
3248      rtx f;
3249      int pass;
3250 {
3251   int changed = 0;
3252
3253   gcse_subst_count = 0;
3254   gcse_create_count = 0;
3255
3256   alloc_expr_hash_table (max_cuid);
3257   alloc_rd_mem (n_basic_blocks, max_cuid);
3258   compute_expr_hash_table (f);
3259   if (gcse_file)
3260     dump_hash_table (gcse_file, "Expression", expr_hash_table,
3261                      expr_hash_table_size, n_exprs);
3262   if (n_exprs > 0)
3263     {
3264       compute_kill_rd ();
3265       compute_rd ();
3266       alloc_avail_expr_mem (n_basic_blocks, n_exprs);
3267       compute_ae_gen ();
3268       compute_ae_kill ();
3269       compute_available ();
3270       changed = classic_gcse ();
3271       free_avail_expr_mem ();
3272     }
3273   free_rd_mem ();
3274   free_expr_hash_table ();
3275
3276   if (gcse_file)
3277     {
3278       fprintf (gcse_file, "\n");
3279       fprintf (gcse_file, "GCSE of %s, pass %d: %d bytes needed, %d substs, %d insns created\n",
3280                current_function_name, pass,
3281                bytes_used, gcse_subst_count, gcse_create_count);
3282     }
3283
3284   return changed;
3285 }
3286 \f
3287 /* Compute copy/constant propagation working variables.  */
3288
3289 /* Local properties of assignments.  */
3290
3291 static sbitmap *cprop_pavloc;
3292 static sbitmap *cprop_absaltered;
3293
3294 /* Global properties of assignments (computed from the local properties).  */
3295
3296 static sbitmap *cprop_avin;
3297 static sbitmap *cprop_avout;
3298
3299 /* Allocate vars used for copy/const propagation.
3300    N_BLOCKS is the number of basic blocks.
3301    N_SETS is the number of sets.  */
3302
3303 static void
3304 alloc_cprop_mem (n_blocks, n_sets)
3305      int n_blocks, n_sets;
3306 {
3307   cprop_pavloc = sbitmap_vector_alloc (n_blocks, n_sets);
3308   cprop_absaltered = sbitmap_vector_alloc (n_blocks, n_sets);
3309
3310   cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
3311   cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
3312 }
3313
3314 /* Free vars used by copy/const propagation.  */
3315
3316 static void
3317 free_cprop_mem ()
3318 {
3319   free (cprop_pavloc);
3320   free (cprop_absaltered);
3321   free (cprop_avin);
3322   free (cprop_avout);
3323 }
3324
3325 /* Dump copy/const propagation data.  */
3326
3327 void
3328 dump_cprop_data (file)
3329      FILE *file;
3330 {
3331   dump_sbitmap_vector (file, "CPROP partially locally available sets", "BB",
3332                        cprop_pavloc, n_basic_blocks);
3333   dump_sbitmap_vector (file, "CPROP absolutely altered sets", "BB",
3334                        cprop_absaltered, n_basic_blocks);
3335
3336   dump_sbitmap_vector (file, "CPROP available incoming sets", "BB",
3337                        cprop_avin, n_basic_blocks);
3338   dump_sbitmap_vector (file, "CPROP available outgoing sets", "BB",
3339                        cprop_avout, n_basic_blocks);
3340 }
3341
3342 /* For each block, compute whether X is transparent.
3343    X is either an expression or an assignment [though we don't care which,
3344    for this context an assignment is treated as an expression].
3345    For each block where an element of X is modified, set (SET_P == 1) or reset
3346    (SET_P == 0) the INDX bit in BMAP.  */
3347
3348 static void
3349 compute_transp (x, indx, bmap, set_p)
3350      rtx x;
3351      int indx;
3352      sbitmap *bmap;
3353      int set_p;
3354 {
3355   int bb,i;
3356   enum rtx_code code;
3357   char *fmt;
3358
3359   /* repeat is used to turn tail-recursion into iteration.  */
3360  repeat:
3361
3362   if (x == 0)
3363     return;
3364
3365   code = GET_CODE (x);
3366   switch (code)
3367     {
3368     case REG:
3369       {
3370         reg_set *r;
3371         int regno = REGNO (x);
3372
3373         if (set_p)
3374           {
3375             if (regno < FIRST_PSEUDO_REGISTER)
3376               {
3377                 for (bb = 0; bb < n_basic_blocks; bb++)
3378                   if (TEST_BIT (reg_set_in_block[bb], regno))
3379                     SET_BIT (bmap[bb], indx);
3380               }
3381             else
3382               {
3383                 for (r = reg_set_table[regno]; r != NULL; r = r->next)
3384                   {
3385                     bb = BLOCK_NUM (r->insn);
3386                     SET_BIT (bmap[bb], indx);
3387                   }
3388               }
3389           }
3390         else
3391           {
3392             if (regno < FIRST_PSEUDO_REGISTER)
3393               {
3394                 for (bb = 0; bb < n_basic_blocks; bb++)
3395                   if (TEST_BIT (reg_set_in_block[bb], regno))
3396                     RESET_BIT (bmap[bb], indx);
3397               }
3398             else
3399               {
3400                 for (r = reg_set_table[regno]; r != NULL; r = r->next)
3401                   {
3402                     bb = BLOCK_NUM (r->insn);
3403                     RESET_BIT (bmap[bb], indx);
3404                   }
3405               }
3406           }
3407         return;
3408       }
3409
3410     case MEM:
3411       if (set_p)
3412         {
3413           for (bb = 0; bb < n_basic_blocks; bb++)
3414             if (mem_set_in_block[bb])
3415               SET_BIT (bmap[bb], indx);
3416         }
3417       else
3418         {
3419           for (bb = 0; bb < n_basic_blocks; bb++)
3420             if (mem_set_in_block[bb])
3421               RESET_BIT (bmap[bb], indx);
3422         }
3423       x = XEXP (x, 0);
3424       goto repeat;
3425
3426     case PC:
3427     case CC0: /*FIXME*/
3428     case CONST:
3429     case CONST_INT:
3430     case CONST_DOUBLE:
3431     case SYMBOL_REF:
3432     case LABEL_REF:
3433     case ADDR_VEC:
3434     case ADDR_DIFF_VEC:
3435       return;
3436
3437     default:
3438       break;
3439     }
3440
3441   i = GET_RTX_LENGTH (code) - 1;
3442   fmt = GET_RTX_FORMAT (code);
3443   for (; i >= 0; i--)
3444     {
3445       if (fmt[i] == 'e')
3446         {
3447           rtx tem = XEXP (x, i);
3448
3449           /* If we are about to do the last recursive call
3450              needed at this level, change it into iteration.
3451              This function is called enough to be worth it.  */
3452           if (i == 0)
3453             {
3454               x = tem;
3455               goto repeat;
3456             }
3457           compute_transp (tem, indx, bmap, set_p);
3458         }
3459       else if (fmt[i] == 'E')
3460         {
3461           int j;
3462           for (j = 0; j < XVECLEN (x, i); j++)
3463             compute_transp (XVECEXP (x, i, j), indx, bmap, set_p);
3464         }
3465     }
3466 }
3467
3468 static void
3469 compute_cprop_local_properties ()
3470 {
3471   int i;
3472
3473   sbitmap_vector_zero (cprop_absaltered, n_basic_blocks);
3474   sbitmap_vector_zero (cprop_pavloc, n_basic_blocks);
3475
3476   for (i = 0; i < set_hash_table_size; i++)
3477     {
3478       struct expr *expr;
3479
3480       for (expr = set_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
3481         {
3482           struct occr *occr;
3483           int indx = expr->bitmap_index;
3484
3485           /* The assignment is absolutely altered if any operand is modified
3486              by this block [excluding the assignment itself].
3487              We start by assuming all are transparent [none are killed], and
3488              then setting the bits for those that are.  */
3489
3490           compute_transp (expr->expr, indx, cprop_absaltered, 1);
3491
3492           /* The occurrences recorded in avail_occr are exactly those that
3493              we want to set to non-zero in PAVLOC.  */
3494
3495           for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
3496             {
3497               int bb = BLOCK_NUM (occr->insn);
3498               SET_BIT (cprop_pavloc[bb], indx);
3499             }
3500         }
3501     }
3502 }
3503
3504 static void
3505 compute_cprop_avinout ()
3506 {
3507   int bb, changed, passes;
3508
3509   sbitmap_zero (cprop_avin[0]);
3510   sbitmap_vector_ones (cprop_avout, n_basic_blocks);
3511
3512   passes = 0;
3513   changed = 1;
3514   while (changed)
3515     {
3516       changed = 0;
3517       for (bb = 0; bb < n_basic_blocks; bb++)
3518         {
3519           if (bb != 0)
3520             sbitmap_intersect_of_predecessors (cprop_avin[bb], cprop_avout,
3521                                                bb, s_preds);
3522           changed |= sbitmap_union_of_diff (cprop_avout[bb],
3523                                             cprop_pavloc[bb],
3524                                             cprop_avin[bb],
3525                                             cprop_absaltered[bb]);
3526         }
3527       passes++;
3528     }
3529
3530   if (gcse_file)
3531     fprintf (gcse_file, "cprop avail expr computation: %d passes\n", passes);
3532 }
3533
3534 /* Top level routine to do the dataflow analysis needed by copy/const
3535    propagation.  */
3536
3537 static void
3538 compute_cprop_data ()
3539 {
3540   compute_cprop_local_properties ();
3541   compute_cprop_avinout ();
3542 }
3543 \f
3544 /* Copy/constant propagation.  */
3545
3546 struct reg_use {
3547   rtx reg_rtx;
3548 };
3549
3550 /* Maximum number of register uses in an insn that we handle.  */
3551 #define MAX_USES 8
3552
3553 /* Table of uses found in an insn.
3554    Allocated statically to avoid alloc/free complexity and overhead.  */
3555 static struct reg_use reg_use_table[MAX_USES];
3556
3557 /* Index into `reg_use_table' while building it.  */
3558 static int reg_use_count;
3559
3560 /* Set up a list of register numbers used in INSN.
3561    The found uses are stored in `reg_use_table'.
3562    `reg_use_count' is initialized to zero before entry, and
3563    contains the number of uses in the table upon exit.
3564
3565    ??? If a register appears multiple times we will record it multiple
3566    times.  This doesn't hurt anything but it will slow things down.  */
3567
3568 static void
3569 find_used_regs (x)
3570      rtx x;
3571 {
3572   int i;
3573   enum rtx_code code;
3574   char *fmt;
3575
3576   /* repeat is used to turn tail-recursion into iteration.  */
3577  repeat:
3578
3579   if (x == 0)
3580     return;
3581
3582   code = GET_CODE (x);
3583   switch (code)
3584     {
3585     case REG:
3586       if (reg_use_count == MAX_USES)
3587         return;
3588       reg_use_table[reg_use_count].reg_rtx = x;
3589       reg_use_count++;
3590       return;
3591
3592     case MEM:
3593       x = XEXP (x, 0);
3594       goto repeat;
3595
3596     case PC:
3597     case CC0:
3598     case CONST:
3599     case CONST_INT:
3600     case CONST_DOUBLE:
3601     case SYMBOL_REF:
3602     case LABEL_REF:
3603     case CLOBBER:
3604     case ADDR_VEC:
3605     case ADDR_DIFF_VEC:
3606     case ASM_INPUT: /*FIXME*/
3607       return;
3608
3609     case SET:
3610       if (GET_CODE (SET_DEST (x)) == MEM)
3611         find_used_regs (SET_DEST (x));
3612       x = SET_SRC (x);
3613       goto repeat;
3614
3615     default:
3616       break;
3617     }
3618
3619   /* Recursively scan the operands of this expression.  */
3620
3621   fmt = GET_RTX_FORMAT (code);
3622   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3623     {
3624       if (fmt[i] == 'e')
3625         {
3626           /* If we are about to do the last recursive call
3627              needed at this level, change it into iteration.
3628              This function is called enough to be worth it.  */
3629           if (i == 0)
3630             {
3631               x = XEXP (x, 0);
3632               goto repeat;
3633             }
3634           find_used_regs (XEXP (x, i));
3635         }
3636       else if (fmt[i] == 'E')
3637         {
3638           int j;
3639           for (j = 0; j < XVECLEN (x, i); j++)
3640             find_used_regs (XVECEXP (x, i, j));
3641         }
3642     }
3643 }
3644
3645 /* Try to replace all non-SET_DEST occurrences of FROM in INSN with TO.
3646    Returns non-zero is successful.  */
3647
3648 static int
3649 try_replace_reg (from, to, insn)
3650      rtx from, to, insn;
3651 {
3652   return validate_replace_src (from, to, insn);
3653 }
3654
3655 /* Find a set of REGNO that is available on entry to INSN's block.
3656    Returns NULL if not found.  */
3657
3658 static struct expr *
3659 find_avail_set (regno, insn)
3660      int regno;
3661      rtx insn;
3662 {
3663   struct expr *set = lookup_set (regno, NULL_RTX);
3664
3665   while (set)
3666     {
3667       if (TEST_BIT (cprop_avin[BLOCK_NUM (insn)], set->bitmap_index))
3668         break;
3669       set = next_set (regno, set);
3670     }
3671
3672   return set;
3673 }
3674
3675 /* Perform constant and copy propagation on INSN.
3676    The result is non-zero if a change was made.  */
3677
3678 static int
3679 cprop_insn (insn)
3680      rtx insn;
3681 {
3682   struct reg_use *reg_used;
3683   int changed = 0;
3684
3685   /* ??? For now only propagate into SETs.  */
3686   if (GET_CODE (insn) != INSN
3687       || GET_CODE (PATTERN (insn)) != SET)
3688     return 0;
3689
3690   reg_use_count = 0;
3691   find_used_regs (PATTERN (insn));
3692
3693   reg_used = &reg_use_table[0];
3694   for ( ; reg_use_count > 0; reg_used++, reg_use_count--)
3695     {
3696       rtx pat, src;
3697       struct expr *set;
3698       int regno = REGNO (reg_used->reg_rtx);
3699
3700       /* Ignore registers created by GCSE.
3701          We do this because ... */
3702       if (regno >= max_gcse_regno)
3703         continue;
3704
3705       /* If the register has already been set in this block, there's
3706          nothing we can do.  */
3707       if (! oprs_not_set_p (reg_used->reg_rtx, insn))
3708         continue;
3709
3710       /* Find an assignment that sets reg_used and is available
3711          at the start of the block.  */
3712       set = find_avail_set (regno, insn);
3713       if (! set)
3714         continue;
3715   
3716       pat = set->expr;
3717       /* ??? We might be able to handle PARALLELs.  Later.  */
3718       if (GET_CODE (pat) != SET)
3719         abort ();
3720       src = SET_SRC (pat);
3721
3722       if (GET_CODE (src) == CONST_INT)
3723         {
3724           if (try_replace_reg (reg_used->reg_rtx, src, insn))
3725             {
3726               changed = 1;
3727               const_prop_count++;
3728               if (gcse_file != NULL)
3729                 {
3730                   fprintf (gcse_file, "CONST-PROP: Replacing reg %d in insn %d with constant ",
3731                            regno, INSN_UID (insn));
3732                   fprintf (gcse_file, HOST_WIDE_INT_PRINT_DEC, INTVAL (src));
3733                   fprintf (gcse_file, "\n");
3734                 }
3735
3736               /* The original insn setting reg_used may or may not now be
3737                  deletable.  We leave the deletion to flow.  */
3738             }
3739         }
3740       else if (GET_CODE (src) == REG
3741                && REGNO (src) >= FIRST_PSEUDO_REGISTER
3742                && REGNO (src) != regno)
3743         {
3744           /* We know the set is available.
3745              Now check that SET_SRC is ANTLOC (i.e. none of the source operands
3746              have changed since the start of the block).  */
3747           if (oprs_not_set_p (src, insn))
3748             {
3749               if (try_replace_reg (reg_used->reg_rtx, src, insn))
3750                 {
3751                   changed = 1;
3752                   copy_prop_count++;
3753                   if (gcse_file != NULL)
3754                     {
3755                       fprintf (gcse_file, "COPY-PROP: Replacing reg %d in insn %d with reg %d\n",
3756                                regno, INSN_UID (insn), REGNO (src));
3757                     }
3758
3759                   /* The original insn setting reg_used may or may not now be
3760                      deletable.  We leave the deletion to flow.  */
3761                   /* FIXME: If it turns out that the insn isn't deletable,
3762                      then we may have unnecessarily extended register lifetimes
3763                      and made things worse.  */
3764                 }
3765             }
3766         }
3767     }
3768
3769   return changed;
3770 }
3771
3772 /* Forward propagate copies.
3773    This includes copies and constants.
3774    Return non-zero if a change was made.  */
3775
3776 static int
3777 cprop ()
3778 {
3779   int bb, changed;
3780   rtx insn;
3781
3782   /* Note we start at block 1.  */
3783
3784   changed = 0;
3785   for (bb = 1; bb < n_basic_blocks; bb++)
3786     {
3787       /* Reset tables used to keep track of what's still valid [since the
3788          start of the block].  */
3789       reset_opr_set_tables ();
3790
3791       for (insn = basic_block_head[bb];
3792            insn != NULL && insn != NEXT_INSN (basic_block_end[bb]);
3793            insn = NEXT_INSN (insn))
3794         {
3795           if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3796             {
3797               changed |= cprop_insn (insn);
3798
3799               /* Keep track of everything modified by this insn.  */
3800               /* ??? Need to be careful w.r.t. mods done to INSN.  */
3801               mark_oprs_set (insn);
3802             }
3803         }
3804     }
3805
3806   if (gcse_file != NULL)
3807     fprintf (gcse_file, "\n");
3808
3809   return changed;
3810 }
3811
3812 /* Perform one copy/constant propagation pass.
3813    F is the first insn in the function.
3814    PASS is the pass count.  */
3815
3816 static int
3817 one_cprop_pass (f, pass)
3818      rtx f;
3819      int pass;
3820 {
3821   int changed = 0;
3822
3823   const_prop_count = 0;
3824   copy_prop_count = 0;
3825
3826   alloc_set_hash_table (max_cuid);
3827   compute_set_hash_table (f);
3828   if (gcse_file)
3829     dump_hash_table (gcse_file, "SET", set_hash_table, set_hash_table_size,
3830                      n_sets);
3831   if (n_sets > 0)
3832     {
3833       alloc_cprop_mem (n_basic_blocks, n_sets);
3834       compute_cprop_data ();
3835       changed = cprop ();
3836       free_cprop_mem ();
3837     }
3838   free_set_hash_table ();
3839
3840   if (gcse_file)
3841     {
3842       fprintf (gcse_file, "CPROP of %s, pass %d: %d bytes needed, %d const props, %d copy props\n",
3843                current_function_name, pass,
3844                bytes_used, const_prop_count, copy_prop_count);
3845       fprintf (gcse_file, "\n");
3846     }
3847
3848   return changed;
3849 }
3850 \f
3851 /* Compute PRE working variables.  */
3852
3853 /* Local properties of expressions.  */
3854 /* Nonzero for expressions that are transparent in the block.  */
3855 static sbitmap *pre_transp;
3856 /* Nonzero for expressions that are computed (available) in the block.  */
3857 static sbitmap *pre_comp;
3858 /* Nonzero for expressions that are locally anticipatable in the block.  */
3859 static sbitmap *pre_antloc;
3860
3861 /* Global properties (computed from the expression local properties).  */
3862 /* Nonzero for expressions that are available on block entry/exit.  */
3863 static sbitmap *pre_avin;
3864 static sbitmap *pre_avout;
3865 /* Nonzero for expressions that are anticipatable on block entry/exit.  */
3866 static sbitmap *pre_antin;
3867 static sbitmap *pre_antout;
3868 /* Nonzero for expressions that are partially available on block entry/exit.  */
3869 static sbitmap *pre_pavin;
3870 static sbitmap *pre_pavout;
3871 /* Nonzero for expressions that are "placement possible" on block entry/exit.  */
3872 static sbitmap *pre_ppin;
3873 static sbitmap *pre_ppout;
3874
3875 /* Used while performing PRE to denote which insns are redundant.  */
3876 static sbitmap pre_redundant;
3877
3878 /* Allocate vars used for PRE analysis.  */
3879
3880 static void
3881 alloc_pre_mem (n_blocks, n_exprs)
3882      int n_blocks, n_exprs;
3883 {
3884   pre_transp = sbitmap_vector_alloc (n_blocks, n_exprs);
3885   pre_comp = sbitmap_vector_alloc (n_blocks, n_exprs);
3886   pre_antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
3887
3888   pre_avin = sbitmap_vector_alloc (n_blocks, n_exprs);
3889   pre_avout = sbitmap_vector_alloc (n_blocks, n_exprs);
3890   pre_antin = sbitmap_vector_alloc (n_blocks, n_exprs);
3891   pre_antout = sbitmap_vector_alloc (n_blocks, n_exprs);
3892
3893   pre_pavin = sbitmap_vector_alloc (n_blocks, n_exprs);
3894   pre_pavout = sbitmap_vector_alloc (n_blocks, n_exprs);
3895   pre_ppin = sbitmap_vector_alloc (n_blocks, n_exprs);
3896   pre_ppout = sbitmap_vector_alloc (n_blocks, n_exprs);
3897 }
3898
3899 /* Free vars used for PRE analysis.  */
3900
3901 static void
3902 free_pre_mem ()
3903 {
3904   free (pre_transp);
3905   free (pre_comp);
3906   free (pre_antloc);
3907
3908   free (pre_avin);
3909   free (pre_avout);
3910   free (pre_antin);
3911   free (pre_antout);
3912
3913   free (pre_pavin);
3914   free (pre_pavout);
3915   free (pre_ppin);
3916   free (pre_ppout);
3917 }
3918
3919 /* Dump PRE data.  */
3920
3921 void
3922 dump_pre_data (file)
3923      FILE *file;
3924 {
3925   dump_sbitmap_vector (file, "PRE locally transparent expressions", "BB",
3926                        pre_transp, n_basic_blocks);
3927   dump_sbitmap_vector (file, "PRE locally available expressions", "BB",
3928                        pre_comp, n_basic_blocks);
3929   dump_sbitmap_vector (file, "PRE locally anticipatable expressions", "BB",
3930                        pre_antloc, n_basic_blocks);
3931
3932   dump_sbitmap_vector (file, "PRE available incoming expressions", "BB",
3933                        pre_avin, n_basic_blocks);
3934   dump_sbitmap_vector (file, "PRE available outgoing expressions", "BB",
3935                        pre_avout, n_basic_blocks);
3936   dump_sbitmap_vector (file, "PRE anticipatable incoming expressions", "BB",
3937                        pre_antin, n_basic_blocks);
3938   dump_sbitmap_vector (file, "PRE anticipatable outgoing expressions", "BB",
3939                        pre_antout, n_basic_blocks);
3940
3941   dump_sbitmap_vector (file, "PRE partially available incoming expressions", "BB",
3942                        pre_pavin, n_basic_blocks);
3943   dump_sbitmap_vector (file, "PRE partially available outgoing expressions", "BB",
3944                        pre_pavout, n_basic_blocks);
3945   dump_sbitmap_vector (file, "PRE placement possible on incoming", "BB",
3946                        pre_ppin, n_basic_blocks);
3947   dump_sbitmap_vector (file, "PRE placement possible on outgoing", "BB",
3948                        pre_ppout, n_basic_blocks);
3949 }
3950
3951 /* Compute the local properties of each recorded expression.
3952    Local properties are those that are defined by the block, irrespective
3953    of other blocks.
3954
3955    An expression is transparent in a block if its operands are not modified
3956    in the block.
3957
3958    An expression is computed (locally available) in a block if it is computed
3959    at least once and expression would contain the same value if the
3960    computation was moved to the end of the block.
3961
3962    An expression is locally anticipatable in a block if it is computed at
3963    least once and expression would contain the same value if the computation
3964    was moved to the beginning of the block.  */
3965
3966 static void
3967 compute_pre_local_properties ()
3968 {
3969   int i;
3970
3971   sbitmap_vector_ones (pre_transp, n_basic_blocks);
3972   sbitmap_vector_zero (pre_comp, n_basic_blocks);
3973   sbitmap_vector_zero (pre_antloc, n_basic_blocks);
3974
3975   for (i = 0; i < expr_hash_table_size; i++)
3976     {
3977       struct expr *expr;
3978
3979       for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
3980         {
3981           struct occr *occr;
3982           int indx = expr->bitmap_index;
3983
3984           /* The expression is transparent in this block if it is not killed.
3985              We start by assuming all are transparent [none are killed], and then
3986              reset the bits for those that are.  */
3987
3988           compute_transp (expr->expr, indx, pre_transp, 0);
3989
3990           /* The occurrences recorded in antic_occr are exactly those that
3991              we want to set to non-zero in ANTLOC.  */
3992
3993           for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
3994             {
3995               int bb = BLOCK_NUM (occr->insn);
3996               SET_BIT (pre_antloc[bb], indx);
3997
3998               /* While we're scanning the table, this is a good place to
3999                  initialize this.  */
4000               occr->deleted_p = 0;
4001             }
4002
4003           /* The occurrences recorded in avail_occr are exactly those that
4004              we want to set to non-zero in COMP.  */
4005
4006           for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
4007             {
4008               int bb = BLOCK_NUM (occr->insn);
4009               SET_BIT (pre_comp[bb], indx);
4010
4011               /* While we're scanning the table, this is a good place to
4012                  initialize this.  */
4013               occr->copied_p = 0;
4014             }
4015
4016           /* While we're scanning the table, this is a good place to
4017              initialize this.  */
4018           expr->reaching_reg = 0;
4019         }
4020     }
4021 }
4022
4023 /* Compute expression availability at entrance and exit of each block.  */
4024
4025 static void
4026 compute_pre_avinout ()
4027 {
4028   int bb, changed, passes;
4029
4030   sbitmap_zero (pre_avin[0]);
4031   sbitmap_vector_ones (pre_avout, n_basic_blocks);
4032
4033   passes = 0;
4034   changed = 1;
4035   while (changed)
4036     {
4037       changed = 0;
4038       for (bb = 0; bb < n_basic_blocks; bb++)
4039         {
4040           if (bb != 0)
4041             sbitmap_intersect_of_predecessors (pre_avin[bb], pre_avout,
4042                                                bb, s_preds);
4043           changed |= sbitmap_a_or_b_and_c (pre_avout[bb], pre_comp[bb],
4044                                            pre_transp[bb], pre_avin[bb]);
4045         }
4046       passes++;
4047     }
4048
4049   if (gcse_file)
4050     fprintf (gcse_file, "avail expr computation: %d passes\n", passes);
4051 }
4052
4053 /* Compute expression anticipatability at entrance and exit of each block.  */
4054
4055 static void
4056 compute_pre_antinout ()
4057 {
4058   int bb, changed, passes;
4059
4060   sbitmap_zero (pre_antout[n_basic_blocks - 1]);
4061   sbitmap_vector_ones (pre_antin, n_basic_blocks);
4062
4063   passes = 0;
4064   changed = 1;
4065   while (changed)
4066     {
4067       changed = 0;
4068       /* We scan the blocks in the reverse order to speed up
4069          the convergence.  */
4070       for (bb = n_basic_blocks - 1; bb >= 0; bb--)
4071         {
4072           if (bb != n_basic_blocks - 1)
4073             sbitmap_intersect_of_successors (pre_antout[bb], pre_antin,
4074                                              bb, s_succs);
4075           changed |= sbitmap_a_or_b_and_c (pre_antin[bb], pre_antloc[bb],
4076                                            pre_transp[bb], pre_antout[bb]);
4077         }
4078       passes++;
4079     }
4080
4081   if (gcse_file)
4082     fprintf (gcse_file, "antic expr computation: %d passes\n", passes);
4083 }
4084
4085 /* Compute expression partial availability at entrance and exit of
4086    each block.  */
4087
4088 static void
4089 compute_pre_pavinout ()
4090 {
4091   int bb, changed, passes;
4092
4093   sbitmap_zero (pre_pavin[0]);
4094   sbitmap_vector_zero (pre_pavout, n_basic_blocks);
4095
4096   passes = 0;
4097   changed = 1;
4098   while (changed)
4099     {
4100       changed = 0;
4101       for (bb = 0; bb < n_basic_blocks; bb++)
4102         {
4103           if (bb != 0)
4104             sbitmap_union_of_predecessors (pre_pavin[bb], pre_pavout,
4105                                            bb, s_preds);
4106           changed |= sbitmap_a_or_b_and_c (pre_pavout[bb], pre_comp[bb],
4107                                            pre_transp[bb], pre_pavin[bb]);
4108         }
4109       passes++;
4110     }
4111
4112   if (gcse_file)
4113     fprintf (gcse_file, "partially avail expr computation: %d passes\n", passes);
4114 }
4115
4116 /* Compute "placement possible" information on entrance and exit of
4117    each block.
4118
4119    From Fred Chow's Thesis:
4120    A computation `e' is PP at a point `p' if it is anticipated at `p' and
4121    all the anticipated e's can be rendered redundant by zero or more
4122    insertions at that point and some other points in the procedure, and
4123    these insertions satisfy the conditions that the insertions are always
4124    at points that `e' is anticipated and the first anticipated e's after the
4125    insertions are rendered redundant.  */
4126
4127 static void
4128 compute_pre_ppinout ()
4129 {
4130   int bb, i, changed, size, passes;
4131
4132   sbitmap_vector_ones (pre_ppin, n_basic_blocks);
4133   /* ??? Inefficient as we set pre_ppin[0] twice, but simple.  */
4134   sbitmap_zero (pre_ppin[0]);
4135
4136   sbitmap_vector_ones (pre_ppout, n_basic_blocks);
4137   /* ??? Inefficient as we set pre_ppout[n_basic_blocks-1] twice, but simple.  */
4138   sbitmap_zero (pre_ppout[n_basic_blocks - 1]);
4139
4140   size = pre_ppin[0]->size;
4141   passes = 0;
4142   changed = 1;
4143   while (changed)
4144     {
4145       changed = 0;
4146       for (bb = 1; bb < n_basic_blocks; bb++)
4147         {
4148           sbitmap_ptr antin = pre_antin[bb]->elms;
4149           sbitmap_ptr pavin = pre_pavin[bb]->elms;
4150           sbitmap_ptr antloc = pre_antloc[bb]->elms;
4151           sbitmap_ptr transp = pre_transp[bb]->elms;
4152           sbitmap_ptr ppout = pre_ppout[bb]->elms;
4153           sbitmap_ptr ppin = pre_ppin[bb]->elms;
4154
4155           for (i = 0; i < size; i++)
4156             {
4157               int_list_ptr pred;
4158               SBITMAP_ELT_TYPE tmp = *antin & *pavin & (*antloc | (*transp & *ppout));
4159               SBITMAP_ELT_TYPE pred_val = -1L;
4160
4161               for (pred = s_preds[bb]; pred != NULL; pred = pred->next)
4162                 {
4163                   int pred_bb = INT_LIST_VAL (pred);
4164                   sbitmap_ptr ppout_j,avout_j;
4165
4166                   if (pred_bb == ENTRY_BLOCK)
4167                     continue;
4168
4169                   /* If this is a back edge, propagate info along the back
4170                      edge to allow for loop invariant code motion.
4171
4172                      See FOLLOW_BACK_EDGES at the top of this file for a longer
4173                      discussion about loop invariant code motion in pre.  */
4174                   if (! FOLLOW_BACK_EDGES
4175                       && (INSN_CUID (BLOCK_HEAD (bb))
4176                           < INSN_CUID (BLOCK_END (pred_bb))))
4177                     {
4178                       pred_val = 0;
4179                     }
4180                   else
4181                     {
4182                       ppout_j = pre_ppout[pred_bb]->elms + i;
4183                       avout_j = pre_avout[pred_bb]->elms + i;
4184                       pred_val &= *ppout_j | *avout_j;
4185                     }
4186                 }
4187               tmp &= pred_val;
4188               *ppin = tmp;
4189               antin++; pavin++; antloc++; transp++; ppout++; ppin++;
4190             }
4191         }
4192
4193       for (bb = 0; bb < n_basic_blocks - 1; bb++)
4194         {
4195           sbitmap_ptr ppout = pre_ppout[bb]->elms;
4196
4197           for (i = 0; i < size; i++)
4198             {
4199               int_list_ptr succ;
4200               SBITMAP_ELT_TYPE tmp = -1L;
4201
4202               for (succ = s_succs[bb]; succ != NULL; succ = succ->next)
4203                 {
4204                   int succ_bb = INT_LIST_VAL (succ);
4205                   sbitmap_ptr ppin;
4206
4207                   if (succ_bb == EXIT_BLOCK)
4208                     continue;
4209
4210                   ppin = pre_ppin[succ_bb]->elms + i;
4211                   tmp &= *ppin;
4212                 }
4213               if (*ppout != tmp)
4214                 {
4215                   changed = 1;
4216                   *ppout++ = tmp;
4217                 }
4218               else
4219                 ppout++;
4220             }
4221         }
4222
4223       passes++;
4224     }
4225
4226   if (gcse_file)
4227     fprintf (gcse_file, "placement possible computation: %d passes\n", passes);
4228 }
4229
4230 /* Top level routine to do the dataflow analysis needed by PRE.  */
4231
4232 static void
4233 compute_pre_data ()
4234 {
4235   compute_pre_local_properties ();
4236   compute_pre_avinout ();
4237   compute_pre_antinout ();
4238   compute_pre_pavinout ();
4239   compute_pre_ppinout ();
4240   if (gcse_file)
4241     fprintf (gcse_file, "\n");
4242 }
4243 \f
4244 /* PRE utilities */
4245
4246 /* Return non-zero if occurrence OCCR of expression EXPR reaches block BB.
4247
4248    VISITED is a pointer to a working buffer for tracking which BB's have
4249    been visited.  It is NULL for the top-level call.
4250
4251    We treat reaching expressions that go through blocks containing the same
4252    reaching expression as "not reaching".  E.g. if EXPR is generated in blocks
4253    2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
4254    2 as not reaching.  The intent is to improve the probability of finding
4255    only one reaching expression and to reduce register lifetimes by picking
4256    the closest such expression.  */
4257
4258 static int
4259 pre_expr_reaches_here_p (occr, expr, bb, visited)
4260      struct occr *occr;
4261      struct expr *expr;
4262      int bb;
4263      char *visited;
4264 {
4265   int_list_ptr pred;
4266
4267   if (visited == NULL)
4268     {
4269       visited = (char *) alloca (n_basic_blocks);
4270       bzero (visited, n_basic_blocks);
4271     }
4272
4273   for (pred = s_preds[bb]; pred != NULL; pred = pred->next)
4274     {
4275       int pred_bb = INT_LIST_VAL (pred);
4276
4277       if (pred_bb == ENTRY_BLOCK
4278           /* Has predecessor has already been visited?  */
4279           || visited[pred_bb])
4280         {
4281           /* Nothing to do.  */
4282         }
4283       /* Does this predecessor generate this expression?  */
4284       else if (TEST_BIT (pre_comp[pred_bb], expr->bitmap_index))
4285         {
4286           /* Is this the occurrence we're looking for?
4287              Note that there's only one generating occurrence per block
4288              so we just need to check the block number.  */
4289           if (BLOCK_NUM (occr->insn) == pred_bb)
4290             return 1;
4291           visited[pred_bb] = 1;
4292         }
4293       /* Ignore this predecessor if it kills the expression.  */
4294       else if (! TEST_BIT (pre_transp[pred_bb], expr->bitmap_index))
4295         visited[pred_bb] = 1;
4296       /* Neither gen nor kill.  */
4297       else
4298         {
4299           visited[pred_bb] = 1;
4300           if (pre_expr_reaches_here_p (occr, expr, pred_bb, visited))
4301             return 1;
4302         }
4303     }
4304
4305   /* All paths have been checked.  */
4306   return 0;
4307 }
4308 \f
4309 /* Add EXPR to the end of basic block BB.  */
4310
4311 static void
4312 pre_insert_insn (expr, bb)
4313      struct expr *expr;
4314      int bb;
4315 {
4316   rtx insn = BLOCK_END (bb);
4317   rtx new_insn;
4318   rtx reg = expr->reaching_reg;
4319   int regno = REGNO (reg);
4320   rtx pat;
4321
4322   pat = gen_rtx (SET, VOIDmode, reg, copy_rtx (expr->expr));
4323
4324   /* If the last insn is a jump, insert EXPR in front [taking care to
4325      handle cc0, etc. properly].  */
4326
4327   if (GET_CODE (insn) == JUMP_INSN)
4328     {
4329 #ifdef HAVE_cc0
4330       rtx note;
4331 #endif
4332
4333       /* If this is a jump table, then we can't insert stuff here.  Since
4334          we know the previous real insn must be the tablejump, we insert
4335          the new instruction just before the tablejump.  */
4336       if (GET_CODE (PATTERN (insn)) == ADDR_VEC
4337           || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
4338         insn = prev_real_insn (insn);
4339
4340 #ifdef HAVE_cc0
4341       /* FIXME: 'twould be nice to call prev_cc0_setter here but it aborts
4342          if cc0 isn't set.  */
4343       note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
4344       if (note)
4345         insn = XEXP (note, 0);
4346       else
4347         {
4348           rtx maybe_cc0_setter = prev_nonnote_insn (insn);
4349           if (maybe_cc0_setter
4350               && GET_RTX_CLASS (GET_CODE (maybe_cc0_setter)) == 'i'
4351               && sets_cc0_p (PATTERN (maybe_cc0_setter)))
4352             insn = maybe_cc0_setter;
4353         }
4354 #endif
4355       /* FIXME: What if something in cc0/jump uses value set in new insn?  */
4356       new_insn = emit_insn_before (pat, insn);
4357       if (BLOCK_HEAD (bb) == insn)
4358         BLOCK_HEAD (bb) = new_insn;
4359       /* Keep block number table up to date.  */
4360       set_block_num (new_insn, bb);
4361       /* Keep register set table up to date.  */
4362       record_one_set (regno, new_insn);
4363     }
4364   else
4365     {
4366       new_insn = emit_insn_after (pat, insn);
4367       BLOCK_END (bb) = new_insn;
4368       /* Keep block number table up to date.  */
4369       set_block_num (new_insn, bb);
4370       /* Keep register set table up to date.  */
4371       record_one_set (regno, new_insn);
4372     }
4373
4374   gcse_create_count++;
4375
4376   if (gcse_file)
4377     {
4378       fprintf (gcse_file, "PRE: end of bb %d, insn %d, copying expression %d to reg %d\n",
4379                bb, INSN_UID (new_insn), expr->bitmap_index, regno);
4380     }
4381 }
4382
4383 /* Insert partially redundant expressions at the ends of appropriate basic
4384    blocks making them now redundant.  */
4385
4386 static void
4387 pre_insert (index_map)
4388      struct expr **index_map;
4389 {
4390   int bb, i, size;
4391
4392   /* Compute INSERT = PPOUT & (~AVOUT) & (~PPIN | ~TRANSP) for each
4393      expression.  Where INSERT == TRUE, add the expression at the end of
4394      the basic block.  */
4395
4396   size = pre_ppout[0]->size;
4397   for (bb = 0; bb < n_basic_blocks; bb++)
4398     {
4399       int indx;
4400       sbitmap_ptr ppout = pre_ppout[bb]->elms;
4401       sbitmap_ptr avout = pre_avout[bb]->elms;
4402       sbitmap_ptr ppin = pre_ppin[bb]->elms;
4403       sbitmap_ptr transp = pre_transp[bb]->elms;
4404
4405       for (i = indx = 0;
4406            i < size;
4407            i++, indx += SBITMAP_ELT_BITS, ppout++, avout++, ppin++, transp++)
4408         {
4409           int j;
4410           SBITMAP_ELT_TYPE insert = *ppout & (~*avout) & (~*ppin | ~*transp);
4411
4412           for (j = indx; insert != 0 && j < n_exprs; j++, insert >>= 1)
4413             {
4414               if ((insert & 1) != 0
4415                   /* If the basic block isn't reachable, PPOUT will be TRUE.
4416                      However, we don't want to insert a copy here because the
4417                      expression may not really be redundant.  So only insert
4418                      an insn if the expression was deleted.  */
4419                   && index_map[j]->reaching_reg != NULL)
4420                 pre_insert_insn (index_map[j], bb);
4421             }
4422         }
4423     }
4424 }
4425
4426 /* Copy the result of INSN to REG.
4427    INDX is the expression number.  */
4428
4429 static void
4430 pre_insert_copy_insn (expr, insn)
4431      struct expr *expr;
4432      rtx insn;
4433 {
4434   rtx reg = expr->reaching_reg;
4435   int regno = REGNO (reg);
4436   int indx = expr->bitmap_index;
4437   rtx set = single_set (insn);
4438   rtx new_insn;
4439
4440   if (!set)
4441     abort ();
4442   new_insn = emit_insn_after (gen_rtx (SET, VOIDmode, reg, SET_DEST (set)),
4443                               insn);
4444   /* Keep block number table up to date.  */
4445   set_block_num (new_insn, BLOCK_NUM (insn));
4446   /* Keep register set table up to date.  */
4447   record_one_set (regno, new_insn);
4448
4449   gcse_create_count++;
4450
4451   if (gcse_file)
4452     {
4453       fprintf (gcse_file, "PRE: bb %d, insn %d, copying expression %d in insn %d to reg %d\n",
4454                BLOCK_NUM (insn), INSN_UID (new_insn), indx, INSN_UID (insn), regno);
4455     }
4456 }
4457
4458 /* Copy available expressions that reach the redundant expression
4459    to `reaching_reg'.  */
4460
4461 static void
4462 pre_insert_copies ()
4463 {
4464   int i;
4465
4466   /* For each available expression in the table, copy the result to
4467      `reaching_reg' if the expression reaches a deleted one.
4468
4469      ??? The current algorithm is rather brute force.
4470      Need to do some profiling.  */
4471
4472   for (i = 0; i < expr_hash_table_size; i++)
4473     {
4474       struct expr *expr;
4475
4476       for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
4477         {
4478           struct occr *occr;
4479
4480           /* If the basic block isn't reachable, PPOUT will be TRUE.
4481              However, we don't want to insert a copy here because the
4482              expression may not really be redundant.  So only insert
4483              an insn if the expression was deleted.
4484              This test also avoids further processing if the expression
4485              wasn't deleted anywhere.  */
4486           if (expr->reaching_reg == NULL)
4487             continue;
4488
4489           for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
4490             {
4491               struct occr *avail;
4492
4493               if (! occr->deleted_p)
4494                 continue;
4495
4496               for (avail = expr->avail_occr; avail != NULL; avail = avail->next)
4497                 {
4498                   rtx insn = avail->insn;
4499
4500                   /* No need to handle this one if handled already.  */
4501                   if (avail->copied_p)
4502                     continue;
4503                   /* Don't handle this one if it's a redundant one.  */
4504                   if (TEST_BIT (pre_redundant, INSN_CUID (insn)))
4505                     continue;
4506                   /* Or if the expression doesn't reach the deleted one.  */
4507                   if (! pre_expr_reaches_here_p (avail, expr,
4508                                                  BLOCK_NUM (occr->insn),
4509                                                  NULL))
4510                     continue;
4511
4512                   /* Copy the result of avail to reaching_reg.  */
4513                   pre_insert_copy_insn (expr, insn);
4514                   avail->copied_p = 1;
4515                 }
4516             }
4517         }
4518     }
4519 }
4520
4521 /* Delete redundant computations.
4522    These are ones that satisy ANTLOC & PPIN.
4523    Deletion is done by changing the insn to copy the `reaching_reg' of
4524    the expression into the result of the SET.  It is left to later passes
4525    (cprop, cse2, flow, combine, regmove) to propagate the copy or eliminate it.
4526
4527    Returns non-zero if a change is made.  */
4528
4529 static int
4530 pre_delete ()
4531 {
4532   int i, changed;
4533
4534   changed = 0;
4535   for (i = 0; i < expr_hash_table_size; i++)
4536     {
4537       struct expr *expr;
4538
4539       for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
4540         {
4541           struct occr *occr;
4542           int indx = expr->bitmap_index;
4543
4544           /* We only need to search antic_occr since we require
4545              ANTLOC != 0.  */
4546
4547           for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
4548             {
4549               rtx insn = occr->insn;
4550               rtx set;
4551               int bb = BLOCK_NUM (insn);
4552               sbitmap ppin = pre_ppin[bb];
4553
4554               if (TEST_BIT (ppin, indx))
4555                 {
4556                   set = single_set (insn);
4557                   if (! set)
4558                     abort ();
4559
4560                   /* Create a pseudo-reg to store the result of reaching
4561                      expressions into.  Get the mode for the new pseudo
4562                      from the mode of the original destination pseudo.  */
4563                   if (expr->reaching_reg == NULL)
4564                     expr->reaching_reg
4565                       = gen_reg_rtx (GET_MODE (SET_DEST (set)));
4566
4567                   /* In theory this should never fail since we're creating
4568                      a reg->reg copy.
4569
4570                      However, on the x86 some of the movXX patterns actually
4571                      contain clobbers of scratch regs.  This may cause the
4572                      insn created by validate_change to not patch any pattern
4573                      and thus cause validate_change to fail.   */
4574                   if (validate_change (insn, &SET_SRC (set),
4575                                        expr->reaching_reg, 0))
4576                     {
4577                       occr->deleted_p = 1;
4578                       SET_BIT (pre_redundant, INSN_CUID (insn));
4579                       changed = 1;
4580                       gcse_subst_count++;
4581                     }
4582
4583                   if (gcse_file)
4584                     {
4585                       fprintf (gcse_file, "PRE: redundant insn %d (expression %d) in bb %d, reaching reg is %d\n",
4586                                INSN_UID (insn), indx, bb, REGNO (expr->reaching_reg));
4587                     }
4588                 }
4589             }
4590         }
4591     }
4592
4593   return changed;
4594 }
4595
4596 /* Perform GCSE optimizations using PRE.
4597    This is called by one_pre_gcse_pass after all the dataflow analysis
4598    has been done.
4599
4600    This is based on the original Morel-Renvoise paper and Fred Chow's thesis.
4601
4602    The M-R paper uses "TRANSP" to describe an expression as being transparent
4603    in a block where as Chow's thesis uses "ALTERED".  We use TRANSP.
4604
4605    ??? A new pseudo reg is created to hold the reaching expression.
4606    The nice thing about the classical approach is that it would try to
4607    use an existing reg.  If the register can't be adequately optimized
4608    [i.e. we introduce reload problems], one could add a pass here to
4609    propagate the new register through the block.
4610
4611    ??? We don't handle single sets in PARALLELs because we're [currently]
4612    not able to copy the rest of the parallel when we insert copies to create
4613    full redundancies from partial redundancies.  However, there's no reason
4614    why we can't handle PARALLELs in the cases where there are no partial
4615    redundancies.  */
4616
4617 static int
4618 pre_gcse ()
4619 {
4620   int i;
4621   int changed;
4622   struct expr **index_map;
4623
4624   /* Compute a mapping from expression number (`bitmap_index') to
4625      hash table entry.  */
4626
4627   index_map = (struct expr **) alloca (n_exprs * sizeof (struct expr *));
4628   bzero ((char *) index_map, n_exprs * sizeof (struct expr *));
4629   for (i = 0; i < expr_hash_table_size; i++)
4630     {
4631       struct expr *expr;
4632
4633       for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
4634         index_map[expr->bitmap_index] = expr;
4635     }
4636
4637   /* Reset bitmap used to track which insns are redundant.  */
4638   pre_redundant = sbitmap_alloc (max_cuid);
4639   sbitmap_zero (pre_redundant);
4640
4641   /* Delete the redundant insns first so that
4642      - we know what register to use for the new insns and for the other
4643        ones with reaching expressions
4644      - we know which insns are redundant when we go to create copies  */
4645   changed = pre_delete ();
4646
4647   /* Insert insns in places that make partially redundant expressions
4648      fully redundant.  */
4649   pre_insert (index_map);
4650
4651   /* In other places with reaching expressions, copy the expression to the
4652      specially allocated pseudo-reg that reaches the redundant expression.  */
4653   pre_insert_copies ();
4654
4655   free (pre_redundant);
4656
4657   return changed;
4658 }
4659
4660 /* Top level routine to perform one PRE GCSE pass.
4661
4662    Return non-zero if a change was made.  */
4663
4664 static int
4665 one_pre_gcse_pass (f, pass)
4666      rtx f;
4667      int pass;
4668 {
4669   int changed = 0;
4670
4671   gcse_subst_count = 0;
4672   gcse_create_count = 0;
4673
4674   alloc_expr_hash_table (max_cuid);
4675   compute_expr_hash_table (f);
4676   if (gcse_file)
4677     dump_hash_table (gcse_file, "Expression", expr_hash_table,
4678                      expr_hash_table_size, n_exprs);
4679   if (n_exprs > 0)
4680     {
4681       alloc_pre_mem (n_basic_blocks, n_exprs);
4682       compute_pre_data ();
4683       changed |= pre_gcse ();
4684       free_pre_mem ();
4685     }
4686   free_expr_hash_table ();
4687
4688   if (gcse_file)
4689     {
4690       fprintf (gcse_file, "\n");
4691       fprintf (gcse_file, "PRE GCSE of %s, pass %d: %d bytes needed, %d substs, %d insns created\n",
4692                current_function_name, pass,
4693                bytes_used, gcse_subst_count, gcse_create_count);
4694     }
4695
4696   return changed;
4697 }