annotate gcc/cprop.c @ 158:494b0b89df80 default tip

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