OpenACC acc_on_device.
[official-gcc.git] / gcc / ira.c
bloba48368ccced2d13f3e7b27f3267757103e290781
1 /* Integrated Register Allocator (IRA) entry point.
2 Copyright (C) 2006-2014 Free Software Foundation, Inc.
3 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 /* The integrated register allocator (IRA) is a
22 regional register allocator performing graph coloring on a top-down
23 traversal of nested regions. Graph coloring in a region is based
24 on Chaitin-Briggs algorithm. It is called integrated because
25 register coalescing, register live range splitting, and choosing a
26 better hard register are done on-the-fly during coloring. Register
27 coalescing and choosing a cheaper hard register is done by hard
28 register preferencing during hard register assigning. The live
29 range splitting is a byproduct of the regional register allocation.
31 Major IRA notions are:
33 o *Region* is a part of CFG where graph coloring based on
34 Chaitin-Briggs algorithm is done. IRA can work on any set of
35 nested CFG regions forming a tree. Currently the regions are
36 the entire function for the root region and natural loops for
37 the other regions. Therefore data structure representing a
38 region is called loop_tree_node.
40 o *Allocno class* is a register class used for allocation of
41 given allocno. It means that only hard register of given
42 register class can be assigned to given allocno. In reality,
43 even smaller subset of (*profitable*) hard registers can be
44 assigned. In rare cases, the subset can be even smaller
45 because our modification of Chaitin-Briggs algorithm requires
46 that sets of hard registers can be assigned to allocnos forms a
47 forest, i.e. the sets can be ordered in a way where any
48 previous set is not intersected with given set or is a superset
49 of given set.
51 o *Pressure class* is a register class belonging to a set of
52 register classes containing all of the hard-registers available
53 for register allocation. The set of all pressure classes for a
54 target is defined in the corresponding machine-description file
55 according some criteria. Register pressure is calculated only
56 for pressure classes and it affects some IRA decisions as
57 forming allocation regions.
59 o *Allocno* represents the live range of a pseudo-register in a
60 region. Besides the obvious attributes like the corresponding
61 pseudo-register number, allocno class, conflicting allocnos and
62 conflicting hard-registers, there are a few allocno attributes
63 which are important for understanding the allocation algorithm:
65 - *Live ranges*. This is a list of ranges of *program points*
66 where the allocno lives. Program points represent places
67 where a pseudo can be born or become dead (there are
68 approximately two times more program points than the insns)
69 and they are represented by integers starting with 0. The
70 live ranges are used to find conflicts between allocnos.
71 They also play very important role for the transformation of
72 the IRA internal representation of several regions into a one
73 region representation. The later is used during the reload
74 pass work because each allocno represents all of the
75 corresponding pseudo-registers.
77 - *Hard-register costs*. This is a vector of size equal to the
78 number of available hard-registers of the allocno class. The
79 cost of a callee-clobbered hard-register for an allocno is
80 increased by the cost of save/restore code around the calls
81 through the given allocno's life. If the allocno is a move
82 instruction operand and another operand is a hard-register of
83 the allocno class, the cost of the hard-register is decreased
84 by the move cost.
86 When an allocno is assigned, the hard-register with minimal
87 full cost is used. Initially, a hard-register's full cost is
88 the corresponding value from the hard-register's cost vector.
89 If the allocno is connected by a *copy* (see below) to
90 another allocno which has just received a hard-register, the
91 cost of the hard-register is decreased. Before choosing a
92 hard-register for an allocno, the allocno's current costs of
93 the hard-registers are modified by the conflict hard-register
94 costs of all of the conflicting allocnos which are not
95 assigned yet.
97 - *Conflict hard-register costs*. This is a vector of the same
98 size as the hard-register costs vector. To permit an
99 unassigned allocno to get a better hard-register, IRA uses
100 this vector to calculate the final full cost of the
101 available hard-registers. Conflict hard-register costs of an
102 unassigned allocno are also changed with a change of the
103 hard-register cost of the allocno when a copy involving the
104 allocno is processed as described above. This is done to
105 show other unassigned allocnos that a given allocno prefers
106 some hard-registers in order to remove the move instruction
107 corresponding to the copy.
109 o *Cap*. If a pseudo-register does not live in a region but
110 lives in a nested region, IRA creates a special allocno called
111 a cap in the outer region. A region cap is also created for a
112 subregion cap.
114 o *Copy*. Allocnos can be connected by copies. Copies are used
115 to modify hard-register costs for allocnos during coloring.
116 Such modifications reflects a preference to use the same
117 hard-register for the allocnos connected by copies. Usually
118 copies are created for move insns (in this case it results in
119 register coalescing). But IRA also creates copies for operands
120 of an insn which should be assigned to the same hard-register
121 due to constraints in the machine description (it usually
122 results in removing a move generated in reload to satisfy
123 the constraints) and copies referring to the allocno which is
124 the output operand of an instruction and the allocno which is
125 an input operand dying in the instruction (creation of such
126 copies results in less register shuffling). IRA *does not*
127 create copies between the same register allocnos from different
128 regions because we use another technique for propagating
129 hard-register preference on the borders of regions.
131 Allocnos (including caps) for the upper region in the region tree
132 *accumulate* information important for coloring from allocnos with
133 the same pseudo-register from nested regions. This includes
134 hard-register and memory costs, conflicts with hard-registers,
135 allocno conflicts, allocno copies and more. *Thus, attributes for
136 allocnos in a region have the same values as if the region had no
137 subregions*. It means that attributes for allocnos in the
138 outermost region corresponding to the function have the same values
139 as though the allocation used only one region which is the entire
140 function. It also means that we can look at IRA work as if the
141 first IRA did allocation for all function then it improved the
142 allocation for loops then their subloops and so on.
144 IRA major passes are:
146 o Building IRA internal representation which consists of the
147 following subpasses:
149 * First, IRA builds regions and creates allocnos (file
150 ira-build.c) and initializes most of their attributes.
152 * Then IRA finds an allocno class for each allocno and
153 calculates its initial (non-accumulated) cost of memory and
154 each hard-register of its allocno class (file ira-cost.c).
156 * IRA creates live ranges of each allocno, calulates register
157 pressure for each pressure class in each region, sets up
158 conflict hard registers for each allocno and info about calls
159 the allocno lives through (file ira-lives.c).
161 * IRA removes low register pressure loops from the regions
162 mostly to speed IRA up (file ira-build.c).
164 * IRA propagates accumulated allocno info from lower region
165 allocnos to corresponding upper region allocnos (file
166 ira-build.c).
168 * IRA creates all caps (file ira-build.c).
170 * Having live-ranges of allocnos and their classes, IRA creates
171 conflicting allocnos for each allocno. Conflicting allocnos
172 are stored as a bit vector or array of pointers to the
173 conflicting allocnos whatever is more profitable (file
174 ira-conflicts.c). At this point IRA creates allocno copies.
176 o Coloring. Now IRA has all necessary info to start graph coloring
177 process. It is done in each region on top-down traverse of the
178 region tree (file ira-color.c). There are following subpasses:
180 * Finding profitable hard registers of corresponding allocno
181 class for each allocno. For example, only callee-saved hard
182 registers are frequently profitable for allocnos living
183 through colors. If the profitable hard register set of
184 allocno does not form a tree based on subset relation, we use
185 some approximation to form the tree. This approximation is
186 used to figure out trivial colorability of allocnos. The
187 approximation is a pretty rare case.
189 * Putting allocnos onto the coloring stack. IRA uses Briggs
190 optimistic coloring which is a major improvement over
191 Chaitin's coloring. Therefore IRA does not spill allocnos at
192 this point. There is some freedom in the order of putting
193 allocnos on the stack which can affect the final result of
194 the allocation. IRA uses some heuristics to improve the
195 order. The major one is to form *threads* from colorable
196 allocnos and push them on the stack by threads. Thread is a
197 set of non-conflicting colorable allocnos connected by
198 copies. The thread contains allocnos from the colorable
199 bucket or colorable allocnos already pushed onto the coloring
200 stack. Pushing thread allocnos one after another onto the
201 stack increases chances of removing copies when the allocnos
202 get the same hard reg.
204 We also use a modification of Chaitin-Briggs algorithm which
205 works for intersected register classes of allocnos. To
206 figure out trivial colorability of allocnos, the mentioned
207 above tree of hard register sets is used. To get an idea how
208 the algorithm works in i386 example, let us consider an
209 allocno to which any general hard register can be assigned.
210 If the allocno conflicts with eight allocnos to which only
211 EAX register can be assigned, given allocno is still
212 trivially colorable because all conflicting allocnos might be
213 assigned only to EAX and all other general hard registers are
214 still free.
216 To get an idea of the used trivial colorability criterion, it
217 is also useful to read article "Graph-Coloring Register
218 Allocation for Irregular Architectures" by Michael D. Smith
219 and Glen Holloway. Major difference between the article
220 approach and approach used in IRA is that Smith's approach
221 takes register classes only from machine description and IRA
222 calculate register classes from intermediate code too
223 (e.g. an explicit usage of hard registers in RTL code for
224 parameter passing can result in creation of additional
225 register classes which contain or exclude the hard
226 registers). That makes IRA approach useful for improving
227 coloring even for architectures with regular register files
228 and in fact some benchmarking shows the improvement for
229 regular class architectures is even bigger than for irregular
230 ones. Another difference is that Smith's approach chooses
231 intersection of classes of all insn operands in which a given
232 pseudo occurs. IRA can use bigger classes if it is still
233 more profitable than memory usage.
235 * Popping the allocnos from the stack and assigning them hard
236 registers. If IRA can not assign a hard register to an
237 allocno and the allocno is coalesced, IRA undoes the
238 coalescing and puts the uncoalesced allocnos onto the stack in
239 the hope that some such allocnos will get a hard register
240 separately. If IRA fails to assign hard register or memory
241 is more profitable for it, IRA spills the allocno. IRA
242 assigns the allocno the hard-register with minimal full
243 allocation cost which reflects the cost of usage of the
244 hard-register for the allocno and cost of usage of the
245 hard-register for allocnos conflicting with given allocno.
247 * Chaitin-Briggs coloring assigns as many pseudos as possible
248 to hard registers. After coloringh we try to improve
249 allocation with cost point of view. We improve the
250 allocation by spilling some allocnos and assigning the freed
251 hard registers to other allocnos if it decreases the overall
252 allocation cost.
254 * After allocno assigning in the region, IRA modifies the hard
255 register and memory costs for the corresponding allocnos in
256 the subregions to reflect the cost of possible loads, stores,
257 or moves on the border of the region and its subregions.
258 When default regional allocation algorithm is used
259 (-fira-algorithm=mixed), IRA just propagates the assignment
260 for allocnos if the register pressure in the region for the
261 corresponding pressure class is less than number of available
262 hard registers for given pressure class.
264 o Spill/restore code moving. When IRA performs an allocation
265 by traversing regions in top-down order, it does not know what
266 happens below in the region tree. Therefore, sometimes IRA
267 misses opportunities to perform a better allocation. A simple
268 optimization tries to improve allocation in a region having
269 subregions and containing in another region. If the
270 corresponding allocnos in the subregion are spilled, it spills
271 the region allocno if it is profitable. The optimization
272 implements a simple iterative algorithm performing profitable
273 transformations while they are still possible. It is fast in
274 practice, so there is no real need for a better time complexity
275 algorithm.
277 o Code change. After coloring, two allocnos representing the
278 same pseudo-register outside and inside a region respectively
279 may be assigned to different locations (hard-registers or
280 memory). In this case IRA creates and uses a new
281 pseudo-register inside the region and adds code to move allocno
282 values on the region's borders. This is done during top-down
283 traversal of the regions (file ira-emit.c). In some
284 complicated cases IRA can create a new allocno to move allocno
285 values (e.g. when a swap of values stored in two hard-registers
286 is needed). At this stage, the new allocno is marked as
287 spilled. IRA still creates the pseudo-register and the moves
288 on the region borders even when both allocnos were assigned to
289 the same hard-register. If the reload pass spills a
290 pseudo-register for some reason, the effect will be smaller
291 because another allocno will still be in the hard-register. In
292 most cases, this is better then spilling both allocnos. If
293 reload does not change the allocation for the two
294 pseudo-registers, the trivial move will be removed by
295 post-reload optimizations. IRA does not generate moves for
296 allocnos assigned to the same hard register when the default
297 regional allocation algorithm is used and the register pressure
298 in the region for the corresponding pressure class is less than
299 number of available hard registers for given pressure class.
300 IRA also does some optimizations to remove redundant stores and
301 to reduce code duplication on the region borders.
303 o Flattening internal representation. After changing code, IRA
304 transforms its internal representation for several regions into
305 one region representation (file ira-build.c). This process is
306 called IR flattening. Such process is more complicated than IR
307 rebuilding would be, but is much faster.
309 o After IR flattening, IRA tries to assign hard registers to all
310 spilled allocnos. This is impelemented by a simple and fast
311 priority coloring algorithm (see function
312 ira_reassign_conflict_allocnos::ira-color.c). Here new allocnos
313 created during the code change pass can be assigned to hard
314 registers.
316 o At the end IRA calls the reload pass. The reload pass
317 communicates with IRA through several functions in file
318 ira-color.c to improve its decisions in
320 * sharing stack slots for the spilled pseudos based on IRA info
321 about pseudo-register conflicts.
323 * reassigning hard-registers to all spilled pseudos at the end
324 of each reload iteration.
326 * choosing a better hard-register to spill based on IRA info
327 about pseudo-register live ranges and the register pressure
328 in places where the pseudo-register lives.
330 IRA uses a lot of data representing the target processors. These
331 data are initilized in file ira.c.
333 If function has no loops (or the loops are ignored when
334 -fira-algorithm=CB is used), we have classic Chaitin-Briggs
335 coloring (only instead of separate pass of coalescing, we use hard
336 register preferencing). In such case, IRA works much faster
337 because many things are not made (like IR flattening, the
338 spill/restore optimization, and the code change).
340 Literature is worth to read for better understanding the code:
342 o Preston Briggs, Keith D. Cooper, Linda Torczon. Improvements to
343 Graph Coloring Register Allocation.
345 o David Callahan, Brian Koblenz. Register allocation via
346 hierarchical graph coloring.
348 o Keith Cooper, Anshuman Dasgupta, Jason Eckhardt. Revisiting Graph
349 Coloring Register Allocation: A Study of the Chaitin-Briggs and
350 Callahan-Koblenz Algorithms.
352 o Guei-Yuan Lueh, Thomas Gross, and Ali-Reza Adl-Tabatabai. Global
353 Register Allocation Based on Graph Fusion.
355 o Michael D. Smith and Glenn Holloway. Graph-Coloring Register
356 Allocation for Irregular Architectures
358 o Vladimir Makarov. The Integrated Register Allocator for GCC.
360 o Vladimir Makarov. The top-down register allocator for irregular
361 register file architectures.
366 #include "config.h"
367 #include "system.h"
368 #include "coretypes.h"
369 #include "tm.h"
370 #include "regs.h"
371 #include "tree.h"
372 #include "rtl.h"
373 #include "tm_p.h"
374 #include "target.h"
375 #include "flags.h"
376 #include "obstack.h"
377 #include "bitmap.h"
378 #include "hard-reg-set.h"
379 #include "basic-block.h"
380 #include "df.h"
381 #include "expr.h"
382 #include "recog.h"
383 #include "params.h"
384 #include "tree-pass.h"
385 #include "output.h"
386 #include "except.h"
387 #include "reload.h"
388 #include "diagnostic-core.h"
389 #include "function.h"
390 #include "ggc.h"
391 #include "ira-int.h"
392 #include "lra.h"
393 #include "dce.h"
394 #include "dbgcnt.h"
395 #include "rtl-iter.h"
397 struct target_ira default_target_ira;
398 struct target_ira_int default_target_ira_int;
399 #if SWITCHABLE_TARGET
400 struct target_ira *this_target_ira = &default_target_ira;
401 struct target_ira_int *this_target_ira_int = &default_target_ira_int;
402 #endif
404 /* A modified value of flag `-fira-verbose' used internally. */
405 int internal_flag_ira_verbose;
407 /* Dump file of the allocator if it is not NULL. */
408 FILE *ira_dump_file;
410 /* The number of elements in the following array. */
411 int ira_spilled_reg_stack_slots_num;
413 /* The following array contains info about spilled pseudo-registers
414 stack slots used in current function so far. */
415 struct ira_spilled_reg_stack_slot *ira_spilled_reg_stack_slots;
417 /* Correspondingly overall cost of the allocation, overall cost before
418 reload, cost of the allocnos assigned to hard-registers, cost of
419 the allocnos assigned to memory, cost of loads, stores and register
420 move insns generated for pseudo-register live range splitting (see
421 ira-emit.c). */
422 int ira_overall_cost, overall_cost_before;
423 int ira_reg_cost, ira_mem_cost;
424 int ira_load_cost, ira_store_cost, ira_shuffle_cost;
425 int ira_move_loops_num, ira_additional_jumps_num;
427 /* All registers that can be eliminated. */
429 HARD_REG_SET eliminable_regset;
431 /* Value of max_reg_num () before IRA work start. This value helps
432 us to recognize a situation when new pseudos were created during
433 IRA work. */
434 static int max_regno_before_ira;
436 /* Temporary hard reg set used for a different calculation. */
437 static HARD_REG_SET temp_hard_regset;
439 #define last_mode_for_init_move_cost \
440 (this_target_ira_int->x_last_mode_for_init_move_cost)
443 /* The function sets up the map IRA_REG_MODE_HARD_REGSET. */
444 static void
445 setup_reg_mode_hard_regset (void)
447 int i, m, hard_regno;
449 for (m = 0; m < NUM_MACHINE_MODES; m++)
450 for (hard_regno = 0; hard_regno < FIRST_PSEUDO_REGISTER; hard_regno++)
452 CLEAR_HARD_REG_SET (ira_reg_mode_hard_regset[hard_regno][m]);
453 for (i = hard_regno_nregs[hard_regno][m] - 1; i >= 0; i--)
454 if (hard_regno + i < FIRST_PSEUDO_REGISTER)
455 SET_HARD_REG_BIT (ira_reg_mode_hard_regset[hard_regno][m],
456 hard_regno + i);
461 #define no_unit_alloc_regs \
462 (this_target_ira_int->x_no_unit_alloc_regs)
464 /* The function sets up the three arrays declared above. */
465 static void
466 setup_class_hard_regs (void)
468 int cl, i, hard_regno, n;
469 HARD_REG_SET processed_hard_reg_set;
471 ira_assert (SHRT_MAX >= FIRST_PSEUDO_REGISTER);
472 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
474 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
475 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
476 CLEAR_HARD_REG_SET (processed_hard_reg_set);
477 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
479 ira_non_ordered_class_hard_regs[cl][i] = -1;
480 ira_class_hard_reg_index[cl][i] = -1;
482 for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
484 #ifdef REG_ALLOC_ORDER
485 hard_regno = reg_alloc_order[i];
486 #else
487 hard_regno = i;
488 #endif
489 if (TEST_HARD_REG_BIT (processed_hard_reg_set, hard_regno))
490 continue;
491 SET_HARD_REG_BIT (processed_hard_reg_set, hard_regno);
492 if (! TEST_HARD_REG_BIT (temp_hard_regset, hard_regno))
493 ira_class_hard_reg_index[cl][hard_regno] = -1;
494 else
496 ira_class_hard_reg_index[cl][hard_regno] = n;
497 ira_class_hard_regs[cl][n++] = hard_regno;
500 ira_class_hard_regs_num[cl] = n;
501 for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
502 if (TEST_HARD_REG_BIT (temp_hard_regset, i))
503 ira_non_ordered_class_hard_regs[cl][n++] = i;
504 ira_assert (ira_class_hard_regs_num[cl] == n);
508 /* Set up global variables defining info about hard registers for the
509 allocation. These depend on USE_HARD_FRAME_P whose TRUE value means
510 that we can use the hard frame pointer for the allocation. */
511 static void
512 setup_alloc_regs (bool use_hard_frame_p)
514 #ifdef ADJUST_REG_ALLOC_ORDER
515 ADJUST_REG_ALLOC_ORDER;
516 #endif
517 COPY_HARD_REG_SET (no_unit_alloc_regs, fixed_reg_set);
518 if (! use_hard_frame_p)
519 SET_HARD_REG_BIT (no_unit_alloc_regs, HARD_FRAME_POINTER_REGNUM);
520 setup_class_hard_regs ();
525 #define alloc_reg_class_subclasses \
526 (this_target_ira_int->x_alloc_reg_class_subclasses)
528 /* Initialize the table of subclasses of each reg class. */
529 static void
530 setup_reg_subclasses (void)
532 int i, j;
533 HARD_REG_SET temp_hard_regset2;
535 for (i = 0; i < N_REG_CLASSES; i++)
536 for (j = 0; j < N_REG_CLASSES; j++)
537 alloc_reg_class_subclasses[i][j] = LIM_REG_CLASSES;
539 for (i = 0; i < N_REG_CLASSES; i++)
541 if (i == (int) NO_REGS)
542 continue;
544 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
545 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
546 if (hard_reg_set_empty_p (temp_hard_regset))
547 continue;
548 for (j = 0; j < N_REG_CLASSES; j++)
549 if (i != j)
551 enum reg_class *p;
553 COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[j]);
554 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
555 if (! hard_reg_set_subset_p (temp_hard_regset,
556 temp_hard_regset2))
557 continue;
558 p = &alloc_reg_class_subclasses[j][0];
559 while (*p != LIM_REG_CLASSES) p++;
560 *p = (enum reg_class) i;
567 /* Set up IRA_MEMORY_MOVE_COST and IRA_MAX_MEMORY_MOVE_COST. */
568 static void
569 setup_class_subset_and_memory_move_costs (void)
571 int cl, cl2, mode, cost;
572 HARD_REG_SET temp_hard_regset2;
574 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
575 ira_memory_move_cost[mode][NO_REGS][0]
576 = ira_memory_move_cost[mode][NO_REGS][1] = SHRT_MAX;
577 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
579 if (cl != (int) NO_REGS)
580 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
582 ira_max_memory_move_cost[mode][cl][0]
583 = ira_memory_move_cost[mode][cl][0]
584 = memory_move_cost ((enum machine_mode) mode,
585 (reg_class_t) cl, false);
586 ira_max_memory_move_cost[mode][cl][1]
587 = ira_memory_move_cost[mode][cl][1]
588 = memory_move_cost ((enum machine_mode) mode,
589 (reg_class_t) cl, true);
590 /* Costs for NO_REGS are used in cost calculation on the
591 1st pass when the preferred register classes are not
592 known yet. In this case we take the best scenario. */
593 if (ira_memory_move_cost[mode][NO_REGS][0]
594 > ira_memory_move_cost[mode][cl][0])
595 ira_max_memory_move_cost[mode][NO_REGS][0]
596 = ira_memory_move_cost[mode][NO_REGS][0]
597 = ira_memory_move_cost[mode][cl][0];
598 if (ira_memory_move_cost[mode][NO_REGS][1]
599 > ira_memory_move_cost[mode][cl][1])
600 ira_max_memory_move_cost[mode][NO_REGS][1]
601 = ira_memory_move_cost[mode][NO_REGS][1]
602 = ira_memory_move_cost[mode][cl][1];
605 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
606 for (cl2 = (int) N_REG_CLASSES - 1; cl2 >= 0; cl2--)
608 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
609 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
610 COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl2]);
611 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
612 ira_class_subset_p[cl][cl2]
613 = hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2);
614 if (! hard_reg_set_empty_p (temp_hard_regset2)
615 && hard_reg_set_subset_p (reg_class_contents[cl2],
616 reg_class_contents[cl]))
617 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
619 cost = ira_memory_move_cost[mode][cl2][0];
620 if (cost > ira_max_memory_move_cost[mode][cl][0])
621 ira_max_memory_move_cost[mode][cl][0] = cost;
622 cost = ira_memory_move_cost[mode][cl2][1];
623 if (cost > ira_max_memory_move_cost[mode][cl][1])
624 ira_max_memory_move_cost[mode][cl][1] = cost;
627 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
628 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
630 ira_memory_move_cost[mode][cl][0]
631 = ira_max_memory_move_cost[mode][cl][0];
632 ira_memory_move_cost[mode][cl][1]
633 = ira_max_memory_move_cost[mode][cl][1];
635 setup_reg_subclasses ();
640 /* Define the following macro if allocation through malloc if
641 preferable. */
642 #define IRA_NO_OBSTACK
644 #ifndef IRA_NO_OBSTACK
645 /* Obstack used for storing all dynamic data (except bitmaps) of the
646 IRA. */
647 static struct obstack ira_obstack;
648 #endif
650 /* Obstack used for storing all bitmaps of the IRA. */
651 static struct bitmap_obstack ira_bitmap_obstack;
653 /* Allocate memory of size LEN for IRA data. */
654 void *
655 ira_allocate (size_t len)
657 void *res;
659 #ifndef IRA_NO_OBSTACK
660 res = obstack_alloc (&ira_obstack, len);
661 #else
662 res = xmalloc (len);
663 #endif
664 return res;
667 /* Free memory ADDR allocated for IRA data. */
668 void
669 ira_free (void *addr ATTRIBUTE_UNUSED)
671 #ifndef IRA_NO_OBSTACK
672 /* do nothing */
673 #else
674 free (addr);
675 #endif
679 /* Allocate and returns bitmap for IRA. */
680 bitmap
681 ira_allocate_bitmap (void)
683 return BITMAP_ALLOC (&ira_bitmap_obstack);
686 /* Free bitmap B allocated for IRA. */
687 void
688 ira_free_bitmap (bitmap b ATTRIBUTE_UNUSED)
690 /* do nothing */
695 /* Output information about allocation of all allocnos (except for
696 caps) into file F. */
697 void
698 ira_print_disposition (FILE *f)
700 int i, n, max_regno;
701 ira_allocno_t a;
702 basic_block bb;
704 fprintf (f, "Disposition:");
705 max_regno = max_reg_num ();
706 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
707 for (a = ira_regno_allocno_map[i];
708 a != NULL;
709 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
711 if (n % 4 == 0)
712 fprintf (f, "\n");
713 n++;
714 fprintf (f, " %4d:r%-4d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
715 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
716 fprintf (f, "b%-3d", bb->index);
717 else
718 fprintf (f, "l%-3d", ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
719 if (ALLOCNO_HARD_REGNO (a) >= 0)
720 fprintf (f, " %3d", ALLOCNO_HARD_REGNO (a));
721 else
722 fprintf (f, " mem");
724 fprintf (f, "\n");
727 /* Outputs information about allocation of all allocnos into
728 stderr. */
729 void
730 ira_debug_disposition (void)
732 ira_print_disposition (stderr);
737 /* Set up ira_stack_reg_pressure_class which is the biggest pressure
738 register class containing stack registers or NO_REGS if there are
739 no stack registers. To find this class, we iterate through all
740 register pressure classes and choose the first register pressure
741 class containing all the stack registers and having the biggest
742 size. */
743 static void
744 setup_stack_reg_pressure_class (void)
746 ira_stack_reg_pressure_class = NO_REGS;
747 #ifdef STACK_REGS
749 int i, best, size;
750 enum reg_class cl;
751 HARD_REG_SET temp_hard_regset2;
753 CLEAR_HARD_REG_SET (temp_hard_regset);
754 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
755 SET_HARD_REG_BIT (temp_hard_regset, i);
756 best = 0;
757 for (i = 0; i < ira_pressure_classes_num; i++)
759 cl = ira_pressure_classes[i];
760 COPY_HARD_REG_SET (temp_hard_regset2, temp_hard_regset);
761 AND_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
762 size = hard_reg_set_size (temp_hard_regset2);
763 if (best < size)
765 best = size;
766 ira_stack_reg_pressure_class = cl;
770 #endif
773 /* Find pressure classes which are register classes for which we
774 calculate register pressure in IRA, register pressure sensitive
775 insn scheduling, and register pressure sensitive loop invariant
776 motion.
778 To make register pressure calculation easy, we always use
779 non-intersected register pressure classes. A move of hard
780 registers from one register pressure class is not more expensive
781 than load and store of the hard registers. Most likely an allocno
782 class will be a subset of a register pressure class and in many
783 cases a register pressure class. That makes usage of register
784 pressure classes a good approximation to find a high register
785 pressure. */
786 static void
787 setup_pressure_classes (void)
789 int cost, i, n, curr;
790 int cl, cl2;
791 enum reg_class pressure_classes[N_REG_CLASSES];
792 int m;
793 HARD_REG_SET temp_hard_regset2;
794 bool insert_p;
796 n = 0;
797 for (cl = 0; cl < N_REG_CLASSES; cl++)
799 if (ira_class_hard_regs_num[cl] == 0)
800 continue;
801 if (ira_class_hard_regs_num[cl] != 1
802 /* A register class without subclasses may contain a few
803 hard registers and movement between them is costly
804 (e.g. SPARC FPCC registers). We still should consider it
805 as a candidate for a pressure class. */
806 && alloc_reg_class_subclasses[cl][0] < cl)
808 /* Check that the moves between any hard registers of the
809 current class are not more expensive for a legal mode
810 than load/store of the hard registers of the current
811 class. Such class is a potential candidate to be a
812 register pressure class. */
813 for (m = 0; m < NUM_MACHINE_MODES; m++)
815 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
816 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
817 AND_COMPL_HARD_REG_SET (temp_hard_regset,
818 ira_prohibited_class_mode_regs[cl][m]);
819 if (hard_reg_set_empty_p (temp_hard_regset))
820 continue;
821 ira_init_register_move_cost_if_necessary ((enum machine_mode) m);
822 cost = ira_register_move_cost[m][cl][cl];
823 if (cost <= ira_max_memory_move_cost[m][cl][1]
824 || cost <= ira_max_memory_move_cost[m][cl][0])
825 break;
827 if (m >= NUM_MACHINE_MODES)
828 continue;
830 curr = 0;
831 insert_p = true;
832 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
833 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
834 /* Remove so far added pressure classes which are subset of the
835 current candidate class. Prefer GENERAL_REGS as a pressure
836 register class to another class containing the same
837 allocatable hard registers. We do this because machine
838 dependent cost hooks might give wrong costs for the latter
839 class but always give the right cost for the former class
840 (GENERAL_REGS). */
841 for (i = 0; i < n; i++)
843 cl2 = pressure_classes[i];
844 COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl2]);
845 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
846 if (hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2)
847 && (! hard_reg_set_equal_p (temp_hard_regset, temp_hard_regset2)
848 || cl2 == (int) GENERAL_REGS))
850 pressure_classes[curr++] = (enum reg_class) cl2;
851 insert_p = false;
852 continue;
854 if (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset)
855 && (! hard_reg_set_equal_p (temp_hard_regset2, temp_hard_regset)
856 || cl == (int) GENERAL_REGS))
857 continue;
858 if (hard_reg_set_equal_p (temp_hard_regset2, temp_hard_regset))
859 insert_p = false;
860 pressure_classes[curr++] = (enum reg_class) cl2;
862 /* If the current candidate is a subset of a so far added
863 pressure class, don't add it to the list of the pressure
864 classes. */
865 if (insert_p)
866 pressure_classes[curr++] = (enum reg_class) cl;
867 n = curr;
869 #ifdef ENABLE_IRA_CHECKING
871 HARD_REG_SET ignore_hard_regs;
873 /* Check pressure classes correctness: here we check that hard
874 registers from all register pressure classes contains all hard
875 registers available for the allocation. */
876 CLEAR_HARD_REG_SET (temp_hard_regset);
877 CLEAR_HARD_REG_SET (temp_hard_regset2);
878 COPY_HARD_REG_SET (ignore_hard_regs, no_unit_alloc_regs);
879 for (cl = 0; cl < LIM_REG_CLASSES; cl++)
881 /* For some targets (like MIPS with MD_REGS), there are some
882 classes with hard registers available for allocation but
883 not able to hold value of any mode. */
884 for (m = 0; m < NUM_MACHINE_MODES; m++)
885 if (contains_reg_of_mode[cl][m])
886 break;
887 if (m >= NUM_MACHINE_MODES)
889 IOR_HARD_REG_SET (ignore_hard_regs, reg_class_contents[cl]);
890 continue;
892 for (i = 0; i < n; i++)
893 if ((int) pressure_classes[i] == cl)
894 break;
895 IOR_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
896 if (i < n)
897 IOR_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
899 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
900 /* Some targets (like SPARC with ICC reg) have alocatable regs
901 for which no reg class is defined. */
902 if (REGNO_REG_CLASS (i) == NO_REGS)
903 SET_HARD_REG_BIT (ignore_hard_regs, i);
904 AND_COMPL_HARD_REG_SET (temp_hard_regset, ignore_hard_regs);
905 AND_COMPL_HARD_REG_SET (temp_hard_regset2, ignore_hard_regs);
906 ira_assert (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset));
908 #endif
909 ira_pressure_classes_num = 0;
910 for (i = 0; i < n; i++)
912 cl = (int) pressure_classes[i];
913 ira_reg_pressure_class_p[cl] = true;
914 ira_pressure_classes[ira_pressure_classes_num++] = (enum reg_class) cl;
916 setup_stack_reg_pressure_class ();
919 /* Set up IRA_UNIFORM_CLASS_P. Uniform class is a register class
920 whose register move cost between any registers of the class is the
921 same as for all its subclasses. We use the data to speed up the
922 2nd pass of calculations of allocno costs. */
923 static void
924 setup_uniform_class_p (void)
926 int i, cl, cl2, m;
928 for (cl = 0; cl < N_REG_CLASSES; cl++)
930 ira_uniform_class_p[cl] = false;
931 if (ira_class_hard_regs_num[cl] == 0)
932 continue;
933 /* We can not use alloc_reg_class_subclasses here because move
934 cost hooks does not take into account that some registers are
935 unavailable for the subtarget. E.g. for i686, INT_SSE_REGS
936 is element of alloc_reg_class_subclasses for GENERAL_REGS
937 because SSE regs are unavailable. */
938 for (i = 0; (cl2 = reg_class_subclasses[cl][i]) != LIM_REG_CLASSES; i++)
940 if (ira_class_hard_regs_num[cl2] == 0)
941 continue;
942 for (m = 0; m < NUM_MACHINE_MODES; m++)
943 if (contains_reg_of_mode[cl][m] && contains_reg_of_mode[cl2][m])
945 ira_init_register_move_cost_if_necessary ((enum machine_mode) m);
946 if (ira_register_move_cost[m][cl][cl]
947 != ira_register_move_cost[m][cl2][cl2])
948 break;
950 if (m < NUM_MACHINE_MODES)
951 break;
953 if (cl2 == LIM_REG_CLASSES)
954 ira_uniform_class_p[cl] = true;
958 /* Set up IRA_ALLOCNO_CLASSES, IRA_ALLOCNO_CLASSES_NUM,
959 IRA_IMPORTANT_CLASSES, and IRA_IMPORTANT_CLASSES_NUM.
961 Target may have many subtargets and not all target hard regiters can
962 be used for allocation, e.g. x86 port in 32-bit mode can not use
963 hard registers introduced in x86-64 like r8-r15). Some classes
964 might have the same allocatable hard registers, e.g. INDEX_REGS
965 and GENERAL_REGS in x86 port in 32-bit mode. To decrease different
966 calculations efforts we introduce allocno classes which contain
967 unique non-empty sets of allocatable hard-registers.
969 Pseudo class cost calculation in ira-costs.c is very expensive.
970 Therefore we are trying to decrease number of classes involved in
971 such calculation. Register classes used in the cost calculation
972 are called important classes. They are allocno classes and other
973 non-empty classes whose allocatable hard register sets are inside
974 of an allocno class hard register set. From the first sight, it
975 looks like that they are just allocno classes. It is not true. In
976 example of x86-port in 32-bit mode, allocno classes will contain
977 GENERAL_REGS but not LEGACY_REGS (because allocatable hard
978 registers are the same for the both classes). The important
979 classes will contain GENERAL_REGS and LEGACY_REGS. It is done
980 because a machine description insn constraint may refers for
981 LEGACY_REGS and code in ira-costs.c is mostly base on investigation
982 of the insn constraints. */
983 static void
984 setup_allocno_and_important_classes (void)
986 int i, j, n, cl;
987 bool set_p;
988 HARD_REG_SET temp_hard_regset2;
989 static enum reg_class classes[LIM_REG_CLASSES + 1];
991 n = 0;
992 /* Collect classes which contain unique sets of allocatable hard
993 registers. Prefer GENERAL_REGS to other classes containing the
994 same set of hard registers. */
995 for (i = 0; i < LIM_REG_CLASSES; i++)
997 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
998 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
999 for (j = 0; j < n; j++)
1001 cl = classes[j];
1002 COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
1003 AND_COMPL_HARD_REG_SET (temp_hard_regset2,
1004 no_unit_alloc_regs);
1005 if (hard_reg_set_equal_p (temp_hard_regset,
1006 temp_hard_regset2))
1007 break;
1009 if (j >= n)
1010 classes[n++] = (enum reg_class) i;
1011 else if (i == GENERAL_REGS)
1012 /* Prefer general regs. For i386 example, it means that
1013 we prefer GENERAL_REGS over INDEX_REGS or LEGACY_REGS
1014 (all of them consists of the same available hard
1015 registers). */
1016 classes[j] = (enum reg_class) i;
1018 classes[n] = LIM_REG_CLASSES;
1020 /* Set up classes which can be used for allocnos as classes
1021 conatining non-empty unique sets of allocatable hard
1022 registers. */
1023 ira_allocno_classes_num = 0;
1024 for (i = 0; (cl = classes[i]) != LIM_REG_CLASSES; i++)
1025 if (ira_class_hard_regs_num[cl] > 0)
1026 ira_allocno_classes[ira_allocno_classes_num++] = (enum reg_class) cl;
1027 ira_important_classes_num = 0;
1028 /* Add non-allocno classes containing to non-empty set of
1029 allocatable hard regs. */
1030 for (cl = 0; cl < N_REG_CLASSES; cl++)
1031 if (ira_class_hard_regs_num[cl] > 0)
1033 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
1034 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1035 set_p = false;
1036 for (j = 0; j < ira_allocno_classes_num; j++)
1038 COPY_HARD_REG_SET (temp_hard_regset2,
1039 reg_class_contents[ira_allocno_classes[j]]);
1040 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
1041 if ((enum reg_class) cl == ira_allocno_classes[j])
1042 break;
1043 else if (hard_reg_set_subset_p (temp_hard_regset,
1044 temp_hard_regset2))
1045 set_p = true;
1047 if (set_p && j >= ira_allocno_classes_num)
1048 ira_important_classes[ira_important_classes_num++]
1049 = (enum reg_class) cl;
1051 /* Now add allocno classes to the important classes. */
1052 for (j = 0; j < ira_allocno_classes_num; j++)
1053 ira_important_classes[ira_important_classes_num++]
1054 = ira_allocno_classes[j];
1055 for (cl = 0; cl < N_REG_CLASSES; cl++)
1057 ira_reg_allocno_class_p[cl] = false;
1058 ira_reg_pressure_class_p[cl] = false;
1060 for (j = 0; j < ira_allocno_classes_num; j++)
1061 ira_reg_allocno_class_p[ira_allocno_classes[j]] = true;
1062 setup_pressure_classes ();
1063 setup_uniform_class_p ();
1066 /* Setup translation in CLASS_TRANSLATE of all classes into a class
1067 given by array CLASSES of length CLASSES_NUM. The function is used
1068 make translation any reg class to an allocno class or to an
1069 pressure class. This translation is necessary for some
1070 calculations when we can use only allocno or pressure classes and
1071 such translation represents an approximate representation of all
1072 classes.
1074 The translation in case when allocatable hard register set of a
1075 given class is subset of allocatable hard register set of a class
1076 in CLASSES is pretty simple. We use smallest classes from CLASSES
1077 containing a given class. If allocatable hard register set of a
1078 given class is not a subset of any corresponding set of a class
1079 from CLASSES, we use the cheapest (with load/store point of view)
1080 class from CLASSES whose set intersects with given class set */
1081 static void
1082 setup_class_translate_array (enum reg_class *class_translate,
1083 int classes_num, enum reg_class *classes)
1085 int cl, mode;
1086 enum reg_class aclass, best_class, *cl_ptr;
1087 int i, cost, min_cost, best_cost;
1089 for (cl = 0; cl < N_REG_CLASSES; cl++)
1090 class_translate[cl] = NO_REGS;
1092 for (i = 0; i < classes_num; i++)
1094 aclass = classes[i];
1095 for (cl_ptr = &alloc_reg_class_subclasses[aclass][0];
1096 (cl = *cl_ptr) != LIM_REG_CLASSES;
1097 cl_ptr++)
1098 if (class_translate[cl] == NO_REGS)
1099 class_translate[cl] = aclass;
1100 class_translate[aclass] = aclass;
1102 /* For classes which are not fully covered by one of given classes
1103 (in other words covered by more one given class), use the
1104 cheapest class. */
1105 for (cl = 0; cl < N_REG_CLASSES; cl++)
1107 if (cl == NO_REGS || class_translate[cl] != NO_REGS)
1108 continue;
1109 best_class = NO_REGS;
1110 best_cost = INT_MAX;
1111 for (i = 0; i < classes_num; i++)
1113 aclass = classes[i];
1114 COPY_HARD_REG_SET (temp_hard_regset,
1115 reg_class_contents[aclass]);
1116 AND_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
1117 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1118 if (! hard_reg_set_empty_p (temp_hard_regset))
1120 min_cost = INT_MAX;
1121 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1123 cost = (ira_memory_move_cost[mode][aclass][0]
1124 + ira_memory_move_cost[mode][aclass][1]);
1125 if (min_cost > cost)
1126 min_cost = cost;
1128 if (best_class == NO_REGS || best_cost > min_cost)
1130 best_class = aclass;
1131 best_cost = min_cost;
1135 class_translate[cl] = best_class;
1139 /* Set up array IRA_ALLOCNO_CLASS_TRANSLATE and
1140 IRA_PRESSURE_CLASS_TRANSLATE. */
1141 static void
1142 setup_class_translate (void)
1144 setup_class_translate_array (ira_allocno_class_translate,
1145 ira_allocno_classes_num, ira_allocno_classes);
1146 setup_class_translate_array (ira_pressure_class_translate,
1147 ira_pressure_classes_num, ira_pressure_classes);
1150 /* Order numbers of allocno classes in original target allocno class
1151 array, -1 for non-allocno classes. */
1152 static int allocno_class_order[N_REG_CLASSES];
1154 /* The function used to sort the important classes. */
1155 static int
1156 comp_reg_classes_func (const void *v1p, const void *v2p)
1158 enum reg_class cl1 = *(const enum reg_class *) v1p;
1159 enum reg_class cl2 = *(const enum reg_class *) v2p;
1160 enum reg_class tcl1, tcl2;
1161 int diff;
1163 tcl1 = ira_allocno_class_translate[cl1];
1164 tcl2 = ira_allocno_class_translate[cl2];
1165 if (tcl1 != NO_REGS && tcl2 != NO_REGS
1166 && (diff = allocno_class_order[tcl1] - allocno_class_order[tcl2]) != 0)
1167 return diff;
1168 return (int) cl1 - (int) cl2;
1171 /* For correct work of function setup_reg_class_relation we need to
1172 reorder important classes according to the order of their allocno
1173 classes. It places important classes containing the same
1174 allocatable hard register set adjacent to each other and allocno
1175 class with the allocatable hard register set right after the other
1176 important classes with the same set.
1178 In example from comments of function
1179 setup_allocno_and_important_classes, it places LEGACY_REGS and
1180 GENERAL_REGS close to each other and GENERAL_REGS is after
1181 LEGACY_REGS. */
1182 static void
1183 reorder_important_classes (void)
1185 int i;
1187 for (i = 0; i < N_REG_CLASSES; i++)
1188 allocno_class_order[i] = -1;
1189 for (i = 0; i < ira_allocno_classes_num; i++)
1190 allocno_class_order[ira_allocno_classes[i]] = i;
1191 qsort (ira_important_classes, ira_important_classes_num,
1192 sizeof (enum reg_class), comp_reg_classes_func);
1193 for (i = 0; i < ira_important_classes_num; i++)
1194 ira_important_class_nums[ira_important_classes[i]] = i;
1197 /* Set up IRA_REG_CLASS_SUBUNION, IRA_REG_CLASS_SUPERUNION,
1198 IRA_REG_CLASS_SUPER_CLASSES, IRA_REG_CLASSES_INTERSECT, and
1199 IRA_REG_CLASSES_INTERSECT_P. For the meaning of the relations,
1200 please see corresponding comments in ira-int.h. */
1201 static void
1202 setup_reg_class_relations (void)
1204 int i, cl1, cl2, cl3;
1205 HARD_REG_SET intersection_set, union_set, temp_set2;
1206 bool important_class_p[N_REG_CLASSES];
1208 memset (important_class_p, 0, sizeof (important_class_p));
1209 for (i = 0; i < ira_important_classes_num; i++)
1210 important_class_p[ira_important_classes[i]] = true;
1211 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1213 ira_reg_class_super_classes[cl1][0] = LIM_REG_CLASSES;
1214 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1216 ira_reg_classes_intersect_p[cl1][cl2] = false;
1217 ira_reg_class_intersect[cl1][cl2] = NO_REGS;
1218 ira_reg_class_subset[cl1][cl2] = NO_REGS;
1219 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl1]);
1220 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1221 COPY_HARD_REG_SET (temp_set2, reg_class_contents[cl2]);
1222 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1223 if (hard_reg_set_empty_p (temp_hard_regset)
1224 && hard_reg_set_empty_p (temp_set2))
1226 /* The both classes have no allocatable hard registers
1227 -- take all class hard registers into account and use
1228 reg_class_subunion and reg_class_superunion. */
1229 for (i = 0;; i++)
1231 cl3 = reg_class_subclasses[cl1][i];
1232 if (cl3 == LIM_REG_CLASSES)
1233 break;
1234 if (reg_class_subset_p (ira_reg_class_intersect[cl1][cl2],
1235 (enum reg_class) cl3))
1236 ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1238 ira_reg_class_subunion[cl1][cl2] = reg_class_subunion[cl1][cl2];
1239 ira_reg_class_superunion[cl1][cl2] = reg_class_superunion[cl1][cl2];
1240 continue;
1242 ira_reg_classes_intersect_p[cl1][cl2]
1243 = hard_reg_set_intersect_p (temp_hard_regset, temp_set2);
1244 if (important_class_p[cl1] && important_class_p[cl2]
1245 && hard_reg_set_subset_p (temp_hard_regset, temp_set2))
1247 /* CL1 and CL2 are important classes and CL1 allocatable
1248 hard register set is inside of CL2 allocatable hard
1249 registers -- make CL1 a superset of CL2. */
1250 enum reg_class *p;
1252 p = &ira_reg_class_super_classes[cl1][0];
1253 while (*p != LIM_REG_CLASSES)
1254 p++;
1255 *p++ = (enum reg_class) cl2;
1256 *p = LIM_REG_CLASSES;
1258 ira_reg_class_subunion[cl1][cl2] = NO_REGS;
1259 ira_reg_class_superunion[cl1][cl2] = NO_REGS;
1260 COPY_HARD_REG_SET (intersection_set, reg_class_contents[cl1]);
1261 AND_HARD_REG_SET (intersection_set, reg_class_contents[cl2]);
1262 AND_COMPL_HARD_REG_SET (intersection_set, no_unit_alloc_regs);
1263 COPY_HARD_REG_SET (union_set, reg_class_contents[cl1]);
1264 IOR_HARD_REG_SET (union_set, reg_class_contents[cl2]);
1265 AND_COMPL_HARD_REG_SET (union_set, no_unit_alloc_regs);
1266 for (cl3 = 0; cl3 < N_REG_CLASSES; cl3++)
1268 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl3]);
1269 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1270 if (hard_reg_set_subset_p (temp_hard_regset, intersection_set))
1272 /* CL3 allocatable hard register set is inside of
1273 intersection of allocatable hard register sets
1274 of CL1 and CL2. */
1275 if (important_class_p[cl3])
1277 COPY_HARD_REG_SET
1278 (temp_set2,
1279 reg_class_contents
1280 [(int) ira_reg_class_intersect[cl1][cl2]]);
1281 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1282 if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1283 /* If the allocatable hard register sets are
1284 the same, prefer GENERAL_REGS or the
1285 smallest class for debugging
1286 purposes. */
1287 || (hard_reg_set_equal_p (temp_hard_regset, temp_set2)
1288 && (cl3 == GENERAL_REGS
1289 || ((ira_reg_class_intersect[cl1][cl2]
1290 != GENERAL_REGS)
1291 && hard_reg_set_subset_p
1292 (reg_class_contents[cl3],
1293 reg_class_contents
1294 [(int)
1295 ira_reg_class_intersect[cl1][cl2]])))))
1296 ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1298 COPY_HARD_REG_SET
1299 (temp_set2,
1300 reg_class_contents[(int) ira_reg_class_subset[cl1][cl2]]);
1301 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1302 if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1303 /* Ignore unavailable hard registers and prefer
1304 smallest class for debugging purposes. */
1305 || (hard_reg_set_equal_p (temp_hard_regset, temp_set2)
1306 && hard_reg_set_subset_p
1307 (reg_class_contents[cl3],
1308 reg_class_contents
1309 [(int) ira_reg_class_subset[cl1][cl2]])))
1310 ira_reg_class_subset[cl1][cl2] = (enum reg_class) cl3;
1312 if (important_class_p[cl3]
1313 && hard_reg_set_subset_p (temp_hard_regset, union_set))
1315 /* CL3 allocatbale hard register set is inside of
1316 union of allocatable hard register sets of CL1
1317 and CL2. */
1318 COPY_HARD_REG_SET
1319 (temp_set2,
1320 reg_class_contents[(int) ira_reg_class_subunion[cl1][cl2]]);
1321 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1322 if (ira_reg_class_subunion[cl1][cl2] == NO_REGS
1323 || (hard_reg_set_subset_p (temp_set2, temp_hard_regset)
1325 && (! hard_reg_set_equal_p (temp_set2,
1326 temp_hard_regset)
1327 || cl3 == GENERAL_REGS
1328 /* If the allocatable hard register sets are the
1329 same, prefer GENERAL_REGS or the smallest
1330 class for debugging purposes. */
1331 || (ira_reg_class_subunion[cl1][cl2] != GENERAL_REGS
1332 && hard_reg_set_subset_p
1333 (reg_class_contents[cl3],
1334 reg_class_contents
1335 [(int) ira_reg_class_subunion[cl1][cl2]])))))
1336 ira_reg_class_subunion[cl1][cl2] = (enum reg_class) cl3;
1338 if (hard_reg_set_subset_p (union_set, temp_hard_regset))
1340 /* CL3 allocatable hard register set contains union
1341 of allocatable hard register sets of CL1 and
1342 CL2. */
1343 COPY_HARD_REG_SET
1344 (temp_set2,
1345 reg_class_contents[(int) ira_reg_class_superunion[cl1][cl2]]);
1346 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1347 if (ira_reg_class_superunion[cl1][cl2] == NO_REGS
1348 || (hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1350 && (! hard_reg_set_equal_p (temp_set2,
1351 temp_hard_regset)
1352 || cl3 == GENERAL_REGS
1353 /* If the allocatable hard register sets are the
1354 same, prefer GENERAL_REGS or the smallest
1355 class for debugging purposes. */
1356 || (ira_reg_class_superunion[cl1][cl2] != GENERAL_REGS
1357 && hard_reg_set_subset_p
1358 (reg_class_contents[cl3],
1359 reg_class_contents
1360 [(int) ira_reg_class_superunion[cl1][cl2]])))))
1361 ira_reg_class_superunion[cl1][cl2] = (enum reg_class) cl3;
1368 /* Output all unifrom and important classes into file F. */
1369 static void
1370 print_unform_and_important_classes (FILE *f)
1372 static const char *const reg_class_names[] = REG_CLASS_NAMES;
1373 int i, cl;
1375 fprintf (f, "Uniform classes:\n");
1376 for (cl = 0; cl < N_REG_CLASSES; cl++)
1377 if (ira_uniform_class_p[cl])
1378 fprintf (f, " %s", reg_class_names[cl]);
1379 fprintf (f, "\nImportant classes:\n");
1380 for (i = 0; i < ira_important_classes_num; i++)
1381 fprintf (f, " %s", reg_class_names[ira_important_classes[i]]);
1382 fprintf (f, "\n");
1385 /* Output all possible allocno or pressure classes and their
1386 translation map into file F. */
1387 static void
1388 print_translated_classes (FILE *f, bool pressure_p)
1390 int classes_num = (pressure_p
1391 ? ira_pressure_classes_num : ira_allocno_classes_num);
1392 enum reg_class *classes = (pressure_p
1393 ? ira_pressure_classes : ira_allocno_classes);
1394 enum reg_class *class_translate = (pressure_p
1395 ? ira_pressure_class_translate
1396 : ira_allocno_class_translate);
1397 static const char *const reg_class_names[] = REG_CLASS_NAMES;
1398 int i;
1400 fprintf (f, "%s classes:\n", pressure_p ? "Pressure" : "Allocno");
1401 for (i = 0; i < classes_num; i++)
1402 fprintf (f, " %s", reg_class_names[classes[i]]);
1403 fprintf (f, "\nClass translation:\n");
1404 for (i = 0; i < N_REG_CLASSES; i++)
1405 fprintf (f, " %s -> %s\n", reg_class_names[i],
1406 reg_class_names[class_translate[i]]);
1409 /* Output all possible allocno and translation classes and the
1410 translation maps into stderr. */
1411 void
1412 ira_debug_allocno_classes (void)
1414 print_unform_and_important_classes (stderr);
1415 print_translated_classes (stderr, false);
1416 print_translated_classes (stderr, true);
1419 /* Set up different arrays concerning class subsets, allocno and
1420 important classes. */
1421 static void
1422 find_reg_classes (void)
1424 setup_allocno_and_important_classes ();
1425 setup_class_translate ();
1426 reorder_important_classes ();
1427 setup_reg_class_relations ();
1432 /* Set up the array above. */
1433 static void
1434 setup_hard_regno_aclass (void)
1436 int i;
1438 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1440 #if 1
1441 ira_hard_regno_allocno_class[i]
1442 = (TEST_HARD_REG_BIT (no_unit_alloc_regs, i)
1443 ? NO_REGS
1444 : ira_allocno_class_translate[REGNO_REG_CLASS (i)]);
1445 #else
1446 int j;
1447 enum reg_class cl;
1448 ira_hard_regno_allocno_class[i] = NO_REGS;
1449 for (j = 0; j < ira_allocno_classes_num; j++)
1451 cl = ira_allocno_classes[j];
1452 if (ira_class_hard_reg_index[cl][i] >= 0)
1454 ira_hard_regno_allocno_class[i] = cl;
1455 break;
1458 #endif
1464 /* Form IRA_REG_CLASS_MAX_NREGS and IRA_REG_CLASS_MIN_NREGS maps. */
1465 static void
1466 setup_reg_class_nregs (void)
1468 int i, cl, cl2, m;
1470 for (m = 0; m < MAX_MACHINE_MODE; m++)
1472 for (cl = 0; cl < N_REG_CLASSES; cl++)
1473 ira_reg_class_max_nregs[cl][m]
1474 = ira_reg_class_min_nregs[cl][m]
1475 = targetm.class_max_nregs ((reg_class_t) cl, (enum machine_mode) m);
1476 for (cl = 0; cl < N_REG_CLASSES; cl++)
1477 for (i = 0;
1478 (cl2 = alloc_reg_class_subclasses[cl][i]) != LIM_REG_CLASSES;
1479 i++)
1480 if (ira_reg_class_min_nregs[cl2][m]
1481 < ira_reg_class_min_nregs[cl][m])
1482 ira_reg_class_min_nregs[cl][m] = ira_reg_class_min_nregs[cl2][m];
1488 /* Set up IRA_PROHIBITED_CLASS_MODE_REGS and IRA_CLASS_SINGLETON.
1489 This function is called once IRA_CLASS_HARD_REGS has been initialized. */
1490 static void
1491 setup_prohibited_class_mode_regs (void)
1493 int j, k, hard_regno, cl, last_hard_regno, count;
1495 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
1497 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
1498 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1499 for (j = 0; j < NUM_MACHINE_MODES; j++)
1501 count = 0;
1502 last_hard_regno = -1;
1503 CLEAR_HARD_REG_SET (ira_prohibited_class_mode_regs[cl][j]);
1504 for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1506 hard_regno = ira_class_hard_regs[cl][k];
1507 if (! HARD_REGNO_MODE_OK (hard_regno, (enum machine_mode) j))
1508 SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1509 hard_regno);
1510 else if (in_hard_reg_set_p (temp_hard_regset,
1511 (enum machine_mode) j, hard_regno))
1513 last_hard_regno = hard_regno;
1514 count++;
1517 ira_class_singleton[cl][j] = (count == 1 ? last_hard_regno : -1);
1522 /* Clarify IRA_PROHIBITED_CLASS_MODE_REGS by excluding hard registers
1523 spanning from one register pressure class to another one. It is
1524 called after defining the pressure classes. */
1525 static void
1526 clarify_prohibited_class_mode_regs (void)
1528 int j, k, hard_regno, cl, pclass, nregs;
1530 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
1531 for (j = 0; j < NUM_MACHINE_MODES; j++)
1533 CLEAR_HARD_REG_SET (ira_useful_class_mode_regs[cl][j]);
1534 for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1536 hard_regno = ira_class_hard_regs[cl][k];
1537 if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j], hard_regno))
1538 continue;
1539 nregs = hard_regno_nregs[hard_regno][j];
1540 if (hard_regno + nregs > FIRST_PSEUDO_REGISTER)
1542 SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1543 hard_regno);
1544 continue;
1546 pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
1547 for (nregs-- ;nregs >= 0; nregs--)
1548 if (((enum reg_class) pclass
1549 != ira_pressure_class_translate[REGNO_REG_CLASS
1550 (hard_regno + nregs)]))
1552 SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1553 hard_regno);
1554 break;
1556 if (!TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1557 hard_regno))
1558 add_to_hard_reg_set (&ira_useful_class_mode_regs[cl][j],
1559 (enum machine_mode) j, hard_regno);
1564 /* Allocate and initialize IRA_REGISTER_MOVE_COST, IRA_MAY_MOVE_IN_COST
1565 and IRA_MAY_MOVE_OUT_COST for MODE. */
1566 void
1567 ira_init_register_move_cost (enum machine_mode mode)
1569 static unsigned short last_move_cost[N_REG_CLASSES][N_REG_CLASSES];
1570 bool all_match = true;
1571 unsigned int cl1, cl2;
1573 ira_assert (ira_register_move_cost[mode] == NULL
1574 && ira_may_move_in_cost[mode] == NULL
1575 && ira_may_move_out_cost[mode] == NULL);
1576 ira_assert (have_regs_of_mode[mode]);
1577 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1578 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1580 int cost;
1581 if (!contains_reg_of_mode[cl1][mode]
1582 || !contains_reg_of_mode[cl2][mode])
1584 if ((ira_reg_class_max_nregs[cl1][mode]
1585 > ira_class_hard_regs_num[cl1])
1586 || (ira_reg_class_max_nregs[cl2][mode]
1587 > ira_class_hard_regs_num[cl2]))
1588 cost = 65535;
1589 else
1590 cost = (ira_memory_move_cost[mode][cl1][0]
1591 + ira_memory_move_cost[mode][cl2][1]) * 2;
1593 else
1595 cost = register_move_cost (mode, (enum reg_class) cl1,
1596 (enum reg_class) cl2);
1597 ira_assert (cost < 65535);
1599 all_match &= (last_move_cost[cl1][cl2] == cost);
1600 last_move_cost[cl1][cl2] = cost;
1602 if (all_match && last_mode_for_init_move_cost != -1)
1604 ira_register_move_cost[mode]
1605 = ira_register_move_cost[last_mode_for_init_move_cost];
1606 ira_may_move_in_cost[mode]
1607 = ira_may_move_in_cost[last_mode_for_init_move_cost];
1608 ira_may_move_out_cost[mode]
1609 = ira_may_move_out_cost[last_mode_for_init_move_cost];
1610 return;
1612 last_mode_for_init_move_cost = mode;
1613 ira_register_move_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
1614 ira_may_move_in_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
1615 ira_may_move_out_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
1616 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1617 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1619 int cost;
1620 enum reg_class *p1, *p2;
1622 if (last_move_cost[cl1][cl2] == 65535)
1624 ira_register_move_cost[mode][cl1][cl2] = 65535;
1625 ira_may_move_in_cost[mode][cl1][cl2] = 65535;
1626 ira_may_move_out_cost[mode][cl1][cl2] = 65535;
1628 else
1630 cost = last_move_cost[cl1][cl2];
1632 for (p2 = &reg_class_subclasses[cl2][0];
1633 *p2 != LIM_REG_CLASSES; p2++)
1634 if (ira_class_hard_regs_num[*p2] > 0
1635 && (ira_reg_class_max_nregs[*p2][mode]
1636 <= ira_class_hard_regs_num[*p2]))
1637 cost = MAX (cost, ira_register_move_cost[mode][cl1][*p2]);
1639 for (p1 = &reg_class_subclasses[cl1][0];
1640 *p1 != LIM_REG_CLASSES; p1++)
1641 if (ira_class_hard_regs_num[*p1] > 0
1642 && (ira_reg_class_max_nregs[*p1][mode]
1643 <= ira_class_hard_regs_num[*p1]))
1644 cost = MAX (cost, ira_register_move_cost[mode][*p1][cl2]);
1646 ira_assert (cost <= 65535);
1647 ira_register_move_cost[mode][cl1][cl2] = cost;
1649 if (ira_class_subset_p[cl1][cl2])
1650 ira_may_move_in_cost[mode][cl1][cl2] = 0;
1651 else
1652 ira_may_move_in_cost[mode][cl1][cl2] = cost;
1654 if (ira_class_subset_p[cl2][cl1])
1655 ira_may_move_out_cost[mode][cl1][cl2] = 0;
1656 else
1657 ira_may_move_out_cost[mode][cl1][cl2] = cost;
1664 /* This is called once during compiler work. It sets up
1665 different arrays whose values don't depend on the compiled
1666 function. */
1667 void
1668 ira_init_once (void)
1670 ira_init_costs_once ();
1671 lra_init_once ();
1674 /* Free ira_max_register_move_cost, ira_may_move_in_cost and
1675 ira_may_move_out_cost for each mode. */
1676 static void
1677 free_register_move_costs (void)
1679 int mode, i;
1681 /* Reset move_cost and friends, making sure we only free shared
1682 table entries once. */
1683 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1684 if (ira_register_move_cost[mode])
1686 for (i = 0;
1687 i < mode && (ira_register_move_cost[i]
1688 != ira_register_move_cost[mode]);
1689 i++)
1691 if (i == mode)
1693 free (ira_register_move_cost[mode]);
1694 free (ira_may_move_in_cost[mode]);
1695 free (ira_may_move_out_cost[mode]);
1698 memset (ira_register_move_cost, 0, sizeof ira_register_move_cost);
1699 memset (ira_may_move_in_cost, 0, sizeof ira_may_move_in_cost);
1700 memset (ira_may_move_out_cost, 0, sizeof ira_may_move_out_cost);
1701 last_mode_for_init_move_cost = -1;
1704 /* This is called every time when register related information is
1705 changed. */
1706 void
1707 ira_init (void)
1709 free_register_move_costs ();
1710 setup_reg_mode_hard_regset ();
1711 setup_alloc_regs (flag_omit_frame_pointer != 0);
1712 setup_class_subset_and_memory_move_costs ();
1713 setup_reg_class_nregs ();
1714 setup_prohibited_class_mode_regs ();
1715 find_reg_classes ();
1716 clarify_prohibited_class_mode_regs ();
1717 setup_hard_regno_aclass ();
1718 ira_init_costs ();
1721 /* Function called once at the end of compiler work. */
1722 void
1723 ira_finish_once (void)
1725 ira_finish_costs_once ();
1726 free_register_move_costs ();
1727 lra_finish_once ();
1731 #define ira_prohibited_mode_move_regs_initialized_p \
1732 (this_target_ira_int->x_ira_prohibited_mode_move_regs_initialized_p)
1734 /* Set up IRA_PROHIBITED_MODE_MOVE_REGS. */
1735 static void
1736 setup_prohibited_mode_move_regs (void)
1738 int i, j;
1739 rtx test_reg1, test_reg2, move_pat, move_insn;
1741 if (ira_prohibited_mode_move_regs_initialized_p)
1742 return;
1743 ira_prohibited_mode_move_regs_initialized_p = true;
1744 test_reg1 = gen_rtx_REG (VOIDmode, 0);
1745 test_reg2 = gen_rtx_REG (VOIDmode, 0);
1746 move_pat = gen_rtx_SET (VOIDmode, test_reg1, test_reg2);
1747 move_insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, move_pat, 0, -1, 0);
1748 for (i = 0; i < NUM_MACHINE_MODES; i++)
1750 SET_HARD_REG_SET (ira_prohibited_mode_move_regs[i]);
1751 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1753 if (! HARD_REGNO_MODE_OK (j, (enum machine_mode) i))
1754 continue;
1755 SET_REGNO_RAW (test_reg1, j);
1756 PUT_MODE (test_reg1, (enum machine_mode) i);
1757 SET_REGNO_RAW (test_reg2, j);
1758 PUT_MODE (test_reg2, (enum machine_mode) i);
1759 INSN_CODE (move_insn) = -1;
1760 recog_memoized (move_insn);
1761 if (INSN_CODE (move_insn) < 0)
1762 continue;
1763 extract_insn (move_insn);
1764 if (! constrain_operands (1))
1765 continue;
1766 CLEAR_HARD_REG_BIT (ira_prohibited_mode_move_regs[i], j);
1773 /* Setup possible alternatives in ALTS for INSN. */
1774 void
1775 ira_setup_alts (rtx insn, HARD_REG_SET &alts)
1777 /* MAP nalt * nop -> start of constraints for given operand and
1778 alternative */
1779 static vec<const char *> insn_constraints;
1780 int nop, nalt;
1781 bool curr_swapped;
1782 const char *p;
1783 rtx op;
1784 int commutative = -1;
1786 extract_insn (insn);
1787 CLEAR_HARD_REG_SET (alts);
1788 insn_constraints.release ();
1789 insn_constraints.safe_grow_cleared (recog_data.n_operands
1790 * recog_data.n_alternatives + 1);
1791 /* Check that the hard reg set is enough for holding all
1792 alternatives. It is hard to imagine the situation when the
1793 assertion is wrong. */
1794 ira_assert (recog_data.n_alternatives
1795 <= (int) MAX (sizeof (HARD_REG_ELT_TYPE) * CHAR_BIT,
1796 FIRST_PSEUDO_REGISTER));
1797 for (curr_swapped = false;; curr_swapped = true)
1799 /* Calculate some data common for all alternatives to speed up the
1800 function. */
1801 for (nop = 0; nop < recog_data.n_operands; nop++)
1803 for (nalt = 0, p = recog_data.constraints[nop];
1804 nalt < recog_data.n_alternatives;
1805 nalt++)
1807 insn_constraints[nop * recog_data.n_alternatives + nalt] = p;
1808 while (*p && *p != ',')
1809 p++;
1810 if (*p)
1811 p++;
1814 for (nalt = 0; nalt < recog_data.n_alternatives; nalt++)
1816 if (!TEST_BIT (recog_data.enabled_alternatives, nalt)
1817 || TEST_HARD_REG_BIT (alts, nalt))
1818 continue;
1820 for (nop = 0; nop < recog_data.n_operands; nop++)
1822 int c, len;
1824 op = recog_data.operand[nop];
1825 p = insn_constraints[nop * recog_data.n_alternatives + nalt];
1826 if (*p == 0 || *p == ',')
1827 continue;
1830 switch (c = *p, len = CONSTRAINT_LEN (c, p), c)
1832 case '#':
1833 case ',':
1834 c = '\0';
1835 case '\0':
1836 len = 0;
1837 break;
1839 case '%':
1840 /* We only support one commutative marker, the
1841 first one. We already set commutative
1842 above. */
1843 if (commutative < 0)
1844 commutative = nop;
1845 break;
1847 case '0': case '1': case '2': case '3': case '4':
1848 case '5': case '6': case '7': case '8': case '9':
1849 goto op_success;
1850 break;
1852 case 'g':
1853 goto op_success;
1854 break;
1856 default:
1858 enum constraint_num cn = lookup_constraint (p);
1859 switch (get_constraint_type (cn))
1861 case CT_REGISTER:
1862 if (reg_class_for_constraint (cn) != NO_REGS)
1863 goto op_success;
1864 break;
1866 case CT_CONST_INT:
1867 if (CONST_INT_P (op)
1868 && (insn_const_int_ok_for_constraint
1869 (INTVAL (op), cn)))
1870 goto op_success;
1871 break;
1873 case CT_ADDRESS:
1874 case CT_MEMORY:
1875 goto op_success;
1877 case CT_FIXED_FORM:
1878 if (constraint_satisfied_p (op, cn))
1879 goto op_success;
1880 break;
1882 break;
1885 while (p += len, c);
1886 break;
1887 op_success:
1890 if (nop >= recog_data.n_operands)
1891 SET_HARD_REG_BIT (alts, nalt);
1893 if (commutative < 0)
1894 break;
1895 if (curr_swapped)
1896 break;
1897 op = recog_data.operand[commutative];
1898 recog_data.operand[commutative] = recog_data.operand[commutative + 1];
1899 recog_data.operand[commutative + 1] = op;
1904 /* Return the number of the output non-early clobber operand which
1905 should be the same in any case as operand with number OP_NUM (or
1906 negative value if there is no such operand). The function takes
1907 only really possible alternatives into consideration. */
1909 ira_get_dup_out_num (int op_num, HARD_REG_SET &alts)
1911 int curr_alt, c, original, dup;
1912 bool ignore_p, use_commut_op_p;
1913 const char *str;
1915 if (op_num < 0 || recog_data.n_alternatives == 0)
1916 return -1;
1917 /* We should find duplications only for input operands. */
1918 if (recog_data.operand_type[op_num] != OP_IN)
1919 return -1;
1920 str = recog_data.constraints[op_num];
1921 use_commut_op_p = false;
1922 for (;;)
1924 rtx op = recog_data.operand[op_num];
1926 for (curr_alt = 0, ignore_p = !TEST_HARD_REG_BIT (alts, curr_alt),
1927 original = -1;;)
1929 c = *str;
1930 if (c == '\0')
1931 break;
1932 if (c == '#')
1933 ignore_p = true;
1934 else if (c == ',')
1936 curr_alt++;
1937 ignore_p = !TEST_HARD_REG_BIT (alts, curr_alt);
1939 else if (! ignore_p)
1940 switch (c)
1942 case 'g':
1943 goto fail;
1944 default:
1946 enum constraint_num cn = lookup_constraint (str);
1947 enum reg_class cl = reg_class_for_constraint (cn);
1948 if (cl != NO_REGS
1949 && !targetm.class_likely_spilled_p (cl))
1950 goto fail;
1951 if (constraint_satisfied_p (op, cn))
1952 goto fail;
1953 break;
1956 case '0': case '1': case '2': case '3': case '4':
1957 case '5': case '6': case '7': case '8': case '9':
1958 if (original != -1 && original != c)
1959 goto fail;
1960 original = c;
1961 break;
1963 str += CONSTRAINT_LEN (c, str);
1965 if (original == -1)
1966 goto fail;
1967 dup = -1;
1968 for (ignore_p = false, str = recog_data.constraints[original - '0'];
1969 *str != 0;
1970 str++)
1971 if (ignore_p)
1973 if (*str == ',')
1974 ignore_p = false;
1976 else if (*str == '#')
1977 ignore_p = true;
1978 else if (! ignore_p)
1980 if (*str == '=')
1981 dup = original - '0';
1982 /* It is better ignore an alternative with early clobber. */
1983 else if (*str == '&')
1984 goto fail;
1986 if (dup >= 0)
1987 return dup;
1988 fail:
1989 if (use_commut_op_p)
1990 break;
1991 use_commut_op_p = true;
1992 if (recog_data.constraints[op_num][0] == '%')
1993 str = recog_data.constraints[op_num + 1];
1994 else if (op_num > 0 && recog_data.constraints[op_num - 1][0] == '%')
1995 str = recog_data.constraints[op_num - 1];
1996 else
1997 break;
1999 return -1;
2004 /* Search forward to see if the source register of a copy insn dies
2005 before either it or the destination register is modified, but don't
2006 scan past the end of the basic block. If so, we can replace the
2007 source with the destination and let the source die in the copy
2008 insn.
2010 This will reduce the number of registers live in that range and may
2011 enable the destination and the source coalescing, thus often saving
2012 one register in addition to a register-register copy. */
2014 static void
2015 decrease_live_ranges_number (void)
2017 basic_block bb;
2018 rtx_insn *insn;
2019 rtx set, src, dest, dest_death, q, note;
2020 rtx_insn *p;
2021 int sregno, dregno;
2023 if (! flag_expensive_optimizations)
2024 return;
2026 if (ira_dump_file)
2027 fprintf (ira_dump_file, "Starting decreasing number of live ranges...\n");
2029 FOR_EACH_BB_FN (bb, cfun)
2030 FOR_BB_INSNS (bb, insn)
2032 set = single_set (insn);
2033 if (! set)
2034 continue;
2035 src = SET_SRC (set);
2036 dest = SET_DEST (set);
2037 if (! REG_P (src) || ! REG_P (dest)
2038 || find_reg_note (insn, REG_DEAD, src))
2039 continue;
2040 sregno = REGNO (src);
2041 dregno = REGNO (dest);
2043 /* We don't want to mess with hard regs if register classes
2044 are small. */
2045 if (sregno == dregno
2046 || (targetm.small_register_classes_for_mode_p (GET_MODE (src))
2047 && (sregno < FIRST_PSEUDO_REGISTER
2048 || dregno < FIRST_PSEUDO_REGISTER))
2049 /* We don't see all updates to SP if they are in an
2050 auto-inc memory reference, so we must disallow this
2051 optimization on them. */
2052 || sregno == STACK_POINTER_REGNUM
2053 || dregno == STACK_POINTER_REGNUM)
2054 continue;
2056 dest_death = NULL_RTX;
2058 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
2060 if (! INSN_P (p))
2061 continue;
2062 if (BLOCK_FOR_INSN (p) != bb)
2063 break;
2065 if (reg_set_p (src, p) || reg_set_p (dest, p)
2066 /* If SRC is an asm-declared register, it must not be
2067 replaced in any asm. Unfortunately, the REG_EXPR
2068 tree for the asm variable may be absent in the SRC
2069 rtx, so we can't check the actual register
2070 declaration easily (the asm operand will have it,
2071 though). To avoid complicating the test for a rare
2072 case, we just don't perform register replacement
2073 for a hard reg mentioned in an asm. */
2074 || (sregno < FIRST_PSEUDO_REGISTER
2075 && asm_noperands (PATTERN (p)) >= 0
2076 && reg_overlap_mentioned_p (src, PATTERN (p)))
2077 /* Don't change hard registers used by a call. */
2078 || (CALL_P (p) && sregno < FIRST_PSEUDO_REGISTER
2079 && find_reg_fusage (p, USE, src))
2080 /* Don't change a USE of a register. */
2081 || (GET_CODE (PATTERN (p)) == USE
2082 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
2083 break;
2085 /* See if all of SRC dies in P. This test is slightly
2086 more conservative than it needs to be. */
2087 if ((note = find_regno_note (p, REG_DEAD, sregno))
2088 && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
2090 int failed = 0;
2092 /* We can do the optimization. Scan forward from INSN
2093 again, replacing regs as we go. Set FAILED if a
2094 replacement can't be done. In that case, we can't
2095 move the death note for SRC. This should be
2096 rare. */
2098 /* Set to stop at next insn. */
2099 for (q = next_real_insn (insn);
2100 q != next_real_insn (p);
2101 q = next_real_insn (q))
2103 if (reg_overlap_mentioned_p (src, PATTERN (q)))
2105 /* If SRC is a hard register, we might miss
2106 some overlapping registers with
2107 validate_replace_rtx, so we would have to
2108 undo it. We can't if DEST is present in
2109 the insn, so fail in that combination of
2110 cases. */
2111 if (sregno < FIRST_PSEUDO_REGISTER
2112 && reg_mentioned_p (dest, PATTERN (q)))
2113 failed = 1;
2115 /* Attempt to replace all uses. */
2116 else if (!validate_replace_rtx (src, dest, q))
2117 failed = 1;
2119 /* If this succeeded, but some part of the
2120 register is still present, undo the
2121 replacement. */
2122 else if (sregno < FIRST_PSEUDO_REGISTER
2123 && reg_overlap_mentioned_p (src, PATTERN (q)))
2125 validate_replace_rtx (dest, src, q);
2126 failed = 1;
2130 /* If DEST dies here, remove the death note and
2131 save it for later. Make sure ALL of DEST dies
2132 here; again, this is overly conservative. */
2133 if (! dest_death
2134 && (dest_death = find_regno_note (q, REG_DEAD, dregno)))
2136 if (GET_MODE (XEXP (dest_death, 0)) == GET_MODE (dest))
2137 remove_note (q, dest_death);
2138 else
2140 failed = 1;
2141 dest_death = 0;
2146 if (! failed)
2148 /* Move death note of SRC from P to INSN. */
2149 remove_note (p, note);
2150 XEXP (note, 1) = REG_NOTES (insn);
2151 REG_NOTES (insn) = note;
2154 /* DEST is also dead if INSN has a REG_UNUSED note for
2155 DEST. */
2156 if (! dest_death
2157 && (dest_death
2158 = find_regno_note (insn, REG_UNUSED, dregno)))
2160 PUT_REG_NOTE_KIND (dest_death, REG_DEAD);
2161 remove_note (insn, dest_death);
2164 /* Put death note of DEST on P if we saw it die. */
2165 if (dest_death)
2167 XEXP (dest_death, 1) = REG_NOTES (p);
2168 REG_NOTES (p) = dest_death;
2170 break;
2173 /* If SRC is a hard register which is set or killed in
2174 some other way, we can't do this optimization. */
2175 else if (sregno < FIRST_PSEUDO_REGISTER && dead_or_set_p (p, src))
2176 break;
2183 /* Return nonzero if REGNO is a particularly bad choice for reloading X. */
2184 static bool
2185 ira_bad_reload_regno_1 (int regno, rtx x)
2187 int x_regno, n, i;
2188 ira_allocno_t a;
2189 enum reg_class pref;
2191 /* We only deal with pseudo regs. */
2192 if (! x || GET_CODE (x) != REG)
2193 return false;
2195 x_regno = REGNO (x);
2196 if (x_regno < FIRST_PSEUDO_REGISTER)
2197 return false;
2199 /* If the pseudo prefers REGNO explicitly, then do not consider
2200 REGNO a bad spill choice. */
2201 pref = reg_preferred_class (x_regno);
2202 if (reg_class_size[pref] == 1)
2203 return !TEST_HARD_REG_BIT (reg_class_contents[pref], regno);
2205 /* If the pseudo conflicts with REGNO, then we consider REGNO a
2206 poor choice for a reload regno. */
2207 a = ira_regno_allocno_map[x_regno];
2208 n = ALLOCNO_NUM_OBJECTS (a);
2209 for (i = 0; i < n; i++)
2211 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2212 if (TEST_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), regno))
2213 return true;
2215 return false;
2218 /* Return nonzero if REGNO is a particularly bad choice for reloading
2219 IN or OUT. */
2220 bool
2221 ira_bad_reload_regno (int regno, rtx in, rtx out)
2223 return (ira_bad_reload_regno_1 (regno, in)
2224 || ira_bad_reload_regno_1 (regno, out));
2227 /* Add register clobbers from asm statements. */
2228 static void
2229 compute_regs_asm_clobbered (void)
2231 basic_block bb;
2233 FOR_EACH_BB_FN (bb, cfun)
2235 rtx_insn *insn;
2236 FOR_BB_INSNS_REVERSE (bb, insn)
2238 df_ref def;
2240 if (NONDEBUG_INSN_P (insn) && extract_asm_operands (PATTERN (insn)))
2241 FOR_EACH_INSN_DEF (def, insn)
2243 unsigned int dregno = DF_REF_REGNO (def);
2244 if (HARD_REGISTER_NUM_P (dregno))
2245 add_to_hard_reg_set (&crtl->asm_clobbers,
2246 GET_MODE (DF_REF_REAL_REG (def)),
2247 dregno);
2254 /* Set up ELIMINABLE_REGSET, IRA_NO_ALLOC_REGS, and
2255 REGS_EVER_LIVE. */
2256 void
2257 ira_setup_eliminable_regset (void)
2259 #ifdef ELIMINABLE_REGS
2260 int i;
2261 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
2262 #endif
2263 /* FIXME: If EXIT_IGNORE_STACK is set, we will not save and restore
2264 sp for alloca. So we can't eliminate the frame pointer in that
2265 case. At some point, we should improve this by emitting the
2266 sp-adjusting insns for this case. */
2267 frame_pointer_needed
2268 = (! flag_omit_frame_pointer
2269 || (cfun->calls_alloca && EXIT_IGNORE_STACK)
2270 /* We need the frame pointer to catch stack overflow exceptions
2271 if the stack pointer is moving. */
2272 || (flag_stack_check && STACK_CHECK_MOVING_SP)
2273 || crtl->accesses_prior_frames
2274 || (SUPPORTS_STACK_ALIGNMENT && crtl->stack_realign_needed)
2275 /* We need a frame pointer for all Cilk Plus functions that use
2276 Cilk keywords. */
2277 || (flag_cilkplus && cfun->is_cilk_function)
2278 || targetm.frame_pointer_required ());
2280 /* The chance that FRAME_POINTER_NEEDED is changed from inspecting
2281 RTL is very small. So if we use frame pointer for RA and RTL
2282 actually prevents this, we will spill pseudos assigned to the
2283 frame pointer in LRA. */
2285 if (frame_pointer_needed)
2286 df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
2288 COPY_HARD_REG_SET (ira_no_alloc_regs, no_unit_alloc_regs);
2289 CLEAR_HARD_REG_SET (eliminable_regset);
2291 compute_regs_asm_clobbered ();
2293 /* Build the regset of all eliminable registers and show we can't
2294 use those that we already know won't be eliminated. */
2295 #ifdef ELIMINABLE_REGS
2296 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
2298 bool cannot_elim
2299 = (! targetm.can_eliminate (eliminables[i].from, eliminables[i].to)
2300 || (eliminables[i].to == STACK_POINTER_REGNUM && frame_pointer_needed));
2302 if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, eliminables[i].from))
2304 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
2306 if (cannot_elim)
2307 SET_HARD_REG_BIT (ira_no_alloc_regs, eliminables[i].from);
2309 else if (cannot_elim)
2310 error ("%s cannot be used in asm here",
2311 reg_names[eliminables[i].from]);
2312 else
2313 df_set_regs_ever_live (eliminables[i].from, true);
2315 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2316 if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, HARD_FRAME_POINTER_REGNUM))
2318 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
2319 if (frame_pointer_needed)
2320 SET_HARD_REG_BIT (ira_no_alloc_regs, HARD_FRAME_POINTER_REGNUM);
2322 else if (frame_pointer_needed)
2323 error ("%s cannot be used in asm here",
2324 reg_names[HARD_FRAME_POINTER_REGNUM]);
2325 else
2326 df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
2327 #endif
2329 #else
2330 if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, HARD_FRAME_POINTER_REGNUM))
2332 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
2333 if (frame_pointer_needed)
2334 SET_HARD_REG_BIT (ira_no_alloc_regs, FRAME_POINTER_REGNUM);
2336 else if (frame_pointer_needed)
2337 error ("%s cannot be used in asm here", reg_names[FRAME_POINTER_REGNUM]);
2338 else
2339 df_set_regs_ever_live (FRAME_POINTER_REGNUM, true);
2340 #endif
2345 /* Vector of substitutions of register numbers,
2346 used to map pseudo regs into hardware regs.
2347 This is set up as a result of register allocation.
2348 Element N is the hard reg assigned to pseudo reg N,
2349 or is -1 if no hard reg was assigned.
2350 If N is a hard reg number, element N is N. */
2351 short *reg_renumber;
2353 /* Set up REG_RENUMBER and CALLER_SAVE_NEEDED (used by reload) from
2354 the allocation found by IRA. */
2355 static void
2356 setup_reg_renumber (void)
2358 int regno, hard_regno;
2359 ira_allocno_t a;
2360 ira_allocno_iterator ai;
2362 caller_save_needed = 0;
2363 FOR_EACH_ALLOCNO (a, ai)
2365 if (ira_use_lra_p && ALLOCNO_CAP_MEMBER (a) != NULL)
2366 continue;
2367 /* There are no caps at this point. */
2368 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2369 if (! ALLOCNO_ASSIGNED_P (a))
2370 /* It can happen if A is not referenced but partially anticipated
2371 somewhere in a region. */
2372 ALLOCNO_ASSIGNED_P (a) = true;
2373 ira_free_allocno_updated_costs (a);
2374 hard_regno = ALLOCNO_HARD_REGNO (a);
2375 regno = ALLOCNO_REGNO (a);
2376 reg_renumber[regno] = (hard_regno < 0 ? -1 : hard_regno);
2377 if (hard_regno >= 0)
2379 int i, nwords;
2380 enum reg_class pclass;
2381 ira_object_t obj;
2383 pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
2384 nwords = ALLOCNO_NUM_OBJECTS (a);
2385 for (i = 0; i < nwords; i++)
2387 obj = ALLOCNO_OBJECT (a, i);
2388 IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2389 reg_class_contents[pclass]);
2391 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
2392 && ira_hard_reg_set_intersection_p (hard_regno, ALLOCNO_MODE (a),
2393 call_used_reg_set))
2395 ira_assert (!optimize || flag_caller_saves
2396 || (ALLOCNO_CALLS_CROSSED_NUM (a)
2397 == ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a))
2398 || regno >= ira_reg_equiv_len
2399 || ira_equiv_no_lvalue_p (regno));
2400 caller_save_needed = 1;
2406 /* Set up allocno assignment flags for further allocation
2407 improvements. */
2408 static void
2409 setup_allocno_assignment_flags (void)
2411 int hard_regno;
2412 ira_allocno_t a;
2413 ira_allocno_iterator ai;
2415 FOR_EACH_ALLOCNO (a, ai)
2417 if (! ALLOCNO_ASSIGNED_P (a))
2418 /* It can happen if A is not referenced but partially anticipated
2419 somewhere in a region. */
2420 ira_free_allocno_updated_costs (a);
2421 hard_regno = ALLOCNO_HARD_REGNO (a);
2422 /* Don't assign hard registers to allocnos which are destination
2423 of removed store at the end of loop. It has no sense to keep
2424 the same value in different hard registers. It is also
2425 impossible to assign hard registers correctly to such
2426 allocnos because the cost info and info about intersected
2427 calls are incorrect for them. */
2428 ALLOCNO_ASSIGNED_P (a) = (hard_regno >= 0
2429 || ALLOCNO_EMIT_DATA (a)->mem_optimized_dest_p
2430 || (ALLOCNO_MEMORY_COST (a)
2431 - ALLOCNO_CLASS_COST (a)) < 0);
2432 ira_assert
2433 (hard_regno < 0
2434 || ira_hard_reg_in_set_p (hard_regno, ALLOCNO_MODE (a),
2435 reg_class_contents[ALLOCNO_CLASS (a)]));
2439 /* Evaluate overall allocation cost and the costs for using hard
2440 registers and memory for allocnos. */
2441 static void
2442 calculate_allocation_cost (void)
2444 int hard_regno, cost;
2445 ira_allocno_t a;
2446 ira_allocno_iterator ai;
2448 ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
2449 FOR_EACH_ALLOCNO (a, ai)
2451 hard_regno = ALLOCNO_HARD_REGNO (a);
2452 ira_assert (hard_regno < 0
2453 || (ira_hard_reg_in_set_p
2454 (hard_regno, ALLOCNO_MODE (a),
2455 reg_class_contents[ALLOCNO_CLASS (a)])));
2456 if (hard_regno < 0)
2458 cost = ALLOCNO_MEMORY_COST (a);
2459 ira_mem_cost += cost;
2461 else if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
2463 cost = (ALLOCNO_HARD_REG_COSTS (a)
2464 [ira_class_hard_reg_index
2465 [ALLOCNO_CLASS (a)][hard_regno]]);
2466 ira_reg_cost += cost;
2468 else
2470 cost = ALLOCNO_CLASS_COST (a);
2471 ira_reg_cost += cost;
2473 ira_overall_cost += cost;
2476 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2478 fprintf (ira_dump_file,
2479 "+++Costs: overall %d, reg %d, mem %d, ld %d, st %d, move %d\n",
2480 ira_overall_cost, ira_reg_cost, ira_mem_cost,
2481 ira_load_cost, ira_store_cost, ira_shuffle_cost);
2482 fprintf (ira_dump_file, "+++ move loops %d, new jumps %d\n",
2483 ira_move_loops_num, ira_additional_jumps_num);
2488 #ifdef ENABLE_IRA_CHECKING
2489 /* Check the correctness of the allocation. We do need this because
2490 of complicated code to transform more one region internal
2491 representation into one region representation. */
2492 static void
2493 check_allocation (void)
2495 ira_allocno_t a;
2496 int hard_regno, nregs, conflict_nregs;
2497 ira_allocno_iterator ai;
2499 FOR_EACH_ALLOCNO (a, ai)
2501 int n = ALLOCNO_NUM_OBJECTS (a);
2502 int i;
2504 if (ALLOCNO_CAP_MEMBER (a) != NULL
2505 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
2506 continue;
2507 nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
2508 if (nregs == 1)
2509 /* We allocated a single hard register. */
2510 n = 1;
2511 else if (n > 1)
2512 /* We allocated multiple hard registers, and we will test
2513 conflicts in a granularity of single hard regs. */
2514 nregs = 1;
2516 for (i = 0; i < n; i++)
2518 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2519 ira_object_t conflict_obj;
2520 ira_object_conflict_iterator oci;
2521 int this_regno = hard_regno;
2522 if (n > 1)
2524 if (REG_WORDS_BIG_ENDIAN)
2525 this_regno += n - i - 1;
2526 else
2527 this_regno += i;
2529 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2531 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2532 int conflict_hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
2533 if (conflict_hard_regno < 0)
2534 continue;
2536 conflict_nregs
2537 = (hard_regno_nregs
2538 [conflict_hard_regno][ALLOCNO_MODE (conflict_a)]);
2540 if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1
2541 && conflict_nregs == ALLOCNO_NUM_OBJECTS (conflict_a))
2543 if (REG_WORDS_BIG_ENDIAN)
2544 conflict_hard_regno += (ALLOCNO_NUM_OBJECTS (conflict_a)
2545 - OBJECT_SUBWORD (conflict_obj) - 1);
2546 else
2547 conflict_hard_regno += OBJECT_SUBWORD (conflict_obj);
2548 conflict_nregs = 1;
2551 if ((conflict_hard_regno <= this_regno
2552 && this_regno < conflict_hard_regno + conflict_nregs)
2553 || (this_regno <= conflict_hard_regno
2554 && conflict_hard_regno < this_regno + nregs))
2556 fprintf (stderr, "bad allocation for %d and %d\n",
2557 ALLOCNO_REGNO (a), ALLOCNO_REGNO (conflict_a));
2558 gcc_unreachable ();
2564 #endif
2566 /* Allocate REG_EQUIV_INIT. Set up it from IRA_REG_EQUIV which should
2567 be already calculated. */
2568 static void
2569 setup_reg_equiv_init (void)
2571 int i;
2572 int max_regno = max_reg_num ();
2574 for (i = 0; i < max_regno; i++)
2575 reg_equiv_init (i) = ira_reg_equiv[i].init_insns;
2578 /* Update equiv regno from movement of FROM_REGNO to TO_REGNO. INSNS
2579 are insns which were generated for such movement. It is assumed
2580 that FROM_REGNO and TO_REGNO always have the same value at the
2581 point of any move containing such registers. This function is used
2582 to update equiv info for register shuffles on the region borders
2583 and for caller save/restore insns. */
2584 void
2585 ira_update_equiv_info_by_shuffle_insn (int to_regno, int from_regno, rtx_insn *insns)
2587 rtx_insn *insn;
2588 rtx x, note;
2590 if (! ira_reg_equiv[from_regno].defined_p
2591 && (! ira_reg_equiv[to_regno].defined_p
2592 || ((x = ira_reg_equiv[to_regno].memory) != NULL_RTX
2593 && ! MEM_READONLY_P (x))))
2594 return;
2595 insn = insns;
2596 if (NEXT_INSN (insn) != NULL_RTX)
2598 if (! ira_reg_equiv[to_regno].defined_p)
2600 ira_assert (ira_reg_equiv[to_regno].init_insns == NULL_RTX);
2601 return;
2603 ira_reg_equiv[to_regno].defined_p = false;
2604 ira_reg_equiv[to_regno].memory
2605 = ira_reg_equiv[to_regno].constant
2606 = ira_reg_equiv[to_regno].invariant
2607 = ira_reg_equiv[to_regno].init_insns = NULL_RTX;
2608 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2609 fprintf (ira_dump_file,
2610 " Invalidating equiv info for reg %d\n", to_regno);
2611 return;
2613 /* It is possible that FROM_REGNO still has no equivalence because
2614 in shuffles to_regno<-from_regno and from_regno<-to_regno the 2nd
2615 insn was not processed yet. */
2616 if (ira_reg_equiv[from_regno].defined_p)
2618 ira_reg_equiv[to_regno].defined_p = true;
2619 if ((x = ira_reg_equiv[from_regno].memory) != NULL_RTX)
2621 ira_assert (ira_reg_equiv[from_regno].invariant == NULL_RTX
2622 && ira_reg_equiv[from_regno].constant == NULL_RTX);
2623 ira_assert (ira_reg_equiv[to_regno].memory == NULL_RTX
2624 || rtx_equal_p (ira_reg_equiv[to_regno].memory, x));
2625 ira_reg_equiv[to_regno].memory = x;
2626 if (! MEM_READONLY_P (x))
2627 /* We don't add the insn to insn init list because memory
2628 equivalence is just to say what memory is better to use
2629 when the pseudo is spilled. */
2630 return;
2632 else if ((x = ira_reg_equiv[from_regno].constant) != NULL_RTX)
2634 ira_assert (ira_reg_equiv[from_regno].invariant == NULL_RTX);
2635 ira_assert (ira_reg_equiv[to_regno].constant == NULL_RTX
2636 || rtx_equal_p (ira_reg_equiv[to_regno].constant, x));
2637 ira_reg_equiv[to_regno].constant = x;
2639 else
2641 x = ira_reg_equiv[from_regno].invariant;
2642 ira_assert (x != NULL_RTX);
2643 ira_assert (ira_reg_equiv[to_regno].invariant == NULL_RTX
2644 || rtx_equal_p (ira_reg_equiv[to_regno].invariant, x));
2645 ira_reg_equiv[to_regno].invariant = x;
2647 if (find_reg_note (insn, REG_EQUIV, x) == NULL_RTX)
2649 note = set_unique_reg_note (insn, REG_EQUIV, x);
2650 gcc_assert (note != NULL_RTX);
2651 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2653 fprintf (ira_dump_file,
2654 " Adding equiv note to insn %u for reg %d ",
2655 INSN_UID (insn), to_regno);
2656 dump_value_slim (ira_dump_file, x, 1);
2657 fprintf (ira_dump_file, "\n");
2661 ira_reg_equiv[to_regno].init_insns
2662 = gen_rtx_INSN_LIST (VOIDmode, insn,
2663 ira_reg_equiv[to_regno].init_insns);
2664 if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2665 fprintf (ira_dump_file,
2666 " Adding equiv init move insn %u to reg %d\n",
2667 INSN_UID (insn), to_regno);
2670 /* Fix values of array REG_EQUIV_INIT after live range splitting done
2671 by IRA. */
2672 static void
2673 fix_reg_equiv_init (void)
2675 int max_regno = max_reg_num ();
2676 int i, new_regno, max;
2677 rtx x, prev, next, insn, set;
2679 if (max_regno_before_ira < max_regno)
2681 max = vec_safe_length (reg_equivs);
2682 grow_reg_equivs ();
2683 for (i = FIRST_PSEUDO_REGISTER; i < max; i++)
2684 for (prev = NULL_RTX, x = reg_equiv_init (i);
2685 x != NULL_RTX;
2686 x = next)
2688 next = XEXP (x, 1);
2689 insn = XEXP (x, 0);
2690 set = single_set (insn);
2691 ira_assert (set != NULL_RTX
2692 && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))));
2693 if (REG_P (SET_DEST (set))
2694 && ((int) REGNO (SET_DEST (set)) == i
2695 || (int) ORIGINAL_REGNO (SET_DEST (set)) == i))
2696 new_regno = REGNO (SET_DEST (set));
2697 else if (REG_P (SET_SRC (set))
2698 && ((int) REGNO (SET_SRC (set)) == i
2699 || (int) ORIGINAL_REGNO (SET_SRC (set)) == i))
2700 new_regno = REGNO (SET_SRC (set));
2701 else
2702 gcc_unreachable ();
2703 if (new_regno == i)
2704 prev = x;
2705 else
2707 /* Remove the wrong list element. */
2708 if (prev == NULL_RTX)
2709 reg_equiv_init (i) = next;
2710 else
2711 XEXP (prev, 1) = next;
2712 XEXP (x, 1) = reg_equiv_init (new_regno);
2713 reg_equiv_init (new_regno) = x;
2719 #ifdef ENABLE_IRA_CHECKING
2720 /* Print redundant memory-memory copies. */
2721 static void
2722 print_redundant_copies (void)
2724 int hard_regno;
2725 ira_allocno_t a;
2726 ira_copy_t cp, next_cp;
2727 ira_allocno_iterator ai;
2729 FOR_EACH_ALLOCNO (a, ai)
2731 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2732 /* It is a cap. */
2733 continue;
2734 hard_regno = ALLOCNO_HARD_REGNO (a);
2735 if (hard_regno >= 0)
2736 continue;
2737 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2738 if (cp->first == a)
2739 next_cp = cp->next_first_allocno_copy;
2740 else
2742 next_cp = cp->next_second_allocno_copy;
2743 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL
2744 && cp->insn != NULL_RTX
2745 && ALLOCNO_HARD_REGNO (cp->first) == hard_regno)
2746 fprintf (ira_dump_file,
2747 " Redundant move from %d(freq %d):%d\n",
2748 INSN_UID (cp->insn), cp->freq, hard_regno);
2752 #endif
2754 /* Setup preferred and alternative classes for new pseudo-registers
2755 created by IRA starting with START. */
2756 static void
2757 setup_preferred_alternate_classes_for_new_pseudos (int start)
2759 int i, old_regno;
2760 int max_regno = max_reg_num ();
2762 for (i = start; i < max_regno; i++)
2764 old_regno = ORIGINAL_REGNO (regno_reg_rtx[i]);
2765 ira_assert (i != old_regno);
2766 setup_reg_classes (i, reg_preferred_class (old_regno),
2767 reg_alternate_class (old_regno),
2768 reg_allocno_class (old_regno));
2769 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2770 fprintf (ira_dump_file,
2771 " New r%d: setting preferred %s, alternative %s\n",
2772 i, reg_class_names[reg_preferred_class (old_regno)],
2773 reg_class_names[reg_alternate_class (old_regno)]);
2778 /* The number of entries allocated in teg_info. */
2779 static int allocated_reg_info_size;
2781 /* Regional allocation can create new pseudo-registers. This function
2782 expands some arrays for pseudo-registers. */
2783 static void
2784 expand_reg_info (void)
2786 int i;
2787 int size = max_reg_num ();
2789 resize_reg_info ();
2790 for (i = allocated_reg_info_size; i < size; i++)
2791 setup_reg_classes (i, GENERAL_REGS, ALL_REGS, GENERAL_REGS);
2792 setup_preferred_alternate_classes_for_new_pseudos (allocated_reg_info_size);
2793 allocated_reg_info_size = size;
2796 /* Return TRUE if there is too high register pressure in the function.
2797 It is used to decide when stack slot sharing is worth to do. */
2798 static bool
2799 too_high_register_pressure_p (void)
2801 int i;
2802 enum reg_class pclass;
2804 for (i = 0; i < ira_pressure_classes_num; i++)
2806 pclass = ira_pressure_classes[i];
2807 if (ira_loop_tree_root->reg_pressure[pclass] > 10000)
2808 return true;
2810 return false;
2815 /* Indicate that hard register number FROM was eliminated and replaced with
2816 an offset from hard register number TO. The status of hard registers live
2817 at the start of a basic block is updated by replacing a use of FROM with
2818 a use of TO. */
2820 void
2821 mark_elimination (int from, int to)
2823 basic_block bb;
2824 bitmap r;
2826 FOR_EACH_BB_FN (bb, cfun)
2828 r = DF_LR_IN (bb);
2829 if (bitmap_bit_p (r, from))
2831 bitmap_clear_bit (r, from);
2832 bitmap_set_bit (r, to);
2834 if (! df_live)
2835 continue;
2836 r = DF_LIVE_IN (bb);
2837 if (bitmap_bit_p (r, from))
2839 bitmap_clear_bit (r, from);
2840 bitmap_set_bit (r, to);
2847 /* The length of the following array. */
2848 int ira_reg_equiv_len;
2850 /* Info about equiv. info for each register. */
2851 struct ira_reg_equiv_s *ira_reg_equiv;
2853 /* Expand ira_reg_equiv if necessary. */
2854 void
2855 ira_expand_reg_equiv (void)
2857 int old = ira_reg_equiv_len;
2859 if (ira_reg_equiv_len > max_reg_num ())
2860 return;
2861 ira_reg_equiv_len = max_reg_num () * 3 / 2 + 1;
2862 ira_reg_equiv
2863 = (struct ira_reg_equiv_s *) xrealloc (ira_reg_equiv,
2864 ira_reg_equiv_len
2865 * sizeof (struct ira_reg_equiv_s));
2866 gcc_assert (old < ira_reg_equiv_len);
2867 memset (ira_reg_equiv + old, 0,
2868 sizeof (struct ira_reg_equiv_s) * (ira_reg_equiv_len - old));
2871 static void
2872 init_reg_equiv (void)
2874 ira_reg_equiv_len = 0;
2875 ira_reg_equiv = NULL;
2876 ira_expand_reg_equiv ();
2879 static void
2880 finish_reg_equiv (void)
2882 free (ira_reg_equiv);
2887 struct equivalence
2889 /* Set when a REG_EQUIV note is found or created. Use to
2890 keep track of what memory accesses might be created later,
2891 e.g. by reload. */
2892 rtx replacement;
2893 rtx *src_p;
2894 /* The list of each instruction which initializes this register. */
2895 rtx init_insns;
2896 /* Loop depth is used to recognize equivalences which appear
2897 to be present within the same loop (or in an inner loop). */
2898 int loop_depth;
2899 /* Nonzero if this had a preexisting REG_EQUIV note. */
2900 int is_arg_equivalence;
2901 /* Set when an attempt should be made to replace a register
2902 with the associated src_p entry. */
2903 char replace;
2906 /* reg_equiv[N] (where N is a pseudo reg number) is the equivalence
2907 structure for that register. */
2908 static struct equivalence *reg_equiv;
2910 /* Used for communication between the following two functions: contains
2911 a MEM that we wish to ensure remains unchanged. */
2912 static rtx equiv_mem;
2914 /* Set nonzero if EQUIV_MEM is modified. */
2915 static int equiv_mem_modified;
2917 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
2918 Called via note_stores. */
2919 static void
2920 validate_equiv_mem_from_store (rtx dest, const_rtx set ATTRIBUTE_UNUSED,
2921 void *data ATTRIBUTE_UNUSED)
2923 if ((REG_P (dest)
2924 && reg_overlap_mentioned_p (dest, equiv_mem))
2925 || (MEM_P (dest)
2926 && anti_dependence (equiv_mem, dest)))
2927 equiv_mem_modified = 1;
2930 /* Verify that no store between START and the death of REG invalidates
2931 MEMREF. MEMREF is invalidated by modifying a register used in MEMREF,
2932 by storing into an overlapping memory location, or with a non-const
2933 CALL_INSN.
2935 Return 1 if MEMREF remains valid. */
2936 static int
2937 validate_equiv_mem (rtx_insn *start, rtx reg, rtx memref)
2939 rtx_insn *insn;
2940 rtx note;
2942 equiv_mem = memref;
2943 equiv_mem_modified = 0;
2945 /* If the memory reference has side effects or is volatile, it isn't a
2946 valid equivalence. */
2947 if (side_effects_p (memref))
2948 return 0;
2950 for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
2952 if (! INSN_P (insn))
2953 continue;
2955 if (find_reg_note (insn, REG_DEAD, reg))
2956 return 1;
2958 /* This used to ignore readonly memory and const/pure calls. The problem
2959 is the equivalent form may reference a pseudo which gets assigned a
2960 call clobbered hard reg. When we later replace REG with its
2961 equivalent form, the value in the call-clobbered reg has been
2962 changed and all hell breaks loose. */
2963 if (CALL_P (insn))
2964 return 0;
2966 note_stores (PATTERN (insn), validate_equiv_mem_from_store, NULL);
2968 /* If a register mentioned in MEMREF is modified via an
2969 auto-increment, we lose the equivalence. Do the same if one
2970 dies; although we could extend the life, it doesn't seem worth
2971 the trouble. */
2973 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2974 if ((REG_NOTE_KIND (note) == REG_INC
2975 || REG_NOTE_KIND (note) == REG_DEAD)
2976 && REG_P (XEXP (note, 0))
2977 && reg_overlap_mentioned_p (XEXP (note, 0), memref))
2978 return 0;
2981 return 0;
2984 /* Returns zero if X is known to be invariant. */
2985 static int
2986 equiv_init_varies_p (rtx x)
2988 RTX_CODE code = GET_CODE (x);
2989 int i;
2990 const char *fmt;
2992 switch (code)
2994 case MEM:
2995 return !MEM_READONLY_P (x) || equiv_init_varies_p (XEXP (x, 0));
2997 case CONST:
2998 CASE_CONST_ANY:
2999 case SYMBOL_REF:
3000 case LABEL_REF:
3001 return 0;
3003 case REG:
3004 return reg_equiv[REGNO (x)].replace == 0 && rtx_varies_p (x, 0);
3006 case ASM_OPERANDS:
3007 if (MEM_VOLATILE_P (x))
3008 return 1;
3010 /* Fall through. */
3012 default:
3013 break;
3016 fmt = GET_RTX_FORMAT (code);
3017 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3018 if (fmt[i] == 'e')
3020 if (equiv_init_varies_p (XEXP (x, i)))
3021 return 1;
3023 else if (fmt[i] == 'E')
3025 int j;
3026 for (j = 0; j < XVECLEN (x, i); j++)
3027 if (equiv_init_varies_p (XVECEXP (x, i, j)))
3028 return 1;
3031 return 0;
3034 /* Returns nonzero if X (used to initialize register REGNO) is movable.
3035 X is only movable if the registers it uses have equivalent initializations
3036 which appear to be within the same loop (or in an inner loop) and movable
3037 or if they are not candidates for local_alloc and don't vary. */
3038 static int
3039 equiv_init_movable_p (rtx x, int regno)
3041 int i, j;
3042 const char *fmt;
3043 enum rtx_code code = GET_CODE (x);
3045 switch (code)
3047 case SET:
3048 return equiv_init_movable_p (SET_SRC (x), regno);
3050 case CC0:
3051 case CLOBBER:
3052 return 0;
3054 case PRE_INC:
3055 case PRE_DEC:
3056 case POST_INC:
3057 case POST_DEC:
3058 case PRE_MODIFY:
3059 case POST_MODIFY:
3060 return 0;
3062 case REG:
3063 return ((reg_equiv[REGNO (x)].loop_depth >= reg_equiv[regno].loop_depth
3064 && reg_equiv[REGNO (x)].replace)
3065 || (REG_BASIC_BLOCK (REGNO (x)) < NUM_FIXED_BLOCKS
3066 && ! rtx_varies_p (x, 0)));
3068 case UNSPEC_VOLATILE:
3069 return 0;
3071 case ASM_OPERANDS:
3072 if (MEM_VOLATILE_P (x))
3073 return 0;
3075 /* Fall through. */
3077 default:
3078 break;
3081 fmt = GET_RTX_FORMAT (code);
3082 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3083 switch (fmt[i])
3085 case 'e':
3086 if (! equiv_init_movable_p (XEXP (x, i), regno))
3087 return 0;
3088 break;
3089 case 'E':
3090 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3091 if (! equiv_init_movable_p (XVECEXP (x, i, j), regno))
3092 return 0;
3093 break;
3096 return 1;
3099 /* TRUE if X uses any registers for which reg_equiv[REGNO].replace is
3100 true. */
3101 static int
3102 contains_replace_regs (rtx x)
3104 int i, j;
3105 const char *fmt;
3106 enum rtx_code code = GET_CODE (x);
3108 switch (code)
3110 case CONST:
3111 case LABEL_REF:
3112 case SYMBOL_REF:
3113 CASE_CONST_ANY:
3114 case PC:
3115 case CC0:
3116 case HIGH:
3117 return 0;
3119 case REG:
3120 return reg_equiv[REGNO (x)].replace;
3122 default:
3123 break;
3126 fmt = GET_RTX_FORMAT (code);
3127 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3128 switch (fmt[i])
3130 case 'e':
3131 if (contains_replace_regs (XEXP (x, i)))
3132 return 1;
3133 break;
3134 case 'E':
3135 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3136 if (contains_replace_regs (XVECEXP (x, i, j)))
3137 return 1;
3138 break;
3141 return 0;
3144 /* TRUE if X references a memory location that would be affected by a store
3145 to MEMREF. */
3146 static int
3147 memref_referenced_p (rtx memref, rtx x)
3149 int i, j;
3150 const char *fmt;
3151 enum rtx_code code = GET_CODE (x);
3153 switch (code)
3155 case CONST:
3156 case LABEL_REF:
3157 case SYMBOL_REF:
3158 CASE_CONST_ANY:
3159 case PC:
3160 case CC0:
3161 case HIGH:
3162 case LO_SUM:
3163 return 0;
3165 case REG:
3166 return (reg_equiv[REGNO (x)].replacement
3167 && memref_referenced_p (memref,
3168 reg_equiv[REGNO (x)].replacement));
3170 case MEM:
3171 if (true_dependence (memref, VOIDmode, x))
3172 return 1;
3173 break;
3175 case SET:
3176 /* If we are setting a MEM, it doesn't count (its address does), but any
3177 other SET_DEST that has a MEM in it is referencing the MEM. */
3178 if (MEM_P (SET_DEST (x)))
3180 if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
3181 return 1;
3183 else if (memref_referenced_p (memref, SET_DEST (x)))
3184 return 1;
3186 return memref_referenced_p (memref, SET_SRC (x));
3188 default:
3189 break;
3192 fmt = GET_RTX_FORMAT (code);
3193 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3194 switch (fmt[i])
3196 case 'e':
3197 if (memref_referenced_p (memref, XEXP (x, i)))
3198 return 1;
3199 break;
3200 case 'E':
3201 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3202 if (memref_referenced_p (memref, XVECEXP (x, i, j)))
3203 return 1;
3204 break;
3207 return 0;
3210 /* TRUE if some insn in the range (START, END] references a memory location
3211 that would be affected by a store to MEMREF. */
3212 static int
3213 memref_used_between_p (rtx memref, rtx_insn *start, rtx_insn *end)
3215 rtx_insn *insn;
3217 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
3218 insn = NEXT_INSN (insn))
3220 if (!NONDEBUG_INSN_P (insn))
3221 continue;
3223 if (memref_referenced_p (memref, PATTERN (insn)))
3224 return 1;
3226 /* Nonconst functions may access memory. */
3227 if (CALL_P (insn) && (! RTL_CONST_CALL_P (insn)))
3228 return 1;
3231 return 0;
3234 /* Mark REG as having no known equivalence.
3235 Some instructions might have been processed before and furnished
3236 with REG_EQUIV notes for this register; these notes will have to be
3237 removed.
3238 STORE is the piece of RTL that does the non-constant / conflicting
3239 assignment - a SET, CLOBBER or REG_INC note. It is currently not used,
3240 but needs to be there because this function is called from note_stores. */
3241 static void
3242 no_equiv (rtx reg, const_rtx store ATTRIBUTE_UNUSED,
3243 void *data ATTRIBUTE_UNUSED)
3245 int regno;
3246 rtx list;
3248 if (!REG_P (reg))
3249 return;
3250 regno = REGNO (reg);
3251 list = reg_equiv[regno].init_insns;
3252 if (list == const0_rtx)
3253 return;
3254 reg_equiv[regno].init_insns = const0_rtx;
3255 reg_equiv[regno].replacement = NULL_RTX;
3256 /* This doesn't matter for equivalences made for argument registers, we
3257 should keep their initialization insns. */
3258 if (reg_equiv[regno].is_arg_equivalence)
3259 return;
3260 ira_reg_equiv[regno].defined_p = false;
3261 ira_reg_equiv[regno].init_insns = NULL_RTX;
3262 for (; list; list = XEXP (list, 1))
3264 rtx insn = XEXP (list, 0);
3265 remove_note (insn, find_reg_note (insn, REG_EQUIV, NULL_RTX));
3269 /* Check whether the SUBREG is a paradoxical subreg and set the result
3270 in PDX_SUBREGS. */
3272 static void
3273 set_paradoxical_subreg (rtx_insn *insn, bool *pdx_subregs)
3275 subrtx_iterator::array_type array;
3276 FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
3278 const_rtx subreg = *iter;
3279 if (GET_CODE (subreg) == SUBREG)
3281 const_rtx reg = SUBREG_REG (subreg);
3282 if (REG_P (reg) && paradoxical_subreg_p (subreg))
3283 pdx_subregs[REGNO (reg)] = true;
3288 /* In DEBUG_INSN location adjust REGs from CLEARED_REGS bitmap to the
3289 equivalent replacement. */
3291 static rtx
3292 adjust_cleared_regs (rtx loc, const_rtx old_rtx ATTRIBUTE_UNUSED, void *data)
3294 if (REG_P (loc))
3296 bitmap cleared_regs = (bitmap) data;
3297 if (bitmap_bit_p (cleared_regs, REGNO (loc)))
3298 return simplify_replace_fn_rtx (copy_rtx (*reg_equiv[REGNO (loc)].src_p),
3299 NULL_RTX, adjust_cleared_regs, data);
3301 return NULL_RTX;
3304 /* Nonzero if we recorded an equivalence for a LABEL_REF. */
3305 static int recorded_label_ref;
3307 /* Find registers that are equivalent to a single value throughout the
3308 compilation (either because they can be referenced in memory or are
3309 set once from a single constant). Lower their priority for a
3310 register.
3312 If such a register is only referenced once, try substituting its
3313 value into the using insn. If it succeeds, we can eliminate the
3314 register completely.
3316 Initialize init_insns in ira_reg_equiv array.
3318 Return non-zero if jump label rebuilding should be done. */
3319 static int
3320 update_equiv_regs (void)
3322 rtx_insn *insn;
3323 basic_block bb;
3324 int loop_depth;
3325 bitmap cleared_regs;
3326 bool *pdx_subregs;
3328 /* We need to keep track of whether or not we recorded a LABEL_REF so
3329 that we know if the jump optimizer needs to be rerun. */
3330 recorded_label_ref = 0;
3332 /* Use pdx_subregs to show whether a reg is used in a paradoxical
3333 subreg. */
3334 pdx_subregs = XCNEWVEC (bool, max_regno);
3336 reg_equiv = XCNEWVEC (struct equivalence, max_regno);
3337 grow_reg_equivs ();
3339 init_alias_analysis ();
3341 /* Scan insns and set pdx_subregs[regno] if the reg is used in a
3342 paradoxical subreg. Don't set such reg sequivalent to a mem,
3343 because lra will not substitute such equiv memory in order to
3344 prevent access beyond allocated memory for paradoxical memory subreg. */
3345 FOR_EACH_BB_FN (bb, cfun)
3346 FOR_BB_INSNS (bb, insn)
3347 if (NONDEBUG_INSN_P (insn))
3348 set_paradoxical_subreg (insn, pdx_subregs);
3350 /* Scan the insns and find which registers have equivalences. Do this
3351 in a separate scan of the insns because (due to -fcse-follow-jumps)
3352 a register can be set below its use. */
3353 FOR_EACH_BB_FN (bb, cfun)
3355 loop_depth = bb_loop_depth (bb);
3357 for (insn = BB_HEAD (bb);
3358 insn != NEXT_INSN (BB_END (bb));
3359 insn = NEXT_INSN (insn))
3361 rtx note;
3362 rtx set;
3363 rtx dest, src;
3364 int regno;
3366 if (! INSN_P (insn))
3367 continue;
3369 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3370 if (REG_NOTE_KIND (note) == REG_INC)
3371 no_equiv (XEXP (note, 0), note, NULL);
3373 set = single_set (insn);
3375 /* If this insn contains more (or less) than a single SET,
3376 only mark all destinations as having no known equivalence. */
3377 if (set == 0)
3379 note_stores (PATTERN (insn), no_equiv, NULL);
3380 continue;
3382 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3384 int i;
3386 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
3388 rtx part = XVECEXP (PATTERN (insn), 0, i);
3389 if (part != set)
3390 note_stores (part, no_equiv, NULL);
3394 dest = SET_DEST (set);
3395 src = SET_SRC (set);
3397 /* See if this is setting up the equivalence between an argument
3398 register and its stack slot. */
3399 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
3400 if (note)
3402 gcc_assert (REG_P (dest));
3403 regno = REGNO (dest);
3405 /* Note that we don't want to clear init_insns in
3406 ira_reg_equiv even if there are multiple sets of this
3407 register. */
3408 reg_equiv[regno].is_arg_equivalence = 1;
3410 /* The insn result can have equivalence memory although
3411 the equivalence is not set up by the insn. We add
3412 this insn to init insns as it is a flag for now that
3413 regno has an equivalence. We will remove the insn
3414 from init insn list later. */
3415 if (rtx_equal_p (src, XEXP (note, 0)) || MEM_P (XEXP (note, 0)))
3416 ira_reg_equiv[regno].init_insns
3417 = gen_rtx_INSN_LIST (VOIDmode, insn,
3418 ira_reg_equiv[regno].init_insns);
3420 /* Continue normally in case this is a candidate for
3421 replacements. */
3424 if (!optimize)
3425 continue;
3427 /* We only handle the case of a pseudo register being set
3428 once, or always to the same value. */
3429 /* ??? The mn10200 port breaks if we add equivalences for
3430 values that need an ADDRESS_REGS register and set them equivalent
3431 to a MEM of a pseudo. The actual problem is in the over-conservative
3432 handling of INPADDR_ADDRESS / INPUT_ADDRESS / INPUT triples in
3433 calculate_needs, but we traditionally work around this problem
3434 here by rejecting equivalences when the destination is in a register
3435 that's likely spilled. This is fragile, of course, since the
3436 preferred class of a pseudo depends on all instructions that set
3437 or use it. */
3439 if (!REG_P (dest)
3440 || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
3441 || reg_equiv[regno].init_insns == const0_rtx
3442 || (targetm.class_likely_spilled_p (reg_preferred_class (regno))
3443 && MEM_P (src) && ! reg_equiv[regno].is_arg_equivalence))
3445 /* This might be setting a SUBREG of a pseudo, a pseudo that is
3446 also set somewhere else to a constant. */
3447 note_stores (set, no_equiv, NULL);
3448 continue;
3451 /* Don't set reg (if pdx_subregs[regno] == true) equivalent to a mem. */
3452 if (MEM_P (src) && pdx_subregs[regno])
3454 note_stores (set, no_equiv, NULL);
3455 continue;
3458 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
3460 /* cse sometimes generates function invariants, but doesn't put a
3461 REG_EQUAL note on the insn. Since this note would be redundant,
3462 there's no point creating it earlier than here. */
3463 if (! note && ! rtx_varies_p (src, 0))
3464 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
3466 /* Don't bother considering a REG_EQUAL note containing an EXPR_LIST
3467 since it represents a function call */
3468 if (note && GET_CODE (XEXP (note, 0)) == EXPR_LIST)
3469 note = NULL_RTX;
3471 if (DF_REG_DEF_COUNT (regno) != 1
3472 && (! note
3473 || rtx_varies_p (XEXP (note, 0), 0)
3474 || (reg_equiv[regno].replacement
3475 && ! rtx_equal_p (XEXP (note, 0),
3476 reg_equiv[regno].replacement))))
3478 no_equiv (dest, set, NULL);
3479 continue;
3481 /* Record this insn as initializing this register. */
3482 reg_equiv[regno].init_insns
3483 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv[regno].init_insns);
3485 /* If this register is known to be equal to a constant, record that
3486 it is always equivalent to the constant. */
3487 if (DF_REG_DEF_COUNT (regno) == 1
3488 && note && ! rtx_varies_p (XEXP (note, 0), 0))
3490 rtx note_value = XEXP (note, 0);
3491 remove_note (insn, note);
3492 set_unique_reg_note (insn, REG_EQUIV, note_value);
3495 /* If this insn introduces a "constant" register, decrease the priority
3496 of that register. Record this insn if the register is only used once
3497 more and the equivalence value is the same as our source.
3499 The latter condition is checked for two reasons: First, it is an
3500 indication that it may be more efficient to actually emit the insn
3501 as written (if no registers are available, reload will substitute
3502 the equivalence). Secondly, it avoids problems with any registers
3503 dying in this insn whose death notes would be missed.
3505 If we don't have a REG_EQUIV note, see if this insn is loading
3506 a register used only in one basic block from a MEM. If so, and the
3507 MEM remains unchanged for the life of the register, add a REG_EQUIV
3508 note. */
3510 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
3512 if (note == 0 && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
3513 && MEM_P (SET_SRC (set))
3514 && validate_equiv_mem (insn, dest, SET_SRC (set)))
3515 note = set_unique_reg_note (insn, REG_EQUIV, copy_rtx (SET_SRC (set)));
3517 if (note)
3519 int regno = REGNO (dest);
3520 rtx x = XEXP (note, 0);
3522 /* If we haven't done so, record for reload that this is an
3523 equivalencing insn. */
3524 if (!reg_equiv[regno].is_arg_equivalence)
3525 ira_reg_equiv[regno].init_insns
3526 = gen_rtx_INSN_LIST (VOIDmode, insn,
3527 ira_reg_equiv[regno].init_insns);
3529 /* Record whether or not we created a REG_EQUIV note for a LABEL_REF.
3530 We might end up substituting the LABEL_REF for uses of the
3531 pseudo here or later. That kind of transformation may turn an
3532 indirect jump into a direct jump, in which case we must rerun the
3533 jump optimizer to ensure that the JUMP_LABEL fields are valid. */
3534 if (GET_CODE (x) == LABEL_REF
3535 || (GET_CODE (x) == CONST
3536 && GET_CODE (XEXP (x, 0)) == PLUS
3537 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF)))
3538 recorded_label_ref = 1;
3540 reg_equiv[regno].replacement = x;
3541 reg_equiv[regno].src_p = &SET_SRC (set);
3542 reg_equiv[regno].loop_depth = loop_depth;
3544 /* Don't mess with things live during setjmp. */
3545 if (REG_LIVE_LENGTH (regno) >= 0 && optimize)
3547 /* Note that the statement below does not affect the priority
3548 in local-alloc! */
3549 REG_LIVE_LENGTH (regno) *= 2;
3551 /* If the register is referenced exactly twice, meaning it is
3552 set once and used once, indicate that the reference may be
3553 replaced by the equivalence we computed above. Do this
3554 even if the register is only used in one block so that
3555 dependencies can be handled where the last register is
3556 used in a different block (i.e. HIGH / LO_SUM sequences)
3557 and to reduce the number of registers alive across
3558 calls. */
3560 if (REG_N_REFS (regno) == 2
3561 && (rtx_equal_p (x, src)
3562 || ! equiv_init_varies_p (src))
3563 && NONJUMP_INSN_P (insn)
3564 && equiv_init_movable_p (PATTERN (insn), regno))
3565 reg_equiv[regno].replace = 1;
3571 if (!optimize)
3572 goto out;
3574 /* A second pass, to gather additional equivalences with memory. This needs
3575 to be done after we know which registers we are going to replace. */
3577 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3579 rtx set, src, dest;
3580 unsigned regno;
3582 if (! INSN_P (insn))
3583 continue;
3585 set = single_set (insn);
3586 if (! set)
3587 continue;
3589 dest = SET_DEST (set);
3590 src = SET_SRC (set);
3592 /* If this sets a MEM to the contents of a REG that is only used
3593 in a single basic block, see if the register is always equivalent
3594 to that memory location and if moving the store from INSN to the
3595 insn that set REG is safe. If so, put a REG_EQUIV note on the
3596 initializing insn.
3598 Don't add a REG_EQUIV note if the insn already has one. The existing
3599 REG_EQUIV is likely more useful than the one we are adding.
3601 If one of the regs in the address has reg_equiv[REGNO].replace set,
3602 then we can't add this REG_EQUIV note. The reg_equiv[REGNO].replace
3603 optimization may move the set of this register immediately before
3604 insn, which puts it after reg_equiv[REGNO].init_insns, and hence
3605 the mention in the REG_EQUIV note would be to an uninitialized
3606 pseudo. */
3608 if (MEM_P (dest) && REG_P (src)
3609 && (regno = REGNO (src)) >= FIRST_PSEUDO_REGISTER
3610 && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
3611 && DF_REG_DEF_COUNT (regno) == 1
3612 && reg_equiv[regno].init_insns != 0
3613 && reg_equiv[regno].init_insns != const0_rtx
3614 && ! find_reg_note (XEXP (reg_equiv[regno].init_insns, 0),
3615 REG_EQUIV, NULL_RTX)
3616 && ! contains_replace_regs (XEXP (dest, 0))
3617 && ! pdx_subregs[regno])
3619 rtx_insn *init_insn =
3620 as_a <rtx_insn *> (XEXP (reg_equiv[regno].init_insns, 0));
3621 if (validate_equiv_mem (init_insn, src, dest)
3622 && ! memref_used_between_p (dest, init_insn, insn)
3623 /* Attaching a REG_EQUIV note will fail if INIT_INSN has
3624 multiple sets. */
3625 && set_unique_reg_note (init_insn, REG_EQUIV, copy_rtx (dest)))
3627 /* This insn makes the equivalence, not the one initializing
3628 the register. */
3629 ira_reg_equiv[regno].init_insns
3630 = gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
3631 df_notes_rescan (init_insn);
3636 cleared_regs = BITMAP_ALLOC (NULL);
3637 /* Now scan all regs killed in an insn to see if any of them are
3638 registers only used that once. If so, see if we can replace the
3639 reference with the equivalent form. If we can, delete the
3640 initializing reference and this register will go away. If we
3641 can't replace the reference, and the initializing reference is
3642 within the same loop (or in an inner loop), then move the register
3643 initialization just before the use, so that they are in the same
3644 basic block. */
3645 FOR_EACH_BB_REVERSE_FN (bb, cfun)
3647 loop_depth = bb_loop_depth (bb);
3648 for (insn = BB_END (bb);
3649 insn != PREV_INSN (BB_HEAD (bb));
3650 insn = PREV_INSN (insn))
3652 rtx link;
3654 if (! INSN_P (insn))
3655 continue;
3657 /* Don't substitute into a non-local goto, this confuses CFG. */
3658 if (JUMP_P (insn)
3659 && find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
3660 continue;
3662 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
3664 if (REG_NOTE_KIND (link) == REG_DEAD
3665 /* Make sure this insn still refers to the register. */
3666 && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
3668 int regno = REGNO (XEXP (link, 0));
3669 rtx equiv_insn;
3671 if (! reg_equiv[regno].replace
3672 || reg_equiv[regno].loop_depth < loop_depth
3673 /* There is no sense to move insns if live range
3674 shrinkage or register pressure-sensitive
3675 scheduling were done because it will not
3676 improve allocation but worsen insn schedule
3677 with a big probability. */
3678 || flag_live_range_shrinkage
3679 || (flag_sched_pressure && flag_schedule_insns))
3680 continue;
3682 /* reg_equiv[REGNO].replace gets set only when
3683 REG_N_REFS[REGNO] is 2, i.e. the register is set
3684 once and used once. (If it were only set, but
3685 not used, flow would have deleted the setting
3686 insns.) Hence there can only be one insn in
3687 reg_equiv[REGNO].init_insns. */
3688 gcc_assert (reg_equiv[regno].init_insns
3689 && !XEXP (reg_equiv[regno].init_insns, 1));
3690 equiv_insn = XEXP (reg_equiv[regno].init_insns, 0);
3692 /* We may not move instructions that can throw, since
3693 that changes basic block boundaries and we are not
3694 prepared to adjust the CFG to match. */
3695 if (can_throw_internal (equiv_insn))
3696 continue;
3698 if (asm_noperands (PATTERN (equiv_insn)) < 0
3699 && validate_replace_rtx (regno_reg_rtx[regno],
3700 *(reg_equiv[regno].src_p), insn))
3702 rtx equiv_link;
3703 rtx last_link;
3704 rtx note;
3706 /* Find the last note. */
3707 for (last_link = link; XEXP (last_link, 1);
3708 last_link = XEXP (last_link, 1))
3711 /* Append the REG_DEAD notes from equiv_insn. */
3712 equiv_link = REG_NOTES (equiv_insn);
3713 while (equiv_link)
3715 note = equiv_link;
3716 equiv_link = XEXP (equiv_link, 1);
3717 if (REG_NOTE_KIND (note) == REG_DEAD)
3719 remove_note (equiv_insn, note);
3720 XEXP (last_link, 1) = note;
3721 XEXP (note, 1) = NULL_RTX;
3722 last_link = note;
3726 remove_death (regno, insn);
3727 SET_REG_N_REFS (regno, 0);
3728 REG_FREQ (regno) = 0;
3729 delete_insn (equiv_insn);
3731 reg_equiv[regno].init_insns
3732 = XEXP (reg_equiv[regno].init_insns, 1);
3734 ira_reg_equiv[regno].init_insns = NULL_RTX;
3735 bitmap_set_bit (cleared_regs, regno);
3737 /* Move the initialization of the register to just before
3738 INSN. Update the flow information. */
3739 else if (prev_nondebug_insn (insn) != equiv_insn)
3741 rtx_insn *new_insn;
3743 new_insn = emit_insn_before (PATTERN (equiv_insn), insn);
3744 REG_NOTES (new_insn) = REG_NOTES (equiv_insn);
3745 REG_NOTES (equiv_insn) = 0;
3746 /* Rescan it to process the notes. */
3747 df_insn_rescan (new_insn);
3749 /* Make sure this insn is recognized before
3750 reload begins, otherwise
3751 eliminate_regs_in_insn will die. */
3752 INSN_CODE (new_insn) = INSN_CODE (equiv_insn);
3754 delete_insn (equiv_insn);
3756 XEXP (reg_equiv[regno].init_insns, 0) = new_insn;
3758 REG_BASIC_BLOCK (regno) = bb->index;
3759 REG_N_CALLS_CROSSED (regno) = 0;
3760 REG_FREQ_CALLS_CROSSED (regno) = 0;
3761 REG_N_THROWING_CALLS_CROSSED (regno) = 0;
3762 REG_LIVE_LENGTH (regno) = 2;
3764 if (insn == BB_HEAD (bb))
3765 BB_HEAD (bb) = PREV_INSN (insn);
3767 ira_reg_equiv[regno].init_insns
3768 = gen_rtx_INSN_LIST (VOIDmode, new_insn, NULL_RTX);
3769 bitmap_set_bit (cleared_regs, regno);
3776 if (!bitmap_empty_p (cleared_regs))
3778 FOR_EACH_BB_FN (bb, cfun)
3780 bitmap_and_compl_into (DF_LR_IN (bb), cleared_regs);
3781 bitmap_and_compl_into (DF_LR_OUT (bb), cleared_regs);
3782 if (! df_live)
3783 continue;
3784 bitmap_and_compl_into (DF_LIVE_IN (bb), cleared_regs);
3785 bitmap_and_compl_into (DF_LIVE_OUT (bb), cleared_regs);
3788 /* Last pass - adjust debug insns referencing cleared regs. */
3789 if (MAY_HAVE_DEBUG_INSNS)
3790 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3791 if (DEBUG_INSN_P (insn))
3793 rtx old_loc = INSN_VAR_LOCATION_LOC (insn);
3794 INSN_VAR_LOCATION_LOC (insn)
3795 = simplify_replace_fn_rtx (old_loc, NULL_RTX,
3796 adjust_cleared_regs,
3797 (void *) cleared_regs);
3798 if (old_loc != INSN_VAR_LOCATION_LOC (insn))
3799 df_insn_rescan (insn);
3803 BITMAP_FREE (cleared_regs);
3805 out:
3806 /* Clean up. */
3808 end_alias_analysis ();
3809 free (reg_equiv);
3810 free (pdx_subregs);
3811 return recorded_label_ref;
3816 /* Set up fields memory, constant, and invariant from init_insns in
3817 the structures of array ira_reg_equiv. */
3818 static void
3819 setup_reg_equiv (void)
3821 int i;
3822 rtx elem, prev_elem, next_elem, insn, set, x;
3824 for (i = FIRST_PSEUDO_REGISTER; i < ira_reg_equiv_len; i++)
3825 for (prev_elem = NULL, elem = ira_reg_equiv[i].init_insns;
3826 elem;
3827 prev_elem = elem, elem = next_elem)
3829 next_elem = XEXP (elem, 1);
3830 insn = XEXP (elem, 0);
3831 set = single_set (insn);
3833 /* Init insns can set up equivalence when the reg is a destination or
3834 a source (in this case the destination is memory). */
3835 if (set != 0 && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))))
3837 if ((x = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != NULL)
3839 x = XEXP (x, 0);
3840 if (REG_P (SET_DEST (set))
3841 && REGNO (SET_DEST (set)) == (unsigned int) i
3842 && ! rtx_equal_p (SET_SRC (set), x) && MEM_P (x))
3844 /* This insn reporting the equivalence but
3845 actually not setting it. Remove it from the
3846 list. */
3847 if (prev_elem == NULL)
3848 ira_reg_equiv[i].init_insns = next_elem;
3849 else
3850 XEXP (prev_elem, 1) = next_elem;
3851 elem = prev_elem;
3854 else if (REG_P (SET_DEST (set))
3855 && REGNO (SET_DEST (set)) == (unsigned int) i)
3856 x = SET_SRC (set);
3857 else
3859 gcc_assert (REG_P (SET_SRC (set))
3860 && REGNO (SET_SRC (set)) == (unsigned int) i);
3861 x = SET_DEST (set);
3863 if (! function_invariant_p (x)
3864 || ! flag_pic
3865 /* A function invariant is often CONSTANT_P but may
3866 include a register. We promise to only pass
3867 CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P. */
3868 || (CONSTANT_P (x) && LEGITIMATE_PIC_OPERAND_P (x)))
3870 /* It can happen that a REG_EQUIV note contains a MEM
3871 that is not a legitimate memory operand. As later
3872 stages of reload assume that all addresses found in
3873 the lra_regno_equiv_* arrays were originally
3874 legitimate, we ignore such REG_EQUIV notes. */
3875 if (memory_operand (x, VOIDmode))
3877 ira_reg_equiv[i].defined_p = true;
3878 ira_reg_equiv[i].memory = x;
3879 continue;
3881 else if (function_invariant_p (x))
3883 enum machine_mode mode;
3885 mode = GET_MODE (SET_DEST (set));
3886 if (GET_CODE (x) == PLUS
3887 || x == frame_pointer_rtx || x == arg_pointer_rtx)
3888 /* This is PLUS of frame pointer and a constant,
3889 or fp, or argp. */
3890 ira_reg_equiv[i].invariant = x;
3891 else if (targetm.legitimate_constant_p (mode, x))
3892 ira_reg_equiv[i].constant = x;
3893 else
3895 ira_reg_equiv[i].memory = force_const_mem (mode, x);
3896 if (ira_reg_equiv[i].memory == NULL_RTX)
3898 ira_reg_equiv[i].defined_p = false;
3899 ira_reg_equiv[i].init_insns = NULL_RTX;
3900 break;
3903 ira_reg_equiv[i].defined_p = true;
3904 continue;
3908 ira_reg_equiv[i].defined_p = false;
3909 ira_reg_equiv[i].init_insns = NULL_RTX;
3910 break;
3916 /* Print chain C to FILE. */
3917 static void
3918 print_insn_chain (FILE *file, struct insn_chain *c)
3920 fprintf (file, "insn=%d, ", INSN_UID (c->insn));
3921 bitmap_print (file, &c->live_throughout, "live_throughout: ", ", ");
3922 bitmap_print (file, &c->dead_or_set, "dead_or_set: ", "\n");
3926 /* Print all reload_insn_chains to FILE. */
3927 static void
3928 print_insn_chains (FILE *file)
3930 struct insn_chain *c;
3931 for (c = reload_insn_chain; c ; c = c->next)
3932 print_insn_chain (file, c);
3935 /* Return true if pseudo REGNO should be added to set live_throughout
3936 or dead_or_set of the insn chains for reload consideration. */
3937 static bool
3938 pseudo_for_reload_consideration_p (int regno)
3940 /* Consider spilled pseudos too for IRA because they still have a
3941 chance to get hard-registers in the reload when IRA is used. */
3942 return (reg_renumber[regno] >= 0 || ira_conflicts_p);
3945 /* Init LIVE_SUBREGS[ALLOCNUM] and LIVE_SUBREGS_USED[ALLOCNUM] using
3946 REG to the number of nregs, and INIT_VALUE to get the
3947 initialization. ALLOCNUM need not be the regno of REG. */
3948 static void
3949 init_live_subregs (bool init_value, sbitmap *live_subregs,
3950 bitmap live_subregs_used, int allocnum, rtx reg)
3952 unsigned int regno = REGNO (SUBREG_REG (reg));
3953 int size = GET_MODE_SIZE (GET_MODE (regno_reg_rtx[regno]));
3955 gcc_assert (size > 0);
3957 /* Been there, done that. */
3958 if (bitmap_bit_p (live_subregs_used, allocnum))
3959 return;
3961 /* Create a new one. */
3962 if (live_subregs[allocnum] == NULL)
3963 live_subregs[allocnum] = sbitmap_alloc (size);
3965 /* If the entire reg was live before blasting into subregs, we need
3966 to init all of the subregs to ones else init to 0. */
3967 if (init_value)
3968 bitmap_ones (live_subregs[allocnum]);
3969 else
3970 bitmap_clear (live_subregs[allocnum]);
3972 bitmap_set_bit (live_subregs_used, allocnum);
3975 /* Walk the insns of the current function and build reload_insn_chain,
3976 and record register life information. */
3977 static void
3978 build_insn_chain (void)
3980 unsigned int i;
3981 struct insn_chain **p = &reload_insn_chain;
3982 basic_block bb;
3983 struct insn_chain *c = NULL;
3984 struct insn_chain *next = NULL;
3985 bitmap live_relevant_regs = BITMAP_ALLOC (NULL);
3986 bitmap elim_regset = BITMAP_ALLOC (NULL);
3987 /* live_subregs is a vector used to keep accurate information about
3988 which hardregs are live in multiword pseudos. live_subregs and
3989 live_subregs_used are indexed by pseudo number. The live_subreg
3990 entry for a particular pseudo is only used if the corresponding
3991 element is non zero in live_subregs_used. The sbitmap size of
3992 live_subreg[allocno] is number of bytes that the pseudo can
3993 occupy. */
3994 sbitmap *live_subregs = XCNEWVEC (sbitmap, max_regno);
3995 bitmap live_subregs_used = BITMAP_ALLOC (NULL);
3997 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3998 if (TEST_HARD_REG_BIT (eliminable_regset, i))
3999 bitmap_set_bit (elim_regset, i);
4000 FOR_EACH_BB_REVERSE_FN (bb, cfun)
4002 bitmap_iterator bi;
4003 rtx_insn *insn;
4005 CLEAR_REG_SET (live_relevant_regs);
4006 bitmap_clear (live_subregs_used);
4008 EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb), 0, i, bi)
4010 if (i >= FIRST_PSEUDO_REGISTER)
4011 break;
4012 bitmap_set_bit (live_relevant_regs, i);
4015 EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb),
4016 FIRST_PSEUDO_REGISTER, i, bi)
4018 if (pseudo_for_reload_consideration_p (i))
4019 bitmap_set_bit (live_relevant_regs, i);
4022 FOR_BB_INSNS_REVERSE (bb, insn)
4024 if (!NOTE_P (insn) && !BARRIER_P (insn))
4026 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4027 df_ref def, use;
4029 c = new_insn_chain ();
4030 c->next = next;
4031 next = c;
4032 *p = c;
4033 p = &c->prev;
4035 c->insn = insn;
4036 c->block = bb->index;
4038 if (NONDEBUG_INSN_P (insn))
4039 FOR_EACH_INSN_INFO_DEF (def, insn_info)
4041 unsigned int regno = DF_REF_REGNO (def);
4043 /* Ignore may clobbers because these are generated
4044 from calls. However, every other kind of def is
4045 added to dead_or_set. */
4046 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
4048 if (regno < FIRST_PSEUDO_REGISTER)
4050 if (!fixed_regs[regno])
4051 bitmap_set_bit (&c->dead_or_set, regno);
4053 else if (pseudo_for_reload_consideration_p (regno))
4054 bitmap_set_bit (&c->dead_or_set, regno);
4057 if ((regno < FIRST_PSEUDO_REGISTER
4058 || reg_renumber[regno] >= 0
4059 || ira_conflicts_p)
4060 && (!DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)))
4062 rtx reg = DF_REF_REG (def);
4064 /* We can model subregs, but not if they are
4065 wrapped in ZERO_EXTRACTS. */
4066 if (GET_CODE (reg) == SUBREG
4067 && !DF_REF_FLAGS_IS_SET (def, DF_REF_ZERO_EXTRACT))
4069 unsigned int start = SUBREG_BYTE (reg);
4070 unsigned int last = start
4071 + GET_MODE_SIZE (GET_MODE (reg));
4073 init_live_subregs
4074 (bitmap_bit_p (live_relevant_regs, regno),
4075 live_subregs, live_subregs_used, regno, reg);
4077 if (!DF_REF_FLAGS_IS_SET
4078 (def, DF_REF_STRICT_LOW_PART))
4080 /* Expand the range to cover entire words.
4081 Bytes added here are "don't care". */
4082 start
4083 = start / UNITS_PER_WORD * UNITS_PER_WORD;
4084 last = ((last + UNITS_PER_WORD - 1)
4085 / UNITS_PER_WORD * UNITS_PER_WORD);
4088 /* Ignore the paradoxical bits. */
4089 if (last > SBITMAP_SIZE (live_subregs[regno]))
4090 last = SBITMAP_SIZE (live_subregs[regno]);
4092 while (start < last)
4094 bitmap_clear_bit (live_subregs[regno], start);
4095 start++;
4098 if (bitmap_empty_p (live_subregs[regno]))
4100 bitmap_clear_bit (live_subregs_used, regno);
4101 bitmap_clear_bit (live_relevant_regs, regno);
4103 else
4104 /* Set live_relevant_regs here because
4105 that bit has to be true to get us to
4106 look at the live_subregs fields. */
4107 bitmap_set_bit (live_relevant_regs, regno);
4109 else
4111 /* DF_REF_PARTIAL is generated for
4112 subregs, STRICT_LOW_PART, and
4113 ZERO_EXTRACT. We handle the subreg
4114 case above so here we have to keep from
4115 modeling the def as a killing def. */
4116 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL))
4118 bitmap_clear_bit (live_subregs_used, regno);
4119 bitmap_clear_bit (live_relevant_regs, regno);
4125 bitmap_and_compl_into (live_relevant_regs, elim_regset);
4126 bitmap_copy (&c->live_throughout, live_relevant_regs);
4128 if (NONDEBUG_INSN_P (insn))
4129 FOR_EACH_INSN_INFO_USE (use, insn_info)
4131 unsigned int regno = DF_REF_REGNO (use);
4132 rtx reg = DF_REF_REG (use);
4134 /* DF_REF_READ_WRITE on a use means that this use
4135 is fabricated from a def that is a partial set
4136 to a multiword reg. Here, we only model the
4137 subreg case that is not wrapped in ZERO_EXTRACT
4138 precisely so we do not need to look at the
4139 fabricated use. */
4140 if (DF_REF_FLAGS_IS_SET (use, DF_REF_READ_WRITE)
4141 && !DF_REF_FLAGS_IS_SET (use, DF_REF_ZERO_EXTRACT)
4142 && DF_REF_FLAGS_IS_SET (use, DF_REF_SUBREG))
4143 continue;
4145 /* Add the last use of each var to dead_or_set. */
4146 if (!bitmap_bit_p (live_relevant_regs, regno))
4148 if (regno < FIRST_PSEUDO_REGISTER)
4150 if (!fixed_regs[regno])
4151 bitmap_set_bit (&c->dead_or_set, regno);
4153 else if (pseudo_for_reload_consideration_p (regno))
4154 bitmap_set_bit (&c->dead_or_set, regno);
4157 if (regno < FIRST_PSEUDO_REGISTER
4158 || pseudo_for_reload_consideration_p (regno))
4160 if (GET_CODE (reg) == SUBREG
4161 && !DF_REF_FLAGS_IS_SET (use,
4162 DF_REF_SIGN_EXTRACT
4163 | DF_REF_ZERO_EXTRACT))
4165 unsigned int start = SUBREG_BYTE (reg);
4166 unsigned int last = start
4167 + GET_MODE_SIZE (GET_MODE (reg));
4169 init_live_subregs
4170 (bitmap_bit_p (live_relevant_regs, regno),
4171 live_subregs, live_subregs_used, regno, reg);
4173 /* Ignore the paradoxical bits. */
4174 if (last > SBITMAP_SIZE (live_subregs[regno]))
4175 last = SBITMAP_SIZE (live_subregs[regno]);
4177 while (start < last)
4179 bitmap_set_bit (live_subregs[regno], start);
4180 start++;
4183 else
4184 /* Resetting the live_subregs_used is
4185 effectively saying do not use the subregs
4186 because we are reading the whole
4187 pseudo. */
4188 bitmap_clear_bit (live_subregs_used, regno);
4189 bitmap_set_bit (live_relevant_regs, regno);
4195 /* FIXME!! The following code is a disaster. Reload needs to see the
4196 labels and jump tables that are just hanging out in between
4197 the basic blocks. See pr33676. */
4198 insn = BB_HEAD (bb);
4200 /* Skip over the barriers and cruft. */
4201 while (insn && (BARRIER_P (insn) || NOTE_P (insn)
4202 || BLOCK_FOR_INSN (insn) == bb))
4203 insn = PREV_INSN (insn);
4205 /* While we add anything except barriers and notes, the focus is
4206 to get the labels and jump tables into the
4207 reload_insn_chain. */
4208 while (insn)
4210 if (!NOTE_P (insn) && !BARRIER_P (insn))
4212 if (BLOCK_FOR_INSN (insn))
4213 break;
4215 c = new_insn_chain ();
4216 c->next = next;
4217 next = c;
4218 *p = c;
4219 p = &c->prev;
4221 /* The block makes no sense here, but it is what the old
4222 code did. */
4223 c->block = bb->index;
4224 c->insn = insn;
4225 bitmap_copy (&c->live_throughout, live_relevant_regs);
4227 insn = PREV_INSN (insn);
4231 reload_insn_chain = c;
4232 *p = NULL;
4234 for (i = 0; i < (unsigned int) max_regno; i++)
4235 if (live_subregs[i] != NULL)
4236 sbitmap_free (live_subregs[i]);
4237 free (live_subregs);
4238 BITMAP_FREE (live_subregs_used);
4239 BITMAP_FREE (live_relevant_regs);
4240 BITMAP_FREE (elim_regset);
4242 if (dump_file)
4243 print_insn_chains (dump_file);
4246 /* Examine the rtx found in *LOC, which is read or written to as determined
4247 by TYPE. Return false if we find a reason why an insn containing this
4248 rtx should not be moved (such as accesses to non-constant memory), true
4249 otherwise. */
4250 static bool
4251 rtx_moveable_p (rtx *loc, enum op_type type)
4253 const char *fmt;
4254 rtx x = *loc;
4255 enum rtx_code code = GET_CODE (x);
4256 int i, j;
4258 code = GET_CODE (x);
4259 switch (code)
4261 case CONST:
4262 CASE_CONST_ANY:
4263 case SYMBOL_REF:
4264 case LABEL_REF:
4265 return true;
4267 case PC:
4268 return type == OP_IN;
4270 case CC0:
4271 return false;
4273 case REG:
4274 if (x == frame_pointer_rtx)
4275 return true;
4276 if (HARD_REGISTER_P (x))
4277 return false;
4279 return true;
4281 case MEM:
4282 if (type == OP_IN && MEM_READONLY_P (x))
4283 return rtx_moveable_p (&XEXP (x, 0), OP_IN);
4284 return false;
4286 case SET:
4287 return (rtx_moveable_p (&SET_SRC (x), OP_IN)
4288 && rtx_moveable_p (&SET_DEST (x), OP_OUT));
4290 case STRICT_LOW_PART:
4291 return rtx_moveable_p (&XEXP (x, 0), OP_OUT);
4293 case ZERO_EXTRACT:
4294 case SIGN_EXTRACT:
4295 return (rtx_moveable_p (&XEXP (x, 0), type)
4296 && rtx_moveable_p (&XEXP (x, 1), OP_IN)
4297 && rtx_moveable_p (&XEXP (x, 2), OP_IN));
4299 case CLOBBER:
4300 return rtx_moveable_p (&SET_DEST (x), OP_OUT);
4302 default:
4303 break;
4306 fmt = GET_RTX_FORMAT (code);
4307 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4309 if (fmt[i] == 'e')
4311 if (!rtx_moveable_p (&XEXP (x, i), type))
4312 return false;
4314 else if (fmt[i] == 'E')
4315 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4317 if (!rtx_moveable_p (&XVECEXP (x, i, j), type))
4318 return false;
4321 return true;
4324 /* A wrapper around dominated_by_p, which uses the information in UID_LUID
4325 to give dominance relationships between two insns I1 and I2. */
4326 static bool
4327 insn_dominated_by_p (rtx i1, rtx i2, int *uid_luid)
4329 basic_block bb1 = BLOCK_FOR_INSN (i1);
4330 basic_block bb2 = BLOCK_FOR_INSN (i2);
4332 if (bb1 == bb2)
4333 return uid_luid[INSN_UID (i2)] < uid_luid[INSN_UID (i1)];
4334 return dominated_by_p (CDI_DOMINATORS, bb1, bb2);
4337 /* Record the range of register numbers added by find_moveable_pseudos. */
4338 int first_moveable_pseudo, last_moveable_pseudo;
4340 /* These two vectors hold data for every register added by
4341 find_movable_pseudos, with index 0 holding data for the
4342 first_moveable_pseudo. */
4343 /* The original home register. */
4344 static vec<rtx> pseudo_replaced_reg;
4346 /* Look for instances where we have an instruction that is known to increase
4347 register pressure, and whose result is not used immediately. If it is
4348 possible to move the instruction downwards to just before its first use,
4349 split its lifetime into two ranges. We create a new pseudo to compute the
4350 value, and emit a move instruction just before the first use. If, after
4351 register allocation, the new pseudo remains unallocated, the function
4352 move_unallocated_pseudos then deletes the move instruction and places
4353 the computation just before the first use.
4355 Such a move is safe and profitable if all the input registers remain live
4356 and unchanged between the original computation and its first use. In such
4357 a situation, the computation is known to increase register pressure, and
4358 moving it is known to at least not worsen it.
4360 We restrict moves to only those cases where a register remains unallocated,
4361 in order to avoid interfering too much with the instruction schedule. As
4362 an exception, we may move insns which only modify their input register
4363 (typically induction variables), as this increases the freedom for our
4364 intended transformation, and does not limit the second instruction
4365 scheduler pass. */
4367 static void
4368 find_moveable_pseudos (void)
4370 unsigned i;
4371 int max_regs = max_reg_num ();
4372 int max_uid = get_max_uid ();
4373 basic_block bb;
4374 int *uid_luid = XNEWVEC (int, max_uid);
4375 rtx_insn **closest_uses = XNEWVEC (rtx_insn *, max_regs);
4376 /* A set of registers which are live but not modified throughout a block. */
4377 bitmap_head *bb_transp_live = XNEWVEC (bitmap_head,
4378 last_basic_block_for_fn (cfun));
4379 /* A set of registers which only exist in a given basic block. */
4380 bitmap_head *bb_local = XNEWVEC (bitmap_head,
4381 last_basic_block_for_fn (cfun));
4382 /* A set of registers which are set once, in an instruction that can be
4383 moved freely downwards, but are otherwise transparent to a block. */
4384 bitmap_head *bb_moveable_reg_sets = XNEWVEC (bitmap_head,
4385 last_basic_block_for_fn (cfun));
4386 bitmap_head live, used, set, interesting, unusable_as_input;
4387 bitmap_iterator bi;
4388 bitmap_initialize (&interesting, 0);
4390 first_moveable_pseudo = max_regs;
4391 pseudo_replaced_reg.release ();
4392 pseudo_replaced_reg.safe_grow_cleared (max_regs);
4394 df_analyze ();
4395 calculate_dominance_info (CDI_DOMINATORS);
4397 i = 0;
4398 bitmap_initialize (&live, 0);
4399 bitmap_initialize (&used, 0);
4400 bitmap_initialize (&set, 0);
4401 bitmap_initialize (&unusable_as_input, 0);
4402 FOR_EACH_BB_FN (bb, cfun)
4404 rtx_insn *insn;
4405 bitmap transp = bb_transp_live + bb->index;
4406 bitmap moveable = bb_moveable_reg_sets + bb->index;
4407 bitmap local = bb_local + bb->index;
4409 bitmap_initialize (local, 0);
4410 bitmap_initialize (transp, 0);
4411 bitmap_initialize (moveable, 0);
4412 bitmap_copy (&live, df_get_live_out (bb));
4413 bitmap_and_into (&live, df_get_live_in (bb));
4414 bitmap_copy (transp, &live);
4415 bitmap_clear (moveable);
4416 bitmap_clear (&live);
4417 bitmap_clear (&used);
4418 bitmap_clear (&set);
4419 FOR_BB_INSNS (bb, insn)
4420 if (NONDEBUG_INSN_P (insn))
4422 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4423 df_ref def, use;
4425 uid_luid[INSN_UID (insn)] = i++;
4427 def = df_single_def (insn_info);
4428 use = df_single_use (insn_info);
4429 if (use
4430 && def
4431 && DF_REF_REGNO (use) == DF_REF_REGNO (def)
4432 && !bitmap_bit_p (&set, DF_REF_REGNO (use))
4433 && rtx_moveable_p (&PATTERN (insn), OP_IN))
4435 unsigned regno = DF_REF_REGNO (use);
4436 bitmap_set_bit (moveable, regno);
4437 bitmap_set_bit (&set, regno);
4438 bitmap_set_bit (&used, regno);
4439 bitmap_clear_bit (transp, regno);
4440 continue;
4442 FOR_EACH_INSN_INFO_USE (use, insn_info)
4444 unsigned regno = DF_REF_REGNO (use);
4445 bitmap_set_bit (&used, regno);
4446 if (bitmap_clear_bit (moveable, regno))
4447 bitmap_clear_bit (transp, regno);
4450 FOR_EACH_INSN_INFO_DEF (def, insn_info)
4452 unsigned regno = DF_REF_REGNO (def);
4453 bitmap_set_bit (&set, regno);
4454 bitmap_clear_bit (transp, regno);
4455 bitmap_clear_bit (moveable, regno);
4460 bitmap_clear (&live);
4461 bitmap_clear (&used);
4462 bitmap_clear (&set);
4464 FOR_EACH_BB_FN (bb, cfun)
4466 bitmap local = bb_local + bb->index;
4467 rtx_insn *insn;
4469 FOR_BB_INSNS (bb, insn)
4470 if (NONDEBUG_INSN_P (insn))
4472 df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4473 rtx_insn *def_insn;
4474 rtx closest_use, note;
4475 df_ref def, use;
4476 unsigned regno;
4477 bool all_dominated, all_local;
4478 enum machine_mode mode;
4480 def = df_single_def (insn_info);
4481 /* There must be exactly one def in this insn. */
4482 if (!def || !single_set (insn))
4483 continue;
4484 /* This must be the only definition of the reg. We also limit
4485 which modes we deal with so that we can assume we can generate
4486 move instructions. */
4487 regno = DF_REF_REGNO (def);
4488 mode = GET_MODE (DF_REF_REG (def));
4489 if (DF_REG_DEF_COUNT (regno) != 1
4490 || !DF_REF_INSN_INFO (def)
4491 || HARD_REGISTER_NUM_P (regno)
4492 || DF_REG_EQ_USE_COUNT (regno) > 0
4493 || (!INTEGRAL_MODE_P (mode) && !FLOAT_MODE_P (mode)))
4494 continue;
4495 def_insn = DF_REF_INSN (def);
4497 for (note = REG_NOTES (def_insn); note; note = XEXP (note, 1))
4498 if (REG_NOTE_KIND (note) == REG_EQUIV && MEM_P (XEXP (note, 0)))
4499 break;
4501 if (note)
4503 if (dump_file)
4504 fprintf (dump_file, "Ignoring reg %d, has equiv memory\n",
4505 regno);
4506 bitmap_set_bit (&unusable_as_input, regno);
4507 continue;
4510 use = DF_REG_USE_CHAIN (regno);
4511 all_dominated = true;
4512 all_local = true;
4513 closest_use = NULL_RTX;
4514 for (; use; use = DF_REF_NEXT_REG (use))
4516 rtx_insn *insn;
4517 if (!DF_REF_INSN_INFO (use))
4519 all_dominated = false;
4520 all_local = false;
4521 break;
4523 insn = DF_REF_INSN (use);
4524 if (DEBUG_INSN_P (insn))
4525 continue;
4526 if (BLOCK_FOR_INSN (insn) != BLOCK_FOR_INSN (def_insn))
4527 all_local = false;
4528 if (!insn_dominated_by_p (insn, def_insn, uid_luid))
4529 all_dominated = false;
4530 if (closest_use != insn && closest_use != const0_rtx)
4532 if (closest_use == NULL_RTX)
4533 closest_use = insn;
4534 else if (insn_dominated_by_p (closest_use, insn, uid_luid))
4535 closest_use = insn;
4536 else if (!insn_dominated_by_p (insn, closest_use, uid_luid))
4537 closest_use = const0_rtx;
4540 if (!all_dominated)
4542 if (dump_file)
4543 fprintf (dump_file, "Reg %d not all uses dominated by set\n",
4544 regno);
4545 continue;
4547 if (all_local)
4548 bitmap_set_bit (local, regno);
4549 if (closest_use == const0_rtx || closest_use == NULL
4550 || next_nonnote_nondebug_insn (def_insn) == closest_use)
4552 if (dump_file)
4553 fprintf (dump_file, "Reg %d uninteresting%s\n", regno,
4554 closest_use == const0_rtx || closest_use == NULL
4555 ? " (no unique first use)" : "");
4556 continue;
4558 #ifdef HAVE_cc0
4559 if (reg_referenced_p (cc0_rtx, PATTERN (closest_use)))
4561 if (dump_file)
4562 fprintf (dump_file, "Reg %d: closest user uses cc0\n",
4563 regno);
4564 continue;
4566 #endif
4567 bitmap_set_bit (&interesting, regno);
4568 /* If we get here, we know closest_use is a non-NULL insn
4569 (as opposed to const_0_rtx). */
4570 closest_uses[regno] = as_a <rtx_insn *> (closest_use);
4572 if (dump_file && (all_local || all_dominated))
4574 fprintf (dump_file, "Reg %u:", regno);
4575 if (all_local)
4576 fprintf (dump_file, " local to bb %d", bb->index);
4577 if (all_dominated)
4578 fprintf (dump_file, " def dominates all uses");
4579 if (closest_use != const0_rtx)
4580 fprintf (dump_file, " has unique first use");
4581 fputs ("\n", dump_file);
4586 EXECUTE_IF_SET_IN_BITMAP (&interesting, 0, i, bi)
4588 df_ref def = DF_REG_DEF_CHAIN (i);
4589 rtx_insn *def_insn = DF_REF_INSN (def);
4590 basic_block def_block = BLOCK_FOR_INSN (def_insn);
4591 bitmap def_bb_local = bb_local + def_block->index;
4592 bitmap def_bb_moveable = bb_moveable_reg_sets + def_block->index;
4593 bitmap def_bb_transp = bb_transp_live + def_block->index;
4594 bool local_to_bb_p = bitmap_bit_p (def_bb_local, i);
4595 rtx_insn *use_insn = closest_uses[i];
4596 df_ref use;
4597 bool all_ok = true;
4598 bool all_transp = true;
4600 if (!REG_P (DF_REF_REG (def)))
4601 continue;
4603 if (!local_to_bb_p)
4605 if (dump_file)
4606 fprintf (dump_file, "Reg %u not local to one basic block\n",
4608 continue;
4610 if (reg_equiv_init (i) != NULL_RTX)
4612 if (dump_file)
4613 fprintf (dump_file, "Ignoring reg %u with equiv init insn\n",
4615 continue;
4617 if (!rtx_moveable_p (&PATTERN (def_insn), OP_IN))
4619 if (dump_file)
4620 fprintf (dump_file, "Found def insn %d for %d to be not moveable\n",
4621 INSN_UID (def_insn), i);
4622 continue;
4624 if (dump_file)
4625 fprintf (dump_file, "Examining insn %d, def for %d\n",
4626 INSN_UID (def_insn), i);
4627 FOR_EACH_INSN_USE (use, def_insn)
4629 unsigned regno = DF_REF_REGNO (use);
4630 if (bitmap_bit_p (&unusable_as_input, regno))
4632 all_ok = false;
4633 if (dump_file)
4634 fprintf (dump_file, " found unusable input reg %u.\n", regno);
4635 break;
4637 if (!bitmap_bit_p (def_bb_transp, regno))
4639 if (bitmap_bit_p (def_bb_moveable, regno)
4640 && !control_flow_insn_p (use_insn)
4641 #ifdef HAVE_cc0
4642 && !sets_cc0_p (use_insn)
4643 #endif
4646 if (modified_between_p (DF_REF_REG (use), def_insn, use_insn))
4648 rtx_insn *x = NEXT_INSN (def_insn);
4649 while (!modified_in_p (DF_REF_REG (use), x))
4651 gcc_assert (x != use_insn);
4652 x = NEXT_INSN (x);
4654 if (dump_file)
4655 fprintf (dump_file, " input reg %u modified but insn %d moveable\n",
4656 regno, INSN_UID (x));
4657 emit_insn_after (PATTERN (x), use_insn);
4658 set_insn_deleted (x);
4660 else
4662 if (dump_file)
4663 fprintf (dump_file, " input reg %u modified between def and use\n",
4664 regno);
4665 all_transp = false;
4668 else
4669 all_transp = false;
4672 if (!all_ok)
4673 continue;
4674 if (!dbg_cnt (ira_move))
4675 break;
4676 if (dump_file)
4677 fprintf (dump_file, " all ok%s\n", all_transp ? " and transp" : "");
4679 if (all_transp)
4681 rtx def_reg = DF_REF_REG (def);
4682 rtx newreg = ira_create_new_reg (def_reg);
4683 if (validate_change (def_insn, DF_REF_REAL_LOC (def), newreg, 0))
4685 unsigned nregno = REGNO (newreg);
4686 emit_insn_before (gen_move_insn (def_reg, newreg), use_insn);
4687 nregno -= max_regs;
4688 pseudo_replaced_reg[nregno] = def_reg;
4693 FOR_EACH_BB_FN (bb, cfun)
4695 bitmap_clear (bb_local + bb->index);
4696 bitmap_clear (bb_transp_live + bb->index);
4697 bitmap_clear (bb_moveable_reg_sets + bb->index);
4699 bitmap_clear (&interesting);
4700 bitmap_clear (&unusable_as_input);
4701 free (uid_luid);
4702 free (closest_uses);
4703 free (bb_local);
4704 free (bb_transp_live);
4705 free (bb_moveable_reg_sets);
4707 last_moveable_pseudo = max_reg_num ();
4709 fix_reg_equiv_init ();
4710 expand_reg_info ();
4711 regstat_free_n_sets_and_refs ();
4712 regstat_free_ri ();
4713 regstat_init_n_sets_and_refs ();
4714 regstat_compute_ri ();
4715 free_dominance_info (CDI_DOMINATORS);
4718 /* If SET pattern SET is an assignment from a hard register to a pseudo which
4719 is live at CALL_DOM (if non-NULL, otherwise this check is omitted), return
4720 the destination. Otherwise return NULL. */
4722 static rtx
4723 interesting_dest_for_shprep_1 (rtx set, basic_block call_dom)
4725 rtx src = SET_SRC (set);
4726 rtx dest = SET_DEST (set);
4727 if (!REG_P (src) || !HARD_REGISTER_P (src)
4728 || !REG_P (dest) || HARD_REGISTER_P (dest)
4729 || (call_dom && !bitmap_bit_p (df_get_live_in (call_dom), REGNO (dest))))
4730 return NULL;
4731 return dest;
4734 /* If insn is interesting for parameter range-splitting shring-wrapping
4735 preparation, i.e. it is a single set from a hard register to a pseudo, which
4736 is live at CALL_DOM (if non-NULL, otherwise this check is omitted), or a
4737 parallel statement with only one such statement, return the destination.
4738 Otherwise return NULL. */
4740 static rtx
4741 interesting_dest_for_shprep (rtx_insn *insn, basic_block call_dom)
4743 if (!INSN_P (insn))
4744 return NULL;
4745 rtx pat = PATTERN (insn);
4746 if (GET_CODE (pat) == SET)
4747 return interesting_dest_for_shprep_1 (pat, call_dom);
4749 if (GET_CODE (pat) != PARALLEL)
4750 return NULL;
4751 rtx ret = NULL;
4752 for (int i = 0; i < XVECLEN (pat, 0); i++)
4754 rtx sub = XVECEXP (pat, 0, i);
4755 if (GET_CODE (sub) == USE || GET_CODE (sub) == CLOBBER)
4756 continue;
4757 if (GET_CODE (sub) != SET
4758 || side_effects_p (sub))
4759 return NULL;
4760 rtx dest = interesting_dest_for_shprep_1 (sub, call_dom);
4761 if (dest && ret)
4762 return NULL;
4763 if (dest)
4764 ret = dest;
4766 return ret;
4769 /* Split live ranges of pseudos that are loaded from hard registers in the
4770 first BB in a BB that dominates all non-sibling call if such a BB can be
4771 found and is not in a loop. Return true if the function has made any
4772 changes. */
4774 static bool
4775 split_live_ranges_for_shrink_wrap (void)
4777 basic_block bb, call_dom = NULL;
4778 basic_block first = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
4779 rtx_insn *insn, *last_interesting_insn = NULL;
4780 bitmap_head need_new, reachable;
4781 vec<basic_block> queue;
4783 if (!flag_shrink_wrap)
4784 return false;
4786 bitmap_initialize (&need_new, 0);
4787 bitmap_initialize (&reachable, 0);
4788 queue.create (n_basic_blocks_for_fn (cfun));
4790 FOR_EACH_BB_FN (bb, cfun)
4791 FOR_BB_INSNS (bb, insn)
4792 if (CALL_P (insn) && !SIBLING_CALL_P (insn))
4794 if (bb == first)
4796 bitmap_clear (&need_new);
4797 bitmap_clear (&reachable);
4798 queue.release ();
4799 return false;
4802 bitmap_set_bit (&need_new, bb->index);
4803 bitmap_set_bit (&reachable, bb->index);
4804 queue.quick_push (bb);
4805 break;
4808 if (queue.is_empty ())
4810 bitmap_clear (&need_new);
4811 bitmap_clear (&reachable);
4812 queue.release ();
4813 return false;
4816 while (!queue.is_empty ())
4818 edge e;
4819 edge_iterator ei;
4821 bb = queue.pop ();
4822 FOR_EACH_EDGE (e, ei, bb->succs)
4823 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
4824 && bitmap_set_bit (&reachable, e->dest->index))
4825 queue.quick_push (e->dest);
4827 queue.release ();
4829 FOR_BB_INSNS (first, insn)
4831 rtx dest = interesting_dest_for_shprep (insn, NULL);
4832 if (!dest)
4833 continue;
4835 if (DF_REG_DEF_COUNT (REGNO (dest)) > 1)
4837 bitmap_clear (&need_new);
4838 bitmap_clear (&reachable);
4839 return false;
4842 for (df_ref use = DF_REG_USE_CHAIN (REGNO(dest));
4843 use;
4844 use = DF_REF_NEXT_REG (use))
4846 int ubbi = DF_REF_BB (use)->index;
4847 if (bitmap_bit_p (&reachable, ubbi))
4848 bitmap_set_bit (&need_new, ubbi);
4850 last_interesting_insn = insn;
4853 bitmap_clear (&reachable);
4854 if (!last_interesting_insn)
4856 bitmap_clear (&need_new);
4857 return false;
4860 call_dom = nearest_common_dominator_for_set (CDI_DOMINATORS, &need_new);
4861 bitmap_clear (&need_new);
4862 if (call_dom == first)
4863 return false;
4865 loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
4866 while (bb_loop_depth (call_dom) > 0)
4867 call_dom = get_immediate_dominator (CDI_DOMINATORS, call_dom);
4868 loop_optimizer_finalize ();
4870 if (call_dom == first)
4871 return false;
4873 calculate_dominance_info (CDI_POST_DOMINATORS);
4874 if (dominated_by_p (CDI_POST_DOMINATORS, first, call_dom))
4876 free_dominance_info (CDI_POST_DOMINATORS);
4877 return false;
4879 free_dominance_info (CDI_POST_DOMINATORS);
4881 if (dump_file)
4882 fprintf (dump_file, "Will split live ranges of parameters at BB %i\n",
4883 call_dom->index);
4885 bool ret = false;
4886 FOR_BB_INSNS (first, insn)
4888 rtx dest = interesting_dest_for_shprep (insn, call_dom);
4889 if (!dest)
4890 continue;
4892 rtx newreg = NULL_RTX;
4893 df_ref use, next;
4894 for (use = DF_REG_USE_CHAIN (REGNO (dest)); use; use = next)
4896 rtx_insn *uin = DF_REF_INSN (use);
4897 next = DF_REF_NEXT_REG (use);
4899 basic_block ubb = BLOCK_FOR_INSN (uin);
4900 if (ubb == call_dom
4901 || dominated_by_p (CDI_DOMINATORS, ubb, call_dom))
4903 if (!newreg)
4904 newreg = ira_create_new_reg (dest);
4905 validate_change (uin, DF_REF_REAL_LOC (use), newreg, true);
4909 if (newreg)
4911 rtx new_move = gen_move_insn (newreg, dest);
4912 emit_insn_after (new_move, bb_note (call_dom));
4913 if (dump_file)
4915 fprintf (dump_file, "Split live-range of register ");
4916 print_rtl_single (dump_file, dest);
4918 ret = true;
4921 if (insn == last_interesting_insn)
4922 break;
4924 apply_change_group ();
4925 return ret;
4928 /* Perform the second half of the transformation started in
4929 find_moveable_pseudos. We look for instances where the newly introduced
4930 pseudo remains unallocated, and remove it by moving the definition to
4931 just before its use, replacing the move instruction generated by
4932 find_moveable_pseudos. */
4933 static void
4934 move_unallocated_pseudos (void)
4936 int i;
4937 for (i = first_moveable_pseudo; i < last_moveable_pseudo; i++)
4938 if (reg_renumber[i] < 0)
4940 int idx = i - first_moveable_pseudo;
4941 rtx other_reg = pseudo_replaced_reg[idx];
4942 rtx_insn *def_insn = DF_REF_INSN (DF_REG_DEF_CHAIN (i));
4943 /* The use must follow all definitions of OTHER_REG, so we can
4944 insert the new definition immediately after any of them. */
4945 df_ref other_def = DF_REG_DEF_CHAIN (REGNO (other_reg));
4946 rtx_insn *move_insn = DF_REF_INSN (other_def);
4947 rtx_insn *newinsn = emit_insn_after (PATTERN (def_insn), move_insn);
4948 rtx set;
4949 int success;
4951 if (dump_file)
4952 fprintf (dump_file, "moving def of %d (insn %d now) ",
4953 REGNO (other_reg), INSN_UID (def_insn));
4955 delete_insn (move_insn);
4956 while ((other_def = DF_REG_DEF_CHAIN (REGNO (other_reg))))
4957 delete_insn (DF_REF_INSN (other_def));
4958 delete_insn (def_insn);
4960 set = single_set (newinsn);
4961 success = validate_change (newinsn, &SET_DEST (set), other_reg, 0);
4962 gcc_assert (success);
4963 if (dump_file)
4964 fprintf (dump_file, " %d) rather than keep unallocated replacement %d\n",
4965 INSN_UID (newinsn), i);
4966 SET_REG_N_REFS (i, 0);
4970 /* If the backend knows where to allocate pseudos for hard
4971 register initial values, register these allocations now. */
4972 static void
4973 allocate_initial_values (void)
4975 if (targetm.allocate_initial_value)
4977 rtx hreg, preg, x;
4978 int i, regno;
4980 for (i = 0; HARD_REGISTER_NUM_P (i); i++)
4982 if (! initial_value_entry (i, &hreg, &preg))
4983 break;
4985 x = targetm.allocate_initial_value (hreg);
4986 regno = REGNO (preg);
4987 if (x && REG_N_SETS (regno) <= 1)
4989 if (MEM_P (x))
4990 reg_equiv_memory_loc (regno) = x;
4991 else
4993 basic_block bb;
4994 int new_regno;
4996 gcc_assert (REG_P (x));
4997 new_regno = REGNO (x);
4998 reg_renumber[regno] = new_regno;
4999 /* Poke the regno right into regno_reg_rtx so that even
5000 fixed regs are accepted. */
5001 SET_REGNO (preg, new_regno);
5002 /* Update global register liveness information. */
5003 FOR_EACH_BB_FN (bb, cfun)
5005 if (REGNO_REG_SET_P (df_get_live_in (bb), regno))
5006 SET_REGNO_REG_SET (df_get_live_in (bb), new_regno);
5007 if (REGNO_REG_SET_P (df_get_live_out (bb), regno))
5008 SET_REGNO_REG_SET (df_get_live_out (bb), new_regno);
5014 gcc_checking_assert (! initial_value_entry (FIRST_PSEUDO_REGISTER,
5015 &hreg, &preg));
5020 /* True when we use LRA instead of reload pass for the current
5021 function. */
5022 bool ira_use_lra_p;
5024 /* True if we have allocno conflicts. It is false for non-optimized
5025 mode or when the conflict table is too big. */
5026 bool ira_conflicts_p;
5028 /* Saved between IRA and reload. */
5029 static int saved_flag_ira_share_spill_slots;
5031 /* This is the main entry of IRA. */
5032 static void
5033 ira (FILE *f)
5035 bool loops_p;
5036 int ira_max_point_before_emit;
5037 int rebuild_p;
5038 bool saved_flag_caller_saves = flag_caller_saves;
5039 enum ira_region saved_flag_ira_region = flag_ira_region;
5041 ira_conflicts_p = optimize > 0;
5043 ira_use_lra_p = targetm.lra_p ();
5044 /* If there are too many pseudos and/or basic blocks (e.g. 10K
5045 pseudos and 10K blocks or 100K pseudos and 1K blocks), we will
5046 use simplified and faster algorithms in LRA. */
5047 lra_simple_p
5048 = (ira_use_lra_p
5049 && max_reg_num () >= (1 << 26) / last_basic_block_for_fn (cfun));
5050 if (lra_simple_p)
5052 /* It permits to skip live range splitting in LRA. */
5053 flag_caller_saves = false;
5054 /* There is no sense to do regional allocation when we use
5055 simplified LRA. */
5056 flag_ira_region = IRA_REGION_ONE;
5057 ira_conflicts_p = false;
5060 #ifndef IRA_NO_OBSTACK
5061 gcc_obstack_init (&ira_obstack);
5062 #endif
5063 bitmap_obstack_initialize (&ira_bitmap_obstack);
5065 /* LRA uses its own infrastructure to handle caller save registers. */
5066 if (flag_caller_saves && !ira_use_lra_p)
5067 init_caller_save ();
5069 if (flag_ira_verbose < 10)
5071 internal_flag_ira_verbose = flag_ira_verbose;
5072 ira_dump_file = f;
5074 else
5076 internal_flag_ira_verbose = flag_ira_verbose - 10;
5077 ira_dump_file = stderr;
5080 setup_prohibited_mode_move_regs ();
5081 decrease_live_ranges_number ();
5082 df_note_add_problem ();
5084 /* DF_LIVE can't be used in the register allocator, too many other
5085 parts of the compiler depend on using the "classic" liveness
5086 interpretation of the DF_LR problem. See PR38711.
5087 Remove the problem, so that we don't spend time updating it in
5088 any of the df_analyze() calls during IRA/LRA. */
5089 if (optimize > 1)
5090 df_remove_problem (df_live);
5091 gcc_checking_assert (df_live == NULL);
5093 #ifdef ENABLE_CHECKING
5094 df->changeable_flags |= DF_VERIFY_SCHEDULED;
5095 #endif
5096 df_analyze ();
5098 init_reg_equiv ();
5099 if (ira_conflicts_p)
5101 calculate_dominance_info (CDI_DOMINATORS);
5103 if (split_live_ranges_for_shrink_wrap ())
5104 df_analyze ();
5106 free_dominance_info (CDI_DOMINATORS);
5109 df_clear_flags (DF_NO_INSN_RESCAN);
5111 regstat_init_n_sets_and_refs ();
5112 regstat_compute_ri ();
5114 /* If we are not optimizing, then this is the only place before
5115 register allocation where dataflow is done. And that is needed
5116 to generate these warnings. */
5117 if (warn_clobbered)
5118 generate_setjmp_warnings ();
5120 /* Determine if the current function is a leaf before running IRA
5121 since this can impact optimizations done by the prologue and
5122 epilogue thus changing register elimination offsets. */
5123 crtl->is_leaf = leaf_function_p ();
5125 if (resize_reg_info () && flag_ira_loop_pressure)
5126 ira_set_pseudo_classes (true, ira_dump_file);
5128 rebuild_p = update_equiv_regs ();
5129 setup_reg_equiv ();
5130 setup_reg_equiv_init ();
5132 if (optimize && rebuild_p)
5134 timevar_push (TV_JUMP);
5135 rebuild_jump_labels (get_insns ());
5136 if (purge_all_dead_edges ())
5137 delete_unreachable_blocks ();
5138 timevar_pop (TV_JUMP);
5141 allocated_reg_info_size = max_reg_num ();
5143 if (delete_trivially_dead_insns (get_insns (), max_reg_num ()))
5144 df_analyze ();
5146 /* It is not worth to do such improvement when we use a simple
5147 allocation because of -O0 usage or because the function is too
5148 big. */
5149 if (ira_conflicts_p)
5150 find_moveable_pseudos ();
5152 max_regno_before_ira = max_reg_num ();
5153 ira_setup_eliminable_regset ();
5155 ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
5156 ira_load_cost = ira_store_cost = ira_shuffle_cost = 0;
5157 ira_move_loops_num = ira_additional_jumps_num = 0;
5159 ira_assert (current_loops == NULL);
5160 if (flag_ira_region == IRA_REGION_ALL || flag_ira_region == IRA_REGION_MIXED)
5161 loop_optimizer_init (AVOID_CFG_MODIFICATIONS | LOOPS_HAVE_RECORDED_EXITS);
5163 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
5164 fprintf (ira_dump_file, "Building IRA IR\n");
5165 loops_p = ira_build ();
5167 ira_assert (ira_conflicts_p || !loops_p);
5169 saved_flag_ira_share_spill_slots = flag_ira_share_spill_slots;
5170 if (too_high_register_pressure_p () || cfun->calls_setjmp)
5171 /* It is just wasting compiler's time to pack spilled pseudos into
5172 stack slots in this case -- prohibit it. We also do this if
5173 there is setjmp call because a variable not modified between
5174 setjmp and longjmp the compiler is required to preserve its
5175 value and sharing slots does not guarantee it. */
5176 flag_ira_share_spill_slots = FALSE;
5178 ira_color ();
5180 ira_max_point_before_emit = ira_max_point;
5182 ira_initiate_emit_data ();
5184 ira_emit (loops_p);
5186 max_regno = max_reg_num ();
5187 if (ira_conflicts_p)
5189 if (! loops_p)
5191 if (! ira_use_lra_p)
5192 ira_initiate_assign ();
5194 else
5196 expand_reg_info ();
5198 if (ira_use_lra_p)
5200 ira_allocno_t a;
5201 ira_allocno_iterator ai;
5203 FOR_EACH_ALLOCNO (a, ai)
5204 ALLOCNO_REGNO (a) = REGNO (ALLOCNO_EMIT_DATA (a)->reg);
5206 else
5208 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
5209 fprintf (ira_dump_file, "Flattening IR\n");
5210 ira_flattening (max_regno_before_ira, ira_max_point_before_emit);
5212 /* New insns were generated: add notes and recalculate live
5213 info. */
5214 df_analyze ();
5216 /* ??? Rebuild the loop tree, but why? Does the loop tree
5217 change if new insns were generated? Can that be handled
5218 by updating the loop tree incrementally? */
5219 loop_optimizer_finalize ();
5220 free_dominance_info (CDI_DOMINATORS);
5221 loop_optimizer_init (AVOID_CFG_MODIFICATIONS
5222 | LOOPS_HAVE_RECORDED_EXITS);
5224 if (! ira_use_lra_p)
5226 setup_allocno_assignment_flags ();
5227 ira_initiate_assign ();
5228 ira_reassign_conflict_allocnos (max_regno);
5233 ira_finish_emit_data ();
5235 setup_reg_renumber ();
5237 calculate_allocation_cost ();
5239 #ifdef ENABLE_IRA_CHECKING
5240 if (ira_conflicts_p)
5241 check_allocation ();
5242 #endif
5244 if (max_regno != max_regno_before_ira)
5246 regstat_free_n_sets_and_refs ();
5247 regstat_free_ri ();
5248 regstat_init_n_sets_and_refs ();
5249 regstat_compute_ri ();
5252 overall_cost_before = ira_overall_cost;
5253 if (! ira_conflicts_p)
5254 grow_reg_equivs ();
5255 else
5257 fix_reg_equiv_init ();
5259 #ifdef ENABLE_IRA_CHECKING
5260 print_redundant_copies ();
5261 #endif
5263 ira_spilled_reg_stack_slots_num = 0;
5264 ira_spilled_reg_stack_slots
5265 = ((struct ira_spilled_reg_stack_slot *)
5266 ira_allocate (max_regno
5267 * sizeof (struct ira_spilled_reg_stack_slot)));
5268 memset (ira_spilled_reg_stack_slots, 0,
5269 max_regno * sizeof (struct ira_spilled_reg_stack_slot));
5271 allocate_initial_values ();
5273 /* See comment for find_moveable_pseudos call. */
5274 if (ira_conflicts_p)
5275 move_unallocated_pseudos ();
5277 /* Restore original values. */
5278 if (lra_simple_p)
5280 flag_caller_saves = saved_flag_caller_saves;
5281 flag_ira_region = saved_flag_ira_region;
5285 static void
5286 do_reload (void)
5288 basic_block bb;
5289 bool need_dce;
5291 if (flag_ira_verbose < 10)
5292 ira_dump_file = dump_file;
5294 timevar_push (TV_RELOAD);
5295 if (ira_use_lra_p)
5297 if (current_loops != NULL)
5299 loop_optimizer_finalize ();
5300 free_dominance_info (CDI_DOMINATORS);
5302 FOR_ALL_BB_FN (bb, cfun)
5303 bb->loop_father = NULL;
5304 current_loops = NULL;
5306 if (ira_conflicts_p)
5307 ira_free (ira_spilled_reg_stack_slots);
5309 ira_destroy ();
5311 lra (ira_dump_file);
5312 /* ???!!! Move it before lra () when we use ira_reg_equiv in
5313 LRA. */
5314 vec_free (reg_equivs);
5315 reg_equivs = NULL;
5316 need_dce = false;
5318 else
5320 df_set_flags (DF_NO_INSN_RESCAN);
5321 build_insn_chain ();
5323 need_dce = reload (get_insns (), ira_conflicts_p);
5327 timevar_pop (TV_RELOAD);
5329 timevar_push (TV_IRA);
5331 if (ira_conflicts_p && ! ira_use_lra_p)
5333 ira_free (ira_spilled_reg_stack_slots);
5334 ira_finish_assign ();
5337 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL
5338 && overall_cost_before != ira_overall_cost)
5339 fprintf (ira_dump_file, "+++Overall after reload %d\n", ira_overall_cost);
5341 flag_ira_share_spill_slots = saved_flag_ira_share_spill_slots;
5343 if (! ira_use_lra_p)
5345 ira_destroy ();
5346 if (current_loops != NULL)
5348 loop_optimizer_finalize ();
5349 free_dominance_info (CDI_DOMINATORS);
5351 FOR_ALL_BB_FN (bb, cfun)
5352 bb->loop_father = NULL;
5353 current_loops = NULL;
5355 regstat_free_ri ();
5356 regstat_free_n_sets_and_refs ();
5359 if (optimize)
5360 cleanup_cfg (CLEANUP_EXPENSIVE);
5362 finish_reg_equiv ();
5364 bitmap_obstack_release (&ira_bitmap_obstack);
5365 #ifndef IRA_NO_OBSTACK
5366 obstack_free (&ira_obstack, NULL);
5367 #endif
5369 /* The code after the reload has changed so much that at this point
5370 we might as well just rescan everything. Note that
5371 df_rescan_all_insns is not going to help here because it does not
5372 touch the artificial uses and defs. */
5373 df_finish_pass (true);
5374 df_scan_alloc (NULL);
5375 df_scan_blocks ();
5377 if (optimize > 1)
5379 df_live_add_problem ();
5380 df_live_set_all_dirty ();
5383 if (optimize)
5384 df_analyze ();
5386 if (need_dce && optimize)
5387 run_fast_dce ();
5389 /* Diagnose uses of the hard frame pointer when it is used as a global
5390 register. Often we can get away with letting the user appropriate
5391 the frame pointer, but we should let them know when code generation
5392 makes that impossible. */
5393 if (global_regs[HARD_FRAME_POINTER_REGNUM] && frame_pointer_needed)
5395 tree decl = global_regs_decl[HARD_FRAME_POINTER_REGNUM];
5396 error_at (DECL_SOURCE_LOCATION (current_function_decl),
5397 "frame pointer required, but reserved");
5398 inform (DECL_SOURCE_LOCATION (decl), "for %qD", decl);
5401 timevar_pop (TV_IRA);
5404 /* Run the integrated register allocator. */
5406 namespace {
5408 const pass_data pass_data_ira =
5410 RTL_PASS, /* type */
5411 "ira", /* name */
5412 OPTGROUP_NONE, /* optinfo_flags */
5413 TV_IRA, /* tv_id */
5414 0, /* properties_required */
5415 0, /* properties_provided */
5416 0, /* properties_destroyed */
5417 0, /* todo_flags_start */
5418 TODO_do_not_ggc_collect, /* todo_flags_finish */
5421 class pass_ira : public rtl_opt_pass
5423 public:
5424 pass_ira (gcc::context *ctxt)
5425 : rtl_opt_pass (pass_data_ira, ctxt)
5428 /* opt_pass methods: */
5429 virtual unsigned int execute (function *)
5431 ira (dump_file);
5432 return 0;
5435 }; // class pass_ira
5437 } // anon namespace
5439 rtl_opt_pass *
5440 make_pass_ira (gcc::context *ctxt)
5442 return new pass_ira (ctxt);
5445 namespace {
5447 const pass_data pass_data_reload =
5449 RTL_PASS, /* type */
5450 "reload", /* name */
5451 OPTGROUP_NONE, /* optinfo_flags */
5452 TV_RELOAD, /* tv_id */
5453 0, /* properties_required */
5454 0, /* properties_provided */
5455 0, /* properties_destroyed */
5456 0, /* todo_flags_start */
5457 0, /* todo_flags_finish */
5460 class pass_reload : public rtl_opt_pass
5462 public:
5463 pass_reload (gcc::context *ctxt)
5464 : rtl_opt_pass (pass_data_reload, ctxt)
5467 /* opt_pass methods: */
5468 virtual unsigned int execute (function *)
5470 do_reload ();
5471 return 0;
5474 }; // class pass_reload
5476 } // anon namespace
5478 rtl_opt_pass *
5479 make_pass_reload (gcc::context *ctxt)
5481 return new pass_reload (ctxt);