* haifa-sched.c (split_edges): Pass edgeset_size as second arg to
[official-gcc.git] / gcc / gcse.c
blob4de6083649b51abefb97608f71dab8f2baae55ff
1 /* Global common subexpression elimination/Partial redundancy elimination
2 and global constant/copy propagation for GNU compiler.
3 Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
22 /* TODO
23 - reordering of memory allocation and freeing to be more space efficient
24 - do rough calc of how many regs are needed in each block, and a rough
25 calc of how many regs are available in each class and use that to
26 throttle back the code in cases where RTX_COST is minimal.
27 - dead store elimination
28 - a store to the same address as a load does not kill the load if the
29 source of the store is also the destination of the load. Handling this
30 allows more load motion, particularly out of loops.
31 - ability to realloc sbitmap vectors would allow one initial computation
32 of reg_set_in_block with only subsequent additions, rather than
33 recomputing it for each pass
37 /* References searched while implementing this.
39 Compilers Principles, Techniques and Tools
40 Aho, Sethi, Ullman
41 Addison-Wesley, 1988
43 Global Optimization by Suppression of Partial Redundancies
44 E. Morel, C. Renvoise
45 communications of the acm, Vol. 22, Num. 2, Feb. 1979
47 A Portable Machine-Independent Global Optimizer - Design and Measurements
48 Frederick Chow
49 Stanford Ph.D. thesis, Dec. 1983
51 A Fast Algorithm for Code Movement Optimization
52 D.M. Dhamdhere
53 SIGPLAN Notices, Vol. 23, Num. 10, Oct. 1988
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
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
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
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
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
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
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
89 Partial Dead Code Elimination
90 J. Knoop, O. Ruthing, B. Steffen
91 ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
93 Effective Partial Redundancy Elimination
94 P. Briggs, K.D. Cooper
95 ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
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
101 Optimal Code Motion: Theory and Practice
102 J. Knoop, O. Ruthing, B. Steffen
103 ACM TOPLAS, Vol. 16, Num. 4, Jul. 1994
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
109 Global code motion / global value numbering
110 C. Click
111 ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
113 Value Driven Redundancy Elimination
114 L.T. Simpson
115 Rice University Ph.D. thesis, Apr. 1996
117 Value Numbering
118 L.T. Simpson
119 Massively Scalar Compiler Project, Rice University, Sep. 1996
121 High Performance Compilers for Parallel Computing
122 Michael Wolfe
123 Addison-Wesley, 1996
125 Advanced Compiler Design and Implementation
126 Steven Muchnick
127 Morgan Kaufmann, 1997
129 Building an Optimizing Compiler
130 Robert Morgan
131 Digital Press, 1998
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
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
142 People wishing to do something different can find various possibilities
143 in the above papers and elsewhere.
146 #include "config.h"
147 #include "system.h"
148 #include "toplev.h"
150 #include "rtl.h"
151 #include "tm_p.h"
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"
159 #include "output.h"
160 #include "function.h"
161 #include "expr.h"
163 #include "obstack.h"
164 #define obstack_chunk_alloc gmalloc
165 #define obstack_chunk_free free
167 /* Maximum number of passes to perform. */
168 #define MAX_PASSES 1
170 /* Propagate flow information through back edges and thus enable PRE's
171 moving loop invariant calculations out of loops.
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.
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
183 /* We support GCSE via Partial Redundancy Elimination. PRE optimizations
184 are a superset of those done by GCSE.
186 We perform the following steps:
188 1) Compute basic block information.
190 2) Compute table of places where registers are set.
192 3) Perform copy/constant propagation.
194 4) Perform global cse.
196 5) Perform another pass of copy/constant propagation.
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).
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.
209 PRE handles moving invariant expressions out of loops (by treating them as
210 partially redundant).
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.
216 **********************
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.
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
229 It was found doing copy propagation between each pass enables further
230 substitutions.
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.
236 **********************
238 The steps for PRE are:
240 1) Build the hash table of expressions we wish to GCSE (expr_hash_table).
242 2) Perform the data flow analysis for PRE.
244 3) Delete the redundant instructions
246 4) Insert the required copies [if any] that make the partially
247 redundant instructions fully redundant.
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.
252 The deletion is done first so that when we do insertions we
253 know which pseudo reg to use.
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).
259 PRE GCSE depends heavily on the second CSE pass to clean up the copies
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.
266 **********************
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.
276 Help stamp out big monolithic functions! */
278 /* GCSE global vars. */
280 /* -dG dump file. */
281 static FILE *gcse_file;
283 /* Note whether or not we should run jump optimization after gcse. We
284 want to do this for two cases.
286 * If we changed any jumps via cprop.
288 * If we added any labels via edge splitting. */
290 static int run_jump_opt_after_gcse;
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. */
297 static FILE *debug_stderr;
299 /* An obstack for our working variables. */
300 static struct obstack gcse_obstack;
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. */
305 static char can_copy_p[(int) NUM_MACHINE_MODES];
307 /* Non-zero if can_copy_p has been initialized. */
308 static int can_copy_init_p;
310 struct reg_use {
311 rtx reg_rtx;
314 /* Hash table of expressions. */
316 struct expr
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
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. */
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;
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]. */
345 struct occr
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;
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.
370 /* Total size of the expression hash table, in elements. */
371 static int expr_hash_table_size;
372 /* The table itself.
373 This is an array of `expr_hash_table_size' elements. */
374 static struct expr **expr_hash_table;
376 /* Total size of the copy propagation hash table, in elements. */
377 static int set_hash_table_size;
378 /* The table itself.
379 This is an array of `set_hash_table_size' elements. */
380 static struct expr **set_hash_table;
382 /* Mapping of uids to cuids.
383 Only real insns get cuids. */
384 static int *uid_cuid;
386 /* Highest UID in UID_CUID. */
387 static int max_uid;
389 /* Get the cuid of an insn. */
390 #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
392 /* Number of cuids. */
393 static int max_cuid;
395 /* Mapping of cuids to insns. */
396 static rtx *cuid_insn;
398 /* Get insn from cuid. */
399 #define CUID_INSN(CUID) (cuid_insn[CUID])
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. */
404 static int max_gcse_regno;
406 /* Maximum number of cse-able expressions found. */
407 static int n_exprs;
408 /* Maximum number of assignments for copy propagation found. */
409 static int n_sets;
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.
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
417 a bitmap instead of the lists `reg_set_table' uses].
419 `reg_set_table' and could be turned into an array of bitmaps
420 (num-bbs x num-regs)
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. */
435 typedef 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;
441 static 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. */
445 static int reg_set_table_size;
446 /* Amount to grow `reg_set_table' by when it's full. */
447 #define REG_SET_TABLE_SLOP 100
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. */
452 static sbitmap reg_set_bitmap;
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. */
459 static sbitmap *reg_set_in_block;
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. */
468 static char *mem_set_in_block;
470 /* Various variables for statistics gathering. */
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. */
475 static int bytes_used;
476 /* GCSE substitutions made. */
477 static int gcse_subst_count;
478 /* Number of copy instructions created. */
479 static int gcse_create_count;
480 /* Number of constants propagated. */
481 static int const_prop_count;
482 /* Number of copys propagated. */
483 static int copy_prop_count;
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. */
489 /* A bitmap of all ones for implementing the algorithm for available
490 expressions and reaching definitions. */
491 /* ??? Available expression bitmaps have a different size than reaching
492 definition bitmaps. This should be the larger of the two, however, it
493 is not currently used for reaching definitions. */
494 static sbitmap u_bitmap;
496 /* Each block has a bitmap of each type.
497 The length of each blocks bitmap is:
499 max_cuid - for reaching definitions
500 n_exprs - for available expressions
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]
507 /* For reaching defs */
508 static sbitmap *rd_kill, *rd_gen, *reaching_defs, *rd_out;
510 /* for available exprs */
511 static sbitmap *ae_kill, *ae_gen, *ae_in, *ae_out;
513 /* Objects of this type are passed around by the null-pointer check
514 removal routines. */
515 struct 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;
526 static void compute_can_copy PROTO ((void));
528 static char *gmalloc PROTO ((unsigned int));
529 static char *grealloc PROTO ((char *, unsigned int));
530 static char *gcse_alloc PROTO ((unsigned long));
531 static void alloc_gcse_mem PROTO ((rtx));
532 static void free_gcse_mem PROTO ((void));
533 static void alloc_reg_set_mem PROTO ((int));
534 static void free_reg_set_mem PROTO ((void));
535 static int get_bitmap_width PROTO ((int, int, int));
536 static void record_one_set PROTO ((int, rtx));
537 static void record_set_info PROTO ((rtx, rtx, void *));
538 static void compute_sets PROTO ((rtx));
540 static void hash_scan_insn PROTO ((rtx, int, int));
541 static void hash_scan_set PROTO ((rtx, rtx, int));
542 static void hash_scan_clobber PROTO ((rtx, rtx));
543 static void hash_scan_call PROTO ((rtx, rtx));
544 static int want_to_gcse_p PROTO ((rtx));
545 static int oprs_unchanged_p PROTO ((rtx, rtx, int));
546 static int oprs_anticipatable_p PROTO ((rtx, rtx));
547 static int oprs_available_p PROTO ((rtx, rtx));
548 static void insert_expr_in_table PROTO ((rtx, enum machine_mode,
549 rtx, int, int));
550 static void insert_set_in_table PROTO ((rtx, rtx));
551 static unsigned int hash_expr PROTO ((rtx, enum machine_mode,
552 int *, int));
553 static unsigned int hash_expr_1 PROTO ((rtx, enum machine_mode, int *));
554 static unsigned int hash_set PROTO ((int, int));
555 static int expr_equiv_p PROTO ((rtx, rtx));
556 static void record_last_reg_set_info PROTO ((rtx, int));
557 static void record_last_mem_set_info PROTO ((rtx));
558 static void record_last_set_info PROTO ((rtx, rtx, void *));
559 static void compute_hash_table PROTO ((int));
560 static void alloc_set_hash_table PROTO ((int));
561 static void free_set_hash_table PROTO ((void));
562 static void compute_set_hash_table PROTO ((void));
563 static void alloc_expr_hash_table PROTO ((int));
564 static void free_expr_hash_table PROTO ((void));
565 static void compute_expr_hash_table PROTO ((void));
566 static void dump_hash_table PROTO ((FILE *, const char *, struct expr **,
567 int, int));
568 static struct expr *lookup_expr PROTO ((rtx));
569 static struct expr *lookup_set PROTO ((int, rtx));
570 static struct expr *next_set PROTO ((int, struct expr *));
571 static void reset_opr_set_tables PROTO ((void));
572 static int oprs_not_set_p PROTO ((rtx, rtx));
573 static void mark_call PROTO ((rtx));
574 static void mark_set PROTO ((rtx, rtx));
575 static void mark_clobber PROTO ((rtx, rtx));
576 static void mark_oprs_set PROTO ((rtx));
578 static void alloc_cprop_mem PROTO ((int, int));
579 static void free_cprop_mem PROTO ((void));
580 static void compute_transp PROTO ((rtx, int, sbitmap *, int));
581 static void compute_transpout PROTO ((void));
582 static void compute_local_properties PROTO ((sbitmap *, sbitmap *,
583 sbitmap *, int));
584 static void compute_cprop_data PROTO ((void));
585 static void find_used_regs PROTO ((rtx));
586 static int try_replace_reg PROTO ((rtx, rtx, rtx));
587 static struct expr *find_avail_set PROTO ((int, rtx));
588 static int cprop_jump PROTO((rtx, rtx, struct reg_use *, rtx));
589 #ifdef HAVE_cc0
590 static int cprop_cc0_jump PROTO((rtx, struct reg_use *, rtx));
591 #endif
592 static int cprop_insn PROTO ((rtx, int));
593 static int cprop PROTO ((int));
594 static int one_cprop_pass PROTO ((int, int));
596 static void alloc_pre_mem PROTO ((int, int));
597 static void free_pre_mem PROTO ((void));
598 static void compute_pre_data PROTO ((void));
599 static int pre_expr_reaches_here_p PROTO ((int, struct expr *, int));
600 static void insert_insn_end_bb PROTO ((struct expr *, int, int));
601 static void pre_insert_copy_insn PROTO ((struct expr *, rtx));
602 static void pre_insert_copies PROTO ((void));
603 static int pre_delete PROTO ((void));
604 static int pre_gcse PROTO ((void));
605 static int one_pre_gcse_pass PROTO ((int));
607 static void add_label_notes PROTO ((rtx, rtx));
609 static void alloc_code_hoist_mem PROTO ((int, int));
610 static void free_code_hoist_mem PROTO ((void));
611 static void compute_code_hoist_vbeinout PROTO ((void));
612 static void compute_code_hoist_data PROTO ((void));
613 static int hoist_expr_reaches_here_p PROTO ((int, int, int, char *));
614 static void hoist_code PROTO ((void));
615 static int one_code_hoisting_pass PROTO ((void));
617 static void alloc_rd_mem PROTO ((int, int));
618 static void free_rd_mem PROTO ((void));
619 static void handle_rd_kill_set PROTO ((rtx, int, int));
620 static void compute_kill_rd PROTO ((void));
621 static void compute_rd PROTO ((void));
622 static void alloc_avail_expr_mem PROTO ((int, int));
623 static void free_avail_expr_mem PROTO ((void));
624 static void compute_ae_gen PROTO ((void));
625 static int expr_killed_p PROTO ((rtx, int));
626 static void compute_ae_kill PROTO ((sbitmap *, sbitmap *));
627 static int expr_reaches_here_p PROTO ((struct occr *, struct expr *,
628 int, int));
629 static rtx computing_insn PROTO ((struct expr *, rtx));
630 static int def_reaches_here_p PROTO ((rtx, rtx));
631 static int can_disregard_other_sets PROTO ((struct reg_set **, rtx, int));
632 static int handle_avail_expr PROTO ((rtx, struct expr *));
633 static int classic_gcse PROTO ((void));
634 static int one_classic_gcse_pass PROTO ((int));
635 static void invalidate_nonnull_info PROTO ((rtx, rtx, void *));
636 static void delete_null_pointer_checks_1 PROTO ((int *, sbitmap *, sbitmap *,
637 struct null_pointer_info *));
638 static rtx process_insert_insn PROTO ((struct expr *));
639 static int pre_edge_insert PROTO ((struct edge_list *, struct expr **));
640 static int expr_reaches_here_p_work PROTO ((struct occr *, struct expr *, int, int, char *));
641 static int pre_expr_reaches_here_p_work PROTO ((int, struct expr *,
642 int, char *));
644 /* Entry point for global common subexpression elimination.
645 F is the first instruction in the function. */
648 gcse_main (f, file)
649 rtx f;
650 FILE *file;
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;
660 /* We do not construct an accurate cfg in functions which call
661 setjmp, so just punt to be safe. */
662 if (current_function_calls_setjmp)
663 return 0;
665 /* Assume that we do not need to run jump optimizations after gcse. */
666 run_jump_opt_after_gcse = 0;
668 /* For calling dump_foo fns from gdb. */
669 debug_stderr = stderr;
670 gcse_file = file;
672 /* Identify the basic block information for this function, including
673 successors and predecessors. */
674 max_gcse_regno = max_reg_num ();
675 find_basic_blocks (f, max_gcse_regno, file, 1);
677 if (file)
678 dump_flow_info (file);
680 /* Return if there's nothing to do. */
681 if (n_basic_blocks <= 1)
683 /* Free storage allocated by find_basic_blocks. */
684 free_basic_block_vars (0);
685 return 0;
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.
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)
698 /* Free storage allocated by find_basic_blocks. */
699 free_basic_block_vars (0);
700 return 0;
703 /* See what modes support reg/reg copy operations. */
704 if (! can_copy_init_p)
706 compute_can_copy ();
707 can_copy_init_p = 1;
710 gcc_obstack_init (&gcse_obstack);
711 bytes_used = 0;
713 /* Record where pseudo-registers are set.
714 This data is kept accurate during each pass.
715 ??? We could also record hard-reg information here
716 [since it's unchanging], however it is currently done during
717 hash table computation.
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. */
723 alloc_reg_set_mem (max_gcse_regno);
724 compute_sets (f);
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)
733 changed = 0;
734 if (file)
735 fprintf (file, "GCSE pass %d\n\n", pass + 1);
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;
741 /* Each pass may create new registers, so recalculate each time. */
742 max_gcse_regno = max_reg_num ();
744 alloc_gcse_mem (f);
746 /* Don't allow constant propagation to modify jumps
747 during this pass. */
748 changed = one_cprop_pass (pass + 1, 0);
750 if (optimize_size)
751 changed |= one_classic_gcse_pass (pass + 1);
752 else
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;
761 if (max_pass_bytes < bytes_used)
762 max_pass_bytes = bytes_used;
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. */
768 free_gcse_mem ();
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)
777 max_gcse_regno = max_reg_num ();
778 alloc_gcse_mem (f);
779 changed |= one_code_hoisting_pass ();
780 free_gcse_mem ();
782 if (max_pass_bytes < bytes_used)
783 max_pass_bytes = bytes_used;
786 if (file)
788 fprintf (file, "\n");
789 fflush (file);
791 obstack_free (&gcse_obstack, gcse_obstack_bottom);
792 pass++;
795 /* Do one last pass of copy propagation, including cprop into
796 conditional jumps. */
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 ();
804 if (file)
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);
812 /* Free our obstack. */
813 obstack_free (&gcse_obstack, NULL_PTR);
814 /* Free reg_set_table. */
815 free_reg_set_mem ();
816 /* Free storage allocated by find_basic_blocks. */
817 free_basic_block_vars (0);
818 return run_jump_opt_after_gcse;
821 /* Misc. utilities. */
823 /* Compute which modes support reg/reg copy operations. */
825 static void
826 compute_can_copy ()
828 int i;
829 #ifndef AVOID_CCMODE_COPIES
830 rtx reg,insn;
831 #endif
832 char *free_point = (char *) oballoc (1);
834 bzero (can_copy_p, NUM_MACHINE_MODES);
836 start_sequence ();
837 for (i = 0; i < NUM_MACHINE_MODES; i++)
839 switch (GET_MODE_CLASS (i))
841 case MODE_CC :
842 #ifdef AVOID_CCMODE_COPIES
843 can_copy_p[i] = 0;
844 #else
845 reg = gen_rtx_REG ((enum machine_mode) i, LAST_VIRTUAL_REGISTER + 1);
846 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, reg));
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;
856 end_sequence ();
858 /* Free the objects we just allocated. */
859 obfree (free_point);
862 /* Cover function to xmalloc to record bytes allocated. */
864 static char *
865 gmalloc (size)
866 unsigned int size;
868 bytes_used += size;
869 return xmalloc (size);
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. */
876 static char *
877 grealloc (ptr, size)
878 char *ptr;
879 unsigned int size;
881 return xrealloc (ptr, size);
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. */
888 static char *
889 gcse_alloc (size)
890 unsigned long size;
892 return (char *) obstack_alloc (&gcse_obstack, size);
895 /* Allocate memory for the cuid mapping array,
896 and reg/memory set tracking tables.
898 This is called at the start of each pass. */
900 static void
901 alloc_gcse_mem (f)
902 rtx f;
904 int i,n;
905 rtx insn;
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. */
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))
917 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
918 INSN_CUID (insn) = i++;
919 else
920 INSN_CUID (insn) = i;
923 /* Create a table mapping cuids to insns. */
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))
931 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
933 CUID_INSN (i) = insn;
934 i++;
938 /* Allocate vars to track sets of regs. */
940 reg_set_bitmap = (sbitmap) sbitmap_alloc (max_gcse_regno);
942 /* Allocate vars to track sets of regs, memory per block. */
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);
949 /* Free memory allocated by alloc_gcse_mem. */
951 static void
952 free_gcse_mem ()
954 free (uid_cuid);
955 free (cuid_insn);
957 free (reg_set_bitmap);
959 free (reg_set_in_block);
960 free (mem_set_in_block);
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.
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. */
981 static int
982 get_bitmap_width (n, x, y)
983 int n;
984 int x;
985 int y;
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;
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);
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;
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);
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.
1012 An expression is transparent in a block if its operands are not modified
1013 in the block.
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.
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.
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.
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.
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
1033 the set hash table. Additionally, TRANSP is computed as ~TRANSP,
1034 since this is really cprop's ABSALTERED. */
1036 static void
1037 compute_local_properties (transp, comp, antloc, setp)
1038 sbitmap *transp;
1039 sbitmap *comp;
1040 sbitmap *antloc;
1041 int setp;
1043 int i, hash_table_size;
1044 struct expr **hash_table;
1046 /* Initialize any bitmaps that were passed in. */
1047 if (transp)
1049 if (setp)
1050 sbitmap_vector_zero (transp, n_basic_blocks);
1051 else
1052 sbitmap_vector_ones (transp, n_basic_blocks);
1054 if (comp)
1055 sbitmap_vector_zero (comp, n_basic_blocks);
1056 if (antloc)
1057 sbitmap_vector_zero (antloc, n_basic_blocks);
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;
1065 for (i = 0; i < hash_table_size; i++)
1067 struct expr *expr;
1069 for (expr = hash_table[i]; expr != NULL; expr = expr->next_same_hash)
1071 struct occr *occr;
1072 int indx = expr->bitmap_index;
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. */
1078 if (transp)
1079 compute_transp (expr->expr, indx, transp, setp);
1081 /* The occurrences recorded in antic_occr are exactly those that
1082 we want to set to non-zero in ANTLOC. */
1084 if (antloc)
1086 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
1088 int bb = BLOCK_NUM (occr->insn);
1089 SET_BIT (antloc[bb], indx);
1091 /* While we're scanning the table, this is a good place to
1092 initialize this. */
1093 occr->deleted_p = 0;
1097 /* The occurrences recorded in avail_occr are exactly those that
1098 we want to set to non-zero in COMP. */
1099 if (comp)
1102 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
1104 int bb = BLOCK_NUM (occr->insn);
1105 SET_BIT (comp[bb], indx);
1107 /* While we're scanning the table, this is a good place to
1108 initialize this. */
1109 occr->copied_p = 0;
1113 /* While we're scanning the table, this is a good place to
1114 initialize this. */
1115 expr->reaching_reg = 0;
1121 /* Register set information.
1123 `reg_set_table' records where each register is set or otherwise
1124 modified. */
1126 static struct obstack reg_set_obstack;
1128 static void
1129 alloc_reg_set_mem (n_regs)
1130 int n_regs;
1132 int n;
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);
1139 gcc_obstack_init (&reg_set_obstack);
1142 static void
1143 free_reg_set_mem ()
1145 free (reg_set_table);
1146 obstack_free (&reg_set_obstack, NULL_PTR);
1149 /* Record REGNO in the reg_set table. */
1151 static void
1152 record_one_set (regno, insn)
1153 int regno;
1154 rtx insn;
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;
1159 /* If the table isn't big enough, enlarge it. */
1160 if (regno >= reg_set_table_size)
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;
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
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)
1184 reg_info_ptr2 = reg_info_ptr1;
1185 reg_info_ptr1 = reg_info_ptr1->next;
1187 reg_info_ptr2->next = new_reg_info;
1191 /* Called from compute_sets via note_stores to handle one
1192 SET or CLOBBER in an insn. The DATA is really the instruction
1193 in which the SET is occurring. */
1195 static void
1196 record_set_info (dest, setter, data)
1197 rtx dest, setter ATTRIBUTE_UNUSED;
1198 void *data;
1200 rtx record_set_insn = (rtx) data;
1202 if (GET_CODE (dest) == SUBREG)
1203 dest = SUBREG_REG (dest);
1205 if (GET_CODE (dest) == REG)
1207 if (REGNO (dest) >= FIRST_PSEUDO_REGISTER)
1208 record_one_set (REGNO (dest), record_set_insn);
1212 /* Scan the function and record each set of each pseudo-register.
1214 This is called once, at the start of the gcse pass.
1215 See the comments for `reg_set_table' for further docs. */
1217 static void
1218 compute_sets (f)
1219 rtx f;
1221 rtx insn = f;
1223 while (insn)
1225 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1226 note_stores (PATTERN (insn), record_set_info, insn);
1227 insn = NEXT_INSN (insn);
1231 /* Hash table support. */
1233 #define NEVER_SET -1
1235 /* For each register, the cuid of the first/last insn in the block to set it,
1236 or -1 if not set. */
1237 static int *reg_first_set;
1238 static int *reg_last_set;
1240 /* While computing "first/last set" info, this is the CUID of first/last insn
1241 to set memory or -1 if not set. `mem_last_set' is also used when
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). */
1246 static int mem_first_set;
1247 static int mem_last_set;
1249 /* Perform a quick check whether X, the source of a set, is something
1250 we want to consider for GCSE. */
1252 static int
1253 want_to_gcse_p (x)
1254 rtx x;
1256 enum rtx_code code = GET_CODE (x);
1258 switch (code)
1260 case REG:
1261 case SUBREG:
1262 case CONST_INT:
1263 case CONST_DOUBLE:
1264 case CALL:
1265 return 0;
1267 default:
1268 break;
1271 return 1;
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). */
1278 static int
1279 oprs_unchanged_p (x, insn, avail_p)
1280 rtx x, insn;
1281 int avail_p;
1283 int i;
1284 enum rtx_code code;
1285 const char *fmt;
1287 /* repeat is used to turn tail-recursion into iteration. */
1288 repeat:
1290 if (x == 0)
1291 return 1;
1293 code = GET_CODE (x);
1294 switch (code)
1296 case REG:
1297 if (avail_p)
1298 return (reg_last_set[REGNO (x)] == NEVER_SET
1299 || reg_last_set[REGNO (x)] < INSN_CUID (insn));
1300 else
1301 return (reg_first_set[REGNO (x)] == NEVER_SET
1302 || reg_first_set[REGNO (x)] >= INSN_CUID (insn));
1304 case MEM:
1305 if (avail_p)
1307 if (mem_last_set != NEVER_SET
1308 && mem_last_set >= INSN_CUID (insn))
1309 return 0;
1311 else
1313 if (mem_first_set != NEVER_SET
1314 && mem_first_set < INSN_CUID (insn))
1315 return 0;
1317 x = XEXP (x, 0);
1318 goto repeat;
1320 case PRE_DEC:
1321 case PRE_INC:
1322 case POST_DEC:
1323 case POST_INC:
1324 return 0;
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;
1337 default:
1338 break;
1341 i = GET_RTX_LENGTH (code) - 1;
1342 fmt = GET_RTX_FORMAT (code);
1343 for (; i >= 0; i--)
1345 if (fmt[i] == 'e')
1347 rtx tem = XEXP (x, i);
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)
1354 x = tem;
1355 goto repeat;
1357 if (! oprs_unchanged_p (tem, insn, avail_p))
1358 return 0;
1360 else if (fmt[i] == 'E')
1362 int j;
1363 for (j = 0; j < XVECLEN (x, i); j++)
1365 if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, avail_p))
1366 return 0;
1371 return 1;
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. */
1377 static int
1378 oprs_anticipatable_p (x, insn)
1379 rtx x, insn;
1381 return oprs_unchanged_p (x, insn, 0);
1384 /* Return non-zero if the operands of expression X are unchanged from
1385 INSN to the end of INSN's basic block. */
1387 static int
1388 oprs_available_p (x, insn)
1389 rtx x, insn;
1391 return oprs_unchanged_p (x, insn, 1);
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.
1400 ??? One might want to merge this with canon_hash. Later. */
1402 static unsigned int
1403 hash_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;
1409 unsigned int hash;
1411 *do_not_record_p = 0;
1413 hash = hash_expr_1 (x, mode, do_not_record_p);
1414 return hash % hash_table_size;
1417 /* Subroutine of hash_expr to do the actual work. */
1419 static unsigned int
1420 hash_expr_1 (x, mode, do_not_record_p)
1421 rtx x;
1422 enum machine_mode mode;
1423 int *do_not_record_p;
1425 int i, j;
1426 unsigned hash = 0;
1427 enum rtx_code code;
1428 const char *fmt;
1430 /* repeat is used to turn tail-recursion into iteration. */
1431 repeat:
1433 if (x == 0)
1434 return hash;
1436 code = GET_CODE (x);
1437 switch (code)
1439 case REG:
1441 register int regno = REGNO (x);
1442 hash += ((unsigned) REG << 7) + regno;
1443 return hash;
1446 case CONST_INT:
1448 unsigned HOST_WIDE_INT tem = INTVAL (x);
1449 hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + tem;
1450 return hash;
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++)
1460 unsigned tem = XWINT (x, i);
1461 hash += tem;
1463 else
1464 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1465 + (unsigned) CONST_DOUBLE_HIGH (x));
1466 return hash;
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;
1475 case SYMBOL_REF:
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;
1490 case MEM:
1491 if (MEM_VOLATILE_P (x))
1493 *do_not_record_p = 1;
1494 return 0;
1496 hash += (unsigned) MEM;
1497 hash += MEM_ALIAS_SET (x);
1498 x = XEXP (x, 0);
1499 goto repeat;
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;
1512 case ASM_OPERANDS:
1513 if (MEM_VOLATILE_P (x))
1515 *do_not_record_p = 1;
1516 return 0;
1519 default:
1520 break;
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--)
1528 if (fmt[i] == 'e')
1530 rtx tem = XEXP (x, i);
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)
1537 x = tem;
1538 goto repeat;
1540 hash += hash_expr_1 (tem, 0, do_not_record_p);
1541 if (*do_not_record_p)
1542 return 0;
1544 else if (fmt[i] == 'E')
1545 for (j = 0; j < XVECLEN (x, i); j++)
1547 hash += hash_expr_1 (XVECEXP (x, i, j), 0, do_not_record_p);
1548 if (*do_not_record_p)
1549 return 0;
1551 else if (fmt[i] == 's')
1553 register unsigned char *p = (unsigned char *) XSTR (x, i);
1554 if (p)
1555 while (*p)
1556 hash += *p++;
1558 else if (fmt[i] == 'i')
1560 register unsigned tem = XINT (x, i);
1561 hash += tem;
1563 else
1564 abort ();
1567 return hash;
1570 /* Hash a set of register REGNO.
1572 Sets are hashed on the register that is set.
1573 This simplifies the PRE copy propagation code.
1575 ??? May need to make things more elaborate. Later, as necessary. */
1577 static unsigned int
1578 hash_set (regno, hash_table_size)
1579 int regno;
1580 int hash_table_size;
1582 unsigned int hash;
1584 hash = regno;
1585 return hash % hash_table_size;
1588 /* Return non-zero if exp1 is equivalent to exp2.
1589 ??? Borrowed from cse.c. Might want to remerge with cse.c. Later. */
1591 static int
1592 expr_equiv_p (x, y)
1593 rtx x, y;
1595 register int i, j;
1596 register enum rtx_code code;
1597 register const char *fmt;
1599 if (x == y)
1600 return 1;
1601 if (x == 0 || y == 0)
1602 return x == y;
1604 code = GET_CODE (x);
1605 if (code != GET_CODE (y))
1606 return 0;
1608 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
1609 if (GET_MODE (x) != GET_MODE (y))
1610 return 0;
1612 switch (code)
1614 case PC:
1615 case CC0:
1616 return x == y;
1618 case CONST_INT:
1619 return INTVAL (x) == INTVAL (y);
1621 case LABEL_REF:
1622 return XEXP (x, 0) == XEXP (y, 0);
1624 case SYMBOL_REF:
1625 return XSTR (x, 0) == XSTR (y, 0);
1627 case REG:
1628 return REGNO (x) == REGNO (y);
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;
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))));
1651 default:
1652 break;
1655 /* Compare the elements. If any pair of corresponding elements
1656 fail to match, return 0 for the whole thing. */
1658 fmt = GET_RTX_FORMAT (code);
1659 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1661 switch (fmt[i])
1663 case 'e':
1664 if (! expr_equiv_p (XEXP (x, i), XEXP (y, i)))
1665 return 0;
1666 break;
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;
1676 case 's':
1677 if (strcmp (XSTR (x, i), XSTR (y, i)))
1678 return 0;
1679 break;
1681 case 'i':
1682 if (XINT (x, i) != XINT (y, i))
1683 return 0;
1684 break;
1686 case 'w':
1687 if (XWINT (x, i) != XWINT (y, i))
1688 return 0;
1689 break;
1691 case '0':
1692 break;
1694 default:
1695 abort ();
1699 return 1;
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.
1706 MODE is the mode of the value X is being stored into.
1707 It is only used if X is a CONST_INT.
1709 ANTIC_P is non-zero if X is an anticipatable expression.
1710 AVAIL_P is non-zero if X is an available expression. */
1712 static void
1713 insert_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;
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;
1725 hash = hash_expr (x, mode, &do_not_record_p, expr_hash_table_size);
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;
1733 cur_expr = expr_hash_table[hash];
1734 found = 0;
1736 while (cur_expr && ! (found = expr_equiv_p (cur_expr->expr, x)))
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;
1744 if (! found)
1746 cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
1747 bytes_used += sizeof (struct expr);
1748 if (expr_hash_table[hash] == NULL)
1750 /* This is the first pattern that hashed to this index. */
1751 expr_hash_table[hash] = cur_expr;
1753 else
1755 /* Add EXPR to end of this hash chain. */
1756 last_expr->next_same_hash = cur_expr;
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;
1766 /* Now record the occurrence(s). */
1768 if (antic_p)
1770 antic_occr = cur_expr->antic_occr;
1772 /* Search for another occurrence in the same basic block. */
1773 while (antic_occr && BLOCK_NUM (antic_occr->insn) != BLOCK_NUM (insn))
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;
1781 if (antic_occr)
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 */
1788 else
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;
1803 if (avail_p)
1805 avail_occr = cur_expr->avail_occr;
1807 /* Search for another occurrence in the same basic block. */
1808 while (avail_occr && BLOCK_NUM (avail_occr->insn) != BLOCK_NUM (insn))
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;
1816 if (avail_occr)
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;
1824 else
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;
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. */
1845 static void
1846 insert_set_in_table (x, insn)
1847 rtx x;
1848 rtx insn;
1850 int found;
1851 unsigned int hash;
1852 struct expr *cur_expr, *last_expr = NULL;
1853 struct occr *cur_occr, *last_occr = NULL;
1855 if (GET_CODE (x) != SET
1856 || GET_CODE (SET_DEST (x)) != REG)
1857 abort ();
1859 hash = hash_set (REGNO (SET_DEST (x)), set_hash_table_size);
1861 cur_expr = set_hash_table[hash];
1862 found = 0;
1864 while (cur_expr && ! (found = expr_equiv_p (cur_expr->expr, x)))
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;
1872 if (! found)
1874 cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
1875 bytes_used += sizeof (struct expr);
1876 if (set_hash_table[hash] == NULL)
1878 /* This is the first pattern that hashed to this index. */
1879 set_hash_table[hash] = cur_expr;
1881 else
1883 /* Add EXPR to end of this hash chain. */
1884 last_expr->next_same_hash = cur_expr;
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;
1897 /* Now record the occurrence. */
1899 cur_occr = cur_expr->avail_occr;
1901 /* Search for another occurrence in the same basic block. */
1902 while (cur_occr && BLOCK_NUM (cur_occr->insn) != BLOCK_NUM (insn))
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;
1910 if (cur_occr)
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;
1918 else
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;
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. */
1937 static void
1938 hash_scan_set (pat, insn, set_p)
1939 rtx pat, insn;
1940 int set_p;
1942 rtx src = SET_SRC (pat);
1943 rtx dest = SET_DEST (pat);
1945 if (GET_CODE (src) == CALL)
1946 hash_scan_call (src, insn);
1948 if (GET_CODE (dest) == REG)
1950 int regno = REGNO (dest);
1951 rtx tmp;
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))
1961 /* An expression is not anticipatable if its operands are
1962 modified before this insn. */
1963 int antic_p = oprs_anticipatable_p (src, insn);
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);
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)])
1975 || GET_CODE (src) == CONST_INT
1976 || GET_CODE (src) == SYMBOL_REF
1977 || GET_CODE (src) == CONST_DOUBLE)
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);
1988 static void
1989 hash_scan_clobber (x, insn)
1990 rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
1992 /* Currently nothing to do. */
1995 static void
1996 hash_scan_call (x, insn)
1997 rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
1999 /* Currently nothing to do. */
2002 /* Process INSN and add hash table entries as appropriate.
2004 Only available expressions that set a single pseudo-reg are recorded.
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.
2010 If SET_P is non-zero, this is for the assignment hash table,
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. */
2015 static void
2016 hash_scan_insn (insn, set_p, in_libcall_block)
2017 rtx insn;
2018 int set_p;
2019 int in_libcall_block;
2021 rtx pat = PATTERN (insn);
2023 /* Pick out the sets of INSN and for other forms of instructions record
2024 what's been modified. */
2026 if (GET_CODE (pat) == SET && ! in_libcall_block)
2028 /* Ignore obvious no-ops. */
2029 if (SET_SRC (pat) != SET_DEST (pat))
2030 hash_scan_set (pat, insn, set_p);
2032 else if (GET_CODE (pat) == PARALLEL)
2034 int i;
2036 for (i = 0; i < XVECLEN (pat, 0); i++)
2038 rtx x = XVECEXP (pat, 0, i);
2040 if (GET_CODE (x) == SET)
2042 if (GET_CODE (SET_SRC (x)) == CALL)
2043 hash_scan_call (SET_SRC (x), insn);
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);
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);
2057 static void
2058 dump_hash_table (file, name, table, table_size, total_size)
2059 FILE *file;
2060 const char *name;
2061 struct expr **table;
2062 int table_size, total_size;
2064 int i;
2065 /* Flattened out table, so it's printed in proper order. */
2066 struct expr **flat_table;
2067 unsigned int *hash_val;
2069 flat_table
2070 = (struct expr **) xcalloc (total_size, sizeof (struct expr *));
2071 hash_val = (unsigned int *) xmalloc (total_size * sizeof (unsigned int));
2073 for (i = 0; i < table_size; i++)
2075 struct expr *expr;
2077 for (expr = table[i]; expr != NULL; expr = expr->next_same_hash)
2079 flat_table[expr->bitmap_index] = expr;
2080 hash_val[expr->bitmap_index] = i;
2084 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
2085 name, table_size, total_size);
2087 for (i = 0; i < total_size; i++)
2089 struct expr *expr = flat_table[i];
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");
2097 fprintf (file, "\n");
2099 /* Clean up. */
2100 free (flat_table);
2101 free (hash_val);
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". */
2112 static void
2113 record_last_reg_set_info (insn, regno)
2114 rtx insn;
2115 int regno;
2117 if (reg_first_set[regno] == NEVER_SET)
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);
2123 /* Record memory first/last/block set information for INSN. */
2125 static void
2126 record_last_mem_set_info (insn)
2127 rtx insn;
2129 if (mem_first_set == NEVER_SET)
2130 mem_first_set = INSN_CUID (insn);
2131 mem_last_set = INSN_CUID (insn);
2132 mem_set_in_block[BLOCK_NUM (insn)] = 1;
2135 /* Called from compute_hash_table via note_stores to handle one
2136 SET or CLOBBER in an insn. DATA is really the instruction in which
2137 the SET is taking place. */
2139 static void
2140 record_last_set_info (dest, setter, data)
2141 rtx dest, setter ATTRIBUTE_UNUSED;
2142 void *data;
2144 rtx last_set_insn = (rtx) data;
2146 if (GET_CODE (dest) == SUBREG)
2147 dest = SUBREG_REG (dest);
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);
2157 /* Top level function to create an expression or assignment hash table.
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
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.
2170 F is the first insn.
2171 SET_P is non-zero for computing the assignment hash table. */
2173 static void
2174 compute_hash_table (set_p)
2175 int set_p;
2177 int bb;
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);
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));
2194 for (bb = 0; bb < n_basic_blocks; bb++)
2196 rtx insn;
2197 int regno;
2198 int in_libcall_block;
2199 int i;
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. */
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;
2211 for (insn = BLOCK_HEAD (bb);
2212 insn && insn != NEXT_INSN (BLOCK_END (bb));
2213 insn = NEXT_INSN (insn))
2215 #ifdef NON_SAVING_SETJMP
2216 if (NON_SAVING_SETJMP && GET_CODE (insn) == NOTE
2217 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
2219 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2220 record_last_reg_set_info (insn, regno);
2221 continue;
2223 #endif
2225 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
2226 continue;
2228 if (GET_CODE (insn) == CALL_INSN)
2230 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
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
2243 && regno != FRAME_POINTER_REGNUM)
2244 || global_regs[regno])
2245 record_last_reg_set_info (insn, regno);
2246 if (! CONST_CALL_P (insn))
2247 record_last_mem_set_info (insn);
2250 note_stores (PATTERN (insn), record_last_set_info, insn);
2253 /* The next pass builds the hash table. */
2255 for (insn = BLOCK_HEAD (bb), in_libcall_block = 0;
2256 insn && insn != NEXT_INSN (BLOCK_END (bb));
2257 insn = NEXT_INSN (insn))
2259 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
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);
2270 free (reg_first_set);
2271 free (reg_last_set);
2272 /* Catch bugs early. */
2273 reg_first_set = reg_last_set = 0;
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. */
2280 static void
2281 alloc_set_hash_table (n_insns)
2282 int n_insns;
2284 int n;
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);
2297 /* Free things allocated by alloc_set_hash_table. */
2299 static void
2300 free_set_hash_table ()
2302 free (set_hash_table);
2305 /* Compute the hash table for doing copy/const propagation. */
2307 static void
2308 compute_set_hash_table ()
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 *));
2314 compute_hash_table (1);
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. */
2321 static void
2322 alloc_expr_hash_table (n_insns)
2323 int n_insns;
2325 int n;
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);
2339 /* Free things allocated by alloc_expr_hash_table. */
2341 static void
2342 free_expr_hash_table ()
2344 free (expr_hash_table);
2347 /* Compute the hash table for doing GCSE. */
2349 static void
2350 compute_expr_hash_table ()
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 *));
2356 compute_hash_table (0);
2359 /* Expression tracking support. */
2361 /* Lookup pattern PAT in the expression table.
2362 The result is a pointer to the table entry, or NULL if not found. */
2364 static struct expr *
2365 lookup_expr (pat)
2366 rtx pat;
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;
2373 if (do_not_record_p)
2374 return NULL;
2376 expr = expr_hash_table[hash];
2378 while (expr && ! expr_equiv_p (expr->expr, pat))
2379 expr = expr->next_same_hash;
2381 return expr;
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. */
2389 static struct expr *
2390 lookup_set (regno, pat)
2391 int regno;
2392 rtx pat;
2394 unsigned int hash = hash_set (regno, set_hash_table_size);
2395 struct expr *expr;
2397 expr = set_hash_table[hash];
2399 if (pat)
2401 while (expr && ! expr_equiv_p (expr->expr, pat))
2402 expr = expr->next_same_hash;
2404 else
2406 while (expr && REGNO (SET_DEST (expr->expr)) != regno)
2407 expr = expr->next_same_hash;
2410 return expr;
2413 /* Return the next entry for REGNO in list EXPR. */
2415 static struct expr *
2416 next_set (regno, expr)
2417 int regno;
2418 struct expr *expr;
2421 expr = expr->next_same_hash;
2422 while (expr && REGNO (SET_DEST (expr->expr)) != regno);
2423 return expr;
2426 /* Reset tables used to keep track of what's still available [since the
2427 start of the block]. */
2429 static void
2430 reset_opr_set_tables ()
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;
2441 /* Return non-zero if the operands of X are not set before INSN in
2442 INSN's basic block. */
2444 static int
2445 oprs_not_set_p (x, insn)
2446 rtx x, insn;
2448 int i;
2449 enum rtx_code code;
2450 const char *fmt;
2452 /* repeat is used to turn tail-recursion into iteration. */
2453 repeat:
2455 if (x == 0)
2456 return 1;
2458 code = GET_CODE (x);
2459 switch (code)
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;
2472 case MEM:
2473 if (mem_last_set != 0)
2474 return 0;
2475 x = XEXP (x, 0);
2476 goto repeat;
2478 case REG:
2479 return ! TEST_BIT (reg_set_bitmap, REGNO (x));
2481 default:
2482 break;
2485 fmt = GET_RTX_FORMAT (code);
2486 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2488 if (fmt[i] == 'e')
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)
2496 x = XEXP (x, 0);
2497 goto repeat;
2499 not_set_p = oprs_not_set_p (XEXP (x, i), insn);
2500 if (! not_set_p)
2501 return 0;
2503 else if (fmt[i] == 'E')
2505 int j;
2506 for (j = 0; j < XVECLEN (x, i); j++)
2508 int not_set_p = oprs_not_set_p (XVECEXP (x, i, j), insn);
2509 if (! not_set_p)
2510 return 0;
2515 return 1;
2518 /* Mark things set by a CALL. */
2520 static void
2521 mark_call (insn)
2522 rtx insn;
2524 mem_last_set = INSN_CUID (insn);
2527 /* Mark things set by a SET. */
2529 static void
2530 mark_set (pat, insn)
2531 rtx pat, insn;
2533 rtx dest = SET_DEST (pat);
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);
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);
2546 if (GET_CODE (SET_SRC (pat)) == CALL)
2547 mark_call (insn);
2550 /* Record things set by a CLOBBER. */
2552 static void
2553 mark_clobber (pat, insn)
2554 rtx pat, insn;
2556 rtx clob = XEXP (pat, 0);
2558 while (GET_CODE (clob) == SUBREG || GET_CODE (clob) == STRICT_LOW_PART)
2559 clob = XEXP (clob, 0);
2561 if (GET_CODE (clob) == REG)
2562 SET_BIT (reg_set_bitmap, REGNO (clob));
2563 else
2564 mem_last_set = INSN_CUID (insn);
2567 /* Record things set by INSN.
2568 This data is used by oprs_not_set_p. */
2570 static void
2571 mark_oprs_set (insn)
2572 rtx insn;
2574 rtx pat = PATTERN (insn);
2576 if (GET_CODE (pat) == SET)
2577 mark_set (pat, insn);
2578 else if (GET_CODE (pat) == PARALLEL)
2580 int i;
2582 for (i = 0; i < XVECLEN (pat, 0); i++)
2584 rtx x = XVECEXP (pat, 0, i);
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)
2591 mark_call (insn);
2594 else if (GET_CODE (pat) == CLOBBER)
2595 mark_clobber (pat, insn);
2596 else if (GET_CODE (pat) == CALL)
2597 mark_call (insn);
2601 /* Classic GCSE reaching definition support. */
2603 /* Allocate reaching def variables. */
2605 static void
2606 alloc_rd_mem (n_blocks, n_insns)
2607 int n_blocks, n_insns;
2609 rd_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2610 sbitmap_vector_zero (rd_kill, n_basic_blocks);
2612 rd_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2613 sbitmap_vector_zero (rd_gen, n_basic_blocks);
2615 reaching_defs = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2616 sbitmap_vector_zero (reaching_defs, n_basic_blocks);
2618 rd_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2619 sbitmap_vector_zero (rd_out, n_basic_blocks);
2622 /* Free reaching def variables. */
2624 static void
2625 free_rd_mem ()
2627 free (rd_kill);
2628 free (rd_gen);
2629 free (reaching_defs);
2630 free (rd_out);
2633 /* Add INSN to the kills of BB.
2634 REGNO, set in BB, is killed by INSN. */
2636 static void
2637 handle_rd_kill_set (insn, regno, bb)
2638 rtx insn;
2639 int regno, bb;
2641 struct reg_set *this_reg = reg_set_table[regno];
2643 while (this_reg)
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;
2651 /* Compute the set of kill's for reaching definitions. */
2653 static void
2654 compute_kill_rd ()
2656 int bb,cuid;
2658 /* For each block
2659 For each set bit in `gen' of the block (i.e each insn which
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
2668 for (bb = 0; bb < n_basic_blocks; bb++)
2670 for (cuid = 0; cuid < max_cuid; cuid++)
2672 if (TEST_BIT (rd_gen[bb], cuid))
2674 rtx insn = CUID_INSN (cuid);
2675 rtx pat = PATTERN (insn);
2677 if (GET_CODE (insn) == CALL_INSN)
2679 int regno;
2681 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
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])
2697 handle_rd_kill_set (insn, regno, bb);
2701 if (GET_CODE (pat) == PARALLEL)
2703 int i;
2705 /* We work backwards because ... */
2706 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
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);
2716 else if (GET_CODE (pat) == SET)
2718 if (GET_CODE (SET_DEST (pat)) == REG)
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);
2725 /* FIXME: CLOBBER? */
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. */
2736 static void
2737 compute_rd ()
2739 int bb, changed, passes;
2741 for (bb = 0; bb < n_basic_blocks; bb++)
2742 sbitmap_copy (rd_out[bb] /*dst*/, rd_gen[bb] /*src*/);
2744 passes = 0;
2745 changed = 1;
2746 while (changed)
2748 changed = 0;
2749 for (bb = 0; bb < n_basic_blocks; bb++)
2751 sbitmap_union_of_preds (reaching_defs[bb], rd_out, bb);
2752 changed |= sbitmap_union_of_diff (rd_out[bb], rd_gen[bb],
2753 reaching_defs[bb], rd_kill[bb]);
2755 passes++;
2758 if (gcse_file)
2759 fprintf (gcse_file, "reaching def computation: %d passes\n", passes);
2762 /* Classic GCSE available expression support. */
2764 /* Allocate memory for available expression computation. */
2766 static void
2767 alloc_avail_expr_mem (n_blocks, n_exprs)
2768 int n_blocks, n_exprs;
2770 ae_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2771 sbitmap_vector_zero (ae_kill, n_basic_blocks);
2773 ae_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2774 sbitmap_vector_zero (ae_gen, n_basic_blocks);
2776 ae_in = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2777 sbitmap_vector_zero (ae_in, n_basic_blocks);
2779 ae_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2780 sbitmap_vector_zero (ae_out, n_basic_blocks);
2782 u_bitmap = (sbitmap) sbitmap_alloc (n_exprs);
2783 sbitmap_ones (u_bitmap);
2786 static void
2787 free_avail_expr_mem ()
2789 free (ae_kill);
2790 free (ae_gen);
2791 free (ae_in);
2792 free (ae_out);
2793 free (u_bitmap);
2796 /* Compute the set of available expressions generated in each basic block. */
2798 static void
2799 compute_ae_gen ()
2801 int i;
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. */
2808 for (i = 0; i < expr_hash_table_size; i++)
2810 struct expr *expr = expr_hash_table[i];
2811 while (expr != NULL)
2813 struct occr *occr = expr->avail_occr;
2814 while (occr != NULL)
2816 SET_BIT (ae_gen[BLOCK_NUM (occr->insn)], expr->bitmap_index);
2817 occr = occr->next;
2819 expr = expr->next_same_hash;
2824 /* Return non-zero if expression X is killed in BB. */
2826 static int
2827 expr_killed_p (x, bb)
2828 rtx x;
2829 int bb;
2831 int i;
2832 enum rtx_code code;
2833 const char *fmt;
2835 /* repeat is used to turn tail-recursion into iteration. */
2836 repeat:
2838 if (x == 0)
2839 return 1;
2841 code = GET_CODE (x);
2842 switch (code)
2844 case REG:
2845 return TEST_BIT (reg_set_in_block[bb], REGNO (x));
2847 case MEM:
2848 if (mem_set_in_block[bb])
2849 return 1;
2850 x = XEXP (x, 0);
2851 goto repeat;
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;
2864 default:
2865 break;
2868 i = GET_RTX_LENGTH (code) - 1;
2869 fmt = GET_RTX_FORMAT (code);
2870 for (; i >= 0; i--)
2872 if (fmt[i] == 'e')
2874 rtx tem = XEXP (x, i);
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)
2881 x = tem;
2882 goto repeat;
2884 if (expr_killed_p (tem, bb))
2885 return 1;
2887 else if (fmt[i] == 'E')
2889 int j;
2890 for (j = 0; j < XVECLEN (x, i); j++)
2892 if (expr_killed_p (XVECEXP (x, i, j), bb))
2893 return 1;
2898 return 0;
2901 /* Compute the set of available expressions killed in each basic block. */
2903 static void
2904 compute_ae_kill (ae_gen, ae_kill)
2905 sbitmap *ae_gen, *ae_kill;
2907 int bb,i;
2909 for (bb = 0; bb < n_basic_blocks; bb++)
2911 for (i = 0; i < expr_hash_table_size; i++)
2913 struct expr *expr = expr_hash_table[i];
2915 for ( ; expr != NULL; expr = expr->next_same_hash)
2917 /* Skip EXPR if generated in this block. */
2918 if (TEST_BIT (ae_gen[bb], expr->bitmap_index))
2919 continue;
2921 if (expr_killed_p (expr->expr, bb))
2922 SET_BIT (ae_kill[bb], expr->bitmap_index);
2928 /* Actually perform the Classic GCSE optimizations. */
2930 /* Return non-zero if occurrence OCCR of expression EXPR reaches block BB.
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.
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.
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. */
2946 static int
2947 expr_reaches_here_p_work (occr, expr, bb, check_self_loop, visited)
2948 struct occr *occr;
2949 struct expr *expr;
2950 int bb;
2951 int check_self_loop;
2952 char *visited;
2954 edge pred;
2956 for (pred = BASIC_BLOCK(bb)->pred; pred != NULL; pred = pred->pred_next)
2958 int pred_bb = pred->src->index;
2960 if (visited[pred_bb])
2962 /* This predecessor has already been visited.
2963 Nothing to do. */
2966 else if (pred_bb == bb)
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;
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))
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;
2988 /* Neither gen nor kill. */
2989 else
2991 visited[pred_bb] = 1;
2992 if (expr_reaches_here_p_work (occr, expr, pred_bb, check_self_loop,
2993 visited))
2994 return 1;
2998 /* All paths have been checked. */
2999 return 0;
3002 /* This wrapper for expr_reaches_here_p_work() is to ensure that any
3003 memory allocated for that function is returned. */
3005 static int
3006 expr_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;
3012 int rval;
3013 char * visited = (char *) xcalloc (n_basic_blocks, 1);
3015 rval = expr_reaches_here_p_work(occr, expr, bb, check_self_loop, visited);
3017 free (visited);
3019 return (rval);
3022 /* Return the instruction that computes EXPR that reaches INSN's basic block.
3023 If there is more than one such instruction, return NULL.
3025 Called only by handle_avail_expr. */
3027 static rtx
3028 computing_insn (expr, insn)
3029 struct expr *expr;
3030 rtx insn;
3032 int bb = BLOCK_NUM (insn);
3034 if (expr->avail_occr->next == NULL)
3036 if (BLOCK_NUM (expr->avail_occr->insn) == bb)
3038 /* The available expression is actually itself
3039 (i.e. a loop in the flow graph) so do nothing. */
3040 return NULL;
3042 /* (FIXME) Case that we found a pattern that was created by
3043 a substitution that took place. */
3044 return expr->avail_occr->insn;
3046 else
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;
3055 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
3057 if (BLOCK_NUM (occr->insn) == bb)
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))
3065 if (expr_reaches_here_p (occr, expr, bb, 1))
3067 can_reach++;
3068 if (can_reach > 1)
3069 return NULL;
3070 insn_computes_expr = occr->insn;
3074 else /* Computation of the pattern outside this block. */
3076 if (expr_reaches_here_p (occr, expr, bb, 0))
3078 can_reach++;
3079 if (can_reach > 1)
3080 return NULL;
3081 insn_computes_expr = occr->insn;
3086 if (insn_computes_expr == NULL)
3087 abort ();
3088 return insn_computes_expr;
3092 /* Return non-zero if the definition in DEF_INSN can reach INSN.
3093 Only called by can_disregard_other_sets. */
3095 static int
3096 def_reaches_here_p (insn, def_insn)
3097 rtx insn, def_insn;
3099 rtx reg;
3101 if (TEST_BIT (reaching_defs[BLOCK_NUM (insn)], INSN_CUID (def_insn)))
3102 return 1;
3104 if (BLOCK_NUM (insn) == BLOCK_NUM (def_insn))
3106 if (INSN_CUID (def_insn) < INSN_CUID (insn))
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);
3118 else
3119 return 0;
3122 return 0;
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. */
3131 static int
3132 can_disregard_other_sets (addr_this_reg, insn, for_combine)
3133 struct reg_set **addr_this_reg;
3134 rtx insn;
3135 int for_combine;
3137 int number_of_reaching_defs = 0;
3138 struct reg_set *this_reg = *addr_this_reg;
3140 while (this_reg)
3142 if (def_reaches_here_p (insn, this_reg->insn))
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)))))
3153 /* A setting of the reg to a different value reaches INSN. */
3154 return 0;
3156 if (number_of_reaching_defs > 1)
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. */
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;
3169 *addr_this_reg = this_reg;
3172 /* prev_this_reg = this_reg; */
3173 this_reg = this_reg->next;
3176 return number_of_reaching_defs;
3179 /* Expression computed by insn is available and the substitution is legal,
3180 so try to perform the substitution.
3182 The result is non-zero if any changes were made. */
3184 static int
3185 handle_avail_expr (insn, expr)
3186 rtx insn;
3187 struct expr *expr;
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;
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;
3201 found_setting = 0;
3202 use_src = 0;
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. */
3208 if (GET_CODE (SET_SRC (PATTERN (insn_computes_expr))) == REG)
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)))
3222 use_src = 1;
3223 found_setting = 1;
3227 if (!found_setting)
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;
3241 if (found_setting)
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);
3250 /* We should be able to ignore the return code from validate_change but
3251 to play it safe we check. */
3252 if (changed)
3254 gcse_subst_count++;
3255 if (gcse_file != NULL)
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));
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)*/)
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;
3274 to = gen_reg_rtx (GET_MODE (SET_DEST (PATTERN (insn_computes_expr))));
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. */
3279 new_insn
3280 = emit_insn_after (gen_rtx_SET (VOIDmode, to,
3281 SET_DEST (PATTERN (insn_computes_expr))),
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);
3288 gcse_create_count++;
3289 if (gcse_file != NULL)
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));
3298 pat = PATTERN (insn);
3300 /* Do register replacement for INSN. */
3301 changed = validate_change (insn, &SET_SRC (pat),
3302 SET_DEST (PATTERN (NEXT_INSN (insn_computes_expr))),
3305 /* We should be able to ignore the return code from validate_change but
3306 to play it safe we check. */
3307 if (changed)
3309 gcse_subst_count++;
3310 if (gcse_file != NULL)
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));
3321 return changed;
3324 /* Perform classic GCSE.
3325 This is called by one_classic_gcse_pass after all the dataflow analysis
3326 has been done.
3328 The result is non-zero if a change was made. */
3330 static int
3331 classic_gcse ()
3333 int bb, changed;
3334 rtx insn;
3336 /* Note we start at block 1. */
3338 changed = 0;
3339 for (bb = 1; bb < n_basic_blocks; bb++)
3341 /* Reset tables used to keep track of what's still valid [since the
3342 start of the block]. */
3343 reset_opr_set_tables ();
3345 for (insn = BLOCK_HEAD (bb);
3346 insn != NULL && insn != NEXT_INSN (BLOCK_END (bb));
3347 insn = NEXT_INSN (insn))
3349 /* Is insn of form (set (pseudo-reg) ...)? */
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)
3356 rtx pat = PATTERN (insn);
3357 rtx src = SET_SRC (pat);
3358 struct expr *expr;
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);
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);
3379 return changed;
3382 /* Top level routine to perform one classic GCSE pass.
3384 Return non-zero if a change was made. */
3386 static int
3387 one_classic_gcse_pass (pass)
3388 int pass;
3390 int changed = 0;
3392 gcse_subst_count = 0;
3393 gcse_create_count = 0;
3395 alloc_expr_hash_table (max_cuid);
3396 alloc_rd_mem (n_basic_blocks, max_cuid);
3397 compute_expr_hash_table ();
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)
3403 compute_kill_rd ();
3404 compute_rd ();
3405 alloc_avail_expr_mem (n_basic_blocks, n_exprs);
3406 compute_ae_gen ();
3407 compute_ae_kill (ae_gen, ae_kill);
3408 compute_available (ae_gen, ae_kill, ae_out, ae_in);
3409 changed = classic_gcse ();
3410 free_avail_expr_mem ();
3412 free_rd_mem ();
3413 free_expr_hash_table ();
3415 if (gcse_file)
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);
3423 return changed;
3426 /* Compute copy/constant propagation working variables. */
3428 /* Local properties of assignments. */
3430 static sbitmap *cprop_pavloc;
3431 static sbitmap *cprop_absaltered;
3433 /* Global properties of assignments (computed from the local properties). */
3435 static sbitmap *cprop_avin;
3436 static sbitmap *cprop_avout;
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. */
3442 static void
3443 alloc_cprop_mem (n_blocks, n_sets)
3444 int n_blocks, n_sets;
3446 cprop_pavloc = sbitmap_vector_alloc (n_blocks, n_sets);
3447 cprop_absaltered = sbitmap_vector_alloc (n_blocks, n_sets);
3449 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
3450 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
3453 /* Free vars used by copy/const propagation. */
3455 static void
3456 free_cprop_mem ()
3458 free (cprop_pavloc);
3459 free (cprop_absaltered);
3460 free (cprop_avin);
3461 free (cprop_avout);
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. */
3470 static void
3471 compute_transp (x, indx, bmap, set_p)
3472 rtx x;
3473 int indx;
3474 sbitmap *bmap;
3475 int set_p;
3477 int bb,i;
3478 enum rtx_code code;
3479 const char *fmt;
3481 /* repeat is used to turn tail-recursion into iteration. */
3482 repeat:
3484 if (x == 0)
3485 return;
3487 code = GET_CODE (x);
3488 switch (code)
3490 case REG:
3492 reg_set *r;
3493 int regno = REGNO (x);
3495 if (set_p)
3497 if (regno < FIRST_PSEUDO_REGISTER)
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);
3503 else
3505 for (r = reg_set_table[regno]; r != NULL; r = r->next)
3507 bb = BLOCK_NUM (r->insn);
3508 SET_BIT (bmap[bb], indx);
3512 else
3514 if (regno < FIRST_PSEUDO_REGISTER)
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);
3520 else
3522 for (r = reg_set_table[regno]; r != NULL; r = r->next)
3524 bb = BLOCK_NUM (r->insn);
3525 RESET_BIT (bmap[bb], indx);
3529 return;
3532 case MEM:
3533 if (set_p)
3535 for (bb = 0; bb < n_basic_blocks; bb++)
3536 if (mem_set_in_block[bb])
3537 SET_BIT (bmap[bb], indx);
3539 else
3541 for (bb = 0; bb < n_basic_blocks; bb++)
3542 if (mem_set_in_block[bb])
3543 RESET_BIT (bmap[bb], indx);
3545 x = XEXP (x, 0);
3546 goto repeat;
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;
3559 default:
3560 break;
3563 i = GET_RTX_LENGTH (code) - 1;
3564 fmt = GET_RTX_FORMAT (code);
3565 for (; i >= 0; i--)
3567 if (fmt[i] == 'e')
3569 rtx tem = XEXP (x, i);
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)
3576 x = tem;
3577 goto repeat;
3579 compute_transp (tem, indx, bmap, set_p);
3581 else if (fmt[i] == 'E')
3583 int j;
3584 for (j = 0; j < XVECLEN (x, i); j++)
3585 compute_transp (XVECEXP (x, i, j), indx, bmap, set_p);
3590 /* Top level routine to do the dataflow analysis needed by copy/const
3591 propagation. */
3593 static void
3594 compute_cprop_data ()
3596 compute_local_properties (cprop_absaltered, cprop_pavloc, NULL, 1);
3597 compute_available (cprop_pavloc, cprop_absaltered,
3598 cprop_avout, cprop_avin);
3601 /* Copy/constant propagation. */
3603 /* Maximum number of register uses in an insn that we handle. */
3604 #define MAX_USES 8
3606 /* Table of uses found in an insn.
3607 Allocated statically to avoid alloc/free complexity and overhead. */
3608 static struct reg_use reg_use_table[MAX_USES];
3610 /* Index into `reg_use_table' while building it. */
3611 static int reg_use_count;
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.
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. */
3621 static void
3622 find_used_regs (x)
3623 rtx x;
3625 int i;
3626 enum rtx_code code;
3627 const char *fmt;
3629 /* repeat is used to turn tail-recursion into iteration. */
3630 repeat:
3632 if (x == 0)
3633 return;
3635 code = GET_CODE (x);
3636 switch (code)
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;
3645 case MEM:
3646 x = XEXP (x, 0);
3647 goto repeat;
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;
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;
3668 default:
3669 break;
3672 /* Recursively scan the operands of this expression. */
3674 fmt = GET_RTX_FORMAT (code);
3675 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3677 if (fmt[i] == 'e')
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)
3684 x = XEXP (x, 0);
3685 goto repeat;
3687 find_used_regs (XEXP (x, i));
3689 else if (fmt[i] == 'E')
3691 int j;
3692 for (j = 0; j < XVECLEN (x, i); j++)
3693 find_used_regs (XVECEXP (x, i, j));
3698 /* Try to replace all non-SET_DEST occurrences of FROM in INSN with TO.
3699 Returns non-zero is successful. */
3701 static int
3702 try_replace_reg (from, to, insn)
3703 rtx from, to, insn;
3705 rtx note;
3706 rtx src;
3707 int success;
3708 rtx set;
3710 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
3712 if (!note)
3713 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
3715 /* If this fails we could try to simplify the result of the
3716 replacement and attempt to recognize the simplified insn.
3718 But we need a general simplify_rtx that doesn't have pass
3719 specific state variables. I'm not aware of one at the moment. */
3722 success = validate_replace_src (from, to, insn);
3723 set = single_set (insn);
3725 /* We've failed to do replacement. Try to add REG_EQUAL note to not loose
3726 information. */
3727 if (!success && !note)
3729 if (!set)
3730 return 0;
3731 note = REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL,
3732 copy_rtx (SET_SRC (set)),
3733 REG_NOTES (insn));
3736 /* Always do the replacement in REQ_EQUAL and REG_EQUIV notes. Also
3737 try to simplify them. */
3738 if (note)
3740 rtx simplified;
3741 src = XEXP (note, 0);
3742 replace_rtx (src, from, to);
3744 /* Try to simplify resulting note. */
3745 simplified = simplify_rtx (src);
3746 if (simplified)
3748 src = simplified;
3749 XEXP (note, 0) = src;
3752 /* REG_EQUAL may get simplified into register.
3753 We don't allow that. Remove that note. This code ought
3754 not to hapen, because previous code ought to syntetize
3755 reg-reg move, but be on the safe side. */
3756 else if (REG_P (src))
3757 remove_note (insn, note);
3759 return success;
3761 /* Find a set of REGNO that is available on entry to INSN's block.
3762 Returns NULL if not found. */
3764 static struct expr *
3765 find_avail_set (regno, insn)
3766 int regno;
3767 rtx insn;
3769 /* SET1 contains the last set found that can be returned to the caller for
3770 use in a substitution. */
3771 struct expr *set1 = 0;
3773 /* Loops are not possible here. To get a loop we would need two sets
3774 available at the start of the block containing INSN. ie we would
3775 need two sets like this available at the start of the block:
3777 (set (reg X) (reg Y))
3778 (set (reg Y) (reg X))
3780 This can not happen since the set of (reg Y) would have killed the
3781 set of (reg X) making it unavailable at the start of this block. */
3782 while (1)
3784 rtx src;
3785 struct expr *set = lookup_set (regno, NULL_RTX);
3787 /* Find a set that is available at the start of the block
3788 which contains INSN. */
3789 while (set)
3791 if (TEST_BIT (cprop_avin[BLOCK_NUM (insn)], set->bitmap_index))
3792 break;
3793 set = next_set (regno, set);
3796 /* If no available set was found we've reached the end of the
3797 (possibly empty) copy chain. */
3798 if (set == 0)
3799 break;
3801 if (GET_CODE (set->expr) != SET)
3802 abort ();
3804 src = SET_SRC (set->expr);
3806 /* We know the set is available.
3807 Now check that SRC is ANTLOC (i.e. none of the source operands
3808 have changed since the start of the block).
3810 If the source operand changed, we may still use it for the next
3811 iteration of this loop, but we may not use it for substitutions. */
3812 if (CONSTANT_P (src) || oprs_not_set_p (src, insn))
3813 set1 = set;
3815 /* If the source of the set is anything except a register, then
3816 we have reached the end of the copy chain. */
3817 if (GET_CODE (src) != REG)
3818 break;
3820 /* Follow the copy chain, ie start another iteration of the loop
3821 and see if we have an available copy into SRC. */
3822 regno = REGNO (src);
3825 /* SET1 holds the last set that was available and anticipatable at
3826 INSN. */
3827 return set1;
3830 /* Subroutine of cprop_insn that tries to propagate constants into
3831 JUMP_INSNS. INSN must be a conditional jump; COPY is a copy of it
3832 that we can use for substitutions.
3833 REG_USED is the use we will try to replace, SRC is the constant we
3834 will try to substitute for it.
3835 Returns nonzero if a change was made. */
3836 static int
3837 cprop_jump (insn, copy, reg_used, src)
3838 rtx insn, copy;
3839 struct reg_use *reg_used;
3840 rtx src;
3842 rtx set = PATTERN (copy);
3843 rtx temp;
3845 /* Replace the register with the appropriate constant. */
3846 replace_rtx (SET_SRC (set), reg_used->reg_rtx, src);
3848 temp = simplify_ternary_operation (GET_CODE (SET_SRC (set)),
3849 GET_MODE (SET_SRC (set)),
3850 GET_MODE (XEXP (SET_SRC (set), 0)),
3851 XEXP (SET_SRC (set), 0),
3852 XEXP (SET_SRC (set), 1),
3853 XEXP (SET_SRC (set), 2));
3855 /* If no simplification can be made, then try the next
3856 register. */
3857 if (temp == 0)
3858 return 0;
3860 SET_SRC (set) = temp;
3862 /* That may have changed the structure of TEMP, so
3863 force it to be rerecognized if it has not turned
3864 into a nop or unconditional jump. */
3866 INSN_CODE (copy) = -1;
3867 if ((SET_DEST (set) == pc_rtx
3868 && (SET_SRC (set) == pc_rtx
3869 || GET_CODE (SET_SRC (set)) == LABEL_REF))
3870 || recog (PATTERN (copy), copy, NULL) >= 0)
3872 /* This has either become an unconditional jump
3873 or a nop-jump. We'd like to delete nop jumps
3874 here, but doing so confuses gcse. So we just
3875 make the replacement and let later passes
3876 sort things out. */
3877 PATTERN (insn) = set;
3878 INSN_CODE (insn) = -1;
3880 /* One less use of the label this insn used to jump to
3881 if we turned this into a NOP jump. */
3882 if (SET_SRC (set) == pc_rtx && JUMP_LABEL (insn) != 0)
3883 --LABEL_NUSES (JUMP_LABEL (insn));
3885 /* If this has turned into an unconditional jump,
3886 then put a barrier after it so that the unreachable
3887 code will be deleted. */
3888 if (GET_CODE (SET_SRC (set)) == LABEL_REF)
3889 emit_barrier_after (insn);
3891 run_jump_opt_after_gcse = 1;
3893 const_prop_count++;
3894 if (gcse_file != NULL)
3896 int regno = REGNO (reg_used->reg_rtx);
3897 fprintf (gcse_file, "CONST-PROP: Replacing reg %d in insn %d with constant ",
3898 regno, INSN_UID (insn));
3899 print_rtl (gcse_file, src);
3900 fprintf (gcse_file, "\n");
3902 return 1;
3904 return 0;
3907 #ifdef HAVE_cc0
3908 /* Subroutine of cprop_insn that tries to propagate constants into
3909 JUMP_INSNS for machines that have CC0. INSN is a single set that
3910 stores into CC0; the insn following it is a conditional jump.
3911 REG_USED is the use we will try to replace, SRC is the constant we
3912 will try to substitute for it.
3913 Returns nonzero if a change was made. */
3914 static int
3915 cprop_cc0_jump (insn, reg_used, src)
3916 rtx insn;
3917 struct reg_use *reg_used;
3918 rtx src;
3920 rtx jump = NEXT_INSN (insn);
3921 rtx copy = copy_rtx (jump);
3922 rtx set = PATTERN (copy);
3924 /* We need to copy the source of the cc0 setter, as cprop_jump is going to
3925 substitute into it. */
3926 replace_rtx (SET_SRC (set), cc0_rtx, copy_rtx (SET_SRC (PATTERN (insn))));
3927 if (! cprop_jump (jump, copy, reg_used, src))
3928 return 0;
3930 /* If we succeeded, delete the cc0 setter. */
3931 PUT_CODE (insn, NOTE);
3932 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
3933 NOTE_SOURCE_FILE (insn) = 0;
3934 return 1;
3936 #endif
3938 /* Perform constant and copy propagation on INSN.
3939 The result is non-zero if a change was made. */
3941 static int
3942 cprop_insn (insn, alter_jumps)
3943 rtx insn;
3944 int alter_jumps;
3946 struct reg_use *reg_used;
3947 int changed = 0;
3948 rtx note;
3950 /* Only propagate into SETs. Note that a conditional jump is a
3951 SET with pc_rtx as the destination. */
3952 if ((GET_CODE (insn) != INSN
3953 && GET_CODE (insn) != JUMP_INSN)
3954 || GET_CODE (PATTERN (insn)) != SET)
3955 return 0;
3957 reg_use_count = 0;
3958 find_used_regs (PATTERN (insn));
3960 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
3961 if (!note)
3962 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
3964 /* We may win even when propagating constants into notes. */
3965 if (note)
3966 find_used_regs (XEXP (note, 0));
3968 reg_used = &reg_use_table[0];
3969 for ( ; reg_use_count > 0; reg_used++, reg_use_count--)
3971 rtx pat, src;
3972 struct expr *set;
3973 int regno = REGNO (reg_used->reg_rtx);
3975 /* Ignore registers created by GCSE.
3976 We do this because ... */
3977 if (regno >= max_gcse_regno)
3978 continue;
3980 /* If the register has already been set in this block, there's
3981 nothing we can do. */
3982 if (! oprs_not_set_p (reg_used->reg_rtx, insn))
3983 continue;
3985 /* Find an assignment that sets reg_used and is available
3986 at the start of the block. */
3987 set = find_avail_set (regno, insn);
3988 if (! set)
3989 continue;
3991 pat = set->expr;
3992 /* ??? We might be able to handle PARALLELs. Later. */
3993 if (GET_CODE (pat) != SET)
3994 abort ();
3995 src = SET_SRC (pat);
3997 /* Constant propagation. */
3998 if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE
3999 || GET_CODE (src) == SYMBOL_REF)
4001 /* Handle normal insns first. */
4002 if (GET_CODE (insn) == INSN
4003 && try_replace_reg (reg_used->reg_rtx, src, insn))
4005 changed = 1;
4006 const_prop_count++;
4007 if (gcse_file != NULL)
4009 fprintf (gcse_file, "CONST-PROP: Replacing reg %d in insn %d with constant ",
4010 regno, INSN_UID (insn));
4011 print_rtl (gcse_file, src);
4012 fprintf (gcse_file, "\n");
4015 /* The original insn setting reg_used may or may not now be
4016 deletable. We leave the deletion to flow. */
4019 /* Try to propagate a CONST_INT into a conditional jump.
4020 We're pretty specific about what we will handle in this
4021 code, we can extend this as necessary over time.
4023 Right now the insn in question must look like
4024 (set (pc) (if_then_else ...)) */
4025 else if (alter_jumps
4026 && GET_CODE (insn) == JUMP_INSN
4027 && condjump_p (insn)
4028 && ! simplejump_p (insn))
4029 changed |= cprop_jump (insn, copy_rtx (insn), reg_used, src);
4030 #ifdef HAVE_cc0
4031 /* Similar code for machines that use a pair of CC0 setter and
4032 conditional jump insn. */
4033 else if (alter_jumps
4034 && GET_CODE (PATTERN (insn)) == SET
4035 && SET_DEST (PATTERN (insn)) == cc0_rtx
4036 && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
4037 && condjump_p (NEXT_INSN (insn))
4038 && ! simplejump_p (NEXT_INSN (insn)))
4039 changed |= cprop_cc0_jump (insn, reg_used, src);
4040 #endif
4042 else if (GET_CODE (src) == REG
4043 && REGNO (src) >= FIRST_PSEUDO_REGISTER
4044 && REGNO (src) != regno)
4046 if (try_replace_reg (reg_used->reg_rtx, src, insn))
4048 changed = 1;
4049 copy_prop_count++;
4050 if (gcse_file != NULL)
4052 fprintf (gcse_file, "COPY-PROP: Replacing reg %d in insn %d with reg %d\n",
4053 regno, INSN_UID (insn), REGNO (src));
4056 /* The original insn setting reg_used may or may not now be
4057 deletable. We leave the deletion to flow. */
4058 /* FIXME: If it turns out that the insn isn't deletable,
4059 then we may have unnecessarily extended register lifetimes
4060 and made things worse. */
4065 return changed;
4068 /* Forward propagate copies.
4069 This includes copies and constants.
4070 Return non-zero if a change was made. */
4072 static int
4073 cprop (alter_jumps)
4074 int alter_jumps;
4076 int bb, changed;
4077 rtx insn;
4079 /* Note we start at block 1. */
4081 changed = 0;
4082 for (bb = 1; bb < n_basic_blocks; bb++)
4084 /* Reset tables used to keep track of what's still valid [since the
4085 start of the block]. */
4086 reset_opr_set_tables ();
4088 for (insn = BLOCK_HEAD (bb);
4089 insn != NULL && insn != NEXT_INSN (BLOCK_END (bb));
4090 insn = NEXT_INSN (insn))
4092 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4094 changed |= cprop_insn (insn, alter_jumps);
4096 /* Keep track of everything modified by this insn. */
4097 /* ??? Need to be careful w.r.t. mods done to INSN. Don't
4098 call mark_oprs_set if we turned the insn into a NOTE. */
4099 if (GET_CODE (insn) != NOTE)
4100 mark_oprs_set (insn);
4105 if (gcse_file != NULL)
4106 fprintf (gcse_file, "\n");
4108 return changed;
4111 /* Perform one copy/constant propagation pass.
4112 F is the first insn in the function.
4113 PASS is the pass count. */
4115 static int
4116 one_cprop_pass (pass, alter_jumps)
4117 int pass;
4118 int alter_jumps;
4120 int changed = 0;
4122 const_prop_count = 0;
4123 copy_prop_count = 0;
4125 alloc_set_hash_table (max_cuid);
4126 compute_set_hash_table ();
4127 if (gcse_file)
4128 dump_hash_table (gcse_file, "SET", set_hash_table, set_hash_table_size,
4129 n_sets);
4130 if (n_sets > 0)
4132 alloc_cprop_mem (n_basic_blocks, n_sets);
4133 compute_cprop_data ();
4134 changed = cprop (alter_jumps);
4135 free_cprop_mem ();
4137 free_set_hash_table ();
4139 if (gcse_file)
4141 fprintf (gcse_file, "CPROP of %s, pass %d: %d bytes needed, %d const props, %d copy props\n",
4142 current_function_name, pass,
4143 bytes_used, const_prop_count, copy_prop_count);
4144 fprintf (gcse_file, "\n");
4147 return changed;
4150 /* Compute PRE+LCM working variables. */
4152 /* Local properties of expressions. */
4153 /* Nonzero for expressions that are transparent in the block. */
4154 static sbitmap *transp;
4156 /* Nonzero for expressions that are transparent at the end of the block.
4157 This is only zero for expressions killed by abnormal critical edge
4158 created by a calls. */
4159 static sbitmap *transpout;
4161 /* Nonzero for expressions that are computed (available) in the block. */
4162 static sbitmap *comp;
4164 /* Nonzero for expressions that are locally anticipatable in the block. */
4165 static sbitmap *antloc;
4167 /* Nonzero for expressions where this block is an optimal computation
4168 point. */
4169 static sbitmap *pre_optimal;
4171 /* Nonzero for expressions which are redundant in a particular block. */
4172 static sbitmap *pre_redundant;
4174 /* Nonzero for expressions which should be inserted on a specific edge. */
4175 static sbitmap *pre_insert_map;
4177 /* Nonzero for expressions which should be deleted in a specific block. */
4178 static sbitmap *pre_delete_map;
4180 /* Contains the edge_list returned by pre_edge_lcm. */
4181 static struct edge_list *edge_list;
4183 static sbitmap *temp_bitmap;
4185 /* Redundant insns. */
4186 static sbitmap pre_redundant_insns;
4188 /* Allocate vars used for PRE analysis. */
4190 static void
4191 alloc_pre_mem (n_blocks, n_exprs)
4192 int n_blocks, n_exprs;
4194 transp = sbitmap_vector_alloc (n_blocks, n_exprs);
4195 comp = sbitmap_vector_alloc (n_blocks, n_exprs);
4196 antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
4197 temp_bitmap = sbitmap_vector_alloc (n_blocks, n_exprs);
4199 pre_optimal = NULL;
4200 pre_redundant = NULL;
4201 pre_insert_map = NULL;
4202 pre_delete_map = NULL;
4203 ae_in = NULL;
4204 ae_out = NULL;
4205 u_bitmap = NULL;
4206 transpout = sbitmap_vector_alloc (n_blocks, n_exprs);
4207 ae_kill = sbitmap_vector_alloc (n_blocks, n_exprs);
4208 /* pre_insert and pre_delete are allocated later. */
4211 /* Free vars used for PRE analysis. */
4213 static void
4214 free_pre_mem ()
4216 free (transp);
4217 free (comp);
4218 free (antloc);
4219 free (temp_bitmap);
4221 if (pre_optimal)
4222 free (pre_optimal);
4223 if (pre_redundant)
4224 free (pre_redundant);
4225 if (pre_insert_map)
4226 free (pre_insert_map);
4227 if (pre_delete_map)
4228 free (pre_delete_map);
4229 if (transpout)
4230 free (transpout);
4232 if (ae_in)
4233 free (ae_in);
4234 if (ae_out)
4235 free (ae_out);
4236 if (ae_kill)
4237 free (ae_kill);
4238 if (u_bitmap)
4239 free (u_bitmap);
4241 transp = comp = antloc = NULL;
4242 pre_optimal = pre_redundant = pre_insert_map = pre_delete_map = NULL;
4243 transpout = ae_in = ae_out = ae_kill = NULL;
4244 u_bitmap = NULL;
4248 /* Top level routine to do the dataflow analysis needed by PRE. */
4250 static void
4251 compute_pre_data ()
4253 compute_local_properties (transp, comp, antloc, 0);
4254 compute_transpout ();
4255 sbitmap_vector_zero (ae_kill, n_basic_blocks);
4256 compute_ae_kill (comp, ae_kill);
4257 edge_list = pre_edge_lcm (gcse_file, n_exprs, transp, comp, antloc,
4258 ae_kill, &pre_insert_map, &pre_delete_map);
4262 /* PRE utilities */
4264 /* Return non-zero if an occurrence of expression EXPR in OCCR_BB would reach
4265 block BB.
4267 VISITED is a pointer to a working buffer for tracking which BB's have
4268 been visited. It is NULL for the top-level call.
4270 We treat reaching expressions that go through blocks containing the same
4271 reaching expression as "not reaching". E.g. if EXPR is generated in blocks
4272 2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
4273 2 as not reaching. The intent is to improve the probability of finding
4274 only one reaching expression and to reduce register lifetimes by picking
4275 the closest such expression. */
4277 static int
4278 pre_expr_reaches_here_p_work (occr_bb, expr, bb, visited)
4279 int occr_bb;
4280 struct expr *expr;
4281 int bb;
4282 char *visited;
4284 edge pred;
4286 for (pred = BASIC_BLOCK (bb)->pred; pred != NULL; pred = pred->pred_next)
4288 int pred_bb = pred->src->index;
4290 if (pred->src == ENTRY_BLOCK_PTR
4291 /* Has predecessor has already been visited? */
4292 || visited[pred_bb])
4294 /* Nothing to do. */
4296 /* Does this predecessor generate this expression? */
4297 else if (TEST_BIT (comp[pred_bb], expr->bitmap_index))
4299 /* Is this the occurrence we're looking for?
4300 Note that there's only one generating occurrence per block
4301 so we just need to check the block number. */
4302 if (occr_bb == pred_bb)
4303 return 1;
4304 visited[pred_bb] = 1;
4306 /* Ignore this predecessor if it kills the expression. */
4307 else if (! TEST_BIT (transp[pred_bb], expr->bitmap_index))
4308 visited[pred_bb] = 1;
4309 /* Neither gen nor kill. */
4310 else
4312 visited[pred_bb] = 1;
4313 if (pre_expr_reaches_here_p_work (occr_bb, expr, pred_bb, visited))
4314 return 1;
4318 /* All paths have been checked. */
4319 return 0;
4322 /* The wrapper for pre_expr_reaches_here_work that ensures that any
4323 memory allocated for that function is returned. */
4325 static int
4326 pre_expr_reaches_here_p (occr_bb, expr, bb)
4327 int occr_bb;
4328 struct expr *expr;
4329 int bb;
4331 int rval;
4332 char * visited = (char *) xcalloc (n_basic_blocks, 1);
4334 rval = pre_expr_reaches_here_p_work(occr_bb, expr, bb, visited);
4336 free (visited);
4338 return (rval);
4342 /* Given an expr, generate RTL which we can insert at the end of a BB,
4343 or on an edge. Set the block number of any insns generated to
4344 the value of BB. */
4346 static rtx
4347 process_insert_insn (expr)
4348 struct expr *expr;
4350 rtx reg = expr->reaching_reg;
4351 rtx pat, copied_expr;
4352 rtx first_new_insn;
4354 start_sequence ();
4355 copied_expr = copy_rtx (expr->expr);
4356 emit_move_insn (reg, copied_expr);
4357 first_new_insn = get_insns ();
4358 pat = gen_sequence ();
4359 end_sequence ();
4361 return pat;
4364 /* Add EXPR to the end of basic block BB.
4366 This is used by both the PRE and code hoisting.
4368 For PRE, we want to verify that the expr is either transparent
4369 or locally anticipatable in the target block. This check makes
4370 no sense for code hoisting. */
4372 static void
4373 insert_insn_end_bb (expr, bb, pre)
4374 struct expr *expr;
4375 int bb;
4376 int pre;
4378 rtx insn = BLOCK_END (bb);
4379 rtx new_insn;
4380 rtx reg = expr->reaching_reg;
4381 int regno = REGNO (reg);
4382 rtx pat;
4384 pat = process_insert_insn (expr);
4386 /* If the last insn is a jump, insert EXPR in front [taking care to
4387 handle cc0, etc. properly]. */
4389 if (GET_CODE (insn) == JUMP_INSN)
4391 #ifdef HAVE_cc0
4392 rtx note;
4393 #endif
4395 /* If this is a jump table, then we can't insert stuff here. Since
4396 we know the previous real insn must be the tablejump, we insert
4397 the new instruction just before the tablejump. */
4398 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
4399 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
4400 insn = prev_real_insn (insn);
4402 #ifdef HAVE_cc0
4403 /* FIXME: 'twould be nice to call prev_cc0_setter here but it aborts
4404 if cc0 isn't set. */
4405 note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
4406 if (note)
4407 insn = XEXP (note, 0);
4408 else
4410 rtx maybe_cc0_setter = prev_nonnote_insn (insn);
4411 if (maybe_cc0_setter
4412 && GET_RTX_CLASS (GET_CODE (maybe_cc0_setter)) == 'i'
4413 && sets_cc0_p (PATTERN (maybe_cc0_setter)))
4414 insn = maybe_cc0_setter;
4416 #endif
4417 /* FIXME: What if something in cc0/jump uses value set in new insn? */
4418 new_insn = emit_insn_before (pat, insn);
4419 if (BLOCK_HEAD (bb) == insn)
4420 BLOCK_HEAD (bb) = new_insn;
4422 /* Likewise if the last insn is a call, as will happen in the presence
4423 of exception handling. */
4424 else if (GET_CODE (insn) == CALL_INSN)
4426 HARD_REG_SET parm_regs;
4427 int nparm_regs;
4428 rtx p;
4430 /* Keeping in mind SMALL_REGISTER_CLASSES and parameters in registers,
4431 we search backward and place the instructions before the first
4432 parameter is loaded. Do this for everyone for consistency and a
4433 presumtion that we'll get better code elsewhere as well. */
4435 /* It should always be the case that we can put these instructions
4436 anywhere in the basic block with performing PRE optimizations.
4437 Check this. */
4438 if (pre
4439 && !TEST_BIT (antloc[bb], expr->bitmap_index)
4440 && !TEST_BIT (transp[bb], expr->bitmap_index))
4441 abort ();
4443 /* Since different machines initialize their parameter registers
4444 in different orders, assume nothing. Collect the set of all
4445 parameter registers. */
4446 CLEAR_HARD_REG_SET (parm_regs);
4447 nparm_regs = 0;
4448 for (p = CALL_INSN_FUNCTION_USAGE (insn); p ; p = XEXP (p, 1))
4449 if (GET_CODE (XEXP (p, 0)) == USE
4450 && GET_CODE (XEXP (XEXP (p, 0), 0)) == REG)
4452 int regno = REGNO (XEXP (XEXP (p, 0), 0));
4453 if (regno >= FIRST_PSEUDO_REGISTER)
4454 abort ();
4455 SET_HARD_REG_BIT (parm_regs, regno);
4456 nparm_regs++;
4459 /* Search backward for the first set of a register in this set. */
4460 while (nparm_regs && BLOCK_HEAD (bb) != insn)
4462 insn = PREV_INSN (insn);
4463 p = single_set (insn);
4464 if (p && GET_CODE (SET_DEST (p)) == REG
4465 && REGNO (SET_DEST (p)) < FIRST_PSEUDO_REGISTER
4466 && TEST_HARD_REG_BIT (parm_regs, REGNO (SET_DEST (p))))
4468 CLEAR_HARD_REG_BIT (parm_regs, REGNO (SET_DEST (p)));
4469 nparm_regs--;
4473 /* If we found all the parameter loads, then we want to insert
4474 before the first parameter load.
4476 If we did not find all the parameter loads, then we might have
4477 stopped on the head of the block, which could be a CODE_LABEL.
4478 If we inserted before the CODE_LABEL, then we would be putting
4479 the insn in the wrong basic block. In that case, put the insn
4480 after the CODE_LABEL.
4482 ?!? Do we need to account for NOTE_INSN_BASIC_BLOCK here? */
4483 if (GET_CODE (insn) != CODE_LABEL)
4485 new_insn = emit_insn_before (pat, insn);
4486 if (BLOCK_HEAD (bb) == insn)
4487 BLOCK_HEAD (bb) = new_insn;
4489 else
4491 new_insn = emit_insn_after (pat, insn);
4494 else
4496 new_insn = emit_insn_after (pat, insn);
4497 BLOCK_END (bb) = new_insn;
4500 /* Keep block number table up to date.
4501 Note, PAT could be a multiple insn sequence, we have to make
4502 sure that each insn in the sequence is handled. */
4503 if (GET_CODE (pat) == SEQUENCE)
4505 int i;
4507 for (i = 0; i < XVECLEN (pat, 0); i++)
4509 rtx insn = XVECEXP (pat, 0, i);
4510 set_block_num (insn, bb);
4511 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4512 add_label_notes (PATTERN (insn), new_insn);
4513 note_stores (PATTERN (insn), record_set_info, insn);
4516 else
4518 add_label_notes (SET_SRC (pat), new_insn);
4519 set_block_num (new_insn, bb);
4520 /* Keep register set table up to date. */
4521 record_one_set (regno, new_insn);
4524 gcse_create_count++;
4526 if (gcse_file)
4528 fprintf (gcse_file, "PRE/HOIST: end of bb %d, insn %d, copying expression %d to reg %d\n",
4529 bb, INSN_UID (new_insn), expr->bitmap_index, regno);
4533 /* Insert partially redundant expressions on edges in the CFG to make
4534 the expressions fully redundant. */
4536 static int
4537 pre_edge_insert (edge_list, index_map)
4538 struct edge_list *edge_list;
4539 struct expr **index_map;
4541 int e, i, num_edges, set_size, did_insert = 0;
4542 sbitmap *inserted;
4544 /* Where PRE_INSERT_MAP is nonzero, we add the expression on that edge
4545 if it reaches any of the deleted expressions. */
4547 set_size = pre_insert_map[0]->size;
4548 num_edges = NUM_EDGES (edge_list);
4549 inserted = sbitmap_vector_alloc (num_edges, n_exprs);
4550 sbitmap_vector_zero (inserted, num_edges);
4552 for (e = 0; e < num_edges; e++)
4554 int indx;
4555 basic_block pred = INDEX_EDGE_PRED_BB (edge_list, e);
4556 int bb = pred->index;
4558 for (i = indx = 0; i < set_size; i++, indx += SBITMAP_ELT_BITS)
4560 SBITMAP_ELT_TYPE insert = pre_insert_map[e]->elms[i];
4561 int j;
4563 for (j = indx; insert && j < n_exprs; j++, insert >>= 1)
4565 if ((insert & 1) != 0 && index_map[j]->reaching_reg != NULL_RTX)
4567 struct expr *expr = index_map[j];
4568 struct occr *occr;
4570 /* Now look at each deleted occurence of this expression. */
4571 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
4573 if (! occr->deleted_p)
4574 continue;
4576 /* Insert this expression on this edge if if it would
4577 reach the deleted occurence in BB. */
4578 if (!TEST_BIT (inserted[e], j))
4580 rtx insn;
4581 edge eg = INDEX_EDGE (edge_list, e);
4582 /* We can't insert anything on an abnormal
4583 and critical edge, so we insert the
4584 insn at the end of the previous block. There
4585 are several alternatives detailed in
4586 Morgans book P277 (sec 10.5) for handling
4587 this situation. This one is easiest for now. */
4589 if ((eg->flags & EDGE_ABNORMAL) == EDGE_ABNORMAL)
4591 insert_insn_end_bb (index_map[j], bb, 0);
4593 else
4595 insn = process_insert_insn (index_map[j]);
4596 insert_insn_on_edge (insn, eg);
4598 if (gcse_file)
4600 fprintf (gcse_file,
4601 "PRE/HOIST: edge (%d,%d), copy expression %d\n",
4603 INDEX_EDGE_SUCC_BB (edge_list, e)->index, expr->bitmap_index);
4605 SET_BIT (inserted[e], j);
4606 did_insert = 1;
4607 gcse_create_count++;
4615 /* Clean up. */
4616 free (inserted);
4618 return did_insert;
4621 /* Copy the result of INSN to REG.
4622 INDX is the expression number. */
4624 static void
4625 pre_insert_copy_insn (expr, insn)
4626 struct expr *expr;
4627 rtx insn;
4629 rtx reg = expr->reaching_reg;
4630 int regno = REGNO (reg);
4631 int indx = expr->bitmap_index;
4632 rtx set = single_set (insn);
4633 rtx new_insn;
4634 int bb = BLOCK_NUM (insn);
4636 if (!set)
4637 abort ();
4638 new_insn = emit_insn_after (gen_rtx_SET (VOIDmode, reg, SET_DEST (set)),
4639 insn);
4640 /* Keep block number table up to date. */
4641 set_block_num (new_insn, bb);
4642 /* Keep register set table up to date. */
4643 record_one_set (regno, new_insn);
4644 if (insn == BLOCK_END (bb))
4645 BLOCK_END (bb) = new_insn;
4647 gcse_create_count++;
4649 if (gcse_file)
4650 fprintf (gcse_file,
4651 "PRE: bb %d, insn %d, copy expression %d in insn %d to reg %d\n",
4652 BLOCK_NUM (insn), INSN_UID (new_insn), indx,
4653 INSN_UID (insn), regno);
4656 /* Copy available expressions that reach the redundant expression
4657 to `reaching_reg'. */
4659 static void
4660 pre_insert_copies ()
4662 int i;
4664 /* For each available expression in the table, copy the result to
4665 `reaching_reg' if the expression reaches a deleted one.
4667 ??? The current algorithm is rather brute force.
4668 Need to do some profiling. */
4670 for (i = 0; i < expr_hash_table_size; i++)
4672 struct expr *expr;
4674 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
4676 struct occr *occr;
4678 /* If the basic block isn't reachable, PPOUT will be TRUE.
4679 However, we don't want to insert a copy here because the
4680 expression may not really be redundant. So only insert
4681 an insn if the expression was deleted.
4682 This test also avoids further processing if the expression
4683 wasn't deleted anywhere. */
4684 if (expr->reaching_reg == NULL)
4685 continue;
4687 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
4689 struct occr *avail;
4691 if (! occr->deleted_p)
4692 continue;
4694 for (avail = expr->avail_occr; avail != NULL; avail = avail->next)
4696 rtx insn = avail->insn;
4698 /* No need to handle this one if handled already. */
4699 if (avail->copied_p)
4700 continue;
4701 /* Don't handle this one if it's a redundant one. */
4702 if (TEST_BIT (pre_redundant_insns, INSN_CUID (insn)))
4703 continue;
4704 /* Or if the expression doesn't reach the deleted one. */
4705 if (! pre_expr_reaches_here_p (BLOCK_NUM (avail->insn), expr,
4706 BLOCK_NUM (occr->insn)))
4707 continue;
4709 /* Copy the result of avail to reaching_reg. */
4710 pre_insert_copy_insn (expr, insn);
4711 avail->copied_p = 1;
4718 /* Delete redundant computations.
4719 Deletion is done by changing the insn to copy the `reaching_reg' of
4720 the expression into the result of the SET. It is left to later passes
4721 (cprop, cse2, flow, combine, regmove) to propagate the copy or eliminate it.
4723 Returns non-zero if a change is made. */
4725 static int
4726 pre_delete ()
4728 int i, bb, changed;
4730 /* Compute the expressions which are redundant and need to be replaced by
4731 copies from the reaching reg to the target reg. */
4732 for (bb = 0; bb < n_basic_blocks; bb++)
4733 sbitmap_copy (temp_bitmap[bb], pre_delete_map[bb]);
4735 changed = 0;
4736 for (i = 0; i < expr_hash_table_size; i++)
4738 struct expr *expr;
4740 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
4742 struct occr *occr;
4743 int indx = expr->bitmap_index;
4745 /* We only need to search antic_occr since we require
4746 ANTLOC != 0. */
4748 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
4750 rtx insn = occr->insn;
4751 rtx set;
4752 int bb = BLOCK_NUM (insn);
4754 if (TEST_BIT (temp_bitmap[bb], indx))
4756 set = single_set (insn);
4757 if (! set)
4758 abort ();
4760 /* Create a pseudo-reg to store the result of reaching
4761 expressions into. Get the mode for the new pseudo
4762 from the mode of the original destination pseudo. */
4763 if (expr->reaching_reg == NULL)
4764 expr->reaching_reg
4765 = gen_reg_rtx (GET_MODE (SET_DEST (set)));
4767 /* In theory this should never fail since we're creating
4768 a reg->reg copy.
4770 However, on the x86 some of the movXX patterns actually
4771 contain clobbers of scratch regs. This may cause the
4772 insn created by validate_change to not match any pattern
4773 and thus cause validate_change to fail. */
4774 if (validate_change (insn, &SET_SRC (set),
4775 expr->reaching_reg, 0))
4777 occr->deleted_p = 1;
4778 SET_BIT (pre_redundant_insns, INSN_CUID (insn));
4779 changed = 1;
4780 gcse_subst_count++;
4783 if (gcse_file)
4785 fprintf (gcse_file,
4786 "PRE: redundant insn %d (expression %d) in bb %d, reaching reg is %d\n",
4787 INSN_UID (insn), indx, bb, REGNO (expr->reaching_reg));
4794 return changed;
4797 /* Perform GCSE optimizations using PRE.
4798 This is called by one_pre_gcse_pass after all the dataflow analysis
4799 has been done.
4801 This is based on the original Morel-Renvoise paper Fred Chow's thesis,
4802 and lazy code motion from Knoop, Ruthing and Steffen as described in
4803 Advanced Compiler Design and Implementation.
4805 ??? A new pseudo reg is created to hold the reaching expression.
4806 The nice thing about the classical approach is that it would try to
4807 use an existing reg. If the register can't be adequately optimized
4808 [i.e. we introduce reload problems], one could add a pass here to
4809 propagate the new register through the block.
4811 ??? We don't handle single sets in PARALLELs because we're [currently]
4812 not able to copy the rest of the parallel when we insert copies to create
4813 full redundancies from partial redundancies. However, there's no reason
4814 why we can't handle PARALLELs in the cases where there are no partial
4815 redundancies. */
4817 static int
4818 pre_gcse ()
4820 int i, did_insert;
4821 int changed;
4822 struct expr **index_map;
4824 /* Compute a mapping from expression number (`bitmap_index') to
4825 hash table entry. */
4827 index_map = (struct expr **) xcalloc (n_exprs, sizeof (struct expr *));
4828 for (i = 0; i < expr_hash_table_size; i++)
4830 struct expr *expr;
4832 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
4833 index_map[expr->bitmap_index] = expr;
4836 /* Reset bitmap used to track which insns are redundant. */
4837 pre_redundant_insns = sbitmap_alloc (max_cuid);
4838 sbitmap_zero (pre_redundant_insns);
4840 /* Delete the redundant insns first so that
4841 - we know what register to use for the new insns and for the other
4842 ones with reaching expressions
4843 - we know which insns are redundant when we go to create copies */
4844 changed = pre_delete ();
4846 did_insert = pre_edge_insert (edge_list, index_map);
4847 /* In other places with reaching expressions, copy the expression to the
4848 specially allocated pseudo-reg that reaches the redundant expr. */
4849 pre_insert_copies ();
4850 if (did_insert)
4852 commit_edge_insertions ();
4853 changed = 1;
4856 free (index_map);
4857 free (pre_redundant_insns);
4859 return changed;
4862 /* Top level routine to perform one PRE GCSE pass.
4864 Return non-zero if a change was made. */
4866 static int
4867 one_pre_gcse_pass (pass)
4868 int pass;
4870 int changed = 0;
4872 gcse_subst_count = 0;
4873 gcse_create_count = 0;
4875 alloc_expr_hash_table (max_cuid);
4876 add_noreturn_fake_exit_edges ();
4877 compute_expr_hash_table ();
4878 if (gcse_file)
4879 dump_hash_table (gcse_file, "Expression", expr_hash_table,
4880 expr_hash_table_size, n_exprs);
4881 if (n_exprs > 0)
4883 alloc_pre_mem (n_basic_blocks, n_exprs);
4884 compute_pre_data ();
4885 changed |= pre_gcse ();
4886 free_edge_list (edge_list);
4887 free_pre_mem ();
4889 remove_fake_edges ();
4890 free_expr_hash_table ();
4892 if (gcse_file)
4894 fprintf (gcse_file, "\n");
4895 fprintf (gcse_file, "PRE GCSE of %s, pass %d: %d bytes needed, %d substs, %d insns created\n",
4896 current_function_name, pass,
4897 bytes_used, gcse_subst_count, gcse_create_count);
4900 return changed;
4903 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to INSN.
4904 We have to add REG_LABEL notes, because the following loop optimization
4905 pass requires them. */
4907 /* ??? This is very similar to the loop.c add_label_notes function. We
4908 could probably share code here. */
4910 /* ??? If there was a jump optimization pass after gcse and before loop,
4911 then we would not need to do this here, because jump would add the
4912 necessary REG_LABEL notes. */
4914 static void
4915 add_label_notes (x, insn)
4916 rtx x;
4917 rtx insn;
4919 enum rtx_code code = GET_CODE (x);
4920 int i, j;
4921 const char *fmt;
4923 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
4925 /* This code used to ignore labels that referred to dispatch tables to
4926 avoid flow generating (slighly) worse code.
4928 We no longer ignore such label references (see LABEL_REF handling in
4929 mark_jump_label for additional information). */
4930 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL, XEXP (x, 0),
4931 REG_NOTES (insn));
4932 return;
4935 fmt = GET_RTX_FORMAT (code);
4936 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4938 if (fmt[i] == 'e')
4939 add_label_notes (XEXP (x, i), insn);
4940 else if (fmt[i] == 'E')
4941 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4942 add_label_notes (XVECEXP (x, i, j), insn);
4946 /* Compute transparent outgoing information for each block.
4948 An expression is transparent to an edge unless it is killed by
4949 the edge itself. This can only happen with abnormal control flow,
4950 when the edge is traversed through a call. This happens with
4951 non-local labels and exceptions.
4953 This would not be necessary if we split the edge. While this is
4954 normally impossible for abnormal critical edges, with some effort
4955 it should be possible with exception handling, since we still have
4956 control over which handler should be invoked. But due to increased
4957 EH table sizes, this may not be worthwhile. */
4959 static void
4960 compute_transpout ()
4962 int bb;
4964 sbitmap_vector_ones (transpout, n_basic_blocks);
4966 for (bb = 0; bb < n_basic_blocks; ++bb)
4968 int i;
4970 /* Note that flow inserted a nop a the end of basic blocks that
4971 end in call instructions for reasons other than abnormal
4972 control flow. */
4973 if (GET_CODE (BLOCK_END (bb)) != CALL_INSN)
4974 continue;
4976 for (i = 0; i < expr_hash_table_size; i++)
4978 struct expr *expr;
4979 for (expr = expr_hash_table[i]; expr ; expr = expr->next_same_hash)
4980 if (GET_CODE (expr->expr) == MEM)
4982 rtx addr = XEXP (expr->expr, 0);
4984 if (GET_CODE (addr) == SYMBOL_REF
4985 && CONSTANT_POOL_ADDRESS_P (addr))
4986 continue;
4988 /* ??? Optimally, we would use interprocedural alias
4989 analysis to determine if this mem is actually killed
4990 by this call. */
4991 RESET_BIT (transpout[bb], expr->bitmap_index);
4997 /* Removal of useless null pointer checks */
4999 /* Called via note_stores. X is set by SETTER. If X is a register we must
5000 invalidate nonnull_local and set nonnull_killed. DATA is really a
5001 `null_pointer_info *'.
5003 We ignore hard registers. */
5004 static void
5005 invalidate_nonnull_info (x, setter, data)
5006 rtx x;
5007 rtx setter ATTRIBUTE_UNUSED;
5008 void *data;
5010 int offset, regno;
5011 struct null_pointer_info* npi = (struct null_pointer_info *) data;
5013 offset = 0;
5014 while (GET_CODE (x) == SUBREG)
5015 x = SUBREG_REG (x);
5017 /* Ignore anything that is not a register or is a hard register. */
5018 if (GET_CODE (x) != REG
5019 || REGNO (x) < npi->min_reg
5020 || REGNO (x) >= npi->max_reg)
5021 return;
5023 regno = REGNO (x) - npi->min_reg;
5025 RESET_BIT (npi->nonnull_local[npi->current_block], regno);
5026 SET_BIT (npi->nonnull_killed[npi->current_block], regno);
5029 /* Do null-pointer check elimination for the registers indicated in
5030 NPI. NONNULL_AVIN and NONNULL_AVOUT are pre-allocated sbitmaps;
5031 they are not our responsibility to free. */
5033 static void
5034 delete_null_pointer_checks_1 (block_reg, nonnull_avin, nonnull_avout, npi)
5035 int *block_reg;
5036 sbitmap *nonnull_avin;
5037 sbitmap *nonnull_avout;
5038 struct null_pointer_info *npi;
5040 int bb;
5041 int current_block;
5042 sbitmap *nonnull_local = npi->nonnull_local;
5043 sbitmap *nonnull_killed = npi->nonnull_killed;
5045 /* Compute local properties, nonnull and killed. A register will have
5046 the nonnull property if at the end of the current block its value is
5047 known to be nonnull. The killed property indicates that somewhere in
5048 the block any information we had about the register is killed.
5050 Note that a register can have both properties in a single block. That
5051 indicates that it's killed, then later in the block a new value is
5052 computed. */
5053 sbitmap_vector_zero (nonnull_local, n_basic_blocks);
5054 sbitmap_vector_zero (nonnull_killed, n_basic_blocks);
5055 for (current_block = 0; current_block < n_basic_blocks; current_block++)
5057 rtx insn, stop_insn;
5059 /* Set the current block for invalidate_nonnull_info. */
5060 npi->current_block = current_block;
5062 /* Scan each insn in the basic block looking for memory references and
5063 register sets. */
5064 stop_insn = NEXT_INSN (BLOCK_END (current_block));
5065 for (insn = BLOCK_HEAD (current_block);
5066 insn != stop_insn;
5067 insn = NEXT_INSN (insn))
5069 rtx set;
5070 rtx reg;
5072 /* Ignore anything that is not a normal insn. */
5073 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
5074 continue;
5076 /* Basically ignore anything that is not a simple SET. We do have
5077 to make sure to invalidate nonnull_local and set nonnull_killed
5078 for such insns though. */
5079 set = single_set (insn);
5080 if (!set)
5082 note_stores (PATTERN (insn), invalidate_nonnull_info, npi);
5083 continue;
5086 /* See if we've got a useable memory load. We handle it first
5087 in case it uses its address register as a dest (which kills
5088 the nonnull property). */
5089 if (GET_CODE (SET_SRC (set)) == MEM
5090 && GET_CODE ((reg = XEXP (SET_SRC (set), 0))) == REG
5091 && REGNO (reg) >= npi->min_reg
5092 && REGNO (reg) < npi->max_reg)
5093 SET_BIT (nonnull_local[current_block],
5094 REGNO (reg) - npi->min_reg);
5096 /* Now invalidate stuff clobbered by this insn. */
5097 note_stores (PATTERN (insn), invalidate_nonnull_info, npi);
5099 /* And handle stores, we do these last since any sets in INSN can
5100 not kill the nonnull property if it is derived from a MEM
5101 appearing in a SET_DEST. */
5102 if (GET_CODE (SET_DEST (set)) == MEM
5103 && GET_CODE ((reg = XEXP (SET_DEST (set), 0))) == REG
5104 && REGNO (reg) >= npi->min_reg
5105 && REGNO (reg) < npi->max_reg)
5106 SET_BIT (nonnull_local[current_block],
5107 REGNO (reg) - npi->min_reg);
5111 /* Now compute global properties based on the local properties. This
5112 is a classic global availablity algorithm. */
5113 compute_available (nonnull_local, nonnull_killed,
5114 nonnull_avout, nonnull_avin);
5116 /* Now look at each bb and see if it ends with a compare of a value
5117 against zero. */
5118 for (bb = 0; bb < n_basic_blocks; bb++)
5120 rtx last_insn = BLOCK_END (bb);
5121 rtx condition, earliest;
5122 int compare_and_branch;
5124 /* Since MIN_REG is always at least FIRST_PSEUDO_REGISTER, and
5125 since BLOCK_REG[BB] is zero if this block did not end with a
5126 comparison against zero, this condition works. */
5127 if (block_reg[bb] < npi->min_reg
5128 || block_reg[bb] >= npi->max_reg)
5129 continue;
5131 /* LAST_INSN is a conditional jump. Get its condition. */
5132 condition = get_condition (last_insn, &earliest);
5134 /* If we can't determine the condition then skip. */
5135 if (! condition)
5136 continue;
5138 /* Is the register known to have a nonzero value? */
5139 if (!TEST_BIT (nonnull_avout[bb], block_reg[bb] - npi->min_reg))
5140 continue;
5142 /* Try to compute whether the compare/branch at the loop end is one or
5143 two instructions. */
5144 if (earliest == last_insn)
5145 compare_and_branch = 1;
5146 else if (earliest == prev_nonnote_insn (last_insn))
5147 compare_and_branch = 2;
5148 else
5149 continue;
5151 /* We know the register in this comparison is nonnull at exit from
5152 this block. We can optimize this comparison. */
5153 if (GET_CODE (condition) == NE)
5155 rtx new_jump;
5157 new_jump = emit_jump_insn_before (gen_jump (JUMP_LABEL (last_insn)),
5158 last_insn);
5159 JUMP_LABEL (new_jump) = JUMP_LABEL (last_insn);
5160 LABEL_NUSES (JUMP_LABEL (new_jump))++;
5161 emit_barrier_after (new_jump);
5163 delete_insn (last_insn);
5164 if (compare_and_branch == 2)
5165 delete_insn (earliest);
5167 /* Don't check this block again. (Note that BLOCK_END is
5168 invalid here; we deleted the last instruction in the
5169 block.) */
5170 block_reg[bb] = 0;
5174 /* Find EQ/NE comparisons against zero which can be (indirectly) evaluated
5175 at compile time.
5177 This is conceptually similar to global constant/copy propagation and
5178 classic global CSE (it even uses the same dataflow equations as cprop).
5180 If a register is used as memory address with the form (mem (reg)), then we
5181 know that REG can not be zero at that point in the program. Any instruction
5182 which sets REG "kills" this property.
5184 So, if every path leading to a conditional branch has an available memory
5185 reference of that form, then we know the register can not have the value
5186 zero at the conditional branch.
5188 So we merely need to compute the local properies and propagate that data
5189 around the cfg, then optimize where possible.
5191 We run this pass two times. Once before CSE, then again after CSE. This
5192 has proven to be the most profitable approach. It is rare for new
5193 optimization opportunities of this nature to appear after the first CSE
5194 pass.
5196 This could probably be integrated with global cprop with a little work. */
5198 void
5199 delete_null_pointer_checks (f)
5200 rtx f;
5202 sbitmap *nonnull_avin, *nonnull_avout;
5203 int *block_reg;
5204 int bb;
5205 int reg;
5206 int regs_per_pass;
5207 int max_reg;
5208 struct null_pointer_info npi;
5210 /* First break the program into basic blocks. */
5211 find_basic_blocks (f, max_reg_num (), NULL, 1);
5213 /* If we have only a single block, then there's nothing to do. */
5214 if (n_basic_blocks <= 1)
5216 /* Free storage allocated by find_basic_blocks. */
5217 free_basic_block_vars (0);
5218 return;
5221 /* Trying to perform global optimizations on flow graphs which have
5222 a high connectivity will take a long time and is unlikely to be
5223 particularly useful.
5225 In normal circumstances a cfg should have about twice has many edges
5226 as blocks. But we do not want to punish small functions which have
5227 a couple switch statements. So we require a relatively large number
5228 of basic blocks and the ratio of edges to blocks to be high. */
5229 if (n_basic_blocks > 1000 && n_edges / n_basic_blocks >= 20)
5231 /* Free storage allocated by find_basic_blocks. */
5232 free_basic_block_vars (0);
5233 return;
5236 /* We need four bitmaps, each with a bit for each register in each
5237 basic block. */
5238 max_reg = max_reg_num ();
5239 regs_per_pass = get_bitmap_width (4, n_basic_blocks, max_reg);
5241 /* Allocate bitmaps to hold local and global properties. */
5242 npi.nonnull_local = sbitmap_vector_alloc (n_basic_blocks, regs_per_pass);
5243 npi.nonnull_killed = sbitmap_vector_alloc (n_basic_blocks, regs_per_pass);
5244 nonnull_avin = sbitmap_vector_alloc (n_basic_blocks, regs_per_pass);
5245 nonnull_avout = sbitmap_vector_alloc (n_basic_blocks, regs_per_pass);
5247 /* Go through the basic blocks, seeing whether or not each block
5248 ends with a conditional branch whose condition is a comparison
5249 against zero. Record the register compared in BLOCK_REG. */
5250 block_reg = (int *) xcalloc (n_basic_blocks, sizeof (int));
5251 for (bb = 0; bb < n_basic_blocks; bb++)
5253 rtx last_insn = BLOCK_END (bb);
5254 rtx condition, earliest, reg;
5256 /* We only want conditional branches. */
5257 if (GET_CODE (last_insn) != JUMP_INSN
5258 || !condjump_p (last_insn)
5259 || simplejump_p (last_insn))
5260 continue;
5262 /* LAST_INSN is a conditional jump. Get its condition. */
5263 condition = get_condition (last_insn, &earliest);
5265 /* If we were unable to get the condition, or it is not a equality
5266 comparison against zero then there's nothing we can do. */
5267 if (!condition
5268 || (GET_CODE (condition) != NE && GET_CODE (condition) != EQ)
5269 || GET_CODE (XEXP (condition, 1)) != CONST_INT
5270 || (XEXP (condition, 1)
5271 != CONST0_RTX (GET_MODE (XEXP (condition, 0)))))
5272 continue;
5274 /* We must be checking a register against zero. */
5275 reg = XEXP (condition, 0);
5276 if (GET_CODE (reg) != REG)
5277 continue;
5279 block_reg[bb] = REGNO (reg);
5282 /* Go through the algorithm for each block of registers. */
5283 for (reg = FIRST_PSEUDO_REGISTER; reg < max_reg; reg += regs_per_pass)
5285 npi.min_reg = reg;
5286 npi.max_reg = MIN (reg + regs_per_pass, max_reg);
5287 delete_null_pointer_checks_1 (block_reg, nonnull_avin,
5288 nonnull_avout, &npi);
5291 /* Free storage allocated by find_basic_blocks. */
5292 free_basic_block_vars (0);
5294 /* Free the table of registers compared at the end of every block. */
5295 free (block_reg);
5297 /* Free bitmaps. */
5298 free (npi.nonnull_local);
5299 free (npi.nonnull_killed);
5300 free (nonnull_avin);
5301 free (nonnull_avout);
5304 /* Code Hoisting variables and subroutines. */
5306 /* Very busy expressions. */
5307 static sbitmap *hoist_vbein;
5308 static sbitmap *hoist_vbeout;
5310 /* Hoistable expressions. */
5311 static sbitmap *hoist_exprs;
5313 /* Dominator bitmaps. */
5314 static sbitmap *dominators;
5316 /* ??? We could compute post dominators and run this algorithm in
5317 reverse to to perform tail merging, doing so would probably be
5318 more effective than the tail merging code in jump.c.
5320 It's unclear if tail merging could be run in parallel with
5321 code hoisting. It would be nice. */
5323 /* Allocate vars used for code hoisting analysis. */
5325 static void
5326 alloc_code_hoist_mem (n_blocks, n_exprs)
5327 int n_blocks, n_exprs;
5329 antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
5330 transp = sbitmap_vector_alloc (n_blocks, n_exprs);
5331 comp = sbitmap_vector_alloc (n_blocks, n_exprs);
5333 hoist_vbein = sbitmap_vector_alloc (n_blocks, n_exprs);
5334 hoist_vbeout = sbitmap_vector_alloc (n_blocks, n_exprs);
5335 hoist_exprs = sbitmap_vector_alloc (n_blocks, n_exprs);
5336 transpout = sbitmap_vector_alloc (n_blocks, n_exprs);
5338 dominators = sbitmap_vector_alloc (n_blocks, n_blocks);
5341 /* Free vars used for code hoisting analysis. */
5343 static void
5344 free_code_hoist_mem ()
5346 free (antloc);
5347 free (transp);
5348 free (comp);
5350 free (hoist_vbein);
5351 free (hoist_vbeout);
5352 free (hoist_exprs);
5353 free (transpout);
5355 free (dominators);
5358 /* Compute the very busy expressions at entry/exit from each block.
5360 An expression is very busy if all paths from a given point
5361 compute the expression. */
5363 static void
5364 compute_code_hoist_vbeinout ()
5366 int bb, changed, passes;
5368 sbitmap_vector_zero (hoist_vbeout, n_basic_blocks);
5369 sbitmap_vector_zero (hoist_vbein, n_basic_blocks);
5371 passes = 0;
5372 changed = 1;
5373 while (changed)
5375 changed = 0;
5376 /* We scan the blocks in the reverse order to speed up
5377 the convergence. */
5378 for (bb = n_basic_blocks - 1; bb >= 0; bb--)
5380 changed |= sbitmap_a_or_b_and_c (hoist_vbein[bb], antloc[bb],
5381 hoist_vbeout[bb], transp[bb]);
5382 if (bb != n_basic_blocks - 1)
5383 sbitmap_intersection_of_succs (hoist_vbeout[bb], hoist_vbein, bb);
5385 passes++;
5388 if (gcse_file)
5389 fprintf (gcse_file, "hoisting vbeinout computation: %d passes\n", passes);
5392 /* Top level routine to do the dataflow analysis needed by code hoisting. */
5394 static void
5395 compute_code_hoist_data ()
5397 compute_local_properties (transp, comp, antloc, 0);
5398 compute_transpout ();
5399 compute_code_hoist_vbeinout ();
5400 compute_flow_dominators (dominators, NULL);
5401 if (gcse_file)
5402 fprintf (gcse_file, "\n");
5405 /* Determine if the expression identified by EXPR_INDEX would
5406 reach BB unimpared if it was placed at the end of EXPR_BB.
5408 It's unclear exactly what Muchnick meant by "unimpared". It seems
5409 to me that the expression must either be computed or transparent in
5410 *every* block in the path(s) from EXPR_BB to BB. Any other definition
5411 would allow the expression to be hoisted out of loops, even if
5412 the expression wasn't a loop invariant.
5414 Contrast this to reachability for PRE where an expression is
5415 considered reachable if *any* path reaches instead of *all*
5416 paths. */
5418 static int
5419 hoist_expr_reaches_here_p (expr_bb, expr_index, bb, visited)
5420 int expr_bb;
5421 int expr_index;
5422 int bb;
5423 char *visited;
5425 edge pred;
5426 int visited_allocated_locally = 0;
5429 if (visited == NULL)
5431 visited_allocated_locally = 1;
5432 visited = xcalloc (n_basic_blocks, 1);
5435 visited[expr_bb] = 1;
5436 for (pred = BASIC_BLOCK (bb)->pred; pred != NULL; pred = pred->pred_next)
5438 int pred_bb = pred->src->index;
5440 if (pred->src == ENTRY_BLOCK_PTR)
5441 break;
5442 else if (visited[pred_bb])
5443 continue;
5444 /* Does this predecessor generate this expression? */
5445 else if (TEST_BIT (comp[pred_bb], expr_index))
5446 break;
5447 else if (! TEST_BIT (transp[pred_bb], expr_index))
5448 break;
5449 /* Not killed. */
5450 else
5452 visited[pred_bb] = 1;
5453 if (! hoist_expr_reaches_here_p (expr_bb, expr_index,
5454 pred_bb, visited))
5455 break;
5458 if (visited_allocated_locally)
5459 free (visited);
5460 return (pred == NULL);
5463 /* Actually perform code hoisting. */
5464 static void
5465 hoist_code ()
5467 int bb, dominated, i;
5468 struct expr **index_map;
5470 sbitmap_vector_zero (hoist_exprs, n_basic_blocks);
5472 /* Compute a mapping from expression number (`bitmap_index') to
5473 hash table entry. */
5475 index_map = (struct expr **) xcalloc (n_exprs, sizeof (struct expr *));
5476 for (i = 0; i < expr_hash_table_size; i++)
5478 struct expr *expr;
5480 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
5481 index_map[expr->bitmap_index] = expr;
5484 /* Walk over each basic block looking for potentially hoistable
5485 expressions, nothing gets hoisted from the entry block. */
5486 for (bb = 0; bb < n_basic_blocks; bb++)
5488 int found = 0;
5489 int insn_inserted_p;
5491 /* Examine each expression that is very busy at the exit of this
5492 block. These are the potentially hoistable expressions. */
5493 for (i = 0; i < hoist_vbeout[bb]->n_bits; i++)
5495 int hoistable = 0;
5496 if (TEST_BIT (hoist_vbeout[bb], i)
5497 && TEST_BIT (transpout[bb], i))
5499 /* We've found a potentially hoistable expression, now
5500 we look at every block BB dominates to see if it
5501 computes the expression. */
5502 for (dominated = 0; dominated < n_basic_blocks; dominated++)
5504 /* Ignore self dominance. */
5505 if (bb == dominated
5506 || ! TEST_BIT (dominators[dominated], bb))
5507 continue;
5509 /* We've found a dominated block, now see if it computes
5510 the busy expression and whether or not moving that
5511 expression to the "beginning" of that block is safe. */
5512 if (!TEST_BIT (antloc[dominated], i))
5513 continue;
5515 /* Note if the expression would reach the dominated block
5516 unimpared if it was placed at the end of BB.
5518 Keep track of how many times this expression is hoistable
5519 from a dominated block into BB. */
5520 if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
5521 hoistable++;
5524 /* If we found more than one hoistable occurence of this
5525 expression, then note it in the bitmap of expressions to
5526 hoist. It makes no sense to hoist things which are computed
5527 in only one BB, and doing so tends to pessimize register
5528 allocation. One could increase this value to try harder
5529 to avoid any possible code expansion due to register
5530 allocation issues; however experiments have shown that
5531 the vast majority of hoistable expressions are only movable
5532 from two successors, so raising this threshhold is likely
5533 to nullify any benefit we get from code hoisting. */
5534 if (hoistable > 1)
5536 SET_BIT (hoist_exprs[bb], i);
5537 found = 1;
5542 /* If we found nothing to hoist, then quit now. */
5543 if (! found)
5544 continue;
5546 /* Loop over all the hoistable expressions. */
5547 for (i = 0; i < hoist_exprs[bb]->n_bits; i++)
5549 /* We want to insert the expression into BB only once, so
5550 note when we've inserted it. */
5551 insn_inserted_p = 0;
5553 /* These tests should be the same as the tests above. */
5554 if (TEST_BIT (hoist_vbeout[bb], i))
5556 /* We've found a potentially hoistable expression, now
5557 we look at every block BB dominates to see if it
5558 computes the expression. */
5559 for (dominated = 0; dominated < n_basic_blocks; dominated++)
5561 /* Ignore self dominance. */
5562 if (bb == dominated
5563 || ! TEST_BIT (dominators[dominated], bb))
5564 continue;
5566 /* We've found a dominated block, now see if it computes
5567 the busy expression and whether or not moving that
5568 expression to the "beginning" of that block is safe. */
5569 if (!TEST_BIT (antloc[dominated], i))
5570 continue;
5572 /* The expression is computed in the dominated block and
5573 it would be safe to compute it at the start of the
5574 dominated block. Now we have to determine if the
5575 expresion would reach the dominated block if it was
5576 placed at the end of BB. */
5577 if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
5579 struct expr *expr = index_map[i];
5580 struct occr *occr = expr->antic_occr;
5581 rtx insn;
5582 rtx set;
5585 /* Find the right occurence of this expression. */
5586 while (BLOCK_NUM (occr->insn) != dominated && occr)
5587 occr = occr->next;
5589 /* Should never happen. */
5590 if (!occr)
5591 abort ();
5593 insn = occr->insn;
5595 set = single_set (insn);
5596 if (! set)
5597 abort ();
5599 /* Create a pseudo-reg to store the result of reaching
5600 expressions into. Get the mode for the new pseudo
5601 from the mode of the original destination pseudo. */
5602 if (expr->reaching_reg == NULL)
5603 expr->reaching_reg
5604 = gen_reg_rtx (GET_MODE (SET_DEST (set)));
5606 /* In theory this should never fail since we're creating
5607 a reg->reg copy.
5609 However, on the x86 some of the movXX patterns actually
5610 contain clobbers of scratch regs. This may cause the
5611 insn created by validate_change to not match any
5612 pattern and thus cause validate_change to fail. */
5613 if (validate_change (insn, &SET_SRC (set),
5614 expr->reaching_reg, 0))
5616 occr->deleted_p = 1;
5617 if (!insn_inserted_p)
5619 insert_insn_end_bb (index_map[i], bb, 0);
5620 insn_inserted_p = 1;
5628 free (index_map);
5631 /* Top level routine to perform one code hoisting (aka unification) pass
5633 Return non-zero if a change was made. */
5635 static int
5636 one_code_hoisting_pass ()
5638 int changed = 0;
5640 alloc_expr_hash_table (max_cuid);
5641 compute_expr_hash_table ();
5642 if (gcse_file)
5643 dump_hash_table (gcse_file, "Code Hosting Expressions", expr_hash_table,
5644 expr_hash_table_size, n_exprs);
5645 if (n_exprs > 0)
5647 alloc_code_hoist_mem (n_basic_blocks, n_exprs);
5648 compute_code_hoist_data ();
5649 hoist_code ();
5650 free_code_hoist_mem ();
5652 free_expr_hash_table ();
5654 return changed;