annotate gcc/lra-spills.c @ 138:fc828634a951

merge
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Thu, 08 Nov 2018 14:17:14 +0900
parents 84e7813d76e9
children 1830386684a0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* Change pseudos by memory.
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
2 Copyright (C) 2010-2018 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 This file is part of GCC.
kono
parents:
diff changeset
6
kono
parents:
diff changeset
7 GCC is free software; you can redistribute it and/or modify it under
kono
parents:
diff changeset
8 the terms of the GNU General Public License as published by the Free
kono
parents:
diff changeset
9 Software Foundation; either version 3, or (at your option) any later
kono
parents:
diff changeset
10 version.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
kono
parents:
diff changeset
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
kono
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kono
parents:
diff changeset
15 for more details.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
kono
parents:
diff changeset
18 along with GCC; see the file COPYING3. If not see
kono
parents:
diff changeset
19 <http://www.gnu.org/licenses/>. */
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21
kono
parents:
diff changeset
22 /* This file contains code for a pass to change spilled pseudos into
kono
parents:
diff changeset
23 memory.
kono
parents:
diff changeset
24
kono
parents:
diff changeset
25 The pass creates necessary stack slots and assigns spilled pseudos
kono
parents:
diff changeset
26 to the stack slots in following way:
kono
parents:
diff changeset
27
kono
parents:
diff changeset
28 for all spilled pseudos P most frequently used first do
kono
parents:
diff changeset
29 for all stack slots S do
kono
parents:
diff changeset
30 if P doesn't conflict with pseudos assigned to S then
kono
parents:
diff changeset
31 assign S to P and goto to the next pseudo process
kono
parents:
diff changeset
32 end
kono
parents:
diff changeset
33 end
kono
parents:
diff changeset
34 create new stack slot S and assign P to S
kono
parents:
diff changeset
35 end
kono
parents:
diff changeset
36
kono
parents:
diff changeset
37 The actual algorithm is bit more complicated because of different
kono
parents:
diff changeset
38 pseudo sizes.
kono
parents:
diff changeset
39
kono
parents:
diff changeset
40 After that the code changes spilled pseudos (except ones created
kono
parents:
diff changeset
41 from scratches) by corresponding stack slot memory in RTL.
kono
parents:
diff changeset
42
kono
parents:
diff changeset
43 If at least one stack slot was created, we need to run more passes
kono
parents:
diff changeset
44 because we have new addresses which should be checked and because
kono
parents:
diff changeset
45 the old address displacements might change and address constraints
kono
parents:
diff changeset
46 (or insn memory constraints) might not be satisfied any more.
kono
parents:
diff changeset
47
kono
parents:
diff changeset
48 For some targets, the pass can spill some pseudos into hard
kono
parents:
diff changeset
49 registers of different class (usually into vector registers)
kono
parents:
diff changeset
50 instead of spilling them into memory if it is possible and
kono
parents:
diff changeset
51 profitable. Spilling GENERAL_REGS pseudo into SSE registers for
kono
parents:
diff changeset
52 Intel Corei7 is an example of such optimization. And this is
kono
parents:
diff changeset
53 actually recommended by Intel optimization guide.
kono
parents:
diff changeset
54
kono
parents:
diff changeset
55 The file also contains code for final change of pseudos on hard
kono
parents:
diff changeset
56 regs correspondingly assigned to them. */
kono
parents:
diff changeset
57
kono
parents:
diff changeset
58 #include "config.h"
kono
parents:
diff changeset
59 #include "system.h"
kono
parents:
diff changeset
60 #include "coretypes.h"
kono
parents:
diff changeset
61 #include "backend.h"
kono
parents:
diff changeset
62 #include "target.h"
kono
parents:
diff changeset
63 #include "rtl.h"
kono
parents:
diff changeset
64 #include "df.h"
kono
parents:
diff changeset
65 #include "insn-config.h"
kono
parents:
diff changeset
66 #include "regs.h"
kono
parents:
diff changeset
67 #include "memmodel.h"
kono
parents:
diff changeset
68 #include "ira.h"
kono
parents:
diff changeset
69 #include "recog.h"
kono
parents:
diff changeset
70 #include "output.h"
kono
parents:
diff changeset
71 #include "cfgrtl.h"
kono
parents:
diff changeset
72 #include "lra.h"
kono
parents:
diff changeset
73 #include "lra-int.h"
kono
parents:
diff changeset
74
kono
parents:
diff changeset
75
kono
parents:
diff changeset
76 /* Max regno at the start of the pass. */
kono
parents:
diff changeset
77 static int regs_num;
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 /* Map spilled regno -> hard regno used instead of memory for
kono
parents:
diff changeset
80 spilling. */
kono
parents:
diff changeset
81 static rtx *spill_hard_reg;
kono
parents:
diff changeset
82
kono
parents:
diff changeset
83 /* The structure describes stack slot of a spilled pseudo. */
kono
parents:
diff changeset
84 struct pseudo_slot
kono
parents:
diff changeset
85 {
kono
parents:
diff changeset
86 /* Number (0, 1, ...) of the stack slot to which given pseudo
kono
parents:
diff changeset
87 belongs. */
kono
parents:
diff changeset
88 int slot_num;
kono
parents:
diff changeset
89 /* First or next slot with the same slot number. */
kono
parents:
diff changeset
90 struct pseudo_slot *next, *first;
kono
parents:
diff changeset
91 /* Memory representing the spilled pseudo. */
kono
parents:
diff changeset
92 rtx mem;
kono
parents:
diff changeset
93 };
kono
parents:
diff changeset
94
kono
parents:
diff changeset
95 /* The stack slots for each spilled pseudo. Indexed by regnos. */
kono
parents:
diff changeset
96 static struct pseudo_slot *pseudo_slots;
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 /* The structure describes a register or a stack slot which can be
kono
parents:
diff changeset
99 used for several spilled pseudos. */
kono
parents:
diff changeset
100 struct slot
kono
parents:
diff changeset
101 {
kono
parents:
diff changeset
102 /* First pseudo with given stack slot. */
kono
parents:
diff changeset
103 int regno;
kono
parents:
diff changeset
104 /* Hard reg into which the slot pseudos are spilled. The value is
kono
parents:
diff changeset
105 negative for pseudos spilled into memory. */
kono
parents:
diff changeset
106 int hard_regno;
kono
parents:
diff changeset
107 /* Maximum alignment required by all users of the slot. */
kono
parents:
diff changeset
108 unsigned int align;
kono
parents:
diff changeset
109 /* Maximum size required by all users of the slot. */
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
110 poly_int64 size;
111
kono
parents:
diff changeset
111 /* Memory representing the all stack slot. It can be different from
kono
parents:
diff changeset
112 memory representing a pseudo belonging to give stack slot because
kono
parents:
diff changeset
113 pseudo can be placed in a part of the corresponding stack slot.
kono
parents:
diff changeset
114 The value is NULL for pseudos spilled into a hard reg. */
kono
parents:
diff changeset
115 rtx mem;
kono
parents:
diff changeset
116 /* Combined live ranges of all pseudos belonging to given slot. It
kono
parents:
diff changeset
117 is used to figure out that a new spilled pseudo can use given
kono
parents:
diff changeset
118 stack slot. */
kono
parents:
diff changeset
119 lra_live_range_t live_ranges;
kono
parents:
diff changeset
120 };
kono
parents:
diff changeset
121
kono
parents:
diff changeset
122 /* Array containing info about the stack slots. The array element is
kono
parents:
diff changeset
123 indexed by the stack slot number in the range [0..slots_num). */
kono
parents:
diff changeset
124 static struct slot *slots;
kono
parents:
diff changeset
125 /* The number of the stack slots currently existing. */
kono
parents:
diff changeset
126 static int slots_num;
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 /* Set up memory of the spilled pseudo I. The function can allocate
kono
parents:
diff changeset
129 the corresponding stack slot if it is not done yet. */
kono
parents:
diff changeset
130 static void
kono
parents:
diff changeset
131 assign_mem_slot (int i)
kono
parents:
diff changeset
132 {
kono
parents:
diff changeset
133 rtx x = NULL_RTX;
kono
parents:
diff changeset
134 machine_mode mode = GET_MODE (regno_reg_rtx[i]);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
135 poly_int64 inherent_size = PSEUDO_REGNO_BYTES (i);
111
kono
parents:
diff changeset
136 machine_mode wider_mode
kono
parents:
diff changeset
137 = wider_subreg_mode (mode, lra_reg_info[i].biggest_mode);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
138 poly_int64 total_size = GET_MODE_SIZE (wider_mode);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
139 poly_int64 adjust = 0;
111
kono
parents:
diff changeset
140
kono
parents:
diff changeset
141 lra_assert (regno_reg_rtx[i] != NULL_RTX && REG_P (regno_reg_rtx[i])
kono
parents:
diff changeset
142 && lra_reg_info[i].nrefs != 0 && reg_renumber[i] < 0);
kono
parents:
diff changeset
143
kono
parents:
diff changeset
144 unsigned int slot_num = pseudo_slots[i].slot_num;
kono
parents:
diff changeset
145 x = slots[slot_num].mem;
kono
parents:
diff changeset
146 if (!x)
kono
parents:
diff changeset
147 {
kono
parents:
diff changeset
148 x = assign_stack_local (BLKmode, slots[slot_num].size,
kono
parents:
diff changeset
149 slots[slot_num].align);
kono
parents:
diff changeset
150 slots[slot_num].mem = x;
kono
parents:
diff changeset
151 }
kono
parents:
diff changeset
152
kono
parents:
diff changeset
153 /* On a big endian machine, the "address" of the slot is the address
kono
parents:
diff changeset
154 of the low part that fits its inherent mode. */
kono
parents:
diff changeset
155 adjust += subreg_size_lowpart_offset (inherent_size, total_size);
kono
parents:
diff changeset
156 x = adjust_address_nv (x, GET_MODE (regno_reg_rtx[i]), adjust);
kono
parents:
diff changeset
157
kono
parents:
diff changeset
158 /* Set all of the memory attributes as appropriate for a spill. */
kono
parents:
diff changeset
159 set_mem_attrs_for_spill (x);
kono
parents:
diff changeset
160 pseudo_slots[i].mem = x;
kono
parents:
diff changeset
161 }
kono
parents:
diff changeset
162
kono
parents:
diff changeset
163 /* Sort pseudos according their usage frequencies. */
kono
parents:
diff changeset
164 static int
kono
parents:
diff changeset
165 regno_freq_compare (const void *v1p, const void *v2p)
kono
parents:
diff changeset
166 {
kono
parents:
diff changeset
167 const int regno1 = *(const int *) v1p;
kono
parents:
diff changeset
168 const int regno2 = *(const int *) v2p;
kono
parents:
diff changeset
169 int diff;
kono
parents:
diff changeset
170
kono
parents:
diff changeset
171 if ((diff = lra_reg_info[regno2].freq - lra_reg_info[regno1].freq) != 0)
kono
parents:
diff changeset
172 return diff;
kono
parents:
diff changeset
173 return regno1 - regno2;
kono
parents:
diff changeset
174 }
kono
parents:
diff changeset
175
kono
parents:
diff changeset
176 /* Sort pseudos according to their slots, putting the slots in the order
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
177 that they should be allocated.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
178
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
179 First prefer to group slots with variable sizes together and slots
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
180 with constant sizes together, since that usually makes them easier
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
181 to address from a common anchor point. E.g. loads of polynomial-sized
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
182 registers tend to take polynomial offsets while loads of constant-sized
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
183 registers tend to take constant (non-polynomial) offsets.
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
184
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
185 Next, slots with lower numbers have the highest priority and should
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
186 get the smallest displacement from the stack or frame pointer
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
187 (whichever is being used).
111
kono
parents:
diff changeset
188
kono
parents:
diff changeset
189 The first allocated slot is always closest to the frame pointer,
kono
parents:
diff changeset
190 so prefer lower slot numbers when frame_pointer_needed. If the stack
kono
parents:
diff changeset
191 and frame grow in the same direction, then the first allocated slot is
kono
parents:
diff changeset
192 always closest to the initial stack pointer and furthest away from the
kono
parents:
diff changeset
193 final stack pointer, so allocate higher numbers first when using the
kono
parents:
diff changeset
194 stack pointer in that case. The reverse is true if the stack and
kono
parents:
diff changeset
195 frame grow in opposite directions. */
kono
parents:
diff changeset
196 static int
kono
parents:
diff changeset
197 pseudo_reg_slot_compare (const void *v1p, const void *v2p)
kono
parents:
diff changeset
198 {
kono
parents:
diff changeset
199 const int regno1 = *(const int *) v1p;
kono
parents:
diff changeset
200 const int regno2 = *(const int *) v2p;
kono
parents:
diff changeset
201 int diff, slot_num1, slot_num2;
kono
parents:
diff changeset
202
kono
parents:
diff changeset
203 slot_num1 = pseudo_slots[regno1].slot_num;
kono
parents:
diff changeset
204 slot_num2 = pseudo_slots[regno2].slot_num;
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
205 diff = (int (slots[slot_num1].size.is_constant ())
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
206 - int (slots[slot_num2].size.is_constant ()));
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
207 if (diff != 0)
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
208 return diff;
111
kono
parents:
diff changeset
209 if ((diff = slot_num1 - slot_num2) != 0)
kono
parents:
diff changeset
210 return (frame_pointer_needed
kono
parents:
diff changeset
211 || (!FRAME_GROWS_DOWNWARD) == STACK_GROWS_DOWNWARD ? diff : -diff);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
212 poly_int64 total_size1 = GET_MODE_SIZE (lra_reg_info[regno1].biggest_mode);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
213 poly_int64 total_size2 = GET_MODE_SIZE (lra_reg_info[regno2].biggest_mode);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
214 if ((diff = compare_sizes_for_sort (total_size2, total_size1)) != 0)
111
kono
parents:
diff changeset
215 return diff;
kono
parents:
diff changeset
216 return regno1 - regno2;
kono
parents:
diff changeset
217 }
kono
parents:
diff changeset
218
kono
parents:
diff changeset
219 /* Assign spill hard registers to N pseudos in PSEUDO_REGNOS which is
kono
parents:
diff changeset
220 sorted in order of highest frequency first. Put the pseudos which
kono
parents:
diff changeset
221 did not get a spill hard register at the beginning of array
kono
parents:
diff changeset
222 PSEUDO_REGNOS. Return the number of such pseudos. */
kono
parents:
diff changeset
223 static int
kono
parents:
diff changeset
224 assign_spill_hard_regs (int *pseudo_regnos, int n)
kono
parents:
diff changeset
225 {
kono
parents:
diff changeset
226 int i, k, p, regno, res, spill_class_size, hard_regno, nr;
kono
parents:
diff changeset
227 enum reg_class rclass, spill_class;
kono
parents:
diff changeset
228 machine_mode mode;
kono
parents:
diff changeset
229 lra_live_range_t r;
kono
parents:
diff changeset
230 rtx_insn *insn;
kono
parents:
diff changeset
231 rtx set;
kono
parents:
diff changeset
232 basic_block bb;
kono
parents:
diff changeset
233 HARD_REG_SET conflict_hard_regs;
kono
parents:
diff changeset
234 bitmap setjump_crosses = regstat_get_setjmp_crosses ();
kono
parents:
diff changeset
235 /* Hard registers which can not be used for any purpose at given
kono
parents:
diff changeset
236 program point because they are unallocatable or already allocated
kono
parents:
diff changeset
237 for other pseudos. */
kono
parents:
diff changeset
238 HARD_REG_SET *reserved_hard_regs;
kono
parents:
diff changeset
239
kono
parents:
diff changeset
240 if (! lra_reg_spill_p)
kono
parents:
diff changeset
241 return n;
kono
parents:
diff changeset
242 /* Set up reserved hard regs for every program point. */
kono
parents:
diff changeset
243 reserved_hard_regs = XNEWVEC (HARD_REG_SET, lra_live_max_point);
kono
parents:
diff changeset
244 for (p = 0; p < lra_live_max_point; p++)
kono
parents:
diff changeset
245 COPY_HARD_REG_SET (reserved_hard_regs[p], lra_no_alloc_regs);
kono
parents:
diff changeset
246 for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
kono
parents:
diff changeset
247 if (lra_reg_info[i].nrefs != 0
kono
parents:
diff changeset
248 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
kono
parents:
diff changeset
249 for (r = lra_reg_info[i].live_ranges; r != NULL; r = r->next)
kono
parents:
diff changeset
250 for (p = r->start; p <= r->finish; p++)
kono
parents:
diff changeset
251 add_to_hard_reg_set (&reserved_hard_regs[p],
kono
parents:
diff changeset
252 lra_reg_info[i].biggest_mode, hard_regno);
kono
parents:
diff changeset
253 auto_bitmap ok_insn_bitmap (&reg_obstack);
kono
parents:
diff changeset
254 FOR_EACH_BB_FN (bb, cfun)
kono
parents:
diff changeset
255 FOR_BB_INSNS (bb, insn)
kono
parents:
diff changeset
256 if (DEBUG_INSN_P (insn)
kono
parents:
diff changeset
257 || ((set = single_set (insn)) != NULL_RTX
kono
parents:
diff changeset
258 && REG_P (SET_SRC (set)) && REG_P (SET_DEST (set))))
kono
parents:
diff changeset
259 bitmap_set_bit (ok_insn_bitmap, INSN_UID (insn));
kono
parents:
diff changeset
260 for (res = i = 0; i < n; i++)
kono
parents:
diff changeset
261 {
kono
parents:
diff changeset
262 regno = pseudo_regnos[i];
kono
parents:
diff changeset
263 rclass = lra_get_allocno_class (regno);
kono
parents:
diff changeset
264 if (bitmap_bit_p (setjump_crosses, regno)
kono
parents:
diff changeset
265 || (spill_class
kono
parents:
diff changeset
266 = ((enum reg_class)
kono
parents:
diff changeset
267 targetm.spill_class ((reg_class_t) rclass,
kono
parents:
diff changeset
268 PSEUDO_REGNO_MODE (regno)))) == NO_REGS
kono
parents:
diff changeset
269 || bitmap_intersect_compl_p (&lra_reg_info[regno].insn_bitmap,
kono
parents:
diff changeset
270 ok_insn_bitmap))
kono
parents:
diff changeset
271 {
kono
parents:
diff changeset
272 pseudo_regnos[res++] = regno;
kono
parents:
diff changeset
273 continue;
kono
parents:
diff changeset
274 }
kono
parents:
diff changeset
275 lra_assert (spill_class != NO_REGS);
kono
parents:
diff changeset
276 COPY_HARD_REG_SET (conflict_hard_regs,
kono
parents:
diff changeset
277 lra_reg_info[regno].conflict_hard_regs);
kono
parents:
diff changeset
278 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
kono
parents:
diff changeset
279 for (p = r->start; p <= r->finish; p++)
kono
parents:
diff changeset
280 IOR_HARD_REG_SET (conflict_hard_regs, reserved_hard_regs[p]);
kono
parents:
diff changeset
281 spill_class_size = ira_class_hard_regs_num[spill_class];
kono
parents:
diff changeset
282 mode = lra_reg_info[regno].biggest_mode;
kono
parents:
diff changeset
283 for (k = 0; k < spill_class_size; k++)
kono
parents:
diff changeset
284 {
kono
parents:
diff changeset
285 hard_regno = ira_class_hard_regs[spill_class][k];
kono
parents:
diff changeset
286 if (! overlaps_hard_reg_set_p (conflict_hard_regs, mode, hard_regno))
kono
parents:
diff changeset
287 break;
kono
parents:
diff changeset
288 }
kono
parents:
diff changeset
289 if (k >= spill_class_size)
kono
parents:
diff changeset
290 {
kono
parents:
diff changeset
291 /* There is no available regs -- assign memory later. */
kono
parents:
diff changeset
292 pseudo_regnos[res++] = regno;
kono
parents:
diff changeset
293 continue;
kono
parents:
diff changeset
294 }
kono
parents:
diff changeset
295 if (lra_dump_file != NULL)
kono
parents:
diff changeset
296 fprintf (lra_dump_file, " Spill r%d into hr%d\n", regno, hard_regno);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
297 add_to_hard_reg_set (&hard_regs_spilled_into,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
298 lra_reg_info[regno].biggest_mode, hard_regno);
111
kono
parents:
diff changeset
299 /* Update reserved_hard_regs. */
kono
parents:
diff changeset
300 for (r = lra_reg_info[regno].live_ranges; r != NULL; r = r->next)
kono
parents:
diff changeset
301 for (p = r->start; p <= r->finish; p++)
kono
parents:
diff changeset
302 add_to_hard_reg_set (&reserved_hard_regs[p],
kono
parents:
diff changeset
303 lra_reg_info[regno].biggest_mode, hard_regno);
kono
parents:
diff changeset
304 spill_hard_reg[regno]
kono
parents:
diff changeset
305 = gen_raw_REG (PSEUDO_REGNO_MODE (regno), hard_regno);
kono
parents:
diff changeset
306 for (nr = 0;
kono
parents:
diff changeset
307 nr < hard_regno_nregs (hard_regno,
kono
parents:
diff changeset
308 lra_reg_info[regno].biggest_mode);
kono
parents:
diff changeset
309 nr++)
kono
parents:
diff changeset
310 /* Just loop. */
kono
parents:
diff changeset
311 df_set_regs_ever_live (hard_regno + nr, true);
kono
parents:
diff changeset
312 }
kono
parents:
diff changeset
313 free (reserved_hard_regs);
kono
parents:
diff changeset
314 return res;
kono
parents:
diff changeset
315 }
kono
parents:
diff changeset
316
kono
parents:
diff changeset
317 /* Add pseudo REGNO to slot SLOT_NUM. */
kono
parents:
diff changeset
318 static void
kono
parents:
diff changeset
319 add_pseudo_to_slot (int regno, int slot_num)
kono
parents:
diff changeset
320 {
kono
parents:
diff changeset
321 struct pseudo_slot *first;
kono
parents:
diff changeset
322
kono
parents:
diff changeset
323 /* Each pseudo has an inherent size which comes from its own mode,
kono
parents:
diff changeset
324 and a total size which provides room for paradoxical subregs.
kono
parents:
diff changeset
325 We need to make sure the size and alignment of the slot are
kono
parents:
diff changeset
326 sufficient for both. */
kono
parents:
diff changeset
327 machine_mode mode = wider_subreg_mode (PSEUDO_REGNO_MODE (regno),
kono
parents:
diff changeset
328 lra_reg_info[regno].biggest_mode);
kono
parents:
diff changeset
329 unsigned int align = spill_slot_alignment (mode);
kono
parents:
diff changeset
330 slots[slot_num].align = MAX (slots[slot_num].align, align);
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
331 slots[slot_num].size = upper_bound (slots[slot_num].size,
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
332 GET_MODE_SIZE (mode));
111
kono
parents:
diff changeset
333
kono
parents:
diff changeset
334 if (slots[slot_num].regno < 0)
kono
parents:
diff changeset
335 {
kono
parents:
diff changeset
336 /* It is the first pseudo in the slot. */
kono
parents:
diff changeset
337 slots[slot_num].regno = regno;
kono
parents:
diff changeset
338 pseudo_slots[regno].first = &pseudo_slots[regno];
kono
parents:
diff changeset
339 pseudo_slots[regno].next = NULL;
kono
parents:
diff changeset
340 }
kono
parents:
diff changeset
341 else
kono
parents:
diff changeset
342 {
kono
parents:
diff changeset
343 first = pseudo_slots[regno].first = &pseudo_slots[slots[slot_num].regno];
kono
parents:
diff changeset
344 pseudo_slots[regno].next = first->next;
kono
parents:
diff changeset
345 first->next = &pseudo_slots[regno];
kono
parents:
diff changeset
346 }
kono
parents:
diff changeset
347 pseudo_slots[regno].mem = NULL_RTX;
kono
parents:
diff changeset
348 pseudo_slots[regno].slot_num = slot_num;
kono
parents:
diff changeset
349 slots[slot_num].live_ranges
kono
parents:
diff changeset
350 = lra_merge_live_ranges (slots[slot_num].live_ranges,
kono
parents:
diff changeset
351 lra_copy_live_range_list
kono
parents:
diff changeset
352 (lra_reg_info[regno].live_ranges));
kono
parents:
diff changeset
353 }
kono
parents:
diff changeset
354
kono
parents:
diff changeset
355 /* Assign stack slot numbers to pseudos in array PSEUDO_REGNOS of
kono
parents:
diff changeset
356 length N. Sort pseudos in PSEUDO_REGNOS for subsequent assigning
kono
parents:
diff changeset
357 memory stack slots. */
kono
parents:
diff changeset
358 static void
kono
parents:
diff changeset
359 assign_stack_slot_num_and_sort_pseudos (int *pseudo_regnos, int n)
kono
parents:
diff changeset
360 {
kono
parents:
diff changeset
361 int i, j, regno;
kono
parents:
diff changeset
362
kono
parents:
diff changeset
363 slots_num = 0;
kono
parents:
diff changeset
364 /* Assign stack slot numbers to spilled pseudos, use smaller numbers
kono
parents:
diff changeset
365 for most frequently used pseudos. */
kono
parents:
diff changeset
366 for (i = 0; i < n; i++)
kono
parents:
diff changeset
367 {
kono
parents:
diff changeset
368 regno = pseudo_regnos[i];
kono
parents:
diff changeset
369 if (! flag_ira_share_spill_slots)
kono
parents:
diff changeset
370 j = slots_num;
kono
parents:
diff changeset
371 else
kono
parents:
diff changeset
372 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
373 machine_mode mode
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
374 = wider_subreg_mode (PSEUDO_REGNO_MODE (regno),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
375 lra_reg_info[regno].biggest_mode);
111
kono
parents:
diff changeset
376 for (j = 0; j < slots_num; j++)
kono
parents:
diff changeset
377 if (slots[j].hard_regno < 0
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
378 /* Although it's possible to share slots between modes
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
379 with constant and non-constant widths, we usually
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
380 get better spill code by keeping the constant and
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
381 non-constant areas separate. */
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
382 && (GET_MODE_SIZE (mode).is_constant ()
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
383 == slots[j].size.is_constant ())
111
kono
parents:
diff changeset
384 && ! (lra_intersected_live_ranges_p
kono
parents:
diff changeset
385 (slots[j].live_ranges,
kono
parents:
diff changeset
386 lra_reg_info[regno].live_ranges)))
kono
parents:
diff changeset
387 break;
kono
parents:
diff changeset
388 }
kono
parents:
diff changeset
389 if (j >= slots_num)
kono
parents:
diff changeset
390 {
kono
parents:
diff changeset
391 /* New slot. */
kono
parents:
diff changeset
392 slots[j].live_ranges = NULL;
kono
parents:
diff changeset
393 slots[j].size = 0;
kono
parents:
diff changeset
394 slots[j].align = BITS_PER_UNIT;
kono
parents:
diff changeset
395 slots[j].regno = slots[j].hard_regno = -1;
kono
parents:
diff changeset
396 slots[j].mem = NULL_RTX;
kono
parents:
diff changeset
397 slots_num++;
kono
parents:
diff changeset
398 }
kono
parents:
diff changeset
399 add_pseudo_to_slot (regno, j);
kono
parents:
diff changeset
400 }
kono
parents:
diff changeset
401 /* Sort regnos according to their slot numbers. */
kono
parents:
diff changeset
402 qsort (pseudo_regnos, n, sizeof (int), pseudo_reg_slot_compare);
kono
parents:
diff changeset
403 }
kono
parents:
diff changeset
404
kono
parents:
diff changeset
405 /* Recursively process LOC in INSN and change spilled pseudos to the
kono
parents:
diff changeset
406 corresponding memory or spilled hard reg. Ignore spilled pseudos
kono
parents:
diff changeset
407 created from the scratches. Return true if the pseudo nrefs equal
kono
parents:
diff changeset
408 to 0 (don't change the pseudo in this case). Otherwise return false. */
kono
parents:
diff changeset
409 static bool
kono
parents:
diff changeset
410 remove_pseudos (rtx *loc, rtx_insn *insn)
kono
parents:
diff changeset
411 {
kono
parents:
diff changeset
412 int i;
kono
parents:
diff changeset
413 rtx hard_reg;
kono
parents:
diff changeset
414 const char *fmt;
kono
parents:
diff changeset
415 enum rtx_code code;
kono
parents:
diff changeset
416 bool res = false;
kono
parents:
diff changeset
417
kono
parents:
diff changeset
418 if (*loc == NULL_RTX)
kono
parents:
diff changeset
419 return res;
kono
parents:
diff changeset
420 code = GET_CODE (*loc);
kono
parents:
diff changeset
421 if (code == REG && (i = REGNO (*loc)) >= FIRST_PSEUDO_REGISTER
kono
parents:
diff changeset
422 && lra_get_regno_hard_regno (i) < 0
kono
parents:
diff changeset
423 /* We do not want to assign memory for former scratches because
kono
parents:
diff changeset
424 it might result in an address reload for some targets. In
kono
parents:
diff changeset
425 any case we transform such pseudos not getting hard registers
kono
parents:
diff changeset
426 into scratches back. */
kono
parents:
diff changeset
427 && ! lra_former_scratch_p (i))
kono
parents:
diff changeset
428 {
kono
parents:
diff changeset
429 if (lra_reg_info[i].nrefs == 0
kono
parents:
diff changeset
430 && pseudo_slots[i].mem == NULL && spill_hard_reg[i] == NULL)
kono
parents:
diff changeset
431 return true;
kono
parents:
diff changeset
432 if ((hard_reg = spill_hard_reg[i]) != NULL_RTX)
kono
parents:
diff changeset
433 *loc = copy_rtx (hard_reg);
kono
parents:
diff changeset
434 else
kono
parents:
diff changeset
435 {
kono
parents:
diff changeset
436 rtx x = lra_eliminate_regs_1 (insn, pseudo_slots[i].mem,
kono
parents:
diff changeset
437 GET_MODE (pseudo_slots[i].mem),
kono
parents:
diff changeset
438 false, false, 0, true);
kono
parents:
diff changeset
439 *loc = x != pseudo_slots[i].mem ? x : copy_rtx (x);
kono
parents:
diff changeset
440 }
kono
parents:
diff changeset
441 return res;
kono
parents:
diff changeset
442 }
kono
parents:
diff changeset
443
kono
parents:
diff changeset
444 fmt = GET_RTX_FORMAT (code);
kono
parents:
diff changeset
445 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
kono
parents:
diff changeset
446 {
kono
parents:
diff changeset
447 if (fmt[i] == 'e')
kono
parents:
diff changeset
448 res = remove_pseudos (&XEXP (*loc, i), insn) || res;
kono
parents:
diff changeset
449 else if (fmt[i] == 'E')
kono
parents:
diff changeset
450 {
kono
parents:
diff changeset
451 int j;
kono
parents:
diff changeset
452
kono
parents:
diff changeset
453 for (j = XVECLEN (*loc, i) - 1; j >= 0; j--)
kono
parents:
diff changeset
454 res = remove_pseudos (&XVECEXP (*loc, i, j), insn) || res;
kono
parents:
diff changeset
455 }
kono
parents:
diff changeset
456 }
kono
parents:
diff changeset
457 return res;
kono
parents:
diff changeset
458 }
kono
parents:
diff changeset
459
kono
parents:
diff changeset
460 /* Convert spilled pseudos into their stack slots or spill hard regs,
kono
parents:
diff changeset
461 put insns to process on the constraint stack (that is all insns in
kono
parents:
diff changeset
462 which pseudos were changed to memory or spill hard regs). */
kono
parents:
diff changeset
463 static void
kono
parents:
diff changeset
464 spill_pseudos (void)
kono
parents:
diff changeset
465 {
kono
parents:
diff changeset
466 basic_block bb;
kono
parents:
diff changeset
467 rtx_insn *insn, *curr;
kono
parents:
diff changeset
468 int i;
kono
parents:
diff changeset
469
kono
parents:
diff changeset
470 auto_bitmap spilled_pseudos (&reg_obstack);
kono
parents:
diff changeset
471 auto_bitmap changed_insns (&reg_obstack);
kono
parents:
diff changeset
472 for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
kono
parents:
diff changeset
473 {
kono
parents:
diff changeset
474 if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
kono
parents:
diff changeset
475 && ! lra_former_scratch_p (i))
kono
parents:
diff changeset
476 {
kono
parents:
diff changeset
477 bitmap_set_bit (spilled_pseudos, i);
kono
parents:
diff changeset
478 bitmap_ior_into (changed_insns, &lra_reg_info[i].insn_bitmap);
kono
parents:
diff changeset
479 }
kono
parents:
diff changeset
480 }
kono
parents:
diff changeset
481 FOR_EACH_BB_FN (bb, cfun)
kono
parents:
diff changeset
482 {
kono
parents:
diff changeset
483 FOR_BB_INSNS_SAFE (bb, insn, curr)
kono
parents:
diff changeset
484 {
kono
parents:
diff changeset
485 bool removed_pseudo_p = false;
kono
parents:
diff changeset
486
kono
parents:
diff changeset
487 if (bitmap_bit_p (changed_insns, INSN_UID (insn)))
kono
parents:
diff changeset
488 {
kono
parents:
diff changeset
489 rtx *link_loc, link;
kono
parents:
diff changeset
490
kono
parents:
diff changeset
491 removed_pseudo_p = remove_pseudos (&PATTERN (insn), insn);
kono
parents:
diff changeset
492 if (CALL_P (insn)
kono
parents:
diff changeset
493 && remove_pseudos (&CALL_INSN_FUNCTION_USAGE (insn), insn))
kono
parents:
diff changeset
494 removed_pseudo_p = true;
kono
parents:
diff changeset
495 for (link_loc = &REG_NOTES (insn);
kono
parents:
diff changeset
496 (link = *link_loc) != NULL_RTX;
kono
parents:
diff changeset
497 link_loc = &XEXP (link, 1))
kono
parents:
diff changeset
498 {
kono
parents:
diff changeset
499 switch (REG_NOTE_KIND (link))
kono
parents:
diff changeset
500 {
kono
parents:
diff changeset
501 case REG_FRAME_RELATED_EXPR:
kono
parents:
diff changeset
502 case REG_CFA_DEF_CFA:
kono
parents:
diff changeset
503 case REG_CFA_ADJUST_CFA:
kono
parents:
diff changeset
504 case REG_CFA_OFFSET:
kono
parents:
diff changeset
505 case REG_CFA_REGISTER:
kono
parents:
diff changeset
506 case REG_CFA_EXPRESSION:
kono
parents:
diff changeset
507 case REG_CFA_RESTORE:
kono
parents:
diff changeset
508 case REG_CFA_SET_VDRAP:
kono
parents:
diff changeset
509 if (remove_pseudos (&XEXP (link, 0), insn))
kono
parents:
diff changeset
510 removed_pseudo_p = true;
kono
parents:
diff changeset
511 break;
kono
parents:
diff changeset
512 default:
kono
parents:
diff changeset
513 break;
kono
parents:
diff changeset
514 }
kono
parents:
diff changeset
515 }
kono
parents:
diff changeset
516 if (lra_dump_file != NULL)
kono
parents:
diff changeset
517 fprintf (lra_dump_file,
kono
parents:
diff changeset
518 "Changing spilled pseudos to memory in insn #%u\n",
kono
parents:
diff changeset
519 INSN_UID (insn));
kono
parents:
diff changeset
520 lra_push_insn (insn);
kono
parents:
diff changeset
521 if (lra_reg_spill_p || targetm.different_addr_displacement_p ())
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
522 lra_set_used_insn_alternative (insn, LRA_UNKNOWN_ALT);
111
kono
parents:
diff changeset
523 }
kono
parents:
diff changeset
524 else if (CALL_P (insn)
kono
parents:
diff changeset
525 /* Presence of any pseudo in CALL_INSN_FUNCTION_USAGE
kono
parents:
diff changeset
526 does not affect value of insn_bitmap of the
kono
parents:
diff changeset
527 corresponding lra_reg_info. That is because we
kono
parents:
diff changeset
528 don't need to reload pseudos in
kono
parents:
diff changeset
529 CALL_INSN_FUNCTION_USAGEs. So if we process only
kono
parents:
diff changeset
530 insns in the insn_bitmap of given pseudo here, we
kono
parents:
diff changeset
531 can miss the pseudo in some
kono
parents:
diff changeset
532 CALL_INSN_FUNCTION_USAGEs. */
kono
parents:
diff changeset
533 && remove_pseudos (&CALL_INSN_FUNCTION_USAGE (insn), insn))
kono
parents:
diff changeset
534 removed_pseudo_p = true;
kono
parents:
diff changeset
535 if (removed_pseudo_p)
kono
parents:
diff changeset
536 {
kono
parents:
diff changeset
537 lra_assert (DEBUG_INSN_P (insn));
kono
parents:
diff changeset
538 lra_invalidate_insn_data (insn);
kono
parents:
diff changeset
539 INSN_VAR_LOCATION_LOC (insn) = gen_rtx_UNKNOWN_VAR_LOC ();
kono
parents:
diff changeset
540 if (lra_dump_file != NULL)
kono
parents:
diff changeset
541 fprintf (lra_dump_file,
kono
parents:
diff changeset
542 "Debug insn #%u is reset because it referenced "
kono
parents:
diff changeset
543 "removed pseudo\n", INSN_UID (insn));
kono
parents:
diff changeset
544 }
kono
parents:
diff changeset
545 bitmap_and_compl_into (df_get_live_in (bb), spilled_pseudos);
kono
parents:
diff changeset
546 bitmap_and_compl_into (df_get_live_out (bb), spilled_pseudos);
kono
parents:
diff changeset
547 }
kono
parents:
diff changeset
548 }
kono
parents:
diff changeset
549 }
kono
parents:
diff changeset
550
kono
parents:
diff changeset
551 /* Return true if we need to change some pseudos into memory. */
kono
parents:
diff changeset
552 bool
kono
parents:
diff changeset
553 lra_need_for_spills_p (void)
kono
parents:
diff changeset
554 {
kono
parents:
diff changeset
555 int i; max_regno = max_reg_num ();
kono
parents:
diff changeset
556
kono
parents:
diff changeset
557 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
kono
parents:
diff changeset
558 if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
kono
parents:
diff changeset
559 && ! lra_former_scratch_p (i))
kono
parents:
diff changeset
560 return true;
kono
parents:
diff changeset
561 return false;
kono
parents:
diff changeset
562 }
kono
parents:
diff changeset
563
kono
parents:
diff changeset
564 /* Change spilled pseudos into memory or spill hard regs. Put changed
kono
parents:
diff changeset
565 insns on the constraint stack (these insns will be considered on
kono
parents:
diff changeset
566 the next constraint pass). The changed insns are all insns in
kono
parents:
diff changeset
567 which pseudos were changed. */
kono
parents:
diff changeset
568 void
kono
parents:
diff changeset
569 lra_spill (void)
kono
parents:
diff changeset
570 {
kono
parents:
diff changeset
571 int i, n, curr_regno;
kono
parents:
diff changeset
572 int *pseudo_regnos;
kono
parents:
diff changeset
573
kono
parents:
diff changeset
574 regs_num = max_reg_num ();
kono
parents:
diff changeset
575 spill_hard_reg = XNEWVEC (rtx, regs_num);
kono
parents:
diff changeset
576 pseudo_regnos = XNEWVEC (int, regs_num);
kono
parents:
diff changeset
577 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
kono
parents:
diff changeset
578 if (lra_reg_info[i].nrefs != 0 && lra_get_regno_hard_regno (i) < 0
kono
parents:
diff changeset
579 /* We do not want to assign memory for former scratches. */
kono
parents:
diff changeset
580 && ! lra_former_scratch_p (i))
kono
parents:
diff changeset
581 pseudo_regnos[n++] = i;
kono
parents:
diff changeset
582 lra_assert (n > 0);
kono
parents:
diff changeset
583 pseudo_slots = XNEWVEC (struct pseudo_slot, regs_num);
kono
parents:
diff changeset
584 for (i = FIRST_PSEUDO_REGISTER; i < regs_num; i++)
kono
parents:
diff changeset
585 {
kono
parents:
diff changeset
586 spill_hard_reg[i] = NULL_RTX;
kono
parents:
diff changeset
587 pseudo_slots[i].mem = NULL_RTX;
kono
parents:
diff changeset
588 }
kono
parents:
diff changeset
589 slots = XNEWVEC (struct slot, regs_num);
kono
parents:
diff changeset
590 /* Sort regnos according their usage frequencies. */
kono
parents:
diff changeset
591 qsort (pseudo_regnos, n, sizeof (int), regno_freq_compare);
kono
parents:
diff changeset
592 n = assign_spill_hard_regs (pseudo_regnos, n);
kono
parents:
diff changeset
593 assign_stack_slot_num_and_sort_pseudos (pseudo_regnos, n);
kono
parents:
diff changeset
594 for (i = 0; i < n; i++)
kono
parents:
diff changeset
595 if (pseudo_slots[pseudo_regnos[i]].mem == NULL_RTX)
kono
parents:
diff changeset
596 assign_mem_slot (pseudo_regnos[i]);
kono
parents:
diff changeset
597 if (n > 0 && crtl->stack_alignment_needed)
kono
parents:
diff changeset
598 /* If we have a stack frame, we must align it now. The stack size
kono
parents:
diff changeset
599 may be a part of the offset computation for register
kono
parents:
diff changeset
600 elimination. */
kono
parents:
diff changeset
601 assign_stack_local (BLKmode, 0, crtl->stack_alignment_needed);
kono
parents:
diff changeset
602 if (lra_dump_file != NULL)
kono
parents:
diff changeset
603 {
kono
parents:
diff changeset
604 for (i = 0; i < slots_num; i++)
kono
parents:
diff changeset
605 {
131
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
606 fprintf (lra_dump_file, " Slot %d regnos (width = ", i);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
607 print_dec (GET_MODE_SIZE (GET_MODE (slots[i].mem)),
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
608 lra_dump_file, SIGNED);
84e7813d76e9 gcc-8.2
mir3636
parents: 111
diff changeset
609 fprintf (lra_dump_file, "):");
111
kono
parents:
diff changeset
610 for (curr_regno = slots[i].regno;;
kono
parents:
diff changeset
611 curr_regno = pseudo_slots[curr_regno].next - pseudo_slots)
kono
parents:
diff changeset
612 {
kono
parents:
diff changeset
613 fprintf (lra_dump_file, " %d", curr_regno);
kono
parents:
diff changeset
614 if (pseudo_slots[curr_regno].next == NULL)
kono
parents:
diff changeset
615 break;
kono
parents:
diff changeset
616 }
kono
parents:
diff changeset
617 fprintf (lra_dump_file, "\n");
kono
parents:
diff changeset
618 }
kono
parents:
diff changeset
619 }
kono
parents:
diff changeset
620 spill_pseudos ();
kono
parents:
diff changeset
621 free (slots);
kono
parents:
diff changeset
622 free (pseudo_slots);
kono
parents:
diff changeset
623 free (pseudo_regnos);
kono
parents:
diff changeset
624 free (spill_hard_reg);
kono
parents:
diff changeset
625 }
kono
parents:
diff changeset
626
kono
parents:
diff changeset
627 /* Apply alter_subreg for subregs of regs in *LOC. Use FINAL_P for
kono
parents:
diff changeset
628 alter_subreg calls. Return true if any subreg of reg is
kono
parents:
diff changeset
629 processed. */
kono
parents:
diff changeset
630 static bool
kono
parents:
diff changeset
631 alter_subregs (rtx *loc, bool final_p)
kono
parents:
diff changeset
632 {
kono
parents:
diff changeset
633 int i;
kono
parents:
diff changeset
634 rtx x = *loc;
kono
parents:
diff changeset
635 bool res;
kono
parents:
diff changeset
636 const char *fmt;
kono
parents:
diff changeset
637 enum rtx_code code;
kono
parents:
diff changeset
638
kono
parents:
diff changeset
639 if (x == NULL_RTX)
kono
parents:
diff changeset
640 return false;
kono
parents:
diff changeset
641 code = GET_CODE (x);
kono
parents:
diff changeset
642 if (code == SUBREG && REG_P (SUBREG_REG (x)))
kono
parents:
diff changeset
643 {
kono
parents:
diff changeset
644 lra_assert (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER);
kono
parents:
diff changeset
645 alter_subreg (loc, final_p);
kono
parents:
diff changeset
646 return true;
kono
parents:
diff changeset
647 }
kono
parents:
diff changeset
648 fmt = GET_RTX_FORMAT (code);
kono
parents:
diff changeset
649 res = false;
kono
parents:
diff changeset
650 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
kono
parents:
diff changeset
651 {
kono
parents:
diff changeset
652 if (fmt[i] == 'e')
kono
parents:
diff changeset
653 {
kono
parents:
diff changeset
654 if (alter_subregs (&XEXP (x, i), final_p))
kono
parents:
diff changeset
655 res = true;
kono
parents:
diff changeset
656 }
kono
parents:
diff changeset
657 else if (fmt[i] == 'E')
kono
parents:
diff changeset
658 {
kono
parents:
diff changeset
659 int j;
kono
parents:
diff changeset
660
kono
parents:
diff changeset
661 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
kono
parents:
diff changeset
662 if (alter_subregs (&XVECEXP (x, i, j), final_p))
kono
parents:
diff changeset
663 res = true;
kono
parents:
diff changeset
664 }
kono
parents:
diff changeset
665 }
kono
parents:
diff changeset
666 return res;
kono
parents:
diff changeset
667 }
kono
parents:
diff changeset
668
kono
parents:
diff changeset
669 /* Return true if REGNO is used for return in the current
kono
parents:
diff changeset
670 function. */
kono
parents:
diff changeset
671 static bool
kono
parents:
diff changeset
672 return_regno_p (unsigned int regno)
kono
parents:
diff changeset
673 {
kono
parents:
diff changeset
674 rtx outgoing = crtl->return_rtx;
kono
parents:
diff changeset
675
kono
parents:
diff changeset
676 if (! outgoing)
kono
parents:
diff changeset
677 return false;
kono
parents:
diff changeset
678
kono
parents:
diff changeset
679 if (REG_P (outgoing))
kono
parents:
diff changeset
680 return REGNO (outgoing) == regno;
kono
parents:
diff changeset
681 else if (GET_CODE (outgoing) == PARALLEL)
kono
parents:
diff changeset
682 {
kono
parents:
diff changeset
683 int i;
kono
parents:
diff changeset
684
kono
parents:
diff changeset
685 for (i = 0; i < XVECLEN (outgoing, 0); i++)
kono
parents:
diff changeset
686 {
kono
parents:
diff changeset
687 rtx x = XEXP (XVECEXP (outgoing, 0, i), 0);
kono
parents:
diff changeset
688
kono
parents:
diff changeset
689 if (REG_P (x) && REGNO (x) == regno)
kono
parents:
diff changeset
690 return true;
kono
parents:
diff changeset
691 }
kono
parents:
diff changeset
692 }
kono
parents:
diff changeset
693 return false;
kono
parents:
diff changeset
694 }
kono
parents:
diff changeset
695
kono
parents:
diff changeset
696 /* Return true if REGNO is in one of subsequent USE after INSN in the
kono
parents:
diff changeset
697 same BB. */
kono
parents:
diff changeset
698 static bool
kono
parents:
diff changeset
699 regno_in_use_p (rtx_insn *insn, unsigned int regno)
kono
parents:
diff changeset
700 {
kono
parents:
diff changeset
701 static lra_insn_recog_data_t id;
kono
parents:
diff changeset
702 static struct lra_static_insn_data *static_id;
kono
parents:
diff changeset
703 struct lra_insn_reg *reg;
kono
parents:
diff changeset
704 int i, arg_regno;
kono
parents:
diff changeset
705 basic_block bb = BLOCK_FOR_INSN (insn);
kono
parents:
diff changeset
706
kono
parents:
diff changeset
707 while ((insn = next_nondebug_insn (insn)) != NULL_RTX)
kono
parents:
diff changeset
708 {
kono
parents:
diff changeset
709 if (BARRIER_P (insn) || bb != BLOCK_FOR_INSN (insn))
kono
parents:
diff changeset
710 return false;
kono
parents:
diff changeset
711 if (! INSN_P (insn))
kono
parents:
diff changeset
712 continue;
kono
parents:
diff changeset
713 if (GET_CODE (PATTERN (insn)) == USE
kono
parents:
diff changeset
714 && REG_P (XEXP (PATTERN (insn), 0))
kono
parents:
diff changeset
715 && regno == REGNO (XEXP (PATTERN (insn), 0)))
kono
parents:
diff changeset
716 return true;
kono
parents:
diff changeset
717 /* Check that the regno is not modified. */
kono
parents:
diff changeset
718 id = lra_get_insn_recog_data (insn);
kono
parents:
diff changeset
719 for (reg = id->regs; reg != NULL; reg = reg->next)
kono
parents:
diff changeset
720 if (reg->type != OP_IN && reg->regno == (int) regno)
kono
parents:
diff changeset
721 return false;
kono
parents:
diff changeset
722 static_id = id->insn_static_data;
kono
parents:
diff changeset
723 for (reg = static_id->hard_regs; reg != NULL; reg = reg->next)
kono
parents:
diff changeset
724 if (reg->type != OP_IN && reg->regno == (int) regno)
kono
parents:
diff changeset
725 return false;
kono
parents:
diff changeset
726 if (id->arg_hard_regs != NULL)
kono
parents:
diff changeset
727 for (i = 0; (arg_regno = id->arg_hard_regs[i]) >= 0; i++)
kono
parents:
diff changeset
728 if ((int) regno == (arg_regno >= FIRST_PSEUDO_REGISTER
kono
parents:
diff changeset
729 ? arg_regno : arg_regno - FIRST_PSEUDO_REGISTER))
kono
parents:
diff changeset
730 return false;
kono
parents:
diff changeset
731 }
kono
parents:
diff changeset
732 return false;
kono
parents:
diff changeset
733 }
kono
parents:
diff changeset
734
kono
parents:
diff changeset
735 /* Final change of pseudos got hard registers into the corresponding
kono
parents:
diff changeset
736 hard registers and removing temporary clobbers. */
kono
parents:
diff changeset
737 void
kono
parents:
diff changeset
738 lra_final_code_change (void)
kono
parents:
diff changeset
739 {
kono
parents:
diff changeset
740 int i, hard_regno;
kono
parents:
diff changeset
741 basic_block bb;
kono
parents:
diff changeset
742 rtx_insn *insn, *curr;
kono
parents:
diff changeset
743 int max_regno = max_reg_num ();
kono
parents:
diff changeset
744
kono
parents:
diff changeset
745 for (i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
kono
parents:
diff changeset
746 if (lra_reg_info[i].nrefs != 0
kono
parents:
diff changeset
747 && (hard_regno = lra_get_regno_hard_regno (i)) >= 0)
kono
parents:
diff changeset
748 SET_REGNO (regno_reg_rtx[i], hard_regno);
kono
parents:
diff changeset
749 FOR_EACH_BB_FN (bb, cfun)
kono
parents:
diff changeset
750 FOR_BB_INSNS_SAFE (bb, insn, curr)
kono
parents:
diff changeset
751 if (INSN_P (insn))
kono
parents:
diff changeset
752 {
kono
parents:
diff changeset
753 rtx pat = PATTERN (insn);
kono
parents:
diff changeset
754
kono
parents:
diff changeset
755 if (GET_CODE (pat) == CLOBBER && LRA_TEMP_CLOBBER_P (pat))
kono
parents:
diff changeset
756 {
kono
parents:
diff changeset
757 /* Remove clobbers temporarily created in LRA. We don't
kono
parents:
diff changeset
758 need them anymore and don't want to waste compiler
kono
parents:
diff changeset
759 time processing them in a few subsequent passes. */
kono
parents:
diff changeset
760 lra_invalidate_insn_data (insn);
kono
parents:
diff changeset
761 delete_insn (insn);
kono
parents:
diff changeset
762 continue;
kono
parents:
diff changeset
763 }
kono
parents:
diff changeset
764
kono
parents:
diff changeset
765 /* IRA can generate move insns involving pseudos. It is
kono
parents:
diff changeset
766 better remove them earlier to speed up compiler a bit.
kono
parents:
diff changeset
767 It is also better to do it here as they might not pass
kono
parents:
diff changeset
768 final RTL check in LRA, (e.g. insn moving a control
kono
parents:
diff changeset
769 register into itself). So remove an useless move insn
kono
parents:
diff changeset
770 unless next insn is USE marking the return reg (we should
kono
parents:
diff changeset
771 save this as some subsequent optimizations assume that
kono
parents:
diff changeset
772 such original insns are saved). */
kono
parents:
diff changeset
773 if (NONJUMP_INSN_P (insn) && GET_CODE (pat) == SET
kono
parents:
diff changeset
774 && REG_P (SET_SRC (pat)) && REG_P (SET_DEST (pat))
kono
parents:
diff changeset
775 && REGNO (SET_SRC (pat)) == REGNO (SET_DEST (pat))
kono
parents:
diff changeset
776 && (! return_regno_p (REGNO (SET_SRC (pat)))
kono
parents:
diff changeset
777 || ! regno_in_use_p (insn, REGNO (SET_SRC (pat)))))
kono
parents:
diff changeset
778 {
kono
parents:
diff changeset
779 lra_invalidate_insn_data (insn);
kono
parents:
diff changeset
780 delete_insn (insn);
kono
parents:
diff changeset
781 continue;
kono
parents:
diff changeset
782 }
kono
parents:
diff changeset
783
kono
parents:
diff changeset
784 lra_insn_recog_data_t id = lra_get_insn_recog_data (insn);
kono
parents:
diff changeset
785 struct lra_insn_reg *reg;
kono
parents:
diff changeset
786
kono
parents:
diff changeset
787 for (reg = id->regs; reg != NULL; reg = reg->next)
kono
parents:
diff changeset
788 if (reg->regno >= FIRST_PSEUDO_REGISTER
kono
parents:
diff changeset
789 && lra_reg_info [reg->regno].nrefs == 0)
kono
parents:
diff changeset
790 break;
kono
parents:
diff changeset
791
kono
parents:
diff changeset
792 if (reg != NULL)
kono
parents:
diff changeset
793 {
kono
parents:
diff changeset
794 /* Pseudos still can be in debug insns in some very rare
kono
parents:
diff changeset
795 and complicated cases, e.g. the pseudo was removed by
kono
parents:
diff changeset
796 inheritance and the debug insn is not EBBs where the
kono
parents:
diff changeset
797 inheritance happened. It is difficult and time
kono
parents:
diff changeset
798 consuming to find what hard register corresponds the
kono
parents:
diff changeset
799 pseudo -- so just remove the debug insn. Another
kono
parents:
diff changeset
800 solution could be assigning hard reg/memory but it
kono
parents:
diff changeset
801 would be a misleading info. It is better not to have
kono
parents:
diff changeset
802 info than have it wrong. */
kono
parents:
diff changeset
803 lra_assert (DEBUG_INSN_P (insn));
kono
parents:
diff changeset
804 lra_invalidate_insn_data (insn);
kono
parents:
diff changeset
805 delete_insn (insn);
kono
parents:
diff changeset
806 continue;
kono
parents:
diff changeset
807 }
kono
parents:
diff changeset
808
kono
parents:
diff changeset
809 struct lra_static_insn_data *static_id = id->insn_static_data;
kono
parents:
diff changeset
810 bool insn_change_p = false;
kono
parents:
diff changeset
811
kono
parents:
diff changeset
812 for (i = id->insn_static_data->n_operands - 1; i >= 0; i--)
kono
parents:
diff changeset
813 if ((DEBUG_INSN_P (insn) || ! static_id->operand[i].is_operator)
kono
parents:
diff changeset
814 && alter_subregs (id->operand_loc[i], ! DEBUG_INSN_P (insn)))
kono
parents:
diff changeset
815 {
kono
parents:
diff changeset
816 lra_update_dup (id, i);
kono
parents:
diff changeset
817 insn_change_p = true;
kono
parents:
diff changeset
818 }
kono
parents:
diff changeset
819 if (insn_change_p)
kono
parents:
diff changeset
820 lra_update_operator_dups (id);
kono
parents:
diff changeset
821 }
kono
parents:
diff changeset
822 }