2003-04-02 Aldy Hernandez <aldyh@redhat.com>
[official-gcc.git] / gcc / gcse.c
blobe7a6845581d3cc81bb263286cae0811ab6b51098
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, 2003
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 "coretypes.h"
149 #include "tm.h"
150 #include "toplev.h"
152 #include "rtl.h"
153 #include "tm_p.h"
154 #include "regs.h"
155 #include "hard-reg-set.h"
156 #include "flags.h"
157 #include "real.h"
158 #include "insn-config.h"
159 #include "recog.h"
160 #include "basic-block.h"
161 #include "output.h"
162 #include "function.h"
163 #include "expr.h"
164 #include "except.h"
165 #include "ggc.h"
166 #include "params.h"
167 #include "cselib.h"
169 #include "obstack.h"
171 /* Propagate flow information through back edges and thus enable PRE's
172 moving loop invariant calculations out of loops.
174 Originally this tended to create worse overall code, but several
175 improvements during the development of PRE seem to have made following
176 back edges generally a win.
178 Note much of the loop invariant code motion done here would normally
179 be done by loop.c, which has more heuristics for when to move invariants
180 out of loops. At some point we might need to move some of those
181 heuristics into gcse.c. */
183 /* We support GCSE via Partial Redundancy Elimination. PRE optimizations
184 are a superset of those done by GCSE.
186 We perform the following steps:
188 1) Compute basic block information.
190 2) Compute table of places where registers are set.
192 3) Perform copy/constant propagation.
194 4) Perform global cse.
196 5) Perform another pass of copy/constant propagation.
198 Two passes of copy/constant propagation are done because the first one
199 enables more GCSE and the second one helps to clean up the copies that
200 GCSE creates. This is needed more for PRE than for Classic because Classic
201 GCSE will try to use an existing register containing the common
202 subexpression rather than create a new one. This is harder to do for PRE
203 because of the code motion (which Classic GCSE doesn't do).
205 Expressions we are interested in GCSE-ing are of the form
206 (set (pseudo-reg) (expression)).
207 Function want_to_gcse_p says what these are.
209 PRE handles moving invariant expressions out of loops (by treating them as
210 partially redundant).
212 Eventually it would be nice to replace cse.c/gcse.c with SSA (static single
213 assignment) based GVN (global value numbering). L. T. Simpson's paper
214 (Rice University) on value numbering is a useful reference for this.
216 **********************
218 We used to support multiple passes but there are diminishing returns in
219 doing so. The first pass usually makes 90% of the changes that are doable.
220 A second pass can make a few more changes made possible by the first pass.
221 Experiments show any further passes don't make enough changes to justify
222 the expense.
224 A study of spec92 using an unlimited number of passes:
225 [1 pass] = 1208 substitutions, [2] = 577, [3] = 202, [4] = 192, [5] = 83,
226 [6] = 34, [7] = 17, [8] = 9, [9] = 4, [10] = 4, [11] = 2,
227 [12] = 2, [13] = 1, [15] = 1, [16] = 2, [41] = 1
229 It was found doing copy propagation between each pass enables further
230 substitutions.
232 PRE is quite expensive in complicated functions because the DFA can take
233 awhile to converge. Hence we only perform one pass. The parameter max-gcse-passes can
234 be modified if one wants to experiment.
236 **********************
238 The steps for PRE are:
240 1) Build the hash table of expressions we wish to GCSE (expr_hash_table).
242 2) Perform the data flow analysis for PRE.
244 3) Delete the redundant instructions
246 4) Insert the required copies [if any] that make the partially
247 redundant instructions fully redundant.
249 5) For other reaching expressions, insert an instruction to copy the value
250 to a newly created pseudo that will reach the redundant instruction.
252 The deletion is done first so that when we do insertions we
253 know which pseudo reg to use.
255 Various papers have argued that PRE DFA is expensive (O(n^2)) and others
256 argue it is not. The number of iterations for the algorithm to converge
257 is typically 2-4 so I don't view it as that expensive (relatively speaking).
259 PRE GCSE depends heavily on the second CSE pass to clean up the copies
260 we create. To make an expression reach the place where it's redundant,
261 the result of the expression is copied to a new register, and the redundant
262 expression is deleted by replacing it with this new register. Classic GCSE
263 doesn't have this problem as much as it computes the reaching defs of
264 each register in each block and thus can try to use an existing register.
266 **********************
268 A fair bit of simplicity is created by creating small functions for simple
269 tasks, even when the function is only called in one place. This may
270 measurably slow things down [or may not] by creating more function call
271 overhead than is necessary. The source is laid out so that it's trivial
272 to make the affected functions inline so that one can measure what speed
273 up, if any, can be achieved, and maybe later when things settle things can
274 be rearranged.
276 Help stamp out big monolithic functions! */
278 /* GCSE global vars. */
280 /* -dG dump file. */
281 static FILE *gcse_file;
283 /* Note whether or not we should run jump optimization after gcse. We
284 want to do this for two cases.
286 * If we changed any jumps via cprop.
288 * If we added any labels via edge splitting. */
290 static int run_jump_opt_after_gcse;
292 /* Bitmaps are normally not included in debugging dumps.
293 However it's useful to be able to print them from GDB.
294 We could create special functions for this, but it's simpler to
295 just allow passing stderr to the dump_foo fns. Since stderr can
296 be a macro, we store a copy here. */
297 static FILE *debug_stderr;
299 /* An obstack for our working variables. */
300 static struct obstack gcse_obstack;
302 /* Nonzero for each mode that supports (set (reg) (reg)).
303 This is trivially true for integer and floating point values.
304 It may or may not be true for condition codes. */
305 static char can_copy_p[(int) NUM_MACHINE_MODES];
307 /* Nonzero if can_copy_p has been initialized. */
308 static int can_copy_init_p;
310 struct reg_use {rtx reg_rtx; };
312 /* Hash table of expressions. */
314 struct expr
316 /* The expression (SET_SRC for expressions, PATTERN for assignments). */
317 rtx expr;
318 /* Index in the available expression bitmaps. */
319 int bitmap_index;
320 /* Next entry with the same hash. */
321 struct expr *next_same_hash;
322 /* List of anticipatable occurrences in basic blocks in the function.
323 An "anticipatable occurrence" is one that is the first occurrence in the
324 basic block, the operands are not modified in the basic block prior
325 to the occurrence and the output is not used between the start of
326 the block and the occurrence. */
327 struct occr *antic_occr;
328 /* List of available occurrence in basic blocks in the function.
329 An "available occurrence" is one that is the last occurrence in the
330 basic block and the operands are not modified by following statements in
331 the basic block [including this insn]. */
332 struct occr *avail_occr;
333 /* Non-null if the computation is PRE redundant.
334 The value is the newly created pseudo-reg to record a copy of the
335 expression in all the places that reach the redundant copy. */
336 rtx reaching_reg;
339 /* Occurrence of an expression.
340 There is one per basic block. If a pattern appears more than once the
341 last appearance is used [or first for anticipatable expressions]. */
343 struct occr
345 /* Next occurrence of this expression. */
346 struct occr *next;
347 /* The insn that computes the expression. */
348 rtx insn;
349 /* Nonzero if this [anticipatable] occurrence has been deleted. */
350 char deleted_p;
351 /* Nonzero if this [available] occurrence has been copied to
352 reaching_reg. */
353 /* ??? This is mutually exclusive with deleted_p, so they could share
354 the same byte. */
355 char copied_p;
358 /* Expression and copy propagation hash tables.
359 Each hash table is an array of buckets.
360 ??? It is known that if it were an array of entries, structure elements
361 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
362 not clear whether in the final analysis a sufficient amount of memory would
363 be saved as the size of the available expression bitmaps would be larger
364 [one could build a mapping table without holes afterwards though].
365 Someday I'll perform the computation and figure it out. */
367 struct hash_table
369 /* The table itself.
370 This is an array of `expr_hash_table_size' elements. */
371 struct expr **table;
373 /* Size of the hash table, in elements. */
374 unsigned int size;
376 /* Number of hash table elements. */
377 unsigned int n_elems;
379 /* Whether the table is expression of copy propagation one. */
380 int set_p;
383 /* Expression hash table. */
384 static struct hash_table expr_hash_table;
386 /* Copy propagation hash table. */
387 static struct hash_table set_hash_table;
389 /* Mapping of uids to cuids.
390 Only real insns get cuids. */
391 static int *uid_cuid;
393 /* Highest UID in UID_CUID. */
394 static int max_uid;
396 /* Get the cuid of an insn. */
397 #ifdef ENABLE_CHECKING
398 #define INSN_CUID(INSN) (INSN_UID (INSN) > max_uid ? (abort (), 0) : uid_cuid[INSN_UID (INSN)])
399 #else
400 #define INSN_CUID(INSN) (uid_cuid[INSN_UID (INSN)])
401 #endif
403 /* Number of cuids. */
404 static int max_cuid;
406 /* Mapping of cuids to insns. */
407 static rtx *cuid_insn;
409 /* Get insn from cuid. */
410 #define CUID_INSN(CUID) (cuid_insn[CUID])
412 /* Maximum register number in function prior to doing gcse + 1.
413 Registers created during this pass have regno >= max_gcse_regno.
414 This is named with "gcse" to not collide with global of same name. */
415 static unsigned int max_gcse_regno;
417 /* Table of registers that are modified.
419 For each register, each element is a list of places where the pseudo-reg
420 is set.
422 For simplicity, GCSE is done on sets of pseudo-regs only. PRE GCSE only
423 requires knowledge of which blocks kill which regs [and thus could use
424 a bitmap instead of the lists `reg_set_table' uses].
426 `reg_set_table' and could be turned into an array of bitmaps (num-bbs x
427 num-regs) [however perhaps it may be useful to keep the data as is]. One
428 advantage of recording things this way is that `reg_set_table' is fairly
429 sparse with respect to pseudo regs but for hard regs could be fairly dense
430 [relatively speaking]. And recording sets of pseudo-regs in lists speeds
431 up functions like compute_transp since in the case of pseudo-regs we only
432 need to iterate over the number of times a pseudo-reg is set, not over the
433 number of basic blocks [clearly there is a bit of a slow down in the cases
434 where a pseudo is set more than once in a block, however it is believed
435 that the net effect is to speed things up]. This isn't done for hard-regs
436 because recording call-clobbered hard-regs in `reg_set_table' at each
437 function call can consume a fair bit of memory, and iterating over
438 hard-regs stored this way in compute_transp will be more expensive. */
440 typedef struct reg_set
442 /* The next setting of this register. */
443 struct reg_set *next;
444 /* The insn where it was set. */
445 rtx insn;
446 } reg_set;
448 static reg_set **reg_set_table;
450 /* Size of `reg_set_table'.
451 The table starts out at max_gcse_regno + slop, and is enlarged as
452 necessary. */
453 static int reg_set_table_size;
455 /* Amount to grow `reg_set_table' by when it's full. */
456 #define REG_SET_TABLE_SLOP 100
458 /* This is a list of expressions which are MEMs and will be used by load
459 or store motion.
460 Load motion tracks MEMs which aren't killed by
461 anything except itself. (ie, loads and stores to a single location).
462 We can then allow movement of these MEM refs with a little special
463 allowance. (all stores copy the same value to the reaching reg used
464 for the loads). This means all values used to store into memory must have
465 no side effects so we can re-issue the setter value.
466 Store Motion uses this structure as an expression table to track stores
467 which look interesting, and might be moveable towards the exit block. */
469 struct ls_expr
471 struct expr * expr; /* Gcse expression reference for LM. */
472 rtx pattern; /* Pattern of this mem. */
473 rtx pattern_regs; /* List of registers mentioned by the 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 /* Array of implicit set patterns indexed by basic block index. */
484 static rtx *implicit_sets;
486 /* Head of the list of load/store memory refs. */
487 static struct ls_expr * pre_ldst_mems = NULL;
489 /* Bitmap containing one bit for each register in the program.
490 Used when performing GCSE to track which registers have been set since
491 the start of the basic block. */
492 static regset reg_set_bitmap;
494 /* For each block, a bitmap of registers set in the block.
495 This is used by expr_killed_p and compute_transp.
496 It is computed during hash table computation and not by compute_sets
497 as it includes registers added since the last pass (or between cprop and
498 gcse) and it's currently not easy to realloc sbitmap vectors. */
499 static sbitmap *reg_set_in_block;
501 /* Array, indexed by basic block number for a list of insns which modify
502 memory within that block. */
503 static rtx * modify_mem_list;
504 bitmap modify_mem_list_set;
506 /* This array parallels modify_mem_list, but is kept canonicalized. */
507 static rtx * canon_modify_mem_list;
508 bitmap canon_modify_mem_list_set;
509 /* Various variables for statistics gathering. */
511 /* Memory used in a pass.
512 This isn't intended to be absolutely precise. Its intent is only
513 to keep an eye on memory usage. */
514 static int bytes_used;
516 /* GCSE substitutions made. */
517 static int gcse_subst_count;
518 /* Number of copy instructions created. */
519 static int gcse_create_count;
520 /* Number of constants propagated. */
521 static int const_prop_count;
522 /* Number of copys propagated. */
523 static int copy_prop_count;
525 /* These variables are used by classic GCSE.
526 Normally they'd be defined a bit later, but `rd_gen' needs to
527 be declared sooner. */
529 /* Each block has a bitmap of each type.
530 The length of each blocks bitmap is:
532 max_cuid - for reaching definitions
533 n_exprs - for available expressions
535 Thus we view the bitmaps as 2 dimensional arrays. i.e.
536 rd_kill[block_num][cuid_num]
537 ae_kill[block_num][expr_num] */
539 /* For reaching defs */
540 static sbitmap *rd_kill, *rd_gen, *reaching_defs, *rd_out;
542 /* for available exprs */
543 static sbitmap *ae_kill, *ae_gen, *ae_in, *ae_out;
545 /* Objects of this type are passed around by the null-pointer check
546 removal routines. */
547 struct null_pointer_info
549 /* The basic block being processed. */
550 basic_block current_block;
551 /* The first register to be handled in this pass. */
552 unsigned int min_reg;
553 /* One greater than the last register to be handled in this pass. */
554 unsigned int max_reg;
555 sbitmap *nonnull_local;
556 sbitmap *nonnull_killed;
559 static void compute_can_copy PARAMS ((void));
560 static char *gmalloc PARAMS ((unsigned int));
561 static char *grealloc PARAMS ((char *, unsigned int));
562 static char *gcse_alloc PARAMS ((unsigned long));
563 static void alloc_gcse_mem PARAMS ((rtx));
564 static void free_gcse_mem PARAMS ((void));
565 static void alloc_reg_set_mem PARAMS ((int));
566 static void free_reg_set_mem PARAMS ((void));
567 static int get_bitmap_width PARAMS ((int, int, int));
568 static void record_one_set PARAMS ((int, rtx));
569 static void record_set_info PARAMS ((rtx, rtx, void *));
570 static void compute_sets PARAMS ((rtx));
571 static void hash_scan_insn PARAMS ((rtx, struct hash_table *, int));
572 static void hash_scan_set PARAMS ((rtx, rtx, struct hash_table *));
573 static void hash_scan_clobber PARAMS ((rtx, rtx, struct hash_table *));
574 static void hash_scan_call PARAMS ((rtx, rtx, struct hash_table *));
575 static int want_to_gcse_p PARAMS ((rtx));
576 static bool gcse_constant_p PARAMS ((rtx));
577 static int oprs_unchanged_p PARAMS ((rtx, rtx, int));
578 static int oprs_anticipatable_p PARAMS ((rtx, rtx));
579 static int oprs_available_p PARAMS ((rtx, rtx));
580 static void insert_expr_in_table PARAMS ((rtx, enum machine_mode, rtx,
581 int, int, struct hash_table *));
582 static void insert_set_in_table PARAMS ((rtx, rtx, struct hash_table *));
583 static unsigned int hash_expr PARAMS ((rtx, enum machine_mode, int *, int));
584 static unsigned int hash_expr_1 PARAMS ((rtx, enum machine_mode, int *));
585 static unsigned int hash_string_1 PARAMS ((const char *));
586 static unsigned int hash_set PARAMS ((int, int));
587 static int expr_equiv_p PARAMS ((rtx, rtx));
588 static void record_last_reg_set_info PARAMS ((rtx, int));
589 static void record_last_mem_set_info PARAMS ((rtx));
590 static void record_last_set_info PARAMS ((rtx, rtx, void *));
591 static void compute_hash_table PARAMS ((struct hash_table *));
592 static void alloc_hash_table PARAMS ((int, struct hash_table *, int));
593 static void free_hash_table PARAMS ((struct hash_table *));
594 static void compute_hash_table_work PARAMS ((struct hash_table *));
595 static void dump_hash_table PARAMS ((FILE *, const char *,
596 struct hash_table *));
597 static struct expr *lookup_expr PARAMS ((rtx, struct hash_table *));
598 static struct expr *lookup_set PARAMS ((unsigned int, struct hash_table *));
599 static struct expr *next_set PARAMS ((unsigned int, struct expr *));
600 static void reset_opr_set_tables PARAMS ((void));
601 static int oprs_not_set_p PARAMS ((rtx, rtx));
602 static void mark_call PARAMS ((rtx));
603 static void mark_set PARAMS ((rtx, rtx));
604 static void mark_clobber PARAMS ((rtx, rtx));
605 static void mark_oprs_set PARAMS ((rtx));
606 static void alloc_cprop_mem PARAMS ((int, int));
607 static void free_cprop_mem PARAMS ((void));
608 static void compute_transp PARAMS ((rtx, int, sbitmap *, int));
609 static void compute_transpout PARAMS ((void));
610 static void compute_local_properties PARAMS ((sbitmap *, sbitmap *, sbitmap *,
611 struct hash_table *));
612 static void compute_cprop_data PARAMS ((void));
613 static void find_used_regs PARAMS ((rtx *, void *));
614 static int try_replace_reg PARAMS ((rtx, rtx, rtx));
615 static struct expr *find_avail_set PARAMS ((int, rtx));
616 static int cprop_jump PARAMS ((basic_block, rtx, rtx, rtx, rtx));
617 static void mems_conflict_for_gcse_p PARAMS ((rtx, rtx, void *));
618 static int load_killed_in_block_p PARAMS ((basic_block, int, rtx, int));
619 static void canon_list_insert PARAMS ((rtx, rtx, void *));
620 static int cprop_insn PARAMS ((rtx, int));
621 static int cprop PARAMS ((int));
622 static rtx fis_get_condition PARAMS ((rtx));
623 static void find_implicit_sets PARAMS ((void));
624 static int one_cprop_pass PARAMS ((int, int, int));
625 static bool constprop_register PARAMS ((rtx, rtx, rtx, int));
626 static struct expr *find_bypass_set PARAMS ((int, int));
627 static bool reg_killed_on_edge PARAMS ((rtx, edge));
628 static int bypass_block PARAMS ((basic_block, rtx, rtx));
629 static int bypass_conditional_jumps PARAMS ((void));
630 static void alloc_pre_mem PARAMS ((int, int));
631 static void free_pre_mem PARAMS ((void));
632 static void compute_pre_data PARAMS ((void));
633 static int pre_expr_reaches_here_p PARAMS ((basic_block, struct expr *,
634 basic_block));
635 static void insert_insn_end_bb PARAMS ((struct expr *, basic_block, int));
636 static void pre_insert_copy_insn PARAMS ((struct expr *, rtx));
637 static void pre_insert_copies PARAMS ((void));
638 static int pre_delete PARAMS ((void));
639 static int pre_gcse PARAMS ((void));
640 static int one_pre_gcse_pass PARAMS ((int));
641 static void add_label_notes PARAMS ((rtx, rtx));
642 static void alloc_code_hoist_mem PARAMS ((int, int));
643 static void free_code_hoist_mem PARAMS ((void));
644 static void compute_code_hoist_vbeinout PARAMS ((void));
645 static void compute_code_hoist_data PARAMS ((void));
646 static int hoist_expr_reaches_here_p PARAMS ((basic_block, int, basic_block,
647 char *));
648 static void hoist_code PARAMS ((void));
649 static int one_code_hoisting_pass PARAMS ((void));
650 static void alloc_rd_mem PARAMS ((int, int));
651 static void free_rd_mem PARAMS ((void));
652 static void handle_rd_kill_set PARAMS ((rtx, int, basic_block));
653 static void compute_kill_rd PARAMS ((void));
654 static void compute_rd PARAMS ((void));
655 static void alloc_avail_expr_mem PARAMS ((int, int));
656 static void free_avail_expr_mem PARAMS ((void));
657 static void compute_ae_gen PARAMS ((struct hash_table *));
658 static int expr_killed_p PARAMS ((rtx, basic_block));
659 static void compute_ae_kill PARAMS ((sbitmap *, sbitmap *, struct hash_table *));
660 static int expr_reaches_here_p PARAMS ((struct occr *, struct expr *,
661 basic_block, int));
662 static rtx computing_insn PARAMS ((struct expr *, rtx));
663 static int def_reaches_here_p PARAMS ((rtx, rtx));
664 static int can_disregard_other_sets PARAMS ((struct reg_set **, rtx, int));
665 static int handle_avail_expr PARAMS ((rtx, struct expr *));
666 static int classic_gcse PARAMS ((void));
667 static int one_classic_gcse_pass PARAMS ((int));
668 static void invalidate_nonnull_info PARAMS ((rtx, rtx, void *));
669 static int delete_null_pointer_checks_1 PARAMS ((unsigned int *,
670 sbitmap *, sbitmap *,
671 struct null_pointer_info *));
672 static rtx process_insert_insn PARAMS ((struct expr *));
673 static int pre_edge_insert PARAMS ((struct edge_list *, struct expr **));
674 static int expr_reaches_here_p_work PARAMS ((struct occr *, struct expr *,
675 basic_block, int, char *));
676 static int pre_expr_reaches_here_p_work PARAMS ((basic_block, struct expr *,
677 basic_block, char *));
678 static struct ls_expr * ldst_entry PARAMS ((rtx));
679 static void free_ldst_entry PARAMS ((struct ls_expr *));
680 static void free_ldst_mems PARAMS ((void));
681 static void print_ldst_list PARAMS ((FILE *));
682 static struct ls_expr * find_rtx_in_ldst PARAMS ((rtx));
683 static int enumerate_ldsts PARAMS ((void));
684 static inline struct ls_expr * first_ls_expr PARAMS ((void));
685 static inline struct ls_expr * next_ls_expr PARAMS ((struct ls_expr *));
686 static int simple_mem PARAMS ((rtx));
687 static void invalidate_any_buried_refs PARAMS ((rtx));
688 static void compute_ld_motion_mems PARAMS ((void));
689 static void trim_ld_motion_mems PARAMS ((void));
690 static void update_ld_motion_stores PARAMS ((struct expr *));
691 static void reg_set_info PARAMS ((rtx, rtx, void *));
692 static bool store_ops_ok PARAMS ((rtx, int *));
693 static rtx extract_mentioned_regs PARAMS ((rtx));
694 static rtx extract_mentioned_regs_helper PARAMS ((rtx, rtx));
695 static void find_moveable_store PARAMS ((rtx, int *, int *));
696 static int compute_store_table PARAMS ((void));
697 static bool load_kills_store PARAMS ((rtx, rtx));
698 static bool find_loads PARAMS ((rtx, rtx));
699 static bool store_killed_in_insn PARAMS ((rtx, rtx, rtx));
700 static bool store_killed_after PARAMS ((rtx, rtx, rtx, basic_block,
701 int *, rtx *));
702 static bool store_killed_before PARAMS ((rtx, rtx, rtx, basic_block,
703 int *));
704 static void build_store_vectors PARAMS ((void));
705 static void insert_insn_start_bb PARAMS ((rtx, basic_block));
706 static int insert_store PARAMS ((struct ls_expr *, edge));
707 static void replace_store_insn PARAMS ((rtx, rtx, basic_block));
708 static void delete_store PARAMS ((struct ls_expr *,
709 basic_block));
710 static void free_store_memory PARAMS ((void));
711 static void store_motion PARAMS ((void));
712 static void free_insn_expr_list_list PARAMS ((rtx *));
713 static void clear_modify_mem_tables PARAMS ((void));
714 static void free_modify_mem_tables PARAMS ((void));
715 static rtx gcse_emit_move_after PARAMS ((rtx, rtx, rtx));
716 static void local_cprop_find_used_regs PARAMS ((rtx *, void *));
717 static bool do_local_cprop PARAMS ((rtx, rtx, int, rtx*));
718 static bool adjust_libcall_notes PARAMS ((rtx, rtx, rtx, rtx*));
719 static void local_cprop_pass PARAMS ((int));
721 /* Entry point for global common subexpression elimination.
722 F is the first instruction in the function. */
725 gcse_main (f, file)
726 rtx f;
727 FILE *file;
729 int changed, pass;
730 /* Bytes used at start of pass. */
731 int initial_bytes_used;
732 /* Maximum number of bytes used by a pass. */
733 int max_pass_bytes;
734 /* Point to release obstack data from for each pass. */
735 char *gcse_obstack_bottom;
737 /* We do not construct an accurate cfg in functions which call
738 setjmp, so just punt to be safe. */
739 if (current_function_calls_setjmp)
740 return 0;
742 /* Assume that we do not need to run jump optimizations after gcse. */
743 run_jump_opt_after_gcse = 0;
745 /* For calling dump_foo fns from gdb. */
746 debug_stderr = stderr;
747 gcse_file = file;
749 /* Identify the basic block information for this function, including
750 successors and predecessors. */
751 max_gcse_regno = max_reg_num ();
753 if (file)
754 dump_flow_info (file);
756 /* Return if there's nothing to do. */
757 if (n_basic_blocks <= 1)
758 return 0;
760 /* Trying to perform global optimizations on flow graphs which have
761 a high connectivity will take a long time and is unlikely to be
762 particularly useful.
764 In normal circumstances a cfg should have about twice as many edges
765 as blocks. But we do not want to punish small functions which have
766 a couple switch statements. So we require a relatively large number
767 of basic blocks and the ratio of edges to blocks to be high. */
768 if (n_basic_blocks > 1000 && n_edges / n_basic_blocks >= 20)
770 if (warn_disabled_optimization)
771 warning ("GCSE disabled: %d > 1000 basic blocks and %d >= 20 edges/basic block",
772 n_basic_blocks, n_edges / n_basic_blocks);
773 return 0;
776 /* If allocating memory for the cprop bitmap would take up too much
777 storage it's better just to disable the optimization. */
778 if ((n_basic_blocks
779 * SBITMAP_SET_SIZE (max_gcse_regno)
780 * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
782 if (warn_disabled_optimization)
783 warning ("GCSE disabled: %d basic blocks and %d registers",
784 n_basic_blocks, max_gcse_regno);
786 return 0;
789 /* See what modes support reg/reg copy operations. */
790 if (! can_copy_init_p)
792 compute_can_copy ();
793 can_copy_init_p = 1;
796 gcc_obstack_init (&gcse_obstack);
797 bytes_used = 0;
799 /* We need alias. */
800 init_alias_analysis ();
801 /* Record where pseudo-registers are set. This data is kept accurate
802 during each pass. ??? We could also record hard-reg information here
803 [since it's unchanging], however it is currently done during hash table
804 computation.
806 It may be tempting to compute MEM set information here too, but MEM sets
807 will be subject to code motion one day and thus we need to compute
808 information about memory sets when we build the hash tables. */
810 alloc_reg_set_mem (max_gcse_regno);
811 compute_sets (f);
813 pass = 0;
814 initial_bytes_used = bytes_used;
815 max_pass_bytes = 0;
816 gcse_obstack_bottom = gcse_alloc (1);
817 changed = 1;
818 while (changed && pass < MAX_GCSE_PASSES)
820 changed = 0;
821 if (file)
822 fprintf (file, "GCSE pass %d\n\n", pass + 1);
824 /* Initialize bytes_used to the space for the pred/succ lists,
825 and the reg_set_table data. */
826 bytes_used = initial_bytes_used;
828 /* Each pass may create new registers, so recalculate each time. */
829 max_gcse_regno = max_reg_num ();
831 alloc_gcse_mem (f);
833 /* Don't allow constant propagation to modify jumps
834 during this pass. */
835 changed = one_cprop_pass (pass + 1, 0, 0);
837 if (optimize_size)
838 changed |= one_classic_gcse_pass (pass + 1);
839 else
841 changed |= one_pre_gcse_pass (pass + 1);
842 /* We may have just created new basic blocks. Release and
843 recompute various things which are sized on the number of
844 basic blocks. */
845 if (changed)
847 free_modify_mem_tables ();
848 modify_mem_list
849 = (rtx *) gmalloc (last_basic_block * sizeof (rtx));
850 canon_modify_mem_list
851 = (rtx *) gmalloc (last_basic_block * sizeof (rtx));
852 memset ((char *) modify_mem_list, 0, last_basic_block * sizeof (rtx));
853 memset ((char *) canon_modify_mem_list, 0, last_basic_block * sizeof (rtx));
855 free_reg_set_mem ();
856 alloc_reg_set_mem (max_reg_num ());
857 compute_sets (f);
858 run_jump_opt_after_gcse = 1;
861 if (max_pass_bytes < bytes_used)
862 max_pass_bytes = bytes_used;
864 /* Free up memory, then reallocate for code hoisting. We can
865 not re-use the existing allocated memory because the tables
866 will not have info for the insns or registers created by
867 partial redundancy elimination. */
868 free_gcse_mem ();
870 /* It does not make sense to run code hoisting unless we optimizing
871 for code size -- it rarely makes programs faster, and can make
872 them bigger if we did partial redundancy elimination (when optimizing
873 for space, we use a classic gcse algorithm instead of partial
874 redundancy algorithms). */
875 if (optimize_size)
877 max_gcse_regno = max_reg_num ();
878 alloc_gcse_mem (f);
879 changed |= one_code_hoisting_pass ();
880 free_gcse_mem ();
882 if (max_pass_bytes < bytes_used)
883 max_pass_bytes = bytes_used;
886 if (file)
888 fprintf (file, "\n");
889 fflush (file);
892 obstack_free (&gcse_obstack, gcse_obstack_bottom);
893 pass++;
896 /* Do one last pass of copy propagation, including cprop into
897 conditional jumps. */
899 max_gcse_regno = max_reg_num ();
900 alloc_gcse_mem (f);
901 /* This time, go ahead and allow cprop to alter jumps. */
902 one_cprop_pass (pass + 1, 1, 0);
903 free_gcse_mem ();
905 if (file)
907 fprintf (file, "GCSE of %s: %d basic blocks, ",
908 current_function_name, n_basic_blocks);
909 fprintf (file, "%d pass%s, %d bytes\n\n",
910 pass, pass > 1 ? "es" : "", max_pass_bytes);
913 obstack_free (&gcse_obstack, NULL);
914 free_reg_set_mem ();
915 /* We are finished with alias. */
916 end_alias_analysis ();
917 allocate_reg_info (max_reg_num (), FALSE, FALSE);
919 if (!optimize_size && flag_gcse_sm)
920 store_motion ();
922 /* Record where pseudo-registers are set. */
923 return run_jump_opt_after_gcse;
926 /* Misc. utilities. */
928 /* Compute which modes support reg/reg copy operations. */
930 static void
931 compute_can_copy ()
933 int i;
934 #ifndef AVOID_CCMODE_COPIES
935 rtx reg, insn;
936 #endif
937 memset (can_copy_p, 0, NUM_MACHINE_MODES);
939 start_sequence ();
940 for (i = 0; i < NUM_MACHINE_MODES; i++)
941 if (GET_MODE_CLASS (i) == MODE_CC)
943 #ifdef AVOID_CCMODE_COPIES
944 can_copy_p[i] = 0;
945 #else
946 reg = gen_rtx_REG ((enum machine_mode) i, LAST_VIRTUAL_REGISTER + 1);
947 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, reg));
948 if (recog (PATTERN (insn), insn, NULL) >= 0)
949 can_copy_p[i] = 1;
950 #endif
952 else
953 can_copy_p[i] = 1;
955 end_sequence ();
958 /* Cover function to xmalloc to record bytes allocated. */
960 static char *
961 gmalloc (size)
962 unsigned int size;
964 bytes_used += size;
965 return xmalloc (size);
968 /* Cover function to xrealloc.
969 We don't record the additional size since we don't know it.
970 It won't affect memory usage stats much anyway. */
972 static char *
973 grealloc (ptr, size)
974 char *ptr;
975 unsigned int size;
977 return xrealloc (ptr, size);
980 /* Cover function to obstack_alloc. */
982 static char *
983 gcse_alloc (size)
984 unsigned long size;
986 bytes_used += size;
987 return (char *) obstack_alloc (&gcse_obstack, size);
990 /* Allocate memory for the cuid mapping array,
991 and reg/memory set tracking tables.
993 This is called at the start of each pass. */
995 static void
996 alloc_gcse_mem (f)
997 rtx f;
999 int i, n;
1000 rtx insn;
1002 /* Find the largest UID and create a mapping from UIDs to CUIDs.
1003 CUIDs are like UIDs except they increase monotonically, have no gaps,
1004 and only apply to real insns. */
1006 max_uid = get_max_uid ();
1007 n = (max_uid + 1) * sizeof (int);
1008 uid_cuid = (int *) gmalloc (n);
1009 memset ((char *) uid_cuid, 0, n);
1010 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
1012 if (INSN_P (insn))
1013 uid_cuid[INSN_UID (insn)] = i++;
1014 else
1015 uid_cuid[INSN_UID (insn)] = i;
1018 /* Create a table mapping cuids to insns. */
1020 max_cuid = i;
1021 n = (max_cuid + 1) * sizeof (rtx);
1022 cuid_insn = (rtx *) gmalloc (n);
1023 memset ((char *) cuid_insn, 0, n);
1024 for (insn = f, i = 0; insn; insn = NEXT_INSN (insn))
1025 if (INSN_P (insn))
1026 CUID_INSN (i++) = insn;
1028 /* Allocate vars to track sets of regs. */
1029 reg_set_bitmap = BITMAP_XMALLOC ();
1031 /* Allocate vars to track sets of regs, memory per block. */
1032 reg_set_in_block = (sbitmap *) sbitmap_vector_alloc (last_basic_block,
1033 max_gcse_regno);
1034 /* Allocate array to keep a list of insns which modify memory in each
1035 basic block. */
1036 modify_mem_list = (rtx *) gmalloc (last_basic_block * sizeof (rtx));
1037 canon_modify_mem_list = (rtx *) gmalloc (last_basic_block * sizeof (rtx));
1038 memset ((char *) modify_mem_list, 0, last_basic_block * sizeof (rtx));
1039 memset ((char *) canon_modify_mem_list, 0, last_basic_block * sizeof (rtx));
1040 modify_mem_list_set = BITMAP_XMALLOC ();
1041 canon_modify_mem_list_set = BITMAP_XMALLOC ();
1044 /* Free memory allocated by alloc_gcse_mem. */
1046 static void
1047 free_gcse_mem ()
1049 free (uid_cuid);
1050 free (cuid_insn);
1052 BITMAP_XFREE (reg_set_bitmap);
1054 sbitmap_vector_free (reg_set_in_block);
1055 free_modify_mem_tables ();
1056 BITMAP_XFREE (modify_mem_list_set);
1057 BITMAP_XFREE (canon_modify_mem_list_set);
1060 /* Many of the global optimization algorithms work by solving dataflow
1061 equations for various expressions. Initially, some local value is
1062 computed for each expression in each block. Then, the values across the
1063 various blocks are combined (by following flow graph edges) to arrive at
1064 global values. Conceptually, each set of equations is independent. We
1065 may therefore solve all the equations in parallel, solve them one at a
1066 time, or pick any intermediate approach.
1068 When you're going to need N two-dimensional bitmaps, each X (say, the
1069 number of blocks) by Y (say, the number of expressions), call this
1070 function. It's not important what X and Y represent; only that Y
1071 correspond to the things that can be done in parallel. This function will
1072 return an appropriate chunking factor C; you should solve C sets of
1073 equations in parallel. By going through this function, we can easily
1074 trade space against time; by solving fewer equations in parallel we use
1075 less space. */
1077 static int
1078 get_bitmap_width (n, x, y)
1079 int n;
1080 int x;
1081 int y;
1083 /* It's not really worth figuring out *exactly* how much memory will
1084 be used by a particular choice. The important thing is to get
1085 something approximately right. */
1086 size_t max_bitmap_memory = 10 * 1024 * 1024;
1088 /* The number of bytes we'd use for a single column of minimum
1089 width. */
1090 size_t column_size = n * x * sizeof (SBITMAP_ELT_TYPE);
1092 /* Often, it's reasonable just to solve all the equations in
1093 parallel. */
1094 if (column_size * SBITMAP_SET_SIZE (y) <= max_bitmap_memory)
1095 return y;
1097 /* Otherwise, pick the largest width we can, without going over the
1098 limit. */
1099 return SBITMAP_ELT_BITS * ((max_bitmap_memory + column_size - 1)
1100 / column_size);
1103 /* Compute the local properties of each recorded expression.
1105 Local properties are those that are defined by the block, irrespective of
1106 other blocks.
1108 An expression is transparent in a block if its operands are not modified
1109 in the block.
1111 An expression is computed (locally available) in a block if it is computed
1112 at least once and expression would contain the same value if the
1113 computation was moved to the end of the block.
1115 An expression is locally anticipatable in a block if it is computed at
1116 least once and expression would contain the same value if the computation
1117 was moved to the beginning of the block.
1119 We call this routine for cprop, pre and code hoisting. They all compute
1120 basically the same information and thus can easily share this code.
1122 TRANSP, COMP, and ANTLOC are destination sbitmaps for recording local
1123 properties. If NULL, then it is not necessary to compute or record that
1124 particular property.
1126 TABLE controls which hash table to look at. If it is set hash table,
1127 additionally, TRANSP is computed as ~TRANSP, since this is really cprop's
1128 ABSALTERED. */
1130 static void
1131 compute_local_properties (transp, comp, antloc, table)
1132 sbitmap *transp;
1133 sbitmap *comp;
1134 sbitmap *antloc;
1135 struct hash_table *table;
1137 unsigned int i;
1139 /* Initialize any bitmaps that were passed in. */
1140 if (transp)
1142 if (table->set_p)
1143 sbitmap_vector_zero (transp, last_basic_block);
1144 else
1145 sbitmap_vector_ones (transp, last_basic_block);
1148 if (comp)
1149 sbitmap_vector_zero (comp, last_basic_block);
1150 if (antloc)
1151 sbitmap_vector_zero (antloc, last_basic_block);
1153 for (i = 0; i < table->size; i++)
1155 struct expr *expr;
1157 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
1159 int indx = expr->bitmap_index;
1160 struct occr *occr;
1162 /* The expression is transparent in this block if it is not killed.
1163 We start by assuming all are transparent [none are killed], and
1164 then reset the bits for those that are. */
1165 if (transp)
1166 compute_transp (expr->expr, indx, transp, table->set_p);
1168 /* The occurrences recorded in antic_occr are exactly those that
1169 we want to set to nonzero in ANTLOC. */
1170 if (antloc)
1171 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
1173 SET_BIT (antloc[BLOCK_NUM (occr->insn)], indx);
1175 /* While we're scanning the table, this is a good place to
1176 initialize this. */
1177 occr->deleted_p = 0;
1180 /* The occurrences recorded in avail_occr are exactly those that
1181 we want to set to nonzero in COMP. */
1182 if (comp)
1183 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
1185 SET_BIT (comp[BLOCK_NUM (occr->insn)], indx);
1187 /* While we're scanning the table, this is a good place to
1188 initialize this. */
1189 occr->copied_p = 0;
1192 /* While we're scanning the table, this is a good place to
1193 initialize this. */
1194 expr->reaching_reg = 0;
1199 /* Register set information.
1201 `reg_set_table' records where each register is set or otherwise
1202 modified. */
1204 static struct obstack reg_set_obstack;
1206 static void
1207 alloc_reg_set_mem (n_regs)
1208 int n_regs;
1210 unsigned int n;
1212 reg_set_table_size = n_regs + REG_SET_TABLE_SLOP;
1213 n = reg_set_table_size * sizeof (struct reg_set *);
1214 reg_set_table = (struct reg_set **) gmalloc (n);
1215 memset ((char *) reg_set_table, 0, n);
1217 gcc_obstack_init (&reg_set_obstack);
1220 static void
1221 free_reg_set_mem ()
1223 free (reg_set_table);
1224 obstack_free (&reg_set_obstack, NULL);
1227 /* Record REGNO in the reg_set table. */
1229 static void
1230 record_one_set (regno, insn)
1231 int regno;
1232 rtx insn;
1234 /* Allocate a new reg_set element and link it onto the list. */
1235 struct reg_set *new_reg_info;
1237 /* If the table isn't big enough, enlarge it. */
1238 if (regno >= reg_set_table_size)
1240 int new_size = regno + REG_SET_TABLE_SLOP;
1242 reg_set_table
1243 = (struct reg_set **) grealloc ((char *) reg_set_table,
1244 new_size * sizeof (struct reg_set *));
1245 memset ((char *) (reg_set_table + reg_set_table_size), 0,
1246 (new_size - reg_set_table_size) * sizeof (struct reg_set *));
1247 reg_set_table_size = new_size;
1250 new_reg_info = (struct reg_set *) obstack_alloc (&reg_set_obstack,
1251 sizeof (struct reg_set));
1252 bytes_used += sizeof (struct reg_set);
1253 new_reg_info->insn = insn;
1254 new_reg_info->next = reg_set_table[regno];
1255 reg_set_table[regno] = new_reg_info;
1258 /* Called from compute_sets via note_stores to handle one SET or CLOBBER in
1259 an insn. The DATA is really the instruction in which the SET is
1260 occurring. */
1262 static void
1263 record_set_info (dest, setter, data)
1264 rtx dest, setter ATTRIBUTE_UNUSED;
1265 void *data;
1267 rtx record_set_insn = (rtx) data;
1269 if (GET_CODE (dest) == REG && REGNO (dest) >= FIRST_PSEUDO_REGISTER)
1270 record_one_set (REGNO (dest), record_set_insn);
1273 /* Scan the function and record each set of each pseudo-register.
1275 This is called once, at the start of the gcse pass. See the comments for
1276 `reg_set_table' for further documentation. */
1278 static void
1279 compute_sets (f)
1280 rtx f;
1282 rtx insn;
1284 for (insn = f; insn != 0; insn = NEXT_INSN (insn))
1285 if (INSN_P (insn))
1286 note_stores (PATTERN (insn), record_set_info, insn);
1289 /* Hash table support. */
1291 struct reg_avail_info
1293 basic_block last_bb;
1294 int first_set;
1295 int last_set;
1298 static struct reg_avail_info *reg_avail_info;
1299 static basic_block current_bb;
1302 /* See whether X, the source of a set, is something we want to consider for
1303 GCSE. */
1305 static GTY(()) rtx test_insn;
1306 static int
1307 want_to_gcse_p (x)
1308 rtx x;
1310 int num_clobbers = 0;
1311 int icode;
1313 switch (GET_CODE (x))
1315 case REG:
1316 case SUBREG:
1317 case CONST_INT:
1318 case CONST_DOUBLE:
1319 case CONST_VECTOR:
1320 case CALL:
1321 case CONSTANT_P_RTX:
1322 return 0;
1324 default:
1325 break;
1328 /* If this is a valid operand, we are OK. If it's VOIDmode, we aren't. */
1329 if (general_operand (x, GET_MODE (x)))
1330 return 1;
1331 else if (GET_MODE (x) == VOIDmode)
1332 return 0;
1334 /* Otherwise, check if we can make a valid insn from it. First initialize
1335 our test insn if we haven't already. */
1336 if (test_insn == 0)
1338 test_insn
1339 = make_insn_raw (gen_rtx_SET (VOIDmode,
1340 gen_rtx_REG (word_mode,
1341 FIRST_PSEUDO_REGISTER * 2),
1342 const0_rtx));
1343 NEXT_INSN (test_insn) = PREV_INSN (test_insn) = 0;
1346 /* Now make an insn like the one we would make when GCSE'ing and see if
1347 valid. */
1348 PUT_MODE (SET_DEST (PATTERN (test_insn)), GET_MODE (x));
1349 SET_SRC (PATTERN (test_insn)) = x;
1350 return ((icode = recog (PATTERN (test_insn), test_insn, &num_clobbers)) >= 0
1351 && (num_clobbers == 0 || ! added_clobbers_hard_reg_p (icode)));
1354 /* Return nonzero if the operands of expression X are unchanged from the
1355 start of INSN's basic block up to but not including INSN (if AVAIL_P == 0),
1356 or from INSN to the end of INSN's basic block (if AVAIL_P != 0). */
1358 static int
1359 oprs_unchanged_p (x, insn, avail_p)
1360 rtx x, insn;
1361 int avail_p;
1363 int i, j;
1364 enum rtx_code code;
1365 const char *fmt;
1367 if (x == 0)
1368 return 1;
1370 code = GET_CODE (x);
1371 switch (code)
1373 case REG:
1375 struct reg_avail_info *info = &reg_avail_info[REGNO (x)];
1377 if (info->last_bb != current_bb)
1378 return 1;
1379 if (avail_p)
1380 return info->last_set < INSN_CUID (insn);
1381 else
1382 return info->first_set >= INSN_CUID (insn);
1385 case MEM:
1386 if (load_killed_in_block_p (current_bb, INSN_CUID (insn),
1387 x, avail_p))
1388 return 0;
1389 else
1390 return oprs_unchanged_p (XEXP (x, 0), insn, avail_p);
1392 case PRE_DEC:
1393 case PRE_INC:
1394 case POST_DEC:
1395 case POST_INC:
1396 case PRE_MODIFY:
1397 case POST_MODIFY:
1398 return 0;
1400 case PC:
1401 case CC0: /*FIXME*/
1402 case CONST:
1403 case CONST_INT:
1404 case CONST_DOUBLE:
1405 case CONST_VECTOR:
1406 case SYMBOL_REF:
1407 case LABEL_REF:
1408 case ADDR_VEC:
1409 case ADDR_DIFF_VEC:
1410 return 1;
1412 default:
1413 break;
1416 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
1418 if (fmt[i] == 'e')
1420 /* If we are about to do the last recursive call needed at this
1421 level, change it into iteration. This function is called enough
1422 to be worth it. */
1423 if (i == 0)
1424 return oprs_unchanged_p (XEXP (x, i), insn, avail_p);
1426 else if (! oprs_unchanged_p (XEXP (x, i), insn, avail_p))
1427 return 0;
1429 else if (fmt[i] == 'E')
1430 for (j = 0; j < XVECLEN (x, i); j++)
1431 if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, avail_p))
1432 return 0;
1435 return 1;
1438 /* Used for communication between mems_conflict_for_gcse_p and
1439 load_killed_in_block_p. Nonzero if mems_conflict_for_gcse_p finds a
1440 conflict between two memory references. */
1441 static int gcse_mems_conflict_p;
1443 /* Used for communication between mems_conflict_for_gcse_p and
1444 load_killed_in_block_p. A memory reference for a load instruction,
1445 mems_conflict_for_gcse_p will see if a memory store conflicts with
1446 this memory load. */
1447 static rtx gcse_mem_operand;
1449 /* DEST is the output of an instruction. If it is a memory reference, and
1450 possibly conflicts with the load found in gcse_mem_operand, then set
1451 gcse_mems_conflict_p to a nonzero value. */
1453 static void
1454 mems_conflict_for_gcse_p (dest, setter, data)
1455 rtx dest, setter ATTRIBUTE_UNUSED;
1456 void *data ATTRIBUTE_UNUSED;
1458 while (GET_CODE (dest) == SUBREG
1459 || GET_CODE (dest) == ZERO_EXTRACT
1460 || GET_CODE (dest) == SIGN_EXTRACT
1461 || GET_CODE (dest) == STRICT_LOW_PART)
1462 dest = XEXP (dest, 0);
1464 /* If DEST is not a MEM, then it will not conflict with the load. Note
1465 that function calls are assumed to clobber memory, but are handled
1466 elsewhere. */
1467 if (GET_CODE (dest) != MEM)
1468 return;
1470 /* If we are setting a MEM in our list of specially recognized MEMs,
1471 don't mark as killed this time. */
1473 if (expr_equiv_p (dest, gcse_mem_operand) && pre_ldst_mems != NULL)
1475 if (!find_rtx_in_ldst (dest))
1476 gcse_mems_conflict_p = 1;
1477 return;
1480 if (true_dependence (dest, GET_MODE (dest), gcse_mem_operand,
1481 rtx_addr_varies_p))
1482 gcse_mems_conflict_p = 1;
1485 /* Return nonzero if the expression in X (a memory reference) is killed
1486 in block BB before or after the insn with the CUID in UID_LIMIT.
1487 AVAIL_P is nonzero for kills after UID_LIMIT, and zero for kills
1488 before UID_LIMIT.
1490 To check the entire block, set UID_LIMIT to max_uid + 1 and
1491 AVAIL_P to 0. */
1493 static int
1494 load_killed_in_block_p (bb, uid_limit, x, avail_p)
1495 basic_block bb;
1496 int uid_limit;
1497 rtx x;
1498 int avail_p;
1500 rtx list_entry = modify_mem_list[bb->index];
1501 while (list_entry)
1503 rtx setter;
1504 /* Ignore entries in the list that do not apply. */
1505 if ((avail_p
1506 && INSN_CUID (XEXP (list_entry, 0)) < uid_limit)
1507 || (! avail_p
1508 && INSN_CUID (XEXP (list_entry, 0)) > uid_limit))
1510 list_entry = XEXP (list_entry, 1);
1511 continue;
1514 setter = XEXP (list_entry, 0);
1516 /* If SETTER is a call everything is clobbered. Note that calls
1517 to pure functions are never put on the list, so we need not
1518 worry about them. */
1519 if (GET_CODE (setter) == CALL_INSN)
1520 return 1;
1522 /* SETTER must be an INSN of some kind that sets memory. Call
1523 note_stores to examine each hunk of memory that is modified.
1525 The note_stores interface is pretty limited, so we have to
1526 communicate via global variables. Yuk. */
1527 gcse_mem_operand = x;
1528 gcse_mems_conflict_p = 0;
1529 note_stores (PATTERN (setter), mems_conflict_for_gcse_p, NULL);
1530 if (gcse_mems_conflict_p)
1531 return 1;
1532 list_entry = XEXP (list_entry, 1);
1534 return 0;
1537 /* Return nonzero if the operands of expression X are unchanged from
1538 the start of INSN's basic block up to but not including INSN. */
1540 static int
1541 oprs_anticipatable_p (x, insn)
1542 rtx x, insn;
1544 return oprs_unchanged_p (x, insn, 0);
1547 /* Return nonzero if the operands of expression X are unchanged from
1548 INSN to the end of INSN's basic block. */
1550 static int
1551 oprs_available_p (x, insn)
1552 rtx x, insn;
1554 return oprs_unchanged_p (x, insn, 1);
1557 /* Hash expression X.
1559 MODE is only used if X is a CONST_INT. DO_NOT_RECORD_P is a boolean
1560 indicating if a volatile operand is found or if the expression contains
1561 something we don't want to insert in the table.
1563 ??? One might want to merge this with canon_hash. Later. */
1565 static unsigned int
1566 hash_expr (x, mode, do_not_record_p, hash_table_size)
1567 rtx x;
1568 enum machine_mode mode;
1569 int *do_not_record_p;
1570 int hash_table_size;
1572 unsigned int hash;
1574 *do_not_record_p = 0;
1576 hash = hash_expr_1 (x, mode, do_not_record_p);
1577 return hash % hash_table_size;
1580 /* Hash a string. Just add its bytes up. */
1582 static inline unsigned
1583 hash_string_1 (ps)
1584 const char *ps;
1586 unsigned hash = 0;
1587 const unsigned char *p = (const unsigned char *) ps;
1589 if (p)
1590 while (*p)
1591 hash += *p++;
1593 return hash;
1596 /* Subroutine of hash_expr to do the actual work. */
1598 static unsigned int
1599 hash_expr_1 (x, mode, do_not_record_p)
1600 rtx x;
1601 enum machine_mode mode;
1602 int *do_not_record_p;
1604 int i, j;
1605 unsigned hash = 0;
1606 enum rtx_code code;
1607 const char *fmt;
1609 /* Used to turn recursion into iteration. We can't rely on GCC's
1610 tail-recursion elimination since we need to keep accumulating values
1611 in HASH. */
1613 if (x == 0)
1614 return hash;
1616 repeat:
1617 code = GET_CODE (x);
1618 switch (code)
1620 case REG:
1621 hash += ((unsigned int) REG << 7) + REGNO (x);
1622 return hash;
1624 case CONST_INT:
1625 hash += (((unsigned int) CONST_INT << 7) + (unsigned int) mode
1626 + (unsigned int) INTVAL (x));
1627 return hash;
1629 case CONST_DOUBLE:
1630 /* This is like the general case, except that it only counts
1631 the integers representing the constant. */
1632 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1633 if (GET_MODE (x) != VOIDmode)
1634 for (i = 2; i < GET_RTX_LENGTH (CONST_DOUBLE); i++)
1635 hash += (unsigned int) XWINT (x, i);
1636 else
1637 hash += ((unsigned int) CONST_DOUBLE_LOW (x)
1638 + (unsigned int) CONST_DOUBLE_HIGH (x));
1639 return hash;
1641 case CONST_VECTOR:
1643 int units;
1644 rtx elt;
1646 units = CONST_VECTOR_NUNITS (x);
1648 for (i = 0; i < units; ++i)
1650 elt = CONST_VECTOR_ELT (x, i);
1651 hash += hash_expr_1 (elt, GET_MODE (elt), do_not_record_p);
1654 return hash;
1657 /* Assume there is only one rtx object for any given label. */
1658 case LABEL_REF:
1659 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1660 differences and differences between each stage's debugging dumps. */
1661 hash += (((unsigned int) LABEL_REF << 7)
1662 + CODE_LABEL_NUMBER (XEXP (x, 0)));
1663 return hash;
1665 case SYMBOL_REF:
1667 /* Don't hash on the symbol's address to avoid bootstrap differences.
1668 Different hash values may cause expressions to be recorded in
1669 different orders and thus different registers to be used in the
1670 final assembler. This also avoids differences in the dump files
1671 between various stages. */
1672 unsigned int h = 0;
1673 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1675 while (*p)
1676 h += (h << 7) + *p++; /* ??? revisit */
1678 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1679 return hash;
1682 case MEM:
1683 if (MEM_VOLATILE_P (x))
1685 *do_not_record_p = 1;
1686 return 0;
1689 hash += (unsigned int) MEM;
1690 /* We used alias set for hashing, but this is not good, since the alias
1691 set may differ in -fprofile-arcs and -fbranch-probabilities compilation
1692 causing the profiles to fail to match. */
1693 x = XEXP (x, 0);
1694 goto repeat;
1696 case PRE_DEC:
1697 case PRE_INC:
1698 case POST_DEC:
1699 case POST_INC:
1700 case PC:
1701 case CC0:
1702 case CALL:
1703 case UNSPEC_VOLATILE:
1704 *do_not_record_p = 1;
1705 return 0;
1707 case ASM_OPERANDS:
1708 if (MEM_VOLATILE_P (x))
1710 *do_not_record_p = 1;
1711 return 0;
1713 else
1715 /* We don't want to take the filename and line into account. */
1716 hash += (unsigned) code + (unsigned) GET_MODE (x)
1717 + hash_string_1 (ASM_OPERANDS_TEMPLATE (x))
1718 + hash_string_1 (ASM_OPERANDS_OUTPUT_CONSTRAINT (x))
1719 + (unsigned) ASM_OPERANDS_OUTPUT_IDX (x);
1721 if (ASM_OPERANDS_INPUT_LENGTH (x))
1723 for (i = 1; i < ASM_OPERANDS_INPUT_LENGTH (x); i++)
1725 hash += (hash_expr_1 (ASM_OPERANDS_INPUT (x, i),
1726 GET_MODE (ASM_OPERANDS_INPUT (x, i)),
1727 do_not_record_p)
1728 + hash_string_1 (ASM_OPERANDS_INPUT_CONSTRAINT
1729 (x, i)));
1732 hash += hash_string_1 (ASM_OPERANDS_INPUT_CONSTRAINT (x, 0));
1733 x = ASM_OPERANDS_INPUT (x, 0);
1734 mode = GET_MODE (x);
1735 goto repeat;
1737 return hash;
1740 default:
1741 break;
1744 hash += (unsigned) code + (unsigned) GET_MODE (x);
1745 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
1747 if (fmt[i] == 'e')
1749 /* If we are about to do the last recursive call
1750 needed at this level, change it into iteration.
1751 This function is called enough to be worth it. */
1752 if (i == 0)
1754 x = XEXP (x, i);
1755 goto repeat;
1758 hash += hash_expr_1 (XEXP (x, i), 0, do_not_record_p);
1759 if (*do_not_record_p)
1760 return 0;
1763 else if (fmt[i] == 'E')
1764 for (j = 0; j < XVECLEN (x, i); j++)
1766 hash += hash_expr_1 (XVECEXP (x, i, j), 0, do_not_record_p);
1767 if (*do_not_record_p)
1768 return 0;
1771 else if (fmt[i] == 's')
1772 hash += hash_string_1 (XSTR (x, i));
1773 else if (fmt[i] == 'i')
1774 hash += (unsigned int) XINT (x, i);
1775 else
1776 abort ();
1779 return hash;
1782 /* Hash a set of register REGNO.
1784 Sets are hashed on the register that is set. This simplifies the PRE copy
1785 propagation code.
1787 ??? May need to make things more elaborate. Later, as necessary. */
1789 static unsigned int
1790 hash_set (regno, hash_table_size)
1791 int regno;
1792 int hash_table_size;
1794 unsigned int hash;
1796 hash = regno;
1797 return hash % hash_table_size;
1800 /* Return nonzero if exp1 is equivalent to exp2.
1801 ??? Borrowed from cse.c. Might want to remerge with cse.c. Later. */
1803 static int
1804 expr_equiv_p (x, y)
1805 rtx x, y;
1807 int i, j;
1808 enum rtx_code code;
1809 const char *fmt;
1811 if (x == y)
1812 return 1;
1814 if (x == 0 || y == 0)
1815 return x == y;
1817 code = GET_CODE (x);
1818 if (code != GET_CODE (y))
1819 return 0;
1821 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent. */
1822 if (GET_MODE (x) != GET_MODE (y))
1823 return 0;
1825 switch (code)
1827 case PC:
1828 case CC0:
1829 return x == y;
1831 case CONST_INT:
1832 return INTVAL (x) == INTVAL (y);
1834 case LABEL_REF:
1835 return XEXP (x, 0) == XEXP (y, 0);
1837 case SYMBOL_REF:
1838 return XSTR (x, 0) == XSTR (y, 0);
1840 case REG:
1841 return REGNO (x) == REGNO (y);
1843 case MEM:
1844 /* Can't merge two expressions in different alias sets, since we can
1845 decide that the expression is transparent in a block when it isn't,
1846 due to it being set with the different alias set. */
1847 if (MEM_ALIAS_SET (x) != MEM_ALIAS_SET (y))
1848 return 0;
1849 break;
1851 /* For commutative operations, check both orders. */
1852 case PLUS:
1853 case MULT:
1854 case AND:
1855 case IOR:
1856 case XOR:
1857 case NE:
1858 case EQ:
1859 return ((expr_equiv_p (XEXP (x, 0), XEXP (y, 0))
1860 && expr_equiv_p (XEXP (x, 1), XEXP (y, 1)))
1861 || (expr_equiv_p (XEXP (x, 0), XEXP (y, 1))
1862 && expr_equiv_p (XEXP (x, 1), XEXP (y, 0))));
1864 case ASM_OPERANDS:
1865 /* We don't use the generic code below because we want to
1866 disregard filename and line numbers. */
1868 /* A volatile asm isn't equivalent to any other. */
1869 if (MEM_VOLATILE_P (x) || MEM_VOLATILE_P (y))
1870 return 0;
1872 if (GET_MODE (x) != GET_MODE (y)
1873 || strcmp (ASM_OPERANDS_TEMPLATE (x), ASM_OPERANDS_TEMPLATE (y))
1874 || strcmp (ASM_OPERANDS_OUTPUT_CONSTRAINT (x),
1875 ASM_OPERANDS_OUTPUT_CONSTRAINT (y))
1876 || ASM_OPERANDS_OUTPUT_IDX (x) != ASM_OPERANDS_OUTPUT_IDX (y)
1877 || ASM_OPERANDS_INPUT_LENGTH (x) != ASM_OPERANDS_INPUT_LENGTH (y))
1878 return 0;
1880 if (ASM_OPERANDS_INPUT_LENGTH (x))
1882 for (i = ASM_OPERANDS_INPUT_LENGTH (x) - 1; i >= 0; i--)
1883 if (! expr_equiv_p (ASM_OPERANDS_INPUT (x, i),
1884 ASM_OPERANDS_INPUT (y, i))
1885 || strcmp (ASM_OPERANDS_INPUT_CONSTRAINT (x, i),
1886 ASM_OPERANDS_INPUT_CONSTRAINT (y, i)))
1887 return 0;
1890 return 1;
1892 default:
1893 break;
1896 /* Compare the elements. If any pair of corresponding elements
1897 fail to match, return 0 for the whole thing. */
1899 fmt = GET_RTX_FORMAT (code);
1900 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1902 switch (fmt[i])
1904 case 'e':
1905 if (! expr_equiv_p (XEXP (x, i), XEXP (y, i)))
1906 return 0;
1907 break;
1909 case 'E':
1910 if (XVECLEN (x, i) != XVECLEN (y, i))
1911 return 0;
1912 for (j = 0; j < XVECLEN (x, i); j++)
1913 if (! expr_equiv_p (XVECEXP (x, i, j), XVECEXP (y, i, j)))
1914 return 0;
1915 break;
1917 case 's':
1918 if (strcmp (XSTR (x, i), XSTR (y, i)))
1919 return 0;
1920 break;
1922 case 'i':
1923 if (XINT (x, i) != XINT (y, i))
1924 return 0;
1925 break;
1927 case 'w':
1928 if (XWINT (x, i) != XWINT (y, i))
1929 return 0;
1930 break;
1932 case '0':
1933 break;
1935 default:
1936 abort ();
1940 return 1;
1943 /* Insert expression X in INSN in the hash TABLE.
1944 If it is already present, record it as the last occurrence in INSN's
1945 basic block.
1947 MODE is the mode of the value X is being stored into.
1948 It is only used if X is a CONST_INT.
1950 ANTIC_P is nonzero if X is an anticipatable expression.
1951 AVAIL_P is nonzero if X is an available expression. */
1953 static void
1954 insert_expr_in_table (x, mode, insn, antic_p, avail_p, table)
1955 rtx x;
1956 enum machine_mode mode;
1957 rtx insn;
1958 int antic_p, avail_p;
1959 struct hash_table *table;
1961 int found, do_not_record_p;
1962 unsigned int hash;
1963 struct expr *cur_expr, *last_expr = NULL;
1964 struct occr *antic_occr, *avail_occr;
1965 struct occr *last_occr = NULL;
1967 hash = hash_expr (x, mode, &do_not_record_p, table->size);
1969 /* Do not insert expression in table if it contains volatile operands,
1970 or if hash_expr determines the expression is something we don't want
1971 to or can't handle. */
1972 if (do_not_record_p)
1973 return;
1975 cur_expr = table->table[hash];
1976 found = 0;
1978 while (cur_expr && 0 == (found = expr_equiv_p (cur_expr->expr, x)))
1980 /* If the expression isn't found, save a pointer to the end of
1981 the list. */
1982 last_expr = cur_expr;
1983 cur_expr = cur_expr->next_same_hash;
1986 if (! found)
1988 cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
1989 bytes_used += sizeof (struct expr);
1990 if (table->table[hash] == NULL)
1991 /* This is the first pattern that hashed to this index. */
1992 table->table[hash] = cur_expr;
1993 else
1994 /* Add EXPR to end of this hash chain. */
1995 last_expr->next_same_hash = cur_expr;
1997 /* Set the fields of the expr element. */
1998 cur_expr->expr = x;
1999 cur_expr->bitmap_index = table->n_elems++;
2000 cur_expr->next_same_hash = NULL;
2001 cur_expr->antic_occr = NULL;
2002 cur_expr->avail_occr = NULL;
2005 /* Now record the occurrence(s). */
2006 if (antic_p)
2008 antic_occr = cur_expr->antic_occr;
2010 /* Search for another occurrence in the same basic block. */
2011 while (antic_occr && BLOCK_NUM (antic_occr->insn) != BLOCK_NUM (insn))
2013 /* If an occurrence isn't found, save a pointer to the end of
2014 the list. */
2015 last_occr = antic_occr;
2016 antic_occr = antic_occr->next;
2019 if (antic_occr)
2020 /* Found another instance of the expression in the same basic block.
2021 Prefer the currently recorded one. We want the first one in the
2022 block and the block is scanned from start to end. */
2023 ; /* nothing to do */
2024 else
2026 /* First occurrence of this expression in this basic block. */
2027 antic_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
2028 bytes_used += sizeof (struct occr);
2029 /* First occurrence of this expression in any block? */
2030 if (cur_expr->antic_occr == NULL)
2031 cur_expr->antic_occr = antic_occr;
2032 else
2033 last_occr->next = antic_occr;
2035 antic_occr->insn = insn;
2036 antic_occr->next = NULL;
2040 if (avail_p)
2042 avail_occr = cur_expr->avail_occr;
2044 /* Search for another occurrence in the same basic block. */
2045 while (avail_occr && BLOCK_NUM (avail_occr->insn) != BLOCK_NUM (insn))
2047 /* If an occurrence isn't found, save a pointer to the end of
2048 the list. */
2049 last_occr = avail_occr;
2050 avail_occr = avail_occr->next;
2053 if (avail_occr)
2054 /* Found another instance of the expression in the same basic block.
2055 Prefer this occurrence to the currently recorded one. We want
2056 the last one in the block and the block is scanned from start
2057 to end. */
2058 avail_occr->insn = insn;
2059 else
2061 /* First occurrence of this expression in this basic block. */
2062 avail_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
2063 bytes_used += sizeof (struct occr);
2065 /* First occurrence of this expression in any block? */
2066 if (cur_expr->avail_occr == NULL)
2067 cur_expr->avail_occr = avail_occr;
2068 else
2069 last_occr->next = avail_occr;
2071 avail_occr->insn = insn;
2072 avail_occr->next = NULL;
2077 /* Insert pattern X in INSN in the hash table.
2078 X is a SET of a reg to either another reg or a constant.
2079 If it is already present, record it as the last occurrence in INSN's
2080 basic block. */
2082 static void
2083 insert_set_in_table (x, insn, table)
2084 rtx x;
2085 rtx insn;
2086 struct hash_table *table;
2088 int found;
2089 unsigned int hash;
2090 struct expr *cur_expr, *last_expr = NULL;
2091 struct occr *cur_occr, *last_occr = NULL;
2093 if (GET_CODE (x) != SET
2094 || GET_CODE (SET_DEST (x)) != REG)
2095 abort ();
2097 hash = hash_set (REGNO (SET_DEST (x)), table->size);
2099 cur_expr = table->table[hash];
2100 found = 0;
2102 while (cur_expr && 0 == (found = expr_equiv_p (cur_expr->expr, x)))
2104 /* If the expression isn't found, save a pointer to the end of
2105 the list. */
2106 last_expr = cur_expr;
2107 cur_expr = cur_expr->next_same_hash;
2110 if (! found)
2112 cur_expr = (struct expr *) gcse_alloc (sizeof (struct expr));
2113 bytes_used += sizeof (struct expr);
2114 if (table->table[hash] == NULL)
2115 /* This is the first pattern that hashed to this index. */
2116 table->table[hash] = cur_expr;
2117 else
2118 /* Add EXPR to end of this hash chain. */
2119 last_expr->next_same_hash = cur_expr;
2121 /* Set the fields of the expr element.
2122 We must copy X because it can be modified when copy propagation is
2123 performed on its operands. */
2124 cur_expr->expr = copy_rtx (x);
2125 cur_expr->bitmap_index = table->n_elems++;
2126 cur_expr->next_same_hash = NULL;
2127 cur_expr->antic_occr = NULL;
2128 cur_expr->avail_occr = NULL;
2131 /* Now record the occurrence. */
2132 cur_occr = cur_expr->avail_occr;
2134 /* Search for another occurrence in the same basic block. */
2135 while (cur_occr && BLOCK_NUM (cur_occr->insn) != BLOCK_NUM (insn))
2137 /* If an occurrence isn't found, save a pointer to the end of
2138 the list. */
2139 last_occr = cur_occr;
2140 cur_occr = cur_occr->next;
2143 if (cur_occr)
2144 /* Found another instance of the expression in the same basic block.
2145 Prefer this occurrence to the currently recorded one. We want the
2146 last one in the block and the block is scanned from start to end. */
2147 cur_occr->insn = insn;
2148 else
2150 /* First occurrence of this expression in this basic block. */
2151 cur_occr = (struct occr *) gcse_alloc (sizeof (struct occr));
2152 bytes_used += sizeof (struct occr);
2154 /* First occurrence of this expression in any block? */
2155 if (cur_expr->avail_occr == NULL)
2156 cur_expr->avail_occr = cur_occr;
2157 else
2158 last_occr->next = cur_occr;
2160 cur_occr->insn = insn;
2161 cur_occr->next = NULL;
2165 /* Determine whether the rtx X should be treated as a constant for
2166 the purposes of GCSE's constant propagation. */
2168 static bool
2169 gcse_constant_p (x)
2170 rtx x;
2172 /* Consider a COMPARE of two integers constant. */
2173 if (GET_CODE (x) == COMPARE
2174 && GET_CODE (XEXP (x, 0)) == CONST_INT
2175 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2176 return true;
2178 if (GET_CODE (x) == CONSTANT_P_RTX)
2179 return false;
2181 return CONSTANT_P (x);
2184 /* Scan pattern PAT of INSN and add an entry to the hash TABLE (set or
2185 expression one). */
2187 static void
2188 hash_scan_set (pat, insn, table)
2189 rtx pat, insn;
2190 struct hash_table *table;
2192 rtx src = SET_SRC (pat);
2193 rtx dest = SET_DEST (pat);
2194 rtx note;
2196 if (GET_CODE (src) == CALL)
2197 hash_scan_call (src, insn, table);
2199 else if (GET_CODE (dest) == REG)
2201 unsigned int regno = REGNO (dest);
2202 rtx tmp;
2204 /* If this is a single set and we are doing constant propagation,
2205 see if a REG_NOTE shows this equivalent to a constant. */
2206 if (table->set_p && (note = find_reg_equal_equiv_note (insn)) != 0
2207 && gcse_constant_p (XEXP (note, 0)))
2208 src = XEXP (note, 0), pat = gen_rtx_SET (VOIDmode, dest, src);
2210 /* Only record sets of pseudo-regs in the hash table. */
2211 if (! table->set_p
2212 && regno >= FIRST_PSEUDO_REGISTER
2213 /* Don't GCSE something if we can't do a reg/reg copy. */
2214 && can_copy_p [GET_MODE (dest)]
2215 /* GCSE commonly inserts instruction after the insn. We can't
2216 do that easily for EH_REGION notes so disable GCSE on these
2217 for now. */
2218 && !find_reg_note (insn, REG_EH_REGION, NULL_RTX)
2219 /* Is SET_SRC something we want to gcse? */
2220 && want_to_gcse_p (src)
2221 /* Don't CSE a nop. */
2222 && ! set_noop_p (pat)
2223 /* Don't GCSE if it has attached REG_EQUIV note.
2224 At this point this only function parameters should have
2225 REG_EQUIV notes and if the argument slot is used somewhere
2226 explicitly, it means address of parameter has been taken,
2227 so we should not extend the lifetime of the pseudo. */
2228 && ((note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) == 0
2229 || GET_CODE (XEXP (note, 0)) != MEM))
2231 /* An expression is not anticipatable if its operands are
2232 modified before this insn or if this is not the only SET in
2233 this insn. */
2234 int antic_p = oprs_anticipatable_p (src, insn) && single_set (insn);
2235 /* An expression is not available if its operands are
2236 subsequently modified, including this insn. It's also not
2237 available if this is a branch, because we can't insert
2238 a set after the branch. */
2239 int avail_p = (oprs_available_p (src, insn)
2240 && ! JUMP_P (insn));
2242 insert_expr_in_table (src, GET_MODE (dest), insn, antic_p, avail_p, table);
2245 /* Record sets for constant/copy propagation. */
2246 else if (table->set_p
2247 && regno >= FIRST_PSEUDO_REGISTER
2248 && ((GET_CODE (src) == REG
2249 && REGNO (src) >= FIRST_PSEUDO_REGISTER
2250 && can_copy_p [GET_MODE (dest)]
2251 && REGNO (src) != regno)
2252 || gcse_constant_p (src))
2253 /* A copy is not available if its src or dest is subsequently
2254 modified. Here we want to search from INSN+1 on, but
2255 oprs_available_p searches from INSN on. */
2256 && (insn == BLOCK_END (BLOCK_NUM (insn))
2257 || ((tmp = next_nonnote_insn (insn)) != NULL_RTX
2258 && oprs_available_p (pat, tmp))))
2259 insert_set_in_table (pat, insn, table);
2263 static void
2264 hash_scan_clobber (x, insn, table)
2265 rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
2266 struct hash_table *table ATTRIBUTE_UNUSED;
2268 /* Currently nothing to do. */
2271 static void
2272 hash_scan_call (x, insn, table)
2273 rtx x ATTRIBUTE_UNUSED, insn ATTRIBUTE_UNUSED;
2274 struct hash_table *table ATTRIBUTE_UNUSED;
2276 /* Currently nothing to do. */
2279 /* Process INSN and add hash table entries as appropriate.
2281 Only available expressions that set a single pseudo-reg are recorded.
2283 Single sets in a PARALLEL could be handled, but it's an extra complication
2284 that isn't dealt with right now. The trick is handling the CLOBBERs that
2285 are also in the PARALLEL. Later.
2287 If SET_P is nonzero, this is for the assignment hash table,
2288 otherwise it is for the expression hash table.
2289 If IN_LIBCALL_BLOCK nonzero, we are in a libcall block, and should
2290 not record any expressions. */
2292 static void
2293 hash_scan_insn (insn, table, in_libcall_block)
2294 rtx insn;
2295 struct hash_table *table;
2296 int in_libcall_block;
2298 rtx pat = PATTERN (insn);
2299 int i;
2301 if (in_libcall_block)
2302 return;
2304 /* Pick out the sets of INSN and for other forms of instructions record
2305 what's been modified. */
2307 if (GET_CODE (pat) == SET)
2308 hash_scan_set (pat, insn, table);
2309 else if (GET_CODE (pat) == PARALLEL)
2310 for (i = 0; i < XVECLEN (pat, 0); i++)
2312 rtx x = XVECEXP (pat, 0, i);
2314 if (GET_CODE (x) == SET)
2315 hash_scan_set (x, insn, table);
2316 else if (GET_CODE (x) == CLOBBER)
2317 hash_scan_clobber (x, insn, table);
2318 else if (GET_CODE (x) == CALL)
2319 hash_scan_call (x, insn, table);
2322 else if (GET_CODE (pat) == CLOBBER)
2323 hash_scan_clobber (pat, insn, table);
2324 else if (GET_CODE (pat) == CALL)
2325 hash_scan_call (pat, insn, table);
2328 static void
2329 dump_hash_table (file, name, table)
2330 FILE *file;
2331 const char *name;
2332 struct hash_table *table;
2334 int i;
2335 /* Flattened out table, so it's printed in proper order. */
2336 struct expr **flat_table;
2337 unsigned int *hash_val;
2338 struct expr *expr;
2340 flat_table
2341 = (struct expr **) xcalloc (table->n_elems, sizeof (struct expr *));
2342 hash_val = (unsigned int *) xmalloc (table->n_elems * sizeof (unsigned int));
2344 for (i = 0; i < (int) table->size; i++)
2345 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
2347 flat_table[expr->bitmap_index] = expr;
2348 hash_val[expr->bitmap_index] = i;
2351 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
2352 name, table->size, table->n_elems);
2354 for (i = 0; i < (int) table->n_elems; i++)
2355 if (flat_table[i] != 0)
2357 expr = flat_table[i];
2358 fprintf (file, "Index %d (hash value %d)\n ",
2359 expr->bitmap_index, hash_val[i]);
2360 print_rtl (file, expr->expr);
2361 fprintf (file, "\n");
2364 fprintf (file, "\n");
2366 free (flat_table);
2367 free (hash_val);
2370 /* Record register first/last/block set information for REGNO in INSN.
2372 first_set records the first place in the block where the register
2373 is set and is used to compute "anticipatability".
2375 last_set records the last place in the block where the register
2376 is set and is used to compute "availability".
2378 last_bb records the block for which first_set and last_set are
2379 valid, as a quick test to invalidate them.
2381 reg_set_in_block records whether the register is set in the block
2382 and is used to compute "transparency". */
2384 static void
2385 record_last_reg_set_info (insn, regno)
2386 rtx insn;
2387 int regno;
2389 struct reg_avail_info *info = &reg_avail_info[regno];
2390 int cuid = INSN_CUID (insn);
2392 info->last_set = cuid;
2393 if (info->last_bb != current_bb)
2395 info->last_bb = current_bb;
2396 info->first_set = cuid;
2397 SET_BIT (reg_set_in_block[current_bb->index], regno);
2402 /* Record all of the canonicalized MEMs of record_last_mem_set_info's insn.
2403 Note we store a pair of elements in the list, so they have to be
2404 taken off pairwise. */
2406 static void
2407 canon_list_insert (dest, unused1, v_insn)
2408 rtx dest ATTRIBUTE_UNUSED;
2409 rtx unused1 ATTRIBUTE_UNUSED;
2410 void * v_insn;
2412 rtx dest_addr, insn;
2413 int bb;
2415 while (GET_CODE (dest) == SUBREG
2416 || GET_CODE (dest) == ZERO_EXTRACT
2417 || GET_CODE (dest) == SIGN_EXTRACT
2418 || GET_CODE (dest) == STRICT_LOW_PART)
2419 dest = XEXP (dest, 0);
2421 /* If DEST is not a MEM, then it will not conflict with a load. Note
2422 that function calls are assumed to clobber memory, but are handled
2423 elsewhere. */
2425 if (GET_CODE (dest) != MEM)
2426 return;
2428 dest_addr = get_addr (XEXP (dest, 0));
2429 dest_addr = canon_rtx (dest_addr);
2430 insn = (rtx) v_insn;
2431 bb = BLOCK_NUM (insn);
2433 canon_modify_mem_list[bb] =
2434 alloc_EXPR_LIST (VOIDmode, dest_addr, canon_modify_mem_list[bb]);
2435 canon_modify_mem_list[bb] =
2436 alloc_EXPR_LIST (VOIDmode, dest, canon_modify_mem_list[bb]);
2437 bitmap_set_bit (canon_modify_mem_list_set, bb);
2440 /* Record memory modification information for INSN. We do not actually care
2441 about the memory location(s) that are set, or even how they are set (consider
2442 a CALL_INSN). We merely need to record which insns modify memory. */
2444 static void
2445 record_last_mem_set_info (insn)
2446 rtx insn;
2448 int bb = BLOCK_NUM (insn);
2450 /* load_killed_in_block_p will handle the case of calls clobbering
2451 everything. */
2452 modify_mem_list[bb] = alloc_INSN_LIST (insn, modify_mem_list[bb]);
2453 bitmap_set_bit (modify_mem_list_set, bb);
2455 if (GET_CODE (insn) == CALL_INSN)
2457 /* Note that traversals of this loop (other than for free-ing)
2458 will break after encountering a CALL_INSN. So, there's no
2459 need to insert a pair of items, as canon_list_insert does. */
2460 canon_modify_mem_list[bb] =
2461 alloc_INSN_LIST (insn, canon_modify_mem_list[bb]);
2462 bitmap_set_bit (canon_modify_mem_list_set, bb);
2464 else
2465 note_stores (PATTERN (insn), canon_list_insert, (void*) insn);
2468 /* Called from compute_hash_table via note_stores to handle one
2469 SET or CLOBBER in an insn. DATA is really the instruction in which
2470 the SET is taking place. */
2472 static void
2473 record_last_set_info (dest, setter, data)
2474 rtx dest, setter ATTRIBUTE_UNUSED;
2475 void *data;
2477 rtx last_set_insn = (rtx) data;
2479 if (GET_CODE (dest) == SUBREG)
2480 dest = SUBREG_REG (dest);
2482 if (GET_CODE (dest) == REG)
2483 record_last_reg_set_info (last_set_insn, REGNO (dest));
2484 else if (GET_CODE (dest) == MEM
2485 /* Ignore pushes, they clobber nothing. */
2486 && ! push_operand (dest, GET_MODE (dest)))
2487 record_last_mem_set_info (last_set_insn);
2490 /* Top level function to create an expression or assignment hash table.
2492 Expression entries are placed in the hash table if
2493 - they are of the form (set (pseudo-reg) src),
2494 - src is something we want to perform GCSE on,
2495 - none of the operands are subsequently modified in the block
2497 Assignment entries are placed in the hash table if
2498 - they are of the form (set (pseudo-reg) src),
2499 - src is something we want to perform const/copy propagation on,
2500 - none of the operands or target are subsequently modified in the block
2502 Currently src must be a pseudo-reg or a const_int.
2504 TABLE is the table computed. */
2506 static void
2507 compute_hash_table_work (table)
2508 struct hash_table *table;
2510 unsigned int i;
2512 /* While we compute the hash table we also compute a bit array of which
2513 registers are set in which blocks.
2514 ??? This isn't needed during const/copy propagation, but it's cheap to
2515 compute. Later. */
2516 sbitmap_vector_zero (reg_set_in_block, last_basic_block);
2518 /* re-Cache any INSN_LIST nodes we have allocated. */
2519 clear_modify_mem_tables ();
2520 /* Some working arrays used to track first and last set in each block. */
2521 reg_avail_info = (struct reg_avail_info*)
2522 gmalloc (max_gcse_regno * sizeof (struct reg_avail_info));
2524 for (i = 0; i < max_gcse_regno; ++i)
2525 reg_avail_info[i].last_bb = NULL;
2527 FOR_EACH_BB (current_bb)
2529 rtx insn;
2530 unsigned int regno;
2531 int in_libcall_block;
2533 /* First pass over the instructions records information used to
2534 determine when registers and memory are first and last set.
2535 ??? hard-reg reg_set_in_block computation
2536 could be moved to compute_sets since they currently don't change. */
2538 for (insn = current_bb->head;
2539 insn && insn != NEXT_INSN (current_bb->end);
2540 insn = NEXT_INSN (insn))
2542 if (! INSN_P (insn))
2543 continue;
2545 if (GET_CODE (insn) == CALL_INSN)
2547 bool clobbers_all = false;
2548 #ifdef NON_SAVING_SETJMP
2549 if (NON_SAVING_SETJMP
2550 && find_reg_note (insn, REG_SETJMP, NULL_RTX))
2551 clobbers_all = true;
2552 #endif
2554 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2555 if (clobbers_all
2556 || TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
2557 record_last_reg_set_info (insn, regno);
2559 mark_call (insn);
2562 note_stores (PATTERN (insn), record_last_set_info, insn);
2565 /* Insert implicit sets in the hash table. */
2566 if (table->set_p
2567 && implicit_sets[current_bb->index] != NULL_RTX)
2568 hash_scan_set (implicit_sets[current_bb->index],
2569 current_bb->head, table);
2571 /* The next pass builds the hash table. */
2573 for (insn = current_bb->head, in_libcall_block = 0;
2574 insn && insn != NEXT_INSN (current_bb->end);
2575 insn = NEXT_INSN (insn))
2576 if (INSN_P (insn))
2578 if (find_reg_note (insn, REG_LIBCALL, NULL_RTX))
2579 in_libcall_block = 1;
2580 else if (table->set_p && find_reg_note (insn, REG_RETVAL, NULL_RTX))
2581 in_libcall_block = 0;
2582 hash_scan_insn (insn, table, in_libcall_block);
2583 if (!table->set_p && find_reg_note (insn, REG_RETVAL, NULL_RTX))
2584 in_libcall_block = 0;
2588 free (reg_avail_info);
2589 reg_avail_info = NULL;
2592 /* Allocate space for the set/expr hash TABLE.
2593 N_INSNS is the number of instructions in the function.
2594 It is used to determine the number of buckets to use.
2595 SET_P determines whether set or expression table will
2596 be created. */
2598 static void
2599 alloc_hash_table (n_insns, table, set_p)
2600 int n_insns;
2601 struct hash_table *table;
2602 int set_p;
2604 int n;
2606 table->size = n_insns / 4;
2607 if (table->size < 11)
2608 table->size = 11;
2610 /* Attempt to maintain efficient use of hash table.
2611 Making it an odd number is simplest for now.
2612 ??? Later take some measurements. */
2613 table->size |= 1;
2614 n = table->size * sizeof (struct expr *);
2615 table->table = (struct expr **) gmalloc (n);
2616 table->set_p = set_p;
2619 /* Free things allocated by alloc_hash_table. */
2621 static void
2622 free_hash_table (table)
2623 struct hash_table *table;
2625 free (table->table);
2628 /* Compute the hash TABLE for doing copy/const propagation or
2629 expression hash table. */
2631 static void
2632 compute_hash_table (table)
2633 struct hash_table *table;
2635 /* Initialize count of number of entries in hash table. */
2636 table->n_elems = 0;
2637 memset ((char *) table->table, 0,
2638 table->size * sizeof (struct expr *));
2640 compute_hash_table_work (table);
2643 /* Expression tracking support. */
2645 /* Lookup pattern PAT in the expression TABLE.
2646 The result is a pointer to the table entry, or NULL if not found. */
2648 static struct expr *
2649 lookup_expr (pat, table)
2650 rtx pat;
2651 struct hash_table *table;
2653 int do_not_record_p;
2654 unsigned int hash = hash_expr (pat, GET_MODE (pat), &do_not_record_p,
2655 table->size);
2656 struct expr *expr;
2658 if (do_not_record_p)
2659 return NULL;
2661 expr = table->table[hash];
2663 while (expr && ! expr_equiv_p (expr->expr, pat))
2664 expr = expr->next_same_hash;
2666 return expr;
2669 /* Lookup REGNO in the set TABLE. The result is a pointer to the
2670 table entry, or NULL if not found. */
2672 static struct expr *
2673 lookup_set (regno, table)
2674 unsigned int regno;
2675 struct hash_table *table;
2677 unsigned int hash = hash_set (regno, table->size);
2678 struct expr *expr;
2680 expr = table->table[hash];
2682 while (expr && REGNO (SET_DEST (expr->expr)) != regno)
2683 expr = expr->next_same_hash;
2685 return expr;
2688 /* Return the next entry for REGNO in list EXPR. */
2690 static struct expr *
2691 next_set (regno, expr)
2692 unsigned int regno;
2693 struct expr *expr;
2696 expr = expr->next_same_hash;
2697 while (expr && REGNO (SET_DEST (expr->expr)) != regno);
2699 return expr;
2702 /* Like free_INSN_LIST_list or free_EXPR_LIST_list, except that the node
2703 types may be mixed. */
2705 static void
2706 free_insn_expr_list_list (listp)
2707 rtx *listp;
2709 rtx list, next;
2711 for (list = *listp; list ; list = next)
2713 next = XEXP (list, 1);
2714 if (GET_CODE (list) == EXPR_LIST)
2715 free_EXPR_LIST_node (list);
2716 else
2717 free_INSN_LIST_node (list);
2720 *listp = NULL;
2723 /* Clear canon_modify_mem_list and modify_mem_list tables. */
2724 static void
2725 clear_modify_mem_tables ()
2727 int i;
2729 EXECUTE_IF_SET_IN_BITMAP
2730 (modify_mem_list_set, 0, i, free_INSN_LIST_list (modify_mem_list + i));
2731 bitmap_clear (modify_mem_list_set);
2733 EXECUTE_IF_SET_IN_BITMAP
2734 (canon_modify_mem_list_set, 0, i,
2735 free_insn_expr_list_list (canon_modify_mem_list + i));
2736 bitmap_clear (canon_modify_mem_list_set);
2739 /* Release memory used by modify_mem_list_set and canon_modify_mem_list_set. */
2741 static void
2742 free_modify_mem_tables ()
2744 clear_modify_mem_tables ();
2745 free (modify_mem_list);
2746 free (canon_modify_mem_list);
2747 modify_mem_list = 0;
2748 canon_modify_mem_list = 0;
2751 /* Reset tables used to keep track of what's still available [since the
2752 start of the block]. */
2754 static void
2755 reset_opr_set_tables ()
2757 /* Maintain a bitmap of which regs have been set since beginning of
2758 the block. */
2759 CLEAR_REG_SET (reg_set_bitmap);
2761 /* Also keep a record of the last instruction to modify memory.
2762 For now this is very trivial, we only record whether any memory
2763 location has been modified. */
2764 clear_modify_mem_tables ();
2767 /* Return nonzero if the operands of X are not set before INSN in
2768 INSN's basic block. */
2770 static int
2771 oprs_not_set_p (x, insn)
2772 rtx x, insn;
2774 int i, j;
2775 enum rtx_code code;
2776 const char *fmt;
2778 if (x == 0)
2779 return 1;
2781 code = GET_CODE (x);
2782 switch (code)
2784 case PC:
2785 case CC0:
2786 case CONST:
2787 case CONST_INT:
2788 case CONST_DOUBLE:
2789 case CONST_VECTOR:
2790 case SYMBOL_REF:
2791 case LABEL_REF:
2792 case ADDR_VEC:
2793 case ADDR_DIFF_VEC:
2794 return 1;
2796 case MEM:
2797 if (load_killed_in_block_p (BLOCK_FOR_INSN (insn),
2798 INSN_CUID (insn), x, 0))
2799 return 0;
2800 else
2801 return oprs_not_set_p (XEXP (x, 0), insn);
2803 case REG:
2804 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
2806 default:
2807 break;
2810 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
2812 if (fmt[i] == 'e')
2814 /* If we are about to do the last recursive call
2815 needed at this level, change it into iteration.
2816 This function is called enough to be worth it. */
2817 if (i == 0)
2818 return oprs_not_set_p (XEXP (x, i), insn);
2820 if (! oprs_not_set_p (XEXP (x, i), insn))
2821 return 0;
2823 else if (fmt[i] == 'E')
2824 for (j = 0; j < XVECLEN (x, i); j++)
2825 if (! oprs_not_set_p (XVECEXP (x, i, j), insn))
2826 return 0;
2829 return 1;
2832 /* Mark things set by a CALL. */
2834 static void
2835 mark_call (insn)
2836 rtx insn;
2838 if (! CONST_OR_PURE_CALL_P (insn))
2839 record_last_mem_set_info (insn);
2842 /* Mark things set by a SET. */
2844 static void
2845 mark_set (pat, insn)
2846 rtx pat, insn;
2848 rtx dest = SET_DEST (pat);
2850 while (GET_CODE (dest) == SUBREG
2851 || GET_CODE (dest) == ZERO_EXTRACT
2852 || GET_CODE (dest) == SIGN_EXTRACT
2853 || GET_CODE (dest) == STRICT_LOW_PART)
2854 dest = XEXP (dest, 0);
2856 if (GET_CODE (dest) == REG)
2857 SET_REGNO_REG_SET (reg_set_bitmap, REGNO (dest));
2858 else if (GET_CODE (dest) == MEM)
2859 record_last_mem_set_info (insn);
2861 if (GET_CODE (SET_SRC (pat)) == CALL)
2862 mark_call (insn);
2865 /* Record things set by a CLOBBER. */
2867 static void
2868 mark_clobber (pat, insn)
2869 rtx pat, insn;
2871 rtx clob = XEXP (pat, 0);
2873 while (GET_CODE (clob) == SUBREG || GET_CODE (clob) == STRICT_LOW_PART)
2874 clob = XEXP (clob, 0);
2876 if (GET_CODE (clob) == REG)
2877 SET_REGNO_REG_SET (reg_set_bitmap, REGNO (clob));
2878 else
2879 record_last_mem_set_info (insn);
2882 /* Record things set by INSN.
2883 This data is used by oprs_not_set_p. */
2885 static void
2886 mark_oprs_set (insn)
2887 rtx insn;
2889 rtx pat = PATTERN (insn);
2890 int i;
2892 if (GET_CODE (pat) == SET)
2893 mark_set (pat, insn);
2894 else if (GET_CODE (pat) == PARALLEL)
2895 for (i = 0; i < XVECLEN (pat, 0); i++)
2897 rtx x = XVECEXP (pat, 0, i);
2899 if (GET_CODE (x) == SET)
2900 mark_set (x, insn);
2901 else if (GET_CODE (x) == CLOBBER)
2902 mark_clobber (x, insn);
2903 else if (GET_CODE (x) == CALL)
2904 mark_call (insn);
2907 else if (GET_CODE (pat) == CLOBBER)
2908 mark_clobber (pat, insn);
2909 else if (GET_CODE (pat) == CALL)
2910 mark_call (insn);
2914 /* Classic GCSE reaching definition support. */
2916 /* Allocate reaching def variables. */
2918 static void
2919 alloc_rd_mem (n_blocks, n_insns)
2920 int n_blocks, n_insns;
2922 rd_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2923 sbitmap_vector_zero (rd_kill, n_blocks);
2925 rd_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2926 sbitmap_vector_zero (rd_gen, n_blocks);
2928 reaching_defs = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2929 sbitmap_vector_zero (reaching_defs, n_blocks);
2931 rd_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_insns);
2932 sbitmap_vector_zero (rd_out, n_blocks);
2935 /* Free reaching def variables. */
2937 static void
2938 free_rd_mem ()
2940 sbitmap_vector_free (rd_kill);
2941 sbitmap_vector_free (rd_gen);
2942 sbitmap_vector_free (reaching_defs);
2943 sbitmap_vector_free (rd_out);
2946 /* Add INSN to the kills of BB. REGNO, set in BB, is killed by INSN. */
2948 static void
2949 handle_rd_kill_set (insn, regno, bb)
2950 rtx insn;
2951 int regno;
2952 basic_block bb;
2954 struct reg_set *this_reg;
2956 for (this_reg = reg_set_table[regno]; this_reg; this_reg = this_reg ->next)
2957 if (BLOCK_NUM (this_reg->insn) != BLOCK_NUM (insn))
2958 SET_BIT (rd_kill[bb->index], INSN_CUID (this_reg->insn));
2961 /* Compute the set of kill's for reaching definitions. */
2963 static void
2964 compute_kill_rd ()
2966 int cuid;
2967 unsigned int regno;
2968 int i;
2969 basic_block bb;
2971 /* For each block
2972 For each set bit in `gen' of the block (i.e each insn which
2973 generates a definition in the block)
2974 Call the reg set by the insn corresponding to that bit regx
2975 Look at the linked list starting at reg_set_table[regx]
2976 For each setting of regx in the linked list, which is not in
2977 this block
2978 Set the bit in `kill' corresponding to that insn. */
2979 FOR_EACH_BB (bb)
2980 for (cuid = 0; cuid < max_cuid; cuid++)
2981 if (TEST_BIT (rd_gen[bb->index], cuid))
2983 rtx insn = CUID_INSN (cuid);
2984 rtx pat = PATTERN (insn);
2986 if (GET_CODE (insn) == CALL_INSN)
2988 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
2989 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
2990 handle_rd_kill_set (insn, regno, bb);
2993 if (GET_CODE (pat) == PARALLEL)
2995 for (i = XVECLEN (pat, 0) - 1; i >= 0; i--)
2997 enum rtx_code code = GET_CODE (XVECEXP (pat, 0, i));
2999 if ((code == SET || code == CLOBBER)
3000 && GET_CODE (XEXP (XVECEXP (pat, 0, i), 0)) == REG)
3001 handle_rd_kill_set (insn,
3002 REGNO (XEXP (XVECEXP (pat, 0, i), 0)),
3003 bb);
3006 else if (GET_CODE (pat) == SET && GET_CODE (SET_DEST (pat)) == REG)
3007 /* Each setting of this register outside of this block
3008 must be marked in the set of kills in this block. */
3009 handle_rd_kill_set (insn, REGNO (SET_DEST (pat)), bb);
3013 /* Compute the reaching definitions as in
3014 Compilers Principles, Techniques, and Tools. Aho, Sethi, Ullman,
3015 Chapter 10. It is the same algorithm as used for computing available
3016 expressions but applied to the gens and kills of reaching definitions. */
3018 static void
3019 compute_rd ()
3021 int changed, passes;
3022 basic_block bb;
3024 FOR_EACH_BB (bb)
3025 sbitmap_copy (rd_out[bb->index] /*dst*/, rd_gen[bb->index] /*src*/);
3027 passes = 0;
3028 changed = 1;
3029 while (changed)
3031 changed = 0;
3032 FOR_EACH_BB (bb)
3034 sbitmap_union_of_preds (reaching_defs[bb->index], rd_out, bb->index);
3035 changed |= sbitmap_union_of_diff_cg (rd_out[bb->index], rd_gen[bb->index],
3036 reaching_defs[bb->index], rd_kill[bb->index]);
3038 passes++;
3041 if (gcse_file)
3042 fprintf (gcse_file, "reaching def computation: %d passes\n", passes);
3045 /* Classic GCSE available expression support. */
3047 /* Allocate memory for available expression computation. */
3049 static void
3050 alloc_avail_expr_mem (n_blocks, n_exprs)
3051 int n_blocks, n_exprs;
3053 ae_kill = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
3054 sbitmap_vector_zero (ae_kill, n_blocks);
3056 ae_gen = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
3057 sbitmap_vector_zero (ae_gen, n_blocks);
3059 ae_in = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
3060 sbitmap_vector_zero (ae_in, n_blocks);
3062 ae_out = (sbitmap *) sbitmap_vector_alloc (n_blocks, n_exprs);
3063 sbitmap_vector_zero (ae_out, n_blocks);
3066 static void
3067 free_avail_expr_mem ()
3069 sbitmap_vector_free (ae_kill);
3070 sbitmap_vector_free (ae_gen);
3071 sbitmap_vector_free (ae_in);
3072 sbitmap_vector_free (ae_out);
3075 /* Compute the set of available expressions generated in each basic block. */
3077 static void
3078 compute_ae_gen (expr_hash_table)
3079 struct hash_table *expr_hash_table;
3081 unsigned int i;
3082 struct expr *expr;
3083 struct occr *occr;
3085 /* For each recorded occurrence of each expression, set ae_gen[bb][expr].
3086 This is all we have to do because an expression is not recorded if it
3087 is not available, and the only expressions we want to work with are the
3088 ones that are recorded. */
3089 for (i = 0; i < expr_hash_table->size; i++)
3090 for (expr = expr_hash_table->table[i]; expr != 0; expr = expr->next_same_hash)
3091 for (occr = expr->avail_occr; occr != 0; occr = occr->next)
3092 SET_BIT (ae_gen[BLOCK_NUM (occr->insn)], expr->bitmap_index);
3095 /* Return nonzero if expression X is killed in BB. */
3097 static int
3098 expr_killed_p (x, bb)
3099 rtx x;
3100 basic_block bb;
3102 int i, j;
3103 enum rtx_code code;
3104 const char *fmt;
3106 if (x == 0)
3107 return 1;
3109 code = GET_CODE (x);
3110 switch (code)
3112 case REG:
3113 return TEST_BIT (reg_set_in_block[bb->index], REGNO (x));
3115 case MEM:
3116 if (load_killed_in_block_p (bb, get_max_uid () + 1, x, 0))
3117 return 1;
3118 else
3119 return expr_killed_p (XEXP (x, 0), bb);
3121 case PC:
3122 case CC0: /*FIXME*/
3123 case CONST:
3124 case CONST_INT:
3125 case CONST_DOUBLE:
3126 case CONST_VECTOR:
3127 case SYMBOL_REF:
3128 case LABEL_REF:
3129 case ADDR_VEC:
3130 case ADDR_DIFF_VEC:
3131 return 0;
3133 default:
3134 break;
3137 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
3139 if (fmt[i] == 'e')
3141 /* If we are about to do the last recursive call
3142 needed at this level, change it into iteration.
3143 This function is called enough to be worth it. */
3144 if (i == 0)
3145 return expr_killed_p (XEXP (x, i), bb);
3146 else if (expr_killed_p (XEXP (x, i), bb))
3147 return 1;
3149 else if (fmt[i] == 'E')
3150 for (j = 0; j < XVECLEN (x, i); j++)
3151 if (expr_killed_p (XVECEXP (x, i, j), bb))
3152 return 1;
3155 return 0;
3158 /* Compute the set of available expressions killed in each basic block. */
3160 static void
3161 compute_ae_kill (ae_gen, ae_kill, expr_hash_table)
3162 sbitmap *ae_gen, *ae_kill;
3163 struct hash_table *expr_hash_table;
3165 basic_block bb;
3166 unsigned int i;
3167 struct expr *expr;
3169 FOR_EACH_BB (bb)
3170 for (i = 0; i < expr_hash_table->size; i++)
3171 for (expr = expr_hash_table->table[i]; expr; expr = expr->next_same_hash)
3173 /* Skip EXPR if generated in this block. */
3174 if (TEST_BIT (ae_gen[bb->index], expr->bitmap_index))
3175 continue;
3177 if (expr_killed_p (expr->expr, bb))
3178 SET_BIT (ae_kill[bb->index], expr->bitmap_index);
3182 /* Actually perform the Classic GCSE optimizations. */
3184 /* Return nonzero if occurrence OCCR of expression EXPR reaches block BB.
3186 CHECK_SELF_LOOP is nonzero if we should consider a block reaching itself
3187 as a positive reach. We want to do this when there are two computations
3188 of the expression in the block.
3190 VISITED is a pointer to a working buffer for tracking which BB's have
3191 been visited. It is NULL for the top-level call.
3193 We treat reaching expressions that go through blocks containing the same
3194 reaching expression as "not reaching". E.g. if EXPR is generated in blocks
3195 2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
3196 2 as not reaching. The intent is to improve the probability of finding
3197 only one reaching expression and to reduce register lifetimes by picking
3198 the closest such expression. */
3200 static int
3201 expr_reaches_here_p_work (occr, expr, bb, check_self_loop, visited)
3202 struct occr *occr;
3203 struct expr *expr;
3204 basic_block bb;
3205 int check_self_loop;
3206 char *visited;
3208 edge pred;
3210 for (pred = bb->pred; pred != NULL; pred = pred->pred_next)
3212 basic_block pred_bb = pred->src;
3214 if (visited[pred_bb->index])
3215 /* This predecessor has already been visited. Nothing to do. */
3217 else if (pred_bb == bb)
3219 /* BB loops on itself. */
3220 if (check_self_loop
3221 && TEST_BIT (ae_gen[pred_bb->index], expr->bitmap_index)
3222 && BLOCK_NUM (occr->insn) == pred_bb->index)
3223 return 1;
3225 visited[pred_bb->index] = 1;
3228 /* Ignore this predecessor if it kills the expression. */
3229 else if (TEST_BIT (ae_kill[pred_bb->index], expr->bitmap_index))
3230 visited[pred_bb->index] = 1;
3232 /* Does this predecessor generate this expression? */
3233 else if (TEST_BIT (ae_gen[pred_bb->index], expr->bitmap_index))
3235 /* Is this the occurrence we're looking for?
3236 Note that there's only one generating occurrence per block
3237 so we just need to check the block number. */
3238 if (BLOCK_NUM (occr->insn) == pred_bb->index)
3239 return 1;
3241 visited[pred_bb->index] = 1;
3244 /* Neither gen nor kill. */
3245 else
3247 visited[pred_bb->index] = 1;
3248 if (expr_reaches_here_p_work (occr, expr, pred_bb, check_self_loop,
3249 visited))
3251 return 1;
3255 /* All paths have been checked. */
3256 return 0;
3259 /* This wrapper for expr_reaches_here_p_work() is to ensure that any
3260 memory allocated for that function is returned. */
3262 static int
3263 expr_reaches_here_p (occr, expr, bb, check_self_loop)
3264 struct occr *occr;
3265 struct expr *expr;
3266 basic_block bb;
3267 int check_self_loop;
3269 int rval;
3270 char *visited = (char *) xcalloc (last_basic_block, 1);
3272 rval = expr_reaches_here_p_work (occr, expr, bb, check_self_loop, visited);
3274 free (visited);
3275 return rval;
3278 /* Return the instruction that computes EXPR that reaches INSN's basic block.
3279 If there is more than one such instruction, return NULL.
3281 Called only by handle_avail_expr. */
3283 static rtx
3284 computing_insn (expr, insn)
3285 struct expr *expr;
3286 rtx insn;
3288 basic_block bb = BLOCK_FOR_INSN (insn);
3290 if (expr->avail_occr->next == NULL)
3292 if (BLOCK_FOR_INSN (expr->avail_occr->insn) == bb)
3293 /* The available expression is actually itself
3294 (i.e. a loop in the flow graph) so do nothing. */
3295 return NULL;
3297 /* (FIXME) Case that we found a pattern that was created by
3298 a substitution that took place. */
3299 return expr->avail_occr->insn;
3301 else
3303 /* Pattern is computed more than once.
3304 Search backwards from this insn to see how many of these
3305 computations actually reach this insn. */
3306 struct occr *occr;
3307 rtx insn_computes_expr = NULL;
3308 int can_reach = 0;
3310 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
3312 if (BLOCK_FOR_INSN (occr->insn) == bb)
3314 /* The expression is generated in this block.
3315 The only time we care about this is when the expression
3316 is generated later in the block [and thus there's a loop].
3317 We let the normal cse pass handle the other cases. */
3318 if (INSN_CUID (insn) < INSN_CUID (occr->insn)
3319 && expr_reaches_here_p (occr, expr, bb, 1))
3321 can_reach++;
3322 if (can_reach > 1)
3323 return NULL;
3325 insn_computes_expr = occr->insn;
3328 else if (expr_reaches_here_p (occr, expr, bb, 0))
3330 can_reach++;
3331 if (can_reach > 1)
3332 return NULL;
3334 insn_computes_expr = occr->insn;
3338 if (insn_computes_expr == NULL)
3339 abort ();
3341 return insn_computes_expr;
3345 /* Return nonzero if the definition in DEF_INSN can reach INSN.
3346 Only called by can_disregard_other_sets. */
3348 static int
3349 def_reaches_here_p (insn, def_insn)
3350 rtx insn, def_insn;
3352 rtx reg;
3354 if (TEST_BIT (reaching_defs[BLOCK_NUM (insn)], INSN_CUID (def_insn)))
3355 return 1;
3357 if (BLOCK_NUM (insn) == BLOCK_NUM (def_insn))
3359 if (INSN_CUID (def_insn) < INSN_CUID (insn))
3361 if (GET_CODE (PATTERN (def_insn)) == PARALLEL)
3362 return 1;
3363 else if (GET_CODE (PATTERN (def_insn)) == CLOBBER)
3364 reg = XEXP (PATTERN (def_insn), 0);
3365 else if (GET_CODE (PATTERN (def_insn)) == SET)
3366 reg = SET_DEST (PATTERN (def_insn));
3367 else
3368 abort ();
3370 return ! reg_set_between_p (reg, NEXT_INSN (def_insn), insn);
3372 else
3373 return 0;
3376 return 0;
3379 /* Return nonzero if *ADDR_THIS_REG can only have one value at INSN. The
3380 value returned is the number of definitions that reach INSN. Returning a
3381 value of zero means that [maybe] more than one definition reaches INSN and
3382 the caller can't perform whatever optimization it is trying. i.e. it is
3383 always safe to return zero. */
3385 static int
3386 can_disregard_other_sets (addr_this_reg, insn, for_combine)
3387 struct reg_set **addr_this_reg;
3388 rtx insn;
3389 int for_combine;
3391 int number_of_reaching_defs = 0;
3392 struct reg_set *this_reg;
3394 for (this_reg = *addr_this_reg; this_reg != 0; this_reg = this_reg->next)
3395 if (def_reaches_here_p (insn, this_reg->insn))
3397 number_of_reaching_defs++;
3398 /* Ignore parallels for now. */
3399 if (GET_CODE (PATTERN (this_reg->insn)) == PARALLEL)
3400 return 0;
3402 if (!for_combine
3403 && (GET_CODE (PATTERN (this_reg->insn)) == CLOBBER
3404 || ! rtx_equal_p (SET_SRC (PATTERN (this_reg->insn)),
3405 SET_SRC (PATTERN (insn)))))
3406 /* A setting of the reg to a different value reaches INSN. */
3407 return 0;
3409 if (number_of_reaching_defs > 1)
3411 /* If in this setting the value the register is being set to is
3412 equal to the previous value the register was set to and this
3413 setting reaches the insn we are trying to do the substitution
3414 on then we are ok. */
3415 if (GET_CODE (PATTERN (this_reg->insn)) == CLOBBER)
3416 return 0;
3417 else if (! rtx_equal_p (SET_SRC (PATTERN (this_reg->insn)),
3418 SET_SRC (PATTERN (insn))))
3419 return 0;
3422 *addr_this_reg = this_reg;
3425 return number_of_reaching_defs;
3428 /* Expression computed by insn is available and the substitution is legal,
3429 so try to perform the substitution.
3431 The result is nonzero if any changes were made. */
3433 static int
3434 handle_avail_expr (insn, expr)
3435 rtx insn;
3436 struct expr *expr;
3438 rtx pat, insn_computes_expr, expr_set;
3439 rtx to;
3440 struct reg_set *this_reg;
3441 int found_setting, use_src;
3442 int changed = 0;
3444 /* We only handle the case where one computation of the expression
3445 reaches this instruction. */
3446 insn_computes_expr = computing_insn (expr, insn);
3447 if (insn_computes_expr == NULL)
3448 return 0;
3449 expr_set = single_set (insn_computes_expr);
3450 if (!expr_set)
3451 abort ();
3453 found_setting = 0;
3454 use_src = 0;
3456 /* At this point we know only one computation of EXPR outside of this
3457 block reaches this insn. Now try to find a register that the
3458 expression is computed into. */
3459 if (GET_CODE (SET_SRC (expr_set)) == REG)
3461 /* This is the case when the available expression that reaches
3462 here has already been handled as an available expression. */
3463 unsigned int regnum_for_replacing
3464 = REGNO (SET_SRC (expr_set));
3466 /* If the register was created by GCSE we can't use `reg_set_table',
3467 however we know it's set only once. */
3468 if (regnum_for_replacing >= max_gcse_regno
3469 /* If the register the expression is computed into is set only once,
3470 or only one set reaches this insn, we can use it. */
3471 || (((this_reg = reg_set_table[regnum_for_replacing]),
3472 this_reg->next == NULL)
3473 || can_disregard_other_sets (&this_reg, insn, 0)))
3475 use_src = 1;
3476 found_setting = 1;
3480 if (!found_setting)
3482 unsigned int regnum_for_replacing
3483 = REGNO (SET_DEST (expr_set));
3485 /* This shouldn't happen. */
3486 if (regnum_for_replacing >= max_gcse_regno)
3487 abort ();
3489 this_reg = reg_set_table[regnum_for_replacing];
3491 /* If the register the expression is computed into is set only once,
3492 or only one set reaches this insn, use it. */
3493 if (this_reg->next == NULL
3494 || can_disregard_other_sets (&this_reg, insn, 0))
3495 found_setting = 1;
3498 if (found_setting)
3500 pat = PATTERN (insn);
3501 if (use_src)
3502 to = SET_SRC (expr_set);
3503 else
3504 to = SET_DEST (expr_set);
3505 changed = validate_change (insn, &SET_SRC (pat), to, 0);
3507 /* We should be able to ignore the return code from validate_change but
3508 to play it safe we check. */
3509 if (changed)
3511 gcse_subst_count++;
3512 if (gcse_file != NULL)
3514 fprintf (gcse_file, "GCSE: Replacing the source in insn %d with",
3515 INSN_UID (insn));
3516 fprintf (gcse_file, " reg %d %s insn %d\n",
3517 REGNO (to), use_src ? "from" : "set in",
3518 INSN_UID (insn_computes_expr));
3523 /* The register that the expr is computed into is set more than once. */
3524 else if (1 /*expensive_op(this_pattrn->op) && do_expensive_gcse)*/)
3526 /* Insert an insn after insnx that copies the reg set in insnx
3527 into a new pseudo register call this new register REGN.
3528 From insnb until end of basic block or until REGB is set
3529 replace all uses of REGB with REGN. */
3530 rtx new_insn;
3532 to = gen_reg_rtx (GET_MODE (SET_DEST (expr_set)));
3534 /* Generate the new insn. */
3535 /* ??? If the change fails, we return 0, even though we created
3536 an insn. I think this is ok. */
3537 new_insn
3538 = emit_insn_after (gen_rtx_SET (VOIDmode, to,
3539 SET_DEST (expr_set)),
3540 insn_computes_expr);
3542 /* Keep register set table up to date. */
3543 record_one_set (REGNO (to), new_insn);
3545 gcse_create_count++;
3546 if (gcse_file != NULL)
3548 fprintf (gcse_file, "GCSE: Creating insn %d to copy value of reg %d",
3549 INSN_UID (NEXT_INSN (insn_computes_expr)),
3550 REGNO (SET_SRC (PATTERN (NEXT_INSN (insn_computes_expr)))));
3551 fprintf (gcse_file, ", computed in insn %d,\n",
3552 INSN_UID (insn_computes_expr));
3553 fprintf (gcse_file, " into newly allocated reg %d\n",
3554 REGNO (to));
3557 pat = PATTERN (insn);
3559 /* Do register replacement for INSN. */
3560 changed = validate_change (insn, &SET_SRC (pat),
3561 SET_DEST (PATTERN
3562 (NEXT_INSN (insn_computes_expr))),
3565 /* We should be able to ignore the return code from validate_change but
3566 to play it safe we check. */
3567 if (changed)
3569 gcse_subst_count++;
3570 if (gcse_file != NULL)
3572 fprintf (gcse_file,
3573 "GCSE: Replacing the source in insn %d with reg %d ",
3574 INSN_UID (insn),
3575 REGNO (SET_DEST (PATTERN (NEXT_INSN
3576 (insn_computes_expr)))));
3577 fprintf (gcse_file, "set in insn %d\n",
3578 INSN_UID (insn_computes_expr));
3583 return changed;
3586 /* Perform classic GCSE. This is called by one_classic_gcse_pass after all
3587 the dataflow analysis has been done.
3589 The result is nonzero if a change was made. */
3591 static int
3592 classic_gcse ()
3594 int changed;
3595 rtx insn;
3596 basic_block bb;
3598 /* Note we start at block 1. */
3600 if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
3601 return 0;
3603 changed = 0;
3604 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb, EXIT_BLOCK_PTR, next_bb)
3606 /* Reset tables used to keep track of what's still valid [since the
3607 start of the block]. */
3608 reset_opr_set_tables ();
3610 for (insn = bb->head;
3611 insn != NULL && insn != NEXT_INSN (bb->end);
3612 insn = NEXT_INSN (insn))
3614 /* Is insn of form (set (pseudo-reg) ...)? */
3615 if (GET_CODE (insn) == INSN
3616 && GET_CODE (PATTERN (insn)) == SET
3617 && GET_CODE (SET_DEST (PATTERN (insn))) == REG
3618 && REGNO (SET_DEST (PATTERN (insn))) >= FIRST_PSEUDO_REGISTER)
3620 rtx pat = PATTERN (insn);
3621 rtx src = SET_SRC (pat);
3622 struct expr *expr;
3624 if (want_to_gcse_p (src)
3625 /* Is the expression recorded? */
3626 && ((expr = lookup_expr (src, &expr_hash_table)) != NULL)
3627 /* Is the expression available [at the start of the
3628 block]? */
3629 && TEST_BIT (ae_in[bb->index], expr->bitmap_index)
3630 /* Are the operands unchanged since the start of the
3631 block? */
3632 && oprs_not_set_p (src, insn))
3633 changed |= handle_avail_expr (insn, expr);
3636 /* Keep track of everything modified by this insn. */
3637 /* ??? Need to be careful w.r.t. mods done to INSN. */
3638 if (INSN_P (insn))
3639 mark_oprs_set (insn);
3643 return changed;
3646 /* Top level routine to perform one classic GCSE pass.
3648 Return nonzero if a change was made. */
3650 static int
3651 one_classic_gcse_pass (pass)
3652 int pass;
3654 int changed = 0;
3656 gcse_subst_count = 0;
3657 gcse_create_count = 0;
3659 alloc_hash_table (max_cuid, &expr_hash_table, 0);
3660 alloc_rd_mem (last_basic_block, max_cuid);
3661 compute_hash_table (&expr_hash_table);
3662 if (gcse_file)
3663 dump_hash_table (gcse_file, "Expression", &expr_hash_table);
3665 if (expr_hash_table.n_elems > 0)
3667 compute_kill_rd ();
3668 compute_rd ();
3669 alloc_avail_expr_mem (last_basic_block, expr_hash_table.n_elems);
3670 compute_ae_gen (&expr_hash_table);
3671 compute_ae_kill (ae_gen, ae_kill, &expr_hash_table);
3672 compute_available (ae_gen, ae_kill, ae_out, ae_in);
3673 changed = classic_gcse ();
3674 free_avail_expr_mem ();
3677 free_rd_mem ();
3678 free_hash_table (&expr_hash_table);
3680 if (gcse_file)
3682 fprintf (gcse_file, "\n");
3683 fprintf (gcse_file, "GCSE of %s, pass %d: %d bytes needed, %d substs,",
3684 current_function_name, pass, bytes_used, gcse_subst_count);
3685 fprintf (gcse_file, "%d insns created\n", gcse_create_count);
3688 return changed;
3691 /* Compute copy/constant propagation working variables. */
3693 /* Local properties of assignments. */
3694 static sbitmap *cprop_pavloc;
3695 static sbitmap *cprop_absaltered;
3697 /* Global properties of assignments (computed from the local properties). */
3698 static sbitmap *cprop_avin;
3699 static sbitmap *cprop_avout;
3701 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
3702 basic blocks. N_SETS is the number of sets. */
3704 static void
3705 alloc_cprop_mem (n_blocks, n_sets)
3706 int n_blocks, n_sets;
3708 cprop_pavloc = sbitmap_vector_alloc (n_blocks, n_sets);
3709 cprop_absaltered = sbitmap_vector_alloc (n_blocks, n_sets);
3711 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
3712 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
3715 /* Free vars used by copy/const propagation. */
3717 static void
3718 free_cprop_mem ()
3720 sbitmap_vector_free (cprop_pavloc);
3721 sbitmap_vector_free (cprop_absaltered);
3722 sbitmap_vector_free (cprop_avin);
3723 sbitmap_vector_free (cprop_avout);
3726 /* For each block, compute whether X is transparent. X is either an
3727 expression or an assignment [though we don't care which, for this context
3728 an assignment is treated as an expression]. For each block where an
3729 element of X is modified, set (SET_P == 1) or reset (SET_P == 0) the INDX
3730 bit in BMAP. */
3732 static void
3733 compute_transp (x, indx, bmap, set_p)
3734 rtx x;
3735 int indx;
3736 sbitmap *bmap;
3737 int set_p;
3739 int i, j;
3740 basic_block bb;
3741 enum rtx_code code;
3742 reg_set *r;
3743 const char *fmt;
3745 /* repeat is used to turn tail-recursion into iteration since GCC
3746 can't do it when there's no return value. */
3747 repeat:
3749 if (x == 0)
3750 return;
3752 code = GET_CODE (x);
3753 switch (code)
3755 case REG:
3756 if (set_p)
3758 if (REGNO (x) < FIRST_PSEUDO_REGISTER)
3760 FOR_EACH_BB (bb)
3761 if (TEST_BIT (reg_set_in_block[bb->index], REGNO (x)))
3762 SET_BIT (bmap[bb->index], indx);
3764 else
3766 for (r = reg_set_table[REGNO (x)]; r != NULL; r = r->next)
3767 SET_BIT (bmap[BLOCK_NUM (r->insn)], indx);
3770 else
3772 if (REGNO (x) < FIRST_PSEUDO_REGISTER)
3774 FOR_EACH_BB (bb)
3775 if (TEST_BIT (reg_set_in_block[bb->index], REGNO (x)))
3776 RESET_BIT (bmap[bb->index], indx);
3778 else
3780 for (r = reg_set_table[REGNO (x)]; r != NULL; r = r->next)
3781 RESET_BIT (bmap[BLOCK_NUM (r->insn)], indx);
3785 return;
3787 case MEM:
3788 FOR_EACH_BB (bb)
3790 rtx list_entry = canon_modify_mem_list[bb->index];
3792 while (list_entry)
3794 rtx dest, dest_addr;
3796 if (GET_CODE (XEXP (list_entry, 0)) == CALL_INSN)
3798 if (set_p)
3799 SET_BIT (bmap[bb->index], indx);
3800 else
3801 RESET_BIT (bmap[bb->index], indx);
3802 break;
3804 /* LIST_ENTRY must be an INSN of some kind that sets memory.
3805 Examine each hunk of memory that is modified. */
3807 dest = XEXP (list_entry, 0);
3808 list_entry = XEXP (list_entry, 1);
3809 dest_addr = XEXP (list_entry, 0);
3811 if (canon_true_dependence (dest, GET_MODE (dest), dest_addr,
3812 x, rtx_addr_varies_p))
3814 if (set_p)
3815 SET_BIT (bmap[bb->index], indx);
3816 else
3817 RESET_BIT (bmap[bb->index], indx);
3818 break;
3820 list_entry = XEXP (list_entry, 1);
3824 x = XEXP (x, 0);
3825 goto repeat;
3827 case PC:
3828 case CC0: /*FIXME*/
3829 case CONST:
3830 case CONST_INT:
3831 case CONST_DOUBLE:
3832 case CONST_VECTOR:
3833 case SYMBOL_REF:
3834 case LABEL_REF:
3835 case ADDR_VEC:
3836 case ADDR_DIFF_VEC:
3837 return;
3839 default:
3840 break;
3843 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
3845 if (fmt[i] == 'e')
3847 /* If we are about to do the last recursive call
3848 needed at this level, change it into iteration.
3849 This function is called enough to be worth it. */
3850 if (i == 0)
3852 x = XEXP (x, i);
3853 goto repeat;
3856 compute_transp (XEXP (x, i), indx, bmap, set_p);
3858 else if (fmt[i] == 'E')
3859 for (j = 0; j < XVECLEN (x, i); j++)
3860 compute_transp (XVECEXP (x, i, j), indx, bmap, set_p);
3864 /* Top level routine to do the dataflow analysis needed by copy/const
3865 propagation. */
3867 static void
3868 compute_cprop_data ()
3870 compute_local_properties (cprop_absaltered, cprop_pavloc, NULL, &set_hash_table);
3871 compute_available (cprop_pavloc, cprop_absaltered,
3872 cprop_avout, cprop_avin);
3875 /* Copy/constant propagation. */
3877 /* Maximum number of register uses in an insn that we handle. */
3878 #define MAX_USES 8
3880 /* Table of uses found in an insn.
3881 Allocated statically to avoid alloc/free complexity and overhead. */
3882 static struct reg_use reg_use_table[MAX_USES];
3884 /* Index into `reg_use_table' while building it. */
3885 static int reg_use_count;
3887 /* Set up a list of register numbers used in INSN. The found uses are stored
3888 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
3889 and contains the number of uses in the table upon exit.
3891 ??? If a register appears multiple times we will record it multiple times.
3892 This doesn't hurt anything but it will slow things down. */
3894 static void
3895 find_used_regs (xptr, data)
3896 rtx *xptr;
3897 void *data ATTRIBUTE_UNUSED;
3899 int i, j;
3900 enum rtx_code code;
3901 const char *fmt;
3902 rtx x = *xptr;
3904 /* repeat is used to turn tail-recursion into iteration since GCC
3905 can't do it when there's no return value. */
3906 repeat:
3907 if (x == 0)
3908 return;
3910 code = GET_CODE (x);
3911 if (REG_P (x))
3913 if (reg_use_count == MAX_USES)
3914 return;
3916 reg_use_table[reg_use_count].reg_rtx = x;
3917 reg_use_count++;
3920 /* Recursively scan the operands of this expression. */
3922 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
3924 if (fmt[i] == 'e')
3926 /* If we are about to do the last recursive call
3927 needed at this level, change it into iteration.
3928 This function is called enough to be worth it. */
3929 if (i == 0)
3931 x = XEXP (x, 0);
3932 goto repeat;
3935 find_used_regs (&XEXP (x, i), data);
3937 else if (fmt[i] == 'E')
3938 for (j = 0; j < XVECLEN (x, i); j++)
3939 find_used_regs (&XVECEXP (x, i, j), data);
3943 /* Try to replace all non-SET_DEST occurrences of FROM in INSN with TO.
3944 Returns nonzero is successful. */
3946 static int
3947 try_replace_reg (from, to, insn)
3948 rtx from, to, insn;
3950 rtx note = find_reg_equal_equiv_note (insn);
3951 rtx src = 0;
3952 int success = 0;
3953 rtx set = single_set (insn);
3955 validate_replace_src_group (from, to, insn);
3956 if (num_changes_pending () && apply_change_group ())
3957 success = 1;
3959 if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
3961 /* If above failed and this is a single set, try to simplify the source of
3962 the set given our substitution. We could perhaps try this for multiple
3963 SETs, but it probably won't buy us anything. */
3964 src = simplify_replace_rtx (SET_SRC (set), from, to);
3966 if (!rtx_equal_p (src, SET_SRC (set))
3967 && validate_change (insn, &SET_SRC (set), src, 0))
3968 success = 1;
3970 /* If we've failed to do replacement, have a single SET, and don't already
3971 have a note, add a REG_EQUAL note to not lose information. */
3972 if (!success && note == 0 && set != 0)
3973 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
3976 /* If there is already a NOTE, update the expression in it with our
3977 replacement. */
3978 else if (note != 0)
3979 XEXP (note, 0) = simplify_replace_rtx (XEXP (note, 0), from, to);
3981 /* REG_EQUAL may get simplified into register.
3982 We don't allow that. Remove that note. This code ought
3983 not to happen, because previous code ought to synthesize
3984 reg-reg move, but be on the safe side. */
3985 if (note && REG_P (XEXP (note, 0)))
3986 remove_note (insn, note);
3988 return success;
3991 /* Find a set of REGNOs that are available on entry to INSN's block. Returns
3992 NULL no such set is found. */
3994 static struct expr *
3995 find_avail_set (regno, insn)
3996 int regno;
3997 rtx insn;
3999 /* SET1 contains the last set found that can be returned to the caller for
4000 use in a substitution. */
4001 struct expr *set1 = 0;
4003 /* Loops are not possible here. To get a loop we would need two sets
4004 available at the start of the block containing INSN. ie we would
4005 need two sets like this available at the start of the block:
4007 (set (reg X) (reg Y))
4008 (set (reg Y) (reg X))
4010 This can not happen since the set of (reg Y) would have killed the
4011 set of (reg X) making it unavailable at the start of this block. */
4012 while (1)
4014 rtx src;
4015 struct expr *set = lookup_set (regno, &set_hash_table);
4017 /* Find a set that is available at the start of the block
4018 which contains INSN. */
4019 while (set)
4021 if (TEST_BIT (cprop_avin[BLOCK_NUM (insn)], set->bitmap_index))
4022 break;
4023 set = next_set (regno, set);
4026 /* If no available set was found we've reached the end of the
4027 (possibly empty) copy chain. */
4028 if (set == 0)
4029 break;
4031 if (GET_CODE (set->expr) != SET)
4032 abort ();
4034 src = SET_SRC (set->expr);
4036 /* We know the set is available.
4037 Now check that SRC is ANTLOC (i.e. none of the source operands
4038 have changed since the start of the block).
4040 If the source operand changed, we may still use it for the next
4041 iteration of this loop, but we may not use it for substitutions. */
4043 if (gcse_constant_p (src) || oprs_not_set_p (src, insn))
4044 set1 = set;
4046 /* If the source of the set is anything except a register, then
4047 we have reached the end of the copy chain. */
4048 if (GET_CODE (src) != REG)
4049 break;
4051 /* Follow the copy chain, ie start another iteration of the loop
4052 and see if we have an available copy into SRC. */
4053 regno = REGNO (src);
4056 /* SET1 holds the last set that was available and anticipatable at
4057 INSN. */
4058 return set1;
4061 /* Subroutine of cprop_insn that tries to propagate constants into
4062 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
4063 it is the instruction that immediately precedes JUMP, and must be a
4064 single SET of a register. FROM is what we will try to replace,
4065 SRC is the constant we will try to substitute for it. Returns nonzero
4066 if a change was made. */
4068 static int
4069 cprop_jump (bb, setcc, jump, from, src)
4070 basic_block bb;
4071 rtx setcc;
4072 rtx jump;
4073 rtx from;
4074 rtx src;
4076 rtx new, new_set;
4077 rtx set = pc_set (jump);
4079 /* First substitute in the INSN condition as the SET_SRC of the JUMP,
4080 then substitute that given values in this expanded JUMP. */
4081 if (setcc != NULL
4082 && !modified_between_p (from, setcc, jump)
4083 && !modified_between_p (src, setcc, jump))
4085 rtx setcc_set = single_set (setcc);
4086 new_set = simplify_replace_rtx (SET_SRC (set),
4087 SET_DEST (setcc_set),
4088 SET_SRC (setcc_set));
4090 else
4091 new_set = set;
4093 new = simplify_replace_rtx (new_set, from, src);
4095 /* If no simplification can be made, then try the next
4096 register. */
4097 if (rtx_equal_p (new, new_set) || rtx_equal_p (new, SET_SRC (set)))
4098 return 0;
4100 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
4101 if (new == pc_rtx)
4102 delete_insn (jump);
4103 else
4105 /* Ensure the value computed inside the jump insn to be equivalent
4106 to one computed by setcc. */
4107 if (setcc
4108 && modified_in_p (new, setcc))
4109 return 0;
4110 if (! validate_change (jump, &SET_SRC (set), new, 0))
4111 return 0;
4113 /* If this has turned into an unconditional jump,
4114 then put a barrier after it so that the unreachable
4115 code will be deleted. */
4116 if (GET_CODE (SET_SRC (set)) == LABEL_REF)
4117 emit_barrier_after (jump);
4120 #ifdef HAVE_cc0
4121 /* Delete the cc0 setter. */
4122 if (setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
4123 delete_insn (setcc);
4124 #endif
4126 run_jump_opt_after_gcse = 1;
4128 const_prop_count++;
4129 if (gcse_file != NULL)
4131 fprintf (gcse_file,
4132 "CONST-PROP: Replacing reg %d in jump_insn %d with constant ",
4133 REGNO (from), INSN_UID (jump));
4134 print_rtl (gcse_file, src);
4135 fprintf (gcse_file, "\n");
4137 purge_dead_edges (bb);
4139 return 1;
4142 static bool
4143 constprop_register (insn, from, to, alter_jumps)
4144 rtx insn;
4145 rtx from;
4146 rtx to;
4147 int alter_jumps;
4149 rtx sset;
4151 /* Check for reg or cc0 setting instructions followed by
4152 conditional branch instructions first. */
4153 if (alter_jumps
4154 && (sset = single_set (insn)) != NULL
4155 && NEXT_INSN (insn)
4156 && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
4158 rtx dest = SET_DEST (sset);
4159 if ((REG_P (dest) || CC0_P (dest))
4160 && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn), from, to))
4161 return 1;
4164 /* Handle normal insns next. */
4165 if (GET_CODE (insn) == INSN
4166 && try_replace_reg (from, to, insn))
4167 return 1;
4169 /* Try to propagate a CONST_INT into a conditional jump.
4170 We're pretty specific about what we will handle in this
4171 code, we can extend this as necessary over time.
4173 Right now the insn in question must look like
4174 (set (pc) (if_then_else ...)) */
4175 else if (alter_jumps && any_condjump_p (insn) && onlyjump_p (insn))
4176 return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, to);
4177 return 0;
4180 /* Perform constant and copy propagation on INSN.
4181 The result is nonzero if a change was made. */
4183 static int
4184 cprop_insn (insn, alter_jumps)
4185 rtx insn;
4186 int alter_jumps;
4188 struct reg_use *reg_used;
4189 int changed = 0;
4190 rtx note;
4192 if (!INSN_P (insn))
4193 return 0;
4195 reg_use_count = 0;
4196 note_uses (&PATTERN (insn), find_used_regs, NULL);
4198 note = find_reg_equal_equiv_note (insn);
4200 /* We may win even when propagating constants into notes. */
4201 if (note)
4202 find_used_regs (&XEXP (note, 0), NULL);
4204 for (reg_used = &reg_use_table[0]; reg_use_count > 0;
4205 reg_used++, reg_use_count--)
4207 unsigned int regno = REGNO (reg_used->reg_rtx);
4208 rtx pat, src;
4209 struct expr *set;
4211 /* Ignore registers created by GCSE.
4212 We do this because ... */
4213 if (regno >= max_gcse_regno)
4214 continue;
4216 /* If the register has already been set in this block, there's
4217 nothing we can do. */
4218 if (! oprs_not_set_p (reg_used->reg_rtx, insn))
4219 continue;
4221 /* Find an assignment that sets reg_used and is available
4222 at the start of the block. */
4223 set = find_avail_set (regno, insn);
4224 if (! set)
4225 continue;
4227 pat = set->expr;
4228 /* ??? We might be able to handle PARALLELs. Later. */
4229 if (GET_CODE (pat) != SET)
4230 abort ();
4232 src = SET_SRC (pat);
4234 /* Constant propagation. */
4235 if (gcse_constant_p (src))
4237 if (constprop_register (insn, reg_used->reg_rtx, src, alter_jumps))
4239 changed = 1;
4240 const_prop_count++;
4241 if (gcse_file != NULL)
4243 fprintf (gcse_file, "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
4244 fprintf (gcse_file, "insn %d with constant ", INSN_UID (insn));
4245 print_rtl (gcse_file, src);
4246 fprintf (gcse_file, "\n");
4250 else if (GET_CODE (src) == REG
4251 && REGNO (src) >= FIRST_PSEUDO_REGISTER
4252 && REGNO (src) != regno)
4254 if (try_replace_reg (reg_used->reg_rtx, src, insn))
4256 changed = 1;
4257 copy_prop_count++;
4258 if (gcse_file != NULL)
4260 fprintf (gcse_file, "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
4261 regno, INSN_UID (insn));
4262 fprintf (gcse_file, " with reg %d\n", REGNO (src));
4265 /* The original insn setting reg_used may or may not now be
4266 deletable. We leave the deletion to flow. */
4267 /* FIXME: If it turns out that the insn isn't deletable,
4268 then we may have unnecessarily extended register lifetimes
4269 and made things worse. */
4274 return changed;
4277 /* Like find_used_regs, but avoid recording uses that appear in
4278 input-output contexts such as zero_extract or pre_dec. This
4279 restricts the cases we consider to those for which local cprop
4280 can legitimately make replacements. */
4282 static void
4283 local_cprop_find_used_regs (xptr, data)
4284 rtx *xptr;
4285 void *data;
4287 rtx x = *xptr;
4289 if (x == 0)
4290 return;
4292 switch (GET_CODE (x))
4294 case ZERO_EXTRACT:
4295 case SIGN_EXTRACT:
4296 case STRICT_LOW_PART:
4297 return;
4299 case PRE_DEC:
4300 case PRE_INC:
4301 case POST_DEC:
4302 case POST_INC:
4303 case PRE_MODIFY:
4304 case POST_MODIFY:
4305 /* Can only legitimately appear this early in the context of
4306 stack pushes for function arguments, but handle all of the
4307 codes nonetheless. */
4308 return;
4310 case SUBREG:
4311 /* Setting a subreg of a register larger than word_mode leaves
4312 the non-written words unchanged. */
4313 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) > BITS_PER_WORD)
4314 return;
4315 break;
4317 default:
4318 break;
4321 find_used_regs (xptr, data);
4324 /* LIBCALL_SP is a zero-terminated array of insns at the end of a libcall;
4325 their REG_EQUAL notes need updating. */
4327 static bool
4328 do_local_cprop (x, insn, alter_jumps, libcall_sp)
4329 rtx x;
4330 rtx insn;
4331 int alter_jumps;
4332 rtx *libcall_sp;
4334 rtx newreg = NULL, newcnst = NULL;
4336 /* Rule out USE instructions and ASM statements as we don't want to
4337 change the hard registers mentioned. */
4338 if (GET_CODE (x) == REG
4339 && (REGNO (x) >= FIRST_PSEUDO_REGISTER
4340 || (GET_CODE (PATTERN (insn)) != USE
4341 && asm_noperands (PATTERN (insn)) < 0)))
4343 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0);
4344 struct elt_loc_list *l;
4346 if (!val)
4347 return false;
4348 for (l = val->locs; l; l = l->next)
4350 rtx this_rtx = l->loc;
4351 rtx note;
4353 if (l->in_libcall)
4354 continue;
4356 if (gcse_constant_p (this_rtx))
4357 newcnst = this_rtx;
4358 if (REG_P (this_rtx) && REGNO (this_rtx) >= FIRST_PSEUDO_REGISTER
4359 /* Don't copy propagate if it has attached REG_EQUIV note.
4360 At this point this only function parameters should have
4361 REG_EQUIV notes and if the argument slot is used somewhere
4362 explicitly, it means address of parameter has been taken,
4363 so we should not extend the lifetime of the pseudo. */
4364 && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
4365 || GET_CODE (XEXP (note, 0)) != MEM))
4366 newreg = this_rtx;
4368 if (newcnst && constprop_register (insn, x, newcnst, alter_jumps))
4370 /* If we find a case where we can't fix the retval REG_EQUAL notes
4371 match the new register, we either have to abandon this replacement
4372 or fix delete_trivially_dead_insns to preserve the setting insn,
4373 or make it delete the REG_EUAQL note, and fix up all passes that
4374 require the REG_EQUAL note there. */
4375 if (!adjust_libcall_notes (x, newcnst, insn, libcall_sp))
4376 abort ();
4377 if (gcse_file != NULL)
4379 fprintf (gcse_file, "LOCAL CONST-PROP: Replacing reg %d in ",
4380 REGNO (x));
4381 fprintf (gcse_file, "insn %d with constant ",
4382 INSN_UID (insn));
4383 print_rtl (gcse_file, newcnst);
4384 fprintf (gcse_file, "\n");
4386 const_prop_count++;
4387 return true;
4389 else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
4391 adjust_libcall_notes (x, newreg, insn, libcall_sp);
4392 if (gcse_file != NULL)
4394 fprintf (gcse_file,
4395 "LOCAL COPY-PROP: Replacing reg %d in insn %d",
4396 REGNO (x), INSN_UID (insn));
4397 fprintf (gcse_file, " with reg %d\n", REGNO (newreg));
4399 copy_prop_count++;
4400 return true;
4403 return false;
4406 /* LIBCALL_SP is a zero-terminated array of insns at the end of a libcall;
4407 their REG_EQUAL notes need updating to reflect that OLDREG has been
4408 replaced with NEWVAL in INSN. Return true if all substitutions could
4409 be made. */
4410 static bool
4411 adjust_libcall_notes (oldreg, newval, insn, libcall_sp)
4412 rtx oldreg, newval, insn, *libcall_sp;
4414 rtx end;
4416 while ((end = *libcall_sp++))
4418 rtx note = find_reg_equal_equiv_note (end);
4420 if (! note)
4421 continue;
4423 if (REG_P (newval))
4425 if (reg_set_between_p (newval, PREV_INSN (insn), end))
4429 note = find_reg_equal_equiv_note (end);
4430 if (! note)
4431 continue;
4432 if (reg_mentioned_p (newval, XEXP (note, 0)))
4433 return false;
4435 while ((end = *libcall_sp++));
4436 return true;
4439 XEXP (note, 0) = replace_rtx (XEXP (note, 0), oldreg, newval);
4440 insn = end;
4442 return true;
4445 #define MAX_NESTED_LIBCALLS 9
4447 static void
4448 local_cprop_pass (alter_jumps)
4449 int alter_jumps;
4451 rtx insn;
4452 struct reg_use *reg_used;
4453 rtx libcall_stack[MAX_NESTED_LIBCALLS + 1], *libcall_sp;
4454 bool changed = false;
4456 cselib_init ();
4457 libcall_sp = &libcall_stack[MAX_NESTED_LIBCALLS];
4458 *libcall_sp = 0;
4459 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
4461 if (INSN_P (insn))
4463 rtx note = find_reg_note (insn, REG_LIBCALL, NULL_RTX);
4465 if (note)
4467 if (libcall_sp == libcall_stack)
4468 abort ();
4469 *--libcall_sp = XEXP (note, 0);
4471 note = find_reg_note (insn, REG_RETVAL, NULL_RTX);
4472 if (note)
4473 libcall_sp++;
4474 note = find_reg_equal_equiv_note (insn);
4477 reg_use_count = 0;
4478 note_uses (&PATTERN (insn), local_cprop_find_used_regs, NULL);
4479 if (note)
4480 local_cprop_find_used_regs (&XEXP (note, 0), NULL);
4482 for (reg_used = &reg_use_table[0]; reg_use_count > 0;
4483 reg_used++, reg_use_count--)
4484 if (do_local_cprop (reg_used->reg_rtx, insn, alter_jumps,
4485 libcall_sp))
4487 changed = true;
4488 break;
4491 while (reg_use_count);
4493 cselib_process_insn (insn);
4495 cselib_finish ();
4496 /* Global analysis may get into infinite loops for unreachable blocks. */
4497 if (changed && alter_jumps)
4499 delete_unreachable_blocks ();
4500 free_reg_set_mem ();
4501 alloc_reg_set_mem (max_reg_num ());
4502 compute_sets (get_insns ());
4506 /* Forward propagate copies. This includes copies and constants. Return
4507 nonzero if a change was made. */
4509 static int
4510 cprop (alter_jumps)
4511 int alter_jumps;
4513 int changed;
4514 basic_block bb;
4515 rtx insn;
4517 /* Note we start at block 1. */
4518 if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
4520 if (gcse_file != NULL)
4521 fprintf (gcse_file, "\n");
4522 return 0;
4525 changed = 0;
4526 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb, EXIT_BLOCK_PTR, next_bb)
4528 /* Reset tables used to keep track of what's still valid [since the
4529 start of the block]. */
4530 reset_opr_set_tables ();
4532 for (insn = bb->head;
4533 insn != NULL && insn != NEXT_INSN (bb->end);
4534 insn = NEXT_INSN (insn))
4535 if (INSN_P (insn))
4537 changed |= cprop_insn (insn, alter_jumps);
4539 /* Keep track of everything modified by this insn. */
4540 /* ??? Need to be careful w.r.t. mods done to INSN. Don't
4541 call mark_oprs_set if we turned the insn into a NOTE. */
4542 if (GET_CODE (insn) != NOTE)
4543 mark_oprs_set (insn);
4547 if (gcse_file != NULL)
4548 fprintf (gcse_file, "\n");
4550 return changed;
4553 /* Similar to get_condition, only the resulting condition must be
4554 valid at JUMP, instead of at EARLIEST.
4556 This differs from noce_get_condition in ifcvt.c in that we prefer not to
4557 settle for the condition variable in the jump instruction being integral.
4558 We prefer to be able to record the value of a user variable, rather than
4559 the value of a temporary used in a condition. This could be solved by
4560 recording the value of *every* register scaned by canonicalize_condition,
4561 but this would require some code reorganization. */
4563 static rtx
4564 fis_get_condition (jump)
4565 rtx jump;
4567 rtx cond, set, tmp, insn, earliest;
4568 bool reverse;
4570 if (! any_condjump_p (jump))
4571 return NULL_RTX;
4573 set = pc_set (jump);
4574 cond = XEXP (SET_SRC (set), 0);
4576 /* If this branches to JUMP_LABEL when the condition is false,
4577 reverse the condition. */
4578 reverse = (GET_CODE (XEXP (SET_SRC (set), 2)) == LABEL_REF
4579 && XEXP (XEXP (SET_SRC (set), 2), 0) == JUMP_LABEL (jump));
4581 /* Use canonicalize_condition to do the dirty work of manipulating
4582 MODE_CC values and COMPARE rtx codes. */
4583 tmp = canonicalize_condition (jump, cond, reverse, &earliest, NULL_RTX);
4584 if (!tmp)
4585 return NULL_RTX;
4587 /* Verify that the given condition is valid at JUMP by virtue of not
4588 having been modified since EARLIEST. */
4589 for (insn = earliest; insn != jump; insn = NEXT_INSN (insn))
4590 if (INSN_P (insn) && modified_in_p (tmp, insn))
4591 break;
4592 if (insn == jump)
4593 return tmp;
4595 /* The condition was modified. See if we can get a partial result
4596 that doesn't follow all the reversals. Perhaps combine can fold
4597 them together later. */
4598 tmp = XEXP (tmp, 0);
4599 if (!REG_P (tmp) || GET_MODE_CLASS (GET_MODE (tmp)) != MODE_INT)
4600 return NULL_RTX;
4601 tmp = canonicalize_condition (jump, cond, reverse, &earliest, tmp);
4602 if (!tmp)
4603 return NULL_RTX;
4605 /* For sanity's sake, re-validate the new result. */
4606 for (insn = earliest; insn != jump; insn = NEXT_INSN (insn))
4607 if (INSN_P (insn) && modified_in_p (tmp, insn))
4608 return NULL_RTX;
4610 return tmp;
4613 /* Find the implicit sets of a function. An "implicit set" is a constraint
4614 on the value of a variable, implied by a conditional jump. For example,
4615 following "if (x == 2)", the then branch may be optimized as though the
4616 conditional performed an "explicit set", in this example, "x = 2". This
4617 function records the set patterns that are implicit at the start of each
4618 basic block. */
4620 static void
4621 find_implicit_sets ()
4623 basic_block bb, dest;
4624 unsigned int count;
4625 rtx cond, new;
4627 count = 0;
4628 FOR_EACH_BB (bb)
4629 /* Check for more than one sucessor. */
4630 if (bb->succ && bb->succ->succ_next)
4632 cond = fis_get_condition (bb->end);
4634 if (cond
4635 && (GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
4636 && GET_CODE (XEXP (cond, 0)) == REG
4637 && REGNO (XEXP (cond, 0)) >= FIRST_PSEUDO_REGISTER
4638 && gcse_constant_p (XEXP (cond, 1)))
4640 dest = GET_CODE (cond) == EQ ? BRANCH_EDGE (bb)->dest
4641 : FALLTHRU_EDGE (bb)->dest;
4643 if (dest && ! dest->pred->pred_next
4644 && dest != EXIT_BLOCK_PTR)
4646 new = gen_rtx_SET (VOIDmode, XEXP (cond, 0),
4647 XEXP (cond, 1));
4648 implicit_sets[dest->index] = new;
4649 if (gcse_file)
4651 fprintf(gcse_file, "Implicit set of reg %d in ",
4652 REGNO (XEXP (cond, 0)));
4653 fprintf(gcse_file, "basic block %d\n", dest->index);
4655 count++;
4660 if (gcse_file)
4661 fprintf (gcse_file, "Found %d implicit sets\n", count);
4664 /* Perform one copy/constant propagation pass.
4665 PASS is the pass count. If CPROP_JUMPS is true, perform constant
4666 propagation into conditional jumps. If BYPASS_JUMPS is true,
4667 perform conditional jump bypassing optimizations. */
4669 static int
4670 one_cprop_pass (pass, cprop_jumps, bypass_jumps)
4671 int pass;
4672 int cprop_jumps;
4673 int bypass_jumps;
4675 int changed = 0;
4677 const_prop_count = 0;
4678 copy_prop_count = 0;
4680 local_cprop_pass (cprop_jumps);
4682 /* Determine implicit sets. */
4683 implicit_sets = (rtx *) xcalloc (last_basic_block, sizeof (rtx));
4684 find_implicit_sets ();
4686 alloc_hash_table (max_cuid, &set_hash_table, 1);
4687 compute_hash_table (&set_hash_table);
4689 /* Free implicit_sets before peak usage. */
4690 free (implicit_sets);
4691 implicit_sets = NULL;
4693 if (gcse_file)
4694 dump_hash_table (gcse_file, "SET", &set_hash_table);
4695 if (set_hash_table.n_elems > 0)
4697 alloc_cprop_mem (last_basic_block, set_hash_table.n_elems);
4698 compute_cprop_data ();
4699 changed = cprop (cprop_jumps);
4700 if (bypass_jumps)
4701 changed |= bypass_conditional_jumps ();
4702 free_cprop_mem ();
4705 free_hash_table (&set_hash_table);
4707 if (gcse_file)
4709 fprintf (gcse_file, "CPROP of %s, pass %d: %d bytes needed, ",
4710 current_function_name, pass, bytes_used);
4711 fprintf (gcse_file, "%d const props, %d copy props\n\n",
4712 const_prop_count, copy_prop_count);
4714 /* Global analysis may get into infinite loops for unreachable blocks. */
4715 if (changed && cprop_jumps)
4716 delete_unreachable_blocks ();
4718 return changed;
4721 /* Bypass conditional jumps. */
4723 /* The value of last_basic_block at the beginning of the jump_bypass
4724 pass. The use of redirect_edge_and_branch_force may introduce new
4725 basic blocks, but the data flow analysis is only valid for basic
4726 block indices less than bypass_last_basic_block. */
4728 static int bypass_last_basic_block;
4730 /* Find a set of REGNO to a constant that is available at the end of basic
4731 block BB. Returns NULL if no such set is found. Based heavily upon
4732 find_avail_set. */
4734 static struct expr *
4735 find_bypass_set (regno, bb)
4736 int regno;
4737 int bb;
4739 struct expr *result = 0;
4741 for (;;)
4743 rtx src;
4744 struct expr *set = lookup_set (regno, &set_hash_table);
4746 while (set)
4748 if (TEST_BIT (cprop_avout[bb], set->bitmap_index))
4749 break;
4750 set = next_set (regno, set);
4753 if (set == 0)
4754 break;
4756 if (GET_CODE (set->expr) != SET)
4757 abort ();
4759 src = SET_SRC (set->expr);
4760 if (gcse_constant_p (src))
4761 result = set;
4763 if (GET_CODE (src) != REG)
4764 break;
4766 regno = REGNO (src);
4768 return result;
4772 /* Subroutine of bypass_block that checks whether a pseudo is killed by
4773 any of the instructions inserted on an edge. Jump bypassing places
4774 condition code setters on CFG edges using insert_insn_on_edge. This
4775 function is required to check that our data flow analysis is still
4776 valid prior to commit_edge_insertions. */
4778 static bool
4779 reg_killed_on_edge (reg, e)
4780 rtx reg;
4781 edge e;
4783 rtx insn;
4785 for (insn = e->insns; insn; insn = NEXT_INSN (insn))
4786 if (INSN_P (insn) && reg_set_p (reg, insn))
4787 return true;
4789 return false;
4792 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
4793 basic block BB which has more than one predecessor. If not NULL, SETCC
4794 is the first instruction of BB, which is immediately followed by JUMP_INSN
4795 JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
4796 Returns nonzero if a change was made.
4798 During the jump bypassing pass, we may place copies of SETCC instuctions
4799 on CFG edges. The following routine must be careful to pay attention to
4800 these inserted insns when performing its transformations. */
4802 static int
4803 bypass_block (bb, setcc, jump)
4804 basic_block bb;
4805 rtx setcc, jump;
4807 rtx insn, note;
4808 edge e, enext, edest;
4809 int i, change;
4810 int may_be_loop_header;
4812 insn = (setcc != NULL) ? setcc : jump;
4814 /* Determine set of register uses in INSN. */
4815 reg_use_count = 0;
4816 note_uses (&PATTERN (insn), find_used_regs, NULL);
4817 note = find_reg_equal_equiv_note (insn);
4818 if (note)
4819 find_used_regs (&XEXP (note, 0), NULL);
4821 may_be_loop_header = false;
4822 for (e = bb->pred; e; e = e->pred_next)
4823 if (e->flags & EDGE_DFS_BACK)
4825 may_be_loop_header = true;
4826 break;
4829 change = 0;
4830 for (e = bb->pred; e; e = enext)
4832 enext = e->pred_next;
4833 if (e->flags & EDGE_COMPLEX)
4834 continue;
4836 /* We can't redirect edges from new basic blocks. */
4837 if (e->src->index >= bypass_last_basic_block)
4838 continue;
4840 /* The irreducible loops created by redirecting of edges entering the
4841 loop from outside would decrease effectivity of some of the following
4842 optimalizations, so prevent this. */
4843 if (may_be_loop_header
4844 && !(e->flags & EDGE_DFS_BACK))
4845 continue;
4847 for (i = 0; i < reg_use_count; i++)
4849 struct reg_use *reg_used = &reg_use_table[i];
4850 unsigned int regno = REGNO (reg_used->reg_rtx);
4851 basic_block dest, old_dest;
4852 struct expr *set;
4853 rtx src, new;
4855 if (regno >= max_gcse_regno)
4856 continue;
4858 set = find_bypass_set (regno, e->src->index);
4860 if (! set)
4861 continue;
4863 /* Check the data flow is valid after edge insertions. */
4864 if (e->insns && reg_killed_on_edge (reg_used->reg_rtx, e))
4865 continue;
4867 src = SET_SRC (pc_set (jump));
4869 if (setcc != NULL)
4870 src = simplify_replace_rtx (src,
4871 SET_DEST (PATTERN (setcc)),
4872 SET_SRC (PATTERN (setcc)));
4874 new = simplify_replace_rtx (src, reg_used->reg_rtx,
4875 SET_SRC (set->expr));
4877 /* Jump bypassing may have already placed instructions on
4878 edges of the CFG. We can't bypass an outgoing edge that
4879 has instructions associated with it, as these insns won't
4880 get executed if the incoming edge is redirected. */
4882 if (new == pc_rtx)
4884 edest = FALLTHRU_EDGE (bb);
4885 dest = edest->insns ? NULL : edest->dest;
4887 else if (GET_CODE (new) == LABEL_REF)
4889 dest = BLOCK_FOR_INSN (XEXP (new, 0));
4890 /* Don't bypass edges containing instructions. */
4891 for (edest = bb->succ; edest; edest = edest->succ_next)
4892 if (edest->dest == dest && edest->insns)
4894 dest = NULL;
4895 break;
4898 else
4899 dest = NULL;
4901 old_dest = e->dest;
4902 if (dest != NULL
4903 && dest != old_dest
4904 && dest != EXIT_BLOCK_PTR)
4906 redirect_edge_and_branch_force (e, dest);
4908 /* Copy the register setter to the redirected edge.
4909 Don't copy CC0 setters, as CC0 is dead after jump. */
4910 if (setcc)
4912 rtx pat = PATTERN (setcc);
4913 if (!CC0_P (SET_DEST (pat)))
4914 insert_insn_on_edge (copy_insn (pat), e);
4917 if (gcse_file != NULL)
4919 fprintf (gcse_file, "JUMP-BYPASS: Proved reg %d in jump_insn %d equals constant ",
4920 regno, INSN_UID (jump));
4921 print_rtl (gcse_file, SET_SRC (set->expr));
4922 fprintf (gcse_file, "\nBypass edge from %d->%d to %d\n",
4923 e->src->index, old_dest->index, dest->index);
4925 change = 1;
4926 break;
4930 return change;
4933 /* Find basic blocks with more than one predecessor that only contain a
4934 single conditional jump. If the result of the comparison is known at
4935 compile-time from any incoming edge, redirect that edge to the
4936 appropriate target. Returns nonzero if a change was made.
4938 This function is now mis-named, because we also handle indirect jumps. */
4940 static int
4941 bypass_conditional_jumps ()
4943 basic_block bb;
4944 int changed;
4945 rtx setcc;
4946 rtx insn;
4947 rtx dest;
4949 /* Note we start at block 1. */
4950 if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
4951 return 0;
4953 bypass_last_basic_block = last_basic_block;
4954 mark_dfs_back_edges ();
4956 changed = 0;
4957 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb,
4958 EXIT_BLOCK_PTR, next_bb)
4960 /* Check for more than one predecessor. */
4961 if (bb->pred && bb->pred->pred_next)
4963 setcc = NULL_RTX;
4964 for (insn = bb->head;
4965 insn != NULL && insn != NEXT_INSN (bb->end);
4966 insn = NEXT_INSN (insn))
4967 if (GET_CODE (insn) == INSN)
4969 if (setcc)
4970 break;
4971 if (GET_CODE (PATTERN (insn)) != SET)
4972 break;
4974 dest = SET_DEST (PATTERN (insn));
4975 if (REG_P (dest) || CC0_P (dest))
4976 setcc = insn;
4977 else
4978 break;
4980 else if (GET_CODE (insn) == JUMP_INSN)
4982 if ((any_condjump_p (insn) || computed_jump_p (insn))
4983 && onlyjump_p (insn))
4984 changed |= bypass_block (bb, setcc, insn);
4985 break;
4987 else if (INSN_P (insn))
4988 break;
4992 /* If we bypassed any register setting insns, we inserted a
4993 copy on the redirected edge. These need to be committed. */
4994 if (changed)
4995 commit_edge_insertions();
4997 return changed;
5000 /* Compute PRE+LCM working variables. */
5002 /* Local properties of expressions. */
5003 /* Nonzero for expressions that are transparent in the block. */
5004 static sbitmap *transp;
5006 /* Nonzero for expressions that are transparent at the end of the block.
5007 This is only zero for expressions killed by abnormal critical edge
5008 created by a calls. */
5009 static sbitmap *transpout;
5011 /* Nonzero for expressions that are computed (available) in the block. */
5012 static sbitmap *comp;
5014 /* Nonzero for expressions that are locally anticipatable in the block. */
5015 static sbitmap *antloc;
5017 /* Nonzero for expressions where this block is an optimal computation
5018 point. */
5019 static sbitmap *pre_optimal;
5021 /* Nonzero for expressions which are redundant in a particular block. */
5022 static sbitmap *pre_redundant;
5024 /* Nonzero for expressions which should be inserted on a specific edge. */
5025 static sbitmap *pre_insert_map;
5027 /* Nonzero for expressions which should be deleted in a specific block. */
5028 static sbitmap *pre_delete_map;
5030 /* Contains the edge_list returned by pre_edge_lcm. */
5031 static struct edge_list *edge_list;
5033 /* Redundant insns. */
5034 static sbitmap pre_redundant_insns;
5036 /* Allocate vars used for PRE analysis. */
5038 static void
5039 alloc_pre_mem (n_blocks, n_exprs)
5040 int n_blocks, n_exprs;
5042 transp = sbitmap_vector_alloc (n_blocks, n_exprs);
5043 comp = sbitmap_vector_alloc (n_blocks, n_exprs);
5044 antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
5046 pre_optimal = NULL;
5047 pre_redundant = NULL;
5048 pre_insert_map = NULL;
5049 pre_delete_map = NULL;
5050 ae_in = NULL;
5051 ae_out = NULL;
5052 ae_kill = sbitmap_vector_alloc (n_blocks, n_exprs);
5054 /* pre_insert and pre_delete are allocated later. */
5057 /* Free vars used for PRE analysis. */
5059 static void
5060 free_pre_mem ()
5062 sbitmap_vector_free (transp);
5063 sbitmap_vector_free (comp);
5065 /* ANTLOC and AE_KILL are freed just after pre_lcm finishes. */
5067 if (pre_optimal)
5068 sbitmap_vector_free (pre_optimal);
5069 if (pre_redundant)
5070 sbitmap_vector_free (pre_redundant);
5071 if (pre_insert_map)
5072 sbitmap_vector_free (pre_insert_map);
5073 if (pre_delete_map)
5074 sbitmap_vector_free (pre_delete_map);
5075 if (ae_in)
5076 sbitmap_vector_free (ae_in);
5077 if (ae_out)
5078 sbitmap_vector_free (ae_out);
5080 transp = comp = NULL;
5081 pre_optimal = pre_redundant = pre_insert_map = pre_delete_map = NULL;
5082 ae_in = ae_out = NULL;
5085 /* Top level routine to do the dataflow analysis needed by PRE. */
5087 static void
5088 compute_pre_data ()
5090 sbitmap trapping_expr;
5091 basic_block bb;
5092 unsigned int ui;
5094 compute_local_properties (transp, comp, antloc, &expr_hash_table);
5095 sbitmap_vector_zero (ae_kill, last_basic_block);
5097 /* Collect expressions which might trap. */
5098 trapping_expr = sbitmap_alloc (expr_hash_table.n_elems);
5099 sbitmap_zero (trapping_expr);
5100 for (ui = 0; ui < expr_hash_table.size; ui++)
5102 struct expr *e;
5103 for (e = expr_hash_table.table[ui]; e != NULL; e = e->next_same_hash)
5104 if (may_trap_p (e->expr))
5105 SET_BIT (trapping_expr, e->bitmap_index);
5108 /* Compute ae_kill for each basic block using:
5110 ~(TRANSP | COMP)
5112 This is significantly faster than compute_ae_kill. */
5114 FOR_EACH_BB (bb)
5116 edge e;
5118 /* If the current block is the destination of an abnormal edge, we
5119 kill all trapping expressions because we won't be able to properly
5120 place the instruction on the edge. So make them neither
5121 anticipatable nor transparent. This is fairly conservative. */
5122 for (e = bb->pred; e ; e = e->pred_next)
5123 if (e->flags & EDGE_ABNORMAL)
5125 sbitmap_difference (antloc[bb->index], antloc[bb->index], trapping_expr);
5126 sbitmap_difference (transp[bb->index], transp[bb->index], trapping_expr);
5127 break;
5130 sbitmap_a_or_b (ae_kill[bb->index], transp[bb->index], comp[bb->index]);
5131 sbitmap_not (ae_kill[bb->index], ae_kill[bb->index]);
5134 edge_list = pre_edge_lcm (gcse_file, expr_hash_table.n_elems, transp, comp, antloc,
5135 ae_kill, &pre_insert_map, &pre_delete_map);
5136 sbitmap_vector_free (antloc);
5137 antloc = NULL;
5138 sbitmap_vector_free (ae_kill);
5139 ae_kill = NULL;
5140 sbitmap_free (trapping_expr);
5143 /* PRE utilities */
5145 /* Return nonzero if an occurrence of expression EXPR in OCCR_BB would reach
5146 block BB.
5148 VISITED is a pointer to a working buffer for tracking which BB's have
5149 been visited. It is NULL for the top-level call.
5151 We treat reaching expressions that go through blocks containing the same
5152 reaching expression as "not reaching". E.g. if EXPR is generated in blocks
5153 2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
5154 2 as not reaching. The intent is to improve the probability of finding
5155 only one reaching expression and to reduce register lifetimes by picking
5156 the closest such expression. */
5158 static int
5159 pre_expr_reaches_here_p_work (occr_bb, expr, bb, visited)
5160 basic_block occr_bb;
5161 struct expr *expr;
5162 basic_block bb;
5163 char *visited;
5165 edge pred;
5167 for (pred = bb->pred; pred != NULL; pred = pred->pred_next)
5169 basic_block pred_bb = pred->src;
5171 if (pred->src == ENTRY_BLOCK_PTR
5172 /* Has predecessor has already been visited? */
5173 || visited[pred_bb->index])
5174 ;/* Nothing to do. */
5176 /* Does this predecessor generate this expression? */
5177 else if (TEST_BIT (comp[pred_bb->index], expr->bitmap_index))
5179 /* Is this the occurrence we're looking for?
5180 Note that there's only one generating occurrence per block
5181 so we just need to check the block number. */
5182 if (occr_bb == pred_bb)
5183 return 1;
5185 visited[pred_bb->index] = 1;
5187 /* Ignore this predecessor if it kills the expression. */
5188 else if (! TEST_BIT (transp[pred_bb->index], expr->bitmap_index))
5189 visited[pred_bb->index] = 1;
5191 /* Neither gen nor kill. */
5192 else
5194 visited[pred_bb->index] = 1;
5195 if (pre_expr_reaches_here_p_work (occr_bb, expr, pred_bb, visited))
5196 return 1;
5200 /* All paths have been checked. */
5201 return 0;
5204 /* The wrapper for pre_expr_reaches_here_work that ensures that any
5205 memory allocated for that function is returned. */
5207 static int
5208 pre_expr_reaches_here_p (occr_bb, expr, bb)
5209 basic_block occr_bb;
5210 struct expr *expr;
5211 basic_block bb;
5213 int rval;
5214 char *visited = (char *) xcalloc (last_basic_block, 1);
5216 rval = pre_expr_reaches_here_p_work (occr_bb, expr, bb, visited);
5218 free (visited);
5219 return rval;
5223 /* Given an expr, generate RTL which we can insert at the end of a BB,
5224 or on an edge. Set the block number of any insns generated to
5225 the value of BB. */
5227 static rtx
5228 process_insert_insn (expr)
5229 struct expr *expr;
5231 rtx reg = expr->reaching_reg;
5232 rtx exp = copy_rtx (expr->expr);
5233 rtx pat;
5235 start_sequence ();
5237 /* If the expression is something that's an operand, like a constant,
5238 just copy it to a register. */
5239 if (general_operand (exp, GET_MODE (reg)))
5240 emit_move_insn (reg, exp);
5242 /* Otherwise, make a new insn to compute this expression and make sure the
5243 insn will be recognized (this also adds any needed CLOBBERs). Copy the
5244 expression to make sure we don't have any sharing issues. */
5245 else if (insn_invalid_p (emit_insn (gen_rtx_SET (VOIDmode, reg, exp))))
5246 abort ();
5248 pat = get_insns ();
5249 end_sequence ();
5251 return pat;
5254 /* Add EXPR to the end of basic block BB.
5256 This is used by both the PRE and code hoisting.
5258 For PRE, we want to verify that the expr is either transparent
5259 or locally anticipatable in the target block. This check makes
5260 no sense for code hoisting. */
5262 static void
5263 insert_insn_end_bb (expr, bb, pre)
5264 struct expr *expr;
5265 basic_block bb;
5266 int pre;
5268 rtx insn = bb->end;
5269 rtx new_insn;
5270 rtx reg = expr->reaching_reg;
5271 int regno = REGNO (reg);
5272 rtx pat, pat_end;
5274 pat = process_insert_insn (expr);
5275 if (pat == NULL_RTX || ! INSN_P (pat))
5276 abort ();
5278 pat_end = pat;
5279 while (NEXT_INSN (pat_end) != NULL_RTX)
5280 pat_end = NEXT_INSN (pat_end);
5282 /* If the last insn is a jump, insert EXPR in front [taking care to
5283 handle cc0, etc. properly]. Similary we need to care trapping
5284 instructions in presence of non-call exceptions. */
5286 if (GET_CODE (insn) == JUMP_INSN
5287 || (GET_CODE (insn) == INSN
5288 && (bb->succ->succ_next || (bb->succ->flags & EDGE_ABNORMAL))))
5290 #ifdef HAVE_cc0
5291 rtx note;
5292 #endif
5293 /* It should always be the case that we can put these instructions
5294 anywhere in the basic block with performing PRE optimizations.
5295 Check this. */
5296 if (GET_CODE (insn) == INSN && pre
5297 && !TEST_BIT (antloc[bb->index], expr->bitmap_index)
5298 && !TEST_BIT (transp[bb->index], expr->bitmap_index))
5299 abort ();
5301 /* If this is a jump table, then we can't insert stuff here. Since
5302 we know the previous real insn must be the tablejump, we insert
5303 the new instruction just before the tablejump. */
5304 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
5305 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
5306 insn = prev_real_insn (insn);
5308 #ifdef HAVE_cc0
5309 /* FIXME: 'twould be nice to call prev_cc0_setter here but it aborts
5310 if cc0 isn't set. */
5311 note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
5312 if (note)
5313 insn = XEXP (note, 0);
5314 else
5316 rtx maybe_cc0_setter = prev_nonnote_insn (insn);
5317 if (maybe_cc0_setter
5318 && INSN_P (maybe_cc0_setter)
5319 && sets_cc0_p (PATTERN (maybe_cc0_setter)))
5320 insn = maybe_cc0_setter;
5322 #endif
5323 /* FIXME: What if something in cc0/jump uses value set in new insn? */
5324 new_insn = emit_insn_before (pat, insn);
5327 /* Likewise if the last insn is a call, as will happen in the presence
5328 of exception handling. */
5329 else if (GET_CODE (insn) == CALL_INSN
5330 && (bb->succ->succ_next || (bb->succ->flags & EDGE_ABNORMAL)))
5332 /* Keeping in mind SMALL_REGISTER_CLASSES and parameters in registers,
5333 we search backward and place the instructions before the first
5334 parameter is loaded. Do this for everyone for consistency and a
5335 presumption that we'll get better code elsewhere as well.
5337 It should always be the case that we can put these instructions
5338 anywhere in the basic block with performing PRE optimizations.
5339 Check this. */
5341 if (pre
5342 && !TEST_BIT (antloc[bb->index], expr->bitmap_index)
5343 && !TEST_BIT (transp[bb->index], expr->bitmap_index))
5344 abort ();
5346 /* Since different machines initialize their parameter registers
5347 in different orders, assume nothing. Collect the set of all
5348 parameter registers. */
5349 insn = find_first_parameter_load (insn, bb->head);
5351 /* If we found all the parameter loads, then we want to insert
5352 before the first parameter load.
5354 If we did not find all the parameter loads, then we might have
5355 stopped on the head of the block, which could be a CODE_LABEL.
5356 If we inserted before the CODE_LABEL, then we would be putting
5357 the insn in the wrong basic block. In that case, put the insn
5358 after the CODE_LABEL. Also, respect NOTE_INSN_BASIC_BLOCK. */
5359 while (GET_CODE (insn) == CODE_LABEL
5360 || NOTE_INSN_BASIC_BLOCK_P (insn))
5361 insn = NEXT_INSN (insn);
5363 new_insn = emit_insn_before (pat, insn);
5365 else
5366 new_insn = emit_insn_after (pat, insn);
5368 while (1)
5370 if (INSN_P (pat))
5372 add_label_notes (PATTERN (pat), new_insn);
5373 note_stores (PATTERN (pat), record_set_info, pat);
5375 if (pat == pat_end)
5376 break;
5377 pat = NEXT_INSN (pat);
5380 gcse_create_count++;
5382 if (gcse_file)
5384 fprintf (gcse_file, "PRE/HOIST: end of bb %d, insn %d, ",
5385 bb->index, INSN_UID (new_insn));
5386 fprintf (gcse_file, "copying expression %d to reg %d\n",
5387 expr->bitmap_index, regno);
5391 /* Insert partially redundant expressions on edges in the CFG to make
5392 the expressions fully redundant. */
5394 static int
5395 pre_edge_insert (edge_list, index_map)
5396 struct edge_list *edge_list;
5397 struct expr **index_map;
5399 int e, i, j, num_edges, set_size, did_insert = 0;
5400 sbitmap *inserted;
5402 /* Where PRE_INSERT_MAP is nonzero, we add the expression on that edge
5403 if it reaches any of the deleted expressions. */
5405 set_size = pre_insert_map[0]->size;
5406 num_edges = NUM_EDGES (edge_list);
5407 inserted = sbitmap_vector_alloc (num_edges, expr_hash_table.n_elems);
5408 sbitmap_vector_zero (inserted, num_edges);
5410 for (e = 0; e < num_edges; e++)
5412 int indx;
5413 basic_block bb = INDEX_EDGE_PRED_BB (edge_list, e);
5415 for (i = indx = 0; i < set_size; i++, indx += SBITMAP_ELT_BITS)
5417 SBITMAP_ELT_TYPE insert = pre_insert_map[e]->elms[i];
5419 for (j = indx; insert && j < (int) expr_hash_table.n_elems; j++, insert >>= 1)
5420 if ((insert & 1) != 0 && index_map[j]->reaching_reg != NULL_RTX)
5422 struct expr *expr = index_map[j];
5423 struct occr *occr;
5425 /* Now look at each deleted occurrence of this expression. */
5426 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
5428 if (! occr->deleted_p)
5429 continue;
5431 /* Insert this expression on this edge if if it would
5432 reach the deleted occurrence in BB. */
5433 if (!TEST_BIT (inserted[e], j))
5435 rtx insn;
5436 edge eg = INDEX_EDGE (edge_list, e);
5438 /* We can't insert anything on an abnormal and
5439 critical edge, so we insert the insn at the end of
5440 the previous block. There are several alternatives
5441 detailed in Morgans book P277 (sec 10.5) for
5442 handling this situation. This one is easiest for
5443 now. */
5445 if ((eg->flags & EDGE_ABNORMAL) == EDGE_ABNORMAL)
5446 insert_insn_end_bb (index_map[j], bb, 0);
5447 else
5449 insn = process_insert_insn (index_map[j]);
5450 insert_insn_on_edge (insn, eg);
5453 if (gcse_file)
5455 fprintf (gcse_file, "PRE/HOIST: edge (%d,%d), ",
5456 bb->index,
5457 INDEX_EDGE_SUCC_BB (edge_list, e)->index);
5458 fprintf (gcse_file, "copy expression %d\n",
5459 expr->bitmap_index);
5462 update_ld_motion_stores (expr);
5463 SET_BIT (inserted[e], j);
5464 did_insert = 1;
5465 gcse_create_count++;
5472 sbitmap_vector_free (inserted);
5473 return did_insert;
5476 /* Copy the result of INSN to REG. INDX is the expression number. */
5478 static void
5479 pre_insert_copy_insn (expr, insn)
5480 struct expr *expr;
5481 rtx insn;
5483 rtx reg = expr->reaching_reg;
5484 int regno = REGNO (reg);
5485 int indx = expr->bitmap_index;
5486 rtx set = single_set (insn);
5487 rtx new_insn;
5489 if (!set)
5490 abort ();
5492 new_insn = emit_insn_after (gen_move_insn (reg, copy_rtx (SET_DEST (set))), insn);
5494 /* Keep register set table up to date. */
5495 record_one_set (regno, new_insn);
5497 gcse_create_count++;
5499 if (gcse_file)
5500 fprintf (gcse_file,
5501 "PRE: bb %d, insn %d, copy expression %d in insn %d to reg %d\n",
5502 BLOCK_NUM (insn), INSN_UID (new_insn), indx,
5503 INSN_UID (insn), regno);
5504 update_ld_motion_stores (expr);
5507 /* Copy available expressions that reach the redundant expression
5508 to `reaching_reg'. */
5510 static void
5511 pre_insert_copies ()
5513 unsigned int i;
5514 struct expr *expr;
5515 struct occr *occr;
5516 struct occr *avail;
5518 /* For each available expression in the table, copy the result to
5519 `reaching_reg' if the expression reaches a deleted one.
5521 ??? The current algorithm is rather brute force.
5522 Need to do some profiling. */
5524 for (i = 0; i < expr_hash_table.size; i++)
5525 for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
5527 /* If the basic block isn't reachable, PPOUT will be TRUE. However,
5528 we don't want to insert a copy here because the expression may not
5529 really be redundant. So only insert an insn if the expression was
5530 deleted. This test also avoids further processing if the
5531 expression wasn't deleted anywhere. */
5532 if (expr->reaching_reg == NULL)
5533 continue;
5535 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
5537 if (! occr->deleted_p)
5538 continue;
5540 for (avail = expr->avail_occr; avail != NULL; avail = avail->next)
5542 rtx insn = avail->insn;
5544 /* No need to handle this one if handled already. */
5545 if (avail->copied_p)
5546 continue;
5548 /* Don't handle this one if it's a redundant one. */
5549 if (TEST_BIT (pre_redundant_insns, INSN_CUID (insn)))
5550 continue;
5552 /* Or if the expression doesn't reach the deleted one. */
5553 if (! pre_expr_reaches_here_p (BLOCK_FOR_INSN (avail->insn),
5554 expr,
5555 BLOCK_FOR_INSN (occr->insn)))
5556 continue;
5558 /* Copy the result of avail to reaching_reg. */
5559 pre_insert_copy_insn (expr, insn);
5560 avail->copied_p = 1;
5566 /* Emit move from SRC to DEST noting the equivalence with expression computed
5567 in INSN. */
5568 static rtx
5569 gcse_emit_move_after (src, dest, insn)
5570 rtx src, dest, insn;
5572 rtx new;
5573 rtx set = single_set (insn), set2;
5574 rtx note;
5575 rtx eqv;
5577 /* This should never fail since we're creating a reg->reg copy
5578 we've verified to be valid. */
5580 new = emit_insn_after (gen_move_insn (dest, src), insn);
5582 /* Note the equivalence for local CSE pass. */
5583 set2 = single_set (new);
5584 if (!set2 || !rtx_equal_p (SET_DEST (set2), dest))
5585 return new;
5586 if ((note = find_reg_equal_equiv_note (insn)))
5587 eqv = XEXP (note, 0);
5588 else
5589 eqv = SET_SRC (set);
5591 set_unique_reg_note (new, REG_EQUAL, copy_insn_1 (eqv));
5593 return new;
5596 /* Delete redundant computations.
5597 Deletion is done by changing the insn to copy the `reaching_reg' of
5598 the expression into the result of the SET. It is left to later passes
5599 (cprop, cse2, flow, combine, regmove) to propagate the copy or eliminate it.
5601 Returns nonzero if a change is made. */
5603 static int
5604 pre_delete ()
5606 unsigned int i;
5607 int changed;
5608 struct expr *expr;
5609 struct occr *occr;
5611 changed = 0;
5612 for (i = 0; i < expr_hash_table.size; i++)
5613 for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
5615 int indx = expr->bitmap_index;
5617 /* We only need to search antic_occr since we require
5618 ANTLOC != 0. */
5620 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
5622 rtx insn = occr->insn;
5623 rtx set;
5624 basic_block bb = BLOCK_FOR_INSN (insn);
5626 if (TEST_BIT (pre_delete_map[bb->index], indx))
5628 set = single_set (insn);
5629 if (! set)
5630 abort ();
5632 /* Create a pseudo-reg to store the result of reaching
5633 expressions into. Get the mode for the new pseudo from
5634 the mode of the original destination pseudo. */
5635 if (expr->reaching_reg == NULL)
5636 expr->reaching_reg
5637 = gen_reg_rtx (GET_MODE (SET_DEST (set)));
5639 gcse_emit_move_after (expr->reaching_reg, SET_DEST (set), insn);
5640 delete_insn (insn);
5641 occr->deleted_p = 1;
5642 SET_BIT (pre_redundant_insns, INSN_CUID (insn));
5643 changed = 1;
5644 gcse_subst_count++;
5646 if (gcse_file)
5648 fprintf (gcse_file,
5649 "PRE: redundant insn %d (expression %d) in ",
5650 INSN_UID (insn), indx);
5651 fprintf (gcse_file, "bb %d, reaching reg is %d\n",
5652 bb->index, REGNO (expr->reaching_reg));
5658 return changed;
5661 /* Perform GCSE optimizations using PRE.
5662 This is called by one_pre_gcse_pass after all the dataflow analysis
5663 has been done.
5665 This is based on the original Morel-Renvoise paper Fred Chow's thesis, and
5666 lazy code motion from Knoop, Ruthing and Steffen as described in Advanced
5667 Compiler Design and Implementation.
5669 ??? A new pseudo reg is created to hold the reaching expression. The nice
5670 thing about the classical approach is that it would try to use an existing
5671 reg. If the register can't be adequately optimized [i.e. we introduce
5672 reload problems], one could add a pass here to propagate the new register
5673 through the block.
5675 ??? We don't handle single sets in PARALLELs because we're [currently] not
5676 able to copy the rest of the parallel when we insert copies to create full
5677 redundancies from partial redundancies. However, there's no reason why we
5678 can't handle PARALLELs in the cases where there are no partial
5679 redundancies. */
5681 static int
5682 pre_gcse ()
5684 unsigned int i;
5685 int did_insert, changed;
5686 struct expr **index_map;
5687 struct expr *expr;
5689 /* Compute a mapping from expression number (`bitmap_index') to
5690 hash table entry. */
5692 index_map = (struct expr **) xcalloc (expr_hash_table.n_elems, sizeof (struct expr *));
5693 for (i = 0; i < expr_hash_table.size; i++)
5694 for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
5695 index_map[expr->bitmap_index] = expr;
5697 /* Reset bitmap used to track which insns are redundant. */
5698 pre_redundant_insns = sbitmap_alloc (max_cuid);
5699 sbitmap_zero (pre_redundant_insns);
5701 /* Delete the redundant insns first so that
5702 - we know what register to use for the new insns and for the other
5703 ones with reaching expressions
5704 - we know which insns are redundant when we go to create copies */
5706 changed = pre_delete ();
5708 did_insert = pre_edge_insert (edge_list, index_map);
5710 /* In other places with reaching expressions, copy the expression to the
5711 specially allocated pseudo-reg that reaches the redundant expr. */
5712 pre_insert_copies ();
5713 if (did_insert)
5715 commit_edge_insertions ();
5716 changed = 1;
5719 free (index_map);
5720 sbitmap_free (pre_redundant_insns);
5721 return changed;
5724 /* Top level routine to perform one PRE GCSE pass.
5726 Return nonzero if a change was made. */
5728 static int
5729 one_pre_gcse_pass (pass)
5730 int pass;
5732 int changed = 0;
5734 gcse_subst_count = 0;
5735 gcse_create_count = 0;
5737 alloc_hash_table (max_cuid, &expr_hash_table, 0);
5738 add_noreturn_fake_exit_edges ();
5739 if (flag_gcse_lm)
5740 compute_ld_motion_mems ();
5742 compute_hash_table (&expr_hash_table);
5743 trim_ld_motion_mems ();
5744 if (gcse_file)
5745 dump_hash_table (gcse_file, "Expression", &expr_hash_table);
5747 if (expr_hash_table.n_elems > 0)
5749 alloc_pre_mem (last_basic_block, expr_hash_table.n_elems);
5750 compute_pre_data ();
5751 changed |= pre_gcse ();
5752 free_edge_list (edge_list);
5753 free_pre_mem ();
5756 free_ldst_mems ();
5757 remove_fake_edges ();
5758 free_hash_table (&expr_hash_table);
5760 if (gcse_file)
5762 fprintf (gcse_file, "\nPRE GCSE of %s, pass %d: %d bytes needed, ",
5763 current_function_name, pass, bytes_used);
5764 fprintf (gcse_file, "%d substs, %d insns created\n",
5765 gcse_subst_count, gcse_create_count);
5768 return changed;
5771 /* If X contains any LABEL_REF's, add REG_LABEL notes for them to INSN.
5772 If notes are added to an insn which references a CODE_LABEL, the
5773 LABEL_NUSES count is incremented. We have to add REG_LABEL notes,
5774 because the following loop optimization pass requires them. */
5776 /* ??? This is very similar to the loop.c add_label_notes function. We
5777 could probably share code here. */
5779 /* ??? If there was a jump optimization pass after gcse and before loop,
5780 then we would not need to do this here, because jump would add the
5781 necessary REG_LABEL notes. */
5783 static void
5784 add_label_notes (x, insn)
5785 rtx x;
5786 rtx insn;
5788 enum rtx_code code = GET_CODE (x);
5789 int i, j;
5790 const char *fmt;
5792 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
5794 /* This code used to ignore labels that referred to dispatch tables to
5795 avoid flow generating (slighly) worse code.
5797 We no longer ignore such label references (see LABEL_REF handling in
5798 mark_jump_label for additional information). */
5800 REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_LABEL, XEXP (x, 0),
5801 REG_NOTES (insn));
5802 if (LABEL_P (XEXP (x, 0)))
5803 LABEL_NUSES (XEXP (x, 0))++;
5804 return;
5807 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
5809 if (fmt[i] == 'e')
5810 add_label_notes (XEXP (x, i), insn);
5811 else if (fmt[i] == 'E')
5812 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
5813 add_label_notes (XVECEXP (x, i, j), insn);
5817 /* Compute transparent outgoing information for each block.
5819 An expression is transparent to an edge unless it is killed by
5820 the edge itself. This can only happen with abnormal control flow,
5821 when the edge is traversed through a call. This happens with
5822 non-local labels and exceptions.
5824 This would not be necessary if we split the edge. While this is
5825 normally impossible for abnormal critical edges, with some effort
5826 it should be possible with exception handling, since we still have
5827 control over which handler should be invoked. But due to increased
5828 EH table sizes, this may not be worthwhile. */
5830 static void
5831 compute_transpout ()
5833 basic_block bb;
5834 unsigned int i;
5835 struct expr *expr;
5837 sbitmap_vector_ones (transpout, last_basic_block);
5839 FOR_EACH_BB (bb)
5841 /* Note that flow inserted a nop a the end of basic blocks that
5842 end in call instructions for reasons other than abnormal
5843 control flow. */
5844 if (GET_CODE (bb->end) != CALL_INSN)
5845 continue;
5847 for (i = 0; i < expr_hash_table.size; i++)
5848 for (expr = expr_hash_table.table[i]; expr ; expr = expr->next_same_hash)
5849 if (GET_CODE (expr->expr) == MEM)
5851 if (GET_CODE (XEXP (expr->expr, 0)) == SYMBOL_REF
5852 && CONSTANT_POOL_ADDRESS_P (XEXP (expr->expr, 0)))
5853 continue;
5855 /* ??? Optimally, we would use interprocedural alias
5856 analysis to determine if this mem is actually killed
5857 by this call. */
5858 RESET_BIT (transpout[bb->index], expr->bitmap_index);
5863 /* Removal of useless null pointer checks */
5865 /* Called via note_stores. X is set by SETTER. If X is a register we must
5866 invalidate nonnull_local and set nonnull_killed. DATA is really a
5867 `null_pointer_info *'.
5869 We ignore hard registers. */
5871 static void
5872 invalidate_nonnull_info (x, setter, data)
5873 rtx x;
5874 rtx setter ATTRIBUTE_UNUSED;
5875 void *data;
5877 unsigned int regno;
5878 struct null_pointer_info *npi = (struct null_pointer_info *) data;
5880 while (GET_CODE (x) == SUBREG)
5881 x = SUBREG_REG (x);
5883 /* Ignore anything that is not a register or is a hard register. */
5884 if (GET_CODE (x) != REG
5885 || REGNO (x) < npi->min_reg
5886 || REGNO (x) >= npi->max_reg)
5887 return;
5889 regno = REGNO (x) - npi->min_reg;
5891 RESET_BIT (npi->nonnull_local[npi->current_block->index], regno);
5892 SET_BIT (npi->nonnull_killed[npi->current_block->index], regno);
5895 /* Do null-pointer check elimination for the registers indicated in
5896 NPI. NONNULL_AVIN and NONNULL_AVOUT are pre-allocated sbitmaps;
5897 they are not our responsibility to free. */
5899 static int
5900 delete_null_pointer_checks_1 (block_reg, nonnull_avin,
5901 nonnull_avout, npi)
5902 unsigned int *block_reg;
5903 sbitmap *nonnull_avin;
5904 sbitmap *nonnull_avout;
5905 struct null_pointer_info *npi;
5907 basic_block bb, current_block;
5908 sbitmap *nonnull_local = npi->nonnull_local;
5909 sbitmap *nonnull_killed = npi->nonnull_killed;
5910 int something_changed = 0;
5912 /* Compute local properties, nonnull and killed. A register will have
5913 the nonnull property if at the end of the current block its value is
5914 known to be nonnull. The killed property indicates that somewhere in
5915 the block any information we had about the register is killed.
5917 Note that a register can have both properties in a single block. That
5918 indicates that it's killed, then later in the block a new value is
5919 computed. */
5920 sbitmap_vector_zero (nonnull_local, last_basic_block);
5921 sbitmap_vector_zero (nonnull_killed, last_basic_block);
5923 FOR_EACH_BB (current_block)
5925 rtx insn, stop_insn;
5927 /* Set the current block for invalidate_nonnull_info. */
5928 npi->current_block = current_block;
5930 /* Scan each insn in the basic block looking for memory references and
5931 register sets. */
5932 stop_insn = NEXT_INSN (current_block->end);
5933 for (insn = current_block->head;
5934 insn != stop_insn;
5935 insn = NEXT_INSN (insn))
5937 rtx set;
5938 rtx reg;
5940 /* Ignore anything that is not a normal insn. */
5941 if (! INSN_P (insn))
5942 continue;
5944 /* Basically ignore anything that is not a simple SET. We do have
5945 to make sure to invalidate nonnull_local and set nonnull_killed
5946 for such insns though. */
5947 set = single_set (insn);
5948 if (!set)
5950 note_stores (PATTERN (insn), invalidate_nonnull_info, npi);
5951 continue;
5954 /* See if we've got a usable memory load. We handle it first
5955 in case it uses its address register as a dest (which kills
5956 the nonnull property). */
5957 if (GET_CODE (SET_SRC (set)) == MEM
5958 && GET_CODE ((reg = XEXP (SET_SRC (set), 0))) == REG
5959 && REGNO (reg) >= npi->min_reg
5960 && REGNO (reg) < npi->max_reg)
5961 SET_BIT (nonnull_local[current_block->index],
5962 REGNO (reg) - npi->min_reg);
5964 /* Now invalidate stuff clobbered by this insn. */
5965 note_stores (PATTERN (insn), invalidate_nonnull_info, npi);
5967 /* And handle stores, we do these last since any sets in INSN can
5968 not kill the nonnull property if it is derived from a MEM
5969 appearing in a SET_DEST. */
5970 if (GET_CODE (SET_DEST (set)) == MEM
5971 && GET_CODE ((reg = XEXP (SET_DEST (set), 0))) == REG
5972 && REGNO (reg) >= npi->min_reg
5973 && REGNO (reg) < npi->max_reg)
5974 SET_BIT (nonnull_local[current_block->index],
5975 REGNO (reg) - npi->min_reg);
5979 /* Now compute global properties based on the local properties. This
5980 is a classic global availability algorithm. */
5981 compute_available (nonnull_local, nonnull_killed,
5982 nonnull_avout, nonnull_avin);
5984 /* Now look at each bb and see if it ends with a compare of a value
5985 against zero. */
5986 FOR_EACH_BB (bb)
5988 rtx last_insn = bb->end;
5989 rtx condition, earliest;
5990 int compare_and_branch;
5992 /* Since MIN_REG is always at least FIRST_PSEUDO_REGISTER, and
5993 since BLOCK_REG[BB] is zero if this block did not end with a
5994 comparison against zero, this condition works. */
5995 if (block_reg[bb->index] < npi->min_reg
5996 || block_reg[bb->index] >= npi->max_reg)
5997 continue;
5999 /* LAST_INSN is a conditional jump. Get its condition. */
6000 condition = get_condition (last_insn, &earliest);
6002 /* If we can't determine the condition then skip. */
6003 if (! condition)
6004 continue;
6006 /* Is the register known to have a nonzero value? */
6007 if (!TEST_BIT (nonnull_avout[bb->index], block_reg[bb->index] - npi->min_reg))
6008 continue;
6010 /* Try to compute whether the compare/branch at the loop end is one or
6011 two instructions. */
6012 if (earliest == last_insn)
6013 compare_and_branch = 1;
6014 else if (earliest == prev_nonnote_insn (last_insn))
6015 compare_and_branch = 2;
6016 else
6017 continue;
6019 /* We know the register in this comparison is nonnull at exit from
6020 this block. We can optimize this comparison. */
6021 if (GET_CODE (condition) == NE)
6023 rtx new_jump;
6025 new_jump = emit_jump_insn_after (gen_jump (JUMP_LABEL (last_insn)),
6026 last_insn);
6027 JUMP_LABEL (new_jump) = JUMP_LABEL (last_insn);
6028 LABEL_NUSES (JUMP_LABEL (new_jump))++;
6029 emit_barrier_after (new_jump);
6032 something_changed = 1;
6033 delete_insn (last_insn);
6034 if (compare_and_branch == 2)
6035 delete_insn (earliest);
6036 purge_dead_edges (bb);
6038 /* Don't check this block again. (Note that BLOCK_END is
6039 invalid here; we deleted the last instruction in the
6040 block.) */
6041 block_reg[bb->index] = 0;
6044 return something_changed;
6047 /* Find EQ/NE comparisons against zero which can be (indirectly) evaluated
6048 at compile time.
6050 This is conceptually similar to global constant/copy propagation and
6051 classic global CSE (it even uses the same dataflow equations as cprop).
6053 If a register is used as memory address with the form (mem (reg)), then we
6054 know that REG can not be zero at that point in the program. Any instruction
6055 which sets REG "kills" this property.
6057 So, if every path leading to a conditional branch has an available memory
6058 reference of that form, then we know the register can not have the value
6059 zero at the conditional branch.
6061 So we merely need to compute the local properties and propagate that data
6062 around the cfg, then optimize where possible.
6064 We run this pass two times. Once before CSE, then again after CSE. This
6065 has proven to be the most profitable approach. It is rare for new
6066 optimization opportunities of this nature to appear after the first CSE
6067 pass.
6069 This could probably be integrated with global cprop with a little work. */
6072 delete_null_pointer_checks (f)
6073 rtx f ATTRIBUTE_UNUSED;
6075 sbitmap *nonnull_avin, *nonnull_avout;
6076 unsigned int *block_reg;
6077 basic_block bb;
6078 int reg;
6079 int regs_per_pass;
6080 int max_reg;
6081 struct null_pointer_info npi;
6082 int something_changed = 0;
6084 /* If we have only a single block, then there's nothing to do. */
6085 if (n_basic_blocks <= 1)
6086 return 0;
6088 /* Trying to perform global optimizations on flow graphs which have
6089 a high connectivity will take a long time and is unlikely to be
6090 particularly useful.
6092 In normal circumstances a cfg should have about twice as many edges
6093 as blocks. But we do not want to punish small functions which have
6094 a couple switch statements. So we require a relatively large number
6095 of basic blocks and the ratio of edges to blocks to be high. */
6096 if (n_basic_blocks > 1000 && n_edges / n_basic_blocks >= 20)
6097 return 0;
6099 /* We need four bitmaps, each with a bit for each register in each
6100 basic block. */
6101 max_reg = max_reg_num ();
6102 regs_per_pass = get_bitmap_width (4, last_basic_block, max_reg);
6104 /* Allocate bitmaps to hold local and global properties. */
6105 npi.nonnull_local = sbitmap_vector_alloc (last_basic_block, regs_per_pass);
6106 npi.nonnull_killed = sbitmap_vector_alloc (last_basic_block, regs_per_pass);
6107 nonnull_avin = sbitmap_vector_alloc (last_basic_block, regs_per_pass);
6108 nonnull_avout = sbitmap_vector_alloc (last_basic_block, regs_per_pass);
6110 /* Go through the basic blocks, seeing whether or not each block
6111 ends with a conditional branch whose condition is a comparison
6112 against zero. Record the register compared in BLOCK_REG. */
6113 block_reg = (unsigned int *) xcalloc (last_basic_block, sizeof (int));
6114 FOR_EACH_BB (bb)
6116 rtx last_insn = bb->end;
6117 rtx condition, earliest, reg;
6119 /* We only want conditional branches. */
6120 if (GET_CODE (last_insn) != JUMP_INSN
6121 || !any_condjump_p (last_insn)
6122 || !onlyjump_p (last_insn))
6123 continue;
6125 /* LAST_INSN is a conditional jump. Get its condition. */
6126 condition = get_condition (last_insn, &earliest);
6128 /* If we were unable to get the condition, or it is not an equality
6129 comparison against zero then there's nothing we can do. */
6130 if (!condition
6131 || (GET_CODE (condition) != NE && GET_CODE (condition) != EQ)
6132 || GET_CODE (XEXP (condition, 1)) != CONST_INT
6133 || (XEXP (condition, 1)
6134 != CONST0_RTX (GET_MODE (XEXP (condition, 0)))))
6135 continue;
6137 /* We must be checking a register against zero. */
6138 reg = XEXP (condition, 0);
6139 if (GET_CODE (reg) != REG)
6140 continue;
6142 block_reg[bb->index] = REGNO (reg);
6145 /* Go through the algorithm for each block of registers. */
6146 for (reg = FIRST_PSEUDO_REGISTER; reg < max_reg; reg += regs_per_pass)
6148 npi.min_reg = reg;
6149 npi.max_reg = MIN (reg + regs_per_pass, max_reg);
6150 something_changed |= delete_null_pointer_checks_1 (block_reg,
6151 nonnull_avin,
6152 nonnull_avout,
6153 &npi);
6156 /* Free the table of registers compared at the end of every block. */
6157 free (block_reg);
6159 /* Free bitmaps. */
6160 sbitmap_vector_free (npi.nonnull_local);
6161 sbitmap_vector_free (npi.nonnull_killed);
6162 sbitmap_vector_free (nonnull_avin);
6163 sbitmap_vector_free (nonnull_avout);
6165 return something_changed;
6168 /* Code Hoisting variables and subroutines. */
6170 /* Very busy expressions. */
6171 static sbitmap *hoist_vbein;
6172 static sbitmap *hoist_vbeout;
6174 /* Hoistable expressions. */
6175 static sbitmap *hoist_exprs;
6177 /* Dominator bitmaps. */
6178 dominance_info dominators;
6180 /* ??? We could compute post dominators and run this algorithm in
6181 reverse to perform tail merging, doing so would probably be
6182 more effective than the tail merging code in jump.c.
6184 It's unclear if tail merging could be run in parallel with
6185 code hoisting. It would be nice. */
6187 /* Allocate vars used for code hoisting analysis. */
6189 static void
6190 alloc_code_hoist_mem (n_blocks, n_exprs)
6191 int n_blocks, n_exprs;
6193 antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
6194 transp = sbitmap_vector_alloc (n_blocks, n_exprs);
6195 comp = sbitmap_vector_alloc (n_blocks, n_exprs);
6197 hoist_vbein = sbitmap_vector_alloc (n_blocks, n_exprs);
6198 hoist_vbeout = sbitmap_vector_alloc (n_blocks, n_exprs);
6199 hoist_exprs = sbitmap_vector_alloc (n_blocks, n_exprs);
6200 transpout = sbitmap_vector_alloc (n_blocks, n_exprs);
6203 /* Free vars used for code hoisting analysis. */
6205 static void
6206 free_code_hoist_mem ()
6208 sbitmap_vector_free (antloc);
6209 sbitmap_vector_free (transp);
6210 sbitmap_vector_free (comp);
6212 sbitmap_vector_free (hoist_vbein);
6213 sbitmap_vector_free (hoist_vbeout);
6214 sbitmap_vector_free (hoist_exprs);
6215 sbitmap_vector_free (transpout);
6217 free_dominance_info (dominators);
6220 /* Compute the very busy expressions at entry/exit from each block.
6222 An expression is very busy if all paths from a given point
6223 compute the expression. */
6225 static void
6226 compute_code_hoist_vbeinout ()
6228 int changed, passes;
6229 basic_block bb;
6231 sbitmap_vector_zero (hoist_vbeout, last_basic_block);
6232 sbitmap_vector_zero (hoist_vbein, last_basic_block);
6234 passes = 0;
6235 changed = 1;
6237 while (changed)
6239 changed = 0;
6241 /* We scan the blocks in the reverse order to speed up
6242 the convergence. */
6243 FOR_EACH_BB_REVERSE (bb)
6245 changed |= sbitmap_a_or_b_and_c_cg (hoist_vbein[bb->index], antloc[bb->index],
6246 hoist_vbeout[bb->index], transp[bb->index]);
6247 if (bb->next_bb != EXIT_BLOCK_PTR)
6248 sbitmap_intersection_of_succs (hoist_vbeout[bb->index], hoist_vbein, bb->index);
6251 passes++;
6254 if (gcse_file)
6255 fprintf (gcse_file, "hoisting vbeinout computation: %d passes\n", passes);
6258 /* Top level routine to do the dataflow analysis needed by code hoisting. */
6260 static void
6261 compute_code_hoist_data ()
6263 compute_local_properties (transp, comp, antloc, &expr_hash_table);
6264 compute_transpout ();
6265 compute_code_hoist_vbeinout ();
6266 dominators = calculate_dominance_info (CDI_DOMINATORS);
6267 if (gcse_file)
6268 fprintf (gcse_file, "\n");
6271 /* Determine if the expression identified by EXPR_INDEX would
6272 reach BB unimpared if it was placed at the end of EXPR_BB.
6274 It's unclear exactly what Muchnick meant by "unimpared". It seems
6275 to me that the expression must either be computed or transparent in
6276 *every* block in the path(s) from EXPR_BB to BB. Any other definition
6277 would allow the expression to be hoisted out of loops, even if
6278 the expression wasn't a loop invariant.
6280 Contrast this to reachability for PRE where an expression is
6281 considered reachable if *any* path reaches instead of *all*
6282 paths. */
6284 static int
6285 hoist_expr_reaches_here_p (expr_bb, expr_index, bb, visited)
6286 basic_block expr_bb;
6287 int expr_index;
6288 basic_block bb;
6289 char *visited;
6291 edge pred;
6292 int visited_allocated_locally = 0;
6295 if (visited == NULL)
6297 visited_allocated_locally = 1;
6298 visited = xcalloc (last_basic_block, 1);
6301 for (pred = bb->pred; pred != NULL; pred = pred->pred_next)
6303 basic_block pred_bb = pred->src;
6305 if (pred->src == ENTRY_BLOCK_PTR)
6306 break;
6307 else if (pred_bb == expr_bb)
6308 continue;
6309 else if (visited[pred_bb->index])
6310 continue;
6312 /* Does this predecessor generate this expression? */
6313 else if (TEST_BIT (comp[pred_bb->index], expr_index))
6314 break;
6315 else if (! TEST_BIT (transp[pred_bb->index], expr_index))
6316 break;
6318 /* Not killed. */
6319 else
6321 visited[pred_bb->index] = 1;
6322 if (! hoist_expr_reaches_here_p (expr_bb, expr_index,
6323 pred_bb, visited))
6324 break;
6327 if (visited_allocated_locally)
6328 free (visited);
6330 return (pred == NULL);
6333 /* Actually perform code hoisting. */
6335 static void
6336 hoist_code ()
6338 basic_block bb, dominated;
6339 basic_block *domby;
6340 unsigned int domby_len;
6341 unsigned int i,j;
6342 struct expr **index_map;
6343 struct expr *expr;
6345 sbitmap_vector_zero (hoist_exprs, last_basic_block);
6347 /* Compute a mapping from expression number (`bitmap_index') to
6348 hash table entry. */
6350 index_map = (struct expr **) xcalloc (expr_hash_table.n_elems, sizeof (struct expr *));
6351 for (i = 0; i < expr_hash_table.size; i++)
6352 for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
6353 index_map[expr->bitmap_index] = expr;
6355 /* Walk over each basic block looking for potentially hoistable
6356 expressions, nothing gets hoisted from the entry block. */
6357 FOR_EACH_BB (bb)
6359 int found = 0;
6360 int insn_inserted_p;
6362 domby_len = get_dominated_by (dominators, bb, &domby);
6363 /* Examine each expression that is very busy at the exit of this
6364 block. These are the potentially hoistable expressions. */
6365 for (i = 0; i < hoist_vbeout[bb->index]->n_bits; i++)
6367 int hoistable = 0;
6369 if (TEST_BIT (hoist_vbeout[bb->index], i)
6370 && TEST_BIT (transpout[bb->index], i))
6372 /* We've found a potentially hoistable expression, now
6373 we look at every block BB dominates to see if it
6374 computes the expression. */
6375 for (j = 0; j < domby_len; j++)
6377 dominated = domby[j];
6378 /* Ignore self dominance. */
6379 if (bb == dominated)
6380 continue;
6381 /* We've found a dominated block, now see if it computes
6382 the busy expression and whether or not moving that
6383 expression to the "beginning" of that block is safe. */
6384 if (!TEST_BIT (antloc[dominated->index], i))
6385 continue;
6387 /* Note if the expression would reach the dominated block
6388 unimpared if it was placed at the end of BB.
6390 Keep track of how many times this expression is hoistable
6391 from a dominated block into BB. */
6392 if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
6393 hoistable++;
6396 /* If we found more than one hoistable occurrence of this
6397 expression, then note it in the bitmap of expressions to
6398 hoist. It makes no sense to hoist things which are computed
6399 in only one BB, and doing so tends to pessimize register
6400 allocation. One could increase this value to try harder
6401 to avoid any possible code expansion due to register
6402 allocation issues; however experiments have shown that
6403 the vast majority of hoistable expressions are only movable
6404 from two successors, so raising this threshhold is likely
6405 to nullify any benefit we get from code hoisting. */
6406 if (hoistable > 1)
6408 SET_BIT (hoist_exprs[bb->index], i);
6409 found = 1;
6413 /* If we found nothing to hoist, then quit now. */
6414 if (! found)
6416 free (domby);
6417 continue;
6420 /* Loop over all the hoistable expressions. */
6421 for (i = 0; i < hoist_exprs[bb->index]->n_bits; i++)
6423 /* We want to insert the expression into BB only once, so
6424 note when we've inserted it. */
6425 insn_inserted_p = 0;
6427 /* These tests should be the same as the tests above. */
6428 if (TEST_BIT (hoist_vbeout[bb->index], i))
6430 /* We've found a potentially hoistable expression, now
6431 we look at every block BB dominates to see if it
6432 computes the expression. */
6433 for (j = 0; j < domby_len; j++)
6435 dominated = domby[j];
6436 /* Ignore self dominance. */
6437 if (bb == dominated)
6438 continue;
6440 /* We've found a dominated block, now see if it computes
6441 the busy expression and whether or not moving that
6442 expression to the "beginning" of that block is safe. */
6443 if (!TEST_BIT (antloc[dominated->index], i))
6444 continue;
6446 /* The expression is computed in the dominated block and
6447 it would be safe to compute it at the start of the
6448 dominated block. Now we have to determine if the
6449 expression would reach the dominated block if it was
6450 placed at the end of BB. */
6451 if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
6453 struct expr *expr = index_map[i];
6454 struct occr *occr = expr->antic_occr;
6455 rtx insn;
6456 rtx set;
6458 /* Find the right occurrence of this expression. */
6459 while (BLOCK_FOR_INSN (occr->insn) != dominated && occr)
6460 occr = occr->next;
6462 /* Should never happen. */
6463 if (!occr)
6464 abort ();
6466 insn = occr->insn;
6468 set = single_set (insn);
6469 if (! set)
6470 abort ();
6472 /* Create a pseudo-reg to store the result of reaching
6473 expressions into. Get the mode for the new pseudo
6474 from the mode of the original destination pseudo. */
6475 if (expr->reaching_reg == NULL)
6476 expr->reaching_reg
6477 = gen_reg_rtx (GET_MODE (SET_DEST (set)));
6479 gcse_emit_move_after (expr->reaching_reg, SET_DEST (set), insn);
6480 delete_insn (insn);
6481 occr->deleted_p = 1;
6482 if (!insn_inserted_p)
6484 insert_insn_end_bb (index_map[i], bb, 0);
6485 insn_inserted_p = 1;
6491 free (domby);
6494 free (index_map);
6497 /* Top level routine to perform one code hoisting (aka unification) pass
6499 Return nonzero if a change was made. */
6501 static int
6502 one_code_hoisting_pass ()
6504 int changed = 0;
6506 alloc_hash_table (max_cuid, &expr_hash_table, 0);
6507 compute_hash_table (&expr_hash_table);
6508 if (gcse_file)
6509 dump_hash_table (gcse_file, "Code Hosting Expressions", &expr_hash_table);
6511 if (expr_hash_table.n_elems > 0)
6513 alloc_code_hoist_mem (last_basic_block, expr_hash_table.n_elems);
6514 compute_code_hoist_data ();
6515 hoist_code ();
6516 free_code_hoist_mem ();
6519 free_hash_table (&expr_hash_table);
6521 return changed;
6524 /* Here we provide the things required to do store motion towards
6525 the exit. In order for this to be effective, gcse also needed to
6526 be taught how to move a load when it is kill only by a store to itself.
6528 int i;
6529 float a[10];
6531 void foo(float scale)
6533 for (i=0; i<10; i++)
6534 a[i] *= scale;
6537 'i' is both loaded and stored to in the loop. Normally, gcse cannot move
6538 the load out since its live around the loop, and stored at the bottom
6539 of the loop.
6541 The 'Load Motion' referred to and implemented in this file is
6542 an enhancement to gcse which when using edge based lcm, recognizes
6543 this situation and allows gcse to move the load out of the loop.
6545 Once gcse has hoisted the load, store motion can then push this
6546 load towards the exit, and we end up with no loads or stores of 'i'
6547 in the loop. */
6549 /* This will search the ldst list for a matching expression. If it
6550 doesn't find one, we create one and initialize it. */
6552 static struct ls_expr *
6553 ldst_entry (x)
6554 rtx x;
6556 struct ls_expr * ptr;
6558 for (ptr = first_ls_expr(); ptr != NULL; ptr = next_ls_expr (ptr))
6559 if (expr_equiv_p (ptr->pattern, x))
6560 break;
6562 if (!ptr)
6564 ptr = (struct ls_expr *) xmalloc (sizeof (struct ls_expr));
6566 ptr->next = pre_ldst_mems;
6567 ptr->expr = NULL;
6568 ptr->pattern = x;
6569 ptr->pattern_regs = NULL_RTX;
6570 ptr->loads = NULL_RTX;
6571 ptr->stores = NULL_RTX;
6572 ptr->reaching_reg = NULL_RTX;
6573 ptr->invalid = 0;
6574 ptr->index = 0;
6575 ptr->hash_index = 0;
6576 pre_ldst_mems = ptr;
6579 return ptr;
6582 /* Free up an individual ldst entry. */
6584 static void
6585 free_ldst_entry (ptr)
6586 struct ls_expr * ptr;
6588 free_INSN_LIST_list (& ptr->loads);
6589 free_INSN_LIST_list (& ptr->stores);
6591 free (ptr);
6594 /* Free up all memory associated with the ldst list. */
6596 static void
6597 free_ldst_mems ()
6599 while (pre_ldst_mems)
6601 struct ls_expr * tmp = pre_ldst_mems;
6603 pre_ldst_mems = pre_ldst_mems->next;
6605 free_ldst_entry (tmp);
6608 pre_ldst_mems = NULL;
6611 /* Dump debugging info about the ldst list. */
6613 static void
6614 print_ldst_list (file)
6615 FILE * file;
6617 struct ls_expr * ptr;
6619 fprintf (file, "LDST list: \n");
6621 for (ptr = first_ls_expr(); ptr != NULL; ptr = next_ls_expr (ptr))
6623 fprintf (file, " Pattern (%3d): ", ptr->index);
6625 print_rtl (file, ptr->pattern);
6627 fprintf (file, "\n Loads : ");
6629 if (ptr->loads)
6630 print_rtl (file, ptr->loads);
6631 else
6632 fprintf (file, "(nil)");
6634 fprintf (file, "\n Stores : ");
6636 if (ptr->stores)
6637 print_rtl (file, ptr->stores);
6638 else
6639 fprintf (file, "(nil)");
6641 fprintf (file, "\n\n");
6644 fprintf (file, "\n");
6647 /* Returns 1 if X is in the list of ldst only expressions. */
6649 static struct ls_expr *
6650 find_rtx_in_ldst (x)
6651 rtx x;
6653 struct ls_expr * ptr;
6655 for (ptr = pre_ldst_mems; ptr != NULL; ptr = ptr->next)
6656 if (expr_equiv_p (ptr->pattern, x) && ! ptr->invalid)
6657 return ptr;
6659 return NULL;
6662 /* Assign each element of the list of mems a monotonically increasing value. */
6664 static int
6665 enumerate_ldsts ()
6667 struct ls_expr * ptr;
6668 int n = 0;
6670 for (ptr = pre_ldst_mems; ptr != NULL; ptr = ptr->next)
6671 ptr->index = n++;
6673 return n;
6676 /* Return first item in the list. */
6678 static inline struct ls_expr *
6679 first_ls_expr ()
6681 return pre_ldst_mems;
6684 /* Return the next item in ther list after the specified one. */
6686 static inline struct ls_expr *
6687 next_ls_expr (ptr)
6688 struct ls_expr * ptr;
6690 return ptr->next;
6693 /* Load Motion for loads which only kill themselves. */
6695 /* Return true if x is a simple MEM operation, with no registers or
6696 side effects. These are the types of loads we consider for the
6697 ld_motion list, otherwise we let the usual aliasing take care of it. */
6699 static int
6700 simple_mem (x)
6701 rtx x;
6703 if (GET_CODE (x) != MEM)
6704 return 0;
6706 if (MEM_VOLATILE_P (x))
6707 return 0;
6709 if (GET_MODE (x) == BLKmode)
6710 return 0;
6712 /* If we are handling exceptions, we must be careful with memory references
6713 that may trap. If we are not, the behavior is undefined, so we may just
6714 continue. */
6715 if (flag_non_call_exceptions && may_trap_p (x))
6716 return 0;
6718 if (side_effects_p (x))
6719 return 0;
6721 /* Do not consider function arguments passed on stack. */
6722 if (reg_mentioned_p (stack_pointer_rtx, x))
6723 return 0;
6725 if (flag_float_store && FLOAT_MODE_P (GET_MODE (x)))
6726 return 0;
6728 return 1;
6731 /* Make sure there isn't a buried reference in this pattern anywhere.
6732 If there is, invalidate the entry for it since we're not capable
6733 of fixing it up just yet.. We have to be sure we know about ALL
6734 loads since the aliasing code will allow all entries in the
6735 ld_motion list to not-alias itself. If we miss a load, we will get
6736 the wrong value since gcse might common it and we won't know to
6737 fix it up. */
6739 static void
6740 invalidate_any_buried_refs (x)
6741 rtx x;
6743 const char * fmt;
6744 int i, j;
6745 struct ls_expr * ptr;
6747 /* Invalidate it in the list. */
6748 if (GET_CODE (x) == MEM && simple_mem (x))
6750 ptr = ldst_entry (x);
6751 ptr->invalid = 1;
6754 /* Recursively process the insn. */
6755 fmt = GET_RTX_FORMAT (GET_CODE (x));
6757 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
6759 if (fmt[i] == 'e')
6760 invalidate_any_buried_refs (XEXP (x, i));
6761 else if (fmt[i] == 'E')
6762 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
6763 invalidate_any_buried_refs (XVECEXP (x, i, j));
6767 /* Find all the 'simple' MEMs which are used in LOADs and STORES. Simple
6768 being defined as MEM loads and stores to symbols, with no
6769 side effects and no registers in the expression. If there are any
6770 uses/defs which don't match this criteria, it is invalidated and
6771 trimmed out later. */
6773 static void
6774 compute_ld_motion_mems ()
6776 struct ls_expr * ptr;
6777 basic_block bb;
6778 rtx insn;
6780 pre_ldst_mems = NULL;
6782 FOR_EACH_BB (bb)
6784 for (insn = bb->head;
6785 insn && insn != NEXT_INSN (bb->end);
6786 insn = NEXT_INSN (insn))
6788 if (GET_RTX_CLASS (GET_CODE (insn)) == 'i')
6790 if (GET_CODE (PATTERN (insn)) == SET)
6792 rtx src = SET_SRC (PATTERN (insn));
6793 rtx dest = SET_DEST (PATTERN (insn));
6795 /* Check for a simple LOAD... */
6796 if (GET_CODE (src) == MEM && simple_mem (src))
6798 ptr = ldst_entry (src);
6799 if (GET_CODE (dest) == REG)
6800 ptr->loads = alloc_INSN_LIST (insn, ptr->loads);
6801 else
6802 ptr->invalid = 1;
6804 else
6806 /* Make sure there isn't a buried load somewhere. */
6807 invalidate_any_buried_refs (src);
6810 /* Check for stores. Don't worry about aliased ones, they
6811 will block any movement we might do later. We only care
6812 about this exact pattern since those are the only
6813 circumstance that we will ignore the aliasing info. */
6814 if (GET_CODE (dest) == MEM && simple_mem (dest))
6816 ptr = ldst_entry (dest);
6818 if (GET_CODE (src) != MEM
6819 && GET_CODE (src) != ASM_OPERANDS)
6820 ptr->stores = alloc_INSN_LIST (insn, ptr->stores);
6821 else
6822 ptr->invalid = 1;
6825 else
6826 invalidate_any_buried_refs (PATTERN (insn));
6832 /* Remove any references that have been either invalidated or are not in the
6833 expression list for pre gcse. */
6835 static void
6836 trim_ld_motion_mems ()
6838 struct ls_expr * last = NULL;
6839 struct ls_expr * ptr = first_ls_expr ();
6841 while (ptr != NULL)
6843 int del = ptr->invalid;
6844 struct expr * expr = NULL;
6846 /* Delete if entry has been made invalid. */
6847 if (!del)
6849 unsigned int i;
6851 del = 1;
6852 /* Delete if we cannot find this mem in the expression list. */
6853 for (i = 0; i < expr_hash_table.size && del; i++)
6855 for (expr = expr_hash_table.table[i];
6856 expr != NULL;
6857 expr = expr->next_same_hash)
6858 if (expr_equiv_p (expr->expr, ptr->pattern))
6860 del = 0;
6861 break;
6866 if (del)
6868 if (last != NULL)
6870 last->next = ptr->next;
6871 free_ldst_entry (ptr);
6872 ptr = last->next;
6874 else
6876 pre_ldst_mems = pre_ldst_mems->next;
6877 free_ldst_entry (ptr);
6878 ptr = pre_ldst_mems;
6881 else
6883 /* Set the expression field if we are keeping it. */
6884 last = ptr;
6885 ptr->expr = expr;
6886 ptr = ptr->next;
6890 /* Show the world what we've found. */
6891 if (gcse_file && pre_ldst_mems != NULL)
6892 print_ldst_list (gcse_file);
6895 /* This routine will take an expression which we are replacing with
6896 a reaching register, and update any stores that are needed if
6897 that expression is in the ld_motion list. Stores are updated by
6898 copying their SRC to the reaching register, and then storeing
6899 the reaching register into the store location. These keeps the
6900 correct value in the reaching register for the loads. */
6902 static void
6903 update_ld_motion_stores (expr)
6904 struct expr * expr;
6906 struct ls_expr * mem_ptr;
6908 if ((mem_ptr = find_rtx_in_ldst (expr->expr)))
6910 /* We can try to find just the REACHED stores, but is shouldn't
6911 matter to set the reaching reg everywhere... some might be
6912 dead and should be eliminated later. */
6914 /* We replace SET mem = expr with
6915 SET reg = expr
6916 SET mem = reg , where reg is the
6917 reaching reg used in the load. */
6918 rtx list = mem_ptr->stores;
6920 for ( ; list != NULL_RTX; list = XEXP (list, 1))
6922 rtx insn = XEXP (list, 0);
6923 rtx pat = PATTERN (insn);
6924 rtx src = SET_SRC (pat);
6925 rtx reg = expr->reaching_reg;
6926 rtx copy, new;
6928 /* If we've already copied it, continue. */
6929 if (expr->reaching_reg == src)
6930 continue;
6932 if (gcse_file)
6934 fprintf (gcse_file, "PRE: store updated with reaching reg ");
6935 print_rtl (gcse_file, expr->reaching_reg);
6936 fprintf (gcse_file, ":\n ");
6937 print_inline_rtx (gcse_file, insn, 8);
6938 fprintf (gcse_file, "\n");
6941 copy = gen_move_insn ( reg, copy_rtx (SET_SRC (pat)));
6942 new = emit_insn_before (copy, insn);
6943 record_one_set (REGNO (reg), new);
6944 SET_SRC (pat) = reg;
6946 /* un-recognize this pattern since it's probably different now. */
6947 INSN_CODE (insn) = -1;
6948 gcse_create_count++;
6953 /* Store motion code. */
6955 #define ANTIC_STORE_LIST(x) ((x)->loads)
6956 #define AVAIL_STORE_LIST(x) ((x)->stores)
6957 #define LAST_AVAIL_CHECK_FAILURE(x) ((x)->reaching_reg)
6959 /* This is used to communicate the target bitvector we want to use in the
6960 reg_set_info routine when called via the note_stores mechanism. */
6961 static int * regvec;
6963 /* And current insn, for the same routine. */
6964 static rtx compute_store_table_current_insn;
6966 /* Used in computing the reverse edge graph bit vectors. */
6967 static sbitmap * st_antloc;
6969 /* Global holding the number of store expressions we are dealing with. */
6970 static int num_stores;
6972 /* Checks to set if we need to mark a register set. Called from note_stores. */
6974 static void
6975 reg_set_info (dest, setter, data)
6976 rtx dest, setter ATTRIBUTE_UNUSED;
6977 void * data ATTRIBUTE_UNUSED;
6979 if (GET_CODE (dest) == SUBREG)
6980 dest = SUBREG_REG (dest);
6982 if (GET_CODE (dest) == REG)
6983 regvec[REGNO (dest)] = INSN_UID (compute_store_table_current_insn);
6986 /* Return zero if some of the registers in list X are killed
6987 due to set of registers in bitmap REGS_SET. */
6989 static bool
6990 store_ops_ok (x, regs_set)
6991 rtx x;
6992 int *regs_set;
6994 rtx reg;
6996 for (; x; x = XEXP (x, 1))
6998 reg = XEXP (x, 0);
6999 if (regs_set[REGNO(reg)])
7000 return false;
7003 return true;
7006 /* Returns a list of registers mentioned in X. */
7007 static rtx
7008 extract_mentioned_regs (x)
7009 rtx x;
7011 return extract_mentioned_regs_helper (x, NULL_RTX);
7014 /* Helper for extract_mentioned_regs; ACCUM is used to accumulate used
7015 registers. */
7016 static rtx
7017 extract_mentioned_regs_helper (x, accum)
7018 rtx x;
7019 rtx accum;
7021 int i;
7022 enum rtx_code code;
7023 const char * fmt;
7025 /* Repeat is used to turn tail-recursion into iteration. */
7026 repeat:
7028 if (x == 0)
7029 return accum;
7031 code = GET_CODE (x);
7032 switch (code)
7034 case REG:
7035 return alloc_EXPR_LIST (0, x, accum);
7037 case MEM:
7038 x = XEXP (x, 0);
7039 goto repeat;
7041 case PRE_DEC:
7042 case PRE_INC:
7043 case POST_DEC:
7044 case POST_INC:
7045 /* We do not run this function with arguments having side effects. */
7046 abort ();
7048 case PC:
7049 case CC0: /*FIXME*/
7050 case CONST:
7051 case CONST_INT:
7052 case CONST_DOUBLE:
7053 case CONST_VECTOR:
7054 case SYMBOL_REF:
7055 case LABEL_REF:
7056 case ADDR_VEC:
7057 case ADDR_DIFF_VEC:
7058 return accum;
7060 default:
7061 break;
7064 i = GET_RTX_LENGTH (code) - 1;
7065 fmt = GET_RTX_FORMAT (code);
7067 for (; i >= 0; i--)
7069 if (fmt[i] == 'e')
7071 rtx tem = XEXP (x, i);
7073 /* If we are about to do the last recursive call
7074 needed at this level, change it into iteration. */
7075 if (i == 0)
7077 x = tem;
7078 goto repeat;
7081 accum = extract_mentioned_regs_helper (tem, accum);
7083 else if (fmt[i] == 'E')
7085 int j;
7087 for (j = 0; j < XVECLEN (x, i); j++)
7088 accum = extract_mentioned_regs_helper (XVECEXP (x, i, j), accum);
7092 return accum;
7095 /* Determine whether INSN is MEM store pattern that we will consider moving.
7096 REGS_SET_BEFORE is bitmap of registers set before (and including) the
7097 current insn, REGS_SET_AFTER is bitmap of registers set after (and
7098 including) the insn in this basic block. We must be passing through BB from
7099 head to end, as we are using this fact to speed things up.
7101 The results are stored this way:
7103 -- the first anticipatable expression is added into ANTIC_STORE_LIST
7104 -- if the processed expression is not anticipatable, NULL_RTX is added
7105 there instead, so that we can use it as indicator that no further
7106 expression of this type may be anticipatable
7107 -- if the expression is available, it is added as head of AVAIL_STORE_LIST;
7108 consequently, all of them but this head are dead and may be deleted.
7109 -- if the expression is not available, the insn due to that it fails to be
7110 available is stored in reaching_reg.
7112 The things are complicated a bit by fact that there already may be stores
7113 to the same MEM from other blocks; also caller must take care of the
7114 neccessary cleanup of the temporary markers after end of the basic block.
7117 static void
7118 find_moveable_store (insn, regs_set_before, regs_set_after)
7119 rtx insn;
7120 int *regs_set_before;
7121 int *regs_set_after;
7123 struct ls_expr * ptr;
7124 rtx dest, set, tmp;
7125 int check_anticipatable, check_available;
7126 basic_block bb = BLOCK_FOR_INSN (insn);
7128 set = single_set (insn);
7129 if (!set)
7130 return;
7132 dest = SET_DEST (set);
7134 if (GET_CODE (dest) != MEM || MEM_VOLATILE_P (dest)
7135 || GET_MODE (dest) == BLKmode)
7136 return;
7138 if (side_effects_p (dest))
7139 return;
7141 /* If we are handling exceptions, we must be careful with memory references
7142 that may trap. If we are not, the behavior is undefined, so we may just
7143 continue. */
7144 if (flag_exceptions && may_trap_p (dest))
7145 return;
7147 /* Do not consider MEMs that mention stack pointer; in the following
7148 we rely on that constant functions do not read memory, which of course
7149 does not include their arguments if passed on stack.
7151 Note that this is not quite correct -- we may use other registers
7152 to address stack. See store_killed_in_insn for handling of this
7153 case. */
7154 if (reg_mentioned_p (stack_pointer_rtx, dest))
7155 return;
7157 ptr = ldst_entry (dest);
7158 if (!ptr->pattern_regs)
7159 ptr->pattern_regs = extract_mentioned_regs (dest);
7161 /* Do not check for anticipatability if we either found one anticipatable
7162 store already, or tested for one and found out that it was killed. */
7163 check_anticipatable = 0;
7164 if (!ANTIC_STORE_LIST (ptr))
7165 check_anticipatable = 1;
7166 else
7168 tmp = XEXP (ANTIC_STORE_LIST (ptr), 0);
7169 if (tmp != NULL_RTX
7170 && BLOCK_FOR_INSN (tmp) != bb)
7171 check_anticipatable = 1;
7173 if (check_anticipatable)
7175 if (store_killed_before (dest, ptr->pattern_regs, insn, bb, regs_set_before))
7176 tmp = NULL_RTX;
7177 else
7178 tmp = insn;
7179 ANTIC_STORE_LIST (ptr) = alloc_INSN_LIST (tmp,
7180 ANTIC_STORE_LIST (ptr));
7183 /* It is not neccessary to check whether store is available if we did
7184 it successfully before; if we failed before, do not bother to check
7185 until we reach the insn that caused us to fail. */
7186 check_available = 0;
7187 if (!AVAIL_STORE_LIST (ptr))
7188 check_available = 1;
7189 else
7191 tmp = XEXP (AVAIL_STORE_LIST (ptr), 0);
7192 if (BLOCK_FOR_INSN (tmp) != bb)
7193 check_available = 1;
7195 if (check_available)
7197 /* Check that we have already reached the insn at that the check
7198 failed last time. */
7199 if (LAST_AVAIL_CHECK_FAILURE (ptr))
7201 for (tmp = bb->end;
7202 tmp != insn && tmp != LAST_AVAIL_CHECK_FAILURE (ptr);
7203 tmp = PREV_INSN (tmp))
7204 continue;
7205 if (tmp == insn)
7206 check_available = 0;
7208 else
7209 check_available = store_killed_after (dest, ptr->pattern_regs, insn,
7210 bb, regs_set_after,
7211 &LAST_AVAIL_CHECK_FAILURE (ptr));
7213 if (!check_available)
7214 AVAIL_STORE_LIST (ptr) = alloc_INSN_LIST (insn, AVAIL_STORE_LIST (ptr));
7217 /* Find available and anticipatable stores. */
7219 static int
7220 compute_store_table ()
7222 int ret;
7223 basic_block bb;
7224 unsigned regno;
7225 rtx insn, pat, tmp;
7226 int *last_set_in, *already_set;
7227 struct ls_expr * ptr, **prev_next_ptr_ptr;
7229 max_gcse_regno = max_reg_num ();
7231 reg_set_in_block = (sbitmap *) sbitmap_vector_alloc (last_basic_block,
7232 max_gcse_regno);
7233 sbitmap_vector_zero (reg_set_in_block, last_basic_block);
7234 pre_ldst_mems = 0;
7235 last_set_in = xmalloc (sizeof (int) * max_gcse_regno);
7236 already_set = xmalloc (sizeof (int) * max_gcse_regno);
7238 /* Find all the stores we care about. */
7239 FOR_EACH_BB (bb)
7241 /* First compute the registers set in this block. */
7242 memset (last_set_in, 0, sizeof (int) * max_gcse_regno);
7243 regvec = last_set_in;
7245 for (insn = bb->head;
7246 insn != NEXT_INSN (bb->end);
7247 insn = NEXT_INSN (insn))
7249 if (! INSN_P (insn))
7250 continue;
7252 if (GET_CODE (insn) == CALL_INSN)
7254 bool clobbers_all = false;
7255 #ifdef NON_SAVING_SETJMP
7256 if (NON_SAVING_SETJMP
7257 && find_reg_note (insn, REG_SETJMP, NULL_RTX))
7258 clobbers_all = true;
7259 #endif
7261 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
7262 if (clobbers_all
7263 || TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
7264 last_set_in[regno] = INSN_UID (insn);
7267 pat = PATTERN (insn);
7268 compute_store_table_current_insn = insn;
7269 note_stores (pat, reg_set_info, NULL);
7272 /* Record the set registers. */
7273 for (regno = 0; regno < max_gcse_regno; regno++)
7274 if (last_set_in[regno])
7275 SET_BIT (reg_set_in_block[bb->index], regno);
7277 /* Now find the stores. */
7278 memset (already_set, 0, sizeof (int) * max_gcse_regno);
7279 regvec = already_set;
7280 for (insn = bb->head;
7281 insn != NEXT_INSN (bb->end);
7282 insn = NEXT_INSN (insn))
7284 if (! INSN_P (insn))
7285 continue;
7287 if (GET_CODE (insn) == CALL_INSN)
7289 bool clobbers_all = false;
7290 #ifdef NON_SAVING_SETJMP
7291 if (NON_SAVING_SETJMP
7292 && find_reg_note (insn, REG_SETJMP, NULL_RTX))
7293 clobbers_all = true;
7294 #endif
7296 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
7297 if (clobbers_all
7298 || TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
7299 already_set[regno] = 1;
7302 pat = PATTERN (insn);
7303 note_stores (pat, reg_set_info, NULL);
7305 /* Now that we've marked regs, look for stores. */
7306 find_moveable_store (insn, already_set, last_set_in);
7308 /* Unmark regs that are no longer set. */
7309 for (regno = 0; regno < max_gcse_regno; regno++)
7310 if (last_set_in[regno] == INSN_UID (insn))
7311 last_set_in[regno] = 0;
7314 /* Clear temporary marks. */
7315 for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
7317 LAST_AVAIL_CHECK_FAILURE(ptr) = NULL_RTX;
7318 if (ANTIC_STORE_LIST (ptr)
7319 && (tmp = XEXP (ANTIC_STORE_LIST (ptr), 0)) == NULL_RTX)
7320 ANTIC_STORE_LIST (ptr) = XEXP (ANTIC_STORE_LIST (ptr), 1);
7324 /* Remove the stores that are not available anywhere, as there will
7325 be no opportunity to optimize them. */
7326 for (ptr = pre_ldst_mems, prev_next_ptr_ptr = &pre_ldst_mems;
7327 ptr != NULL;
7328 ptr = *prev_next_ptr_ptr)
7330 if (!AVAIL_STORE_LIST (ptr))
7332 *prev_next_ptr_ptr = ptr->next;
7333 free_ldst_entry (ptr);
7335 else
7336 prev_next_ptr_ptr = &ptr->next;
7339 ret = enumerate_ldsts ();
7341 if (gcse_file)
7343 fprintf (gcse_file, "ST_avail and ST_antic (shown under loads..)\n");
7344 print_ldst_list (gcse_file);
7347 free (last_set_in);
7348 free (already_set);
7349 return ret;
7352 /* Check to see if the load X is aliased with STORE_PATTERN. */
7354 static bool
7355 load_kills_store (x, store_pattern)
7356 rtx x, store_pattern;
7358 if (true_dependence (x, GET_MODE (x), store_pattern, rtx_addr_varies_p))
7359 return true;
7360 return false;
7363 /* Go through the entire insn X, looking for any loads which might alias
7364 STORE_PATTERN. Return true if found. */
7366 static bool
7367 find_loads (x, store_pattern)
7368 rtx x, store_pattern;
7370 const char * fmt;
7371 int i, j;
7372 int ret = false;
7374 if (!x)
7375 return false;
7377 if (GET_CODE (x) == SET)
7378 x = SET_SRC (x);
7380 if (GET_CODE (x) == MEM)
7382 if (load_kills_store (x, store_pattern))
7383 return true;
7386 /* Recursively process the insn. */
7387 fmt = GET_RTX_FORMAT (GET_CODE (x));
7389 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0 && !ret; i--)
7391 if (fmt[i] == 'e')
7392 ret |= find_loads (XEXP (x, i), store_pattern);
7393 else if (fmt[i] == 'E')
7394 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
7395 ret |= find_loads (XVECEXP (x, i, j), store_pattern);
7397 return ret;
7400 /* Check if INSN kills the store pattern X (is aliased with it).
7401 Return true if it it does. */
7403 static bool
7404 store_killed_in_insn (x, x_regs, insn)
7405 rtx x, x_regs, insn;
7407 if (GET_RTX_CLASS (GET_CODE (insn)) != 'i')
7408 return false;
7410 if (GET_CODE (insn) == CALL_INSN)
7412 /* A normal or pure call might read from pattern,
7413 but a const call will not. */
7414 if (! CONST_OR_PURE_CALL_P (insn) || pure_call_p (insn))
7415 return true;
7417 /* But even a const call reads its parameters. It is not trivial
7418 check that base of the mem is not related to stack pointer,
7419 so unless it contains no registers, just assume it may. */
7420 if (x_regs)
7421 return true;
7423 return false;
7426 if (GET_CODE (PATTERN (insn)) == SET)
7428 rtx pat = PATTERN (insn);
7429 /* Check for memory stores to aliased objects. */
7430 if (GET_CODE (SET_DEST (pat)) == MEM && !expr_equiv_p (SET_DEST (pat), x))
7431 /* pretend its a load and check for aliasing. */
7432 if (find_loads (SET_DEST (pat), x))
7433 return true;
7434 return find_loads (SET_SRC (pat), x);
7436 else
7437 return find_loads (PATTERN (insn), x);
7440 /* Returns true if the expression X is loaded or clobbered on or after INSN
7441 within basic block BB. REGS_SET_AFTER is bitmap of registers set in
7442 or after the insn. X_REGS is list of registers mentioned in X. If the store
7443 is killed, return the last insn in that it occurs in FAIL_INSN. */
7445 static bool
7446 store_killed_after (x, x_regs, insn, bb, regs_set_after, fail_insn)
7447 rtx x, x_regs, insn;
7448 basic_block bb;
7449 int *regs_set_after;
7450 rtx *fail_insn;
7452 rtx last = bb->end, act;
7454 if (!store_ops_ok (x_regs, regs_set_after))
7456 /* We do not know where it will happen. */
7457 if (fail_insn)
7458 *fail_insn = NULL_RTX;
7459 return true;
7462 /* Scan from the end, so that fail_insn is determined correctly. */
7463 for (act = last; act != PREV_INSN (insn); act = PREV_INSN (act))
7464 if (store_killed_in_insn (x, x_regs, act))
7466 if (fail_insn)
7467 *fail_insn = act;
7468 return true;
7471 return false;
7474 /* Returns true if the expression X is loaded or clobbered on or before INSN
7475 within basic block BB. X_REGS is list of registers mentioned in X.
7476 REGS_SET_BEFORE is bitmap of registers set before or in this insn. */
7477 static bool
7478 store_killed_before (x, x_regs, insn, bb, regs_set_before)
7479 rtx x, x_regs, insn;
7480 basic_block bb;
7481 int *regs_set_before;
7483 rtx first = bb->head;
7485 if (!store_ops_ok (x_regs, regs_set_before))
7486 return true;
7488 for ( ; insn != PREV_INSN (first); insn = PREV_INSN (insn))
7489 if (store_killed_in_insn (x, x_regs, insn))
7490 return true;
7492 return false;
7495 /* Fill in available, anticipatable, transparent and kill vectors in
7496 STORE_DATA, based on lists of available and anticipatable stores. */
7497 static void
7498 build_store_vectors ()
7500 basic_block bb;
7501 int *regs_set_in_block;
7502 rtx insn, st;
7503 struct ls_expr * ptr;
7504 unsigned regno;
7506 /* Build the gen_vector. This is any store in the table which is not killed
7507 by aliasing later in its block. */
7508 ae_gen = (sbitmap *) sbitmap_vector_alloc (last_basic_block, num_stores);
7509 sbitmap_vector_zero (ae_gen, last_basic_block);
7511 st_antloc = (sbitmap *) sbitmap_vector_alloc (last_basic_block, num_stores);
7512 sbitmap_vector_zero (st_antloc, last_basic_block);
7514 for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
7516 for (st = AVAIL_STORE_LIST (ptr); st != NULL; st = XEXP (st, 1))
7518 insn = XEXP (st, 0);
7519 bb = BLOCK_FOR_INSN (insn);
7521 /* If we've already seen an available expression in this block,
7522 we can delete this one (It occurs earlier in the block). We'll
7523 copy the SRC expression to an unused register in case there
7524 are any side effects. */
7525 if (TEST_BIT (ae_gen[bb->index], ptr->index))
7527 rtx r = gen_reg_rtx (GET_MODE (ptr->pattern));
7528 if (gcse_file)
7529 fprintf (gcse_file, "Removing redundant store:\n");
7530 replace_store_insn (r, XEXP (st, 0), bb);
7531 continue;
7533 SET_BIT (ae_gen[bb->index], ptr->index);
7536 for (st = ANTIC_STORE_LIST (ptr); st != NULL; st = XEXP (st, 1))
7538 insn = XEXP (st, 0);
7539 bb = BLOCK_FOR_INSN (insn);
7540 SET_BIT (st_antloc[bb->index], ptr->index);
7544 ae_kill = (sbitmap *) sbitmap_vector_alloc (last_basic_block, num_stores);
7545 sbitmap_vector_zero (ae_kill, last_basic_block);
7547 transp = (sbitmap *) sbitmap_vector_alloc (last_basic_block, num_stores);
7548 sbitmap_vector_zero (transp, last_basic_block);
7549 regs_set_in_block = xmalloc (sizeof (int) * max_gcse_regno);
7551 FOR_EACH_BB (bb)
7553 for (regno = 0; regno < max_gcse_regno; regno++)
7554 regs_set_in_block[regno] = TEST_BIT (reg_set_in_block[bb->index], regno);
7556 for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
7558 if (store_killed_after (ptr->pattern, ptr->pattern_regs, bb->head,
7559 bb, regs_set_in_block, NULL))
7561 /* It should not be neccessary to consider the expression
7562 killed if it is both anticipatable and available. */
7563 if (!TEST_BIT (st_antloc[bb->index], ptr->index)
7564 || !TEST_BIT (ae_gen[bb->index], ptr->index))
7565 SET_BIT (ae_kill[bb->index], ptr->index);
7567 else
7568 SET_BIT (transp[bb->index], ptr->index);
7572 free (regs_set_in_block);
7574 if (gcse_file)
7576 dump_sbitmap_vector (gcse_file, "st_antloc", "", st_antloc, last_basic_block);
7577 dump_sbitmap_vector (gcse_file, "st_kill", "", ae_kill, last_basic_block);
7578 dump_sbitmap_vector (gcse_file, "Transpt", "", transp, last_basic_block);
7579 dump_sbitmap_vector (gcse_file, "st_avloc", "", ae_gen, last_basic_block);
7583 /* Insert an instruction at the beginning of a basic block, and update
7584 the BLOCK_HEAD if needed. */
7586 static void
7587 insert_insn_start_bb (insn, bb)
7588 rtx insn;
7589 basic_block bb;
7591 /* Insert at start of successor block. */
7592 rtx prev = PREV_INSN (bb->head);
7593 rtx before = bb->head;
7594 while (before != 0)
7596 if (GET_CODE (before) != CODE_LABEL
7597 && (GET_CODE (before) != NOTE
7598 || NOTE_LINE_NUMBER (before) != NOTE_INSN_BASIC_BLOCK))
7599 break;
7600 prev = before;
7601 if (prev == bb->end)
7602 break;
7603 before = NEXT_INSN (before);
7606 insn = emit_insn_after (insn, prev);
7608 if (gcse_file)
7610 fprintf (gcse_file, "STORE_MOTION insert store at start of BB %d:\n",
7611 bb->index);
7612 print_inline_rtx (gcse_file, insn, 6);
7613 fprintf (gcse_file, "\n");
7617 /* This routine will insert a store on an edge. EXPR is the ldst entry for
7618 the memory reference, and E is the edge to insert it on. Returns nonzero
7619 if an edge insertion was performed. */
7621 static int
7622 insert_store (expr, e)
7623 struct ls_expr * expr;
7624 edge e;
7626 rtx reg, insn;
7627 basic_block bb;
7628 edge tmp;
7630 /* We did all the deleted before this insert, so if we didn't delete a
7631 store, then we haven't set the reaching reg yet either. */
7632 if (expr->reaching_reg == NULL_RTX)
7633 return 0;
7635 reg = expr->reaching_reg;
7636 insn = gen_move_insn (copy_rtx (expr->pattern), reg);
7638 /* If we are inserting this expression on ALL predecessor edges of a BB,
7639 insert it at the start of the BB, and reset the insert bits on the other
7640 edges so we don't try to insert it on the other edges. */
7641 bb = e->dest;
7642 for (tmp = e->dest->pred; tmp ; tmp = tmp->pred_next)
7644 int index = EDGE_INDEX (edge_list, tmp->src, tmp->dest);
7645 if (index == EDGE_INDEX_NO_EDGE)
7646 abort ();
7647 if (! TEST_BIT (pre_insert_map[index], expr->index))
7648 break;
7651 /* If tmp is NULL, we found an insertion on every edge, blank the
7652 insertion vector for these edges, and insert at the start of the BB. */
7653 if (!tmp && bb != EXIT_BLOCK_PTR)
7655 for (tmp = e->dest->pred; tmp ; tmp = tmp->pred_next)
7657 int index = EDGE_INDEX (edge_list, tmp->src, tmp->dest);
7658 RESET_BIT (pre_insert_map[index], expr->index);
7660 insert_insn_start_bb (insn, bb);
7661 return 0;
7664 /* We can't insert on this edge, so we'll insert at the head of the
7665 successors block. See Morgan, sec 10.5. */
7666 if ((e->flags & EDGE_ABNORMAL) == EDGE_ABNORMAL)
7668 insert_insn_start_bb (insn, bb);
7669 return 0;
7672 insert_insn_on_edge (insn, e);
7674 if (gcse_file)
7676 fprintf (gcse_file, "STORE_MOTION insert insn on edge (%d, %d):\n",
7677 e->src->index, e->dest->index);
7678 print_inline_rtx (gcse_file, insn, 6);
7679 fprintf (gcse_file, "\n");
7682 return 1;
7685 /* This routine will replace a store with a SET to a specified register. */
7687 static void
7688 replace_store_insn (reg, del, bb)
7689 rtx reg, del;
7690 basic_block bb;
7692 rtx insn;
7694 insn = gen_move_insn (reg, SET_SRC (PATTERN (del)));
7695 insn = emit_insn_after (insn, del);
7697 if (gcse_file)
7699 fprintf (gcse_file,
7700 "STORE_MOTION delete insn in BB %d:\n ", bb->index);
7701 print_inline_rtx (gcse_file, del, 6);
7702 fprintf (gcse_file, "\nSTORE MOTION replaced with insn:\n ");
7703 print_inline_rtx (gcse_file, insn, 6);
7704 fprintf (gcse_file, "\n");
7707 delete_insn (del);
7711 /* Delete a store, but copy the value that would have been stored into
7712 the reaching_reg for later storing. */
7714 static void
7715 delete_store (expr, bb)
7716 struct ls_expr * expr;
7717 basic_block bb;
7719 rtx reg, i, del;
7721 if (expr->reaching_reg == NULL_RTX)
7722 expr->reaching_reg = gen_reg_rtx (GET_MODE (expr->pattern));
7724 reg = expr->reaching_reg;
7726 for (i = AVAIL_STORE_LIST (expr); i; i = XEXP (i, 1))
7728 del = XEXP (i, 0);
7729 if (BLOCK_FOR_INSN (del) == bb)
7731 /* We know there is only one since we deleted redundant
7732 ones during the available computation. */
7733 replace_store_insn (reg, del, bb);
7734 break;
7739 /* Free memory used by store motion. */
7741 static void
7742 free_store_memory ()
7744 free_ldst_mems ();
7746 if (ae_gen)
7747 sbitmap_vector_free (ae_gen);
7748 if (ae_kill)
7749 sbitmap_vector_free (ae_kill);
7750 if (transp)
7751 sbitmap_vector_free (transp);
7752 if (st_antloc)
7753 sbitmap_vector_free (st_antloc);
7754 if (pre_insert_map)
7755 sbitmap_vector_free (pre_insert_map);
7756 if (pre_delete_map)
7757 sbitmap_vector_free (pre_delete_map);
7758 if (reg_set_in_block)
7759 sbitmap_vector_free (reg_set_in_block);
7761 ae_gen = ae_kill = transp = st_antloc = NULL;
7762 pre_insert_map = pre_delete_map = reg_set_in_block = NULL;
7765 /* Perform store motion. Much like gcse, except we move expressions the
7766 other way by looking at the flowgraph in reverse. */
7768 static void
7769 store_motion ()
7771 basic_block bb;
7772 int x;
7773 struct ls_expr * ptr;
7774 int update_flow = 0;
7776 if (gcse_file)
7778 fprintf (gcse_file, "before store motion\n");
7779 print_rtl (gcse_file, get_insns ());
7783 init_alias_analysis ();
7785 /* Find all the available and anticipatable stores. */
7786 num_stores = compute_store_table ();
7787 if (num_stores == 0)
7789 sbitmap_vector_free (reg_set_in_block);
7790 end_alias_analysis ();
7791 return;
7794 /* Now compute kill & transp vectors. */
7795 build_store_vectors ();
7796 add_noreturn_fake_exit_edges ();
7798 edge_list = pre_edge_rev_lcm (gcse_file, num_stores, transp, ae_gen,
7799 st_antloc, ae_kill, &pre_insert_map,
7800 &pre_delete_map);
7802 /* Now we want to insert the new stores which are going to be needed. */
7803 for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
7805 FOR_EACH_BB (bb)
7806 if (TEST_BIT (pre_delete_map[bb->index], ptr->index))
7807 delete_store (ptr, bb);
7809 for (x = 0; x < NUM_EDGES (edge_list); x++)
7810 if (TEST_BIT (pre_insert_map[x], ptr->index))
7811 update_flow |= insert_store (ptr, INDEX_EDGE (edge_list, x));
7814 if (update_flow)
7815 commit_edge_insertions ();
7817 free_store_memory ();
7818 free_edge_list (edge_list);
7819 remove_fake_edges ();
7820 end_alias_analysis ();
7824 /* Entry point for jump bypassing optimization pass. */
7827 bypass_jumps (file)
7828 FILE *file;
7830 int changed;
7832 /* We do not construct an accurate cfg in functions which call
7833 setjmp, so just punt to be safe. */
7834 if (current_function_calls_setjmp)
7835 return 0;
7837 /* For calling dump_foo fns from gdb. */
7838 debug_stderr = stderr;
7839 gcse_file = file;
7841 /* Identify the basic block information for this function, including
7842 successors and predecessors. */
7843 max_gcse_regno = max_reg_num ();
7845 if (file)
7846 dump_flow_info (file);
7848 /* Return if there's nothing to do. */
7849 if (n_basic_blocks <= 1)
7850 return 0;
7852 /* Trying to perform global optimizations on flow graphs which have
7853 a high connectivity will take a long time and is unlikely to be
7854 particularly useful.
7856 In normal circumstances a cfg should have about twice as many edges
7857 as blocks. But we do not want to punish small functions which have
7858 a couple switch statements. So we require a relatively large number
7859 of basic blocks and the ratio of edges to blocks to be high. */
7860 if (n_basic_blocks > 1000 && n_edges / n_basic_blocks >= 20)
7862 if (warn_disabled_optimization)
7863 warning ("BYPASS disabled: %d > 1000 basic blocks and %d >= 20 edges/basic block",
7864 n_basic_blocks, n_edges / n_basic_blocks);
7865 return 0;
7868 /* If allocating memory for the cprop bitmap would take up too much
7869 storage it's better just to disable the optimization. */
7870 if ((n_basic_blocks
7871 * SBITMAP_SET_SIZE (max_gcse_regno)
7872 * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
7874 if (warn_disabled_optimization)
7875 warning ("GCSE disabled: %d basic blocks and %d registers",
7876 n_basic_blocks, max_gcse_regno);
7878 return 0;
7881 /* See what modes support reg/reg copy operations. */
7882 if (! can_copy_init_p)
7884 compute_can_copy ();
7885 can_copy_init_p = 1;
7888 gcc_obstack_init (&gcse_obstack);
7889 bytes_used = 0;
7891 /* We need alias. */
7892 init_alias_analysis ();
7894 /* Record where pseudo-registers are set. This data is kept accurate
7895 during each pass. ??? We could also record hard-reg information here
7896 [since it's unchanging], however it is currently done during hash table
7897 computation.
7899 It may be tempting to compute MEM set information here too, but MEM sets
7900 will be subject to code motion one day and thus we need to compute
7901 information about memory sets when we build the hash tables. */
7903 alloc_reg_set_mem (max_gcse_regno);
7904 compute_sets (get_insns ());
7906 max_gcse_regno = max_reg_num ();
7907 alloc_gcse_mem (get_insns ());
7908 changed = one_cprop_pass (1, 1, 1);
7909 free_gcse_mem ();
7911 if (file)
7913 fprintf (file, "BYPASS of %s: %d basic blocks, ",
7914 current_function_name, n_basic_blocks);
7915 fprintf (file, "%d bytes\n\n", bytes_used);
7918 obstack_free (&gcse_obstack, NULL);
7919 free_reg_set_mem ();
7921 /* We are finished with alias. */
7922 end_alias_analysis ();
7923 allocate_reg_info (max_reg_num (), FALSE, FALSE);
7925 return changed;
7928 #include "gt-gcse.h"