]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/gcse.c
configure.ac (cygwin noconfigdirs): Remove libgcj.
[thirdparty/gcc.git] / gcc / gcse.c
1 /* Global common subexpression elimination/Partial redundancy elimination
2 and global constant/copy propagation for GNU compiler.
3 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 /* TODO
23 - reordering of memory allocation and freeing to be more space efficient
24 - do rough calc of how many regs are needed in each block, and a rough
25 calc of how many regs are available in each class and use that to
26 throttle back the code in cases where RTX_COST is minimal.
27 - a store to the same address as a load does not kill the load if the
28 source of the store is also the destination of the load. Handling this
29 allows more load motion, particularly out of loops.
30
31 */
32
33 /* References searched while implementing this.
34
35 Compilers Principles, Techniques and Tools
36 Aho, Sethi, Ullman
37 Addison-Wesley, 1988
38
39 Global Optimization by Suppression of Partial Redundancies
40 E. Morel, C. Renvoise
41 communications of the acm, Vol. 22, Num. 2, Feb. 1979
42
43 A Portable Machine-Independent Global Optimizer - Design and Measurements
44 Frederick Chow
45 Stanford Ph.D. thesis, Dec. 1983
46
47 A Fast Algorithm for Code Movement Optimization
48 D.M. Dhamdhere
49 SIGPLAN Notices, Vol. 23, Num. 10, Oct. 1988
50
51 A Solution to a Problem with Morel and Renvoise's
52 Global Optimization by Suppression of Partial Redundancies
53 K-H Drechsler, M.P. Stadel
54 ACM TOPLAS, Vol. 10, Num. 4, Oct. 1988
55
56 Practical Adaptation of the Global Optimization
57 Algorithm of Morel and Renvoise
58 D.M. Dhamdhere
59 ACM TOPLAS, Vol. 13, Num. 2. Apr. 1991
60
61 Efficiently Computing Static Single Assignment Form and the Control
62 Dependence Graph
63 R. Cytron, J. Ferrante, B.K. Rosen, M.N. Wegman, and F.K. Zadeck
64 ACM TOPLAS, Vol. 13, Num. 4, Oct. 1991
65
66 Lazy Code Motion
67 J. Knoop, O. Ruthing, B. Steffen
68 ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
69
70 What's In a Region? Or Computing Control Dependence Regions in Near-Linear
71 Time for Reducible Flow Control
72 Thomas Ball
73 ACM Letters on Programming Languages and Systems,
74 Vol. 2, Num. 1-4, Mar-Dec 1993
75
76 An Efficient Representation for Sparse Sets
77 Preston Briggs, Linda Torczon
78 ACM Letters on Programming Languages and Systems,
79 Vol. 2, Num. 1-4, Mar-Dec 1993
80
81 A Variation of Knoop, Ruthing, and Steffen's Lazy Code Motion
82 K-H Drechsler, M.P. Stadel
83 ACM SIGPLAN Notices, Vol. 28, Num. 5, May 1993
84
85 Partial Dead Code Elimination
86 J. Knoop, O. Ruthing, B. Steffen
87 ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
88
89 Effective Partial Redundancy Elimination
90 P. Briggs, K.D. Cooper
91 ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
92
93 The Program Structure Tree: Computing Control Regions in Linear Time
94 R. Johnson, D. Pearson, K. Pingali
95 ACM SIGPLAN Notices, Vol. 29, Num. 6, Jun. 1994
96
97 Optimal Code Motion: Theory and Practice
98 J. Knoop, O. Ruthing, B. Steffen
99 ACM TOPLAS, Vol. 16, Num. 4, Jul. 1994
100
101 The power of assignment motion
102 J. Knoop, O. Ruthing, B. Steffen
103 ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
104
105 Global code motion / global value numbering
106 C. Click
107 ACM SIGPLAN Notices Vol. 30, Num. 6, Jun. 1995, '95 Conference on PLDI
108
109 Value Driven Redundancy Elimination
110 L.T. Simpson
111 Rice University Ph.D. thesis, Apr. 1996
112
113 Value Numbering
114 L.T. Simpson
115 Massively Scalar Compiler Project, Rice University, Sep. 1996
116
117 High Performance Compilers for Parallel Computing
118 Michael Wolfe
119 Addison-Wesley, 1996
120
121 Advanced Compiler Design and Implementation
122 Steven Muchnick
123 Morgan Kaufmann, 1997
124
125 Building an Optimizing Compiler
126 Robert Morgan
127 Digital Press, 1998
128
129 People wishing to speed up the code here should read:
130 Elimination Algorithms for Data Flow Analysis
131 B.G. Ryder, M.C. Paull
132 ACM Computing Surveys, Vol. 18, Num. 3, Sep. 1986
133
134 How to Analyze Large Programs Efficiently and Informatively
135 D.M. Dhamdhere, B.K. Rosen, F.K. Zadeck
136 ACM SIGPLAN Notices Vol. 27, Num. 7, Jul. 1992, '92 Conference on PLDI
137
138 People wishing to do something different can find various possibilities
139 in the above papers and elsewhere.
140 */
141
142 #include "config.h"
143 #include "system.h"
144 #include "coretypes.h"
145 #include "tm.h"
146 #include "toplev.h"
147
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 "real.h"
155 #include "insn-config.h"
156 #include "recog.h"
157 #include "basic-block.h"
158 #include "output.h"
159 #include "function.h"
160 #include "expr.h"
161 #include "except.h"
162 #include "ggc.h"
163 #include "params.h"
164 #include "cselib.h"
165 #include "intl.h"
166 #include "obstack.h"
167 #include "timevar.h"
168 #include "tree-pass.h"
169 #include "hashtab.h"
170 #include "df.h"
171 #include "dbgcnt.h"
172
173 /* Propagate flow information through back edges and thus enable PRE's
174 moving loop invariant calculations out of loops.
175
176 Originally this tended to create worse overall code, but several
177 improvements during the development of PRE seem to have made following
178 back edges generally a win.
179
180 Note much of the loop invariant code motion done here would normally
181 be done by loop.c, which has more heuristics for when to move invariants
182 out of loops. At some point we might need to move some of those
183 heuristics into gcse.c. */
184
185 /* We support GCSE via Partial Redundancy Elimination. PRE optimizations
186 are a superset of those done by GCSE.
187
188 We perform the following steps:
189
190 1) Compute table of places where registers are set.
191
192 2) Perform copy/constant propagation.
193
194 3) Perform global cse using lazy code motion if not optimizing
195 for size, or code hoisting if we are.
196
197 4) Perform another pass of copy/constant propagation. Try to bypass
198 conditional jumps if the condition can be computed from a value of
199 an incoming edge.
200
201 5) Perform store motion.
202
203 Two passes of copy/constant propagation are done because the first one
204 enables more GCSE and the second one helps to clean up the copies that
205 GCSE creates. This is needed more for PRE than for Classic because Classic
206 GCSE will try to use an existing register containing the common
207 subexpression rather than create a new one. This is harder to do for PRE
208 because of the code motion (which Classic GCSE doesn't do).
209
210 Expressions we are interested in GCSE-ing are of the form
211 (set (pseudo-reg) (expression)).
212 Function want_to_gcse_p says what these are.
213
214 In addition, expressions in REG_EQUAL notes are candidates for GXSE-ing.
215 This allows PRE to hoist expressions that are expressed in multiple insns,
216 such as comprex address calculations (e.g. for PIC code, or loads with a
217 high part and as lowe part).
218
219 PRE handles moving invariant expressions out of loops (by treating them as
220 partially redundant).
221
222 Eventually it would be nice to replace cse.c/gcse.c with SSA (static single
223 assignment) based GVN (global value numbering). L. T. Simpson's paper
224 (Rice University) on value numbering is a useful reference for this.
225
226 **********************
227
228 We used to support multiple passes but there are diminishing returns in
229 doing so. The first pass usually makes 90% of the changes that are doable.
230 A second pass can make a few more changes made possible by the first pass.
231 Experiments show any further passes don't make enough changes to justify
232 the expense.
233
234 A study of spec92 using an unlimited number of passes:
235 [1 pass] = 1208 substitutions, [2] = 577, [3] = 202, [4] = 192, [5] = 83,
236 [6] = 34, [7] = 17, [8] = 9, [9] = 4, [10] = 4, [11] = 2,
237 [12] = 2, [13] = 1, [15] = 1, [16] = 2, [41] = 1
238
239 It was found doing copy propagation between each pass enables further
240 substitutions.
241
242 This study was done before expressions in REG_EQUAL notes were added as
243 candidate expressions for optimization, and before the GIMPLE optimizers
244 were added. Probably, multiple passes is even less efficient now than
245 at the time when the study was conducted.
246
247 PRE is quite expensive in complicated functions because the DFA can take
248 a while to converge. Hence we only perform one pass.
249
250 **********************
251
252 The steps for PRE are:
253
254 1) Build the hash table of expressions we wish to GCSE (expr_hash_table).
255
256 2) Perform the data flow analysis for PRE.
257
258 3) Delete the redundant instructions
259
260 4) Insert the required copies [if any] that make the partially
261 redundant instructions fully redundant.
262
263 5) For other reaching expressions, insert an instruction to copy the value
264 to a newly created pseudo that will reach the redundant instruction.
265
266 The deletion is done first so that when we do insertions we
267 know which pseudo reg to use.
268
269 Various papers have argued that PRE DFA is expensive (O(n^2)) and others
270 argue it is not. The number of iterations for the algorithm to converge
271 is typically 2-4 so I don't view it as that expensive (relatively speaking).
272
273 PRE GCSE depends heavily on the second CSE pass to clean up the copies
274 we create. To make an expression reach the place where it's redundant,
275 the result of the expression is copied to a new register, and the redundant
276 expression is deleted by replacing it with this new register. Classic GCSE
277 doesn't have this problem as much as it computes the reaching defs of
278 each register in each block and thus can try to use an existing
279 register. */
280 \f
281 /* GCSE global vars. */
282
283 /* Set to non-zero if CSE should run after all GCSE optimizations are done. */
284 int flag_rerun_cse_after_global_opts;
285
286 /* An obstack for our working variables. */
287 static struct obstack gcse_obstack;
288
289 struct reg_use {rtx reg_rtx; };
290
291 /* Hash table of expressions. */
292
293 struct expr
294 {
295 /* The expression (SET_SRC for expressions, PATTERN for assignments). */
296 rtx expr;
297 /* Index in the available expression bitmaps. */
298 int bitmap_index;
299 /* Next entry with the same hash. */
300 struct expr *next_same_hash;
301 /* List of anticipatable occurrences in basic blocks in the function.
302 An "anticipatable occurrence" is one that is the first occurrence in the
303 basic block, the operands are not modified in the basic block prior
304 to the occurrence and the output is not used between the start of
305 the block and the occurrence. */
306 struct occr *antic_occr;
307 /* List of available occurrence in basic blocks in the function.
308 An "available occurrence" is one that is the last occurrence in the
309 basic block and the operands are not modified by following statements in
310 the basic block [including this insn]. */
311 struct occr *avail_occr;
312 /* Non-null if the computation is PRE redundant.
313 The value is the newly created pseudo-reg to record a copy of the
314 expression in all the places that reach the redundant copy. */
315 rtx reaching_reg;
316 };
317
318 /* Occurrence of an expression.
319 There is one per basic block. If a pattern appears more than once the
320 last appearance is used [or first for anticipatable expressions]. */
321
322 struct occr
323 {
324 /* Next occurrence of this expression. */
325 struct occr *next;
326 /* The insn that computes the expression. */
327 rtx insn;
328 /* Nonzero if this [anticipatable] occurrence has been deleted. */
329 char deleted_p;
330 /* Nonzero if this [available] occurrence has been copied to
331 reaching_reg. */
332 /* ??? This is mutually exclusive with deleted_p, so they could share
333 the same byte. */
334 char copied_p;
335 };
336
337 /* Expression and copy propagation hash tables.
338 Each hash table is an array of buckets.
339 ??? It is known that if it were an array of entries, structure elements
340 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
341 not clear whether in the final analysis a sufficient amount of memory would
342 be saved as the size of the available expression bitmaps would be larger
343 [one could build a mapping table without holes afterwards though].
344 Someday I'll perform the computation and figure it out. */
345
346 struct hash_table_d
347 {
348 /* The table itself.
349 This is an array of `expr_hash_table_size' elements. */
350 struct expr **table;
351
352 /* Size of the hash table, in elements. */
353 unsigned int size;
354
355 /* Number of hash table elements. */
356 unsigned int n_elems;
357
358 /* Whether the table is expression of copy propagation one. */
359 int set_p;
360 };
361
362 /* Expression hash table. */
363 static struct hash_table_d expr_hash_table;
364
365 /* Copy propagation hash table. */
366 static struct hash_table_d set_hash_table;
367
368 /* This is a list of expressions which are MEMs and will be used by load
369 or store motion.
370 Load motion tracks MEMs which aren't killed by
371 anything except itself. (i.e., loads and stores to a single location).
372 We can then allow movement of these MEM refs with a little special
373 allowance. (all stores copy the same value to the reaching reg used
374 for the loads). This means all values used to store into memory must have
375 no side effects so we can re-issue the setter value.
376 Store Motion uses this structure as an expression table to track stores
377 which look interesting, and might be moveable towards the exit block. */
378
379 struct ls_expr
380 {
381 struct expr * expr; /* Gcse expression reference for LM. */
382 rtx pattern; /* Pattern of this mem. */
383 rtx pattern_regs; /* List of registers mentioned by the mem. */
384 rtx loads; /* INSN list of loads seen. */
385 rtx stores; /* INSN list of stores seen. */
386 struct ls_expr * next; /* Next in the list. */
387 int invalid; /* Invalid for some reason. */
388 int index; /* If it maps to a bitmap index. */
389 unsigned int hash_index; /* Index when in a hash table. */
390 rtx reaching_reg; /* Register to use when re-writing. */
391 };
392
393 /* Array of implicit set patterns indexed by basic block index. */
394 static rtx *implicit_sets;
395
396 /* Head of the list of load/store memory refs. */
397 static struct ls_expr * pre_ldst_mems = NULL;
398
399 /* Hashtable for the load/store memory refs. */
400 static htab_t pre_ldst_table = NULL;
401
402 /* Bitmap containing one bit for each register in the program.
403 Used when performing GCSE to track which registers have been set since
404 the start of the basic block. */
405 static regset reg_set_bitmap;
406
407 /* Array, indexed by basic block number for a list of insns which modify
408 memory within that block. */
409 static rtx * modify_mem_list;
410 static bitmap modify_mem_list_set;
411
412 /* This array parallels modify_mem_list, but is kept canonicalized. */
413 static rtx * canon_modify_mem_list;
414
415 /* Bitmap indexed by block numbers to record which blocks contain
416 function calls. */
417 static bitmap blocks_with_calls;
418
419 /* Various variables for statistics gathering. */
420
421 /* Memory used in a pass.
422 This isn't intended to be absolutely precise. Its intent is only
423 to keep an eye on memory usage. */
424 static int bytes_used;
425
426 /* GCSE substitutions made. */
427 static int gcse_subst_count;
428 /* Number of copy instructions created. */
429 static int gcse_create_count;
430 /* Number of local constants propagated. */
431 static int local_const_prop_count;
432 /* Number of local copies propagated. */
433 static int local_copy_prop_count;
434 /* Number of global constants propagated. */
435 static int global_const_prop_count;
436 /* Number of global copies propagated. */
437 static int global_copy_prop_count;
438 \f
439 /* For available exprs */
440 static sbitmap *ae_kill;
441 \f
442 static void compute_can_copy (void);
443 static void *gmalloc (size_t) ATTRIBUTE_MALLOC;
444 static void *gcalloc (size_t, size_t) ATTRIBUTE_MALLOC;
445 static void *gcse_alloc (unsigned long);
446 static void alloc_gcse_mem (void);
447 static void free_gcse_mem (void);
448 static void hash_scan_insn (rtx, struct hash_table_d *);
449 static void hash_scan_set (rtx, rtx, struct hash_table_d *);
450 static void hash_scan_clobber (rtx, rtx, struct hash_table_d *);
451 static void hash_scan_call (rtx, rtx, struct hash_table_d *);
452 static int want_to_gcse_p (rtx);
453 static bool gcse_constant_p (const_rtx);
454 static int oprs_unchanged_p (const_rtx, const_rtx, int);
455 static int oprs_anticipatable_p (const_rtx, const_rtx);
456 static int oprs_available_p (const_rtx, const_rtx);
457 static void insert_expr_in_table (rtx, enum machine_mode, rtx, int, int,
458 struct hash_table_d *);
459 static void insert_set_in_table (rtx, rtx, struct hash_table_d *);
460 static unsigned int hash_expr (const_rtx, enum machine_mode, int *, int);
461 static unsigned int hash_set (int, int);
462 static int expr_equiv_p (const_rtx, const_rtx);
463 static void record_last_reg_set_info (rtx, int);
464 static void record_last_mem_set_info (rtx);
465 static void record_last_set_info (rtx, const_rtx, void *);
466 static void compute_hash_table (struct hash_table_d *);
467 static void alloc_hash_table (int, struct hash_table_d *, int);
468 static void free_hash_table (struct hash_table_d *);
469 static void compute_hash_table_work (struct hash_table_d *);
470 static void dump_hash_table (FILE *, const char *, struct hash_table_d *);
471 static struct expr *lookup_set (unsigned int, struct hash_table_d *);
472 static struct expr *next_set (unsigned int, struct expr *);
473 static void reset_opr_set_tables (void);
474 static int oprs_not_set_p (const_rtx, const_rtx);
475 static void mark_call (rtx);
476 static void mark_set (rtx, rtx);
477 static void mark_clobber (rtx, rtx);
478 static void mark_oprs_set (rtx);
479 static void alloc_cprop_mem (int, int);
480 static void free_cprop_mem (void);
481 static void compute_transp (const_rtx, int, sbitmap *, int);
482 static void compute_transpout (void);
483 static void compute_local_properties (sbitmap *, sbitmap *, sbitmap *,
484 struct hash_table_d *);
485 static void compute_cprop_data (void);
486 static void find_used_regs (rtx *, void *);
487 static int try_replace_reg (rtx, rtx, rtx);
488 static struct expr *find_avail_set (int, rtx);
489 static int cprop_jump (basic_block, rtx, rtx, rtx, rtx);
490 static void mems_conflict_for_gcse_p (rtx, const_rtx, void *);
491 static int load_killed_in_block_p (const_basic_block, int, const_rtx, int);
492 static void canon_list_insert (rtx, const_rtx, void *);
493 static int cprop_insn (rtx);
494 static void find_implicit_sets (void);
495 static int one_cprop_pass (void);
496 static bool constprop_register (rtx, rtx, rtx);
497 static struct expr *find_bypass_set (int, int);
498 static bool reg_killed_on_edge (const_rtx, const_edge);
499 static int bypass_block (basic_block, rtx, rtx);
500 static int bypass_conditional_jumps (void);
501 static void alloc_pre_mem (int, int);
502 static void free_pre_mem (void);
503 static void compute_pre_data (void);
504 static int pre_expr_reaches_here_p (basic_block, struct expr *,
505 basic_block);
506 static void insert_insn_end_basic_block (struct expr *, basic_block, int);
507 static void pre_insert_copy_insn (struct expr *, rtx);
508 static void pre_insert_copies (void);
509 static int pre_delete (void);
510 static int pre_gcse (void);
511 static int one_pre_gcse_pass (void);
512 static void add_label_notes (rtx, rtx);
513 static void alloc_code_hoist_mem (int, int);
514 static void free_code_hoist_mem (void);
515 static void compute_code_hoist_vbeinout (void);
516 static void compute_code_hoist_data (void);
517 static int hoist_expr_reaches_here_p (basic_block, int, basic_block, char *);
518 static int hoist_code (void);
519 static int one_code_hoisting_pass (void);
520 static rtx process_insert_insn (struct expr *);
521 static int pre_edge_insert (struct edge_list *, struct expr **);
522 static int pre_expr_reaches_here_p_work (basic_block, struct expr *,
523 basic_block, char *);
524 static struct ls_expr * ldst_entry (rtx);
525 static void free_ldst_entry (struct ls_expr *);
526 static void free_ldst_mems (void);
527 static void print_ldst_list (FILE *);
528 static struct ls_expr * find_rtx_in_ldst (rtx);
529 static inline struct ls_expr * first_ls_expr (void);
530 static inline struct ls_expr * next_ls_expr (struct ls_expr *);
531 static int simple_mem (const_rtx);
532 static void invalidate_any_buried_refs (rtx);
533 static void compute_ld_motion_mems (void);
534 static void trim_ld_motion_mems (void);
535 static void update_ld_motion_stores (struct expr *);
536 static void free_insn_expr_list_list (rtx *);
537 static void clear_modify_mem_tables (void);
538 static void free_modify_mem_tables (void);
539 static rtx gcse_emit_move_after (rtx, rtx, rtx);
540 static void local_cprop_find_used_regs (rtx *, void *);
541 static bool do_local_cprop (rtx, rtx);
542 static int local_cprop_pass (void);
543 static bool is_too_expensive (const char *);
544
545 #define GNEW(T) ((T *) gmalloc (sizeof (T)))
546 #define GCNEW(T) ((T *) gcalloc (1, sizeof (T)))
547
548 #define GNEWVEC(T, N) ((T *) gmalloc (sizeof (T) * (N)))
549 #define GCNEWVEC(T, N) ((T *) gcalloc ((N), sizeof (T)))
550
551 #define GNEWVAR(T, S) ((T *) gmalloc ((S)))
552 #define GCNEWVAR(T, S) ((T *) gcalloc (1, (S)))
553
554 #define GOBNEW(T) ((T *) gcse_alloc (sizeof (T)))
555 #define GOBNEWVAR(T, S) ((T *) gcse_alloc ((S)))
556 \f
557 /* Misc. utilities. */
558
559 /* Nonzero for each mode that supports (set (reg) (reg)).
560 This is trivially true for integer and floating point values.
561 It may or may not be true for condition codes. */
562 static char can_copy[(int) NUM_MACHINE_MODES];
563
564 /* Compute which modes support reg/reg copy operations. */
565
566 static void
567 compute_can_copy (void)
568 {
569 int i;
570 #ifndef AVOID_CCMODE_COPIES
571 rtx reg, insn;
572 #endif
573 memset (can_copy, 0, NUM_MACHINE_MODES);
574
575 start_sequence ();
576 for (i = 0; i < NUM_MACHINE_MODES; i++)
577 if (GET_MODE_CLASS (i) == MODE_CC)
578 {
579 #ifdef AVOID_CCMODE_COPIES
580 can_copy[i] = 0;
581 #else
582 reg = gen_rtx_REG ((enum machine_mode) i, LAST_VIRTUAL_REGISTER + 1);
583 insn = emit_insn (gen_rtx_SET (VOIDmode, reg, reg));
584 if (recog (PATTERN (insn), insn, NULL) >= 0)
585 can_copy[i] = 1;
586 #endif
587 }
588 else
589 can_copy[i] = 1;
590
591 end_sequence ();
592 }
593
594 /* Returns whether the mode supports reg/reg copy operations. */
595
596 bool
597 can_copy_p (enum machine_mode mode)
598 {
599 static bool can_copy_init_p = false;
600
601 if (! can_copy_init_p)
602 {
603 compute_can_copy ();
604 can_copy_init_p = true;
605 }
606
607 return can_copy[mode] != 0;
608 }
609
610 \f
611 /* Cover function to xmalloc to record bytes allocated. */
612
613 static void *
614 gmalloc (size_t size)
615 {
616 bytes_used += size;
617 return xmalloc (size);
618 }
619
620 /* Cover function to xcalloc to record bytes allocated. */
621
622 static void *
623 gcalloc (size_t nelem, size_t elsize)
624 {
625 bytes_used += nelem * elsize;
626 return xcalloc (nelem, elsize);
627 }
628
629 /* Cover function to obstack_alloc. */
630
631 static void *
632 gcse_alloc (unsigned long size)
633 {
634 bytes_used += size;
635 return obstack_alloc (&gcse_obstack, size);
636 }
637
638 /* Allocate memory for the reg/memory set tracking tables.
639 This is called at the start of each pass. */
640
641 static void
642 alloc_gcse_mem (void)
643 {
644 /* Allocate vars to track sets of regs. */
645 reg_set_bitmap = BITMAP_ALLOC (NULL);
646
647 /* Allocate array to keep a list of insns which modify memory in each
648 basic block. */
649 modify_mem_list = GCNEWVEC (rtx, last_basic_block);
650 canon_modify_mem_list = GCNEWVEC (rtx, last_basic_block);
651 modify_mem_list_set = BITMAP_ALLOC (NULL);
652 blocks_with_calls = BITMAP_ALLOC (NULL);
653 }
654
655 /* Free memory allocated by alloc_gcse_mem. */
656
657 static void
658 free_gcse_mem (void)
659 {
660 free_modify_mem_tables ();
661 BITMAP_FREE (modify_mem_list_set);
662 BITMAP_FREE (blocks_with_calls);
663 }
664 \f
665 /* Compute the local properties of each recorded expression.
666
667 Local properties are those that are defined by the block, irrespective of
668 other blocks.
669
670 An expression is transparent in a block if its operands are not modified
671 in the block.
672
673 An expression is computed (locally available) in a block if it is computed
674 at least once and expression would contain the same value if the
675 computation was moved to the end of the block.
676
677 An expression is locally anticipatable in a block if it is computed at
678 least once and expression would contain the same value if the computation
679 was moved to the beginning of the block.
680
681 We call this routine for cprop, pre and code hoisting. They all compute
682 basically the same information and thus can easily share this code.
683
684 TRANSP, COMP, and ANTLOC are destination sbitmaps for recording local
685 properties. If NULL, then it is not necessary to compute or record that
686 particular property.
687
688 TABLE controls which hash table to look at. If it is set hash table,
689 additionally, TRANSP is computed as ~TRANSP, since this is really cprop's
690 ABSALTERED. */
691
692 static void
693 compute_local_properties (sbitmap *transp, sbitmap *comp, sbitmap *antloc,
694 struct hash_table_d *table)
695 {
696 unsigned int i;
697
698 /* Initialize any bitmaps that were passed in. */
699 if (transp)
700 {
701 if (table->set_p)
702 sbitmap_vector_zero (transp, last_basic_block);
703 else
704 sbitmap_vector_ones (transp, last_basic_block);
705 }
706
707 if (comp)
708 sbitmap_vector_zero (comp, last_basic_block);
709 if (antloc)
710 sbitmap_vector_zero (antloc, last_basic_block);
711
712 for (i = 0; i < table->size; i++)
713 {
714 struct expr *expr;
715
716 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
717 {
718 int indx = expr->bitmap_index;
719 struct occr *occr;
720
721 /* The expression is transparent in this block if it is not killed.
722 We start by assuming all are transparent [none are killed], and
723 then reset the bits for those that are. */
724 if (transp)
725 compute_transp (expr->expr, indx, transp, table->set_p);
726
727 /* The occurrences recorded in antic_occr are exactly those that
728 we want to set to nonzero in ANTLOC. */
729 if (antloc)
730 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
731 {
732 SET_BIT (antloc[BLOCK_NUM (occr->insn)], indx);
733
734 /* While we're scanning the table, this is a good place to
735 initialize this. */
736 occr->deleted_p = 0;
737 }
738
739 /* The occurrences recorded in avail_occr are exactly those that
740 we want to set to nonzero in COMP. */
741 if (comp)
742 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
743 {
744 SET_BIT (comp[BLOCK_NUM (occr->insn)], indx);
745
746 /* While we're scanning the table, this is a good place to
747 initialize this. */
748 occr->copied_p = 0;
749 }
750
751 /* While we're scanning the table, this is a good place to
752 initialize this. */
753 expr->reaching_reg = 0;
754 }
755 }
756 }
757 \f
758 /* Hash table support. */
759
760 struct reg_avail_info
761 {
762 basic_block last_bb;
763 int first_set;
764 int last_set;
765 };
766
767 static struct reg_avail_info *reg_avail_info;
768 static basic_block current_bb;
769
770
771 /* See whether X, the source of a set, is something we want to consider for
772 GCSE. */
773
774 static int
775 want_to_gcse_p (rtx x)
776 {
777 #ifdef STACK_REGS
778 /* On register stack architectures, don't GCSE constants from the
779 constant pool, as the benefits are often swamped by the overhead
780 of shuffling the register stack between basic blocks. */
781 if (IS_STACK_MODE (GET_MODE (x)))
782 x = avoid_constant_pool_reference (x);
783 #endif
784
785 switch (GET_CODE (x))
786 {
787 case REG:
788 case SUBREG:
789 case CONST_INT:
790 case CONST_DOUBLE:
791 case CONST_FIXED:
792 case CONST_VECTOR:
793 case CALL:
794 return 0;
795
796 default:
797 return can_assign_to_reg_without_clobbers_p (x);
798 }
799 }
800
801 /* Used internally by can_assign_to_reg_without_clobbers_p. */
802
803 static GTY(()) rtx test_insn;
804
805 /* Return true if we can assign X to a pseudo register such that the
806 resulting insn does not result in clobbering a hard register as a
807 side-effect.
808 This function is typically used by code motion passes, to verify
809 that it is safe to insert an insn without worrying about clobbering
810 maybe live hard regs. */
811
812 bool
813 can_assign_to_reg_without_clobbers_p (rtx x)
814 {
815 int num_clobbers = 0;
816 int icode;
817
818 /* If this is a valid operand, we are OK. If it's VOIDmode, we aren't. */
819 if (general_operand (x, GET_MODE (x)))
820 return 1;
821 else if (GET_MODE (x) == VOIDmode)
822 return 0;
823
824 /* Otherwise, check if we can make a valid insn from it. First initialize
825 our test insn if we haven't already. */
826 if (test_insn == 0)
827 {
828 test_insn
829 = make_insn_raw (gen_rtx_SET (VOIDmode,
830 gen_rtx_REG (word_mode,
831 FIRST_PSEUDO_REGISTER * 2),
832 const0_rtx));
833 NEXT_INSN (test_insn) = PREV_INSN (test_insn) = 0;
834 }
835
836 /* Now make an insn like the one we would make when GCSE'ing and see if
837 valid. */
838 PUT_MODE (SET_DEST (PATTERN (test_insn)), GET_MODE (x));
839 SET_SRC (PATTERN (test_insn)) = x;
840 return ((icode = recog (PATTERN (test_insn), test_insn, &num_clobbers)) >= 0
841 && (num_clobbers == 0 || ! added_clobbers_hard_reg_p (icode)));
842 }
843
844 /* Return nonzero if the operands of expression X are unchanged from the
845 start of INSN's basic block up to but not including INSN (if AVAIL_P == 0),
846 or from INSN to the end of INSN's basic block (if AVAIL_P != 0). */
847
848 static int
849 oprs_unchanged_p (const_rtx x, const_rtx insn, int avail_p)
850 {
851 int i, j;
852 enum rtx_code code;
853 const char *fmt;
854
855 if (x == 0)
856 return 1;
857
858 code = GET_CODE (x);
859 switch (code)
860 {
861 case REG:
862 {
863 struct reg_avail_info *info = &reg_avail_info[REGNO (x)];
864
865 if (info->last_bb != current_bb)
866 return 1;
867 if (avail_p)
868 return info->last_set < DF_INSN_LUID (insn);
869 else
870 return info->first_set >= DF_INSN_LUID (insn);
871 }
872
873 case MEM:
874 if (load_killed_in_block_p (current_bb, DF_INSN_LUID (insn),
875 x, avail_p))
876 return 0;
877 else
878 return oprs_unchanged_p (XEXP (x, 0), insn, avail_p);
879
880 case PRE_DEC:
881 case PRE_INC:
882 case POST_DEC:
883 case POST_INC:
884 case PRE_MODIFY:
885 case POST_MODIFY:
886 return 0;
887
888 case PC:
889 case CC0: /*FIXME*/
890 case CONST:
891 case CONST_INT:
892 case CONST_DOUBLE:
893 case CONST_FIXED:
894 case CONST_VECTOR:
895 case SYMBOL_REF:
896 case LABEL_REF:
897 case ADDR_VEC:
898 case ADDR_DIFF_VEC:
899 return 1;
900
901 default:
902 break;
903 }
904
905 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
906 {
907 if (fmt[i] == 'e')
908 {
909 /* If we are about to do the last recursive call needed at this
910 level, change it into iteration. This function is called enough
911 to be worth it. */
912 if (i == 0)
913 return oprs_unchanged_p (XEXP (x, i), insn, avail_p);
914
915 else if (! oprs_unchanged_p (XEXP (x, i), insn, avail_p))
916 return 0;
917 }
918 else if (fmt[i] == 'E')
919 for (j = 0; j < XVECLEN (x, i); j++)
920 if (! oprs_unchanged_p (XVECEXP (x, i, j), insn, avail_p))
921 return 0;
922 }
923
924 return 1;
925 }
926
927 /* Used for communication between mems_conflict_for_gcse_p and
928 load_killed_in_block_p. Nonzero if mems_conflict_for_gcse_p finds a
929 conflict between two memory references. */
930 static int gcse_mems_conflict_p;
931
932 /* Used for communication between mems_conflict_for_gcse_p and
933 load_killed_in_block_p. A memory reference for a load instruction,
934 mems_conflict_for_gcse_p will see if a memory store conflicts with
935 this memory load. */
936 static const_rtx gcse_mem_operand;
937
938 /* DEST is the output of an instruction. If it is a memory reference, and
939 possibly conflicts with the load found in gcse_mem_operand, then set
940 gcse_mems_conflict_p to a nonzero value. */
941
942 static void
943 mems_conflict_for_gcse_p (rtx dest, const_rtx setter ATTRIBUTE_UNUSED,
944 void *data ATTRIBUTE_UNUSED)
945 {
946 while (GET_CODE (dest) == SUBREG
947 || GET_CODE (dest) == ZERO_EXTRACT
948 || GET_CODE (dest) == STRICT_LOW_PART)
949 dest = XEXP (dest, 0);
950
951 /* If DEST is not a MEM, then it will not conflict with the load. Note
952 that function calls are assumed to clobber memory, but are handled
953 elsewhere. */
954 if (! MEM_P (dest))
955 return;
956
957 /* If we are setting a MEM in our list of specially recognized MEMs,
958 don't mark as killed this time. */
959
960 if (expr_equiv_p (dest, gcse_mem_operand) && pre_ldst_mems != NULL)
961 {
962 if (!find_rtx_in_ldst (dest))
963 gcse_mems_conflict_p = 1;
964 return;
965 }
966
967 if (true_dependence (dest, GET_MODE (dest), gcse_mem_operand,
968 rtx_addr_varies_p))
969 gcse_mems_conflict_p = 1;
970 }
971
972 /* Return nonzero if the expression in X (a memory reference) is killed
973 in block BB before or after the insn with the LUID in UID_LIMIT.
974 AVAIL_P is nonzero for kills after UID_LIMIT, and zero for kills
975 before UID_LIMIT.
976
977 To check the entire block, set UID_LIMIT to max_uid + 1 and
978 AVAIL_P to 0. */
979
980 static int
981 load_killed_in_block_p (const_basic_block bb, int uid_limit, const_rtx x, int avail_p)
982 {
983 rtx list_entry = modify_mem_list[bb->index];
984
985 /* If this is a readonly then we aren't going to be changing it. */
986 if (MEM_READONLY_P (x))
987 return 0;
988
989 while (list_entry)
990 {
991 rtx setter;
992 /* Ignore entries in the list that do not apply. */
993 if ((avail_p
994 && DF_INSN_LUID (XEXP (list_entry, 0)) < uid_limit)
995 || (! avail_p
996 && DF_INSN_LUID (XEXP (list_entry, 0)) > uid_limit))
997 {
998 list_entry = XEXP (list_entry, 1);
999 continue;
1000 }
1001
1002 setter = XEXP (list_entry, 0);
1003
1004 /* If SETTER is a call everything is clobbered. Note that calls
1005 to pure functions are never put on the list, so we need not
1006 worry about them. */
1007 if (CALL_P (setter))
1008 return 1;
1009
1010 /* SETTER must be an INSN of some kind that sets memory. Call
1011 note_stores to examine each hunk of memory that is modified.
1012
1013 The note_stores interface is pretty limited, so we have to
1014 communicate via global variables. Yuk. */
1015 gcse_mem_operand = x;
1016 gcse_mems_conflict_p = 0;
1017 note_stores (PATTERN (setter), mems_conflict_for_gcse_p, NULL);
1018 if (gcse_mems_conflict_p)
1019 return 1;
1020 list_entry = XEXP (list_entry, 1);
1021 }
1022 return 0;
1023 }
1024
1025 /* Return nonzero if the operands of expression X are unchanged from
1026 the start of INSN's basic block up to but not including INSN. */
1027
1028 static int
1029 oprs_anticipatable_p (const_rtx x, const_rtx insn)
1030 {
1031 return oprs_unchanged_p (x, insn, 0);
1032 }
1033
1034 /* Return nonzero if the operands of expression X are unchanged from
1035 INSN to the end of INSN's basic block. */
1036
1037 static int
1038 oprs_available_p (const_rtx x, const_rtx insn)
1039 {
1040 return oprs_unchanged_p (x, insn, 1);
1041 }
1042
1043 /* Hash expression X.
1044
1045 MODE is only used if X is a CONST_INT. DO_NOT_RECORD_P is a boolean
1046 indicating if a volatile operand is found or if the expression contains
1047 something we don't want to insert in the table. HASH_TABLE_SIZE is
1048 the current size of the hash table to be probed. */
1049
1050 static unsigned int
1051 hash_expr (const_rtx x, enum machine_mode mode, int *do_not_record_p,
1052 int hash_table_size)
1053 {
1054 unsigned int hash;
1055
1056 *do_not_record_p = 0;
1057
1058 hash = hash_rtx (x, mode, do_not_record_p,
1059 NULL, /*have_reg_qty=*/false);
1060 return hash % hash_table_size;
1061 }
1062
1063 /* Hash a set of register REGNO.
1064
1065 Sets are hashed on the register that is set. This simplifies the PRE copy
1066 propagation code.
1067
1068 ??? May need to make things more elaborate. Later, as necessary. */
1069
1070 static unsigned int
1071 hash_set (int regno, int hash_table_size)
1072 {
1073 unsigned int hash;
1074
1075 hash = regno;
1076 return hash % hash_table_size;
1077 }
1078
1079 /* Return nonzero if exp1 is equivalent to exp2. */
1080
1081 static int
1082 expr_equiv_p (const_rtx x, const_rtx y)
1083 {
1084 return exp_equiv_p (x, y, 0, true);
1085 }
1086
1087 /* Insert expression X in INSN in the hash TABLE.
1088 If it is already present, record it as the last occurrence in INSN's
1089 basic block.
1090
1091 MODE is the mode of the value X is being stored into.
1092 It is only used if X is a CONST_INT.
1093
1094 ANTIC_P is nonzero if X is an anticipatable expression.
1095 AVAIL_P is nonzero if X is an available expression. */
1096
1097 static void
1098 insert_expr_in_table (rtx x, enum machine_mode mode, rtx insn, int antic_p,
1099 int avail_p, struct hash_table_d *table)
1100 {
1101 int found, do_not_record_p;
1102 unsigned int hash;
1103 struct expr *cur_expr, *last_expr = NULL;
1104 struct occr *antic_occr, *avail_occr;
1105
1106 hash = hash_expr (x, mode, &do_not_record_p, table->size);
1107
1108 /* Do not insert expression in table if it contains volatile operands,
1109 or if hash_expr determines the expression is something we don't want
1110 to or can't handle. */
1111 if (do_not_record_p)
1112 return;
1113
1114 cur_expr = table->table[hash];
1115 found = 0;
1116
1117 while (cur_expr && 0 == (found = expr_equiv_p (cur_expr->expr, x)))
1118 {
1119 /* If the expression isn't found, save a pointer to the end of
1120 the list. */
1121 last_expr = cur_expr;
1122 cur_expr = cur_expr->next_same_hash;
1123 }
1124
1125 if (! found)
1126 {
1127 cur_expr = GOBNEW (struct expr);
1128 bytes_used += sizeof (struct expr);
1129 if (table->table[hash] == NULL)
1130 /* This is the first pattern that hashed to this index. */
1131 table->table[hash] = cur_expr;
1132 else
1133 /* Add EXPR to end of this hash chain. */
1134 last_expr->next_same_hash = cur_expr;
1135
1136 /* Set the fields of the expr element. */
1137 cur_expr->expr = x;
1138 cur_expr->bitmap_index = table->n_elems++;
1139 cur_expr->next_same_hash = NULL;
1140 cur_expr->antic_occr = NULL;
1141 cur_expr->avail_occr = NULL;
1142 }
1143
1144 /* Now record the occurrence(s). */
1145 if (antic_p)
1146 {
1147 antic_occr = cur_expr->antic_occr;
1148
1149 if (antic_occr && BLOCK_NUM (antic_occr->insn) != BLOCK_NUM (insn))
1150 antic_occr = NULL;
1151
1152 if (antic_occr)
1153 /* Found another instance of the expression in the same basic block.
1154 Prefer the currently recorded one. We want the first one in the
1155 block and the block is scanned from start to end. */
1156 ; /* nothing to do */
1157 else
1158 {
1159 /* First occurrence of this expression in this basic block. */
1160 antic_occr = GOBNEW (struct occr);
1161 bytes_used += sizeof (struct occr);
1162 antic_occr->insn = insn;
1163 antic_occr->next = cur_expr->antic_occr;
1164 antic_occr->deleted_p = 0;
1165 cur_expr->antic_occr = antic_occr;
1166 }
1167 }
1168
1169 if (avail_p)
1170 {
1171 avail_occr = cur_expr->avail_occr;
1172
1173 if (avail_occr && BLOCK_NUM (avail_occr->insn) == BLOCK_NUM (insn))
1174 {
1175 /* Found another instance of the expression in the same basic block.
1176 Prefer this occurrence to the currently recorded one. We want
1177 the last one in the block and the block is scanned from start
1178 to end. */
1179 avail_occr->insn = insn;
1180 }
1181 else
1182 {
1183 /* First occurrence of this expression in this basic block. */
1184 avail_occr = GOBNEW (struct occr);
1185 bytes_used += sizeof (struct occr);
1186 avail_occr->insn = insn;
1187 avail_occr->next = cur_expr->avail_occr;
1188 avail_occr->deleted_p = 0;
1189 cur_expr->avail_occr = avail_occr;
1190 }
1191 }
1192 }
1193
1194 /* Insert pattern X in INSN in the hash table.
1195 X is a SET of a reg to either another reg or a constant.
1196 If it is already present, record it as the last occurrence in INSN's
1197 basic block. */
1198
1199 static void
1200 insert_set_in_table (rtx x, rtx insn, struct hash_table_d *table)
1201 {
1202 int found;
1203 unsigned int hash;
1204 struct expr *cur_expr, *last_expr = NULL;
1205 struct occr *cur_occr;
1206
1207 gcc_assert (GET_CODE (x) == SET && REG_P (SET_DEST (x)));
1208
1209 hash = hash_set (REGNO (SET_DEST (x)), table->size);
1210
1211 cur_expr = table->table[hash];
1212 found = 0;
1213
1214 while (cur_expr && 0 == (found = expr_equiv_p (cur_expr->expr, x)))
1215 {
1216 /* If the expression isn't found, save a pointer to the end of
1217 the list. */
1218 last_expr = cur_expr;
1219 cur_expr = cur_expr->next_same_hash;
1220 }
1221
1222 if (! found)
1223 {
1224 cur_expr = GOBNEW (struct expr);
1225 bytes_used += sizeof (struct expr);
1226 if (table->table[hash] == NULL)
1227 /* This is the first pattern that hashed to this index. */
1228 table->table[hash] = cur_expr;
1229 else
1230 /* Add EXPR to end of this hash chain. */
1231 last_expr->next_same_hash = cur_expr;
1232
1233 /* Set the fields of the expr element.
1234 We must copy X because it can be modified when copy propagation is
1235 performed on its operands. */
1236 cur_expr->expr = copy_rtx (x);
1237 cur_expr->bitmap_index = table->n_elems++;
1238 cur_expr->next_same_hash = NULL;
1239 cur_expr->antic_occr = NULL;
1240 cur_expr->avail_occr = NULL;
1241 }
1242
1243 /* Now record the occurrence. */
1244 cur_occr = cur_expr->avail_occr;
1245
1246 if (cur_occr && BLOCK_NUM (cur_occr->insn) == BLOCK_NUM (insn))
1247 {
1248 /* Found another instance of the expression in the same basic block.
1249 Prefer this occurrence to the currently recorded one. We want
1250 the last one in the block and the block is scanned from start
1251 to end. */
1252 cur_occr->insn = insn;
1253 }
1254 else
1255 {
1256 /* First occurrence of this expression in this basic block. */
1257 cur_occr = GOBNEW (struct occr);
1258 bytes_used += sizeof (struct occr);
1259 cur_occr->insn = insn;
1260 cur_occr->next = cur_expr->avail_occr;
1261 cur_occr->deleted_p = 0;
1262 cur_expr->avail_occr = cur_occr;
1263 }
1264 }
1265
1266 /* Determine whether the rtx X should be treated as a constant for
1267 the purposes of GCSE's constant propagation. */
1268
1269 static bool
1270 gcse_constant_p (const_rtx x)
1271 {
1272 /* Consider a COMPARE of two integers constant. */
1273 if (GET_CODE (x) == COMPARE
1274 && GET_CODE (XEXP (x, 0)) == CONST_INT
1275 && GET_CODE (XEXP (x, 1)) == CONST_INT)
1276 return true;
1277
1278 /* Consider a COMPARE of the same registers is a constant
1279 if they are not floating point registers. */
1280 if (GET_CODE(x) == COMPARE
1281 && REG_P (XEXP (x, 0)) && REG_P (XEXP (x, 1))
1282 && REGNO (XEXP (x, 0)) == REGNO (XEXP (x, 1))
1283 && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 0)))
1284 && ! FLOAT_MODE_P (GET_MODE (XEXP (x, 1))))
1285 return true;
1286
1287 /* Since X might be inserted more than once we have to take care that it
1288 is sharable. */
1289 return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
1290 }
1291
1292 /* Scan pattern PAT of INSN and add an entry to the hash TABLE (set or
1293 expression one). */
1294
1295 static void
1296 hash_scan_set (rtx pat, rtx insn, struct hash_table_d *table)
1297 {
1298 rtx src = SET_SRC (pat);
1299 rtx dest = SET_DEST (pat);
1300 rtx note;
1301
1302 if (GET_CODE (src) == CALL)
1303 hash_scan_call (src, insn, table);
1304
1305 else if (REG_P (dest))
1306 {
1307 unsigned int regno = REGNO (dest);
1308 rtx tmp;
1309
1310 /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
1311
1312 This allows us to do a single GCSE pass and still eliminate
1313 redundant constants, addresses or other expressions that are
1314 constructed with multiple instructions.
1315
1316 However, keep the original SRC if INSN is a simple reg-reg move. In
1317 In this case, there will almost always be a REG_EQUAL note on the
1318 insn that sets SRC. By recording the REG_EQUAL value here as SRC
1319 for INSN, we miss copy propagation opportunities and we perform the
1320 same PRE GCSE operation repeatedly on the same REG_EQUAL value if we
1321 do more than one PRE GCSE pass.
1322
1323 Note that this does not impede profitable constant propagations. We
1324 "look through" reg-reg sets in lookup_avail_set. */
1325 note = find_reg_equal_equiv_note (insn);
1326 if (note != 0
1327 && REG_NOTE_KIND (note) == REG_EQUAL
1328 && !REG_P (src)
1329 && (table->set_p
1330 ? gcse_constant_p (XEXP (note, 0))
1331 : want_to_gcse_p (XEXP (note, 0))))
1332 src = XEXP (note, 0), pat = gen_rtx_SET (VOIDmode, dest, src);
1333
1334 /* Only record sets of pseudo-regs in the hash table. */
1335 if (! table->set_p
1336 && regno >= FIRST_PSEUDO_REGISTER
1337 /* Don't GCSE something if we can't do a reg/reg copy. */
1338 && can_copy_p (GET_MODE (dest))
1339 /* GCSE commonly inserts instruction after the insn. We can't
1340 do that easily for EH_REGION notes so disable GCSE on these
1341 for now. */
1342 && !find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1343 /* Is SET_SRC something we want to gcse? */
1344 && want_to_gcse_p (src)
1345 /* Don't CSE a nop. */
1346 && ! set_noop_p (pat)
1347 /* Don't GCSE if it has attached REG_EQUIV note.
1348 At this point this only function parameters should have
1349 REG_EQUIV notes and if the argument slot is used somewhere
1350 explicitly, it means address of parameter has been taken,
1351 so we should not extend the lifetime of the pseudo. */
1352 && (note == NULL_RTX || ! MEM_P (XEXP (note, 0))))
1353 {
1354 /* An expression is not anticipatable if its operands are
1355 modified before this insn or if this is not the only SET in
1356 this insn. The latter condition does not have to mean that
1357 SRC itself is not anticipatable, but we just will not be
1358 able to handle code motion of insns with multiple sets. */
1359 int antic_p = oprs_anticipatable_p (src, insn)
1360 && !multiple_sets (insn);
1361 /* An expression is not available if its operands are
1362 subsequently modified, including this insn. It's also not
1363 available if this is a branch, because we can't insert
1364 a set after the branch. */
1365 int avail_p = (oprs_available_p (src, insn)
1366 && ! JUMP_P (insn));
1367
1368 insert_expr_in_table (src, GET_MODE (dest), insn, antic_p, avail_p, table);
1369 }
1370
1371 /* Record sets for constant/copy propagation. */
1372 else if (table->set_p
1373 && regno >= FIRST_PSEUDO_REGISTER
1374 && ((REG_P (src)
1375 && REGNO (src) >= FIRST_PSEUDO_REGISTER
1376 && can_copy_p (GET_MODE (dest))
1377 && REGNO (src) != regno)
1378 || gcse_constant_p (src))
1379 /* A copy is not available if its src or dest is subsequently
1380 modified. Here we want to search from INSN+1 on, but
1381 oprs_available_p searches from INSN on. */
1382 && (insn == BB_END (BLOCK_FOR_INSN (insn))
1383 || (tmp = next_nonnote_insn (insn)) == NULL_RTX
1384 || BLOCK_FOR_INSN (tmp) != BLOCK_FOR_INSN (insn)
1385 || oprs_available_p (pat, tmp)))
1386 insert_set_in_table (pat, insn, table);
1387 }
1388 /* In case of store we want to consider the memory value as available in
1389 the REG stored in that memory. This makes it possible to remove
1390 redundant loads from due to stores to the same location. */
1391 else if (flag_gcse_las && REG_P (src) && MEM_P (dest))
1392 {
1393 unsigned int regno = REGNO (src);
1394
1395 /* Do not do this for constant/copy propagation. */
1396 if (! table->set_p
1397 /* Only record sets of pseudo-regs in the hash table. */
1398 && regno >= FIRST_PSEUDO_REGISTER
1399 /* Don't GCSE something if we can't do a reg/reg copy. */
1400 && can_copy_p (GET_MODE (src))
1401 /* GCSE commonly inserts instruction after the insn. We can't
1402 do that easily for EH_REGION notes so disable GCSE on these
1403 for now. */
1404 && ! find_reg_note (insn, REG_EH_REGION, NULL_RTX)
1405 /* Is SET_DEST something we want to gcse? */
1406 && want_to_gcse_p (dest)
1407 /* Don't CSE a nop. */
1408 && ! set_noop_p (pat)
1409 /* Don't GCSE if it has attached REG_EQUIV note.
1410 At this point this only function parameters should have
1411 REG_EQUIV notes and if the argument slot is used somewhere
1412 explicitly, it means address of parameter has been taken,
1413 so we should not extend the lifetime of the pseudo. */
1414 && ((note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) == 0
1415 || ! MEM_P (XEXP (note, 0))))
1416 {
1417 /* Stores are never anticipatable. */
1418 int antic_p = 0;
1419 /* An expression is not available if its operands are
1420 subsequently modified, including this insn. It's also not
1421 available if this is a branch, because we can't insert
1422 a set after the branch. */
1423 int avail_p = oprs_available_p (dest, insn)
1424 && ! JUMP_P (insn);
1425
1426 /* Record the memory expression (DEST) in the hash table. */
1427 insert_expr_in_table (dest, GET_MODE (dest), insn,
1428 antic_p, avail_p, table);
1429 }
1430 }
1431 }
1432
1433 static void
1434 hash_scan_clobber (rtx x ATTRIBUTE_UNUSED, rtx insn ATTRIBUTE_UNUSED,
1435 struct hash_table_d *table ATTRIBUTE_UNUSED)
1436 {
1437 /* Currently nothing to do. */
1438 }
1439
1440 static void
1441 hash_scan_call (rtx x ATTRIBUTE_UNUSED, rtx insn ATTRIBUTE_UNUSED,
1442 struct hash_table_d *table ATTRIBUTE_UNUSED)
1443 {
1444 /* Currently nothing to do. */
1445 }
1446
1447 /* Process INSN and add hash table entries as appropriate.
1448
1449 Only available expressions that set a single pseudo-reg are recorded.
1450
1451 Single sets in a PARALLEL could be handled, but it's an extra complication
1452 that isn't dealt with right now. The trick is handling the CLOBBERs that
1453 are also in the PARALLEL. Later.
1454
1455 If SET_P is nonzero, this is for the assignment hash table,
1456 otherwise it is for the expression hash table. */
1457
1458 static void
1459 hash_scan_insn (rtx insn, struct hash_table_d *table)
1460 {
1461 rtx pat = PATTERN (insn);
1462 int i;
1463
1464 /* Pick out the sets of INSN and for other forms of instructions record
1465 what's been modified. */
1466
1467 if (GET_CODE (pat) == SET)
1468 hash_scan_set (pat, insn, table);
1469 else if (GET_CODE (pat) == PARALLEL)
1470 for (i = 0; i < XVECLEN (pat, 0); i++)
1471 {
1472 rtx x = XVECEXP (pat, 0, i);
1473
1474 if (GET_CODE (x) == SET)
1475 hash_scan_set (x, insn, table);
1476 else if (GET_CODE (x) == CLOBBER)
1477 hash_scan_clobber (x, insn, table);
1478 else if (GET_CODE (x) == CALL)
1479 hash_scan_call (x, insn, table);
1480 }
1481
1482 else if (GET_CODE (pat) == CLOBBER)
1483 hash_scan_clobber (pat, insn, table);
1484 else if (GET_CODE (pat) == CALL)
1485 hash_scan_call (pat, insn, table);
1486 }
1487
1488 static void
1489 dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
1490 {
1491 int i;
1492 /* Flattened out table, so it's printed in proper order. */
1493 struct expr **flat_table;
1494 unsigned int *hash_val;
1495 struct expr *expr;
1496
1497 flat_table = XCNEWVEC (struct expr *, table->n_elems);
1498 hash_val = XNEWVEC (unsigned int, table->n_elems);
1499
1500 for (i = 0; i < (int) table->size; i++)
1501 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
1502 {
1503 flat_table[expr->bitmap_index] = expr;
1504 hash_val[expr->bitmap_index] = i;
1505 }
1506
1507 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
1508 name, table->size, table->n_elems);
1509
1510 for (i = 0; i < (int) table->n_elems; i++)
1511 if (flat_table[i] != 0)
1512 {
1513 expr = flat_table[i];
1514 fprintf (file, "Index %d (hash value %d)\n ",
1515 expr->bitmap_index, hash_val[i]);
1516 print_rtl (file, expr->expr);
1517 fprintf (file, "\n");
1518 }
1519
1520 fprintf (file, "\n");
1521
1522 free (flat_table);
1523 free (hash_val);
1524 }
1525
1526 /* Record register first/last/block set information for REGNO in INSN.
1527
1528 first_set records the first place in the block where the register
1529 is set and is used to compute "anticipatability".
1530
1531 last_set records the last place in the block where the register
1532 is set and is used to compute "availability".
1533
1534 last_bb records the block for which first_set and last_set are
1535 valid, as a quick test to invalidate them. */
1536
1537 static void
1538 record_last_reg_set_info (rtx insn, int regno)
1539 {
1540 struct reg_avail_info *info = &reg_avail_info[regno];
1541 int luid = DF_INSN_LUID (insn);
1542
1543 info->last_set = luid;
1544 if (info->last_bb != current_bb)
1545 {
1546 info->last_bb = current_bb;
1547 info->first_set = luid;
1548 }
1549 }
1550
1551
1552 /* Record all of the canonicalized MEMs of record_last_mem_set_info's insn.
1553 Note we store a pair of elements in the list, so they have to be
1554 taken off pairwise. */
1555
1556 static void
1557 canon_list_insert (rtx dest ATTRIBUTE_UNUSED, const_rtx unused1 ATTRIBUTE_UNUSED,
1558 void * v_insn)
1559 {
1560 rtx dest_addr, insn;
1561 int bb;
1562
1563 while (GET_CODE (dest) == SUBREG
1564 || GET_CODE (dest) == ZERO_EXTRACT
1565 || GET_CODE (dest) == STRICT_LOW_PART)
1566 dest = XEXP (dest, 0);
1567
1568 /* If DEST is not a MEM, then it will not conflict with a load. Note
1569 that function calls are assumed to clobber memory, but are handled
1570 elsewhere. */
1571
1572 if (! MEM_P (dest))
1573 return;
1574
1575 dest_addr = get_addr (XEXP (dest, 0));
1576 dest_addr = canon_rtx (dest_addr);
1577 insn = (rtx) v_insn;
1578 bb = BLOCK_NUM (insn);
1579
1580 canon_modify_mem_list[bb] =
1581 alloc_EXPR_LIST (VOIDmode, dest_addr, canon_modify_mem_list[bb]);
1582 canon_modify_mem_list[bb] =
1583 alloc_EXPR_LIST (VOIDmode, dest, canon_modify_mem_list[bb]);
1584 }
1585
1586 /* Record memory modification information for INSN. We do not actually care
1587 about the memory location(s) that are set, or even how they are set (consider
1588 a CALL_INSN). We merely need to record which insns modify memory. */
1589
1590 static void
1591 record_last_mem_set_info (rtx insn)
1592 {
1593 int bb = BLOCK_NUM (insn);
1594
1595 /* load_killed_in_block_p will handle the case of calls clobbering
1596 everything. */
1597 modify_mem_list[bb] = alloc_INSN_LIST (insn, modify_mem_list[bb]);
1598 bitmap_set_bit (modify_mem_list_set, bb);
1599
1600 if (CALL_P (insn))
1601 {
1602 /* Note that traversals of this loop (other than for free-ing)
1603 will break after encountering a CALL_INSN. So, there's no
1604 need to insert a pair of items, as canon_list_insert does. */
1605 canon_modify_mem_list[bb] =
1606 alloc_INSN_LIST (insn, canon_modify_mem_list[bb]);
1607 bitmap_set_bit (blocks_with_calls, bb);
1608 }
1609 else
1610 note_stores (PATTERN (insn), canon_list_insert, (void*) insn);
1611 }
1612
1613 /* Called from compute_hash_table via note_stores to handle one
1614 SET or CLOBBER in an insn. DATA is really the instruction in which
1615 the SET is taking place. */
1616
1617 static void
1618 record_last_set_info (rtx dest, const_rtx setter ATTRIBUTE_UNUSED, void *data)
1619 {
1620 rtx last_set_insn = (rtx) data;
1621
1622 if (GET_CODE (dest) == SUBREG)
1623 dest = SUBREG_REG (dest);
1624
1625 if (REG_P (dest))
1626 record_last_reg_set_info (last_set_insn, REGNO (dest));
1627 else if (MEM_P (dest)
1628 /* Ignore pushes, they clobber nothing. */
1629 && ! push_operand (dest, GET_MODE (dest)))
1630 record_last_mem_set_info (last_set_insn);
1631 }
1632
1633 /* Top level function to create an expression or assignment hash table.
1634
1635 Expression entries are placed in the hash table if
1636 - they are of the form (set (pseudo-reg) src),
1637 - src is something we want to perform GCSE on,
1638 - none of the operands are subsequently modified in the block
1639
1640 Assignment entries are placed in the hash table if
1641 - they are of the form (set (pseudo-reg) src),
1642 - src is something we want to perform const/copy propagation on,
1643 - none of the operands or target are subsequently modified in the block
1644
1645 Currently src must be a pseudo-reg or a const_int.
1646
1647 TABLE is the table computed. */
1648
1649 static void
1650 compute_hash_table_work (struct hash_table_d *table)
1651 {
1652 int i;
1653
1654 /* re-Cache any INSN_LIST nodes we have allocated. */
1655 clear_modify_mem_tables ();
1656 /* Some working arrays used to track first and last set in each block. */
1657 reg_avail_info = GNEWVEC (struct reg_avail_info, max_reg_num ());
1658
1659 for (i = 0; i < max_reg_num (); ++i)
1660 reg_avail_info[i].last_bb = NULL;
1661
1662 FOR_EACH_BB (current_bb)
1663 {
1664 rtx insn;
1665 unsigned int regno;
1666
1667 /* First pass over the instructions records information used to
1668 determine when registers and memory are first and last set. */
1669 FOR_BB_INSNS (current_bb, insn)
1670 {
1671 if (! INSN_P (insn))
1672 continue;
1673
1674 if (CALL_P (insn))
1675 {
1676 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1677 if (TEST_HARD_REG_BIT (regs_invalidated_by_call, regno))
1678 record_last_reg_set_info (insn, regno);
1679
1680 mark_call (insn);
1681 }
1682
1683 note_stores (PATTERN (insn), record_last_set_info, insn);
1684 }
1685
1686 /* Insert implicit sets in the hash table. */
1687 if (table->set_p
1688 && implicit_sets[current_bb->index] != NULL_RTX)
1689 hash_scan_set (implicit_sets[current_bb->index],
1690 BB_HEAD (current_bb), table);
1691
1692 /* The next pass builds the hash table. */
1693 FOR_BB_INSNS (current_bb, insn)
1694 if (INSN_P (insn))
1695 hash_scan_insn (insn, table);
1696 }
1697
1698 free (reg_avail_info);
1699 reg_avail_info = NULL;
1700 }
1701
1702 /* Allocate space for the set/expr hash TABLE.
1703 N_INSNS is the number of instructions in the function.
1704 It is used to determine the number of buckets to use.
1705 SET_P determines whether set or expression table will
1706 be created. */
1707
1708 static void
1709 alloc_hash_table (int n_insns, struct hash_table_d *table, int set_p)
1710 {
1711 int n;
1712
1713 table->size = n_insns / 4;
1714 if (table->size < 11)
1715 table->size = 11;
1716
1717 /* Attempt to maintain efficient use of hash table.
1718 Making it an odd number is simplest for now.
1719 ??? Later take some measurements. */
1720 table->size |= 1;
1721 n = table->size * sizeof (struct expr *);
1722 table->table = GNEWVAR (struct expr *, n);
1723 table->set_p = set_p;
1724 }
1725
1726 /* Free things allocated by alloc_hash_table. */
1727
1728 static void
1729 free_hash_table (struct hash_table_d *table)
1730 {
1731 free (table->table);
1732 }
1733
1734 /* Compute the hash TABLE for doing copy/const propagation or
1735 expression hash table. */
1736
1737 static void
1738 compute_hash_table (struct hash_table_d *table)
1739 {
1740 /* Initialize count of number of entries in hash table. */
1741 table->n_elems = 0;
1742 memset (table->table, 0, table->size * sizeof (struct expr *));
1743
1744 compute_hash_table_work (table);
1745 }
1746 \f
1747 /* Expression tracking support. */
1748
1749 /* Lookup REGNO in the set TABLE. The result is a pointer to the
1750 table entry, or NULL if not found. */
1751
1752 static struct expr *
1753 lookup_set (unsigned int regno, struct hash_table_d *table)
1754 {
1755 unsigned int hash = hash_set (regno, table->size);
1756 struct expr *expr;
1757
1758 expr = table->table[hash];
1759
1760 while (expr && REGNO (SET_DEST (expr->expr)) != regno)
1761 expr = expr->next_same_hash;
1762
1763 return expr;
1764 }
1765
1766 /* Return the next entry for REGNO in list EXPR. */
1767
1768 static struct expr *
1769 next_set (unsigned int regno, struct expr *expr)
1770 {
1771 do
1772 expr = expr->next_same_hash;
1773 while (expr && REGNO (SET_DEST (expr->expr)) != regno);
1774
1775 return expr;
1776 }
1777
1778 /* Like free_INSN_LIST_list or free_EXPR_LIST_list, except that the node
1779 types may be mixed. */
1780
1781 static void
1782 free_insn_expr_list_list (rtx *listp)
1783 {
1784 rtx list, next;
1785
1786 for (list = *listp; list ; list = next)
1787 {
1788 next = XEXP (list, 1);
1789 if (GET_CODE (list) == EXPR_LIST)
1790 free_EXPR_LIST_node (list);
1791 else
1792 free_INSN_LIST_node (list);
1793 }
1794
1795 *listp = NULL;
1796 }
1797
1798 /* Clear canon_modify_mem_list and modify_mem_list tables. */
1799 static void
1800 clear_modify_mem_tables (void)
1801 {
1802 unsigned i;
1803 bitmap_iterator bi;
1804
1805 EXECUTE_IF_SET_IN_BITMAP (modify_mem_list_set, 0, i, bi)
1806 {
1807 free_INSN_LIST_list (modify_mem_list + i);
1808 free_insn_expr_list_list (canon_modify_mem_list + i);
1809 }
1810 bitmap_clear (modify_mem_list_set);
1811 bitmap_clear (blocks_with_calls);
1812 }
1813
1814 /* Release memory used by modify_mem_list_set. */
1815
1816 static void
1817 free_modify_mem_tables (void)
1818 {
1819 clear_modify_mem_tables ();
1820 free (modify_mem_list);
1821 free (canon_modify_mem_list);
1822 modify_mem_list = 0;
1823 canon_modify_mem_list = 0;
1824 }
1825
1826 /* Reset tables used to keep track of what's still available [since the
1827 start of the block]. */
1828
1829 static void
1830 reset_opr_set_tables (void)
1831 {
1832 /* Maintain a bitmap of which regs have been set since beginning of
1833 the block. */
1834 CLEAR_REG_SET (reg_set_bitmap);
1835
1836 /* Also keep a record of the last instruction to modify memory.
1837 For now this is very trivial, we only record whether any memory
1838 location has been modified. */
1839 clear_modify_mem_tables ();
1840 }
1841
1842 /* Return nonzero if the operands of X are not set before INSN in
1843 INSN's basic block. */
1844
1845 static int
1846 oprs_not_set_p (const_rtx x, const_rtx insn)
1847 {
1848 int i, j;
1849 enum rtx_code code;
1850 const char *fmt;
1851
1852 if (x == 0)
1853 return 1;
1854
1855 code = GET_CODE (x);
1856 switch (code)
1857 {
1858 case PC:
1859 case CC0:
1860 case CONST:
1861 case CONST_INT:
1862 case CONST_DOUBLE:
1863 case CONST_FIXED:
1864 case CONST_VECTOR:
1865 case SYMBOL_REF:
1866 case LABEL_REF:
1867 case ADDR_VEC:
1868 case ADDR_DIFF_VEC:
1869 return 1;
1870
1871 case MEM:
1872 if (load_killed_in_block_p (BLOCK_FOR_INSN (insn),
1873 DF_INSN_LUID (insn), x, 0))
1874 return 0;
1875 else
1876 return oprs_not_set_p (XEXP (x, 0), insn);
1877
1878 case REG:
1879 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
1880
1881 default:
1882 break;
1883 }
1884
1885 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
1886 {
1887 if (fmt[i] == 'e')
1888 {
1889 /* If we are about to do the last recursive call
1890 needed at this level, change it into iteration.
1891 This function is called enough to be worth it. */
1892 if (i == 0)
1893 return oprs_not_set_p (XEXP (x, i), insn);
1894
1895 if (! oprs_not_set_p (XEXP (x, i), insn))
1896 return 0;
1897 }
1898 else if (fmt[i] == 'E')
1899 for (j = 0; j < XVECLEN (x, i); j++)
1900 if (! oprs_not_set_p (XVECEXP (x, i, j), insn))
1901 return 0;
1902 }
1903
1904 return 1;
1905 }
1906
1907 /* Mark things set by a CALL. */
1908
1909 static void
1910 mark_call (rtx insn)
1911 {
1912 if (! RTL_CONST_OR_PURE_CALL_P (insn))
1913 record_last_mem_set_info (insn);
1914 }
1915
1916 /* Mark things set by a SET. */
1917
1918 static void
1919 mark_set (rtx pat, rtx insn)
1920 {
1921 rtx dest = SET_DEST (pat);
1922
1923 while (GET_CODE (dest) == SUBREG
1924 || GET_CODE (dest) == ZERO_EXTRACT
1925 || GET_CODE (dest) == STRICT_LOW_PART)
1926 dest = XEXP (dest, 0);
1927
1928 if (REG_P (dest))
1929 SET_REGNO_REG_SET (reg_set_bitmap, REGNO (dest));
1930 else if (MEM_P (dest))
1931 record_last_mem_set_info (insn);
1932
1933 if (GET_CODE (SET_SRC (pat)) == CALL)
1934 mark_call (insn);
1935 }
1936
1937 /* Record things set by a CLOBBER. */
1938
1939 static void
1940 mark_clobber (rtx pat, rtx insn)
1941 {
1942 rtx clob = XEXP (pat, 0);
1943
1944 while (GET_CODE (clob) == SUBREG || GET_CODE (clob) == STRICT_LOW_PART)
1945 clob = XEXP (clob, 0);
1946
1947 if (REG_P (clob))
1948 SET_REGNO_REG_SET (reg_set_bitmap, REGNO (clob));
1949 else
1950 record_last_mem_set_info (insn);
1951 }
1952
1953 /* Record things set by INSN.
1954 This data is used by oprs_not_set_p. */
1955
1956 static void
1957 mark_oprs_set (rtx insn)
1958 {
1959 rtx pat = PATTERN (insn);
1960 int i;
1961
1962 if (GET_CODE (pat) == SET)
1963 mark_set (pat, insn);
1964 else if (GET_CODE (pat) == PARALLEL)
1965 for (i = 0; i < XVECLEN (pat, 0); i++)
1966 {
1967 rtx x = XVECEXP (pat, 0, i);
1968
1969 if (GET_CODE (x) == SET)
1970 mark_set (x, insn);
1971 else if (GET_CODE (x) == CLOBBER)
1972 mark_clobber (x, insn);
1973 else if (GET_CODE (x) == CALL)
1974 mark_call (insn);
1975 }
1976
1977 else if (GET_CODE (pat) == CLOBBER)
1978 mark_clobber (pat, insn);
1979 else if (GET_CODE (pat) == CALL)
1980 mark_call (insn);
1981 }
1982
1983 \f
1984 /* Compute copy/constant propagation working variables. */
1985
1986 /* Local properties of assignments. */
1987 static sbitmap *cprop_pavloc;
1988 static sbitmap *cprop_absaltered;
1989
1990 /* Global properties of assignments (computed from the local properties). */
1991 static sbitmap *cprop_avin;
1992 static sbitmap *cprop_avout;
1993
1994 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
1995 basic blocks. N_SETS is the number of sets. */
1996
1997 static void
1998 alloc_cprop_mem (int n_blocks, int n_sets)
1999 {
2000 cprop_pavloc = sbitmap_vector_alloc (n_blocks, n_sets);
2001 cprop_absaltered = sbitmap_vector_alloc (n_blocks, n_sets);
2002
2003 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
2004 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
2005 }
2006
2007 /* Free vars used by copy/const propagation. */
2008
2009 static void
2010 free_cprop_mem (void)
2011 {
2012 sbitmap_vector_free (cprop_pavloc);
2013 sbitmap_vector_free (cprop_absaltered);
2014 sbitmap_vector_free (cprop_avin);
2015 sbitmap_vector_free (cprop_avout);
2016 }
2017
2018 /* For each block, compute whether X is transparent. X is either an
2019 expression or an assignment [though we don't care which, for this context
2020 an assignment is treated as an expression]. For each block where an
2021 element of X is modified, set (SET_P == 1) or reset (SET_P == 0) the INDX
2022 bit in BMAP. */
2023
2024 static void
2025 compute_transp (const_rtx x, int indx, sbitmap *bmap, int set_p)
2026 {
2027 int i, j;
2028 enum rtx_code code;
2029 const char *fmt;
2030
2031 /* repeat is used to turn tail-recursion into iteration since GCC
2032 can't do it when there's no return value. */
2033 repeat:
2034
2035 if (x == 0)
2036 return;
2037
2038 code = GET_CODE (x);
2039 switch (code)
2040 {
2041 case REG:
2042 if (set_p)
2043 {
2044 df_ref def;
2045 for (def = DF_REG_DEF_CHAIN (REGNO (x));
2046 def;
2047 def = DF_REF_NEXT_REG (def))
2048 SET_BIT (bmap[DF_REF_BB (def)->index], indx);
2049 }
2050 else
2051 {
2052 df_ref def;
2053 for (def = DF_REG_DEF_CHAIN (REGNO (x));
2054 def;
2055 def = DF_REF_NEXT_REG (def))
2056 RESET_BIT (bmap[DF_REF_BB (def)->index], indx);
2057 }
2058
2059 return;
2060
2061 case MEM:
2062 if (! MEM_READONLY_P (x))
2063 {
2064 bitmap_iterator bi;
2065 unsigned bb_index;
2066
2067 /* First handle all the blocks with calls. We don't need to
2068 do any list walking for them. */
2069 EXECUTE_IF_SET_IN_BITMAP (blocks_with_calls, 0, bb_index, bi)
2070 {
2071 if (set_p)
2072 SET_BIT (bmap[bb_index], indx);
2073 else
2074 RESET_BIT (bmap[bb_index], indx);
2075 }
2076
2077 /* Now iterate over the blocks which have memory modifications
2078 but which do not have any calls. */
2079 EXECUTE_IF_AND_COMPL_IN_BITMAP (modify_mem_list_set,
2080 blocks_with_calls,
2081 0, bb_index, bi)
2082 {
2083 rtx list_entry = canon_modify_mem_list[bb_index];
2084
2085 while (list_entry)
2086 {
2087 rtx dest, dest_addr;
2088
2089 /* LIST_ENTRY must be an INSN of some kind that sets memory.
2090 Examine each hunk of memory that is modified. */
2091
2092 dest = XEXP (list_entry, 0);
2093 list_entry = XEXP (list_entry, 1);
2094 dest_addr = XEXP (list_entry, 0);
2095
2096 if (canon_true_dependence (dest, GET_MODE (dest), dest_addr,
2097 x, NULL_RTX, rtx_addr_varies_p))
2098 {
2099 if (set_p)
2100 SET_BIT (bmap[bb_index], indx);
2101 else
2102 RESET_BIT (bmap[bb_index], indx);
2103 break;
2104 }
2105 list_entry = XEXP (list_entry, 1);
2106 }
2107 }
2108 }
2109
2110 x = XEXP (x, 0);
2111 goto repeat;
2112
2113 case PC:
2114 case CC0: /*FIXME*/
2115 case CONST:
2116 case CONST_INT:
2117 case CONST_DOUBLE:
2118 case CONST_FIXED:
2119 case CONST_VECTOR:
2120 case SYMBOL_REF:
2121 case LABEL_REF:
2122 case ADDR_VEC:
2123 case ADDR_DIFF_VEC:
2124 return;
2125
2126 default:
2127 break;
2128 }
2129
2130 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
2131 {
2132 if (fmt[i] == 'e')
2133 {
2134 /* If we are about to do the last recursive call
2135 needed at this level, change it into iteration.
2136 This function is called enough to be worth it. */
2137 if (i == 0)
2138 {
2139 x = XEXP (x, i);
2140 goto repeat;
2141 }
2142
2143 compute_transp (XEXP (x, i), indx, bmap, set_p);
2144 }
2145 else if (fmt[i] == 'E')
2146 for (j = 0; j < XVECLEN (x, i); j++)
2147 compute_transp (XVECEXP (x, i, j), indx, bmap, set_p);
2148 }
2149 }
2150
2151 /* Top level routine to do the dataflow analysis needed by copy/const
2152 propagation. */
2153
2154 static void
2155 compute_cprop_data (void)
2156 {
2157 compute_local_properties (cprop_absaltered, cprop_pavloc, NULL, &set_hash_table);
2158 compute_available (cprop_pavloc, cprop_absaltered,
2159 cprop_avout, cprop_avin);
2160 }
2161 \f
2162 /* Copy/constant propagation. */
2163
2164 /* Maximum number of register uses in an insn that we handle. */
2165 #define MAX_USES 8
2166
2167 /* Table of uses found in an insn.
2168 Allocated statically to avoid alloc/free complexity and overhead. */
2169 static struct reg_use reg_use_table[MAX_USES];
2170
2171 /* Index into `reg_use_table' while building it. */
2172 static int reg_use_count;
2173
2174 /* Set up a list of register numbers used in INSN. The found uses are stored
2175 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
2176 and contains the number of uses in the table upon exit.
2177
2178 ??? If a register appears multiple times we will record it multiple times.
2179 This doesn't hurt anything but it will slow things down. */
2180
2181 static void
2182 find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
2183 {
2184 int i, j;
2185 enum rtx_code code;
2186 const char *fmt;
2187 rtx x = *xptr;
2188
2189 /* repeat is used to turn tail-recursion into iteration since GCC
2190 can't do it when there's no return value. */
2191 repeat:
2192 if (x == 0)
2193 return;
2194
2195 code = GET_CODE (x);
2196 if (REG_P (x))
2197 {
2198 if (reg_use_count == MAX_USES)
2199 return;
2200
2201 reg_use_table[reg_use_count].reg_rtx = x;
2202 reg_use_count++;
2203 }
2204
2205 /* Recursively scan the operands of this expression. */
2206
2207 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
2208 {
2209 if (fmt[i] == 'e')
2210 {
2211 /* If we are about to do the last recursive call
2212 needed at this level, change it into iteration.
2213 This function is called enough to be worth it. */
2214 if (i == 0)
2215 {
2216 x = XEXP (x, 0);
2217 goto repeat;
2218 }
2219
2220 find_used_regs (&XEXP (x, i), data);
2221 }
2222 else if (fmt[i] == 'E')
2223 for (j = 0; j < XVECLEN (x, i); j++)
2224 find_used_regs (&XVECEXP (x, i, j), data);
2225 }
2226 }
2227
2228 /* Try to replace all non-SET_DEST occurrences of FROM in INSN with TO.
2229 Returns nonzero is successful. */
2230
2231 static int
2232 try_replace_reg (rtx from, rtx to, rtx insn)
2233 {
2234 rtx note = find_reg_equal_equiv_note (insn);
2235 rtx src = 0;
2236 int success = 0;
2237 rtx set = single_set (insn);
2238
2239 /* Usually we substitute easy stuff, so we won't copy everything.
2240 We however need to take care to not duplicate non-trivial CONST
2241 expressions. */
2242 to = copy_rtx (to);
2243
2244 validate_replace_src_group (from, to, insn);
2245 if (num_changes_pending () && apply_change_group ())
2246 success = 1;
2247
2248 /* Try to simplify SET_SRC if we have substituted a constant. */
2249 if (success && set && CONSTANT_P (to))
2250 {
2251 src = simplify_rtx (SET_SRC (set));
2252
2253 if (src)
2254 validate_change (insn, &SET_SRC (set), src, 0);
2255 }
2256
2257 /* If there is already a REG_EQUAL note, update the expression in it
2258 with our replacement. */
2259 if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
2260 set_unique_reg_note (insn, REG_EQUAL,
2261 simplify_replace_rtx (XEXP (note, 0), from,
2262 copy_rtx (to)));
2263 if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
2264 {
2265 /* If above failed and this is a single set, try to simplify the source of
2266 the set given our substitution. We could perhaps try this for multiple
2267 SETs, but it probably won't buy us anything. */
2268 src = simplify_replace_rtx (SET_SRC (set), from, to);
2269
2270 if (!rtx_equal_p (src, SET_SRC (set))
2271 && validate_change (insn, &SET_SRC (set), src, 0))
2272 success = 1;
2273
2274 /* If we've failed to do replacement, have a single SET, don't already
2275 have a note, and have no special SET, add a REG_EQUAL note to not
2276 lose information. */
2277 if (!success && note == 0 && set != 0
2278 && GET_CODE (SET_DEST (set)) != ZERO_EXTRACT
2279 && GET_CODE (SET_DEST (set)) != STRICT_LOW_PART)
2280 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
2281 }
2282
2283 /* REG_EQUAL may get simplified into register.
2284 We don't allow that. Remove that note. This code ought
2285 not to happen, because previous code ought to synthesize
2286 reg-reg move, but be on the safe side. */
2287 if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
2288 remove_note (insn, note);
2289
2290 return success;
2291 }
2292
2293 /* Find a set of REGNOs that are available on entry to INSN's block. Returns
2294 NULL no such set is found. */
2295
2296 static struct expr *
2297 find_avail_set (int regno, rtx insn)
2298 {
2299 /* SET1 contains the last set found that can be returned to the caller for
2300 use in a substitution. */
2301 struct expr *set1 = 0;
2302
2303 /* Loops are not possible here. To get a loop we would need two sets
2304 available at the start of the block containing INSN. i.e. we would
2305 need two sets like this available at the start of the block:
2306
2307 (set (reg X) (reg Y))
2308 (set (reg Y) (reg X))
2309
2310 This can not happen since the set of (reg Y) would have killed the
2311 set of (reg X) making it unavailable at the start of this block. */
2312 while (1)
2313 {
2314 rtx src;
2315 struct expr *set = lookup_set (regno, &set_hash_table);
2316
2317 /* Find a set that is available at the start of the block
2318 which contains INSN. */
2319 while (set)
2320 {
2321 if (TEST_BIT (cprop_avin[BLOCK_NUM (insn)], set->bitmap_index))
2322 break;
2323 set = next_set (regno, set);
2324 }
2325
2326 /* If no available set was found we've reached the end of the
2327 (possibly empty) copy chain. */
2328 if (set == 0)
2329 break;
2330
2331 gcc_assert (GET_CODE (set->expr) == SET);
2332
2333 src = SET_SRC (set->expr);
2334
2335 /* We know the set is available.
2336 Now check that SRC is ANTLOC (i.e. none of the source operands
2337 have changed since the start of the block).
2338
2339 If the source operand changed, we may still use it for the next
2340 iteration of this loop, but we may not use it for substitutions. */
2341
2342 if (gcse_constant_p (src) || oprs_not_set_p (src, insn))
2343 set1 = set;
2344
2345 /* If the source of the set is anything except a register, then
2346 we have reached the end of the copy chain. */
2347 if (! REG_P (src))
2348 break;
2349
2350 /* Follow the copy chain, i.e. start another iteration of the loop
2351 and see if we have an available copy into SRC. */
2352 regno = REGNO (src);
2353 }
2354
2355 /* SET1 holds the last set that was available and anticipatable at
2356 INSN. */
2357 return set1;
2358 }
2359
2360 /* Subroutine of cprop_insn that tries to propagate constants into
2361 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
2362 it is the instruction that immediately precedes JUMP, and must be a
2363 single SET of a register. FROM is what we will try to replace,
2364 SRC is the constant we will try to substitute for it. Returns nonzero
2365 if a change was made. */
2366
2367 static int
2368 cprop_jump (basic_block bb, rtx setcc, rtx jump, rtx from, rtx src)
2369 {
2370 rtx new_rtx, set_src, note_src;
2371 rtx set = pc_set (jump);
2372 rtx note = find_reg_equal_equiv_note (jump);
2373
2374 if (note)
2375 {
2376 note_src = XEXP (note, 0);
2377 if (GET_CODE (note_src) == EXPR_LIST)
2378 note_src = NULL_RTX;
2379 }
2380 else note_src = NULL_RTX;
2381
2382 /* Prefer REG_EQUAL notes except those containing EXPR_LISTs. */
2383 set_src = note_src ? note_src : SET_SRC (set);
2384
2385 /* First substitute the SETCC condition into the JUMP instruction,
2386 then substitute that given values into this expanded JUMP. */
2387 if (setcc != NULL_RTX
2388 && !modified_between_p (from, setcc, jump)
2389 && !modified_between_p (src, setcc, jump))
2390 {
2391 rtx setcc_src;
2392 rtx setcc_set = single_set (setcc);
2393 rtx setcc_note = find_reg_equal_equiv_note (setcc);
2394 setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
2395 ? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
2396 set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
2397 setcc_src);
2398 }
2399 else
2400 setcc = NULL_RTX;
2401
2402 new_rtx = simplify_replace_rtx (set_src, from, src);
2403
2404 /* If no simplification can be made, then try the next register. */
2405 if (rtx_equal_p (new_rtx, SET_SRC (set)))
2406 return 0;
2407
2408 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
2409 if (new_rtx == pc_rtx)
2410 delete_insn (jump);
2411 else
2412 {
2413 /* Ensure the value computed inside the jump insn to be equivalent
2414 to one computed by setcc. */
2415 if (setcc && modified_in_p (new_rtx, setcc))
2416 return 0;
2417 if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
2418 {
2419 /* When (some) constants are not valid in a comparison, and there
2420 are two registers to be replaced by constants before the entire
2421 comparison can be folded into a constant, we need to keep
2422 intermediate information in REG_EQUAL notes. For targets with
2423 separate compare insns, such notes are added by try_replace_reg.
2424 When we have a combined compare-and-branch instruction, however,
2425 we need to attach a note to the branch itself to make this
2426 optimization work. */
2427
2428 if (!rtx_equal_p (new_rtx, note_src))
2429 set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
2430 return 0;
2431 }
2432
2433 /* Remove REG_EQUAL note after simplification. */
2434 if (note_src)
2435 remove_note (jump, note);
2436 }
2437
2438 #ifdef HAVE_cc0
2439 /* Delete the cc0 setter. */
2440 if (setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
2441 delete_insn (setcc);
2442 #endif
2443
2444 global_const_prop_count++;
2445 if (dump_file != NULL)
2446 {
2447 fprintf (dump_file,
2448 "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with constant ",
2449 REGNO (from), INSN_UID (jump));
2450 print_rtl (dump_file, src);
2451 fprintf (dump_file, "\n");
2452 }
2453 purge_dead_edges (bb);
2454
2455 /* If a conditional jump has been changed into unconditional jump, remove
2456 the jump and make the edge fallthru - this is always called in
2457 cfglayout mode. */
2458 if (new_rtx != pc_rtx && simplejump_p (jump))
2459 {
2460 edge e;
2461 edge_iterator ei;
2462
2463 for (ei = ei_start (bb->succs); (e = ei_safe_edge (ei)); ei_next (&ei))
2464 if (e->dest != EXIT_BLOCK_PTR
2465 && BB_HEAD (e->dest) == JUMP_LABEL (jump))
2466 {
2467 e->flags |= EDGE_FALLTHRU;
2468 break;
2469 }
2470 delete_insn (jump);
2471 }
2472
2473 return 1;
2474 }
2475
2476 static bool
2477 constprop_register (rtx insn, rtx from, rtx to)
2478 {
2479 rtx sset;
2480
2481 /* Check for reg or cc0 setting instructions followed by
2482 conditional branch instructions first. */
2483 if ((sset = single_set (insn)) != NULL
2484 && NEXT_INSN (insn)
2485 && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
2486 {
2487 rtx dest = SET_DEST (sset);
2488 if ((REG_P (dest) || CC0_P (dest))
2489 && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn), from, to))
2490 return 1;
2491 }
2492
2493 /* Handle normal insns next. */
2494 if (NONJUMP_INSN_P (insn)
2495 && try_replace_reg (from, to, insn))
2496 return 1;
2497
2498 /* Try to propagate a CONST_INT into a conditional jump.
2499 We're pretty specific about what we will handle in this
2500 code, we can extend this as necessary over time.
2501
2502 Right now the insn in question must look like
2503 (set (pc) (if_then_else ...)) */
2504 else if (any_condjump_p (insn) && onlyjump_p (insn))
2505 return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, to);
2506 return 0;
2507 }
2508
2509 /* Perform constant and copy propagation on INSN.
2510 The result is nonzero if a change was made. */
2511
2512 static int
2513 cprop_insn (rtx insn)
2514 {
2515 struct reg_use *reg_used;
2516 int changed = 0;
2517 rtx note;
2518
2519 if (!INSN_P (insn))
2520 return 0;
2521
2522 reg_use_count = 0;
2523 note_uses (&PATTERN (insn), find_used_regs, NULL);
2524
2525 note = find_reg_equal_equiv_note (insn);
2526
2527 /* We may win even when propagating constants into notes. */
2528 if (note)
2529 find_used_regs (&XEXP (note, 0), NULL);
2530
2531 for (reg_used = &reg_use_table[0]; reg_use_count > 0;
2532 reg_used++, reg_use_count--)
2533 {
2534 unsigned int regno = REGNO (reg_used->reg_rtx);
2535 rtx pat, src;
2536 struct expr *set;
2537
2538 /* If the register has already been set in this block, there's
2539 nothing we can do. */
2540 if (! oprs_not_set_p (reg_used->reg_rtx, insn))
2541 continue;
2542
2543 /* Find an assignment that sets reg_used and is available
2544 at the start of the block. */
2545 set = find_avail_set (regno, insn);
2546 if (! set)
2547 continue;
2548
2549 pat = set->expr;
2550 /* ??? We might be able to handle PARALLELs. Later. */
2551 gcc_assert (GET_CODE (pat) == SET);
2552
2553 src = SET_SRC (pat);
2554
2555 /* Constant propagation. */
2556 if (gcse_constant_p (src))
2557 {
2558 if (constprop_register (insn, reg_used->reg_rtx, src))
2559 {
2560 changed = 1;
2561 global_const_prop_count++;
2562 if (dump_file != NULL)
2563 {
2564 fprintf (dump_file, "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
2565 fprintf (dump_file, "insn %d with constant ", INSN_UID (insn));
2566 print_rtl (dump_file, src);
2567 fprintf (dump_file, "\n");
2568 }
2569 if (INSN_DELETED_P (insn))
2570 return 1;
2571 }
2572 }
2573 else if (REG_P (src)
2574 && REGNO (src) >= FIRST_PSEUDO_REGISTER
2575 && REGNO (src) != regno)
2576 {
2577 if (try_replace_reg (reg_used->reg_rtx, src, insn))
2578 {
2579 changed = 1;
2580 global_copy_prop_count++;
2581 if (dump_file != NULL)
2582 {
2583 fprintf (dump_file, "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
2584 regno, INSN_UID (insn));
2585 fprintf (dump_file, " with reg %d\n", REGNO (src));
2586 }
2587
2588 /* The original insn setting reg_used may or may not now be
2589 deletable. We leave the deletion to flow. */
2590 /* FIXME: If it turns out that the insn isn't deletable,
2591 then we may have unnecessarily extended register lifetimes
2592 and made things worse. */
2593 }
2594 }
2595 }
2596
2597 return changed;
2598 }
2599
2600 /* Like find_used_regs, but avoid recording uses that appear in
2601 input-output contexts such as zero_extract or pre_dec. This
2602 restricts the cases we consider to those for which local cprop
2603 can legitimately make replacements. */
2604
2605 static void
2606 local_cprop_find_used_regs (rtx *xptr, void *data)
2607 {
2608 rtx x = *xptr;
2609
2610 if (x == 0)
2611 return;
2612
2613 switch (GET_CODE (x))
2614 {
2615 case ZERO_EXTRACT:
2616 case SIGN_EXTRACT:
2617 case STRICT_LOW_PART:
2618 return;
2619
2620 case PRE_DEC:
2621 case PRE_INC:
2622 case POST_DEC:
2623 case POST_INC:
2624 case PRE_MODIFY:
2625 case POST_MODIFY:
2626 /* Can only legitimately appear this early in the context of
2627 stack pushes for function arguments, but handle all of the
2628 codes nonetheless. */
2629 return;
2630
2631 case SUBREG:
2632 /* Setting a subreg of a register larger than word_mode leaves
2633 the non-written words unchanged. */
2634 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) > BITS_PER_WORD)
2635 return;
2636 break;
2637
2638 default:
2639 break;
2640 }
2641
2642 find_used_regs (xptr, data);
2643 }
2644
2645 /* Try to perform local const/copy propagation on X in INSN. */
2646
2647 static bool
2648 do_local_cprop (rtx x, rtx insn)
2649 {
2650 rtx newreg = NULL, newcnst = NULL;
2651
2652 /* Rule out USE instructions and ASM statements as we don't want to
2653 change the hard registers mentioned. */
2654 if (REG_P (x)
2655 && (REGNO (x) >= FIRST_PSEUDO_REGISTER
2656 || (GET_CODE (PATTERN (insn)) != USE
2657 && asm_noperands (PATTERN (insn)) < 0)))
2658 {
2659 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0);
2660 struct elt_loc_list *l;
2661
2662 if (!val)
2663 return false;
2664 for (l = val->locs; l; l = l->next)
2665 {
2666 rtx this_rtx = l->loc;
2667 rtx note;
2668
2669 if (gcse_constant_p (this_rtx))
2670 newcnst = this_rtx;
2671 if (REG_P (this_rtx) && REGNO (this_rtx) >= FIRST_PSEUDO_REGISTER
2672 /* Don't copy propagate if it has attached REG_EQUIV note.
2673 At this point this only function parameters should have
2674 REG_EQUIV notes and if the argument slot is used somewhere
2675 explicitly, it means address of parameter has been taken,
2676 so we should not extend the lifetime of the pseudo. */
2677 && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
2678 || ! MEM_P (XEXP (note, 0))))
2679 newreg = this_rtx;
2680 }
2681 if (newcnst && constprop_register (insn, x, newcnst))
2682 {
2683 if (dump_file != NULL)
2684 {
2685 fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
2686 REGNO (x));
2687 fprintf (dump_file, "insn %d with constant ",
2688 INSN_UID (insn));
2689 print_rtl (dump_file, newcnst);
2690 fprintf (dump_file, "\n");
2691 }
2692 local_const_prop_count++;
2693 return true;
2694 }
2695 else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
2696 {
2697 if (dump_file != NULL)
2698 {
2699 fprintf (dump_file,
2700 "LOCAL COPY-PROP: Replacing reg %d in insn %d",
2701 REGNO (x), INSN_UID (insn));
2702 fprintf (dump_file, " with reg %d\n", REGNO (newreg));
2703 }
2704 local_copy_prop_count++;
2705 return true;
2706 }
2707 }
2708 return false;
2709 }
2710
2711 /* Do local const/copy propagation (i.e. within each basic block). */
2712
2713 static int
2714 local_cprop_pass (void)
2715 {
2716 basic_block bb;
2717 rtx insn;
2718 struct reg_use *reg_used;
2719 bool changed = false;
2720
2721 cselib_init (false);
2722 FOR_EACH_BB (bb)
2723 {
2724 FOR_BB_INSNS (bb, insn)
2725 {
2726 if (INSN_P (insn))
2727 {
2728 rtx note = find_reg_equal_equiv_note (insn);
2729 do
2730 {
2731 reg_use_count = 0;
2732 note_uses (&PATTERN (insn), local_cprop_find_used_regs,
2733 NULL);
2734 if (note)
2735 local_cprop_find_used_regs (&XEXP (note, 0), NULL);
2736
2737 for (reg_used = &reg_use_table[0]; reg_use_count > 0;
2738 reg_used++, reg_use_count--)
2739 {
2740 if (do_local_cprop (reg_used->reg_rtx, insn))
2741 {
2742 changed = true;
2743 break;
2744 }
2745 }
2746 if (INSN_DELETED_P (insn))
2747 break;
2748 }
2749 while (reg_use_count);
2750 }
2751 cselib_process_insn (insn);
2752 }
2753
2754 /* Forget everything at the end of a basic block. */
2755 cselib_clear_table ();
2756 }
2757
2758 cselib_finish ();
2759
2760 return changed;
2761 }
2762
2763 /* Similar to get_condition, only the resulting condition must be
2764 valid at JUMP, instead of at EARLIEST.
2765
2766 This differs from noce_get_condition in ifcvt.c in that we prefer not to
2767 settle for the condition variable in the jump instruction being integral.
2768 We prefer to be able to record the value of a user variable, rather than
2769 the value of a temporary used in a condition. This could be solved by
2770 recording the value of *every* register scanned by canonicalize_condition,
2771 but this would require some code reorganization. */
2772
2773 rtx
2774 fis_get_condition (rtx jump)
2775 {
2776 return get_condition (jump, NULL, false, true);
2777 }
2778
2779 /* Check the comparison COND to see if we can safely form an implicit set from
2780 it. COND is either an EQ or NE comparison. */
2781
2782 static bool
2783 implicit_set_cond_p (const_rtx cond)
2784 {
2785 const enum machine_mode mode = GET_MODE (XEXP (cond, 0));
2786 const_rtx cst = XEXP (cond, 1);
2787
2788 /* We can't perform this optimization if either operand might be or might
2789 contain a signed zero. */
2790 if (HONOR_SIGNED_ZEROS (mode))
2791 {
2792 /* It is sufficient to check if CST is or contains a zero. We must
2793 handle float, complex, and vector. If any subpart is a zero, then
2794 the optimization can't be performed. */
2795 /* ??? The complex and vector checks are not implemented yet. We just
2796 always return zero for them. */
2797 if (GET_CODE (cst) == CONST_DOUBLE)
2798 {
2799 REAL_VALUE_TYPE d;
2800 REAL_VALUE_FROM_CONST_DOUBLE (d, cst);
2801 if (REAL_VALUES_EQUAL (d, dconst0))
2802 return 0;
2803 }
2804 else
2805 return 0;
2806 }
2807
2808 return gcse_constant_p (cst);
2809 }
2810
2811 /* Find the implicit sets of a function. An "implicit set" is a constraint
2812 on the value of a variable, implied by a conditional jump. For example,
2813 following "if (x == 2)", the then branch may be optimized as though the
2814 conditional performed an "explicit set", in this example, "x = 2". This
2815 function records the set patterns that are implicit at the start of each
2816 basic block.
2817
2818 FIXME: This would be more effective if critical edges are pre-split. As
2819 it is now, we can't record implicit sets for blocks that have
2820 critical successor edges. This results in missed optimizations
2821 and in more (unnecessary) work in cfgcleanup.c:thread_jump(). */
2822
2823 static void
2824 find_implicit_sets (void)
2825 {
2826 basic_block bb, dest;
2827 unsigned int count;
2828 rtx cond, new_rtx;
2829
2830 count = 0;
2831 FOR_EACH_BB (bb)
2832 /* Check for more than one successor. */
2833 if (EDGE_COUNT (bb->succs) > 1)
2834 {
2835 cond = fis_get_condition (BB_END (bb));
2836
2837 if (cond
2838 && (GET_CODE (cond) == EQ || GET_CODE (cond) == NE)
2839 && REG_P (XEXP (cond, 0))
2840 && REGNO (XEXP (cond, 0)) >= FIRST_PSEUDO_REGISTER
2841 && implicit_set_cond_p (cond))
2842 {
2843 dest = GET_CODE (cond) == EQ ? BRANCH_EDGE (bb)->dest
2844 : FALLTHRU_EDGE (bb)->dest;
2845
2846 if (dest
2847 /* Record nothing for a critical edge. */
2848 && single_pred_p (dest)
2849 && dest != EXIT_BLOCK_PTR)
2850 {
2851 new_rtx = gen_rtx_SET (VOIDmode, XEXP (cond, 0),
2852 XEXP (cond, 1));
2853 implicit_sets[dest->index] = new_rtx;
2854 if (dump_file)
2855 {
2856 fprintf(dump_file, "Implicit set of reg %d in ",
2857 REGNO (XEXP (cond, 0)));
2858 fprintf(dump_file, "basic block %d\n", dest->index);
2859 }
2860 count++;
2861 }
2862 }
2863 }
2864
2865 if (dump_file)
2866 fprintf (dump_file, "Found %d implicit sets\n", count);
2867 }
2868
2869 /* Bypass conditional jumps. */
2870
2871 /* The value of last_basic_block at the beginning of the jump_bypass
2872 pass. The use of redirect_edge_and_branch_force may introduce new
2873 basic blocks, but the data flow analysis is only valid for basic
2874 block indices less than bypass_last_basic_block. */
2875
2876 static int bypass_last_basic_block;
2877
2878 /* Find a set of REGNO to a constant that is available at the end of basic
2879 block BB. Returns NULL if no such set is found. Based heavily upon
2880 find_avail_set. */
2881
2882 static struct expr *
2883 find_bypass_set (int regno, int bb)
2884 {
2885 struct expr *result = 0;
2886
2887 for (;;)
2888 {
2889 rtx src;
2890 struct expr *set = lookup_set (regno, &set_hash_table);
2891
2892 while (set)
2893 {
2894 if (TEST_BIT (cprop_avout[bb], set->bitmap_index))
2895 break;
2896 set = next_set (regno, set);
2897 }
2898
2899 if (set == 0)
2900 break;
2901
2902 gcc_assert (GET_CODE (set->expr) == SET);
2903
2904 src = SET_SRC (set->expr);
2905 if (gcse_constant_p (src))
2906 result = set;
2907
2908 if (! REG_P (src))
2909 break;
2910
2911 regno = REGNO (src);
2912 }
2913 return result;
2914 }
2915
2916
2917 /* Subroutine of bypass_block that checks whether a pseudo is killed by
2918 any of the instructions inserted on an edge. Jump bypassing places
2919 condition code setters on CFG edges using insert_insn_on_edge. This
2920 function is required to check that our data flow analysis is still
2921 valid prior to commit_edge_insertions. */
2922
2923 static bool
2924 reg_killed_on_edge (const_rtx reg, const_edge e)
2925 {
2926 rtx insn;
2927
2928 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
2929 if (INSN_P (insn) && reg_set_p (reg, insn))
2930 return true;
2931
2932 return false;
2933 }
2934
2935 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
2936 basic block BB which has more than one predecessor. If not NULL, SETCC
2937 is the first instruction of BB, which is immediately followed by JUMP_INSN
2938 JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
2939 Returns nonzero if a change was made.
2940
2941 During the jump bypassing pass, we may place copies of SETCC instructions
2942 on CFG edges. The following routine must be careful to pay attention to
2943 these inserted insns when performing its transformations. */
2944
2945 static int
2946 bypass_block (basic_block bb, rtx setcc, rtx jump)
2947 {
2948 rtx insn, note;
2949 edge e, edest;
2950 int i, change;
2951 int may_be_loop_header;
2952 unsigned removed_p;
2953 edge_iterator ei;
2954
2955 insn = (setcc != NULL) ? setcc : jump;
2956
2957 /* Determine set of register uses in INSN. */
2958 reg_use_count = 0;
2959 note_uses (&PATTERN (insn), find_used_regs, NULL);
2960 note = find_reg_equal_equiv_note (insn);
2961 if (note)
2962 find_used_regs (&XEXP (note, 0), NULL);
2963
2964 may_be_loop_header = false;
2965 FOR_EACH_EDGE (e, ei, bb->preds)
2966 if (e->flags & EDGE_DFS_BACK)
2967 {
2968 may_be_loop_header = true;
2969 break;
2970 }
2971
2972 change = 0;
2973 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
2974 {
2975 removed_p = 0;
2976
2977 if (e->flags & EDGE_COMPLEX)
2978 {
2979 ei_next (&ei);
2980 continue;
2981 }
2982
2983 /* We can't redirect edges from new basic blocks. */
2984 if (e->src->index >= bypass_last_basic_block)
2985 {
2986 ei_next (&ei);
2987 continue;
2988 }
2989
2990 /* The irreducible loops created by redirecting of edges entering the
2991 loop from outside would decrease effectiveness of some of the following
2992 optimizations, so prevent this. */
2993 if (may_be_loop_header
2994 && !(e->flags & EDGE_DFS_BACK))
2995 {
2996 ei_next (&ei);
2997 continue;
2998 }
2999
3000 for (i = 0; i < reg_use_count; i++)
3001 {
3002 struct reg_use *reg_used = &reg_use_table[i];
3003 unsigned int regno = REGNO (reg_used->reg_rtx);
3004 basic_block dest, old_dest;
3005 struct expr *set;
3006 rtx src, new_rtx;
3007
3008 set = find_bypass_set (regno, e->src->index);
3009
3010 if (! set)
3011 continue;
3012
3013 /* Check the data flow is valid after edge insertions. */
3014 if (e->insns.r && reg_killed_on_edge (reg_used->reg_rtx, e))
3015 continue;
3016
3017 src = SET_SRC (pc_set (jump));
3018
3019 if (setcc != NULL)
3020 src = simplify_replace_rtx (src,
3021 SET_DEST (PATTERN (setcc)),
3022 SET_SRC (PATTERN (setcc)));
3023
3024 new_rtx = simplify_replace_rtx (src, reg_used->reg_rtx,
3025 SET_SRC (set->expr));
3026
3027 /* Jump bypassing may have already placed instructions on
3028 edges of the CFG. We can't bypass an outgoing edge that
3029 has instructions associated with it, as these insns won't
3030 get executed if the incoming edge is redirected. */
3031
3032 if (new_rtx == pc_rtx)
3033 {
3034 edest = FALLTHRU_EDGE (bb);
3035 dest = edest->insns.r ? NULL : edest->dest;
3036 }
3037 else if (GET_CODE (new_rtx) == LABEL_REF)
3038 {
3039 dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
3040 /* Don't bypass edges containing instructions. */
3041 edest = find_edge (bb, dest);
3042 if (edest && edest->insns.r)
3043 dest = NULL;
3044 }
3045 else
3046 dest = NULL;
3047
3048 /* Avoid unification of the edge with other edges from original
3049 branch. We would end up emitting the instruction on "both"
3050 edges. */
3051
3052 if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
3053 && find_edge (e->src, dest))
3054 dest = NULL;
3055
3056 old_dest = e->dest;
3057 if (dest != NULL
3058 && dest != old_dest
3059 && dest != EXIT_BLOCK_PTR)
3060 {
3061 redirect_edge_and_branch_force (e, dest);
3062
3063 /* Copy the register setter to the redirected edge.
3064 Don't copy CC0 setters, as CC0 is dead after jump. */
3065 if (setcc)
3066 {
3067 rtx pat = PATTERN (setcc);
3068 if (!CC0_P (SET_DEST (pat)))
3069 insert_insn_on_edge (copy_insn (pat), e);
3070 }
3071
3072 if (dump_file != NULL)
3073 {
3074 fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
3075 "in jump_insn %d equals constant ",
3076 regno, INSN_UID (jump));
3077 print_rtl (dump_file, SET_SRC (set->expr));
3078 fprintf (dump_file, "\nBypass edge from %d->%d to %d\n",
3079 e->src->index, old_dest->index, dest->index);
3080 }
3081 change = 1;
3082 removed_p = 1;
3083 break;
3084 }
3085 }
3086 if (!removed_p)
3087 ei_next (&ei);
3088 }
3089 return change;
3090 }
3091
3092 /* Find basic blocks with more than one predecessor that only contain a
3093 single conditional jump. If the result of the comparison is known at
3094 compile-time from any incoming edge, redirect that edge to the
3095 appropriate target. Returns nonzero if a change was made.
3096
3097 This function is now mis-named, because we also handle indirect jumps. */
3098
3099 static int
3100 bypass_conditional_jumps (void)
3101 {
3102 basic_block bb;
3103 int changed;
3104 rtx setcc;
3105 rtx insn;
3106 rtx dest;
3107
3108 /* Note we start at block 1. */
3109 if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
3110 return 0;
3111
3112 bypass_last_basic_block = last_basic_block;
3113 mark_dfs_back_edges ();
3114
3115 changed = 0;
3116 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb,
3117 EXIT_BLOCK_PTR, next_bb)
3118 {
3119 /* Check for more than one predecessor. */
3120 if (!single_pred_p (bb))
3121 {
3122 setcc = NULL_RTX;
3123 FOR_BB_INSNS (bb, insn)
3124 if (NONJUMP_INSN_P (insn))
3125 {
3126 if (setcc)
3127 break;
3128 if (GET_CODE (PATTERN (insn)) != SET)
3129 break;
3130
3131 dest = SET_DEST (PATTERN (insn));
3132 if (REG_P (dest) || CC0_P (dest))
3133 setcc = insn;
3134 else
3135 break;
3136 }
3137 else if (JUMP_P (insn))
3138 {
3139 if ((any_condjump_p (insn) || computed_jump_p (insn))
3140 && onlyjump_p (insn))
3141 changed |= bypass_block (bb, setcc, insn);
3142 break;
3143 }
3144 else if (INSN_P (insn))
3145 break;
3146 }
3147 }
3148
3149 /* If we bypassed any register setting insns, we inserted a
3150 copy on the redirected edge. These need to be committed. */
3151 if (changed)
3152 commit_edge_insertions ();
3153
3154 return changed;
3155 }
3156 \f
3157 /* Compute PRE+LCM working variables. */
3158
3159 /* Local properties of expressions. */
3160 /* Nonzero for expressions that are transparent in the block. */
3161 static sbitmap *transp;
3162
3163 /* Nonzero for expressions that are transparent at the end of the block.
3164 This is only zero for expressions killed by abnormal critical edge
3165 created by a calls. */
3166 static sbitmap *transpout;
3167
3168 /* Nonzero for expressions that are computed (available) in the block. */
3169 static sbitmap *comp;
3170
3171 /* Nonzero for expressions that are locally anticipatable in the block. */
3172 static sbitmap *antloc;
3173
3174 /* Nonzero for expressions where this block is an optimal computation
3175 point. */
3176 static sbitmap *pre_optimal;
3177
3178 /* Nonzero for expressions which are redundant in a particular block. */
3179 static sbitmap *pre_redundant;
3180
3181 /* Nonzero for expressions which should be inserted on a specific edge. */
3182 static sbitmap *pre_insert_map;
3183
3184 /* Nonzero for expressions which should be deleted in a specific block. */
3185 static sbitmap *pre_delete_map;
3186
3187 /* Contains the edge_list returned by pre_edge_lcm. */
3188 static struct edge_list *edge_list;
3189
3190 /* Allocate vars used for PRE analysis. */
3191
3192 static void
3193 alloc_pre_mem (int n_blocks, int n_exprs)
3194 {
3195 transp = sbitmap_vector_alloc (n_blocks, n_exprs);
3196 comp = sbitmap_vector_alloc (n_blocks, n_exprs);
3197 antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
3198
3199 pre_optimal = NULL;
3200 pre_redundant = NULL;
3201 pre_insert_map = NULL;
3202 pre_delete_map = NULL;
3203 ae_kill = sbitmap_vector_alloc (n_blocks, n_exprs);
3204
3205 /* pre_insert and pre_delete are allocated later. */
3206 }
3207
3208 /* Free vars used for PRE analysis. */
3209
3210 static void
3211 free_pre_mem (void)
3212 {
3213 sbitmap_vector_free (transp);
3214 sbitmap_vector_free (comp);
3215
3216 /* ANTLOC and AE_KILL are freed just after pre_lcm finishes. */
3217
3218 if (pre_optimal)
3219 sbitmap_vector_free (pre_optimal);
3220 if (pre_redundant)
3221 sbitmap_vector_free (pre_redundant);
3222 if (pre_insert_map)
3223 sbitmap_vector_free (pre_insert_map);
3224 if (pre_delete_map)
3225 sbitmap_vector_free (pre_delete_map);
3226
3227 transp = comp = NULL;
3228 pre_optimal = pre_redundant = pre_insert_map = pre_delete_map = NULL;
3229 }
3230
3231 /* Top level routine to do the dataflow analysis needed by PRE. */
3232
3233 static void
3234 compute_pre_data (void)
3235 {
3236 sbitmap trapping_expr;
3237 basic_block bb;
3238 unsigned int ui;
3239
3240 compute_local_properties (transp, comp, antloc, &expr_hash_table);
3241 sbitmap_vector_zero (ae_kill, last_basic_block);
3242
3243 /* Collect expressions which might trap. */
3244 trapping_expr = sbitmap_alloc (expr_hash_table.n_elems);
3245 sbitmap_zero (trapping_expr);
3246 for (ui = 0; ui < expr_hash_table.size; ui++)
3247 {
3248 struct expr *e;
3249 for (e = expr_hash_table.table[ui]; e != NULL; e = e->next_same_hash)
3250 if (may_trap_p (e->expr))
3251 SET_BIT (trapping_expr, e->bitmap_index);
3252 }
3253
3254 /* Compute ae_kill for each basic block using:
3255
3256 ~(TRANSP | COMP)
3257 */
3258
3259 FOR_EACH_BB (bb)
3260 {
3261 edge e;
3262 edge_iterator ei;
3263
3264 /* If the current block is the destination of an abnormal edge, we
3265 kill all trapping expressions because we won't be able to properly
3266 place the instruction on the edge. So make them neither
3267 anticipatable nor transparent. This is fairly conservative. */
3268 FOR_EACH_EDGE (e, ei, bb->preds)
3269 if (e->flags & EDGE_ABNORMAL)
3270 {
3271 sbitmap_difference (antloc[bb->index], antloc[bb->index], trapping_expr);
3272 sbitmap_difference (transp[bb->index], transp[bb->index], trapping_expr);
3273 break;
3274 }
3275
3276 sbitmap_a_or_b (ae_kill[bb->index], transp[bb->index], comp[bb->index]);
3277 sbitmap_not (ae_kill[bb->index], ae_kill[bb->index]);
3278 }
3279
3280 edge_list = pre_edge_lcm (expr_hash_table.n_elems, transp, comp, antloc,
3281 ae_kill, &pre_insert_map, &pre_delete_map);
3282 sbitmap_vector_free (antloc);
3283 antloc = NULL;
3284 sbitmap_vector_free (ae_kill);
3285 ae_kill = NULL;
3286 sbitmap_free (trapping_expr);
3287 }
3288 \f
3289 /* PRE utilities */
3290
3291 /* Return nonzero if an occurrence of expression EXPR in OCCR_BB would reach
3292 block BB.
3293
3294 VISITED is a pointer to a working buffer for tracking which BB's have
3295 been visited. It is NULL for the top-level call.
3296
3297 We treat reaching expressions that go through blocks containing the same
3298 reaching expression as "not reaching". E.g. if EXPR is generated in blocks
3299 2 and 3, INSN is in block 4, and 2->3->4, we treat the expression in block
3300 2 as not reaching. The intent is to improve the probability of finding
3301 only one reaching expression and to reduce register lifetimes by picking
3302 the closest such expression. */
3303
3304 static int
3305 pre_expr_reaches_here_p_work (basic_block occr_bb, struct expr *expr, basic_block bb, char *visited)
3306 {
3307 edge pred;
3308 edge_iterator ei;
3309
3310 FOR_EACH_EDGE (pred, ei, bb->preds)
3311 {
3312 basic_block pred_bb = pred->src;
3313
3314 if (pred->src == ENTRY_BLOCK_PTR
3315 /* Has predecessor has already been visited? */
3316 || visited[pred_bb->index])
3317 ;/* Nothing to do. */
3318
3319 /* Does this predecessor generate this expression? */
3320 else if (TEST_BIT (comp[pred_bb->index], expr->bitmap_index))
3321 {
3322 /* Is this the occurrence we're looking for?
3323 Note that there's only one generating occurrence per block
3324 so we just need to check the block number. */
3325 if (occr_bb == pred_bb)
3326 return 1;
3327
3328 visited[pred_bb->index] = 1;
3329 }
3330 /* Ignore this predecessor if it kills the expression. */
3331 else if (! TEST_BIT (transp[pred_bb->index], expr->bitmap_index))
3332 visited[pred_bb->index] = 1;
3333
3334 /* Neither gen nor kill. */
3335 else
3336 {
3337 visited[pred_bb->index] = 1;
3338 if (pre_expr_reaches_here_p_work (occr_bb, expr, pred_bb, visited))
3339 return 1;
3340 }
3341 }
3342
3343 /* All paths have been checked. */
3344 return 0;
3345 }
3346
3347 /* The wrapper for pre_expr_reaches_here_work that ensures that any
3348 memory allocated for that function is returned. */
3349
3350 static int
3351 pre_expr_reaches_here_p (basic_block occr_bb, struct expr *expr, basic_block bb)
3352 {
3353 int rval;
3354 char *visited = XCNEWVEC (char, last_basic_block);
3355
3356 rval = pre_expr_reaches_here_p_work (occr_bb, expr, bb, visited);
3357
3358 free (visited);
3359 return rval;
3360 }
3361 \f
3362
3363 /* Given an expr, generate RTL which we can insert at the end of a BB,
3364 or on an edge. Set the block number of any insns generated to
3365 the value of BB. */
3366
3367 static rtx
3368 process_insert_insn (struct expr *expr)
3369 {
3370 rtx reg = expr->reaching_reg;
3371 rtx exp = copy_rtx (expr->expr);
3372 rtx pat;
3373
3374 start_sequence ();
3375
3376 /* If the expression is something that's an operand, like a constant,
3377 just copy it to a register. */
3378 if (general_operand (exp, GET_MODE (reg)))
3379 emit_move_insn (reg, exp);
3380
3381 /* Otherwise, make a new insn to compute this expression and make sure the
3382 insn will be recognized (this also adds any needed CLOBBERs). Copy the
3383 expression to make sure we don't have any sharing issues. */
3384 else
3385 {
3386 rtx insn = emit_insn (gen_rtx_SET (VOIDmode, reg, exp));
3387
3388 if (insn_invalid_p (insn))
3389 gcc_unreachable ();
3390 }
3391
3392
3393 pat = get_insns ();
3394 end_sequence ();
3395
3396 return pat;
3397 }
3398
3399 /* Add EXPR to the end of basic block BB.
3400
3401 This is used by both the PRE and code hoisting.
3402
3403 For PRE, we want to verify that the expr is either transparent
3404 or locally anticipatable in the target block. This check makes
3405 no sense for code hoisting. */
3406
3407 static void
3408 insert_insn_end_basic_block (struct expr *expr, basic_block bb, int pre)
3409 {
3410 rtx insn = BB_END (bb);
3411 rtx new_insn;
3412 rtx reg = expr->reaching_reg;
3413 int regno = REGNO (reg);
3414 rtx pat, pat_end;
3415
3416 pat = process_insert_insn (expr);
3417 gcc_assert (pat && INSN_P (pat));
3418
3419 pat_end = pat;
3420 while (NEXT_INSN (pat_end) != NULL_RTX)
3421 pat_end = NEXT_INSN (pat_end);
3422
3423 /* If the last insn is a jump, insert EXPR in front [taking care to
3424 handle cc0, etc. properly]. Similarly we need to care trapping
3425 instructions in presence of non-call exceptions. */
3426
3427 if (JUMP_P (insn)
3428 || (NONJUMP_INSN_P (insn)
3429 && (!single_succ_p (bb)
3430 || single_succ_edge (bb)->flags & EDGE_ABNORMAL)))
3431 {
3432 #ifdef HAVE_cc0
3433 rtx note;
3434 #endif
3435 /* It should always be the case that we can put these instructions
3436 anywhere in the basic block with performing PRE optimizations.
3437 Check this. */
3438 gcc_assert (!NONJUMP_INSN_P (insn) || !pre
3439 || TEST_BIT (antloc[bb->index], expr->bitmap_index)
3440 || TEST_BIT (transp[bb->index], expr->bitmap_index));
3441
3442 /* If this is a jump table, then we can't insert stuff here. Since
3443 we know the previous real insn must be the tablejump, we insert
3444 the new instruction just before the tablejump. */
3445 if (GET_CODE (PATTERN (insn)) == ADDR_VEC
3446 || GET_CODE (PATTERN (insn)) == ADDR_DIFF_VEC)
3447 insn = prev_real_insn (insn);
3448
3449 #ifdef HAVE_cc0
3450 /* FIXME: 'twould be nice to call prev_cc0_setter here but it aborts
3451 if cc0 isn't set. */
3452 note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
3453 if (note)
3454 insn = XEXP (note, 0);
3455 else
3456 {
3457 rtx maybe_cc0_setter = prev_nonnote_insn (insn);
3458 if (maybe_cc0_setter
3459 && INSN_P (maybe_cc0_setter)
3460 && sets_cc0_p (PATTERN (maybe_cc0_setter)))
3461 insn = maybe_cc0_setter;
3462 }
3463 #endif
3464 /* FIXME: What if something in cc0/jump uses value set in new insn? */
3465 new_insn = emit_insn_before_noloc (pat, insn, bb);
3466 }
3467
3468 /* Likewise if the last insn is a call, as will happen in the presence
3469 of exception handling. */
3470 else if (CALL_P (insn)
3471 && (!single_succ_p (bb)
3472 || single_succ_edge (bb)->flags & EDGE_ABNORMAL))
3473 {
3474 /* Keeping in mind SMALL_REGISTER_CLASSES and parameters in registers,
3475 we search backward and place the instructions before the first
3476 parameter is loaded. Do this for everyone for consistency and a
3477 presumption that we'll get better code elsewhere as well.
3478
3479 It should always be the case that we can put these instructions
3480 anywhere in the basic block with performing PRE optimizations.
3481 Check this. */
3482
3483 gcc_assert (!pre
3484 || TEST_BIT (antloc[bb->index], expr->bitmap_index)
3485 || TEST_BIT (transp[bb->index], expr->bitmap_index));
3486
3487 /* Since different machines initialize their parameter registers
3488 in different orders, assume nothing. Collect the set of all
3489 parameter registers. */
3490 insn = find_first_parameter_load (insn, BB_HEAD (bb));
3491
3492 /* If we found all the parameter loads, then we want to insert
3493 before the first parameter load.
3494
3495 If we did not find all the parameter loads, then we might have
3496 stopped on the head of the block, which could be a CODE_LABEL.
3497 If we inserted before the CODE_LABEL, then we would be putting
3498 the insn in the wrong basic block. In that case, put the insn
3499 after the CODE_LABEL. Also, respect NOTE_INSN_BASIC_BLOCK. */
3500 while (LABEL_P (insn)
3501 || NOTE_INSN_BASIC_BLOCK_P (insn))
3502 insn = NEXT_INSN (insn);
3503
3504 new_insn = emit_insn_before_noloc (pat, insn, bb);
3505 }
3506 else
3507 new_insn = emit_insn_after_noloc (pat, insn, bb);
3508
3509 while (1)
3510 {
3511 if (INSN_P (pat))
3512 add_label_notes (PATTERN (pat), new_insn);
3513 if (pat == pat_end)
3514 break;
3515 pat = NEXT_INSN (pat);
3516 }
3517
3518 gcse_create_count++;
3519
3520 if (dump_file)
3521 {
3522 fprintf (dump_file, "PRE/HOIST: end of bb %d, insn %d, ",
3523 bb->index, INSN_UID (new_insn));
3524 fprintf (dump_file, "copying expression %d to reg %d\n",
3525 expr->bitmap_index, regno);
3526 }
3527 }
3528
3529 /* Insert partially redundant expressions on edges in the CFG to make
3530 the expressions fully redundant. */
3531
3532 static int
3533 pre_edge_insert (struct edge_list *edge_list, struct expr **index_map)
3534 {
3535 int e, i, j, num_edges, set_size, did_insert = 0;
3536 sbitmap *inserted;
3537
3538 /* Where PRE_INSERT_MAP is nonzero, we add the expression on that edge
3539 if it reaches any of the deleted expressions. */
3540
3541 set_size = pre_insert_map[0]->size;
3542 num_edges = NUM_EDGES (edge_list);
3543 inserted = sbitmap_vector_alloc (num_edges, expr_hash_table.n_elems);
3544 sbitmap_vector_zero (inserted, num_edges);
3545
3546 for (e = 0; e < num_edges; e++)
3547 {
3548 int indx;
3549 basic_block bb = INDEX_EDGE_PRED_BB (edge_list, e);
3550
3551 for (i = indx = 0; i < set_size; i++, indx += SBITMAP_ELT_BITS)
3552 {
3553 SBITMAP_ELT_TYPE insert = pre_insert_map[e]->elms[i];
3554
3555 for (j = indx; insert && j < (int) expr_hash_table.n_elems; j++, insert >>= 1)
3556 if ((insert & 1) != 0 && index_map[j]->reaching_reg != NULL_RTX)
3557 {
3558 struct expr *expr = index_map[j];
3559 struct occr *occr;
3560
3561 /* Now look at each deleted occurrence of this expression. */
3562 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
3563 {
3564 if (! occr->deleted_p)
3565 continue;
3566
3567 /* Insert this expression on this edge if it would
3568 reach the deleted occurrence in BB. */
3569 if (!TEST_BIT (inserted[e], j))
3570 {
3571 rtx insn;
3572 edge eg = INDEX_EDGE (edge_list, e);
3573
3574 /* We can't insert anything on an abnormal and
3575 critical edge, so we insert the insn at the end of
3576 the previous block. There are several alternatives
3577 detailed in Morgans book P277 (sec 10.5) for
3578 handling this situation. This one is easiest for
3579 now. */
3580
3581 if (eg->flags & EDGE_ABNORMAL)
3582 insert_insn_end_basic_block (index_map[j], bb, 0);
3583 else
3584 {
3585 insn = process_insert_insn (index_map[j]);
3586 insert_insn_on_edge (insn, eg);
3587 }
3588
3589 if (dump_file)
3590 {
3591 fprintf (dump_file, "PRE: edge (%d,%d), ",
3592 bb->index,
3593 INDEX_EDGE_SUCC_BB (edge_list, e)->index);
3594 fprintf (dump_file, "copy expression %d\n",
3595 expr->bitmap_index);
3596 }
3597
3598 update_ld_motion_stores (expr);
3599 SET_BIT (inserted[e], j);
3600 did_insert = 1;
3601 gcse_create_count++;
3602 }
3603 }
3604 }
3605 }
3606 }
3607
3608 sbitmap_vector_free (inserted);
3609 return did_insert;
3610 }
3611
3612 /* Copy the result of EXPR->EXPR generated by INSN to EXPR->REACHING_REG.
3613 Given "old_reg <- expr" (INSN), instead of adding after it
3614 reaching_reg <- old_reg
3615 it's better to do the following:
3616 reaching_reg <- expr
3617 old_reg <- reaching_reg
3618 because this way copy propagation can discover additional PRE
3619 opportunities. But if this fails, we try the old way.
3620 When "expr" is a store, i.e.
3621 given "MEM <- old_reg", instead of adding after it
3622 reaching_reg <- old_reg
3623 it's better to add it before as follows:
3624 reaching_reg <- old_reg
3625 MEM <- reaching_reg. */
3626
3627 static void
3628 pre_insert_copy_insn (struct expr *expr, rtx insn)
3629 {
3630 rtx reg = expr->reaching_reg;
3631 int regno = REGNO (reg);
3632 int indx = expr->bitmap_index;
3633 rtx pat = PATTERN (insn);
3634 rtx set, first_set, new_insn;
3635 rtx old_reg;
3636 int i;
3637
3638 /* This block matches the logic in hash_scan_insn. */
3639 switch (GET_CODE (pat))
3640 {
3641 case SET:
3642 set = pat;
3643 break;
3644
3645 case PARALLEL:
3646 /* Search through the parallel looking for the set whose
3647 source was the expression that we're interested in. */
3648 first_set = NULL_RTX;
3649 set = NULL_RTX;
3650 for (i = 0; i < XVECLEN (pat, 0); i++)
3651 {
3652 rtx x = XVECEXP (pat, 0, i);
3653 if (GET_CODE (x) == SET)
3654 {
3655 /* If the source was a REG_EQUAL or REG_EQUIV note, we
3656 may not find an equivalent expression, but in this
3657 case the PARALLEL will have a single set. */
3658 if (first_set == NULL_RTX)
3659 first_set = x;
3660 if (expr_equiv_p (SET_SRC (x), expr->expr))
3661 {
3662 set = x;
3663 break;
3664 }
3665 }
3666 }
3667
3668 gcc_assert (first_set);
3669 if (set == NULL_RTX)
3670 set = first_set;
3671 break;
3672
3673 default:
3674 gcc_unreachable ();
3675 }
3676
3677 if (REG_P (SET_DEST (set)))
3678 {
3679 old_reg = SET_DEST (set);
3680 /* Check if we can modify the set destination in the original insn. */
3681 if (validate_change (insn, &SET_DEST (set), reg, 0))
3682 {
3683 new_insn = gen_move_insn (old_reg, reg);
3684 new_insn = emit_insn_after (new_insn, insn);
3685 }
3686 else
3687 {
3688 new_insn = gen_move_insn (reg, old_reg);
3689 new_insn = emit_insn_after (new_insn, insn);
3690 }
3691 }
3692 else /* This is possible only in case of a store to memory. */
3693 {
3694 old_reg = SET_SRC (set);
3695 new_insn = gen_move_insn (reg, old_reg);
3696
3697 /* Check if we can modify the set source in the original insn. */
3698 if (validate_change (insn, &SET_SRC (set), reg, 0))
3699 new_insn = emit_insn_before (new_insn, insn);
3700 else
3701 new_insn = emit_insn_after (new_insn, insn);
3702 }
3703
3704 gcse_create_count++;
3705
3706 if (dump_file)
3707 fprintf (dump_file,
3708 "PRE: bb %d, insn %d, copy expression %d in insn %d to reg %d\n",
3709 BLOCK_NUM (insn), INSN_UID (new_insn), indx,
3710 INSN_UID (insn), regno);
3711 }
3712
3713 /* Copy available expressions that reach the redundant expression
3714 to `reaching_reg'. */
3715
3716 static void
3717 pre_insert_copies (void)
3718 {
3719 unsigned int i, added_copy;
3720 struct expr *expr;
3721 struct occr *occr;
3722 struct occr *avail;
3723
3724 /* For each available expression in the table, copy the result to
3725 `reaching_reg' if the expression reaches a deleted one.
3726
3727 ??? The current algorithm is rather brute force.
3728 Need to do some profiling. */
3729
3730 for (i = 0; i < expr_hash_table.size; i++)
3731 for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
3732 {
3733 /* If the basic block isn't reachable, PPOUT will be TRUE. However,
3734 we don't want to insert a copy here because the expression may not
3735 really be redundant. So only insert an insn if the expression was
3736 deleted. This test also avoids further processing if the
3737 expression wasn't deleted anywhere. */
3738 if (expr->reaching_reg == NULL)
3739 continue;
3740
3741 /* Set when we add a copy for that expression. */
3742 added_copy = 0;
3743
3744 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
3745 {
3746 if (! occr->deleted_p)
3747 continue;
3748
3749 for (avail = expr->avail_occr; avail != NULL; avail = avail->next)
3750 {
3751 rtx insn = avail->insn;
3752
3753 /* No need to handle this one if handled already. */
3754 if (avail->copied_p)
3755 continue;
3756
3757 /* Don't handle this one if it's a redundant one. */
3758 if (INSN_DELETED_P (insn))
3759 continue;
3760
3761 /* Or if the expression doesn't reach the deleted one. */
3762 if (! pre_expr_reaches_here_p (BLOCK_FOR_INSN (avail->insn),
3763 expr,
3764 BLOCK_FOR_INSN (occr->insn)))
3765 continue;
3766
3767 added_copy = 1;
3768
3769 /* Copy the result of avail to reaching_reg. */
3770 pre_insert_copy_insn (expr, insn);
3771 avail->copied_p = 1;
3772 }
3773 }
3774
3775 if (added_copy)
3776 update_ld_motion_stores (expr);
3777 }
3778 }
3779
3780 /* Emit move from SRC to DEST noting the equivalence with expression computed
3781 in INSN. */
3782 static rtx
3783 gcse_emit_move_after (rtx src, rtx dest, rtx insn)
3784 {
3785 rtx new_rtx;
3786 rtx set = single_set (insn), set2;
3787 rtx note;
3788 rtx eqv;
3789
3790 /* This should never fail since we're creating a reg->reg copy
3791 we've verified to be valid. */
3792
3793 new_rtx = emit_insn_after (gen_move_insn (dest, src), insn);
3794
3795 /* Note the equivalence for local CSE pass. */
3796 set2 = single_set (new_rtx);
3797 if (!set2 || !rtx_equal_p (SET_DEST (set2), dest))
3798 return new_rtx;
3799 if ((note = find_reg_equal_equiv_note (insn)))
3800 eqv = XEXP (note, 0);
3801 else
3802 eqv = SET_SRC (set);
3803
3804 set_unique_reg_note (new_rtx, REG_EQUAL, copy_insn_1 (eqv));
3805
3806 return new_rtx;
3807 }
3808
3809 /* Delete redundant computations.
3810 Deletion is done by changing the insn to copy the `reaching_reg' of
3811 the expression into the result of the SET. It is left to later passes
3812 (cprop, cse2, flow, combine, regmove) to propagate the copy or eliminate it.
3813
3814 Returns nonzero if a change is made. */
3815
3816 static int
3817 pre_delete (void)
3818 {
3819 unsigned int i;
3820 int changed;
3821 struct expr *expr;
3822 struct occr *occr;
3823
3824 changed = 0;
3825 for (i = 0; i < expr_hash_table.size; i++)
3826 for (expr = expr_hash_table.table[i];
3827 expr != NULL;
3828 expr = expr->next_same_hash)
3829 {
3830 int indx = expr->bitmap_index;
3831
3832 /* We only need to search antic_occr since we require
3833 ANTLOC != 0. */
3834
3835 for (occr = expr->antic_occr; occr != NULL; occr = occr->next)
3836 {
3837 rtx insn = occr->insn;
3838 rtx set;
3839 basic_block bb = BLOCK_FOR_INSN (insn);
3840
3841 /* We only delete insns that have a single_set. */
3842 if (TEST_BIT (pre_delete_map[bb->index], indx)
3843 && (set = single_set (insn)) != 0
3844 && dbg_cnt (pre_insn))
3845 {
3846 /* Create a pseudo-reg to store the result of reaching
3847 expressions into. Get the mode for the new pseudo from
3848 the mode of the original destination pseudo. */
3849 if (expr->reaching_reg == NULL)
3850 expr->reaching_reg = gen_reg_rtx_and_attrs (SET_DEST (set));
3851
3852 gcse_emit_move_after (expr->reaching_reg, SET_DEST (set), insn);
3853 delete_insn (insn);
3854 occr->deleted_p = 1;
3855 changed = 1;
3856 gcse_subst_count++;
3857
3858 if (dump_file)
3859 {
3860 fprintf (dump_file,
3861 "PRE: redundant insn %d (expression %d) in ",
3862 INSN_UID (insn), indx);
3863 fprintf (dump_file, "bb %d, reaching reg is %d\n",
3864 bb->index, REGNO (expr->reaching_reg));
3865 }
3866 }
3867 }
3868 }
3869
3870 return changed;
3871 }
3872
3873 /* Perform GCSE optimizations using PRE.
3874 This is called by one_pre_gcse_pass after all the dataflow analysis
3875 has been done.
3876
3877 This is based on the original Morel-Renvoise paper Fred Chow's thesis, and
3878 lazy code motion from Knoop, Ruthing and Steffen as described in Advanced
3879 Compiler Design and Implementation.
3880
3881 ??? A new pseudo reg is created to hold the reaching expression. The nice
3882 thing about the classical approach is that it would try to use an existing
3883 reg. If the register can't be adequately optimized [i.e. we introduce
3884 reload problems], one could add a pass here to propagate the new register
3885 through the block.
3886
3887 ??? We don't handle single sets in PARALLELs because we're [currently] not
3888 able to copy the rest of the parallel when we insert copies to create full
3889 redundancies from partial redundancies. However, there's no reason why we
3890 can't handle PARALLELs in the cases where there are no partial
3891 redundancies. */
3892
3893 static int
3894 pre_gcse (void)
3895 {
3896 unsigned int i;
3897 int did_insert, changed;
3898 struct expr **index_map;
3899 struct expr *expr;
3900
3901 /* Compute a mapping from expression number (`bitmap_index') to
3902 hash table entry. */
3903
3904 index_map = XCNEWVEC (struct expr *, expr_hash_table.n_elems);
3905 for (i = 0; i < expr_hash_table.size; i++)
3906 for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
3907 index_map[expr->bitmap_index] = expr;
3908
3909 /* Delete the redundant insns first so that
3910 - we know what register to use for the new insns and for the other
3911 ones with reaching expressions
3912 - we know which insns are redundant when we go to create copies */
3913
3914 changed = pre_delete ();
3915 did_insert = pre_edge_insert (edge_list, index_map);
3916
3917 /* In other places with reaching expressions, copy the expression to the
3918 specially allocated pseudo-reg that reaches the redundant expr. */
3919 pre_insert_copies ();
3920 if (did_insert)
3921 {
3922 commit_edge_insertions ();
3923 changed = 1;
3924 }
3925
3926 free (index_map);
3927 return changed;
3928 }
3929
3930 /* Top level routine to perform one PRE GCSE pass.
3931
3932 Return nonzero if a change was made. */
3933
3934 static int
3935 one_pre_gcse_pass (void)
3936 {
3937 int changed = 0;
3938
3939 gcse_subst_count = 0;
3940 gcse_create_count = 0;
3941
3942 /* Return if there's nothing to do, or it is too expensive. */
3943 if (n_basic_blocks <= NUM_FIXED_BLOCKS + 1
3944 || is_too_expensive (_("PRE disabled")))
3945 return 0;
3946
3947 /* We need alias. */
3948 init_alias_analysis ();
3949
3950 bytes_used = 0;
3951 gcc_obstack_init (&gcse_obstack);
3952 alloc_gcse_mem ();
3953
3954 alloc_hash_table (get_max_uid (), &expr_hash_table, 0);
3955 add_noreturn_fake_exit_edges ();
3956 if (flag_gcse_lm)
3957 compute_ld_motion_mems ();
3958
3959 compute_hash_table (&expr_hash_table);
3960 trim_ld_motion_mems ();
3961 if (dump_file)
3962 dump_hash_table (dump_file, "Expression", &expr_hash_table);
3963
3964 if (expr_hash_table.n_elems > 0)
3965 {
3966 alloc_pre_mem (last_basic_block, expr_hash_table.n_elems);
3967 compute_pre_data ();
3968 changed |= pre_gcse ();
3969 free_edge_list (edge_list);
3970 free_pre_mem ();
3971 }
3972
3973 free_ldst_mems ();
3974 remove_fake_exit_edges ();
3975 free_hash_table (&expr_hash_table);
3976
3977 free_gcse_mem ();
3978 obstack_free (&gcse_obstack, NULL);
3979
3980 /* We are finished with alias. */
3981 end_alias_analysis ();
3982
3983 if (dump_file)
3984 {
3985 fprintf (dump_file, "PRE GCSE of %s, %d basic blocks, %d bytes needed, ",
3986 current_function_name (), n_basic_blocks, bytes_used);
3987 fprintf (dump_file, "%d substs, %d insns created\n",
3988 gcse_subst_count, gcse_create_count);
3989 }
3990
3991 return changed;
3992 }
3993 \f
3994 /* If X contains any LABEL_REF's, add REG_LABEL_OPERAND notes for them
3995 to INSN. If such notes are added to an insn which references a
3996 CODE_LABEL, the LABEL_NUSES count is incremented. We have to add
3997 that note, because the following loop optimization pass requires
3998 them. */
3999
4000 /* ??? If there was a jump optimization pass after gcse and before loop,
4001 then we would not need to do this here, because jump would add the
4002 necessary REG_LABEL_OPERAND and REG_LABEL_TARGET notes. */
4003
4004 static void
4005 add_label_notes (rtx x, rtx insn)
4006 {
4007 enum rtx_code code = GET_CODE (x);
4008 int i, j;
4009 const char *fmt;
4010
4011 if (code == LABEL_REF && !LABEL_REF_NONLOCAL_P (x))
4012 {
4013 /* This code used to ignore labels that referred to dispatch tables to
4014 avoid flow generating (slightly) worse code.
4015
4016 We no longer ignore such label references (see LABEL_REF handling in
4017 mark_jump_label for additional information). */
4018
4019 /* There's no reason for current users to emit jump-insns with
4020 such a LABEL_REF, so we don't have to handle REG_LABEL_TARGET
4021 notes. */
4022 gcc_assert (!JUMP_P (insn));
4023 add_reg_note (insn, REG_LABEL_OPERAND, XEXP (x, 0));
4024
4025 if (LABEL_P (XEXP (x, 0)))
4026 LABEL_NUSES (XEXP (x, 0))++;
4027
4028 return;
4029 }
4030
4031 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
4032 {
4033 if (fmt[i] == 'e')
4034 add_label_notes (XEXP (x, i), insn);
4035 else if (fmt[i] == 'E')
4036 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4037 add_label_notes (XVECEXP (x, i, j), insn);
4038 }
4039 }
4040
4041 /* Compute transparent outgoing information for each block.
4042
4043 An expression is transparent to an edge unless it is killed by
4044 the edge itself. This can only happen with abnormal control flow,
4045 when the edge is traversed through a call. This happens with
4046 non-local labels and exceptions.
4047
4048 This would not be necessary if we split the edge. While this is
4049 normally impossible for abnormal critical edges, with some effort
4050 it should be possible with exception handling, since we still have
4051 control over which handler should be invoked. But due to increased
4052 EH table sizes, this may not be worthwhile. */
4053
4054 static void
4055 compute_transpout (void)
4056 {
4057 basic_block bb;
4058 unsigned int i;
4059 struct expr *expr;
4060
4061 sbitmap_vector_ones (transpout, last_basic_block);
4062
4063 FOR_EACH_BB (bb)
4064 {
4065 /* Note that flow inserted a nop at the end of basic blocks that
4066 end in call instructions for reasons other than abnormal
4067 control flow. */
4068 if (! CALL_P (BB_END (bb)))
4069 continue;
4070
4071 for (i = 0; i < expr_hash_table.size; i++)
4072 for (expr = expr_hash_table.table[i]; expr ; expr = expr->next_same_hash)
4073 if (MEM_P (expr->expr))
4074 {
4075 if (GET_CODE (XEXP (expr->expr, 0)) == SYMBOL_REF
4076 && CONSTANT_POOL_ADDRESS_P (XEXP (expr->expr, 0)))
4077 continue;
4078
4079 /* ??? Optimally, we would use interprocedural alias
4080 analysis to determine if this mem is actually killed
4081 by this call. */
4082 RESET_BIT (transpout[bb->index], expr->bitmap_index);
4083 }
4084 }
4085 }
4086
4087 /* Code Hoisting variables and subroutines. */
4088
4089 /* Very busy expressions. */
4090 static sbitmap *hoist_vbein;
4091 static sbitmap *hoist_vbeout;
4092
4093 /* Hoistable expressions. */
4094 static sbitmap *hoist_exprs;
4095
4096 /* ??? We could compute post dominators and run this algorithm in
4097 reverse to perform tail merging, doing so would probably be
4098 more effective than the tail merging code in jump.c.
4099
4100 It's unclear if tail merging could be run in parallel with
4101 code hoisting. It would be nice. */
4102
4103 /* Allocate vars used for code hoisting analysis. */
4104
4105 static void
4106 alloc_code_hoist_mem (int n_blocks, int n_exprs)
4107 {
4108 antloc = sbitmap_vector_alloc (n_blocks, n_exprs);
4109 transp = sbitmap_vector_alloc (n_blocks, n_exprs);
4110 comp = sbitmap_vector_alloc (n_blocks, n_exprs);
4111
4112 hoist_vbein = sbitmap_vector_alloc (n_blocks, n_exprs);
4113 hoist_vbeout = sbitmap_vector_alloc (n_blocks, n_exprs);
4114 hoist_exprs = sbitmap_vector_alloc (n_blocks, n_exprs);
4115 transpout = sbitmap_vector_alloc (n_blocks, n_exprs);
4116 }
4117
4118 /* Free vars used for code hoisting analysis. */
4119
4120 static void
4121 free_code_hoist_mem (void)
4122 {
4123 sbitmap_vector_free (antloc);
4124 sbitmap_vector_free (transp);
4125 sbitmap_vector_free (comp);
4126
4127 sbitmap_vector_free (hoist_vbein);
4128 sbitmap_vector_free (hoist_vbeout);
4129 sbitmap_vector_free (hoist_exprs);
4130 sbitmap_vector_free (transpout);
4131
4132 free_dominance_info (CDI_DOMINATORS);
4133 }
4134
4135 /* Compute the very busy expressions at entry/exit from each block.
4136
4137 An expression is very busy if all paths from a given point
4138 compute the expression. */
4139
4140 static void
4141 compute_code_hoist_vbeinout (void)
4142 {
4143 int changed, passes;
4144 basic_block bb;
4145
4146 sbitmap_vector_zero (hoist_vbeout, last_basic_block);
4147 sbitmap_vector_zero (hoist_vbein, last_basic_block);
4148
4149 passes = 0;
4150 changed = 1;
4151
4152 while (changed)
4153 {
4154 changed = 0;
4155
4156 /* We scan the blocks in the reverse order to speed up
4157 the convergence. */
4158 FOR_EACH_BB_REVERSE (bb)
4159 {
4160 if (bb->next_bb != EXIT_BLOCK_PTR)
4161 sbitmap_intersection_of_succs (hoist_vbeout[bb->index],
4162 hoist_vbein, bb->index);
4163
4164 changed |= sbitmap_a_or_b_and_c_cg (hoist_vbein[bb->index],
4165 antloc[bb->index],
4166 hoist_vbeout[bb->index],
4167 transp[bb->index]);
4168 }
4169
4170 passes++;
4171 }
4172
4173 if (dump_file)
4174 fprintf (dump_file, "hoisting vbeinout computation: %d passes\n", passes);
4175 }
4176
4177 /* Top level routine to do the dataflow analysis needed by code hoisting. */
4178
4179 static void
4180 compute_code_hoist_data (void)
4181 {
4182 compute_local_properties (transp, comp, antloc, &expr_hash_table);
4183 compute_transpout ();
4184 compute_code_hoist_vbeinout ();
4185 calculate_dominance_info (CDI_DOMINATORS);
4186 if (dump_file)
4187 fprintf (dump_file, "\n");
4188 }
4189
4190 /* Determine if the expression identified by EXPR_INDEX would
4191 reach BB unimpared if it was placed at the end of EXPR_BB.
4192
4193 It's unclear exactly what Muchnick meant by "unimpared". It seems
4194 to me that the expression must either be computed or transparent in
4195 *every* block in the path(s) from EXPR_BB to BB. Any other definition
4196 would allow the expression to be hoisted out of loops, even if
4197 the expression wasn't a loop invariant.
4198
4199 Contrast this to reachability for PRE where an expression is
4200 considered reachable if *any* path reaches instead of *all*
4201 paths. */
4202
4203 static int
4204 hoist_expr_reaches_here_p (basic_block expr_bb, int expr_index, basic_block bb, char *visited)
4205 {
4206 edge pred;
4207 edge_iterator ei;
4208 int visited_allocated_locally = 0;
4209
4210
4211 if (visited == NULL)
4212 {
4213 visited_allocated_locally = 1;
4214 visited = XCNEWVEC (char, last_basic_block);
4215 }
4216
4217 FOR_EACH_EDGE (pred, ei, bb->preds)
4218 {
4219 basic_block pred_bb = pred->src;
4220
4221 if (pred->src == ENTRY_BLOCK_PTR)
4222 break;
4223 else if (pred_bb == expr_bb)
4224 continue;
4225 else if (visited[pred_bb->index])
4226 continue;
4227
4228 /* Does this predecessor generate this expression? */
4229 else if (TEST_BIT (comp[pred_bb->index], expr_index))
4230 break;
4231 else if (! TEST_BIT (transp[pred_bb->index], expr_index))
4232 break;
4233
4234 /* Not killed. */
4235 else
4236 {
4237 visited[pred_bb->index] = 1;
4238 if (! hoist_expr_reaches_here_p (expr_bb, expr_index,
4239 pred_bb, visited))
4240 break;
4241 }
4242 }
4243 if (visited_allocated_locally)
4244 free (visited);
4245
4246 return (pred == NULL);
4247 }
4248 \f
4249 /* Actually perform code hoisting. */
4250
4251 static int
4252 hoist_code (void)
4253 {
4254 basic_block bb, dominated;
4255 VEC (basic_block, heap) *domby;
4256 unsigned int i,j;
4257 struct expr **index_map;
4258 struct expr *expr;
4259 int changed = 0;
4260
4261 sbitmap_vector_zero (hoist_exprs, last_basic_block);
4262
4263 /* Compute a mapping from expression number (`bitmap_index') to
4264 hash table entry. */
4265
4266 index_map = XCNEWVEC (struct expr *, expr_hash_table.n_elems);
4267 for (i = 0; i < expr_hash_table.size; i++)
4268 for (expr = expr_hash_table.table[i]; expr != NULL; expr = expr->next_same_hash)
4269 index_map[expr->bitmap_index] = expr;
4270
4271 /* Walk over each basic block looking for potentially hoistable
4272 expressions, nothing gets hoisted from the entry block. */
4273 FOR_EACH_BB (bb)
4274 {
4275 int found = 0;
4276 int insn_inserted_p;
4277
4278 domby = get_dominated_by (CDI_DOMINATORS, bb);
4279 /* Examine each expression that is very busy at the exit of this
4280 block. These are the potentially hoistable expressions. */
4281 for (i = 0; i < hoist_vbeout[bb->index]->n_bits; i++)
4282 {
4283 int hoistable = 0;
4284
4285 if (TEST_BIT (hoist_vbeout[bb->index], i)
4286 && TEST_BIT (transpout[bb->index], i))
4287 {
4288 /* We've found a potentially hoistable expression, now
4289 we look at every block BB dominates to see if it
4290 computes the expression. */
4291 for (j = 0; VEC_iterate (basic_block, domby, j, dominated); j++)
4292 {
4293 /* Ignore self dominance. */
4294 if (bb == dominated)
4295 continue;
4296 /* We've found a dominated block, now see if it computes
4297 the busy expression and whether or not moving that
4298 expression to the "beginning" of that block is safe. */
4299 if (!TEST_BIT (antloc[dominated->index], i))
4300 continue;
4301
4302 /* Note if the expression would reach the dominated block
4303 unimpared if it was placed at the end of BB.
4304
4305 Keep track of how many times this expression is hoistable
4306 from a dominated block into BB. */
4307 if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
4308 hoistable++;
4309 }
4310
4311 /* If we found more than one hoistable occurrence of this
4312 expression, then note it in the bitmap of expressions to
4313 hoist. It makes no sense to hoist things which are computed
4314 in only one BB, and doing so tends to pessimize register
4315 allocation. One could increase this value to try harder
4316 to avoid any possible code expansion due to register
4317 allocation issues; however experiments have shown that
4318 the vast majority of hoistable expressions are only movable
4319 from two successors, so raising this threshold is likely
4320 to nullify any benefit we get from code hoisting. */
4321 if (hoistable > 1)
4322 {
4323 SET_BIT (hoist_exprs[bb->index], i);
4324 found = 1;
4325 }
4326 }
4327 }
4328 /* If we found nothing to hoist, then quit now. */
4329 if (! found)
4330 {
4331 VEC_free (basic_block, heap, domby);
4332 continue;
4333 }
4334
4335 /* Loop over all the hoistable expressions. */
4336 for (i = 0; i < hoist_exprs[bb->index]->n_bits; i++)
4337 {
4338 /* We want to insert the expression into BB only once, so
4339 note when we've inserted it. */
4340 insn_inserted_p = 0;
4341
4342 /* These tests should be the same as the tests above. */
4343 if (TEST_BIT (hoist_exprs[bb->index], i))
4344 {
4345 /* We've found a potentially hoistable expression, now
4346 we look at every block BB dominates to see if it
4347 computes the expression. */
4348 for (j = 0; VEC_iterate (basic_block, domby, j, dominated); j++)
4349 {
4350 /* Ignore self dominance. */
4351 if (bb == dominated)
4352 continue;
4353
4354 /* We've found a dominated block, now see if it computes
4355 the busy expression and whether or not moving that
4356 expression to the "beginning" of that block is safe. */
4357 if (!TEST_BIT (antloc[dominated->index], i))
4358 continue;
4359
4360 /* The expression is computed in the dominated block and
4361 it would be safe to compute it at the start of the
4362 dominated block. Now we have to determine if the
4363 expression would reach the dominated block if it was
4364 placed at the end of BB. */
4365 if (hoist_expr_reaches_here_p (bb, i, dominated, NULL))
4366 {
4367 struct expr *expr = index_map[i];
4368 struct occr *occr = expr->antic_occr;
4369 rtx insn;
4370 rtx set;
4371
4372 /* Find the right occurrence of this expression. */
4373 while (BLOCK_FOR_INSN (occr->insn) != dominated && occr)
4374 occr = occr->next;
4375
4376 gcc_assert (occr);
4377 insn = occr->insn;
4378 set = single_set (insn);
4379 gcc_assert (set);
4380
4381 /* Create a pseudo-reg to store the result of reaching
4382 expressions into. Get the mode for the new pseudo
4383 from the mode of the original destination pseudo. */
4384 if (expr->reaching_reg == NULL)
4385 expr->reaching_reg
4386 = gen_reg_rtx_and_attrs (SET_DEST (set));
4387
4388 gcse_emit_move_after (expr->reaching_reg, SET_DEST (set), insn);
4389 delete_insn (insn);
4390 occr->deleted_p = 1;
4391 changed = 1;
4392 gcse_subst_count++;
4393
4394 if (!insn_inserted_p)
4395 {
4396 insert_insn_end_basic_block (index_map[i], bb, 0);
4397 insn_inserted_p = 1;
4398 }
4399 }
4400 }
4401 }
4402 }
4403 VEC_free (basic_block, heap, domby);
4404 }
4405
4406 free (index_map);
4407
4408 return changed;
4409 }
4410
4411 /* Top level routine to perform one code hoisting (aka unification) pass
4412
4413 Return nonzero if a change was made. */
4414
4415 static int
4416 one_code_hoisting_pass (void)
4417 {
4418 int changed = 0;
4419
4420 gcse_subst_count = 0;
4421 gcse_create_count = 0;
4422
4423 /* Return if there's nothing to do, or it is too expensive. */
4424 if (n_basic_blocks <= NUM_FIXED_BLOCKS + 1
4425 || is_too_expensive (_("GCSE disabled")))
4426 return 0;
4427
4428 /* We need alias. */
4429 init_alias_analysis ();
4430
4431 bytes_used = 0;
4432 gcc_obstack_init (&gcse_obstack);
4433 alloc_gcse_mem ();
4434
4435 alloc_hash_table (get_max_uid (), &expr_hash_table, 0);
4436 compute_hash_table (&expr_hash_table);
4437 if (dump_file)
4438 dump_hash_table (dump_file, "Code Hosting Expressions", &expr_hash_table);
4439
4440 if (expr_hash_table.n_elems > 0)
4441 {
4442 alloc_code_hoist_mem (last_basic_block, expr_hash_table.n_elems);
4443 compute_code_hoist_data ();
4444 changed = hoist_code ();
4445 free_code_hoist_mem ();
4446 }
4447
4448 free_hash_table (&expr_hash_table);
4449 free_gcse_mem ();
4450 obstack_free (&gcse_obstack, NULL);
4451
4452 /* We are finished with alias. */
4453 end_alias_analysis ();
4454
4455 if (dump_file)
4456 {
4457 fprintf (dump_file, "HOIST of %s, %d basic blocks, %d bytes needed, ",
4458 current_function_name (), n_basic_blocks, bytes_used);
4459 fprintf (dump_file, "%d substs, %d insns created\n",
4460 gcse_subst_count, gcse_create_count);
4461 }
4462
4463 return changed;
4464 }
4465 \f
4466 /* Here we provide the things required to do store motion towards
4467 the exit. In order for this to be effective, gcse also needed to
4468 be taught how to move a load when it is kill only by a store to itself.
4469
4470 int i;
4471 float a[10];
4472
4473 void foo(float scale)
4474 {
4475 for (i=0; i<10; i++)
4476 a[i] *= scale;
4477 }
4478
4479 'i' is both loaded and stored to in the loop. Normally, gcse cannot move
4480 the load out since its live around the loop, and stored at the bottom
4481 of the loop.
4482
4483 The 'Load Motion' referred to and implemented in this file is
4484 an enhancement to gcse which when using edge based lcm, recognizes
4485 this situation and allows gcse to move the load out of the loop.
4486
4487 Once gcse has hoisted the load, store motion can then push this
4488 load towards the exit, and we end up with no loads or stores of 'i'
4489 in the loop. */
4490
4491 static hashval_t
4492 pre_ldst_expr_hash (const void *p)
4493 {
4494 int do_not_record_p = 0;
4495 const struct ls_expr *const x = (const struct ls_expr *) p;
4496 return hash_rtx (x->pattern, GET_MODE (x->pattern), &do_not_record_p, NULL, false);
4497 }
4498
4499 static int
4500 pre_ldst_expr_eq (const void *p1, const void *p2)
4501 {
4502 const struct ls_expr *const ptr1 = (const struct ls_expr *) p1,
4503 *const ptr2 = (const struct ls_expr *) p2;
4504 return expr_equiv_p (ptr1->pattern, ptr2->pattern);
4505 }
4506
4507 /* This will search the ldst list for a matching expression. If it
4508 doesn't find one, we create one and initialize it. */
4509
4510 static struct ls_expr *
4511 ldst_entry (rtx x)
4512 {
4513 int do_not_record_p = 0;
4514 struct ls_expr * ptr;
4515 unsigned int hash;
4516 void **slot;
4517 struct ls_expr e;
4518
4519 hash = hash_rtx (x, GET_MODE (x), &do_not_record_p,
4520 NULL, /*have_reg_qty=*/false);
4521
4522 e.pattern = x;
4523 slot = htab_find_slot_with_hash (pre_ldst_table, &e, hash, INSERT);
4524 if (*slot)
4525 return (struct ls_expr *)*slot;
4526
4527 ptr = XNEW (struct ls_expr);
4528
4529 ptr->next = pre_ldst_mems;
4530 ptr->expr = NULL;
4531 ptr->pattern = x;
4532 ptr->pattern_regs = NULL_RTX;
4533 ptr->loads = NULL_RTX;
4534 ptr->stores = NULL_RTX;
4535 ptr->reaching_reg = NULL_RTX;
4536 ptr->invalid = 0;
4537 ptr->index = 0;
4538 ptr->hash_index = hash;
4539 pre_ldst_mems = ptr;
4540 *slot = ptr;
4541
4542 return ptr;
4543 }
4544
4545 /* Free up an individual ldst entry. */
4546
4547 static void
4548 free_ldst_entry (struct ls_expr * ptr)
4549 {
4550 free_INSN_LIST_list (& ptr->loads);
4551 free_INSN_LIST_list (& ptr->stores);
4552
4553 free (ptr);
4554 }
4555
4556 /* Free up all memory associated with the ldst list. */
4557
4558 static void
4559 free_ldst_mems (void)
4560 {
4561 if (pre_ldst_table)
4562 htab_delete (pre_ldst_table);
4563 pre_ldst_table = NULL;
4564
4565 while (pre_ldst_mems)
4566 {
4567 struct ls_expr * tmp = pre_ldst_mems;
4568
4569 pre_ldst_mems = pre_ldst_mems->next;
4570
4571 free_ldst_entry (tmp);
4572 }
4573
4574 pre_ldst_mems = NULL;
4575 }
4576
4577 /* Dump debugging info about the ldst list. */
4578
4579 static void
4580 print_ldst_list (FILE * file)
4581 {
4582 struct ls_expr * ptr;
4583
4584 fprintf (file, "LDST list: \n");
4585
4586 for (ptr = first_ls_expr (); ptr != NULL; ptr = next_ls_expr (ptr))
4587 {
4588 fprintf (file, " Pattern (%3d): ", ptr->index);
4589
4590 print_rtl (file, ptr->pattern);
4591
4592 fprintf (file, "\n Loads : ");
4593
4594 if (ptr->loads)
4595 print_rtl (file, ptr->loads);
4596 else
4597 fprintf (file, "(nil)");
4598
4599 fprintf (file, "\n Stores : ");
4600
4601 if (ptr->stores)
4602 print_rtl (file, ptr->stores);
4603 else
4604 fprintf (file, "(nil)");
4605
4606 fprintf (file, "\n\n");
4607 }
4608
4609 fprintf (file, "\n");
4610 }
4611
4612 /* Returns 1 if X is in the list of ldst only expressions. */
4613
4614 static struct ls_expr *
4615 find_rtx_in_ldst (rtx x)
4616 {
4617 struct ls_expr e;
4618 void **slot;
4619 if (!pre_ldst_table)
4620 return NULL;
4621 e.pattern = x;
4622 slot = htab_find_slot (pre_ldst_table, &e, NO_INSERT);
4623 if (!slot || ((struct ls_expr *)*slot)->invalid)
4624 return NULL;
4625 return (struct ls_expr *) *slot;
4626 }
4627
4628 /* Return first item in the list. */
4629
4630 static inline struct ls_expr *
4631 first_ls_expr (void)
4632 {
4633 return pre_ldst_mems;
4634 }
4635
4636 /* Return the next item in the list after the specified one. */
4637
4638 static inline struct ls_expr *
4639 next_ls_expr (struct ls_expr * ptr)
4640 {
4641 return ptr->next;
4642 }
4643 \f
4644 /* Load Motion for loads which only kill themselves. */
4645
4646 /* Return true if x is a simple MEM operation, with no registers or
4647 side effects. These are the types of loads we consider for the
4648 ld_motion list, otherwise we let the usual aliasing take care of it. */
4649
4650 static int
4651 simple_mem (const_rtx x)
4652 {
4653 if (! MEM_P (x))
4654 return 0;
4655
4656 if (MEM_VOLATILE_P (x))
4657 return 0;
4658
4659 if (GET_MODE (x) == BLKmode)
4660 return 0;
4661
4662 /* If we are handling exceptions, we must be careful with memory references
4663 that may trap. If we are not, the behavior is undefined, so we may just
4664 continue. */
4665 if (flag_non_call_exceptions && may_trap_p (x))
4666 return 0;
4667
4668 if (side_effects_p (x))
4669 return 0;
4670
4671 /* Do not consider function arguments passed on stack. */
4672 if (reg_mentioned_p (stack_pointer_rtx, x))
4673 return 0;
4674
4675 if (flag_float_store && FLOAT_MODE_P (GET_MODE (x)))
4676 return 0;
4677
4678 return 1;
4679 }
4680
4681 /* Make sure there isn't a buried reference in this pattern anywhere.
4682 If there is, invalidate the entry for it since we're not capable
4683 of fixing it up just yet.. We have to be sure we know about ALL
4684 loads since the aliasing code will allow all entries in the
4685 ld_motion list to not-alias itself. If we miss a load, we will get
4686 the wrong value since gcse might common it and we won't know to
4687 fix it up. */
4688
4689 static void
4690 invalidate_any_buried_refs (rtx x)
4691 {
4692 const char * fmt;
4693 int i, j;
4694 struct ls_expr * ptr;
4695
4696 /* Invalidate it in the list. */
4697 if (MEM_P (x) && simple_mem (x))
4698 {
4699 ptr = ldst_entry (x);
4700 ptr->invalid = 1;
4701 }
4702
4703 /* Recursively process the insn. */
4704 fmt = GET_RTX_FORMAT (GET_CODE (x));
4705
4706 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
4707 {
4708 if (fmt[i] == 'e')
4709 invalidate_any_buried_refs (XEXP (x, i));
4710 else if (fmt[i] == 'E')
4711 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4712 invalidate_any_buried_refs (XVECEXP (x, i, j));
4713 }
4714 }
4715
4716 /* Find all the 'simple' MEMs which are used in LOADs and STORES. Simple
4717 being defined as MEM loads and stores to symbols, with no side effects
4718 and no registers in the expression. For a MEM destination, we also
4719 check that the insn is still valid if we replace the destination with a
4720 REG, as is done in update_ld_motion_stores. If there are any uses/defs
4721 which don't match this criteria, they are invalidated and trimmed out
4722 later. */
4723
4724 static void
4725 compute_ld_motion_mems (void)
4726 {
4727 struct ls_expr * ptr;
4728 basic_block bb;
4729 rtx insn;
4730
4731 pre_ldst_mems = NULL;
4732 pre_ldst_table = htab_create (13, pre_ldst_expr_hash,
4733 pre_ldst_expr_eq, NULL);
4734
4735 FOR_EACH_BB (bb)
4736 {
4737 FOR_BB_INSNS (bb, insn)
4738 {
4739 if (INSN_P (insn))
4740 {
4741 if (GET_CODE (PATTERN (insn)) == SET)
4742 {
4743 rtx src = SET_SRC (PATTERN (insn));
4744 rtx dest = SET_DEST (PATTERN (insn));
4745
4746 /* Check for a simple LOAD... */
4747 if (MEM_P (src) && simple_mem (src))
4748 {
4749 ptr = ldst_entry (src);
4750 if (REG_P (dest))
4751 ptr->loads = alloc_INSN_LIST (insn, ptr->loads);
4752 else
4753 ptr->invalid = 1;
4754 }
4755 else
4756 {
4757 /* Make sure there isn't a buried load somewhere. */
4758 invalidate_any_buried_refs (src);
4759 }
4760
4761 /* Check for stores. Don't worry about aliased ones, they
4762 will block any movement we might do later. We only care
4763 about this exact pattern since those are the only
4764 circumstance that we will ignore the aliasing info. */
4765 if (MEM_P (dest) && simple_mem (dest))
4766 {
4767 ptr = ldst_entry (dest);
4768
4769 if (! MEM_P (src)
4770 && GET_CODE (src) != ASM_OPERANDS
4771 /* Check for REG manually since want_to_gcse_p
4772 returns 0 for all REGs. */
4773 && can_assign_to_reg_without_clobbers_p (src))
4774 ptr->stores = alloc_INSN_LIST (insn, ptr->stores);
4775 else
4776 ptr->invalid = 1;
4777 }
4778 }
4779 else
4780 invalidate_any_buried_refs (PATTERN (insn));
4781 }
4782 }
4783 }
4784 }
4785
4786 /* Remove any references that have been either invalidated or are not in the
4787 expression list for pre gcse. */
4788
4789 static void
4790 trim_ld_motion_mems (void)
4791 {
4792 struct ls_expr * * last = & pre_ldst_mems;
4793 struct ls_expr * ptr = pre_ldst_mems;
4794
4795 while (ptr != NULL)
4796 {
4797 struct expr * expr;
4798
4799 /* Delete if entry has been made invalid. */
4800 if (! ptr->invalid)
4801 {
4802 /* Delete if we cannot find this mem in the expression list. */
4803 unsigned int hash = ptr->hash_index % expr_hash_table.size;
4804
4805 for (expr = expr_hash_table.table[hash];
4806 expr != NULL;
4807 expr = expr->next_same_hash)
4808 if (expr_equiv_p (expr->expr, ptr->pattern))
4809 break;
4810 }
4811 else
4812 expr = (struct expr *) 0;
4813
4814 if (expr)
4815 {
4816 /* Set the expression field if we are keeping it. */
4817 ptr->expr = expr;
4818 last = & ptr->next;
4819 ptr = ptr->next;
4820 }
4821 else
4822 {
4823 *last = ptr->next;
4824 htab_remove_elt_with_hash (pre_ldst_table, ptr, ptr->hash_index);
4825 free_ldst_entry (ptr);
4826 ptr = * last;
4827 }
4828 }
4829
4830 /* Show the world what we've found. */
4831 if (dump_file && pre_ldst_mems != NULL)
4832 print_ldst_list (dump_file);
4833 }
4834
4835 /* This routine will take an expression which we are replacing with
4836 a reaching register, and update any stores that are needed if
4837 that expression is in the ld_motion list. Stores are updated by
4838 copying their SRC to the reaching register, and then storing
4839 the reaching register into the store location. These keeps the
4840 correct value in the reaching register for the loads. */
4841
4842 static void
4843 update_ld_motion_stores (struct expr * expr)
4844 {
4845 struct ls_expr * mem_ptr;
4846
4847 if ((mem_ptr = find_rtx_in_ldst (expr->expr)))
4848 {
4849 /* We can try to find just the REACHED stores, but is shouldn't
4850 matter to set the reaching reg everywhere... some might be
4851 dead and should be eliminated later. */
4852
4853 /* We replace (set mem expr) with (set reg expr) (set mem reg)
4854 where reg is the reaching reg used in the load. We checked in
4855 compute_ld_motion_mems that we can replace (set mem expr) with
4856 (set reg expr) in that insn. */
4857 rtx list = mem_ptr->stores;
4858
4859 for ( ; list != NULL_RTX; list = XEXP (list, 1))
4860 {
4861 rtx insn = XEXP (list, 0);
4862 rtx pat = PATTERN (insn);
4863 rtx src = SET_SRC (pat);
4864 rtx reg = expr->reaching_reg;
4865 rtx copy, new_rtx;
4866
4867 /* If we've already copied it, continue. */
4868 if (expr->reaching_reg == src)
4869 continue;
4870
4871 if (dump_file)
4872 {
4873 fprintf (dump_file, "PRE: store updated with reaching reg ");
4874 print_rtl (dump_file, expr->reaching_reg);
4875 fprintf (dump_file, ":\n ");
4876 print_inline_rtx (dump_file, insn, 8);
4877 fprintf (dump_file, "\n");
4878 }
4879
4880 copy = gen_move_insn (reg, copy_rtx (SET_SRC (pat)));
4881 new_rtx = emit_insn_before (copy, insn);
4882 SET_SRC (pat) = reg;
4883 df_insn_rescan (insn);
4884
4885 /* un-recognize this pattern since it's probably different now. */
4886 INSN_CODE (insn) = -1;
4887 gcse_create_count++;
4888 }
4889 }
4890 }
4891 \f
4892 /* Return true if the graph is too expensive to optimize. PASS is the
4893 optimization about to be performed. */
4894
4895 static bool
4896 is_too_expensive (const char *pass)
4897 {
4898 /* Trying to perform global optimizations on flow graphs which have
4899 a high connectivity will take a long time and is unlikely to be
4900 particularly useful.
4901
4902 In normal circumstances a cfg should have about twice as many
4903 edges as blocks. But we do not want to punish small functions
4904 which have a couple switch statements. Rather than simply
4905 threshold the number of blocks, uses something with a more
4906 graceful degradation. */
4907 if (n_edges > 20000 + n_basic_blocks * 4)
4908 {
4909 warning (OPT_Wdisabled_optimization,
4910 "%s: %d basic blocks and %d edges/basic block",
4911 pass, n_basic_blocks, n_edges / n_basic_blocks);
4912
4913 return true;
4914 }
4915
4916 /* If allocating memory for the cprop bitmap would take up too much
4917 storage it's better just to disable the optimization. */
4918 if ((n_basic_blocks
4919 * SBITMAP_SET_SIZE (max_reg_num ())
4920 * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
4921 {
4922 warning (OPT_Wdisabled_optimization,
4923 "%s: %d basic blocks and %d registers",
4924 pass, n_basic_blocks, max_reg_num ());
4925
4926 return true;
4927 }
4928
4929 return false;
4930 }
4931
4932 \f
4933 /* Main function for the CPROP pass. */
4934
4935 static int
4936 one_cprop_pass (void)
4937 {
4938 int changed = 0;
4939
4940 /* Return if there's nothing to do, or it is too expensive. */
4941 if (n_basic_blocks <= NUM_FIXED_BLOCKS + 1
4942 || is_too_expensive (_ ("const/copy propagation disabled")))
4943 return 0;
4944
4945 global_const_prop_count = local_const_prop_count = 0;
4946 global_copy_prop_count = local_copy_prop_count = 0;
4947
4948 bytes_used = 0;
4949 gcc_obstack_init (&gcse_obstack);
4950 alloc_gcse_mem ();
4951
4952 /* Do a local const/copy propagation pass first. The global pass
4953 only handles global opportunities.
4954 If the local pass changes something, remove any unreachable blocks
4955 because the CPROP global dataflow analysis may get into infinite
4956 loops for CFGs with unreachable blocks.
4957
4958 FIXME: This local pass should not be necessary after CSE (but for
4959 some reason it still is). It is also (proven) not necessary
4960 to run the local pass right after FWPWOP.
4961
4962 FIXME: The global analysis would not get into infinite loops if it
4963 would use the DF solver (via df_simple_dataflow) instead of
4964 the solver implemented in this file. */
4965 if (local_cprop_pass ())
4966 {
4967 delete_unreachable_blocks ();
4968 df_analyze ();
4969 }
4970
4971 /* Determine implicit sets. */
4972 implicit_sets = XCNEWVEC (rtx, last_basic_block);
4973 find_implicit_sets ();
4974
4975 alloc_hash_table (get_max_uid (), &set_hash_table, 1);
4976 compute_hash_table (&set_hash_table);
4977
4978 /* Free implicit_sets before peak usage. */
4979 free (implicit_sets);
4980 implicit_sets = NULL;
4981
4982 if (dump_file)
4983 dump_hash_table (dump_file, "SET", &set_hash_table);
4984 if (set_hash_table.n_elems > 0)
4985 {
4986 basic_block bb;
4987 rtx insn;
4988
4989 alloc_cprop_mem (last_basic_block, set_hash_table.n_elems);
4990 compute_cprop_data ();
4991
4992 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb, EXIT_BLOCK_PTR, next_bb)
4993 {
4994 /* Reset tables used to keep track of what's still valid [since
4995 the start of the block]. */
4996 reset_opr_set_tables ();
4997
4998 FOR_BB_INSNS (bb, insn)
4999 if (INSN_P (insn))
5000 {
5001 changed |= cprop_insn (insn);
5002
5003 /* Keep track of everything modified by this insn. */
5004 /* ??? Need to be careful w.r.t. mods done to INSN.
5005 Don't call mark_oprs_set if we turned the
5006 insn into a NOTE. */
5007 if (! NOTE_P (insn))
5008 mark_oprs_set (insn);
5009 }
5010 }
5011
5012 changed |= bypass_conditional_jumps ();
5013 free_cprop_mem ();
5014 }
5015
5016 free_hash_table (&set_hash_table);
5017 free_gcse_mem ();
5018 obstack_free (&gcse_obstack, NULL);
5019
5020 if (dump_file)
5021 {
5022 fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
5023 current_function_name (), n_basic_blocks, bytes_used);
5024 fprintf (dump_file, "%d local const props, %d local copy props, ",
5025 local_const_prop_count, local_copy_prop_count);
5026 fprintf (dump_file, "%d global const props, %d global copy props\n\n",
5027 global_const_prop_count, global_copy_prop_count);
5028 }
5029
5030 return changed;
5031 }
5032
5033 \f
5034 /* All the passes implemented in this file. Each pass has its
5035 own gate and execute function, and at the end of the file a
5036 pass definition for passes.c.
5037
5038 We do not construct an accurate cfg in functions which call
5039 setjmp, so none of these passes runs if the function calls
5040 setjmp.
5041 FIXME: Should just handle setjmp via REG_SETJMP notes. */
5042
5043 static bool
5044 gate_rtl_cprop (void)
5045 {
5046 return optimize > 0 && flag_gcse
5047 && !cfun->calls_setjmp
5048 && dbg_cnt (cprop);
5049 }
5050
5051 static unsigned int
5052 execute_rtl_cprop (void)
5053 {
5054 delete_unreachable_blocks ();
5055 df_note_add_problem ();
5056 df_set_flags (DF_LR_RUN_DCE);
5057 df_analyze ();
5058 flag_rerun_cse_after_global_opts |= one_cprop_pass ();
5059 return 0;
5060 }
5061
5062 static bool
5063 gate_rtl_pre (void)
5064 {
5065 return optimize > 0 && flag_gcse
5066 && !cfun->calls_setjmp
5067 && optimize_function_for_speed_p (cfun)
5068 && dbg_cnt (pre);
5069 }
5070
5071 static unsigned int
5072 execute_rtl_pre (void)
5073 {
5074 delete_unreachable_blocks ();
5075 df_note_add_problem ();
5076 df_analyze ();
5077 flag_rerun_cse_after_global_opts |= one_pre_gcse_pass ();
5078 return 0;
5079 }
5080
5081 static bool
5082 gate_rtl_hoist (void)
5083 {
5084 return optimize > 0 && flag_gcse
5085 && !cfun->calls_setjmp
5086 /* It does not make sense to run code hoisting unless we are optimizing
5087 for code size -- it rarely makes programs faster, and can make then
5088 bigger if we did PRE (when optimizing for space, we don't run PRE). */
5089 && optimize_function_for_size_p (cfun)
5090 && dbg_cnt (hoist);
5091 }
5092
5093 static unsigned int
5094 execute_rtl_hoist (void)
5095 {
5096 delete_unreachable_blocks ();
5097 df_note_add_problem ();
5098 df_analyze ();
5099 flag_rerun_cse_after_global_opts |= one_code_hoisting_pass ();
5100 return 0;
5101 }
5102
5103 struct rtl_opt_pass pass_rtl_cprop =
5104 {
5105 {
5106 RTL_PASS,
5107 "cprop", /* name */
5108 gate_rtl_cprop, /* gate */
5109 execute_rtl_cprop, /* execute */
5110 NULL, /* sub */
5111 NULL, /* next */
5112 0, /* static_pass_number */
5113 TV_CPROP, /* tv_id */
5114 PROP_cfglayout, /* properties_required */
5115 0, /* properties_provided */
5116 0, /* properties_destroyed */
5117 0, /* todo_flags_start */
5118 TODO_df_finish | TODO_verify_rtl_sharing |
5119 TODO_dump_func |
5120 TODO_verify_flow | TODO_ggc_collect /* todo_flags_finish */
5121 }
5122 };
5123
5124 struct rtl_opt_pass pass_rtl_pre =
5125 {
5126 {
5127 RTL_PASS,
5128 "pre", /* name */
5129 gate_rtl_pre, /* gate */
5130 execute_rtl_pre, /* execute */
5131 NULL, /* sub */
5132 NULL, /* next */
5133 0, /* static_pass_number */
5134 TV_PRE, /* tv_id */
5135 PROP_cfglayout, /* properties_required */
5136 0, /* properties_provided */
5137 0, /* properties_destroyed */
5138 0, /* todo_flags_start */
5139 TODO_df_finish | TODO_verify_rtl_sharing |
5140 TODO_dump_func |
5141 TODO_verify_flow | TODO_ggc_collect /* todo_flags_finish */
5142 }
5143 };
5144
5145 struct rtl_opt_pass pass_rtl_hoist =
5146 {
5147 {
5148 RTL_PASS,
5149 "hoist", /* name */
5150 gate_rtl_hoist, /* gate */
5151 execute_rtl_hoist, /* execute */
5152 NULL, /* sub */
5153 NULL, /* next */
5154 0, /* static_pass_number */
5155 TV_HOIST, /* tv_id */
5156 PROP_cfglayout, /* properties_required */
5157 0, /* properties_provided */
5158 0, /* properties_destroyed */
5159 0, /* todo_flags_start */
5160 TODO_df_finish | TODO_verify_rtl_sharing |
5161 TODO_dump_func |
5162 TODO_verify_flow | TODO_ggc_collect /* todo_flags_finish */
5163 }
5164 };
5165
5166 #include "gt-gcse.h"