]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/gcse.c
Daily bump.
[thirdparty/gcc.git] / gcc / gcse.c
CommitLineData
f4e584dc 1/* Global common subexpression elimination/Partial redundancy elimination
7506f491 2 and global constant/copy propagation for GNU compiler.
a5cad800 3 Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
7506f491
DE
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, 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.
f4e584dc
JL
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.
7506f491
DE
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
7506f491
DE
35*/
36
37/* References searched while implementing this.
7506f491
DE
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
7506f491
DE
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
7506f491
DE
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
f4e584dc
JL
125 Advanced Compiler Design and Implementation
126 Steven Muchnick
127 Morgan Kaufmann, 1997
128
a42cd965
AM
129 Building an Optimizing Compiler
130 Robert Morgan
131 Digital Press, 1998
132
f4e584dc
JL
133 People wishing to speed up the code here should read:
134 Elimination Algorithms for Data Flow Analysis
135 B.G. Ryder, M.C. Paull
136 ACM Computing Surveys, Vol. 18, Num. 3, Sep. 1986
137
138 How to Analyze Large Programs Efficiently and Informatively
139 D.M. Dhamdhere, B.K. Rosen, F.K. Zadeck
140 ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
141
7506f491
DE
142 People wishing to do something different can find various possibilities
143 in the above papers and elsewhere.
144*/
145
146#include "config.h"
50b2596f 147#include "system.h"
01198c2f 148#include "toplev.h"
7506f491
DE
149
150#include "rtl.h"
6baf1cc8 151#include "tm_p.h"
7506f491
DE
152#include "regs.h"
153#include "hard-reg-set.h"
154#include "flags.h"
155#include "real.h"
156#include "insn-config.h"
157#include "recog.h"
158#include "basic-block.h"
50b2596f 159#include "output.h"
49ad7cfa 160#include "function.h"
3cdbd1f8 161#include "expr.h"
7506f491
DE
162
163#include "obstack.h"
164#define obstack_chunk_alloc gmalloc
165#define obstack_chunk_free free
166
167/* Maximum number of passes to perform. */
168#define MAX_PASSES 1
169
170/* Propagate flow information through back edges and thus enable PRE's
171 moving loop invariant calculations out of loops.
172
173 Originally this tended to create worse overall code, but several
174 improvements during the development of PRE seem to have made following
175 back edges generally a win.
176
177 Note much of the loop invariant code motion done here would normally
178 be done by loop.c, which has more heuristics for when to move invariants
179 out of loops. At some point we might need to move some of those
180 heuristics into gcse.c. */
181#define FOLLOW_BACK_EDGES 1
182
f4e584dc
JL
183/* We support GCSE via Partial Redundancy Elimination. PRE optimizations
184 are a superset of those done by GCSE.
7506f491 185
f4e584dc 186 We perform the following steps:
7506f491
DE
187
188 1) Compute basic block information.
189
190 2) Compute table of places where registers are set.
191
192 3) Perform copy/constant propagation.
193
194 4) Perform global cse.
195
e78d9500 196 5) Perform another pass of copy/constant propagation.
7506f491
DE
197
198 Two passes of copy/constant propagation are done because the first one
199 enables more GCSE and the second one helps to clean up the copies that
200 GCSE creates. This is needed more for PRE than for Classic because Classic
201 GCSE will try to use an existing register containing the common
202 subexpression rather than create a new one. This is harder to do for PRE
203 because of the code motion (which Classic GCSE doesn't do).
204
205 Expressions we are interested in GCSE-ing are of the form
206 (set (pseudo-reg) (expression)).
207 Function want_to_gcse_p says what these are.
208
209 PRE handles moving invariant expressions out of loops (by treating them as
f4e584dc 210 partially redundant).
7506f491
DE
211
212 Eventually it would be nice to replace cse.c/gcse.c with SSA (static single
213 assignment) based GVN (global value numbering). L. T. Simpson's paper
214 (Rice University) on value numbering is a useful reference for this.
215
216 **********************
217
218 We used to support multiple passes but there are diminishing returns in
219 doing so. The first pass usually makes 90% of the changes that are doable.
220 A second pass can make a few more changes made possible by the first pass.
221 Experiments show any further passes don't make enough changes to justify
222 the expense.
223
224 A study of spec92 using an unlimited number of passes:
225 [1 pass] = 1208 substitutions, [2] = 577, [3] = 202, [4] = 192, [5] = 83,
226 [6] = 34, [7] = 17, [8] = 9, [9] = 4, [10] = 4, [11] = 2,
227 [12] = 2, [13] = 1, [15] = 1, [16] = 2, [41] = 1
228
229 It was found doing copy propagation between each pass enables further
230 substitutions.
231
232 PRE is quite expensive in complicated functions because the DFA can take
233 awhile to converge. Hence we only perform one pass. Macro MAX_PASSES can
234 be modified if one wants to experiment.
235
236 **********************
237
238 The steps for PRE are:
239
240 1) Build the hash table of expressions we wish to GCSE (expr_hash_table).
241
242 2) Perform the data flow analysis for PRE.
243
244 3) Delete the redundant instructions
245
246 4) Insert the required copies [if any] that make the partially
247 redundant instructions fully redundant.
248
249 5) For other reaching expressions, insert an instruction to copy the value
250 to a newly created pseudo that will reach the redundant instruction.
251
252 The deletion is done first so that when we do insertions we
253 know which pseudo reg to use.
254
255 Various papers have argued that PRE DFA is expensive (O(n^2)) and others
256 argue it is not. The number of iterations for the algorithm to converge
257 is typically 2-4 so I don't view it as that expensive (relatively speaking).
258
f4e584dc 259 PRE GCSE depends heavily on the second CSE pass to clean up the copies
7506f491
DE
260 we create. To make an expression reach the place where it's redundant,
261 the result of the expression is copied to a new register, and the redundant
262 expression is deleted by replacing it with this new register. Classic GCSE
263 doesn't have this problem as much as it computes the reaching defs of
264 each register in each block and thus can try to use an existing register.
265
266 **********************
267
7506f491
DE
268 A fair bit of simplicity is created by creating small functions for simple
269 tasks, even when the function is only called in one place. This may
270 measurably slow things down [or may not] by creating more function call
271 overhead than is necessary. The source is laid out so that it's trivial
272 to make the affected functions inline so that one can measure what speed
273 up, if any, can be achieved, and maybe later when things settle things can
274 be rearranged.
275
276 Help stamp out big monolithic functions! */
277\f
278/* GCSE global vars. */
279
280/* -dG dump file. */
281static FILE *gcse_file;
282
f4e584dc
JL
283/* Note whether or not we should run jump optimization after gcse. We
284 want to do this for two cases.
285
286 * If we changed any jumps via cprop.
287
288 * If we added any labels via edge splitting. */
289
290static int run_jump_opt_after_gcse;
291
7506f491
DE
292/* Bitmaps are normally not included in debugging dumps.
293 However it's useful to be able to print them from GDB.
294 We could create special functions for this, but it's simpler to
295 just allow passing stderr to the dump_foo fns. Since stderr can
296 be a macro, we store a copy here. */
297static FILE *debug_stderr;
298
299/* An obstack for our working variables. */
300static struct obstack gcse_obstack;
301
302/* Non-zero for each mode that supports (set (reg) (reg)).
303 This is trivially true for integer and floating point values.
304 It may or may not be true for condition codes. */
305static char can_copy_p[(int) NUM_MACHINE_MODES];
306
307/* Non-zero if can_copy_p has been initialized. */
308static int can_copy_init_p;
309
abd535b6
BS
310struct reg_use {
311 rtx reg_rtx;
312};
313
7506f491
DE
314/* Hash table of expressions. */
315
316struct expr
317{
318 /* The expression (SET_SRC for expressions, PATTERN for assignments). */
319 rtx expr;
320 /* Index in the available expression bitmaps. */
321 int bitmap_index;
322 /* Next entry with the same hash. */
323 struct expr *next_same_hash;
324 /* List of anticipatable occurrences in basic blocks in the function.
325 An "anticipatable occurrence" is one that is the first occurrence in the
f4e584dc
JL
326 basic block, the operands are not modified in the basic block prior
327 to the occurrence and the output is not used between the start of
328 the block and the occurrence. */
7506f491
DE
329 struct occr *antic_occr;
330 /* List of available occurrence in basic blocks in the function.
331 An "available occurrence" is one that is the last occurrence in the
332 basic block and the operands are not modified by following statements in
333 the basic block [including this insn]. */
334 struct occr *avail_occr;
335 /* Non-null if the computation is PRE redundant.
336 The value is the newly created pseudo-reg to record a copy of the
337 expression in all the places that reach the redundant copy. */
338 rtx reaching_reg;
339};
340
341/* Occurrence of an expression.
342 There is one per basic block. If a pattern appears more than once the
343 last appearance is used [or first for anticipatable expressions]. */
344
345struct occr
346{
347 /* Next occurrence of this expression. */
348 struct occr *next;
349 /* The insn that computes the expression. */
350 rtx insn;
351 /* Non-zero if this [anticipatable] occurrence has been deleted. */
352 char deleted_p;
353 /* Non-zero if this [available] occurrence has been copied to
354 reaching_reg. */
355 /* ??? This is mutually exclusive with deleted_p, so they could share
356 the same byte. */
357 char copied_p;
358};
359
360/* Expression and copy propagation hash tables.
361 Each hash table is an array of buckets.
362 ??? It is known that if it were an array of entries, structure elements
363 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
364 not clear whether in the final analysis a sufficient amount of memory would
365 be saved as the size of the available expression bitmaps would be larger
366 [one could build a mapping table without holes afterwards though].
367 Someday I'll perform the computation and figure it out.
368*/
369
370/* Total size of the expression hash table, in elements. */
371static int expr_hash_table_size;
372/* The table itself.
373 This is an array of `expr_hash_table_size' elements. */
374static struct expr **expr_hash_table;
375
376/* Total size of the copy propagation hash table, in elements. */
377static int set_hash_table_size;
378/* The table itself.
379 This is an array of `set_hash_table_size' elements. */
380static struct expr **set_hash_table;
381
382/* Mapping of uids to cuids.
383 Only real insns get cuids. */
384static int *uid_cuid;
385
386/* Highest UID in UID_CUID. */
387static int max_uid;
388
389/* Get the cuid of an insn. */
390#define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
391
392/* Number of cuids. */
393static int max_cuid;
394
395/* Mapping of cuids to insns. */
396static rtx *cuid_insn;
397
398/* Get insn from cuid. */
399#define CUID_INSN(CUID) (cuid_insn[CUID])
400
401/* Maximum register number in function prior to doing gcse + 1.
402 Registers created during this pass have regno >= max_gcse_regno.
403 This is named with "gcse" to not collide with global of same name. */
404static int max_gcse_regno;
405
406/* Maximum number of cse-able expressions found. */
407static int n_exprs;
408/* Maximum number of assignments for copy propagation found. */
409static int n_sets;
410
411/* Table of registers that are modified.
412 For each register, each element is a list of places where the pseudo-reg
413 is set.
414
415 For simplicity, GCSE is done on sets of pseudo-regs only. PRE GCSE only
416 requires knowledge of which blocks kill which regs [and thus could use
f4e584dc 417 a bitmap instead of the lists `reg_set_table' uses].
7506f491 418
f4e584dc
JL
419 `reg_set_table' and could be turned into an array of bitmaps
420 (num-bbs x num-regs)
7506f491
DE
421 [however perhaps it may be useful to keep the data as is].
422 One advantage of recording things this way is that `reg_set_table' is
423 fairly sparse with respect to pseudo regs but for hard regs could be
424 fairly dense [relatively speaking].
425 And recording sets of pseudo-regs in lists speeds
426 up functions like compute_transp since in the case of pseudo-regs we only
427 need to iterate over the number of times a pseudo-reg is set, not over the
428 number of basic blocks [clearly there is a bit of a slow down in the cases
429 where a pseudo is set more than once in a block, however it is believed
430 that the net effect is to speed things up]. This isn't done for hard-regs
431 because recording call-clobbered hard-regs in `reg_set_table' at each
432 function call can consume a fair bit of memory, and iterating over hard-regs
433 stored this way in compute_transp will be more expensive. */
434
435typedef struct reg_set {
436 /* The next setting of this register. */
437 struct reg_set *next;
438 /* The insn where it was set. */
439 rtx insn;
440} reg_set;
441static reg_set **reg_set_table;
442/* Size of `reg_set_table'.
443 The table starts out at max_gcse_regno + slop, and is enlarged as
444 necessary. */
445static int reg_set_table_size;
446/* Amount to grow `reg_set_table' by when it's full. */
447#define REG_SET_TABLE_SLOP 100
448
449/* Bitmap containing one bit for each register in the program.
450 Used when performing GCSE to track which registers have been set since
451 the start of the basic block. */
452static sbitmap reg_set_bitmap;
453
454/* For each block, a bitmap of registers set in the block.
455 This is used by expr_killed_p and compute_transp.
456 It is computed during hash table computation and not by compute_sets
457 as it includes registers added since the last pass (or between cprop and
458 gcse) and it's currently not easy to realloc sbitmap vectors. */
459static sbitmap *reg_set_in_block;
460
461/* For each block, non-zero if memory is set in that block.
462 This is computed during hash table computation and is used by
463 expr_killed_p and compute_transp.
464 ??? Handling of memory is very simple, we don't make any attempt
465 to optimize things (later).
466 ??? This can be computed by compute_sets since the information
467 doesn't change. */
468static char *mem_set_in_block;
469
470/* Various variables for statistics gathering. */
471
472/* Memory used in a pass.
473 This isn't intended to be absolutely precise. Its intent is only
474 to keep an eye on memory usage. */
475static int bytes_used;
476/* GCSE substitutions made. */
477static int gcse_subst_count;
478/* Number of copy instructions created. */
479static int gcse_create_count;
480/* Number of constants propagated. */
481static int const_prop_count;
482/* Number of copys propagated. */
483static int copy_prop_count;
7506f491
DE
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. */
494static 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 */
508static sbitmap *rd_kill, *rd_gen, *reaching_defs, *rd_out;
509
510/* for available exprs */
511static sbitmap *ae_kill, *ae_gen, *ae_in, *ae_out;
b5ce41ff 512
0511851c
MM
513/* Objects of this type are passed around by the null-pointer check
514 removal routines. */
515struct null_pointer_info {
516 /* The basic block being processed. */
517 int current_block;
518 /* The first register to be handled in this pass. */
519 int min_reg;
520 /* One greater than the last register to be handled in this pass. */
521 int max_reg;
522 sbitmap *nonnull_local;
523 sbitmap *nonnull_killed;
524};
7506f491 525\f
ac7c5af5
JL
526static void compute_can_copy PROTO ((void));
527
528static char *gmalloc PROTO ((unsigned int));
529static char *grealloc PROTO ((char *, unsigned int));
530static char *gcse_alloc PROTO ((unsigned long));
531static void alloc_gcse_mem PROTO ((rtx));
532static void free_gcse_mem PROTO ((void));
ac7c5af5
JL
533static void alloc_reg_set_mem PROTO ((int));
534static void free_reg_set_mem PROTO ((void));
0511851c 535static int get_bitmap_width PROTO ((int, int, int));
ac7c5af5 536static void record_one_set PROTO ((int, rtx));
84832317 537static void record_set_info PROTO ((rtx, rtx, void *));
ac7c5af5
JL
538static void compute_sets PROTO ((rtx));
539
540static void hash_scan_insn PROTO ((rtx, int, int));
541static void hash_scan_set PROTO ((rtx, rtx, int));
542static void hash_scan_clobber PROTO ((rtx, rtx));
543static void hash_scan_call PROTO ((rtx, rtx));
ac7c5af5
JL
544static int want_to_gcse_p PROTO ((rtx));
545static int oprs_unchanged_p PROTO ((rtx, rtx, int));
7506f491 546static int oprs_anticipatable_p PROTO ((rtx, rtx));
ac7c5af5 547static int oprs_available_p PROTO ((rtx, rtx));
b5ce41ff
JL
548static void insert_expr_in_table PROTO ((rtx, enum machine_mode,
549 rtx, int, int));
7506f491 550static void insert_set_in_table PROTO ((rtx, rtx));
b5ce41ff
JL
551static unsigned int hash_expr PROTO ((rtx, enum machine_mode,
552 int *, int));
7506f491 553static unsigned int hash_expr_1 PROTO ((rtx, enum machine_mode, int *));
ac7c5af5
JL
554static unsigned int hash_set PROTO ((int, int));
555static int expr_equiv_p PROTO ((rtx, rtx));
7506f491
DE
556static void record_last_reg_set_info PROTO ((rtx, int));
557static void record_last_mem_set_info PROTO ((rtx));
84832317 558static void record_last_set_info PROTO ((rtx, rtx, void *));
b5ce41ff 559static void compute_hash_table PROTO ((int));
7506f491
DE
560static void alloc_set_hash_table PROTO ((int));
561static void free_set_hash_table PROTO ((void));
b5ce41ff 562static void compute_set_hash_table PROTO ((void));
7506f491
DE
563static void alloc_expr_hash_table PROTO ((int));
564static void free_expr_hash_table PROTO ((void));
b5ce41ff 565static void compute_expr_hash_table PROTO ((void));
a65f3558
JL
566static void dump_hash_table PROTO ((FILE *, const char *, struct expr **,
567 int, int));
7506f491 568static struct expr *lookup_expr PROTO ((rtx));
ac7c5af5
JL
569static struct expr *lookup_set PROTO ((int, rtx));
570static struct expr *next_set PROTO ((int, struct expr *));
7506f491 571static void reset_opr_set_tables PROTO ((void));
ac7c5af5 572static int oprs_not_set_p PROTO ((rtx, rtx));
b5ce41ff 573static void mark_call PROTO ((rtx));
ac7c5af5
JL
574static void mark_set PROTO ((rtx, rtx));
575static void mark_clobber PROTO ((rtx, rtx));
576static void mark_oprs_set PROTO ((rtx));
577
ac7c5af5
JL
578static void alloc_cprop_mem PROTO ((int, int));
579static void free_cprop_mem PROTO ((void));
ac7c5af5 580static void compute_transp PROTO ((rtx, int, sbitmap *, int));
a65f3558 581static void compute_transpout PROTO ((void));
b5ce41ff
JL
582static void compute_local_properties PROTO ((sbitmap *, sbitmap *,
583 sbitmap *, int));
ac7c5af5
JL
584static void compute_cprop_data PROTO ((void));
585static void find_used_regs PROTO ((rtx));
586static int try_replace_reg PROTO ((rtx, rtx, rtx));
7506f491 587static struct expr *find_avail_set PROTO ((int, rtx));
abd535b6 588static int cprop_jump PROTO((rtx, rtx, struct reg_use *, rtx));
e2bef702 589#ifdef HAVE_cc0
abd535b6 590static int cprop_cc0_jump PROTO((rtx, struct reg_use *, rtx));
e2bef702 591#endif
b5ce41ff
JL
592static int cprop_insn PROTO ((rtx, int));
593static int cprop PROTO ((int));
594static int one_cprop_pass PROTO ((int, int));
7506f491 595
ac7c5af5
JL
596static void alloc_pre_mem PROTO ((int, int));
597static void free_pre_mem PROTO ((void));
ac7c5af5 598static void compute_pre_data PROTO ((void));
89e606c9 599static int pre_expr_reaches_here_p PROTO ((int, struct expr *, int));
a65f3558 600static void insert_insn_end_bb PROTO ((struct expr *, int, int));
7506f491 601static void pre_insert_copy_insn PROTO ((struct expr *, rtx));
ac7c5af5
JL
602static void pre_insert_copies PROTO ((void));
603static int pre_delete PROTO ((void));
604static int pre_gcse PROTO ((void));
b5ce41ff 605static int one_pre_gcse_pass PROTO ((int));
aeb2f500
JW
606
607static void add_label_notes PROTO ((rtx, rtx));
b5ce41ff 608
bb457bd9
JL
609static void alloc_code_hoist_mem PROTO ((int, int));
610static void free_code_hoist_mem PROTO ((void));
611static void compute_code_hoist_vbeinout PROTO ((void));
612static void compute_code_hoist_data PROTO ((void));
613static int hoist_expr_reaches_here_p PROTO ((int, int, int, char *));
614static void hoist_code PROTO ((void));
615static int one_code_hoisting_pass PROTO ((void));
616
b5ce41ff
JL
617static void alloc_rd_mem PROTO ((int, int));
618static void free_rd_mem PROTO ((void));
619static void handle_rd_kill_set PROTO ((rtx, int, int));
620static void compute_kill_rd PROTO ((void));
621static void compute_rd PROTO ((void));
622static void alloc_avail_expr_mem PROTO ((int, int));
623static void free_avail_expr_mem PROTO ((void));
624static void compute_ae_gen PROTO ((void));
625static int expr_killed_p PROTO ((rtx, int));
a42cd965 626static void compute_ae_kill PROTO ((sbitmap *, sbitmap *));
b5ce41ff 627static int expr_reaches_here_p PROTO ((struct occr *, struct expr *,
283a2545 628 int, int));
b5ce41ff
JL
629static rtx computing_insn PROTO ((struct expr *, rtx));
630static int def_reaches_here_p PROTO ((rtx, rtx));
631static int can_disregard_other_sets PROTO ((struct reg_set **, rtx, int));
632static int handle_avail_expr PROTO ((rtx, struct expr *));
633static int classic_gcse PROTO ((void));
634static int one_classic_gcse_pass PROTO ((int));
84832317 635static void invalidate_nonnull_info PROTO ((rtx, rtx, void *));
b71a2ff8 636static void delete_null_pointer_checks_1 PROTO ((int *, sbitmap *, sbitmap *,
0511851c 637 struct null_pointer_info *));
36f0e0a6
KG
638static rtx process_insert_insn PROTO ((struct expr *));
639static int pre_edge_insert PROTO ((struct edge_list *, struct expr **));
a8f227e7 640static int expr_reaches_here_p_work PROTO ((struct occr *, struct expr *, int, int, char *));
89e606c9
JL
641static int pre_expr_reaches_here_p_work PROTO ((int, struct expr *,
642 int, char *));
7506f491
DE
643\f
644/* Entry point for global common subexpression elimination.
645 F is the first instruction in the function. */
646
e78d9500 647int
7506f491
DE
648gcse_main (f, file)
649 rtx f;
650 FILE *file;
651{
652 int changed, pass;
653 /* Bytes used at start of pass. */
654 int initial_bytes_used;
655 /* Maximum number of bytes used by a pass. */
656 int max_pass_bytes;
657 /* Point to release obstack data from for each pass. */
658 char *gcse_obstack_bottom;
659
b5ce41ff
JL
660 /* We do not construct an accurate cfg in functions which call
661 setjmp, so just punt to be safe. */
7506f491 662 if (current_function_calls_setjmp)
e78d9500 663 return 0;
7506f491 664
b5ce41ff
JL
665 /* Assume that we do not need to run jump optimizations after gcse. */
666 run_jump_opt_after_gcse = 0;
667
7506f491
DE
668 /* For calling dump_foo fns from gdb. */
669 debug_stderr = stderr;
b5ce41ff 670 gcse_file = file;
7506f491 671
b5ce41ff
JL
672 /* Identify the basic block information for this function, including
673 successors and predecessors. */
7506f491 674 max_gcse_regno = max_reg_num ();
359da67d 675 find_basic_blocks (f, max_gcse_regno, file, 1);
7506f491 676
a42cd965
AM
677 if (file)
678 dump_flow_info (file);
679
7506f491
DE
680 /* Return if there's nothing to do. */
681 if (n_basic_blocks <= 1)
682 {
683 /* Free storage allocated by find_basic_blocks. */
684 free_basic_block_vars (0);
e78d9500 685 return 0;
7506f491
DE
686 }
687
55f7891b
JL
688 /* Trying to perform global optimizations on flow graphs which have
689 a high connectivity will take a long time and is unlikely to be
690 particularly useful.
691
692 In normal circumstances a cfg should have about twice has many edges
693 as blocks. But we do not want to punish small functions which have
694 a couple switch statements. So we require a relatively large number
695 of basic blocks and the ratio of edges to blocks to be high. */
696 if (n_basic_blocks > 1000 && n_edges / n_basic_blocks >= 20)
697 {
698 /* Free storage allocated by find_basic_blocks. */
699 free_basic_block_vars (0);
700 return 0;
701 }
702
7506f491
DE
703 /* See what modes support reg/reg copy operations. */
704 if (! can_copy_init_p)
705 {
706 compute_can_copy ();
707 can_copy_init_p = 1;
708 }
709
710 gcc_obstack_init (&gcse_obstack);
a42cd965 711 bytes_used = 0;
7506f491
DE
712
713 /* Record where pseudo-registers are set.
714 This data is kept accurate during each pass.
b5ce41ff 715 ??? We could also record hard-reg information here
7506f491 716 [since it's unchanging], however it is currently done during
b5ce41ff
JL
717 hash table computation.
718
719 It may be tempting to compute MEM set information here too, but MEM
720 sets will be subject to code motion one day and thus we need to compute
721 information about memory sets when we build the hash tables. */
7506f491
DE
722
723 alloc_reg_set_mem (max_gcse_regno);
724 compute_sets (f);
725
726 pass = 0;
727 initial_bytes_used = bytes_used;
728 max_pass_bytes = 0;
729 gcse_obstack_bottom = gcse_alloc (1);
730 changed = 1;
731 while (changed && pass < MAX_PASSES)
732 {
733 changed = 0;
734 if (file)
735 fprintf (file, "GCSE pass %d\n\n", pass + 1);
736
737 /* Initialize bytes_used to the space for the pred/succ lists,
738 and the reg_set_table data. */
739 bytes_used = initial_bytes_used;
740
741 /* Each pass may create new registers, so recalculate each time. */
742 max_gcse_regno = max_reg_num ();
743
744 alloc_gcse_mem (f);
745
b5ce41ff
JL
746 /* Don't allow constant propagation to modify jumps
747 during this pass. */
748 changed = one_cprop_pass (pass + 1, 0);
7506f491
DE
749
750 if (optimize_size)
b5ce41ff 751 changed |= one_classic_gcse_pass (pass + 1);
7506f491 752 else
a42cd965
AM
753 {
754 changed |= one_pre_gcse_pass (pass + 1);
755 free_reg_set_mem ();
756 alloc_reg_set_mem (max_reg_num ());
757 compute_sets (f);
758 run_jump_opt_after_gcse = 1;
759 }
7506f491
DE
760
761 if (max_pass_bytes < bytes_used)
762 max_pass_bytes = bytes_used;
763
bb457bd9
JL
764 /* Free up memory, then reallocate for code hoisting. We can
765 not re-use the existing allocated memory because the tables
766 will not have info for the insns or registers created by
767 partial redundancy elimination. */
7506f491
DE
768 free_gcse_mem ();
769
bb457bd9
JL
770 /* It does not make sense to run code hoisting unless we optimizing
771 for code size -- it rarely makes programs faster, and can make
772 them bigger if we did partial redundancy elimination (when optimizing
773 for space, we use a classic gcse algorithm instead of partial
774 redundancy algorithms). */
775 if (optimize_size)
776 {
777 max_gcse_regno = max_reg_num ();
778 alloc_gcse_mem (f);
779 changed |= one_code_hoisting_pass ();
780 free_gcse_mem ();
781
782 if (max_pass_bytes < bytes_used)
783 max_pass_bytes = bytes_used;
784 }
785
7506f491
DE
786 if (file)
787 {
788 fprintf (file, "\n");
789 fflush (file);
790 }
791 obstack_free (&gcse_obstack, gcse_obstack_bottom);
792 pass++;
793 }
794
b5ce41ff
JL
795 /* Do one last pass of copy propagation, including cprop into
796 conditional jumps. */
797
798 max_gcse_regno = max_reg_num ();
799 alloc_gcse_mem (f);
800 /* This time, go ahead and allow cprop to alter jumps. */
801 one_cprop_pass (pass + 1, 1);
802 free_gcse_mem ();
7506f491
DE
803
804 if (file)
805 {
806 fprintf (file, "GCSE of %s: %d basic blocks, ",
807 current_function_name, n_basic_blocks);
808 fprintf (file, "%d pass%s, %d bytes\n\n",
809 pass, pass > 1 ? "es" : "", max_pass_bytes);
810 }
811
812 /* Free our obstack. */
813 obstack_free (&gcse_obstack, NULL_PTR);
814 /* Free reg_set_table. */
815 free_reg_set_mem ();
7506f491
DE
816 /* Free storage allocated by find_basic_blocks. */
817 free_basic_block_vars (0);
e78d9500 818 return run_jump_opt_after_gcse;
7506f491
DE
819}
820\f
821/* Misc. utilities. */
822
823/* Compute which modes support reg/reg copy operations. */
824
825static void
826compute_can_copy ()
827{
828 int i;
50b2596f 829#ifndef AVOID_CCMODE_COPIES
7506f491 830 rtx reg,insn;
50b2596f 831#endif
7506f491
DE
832 char *free_point = (char *) oballoc (1);
833
834 bzero (can_copy_p, NUM_MACHINE_MODES);
835
836 start_sequence ();
837 for (i = 0; i < NUM_MACHINE_MODES; i++)
838 {
839 switch (GET_MODE_CLASS (i))
840 {
841 case MODE_CC :
842#ifdef AVOID_CCMODE_COPIES
843 can_copy_p[i] = 0;
844#else
9e6a5703
JC
845 reg = gen_rtx_REG ((enum machine_mode) i, LAST_VIRTUAL_REGISTER + 1);
846 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, reg));
7506f491
DE
847 if (recog (PATTERN (insn), insn, NULL_PTR) >= 0)
848 can_copy_p[i] = 1;
849#endif
850 break;
851 default :
852 can_copy_p[i] = 1;
853 break;
854 }
855 }
856 end_sequence ();
857
858 /* Free the objects we just allocated. */
859 obfree (free_point);
860}
861\f
862/* Cover function to xmalloc to record bytes allocated. */
863
864static char *
865gmalloc (size)
866 unsigned int size;
867{
868 bytes_used += size;
869 return xmalloc (size);
870}
871
872/* Cover function to xrealloc.
873 We don't record the additional size since we don't know it.
874 It won't affect memory usage stats much anyway. */
875
876static char *
877grealloc (ptr, size)
878 char *ptr;
879 unsigned int size;
880{
881 return xrealloc (ptr, size);
882}
883
884/* Cover function to obstack_alloc.
885 We don't need to record the bytes allocated here since
886 obstack_chunk_alloc is set to gmalloc. */
887
888static char *
889gcse_alloc (size)
890 unsigned long size;
891{
892 return (char *) obstack_alloc (&gcse_obstack, size);
893}
894
895/* Allocate memory for the cuid mapping array,
896 and reg/memory set tracking tables.
897
898 This is called at the start of each pass. */
899
900static void
901alloc_gcse_mem (f)
902 rtx f;
903{
904 int i,n;
905 rtx insn;
906
907 /* Find the largest UID and create a mapping from UIDs to CUIDs.
908 CUIDs are like UIDs except they increase monotonically, have no gaps,
909 and only apply to real insns. */
910
911 max_uid = get_max_uid ();
912 n = (max_uid + 1) * sizeof (int);
913 uid_cuid = (int *) gmalloc (n);
914 bzero ((char *) uid_cuid, n);
915 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
916 {
917 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
918 INSN_CUID (insn) = i++;
919 else
920 INSN_CUID (insn) = i;
921 }
922
923 /* Create a table mapping cuids to insns. */
924
925 max_cuid = i;
926 n = (max_cuid + 1) * sizeof (rtx);
927 cuid_insn = (rtx *) gmalloc (n);
928 bzero ((char *) cuid_insn, n);
929 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
930 {
931 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
932 {
933 CUID_INSN (i) = insn;
934 i++;
935 }
936 }
937
938 /* Allocate vars to track sets of regs. */
939
940 reg_set_bitmap = (sbitmap) sbitmap_alloc (max_gcse_regno);
941
942 /* Allocate vars to track sets of regs, memory per block. */
943
944 reg_set_in_block = (sbitmap *) sbitmap_vector_alloc (n_basic_blocks,
945 max_gcse_regno);
946 mem_set_in_block = (char *) gmalloc (n_basic_blocks);
947}
948
949/* Free memory allocated by alloc_gcse_mem. */
950
951static void
952free_gcse_mem ()
953{
954 free (uid_cuid);
955 free (cuid_insn);
956
957 free (reg_set_bitmap);
958
959 free (reg_set_in_block);
960 free (mem_set_in_block);
961}
962
0511851c
MM
963/* Many of the global optimization algorithms work by solving dataflow
964 equations for various expressions. Initially, some local value is
965 computed for each expression in each block. Then, the values
966 across the various blocks are combined (by following flow graph
967 edges) to arrive at global values. Conceptually, each set of
968 equations is independent. We may therefore solve all the equations
969 in parallel, solve them one at a time, or pick any intermediate
970 approach.
971
972 When you're going to need N two-dimensional bitmaps, each X (say,
973 the number of blocks) by Y (say, the number of expressions), call
974 this function. It's not important what X and Y represent; only
975 that Y correspond to the things that can be done in parallel. This
976 function will return an appropriate chunking factor C; you should
977 solve C sets of equations in parallel. By going through this
978 function, we can easily trade space against time; by solving fewer
979 equations in parallel we use less space. */
980
981static int
982get_bitmap_width (n, x, y)
983 int n;
984 int x;
985 int y;
986{
987 /* It's not really worth figuring out *exactly* how much memory will
988 be used by a particular choice. The important thing is to get
989 something approximately right. */
990 size_t max_bitmap_memory = 10 * 1024 * 1024;
991
992 /* The number of bytes we'd use for a single column of minimum
993 width. */
994 size_t column_size = n * x * sizeof (SBITMAP_ELT_TYPE);
995
996 /* Often, it's reasonable just to solve all the equations in
997 parallel. */
998 if (column_size * SBITMAP_SET_SIZE (y) <= max_bitmap_memory)
999 return y;
1000
1001 /* Otherwise, pick the largest width we can, without going over the
1002 limit. */
1003 return SBITMAP_ELT_BITS * ((max_bitmap_memory + column_size - 1)
1004 / column_size);
1005}
1006
b5ce41ff
JL
1007\f
1008/* Compute the local properties of each recorded expression.
1009 Local properties are those that are defined by the block, irrespective
1010 of other blocks.
1011
1012 An expression is transparent in a block if its operands are not modified
1013 in the block.
1014
1015 An expression is computed (locally available) in a block if it is computed
1016 at least once and expression would contain the same value if the
1017 computation was moved to the end of the block.
1018
1019 An expression is locally anticipatable in a block if it is computed at
1020 least once and expression would contain the same value if the computation
1021 was moved to the beginning of the block.
1022
1023 We call this routine for cprop, pre and code hoisting. They all
1024 compute basically the same information and thus can easily share
1025 this code.
7506f491 1026
b5ce41ff
JL
1027 TRANSP, COMP, and ANTLOC are destination sbitmaps for recording
1028 local properties. If NULL, then it is not necessary to compute
1029 or record that particular property.
1030
1031 SETP controls which hash table to look at. If zero, this routine
1032 looks at the expr hash table; if nonzero this routine looks at
695ab36a
BS
1033 the set hash table. Additionally, TRANSP is computed as ~TRANSP,
1034 since this is really cprop's ABSALTERED. */
b5ce41ff
JL
1035
1036static void
1037compute_local_properties (transp, comp, antloc, setp)
1038 sbitmap *transp;
1039 sbitmap *comp;
1040 sbitmap *antloc;
1041 int setp;
1042{
1043 int i, hash_table_size;
1044 struct expr **hash_table;
1045
1046 /* Initialize any bitmaps that were passed in. */
1047 if (transp)
695ab36a
BS
1048 {
1049 if (setp)
1050 sbitmap_vector_zero (transp, n_basic_blocks);
1051 else
1052 sbitmap_vector_ones (transp, n_basic_blocks);
1053 }
b5ce41ff
JL
1054 if (comp)
1055 sbitmap_vector_zero (comp, n_basic_blocks);
1056 if (antloc)
1057 sbitmap_vector_zero (antloc, n_basic_blocks);
1058
1059 /* We use the same code for cprop, pre and hoisting. For cprop
1060 we care about the set hash table, for pre and hoisting we
1061 care about the expr hash table. */
1062 hash_table_size = setp ? set_hash_table_size : expr_hash_table_size;
1063 hash_table = setp ? set_hash_table : expr_hash_table;
1064
1065 for (i = 0; i < hash_table_size; i++)
7506f491 1066 {
b5ce41ff
JL
1067 struct expr *expr;
1068
1069 for (expr = hash_table[i]; expr != NULL; expr = expr->next_same_hash)
1070 {
1071 struct occr *occr;
1072 int indx = expr->bitmap_index;
1073
1074 /* The expression is transparent in this block if it is not killed.
1075 We start by assuming all are transparent [none are killed], and
1076 then reset the bits for those that are. */
1077
1078 if (transp)
1079 compute_transp (expr->expr, indx, transp, setp);
1080
1081 /* The occurrences recorded in antic_occr are exactly those that
1082 we want to set to non-zero in ANTLOC. */
1083
1084 if (antloc)
1085 {
1086 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
1087 {
1088 int bb = BLOCK_NUM (occr->insn);
1089 SET_BIT (antloc[bb], indx);
1090
1091 /* While we're scanning the table, this is a good place to
1092 initialize this. */
1093 occr->deleted_p = 0;
1094 }
1095 }
1096
1097 /* The occurrences recorded in avail_occr are exactly those that
1098 we want to set to non-zero in COMP. */
1099 if (comp)
1100 {
1101
1102 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
1103 {
1104 int bb = BLOCK_NUM (occr->insn);
1105 SET_BIT (comp[bb], indx);
1106
1107 /* While we're scanning the table, this is a good place to
1108 initialize this. */
1109 occr->copied_p = 0;
1110 }
1111 }
1112
1113 /* While we're scanning the table, this is a good place to
1114 initialize this. */
1115 expr->reaching_reg = 0;
1116 }
7506f491 1117 }
7506f491 1118}
b5ce41ff 1119
7506f491
DE
1120\f
1121/* Register set information.
1122
1123 `reg_set_table' records where each register is set or otherwise
1124 modified. */
1125
1126static struct obstack reg_set_obstack;
1127
1128static void
1129alloc_reg_set_mem (n_regs)
1130 int n_regs;
1131{
1132 int n;
1133
1134 reg_set_table_size = n_regs + REG_SET_TABLE_SLOP;
1135 n = reg_set_table_size * sizeof (struct reg_set *);
1136 reg_set_table = (struct reg_set **) gmalloc (n);
1137 bzero ((char *) reg_set_table, n);
1138
1139 gcc_obstack_init (&reg_set_obstack);
1140}
1141
1142static void
1143free_reg_set_mem ()
1144{
1145 free (reg_set_table);
1146 obstack_free (&reg_set_obstack, NULL_PTR);
1147}
1148
1149/* Record REGNO in the reg_set table. */
1150
1151static void
1152record_one_set (regno, insn)
1153 int regno;
1154 rtx insn;
1155{
1156 /* allocate a new reg_set element and link it onto the list */
1157 struct reg_set *new_reg_info, *reg_info_ptr1, *reg_info_ptr2;
1158
1159 /* If the table isn't big enough, enlarge it. */
1160 if (regno >= reg_set_table_size)
1161 {
1162 int new_size = regno + REG_SET_TABLE_SLOP;
1163 reg_set_table = (struct reg_set **)
1164 grealloc ((char *) reg_set_table,
1165 new_size * sizeof (struct reg_set *));
1166 bzero ((char *) (reg_set_table + reg_set_table_size),
1167 (new_size - reg_set_table_size) * sizeof (struct reg_set *));
1168 reg_set_table_size = new_size;
1169 }
1170
1171 new_reg_info = (struct reg_set *) obstack_alloc (&reg_set_obstack,
1172 sizeof (struct reg_set));
1173 bytes_used += sizeof (struct reg_set);
1174 new_reg_info->insn = insn;
1175 new_reg_info->next = NULL;
1176 if (reg_set_table[regno] == NULL)
1177 reg_set_table[regno] = new_reg_info;
1178 else
1179 {
1180 reg_info_ptr1 = reg_info_ptr2 = reg_set_table[regno];
1181 /* ??? One could keep a "last" pointer to speed this up. */
1182 while (reg_info_ptr1 != NULL)
1183 {
1184 reg_info_ptr2 = reg_info_ptr1;
1185 reg_info_ptr1 = reg_info_ptr1->next;
1186 }
1187 reg_info_ptr2->next = new_reg_info;
1188 }
1189}
1190
7506f491 1191/* Called from compute_sets via note_stores to handle one
84832317
MM
1192 SET or CLOBBER in an insn. The DATA is really the instruction
1193 in which the SET is occurring. */
7506f491
DE
1194
1195static void
84832317 1196record_set_info (dest, setter, data)
50b2596f 1197 rtx dest, setter ATTRIBUTE_UNUSED;
84832317 1198 void *data;
7506f491 1199{
84832317
MM
1200 rtx record_set_insn = (rtx) data;
1201
7506f491
DE
1202 if (GET_CODE (dest) == SUBREG)
1203 dest = SUBREG_REG (dest);
1204
1205 if (GET_CODE (dest) == REG)
1206 {
1207 if (REGNO (dest) >= FIRST_PSEUDO_REGISTER)
1208 record_one_set (REGNO (dest), record_set_insn);
1209 }
1210}
1211
1212/* Scan the function and record each set of each pseudo-register.
1213
1214 This is called once, at the start of the gcse pass.
1215 See the comments for `reg_set_table' for further docs. */
1216
1217static void
1218compute_sets (f)
1219 rtx f;
1220{
1221 rtx insn = f;
1222
1223 while (insn)
1224 {
1225 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
84832317 1226 note_stores (PATTERN (insn), record_set_info, insn);
7506f491
DE
1227 insn = NEXT_INSN (insn);
1228 }
1229}
1230\f
1231/* Hash table support. */
1232
b86ba9c8
GK
1233#define NEVER_SET -1
1234
7506f491 1235/* For each register, the cuid of the first/last insn in the block to set it,
e7d99f1e 1236 or -1 if not set. */
7506f491
DE
1237static int *reg_first_set;
1238static int *reg_last_set;
1239
1240/* While computing "first/last set" info, this is the CUID of first/last insn
e7d99f1e 1241 to set memory or -1 if not set. `mem_last_set' is also used when
7506f491
DE
1242 performing GCSE to record whether memory has been set since the beginning
1243 of the block.
1244 Note that handling of memory is very simple, we don't make any attempt
1245 to optimize things (later). */
1246static int mem_first_set;
1247static int mem_last_set;
1248
7506f491
DE
1249/* Perform a quick check whether X, the source of a set, is something
1250 we want to consider for GCSE. */
1251
1252static int
1253want_to_gcse_p (x)
1254 rtx x;
1255{
1256 enum rtx_code code = GET_CODE (x);
1257
1258 switch (code)
1259 {
1260 case REG:
1261 case SUBREG:
1262 case CONST_INT:
1263 case CONST_DOUBLE:
1264 case CALL:
1265 return 0;
1266
1267 default:
1268 break;
1269 }
1270
1271 return 1;
1272}
1273
1274/* Return non-zero if the operands of expression X are unchanged from the
1275 start of INSN's basic block up to but not including INSN (if AVAIL_P == 0),
1276 or from INSN to the end of INSN's basic block (if AVAIL_P != 0). */
1277
1278static int
1279oprs_unchanged_p (x, insn, avail_p)
1280 rtx x, insn;
1281 int avail_p;
1282{
1283 int i;
1284 enum rtx_code code;
6f7d635c 1285 const char *fmt;
7506f491
DE
1286
1287 /* repeat is used to turn tail-recursion into iteration. */
1288 repeat:
1289
1290 if (x == 0)
1291 return 1;
1292
1293 code = GET_CODE (x);
1294 switch (code)
1295 {
1296 case REG:
1297 if (avail_p)
b86ba9c8 1298 return (reg_last_set[REGNO (x)] == NEVER_SET
7506f491
DE
1299 || reg_last_set[REGNO (x)] < INSN_CUID (insn));
1300 else
b86ba9c8 1301 return (reg_first_set[REGNO (x)] == NEVER_SET
7506f491
DE
1302 || reg_first_set[REGNO (x)] >= INSN_CUID (insn));
1303
1304 case MEM:
1305 if (avail_p)
1306 {
b86ba9c8 1307 if (mem_last_set != NEVER_SET
7506f491
DE
1308 && mem_last_set >= INSN_CUID (insn))
1309 return 0;
1310 }
1311 else
1312 {
b86ba9c8 1313 if (mem_first_set != NEVER_SET
7506f491
DE
1314 && mem_first_set < INSN_CUID (insn))
1315 return 0;
1316 }
1317 x = XEXP (x, 0);
1318 goto repeat;
1319
1320 case PRE_DEC:
1321 case PRE_INC:
1322 case POST_DEC:
1323 case POST_INC:
1324 return 0;
1325
1326 case PC:
1327 case CC0: /*FIXME*/
1328 case CONST:
1329 case CONST_INT:
1330 case CONST_DOUBLE:
1331 case SYMBOL_REF:
1332 case LABEL_REF:
1333 case ADDR_VEC:
1334 case ADDR_DIFF_VEC:
1335 return 1;
1336
1337 default:
1338 break;
1339 }
1340
1341 i = GET_RTX_LENGTH (code) - 1;
1342 fmt = GET_RTX_FORMAT (code);
1343 for (; i >= 0; i--)
1344 {
1345 if (fmt[i] == 'e')
1346 {
1347 rtx tem = XEXP (x, i);
1348
1349 /* If we are about to do the last recursive call
1350 needed at this level, change it into iteration.
1351 This function is called enough to be worth it. */
1352 if (i == 0)
1353 {
1354 x = tem;
1355 goto repeat;
1356 }
1357 if (! oprs_unchanged_p (tem, insn, avail_p))
1358 return 0;
1359 }
1360 else if (fmt[i] == 'E')
1361 {
1362 int j;
1363 for (j = 0; j < XVECLEN (x, i); j++)
1364 {
1365 if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, avail_p))
1366 return 0;
1367 }
1368 }
1369 }
1370
1371 return 1;
1372}
1373
1374/* Return non-zero if the operands of expression X are unchanged from
1375 the start of INSN's basic block up to but not including INSN. */
1376
1377static int
1378oprs_anticipatable_p (x, insn)
1379 rtx x, insn;
1380{
1381 return oprs_unchanged_p (x, insn, 0);
1382}
1383
1384/* Return non-zero if the operands of expression X are unchanged from
1385 INSN to the end of INSN's basic block. */
1386
1387static int
1388oprs_available_p (x, insn)
1389 rtx x, insn;
1390{
1391 return oprs_unchanged_p (x, insn, 1);
1392}
1393
1394/* Hash expression X.
1395 MODE is only used if X is a CONST_INT.
1396 A boolean indicating if a volatile operand is found or if the expression
1397 contains something we don't want to insert in the table is stored in
1398 DO_NOT_RECORD_P.
1399
1400 ??? One might want to merge this with canon_hash. Later. */
1401
1402static unsigned int
1403hash_expr (x, mode, do_not_record_p, hash_table_size)
1404 rtx x;
1405 enum machine_mode mode;
1406 int *do_not_record_p;
1407 int hash_table_size;
1408{
1409 unsigned int hash;
1410
1411 *do_not_record_p = 0;
1412
1413 hash = hash_expr_1 (x, mode, do_not_record_p);
1414 return hash % hash_table_size;
1415}
1416
1417/* Subroutine of hash_expr to do the actual work. */
1418
1419static unsigned int
1420hash_expr_1 (x, mode, do_not_record_p)
1421 rtx x;
1422 enum machine_mode mode;
1423 int *do_not_record_p;
1424{
1425 int i, j;
1426 unsigned hash = 0;
1427 enum rtx_code code;
6f7d635c 1428 const char *fmt;
7506f491
DE
1429
1430 /* repeat is used to turn tail-recursion into iteration. */
1431 repeat:
1432
1433 if (x == 0)
1434 return hash;
1435
1436 code = GET_CODE (x);
1437 switch (code)
1438 {
1439 case REG:
1440 {
1441 register int regno = REGNO (x);
1442 hash += ((unsigned) REG << 7) + regno;
1443 return hash;
1444 }
1445
1446 case CONST_INT:
1447 {
1448 unsigned HOST_WIDE_INT tem = INTVAL (x);
1449 hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + tem;
1450 return hash;
1451 }
1452
1453 case CONST_DOUBLE:
1454 /* This is like the general case, except that it only counts
1455 the integers representing the constant. */
1456 hash += (unsigned) code + (unsigned) GET_MODE (x);
1457 if (GET_MODE (x) != VOIDmode)
1458 for (i = 2; i < GET_RTX_LENGTH (CONST_DOUBLE); i++)
1459 {
8a34409d 1460 unsigned tem = XWINT (x, i);
7506f491
DE
1461 hash += tem;
1462 }
1463 else
1464 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1465 + (unsigned) CONST_DOUBLE_HIGH (x));
1466 return hash;
1467
1468 /* Assume there is only one rtx object for any given label. */
1469 case LABEL_REF:
1470 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1471 differences and differences between each stage's debugging dumps. */
1472 hash += ((unsigned) LABEL_REF << 7) + CODE_LABEL_NUMBER (XEXP (x, 0));
1473 return hash;
1474
1475 case SYMBOL_REF:
1476 {
1477 /* Don't hash on the symbol's address to avoid bootstrap differences.
1478 Different hash values may cause expressions to be recorded in
1479 different orders and thus different registers to be used in the
1480 final assembler. This also avoids differences in the dump files
1481 between various stages. */
1482 unsigned int h = 0;
1483 unsigned char *p = (unsigned char *) XSTR (x, 0);
1484 while (*p)
1485 h += (h << 7) + *p++; /* ??? revisit */
1486 hash += ((unsigned) SYMBOL_REF << 7) + h;
1487 return hash;
1488 }
1489
1490 case MEM:
1491 if (MEM_VOLATILE_P (x))
1492 {
1493 *do_not_record_p = 1;
1494 return 0;
1495 }
1496 hash += (unsigned) MEM;
297c3335 1497 hash += MEM_ALIAS_SET (x);
7506f491
DE
1498 x = XEXP (x, 0);
1499 goto repeat;
1500
1501 case PRE_DEC:
1502 case PRE_INC:
1503 case POST_DEC:
1504 case POST_INC:
1505 case PC:
1506 case CC0:
1507 case CALL:
1508 case UNSPEC_VOLATILE:
1509 *do_not_record_p = 1;
1510 return 0;
1511
1512 case ASM_OPERANDS:
1513 if (MEM_VOLATILE_P (x))
1514 {
1515 *do_not_record_p = 1;
1516 return 0;
1517 }
1518
1519 default:
1520 break;
1521 }
1522
1523 i = GET_RTX_LENGTH (code) - 1;
1524 hash += (unsigned) code + (unsigned) GET_MODE (x);
1525 fmt = GET_RTX_FORMAT (code);
1526 for (; i >= 0; i--)
1527 {
1528 if (fmt[i] == 'e')
1529 {
1530 rtx tem = XEXP (x, i);
1531
1532 /* If we are about to do the last recursive call
1533 needed at this level, change it into iteration.
1534 This function is called enough to be worth it. */
1535 if (i == 0)
1536 {
1537 x = tem;
1538 goto repeat;
1539 }
1540 hash += hash_expr_1 (tem, 0, do_not_record_p);
1541 if (*do_not_record_p)
1542 return 0;
1543 }
1544 else if (fmt[i] == 'E')
1545 for (j = 0; j < XVECLEN (x, i); j++)
1546 {
1547 hash += hash_expr_1 (XVECEXP (x, i, j), 0, do_not_record_p);
1548 if (*do_not_record_p)
1549 return 0;
1550 }
1551 else if (fmt[i] == 's')
1552 {
1553 register unsigned char *p = (unsigned char *) XSTR (x, i);
1554 if (p)
1555 while (*p)
1556 hash += *p++;
1557 }
1558 else if (fmt[i] == 'i')
1559 {
1560 register unsigned tem = XINT (x, i);
1561 hash += tem;
1562 }
1563 else
1564 abort ();
1565 }
1566
1567 return hash;
1568}
1569
1570/* Hash a set of register REGNO.
1571
1572 Sets are hashed on the register that is set.
1573 This simplifies the PRE copy propagation code.
1574
1575 ??? May need to make things more elaborate. Later, as necessary. */
1576
1577static unsigned int
1578hash_set (regno, hash_table_size)
1579 int regno;
1580 int hash_table_size;
1581{
1582 unsigned int hash;
1583
1584 hash = regno;
1585 return hash % hash_table_size;
1586}
1587
1588/* Return non-zero if exp1 is equivalent to exp2.
1589 ??? Borrowed from cse.c. Might want to remerge with cse.c. Later. */
1590
1591static int
1592expr_equiv_p (x, y)
1593 rtx x, y;
1594{
1595 register int i, j;
1596 register enum rtx_code code;
6f7d635c 1597 register const char *fmt;
7506f491
DE
1598
1599 if (x == y)
1600 return 1;
1601 if (x == 0 || y == 0)
1602 return x == y;
1603
1604 code = GET_CODE (x);
1605 if (code != GET_CODE (y))
1606 return 0;
1607
1608 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
1609 if (GET_MODE (x) != GET_MODE (y))
1610 return 0;
1611
1612 switch (code)
1613 {
1614 case PC:
1615 case CC0:
1616 return x == y;
1617
1618 case CONST_INT:
1619 return INTVAL (x) == INTVAL (y);
1620
1621 case LABEL_REF:
1622 return XEXP (x, 0) == XEXP (y, 0);
1623
1624 case SYMBOL_REF:
1625 return XSTR (x, 0) == XSTR (y, 0);
1626
1627 case REG:
1628 return REGNO (x) == REGNO (y);
1629
297c3335
RH
1630 case MEM:
1631 /* Can't merge two expressions in different alias sets, since we can
1632 decide that the expression is transparent in a block when it isn't,
1633 due to it being set with the different alias set. */
1634 if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
1635 return 0;
1636 break;
1637
7506f491
DE
1638 /* For commutative operations, check both orders. */
1639 case PLUS:
1640 case MULT:
1641 case AND:
1642 case IOR:
1643 case XOR:
1644 case NE:
1645 case EQ:
1646 return ((expr_equiv_p (XEXP (x, 0), XEXP (y, 0))
1647 && expr_equiv_p (XEXP (x, 1), XEXP (y, 1)))
1648 || (expr_equiv_p (XEXP (x, 0), XEXP (y, 1))
1649 && expr_equiv_p (XEXP (x, 1), XEXP (y, 0))));
1650
1651 default:
1652 break;
1653 }
1654
1655 /* Compare the elements. If any pair of corresponding elements
1656 fail to match, return 0 for the whole thing. */
1657
1658 fmt = GET_RTX_FORMAT (code);
1659 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1660 {
1661 switch (fmt[i])
1662 {
1663 case 'e':
1664 if (! expr_equiv_p (XEXP (x, i), XEXP (y, i)))
1665 return 0;
1666 break;
1667
1668 case 'E':
1669 if (XVECLEN (x, i) != XVECLEN (y, i))
1670 return 0;
1671 for (j = 0; j < XVECLEN (x, i); j++)
1672 if (! expr_equiv_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
1673 return 0;
1674 break;
1675
1676 case 's':
1677 if (strcmp (XSTR (x, i), XSTR (y, i)))
1678 return 0;
1679 break;
1680
1681 case 'i':
1682 if (XINT (x, i) != XINT (y, i))
1683 return 0;
1684 break;
1685
1686 case 'w':
1687 if (XWINT (x, i) != XWINT (y, i))
1688 return 0;
1689 break;
1690
1691 case '0':
1692 break;
1693
1694 default:
1695 abort ();
1696 }
1697 }
1698
1699 return 1;
1700}
1701
1702/* Insert expression X in INSN in the hash table.
1703 If it is already present, record it as the last occurrence in INSN's
1704 basic block.
1705
1706 MODE is the mode of the value X is being stored into.
1707 It is only used if X is a CONST_INT.
1708
1709 ANTIC_P is non-zero if X is an anticipatable expression.
1710 AVAIL_P is non-zero if X is an available expression. */
1711
1712static void
1713insert_expr_in_table (x, mode, insn, antic_p, avail_p)
1714 rtx x;
1715 enum machine_mode mode;
1716 rtx insn;
1717 int antic_p, avail_p;
1718{
1719 int found, do_not_record_p;
1720 unsigned int hash;
1721 struct expr *cur_expr, *last_expr = NULL;
1722 struct occr *antic_occr, *avail_occr;
1723 struct occr *last_occr = NULL;
1724
1725 hash = hash_expr (x, mode, &do_not_record_p, expr_hash_table_size);
1726
1727 /* Do not insert expression in table if it contains volatile operands,
1728 or if hash_expr determines the expression is something we don't want
1729 to or can't handle. */
1730 if (do_not_record_p)
1731 return;
1732
1733 cur_expr = expr_hash_table[hash];
1734 found = 0;
1735
1736 while (cur_expr && ! (found = expr_equiv_p (cur_expr->expr, x)))
1737 {
1738 /* If the expression isn't found, save a pointer to the end of
1739 the list. */
1740 last_expr = cur_expr;
1741 cur_expr = cur_expr->next_same_hash;
1742 }
1743
1744 if (! found)
1745 {
1746 cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
1747 bytes_used += sizeof (struct expr);
1748 if (expr_hash_table[hash] == NULL)
1749 {
1750 /* This is the first pattern that hashed to this index. */
1751 expr_hash_table[hash] = cur_expr;
1752 }
1753 else
1754 {
1755 /* Add EXPR to end of this hash chain. */
1756 last_expr->next_same_hash = cur_expr;
1757 }
1758 /* Set the fields of the expr element. */
1759 cur_expr->expr = x;
1760 cur_expr->bitmap_index = n_exprs++;
1761 cur_expr->next_same_hash = NULL;
1762 cur_expr->antic_occr = NULL;
1763 cur_expr->avail_occr = NULL;
1764 }
1765
1766 /* Now record the occurrence(s). */
1767
1768 if (antic_p)
1769 {
1770 antic_occr = cur_expr->antic_occr;
1771
1772 /* Search for another occurrence in the same basic block. */
1773 while (antic_occr && BLOCK_NUM (antic_occr->insn) != BLOCK_NUM (insn))
1774 {
1775 /* If an occurrence isn't found, save a pointer to the end of
1776 the list. */
1777 last_occr = antic_occr;
1778 antic_occr = antic_occr->next;
1779 }
1780
1781 if (antic_occr)
1782 {
1783 /* Found another instance of the expression in the same basic block.
1784 Prefer the currently recorded one. We want the first one in the
1785 block and the block is scanned from start to end. */
1786 ; /* nothing to do */
1787 }
1788 else
1789 {
1790 /* First occurrence of this expression in this basic block. */
1791 antic_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
1792 bytes_used += sizeof (struct occr);
1793 /* First occurrence of this expression in any block? */
1794 if (cur_expr->antic_occr == NULL)
1795 cur_expr->antic_occr = antic_occr;
1796 else
1797 last_occr->next = antic_occr;
1798 antic_occr->insn = insn;
1799 antic_occr->next = NULL;
1800 }
1801 }
1802
1803 if (avail_p)
1804 {
1805 avail_occr = cur_expr->avail_occr;
1806
1807 /* Search for another occurrence in the same basic block. */
1808 while (avail_occr && BLOCK_NUM (avail_occr->insn) != BLOCK_NUM (insn))
1809 {
1810 /* If an occurrence isn't found, save a pointer to the end of
1811 the list. */
1812 last_occr = avail_occr;
1813 avail_occr = avail_occr->next;
1814 }
1815
1816 if (avail_occr)
1817 {
1818 /* Found another instance of the expression in the same basic block.
1819 Prefer this occurrence to the currently recorded one. We want
1820 the last one in the block and the block is scanned from start
1821 to end. */
1822 avail_occr->insn = insn;
1823 }
1824 else
1825 {
1826 /* First occurrence of this expression in this basic block. */
1827 avail_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
1828 bytes_used += sizeof (struct occr);
1829 /* First occurrence of this expression in any block? */
1830 if (cur_expr->avail_occr == NULL)
1831 cur_expr->avail_occr = avail_occr;
1832 else
1833 last_occr->next = avail_occr;
1834 avail_occr->insn = insn;
1835 avail_occr->next = NULL;
1836 }
1837 }
1838}
1839
1840/* Insert pattern X in INSN in the hash table.
1841 X is a SET of a reg to either another reg or a constant.
1842 If it is already present, record it as the last occurrence in INSN's
1843 basic block. */
1844
1845static void
1846insert_set_in_table (x, insn)
1847 rtx x;
1848 rtx insn;
1849{
1850 int found;
1851 unsigned int hash;
1852 struct expr *cur_expr, *last_expr = NULL;
1853 struct occr *cur_occr, *last_occr = NULL;
1854
1855 if (GET_CODE (x) != SET
1856 || GET_CODE (SET_DEST (x)) != REG)
1857 abort ();
1858
1859 hash = hash_set (REGNO (SET_DEST (x)), set_hash_table_size);
1860
1861 cur_expr = set_hash_table[hash];
1862 found = 0;
1863
1864 while (cur_expr && ! (found = expr_equiv_p (cur_expr->expr, x)))
1865 {
1866 /* If the expression isn't found, save a pointer to the end of
1867 the list. */
1868 last_expr = cur_expr;
1869 cur_expr = cur_expr->next_same_hash;
1870 }
1871
1872 if (! found)
1873 {
1874 cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
1875 bytes_used += sizeof (struct expr);
1876 if (set_hash_table[hash] == NULL)
1877 {
1878 /* This is the first pattern that hashed to this index. */
1879 set_hash_table[hash] = cur_expr;
1880 }
1881 else
1882 {
1883 /* Add EXPR to end of this hash chain. */
1884 last_expr->next_same_hash = cur_expr;
1885 }
1886 /* Set the fields of the expr element.
1887 We must copy X because it can be modified when copy propagation is
1888 performed on its operands. */
1889 /* ??? Should this go in a different obstack? */
1890 cur_expr->expr = copy_rtx (x);
1891 cur_expr->bitmap_index = n_sets++;
1892 cur_expr->next_same_hash = NULL;
1893 cur_expr->antic_occr = NULL;
1894 cur_expr->avail_occr = NULL;
1895 }
1896
1897 /* Now record the occurrence. */
1898
1899 cur_occr = cur_expr->avail_occr;
1900
1901 /* Search for another occurrence in the same basic block. */
1902 while (cur_occr && BLOCK_NUM (cur_occr->insn) != BLOCK_NUM (insn))
1903 {
1904 /* If an occurrence isn't found, save a pointer to the end of
1905 the list. */
1906 last_occr = cur_occr;
1907 cur_occr = cur_occr->next;
1908 }
1909
1910 if (cur_occr)
1911 {
1912 /* Found another instance of the expression in the same basic block.
1913 Prefer this occurrence to the currently recorded one. We want
1914 the last one in the block and the block is scanned from start
1915 to end. */
1916 cur_occr->insn = insn;
1917 }
1918 else
1919 {
1920 /* First occurrence of this expression in this basic block. */
1921 cur_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
1922 bytes_used += sizeof (struct occr);
1923 /* First occurrence of this expression in any block? */
1924 if (cur_expr->avail_occr == NULL)
1925 cur_expr->avail_occr = cur_occr;
1926 else
1927 last_occr->next = cur_occr;
1928 cur_occr->insn = insn;
1929 cur_occr->next = NULL;
1930 }
1931}
1932
1933/* Scan pattern PAT of INSN and add an entry to the hash table.
1934 If SET_P is non-zero, this is for the assignment hash table,
1935 otherwise it is for the expression hash table. */
1936
1937static void
1938hash_scan_set (pat, insn, set_p)
1939 rtx pat, insn;
1940 int set_p;
1941{
1942 rtx src = SET_SRC (pat);
1943 rtx dest = SET_DEST (pat);
1944
1945 if (GET_CODE (src) == CALL)
1946 hash_scan_call (src, insn);
1947
1948 if (GET_CODE (dest) == REG)
1949 {
1950 int regno = REGNO (dest);
1951 rtx tmp;
1952
1953 /* Only record sets of pseudo-regs in the hash table. */
1954 if (! set_p
1955 && regno >= FIRST_PSEUDO_REGISTER
1956 /* Don't GCSE something if we can't do a reg/reg copy. */
1957 && can_copy_p [GET_MODE (dest)]
1958 /* Is SET_SRC something we want to gcse? */
1959 && want_to_gcse_p (src))
1960 {
1961 /* An expression is not anticipatable if its operands are
1962 modified before this insn. */
3cce638b 1963 int antic_p = oprs_anticipatable_p (src, insn);
7506f491
DE
1964 /* An expression is not available if its operands are
1965 subsequently modified, including this insn. */
1966 int avail_p = oprs_available_p (src, insn);
1967 insert_expr_in_table (src, GET_MODE (dest), insn, antic_p, avail_p);
1968 }
1969 /* Record sets for constant/copy propagation. */
1970 else if (set_p
1971 && regno >= FIRST_PSEUDO_REGISTER
1972 && ((GET_CODE (src) == REG
1973 && REGNO (src) >= FIRST_PSEUDO_REGISTER
1974 && can_copy_p [GET_MODE (dest)])
e78d9500 1975 || GET_CODE (src) == CONST_INT
05f6f07c 1976 || GET_CODE (src) == SYMBOL_REF
e78d9500 1977 || GET_CODE (src) == CONST_DOUBLE)
7506f491
DE
1978 /* A copy is not available if its src or dest is subsequently
1979 modified. Here we want to search from INSN+1 on, but
1980 oprs_available_p searches from INSN on. */
1981 && (insn == BLOCK_END (BLOCK_NUM (insn))
1982 || ((tmp = next_nonnote_insn (insn)) != NULL_RTX
1983 && oprs_available_p (pat, tmp))))
1984 insert_set_in_table (pat, insn);
1985 }
7506f491
DE
1986}
1987
1988static void
1989hash_scan_clobber (x, insn)
50b2596f 1990 rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
7506f491
DE
1991{
1992 /* Currently nothing to do. */
1993}
1994
1995static void
1996hash_scan_call (x, insn)
50b2596f 1997 rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
7506f491
DE
1998{
1999 /* Currently nothing to do. */
2000}
2001
2002/* Process INSN and add hash table entries as appropriate.
2003
2004 Only available expressions that set a single pseudo-reg are recorded.
2005
2006 Single sets in a PARALLEL could be handled, but it's an extra complication
2007 that isn't dealt with right now. The trick is handling the CLOBBERs that
2008 are also in the PARALLEL. Later.
2009
2010 If SET_P is non-zero, this is for the assignment hash table,
ed79bb3d
R
2011 otherwise it is for the expression hash table.
2012 If IN_LIBCALL_BLOCK nonzero, we are in a libcall block, and should
2013 not record any expressions. */
7506f491
DE
2014
2015static void
ed79bb3d 2016hash_scan_insn (insn, set_p, in_libcall_block)
7506f491
DE
2017 rtx insn;
2018 int set_p;
48e87cef 2019 int in_libcall_block;
7506f491
DE
2020{
2021 rtx pat = PATTERN (insn);
2022
2023 /* Pick out the sets of INSN and for other forms of instructions record
2024 what's been modified. */
2025
ed79bb3d 2026 if (GET_CODE (pat) == SET && ! in_libcall_block)
21e3a717
BS
2027 {
2028 /* Ignore obvious no-ops. */
2029 if (SET_SRC (pat) != SET_DEST (pat))
2030 hash_scan_set (pat, insn, set_p);
2031 }
7506f491
DE
2032 else if (GET_CODE (pat) == PARALLEL)
2033 {
2034 int i;
2035
2036 for (i = 0; i < XVECLEN (pat, 0); i++)
2037 {
2038 rtx x = XVECEXP (pat, 0, i);
2039
2040 if (GET_CODE (x) == SET)
2041 {
2042 if (GET_CODE (SET_SRC (x)) == CALL)
2043 hash_scan_call (SET_SRC (x), insn);
7506f491
DE
2044 }
2045 else if (GET_CODE (x) == CLOBBER)
2046 hash_scan_clobber (x, insn);
2047 else if (GET_CODE (x) == CALL)
2048 hash_scan_call (x, insn);
2049 }
2050 }
2051 else if (GET_CODE (pat) == CLOBBER)
2052 hash_scan_clobber (pat, insn);
2053 else if (GET_CODE (pat) == CALL)
2054 hash_scan_call (pat, insn);
2055}
2056
2057static void
2058dump_hash_table (file, name, table, table_size, total_size)
2059 FILE *file;
dff01034 2060 const char *name;
7506f491
DE
2061 struct expr **table;
2062 int table_size, total_size;
2063{
2064 int i;
2065 /* Flattened out table, so it's printed in proper order. */
4da896b2
MM
2066 struct expr **flat_table;
2067 unsigned int *hash_val;
2068
2069 flat_table
2070 = (struct expr **) xcalloc (total_size, sizeof (struct expr *));
2071 hash_val = (unsigned int *) xmalloc (total_size * sizeof (unsigned int));
7506f491 2072
7506f491
DE
2073 for (i = 0; i < table_size; i++)
2074 {
2075 struct expr *expr;
2076
2077 for (expr = table[i]; expr != NULL; expr = expr->next_same_hash)
2078 {
2079 flat_table[expr->bitmap_index] = expr;
2080 hash_val[expr->bitmap_index] = i;
2081 }
2082 }
2083
2084 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
2085 name, table_size, total_size);
2086
2087 for (i = 0; i < total_size; i++)
2088 {
2089 struct expr *expr = flat_table[i];
2090
2091 fprintf (file, "Index %d (hash value %d)\n ",
2092 expr->bitmap_index, hash_val[i]);
2093 print_rtl (file, expr->expr);
2094 fprintf (file, "\n");
2095 }
2096
2097 fprintf (file, "\n");
4da896b2
MM
2098
2099 /* Clean up. */
2100 free (flat_table);
2101 free (hash_val);
7506f491
DE
2102}
2103
2104/* Record register first/last/block set information for REGNO in INSN.
2105 reg_first_set records the first place in the block where the register
2106 is set and is used to compute "anticipatability".
2107 reg_last_set records the last place in the block where the register
2108 is set and is used to compute "availability".
2109 reg_set_in_block records whether the register is set in the block
2110 and is used to compute "transparency". */
2111
2112static void
2113record_last_reg_set_info (insn, regno)
2114 rtx insn;
2115 int regno;
2116{
b86ba9c8 2117 if (reg_first_set[regno] == NEVER_SET)
7506f491
DE
2118 reg_first_set[regno] = INSN_CUID (insn);
2119 reg_last_set[regno] = INSN_CUID (insn);
2120 SET_BIT (reg_set_in_block[BLOCK_NUM (insn)], regno);
2121}
2122
2123/* Record memory first/last/block set information for INSN. */
2124
2125static void
2126record_last_mem_set_info (insn)
2127 rtx insn;
2128{
b86ba9c8 2129 if (mem_first_set == NEVER_SET)
7506f491
DE
2130 mem_first_set = INSN_CUID (insn);
2131 mem_last_set = INSN_CUID (insn);
2132 mem_set_in_block[BLOCK_NUM (insn)] = 1;
2133}
2134
7506f491 2135/* Called from compute_hash_table via note_stores to handle one
84832317
MM
2136 SET or CLOBBER in an insn. DATA is really the instruction in which
2137 the SET is taking place. */
7506f491
DE
2138
2139static void
84832317 2140record_last_set_info (dest, setter, data)
50b2596f 2141 rtx dest, setter ATTRIBUTE_UNUSED;
84832317 2142 void *data;
7506f491 2143{
84832317
MM
2144 rtx last_set_insn = (rtx) data;
2145
7506f491
DE
2146 if (GET_CODE (dest) == SUBREG)
2147 dest = SUBREG_REG (dest);
2148
2149 if (GET_CODE (dest) == REG)
2150 record_last_reg_set_info (last_set_insn, REGNO (dest));
2151 else if (GET_CODE (dest) == MEM
2152 /* Ignore pushes, they clobber nothing. */
2153 && ! push_operand (dest, GET_MODE (dest)))
2154 record_last_mem_set_info (last_set_insn);
2155}
2156
2157/* Top level function to create an expression or assignment hash table.
2158
2159 Expression entries are placed in the hash table if
2160 - they are of the form (set (pseudo-reg) src),
2161 - src is something we want to perform GCSE on,
2162 - none of the operands are subsequently modified in the block
2163
2164 Assignment entries are placed in the hash table if
2165 - they are of the form (set (pseudo-reg) src),
2166 - src is something we want to perform const/copy propagation on,
2167 - none of the operands or target are subsequently modified in the block
2168 Currently src must be a pseudo-reg or a const_int.
2169
2170 F is the first insn.
2171 SET_P is non-zero for computing the assignment hash table. */
2172
2173static void
b5ce41ff 2174compute_hash_table (set_p)
7506f491
DE
2175 int set_p;
2176{
2177 int bb;
2178
2179 /* While we compute the hash table we also compute a bit array of which
2180 registers are set in which blocks.
2181 We also compute which blocks set memory, in the absence of aliasing
2182 support [which is TODO].
2183 ??? This isn't needed during const/copy propagation, but it's cheap to
2184 compute. Later. */
2185 sbitmap_vector_zero (reg_set_in_block, n_basic_blocks);
2186 bzero ((char *) mem_set_in_block, n_basic_blocks);
2187
2188 /* Some working arrays used to track first and last set in each block. */
2189 /* ??? One could use alloca here, but at some size a threshold is crossed
2190 beyond which one should use malloc. Are we at that threshold here? */
2191 reg_first_set = (int *) gmalloc (max_gcse_regno * sizeof (int));
2192 reg_last_set = (int *) gmalloc (max_gcse_regno * sizeof (int));
2193
2194 for (bb = 0; bb < n_basic_blocks; bb++)
2195 {
2196 rtx insn;
2197 int regno;
ed79bb3d 2198 int in_libcall_block;
b86ba9c8 2199 int i;
7506f491
DE
2200
2201 /* First pass over the instructions records information used to
2202 determine when registers and memory are first and last set.
2203 ??? The mem_set_in_block and hard-reg reg_set_in_block computation
2204 could be moved to compute_sets since they currently don't change. */
2205
b86ba9c8
GK
2206 for (i = 0; i < max_gcse_regno; i++)
2207 reg_first_set[i] = reg_last_set[i] = NEVER_SET;
2208 mem_first_set = NEVER_SET;
2209 mem_last_set = NEVER_SET;
7506f491 2210
3b413743
RH
2211 for (insn = BLOCK_HEAD (bb);
2212 insn && insn != NEXT_INSN (BLOCK_END (bb));
7506f491
DE
2213 insn = NEXT_INSN (insn))
2214 {
2215#ifdef NON_SAVING_SETJMP
2216 if (NON_SAVING_SETJMP && GET_CODE (insn) == NOTE
2217 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
2218 {
2219 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2220 record_last_reg_set_info (insn, regno);
2221 continue;
2222 }
2223#endif
2224
2225 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
2226 continue;
2227
2228 if (GET_CODE (insn) == CALL_INSN)
2229 {
2230 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
15f8470f
JL
2231 if ((call_used_regs[regno]
2232 && regno != STACK_POINTER_REGNUM
2233#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2234 && regno != HARD_FRAME_POINTER_REGNUM
2235#endif
2236#if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2237 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2238#endif
2239#if defined (PIC_OFFSET_TABLE_REGNUM) && !defined (PIC_OFFSET_TABLE_REG_CALL_CLOBBERED)
2240 && ! (regno == PIC_OFFSET_TABLE_REGNUM && flag_pic)
2241#endif
2242
2243 && regno != FRAME_POINTER_REGNUM)
2244 || global_regs[regno])
7506f491
DE
2245 record_last_reg_set_info (insn, regno);
2246 if (! CONST_CALL_P (insn))
2247 record_last_mem_set_info (insn);
2248 }
2249
84832317 2250 note_stores (PATTERN (insn), record_last_set_info, insn);
7506f491
DE
2251 }
2252
2253 /* The next pass builds the hash table. */
2254
3b413743
RH
2255 for (insn = BLOCK_HEAD (bb), in_libcall_block = 0;
2256 insn && insn != NEXT_INSN (BLOCK_END (bb));
7506f491
DE
2257 insn = NEXT_INSN (insn))
2258 {
2259 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
ed79bb3d
R
2260 {
2261 if (find_reg_note (insn, REG_LIBCALL, NULL_RTX))
2262 in_libcall_block = 1;
2263 else if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
2264 in_libcall_block = 0;
2265 hash_scan_insn (insn, set_p, in_libcall_block);
2266 }
7506f491
DE
2267 }
2268 }
2269
2270 free (reg_first_set);
2271 free (reg_last_set);
2272 /* Catch bugs early. */
2273 reg_first_set = reg_last_set = 0;
2274}
2275
2276/* Allocate space for the set hash table.
2277 N_INSNS is the number of instructions in the function.
2278 It is used to determine the number of buckets to use. */
2279
2280static void
2281alloc_set_hash_table (n_insns)
2282 int n_insns;
2283{
2284 int n;
2285
2286 set_hash_table_size = n_insns / 4;
2287 if (set_hash_table_size < 11)
2288 set_hash_table_size = 11;
2289 /* Attempt to maintain efficient use of hash table.
2290 Making it an odd number is simplest for now.
2291 ??? Later take some measurements. */
2292 set_hash_table_size |= 1;
2293 n = set_hash_table_size * sizeof (struct expr *);
2294 set_hash_table = (struct expr **) gmalloc (n);
2295}
2296
2297/* Free things allocated by alloc_set_hash_table. */
2298
2299static void
2300free_set_hash_table ()
2301{
2302 free (set_hash_table);
2303}
2304
2305/* Compute the hash table for doing copy/const propagation. */
2306
2307static void
b5ce41ff 2308compute_set_hash_table ()
7506f491
DE
2309{
2310 /* Initialize count of number of entries in hash table. */
2311 n_sets = 0;
2312 bzero ((char *) set_hash_table, set_hash_table_size * sizeof (struct expr *));
2313
b5ce41ff 2314 compute_hash_table (1);
7506f491
DE
2315}
2316
2317/* Allocate space for the expression hash table.
2318 N_INSNS is the number of instructions in the function.
2319 It is used to determine the number of buckets to use. */
2320
2321static void
2322alloc_expr_hash_table (n_insns)
2323 int n_insns;
2324{
2325 int n;
2326
2327 expr_hash_table_size = n_insns / 2;
2328 /* Make sure the amount is usable. */
2329 if (expr_hash_table_size < 11)
2330 expr_hash_table_size = 11;
2331 /* Attempt to maintain efficient use of hash table.
2332 Making it an odd number is simplest for now.
2333 ??? Later take some measurements. */
2334 expr_hash_table_size |= 1;
2335 n = expr_hash_table_size * sizeof (struct expr *);
2336 expr_hash_table = (struct expr **) gmalloc (n);
2337}
2338
2339/* Free things allocated by alloc_expr_hash_table. */
2340
2341static void
2342free_expr_hash_table ()
2343{
2344 free (expr_hash_table);
2345}
2346
2347/* Compute the hash table for doing GCSE. */
2348
2349static void
b5ce41ff 2350compute_expr_hash_table ()
7506f491
DE
2351{
2352 /* Initialize count of number of entries in hash table. */
2353 n_exprs = 0;
2354 bzero ((char *) expr_hash_table, expr_hash_table_size * sizeof (struct expr *));
2355
b5ce41ff 2356 compute_hash_table (0);
7506f491
DE
2357}
2358\f
2359/* Expression tracking support. */
2360
2361/* Lookup pattern PAT in the expression table.
2362 The result is a pointer to the table entry, or NULL if not found. */
2363
2364static struct expr *
2365lookup_expr (pat)
2366 rtx pat;
2367{
2368 int do_not_record_p;
2369 unsigned int hash = hash_expr (pat, GET_MODE (pat), &do_not_record_p,
2370 expr_hash_table_size);
2371 struct expr *expr;
2372
2373 if (do_not_record_p)
2374 return NULL;
2375
2376 expr = expr_hash_table[hash];
2377
2378 while (expr && ! expr_equiv_p (expr->expr, pat))
2379 expr = expr->next_same_hash;
2380
2381 return expr;
2382}
2383
2384/* Lookup REGNO in the set table.
2385 If PAT is non-NULL look for the entry that matches it, otherwise return
2386 the first entry for REGNO.
2387 The result is a pointer to the table entry, or NULL if not found. */
2388
2389static struct expr *
2390lookup_set (regno, pat)
2391 int regno;
2392 rtx pat;
2393{
2394 unsigned int hash = hash_set (regno, set_hash_table_size);
2395 struct expr *expr;
2396
2397 expr = set_hash_table[hash];
2398
2399 if (pat)
2400 {
2401 while (expr && ! expr_equiv_p (expr->expr, pat))
2402 expr = expr->next_same_hash;
2403 }
2404 else
2405 {
2406 while (expr && REGNO (SET_DEST (expr->expr)) != regno)
2407 expr = expr->next_same_hash;
2408 }
2409
2410 return expr;
2411}
2412
2413/* Return the next entry for REGNO in list EXPR. */
2414
2415static struct expr *
2416next_set (regno, expr)
2417 int regno;
2418 struct expr *expr;
2419{
2420 do
2421 expr = expr->next_same_hash;
2422 while (expr && REGNO (SET_DEST (expr->expr)) != regno);
2423 return expr;
2424}
2425
2426/* Reset tables used to keep track of what's still available [since the
2427 start of the block]. */
2428
2429static void
2430reset_opr_set_tables ()
2431{
2432 /* Maintain a bitmap of which regs have been set since beginning of
2433 the block. */
2434 sbitmap_zero (reg_set_bitmap);
2435 /* Also keep a record of the last instruction to modify memory.
2436 For now this is very trivial, we only record whether any memory
2437 location has been modified. */
2438 mem_last_set = 0;
2439}
2440
2441/* Return non-zero if the operands of X are not set before INSN in
2442 INSN's basic block. */
2443
2444static int
2445oprs_not_set_p (x, insn)
2446 rtx x, insn;
2447{
2448 int i;
2449 enum rtx_code code;
6f7d635c 2450 const char *fmt;
7506f491
DE
2451
2452 /* repeat is used to turn tail-recursion into iteration. */
2453repeat:
2454
2455 if (x == 0)
2456 return 1;
2457
2458 code = GET_CODE (x);
2459 switch (code)
2460 {
2461 case PC:
2462 case CC0:
2463 case CONST:
2464 case CONST_INT:
2465 case CONST_DOUBLE:
2466 case SYMBOL_REF:
2467 case LABEL_REF:
2468 case ADDR_VEC:
2469 case ADDR_DIFF_VEC:
2470 return 1;
2471
2472 case MEM:
2473 if (mem_last_set != 0)
2474 return 0;
2475 x = XEXP (x, 0);
2476 goto repeat;
2477
2478 case REG:
2479 return ! TEST_BIT (reg_set_bitmap, REGNO (x));
2480
2481 default:
2482 break;
2483 }
2484
2485 fmt = GET_RTX_FORMAT (code);
2486 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2487 {
2488 if (fmt[i] == 'e')
2489 {
2490 int not_set_p;
2491 /* If we are about to do the last recursive call
2492 needed at this level, change it into iteration.
2493 This function is called enough to be worth it. */
2494 if (i == 0)
2495 {
2496 x = XEXP (x, 0);
2497 goto repeat;
2498 }
2499 not_set_p = oprs_not_set_p (XEXP (x, i), insn);
2500 if (! not_set_p)
2501 return 0;
2502 }
2503 else if (fmt[i] == 'E')
2504 {
2505 int j;
2506 for (j = 0; j < XVECLEN (x, i); j++)
2507 {
2508 int not_set_p = oprs_not_set_p (XVECEXP (x, i, j), insn);
2509 if (! not_set_p)
2510 return 0;
2511 }
2512 }
2513 }
2514
2515 return 1;
2516}
2517
2518/* Mark things set by a CALL. */
2519
2520static void
b5ce41ff
JL
2521mark_call (insn)
2522 rtx insn;
7506f491
DE
2523{
2524 mem_last_set = INSN_CUID (insn);
2525}
2526
2527/* Mark things set by a SET. */
2528
2529static void
2530mark_set (pat, insn)
2531 rtx pat, insn;
2532{
2533 rtx dest = SET_DEST (pat);
2534
2535 while (GET_CODE (dest) == SUBREG
2536 || GET_CODE (dest) == ZERO_EXTRACT
2537 || GET_CODE (dest) == SIGN_EXTRACT
2538 || GET_CODE (dest) == STRICT_LOW_PART)
2539 dest = XEXP (dest, 0);
2540
2541 if (GET_CODE (dest) == REG)
2542 SET_BIT (reg_set_bitmap, REGNO (dest));
2543 else if (GET_CODE (dest) == MEM)
2544 mem_last_set = INSN_CUID (insn);
2545
2546 if (GET_CODE (SET_SRC (pat)) == CALL)
b5ce41ff 2547 mark_call (insn);
7506f491
DE
2548}
2549
2550/* Record things set by a CLOBBER. */
2551
2552static void
2553mark_clobber (pat, insn)
2554 rtx pat, insn;
2555{
2556 rtx clob = XEXP (pat, 0);
2557
2558 while (GET_CODE (clob) == SUBREG || GET_CODE (clob) == STRICT_LOW_PART)
2559 clob = XEXP (clob, 0);
2560
2561 if (GET_CODE (clob) == REG)
2562 SET_BIT (reg_set_bitmap, REGNO (clob));
2563 else
2564 mem_last_set = INSN_CUID (insn);
2565}
2566
2567/* Record things set by INSN.
2568 This data is used by oprs_not_set_p. */
2569
2570static void
2571mark_oprs_set (insn)
2572 rtx insn;
2573{
2574 rtx pat = PATTERN (insn);
2575
2576 if (GET_CODE (pat) == SET)
2577 mark_set (pat, insn);
2578 else if (GET_CODE (pat) == PARALLEL)
2579 {
2580 int i;
2581
2582 for (i = 0; i < XVECLEN (pat, 0); i++)
2583 {
2584 rtx x = XVECEXP (pat, 0, i);
2585
2586 if (GET_CODE (x) == SET)
2587 mark_set (x, insn);
2588 else if (GET_CODE (x) == CLOBBER)
2589 mark_clobber (x, insn);
2590 else if (GET_CODE (x) == CALL)
b5ce41ff 2591 mark_call (insn);
7506f491
DE
2592 }
2593 }
2594 else if (GET_CODE (pat) == CLOBBER)
2595 mark_clobber (pat, insn);
2596 else if (GET_CODE (pat) == CALL)
b5ce41ff 2597 mark_call (insn);
7506f491 2598}
b5ce41ff 2599
7506f491
DE
2600\f
2601/* Classic GCSE reaching definition support. */
2602
2603/* Allocate reaching def variables. */
2604
2605static void
2606alloc_rd_mem (n_blocks, n_insns)
2607 int n_blocks, n_insns;
2608{
2609 rd_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2610 sbitmap_vector_zero (rd_kill, n_basic_blocks);
2611
2612 rd_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2613 sbitmap_vector_zero (rd_gen, n_basic_blocks);
2614
2615 reaching_defs = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2616 sbitmap_vector_zero (reaching_defs, n_basic_blocks);
2617
2618 rd_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2619 sbitmap_vector_zero (rd_out, n_basic_blocks);
2620}
2621
2622/* Free reaching def variables. */
2623
2624static void
2625free_rd_mem ()
2626{
2627 free (rd_kill);
2628 free (rd_gen);
2629 free (reaching_defs);
2630 free (rd_out);
2631}
2632
2633/* Add INSN to the kills of BB.
2634 REGNO, set in BB, is killed by INSN. */
2635
2636static void
2637handle_rd_kill_set (insn, regno, bb)
2638 rtx insn;
2639 int regno, bb;
2640{
2641 struct reg_set *this_reg = reg_set_table[regno];
2642
2643 while (this_reg)
2644 {
2645 if (BLOCK_NUM (this_reg->insn) != BLOCK_NUM (insn))
2646 SET_BIT (rd_kill[bb], INSN_CUID (this_reg->insn));
2647 this_reg = this_reg->next;
2648 }
2649}
2650
7506f491
DE
2651/* Compute the set of kill's for reaching definitions. */
2652
2653static void
2654compute_kill_rd ()
2655{
2656 int bb,cuid;
2657
2658 /* For each block
2659 For each set bit in `gen' of the block (i.e each insn which
ac7c5af5
JL
2660 generates a definition in the block)
2661 Call the reg set by the insn corresponding to that bit regx
2662 Look at the linked list starting at reg_set_table[regx]
2663 For each setting of regx in the linked list, which is not in
2664 this block
2665 Set the bit in `kill' corresponding to that insn
7506f491
DE
2666 */
2667
2668 for (bb = 0; bb < n_basic_blocks; bb++)
2669 {
2670 for (cuid = 0; cuid < max_cuid; cuid++)
2671 {
2672 if (TEST_BIT (rd_gen[bb], cuid))
ac7c5af5 2673 {
7506f491
DE
2674 rtx insn = CUID_INSN (cuid);
2675 rtx pat = PATTERN (insn);
2676
2677 if (GET_CODE (insn) == CALL_INSN)
ac7c5af5 2678 {
7506f491
DE
2679 int regno;
2680
2681 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
ac7c5af5 2682 {
15f8470f
JL
2683 if ((call_used_regs[regno]
2684 && regno != STACK_POINTER_REGNUM
2685#if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2686 && regno != HARD_FRAME_POINTER_REGNUM
2687#endif
2688#if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2689 && ! (regno == ARG_POINTER_REGNUM
2690 && fixed_regs[regno])
2691#endif
2692#if defined (PIC_OFFSET_TABLE_REGNUM) && !defined (PIC_OFFSET_TABLE_REG_CALL_CLOBBERED)
2693 && ! (regno == PIC_OFFSET_TABLE_REGNUM && flag_pic)
2694#endif
2695 && regno != FRAME_POINTER_REGNUM)
2696 || global_regs[regno])
7506f491 2697 handle_rd_kill_set (insn, regno, bb);
ac7c5af5
JL
2698 }
2699 }
7506f491
DE
2700
2701 if (GET_CODE (pat) == PARALLEL)
2702 {
2703 int i;
2704
2705 /* We work backwards because ... */
2706 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
2707 {
2708 enum rtx_code code = GET_CODE (XVECEXP (pat, 0, i));
2709 if ((code == SET || code == CLOBBER)
2710 && GET_CODE (XEXP (XVECEXP (pat, 0, i), 0)) == REG)
2711 handle_rd_kill_set (insn,
2712 REGNO (XEXP (XVECEXP (pat, 0, i), 0)),
2713 bb);
2714 }
2715 }
2716 else if (GET_CODE (pat) == SET)
2717 {
2718 if (GET_CODE (SET_DEST (pat)) == REG)
2719 {
2720 /* Each setting of this register outside of this block
2721 must be marked in the set of kills in this block. */
2722 handle_rd_kill_set (insn, REGNO (SET_DEST (pat)), bb);
2723 }
ac7c5af5 2724 }
7506f491 2725 /* FIXME: CLOBBER? */
ac7c5af5 2726 }
7506f491
DE
2727 }
2728 }
2729}
2730
2731/* Compute the reaching definitions as in
2732 Compilers Principles, Techniques, and Tools. Aho, Sethi, Ullman,
2733 Chapter 10. It is the same algorithm as used for computing available
2734 expressions but applied to the gens and kills of reaching definitions. */
2735
2736static void
2737compute_rd ()
2738{
2739 int bb, changed, passes;
2740
2741 for (bb = 0; bb < n_basic_blocks; bb++)
2742 sbitmap_copy (rd_out[bb] /*dst*/, rd_gen[bb] /*src*/);
2743
2744 passes = 0;
2745 changed = 1;
2746 while (changed)
2747 {
2748 changed = 0;
2749 for (bb = 0; bb < n_basic_blocks; bb++)
ac7c5af5 2750 {
36349f8b 2751 sbitmap_union_of_preds (reaching_defs[bb], rd_out, bb);
7506f491
DE
2752 changed |= sbitmap_union_of_diff (rd_out[bb], rd_gen[bb],
2753 reaching_defs[bb], rd_kill[bb]);
ac7c5af5 2754 }
7506f491
DE
2755 passes++;
2756 }
2757
2758 if (gcse_file)
2759 fprintf (gcse_file, "reaching def computation: %d passes\n", passes);
2760}
2761\f
2762/* Classic GCSE available expression support. */
2763
2764/* Allocate memory for available expression computation. */
2765
2766static void
2767alloc_avail_expr_mem (n_blocks, n_exprs)
2768 int n_blocks, n_exprs;
2769{
2770 ae_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2771 sbitmap_vector_zero (ae_kill, n_basic_blocks);
2772
2773 ae_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2774 sbitmap_vector_zero (ae_gen, n_basic_blocks);
2775
2776 ae_in = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2777 sbitmap_vector_zero (ae_in, n_basic_blocks);
2778
2779 ae_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2780 sbitmap_vector_zero (ae_out, n_basic_blocks);
2781
2782 u_bitmap = (sbitmap) sbitmap_alloc (n_exprs);
2783 sbitmap_ones (u_bitmap);
2784}
2785
2786static void
2787free_avail_expr_mem ()
2788{
2789 free (ae_kill);
2790 free (ae_gen);
2791 free (ae_in);
2792 free (ae_out);
2793 free (u_bitmap);
2794}
2795
2796/* Compute the set of available expressions generated in each basic block. */
2797
2798static void
2799compute_ae_gen ()
2800{
2801 int i;
2802
2803 /* For each recorded occurrence of each expression, set ae_gen[bb][expr].
2804 This is all we have to do because an expression is not recorded if it
2805 is not available, and the only expressions we want to work with are the
2806 ones that are recorded. */
2807
2808 for (i = 0; i < expr_hash_table_size; i++)
2809 {
2810 struct expr *expr = expr_hash_table[i];
2811 while (expr != NULL)
2812 {
2813 struct occr *occr = expr->avail_occr;
2814 while (occr != NULL)
2815 {
2816 SET_BIT (ae_gen[BLOCK_NUM (occr->insn)], expr->bitmap_index);
2817 occr = occr->next;
2818 }
2819 expr = expr->next_same_hash;
2820 }
2821 }
2822}
2823
2824/* Return non-zero if expression X is killed in BB. */
2825
2826static int
2827expr_killed_p (x, bb)
2828 rtx x;
2829 int bb;
2830{
2831 int i;
2832 enum rtx_code code;
6f7d635c 2833 const char *fmt;
7506f491
DE
2834
2835 /* repeat is used to turn tail-recursion into iteration. */
2836 repeat:
2837
2838 if (x == 0)
2839 return 1;
2840
2841 code = GET_CODE (x);
2842 switch (code)
2843 {
2844 case REG:
2845 return TEST_BIT (reg_set_in_block[bb], REGNO (x));
2846
2847 case MEM:
2848 if (mem_set_in_block[bb])
2849 return 1;
2850 x = XEXP (x, 0);
2851 goto repeat;
2852
2853 case PC:
2854 case CC0: /*FIXME*/
2855 case CONST:
2856 case CONST_INT:
2857 case CONST_DOUBLE:
2858 case SYMBOL_REF:
2859 case LABEL_REF:
2860 case ADDR_VEC:
2861 case ADDR_DIFF_VEC:
2862 return 0;
2863
2864 default:
2865 break;
2866 }
2867
2868 i = GET_RTX_LENGTH (code) - 1;
2869 fmt = GET_RTX_FORMAT (code);
2870 for (; i >= 0; i--)
2871 {
2872 if (fmt[i] == 'e')
2873 {
2874 rtx tem = XEXP (x, i);
2875
2876 /* If we are about to do the last recursive call
2877 needed at this level, change it into iteration.
2878 This function is called enough to be worth it. */
2879 if (i == 0)
2880 {
2881 x = tem;
2882 goto repeat;
2883 }
2884 if (expr_killed_p (tem, bb))
2885 return 1;
2886 }
2887 else if (fmt[i] == 'E')
2888 {
2889 int j;
2890 for (j = 0; j < XVECLEN (x, i); j++)
2891 {
2892 if (expr_killed_p (XVECEXP (x, i, j), bb))
2893 return 1;
2894 }
2895 }
2896 }
2897
2898 return 0;
2899}
2900
2901/* Compute the set of available expressions killed in each basic block. */
2902
2903static void
a42cd965
AM
2904compute_ae_kill (ae_gen, ae_kill)
2905 sbitmap *ae_gen, *ae_kill;
7506f491
DE
2906{
2907 int bb,i;
2908
2909 for (bb = 0; bb < n_basic_blocks; bb++)
2910 {
2911 for (i = 0; i < expr_hash_table_size; i++)
2912 {
2913 struct expr *expr = expr_hash_table[i];
2914
2915 for ( ; expr != NULL; expr = expr->next_same_hash)
2916 {
2917 /* Skip EXPR if generated in this block. */
2918 if (TEST_BIT (ae_gen[bb], expr->bitmap_index))
2919 continue;
2920
2921 if (expr_killed_p (expr->expr, bb))
2922 SET_BIT (ae_kill[bb], expr->bitmap_index);
2923 }
2924 }
2925 }
2926}
7506f491
DE
2927\f
2928/* Actually perform the Classic GCSE optimizations. */
2929
2930/* Return non-zero if occurrence OCCR of expression EXPR reaches block BB.
2931
2932 CHECK_SELF_LOOP is non-zero if we should consider a block reaching itself
2933 as a positive reach. We want to do this when there are two computations
2934 of the expression in the block.
2935
2936 VISITED is a pointer to a working buffer for tracking which BB's have
2937 been visited. It is NULL for the top-level call.
2938
2939 We treat reaching expressions that go through blocks containing the same
2940 reaching expression as "not reaching". E.g. if EXPR is generated in blocks
2941 2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
2942 2 as not reaching. The intent is to improve the probability of finding
2943 only one reaching expression and to reduce register lifetimes by picking
2944 the closest such expression. */
2945
2946static int
283a2545 2947expr_reaches_here_p_work (occr, expr, bb, check_self_loop, visited)
7506f491
DE
2948 struct occr *occr;
2949 struct expr *expr;
2950 int bb;
2951 int check_self_loop;
2952 char *visited;
2953{
36349f8b 2954 edge pred;
7506f491 2955
36349f8b 2956 for (pred = BASIC_BLOCK(bb)->pred; pred != NULL; pred = pred->pred_next)
7506f491 2957 {
36349f8b 2958 int pred_bb = pred->src->index;
7506f491
DE
2959
2960 if (visited[pred_bb])
ac7c5af5 2961 {
7506f491
DE
2962 /* This predecessor has already been visited.
2963 Nothing to do. */
2964 ;
2965 }
2966 else if (pred_bb == bb)
ac7c5af5 2967 {
7506f491
DE
2968 /* BB loops on itself. */
2969 if (check_self_loop
2970 && TEST_BIT (ae_gen[pred_bb], expr->bitmap_index)
2971 && BLOCK_NUM (occr->insn) == pred_bb)
2972 return 1;
2973 visited[pred_bb] = 1;
ac7c5af5 2974 }
7506f491
DE
2975 /* Ignore this predecessor if it kills the expression. */
2976 else if (TEST_BIT (ae_kill[pred_bb], expr->bitmap_index))
2977 visited[pred_bb] = 1;
2978 /* Does this predecessor generate this expression? */
2979 else if (TEST_BIT (ae_gen[pred_bb], expr->bitmap_index))
2980 {
2981 /* Is this the occurrence we're looking for?
2982 Note that there's only one generating occurrence per block
2983 so we just need to check the block number. */
2984 if (BLOCK_NUM (occr->insn) == pred_bb)
2985 return 1;
2986 visited[pred_bb] = 1;
2987 }
2988 /* Neither gen nor kill. */
2989 else
ac7c5af5 2990 {
7506f491 2991 visited[pred_bb] = 1;
283a2545
RL
2992 if (expr_reaches_here_p_work (occr, expr, pred_bb, check_self_loop,
2993 visited))
7506f491 2994 return 1;
ac7c5af5 2995 }
7506f491
DE
2996 }
2997
2998 /* All paths have been checked. */
2999 return 0;
3000}
3001
283a2545
RL
3002/* This wrapper for expr_reaches_here_p_work() is to ensure that any
3003 memory allocated for that function is returned. */
3004
3005static int
3006expr_reaches_here_p (occr, expr, bb, check_self_loop)
3007 struct occr *occr;
3008 struct expr *expr;
3009 int bb;
3010 int check_self_loop;
3011{
3012 int rval;
3013 char * visited = (char *) xcalloc (n_basic_blocks, 1);
3014
3015 rval = expr_reaches_here_p_work(occr, expr, bb, check_self_loop, visited);
3016
3017 free (visited);
3018
3019 return (rval);
3020}
3021
7506f491
DE
3022/* Return the instruction that computes EXPR that reaches INSN's basic block.
3023 If there is more than one such instruction, return NULL.
3024
3025 Called only by handle_avail_expr. */
3026
3027static rtx
3028computing_insn (expr, insn)
3029 struct expr *expr;
3030 rtx insn;
3031{
3032 int bb = BLOCK_NUM (insn);
3033
3034 if (expr->avail_occr->next == NULL)
3035 {
3036 if (BLOCK_NUM (expr->avail_occr->insn) == bb)
3037 {
3038 /* The available expression is actually itself
3039 (i.e. a loop in the flow graph) so do nothing. */
3040 return NULL;
3041 }
3042 /* (FIXME) Case that we found a pattern that was created by
3043 a substitution that took place. */
3044 return expr->avail_occr->insn;
3045 }
3046 else
3047 {
3048 /* Pattern is computed more than once.
3049 Search backwards from this insn to see how many of these
3050 computations actually reach this insn. */
3051 struct occr *occr;
3052 rtx insn_computes_expr = NULL;
3053 int can_reach = 0;
3054
3055 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
3056 {
3057 if (BLOCK_NUM (occr->insn) == bb)
3058 {
3059 /* The expression is generated in this block.
3060 The only time we care about this is when the expression
3061 is generated later in the block [and thus there's a loop].
3062 We let the normal cse pass handle the other cases. */
3063 if (INSN_CUID (insn) < INSN_CUID (occr->insn))
3064 {
283a2545 3065 if (expr_reaches_here_p (occr, expr, bb, 1))
7506f491
DE
3066 {
3067 can_reach++;
3068 if (can_reach > 1)
3069 return NULL;
3070 insn_computes_expr = occr->insn;
3071 }
3072 }
3073 }
3074 else /* Computation of the pattern outside this block. */
3075 {
283a2545 3076 if (expr_reaches_here_p (occr, expr, bb, 0))
7506f491
DE
3077 {
3078 can_reach++;
3079 if (can_reach > 1)
3080 return NULL;
3081 insn_computes_expr = occr->insn;
3082 }
3083 }
3084 }
3085
3086 if (insn_computes_expr == NULL)
3087 abort ();
3088 return insn_computes_expr;
3089 }
3090}
3091
3092/* Return non-zero if the definition in DEF_INSN can reach INSN.
3093 Only called by can_disregard_other_sets. */
3094
3095static int
3096def_reaches_here_p (insn, def_insn)
3097 rtx insn, def_insn;
3098{
3099 rtx reg;
3100
3101 if (TEST_BIT (reaching_defs[BLOCK_NUM (insn)], INSN_CUID (def_insn)))
3102 return 1;
3103
3104 if (BLOCK_NUM (insn) == BLOCK_NUM (def_insn))
3105 {
3106 if (INSN_CUID (def_insn) < INSN_CUID (insn))
ac7c5af5 3107 {
7506f491
DE
3108 if (GET_CODE (PATTERN (def_insn)) == PARALLEL)
3109 return 1;
3110 if (GET_CODE (PATTERN (def_insn)) == CLOBBER)
3111 reg = XEXP (PATTERN (def_insn), 0);
3112 else if (GET_CODE (PATTERN (def_insn)) == SET)
3113 reg = SET_DEST (PATTERN (def_insn));
3114 else
3115 abort ();
3116 return ! reg_set_between_p (reg, NEXT_INSN (def_insn), insn);
3117 }
3118 else
3119 return 0;
3120 }
3121
3122 return 0;
3123}
3124
3125/* Return non-zero if *ADDR_THIS_REG can only have one value at INSN.
3126 The value returned is the number of definitions that reach INSN.
3127 Returning a value of zero means that [maybe] more than one definition
3128 reaches INSN and the caller can't perform whatever optimization it is
3129 trying. i.e. it is always safe to return zero. */
3130
3131static int
3132can_disregard_other_sets (addr_this_reg, insn, for_combine)
3133 struct reg_set **addr_this_reg;
3134 rtx insn;
3135 int for_combine;
3136{
3137 int number_of_reaching_defs = 0;
3138 struct reg_set *this_reg = *addr_this_reg;
3139
3140 while (this_reg)
3141 {
3142 if (def_reaches_here_p (insn, this_reg->insn))
3143 {
3144 number_of_reaching_defs++;
3145 /* Ignore parallels for now. */
3146 if (GET_CODE (PATTERN (this_reg->insn)) == PARALLEL)
3147 return 0;
3148 if (!for_combine
3149 && (GET_CODE (PATTERN (this_reg->insn)) == CLOBBER
3150 || ! rtx_equal_p (SET_SRC (PATTERN (this_reg->insn)),
3151 SET_SRC (PATTERN (insn)))))
3152 {
3153 /* A setting of the reg to a different value reaches INSN. */
3154 return 0;
3155 }
3156 if (number_of_reaching_defs > 1)
3157 {
3158 /* If in this setting the value the register is being
3159 set to is equal to the previous value the register
3160 was set to and this setting reaches the insn we are
3161 trying to do the substitution on then we are ok. */
3162
3163 if (GET_CODE (PATTERN (this_reg->insn)) == CLOBBER)
3164 return 0;
3165 if (! rtx_equal_p (SET_SRC (PATTERN (this_reg->insn)),
3166 SET_SRC (PATTERN (insn))))
3167 return 0;
3168 }
3169 *addr_this_reg = this_reg;
3170 }
3171
3172 /* prev_this_reg = this_reg; */
3173 this_reg = this_reg->next;
3174 }
3175
3176 return number_of_reaching_defs;
3177}
3178
3179/* Expression computed by insn is available and the substitution is legal,
3180 so try to perform the substitution.
3181
3182 The result is non-zero if any changes were made. */
3183
3184static int
3185handle_avail_expr (insn, expr)
3186 rtx insn;
3187 struct expr *expr;
3188{
3189 rtx pat, insn_computes_expr;
3190 rtx to;
3191 struct reg_set *this_reg;
3192 int found_setting, use_src;
3193 int changed = 0;
3194
3195 /* We only handle the case where one computation of the expression
3196 reaches this instruction. */
3197 insn_computes_expr = computing_insn (expr, insn);
3198 if (insn_computes_expr == NULL)
3199 return 0;
3200
3201 found_setting = 0;
3202 use_src = 0;
3203
3204 /* At this point we know only one computation of EXPR outside of this
3205 block reaches this insn. Now try to find a register that the
3206 expression is computed into. */
3207
3208 if (GET_CODE (SET_SRC (PATTERN (insn_computes_expr))) == REG)
3209 {
3210 /* This is the case when the available expression that reaches
3211 here has already been handled as an available expression. */
3212 int regnum_for_replacing = REGNO (SET_SRC (PATTERN (insn_computes_expr)));
3213 /* If the register was created by GCSE we can't use `reg_set_table',
3214 however we know it's set only once. */
3215 if (regnum_for_replacing >= max_gcse_regno
3216 /* If the register the expression is computed into is set only once,
3217 or only one set reaches this insn, we can use it. */
3218 || (((this_reg = reg_set_table[regnum_for_replacing]),
3219 this_reg->next == NULL)
3220 || can_disregard_other_sets (&this_reg, insn, 0)))
3221 {
3222 use_src = 1;
3223 found_setting = 1;
3224 }
3225 }
3226
3227 if (!found_setting)
3228 {
3229 int regnum_for_replacing = REGNO (SET_DEST (PATTERN (insn_computes_expr)));
3230 /* This shouldn't happen. */
3231 if (regnum_for_replacing >= max_gcse_regno)
3232 abort ();
3233 this_reg = reg_set_table[regnum_for_replacing];
3234 /* If the register the expression is computed into is set only once,
3235 or only one set reaches this insn, use it. */
3236 if (this_reg->next == NULL
3237 || can_disregard_other_sets (&this_reg, insn, 0))
3238 found_setting = 1;
3239 }
3240
3241 if (found_setting)
3242 {
3243 pat = PATTERN (insn);
3244 if (use_src)
3245 to = SET_SRC (PATTERN (insn_computes_expr));
3246 else
3247 to = SET_DEST (PATTERN (insn_computes_expr));
3248 changed = validate_change (insn, &SET_SRC (pat), to, 0);
3249
3250 /* We should be able to ignore the return code from validate_change but
3251 to play it safe we check. */
3252 if (changed)
3253 {
3254 gcse_subst_count++;
3255 if (gcse_file != NULL)
3256 {
3257 fprintf (gcse_file, "GCSE: Replacing the source in insn %d with reg %d %s insn %d\n",
3258 INSN_UID (insn), REGNO (to),
3259 use_src ? "from" : "set in",
3260 INSN_UID (insn_computes_expr));
3261 }
3262
3263 }
3264 }
3265 /* The register that the expr is computed into is set more than once. */
3266 else if (1 /*expensive_op(this_pattrn->op) && do_expensive_gcse)*/)
3267 {
3268 /* Insert an insn after insnx that copies the reg set in insnx
3269 into a new pseudo register call this new register REGN.
3270 From insnb until end of basic block or until REGB is set
3271 replace all uses of REGB with REGN. */
3272 rtx new_insn;
3273
3274 to = gen_reg_rtx (GET_MODE (SET_DEST (PATTERN (insn_computes_expr))));
3275
3276 /* Generate the new insn. */
3277 /* ??? If the change fails, we return 0, even though we created
3278 an insn. I think this is ok. */
9e6a5703
JC
3279 new_insn
3280 = emit_insn_after (gen_rtx_SET (VOIDmode, to,
3281 SET_DEST (PATTERN (insn_computes_expr))),
7506f491
DE
3282 insn_computes_expr);
3283 /* Keep block number table up to date. */
3284 set_block_num (new_insn, BLOCK_NUM (insn_computes_expr));
3285 /* Keep register set table up to date. */
3286 record_one_set (REGNO (to), new_insn);
3287
3288 gcse_create_count++;
3289 if (gcse_file != NULL)
ac7c5af5 3290 {
7506f491
DE
3291 fprintf (gcse_file, "GCSE: Creating insn %d to copy value of reg %d, computed in insn %d,\n",
3292 INSN_UID (NEXT_INSN (insn_computes_expr)),
3293 REGNO (SET_SRC (PATTERN (NEXT_INSN (insn_computes_expr)))),
3294 INSN_UID (insn_computes_expr));
3295 fprintf (gcse_file, " into newly allocated reg %d\n", REGNO (to));
ac7c5af5 3296 }
7506f491
DE
3297
3298 pat = PATTERN (insn);
3299
3300 /* Do register replacement for INSN. */
3301 changed = validate_change (insn, &SET_SRC (pat),
3302 SET_DEST (PATTERN (NEXT_INSN (insn_computes_expr))),
3303 0);
3304
3305 /* We should be able to ignore the return code from validate_change but
3306 to play it safe we check. */
3307 if (changed)
3308 {
3309 gcse_subst_count++;
3310 if (gcse_file != NULL)
3311 {
3312 fprintf (gcse_file, "GCSE: Replacing the source in insn %d with reg %d set in insn %d\n",
3313 INSN_UID (insn),
3314 REGNO (SET_DEST (PATTERN (NEXT_INSN (insn_computes_expr)))),
3315 INSN_UID (insn_computes_expr));
3316 }
3317
3318 }
3319 }
3320
3321 return changed;
3322}
3323
3324/* Perform classic GCSE.
3325 This is called by one_classic_gcse_pass after all the dataflow analysis
3326 has been done.
3327
3328 The result is non-zero if a change was made. */
3329
3330static int
3331classic_gcse ()
3332{
3333 int bb, changed;
3334 rtx insn;
3335
3336 /* Note we start at block 1. */
3337
3338 changed = 0;
3339 for (bb = 1; bb < n_basic_blocks; bb++)
3340 {
3341 /* Reset tables used to keep track of what's still valid [since the
3342 start of the block]. */
3343 reset_opr_set_tables ();
3344
3b413743
RH
3345 for (insn = BLOCK_HEAD (bb);
3346 insn != NULL && insn != NEXT_INSN (BLOCK_END (bb));
7506f491
DE
3347 insn = NEXT_INSN (insn))
3348 {
3349 /* Is insn of form (set (pseudo-reg) ...)? */
3350
3351 if (GET_CODE (insn) == INSN
3352 && GET_CODE (PATTERN (insn)) == SET
3353 && GET_CODE (SET_DEST (PATTERN (insn))) == REG
3354 && REGNO (SET_DEST (PATTERN (insn))) >= FIRST_PSEUDO_REGISTER)
3355 {
3356 rtx pat = PATTERN (insn);
3357 rtx src = SET_SRC (pat);
3358 struct expr *expr;
3359
3360 if (want_to_gcse_p (src)
3361 /* Is the expression recorded? */
3362 && ((expr = lookup_expr (src)) != NULL)
3363 /* Is the expression available [at the start of the
3364 block]? */
3365 && TEST_BIT (ae_in[bb], expr->bitmap_index)
3366 /* Are the operands unchanged since the start of the
3367 block? */
3368 && oprs_not_set_p (src, insn))
3369 changed |= handle_avail_expr (insn, expr);
3370 }
3371
3372 /* Keep track of everything modified by this insn. */
3373 /* ??? Need to be careful w.r.t. mods done to INSN. */
3374 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3375 mark_oprs_set (insn);
ac7c5af5 3376 }
7506f491
DE
3377 }
3378
3379 return changed;
3380}
3381
3382/* Top level routine to perform one classic GCSE pass.
3383
3384 Return non-zero if a change was made. */
3385
3386static int
b5ce41ff 3387one_classic_gcse_pass (pass)
7506f491
DE
3388 int pass;
3389{
3390 int changed = 0;
3391
3392 gcse_subst_count = 0;
3393 gcse_create_count = 0;
3394
3395 alloc_expr_hash_table (max_cuid);
3396 alloc_rd_mem (n_basic_blocks, max_cuid);
b5ce41ff 3397 compute_expr_hash_table ();
7506f491
DE
3398 if (gcse_file)
3399 dump_hash_table (gcse_file, "Expression", expr_hash_table,
3400 expr_hash_table_size, n_exprs);
3401 if (n_exprs > 0)
3402 {
3403 compute_kill_rd ();
3404 compute_rd ();
3405 alloc_avail_expr_mem (n_basic_blocks, n_exprs);
3406 compute_ae_gen ();
a42cd965 3407 compute_ae_kill (ae_gen, ae_kill);
bd0eaec2 3408 compute_available (ae_gen, ae_kill, ae_out, ae_in);
7506f491
DE
3409 changed = classic_gcse ();
3410 free_avail_expr_mem ();
3411 }
3412 free_rd_mem ();
3413 free_expr_hash_table ();
3414
3415 if (gcse_file)
3416 {
3417 fprintf (gcse_file, "\n");
3418 fprintf (gcse_file, "GCSE of %s, pass %d: %d bytes needed, %d substs, %d insns created\n",
3419 current_function_name, pass,
3420 bytes_used, gcse_subst_count, gcse_create_count);
3421 }
3422
3423 return changed;
3424}
3425\f
3426/* Compute copy/constant propagation working variables. */
3427
3428/* Local properties of assignments. */
3429
3430static sbitmap *cprop_pavloc;
3431static sbitmap *cprop_absaltered;
3432
3433/* Global properties of assignments (computed from the local properties). */
3434
3435static sbitmap *cprop_avin;
3436static sbitmap *cprop_avout;
3437
3438/* Allocate vars used for copy/const propagation.
3439 N_BLOCKS is the number of basic blocks.
3440 N_SETS is the number of sets. */
3441
3442static void
3443alloc_cprop_mem (n_blocks, n_sets)
3444 int n_blocks, n_sets;
3445{
3446 cprop_pavloc = sbitmap_vector_alloc (n_blocks, n_sets);
3447 cprop_absaltered = sbitmap_vector_alloc (n_blocks, n_sets);
3448
3449 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
3450 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
3451}
3452
3453/* Free vars used by copy/const propagation. */
3454
3455static void
3456free_cprop_mem ()
3457{
3458 free (cprop_pavloc);
3459 free (cprop_absaltered);
3460 free (cprop_avin);
3461 free (cprop_avout);
3462}
3463
7506f491
DE
3464/* For each block, compute whether X is transparent.
3465 X is either an expression or an assignment [though we don't care which,
3466 for this context an assignment is treated as an expression].
3467 For each block where an element of X is modified, set (SET_P == 1) or reset
3468 (SET_P == 0) the INDX bit in BMAP. */
3469
3470static void
3471compute_transp (x, indx, bmap, set_p)
3472 rtx x;
3473 int indx;
3474 sbitmap *bmap;
3475 int set_p;
3476{
3477 int bb,i;
3478 enum rtx_code code;
6f7d635c 3479 const char *fmt;
7506f491
DE
3480
3481 /* repeat is used to turn tail-recursion into iteration. */
3482 repeat:
3483
3484 if (x == 0)
3485 return;
3486
3487 code = GET_CODE (x);
3488 switch (code)
3489 {
3490 case REG:
3491 {
3492 reg_set *r;
3493 int regno = REGNO (x);
3494
3495 if (set_p)
3496 {
3497 if (regno < FIRST_PSEUDO_REGISTER)
3498 {
3499 for (bb = 0; bb < n_basic_blocks; bb++)
3500 if (TEST_BIT (reg_set_in_block[bb], regno))
3501 SET_BIT (bmap[bb], indx);
3502 }
3503 else
3504 {
3505 for (r = reg_set_table[regno]; r != NULL; r = r->next)
3506 {
3507 bb = BLOCK_NUM (r->insn);
3508 SET_BIT (bmap[bb], indx);
3509 }
3510 }
3511 }
3512 else
3513 {
3514 if (regno < FIRST_PSEUDO_REGISTER)
3515 {
3516 for (bb = 0; bb < n_basic_blocks; bb++)
3517 if (TEST_BIT (reg_set_in_block[bb], regno))
3518 RESET_BIT (bmap[bb], indx);
3519 }
3520 else
3521 {
3522 for (r = reg_set_table[regno]; r != NULL; r = r->next)
3523 {
3524 bb = BLOCK_NUM (r->insn);
3525 RESET_BIT (bmap[bb], indx);
3526 }
3527 }
3528 }
3529 return;
3530 }
3531
3532 case MEM:
3533 if (set_p)
3534 {
3535 for (bb = 0; bb < n_basic_blocks; bb++)
3536 if (mem_set_in_block[bb])
3537 SET_BIT (bmap[bb], indx);
3538 }
3539 else
3540 {
3541 for (bb = 0; bb < n_basic_blocks; bb++)
3542 if (mem_set_in_block[bb])
3543 RESET_BIT (bmap[bb], indx);
3544 }
3545 x = XEXP (x, 0);
3546 goto repeat;
3547
3548 case PC:
3549 case CC0: /*FIXME*/
3550 case CONST:
3551 case CONST_INT:
3552 case CONST_DOUBLE:
3553 case SYMBOL_REF:
3554 case LABEL_REF:
3555 case ADDR_VEC:
3556 case ADDR_DIFF_VEC:
3557 return;
3558
3559 default:
3560 break;
3561 }
3562
3563 i = GET_RTX_LENGTH (code) - 1;
3564 fmt = GET_RTX_FORMAT (code);
3565 for (; i >= 0; i--)
3566 {
3567 if (fmt[i] == 'e')
3568 {
3569 rtx tem = XEXP (x, i);
3570
3571 /* If we are about to do the last recursive call
3572 needed at this level, change it into iteration.
3573 This function is called enough to be worth it. */
3574 if (i == 0)
3575 {
3576 x = tem;
3577 goto repeat;
3578 }
3579 compute_transp (tem, indx, bmap, set_p);
3580 }
3581 else if (fmt[i] == 'E')
3582 {
3583 int j;
3584 for (j = 0; j < XVECLEN (x, i); j++)
3585 compute_transp (XVECEXP (x, i, j), indx, bmap, set_p);
3586 }
3587 }
3588}
3589
7506f491
DE
3590/* Top level routine to do the dataflow analysis needed by copy/const
3591 propagation. */
3592
3593static void
3594compute_cprop_data ()
3595{
b5ce41ff 3596 compute_local_properties (cprop_absaltered, cprop_pavloc, NULL, 1);
ce724250
JL
3597 compute_available (cprop_pavloc, cprop_absaltered,
3598 cprop_avout, cprop_avin);
7506f491
DE
3599}
3600\f
3601/* Copy/constant propagation. */
3602
7506f491
DE
3603/* Maximum number of register uses in an insn that we handle. */
3604#define MAX_USES 8
3605
3606/* Table of uses found in an insn.
3607 Allocated statically to avoid alloc/free complexity and overhead. */
3608static struct reg_use reg_use_table[MAX_USES];
3609
3610/* Index into `reg_use_table' while building it. */
3611static int reg_use_count;
3612
3613/* Set up a list of register numbers used in INSN.
3614 The found uses are stored in `reg_use_table'.
3615 `reg_use_count' is initialized to zero before entry, and
3616 contains the number of uses in the table upon exit.
3617
3618 ??? If a register appears multiple times we will record it multiple
3619 times. This doesn't hurt anything but it will slow things down. */
3620
3621static void
3622find_used_regs (x)
3623 rtx x;
3624{
3625 int i;
3626 enum rtx_code code;
6f7d635c 3627 const char *fmt;
7506f491
DE
3628
3629 /* repeat is used to turn tail-recursion into iteration. */
3630 repeat:
3631
3632 if (x == 0)
3633 return;
3634
3635 code = GET_CODE (x);
3636 switch (code)
3637 {
3638 case REG:
3639 if (reg_use_count == MAX_USES)
3640 return;
3641 reg_use_table[reg_use_count].reg_rtx = x;
3642 reg_use_count++;
3643 return;
3644
3645 case MEM:
3646 x = XEXP (x, 0);
3647 goto repeat;
3648
3649 case PC:
3650 case CC0:
3651 case CONST:
3652 case CONST_INT:
3653 case CONST_DOUBLE:
3654 case SYMBOL_REF:
3655 case LABEL_REF:
3656 case CLOBBER:
3657 case ADDR_VEC:
3658 case ADDR_DIFF_VEC:
3659 case ASM_INPUT: /*FIXME*/
3660 return;
3661
3662 case SET:
3663 if (GET_CODE (SET_DEST (x)) == MEM)
3664 find_used_regs (SET_DEST (x));
3665 x = SET_SRC (x);
3666 goto repeat;
3667
3668 default:
3669 break;
3670 }
3671
3672 /* Recursively scan the operands of this expression. */
3673
3674 fmt = GET_RTX_FORMAT (code);
3675 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3676 {
3677 if (fmt[i] == 'e')
3678 {
3679 /* If we are about to do the last recursive call
3680 needed at this level, change it into iteration.
3681 This function is called enough to be worth it. */
3682 if (i == 0)
3683 {
3684 x = XEXP (x, 0);
3685 goto repeat;
3686 }
3687 find_used_regs (XEXP (x, i));
3688 }
3689 else if (fmt[i] == 'E')
3690 {
3691 int j;
3692 for (j = 0; j < XVECLEN (x, i); j++)
3693 find_used_regs (XVECEXP (x, i, j));
3694 }
3695 }
3696}
3697
3698/* Try to replace all non-SET_DEST occurrences of FROM in INSN with TO.
3699 Returns non-zero is successful. */
3700
3701static int
3702try_replace_reg (from, to, insn)
3703 rtx from, to, insn;
3704{
e78d9500
JL
3705 /* If this fails we could try to simplify the result of the
3706 replacement and attempt to recognize the simplified insn.
3707
3708 But we need a general simplify_rtx that doesn't have pass
3709 specific state variables. I'm not aware of one at the moment. */
7506f491
DE
3710 return validate_replace_src (from, to, insn);
3711}
3712
3713/* Find a set of REGNO that is available on entry to INSN's block.
3714 Returns NULL if not found. */
3715
3716static struct expr *
3717find_avail_set (regno, insn)
3718 int regno;
3719 rtx insn;
3720{
cafba495
BS
3721 /* SET1 contains the last set found that can be returned to the caller for
3722 use in a substitution. */
3723 struct expr *set1 = 0;
3724
3725 /* Loops are not possible here. To get a loop we would need two sets
3726 available at the start of the block containing INSN. ie we would
3727 need two sets like this available at the start of the block:
3728
3729 (set (reg X) (reg Y))
3730 (set (reg Y) (reg X))
3731
3732 This can not happen since the set of (reg Y) would have killed the
3733 set of (reg X) making it unavailable at the start of this block. */
3734 while (1)
3735 {
3736 rtx src;
3737 struct expr *set = lookup_set (regno, NULL_RTX);
3738
3739 /* Find a set that is available at the start of the block
3740 which contains INSN. */
3741 while (set)
3742 {
3743 if (TEST_BIT (cprop_avin[BLOCK_NUM (insn)], set->bitmap_index))
3744 break;
3745 set = next_set (regno, set);
3746 }
7506f491 3747
cafba495
BS
3748 /* If no available set was found we've reached the end of the
3749 (possibly empty) copy chain. */
3750 if (set == 0)
3751 break;
3752
3753 if (GET_CODE (set->expr) != SET)
3754 abort ();
3755
3756 src = SET_SRC (set->expr);
3757
3758 /* We know the set is available.
3759 Now check that SRC is ANTLOC (i.e. none of the source operands
3760 have changed since the start of the block).
3761
3762 If the source operand changed, we may still use it for the next
3763 iteration of this loop, but we may not use it for substitutions. */
3764 if (CONSTANT_P (src) || oprs_not_set_p (src, insn))
3765 set1 = set;
3766
3767 /* If the source of the set is anything except a register, then
3768 we have reached the end of the copy chain. */
3769 if (GET_CODE (src) != REG)
7506f491 3770 break;
7506f491 3771
cafba495
BS
3772 /* Follow the copy chain, ie start another iteration of the loop
3773 and see if we have an available copy into SRC. */
3774 regno = REGNO (src);
3775 }
3776
3777 /* SET1 holds the last set that was available and anticipatable at
3778 INSN. */
3779 return set1;
7506f491
DE
3780}
3781
abd535b6
BS
3782/* Subroutine of cprop_insn that tries to propagate constants into
3783 JUMP_INSNS. INSN must be a conditional jump; COPY is a copy of it
3784 that we can use for substitutions.
3785 REG_USED is the use we will try to replace, SRC is the constant we
3786 will try to substitute for it.
3787 Returns nonzero if a change was made. */
3788static int
3789cprop_jump (insn, copy, reg_used, src)
3790 rtx insn, copy;
3791 struct reg_use *reg_used;
3792 rtx src;
3793{
3794 rtx set = PATTERN (copy);
3795 rtx temp;
3796
3797 /* Replace the register with the appropriate constant. */
3798 replace_rtx (SET_SRC (set), reg_used->reg_rtx, src);
3799
3800 temp = simplify_ternary_operation (GET_CODE (SET_SRC (set)),
3801 GET_MODE (SET_SRC (set)),
3802 GET_MODE (XEXP (SET_SRC (set), 0)),
3803 XEXP (SET_SRC (set), 0),
3804 XEXP (SET_SRC (set), 1),
3805 XEXP (SET_SRC (set), 2));
3806
3807 /* If no simplification can be made, then try the next
3808 register. */
3809 if (temp == 0)
3810 return 0;
3811
3812 SET_SRC (set) = temp;
3813
3814 /* That may have changed the structure of TEMP, so
3815 force it to be rerecognized if it has not turned
3816 into a nop or unconditional jump. */
3817
3818 INSN_CODE (copy) = -1;
3819 if ((SET_DEST (set) == pc_rtx
3820 && (SET_SRC (set) == pc_rtx
3821 || GET_CODE (SET_SRC (set)) == LABEL_REF))
3822 || recog (PATTERN (copy), copy, NULL) >= 0)
3823 {
3824 /* This has either become an unconditional jump
3825 or a nop-jump. We'd like to delete nop jumps
3826 here, but doing so confuses gcse. So we just
3827 make the replacement and let later passes
3828 sort things out. */
3829 PATTERN (insn) = set;
3830 INSN_CODE (insn) = -1;
3831
3832 /* One less use of the label this insn used to jump to
3833 if we turned this into a NOP jump. */
3834 if (SET_SRC (set) == pc_rtx && JUMP_LABEL (insn) != 0)
3835 --LABEL_NUSES (JUMP_LABEL (insn));
3836
3837 /* If this has turned into an unconditional jump,
3838 then put a barrier after it so that the unreachable
3839 code will be deleted. */
3840 if (GET_CODE (SET_SRC (set)) == LABEL_REF)
3841 emit_barrier_after (insn);
3842
3843 run_jump_opt_after_gcse = 1;
3844
3845 const_prop_count++;
3846 if (gcse_file != NULL)
3847 {
3848 int regno = REGNO (reg_used->reg_rtx);
3849 fprintf (gcse_file, "CONST-PROP: Replacing reg %d in insn %d with constant ",
3850 regno, INSN_UID (insn));
3851 print_rtl (gcse_file, src);
3852 fprintf (gcse_file, "\n");
3853 }
3854 return 1;
3855 }
3856 return 0;
3857}
3858
3859#ifdef HAVE_cc0
3860/* Subroutine of cprop_insn that tries to propagate constants into
3861 JUMP_INSNS for machines that have CC0. INSN is a single set that
3862 stores into CC0; the insn following it is a conditional jump.
3863 REG_USED is the use we will try to replace, SRC is the constant we
3864 will try to substitute for it.
3865 Returns nonzero if a change was made. */
3866static int
3867cprop_cc0_jump (insn, reg_used, src)
3868 rtx insn;
3869 struct reg_use *reg_used;
3870 rtx src;
3871{
3872 rtx jump = NEXT_INSN (insn);
3873 rtx copy = copy_rtx (jump);
3874 rtx set = PATTERN (copy);
3875
3876 /* We need to copy the source of the cc0 setter, as cprop_jump is going to
3877 substitute into it. */
3878 replace_rtx (SET_SRC (set), cc0_rtx, copy_rtx (SET_SRC (PATTERN (insn))));
3879 if (! cprop_jump (jump, copy, reg_used, src))
3880 return 0;
3881
3882 /* If we succeeded, delete the cc0 setter. */
3883 PUT_CODE (insn, NOTE);
3884 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
3885 NOTE_SOURCE_FILE (insn) = 0;
3886 return 1;
3887 }
3888#endif
3889
7506f491
DE
3890/* Perform constant and copy propagation on INSN.
3891 The result is non-zero if a change was made. */
3892
3893static int
b5ce41ff 3894cprop_insn (insn, alter_jumps)
7506f491 3895 rtx insn;
b5ce41ff 3896 int alter_jumps;
7506f491
DE
3897{
3898 struct reg_use *reg_used;
3899 int changed = 0;
3900
e78d9500
JL
3901 /* Only propagate into SETs. Note that a conditional jump is a
3902 SET with pc_rtx as the destination. */
3903 if ((GET_CODE (insn) != INSN
3904 && GET_CODE (insn) != JUMP_INSN)
7506f491
DE
3905 || GET_CODE (PATTERN (insn)) != SET)
3906 return 0;
3907
3908 reg_use_count = 0;
3909 find_used_regs (PATTERN (insn));
3910
3911 reg_used = &reg_use_table[0];
3912 for ( ; reg_use_count > 0; reg_used++, reg_use_count--)
3913 {
3914 rtx pat, src;
3915 struct expr *set;
3916 int regno = REGNO (reg_used->reg_rtx);
3917
3918 /* Ignore registers created by GCSE.
3919 We do this because ... */
3920 if (regno >= max_gcse_regno)
3921 continue;
3922
3923 /* If the register has already been set in this block, there's
3924 nothing we can do. */
3925 if (! oprs_not_set_p (reg_used->reg_rtx, insn))
3926 continue;
3927
3928 /* Find an assignment that sets reg_used and is available
3929 at the start of the block. */
3930 set = find_avail_set (regno, insn);
3931 if (! set)
3932 continue;
3933
3934 pat = set->expr;
3935 /* ??? We might be able to handle PARALLELs. Later. */
3936 if (GET_CODE (pat) != SET)
3937 abort ();
3938 src = SET_SRC (pat);
3939
e78d9500 3940 /* Constant propagation. */
05f6f07c
BS
3941 if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE
3942 || GET_CODE (src) == SYMBOL_REF)
7506f491 3943 {
e78d9500
JL
3944 /* Handle normal insns first. */
3945 if (GET_CODE (insn) == INSN
3946 && try_replace_reg (reg_used->reg_rtx, src, insn))
7506f491
DE
3947 {
3948 changed = 1;
3949 const_prop_count++;
3950 if (gcse_file != NULL)
3951 {
3952 fprintf (gcse_file, "CONST-PROP: Replacing reg %d in insn %d with constant ",
3953 regno, INSN_UID (insn));
e78d9500 3954 print_rtl (gcse_file, src);
7506f491
DE
3955 fprintf (gcse_file, "\n");
3956 }
3957
3958 /* The original insn setting reg_used may or may not now be
3959 deletable. We leave the deletion to flow. */
3960 }
e78d9500
JL
3961
3962 /* Try to propagate a CONST_INT into a conditional jump.
3963 We're pretty specific about what we will handle in this
3964 code, we can extend this as necessary over time.
3965
3966 Right now the insn in question must look like
abd535b6 3967 (set (pc) (if_then_else ...)) */
b5ce41ff 3968 else if (alter_jumps
6e9a3c38
JL
3969 && GET_CODE (insn) == JUMP_INSN
3970 && condjump_p (insn)
3971 && ! simplejump_p (insn))
abd535b6
BS
3972 changed |= cprop_jump (insn, copy_rtx (insn), reg_used, src);
3973#ifdef HAVE_cc0
3974 /* Similar code for machines that use a pair of CC0 setter and
3975 conditional jump insn. */
3976 else if (alter_jumps
3977 && GET_CODE (PATTERN (insn)) == SET
3978 && SET_DEST (PATTERN (insn)) == cc0_rtx
3979 && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
3980 && condjump_p (NEXT_INSN (insn))
3981 && ! simplejump_p (NEXT_INSN (insn)))
3982 changed |= cprop_cc0_jump (insn, reg_used, src);
3983#endif
7506f491
DE
3984 }
3985 else if (GET_CODE (src) == REG
3986 && REGNO (src) >= FIRST_PSEUDO_REGISTER
3987 && REGNO (src) != regno)
3988 {
cafba495 3989 if (try_replace_reg (reg_used->reg_rtx, src, insn))
7506f491 3990 {
cafba495
BS
3991 changed = 1;
3992 copy_prop_count++;
3993 if (gcse_file != NULL)
7506f491 3994 {
cafba495
BS
3995 fprintf (gcse_file, "COPY-PROP: Replacing reg %d in insn %d with reg %d\n",
3996 regno, INSN_UID (insn), REGNO (src));
7506f491 3997 }
cafba495
BS
3998
3999 /* The original insn setting reg_used may or may not now be
4000 deletable. We leave the deletion to flow. */
4001 /* FIXME: If it turns out that the insn isn't deletable,
4002 then we may have unnecessarily extended register lifetimes
4003 and made things worse. */
7506f491
DE
4004 }
4005 }
4006 }
4007
4008 return changed;
4009}
4010
4011/* Forward propagate copies.
4012 This includes copies and constants.
4013 Return non-zero if a change was made. */
4014
4015static int
b5ce41ff
JL
4016cprop (alter_jumps)
4017 int alter_jumps;
7506f491
DE
4018{
4019 int bb, changed;
4020 rtx insn;
4021
4022 /* Note we start at block 1. */
4023
4024 changed = 0;
4025 for (bb = 1; bb < n_basic_blocks; bb++)
4026 {
4027 /* Reset tables used to keep track of what's still valid [since the
4028 start of the block]. */
4029 reset_opr_set_tables ();
4030
3b413743
RH
4031 for (insn = BLOCK_HEAD (bb);
4032 insn != NULL && insn != NEXT_INSN (BLOCK_END (bb));
7506f491
DE
4033 insn = NEXT_INSN (insn))
4034 {
4035 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4036 {
b5ce41ff 4037 changed |= cprop_insn (insn, alter_jumps);
7506f491
DE
4038
4039 /* Keep track of everything modified by this insn. */
abd535b6
BS
4040 /* ??? Need to be careful w.r.t. mods done to INSN. Don't
4041 call mark_oprs_set if we turned the insn into a NOTE. */
4042 if (GET_CODE (insn) != NOTE)
4043 mark_oprs_set (insn);
7506f491 4044 }
ac7c5af5 4045 }
7506f491
DE
4046 }
4047
4048 if (gcse_file != NULL)
4049 fprintf (gcse_file, "\n");
4050
4051 return changed;
4052}
4053
4054/* Perform one copy/constant propagation pass.
4055 F is the first insn in the function.
4056 PASS is the pass count. */
4057
4058static int
b5ce41ff 4059one_cprop_pass (pass, alter_jumps)
7506f491 4060 int pass;
b5ce41ff 4061 int alter_jumps;
7506f491
DE
4062{
4063 int changed = 0;
4064
4065 const_prop_count = 0;
4066 copy_prop_count = 0;
4067
4068 alloc_set_hash_table (max_cuid);
b5ce41ff 4069 compute_set_hash_table ();
7506f491
DE
4070 if (gcse_file)
4071 dump_hash_table (gcse_file, "SET", set_hash_table, set_hash_table_size,
4072 n_sets);
4073 if (n_sets > 0)
4074 {
4075 alloc_cprop_mem (n_basic_blocks, n_sets);
4076 compute_cprop_data ();
b5ce41ff 4077 changed = cprop (alter_jumps);
7506f491
DE
4078 free_cprop_mem ();
4079 }
4080 free_set_hash_table ();
4081
4082 if (gcse_file)
4083 {
4084 fprintf (gcse_file, "CPROP of %s, pass %d: %d bytes needed, %d const props, %d copy props\n",
4085 current_function_name, pass,
4086 bytes_used, const_prop_count, copy_prop_count);
4087 fprintf (gcse_file, "\n");
4088 }
4089
4090 return changed;
4091}
4092\f
a65f3558 4093/* Compute PRE+LCM working variables. */
7506f491
DE
4094
4095/* Local properties of expressions. */
4096/* Nonzero for expressions that are transparent in the block. */
a65f3558 4097static sbitmap *transp;
7506f491 4098
5c35539b
RH
4099/* Nonzero for expressions that are transparent at the end of the block.
4100 This is only zero for expressions killed by abnormal critical edge
4101 created by a calls. */
a65f3558 4102static sbitmap *transpout;
5c35539b 4103
a65f3558
JL
4104/* Nonzero for expressions that are computed (available) in the block. */
4105static sbitmap *comp;
7506f491 4106
a65f3558
JL
4107/* Nonzero for expressions that are locally anticipatable in the block. */
4108static sbitmap *antloc;
7506f491 4109
a65f3558
JL
4110/* Nonzero for expressions where this block is an optimal computation
4111 point. */
4112static sbitmap *pre_optimal;
5c35539b 4113
a65f3558
JL
4114/* Nonzero for expressions which are redundant in a particular block. */
4115static sbitmap *pre_redundant;
7506f491 4116
a42cd965
AM
4117/* Nonzero for expressions which should be inserted on a specific edge. */
4118static sbitmap *pre_insert_map;
4119
4120/* Nonzero for expressions which should be deleted in a specific block. */
4121static sbitmap *pre_delete_map;
4122
4123/* Contains the edge_list returned by pre_edge_lcm. */
4124static struct edge_list *edge_list;
4125
a65f3558 4126static sbitmap *temp_bitmap;
7506f491 4127
a65f3558
JL
4128/* Redundant insns. */
4129static sbitmap pre_redundant_insns;
7506f491 4130
a65f3558 4131/* Allocate vars used for PRE analysis. */
7506f491
DE
4132
4133static void
a65f3558
JL
4134alloc_pre_mem (n_blocks, n_exprs)
4135 int n_blocks, n_exprs;
7506f491 4136{
a65f3558
JL
4137 transp = sbitmap_vector_alloc (n_blocks, n_exprs);
4138 comp = sbitmap_vector_alloc (n_blocks, n_exprs);
4139 antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
a65f3558 4140 temp_bitmap = sbitmap_vector_alloc (n_blocks, n_exprs);
5faf03ae 4141
a42cd965
AM
4142 pre_optimal = NULL;
4143 pre_redundant = NULL;
4144 pre_insert_map = NULL;
4145 pre_delete_map = NULL;
4146 ae_in = NULL;
4147 ae_out = NULL;
4148 u_bitmap = NULL;
a65f3558 4149 transpout = sbitmap_vector_alloc (n_blocks, n_exprs);
a42cd965
AM
4150 ae_kill = sbitmap_vector_alloc (n_blocks, n_exprs);
4151 /* pre_insert and pre_delete are allocated later. */
7506f491
DE
4152}
4153
a65f3558 4154/* Free vars used for PRE analysis. */
7506f491
DE
4155
4156static void
a65f3558 4157free_pre_mem ()
7506f491 4158{
a65f3558
JL
4159 free (transp);
4160 free (comp);
4161 free (antloc);
5faf03ae 4162 free (temp_bitmap);
7506f491 4163
a42cd965
AM
4164 if (pre_optimal)
4165 free (pre_optimal);
4166 if (pre_redundant)
4167 free (pre_redundant);
4168 if (pre_insert_map)
4169 free (pre_insert_map);
4170 if (pre_delete_map)
4171 free (pre_delete_map);
4172 if (transpout)
4173 free (transpout);
4174
4175 if (ae_in)
4176 free (ae_in);
4177 if (ae_out)
4178 free (ae_out);
4179 if (ae_kill)
4180 free (ae_kill);
4181 if (u_bitmap)
4182 free (u_bitmap);
4183
4184 transp = comp = antloc = NULL;
4185 pre_optimal = pre_redundant = pre_insert_map = pre_delete_map = NULL;
4186 transpout = ae_in = ae_out = ae_kill = NULL;
4187 u_bitmap = NULL;
4188
7506f491
DE
4189}
4190
4191/* Top level routine to do the dataflow analysis needed by PRE. */
4192
4193static void
4194compute_pre_data ()
4195{
a65f3558
JL
4196 compute_local_properties (transp, comp, antloc, 0);
4197 compute_transpout ();
a42cd965
AM
4198 sbitmap_vector_zero (ae_kill, n_basic_blocks);
4199 compute_ae_kill (comp, ae_kill);
4200 edge_list = pre_edge_lcm (gcse_file, n_exprs, transp, comp, antloc,
4201 ae_kill, &pre_insert_map, &pre_delete_map);
7506f491 4202}
a65f3558 4203
7506f491
DE
4204\f
4205/* PRE utilities */
4206
a65f3558
JL
4207/* Return non-zero if an occurrence of expression EXPR in OCCR_BB would reach
4208 block BB.
7506f491
DE
4209
4210 VISITED is a pointer to a working buffer for tracking which BB's have
4211 been visited. It is NULL for the top-level call.
4212
4213 We treat reaching expressions that go through blocks containing the same
4214 reaching expression as "not reaching". E.g. if EXPR is generated in blocks
4215 2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
4216 2 as not reaching. The intent is to improve the probability of finding
4217 only one reaching expression and to reduce register lifetimes by picking
4218 the closest such expression. */
4219
4220static int
89e606c9 4221pre_expr_reaches_here_p_work (occr_bb, expr, bb, visited)
a65f3558 4222 int occr_bb;
7506f491
DE
4223 struct expr *expr;
4224 int bb;
4225 char *visited;
4226{
36349f8b 4227 edge pred;
7506f491 4228
36349f8b 4229 for (pred = BASIC_BLOCK (bb)->pred; pred != NULL; pred = pred->pred_next)
7506f491 4230 {
36349f8b 4231 int pred_bb = pred->src->index;
7506f491 4232
36349f8b 4233 if (pred->src == ENTRY_BLOCK_PTR
7506f491
DE
4234 /* Has predecessor has already been visited? */
4235 || visited[pred_bb])
ac7c5af5 4236 {
7506f491
DE
4237 /* Nothing to do. */
4238 }
4239 /* Does this predecessor generate this expression? */
89e606c9 4240 else if (TEST_BIT (comp[pred_bb], expr->bitmap_index))
7506f491
DE
4241 {
4242 /* Is this the occurrence we're looking for?
4243 Note that there's only one generating occurrence per block
4244 so we just need to check the block number. */
a65f3558 4245 if (occr_bb == pred_bb)
7506f491
DE
4246 return 1;
4247 visited[pred_bb] = 1;
4248 }
4249 /* Ignore this predecessor if it kills the expression. */
a65f3558 4250 else if (! TEST_BIT (transp[pred_bb], expr->bitmap_index))
7506f491
DE
4251 visited[pred_bb] = 1;
4252 /* Neither gen nor kill. */
4253 else
ac7c5af5 4254 {
7506f491 4255 visited[pred_bb] = 1;
89e606c9 4256 if (pre_expr_reaches_here_p_work (occr_bb, expr, pred_bb, visited))
7506f491 4257 return 1;
ac7c5af5 4258 }
7506f491
DE
4259 }
4260
4261 /* All paths have been checked. */
4262 return 0;
4263}
283a2545
RL
4264
4265/* The wrapper for pre_expr_reaches_here_work that ensures that any
4266 memory allocated for that function is returned. */
4267
4268static int
89e606c9 4269pre_expr_reaches_here_p (occr_bb, expr, bb)
283a2545
RL
4270 int occr_bb;
4271 struct expr *expr;
4272 int bb;
283a2545
RL
4273{
4274 int rval;
4275 char * visited = (char *) xcalloc (n_basic_blocks, 1);
4276
89e606c9 4277 rval = pre_expr_reaches_here_p_work(occr_bb, expr, bb, visited);
283a2545
RL
4278
4279 free (visited);
4280
4281 return (rval);
4282}
7506f491 4283\f
a42cd965
AM
4284
4285/* Given an expr, generate RTL which we can insert at the end of a BB,
4286 or on an edge. Set the block number of any insns generated to
4287 the value of BB. */
4288
4289static rtx
4290process_insert_insn (expr)
4291 struct expr *expr;
4292{
4293 rtx reg = expr->reaching_reg;
4294 rtx pat, copied_expr;
4295 rtx first_new_insn;
4296
4297 start_sequence ();
4298 copied_expr = copy_rtx (expr->expr);
4299 emit_move_insn (reg, copied_expr);
4300 first_new_insn = get_insns ();
4301 pat = gen_sequence ();
4302 end_sequence ();
4303
4304 return pat;
4305}
4306
a65f3558
JL
4307/* Add EXPR to the end of basic block BB.
4308
4309 This is used by both the PRE and code hoisting.
4310
4311 For PRE, we want to verify that the expr is either transparent
4312 or locally anticipatable in the target block. This check makes
4313 no sense for code hoisting. */
7506f491
DE
4314
4315static void
a65f3558 4316insert_insn_end_bb (expr, bb, pre)
7506f491
DE
4317 struct expr *expr;
4318 int bb;
a65f3558 4319 int pre;
7506f491
DE
4320{
4321 rtx insn = BLOCK_END (bb);
4322 rtx new_insn;
4323 rtx reg = expr->reaching_reg;
4324 int regno = REGNO (reg);
a42cd965 4325 rtx pat;
7506f491 4326
a42cd965 4327 pat = process_insert_insn (expr);
7506f491
DE
4328
4329 /* If the last insn is a jump, insert EXPR in front [taking care to
4330 handle cc0, etc. properly]. */
4331
4332 if (GET_CODE (insn) == JUMP_INSN)
4333 {
50b2596f 4334#ifdef HAVE_cc0
7506f491 4335 rtx note;
50b2596f 4336#endif
7506f491
DE
4337
4338 /* If this is a jump table, then we can't insert stuff here. Since
4339 we know the previous real insn must be the tablejump, we insert
4340 the new instruction just before the tablejump. */
4341 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
4342 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
4343 insn = prev_real_insn (insn);
4344
4345#ifdef HAVE_cc0
4346 /* FIXME: 'twould be nice to call prev_cc0_setter here but it aborts
4347 if cc0 isn't set. */
4348 note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
4349 if (note)
4350 insn = XEXP (note, 0);
4351 else
4352 {
4353 rtx maybe_cc0_setter = prev_nonnote_insn (insn);
4354 if (maybe_cc0_setter
4355 && GET_RTX_CLASS (GET_CODE (maybe_cc0_setter)) == 'i'
4356 && sets_cc0_p (PATTERN (maybe_cc0_setter)))
4357 insn = maybe_cc0_setter;
4358 }
4359#endif
4360 /* FIXME: What if something in cc0/jump uses value set in new insn? */
4361 new_insn = emit_insn_before (pat, insn);
4362 if (BLOCK_HEAD (bb) == insn)
4363 BLOCK_HEAD (bb) = new_insn;
3947e2f9
RH
4364 }
4365 /* Likewise if the last insn is a call, as will happen in the presence
4366 of exception handling. */
5c35539b 4367 else if (GET_CODE (insn) == CALL_INSN)
3947e2f9
RH
4368 {
4369 HARD_REG_SET parm_regs;
4370 int nparm_regs;
4371 rtx p;
4372
4373 /* Keeping in mind SMALL_REGISTER_CLASSES and parameters in registers,
4374 we search backward and place the instructions before the first
4375 parameter is loaded. Do this for everyone for consistency and a
4376 presumtion that we'll get better code elsewhere as well. */
4377
4378 /* It should always be the case that we can put these instructions
a65f3558
JL
4379 anywhere in the basic block with performing PRE optimizations.
4380 Check this. */
4381 if (pre
4382 && !TEST_BIT (antloc[bb], expr->bitmap_index)
4383 && !TEST_BIT (transp[bb], expr->bitmap_index))
3947e2f9
RH
4384 abort ();
4385
4386 /* Since different machines initialize their parameter registers
4387 in different orders, assume nothing. Collect the set of all
4388 parameter registers. */
4389 CLEAR_HARD_REG_SET (parm_regs);
4390 nparm_regs = 0;
4391 for (p = CALL_INSN_FUNCTION_USAGE (insn); p ; p = XEXP (p, 1))
4392 if (GET_CODE (XEXP (p, 0)) == USE
4393 && GET_CODE (XEXP (XEXP (p, 0), 0)) == REG)
4394 {
4395 int regno = REGNO (XEXP (XEXP (p, 0), 0));
4396 if (regno >= FIRST_PSEUDO_REGISTER)
5c35539b 4397 abort ();
3947e2f9
RH
4398 SET_HARD_REG_BIT (parm_regs, regno);
4399 nparm_regs++;
4400 }
4401
4402 /* Search backward for the first set of a register in this set. */
4403 while (nparm_regs && BLOCK_HEAD (bb) != insn)
4404 {
4405 insn = PREV_INSN (insn);
4406 p = single_set (insn);
4407 if (p && GET_CODE (SET_DEST (p)) == REG
4408 && REGNO (SET_DEST (p)) < FIRST_PSEUDO_REGISTER
4409 && TEST_HARD_REG_BIT (parm_regs, REGNO (SET_DEST (p))))
4410 {
4411 CLEAR_HARD_REG_BIT (parm_regs, REGNO (SET_DEST (p)));
4412 nparm_regs--;
4413 }
4414 }
4415
b1d26727
JL
4416 /* If we found all the parameter loads, then we want to insert
4417 before the first parameter load.
4418
4419 If we did not find all the parameter loads, then we might have
4420 stopped on the head of the block, which could be a CODE_LABEL.
4421 If we inserted before the CODE_LABEL, then we would be putting
4422 the insn in the wrong basic block. In that case, put the insn
4423 after the CODE_LABEL.
4424
4425 ?!? Do we need to account for NOTE_INSN_BASIC_BLOCK here? */
4426 if (GET_CODE (insn) != CODE_LABEL)
4427 {
4428 new_insn = emit_insn_before (pat, insn);
4429 if (BLOCK_HEAD (bb) == insn)
4430 BLOCK_HEAD (bb) = new_insn;
4431 }
4432 else
4433 {
4434 new_insn = emit_insn_after (pat, insn);
4435 }
7506f491
DE
4436 }
4437 else
4438 {
4439 new_insn = emit_insn_after (pat, insn);
4440 BLOCK_END (bb) = new_insn;
7506f491
DE
4441 }
4442
a65f3558
JL
4443 /* Keep block number table up to date.
4444 Note, PAT could be a multiple insn sequence, we have to make
4445 sure that each insn in the sequence is handled. */
4446 if (GET_CODE (pat) == SEQUENCE)
4447 {
4448 int i;
4449
4450 for (i = 0; i < XVECLEN (pat, 0); i++)
4451 {
4452 rtx insn = XVECEXP (pat, 0, i);
4453 set_block_num (insn, bb);
4454 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4455 add_label_notes (PATTERN (insn), new_insn);
84832317 4456 note_stores (PATTERN (insn), record_set_info, insn);
a65f3558
JL
4457 }
4458 }
4459 else
4460 {
4461 add_label_notes (SET_SRC (pat), new_insn);
4462 set_block_num (new_insn, bb);
4463 /* Keep register set table up to date. */
4464 record_one_set (regno, new_insn);
4465 }
3947e2f9 4466
7506f491
DE
4467 gcse_create_count++;
4468
4469 if (gcse_file)
4470 {
a65f3558 4471 fprintf (gcse_file, "PRE/HOIST: end of bb %d, insn %d, copying expression %d to reg %d\n",
7506f491
DE
4472 bb, INSN_UID (new_insn), expr->bitmap_index, regno);
4473 }
4474}
4475
a42cd965
AM
4476/* Insert partially redundant expressions on edges in the CFG to make
4477 the expressions fully redundant. */
7506f491 4478
a42cd965
AM
4479static int
4480pre_edge_insert (edge_list, index_map)
4481 struct edge_list *edge_list;
7506f491
DE
4482 struct expr **index_map;
4483{
a42cd965 4484 int e, i, num_edges, set_size, did_insert = 0;
a65f3558
JL
4485 sbitmap *inserted;
4486
a42cd965
AM
4487 /* Where PRE_INSERT_MAP is nonzero, we add the expression on that edge
4488 if it reaches any of the deleted expressions. */
7506f491 4489
a42cd965
AM
4490 set_size = pre_insert_map[0]->size;
4491 num_edges = NUM_EDGES (edge_list);
4492 inserted = sbitmap_vector_alloc (num_edges, n_exprs);
4493 sbitmap_vector_zero (inserted, num_edges);
7506f491 4494
a42cd965 4495 for (e = 0; e < num_edges; e++)
7506f491
DE
4496 {
4497 int indx;
a42cd965
AM
4498 basic_block pred = INDEX_EDGE_PRED_BB (edge_list, e);
4499 int bb = pred->index;
a65f3558 4500
a65f3558 4501 for (i = indx = 0; i < set_size; i++, indx += SBITMAP_ELT_BITS)
7506f491 4502 {
a42cd965 4503 SBITMAP_ELT_TYPE insert = pre_insert_map[e]->elms[i];
7506f491 4504 int j;
7506f491 4505
a65f3558 4506 for (j = indx; insert && j < n_exprs; j++, insert >>= 1)
7506f491 4507 {
a65f3558
JL
4508 if ((insert & 1) != 0 && index_map[j]->reaching_reg != NULL_RTX)
4509 {
4510 struct expr *expr = index_map[j];
4511 struct occr *occr;
4512
4513 /* Now look at each deleted occurence of this expression. */
4514 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
4515 {
4516 if (! occr->deleted_p)
4517 continue;
4518
a42cd965
AM
4519 /* Insert this expression on this edge if if it would
4520 reach the deleted occurence in BB. */
89e606c9 4521 if (!TEST_BIT (inserted[e], j))
a65f3558 4522 {
a42cd965
AM
4523 rtx insn;
4524 edge eg = INDEX_EDGE (edge_list, e);
4525 /* We can't insert anything on an abnormal
4526 and critical edge, so we insert the
4527 insn at the end of the previous block. There
4528 are several alternatives detailed in
4529 Morgans book P277 (sec 10.5) for handling
4530 this situation. This one is easiest for now. */
4531
4532 if ((eg->flags & EDGE_ABNORMAL) == EDGE_ABNORMAL)
4533 {
4534 insert_insn_end_bb (index_map[j], bb, 0);
4535 }
4536 else
4537 {
4538 insn = process_insert_insn (index_map[j]);
4539 insert_insn_on_edge (insn, eg);
4540 }
4541 if (gcse_file)
4542 {
4543 fprintf (gcse_file,
4544 "PRE/HOIST: edge (%d,%d), copy expression %d\n",
4545 bb,
4546 INDEX_EDGE_SUCC_BB (edge_list, e)->index, expr->bitmap_index);
4547 }
4548 SET_BIT (inserted[e], j);
4549 did_insert = 1;
4550 gcse_create_count++;
a65f3558
JL
4551 }
4552 }
4553 }
7506f491
DE
4554 }
4555 }
4556 }
5faf03ae
MM
4557
4558 /* Clean up. */
4559 free (inserted);
4560
a42cd965 4561 return did_insert;
7506f491
DE
4562}
4563
4564/* Copy the result of INSN to REG.
4565 INDX is the expression number. */
4566
4567static void
4568pre_insert_copy_insn (expr, insn)
4569 struct expr *expr;
4570 rtx insn;
4571{
4572 rtx reg = expr->reaching_reg;
4573 int regno = REGNO (reg);
4574 int indx = expr->bitmap_index;
4575 rtx set = single_set (insn);
4576 rtx new_insn;
a42cd965 4577 int bb = BLOCK_NUM (insn);
7506f491
DE
4578
4579 if (!set)
4580 abort ();
9e6a5703 4581 new_insn = emit_insn_after (gen_rtx_SET (VOIDmode, reg, SET_DEST (set)),
7506f491
DE
4582 insn);
4583 /* Keep block number table up to date. */
a42cd965 4584 set_block_num (new_insn, bb);
7506f491
DE
4585 /* Keep register set table up to date. */
4586 record_one_set (regno, new_insn);
a42cd965
AM
4587 if (insn == BLOCK_END (bb))
4588 BLOCK_END (bb) = new_insn;
7506f491
DE
4589
4590 gcse_create_count++;
4591
4592 if (gcse_file)
a42cd965
AM
4593 fprintf (gcse_file,
4594 "PRE: bb %d, insn %d, copy expression %d in insn %d to reg %d\n",
4595 BLOCK_NUM (insn), INSN_UID (new_insn), indx,
4596 INSN_UID (insn), regno);
7506f491
DE
4597}
4598
4599/* Copy available expressions that reach the redundant expression
4600 to `reaching_reg'. */
4601
4602static void
4603pre_insert_copies ()
4604{
36f0e0a6 4605 int i;
a65f3558 4606
7506f491
DE
4607 /* For each available expression in the table, copy the result to
4608 `reaching_reg' if the expression reaches a deleted one.
4609
4610 ??? The current algorithm is rather brute force.
4611 Need to do some profiling. */
4612
4613 for (i = 0; i < expr_hash_table_size; i++)
4614 {
4615 struct expr *expr;
4616
4617 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
4618 {
4619 struct occr *occr;
4620
4621 /* If the basic block isn't reachable, PPOUT will be TRUE.
4622 However, we don't want to insert a copy here because the
4623 expression may not really be redundant. So only insert
4624 an insn if the expression was deleted.
4625 This test also avoids further processing if the expression
4626 wasn't deleted anywhere. */
4627 if (expr->reaching_reg == NULL)
4628 continue;
4629
4630 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
4631 {
4632 struct occr *avail;
4633
4634 if (! occr->deleted_p)
4635 continue;
4636
4637 for (avail = expr->avail_occr; avail != NULL; avail = avail->next)
4638 {
4639 rtx insn = avail->insn;
4640
4641 /* No need to handle this one if handled already. */
4642 if (avail->copied_p)
4643 continue;
4644 /* Don't handle this one if it's a redundant one. */
a65f3558 4645 if (TEST_BIT (pre_redundant_insns, INSN_CUID (insn)))
7506f491
DE
4646 continue;
4647 /* Or if the expression doesn't reach the deleted one. */
a65f3558 4648 if (! pre_expr_reaches_here_p (BLOCK_NUM (avail->insn), expr,
89e606c9 4649 BLOCK_NUM (occr->insn)))
7506f491
DE
4650 continue;
4651
4652 /* Copy the result of avail to reaching_reg. */
4653 pre_insert_copy_insn (expr, insn);
4654 avail->copied_p = 1;
4655 }
4656 }
4657 }
4658 }
4659}
4660
4661/* Delete redundant computations.
7506f491
DE
4662 Deletion is done by changing the insn to copy the `reaching_reg' of
4663 the expression into the result of the SET. It is left to later passes
4664 (cprop, cse2, flow, combine, regmove) to propagate the copy or eliminate it.
4665
4666 Returns non-zero if a change is made. */
4667
4668static int
4669pre_delete ()
4670{
a65f3558
JL
4671 int i, bb, changed;
4672
4673 /* Compute the expressions which are redundant and need to be replaced by
4674 copies from the reaching reg to the target reg. */
4675 for (bb = 0; bb < n_basic_blocks; bb++)
a42cd965 4676 sbitmap_copy (temp_bitmap[bb], pre_delete_map[bb]);
7506f491
DE
4677
4678 changed = 0;
4679 for (i = 0; i < expr_hash_table_size; i++)
4680 {
4681 struct expr *expr;
4682
4683 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
4684 {
4685 struct occr *occr;
4686 int indx = expr->bitmap_index;
4687
4688 /* We only need to search antic_occr since we require
4689 ANTLOC != 0. */
4690
4691 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
4692 {
4693 rtx insn = occr->insn;
4694 rtx set;
4695 int bb = BLOCK_NUM (insn);
7506f491 4696
a65f3558 4697 if (TEST_BIT (temp_bitmap[bb], indx))
7506f491 4698 {
7506f491
DE
4699 set = single_set (insn);
4700 if (! set)
4701 abort ();
4702
d3903c22
JL
4703 /* Create a pseudo-reg to store the result of reaching
4704 expressions into. Get the mode for the new pseudo
4705 from the mode of the original destination pseudo. */
4706 if (expr->reaching_reg == NULL)
4707 expr->reaching_reg
4708 = gen_reg_rtx (GET_MODE (SET_DEST (set)));
4709
7506f491
DE
4710 /* In theory this should never fail since we're creating
4711 a reg->reg copy.
4712
4713 However, on the x86 some of the movXX patterns actually
4714 contain clobbers of scratch regs. This may cause the
db35306d 4715 insn created by validate_change to not match any pattern
7506f491
DE
4716 and thus cause validate_change to fail. */
4717 if (validate_change (insn, &SET_SRC (set),
4718 expr->reaching_reg, 0))
4719 {
4720 occr->deleted_p = 1;
a65f3558 4721 SET_BIT (pre_redundant_insns, INSN_CUID (insn));
7506f491
DE
4722 changed = 1;
4723 gcse_subst_count++;
4724 }
4725
4726 if (gcse_file)
4727 {
a65f3558
JL
4728 fprintf (gcse_file,
4729 "PRE: redundant insn %d (expression %d) in bb %d, reaching reg is %d\n",
7506f491
DE
4730 INSN_UID (insn), indx, bb, REGNO (expr->reaching_reg));
4731 }
4732 }
4733 }
4734 }
4735 }
4736
4737 return changed;
4738}
4739
4740/* Perform GCSE optimizations using PRE.
4741 This is called by one_pre_gcse_pass after all the dataflow analysis
4742 has been done.
4743
a65f3558
JL
4744 This is based on the original Morel-Renvoise paper Fred Chow's thesis,
4745 and lazy code motion from Knoop, Ruthing and Steffen as described in
4746 Advanced Compiler Design and Implementation.
7506f491
DE
4747
4748 ??? A new pseudo reg is created to hold the reaching expression.
4749 The nice thing about the classical approach is that it would try to
4750 use an existing reg. If the register can't be adequately optimized
4751 [i.e. we introduce reload problems], one could add a pass here to
4752 propagate the new register through the block.
4753
4754 ??? We don't handle single sets in PARALLELs because we're [currently]
4755 not able to copy the rest of the parallel when we insert copies to create
4756 full redundancies from partial redundancies. However, there's no reason
4757 why we can't handle PARALLELs in the cases where there are no partial
4758 redundancies. */
4759
4760static int
4761pre_gcse ()
4762{
a42cd965 4763 int i, did_insert;
7506f491
DE
4764 int changed;
4765 struct expr **index_map;
4766
4767 /* Compute a mapping from expression number (`bitmap_index') to
4768 hash table entry. */
4769
283a2545 4770 index_map = xcalloc (n_exprs, sizeof (struct expr *));
7506f491
DE
4771 for (i = 0; i < expr_hash_table_size; i++)
4772 {
4773 struct expr *expr;
4774
4775 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
4776 index_map[expr->bitmap_index] = expr;
4777 }
4778
4779 /* Reset bitmap used to track which insns are redundant. */
a65f3558
JL
4780 pre_redundant_insns = sbitmap_alloc (max_cuid);
4781 sbitmap_zero (pre_redundant_insns);
7506f491
DE
4782
4783 /* Delete the redundant insns first so that
4784 - we know what register to use for the new insns and for the other
4785 ones with reaching expressions
4786 - we know which insns are redundant when we go to create copies */
4787 changed = pre_delete ();
4788
a42cd965 4789 did_insert = pre_edge_insert (edge_list, index_map);
7506f491 4790 /* In other places with reaching expressions, copy the expression to the
a42cd965 4791 specially allocated pseudo-reg that reaches the redundant expr. */
7506f491 4792 pre_insert_copies ();
a42cd965
AM
4793 if (did_insert)
4794 {
4795 commit_edge_insertions ();
4796 changed = 1;
4797 }
7506f491 4798
283a2545 4799 free (index_map);
a65f3558 4800 free (pre_redundant_insns);
7506f491
DE
4801
4802 return changed;
4803}
4804
4805/* Top level routine to perform one PRE GCSE pass.
4806
4807 Return non-zero if a change was made. */
4808
4809static int
b5ce41ff 4810one_pre_gcse_pass (pass)
7506f491
DE
4811 int pass;
4812{
4813 int changed = 0;
4814
4815 gcse_subst_count = 0;
4816 gcse_create_count = 0;
4817
4818 alloc_expr_hash_table (max_cuid);
a42cd965 4819 add_noreturn_fake_exit_edges ();
b5ce41ff 4820 compute_expr_hash_table ();
7506f491
DE
4821 if (gcse_file)
4822 dump_hash_table (gcse_file, "Expression", expr_hash_table,
4823 expr_hash_table_size, n_exprs);
4824 if (n_exprs > 0)
4825 {
4826 alloc_pre_mem (n_basic_blocks, n_exprs);
4827 compute_pre_data ();
4828 changed |= pre_gcse ();
a42cd965 4829 free_edge_list (edge_list);
7506f491
DE
4830 free_pre_mem ();
4831 }
a42cd965 4832 remove_fake_edges ();
7506f491
DE
4833 free_expr_hash_table ();
4834
4835 if (gcse_file)
4836 {
4837 fprintf (gcse_file, "\n");
4838 fprintf (gcse_file, "PRE GCSE of %s, pass %d: %d bytes needed, %d substs, %d insns created\n",
4839 current_function_name, pass,
4840 bytes_used, gcse_subst_count, gcse_create_count);
4841 }
4842
4843 return changed;
4844}
aeb2f500
JW
4845\f
4846/* If X contains any LABEL_REF's, add REG_LABEL notes for them to INSN.
4847 We have to add REG_LABEL notes, because the following loop optimization
4848 pass requires them. */
4849
4850/* ??? This is very similar to the loop.c add_label_notes function. We
4851 could probably share code here. */
4852
4853/* ??? If there was a jump optimization pass after gcse and before loop,
4854 then we would not need to do this here, because jump would add the
4855 necessary REG_LABEL notes. */
4856
4857static void
4858add_label_notes (x, insn)
4859 rtx x;
4860 rtx insn;
4861{
4862 enum rtx_code code = GET_CODE (x);
4863 int i, j;
6f7d635c 4864 const char *fmt;
aeb2f500
JW
4865
4866 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
4867 {
6b3603c2 4868 /* This code used to ignore labels that referred to dispatch tables to
ac7c5af5 4869 avoid flow generating (slighly) worse code.
6b3603c2 4870
ac7c5af5
JL
4871 We no longer ignore such label references (see LABEL_REF handling in
4872 mark_jump_label for additional information). */
6b3603c2
JL
4873 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL, XEXP (x, 0),
4874 REG_NOTES (insn));
aeb2f500
JW
4875 return;
4876 }
4877
4878 fmt = GET_RTX_FORMAT (code);
4879 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4880 {
4881 if (fmt[i] == 'e')
4882 add_label_notes (XEXP (x, i), insn);
4883 else if (fmt[i] == 'E')
4884 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4885 add_label_notes (XVECEXP (x, i, j), insn);
4886 }
4887}
a65f3558
JL
4888
4889/* Compute transparent outgoing information for each block.
4890
4891 An expression is transparent to an edge unless it is killed by
4892 the edge itself. This can only happen with abnormal control flow,
4893 when the edge is traversed through a call. This happens with
4894 non-local labels and exceptions.
4895
4896 This would not be necessary if we split the edge. While this is
4897 normally impossible for abnormal critical edges, with some effort
4898 it should be possible with exception handling, since we still have
4899 control over which handler should be invoked. But due to increased
4900 EH table sizes, this may not be worthwhile. */
4901
4902static void
4903compute_transpout ()
4904{
4905 int bb;
4906
4907 sbitmap_vector_ones (transpout, n_basic_blocks);
4908
4909 for (bb = 0; bb < n_basic_blocks; ++bb)
4910 {
4911 int i;
4912
4913 /* Note that flow inserted a nop a the end of basic blocks that
4914 end in call instructions for reasons other than abnormal
4915 control flow. */
4916 if (GET_CODE (BLOCK_END (bb)) != CALL_INSN)
4917 continue;
4918
4919 for (i = 0; i < expr_hash_table_size; i++)
4920 {
4921 struct expr *expr;
4922 for (expr = expr_hash_table[i]; expr ; expr = expr->next_same_hash)
4923 if (GET_CODE (expr->expr) == MEM)
4924 {
4925 rtx addr = XEXP (expr->expr, 0);
4926
4927 if (GET_CODE (addr) == SYMBOL_REF
4928 && CONSTANT_POOL_ADDRESS_P (addr))
4929 continue;
4930
4931 /* ??? Optimally, we would use interprocedural alias
4932 analysis to determine if this mem is actually killed
4933 by this call. */
4934 RESET_BIT (transpout[bb], expr->bitmap_index);
4935 }
4936 }
4937 }
4938}
dfdb644f
JL
4939
4940/* Removal of useless null pointer checks */
4941
dfdb644f 4942/* Called via note_stores. X is set by SETTER. If X is a register we must
0511851c
MM
4943 invalidate nonnull_local and set nonnull_killed. DATA is really a
4944 `null_pointer_info *'.
dfdb644f
JL
4945
4946 We ignore hard registers. */
4947static void
84832317 4948invalidate_nonnull_info (x, setter, data)
dfdb644f
JL
4949 rtx x;
4950 rtx setter ATTRIBUTE_UNUSED;
0511851c 4951 void *data;
dfdb644f
JL
4952{
4953 int offset, regno;
0511851c 4954 struct null_pointer_info* npi = (struct null_pointer_info *) data;
dfdb644f
JL
4955
4956 offset = 0;
4957 while (GET_CODE (x) == SUBREG)
4958 x = SUBREG_REG (x);
4959
4960 /* Ignore anything that is not a register or is a hard register. */
4961 if (GET_CODE (x) != REG
0511851c
MM
4962 || REGNO (x) < npi->min_reg
4963 || REGNO (x) >= npi->max_reg)
dfdb644f
JL
4964 return;
4965
0511851c 4966 regno = REGNO (x) - npi->min_reg;
dfdb644f 4967
0511851c
MM
4968 RESET_BIT (npi->nonnull_local[npi->current_block], regno);
4969 SET_BIT (npi->nonnull_killed[npi->current_block], regno);
dfdb644f
JL
4970}
4971
0511851c
MM
4972/* Do null-pointer check elimination for the registers indicated in
4973 NPI. NONNULL_AVIN and NONNULL_AVOUT are pre-allocated sbitmaps;
4974 they are not our responsibility to free. */
dfdb644f 4975
0511851c 4976static void
b71a2ff8 4977delete_null_pointer_checks_1 (block_reg, nonnull_avin, nonnull_avout, npi)
0511851c
MM
4978 int *block_reg;
4979 sbitmap *nonnull_avin;
4980 sbitmap *nonnull_avout;
4981 struct null_pointer_info *npi;
dfdb644f 4982{
ce724250 4983 int bb;
0511851c
MM
4984 int current_block;
4985 sbitmap *nonnull_local = npi->nonnull_local;
4986 sbitmap *nonnull_killed = npi->nonnull_killed;
dfdb644f 4987
dfdb644f
JL
4988 /* Compute local properties, nonnull and killed. A register will have
4989 the nonnull property if at the end of the current block its value is
4990 known to be nonnull. The killed property indicates that somewhere in
4991 the block any information we had about the register is killed.
4992
4993 Note that a register can have both properties in a single block. That
4994 indicates that it's killed, then later in the block a new value is
4995 computed. */
4996 sbitmap_vector_zero (nonnull_local, n_basic_blocks);
4997 sbitmap_vector_zero (nonnull_killed, n_basic_blocks);
4998 for (current_block = 0; current_block < n_basic_blocks; current_block++)
4999 {
5000 rtx insn, stop_insn;
5001
0511851c
MM
5002 /* Set the current block for invalidate_nonnull_info. */
5003 npi->current_block = current_block;
5004
dfdb644f
JL
5005 /* Scan each insn in the basic block looking for memory references and
5006 register sets. */
5007 stop_insn = NEXT_INSN (BLOCK_END (current_block));
5008 for (insn = BLOCK_HEAD (current_block);
5009 insn != stop_insn;
5010 insn = NEXT_INSN (insn))
5011 {
5012 rtx set;
0511851c 5013 rtx reg;
dfdb644f
JL
5014
5015 /* Ignore anything that is not a normal insn. */
5016 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
5017 continue;
5018
5019 /* Basically ignore anything that is not a simple SET. We do have
5020 to make sure to invalidate nonnull_local and set nonnull_killed
5021 for such insns though. */
5022 set = single_set (insn);
5023 if (!set)
5024 {
0511851c 5025 note_stores (PATTERN (insn), invalidate_nonnull_info, npi);
dfdb644f
JL
5026 continue;
5027 }
5028
5029 /* See if we've got a useable memory load. We handle it first
5030 in case it uses its address register as a dest (which kills
5031 the nonnull property). */
5032 if (GET_CODE (SET_SRC (set)) == MEM
0511851c
MM
5033 && GET_CODE ((reg = XEXP (SET_SRC (set), 0))) == REG
5034 && REGNO (reg) >= npi->min_reg
5035 && REGNO (reg) < npi->max_reg)
dfdb644f 5036 SET_BIT (nonnull_local[current_block],
0511851c 5037 REGNO (reg) - npi->min_reg);
dfdb644f
JL
5038
5039 /* Now invalidate stuff clobbered by this insn. */
0511851c 5040 note_stores (PATTERN (insn), invalidate_nonnull_info, npi);
dfdb644f
JL
5041
5042 /* And handle stores, we do these last since any sets in INSN can
5043 not kill the nonnull property if it is derived from a MEM
5044 appearing in a SET_DEST. */
5045 if (GET_CODE (SET_DEST (set)) == MEM
0511851c
MM
5046 && GET_CODE ((reg = XEXP (SET_DEST (set), 0))) == REG
5047 && REGNO (reg) >= npi->min_reg
5048 && REGNO (reg) < npi->max_reg)
dfdb644f 5049 SET_BIT (nonnull_local[current_block],
0511851c 5050 REGNO (reg) - npi->min_reg);
dfdb644f
JL
5051 }
5052 }
5053
5054 /* Now compute global properties based on the local properties. This
5055 is a classic global availablity algorithm. */
ce724250
JL
5056 compute_available (nonnull_local, nonnull_killed,
5057 nonnull_avout, nonnull_avin);
dfdb644f
JL
5058
5059 /* Now look at each bb and see if it ends with a compare of a value
5060 against zero. */
5061 for (bb = 0; bb < n_basic_blocks; bb++)
5062 {
5063 rtx last_insn = BLOCK_END (bb);
0511851c 5064 rtx condition, earliest;
dfdb644f
JL
5065 int compare_and_branch;
5066
0511851c
MM
5067 /* Since MIN_REG is always at least FIRST_PSEUDO_REGISTER, and
5068 since BLOCK_REG[BB] is zero if this block did not end with a
5069 comparison against zero, this condition works. */
5070 if (block_reg[bb] < npi->min_reg
5071 || block_reg[bb] >= npi->max_reg)
dfdb644f
JL
5072 continue;
5073
5074 /* LAST_INSN is a conditional jump. Get its condition. */
5075 condition = get_condition (last_insn, &earliest);
5076
dfdb644f 5077 /* Is the register known to have a nonzero value? */
0511851c 5078 if (!TEST_BIT (nonnull_avout[bb], block_reg[bb] - npi->min_reg))
dfdb644f
JL
5079 continue;
5080
5081 /* Try to compute whether the compare/branch at the loop end is one or
5082 two instructions. */
5083 if (earliest == last_insn)
5084 compare_and_branch = 1;
5085 else if (earliest == prev_nonnote_insn (last_insn))
5086 compare_and_branch = 2;
5087 else
5088 continue;
5089
5090 /* We know the register in this comparison is nonnull at exit from
5091 this block. We can optimize this comparison. */
5092 if (GET_CODE (condition) == NE)
5093 {
5094 rtx new_jump;
5095
5096 new_jump = emit_jump_insn_before (gen_jump (JUMP_LABEL (last_insn)),
5097 last_insn);
5098 JUMP_LABEL (new_jump) = JUMP_LABEL (last_insn);
5099 LABEL_NUSES (JUMP_LABEL (new_jump))++;
5100 emit_barrier_after (new_jump);
5101 }
5102 delete_insn (last_insn);
5103 if (compare_and_branch == 2)
5104 delete_insn (earliest);
0511851c
MM
5105
5106 /* Don't check this block again. (Note that BLOCK_END is
5107 invalid here; we deleted the last instruction in the
5108 block.) */
5109 block_reg[bb] = 0;
5110 }
5111}
5112
5113/* Find EQ/NE comparisons against zero which can be (indirectly) evaluated
5114 at compile time.
5115
5116 This is conceptually similar to global constant/copy propagation and
5117 classic global CSE (it even uses the same dataflow equations as cprop).
5118
5119 If a register is used as memory address with the form (mem (reg)), then we
5120 know that REG can not be zero at that point in the program. Any instruction
5121 which sets REG "kills" this property.
5122
5123 So, if every path leading to a conditional branch has an available memory
5124 reference of that form, then we know the register can not have the value
5125 zero at the conditional branch.
5126
5127 So we merely need to compute the local properies and propagate that data
5128 around the cfg, then optimize where possible.
5129
5130 We run this pass two times. Once before CSE, then again after CSE. This
5131 has proven to be the most profitable approach. It is rare for new
5132 optimization opportunities of this nature to appear after the first CSE
5133 pass.
5134
5135 This could probably be integrated with global cprop with a little work. */
5136
5137void
5138delete_null_pointer_checks (f)
5139 rtx f;
5140{
0511851c
MM
5141 sbitmap *nonnull_avin, *nonnull_avout;
5142 int *block_reg;
5143 int bb;
5144 int reg;
5145 int regs_per_pass;
5146 int max_reg;
5147 struct null_pointer_info npi;
5148
5149 /* First break the program into basic blocks. */
5150 find_basic_blocks (f, max_reg_num (), NULL, 1);
5151
5152 /* If we have only a single block, then there's nothing to do. */
5153 if (n_basic_blocks <= 1)
5154 {
5155 /* Free storage allocated by find_basic_blocks. */
5156 free_basic_block_vars (0);
5157 return;
5158 }
5159
5160 /* Trying to perform global optimizations on flow graphs which have
5161 a high connectivity will take a long time and is unlikely to be
5162 particularly useful.
5163
5164 In normal circumstances a cfg should have about twice has many edges
5165 as blocks. But we do not want to punish small functions which have
5166 a couple switch statements. So we require a relatively large number
5167 of basic blocks and the ratio of edges to blocks to be high. */
5168 if (n_basic_blocks > 1000 && n_edges / n_basic_blocks >= 20)
5169 {
5170 /* Free storage allocated by find_basic_blocks. */
5171 free_basic_block_vars (0);
5172 return;
5173 }
5174
0511851c
MM
5175 /* We need four bitmaps, each with a bit for each register in each
5176 basic block. */
5177 max_reg = max_reg_num ();
5178 regs_per_pass = get_bitmap_width (4, n_basic_blocks, max_reg);
5179
5180 /* Allocate bitmaps to hold local and global properties. */
5181 npi.nonnull_local = sbitmap_vector_alloc (n_basic_blocks, regs_per_pass);
5182 npi.nonnull_killed = sbitmap_vector_alloc (n_basic_blocks, regs_per_pass);
5183 nonnull_avin = sbitmap_vector_alloc (n_basic_blocks, regs_per_pass);
5184 nonnull_avout = sbitmap_vector_alloc (n_basic_blocks, regs_per_pass);
5185
5186 /* Go through the basic blocks, seeing whether or not each block
5187 ends with a conditional branch whose condition is a comparison
5188 against zero. Record the register compared in BLOCK_REG. */
5189 block_reg = (int *) xcalloc (n_basic_blocks, sizeof (int));
5190 for (bb = 0; bb < n_basic_blocks; bb++)
5191 {
5192 rtx last_insn = BLOCK_END (bb);
5193 rtx condition, earliest, reg;
5194
5195 /* We only want conditional branches. */
5196 if (GET_CODE (last_insn) != JUMP_INSN
5197 || !condjump_p (last_insn)
5198 || simplejump_p (last_insn))
5199 continue;
5200
5201 /* LAST_INSN is a conditional jump. Get its condition. */
5202 condition = get_condition (last_insn, &earliest);
5203
5204 /* If we were unable to get the condition, or it is not a equality
5205 comparison against zero then there's nothing we can do. */
5206 if (!condition
5207 || (GET_CODE (condition) != NE && GET_CODE (condition) != EQ)
5208 || GET_CODE (XEXP (condition, 1)) != CONST_INT
5209 || (XEXP (condition, 1)
5210 != CONST0_RTX (GET_MODE (XEXP (condition, 0)))))
5211 continue;
5212
5213 /* We must be checking a register against zero. */
5214 reg = XEXP (condition, 0);
5215 if (GET_CODE (reg) != REG)
5216 continue;
5217
5218 block_reg[bb] = REGNO (reg);
5219 }
5220
5221 /* Go through the algorithm for each block of registers. */
5222 for (reg = FIRST_PSEUDO_REGISTER; reg < max_reg; reg += regs_per_pass)
5223 {
5224 npi.min_reg = reg;
5225 npi.max_reg = MIN (reg + regs_per_pass, max_reg);
b71a2ff8 5226 delete_null_pointer_checks_1 (block_reg, nonnull_avin,
0511851c 5227 nonnull_avout, &npi);
dfdb644f
JL
5228 }
5229
5230 /* Free storage allocated by find_basic_blocks. */
5231 free_basic_block_vars (0);
5232
0511851c
MM
5233 /* Free the table of registers compared at the end of every block. */
5234 free (block_reg);
5235
dfdb644f 5236 /* Free bitmaps. */
0511851c
MM
5237 free (npi.nonnull_local);
5238 free (npi.nonnull_killed);
dfdb644f
JL
5239 free (nonnull_avin);
5240 free (nonnull_avout);
5241}
bb457bd9
JL
5242
5243/* Code Hoisting variables and subroutines. */
5244
5245/* Very busy expressions. */
5246static sbitmap *hoist_vbein;
5247static sbitmap *hoist_vbeout;
5248
5249/* Hoistable expressions. */
5250static sbitmap *hoist_exprs;
5251
5252/* Dominator bitmaps. */
5253static sbitmap *dominators;
bb457bd9
JL
5254
5255/* ??? We could compute post dominators and run this algorithm in
5256 reverse to to perform tail merging, doing so would probably be
5257 more effective than the tail merging code in jump.c.
5258
5259 It's unclear if tail merging could be run in parallel with
5260 code hoisting. It would be nice. */
5261
5262/* Allocate vars used for code hoisting analysis. */
5263
5264static void
5265alloc_code_hoist_mem (n_blocks, n_exprs)
5266 int n_blocks, n_exprs;
5267{
5268 antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
5269 transp = sbitmap_vector_alloc (n_blocks, n_exprs);
5270 comp = sbitmap_vector_alloc (n_blocks, n_exprs);
5271
5272 hoist_vbein = sbitmap_vector_alloc (n_blocks, n_exprs);
5273 hoist_vbeout = sbitmap_vector_alloc (n_blocks, n_exprs);
5274 hoist_exprs = sbitmap_vector_alloc (n_blocks, n_exprs);
5275 transpout = sbitmap_vector_alloc (n_blocks, n_exprs);
5276
5277 dominators = sbitmap_vector_alloc (n_blocks, n_blocks);
bb457bd9
JL
5278}
5279
5280/* Free vars used for code hoisting analysis. */
5281
5282static void
5283free_code_hoist_mem ()
5284{
5285 free (antloc);
5286 free (transp);
5287 free (comp);
5288
5289 free (hoist_vbein);
5290 free (hoist_vbeout);
5291 free (hoist_exprs);
5292 free (transpout);
5293
5294 free (dominators);
bb457bd9
JL
5295}
5296
5297/* Compute the very busy expressions at entry/exit from each block.
5298
5299 An expression is very busy if all paths from a given point
5300 compute the expression. */
5301
5302static void
5303compute_code_hoist_vbeinout ()
5304{
5305 int bb, changed, passes;
5306
5307 sbitmap_vector_zero (hoist_vbeout, n_basic_blocks);
5308 sbitmap_vector_zero (hoist_vbein, n_basic_blocks);
5309
5310 passes = 0;
5311 changed = 1;
5312 while (changed)
5313 {
5314 changed = 0;
5315 /* We scan the blocks in the reverse order to speed up
5316 the convergence. */
5317 for (bb = n_basic_blocks - 1; bb >= 0; bb--)
5318 {
5319 changed |= sbitmap_a_or_b_and_c (hoist_vbein[bb], antloc[bb],
5320 hoist_vbeout[bb], transp[bb]);
5321 if (bb != n_basic_blocks - 1)
a42cd965 5322 sbitmap_intersection_of_succs (hoist_vbeout[bb], hoist_vbein, bb);
bb457bd9
JL
5323 }
5324 passes++;
5325 }
5326
5327 if (gcse_file)
5328 fprintf (gcse_file, "hoisting vbeinout computation: %d passes\n", passes);
5329}
5330
5331/* Top level routine to do the dataflow analysis needed by code hoisting. */
5332
5333static void
5334compute_code_hoist_data ()
5335{
5336 compute_local_properties (transp, comp, antloc, 0);
5337 compute_transpout ();
5338 compute_code_hoist_vbeinout ();
092ae4ba 5339 compute_flow_dominators (dominators, NULL);
bb457bd9
JL
5340 if (gcse_file)
5341 fprintf (gcse_file, "\n");
5342}
5343
5344/* Determine if the expression identified by EXPR_INDEX would
5345 reach BB unimpared if it was placed at the end of EXPR_BB.
5346
5347 It's unclear exactly what Muchnick meant by "unimpared". It seems
5348 to me that the expression must either be computed or transparent in
5349 *every* block in the path(s) from EXPR_BB to BB. Any other definition
5350 would allow the expression to be hoisted out of loops, even if
5351 the expression wasn't a loop invariant.
5352
5353 Contrast this to reachability for PRE where an expression is
5354 considered reachable if *any* path reaches instead of *all*
5355 paths. */
5356
5357static int
5358hoist_expr_reaches_here_p (expr_bb, expr_index, bb, visited)
5359 int expr_bb;
5360 int expr_index;
5361 int bb;
5362 char *visited;
5363{
5364 edge pred;
283a2545
RL
5365 int visited_allocated_locally = 0;
5366
bb457bd9
JL
5367
5368 if (visited == NULL)
5369 {
283a2545
RL
5370 visited_allocated_locally = 1;
5371 visited = xcalloc (n_basic_blocks, 1);
bb457bd9
JL
5372 }
5373
5374 visited[expr_bb] = 1;
5375 for (pred = BASIC_BLOCK (bb)->pred; pred != NULL; pred = pred->pred_next)
5376 {
5377 int pred_bb = pred->src->index;
5378
5379 if (pred->src == ENTRY_BLOCK_PTR)
5380 break;
5381 else if (visited[pred_bb])
5382 continue;
5383 /* Does this predecessor generate this expression? */
5384 else if (TEST_BIT (comp[pred_bb], expr_index))
5385 break;
5386 else if (! TEST_BIT (transp[pred_bb], expr_index))
5387 break;
5388 /* Not killed. */
5389 else
5390 {
5391 visited[pred_bb] = 1;
5392 if (! hoist_expr_reaches_here_p (expr_bb, expr_index,
5393 pred_bb, visited))
5394 break;
5395 }
5396 }
283a2545
RL
5397 if (visited_allocated_locally)
5398 free (visited);
bb457bd9
JL
5399 return (pred == NULL);
5400}
5401\f
5402/* Actually perform code hoisting. */
5403static void
5404hoist_code ()
5405{
5406 int bb, dominated, i;
5407 struct expr **index_map;
5408
5409 sbitmap_vector_zero (hoist_exprs, n_basic_blocks);
5410
5411 /* Compute a mapping from expression number (`bitmap_index') to
5412 hash table entry. */
5413
283a2545 5414 index_map = xcalloc (n_exprs, sizeof (struct expr *));
bb457bd9
JL
5415 for (i = 0; i < expr_hash_table_size; i++)
5416 {
5417 struct expr *expr;
5418
5419 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
5420 index_map[expr->bitmap_index] = expr;
5421 }
5422
5423 /* Walk over each basic block looking for potentially hoistable
5424 expressions, nothing gets hoisted from the entry block. */
5425 for (bb = 0; bb < n_basic_blocks; bb++)
5426 {
5427 int found = 0;
5428 int insn_inserted_p;
5429
5430 /* Examine each expression that is very busy at the exit of this
5431 block. These are the potentially hoistable expressions. */
5432 for (i = 0; i < hoist_vbeout[bb]->n_bits; i++)
5433 {
5434 int hoistable = 0;
5435 if (TEST_BIT (hoist_vbeout[bb], i)
5436 && TEST_BIT (transpout[bb], i))
5437 {
5438 /* We've found a potentially hoistable expression, now
5439 we look at every block BB dominates to see if it
5440 computes the expression. */
5441 for (dominated = 0; dominated < n_basic_blocks; dominated++)
5442 {
5443 /* Ignore self dominance. */
5444 if (bb == dominated
5445 || ! TEST_BIT (dominators[dominated], bb))
5446 continue;
5447
5448 /* We've found a dominated block, now see if it computes
5449 the busy expression and whether or not moving that
5450 expression to the "beginning" of that block is safe. */
5451 if (!TEST_BIT (antloc[dominated], i))
5452 continue;
5453
5454 /* Note if the expression would reach the dominated block
5455 unimpared if it was placed at the end of BB.
5456
5457 Keep track of how many times this expression is hoistable
5458 from a dominated block into BB. */
5459 if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
5460 hoistable++;
5461 }
5462
5463 /* If we found more than one hoistable occurence of this
5464 expression, then note it in the bitmap of expressions to
5465 hoist. It makes no sense to hoist things which are computed
5466 in only one BB, and doing so tends to pessimize register
5467 allocation. One could increase this value to try harder
5468 to avoid any possible code expansion due to register
5469 allocation issues; however experiments have shown that
5470 the vast majority of hoistable expressions are only movable
5471 from two successors, so raising this threshhold is likely
5472 to nullify any benefit we get from code hoisting. */
5473 if (hoistable > 1)
5474 {
5475 SET_BIT (hoist_exprs[bb], i);
5476 found = 1;
5477 }
5478 }
5479 }
5480
5481 /* If we found nothing to hoist, then quit now. */
5482 if (! found)
5483 continue;
5484
5485 /* Loop over all the hoistable expressions. */
5486 for (i = 0; i < hoist_exprs[bb]->n_bits; i++)
5487 {
5488 /* We want to insert the expression into BB only once, so
5489 note when we've inserted it. */
5490 insn_inserted_p = 0;
5491
5492 /* These tests should be the same as the tests above. */
5493 if (TEST_BIT (hoist_vbeout[bb], i))
5494 {
5495 /* We've found a potentially hoistable expression, now
5496 we look at every block BB dominates to see if it
5497 computes the expression. */
5498 for (dominated = 0; dominated < n_basic_blocks; dominated++)
5499 {
5500 /* Ignore self dominance. */
5501 if (bb == dominated
5502 || ! TEST_BIT (dominators[dominated], bb))
5503 continue;
5504
5505 /* We've found a dominated block, now see if it computes
5506 the busy expression and whether or not moving that
5507 expression to the "beginning" of that block is safe. */
5508 if (!TEST_BIT (antloc[dominated], i))
5509 continue;
5510
5511 /* The expression is computed in the dominated block and
5512 it would be safe to compute it at the start of the
5513 dominated block. Now we have to determine if the
5514 expresion would reach the dominated block if it was
5515 placed at the end of BB. */
5516 if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
5517 {
5518 struct expr *expr = index_map[i];
5519 struct occr *occr = expr->antic_occr;
5520 rtx insn;
5521 rtx set;
5522
5523
5524 /* Find the right occurence of this expression. */
5525 while (BLOCK_NUM (occr->insn) != dominated && occr)
5526 occr = occr->next;
5527
5528 /* Should never happen. */
5529 if (!occr)
5530 abort ();
5531
5532 insn = occr->insn;
5533
5534 set = single_set (insn);
5535 if (! set)
5536 abort ();
5537
5538 /* Create a pseudo-reg to store the result of reaching
5539 expressions into. Get the mode for the new pseudo
5540 from the mode of the original destination pseudo. */
5541 if (expr->reaching_reg == NULL)
5542 expr->reaching_reg
5543 = gen_reg_rtx (GET_MODE (SET_DEST (set)));
5544
5545 /* In theory this should never fail since we're creating
5546 a reg->reg copy.
5547
5548 However, on the x86 some of the movXX patterns actually
5549 contain clobbers of scratch regs. This may cause the
5550 insn created by validate_change to not match any
5551 pattern and thus cause validate_change to fail. */
5552 if (validate_change (insn, &SET_SRC (set),
5553 expr->reaching_reg, 0))
5554 {
5555 occr->deleted_p = 1;
5556 if (!insn_inserted_p)
5557 {
5558 insert_insn_end_bb (index_map[i], bb, 0);
5559 insn_inserted_p = 1;
5560 }
5561 }
5562 }
5563 }
5564 }
5565 }
5566 }
283a2545 5567 free (index_map);
bb457bd9
JL
5568}
5569
5570/* Top level routine to perform one code hoisting (aka unification) pass
5571
5572 Return non-zero if a change was made. */
5573
5574static int
5575one_code_hoisting_pass ()
5576{
5577 int changed = 0;
5578
5579 alloc_expr_hash_table (max_cuid);
5580 compute_expr_hash_table ();
5581 if (gcse_file)
5582 dump_hash_table (gcse_file, "Code Hosting Expressions", expr_hash_table,
5583 expr_hash_table_size, n_exprs);
5584 if (n_exprs > 0)
5585 {
5586 alloc_code_hoist_mem (n_basic_blocks, n_exprs);
5587 compute_code_hoist_data ();
5588 hoist_code ();
5589 free_code_hoist_mem ();
5590 }
5591 free_expr_hash_table ();
5592
5593 return changed;
5594}