OSDN Git Service

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