1 /* Global common subexpression elimination/Partial redundancy elimination
2 and global constant/copy propagation for GNU compiler.
3 Copyright (C) 1997, 1998, 1999 Free Software Foundation, Inc.
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)
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. */
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
43 Global Optimization by Suppression of Partial Redundancies
45 communications of the acm, Vol. 22, Num. 2, Feb. 1979
47 A Portable Machine-Independent Global Optimizer - Design and Measurements
49 Stanford Ph.D. thesis, Dec. 1983
51 A Fast Algorithm for Code Movement Optimization
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
63 ACM TOPLAS, Vol. 13, Num. 2. Apr. 1991
65 Efficiently Computing Static Single Assignment Form and the Control
67 R. Cytron, J. Ferrante, B.K. Rosen, M.N. Wegman, and F.K. Zadeck
68 ACM TOPLAS, Vol. 13, Num. 4, Oct. 1991
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
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
111 ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
113 Value Driven Redundancy Elimination
115 Rice University Ph.D. thesis, Apr. 1996
119 Massively Scalar Compiler Project, Rice University, Sep. 1996
121 High Performance Compilers for Parallel Computing
125 Advanced Compiler Design and Implementation
127 Morgan Kaufmann, 1997
129 People wishing to speed up the code here should read:
130 Elimination Algorithms for Data Flow Analysis
131 B.G. Ryder, M.C. Paull
132 ACM Computing Surveys, Vol. 18, Num. 3, Sep. 1986
134 How to Analyze Large Programs Efficiently and Informatively
135 D.M. Dhamdhere, B.K. Rosen, F.K. Zadeck
136 ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
138 People wishing to do something different can find various possibilities
139 in the above papers and elsewhere.
148 #include "hard-reg-set.h"
151 #include "insn-config.h"
153 #include "basic-block.h"
155 #include "function.h"
159 #define obstack_chunk_alloc gmalloc
160 #define obstack_chunk_free free
162 /* Maximum number of passes to perform. */
165 /* Propagate flow information through back edges and thus enable PRE's
166 moving loop invariant calculations out of loops.
168 Originally this tended to create worse overall code, but several
169 improvements during the development of PRE seem to have made following
170 back edges generally a win.
172 Note much of the loop invariant code motion done here would normally
173 be done by loop.c, which has more heuristics for when to move invariants
174 out of loops. At some point we might need to move some of those
175 heuristics into gcse.c. */
176 #define FOLLOW_BACK_EDGES 1
178 /* We support GCSE via Partial Redundancy Elimination. PRE optimizations
179 are a superset of those done by GCSE.
181 We perform the following steps:
183 1) Compute basic block information.
185 2) Compute table of places where registers are set.
187 3) Perform copy/constant propagation.
189 4) Perform global cse.
191 5) Perform another pass of copy/constant propagation.
193 Two passes of copy/constant propagation are done because the first one
194 enables more GCSE and the second one helps to clean up the copies that
195 GCSE creates. This is needed more for PRE than for Classic because Classic
196 GCSE will try to use an existing register containing the common
197 subexpression rather than create a new one. This is harder to do for PRE
198 because of the code motion (which Classic GCSE doesn't do).
200 Expressions we are interested in GCSE-ing are of the form
201 (set (pseudo-reg) (expression)).
202 Function want_to_gcse_p says what these are.
204 PRE handles moving invariant expressions out of loops (by treating them as
205 partially redundant).
207 Eventually it would be nice to replace cse.c/gcse.c with SSA (static single
208 assignment) based GVN (global value numbering). L. T. Simpson's paper
209 (Rice University) on value numbering is a useful reference for this.
211 **********************
213 We used to support multiple passes but there are diminishing returns in
214 doing so. The first pass usually makes 90% of the changes that are doable.
215 A second pass can make a few more changes made possible by the first pass.
216 Experiments show any further passes don't make enough changes to justify
219 A study of spec92 using an unlimited number of passes:
220 [1 pass] = 1208 substitutions, [2] = 577, [3] = 202, [4] = 192, [5] = 83,
221 [6] = 34, [7] = 17, [8] = 9, [9] = 4, [10] = 4, [11] = 2,
222 [12] = 2, [13] = 1, [15] = 1, [16] = 2, [41] = 1
224 It was found doing copy propagation between each pass enables further
227 PRE is quite expensive in complicated functions because the DFA can take
228 awhile to converge. Hence we only perform one pass. Macro MAX_PASSES can
229 be modified if one wants to experiment.
231 **********************
233 The steps for PRE are:
235 1) Build the hash table of expressions we wish to GCSE (expr_hash_table).
237 2) Perform the data flow analysis for PRE.
239 3) Delete the redundant instructions
241 4) Insert the required copies [if any] that make the partially
242 redundant instructions fully redundant.
244 5) For other reaching expressions, insert an instruction to copy the value
245 to a newly created pseudo that will reach the redundant instruction.
247 The deletion is done first so that when we do insertions we
248 know which pseudo reg to use.
250 Various papers have argued that PRE DFA is expensive (O(n^2)) and others
251 argue it is not. The number of iterations for the algorithm to converge
252 is typically 2-4 so I don't view it as that expensive (relatively speaking).
254 PRE GCSE depends heavily on the second CSE pass to clean up the copies
255 we create. To make an expression reach the place where it's redundant,
256 the result of the expression is copied to a new register, and the redundant
257 expression is deleted by replacing it with this new register. Classic GCSE
258 doesn't have this problem as much as it computes the reaching defs of
259 each register in each block and thus can try to use an existing register.
261 **********************
263 A fair bit of simplicity is created by creating small functions for simple
264 tasks, even when the function is only called in one place. This may
265 measurably slow things down [or may not] by creating more function call
266 overhead than is necessary. The source is laid out so that it's trivial
267 to make the affected functions inline so that one can measure what speed
268 up, if any, can be achieved, and maybe later when things settle things can
271 Help stamp out big monolithic functions! */
273 /* GCSE global vars. */
276 static FILE *gcse_file;
278 /* Note whether or not we should run jump optimization after gcse. We
279 want to do this for two cases.
281 * If we changed any jumps via cprop.
283 * If we added any labels via edge splitting. */
285 static int run_jump_opt_after_gcse;
287 /* Element I is a list of I's predecessors/successors. */
288 static int_list_ptr *s_preds;
289 static int_list_ptr *s_succs;
291 /* Element I is the number of predecessors/successors of basic block I. */
292 static int *num_preds;
293 static int *num_succs;
295 /* Bitmaps are normally not included in debugging dumps.
296 However it's useful to be able to print them from GDB.
297 We could create special functions for this, but it's simpler to
298 just allow passing stderr to the dump_foo fns. Since stderr can
299 be a macro, we store a copy here. */
300 static FILE *debug_stderr;
302 /* An obstack for our working variables. */
303 static struct obstack gcse_obstack;
305 /* Non-zero for each mode that supports (set (reg) (reg)).
306 This is trivially true for integer and floating point values.
307 It may or may not be true for condition codes. */
308 static char can_copy_p[(int) NUM_MACHINE_MODES];
310 /* Non-zero if can_copy_p has been initialized. */
311 static int can_copy_init_p;
317 /* Hash table of expressions. */
321 /* The expression (SET_SRC for expressions, PATTERN for assignments). */
323 /* Index in the available expression bitmaps. */
325 /* Next entry with the same hash. */
326 struct expr *next_same_hash;
327 /* List of anticipatable occurrences in basic blocks in the function.
328 An "anticipatable occurrence" is one that is the first occurrence in the
329 basic block, the operands are not modified in the basic block prior
330 to the occurrence and the output is not used between the start of
331 the block and the occurrence. */
332 struct occr *antic_occr;
333 /* List of available occurrence in basic blocks in the function.
334 An "available occurrence" is one that is the last occurrence in the
335 basic block and the operands are not modified by following statements in
336 the basic block [including this insn]. */
337 struct occr *avail_occr;
338 /* Non-null if the computation is PRE redundant.
339 The value is the newly created pseudo-reg to record a copy of the
340 expression in all the places that reach the redundant copy. */
344 /* Occurrence of an expression.
345 There is one per basic block. If a pattern appears more than once the
346 last appearance is used [or first for anticipatable expressions]. */
350 /* Next occurrence of this expression. */
352 /* The insn that computes the expression. */
354 /* Non-zero if this [anticipatable] occurrence has been deleted. */
356 /* Non-zero if this [available] occurrence has been copied to
358 /* ??? This is mutually exclusive with deleted_p, so they could share
363 /* Expression and copy propagation hash tables.
364 Each hash table is an array of buckets.
365 ??? It is known that if it were an array of entries, structure elements
366 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
367 not clear whether in the final analysis a sufficient amount of memory would
368 be saved as the size of the available expression bitmaps would be larger
369 [one could build a mapping table without holes afterwards though].
370 Someday I'll perform the computation and figure it out.
373 /* Total size of the expression hash table, in elements. */
374 static int expr_hash_table_size;
376 This is an array of `expr_hash_table_size' elements. */
377 static struct expr **expr_hash_table;
379 /* Total size of the copy propagation hash table, in elements. */
380 static int set_hash_table_size;
382 This is an array of `set_hash_table_size' elements. */
383 static struct expr **set_hash_table;
385 /* Mapping of uids to cuids.
386 Only real insns get cuids. */
387 static int *uid_cuid;
389 /* Highest UID in UID_CUID. */
392 /* Get the cuid of an insn. */
393 #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
395 /* Number of cuids. */
398 /* Mapping of cuids to insns. */
399 static rtx *cuid_insn;
401 /* Get insn from cuid. */
402 #define CUID_INSN(CUID) (cuid_insn[CUID])
404 /* Maximum register number in function prior to doing gcse + 1.
405 Registers created during this pass have regno >= max_gcse_regno.
406 This is named with "gcse" to not collide with global of same name. */
407 static int max_gcse_regno;
409 /* Maximum number of cse-able expressions found. */
411 /* Maximum number of assignments for copy propagation found. */
414 /* Table of registers that are modified.
415 For each register, each element is a list of places where the pseudo-reg
418 For simplicity, GCSE is done on sets of pseudo-regs only. PRE GCSE only
419 requires knowledge of which blocks kill which regs [and thus could use
420 a bitmap instead of the lists `reg_set_table' uses].
422 `reg_set_table' and could be turned into an array of bitmaps
424 [however perhaps it may be useful to keep the data as is].
425 One advantage of recording things this way is that `reg_set_table' is
426 fairly sparse with respect to pseudo regs but for hard regs could be
427 fairly dense [relatively speaking].
428 And recording sets of pseudo-regs in lists speeds
429 up functions like compute_transp since in the case of pseudo-regs we only
430 need to iterate over the number of times a pseudo-reg is set, not over the
431 number of basic blocks [clearly there is a bit of a slow down in the cases
432 where a pseudo is set more than once in a block, however it is believed
433 that the net effect is to speed things up]. This isn't done for hard-regs
434 because recording call-clobbered hard-regs in `reg_set_table' at each
435 function call can consume a fair bit of memory, and iterating over hard-regs
436 stored this way in compute_transp will be more expensive. */
438 typedef struct reg_set {
439 /* The next setting of this register. */
440 struct reg_set *next;
441 /* The insn where it was set. */
444 static reg_set **reg_set_table;
445 /* Size of `reg_set_table'.
446 The table starts out at max_gcse_regno + slop, and is enlarged as
448 static int reg_set_table_size;
449 /* Amount to grow `reg_set_table' by when it's full. */
450 #define REG_SET_TABLE_SLOP 100
452 /* Bitmap containing one bit for each register in the program.
453 Used when performing GCSE to track which registers have been set since
454 the start of the basic block. */
455 static sbitmap reg_set_bitmap;
457 /* For each block, a bitmap of registers set in the block.
458 This is used by expr_killed_p and compute_transp.
459 It is computed during hash table computation and not by compute_sets
460 as it includes registers added since the last pass (or between cprop and
461 gcse) and it's currently not easy to realloc sbitmap vectors. */
462 static sbitmap *reg_set_in_block;
464 /* For each block, non-zero if memory is set in that block.
465 This is computed during hash table computation and is used by
466 expr_killed_p and compute_transp.
467 ??? Handling of memory is very simple, we don't make any attempt
468 to optimize things (later).
469 ??? This can be computed by compute_sets since the information
471 static char *mem_set_in_block;
473 /* Various variables for statistics gathering. */
475 /* Memory used in a pass.
476 This isn't intended to be absolutely precise. Its intent is only
477 to keep an eye on memory usage. */
478 static int bytes_used;
479 /* GCSE substitutions made. */
480 static int gcse_subst_count;
481 /* Number of copy instructions created. */
482 static int gcse_create_count;
483 /* Number of constants propagated. */
484 static int const_prop_count;
485 /* Number of copys propagated. */
486 static int copy_prop_count;
488 /* These variables are used by classic GCSE.
489 Normally they'd be defined a bit later, but `rd_gen' needs to
490 be declared sooner. */
492 /* A bitmap of all ones for implementing the algorithm for available
493 expressions and reaching definitions. */
494 /* ??? Available expression bitmaps have a different size than reaching
495 definition bitmaps. This should be the larger of the two, however, it
496 is not currently used for reaching definitions. */
497 static sbitmap u_bitmap;
499 /* Each block has a bitmap of each type.
500 The length of each blocks bitmap is:
502 max_cuid - for reaching definitions
503 n_exprs - for available expressions
505 Thus we view the bitmaps as 2 dimensional arrays. i.e.
506 rd_kill[block_num][cuid_num]
507 ae_kill[block_num][expr_num]
510 /* For reaching defs */
511 static sbitmap *rd_kill, *rd_gen, *reaching_defs, *rd_out;
513 /* for available exprs */
514 static sbitmap *ae_kill, *ae_gen, *ae_in, *ae_out;
517 static void compute_can_copy PROTO ((void));
519 static char *gmalloc PROTO ((unsigned int));
520 static char *grealloc PROTO ((char *, unsigned int));
521 static char *gcse_alloc PROTO ((unsigned long));
522 static void alloc_gcse_mem PROTO ((rtx));
523 static void free_gcse_mem PROTO ((void));
524 static void alloc_reg_set_mem PROTO ((int));
525 static void free_reg_set_mem PROTO ((void));
526 static void record_one_set PROTO ((int, rtx));
527 static void record_set_info PROTO ((rtx, rtx));
528 static void compute_sets PROTO ((rtx));
530 static void hash_scan_insn PROTO ((rtx, int, int));
531 static void hash_scan_set PROTO ((rtx, rtx, int));
532 static void hash_scan_clobber PROTO ((rtx, rtx));
533 static void hash_scan_call PROTO ((rtx, rtx));
534 static int want_to_gcse_p PROTO ((rtx));
535 static int oprs_unchanged_p PROTO ((rtx, rtx, int));
536 static int oprs_anticipatable_p PROTO ((rtx, rtx));
537 static int oprs_available_p PROTO ((rtx, rtx));
538 static void insert_expr_in_table PROTO ((rtx, enum machine_mode,
540 static void insert_set_in_table PROTO ((rtx, rtx));
541 static unsigned int hash_expr PROTO ((rtx, enum machine_mode,
543 static unsigned int hash_expr_1 PROTO ((rtx, enum machine_mode, int *));
544 static unsigned int hash_set PROTO ((int, int));
545 static int expr_equiv_p PROTO ((rtx, rtx));
546 static void record_last_reg_set_info PROTO ((rtx, int));
547 static void record_last_mem_set_info PROTO ((rtx));
548 static void record_last_set_info PROTO ((rtx, rtx));
549 static void compute_hash_table PROTO ((int));
550 static void alloc_set_hash_table PROTO ((int));
551 static void free_set_hash_table PROTO ((void));
552 static void compute_set_hash_table PROTO ((void));
553 static void alloc_expr_hash_table PROTO ((int));
554 static void free_expr_hash_table PROTO ((void));
555 static void compute_expr_hash_table PROTO ((void));
556 static void dump_hash_table PROTO ((FILE *, const char *, struct expr **,
558 static struct expr *lookup_expr PROTO ((rtx));
559 static struct expr *lookup_set PROTO ((int, rtx));
560 static struct expr *next_set PROTO ((int, struct expr *));
561 static void reset_opr_set_tables PROTO ((void));
562 static int oprs_not_set_p PROTO ((rtx, rtx));
563 static void mark_call PROTO ((rtx));
564 static void mark_set PROTO ((rtx, rtx));
565 static void mark_clobber PROTO ((rtx, rtx));
566 static void mark_oprs_set PROTO ((rtx));
568 static void alloc_cprop_mem PROTO ((int, int));
569 static void free_cprop_mem PROTO ((void));
570 static void compute_transp PROTO ((rtx, int, sbitmap *, int));
571 static void compute_transpout PROTO ((void));
572 static void compute_local_properties PROTO ((sbitmap *, sbitmap *,
574 static void compute_cprop_avinout PROTO ((void));
575 static void compute_cprop_data PROTO ((void));
576 static void find_used_regs PROTO ((rtx));
577 static int try_replace_reg PROTO ((rtx, rtx, rtx));
578 static struct expr *find_avail_set PROTO ((int, rtx));
579 static int cprop_jump PROTO((rtx, rtx, struct reg_use *, rtx));
580 static int cprop_cc0_jump PROTO((rtx, struct reg_use *, rtx));
581 static int cprop_insn PROTO ((rtx, int));
582 static int cprop PROTO ((int));
583 static int one_cprop_pass PROTO ((int, int));
585 static void alloc_pre_mem PROTO ((int, int));
586 static void free_pre_mem PROTO ((void));
587 static void compute_pre_data PROTO ((void));
588 static int pre_expr_reaches_here_p PROTO ((int, struct expr *,
590 static void insert_insn_end_bb PROTO ((struct expr *, int, int));
591 static void pre_insert PROTO ((struct expr **));
592 static void pre_insert_copy_insn PROTO ((struct expr *, rtx));
593 static void pre_insert_copies PROTO ((void));
594 static int pre_delete PROTO ((void));
595 static int pre_gcse PROTO ((void));
596 static int one_pre_gcse_pass PROTO ((int));
598 static void add_label_notes PROTO ((rtx, rtx));
600 static void alloc_rd_mem PROTO ((int, int));
601 static void free_rd_mem PROTO ((void));
602 static void handle_rd_kill_set PROTO ((rtx, int, int));
603 static void compute_kill_rd PROTO ((void));
604 static void compute_rd PROTO ((void));
605 static void alloc_avail_expr_mem PROTO ((int, int));
606 static void free_avail_expr_mem PROTO ((void));
607 static void compute_ae_gen PROTO ((void));
608 static int expr_killed_p PROTO ((rtx, int));
609 static void compute_ae_kill PROTO ((void));
610 static void compute_available PROTO ((void));
611 static int expr_reaches_here_p PROTO ((struct occr *, struct expr *,
613 static rtx computing_insn PROTO ((struct expr *, rtx));
614 static int def_reaches_here_p PROTO ((rtx, rtx));
615 static int can_disregard_other_sets PROTO ((struct reg_set **, rtx, int));
616 static int handle_avail_expr PROTO ((rtx, struct expr *));
617 static int classic_gcse PROTO ((void));
618 static int one_classic_gcse_pass PROTO ((int));
621 /* Entry point for global common subexpression elimination.
622 F is the first instruction in the function. */
630 /* Bytes used at start of pass. */
631 int initial_bytes_used;
632 /* Maximum number of bytes used by a pass. */
634 /* Point to release obstack data from for each pass. */
635 char *gcse_obstack_bottom;
637 /* We do not construct an accurate cfg in functions which call
638 setjmp, so just punt to be safe. */
639 if (current_function_calls_setjmp)
642 /* Assume that we do not need to run jump optimizations after gcse. */
643 run_jump_opt_after_gcse = 0;
645 /* For calling dump_foo fns from gdb. */
646 debug_stderr = stderr;
649 /* Identify the basic block information for this function, including
650 successors and predecessors. */
651 max_gcse_regno = max_reg_num ();
652 find_basic_blocks (f, max_gcse_regno, file, 1);
654 /* Return if there's nothing to do. */
655 if (n_basic_blocks <= 1)
657 /* Free storage allocated by find_basic_blocks. */
658 free_basic_block_vars (0);
662 /* See what modes support reg/reg copy operations. */
663 if (! can_copy_init_p)
669 gcc_obstack_init (&gcse_obstack);
671 /* Allocate and compute predecessors/successors. */
673 s_preds = (int_list_ptr *) alloca (n_basic_blocks * sizeof (int_list_ptr));
674 s_succs = (int_list_ptr *) alloca (n_basic_blocks * sizeof (int_list_ptr));
675 num_preds = (int *) alloca (n_basic_blocks * sizeof (int));
676 num_succs = (int *) alloca (n_basic_blocks * sizeof (int));
677 bytes_used = 4 * n_basic_blocks * sizeof (int_list_ptr);
678 compute_preds_succs (s_preds, s_succs, num_preds, num_succs);
681 dump_bb_data (file, s_preds, s_succs, 0);
683 /* Record where pseudo-registers are set.
684 This data is kept accurate during each pass.
685 ??? We could also record hard-reg information here
686 [since it's unchanging], however it is currently done during
687 hash table computation.
689 It may be tempting to compute MEM set information here too, but MEM
690 sets will be subject to code motion one day and thus we need to compute
691 information about memory sets when we build the hash tables. */
693 alloc_reg_set_mem (max_gcse_regno);
697 initial_bytes_used = bytes_used;
699 gcse_obstack_bottom = gcse_alloc (1);
701 while (changed && pass < MAX_PASSES)
705 fprintf (file, "GCSE pass %d\n\n", pass + 1);
707 /* Initialize bytes_used to the space for the pred/succ lists,
708 and the reg_set_table data. */
709 bytes_used = initial_bytes_used;
711 /* Each pass may create new registers, so recalculate each time. */
712 max_gcse_regno = max_reg_num ();
716 /* Don't allow constant propagation to modify jumps
718 changed = one_cprop_pass (pass + 1, 0);
721 changed |= one_classic_gcse_pass (pass + 1);
723 changed |= one_pre_gcse_pass (pass + 1);
725 if (max_pass_bytes < bytes_used)
726 max_pass_bytes = bytes_used;
732 fprintf (file, "\n");
735 obstack_free (&gcse_obstack, gcse_obstack_bottom);
739 /* Do one last pass of copy propagation, including cprop into
740 conditional jumps. */
742 max_gcse_regno = max_reg_num ();
744 /* This time, go ahead and allow cprop to alter jumps. */
745 one_cprop_pass (pass + 1, 1);
750 fprintf (file, "GCSE of %s: %d basic blocks, ",
751 current_function_name, n_basic_blocks);
752 fprintf (file, "%d pass%s, %d bytes\n\n",
753 pass, pass > 1 ? "es" : "", max_pass_bytes);
756 /* Free our obstack. */
757 obstack_free (&gcse_obstack, NULL_PTR);
758 /* Free reg_set_table. */
760 /* Free storage used to record predecessor/successor data. */
762 /* Free storage allocated by find_basic_blocks. */
763 free_basic_block_vars (0);
764 return run_jump_opt_after_gcse;
767 /* Misc. utilities. */
769 /* Compute which modes support reg/reg copy operations. */
775 #ifndef AVOID_CCMODE_COPIES
778 char *free_point = (char *) oballoc (1);
780 bzero (can_copy_p, NUM_MACHINE_MODES);
783 for (i = 0; i < NUM_MACHINE_MODES; i++)
785 switch (GET_MODE_CLASS (i))
788 #ifdef AVOID_CCMODE_COPIES
791 reg = gen_rtx_REG ((enum machine_mode) i, LAST_VIRTUAL_REGISTER + 1);
792 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, reg));
793 if (recog (PATTERN (insn), insn, NULL_PTR) >= 0)
804 /* Free the objects we just allocated. */
808 /* Cover function to xmalloc to record bytes allocated. */
815 return xmalloc (size);
818 /* Cover function to xrealloc.
819 We don't record the additional size since we don't know it.
820 It won't affect memory usage stats much anyway. */
827 return xrealloc (ptr, size);
830 /* Cover function to obstack_alloc.
831 We don't need to record the bytes allocated here since
832 obstack_chunk_alloc is set to gmalloc. */
838 return (char *) obstack_alloc (&gcse_obstack, size);
841 /* Allocate memory for the cuid mapping array,
842 and reg/memory set tracking tables.
844 This is called at the start of each pass. */
853 /* Find the largest UID and create a mapping from UIDs to CUIDs.
854 CUIDs are like UIDs except they increase monotonically, have no gaps,
855 and only apply to real insns. */
857 max_uid = get_max_uid ();
858 n = (max_uid + 1) * sizeof (int);
859 uid_cuid = (int *) gmalloc (n);
860 bzero ((char *) uid_cuid, n);
861 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
863 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
864 INSN_CUID (insn) = i++;
866 INSN_CUID (insn) = i;
869 /* Create a table mapping cuids to insns. */
872 n = (max_cuid + 1) * sizeof (rtx);
873 cuid_insn = (rtx *) gmalloc (n);
874 bzero ((char *) cuid_insn, n);
875 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
877 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
879 CUID_INSN (i) = insn;
884 /* Allocate vars to track sets of regs. */
886 reg_set_bitmap = (sbitmap) sbitmap_alloc (max_gcse_regno);
888 /* Allocate vars to track sets of regs, memory per block. */
890 reg_set_in_block = (sbitmap *) sbitmap_vector_alloc (n_basic_blocks,
892 mem_set_in_block = (char *) gmalloc (n_basic_blocks);
895 /* Free memory allocated by alloc_gcse_mem. */
903 free (reg_set_bitmap);
905 free (reg_set_in_block);
906 free (mem_set_in_block);
910 /* Compute the local properties of each recorded expression.
911 Local properties are those that are defined by the block, irrespective
914 An expression is transparent in a block if its operands are not modified
917 An expression is computed (locally available) in a block if it is computed
918 at least once and expression would contain the same value if the
919 computation was moved to the end of the block.
921 An expression is locally anticipatable in a block if it is computed at
922 least once and expression would contain the same value if the computation
923 was moved to the beginning of the block.
925 We call this routine for cprop, pre and code hoisting. They all
926 compute basically the same information and thus can easily share
929 TRANSP, COMP, and ANTLOC are destination sbitmaps for recording
930 local properties. If NULL, then it is not necessary to compute
931 or record that particular property.
933 SETP controls which hash table to look at. If zero, this routine
934 looks at the expr hash table; if nonzero this routine looks at
935 the set hash table. Additionally, TRANSP is computed as ~TRANSP,
936 since this is really cprop's ABSALTERED. */
939 compute_local_properties (transp, comp, antloc, setp)
945 int i, hash_table_size;
946 struct expr **hash_table;
948 /* Initialize any bitmaps that were passed in. */
952 sbitmap_vector_zero (transp, n_basic_blocks);
954 sbitmap_vector_ones (transp, n_basic_blocks);
957 sbitmap_vector_zero (comp, n_basic_blocks);
959 sbitmap_vector_zero (antloc, n_basic_blocks);
961 /* We use the same code for cprop, pre and hoisting. For cprop
962 we care about the set hash table, for pre and hoisting we
963 care about the expr hash table. */
964 hash_table_size = setp ? set_hash_table_size : expr_hash_table_size;
965 hash_table = setp ? set_hash_table : expr_hash_table;
967 for (i = 0; i < hash_table_size; i++)
971 for (expr = hash_table[i]; expr != NULL; expr = expr->next_same_hash)
974 int indx = expr->bitmap_index;
976 /* The expression is transparent in this block if it is not killed.
977 We start by assuming all are transparent [none are killed], and
978 then reset the bits for those that are. */
981 compute_transp (expr->expr, indx, transp, setp);
983 /* The occurrences recorded in antic_occr are exactly those that
984 we want to set to non-zero in ANTLOC. */
988 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
990 int bb = BLOCK_NUM (occr->insn);
991 SET_BIT (antloc[bb], indx);
993 /* While we're scanning the table, this is a good place to
999 /* The occurrences recorded in avail_occr are exactly those that
1000 we want to set to non-zero in COMP. */
1004 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
1006 int bb = BLOCK_NUM (occr->insn);
1007 SET_BIT (comp[bb], indx);
1009 /* While we're scanning the table, this is a good place to
1015 /* While we're scanning the table, this is a good place to
1017 expr->reaching_reg = 0;
1023 /* Register set information.
1025 `reg_set_table' records where each register is set or otherwise
1028 static struct obstack reg_set_obstack;
1031 alloc_reg_set_mem (n_regs)
1036 reg_set_table_size = n_regs + REG_SET_TABLE_SLOP;
1037 n = reg_set_table_size * sizeof (struct reg_set *);
1038 reg_set_table = (struct reg_set **) gmalloc (n);
1039 bzero ((char *) reg_set_table, n);
1041 gcc_obstack_init (®_set_obstack);
1047 free (reg_set_table);
1048 obstack_free (®_set_obstack, NULL_PTR);
1051 /* Record REGNO in the reg_set table. */
1054 record_one_set (regno, insn)
1058 /* allocate a new reg_set element and link it onto the list */
1059 struct reg_set *new_reg_info, *reg_info_ptr1, *reg_info_ptr2;
1061 /* If the table isn't big enough, enlarge it. */
1062 if (regno >= reg_set_table_size)
1064 int new_size = regno + REG_SET_TABLE_SLOP;
1065 reg_set_table = (struct reg_set **)
1066 grealloc ((char *) reg_set_table,
1067 new_size * sizeof (struct reg_set *));
1068 bzero ((char *) (reg_set_table + reg_set_table_size),
1069 (new_size - reg_set_table_size) * sizeof (struct reg_set *));
1070 reg_set_table_size = new_size;
1073 new_reg_info = (struct reg_set *) obstack_alloc (®_set_obstack,
1074 sizeof (struct reg_set));
1075 bytes_used += sizeof (struct reg_set);
1076 new_reg_info->insn = insn;
1077 new_reg_info->next = NULL;
1078 if (reg_set_table[regno] == NULL)
1079 reg_set_table[regno] = new_reg_info;
1082 reg_info_ptr1 = reg_info_ptr2 = reg_set_table[regno];
1083 /* ??? One could keep a "last" pointer to speed this up. */
1084 while (reg_info_ptr1 != NULL)
1086 reg_info_ptr2 = reg_info_ptr1;
1087 reg_info_ptr1 = reg_info_ptr1->next;
1089 reg_info_ptr2->next = new_reg_info;
1093 /* For communication between next two functions (via note_stores). */
1094 static rtx record_set_insn;
1096 /* Called from compute_sets via note_stores to handle one
1097 SET or CLOBBER in an insn. */
1100 record_set_info (dest, setter)
1101 rtx dest, setter ATTRIBUTE_UNUSED;
1103 if (GET_CODE (dest) == SUBREG)
1104 dest = SUBREG_REG (dest);
1106 if (GET_CODE (dest) == REG)
1108 if (REGNO (dest) >= FIRST_PSEUDO_REGISTER)
1109 record_one_set (REGNO (dest), record_set_insn);
1113 /* Scan the function and record each set of each pseudo-register.
1115 This is called once, at the start of the gcse pass.
1116 See the comments for `reg_set_table' for further docs. */
1126 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
1128 record_set_insn = insn;
1129 note_stores (PATTERN (insn), record_set_info);
1131 insn = NEXT_INSN (insn);
1135 /* Hash table support. */
1137 #define NEVER_SET -1
1139 /* For each register, the cuid of the first/last insn in the block to set it,
1140 or -1 if not set. */
1141 static int *reg_first_set;
1142 static int *reg_last_set;
1144 /* While computing "first/last set" info, this is the CUID of first/last insn
1145 to set memory or -1 if not set. `mem_last_set' is also used when
1146 performing GCSE to record whether memory has been set since the beginning
1148 Note that handling of memory is very simple, we don't make any attempt
1149 to optimize things (later). */
1150 static int mem_first_set;
1151 static int mem_last_set;
1153 /* Perform a quick check whether X, the source of a set, is something
1154 we want to consider for GCSE. */
1160 enum rtx_code code = GET_CODE (x);
1178 /* Return non-zero if the operands of expression X are unchanged from the
1179 start of INSN's basic block up to but not including INSN (if AVAIL_P == 0),
1180 or from INSN to the end of INSN's basic block (if AVAIL_P != 0). */
1183 oprs_unchanged_p (x, insn, avail_p)
1191 /* repeat is used to turn tail-recursion into iteration. */
1197 code = GET_CODE (x);
1202 return (reg_last_set[REGNO (x)] == NEVER_SET
1203 || reg_last_set[REGNO (x)] < INSN_CUID (insn));
1205 return (reg_first_set[REGNO (x)] == NEVER_SET
1206 || reg_first_set[REGNO (x)] >= INSN_CUID (insn));
1211 if (mem_last_set != NEVER_SET
1212 && mem_last_set >= INSN_CUID (insn))
1217 if (mem_first_set != NEVER_SET
1218 && mem_first_set < INSN_CUID (insn))
1245 i = GET_RTX_LENGTH (code) - 1;
1246 fmt = GET_RTX_FORMAT (code);
1251 rtx tem = XEXP (x, i);
1253 /* If we are about to do the last recursive call
1254 needed at this level, change it into iteration.
1255 This function is called enough to be worth it. */
1261 if (! oprs_unchanged_p (tem, insn, avail_p))
1264 else if (fmt[i] == 'E')
1267 for (j = 0; j < XVECLEN (x, i); j++)
1269 if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, avail_p))
1278 /* Return non-zero if the operands of expression X are unchanged from
1279 the start of INSN's basic block up to but not including INSN. */
1282 oprs_anticipatable_p (x, insn)
1285 return oprs_unchanged_p (x, insn, 0);
1288 /* Return non-zero if the operands of expression X are unchanged from
1289 INSN to the end of INSN's basic block. */
1292 oprs_available_p (x, insn)
1295 return oprs_unchanged_p (x, insn, 1);
1298 /* Hash expression X.
1299 MODE is only used if X is a CONST_INT.
1300 A boolean indicating if a volatile operand is found or if the expression
1301 contains something we don't want to insert in the table is stored in
1304 ??? One might want to merge this with canon_hash. Later. */
1307 hash_expr (x, mode, do_not_record_p, hash_table_size)
1309 enum machine_mode mode;
1310 int *do_not_record_p;
1311 int hash_table_size;
1315 *do_not_record_p = 0;
1317 hash = hash_expr_1 (x, mode, do_not_record_p);
1318 return hash % hash_table_size;
1321 /* Subroutine of hash_expr to do the actual work. */
1324 hash_expr_1 (x, mode, do_not_record_p)
1326 enum machine_mode mode;
1327 int *do_not_record_p;
1334 /* repeat is used to turn tail-recursion into iteration. */
1340 code = GET_CODE (x);
1345 register int regno = REGNO (x);
1346 hash += ((unsigned) REG << 7) + regno;
1352 unsigned HOST_WIDE_INT tem = INTVAL (x);
1353 hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + tem;
1358 /* This is like the general case, except that it only counts
1359 the integers representing the constant. */
1360 hash += (unsigned) code + (unsigned) GET_MODE (x);
1361 if (GET_MODE (x) != VOIDmode)
1362 for (i = 2; i < GET_RTX_LENGTH (CONST_DOUBLE); i++)
1364 unsigned tem = XINT (x, i);
1368 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1369 + (unsigned) CONST_DOUBLE_HIGH (x));
1372 /* Assume there is only one rtx object for any given label. */
1374 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1375 differences and differences between each stage's debugging dumps. */
1376 hash += ((unsigned) LABEL_REF << 7) + CODE_LABEL_NUMBER (XEXP (x, 0));
1381 /* Don't hash on the symbol's address to avoid bootstrap differences.
1382 Different hash values may cause expressions to be recorded in
1383 different orders and thus different registers to be used in the
1384 final assembler. This also avoids differences in the dump files
1385 between various stages. */
1387 unsigned char *p = (unsigned char *) XSTR (x, 0);
1389 h += (h << 7) + *p++; /* ??? revisit */
1390 hash += ((unsigned) SYMBOL_REF << 7) + h;
1395 if (MEM_VOLATILE_P (x))
1397 *do_not_record_p = 1;
1400 hash += (unsigned) MEM;
1411 case UNSPEC_VOLATILE:
1412 *do_not_record_p = 1;
1416 if (MEM_VOLATILE_P (x))
1418 *do_not_record_p = 1;
1426 i = GET_RTX_LENGTH (code) - 1;
1427 hash += (unsigned) code + (unsigned) GET_MODE (x);
1428 fmt = GET_RTX_FORMAT (code);
1433 rtx tem = XEXP (x, i);
1435 /* If we are about to do the last recursive call
1436 needed at this level, change it into iteration.
1437 This function is called enough to be worth it. */
1443 hash += hash_expr_1 (tem, 0, do_not_record_p);
1444 if (*do_not_record_p)
1447 else if (fmt[i] == 'E')
1448 for (j = 0; j < XVECLEN (x, i); j++)
1450 hash += hash_expr_1 (XVECEXP (x, i, j), 0, do_not_record_p);
1451 if (*do_not_record_p)
1454 else if (fmt[i] == 's')
1456 register unsigned char *p = (unsigned char *) XSTR (x, i);
1461 else if (fmt[i] == 'i')
1463 register unsigned tem = XINT (x, i);
1473 /* Hash a set of register REGNO.
1475 Sets are hashed on the register that is set.
1476 This simplifies the PRE copy propagation code.
1478 ??? May need to make things more elaborate. Later, as necessary. */
1481 hash_set (regno, hash_table_size)
1483 int hash_table_size;
1488 return hash % hash_table_size;
1491 /* Return non-zero if exp1 is equivalent to exp2.
1492 ??? Borrowed from cse.c. Might want to remerge with cse.c. Later. */
1499 register enum rtx_code code;
1500 register const char *fmt;
1504 if (x == 0 || y == 0)
1507 code = GET_CODE (x);
1508 if (code != GET_CODE (y))
1511 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
1512 if (GET_MODE (x) != GET_MODE (y))
1522 return INTVAL (x) == INTVAL (y);
1525 return XEXP (x, 0) == XEXP (y, 0);
1528 return XSTR (x, 0) == XSTR (y, 0);
1531 return REGNO (x) == REGNO (y);
1533 /* For commutative operations, check both orders. */
1541 return ((expr_equiv_p (XEXP (x, 0), XEXP (y, 0))
1542 && expr_equiv_p (XEXP (x, 1), XEXP (y, 1)))
1543 || (expr_equiv_p (XEXP (x, 0), XEXP (y, 1))
1544 && expr_equiv_p (XEXP (x, 1), XEXP (y, 0))));
1550 /* Compare the elements. If any pair of corresponding elements
1551 fail to match, return 0 for the whole thing. */
1553 fmt = GET_RTX_FORMAT (code);
1554 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1559 if (! expr_equiv_p (XEXP (x, i), XEXP (y, i)))
1564 if (XVECLEN (x, i) != XVECLEN (y, i))
1566 for (j = 0; j < XVECLEN (x, i); j++)
1567 if (! expr_equiv_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
1572 if (strcmp (XSTR (x, i), XSTR (y, i)))
1577 if (XINT (x, i) != XINT (y, i))
1582 if (XWINT (x, i) != XWINT (y, i))
1597 /* Insert expression X in INSN in the hash table.
1598 If it is already present, record it as the last occurrence in INSN's
1601 MODE is the mode of the value X is being stored into.
1602 It is only used if X is a CONST_INT.
1604 ANTIC_P is non-zero if X is an anticipatable expression.
1605 AVAIL_P is non-zero if X is an available expression. */
1608 insert_expr_in_table (x, mode, insn, antic_p, avail_p)
1610 enum machine_mode mode;
1612 int antic_p, avail_p;
1614 int found, do_not_record_p;
1616 struct expr *cur_expr, *last_expr = NULL;
1617 struct occr *antic_occr, *avail_occr;
1618 struct occr *last_occr = NULL;
1620 hash = hash_expr (x, mode, &do_not_record_p, expr_hash_table_size);
1622 /* Do not insert expression in table if it contains volatile operands,
1623 or if hash_expr determines the expression is something we don't want
1624 to or can't handle. */
1625 if (do_not_record_p)
1628 cur_expr = expr_hash_table[hash];
1631 while (cur_expr && ! (found = expr_equiv_p (cur_expr->expr, x)))
1633 /* If the expression isn't found, save a pointer to the end of
1635 last_expr = cur_expr;
1636 cur_expr = cur_expr->next_same_hash;
1641 cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
1642 bytes_used += sizeof (struct expr);
1643 if (expr_hash_table[hash] == NULL)
1645 /* This is the first pattern that hashed to this index. */
1646 expr_hash_table[hash] = cur_expr;
1650 /* Add EXPR to end of this hash chain. */
1651 last_expr->next_same_hash = cur_expr;
1653 /* Set the fields of the expr element. */
1655 cur_expr->bitmap_index = n_exprs++;
1656 cur_expr->next_same_hash = NULL;
1657 cur_expr->antic_occr = NULL;
1658 cur_expr->avail_occr = NULL;
1661 /* Now record the occurrence(s). */
1665 antic_occr = cur_expr->antic_occr;
1667 /* Search for another occurrence in the same basic block. */
1668 while (antic_occr && BLOCK_NUM (antic_occr->insn) != BLOCK_NUM (insn))
1670 /* If an occurrence isn't found, save a pointer to the end of
1672 last_occr = antic_occr;
1673 antic_occr = antic_occr->next;
1678 /* Found another instance of the expression in the same basic block.
1679 Prefer the currently recorded one. We want the first one in the
1680 block and the block is scanned from start to end. */
1681 ; /* nothing to do */
1685 /* First occurrence of this expression in this basic block. */
1686 antic_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
1687 bytes_used += sizeof (struct occr);
1688 /* First occurrence of this expression in any block? */
1689 if (cur_expr->antic_occr == NULL)
1690 cur_expr->antic_occr = antic_occr;
1692 last_occr->next = antic_occr;
1693 antic_occr->insn = insn;
1694 antic_occr->next = NULL;
1700 avail_occr = cur_expr->avail_occr;
1702 /* Search for another occurrence in the same basic block. */
1703 while (avail_occr && BLOCK_NUM (avail_occr->insn) != BLOCK_NUM (insn))
1705 /* If an occurrence isn't found, save a pointer to the end of
1707 last_occr = avail_occr;
1708 avail_occr = avail_occr->next;
1713 /* Found another instance of the expression in the same basic block.
1714 Prefer this occurrence to the currently recorded one. We want
1715 the last one in the block and the block is scanned from start
1717 avail_occr->insn = insn;
1721 /* First occurrence of this expression in this basic block. */
1722 avail_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
1723 bytes_used += sizeof (struct occr);
1724 /* First occurrence of this expression in any block? */
1725 if (cur_expr->avail_occr == NULL)
1726 cur_expr->avail_occr = avail_occr;
1728 last_occr->next = avail_occr;
1729 avail_occr->insn = insn;
1730 avail_occr->next = NULL;
1735 /* Insert pattern X in INSN in the hash table.
1736 X is a SET of a reg to either another reg or a constant.
1737 If it is already present, record it as the last occurrence in INSN's
1741 insert_set_in_table (x, insn)
1747 struct expr *cur_expr, *last_expr = NULL;
1748 struct occr *cur_occr, *last_occr = NULL;
1750 if (GET_CODE (x) != SET
1751 || GET_CODE (SET_DEST (x)) != REG)
1754 hash = hash_set (REGNO (SET_DEST (x)), set_hash_table_size);
1756 cur_expr = set_hash_table[hash];
1759 while (cur_expr && ! (found = expr_equiv_p (cur_expr->expr, x)))
1761 /* If the expression isn't found, save a pointer to the end of
1763 last_expr = cur_expr;
1764 cur_expr = cur_expr->next_same_hash;
1769 cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
1770 bytes_used += sizeof (struct expr);
1771 if (set_hash_table[hash] == NULL)
1773 /* This is the first pattern that hashed to this index. */
1774 set_hash_table[hash] = cur_expr;
1778 /* Add EXPR to end of this hash chain. */
1779 last_expr->next_same_hash = cur_expr;
1781 /* Set the fields of the expr element.
1782 We must copy X because it can be modified when copy propagation is
1783 performed on its operands. */
1784 /* ??? Should this go in a different obstack? */
1785 cur_expr->expr = copy_rtx (x);
1786 cur_expr->bitmap_index = n_sets++;
1787 cur_expr->next_same_hash = NULL;
1788 cur_expr->antic_occr = NULL;
1789 cur_expr->avail_occr = NULL;
1792 /* Now record the occurrence. */
1794 cur_occr = cur_expr->avail_occr;
1796 /* Search for another occurrence in the same basic block. */
1797 while (cur_occr && BLOCK_NUM (cur_occr->insn) != BLOCK_NUM (insn))
1799 /* If an occurrence isn't found, save a pointer to the end of
1801 last_occr = cur_occr;
1802 cur_occr = cur_occr->next;
1807 /* Found another instance of the expression in the same basic block.
1808 Prefer this occurrence to the currently recorded one. We want
1809 the last one in the block and the block is scanned from start
1811 cur_occr->insn = insn;
1815 /* First occurrence of this expression in this basic block. */
1816 cur_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
1817 bytes_used += sizeof (struct occr);
1818 /* First occurrence of this expression in any block? */
1819 if (cur_expr->avail_occr == NULL)
1820 cur_expr->avail_occr = cur_occr;
1822 last_occr->next = cur_occr;
1823 cur_occr->insn = insn;
1824 cur_occr->next = NULL;
1828 /* Scan pattern PAT of INSN and add an entry to the hash table.
1829 If SET_P is non-zero, this is for the assignment hash table,
1830 otherwise it is for the expression hash table. */
1833 hash_scan_set (pat, insn, set_p)
1837 rtx src = SET_SRC (pat);
1838 rtx dest = SET_DEST (pat);
1840 if (GET_CODE (src) == CALL)
1841 hash_scan_call (src, insn);
1843 if (GET_CODE (dest) == REG)
1845 int regno = REGNO (dest);
1848 /* Only record sets of pseudo-regs in the hash table. */
1850 && regno >= FIRST_PSEUDO_REGISTER
1851 /* Don't GCSE something if we can't do a reg/reg copy. */
1852 && can_copy_p [GET_MODE (dest)]
1853 /* Is SET_SRC something we want to gcse? */
1854 && want_to_gcse_p (src))
1856 /* An expression is not anticipatable if its operands are
1857 modified before this insn. */
1858 int antic_p = ! optimize_size && oprs_anticipatable_p (src, insn);
1859 /* An expression is not available if its operands are
1860 subsequently modified, including this insn. */
1861 int avail_p = oprs_available_p (src, insn);
1862 insert_expr_in_table (src, GET_MODE (dest), insn, antic_p, avail_p);
1864 /* Record sets for constant/copy propagation. */
1866 && regno >= FIRST_PSEUDO_REGISTER
1867 && ((GET_CODE (src) == REG
1868 && REGNO (src) >= FIRST_PSEUDO_REGISTER
1869 && can_copy_p [GET_MODE (dest)])
1870 || GET_CODE (src) == CONST_INT
1871 || GET_CODE (src) == SYMBOL_REF
1872 || GET_CODE (src) == CONST_DOUBLE)
1873 /* A copy is not available if its src or dest is subsequently
1874 modified. Here we want to search from INSN+1 on, but
1875 oprs_available_p searches from INSN on. */
1876 && (insn == BLOCK_END (BLOCK_NUM (insn))
1877 || ((tmp = next_nonnote_insn (insn)) != NULL_RTX
1878 && oprs_available_p (pat, tmp))))
1879 insert_set_in_table (pat, insn);
1884 hash_scan_clobber (x, insn)
1885 rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
1887 /* Currently nothing to do. */
1891 hash_scan_call (x, insn)
1892 rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
1894 /* Currently nothing to do. */
1897 /* Process INSN and add hash table entries as appropriate.
1899 Only available expressions that set a single pseudo-reg are recorded.
1901 Single sets in a PARALLEL could be handled, but it's an extra complication
1902 that isn't dealt with right now. The trick is handling the CLOBBERs that
1903 are also in the PARALLEL. Later.
1905 If SET_P is non-zero, this is for the assignment hash table,
1906 otherwise it is for the expression hash table.
1907 If IN_LIBCALL_BLOCK nonzero, we are in a libcall block, and should
1908 not record any expressions. */
1911 hash_scan_insn (insn, set_p, in_libcall_block)
1914 int in_libcall_block;
1916 rtx pat = PATTERN (insn);
1918 /* Pick out the sets of INSN and for other forms of instructions record
1919 what's been modified. */
1921 if (GET_CODE (pat) == SET && ! in_libcall_block)
1922 hash_scan_set (pat, insn, set_p);
1923 else if (GET_CODE (pat) == PARALLEL)
1927 for (i = 0; i < XVECLEN (pat, 0); i++)
1929 rtx x = XVECEXP (pat, 0, i);
1931 if (GET_CODE (x) == SET)
1933 if (GET_CODE (SET_SRC (x)) == CALL)
1934 hash_scan_call (SET_SRC (x), insn);
1936 else if (GET_CODE (x) == CLOBBER)
1937 hash_scan_clobber (x, insn);
1938 else if (GET_CODE (x) == CALL)
1939 hash_scan_call (x, insn);
1942 else if (GET_CODE (pat) == CLOBBER)
1943 hash_scan_clobber (pat, insn);
1944 else if (GET_CODE (pat) == CALL)
1945 hash_scan_call (pat, insn);
1949 dump_hash_table (file, name, table, table_size, total_size)
1952 struct expr **table;
1953 int table_size, total_size;
1956 /* Flattened out table, so it's printed in proper order. */
1957 struct expr **flat_table = (struct expr **) alloca (total_size * sizeof (struct expr *));
1958 unsigned int *hash_val = (unsigned int *) alloca (total_size * sizeof (unsigned int));
1960 bzero ((char *) flat_table, total_size * sizeof (struct expr *));
1961 for (i = 0; i < table_size; i++)
1965 for (expr = table[i]; expr != NULL; expr = expr->next_same_hash)
1967 flat_table[expr->bitmap_index] = expr;
1968 hash_val[expr->bitmap_index] = i;
1972 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
1973 name, table_size, total_size);
1975 for (i = 0; i < total_size; i++)
1977 struct expr *expr = flat_table[i];
1979 fprintf (file, "Index %d (hash value %d)\n ",
1980 expr->bitmap_index, hash_val[i]);
1981 print_rtl (file, expr->expr);
1982 fprintf (file, "\n");
1985 fprintf (file, "\n");
1988 /* Record register first/last/block set information for REGNO in INSN.
1989 reg_first_set records the first place in the block where the register
1990 is set and is used to compute "anticipatability".
1991 reg_last_set records the last place in the block where the register
1992 is set and is used to compute "availability".
1993 reg_set_in_block records whether the register is set in the block
1994 and is used to compute "transparency". */
1997 record_last_reg_set_info (insn, regno)
2001 if (reg_first_set[regno] == NEVER_SET)
2002 reg_first_set[regno] = INSN_CUID (insn);
2003 reg_last_set[regno] = INSN_CUID (insn);
2004 SET_BIT (reg_set_in_block[BLOCK_NUM (insn)], regno);
2007 /* Record memory first/last/block set information for INSN. */
2010 record_last_mem_set_info (insn)
2013 if (mem_first_set == NEVER_SET)
2014 mem_first_set = INSN_CUID (insn);
2015 mem_last_set = INSN_CUID (insn);
2016 mem_set_in_block[BLOCK_NUM (insn)] = 1;
2019 /* Used for communicating between next two routines. */
2020 static rtx last_set_insn;
2022 /* Called from compute_hash_table via note_stores to handle one
2023 SET or CLOBBER in an insn. */
2026 record_last_set_info (dest, setter)
2027 rtx dest, setter ATTRIBUTE_UNUSED;
2029 if (GET_CODE (dest) == SUBREG)
2030 dest = SUBREG_REG (dest);
2032 if (GET_CODE (dest) == REG)
2033 record_last_reg_set_info (last_set_insn, REGNO (dest));
2034 else if (GET_CODE (dest) == MEM
2035 /* Ignore pushes, they clobber nothing. */
2036 && ! push_operand (dest, GET_MODE (dest)))
2037 record_last_mem_set_info (last_set_insn);
2040 /* Top level function to create an expression or assignment hash table.
2042 Expression entries are placed in the hash table if
2043 - they are of the form (set (pseudo-reg) src),
2044 - src is something we want to perform GCSE on,
2045 - none of the operands are subsequently modified in the block
2047 Assignment entries are placed in the hash table if
2048 - they are of the form (set (pseudo-reg) src),
2049 - src is something we want to perform const/copy propagation on,
2050 - none of the operands or target are subsequently modified in the block
2051 Currently src must be a pseudo-reg or a const_int.
2053 F is the first insn.
2054 SET_P is non-zero for computing the assignment hash table. */
2057 compute_hash_table (set_p)
2062 /* While we compute the hash table we also compute a bit array of which
2063 registers are set in which blocks.
2064 We also compute which blocks set memory, in the absence of aliasing
2065 support [which is TODO].
2066 ??? This isn't needed during const/copy propagation, but it's cheap to
2068 sbitmap_vector_zero (reg_set_in_block, n_basic_blocks);
2069 bzero ((char *) mem_set_in_block, n_basic_blocks);
2071 /* Some working arrays used to track first and last set in each block. */
2072 /* ??? One could use alloca here, but at some size a threshold is crossed
2073 beyond which one should use malloc. Are we at that threshold here? */
2074 reg_first_set = (int *) gmalloc (max_gcse_regno * sizeof (int));
2075 reg_last_set = (int *) gmalloc (max_gcse_regno * sizeof (int));
2077 for (bb = 0; bb < n_basic_blocks; bb++)
2081 int in_libcall_block;
2084 /* First pass over the instructions records information used to
2085 determine when registers and memory are first and last set.
2086 ??? The mem_set_in_block and hard-reg reg_set_in_block computation
2087 could be moved to compute_sets since they currently don't change. */
2089 for (i = 0; i < max_gcse_regno; i++)
2090 reg_first_set[i] = reg_last_set[i] = NEVER_SET;
2091 mem_first_set = NEVER_SET;
2092 mem_last_set = NEVER_SET;
2094 for (insn = BLOCK_HEAD (bb);
2095 insn && insn != NEXT_INSN (BLOCK_END (bb));
2096 insn = NEXT_INSN (insn))
2098 #ifdef NON_SAVING_SETJMP
2099 if (NON_SAVING_SETJMP && GET_CODE (insn) == NOTE
2100 && NOTE_LINE_NUMBER (insn) == NOTE_INSN_SETJMP)
2102 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2103 record_last_reg_set_info (insn, regno);
2108 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
2111 if (GET_CODE (insn) == CALL_INSN)
2113 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2114 if ((call_used_regs[regno]
2115 && regno != STACK_POINTER_REGNUM
2116 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2117 && regno != HARD_FRAME_POINTER_REGNUM
2119 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2120 && ! (regno == ARG_POINTER_REGNUM && fixed_regs[regno])
2122 #if defined (PIC_OFFSET_TABLE_REGNUM) && !defined (PIC_OFFSET_TABLE_REG_CALL_CLOBBERED)
2123 && ! (regno == PIC_OFFSET_TABLE_REGNUM && flag_pic)
2126 && regno != FRAME_POINTER_REGNUM)
2127 || global_regs[regno])
2128 record_last_reg_set_info (insn, regno);
2129 if (! CONST_CALL_P (insn))
2130 record_last_mem_set_info (insn);
2133 last_set_insn = insn;
2134 note_stores (PATTERN (insn), record_last_set_info);
2137 /* The next pass builds the hash table. */
2139 for (insn = BLOCK_HEAD (bb), in_libcall_block = 0;
2140 insn && insn != NEXT_INSN (BLOCK_END (bb));
2141 insn = NEXT_INSN (insn))
2143 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
2145 if (find_reg_note (insn, REG_LIBCALL, NULL_RTX))
2146 in_libcall_block = 1;
2147 else if (find_reg_note (insn, REG_RETVAL, NULL_RTX))
2148 in_libcall_block = 0;
2149 hash_scan_insn (insn, set_p, in_libcall_block);
2154 free (reg_first_set);
2155 free (reg_last_set);
2156 /* Catch bugs early. */
2157 reg_first_set = reg_last_set = 0;
2160 /* Allocate space for the set hash table.
2161 N_INSNS is the number of instructions in the function.
2162 It is used to determine the number of buckets to use. */
2165 alloc_set_hash_table (n_insns)
2170 set_hash_table_size = n_insns / 4;
2171 if (set_hash_table_size < 11)
2172 set_hash_table_size = 11;
2173 /* Attempt to maintain efficient use of hash table.
2174 Making it an odd number is simplest for now.
2175 ??? Later take some measurements. */
2176 set_hash_table_size |= 1;
2177 n = set_hash_table_size * sizeof (struct expr *);
2178 set_hash_table = (struct expr **) gmalloc (n);
2181 /* Free things allocated by alloc_set_hash_table. */
2184 free_set_hash_table ()
2186 free (set_hash_table);
2189 /* Compute the hash table for doing copy/const propagation. */
2192 compute_set_hash_table ()
2194 /* Initialize count of number of entries in hash table. */
2196 bzero ((char *) set_hash_table, set_hash_table_size * sizeof (struct expr *));
2198 compute_hash_table (1);
2201 /* Allocate space for the expression hash table.
2202 N_INSNS is the number of instructions in the function.
2203 It is used to determine the number of buckets to use. */
2206 alloc_expr_hash_table (n_insns)
2211 expr_hash_table_size = n_insns / 2;
2212 /* Make sure the amount is usable. */
2213 if (expr_hash_table_size < 11)
2214 expr_hash_table_size = 11;
2215 /* Attempt to maintain efficient use of hash table.
2216 Making it an odd number is simplest for now.
2217 ??? Later take some measurements. */
2218 expr_hash_table_size |= 1;
2219 n = expr_hash_table_size * sizeof (struct expr *);
2220 expr_hash_table = (struct expr **) gmalloc (n);
2223 /* Free things allocated by alloc_expr_hash_table. */
2226 free_expr_hash_table ()
2228 free (expr_hash_table);
2231 /* Compute the hash table for doing GCSE. */
2234 compute_expr_hash_table ()
2236 /* Initialize count of number of entries in hash table. */
2238 bzero ((char *) expr_hash_table, expr_hash_table_size * sizeof (struct expr *));
2240 compute_hash_table (0);
2243 /* Expression tracking support. */
2245 /* Lookup pattern PAT in the expression table.
2246 The result is a pointer to the table entry, or NULL if not found. */
2248 static struct expr *
2252 int do_not_record_p;
2253 unsigned int hash = hash_expr (pat, GET_MODE (pat), &do_not_record_p,
2254 expr_hash_table_size);
2257 if (do_not_record_p)
2260 expr = expr_hash_table[hash];
2262 while (expr && ! expr_equiv_p (expr->expr, pat))
2263 expr = expr->next_same_hash;
2268 /* Lookup REGNO in the set table.
2269 If PAT is non-NULL look for the entry that matches it, otherwise return
2270 the first entry for REGNO.
2271 The result is a pointer to the table entry, or NULL if not found. */
2273 static struct expr *
2274 lookup_set (regno, pat)
2278 unsigned int hash = hash_set (regno, set_hash_table_size);
2281 expr = set_hash_table[hash];
2285 while (expr && ! expr_equiv_p (expr->expr, pat))
2286 expr = expr->next_same_hash;
2290 while (expr && REGNO (SET_DEST (expr->expr)) != regno)
2291 expr = expr->next_same_hash;
2297 /* Return the next entry for REGNO in list EXPR. */
2299 static struct expr *
2300 next_set (regno, expr)
2305 expr = expr->next_same_hash;
2306 while (expr && REGNO (SET_DEST (expr->expr)) != regno);
2310 /* Reset tables used to keep track of what's still available [since the
2311 start of the block]. */
2314 reset_opr_set_tables ()
2316 /* Maintain a bitmap of which regs have been set since beginning of
2318 sbitmap_zero (reg_set_bitmap);
2319 /* Also keep a record of the last instruction to modify memory.
2320 For now this is very trivial, we only record whether any memory
2321 location has been modified. */
2325 /* Return non-zero if the operands of X are not set before INSN in
2326 INSN's basic block. */
2329 oprs_not_set_p (x, insn)
2336 /* repeat is used to turn tail-recursion into iteration. */
2342 code = GET_CODE (x);
2357 if (mem_last_set != 0)
2363 return ! TEST_BIT (reg_set_bitmap, REGNO (x));
2369 fmt = GET_RTX_FORMAT (code);
2370 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2375 /* If we are about to do the last recursive call
2376 needed at this level, change it into iteration.
2377 This function is called enough to be worth it. */
2383 not_set_p = oprs_not_set_p (XEXP (x, i), insn);
2387 else if (fmt[i] == 'E')
2390 for (j = 0; j < XVECLEN (x, i); j++)
2392 int not_set_p = oprs_not_set_p (XVECEXP (x, i, j), insn);
2402 /* Mark things set by a CALL. */
2408 mem_last_set = INSN_CUID (insn);
2411 /* Mark things set by a SET. */
2414 mark_set (pat, insn)
2417 rtx dest = SET_DEST (pat);
2419 while (GET_CODE (dest) == SUBREG
2420 || GET_CODE (dest) == ZERO_EXTRACT
2421 || GET_CODE (dest) == SIGN_EXTRACT
2422 || GET_CODE (dest) == STRICT_LOW_PART)
2423 dest = XEXP (dest, 0);
2425 if (GET_CODE (dest) == REG)
2426 SET_BIT (reg_set_bitmap, REGNO (dest));
2427 else if (GET_CODE (dest) == MEM)
2428 mem_last_set = INSN_CUID (insn);
2430 if (GET_CODE (SET_SRC (pat)) == CALL)
2434 /* Record things set by a CLOBBER. */
2437 mark_clobber (pat, insn)
2440 rtx clob = XEXP (pat, 0);
2442 while (GET_CODE (clob) == SUBREG || GET_CODE (clob) == STRICT_LOW_PART)
2443 clob = XEXP (clob, 0);
2445 if (GET_CODE (clob) == REG)
2446 SET_BIT (reg_set_bitmap, REGNO (clob));
2448 mem_last_set = INSN_CUID (insn);
2451 /* Record things set by INSN.
2452 This data is used by oprs_not_set_p. */
2455 mark_oprs_set (insn)
2458 rtx pat = PATTERN (insn);
2460 if (GET_CODE (pat) == SET)
2461 mark_set (pat, insn);
2462 else if (GET_CODE (pat) == PARALLEL)
2466 for (i = 0; i < XVECLEN (pat, 0); i++)
2468 rtx x = XVECEXP (pat, 0, i);
2470 if (GET_CODE (x) == SET)
2472 else if (GET_CODE (x) == CLOBBER)
2473 mark_clobber (x, insn);
2474 else if (GET_CODE (x) == CALL)
2478 else if (GET_CODE (pat) == CLOBBER)
2479 mark_clobber (pat, insn);
2480 else if (GET_CODE (pat) == CALL)
2485 /* Classic GCSE reaching definition support. */
2487 /* Allocate reaching def variables. */
2490 alloc_rd_mem (n_blocks, n_insns)
2491 int n_blocks, n_insns;
2493 rd_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2494 sbitmap_vector_zero (rd_kill, n_basic_blocks);
2496 rd_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2497 sbitmap_vector_zero (rd_gen, n_basic_blocks);
2499 reaching_defs = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2500 sbitmap_vector_zero (reaching_defs, n_basic_blocks);
2502 rd_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2503 sbitmap_vector_zero (rd_out, n_basic_blocks);
2506 /* Free reaching def variables. */
2513 free (reaching_defs);
2517 /* Add INSN to the kills of BB.
2518 REGNO, set in BB, is killed by INSN. */
2521 handle_rd_kill_set (insn, regno, bb)
2525 struct reg_set *this_reg = reg_set_table[regno];
2529 if (BLOCK_NUM (this_reg->insn) != BLOCK_NUM (insn))
2530 SET_BIT (rd_kill[bb], INSN_CUID (this_reg->insn));
2531 this_reg = this_reg->next;
2535 /* Compute the set of kill's for reaching definitions. */
2543 For each set bit in `gen' of the block (i.e each insn which
2544 generates a definition in the block)
2545 Call the reg set by the insn corresponding to that bit regx
2546 Look at the linked list starting at reg_set_table[regx]
2547 For each setting of regx in the linked list, which is not in
2549 Set the bit in `kill' corresponding to that insn
2552 for (bb = 0; bb < n_basic_blocks; bb++)
2554 for (cuid = 0; cuid < max_cuid; cuid++)
2556 if (TEST_BIT (rd_gen[bb], cuid))
2558 rtx insn = CUID_INSN (cuid);
2559 rtx pat = PATTERN (insn);
2561 if (GET_CODE (insn) == CALL_INSN)
2565 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2567 if ((call_used_regs[regno]
2568 && regno != STACK_POINTER_REGNUM
2569 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2570 && regno != HARD_FRAME_POINTER_REGNUM
2572 #if ARG_POINTER_REGNUM != FRAME_POINTER_REGNUM
2573 && ! (regno == ARG_POINTER_REGNUM
2574 && fixed_regs[regno])
2576 #if defined (PIC_OFFSET_TABLE_REGNUM) && !defined (PIC_OFFSET_TABLE_REG_CALL_CLOBBERED)
2577 && ! (regno == PIC_OFFSET_TABLE_REGNUM && flag_pic)
2579 && regno != FRAME_POINTER_REGNUM)
2580 || global_regs[regno])
2581 handle_rd_kill_set (insn, regno, bb);
2585 if (GET_CODE (pat) == PARALLEL)
2589 /* We work backwards because ... */
2590 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
2592 enum rtx_code code = GET_CODE (XVECEXP (pat, 0, i));
2593 if ((code == SET || code == CLOBBER)
2594 && GET_CODE (XEXP (XVECEXP (pat, 0, i), 0)) == REG)
2595 handle_rd_kill_set (insn,
2596 REGNO (XEXP (XVECEXP (pat, 0, i), 0)),
2600 else if (GET_CODE (pat) == SET)
2602 if (GET_CODE (SET_DEST (pat)) == REG)
2604 /* Each setting of this register outside of this block
2605 must be marked in the set of kills in this block. */
2606 handle_rd_kill_set (insn, REGNO (SET_DEST (pat)), bb);
2609 /* FIXME: CLOBBER? */
2615 /* Compute the reaching definitions as in
2616 Compilers Principles, Techniques, and Tools. Aho, Sethi, Ullman,
2617 Chapter 10. It is the same algorithm as used for computing available
2618 expressions but applied to the gens and kills of reaching definitions. */
2623 int bb, changed, passes;
2625 for (bb = 0; bb < n_basic_blocks; bb++)
2626 sbitmap_copy (rd_out[bb] /*dst*/, rd_gen[bb] /*src*/);
2633 for (bb = 0; bb < n_basic_blocks; bb++)
2635 sbitmap_union_of_predecessors (reaching_defs[bb], rd_out,
2637 changed |= sbitmap_union_of_diff (rd_out[bb], rd_gen[bb],
2638 reaching_defs[bb], rd_kill[bb]);
2644 fprintf (gcse_file, "reaching def computation: %d passes\n", passes);
2647 /* Classic GCSE available expression support. */
2649 /* Allocate memory for available expression computation. */
2652 alloc_avail_expr_mem (n_blocks, n_exprs)
2653 int n_blocks, n_exprs;
2655 ae_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2656 sbitmap_vector_zero (ae_kill, n_basic_blocks);
2658 ae_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2659 sbitmap_vector_zero (ae_gen, n_basic_blocks);
2661 ae_in = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2662 sbitmap_vector_zero (ae_in, n_basic_blocks);
2664 ae_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
2665 sbitmap_vector_zero (ae_out, n_basic_blocks);
2667 u_bitmap = (sbitmap) sbitmap_alloc (n_exprs);
2668 sbitmap_ones (u_bitmap);
2672 free_avail_expr_mem ()
2681 /* Compute the set of available expressions generated in each basic block. */
2688 /* For each recorded occurrence of each expression, set ae_gen[bb][expr].
2689 This is all we have to do because an expression is not recorded if it
2690 is not available, and the only expressions we want to work with are the
2691 ones that are recorded. */
2693 for (i = 0; i < expr_hash_table_size; i++)
2695 struct expr *expr = expr_hash_table[i];
2696 while (expr != NULL)
2698 struct occr *occr = expr->avail_occr;
2699 while (occr != NULL)
2701 SET_BIT (ae_gen[BLOCK_NUM (occr->insn)], expr->bitmap_index);
2704 expr = expr->next_same_hash;
2709 /* Return non-zero if expression X is killed in BB. */
2712 expr_killed_p (x, bb)
2720 /* repeat is used to turn tail-recursion into iteration. */
2726 code = GET_CODE (x);
2730 return TEST_BIT (reg_set_in_block[bb], REGNO (x));
2733 if (mem_set_in_block[bb])
2753 i = GET_RTX_LENGTH (code) - 1;
2754 fmt = GET_RTX_FORMAT (code);
2759 rtx tem = XEXP (x, i);
2761 /* If we are about to do the last recursive call
2762 needed at this level, change it into iteration.
2763 This function is called enough to be worth it. */
2769 if (expr_killed_p (tem, bb))
2772 else if (fmt[i] == 'E')
2775 for (j = 0; j < XVECLEN (x, i); j++)
2777 if (expr_killed_p (XVECEXP (x, i, j), bb))
2786 /* Compute the set of available expressions killed in each basic block. */
2793 for (bb = 0; bb < n_basic_blocks; bb++)
2795 for (i = 0; i < expr_hash_table_size; i++)
2797 struct expr *expr = expr_hash_table[i];
2799 for ( ; expr != NULL; expr = expr->next_same_hash)
2801 /* Skip EXPR if generated in this block. */
2802 if (TEST_BIT (ae_gen[bb], expr->bitmap_index))
2805 if (expr_killed_p (expr->expr, bb))
2806 SET_BIT (ae_kill[bb], expr->bitmap_index);
2812 /* Compute available expressions.
2814 Implement the algorithm to find available expressions
2815 as given in the Aho Sethi Ullman book, pages 627-631. */
2818 compute_available ()
2820 int bb, changed, passes;
2822 sbitmap_zero (ae_in[0]);
2824 sbitmap_copy (ae_out[0] /*dst*/, ae_gen[0] /*src*/);
2826 for (bb = 1; bb < n_basic_blocks; bb++)
2827 sbitmap_difference (ae_out[bb], u_bitmap, ae_kill[bb]);
2834 for (bb = 1; bb < n_basic_blocks; bb++)
2836 sbitmap_intersect_of_predecessors (ae_in[bb], ae_out, bb, s_preds);
2837 changed |= sbitmap_union_of_diff (ae_out[bb], ae_gen[bb],
2838 ae_in[bb], ae_kill[bb]);
2844 fprintf (gcse_file, "avail expr computation: %d passes\n", passes);
2847 /* Actually perform the Classic GCSE optimizations. */
2849 /* Return non-zero if occurrence OCCR of expression EXPR reaches block BB.
2851 CHECK_SELF_LOOP is non-zero if we should consider a block reaching itself
2852 as a positive reach. We want to do this when there are two computations
2853 of the expression in the block.
2855 VISITED is a pointer to a working buffer for tracking which BB's have
2856 been visited. It is NULL for the top-level call.
2858 We treat reaching expressions that go through blocks containing the same
2859 reaching expression as "not reaching". E.g. if EXPR is generated in blocks
2860 2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
2861 2 as not reaching. The intent is to improve the probability of finding
2862 only one reaching expression and to reduce register lifetimes by picking
2863 the closest such expression. */
2866 expr_reaches_here_p (occr, expr, bb, check_self_loop, visited)
2870 int check_self_loop;
2875 if (visited == NULL)
2877 visited = (char *) alloca (n_basic_blocks);
2878 bzero (visited, n_basic_blocks);
2881 for (pred = s_preds[bb]; pred != NULL; pred = pred->next)
2883 int pred_bb = INT_LIST_VAL (pred);
2885 if (visited[pred_bb])
2887 /* This predecessor has already been visited.
2891 else if (pred_bb == bb)
2893 /* BB loops on itself. */
2895 && TEST_BIT (ae_gen[pred_bb], expr->bitmap_index)
2896 && BLOCK_NUM (occr->insn) == pred_bb)
2898 visited[pred_bb] = 1;
2900 /* Ignore this predecessor if it kills the expression. */
2901 else if (TEST_BIT (ae_kill[pred_bb], expr->bitmap_index))
2902 visited[pred_bb] = 1;
2903 /* Does this predecessor generate this expression? */
2904 else if (TEST_BIT (ae_gen[pred_bb], expr->bitmap_index))
2906 /* Is this the occurrence we're looking for?
2907 Note that there's only one generating occurrence per block
2908 so we just need to check the block number. */
2909 if (BLOCK_NUM (occr->insn) == pred_bb)
2911 visited[pred_bb] = 1;
2913 /* Neither gen nor kill. */
2916 visited[pred_bb] = 1;
2917 if (expr_reaches_here_p (occr, expr, pred_bb, check_self_loop, visited))
2922 /* All paths have been checked. */
2926 /* Return the instruction that computes EXPR that reaches INSN's basic block.
2927 If there is more than one such instruction, return NULL.
2929 Called only by handle_avail_expr. */
2932 computing_insn (expr, insn)
2936 int bb = BLOCK_NUM (insn);
2938 if (expr->avail_occr->next == NULL)
2940 if (BLOCK_NUM (expr->avail_occr->insn) == bb)
2942 /* The available expression is actually itself
2943 (i.e. a loop in the flow graph) so do nothing. */
2946 /* (FIXME) Case that we found a pattern that was created by
2947 a substitution that took place. */
2948 return expr->avail_occr->insn;
2952 /* Pattern is computed more than once.
2953 Search backwards from this insn to see how many of these
2954 computations actually reach this insn. */
2956 rtx insn_computes_expr = NULL;
2959 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
2961 if (BLOCK_NUM (occr->insn) == bb)
2963 /* The expression is generated in this block.
2964 The only time we care about this is when the expression
2965 is generated later in the block [and thus there's a loop].
2966 We let the normal cse pass handle the other cases. */
2967 if (INSN_CUID (insn) < INSN_CUID (occr->insn))
2969 if (expr_reaches_here_p (occr, expr, bb, 1, NULL))
2974 insn_computes_expr = occr->insn;
2978 else /* Computation of the pattern outside this block. */
2980 if (expr_reaches_here_p (occr, expr, bb, 0, NULL))
2985 insn_computes_expr = occr->insn;
2990 if (insn_computes_expr == NULL)
2992 return insn_computes_expr;
2996 /* Return non-zero if the definition in DEF_INSN can reach INSN.
2997 Only called by can_disregard_other_sets. */
3000 def_reaches_here_p (insn, def_insn)
3005 if (TEST_BIT (reaching_defs[BLOCK_NUM (insn)], INSN_CUID (def_insn)))
3008 if (BLOCK_NUM (insn) == BLOCK_NUM (def_insn))
3010 if (INSN_CUID (def_insn) < INSN_CUID (insn))
3012 if (GET_CODE (PATTERN (def_insn)) == PARALLEL)
3014 if (GET_CODE (PATTERN (def_insn)) == CLOBBER)
3015 reg = XEXP (PATTERN (def_insn), 0);
3016 else if (GET_CODE (PATTERN (def_insn)) == SET)
3017 reg = SET_DEST (PATTERN (def_insn));
3020 return ! reg_set_between_p (reg, NEXT_INSN (def_insn), insn);
3029 /* Return non-zero if *ADDR_THIS_REG can only have one value at INSN.
3030 The value returned is the number of definitions that reach INSN.
3031 Returning a value of zero means that [maybe] more than one definition
3032 reaches INSN and the caller can't perform whatever optimization it is
3033 trying. i.e. it is always safe to return zero. */
3036 can_disregard_other_sets (addr_this_reg, insn, for_combine)
3037 struct reg_set **addr_this_reg;
3041 int number_of_reaching_defs = 0;
3042 struct reg_set *this_reg = *addr_this_reg;
3046 if (def_reaches_here_p (insn, this_reg->insn))
3048 number_of_reaching_defs++;
3049 /* Ignore parallels for now. */
3050 if (GET_CODE (PATTERN (this_reg->insn)) == PARALLEL)
3053 && (GET_CODE (PATTERN (this_reg->insn)) == CLOBBER
3054 || ! rtx_equal_p (SET_SRC (PATTERN (this_reg->insn)),
3055 SET_SRC (PATTERN (insn)))))
3057 /* A setting of the reg to a different value reaches INSN. */
3060 if (number_of_reaching_defs > 1)
3062 /* If in this setting the value the register is being
3063 set to is equal to the previous value the register
3064 was set to and this setting reaches the insn we are
3065 trying to do the substitution on then we are ok. */
3067 if (GET_CODE (PATTERN (this_reg->insn)) == CLOBBER)
3069 if (! rtx_equal_p (SET_SRC (PATTERN (this_reg->insn)),
3070 SET_SRC (PATTERN (insn))))
3073 *addr_this_reg = this_reg;
3076 /* prev_this_reg = this_reg; */
3077 this_reg = this_reg->next;
3080 return number_of_reaching_defs;
3083 /* Expression computed by insn is available and the substitution is legal,
3084 so try to perform the substitution.
3086 The result is non-zero if any changes were made. */
3089 handle_avail_expr (insn, expr)
3093 rtx pat, insn_computes_expr;
3095 struct reg_set *this_reg;
3096 int found_setting, use_src;
3099 /* We only handle the case where one computation of the expression
3100 reaches this instruction. */
3101 insn_computes_expr = computing_insn (expr, insn);
3102 if (insn_computes_expr == NULL)
3108 /* At this point we know only one computation of EXPR outside of this
3109 block reaches this insn. Now try to find a register that the
3110 expression is computed into. */
3112 if (GET_CODE (SET_SRC (PATTERN (insn_computes_expr))) == REG)
3114 /* This is the case when the available expression that reaches
3115 here has already been handled as an available expression. */
3116 int regnum_for_replacing = REGNO (SET_SRC (PATTERN (insn_computes_expr)));
3117 /* If the register was created by GCSE we can't use `reg_set_table',
3118 however we know it's set only once. */
3119 if (regnum_for_replacing >= max_gcse_regno
3120 /* If the register the expression is computed into is set only once,
3121 or only one set reaches this insn, we can use it. */
3122 || (((this_reg = reg_set_table[regnum_for_replacing]),
3123 this_reg->next == NULL)
3124 || can_disregard_other_sets (&this_reg, insn, 0)))
3133 int regnum_for_replacing = REGNO (SET_DEST (PATTERN (insn_computes_expr)));
3134 /* This shouldn't happen. */
3135 if (regnum_for_replacing >= max_gcse_regno)
3137 this_reg = reg_set_table[regnum_for_replacing];
3138 /* If the register the expression is computed into is set only once,
3139 or only one set reaches this insn, use it. */
3140 if (this_reg->next == NULL
3141 || can_disregard_other_sets (&this_reg, insn, 0))
3147 pat = PATTERN (insn);
3149 to = SET_SRC (PATTERN (insn_computes_expr));
3151 to = SET_DEST (PATTERN (insn_computes_expr));
3152 changed = validate_change (insn, &SET_SRC (pat), to, 0);
3154 /* We should be able to ignore the return code from validate_change but
3155 to play it safe we check. */
3159 if (gcse_file != NULL)
3161 fprintf (gcse_file, "GCSE: Replacing the source in insn %d with reg %d %s insn %d\n",
3162 INSN_UID (insn), REGNO (to),
3163 use_src ? "from" : "set in",
3164 INSN_UID (insn_computes_expr));
3169 /* The register that the expr is computed into is set more than once. */
3170 else if (1 /*expensive_op(this_pattrn->op) && do_expensive_gcse)*/)
3172 /* Insert an insn after insnx that copies the reg set in insnx
3173 into a new pseudo register call this new register REGN.
3174 From insnb until end of basic block or until REGB is set
3175 replace all uses of REGB with REGN. */
3178 to = gen_reg_rtx (GET_MODE (SET_DEST (PATTERN (insn_computes_expr))));
3180 /* Generate the new insn. */
3181 /* ??? If the change fails, we return 0, even though we created
3182 an insn. I think this is ok. */
3184 = emit_insn_after (gen_rtx_SET (VOIDmode, to,
3185 SET_DEST (PATTERN (insn_computes_expr))),
3186 insn_computes_expr);
3187 /* Keep block number table up to date. */
3188 set_block_num (new_insn, BLOCK_NUM (insn_computes_expr));
3189 /* Keep register set table up to date. */
3190 record_one_set (REGNO (to), new_insn);
3192 gcse_create_count++;
3193 if (gcse_file != NULL)
3195 fprintf (gcse_file, "GCSE: Creating insn %d to copy value of reg %d, computed in insn %d,\n",
3196 INSN_UID (NEXT_INSN (insn_computes_expr)),
3197 REGNO (SET_SRC (PATTERN (NEXT_INSN (insn_computes_expr)))),
3198 INSN_UID (insn_computes_expr));
3199 fprintf (gcse_file, " into newly allocated reg %d\n", REGNO (to));
3202 pat = PATTERN (insn);
3204 /* Do register replacement for INSN. */
3205 changed = validate_change (insn, &SET_SRC (pat),
3206 SET_DEST (PATTERN (NEXT_INSN (insn_computes_expr))),
3209 /* We should be able to ignore the return code from validate_change but
3210 to play it safe we check. */
3214 if (gcse_file != NULL)
3216 fprintf (gcse_file, "GCSE: Replacing the source in insn %d with reg %d set in insn %d\n",
3218 REGNO (SET_DEST (PATTERN (NEXT_INSN (insn_computes_expr)))),
3219 INSN_UID (insn_computes_expr));
3228 /* Perform classic GCSE.
3229 This is called by one_classic_gcse_pass after all the dataflow analysis
3232 The result is non-zero if a change was made. */
3240 /* Note we start at block 1. */
3243 for (bb = 1; bb < n_basic_blocks; bb++)
3245 /* Reset tables used to keep track of what's still valid [since the
3246 start of the block]. */
3247 reset_opr_set_tables ();
3249 for (insn = BLOCK_HEAD (bb);
3250 insn != NULL && insn != NEXT_INSN (BLOCK_END (bb));
3251 insn = NEXT_INSN (insn))
3253 /* Is insn of form (set (pseudo-reg) ...)? */
3255 if (GET_CODE (insn) == INSN
3256 && GET_CODE (PATTERN (insn)) == SET
3257 && GET_CODE (SET_DEST (PATTERN (insn))) == REG
3258 && REGNO (SET_DEST (PATTERN (insn))) >= FIRST_PSEUDO_REGISTER)
3260 rtx pat = PATTERN (insn);
3261 rtx src = SET_SRC (pat);
3264 if (want_to_gcse_p (src)
3265 /* Is the expression recorded? */
3266 && ((expr = lookup_expr (src)) != NULL)
3267 /* Is the expression available [at the start of the
3269 && TEST_BIT (ae_in[bb], expr->bitmap_index)
3270 /* Are the operands unchanged since the start of the
3272 && oprs_not_set_p (src, insn))
3273 changed |= handle_avail_expr (insn, expr);
3276 /* Keep track of everything modified by this insn. */
3277 /* ??? Need to be careful w.r.t. mods done to INSN. */
3278 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3279 mark_oprs_set (insn);
3286 /* Top level routine to perform one classic GCSE pass.
3288 Return non-zero if a change was made. */
3291 one_classic_gcse_pass (pass)
3296 gcse_subst_count = 0;
3297 gcse_create_count = 0;
3299 alloc_expr_hash_table (max_cuid);
3300 alloc_rd_mem (n_basic_blocks, max_cuid);
3301 compute_expr_hash_table ();
3303 dump_hash_table (gcse_file, "Expression", expr_hash_table,
3304 expr_hash_table_size, n_exprs);
3309 alloc_avail_expr_mem (n_basic_blocks, n_exprs);
3312 compute_available ();
3313 changed = classic_gcse ();
3314 free_avail_expr_mem ();
3317 free_expr_hash_table ();
3321 fprintf (gcse_file, "\n");
3322 fprintf (gcse_file, "GCSE of %s, pass %d: %d bytes needed, %d substs, %d insns created\n",
3323 current_function_name, pass,
3324 bytes_used, gcse_subst_count, gcse_create_count);
3330 /* Compute copy/constant propagation working variables. */
3332 /* Local properties of assignments. */
3334 static sbitmap *cprop_pavloc;
3335 static sbitmap *cprop_absaltered;
3337 /* Global properties of assignments (computed from the local properties). */
3339 static sbitmap *cprop_avin;
3340 static sbitmap *cprop_avout;
3342 /* Allocate vars used for copy/const propagation.
3343 N_BLOCKS is the number of basic blocks.
3344 N_SETS is the number of sets. */
3347 alloc_cprop_mem (n_blocks, n_sets)
3348 int n_blocks, n_sets;
3350 cprop_pavloc = sbitmap_vector_alloc (n_blocks, n_sets);
3351 cprop_absaltered = sbitmap_vector_alloc (n_blocks, n_sets);
3353 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
3354 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
3357 /* Free vars used by copy/const propagation. */
3362 free (cprop_pavloc);
3363 free (cprop_absaltered);
3368 /* For each block, compute whether X is transparent.
3369 X is either an expression or an assignment [though we don't care which,
3370 for this context an assignment is treated as an expression].
3371 For each block where an element of X is modified, set (SET_P == 1) or reset
3372 (SET_P == 0) the INDX bit in BMAP. */
3375 compute_transp (x, indx, bmap, set_p)
3385 /* repeat is used to turn tail-recursion into iteration. */
3391 code = GET_CODE (x);
3397 int regno = REGNO (x);
3401 if (regno < FIRST_PSEUDO_REGISTER)
3403 for (bb = 0; bb < n_basic_blocks; bb++)
3404 if (TEST_BIT (reg_set_in_block[bb], regno))
3405 SET_BIT (bmap[bb], indx);
3409 for (r = reg_set_table[regno]; r != NULL; r = r->next)
3411 bb = BLOCK_NUM (r->insn);
3412 SET_BIT (bmap[bb], indx);
3418 if (regno < FIRST_PSEUDO_REGISTER)
3420 for (bb = 0; bb < n_basic_blocks; bb++)
3421 if (TEST_BIT (reg_set_in_block[bb], regno))
3422 RESET_BIT (bmap[bb], indx);
3426 for (r = reg_set_table[regno]; r != NULL; r = r->next)
3428 bb = BLOCK_NUM (r->insn);
3429 RESET_BIT (bmap[bb], indx);
3439 for (bb = 0; bb < n_basic_blocks; bb++)
3440 if (mem_set_in_block[bb])
3441 SET_BIT (bmap[bb], indx);
3445 for (bb = 0; bb < n_basic_blocks; bb++)
3446 if (mem_set_in_block[bb])
3447 RESET_BIT (bmap[bb], indx);
3467 i = GET_RTX_LENGTH (code) - 1;
3468 fmt = GET_RTX_FORMAT (code);
3473 rtx tem = XEXP (x, i);
3475 /* If we are about to do the last recursive call
3476 needed at this level, change it into iteration.
3477 This function is called enough to be worth it. */
3483 compute_transp (tem, indx, bmap, set_p);
3485 else if (fmt[i] == 'E')
3488 for (j = 0; j < XVECLEN (x, i); j++)
3489 compute_transp (XVECEXP (x, i, j), indx, bmap, set_p);
3494 /* Compute the available expressions at the start and end of each
3495 basic block for cprop. This particular dataflow equation is
3496 used often enough that we might want to generalize it and make
3497 as a subroutine for other global optimizations that need available
3498 in/out information. */
3500 compute_cprop_avinout ()
3502 int bb, changed, passes;
3504 sbitmap_zero (cprop_avin[0]);
3505 sbitmap_vector_ones (cprop_avout, n_basic_blocks);
3512 for (bb = 0; bb < n_basic_blocks; bb++)
3515 sbitmap_intersect_of_predecessors (cprop_avin[bb],
3516 cprop_avout, bb, s_preds);
3517 changed |= sbitmap_union_of_diff (cprop_avout[bb],
3520 cprop_absaltered[bb]);
3526 fprintf (gcse_file, "cprop avail expr computation: %d passes\n", passes);
3529 /* Top level routine to do the dataflow analysis needed by copy/const
3533 compute_cprop_data ()
3535 compute_local_properties (cprop_absaltered, cprop_pavloc, NULL, 1);
3536 compute_cprop_avinout ();
3539 /* Copy/constant propagation. */
3541 /* Maximum number of register uses in an insn that we handle. */
3544 /* Table of uses found in an insn.
3545 Allocated statically to avoid alloc/free complexity and overhead. */
3546 static struct reg_use reg_use_table[MAX_USES];
3548 /* Index into `reg_use_table' while building it. */
3549 static int reg_use_count;
3551 /* Set up a list of register numbers used in INSN.
3552 The found uses are stored in `reg_use_table'.
3553 `reg_use_count' is initialized to zero before entry, and
3554 contains the number of uses in the table upon exit.
3556 ??? If a register appears multiple times we will record it multiple
3557 times. This doesn't hurt anything but it will slow things down. */
3567 /* repeat is used to turn tail-recursion into iteration. */
3573 code = GET_CODE (x);
3577 if (reg_use_count == MAX_USES)
3579 reg_use_table[reg_use_count].reg_rtx = x;
3597 case ASM_INPUT: /*FIXME*/
3601 if (GET_CODE (SET_DEST (x)) == MEM)
3602 find_used_regs (SET_DEST (x));
3610 /* Recursively scan the operands of this expression. */
3612 fmt = GET_RTX_FORMAT (code);
3613 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3617 /* If we are about to do the last recursive call
3618 needed at this level, change it into iteration.
3619 This function is called enough to be worth it. */
3625 find_used_regs (XEXP (x, i));
3627 else if (fmt[i] == 'E')
3630 for (j = 0; j < XVECLEN (x, i); j++)
3631 find_used_regs (XVECEXP (x, i, j));
3636 /* Try to replace all non-SET_DEST occurrences of FROM in INSN with TO.
3637 Returns non-zero is successful. */
3640 try_replace_reg (from, to, insn)
3643 /* If this fails we could try to simplify the result of the
3644 replacement and attempt to recognize the simplified insn.
3646 But we need a general simplify_rtx that doesn't have pass
3647 specific state variables. I'm not aware of one at the moment. */
3648 return validate_replace_src (from, to, insn);
3651 /* Find a set of REGNO that is available on entry to INSN's block.
3652 Returns NULL if not found. */
3654 static struct expr *
3655 find_avail_set (regno, insn)
3659 /* SET1 contains the last set found that can be returned to the caller for
3660 use in a substitution. */
3661 struct expr *set1 = 0;
3663 /* Loops are not possible here. To get a loop we would need two sets
3664 available at the start of the block containing INSN. ie we would
3665 need two sets like this available at the start of the block:
3667 (set (reg X) (reg Y))
3668 (set (reg Y) (reg X))
3670 This can not happen since the set of (reg Y) would have killed the
3671 set of (reg X) making it unavailable at the start of this block. */
3675 struct expr *set = lookup_set (regno, NULL_RTX);
3677 /* Find a set that is available at the start of the block
3678 which contains INSN. */
3681 if (TEST_BIT (cprop_avin[BLOCK_NUM (insn)], set->bitmap_index))
3683 set = next_set (regno, set);
3686 /* If no available set was found we've reached the end of the
3687 (possibly empty) copy chain. */
3691 if (GET_CODE (set->expr) != SET)
3694 src = SET_SRC (set->expr);
3696 /* We know the set is available.
3697 Now check that SRC is ANTLOC (i.e. none of the source operands
3698 have changed since the start of the block).
3700 If the source operand changed, we may still use it for the next
3701 iteration of this loop, but we may not use it for substitutions. */
3702 if (CONSTANT_P (src) || oprs_not_set_p (src, insn))
3705 /* If the source of the set is anything except a register, then
3706 we have reached the end of the copy chain. */
3707 if (GET_CODE (src) != REG)
3710 /* Follow the copy chain, ie start another iteration of the loop
3711 and see if we have an available copy into SRC. */
3712 regno = REGNO (src);
3715 /* SET1 holds the last set that was available and anticipatable at
3720 /* Subroutine of cprop_insn that tries to propagate constants into
3721 JUMP_INSNS. INSN must be a conditional jump; COPY is a copy of it
3722 that we can use for substitutions.
3723 REG_USED is the use we will try to replace, SRC is the constant we
3724 will try to substitute for it.
3725 Returns nonzero if a change was made. */
3727 cprop_jump (insn, copy, reg_used, src)
3729 struct reg_use *reg_used;
3732 rtx set = PATTERN (copy);
3735 /* Replace the register with the appropriate constant. */
3736 replace_rtx (SET_SRC (set), reg_used->reg_rtx, src);
3738 temp = simplify_ternary_operation (GET_CODE (SET_SRC (set)),
3739 GET_MODE (SET_SRC (set)),
3740 GET_MODE (XEXP (SET_SRC (set), 0)),
3741 XEXP (SET_SRC (set), 0),
3742 XEXP (SET_SRC (set), 1),
3743 XEXP (SET_SRC (set), 2));
3745 /* If no simplification can be made, then try the next
3750 SET_SRC (set) = temp;
3752 /* That may have changed the structure of TEMP, so
3753 force it to be rerecognized if it has not turned
3754 into a nop or unconditional jump. */
3756 INSN_CODE (copy) = -1;
3757 if ((SET_DEST (set) == pc_rtx
3758 && (SET_SRC (set) == pc_rtx
3759 || GET_CODE (SET_SRC (set)) == LABEL_REF))
3760 || recog (PATTERN (copy), copy, NULL) >= 0)
3762 /* This has either become an unconditional jump
3763 or a nop-jump. We'd like to delete nop jumps
3764 here, but doing so confuses gcse. So we just
3765 make the replacement and let later passes
3767 PATTERN (insn) = set;
3768 INSN_CODE (insn) = -1;
3770 /* One less use of the label this insn used to jump to
3771 if we turned this into a NOP jump. */
3772 if (SET_SRC (set) == pc_rtx && JUMP_LABEL (insn) != 0)
3773 --LABEL_NUSES (JUMP_LABEL (insn));
3775 /* If this has turned into an unconditional jump,
3776 then put a barrier after it so that the unreachable
3777 code will be deleted. */
3778 if (GET_CODE (SET_SRC (set)) == LABEL_REF)
3779 emit_barrier_after (insn);
3781 run_jump_opt_after_gcse = 1;
3784 if (gcse_file != NULL)
3786 int regno = REGNO (reg_used->reg_rtx);
3787 fprintf (gcse_file, "CONST-PROP: Replacing reg %d in insn %d with constant ",
3788 regno, INSN_UID (insn));
3789 print_rtl (gcse_file, src);
3790 fprintf (gcse_file, "\n");
3798 /* Subroutine of cprop_insn that tries to propagate constants into
3799 JUMP_INSNS for machines that have CC0. INSN is a single set that
3800 stores into CC0; the insn following it is a conditional jump.
3801 REG_USED is the use we will try to replace, SRC is the constant we
3802 will try to substitute for it.
3803 Returns nonzero if a change was made. */
3805 cprop_cc0_jump (insn, reg_used, src)
3807 struct reg_use *reg_used;
3810 rtx jump = NEXT_INSN (insn);
3811 rtx copy = copy_rtx (jump);
3812 rtx set = PATTERN (copy);
3814 /* We need to copy the source of the cc0 setter, as cprop_jump is going to
3815 substitute into it. */
3816 replace_rtx (SET_SRC (set), cc0_rtx, copy_rtx (SET_SRC (PATTERN (insn))));
3817 if (! cprop_jump (jump, copy, reg_used, src))
3820 /* If we succeeded, delete the cc0 setter. */
3821 PUT_CODE (insn, NOTE);
3822 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
3823 NOTE_SOURCE_FILE (insn) = 0;
3828 /* Perform constant and copy propagation on INSN.
3829 The result is non-zero if a change was made. */
3832 cprop_insn (insn, alter_jumps)
3836 struct reg_use *reg_used;
3839 /* Only propagate into SETs. Note that a conditional jump is a
3840 SET with pc_rtx as the destination. */
3841 if ((GET_CODE (insn) != INSN
3842 && GET_CODE (insn) != JUMP_INSN)
3843 || GET_CODE (PATTERN (insn)) != SET)
3847 find_used_regs (PATTERN (insn));
3849 reg_used = ®_use_table[0];
3850 for ( ; reg_use_count > 0; reg_used++, reg_use_count--)
3854 int regno = REGNO (reg_used->reg_rtx);
3856 /* Ignore registers created by GCSE.
3857 We do this because ... */
3858 if (regno >= max_gcse_regno)
3861 /* If the register has already been set in this block, there's
3862 nothing we can do. */
3863 if (! oprs_not_set_p (reg_used->reg_rtx, insn))
3866 /* Find an assignment that sets reg_used and is available
3867 at the start of the block. */
3868 set = find_avail_set (regno, insn);
3873 /* ??? We might be able to handle PARALLELs. Later. */
3874 if (GET_CODE (pat) != SET)
3876 src = SET_SRC (pat);
3878 /* Constant propagation. */
3879 if (GET_CODE (src) == CONST_INT || GET_CODE (src) == CONST_DOUBLE
3880 || GET_CODE (src) == SYMBOL_REF)
3882 /* Handle normal insns first. */
3883 if (GET_CODE (insn) == INSN
3884 && try_replace_reg (reg_used->reg_rtx, src, insn))
3888 if (gcse_file != NULL)
3890 fprintf (gcse_file, "CONST-PROP: Replacing reg %d in insn %d with constant ",
3891 regno, INSN_UID (insn));
3892 print_rtl (gcse_file, src);
3893 fprintf (gcse_file, "\n");
3896 /* The original insn setting reg_used may or may not now be
3897 deletable. We leave the deletion to flow. */
3900 /* Try to propagate a CONST_INT into a conditional jump.
3901 We're pretty specific about what we will handle in this
3902 code, we can extend this as necessary over time.
3904 Right now the insn in question must look like
3905 (set (pc) (if_then_else ...)) */
3906 else if (alter_jumps
3907 && GET_CODE (insn) == JUMP_INSN
3908 && condjump_p (insn)
3909 && ! simplejump_p (insn))
3910 changed |= cprop_jump (insn, copy_rtx (insn), reg_used, src);
3912 /* Similar code for machines that use a pair of CC0 setter and
3913 conditional jump insn. */
3914 else if (alter_jumps
3915 && GET_CODE (PATTERN (insn)) == SET
3916 && SET_DEST (PATTERN (insn)) == cc0_rtx
3917 && GET_CODE (NEXT_INSN (insn)) == JUMP_INSN
3918 && condjump_p (NEXT_INSN (insn))
3919 && ! simplejump_p (NEXT_INSN (insn)))
3920 changed |= cprop_cc0_jump (insn, reg_used, src);
3923 else if (GET_CODE (src) == REG
3924 && REGNO (src) >= FIRST_PSEUDO_REGISTER
3925 && REGNO (src) != regno)
3927 if (try_replace_reg (reg_used->reg_rtx, src, insn))
3931 if (gcse_file != NULL)
3933 fprintf (gcse_file, "COPY-PROP: Replacing reg %d in insn %d with reg %d\n",
3934 regno, INSN_UID (insn), REGNO (src));
3937 /* The original insn setting reg_used may or may not now be
3938 deletable. We leave the deletion to flow. */
3939 /* FIXME: If it turns out that the insn isn't deletable,
3940 then we may have unnecessarily extended register lifetimes
3941 and made things worse. */
3949 /* Forward propagate copies.
3950 This includes copies and constants.
3951 Return non-zero if a change was made. */
3960 /* Note we start at block 1. */
3963 for (bb = 1; bb < n_basic_blocks; bb++)
3965 /* Reset tables used to keep track of what's still valid [since the
3966 start of the block]. */
3967 reset_opr_set_tables ();
3969 for (insn = BLOCK_HEAD (bb);
3970 insn != NULL && insn != NEXT_INSN (BLOCK_END (bb));
3971 insn = NEXT_INSN (insn))
3973 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
3975 changed |= cprop_insn (insn, alter_jumps);
3977 /* Keep track of everything modified by this insn. */
3978 /* ??? Need to be careful w.r.t. mods done to INSN. Don't
3979 call mark_oprs_set if we turned the insn into a NOTE. */
3980 if (GET_CODE (insn) != NOTE)
3981 mark_oprs_set (insn);
3986 if (gcse_file != NULL)
3987 fprintf (gcse_file, "\n");
3992 /* Perform one copy/constant propagation pass.
3993 F is the first insn in the function.
3994 PASS is the pass count. */
3997 one_cprop_pass (pass, alter_jumps)
4003 const_prop_count = 0;
4004 copy_prop_count = 0;
4006 alloc_set_hash_table (max_cuid);
4007 compute_set_hash_table ();
4009 dump_hash_table (gcse_file, "SET", set_hash_table, set_hash_table_size,
4013 alloc_cprop_mem (n_basic_blocks, n_sets);
4014 compute_cprop_data ();
4015 changed = cprop (alter_jumps);
4018 free_set_hash_table ();
4022 fprintf (gcse_file, "CPROP of %s, pass %d: %d bytes needed, %d const props, %d copy props\n",
4023 current_function_name, pass,
4024 bytes_used, const_prop_count, copy_prop_count);
4025 fprintf (gcse_file, "\n");
4031 /* Compute PRE+LCM working variables. */
4033 /* Local properties of expressions. */
4034 /* Nonzero for expressions that are transparent in the block. */
4035 static sbitmap *transp;
4037 /* Nonzero for expressions that are transparent at the end of the block.
4038 This is only zero for expressions killed by abnormal critical edge
4039 created by a calls. */
4040 static sbitmap *transpout;
4042 /* Nonzero for expressions that are computed (available) in the block. */
4043 static sbitmap *comp;
4045 /* Nonzero for expressions that are locally anticipatable in the block. */
4046 static sbitmap *antloc;
4048 /* Nonzero for expressions where this block is an optimal computation
4050 static sbitmap *pre_optimal;
4052 /* Nonzero for expressions which are redundant in a particular block. */
4053 static sbitmap *pre_redundant;
4055 static sbitmap *temp_bitmap;
4057 /* Redundant insns. */
4058 static sbitmap pre_redundant_insns;
4060 /* Allocate vars used for PRE analysis. */
4063 alloc_pre_mem (n_blocks, n_exprs)
4064 int n_blocks, n_exprs;
4066 transp = sbitmap_vector_alloc (n_blocks, n_exprs);
4067 comp = sbitmap_vector_alloc (n_blocks, n_exprs);
4068 antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
4070 temp_bitmap = sbitmap_vector_alloc (n_blocks, n_exprs);
4071 pre_optimal = sbitmap_vector_alloc (n_blocks, n_exprs);
4072 pre_redundant = sbitmap_vector_alloc (n_blocks, n_exprs);
4073 transpout = sbitmap_vector_alloc (n_blocks, n_exprs);
4076 /* Free vars used for PRE analysis. */
4086 free (pre_redundant);
4090 /* Top level routine to do the dataflow analysis needed by PRE. */
4095 compute_local_properties (transp, comp, antloc, 0);
4096 compute_transpout ();
4097 pre_lcm (n_basic_blocks, n_exprs, s_preds, s_succs, transp,
4098 antloc, pre_redundant, pre_optimal);
4104 /* Return non-zero if an occurrence of expression EXPR in OCCR_BB would reach
4107 VISITED is a pointer to a working buffer for tracking which BB's have
4108 been visited. It is NULL for the top-level call.
4110 CHECK_PRE_COMP controls whether or not we check for a computation of
4113 We treat reaching expressions that go through blocks containing the same
4114 reaching expression as "not reaching". E.g. if EXPR is generated in blocks
4115 2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
4116 2 as not reaching. The intent is to improve the probability of finding
4117 only one reaching expression and to reduce register lifetimes by picking
4118 the closest such expression. */
4121 pre_expr_reaches_here_p (occr_bb, expr, bb, check_pre_comp, visited)
4130 if (visited == NULL)
4132 visited = (char *) alloca (n_basic_blocks);
4133 bzero (visited, n_basic_blocks);
4136 for (pred = s_preds[bb]; pred != NULL; pred = pred->next)
4138 int pred_bb = INT_LIST_VAL (pred);
4140 if (pred_bb == ENTRY_BLOCK
4141 /* Has predecessor has already been visited? */
4142 || visited[pred_bb])
4144 /* Nothing to do. */
4146 /* Does this predecessor generate this expression? */
4147 else if ((!check_pre_comp && occr_bb == pred_bb)
4148 || TEST_BIT (comp[pred_bb], expr->bitmap_index))
4150 /* Is this the occurrence we're looking for?
4151 Note that there's only one generating occurrence per block
4152 so we just need to check the block number. */
4153 if (occr_bb == pred_bb)
4155 visited[pred_bb] = 1;
4157 /* Ignore this predecessor if it kills the expression. */
4158 else if (! TEST_BIT (transp[pred_bb], expr->bitmap_index))
4159 visited[pred_bb] = 1;
4160 /* Neither gen nor kill. */
4163 visited[pred_bb] = 1;
4164 if (pre_expr_reaches_here_p (occr_bb, expr, pred_bb,
4165 check_pre_comp, visited))
4170 /* All paths have been checked. */
4174 /* Add EXPR to the end of basic block BB.
4176 This is used by both the PRE and code hoisting.
4178 For PRE, we want to verify that the expr is either transparent
4179 or locally anticipatable in the target block. This check makes
4180 no sense for code hoisting. */
4183 insert_insn_end_bb (expr, bb, pre)
4188 rtx insn = BLOCK_END (bb);
4190 rtx reg = expr->reaching_reg;
4191 int regno = REGNO (reg);
4192 rtx pat, copied_expr;
4196 copied_expr = copy_rtx (expr->expr);
4197 emit_move_insn (reg, copied_expr);
4198 first_new_insn = get_insns ();
4199 pat = gen_sequence ();
4202 /* If the last insn is a jump, insert EXPR in front [taking care to
4203 handle cc0, etc. properly]. */
4205 if (GET_CODE (insn) == JUMP_INSN)
4211 /* If this is a jump table, then we can't insert stuff here. Since
4212 we know the previous real insn must be the tablejump, we insert
4213 the new instruction just before the tablejump. */
4214 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
4215 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
4216 insn = prev_real_insn (insn);
4219 /* FIXME: 'twould be nice to call prev_cc0_setter here but it aborts
4220 if cc0 isn't set. */
4221 note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
4223 insn = XEXP (note, 0);
4226 rtx maybe_cc0_setter = prev_nonnote_insn (insn);
4227 if (maybe_cc0_setter
4228 && GET_RTX_CLASS (GET_CODE (maybe_cc0_setter)) == 'i'
4229 && sets_cc0_p (PATTERN (maybe_cc0_setter)))
4230 insn = maybe_cc0_setter;
4233 /* FIXME: What if something in cc0/jump uses value set in new insn? */
4234 new_insn = emit_insn_before (pat, insn);
4235 if (BLOCK_HEAD (bb) == insn)
4236 BLOCK_HEAD (bb) = new_insn;
4238 /* Likewise if the last insn is a call, as will happen in the presence
4239 of exception handling. */
4240 else if (GET_CODE (insn) == CALL_INSN)
4242 HARD_REG_SET parm_regs;
4246 /* Keeping in mind SMALL_REGISTER_CLASSES and parameters in registers,
4247 we search backward and place the instructions before the first
4248 parameter is loaded. Do this for everyone for consistency and a
4249 presumtion that we'll get better code elsewhere as well. */
4251 /* It should always be the case that we can put these instructions
4252 anywhere in the basic block with performing PRE optimizations.
4255 && !TEST_BIT (antloc[bb], expr->bitmap_index)
4256 && !TEST_BIT (transp[bb], expr->bitmap_index))
4259 /* Since different machines initialize their parameter registers
4260 in different orders, assume nothing. Collect the set of all
4261 parameter registers. */
4262 CLEAR_HARD_REG_SET (parm_regs);
4264 for (p = CALL_INSN_FUNCTION_USAGE (insn); p ; p = XEXP (p, 1))
4265 if (GET_CODE (XEXP (p, 0)) == USE
4266 && GET_CODE (XEXP (XEXP (p, 0), 0)) == REG)
4268 int regno = REGNO (XEXP (XEXP (p, 0), 0));
4269 if (regno >= FIRST_PSEUDO_REGISTER)
4271 SET_HARD_REG_BIT (parm_regs, regno);
4275 /* Search backward for the first set of a register in this set. */
4276 while (nparm_regs && BLOCK_HEAD (bb) != insn)
4278 insn = PREV_INSN (insn);
4279 p = single_set (insn);
4280 if (p && GET_CODE (SET_DEST (p)) == REG
4281 && REGNO (SET_DEST (p)) < FIRST_PSEUDO_REGISTER
4282 && TEST_HARD_REG_BIT (parm_regs, REGNO (SET_DEST (p))))
4284 CLEAR_HARD_REG_BIT (parm_regs, REGNO (SET_DEST (p)));
4289 /* If we found all the parameter loads, then we want to insert
4290 before the first parameter load.
4292 If we did not find all the parameter loads, then we might have
4293 stopped on the head of the block, which could be a CODE_LABEL.
4294 If we inserted before the CODE_LABEL, then we would be putting
4295 the insn in the wrong basic block. In that case, put the insn
4296 after the CODE_LABEL.
4298 ?!? Do we need to account for NOTE_INSN_BASIC_BLOCK here? */
4299 if (GET_CODE (insn) != CODE_LABEL)
4301 new_insn = emit_insn_before (pat, insn);
4302 if (BLOCK_HEAD (bb) == insn)
4303 BLOCK_HEAD (bb) = new_insn;
4307 new_insn = emit_insn_after (pat, insn);
4312 new_insn = emit_insn_after (pat, insn);
4313 BLOCK_END (bb) = new_insn;
4316 /* Keep block number table up to date.
4317 Note, PAT could be a multiple insn sequence, we have to make
4318 sure that each insn in the sequence is handled. */
4319 if (GET_CODE (pat) == SEQUENCE)
4323 for (i = 0; i < XVECLEN (pat, 0); i++)
4325 rtx insn = XVECEXP (pat, 0, i);
4326 set_block_num (insn, bb);
4327 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
4328 add_label_notes (PATTERN (insn), new_insn);
4329 record_set_insn = insn;
4330 note_stores (PATTERN (insn), record_set_info);
4335 add_label_notes (SET_SRC (pat), new_insn);
4336 set_block_num (new_insn, bb);
4337 /* Keep register set table up to date. */
4338 record_one_set (regno, new_insn);
4341 gcse_create_count++;
4345 fprintf (gcse_file, "PRE/HOIST: end of bb %d, insn %d, copying expression %d to reg %d\n",
4346 bb, INSN_UID (new_insn), expr->bitmap_index, regno);
4350 /* Insert partially redundant expressions at the ends of appropriate basic
4351 blocks making them fully redundant. */
4354 pre_insert (index_map)
4355 struct expr **index_map;
4357 int bb, i, set_size;
4360 /* Compute INSERT = PRE_OPTIMAL & ~PRE_REDUNDANT.
4361 Where INSERT is nonzero, we add the expression at the end of the basic
4362 block if it reaches any of the deleted expressions. */
4364 set_size = pre_optimal[0]->size;
4365 inserted = sbitmap_vector_alloc (n_basic_blocks, n_exprs);
4366 sbitmap_vector_zero (inserted, n_basic_blocks);
4368 for (bb = 0; bb < n_basic_blocks; bb++)
4372 /* This computes the number of potential insertions we need. */
4373 sbitmap_not (temp_bitmap[bb], pre_redundant[bb]);
4374 sbitmap_a_and_b (temp_bitmap[bb], temp_bitmap[bb], pre_optimal[bb]);
4376 /* TEMP_BITMAP[bb] now contains a bitmap of the expressions that we need
4377 to insert at the end of this basic block. */
4378 for (i = indx = 0; i < set_size; i++, indx += SBITMAP_ELT_BITS)
4380 SBITMAP_ELT_TYPE insert = temp_bitmap[bb]->elms[i];
4383 for (j = indx; insert && j < n_exprs; j++, insert >>= 1)
4385 if ((insert & 1) != 0 && index_map[j]->reaching_reg != NULL_RTX)
4387 struct expr *expr = index_map[j];
4390 /* Now look at each deleted occurence of this expression. */
4391 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
4393 if (! occr->deleted_p)
4396 /* Insert this expression at the end of BB if it would
4397 reach the deleted occurence. */
4398 if (!TEST_BIT (inserted[bb], j)
4399 && pre_expr_reaches_here_p (bb, expr,
4400 BLOCK_NUM (occr->insn), 0,
4403 SET_BIT (inserted[bb], j);
4404 insert_insn_end_bb (index_map[j], bb, 1);
4413 /* Copy the result of INSN to REG.
4414 INDX is the expression number. */
4417 pre_insert_copy_insn (expr, insn)
4421 rtx reg = expr->reaching_reg;
4422 int regno = REGNO (reg);
4423 int indx = expr->bitmap_index;
4424 rtx set = single_set (insn);
4429 new_insn = emit_insn_after (gen_rtx_SET (VOIDmode, reg, SET_DEST (set)),
4431 /* Keep block number table up to date. */
4432 set_block_num (new_insn, BLOCK_NUM (insn));
4433 /* Keep register set table up to date. */
4434 record_one_set (regno, new_insn);
4436 gcse_create_count++;
4440 fprintf (gcse_file, "PRE: bb %d, insn %d, copying expression %d in insn %d to reg %d\n",
4441 BLOCK_NUM (insn), INSN_UID (new_insn), indx, INSN_UID (insn), regno);
4445 /* Copy available expressions that reach the redundant expression
4446 to `reaching_reg'. */
4449 pre_insert_copies ()
4453 for (bb = 0; bb < n_basic_blocks; bb++)
4455 sbitmap_a_and_b (temp_bitmap[bb], pre_optimal[bb], pre_redundant[bb]);
4458 /* For each available expression in the table, copy the result to
4459 `reaching_reg' if the expression reaches a deleted one.
4461 ??? The current algorithm is rather brute force.
4462 Need to do some profiling. */
4464 for (i = 0; i < expr_hash_table_size; i++)
4468 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
4472 /* If the basic block isn't reachable, PPOUT will be TRUE.
4473 However, we don't want to insert a copy here because the
4474 expression may not really be redundant. So only insert
4475 an insn if the expression was deleted.
4476 This test also avoids further processing if the expression
4477 wasn't deleted anywhere. */
4478 if (expr->reaching_reg == NULL)
4481 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
4485 if (! occr->deleted_p)
4488 for (avail = expr->avail_occr; avail != NULL; avail = avail->next)
4490 rtx insn = avail->insn;
4491 int bb = BLOCK_NUM (insn);
4493 if (!TEST_BIT (temp_bitmap[bb], expr->bitmap_index))
4496 /* No need to handle this one if handled already. */
4497 if (avail->copied_p)
4499 /* Don't handle this one if it's a redundant one. */
4500 if (TEST_BIT (pre_redundant_insns, INSN_CUID (insn)))
4502 /* Or if the expression doesn't reach the deleted one. */
4503 if (! pre_expr_reaches_here_p (BLOCK_NUM (avail->insn), expr,
4504 BLOCK_NUM (occr->insn),
4508 /* Copy the result of avail to reaching_reg. */
4509 pre_insert_copy_insn (expr, insn);
4510 avail->copied_p = 1;
4517 /* Delete redundant computations.
4518 Deletion is done by changing the insn to copy the `reaching_reg' of
4519 the expression into the result of the SET. It is left to later passes
4520 (cprop, cse2, flow, combine, regmove) to propagate the copy or eliminate it.
4522 Returns non-zero if a change is made. */
4529 /* Compute the expressions which are redundant and need to be replaced by
4530 copies from the reaching reg to the target reg. */
4531 for (bb = 0; bb < n_basic_blocks; bb++)
4533 sbitmap_not (temp_bitmap[bb], pre_optimal[bb]);
4534 sbitmap_a_and_b (temp_bitmap[bb], temp_bitmap[bb], pre_redundant[bb]);
4538 for (i = 0; i < expr_hash_table_size; i++)
4542 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
4545 int indx = expr->bitmap_index;
4547 /* We only need to search antic_occr since we require
4550 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
4552 rtx insn = occr->insn;
4554 int bb = BLOCK_NUM (insn);
4556 if (TEST_BIT (temp_bitmap[bb], indx))
4558 set = single_set (insn);
4562 /* Create a pseudo-reg to store the result of reaching
4563 expressions into. Get the mode for the new pseudo
4564 from the mode of the original destination pseudo. */
4565 if (expr->reaching_reg == NULL)
4567 = gen_reg_rtx (GET_MODE (SET_DEST (set)));
4569 /* In theory this should never fail since we're creating
4572 However, on the x86 some of the movXX patterns actually
4573 contain clobbers of scratch regs. This may cause the
4574 insn created by validate_change to not match any pattern
4575 and thus cause validate_change to fail. */
4576 if (validate_change (insn, &SET_SRC (set),
4577 expr->reaching_reg, 0))
4579 occr->deleted_p = 1;
4580 SET_BIT (pre_redundant_insns, INSN_CUID (insn));
4588 "PRE: redundant insn %d (expression %d) in bb %d, reaching reg is %d\n",
4589 INSN_UID (insn), indx, bb, REGNO (expr->reaching_reg));
4599 /* Perform GCSE optimizations using PRE.
4600 This is called by one_pre_gcse_pass after all the dataflow analysis
4603 This is based on the original Morel-Renvoise paper Fred Chow's thesis,
4604 and lazy code motion from Knoop, Ruthing and Steffen as described in
4605 Advanced Compiler Design and Implementation.
4607 ??? A new pseudo reg is created to hold the reaching expression.
4608 The nice thing about the classical approach is that it would try to
4609 use an existing reg. If the register can't be adequately optimized
4610 [i.e. we introduce reload problems], one could add a pass here to
4611 propagate the new register through the block.
4613 ??? We don't handle single sets in PARALLELs because we're [currently]
4614 not able to copy the rest of the parallel when we insert copies to create
4615 full redundancies from partial redundancies. However, there's no reason
4616 why we can't handle PARALLELs in the cases where there are no partial
4624 struct expr **index_map;
4626 /* Compute a mapping from expression number (`bitmap_index') to
4627 hash table entry. */
4629 index_map = (struct expr **) alloca (n_exprs * sizeof (struct expr *));
4630 bzero ((char *) index_map, n_exprs * sizeof (struct expr *));
4631 for (i = 0; i < expr_hash_table_size; i++)
4635 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
4636 index_map[expr->bitmap_index] = expr;
4639 /* Reset bitmap used to track which insns are redundant. */
4640 pre_redundant_insns = sbitmap_alloc (max_cuid);
4641 sbitmap_zero (pre_redundant_insns);
4643 /* Delete the redundant insns first so that
4644 - we know what register to use for the new insns and for the other
4645 ones with reaching expressions
4646 - we know which insns are redundant when we go to create copies */
4647 changed = pre_delete ();
4649 /* Insert insns in places that make partially redundant expressions
4651 pre_insert (index_map);
4653 /* In other places with reaching expressions, copy the expression to the
4654 specially allocated pseudo-reg that reaches the redundant expression. */
4655 pre_insert_copies ();
4657 free (pre_redundant_insns);
4662 /* Top level routine to perform one PRE GCSE pass.
4664 Return non-zero if a change was made. */
4667 one_pre_gcse_pass (pass)
4672 gcse_subst_count = 0;
4673 gcse_create_count = 0;
4675 alloc_expr_hash_table (max_cuid);
4676 compute_expr_hash_table ();
4678 dump_hash_table (gcse_file, "Expression", expr_hash_table,
4679 expr_hash_table_size, n_exprs);
4682 alloc_pre_mem (n_basic_blocks, n_exprs);
4683 compute_pre_data ();
4684 changed |= pre_gcse ();
4687 free_expr_hash_table ();
4691 fprintf (gcse_file, "\n");
4692 fprintf (gcse_file, "PRE GCSE of %s, pass %d: %d bytes needed, %d substs, %d insns created\n",
4693 current_function_name, pass,
4694 bytes_used, gcse_subst_count, gcse_create_count);
4700 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to INSN.
4701 We have to add REG_LABEL notes, because the following loop optimization
4702 pass requires them. */
4704 /* ??? This is very similar to the loop.c add_label_notes function. We
4705 could probably share code here. */
4707 /* ??? If there was a jump optimization pass after gcse and before loop,
4708 then we would not need to do this here, because jump would add the
4709 necessary REG_LABEL notes. */
4712 add_label_notes (x, insn)
4716 enum rtx_code code = GET_CODE (x);
4720 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
4722 /* This code used to ignore labels that referred to dispatch tables to
4723 avoid flow generating (slighly) worse code.
4725 We no longer ignore such label references (see LABEL_REF handling in
4726 mark_jump_label for additional information). */
4727 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_LABEL, XEXP (x, 0),
4732 fmt = GET_RTX_FORMAT (code);
4733 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4736 add_label_notes (XEXP (x, i), insn);
4737 else if (fmt[i] == 'E')
4738 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4739 add_label_notes (XVECEXP (x, i, j), insn);
4743 /* Compute transparent outgoing information for each block.
4745 An expression is transparent to an edge unless it is killed by
4746 the edge itself. This can only happen with abnormal control flow,
4747 when the edge is traversed through a call. This happens with
4748 non-local labels and exceptions.
4750 This would not be necessary if we split the edge. While this is
4751 normally impossible for abnormal critical edges, with some effort
4752 it should be possible with exception handling, since we still have
4753 control over which handler should be invoked. But due to increased
4754 EH table sizes, this may not be worthwhile. */
4757 compute_transpout ()
4761 sbitmap_vector_ones (transpout, n_basic_blocks);
4763 for (bb = 0; bb < n_basic_blocks; ++bb)
4767 /* Note that flow inserted a nop a the end of basic blocks that
4768 end in call instructions for reasons other than abnormal
4770 if (GET_CODE (BLOCK_END (bb)) != CALL_INSN)
4773 for (i = 0; i < expr_hash_table_size; i++)
4776 for (expr = expr_hash_table[i]; expr ; expr = expr->next_same_hash)
4777 if (GET_CODE (expr->expr) == MEM)
4779 rtx addr = XEXP (expr->expr, 0);
4781 if (GET_CODE (addr) == SYMBOL_REF
4782 && CONSTANT_POOL_ADDRESS_P (addr))
4785 /* ??? Optimally, we would use interprocedural alias
4786 analysis to determine if this mem is actually killed
4788 RESET_BIT (transpout[bb], expr->bitmap_index);