annotate gcc/cprop.c @ 131:84e7813d76e9

gcc-8.2
author mir3636
date Thu, 25 Oct 2018 07:37:49 +0900
parents 04ced10e8804
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Global constant/copy propagation for RTL.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 1997-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3
kono
parents:
diff changeset
4 This file is part of GCC.
kono
parents:
diff changeset
5
kono
parents:
diff changeset
6 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
7 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
8 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
9 version.
kono
parents:
diff changeset
10
kono
parents:
diff changeset
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
14 for more details.
kono
parents:
diff changeset
15
kono
parents:
diff changeset
16 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
17 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
18 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
19
kono
parents:
diff changeset
20 #include "config.h"
kono
parents:
diff changeset
21 #include "system.h"
kono
parents:
diff changeset
22 #include "coretypes.h"
kono
parents:
diff changeset
23 #include "backend.h"
kono
parents:
diff changeset
24 #include "rtl.h"
kono
parents:
diff changeset
25 #include "cfghooks.h"
kono
parents:
diff changeset
26 #include "df.h"
kono
parents:
diff changeset
27 #include "insn-config.h"
kono
parents:
diff changeset
28 #include "memmodel.h"
kono
parents:
diff changeset
29 #include "emit-rtl.h"
kono
parents:
diff changeset
30 #include "recog.h"
kono
parents:
diff changeset
31 #include "diagnostic-core.h"
kono
parents:
diff changeset
32 #include "toplev.h"
kono
parents:
diff changeset
33 #include "cfgrtl.h"
kono
parents:
diff changeset
34 #include "cfganal.h"
kono
parents:
diff changeset
35 #include "lcm.h"
kono
parents:
diff changeset
36 #include "cfgcleanup.h"
kono
parents:
diff changeset
37 #include "params.h"
kono
parents:
diff changeset
38 #include "cselib.h"
kono
parents:
diff changeset
39 #include "intl.h"
kono
parents:
diff changeset
40 #include "tree-pass.h"
kono
parents:
diff changeset
41 #include "dbgcnt.h"
kono
parents:
diff changeset
42 #include "cfgloop.h"
kono
parents:
diff changeset
43 #include "gcse.h"
kono
parents:
diff changeset
44
kono
parents:
diff changeset
45
kono
parents:
diff changeset
46 /* An obstack for our working variables. */
kono
parents:
diff changeset
47 static struct obstack cprop_obstack;
kono
parents:
diff changeset
48
kono
parents:
diff changeset
49 /* Occurrence of an expression.
kono
parents:
diff changeset
50 There is one per basic block. If a pattern appears more than once the
kono
parents:
diff changeset
51 last appearance is used. */
kono
parents:
diff changeset
52
kono
parents:
diff changeset
53 struct cprop_occr
kono
parents:
diff changeset
54 {
kono
parents:
diff changeset
55 /* Next occurrence of this expression. */
kono
parents:
diff changeset
56 struct cprop_occr *next;
kono
parents:
diff changeset
57 /* The insn that computes the expression. */
kono
parents:
diff changeset
58 rtx_insn *insn;
kono
parents:
diff changeset
59 };
kono
parents:
diff changeset
60
kono
parents:
diff changeset
61 /* Hash table entry for assignment expressions. */
kono
parents:
diff changeset
62
kono
parents:
diff changeset
63 struct cprop_expr
kono
parents:
diff changeset
64 {
kono
parents:
diff changeset
65 /* The expression (DEST := SRC). */
kono
parents:
diff changeset
66 rtx dest;
kono
parents:
diff changeset
67 rtx src;
kono
parents:
diff changeset
68
kono
parents:
diff changeset
69 /* Index in the available expression bitmaps. */
kono
parents:
diff changeset
70 int bitmap_index;
kono
parents:
diff changeset
71 /* Next entry with the same hash. */
kono
parents:
diff changeset
72 struct cprop_expr *next_same_hash;
kono
parents:
diff changeset
73 /* List of available occurrence in basic blocks in the function.
kono
parents:
diff changeset
74 An "available occurrence" is one that is the last occurrence in the
kono
parents:
diff changeset
75 basic block and whose operands are not modified by following statements
kono
parents:
diff changeset
76 in the basic block [including this insn]. */
kono
parents:
diff changeset
77 struct cprop_occr *avail_occr;
kono
parents:
diff changeset
78 };
kono
parents:
diff changeset
79
kono
parents:
diff changeset
80 /* Hash table for copy propagation expressions.
kono
parents:
diff changeset
81 Each hash table is an array of buckets.
kono
parents:
diff changeset
82 ??? It is known that if it were an array of entries, structure elements
kono
parents:
diff changeset
83 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
kono
parents:
diff changeset
84 not clear whether in the final analysis a sufficient amount of memory would
kono
parents:
diff changeset
85 be saved as the size of the available expression bitmaps would be larger
kono
parents:
diff changeset
86 [one could build a mapping table without holes afterwards though].
kono
parents:
diff changeset
87 Someday I'll perform the computation and figure it out. */
kono
parents:
diff changeset
88
kono
parents:
diff changeset
89 struct hash_table_d
kono
parents:
diff changeset
90 {
kono
parents:
diff changeset
91 /* The table itself.
kono
parents:
diff changeset
92 This is an array of `set_hash_table_size' elements. */
kono
parents:
diff changeset
93 struct cprop_expr **table;
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 /* Size of the hash table, in elements. */
kono
parents:
diff changeset
96 unsigned int size;
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 /* Number of hash table elements. */
kono
parents:
diff changeset
99 unsigned int n_elems;
kono
parents:
diff changeset
100 };
kono
parents:
diff changeset
101
kono
parents:
diff changeset
102 /* Copy propagation hash table. */
kono
parents:
diff changeset
103 static struct hash_table_d set_hash_table;
kono
parents:
diff changeset
104
kono
parents:
diff changeset
105 /* Array of implicit set patterns indexed by basic block index. */
kono
parents:
diff changeset
106 static rtx *implicit_sets;
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 /* Array of indexes of expressions for implicit set patterns indexed by basic
kono
parents:
diff changeset
109 block index. In other words, implicit_set_indexes[i] is the bitmap_index
kono
parents:
diff changeset
110 of the expression whose RTX is implicit_sets[i]. */
kono
parents:
diff changeset
111 static int *implicit_set_indexes;
kono
parents:
diff changeset
112
kono
parents:
diff changeset
113 /* Bitmap containing one bit for each register in the program.
kono
parents:
diff changeset
114 Used when performing GCSE to track which registers have been set since
kono
parents:
diff changeset
115 the start or end of the basic block while traversing that block. */
kono
parents:
diff changeset
116 static regset reg_set_bitmap;
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 /* Various variables for statistics gathering. */
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 /* Memory used in a pass.
kono
parents:
diff changeset
121 This isn't intended to be absolutely precise. Its intent is only
kono
parents:
diff changeset
122 to keep an eye on memory usage. */
kono
parents:
diff changeset
123 static int bytes_used;
kono
parents:
diff changeset
124
kono
parents:
diff changeset
125 /* Number of local constants propagated. */
kono
parents:
diff changeset
126 static int local_const_prop_count;
kono
parents:
diff changeset
127 /* Number of local copies propagated. */
kono
parents:
diff changeset
128 static int local_copy_prop_count;
kono
parents:
diff changeset
129 /* Number of global constants propagated. */
kono
parents:
diff changeset
130 static int global_const_prop_count;
kono
parents:
diff changeset
131 /* Number of global copies propagated. */
kono
parents:
diff changeset
132 static int global_copy_prop_count;
kono
parents:
diff changeset
133
kono
parents:
diff changeset
134 #define GOBNEW(T) ((T *) cprop_alloc (sizeof (T)))
kono
parents:
diff changeset
135 #define GOBNEWVAR(T, S) ((T *) cprop_alloc ((S)))
kono
parents:
diff changeset
136
kono
parents:
diff changeset
137 /* Cover function to obstack_alloc. */
kono
parents:
diff changeset
138
kono
parents:
diff changeset
139 static void *
kono
parents:
diff changeset
140 cprop_alloc (unsigned long size)
kono
parents:
diff changeset
141 {
kono
parents:
diff changeset
142 bytes_used += size;
kono
parents:
diff changeset
143 return obstack_alloc (&cprop_obstack, size);
kono
parents:
diff changeset
144 }
kono
parents:
diff changeset
145
kono
parents:
diff changeset
146 /* Return nonzero if register X is unchanged from INSN to the end
kono
parents:
diff changeset
147 of INSN's basic block. */
kono
parents:
diff changeset
148
kono
parents:
diff changeset
149 static int
kono
parents:
diff changeset
150 reg_available_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
151 {
kono
parents:
diff changeset
152 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
kono
parents:
diff changeset
153 }
kono
parents:
diff changeset
154
kono
parents:
diff changeset
155 /* Hash a set of register REGNO.
kono
parents:
diff changeset
156
kono
parents:
diff changeset
157 Sets are hashed on the register that is set. This simplifies the PRE copy
kono
parents:
diff changeset
158 propagation code.
kono
parents:
diff changeset
159
kono
parents:
diff changeset
160 ??? May need to make things more elaborate. Later, as necessary. */
kono
parents:
diff changeset
161
kono
parents:
diff changeset
162 static unsigned int
kono
parents:
diff changeset
163 hash_mod (int regno, int hash_table_size)
kono
parents:
diff changeset
164 {
kono
parents:
diff changeset
165 return (unsigned) regno % hash_table_size;
kono
parents:
diff changeset
166 }
kono
parents:
diff changeset
167
kono
parents:
diff changeset
168 /* Insert assignment DEST:=SET from INSN in the hash table.
kono
parents:
diff changeset
169 DEST is a register and SET is a register or a suitable constant.
kono
parents:
diff changeset
170 If the assignment is already present in the table, record it as
kono
parents:
diff changeset
171 the last occurrence in INSN's basic block.
kono
parents:
diff changeset
172 IMPLICIT is true if it's an implicit set, false otherwise. */
kono
parents:
diff changeset
173
kono
parents:
diff changeset
174 static void
kono
parents:
diff changeset
175 insert_set_in_table (rtx dest, rtx src, rtx_insn *insn,
kono
parents:
diff changeset
176 struct hash_table_d *table, bool implicit)
kono
parents:
diff changeset
177 {
kono
parents:
diff changeset
178 bool found = false;
kono
parents:
diff changeset
179 unsigned int hash;
kono
parents:
diff changeset
180 struct cprop_expr *cur_expr, *last_expr = NULL;
kono
parents:
diff changeset
181 struct cprop_occr *cur_occr;
kono
parents:
diff changeset
182
kono
parents:
diff changeset
183 hash = hash_mod (REGNO (dest), table->size);
kono
parents:
diff changeset
184
kono
parents:
diff changeset
185 for (cur_expr = table->table[hash]; cur_expr;
kono
parents:
diff changeset
186 cur_expr = cur_expr->next_same_hash)
kono
parents:
diff changeset
187 {
kono
parents:
diff changeset
188 if (dest == cur_expr->dest
kono
parents:
diff changeset
189 && src == cur_expr->src)
kono
parents:
diff changeset
190 {
kono
parents:
diff changeset
191 found = true;
kono
parents:
diff changeset
192 break;
kono
parents:
diff changeset
193 }
kono
parents:
diff changeset
194 last_expr = cur_expr;
kono
parents:
diff changeset
195 }
kono
parents:
diff changeset
196
kono
parents:
diff changeset
197 if (! found)
kono
parents:
diff changeset
198 {
kono
parents:
diff changeset
199 cur_expr = GOBNEW (struct cprop_expr);
kono
parents:
diff changeset
200 bytes_used += sizeof (struct cprop_expr);
kono
parents:
diff changeset
201 if (table->table[hash] == NULL)
kono
parents:
diff changeset
202 /* This is the first pattern that hashed to this index. */
kono
parents:
diff changeset
203 table->table[hash] = cur_expr;
kono
parents:
diff changeset
204 else
kono
parents:
diff changeset
205 /* Add EXPR to end of this hash chain. */
kono
parents:
diff changeset
206 last_expr->next_same_hash = cur_expr;
kono
parents:
diff changeset
207
kono
parents:
diff changeset
208 /* Set the fields of the expr element.
kono
parents:
diff changeset
209 We must copy X because it can be modified when copy propagation is
kono
parents:
diff changeset
210 performed on its operands. */
kono
parents:
diff changeset
211 cur_expr->dest = copy_rtx (dest);
kono
parents:
diff changeset
212 cur_expr->src = copy_rtx (src);
kono
parents:
diff changeset
213 cur_expr->bitmap_index = table->n_elems++;
kono
parents:
diff changeset
214 cur_expr->next_same_hash = NULL;
kono
parents:
diff changeset
215 cur_expr->avail_occr = NULL;
kono
parents:
diff changeset
216 }
kono
parents:
diff changeset
217
kono
parents:
diff changeset
218 /* Now record the occurrence. */
kono
parents:
diff changeset
219 cur_occr = cur_expr->avail_occr;
kono
parents:
diff changeset
220
kono
parents:
diff changeset
221 if (cur_occr
kono
parents:
diff changeset
222 && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn))
kono
parents:
diff changeset
223 {
kono
parents:
diff changeset
224 /* Found another instance of the expression in the same basic block.
kono
parents:
diff changeset
225 Prefer this occurrence to the currently recorded one. We want
kono
parents:
diff changeset
226 the last one in the block and the block is scanned from start
kono
parents:
diff changeset
227 to end. */
kono
parents:
diff changeset
228 cur_occr->insn = insn;
kono
parents:
diff changeset
229 }
kono
parents:
diff changeset
230 else
kono
parents:
diff changeset
231 {
kono
parents:
diff changeset
232 /* First occurrence of this expression in this basic block. */
kono
parents:
diff changeset
233 cur_occr = GOBNEW (struct cprop_occr);
kono
parents:
diff changeset
234 bytes_used += sizeof (struct cprop_occr);
kono
parents:
diff changeset
235 cur_occr->insn = insn;
kono
parents:
diff changeset
236 cur_occr->next = cur_expr->avail_occr;
kono
parents:
diff changeset
237 cur_expr->avail_occr = cur_occr;
kono
parents:
diff changeset
238 }
kono
parents:
diff changeset
239
kono
parents:
diff changeset
240 /* Record bitmap_index of the implicit set in implicit_set_indexes. */
kono
parents:
diff changeset
241 if (implicit)
kono
parents:
diff changeset
242 implicit_set_indexes[BLOCK_FOR_INSN (insn)->index]
kono
parents:
diff changeset
243 = cur_expr->bitmap_index;
kono
parents:
diff changeset
244 }
kono
parents:
diff changeset
245
kono
parents:
diff changeset
246 /* Determine whether the rtx X should be treated as a constant for CPROP.
kono
parents:
diff changeset
247 Since X might be inserted more than once we have to take care that it
kono
parents:
diff changeset
248 is sharable. */
kono
parents:
diff changeset
249
kono
parents:
diff changeset
250 static bool
kono
parents:
diff changeset
251 cprop_constant_p (const_rtx x)
kono
parents:
diff changeset
252 {
kono
parents:
diff changeset
253 return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
kono
parents:
diff changeset
254 }
kono
parents:
diff changeset
255
kono
parents:
diff changeset
256 /* Determine whether the rtx X should be treated as a register that can
kono
parents:
diff changeset
257 be propagated. Any pseudo-register is fine. */
kono
parents:
diff changeset
258
kono
parents:
diff changeset
259 static bool
kono
parents:
diff changeset
260 cprop_reg_p (const_rtx x)
kono
parents:
diff changeset
261 {
kono
parents:
diff changeset
262 return REG_P (x) && !HARD_REGISTER_P (x);
kono
parents:
diff changeset
263 }
kono
parents:
diff changeset
264
kono
parents:
diff changeset
265 /* Scan SET present in INSN and add an entry to the hash TABLE.
kono
parents:
diff changeset
266 IMPLICIT is true if it's an implicit set, false otherwise. */
kono
parents:
diff changeset
267
kono
parents:
diff changeset
268 static void
kono
parents:
diff changeset
269 hash_scan_set (rtx set, rtx_insn *insn, struct hash_table_d *table,
kono
parents:
diff changeset
270 bool implicit)
kono
parents:
diff changeset
271 {
kono
parents:
diff changeset
272 rtx src = SET_SRC (set);
kono
parents:
diff changeset
273 rtx dest = SET_DEST (set);
kono
parents:
diff changeset
274
kono
parents:
diff changeset
275 if (cprop_reg_p (dest)
kono
parents:
diff changeset
276 && reg_available_p (dest, insn)
kono
parents:
diff changeset
277 && can_copy_p (GET_MODE (dest)))
kono
parents:
diff changeset
278 {
kono
parents:
diff changeset
279 /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
kono
parents:
diff changeset
280
kono
parents:
diff changeset
281 This allows us to do a single CPROP pass and still eliminate
kono
parents:
diff changeset
282 redundant constants, addresses or other expressions that are
kono
parents:
diff changeset
283 constructed with multiple instructions.
kono
parents:
diff changeset
284
kono
parents:
diff changeset
285 However, keep the original SRC if INSN is a simple reg-reg move. In
kono
parents:
diff changeset
286 In this case, there will almost always be a REG_EQUAL note on the
kono
parents:
diff changeset
287 insn that sets SRC. By recording the REG_EQUAL value here as SRC
kono
parents:
diff changeset
288 for INSN, we miss copy propagation opportunities.
kono
parents:
diff changeset
289
kono
parents:
diff changeset
290 Note that this does not impede profitable constant propagations. We
kono
parents:
diff changeset
291 "look through" reg-reg sets in lookup_set. */
kono
parents:
diff changeset
292 rtx note = find_reg_equal_equiv_note (insn);
kono
parents:
diff changeset
293 if (note != 0
kono
parents:
diff changeset
294 && REG_NOTE_KIND (note) == REG_EQUAL
kono
parents:
diff changeset
295 && !REG_P (src)
kono
parents:
diff changeset
296 && cprop_constant_p (XEXP (note, 0)))
kono
parents:
diff changeset
297 src = XEXP (note, 0), set = gen_rtx_SET (dest, src);
kono
parents:
diff changeset
298
kono
parents:
diff changeset
299 /* Record sets for constant/copy propagation. */
kono
parents:
diff changeset
300 if ((cprop_reg_p (src)
kono
parents:
diff changeset
301 && src != dest
kono
parents:
diff changeset
302 && reg_available_p (src, insn))
kono
parents:
diff changeset
303 || cprop_constant_p (src))
kono
parents:
diff changeset
304 insert_set_in_table (dest, src, insn, table, implicit);
kono
parents:
diff changeset
305 }
kono
parents:
diff changeset
306 }
kono
parents:
diff changeset
307
kono
parents:
diff changeset
308 /* Process INSN and add hash table entries as appropriate. */
kono
parents:
diff changeset
309
kono
parents:
diff changeset
310 static void
kono
parents:
diff changeset
311 hash_scan_insn (rtx_insn *insn, struct hash_table_d *table)
kono
parents:
diff changeset
312 {
kono
parents:
diff changeset
313 rtx pat = PATTERN (insn);
kono
parents:
diff changeset
314 int i;
kono
parents:
diff changeset
315
kono
parents:
diff changeset
316 /* Pick out the sets of INSN and for other forms of instructions record
kono
parents:
diff changeset
317 what's been modified. */
kono
parents:
diff changeset
318
kono
parents:
diff changeset
319 if (GET_CODE (pat) == SET)
kono
parents:
diff changeset
320 hash_scan_set (pat, insn, table, false);
kono
parents:
diff changeset
321 else if (GET_CODE (pat) == PARALLEL)
kono
parents:
diff changeset
322 for (i = 0; i < XVECLEN (pat, 0); i++)
kono
parents:
diff changeset
323 {
kono
parents:
diff changeset
324 rtx x = XVECEXP (pat, 0, i);
kono
parents:
diff changeset
325
kono
parents:
diff changeset
326 if (GET_CODE (x) == SET)
kono
parents:
diff changeset
327 hash_scan_set (x, insn, table, false);
kono
parents:
diff changeset
328 }
kono
parents:
diff changeset
329 }
kono
parents:
diff changeset
330
kono
parents:
diff changeset
331 /* Dump the hash table TABLE to file FILE under the name NAME. */
kono
parents:
diff changeset
332
kono
parents:
diff changeset
333 static void
kono
parents:
diff changeset
334 dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
kono
parents:
diff changeset
335 {
kono
parents:
diff changeset
336 int i;
kono
parents:
diff changeset
337 /* Flattened out table, so it's printed in proper order. */
kono
parents:
diff changeset
338 struct cprop_expr **flat_table;
kono
parents:
diff changeset
339 unsigned int *hash_val;
kono
parents:
diff changeset
340 struct cprop_expr *expr;
kono
parents:
diff changeset
341
kono
parents:
diff changeset
342 flat_table = XCNEWVEC (struct cprop_expr *, table->n_elems);
kono
parents:
diff changeset
343 hash_val = XNEWVEC (unsigned int, table->n_elems);
kono
parents:
diff changeset
344
kono
parents:
diff changeset
345 for (i = 0; i < (int) table->size; i++)
kono
parents:
diff changeset
346 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
kono
parents:
diff changeset
347 {
kono
parents:
diff changeset
348 flat_table[expr->bitmap_index] = expr;
kono
parents:
diff changeset
349 hash_val[expr->bitmap_index] = i;
kono
parents:
diff changeset
350 }
kono
parents:
diff changeset
351
kono
parents:
diff changeset
352 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
kono
parents:
diff changeset
353 name, table->size, table->n_elems);
kono
parents:
diff changeset
354
kono
parents:
diff changeset
355 for (i = 0; i < (int) table->n_elems; i++)
kono
parents:
diff changeset
356 if (flat_table[i] != 0)
kono
parents:
diff changeset
357 {
kono
parents:
diff changeset
358 expr = flat_table[i];
kono
parents:
diff changeset
359 fprintf (file, "Index %d (hash value %d)\n ",
kono
parents:
diff changeset
360 expr->bitmap_index, hash_val[i]);
kono
parents:
diff changeset
361 print_rtl (file, expr->dest);
kono
parents:
diff changeset
362 fprintf (file, " := ");
kono
parents:
diff changeset
363 print_rtl (file, expr->src);
kono
parents:
diff changeset
364 fprintf (file, "\n");
kono
parents:
diff changeset
365 }
kono
parents:
diff changeset
366
kono
parents:
diff changeset
367 fprintf (file, "\n");
kono
parents:
diff changeset
368
kono
parents:
diff changeset
369 free (flat_table);
kono
parents:
diff changeset
370 free (hash_val);
kono
parents:
diff changeset
371 }
kono
parents:
diff changeset
372
kono
parents:
diff changeset
373 /* Record as unavailable all registers that are DEF operands of INSN. */
kono
parents:
diff changeset
374
kono
parents:
diff changeset
375 static void
kono
parents:
diff changeset
376 make_set_regs_unavailable (rtx_insn *insn)
kono
parents:
diff changeset
377 {
kono
parents:
diff changeset
378 df_ref def;
kono
parents:
diff changeset
379
kono
parents:
diff changeset
380 FOR_EACH_INSN_DEF (def, insn)
kono
parents:
diff changeset
381 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
kono
parents:
diff changeset
382 }
kono
parents:
diff changeset
383
kono
parents:
diff changeset
384 /* Top level function to create an assignment hash table.
kono
parents:
diff changeset
385
kono
parents:
diff changeset
386 Assignment entries are placed in the hash table if
kono
parents:
diff changeset
387 - they are of the form (set (pseudo-reg) src),
kono
parents:
diff changeset
388 - src is something we want to perform const/copy propagation on,
kono
parents:
diff changeset
389 - none of the operands or target are subsequently modified in the block
kono
parents:
diff changeset
390
kono
parents:
diff changeset
391 Currently src must be a pseudo-reg or a const_int.
kono
parents:
diff changeset
392
kono
parents:
diff changeset
393 TABLE is the table computed. */
kono
parents:
diff changeset
394
kono
parents:
diff changeset
395 static void
kono
parents:
diff changeset
396 compute_hash_table_work (struct hash_table_d *table)
kono
parents:
diff changeset
397 {
kono
parents:
diff changeset
398 basic_block bb;
kono
parents:
diff changeset
399
kono
parents:
diff changeset
400 /* Allocate vars to track sets of regs. */
kono
parents:
diff changeset
401 reg_set_bitmap = ALLOC_REG_SET (NULL);
kono
parents:
diff changeset
402
kono
parents:
diff changeset
403 FOR_EACH_BB_FN (bb, cfun)
kono
parents:
diff changeset
404 {
kono
parents:
diff changeset
405 rtx_insn *insn;
kono
parents:
diff changeset
406
kono
parents:
diff changeset
407 /* Reset tables used to keep track of what's not yet invalid [since
kono
parents:
diff changeset
408 the end of the block]. */
kono
parents:
diff changeset
409 CLEAR_REG_SET (reg_set_bitmap);
kono
parents:
diff changeset
410
kono
parents:
diff changeset
411 /* Go over all insns from the last to the first. This is convenient
kono
parents:
diff changeset
412 for tracking available registers, i.e. not set between INSN and
kono
parents:
diff changeset
413 the end of the basic block BB. */
kono
parents:
diff changeset
414 FOR_BB_INSNS_REVERSE (bb, insn)
kono
parents:
diff changeset
415 {
kono
parents:
diff changeset
416 /* Only real insns are interesting. */
kono
parents:
diff changeset
417 if (!NONDEBUG_INSN_P (insn))
kono
parents:
diff changeset
418 continue;
kono
parents:
diff changeset
419
kono
parents:
diff changeset
420 /* Record interesting sets from INSN in the hash table. */
kono
parents:
diff changeset
421 hash_scan_insn (insn, table);
kono
parents:
diff changeset
422
kono
parents:
diff changeset
423 /* Any registers set in INSN will make SETs above it not AVAIL. */
kono
parents:
diff changeset
424 make_set_regs_unavailable (insn);
kono
parents:
diff changeset
425 }
kono
parents:
diff changeset
426
kono
parents:
diff changeset
427 /* Insert implicit sets in the hash table, pretending they appear as
kono
parents:
diff changeset
428 insns at the head of the basic block. */
kono
parents:
diff changeset
429 if (implicit_sets[bb->index] != NULL_RTX)
kono
parents:
diff changeset
430 hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table, true);
kono
parents:
diff changeset
431 }
kono
parents:
diff changeset
432
kono
parents:
diff changeset
433 FREE_REG_SET (reg_set_bitmap);
kono
parents:
diff changeset
434 }
kono
parents:
diff changeset
435
kono
parents:
diff changeset
436 /* Allocate space for the set/expr hash TABLE.
kono
parents:
diff changeset
437 It is used to determine the number of buckets to use. */
kono
parents:
diff changeset
438
kono
parents:
diff changeset
439 static void
kono
parents:
diff changeset
440 alloc_hash_table (struct hash_table_d *table)
kono
parents:
diff changeset
441 {
kono
parents:
diff changeset
442 int n;
kono
parents:
diff changeset
443
kono
parents:
diff changeset
444 n = get_max_insn_count ();
kono
parents:
diff changeset
445
kono
parents:
diff changeset
446 table->size = n / 4;
kono
parents:
diff changeset
447 if (table->size < 11)
kono
parents:
diff changeset
448 table->size = 11;
kono
parents:
diff changeset
449
kono
parents:
diff changeset
450 /* Attempt to maintain efficient use of hash table.
kono
parents:
diff changeset
451 Making it an odd number is simplest for now.
kono
parents:
diff changeset
452 ??? Later take some measurements. */
kono
parents:
diff changeset
453 table->size |= 1;
kono
parents:
diff changeset
454 n = table->size * sizeof (struct cprop_expr *);
kono
parents:
diff changeset
455 table->table = XNEWVAR (struct cprop_expr *, n);
kono
parents:
diff changeset
456 }
kono
parents:
diff changeset
457
kono
parents:
diff changeset
458 /* Free things allocated by alloc_hash_table. */
kono
parents:
diff changeset
459
kono
parents:
diff changeset
460 static void
kono
parents:
diff changeset
461 free_hash_table (struct hash_table_d *table)
kono
parents:
diff changeset
462 {
kono
parents:
diff changeset
463 free (table->table);
kono
parents:
diff changeset
464 }
kono
parents:
diff changeset
465
kono
parents:
diff changeset
466 /* Compute the hash TABLE for doing copy/const propagation or
kono
parents:
diff changeset
467 expression hash table. */
kono
parents:
diff changeset
468
kono
parents:
diff changeset
469 static void
kono
parents:
diff changeset
470 compute_hash_table (struct hash_table_d *table)
kono
parents:
diff changeset
471 {
kono
parents:
diff changeset
472 /* Initialize count of number of entries in hash table. */
kono
parents:
diff changeset
473 table->n_elems = 0;
kono
parents:
diff changeset
474 memset (table->table, 0, table->size * sizeof (struct cprop_expr *));
kono
parents:
diff changeset
475
kono
parents:
diff changeset
476 compute_hash_table_work (table);
kono
parents:
diff changeset
477 }
kono
parents:
diff changeset
478
kono
parents:
diff changeset
479 /* Expression tracking support. */
kono
parents:
diff changeset
480
kono
parents:
diff changeset
481 /* Lookup REGNO in the set TABLE. The result is a pointer to the
kono
parents:
diff changeset
482 table entry, or NULL if not found. */
kono
parents:
diff changeset
483
kono
parents:
diff changeset
484 static struct cprop_expr *
kono
parents:
diff changeset
485 lookup_set (unsigned int regno, struct hash_table_d *table)
kono
parents:
diff changeset
486 {
kono
parents:
diff changeset
487 unsigned int hash = hash_mod (regno, table->size);
kono
parents:
diff changeset
488 struct cprop_expr *expr;
kono
parents:
diff changeset
489
kono
parents:
diff changeset
490 expr = table->table[hash];
kono
parents:
diff changeset
491
kono
parents:
diff changeset
492 while (expr && REGNO (expr->dest) != regno)
kono
parents:
diff changeset
493 expr = expr->next_same_hash;
kono
parents:
diff changeset
494
kono
parents:
diff changeset
495 return expr;
kono
parents:
diff changeset
496 }
kono
parents:
diff changeset
497
kono
parents:
diff changeset
498 /* Return the next entry for REGNO in list EXPR. */
kono
parents:
diff changeset
499
kono
parents:
diff changeset
500 static struct cprop_expr *
kono
parents:
diff changeset
501 next_set (unsigned int regno, struct cprop_expr *expr)
kono
parents:
diff changeset
502 {
kono
parents:
diff changeset
503 do
kono
parents:
diff changeset
504 expr = expr->next_same_hash;
kono
parents:
diff changeset
505 while (expr && REGNO (expr->dest) != regno);
kono
parents:
diff changeset
506
kono
parents:
diff changeset
507 return expr;
kono
parents:
diff changeset
508 }
kono
parents:
diff changeset
509
kono
parents:
diff changeset
510 /* Reset tables used to keep track of what's still available [since the
kono
parents:
diff changeset
511 start of the block]. */
kono
parents:
diff changeset
512
kono
parents:
diff changeset
513 static void
kono
parents:
diff changeset
514 reset_opr_set_tables (void)
kono
parents:
diff changeset
515 {
kono
parents:
diff changeset
516 /* Maintain a bitmap of which regs have been set since beginning of
kono
parents:
diff changeset
517 the block. */
kono
parents:
diff changeset
518 CLEAR_REG_SET (reg_set_bitmap);
kono
parents:
diff changeset
519 }
kono
parents:
diff changeset
520
kono
parents:
diff changeset
521 /* Return nonzero if the register X has not been set yet [since the
kono
parents:
diff changeset
522 start of the basic block containing INSN]. */
kono
parents:
diff changeset
523
kono
parents:
diff changeset
524 static int
kono
parents:
diff changeset
525 reg_not_set_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
526 {
kono
parents:
diff changeset
527 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
kono
parents:
diff changeset
528 }
kono
parents:
diff changeset
529
kono
parents:
diff changeset
530 /* Record things set by INSN.
kono
parents:
diff changeset
531 This data is used by reg_not_set_p. */
kono
parents:
diff changeset
532
kono
parents:
diff changeset
533 static void
kono
parents:
diff changeset
534 mark_oprs_set (rtx_insn *insn)
kono
parents:
diff changeset
535 {
kono
parents:
diff changeset
536 df_ref def;
kono
parents:
diff changeset
537
kono
parents:
diff changeset
538 FOR_EACH_INSN_DEF (def, insn)
kono
parents:
diff changeset
539 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
kono
parents:
diff changeset
540 }
kono
parents:
diff changeset
541
kono
parents:
diff changeset
542 /* Compute copy/constant propagation working variables. */
kono
parents:
diff changeset
543
kono
parents:
diff changeset
544 /* Local properties of assignments. */
kono
parents:
diff changeset
545 static sbitmap *cprop_avloc;
kono
parents:
diff changeset
546 static sbitmap *cprop_kill;
kono
parents:
diff changeset
547
kono
parents:
diff changeset
548 /* Global properties of assignments (computed from the local properties). */
kono
parents:
diff changeset
549 static sbitmap *cprop_avin;
kono
parents:
diff changeset
550 static sbitmap *cprop_avout;
kono
parents:
diff changeset
551
kono
parents:
diff changeset
552 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
kono
parents:
diff changeset
553 basic blocks. N_SETS is the number of sets. */
kono
parents:
diff changeset
554
kono
parents:
diff changeset
555 static void
kono
parents:
diff changeset
556 alloc_cprop_mem (int n_blocks, int n_sets)
kono
parents:
diff changeset
557 {
kono
parents:
diff changeset
558 cprop_avloc = sbitmap_vector_alloc (n_blocks, n_sets);
kono
parents:
diff changeset
559 cprop_kill = sbitmap_vector_alloc (n_blocks, n_sets);
kono
parents:
diff changeset
560
kono
parents:
diff changeset
561 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
kono
parents:
diff changeset
562 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
kono
parents:
diff changeset
563 }
kono
parents:
diff changeset
564
kono
parents:
diff changeset
565 /* Free vars used by copy/const propagation. */
kono
parents:
diff changeset
566
kono
parents:
diff changeset
567 static void
kono
parents:
diff changeset
568 free_cprop_mem (void)
kono
parents:
diff changeset
569 {
kono
parents:
diff changeset
570 sbitmap_vector_free (cprop_avloc);
kono
parents:
diff changeset
571 sbitmap_vector_free (cprop_kill);
kono
parents:
diff changeset
572 sbitmap_vector_free (cprop_avin);
kono
parents:
diff changeset
573 sbitmap_vector_free (cprop_avout);
kono
parents:
diff changeset
574 }
kono
parents:
diff changeset
575
kono
parents:
diff changeset
576 /* Compute the local properties of each recorded expression.
kono
parents:
diff changeset
577
kono
parents:
diff changeset
578 Local properties are those that are defined by the block, irrespective of
kono
parents:
diff changeset
579 other blocks.
kono
parents:
diff changeset
580
kono
parents:
diff changeset
581 An expression is killed in a block if its operands, either DEST or SRC, are
kono
parents:
diff changeset
582 modified in the block.
kono
parents:
diff changeset
583
kono
parents:
diff changeset
584 An expression is computed (locally available) in a block if it is computed
kono
parents:
diff changeset
585 at least once and expression would contain the same value if the
kono
parents:
diff changeset
586 computation was moved to the end of the block.
kono
parents:
diff changeset
587
kono
parents:
diff changeset
588 KILL and COMP are destination sbitmaps for recording local properties. */
kono
parents:
diff changeset
589
kono
parents:
diff changeset
590 static void
kono
parents:
diff changeset
591 compute_local_properties (sbitmap *kill, sbitmap *comp,
kono
parents:
diff changeset
592 struct hash_table_d *table)
kono
parents:
diff changeset
593 {
kono
parents:
diff changeset
594 unsigned int i;
kono
parents:
diff changeset
595
kono
parents:
diff changeset
596 /* Initialize the bitmaps that were passed in. */
kono
parents:
diff changeset
597 bitmap_vector_clear (kill, last_basic_block_for_fn (cfun));
kono
parents:
diff changeset
598 bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
kono
parents:
diff changeset
599
kono
parents:
diff changeset
600 for (i = 0; i < table->size; i++)
kono
parents:
diff changeset
601 {
kono
parents:
diff changeset
602 struct cprop_expr *expr;
kono
parents:
diff changeset
603
kono
parents:
diff changeset
604 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
kono
parents:
diff changeset
605 {
kono
parents:
diff changeset
606 int indx = expr->bitmap_index;
kono
parents:
diff changeset
607 df_ref def;
kono
parents:
diff changeset
608 struct cprop_occr *occr;
kono
parents:
diff changeset
609
kono
parents:
diff changeset
610 /* For each definition of the destination pseudo-reg, the expression
kono
parents:
diff changeset
611 is killed in the block where the definition is. */
kono
parents:
diff changeset
612 for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest));
kono
parents:
diff changeset
613 def; def = DF_REF_NEXT_REG (def))
kono
parents:
diff changeset
614 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
kono
parents:
diff changeset
615
kono
parents:
diff changeset
616 /* If the source is a pseudo-reg, for each definition of the source,
kono
parents:
diff changeset
617 the expression is killed in the block where the definition is. */
kono
parents:
diff changeset
618 if (REG_P (expr->src))
kono
parents:
diff changeset
619 for (def = DF_REG_DEF_CHAIN (REGNO (expr->src));
kono
parents:
diff changeset
620 def; def = DF_REF_NEXT_REG (def))
kono
parents:
diff changeset
621 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
kono
parents:
diff changeset
622
kono
parents:
diff changeset
623 /* The occurrences recorded in avail_occr are exactly those that
kono
parents:
diff changeset
624 are locally available in the block where they are. */
kono
parents:
diff changeset
625 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
kono
parents:
diff changeset
626 {
kono
parents:
diff changeset
627 bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
kono
parents:
diff changeset
628 }
kono
parents:
diff changeset
629 }
kono
parents:
diff changeset
630 }
kono
parents:
diff changeset
631 }
kono
parents:
diff changeset
632
kono
parents:
diff changeset
633 /* Hash table support. */
kono
parents:
diff changeset
634
kono
parents:
diff changeset
635 /* Top level routine to do the dataflow analysis needed by copy/const
kono
parents:
diff changeset
636 propagation. */
kono
parents:
diff changeset
637
kono
parents:
diff changeset
638 static void
kono
parents:
diff changeset
639 compute_cprop_data (void)
kono
parents:
diff changeset
640 {
kono
parents:
diff changeset
641 basic_block bb;
kono
parents:
diff changeset
642
kono
parents:
diff changeset
643 compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table);
kono
parents:
diff changeset
644 compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin);
kono
parents:
diff changeset
645
kono
parents:
diff changeset
646 /* Merge implicit sets into CPROP_AVIN. They are always available at the
kono
parents:
diff changeset
647 entry of their basic block. We need to do this because 1) implicit sets
kono
parents:
diff changeset
648 aren't recorded for the local pass so they cannot be propagated within
kono
parents:
diff changeset
649 their basic block by this pass and 2) the global pass would otherwise
kono
parents:
diff changeset
650 propagate them only in the successors of their basic block. */
kono
parents:
diff changeset
651 FOR_EACH_BB_FN (bb, cfun)
kono
parents:
diff changeset
652 {
kono
parents:
diff changeset
653 int index = implicit_set_indexes[bb->index];
kono
parents:
diff changeset
654 if (index != -1)
kono
parents:
diff changeset
655 bitmap_set_bit (cprop_avin[bb->index], index);
kono
parents:
diff changeset
656 }
kono
parents:
diff changeset
657 }
kono
parents:
diff changeset
658
kono
parents:
diff changeset
659 /* Copy/constant propagation. */
kono
parents:
diff changeset
660
kono
parents:
diff changeset
661 /* Maximum number of register uses in an insn that we handle. */
kono
parents:
diff changeset
662 #define MAX_USES 8
kono
parents:
diff changeset
663
kono
parents:
diff changeset
664 /* Table of uses (registers, both hard and pseudo) found in an insn.
kono
parents:
diff changeset
665 Allocated statically to avoid alloc/free complexity and overhead. */
kono
parents:
diff changeset
666 static rtx reg_use_table[MAX_USES];
kono
parents:
diff changeset
667
kono
parents:
diff changeset
668 /* Index into `reg_use_table' while building it. */
kono
parents:
diff changeset
669 static unsigned reg_use_count;
kono
parents:
diff changeset
670
kono
parents:
diff changeset
671 /* Set up a list of register numbers used in INSN. The found uses are stored
kono
parents:
diff changeset
672 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
kono
parents:
diff changeset
673 and contains the number of uses in the table upon exit.
kono
parents:
diff changeset
674
kono
parents:
diff changeset
675 ??? If a register appears multiple times we will record it multiple times.
kono
parents:
diff changeset
676 This doesn't hurt anything but it will slow things down. */
kono
parents:
diff changeset
677
kono
parents:
diff changeset
678 static void
kono
parents:
diff changeset
679 find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
680 {
kono
parents:
diff changeset
681 int i, j;
kono
parents:
diff changeset
682 enum rtx_code code;
kono
parents:
diff changeset
683 const char *fmt;
kono
parents:
diff changeset
684 rtx x = *xptr;
kono
parents:
diff changeset
685
kono
parents:
diff changeset
686 /* repeat is used to turn tail-recursion into iteration since GCC
kono
parents:
diff changeset
687 can't do it when there's no return value. */
kono
parents:
diff changeset
688 repeat:
kono
parents:
diff changeset
689 if (x == 0)
kono
parents:
diff changeset
690 return;
kono
parents:
diff changeset
691
kono
parents:
diff changeset
692 code = GET_CODE (x);
kono
parents:
diff changeset
693 if (REG_P (x))
kono
parents:
diff changeset
694 {
kono
parents:
diff changeset
695 if (reg_use_count == MAX_USES)
kono
parents:
diff changeset
696 return;
kono
parents:
diff changeset
697
kono
parents:
diff changeset
698 reg_use_table[reg_use_count] = x;
kono
parents:
diff changeset
699 reg_use_count++;
kono
parents:
diff changeset
700 }
kono
parents:
diff changeset
701
kono
parents:
diff changeset
702 /* Recursively scan the operands of this expression. */
kono
parents:
diff changeset
703
kono
parents:
diff changeset
704 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
kono
parents:
diff changeset
705 {
kono
parents:
diff changeset
706 if (fmt[i] == 'e')
kono
parents:
diff changeset
707 {
kono
parents:
diff changeset
708 /* If we are about to do the last recursive call
kono
parents:
diff changeset
709 needed at this level, change it into iteration.
kono
parents:
diff changeset
710 This function is called enough to be worth it. */
kono
parents:
diff changeset
711 if (i == 0)
kono
parents:
diff changeset
712 {
kono
parents:
diff changeset
713 x = XEXP (x, 0);
kono
parents:
diff changeset
714 goto repeat;
kono
parents:
diff changeset
715 }
kono
parents:
diff changeset
716
kono
parents:
diff changeset
717 find_used_regs (&XEXP (x, i), data);
kono
parents:
diff changeset
718 }
kono
parents:
diff changeset
719 else if (fmt[i] == 'E')
kono
parents:
diff changeset
720 for (j = 0; j < XVECLEN (x, i); j++)
kono
parents:
diff changeset
721 find_used_regs (&XVECEXP (x, i, j), data);
kono
parents:
diff changeset
722 }
kono
parents:
diff changeset
723 }
kono
parents:
diff changeset
724
kono
parents:
diff changeset
725 /* Try to replace all uses of FROM in INSN with TO.
kono
parents:
diff changeset
726 Return nonzero if successful. */
kono
parents:
diff changeset
727
kono
parents:
diff changeset
728 static int
kono
parents:
diff changeset
729 try_replace_reg (rtx from, rtx to, rtx_insn *insn)
kono
parents:
diff changeset
730 {
kono
parents:
diff changeset
731 rtx note = find_reg_equal_equiv_note (insn);
kono
parents:
diff changeset
732 rtx src = 0;
kono
parents:
diff changeset
733 int success = 0;
kono
parents:
diff changeset
734 rtx set = single_set (insn);
kono
parents:
diff changeset
735
kono
parents:
diff changeset
736 bool check_rtx_costs = true;
kono
parents:
diff changeset
737 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
kono
parents:
diff changeset
738 int old_cost = set ? set_rtx_cost (set, speed) : 0;
kono
parents:
diff changeset
739
kono
parents:
diff changeset
740 if (!set
kono
parents:
diff changeset
741 || CONSTANT_P (SET_SRC (set))
kono
parents:
diff changeset
742 || (note != 0
kono
parents:
diff changeset
743 && REG_NOTE_KIND (note) == REG_EQUAL
kono
parents:
diff changeset
744 && (GET_CODE (XEXP (note, 0)) == CONST
kono
parents:
diff changeset
745 || CONSTANT_P (XEXP (note, 0)))))
kono
parents:
diff changeset
746 check_rtx_costs = false;
kono
parents:
diff changeset
747
kono
parents:
diff changeset
748 /* Usually we substitute easy stuff, so we won't copy everything.
kono
parents:
diff changeset
749 We however need to take care to not duplicate non-trivial CONST
kono
parents:
diff changeset
750 expressions. */
kono
parents:
diff changeset
751 to = copy_rtx (to);
kono
parents:
diff changeset
752
kono
parents:
diff changeset
753 validate_replace_src_group (from, to, insn);
kono
parents:
diff changeset
754
kono
parents:
diff changeset
755 /* If TO is a constant, check the cost of the set after propagation
kono
parents:
diff changeset
756 to the cost of the set before the propagation. If the cost is
kono
parents:
diff changeset
757 higher, then do not replace FROM with TO. */
kono
parents:
diff changeset
758
kono
parents:
diff changeset
759 if (check_rtx_costs
kono
parents:
diff changeset
760 && CONSTANT_P (to)
kono
parents:
diff changeset
761 && set_rtx_cost (set, speed) > old_cost)
kono
parents:
diff changeset
762 {
kono
parents:
diff changeset
763 cancel_changes (0);
kono
parents:
diff changeset
764 return false;
kono
parents:
diff changeset
765 }
kono
parents:
diff changeset
766
kono
parents:
diff changeset
767
kono
parents:
diff changeset
768 if (num_changes_pending () && apply_change_group ())
kono
parents:
diff changeset
769 success = 1;
kono
parents:
diff changeset
770
kono
parents:
diff changeset
771 /* Try to simplify SET_SRC if we have substituted a constant. */
kono
parents:
diff changeset
772 if (success && set && CONSTANT_P (to))
kono
parents:
diff changeset
773 {
kono
parents:
diff changeset
774 src = simplify_rtx (SET_SRC (set));
kono
parents:
diff changeset
775
kono
parents:
diff changeset
776 if (src)
kono
parents:
diff changeset
777 validate_change (insn, &SET_SRC (set), src, 0);
kono
parents:
diff changeset
778 }
kono
parents:
diff changeset
779
kono
parents:
diff changeset
780 /* If there is already a REG_EQUAL note, update the expression in it
kono
parents:
diff changeset
781 with our replacement. */
kono
parents:
diff changeset
782 if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
kono
parents:
diff changeset
783 set_unique_reg_note (insn, REG_EQUAL,
kono
parents:
diff changeset
784 simplify_replace_rtx (XEXP (note, 0), from, to));
kono
parents:
diff changeset
785 if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
kono
parents:
diff changeset
786 {
kono
parents:
diff changeset
787 /* If above failed and this is a single set, try to simplify the source
kono
parents:
diff changeset
788 of the set given our substitution. We could perhaps try this for
kono
parents:
diff changeset
789 multiple SETs, but it probably won't buy us anything. */
kono
parents:
diff changeset
790 src = simplify_replace_rtx (SET_SRC (set), from, to);
kono
parents:
diff changeset
791
kono
parents:
diff changeset
792 if (!rtx_equal_p (src, SET_SRC (set))
kono
parents:
diff changeset
793 && validate_change (insn, &SET_SRC (set), src, 0))
kono
parents:
diff changeset
794 success = 1;
kono
parents:
diff changeset
795
kono
parents:
diff changeset
796 /* If we've failed perform the replacement, have a single SET to
kono
parents:
diff changeset
797 a REG destination and don't yet have a note, add a REG_EQUAL note
kono
parents:
diff changeset
798 to not lose information. */
kono
parents:
diff changeset
799 if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set)))
kono
parents:
diff changeset
800 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
kono
parents:
diff changeset
801 }
kono
parents:
diff changeset
802
kono
parents:
diff changeset
803 if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set)))
kono
parents:
diff changeset
804 {
kono
parents:
diff changeset
805 /* Registers can also appear as uses in SET_DEST if it is a MEM.
kono
parents:
diff changeset
806 We could perhaps try this for multiple SETs, but it probably
kono
parents:
diff changeset
807 won't buy us anything. */
kono
parents:
diff changeset
808 rtx dest = simplify_replace_rtx (SET_DEST (set), from, to);
kono
parents:
diff changeset
809
kono
parents:
diff changeset
810 if (!rtx_equal_p (dest, SET_DEST (set))
kono
parents:
diff changeset
811 && validate_change (insn, &SET_DEST (set), dest, 0))
kono
parents:
diff changeset
812 success = 1;
kono
parents:
diff changeset
813 }
kono
parents:
diff changeset
814
kono
parents:
diff changeset
815 /* REG_EQUAL may get simplified into register.
kono
parents:
diff changeset
816 We don't allow that. Remove that note. This code ought
kono
parents:
diff changeset
817 not to happen, because previous code ought to synthesize
kono
parents:
diff changeset
818 reg-reg move, but be on the safe side. */
kono
parents:
diff changeset
819 if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
kono
parents:
diff changeset
820 remove_note (insn, note);
kono
parents:
diff changeset
821
kono
parents:
diff changeset
822 return success;
kono
parents:
diff changeset
823 }
kono
parents:
diff changeset
824
kono
parents:
diff changeset
825 /* Find a set of REGNOs that are available on entry to INSN's block. If found,
kono
parents:
diff changeset
826 SET_RET[0] will be assigned a set with a register source and SET_RET[1] a
kono
parents:
diff changeset
827 set with a constant source. If not found the corresponding entry is set to
kono
parents:
diff changeset
828 NULL. */
kono
parents:
diff changeset
829
kono
parents:
diff changeset
830 static void
kono
parents:
diff changeset
831 find_avail_set (int regno, rtx_insn *insn, struct cprop_expr *set_ret[2])
kono
parents:
diff changeset
832 {
kono
parents:
diff changeset
833 set_ret[0] = set_ret[1] = NULL;
kono
parents:
diff changeset
834
kono
parents:
diff changeset
835 /* Loops are not possible here. To get a loop we would need two sets
kono
parents:
diff changeset
836 available at the start of the block containing INSN. i.e. we would
kono
parents:
diff changeset
837 need two sets like this available at the start of the block:
kono
parents:
diff changeset
838
kono
parents:
diff changeset
839 (set (reg X) (reg Y))
kono
parents:
diff changeset
840 (set (reg Y) (reg X))
kono
parents:
diff changeset
841
kono
parents:
diff changeset
842 This can not happen since the set of (reg Y) would have killed the
kono
parents:
diff changeset
843 set of (reg X) making it unavailable at the start of this block. */
kono
parents:
diff changeset
844 while (1)
kono
parents:
diff changeset
845 {
kono
parents:
diff changeset
846 rtx src;
kono
parents:
diff changeset
847 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
kono
parents:
diff changeset
848
kono
parents:
diff changeset
849 /* Find a set that is available at the start of the block
kono
parents:
diff changeset
850 which contains INSN. */
kono
parents:
diff changeset
851 while (set)
kono
parents:
diff changeset
852 {
kono
parents:
diff changeset
853 if (bitmap_bit_p (cprop_avin[BLOCK_FOR_INSN (insn)->index],
kono
parents:
diff changeset
854 set->bitmap_index))
kono
parents:
diff changeset
855 break;
kono
parents:
diff changeset
856 set = next_set (regno, set);
kono
parents:
diff changeset
857 }
kono
parents:
diff changeset
858
kono
parents:
diff changeset
859 /* If no available set was found we've reached the end of the
kono
parents:
diff changeset
860 (possibly empty) copy chain. */
kono
parents:
diff changeset
861 if (set == 0)
kono
parents:
diff changeset
862 break;
kono
parents:
diff changeset
863
kono
parents:
diff changeset
864 src = set->src;
kono
parents:
diff changeset
865
kono
parents:
diff changeset
866 /* We know the set is available.
kono
parents:
diff changeset
867 Now check that SRC is locally anticipatable (i.e. none of the
kono
parents:
diff changeset
868 source operands have changed since the start of the block).
kono
parents:
diff changeset
869
kono
parents:
diff changeset
870 If the source operand changed, we may still use it for the next
kono
parents:
diff changeset
871 iteration of this loop, but we may not use it for substitutions. */
kono
parents:
diff changeset
872
kono
parents:
diff changeset
873 if (cprop_constant_p (src))
kono
parents:
diff changeset
874 set_ret[1] = set;
kono
parents:
diff changeset
875 else if (reg_not_set_p (src, insn))
kono
parents:
diff changeset
876 set_ret[0] = set;
kono
parents:
diff changeset
877
kono
parents:
diff changeset
878 /* If the source of the set is anything except a register, then
kono
parents:
diff changeset
879 we have reached the end of the copy chain. */
kono
parents:
diff changeset
880 if (! REG_P (src))
kono
parents:
diff changeset
881 break;
kono
parents:
diff changeset
882
kono
parents:
diff changeset
883 /* Follow the copy chain, i.e. start another iteration of the loop
kono
parents:
diff changeset
884 and see if we have an available copy into SRC. */
kono
parents:
diff changeset
885 regno = REGNO (src);
kono
parents:
diff changeset
886 }
kono
parents:
diff changeset
887 }
kono
parents:
diff changeset
888
kono
parents:
diff changeset
889 /* Subroutine of cprop_insn that tries to propagate constants into
kono
parents:
diff changeset
890 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
kono
parents:
diff changeset
891 it is the instruction that immediately precedes JUMP, and must be a
kono
parents:
diff changeset
892 single SET of a register. FROM is what we will try to replace,
kono
parents:
diff changeset
893 SRC is the constant we will try to substitute for it. Return nonzero
kono
parents:
diff changeset
894 if a change was made. */
kono
parents:
diff changeset
895
kono
parents:
diff changeset
896 static int
kono
parents:
diff changeset
897 cprop_jump (basic_block bb, rtx_insn *setcc, rtx_insn *jump, rtx from, rtx src)
kono
parents:
diff changeset
898 {
kono
parents:
diff changeset
899 rtx new_rtx, set_src, note_src;
kono
parents:
diff changeset
900 rtx set = pc_set (jump);
kono
parents:
diff changeset
901 rtx note = find_reg_equal_equiv_note (jump);
kono
parents:
diff changeset
902
kono
parents:
diff changeset
903 if (note)
kono
parents:
diff changeset
904 {
kono
parents:
diff changeset
905 note_src = XEXP (note, 0);
kono
parents:
diff changeset
906 if (GET_CODE (note_src) == EXPR_LIST)
kono
parents:
diff changeset
907 note_src = NULL_RTX;
kono
parents:
diff changeset
908 }
kono
parents:
diff changeset
909 else note_src = NULL_RTX;
kono
parents:
diff changeset
910
kono
parents:
diff changeset
911 /* Prefer REG_EQUAL notes except those containing EXPR_LISTs. */
kono
parents:
diff changeset
912 set_src = note_src ? note_src : SET_SRC (set);
kono
parents:
diff changeset
913
kono
parents:
diff changeset
914 /* First substitute the SETCC condition into the JUMP instruction,
kono
parents:
diff changeset
915 then substitute that given values into this expanded JUMP. */
kono
parents:
diff changeset
916 if (setcc != NULL_RTX
kono
parents:
diff changeset
917 && !modified_between_p (from, setcc, jump)
kono
parents:
diff changeset
918 && !modified_between_p (src, setcc, jump))
kono
parents:
diff changeset
919 {
kono
parents:
diff changeset
920 rtx setcc_src;
kono
parents:
diff changeset
921 rtx setcc_set = single_set (setcc);
kono
parents:
diff changeset
922 rtx setcc_note = find_reg_equal_equiv_note (setcc);
kono
parents:
diff changeset
923 setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
kono
parents:
diff changeset
924 ? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
kono
parents:
diff changeset
925 set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
kono
parents:
diff changeset
926 setcc_src);
kono
parents:
diff changeset
927 }
kono
parents:
diff changeset
928 else
kono
parents:
diff changeset
929 setcc = NULL;
kono
parents:
diff changeset
930
kono
parents:
diff changeset
931 new_rtx = simplify_replace_rtx (set_src, from, src);
kono
parents:
diff changeset
932
kono
parents:
diff changeset
933 /* If no simplification can be made, then try the next register. */
kono
parents:
diff changeset
934 if (rtx_equal_p (new_rtx, SET_SRC (set)))
kono
parents:
diff changeset
935 return 0;
kono
parents:
diff changeset
936
kono
parents:
diff changeset
937 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
kono
parents:
diff changeset
938 if (new_rtx == pc_rtx)
kono
parents:
diff changeset
939 delete_insn (jump);
kono
parents:
diff changeset
940 else
kono
parents:
diff changeset
941 {
kono
parents:
diff changeset
942 /* Ensure the value computed inside the jump insn to be equivalent
kono
parents:
diff changeset
943 to one computed by setcc. */
kono
parents:
diff changeset
944 if (setcc && modified_in_p (new_rtx, setcc))
kono
parents:
diff changeset
945 return 0;
kono
parents:
diff changeset
946 if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
kono
parents:
diff changeset
947 {
kono
parents:
diff changeset
948 /* When (some) constants are not valid in a comparison, and there
kono
parents:
diff changeset
949 are two registers to be replaced by constants before the entire
kono
parents:
diff changeset
950 comparison can be folded into a constant, we need to keep
kono
parents:
diff changeset
951 intermediate information in REG_EQUAL notes. For targets with
kono
parents:
diff changeset
952 separate compare insns, such notes are added by try_replace_reg.
kono
parents:
diff changeset
953 When we have a combined compare-and-branch instruction, however,
kono
parents:
diff changeset
954 we need to attach a note to the branch itself to make this
kono
parents:
diff changeset
955 optimization work. */
kono
parents:
diff changeset
956
kono
parents:
diff changeset
957 if (!rtx_equal_p (new_rtx, note_src))
kono
parents:
diff changeset
958 set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
kono
parents:
diff changeset
959 return 0;
kono
parents:
diff changeset
960 }
kono
parents:
diff changeset
961
kono
parents:
diff changeset
962 /* Remove REG_EQUAL note after simplification. */
kono
parents:
diff changeset
963 if (note_src)
kono
parents:
diff changeset
964 remove_note (jump, note);
kono
parents:
diff changeset
965 }
kono
parents:
diff changeset
966
kono
parents:
diff changeset
967 /* Delete the cc0 setter. */
kono
parents:
diff changeset
968 if (HAVE_cc0 && setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
kono
parents:
diff changeset
969 delete_insn (setcc);
kono
parents:
diff changeset
970
kono
parents:
diff changeset
971 global_const_prop_count++;
kono
parents:
diff changeset
972 if (dump_file != NULL)
kono
parents:
diff changeset
973 {
kono
parents:
diff changeset
974 fprintf (dump_file,
kono
parents:
diff changeset
975 "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with "
kono
parents:
diff changeset
976 "constant ", REGNO (from), INSN_UID (jump));
kono
parents:
diff changeset
977 print_rtl (dump_file, src);
kono
parents:
diff changeset
978 fprintf (dump_file, "\n");
kono
parents:
diff changeset
979 }
kono
parents:
diff changeset
980 purge_dead_edges (bb);
kono
parents:
diff changeset
981
kono
parents:
diff changeset
982 /* If a conditional jump has been changed into unconditional jump, remove
kono
parents:
diff changeset
983 the jump and make the edge fallthru - this is always called in
kono
parents:
diff changeset
984 cfglayout mode. */
kono
parents:
diff changeset
985 if (new_rtx != pc_rtx && simplejump_p (jump))
kono
parents:
diff changeset
986 {
kono
parents:
diff changeset
987 edge e;
kono
parents:
diff changeset
988 edge_iterator ei;
kono
parents:
diff changeset
989
kono
parents:
diff changeset
990 FOR_EACH_EDGE (e, ei, bb->succs)
kono
parents:
diff changeset
991 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
kono
parents:
diff changeset
992 && BB_HEAD (e->dest) == JUMP_LABEL (jump))
kono
parents:
diff changeset
993 {
kono
parents:
diff changeset
994 e->flags |= EDGE_FALLTHRU;
kono
parents:
diff changeset
995 break;
kono
parents:
diff changeset
996 }
kono
parents:
diff changeset
997 delete_insn (jump);
kono
parents:
diff changeset
998 }
kono
parents:
diff changeset
999
kono
parents:
diff changeset
1000 return 1;
kono
parents:
diff changeset
1001 }
kono
parents:
diff changeset
1002
kono
parents:
diff changeset
1003 /* Subroutine of cprop_insn that tries to propagate constants. FROM is what
kono
parents:
diff changeset
1004 we will try to replace, SRC is the constant we will try to substitute for
kono
parents:
diff changeset
1005 it and INSN is the instruction where this will be happening. */
kono
parents:
diff changeset
1006
kono
parents:
diff changeset
1007 static int
kono
parents:
diff changeset
1008 constprop_register (rtx from, rtx src, rtx_insn *insn)
kono
parents:
diff changeset
1009 {
kono
parents:
diff changeset
1010 rtx sset;
kono
parents:
diff changeset
1011
kono
parents:
diff changeset
1012 /* Check for reg or cc0 setting instructions followed by
kono
parents:
diff changeset
1013 conditional branch instructions first. */
kono
parents:
diff changeset
1014 if ((sset = single_set (insn)) != NULL
kono
parents:
diff changeset
1015 && NEXT_INSN (insn)
kono
parents:
diff changeset
1016 && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
kono
parents:
diff changeset
1017 {
kono
parents:
diff changeset
1018 rtx dest = SET_DEST (sset);
kono
parents:
diff changeset
1019 if ((REG_P (dest) || CC0_P (dest))
kono
parents:
diff changeset
1020 && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn),
kono
parents:
diff changeset
1021 from, src))
kono
parents:
diff changeset
1022 return 1;
kono
parents:
diff changeset
1023 }
kono
parents:
diff changeset
1024
kono
parents:
diff changeset
1025 /* Handle normal insns next. */
kono
parents:
diff changeset
1026 if (NONJUMP_INSN_P (insn) && try_replace_reg (from, src, insn))
kono
parents:
diff changeset
1027 return 1;
kono
parents:
diff changeset
1028
kono
parents:
diff changeset
1029 /* Try to propagate a CONST_INT into a conditional jump.
kono
parents:
diff changeset
1030 We're pretty specific about what we will handle in this
kono
parents:
diff changeset
1031 code, we can extend this as necessary over time.
kono
parents:
diff changeset
1032
kono
parents:
diff changeset
1033 Right now the insn in question must look like
kono
parents:
diff changeset
1034 (set (pc) (if_then_else ...)) */
kono
parents:
diff changeset
1035 else if (any_condjump_p (insn) && onlyjump_p (insn))
kono
parents:
diff changeset
1036 return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, src);
kono
parents:
diff changeset
1037 return 0;
kono
parents:
diff changeset
1038 }
kono
parents:
diff changeset
1039
kono
parents:
diff changeset
1040 /* Perform constant and copy propagation on INSN.
kono
parents:
diff changeset
1041 Return nonzero if a change was made. */
kono
parents:
diff changeset
1042
kono
parents:
diff changeset
1043 static int
kono
parents:
diff changeset
1044 cprop_insn (rtx_insn *insn)
kono
parents:
diff changeset
1045 {
kono
parents:
diff changeset
1046 unsigned i;
kono
parents:
diff changeset
1047 int changed = 0, changed_this_round;
kono
parents:
diff changeset
1048 rtx note;
kono
parents:
diff changeset
1049
kono
parents:
diff changeset
1050 do
kono
parents:
diff changeset
1051 {
kono
parents:
diff changeset
1052 changed_this_round = 0;
kono
parents:
diff changeset
1053 reg_use_count = 0;
kono
parents:
diff changeset
1054 note_uses (&PATTERN (insn), find_used_regs, NULL);
kono
parents:
diff changeset
1055
kono
parents:
diff changeset
1056 /* We may win even when propagating constants into notes. */
kono
parents:
diff changeset
1057 note = find_reg_equal_equiv_note (insn);
kono
parents:
diff changeset
1058 if (note)
kono
parents:
diff changeset
1059 find_used_regs (&XEXP (note, 0), NULL);
kono
parents:
diff changeset
1060
kono
parents:
diff changeset
1061 for (i = 0; i < reg_use_count; i++)
kono
parents:
diff changeset
1062 {
kono
parents:
diff changeset
1063 rtx reg_used = reg_use_table[i];
kono
parents:
diff changeset
1064 unsigned int regno = REGNO (reg_used);
kono
parents:
diff changeset
1065 rtx src_cst = NULL, src_reg = NULL;
kono
parents:
diff changeset
1066 struct cprop_expr *set[2];
kono
parents:
diff changeset
1067
kono
parents:
diff changeset
1068 /* If the register has already been set in this block, there's
kono
parents:
diff changeset
1069 nothing we can do. */
kono
parents:
diff changeset
1070 if (! reg_not_set_p (reg_used, insn))
kono
parents:
diff changeset
1071 continue;
kono
parents:
diff changeset
1072
kono
parents:
diff changeset
1073 /* Find an assignment that sets reg_used and is available
kono
parents:
diff changeset
1074 at the start of the block. */
kono
parents:
diff changeset
1075 find_avail_set (regno, insn, set);
kono
parents:
diff changeset
1076 if (set[0])
kono
parents:
diff changeset
1077 src_reg = set[0]->src;
kono
parents:
diff changeset
1078 if (set[1])
kono
parents:
diff changeset
1079 src_cst = set[1]->src;
kono
parents:
diff changeset
1080
kono
parents:
diff changeset
1081 /* Constant propagation. */
kono
parents:
diff changeset
1082 if (src_cst && cprop_constant_p (src_cst)
kono
parents:
diff changeset
1083 && constprop_register (reg_used, src_cst, insn))
kono
parents:
diff changeset
1084 {
kono
parents:
diff changeset
1085 changed_this_round = changed = 1;
kono
parents:
diff changeset
1086 global_const_prop_count++;
kono
parents:
diff changeset
1087 if (dump_file != NULL)
kono
parents:
diff changeset
1088 {
kono
parents:
diff changeset
1089 fprintf (dump_file,
kono
parents:
diff changeset
1090 "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
kono
parents:
diff changeset
1091 fprintf (dump_file, "insn %d with constant ",
kono
parents:
diff changeset
1092 INSN_UID (insn));
kono
parents:
diff changeset
1093 print_rtl (dump_file, src_cst);
kono
parents:
diff changeset
1094 fprintf (dump_file, "\n");
kono
parents:
diff changeset
1095 }
kono
parents:
diff changeset
1096 if (insn->deleted ())
kono
parents:
diff changeset
1097 return 1;
kono
parents:
diff changeset
1098 }
kono
parents:
diff changeset
1099 /* Copy propagation. */
kono
parents:
diff changeset
1100 else if (src_reg && cprop_reg_p (src_reg)
kono
parents:
diff changeset
1101 && REGNO (src_reg) != regno
kono
parents:
diff changeset
1102 && try_replace_reg (reg_used, src_reg, insn))
kono
parents:
diff changeset
1103 {
kono
parents:
diff changeset
1104 changed_this_round = changed = 1;
kono
parents:
diff changeset
1105 global_copy_prop_count++;
kono
parents:
diff changeset
1106 if (dump_file != NULL)
kono
parents:
diff changeset
1107 {
kono
parents:
diff changeset
1108 fprintf (dump_file,
kono
parents:
diff changeset
1109 "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
kono
parents:
diff changeset
1110 regno, INSN_UID (insn));
kono
parents:
diff changeset
1111 fprintf (dump_file, " with reg %d\n", REGNO (src_reg));
kono
parents:
diff changeset
1112 }
kono
parents:
diff changeset
1113
kono
parents:
diff changeset
1114 /* The original insn setting reg_used may or may not now be
kono
parents:
diff changeset
1115 deletable. We leave the deletion to DCE. */
kono
parents:
diff changeset
1116 /* FIXME: If it turns out that the insn isn't deletable,
kono
parents:
diff changeset
1117 then we may have unnecessarily extended register lifetimes
kono
parents:
diff changeset
1118 and made things worse. */
kono
parents:
diff changeset
1119 }
kono
parents:
diff changeset
1120 }
kono
parents:
diff changeset
1121 }
kono
parents:
diff changeset
1122 /* If try_replace_reg simplified the insn, the regs found by find_used_regs
kono
parents:
diff changeset
1123 may not be valid anymore. Start over. */
kono
parents:
diff changeset
1124 while (changed_this_round);
kono
parents:
diff changeset
1125
kono
parents:
diff changeset
1126 if (changed && DEBUG_INSN_P (insn))
kono
parents:
diff changeset
1127 return 0;
kono
parents:
diff changeset
1128
kono
parents:
diff changeset
1129 return changed;
kono
parents:
diff changeset
1130 }
kono
parents:
diff changeset
1131
kono
parents:
diff changeset
1132 /* Like find_used_regs, but avoid recording uses that appear in
kono
parents:
diff changeset
1133 input-output contexts such as zero_extract or pre_dec. This
kono
parents:
diff changeset
1134 restricts the cases we consider to those for which local cprop
kono
parents:
diff changeset
1135 can legitimately make replacements. */
kono
parents:
diff changeset
1136
kono
parents:
diff changeset
1137 static void
kono
parents:
diff changeset
1138 local_cprop_find_used_regs (rtx *xptr, void *data)
kono
parents:
diff changeset
1139 {
kono
parents:
diff changeset
1140 rtx x = *xptr;
kono
parents:
diff changeset
1141
kono
parents:
diff changeset
1142 if (x == 0)
kono
parents:
diff changeset
1143 return;
kono
parents:
diff changeset
1144
kono
parents:
diff changeset
1145 switch (GET_CODE (x))
kono
parents:
diff changeset
1146 {
kono
parents:
diff changeset
1147 case ZERO_EXTRACT:
kono
parents:
diff changeset
1148 case SIGN_EXTRACT:
kono
parents:
diff changeset
1149 case STRICT_LOW_PART:
kono
parents:
diff changeset
1150 return;
kono
parents:
diff changeset
1151
kono
parents:
diff changeset
1152 case PRE_DEC:
kono
parents:
diff changeset
1153 case PRE_INC:
kono
parents:
diff changeset
1154 case POST_DEC:
kono
parents:
diff changeset
1155 case POST_INC:
kono
parents:
diff changeset
1156 case PRE_MODIFY:
kono
parents:
diff changeset
1157 case POST_MODIFY:
kono
parents:
diff changeset
1158 /* Can only legitimately appear this early in the context of
kono
parents:
diff changeset
1159 stack pushes for function arguments, but handle all of the
kono
parents:
diff changeset
1160 codes nonetheless. */
kono
parents:
diff changeset
1161 return;
kono
parents:
diff changeset
1162
kono
parents:
diff changeset
1163 case SUBREG:
kono
parents:
diff changeset
1164 if (read_modify_subreg_p (x))
kono
parents:
diff changeset
1165 return;
kono
parents:
diff changeset
1166 break;
kono
parents:
diff changeset
1167
kono
parents:
diff changeset
1168 default:
kono
parents:
diff changeset
1169 break;
kono
parents:
diff changeset
1170 }
kono
parents:
diff changeset
1171
kono
parents:
diff changeset
1172 find_used_regs (xptr, data);
kono
parents:
diff changeset
1173 }
kono
parents:
diff changeset
1174
kono
parents:
diff changeset
1175 /* Try to perform local const/copy propagation on X in INSN. */
kono
parents:
diff changeset
1176
kono
parents:
diff changeset
1177 static bool
kono
parents:
diff changeset
1178 do_local_cprop (rtx x, rtx_insn *insn)
kono
parents:
diff changeset
1179 {
kono
parents:
diff changeset
1180 rtx newreg = NULL, newcnst = NULL;
kono
parents:
diff changeset
1181
kono
parents:
diff changeset
1182 /* Rule out USE instructions and ASM statements as we don't want to
kono
parents:
diff changeset
1183 change the hard registers mentioned. */
kono
parents:
diff changeset
1184 if (REG_P (x)
kono
parents:
diff changeset
1185 && (cprop_reg_p (x)
kono
parents:
diff changeset
1186 || (GET_CODE (PATTERN (insn)) != USE
kono
parents:
diff changeset
1187 && asm_noperands (PATTERN (insn)) < 0)))
kono
parents:
diff changeset
1188 {
kono
parents:
diff changeset
1189 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
kono
parents:
diff changeset
1190 struct elt_loc_list *l;
kono
parents:
diff changeset
1191
kono
parents:
diff changeset
1192 if (!val)
kono
parents:
diff changeset
1193 return false;
kono
parents:
diff changeset
1194 for (l = val->locs; l; l = l->next)
kono
parents:
diff changeset
1195 {
kono
parents:
diff changeset
1196 rtx this_rtx = l->loc;
kono
parents:
diff changeset
1197 rtx note;
kono
parents:
diff changeset
1198
kono
parents:
diff changeset
1199 if (cprop_constant_p (this_rtx))
kono
parents:
diff changeset
1200 newcnst = this_rtx;
kono
parents:
diff changeset
1201 if (cprop_reg_p (this_rtx)
kono
parents:
diff changeset
1202 /* Don't copy propagate if it has attached REG_EQUIV note.
kono
parents:
diff changeset
1203 At this point this only function parameters should have
kono
parents:
diff changeset
1204 REG_EQUIV notes and if the argument slot is used somewhere
kono
parents:
diff changeset
1205 explicitly, it means address of parameter has been taken,
kono
parents:
diff changeset
1206 so we should not extend the lifetime of the pseudo. */
kono
parents:
diff changeset
1207 && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
kono
parents:
diff changeset
1208 || ! MEM_P (XEXP (note, 0))))
kono
parents:
diff changeset
1209 newreg = this_rtx;
kono
parents:
diff changeset
1210 }
kono
parents:
diff changeset
1211 if (newcnst && constprop_register (x, newcnst, insn))
kono
parents:
diff changeset
1212 {
kono
parents:
diff changeset
1213 if (dump_file != NULL)
kono
parents:
diff changeset
1214 {
kono
parents:
diff changeset
1215 fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
kono
parents:
diff changeset
1216 REGNO (x));
kono
parents:
diff changeset
1217 fprintf (dump_file, "insn %d with constant ",
kono
parents:
diff changeset
1218 INSN_UID (insn));
kono
parents:
diff changeset
1219 print_rtl (dump_file, newcnst);
kono
parents:
diff changeset
1220 fprintf (dump_file, "\n");
kono
parents:
diff changeset
1221 }
kono
parents:
diff changeset
1222 local_const_prop_count++;
kono
parents:
diff changeset
1223 return true;
kono
parents:
diff changeset
1224 }
kono
parents:
diff changeset
1225 else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
kono
parents:
diff changeset
1226 {
kono
parents:
diff changeset
1227 if (dump_file != NULL)
kono
parents:
diff changeset
1228 {
kono
parents:
diff changeset
1229 fprintf (dump_file,
kono
parents:
diff changeset
1230 "LOCAL COPY-PROP: Replacing reg %d in insn %d",
kono
parents:
diff changeset
1231 REGNO (x), INSN_UID (insn));
kono
parents:
diff changeset
1232 fprintf (dump_file, " with reg %d\n", REGNO (newreg));
kono
parents:
diff changeset
1233 }
kono
parents:
diff changeset
1234 local_copy_prop_count++;
kono
parents:
diff changeset
1235 return true;
kono
parents:
diff changeset
1236 }
kono
parents:
diff changeset
1237 }
kono
parents:
diff changeset
1238 return false;
kono
parents:
diff changeset
1239 }
kono
parents:
diff changeset
1240
kono
parents:
diff changeset
1241 /* Do local const/copy propagation (i.e. within each basic block). */
kono
parents:
diff changeset
1242
kono
parents:
diff changeset
1243 static int
kono
parents:
diff changeset
1244 local_cprop_pass (void)
kono
parents:
diff changeset
1245 {
kono
parents:
diff changeset
1246 basic_block bb;
kono
parents:
diff changeset
1247 rtx_insn *insn;
kono
parents:
diff changeset
1248 bool changed = false;
kono
parents:
diff changeset
1249 unsigned i;
kono
parents:
diff changeset
1250
kono
parents:
diff changeset
1251 auto_vec<rtx_insn *> uncond_traps;
kono
parents:
diff changeset
1252
kono
parents:
diff changeset
1253 cselib_init (0);
kono
parents:
diff changeset
1254 FOR_EACH_BB_FN (bb, cfun)
kono
parents:
diff changeset
1255 {
kono
parents:
diff changeset
1256 FOR_BB_INSNS (bb, insn)
kono
parents:
diff changeset
1257 {
kono
parents:
diff changeset
1258 if (INSN_P (insn))
kono
parents:
diff changeset
1259 {
kono
parents:
diff changeset
1260 bool was_uncond_trap
kono
parents:
diff changeset
1261 = (GET_CODE (PATTERN (insn)) == TRAP_IF
kono
parents:
diff changeset
1262 && XEXP (PATTERN (insn), 0) == const1_rtx);
kono
parents:
diff changeset
1263 rtx note = find_reg_equal_equiv_note (insn);
kono
parents:
diff changeset
1264 do
kono
parents:
diff changeset
1265 {
kono
parents:
diff changeset
1266 reg_use_count = 0;
kono
parents:
diff changeset
1267 note_uses (&PATTERN (insn), local_cprop_find_used_regs,
kono
parents:
diff changeset
1268 NULL);
kono
parents:
diff changeset
1269 if (note)
kono
parents:
diff changeset
1270 local_cprop_find_used_regs (&XEXP (note, 0), NULL);
kono
parents:
diff changeset
1271
kono
parents:
diff changeset
1272 for (i = 0; i < reg_use_count; i++)
kono
parents:
diff changeset
1273 {
kono
parents:
diff changeset
1274 if (do_local_cprop (reg_use_table[i], insn))
kono
parents:
diff changeset
1275 {
kono
parents:
diff changeset
1276 if (!DEBUG_INSN_P (insn))
kono
parents:
diff changeset
1277 changed = true;
kono
parents:
diff changeset
1278 break;
kono
parents:
diff changeset
1279 }
kono
parents:
diff changeset
1280 }
kono
parents:
diff changeset
1281 if (!was_uncond_trap
kono
parents:
diff changeset
1282 && GET_CODE (PATTERN (insn)) == TRAP_IF
kono
parents:
diff changeset
1283 && XEXP (PATTERN (insn), 0) == const1_rtx)
kono
parents:
diff changeset
1284 {
kono
parents:
diff changeset
1285 uncond_traps.safe_push (insn);
kono
parents:
diff changeset
1286 break;
kono
parents:
diff changeset
1287 }
kono
parents:
diff changeset
1288 if (insn->deleted ())
kono
parents:
diff changeset
1289 break;
kono
parents:
diff changeset
1290 }
kono
parents:
diff changeset
1291 while (i < reg_use_count);
kono
parents:
diff changeset
1292 }
kono
parents:
diff changeset
1293 cselib_process_insn (insn);
kono
parents:
diff changeset
1294 }
kono
parents:
diff changeset
1295
kono
parents:
diff changeset
1296 /* Forget everything at the end of a basic block. */
kono
parents:
diff changeset
1297 cselib_clear_table ();
kono
parents:
diff changeset
1298 }
kono
parents:
diff changeset
1299
kono
parents:
diff changeset
1300 cselib_finish ();
kono
parents:
diff changeset
1301
kono
parents:
diff changeset
1302 while (!uncond_traps.is_empty ())
kono
parents:
diff changeset
1303 {
kono
parents:
diff changeset
1304 rtx_insn *insn = uncond_traps.pop ();
kono
parents:
diff changeset
1305 basic_block to_split = BLOCK_FOR_INSN (insn);
kono
parents:
diff changeset
1306 remove_edge (split_block (to_split, insn));
kono
parents:
diff changeset
1307 emit_barrier_after_bb (to_split);
kono
parents:
diff changeset
1308 }
kono
parents:
diff changeset
1309
kono
parents:
diff changeset
1310 return changed;
kono
parents:
diff changeset
1311 }
kono
parents:
diff changeset
1312
kono
parents:
diff changeset
1313 /* Similar to get_condition, only the resulting condition must be
kono
parents:
diff changeset
1314 valid at JUMP, instead of at EARLIEST.
kono
parents:
diff changeset
1315
kono
parents:
diff changeset
1316 This differs from noce_get_condition in ifcvt.c in that we prefer not to
kono
parents:
diff changeset
1317 settle for the condition variable in the jump instruction being integral.
kono
parents:
diff changeset
1318 We prefer to be able to record the value of a user variable, rather than
kono
parents:
diff changeset
1319 the value of a temporary used in a condition. This could be solved by
kono
parents:
diff changeset
1320 recording the value of *every* register scanned by canonicalize_condition,
kono
parents:
diff changeset
1321 but this would require some code reorganization. */
kono
parents:
diff changeset
1322
kono
parents:
diff changeset
1323 rtx
kono
parents:
diff changeset
1324 fis_get_condition (rtx_insn *jump)
kono
parents:
diff changeset
1325 {
kono
parents:
diff changeset
1326 return get_condition (jump, NULL, false, true);
kono
parents:
diff changeset
1327 }
kono
parents:
diff changeset
1328
kono
parents:
diff changeset
1329 /* Check the comparison COND to see if we can safely form an implicit
kono
parents:
diff changeset
1330 set from it. */
kono
parents:
diff changeset
1331
kono
parents:
diff changeset
1332 static bool
kono
parents:
diff changeset
1333 implicit_set_cond_p (const_rtx cond)
kono
parents:
diff changeset
1334 {
kono
parents:
diff changeset
1335 machine_mode mode;
kono
parents:
diff changeset
1336 rtx cst;
kono
parents:
diff changeset
1337
kono
parents:
diff changeset
1338 /* COND must be either an EQ or NE comparison. */
kono
parents:
diff changeset
1339 if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE)
kono
parents:
diff changeset
1340 return false;
kono
parents:
diff changeset
1341
kono
parents:
diff changeset
1342 /* The first operand of COND must be a register we can propagate. */
kono
parents:
diff changeset
1343 if (!cprop_reg_p (XEXP (cond, 0)))
kono
parents:
diff changeset
1344 return false;
kono
parents:
diff changeset
1345
kono
parents:
diff changeset
1346 /* The second operand of COND must be a suitable constant. */
kono
parents:
diff changeset
1347 mode = GET_MODE (XEXP (cond, 0));
kono
parents:
diff changeset
1348 cst = XEXP (cond, 1);
kono
parents:
diff changeset
1349
kono
parents:
diff changeset
1350 /* We can't perform this optimization if either operand might be or might
kono
parents:
diff changeset
1351 contain a signed zero. */
kono
parents:
diff changeset
1352 if (HONOR_SIGNED_ZEROS (mode))
kono
parents:
diff changeset
1353 {
kono
parents:
diff changeset
1354 /* It is sufficient to check if CST is or contains a zero. We must
kono
parents:
diff changeset
1355 handle float, complex, and vector. If any subpart is a zero, then
kono
parents:
diff changeset
1356 the optimization can't be performed. */
kono
parents:
diff changeset
1357 /* ??? The complex and vector checks are not implemented yet. We just
kono
parents:
diff changeset
1358 always return zero for them. */
kono
parents:
diff changeset
1359 if (CONST_DOUBLE_AS_FLOAT_P (cst)
kono
parents:
diff changeset
1360 && real_equal (CONST_DOUBLE_REAL_VALUE (cst), &dconst0))
kono
parents:
diff changeset
1361 return 0;
kono
parents:
diff changeset
1362 else
kono
parents:
diff changeset
1363 return 0;
kono
parents:
diff changeset
1364 }
kono
parents:
diff changeset
1365
kono
parents:
diff changeset
1366 return cprop_constant_p (cst);
kono
parents:
diff changeset
1367 }
kono
parents:
diff changeset
1368
kono
parents:
diff changeset
1369 /* Find the implicit sets of a function. An "implicit set" is a constraint
kono
parents:
diff changeset
1370 on the value of a variable, implied by a conditional jump. For example,
kono
parents:
diff changeset
1371 following "if (x == 2)", the then branch may be optimized as though the
kono
parents:
diff changeset
1372 conditional performed an "explicit set", in this example, "x = 2". This
kono
parents:
diff changeset
1373 function records the set patterns that are implicit at the start of each
kono
parents:
diff changeset
1374 basic block.
kono
parents:
diff changeset
1375
kono
parents:
diff changeset
1376 If an implicit set is found but the set is implicit on a critical edge,
kono
parents:
diff changeset
1377 this critical edge is split.
kono
parents:
diff changeset
1378
kono
parents:
diff changeset
1379 Return true if the CFG was modified, false otherwise. */
kono
parents:
diff changeset
1380
kono
parents:
diff changeset
1381 static bool
kono
parents:
diff changeset
1382 find_implicit_sets (void)
kono
parents:
diff changeset
1383 {
kono
parents:
diff changeset
1384 basic_block bb, dest;
kono
parents:
diff changeset
1385 rtx cond, new_rtx;
kono
parents:
diff changeset
1386 unsigned int count = 0;
kono
parents:
diff changeset
1387 bool edges_split = false;
kono
parents:
diff changeset
1388 size_t implicit_sets_size = last_basic_block_for_fn (cfun) + 10;
kono
parents:
diff changeset
1389
kono
parents:
diff changeset
1390 implicit_sets = XCNEWVEC (rtx, implicit_sets_size);
kono
parents:
diff changeset
1391
kono
parents:
diff changeset
1392 FOR_EACH_BB_FN (bb, cfun)
kono
parents:
diff changeset
1393 {
kono
parents:
diff changeset
1394 /* Check for more than one successor. */
kono
parents:
diff changeset
1395 if (EDGE_COUNT (bb->succs) <= 1)
kono
parents:
diff changeset
1396 continue;
kono
parents:
diff changeset
1397
kono
parents:
diff changeset
1398 cond = fis_get_condition (BB_END (bb));
kono
parents:
diff changeset
1399
kono
parents:
diff changeset
1400 /* If no condition is found or if it isn't of a suitable form,
kono
parents:
diff changeset
1401 ignore it. */
kono
parents:
diff changeset
1402 if (! cond || ! implicit_set_cond_p (cond))
kono
parents:
diff changeset
1403 continue;
kono
parents:
diff changeset
1404
kono
parents:
diff changeset
1405 dest = GET_CODE (cond) == EQ
kono
parents:
diff changeset
1406 ? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest;
kono
parents:
diff changeset
1407
kono
parents:
diff changeset
1408 /* If DEST doesn't go anywhere, ignore it. */
kono
parents:
diff changeset
1409 if (! dest || dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
kono
parents:
diff changeset
1410 continue;
kono
parents:
diff changeset
1411
kono
parents:
diff changeset
1412 /* We have found a suitable implicit set. Try to record it now as
kono
parents:
diff changeset
1413 a SET in DEST. If DEST has more than one predecessor, the edge
kono
parents:
diff changeset
1414 between BB and DEST is a critical edge and we must split it,
kono
parents:
diff changeset
1415 because we can only record one implicit set per DEST basic block. */
kono
parents:
diff changeset
1416 if (! single_pred_p (dest))
kono
parents:
diff changeset
1417 {
kono
parents:
diff changeset
1418 dest = split_edge (find_edge (bb, dest));
kono
parents:
diff changeset
1419 edges_split = true;
kono
parents:
diff changeset
1420 }
kono
parents:
diff changeset
1421
kono
parents:
diff changeset
1422 if (implicit_sets_size <= (size_t) dest->index)
kono
parents:
diff changeset
1423 {
kono
parents:
diff changeset
1424 size_t old_implicit_sets_size = implicit_sets_size;
kono
parents:
diff changeset
1425 implicit_sets_size *= 2;
kono
parents:
diff changeset
1426 implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size);
kono
parents:
diff changeset
1427 memset (implicit_sets + old_implicit_sets_size, 0,
kono
parents:
diff changeset
1428 (implicit_sets_size - old_implicit_sets_size) * sizeof (rtx));
kono
parents:
diff changeset
1429 }
kono
parents:
diff changeset
1430
kono
parents:
diff changeset
1431 new_rtx = gen_rtx_SET (XEXP (cond, 0), XEXP (cond, 1));
kono
parents:
diff changeset
1432 implicit_sets[dest->index] = new_rtx;
kono
parents:
diff changeset
1433 if (dump_file)
kono
parents:
diff changeset
1434 {
kono
parents:
diff changeset
1435 fprintf (dump_file, "Implicit set of reg %d in ",
kono
parents:
diff changeset
1436 REGNO (XEXP (cond, 0)));
kono
parents:
diff changeset
1437 fprintf (dump_file, "basic block %d\n", dest->index);
kono
parents:
diff changeset
1438 }
kono
parents:
diff changeset
1439 count++;
kono
parents:
diff changeset
1440 }
kono
parents:
diff changeset
1441
kono
parents:
diff changeset
1442 if (dump_file)
kono
parents:
diff changeset
1443 fprintf (dump_file, "Found %d implicit sets\n", count);
kono
parents:
diff changeset
1444
kono
parents:
diff changeset
1445 /* Confess our sins. */
kono
parents:
diff changeset
1446 return edges_split;
kono
parents:
diff changeset
1447 }
kono
parents:
diff changeset
1448
kono
parents:
diff changeset
1449 /* Bypass conditional jumps. */
kono
parents:
diff changeset
1450
kono
parents:
diff changeset
1451 /* The value of last_basic_block at the beginning of the jump_bypass
kono
parents:
diff changeset
1452 pass. The use of redirect_edge_and_branch_force may introduce new
kono
parents:
diff changeset
1453 basic blocks, but the data flow analysis is only valid for basic
kono
parents:
diff changeset
1454 block indices less than bypass_last_basic_block. */
kono
parents:
diff changeset
1455
kono
parents:
diff changeset
1456 static int bypass_last_basic_block;
kono
parents:
diff changeset
1457
kono
parents:
diff changeset
1458 /* Find a set of REGNO to a constant that is available at the end of basic
kono
parents:
diff changeset
1459 block BB. Return NULL if no such set is found. Based heavily upon
kono
parents:
diff changeset
1460 find_avail_set. */
kono
parents:
diff changeset
1461
kono
parents:
diff changeset
1462 static struct cprop_expr *
kono
parents:
diff changeset
1463 find_bypass_set (int regno, int bb)
kono
parents:
diff changeset
1464 {
kono
parents:
diff changeset
1465 struct cprop_expr *result = 0;
kono
parents:
diff changeset
1466
kono
parents:
diff changeset
1467 for (;;)
kono
parents:
diff changeset
1468 {
kono
parents:
diff changeset
1469 rtx src;
kono
parents:
diff changeset
1470 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
kono
parents:
diff changeset
1471
kono
parents:
diff changeset
1472 while (set)
kono
parents:
diff changeset
1473 {
kono
parents:
diff changeset
1474 if (bitmap_bit_p (cprop_avout[bb], set->bitmap_index))
kono
parents:
diff changeset
1475 break;
kono
parents:
diff changeset
1476 set = next_set (regno, set);
kono
parents:
diff changeset
1477 }
kono
parents:
diff changeset
1478
kono
parents:
diff changeset
1479 if (set == 0)
kono
parents:
diff changeset
1480 break;
kono
parents:
diff changeset
1481
kono
parents:
diff changeset
1482 src = set->src;
kono
parents:
diff changeset
1483 if (cprop_constant_p (src))
kono
parents:
diff changeset
1484 result = set;
kono
parents:
diff changeset
1485
kono
parents:
diff changeset
1486 if (! REG_P (src))
kono
parents:
diff changeset
1487 break;
kono
parents:
diff changeset
1488
kono
parents:
diff changeset
1489 regno = REGNO (src);
kono
parents:
diff changeset
1490 }
kono
parents:
diff changeset
1491 return result;
kono
parents:
diff changeset
1492 }
kono
parents:
diff changeset
1493
kono
parents:
diff changeset
1494 /* Subroutine of bypass_block that checks whether a pseudo is killed by
kono
parents:
diff changeset
1495 any of the instructions inserted on an edge. Jump bypassing places
kono
parents:
diff changeset
1496 condition code setters on CFG edges using insert_insn_on_edge. This
kono
parents:
diff changeset
1497 function is required to check that our data flow analysis is still
kono
parents:
diff changeset
1498 valid prior to commit_edge_insertions. */
kono
parents:
diff changeset
1499
kono
parents:
diff changeset
1500 static bool
kono
parents:
diff changeset
1501 reg_killed_on_edge (const_rtx reg, const_edge e)
kono
parents:
diff changeset
1502 {
kono
parents:
diff changeset
1503 rtx_insn *insn;
kono
parents:
diff changeset
1504
kono
parents:
diff changeset
1505 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
kono
parents:
diff changeset
1506 if (INSN_P (insn) && reg_set_p (reg, insn))
kono
parents:
diff changeset
1507 return true;
kono
parents:
diff changeset
1508
kono
parents:
diff changeset
1509 return false;
kono
parents:
diff changeset
1510 }
kono
parents:
diff changeset
1511
kono
parents:
diff changeset
1512 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
kono
parents:
diff changeset
1513 basic block BB which has more than one predecessor. If not NULL, SETCC
kono
parents:
diff changeset
1514 is the first instruction of BB, which is immediately followed by JUMP_INSN
kono
parents:
diff changeset
1515 JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
kono
parents:
diff changeset
1516 Returns nonzero if a change was made.
kono
parents:
diff changeset
1517
kono
parents:
diff changeset
1518 During the jump bypassing pass, we may place copies of SETCC instructions
kono
parents:
diff changeset
1519 on CFG edges. The following routine must be careful to pay attention to
kono
parents:
diff changeset
1520 these inserted insns when performing its transformations. */
kono
parents:
diff changeset
1521
kono
parents:
diff changeset
1522 static int
kono
parents:
diff changeset
1523 bypass_block (basic_block bb, rtx_insn *setcc, rtx_insn *jump)
kono
parents:
diff changeset
1524 {
kono
parents:
diff changeset
1525 rtx_insn *insn;
kono
parents:
diff changeset
1526 rtx note;
kono
parents:
diff changeset
1527 edge e, edest;
kono
parents:
diff changeset
1528 int change;
kono
parents:
diff changeset
1529 int may_be_loop_header = false;
kono
parents:
diff changeset
1530 unsigned removed_p;
kono
parents:
diff changeset
1531 unsigned i;
kono
parents:
diff changeset
1532 edge_iterator ei;
kono
parents:
diff changeset
1533
kono
parents:
diff changeset
1534 insn = (setcc != NULL) ? setcc : jump;
kono
parents:
diff changeset
1535
kono
parents:
diff changeset
1536 /* Determine set of register uses in INSN. */
kono
parents:
diff changeset
1537 reg_use_count = 0;
kono
parents:
diff changeset
1538 note_uses (&PATTERN (insn), find_used_regs, NULL);
kono
parents:
diff changeset
1539 note = find_reg_equal_equiv_note (insn);
kono
parents:
diff changeset
1540 if (note)
kono
parents:
diff changeset
1541 find_used_regs (&XEXP (note, 0), NULL);
kono
parents:
diff changeset
1542
kono
parents:
diff changeset
1543 if (current_loops)
kono
parents:
diff changeset
1544 {
kono
parents:
diff changeset
1545 /* If we are to preserve loop structure then do not bypass
kono
parents:
diff changeset
1546 a loop header. This will either rotate the loop, create
kono
parents:
diff changeset
1547 multiple entry loops or even irreducible regions. */
kono
parents:
diff changeset
1548 if (bb == bb->loop_father->header)
kono
parents:
diff changeset
1549 return 0;
kono
parents:
diff changeset
1550 }
kono
parents:
diff changeset
1551 else
kono
parents:
diff changeset
1552 {
kono
parents:
diff changeset
1553 FOR_EACH_EDGE (e, ei, bb->preds)
kono
parents:
diff changeset
1554 if (e->flags & EDGE_DFS_BACK)
kono
parents:
diff changeset
1555 {
kono
parents:
diff changeset
1556 may_be_loop_header = true;
kono
parents:
diff changeset
1557 break;
kono
parents:
diff changeset
1558 }
kono
parents:
diff changeset
1559 }
kono
parents:
diff changeset
1560
kono
parents:
diff changeset
1561 change = 0;
kono
parents:
diff changeset
1562 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
kono
parents:
diff changeset
1563 {
kono
parents:
diff changeset
1564 removed_p = 0;
kono
parents:
diff changeset
1565
kono
parents:
diff changeset
1566 if (e->flags & EDGE_COMPLEX)
kono
parents:
diff changeset
1567 {
kono
parents:
diff changeset
1568 ei_next (&ei);
kono
parents:
diff changeset
1569 continue;
kono
parents:
diff changeset
1570 }
kono
parents:
diff changeset
1571
kono
parents:
diff changeset
1572 /* We can't redirect edges from new basic blocks. */
kono
parents:
diff changeset
1573 if (e->src->index >= bypass_last_basic_block)
kono
parents:
diff changeset
1574 {
kono
parents:
diff changeset
1575 ei_next (&ei);
kono
parents:
diff changeset
1576 continue;
kono
parents:
diff changeset
1577 }
kono
parents:
diff changeset
1578
kono
parents:
diff changeset
1579 /* The irreducible loops created by redirecting of edges entering the
kono
parents:
diff changeset
1580 loop from outside would decrease effectiveness of some of the
kono
parents:
diff changeset
1581 following optimizations, so prevent this. */
kono
parents:
diff changeset
1582 if (may_be_loop_header
kono
parents:
diff changeset
1583 && !(e->flags & EDGE_DFS_BACK))
kono
parents:
diff changeset
1584 {
kono
parents:
diff changeset
1585 ei_next (&ei);
kono
parents:
diff changeset
1586 continue;
kono
parents:
diff changeset
1587 }
kono
parents:
diff changeset
1588
kono
parents:
diff changeset
1589 for (i = 0; i < reg_use_count; i++)
kono
parents:
diff changeset
1590 {
kono
parents:
diff changeset
1591 rtx reg_used = reg_use_table[i];
kono
parents:
diff changeset
1592 unsigned int regno = REGNO (reg_used);
kono
parents:
diff changeset
1593 basic_block dest, old_dest;
kono
parents:
diff changeset
1594 struct cprop_expr *set;
kono
parents:
diff changeset
1595 rtx src, new_rtx;
kono
parents:
diff changeset
1596
kono
parents:
diff changeset
1597 set = find_bypass_set (regno, e->src->index);
kono
parents:
diff changeset
1598
kono
parents:
diff changeset
1599 if (! set)
kono
parents:
diff changeset
1600 continue;
kono
parents:
diff changeset
1601
kono
parents:
diff changeset
1602 /* Check the data flow is valid after edge insertions. */
kono
parents:
diff changeset
1603 if (e->insns.r && reg_killed_on_edge (reg_used, e))
kono
parents:
diff changeset
1604 continue;
kono
parents:
diff changeset
1605
kono
parents:
diff changeset
1606 src = SET_SRC (pc_set (jump));
kono
parents:
diff changeset
1607
kono
parents:
diff changeset
1608 if (setcc != NULL)
kono
parents:
diff changeset
1609 src = simplify_replace_rtx (src,
kono
parents:
diff changeset
1610 SET_DEST (PATTERN (setcc)),
kono
parents:
diff changeset
1611 SET_SRC (PATTERN (setcc)));
kono
parents:
diff changeset
1612
kono
parents:
diff changeset
1613 new_rtx = simplify_replace_rtx (src, reg_used, set->src);
kono
parents:
diff changeset
1614
kono
parents:
diff changeset
1615 /* Jump bypassing may have already placed instructions on
kono
parents:
diff changeset
1616 edges of the CFG. We can't bypass an outgoing edge that
kono
parents:
diff changeset
1617 has instructions associated with it, as these insns won't
kono
parents:
diff changeset
1618 get executed if the incoming edge is redirected. */
kono
parents:
diff changeset
1619 if (new_rtx == pc_rtx)
kono
parents:
diff changeset
1620 {
kono
parents:
diff changeset
1621 edest = FALLTHRU_EDGE (bb);
kono
parents:
diff changeset
1622 dest = edest->insns.r ? NULL : edest->dest;
kono
parents:
diff changeset
1623 }
kono
parents:
diff changeset
1624 else if (GET_CODE (new_rtx) == LABEL_REF)
kono
parents:
diff changeset
1625 {
kono
parents:
diff changeset
1626 dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
kono
parents:
diff changeset
1627 /* Don't bypass edges containing instructions. */
kono
parents:
diff changeset
1628 edest = find_edge (bb, dest);
kono
parents:
diff changeset
1629 if (edest && edest->insns.r)
kono
parents:
diff changeset
1630 dest = NULL;
kono
parents:
diff changeset
1631 }
kono
parents:
diff changeset
1632 else
kono
parents:
diff changeset
1633 dest = NULL;
kono
parents:
diff changeset
1634
kono
parents:
diff changeset
1635 /* Avoid unification of the edge with other edges from original
kono
parents:
diff changeset
1636 branch. We would end up emitting the instruction on "both"
kono
parents:
diff changeset
1637 edges. */
kono
parents:
diff changeset
1638 if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
kono
parents:
diff changeset
1639 && find_edge (e->src, dest))
kono
parents:
diff changeset
1640 dest = NULL;
kono
parents:
diff changeset
1641
kono
parents:
diff changeset
1642 old_dest = e->dest;
kono
parents:
diff changeset
1643 if (dest != NULL
kono
parents:
diff changeset
1644 && dest != old_dest
kono
parents:
diff changeset
1645 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
kono
parents:
diff changeset
1646 {
kono
parents:
diff changeset
1647 redirect_edge_and_branch_force (e, dest);
kono
parents:
diff changeset
1648
kono
parents:
diff changeset
1649 /* Copy the register setter to the redirected edge.
kono
parents:
diff changeset
1650 Don't copy CC0 setters, as CC0 is dead after jump. */
kono
parents:
diff changeset
1651 if (setcc)
kono
parents:
diff changeset
1652 {
kono
parents:
diff changeset
1653 rtx pat = PATTERN (setcc);
kono
parents:
diff changeset
1654 if (!CC0_P (SET_DEST (pat)))
kono
parents:
diff changeset
1655 insert_insn_on_edge (copy_insn (pat), e);
kono
parents:
diff changeset
1656 }
kono
parents:
diff changeset
1657
kono
parents:
diff changeset
1658 if (dump_file != NULL)
kono
parents:
diff changeset
1659 {
kono
parents:
diff changeset
1660 fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
kono
parents:
diff changeset
1661 "in jump_insn %d equals constant ",
kono
parents:
diff changeset
1662 regno, INSN_UID (jump));
kono
parents:
diff changeset
1663 print_rtl (dump_file, set->src);
kono
parents:
diff changeset
1664 fprintf (dump_file, "\n\t when BB %d is entered from "
kono
parents:
diff changeset
1665 "BB %d. Redirect edge %d->%d to %d.\n",
kono
parents:
diff changeset
1666 old_dest->index, e->src->index, e->src->index,
kono
parents:
diff changeset
1667 old_dest->index, dest->index);
kono
parents:
diff changeset
1668 }
kono
parents:
diff changeset
1669 change = 1;
kono
parents:
diff changeset
1670 removed_p = 1;
kono
parents:
diff changeset
1671 break;
kono
parents:
diff changeset
1672 }
kono
parents:
diff changeset
1673 }
kono
parents:
diff changeset
1674 if (!removed_p)
kono
parents:
diff changeset
1675 ei_next (&ei);
kono
parents:
diff changeset
1676 }
kono
parents:
diff changeset
1677 return change;
kono
parents:
diff changeset
1678 }
kono
parents:
diff changeset
1679
kono
parents:
diff changeset
1680 /* Find basic blocks with more than one predecessor that only contain a
kono
parents:
diff changeset
1681 single conditional jump. If the result of the comparison is known at
kono
parents:
diff changeset
1682 compile-time from any incoming edge, redirect that edge to the
kono
parents:
diff changeset
1683 appropriate target. Return nonzero if a change was made.
kono
parents:
diff changeset
1684
kono
parents:
diff changeset
1685 This function is now mis-named, because we also handle indirect jumps. */
kono
parents:
diff changeset
1686
kono
parents:
diff changeset
1687 static int
kono
parents:
diff changeset
1688 bypass_conditional_jumps (void)
kono
parents:
diff changeset
1689 {
kono
parents:
diff changeset
1690 basic_block bb;
kono
parents:
diff changeset
1691 int changed;
kono
parents:
diff changeset
1692 rtx_insn *setcc;
kono
parents:
diff changeset
1693 rtx_insn *insn;
kono
parents:
diff changeset
1694 rtx dest;
kono
parents:
diff changeset
1695
kono
parents:
diff changeset
1696 /* Note we start at block 1. */
kono
parents:
diff changeset
1697 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
kono
parents:
diff changeset
1698 return 0;
kono
parents:
diff changeset
1699
kono
parents:
diff changeset
1700 mark_dfs_back_edges ();
kono
parents:
diff changeset
1701
kono
parents:
diff changeset
1702 changed = 0;
kono
parents:
diff changeset
1703 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
kono
parents:
diff changeset
1704 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
kono
parents:
diff changeset
1705 {
kono
parents:
diff changeset
1706 /* Check for more than one predecessor. */
kono
parents:
diff changeset
1707 if (!single_pred_p (bb))
kono
parents:
diff changeset
1708 {
kono
parents:
diff changeset
1709 setcc = NULL;
kono
parents:
diff changeset
1710 FOR_BB_INSNS (bb, insn)
kono
parents:
diff changeset
1711 if (DEBUG_INSN_P (insn))
kono
parents:
diff changeset
1712 continue;
kono
parents:
diff changeset
1713 else if (NONJUMP_INSN_P (insn))
kono
parents:
diff changeset
1714 {
kono
parents:
diff changeset
1715 if (setcc)
kono
parents:
diff changeset
1716 break;
kono
parents:
diff changeset
1717 if (GET_CODE (PATTERN (insn)) != SET)
kono
parents:
diff changeset
1718 break;
kono
parents:
diff changeset
1719
kono
parents:
diff changeset
1720 dest = SET_DEST (PATTERN (insn));
kono
parents:
diff changeset
1721 if (REG_P (dest) || CC0_P (dest))
kono
parents:
diff changeset
1722 setcc = insn;
kono
parents:
diff changeset
1723 else
kono
parents:
diff changeset
1724 break;
kono
parents:
diff changeset
1725 }
kono
parents:
diff changeset
1726 else if (JUMP_P (insn))
kono
parents:
diff changeset
1727 {
kono
parents:
diff changeset
1728 if ((any_condjump_p (insn) || computed_jump_p (insn))
kono
parents:
diff changeset
1729 && onlyjump_p (insn))
kono
parents:
diff changeset
1730 changed |= bypass_block (bb, setcc, insn);
kono
parents:
diff changeset
1731 break;
kono
parents:
diff changeset
1732 }
kono
parents:
diff changeset
1733 else if (INSN_P (insn))
kono
parents:
diff changeset
1734 break;
kono
parents:
diff changeset
1735 }
kono
parents:
diff changeset
1736 }
kono
parents:
diff changeset
1737
kono
parents:
diff changeset
1738 /* If we bypassed any register setting insns, we inserted a
kono
parents:
diff changeset
1739 copy on the redirected edge. These need to be committed. */
kono
parents:
diff changeset
1740 if (changed)
kono
parents:
diff changeset
1741 commit_edge_insertions ();
kono
parents:
diff changeset
1742
kono
parents:
diff changeset
1743 return changed;
kono
parents:
diff changeset
1744 }
kono
parents:
diff changeset
1745
kono
parents:
diff changeset
1746 /* Main function for the CPROP pass. */
kono
parents:
diff changeset
1747
kono
parents:
diff changeset
1748 static int
kono
parents:
diff changeset
1749 one_cprop_pass (void)
kono
parents:
diff changeset
1750 {
kono
parents:
diff changeset
1751 int i;
kono
parents:
diff changeset
1752 int changed = 0;
kono
parents:
diff changeset
1753
kono
parents:
diff changeset
1754 /* Return if there's nothing to do, or it is too expensive. */
kono
parents:
diff changeset
1755 if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
kono
parents:
diff changeset
1756 || gcse_or_cprop_is_too_expensive (_ ("const/copy propagation disabled")))
kono
parents:
diff changeset
1757 return 0;
kono
parents:
diff changeset
1758
kono
parents:
diff changeset
1759 global_const_prop_count = local_const_prop_count = 0;
kono
parents:
diff changeset
1760 global_copy_prop_count = local_copy_prop_count = 0;
kono
parents:
diff changeset
1761
kono
parents:
diff changeset
1762 bytes_used = 0;
kono
parents:
diff changeset
1763 gcc_obstack_init (&cprop_obstack);
kono
parents:
diff changeset
1764
kono
parents:
diff changeset
1765 /* Do a local const/copy propagation pass first. The global pass
kono
parents:
diff changeset
1766 only handles global opportunities.
kono
parents:
diff changeset
1767 If the local pass changes something, remove any unreachable blocks
kono
parents:
diff changeset
1768 because the CPROP global dataflow analysis may get into infinite
kono
parents:
diff changeset
1769 loops for CFGs with unreachable blocks.
kono
parents:
diff changeset
1770
kono
parents:
diff changeset
1771 FIXME: This local pass should not be necessary after CSE (but for
kono
parents:
diff changeset
1772 some reason it still is). It is also (proven) not necessary
kono
parents:
diff changeset
1773 to run the local pass right after FWPWOP.
kono
parents:
diff changeset
1774
kono
parents:
diff changeset
1775 FIXME: The global analysis would not get into infinite loops if it
kono
parents:
diff changeset
1776 would use the DF solver (via df_simple_dataflow) instead of
kono
parents:
diff changeset
1777 the solver implemented in this file. */
kono
parents:
diff changeset
1778 changed |= local_cprop_pass ();
kono
parents:
diff changeset
1779 if (changed)
kono
parents:
diff changeset
1780 delete_unreachable_blocks ();
kono
parents:
diff changeset
1781
kono
parents:
diff changeset
1782 /* Determine implicit sets. This may change the CFG (split critical
kono
parents:
diff changeset
1783 edges if that exposes an implicit set).
kono
parents:
diff changeset
1784 Note that find_implicit_sets() does not rely on up-to-date DF caches
kono
parents:
diff changeset
1785 so that we do not have to re-run df_analyze() even if local CPROP
kono
parents:
diff changeset
1786 changed something.
kono
parents:
diff changeset
1787 ??? This could run earlier so that any uncovered implicit sets
kono
parents:
diff changeset
1788 sets could be exploited in local_cprop_pass() also. Later. */
kono
parents:
diff changeset
1789 changed |= find_implicit_sets ();
kono
parents:
diff changeset
1790
kono
parents:
diff changeset
1791 /* If local_cprop_pass() or find_implicit_sets() changed something,
kono
parents:
diff changeset
1792 run df_analyze() to bring all insn caches up-to-date, and to take
kono
parents:
diff changeset
1793 new basic blocks from edge splitting on the DF radar.
kono
parents:
diff changeset
1794 NB: This also runs the fast DCE pass, because execute_rtl_cprop
kono
parents:
diff changeset
1795 sets DF_LR_RUN_DCE. */
kono
parents:
diff changeset
1796 if (changed)
kono
parents:
diff changeset
1797 df_analyze ();
kono
parents:
diff changeset
1798
kono
parents:
diff changeset
1799 /* Initialize implicit_set_indexes array. */
kono
parents:
diff changeset
1800 implicit_set_indexes = XNEWVEC (int, last_basic_block_for_fn (cfun));
kono
parents:
diff changeset
1801 for (i = 0; i < last_basic_block_for_fn (cfun); i++)
kono
parents:
diff changeset
1802 implicit_set_indexes[i] = -1;
kono
parents:
diff changeset
1803
kono
parents:
diff changeset
1804 alloc_hash_table (&set_hash_table);
kono
parents:
diff changeset
1805 compute_hash_table (&set_hash_table);
kono
parents:
diff changeset
1806
kono
parents:
diff changeset
1807 /* Free implicit_sets before peak usage. */
kono
parents:
diff changeset
1808 free (implicit_sets);
kono
parents:
diff changeset
1809 implicit_sets = NULL;
kono
parents:
diff changeset
1810
kono
parents:
diff changeset
1811 if (dump_file)
kono
parents:
diff changeset
1812 dump_hash_table (dump_file, "SET", &set_hash_table);
kono
parents:
diff changeset
1813 if (set_hash_table.n_elems > 0)
kono
parents:
diff changeset
1814 {
kono
parents:
diff changeset
1815 basic_block bb;
kono
parents:
diff changeset
1816 auto_vec<rtx_insn *> uncond_traps;
kono
parents:
diff changeset
1817
kono
parents:
diff changeset
1818 alloc_cprop_mem (last_basic_block_for_fn (cfun),
kono
parents:
diff changeset
1819 set_hash_table.n_elems);
kono
parents:
diff changeset
1820 compute_cprop_data ();
kono
parents:
diff changeset
1821
kono
parents:
diff changeset
1822 free (implicit_set_indexes);
kono
parents:
diff changeset
1823 implicit_set_indexes = NULL;
kono
parents:
diff changeset
1824
kono
parents:
diff changeset
1825 /* Allocate vars to track sets of regs. */
kono
parents:
diff changeset
1826 reg_set_bitmap = ALLOC_REG_SET (NULL);
kono
parents:
diff changeset
1827
kono
parents:
diff changeset
1828 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
kono
parents:
diff changeset
1829 EXIT_BLOCK_PTR_FOR_FN (cfun),
kono
parents:
diff changeset
1830 next_bb)
kono
parents:
diff changeset
1831 {
kono
parents:
diff changeset
1832 bool seen_uncond_trap = false;
kono
parents:
diff changeset
1833 rtx_insn *insn;
kono
parents:
diff changeset
1834
kono
parents:
diff changeset
1835 /* Reset tables used to keep track of what's still valid [since
kono
parents:
diff changeset
1836 the start of the block]. */
kono
parents:
diff changeset
1837 reset_opr_set_tables ();
kono
parents:
diff changeset
1838
kono
parents:
diff changeset
1839 FOR_BB_INSNS (bb, insn)
kono
parents:
diff changeset
1840 if (INSN_P (insn))
kono
parents:
diff changeset
1841 {
kono
parents:
diff changeset
1842 bool was_uncond_trap
kono
parents:
diff changeset
1843 = (GET_CODE (PATTERN (insn)) == TRAP_IF
kono
parents:
diff changeset
1844 && XEXP (PATTERN (insn), 0) == const1_rtx);
kono
parents:
diff changeset
1845
kono
parents:
diff changeset
1846 changed |= cprop_insn (insn);
kono
parents:
diff changeset
1847
kono
parents:
diff changeset
1848 /* Keep track of everything modified by this insn. */
kono
parents:
diff changeset
1849 /* ??? Need to be careful w.r.t. mods done to INSN.
kono
parents:
diff changeset
1850 Don't call mark_oprs_set if we turned the
kono
parents:
diff changeset
1851 insn into a NOTE, or deleted the insn. */
kono
parents:
diff changeset
1852 if (! NOTE_P (insn) && ! insn->deleted ())
kono
parents:
diff changeset
1853 mark_oprs_set (insn);
kono
parents:
diff changeset
1854
kono
parents:
diff changeset
1855 if (!was_uncond_trap
kono
parents:
diff changeset
1856 && GET_CODE (PATTERN (insn)) == TRAP_IF
kono
parents:
diff changeset
1857 && XEXP (PATTERN (insn), 0) == const1_rtx)
kono
parents:
diff changeset
1858 {
kono
parents:
diff changeset
1859 /* If we have already seen an unconditional trap
kono
parents:
diff changeset
1860 earlier, the rest of the bb is going to be removed
kono
parents:
diff changeset
1861 as unreachable. Just turn it into a note, so that
kono
parents:
diff changeset
1862 RTL verification doesn't complain about it before
kono
parents:
diff changeset
1863 it is finally removed. */
kono
parents:
diff changeset
1864 if (seen_uncond_trap)
kono
parents:
diff changeset
1865 set_insn_deleted (insn);
kono
parents:
diff changeset
1866 else
kono
parents:
diff changeset
1867 {
kono
parents:
diff changeset
1868 seen_uncond_trap = true;
kono
parents:
diff changeset
1869 uncond_traps.safe_push (insn);
kono
parents:
diff changeset
1870 }
kono
parents:
diff changeset
1871 }
kono
parents:
diff changeset
1872 }
kono
parents:
diff changeset
1873 }
kono
parents:
diff changeset
1874
kono
parents:
diff changeset
1875 /* Make sure bypass_conditional_jumps will ignore not just its new
kono
parents:
diff changeset
1876 basic blocks, but also the ones after unconditional traps (those are
kono
parents:
diff changeset
1877 unreachable and will be eventually removed as such). */
kono
parents:
diff changeset
1878 bypass_last_basic_block = last_basic_block_for_fn (cfun);
kono
parents:
diff changeset
1879
kono
parents:
diff changeset
1880 while (!uncond_traps.is_empty ())
kono
parents:
diff changeset
1881 {
kono
parents:
diff changeset
1882 rtx_insn *insn = uncond_traps.pop ();
kono
parents:
diff changeset
1883 basic_block to_split = BLOCK_FOR_INSN (insn);
kono
parents:
diff changeset
1884 remove_edge (split_block (to_split, insn));
kono
parents:
diff changeset
1885 emit_barrier_after_bb (to_split);
kono
parents:
diff changeset
1886 }
kono
parents:
diff changeset
1887
kono
parents:
diff changeset
1888 changed |= bypass_conditional_jumps ();
kono
parents:
diff changeset
1889
kono
parents:
diff changeset
1890 FREE_REG_SET (reg_set_bitmap);
kono
parents:
diff changeset
1891 free_cprop_mem ();
kono
parents:
diff changeset
1892 }
kono
parents:
diff changeset
1893 else
kono
parents:
diff changeset
1894 {
kono
parents:
diff changeset
1895 free (implicit_set_indexes);
kono
parents:
diff changeset
1896 implicit_set_indexes = NULL;
kono
parents:
diff changeset
1897 }
kono
parents:
diff changeset
1898
kono
parents:
diff changeset
1899 free_hash_table (&set_hash_table);
kono
parents:
diff changeset
1900 obstack_free (&cprop_obstack, NULL);
kono
parents:
diff changeset
1901
kono
parents:
diff changeset
1902 if (dump_file)
kono
parents:
diff changeset
1903 {
kono
parents:
diff changeset
1904 fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
kono
parents:
diff changeset
1905 current_function_name (), n_basic_blocks_for_fn (cfun),
kono
parents:
diff changeset
1906 bytes_used);
kono
parents:
diff changeset
1907 fprintf (dump_file, "%d local const props, %d local copy props, ",
kono
parents:
diff changeset
1908 local_const_prop_count, local_copy_prop_count);
kono
parents:
diff changeset
1909 fprintf (dump_file, "%d global const props, %d global copy props\n\n",
kono
parents:
diff changeset
1910 global_const_prop_count, global_copy_prop_count);
kono
parents:
diff changeset
1911 }
kono
parents:
diff changeset
1912
kono
parents:
diff changeset
1913 return changed;
kono
parents:
diff changeset
1914 }
kono
parents:
diff changeset
1915
kono
parents:
diff changeset
1916 /* All the passes implemented in this file. Each pass has its
kono
parents:
diff changeset
1917 own gate and execute function, and at the end of the file a
kono
parents:
diff changeset
1918 pass definition for passes.c.
kono
parents:
diff changeset
1919
kono
parents:
diff changeset
1920 We do not construct an accurate cfg in functions which call
kono
parents:
diff changeset
1921 setjmp, so none of these passes runs if the function calls
kono
parents:
diff changeset
1922 setjmp.
kono
parents:
diff changeset
1923 FIXME: Should just handle setjmp via REG_SETJMP notes. */
kono
parents:
diff changeset
1924
kono
parents:
diff changeset
1925 static unsigned int
kono
parents:
diff changeset
1926 execute_rtl_cprop (void)
kono
parents:
diff changeset
1927 {
kono
parents:
diff changeset
1928 int changed;
kono
parents:
diff changeset
1929 delete_unreachable_blocks ();
kono
parents:
diff changeset
1930 df_set_flags (DF_LR_RUN_DCE);
kono
parents:
diff changeset
1931 df_analyze ();
kono
parents:
diff changeset
1932 changed = one_cprop_pass ();
kono
parents:
diff changeset
1933 flag_rerun_cse_after_global_opts |= changed;
kono
parents:
diff changeset
1934 if (changed)
kono
parents:
diff changeset
1935 cleanup_cfg (CLEANUP_CFG_CHANGED);
kono
parents:
diff changeset
1936 return 0;
kono
parents:
diff changeset
1937 }
kono
parents:
diff changeset
1938
kono
parents:
diff changeset
1939 namespace {
kono
parents:
diff changeset
1940
kono
parents:
diff changeset
1941 const pass_data pass_data_rtl_cprop =
kono
parents:
diff changeset
1942 {
kono
parents:
diff changeset
1943 RTL_PASS, /* type */
kono
parents:
diff changeset
1944 "cprop", /* name */
kono
parents:
diff changeset
1945 OPTGROUP_NONE, /* optinfo_flags */
kono
parents:
diff changeset
1946 TV_CPROP, /* tv_id */
kono
parents:
diff changeset
1947 PROP_cfglayout, /* properties_required */
kono
parents:
diff changeset
1948 0, /* properties_provided */
kono
parents:
diff changeset
1949 0, /* properties_destroyed */
kono
parents:
diff changeset
1950 0, /* todo_flags_start */
kono
parents:
diff changeset
1951 TODO_df_finish, /* todo_flags_finish */
kono
parents:
diff changeset
1952 };
kono
parents:
diff changeset
1953
kono
parents:
diff changeset
1954 class pass_rtl_cprop : public rtl_opt_pass
kono
parents:
diff changeset
1955 {
kono
parents:
diff changeset
1956 public:
kono
parents:
diff changeset
1957 pass_rtl_cprop (gcc::context *ctxt)
kono
parents:
diff changeset
1958 : rtl_opt_pass (pass_data_rtl_cprop, ctxt)
kono
parents:
diff changeset
1959 {}
kono
parents:
diff changeset
1960
kono
parents:
diff changeset
1961 /* opt_pass methods: */
kono
parents:
diff changeset
1962 opt_pass * clone () { return new pass_rtl_cprop (m_ctxt); }
kono
parents:
diff changeset
1963 virtual bool gate (function *fun)
kono
parents:
diff changeset
1964 {
kono
parents:
diff changeset
1965 return optimize > 0 && flag_gcse
kono
parents:
diff changeset
1966 && !fun->calls_setjmp
kono
parents:
diff changeset
1967 && dbg_cnt (cprop);
kono
parents:
diff changeset
1968 }
kono
parents:
diff changeset
1969
kono
parents:
diff changeset
1970 virtual unsigned int execute (function *) { return execute_rtl_cprop (); }
kono
parents:
diff changeset
1971
kono
parents:
diff changeset
1972 }; // class pass_rtl_cprop
kono
parents:
diff changeset
1973
kono
parents:
diff changeset
1974 } // anon namespace
kono
parents:
diff changeset
1975
kono
parents:
diff changeset
1976 rtl_opt_pass *
kono
parents:
diff changeset
1977 make_pass_rtl_cprop (gcc::context *ctxt)
kono
parents:
diff changeset
1978 {
kono
parents:
diff changeset
1979 return new pass_rtl_cprop (ctxt);
kono
parents:
diff changeset
1980 }