gcse.c (try_replace_reg): Use num_changes_pending.
[official-gcc.git] / gcc / gcse.c
blob620b98636be1e0058954e4ef7b33e52ba0fb0a70
1 /* Global common subexpression elimination/Partial redundancy elimination
2 and global constant/copy propagation for GNU compiler.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21 02111-1307, USA. */
23 /* TODO
24 - reordering of memory allocation and freeing to be more space efficient
25 - do rough calc of how many regs are needed in each block, and a rough
26 calc of how many regs are available in each class and use that to
27 throttle back the code in cases where RTX_COST is minimal.
28 - a store to the same address as a load does not kill the load if the
29 source of the store is also the destination of the load. Handling this
30 allows more load motion, particularly out of loops.
31 - ability to realloc sbitmap vectors would allow one initial computation
32 of reg_set_in_block with only subsequent additions, rather than
33 recomputing it for each pass
37 /* References searched while implementing this.
39 Compilers Principles, Techniques and Tools
40 Aho, Sethi, Ullman
41 Addison-Wesley, 1988
43 Global Optimization by Suppression of Partial Redundancies
44 E. Morel, C. Renvoise
45 communications of the acm, Vol. 22, Num. 2, Feb. 1979
47 A Portable Machine-Independent Global Optimizer - Design and Measurements
48 Frederick Chow
49 Stanford Ph.D. thesis, Dec. 1983
51 A Fast Algorithm for Code Movement Optimization
52 D.M. Dhamdhere
53 SIGPLAN Notices, Vol. 23, Num. 10, Oct. 1988
55 A Solution to a Problem with Morel and Renvoise's
56 Global Optimization by Suppression of Partial Redundancies
57 K-H Drechsler, M.P. Stadel
58 ACM TOPLAS, Vol. 10, Num. 4, Oct. 1988
60 Practical Adaptation of the Global Optimization
61 Algorithm of Morel and Renvoise
62 D.M. Dhamdhere
63 ACM TOPLAS, Vol. 13, Num. 2. Apr. 1991
65 Efficiently Computing Static Single Assignment Form and the Control
66 Dependence Graph
67 R. Cytron, J. Ferrante, B.K. Rosen, M.N. Wegman, and F.K. Zadeck
68 ACM TOPLAS, Vol. 13, Num. 4, Oct. 1991
70 Lazy Code Motion
71 J. Knoop, O. Ruthing, B. Steffen
72 ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
74 What's In a Region? Or Computing Control Dependence Regions in Near-Linear
75 Time for Reducible Flow Control
76 Thomas Ball
77 ACM Letters on Programming Languages and Systems,
78 Vol. 2, Num. 1-4, Mar-Dec 1993
80 An Efficient Representation for Sparse Sets
81 Preston Briggs, Linda Torczon
82 ACM Letters on Programming Languages and Systems,
83 Vol. 2, Num. 1-4, Mar-Dec 1993
85 A Variation of Knoop, Ruthing, and Steffen's Lazy Code Motion
86 K-H Drechsler, M.P. Stadel
87 ACM SIGPLAN Notices, Vol. 28, Num. 5, May 1993
89 Partial Dead Code Elimination
90 J. Knoop, O. Ruthing, B. Steffen
91 ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
93 Effective Partial Redundancy Elimination
94 P. Briggs, K.D. Cooper
95 ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
97 The Program Structure Tree: Computing Control Regions in Linear Time
98 R. Johnson, D. Pearson, K. Pingali
99 ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
101 Optimal Code Motion: Theory and Practice
102 J. Knoop, O. Ruthing, B. Steffen
103 ACM TOPLAS, Vol. 16, Num. 4, Jul. 1994
105 The power of assignment motion
106 J. Knoop, O. Ruthing, B. Steffen
107 ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
109 Global code motion / global value numbering
110 C. Click
111 ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
113 Value Driven Redundancy Elimination
114 L.T. Simpson
115 Rice University Ph.D. thesis, Apr. 1996
117 Value Numbering
118 L.T. Simpson
119 Massively Scalar Compiler Project, Rice University, Sep. 1996
121 High Performance Compilers for Parallel Computing
122 Michael Wolfe
123 Addison-Wesley, 1996
125 Advanced Compiler Design and Implementation
126 Steven Muchnick
127 Morgan Kaufmann, 1997
129 Building an Optimizing Compiler
130 Robert Morgan
131 Digital Press, 1998
133 People wishing to speed up the code here should read:
134 Elimination Algorithms for Data Flow Analysis
135 B.G. Ryder, M.C. Paull
136 ACM Computing Surveys, Vol. 18, Num. 3, Sep. 1986
138 How to Analyze Large Programs Efficiently and Informatively
139 D.M. Dhamdhere, B.K. Rosen, F.K. Zadeck
140 ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
142 People wishing to do something different can find various possibilities
143 in the above papers and elsewhere.
146 #include "config.h"
147 #include "system.h"
148 #include "toplev.h"
150 #include "rtl.h"
151 #include "tm_p.h"
152 #include "regs.h"
153 #include "hard-reg-set.h"
154 #include "flags.h"
155 #include "real.h"
156 #include "insn-config.h"
157 #include "recog.h"
158 #include "basic-block.h"
159 #include "output.h"
160 #include "function.h"
161 #include "expr.h"
162 #include "except.h"
163 #include "ggc.h"
164 #include "params.h"
165 #include "cselib.h"
167 #include "obstack.h"
169 /* We don't want to use xmalloc. */
170 #undef obstack_chunk_alloc
171 #define obstack_chunk_alloc gmalloc
173 /* Propagate flow information through back edges and thus enable PRE's
174 moving loop invariant calculations out of loops.
176 Originally this tended to create worse overall code, but several
177 improvements during the development of PRE seem to have made following
178 back edges generally a win.
180 Note much of the loop invariant code motion done here would normally
181 be done by loop.c, which has more heuristics for when to move invariants
182 out of loops. At some point we might need to move some of those
183 heuristics into gcse.c. */
184 #define FOLLOW_BACK_EDGES 1
186 /* We support GCSE via Partial Redundancy Elimination. PRE optimizations
187 are a superset of those done by GCSE.
189 We perform the following steps:
191 1) Compute basic block information.
193 2) Compute table of places where registers are set.
195 3) Perform copy/constant propagation.
197 4) Perform global cse.
199 5) Perform another pass of copy/constant propagation.
201 Two passes of copy/constant propagation are done because the first one
202 enables more GCSE and the second one helps to clean up the copies that
203 GCSE creates. This is needed more for PRE than for Classic because Classic
204 GCSE will try to use an existing register containing the common
205 subexpression rather than create a new one. This is harder to do for PRE
206 because of the code motion (which Classic GCSE doesn't do).
208 Expressions we are interested in GCSE-ing are of the form
209 (set (pseudo-reg) (expression)).
210 Function want_to_gcse_p says what these are.
212 PRE handles moving invariant expressions out of loops (by treating them as
213 partially redundant).
215 Eventually it would be nice to replace cse.c/gcse.c with SSA (static single
216 assignment) based GVN (global value numbering). L. T. Simpson's paper
217 (Rice University) on value numbering is a useful reference for this.
219 **********************
221 We used to support multiple passes but there are diminishing returns in
222 doing so. The first pass usually makes 90% of the changes that are doable.
223 A second pass can make a few more changes made possible by the first pass.
224 Experiments show any further passes don't make enough changes to justify
225 the expense.
227 A study of spec92 using an unlimited number of passes:
228 [1 pass] = 1208 substitutions, [2] = 577, [3] = 202, [4] = 192, [5] = 83,
229 [6] = 34, [7] = 17, [8] = 9, [9] = 4, [10] = 4, [11] = 2,
230 [12] = 2, [13] = 1, [15] = 1, [16] = 2, [41] = 1
232 It was found doing copy propagation between each pass enables further
233 substitutions.
235 PRE is quite expensive in complicated functions because the DFA can take
236 awhile to converge. Hence we only perform one pass. The parameter max-gcse-passes can
237 be modified if one wants to experiment.
239 **********************
241 The steps for PRE are:
243 1) Build the hash table of expressions we wish to GCSE (expr_hash_table).
245 2) Perform the data flow analysis for PRE.
247 3) Delete the redundant instructions
249 4) Insert the required copies [if any] that make the partially
250 redundant instructions fully redundant.
252 5) For other reaching expressions, insert an instruction to copy the value
253 to a newly created pseudo that will reach the redundant instruction.
255 The deletion is done first so that when we do insertions we
256 know which pseudo reg to use.
258 Various papers have argued that PRE DFA is expensive (O(n^2)) and others
259 argue it is not. The number of iterations for the algorithm to converge
260 is typically 2-4 so I don't view it as that expensive (relatively speaking).
262 PRE GCSE depends heavily on the second CSE pass to clean up the copies
263 we create. To make an expression reach the place where it's redundant,
264 the result of the expression is copied to a new register, and the redundant
265 expression is deleted by replacing it with this new register. Classic GCSE
266 doesn't have this problem as much as it computes the reaching defs of
267 each register in each block and thus can try to use an existing register.
269 **********************
271 A fair bit of simplicity is created by creating small functions for simple
272 tasks, even when the function is only called in one place. This may
273 measurably slow things down [or may not] by creating more function call
274 overhead than is necessary. The source is laid out so that it's trivial
275 to make the affected functions inline so that one can measure what speed
276 up, if any, can be achieved, and maybe later when things settle things can
277 be rearranged.
279 Help stamp out big monolithic functions! */
281 /* GCSE global vars. */
283 /* -dG dump file. */
284 static FILE *gcse_file;
286 /* Note whether or not we should run jump optimization after gcse. We
287 want to do this for two cases.
289 * If we changed any jumps via cprop.
291 * If we added any labels via edge splitting. */
293 static int run_jump_opt_after_gcse;
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;
313 struct reg_use {rtx reg_rtx; };
315 /* Hash table of expressions. */
317 struct expr
319 /* The expression (SET_SRC for expressions, PATTERN for assignments). */
320 rtx expr;
321 /* Index in the available expression bitmaps. */
322 int bitmap_index;
323 /* Next entry with the same hash. */
324 struct expr *next_same_hash;
325 /* List of anticipatable occurrences in basic blocks in the function.
326 An "anticipatable occurrence" is one that is the first occurrence in the
327 basic block, the operands are not modified in the basic block prior
328 to the occurrence and the output is not used between the start of
329 the block and the occurrence. */
330 struct occr *antic_occr;
331 /* List of available occurrence in basic blocks in the function.
332 An "available occurrence" is one that is the last occurrence in the
333 basic block and the operands are not modified by following statements in
334 the basic block [including this insn]. */
335 struct occr *avail_occr;
336 /* Non-null if the computation is PRE redundant.
337 The value is the newly created pseudo-reg to record a copy of the
338 expression in all the places that reach the redundant copy. */
339 rtx reaching_reg;
342 /* Occurrence of an expression.
343 There is one per basic block. If a pattern appears more than once the
344 last appearance is used [or first for anticipatable expressions]. */
346 struct occr
348 /* Next occurrence of this expression. */
349 struct occr *next;
350 /* The insn that computes the expression. */
351 rtx insn;
352 /* Non-zero if this [anticipatable] occurrence has been deleted. */
353 char deleted_p;
354 /* Non-zero if this [available] occurrence has been copied to
355 reaching_reg. */
356 /* ??? This is mutually exclusive with deleted_p, so they could share
357 the same byte. */
358 char copied_p;
361 /* Expression and copy propagation hash tables.
362 Each hash table is an array of buckets.
363 ??? It is known that if it were an array of entries, structure elements
364 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
365 not clear whether in the final analysis a sufficient amount of memory would
366 be saved as the size of the available expression bitmaps would be larger
367 [one could build a mapping table without holes afterwards though].
368 Someday I'll perform the computation and figure it out. */
370 /* Total size of the expression hash table, in elements. */
371 static unsigned int expr_hash_table_size;
373 /* The table itself.
374 This is an array of `expr_hash_table_size' elements. */
375 static struct expr **expr_hash_table;
377 /* Total size of the copy propagation hash table, in elements. */
378 static unsigned int set_hash_table_size;
380 /* The table itself.
381 This is an array of `set_hash_table_size' elements. */
382 static struct expr **set_hash_table;
384 /* Mapping of uids to cuids.
385 Only real insns get cuids. */
386 static int *uid_cuid;
388 /* Highest UID in UID_CUID. */
389 static int max_uid;
391 /* Get the cuid of an insn. */
392 #ifdef ENABLE_CHECKING
393 #define INSN_CUID(INSN) (INSN_UID (INSN) > max_uid ? (abort (), 0) : uid_cuid[INSN_UID (INSN)])
394 #else
395 #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
396 #endif
398 /* Number of cuids. */
399 static int max_cuid;
401 /* Mapping of cuids to insns. */
402 static rtx *cuid_insn;
404 /* Get insn from cuid. */
405 #define CUID_INSN(CUID) (cuid_insn[CUID])
407 /* Maximum register number in function prior to doing gcse + 1.
408 Registers created during this pass have regno >= max_gcse_regno.
409 This is named with "gcse" to not collide with global of same name. */
410 static unsigned int max_gcse_regno;
412 /* Maximum number of cse-able expressions found. */
413 static int n_exprs;
415 /* Maximum number of assignments for copy propagation found. */
416 static int n_sets;
418 /* Table of registers that are modified.
420 For each register, each element is a list of places where the pseudo-reg
421 is set.
423 For simplicity, GCSE is done on sets of pseudo-regs only. PRE GCSE only
424 requires knowledge of which blocks kill which regs [and thus could use
425 a bitmap instead of the lists `reg_set_table' uses].
427 `reg_set_table' and could be turned into an array of bitmaps (num-bbs x
428 num-regs) [however perhaps it may be useful to keep the data as is]. One
429 advantage of recording things this way is that `reg_set_table' is fairly
430 sparse with respect to pseudo regs but for hard regs could be fairly dense
431 [relatively speaking]. And recording sets of pseudo-regs in lists speeds
432 up functions like compute_transp since in the case of pseudo-regs we only
433 need to iterate over the number of times a pseudo-reg is set, not over the
434 number of basic blocks [clearly there is a bit of a slow down in the cases
435 where a pseudo is set more than once in a block, however it is believed
436 that the net effect is to speed things up]. This isn't done for hard-regs
437 because recording call-clobbered hard-regs in `reg_set_table' at each
438 function call can consume a fair bit of memory, and iterating over
439 hard-regs stored this way in compute_transp will be more expensive. */
441 typedef struct reg_set
443 /* The next setting of this register. */
444 struct reg_set *next;
445 /* The insn where it was set. */
446 rtx insn;
447 } reg_set;
449 static reg_set **reg_set_table;
451 /* Size of `reg_set_table'.
452 The table starts out at max_gcse_regno + slop, and is enlarged as
453 necessary. */
454 static int reg_set_table_size;
456 /* Amount to grow `reg_set_table' by when it's full. */
457 #define REG_SET_TABLE_SLOP 100
459 /* This is a list of expressions which are MEMs and will be used by load
460 or store motion.
461 Load motion tracks MEMs which aren't killed by
462 anything except itself. (ie, loads and stores to a single location).
463 We can then allow movement of these MEM refs with a little special
464 allowance. (all stores copy the same value to the reaching reg used
465 for the loads). This means all values used to store into memory must have
466 no side effects so we can re-issue the setter value.
467 Store Motion uses this structure as an expression table to track stores
468 which look interesting, and might be moveable towards the exit block. */
470 struct ls_expr
472 struct expr * expr; /* Gcse expression reference for LM. */
473 rtx pattern; /* Pattern of this mem. */
474 rtx loads; /* INSN list of loads seen. */
475 rtx stores; /* INSN list of stores seen. */
476 struct ls_expr * next; /* Next in the list. */
477 int invalid; /* Invalid for some reason. */
478 int index; /* If it maps to a bitmap index. */
479 int hash_index; /* Index when in a hash table. */
480 rtx reaching_reg; /* Register to use when re-writing. */
483 /* Head of the list of load/store memory refs. */
484 static struct ls_expr * pre_ldst_mems = NULL;
486 /* Bitmap containing one bit for each register in the program.
487 Used when performing GCSE to track which registers have been set since
488 the start of the basic block. */
489 static regset reg_set_bitmap;
491 /* For each block, a bitmap of registers set in the block.
492 This is used by expr_killed_p and compute_transp.
493 It is computed during hash table computation and not by compute_sets
494 as it includes registers added since the last pass (or between cprop and
495 gcse) and it's currently not easy to realloc sbitmap vectors. */
496 static sbitmap *reg_set_in_block;
498 /* Array, indexed by basic block number for a list of insns which modify
499 memory within that block. */
500 static rtx * modify_mem_list;
501 bitmap modify_mem_list_set;
503 /* This array parallels modify_mem_list, but is kept canonicalized. */
504 static rtx * canon_modify_mem_list;
505 bitmap canon_modify_mem_list_set;
506 /* Various variables for statistics gathering. */
508 /* Memory used in a pass.
509 This isn't intended to be absolutely precise. Its intent is only
510 to keep an eye on memory usage. */
511 static int bytes_used;
513 /* GCSE substitutions made. */
514 static int gcse_subst_count;
515 /* Number of copy instructions created. */
516 static int gcse_create_count;
517 /* Number of constants propagated. */
518 static int const_prop_count;
519 /* Number of copys propagated. */
520 static int copy_prop_count;
522 /* These variables are used by classic GCSE.
523 Normally they'd be defined a bit later, but `rd_gen' needs to
524 be declared sooner. */
526 /* Each block has a bitmap of each type.
527 The length of each blocks bitmap is:
529 max_cuid - for reaching definitions
530 n_exprs - for available expressions
532 Thus we view the bitmaps as 2 dimensional arrays. i.e.
533 rd_kill[block_num][cuid_num]
534 ae_kill[block_num][expr_num] */
536 /* For reaching defs */
537 static sbitmap *rd_kill, *rd_gen, *reaching_defs, *rd_out;
539 /* for available exprs */
540 static sbitmap *ae_kill, *ae_gen, *ae_in, *ae_out;
542 /* Objects of this type are passed around by the null-pointer check
543 removal routines. */
544 struct null_pointer_info
546 /* The basic block being processed. */
547 basic_block current_block;
548 /* The first register to be handled in this pass. */
549 unsigned int min_reg;
550 /* One greater than the last register to be handled in this pass. */
551 unsigned int max_reg;
552 sbitmap *nonnull_local;
553 sbitmap *nonnull_killed;
556 static void compute_can_copy PARAMS ((void));
557 static char *gmalloc PARAMS ((unsigned int));
558 static char *grealloc PARAMS ((char *, unsigned int));
559 static char *gcse_alloc PARAMS ((unsigned long));
560 static void alloc_gcse_mem PARAMS ((rtx));
561 static void free_gcse_mem PARAMS ((void));
562 static void alloc_reg_set_mem PARAMS ((int));
563 static void free_reg_set_mem PARAMS ((void));
564 static int get_bitmap_width PARAMS ((int, int, int));
565 static void record_one_set PARAMS ((int, rtx));
566 static void record_set_info PARAMS ((rtx, rtx, void *));
567 static void compute_sets PARAMS ((rtx));
568 static void hash_scan_insn PARAMS ((rtx, int, int));
569 static void hash_scan_set PARAMS ((rtx, rtx, int));
570 static void hash_scan_clobber PARAMS ((rtx, rtx));
571 static void hash_scan_call PARAMS ((rtx, rtx));
572 static int want_to_gcse_p PARAMS ((rtx));
573 static int oprs_unchanged_p PARAMS ((rtx, rtx, int));
574 static int oprs_anticipatable_p PARAMS ((rtx, rtx));
575 static int oprs_available_p PARAMS ((rtx, rtx));
576 static void insert_expr_in_table PARAMS ((rtx, enum machine_mode, rtx,
577 int, int));
578 static void insert_set_in_table PARAMS ((rtx, rtx));
579 static unsigned int hash_expr PARAMS ((rtx, enum machine_mode, int *, int));
580 static unsigned int hash_expr_1 PARAMS ((rtx, enum machine_mode, int *));
581 static unsigned int hash_string_1 PARAMS ((const char *));
582 static unsigned int hash_set PARAMS ((int, int));
583 static int expr_equiv_p PARAMS ((rtx, rtx));
584 static void record_last_reg_set_info PARAMS ((rtx, int));
585 static void record_last_mem_set_info PARAMS ((rtx));
586 static void record_last_set_info PARAMS ((rtx, rtx, void *));
587 static void compute_hash_table PARAMS ((int));
588 static void alloc_set_hash_table PARAMS ((int));
589 static void free_set_hash_table PARAMS ((void));
590 static void compute_set_hash_table PARAMS ((void));
591 static void alloc_expr_hash_table PARAMS ((unsigned int));
592 static void free_expr_hash_table PARAMS ((void));
593 static void compute_expr_hash_table PARAMS ((void));
594 static void dump_hash_table PARAMS ((FILE *, const char *, struct expr **,
595 int, int));
596 static struct expr *lookup_expr PARAMS ((rtx));
597 static struct expr *lookup_set PARAMS ((unsigned int, rtx));
598 static struct expr *next_set PARAMS ((unsigned int, struct expr *));
599 static void reset_opr_set_tables PARAMS ((void));
600 static int oprs_not_set_p PARAMS ((rtx, rtx));
601 static void mark_call PARAMS ((rtx));
602 static void mark_set PARAMS ((rtx, rtx));
603 static void mark_clobber PARAMS ((rtx, rtx));
604 static void mark_oprs_set PARAMS ((rtx));
605 static void alloc_cprop_mem PARAMS ((int, int));
606 static void free_cprop_mem PARAMS ((void));
607 static void compute_transp PARAMS ((rtx, int, sbitmap *, int));
608 static void compute_transpout PARAMS ((void));
609 static void compute_local_properties PARAMS ((sbitmap *, sbitmap *, sbitmap *,
610 int));
611 static void compute_cprop_data PARAMS ((void));
612 static void find_used_regs PARAMS ((rtx *, void *));
613 static int try_replace_reg PARAMS ((rtx, rtx, rtx));
614 static struct expr *find_avail_set PARAMS ((int, rtx));
615 static int cprop_jump PARAMS ((basic_block, rtx, rtx, rtx, rtx));
616 static void mems_conflict_for_gcse_p PARAMS ((rtx, rtx, void *));
617 static int load_killed_in_block_p PARAMS ((basic_block, int, rtx, int));
618 static void canon_list_insert PARAMS ((rtx, rtx, void *));
619 static int cprop_insn PARAMS ((rtx, int));
620 static int cprop PARAMS ((int));
621 static int one_cprop_pass PARAMS ((int, int));
622 static bool constprop_register PARAMS ((rtx, rtx, rtx, int));
623 static struct expr *find_bypass_set PARAMS ((int, int));
624 static int bypass_block PARAMS ((basic_block, rtx, rtx));
625 static int bypass_conditional_jumps PARAMS ((void));
626 static void alloc_pre_mem PARAMS ((int, int));
627 static void free_pre_mem PARAMS ((void));
628 static void compute_pre_data PARAMS ((void));
629 static int pre_expr_reaches_here_p PARAMS ((basic_block, struct expr *,
630 basic_block));
631 static void insert_insn_end_bb PARAMS ((struct expr *, basic_block, int));
632 static void pre_insert_copy_insn PARAMS ((struct expr *, rtx));
633 static void pre_insert_copies PARAMS ((void));
634 static int pre_delete PARAMS ((void));
635 static int pre_gcse PARAMS ((void));
636 static int one_pre_gcse_pass PARAMS ((int));
637 static void add_label_notes PARAMS ((rtx, rtx));
638 static void alloc_code_hoist_mem PARAMS ((int, int));
639 static void free_code_hoist_mem PARAMS ((void));
640 static void compute_code_hoist_vbeinout PARAMS ((void));
641 static void compute_code_hoist_data PARAMS ((void));
642 static int hoist_expr_reaches_here_p PARAMS ((basic_block, int, basic_block,
643 char *));
644 static void hoist_code PARAMS ((void));
645 static int one_code_hoisting_pass PARAMS ((void));
646 static void alloc_rd_mem PARAMS ((int, int));
647 static void free_rd_mem PARAMS ((void));
648 static void handle_rd_kill_set PARAMS ((rtx, int, basic_block));
649 static void compute_kill_rd PARAMS ((void));
650 static void compute_rd PARAMS ((void));
651 static void alloc_avail_expr_mem PARAMS ((int, int));
652 static void free_avail_expr_mem PARAMS ((void));
653 static void compute_ae_gen PARAMS ((void));
654 static int expr_killed_p PARAMS ((rtx, basic_block));
655 static void compute_ae_kill PARAMS ((sbitmap *, sbitmap *));
656 static int expr_reaches_here_p PARAMS ((struct occr *, struct expr *,
657 basic_block, int));
658 static rtx computing_insn PARAMS ((struct expr *, rtx));
659 static int def_reaches_here_p PARAMS ((rtx, rtx));
660 static int can_disregard_other_sets PARAMS ((struct reg_set **, rtx, int));
661 static int handle_avail_expr PARAMS ((rtx, struct expr *));
662 static int classic_gcse PARAMS ((void));
663 static int one_classic_gcse_pass PARAMS ((int));
664 static void invalidate_nonnull_info PARAMS ((rtx, rtx, void *));
665 static int delete_null_pointer_checks_1 PARAMS ((unsigned int *,
666 sbitmap *, sbitmap *,
667 struct null_pointer_info *));
668 static rtx process_insert_insn PARAMS ((struct expr *));
669 static int pre_edge_insert PARAMS ((struct edge_list *, struct expr **));
670 static int expr_reaches_here_p_work PARAMS ((struct occr *, struct expr *,
671 basic_block, int, char *));
672 static int pre_expr_reaches_here_p_work PARAMS ((basic_block, struct expr *,
673 basic_block, char *));
674 static struct ls_expr * ldst_entry PARAMS ((rtx));
675 static void free_ldst_entry PARAMS ((struct ls_expr *));
676 static void free_ldst_mems PARAMS ((void));
677 static void print_ldst_list PARAMS ((FILE *));
678 static struct ls_expr * find_rtx_in_ldst PARAMS ((rtx));
679 static int enumerate_ldsts PARAMS ((void));
680 static inline struct ls_expr * first_ls_expr PARAMS ((void));
681 static inline struct ls_expr * next_ls_expr PARAMS ((struct ls_expr *));
682 static int simple_mem PARAMS ((rtx));
683 static void invalidate_any_buried_refs PARAMS ((rtx));
684 static void compute_ld_motion_mems PARAMS ((void));
685 static void trim_ld_motion_mems PARAMS ((void));
686 static void update_ld_motion_stores PARAMS ((struct expr *));
687 static void reg_set_info PARAMS ((rtx, rtx, void *));
688 static int store_ops_ok PARAMS ((rtx, basic_block));
689 static void find_moveable_store PARAMS ((rtx));
690 static int compute_store_table PARAMS ((void));
691 static int load_kills_store PARAMS ((rtx, rtx));
692 static int find_loads PARAMS ((rtx, rtx));
693 static int store_killed_in_insn PARAMS ((rtx, rtx));
694 static int store_killed_after PARAMS ((rtx, rtx, basic_block));
695 static int store_killed_before PARAMS ((rtx, rtx, basic_block));
696 static void build_store_vectors PARAMS ((void));
697 static void insert_insn_start_bb PARAMS ((rtx, basic_block));
698 static int insert_store PARAMS ((struct ls_expr *, edge));
699 static void replace_store_insn PARAMS ((rtx, rtx, basic_block));
700 static void delete_store PARAMS ((struct ls_expr *,
701 basic_block));
702 static void free_store_memory PARAMS ((void));
703 static void store_motion PARAMS ((void));
704 static void free_insn_expr_list_list PARAMS ((rtx *));
705 static void clear_modify_mem_tables PARAMS ((void));
706 static void free_modify_mem_tables PARAMS ((void));
707 static rtx gcse_emit_move_after PARAMS ((rtx, rtx, rtx));
708 static bool do_local_cprop PARAMS ((rtx, rtx, int));
709 static void local_cprop_pass PARAMS ((int));
711 /* Entry point for global common subexpression elimination.
712 F is the first instruction in the function. */
715 gcse_main (f, file)
716 rtx f;
717 FILE *file;
719 int changed, pass;
720 /* Bytes used at start of pass. */
721 int initial_bytes_used;
722 /* Maximum number of bytes used by a pass. */
723 int max_pass_bytes;
724 /* Point to release obstack data from for each pass. */
725 char *gcse_obstack_bottom;
727 /* Insertion of instructions on edges can create new basic blocks; we
728 need the original basic block count so that we can properly deallocate
729 arrays sized on the number of basic blocks originally in the cfg. */
730 int orig_bb_count;
731 /* We do not construct an accurate cfg in functions which call
732 setjmp, so just punt to be safe. */
733 if (current_function_calls_setjmp)
734 return 0;
736 /* Assume that we do not need to run jump optimizations after gcse. */
737 run_jump_opt_after_gcse = 0;
739 /* For calling dump_foo fns from gdb. */
740 debug_stderr = stderr;
741 gcse_file = file;
743 /* Identify the basic block information for this function, including
744 successors and predecessors. */
745 max_gcse_regno = max_reg_num ();
747 if (file)
748 dump_flow_info (file);
750 orig_bb_count = n_basic_blocks;
751 /* Return if there's nothing to do. */
752 if (n_basic_blocks <= 1)
753 return 0;
755 /* Trying to perform global optimizations on flow graphs which have
756 a high connectivity will take a long time and is unlikely to be
757 particularly useful.
759 In normal circumstances a cfg should have about twice as many edges
760 as blocks. But we do not want to punish small functions which have
761 a couple switch statements. So we require a relatively large number
762 of basic blocks and the ratio of edges to blocks to be high. */
763 if (n_basic_blocks > 1000 && n_edges / n_basic_blocks >= 20)
765 if (warn_disabled_optimization)
766 warning ("GCSE disabled: %d > 1000 basic blocks and %d >= 20 edges/basic block",
767 n_basic_blocks, n_edges / n_basic_blocks);
768 return 0;
771 /* If allocating memory for the cprop bitmap would take up too much
772 storage it's better just to disable the optimization. */
773 if ((n_basic_blocks
774 * SBITMAP_SET_SIZE (max_gcse_regno)
775 * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
777 if (warn_disabled_optimization)
778 warning ("GCSE disabled: %d basic blocks and %d registers",
779 n_basic_blocks, max_gcse_regno);
781 return 0;
784 /* See what modes support reg/reg copy operations. */
785 if (! can_copy_init_p)
787 compute_can_copy ();
788 can_copy_init_p = 1;
791 gcc_obstack_init (&gcse_obstack);
792 bytes_used = 0;
794 /* We need alias. */
795 init_alias_analysis ();
796 /* Record where pseudo-registers are set. This data is kept accurate
797 during each pass. ??? We could also record hard-reg information here
798 [since it's unchanging], however it is currently done during hash table
799 computation.
801 It may be tempting to compute MEM set information here too, but MEM sets
802 will be subject to code motion one day and thus we need to compute
803 information about memory sets when we build the hash tables. */
805 alloc_reg_set_mem (max_gcse_regno);
806 compute_sets (f);
808 pass = 0;
809 initial_bytes_used = bytes_used;
810 max_pass_bytes = 0;
811 gcse_obstack_bottom = gcse_alloc (1);
812 changed = 1;
813 while (changed && pass < MAX_GCSE_PASSES)
815 changed = 0;
816 if (file)
817 fprintf (file, "GCSE pass %d\n\n", pass + 1);
819 /* Initialize bytes_used to the space for the pred/succ lists,
820 and the reg_set_table data. */
821 bytes_used = initial_bytes_used;
823 /* Each pass may create new registers, so recalculate each time. */
824 max_gcse_regno = max_reg_num ();
826 alloc_gcse_mem (f);
828 /* Don't allow constant propagation to modify jumps
829 during this pass. */
830 changed = one_cprop_pass (pass + 1, 0);
832 if (optimize_size)
833 changed |= one_classic_gcse_pass (pass + 1);
834 else
836 changed |= one_pre_gcse_pass (pass + 1);
837 /* We may have just created new basic blocks. Release and
838 recompute various things which are sized on the number of
839 basic blocks. */
840 if (changed)
842 free_modify_mem_tables ();
843 modify_mem_list
844 = (rtx *) gmalloc (last_basic_block * sizeof (rtx));
845 canon_modify_mem_list
846 = (rtx *) gmalloc (last_basic_block * sizeof (rtx));
847 memset ((char *) modify_mem_list, 0, last_basic_block * sizeof (rtx));
848 memset ((char *) canon_modify_mem_list, 0, last_basic_block * sizeof (rtx));
849 orig_bb_count = n_basic_blocks;
851 free_reg_set_mem ();
852 alloc_reg_set_mem (max_reg_num ());
853 compute_sets (f);
854 run_jump_opt_after_gcse = 1;
857 if (max_pass_bytes < bytes_used)
858 max_pass_bytes = bytes_used;
860 /* Free up memory, then reallocate for code hoisting. We can
861 not re-use the existing allocated memory because the tables
862 will not have info for the insns or registers created by
863 partial redundancy elimination. */
864 free_gcse_mem ();
866 /* It does not make sense to run code hoisting unless we optimizing
867 for code size -- it rarely makes programs faster, and can make
868 them bigger if we did partial redundancy elimination (when optimizing
869 for space, we use a classic gcse algorithm instead of partial
870 redundancy algorithms). */
871 if (optimize_size)
873 max_gcse_regno = max_reg_num ();
874 alloc_gcse_mem (f);
875 changed |= one_code_hoisting_pass ();
876 free_gcse_mem ();
878 if (max_pass_bytes < bytes_used)
879 max_pass_bytes = bytes_used;
882 if (file)
884 fprintf (file, "\n");
885 fflush (file);
888 obstack_free (&gcse_obstack, gcse_obstack_bottom);
889 pass++;
892 /* Do one last pass of copy propagation, including cprop into
893 conditional jumps. */
895 max_gcse_regno = max_reg_num ();
896 alloc_gcse_mem (f);
897 /* This time, go ahead and allow cprop to alter jumps. */
898 one_cprop_pass (pass + 1, 1);
899 free_gcse_mem ();
901 if (file)
903 fprintf (file, "GCSE of %s: %d basic blocks, ",
904 current_function_name, n_basic_blocks);
905 fprintf (file, "%d pass%s, %d bytes\n\n",
906 pass, pass > 1 ? "es" : "", max_pass_bytes);
909 obstack_free (&gcse_obstack, NULL);
910 free_reg_set_mem ();
911 /* We are finished with alias. */
912 end_alias_analysis ();
913 allocate_reg_info (max_reg_num (), FALSE, FALSE);
915 /* Store motion disabled until it is fixed. */
916 if (0 && !optimize_size && flag_gcse_sm)
917 store_motion ();
918 /* Record where pseudo-registers are set. */
919 return run_jump_opt_after_gcse;
922 /* Misc. utilities. */
924 /* Compute which modes support reg/reg copy operations. */
926 static void
927 compute_can_copy ()
929 int i;
930 #ifndef AVOID_CCMODE_COPIES
931 rtx reg, insn;
932 #endif
933 memset (can_copy_p, 0, NUM_MACHINE_MODES);
935 start_sequence ();
936 for (i = 0; i < NUM_MACHINE_MODES; i++)
937 if (GET_MODE_CLASS (i) == MODE_CC)
939 #ifdef AVOID_CCMODE_COPIES
940 can_copy_p[i] = 0;
941 #else
942 reg = gen_rtx_REG ((enum machine_mode) i, LAST_VIRTUAL_REGISTER + 1);
943 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, reg));
944 if (recog (PATTERN (insn), insn, NULL) >= 0)
945 can_copy_p[i] = 1;
946 #endif
948 else
949 can_copy_p[i] = 1;
951 end_sequence ();
954 /* Cover function to xmalloc to record bytes allocated. */
956 static char *
957 gmalloc (size)
958 unsigned int size;
960 bytes_used += size;
961 return xmalloc (size);
964 /* Cover function to xrealloc.
965 We don't record the additional size since we don't know it.
966 It won't affect memory usage stats much anyway. */
968 static char *
969 grealloc (ptr, size)
970 char *ptr;
971 unsigned int size;
973 return xrealloc (ptr, size);
976 /* Cover function to obstack_alloc.
977 We don't need to record the bytes allocated here since
978 obstack_chunk_alloc is set to gmalloc. */
980 static char *
981 gcse_alloc (size)
982 unsigned long size;
984 return (char *) obstack_alloc (&gcse_obstack, size);
987 /* Allocate memory for the cuid mapping array,
988 and reg/memory set tracking tables.
990 This is called at the start of each pass. */
992 static void
993 alloc_gcse_mem (f)
994 rtx f;
996 int i, n;
997 rtx insn;
999 /* Find the largest UID and create a mapping from UIDs to CUIDs.
1000 CUIDs are like UIDs except they increase monotonically, have no gaps,
1001 and only apply to real insns. */
1003 max_uid = get_max_uid ();
1004 n = (max_uid + 1) * sizeof (int);
1005 uid_cuid = (int *) gmalloc (n);
1006 memset ((char *) uid_cuid, 0, n);
1007 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
1009 if (INSN_P (insn))
1010 uid_cuid[INSN_UID (insn)] = i++;
1011 else
1012 uid_cuid[INSN_UID (insn)] = i;
1015 /* Create a table mapping cuids to insns. */
1017 max_cuid = i;
1018 n = (max_cuid + 1) * sizeof (rtx);
1019 cuid_insn = (rtx *) gmalloc (n);
1020 memset ((char *) cuid_insn, 0, n);
1021 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
1022 if (INSN_P (insn))
1023 CUID_INSN (i++) = insn;
1025 /* Allocate vars to track sets of regs. */
1026 reg_set_bitmap = BITMAP_XMALLOC ();
1028 /* Allocate vars to track sets of regs, memory per block. */
1029 reg_set_in_block = (sbitmap *) sbitmap_vector_alloc (last_basic_block,
1030 max_gcse_regno);
1031 /* Allocate array to keep a list of insns which modify memory in each
1032 basic block. */
1033 modify_mem_list = (rtx *) gmalloc (last_basic_block * sizeof (rtx));
1034 canon_modify_mem_list = (rtx *) gmalloc (last_basic_block * sizeof (rtx));
1035 memset ((char *) modify_mem_list, 0, last_basic_block * sizeof (rtx));
1036 memset ((char *) canon_modify_mem_list, 0, last_basic_block * sizeof (rtx));
1037 modify_mem_list_set = BITMAP_XMALLOC ();
1038 canon_modify_mem_list_set = BITMAP_XMALLOC ();
1041 /* Free memory allocated by alloc_gcse_mem. */
1043 static void
1044 free_gcse_mem ()
1046 free (uid_cuid);
1047 free (cuid_insn);
1049 BITMAP_XFREE (reg_set_bitmap);
1051 sbitmap_vector_free (reg_set_in_block);
1052 free_modify_mem_tables ();
1053 BITMAP_XFREE (modify_mem_list_set);
1054 BITMAP_XFREE (canon_modify_mem_list_set);
1057 /* Many of the global optimization algorithms work by solving dataflow
1058 equations for various expressions. Initially, some local value is
1059 computed for each expression in each block. Then, the values across the
1060 various blocks are combined (by following flow graph edges) to arrive at
1061 global values. Conceptually, each set of equations is independent. We
1062 may therefore solve all the equations in parallel, solve them one at a
1063 time, or pick any intermediate approach.
1065 When you're going to need N two-dimensional bitmaps, each X (say, the
1066 number of blocks) by Y (say, the number of expressions), call this
1067 function. It's not important what X and Y represent; only that Y
1068 correspond to the things that can be done in parallel. This function will
1069 return an appropriate chunking factor C; you should solve C sets of
1070 equations in parallel. By going through this function, we can easily
1071 trade space against time; by solving fewer equations in parallel we use
1072 less space. */
1074 static int
1075 get_bitmap_width (n, x, y)
1076 int n;
1077 int x;
1078 int y;
1080 /* It's not really worth figuring out *exactly* how much memory will
1081 be used by a particular choice. The important thing is to get
1082 something approximately right. */
1083 size_t max_bitmap_memory = 10 * 1024 * 1024;
1085 /* The number of bytes we'd use for a single column of minimum
1086 width. */
1087 size_t column_size = n * x * sizeof (SBITMAP_ELT_TYPE);
1089 /* Often, it's reasonable just to solve all the equations in
1090 parallel. */
1091 if (column_size * SBITMAP_SET_SIZE (y) <= max_bitmap_memory)
1092 return y;
1094 /* Otherwise, pick the largest width we can, without going over the
1095 limit. */
1096 return SBITMAP_ELT_BITS * ((max_bitmap_memory + column_size - 1)
1097 / column_size);
1100 /* Compute the local properties of each recorded expression.
1102 Local properties are those that are defined by the block, irrespective of
1103 other blocks.
1105 An expression is transparent in a block if its operands are not modified
1106 in the block.
1108 An expression is computed (locally available) in a block if it is computed
1109 at least once and expression would contain the same value if the
1110 computation was moved to the end of the block.
1112 An expression is locally anticipatable in a block if it is computed at
1113 least once and expression would contain the same value if the computation
1114 was moved to the beginning of the block.
1116 We call this routine for cprop, pre and code hoisting. They all compute
1117 basically the same information and thus can easily share this code.
1119 TRANSP, COMP, and ANTLOC are destination sbitmaps for recording local
1120 properties. If NULL, then it is not necessary to compute or record that
1121 particular property.
1123 SETP controls which hash table to look at. If zero, this routine looks at
1124 the expr hash table; if nonzero this routine looks at the set hash table.
1125 Additionally, TRANSP is computed as ~TRANSP, since this is really cprop's
1126 ABSALTERED. */
1128 static void
1129 compute_local_properties (transp, comp, antloc, setp)
1130 sbitmap *transp;
1131 sbitmap *comp;
1132 sbitmap *antloc;
1133 int setp;
1135 unsigned int i, hash_table_size;
1136 struct expr **hash_table;
1138 /* Initialize any bitmaps that were passed in. */
1139 if (transp)
1141 if (setp)
1142 sbitmap_vector_zero (transp, last_basic_block);
1143 else
1144 sbitmap_vector_ones (transp, last_basic_block);
1147 if (comp)
1148 sbitmap_vector_zero (comp, last_basic_block);
1149 if (antloc)
1150 sbitmap_vector_zero (antloc, last_basic_block);
1152 /* We use the same code for cprop, pre and hoisting. For cprop
1153 we care about the set hash table, for pre and hoisting we
1154 care about the expr hash table. */
1155 hash_table_size = setp ? set_hash_table_size : expr_hash_table_size;
1156 hash_table = setp ? set_hash_table : expr_hash_table;
1158 for (i = 0; i < hash_table_size; i++)
1160 struct expr *expr;
1162 for (expr = hash_table[i]; expr != NULL; expr = expr->next_same_hash)
1164 int indx = expr->bitmap_index;
1165 struct occr *occr;
1167 /* The expression is transparent in this block if it is not killed.
1168 We start by assuming all are transparent [none are killed], and
1169 then reset the bits for those that are. */
1170 if (transp)
1171 compute_transp (expr->expr, indx, transp, setp);
1173 /* The occurrences recorded in antic_occr are exactly those that
1174 we want to set to non-zero in ANTLOC. */
1175 if (antloc)
1176 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
1178 SET_BIT (antloc[BLOCK_NUM (occr->insn)], indx);
1180 /* While we're scanning the table, this is a good place to
1181 initialize this. */
1182 occr->deleted_p = 0;
1185 /* The occurrences recorded in avail_occr are exactly those that
1186 we want to set to non-zero in COMP. */
1187 if (comp)
1188 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
1190 SET_BIT (comp[BLOCK_NUM (occr->insn)], indx);
1192 /* While we're scanning the table, this is a good place to
1193 initialize this. */
1194 occr->copied_p = 0;
1197 /* While we're scanning the table, this is a good place to
1198 initialize this. */
1199 expr->reaching_reg = 0;
1204 /* Register set information.
1206 `reg_set_table' records where each register is set or otherwise
1207 modified. */
1209 static struct obstack reg_set_obstack;
1211 static void
1212 alloc_reg_set_mem (n_regs)
1213 int n_regs;
1215 unsigned int n;
1217 reg_set_table_size = n_regs + REG_SET_TABLE_SLOP;
1218 n = reg_set_table_size * sizeof (struct reg_set *);
1219 reg_set_table = (struct reg_set **) gmalloc (n);
1220 memset ((char *) reg_set_table, 0, n);
1222 gcc_obstack_init (&reg_set_obstack);
1225 static void
1226 free_reg_set_mem ()
1228 free (reg_set_table);
1229 obstack_free (&reg_set_obstack, NULL);
1232 /* Record REGNO in the reg_set table. */
1234 static void
1235 record_one_set (regno, insn)
1236 int regno;
1237 rtx insn;
1239 /* Allocate a new reg_set element and link it onto the list. */
1240 struct reg_set *new_reg_info;
1242 /* If the table isn't big enough, enlarge it. */
1243 if (regno >= reg_set_table_size)
1245 int new_size = regno + REG_SET_TABLE_SLOP;
1247 reg_set_table
1248 = (struct reg_set **) grealloc ((char *) reg_set_table,
1249 new_size * sizeof (struct reg_set *));
1250 memset ((char *) (reg_set_table + reg_set_table_size), 0,
1251 (new_size - reg_set_table_size) * sizeof (struct reg_set *));
1252 reg_set_table_size = new_size;
1255 new_reg_info = (struct reg_set *) obstack_alloc (&reg_set_obstack,
1256 sizeof (struct reg_set));
1257 bytes_used += sizeof (struct reg_set);
1258 new_reg_info->insn = insn;
1259 new_reg_info->next = reg_set_table[regno];
1260 reg_set_table[regno] = new_reg_info;
1263 /* Called from compute_sets via note_stores to handle one SET or CLOBBER in
1264 an insn. The DATA is really the instruction in which the SET is
1265 occurring. */
1267 static void
1268 record_set_info (dest, setter, data)
1269 rtx dest, setter ATTRIBUTE_UNUSED;
1270 void *data;
1272 rtx record_set_insn = (rtx) data;
1274 if (GET_CODE (dest) == REG && REGNO (dest) >= FIRST_PSEUDO_REGISTER)
1275 record_one_set (REGNO (dest), record_set_insn);
1278 /* Scan the function and record each set of each pseudo-register.
1280 This is called once, at the start of the gcse pass. See the comments for
1281 `reg_set_table' for further documenation. */
1283 static void
1284 compute_sets (f)
1285 rtx f;
1287 rtx insn;
1289 for (insn = f; insn != 0; insn = NEXT_INSN (insn))
1290 if (INSN_P (insn))
1291 note_stores (PATTERN (insn), record_set_info, insn);
1294 /* Hash table support. */
1296 /* For each register, the cuid of the first/last insn in the block
1297 that set it, or -1 if not set. */
1298 #define NEVER_SET -1
1300 struct reg_avail_info
1302 basic_block last_bb;
1303 int first_set;
1304 int last_set;
1307 static struct reg_avail_info *reg_avail_info;
1308 static basic_block current_bb;
1311 /* See whether X, the source of a set, is something we want to consider for
1312 GCSE. */
1314 static GTY(()) rtx test_insn;
1315 static int
1316 want_to_gcse_p (x)
1317 rtx x;
1319 int num_clobbers = 0;
1320 int icode;
1322 switch (GET_CODE (x))
1324 case REG:
1325 case SUBREG:
1326 case CONST_INT:
1327 case CONST_DOUBLE:
1328 case CONST_VECTOR:
1329 case CALL:
1330 return 0;
1332 default:
1333 break;
1336 /* If this is a valid operand, we are OK. If it's VOIDmode, we aren't. */
1337 if (general_operand (x, GET_MODE (x)))
1338 return 1;
1339 else if (GET_MODE (x) == VOIDmode)
1340 return 0;
1342 /* Otherwise, check if we can make a valid insn from it. First initialize
1343 our test insn if we haven't already. */
1344 if (test_insn == 0)
1346 test_insn
1347 = make_insn_raw (gen_rtx_SET (VOIDmode,
1348 gen_rtx_REG (word_mode,
1349 FIRST_PSEUDO_REGISTER * 2),
1350 const0_rtx));
1351 NEXT_INSN (test_insn) = PREV_INSN (test_insn) = 0;
1354 /* Now make an insn like the one we would make when GCSE'ing and see if
1355 valid. */
1356 PUT_MODE (SET_DEST (PATTERN (test_insn)), GET_MODE (x));
1357 SET_SRC (PATTERN (test_insn)) = x;
1358 return ((icode = recog (PATTERN (test_insn), test_insn, &num_clobbers)) >= 0
1359 && (num_clobbers == 0 || ! added_clobbers_hard_reg_p (icode)));
1362 /* Return non-zero if the operands of expression X are unchanged from the
1363 start of INSN's basic block up to but not including INSN (if AVAIL_P == 0),
1364 or from INSN to the end of INSN's basic block (if AVAIL_P != 0). */
1366 static int
1367 oprs_unchanged_p (x, insn, avail_p)
1368 rtx x, insn;
1369 int avail_p;
1371 int i, j;
1372 enum rtx_code code;
1373 const char *fmt;
1375 if (x == 0)
1376 return 1;
1378 code = GET_CODE (x);
1379 switch (code)
1381 case REG:
1383 struct reg_avail_info *info = &reg_avail_info[REGNO (x)];
1385 if (info->last_bb != current_bb)
1386 return 1;
1387 if (avail_p)
1388 return info->last_set < INSN_CUID (insn);
1389 else
1390 return info->first_set >= INSN_CUID (insn);
1393 case MEM:
1394 if (load_killed_in_block_p (current_bb, INSN_CUID (insn),
1395 x, avail_p))
1396 return 0;
1397 else
1398 return oprs_unchanged_p (XEXP (x, 0), insn, avail_p);
1400 case PRE_DEC:
1401 case PRE_INC:
1402 case POST_DEC:
1403 case POST_INC:
1404 case PRE_MODIFY:
1405 case POST_MODIFY:
1406 return 0;
1408 case PC:
1409 case CC0: /*FIXME*/
1410 case CONST:
1411 case CONST_INT:
1412 case CONST_DOUBLE:
1413 case CONST_VECTOR:
1414 case SYMBOL_REF:
1415 case LABEL_REF:
1416 case ADDR_VEC:
1417 case ADDR_DIFF_VEC:
1418 return 1;
1420 default:
1421 break;
1424 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
1426 if (fmt[i] == 'e')
1428 /* If we are about to do the last recursive call needed at this
1429 level, change it into iteration. This function is called enough
1430 to be worth it. */
1431 if (i == 0)
1432 return oprs_unchanged_p (XEXP (x, i), insn, avail_p);
1434 else if (! oprs_unchanged_p (XEXP (x, i), insn, avail_p))
1435 return 0;
1437 else if (fmt[i] == 'E')
1438 for (j = 0; j < XVECLEN (x, i); j++)
1439 if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, avail_p))
1440 return 0;
1443 return 1;
1446 /* Used for communication between mems_conflict_for_gcse_p and
1447 load_killed_in_block_p. Nonzero if mems_conflict_for_gcse_p finds a
1448 conflict between two memory references. */
1449 static int gcse_mems_conflict_p;
1451 /* Used for communication between mems_conflict_for_gcse_p and
1452 load_killed_in_block_p. A memory reference for a load instruction,
1453 mems_conflict_for_gcse_p will see if a memory store conflicts with
1454 this memory load. */
1455 static rtx gcse_mem_operand;
1457 /* DEST is the output of an instruction. If it is a memory reference, and
1458 possibly conflicts with the load found in gcse_mem_operand, then set
1459 gcse_mems_conflict_p to a nonzero value. */
1461 static void
1462 mems_conflict_for_gcse_p (dest, setter, data)
1463 rtx dest, setter ATTRIBUTE_UNUSED;
1464 void *data ATTRIBUTE_UNUSED;
1466 while (GET_CODE (dest) == SUBREG
1467 || GET_CODE (dest) == ZERO_EXTRACT
1468 || GET_CODE (dest) == SIGN_EXTRACT
1469 || GET_CODE (dest) == STRICT_LOW_PART)
1470 dest = XEXP (dest, 0);
1472 /* If DEST is not a MEM, then it will not conflict with the load. Note
1473 that function calls are assumed to clobber memory, but are handled
1474 elsewhere. */
1475 if (GET_CODE (dest) != MEM)
1476 return;
1478 /* If we are setting a MEM in our list of specially recognized MEMs,
1479 don't mark as killed this time. */
1481 if (dest == gcse_mem_operand && pre_ldst_mems != NULL)
1483 if (!find_rtx_in_ldst (dest))
1484 gcse_mems_conflict_p = 1;
1485 return;
1488 if (true_dependence (dest, GET_MODE (dest), gcse_mem_operand,
1489 rtx_addr_varies_p))
1490 gcse_mems_conflict_p = 1;
1493 /* Return nonzero if the expression in X (a memory reference) is killed
1494 in block BB before or after the insn with the CUID in UID_LIMIT.
1495 AVAIL_P is nonzero for kills after UID_LIMIT, and zero for kills
1496 before UID_LIMIT.
1498 To check the entire block, set UID_LIMIT to max_uid + 1 and
1499 AVAIL_P to 0. */
1501 static int
1502 load_killed_in_block_p (bb, uid_limit, x, avail_p)
1503 basic_block bb;
1504 int uid_limit;
1505 rtx x;
1506 int avail_p;
1508 rtx list_entry = modify_mem_list[bb->index];
1509 while (list_entry)
1511 rtx setter;
1512 /* Ignore entries in the list that do not apply. */
1513 if ((avail_p
1514 && INSN_CUID (XEXP (list_entry, 0)) < uid_limit)
1515 || (! avail_p
1516 && INSN_CUID (XEXP (list_entry, 0)) > uid_limit))
1518 list_entry = XEXP (list_entry, 1);
1519 continue;
1522 setter = XEXP (list_entry, 0);
1524 /* If SETTER is a call everything is clobbered. Note that calls
1525 to pure functions are never put on the list, so we need not
1526 worry about them. */
1527 if (GET_CODE (setter) == CALL_INSN)
1528 return 1;
1530 /* SETTER must be an INSN of some kind that sets memory. Call
1531 note_stores to examine each hunk of memory that is modified.
1533 The note_stores interface is pretty limited, so we have to
1534 communicate via global variables. Yuk. */
1535 gcse_mem_operand = x;
1536 gcse_mems_conflict_p = 0;
1537 note_stores (PATTERN (setter), mems_conflict_for_gcse_p, NULL);
1538 if (gcse_mems_conflict_p)
1539 return 1;
1540 list_entry = XEXP (list_entry, 1);
1542 return 0;
1545 /* Return non-zero if the operands of expression X are unchanged from
1546 the start of INSN's basic block up to but not including INSN. */
1548 static int
1549 oprs_anticipatable_p (x, insn)
1550 rtx x, insn;
1552 return oprs_unchanged_p (x, insn, 0);
1555 /* Return non-zero if the operands of expression X are unchanged from
1556 INSN to the end of INSN's basic block. */
1558 static int
1559 oprs_available_p (x, insn)
1560 rtx x, insn;
1562 return oprs_unchanged_p (x, insn, 1);
1565 /* Hash expression X.
1567 MODE is only used if X is a CONST_INT. DO_NOT_RECORD_P is a boolean
1568 indicating if a volatile operand is found or if the expression contains
1569 something we don't want to insert in the table.
1571 ??? One might want to merge this with canon_hash. Later. */
1573 static unsigned int
1574 hash_expr (x, mode, do_not_record_p, hash_table_size)
1575 rtx x;
1576 enum machine_mode mode;
1577 int *do_not_record_p;
1578 int hash_table_size;
1580 unsigned int hash;
1582 *do_not_record_p = 0;
1584 hash = hash_expr_1 (x, mode, do_not_record_p);
1585 return hash % hash_table_size;
1588 /* Hash a string. Just add its bytes up. */
1590 static inline unsigned
1591 hash_string_1 (ps)
1592 const char *ps;
1594 unsigned hash = 0;
1595 const unsigned char *p = (const unsigned char *) ps;
1597 if (p)
1598 while (*p)
1599 hash += *p++;
1601 return hash;
1604 /* Subroutine of hash_expr to do the actual work. */
1606 static unsigned int
1607 hash_expr_1 (x, mode, do_not_record_p)
1608 rtx x;
1609 enum machine_mode mode;
1610 int *do_not_record_p;
1612 int i, j;
1613 unsigned hash = 0;
1614 enum rtx_code code;
1615 const char *fmt;
1617 /* Used to turn recursion into iteration. We can't rely on GCC's
1618 tail-recursion eliminatio since we need to keep accumulating values
1619 in HASH. */
1621 if (x == 0)
1622 return hash;
1624 repeat:
1625 code = GET_CODE (x);
1626 switch (code)
1628 case REG:
1629 hash += ((unsigned int) REG << 7) + REGNO (x);
1630 return hash;
1632 case CONST_INT:
1633 hash += (((unsigned int) CONST_INT << 7) + (unsigned int) mode
1634 + (unsigned int) INTVAL (x));
1635 return hash;
1637 case CONST_DOUBLE:
1638 /* This is like the general case, except that it only counts
1639 the integers representing the constant. */
1640 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1641 if (GET_MODE (x) != VOIDmode)
1642 for (i = 2; i < GET_RTX_LENGTH (CONST_DOUBLE); i++)
1643 hash += (unsigned int) XWINT (x, i);
1644 else
1645 hash += ((unsigned int) CONST_DOUBLE_LOW (x)
1646 + (unsigned int) CONST_DOUBLE_HIGH (x));
1647 return hash;
1649 case CONST_VECTOR:
1651 int units;
1652 rtx elt;
1654 units = CONST_VECTOR_NUNITS (x);
1656 for (i = 0; i < units; ++i)
1658 elt = CONST_VECTOR_ELT (x, i);
1659 hash += hash_expr_1 (elt, GET_MODE (elt), do_not_record_p);
1662 return hash;
1665 /* Assume there is only one rtx object for any given label. */
1666 case LABEL_REF:
1667 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1668 differences and differences between each stage's debugging dumps. */
1669 hash += (((unsigned int) LABEL_REF << 7)
1670 + CODE_LABEL_NUMBER (XEXP (x, 0)));
1671 return hash;
1673 case SYMBOL_REF:
1675 /* Don't hash on the symbol's address to avoid bootstrap differences.
1676 Different hash values may cause expressions to be recorded in
1677 different orders and thus different registers to be used in the
1678 final assembler. This also avoids differences in the dump files
1679 between various stages. */
1680 unsigned int h = 0;
1681 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1683 while (*p)
1684 h += (h << 7) + *p++; /* ??? revisit */
1686 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1687 return hash;
1690 case MEM:
1691 if (MEM_VOLATILE_P (x))
1693 *do_not_record_p = 1;
1694 return 0;
1697 hash += (unsigned int) MEM;
1698 /* We used alias set for hashing, but this is not good, since the alias
1699 set may differ in -fprofile-arcs and -fbranch-probabilities compilation
1700 causing the profiles to fail to match. */
1701 x = XEXP (x, 0);
1702 goto repeat;
1704 case PRE_DEC:
1705 case PRE_INC:
1706 case POST_DEC:
1707 case POST_INC:
1708 case PC:
1709 case CC0:
1710 case CALL:
1711 case UNSPEC_VOLATILE:
1712 *do_not_record_p = 1;
1713 return 0;
1715 case ASM_OPERANDS:
1716 if (MEM_VOLATILE_P (x))
1718 *do_not_record_p = 1;
1719 return 0;
1721 else
1723 /* We don't want to take the filename and line into account. */
1724 hash += (unsigned) code + (unsigned) GET_MODE (x)
1725 + hash_string_1 (ASM_OPERANDS_TEMPLATE (x))
1726 + hash_string_1 (ASM_OPERANDS_OUTPUT_CONSTRAINT (x))
1727 + (unsigned) ASM_OPERANDS_OUTPUT_IDX (x);
1729 if (ASM_OPERANDS_INPUT_LENGTH (x))
1731 for (i = 1; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
1733 hash += (hash_expr_1 (ASM_OPERANDS_INPUT (x, i),
1734 GET_MODE (ASM_OPERANDS_INPUT (x, i)),
1735 do_not_record_p)
1736 + hash_string_1 (ASM_OPERANDS_INPUT_CONSTRAINT
1737 (x, i)));
1740 hash += hash_string_1 (ASM_OPERANDS_INPUT_CONSTRAINT (x, 0));
1741 x = ASM_OPERANDS_INPUT (x, 0);
1742 mode = GET_MODE (x);
1743 goto repeat;
1745 return hash;
1748 default:
1749 break;
1752 hash += (unsigned) code + (unsigned) GET_MODE (x);
1753 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
1755 if (fmt[i] == 'e')
1757 /* If we are about to do the last recursive call
1758 needed at this level, change it into iteration.
1759 This function is called enough to be worth it. */
1760 if (i == 0)
1762 x = XEXP (x, i);
1763 goto repeat;
1766 hash += hash_expr_1 (XEXP (x, i), 0, do_not_record_p);
1767 if (*do_not_record_p)
1768 return 0;
1771 else if (fmt[i] == 'E')
1772 for (j = 0; j < XVECLEN (x, i); j++)
1774 hash += hash_expr_1 (XVECEXP (x, i, j), 0, do_not_record_p);
1775 if (*do_not_record_p)
1776 return 0;
1779 else if (fmt[i] == 's')
1780 hash += hash_string_1 (XSTR (x, i));
1781 else if (fmt[i] == 'i')
1782 hash += (unsigned int) XINT (x, i);
1783 else
1784 abort ();
1787 return hash;
1790 /* Hash a set of register REGNO.
1792 Sets are hashed on the register that is set. This simplifies the PRE copy
1793 propagation code.
1795 ??? May need to make things more elaborate. Later, as necessary. */
1797 static unsigned int
1798 hash_set (regno, hash_table_size)
1799 int regno;
1800 int hash_table_size;
1802 unsigned int hash;
1804 hash = regno;
1805 return hash % hash_table_size;
1808 /* Return non-zero if exp1 is equivalent to exp2.
1809 ??? Borrowed from cse.c. Might want to remerge with cse.c. Later. */
1811 static int
1812 expr_equiv_p (x, y)
1813 rtx x, y;
1815 int i, j;
1816 enum rtx_code code;
1817 const char *fmt;
1819 if (x == y)
1820 return 1;
1822 if (x == 0 || y == 0)
1823 return x == y;
1825 code = GET_CODE (x);
1826 if (code != GET_CODE (y))
1827 return 0;
1829 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
1830 if (GET_MODE (x) != GET_MODE (y))
1831 return 0;
1833 switch (code)
1835 case PC:
1836 case CC0:
1837 return x == y;
1839 case CONST_INT:
1840 return INTVAL (x) == INTVAL (y);
1842 case LABEL_REF:
1843 return XEXP (x, 0) == XEXP (y, 0);
1845 case SYMBOL_REF:
1846 return XSTR (x, 0) == XSTR (y, 0);
1848 case REG:
1849 return REGNO (x) == REGNO (y);
1851 case MEM:
1852 /* Can't merge two expressions in different alias sets, since we can
1853 decide that the expression is transparent in a block when it isn't,
1854 due to it being set with the different alias set. */
1855 if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
1856 return 0;
1857 break;
1859 /* For commutative operations, check both orders. */
1860 case PLUS:
1861 case MULT:
1862 case AND:
1863 case IOR:
1864 case XOR:
1865 case NE:
1866 case EQ:
1867 return ((expr_equiv_p (XEXP (x, 0), XEXP (y, 0))
1868 && expr_equiv_p (XEXP (x, 1), XEXP (y, 1)))
1869 || (expr_equiv_p (XEXP (x, 0), XEXP (y, 1))
1870 && expr_equiv_p (XEXP (x, 1), XEXP (y, 0))));
1872 case ASM_OPERANDS:
1873 /* We don't use the generic code below because we want to
1874 disregard filename and line numbers. */
1876 /* A volatile asm isn't equivalent to any other. */
1877 if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
1878 return 0;
1880 if (GET_MODE (x) != GET_MODE (y)
1881 || strcmp (ASM_OPERANDS_TEMPLATE (x), ASM_OPERANDS_TEMPLATE (y))
1882 || strcmp (ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
1883 ASM_OPERANDS_OUTPUT_CONSTRAINT (y))
1884 || ASM_OPERANDS_OUTPUT_IDX (x) != ASM_OPERANDS_OUTPUT_IDX (y)
1885 || ASM_OPERANDS_INPUT_LENGTH (x) != ASM_OPERANDS_INPUT_LENGTH (y))
1886 return 0;
1888 if (ASM_OPERANDS_INPUT_LENGTH (x))
1890 for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; i--)
1891 if (! expr_equiv_p (ASM_OPERANDS_INPUT (x, i),
1892 ASM_OPERANDS_INPUT (y, i))
1893 || strcmp (ASM_OPERANDS_INPUT_CONSTRAINT (x, i),
1894 ASM_OPERANDS_INPUT_CONSTRAINT (y, i)))
1895 return 0;
1898 return 1;
1900 default:
1901 break;
1904 /* Compare the elements. If any pair of corresponding elements
1905 fail to match, return 0 for the whole thing. */
1907 fmt = GET_RTX_FORMAT (code);
1908 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1910 switch (fmt[i])
1912 case 'e':
1913 if (! expr_equiv_p (XEXP (x, i), XEXP (y, i)))
1914 return 0;
1915 break;
1917 case 'E':
1918 if (XVECLEN (x, i) != XVECLEN (y, i))
1919 return 0;
1920 for (j = 0; j < XVECLEN (x, i); j++)
1921 if (! expr_equiv_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
1922 return 0;
1923 break;
1925 case 's':
1926 if (strcmp (XSTR (x, i), XSTR (y, i)))
1927 return 0;
1928 break;
1930 case 'i':
1931 if (XINT (x, i) != XINT (y, i))
1932 return 0;
1933 break;
1935 case 'w':
1936 if (XWINT (x, i) != XWINT (y, i))
1937 return 0;
1938 break;
1940 case '0':
1941 break;
1943 default:
1944 abort ();
1948 return 1;
1951 /* Insert expression X in INSN in the hash table.
1952 If it is already present, record it as the last occurrence in INSN's
1953 basic block.
1955 MODE is the mode of the value X is being stored into.
1956 It is only used if X is a CONST_INT.
1958 ANTIC_P is non-zero if X is an anticipatable expression.
1959 AVAIL_P is non-zero if X is an available expression. */
1961 static void
1962 insert_expr_in_table (x, mode, insn, antic_p, avail_p)
1963 rtx x;
1964 enum machine_mode mode;
1965 rtx insn;
1966 int antic_p, avail_p;
1968 int found, do_not_record_p;
1969 unsigned int hash;
1970 struct expr *cur_expr, *last_expr = NULL;
1971 struct occr *antic_occr, *avail_occr;
1972 struct occr *last_occr = NULL;
1974 hash = hash_expr (x, mode, &do_not_record_p, expr_hash_table_size);
1976 /* Do not insert expression in table if it contains volatile operands,
1977 or if hash_expr determines the expression is something we don't want
1978 to or can't handle. */
1979 if (do_not_record_p)
1980 return;
1982 cur_expr = expr_hash_table[hash];
1983 found = 0;
1985 while (cur_expr && 0 == (found = expr_equiv_p (cur_expr->expr, x)))
1987 /* If the expression isn't found, save a pointer to the end of
1988 the list. */
1989 last_expr = cur_expr;
1990 cur_expr = cur_expr->next_same_hash;
1993 if (! found)
1995 cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
1996 bytes_used += sizeof (struct expr);
1997 if (expr_hash_table[hash] == NULL)
1998 /* This is the first pattern that hashed to this index. */
1999 expr_hash_table[hash] = cur_expr;
2000 else
2001 /* Add EXPR to end of this hash chain. */
2002 last_expr->next_same_hash = cur_expr;
2004 /* Set the fields of the expr element. */
2005 cur_expr->expr = x;
2006 cur_expr->bitmap_index = n_exprs++;
2007 cur_expr->next_same_hash = NULL;
2008 cur_expr->antic_occr = NULL;
2009 cur_expr->avail_occr = NULL;
2012 /* Now record the occurrence(s). */
2013 if (antic_p)
2015 antic_occr = cur_expr->antic_occr;
2017 /* Search for another occurrence in the same basic block. */
2018 while (antic_occr && BLOCK_NUM (antic_occr->insn) != BLOCK_NUM (insn))
2020 /* If an occurrence isn't found, save a pointer to the end of
2021 the list. */
2022 last_occr = antic_occr;
2023 antic_occr = antic_occr->next;
2026 if (antic_occr)
2027 /* Found another instance of the expression in the same basic block.
2028 Prefer the currently recorded one. We want the first one in the
2029 block and the block is scanned from start to end. */
2030 ; /* nothing to do */
2031 else
2033 /* First occurrence of this expression in this basic block. */
2034 antic_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
2035 bytes_used += sizeof (struct occr);
2036 /* First occurrence of this expression in any block? */
2037 if (cur_expr->antic_occr == NULL)
2038 cur_expr->antic_occr = antic_occr;
2039 else
2040 last_occr->next = antic_occr;
2042 antic_occr->insn = insn;
2043 antic_occr->next = NULL;
2047 if (avail_p)
2049 avail_occr = cur_expr->avail_occr;
2051 /* Search for another occurrence in the same basic block. */
2052 while (avail_occr && BLOCK_NUM (avail_occr->insn) != BLOCK_NUM (insn))
2054 /* If an occurrence isn't found, save a pointer to the end of
2055 the list. */
2056 last_occr = avail_occr;
2057 avail_occr = avail_occr->next;
2060 if (avail_occr)
2061 /* Found another instance of the expression in the same basic block.
2062 Prefer this occurrence to the currently recorded one. We want
2063 the last one in the block and the block is scanned from start
2064 to end. */
2065 avail_occr->insn = insn;
2066 else
2068 /* First occurrence of this expression in this basic block. */
2069 avail_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
2070 bytes_used += sizeof (struct occr);
2072 /* First occurrence of this expression in any block? */
2073 if (cur_expr->avail_occr == NULL)
2074 cur_expr->avail_occr = avail_occr;
2075 else
2076 last_occr->next = avail_occr;
2078 avail_occr->insn = insn;
2079 avail_occr->next = NULL;
2084 /* Insert pattern X in INSN in the hash table.
2085 X is a SET of a reg to either another reg or a constant.
2086 If it is already present, record it as the last occurrence in INSN's
2087 basic block. */
2089 static void
2090 insert_set_in_table (x, insn)
2091 rtx x;
2092 rtx insn;
2094 int found;
2095 unsigned int hash;
2096 struct expr *cur_expr, *last_expr = NULL;
2097 struct occr *cur_occr, *last_occr = NULL;
2099 if (GET_CODE (x) != SET
2100 || GET_CODE (SET_DEST (x)) != REG)
2101 abort ();
2103 hash = hash_set (REGNO (SET_DEST (x)), set_hash_table_size);
2105 cur_expr = set_hash_table[hash];
2106 found = 0;
2108 while (cur_expr && 0 == (found = expr_equiv_p (cur_expr->expr, x)))
2110 /* If the expression isn't found, save a pointer to the end of
2111 the list. */
2112 last_expr = cur_expr;
2113 cur_expr = cur_expr->next_same_hash;
2116 if (! found)
2118 cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
2119 bytes_used += sizeof (struct expr);
2120 if (set_hash_table[hash] == NULL)
2121 /* This is the first pattern that hashed to this index. */
2122 set_hash_table[hash] = cur_expr;
2123 else
2124 /* Add EXPR to end of this hash chain. */
2125 last_expr->next_same_hash = cur_expr;
2127 /* Set the fields of the expr element.
2128 We must copy X because it can be modified when copy propagation is
2129 performed on its operands. */
2130 cur_expr->expr = copy_rtx (x);
2131 cur_expr->bitmap_index = n_sets++;
2132 cur_expr->next_same_hash = NULL;
2133 cur_expr->antic_occr = NULL;
2134 cur_expr->avail_occr = NULL;
2137 /* Now record the occurrence. */
2138 cur_occr = cur_expr->avail_occr;
2140 /* Search for another occurrence in the same basic block. */
2141 while (cur_occr && BLOCK_NUM (cur_occr->insn) != BLOCK_NUM (insn))
2143 /* If an occurrence isn't found, save a pointer to the end of
2144 the list. */
2145 last_occr = cur_occr;
2146 cur_occr = cur_occr->next;
2149 if (cur_occr)
2150 /* Found another instance of the expression in the same basic block.
2151 Prefer this occurrence to the currently recorded one. We want the
2152 last one in the block and the block is scanned from start to end. */
2153 cur_occr->insn = insn;
2154 else
2156 /* First occurrence of this expression in this basic block. */
2157 cur_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
2158 bytes_used += sizeof (struct occr);
2160 /* First occurrence of this expression in any block? */
2161 if (cur_expr->avail_occr == NULL)
2162 cur_expr->avail_occr = cur_occr;
2163 else
2164 last_occr->next = cur_occr;
2166 cur_occr->insn = insn;
2167 cur_occr->next = NULL;
2171 /* Scan pattern PAT of INSN and add an entry to the hash table. If SET_P is
2172 non-zero, this is for the assignment hash table, otherwise it is for the
2173 expression hash table. */
2175 static void
2176 hash_scan_set (pat, insn, set_p)
2177 rtx pat, insn;
2178 int set_p;
2180 rtx src = SET_SRC (pat);
2181 rtx dest = SET_DEST (pat);
2182 rtx note;
2184 if (GET_CODE (src) == CALL)
2185 hash_scan_call (src, insn);
2187 else if (GET_CODE (dest) == REG)
2189 unsigned int regno = REGNO (dest);
2190 rtx tmp;
2192 /* If this is a single set and we are doing constant propagation,
2193 see if a REG_NOTE shows this equivalent to a constant. */
2194 if (set_p && (note = find_reg_equal_equiv_note (insn)) != 0
2195 && CONSTANT_P (XEXP (note, 0)))
2196 src = XEXP (note, 0), pat = gen_rtx_SET (VOIDmode, dest, src);
2198 /* Only record sets of pseudo-regs in the hash table. */
2199 if (! set_p
2200 && regno >= FIRST_PSEUDO_REGISTER
2201 /* Don't GCSE something if we can't do a reg/reg copy. */
2202 && can_copy_p [GET_MODE (dest)]
2203 /* GCSE commonly inserts instruction after the insn. We can't
2204 do that easily for EH_REGION notes so disable GCSE on these
2205 for now. */
2206 && !find_reg_note (insn, REG_EH_REGION, NULL_RTX)
2207 /* Is SET_SRC something we want to gcse? */
2208 && want_to_gcse_p (src)
2209 /* Don't CSE a nop. */
2210 && ! set_noop_p (pat)
2211 /* Don't GCSE if it has attached REG_EQUIV note.
2212 At this point this only function parameters should have
2213 REG_EQUIV notes and if the argument slot is used somewhere
2214 explicitly, it means address of parameter has been taken,
2215 so we should not extend the lifetime of the pseudo. */
2216 && ((note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) == 0
2217 || GET_CODE (XEXP (note, 0)) != MEM))
2219 /* An expression is not anticipatable if its operands are
2220 modified before this insn or if this is not the only SET in
2221 this insn. */
2222 int antic_p = oprs_anticipatable_p (src, insn) && single_set (insn);
2223 /* An expression is not available if its operands are
2224 subsequently modified, including this insn. It's also not
2225 available if this is a branch, because we can't insert
2226 a set after the branch. */
2227 int avail_p = (oprs_available_p (src, insn)
2228 && ! JUMP_P (insn));
2230 insert_expr_in_table (src, GET_MODE (dest), insn, antic_p, avail_p);
2233 /* Record sets for constant/copy propagation. */
2234 else if (set_p
2235 && regno >= FIRST_PSEUDO_REGISTER
2236 && ((GET_CODE (src) == REG
2237 && REGNO (src) >= FIRST_PSEUDO_REGISTER
2238 && can_copy_p [GET_MODE (dest)]
2239 && REGNO (src) != regno)
2240 || CONSTANT_P (src))
2241 /* A copy is not available if its src or dest is subsequently
2242 modified. Here we want to search from INSN+1 on, but
2243 oprs_available_p searches from INSN on. */
2244 && (insn == BLOCK_END (BLOCK_NUM (insn))
2245 || ((tmp = next_nonnote_insn (insn)) != NULL_RTX
2246 && oprs_available_p (pat, tmp))))
2247 insert_set_in_table (pat, insn);
2251 static void
2252 hash_scan_clobber (x, insn)
2253 rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
2255 /* Currently nothing to do. */
2258 static void
2259 hash_scan_call (x, insn)
2260 rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
2262 /* Currently nothing to do. */
2265 /* Process INSN and add hash table entries as appropriate.
2267 Only available expressions that set a single pseudo-reg are recorded.
2269 Single sets in a PARALLEL could be handled, but it's an extra complication
2270 that isn't dealt with right now. The trick is handling the CLOBBERs that
2271 are also in the PARALLEL. Later.
2273 If SET_P is non-zero, this is for the assignment hash table,
2274 otherwise it is for the expression hash table.
2275 If IN_LIBCALL_BLOCK nonzero, we are in a libcall block, and should
2276 not record any expressions. */
2278 static void
2279 hash_scan_insn (insn, set_p, in_libcall_block)
2280 rtx insn;
2281 int set_p;
2282 int in_libcall_block;
2284 rtx pat = PATTERN (insn);
2285 int i;
2287 if (in_libcall_block)
2288 return;
2290 /* Pick out the sets of INSN and for other forms of instructions record
2291 what's been modified. */
2293 if (GET_CODE (pat) == SET)
2294 hash_scan_set (pat, insn, set_p);
2295 else if (GET_CODE (pat) == PARALLEL)
2296 for (i = 0; i < XVECLEN (pat, 0); i++)
2298 rtx x = XVECEXP (pat, 0, i);
2300 if (GET_CODE (x) == SET)
2301 hash_scan_set (x, insn, set_p);
2302 else if (GET_CODE (x) == CLOBBER)
2303 hash_scan_clobber (x, insn);
2304 else if (GET_CODE (x) == CALL)
2305 hash_scan_call (x, insn);
2308 else if (GET_CODE (pat) == CLOBBER)
2309 hash_scan_clobber (pat, insn);
2310 else if (GET_CODE (pat) == CALL)
2311 hash_scan_call (pat, insn);
2314 static void
2315 dump_hash_table (file, name, table, table_size, total_size)
2316 FILE *file;
2317 const char *name;
2318 struct expr **table;
2319 int table_size, total_size;
2321 int i;
2322 /* Flattened out table, so it's printed in proper order. */
2323 struct expr **flat_table;
2324 unsigned int *hash_val;
2325 struct expr *expr;
2327 flat_table
2328 = (struct expr **) xcalloc (total_size, sizeof (struct expr *));
2329 hash_val = (unsigned int *) xmalloc (total_size * sizeof (unsigned int));
2331 for (i = 0; i < table_size; i++)
2332 for (expr = table[i]; expr != NULL; expr = expr->next_same_hash)
2334 flat_table[expr->bitmap_index] = expr;
2335 hash_val[expr->bitmap_index] = i;
2338 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
2339 name, table_size, total_size);
2341 for (i = 0; i < total_size; i++)
2342 if (flat_table[i] != 0)
2344 expr = flat_table[i];
2345 fprintf (file, "Index %d (hash value %d)\n ",
2346 expr->bitmap_index, hash_val[i]);
2347 print_rtl (file, expr->expr);
2348 fprintf (file, "\n");
2351 fprintf (file, "\n");
2353 free (flat_table);
2354 free (hash_val);
2357 /* Record register first/last/block set information for REGNO in INSN.
2359 first_set records the first place in the block where the register
2360 is set and is used to compute "anticipatability".
2362 last_set records the last place in the block where the register
2363 is set and is used to compute "availability".
2365 last_bb records the block for which first_set and last_set are
2366 valid, as a quick test to invalidate them.
2368 reg_set_in_block records whether the register is set in the block
2369 and is used to compute "transparency". */
2371 static void
2372 record_last_reg_set_info (insn, regno)
2373 rtx insn;
2374 int regno;
2376 struct reg_avail_info *info = &reg_avail_info[regno];
2377 int cuid = INSN_CUID (insn);
2379 info->last_set = cuid;
2380 if (info->last_bb != current_bb)
2382 info->last_bb = current_bb;
2383 info->first_set = cuid;
2384 SET_BIT (reg_set_in_block[current_bb->index], regno);
2389 /* Record all of the canonicalized MEMs of record_last_mem_set_info's insn.
2390 Note we store a pair of elements in the list, so they have to be
2391 taken off pairwise. */
2393 static void
2394 canon_list_insert (dest, unused1, v_insn)
2395 rtx dest ATTRIBUTE_UNUSED;
2396 rtx unused1 ATTRIBUTE_UNUSED;
2397 void * v_insn;
2399 rtx dest_addr, insn;
2400 int bb;
2402 while (GET_CODE (dest) == SUBREG
2403 || GET_CODE (dest) == ZERO_EXTRACT
2404 || GET_CODE (dest) == SIGN_EXTRACT
2405 || GET_CODE (dest) == STRICT_LOW_PART)
2406 dest = XEXP (dest, 0);
2408 /* If DEST is not a MEM, then it will not conflict with a load. Note
2409 that function calls are assumed to clobber memory, but are handled
2410 elsewhere. */
2412 if (GET_CODE (dest) != MEM)
2413 return;
2415 dest_addr = get_addr (XEXP (dest, 0));
2416 dest_addr = canon_rtx (dest_addr);
2417 insn = (rtx) v_insn;
2418 bb = BLOCK_NUM (insn);
2420 canon_modify_mem_list[bb] =
2421 alloc_EXPR_LIST (VOIDmode, dest_addr, canon_modify_mem_list[bb]);
2422 canon_modify_mem_list[bb] =
2423 alloc_EXPR_LIST (VOIDmode, dest, canon_modify_mem_list[bb]);
2424 bitmap_set_bit (canon_modify_mem_list_set, bb);
2427 /* Record memory modification information for INSN. We do not actually care
2428 about the memory location(s) that are set, or even how they are set (consider
2429 a CALL_INSN). We merely need to record which insns modify memory. */
2431 static void
2432 record_last_mem_set_info (insn)
2433 rtx insn;
2435 int bb = BLOCK_NUM (insn);
2437 /* load_killed_in_block_p will handle the case of calls clobbering
2438 everything. */
2439 modify_mem_list[bb] = alloc_INSN_LIST (insn, modify_mem_list[bb]);
2440 bitmap_set_bit (modify_mem_list_set, bb);
2442 if (GET_CODE (insn) == CALL_INSN)
2444 /* Note that traversals of this loop (other than for free-ing)
2445 will break after encountering a CALL_INSN. So, there's no
2446 need to insert a pair of items, as canon_list_insert does. */
2447 canon_modify_mem_list[bb] =
2448 alloc_INSN_LIST (insn, canon_modify_mem_list[bb]);
2449 bitmap_set_bit (canon_modify_mem_list_set, bb);
2451 else
2452 note_stores (PATTERN (insn), canon_list_insert, (void*) insn);
2455 /* Called from compute_hash_table via note_stores to handle one
2456 SET or CLOBBER in an insn. DATA is really the instruction in which
2457 the SET is taking place. */
2459 static void
2460 record_last_set_info (dest, setter, data)
2461 rtx dest, setter ATTRIBUTE_UNUSED;
2462 void *data;
2464 rtx last_set_insn = (rtx) data;
2466 if (GET_CODE (dest) == SUBREG)
2467 dest = SUBREG_REG (dest);
2469 if (GET_CODE (dest) == REG)
2470 record_last_reg_set_info (last_set_insn, REGNO (dest));
2471 else if (GET_CODE (dest) == MEM
2472 /* Ignore pushes, they clobber nothing. */
2473 && ! push_operand (dest, GET_MODE (dest)))
2474 record_last_mem_set_info (last_set_insn);
2477 /* Top level function to create an expression or assignment hash table.
2479 Expression entries are placed in the hash table if
2480 - they are of the form (set (pseudo-reg) src),
2481 - src is something we want to perform GCSE on,
2482 - none of the operands are subsequently modified in the block
2484 Assignment entries are placed in the hash table if
2485 - they are of the form (set (pseudo-reg) src),
2486 - src is something we want to perform const/copy propagation on,
2487 - none of the operands or target are subsequently modified in the block
2489 Currently src must be a pseudo-reg or a const_int.
2491 F is the first insn.
2492 SET_P is non-zero for computing the assignment hash table. */
2494 static void
2495 compute_hash_table (set_p)
2496 int set_p;
2498 unsigned int i;
2500 /* While we compute the hash table we also compute a bit array of which
2501 registers are set in which blocks.
2502 ??? This isn't needed during const/copy propagation, but it's cheap to
2503 compute. Later. */
2504 sbitmap_vector_zero (reg_set_in_block, last_basic_block);
2506 /* re-Cache any INSN_LIST nodes we have allocated. */
2507 clear_modify_mem_tables ();
2508 /* Some working arrays used to track first and last set in each block. */
2509 reg_avail_info = (struct reg_avail_info*)
2510 gmalloc (max_gcse_regno * sizeof (struct reg_avail_info));
2512 for (i = 0; i < max_gcse_regno; ++i)
2513 reg_avail_info[i].last_bb = NULL;
2515 FOR_EACH_BB (current_bb)
2517 rtx insn;
2518 unsigned int regno;
2519 int in_libcall_block;
2521 /* First pass over the instructions records information used to
2522 determine when registers and memory are first and last set.
2523 ??? hard-reg reg_set_in_block computation
2524 could be moved to compute_sets since they currently don't change. */
2526 for (insn = current_bb->head;
2527 insn && insn != NEXT_INSN (current_bb->end);
2528 insn = NEXT_INSN (insn))
2530 if (! INSN_P (insn))
2531 continue;
2533 if (GET_CODE (insn) == CALL_INSN)
2535 bool clobbers_all = false;
2536 #ifdef NON_SAVING_SETJMP
2537 if (NON_SAVING_SETJMP
2538 && find_reg_note (insn, REG_SETJMP, NULL_RTX))
2539 clobbers_all = true;
2540 #endif
2542 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2543 if (clobbers_all
2544 || TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
2545 record_last_reg_set_info (insn, regno);
2547 mark_call (insn);
2550 note_stores (PATTERN (insn), record_last_set_info, insn);
2553 /* The next pass builds the hash table. */
2555 for (insn = current_bb->head, in_libcall_block = 0;
2556 insn && insn != NEXT_INSN (current_bb->end);
2557 insn = NEXT_INSN (insn))
2558 if (INSN_P (insn))
2560 if (find_reg_note (insn, REG_LIBCALL, NULL_RTX))
2561 in_libcall_block = 1;
2562 else if (set_p && find_reg_note (insn, REG_RETVAL, NULL_RTX))
2563 in_libcall_block = 0;
2564 hash_scan_insn (insn, set_p, in_libcall_block);
2565 if (!set_p && find_reg_note (insn, REG_RETVAL, NULL_RTX))
2566 in_libcall_block = 0;
2570 free (reg_avail_info);
2571 reg_avail_info = NULL;
2574 /* Allocate space for the set hash table.
2575 N_INSNS is the number of instructions in the function.
2576 It is used to determine the number of buckets to use. */
2578 static void
2579 alloc_set_hash_table (n_insns)
2580 int n_insns;
2582 int n;
2584 set_hash_table_size = n_insns / 4;
2585 if (set_hash_table_size < 11)
2586 set_hash_table_size = 11;
2588 /* Attempt to maintain efficient use of hash table.
2589 Making it an odd number is simplest for now.
2590 ??? Later take some measurements. */
2591 set_hash_table_size |= 1;
2592 n = set_hash_table_size * sizeof (struct expr *);
2593 set_hash_table = (struct expr **) gmalloc (n);
2596 /* Free things allocated by alloc_set_hash_table. */
2598 static void
2599 free_set_hash_table ()
2601 free (set_hash_table);
2604 /* Compute the hash table for doing copy/const propagation. */
2606 static void
2607 compute_set_hash_table ()
2609 /* Initialize count of number of entries in hash table. */
2610 n_sets = 0;
2611 memset ((char *) set_hash_table, 0,
2612 set_hash_table_size * sizeof (struct expr *));
2614 compute_hash_table (1);
2617 /* Allocate space for the expression hash table.
2618 N_INSNS is the number of instructions in the function.
2619 It is used to determine the number of buckets to use. */
2621 static void
2622 alloc_expr_hash_table (n_insns)
2623 unsigned int n_insns;
2625 int n;
2627 expr_hash_table_size = n_insns / 2;
2628 /* Make sure the amount is usable. */
2629 if (expr_hash_table_size < 11)
2630 expr_hash_table_size = 11;
2632 /* Attempt to maintain efficient use of hash table.
2633 Making it an odd number is simplest for now.
2634 ??? Later take some measurements. */
2635 expr_hash_table_size |= 1;
2636 n = expr_hash_table_size * sizeof (struct expr *);
2637 expr_hash_table = (struct expr **) gmalloc (n);
2640 /* Free things allocated by alloc_expr_hash_table. */
2642 static void
2643 free_expr_hash_table ()
2645 free (expr_hash_table);
2648 /* Compute the hash table for doing GCSE. */
2650 static void
2651 compute_expr_hash_table ()
2653 /* Initialize count of number of entries in hash table. */
2654 n_exprs = 0;
2655 memset ((char *) expr_hash_table, 0,
2656 expr_hash_table_size * sizeof (struct expr *));
2658 compute_hash_table (0);
2661 /* Expression tracking support. */
2663 /* Lookup pattern PAT in the expression table.
2664 The result is a pointer to the table entry, or NULL if not found. */
2666 static struct expr *
2667 lookup_expr (pat)
2668 rtx pat;
2670 int do_not_record_p;
2671 unsigned int hash = hash_expr (pat, GET_MODE (pat), &do_not_record_p,
2672 expr_hash_table_size);
2673 struct expr *expr;
2675 if (do_not_record_p)
2676 return NULL;
2678 expr = expr_hash_table[hash];
2680 while (expr && ! expr_equiv_p (expr->expr, pat))
2681 expr = expr->next_same_hash;
2683 return expr;
2686 /* Lookup REGNO in the set table. If PAT is non-NULL look for the entry that
2687 matches it, otherwise return the first entry for REGNO. The result is a
2688 pointer to the table entry, or NULL if not found. */
2690 static struct expr *
2691 lookup_set (regno, pat)
2692 unsigned int regno;
2693 rtx pat;
2695 unsigned int hash = hash_set (regno, set_hash_table_size);
2696 struct expr *expr;
2698 expr = set_hash_table[hash];
2700 if (pat)
2702 while (expr && ! expr_equiv_p (expr->expr, pat))
2703 expr = expr->next_same_hash;
2705 else
2707 while (expr && REGNO (SET_DEST (expr->expr)) != regno)
2708 expr = expr->next_same_hash;
2711 return expr;
2714 /* Return the next entry for REGNO in list EXPR. */
2716 static struct expr *
2717 next_set (regno, expr)
2718 unsigned int regno;
2719 struct expr *expr;
2722 expr = expr->next_same_hash;
2723 while (expr && REGNO (SET_DEST (expr->expr)) != regno);
2725 return expr;
2728 /* Like free_INSN_LIST_list or free_EXPR_LIST_list, except that the node
2729 types may be mixed. */
2731 static void
2732 free_insn_expr_list_list (listp)
2733 rtx *listp;
2735 rtx list, next;
2737 for (list = *listp; list ; list = next)
2739 next = XEXP (list, 1);
2740 if (GET_CODE (list) == EXPR_LIST)
2741 free_EXPR_LIST_node (list);
2742 else
2743 free_INSN_LIST_node (list);
2746 *listp = NULL;
2749 /* Clear canon_modify_mem_list and modify_mem_list tables. */
2750 static void
2751 clear_modify_mem_tables ()
2753 int i;
2755 EXECUTE_IF_SET_IN_BITMAP
2756 (modify_mem_list_set, 0, i, free_INSN_LIST_list (modify_mem_list + i));
2757 bitmap_clear (modify_mem_list_set);
2759 EXECUTE_IF_SET_IN_BITMAP
2760 (canon_modify_mem_list_set, 0, i,
2761 free_insn_expr_list_list (canon_modify_mem_list + i));
2762 bitmap_clear (canon_modify_mem_list_set);
2765 /* Release memory used by modify_mem_list_set and canon_modify_mem_list_set. */
2767 static void
2768 free_modify_mem_tables ()
2770 clear_modify_mem_tables ();
2771 free (modify_mem_list);
2772 free (canon_modify_mem_list);
2773 modify_mem_list = 0;
2774 canon_modify_mem_list = 0;
2777 /* Reset tables used to keep track of what's still available [since the
2778 start of the block]. */
2780 static void
2781 reset_opr_set_tables ()
2783 /* Maintain a bitmap of which regs have been set since beginning of
2784 the block. */
2785 CLEAR_REG_SET (reg_set_bitmap);
2787 /* Also keep a record of the last instruction to modify memory.
2788 For now this is very trivial, we only record whether any memory
2789 location has been modified. */
2790 clear_modify_mem_tables ();
2793 /* Return non-zero if the operands of X are not set before INSN in
2794 INSN's basic block. */
2796 static int
2797 oprs_not_set_p (x, insn)
2798 rtx x, insn;
2800 int i, j;
2801 enum rtx_code code;
2802 const char *fmt;
2804 if (x == 0)
2805 return 1;
2807 code = GET_CODE (x);
2808 switch (code)
2810 case PC:
2811 case CC0:
2812 case CONST:
2813 case CONST_INT:
2814 case CONST_DOUBLE:
2815 case CONST_VECTOR:
2816 case SYMBOL_REF:
2817 case LABEL_REF:
2818 case ADDR_VEC:
2819 case ADDR_DIFF_VEC:
2820 return 1;
2822 case MEM:
2823 if (load_killed_in_block_p (BLOCK_FOR_INSN (insn),
2824 INSN_CUID (insn), x, 0))
2825 return 0;
2826 else
2827 return oprs_not_set_p (XEXP (x, 0), insn);
2829 case REG:
2830 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
2832 default:
2833 break;
2836 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
2838 if (fmt[i] == 'e')
2840 /* If we are about to do the last recursive call
2841 needed at this level, change it into iteration.
2842 This function is called enough to be worth it. */
2843 if (i == 0)
2844 return oprs_not_set_p (XEXP (x, i), insn);
2846 if (! oprs_not_set_p (XEXP (x, i), insn))
2847 return 0;
2849 else if (fmt[i] == 'E')
2850 for (j = 0; j < XVECLEN (x, i); j++)
2851 if (! oprs_not_set_p (XVECEXP (x, i, j), insn))
2852 return 0;
2855 return 1;
2858 /* Mark things set by a CALL. */
2860 static void
2861 mark_call (insn)
2862 rtx insn;
2864 if (! CONST_OR_PURE_CALL_P (insn))
2865 record_last_mem_set_info (insn);
2868 /* Mark things set by a SET. */
2870 static void
2871 mark_set (pat, insn)
2872 rtx pat, insn;
2874 rtx dest = SET_DEST (pat);
2876 while (GET_CODE (dest) == SUBREG
2877 || GET_CODE (dest) == ZERO_EXTRACT
2878 || GET_CODE (dest) == SIGN_EXTRACT
2879 || GET_CODE (dest) == STRICT_LOW_PART)
2880 dest = XEXP (dest, 0);
2882 if (GET_CODE (dest) == REG)
2883 SET_REGNO_REG_SET (reg_set_bitmap, REGNO (dest));
2884 else if (GET_CODE (dest) == MEM)
2885 record_last_mem_set_info (insn);
2887 if (GET_CODE (SET_SRC (pat)) == CALL)
2888 mark_call (insn);
2891 /* Record things set by a CLOBBER. */
2893 static void
2894 mark_clobber (pat, insn)
2895 rtx pat, insn;
2897 rtx clob = XEXP (pat, 0);
2899 while (GET_CODE (clob) == SUBREG || GET_CODE (clob) == STRICT_LOW_PART)
2900 clob = XEXP (clob, 0);
2902 if (GET_CODE (clob) == REG)
2903 SET_REGNO_REG_SET (reg_set_bitmap, REGNO (clob));
2904 else
2905 record_last_mem_set_info (insn);
2908 /* Record things set by INSN.
2909 This data is used by oprs_not_set_p. */
2911 static void
2912 mark_oprs_set (insn)
2913 rtx insn;
2915 rtx pat = PATTERN (insn);
2916 int i;
2918 if (GET_CODE (pat) == SET)
2919 mark_set (pat, insn);
2920 else if (GET_CODE (pat) == PARALLEL)
2921 for (i = 0; i < XVECLEN (pat, 0); i++)
2923 rtx x = XVECEXP (pat, 0, i);
2925 if (GET_CODE (x) == SET)
2926 mark_set (x, insn);
2927 else if (GET_CODE (x) == CLOBBER)
2928 mark_clobber (x, insn);
2929 else if (GET_CODE (x) == CALL)
2930 mark_call (insn);
2933 else if (GET_CODE (pat) == CLOBBER)
2934 mark_clobber (pat, insn);
2935 else if (GET_CODE (pat) == CALL)
2936 mark_call (insn);
2940 /* Classic GCSE reaching definition support. */
2942 /* Allocate reaching def variables. */
2944 static void
2945 alloc_rd_mem (n_blocks, n_insns)
2946 int n_blocks, n_insns;
2948 rd_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2949 sbitmap_vector_zero (rd_kill, n_blocks);
2951 rd_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2952 sbitmap_vector_zero (rd_gen, n_blocks);
2954 reaching_defs = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2955 sbitmap_vector_zero (reaching_defs, n_blocks);
2957 rd_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2958 sbitmap_vector_zero (rd_out, n_blocks);
2961 /* Free reaching def variables. */
2963 static void
2964 free_rd_mem ()
2966 sbitmap_vector_free (rd_kill);
2967 sbitmap_vector_free (rd_gen);
2968 sbitmap_vector_free (reaching_defs);
2969 sbitmap_vector_free (rd_out);
2972 /* Add INSN to the kills of BB. REGNO, set in BB, is killed by INSN. */
2974 static void
2975 handle_rd_kill_set (insn, regno, bb)
2976 rtx insn;
2977 int regno;
2978 basic_block bb;
2980 struct reg_set *this_reg;
2982 for (this_reg = reg_set_table[regno]; this_reg; this_reg = this_reg ->next)
2983 if (BLOCK_NUM (this_reg->insn) != BLOCK_NUM (insn))
2984 SET_BIT (rd_kill[bb->index], INSN_CUID (this_reg->insn));
2987 /* Compute the set of kill's for reaching definitions. */
2989 static void
2990 compute_kill_rd ()
2992 int cuid;
2993 unsigned int regno;
2994 int i;
2995 basic_block bb;
2997 /* For each block
2998 For each set bit in `gen' of the block (i.e each insn which
2999 generates a definition in the block)
3000 Call the reg set by the insn corresponding to that bit regx
3001 Look at the linked list starting at reg_set_table[regx]
3002 For each setting of regx in the linked list, which is not in
3003 this block
3004 Set the bit in `kill' corresponding to that insn. */
3005 FOR_EACH_BB (bb)
3006 for (cuid = 0; cuid < max_cuid; cuid++)
3007 if (TEST_BIT (rd_gen[bb->index], cuid))
3009 rtx insn = CUID_INSN (cuid);
3010 rtx pat = PATTERN (insn);
3012 if (GET_CODE (insn) == CALL_INSN)
3014 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
3015 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
3016 handle_rd_kill_set (insn, regno, bb);
3019 if (GET_CODE (pat) == PARALLEL)
3021 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
3023 enum rtx_code code = GET_CODE (XVECEXP (pat, 0, i));
3025 if ((code == SET || code == CLOBBER)
3026 && GET_CODE (XEXP (XVECEXP (pat, 0, i), 0)) == REG)
3027 handle_rd_kill_set (insn,
3028 REGNO (XEXP (XVECEXP (pat, 0, i), 0)),
3029 bb);
3032 else if (GET_CODE (pat) == SET && GET_CODE (SET_DEST (pat)) == REG)
3033 /* Each setting of this register outside of this block
3034 must be marked in the set of kills in this block. */
3035 handle_rd_kill_set (insn, REGNO (SET_DEST (pat)), bb);
3039 /* Compute the reaching definitions as in
3040 Compilers Principles, Techniques, and Tools. Aho, Sethi, Ullman,
3041 Chapter 10. It is the same algorithm as used for computing available
3042 expressions but applied to the gens and kills of reaching definitions. */
3044 static void
3045 compute_rd ()
3047 int changed, passes;
3048 basic_block bb;
3050 FOR_EACH_BB (bb)
3051 sbitmap_copy (rd_out[bb->index] /*dst*/, rd_gen[bb->index] /*src*/);
3053 passes = 0;
3054 changed = 1;
3055 while (changed)
3057 changed = 0;
3058 FOR_EACH_BB (bb)
3060 sbitmap_union_of_preds (reaching_defs[bb->index], rd_out, bb->index);
3061 changed |= sbitmap_union_of_diff_cg (rd_out[bb->index], rd_gen[bb->index],
3062 reaching_defs[bb->index], rd_kill[bb->index]);
3064 passes++;
3067 if (gcse_file)
3068 fprintf (gcse_file, "reaching def computation: %d passes\n", passes);
3071 /* Classic GCSE available expression support. */
3073 /* Allocate memory for available expression computation. */
3075 static void
3076 alloc_avail_expr_mem (n_blocks, n_exprs)
3077 int n_blocks, n_exprs;
3079 ae_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
3080 sbitmap_vector_zero (ae_kill, n_blocks);
3082 ae_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
3083 sbitmap_vector_zero (ae_gen, n_blocks);
3085 ae_in = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
3086 sbitmap_vector_zero (ae_in, n_blocks);
3088 ae_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
3089 sbitmap_vector_zero (ae_out, n_blocks);
3092 static void
3093 free_avail_expr_mem ()
3095 sbitmap_vector_free (ae_kill);
3096 sbitmap_vector_free (ae_gen);
3097 sbitmap_vector_free (ae_in);
3098 sbitmap_vector_free (ae_out);
3101 /* Compute the set of available expressions generated in each basic block. */
3103 static void
3104 compute_ae_gen ()
3106 unsigned int i;
3107 struct expr *expr;
3108 struct occr *occr;
3110 /* For each recorded occurrence of each expression, set ae_gen[bb][expr].
3111 This is all we have to do because an expression is not recorded if it
3112 is not available, and the only expressions we want to work with are the
3113 ones that are recorded. */
3114 for (i = 0; i < expr_hash_table_size; i++)
3115 for (expr = expr_hash_table[i]; expr != 0; expr = expr->next_same_hash)
3116 for (occr = expr->avail_occr; occr != 0; occr = occr->next)
3117 SET_BIT (ae_gen[BLOCK_NUM (occr->insn)], expr->bitmap_index);
3120 /* Return non-zero if expression X is killed in BB. */
3122 static int
3123 expr_killed_p (x, bb)
3124 rtx x;
3125 basic_block bb;
3127 int i, j;
3128 enum rtx_code code;
3129 const char *fmt;
3131 if (x == 0)
3132 return 1;
3134 code = GET_CODE (x);
3135 switch (code)
3137 case REG:
3138 return TEST_BIT (reg_set_in_block[bb->index], REGNO (x));
3140 case MEM:
3141 if (load_killed_in_block_p (bb, get_max_uid () + 1, x, 0))
3142 return 1;
3143 else
3144 return expr_killed_p (XEXP (x, 0), bb);
3146 case PC:
3147 case CC0: /*FIXME*/
3148 case CONST:
3149 case CONST_INT:
3150 case CONST_DOUBLE:
3151 case CONST_VECTOR:
3152 case SYMBOL_REF:
3153 case LABEL_REF:
3154 case ADDR_VEC:
3155 case ADDR_DIFF_VEC:
3156 return 0;
3158 default:
3159 break;
3162 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
3164 if (fmt[i] == 'e')
3166 /* If we are about to do the last recursive call
3167 needed at this level, change it into iteration.
3168 This function is called enough to be worth it. */
3169 if (i == 0)
3170 return expr_killed_p (XEXP (x, i), bb);
3171 else if (expr_killed_p (XEXP (x, i), bb))
3172 return 1;
3174 else if (fmt[i] == 'E')
3175 for (j = 0; j < XVECLEN (x, i); j++)
3176 if (expr_killed_p (XVECEXP (x, i, j), bb))
3177 return 1;
3180 return 0;
3183 /* Compute the set of available expressions killed in each basic block. */
3185 static void
3186 compute_ae_kill (ae_gen, ae_kill)
3187 sbitmap *ae_gen, *ae_kill;
3189 basic_block bb;
3190 unsigned int i;
3191 struct expr *expr;
3193 FOR_EACH_BB (bb)
3194 for (i = 0; i < expr_hash_table_size; i++)
3195 for (expr = expr_hash_table[i]; expr; expr = expr->next_same_hash)
3197 /* Skip EXPR if generated in this block. */
3198 if (TEST_BIT (ae_gen[bb->index], expr->bitmap_index))
3199 continue;
3201 if (expr_killed_p (expr->expr, bb))
3202 SET_BIT (ae_kill[bb->index], expr->bitmap_index);
3206 /* Actually perform the Classic GCSE optimizations. */
3208 /* Return non-zero if occurrence OCCR of expression EXPR reaches block BB.
3210 CHECK_SELF_LOOP is non-zero if we should consider a block reaching itself
3211 as a positive reach. We want to do this when there are two computations
3212 of the expression in the block.
3214 VISITED is a pointer to a working buffer for tracking which BB's have
3215 been visited. It is NULL for the top-level call.
3217 We treat reaching expressions that go through blocks containing the same
3218 reaching expression as "not reaching". E.g. if EXPR is generated in blocks
3219 2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
3220 2 as not reaching. The intent is to improve the probability of finding
3221 only one reaching expression and to reduce register lifetimes by picking
3222 the closest such expression. */
3224 static int
3225 expr_reaches_here_p_work (occr, expr, bb, check_self_loop, visited)
3226 struct occr *occr;
3227 struct expr *expr;
3228 basic_block bb;
3229 int check_self_loop;
3230 char *visited;
3232 edge pred;
3234 for (pred = bb->pred; pred != NULL; pred = pred->pred_next)
3236 basic_block pred_bb = pred->src;
3238 if (visited[pred_bb->index])
3239 /* This predecessor has already been visited. Nothing to do. */
3241 else if (pred_bb == bb)
3243 /* BB loops on itself. */
3244 if (check_self_loop
3245 && TEST_BIT (ae_gen[pred_bb->index], expr->bitmap_index)
3246 && BLOCK_NUM (occr->insn) == pred_bb->index)
3247 return 1;
3249 visited[pred_bb->index] = 1;
3252 /* Ignore this predecessor if it kills the expression. */
3253 else if (TEST_BIT (ae_kill[pred_bb->index], expr->bitmap_index))
3254 visited[pred_bb->index] = 1;
3256 /* Does this predecessor generate this expression? */
3257 else if (TEST_BIT (ae_gen[pred_bb->index], expr->bitmap_index))
3259 /* Is this the occurrence we're looking for?
3260 Note that there's only one generating occurrence per block
3261 so we just need to check the block number. */
3262 if (BLOCK_NUM (occr->insn) == pred_bb->index)
3263 return 1;
3265 visited[pred_bb->index] = 1;
3268 /* Neither gen nor kill. */
3269 else
3271 visited[pred_bb->index] = 1;
3272 if (expr_reaches_here_p_work (occr, expr, pred_bb, check_self_loop,
3273 visited))
3275 return 1;
3279 /* All paths have been checked. */
3280 return 0;
3283 /* This wrapper for expr_reaches_here_p_work() is to ensure that any
3284 memory allocated for that function is returned. */
3286 static int
3287 expr_reaches_here_p (occr, expr, bb, check_self_loop)
3288 struct occr *occr;
3289 struct expr *expr;
3290 basic_block bb;
3291 int check_self_loop;
3293 int rval;
3294 char *visited = (char *) xcalloc (last_basic_block, 1);
3296 rval = expr_reaches_here_p_work (occr, expr, bb, check_self_loop, visited);
3298 free (visited);
3299 return rval;
3302 /* Return the instruction that computes EXPR that reaches INSN's basic block.
3303 If there is more than one such instruction, return NULL.
3305 Called only by handle_avail_expr. */
3307 static rtx
3308 computing_insn (expr, insn)
3309 struct expr *expr;
3310 rtx insn;
3312 basic_block bb = BLOCK_FOR_INSN (insn);
3314 if (expr->avail_occr->next == NULL)
3316 if (BLOCK_FOR_INSN (expr->avail_occr->insn) == bb)
3317 /* The available expression is actually itself
3318 (i.e. a loop in the flow graph) so do nothing. */
3319 return NULL;
3321 /* (FIXME) Case that we found a pattern that was created by
3322 a substitution that took place. */
3323 return expr->avail_occr->insn;
3325 else
3327 /* Pattern is computed more than once.
3328 Search backwards from this insn to see how many of these
3329 computations actually reach this insn. */
3330 struct occr *occr;
3331 rtx insn_computes_expr = NULL;
3332 int can_reach = 0;
3334 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
3336 if (BLOCK_FOR_INSN (occr->insn) == bb)
3338 /* The expression is generated in this block.
3339 The only time we care about this is when the expression
3340 is generated later in the block [and thus there's a loop].
3341 We let the normal cse pass handle the other cases. */
3342 if (INSN_CUID (insn) < INSN_CUID (occr->insn)
3343 && expr_reaches_here_p (occr, expr, bb, 1))
3345 can_reach++;
3346 if (can_reach > 1)
3347 return NULL;
3349 insn_computes_expr = occr->insn;
3352 else if (expr_reaches_here_p (occr, expr, bb, 0))
3354 can_reach++;
3355 if (can_reach > 1)
3356 return NULL;
3358 insn_computes_expr = occr->insn;
3362 if (insn_computes_expr == NULL)
3363 abort ();
3365 return insn_computes_expr;
3369 /* Return non-zero if the definition in DEF_INSN can reach INSN.
3370 Only called by can_disregard_other_sets. */
3372 static int
3373 def_reaches_here_p (insn, def_insn)
3374 rtx insn, def_insn;
3376 rtx reg;
3378 if (TEST_BIT (reaching_defs[BLOCK_NUM (insn)], INSN_CUID (def_insn)))
3379 return 1;
3381 if (BLOCK_NUM (insn) == BLOCK_NUM (def_insn))
3383 if (INSN_CUID (def_insn) < INSN_CUID (insn))
3385 if (GET_CODE (PATTERN (def_insn)) == PARALLEL)
3386 return 1;
3387 else if (GET_CODE (PATTERN (def_insn)) == CLOBBER)
3388 reg = XEXP (PATTERN (def_insn), 0);
3389 else if (GET_CODE (PATTERN (def_insn)) == SET)
3390 reg = SET_DEST (PATTERN (def_insn));
3391 else
3392 abort ();
3394 return ! reg_set_between_p (reg, NEXT_INSN (def_insn), insn);
3396 else
3397 return 0;
3400 return 0;
3403 /* Return non-zero if *ADDR_THIS_REG can only have one value at INSN. The
3404 value returned is the number of definitions that reach INSN. Returning a
3405 value of zero means that [maybe] more than one definition reaches INSN and
3406 the caller can't perform whatever optimization it is trying. i.e. it is
3407 always safe to return zero. */
3409 static int
3410 can_disregard_other_sets (addr_this_reg, insn, for_combine)
3411 struct reg_set **addr_this_reg;
3412 rtx insn;
3413 int for_combine;
3415 int number_of_reaching_defs = 0;
3416 struct reg_set *this_reg;
3418 for (this_reg = *addr_this_reg; this_reg != 0; this_reg = this_reg->next)
3419 if (def_reaches_here_p (insn, this_reg->insn))
3421 number_of_reaching_defs++;
3422 /* Ignore parallels for now. */
3423 if (GET_CODE (PATTERN (this_reg->insn)) == PARALLEL)
3424 return 0;
3426 if (!for_combine
3427 && (GET_CODE (PATTERN (this_reg->insn)) == CLOBBER
3428 || ! rtx_equal_p (SET_SRC (PATTERN (this_reg->insn)),
3429 SET_SRC (PATTERN (insn)))))
3430 /* A setting of the reg to a different value reaches INSN. */
3431 return 0;
3433 if (number_of_reaching_defs > 1)
3435 /* If in this setting the value the register is being set to is
3436 equal to the previous value the register was set to and this
3437 setting reaches the insn we are trying to do the substitution
3438 on then we are ok. */
3439 if (GET_CODE (PATTERN (this_reg->insn)) == CLOBBER)
3440 return 0;
3441 else if (! rtx_equal_p (SET_SRC (PATTERN (this_reg->insn)),
3442 SET_SRC (PATTERN (insn))))
3443 return 0;
3446 *addr_this_reg = this_reg;
3449 return number_of_reaching_defs;
3452 /* Expression computed by insn is available and the substitution is legal,
3453 so try to perform the substitution.
3455 The result is non-zero if any changes were made. */
3457 static int
3458 handle_avail_expr (insn, expr)
3459 rtx insn;
3460 struct expr *expr;
3462 rtx pat, insn_computes_expr, expr_set;
3463 rtx to;
3464 struct reg_set *this_reg;
3465 int found_setting, use_src;
3466 int changed = 0;
3468 /* We only handle the case where one computation of the expression
3469 reaches this instruction. */
3470 insn_computes_expr = computing_insn (expr, insn);
3471 if (insn_computes_expr == NULL)
3472 return 0;
3473 expr_set = single_set (insn_computes_expr);
3474 if (!expr_set)
3475 abort ();
3477 found_setting = 0;
3478 use_src = 0;
3480 /* At this point we know only one computation of EXPR outside of this
3481 block reaches this insn. Now try to find a register that the
3482 expression is computed into. */
3483 if (GET_CODE (SET_SRC (expr_set)) == REG)
3485 /* This is the case when the available expression that reaches
3486 here has already been handled as an available expression. */
3487 unsigned int regnum_for_replacing
3488 = REGNO (SET_SRC (expr_set));
3490 /* If the register was created by GCSE we can't use `reg_set_table',
3491 however we know it's set only once. */
3492 if (regnum_for_replacing >= max_gcse_regno
3493 /* If the register the expression is computed into is set only once,
3494 or only one set reaches this insn, we can use it. */
3495 || (((this_reg = reg_set_table[regnum_for_replacing]),
3496 this_reg->next == NULL)
3497 || can_disregard_other_sets (&this_reg, insn, 0)))
3499 use_src = 1;
3500 found_setting = 1;
3504 if (!found_setting)
3506 unsigned int regnum_for_replacing
3507 = REGNO (SET_DEST (expr_set));
3509 /* This shouldn't happen. */
3510 if (regnum_for_replacing >= max_gcse_regno)
3511 abort ();
3513 this_reg = reg_set_table[regnum_for_replacing];
3515 /* If the register the expression is computed into is set only once,
3516 or only one set reaches this insn, use it. */
3517 if (this_reg->next == NULL
3518 || can_disregard_other_sets (&this_reg, insn, 0))
3519 found_setting = 1;
3522 if (found_setting)
3524 pat = PATTERN (insn);
3525 if (use_src)
3526 to = SET_SRC (expr_set);
3527 else
3528 to = SET_DEST (expr_set);
3529 changed = validate_change (insn, &SET_SRC (pat), to, 0);
3531 /* We should be able to ignore the return code from validate_change but
3532 to play it safe we check. */
3533 if (changed)
3535 gcse_subst_count++;
3536 if (gcse_file != NULL)
3538 fprintf (gcse_file, "GCSE: Replacing the source in insn %d with",
3539 INSN_UID (insn));
3540 fprintf (gcse_file, " reg %d %s insn %d\n",
3541 REGNO (to), use_src ? "from" : "set in",
3542 INSN_UID (insn_computes_expr));
3547 /* The register that the expr is computed into is set more than once. */
3548 else if (1 /*expensive_op(this_pattrn->op) && do_expensive_gcse)*/)
3550 /* Insert an insn after insnx that copies the reg set in insnx
3551 into a new pseudo register call this new register REGN.
3552 From insnb until end of basic block or until REGB is set
3553 replace all uses of REGB with REGN. */
3554 rtx new_insn;
3556 to = gen_reg_rtx (GET_MODE (SET_DEST (expr_set)));
3558 /* Generate the new insn. */
3559 /* ??? If the change fails, we return 0, even though we created
3560 an insn. I think this is ok. */
3561 new_insn
3562 = emit_insn_after (gen_rtx_SET (VOIDmode, to,
3563 SET_DEST (expr_set)),
3564 insn_computes_expr);
3566 /* Keep register set table up to date. */
3567 record_one_set (REGNO (to), new_insn);
3569 gcse_create_count++;
3570 if (gcse_file != NULL)
3572 fprintf (gcse_file, "GCSE: Creating insn %d to copy value of reg %d",
3573 INSN_UID (NEXT_INSN (insn_computes_expr)),
3574 REGNO (SET_SRC (PATTERN (NEXT_INSN (insn_computes_expr)))));
3575 fprintf (gcse_file, ", computed in insn %d,\n",
3576 INSN_UID (insn_computes_expr));
3577 fprintf (gcse_file, " into newly allocated reg %d\n",
3578 REGNO (to));
3581 pat = PATTERN (insn);
3583 /* Do register replacement for INSN. */
3584 changed = validate_change (insn, &SET_SRC (pat),
3585 SET_DEST (PATTERN
3586 (NEXT_INSN (insn_computes_expr))),
3589 /* We should be able to ignore the return code from validate_change but
3590 to play it safe we check. */
3591 if (changed)
3593 gcse_subst_count++;
3594 if (gcse_file != NULL)
3596 fprintf (gcse_file,
3597 "GCSE: Replacing the source in insn %d with reg %d ",
3598 INSN_UID (insn),
3599 REGNO (SET_DEST (PATTERN (NEXT_INSN
3600 (insn_computes_expr)))));
3601 fprintf (gcse_file, "set in insn %d\n",
3602 INSN_UID (insn_computes_expr));
3607 return changed;
3610 /* Perform classic GCSE. This is called by one_classic_gcse_pass after all
3611 the dataflow analysis has been done.
3613 The result is non-zero if a change was made. */
3615 static int
3616 classic_gcse ()
3618 int changed;
3619 rtx insn;
3620 basic_block bb;
3622 /* Note we start at block 1. */
3624 if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
3625 return 0;
3627 changed = 0;
3628 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb, EXIT_BLOCK_PTR, next_bb)
3630 /* Reset tables used to keep track of what's still valid [since the
3631 start of the block]. */
3632 reset_opr_set_tables ();
3634 for (insn = bb->head;
3635 insn != NULL && insn != NEXT_INSN (bb->end);
3636 insn = NEXT_INSN (insn))
3638 /* Is insn of form (set (pseudo-reg) ...)? */
3639 if (GET_CODE (insn) == INSN
3640 && GET_CODE (PATTERN (insn)) == SET
3641 && GET_CODE (SET_DEST (PATTERN (insn))) == REG
3642 && REGNO (SET_DEST (PATTERN (insn))) >= FIRST_PSEUDO_REGISTER)
3644 rtx pat = PATTERN (insn);
3645 rtx src = SET_SRC (pat);
3646 struct expr *expr;
3648 if (want_to_gcse_p (src)
3649 /* Is the expression recorded? */
3650 && ((expr = lookup_expr (src)) != NULL)
3651 /* Is the expression available [at the start of the
3652 block]? */
3653 && TEST_BIT (ae_in[bb->index], expr->bitmap_index)
3654 /* Are the operands unchanged since the start of the
3655 block? */
3656 && oprs_not_set_p (src, insn))
3657 changed |= handle_avail_expr (insn, expr);
3660 /* Keep track of everything modified by this insn. */
3661 /* ??? Need to be careful w.r.t. mods done to INSN. */
3662 if (INSN_P (insn))
3663 mark_oprs_set (insn);
3667 return changed;
3670 /* Top level routine to perform one classic GCSE pass.
3672 Return non-zero if a change was made. */
3674 static int
3675 one_classic_gcse_pass (pass)
3676 int pass;
3678 int changed = 0;
3680 gcse_subst_count = 0;
3681 gcse_create_count = 0;
3683 alloc_expr_hash_table (max_cuid);
3684 alloc_rd_mem (last_basic_block, max_cuid);
3685 compute_expr_hash_table ();
3686 if (gcse_file)
3687 dump_hash_table (gcse_file, "Expression", expr_hash_table,
3688 expr_hash_table_size, n_exprs);
3690 if (n_exprs > 0)
3692 compute_kill_rd ();
3693 compute_rd ();
3694 alloc_avail_expr_mem (last_basic_block, n_exprs);
3695 compute_ae_gen ();
3696 compute_ae_kill (ae_gen, ae_kill);
3697 compute_available (ae_gen, ae_kill, ae_out, ae_in);
3698 changed = classic_gcse ();
3699 free_avail_expr_mem ();
3702 free_rd_mem ();
3703 free_expr_hash_table ();
3705 if (gcse_file)
3707 fprintf (gcse_file, "\n");
3708 fprintf (gcse_file, "GCSE of %s, pass %d: %d bytes needed, %d substs,",
3709 current_function_name, pass, bytes_used, gcse_subst_count);
3710 fprintf (gcse_file, "%d insns created\n", gcse_create_count);
3713 return changed;
3716 /* Compute copy/constant propagation working variables. */
3718 /* Local properties of assignments. */
3719 static sbitmap *cprop_pavloc;
3720 static sbitmap *cprop_absaltered;
3722 /* Global properties of assignments (computed from the local properties). */
3723 static sbitmap *cprop_avin;
3724 static sbitmap *cprop_avout;
3726 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
3727 basic blocks. N_SETS is the number of sets. */
3729 static void
3730 alloc_cprop_mem (n_blocks, n_sets)
3731 int n_blocks, n_sets;
3733 cprop_pavloc = sbitmap_vector_alloc (n_blocks, n_sets);
3734 cprop_absaltered = sbitmap_vector_alloc (n_blocks, n_sets);
3736 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
3737 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
3740 /* Free vars used by copy/const propagation. */
3742 static void
3743 free_cprop_mem ()
3745 sbitmap_vector_free (cprop_pavloc);
3746 sbitmap_vector_free (cprop_absaltered);
3747 sbitmap_vector_free (cprop_avin);
3748 sbitmap_vector_free (cprop_avout);
3751 /* For each block, compute whether X is transparent. X is either an
3752 expression or an assignment [though we don't care which, for this context
3753 an assignment is treated as an expression]. For each block where an
3754 element of X is modified, set (SET_P == 1) or reset (SET_P == 0) the INDX
3755 bit in BMAP. */
3757 static void
3758 compute_transp (x, indx, bmap, set_p)
3759 rtx x;
3760 int indx;
3761 sbitmap *bmap;
3762 int set_p;
3764 int i, j;
3765 basic_block bb;
3766 enum rtx_code code;
3767 reg_set *r;
3768 const char *fmt;
3770 /* repeat is used to turn tail-recursion into iteration since GCC
3771 can't do it when there's no return value. */
3772 repeat:
3774 if (x == 0)
3775 return;
3777 code = GET_CODE (x);
3778 switch (code)
3780 case REG:
3781 if (set_p)
3783 if (REGNO (x) < FIRST_PSEUDO_REGISTER)
3785 FOR_EACH_BB (bb)
3786 if (TEST_BIT (reg_set_in_block[bb->index], REGNO (x)))
3787 SET_BIT (bmap[bb->index], indx);
3789 else
3791 for (r = reg_set_table[REGNO (x)]; r != NULL; r = r->next)
3792 SET_BIT (bmap[BLOCK_NUM (r->insn)], indx);
3795 else
3797 if (REGNO (x) < FIRST_PSEUDO_REGISTER)
3799 FOR_EACH_BB (bb)
3800 if (TEST_BIT (reg_set_in_block[bb->index], REGNO (x)))
3801 RESET_BIT (bmap[bb->index], indx);
3803 else
3805 for (r = reg_set_table[REGNO (x)]; r != NULL; r = r->next)
3806 RESET_BIT (bmap[BLOCK_NUM (r->insn)], indx);
3810 return;
3812 case MEM:
3813 FOR_EACH_BB (bb)
3815 rtx list_entry = canon_modify_mem_list[bb->index];
3817 while (list_entry)
3819 rtx dest, dest_addr;
3821 if (GET_CODE (XEXP (list_entry, 0)) == CALL_INSN)
3823 if (set_p)
3824 SET_BIT (bmap[bb->index], indx);
3825 else
3826 RESET_BIT (bmap[bb->index], indx);
3827 break;
3829 /* LIST_ENTRY must be an INSN of some kind that sets memory.
3830 Examine each hunk of memory that is modified. */
3832 dest = XEXP (list_entry, 0);
3833 list_entry = XEXP (list_entry, 1);
3834 dest_addr = XEXP (list_entry, 0);
3836 if (canon_true_dependence (dest, GET_MODE (dest), dest_addr,
3837 x, rtx_addr_varies_p))
3839 if (set_p)
3840 SET_BIT (bmap[bb->index], indx);
3841 else
3842 RESET_BIT (bmap[bb->index], indx);
3843 break;
3845 list_entry = XEXP (list_entry, 1);
3849 x = XEXP (x, 0);
3850 goto repeat;
3852 case PC:
3853 case CC0: /*FIXME*/
3854 case CONST:
3855 case CONST_INT:
3856 case CONST_DOUBLE:
3857 case CONST_VECTOR:
3858 case SYMBOL_REF:
3859 case LABEL_REF:
3860 case ADDR_VEC:
3861 case ADDR_DIFF_VEC:
3862 return;
3864 default:
3865 break;
3868 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
3870 if (fmt[i] == 'e')
3872 /* If we are about to do the last recursive call
3873 needed at this level, change it into iteration.
3874 This function is called enough to be worth it. */
3875 if (i == 0)
3877 x = XEXP (x, i);
3878 goto repeat;
3881 compute_transp (XEXP (x, i), indx, bmap, set_p);
3883 else if (fmt[i] == 'E')
3884 for (j = 0; j < XVECLEN (x, i); j++)
3885 compute_transp (XVECEXP (x, i, j), indx, bmap, set_p);
3889 /* Top level routine to do the dataflow analysis needed by copy/const
3890 propagation. */
3892 static void
3893 compute_cprop_data ()
3895 compute_local_properties (cprop_absaltered, cprop_pavloc, NULL, 1);
3896 compute_available (cprop_pavloc, cprop_absaltered,
3897 cprop_avout, cprop_avin);
3900 /* Copy/constant propagation. */
3902 /* Maximum number of register uses in an insn that we handle. */
3903 #define MAX_USES 8
3905 /* Table of uses found in an insn.
3906 Allocated statically to avoid alloc/free complexity and overhead. */
3907 static struct reg_use reg_use_table[MAX_USES];
3909 /* Index into `reg_use_table' while building it. */
3910 static int reg_use_count;
3912 /* Set up a list of register numbers used in INSN. The found uses are stored
3913 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
3914 and contains the number of uses in the table upon exit.
3916 ??? If a register appears multiple times we will record it multiple times.
3917 This doesn't hurt anything but it will slow things down. */
3919 static void
3920 find_used_regs (xptr, data)
3921 rtx *xptr;
3922 void *data ATTRIBUTE_UNUSED;
3924 int i, j;
3925 enum rtx_code code;
3926 const char *fmt;
3927 rtx x = *xptr;
3929 /* repeat is used to turn tail-recursion into iteration since GCC
3930 can't do it when there's no return value. */
3931 repeat:
3932 if (x == 0)
3933 return;
3935 code = GET_CODE (x);
3936 if (REG_P (x))
3938 if (reg_use_count == MAX_USES)
3939 return;
3941 reg_use_table[reg_use_count].reg_rtx = x;
3942 reg_use_count++;
3945 /* Recursively scan the operands of this expression. */
3947 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
3949 if (fmt[i] == 'e')
3951 /* If we are about to do the last recursive call
3952 needed at this level, change it into iteration.
3953 This function is called enough to be worth it. */
3954 if (i == 0)
3956 x = XEXP (x, 0);
3957 goto repeat;
3960 find_used_regs (&XEXP (x, i), data);
3962 else if (fmt[i] == 'E')
3963 for (j = 0; j < XVECLEN (x, i); j++)
3964 find_used_regs (&XVECEXP (x, i, j), data);
3968 /* Try to replace all non-SET_DEST occurrences of FROM in INSN with TO.
3969 Returns non-zero is successful. */
3971 static int
3972 try_replace_reg (from, to, insn)
3973 rtx from, to, insn;
3975 rtx note = find_reg_equal_equiv_note (insn);
3976 rtx src = 0;
3977 int success = 0;
3978 rtx set = single_set (insn);
3980 validate_replace_src_group (from, to, insn);
3981 if (num_changes_pending () && apply_change_group ())
3982 success = 1;
3984 if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
3986 /* If above failed and this is a single set, try to simplify the source of
3987 the set given our substitution. We could perhaps try this for multiple
3988 SETs, but it probably won't buy us anything. */
3989 src = simplify_replace_rtx (SET_SRC (set), from, to);
3991 if (!rtx_equal_p (src, SET_SRC (set))
3992 && validate_change (insn, &SET_SRC (set), src, 0))
3993 success = 1;
3995 /* If we've failed to do replacement, have a single SET, and don't already
3996 have a note, add a REG_EQUAL note to not lose information. */
3997 if (!success && note == 0 && set != 0)
3998 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
4001 /* If there is already a NOTE, update the expression in it with our
4002 replacement. */
4003 else if (note != 0)
4004 XEXP (note, 0) = simplify_replace_rtx (XEXP (note, 0), from, to);
4006 /* REG_EQUAL may get simplified into register.
4007 We don't allow that. Remove that note. This code ought
4008 not to hapen, because previous code ought to syntetize
4009 reg-reg move, but be on the safe side. */
4010 if (note && REG_P (XEXP (note, 0)))
4011 remove_note (insn, note);
4013 return success;
4016 /* Find a set of REGNOs that are available on entry to INSN's block. Returns
4017 NULL no such set is found. */
4019 static struct expr *
4020 find_avail_set (regno, insn)
4021 int regno;
4022 rtx insn;
4024 /* SET1 contains the last set found that can be returned to the caller for
4025 use in a substitution. */
4026 struct expr *set1 = 0;
4028 /* Loops are not possible here. To get a loop we would need two sets
4029 available at the start of the block containing INSN. ie we would
4030 need two sets like this available at the start of the block:
4032 (set (reg X) (reg Y))
4033 (set (reg Y) (reg X))
4035 This can not happen since the set of (reg Y) would have killed the
4036 set of (reg X) making it unavailable at the start of this block. */
4037 while (1)
4039 rtx src;
4040 struct expr *set = lookup_set (regno, NULL_RTX);
4042 /* Find a set that is available at the start of the block
4043 which contains INSN. */
4044 while (set)
4046 if (TEST_BIT (cprop_avin[BLOCK_NUM (insn)], set->bitmap_index))
4047 break;
4048 set = next_set (regno, set);
4051 /* If no available set was found we've reached the end of the
4052 (possibly empty) copy chain. */
4053 if (set == 0)
4054 break;
4056 if (GET_CODE (set->expr) != SET)
4057 abort ();
4059 src = SET_SRC (set->expr);
4061 /* We know the set is available.
4062 Now check that SRC is ANTLOC (i.e. none of the source operands
4063 have changed since the start of the block).
4065 If the source operand changed, we may still use it for the next
4066 iteration of this loop, but we may not use it for substitutions. */
4068 if (CONSTANT_P (src) || oprs_not_set_p (src, insn))
4069 set1 = set;
4071 /* If the source of the set is anything except a register, then
4072 we have reached the end of the copy chain. */
4073 if (GET_CODE (src) != REG)
4074 break;
4076 /* Follow the copy chain, ie start another iteration of the loop
4077 and see if we have an available copy into SRC. */
4078 regno = REGNO (src);
4081 /* SET1 holds the last set that was available and anticipatable at
4082 INSN. */
4083 return set1;
4086 /* Subroutine of cprop_insn that tries to propagate constants into
4087 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
4088 it is the instruction that immediately preceeds JUMP, and must be a
4089 single SET of a register. FROM is what we will try to replace,
4090 SRC is the constant we will try to substitute for it. Returns nonzero
4091 if a change was made. */
4093 static int
4094 cprop_jump (bb, setcc, jump, from, src)
4095 basic_block bb;
4096 rtx setcc;
4097 rtx jump;
4098 rtx from;
4099 rtx src;
4101 rtx new, new_set;
4102 rtx set = pc_set (jump);
4104 /* First substitute in the INSN condition as the SET_SRC of the JUMP,
4105 then substitute that given values in this expanded JUMP. */
4106 if (setcc != NULL)
4108 rtx setcc_set = single_set (setcc);
4109 new_set = simplify_replace_rtx (SET_SRC (set),
4110 SET_DEST (setcc_set),
4111 SET_SRC (setcc_set));
4113 else
4114 new_set = set;
4116 new = simplify_replace_rtx (new_set, from, src);
4118 /* If no simplification can be made, then try the next
4119 register. */
4120 if (rtx_equal_p (new, new_set))
4121 return 0;
4123 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
4124 if (new == pc_rtx)
4125 delete_insn (jump);
4126 else
4128 if (! validate_change (jump, &SET_SRC (set), new, 0))
4129 return 0;
4131 /* If this has turned into an unconditional jump,
4132 then put a barrier after it so that the unreachable
4133 code will be deleted. */
4134 if (GET_CODE (SET_SRC (set)) == LABEL_REF)
4135 emit_barrier_after (jump);
4138 #ifdef HAVE_cc0
4139 /* Delete the cc0 setter. */
4140 if (setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
4141 delete_insn (setcc);
4142 #endif
4144 run_jump_opt_after_gcse = 1;
4146 const_prop_count++;
4147 if (gcse_file != NULL)
4149 fprintf (gcse_file,
4150 "CONST-PROP: Replacing reg %d in jump_insn %d with constant ",
4151 REGNO (from), INSN_UID (jump));
4152 print_rtl (gcse_file, src);
4153 fprintf (gcse_file, "\n");
4155 purge_dead_edges (bb);
4157 return 1;
4160 static bool
4161 constprop_register (insn, from, to, alter_jumps)
4162 rtx insn;
4163 rtx from;
4164 rtx to;
4165 int alter_jumps;
4167 rtx sset;
4169 /* Check for reg or cc0 setting instructions followed by
4170 conditional branch instructions first. */
4171 if (alter_jumps
4172 && (sset = single_set (insn)) != NULL
4173 && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
4175 rtx dest = SET_DEST (sset);
4176 if ((REG_P (dest) || CC0_P (dest))
4177 && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn), from, to))
4178 return 1;
4181 /* Handle normal insns next. */
4182 if (GET_CODE (insn) == INSN
4183 && try_replace_reg (from, to, insn))
4184 return 1;
4186 /* Try to propagate a CONST_INT into a conditional jump.
4187 We're pretty specific about what we will handle in this
4188 code, we can extend this as necessary over time.
4190 Right now the insn in question must look like
4191 (set (pc) (if_then_else ...)) */
4192 else if (alter_jumps && any_condjump_p (insn) && onlyjump_p (insn))
4193 return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, to);
4194 return 0;
4197 /* Perform constant and copy propagation on INSN.
4198 The result is non-zero if a change was made. */
4200 static int
4201 cprop_insn (insn, alter_jumps)
4202 rtx insn;
4203 int alter_jumps;
4205 struct reg_use *reg_used;
4206 int changed = 0;
4207 rtx note;
4209 if (!INSN_P (insn))
4210 return 0;
4212 reg_use_count = 0;
4213 note_uses (&PATTERN (insn), find_used_regs, NULL);
4215 note = find_reg_equal_equiv_note (insn);
4217 /* We may win even when propagating constants into notes. */
4218 if (note)
4219 find_used_regs (&XEXP (note, 0), NULL);
4221 for (reg_used = &reg_use_table[0]; reg_use_count > 0;
4222 reg_used++, reg_use_count--)
4224 unsigned int regno = REGNO (reg_used->reg_rtx);
4225 rtx pat, src;
4226 struct expr *set;
4228 /* Ignore registers created by GCSE.
4229 We do this because ... */
4230 if (regno >= max_gcse_regno)
4231 continue;
4233 /* If the register has already been set in this block, there's
4234 nothing we can do. */
4235 if (! oprs_not_set_p (reg_used->reg_rtx, insn))
4236 continue;
4238 /* Find an assignment that sets reg_used and is available
4239 at the start of the block. */
4240 set = find_avail_set (regno, insn);
4241 if (! set)
4242 continue;
4244 pat = set->expr;
4245 /* ??? We might be able to handle PARALLELs. Later. */
4246 if (GET_CODE (pat) != SET)
4247 abort ();
4249 src = SET_SRC (pat);
4251 /* Constant propagation. */
4252 if (CONSTANT_P (src))
4254 if (constprop_register (insn, reg_used->reg_rtx, src, alter_jumps))
4256 changed = 1;
4257 const_prop_count++;
4258 if (gcse_file != NULL)
4260 fprintf (gcse_file, "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
4261 fprintf (gcse_file, "insn %d with constant ", INSN_UID (insn));
4262 print_rtl (gcse_file, src);
4263 fprintf (gcse_file, "\n");
4267 else if (GET_CODE (src) == REG
4268 && REGNO (src) >= FIRST_PSEUDO_REGISTER
4269 && REGNO (src) != regno)
4271 if (try_replace_reg (reg_used->reg_rtx, src, insn))
4273 changed = 1;
4274 copy_prop_count++;
4275 if (gcse_file != NULL)
4277 fprintf (gcse_file, "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
4278 regno, INSN_UID (insn));
4279 fprintf (gcse_file, " with reg %d\n", REGNO (src));
4282 /* The original insn setting reg_used may or may not now be
4283 deletable. We leave the deletion to flow. */
4284 /* FIXME: If it turns out that the insn isn't deletable,
4285 then we may have unnecessarily extended register lifetimes
4286 and made things worse. */
4291 return changed;
4294 static bool
4295 do_local_cprop (x, insn, alter_jumps)
4296 rtx x;
4297 rtx insn;
4298 int alter_jumps;
4300 rtx newreg = NULL, newcnst = NULL;
4302 /* Rule out USE instructions and ASM statements as we don't want to change the hard
4303 registers mentioned. */
4304 if (GET_CODE (x) == REG
4305 && (REGNO (x) >= FIRST_PSEUDO_REGISTER
4306 || (GET_CODE (PATTERN (insn)) != USE && asm_noperands (PATTERN (insn)) < 0)))
4308 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0);
4309 struct elt_loc_list *l;
4311 if (!val)
4312 return false;
4313 for (l = val->locs; l; l = l->next)
4315 rtx this_rtx = l->loc;
4316 rtx note;
4318 if (CONSTANT_P (this_rtx))
4319 newcnst = this_rtx;
4320 if (REG_P (this_rtx) && REGNO (this_rtx) >= FIRST_PSEUDO_REGISTER
4321 /* Don't copy propagate if it has attached REG_EQUIV note.
4322 At this point this only function parameters should have
4323 REG_EQUIV notes and if the argument slot is used somewhere
4324 explicitly, it means address of parameter has been taken,
4325 so we should not extend the lifetime of the pseudo. */
4326 && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
4327 || GET_CODE (XEXP (note, 0)) != MEM))
4328 newreg = this_rtx;
4330 if (newcnst && constprop_register (insn, x, newcnst, alter_jumps))
4332 if (gcse_file != NULL)
4334 fprintf (gcse_file, "LOCAL CONST-PROP: Replacing reg %d in ",
4335 REGNO (x));
4336 fprintf (gcse_file, "insn %d with constant ",
4337 INSN_UID (insn));
4338 print_rtl (gcse_file, newcnst);
4339 fprintf (gcse_file, "\n");
4341 const_prop_count++;
4342 return true;
4344 else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
4346 if (gcse_file != NULL)
4348 fprintf (gcse_file,
4349 "LOCAL COPY-PROP: Replacing reg %d in insn %d",
4350 REGNO (x), INSN_UID (insn));
4351 fprintf (gcse_file, " with reg %d\n", REGNO (newreg));
4353 copy_prop_count++;
4354 return true;
4357 return false;
4360 static void
4361 local_cprop_pass (alter_jumps)
4362 int alter_jumps;
4364 rtx insn;
4365 struct reg_use *reg_used;
4367 cselib_init ();
4368 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4370 if (INSN_P (insn))
4372 rtx note = find_reg_equal_equiv_note (insn);
4376 reg_use_count = 0;
4377 note_uses (&PATTERN (insn), find_used_regs, NULL);
4378 if (note)
4379 find_used_regs (&XEXP (note, 0), NULL);
4381 for (reg_used = &reg_use_table[0]; reg_use_count > 0;
4382 reg_used++, reg_use_count--)
4383 if (do_local_cprop (reg_used->reg_rtx, insn, alter_jumps))
4384 break;
4386 while (reg_use_count);
4388 cselib_process_insn (insn);
4390 cselib_finish ();
4393 /* Forward propagate copies. This includes copies and constants. Return
4394 non-zero if a change was made. */
4396 static int
4397 cprop (alter_jumps)
4398 int alter_jumps;
4400 int changed;
4401 basic_block bb;
4402 rtx insn;
4404 /* Note we start at block 1. */
4405 if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
4407 if (gcse_file != NULL)
4408 fprintf (gcse_file, "\n");
4409 return 0;
4412 changed = 0;
4413 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb, EXIT_BLOCK_PTR, next_bb)
4415 /* Reset tables used to keep track of what's still valid [since the
4416 start of the block]. */
4417 reset_opr_set_tables ();
4419 for (insn = bb->head;
4420 insn != NULL && insn != NEXT_INSN (bb->end);
4421 insn = NEXT_INSN (insn))
4422 if (INSN_P (insn))
4424 changed |= cprop_insn (insn, alter_jumps);
4426 /* Keep track of everything modified by this insn. */
4427 /* ??? Need to be careful w.r.t. mods done to INSN. Don't
4428 call mark_oprs_set if we turned the insn into a NOTE. */
4429 if (GET_CODE (insn) != NOTE)
4430 mark_oprs_set (insn);
4434 if (gcse_file != NULL)
4435 fprintf (gcse_file, "\n");
4437 return changed;
4440 /* Perform one copy/constant propagation pass.
4441 F is the first insn in the function.
4442 PASS is the pass count. */
4444 static int
4445 one_cprop_pass (pass, alter_jumps)
4446 int pass;
4447 int alter_jumps;
4449 int changed = 0;
4451 const_prop_count = 0;
4452 copy_prop_count = 0;
4454 local_cprop_pass (alter_jumps);
4456 alloc_set_hash_table (max_cuid);
4457 compute_set_hash_table ();
4458 if (gcse_file)
4459 dump_hash_table (gcse_file, "SET", set_hash_table, set_hash_table_size,
4460 n_sets);
4461 if (n_sets > 0)
4463 alloc_cprop_mem (last_basic_block, n_sets);
4464 compute_cprop_data ();
4465 changed = cprop (alter_jumps);
4466 if (alter_jumps)
4467 changed |= bypass_conditional_jumps ();
4468 free_cprop_mem ();
4471 free_set_hash_table ();
4473 if (gcse_file)
4475 fprintf (gcse_file, "CPROP of %s, pass %d: %d bytes needed, ",
4476 current_function_name, pass, bytes_used);
4477 fprintf (gcse_file, "%d const props, %d copy props\n\n",
4478 const_prop_count, copy_prop_count);
4481 return changed;
4484 /* Bypass conditional jumps. */
4486 /* Find a set of REGNO to a constant that is available at the end of basic
4487 block BB. Returns NULL if no such set is found. Based heavily upon
4488 find_avail_set. */
4490 static struct expr *
4491 find_bypass_set (regno, bb)
4492 int regno;
4493 int bb;
4495 struct expr *result = 0;
4497 for (;;)
4499 rtx src;
4500 struct expr *set = lookup_set (regno, NULL_RTX);
4502 while (set)
4504 if (TEST_BIT (cprop_avout[bb], set->bitmap_index))
4505 break;
4506 set = next_set (regno, set);
4509 if (set == 0)
4510 break;
4512 if (GET_CODE (set->expr) != SET)
4513 abort ();
4515 src = SET_SRC (set->expr);
4516 if (CONSTANT_P (src))
4517 result = set;
4519 if (GET_CODE (src) != REG)
4520 break;
4522 regno = REGNO (src);
4524 return result;
4528 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
4529 basic block BB which has more than one predecessor. If not NULL, SETCC
4530 is the first instruction of BB, which is immediately followed by JUMP_INSN
4531 JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
4532 Returns nonzero if a change was made. */
4534 static int
4535 bypass_block (bb, setcc, jump)
4536 basic_block bb;
4537 rtx setcc, jump;
4539 rtx insn, note;
4540 edge e, enext;
4541 int i, change;
4543 insn = (setcc != NULL) ? setcc : jump;
4545 /* Determine set of register uses in INSN. */
4546 reg_use_count = 0;
4547 note_uses (&PATTERN (insn), find_used_regs, NULL);
4548 note = find_reg_equal_equiv_note (insn);
4549 if (note)
4550 find_used_regs (&XEXP (note, 0), NULL);
4552 change = 0;
4553 for (e = bb->pred; e; e = enext)
4555 enext = e->pred_next;
4556 for (i = 0; i < reg_use_count; i++)
4558 struct reg_use *reg_used = &reg_use_table[i];
4559 unsigned int regno = REGNO (reg_used->reg_rtx);
4560 basic_block dest, old_dest;
4561 struct expr *set;
4562 rtx src, new;
4564 if (regno >= max_gcse_regno)
4565 continue;
4567 set = find_bypass_set (regno, e->src->index);
4569 if (! set)
4570 continue;
4572 src = SET_SRC (pc_set (jump));
4574 if (setcc != NULL)
4575 src = simplify_replace_rtx (src,
4576 SET_DEST (PATTERN (setcc)),
4577 SET_SRC (PATTERN (setcc)));
4579 new = simplify_replace_rtx (src, reg_used->reg_rtx,
4580 SET_SRC (set->expr));
4582 if (new == pc_rtx)
4583 dest = FALLTHRU_EDGE (bb)->dest;
4584 else if (GET_CODE (new) == LABEL_REF)
4585 dest = BRANCH_EDGE (bb)->dest;
4586 else
4587 dest = NULL;
4589 /* Once basic block indices are stable, we should be able
4590 to use redirect_edge_and_branch_force instead. */
4591 old_dest = e->dest;
4592 if (dest != NULL && dest != old_dest
4593 && redirect_edge_and_branch (e, dest))
4595 /* Copy the register setter to the redirected edge.
4596 Don't copy CC0 setters, as CC0 is dead after jump. */
4597 if (setcc)
4599 rtx pat = PATTERN (setcc);
4600 if (!CC0_P (SET_DEST (pat)))
4601 insert_insn_on_edge (copy_insn (pat), e);
4604 if (gcse_file != NULL)
4606 fprintf (gcse_file, "JUMP-BYPASS: Proved reg %d in jump_insn %d equals constant ",
4607 regno, INSN_UID (jump));
4608 print_rtl (gcse_file, SET_SRC (set->expr));
4609 fprintf (gcse_file, "\nBypass edge from %d->%d to %d\n",
4610 e->src->index, old_dest->index, dest->index);
4612 change = 1;
4613 break;
4617 return change;
4620 /* Find basic blocks with more than one predecessor that only contain a
4621 single conditional jump. If the result of the comparison is known at
4622 compile-time from any incoming edge, redirect that edge to the
4623 appropriate target. Returns nonzero if a change was made. */
4625 static int
4626 bypass_conditional_jumps ()
4628 basic_block bb;
4629 int changed;
4630 rtx setcc;
4631 rtx insn;
4632 rtx dest;
4634 /* Note we start at block 1. */
4635 if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
4636 return 0;
4638 changed = 0;
4639 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb,
4640 EXIT_BLOCK_PTR, next_bb)
4642 /* Check for more than one predecessor. */
4643 if (bb->pred && bb->pred->pred_next)
4645 setcc = NULL_RTX;
4646 for (insn = bb->head;
4647 insn != NULL && insn != NEXT_INSN (bb->end);
4648 insn = NEXT_INSN (insn))
4649 if (GET_CODE (insn) == INSN)
4651 if (setcc)
4652 break;
4653 if (GET_CODE (PATTERN (insn)) != SET)
4654 break;
4656 dest = SET_DEST (PATTERN (insn));
4657 if (REG_P (dest) || CC0_P (dest))
4658 setcc = insn;
4659 else
4660 break;
4662 else if (GET_CODE (insn) == JUMP_INSN)
4664 if (any_condjump_p (insn) && onlyjump_p (insn))
4665 changed |= bypass_block (bb, setcc, insn);
4666 break;
4668 else if (INSN_P (insn))
4669 break;
4673 /* If we bypassed any register setting insns, we inserted a
4674 copy on the redirected edge. These need to be commited. */
4675 if (changed)
4676 commit_edge_insertions();
4678 return changed;
4681 /* Compute PRE+LCM working variables. */
4683 /* Local properties of expressions. */
4684 /* Nonzero for expressions that are transparent in the block. */
4685 static sbitmap *transp;
4687 /* Nonzero for expressions that are transparent at the end of the block.
4688 This is only zero for expressions killed by abnormal critical edge
4689 created by a calls. */
4690 static sbitmap *transpout;
4692 /* Nonzero for expressions that are computed (available) in the block. */
4693 static sbitmap *comp;
4695 /* Nonzero for expressions that are locally anticipatable in the block. */
4696 static sbitmap *antloc;
4698 /* Nonzero for expressions where this block is an optimal computation
4699 point. */
4700 static sbitmap *pre_optimal;
4702 /* Nonzero for expressions which are redundant in a particular block. */
4703 static sbitmap *pre_redundant;
4705 /* Nonzero for expressions which should be inserted on a specific edge. */
4706 static sbitmap *pre_insert_map;
4708 /* Nonzero for expressions which should be deleted in a specific block. */
4709 static sbitmap *pre_delete_map;
4711 /* Contains the edge_list returned by pre_edge_lcm. */
4712 static struct edge_list *edge_list;
4714 /* Redundant insns. */
4715 static sbitmap pre_redundant_insns;
4717 /* Allocate vars used for PRE analysis. */
4719 static void
4720 alloc_pre_mem (n_blocks, n_exprs)
4721 int n_blocks, n_exprs;
4723 transp = sbitmap_vector_alloc (n_blocks, n_exprs);
4724 comp = sbitmap_vector_alloc (n_blocks, n_exprs);
4725 antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
4727 pre_optimal = NULL;
4728 pre_redundant = NULL;
4729 pre_insert_map = NULL;
4730 pre_delete_map = NULL;
4731 ae_in = NULL;
4732 ae_out = NULL;
4733 ae_kill = sbitmap_vector_alloc (n_blocks, n_exprs);
4735 /* pre_insert and pre_delete are allocated later. */
4738 /* Free vars used for PRE analysis. */
4740 static void
4741 free_pre_mem ()
4743 sbitmap_vector_free (transp);
4744 sbitmap_vector_free (comp);
4746 /* ANTLOC and AE_KILL are freed just after pre_lcm finishes. */
4748 if (pre_optimal)
4749 sbitmap_vector_free (pre_optimal);
4750 if (pre_redundant)
4751 sbitmap_vector_free (pre_redundant);
4752 if (pre_insert_map)
4753 sbitmap_vector_free (pre_insert_map);
4754 if (pre_delete_map)
4755 sbitmap_vector_free (pre_delete_map);
4756 if (ae_in)
4757 sbitmap_vector_free (ae_in);
4758 if (ae_out)
4759 sbitmap_vector_free (ae_out);
4761 transp = comp = NULL;
4762 pre_optimal = pre_redundant = pre_insert_map = pre_delete_map = NULL;
4763 ae_in = ae_out = NULL;
4766 /* Top level routine to do the dataflow analysis needed by PRE. */
4768 static void
4769 compute_pre_data ()
4771 sbitmap trapping_expr;
4772 basic_block bb;
4773 unsigned int ui;
4775 compute_local_properties (transp, comp, antloc, 0);
4776 sbitmap_vector_zero (ae_kill, last_basic_block);
4778 /* Collect expressions which might trap. */
4779 trapping_expr = sbitmap_alloc (n_exprs);
4780 sbitmap_zero (trapping_expr);
4781 for (ui = 0; ui < expr_hash_table_size; ui++)
4783 struct expr *e;
4784 for (e = expr_hash_table[ui]; e != NULL; e = e->next_same_hash)
4785 if (may_trap_p (e->expr))
4786 SET_BIT (trapping_expr, e->bitmap_index);
4789 /* Compute ae_kill for each basic block using:
4791 ~(TRANSP | COMP)
4793 This is significantly faster than compute_ae_kill. */
4795 FOR_EACH_BB (bb)
4797 edge e;
4799 /* If the current block is the destination of an abnormal edge, we
4800 kill all trapping expressions because we won't be able to properly
4801 place the instruction on the edge. So make them neither
4802 anticipatable nor transparent. This is fairly conservative. */
4803 for (e = bb->pred; e ; e = e->pred_next)
4804 if (e->flags & EDGE_ABNORMAL)
4806 sbitmap_difference (antloc[bb->index], antloc[bb->index], trapping_expr);
4807 sbitmap_difference (transp[bb->index], transp[bb->index], trapping_expr);
4808 break;
4811 sbitmap_a_or_b (ae_kill[bb->index], transp[bb->index], comp[bb->index]);
4812 sbitmap_not (ae_kill[bb->index], ae_kill[bb->index]);
4815 edge_list = pre_edge_lcm (gcse_file, n_exprs, transp, comp, antloc,
4816 ae_kill, &pre_insert_map, &pre_delete_map);
4817 sbitmap_vector_free (antloc);
4818 antloc = NULL;
4819 sbitmap_vector_free (ae_kill);
4820 ae_kill = NULL;
4821 sbitmap_free (trapping_expr);
4824 /* PRE utilities */
4826 /* Return non-zero if an occurrence of expression EXPR in OCCR_BB would reach
4827 block BB.
4829 VISITED is a pointer to a working buffer for tracking which BB's have
4830 been visited. It is NULL for the top-level call.
4832 We treat reaching expressions that go through blocks containing the same
4833 reaching expression as "not reaching". E.g. if EXPR is generated in blocks
4834 2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
4835 2 as not reaching. The intent is to improve the probability of finding
4836 only one reaching expression and to reduce register lifetimes by picking
4837 the closest such expression. */
4839 static int
4840 pre_expr_reaches_here_p_work (occr_bb, expr, bb, visited)
4841 basic_block occr_bb;
4842 struct expr *expr;
4843 basic_block bb;
4844 char *visited;
4846 edge pred;
4848 for (pred = bb->pred; pred != NULL; pred = pred->pred_next)
4850 basic_block pred_bb = pred->src;
4852 if (pred->src == ENTRY_BLOCK_PTR
4853 /* Has predecessor has already been visited? */
4854 || visited[pred_bb->index])
4855 ;/* Nothing to do. */
4857 /* Does this predecessor generate this expression? */
4858 else if (TEST_BIT (comp[pred_bb->index], expr->bitmap_index))
4860 /* Is this the occurrence we're looking for?
4861 Note that there's only one generating occurrence per block
4862 so we just need to check the block number. */
4863 if (occr_bb == pred_bb)
4864 return 1;
4866 visited[pred_bb->index] = 1;
4868 /* Ignore this predecessor if it kills the expression. */
4869 else if (! TEST_BIT (transp[pred_bb->index], expr->bitmap_index))
4870 visited[pred_bb->index] = 1;
4872 /* Neither gen nor kill. */
4873 else
4875 visited[pred_bb->index] = 1;
4876 if (pre_expr_reaches_here_p_work (occr_bb, expr, pred_bb, visited))
4877 return 1;
4881 /* All paths have been checked. */
4882 return 0;
4885 /* The wrapper for pre_expr_reaches_here_work that ensures that any
4886 memory allocated for that function is returned. */
4888 static int
4889 pre_expr_reaches_here_p (occr_bb, expr, bb)
4890 basic_block occr_bb;
4891 struct expr *expr;
4892 basic_block bb;
4894 int rval;
4895 char *visited = (char *) xcalloc (last_basic_block, 1);
4897 rval = pre_expr_reaches_here_p_work (occr_bb, expr, bb, visited);
4899 free (visited);
4900 return rval;
4904 /* Given an expr, generate RTL which we can insert at the end of a BB,
4905 or on an edge. Set the block number of any insns generated to
4906 the value of BB. */
4908 static rtx
4909 process_insert_insn (expr)
4910 struct expr *expr;
4912 rtx reg = expr->reaching_reg;
4913 rtx exp = copy_rtx (expr->expr);
4914 rtx pat;
4916 start_sequence ();
4918 /* If the expression is something that's an operand, like a constant,
4919 just copy it to a register. */
4920 if (general_operand (exp, GET_MODE (reg)))
4921 emit_move_insn (reg, exp);
4923 /* Otherwise, make a new insn to compute this expression and make sure the
4924 insn will be recognized (this also adds any needed CLOBBERs). Copy the
4925 expression to make sure we don't have any sharing issues. */
4926 else if (insn_invalid_p (emit_insn (gen_rtx_SET (VOIDmode, reg, exp))))
4927 abort ();
4929 pat = get_insns ();
4930 end_sequence ();
4932 return pat;
4935 /* Add EXPR to the end of basic block BB.
4937 This is used by both the PRE and code hoisting.
4939 For PRE, we want to verify that the expr is either transparent
4940 or locally anticipatable in the target block. This check makes
4941 no sense for code hoisting. */
4943 static void
4944 insert_insn_end_bb (expr, bb, pre)
4945 struct expr *expr;
4946 basic_block bb;
4947 int pre;
4949 rtx insn = bb->end;
4950 rtx new_insn;
4951 rtx reg = expr->reaching_reg;
4952 int regno = REGNO (reg);
4953 rtx pat, pat_end;
4955 pat = process_insert_insn (expr);
4956 if (pat == NULL_RTX || ! INSN_P (pat))
4957 abort ();
4959 pat_end = pat;
4960 while (NEXT_INSN (pat_end) != NULL_RTX)
4961 pat_end = NEXT_INSN (pat_end);
4963 /* If the last insn is a jump, insert EXPR in front [taking care to
4964 handle cc0, etc. properly]. Similary we need to care trapping
4965 instructions in presence of non-call exceptions. */
4967 if (GET_CODE (insn) == JUMP_INSN
4968 || (GET_CODE (insn) == INSN
4969 && (bb->succ->succ_next || (bb->succ->flags & EDGE_ABNORMAL))))
4971 #ifdef HAVE_cc0
4972 rtx note;
4973 #endif
4974 /* It should always be the case that we can put these instructions
4975 anywhere in the basic block with performing PRE optimizations.
4976 Check this. */
4977 if (GET_CODE (insn) == INSN && pre
4978 && !TEST_BIT (antloc[bb->index], expr->bitmap_index)
4979 && !TEST_BIT (transp[bb->index], expr->bitmap_index))
4980 abort ();
4982 /* If this is a jump table, then we can't insert stuff here. Since
4983 we know the previous real insn must be the tablejump, we insert
4984 the new instruction just before the tablejump. */
4985 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
4986 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
4987 insn = prev_real_insn (insn);
4989 #ifdef HAVE_cc0
4990 /* FIXME: 'twould be nice to call prev_cc0_setter here but it aborts
4991 if cc0 isn't set. */
4992 note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
4993 if (note)
4994 insn = XEXP (note, 0);
4995 else
4997 rtx maybe_cc0_setter = prev_nonnote_insn (insn);
4998 if (maybe_cc0_setter
4999 && INSN_P (maybe_cc0_setter)
5000 && sets_cc0_p (PATTERN (maybe_cc0_setter)))
5001 insn = maybe_cc0_setter;
5003 #endif
5004 /* FIXME: What if something in cc0/jump uses value set in new insn? */
5005 new_insn = emit_insn_before (pat, insn);
5008 /* Likewise if the last insn is a call, as will happen in the presence
5009 of exception handling. */
5010 else if (GET_CODE (insn) == CALL_INSN
5011 && (bb->succ->succ_next || (bb->succ->flags & EDGE_ABNORMAL)))
5013 /* Keeping in mind SMALL_REGISTER_CLASSES and parameters in registers,
5014 we search backward and place the instructions before the first
5015 parameter is loaded. Do this for everyone for consistency and a
5016 presumtion that we'll get better code elsewhere as well.
5018 It should always be the case that we can put these instructions
5019 anywhere in the basic block with performing PRE optimizations.
5020 Check this. */
5022 if (pre
5023 && !TEST_BIT (antloc[bb->index], expr->bitmap_index)
5024 && !TEST_BIT (transp[bb->index], expr->bitmap_index))
5025 abort ();
5027 /* Since different machines initialize their parameter registers
5028 in different orders, assume nothing. Collect the set of all
5029 parameter registers. */
5030 insn = find_first_parameter_load (insn, bb->head);
5032 /* If we found all the parameter loads, then we want to insert
5033 before the first parameter load.
5035 If we did not find all the parameter loads, then we might have
5036 stopped on the head of the block, which could be a CODE_LABEL.
5037 If we inserted before the CODE_LABEL, then we would be putting
5038 the insn in the wrong basic block. In that case, put the insn
5039 after the CODE_LABEL. Also, respect NOTE_INSN_BASIC_BLOCK. */
5040 while (GET_CODE (insn) == CODE_LABEL
5041 || NOTE_INSN_BASIC_BLOCK_P (insn))
5042 insn = NEXT_INSN (insn);
5044 new_insn = emit_insn_before (pat, insn);
5046 else
5047 new_insn = emit_insn_after (pat, insn);
5049 while (1)
5051 if (INSN_P (pat))
5053 add_label_notes (PATTERN (pat), new_insn);
5054 note_stores (PATTERN (pat), record_set_info, pat);
5056 if (pat == pat_end)
5057 break;
5058 pat = NEXT_INSN (pat);
5061 gcse_create_count++;
5063 if (gcse_file)
5065 fprintf (gcse_file, "PRE/HOIST: end of bb %d, insn %d, ",
5066 bb->index, INSN_UID (new_insn));
5067 fprintf (gcse_file, "copying expression %d to reg %d\n",
5068 expr->bitmap_index, regno);
5072 /* Insert partially redundant expressions on edges in the CFG to make
5073 the expressions fully redundant. */
5075 static int
5076 pre_edge_insert (edge_list, index_map)
5077 struct edge_list *edge_list;
5078 struct expr **index_map;
5080 int e, i, j, num_edges, set_size, did_insert = 0;
5081 sbitmap *inserted;
5083 /* Where PRE_INSERT_MAP is nonzero, we add the expression on that edge
5084 if it reaches any of the deleted expressions. */
5086 set_size = pre_insert_map[0]->size;
5087 num_edges = NUM_EDGES (edge_list);
5088 inserted = sbitmap_vector_alloc (num_edges, n_exprs);
5089 sbitmap_vector_zero (inserted, num_edges);
5091 for (e = 0; e < num_edges; e++)
5093 int indx;
5094 basic_block bb = INDEX_EDGE_PRED_BB (edge_list, e);
5096 for (i = indx = 0; i < set_size; i++, indx += SBITMAP_ELT_BITS)
5098 SBITMAP_ELT_TYPE insert = pre_insert_map[e]->elms[i];
5100 for (j = indx; insert && j < n_exprs; j++, insert >>= 1)
5101 if ((insert & 1) != 0 && index_map[j]->reaching_reg != NULL_RTX)
5103 struct expr *expr = index_map[j];
5104 struct occr *occr;
5106 /* Now look at each deleted occurrence of this expression. */
5107 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
5109 if (! occr->deleted_p)
5110 continue;
5112 /* Insert this expression on this edge if if it would
5113 reach the deleted occurrence in BB. */
5114 if (!TEST_BIT (inserted[e], j))
5116 rtx insn;
5117 edge eg = INDEX_EDGE (edge_list, e);
5119 /* We can't insert anything on an abnormal and
5120 critical edge, so we insert the insn at the end of
5121 the previous block. There are several alternatives
5122 detailed in Morgans book P277 (sec 10.5) for
5123 handling this situation. This one is easiest for
5124 now. */
5126 if ((eg->flags & EDGE_ABNORMAL) == EDGE_ABNORMAL)
5127 insert_insn_end_bb (index_map[j], bb, 0);
5128 else
5130 insn = process_insert_insn (index_map[j]);
5131 insert_insn_on_edge (insn, eg);
5134 if (gcse_file)
5136 fprintf (gcse_file, "PRE/HOIST: edge (%d,%d), ",
5137 bb->index,
5138 INDEX_EDGE_SUCC_BB (edge_list, e)->index);
5139 fprintf (gcse_file, "copy expression %d\n",
5140 expr->bitmap_index);
5143 update_ld_motion_stores (expr);
5144 SET_BIT (inserted[e], j);
5145 did_insert = 1;
5146 gcse_create_count++;
5153 sbitmap_vector_free (inserted);
5154 return did_insert;
5157 /* Copy the result of INSN to REG. INDX is the expression number. */
5159 static void
5160 pre_insert_copy_insn (expr, insn)
5161 struct expr *expr;
5162 rtx insn;
5164 rtx reg = expr->reaching_reg;
5165 int regno = REGNO (reg);
5166 int indx = expr->bitmap_index;
5167 rtx set = single_set (insn);
5168 rtx new_insn;
5170 if (!set)
5171 abort ();
5173 new_insn = emit_insn_after (gen_move_insn (reg, SET_DEST (set)), insn);
5175 /* Keep register set table up to date. */
5176 record_one_set (regno, new_insn);
5178 gcse_create_count++;
5180 if (gcse_file)
5181 fprintf (gcse_file,
5182 "PRE: bb %d, insn %d, copy expression %d in insn %d to reg %d\n",
5183 BLOCK_NUM (insn), INSN_UID (new_insn), indx,
5184 INSN_UID (insn), regno);
5185 update_ld_motion_stores (expr);
5188 /* Copy available expressions that reach the redundant expression
5189 to `reaching_reg'. */
5191 static void
5192 pre_insert_copies ()
5194 unsigned int i;
5195 struct expr *expr;
5196 struct occr *occr;
5197 struct occr *avail;
5199 /* For each available expression in the table, copy the result to
5200 `reaching_reg' if the expression reaches a deleted one.
5202 ??? The current algorithm is rather brute force.
5203 Need to do some profiling. */
5205 for (i = 0; i < expr_hash_table_size; i++)
5206 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
5208 /* If the basic block isn't reachable, PPOUT will be TRUE. However,
5209 we don't want to insert a copy here because the expression may not
5210 really be redundant. So only insert an insn if the expression was
5211 deleted. This test also avoids further processing if the
5212 expression wasn't deleted anywhere. */
5213 if (expr->reaching_reg == NULL)
5214 continue;
5216 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
5218 if (! occr->deleted_p)
5219 continue;
5221 for (avail = expr->avail_occr; avail != NULL; avail = avail->next)
5223 rtx insn = avail->insn;
5225 /* No need to handle this one if handled already. */
5226 if (avail->copied_p)
5227 continue;
5229 /* Don't handle this one if it's a redundant one. */
5230 if (TEST_BIT (pre_redundant_insns, INSN_CUID (insn)))
5231 continue;
5233 /* Or if the expression doesn't reach the deleted one. */
5234 if (! pre_expr_reaches_here_p (BLOCK_FOR_INSN (avail->insn),
5235 expr,
5236 BLOCK_FOR_INSN (occr->insn)))
5237 continue;
5239 /* Copy the result of avail to reaching_reg. */
5240 pre_insert_copy_insn (expr, insn);
5241 avail->copied_p = 1;
5247 /* Emit move from SRC to DEST noting the equivalence with expression computed
5248 in INSN. */
5249 static rtx
5250 gcse_emit_move_after (src, dest, insn)
5251 rtx src, dest, insn;
5253 rtx new;
5254 rtx set = single_set (insn), set2;
5255 rtx note;
5256 rtx eqv;
5258 /* This should never fail since we're creating a reg->reg copy
5259 we've verified to be valid. */
5261 new = emit_insn_after (gen_move_insn (dest, src), insn);
5263 /* Note the equivalence for local CSE pass. */
5264 set2 = single_set (new);
5265 if (!set2 || !rtx_equal_p (SET_DEST (set2), dest))
5266 return new;
5267 if ((note = find_reg_equal_equiv_note (insn)))
5268 eqv = XEXP (note, 0);
5269 else
5270 eqv = SET_SRC (set);
5272 set_unique_reg_note (new, REG_EQUAL, copy_insn_1 (src));
5274 return new;
5277 /* Delete redundant computations.
5278 Deletion is done by changing the insn to copy the `reaching_reg' of
5279 the expression into the result of the SET. It is left to later passes
5280 (cprop, cse2, flow, combine, regmove) to propagate the copy or eliminate it.
5282 Returns non-zero if a change is made. */
5284 static int
5285 pre_delete ()
5287 unsigned int i;
5288 int changed;
5289 struct expr *expr;
5290 struct occr *occr;
5292 changed = 0;
5293 for (i = 0; i < expr_hash_table_size; i++)
5294 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
5296 int indx = expr->bitmap_index;
5298 /* We only need to search antic_occr since we require
5299 ANTLOC != 0. */
5301 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
5303 rtx insn = occr->insn;
5304 rtx set;
5305 basic_block bb = BLOCK_FOR_INSN (insn);
5307 if (TEST_BIT (pre_delete_map[bb->index], indx))
5309 set = single_set (insn);
5310 if (! set)
5311 abort ();
5313 /* Create a pseudo-reg to store the result of reaching
5314 expressions into. Get the mode for the new pseudo from
5315 the mode of the original destination pseudo. */
5316 if (expr->reaching_reg == NULL)
5317 expr->reaching_reg
5318 = gen_reg_rtx (GET_MODE (SET_DEST (set)));
5320 gcse_emit_move_after (expr->reaching_reg, SET_DEST (set), insn);
5321 delete_insn (insn);
5322 occr->deleted_p = 1;
5323 SET_BIT (pre_redundant_insns, INSN_CUID (insn));
5324 changed = 1;
5325 gcse_subst_count++;
5327 if (gcse_file)
5329 fprintf (gcse_file,
5330 "PRE: redundant insn %d (expression %d) in ",
5331 INSN_UID (insn), indx);
5332 fprintf (gcse_file, "bb %d, reaching reg is %d\n",
5333 bb->index, REGNO (expr->reaching_reg));
5339 return changed;
5342 /* Perform GCSE optimizations using PRE.
5343 This is called by one_pre_gcse_pass after all the dataflow analysis
5344 has been done.
5346 This is based on the original Morel-Renvoise paper Fred Chow's thesis, and
5347 lazy code motion from Knoop, Ruthing and Steffen as described in Advanced
5348 Compiler Design and Implementation.
5350 ??? A new pseudo reg is created to hold the reaching expression. The nice
5351 thing about the classical approach is that it would try to use an existing
5352 reg. If the register can't be adequately optimized [i.e. we introduce
5353 reload problems], one could add a pass here to propagate the new register
5354 through the block.
5356 ??? We don't handle single sets in PARALLELs because we're [currently] not
5357 able to copy the rest of the parallel when we insert copies to create full
5358 redundancies from partial redundancies. However, there's no reason why we
5359 can't handle PARALLELs in the cases where there are no partial
5360 redundancies. */
5362 static int
5363 pre_gcse ()
5365 unsigned int i;
5366 int did_insert, changed;
5367 struct expr **index_map;
5368 struct expr *expr;
5370 /* Compute a mapping from expression number (`bitmap_index') to
5371 hash table entry. */
5373 index_map = (struct expr **) xcalloc (n_exprs, sizeof (struct expr *));
5374 for (i = 0; i < expr_hash_table_size; i++)
5375 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
5376 index_map[expr->bitmap_index] = expr;
5378 /* Reset bitmap used to track which insns are redundant. */
5379 pre_redundant_insns = sbitmap_alloc (max_cuid);
5380 sbitmap_zero (pre_redundant_insns);
5382 /* Delete the redundant insns first so that
5383 - we know what register to use for the new insns and for the other
5384 ones with reaching expressions
5385 - we know which insns are redundant when we go to create copies */
5387 changed = pre_delete ();
5389 did_insert = pre_edge_insert (edge_list, index_map);
5391 /* In other places with reaching expressions, copy the expression to the
5392 specially allocated pseudo-reg that reaches the redundant expr. */
5393 pre_insert_copies ();
5394 if (did_insert)
5396 commit_edge_insertions ();
5397 changed = 1;
5400 free (index_map);
5401 sbitmap_free (pre_redundant_insns);
5402 return changed;
5405 /* Top level routine to perform one PRE GCSE pass.
5407 Return non-zero if a change was made. */
5409 static int
5410 one_pre_gcse_pass (pass)
5411 int pass;
5413 int changed = 0;
5415 gcse_subst_count = 0;
5416 gcse_create_count = 0;
5418 alloc_expr_hash_table (max_cuid);
5419 add_noreturn_fake_exit_edges ();
5420 if (flag_gcse_lm)
5421 compute_ld_motion_mems ();
5423 compute_expr_hash_table ();
5424 trim_ld_motion_mems ();
5425 if (gcse_file)
5426 dump_hash_table (gcse_file, "Expression", expr_hash_table,
5427 expr_hash_table_size, n_exprs);
5429 if (n_exprs > 0)
5431 alloc_pre_mem (last_basic_block, n_exprs);
5432 compute_pre_data ();
5433 changed |= pre_gcse ();
5434 free_edge_list (edge_list);
5435 free_pre_mem ();
5438 free_ldst_mems ();
5439 remove_fake_edges ();
5440 free_expr_hash_table ();
5442 if (gcse_file)
5444 fprintf (gcse_file, "\nPRE GCSE of %s, pass %d: %d bytes needed, ",
5445 current_function_name, pass, bytes_used);
5446 fprintf (gcse_file, "%d substs, %d insns created\n",
5447 gcse_subst_count, gcse_create_count);
5450 return changed;
5453 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to INSN.
5454 If notes are added to an insn which references a CODE_LABEL, the
5455 LABEL_NUSES count is incremented. We have to add REG_LABEL notes,
5456 because the following loop optimization pass requires them. */
5458 /* ??? This is very similar to the loop.c add_label_notes function. We
5459 could probably share code here. */
5461 /* ??? If there was a jump optimization pass after gcse and before loop,
5462 then we would not need to do this here, because jump would add the
5463 necessary REG_LABEL notes. */
5465 static void
5466 add_label_notes (x, insn)
5467 rtx x;
5468 rtx insn;
5470 enum rtx_code code = GET_CODE (x);
5471 int i, j;
5472 const char *fmt;
5474 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
5476 /* This code used to ignore labels that referred to dispatch tables to
5477 avoid flow generating (slighly) worse code.
5479 We no longer ignore such label references (see LABEL_REF handling in
5480 mark_jump_label for additional information). */
5482 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, XEXP (x, 0),
5483 REG_NOTES (insn));
5484 if (LABEL_P (XEXP (x, 0)))
5485 LABEL_NUSES (XEXP (x, 0))++;
5486 return;
5489 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
5491 if (fmt[i] == 'e')
5492 add_label_notes (XEXP (x, i), insn);
5493 else if (fmt[i] == 'E')
5494 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5495 add_label_notes (XVECEXP (x, i, j), insn);
5499 /* Compute transparent outgoing information for each block.
5501 An expression is transparent to an edge unless it is killed by
5502 the edge itself. This can only happen with abnormal control flow,
5503 when the edge is traversed through a call. This happens with
5504 non-local labels and exceptions.
5506 This would not be necessary if we split the edge. While this is
5507 normally impossible for abnormal critical edges, with some effort
5508 it should be possible with exception handling, since we still have
5509 control over which handler should be invoked. But due to increased
5510 EH table sizes, this may not be worthwhile. */
5512 static void
5513 compute_transpout ()
5515 basic_block bb;
5516 unsigned int i;
5517 struct expr *expr;
5519 sbitmap_vector_ones (transpout, last_basic_block);
5521 FOR_EACH_BB (bb)
5523 /* Note that flow inserted a nop a the end of basic blocks that
5524 end in call instructions for reasons other than abnormal
5525 control flow. */
5526 if (GET_CODE (bb->end) != CALL_INSN)
5527 continue;
5529 for (i = 0; i < expr_hash_table_size; i++)
5530 for (expr = expr_hash_table[i]; expr ; expr = expr->next_same_hash)
5531 if (GET_CODE (expr->expr) == MEM)
5533 if (GET_CODE (XEXP (expr->expr, 0)) == SYMBOL_REF
5534 && CONSTANT_POOL_ADDRESS_P (XEXP (expr->expr, 0)))
5535 continue;
5537 /* ??? Optimally, we would use interprocedural alias
5538 analysis to determine if this mem is actually killed
5539 by this call. */
5540 RESET_BIT (transpout[bb->index], expr->bitmap_index);
5545 /* Removal of useless null pointer checks */
5547 /* Called via note_stores. X is set by SETTER. If X is a register we must
5548 invalidate nonnull_local and set nonnull_killed. DATA is really a
5549 `null_pointer_info *'.
5551 We ignore hard registers. */
5553 static void
5554 invalidate_nonnull_info (x, setter, data)
5555 rtx x;
5556 rtx setter ATTRIBUTE_UNUSED;
5557 void *data;
5559 unsigned int regno;
5560 struct null_pointer_info *npi = (struct null_pointer_info *) data;
5562 while (GET_CODE (x) == SUBREG)
5563 x = SUBREG_REG (x);
5565 /* Ignore anything that is not a register or is a hard register. */
5566 if (GET_CODE (x) != REG
5567 || REGNO (x) < npi->min_reg
5568 || REGNO (x) >= npi->max_reg)
5569 return;
5571 regno = REGNO (x) - npi->min_reg;
5573 RESET_BIT (npi->nonnull_local[npi->current_block->index], regno);
5574 SET_BIT (npi->nonnull_killed[npi->current_block->index], regno);
5577 /* Do null-pointer check elimination for the registers indicated in
5578 NPI. NONNULL_AVIN and NONNULL_AVOUT are pre-allocated sbitmaps;
5579 they are not our responsibility to free. */
5581 static int
5582 delete_null_pointer_checks_1 (block_reg, nonnull_avin,
5583 nonnull_avout, npi)
5584 unsigned int *block_reg;
5585 sbitmap *nonnull_avin;
5586 sbitmap *nonnull_avout;
5587 struct null_pointer_info *npi;
5589 basic_block bb, current_block;
5590 sbitmap *nonnull_local = npi->nonnull_local;
5591 sbitmap *nonnull_killed = npi->nonnull_killed;
5592 int something_changed = 0;
5594 /* Compute local properties, nonnull and killed. A register will have
5595 the nonnull property if at the end of the current block its value is
5596 known to be nonnull. The killed property indicates that somewhere in
5597 the block any information we had about the register is killed.
5599 Note that a register can have both properties in a single block. That
5600 indicates that it's killed, then later in the block a new value is
5601 computed. */
5602 sbitmap_vector_zero (nonnull_local, last_basic_block);
5603 sbitmap_vector_zero (nonnull_killed, last_basic_block);
5605 FOR_EACH_BB (current_block)
5607 rtx insn, stop_insn;
5609 /* Set the current block for invalidate_nonnull_info. */
5610 npi->current_block = current_block;
5612 /* Scan each insn in the basic block looking for memory references and
5613 register sets. */
5614 stop_insn = NEXT_INSN (current_block->end);
5615 for (insn = current_block->head;
5616 insn != stop_insn;
5617 insn = NEXT_INSN (insn))
5619 rtx set;
5620 rtx reg;
5622 /* Ignore anything that is not a normal insn. */
5623 if (! INSN_P (insn))
5624 continue;
5626 /* Basically ignore anything that is not a simple SET. We do have
5627 to make sure to invalidate nonnull_local and set nonnull_killed
5628 for such insns though. */
5629 set = single_set (insn);
5630 if (!set)
5632 note_stores (PATTERN (insn), invalidate_nonnull_info, npi);
5633 continue;
5636 /* See if we've got a usable memory load. We handle it first
5637 in case it uses its address register as a dest (which kills
5638 the nonnull property). */
5639 if (GET_CODE (SET_SRC (set)) == MEM
5640 && GET_CODE ((reg = XEXP (SET_SRC (set), 0))) == REG
5641 && REGNO (reg) >= npi->min_reg
5642 && REGNO (reg) < npi->max_reg)
5643 SET_BIT (nonnull_local[current_block->index],
5644 REGNO (reg) - npi->min_reg);
5646 /* Now invalidate stuff clobbered by this insn. */
5647 note_stores (PATTERN (insn), invalidate_nonnull_info, npi);
5649 /* And handle stores, we do these last since any sets in INSN can
5650 not kill the nonnull property if it is derived from a MEM
5651 appearing in a SET_DEST. */
5652 if (GET_CODE (SET_DEST (set)) == MEM
5653 && GET_CODE ((reg = XEXP (SET_DEST (set), 0))) == REG
5654 && REGNO (reg) >= npi->min_reg
5655 && REGNO (reg) < npi->max_reg)
5656 SET_BIT (nonnull_local[current_block->index],
5657 REGNO (reg) - npi->min_reg);
5661 /* Now compute global properties based on the local properties. This
5662 is a classic global availablity algorithm. */
5663 compute_available (nonnull_local, nonnull_killed,
5664 nonnull_avout, nonnull_avin);
5666 /* Now look at each bb and see if it ends with a compare of a value
5667 against zero. */
5668 FOR_EACH_BB (bb)
5670 rtx last_insn = bb->end;
5671 rtx condition, earliest;
5672 int compare_and_branch;
5674 /* Since MIN_REG is always at least FIRST_PSEUDO_REGISTER, and
5675 since BLOCK_REG[BB] is zero if this block did not end with a
5676 comparison against zero, this condition works. */
5677 if (block_reg[bb->index] < npi->min_reg
5678 || block_reg[bb->index] >= npi->max_reg)
5679 continue;
5681 /* LAST_INSN is a conditional jump. Get its condition. */
5682 condition = get_condition (last_insn, &earliest);
5684 /* If we can't determine the condition then skip. */
5685 if (! condition)
5686 continue;
5688 /* Is the register known to have a nonzero value? */
5689 if (!TEST_BIT (nonnull_avout[bb->index], block_reg[bb->index] - npi->min_reg))
5690 continue;
5692 /* Try to compute whether the compare/branch at the loop end is one or
5693 two instructions. */
5694 if (earliest == last_insn)
5695 compare_and_branch = 1;
5696 else if (earliest == prev_nonnote_insn (last_insn))
5697 compare_and_branch = 2;
5698 else
5699 continue;
5701 /* We know the register in this comparison is nonnull at exit from
5702 this block. We can optimize this comparison. */
5703 if (GET_CODE (condition) == NE)
5705 rtx new_jump;
5707 new_jump = emit_jump_insn_after (gen_jump (JUMP_LABEL (last_insn)),
5708 last_insn);
5709 JUMP_LABEL (new_jump) = JUMP_LABEL (last_insn);
5710 LABEL_NUSES (JUMP_LABEL (new_jump))++;
5711 emit_barrier_after (new_jump);
5714 something_changed = 1;
5715 delete_insn (last_insn);
5716 if (compare_and_branch == 2)
5717 delete_insn (earliest);
5718 purge_dead_edges (bb);
5720 /* Don't check this block again. (Note that BLOCK_END is
5721 invalid here; we deleted the last instruction in the
5722 block.) */
5723 block_reg[bb->index] = 0;
5726 return something_changed;
5729 /* Find EQ/NE comparisons against zero which can be (indirectly) evaluated
5730 at compile time.
5732 This is conceptually similar to global constant/copy propagation and
5733 classic global CSE (it even uses the same dataflow equations as cprop).
5735 If a register is used as memory address with the form (mem (reg)), then we
5736 know that REG can not be zero at that point in the program. Any instruction
5737 which sets REG "kills" this property.
5739 So, if every path leading to a conditional branch has an available memory
5740 reference of that form, then we know the register can not have the value
5741 zero at the conditional branch.
5743 So we merely need to compute the local properies and propagate that data
5744 around the cfg, then optimize where possible.
5746 We run this pass two times. Once before CSE, then again after CSE. This
5747 has proven to be the most profitable approach. It is rare for new
5748 optimization opportunities of this nature to appear after the first CSE
5749 pass.
5751 This could probably be integrated with global cprop with a little work. */
5754 delete_null_pointer_checks (f)
5755 rtx f ATTRIBUTE_UNUSED;
5757 sbitmap *nonnull_avin, *nonnull_avout;
5758 unsigned int *block_reg;
5759 basic_block bb;
5760 int reg;
5761 int regs_per_pass;
5762 int max_reg;
5763 struct null_pointer_info npi;
5764 int something_changed = 0;
5766 /* If we have only a single block, then there's nothing to do. */
5767 if (n_basic_blocks <= 1)
5768 return 0;
5770 /* Trying to perform global optimizations on flow graphs which have
5771 a high connectivity will take a long time and is unlikely to be
5772 particularly useful.
5774 In normal circumstances a cfg should have about twice as many edges
5775 as blocks. But we do not want to punish small functions which have
5776 a couple switch statements. So we require a relatively large number
5777 of basic blocks and the ratio of edges to blocks to be high. */
5778 if (n_basic_blocks > 1000 && n_edges / n_basic_blocks >= 20)
5779 return 0;
5781 /* We need four bitmaps, each with a bit for each register in each
5782 basic block. */
5783 max_reg = max_reg_num ();
5784 regs_per_pass = get_bitmap_width (4, last_basic_block, max_reg);
5786 /* Allocate bitmaps to hold local and global properties. */
5787 npi.nonnull_local = sbitmap_vector_alloc (last_basic_block, regs_per_pass);
5788 npi.nonnull_killed = sbitmap_vector_alloc (last_basic_block, regs_per_pass);
5789 nonnull_avin = sbitmap_vector_alloc (last_basic_block, regs_per_pass);
5790 nonnull_avout = sbitmap_vector_alloc (last_basic_block, regs_per_pass);
5792 /* Go through the basic blocks, seeing whether or not each block
5793 ends with a conditional branch whose condition is a comparison
5794 against zero. Record the register compared in BLOCK_REG. */
5795 block_reg = (unsigned int *) xcalloc (last_basic_block, sizeof (int));
5796 FOR_EACH_BB (bb)
5798 rtx last_insn = bb->end;
5799 rtx condition, earliest, reg;
5801 /* We only want conditional branches. */
5802 if (GET_CODE (last_insn) != JUMP_INSN
5803 || !any_condjump_p (last_insn)
5804 || !onlyjump_p (last_insn))
5805 continue;
5807 /* LAST_INSN is a conditional jump. Get its condition. */
5808 condition = get_condition (last_insn, &earliest);
5810 /* If we were unable to get the condition, or it is not an equality
5811 comparison against zero then there's nothing we can do. */
5812 if (!condition
5813 || (GET_CODE (condition) != NE && GET_CODE (condition) != EQ)
5814 || GET_CODE (XEXP (condition, 1)) != CONST_INT
5815 || (XEXP (condition, 1)
5816 != CONST0_RTX (GET_MODE (XEXP (condition, 0)))))
5817 continue;
5819 /* We must be checking a register against zero. */
5820 reg = XEXP (condition, 0);
5821 if (GET_CODE (reg) != REG)
5822 continue;
5824 block_reg[bb->index] = REGNO (reg);
5827 /* Go through the algorithm for each block of registers. */
5828 for (reg = FIRST_PSEUDO_REGISTER; reg < max_reg; reg += regs_per_pass)
5830 npi.min_reg = reg;
5831 npi.max_reg = MIN (reg + regs_per_pass, max_reg);
5832 something_changed |= delete_null_pointer_checks_1 (block_reg,
5833 nonnull_avin,
5834 nonnull_avout,
5835 &npi);
5838 /* Free the table of registers compared at the end of every block. */
5839 free (block_reg);
5841 /* Free bitmaps. */
5842 sbitmap_vector_free (npi.nonnull_local);
5843 sbitmap_vector_free (npi.nonnull_killed);
5844 sbitmap_vector_free (nonnull_avin);
5845 sbitmap_vector_free (nonnull_avout);
5847 return something_changed;
5850 /* Code Hoisting variables and subroutines. */
5852 /* Very busy expressions. */
5853 static sbitmap *hoist_vbein;
5854 static sbitmap *hoist_vbeout;
5856 /* Hoistable expressions. */
5857 static sbitmap *hoist_exprs;
5859 /* Dominator bitmaps. */
5860 dominance_info dominators;
5862 /* ??? We could compute post dominators and run this algorithm in
5863 reverse to to perform tail merging, doing so would probably be
5864 more effective than the tail merging code in jump.c.
5866 It's unclear if tail merging could be run in parallel with
5867 code hoisting. It would be nice. */
5869 /* Allocate vars used for code hoisting analysis. */
5871 static void
5872 alloc_code_hoist_mem (n_blocks, n_exprs)
5873 int n_blocks, n_exprs;
5875 antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
5876 transp = sbitmap_vector_alloc (n_blocks, n_exprs);
5877 comp = sbitmap_vector_alloc (n_blocks, n_exprs);
5879 hoist_vbein = sbitmap_vector_alloc (n_blocks, n_exprs);
5880 hoist_vbeout = sbitmap_vector_alloc (n_blocks, n_exprs);
5881 hoist_exprs = sbitmap_vector_alloc (n_blocks, n_exprs);
5882 transpout = sbitmap_vector_alloc (n_blocks, n_exprs);
5885 /* Free vars used for code hoisting analysis. */
5887 static void
5888 free_code_hoist_mem ()
5890 sbitmap_vector_free (antloc);
5891 sbitmap_vector_free (transp);
5892 sbitmap_vector_free (comp);
5894 sbitmap_vector_free (hoist_vbein);
5895 sbitmap_vector_free (hoist_vbeout);
5896 sbitmap_vector_free (hoist_exprs);
5897 sbitmap_vector_free (transpout);
5899 free_dominance_info (dominators);
5902 /* Compute the very busy expressions at entry/exit from each block.
5904 An expression is very busy if all paths from a given point
5905 compute the expression. */
5907 static void
5908 compute_code_hoist_vbeinout ()
5910 int changed, passes;
5911 basic_block bb;
5913 sbitmap_vector_zero (hoist_vbeout, last_basic_block);
5914 sbitmap_vector_zero (hoist_vbein, last_basic_block);
5916 passes = 0;
5917 changed = 1;
5919 while (changed)
5921 changed = 0;
5923 /* We scan the blocks in the reverse order to speed up
5924 the convergence. */
5925 FOR_EACH_BB_REVERSE (bb)
5927 changed |= sbitmap_a_or_b_and_c_cg (hoist_vbein[bb->index], antloc[bb->index],
5928 hoist_vbeout[bb->index], transp[bb->index]);
5929 if (bb->next_bb != EXIT_BLOCK_PTR)
5930 sbitmap_intersection_of_succs (hoist_vbeout[bb->index], hoist_vbein, bb->index);
5933 passes++;
5936 if (gcse_file)
5937 fprintf (gcse_file, "hoisting vbeinout computation: %d passes\n", passes);
5940 /* Top level routine to do the dataflow analysis needed by code hoisting. */
5942 static void
5943 compute_code_hoist_data ()
5945 compute_local_properties (transp, comp, antloc, 0);
5946 compute_transpout ();
5947 compute_code_hoist_vbeinout ();
5948 dominators = calculate_dominance_info (CDI_DOMINATORS);
5949 if (gcse_file)
5950 fprintf (gcse_file, "\n");
5953 /* Determine if the expression identified by EXPR_INDEX would
5954 reach BB unimpared if it was placed at the end of EXPR_BB.
5956 It's unclear exactly what Muchnick meant by "unimpared". It seems
5957 to me that the expression must either be computed or transparent in
5958 *every* block in the path(s) from EXPR_BB to BB. Any other definition
5959 would allow the expression to be hoisted out of loops, even if
5960 the expression wasn't a loop invariant.
5962 Contrast this to reachability for PRE where an expression is
5963 considered reachable if *any* path reaches instead of *all*
5964 paths. */
5966 static int
5967 hoist_expr_reaches_here_p (expr_bb, expr_index, bb, visited)
5968 basic_block expr_bb;
5969 int expr_index;
5970 basic_block bb;
5971 char *visited;
5973 edge pred;
5974 int visited_allocated_locally = 0;
5977 if (visited == NULL)
5979 visited_allocated_locally = 1;
5980 visited = xcalloc (last_basic_block, 1);
5983 for (pred = bb->pred; pred != NULL; pred = pred->pred_next)
5985 basic_block pred_bb = pred->src;
5987 if (pred->src == ENTRY_BLOCK_PTR)
5988 break;
5989 else if (pred_bb == expr_bb)
5990 continue;
5991 else if (visited[pred_bb->index])
5992 continue;
5994 /* Does this predecessor generate this expression? */
5995 else if (TEST_BIT (comp[pred_bb->index], expr_index))
5996 break;
5997 else if (! TEST_BIT (transp[pred_bb->index], expr_index))
5998 break;
6000 /* Not killed. */
6001 else
6003 visited[pred_bb->index] = 1;
6004 if (! hoist_expr_reaches_here_p (expr_bb, expr_index,
6005 pred_bb, visited))
6006 break;
6009 if (visited_allocated_locally)
6010 free (visited);
6012 return (pred == NULL);
6015 /* Actually perform code hoisting. */
6017 static void
6018 hoist_code ()
6020 basic_block bb, dominated;
6021 basic_block *domby;
6022 unsigned int domby_len;
6023 unsigned int i,j;
6024 struct expr **index_map;
6025 struct expr *expr;
6027 sbitmap_vector_zero (hoist_exprs, last_basic_block);
6029 /* Compute a mapping from expression number (`bitmap_index') to
6030 hash table entry. */
6032 index_map = (struct expr **) xcalloc (n_exprs, sizeof (struct expr *));
6033 for (i = 0; i < expr_hash_table_size; i++)
6034 for (expr = expr_hash_table[i]; expr != NULL; expr = expr->next_same_hash)
6035 index_map[expr->bitmap_index] = expr;
6037 /* Walk over each basic block looking for potentially hoistable
6038 expressions, nothing gets hoisted from the entry block. */
6039 FOR_EACH_BB (bb)
6041 int found = 0;
6042 int insn_inserted_p;
6044 domby_len = get_dominated_by (dominators, bb, &domby);
6045 /* Examine each expression that is very busy at the exit of this
6046 block. These are the potentially hoistable expressions. */
6047 for (i = 0; i < hoist_vbeout[bb->index]->n_bits; i++)
6049 int hoistable = 0;
6051 if (TEST_BIT (hoist_vbeout[bb->index], i)
6052 && TEST_BIT (transpout[bb->index], i))
6054 /* We've found a potentially hoistable expression, now
6055 we look at every block BB dominates to see if it
6056 computes the expression. */
6057 for (j = 0; j < domby_len; j++)
6059 dominated = domby[j];
6060 /* Ignore self dominance. */
6061 if (bb == dominated)
6062 continue;
6063 /* We've found a dominated block, now see if it computes
6064 the busy expression and whether or not moving that
6065 expression to the "beginning" of that block is safe. */
6066 if (!TEST_BIT (antloc[dominated->index], i))
6067 continue;
6069 /* Note if the expression would reach the dominated block
6070 unimpared if it was placed at the end of BB.
6072 Keep track of how many times this expression is hoistable
6073 from a dominated block into BB. */
6074 if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
6075 hoistable++;
6078 /* If we found more than one hoistable occurrence of this
6079 expression, then note it in the bitmap of expressions to
6080 hoist. It makes no sense to hoist things which are computed
6081 in only one BB, and doing so tends to pessimize register
6082 allocation. One could increase this value to try harder
6083 to avoid any possible code expansion due to register
6084 allocation issues; however experiments have shown that
6085 the vast majority of hoistable expressions are only movable
6086 from two successors, so raising this threshhold is likely
6087 to nullify any benefit we get from code hoisting. */
6088 if (hoistable > 1)
6090 SET_BIT (hoist_exprs[bb->index], i);
6091 found = 1;
6095 /* If we found nothing to hoist, then quit now. */
6096 if (! found)
6098 free (domby);
6099 continue;
6102 /* Loop over all the hoistable expressions. */
6103 for (i = 0; i < hoist_exprs[bb->index]->n_bits; i++)
6105 /* We want to insert the expression into BB only once, so
6106 note when we've inserted it. */
6107 insn_inserted_p = 0;
6109 /* These tests should be the same as the tests above. */
6110 if (TEST_BIT (hoist_vbeout[bb->index], i))
6112 /* We've found a potentially hoistable expression, now
6113 we look at every block BB dominates to see if it
6114 computes the expression. */
6115 for (j = 0; j < domby_len; j++)
6117 dominated = domby[j];
6118 /* Ignore self dominance. */
6119 if (bb == dominated)
6120 continue;
6122 /* We've found a dominated block, now see if it computes
6123 the busy expression and whether or not moving that
6124 expression to the "beginning" of that block is safe. */
6125 if (!TEST_BIT (antloc[dominated->index], i))
6126 continue;
6128 /* The expression is computed in the dominated block and
6129 it would be safe to compute it at the start of the
6130 dominated block. Now we have to determine if the
6131 expression would reach the dominated block if it was
6132 placed at the end of BB. */
6133 if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
6135 struct expr *expr = index_map[i];
6136 struct occr *occr = expr->antic_occr;
6137 rtx insn;
6138 rtx set;
6140 /* Find the right occurrence of this expression. */
6141 while (BLOCK_FOR_INSN (occr->insn) != dominated && occr)
6142 occr = occr->next;
6144 /* Should never happen. */
6145 if (!occr)
6146 abort ();
6148 insn = occr->insn;
6150 set = single_set (insn);
6151 if (! set)
6152 abort ();
6154 /* Create a pseudo-reg to store the result of reaching
6155 expressions into. Get the mode for the new pseudo
6156 from the mode of the original destination pseudo. */
6157 if (expr->reaching_reg == NULL)
6158 expr->reaching_reg
6159 = gen_reg_rtx (GET_MODE (SET_DEST (set)));
6161 gcse_emit_move_after (expr->reaching_reg, SET_DEST (set), insn);
6162 delete_insn (insn);
6163 occr->deleted_p = 1;
6164 if (!insn_inserted_p)
6166 insert_insn_end_bb (index_map[i], bb, 0);
6167 insn_inserted_p = 1;
6173 free (domby);
6176 free (index_map);
6179 /* Top level routine to perform one code hoisting (aka unification) pass
6181 Return non-zero if a change was made. */
6183 static int
6184 one_code_hoisting_pass ()
6186 int changed = 0;
6188 alloc_expr_hash_table (max_cuid);
6189 compute_expr_hash_table ();
6190 if (gcse_file)
6191 dump_hash_table (gcse_file, "Code Hosting Expressions", expr_hash_table,
6192 expr_hash_table_size, n_exprs);
6194 if (n_exprs > 0)
6196 alloc_code_hoist_mem (last_basic_block, n_exprs);
6197 compute_code_hoist_data ();
6198 hoist_code ();
6199 free_code_hoist_mem ();
6202 free_expr_hash_table ();
6204 return changed;
6207 /* Here we provide the things required to do store motion towards
6208 the exit. In order for this to be effective, gcse also needed to
6209 be taught how to move a load when it is kill only by a store to itself.
6211 int i;
6212 float a[10];
6214 void foo(float scale)
6216 for (i=0; i<10; i++)
6217 a[i] *= scale;
6220 'i' is both loaded and stored to in the loop. Normally, gcse cannot move
6221 the load out since its live around the loop, and stored at the bottom
6222 of the loop.
6224 The 'Load Motion' referred to and implemented in this file is
6225 an enhancement to gcse which when using edge based lcm, recognizes
6226 this situation and allows gcse to move the load out of the loop.
6228 Once gcse has hoisted the load, store motion can then push this
6229 load towards the exit, and we end up with no loads or stores of 'i'
6230 in the loop. */
6232 /* This will search the ldst list for a matching expression. If it
6233 doesn't find one, we create one and initialize it. */
6235 static struct ls_expr *
6236 ldst_entry (x)
6237 rtx x;
6239 struct ls_expr * ptr;
6241 for (ptr = first_ls_expr(); ptr != NULL; ptr = next_ls_expr (ptr))
6242 if (expr_equiv_p (ptr->pattern, x))
6243 break;
6245 if (!ptr)
6247 ptr = (struct ls_expr *) xmalloc (sizeof (struct ls_expr));
6249 ptr->next = pre_ldst_mems;
6250 ptr->expr = NULL;
6251 ptr->pattern = x;
6252 ptr->loads = NULL_RTX;
6253 ptr->stores = NULL_RTX;
6254 ptr->reaching_reg = NULL_RTX;
6255 ptr->invalid = 0;
6256 ptr->index = 0;
6257 ptr->hash_index = 0;
6258 pre_ldst_mems = ptr;
6261 return ptr;
6264 /* Free up an individual ldst entry. */
6266 static void
6267 free_ldst_entry (ptr)
6268 struct ls_expr * ptr;
6270 free_INSN_LIST_list (& ptr->loads);
6271 free_INSN_LIST_list (& ptr->stores);
6273 free (ptr);
6276 /* Free up all memory associated with the ldst list. */
6278 static void
6279 free_ldst_mems ()
6281 while (pre_ldst_mems)
6283 struct ls_expr * tmp = pre_ldst_mems;
6285 pre_ldst_mems = pre_ldst_mems->next;
6287 free_ldst_entry (tmp);
6290 pre_ldst_mems = NULL;
6293 /* Dump debugging info about the ldst list. */
6295 static void
6296 print_ldst_list (file)
6297 FILE * file;
6299 struct ls_expr * ptr;
6301 fprintf (file, "LDST list: \n");
6303 for (ptr = first_ls_expr(); ptr != NULL; ptr = next_ls_expr (ptr))
6305 fprintf (file, " Pattern (%3d): ", ptr->index);
6307 print_rtl (file, ptr->pattern);
6309 fprintf (file, "\n Loads : ");
6311 if (ptr->loads)
6312 print_rtl (file, ptr->loads);
6313 else
6314 fprintf (file, "(nil)");
6316 fprintf (file, "\n Stores : ");
6318 if (ptr->stores)
6319 print_rtl (file, ptr->stores);
6320 else
6321 fprintf (file, "(nil)");
6323 fprintf (file, "\n\n");
6326 fprintf (file, "\n");
6329 /* Returns 1 if X is in the list of ldst only expressions. */
6331 static struct ls_expr *
6332 find_rtx_in_ldst (x)
6333 rtx x;
6335 struct ls_expr * ptr;
6337 for (ptr = pre_ldst_mems; ptr != NULL; ptr = ptr->next)
6338 if (expr_equiv_p (ptr->pattern, x) && ! ptr->invalid)
6339 return ptr;
6341 return NULL;
6344 /* Assign each element of the list of mems a monotonically increasing value. */
6346 static int
6347 enumerate_ldsts ()
6349 struct ls_expr * ptr;
6350 int n = 0;
6352 for (ptr = pre_ldst_mems; ptr != NULL; ptr = ptr->next)
6353 ptr->index = n++;
6355 return n;
6358 /* Return first item in the list. */
6360 static inline struct ls_expr *
6361 first_ls_expr ()
6363 return pre_ldst_mems;
6366 /* Return the next item in ther list after the specified one. */
6368 static inline struct ls_expr *
6369 next_ls_expr (ptr)
6370 struct ls_expr * ptr;
6372 return ptr->next;
6375 /* Load Motion for loads which only kill themselves. */
6377 /* Return true if x is a simple MEM operation, with no registers or
6378 side effects. These are the types of loads we consider for the
6379 ld_motion list, otherwise we let the usual aliasing take care of it. */
6381 static int
6382 simple_mem (x)
6383 rtx x;
6385 if (GET_CODE (x) != MEM)
6386 return 0;
6388 if (MEM_VOLATILE_P (x))
6389 return 0;
6391 if (GET_MODE (x) == BLKmode)
6392 return 0;
6394 if (!rtx_varies_p (XEXP (x, 0), 0))
6395 return 1;
6397 return 0;
6400 /* Make sure there isn't a buried reference in this pattern anywhere.
6401 If there is, invalidate the entry for it since we're not capable
6402 of fixing it up just yet.. We have to be sure we know about ALL
6403 loads since the aliasing code will allow all entries in the
6404 ld_motion list to not-alias itself. If we miss a load, we will get
6405 the wrong value since gcse might common it and we won't know to
6406 fix it up. */
6408 static void
6409 invalidate_any_buried_refs (x)
6410 rtx x;
6412 const char * fmt;
6413 int i, j;
6414 struct ls_expr * ptr;
6416 /* Invalidate it in the list. */
6417 if (GET_CODE (x) == MEM && simple_mem (x))
6419 ptr = ldst_entry (x);
6420 ptr->invalid = 1;
6423 /* Recursively process the insn. */
6424 fmt = GET_RTX_FORMAT (GET_CODE (x));
6426 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6428 if (fmt[i] == 'e')
6429 invalidate_any_buried_refs (XEXP (x, i));
6430 else if (fmt[i] == 'E')
6431 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6432 invalidate_any_buried_refs (XVECEXP (x, i, j));
6436 /* Find all the 'simple' MEMs which are used in LOADs and STORES. Simple
6437 being defined as MEM loads and stores to symbols, with no
6438 side effects and no registers in the expression. If there are any
6439 uses/defs which don't match this criteria, it is invalidated and
6440 trimmed out later. */
6442 static void
6443 compute_ld_motion_mems ()
6445 struct ls_expr * ptr;
6446 basic_block bb;
6447 rtx insn;
6449 pre_ldst_mems = NULL;
6451 FOR_EACH_BB (bb)
6453 for (insn = bb->head;
6454 insn && insn != NEXT_INSN (bb->end);
6455 insn = NEXT_INSN (insn))
6457 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
6459 if (GET_CODE (PATTERN (insn)) == SET)
6461 rtx src = SET_SRC (PATTERN (insn));
6462 rtx dest = SET_DEST (PATTERN (insn));
6464 /* Check for a simple LOAD... */
6465 if (GET_CODE (src) == MEM && simple_mem (src))
6467 ptr = ldst_entry (src);
6468 if (GET_CODE (dest) == REG)
6469 ptr->loads = alloc_INSN_LIST (insn, ptr->loads);
6470 else
6471 ptr->invalid = 1;
6473 else
6475 /* Make sure there isn't a buried load somewhere. */
6476 invalidate_any_buried_refs (src);
6479 /* Check for stores. Don't worry about aliased ones, they
6480 will block any movement we might do later. We only care
6481 about this exact pattern since those are the only
6482 circumstance that we will ignore the aliasing info. */
6483 if (GET_CODE (dest) == MEM && simple_mem (dest))
6485 ptr = ldst_entry (dest);
6487 if (GET_CODE (src) != MEM
6488 && GET_CODE (src) != ASM_OPERANDS)
6489 ptr->stores = alloc_INSN_LIST (insn, ptr->stores);
6490 else
6491 ptr->invalid = 1;
6494 else
6495 invalidate_any_buried_refs (PATTERN (insn));
6501 /* Remove any references that have been either invalidated or are not in the
6502 expression list for pre gcse. */
6504 static void
6505 trim_ld_motion_mems ()
6507 struct ls_expr * last = NULL;
6508 struct ls_expr * ptr = first_ls_expr ();
6510 while (ptr != NULL)
6512 int del = ptr->invalid;
6513 struct expr * expr = NULL;
6515 /* Delete if entry has been made invalid. */
6516 if (!del)
6518 unsigned int i;
6520 del = 1;
6521 /* Delete if we cannot find this mem in the expression list. */
6522 for (i = 0; i < expr_hash_table_size && del; i++)
6524 for (expr = expr_hash_table[i];
6525 expr != NULL;
6526 expr = expr->next_same_hash)
6527 if (expr_equiv_p (expr->expr, ptr->pattern))
6529 del = 0;
6530 break;
6535 if (del)
6537 if (last != NULL)
6539 last->next = ptr->next;
6540 free_ldst_entry (ptr);
6541 ptr = last->next;
6543 else
6545 pre_ldst_mems = pre_ldst_mems->next;
6546 free_ldst_entry (ptr);
6547 ptr = pre_ldst_mems;
6550 else
6552 /* Set the expression field if we are keeping it. */
6553 last = ptr;
6554 ptr->expr = expr;
6555 ptr = ptr->next;
6559 /* Show the world what we've found. */
6560 if (gcse_file && pre_ldst_mems != NULL)
6561 print_ldst_list (gcse_file);
6564 /* This routine will take an expression which we are replacing with
6565 a reaching register, and update any stores that are needed if
6566 that expression is in the ld_motion list. Stores are updated by
6567 copying their SRC to the reaching register, and then storeing
6568 the reaching register into the store location. These keeps the
6569 correct value in the reaching register for the loads. */
6571 static void
6572 update_ld_motion_stores (expr)
6573 struct expr * expr;
6575 struct ls_expr * mem_ptr;
6577 if ((mem_ptr = find_rtx_in_ldst (expr->expr)))
6579 /* We can try to find just the REACHED stores, but is shouldn't
6580 matter to set the reaching reg everywhere... some might be
6581 dead and should be eliminated later. */
6583 /* We replace SET mem = expr with
6584 SET reg = expr
6585 SET mem = reg , where reg is the
6586 reaching reg used in the load. */
6587 rtx list = mem_ptr->stores;
6589 for ( ; list != NULL_RTX; list = XEXP (list, 1))
6591 rtx insn = XEXP (list, 0);
6592 rtx pat = PATTERN (insn);
6593 rtx src = SET_SRC (pat);
6594 rtx reg = expr->reaching_reg;
6595 rtx copy, new;
6597 /* If we've already copied it, continue. */
6598 if (expr->reaching_reg == src)
6599 continue;
6601 if (gcse_file)
6603 fprintf (gcse_file, "PRE: store updated with reaching reg ");
6604 print_rtl (gcse_file, expr->reaching_reg);
6605 fprintf (gcse_file, ":\n ");
6606 print_inline_rtx (gcse_file, insn, 8);
6607 fprintf (gcse_file, "\n");
6610 copy = gen_move_insn ( reg, SET_SRC (pat));
6611 new = emit_insn_before (copy, insn);
6612 record_one_set (REGNO (reg), new);
6613 SET_SRC (pat) = reg;
6615 /* un-recognize this pattern since it's probably different now. */
6616 INSN_CODE (insn) = -1;
6617 gcse_create_count++;
6622 /* Store motion code. */
6624 /* This is used to communicate the target bitvector we want to use in the
6625 reg_set_info routine when called via the note_stores mechanism. */
6626 static sbitmap * regvec;
6628 /* Used in computing the reverse edge graph bit vectors. */
6629 static sbitmap * st_antloc;
6631 /* Global holding the number of store expressions we are dealing with. */
6632 static int num_stores;
6634 /* Checks to set if we need to mark a register set. Called from note_stores. */
6636 static void
6637 reg_set_info (dest, setter, data)
6638 rtx dest, setter ATTRIBUTE_UNUSED;
6639 void * data ATTRIBUTE_UNUSED;
6641 if (GET_CODE (dest) == SUBREG)
6642 dest = SUBREG_REG (dest);
6644 if (GET_CODE (dest) == REG)
6645 SET_BIT (*regvec, REGNO (dest));
6648 /* Return non-zero if the register operands of expression X are killed
6649 anywhere in basic block BB. */
6651 static int
6652 store_ops_ok (x, bb)
6653 rtx x;
6654 basic_block bb;
6656 int i;
6657 enum rtx_code code;
6658 const char * fmt;
6660 /* Repeat is used to turn tail-recursion into iteration. */
6661 repeat:
6663 if (x == 0)
6664 return 1;
6666 code = GET_CODE (x);
6667 switch (code)
6669 case REG:
6670 /* If a reg has changed after us in this
6671 block, the operand has been killed. */
6672 return TEST_BIT (reg_set_in_block[bb->index], REGNO (x));
6674 case MEM:
6675 x = XEXP (x, 0);
6676 goto repeat;
6678 case PRE_DEC:
6679 case PRE_INC:
6680 case POST_DEC:
6681 case POST_INC:
6682 return 0;
6684 case PC:
6685 case CC0: /*FIXME*/
6686 case CONST:
6687 case CONST_INT:
6688 case CONST_DOUBLE:
6689 case CONST_VECTOR:
6690 case SYMBOL_REF:
6691 case LABEL_REF:
6692 case ADDR_VEC:
6693 case ADDR_DIFF_VEC:
6694 return 1;
6696 default:
6697 break;
6700 i = GET_RTX_LENGTH (code) - 1;
6701 fmt = GET_RTX_FORMAT (code);
6703 for (; i >= 0; i--)
6705 if (fmt[i] == 'e')
6707 rtx tem = XEXP (x, i);
6709 /* If we are about to do the last recursive call
6710 needed at this level, change it into iteration.
6711 This function is called enough to be worth it. */
6712 if (i == 0)
6714 x = tem;
6715 goto repeat;
6718 if (! store_ops_ok (tem, bb))
6719 return 0;
6721 else if (fmt[i] == 'E')
6723 int j;
6725 for (j = 0; j < XVECLEN (x, i); j++)
6727 if (! store_ops_ok (XVECEXP (x, i, j), bb))
6728 return 0;
6733 return 1;
6736 /* Determine whether insn is MEM store pattern that we will consider moving. */
6738 static void
6739 find_moveable_store (insn)
6740 rtx insn;
6742 struct ls_expr * ptr;
6743 rtx dest = PATTERN (insn);
6745 if (GET_CODE (dest) != SET
6746 || GET_CODE (SET_SRC (dest)) == ASM_OPERANDS)
6747 return;
6749 dest = SET_DEST (dest);
6751 if (GET_CODE (dest) != MEM || MEM_VOLATILE_P (dest)
6752 || GET_MODE (dest) == BLKmode)
6753 return;
6755 if (GET_CODE (XEXP (dest, 0)) != SYMBOL_REF)
6756 return;
6758 if (rtx_varies_p (XEXP (dest, 0), 0))
6759 return;
6761 ptr = ldst_entry (dest);
6762 ptr->stores = alloc_INSN_LIST (insn, ptr->stores);
6765 /* Perform store motion. Much like gcse, except we move expressions the
6766 other way by looking at the flowgraph in reverse. */
6768 static int
6769 compute_store_table ()
6771 int ret;
6772 basic_block bb;
6773 unsigned regno;
6774 rtx insn, pat;
6776 max_gcse_regno = max_reg_num ();
6778 reg_set_in_block = (sbitmap *) sbitmap_vector_alloc (last_basic_block,
6779 max_gcse_regno);
6780 sbitmap_vector_zero (reg_set_in_block, last_basic_block);
6781 pre_ldst_mems = 0;
6783 /* Find all the stores we care about. */
6784 FOR_EACH_BB (bb)
6786 regvec = & (reg_set_in_block[bb->index]);
6787 for (insn = bb->end;
6788 insn && insn != PREV_INSN (bb->end);
6789 insn = PREV_INSN (insn))
6791 /* Ignore anything that is not a normal insn. */
6792 if (! INSN_P (insn))
6793 continue;
6795 if (GET_CODE (insn) == CALL_INSN)
6797 bool clobbers_all = false;
6798 #ifdef NON_SAVING_SETJMP
6799 if (NON_SAVING_SETJMP
6800 && find_reg_note (insn, REG_SETJMP, NULL_RTX))
6801 clobbers_all = true;
6802 #endif
6804 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
6805 if (clobbers_all
6806 || TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
6807 SET_BIT (reg_set_in_block[bb->index], regno);
6810 pat = PATTERN (insn);
6811 note_stores (pat, reg_set_info, NULL);
6813 /* Now that we've marked regs, look for stores. */
6814 if (GET_CODE (pat) == SET)
6815 find_moveable_store (insn);
6819 ret = enumerate_ldsts ();
6821 if (gcse_file)
6823 fprintf (gcse_file, "Store Motion Expressions.\n");
6824 print_ldst_list (gcse_file);
6827 return ret;
6830 /* Check to see if the load X is aliased with STORE_PATTERN. */
6832 static int
6833 load_kills_store (x, store_pattern)
6834 rtx x, store_pattern;
6836 if (true_dependence (x, GET_MODE (x), store_pattern, rtx_addr_varies_p))
6837 return 1;
6838 return 0;
6841 /* Go through the entire insn X, looking for any loads which might alias
6842 STORE_PATTERN. Return 1 if found. */
6844 static int
6845 find_loads (x, store_pattern)
6846 rtx x, store_pattern;
6848 const char * fmt;
6849 int i, j;
6850 int ret = 0;
6852 if (!x)
6853 return 0;
6855 if (GET_CODE (x) == SET)
6856 x = SET_SRC (x);
6858 if (GET_CODE (x) == MEM)
6860 if (load_kills_store (x, store_pattern))
6861 return 1;
6864 /* Recursively process the insn. */
6865 fmt = GET_RTX_FORMAT (GET_CODE (x));
6867 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0 && !ret; i--)
6869 if (fmt[i] == 'e')
6870 ret |= find_loads (XEXP (x, i), store_pattern);
6871 else if (fmt[i] == 'E')
6872 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6873 ret |= find_loads (XVECEXP (x, i, j), store_pattern);
6875 return ret;
6878 /* Check if INSN kills the store pattern X (is aliased with it).
6879 Return 1 if it it does. */
6881 static int
6882 store_killed_in_insn (x, insn)
6883 rtx x, insn;
6885 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
6886 return 0;
6888 if (GET_CODE (insn) == CALL_INSN)
6890 /* A normal or pure call might read from pattern,
6891 but a const call will not. */
6892 return ! CONST_OR_PURE_CALL_P (insn) || pure_call_p (insn);
6895 if (GET_CODE (PATTERN (insn)) == SET)
6897 rtx pat = PATTERN (insn);
6898 /* Check for memory stores to aliased objects. */
6899 if (GET_CODE (SET_DEST (pat)) == MEM && !expr_equiv_p (SET_DEST (pat), x))
6900 /* pretend its a load and check for aliasing. */
6901 if (find_loads (SET_DEST (pat), x))
6902 return 1;
6903 return find_loads (SET_SRC (pat), x);
6905 else
6906 return find_loads (PATTERN (insn), x);
6909 /* Returns 1 if the expression X is loaded or clobbered on or after INSN
6910 within basic block BB. */
6912 static int
6913 store_killed_after (x, insn, bb)
6914 rtx x, insn;
6915 basic_block bb;
6917 rtx last = bb->end;
6919 if (insn == last)
6920 return 0;
6922 /* Check if the register operands of the store are OK in this block.
6923 Note that if registers are changed ANYWHERE in the block, we'll
6924 decide we can't move it, regardless of whether it changed above
6925 or below the store. This could be improved by checking the register
6926 operands while lookinng for aliasing in each insn. */
6927 if (!store_ops_ok (XEXP (x, 0), bb))
6928 return 1;
6930 for ( ; insn && insn != NEXT_INSN (last); insn = NEXT_INSN (insn))
6931 if (store_killed_in_insn (x, insn))
6932 return 1;
6934 return 0;
6937 /* Returns 1 if the expression X is loaded or clobbered on or before INSN
6938 within basic block BB. */
6939 static int
6940 store_killed_before (x, insn, bb)
6941 rtx x, insn;
6942 basic_block bb;
6944 rtx first = bb->head;
6946 if (insn == first)
6947 return store_killed_in_insn (x, insn);
6949 /* Check if the register operands of the store are OK in this block.
6950 Note that if registers are changed ANYWHERE in the block, we'll
6951 decide we can't move it, regardless of whether it changed above
6952 or below the store. This could be improved by checking the register
6953 operands while lookinng for aliasing in each insn. */
6954 if (!store_ops_ok (XEXP (x, 0), bb))
6955 return 1;
6957 for ( ; insn && insn != PREV_INSN (first); insn = PREV_INSN (insn))
6958 if (store_killed_in_insn (x, insn))
6959 return 1;
6961 return 0;
6964 #define ANTIC_STORE_LIST(x) ((x)->loads)
6965 #define AVAIL_STORE_LIST(x) ((x)->stores)
6967 /* Given the table of available store insns at the end of blocks,
6968 determine which ones are not killed by aliasing, and generate
6969 the appropriate vectors for gen and killed. */
6970 static void
6971 build_store_vectors ()
6973 basic_block bb, b;
6974 rtx insn, st;
6975 struct ls_expr * ptr;
6977 /* Build the gen_vector. This is any store in the table which is not killed
6978 by aliasing later in its block. */
6979 ae_gen = (sbitmap *) sbitmap_vector_alloc (last_basic_block, num_stores);
6980 sbitmap_vector_zero (ae_gen, last_basic_block);
6982 st_antloc = (sbitmap *) sbitmap_vector_alloc (last_basic_block, num_stores);
6983 sbitmap_vector_zero (st_antloc, last_basic_block);
6985 for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
6987 /* Put all the stores into either the antic list, or the avail list,
6988 or both. */
6989 rtx store_list = ptr->stores;
6990 ptr->stores = NULL_RTX;
6992 for (st = store_list; st != NULL; st = XEXP (st, 1))
6994 insn = XEXP (st, 0);
6995 bb = BLOCK_FOR_INSN (insn);
6997 if (!store_killed_after (ptr->pattern, insn, bb))
6999 /* If we've already seen an availale expression in this block,
7000 we can delete the one we saw already (It occurs earlier in
7001 the block), and replace it with this one). We'll copy the
7002 old SRC expression to an unused register in case there
7003 are any side effects. */
7004 if (TEST_BIT (ae_gen[bb->index], ptr->index))
7006 /* Find previous store. */
7007 rtx st;
7008 for (st = AVAIL_STORE_LIST (ptr); st ; st = XEXP (st, 1))
7009 if (BLOCK_FOR_INSN (XEXP (st, 0)) == bb)
7010 break;
7011 if (st)
7013 rtx r = gen_reg_rtx (GET_MODE (ptr->pattern));
7014 if (gcse_file)
7015 fprintf (gcse_file, "Removing redundant store:\n");
7016 replace_store_insn (r, XEXP (st, 0), bb);
7017 XEXP (st, 0) = insn;
7018 continue;
7021 SET_BIT (ae_gen[bb->index], ptr->index);
7022 AVAIL_STORE_LIST (ptr) = alloc_INSN_LIST (insn,
7023 AVAIL_STORE_LIST (ptr));
7026 if (!store_killed_before (ptr->pattern, insn, bb))
7028 SET_BIT (st_antloc[BLOCK_NUM (insn)], ptr->index);
7029 ANTIC_STORE_LIST (ptr) = alloc_INSN_LIST (insn,
7030 ANTIC_STORE_LIST (ptr));
7034 /* Free the original list of store insns. */
7035 free_INSN_LIST_list (&store_list);
7038 ae_kill = (sbitmap *) sbitmap_vector_alloc (last_basic_block, num_stores);
7039 sbitmap_vector_zero (ae_kill, last_basic_block);
7041 transp = (sbitmap *) sbitmap_vector_alloc (last_basic_block, num_stores);
7042 sbitmap_vector_zero (transp, last_basic_block);
7044 for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
7045 FOR_EACH_BB (b)
7047 if (store_killed_after (ptr->pattern, b->head, b))
7049 /* The anticipatable expression is not killed if it's gen'd. */
7051 We leave this check out for now. If we have a code sequence
7052 in a block which looks like:
7053 ST MEMa = x
7054 L y = MEMa
7055 ST MEMa = z
7056 We should flag this as having an ANTIC expression, NOT
7057 transparent, NOT killed, and AVAIL.
7058 Unfortunately, since we haven't re-written all loads to
7059 use the reaching reg, we'll end up doing an incorrect
7060 Load in the middle here if we push the store down. It happens in
7061 gcc.c-torture/execute/960311-1.c with -O3
7062 If we always kill it in this case, we'll sometimes do
7063 uneccessary work, but it shouldn't actually hurt anything.
7064 if (!TEST_BIT (ae_gen[b], ptr->index)). */
7065 SET_BIT (ae_kill[b->index], ptr->index);
7067 else
7068 SET_BIT (transp[b->index], ptr->index);
7071 /* Any block with no exits calls some non-returning function, so
7072 we better mark the store killed here, or we might not store to
7073 it at all. If we knew it was abort, we wouldn't have to store,
7074 but we don't know that for sure. */
7075 if (gcse_file)
7077 fprintf (gcse_file, "ST_avail and ST_antic (shown under loads..)\n");
7078 print_ldst_list (gcse_file);
7079 dump_sbitmap_vector (gcse_file, "st_antloc", "", st_antloc, last_basic_block);
7080 dump_sbitmap_vector (gcse_file, "st_kill", "", ae_kill, last_basic_block);
7081 dump_sbitmap_vector (gcse_file, "Transpt", "", transp, last_basic_block);
7082 dump_sbitmap_vector (gcse_file, "st_avloc", "", ae_gen, last_basic_block);
7086 /* Insert an instruction at the begining of a basic block, and update
7087 the BLOCK_HEAD if needed. */
7089 static void
7090 insert_insn_start_bb (insn, bb)
7091 rtx insn;
7092 basic_block bb;
7094 /* Insert at start of successor block. */
7095 rtx prev = PREV_INSN (bb->head);
7096 rtx before = bb->head;
7097 while (before != 0)
7099 if (GET_CODE (before) != CODE_LABEL
7100 && (GET_CODE (before) != NOTE
7101 || NOTE_LINE_NUMBER (before) != NOTE_INSN_BASIC_BLOCK))
7102 break;
7103 prev = before;
7104 if (prev == bb->end)
7105 break;
7106 before = NEXT_INSN (before);
7109 insn = emit_insn_after (insn, prev);
7111 if (gcse_file)
7113 fprintf (gcse_file, "STORE_MOTION insert store at start of BB %d:\n",
7114 bb->index);
7115 print_inline_rtx (gcse_file, insn, 6);
7116 fprintf (gcse_file, "\n");
7120 /* This routine will insert a store on an edge. EXPR is the ldst entry for
7121 the memory reference, and E is the edge to insert it on. Returns non-zero
7122 if an edge insertion was performed. */
7124 static int
7125 insert_store (expr, e)
7126 struct ls_expr * expr;
7127 edge e;
7129 rtx reg, insn;
7130 basic_block bb;
7131 edge tmp;
7133 /* We did all the deleted before this insert, so if we didn't delete a
7134 store, then we haven't set the reaching reg yet either. */
7135 if (expr->reaching_reg == NULL_RTX)
7136 return 0;
7138 reg = expr->reaching_reg;
7139 insn = gen_move_insn (expr->pattern, reg);
7141 /* If we are inserting this expression on ALL predecessor edges of a BB,
7142 insert it at the start of the BB, and reset the insert bits on the other
7143 edges so we don't try to insert it on the other edges. */
7144 bb = e->dest;
7145 for (tmp = e->dest->pred; tmp ; tmp = tmp->pred_next)
7147 int index = EDGE_INDEX (edge_list, tmp->src, tmp->dest);
7148 if (index == EDGE_INDEX_NO_EDGE)
7149 abort ();
7150 if (! TEST_BIT (pre_insert_map[index], expr->index))
7151 break;
7154 /* If tmp is NULL, we found an insertion on every edge, blank the
7155 insertion vector for these edges, and insert at the start of the BB. */
7156 if (!tmp && bb != EXIT_BLOCK_PTR)
7158 for (tmp = e->dest->pred; tmp ; tmp = tmp->pred_next)
7160 int index = EDGE_INDEX (edge_list, tmp->src, tmp->dest);
7161 RESET_BIT (pre_insert_map[index], expr->index);
7163 insert_insn_start_bb (insn, bb);
7164 return 0;
7167 /* We can't insert on this edge, so we'll insert at the head of the
7168 successors block. See Morgan, sec 10.5. */
7169 if ((e->flags & EDGE_ABNORMAL) == EDGE_ABNORMAL)
7171 insert_insn_start_bb (insn, bb);
7172 return 0;
7175 insert_insn_on_edge (insn, e);
7177 if (gcse_file)
7179 fprintf (gcse_file, "STORE_MOTION insert insn on edge (%d, %d):\n",
7180 e->src->index, e->dest->index);
7181 print_inline_rtx (gcse_file, insn, 6);
7182 fprintf (gcse_file, "\n");
7185 return 1;
7188 /* This routine will replace a store with a SET to a specified register. */
7190 static void
7191 replace_store_insn (reg, del, bb)
7192 rtx reg, del;
7193 basic_block bb;
7195 rtx insn;
7197 insn = gen_move_insn (reg, SET_SRC (PATTERN (del)));
7198 insn = emit_insn_after (insn, del);
7200 if (gcse_file)
7202 fprintf (gcse_file,
7203 "STORE_MOTION delete insn in BB %d:\n ", bb->index);
7204 print_inline_rtx (gcse_file, del, 6);
7205 fprintf (gcse_file, "\nSTORE MOTION replaced with insn:\n ");
7206 print_inline_rtx (gcse_file, insn, 6);
7207 fprintf (gcse_file, "\n");
7210 delete_insn (del);
7214 /* Delete a store, but copy the value that would have been stored into
7215 the reaching_reg for later storing. */
7217 static void
7218 delete_store (expr, bb)
7219 struct ls_expr * expr;
7220 basic_block bb;
7222 rtx reg, i, del;
7224 if (expr->reaching_reg == NULL_RTX)
7225 expr->reaching_reg = gen_reg_rtx (GET_MODE (expr->pattern));
7228 /* If there is more than 1 store, the earlier ones will be dead,
7229 but it doesn't hurt to replace them here. */
7230 reg = expr->reaching_reg;
7232 for (i = AVAIL_STORE_LIST (expr); i; i = XEXP (i, 1))
7234 del = XEXP (i, 0);
7235 if (BLOCK_FOR_INSN (del) == bb)
7237 /* We know there is only one since we deleted redundant
7238 ones during the available computation. */
7239 replace_store_insn (reg, del, bb);
7240 break;
7245 /* Free memory used by store motion. */
7247 static void
7248 free_store_memory ()
7250 free_ldst_mems ();
7252 if (ae_gen)
7253 sbitmap_vector_free (ae_gen);
7254 if (ae_kill)
7255 sbitmap_vector_free (ae_kill);
7256 if (transp)
7257 sbitmap_vector_free (transp);
7258 if (st_antloc)
7259 sbitmap_vector_free (st_antloc);
7260 if (pre_insert_map)
7261 sbitmap_vector_free (pre_insert_map);
7262 if (pre_delete_map)
7263 sbitmap_vector_free (pre_delete_map);
7264 if (reg_set_in_block)
7265 sbitmap_vector_free (reg_set_in_block);
7267 ae_gen = ae_kill = transp = st_antloc = NULL;
7268 pre_insert_map = pre_delete_map = reg_set_in_block = NULL;
7271 /* Perform store motion. Much like gcse, except we move expressions the
7272 other way by looking at the flowgraph in reverse. */
7274 static void
7275 store_motion ()
7277 basic_block bb;
7278 int x;
7279 struct ls_expr * ptr;
7280 int update_flow = 0;
7282 if (gcse_file)
7284 fprintf (gcse_file, "before store motion\n");
7285 print_rtl (gcse_file, get_insns ());
7289 init_alias_analysis ();
7291 /* Find all the stores that are live to the end of their block. */
7292 num_stores = compute_store_table ();
7293 if (num_stores == 0)
7295 sbitmap_vector_free (reg_set_in_block);
7296 end_alias_analysis ();
7297 return;
7300 /* Now compute whats actually available to move. */
7301 add_noreturn_fake_exit_edges ();
7302 build_store_vectors ();
7304 edge_list = pre_edge_rev_lcm (gcse_file, num_stores, transp, ae_gen,
7305 st_antloc, ae_kill, &pre_insert_map,
7306 &pre_delete_map);
7308 /* Now we want to insert the new stores which are going to be needed. */
7309 for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
7311 FOR_EACH_BB (bb)
7312 if (TEST_BIT (pre_delete_map[bb->index], ptr->index))
7313 delete_store (ptr, bb);
7315 for (x = 0; x < NUM_EDGES (edge_list); x++)
7316 if (TEST_BIT (pre_insert_map[x], ptr->index))
7317 update_flow |= insert_store (ptr, INDEX_EDGE (edge_list, x));
7320 if (update_flow)
7321 commit_edge_insertions ();
7323 free_store_memory ();
7324 free_edge_list (edge_list);
7325 remove_fake_edges ();
7326 end_alias_analysis ();
7329 #include "gt-gcse.h"