dwarf2out.c (mem_loc_descriptor): Recurse on LO_SUM.
[official-gcc.git] / gcc / gcse.c
bloba1de61fd24c3cd79119094d13a0d6a64c251e1be
1 /* Partial redundancy elimination / Hoisting for RTL.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* TODO
22 - reordering of memory allocation and freeing to be more space efficient
23 - do rough calc of how many regs are needed in each block, and a rough
24 calc of how many regs are available in each class and use that to
25 throttle back the code in cases where RTX_COST is minimal.
26 - a store to the same address as a load does not kill the load if the
27 source of the store is also the destination of the load. Handling this
28 allows more load motion, particularly out of loops.
32 /* References searched while implementing this.
34 Compilers Principles, Techniques and Tools
35 Aho, Sethi, Ullman
36 Addison-Wesley, 1988
38 Global Optimization by Suppression of Partial Redundancies
39 E. Morel, C. Renvoise
40 communications of the acm, Vol. 22, Num. 2, Feb. 1979
42 A Portable Machine-Independent Global Optimizer - Design and Measurements
43 Frederick Chow
44 Stanford Ph.D. thesis, Dec. 1983
46 A Fast Algorithm for Code Movement Optimization
47 D.M. Dhamdhere
48 SIGPLAN Notices, Vol. 23, Num. 10, Oct. 1988
50 A Solution to a Problem with Morel and Renvoise's
51 Global Optimization by Suppression of Partial Redundancies
52 K-H Drechsler, M.P. Stadel
53 ACM TOPLAS, Vol. 10, Num. 4, Oct. 1988
55 Practical Adaptation of the Global Optimization
56 Algorithm of Morel and Renvoise
57 D.M. Dhamdhere
58 ACM TOPLAS, Vol. 13, Num. 2. Apr. 1991
60 Efficiently Computing Static Single Assignment Form and the Control
61 Dependence Graph
62 R. Cytron, J. Ferrante, B.K. Rosen, M.N. Wegman, and F.K. Zadeck
63 ACM TOPLAS, Vol. 13, Num. 4, Oct. 1991
65 Lazy Code Motion
66 J. Knoop, O. Ruthing, B. Steffen
67 ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
69 What's In a Region? Or Computing Control Dependence Regions in Near-Linear
70 Time for Reducible Flow Control
71 Thomas Ball
72 ACM Letters on Programming Languages and Systems,
73 Vol. 2, Num. 1-4, Mar-Dec 1993
75 An Efficient Representation for Sparse Sets
76 Preston Briggs, Linda Torczon
77 ACM Letters on Programming Languages and Systems,
78 Vol. 2, Num. 1-4, Mar-Dec 1993
80 A Variation of Knoop, Ruthing, and Steffen's Lazy Code Motion
81 K-H Drechsler, M.P. Stadel
82 ACM SIGPLAN Notices, Vol. 28, Num. 5, May 1993
84 Partial Dead Code Elimination
85 J. Knoop, O. Ruthing, B. Steffen
86 ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
88 Effective Partial Redundancy Elimination
89 P. Briggs, K.D. Cooper
90 ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
92 The Program Structure Tree: Computing Control Regions in Linear Time
93 R. Johnson, D. Pearson, K. Pingali
94 ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
96 Optimal Code Motion: Theory and Practice
97 J. Knoop, O. Ruthing, B. Steffen
98 ACM TOPLAS, Vol. 16, Num. 4, Jul. 1994
100 The power of assignment motion
101 J. Knoop, O. Ruthing, B. Steffen
102 ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
104 Global code motion / global value numbering
105 C. Click
106 ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
108 Value Driven Redundancy Elimination
109 L.T. Simpson
110 Rice University Ph.D. thesis, Apr. 1996
112 Value Numbering
113 L.T. Simpson
114 Massively Scalar Compiler Project, Rice University, Sep. 1996
116 High Performance Compilers for Parallel Computing
117 Michael Wolfe
118 Addison-Wesley, 1996
120 Advanced Compiler Design and Implementation
121 Steven Muchnick
122 Morgan Kaufmann, 1997
124 Building an Optimizing Compiler
125 Robert Morgan
126 Digital Press, 1998
128 People wishing to speed up the code here should read:
129 Elimination Algorithms for Data Flow Analysis
130 B.G. Ryder, M.C. Paull
131 ACM Computing Surveys, Vol. 18, Num. 3, Sep. 1986
133 How to Analyze Large Programs Efficiently and Informatively
134 D.M. Dhamdhere, B.K. Rosen, F.K. Zadeck
135 ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
137 People wishing to do something different can find various possibilities
138 in the above papers and elsewhere.
141 #include "config.h"
142 #include "system.h"
143 #include "coretypes.h"
144 #include "tm.h"
145 #include "diagnostic-core.h"
146 #include "toplev.h"
148 #include "rtl.h"
149 #include "tree.h"
150 #include "tm_p.h"
151 #include "regs.h"
152 #include "hard-reg-set.h"
153 #include "flags.h"
154 #include "insn-config.h"
155 #include "recog.h"
156 #include "basic-block.h"
157 #include "output.h"
158 #include "function.h"
159 #include "expr.h"
160 #include "except.h"
161 #include "ggc.h"
162 #include "params.h"
163 #include "cselib.h"
164 #include "intl.h"
165 #include "obstack.h"
166 #include "timevar.h"
167 #include "tree-pass.h"
168 #include "hashtab.h"
169 #include "df.h"
170 #include "dbgcnt.h"
171 #include "target.h"
172 #include "gcse.h"
174 /* We support GCSE via Partial Redundancy Elimination. PRE optimizations
175 are a superset of those done by classic GCSE.
177 Two passes of copy/constant propagation are done around PRE or hoisting
178 because the first one enables more GCSE and the second one helps to clean
179 up the copies that PRE and HOIST create. This is needed more for PRE than
180 for HOIST because code hoisting will try to use an existing register
181 containing the common subexpression rather than create a new one. This is
182 harder to do for PRE because of the code motion (which HOIST doesn't do).
184 Expressions we are interested in GCSE-ing are of the form
185 (set (pseudo-reg) (expression)).
186 Function want_to_gcse_p says what these are.
188 In addition, expressions in REG_EQUAL notes are candidates for GCSE-ing.
189 This allows PRE to hoist expressions that are expressed in multiple insns,
190 such as complex address calculations (e.g. for PIC code, or loads with a
191 high part and a low part).
193 PRE handles moving invariant expressions out of loops (by treating them as
194 partially redundant).
196 **********************
198 We used to support multiple passes but there are diminishing returns in
199 doing so. The first pass usually makes 90% of the changes that are doable.
200 A second pass can make a few more changes made possible by the first pass.
201 Experiments show any further passes don't make enough changes to justify
202 the expense.
204 A study of spec92 using an unlimited number of passes:
205 [1 pass] = 1208 substitutions, [2] = 577, [3] = 202, [4] = 192, [5] = 83,
206 [6] = 34, [7] = 17, [8] = 9, [9] = 4, [10] = 4, [11] = 2,
207 [12] = 2, [13] = 1, [15] = 1, [16] = 2, [41] = 1
209 It was found doing copy propagation between each pass enables further
210 substitutions.
212 This study was done before expressions in REG_EQUAL notes were added as
213 candidate expressions for optimization, and before the GIMPLE optimizers
214 were added. Probably, multiple passes is even less efficient now than
215 at the time when the study was conducted.
217 PRE is quite expensive in complicated functions because the DFA can take
218 a while to converge. Hence we only perform one pass.
220 **********************
222 The steps for PRE are:
224 1) Build the hash table of expressions we wish to GCSE (expr_hash_table).
226 2) Perform the data flow analysis for PRE.
228 3) Delete the redundant instructions
230 4) Insert the required copies [if any] that make the partially
231 redundant instructions fully redundant.
233 5) For other reaching expressions, insert an instruction to copy the value
234 to a newly created pseudo that will reach the redundant instruction.
236 The deletion is done first so that when we do insertions we
237 know which pseudo reg to use.
239 Various papers have argued that PRE DFA is expensive (O(n^2)) and others
240 argue it is not. The number of iterations for the algorithm to converge
241 is typically 2-4 so I don't view it as that expensive (relatively speaking).
243 PRE GCSE depends heavily on the second CPROP pass to clean up the copies
244 we create. To make an expression reach the place where it's redundant,
245 the result of the expression is copied to a new register, and the redundant
246 expression is deleted by replacing it with this new register. Classic GCSE
247 doesn't have this problem as much as it computes the reaching defs of
248 each register in each block and thus can try to use an existing
249 register. */
251 /* GCSE global vars. */
253 struct target_gcse default_target_gcse;
254 #if SWITCHABLE_TARGET
255 struct target_gcse *this_target_gcse = &default_target_gcse;
256 #endif
258 /* Set to non-zero if CSE should run after all GCSE optimizations are done. */
259 int flag_rerun_cse_after_global_opts;
261 /* An obstack for our working variables. */
262 static struct obstack gcse_obstack;
264 struct reg_use {rtx reg_rtx; };
266 /* Hash table of expressions. */
268 struct expr
270 /* The expression (SET_SRC for expressions, PATTERN for assignments). */
271 rtx expr;
272 /* Index in the available expression bitmaps. */
273 int bitmap_index;
274 /* Next entry with the same hash. */
275 struct expr *next_same_hash;
276 /* List of anticipatable occurrences in basic blocks in the function.
277 An "anticipatable occurrence" is one that is the first occurrence in the
278 basic block, the operands are not modified in the basic block prior
279 to the occurrence and the output is not used between the start of
280 the block and the occurrence. */
281 struct occr *antic_occr;
282 /* List of available occurrence in basic blocks in the function.
283 An "available occurrence" is one that is the last occurrence in the
284 basic block and the operands are not modified by following statements in
285 the basic block [including this insn]. */
286 struct occr *avail_occr;
287 /* Non-null if the computation is PRE redundant.
288 The value is the newly created pseudo-reg to record a copy of the
289 expression in all the places that reach the redundant copy. */
290 rtx reaching_reg;
291 /* Maximum distance in instructions this expression can travel.
292 We avoid moving simple expressions for more than a few instructions
293 to keep register pressure under control.
294 A value of "0" removes restrictions on how far the expression can
295 travel. */
296 int max_distance;
299 /* Occurrence of an expression.
300 There is one per basic block. If a pattern appears more than once the
301 last appearance is used [or first for anticipatable expressions]. */
303 struct occr
305 /* Next occurrence of this expression. */
306 struct occr *next;
307 /* The insn that computes the expression. */
308 rtx insn;
309 /* Nonzero if this [anticipatable] occurrence has been deleted. */
310 char deleted_p;
311 /* Nonzero if this [available] occurrence has been copied to
312 reaching_reg. */
313 /* ??? This is mutually exclusive with deleted_p, so they could share
314 the same byte. */
315 char copied_p;
318 typedef struct occr *occr_t;
319 DEF_VEC_P (occr_t);
320 DEF_VEC_ALLOC_P (occr_t, heap);
322 /* Expression hash tables.
323 Each hash table is an array of buckets.
324 ??? It is known that if it were an array of entries, structure elements
325 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
326 not clear whether in the final analysis a sufficient amount of memory would
327 be saved as the size of the available expression bitmaps would be larger
328 [one could build a mapping table without holes afterwards though].
329 Someday I'll perform the computation and figure it out. */
331 struct hash_table_d
333 /* The table itself.
334 This is an array of `expr_hash_table_size' elements. */
335 struct expr **table;
337 /* Size of the hash table, in elements. */
338 unsigned int size;
340 /* Number of hash table elements. */
341 unsigned int n_elems;
344 /* Expression hash table. */
345 static struct hash_table_d expr_hash_table;
347 /* This is a list of expressions which are MEMs and will be used by load
348 or store motion.
349 Load motion tracks MEMs which aren't killed by
350 anything except itself. (i.e., loads and stores to a single location).
351 We can then allow movement of these MEM refs with a little special
352 allowance. (all stores copy the same value to the reaching reg used
353 for the loads). This means all values used to store into memory must have
354 no side effects so we can re-issue the setter value.
355 Store Motion uses this structure as an expression table to track stores
356 which look interesting, and might be moveable towards the exit block. */
358 struct ls_expr
360 struct expr * expr; /* Gcse expression reference for LM. */
361 rtx pattern; /* Pattern of this mem. */
362 rtx pattern_regs; /* List of registers mentioned by the mem. */
363 rtx loads; /* INSN list of loads seen. */
364 rtx stores; /* INSN list of stores seen. */
365 struct ls_expr * next; /* Next in the list. */
366 int invalid; /* Invalid for some reason. */
367 int index; /* If it maps to a bitmap index. */
368 unsigned int hash_index; /* Index when in a hash table. */
369 rtx reaching_reg; /* Register to use when re-writing. */
372 /* Head of the list of load/store memory refs. */
373 static struct ls_expr * pre_ldst_mems = NULL;
375 /* Hashtable for the load/store memory refs. */
376 static htab_t pre_ldst_table = NULL;
378 /* Bitmap containing one bit for each register in the program.
379 Used when performing GCSE to track which registers have been set since
380 the start of the basic block. */
381 static regset reg_set_bitmap;
383 /* Array, indexed by basic block number for a list of insns which modify
384 memory within that block. */
385 static rtx * modify_mem_list;
386 static bitmap modify_mem_list_set;
388 /* This array parallels modify_mem_list, but is kept canonicalized. */
389 static rtx * canon_modify_mem_list;
391 /* Bitmap indexed by block numbers to record which blocks contain
392 function calls. */
393 static bitmap blocks_with_calls;
395 /* Various variables for statistics gathering. */
397 /* Memory used in a pass.
398 This isn't intended to be absolutely precise. Its intent is only
399 to keep an eye on memory usage. */
400 static int bytes_used;
402 /* GCSE substitutions made. */
403 static int gcse_subst_count;
404 /* Number of copy instructions created. */
405 static int gcse_create_count;
407 /* Doing code hoisting. */
408 static bool doing_code_hoisting_p = false;
410 /* For available exprs */
411 static sbitmap *ae_kill;
413 static void compute_can_copy (void);
414 static void *gmalloc (size_t) ATTRIBUTE_MALLOC;
415 static void *gcalloc (size_t, size_t) ATTRIBUTE_MALLOC;
416 static void *gcse_alloc (unsigned long);
417 static void alloc_gcse_mem (void);
418 static void free_gcse_mem (void);
419 static void hash_scan_insn (rtx, struct hash_table_d *);
420 static void hash_scan_set (rtx, rtx, struct hash_table_d *);
421 static void hash_scan_clobber (rtx, rtx, struct hash_table_d *);
422 static void hash_scan_call (rtx, rtx, struct hash_table_d *);
423 static int want_to_gcse_p (rtx, int *);
424 static int oprs_unchanged_p (const_rtx, const_rtx, int);
425 static int oprs_anticipatable_p (const_rtx, const_rtx);
426 static int oprs_available_p (const_rtx, const_rtx);
427 static void insert_expr_in_table (rtx, enum machine_mode, rtx, int, int, int,
428 struct hash_table_d *);
429 static unsigned int hash_expr (const_rtx, enum machine_mode, int *, int);
430 static int expr_equiv_p (const_rtx, const_rtx);
431 static void record_last_reg_set_info (rtx, int);
432 static void record_last_mem_set_info (rtx);
433 static void record_last_set_info (rtx, const_rtx, void *);
434 static void compute_hash_table (struct hash_table_d *);
435 static void alloc_hash_table (struct hash_table_d *);
436 static void free_hash_table (struct hash_table_d *);
437 static void compute_hash_table_work (struct hash_table_d *);
438 static void dump_hash_table (FILE *, const char *, struct hash_table_d *);
439 static void compute_transp (const_rtx, int, sbitmap *);
440 static void compute_local_properties (sbitmap *, sbitmap *, sbitmap *,
441 struct hash_table_d *);
442 static void mems_conflict_for_gcse_p (rtx, const_rtx, void *);
443 static int load_killed_in_block_p (const_basic_block, int, const_rtx, int);
444 static void canon_list_insert (rtx, const_rtx, void *);
445 static void alloc_pre_mem (int, int);
446 static void free_pre_mem (void);
447 static void compute_pre_data (void);
448 static int pre_expr_reaches_here_p (basic_block, struct expr *,
449 basic_block);
450 static void insert_insn_end_basic_block (struct expr *, basic_block);
451 static void pre_insert_copy_insn (struct expr *, rtx);
452 static void pre_insert_copies (void);
453 static int pre_delete (void);
454 static int pre_gcse (void);
455 static int one_pre_gcse_pass (void);
456 static void add_label_notes (rtx, rtx);
457 static void alloc_code_hoist_mem (int, int);
458 static void free_code_hoist_mem (void);
459 static void compute_code_hoist_vbeinout (void);
460 static void compute_code_hoist_data (void);
461 static int hoist_expr_reaches_here_p (basic_block, int, basic_block, char *,
462 int, int *);
463 static int hoist_code (void);
464 static int one_code_hoisting_pass (void);
465 static rtx process_insert_insn (struct expr *);
466 static int pre_edge_insert (struct edge_list *, struct expr **);
467 static int pre_expr_reaches_here_p_work (basic_block, struct expr *,
468 basic_block, char *);
469 static struct ls_expr * ldst_entry (rtx);
470 static void free_ldst_entry (struct ls_expr *);
471 static void free_ldst_mems (void);
472 static void print_ldst_list (FILE *);
473 static struct ls_expr * find_rtx_in_ldst (rtx);
474 static inline struct ls_expr * first_ls_expr (void);
475 static inline struct ls_expr * next_ls_expr (struct ls_expr *);
476 static int simple_mem (const_rtx);
477 static void invalidate_any_buried_refs (rtx);
478 static void compute_ld_motion_mems (void);
479 static void trim_ld_motion_mems (void);
480 static void update_ld_motion_stores (struct expr *);
481 static void free_insn_expr_list_list (rtx *);
482 static void clear_modify_mem_tables (void);
483 static void free_modify_mem_tables (void);
484 static rtx gcse_emit_move_after (rtx, rtx, rtx);
485 static bool is_too_expensive (const char *);
487 #define GNEW(T) ((T *) gmalloc (sizeof (T)))
488 #define GCNEW(T) ((T *) gcalloc (1, sizeof (T)))
490 #define GNEWVEC(T, N) ((T *) gmalloc (sizeof (T) * (N)))
491 #define GCNEWVEC(T, N) ((T *) gcalloc ((N), sizeof (T)))
493 #define GNEWVAR(T, S) ((T *) gmalloc ((S)))
494 #define GCNEWVAR(T, S) ((T *) gcalloc (1, (S)))
496 #define GOBNEW(T) ((T *) gcse_alloc (sizeof (T)))
497 #define GOBNEWVAR(T, S) ((T *) gcse_alloc ((S)))
499 /* Misc. utilities. */
501 #define can_copy \
502 (this_target_gcse->x_can_copy)
503 #define can_copy_init_p \
504 (this_target_gcse->x_can_copy_init_p)
506 /* Compute which modes support reg/reg copy operations. */
508 static void
509 compute_can_copy (void)
511 int i;
512 #ifndef AVOID_CCMODE_COPIES
513 rtx reg, insn;
514 #endif
515 memset (can_copy, 0, NUM_MACHINE_MODES);
517 start_sequence ();
518 for (i = 0; i < NUM_MACHINE_MODES; i++)
519 if (GET_MODE_CLASS (i) == MODE_CC)
521 #ifdef AVOID_CCMODE_COPIES
522 can_copy[i] = 0;
523 #else
524 reg = gen_rtx_REG ((enum machine_mode) i, LAST_VIRTUAL_REGISTER + 1);
525 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, reg));
526 if (recog (PATTERN (insn), insn, NULL) >= 0)
527 can_copy[i] = 1;
528 #endif
530 else
531 can_copy[i] = 1;
533 end_sequence ();
536 /* Returns whether the mode supports reg/reg copy operations. */
538 bool
539 can_copy_p (enum machine_mode mode)
541 if (! can_copy_init_p)
543 compute_can_copy ();
544 can_copy_init_p = true;
547 return can_copy[mode] != 0;
551 /* Cover function to xmalloc to record bytes allocated. */
553 static void *
554 gmalloc (size_t size)
556 bytes_used += size;
557 return xmalloc (size);
560 /* Cover function to xcalloc to record bytes allocated. */
562 static void *
563 gcalloc (size_t nelem, size_t elsize)
565 bytes_used += nelem * elsize;
566 return xcalloc (nelem, elsize);
569 /* Cover function to obstack_alloc. */
571 static void *
572 gcse_alloc (unsigned long size)
574 bytes_used += size;
575 return obstack_alloc (&gcse_obstack, size);
578 /* Allocate memory for the reg/memory set tracking tables.
579 This is called at the start of each pass. */
581 static void
582 alloc_gcse_mem (void)
584 /* Allocate vars to track sets of regs. */
585 reg_set_bitmap = ALLOC_REG_SET (NULL);
587 /* Allocate array to keep a list of insns which modify memory in each
588 basic block. */
589 modify_mem_list = GCNEWVEC (rtx, last_basic_block);
590 canon_modify_mem_list = GCNEWVEC (rtx, last_basic_block);
591 modify_mem_list_set = BITMAP_ALLOC (NULL);
592 blocks_with_calls = BITMAP_ALLOC (NULL);
595 /* Free memory allocated by alloc_gcse_mem. */
597 static void
598 free_gcse_mem (void)
600 FREE_REG_SET (reg_set_bitmap);
602 free_modify_mem_tables ();
603 BITMAP_FREE (modify_mem_list_set);
604 BITMAP_FREE (blocks_with_calls);
607 /* Compute the local properties of each recorded expression.
609 Local properties are those that are defined by the block, irrespective of
610 other blocks.
612 An expression is transparent in a block if its operands are not modified
613 in the block.
615 An expression is computed (locally available) in a block if it is computed
616 at least once and expression would contain the same value if the
617 computation was moved to the end of the block.
619 An expression is locally anticipatable in a block if it is computed at
620 least once and expression would contain the same value if the computation
621 was moved to the beginning of the block.
623 We call this routine for pre and code hoisting. They all compute
624 basically the same information and thus can easily share this code.
626 TRANSP, COMP, and ANTLOC are destination sbitmaps for recording local
627 properties. If NULL, then it is not necessary to compute or record that
628 particular property.
630 TABLE controls which hash table to look at. */
632 static void
633 compute_local_properties (sbitmap *transp, sbitmap *comp, sbitmap *antloc,
634 struct hash_table_d *table)
636 unsigned int i;
638 /* Initialize any bitmaps that were passed in. */
639 if (transp)
641 sbitmap_vector_ones (transp, last_basic_block);
644 if (comp)
645 sbitmap_vector_zero (comp, last_basic_block);
646 if (antloc)
647 sbitmap_vector_zero (antloc, last_basic_block);
649 for (i = 0; i < table->size; i++)
651 struct expr *expr;
653 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
655 int indx = expr->bitmap_index;
656 struct occr *occr;
658 /* The expression is transparent in this block if it is not killed.
659 We start by assuming all are transparent [none are killed], and
660 then reset the bits for those that are. */
661 if (transp)
662 compute_transp (expr->expr, indx, transp);
664 /* The occurrences recorded in antic_occr are exactly those that
665 we want to set to nonzero in ANTLOC. */
666 if (antloc)
667 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
669 SET_BIT (antloc[BLOCK_FOR_INSN (occr->insn)->index], indx);
671 /* While we're scanning the table, this is a good place to
672 initialize this. */
673 occr->deleted_p = 0;
676 /* The occurrences recorded in avail_occr are exactly those that
677 we want to set to nonzero in COMP. */
678 if (comp)
679 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
681 SET_BIT (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
683 /* While we're scanning the table, this is a good place to
684 initialize this. */
685 occr->copied_p = 0;
688 /* While we're scanning the table, this is a good place to
689 initialize this. */
690 expr->reaching_reg = 0;
695 /* Hash table support. */
697 struct reg_avail_info
699 basic_block last_bb;
700 int first_set;
701 int last_set;
704 static struct reg_avail_info *reg_avail_info;
705 static basic_block current_bb;
708 /* See whether X, the source of a set, is something we want to consider for
709 GCSE. */
711 static int
712 want_to_gcse_p (rtx x, int *max_distance_ptr)
714 #ifdef STACK_REGS
715 /* On register stack architectures, don't GCSE constants from the
716 constant pool, as the benefits are often swamped by the overhead
717 of shuffling the register stack between basic blocks. */
718 if (IS_STACK_MODE (GET_MODE (x)))
719 x = avoid_constant_pool_reference (x);
720 #endif
722 /* GCSE'ing constants:
724 We do not specifically distinguish between constant and non-constant
725 expressions in PRE and Hoist. We use rtx_cost below to limit
726 the maximum distance simple expressions can travel.
728 Nevertheless, constants are much easier to GCSE, and, hence,
729 it is easy to overdo the optimizations. Usually, excessive PRE and
730 Hoisting of constant leads to increased register pressure.
732 RA can deal with this by rematerialing some of the constants.
733 Therefore, it is important that the back-end generates sets of constants
734 in a way that allows reload rematerialize them under high register
735 pressure, i.e., a pseudo register with REG_EQUAL to constant
736 is set only once. Failing to do so will result in IRA/reload
737 spilling such constants under high register pressure instead of
738 rematerializing them. */
740 switch (GET_CODE (x))
742 case REG:
743 case SUBREG:
744 case CALL:
745 return 0;
747 case CONST_INT:
748 case CONST_DOUBLE:
749 case CONST_FIXED:
750 case CONST_VECTOR:
751 if (!doing_code_hoisting_p)
752 /* Do not PRE constants. */
753 return 0;
755 /* FALLTHRU */
757 default:
758 if (doing_code_hoisting_p)
759 /* PRE doesn't implement max_distance restriction. */
761 int cost;
762 int max_distance;
764 gcc_assert (!optimize_function_for_speed_p (cfun)
765 && optimize_function_for_size_p (cfun));
766 cost = rtx_cost (x, SET, 0);
768 if (cost < COSTS_N_INSNS (GCSE_UNRESTRICTED_COST))
770 max_distance = (GCSE_COST_DISTANCE_RATIO * cost) / 10;
771 if (max_distance == 0)
772 return 0;
774 gcc_assert (max_distance > 0);
776 else
777 max_distance = 0;
779 if (max_distance_ptr)
780 *max_distance_ptr = max_distance;
783 return can_assign_to_reg_without_clobbers_p (x);
787 /* Used internally by can_assign_to_reg_without_clobbers_p. */
789 static GTY(()) rtx test_insn;
791 /* Return true if we can assign X to a pseudo register such that the
792 resulting insn does not result in clobbering a hard register as a
793 side-effect.
795 Additionally, if the target requires it, check that the resulting insn
796 can be copied. If it cannot, this means that X is special and probably
797 has hidden side-effects we don't want to mess with.
799 This function is typically used by code motion passes, to verify
800 that it is safe to insert an insn without worrying about clobbering
801 maybe live hard regs. */
803 bool
804 can_assign_to_reg_without_clobbers_p (rtx x)
806 int num_clobbers = 0;
807 int icode;
809 /* If this is a valid operand, we are OK. If it's VOIDmode, we aren't. */
810 if (general_operand (x, GET_MODE (x)))
811 return 1;
812 else if (GET_MODE (x) == VOIDmode)
813 return 0;
815 /* Otherwise, check if we can make a valid insn from it. First initialize
816 our test insn if we haven't already. */
817 if (test_insn == 0)
819 test_insn
820 = make_insn_raw (gen_rtx_SET (VOIDmode,
821 gen_rtx_REG (word_mode,
822 FIRST_PSEUDO_REGISTER * 2),
823 const0_rtx));
824 NEXT_INSN (test_insn) = PREV_INSN (test_insn) = 0;
827 /* Now make an insn like the one we would make when GCSE'ing and see if
828 valid. */
829 PUT_MODE (SET_DEST (PATTERN (test_insn)), GET_MODE (x));
830 SET_SRC (PATTERN (test_insn)) = x;
832 icode = recog (PATTERN (test_insn), test_insn, &num_clobbers);
833 if (icode < 0)
834 return false;
836 if (num_clobbers > 0 && added_clobbers_hard_reg_p (icode))
837 return false;
839 if (targetm.cannot_copy_insn_p && targetm.cannot_copy_insn_p (test_insn))
840 return false;
842 return true;
845 /* Return nonzero if the operands of expression X are unchanged from the
846 start of INSN's basic block up to but not including INSN (if AVAIL_P == 0),
847 or from INSN to the end of INSN's basic block (if AVAIL_P != 0). */
849 static int
850 oprs_unchanged_p (const_rtx x, const_rtx insn, int avail_p)
852 int i, j;
853 enum rtx_code code;
854 const char *fmt;
856 if (x == 0)
857 return 1;
859 code = GET_CODE (x);
860 switch (code)
862 case REG:
864 struct reg_avail_info *info = &reg_avail_info[REGNO (x)];
866 if (info->last_bb != current_bb)
867 return 1;
868 if (avail_p)
869 return info->last_set < DF_INSN_LUID (insn);
870 else
871 return info->first_set >= DF_INSN_LUID (insn);
874 case MEM:
875 if (load_killed_in_block_p (current_bb, DF_INSN_LUID (insn),
876 x, avail_p))
877 return 0;
878 else
879 return oprs_unchanged_p (XEXP (x, 0), insn, avail_p);
881 case PRE_DEC:
882 case PRE_INC:
883 case POST_DEC:
884 case POST_INC:
885 case PRE_MODIFY:
886 case POST_MODIFY:
887 return 0;
889 case PC:
890 case CC0: /*FIXME*/
891 case CONST:
892 case CONST_INT:
893 case CONST_DOUBLE:
894 case CONST_FIXED:
895 case CONST_VECTOR:
896 case SYMBOL_REF:
897 case LABEL_REF:
898 case ADDR_VEC:
899 case ADDR_DIFF_VEC:
900 return 1;
902 default:
903 break;
906 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
908 if (fmt[i] == 'e')
910 /* If we are about to do the last recursive call needed at this
911 level, change it into iteration. This function is called enough
912 to be worth it. */
913 if (i == 0)
914 return oprs_unchanged_p (XEXP (x, i), insn, avail_p);
916 else if (! oprs_unchanged_p (XEXP (x, i), insn, avail_p))
917 return 0;
919 else if (fmt[i] == 'E')
920 for (j = 0; j < XVECLEN (x, i); j++)
921 if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, avail_p))
922 return 0;
925 return 1;
928 /* Used for communication between mems_conflict_for_gcse_p and
929 load_killed_in_block_p. Nonzero if mems_conflict_for_gcse_p finds a
930 conflict between two memory references. */
931 static int gcse_mems_conflict_p;
933 /* Used for communication between mems_conflict_for_gcse_p and
934 load_killed_in_block_p. A memory reference for a load instruction,
935 mems_conflict_for_gcse_p will see if a memory store conflicts with
936 this memory load. */
937 static const_rtx gcse_mem_operand;
939 /* DEST is the output of an instruction. If it is a memory reference, and
940 possibly conflicts with the load found in gcse_mem_operand, then set
941 gcse_mems_conflict_p to a nonzero value. */
943 static void
944 mems_conflict_for_gcse_p (rtx dest, const_rtx setter ATTRIBUTE_UNUSED,
945 void *data ATTRIBUTE_UNUSED)
947 while (GET_CODE (dest) == SUBREG
948 || GET_CODE (dest) == ZERO_EXTRACT
949 || GET_CODE (dest) == STRICT_LOW_PART)
950 dest = XEXP (dest, 0);
952 /* If DEST is not a MEM, then it will not conflict with the load. Note
953 that function calls are assumed to clobber memory, but are handled
954 elsewhere. */
955 if (! MEM_P (dest))
956 return;
958 /* If we are setting a MEM in our list of specially recognized MEMs,
959 don't mark as killed this time. */
961 if (expr_equiv_p (dest, gcse_mem_operand) && pre_ldst_mems != NULL)
963 if (!find_rtx_in_ldst (dest))
964 gcse_mems_conflict_p = 1;
965 return;
968 if (true_dependence (dest, GET_MODE (dest), gcse_mem_operand,
969 rtx_addr_varies_p))
970 gcse_mems_conflict_p = 1;
973 /* Return nonzero if the expression in X (a memory reference) is killed
974 in block BB before or after the insn with the LUID in UID_LIMIT.
975 AVAIL_P is nonzero for kills after UID_LIMIT, and zero for kills
976 before UID_LIMIT.
978 To check the entire block, set UID_LIMIT to max_uid + 1 and
979 AVAIL_P to 0. */
981 static int
982 load_killed_in_block_p (const_basic_block bb, int uid_limit, const_rtx x, int avail_p)
984 rtx list_entry = modify_mem_list[bb->index];
986 /* If this is a readonly then we aren't going to be changing it. */
987 if (MEM_READONLY_P (x))
988 return 0;
990 while (list_entry)
992 rtx setter;
993 /* Ignore entries in the list that do not apply. */
994 if ((avail_p
995 && DF_INSN_LUID (XEXP (list_entry, 0)) < uid_limit)
996 || (! avail_p
997 && DF_INSN_LUID (XEXP (list_entry, 0)) > uid_limit))
999 list_entry = XEXP (list_entry, 1);
1000 continue;
1003 setter = XEXP (list_entry, 0);
1005 /* If SETTER is a call everything is clobbered. Note that calls
1006 to pure functions are never put on the list, so we need not
1007 worry about them. */
1008 if (CALL_P (setter))
1009 return 1;
1011 /* SETTER must be an INSN of some kind that sets memory. Call
1012 note_stores to examine each hunk of memory that is modified.
1014 The note_stores interface is pretty limited, so we have to
1015 communicate via global variables. Yuk. */
1016 gcse_mem_operand = x;
1017 gcse_mems_conflict_p = 0;
1018 note_stores (PATTERN (setter), mems_conflict_for_gcse_p, NULL);
1019 if (gcse_mems_conflict_p)
1020 return 1;
1021 list_entry = XEXP (list_entry, 1);
1023 return 0;
1026 /* Return nonzero if the operands of expression X are unchanged from
1027 the start of INSN's basic block up to but not including INSN. */
1029 static int
1030 oprs_anticipatable_p (const_rtx x, const_rtx insn)
1032 return oprs_unchanged_p (x, insn, 0);
1035 /* Return nonzero if the operands of expression X are unchanged from
1036 INSN to the end of INSN's basic block. */
1038 static int
1039 oprs_available_p (const_rtx x, const_rtx insn)
1041 return oprs_unchanged_p (x, insn, 1);
1044 /* Hash expression X.
1046 MODE is only used if X is a CONST_INT. DO_NOT_RECORD_P is a boolean
1047 indicating if a volatile operand is found or if the expression contains
1048 something we don't want to insert in the table. HASH_TABLE_SIZE is
1049 the current size of the hash table to be probed. */
1051 static unsigned int
1052 hash_expr (const_rtx x, enum machine_mode mode, int *do_not_record_p,
1053 int hash_table_size)
1055 unsigned int hash;
1057 *do_not_record_p = 0;
1059 hash = hash_rtx (x, mode, do_not_record_p,
1060 NULL, /*have_reg_qty=*/false);
1061 return hash % hash_table_size;
1064 /* Return nonzero if exp1 is equivalent to exp2. */
1066 static int
1067 expr_equiv_p (const_rtx x, const_rtx y)
1069 return exp_equiv_p (x, y, 0, true);
1072 /* Insert expression X in INSN in the hash TABLE.
1073 If it is already present, record it as the last occurrence in INSN's
1074 basic block.
1076 MODE is the mode of the value X is being stored into.
1077 It is only used if X is a CONST_INT.
1079 ANTIC_P is nonzero if X is an anticipatable expression.
1080 AVAIL_P is nonzero if X is an available expression.
1082 MAX_DISTANCE is the maximum distance in instructions this expression can
1083 be moved. */
1085 static void
1086 insert_expr_in_table (rtx x, enum machine_mode mode, rtx insn, int antic_p,
1087 int avail_p, int max_distance, struct hash_table_d *table)
1089 int found, do_not_record_p;
1090 unsigned int hash;
1091 struct expr *cur_expr, *last_expr = NULL;
1092 struct occr *antic_occr, *avail_occr;
1094 hash = hash_expr (x, mode, &do_not_record_p, table->size);
1096 /* Do not insert expression in table if it contains volatile operands,
1097 or if hash_expr determines the expression is something we don't want
1098 to or can't handle. */
1099 if (do_not_record_p)
1100 return;
1102 cur_expr = table->table[hash];
1103 found = 0;
1105 while (cur_expr && 0 == (found = expr_equiv_p (cur_expr->expr, x)))
1107 /* If the expression isn't found, save a pointer to the end of
1108 the list. */
1109 last_expr = cur_expr;
1110 cur_expr = cur_expr->next_same_hash;
1113 if (! found)
1115 cur_expr = GOBNEW (struct expr);
1116 bytes_used += sizeof (struct expr);
1117 if (table->table[hash] == NULL)
1118 /* This is the first pattern that hashed to this index. */
1119 table->table[hash] = cur_expr;
1120 else
1121 /* Add EXPR to end of this hash chain. */
1122 last_expr->next_same_hash = cur_expr;
1124 /* Set the fields of the expr element. */
1125 cur_expr->expr = x;
1126 cur_expr->bitmap_index = table->n_elems++;
1127 cur_expr->next_same_hash = NULL;
1128 cur_expr->antic_occr = NULL;
1129 cur_expr->avail_occr = NULL;
1130 gcc_assert (max_distance >= 0);
1131 cur_expr->max_distance = max_distance;
1133 else
1134 gcc_assert (cur_expr->max_distance == max_distance);
1136 /* Now record the occurrence(s). */
1137 if (antic_p)
1139 antic_occr = cur_expr->antic_occr;
1141 if (antic_occr
1142 && BLOCK_FOR_INSN (antic_occr->insn) != BLOCK_FOR_INSN (insn))
1143 antic_occr = NULL;
1145 if (antic_occr)
1146 /* Found another instance of the expression in the same basic block.
1147 Prefer the currently recorded one. We want the first one in the
1148 block and the block is scanned from start to end. */
1149 ; /* nothing to do */
1150 else
1152 /* First occurrence of this expression in this basic block. */
1153 antic_occr = GOBNEW (struct occr);
1154 bytes_used += sizeof (struct occr);
1155 antic_occr->insn = insn;
1156 antic_occr->next = cur_expr->antic_occr;
1157 antic_occr->deleted_p = 0;
1158 cur_expr->antic_occr = antic_occr;
1162 if (avail_p)
1164 avail_occr = cur_expr->avail_occr;
1166 if (avail_occr
1167 && BLOCK_FOR_INSN (avail_occr->insn) == BLOCK_FOR_INSN (insn))
1169 /* Found another instance of the expression in the same basic block.
1170 Prefer this occurrence to the currently recorded one. We want
1171 the last one in the block and the block is scanned from start
1172 to end. */
1173 avail_occr->insn = insn;
1175 else
1177 /* First occurrence of this expression in this basic block. */
1178 avail_occr = GOBNEW (struct occr);
1179 bytes_used += sizeof (struct occr);
1180 avail_occr->insn = insn;
1181 avail_occr->next = cur_expr->avail_occr;
1182 avail_occr->deleted_p = 0;
1183 cur_expr->avail_occr = avail_occr;
1188 /* Scan pattern PAT of INSN and add an entry to the hash TABLE. */
1190 static void
1191 hash_scan_set (rtx pat, rtx insn, struct hash_table_d *table)
1193 rtx src = SET_SRC (pat);
1194 rtx dest = SET_DEST (pat);
1195 rtx note;
1197 if (GET_CODE (src) == CALL)
1198 hash_scan_call (src, insn, table);
1200 else if (REG_P (dest))
1202 unsigned int regno = REGNO (dest);
1203 int max_distance = 0;
1205 /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
1207 This allows us to do a single GCSE pass and still eliminate
1208 redundant constants, addresses or other expressions that are
1209 constructed with multiple instructions.
1211 However, keep the original SRC if INSN is a simple reg-reg move.
1212 In this case, there will almost always be a REG_EQUAL note on the
1213 insn that sets SRC. By recording the REG_EQUAL value here as SRC
1214 for INSN, we miss copy propagation opportunities and we perform the
1215 same PRE GCSE operation repeatedly on the same REG_EQUAL value if we
1216 do more than one PRE GCSE pass.
1218 Note that this does not impede profitable constant propagations. We
1219 "look through" reg-reg sets in lookup_avail_set. */
1220 note = find_reg_equal_equiv_note (insn);
1221 if (note != 0
1222 && REG_NOTE_KIND (note) == REG_EQUAL
1223 && !REG_P (src)
1224 && want_to_gcse_p (XEXP (note, 0), NULL))
1225 src = XEXP (note, 0), pat = gen_rtx_SET (VOIDmode, dest, src);
1227 /* Only record sets of pseudo-regs in the hash table. */
1228 if (regno >= FIRST_PSEUDO_REGISTER
1229 /* Don't GCSE something if we can't do a reg/reg copy. */
1230 && can_copy_p (GET_MODE (dest))
1231 /* GCSE commonly inserts instruction after the insn. We can't
1232 do that easily for EH edges so disable GCSE on these for now. */
1233 /* ??? We can now easily create new EH landing pads at the
1234 gimple level, for splitting edges; there's no reason we
1235 can't do the same thing at the rtl level. */
1236 && !can_throw_internal (insn)
1237 /* Is SET_SRC something we want to gcse? */
1238 && want_to_gcse_p (src, &max_distance)
1239 /* Don't CSE a nop. */
1240 && ! set_noop_p (pat)
1241 /* Don't GCSE if it has attached REG_EQUIV note.
1242 At this point this only function parameters should have
1243 REG_EQUIV notes and if the argument slot is used somewhere
1244 explicitly, it means address of parameter has been taken,
1245 so we should not extend the lifetime of the pseudo. */
1246 && (note == NULL_RTX || ! MEM_P (XEXP (note, 0))))
1248 /* An expression is not anticipatable if its operands are
1249 modified before this insn or if this is not the only SET in
1250 this insn. The latter condition does not have to mean that
1251 SRC itself is not anticipatable, but we just will not be
1252 able to handle code motion of insns with multiple sets. */
1253 int antic_p = oprs_anticipatable_p (src, insn)
1254 && !multiple_sets (insn);
1255 /* An expression is not available if its operands are
1256 subsequently modified, including this insn. It's also not
1257 available if this is a branch, because we can't insert
1258 a set after the branch. */
1259 int avail_p = (oprs_available_p (src, insn)
1260 && ! JUMP_P (insn));
1262 insert_expr_in_table (src, GET_MODE (dest), insn, antic_p, avail_p,
1263 max_distance, table);
1266 /* In case of store we want to consider the memory value as available in
1267 the REG stored in that memory. This makes it possible to remove
1268 redundant loads from due to stores to the same location. */
1269 else if (flag_gcse_las && REG_P (src) && MEM_P (dest))
1271 unsigned int regno = REGNO (src);
1272 int max_distance = 0;
1274 /* Only record sets of pseudo-regs in the hash table. */
1275 if (regno >= FIRST_PSEUDO_REGISTER
1276 /* Don't GCSE something if we can't do a reg/reg copy. */
1277 && can_copy_p (GET_MODE (src))
1278 /* GCSE commonly inserts instruction after the insn. We can't
1279 do that easily for EH edges so disable GCSE on these for now. */
1280 && !can_throw_internal (insn)
1281 /* Is SET_DEST something we want to gcse? */
1282 && want_to_gcse_p (dest, &max_distance)
1283 /* Don't CSE a nop. */
1284 && ! set_noop_p (pat)
1285 /* Don't GCSE if it has attached REG_EQUIV note.
1286 At this point this only function parameters should have
1287 REG_EQUIV notes and if the argument slot is used somewhere
1288 explicitly, it means address of parameter has been taken,
1289 so we should not extend the lifetime of the pseudo. */
1290 && ((note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) == 0
1291 || ! MEM_P (XEXP (note, 0))))
1293 /* Stores are never anticipatable. */
1294 int antic_p = 0;
1295 /* An expression is not available if its operands are
1296 subsequently modified, including this insn. It's also not
1297 available if this is a branch, because we can't insert
1298 a set after the branch. */
1299 int avail_p = oprs_available_p (dest, insn)
1300 && ! JUMP_P (insn);
1302 /* Record the memory expression (DEST) in the hash table. */
1303 insert_expr_in_table (dest, GET_MODE (dest), insn,
1304 antic_p, avail_p, max_distance, table);
1309 static void
1310 hash_scan_clobber (rtx x ATTRIBUTE_UNUSED, rtx insn ATTRIBUTE_UNUSED,
1311 struct hash_table_d *table ATTRIBUTE_UNUSED)
1313 /* Currently nothing to do. */
1316 static void
1317 hash_scan_call (rtx x ATTRIBUTE_UNUSED, rtx insn ATTRIBUTE_UNUSED,
1318 struct hash_table_d *table ATTRIBUTE_UNUSED)
1320 /* Currently nothing to do. */
1323 /* Process INSN and add hash table entries as appropriate.
1325 Only available expressions that set a single pseudo-reg are recorded.
1327 Single sets in a PARALLEL could be handled, but it's an extra complication
1328 that isn't dealt with right now. The trick is handling the CLOBBERs that
1329 are also in the PARALLEL. Later.
1331 If SET_P is nonzero, this is for the assignment hash table,
1332 otherwise it is for the expression hash table. */
1334 static void
1335 hash_scan_insn (rtx insn, struct hash_table_d *table)
1337 rtx pat = PATTERN (insn);
1338 int i;
1340 /* Pick out the sets of INSN and for other forms of instructions record
1341 what's been modified. */
1343 if (GET_CODE (pat) == SET)
1344 hash_scan_set (pat, insn, table);
1345 else if (GET_CODE (pat) == PARALLEL)
1346 for (i = 0; i < XVECLEN (pat, 0); i++)
1348 rtx x = XVECEXP (pat, 0, i);
1350 if (GET_CODE (x) == SET)
1351 hash_scan_set (x, insn, table);
1352 else if (GET_CODE (x) == CLOBBER)
1353 hash_scan_clobber (x, insn, table);
1354 else if (GET_CODE (x) == CALL)
1355 hash_scan_call (x, insn, table);
1358 else if (GET_CODE (pat) == CLOBBER)
1359 hash_scan_clobber (pat, insn, table);
1360 else if (GET_CODE (pat) == CALL)
1361 hash_scan_call (pat, insn, table);
1364 static void
1365 dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
1367 int i;
1368 /* Flattened out table, so it's printed in proper order. */
1369 struct expr **flat_table;
1370 unsigned int *hash_val;
1371 struct expr *expr;
1373 flat_table = XCNEWVEC (struct expr *, table->n_elems);
1374 hash_val = XNEWVEC (unsigned int, table->n_elems);
1376 for (i = 0; i < (int) table->size; i++)
1377 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
1379 flat_table[expr->bitmap_index] = expr;
1380 hash_val[expr->bitmap_index] = i;
1383 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
1384 name, table->size, table->n_elems);
1386 for (i = 0; i < (int) table->n_elems; i++)
1387 if (flat_table[i] != 0)
1389 expr = flat_table[i];
1390 fprintf (file, "Index %d (hash value %d; max distance %d)\n ",
1391 expr->bitmap_index, hash_val[i], expr->max_distance);
1392 print_rtl (file, expr->expr);
1393 fprintf (file, "\n");
1396 fprintf (file, "\n");
1398 free (flat_table);
1399 free (hash_val);
1402 /* Record register first/last/block set information for REGNO in INSN.
1404 first_set records the first place in the block where the register
1405 is set and is used to compute "anticipatability".
1407 last_set records the last place in the block where the register
1408 is set and is used to compute "availability".
1410 last_bb records the block for which first_set and last_set are
1411 valid, as a quick test to invalidate them. */
1413 static void
1414 record_last_reg_set_info (rtx insn, int regno)
1416 struct reg_avail_info *info = &reg_avail_info[regno];
1417 int luid = DF_INSN_LUID (insn);
1419 info->last_set = luid;
1420 if (info->last_bb != current_bb)
1422 info->last_bb = current_bb;
1423 info->first_set = luid;
1428 /* Record all of the canonicalized MEMs of record_last_mem_set_info's insn.
1429 Note we store a pair of elements in the list, so they have to be
1430 taken off pairwise. */
1432 static void
1433 canon_list_insert (rtx dest ATTRIBUTE_UNUSED, const_rtx unused1 ATTRIBUTE_UNUSED,
1434 void * v_insn)
1436 rtx dest_addr, insn;
1437 int bb;
1439 while (GET_CODE (dest) == SUBREG
1440 || GET_CODE (dest) == ZERO_EXTRACT
1441 || GET_CODE (dest) == STRICT_LOW_PART)
1442 dest = XEXP (dest, 0);
1444 /* If DEST is not a MEM, then it will not conflict with a load. Note
1445 that function calls are assumed to clobber memory, but are handled
1446 elsewhere. */
1448 if (! MEM_P (dest))
1449 return;
1451 dest_addr = get_addr (XEXP (dest, 0));
1452 dest_addr = canon_rtx (dest_addr);
1453 insn = (rtx) v_insn;
1454 bb = BLOCK_FOR_INSN (insn)->index;
1456 canon_modify_mem_list[bb] =
1457 alloc_EXPR_LIST (VOIDmode, dest_addr, canon_modify_mem_list[bb]);
1458 canon_modify_mem_list[bb] =
1459 alloc_EXPR_LIST (VOIDmode, dest, canon_modify_mem_list[bb]);
1462 /* Record memory modification information for INSN. We do not actually care
1463 about the memory location(s) that are set, or even how they are set (consider
1464 a CALL_INSN). We merely need to record which insns modify memory. */
1466 static void
1467 record_last_mem_set_info (rtx insn)
1469 int bb = BLOCK_FOR_INSN (insn)->index;
1471 /* load_killed_in_block_p will handle the case of calls clobbering
1472 everything. */
1473 modify_mem_list[bb] = alloc_INSN_LIST (insn, modify_mem_list[bb]);
1474 bitmap_set_bit (modify_mem_list_set, bb);
1476 if (CALL_P (insn))
1478 /* Note that traversals of this loop (other than for free-ing)
1479 will break after encountering a CALL_INSN. So, there's no
1480 need to insert a pair of items, as canon_list_insert does. */
1481 canon_modify_mem_list[bb] =
1482 alloc_INSN_LIST (insn, canon_modify_mem_list[bb]);
1483 bitmap_set_bit (blocks_with_calls, bb);
1485 else
1486 note_stores (PATTERN (insn), canon_list_insert, (void*) insn);
1489 /* Called from compute_hash_table via note_stores to handle one
1490 SET or CLOBBER in an insn. DATA is really the instruction in which
1491 the SET is taking place. */
1493 static void
1494 record_last_set_info (rtx dest, const_rtx setter ATTRIBUTE_UNUSED, void *data)
1496 rtx last_set_insn = (rtx) data;
1498 if (GET_CODE (dest) == SUBREG)
1499 dest = SUBREG_REG (dest);
1501 if (REG_P (dest))
1502 record_last_reg_set_info (last_set_insn, REGNO (dest));
1503 else if (MEM_P (dest)
1504 /* Ignore pushes, they clobber nothing. */
1505 && ! push_operand (dest, GET_MODE (dest)))
1506 record_last_mem_set_info (last_set_insn);
1509 /* Top level function to create an expression hash table.
1511 Expression entries are placed in the hash table if
1512 - they are of the form (set (pseudo-reg) src),
1513 - src is something we want to perform GCSE on,
1514 - none of the operands are subsequently modified in the block
1516 Currently src must be a pseudo-reg or a const_int.
1518 TABLE is the table computed. */
1520 static void
1521 compute_hash_table_work (struct hash_table_d *table)
1523 int i;
1525 /* re-Cache any INSN_LIST nodes we have allocated. */
1526 clear_modify_mem_tables ();
1527 /* Some working arrays used to track first and last set in each block. */
1528 reg_avail_info = GNEWVEC (struct reg_avail_info, max_reg_num ());
1530 for (i = 0; i < max_reg_num (); ++i)
1531 reg_avail_info[i].last_bb = NULL;
1533 FOR_EACH_BB (current_bb)
1535 rtx insn;
1536 unsigned int regno;
1538 /* First pass over the instructions records information used to
1539 determine when registers and memory are first and last set. */
1540 FOR_BB_INSNS (current_bb, insn)
1542 if (!NONDEBUG_INSN_P (insn))
1543 continue;
1545 if (CALL_P (insn))
1547 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1548 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
1549 record_last_reg_set_info (insn, regno);
1551 if (! RTL_CONST_OR_PURE_CALL_P (insn))
1552 record_last_mem_set_info (insn);
1555 note_stores (PATTERN (insn), record_last_set_info, insn);
1558 /* The next pass builds the hash table. */
1559 FOR_BB_INSNS (current_bb, insn)
1560 if (NONDEBUG_INSN_P (insn))
1561 hash_scan_insn (insn, table);
1564 free (reg_avail_info);
1565 reg_avail_info = NULL;
1568 /* Allocate space for the set/expr hash TABLE.
1569 It is used to determine the number of buckets to use. */
1571 static void
1572 alloc_hash_table (struct hash_table_d *table)
1574 int n;
1576 n = get_max_insn_count ();
1578 table->size = n / 4;
1579 if (table->size < 11)
1580 table->size = 11;
1582 /* Attempt to maintain efficient use of hash table.
1583 Making it an odd number is simplest for now.
1584 ??? Later take some measurements. */
1585 table->size |= 1;
1586 n = table->size * sizeof (struct expr *);
1587 table->table = GNEWVAR (struct expr *, n);
1590 /* Free things allocated by alloc_hash_table. */
1592 static void
1593 free_hash_table (struct hash_table_d *table)
1595 free (table->table);
1598 /* Compute the expression hash table TABLE. */
1600 static void
1601 compute_hash_table (struct hash_table_d *table)
1603 /* Initialize count of number of entries in hash table. */
1604 table->n_elems = 0;
1605 memset (table->table, 0, table->size * sizeof (struct expr *));
1607 compute_hash_table_work (table);
1610 /* Expression tracking support. */
1612 /* Like free_INSN_LIST_list or free_EXPR_LIST_list, except that the node
1613 types may be mixed. */
1615 static void
1616 free_insn_expr_list_list (rtx *listp)
1618 rtx list, next;
1620 for (list = *listp; list ; list = next)
1622 next = XEXP (list, 1);
1623 if (GET_CODE (list) == EXPR_LIST)
1624 free_EXPR_LIST_node (list);
1625 else
1626 free_INSN_LIST_node (list);
1629 *listp = NULL;
1632 /* Clear canon_modify_mem_list and modify_mem_list tables. */
1633 static void
1634 clear_modify_mem_tables (void)
1636 unsigned i;
1637 bitmap_iterator bi;
1639 EXECUTE_IF_SET_IN_BITMAP (modify_mem_list_set, 0, i, bi)
1641 free_INSN_LIST_list (modify_mem_list + i);
1642 free_insn_expr_list_list (canon_modify_mem_list + i);
1644 bitmap_clear (modify_mem_list_set);
1645 bitmap_clear (blocks_with_calls);
1648 /* Release memory used by modify_mem_list_set. */
1650 static void
1651 free_modify_mem_tables (void)
1653 clear_modify_mem_tables ();
1654 free (modify_mem_list);
1655 free (canon_modify_mem_list);
1656 modify_mem_list = 0;
1657 canon_modify_mem_list = 0;
1661 /* For each block, compute whether X is transparent. X is either an
1662 expression or an assignment [though we don't care which, for this context
1663 an assignment is treated as an expression]. For each block where an
1664 element of X is modified, reset the INDX bit in BMAP. */
1666 static void
1667 compute_transp (const_rtx x, int indx, sbitmap *bmap)
1669 int i, j;
1670 enum rtx_code code;
1671 const char *fmt;
1673 /* repeat is used to turn tail-recursion into iteration since GCC
1674 can't do it when there's no return value. */
1675 repeat:
1677 if (x == 0)
1678 return;
1680 code = GET_CODE (x);
1681 switch (code)
1683 case REG:
1685 df_ref def;
1686 for (def = DF_REG_DEF_CHAIN (REGNO (x));
1687 def;
1688 def = DF_REF_NEXT_REG (def))
1689 RESET_BIT (bmap[DF_REF_BB (def)->index], indx);
1692 return;
1694 case MEM:
1695 if (! MEM_READONLY_P (x))
1697 bitmap_iterator bi;
1698 unsigned bb_index;
1700 /* First handle all the blocks with calls. We don't need to
1701 do any list walking for them. */
1702 EXECUTE_IF_SET_IN_BITMAP (blocks_with_calls, 0, bb_index, bi)
1704 RESET_BIT (bmap[bb_index], indx);
1707 /* Now iterate over the blocks which have memory modifications
1708 but which do not have any calls. */
1709 EXECUTE_IF_AND_COMPL_IN_BITMAP (modify_mem_list_set,
1710 blocks_with_calls,
1711 0, bb_index, bi)
1713 rtx list_entry = canon_modify_mem_list[bb_index];
1715 while (list_entry)
1717 rtx dest, dest_addr;
1719 /* LIST_ENTRY must be an INSN of some kind that sets memory.
1720 Examine each hunk of memory that is modified. */
1722 dest = XEXP (list_entry, 0);
1723 list_entry = XEXP (list_entry, 1);
1724 dest_addr = XEXP (list_entry, 0);
1726 if (canon_true_dependence (dest, GET_MODE (dest), dest_addr,
1727 x, NULL_RTX, rtx_addr_varies_p))
1729 RESET_BIT (bmap[bb_index], indx);
1731 list_entry = XEXP (list_entry, 1);
1736 x = XEXP (x, 0);
1737 goto repeat;
1739 case PC:
1740 case CC0: /*FIXME*/
1741 case CONST:
1742 case CONST_INT:
1743 case CONST_DOUBLE:
1744 case CONST_FIXED:
1745 case CONST_VECTOR:
1746 case SYMBOL_REF:
1747 case LABEL_REF:
1748 case ADDR_VEC:
1749 case ADDR_DIFF_VEC:
1750 return;
1752 default:
1753 break;
1756 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
1758 if (fmt[i] == 'e')
1760 /* If we are about to do the last recursive call
1761 needed at this level, change it into iteration.
1762 This function is called enough to be worth it. */
1763 if (i == 0)
1765 x = XEXP (x, i);
1766 goto repeat;
1769 compute_transp (XEXP (x, i), indx, bmap);
1771 else if (fmt[i] == 'E')
1772 for (j = 0; j < XVECLEN (x, i); j++)
1773 compute_transp (XVECEXP (x, i, j), indx, bmap);
1778 /* Compute PRE+LCM working variables. */
1780 /* Local properties of expressions. */
1781 /* Nonzero for expressions that are transparent in the block. */
1782 static sbitmap *transp;
1784 /* Nonzero for expressions that are computed (available) in the block. */
1785 static sbitmap *comp;
1787 /* Nonzero for expressions that are locally anticipatable in the block. */
1788 static sbitmap *antloc;
1790 /* Nonzero for expressions where this block is an optimal computation
1791 point. */
1792 static sbitmap *pre_optimal;
1794 /* Nonzero for expressions which are redundant in a particular block. */
1795 static sbitmap *pre_redundant;
1797 /* Nonzero for expressions which should be inserted on a specific edge. */
1798 static sbitmap *pre_insert_map;
1800 /* Nonzero for expressions which should be deleted in a specific block. */
1801 static sbitmap *pre_delete_map;
1803 /* Contains the edge_list returned by pre_edge_lcm. */
1804 static struct edge_list *edge_list;
1806 /* Allocate vars used for PRE analysis. */
1808 static void
1809 alloc_pre_mem (int n_blocks, int n_exprs)
1811 transp = sbitmap_vector_alloc (n_blocks, n_exprs);
1812 comp = sbitmap_vector_alloc (n_blocks, n_exprs);
1813 antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
1815 pre_optimal = NULL;
1816 pre_redundant = NULL;
1817 pre_insert_map = NULL;
1818 pre_delete_map = NULL;
1819 ae_kill = sbitmap_vector_alloc (n_blocks, n_exprs);
1821 /* pre_insert and pre_delete are allocated later. */
1824 /* Free vars used for PRE analysis. */
1826 static void
1827 free_pre_mem (void)
1829 sbitmap_vector_free (transp);
1830 sbitmap_vector_free (comp);
1832 /* ANTLOC and AE_KILL are freed just after pre_lcm finishes. */
1834 if (pre_optimal)
1835 sbitmap_vector_free (pre_optimal);
1836 if (pre_redundant)
1837 sbitmap_vector_free (pre_redundant);
1838 if (pre_insert_map)
1839 sbitmap_vector_free (pre_insert_map);
1840 if (pre_delete_map)
1841 sbitmap_vector_free (pre_delete_map);
1843 transp = comp = NULL;
1844 pre_optimal = pre_redundant = pre_insert_map = pre_delete_map = NULL;
1847 /* Remove certain expressions from anticipatable and transparent
1848 sets of basic blocks that have incoming abnormal edge.
1849 For PRE remove potentially trapping expressions to avoid placing
1850 them on abnormal edges. For hoisting remove memory references that
1851 can be clobbered by calls. */
1853 static void
1854 prune_expressions (bool pre_p)
1856 sbitmap prune_exprs;
1857 unsigned int ui;
1858 basic_block bb;
1860 prune_exprs = sbitmap_alloc (expr_hash_table.n_elems);
1861 sbitmap_zero (prune_exprs);
1862 for (ui = 0; ui < expr_hash_table.size; ui++)
1864 struct expr *e;
1865 for (e = expr_hash_table.table[ui]; e != NULL; e = e->next_same_hash)
1867 /* Note potentially trapping expressions. */
1868 if (may_trap_p (e->expr))
1870 SET_BIT (prune_exprs, e->bitmap_index);
1871 continue;
1874 if (!pre_p && MEM_P (e->expr))
1875 /* Note memory references that can be clobbered by a call.
1876 We do not split abnormal edges in hoisting, so would
1877 a memory reference get hoisted along an abnormal edge,
1878 it would be placed /before/ the call. Therefore, only
1879 constant memory references can be hoisted along abnormal
1880 edges. */
1882 if (GET_CODE (XEXP (e->expr, 0)) == SYMBOL_REF
1883 && CONSTANT_POOL_ADDRESS_P (XEXP (e->expr, 0)))
1884 continue;
1886 if (MEM_READONLY_P (e->expr)
1887 && !MEM_VOLATILE_P (e->expr)
1888 && MEM_NOTRAP_P (e->expr))
1889 /* Constant memory reference, e.g., a PIC address. */
1890 continue;
1892 /* ??? Optimally, we would use interprocedural alias
1893 analysis to determine if this mem is actually killed
1894 by this call. */
1896 SET_BIT (prune_exprs, e->bitmap_index);
1901 FOR_EACH_BB (bb)
1903 edge e;
1904 edge_iterator ei;
1906 /* If the current block is the destination of an abnormal edge, we
1907 kill all trapping (for PRE) and memory (for hoist) expressions
1908 because we won't be able to properly place the instruction on
1909 the edge. So make them neither anticipatable nor transparent.
1910 This is fairly conservative.
1912 ??? For hoisting it may be necessary to check for set-and-jump
1913 instructions here, not just for abnormal edges. The general problem
1914 is that when an expression cannot not be placed right at the end of
1915 a basic block we should account for any side-effects of a subsequent
1916 jump instructions that could clobber the expression. It would
1917 be best to implement this check along the lines of
1918 hoist_expr_reaches_here_p where the target block is already known
1919 and, hence, there's no need to conservatively prune expressions on
1920 "intermediate" set-and-jump instructions. */
1921 FOR_EACH_EDGE (e, ei, bb->preds)
1922 if ((e->flags & EDGE_ABNORMAL)
1923 && (pre_p || CALL_P (BB_END (e->src))))
1925 sbitmap_difference (antloc[bb->index],
1926 antloc[bb->index], prune_exprs);
1927 sbitmap_difference (transp[bb->index],
1928 transp[bb->index], prune_exprs);
1929 break;
1933 sbitmap_free (prune_exprs);
1936 /* It may be necessary to insert a large number of insns on edges to
1937 make the existing occurrences of expressions fully redundant. This
1938 routine examines the set of insertions and deletions and if the ratio
1939 of insertions to deletions is too high for a particular expression, then
1940 the expression is removed from the insertion/deletion sets.
1942 N_ELEMS is the number of elements in the hash table. */
1944 static void
1945 prune_insertions_deletions (int n_elems)
1947 sbitmap_iterator sbi;
1948 sbitmap prune_exprs;
1950 /* We always use I to iterate over blocks/edges and J to iterate over
1951 expressions. */
1952 unsigned int i, j;
1954 /* Counts for the number of times an expression needs to be inserted and
1955 number of times an expression can be removed as a result. */
1956 int *insertions = GCNEWVEC (int, n_elems);
1957 int *deletions = GCNEWVEC (int, n_elems);
1959 /* Set of expressions which require too many insertions relative to
1960 the number of deletions achieved. We will prune these out of the
1961 insertion/deletion sets. */
1962 prune_exprs = sbitmap_alloc (n_elems);
1963 sbitmap_zero (prune_exprs);
1965 /* Iterate over the edges counting the number of times each expression
1966 needs to be inserted. */
1967 for (i = 0; i < (unsigned) n_edges; i++)
1969 EXECUTE_IF_SET_IN_SBITMAP (pre_insert_map[i], 0, j, sbi)
1970 insertions[j]++;
1973 /* Similarly for deletions, but those occur in blocks rather than on
1974 edges. */
1975 for (i = 0; i < (unsigned) last_basic_block; i++)
1977 EXECUTE_IF_SET_IN_SBITMAP (pre_delete_map[i], 0, j, sbi)
1978 deletions[j]++;
1981 /* Now that we have accurate counts, iterate over the elements in the
1982 hash table and see if any need too many insertions relative to the
1983 number of evaluations that can be removed. If so, mark them in
1984 PRUNE_EXPRS. */
1985 for (j = 0; j < (unsigned) n_elems; j++)
1986 if (deletions[j]
1987 && ((unsigned) insertions[j] / deletions[j]) > MAX_GCSE_INSERTION_RATIO)
1988 SET_BIT (prune_exprs, j);
1990 /* Now prune PRE_INSERT_MAP and PRE_DELETE_MAP based on PRUNE_EXPRS. */
1991 EXECUTE_IF_SET_IN_SBITMAP (prune_exprs, 0, j, sbi)
1993 for (i = 0; i < (unsigned) n_edges; i++)
1994 RESET_BIT (pre_insert_map[i], j);
1996 for (i = 0; i < (unsigned) last_basic_block; i++)
1997 RESET_BIT (pre_delete_map[i], j);
2000 sbitmap_free (prune_exprs);
2001 free (insertions);
2002 free (deletions);
2005 /* Top level routine to do the dataflow analysis needed by PRE. */
2007 static void
2008 compute_pre_data (void)
2010 basic_block bb;
2012 compute_local_properties (transp, comp, antloc, &expr_hash_table);
2013 prune_expressions (true);
2014 sbitmap_vector_zero (ae_kill, last_basic_block);
2016 /* Compute ae_kill for each basic block using:
2018 ~(TRANSP | COMP)
2021 FOR_EACH_BB (bb)
2023 sbitmap_a_or_b (ae_kill[bb->index], transp[bb->index], comp[bb->index]);
2024 sbitmap_not (ae_kill[bb->index], ae_kill[bb->index]);
2027 edge_list = pre_edge_lcm (expr_hash_table.n_elems, transp, comp, antloc,
2028 ae_kill, &pre_insert_map, &pre_delete_map);
2029 sbitmap_vector_free (antloc);
2030 antloc = NULL;
2031 sbitmap_vector_free (ae_kill);
2032 ae_kill = NULL;
2034 prune_insertions_deletions (expr_hash_table.n_elems);
2037 /* PRE utilities */
2039 /* Return nonzero if an occurrence of expression EXPR in OCCR_BB would reach
2040 block BB.
2042 VISITED is a pointer to a working buffer for tracking which BB's have
2043 been visited. It is NULL for the top-level call.
2045 We treat reaching expressions that go through blocks containing the same
2046 reaching expression as "not reaching". E.g. if EXPR is generated in blocks
2047 2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
2048 2 as not reaching. The intent is to improve the probability of finding
2049 only one reaching expression and to reduce register lifetimes by picking
2050 the closest such expression. */
2052 static int
2053 pre_expr_reaches_here_p_work (basic_block occr_bb, struct expr *expr, basic_block bb, char *visited)
2055 edge pred;
2056 edge_iterator ei;
2058 FOR_EACH_EDGE (pred, ei, bb->preds)
2060 basic_block pred_bb = pred->src;
2062 if (pred->src == ENTRY_BLOCK_PTR
2063 /* Has predecessor has already been visited? */
2064 || visited[pred_bb->index])
2065 ;/* Nothing to do. */
2067 /* Does this predecessor generate this expression? */
2068 else if (TEST_BIT (comp[pred_bb->index], expr->bitmap_index))
2070 /* Is this the occurrence we're looking for?
2071 Note that there's only one generating occurrence per block
2072 so we just need to check the block number. */
2073 if (occr_bb == pred_bb)
2074 return 1;
2076 visited[pred_bb->index] = 1;
2078 /* Ignore this predecessor if it kills the expression. */
2079 else if (! TEST_BIT (transp[pred_bb->index], expr->bitmap_index))
2080 visited[pred_bb->index] = 1;
2082 /* Neither gen nor kill. */
2083 else
2085 visited[pred_bb->index] = 1;
2086 if (pre_expr_reaches_here_p_work (occr_bb, expr, pred_bb, visited))
2087 return 1;
2091 /* All paths have been checked. */
2092 return 0;
2095 /* The wrapper for pre_expr_reaches_here_work that ensures that any
2096 memory allocated for that function is returned. */
2098 static int
2099 pre_expr_reaches_here_p (basic_block occr_bb, struct expr *expr, basic_block bb)
2101 int rval;
2102 char *visited = XCNEWVEC (char, last_basic_block);
2104 rval = pre_expr_reaches_here_p_work (occr_bb, expr, bb, visited);
2106 free (visited);
2107 return rval;
2111 /* Given an expr, generate RTL which we can insert at the end of a BB,
2112 or on an edge. Set the block number of any insns generated to
2113 the value of BB. */
2115 static rtx
2116 process_insert_insn (struct expr *expr)
2118 rtx reg = expr->reaching_reg;
2119 rtx exp = copy_rtx (expr->expr);
2120 rtx pat;
2122 start_sequence ();
2124 /* If the expression is something that's an operand, like a constant,
2125 just copy it to a register. */
2126 if (general_operand (exp, GET_MODE (reg)))
2127 emit_move_insn (reg, exp);
2129 /* Otherwise, make a new insn to compute this expression and make sure the
2130 insn will be recognized (this also adds any needed CLOBBERs). Copy the
2131 expression to make sure we don't have any sharing issues. */
2132 else
2134 rtx insn = emit_insn (gen_rtx_SET (VOIDmode, reg, exp));
2136 if (insn_invalid_p (insn))
2137 gcc_unreachable ();
2141 pat = get_insns ();
2142 end_sequence ();
2144 return pat;
2147 /* Add EXPR to the end of basic block BB.
2149 This is used by both the PRE and code hoisting. */
2151 static void
2152 insert_insn_end_basic_block (struct expr *expr, basic_block bb)
2154 rtx insn = BB_END (bb);
2155 rtx new_insn;
2156 rtx reg = expr->reaching_reg;
2157 int regno = REGNO (reg);
2158 rtx pat, pat_end;
2160 pat = process_insert_insn (expr);
2161 gcc_assert (pat && INSN_P (pat));
2163 pat_end = pat;
2164 while (NEXT_INSN (pat_end) != NULL_RTX)
2165 pat_end = NEXT_INSN (pat_end);
2167 /* If the last insn is a jump, insert EXPR in front [taking care to
2168 handle cc0, etc. properly]. Similarly we need to care trapping
2169 instructions in presence of non-call exceptions. */
2171 if (JUMP_P (insn)
2172 || (NONJUMP_INSN_P (insn)
2173 && (!single_succ_p (bb)
2174 || single_succ_edge (bb)->flags & EDGE_ABNORMAL)))
2176 #ifdef HAVE_cc0
2177 rtx note;
2178 #endif
2180 /* If this is a jump table, then we can't insert stuff here. Since
2181 we know the previous real insn must be the tablejump, we insert
2182 the new instruction just before the tablejump. */
2183 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
2184 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
2185 insn = prev_active_insn (insn);
2187 #ifdef HAVE_cc0
2188 /* FIXME: 'twould be nice to call prev_cc0_setter here but it aborts
2189 if cc0 isn't set. */
2190 note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
2191 if (note)
2192 insn = XEXP (note, 0);
2193 else
2195 rtx maybe_cc0_setter = prev_nonnote_insn (insn);
2196 if (maybe_cc0_setter
2197 && INSN_P (maybe_cc0_setter)
2198 && sets_cc0_p (PATTERN (maybe_cc0_setter)))
2199 insn = maybe_cc0_setter;
2201 #endif
2202 /* FIXME: What if something in cc0/jump uses value set in new insn? */
2203 new_insn = emit_insn_before_noloc (pat, insn, bb);
2206 /* Likewise if the last insn is a call, as will happen in the presence
2207 of exception handling. */
2208 else if (CALL_P (insn)
2209 && (!single_succ_p (bb)
2210 || single_succ_edge (bb)->flags & EDGE_ABNORMAL))
2212 /* Keeping in mind targets with small register classes and parameters
2213 in registers, we search backward and place the instructions before
2214 the first parameter is loaded. Do this for everyone for consistency
2215 and a presumption that we'll get better code elsewhere as well. */
2217 /* Since different machines initialize their parameter registers
2218 in different orders, assume nothing. Collect the set of all
2219 parameter registers. */
2220 insn = find_first_parameter_load (insn, BB_HEAD (bb));
2222 /* If we found all the parameter loads, then we want to insert
2223 before the first parameter load.
2225 If we did not find all the parameter loads, then we might have
2226 stopped on the head of the block, which could be a CODE_LABEL.
2227 If we inserted before the CODE_LABEL, then we would be putting
2228 the insn in the wrong basic block. In that case, put the insn
2229 after the CODE_LABEL. Also, respect NOTE_INSN_BASIC_BLOCK. */
2230 while (LABEL_P (insn)
2231 || NOTE_INSN_BASIC_BLOCK_P (insn))
2232 insn = NEXT_INSN (insn);
2234 new_insn = emit_insn_before_noloc (pat, insn, bb);
2236 else
2237 new_insn = emit_insn_after_noloc (pat, insn, bb);
2239 while (1)
2241 if (INSN_P (pat))
2242 add_label_notes (PATTERN (pat), new_insn);
2243 if (pat == pat_end)
2244 break;
2245 pat = NEXT_INSN (pat);
2248 gcse_create_count++;
2250 if (dump_file)
2252 fprintf (dump_file, "PRE/HOIST: end of bb %d, insn %d, ",
2253 bb->index, INSN_UID (new_insn));
2254 fprintf (dump_file, "copying expression %d to reg %d\n",
2255 expr->bitmap_index, regno);
2259 /* Insert partially redundant expressions on edges in the CFG to make
2260 the expressions fully redundant. */
2262 static int
2263 pre_edge_insert (struct edge_list *edge_list, struct expr **index_map)
2265 int e, i, j, num_edges, set_size, did_insert = 0;
2266 sbitmap *inserted;
2268 /* Where PRE_INSERT_MAP is nonzero, we add the expression on that edge
2269 if it reaches any of the deleted expressions. */
2271 set_size = pre_insert_map[0]->size;
2272 num_edges = NUM_EDGES (edge_list);
2273 inserted = sbitmap_vector_alloc (num_edges, expr_hash_table.n_elems);
2274 sbitmap_vector_zero (inserted, num_edges);
2276 for (e = 0; e < num_edges; e++)
2278 int indx;
2279 basic_block bb = INDEX_EDGE_PRED_BB (edge_list, e);
2281 for (i = indx = 0; i < set_size; i++, indx += SBITMAP_ELT_BITS)
2283 SBITMAP_ELT_TYPE insert = pre_insert_map[e]->elms[i];
2285 for (j = indx; insert && j < (int) expr_hash_table.n_elems; j++, insert >>= 1)
2286 if ((insert & 1) != 0 && index_map[j]->reaching_reg != NULL_RTX)
2288 struct expr *expr = index_map[j];
2289 struct occr *occr;
2291 /* Now look at each deleted occurrence of this expression. */
2292 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
2294 if (! occr->deleted_p)
2295 continue;
2297 /* Insert this expression on this edge if it would
2298 reach the deleted occurrence in BB. */
2299 if (!TEST_BIT (inserted[e], j))
2301 rtx insn;
2302 edge eg = INDEX_EDGE (edge_list, e);
2304 /* We can't insert anything on an abnormal and
2305 critical edge, so we insert the insn at the end of
2306 the previous block. There are several alternatives
2307 detailed in Morgans book P277 (sec 10.5) for
2308 handling this situation. This one is easiest for
2309 now. */
2311 if (eg->flags & EDGE_ABNORMAL)
2312 insert_insn_end_basic_block (index_map[j], bb);
2313 else
2315 insn = process_insert_insn (index_map[j]);
2316 insert_insn_on_edge (insn, eg);
2319 if (dump_file)
2321 fprintf (dump_file, "PRE: edge (%d,%d), ",
2322 bb->index,
2323 INDEX_EDGE_SUCC_BB (edge_list, e)->index);
2324 fprintf (dump_file, "copy expression %d\n",
2325 expr->bitmap_index);
2328 update_ld_motion_stores (expr);
2329 SET_BIT (inserted[e], j);
2330 did_insert = 1;
2331 gcse_create_count++;
2338 sbitmap_vector_free (inserted);
2339 return did_insert;
2342 /* Copy the result of EXPR->EXPR generated by INSN to EXPR->REACHING_REG.
2343 Given "old_reg <- expr" (INSN), instead of adding after it
2344 reaching_reg <- old_reg
2345 it's better to do the following:
2346 reaching_reg <- expr
2347 old_reg <- reaching_reg
2348 because this way copy propagation can discover additional PRE
2349 opportunities. But if this fails, we try the old way.
2350 When "expr" is a store, i.e.
2351 given "MEM <- old_reg", instead of adding after it
2352 reaching_reg <- old_reg
2353 it's better to add it before as follows:
2354 reaching_reg <- old_reg
2355 MEM <- reaching_reg. */
2357 static void
2358 pre_insert_copy_insn (struct expr *expr, rtx insn)
2360 rtx reg = expr->reaching_reg;
2361 int regno = REGNO (reg);
2362 int indx = expr->bitmap_index;
2363 rtx pat = PATTERN (insn);
2364 rtx set, first_set, new_insn;
2365 rtx old_reg;
2366 int i;
2368 /* This block matches the logic in hash_scan_insn. */
2369 switch (GET_CODE (pat))
2371 case SET:
2372 set = pat;
2373 break;
2375 case PARALLEL:
2376 /* Search through the parallel looking for the set whose
2377 source was the expression that we're interested in. */
2378 first_set = NULL_RTX;
2379 set = NULL_RTX;
2380 for (i = 0; i < XVECLEN (pat, 0); i++)
2382 rtx x = XVECEXP (pat, 0, i);
2383 if (GET_CODE (x) == SET)
2385 /* If the source was a REG_EQUAL or REG_EQUIV note, we
2386 may not find an equivalent expression, but in this
2387 case the PARALLEL will have a single set. */
2388 if (first_set == NULL_RTX)
2389 first_set = x;
2390 if (expr_equiv_p (SET_SRC (x), expr->expr))
2392 set = x;
2393 break;
2398 gcc_assert (first_set);
2399 if (set == NULL_RTX)
2400 set = first_set;
2401 break;
2403 default:
2404 gcc_unreachable ();
2407 if (REG_P (SET_DEST (set)))
2409 old_reg = SET_DEST (set);
2410 /* Check if we can modify the set destination in the original insn. */
2411 if (validate_change (insn, &SET_DEST (set), reg, 0))
2413 new_insn = gen_move_insn (old_reg, reg);
2414 new_insn = emit_insn_after (new_insn, insn);
2416 else
2418 new_insn = gen_move_insn (reg, old_reg);
2419 new_insn = emit_insn_after (new_insn, insn);
2422 else /* This is possible only in case of a store to memory. */
2424 old_reg = SET_SRC (set);
2425 new_insn = gen_move_insn (reg, old_reg);
2427 /* Check if we can modify the set source in the original insn. */
2428 if (validate_change (insn, &SET_SRC (set), reg, 0))
2429 new_insn = emit_insn_before (new_insn, insn);
2430 else
2431 new_insn = emit_insn_after (new_insn, insn);
2434 gcse_create_count++;
2436 if (dump_file)
2437 fprintf (dump_file,
2438 "PRE: bb %d, insn %d, copy expression %d in insn %d to reg %d\n",
2439 BLOCK_FOR_INSN (insn)->index, INSN_UID (new_insn), indx,
2440 INSN_UID (insn), regno);
2443 /* Copy available expressions that reach the redundant expression
2444 to `reaching_reg'. */
2446 static void
2447 pre_insert_copies (void)
2449 unsigned int i, added_copy;
2450 struct expr *expr;
2451 struct occr *occr;
2452 struct occr *avail;
2454 /* For each available expression in the table, copy the result to
2455 `reaching_reg' if the expression reaches a deleted one.
2457 ??? The current algorithm is rather brute force.
2458 Need to do some profiling. */
2460 for (i = 0; i < expr_hash_table.size; i++)
2461 for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
2463 /* If the basic block isn't reachable, PPOUT will be TRUE. However,
2464 we don't want to insert a copy here because the expression may not
2465 really be redundant. So only insert an insn if the expression was
2466 deleted. This test also avoids further processing if the
2467 expression wasn't deleted anywhere. */
2468 if (expr->reaching_reg == NULL)
2469 continue;
2471 /* Set when we add a copy for that expression. */
2472 added_copy = 0;
2474 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
2476 if (! occr->deleted_p)
2477 continue;
2479 for (avail = expr->avail_occr; avail != NULL; avail = avail->next)
2481 rtx insn = avail->insn;
2483 /* No need to handle this one if handled already. */
2484 if (avail->copied_p)
2485 continue;
2487 /* Don't handle this one if it's a redundant one. */
2488 if (INSN_DELETED_P (insn))
2489 continue;
2491 /* Or if the expression doesn't reach the deleted one. */
2492 if (! pre_expr_reaches_here_p (BLOCK_FOR_INSN (avail->insn),
2493 expr,
2494 BLOCK_FOR_INSN (occr->insn)))
2495 continue;
2497 added_copy = 1;
2499 /* Copy the result of avail to reaching_reg. */
2500 pre_insert_copy_insn (expr, insn);
2501 avail->copied_p = 1;
2505 if (added_copy)
2506 update_ld_motion_stores (expr);
2510 /* Emit move from SRC to DEST noting the equivalence with expression computed
2511 in INSN. */
2512 static rtx
2513 gcse_emit_move_after (rtx src, rtx dest, rtx insn)
2515 rtx new_rtx;
2516 rtx set = single_set (insn), set2;
2517 rtx note;
2518 rtx eqv;
2520 /* This should never fail since we're creating a reg->reg copy
2521 we've verified to be valid. */
2523 new_rtx = emit_insn_after (gen_move_insn (dest, src), insn);
2525 /* Note the equivalence for local CSE pass. */
2526 set2 = single_set (new_rtx);
2527 if (!set2 || !rtx_equal_p (SET_DEST (set2), dest))
2528 return new_rtx;
2529 if ((note = find_reg_equal_equiv_note (insn)))
2530 eqv = XEXP (note, 0);
2531 else
2532 eqv = SET_SRC (set);
2534 set_unique_reg_note (new_rtx, REG_EQUAL, copy_insn_1 (eqv));
2536 return new_rtx;
2539 /* Delete redundant computations.
2540 Deletion is done by changing the insn to copy the `reaching_reg' of
2541 the expression into the result of the SET. It is left to later passes
2542 (cprop, cse2, flow, combine, regmove) to propagate the copy or eliminate it.
2544 Returns nonzero if a change is made. */
2546 static int
2547 pre_delete (void)
2549 unsigned int i;
2550 int changed;
2551 struct expr *expr;
2552 struct occr *occr;
2554 changed = 0;
2555 for (i = 0; i < expr_hash_table.size; i++)
2556 for (expr = expr_hash_table.table[i];
2557 expr != NULL;
2558 expr = expr->next_same_hash)
2560 int indx = expr->bitmap_index;
2562 /* We only need to search antic_occr since we require
2563 ANTLOC != 0. */
2565 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
2567 rtx insn = occr->insn;
2568 rtx set;
2569 basic_block bb = BLOCK_FOR_INSN (insn);
2571 /* We only delete insns that have a single_set. */
2572 if (TEST_BIT (pre_delete_map[bb->index], indx)
2573 && (set = single_set (insn)) != 0
2574 && dbg_cnt (pre_insn))
2576 /* Create a pseudo-reg to store the result of reaching
2577 expressions into. Get the mode for the new pseudo from
2578 the mode of the original destination pseudo. */
2579 if (expr->reaching_reg == NULL)
2580 expr->reaching_reg = gen_reg_rtx_and_attrs (SET_DEST (set));
2582 gcse_emit_move_after (expr->reaching_reg, SET_DEST (set), insn);
2583 delete_insn (insn);
2584 occr->deleted_p = 1;
2585 changed = 1;
2586 gcse_subst_count++;
2588 if (dump_file)
2590 fprintf (dump_file,
2591 "PRE: redundant insn %d (expression %d) in ",
2592 INSN_UID (insn), indx);
2593 fprintf (dump_file, "bb %d, reaching reg is %d\n",
2594 bb->index, REGNO (expr->reaching_reg));
2600 return changed;
2603 /* Perform GCSE optimizations using PRE.
2604 This is called by one_pre_gcse_pass after all the dataflow analysis
2605 has been done.
2607 This is based on the original Morel-Renvoise paper Fred Chow's thesis, and
2608 lazy code motion from Knoop, Ruthing and Steffen as described in Advanced
2609 Compiler Design and Implementation.
2611 ??? A new pseudo reg is created to hold the reaching expression. The nice
2612 thing about the classical approach is that it would try to use an existing
2613 reg. If the register can't be adequately optimized [i.e. we introduce
2614 reload problems], one could add a pass here to propagate the new register
2615 through the block.
2617 ??? We don't handle single sets in PARALLELs because we're [currently] not
2618 able to copy the rest of the parallel when we insert copies to create full
2619 redundancies from partial redundancies. However, there's no reason why we
2620 can't handle PARALLELs in the cases where there are no partial
2621 redundancies. */
2623 static int
2624 pre_gcse (void)
2626 unsigned int i;
2627 int did_insert, changed;
2628 struct expr **index_map;
2629 struct expr *expr;
2631 /* Compute a mapping from expression number (`bitmap_index') to
2632 hash table entry. */
2634 index_map = XCNEWVEC (struct expr *, expr_hash_table.n_elems);
2635 for (i = 0; i < expr_hash_table.size; i++)
2636 for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
2637 index_map[expr->bitmap_index] = expr;
2639 /* Delete the redundant insns first so that
2640 - we know what register to use for the new insns and for the other
2641 ones with reaching expressions
2642 - we know which insns are redundant when we go to create copies */
2644 changed = pre_delete ();
2645 did_insert = pre_edge_insert (edge_list, index_map);
2647 /* In other places with reaching expressions, copy the expression to the
2648 specially allocated pseudo-reg that reaches the redundant expr. */
2649 pre_insert_copies ();
2650 if (did_insert)
2652 commit_edge_insertions ();
2653 changed = 1;
2656 free (index_map);
2657 return changed;
2660 /* Top level routine to perform one PRE GCSE pass.
2662 Return nonzero if a change was made. */
2664 static int
2665 one_pre_gcse_pass (void)
2667 int changed = 0;
2669 gcse_subst_count = 0;
2670 gcse_create_count = 0;
2672 /* Return if there's nothing to do, or it is too expensive. */
2673 if (n_basic_blocks <= NUM_FIXED_BLOCKS + 1
2674 || is_too_expensive (_("PRE disabled")))
2675 return 0;
2677 /* We need alias. */
2678 init_alias_analysis ();
2680 bytes_used = 0;
2681 gcc_obstack_init (&gcse_obstack);
2682 alloc_gcse_mem ();
2684 alloc_hash_table (&expr_hash_table);
2685 add_noreturn_fake_exit_edges ();
2686 if (flag_gcse_lm)
2687 compute_ld_motion_mems ();
2689 compute_hash_table (&expr_hash_table);
2690 trim_ld_motion_mems ();
2691 if (dump_file)
2692 dump_hash_table (dump_file, "Expression", &expr_hash_table);
2694 if (expr_hash_table.n_elems > 0)
2696 alloc_pre_mem (last_basic_block, expr_hash_table.n_elems);
2697 compute_pre_data ();
2698 changed |= pre_gcse ();
2699 free_edge_list (edge_list);
2700 free_pre_mem ();
2703 free_ldst_mems ();
2704 remove_fake_exit_edges ();
2705 free_hash_table (&expr_hash_table);
2707 free_gcse_mem ();
2708 obstack_free (&gcse_obstack, NULL);
2710 /* We are finished with alias. */
2711 end_alias_analysis ();
2713 if (dump_file)
2715 fprintf (dump_file, "PRE GCSE of %s, %d basic blocks, %d bytes needed, ",
2716 current_function_name (), n_basic_blocks, bytes_used);
2717 fprintf (dump_file, "%d substs, %d insns created\n",
2718 gcse_subst_count, gcse_create_count);
2721 return changed;
2724 /* If X contains any LABEL_REF's, add REG_LABEL_OPERAND notes for them
2725 to INSN. If such notes are added to an insn which references a
2726 CODE_LABEL, the LABEL_NUSES count is incremented. We have to add
2727 that note, because the following loop optimization pass requires
2728 them. */
2730 /* ??? If there was a jump optimization pass after gcse and before loop,
2731 then we would not need to do this here, because jump would add the
2732 necessary REG_LABEL_OPERAND and REG_LABEL_TARGET notes. */
2734 static void
2735 add_label_notes (rtx x, rtx insn)
2737 enum rtx_code code = GET_CODE (x);
2738 int i, j;
2739 const char *fmt;
2741 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
2743 /* This code used to ignore labels that referred to dispatch tables to
2744 avoid flow generating (slightly) worse code.
2746 We no longer ignore such label references (see LABEL_REF handling in
2747 mark_jump_label for additional information). */
2749 /* There's no reason for current users to emit jump-insns with
2750 such a LABEL_REF, so we don't have to handle REG_LABEL_TARGET
2751 notes. */
2752 gcc_assert (!JUMP_P (insn));
2753 add_reg_note (insn, REG_LABEL_OPERAND, XEXP (x, 0));
2755 if (LABEL_P (XEXP (x, 0)))
2756 LABEL_NUSES (XEXP (x, 0))++;
2758 return;
2761 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
2763 if (fmt[i] == 'e')
2764 add_label_notes (XEXP (x, i), insn);
2765 else if (fmt[i] == 'E')
2766 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2767 add_label_notes (XVECEXP (x, i, j), insn);
2771 /* Code Hoisting variables and subroutines. */
2773 /* Very busy expressions. */
2774 static sbitmap *hoist_vbein;
2775 static sbitmap *hoist_vbeout;
2777 /* ??? We could compute post dominators and run this algorithm in
2778 reverse to perform tail merging, doing so would probably be
2779 more effective than the tail merging code in jump.c.
2781 It's unclear if tail merging could be run in parallel with
2782 code hoisting. It would be nice. */
2784 /* Allocate vars used for code hoisting analysis. */
2786 static void
2787 alloc_code_hoist_mem (int n_blocks, int n_exprs)
2789 antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
2790 transp = sbitmap_vector_alloc (n_blocks, n_exprs);
2791 comp = sbitmap_vector_alloc (n_blocks, n_exprs);
2793 hoist_vbein = sbitmap_vector_alloc (n_blocks, n_exprs);
2794 hoist_vbeout = sbitmap_vector_alloc (n_blocks, n_exprs);
2797 /* Free vars used for code hoisting analysis. */
2799 static void
2800 free_code_hoist_mem (void)
2802 sbitmap_vector_free (antloc);
2803 sbitmap_vector_free (transp);
2804 sbitmap_vector_free (comp);
2806 sbitmap_vector_free (hoist_vbein);
2807 sbitmap_vector_free (hoist_vbeout);
2809 free_dominance_info (CDI_DOMINATORS);
2812 /* Compute the very busy expressions at entry/exit from each block.
2814 An expression is very busy if all paths from a given point
2815 compute the expression. */
2817 static void
2818 compute_code_hoist_vbeinout (void)
2820 int changed, passes;
2821 basic_block bb;
2823 sbitmap_vector_zero (hoist_vbeout, last_basic_block);
2824 sbitmap_vector_zero (hoist_vbein, last_basic_block);
2826 passes = 0;
2827 changed = 1;
2829 while (changed)
2831 changed = 0;
2833 /* We scan the blocks in the reverse order to speed up
2834 the convergence. */
2835 FOR_EACH_BB_REVERSE (bb)
2837 if (bb->next_bb != EXIT_BLOCK_PTR)
2839 sbitmap_intersection_of_succs (hoist_vbeout[bb->index],
2840 hoist_vbein, bb->index);
2842 /* Include expressions in VBEout that are calculated
2843 in BB and available at its end. */
2844 sbitmap_a_or_b (hoist_vbeout[bb->index],
2845 hoist_vbeout[bb->index], comp[bb->index]);
2848 changed |= sbitmap_a_or_b_and_c_cg (hoist_vbein[bb->index],
2849 antloc[bb->index],
2850 hoist_vbeout[bb->index],
2851 transp[bb->index]);
2854 passes++;
2857 if (dump_file)
2859 fprintf (dump_file, "hoisting vbeinout computation: %d passes\n", passes);
2861 FOR_EACH_BB (bb)
2863 fprintf (dump_file, "vbein (%d): ", bb->index);
2864 dump_sbitmap_file (dump_file, hoist_vbein[bb->index]);
2865 fprintf (dump_file, "vbeout(%d): ", bb->index);
2866 dump_sbitmap_file (dump_file, hoist_vbeout[bb->index]);
2871 /* Top level routine to do the dataflow analysis needed by code hoisting. */
2873 static void
2874 compute_code_hoist_data (void)
2876 compute_local_properties (transp, comp, antloc, &expr_hash_table);
2877 prune_expressions (false);
2878 compute_code_hoist_vbeinout ();
2879 calculate_dominance_info (CDI_DOMINATORS);
2880 if (dump_file)
2881 fprintf (dump_file, "\n");
2884 /* Determine if the expression identified by EXPR_INDEX would
2885 reach BB unimpared if it was placed at the end of EXPR_BB.
2886 Stop the search if the expression would need to be moved more
2887 than DISTANCE instructions.
2889 It's unclear exactly what Muchnick meant by "unimpared". It seems
2890 to me that the expression must either be computed or transparent in
2891 *every* block in the path(s) from EXPR_BB to BB. Any other definition
2892 would allow the expression to be hoisted out of loops, even if
2893 the expression wasn't a loop invariant.
2895 Contrast this to reachability for PRE where an expression is
2896 considered reachable if *any* path reaches instead of *all*
2897 paths. */
2899 static int
2900 hoist_expr_reaches_here_p (basic_block expr_bb, int expr_index, basic_block bb,
2901 char *visited, int distance, int *bb_size)
2903 edge pred;
2904 edge_iterator ei;
2905 int visited_allocated_locally = 0;
2907 /* Terminate the search if distance, for which EXPR is allowed to move,
2908 is exhausted. */
2909 if (distance > 0)
2911 distance -= bb_size[bb->index];
2913 if (distance <= 0)
2914 return 0;
2916 else
2917 gcc_assert (distance == 0);
2919 if (visited == NULL)
2921 visited_allocated_locally = 1;
2922 visited = XCNEWVEC (char, last_basic_block);
2925 FOR_EACH_EDGE (pred, ei, bb->preds)
2927 basic_block pred_bb = pred->src;
2929 if (pred->src == ENTRY_BLOCK_PTR)
2930 break;
2931 else if (pred_bb == expr_bb)
2932 continue;
2933 else if (visited[pred_bb->index])
2934 continue;
2936 else if (! TEST_BIT (transp[pred_bb->index], expr_index))
2937 break;
2939 /* Not killed. */
2940 else
2942 visited[pred_bb->index] = 1;
2943 if (! hoist_expr_reaches_here_p (expr_bb, expr_index, pred_bb,
2944 visited, distance, bb_size))
2945 break;
2948 if (visited_allocated_locally)
2949 free (visited);
2951 return (pred == NULL);
2954 /* Find occurence in BB. */
2955 static struct occr *
2956 find_occr_in_bb (struct occr *occr, basic_block bb)
2958 /* Find the right occurrence of this expression. */
2959 while (occr && BLOCK_FOR_INSN (occr->insn) != bb)
2960 occr = occr->next;
2962 return occr;
2965 /* Actually perform code hoisting. */
2967 static int
2968 hoist_code (void)
2970 basic_block bb, dominated;
2971 VEC (basic_block, heap) *dom_tree_walk;
2972 unsigned int dom_tree_walk_index;
2973 VEC (basic_block, heap) *domby;
2974 unsigned int i,j;
2975 struct expr **index_map;
2976 struct expr *expr;
2977 int *to_bb_head;
2978 int *bb_size;
2979 int changed = 0;
2981 /* Compute a mapping from expression number (`bitmap_index') to
2982 hash table entry. */
2984 index_map = XCNEWVEC (struct expr *, expr_hash_table.n_elems);
2985 for (i = 0; i < expr_hash_table.size; i++)
2986 for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
2987 index_map[expr->bitmap_index] = expr;
2989 /* Calculate sizes of basic blocks and note how far
2990 each instruction is from the start of its block. We then use this
2991 data to restrict distance an expression can travel. */
2993 to_bb_head = XCNEWVEC (int, get_max_uid ());
2994 bb_size = XCNEWVEC (int, last_basic_block);
2996 FOR_EACH_BB (bb)
2998 rtx insn;
2999 int to_head;
3001 to_head = 0;
3002 FOR_BB_INSNS (bb, insn)
3004 /* Don't count debug instructions to avoid them affecting
3005 decision choices. */
3006 if (NONDEBUG_INSN_P (insn))
3007 to_bb_head[INSN_UID (insn)] = to_head++;
3010 bb_size[bb->index] = to_head;
3013 gcc_assert (EDGE_COUNT (ENTRY_BLOCK_PTR->succs) == 1
3014 && (EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest
3015 == ENTRY_BLOCK_PTR->next_bb));
3017 dom_tree_walk = get_all_dominated_blocks (CDI_DOMINATORS,
3018 ENTRY_BLOCK_PTR->next_bb);
3020 /* Walk over each basic block looking for potentially hoistable
3021 expressions, nothing gets hoisted from the entry block. */
3022 FOR_EACH_VEC_ELT (basic_block, dom_tree_walk, dom_tree_walk_index, bb)
3024 domby = get_dominated_to_depth (CDI_DOMINATORS, bb, MAX_HOIST_DEPTH);
3026 if (VEC_length (basic_block, domby) == 0)
3027 continue;
3029 /* Examine each expression that is very busy at the exit of this
3030 block. These are the potentially hoistable expressions. */
3031 for (i = 0; i < hoist_vbeout[bb->index]->n_bits; i++)
3033 if (TEST_BIT (hoist_vbeout[bb->index], i))
3035 /* Current expression. */
3036 struct expr *expr = index_map[i];
3037 /* Number of occurences of EXPR that can be hoisted to BB. */
3038 int hoistable = 0;
3039 /* Basic blocks that have occurences reachable from BB. */
3040 bitmap_head _from_bbs, *from_bbs = &_from_bbs;
3041 /* Occurences reachable from BB. */
3042 VEC (occr_t, heap) *occrs_to_hoist = NULL;
3043 /* We want to insert the expression into BB only once, so
3044 note when we've inserted it. */
3045 int insn_inserted_p;
3046 occr_t occr;
3048 bitmap_initialize (from_bbs, 0);
3050 /* If an expression is computed in BB and is available at end of
3051 BB, hoist all occurences dominated by BB to BB. */
3052 if (TEST_BIT (comp[bb->index], i))
3054 occr = find_occr_in_bb (expr->antic_occr, bb);
3056 if (occr)
3058 /* An occurence might've been already deleted
3059 while processing a dominator of BB. */
3060 if (!occr->deleted_p)
3062 gcc_assert (NONDEBUG_INSN_P (occr->insn));
3063 hoistable++;
3066 else
3067 hoistable++;
3070 /* We've found a potentially hoistable expression, now
3071 we look at every block BB dominates to see if it
3072 computes the expression. */
3073 FOR_EACH_VEC_ELT (basic_block, domby, j, dominated)
3075 int max_distance;
3077 /* Ignore self dominance. */
3078 if (bb == dominated)
3079 continue;
3080 /* We've found a dominated block, now see if it computes
3081 the busy expression and whether or not moving that
3082 expression to the "beginning" of that block is safe. */
3083 if (!TEST_BIT (antloc[dominated->index], i))
3084 continue;
3086 occr = find_occr_in_bb (expr->antic_occr, dominated);
3087 gcc_assert (occr);
3089 /* An occurence might've been already deleted
3090 while processing a dominator of BB. */
3091 if (occr->deleted_p)
3092 continue;
3093 gcc_assert (NONDEBUG_INSN_P (occr->insn));
3095 max_distance = expr->max_distance;
3096 if (max_distance > 0)
3097 /* Adjust MAX_DISTANCE to account for the fact that
3098 OCCR won't have to travel all of DOMINATED, but
3099 only part of it. */
3100 max_distance += (bb_size[dominated->index]
3101 - to_bb_head[INSN_UID (occr->insn)]);
3103 /* Note if the expression would reach the dominated block
3104 unimpared if it was placed at the end of BB.
3106 Keep track of how many times this expression is hoistable
3107 from a dominated block into BB. */
3108 if (hoist_expr_reaches_here_p (bb, i, dominated, NULL,
3109 max_distance, bb_size))
3111 hoistable++;
3112 VEC_safe_push (occr_t, heap,
3113 occrs_to_hoist, occr);
3114 bitmap_set_bit (from_bbs, dominated->index);
3118 /* If we found more than one hoistable occurrence of this
3119 expression, then note it in the vector of expressions to
3120 hoist. It makes no sense to hoist things which are computed
3121 in only one BB, and doing so tends to pessimize register
3122 allocation. One could increase this value to try harder
3123 to avoid any possible code expansion due to register
3124 allocation issues; however experiments have shown that
3125 the vast majority of hoistable expressions are only movable
3126 from two successors, so raising this threshold is likely
3127 to nullify any benefit we get from code hoisting. */
3128 if (hoistable > 1 && dbg_cnt (hoist_insn))
3130 /* If (hoistable != VEC_length), then there is
3131 an occurence of EXPR in BB itself. Don't waste
3132 time looking for LCA in this case. */
3133 if ((unsigned) hoistable
3134 == VEC_length (occr_t, occrs_to_hoist))
3136 basic_block lca;
3138 lca = nearest_common_dominator_for_set (CDI_DOMINATORS,
3139 from_bbs);
3140 if (lca != bb)
3141 /* Punt, it's better to hoist these occurences to
3142 LCA. */
3143 VEC_free (occr_t, heap, occrs_to_hoist);
3146 else
3147 /* Punt, no point hoisting a single occurence. */
3148 VEC_free (occr_t, heap, occrs_to_hoist);
3150 insn_inserted_p = 0;
3152 /* Walk through occurences of I'th expressions we want
3153 to hoist to BB and make the transformations. */
3154 FOR_EACH_VEC_ELT (occr_t, occrs_to_hoist, j, occr)
3156 rtx insn;
3157 rtx set;
3159 gcc_assert (!occr->deleted_p);
3161 insn = occr->insn;
3162 set = single_set (insn);
3163 gcc_assert (set);
3165 /* Create a pseudo-reg to store the result of reaching
3166 expressions into. Get the mode for the new pseudo
3167 from the mode of the original destination pseudo.
3169 It is important to use new pseudos whenever we
3170 emit a set. This will allow reload to use
3171 rematerialization for such registers. */
3172 if (!insn_inserted_p)
3173 expr->reaching_reg
3174 = gen_reg_rtx_and_attrs (SET_DEST (set));
3176 gcse_emit_move_after (expr->reaching_reg, SET_DEST (set),
3177 insn);
3178 delete_insn (insn);
3179 occr->deleted_p = 1;
3180 changed = 1;
3181 gcse_subst_count++;
3183 if (!insn_inserted_p)
3185 insert_insn_end_basic_block (expr, bb);
3186 insn_inserted_p = 1;
3190 VEC_free (occr_t, heap, occrs_to_hoist);
3191 bitmap_clear (from_bbs);
3194 VEC_free (basic_block, heap, domby);
3197 VEC_free (basic_block, heap, dom_tree_walk);
3198 free (bb_size);
3199 free (to_bb_head);
3200 free (index_map);
3202 return changed;
3205 /* Top level routine to perform one code hoisting (aka unification) pass
3207 Return nonzero if a change was made. */
3209 static int
3210 one_code_hoisting_pass (void)
3212 int changed = 0;
3214 gcse_subst_count = 0;
3215 gcse_create_count = 0;
3217 /* Return if there's nothing to do, or it is too expensive. */
3218 if (n_basic_blocks <= NUM_FIXED_BLOCKS + 1
3219 || is_too_expensive (_("GCSE disabled")))
3220 return 0;
3222 doing_code_hoisting_p = true;
3224 /* We need alias. */
3225 init_alias_analysis ();
3227 bytes_used = 0;
3228 gcc_obstack_init (&gcse_obstack);
3229 alloc_gcse_mem ();
3231 alloc_hash_table (&expr_hash_table);
3232 compute_hash_table (&expr_hash_table);
3233 if (dump_file)
3234 dump_hash_table (dump_file, "Code Hosting Expressions", &expr_hash_table);
3236 if (expr_hash_table.n_elems > 0)
3238 alloc_code_hoist_mem (last_basic_block, expr_hash_table.n_elems);
3239 compute_code_hoist_data ();
3240 changed = hoist_code ();
3241 free_code_hoist_mem ();
3244 free_hash_table (&expr_hash_table);
3245 free_gcse_mem ();
3246 obstack_free (&gcse_obstack, NULL);
3248 /* We are finished with alias. */
3249 end_alias_analysis ();
3251 if (dump_file)
3253 fprintf (dump_file, "HOIST of %s, %d basic blocks, %d bytes needed, ",
3254 current_function_name (), n_basic_blocks, bytes_used);
3255 fprintf (dump_file, "%d substs, %d insns created\n",
3256 gcse_subst_count, gcse_create_count);
3259 doing_code_hoisting_p = false;
3261 return changed;
3264 /* Here we provide the things required to do store motion towards
3265 the exit. In order for this to be effective, gcse also needed to
3266 be taught how to move a load when it is kill only by a store to itself.
3268 int i;
3269 float a[10];
3271 void foo(float scale)
3273 for (i=0; i<10; i++)
3274 a[i] *= scale;
3277 'i' is both loaded and stored to in the loop. Normally, gcse cannot move
3278 the load out since its live around the loop, and stored at the bottom
3279 of the loop.
3281 The 'Load Motion' referred to and implemented in this file is
3282 an enhancement to gcse which when using edge based lcm, recognizes
3283 this situation and allows gcse to move the load out of the loop.
3285 Once gcse has hoisted the load, store motion can then push this
3286 load towards the exit, and we end up with no loads or stores of 'i'
3287 in the loop. */
3289 static hashval_t
3290 pre_ldst_expr_hash (const void *p)
3292 int do_not_record_p = 0;
3293 const struct ls_expr *const x = (const struct ls_expr *) p;
3294 return hash_rtx (x->pattern, GET_MODE (x->pattern), &do_not_record_p, NULL, false);
3297 static int
3298 pre_ldst_expr_eq (const void *p1, const void *p2)
3300 const struct ls_expr *const ptr1 = (const struct ls_expr *) p1,
3301 *const ptr2 = (const struct ls_expr *) p2;
3302 return expr_equiv_p (ptr1->pattern, ptr2->pattern);
3305 /* This will search the ldst list for a matching expression. If it
3306 doesn't find one, we create one and initialize it. */
3308 static struct ls_expr *
3309 ldst_entry (rtx x)
3311 int do_not_record_p = 0;
3312 struct ls_expr * ptr;
3313 unsigned int hash;
3314 void **slot;
3315 struct ls_expr e;
3317 hash = hash_rtx (x, GET_MODE (x), &do_not_record_p,
3318 NULL, /*have_reg_qty=*/false);
3320 e.pattern = x;
3321 slot = htab_find_slot_with_hash (pre_ldst_table, &e, hash, INSERT);
3322 if (*slot)
3323 return (struct ls_expr *)*slot;
3325 ptr = XNEW (struct ls_expr);
3327 ptr->next = pre_ldst_mems;
3328 ptr->expr = NULL;
3329 ptr->pattern = x;
3330 ptr->pattern_regs = NULL_RTX;
3331 ptr->loads = NULL_RTX;
3332 ptr->stores = NULL_RTX;
3333 ptr->reaching_reg = NULL_RTX;
3334 ptr->invalid = 0;
3335 ptr->index = 0;
3336 ptr->hash_index = hash;
3337 pre_ldst_mems = ptr;
3338 *slot = ptr;
3340 return ptr;
3343 /* Free up an individual ldst entry. */
3345 static void
3346 free_ldst_entry (struct ls_expr * ptr)
3348 free_INSN_LIST_list (& ptr->loads);
3349 free_INSN_LIST_list (& ptr->stores);
3351 free (ptr);
3354 /* Free up all memory associated with the ldst list. */
3356 static void
3357 free_ldst_mems (void)
3359 if (pre_ldst_table)
3360 htab_delete (pre_ldst_table);
3361 pre_ldst_table = NULL;
3363 while (pre_ldst_mems)
3365 struct ls_expr * tmp = pre_ldst_mems;
3367 pre_ldst_mems = pre_ldst_mems->next;
3369 free_ldst_entry (tmp);
3372 pre_ldst_mems = NULL;
3375 /* Dump debugging info about the ldst list. */
3377 static void
3378 print_ldst_list (FILE * file)
3380 struct ls_expr * ptr;
3382 fprintf (file, "LDST list: \n");
3384 for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
3386 fprintf (file, " Pattern (%3d): ", ptr->index);
3388 print_rtl (file, ptr->pattern);
3390 fprintf (file, "\n Loads : ");
3392 if (ptr->loads)
3393 print_rtl (file, ptr->loads);
3394 else
3395 fprintf (file, "(nil)");
3397 fprintf (file, "\n Stores : ");
3399 if (ptr->stores)
3400 print_rtl (file, ptr->stores);
3401 else
3402 fprintf (file, "(nil)");
3404 fprintf (file, "\n\n");
3407 fprintf (file, "\n");
3410 /* Returns 1 if X is in the list of ldst only expressions. */
3412 static struct ls_expr *
3413 find_rtx_in_ldst (rtx x)
3415 struct ls_expr e;
3416 void **slot;
3417 if (!pre_ldst_table)
3418 return NULL;
3419 e.pattern = x;
3420 slot = htab_find_slot (pre_ldst_table, &e, NO_INSERT);
3421 if (!slot || ((struct ls_expr *)*slot)->invalid)
3422 return NULL;
3423 return (struct ls_expr *) *slot;
3426 /* Return first item in the list. */
3428 static inline struct ls_expr *
3429 first_ls_expr (void)
3431 return pre_ldst_mems;
3434 /* Return the next item in the list after the specified one. */
3436 static inline struct ls_expr *
3437 next_ls_expr (struct ls_expr * ptr)
3439 return ptr->next;
3442 /* Load Motion for loads which only kill themselves. */
3444 /* Return true if x is a simple MEM operation, with no registers or
3445 side effects. These are the types of loads we consider for the
3446 ld_motion list, otherwise we let the usual aliasing take care of it. */
3448 static int
3449 simple_mem (const_rtx x)
3451 if (! MEM_P (x))
3452 return 0;
3454 if (MEM_VOLATILE_P (x))
3455 return 0;
3457 if (GET_MODE (x) == BLKmode)
3458 return 0;
3460 /* If we are handling exceptions, we must be careful with memory references
3461 that may trap. If we are not, the behavior is undefined, so we may just
3462 continue. */
3463 if (cfun->can_throw_non_call_exceptions && may_trap_p (x))
3464 return 0;
3466 if (side_effects_p (x))
3467 return 0;
3469 /* Do not consider function arguments passed on stack. */
3470 if (reg_mentioned_p (stack_pointer_rtx, x))
3471 return 0;
3473 if (flag_float_store && FLOAT_MODE_P (GET_MODE (x)))
3474 return 0;
3476 return 1;
3479 /* Make sure there isn't a buried reference in this pattern anywhere.
3480 If there is, invalidate the entry for it since we're not capable
3481 of fixing it up just yet.. We have to be sure we know about ALL
3482 loads since the aliasing code will allow all entries in the
3483 ld_motion list to not-alias itself. If we miss a load, we will get
3484 the wrong value since gcse might common it and we won't know to
3485 fix it up. */
3487 static void
3488 invalidate_any_buried_refs (rtx x)
3490 const char * fmt;
3491 int i, j;
3492 struct ls_expr * ptr;
3494 /* Invalidate it in the list. */
3495 if (MEM_P (x) && simple_mem (x))
3497 ptr = ldst_entry (x);
3498 ptr->invalid = 1;
3501 /* Recursively process the insn. */
3502 fmt = GET_RTX_FORMAT (GET_CODE (x));
3504 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
3506 if (fmt[i] == 'e')
3507 invalidate_any_buried_refs (XEXP (x, i));
3508 else if (fmt[i] == 'E')
3509 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3510 invalidate_any_buried_refs (XVECEXP (x, i, j));
3514 /* Find all the 'simple' MEMs which are used in LOADs and STORES. Simple
3515 being defined as MEM loads and stores to symbols, with no side effects
3516 and no registers in the expression. For a MEM destination, we also
3517 check that the insn is still valid if we replace the destination with a
3518 REG, as is done in update_ld_motion_stores. If there are any uses/defs
3519 which don't match this criteria, they are invalidated and trimmed out
3520 later. */
3522 static void
3523 compute_ld_motion_mems (void)
3525 struct ls_expr * ptr;
3526 basic_block bb;
3527 rtx insn;
3529 pre_ldst_mems = NULL;
3530 pre_ldst_table = htab_create (13, pre_ldst_expr_hash,
3531 pre_ldst_expr_eq, NULL);
3533 FOR_EACH_BB (bb)
3535 FOR_BB_INSNS (bb, insn)
3537 if (NONDEBUG_INSN_P (insn))
3539 if (GET_CODE (PATTERN (insn)) == SET)
3541 rtx src = SET_SRC (PATTERN (insn));
3542 rtx dest = SET_DEST (PATTERN (insn));
3544 /* Check for a simple LOAD... */
3545 if (MEM_P (src) && simple_mem (src))
3547 ptr = ldst_entry (src);
3548 if (REG_P (dest))
3549 ptr->loads = alloc_INSN_LIST (insn, ptr->loads);
3550 else
3551 ptr->invalid = 1;
3553 else
3555 /* Make sure there isn't a buried load somewhere. */
3556 invalidate_any_buried_refs (src);
3559 /* Check for stores. Don't worry about aliased ones, they
3560 will block any movement we might do later. We only care
3561 about this exact pattern since those are the only
3562 circumstance that we will ignore the aliasing info. */
3563 if (MEM_P (dest) && simple_mem (dest))
3565 ptr = ldst_entry (dest);
3567 if (! MEM_P (src)
3568 && GET_CODE (src) != ASM_OPERANDS
3569 /* Check for REG manually since want_to_gcse_p
3570 returns 0 for all REGs. */
3571 && can_assign_to_reg_without_clobbers_p (src))
3572 ptr->stores = alloc_INSN_LIST (insn, ptr->stores);
3573 else
3574 ptr->invalid = 1;
3577 else
3578 invalidate_any_buried_refs (PATTERN (insn));
3584 /* Remove any references that have been either invalidated or are not in the
3585 expression list for pre gcse. */
3587 static void
3588 trim_ld_motion_mems (void)
3590 struct ls_expr * * last = & pre_ldst_mems;
3591 struct ls_expr * ptr = pre_ldst_mems;
3593 while (ptr != NULL)
3595 struct expr * expr;
3597 /* Delete if entry has been made invalid. */
3598 if (! ptr->invalid)
3600 /* Delete if we cannot find this mem in the expression list. */
3601 unsigned int hash = ptr->hash_index % expr_hash_table.size;
3603 for (expr = expr_hash_table.table[hash];
3604 expr != NULL;
3605 expr = expr->next_same_hash)
3606 if (expr_equiv_p (expr->expr, ptr->pattern))
3607 break;
3609 else
3610 expr = (struct expr *) 0;
3612 if (expr)
3614 /* Set the expression field if we are keeping it. */
3615 ptr->expr = expr;
3616 last = & ptr->next;
3617 ptr = ptr->next;
3619 else
3621 *last = ptr->next;
3622 htab_remove_elt_with_hash (pre_ldst_table, ptr, ptr->hash_index);
3623 free_ldst_entry (ptr);
3624 ptr = * last;
3628 /* Show the world what we've found. */
3629 if (dump_file && pre_ldst_mems != NULL)
3630 print_ldst_list (dump_file);
3633 /* This routine will take an expression which we are replacing with
3634 a reaching register, and update any stores that are needed if
3635 that expression is in the ld_motion list. Stores are updated by
3636 copying their SRC to the reaching register, and then storing
3637 the reaching register into the store location. These keeps the
3638 correct value in the reaching register for the loads. */
3640 static void
3641 update_ld_motion_stores (struct expr * expr)
3643 struct ls_expr * mem_ptr;
3645 if ((mem_ptr = find_rtx_in_ldst (expr->expr)))
3647 /* We can try to find just the REACHED stores, but is shouldn't
3648 matter to set the reaching reg everywhere... some might be
3649 dead and should be eliminated later. */
3651 /* We replace (set mem expr) with (set reg expr) (set mem reg)
3652 where reg is the reaching reg used in the load. We checked in
3653 compute_ld_motion_mems that we can replace (set mem expr) with
3654 (set reg expr) in that insn. */
3655 rtx list = mem_ptr->stores;
3657 for ( ; list != NULL_RTX; list = XEXP (list, 1))
3659 rtx insn = XEXP (list, 0);
3660 rtx pat = PATTERN (insn);
3661 rtx src = SET_SRC (pat);
3662 rtx reg = expr->reaching_reg;
3663 rtx copy;
3665 /* If we've already copied it, continue. */
3666 if (expr->reaching_reg == src)
3667 continue;
3669 if (dump_file)
3671 fprintf (dump_file, "PRE: store updated with reaching reg ");
3672 print_rtl (dump_file, expr->reaching_reg);
3673 fprintf (dump_file, ":\n ");
3674 print_inline_rtx (dump_file, insn, 8);
3675 fprintf (dump_file, "\n");
3678 copy = gen_move_insn (reg, copy_rtx (SET_SRC (pat)));
3679 emit_insn_before (copy, insn);
3680 SET_SRC (pat) = reg;
3681 df_insn_rescan (insn);
3683 /* un-recognize this pattern since it's probably different now. */
3684 INSN_CODE (insn) = -1;
3685 gcse_create_count++;
3690 /* Return true if the graph is too expensive to optimize. PASS is the
3691 optimization about to be performed. */
3693 static bool
3694 is_too_expensive (const char *pass)
3696 /* Trying to perform global optimizations on flow graphs which have
3697 a high connectivity will take a long time and is unlikely to be
3698 particularly useful.
3700 In normal circumstances a cfg should have about twice as many
3701 edges as blocks. But we do not want to punish small functions
3702 which have a couple switch statements. Rather than simply
3703 threshold the number of blocks, uses something with a more
3704 graceful degradation. */
3705 if (n_edges > 20000 + n_basic_blocks * 4)
3707 warning (OPT_Wdisabled_optimization,
3708 "%s: %d basic blocks and %d edges/basic block",
3709 pass, n_basic_blocks, n_edges / n_basic_blocks);
3711 return true;
3714 /* If allocating memory for the dataflow bitmaps would take up too much
3715 storage it's better just to disable the optimization. */
3716 if ((n_basic_blocks
3717 * SBITMAP_SET_SIZE (max_reg_num ())
3718 * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
3720 warning (OPT_Wdisabled_optimization,
3721 "%s: %d basic blocks and %d registers",
3722 pass, n_basic_blocks, max_reg_num ());
3724 return true;
3727 return false;
3731 /* All the passes implemented in this file. Each pass has its
3732 own gate and execute function, and at the end of the file a
3733 pass definition for passes.c.
3735 We do not construct an accurate cfg in functions which call
3736 setjmp, so none of these passes runs if the function calls
3737 setjmp.
3738 FIXME: Should just handle setjmp via REG_SETJMP notes. */
3740 static bool
3741 gate_rtl_pre (void)
3743 return optimize > 0 && flag_gcse
3744 && !cfun->calls_setjmp
3745 && optimize_function_for_speed_p (cfun)
3746 && dbg_cnt (pre);
3749 static unsigned int
3750 execute_rtl_pre (void)
3752 int changed;
3753 delete_unreachable_blocks ();
3754 df_analyze ();
3755 changed = one_pre_gcse_pass ();
3756 flag_rerun_cse_after_global_opts |= changed;
3757 if (changed)
3758 cleanup_cfg (0);
3759 return 0;
3762 static bool
3763 gate_rtl_hoist (void)
3765 return optimize > 0 && flag_gcse
3766 && !cfun->calls_setjmp
3767 /* It does not make sense to run code hoisting unless we are optimizing
3768 for code size -- it rarely makes programs faster, and can make then
3769 bigger if we did PRE (when optimizing for space, we don't run PRE). */
3770 && optimize_function_for_size_p (cfun)
3771 && dbg_cnt (hoist);
3774 static unsigned int
3775 execute_rtl_hoist (void)
3777 int changed;
3778 delete_unreachable_blocks ();
3779 df_analyze ();
3780 changed = one_code_hoisting_pass ();
3781 flag_rerun_cse_after_global_opts |= changed;
3782 if (changed)
3783 cleanup_cfg (0);
3784 return 0;
3787 struct rtl_opt_pass pass_rtl_pre =
3790 RTL_PASS,
3791 "rtl pre", /* name */
3792 gate_rtl_pre, /* gate */
3793 execute_rtl_pre, /* execute */
3794 NULL, /* sub */
3795 NULL, /* next */
3796 0, /* static_pass_number */
3797 TV_PRE, /* tv_id */
3798 PROP_cfglayout, /* properties_required */
3799 0, /* properties_provided */
3800 0, /* properties_destroyed */
3801 0, /* todo_flags_start */
3802 TODO_df_finish | TODO_verify_rtl_sharing |
3803 TODO_dump_func |
3804 TODO_verify_flow | TODO_ggc_collect /* todo_flags_finish */
3808 struct rtl_opt_pass pass_rtl_hoist =
3811 RTL_PASS,
3812 "hoist", /* name */
3813 gate_rtl_hoist, /* gate */
3814 execute_rtl_hoist, /* execute */
3815 NULL, /* sub */
3816 NULL, /* next */
3817 0, /* static_pass_number */
3818 TV_HOIST, /* tv_id */
3819 PROP_cfglayout, /* properties_required */
3820 0, /* properties_provided */
3821 0, /* properties_destroyed */
3822 0, /* todo_flags_start */
3823 TODO_df_finish | TODO_verify_rtl_sharing |
3824 TODO_dump_func |
3825 TODO_verify_flow | TODO_ggc_collect /* todo_flags_finish */
3829 #include "gt-gcse.h"