2011-08-19 Andrew Stubbs <ams@codesourcery.com>
[official-gcc.git] / gcc / ira.c
blobe4be8b52f714d9d7dc9b177a0a9296114933dae6
1 /* Integrated Register Allocator (IRA) entry point.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* The integrated register allocator (IRA) is a
23 regional register allocator performing graph coloring on a top-down
24 traversal of nested regions. Graph coloring in a region is based
25 on Chaitin-Briggs algorithm. It is called integrated because
26 register coalescing, register live range splitting, and choosing a
27 better hard register are done on-the-fly during coloring. Register
28 coalescing and choosing a cheaper hard register is done by hard
29 register preferencing during hard register assigning. The live
30 range splitting is a byproduct of the regional register allocation.
32 Major IRA notions are:
34 o *Region* is a part of CFG where graph coloring based on
35 Chaitin-Briggs algorithm is done. IRA can work on any set of
36 nested CFG regions forming a tree. Currently the regions are
37 the entire function for the root region and natural loops for
38 the other regions. Therefore data structure representing a
39 region is called loop_tree_node.
41 o *Allocno class* is a register class used for allocation of
42 given allocno. It means that only hard register of given
43 register class can be assigned to given allocno. In reality,
44 even smaller subset of (*profitable*) hard registers can be
45 assigned. In rare cases, the subset can be even smaller
46 because our modification of Chaitin-Briggs algorithm requires
47 that sets of hard registers can be assigned to allocnos forms a
48 forest, i.e. the sets can be ordered in a way where any
49 previous set is not intersected with given set or is a superset
50 of given set.
52 o *Pressure class* is a register class belonging to a set of
53 register classes containing all of the hard-registers available
54 for register allocation. The set of all pressure classes for a
55 target is defined in the corresponding machine-description file
56 according some criteria. Register pressure is calculated only
57 for pressure classes and it affects some IRA decisions as
58 forming allocation regions.
60 o *Allocno* represents the live range of a pseudo-register in a
61 region. Besides the obvious attributes like the corresponding
62 pseudo-register number, allocno class, conflicting allocnos and
63 conflicting hard-registers, there are a few allocno attributes
64 which are important for understanding the allocation algorithm:
66 - *Live ranges*. This is a list of ranges of *program points*
67 where the allocno lives. Program points represent places
68 where a pseudo can be born or become dead (there are
69 approximately two times more program points than the insns)
70 and they are represented by integers starting with 0. The
71 live ranges are used to find conflicts between allocnos.
72 They also play very important role for the transformation of
73 the IRA internal representation of several regions into a one
74 region representation. The later is used during the reload
75 pass work because each allocno represents all of the
76 corresponding pseudo-registers.
78 - *Hard-register costs*. This is a vector of size equal to the
79 number of available hard-registers of the allocno class. The
80 cost of a callee-clobbered hard-register for an allocno is
81 increased by the cost of save/restore code around the calls
82 through the given allocno's life. If the allocno is a move
83 instruction operand and another operand is a hard-register of
84 the allocno class, the cost of the hard-register is decreased
85 by the move cost.
87 When an allocno is assigned, the hard-register with minimal
88 full cost is used. Initially, a hard-register's full cost is
89 the corresponding value from the hard-register's cost vector.
90 If the allocno is connected by a *copy* (see below) to
91 another allocno which has just received a hard-register, the
92 cost of the hard-register is decreased. Before choosing a
93 hard-register for an allocno, the allocno's current costs of
94 the hard-registers are modified by the conflict hard-register
95 costs of all of the conflicting allocnos which are not
96 assigned yet.
98 - *Conflict hard-register costs*. This is a vector of the same
99 size as the hard-register costs vector. To permit an
100 unassigned allocno to get a better hard-register, IRA uses
101 this vector to calculate the final full cost of the
102 available hard-registers. Conflict hard-register costs of an
103 unassigned allocno are also changed with a change of the
104 hard-register cost of the allocno when a copy involving the
105 allocno is processed as described above. This is done to
106 show other unassigned allocnos that a given allocno prefers
107 some hard-registers in order to remove the move instruction
108 corresponding to the copy.
110 o *Cap*. If a pseudo-register does not live in a region but
111 lives in a nested region, IRA creates a special allocno called
112 a cap in the outer region. A region cap is also created for a
113 subregion cap.
115 o *Copy*. Allocnos can be connected by copies. Copies are used
116 to modify hard-register costs for allocnos during coloring.
117 Such modifications reflects a preference to use the same
118 hard-register for the allocnos connected by copies. Usually
119 copies are created for move insns (in this case it results in
120 register coalescing). But IRA also creates copies for operands
121 of an insn which should be assigned to the same hard-register
122 due to constraints in the machine description (it usually
123 results in removing a move generated in reload to satisfy
124 the constraints) and copies referring to the allocno which is
125 the output operand of an instruction and the allocno which is
126 an input operand dying in the instruction (creation of such
127 copies results in less register shuffling). IRA *does not*
128 create copies between the same register allocnos from different
129 regions because we use another technique for propagating
130 hard-register preference on the borders of regions.
132 Allocnos (including caps) for the upper region in the region tree
133 *accumulate* information important for coloring from allocnos with
134 the same pseudo-register from nested regions. This includes
135 hard-register and memory costs, conflicts with hard-registers,
136 allocno conflicts, allocno copies and more. *Thus, attributes for
137 allocnos in a region have the same values as if the region had no
138 subregions*. It means that attributes for allocnos in the
139 outermost region corresponding to the function have the same values
140 as though the allocation used only one region which is the entire
141 function. It also means that we can look at IRA work as if the
142 first IRA did allocation for all function then it improved the
143 allocation for loops then their subloops and so on.
145 IRA major passes are:
147 o Building IRA internal representation which consists of the
148 following subpasses:
150 * First, IRA builds regions and creates allocnos (file
151 ira-build.c) and initializes most of their attributes.
153 * Then IRA finds an allocno class for each allocno and
154 calculates its initial (non-accumulated) cost of memory and
155 each hard-register of its allocno class (file ira-cost.c).
157 * IRA creates live ranges of each allocno, calulates register
158 pressure for each pressure class in each region, sets up
159 conflict hard registers for each allocno and info about calls
160 the allocno lives through (file ira-lives.c).
162 * IRA removes low register pressure loops from the regions
163 mostly to speed IRA up (file ira-build.c).
165 * IRA propagates accumulated allocno info from lower region
166 allocnos to corresponding upper region allocnos (file
167 ira-build.c).
169 * IRA creates all caps (file ira-build.c).
171 * Having live-ranges of allocnos and their classes, IRA creates
172 conflicting allocnos for each allocno. Conflicting allocnos
173 are stored as a bit vector or array of pointers to the
174 conflicting allocnos whatever is more profitable (file
175 ira-conflicts.c). At this point IRA creates allocno copies.
177 o Coloring. Now IRA has all necessary info to start graph coloring
178 process. It is done in each region on top-down traverse of the
179 region tree (file ira-color.c). There are following subpasses:
181 * Finding profitable hard registers of corresponding allocno
182 class for each allocno. For example, only callee-saved hard
183 registers are frequently profitable for allocnos living
184 through colors. If the profitable hard register set of
185 allocno does not form a tree based on subset relation, we use
186 some approximation to form the tree. This approximation is
187 used to figure out trivial colorability of allocnos. The
188 approximation is a pretty rare case.
190 * Putting allocnos onto the coloring stack. IRA uses Briggs
191 optimistic coloring which is a major improvement over
192 Chaitin's coloring. Therefore IRA does not spill allocnos at
193 this point. There is some freedom in the order of putting
194 allocnos on the stack which can affect the final result of
195 the allocation. IRA uses some heuristics to improve the
196 order.
198 We also use a modification of Chaitin-Briggs algorithm which
199 works for intersected register classes of allocnos. To
200 figure out trivial colorability of allocnos, the mentioned
201 above tree of hard register sets is used. To get an idea how
202 the algorithm works in i386 example, let us consider an
203 allocno to which any general hard register can be assigned.
204 If the allocno conflicts with eight allocnos to which only
205 EAX register can be assigned, given allocno is still
206 trivially colorable because all conflicting allocnos might be
207 assigned only to EAX and all other general hard registers are
208 still free.
210 To get an idea of the used trivial colorability criterion, it
211 is also useful to read article "Graph-Coloring Register
212 Allocation for Irregular Architectures" by Michael D. Smith
213 and Glen Holloway. Major difference between the article
214 approach and approach used in IRA is that Smith's approach
215 takes register classes only from machine description and IRA
216 calculate register classes from intermediate code too
217 (e.g. an explicit usage of hard registers in RTL code for
218 parameter passing can result in creation of additional
219 register classes which contain or exclude the hard
220 registers). That makes IRA approach useful for improving
221 coloring even for architectures with regular register files
222 and in fact some benchmarking shows the improvement for
223 regular class architectures is even bigger than for irregular
224 ones. Another difference is that Smith's approach chooses
225 intersection of classes of all insn operands in which a given
226 pseudo occurs. IRA can use bigger classes if it is still
227 more profitable than memory usage.
229 * Popping the allocnos from the stack and assigning them hard
230 registers. If IRA can not assign a hard register to an
231 allocno and the allocno is coalesced, IRA undoes the
232 coalescing and puts the uncoalesced allocnos onto the stack in
233 the hope that some such allocnos will get a hard register
234 separately. If IRA fails to assign hard register or memory
235 is more profitable for it, IRA spills the allocno. IRA
236 assigns the allocno the hard-register with minimal full
237 allocation cost which reflects the cost of usage of the
238 hard-register for the allocno and cost of usage of the
239 hard-register for allocnos conflicting with given allocno.
241 * Chaitin-Briggs coloring assigns as many pseudos as possible
242 to hard registers. After coloringh we try to improve
243 allocation with cost point of view. We improve the
244 allocation by spilling some allocnos and assigning the freed
245 hard registers to other allocnos if it decreases the overall
246 allocation cost.
248 * After allono assigning in the region, IRA modifies the hard
249 register and memory costs for the corresponding allocnos in
250 the subregions to reflect the cost of possible loads, stores,
251 or moves on the border of the region and its subregions.
252 When default regional allocation algorithm is used
253 (-fira-algorithm=mixed), IRA just propagates the assignment
254 for allocnos if the register pressure in the region for the
255 corresponding pressure class is less than number of available
256 hard registers for given pressure class.
258 o Spill/restore code moving. When IRA performs an allocation
259 by traversing regions in top-down order, it does not know what
260 happens below in the region tree. Therefore, sometimes IRA
261 misses opportunities to perform a better allocation. A simple
262 optimization tries to improve allocation in a region having
263 subregions and containing in another region. If the
264 corresponding allocnos in the subregion are spilled, it spills
265 the region allocno if it is profitable. The optimization
266 implements a simple iterative algorithm performing profitable
267 transformations while they are still possible. It is fast in
268 practice, so there is no real need for a better time complexity
269 algorithm.
271 o Code change. After coloring, two allocnos representing the
272 same pseudo-register outside and inside a region respectively
273 may be assigned to different locations (hard-registers or
274 memory). In this case IRA creates and uses a new
275 pseudo-register inside the region and adds code to move allocno
276 values on the region's borders. This is done during top-down
277 traversal of the regions (file ira-emit.c). In some
278 complicated cases IRA can create a new allocno to move allocno
279 values (e.g. when a swap of values stored in two hard-registers
280 is needed). At this stage, the new allocno is marked as
281 spilled. IRA still creates the pseudo-register and the moves
282 on the region borders even when both allocnos were assigned to
283 the same hard-register. If the reload pass spills a
284 pseudo-register for some reason, the effect will be smaller
285 because another allocno will still be in the hard-register. In
286 most cases, this is better then spilling both allocnos. If
287 reload does not change the allocation for the two
288 pseudo-registers, the trivial move will be removed by
289 post-reload optimizations. IRA does not generate moves for
290 allocnos assigned to the same hard register when the default
291 regional allocation algorithm is used and the register pressure
292 in the region for the corresponding pressure class is less than
293 number of available hard registers for given pressure class.
294 IRA also does some optimizations to remove redundant stores and
295 to reduce code duplication on the region borders.
297 o Flattening internal representation. After changing code, IRA
298 transforms its internal representation for several regions into
299 one region representation (file ira-build.c). This process is
300 called IR flattening. Such process is more complicated than IR
301 rebuilding would be, but is much faster.
303 o After IR flattening, IRA tries to assign hard registers to all
304 spilled allocnos. This is impelemented by a simple and fast
305 priority coloring algorithm (see function
306 ira_reassign_conflict_allocnos::ira-color.c). Here new allocnos
307 created during the code change pass can be assigned to hard
308 registers.
310 o At the end IRA calls the reload pass. The reload pass
311 communicates with IRA through several functions in file
312 ira-color.c to improve its decisions in
314 * sharing stack slots for the spilled pseudos based on IRA info
315 about pseudo-register conflicts.
317 * reassigning hard-registers to all spilled pseudos at the end
318 of each reload iteration.
320 * choosing a better hard-register to spill based on IRA info
321 about pseudo-register live ranges and the register pressure
322 in places where the pseudo-register lives.
324 IRA uses a lot of data representing the target processors. These
325 data are initilized in file ira.c.
327 If function has no loops (or the loops are ignored when
328 -fira-algorithm=CB is used), we have classic Chaitin-Briggs
329 coloring (only instead of separate pass of coalescing, we use hard
330 register preferencing). In such case, IRA works much faster
331 because many things are not made (like IR flattening, the
332 spill/restore optimization, and the code change).
334 Literature is worth to read for better understanding the code:
336 o Preston Briggs, Keith D. Cooper, Linda Torczon. Improvements to
337 Graph Coloring Register Allocation.
339 o David Callahan, Brian Koblenz. Register allocation via
340 hierarchical graph coloring.
342 o Keith Cooper, Anshuman Dasgupta, Jason Eckhardt. Revisiting Graph
343 Coloring Register Allocation: A Study of the Chaitin-Briggs and
344 Callahan-Koblenz Algorithms.
346 o Guei-Yuan Lueh, Thomas Gross, and Ali-Reza Adl-Tabatabai. Global
347 Register Allocation Based on Graph Fusion.
349 o Michael D. Smith and Glenn Holloway. Graph-Coloring Register
350 Allocation for Irregular Architectures
352 o Vladimir Makarov. The Integrated Register Allocator for GCC.
354 o Vladimir Makarov. The top-down register allocator for irregular
355 register file architectures.
360 #include "config.h"
361 #include "system.h"
362 #include "coretypes.h"
363 #include "tm.h"
364 #include "regs.h"
365 #include "rtl.h"
366 #include "tm_p.h"
367 #include "target.h"
368 #include "flags.h"
369 #include "obstack.h"
370 #include "bitmap.h"
371 #include "hard-reg-set.h"
372 #include "basic-block.h"
373 #include "df.h"
374 #include "expr.h"
375 #include "recog.h"
376 #include "params.h"
377 #include "timevar.h"
378 #include "tree-pass.h"
379 #include "output.h"
380 #include "except.h"
381 #include "reload.h"
382 #include "diagnostic-core.h"
383 #include "integrate.h"
384 #include "ggc.h"
385 #include "ira-int.h"
386 #include "dce.h"
389 struct target_ira default_target_ira;
390 struct target_ira_int default_target_ira_int;
391 #if SWITCHABLE_TARGET
392 struct target_ira *this_target_ira = &default_target_ira;
393 struct target_ira_int *this_target_ira_int = &default_target_ira_int;
394 #endif
396 /* A modified value of flag `-fira-verbose' used internally. */
397 int internal_flag_ira_verbose;
399 /* Dump file of the allocator if it is not NULL. */
400 FILE *ira_dump_file;
402 /* The number of elements in the following array. */
403 int ira_spilled_reg_stack_slots_num;
405 /* The following array contains info about spilled pseudo-registers
406 stack slots used in current function so far. */
407 struct ira_spilled_reg_stack_slot *ira_spilled_reg_stack_slots;
409 /* Correspondingly overall cost of the allocation, cost of the
410 allocnos assigned to hard-registers, cost of the allocnos assigned
411 to memory, cost of loads, stores and register move insns generated
412 for pseudo-register live range splitting (see ira-emit.c). */
413 int ira_overall_cost;
414 int ira_reg_cost, ira_mem_cost;
415 int ira_load_cost, ira_store_cost, ira_shuffle_cost;
416 int ira_move_loops_num, ira_additional_jumps_num;
418 /* All registers that can be eliminated. */
420 HARD_REG_SET eliminable_regset;
422 /* Temporary hard reg set used for a different calculation. */
423 static HARD_REG_SET temp_hard_regset;
427 /* The function sets up the map IRA_REG_MODE_HARD_REGSET. */
428 static void
429 setup_reg_mode_hard_regset (void)
431 int i, m, hard_regno;
433 for (m = 0; m < NUM_MACHINE_MODES; m++)
434 for (hard_regno = 0; hard_regno < FIRST_PSEUDO_REGISTER; hard_regno++)
436 CLEAR_HARD_REG_SET (ira_reg_mode_hard_regset[hard_regno][m]);
437 for (i = hard_regno_nregs[hard_regno][m] - 1; i >= 0; i--)
438 if (hard_regno + i < FIRST_PSEUDO_REGISTER)
439 SET_HARD_REG_BIT (ira_reg_mode_hard_regset[hard_regno][m],
440 hard_regno + i);
445 #define no_unit_alloc_regs \
446 (this_target_ira_int->x_no_unit_alloc_regs)
448 /* The function sets up the three arrays declared above. */
449 static void
450 setup_class_hard_regs (void)
452 int cl, i, hard_regno, n;
453 HARD_REG_SET processed_hard_reg_set;
455 ira_assert (SHRT_MAX >= FIRST_PSEUDO_REGISTER);
456 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
458 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
459 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
460 CLEAR_HARD_REG_SET (processed_hard_reg_set);
461 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
463 ira_non_ordered_class_hard_regs[cl][i] = -1;
464 ira_class_hard_reg_index[cl][i] = -1;
466 for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
468 #ifdef REG_ALLOC_ORDER
469 hard_regno = reg_alloc_order[i];
470 #else
471 hard_regno = i;
472 #endif
473 if (TEST_HARD_REG_BIT (processed_hard_reg_set, hard_regno))
474 continue;
475 SET_HARD_REG_BIT (processed_hard_reg_set, hard_regno);
476 if (! TEST_HARD_REG_BIT (temp_hard_regset, hard_regno))
477 ira_class_hard_reg_index[cl][hard_regno] = -1;
478 else
480 ira_class_hard_reg_index[cl][hard_regno] = n;
481 ira_class_hard_regs[cl][n++] = hard_regno;
484 ira_class_hard_regs_num[cl] = n;
485 for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
486 if (TEST_HARD_REG_BIT (temp_hard_regset, i))
487 ira_non_ordered_class_hard_regs[cl][n++] = i;
488 ira_assert (ira_class_hard_regs_num[cl] == n);
492 /* Set up IRA_AVAILABLE_CLASS_REGS. */
493 static void
494 setup_available_class_regs (void)
496 int i, j;
498 memset (ira_available_class_regs, 0, sizeof (ira_available_class_regs));
499 for (i = 0; i < N_REG_CLASSES; i++)
501 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
502 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
503 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
504 if (TEST_HARD_REG_BIT (temp_hard_regset, j))
505 ira_available_class_regs[i]++;
509 /* Set up global variables defining info about hard registers for the
510 allocation. These depend on USE_HARD_FRAME_P whose TRUE value means
511 that we can use the hard frame pointer for the allocation. */
512 static void
513 setup_alloc_regs (bool use_hard_frame_p)
515 #ifdef ADJUST_REG_ALLOC_ORDER
516 ADJUST_REG_ALLOC_ORDER;
517 #endif
518 COPY_HARD_REG_SET (no_unit_alloc_regs, fixed_reg_set);
519 if (! use_hard_frame_p)
520 SET_HARD_REG_BIT (no_unit_alloc_regs, HARD_FRAME_POINTER_REGNUM);
521 setup_class_hard_regs ();
522 setup_available_class_regs ();
527 #define alloc_reg_class_subclasses \
528 (this_target_ira_int->x_alloc_reg_class_subclasses)
530 /* Initialize the table of subclasses of each reg class. */
531 static void
532 setup_reg_subclasses (void)
534 int i, j;
535 HARD_REG_SET temp_hard_regset2;
537 for (i = 0; i < N_REG_CLASSES; i++)
538 for (j = 0; j < N_REG_CLASSES; j++)
539 alloc_reg_class_subclasses[i][j] = LIM_REG_CLASSES;
541 for (i = 0; i < N_REG_CLASSES; i++)
543 if (i == (int) NO_REGS)
544 continue;
546 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
547 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
548 if (hard_reg_set_empty_p (temp_hard_regset))
549 continue;
550 for (j = 0; j < N_REG_CLASSES; j++)
551 if (i != j)
553 enum reg_class *p;
555 COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[j]);
556 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
557 if (! hard_reg_set_subset_p (temp_hard_regset,
558 temp_hard_regset2))
559 continue;
560 p = &alloc_reg_class_subclasses[j][0];
561 while (*p != LIM_REG_CLASSES) p++;
562 *p = (enum reg_class) i;
569 /* Set up IRA_MEMORY_MOVE_COST and IRA_MAX_MEMORY_MOVE_COST. */
570 static void
571 setup_class_subset_and_memory_move_costs (void)
573 int cl, cl2, mode, cost;
574 HARD_REG_SET temp_hard_regset2;
576 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
577 ira_memory_move_cost[mode][NO_REGS][0]
578 = ira_memory_move_cost[mode][NO_REGS][1] = SHRT_MAX;
579 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
581 if (cl != (int) NO_REGS)
582 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
584 ira_max_memory_move_cost[mode][cl][0]
585 = ira_memory_move_cost[mode][cl][0]
586 = memory_move_cost ((enum machine_mode) mode,
587 (reg_class_t) cl, false);
588 ira_max_memory_move_cost[mode][cl][1]
589 = ira_memory_move_cost[mode][cl][1]
590 = memory_move_cost ((enum machine_mode) mode,
591 (reg_class_t) cl, true);
592 /* Costs for NO_REGS are used in cost calculation on the
593 1st pass when the preferred register classes are not
594 known yet. In this case we take the best scenario. */
595 if (ira_memory_move_cost[mode][NO_REGS][0]
596 > ira_memory_move_cost[mode][cl][0])
597 ira_max_memory_move_cost[mode][NO_REGS][0]
598 = ira_memory_move_cost[mode][NO_REGS][0]
599 = ira_memory_move_cost[mode][cl][0];
600 if (ira_memory_move_cost[mode][NO_REGS][1]
601 > ira_memory_move_cost[mode][cl][1])
602 ira_max_memory_move_cost[mode][NO_REGS][1]
603 = ira_memory_move_cost[mode][NO_REGS][1]
604 = ira_memory_move_cost[mode][cl][1];
607 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
608 for (cl2 = (int) N_REG_CLASSES - 1; cl2 >= 0; cl2--)
610 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
611 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
612 COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl2]);
613 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
614 ira_class_subset_p[cl][cl2]
615 = hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2);
616 if (! hard_reg_set_empty_p (temp_hard_regset2)
617 && hard_reg_set_subset_p (reg_class_contents[cl2],
618 reg_class_contents[cl]))
619 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
621 cost = ira_memory_move_cost[mode][cl2][0];
622 if (cost > ira_max_memory_move_cost[mode][cl][0])
623 ira_max_memory_move_cost[mode][cl][0] = cost;
624 cost = ira_memory_move_cost[mode][cl2][1];
625 if (cost > ira_max_memory_move_cost[mode][cl][1])
626 ira_max_memory_move_cost[mode][cl][1] = cost;
629 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
630 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
632 ira_memory_move_cost[mode][cl][0]
633 = ira_max_memory_move_cost[mode][cl][0];
634 ira_memory_move_cost[mode][cl][1]
635 = ira_max_memory_move_cost[mode][cl][1];
637 setup_reg_subclasses ();
642 /* Define the following macro if allocation through malloc if
643 preferable. */
644 #define IRA_NO_OBSTACK
646 #ifndef IRA_NO_OBSTACK
647 /* Obstack used for storing all dynamic data (except bitmaps) of the
648 IRA. */
649 static struct obstack ira_obstack;
650 #endif
652 /* Obstack used for storing all bitmaps of the IRA. */
653 static struct bitmap_obstack ira_bitmap_obstack;
655 /* Allocate memory of size LEN for IRA data. */
656 void *
657 ira_allocate (size_t len)
659 void *res;
661 #ifndef IRA_NO_OBSTACK
662 res = obstack_alloc (&ira_obstack, len);
663 #else
664 res = xmalloc (len);
665 #endif
666 return res;
669 /* Free memory ADDR allocated for IRA data. */
670 void
671 ira_free (void *addr ATTRIBUTE_UNUSED)
673 #ifndef IRA_NO_OBSTACK
674 /* do nothing */
675 #else
676 free (addr);
677 #endif
681 /* Allocate and returns bitmap for IRA. */
682 bitmap
683 ira_allocate_bitmap (void)
685 return BITMAP_ALLOC (&ira_bitmap_obstack);
688 /* Free bitmap B allocated for IRA. */
689 void
690 ira_free_bitmap (bitmap b ATTRIBUTE_UNUSED)
692 /* do nothing */
697 /* Output information about allocation of all allocnos (except for
698 caps) into file F. */
699 void
700 ira_print_disposition (FILE *f)
702 int i, n, max_regno;
703 ira_allocno_t a;
704 basic_block bb;
706 fprintf (f, "Disposition:");
707 max_regno = max_reg_num ();
708 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
709 for (a = ira_regno_allocno_map[i];
710 a != NULL;
711 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
713 if (n % 4 == 0)
714 fprintf (f, "\n");
715 n++;
716 fprintf (f, " %4d:r%-4d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
717 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
718 fprintf (f, "b%-3d", bb->index);
719 else
720 fprintf (f, "l%-3d", ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
721 if (ALLOCNO_HARD_REGNO (a) >= 0)
722 fprintf (f, " %3d", ALLOCNO_HARD_REGNO (a));
723 else
724 fprintf (f, " mem");
726 fprintf (f, "\n");
729 /* Outputs information about allocation of all allocnos into
730 stderr. */
731 void
732 ira_debug_disposition (void)
734 ira_print_disposition (stderr);
739 /* Set up ira_stack_reg_pressure_class which is the biggest pressure
740 register class containing stack registers or NO_REGS if there are
741 no stack registers. To find this class, we iterate through all
742 register pressure classes and choose the first register pressure
743 class containing all the stack registers and having the biggest
744 size. */
745 static void
746 setup_stack_reg_pressure_class (void)
748 ira_stack_reg_pressure_class = NO_REGS;
749 #ifdef STACK_REGS
751 int i, best, size;
752 enum reg_class cl;
753 HARD_REG_SET temp_hard_regset2;
755 CLEAR_HARD_REG_SET (temp_hard_regset);
756 for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
757 SET_HARD_REG_BIT (temp_hard_regset, i);
758 best = 0;
759 for (i = 0; i < ira_pressure_classes_num; i++)
761 cl = ira_pressure_classes[i];
762 COPY_HARD_REG_SET (temp_hard_regset2, temp_hard_regset);
763 AND_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
764 size = hard_reg_set_size (temp_hard_regset2);
765 if (best < size)
767 best = size;
768 ira_stack_reg_pressure_class = cl;
772 #endif
775 /* Find pressure classes which are register classes for which we
776 calculate register pressure in IRA, register pressure sensitive
777 insn scheduling, and register pressure sensitive loop invariant
778 motion.
780 To make register pressure calculation easy, we always use
781 non-intersected register pressure classes. A move of hard
782 registers from one register pressure class is not more expensive
783 than load and store of the hard registers. Most likely an allocno
784 class will be a subset of a register pressure class and in many
785 cases a register pressure class. That makes usage of register
786 pressure classes a good approximation to find a high register
787 pressure. */
788 static void
789 setup_pressure_classes (void)
791 int cost, i, n, curr;
792 int cl, cl2;
793 enum reg_class pressure_classes[N_REG_CLASSES];
794 int m;
795 HARD_REG_SET temp_hard_regset2;
796 bool insert_p;
798 n = 0;
799 for (cl = 0; cl < N_REG_CLASSES; cl++)
801 if (ira_available_class_regs[cl] == 0)
802 continue;
803 if (ira_available_class_regs[cl] != 1
804 /* A register class without subclasses may contain a few
805 hard registers and movement between them is costly
806 (e.g. SPARC FPCC registers). We still should consider it
807 as a candidate for a pressure class. */
808 && alloc_reg_class_subclasses[cl][0] != LIM_REG_CLASSES)
810 /* Check that the moves between any hard registers of the
811 current class are not more expensive for a legal mode
812 than load/store of the hard registers of the current
813 class. Such class is a potential candidate to be a
814 register pressure class. */
815 for (m = 0; m < NUM_MACHINE_MODES; m++)
817 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
818 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
819 AND_COMPL_HARD_REG_SET (temp_hard_regset,
820 ira_prohibited_class_mode_regs[cl][m]);
821 if (hard_reg_set_empty_p (temp_hard_regset))
822 continue;
823 ira_init_register_move_cost_if_necessary ((enum machine_mode) m);
824 cost = ira_register_move_cost[m][cl][cl];
825 if (cost <= ira_max_memory_move_cost[m][cl][1]
826 || cost <= ira_max_memory_move_cost[m][cl][0])
827 break;
829 if (m >= NUM_MACHINE_MODES)
830 continue;
832 curr = 0;
833 insert_p = true;
834 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
835 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
836 /* Remove so far added pressure classes which are subset of the
837 current candidate class. Prefer GENERAL_REGS as a pressure
838 register class to another class containing the same
839 allocatable hard registers. We do this because machine
840 dependent cost hooks might give wrong costs for the latter
841 class but always give the right cost for the former class
842 (GENERAL_REGS). */
843 for (i = 0; i < n; i++)
845 cl2 = pressure_classes[i];
846 COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl2]);
847 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
848 if (hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2)
849 && (! hard_reg_set_equal_p (temp_hard_regset, temp_hard_regset2)
850 || cl2 == (int) GENERAL_REGS))
852 pressure_classes[curr++] = (enum reg_class) cl2;
853 insert_p = false;
854 continue;
856 if (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset)
857 && (! hard_reg_set_equal_p (temp_hard_regset2, temp_hard_regset)
858 || cl == (int) GENERAL_REGS))
859 continue;
860 if (hard_reg_set_equal_p (temp_hard_regset2, temp_hard_regset))
861 insert_p = false;
862 pressure_classes[curr++] = (enum reg_class) cl2;
864 /* If the current candidate is a subset of a so far added
865 pressure class, don't add it to the list of the pressure
866 classes. */
867 if (insert_p)
868 pressure_classes[curr++] = (enum reg_class) cl;
869 n = curr;
871 #ifdef ENABLE_IRA_CHECKING
873 HARD_REG_SET ignore_hard_regs;
875 /* Check pressure classes correctness: here we check that hard
876 registers from all register pressure classes contains all hard
877 registers available for the allocation. */
878 CLEAR_HARD_REG_SET (temp_hard_regset);
879 CLEAR_HARD_REG_SET (temp_hard_regset2);
880 COPY_HARD_REG_SET (ignore_hard_regs, no_unit_alloc_regs);
881 for (cl = 0; cl < LIM_REG_CLASSES; cl++)
883 /* For some targets (like MIPS with MD_REGS), there are some
884 classes with hard registers available for allocation but
885 not able to hold value of any mode. */
886 for (m = 0; m < NUM_MACHINE_MODES; m++)
887 if (contains_reg_of_mode[cl][m])
888 break;
889 if (m >= NUM_MACHINE_MODES)
891 IOR_HARD_REG_SET (ignore_hard_regs, reg_class_contents[cl]);
892 continue;
894 for (i = 0; i < n; i++)
895 if ((int) pressure_classes[i] == cl)
896 break;
897 IOR_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
898 if (i < n)
899 IOR_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
901 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
902 /* Some targets (like SPARC with ICC reg) have alocatable regs
903 for which no reg class is defined. */
904 if (REGNO_REG_CLASS (i) == NO_REGS)
905 SET_HARD_REG_BIT (ignore_hard_regs, i);
906 AND_COMPL_HARD_REG_SET (temp_hard_regset, ignore_hard_regs);
907 AND_COMPL_HARD_REG_SET (temp_hard_regset2, ignore_hard_regs);
908 ira_assert (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset));
910 #endif
911 ira_pressure_classes_num = 0;
912 for (i = 0; i < n; i++)
914 cl = (int) pressure_classes[i];
915 ira_reg_pressure_class_p[cl] = true;
916 ira_pressure_classes[ira_pressure_classes_num++] = (enum reg_class) cl;
918 setup_stack_reg_pressure_class ();
921 /* Set up IRA_ALLOCNO_CLASSES, IRA_ALLOCNO_CLASSES_NUM,
922 IRA_IMPORTANT_CLASSES, and IRA_IMPORTANT_CLASSES_NUM.
924 Target may have many subtargets and not all target hard regiters can
925 be used for allocation, e.g. x86 port in 32-bit mode can not use
926 hard registers introduced in x86-64 like r8-r15). Some classes
927 might have the same allocatable hard registers, e.g. INDEX_REGS
928 and GENERAL_REGS in x86 port in 32-bit mode. To decrease different
929 calculations efforts we introduce allocno classes which contain
930 unique non-empty sets of allocatable hard-registers.
932 Pseudo class cost calculation in ira-costs.c is very expensive.
933 Therefore we are trying to decrease number of classes involved in
934 such calculation. Register classes used in the cost calculation
935 are called important classes. They are allocno classes and other
936 non-empty classes whose allocatable hard register sets are inside
937 of an allocno class hard register set. From the first sight, it
938 looks like that they are just allocno classes. It is not true. In
939 example of x86-port in 32-bit mode, allocno classes will contain
940 GENERAL_REGS but not LEGACY_REGS (because allocatable hard
941 registers are the same for the both classes). The important
942 classes will contain GENERAL_REGS and LEGACY_REGS. It is done
943 because a machine description insn constraint may refers for
944 LEGACY_REGS and code in ira-costs.c is mostly base on investigation
945 of the insn constraints. */
946 static void
947 setup_allocno_and_important_classes (void)
949 int i, j, n, cl;
950 bool set_p;
951 HARD_REG_SET temp_hard_regset2;
952 static enum reg_class classes[LIM_REG_CLASSES + 1];
954 n = 0;
955 /* Collect classes which contain unique sets of allocatable hard
956 registers. Prefer GENERAL_REGS to other classes containing the
957 same set of hard registers. */
958 for (i = 0; i < LIM_REG_CLASSES; i++)
960 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
961 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
962 for (j = 0; j < n; j++)
964 cl = classes[j];
965 COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
966 AND_COMPL_HARD_REG_SET (temp_hard_regset2,
967 no_unit_alloc_regs);
968 if (hard_reg_set_equal_p (temp_hard_regset,
969 temp_hard_regset2))
970 break;
972 if (j >= n)
973 classes[n++] = (enum reg_class) i;
974 else if (i == GENERAL_REGS)
975 /* Prefer general regs. For i386 example, it means that
976 we prefer GENERAL_REGS over INDEX_REGS or LEGACY_REGS
977 (all of them consists of the same available hard
978 registers). */
979 classes[j] = (enum reg_class) i;
981 classes[n] = LIM_REG_CLASSES;
983 /* Set up classes which can be used for allocnos as classes
984 conatining non-empty unique sets of allocatable hard
985 registers. */
986 ira_allocno_classes_num = 0;
987 for (i = 0; (cl = classes[i]) != LIM_REG_CLASSES; i++)
989 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
990 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
991 if (hard_reg_set_empty_p (temp_hard_regset))
992 continue;
993 ira_allocno_classes[ira_allocno_classes_num++] = (enum reg_class) cl;
995 ira_important_classes_num = 0;
996 /* Add non-allocno classes containing to non-empty set of
997 allocatable hard regs. */
998 for (cl = 0; cl < N_REG_CLASSES; cl++)
1000 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
1001 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1002 if (! hard_reg_set_empty_p (temp_hard_regset))
1004 set_p = false;
1005 for (j = 0; j < ira_allocno_classes_num; j++)
1007 COPY_HARD_REG_SET (temp_hard_regset2,
1008 reg_class_contents[ira_allocno_classes[j]]);
1009 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
1010 if ((enum reg_class) cl == ira_allocno_classes[j])
1011 break;
1012 else if (hard_reg_set_subset_p (temp_hard_regset,
1013 temp_hard_regset2))
1014 set_p = true;
1016 if (set_p && j >= ira_allocno_classes_num)
1017 ira_important_classes[ira_important_classes_num++]
1018 = (enum reg_class) cl;
1021 /* Now add allocno classes to the important classes. */
1022 for (j = 0; j < ira_allocno_classes_num; j++)
1023 ira_important_classes[ira_important_classes_num++]
1024 = ira_allocno_classes[j];
1025 for (cl = 0; cl < N_REG_CLASSES; cl++)
1027 ira_reg_allocno_class_p[cl] = false;
1028 ira_reg_pressure_class_p[cl] = false;
1030 for (j = 0; j < ira_allocno_classes_num; j++)
1031 ira_reg_allocno_class_p[ira_allocno_classes[j]] = true;
1032 setup_pressure_classes ();
1035 /* Setup translation in CLASS_TRANSLATE of all classes into a class
1036 given by array CLASSES of length CLASSES_NUM. The function is used
1037 make translation any reg class to an allocno class or to an
1038 pressure class. This translation is necessary for some
1039 calculations when we can use only allocno or pressure classes and
1040 such translation represents an approximate representation of all
1041 classes.
1043 The translation in case when allocatable hard register set of a
1044 given class is subset of allocatable hard register set of a class
1045 in CLASSES is pretty simple. We use smallest classes from CLASSES
1046 containing a given class. If allocatable hard register set of a
1047 given class is not a subset of any corresponding set of a class
1048 from CLASSES, we use the cheapest (with load/store point of view)
1049 class from CLASSES whose set intersects with given class set */
1050 static void
1051 setup_class_translate_array (enum reg_class *class_translate,
1052 int classes_num, enum reg_class *classes)
1054 int cl, mode;
1055 enum reg_class aclass, best_class, *cl_ptr;
1056 int i, cost, min_cost, best_cost;
1058 for (cl = 0; cl < N_REG_CLASSES; cl++)
1059 class_translate[cl] = NO_REGS;
1061 for (i = 0; i < classes_num; i++)
1063 aclass = classes[i];
1064 for (cl_ptr = &alloc_reg_class_subclasses[aclass][0];
1065 (cl = *cl_ptr) != LIM_REG_CLASSES;
1066 cl_ptr++)
1067 if (class_translate[cl] == NO_REGS)
1068 class_translate[cl] = aclass;
1069 class_translate[aclass] = aclass;
1071 /* For classes which are not fully covered by one of given classes
1072 (in other words covered by more one given class), use the
1073 cheapest class. */
1074 for (cl = 0; cl < N_REG_CLASSES; cl++)
1076 if (cl == NO_REGS || class_translate[cl] != NO_REGS)
1077 continue;
1078 best_class = NO_REGS;
1079 best_cost = INT_MAX;
1080 for (i = 0; i < classes_num; i++)
1082 aclass = classes[i];
1083 COPY_HARD_REG_SET (temp_hard_regset,
1084 reg_class_contents[aclass]);
1085 AND_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
1086 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1087 if (! hard_reg_set_empty_p (temp_hard_regset))
1089 min_cost = INT_MAX;
1090 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1092 cost = (ira_memory_move_cost[mode][cl][0]
1093 + ira_memory_move_cost[mode][cl][1]);
1094 if (min_cost > cost)
1095 min_cost = cost;
1097 if (best_class == NO_REGS || best_cost > min_cost)
1099 best_class = aclass;
1100 best_cost = min_cost;
1104 class_translate[cl] = best_class;
1108 /* Set up array IRA_ALLOCNO_CLASS_TRANSLATE and
1109 IRA_PRESSURE_CLASS_TRANSLATE. */
1110 static void
1111 setup_class_translate (void)
1113 setup_class_translate_array (ira_allocno_class_translate,
1114 ira_allocno_classes_num, ira_allocno_classes);
1115 setup_class_translate_array (ira_pressure_class_translate,
1116 ira_pressure_classes_num, ira_pressure_classes);
1119 /* Order numbers of allocno classes in original target allocno class
1120 array, -1 for non-allocno classes. */
1121 static int allocno_class_order[N_REG_CLASSES];
1123 /* The function used to sort the important classes. */
1124 static int
1125 comp_reg_classes_func (const void *v1p, const void *v2p)
1127 enum reg_class cl1 = *(const enum reg_class *) v1p;
1128 enum reg_class cl2 = *(const enum reg_class *) v2p;
1129 enum reg_class tcl1, tcl2;
1130 int diff;
1132 tcl1 = ira_allocno_class_translate[cl1];
1133 tcl2 = ira_allocno_class_translate[cl2];
1134 if (tcl1 != NO_REGS && tcl2 != NO_REGS
1135 && (diff = allocno_class_order[tcl1] - allocno_class_order[tcl2]) != 0)
1136 return diff;
1137 return (int) cl1 - (int) cl2;
1140 /* For correct work of function setup_reg_class_relation we need to
1141 reorder important classes according to the order of their allocno
1142 classes. It places important classes containing the same
1143 allocatable hard register set adjacent to each other and allocno
1144 class with the allocatable hard register set right after the other
1145 important classes with the same set.
1147 In example from comments of function
1148 setup_allocno_and_important_classes, it places LEGACY_REGS and
1149 GENERAL_REGS close to each other and GENERAL_REGS is after
1150 LEGACY_REGS. */
1151 static void
1152 reorder_important_classes (void)
1154 int i;
1156 for (i = 0; i < N_REG_CLASSES; i++)
1157 allocno_class_order[i] = -1;
1158 for (i = 0; i < ira_allocno_classes_num; i++)
1159 allocno_class_order[ira_allocno_classes[i]] = i;
1160 qsort (ira_important_classes, ira_important_classes_num,
1161 sizeof (enum reg_class), comp_reg_classes_func);
1162 for (i = 0; i < ira_important_classes_num; i++)
1163 ira_important_class_nums[ira_important_classes[i]] = i;
1166 /* Set up IRA_REG_CLASS_SUBUNION, IRA_REG_CLASS_SUPERUNION,
1167 IRA_REG_CLASS_SUPER_CLASSES, IRA_REG_CLASSES_INTERSECT, and
1168 IRA_REG_CLASSES_INTERSECT_P. For the meaning of the relations,
1169 please see corresponding comments in ira-int.h. */
1170 static void
1171 setup_reg_class_relations (void)
1173 int i, cl1, cl2, cl3;
1174 HARD_REG_SET intersection_set, union_set, temp_set2;
1175 bool important_class_p[N_REG_CLASSES];
1177 memset (important_class_p, 0, sizeof (important_class_p));
1178 for (i = 0; i < ira_important_classes_num; i++)
1179 important_class_p[ira_important_classes[i]] = true;
1180 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1182 ira_reg_class_super_classes[cl1][0] = LIM_REG_CLASSES;
1183 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1185 ira_reg_classes_intersect_p[cl1][cl2] = false;
1186 ira_reg_class_intersect[cl1][cl2] = NO_REGS;
1187 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl1]);
1188 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1189 COPY_HARD_REG_SET (temp_set2, reg_class_contents[cl2]);
1190 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1191 if (hard_reg_set_empty_p (temp_hard_regset)
1192 && hard_reg_set_empty_p (temp_set2))
1194 /* The both classes have no allocatable hard registers
1195 -- take all class hard registers into account and use
1196 reg_class_subunion and reg_class_superunion. */
1197 for (i = 0;; i++)
1199 cl3 = reg_class_subclasses[cl1][i];
1200 if (cl3 == LIM_REG_CLASSES)
1201 break;
1202 if (reg_class_subset_p (ira_reg_class_intersect[cl1][cl2],
1203 (enum reg_class) cl3))
1204 ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1206 ira_reg_class_subunion[cl1][cl2] = reg_class_subunion[cl1][cl2];
1207 ira_reg_class_superunion[cl1][cl2] = reg_class_superunion[cl1][cl2];
1208 continue;
1210 ira_reg_classes_intersect_p[cl1][cl2]
1211 = hard_reg_set_intersect_p (temp_hard_regset, temp_set2);
1212 if (important_class_p[cl1] && important_class_p[cl2]
1213 && hard_reg_set_subset_p (temp_hard_regset, temp_set2))
1215 /* CL1 and CL2 are important classes and CL1 allocatable
1216 hard register set is inside of CL2 allocatable hard
1217 registers -- make CL1 a superset of CL2. */
1218 enum reg_class *p;
1220 p = &ira_reg_class_super_classes[cl1][0];
1221 while (*p != LIM_REG_CLASSES)
1222 p++;
1223 *p++ = (enum reg_class) cl2;
1224 *p = LIM_REG_CLASSES;
1226 ira_reg_class_subunion[cl1][cl2] = NO_REGS;
1227 ira_reg_class_superunion[cl1][cl2] = NO_REGS;
1228 COPY_HARD_REG_SET (intersection_set, reg_class_contents[cl1]);
1229 AND_HARD_REG_SET (intersection_set, reg_class_contents[cl2]);
1230 AND_COMPL_HARD_REG_SET (intersection_set, no_unit_alloc_regs);
1231 COPY_HARD_REG_SET (union_set, reg_class_contents[cl1]);
1232 IOR_HARD_REG_SET (union_set, reg_class_contents[cl2]);
1233 AND_COMPL_HARD_REG_SET (union_set, no_unit_alloc_regs);
1234 for (i = 0; i < ira_important_classes_num; i++)
1236 cl3 = ira_important_classes[i];
1237 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl3]);
1238 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1239 if (hard_reg_set_subset_p (temp_hard_regset, intersection_set))
1241 /* CL3 allocatable hard register set is inside of
1242 intersection of allocatable hard register sets
1243 of CL1 and CL2. */
1244 COPY_HARD_REG_SET
1245 (temp_set2,
1246 reg_class_contents[(int)
1247 ira_reg_class_intersect[cl1][cl2]]);
1248 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1249 if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1250 /* If the allocatable hard register sets are the
1251 same, prefer GENERAL_REGS or the smallest
1252 class for debugging purposes. */
1253 || (hard_reg_set_equal_p (temp_hard_regset, temp_set2)
1254 && (cl3 == GENERAL_REGS
1255 || (ira_reg_class_intersect[cl1][cl2] != GENERAL_REGS
1256 && hard_reg_set_subset_p
1257 (reg_class_contents[cl3],
1258 reg_class_contents
1259 [(int) ira_reg_class_intersect[cl1][cl2]])))))
1260 ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1262 if (hard_reg_set_subset_p (temp_hard_regset, union_set))
1264 /* CL3 allocatbale hard register set is inside of
1265 union of allocatable hard register sets of CL1
1266 and CL2. */
1267 COPY_HARD_REG_SET
1268 (temp_set2,
1269 reg_class_contents[(int) ira_reg_class_subunion[cl1][cl2]]);
1270 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1271 if (ira_reg_class_subunion[cl1][cl2] == NO_REGS
1272 || (hard_reg_set_subset_p (temp_set2, temp_hard_regset)
1274 && (! hard_reg_set_equal_p (temp_set2,
1275 temp_hard_regset)
1276 || cl3 == GENERAL_REGS
1277 /* If the allocatable hard register sets are the
1278 same, prefer GENERAL_REGS or the smallest
1279 class for debugging purposes. */
1280 || (ira_reg_class_subunion[cl1][cl2] != GENERAL_REGS
1281 && hard_reg_set_subset_p
1282 (reg_class_contents[cl3],
1283 reg_class_contents
1284 [(int) ira_reg_class_subunion[cl1][cl2]])))))
1285 ira_reg_class_subunion[cl1][cl2] = (enum reg_class) cl3;
1287 if (hard_reg_set_subset_p (union_set, temp_hard_regset))
1289 /* CL3 allocatable hard register set contains union
1290 of allocatable hard register sets of CL1 and
1291 CL2. */
1292 COPY_HARD_REG_SET
1293 (temp_set2,
1294 reg_class_contents[(int) ira_reg_class_superunion[cl1][cl2]]);
1295 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1296 if (ira_reg_class_superunion[cl1][cl2] == NO_REGS
1297 || (hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1299 && (! hard_reg_set_equal_p (temp_set2,
1300 temp_hard_regset)
1301 || cl3 == GENERAL_REGS
1302 /* If the allocatable hard register sets are the
1303 same, prefer GENERAL_REGS or the smallest
1304 class for debugging purposes. */
1305 || (ira_reg_class_superunion[cl1][cl2] != GENERAL_REGS
1306 && hard_reg_set_subset_p
1307 (reg_class_contents[cl3],
1308 reg_class_contents
1309 [(int) ira_reg_class_superunion[cl1][cl2]])))))
1310 ira_reg_class_superunion[cl1][cl2] = (enum reg_class) cl3;
1317 /* Output all possible allocno classes and the translation map into
1318 file F. */
1319 static void
1320 print_classes (FILE *f, bool pressure_p)
1322 int classes_num = (pressure_p
1323 ? ira_pressure_classes_num : ira_allocno_classes_num);
1324 enum reg_class *classes = (pressure_p
1325 ? ira_pressure_classes : ira_allocno_classes);
1326 enum reg_class *class_translate = (pressure_p
1327 ? ira_pressure_class_translate
1328 : ira_allocno_class_translate);
1329 static const char *const reg_class_names[] = REG_CLASS_NAMES;
1330 int i;
1332 fprintf (f, "%s classes:\n", pressure_p ? "Pressure" : "Allocno");
1333 for (i = 0; i < classes_num; i++)
1334 fprintf (f, " %s", reg_class_names[classes[i]]);
1335 fprintf (f, "\nClass translation:\n");
1336 for (i = 0; i < N_REG_CLASSES; i++)
1337 fprintf (f, " %s -> %s\n", reg_class_names[i],
1338 reg_class_names[class_translate[i]]);
1341 /* Output all possible allocno and translation classes and the
1342 translation maps into stderr. */
1343 void
1344 ira_debug_allocno_classes (void)
1346 print_classes (stderr, false);
1347 print_classes (stderr, true);
1350 /* Set up different arrays concerning class subsets, allocno and
1351 important classes. */
1352 static void
1353 find_reg_classes (void)
1355 setup_allocno_and_important_classes ();
1356 setup_class_translate ();
1357 reorder_important_classes ();
1358 setup_reg_class_relations ();
1363 /* Set up the array above. */
1364 static void
1365 setup_hard_regno_aclass (void)
1367 int i;
1369 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1371 #if 1
1372 ira_hard_regno_allocno_class[i]
1373 = (TEST_HARD_REG_BIT (no_unit_alloc_regs, i)
1374 ? NO_REGS
1375 : ira_allocno_class_translate[REGNO_REG_CLASS (i)]);
1376 #else
1377 int j;
1378 enum reg_class cl;
1379 ira_hard_regno_allocno_class[i] = NO_REGS;
1380 for (j = 0; j < ira_allocno_classes_num; j++)
1382 cl = ira_allocno_classes[j];
1383 if (ira_class_hard_reg_index[cl][i] >= 0)
1385 ira_hard_regno_allocno_class[i] = cl;
1386 break;
1389 #endif
1395 /* Form IRA_REG_CLASS_MAX_NREGS and IRA_REG_CLASS_MIN_NREGS maps. */
1396 static void
1397 setup_reg_class_nregs (void)
1399 int i, cl, cl2, m;
1401 for (m = 0; m < MAX_MACHINE_MODE; m++)
1403 for (cl = 0; cl < N_REG_CLASSES; cl++)
1404 ira_reg_class_max_nregs[cl][m]
1405 = ira_reg_class_min_nregs[cl][m]
1406 = targetm.class_max_nregs ((reg_class_t) cl, (enum machine_mode) m);
1407 for (cl = 0; cl < N_REG_CLASSES; cl++)
1408 for (i = 0;
1409 (cl2 = alloc_reg_class_subclasses[cl][i]) != LIM_REG_CLASSES;
1410 i++)
1411 if (ira_reg_class_min_nregs[cl2][m]
1412 < ira_reg_class_min_nregs[cl][m])
1413 ira_reg_class_min_nregs[cl][m] = ira_reg_class_min_nregs[cl2][m];
1419 /* Set up IRA_PROHIBITED_CLASS_MODE_REGS. */
1420 static void
1421 setup_prohibited_class_mode_regs (void)
1423 int j, k, hard_regno, cl;
1425 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
1427 for (j = 0; j < NUM_MACHINE_MODES; j++)
1429 CLEAR_HARD_REG_SET (ira_prohibited_class_mode_regs[cl][j]);
1430 for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1432 hard_regno = ira_class_hard_regs[cl][k];
1433 if (! HARD_REGNO_MODE_OK (hard_regno, (enum machine_mode) j))
1434 SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1435 hard_regno);
1441 /* Clarify IRA_PROHIBITED_CLASS_MODE_REGS by excluding hard registers
1442 spanning from one register pressure class to another one. It is
1443 called after defining the pressure classes. */
1444 static void
1445 clarify_prohibited_class_mode_regs (void)
1447 int j, k, hard_regno, cl, pclass, nregs;
1449 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
1450 for (j = 0; j < NUM_MACHINE_MODES; j++)
1451 for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1453 hard_regno = ira_class_hard_regs[cl][k];
1454 if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j], hard_regno))
1455 continue;
1456 nregs = hard_regno_nregs[hard_regno][j];
1457 if (hard_regno + nregs > FIRST_PSEUDO_REGISTER)
1459 SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1460 hard_regno);
1461 continue;
1463 pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
1464 for (nregs-- ;nregs >= 0; nregs--)
1465 if (((enum reg_class) pclass
1466 != ira_pressure_class_translate[REGNO_REG_CLASS
1467 (hard_regno + nregs)]))
1469 SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1470 hard_regno);
1471 break;
1478 /* Allocate and initialize IRA_REGISTER_MOVE_COST,
1479 IRA_MAX_REGISTER_MOVE_COST, IRA_MAY_MOVE_IN_COST,
1480 IRA_MAY_MOVE_OUT_COST, IRA_MAX_MAY_MOVE_IN_COST, and
1481 IRA_MAX_MAY_MOVE_OUT_COST for MODE if it is not done yet. */
1482 void
1483 ira_init_register_move_cost (enum machine_mode mode)
1485 int cl1, cl2, cl3;
1487 ira_assert (ira_register_move_cost[mode] == NULL
1488 && ira_max_register_move_cost[mode] == NULL
1489 && ira_may_move_in_cost[mode] == NULL
1490 && ira_may_move_out_cost[mode] == NULL
1491 && ira_max_may_move_in_cost[mode] == NULL
1492 && ira_max_may_move_out_cost[mode] == NULL);
1493 if (move_cost[mode] == NULL)
1494 init_move_cost (mode);
1495 ira_register_move_cost[mode] = move_cost[mode];
1496 /* Don't use ira_allocate because the tables exist out of scope of a
1497 IRA call. */
1498 ira_max_register_move_cost[mode]
1499 = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1500 memcpy (ira_max_register_move_cost[mode], ira_register_move_cost[mode],
1501 sizeof (move_table) * N_REG_CLASSES);
1502 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1504 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl1]);
1505 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1506 if (hard_reg_set_empty_p (temp_hard_regset))
1507 continue;
1508 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1509 if (hard_reg_set_subset_p (reg_class_contents[cl1],
1510 reg_class_contents[cl2]))
1511 for (cl3 = 0; cl3 < N_REG_CLASSES; cl3++)
1513 if (ira_max_register_move_cost[mode][cl2][cl3]
1514 < ira_register_move_cost[mode][cl1][cl3])
1515 ira_max_register_move_cost[mode][cl2][cl3]
1516 = ira_register_move_cost[mode][cl1][cl3];
1517 if (ira_max_register_move_cost[mode][cl3][cl2]
1518 < ira_register_move_cost[mode][cl3][cl1])
1519 ira_max_register_move_cost[mode][cl3][cl2]
1520 = ira_register_move_cost[mode][cl3][cl1];
1523 ira_may_move_in_cost[mode]
1524 = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1525 memcpy (ira_may_move_in_cost[mode], may_move_in_cost[mode],
1526 sizeof (move_table) * N_REG_CLASSES);
1527 ira_may_move_out_cost[mode]
1528 = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1529 memcpy (ira_may_move_out_cost[mode], may_move_out_cost[mode],
1530 sizeof (move_table) * N_REG_CLASSES);
1531 ira_max_may_move_in_cost[mode]
1532 = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1533 memcpy (ira_max_may_move_in_cost[mode], ira_max_register_move_cost[mode],
1534 sizeof (move_table) * N_REG_CLASSES);
1535 ira_max_may_move_out_cost[mode]
1536 = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1537 memcpy (ira_max_may_move_out_cost[mode], ira_max_register_move_cost[mode],
1538 sizeof (move_table) * N_REG_CLASSES);
1539 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1541 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1543 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl2]);
1544 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1545 if (hard_reg_set_empty_p (temp_hard_regset))
1546 continue;
1547 if (ira_class_subset_p[cl1][cl2])
1548 ira_may_move_in_cost[mode][cl1][cl2] = 0;
1549 if (ira_class_subset_p[cl2][cl1])
1550 ira_may_move_out_cost[mode][cl1][cl2] = 0;
1551 if (ira_class_subset_p[cl1][cl2])
1552 ira_max_may_move_in_cost[mode][cl1][cl2] = 0;
1553 if (ira_class_subset_p[cl2][cl1])
1554 ira_max_may_move_out_cost[mode][cl1][cl2] = 0;
1555 ira_register_move_cost[mode][cl1][cl2]
1556 = ira_max_register_move_cost[mode][cl1][cl2];
1557 ira_may_move_in_cost[mode][cl1][cl2]
1558 = ira_max_may_move_in_cost[mode][cl1][cl2];
1559 ira_may_move_out_cost[mode][cl1][cl2]
1560 = ira_max_may_move_out_cost[mode][cl1][cl2];
1567 /* This is called once during compiler work. It sets up
1568 different arrays whose values don't depend on the compiled
1569 function. */
1570 void
1571 ira_init_once (void)
1573 int mode;
1575 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1577 ira_register_move_cost[mode] = NULL;
1578 ira_max_register_move_cost[mode] = NULL;
1579 ira_may_move_in_cost[mode] = NULL;
1580 ira_may_move_out_cost[mode] = NULL;
1581 ira_max_may_move_in_cost[mode] = NULL;
1582 ira_max_may_move_out_cost[mode] = NULL;
1584 ira_init_costs_once ();
1587 /* Free ira_max_register_move_cost, ira_may_move_in_cost,
1588 ira_may_move_out_cost, ira_max_may_move_in_cost, and
1589 ira_max_may_move_out_cost for each mode. */
1590 static void
1591 free_register_move_costs (void)
1593 int mode;
1595 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1597 free (ira_max_register_move_cost[mode]);
1598 free (ira_may_move_in_cost[mode]);
1599 free (ira_may_move_out_cost[mode]);
1600 free (ira_max_may_move_in_cost[mode]);
1601 free (ira_max_may_move_out_cost[mode]);
1602 ira_register_move_cost[mode] = NULL;
1603 ira_max_register_move_cost[mode] = NULL;
1604 ira_may_move_in_cost[mode] = NULL;
1605 ira_may_move_out_cost[mode] = NULL;
1606 ira_max_may_move_in_cost[mode] = NULL;
1607 ira_max_may_move_out_cost[mode] = NULL;
1611 /* This is called every time when register related information is
1612 changed. */
1613 void
1614 ira_init (void)
1616 free_register_move_costs ();
1617 setup_reg_mode_hard_regset ();
1618 setup_alloc_regs (flag_omit_frame_pointer != 0);
1619 setup_class_subset_and_memory_move_costs ();
1620 setup_reg_class_nregs ();
1621 setup_prohibited_class_mode_regs ();
1622 find_reg_classes ();
1623 clarify_prohibited_class_mode_regs ();
1624 setup_hard_regno_aclass ();
1625 ira_init_costs ();
1628 /* Function called once at the end of compiler work. */
1629 void
1630 ira_finish_once (void)
1632 ira_finish_costs_once ();
1633 free_register_move_costs ();
1637 #define ira_prohibited_mode_move_regs_initialized_p \
1638 (this_target_ira_int->x_ira_prohibited_mode_move_regs_initialized_p)
1640 /* Set up IRA_PROHIBITED_MODE_MOVE_REGS. */
1641 static void
1642 setup_prohibited_mode_move_regs (void)
1644 int i, j;
1645 rtx test_reg1, test_reg2, move_pat, move_insn;
1647 if (ira_prohibited_mode_move_regs_initialized_p)
1648 return;
1649 ira_prohibited_mode_move_regs_initialized_p = true;
1650 test_reg1 = gen_rtx_REG (VOIDmode, 0);
1651 test_reg2 = gen_rtx_REG (VOIDmode, 0);
1652 move_pat = gen_rtx_SET (VOIDmode, test_reg1, test_reg2);
1653 move_insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, move_pat, 0, -1, 0);
1654 for (i = 0; i < NUM_MACHINE_MODES; i++)
1656 SET_HARD_REG_SET (ira_prohibited_mode_move_regs[i]);
1657 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1659 if (! HARD_REGNO_MODE_OK (j, (enum machine_mode) i))
1660 continue;
1661 SET_REGNO_RAW (test_reg1, j);
1662 PUT_MODE (test_reg1, (enum machine_mode) i);
1663 SET_REGNO_RAW (test_reg2, j);
1664 PUT_MODE (test_reg2, (enum machine_mode) i);
1665 INSN_CODE (move_insn) = -1;
1666 recog_memoized (move_insn);
1667 if (INSN_CODE (move_insn) < 0)
1668 continue;
1669 extract_insn (move_insn);
1670 if (! constrain_operands (1))
1671 continue;
1672 CLEAR_HARD_REG_BIT (ira_prohibited_mode_move_regs[i], j);
1679 /* Return nonzero if REGNO is a particularly bad choice for reloading X. */
1680 static bool
1681 ira_bad_reload_regno_1 (int regno, rtx x)
1683 int x_regno, n, i;
1684 ira_allocno_t a;
1685 enum reg_class pref;
1687 /* We only deal with pseudo regs. */
1688 if (! x || GET_CODE (x) != REG)
1689 return false;
1691 x_regno = REGNO (x);
1692 if (x_regno < FIRST_PSEUDO_REGISTER)
1693 return false;
1695 /* If the pseudo prefers REGNO explicitly, then do not consider
1696 REGNO a bad spill choice. */
1697 pref = reg_preferred_class (x_regno);
1698 if (reg_class_size[pref] == 1)
1699 return !TEST_HARD_REG_BIT (reg_class_contents[pref], regno);
1701 /* If the pseudo conflicts with REGNO, then we consider REGNO a
1702 poor choice for a reload regno. */
1703 a = ira_regno_allocno_map[x_regno];
1704 n = ALLOCNO_NUM_OBJECTS (a);
1705 for (i = 0; i < n; i++)
1707 ira_object_t obj = ALLOCNO_OBJECT (a, i);
1708 if (TEST_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), regno))
1709 return true;
1711 return false;
1714 /* Return nonzero if REGNO is a particularly bad choice for reloading
1715 IN or OUT. */
1716 bool
1717 ira_bad_reload_regno (int regno, rtx in, rtx out)
1719 return (ira_bad_reload_regno_1 (regno, in)
1720 || ira_bad_reload_regno_1 (regno, out));
1723 /* Return TRUE if *LOC contains an asm. */
1724 static int
1725 insn_contains_asm_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
1727 if ( !*loc)
1728 return FALSE;
1729 if (GET_CODE (*loc) == ASM_OPERANDS)
1730 return TRUE;
1731 return FALSE;
1735 /* Return TRUE if INSN contains an ASM. */
1736 static bool
1737 insn_contains_asm (rtx insn)
1739 return for_each_rtx (&insn, insn_contains_asm_1, NULL);
1742 /* Add register clobbers from asm statements. */
1743 static void
1744 compute_regs_asm_clobbered (void)
1746 basic_block bb;
1748 FOR_EACH_BB (bb)
1750 rtx insn;
1751 FOR_BB_INSNS_REVERSE (bb, insn)
1753 df_ref *def_rec;
1755 if (insn_contains_asm (insn))
1756 for (def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++)
1758 df_ref def = *def_rec;
1759 unsigned int dregno = DF_REF_REGNO (def);
1760 if (HARD_REGISTER_NUM_P (dregno))
1761 add_to_hard_reg_set (&crtl->asm_clobbers,
1762 GET_MODE (DF_REF_REAL_REG (def)),
1763 dregno);
1770 /* Set up ELIMINABLE_REGSET, IRA_NO_ALLOC_REGS, and REGS_EVER_LIVE. */
1771 void
1772 ira_setup_eliminable_regset (void)
1774 #ifdef ELIMINABLE_REGS
1775 int i;
1776 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
1777 #endif
1778 /* FIXME: If EXIT_IGNORE_STACK is set, we will not save and restore
1779 sp for alloca. So we can't eliminate the frame pointer in that
1780 case. At some point, we should improve this by emitting the
1781 sp-adjusting insns for this case. */
1782 int need_fp
1783 = (! flag_omit_frame_pointer
1784 || (cfun->calls_alloca && EXIT_IGNORE_STACK)
1785 /* We need the frame pointer to catch stack overflow exceptions
1786 if the stack pointer is moving. */
1787 || (flag_stack_check && STACK_CHECK_MOVING_SP)
1788 || crtl->accesses_prior_frames
1789 || crtl->stack_realign_needed
1790 || targetm.frame_pointer_required ());
1792 frame_pointer_needed = need_fp;
1794 COPY_HARD_REG_SET (ira_no_alloc_regs, no_unit_alloc_regs);
1795 CLEAR_HARD_REG_SET (eliminable_regset);
1797 compute_regs_asm_clobbered ();
1799 /* Build the regset of all eliminable registers and show we can't
1800 use those that we already know won't be eliminated. */
1801 #ifdef ELIMINABLE_REGS
1802 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
1804 bool cannot_elim
1805 = (! targetm.can_eliminate (eliminables[i].from, eliminables[i].to)
1806 || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp));
1808 if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, eliminables[i].from))
1810 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
1812 if (cannot_elim)
1813 SET_HARD_REG_BIT (ira_no_alloc_regs, eliminables[i].from);
1815 else if (cannot_elim)
1816 error ("%s cannot be used in asm here",
1817 reg_names[eliminables[i].from]);
1818 else
1819 df_set_regs_ever_live (eliminables[i].from, true);
1821 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1822 if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, HARD_FRAME_POINTER_REGNUM))
1824 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
1825 if (need_fp)
1826 SET_HARD_REG_BIT (ira_no_alloc_regs, HARD_FRAME_POINTER_REGNUM);
1828 else if (need_fp)
1829 error ("%s cannot be used in asm here",
1830 reg_names[HARD_FRAME_POINTER_REGNUM]);
1831 else
1832 df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
1833 #endif
1835 #else
1836 if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, HARD_FRAME_POINTER_REGNUM))
1838 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
1839 if (need_fp)
1840 SET_HARD_REG_BIT (ira_no_alloc_regs, FRAME_POINTER_REGNUM);
1842 else if (need_fp)
1843 error ("%s cannot be used in asm here", reg_names[FRAME_POINTER_REGNUM]);
1844 else
1845 df_set_regs_ever_live (FRAME_POINTER_REGNUM, true);
1846 #endif
1851 /* The length of the following two arrays. */
1852 int ira_reg_equiv_len;
1854 /* The element value is TRUE if the corresponding regno value is
1855 invariant. */
1856 bool *ira_reg_equiv_invariant_p;
1858 /* The element value is equiv constant of given pseudo-register or
1859 NULL_RTX. */
1860 rtx *ira_reg_equiv_const;
1862 /* Set up the two arrays declared above. */
1863 static void
1864 find_reg_equiv_invariant_const (void)
1866 unsigned int i;
1867 bool invariant_p;
1868 rtx list, insn, note, constant, x;
1870 for (i = FIRST_PSEUDO_REGISTER; i < VEC_length (reg_equivs_t, reg_equivs); i++)
1872 constant = NULL_RTX;
1873 invariant_p = false;
1874 for (list = reg_equiv_init (i); list != NULL_RTX; list = XEXP (list, 1))
1876 insn = XEXP (list, 0);
1877 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
1879 if (note == NULL_RTX)
1880 continue;
1882 x = XEXP (note, 0);
1884 if (! CONSTANT_P (x)
1885 || ! flag_pic || LEGITIMATE_PIC_OPERAND_P (x))
1887 /* It can happen that a REG_EQUIV note contains a MEM
1888 that is not a legitimate memory operand. As later
1889 stages of the reload assume that all addresses found
1890 in the reg_equiv_* arrays were originally legitimate,
1891 we ignore such REG_EQUIV notes. */
1892 if (memory_operand (x, VOIDmode))
1893 invariant_p = MEM_READONLY_P (x);
1894 else if (function_invariant_p (x))
1896 if (GET_CODE (x) == PLUS
1897 || x == frame_pointer_rtx || x == arg_pointer_rtx)
1898 invariant_p = true;
1899 else
1900 constant = x;
1904 ira_reg_equiv_invariant_p[i] = invariant_p;
1905 ira_reg_equiv_const[i] = constant;
1911 /* Vector of substitutions of register numbers,
1912 used to map pseudo regs into hardware regs.
1913 This is set up as a result of register allocation.
1914 Element N is the hard reg assigned to pseudo reg N,
1915 or is -1 if no hard reg was assigned.
1916 If N is a hard reg number, element N is N. */
1917 short *reg_renumber;
1919 /* Set up REG_RENUMBER and CALLER_SAVE_NEEDED (used by reload) from
1920 the allocation found by IRA. */
1921 static void
1922 setup_reg_renumber (void)
1924 int regno, hard_regno;
1925 ira_allocno_t a;
1926 ira_allocno_iterator ai;
1928 caller_save_needed = 0;
1929 FOR_EACH_ALLOCNO (a, ai)
1931 /* There are no caps at this point. */
1932 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
1933 if (! ALLOCNO_ASSIGNED_P (a))
1934 /* It can happen if A is not referenced but partially anticipated
1935 somewhere in a region. */
1936 ALLOCNO_ASSIGNED_P (a) = true;
1937 ira_free_allocno_updated_costs (a);
1938 hard_regno = ALLOCNO_HARD_REGNO (a);
1939 regno = ALLOCNO_REGNO (a);
1940 reg_renumber[regno] = (hard_regno < 0 ? -1 : hard_regno);
1941 if (hard_regno >= 0)
1943 int i, nwords;
1944 enum reg_class pclass;
1945 ira_object_t obj;
1947 pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
1948 nwords = ALLOCNO_NUM_OBJECTS (a);
1949 for (i = 0; i < nwords; i++)
1951 obj = ALLOCNO_OBJECT (a, i);
1952 IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
1953 reg_class_contents[pclass]);
1955 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
1956 && ira_hard_reg_set_intersection_p (hard_regno, ALLOCNO_MODE (a),
1957 call_used_reg_set))
1959 ira_assert (!optimize || flag_caller_saves
1960 || regno >= ira_reg_equiv_len
1961 || ira_reg_equiv_const[regno]
1962 || ira_reg_equiv_invariant_p[regno]);
1963 caller_save_needed = 1;
1969 /* Set up allocno assignment flags for further allocation
1970 improvements. */
1971 static void
1972 setup_allocno_assignment_flags (void)
1974 int hard_regno;
1975 ira_allocno_t a;
1976 ira_allocno_iterator ai;
1978 FOR_EACH_ALLOCNO (a, ai)
1980 if (! ALLOCNO_ASSIGNED_P (a))
1981 /* It can happen if A is not referenced but partially anticipated
1982 somewhere in a region. */
1983 ira_free_allocno_updated_costs (a);
1984 hard_regno = ALLOCNO_HARD_REGNO (a);
1985 /* Don't assign hard registers to allocnos which are destination
1986 of removed store at the end of loop. It has no sense to keep
1987 the same value in different hard registers. It is also
1988 impossible to assign hard registers correctly to such
1989 allocnos because the cost info and info about intersected
1990 calls are incorrect for them. */
1991 ALLOCNO_ASSIGNED_P (a) = (hard_regno >= 0
1992 || ALLOCNO_EMIT_DATA (a)->mem_optimized_dest_p
1993 || (ALLOCNO_MEMORY_COST (a)
1994 - ALLOCNO_CLASS_COST (a)) < 0);
1995 ira_assert
1996 (hard_regno < 0
1997 || ira_hard_reg_in_set_p (hard_regno, ALLOCNO_MODE (a),
1998 reg_class_contents[ALLOCNO_CLASS (a)]));
2002 /* Evaluate overall allocation cost and the costs for using hard
2003 registers and memory for allocnos. */
2004 static void
2005 calculate_allocation_cost (void)
2007 int hard_regno, cost;
2008 ira_allocno_t a;
2009 ira_allocno_iterator ai;
2011 ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
2012 FOR_EACH_ALLOCNO (a, ai)
2014 hard_regno = ALLOCNO_HARD_REGNO (a);
2015 ira_assert (hard_regno < 0
2016 || (ira_hard_reg_in_set_p
2017 (hard_regno, ALLOCNO_MODE (a),
2018 reg_class_contents[ALLOCNO_CLASS (a)])));
2019 if (hard_regno < 0)
2021 cost = ALLOCNO_MEMORY_COST (a);
2022 ira_mem_cost += cost;
2024 else if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
2026 cost = (ALLOCNO_HARD_REG_COSTS (a)
2027 [ira_class_hard_reg_index
2028 [ALLOCNO_CLASS (a)][hard_regno]]);
2029 ira_reg_cost += cost;
2031 else
2033 cost = ALLOCNO_CLASS_COST (a);
2034 ira_reg_cost += cost;
2036 ira_overall_cost += cost;
2039 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2041 fprintf (ira_dump_file,
2042 "+++Costs: overall %d, reg %d, mem %d, ld %d, st %d, move %d\n",
2043 ira_overall_cost, ira_reg_cost, ira_mem_cost,
2044 ira_load_cost, ira_store_cost, ira_shuffle_cost);
2045 fprintf (ira_dump_file, "+++ move loops %d, new jumps %d\n",
2046 ira_move_loops_num, ira_additional_jumps_num);
2051 #ifdef ENABLE_IRA_CHECKING
2052 /* Check the correctness of the allocation. We do need this because
2053 of complicated code to transform more one region internal
2054 representation into one region representation. */
2055 static void
2056 check_allocation (void)
2058 ira_allocno_t a;
2059 int hard_regno, nregs, conflict_nregs;
2060 ira_allocno_iterator ai;
2062 FOR_EACH_ALLOCNO (a, ai)
2064 int n = ALLOCNO_NUM_OBJECTS (a);
2065 int i;
2067 if (ALLOCNO_CAP_MEMBER (a) != NULL
2068 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
2069 continue;
2070 nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
2071 if (nregs == 1)
2072 /* We allocated a single hard register. */
2073 n = 1;
2074 else if (n > 1)
2075 /* We allocated multiple hard registers, and we will test
2076 conflicts in a granularity of single hard regs. */
2077 nregs = 1;
2079 for (i = 0; i < n; i++)
2081 ira_object_t obj = ALLOCNO_OBJECT (a, i);
2082 ira_object_t conflict_obj;
2083 ira_object_conflict_iterator oci;
2084 int this_regno = hard_regno;
2085 if (n > 1)
2087 if (WORDS_BIG_ENDIAN)
2088 this_regno += n - i - 1;
2089 else
2090 this_regno += i;
2092 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2094 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2095 int conflict_hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
2096 if (conflict_hard_regno < 0)
2097 continue;
2099 conflict_nregs
2100 = (hard_regno_nregs
2101 [conflict_hard_regno][ALLOCNO_MODE (conflict_a)]);
2103 if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1
2104 && conflict_nregs == ALLOCNO_NUM_OBJECTS (conflict_a))
2106 if (WORDS_BIG_ENDIAN)
2107 conflict_hard_regno += (ALLOCNO_NUM_OBJECTS (conflict_a)
2108 - OBJECT_SUBWORD (conflict_obj) - 1);
2109 else
2110 conflict_hard_regno += OBJECT_SUBWORD (conflict_obj);
2111 conflict_nregs = 1;
2114 if ((conflict_hard_regno <= this_regno
2115 && this_regno < conflict_hard_regno + conflict_nregs)
2116 || (this_regno <= conflict_hard_regno
2117 && conflict_hard_regno < this_regno + nregs))
2119 fprintf (stderr, "bad allocation for %d and %d\n",
2120 ALLOCNO_REGNO (a), ALLOCNO_REGNO (conflict_a));
2121 gcc_unreachable ();
2127 #endif
2129 /* Fix values of array REG_EQUIV_INIT after live range splitting done
2130 by IRA. */
2131 static void
2132 fix_reg_equiv_init (void)
2134 unsigned int max_regno = max_reg_num ();
2135 int i, new_regno, max;
2136 rtx x, prev, next, insn, set;
2138 if (VEC_length (reg_equivs_t, reg_equivs) < max_regno)
2140 max = VEC_length (reg_equivs_t, reg_equivs);
2141 grow_reg_equivs ();
2142 for (i = FIRST_PSEUDO_REGISTER; i < max; i++)
2143 for (prev = NULL_RTX, x = reg_equiv_init (i);
2144 x != NULL_RTX;
2145 x = next)
2147 next = XEXP (x, 1);
2148 insn = XEXP (x, 0);
2149 set = single_set (insn);
2150 ira_assert (set != NULL_RTX
2151 && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))));
2152 if (REG_P (SET_DEST (set))
2153 && ((int) REGNO (SET_DEST (set)) == i
2154 || (int) ORIGINAL_REGNO (SET_DEST (set)) == i))
2155 new_regno = REGNO (SET_DEST (set));
2156 else if (REG_P (SET_SRC (set))
2157 && ((int) REGNO (SET_SRC (set)) == i
2158 || (int) ORIGINAL_REGNO (SET_SRC (set)) == i))
2159 new_regno = REGNO (SET_SRC (set));
2160 else
2161 gcc_unreachable ();
2162 if (new_regno == i)
2163 prev = x;
2164 else
2166 if (prev == NULL_RTX)
2167 reg_equiv_init (i) = next;
2168 else
2169 XEXP (prev, 1) = next;
2170 XEXP (x, 1) = reg_equiv_init (new_regno);
2171 reg_equiv_init (new_regno) = x;
2177 #ifdef ENABLE_IRA_CHECKING
2178 /* Print redundant memory-memory copies. */
2179 static void
2180 print_redundant_copies (void)
2182 int hard_regno;
2183 ira_allocno_t a;
2184 ira_copy_t cp, next_cp;
2185 ira_allocno_iterator ai;
2187 FOR_EACH_ALLOCNO (a, ai)
2189 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2190 /* It is a cap. */
2191 continue;
2192 hard_regno = ALLOCNO_HARD_REGNO (a);
2193 if (hard_regno >= 0)
2194 continue;
2195 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2196 if (cp->first == a)
2197 next_cp = cp->next_first_allocno_copy;
2198 else
2200 next_cp = cp->next_second_allocno_copy;
2201 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL
2202 && cp->insn != NULL_RTX
2203 && ALLOCNO_HARD_REGNO (cp->first) == hard_regno)
2204 fprintf (ira_dump_file,
2205 " Redundant move from %d(freq %d):%d\n",
2206 INSN_UID (cp->insn), cp->freq, hard_regno);
2210 #endif
2212 /* Setup preferred and alternative classes for new pseudo-registers
2213 created by IRA starting with START. */
2214 static void
2215 setup_preferred_alternate_classes_for_new_pseudos (int start)
2217 int i, old_regno;
2218 int max_regno = max_reg_num ();
2220 for (i = start; i < max_regno; i++)
2222 old_regno = ORIGINAL_REGNO (regno_reg_rtx[i]);
2223 ira_assert (i != old_regno);
2224 setup_reg_classes (i, reg_preferred_class (old_regno),
2225 reg_alternate_class (old_regno),
2226 reg_allocno_class (old_regno));
2227 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2228 fprintf (ira_dump_file,
2229 " New r%d: setting preferred %s, alternative %s\n",
2230 i, reg_class_names[reg_preferred_class (old_regno)],
2231 reg_class_names[reg_alternate_class (old_regno)]);
2237 /* Regional allocation can create new pseudo-registers. This function
2238 expands some arrays for pseudo-registers. */
2239 static void
2240 expand_reg_info (int old_size)
2242 int i;
2243 int size = max_reg_num ();
2245 resize_reg_info ();
2246 for (i = old_size; i < size; i++)
2247 setup_reg_classes (i, GENERAL_REGS, ALL_REGS, GENERAL_REGS);
2250 /* Return TRUE if there is too high register pressure in the function.
2251 It is used to decide when stack slot sharing is worth to do. */
2252 static bool
2253 too_high_register_pressure_p (void)
2255 int i;
2256 enum reg_class pclass;
2258 for (i = 0; i < ira_pressure_classes_num; i++)
2260 pclass = ira_pressure_classes[i];
2261 if (ira_loop_tree_root->reg_pressure[pclass] > 10000)
2262 return true;
2264 return false;
2269 /* Indicate that hard register number FROM was eliminated and replaced with
2270 an offset from hard register number TO. The status of hard registers live
2271 at the start of a basic block is updated by replacing a use of FROM with
2272 a use of TO. */
2274 void
2275 mark_elimination (int from, int to)
2277 basic_block bb;
2279 FOR_EACH_BB (bb)
2281 /* We don't use LIVE info in IRA. */
2282 bitmap r = DF_LR_IN (bb);
2284 if (REGNO_REG_SET_P (r, from))
2286 CLEAR_REGNO_REG_SET (r, from);
2287 SET_REGNO_REG_SET (r, to);
2294 struct equivalence
2296 /* Set when a REG_EQUIV note is found or created. Use to
2297 keep track of what memory accesses might be created later,
2298 e.g. by reload. */
2299 rtx replacement;
2300 rtx *src_p;
2301 /* The list of each instruction which initializes this register. */
2302 rtx init_insns;
2303 /* Loop depth is used to recognize equivalences which appear
2304 to be present within the same loop (or in an inner loop). */
2305 int loop_depth;
2306 /* Nonzero if this had a preexisting REG_EQUIV note. */
2307 int is_arg_equivalence;
2308 /* Set when an attempt should be made to replace a register
2309 with the associated src_p entry. */
2310 char replace;
2313 /* reg_equiv[N] (where N is a pseudo reg number) is the equivalence
2314 structure for that register. */
2315 static struct equivalence *reg_equiv;
2317 /* Used for communication between the following two functions: contains
2318 a MEM that we wish to ensure remains unchanged. */
2319 static rtx equiv_mem;
2321 /* Set nonzero if EQUIV_MEM is modified. */
2322 static int equiv_mem_modified;
2324 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
2325 Called via note_stores. */
2326 static void
2327 validate_equiv_mem_from_store (rtx dest, const_rtx set ATTRIBUTE_UNUSED,
2328 void *data ATTRIBUTE_UNUSED)
2330 if ((REG_P (dest)
2331 && reg_overlap_mentioned_p (dest, equiv_mem))
2332 || (MEM_P (dest)
2333 && true_dependence (dest, VOIDmode, equiv_mem, rtx_varies_p)))
2334 equiv_mem_modified = 1;
2337 /* Verify that no store between START and the death of REG invalidates
2338 MEMREF. MEMREF is invalidated by modifying a register used in MEMREF,
2339 by storing into an overlapping memory location, or with a non-const
2340 CALL_INSN.
2342 Return 1 if MEMREF remains valid. */
2343 static int
2344 validate_equiv_mem (rtx start, rtx reg, rtx memref)
2346 rtx insn;
2347 rtx note;
2349 equiv_mem = memref;
2350 equiv_mem_modified = 0;
2352 /* If the memory reference has side effects or is volatile, it isn't a
2353 valid equivalence. */
2354 if (side_effects_p (memref))
2355 return 0;
2357 for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
2359 if (! INSN_P (insn))
2360 continue;
2362 if (find_reg_note (insn, REG_DEAD, reg))
2363 return 1;
2365 /* This used to ignore readonly memory and const/pure calls. The problem
2366 is the equivalent form may reference a pseudo which gets assigned a
2367 call clobbered hard reg. When we later replace REG with its
2368 equivalent form, the value in the call-clobbered reg has been
2369 changed and all hell breaks loose. */
2370 if (CALL_P (insn))
2371 return 0;
2373 note_stores (PATTERN (insn), validate_equiv_mem_from_store, NULL);
2375 /* If a register mentioned in MEMREF is modified via an
2376 auto-increment, we lose the equivalence. Do the same if one
2377 dies; although we could extend the life, it doesn't seem worth
2378 the trouble. */
2380 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2381 if ((REG_NOTE_KIND (note) == REG_INC
2382 || REG_NOTE_KIND (note) == REG_DEAD)
2383 && REG_P (XEXP (note, 0))
2384 && reg_overlap_mentioned_p (XEXP (note, 0), memref))
2385 return 0;
2388 return 0;
2391 /* Returns zero if X is known to be invariant. */
2392 static int
2393 equiv_init_varies_p (rtx x)
2395 RTX_CODE code = GET_CODE (x);
2396 int i;
2397 const char *fmt;
2399 switch (code)
2401 case MEM:
2402 return !MEM_READONLY_P (x) || equiv_init_varies_p (XEXP (x, 0));
2404 case CONST:
2405 case CONST_INT:
2406 case CONST_DOUBLE:
2407 case CONST_FIXED:
2408 case CONST_VECTOR:
2409 case SYMBOL_REF:
2410 case LABEL_REF:
2411 return 0;
2413 case REG:
2414 return reg_equiv[REGNO (x)].replace == 0 && rtx_varies_p (x, 0);
2416 case ASM_OPERANDS:
2417 if (MEM_VOLATILE_P (x))
2418 return 1;
2420 /* Fall through. */
2422 default:
2423 break;
2426 fmt = GET_RTX_FORMAT (code);
2427 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2428 if (fmt[i] == 'e')
2430 if (equiv_init_varies_p (XEXP (x, i)))
2431 return 1;
2433 else if (fmt[i] == 'E')
2435 int j;
2436 for (j = 0; j < XVECLEN (x, i); j++)
2437 if (equiv_init_varies_p (XVECEXP (x, i, j)))
2438 return 1;
2441 return 0;
2444 /* Returns nonzero if X (used to initialize register REGNO) is movable.
2445 X is only movable if the registers it uses have equivalent initializations
2446 which appear to be within the same loop (or in an inner loop) and movable
2447 or if they are not candidates for local_alloc and don't vary. */
2448 static int
2449 equiv_init_movable_p (rtx x, int regno)
2451 int i, j;
2452 const char *fmt;
2453 enum rtx_code code = GET_CODE (x);
2455 switch (code)
2457 case SET:
2458 return equiv_init_movable_p (SET_SRC (x), regno);
2460 case CC0:
2461 case CLOBBER:
2462 return 0;
2464 case PRE_INC:
2465 case PRE_DEC:
2466 case POST_INC:
2467 case POST_DEC:
2468 case PRE_MODIFY:
2469 case POST_MODIFY:
2470 return 0;
2472 case REG:
2473 return ((reg_equiv[REGNO (x)].loop_depth >= reg_equiv[regno].loop_depth
2474 && reg_equiv[REGNO (x)].replace)
2475 || (REG_BASIC_BLOCK (REGNO (x)) < NUM_FIXED_BLOCKS
2476 && ! rtx_varies_p (x, 0)));
2478 case UNSPEC_VOLATILE:
2479 return 0;
2481 case ASM_OPERANDS:
2482 if (MEM_VOLATILE_P (x))
2483 return 0;
2485 /* Fall through. */
2487 default:
2488 break;
2491 fmt = GET_RTX_FORMAT (code);
2492 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2493 switch (fmt[i])
2495 case 'e':
2496 if (! equiv_init_movable_p (XEXP (x, i), regno))
2497 return 0;
2498 break;
2499 case 'E':
2500 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2501 if (! equiv_init_movable_p (XVECEXP (x, i, j), regno))
2502 return 0;
2503 break;
2506 return 1;
2509 /* TRUE if X uses any registers for which reg_equiv[REGNO].replace is
2510 true. */
2511 static int
2512 contains_replace_regs (rtx x)
2514 int i, j;
2515 const char *fmt;
2516 enum rtx_code code = GET_CODE (x);
2518 switch (code)
2520 case CONST_INT:
2521 case CONST:
2522 case LABEL_REF:
2523 case SYMBOL_REF:
2524 case CONST_DOUBLE:
2525 case CONST_FIXED:
2526 case CONST_VECTOR:
2527 case PC:
2528 case CC0:
2529 case HIGH:
2530 return 0;
2532 case REG:
2533 return reg_equiv[REGNO (x)].replace;
2535 default:
2536 break;
2539 fmt = GET_RTX_FORMAT (code);
2540 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2541 switch (fmt[i])
2543 case 'e':
2544 if (contains_replace_regs (XEXP (x, i)))
2545 return 1;
2546 break;
2547 case 'E':
2548 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2549 if (contains_replace_regs (XVECEXP (x, i, j)))
2550 return 1;
2551 break;
2554 return 0;
2557 /* TRUE if X references a memory location that would be affected by a store
2558 to MEMREF. */
2559 static int
2560 memref_referenced_p (rtx memref, rtx x)
2562 int i, j;
2563 const char *fmt;
2564 enum rtx_code code = GET_CODE (x);
2566 switch (code)
2568 case CONST_INT:
2569 case CONST:
2570 case LABEL_REF:
2571 case SYMBOL_REF:
2572 case CONST_DOUBLE:
2573 case CONST_FIXED:
2574 case CONST_VECTOR:
2575 case PC:
2576 case CC0:
2577 case HIGH:
2578 case LO_SUM:
2579 return 0;
2581 case REG:
2582 return (reg_equiv[REGNO (x)].replacement
2583 && memref_referenced_p (memref,
2584 reg_equiv[REGNO (x)].replacement));
2586 case MEM:
2587 if (true_dependence (memref, VOIDmode, x, rtx_varies_p))
2588 return 1;
2589 break;
2591 case SET:
2592 /* If we are setting a MEM, it doesn't count (its address does), but any
2593 other SET_DEST that has a MEM in it is referencing the MEM. */
2594 if (MEM_P (SET_DEST (x)))
2596 if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
2597 return 1;
2599 else if (memref_referenced_p (memref, SET_DEST (x)))
2600 return 1;
2602 return memref_referenced_p (memref, SET_SRC (x));
2604 default:
2605 break;
2608 fmt = GET_RTX_FORMAT (code);
2609 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2610 switch (fmt[i])
2612 case 'e':
2613 if (memref_referenced_p (memref, XEXP (x, i)))
2614 return 1;
2615 break;
2616 case 'E':
2617 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2618 if (memref_referenced_p (memref, XVECEXP (x, i, j)))
2619 return 1;
2620 break;
2623 return 0;
2626 /* TRUE if some insn in the range (START, END] references a memory location
2627 that would be affected by a store to MEMREF. */
2628 static int
2629 memref_used_between_p (rtx memref, rtx start, rtx end)
2631 rtx insn;
2633 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2634 insn = NEXT_INSN (insn))
2636 if (!NONDEBUG_INSN_P (insn))
2637 continue;
2639 if (memref_referenced_p (memref, PATTERN (insn)))
2640 return 1;
2642 /* Nonconst functions may access memory. */
2643 if (CALL_P (insn) && (! RTL_CONST_CALL_P (insn)))
2644 return 1;
2647 return 0;
2650 /* Mark REG as having no known equivalence.
2651 Some instructions might have been processed before and furnished
2652 with REG_EQUIV notes for this register; these notes will have to be
2653 removed.
2654 STORE is the piece of RTL that does the non-constant / conflicting
2655 assignment - a SET, CLOBBER or REG_INC note. It is currently not used,
2656 but needs to be there because this function is called from note_stores. */
2657 static void
2658 no_equiv (rtx reg, const_rtx store ATTRIBUTE_UNUSED,
2659 void *data ATTRIBUTE_UNUSED)
2661 int regno;
2662 rtx list;
2664 if (!REG_P (reg))
2665 return;
2666 regno = REGNO (reg);
2667 list = reg_equiv[regno].init_insns;
2668 if (list == const0_rtx)
2669 return;
2670 reg_equiv[regno].init_insns = const0_rtx;
2671 reg_equiv[regno].replacement = NULL_RTX;
2672 /* This doesn't matter for equivalences made for argument registers, we
2673 should keep their initialization insns. */
2674 if (reg_equiv[regno].is_arg_equivalence)
2675 return;
2676 reg_equiv_init (regno) = NULL_RTX;
2677 for (; list; list = XEXP (list, 1))
2679 rtx insn = XEXP (list, 0);
2680 remove_note (insn, find_reg_note (insn, REG_EQUIV, NULL_RTX));
2684 /* In DEBUG_INSN location adjust REGs from CLEARED_REGS bitmap to the
2685 equivalent replacement. */
2687 static rtx
2688 adjust_cleared_regs (rtx loc, const_rtx old_rtx ATTRIBUTE_UNUSED, void *data)
2690 if (REG_P (loc))
2692 bitmap cleared_regs = (bitmap) data;
2693 if (bitmap_bit_p (cleared_regs, REGNO (loc)))
2694 return simplify_replace_fn_rtx (*reg_equiv[REGNO (loc)].src_p,
2695 NULL_RTX, adjust_cleared_regs, data);
2697 return NULL_RTX;
2700 /* Nonzero if we recorded an equivalence for a LABEL_REF. */
2701 static int recorded_label_ref;
2703 /* Find registers that are equivalent to a single value throughout the
2704 compilation (either because they can be referenced in memory or are
2705 set once from a single constant). Lower their priority for a
2706 register.
2708 If such a register is only referenced once, try substituting its
2709 value into the using insn. If it succeeds, we can eliminate the
2710 register completely.
2712 Initialize the REG_EQUIV_INIT array of initializing insns.
2714 Return non-zero if jump label rebuilding should be done. */
2715 static int
2716 update_equiv_regs (void)
2718 rtx insn;
2719 basic_block bb;
2720 int loop_depth;
2721 bitmap cleared_regs;
2723 /* We need to keep track of whether or not we recorded a LABEL_REF so
2724 that we know if the jump optimizer needs to be rerun. */
2725 recorded_label_ref = 0;
2727 reg_equiv = XCNEWVEC (struct equivalence, max_regno);
2728 grow_reg_equivs ();
2730 init_alias_analysis ();
2732 /* Scan the insns and find which registers have equivalences. Do this
2733 in a separate scan of the insns because (due to -fcse-follow-jumps)
2734 a register can be set below its use. */
2735 FOR_EACH_BB (bb)
2737 loop_depth = bb->loop_depth;
2739 for (insn = BB_HEAD (bb);
2740 insn != NEXT_INSN (BB_END (bb));
2741 insn = NEXT_INSN (insn))
2743 rtx note;
2744 rtx set;
2745 rtx dest, src;
2746 int regno;
2748 if (! INSN_P (insn))
2749 continue;
2751 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2752 if (REG_NOTE_KIND (note) == REG_INC)
2753 no_equiv (XEXP (note, 0), note, NULL);
2755 set = single_set (insn);
2757 /* If this insn contains more (or less) than a single SET,
2758 only mark all destinations as having no known equivalence. */
2759 if (set == 0)
2761 note_stores (PATTERN (insn), no_equiv, NULL);
2762 continue;
2764 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2766 int i;
2768 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
2770 rtx part = XVECEXP (PATTERN (insn), 0, i);
2771 if (part != set)
2772 note_stores (part, no_equiv, NULL);
2776 dest = SET_DEST (set);
2777 src = SET_SRC (set);
2779 /* See if this is setting up the equivalence between an argument
2780 register and its stack slot. */
2781 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
2782 if (note)
2784 gcc_assert (REG_P (dest));
2785 regno = REGNO (dest);
2787 /* Note that we don't want to clear reg_equiv_init even if there
2788 are multiple sets of this register. */
2789 reg_equiv[regno].is_arg_equivalence = 1;
2791 /* Record for reload that this is an equivalencing insn. */
2792 if (rtx_equal_p (src, XEXP (note, 0)))
2793 reg_equiv_init (regno)
2794 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init (regno));
2796 /* Continue normally in case this is a candidate for
2797 replacements. */
2800 if (!optimize)
2801 continue;
2803 /* We only handle the case of a pseudo register being set
2804 once, or always to the same value. */
2805 /* ??? The mn10200 port breaks if we add equivalences for
2806 values that need an ADDRESS_REGS register and set them equivalent
2807 to a MEM of a pseudo. The actual problem is in the over-conservative
2808 handling of INPADDR_ADDRESS / INPUT_ADDRESS / INPUT triples in
2809 calculate_needs, but we traditionally work around this problem
2810 here by rejecting equivalences when the destination is in a register
2811 that's likely spilled. This is fragile, of course, since the
2812 preferred class of a pseudo depends on all instructions that set
2813 or use it. */
2815 if (!REG_P (dest)
2816 || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
2817 || reg_equiv[regno].init_insns == const0_rtx
2818 || (targetm.class_likely_spilled_p (reg_preferred_class (regno))
2819 && MEM_P (src) && ! reg_equiv[regno].is_arg_equivalence))
2821 /* This might be setting a SUBREG of a pseudo, a pseudo that is
2822 also set somewhere else to a constant. */
2823 note_stores (set, no_equiv, NULL);
2824 continue;
2827 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
2829 /* cse sometimes generates function invariants, but doesn't put a
2830 REG_EQUAL note on the insn. Since this note would be redundant,
2831 there's no point creating it earlier than here. */
2832 if (! note && ! rtx_varies_p (src, 0))
2833 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
2835 /* Don't bother considering a REG_EQUAL note containing an EXPR_LIST
2836 since it represents a function call */
2837 if (note && GET_CODE (XEXP (note, 0)) == EXPR_LIST)
2838 note = NULL_RTX;
2840 if (DF_REG_DEF_COUNT (regno) != 1
2841 && (! note
2842 || rtx_varies_p (XEXP (note, 0), 0)
2843 || (reg_equiv[regno].replacement
2844 && ! rtx_equal_p (XEXP (note, 0),
2845 reg_equiv[regno].replacement))))
2847 no_equiv (dest, set, NULL);
2848 continue;
2850 /* Record this insn as initializing this register. */
2851 reg_equiv[regno].init_insns
2852 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv[regno].init_insns);
2854 /* If this register is known to be equal to a constant, record that
2855 it is always equivalent to the constant. */
2856 if (DF_REG_DEF_COUNT (regno) == 1
2857 && note && ! rtx_varies_p (XEXP (note, 0), 0))
2859 rtx note_value = XEXP (note, 0);
2860 remove_note (insn, note);
2861 set_unique_reg_note (insn, REG_EQUIV, note_value);
2864 /* If this insn introduces a "constant" register, decrease the priority
2865 of that register. Record this insn if the register is only used once
2866 more and the equivalence value is the same as our source.
2868 The latter condition is checked for two reasons: First, it is an
2869 indication that it may be more efficient to actually emit the insn
2870 as written (if no registers are available, reload will substitute
2871 the equivalence). Secondly, it avoids problems with any registers
2872 dying in this insn whose death notes would be missed.
2874 If we don't have a REG_EQUIV note, see if this insn is loading
2875 a register used only in one basic block from a MEM. If so, and the
2876 MEM remains unchanged for the life of the register, add a REG_EQUIV
2877 note. */
2879 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
2881 if (note == 0 && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
2882 && MEM_P (SET_SRC (set))
2883 && validate_equiv_mem (insn, dest, SET_SRC (set)))
2884 note = set_unique_reg_note (insn, REG_EQUIV, copy_rtx (SET_SRC (set)));
2886 if (note)
2888 int regno = REGNO (dest);
2889 rtx x = XEXP (note, 0);
2891 /* If we haven't done so, record for reload that this is an
2892 equivalencing insn. */
2893 if (!reg_equiv[regno].is_arg_equivalence)
2894 reg_equiv_init (regno)
2895 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init (regno));
2897 /* Record whether or not we created a REG_EQUIV note for a LABEL_REF.
2898 We might end up substituting the LABEL_REF for uses of the
2899 pseudo here or later. That kind of transformation may turn an
2900 indirect jump into a direct jump, in which case we must rerun the
2901 jump optimizer to ensure that the JUMP_LABEL fields are valid. */
2902 if (GET_CODE (x) == LABEL_REF
2903 || (GET_CODE (x) == CONST
2904 && GET_CODE (XEXP (x, 0)) == PLUS
2905 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF)))
2906 recorded_label_ref = 1;
2908 reg_equiv[regno].replacement = x;
2909 reg_equiv[regno].src_p = &SET_SRC (set);
2910 reg_equiv[regno].loop_depth = loop_depth;
2912 /* Don't mess with things live during setjmp. */
2913 if (REG_LIVE_LENGTH (regno) >= 0 && optimize)
2915 /* Note that the statement below does not affect the priority
2916 in local-alloc! */
2917 REG_LIVE_LENGTH (regno) *= 2;
2919 /* If the register is referenced exactly twice, meaning it is
2920 set once and used once, indicate that the reference may be
2921 replaced by the equivalence we computed above. Do this
2922 even if the register is only used in one block so that
2923 dependencies can be handled where the last register is
2924 used in a different block (i.e. HIGH / LO_SUM sequences)
2925 and to reduce the number of registers alive across
2926 calls. */
2928 if (REG_N_REFS (regno) == 2
2929 && (rtx_equal_p (x, src)
2930 || ! equiv_init_varies_p (src))
2931 && NONJUMP_INSN_P (insn)
2932 && equiv_init_movable_p (PATTERN (insn), regno))
2933 reg_equiv[regno].replace = 1;
2939 if (!optimize)
2940 goto out;
2942 /* A second pass, to gather additional equivalences with memory. This needs
2943 to be done after we know which registers we are going to replace. */
2945 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2947 rtx set, src, dest;
2948 unsigned regno;
2950 if (! INSN_P (insn))
2951 continue;
2953 set = single_set (insn);
2954 if (! set)
2955 continue;
2957 dest = SET_DEST (set);
2958 src = SET_SRC (set);
2960 /* If this sets a MEM to the contents of a REG that is only used
2961 in a single basic block, see if the register is always equivalent
2962 to that memory location and if moving the store from INSN to the
2963 insn that set REG is safe. If so, put a REG_EQUIV note on the
2964 initializing insn.
2966 Don't add a REG_EQUIV note if the insn already has one. The existing
2967 REG_EQUIV is likely more useful than the one we are adding.
2969 If one of the regs in the address has reg_equiv[REGNO].replace set,
2970 then we can't add this REG_EQUIV note. The reg_equiv[REGNO].replace
2971 optimization may move the set of this register immediately before
2972 insn, which puts it after reg_equiv[REGNO].init_insns, and hence
2973 the mention in the REG_EQUIV note would be to an uninitialized
2974 pseudo. */
2976 if (MEM_P (dest) && REG_P (src)
2977 && (regno = REGNO (src)) >= FIRST_PSEUDO_REGISTER
2978 && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
2979 && DF_REG_DEF_COUNT (regno) == 1
2980 && reg_equiv[regno].init_insns != 0
2981 && reg_equiv[regno].init_insns != const0_rtx
2982 && ! find_reg_note (XEXP (reg_equiv[regno].init_insns, 0),
2983 REG_EQUIV, NULL_RTX)
2984 && ! contains_replace_regs (XEXP (dest, 0)))
2986 rtx init_insn = XEXP (reg_equiv[regno].init_insns, 0);
2987 if (validate_equiv_mem (init_insn, src, dest)
2988 && ! memref_used_between_p (dest, init_insn, insn)
2989 /* Attaching a REG_EQUIV note will fail if INIT_INSN has
2990 multiple sets. */
2991 && set_unique_reg_note (init_insn, REG_EQUIV, copy_rtx (dest)))
2993 /* This insn makes the equivalence, not the one initializing
2994 the register. */
2995 reg_equiv_init (regno)
2996 = gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
2997 df_notes_rescan (init_insn);
3002 cleared_regs = BITMAP_ALLOC (NULL);
3003 /* Now scan all regs killed in an insn to see if any of them are
3004 registers only used that once. If so, see if we can replace the
3005 reference with the equivalent form. If we can, delete the
3006 initializing reference and this register will go away. If we
3007 can't replace the reference, and the initializing reference is
3008 within the same loop (or in an inner loop), then move the register
3009 initialization just before the use, so that they are in the same
3010 basic block. */
3011 FOR_EACH_BB_REVERSE (bb)
3013 loop_depth = bb->loop_depth;
3014 for (insn = BB_END (bb);
3015 insn != PREV_INSN (BB_HEAD (bb));
3016 insn = PREV_INSN (insn))
3018 rtx link;
3020 if (! INSN_P (insn))
3021 continue;
3023 /* Don't substitute into a non-local goto, this confuses CFG. */
3024 if (JUMP_P (insn)
3025 && find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
3026 continue;
3028 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
3030 if (REG_NOTE_KIND (link) == REG_DEAD
3031 /* Make sure this insn still refers to the register. */
3032 && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
3034 int regno = REGNO (XEXP (link, 0));
3035 rtx equiv_insn;
3037 if (! reg_equiv[regno].replace
3038 || reg_equiv[regno].loop_depth < loop_depth
3039 /* There is no sense to move insns if we did
3040 register pressure-sensitive scheduling was
3041 done because it will not improve allocation
3042 but worsen insn schedule with a big
3043 probability. */
3044 || (flag_sched_pressure && flag_schedule_insns))
3045 continue;
3047 /* reg_equiv[REGNO].replace gets set only when
3048 REG_N_REFS[REGNO] is 2, i.e. the register is set
3049 once and used once. (If it were only set, but not used,
3050 flow would have deleted the setting insns.) Hence
3051 there can only be one insn in reg_equiv[REGNO].init_insns. */
3052 gcc_assert (reg_equiv[regno].init_insns
3053 && !XEXP (reg_equiv[regno].init_insns, 1));
3054 equiv_insn = XEXP (reg_equiv[regno].init_insns, 0);
3056 /* We may not move instructions that can throw, since
3057 that changes basic block boundaries and we are not
3058 prepared to adjust the CFG to match. */
3059 if (can_throw_internal (equiv_insn))
3060 continue;
3062 if (asm_noperands (PATTERN (equiv_insn)) < 0
3063 && validate_replace_rtx (regno_reg_rtx[regno],
3064 *(reg_equiv[regno].src_p), insn))
3066 rtx equiv_link;
3067 rtx last_link;
3068 rtx note;
3070 /* Find the last note. */
3071 for (last_link = link; XEXP (last_link, 1);
3072 last_link = XEXP (last_link, 1))
3075 /* Append the REG_DEAD notes from equiv_insn. */
3076 equiv_link = REG_NOTES (equiv_insn);
3077 while (equiv_link)
3079 note = equiv_link;
3080 equiv_link = XEXP (equiv_link, 1);
3081 if (REG_NOTE_KIND (note) == REG_DEAD)
3083 remove_note (equiv_insn, note);
3084 XEXP (last_link, 1) = note;
3085 XEXP (note, 1) = NULL_RTX;
3086 last_link = note;
3090 remove_death (regno, insn);
3091 SET_REG_N_REFS (regno, 0);
3092 REG_FREQ (regno) = 0;
3093 delete_insn (equiv_insn);
3095 reg_equiv[regno].init_insns
3096 = XEXP (reg_equiv[regno].init_insns, 1);
3098 reg_equiv_init (regno) = NULL_RTX;
3099 bitmap_set_bit (cleared_regs, regno);
3101 /* Move the initialization of the register to just before
3102 INSN. Update the flow information. */
3103 else if (prev_nondebug_insn (insn) != equiv_insn)
3105 rtx new_insn;
3107 new_insn = emit_insn_before (PATTERN (equiv_insn), insn);
3108 REG_NOTES (new_insn) = REG_NOTES (equiv_insn);
3109 REG_NOTES (equiv_insn) = 0;
3110 /* Rescan it to process the notes. */
3111 df_insn_rescan (new_insn);
3113 /* Make sure this insn is recognized before
3114 reload begins, otherwise
3115 eliminate_regs_in_insn will die. */
3116 INSN_CODE (new_insn) = INSN_CODE (equiv_insn);
3118 delete_insn (equiv_insn);
3120 XEXP (reg_equiv[regno].init_insns, 0) = new_insn;
3122 REG_BASIC_BLOCK (regno) = bb->index;
3123 REG_N_CALLS_CROSSED (regno) = 0;
3124 REG_FREQ_CALLS_CROSSED (regno) = 0;
3125 REG_N_THROWING_CALLS_CROSSED (regno) = 0;
3126 REG_LIVE_LENGTH (regno) = 2;
3128 if (insn == BB_HEAD (bb))
3129 BB_HEAD (bb) = PREV_INSN (insn);
3131 reg_equiv_init (regno)
3132 = gen_rtx_INSN_LIST (VOIDmode, new_insn, NULL_RTX);
3133 bitmap_set_bit (cleared_regs, regno);
3140 if (!bitmap_empty_p (cleared_regs))
3142 FOR_EACH_BB (bb)
3144 bitmap_and_compl_into (DF_LIVE_IN (bb), cleared_regs);
3145 bitmap_and_compl_into (DF_LIVE_OUT (bb), cleared_regs);
3146 bitmap_and_compl_into (DF_LR_IN (bb), cleared_regs);
3147 bitmap_and_compl_into (DF_LR_OUT (bb), cleared_regs);
3150 /* Last pass - adjust debug insns referencing cleared regs. */
3151 if (MAY_HAVE_DEBUG_INSNS)
3152 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3153 if (DEBUG_INSN_P (insn))
3155 rtx old_loc = INSN_VAR_LOCATION_LOC (insn);
3156 INSN_VAR_LOCATION_LOC (insn)
3157 = simplify_replace_fn_rtx (old_loc, NULL_RTX,
3158 adjust_cleared_regs,
3159 (void *) cleared_regs);
3160 if (old_loc != INSN_VAR_LOCATION_LOC (insn))
3161 df_insn_rescan (insn);
3165 BITMAP_FREE (cleared_regs);
3167 out:
3168 /* Clean up. */
3170 end_alias_analysis ();
3171 free (reg_equiv);
3172 return recorded_label_ref;
3177 /* Print chain C to FILE. */
3178 static void
3179 print_insn_chain (FILE *file, struct insn_chain *c)
3181 fprintf (file, "insn=%d, ", INSN_UID(c->insn));
3182 bitmap_print (file, &c->live_throughout, "live_throughout: ", ", ");
3183 bitmap_print (file, &c->dead_or_set, "dead_or_set: ", "\n");
3187 /* Print all reload_insn_chains to FILE. */
3188 static void
3189 print_insn_chains (FILE *file)
3191 struct insn_chain *c;
3192 for (c = reload_insn_chain; c ; c = c->next)
3193 print_insn_chain (file, c);
3196 /* Return true if pseudo REGNO should be added to set live_throughout
3197 or dead_or_set of the insn chains for reload consideration. */
3198 static bool
3199 pseudo_for_reload_consideration_p (int regno)
3201 /* Consider spilled pseudos too for IRA because they still have a
3202 chance to get hard-registers in the reload when IRA is used. */
3203 return (reg_renumber[regno] >= 0 || ira_conflicts_p);
3206 /* Init LIVE_SUBREGS[ALLOCNUM] and LIVE_SUBREGS_USED[ALLOCNUM] using
3207 REG to the number of nregs, and INIT_VALUE to get the
3208 initialization. ALLOCNUM need not be the regno of REG. */
3209 static void
3210 init_live_subregs (bool init_value, sbitmap *live_subregs,
3211 int *live_subregs_used, int allocnum, rtx reg)
3213 unsigned int regno = REGNO (SUBREG_REG (reg));
3214 int size = GET_MODE_SIZE (GET_MODE (regno_reg_rtx[regno]));
3216 gcc_assert (size > 0);
3218 /* Been there, done that. */
3219 if (live_subregs_used[allocnum])
3220 return;
3222 /* Create a new one with zeros. */
3223 if (live_subregs[allocnum] == NULL)
3224 live_subregs[allocnum] = sbitmap_alloc (size);
3226 /* If the entire reg was live before blasting into subregs, we need
3227 to init all of the subregs to ones else init to 0. */
3228 if (init_value)
3229 sbitmap_ones (live_subregs[allocnum]);
3230 else
3231 sbitmap_zero (live_subregs[allocnum]);
3233 /* Set the number of bits that we really want. */
3234 live_subregs_used[allocnum] = size;
3237 /* Walk the insns of the current function and build reload_insn_chain,
3238 and record register life information. */
3239 static void
3240 build_insn_chain (void)
3242 unsigned int i;
3243 struct insn_chain **p = &reload_insn_chain;
3244 basic_block bb;
3245 struct insn_chain *c = NULL;
3246 struct insn_chain *next = NULL;
3247 bitmap live_relevant_regs = BITMAP_ALLOC (NULL);
3248 bitmap elim_regset = BITMAP_ALLOC (NULL);
3249 /* live_subregs is a vector used to keep accurate information about
3250 which hardregs are live in multiword pseudos. live_subregs and
3251 live_subregs_used are indexed by pseudo number. The live_subreg
3252 entry for a particular pseudo is only used if the corresponding
3253 element is non zero in live_subregs_used. The value in
3254 live_subregs_used is number of bytes that the pseudo can
3255 occupy. */
3256 sbitmap *live_subregs = XCNEWVEC (sbitmap, max_regno);
3257 int *live_subregs_used = XNEWVEC (int, max_regno);
3259 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3260 if (TEST_HARD_REG_BIT (eliminable_regset, i))
3261 bitmap_set_bit (elim_regset, i);
3262 FOR_EACH_BB_REVERSE (bb)
3264 bitmap_iterator bi;
3265 rtx insn;
3267 CLEAR_REG_SET (live_relevant_regs);
3268 memset (live_subregs_used, 0, max_regno * sizeof (int));
3270 EXECUTE_IF_SET_IN_BITMAP (DF_LR_OUT (bb), 0, i, bi)
3272 if (i >= FIRST_PSEUDO_REGISTER)
3273 break;
3274 bitmap_set_bit (live_relevant_regs, i);
3277 EXECUTE_IF_SET_IN_BITMAP (DF_LR_OUT (bb),
3278 FIRST_PSEUDO_REGISTER, i, bi)
3280 if (pseudo_for_reload_consideration_p (i))
3281 bitmap_set_bit (live_relevant_regs, i);
3284 FOR_BB_INSNS_REVERSE (bb, insn)
3286 if (!NOTE_P (insn) && !BARRIER_P (insn))
3288 unsigned int uid = INSN_UID (insn);
3289 df_ref *def_rec;
3290 df_ref *use_rec;
3292 c = new_insn_chain ();
3293 c->next = next;
3294 next = c;
3295 *p = c;
3296 p = &c->prev;
3298 c->insn = insn;
3299 c->block = bb->index;
3301 if (INSN_P (insn))
3302 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
3304 df_ref def = *def_rec;
3305 unsigned int regno = DF_REF_REGNO (def);
3307 /* Ignore may clobbers because these are generated
3308 from calls. However, every other kind of def is
3309 added to dead_or_set. */
3310 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
3312 if (regno < FIRST_PSEUDO_REGISTER)
3314 if (!fixed_regs[regno])
3315 bitmap_set_bit (&c->dead_or_set, regno);
3317 else if (pseudo_for_reload_consideration_p (regno))
3318 bitmap_set_bit (&c->dead_or_set, regno);
3321 if ((regno < FIRST_PSEUDO_REGISTER
3322 || reg_renumber[regno] >= 0
3323 || ira_conflicts_p)
3324 && (!DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)))
3326 rtx reg = DF_REF_REG (def);
3328 /* We can model subregs, but not if they are
3329 wrapped in ZERO_EXTRACTS. */
3330 if (GET_CODE (reg) == SUBREG
3331 && !DF_REF_FLAGS_IS_SET (def, DF_REF_ZERO_EXTRACT))
3333 unsigned int start = SUBREG_BYTE (reg);
3334 unsigned int last = start
3335 + GET_MODE_SIZE (GET_MODE (reg));
3337 init_live_subregs
3338 (bitmap_bit_p (live_relevant_regs, regno),
3339 live_subregs, live_subregs_used, regno, reg);
3341 if (!DF_REF_FLAGS_IS_SET
3342 (def, DF_REF_STRICT_LOW_PART))
3344 /* Expand the range to cover entire words.
3345 Bytes added here are "don't care". */
3346 start
3347 = start / UNITS_PER_WORD * UNITS_PER_WORD;
3348 last = ((last + UNITS_PER_WORD - 1)
3349 / UNITS_PER_WORD * UNITS_PER_WORD);
3352 /* Ignore the paradoxical bits. */
3353 if ((int)last > live_subregs_used[regno])
3354 last = live_subregs_used[regno];
3356 while (start < last)
3358 RESET_BIT (live_subregs[regno], start);
3359 start++;
3362 if (sbitmap_empty_p (live_subregs[regno]))
3364 live_subregs_used[regno] = 0;
3365 bitmap_clear_bit (live_relevant_regs, regno);
3367 else
3368 /* Set live_relevant_regs here because
3369 that bit has to be true to get us to
3370 look at the live_subregs fields. */
3371 bitmap_set_bit (live_relevant_regs, regno);
3373 else
3375 /* DF_REF_PARTIAL is generated for
3376 subregs, STRICT_LOW_PART, and
3377 ZERO_EXTRACT. We handle the subreg
3378 case above so here we have to keep from
3379 modeling the def as a killing def. */
3380 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL))
3382 bitmap_clear_bit (live_relevant_regs, regno);
3383 live_subregs_used[regno] = 0;
3389 bitmap_and_compl_into (live_relevant_regs, elim_regset);
3390 bitmap_copy (&c->live_throughout, live_relevant_regs);
3392 if (INSN_P (insn))
3393 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
3395 df_ref use = *use_rec;
3396 unsigned int regno = DF_REF_REGNO (use);
3397 rtx reg = DF_REF_REG (use);
3399 /* DF_REF_READ_WRITE on a use means that this use
3400 is fabricated from a def that is a partial set
3401 to a multiword reg. Here, we only model the
3402 subreg case that is not wrapped in ZERO_EXTRACT
3403 precisely so we do not need to look at the
3404 fabricated use. */
3405 if (DF_REF_FLAGS_IS_SET (use, DF_REF_READ_WRITE)
3406 && !DF_REF_FLAGS_IS_SET (use, DF_REF_ZERO_EXTRACT)
3407 && DF_REF_FLAGS_IS_SET (use, DF_REF_SUBREG))
3408 continue;
3410 /* Add the last use of each var to dead_or_set. */
3411 if (!bitmap_bit_p (live_relevant_regs, regno))
3413 if (regno < FIRST_PSEUDO_REGISTER)
3415 if (!fixed_regs[regno])
3416 bitmap_set_bit (&c->dead_or_set, regno);
3418 else if (pseudo_for_reload_consideration_p (regno))
3419 bitmap_set_bit (&c->dead_or_set, regno);
3422 if (regno < FIRST_PSEUDO_REGISTER
3423 || pseudo_for_reload_consideration_p (regno))
3425 if (GET_CODE (reg) == SUBREG
3426 && !DF_REF_FLAGS_IS_SET (use,
3427 DF_REF_SIGN_EXTRACT
3428 | DF_REF_ZERO_EXTRACT))
3430 unsigned int start = SUBREG_BYTE (reg);
3431 unsigned int last = start
3432 + GET_MODE_SIZE (GET_MODE (reg));
3434 init_live_subregs
3435 (bitmap_bit_p (live_relevant_regs, regno),
3436 live_subregs, live_subregs_used, regno, reg);
3438 /* Ignore the paradoxical bits. */
3439 if ((int)last > live_subregs_used[regno])
3440 last = live_subregs_used[regno];
3442 while (start < last)
3444 SET_BIT (live_subregs[regno], start);
3445 start++;
3448 else
3449 /* Resetting the live_subregs_used is
3450 effectively saying do not use the subregs
3451 because we are reading the whole
3452 pseudo. */
3453 live_subregs_used[regno] = 0;
3454 bitmap_set_bit (live_relevant_regs, regno);
3460 /* FIXME!! The following code is a disaster. Reload needs to see the
3461 labels and jump tables that are just hanging out in between
3462 the basic blocks. See pr33676. */
3463 insn = BB_HEAD (bb);
3465 /* Skip over the barriers and cruft. */
3466 while (insn && (BARRIER_P (insn) || NOTE_P (insn)
3467 || BLOCK_FOR_INSN (insn) == bb))
3468 insn = PREV_INSN (insn);
3470 /* While we add anything except barriers and notes, the focus is
3471 to get the labels and jump tables into the
3472 reload_insn_chain. */
3473 while (insn)
3475 if (!NOTE_P (insn) && !BARRIER_P (insn))
3477 if (BLOCK_FOR_INSN (insn))
3478 break;
3480 c = new_insn_chain ();
3481 c->next = next;
3482 next = c;
3483 *p = c;
3484 p = &c->prev;
3486 /* The block makes no sense here, but it is what the old
3487 code did. */
3488 c->block = bb->index;
3489 c->insn = insn;
3490 bitmap_copy (&c->live_throughout, live_relevant_regs);
3492 insn = PREV_INSN (insn);
3496 for (i = 0; i < (unsigned int) max_regno; i++)
3497 free (live_subregs[i]);
3499 reload_insn_chain = c;
3500 *p = NULL;
3502 free (live_subregs);
3503 free (live_subregs_used);
3504 BITMAP_FREE (live_relevant_regs);
3505 BITMAP_FREE (elim_regset);
3507 if (dump_file)
3508 print_insn_chains (dump_file);
3513 /* All natural loops. */
3514 struct loops ira_loops;
3516 /* True if we have allocno conflicts. It is false for non-optimized
3517 mode or when the conflict table is too big. */
3518 bool ira_conflicts_p;
3520 /* This is the main entry of IRA. */
3521 static void
3522 ira (FILE *f)
3524 int overall_cost_before, allocated_reg_info_size;
3525 bool loops_p;
3526 int max_regno_before_ira, ira_max_point_before_emit;
3527 int rebuild_p;
3528 int saved_flag_ira_share_spill_slots;
3529 basic_block bb;
3530 bool need_dce;
3532 timevar_push (TV_IRA);
3534 if (flag_caller_saves)
3535 init_caller_save ();
3537 if (flag_ira_verbose < 10)
3539 internal_flag_ira_verbose = flag_ira_verbose;
3540 ira_dump_file = f;
3542 else
3544 internal_flag_ira_verbose = flag_ira_verbose - 10;
3545 ira_dump_file = stderr;
3548 ira_conflicts_p = optimize > 0;
3549 setup_prohibited_mode_move_regs ();
3551 df_note_add_problem ();
3553 if (optimize == 1)
3555 df_live_add_problem ();
3556 df_live_set_all_dirty ();
3558 #ifdef ENABLE_CHECKING
3559 df->changeable_flags |= DF_VERIFY_SCHEDULED;
3560 #endif
3561 df_analyze ();
3562 df_clear_flags (DF_NO_INSN_RESCAN);
3563 regstat_init_n_sets_and_refs ();
3564 regstat_compute_ri ();
3566 /* If we are not optimizing, then this is the only place before
3567 register allocation where dataflow is done. And that is needed
3568 to generate these warnings. */
3569 if (warn_clobbered)
3570 generate_setjmp_warnings ();
3572 /* Determine if the current function is a leaf before running IRA
3573 since this can impact optimizations done by the prologue and
3574 epilogue thus changing register elimination offsets. */
3575 current_function_is_leaf = leaf_function_p ();
3577 if (resize_reg_info () && flag_ira_loop_pressure)
3578 ira_set_pseudo_classes (ira_dump_file);
3580 rebuild_p = update_equiv_regs ();
3582 #ifndef IRA_NO_OBSTACK
3583 gcc_obstack_init (&ira_obstack);
3584 #endif
3585 bitmap_obstack_initialize (&ira_bitmap_obstack);
3586 if (optimize)
3588 max_regno = max_reg_num ();
3589 ira_reg_equiv_len = max_regno;
3590 ira_reg_equiv_invariant_p
3591 = (bool *) ira_allocate (max_regno * sizeof (bool));
3592 memset (ira_reg_equiv_invariant_p, 0, max_regno * sizeof (bool));
3593 ira_reg_equiv_const = (rtx *) ira_allocate (max_regno * sizeof (rtx));
3594 memset (ira_reg_equiv_const, 0, max_regno * sizeof (rtx));
3595 find_reg_equiv_invariant_const ();
3596 if (rebuild_p)
3598 timevar_push (TV_JUMP);
3599 rebuild_jump_labels (get_insns ());
3600 if (purge_all_dead_edges ())
3601 delete_unreachable_blocks ();
3602 timevar_pop (TV_JUMP);
3606 max_regno_before_ira = allocated_reg_info_size = max_reg_num ();
3607 ira_setup_eliminable_regset ();
3609 ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
3610 ira_load_cost = ira_store_cost = ira_shuffle_cost = 0;
3611 ira_move_loops_num = ira_additional_jumps_num = 0;
3613 ira_assert (current_loops == NULL);
3614 flow_loops_find (&ira_loops);
3615 record_loop_exits ();
3616 current_loops = &ira_loops;
3618 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3619 fprintf (ira_dump_file, "Building IRA IR\n");
3620 loops_p = ira_build (optimize
3621 && (flag_ira_region == IRA_REGION_ALL
3622 || flag_ira_region == IRA_REGION_MIXED));
3624 ira_assert (ira_conflicts_p || !loops_p);
3626 saved_flag_ira_share_spill_slots = flag_ira_share_spill_slots;
3627 if (too_high_register_pressure_p () || cfun->calls_setjmp)
3628 /* It is just wasting compiler's time to pack spilled pseudos into
3629 stack slots in this case -- prohibit it. We also do this if
3630 there is setjmp call because a variable not modified between
3631 setjmp and longjmp the compiler is required to preserve its
3632 value and sharing slots does not guarantee it. */
3633 flag_ira_share_spill_slots = FALSE;
3635 ira_color ();
3637 ira_max_point_before_emit = ira_max_point;
3639 ira_initiate_emit_data ();
3641 ira_emit (loops_p);
3643 if (ira_conflicts_p)
3645 max_regno = max_reg_num ();
3647 if (! loops_p)
3648 ira_initiate_assign ();
3649 else
3651 expand_reg_info (allocated_reg_info_size);
3652 setup_preferred_alternate_classes_for_new_pseudos
3653 (allocated_reg_info_size);
3654 allocated_reg_info_size = max_regno;
3656 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3657 fprintf (ira_dump_file, "Flattening IR\n");
3658 ira_flattening (max_regno_before_ira, ira_max_point_before_emit);
3659 /* New insns were generated: add notes and recalculate live
3660 info. */
3661 df_analyze ();
3663 flow_loops_find (&ira_loops);
3664 record_loop_exits ();
3665 current_loops = &ira_loops;
3667 setup_allocno_assignment_flags ();
3668 ira_initiate_assign ();
3669 ira_reassign_conflict_allocnos (max_regno);
3673 ira_finish_emit_data ();
3675 setup_reg_renumber ();
3677 calculate_allocation_cost ();
3679 #ifdef ENABLE_IRA_CHECKING
3680 if (ira_conflicts_p)
3681 check_allocation ();
3682 #endif
3684 if (delete_trivially_dead_insns (get_insns (), max_reg_num ()))
3685 df_analyze ();
3687 if (max_regno != max_regno_before_ira)
3689 regstat_free_n_sets_and_refs ();
3690 regstat_free_ri ();
3691 regstat_init_n_sets_and_refs ();
3692 regstat_compute_ri ();
3695 overall_cost_before = ira_overall_cost;
3696 if (! ira_conflicts_p)
3697 grow_reg_equivs ();
3698 else
3700 fix_reg_equiv_init ();
3702 #ifdef ENABLE_IRA_CHECKING
3703 print_redundant_copies ();
3704 #endif
3706 ira_spilled_reg_stack_slots_num = 0;
3707 ira_spilled_reg_stack_slots
3708 = ((struct ira_spilled_reg_stack_slot *)
3709 ira_allocate (max_regno
3710 * sizeof (struct ira_spilled_reg_stack_slot)));
3711 memset (ira_spilled_reg_stack_slots, 0,
3712 max_regno * sizeof (struct ira_spilled_reg_stack_slot));
3714 allocate_initial_values (reg_equivs);
3716 timevar_pop (TV_IRA);
3718 timevar_push (TV_RELOAD);
3719 df_set_flags (DF_NO_INSN_RESCAN);
3720 build_insn_chain ();
3722 need_dce = reload (get_insns (), ira_conflicts_p);
3724 timevar_pop (TV_RELOAD);
3726 timevar_push (TV_IRA);
3728 if (ira_conflicts_p)
3730 ira_free (ira_spilled_reg_stack_slots);
3732 ira_finish_assign ();
3735 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL
3736 && overall_cost_before != ira_overall_cost)
3737 fprintf (ira_dump_file, "+++Overall after reload %d\n", ira_overall_cost);
3738 ira_destroy ();
3740 flag_ira_share_spill_slots = saved_flag_ira_share_spill_slots;
3742 flow_loops_free (&ira_loops);
3743 free_dominance_info (CDI_DOMINATORS);
3744 FOR_ALL_BB (bb)
3745 bb->loop_father = NULL;
3746 current_loops = NULL;
3748 regstat_free_ri ();
3749 regstat_free_n_sets_and_refs ();
3751 if (optimize)
3753 cleanup_cfg (CLEANUP_EXPENSIVE);
3755 ira_free (ira_reg_equiv_invariant_p);
3756 ira_free (ira_reg_equiv_const);
3759 bitmap_obstack_release (&ira_bitmap_obstack);
3760 #ifndef IRA_NO_OBSTACK
3761 obstack_free (&ira_obstack, NULL);
3762 #endif
3764 /* The code after the reload has changed so much that at this point
3765 we might as well just rescan everything. Note that
3766 df_rescan_all_insns is not going to help here because it does not
3767 touch the artificial uses and defs. */
3768 df_finish_pass (true);
3769 if (optimize > 1)
3770 df_live_add_problem ();
3771 df_scan_alloc (NULL);
3772 df_scan_blocks ();
3774 if (optimize)
3775 df_analyze ();
3777 if (need_dce && optimize)
3778 run_fast_dce ();
3780 timevar_pop (TV_IRA);
3785 static bool
3786 gate_ira (void)
3788 return true;
3791 /* Run the integrated register allocator. */
3792 static unsigned int
3793 rest_of_handle_ira (void)
3795 ira (dump_file);
3796 return 0;
3799 struct rtl_opt_pass pass_ira =
3802 RTL_PASS,
3803 "ira", /* name */
3804 gate_ira, /* gate */
3805 rest_of_handle_ira, /* execute */
3806 NULL, /* sub */
3807 NULL, /* next */
3808 0, /* static_pass_number */
3809 TV_NONE, /* tv_id */
3810 0, /* properties_required */
3811 0, /* properties_provided */
3812 0, /* properties_destroyed */
3813 0, /* todo_flags_start */
3814 TODO_ggc_collect /* todo_flags_finish */