init.c (sort_mem_initializers): Rename "field_type" to "ctx".
[official-gcc.git] / gcc / ira.c
blob943b06bd391a1a0b64188a17943fb312800e8faf
1 /* Integrated Register Allocator (IRA) entry point.
2 Copyright (C) 2006, 2007, 2008, 2009, 2010
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 *Cover class* is a register class belonging to a set of
42 non-intersecting register classes containing all of the
43 hard-registers available for register allocation. The set of
44 all cover classes for a target is defined in the corresponding
45 machine-description file according some criteria. Such notion
46 is needed because Chaitin-Briggs algorithm works on
47 non-intersected register classes.
49 o *Allocno* represents the live range of a pseudo-register in a
50 region. Besides the obvious attributes like the corresponding
51 pseudo-register number, cover class, conflicting allocnos and
52 conflicting hard-registers, there are a few allocno attributes
53 which are important for understanding the allocation algorithm:
55 - *Live ranges*. This is a list of ranges of *program
56 points* where the allocno lives. Program points represent
57 places where a pseudo can be born or become dead (there are
58 approximately two times more program points than the insns)
59 and they are represented by integers starting with 0. The
60 live ranges are used to find conflicts between allocnos of
61 different cover classes. They also play very important role
62 for the transformation of the IRA internal representation of
63 several regions into a one region representation. The later is
64 used during the reload pass work because each allocno
65 represents all of the corresponding pseudo-registers.
67 - *Hard-register costs*. This is a vector of size equal to the
68 number of available hard-registers of the allocno's cover
69 class. The cost of a callee-clobbered hard-register for an
70 allocno is increased by the cost of save/restore code around
71 the calls through the given allocno's life. If the allocno
72 is a move instruction operand and another operand is a
73 hard-register of the allocno's cover class, the cost of the
74 hard-register is decreased by the move cost.
76 When an allocno is assigned, the hard-register with minimal
77 full cost is used. Initially, a hard-register's full cost is
78 the corresponding value from the hard-register's cost vector.
79 If the allocno is connected by a *copy* (see below) to
80 another allocno which has just received a hard-register, the
81 cost of the hard-register is decreased. Before choosing a
82 hard-register for an allocno, the allocno's current costs of
83 the hard-registers are modified by the conflict hard-register
84 costs of all of the conflicting allocnos which are not
85 assigned yet.
87 - *Conflict hard-register costs*. This is a vector of the same
88 size as the hard-register costs vector. To permit an
89 unassigned allocno to get a better hard-register, IRA uses
90 this vector to calculate the final full cost of the
91 available hard-registers. Conflict hard-register costs of an
92 unassigned allocno are also changed with a change of the
93 hard-register cost of the allocno when a copy involving the
94 allocno is processed as described above. This is done to
95 show other unassigned allocnos that a given allocno prefers
96 some hard-registers in order to remove the move instruction
97 corresponding to the copy.
99 o *Cap*. If a pseudo-register does not live in a region but
100 lives in a nested region, IRA creates a special allocno called
101 a cap in the outer region. A region cap is also created for a
102 subregion cap.
104 o *Copy*. Allocnos can be connected by copies. Copies are used
105 to modify hard-register costs for allocnos during coloring.
106 Such modifications reflects a preference to use the same
107 hard-register for the allocnos connected by copies. Usually
108 copies are created for move insns (in this case it results in
109 register coalescing). But IRA also creates copies for operands
110 of an insn which should be assigned to the same hard-register
111 due to constraints in the machine description (it usually
112 results in removing a move generated in reload to satisfy
113 the constraints) and copies referring to the allocno which is
114 the output operand of an instruction and the allocno which is
115 an input operand dying in the instruction (creation of such
116 copies results in less register shuffling). IRA *does not*
117 create copies between the same register allocnos from different
118 regions because we use another technique for propagating
119 hard-register preference on the borders of regions.
121 Allocnos (including caps) for the upper region in the region tree
122 *accumulate* information important for coloring from allocnos with
123 the same pseudo-register from nested regions. This includes
124 hard-register and memory costs, conflicts with hard-registers,
125 allocno conflicts, allocno copies and more. *Thus, attributes for
126 allocnos in a region have the same values as if the region had no
127 subregions*. It means that attributes for allocnos in the
128 outermost region corresponding to the function have the same values
129 as though the allocation used only one region which is the entire
130 function. It also means that we can look at IRA work as if the
131 first IRA did allocation for all function then it improved the
132 allocation for loops then their subloops and so on.
134 IRA major passes are:
136 o Building IRA internal representation which consists of the
137 following subpasses:
139 * First, IRA builds regions and creates allocnos (file
140 ira-build.c) and initializes most of their attributes.
142 * Then IRA finds a cover class for each allocno and calculates
143 its initial (non-accumulated) cost of memory and each
144 hard-register of its cover class (file ira-cost.c).
146 * IRA creates live ranges of each allocno, calulates register
147 pressure for each cover class in each region, sets up
148 conflict hard registers for each allocno and info about calls
149 the allocno lives through (file ira-lives.c).
151 * IRA removes low register pressure loops from the regions
152 mostly to speed IRA up (file ira-build.c).
154 * IRA propagates accumulated allocno info from lower region
155 allocnos to corresponding upper region allocnos (file
156 ira-build.c).
158 * IRA creates all caps (file ira-build.c).
160 * Having live-ranges of allocnos and their cover classes, IRA
161 creates conflicting allocnos of the same cover class for each
162 allocno. Conflicting allocnos are stored as a bit vector or
163 array of pointers to the conflicting allocnos whatever is
164 more profitable (file ira-conflicts.c). At this point IRA
165 creates allocno copies.
167 o Coloring. Now IRA has all necessary info to start graph coloring
168 process. It is done in each region on top-down traverse of the
169 region tree (file ira-color.c). There are following subpasses:
171 * Optional aggressive coalescing of allocnos in the region.
173 * Putting allocnos onto the coloring stack. IRA uses Briggs
174 optimistic coloring which is a major improvement over
175 Chaitin's coloring. Therefore IRA does not spill allocnos at
176 this point. There is some freedom in the order of putting
177 allocnos on the stack which can affect the final result of
178 the allocation. IRA uses some heuristics to improve the order.
180 * Popping the allocnos from the stack and assigning them hard
181 registers. If IRA can not assign a hard register to an
182 allocno and the allocno is coalesced, IRA undoes the
183 coalescing and puts the uncoalesced allocnos onto the stack in
184 the hope that some such allocnos will get a hard register
185 separately. If IRA fails to assign hard register or memory
186 is more profitable for it, IRA spills the allocno. IRA
187 assigns the allocno the hard-register with minimal full
188 allocation cost which reflects the cost of usage of the
189 hard-register for the allocno and cost of usage of the
190 hard-register for allocnos conflicting with given allocno.
192 * After allono assigning in the region, IRA modifies the hard
193 register and memory costs for the corresponding allocnos in
194 the subregions to reflect the cost of possible loads, stores,
195 or moves on the border of the region and its subregions.
196 When default regional allocation algorithm is used
197 (-fira-algorithm=mixed), IRA just propagates the assignment
198 for allocnos if the register pressure in the region for the
199 corresponding cover class is less than number of available
200 hard registers for given cover class.
202 o Spill/restore code moving. When IRA performs an allocation
203 by traversing regions in top-down order, it does not know what
204 happens below in the region tree. Therefore, sometimes IRA
205 misses opportunities to perform a better allocation. A simple
206 optimization tries to improve allocation in a region having
207 subregions and containing in another region. If the
208 corresponding allocnos in the subregion are spilled, it spills
209 the region allocno if it is profitable. The optimization
210 implements a simple iterative algorithm performing profitable
211 transformations while they are still possible. It is fast in
212 practice, so there is no real need for a better time complexity
213 algorithm.
215 o Code change. After coloring, two allocnos representing the same
216 pseudo-register outside and inside a region respectively may be
217 assigned to different locations (hard-registers or memory). In
218 this case IRA creates and uses a new pseudo-register inside the
219 region and adds code to move allocno values on the region's
220 borders. This is done during top-down traversal of the regions
221 (file ira-emit.c). In some complicated cases IRA can create a
222 new allocno to move allocno values (e.g. when a swap of values
223 stored in two hard-registers is needed). At this stage, the
224 new allocno is marked as spilled. IRA still creates the
225 pseudo-register and the moves on the region borders even when
226 both allocnos were assigned to the same hard-register. If the
227 reload pass spills a pseudo-register for some reason, the
228 effect will be smaller because another allocno will still be in
229 the hard-register. In most cases, this is better then spilling
230 both allocnos. If reload does not change the allocation
231 for the two pseudo-registers, the trivial move will be removed
232 by post-reload optimizations. IRA does not generate moves for
233 allocnos assigned to the same hard register when the default
234 regional allocation algorithm is used and the register pressure
235 in the region for the corresponding allocno cover class is less
236 than number of available hard registers for given cover class.
237 IRA also does some optimizations to remove redundant stores and
238 to reduce code duplication on the region borders.
240 o Flattening internal representation. After changing code, IRA
241 transforms its internal representation for several regions into
242 one region representation (file ira-build.c). This process is
243 called IR flattening. Such process is more complicated than IR
244 rebuilding would be, but is much faster.
246 o After IR flattening, IRA tries to assign hard registers to all
247 spilled allocnos. This is impelemented by a simple and fast
248 priority coloring algorithm (see function
249 ira_reassign_conflict_allocnos::ira-color.c). Here new allocnos
250 created during the code change pass can be assigned to hard
251 registers.
253 o At the end IRA calls the reload pass. The reload pass
254 communicates with IRA through several functions in file
255 ira-color.c to improve its decisions in
257 * sharing stack slots for the spilled pseudos based on IRA info
258 about pseudo-register conflicts.
260 * reassigning hard-registers to all spilled pseudos at the end
261 of each reload iteration.
263 * choosing a better hard-register to spill based on IRA info
264 about pseudo-register live ranges and the register pressure
265 in places where the pseudo-register lives.
267 IRA uses a lot of data representing the target processors. These
268 data are initilized in file ira.c.
270 If function has no loops (or the loops are ignored when
271 -fira-algorithm=CB is used), we have classic Chaitin-Briggs
272 coloring (only instead of separate pass of coalescing, we use hard
273 register preferencing). In such case, IRA works much faster
274 because many things are not made (like IR flattening, the
275 spill/restore optimization, and the code change).
277 Literature is worth to read for better understanding the code:
279 o Preston Briggs, Keith D. Cooper, Linda Torczon. Improvements to
280 Graph Coloring Register Allocation.
282 o David Callahan, Brian Koblenz. Register allocation via
283 hierarchical graph coloring.
285 o Keith Cooper, Anshuman Dasgupta, Jason Eckhardt. Revisiting Graph
286 Coloring Register Allocation: A Study of the Chaitin-Briggs and
287 Callahan-Koblenz Algorithms.
289 o Guei-Yuan Lueh, Thomas Gross, and Ali-Reza Adl-Tabatabai. Global
290 Register Allocation Based on Graph Fusion.
292 o Vladimir Makarov. The Integrated Register Allocator for GCC.
294 o Vladimir Makarov. The top-down register allocator for irregular
295 register file architectures.
300 #include "config.h"
301 #include "system.h"
302 #include "coretypes.h"
303 #include "tm.h"
304 #include "regs.h"
305 #include "rtl.h"
306 #include "tm_p.h"
307 #include "target.h"
308 #include "flags.h"
309 #include "obstack.h"
310 #include "bitmap.h"
311 #include "hard-reg-set.h"
312 #include "basic-block.h"
313 #include "df.h"
314 #include "expr.h"
315 #include "recog.h"
316 #include "params.h"
317 #include "timevar.h"
318 #include "tree-pass.h"
319 #include "output.h"
320 #include "except.h"
321 #include "reload.h"
322 #include "diagnostic-core.h"
323 #include "integrate.h"
324 #include "ggc.h"
325 #include "ira-int.h"
328 struct target_ira default_target_ira;
329 struct target_ira_int default_target_ira_int;
330 #if SWITCHABLE_TARGET
331 struct target_ira *this_target_ira = &default_target_ira;
332 struct target_ira_int *this_target_ira_int = &default_target_ira_int;
333 #endif
335 /* A modified value of flag `-fira-verbose' used internally. */
336 int internal_flag_ira_verbose;
338 /* Dump file of the allocator if it is not NULL. */
339 FILE *ira_dump_file;
341 /* The number of elements in the following array. */
342 int ira_spilled_reg_stack_slots_num;
344 /* The following array contains info about spilled pseudo-registers
345 stack slots used in current function so far. */
346 struct ira_spilled_reg_stack_slot *ira_spilled_reg_stack_slots;
348 /* Correspondingly overall cost of the allocation, cost of the
349 allocnos assigned to hard-registers, cost of the allocnos assigned
350 to memory, cost of loads, stores and register move insns generated
351 for pseudo-register live range splitting (see ira-emit.c). */
352 int ira_overall_cost;
353 int ira_reg_cost, ira_mem_cost;
354 int ira_load_cost, ira_store_cost, ira_shuffle_cost;
355 int ira_move_loops_num, ira_additional_jumps_num;
357 /* All registers that can be eliminated. */
359 HARD_REG_SET eliminable_regset;
361 /* Temporary hard reg set used for a different calculation. */
362 static HARD_REG_SET temp_hard_regset;
366 /* The function sets up the map IRA_REG_MODE_HARD_REGSET. */
367 static void
368 setup_reg_mode_hard_regset (void)
370 int i, m, hard_regno;
372 for (m = 0; m < NUM_MACHINE_MODES; m++)
373 for (hard_regno = 0; hard_regno < FIRST_PSEUDO_REGISTER; hard_regno++)
375 CLEAR_HARD_REG_SET (ira_reg_mode_hard_regset[hard_regno][m]);
376 for (i = hard_regno_nregs[hard_regno][m] - 1; i >= 0; i--)
377 if (hard_regno + i < FIRST_PSEUDO_REGISTER)
378 SET_HARD_REG_BIT (ira_reg_mode_hard_regset[hard_regno][m],
379 hard_regno + i);
384 #define no_unit_alloc_regs \
385 (this_target_ira_int->x_no_unit_alloc_regs)
387 /* The function sets up the three arrays declared above. */
388 static void
389 setup_class_hard_regs (void)
391 int cl, i, hard_regno, n;
392 HARD_REG_SET processed_hard_reg_set;
394 ira_assert (SHRT_MAX >= FIRST_PSEUDO_REGISTER);
395 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
397 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
398 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
399 CLEAR_HARD_REG_SET (processed_hard_reg_set);
400 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
402 ira_non_ordered_class_hard_regs[cl][0] = -1;
403 ira_class_hard_reg_index[cl][0] = -1;
405 for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
407 #ifdef REG_ALLOC_ORDER
408 hard_regno = reg_alloc_order[i];
409 #else
410 hard_regno = i;
411 #endif
412 if (TEST_HARD_REG_BIT (processed_hard_reg_set, hard_regno))
413 continue;
414 SET_HARD_REG_BIT (processed_hard_reg_set, hard_regno);
415 if (! TEST_HARD_REG_BIT (temp_hard_regset, hard_regno))
416 ira_class_hard_reg_index[cl][hard_regno] = -1;
417 else
419 ira_class_hard_reg_index[cl][hard_regno] = n;
420 ira_class_hard_regs[cl][n++] = hard_regno;
423 ira_class_hard_regs_num[cl] = n;
424 for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
425 if (TEST_HARD_REG_BIT (temp_hard_regset, i))
426 ira_non_ordered_class_hard_regs[cl][n++] = i;
427 ira_assert (ira_class_hard_regs_num[cl] == n);
431 /* Set up IRA_AVAILABLE_CLASS_REGS. */
432 static void
433 setup_available_class_regs (void)
435 int i, j;
437 memset (ira_available_class_regs, 0, sizeof (ira_available_class_regs));
438 for (i = 0; i < N_REG_CLASSES; i++)
440 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
441 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
442 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
443 if (TEST_HARD_REG_BIT (temp_hard_regset, j))
444 ira_available_class_regs[i]++;
448 /* Set up global variables defining info about hard registers for the
449 allocation. These depend on USE_HARD_FRAME_P whose TRUE value means
450 that we can use the hard frame pointer for the allocation. */
451 static void
452 setup_alloc_regs (bool use_hard_frame_p)
454 #ifdef ADJUST_REG_ALLOC_ORDER
455 ADJUST_REG_ALLOC_ORDER;
456 #endif
457 COPY_HARD_REG_SET (no_unit_alloc_regs, fixed_reg_set);
458 if (! use_hard_frame_p)
459 SET_HARD_REG_BIT (no_unit_alloc_regs, HARD_FRAME_POINTER_REGNUM);
460 setup_class_hard_regs ();
461 setup_available_class_regs ();
466 /* Set up IRA_MEMORY_MOVE_COST, IRA_REGISTER_MOVE_COST. */
467 static void
468 setup_class_subset_and_memory_move_costs (void)
470 int cl, cl2, mode;
471 HARD_REG_SET temp_hard_regset2;
473 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
474 ira_memory_move_cost[mode][NO_REGS][0]
475 = ira_memory_move_cost[mode][NO_REGS][1] = SHRT_MAX;
476 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
478 if (cl != (int) NO_REGS)
479 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
481 ira_memory_move_cost[mode][cl][0] =
482 memory_move_cost ((enum machine_mode) mode,
483 (enum reg_class) cl, false);
484 ira_memory_move_cost[mode][cl][1] =
485 memory_move_cost ((enum machine_mode) mode,
486 (enum reg_class) cl, true);
487 /* Costs for NO_REGS are used in cost calculation on the
488 1st pass when the preferred register classes are not
489 known yet. In this case we take the best scenario. */
490 if (ira_memory_move_cost[mode][NO_REGS][0]
491 > ira_memory_move_cost[mode][cl][0])
492 ira_memory_move_cost[mode][NO_REGS][0]
493 = ira_memory_move_cost[mode][cl][0];
494 if (ira_memory_move_cost[mode][NO_REGS][1]
495 > ira_memory_move_cost[mode][cl][1])
496 ira_memory_move_cost[mode][NO_REGS][1]
497 = ira_memory_move_cost[mode][cl][1];
499 for (cl2 = (int) N_REG_CLASSES - 1; cl2 >= 0; cl2--)
501 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
502 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
503 COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl2]);
504 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
505 ira_class_subset_p[cl][cl2]
506 = hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2);
513 /* Define the following macro if allocation through malloc if
514 preferable. */
515 #define IRA_NO_OBSTACK
517 #ifndef IRA_NO_OBSTACK
518 /* Obstack used for storing all dynamic data (except bitmaps) of the
519 IRA. */
520 static struct obstack ira_obstack;
521 #endif
523 /* Obstack used for storing all bitmaps of the IRA. */
524 static struct bitmap_obstack ira_bitmap_obstack;
526 /* Allocate memory of size LEN for IRA data. */
527 void *
528 ira_allocate (size_t len)
530 void *res;
532 #ifndef IRA_NO_OBSTACK
533 res = obstack_alloc (&ira_obstack, len);
534 #else
535 res = xmalloc (len);
536 #endif
537 return res;
540 /* Reallocate memory PTR of size LEN for IRA data. */
541 void *
542 ira_reallocate (void *ptr, size_t len)
544 void *res;
546 #ifndef IRA_NO_OBSTACK
547 res = obstack_alloc (&ira_obstack, len);
548 #else
549 res = xrealloc (ptr, len);
550 #endif
551 return res;
554 /* Free memory ADDR allocated for IRA data. */
555 void
556 ira_free (void *addr ATTRIBUTE_UNUSED)
558 #ifndef IRA_NO_OBSTACK
559 /* do nothing */
560 #else
561 free (addr);
562 #endif
566 /* Allocate and returns bitmap for IRA. */
567 bitmap
568 ira_allocate_bitmap (void)
570 return BITMAP_ALLOC (&ira_bitmap_obstack);
573 /* Free bitmap B allocated for IRA. */
574 void
575 ira_free_bitmap (bitmap b ATTRIBUTE_UNUSED)
577 /* do nothing */
582 /* Output information about allocation of all allocnos (except for
583 caps) into file F. */
584 void
585 ira_print_disposition (FILE *f)
587 int i, n, max_regno;
588 ira_allocno_t a;
589 basic_block bb;
591 fprintf (f, "Disposition:");
592 max_regno = max_reg_num ();
593 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
594 for (a = ira_regno_allocno_map[i];
595 a != NULL;
596 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
598 if (n % 4 == 0)
599 fprintf (f, "\n");
600 n++;
601 fprintf (f, " %4d:r%-4d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
602 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
603 fprintf (f, "b%-3d", bb->index);
604 else
605 fprintf (f, "l%-3d", ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
606 if (ALLOCNO_HARD_REGNO (a) >= 0)
607 fprintf (f, " %3d", ALLOCNO_HARD_REGNO (a));
608 else
609 fprintf (f, " mem");
611 fprintf (f, "\n");
614 /* Outputs information about allocation of all allocnos into
615 stderr. */
616 void
617 ira_debug_disposition (void)
619 ira_print_disposition (stderr);
623 #define alloc_reg_class_subclasses \
624 (this_target_ira_int->x_alloc_reg_class_subclasses)
626 /* Initialize the table of subclasses of each reg class. */
627 static void
628 setup_reg_subclasses (void)
630 int i, j;
631 HARD_REG_SET temp_hard_regset2;
633 for (i = 0; i < N_REG_CLASSES; i++)
634 for (j = 0; j < N_REG_CLASSES; j++)
635 alloc_reg_class_subclasses[i][j] = LIM_REG_CLASSES;
637 for (i = 0; i < N_REG_CLASSES; i++)
639 if (i == (int) NO_REGS)
640 continue;
642 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
643 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
644 if (hard_reg_set_empty_p (temp_hard_regset))
645 continue;
646 for (j = 0; j < N_REG_CLASSES; j++)
647 if (i != j)
649 enum reg_class *p;
651 COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[j]);
652 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
653 if (! hard_reg_set_subset_p (temp_hard_regset,
654 temp_hard_regset2))
655 continue;
656 p = &alloc_reg_class_subclasses[j][0];
657 while (*p != LIM_REG_CLASSES) p++;
658 *p = (enum reg_class) i;
665 /* Set the four global variables defined above. */
666 static void
667 setup_cover_and_important_classes (void)
669 int i, j, n, cl;
670 bool set_p;
671 const reg_class_t *cover_classes;
672 HARD_REG_SET temp_hard_regset2;
673 static enum reg_class classes[LIM_REG_CLASSES + 1];
675 if (targetm.ira_cover_classes == NULL)
676 cover_classes = NULL;
677 else
678 cover_classes = targetm.ira_cover_classes ();
679 if (cover_classes == NULL)
680 ira_assert (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY);
681 else
683 for (i = 0; (cl = cover_classes[i]) != LIM_REG_CLASSES; i++)
684 classes[i] = (enum reg_class) cl;
685 classes[i] = LIM_REG_CLASSES;
688 if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
690 n = 0;
691 for (i = 0; i <= LIM_REG_CLASSES; i++)
693 if (i == NO_REGS)
694 continue;
695 #ifdef CONSTRAINT_NUM_DEFINED_P
696 for (j = 0; j < CONSTRAINT__LIMIT; j++)
697 if ((int) REG_CLASS_FOR_CONSTRAINT ((enum constraint_num) j) == i)
698 break;
699 if (j < CONSTRAINT__LIMIT)
701 classes[n++] = (enum reg_class) i;
702 continue;
704 #endif
705 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
706 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
707 for (j = 0; j < LIM_REG_CLASSES; j++)
709 if (i == j)
710 continue;
711 COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[j]);
712 AND_COMPL_HARD_REG_SET (temp_hard_regset2,
713 no_unit_alloc_regs);
714 if (hard_reg_set_equal_p (temp_hard_regset,
715 temp_hard_regset2))
716 break;
718 if (j >= i)
719 classes[n++] = (enum reg_class) i;
721 classes[n] = LIM_REG_CLASSES;
724 ira_reg_class_cover_size = 0;
725 for (i = 0; (cl = classes[i]) != LIM_REG_CLASSES; i++)
727 for (j = 0; j < i; j++)
728 if (flag_ira_algorithm != IRA_ALGORITHM_PRIORITY
729 && reg_classes_intersect_p ((enum reg_class) cl, classes[j]))
730 gcc_unreachable ();
731 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
732 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
733 if (! hard_reg_set_empty_p (temp_hard_regset))
734 ira_reg_class_cover[ira_reg_class_cover_size++] = (enum reg_class) cl;
736 ira_important_classes_num = 0;
737 for (cl = 0; cl < N_REG_CLASSES; cl++)
739 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
740 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
741 if (! hard_reg_set_empty_p (temp_hard_regset))
743 set_p = false;
744 for (j = 0; j < ira_reg_class_cover_size; j++)
746 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
747 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
748 COPY_HARD_REG_SET (temp_hard_regset2,
749 reg_class_contents[ira_reg_class_cover[j]]);
750 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
751 if ((enum reg_class) cl == ira_reg_class_cover[j]
752 || hard_reg_set_equal_p (temp_hard_regset,
753 temp_hard_regset2))
754 break;
755 else if (hard_reg_set_subset_p (temp_hard_regset,
756 temp_hard_regset2))
757 set_p = true;
759 if (set_p && j >= ira_reg_class_cover_size)
760 ira_important_classes[ira_important_classes_num++]
761 = (enum reg_class) cl;
764 for (j = 0; j < ira_reg_class_cover_size; j++)
765 ira_important_classes[ira_important_classes_num++]
766 = ira_reg_class_cover[j];
769 /* Set up array IRA_CLASS_TRANSLATE. */
770 static void
771 setup_class_translate (void)
773 int cl, mode;
774 enum reg_class cover_class, best_class, *cl_ptr;
775 int i, cost, min_cost, best_cost;
777 for (cl = 0; cl < N_REG_CLASSES; cl++)
778 ira_class_translate[cl] = NO_REGS;
780 if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
781 for (cl = 0; cl < LIM_REG_CLASSES; cl++)
783 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
784 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
785 for (i = 0; i < ira_reg_class_cover_size; i++)
787 HARD_REG_SET temp_hard_regset2;
789 cover_class = ira_reg_class_cover[i];
790 COPY_HARD_REG_SET (temp_hard_regset2,
791 reg_class_contents[cover_class]);
792 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
793 if (hard_reg_set_equal_p (temp_hard_regset, temp_hard_regset2))
794 ira_class_translate[cl] = cover_class;
797 for (i = 0; i < ira_reg_class_cover_size; i++)
799 cover_class = ira_reg_class_cover[i];
800 if (flag_ira_algorithm != IRA_ALGORITHM_PRIORITY)
801 for (cl_ptr = &alloc_reg_class_subclasses[cover_class][0];
802 (cl = *cl_ptr) != LIM_REG_CLASSES;
803 cl_ptr++)
805 if (ira_class_translate[cl] == NO_REGS)
806 ira_class_translate[cl] = cover_class;
807 #ifdef ENABLE_IRA_CHECKING
808 else
810 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
811 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
812 if (! hard_reg_set_empty_p (temp_hard_regset))
813 gcc_unreachable ();
815 #endif
817 ira_class_translate[cover_class] = cover_class;
819 /* For classes which are not fully covered by a cover class (in
820 other words covered by more one cover class), use the cheapest
821 cover class. */
822 for (cl = 0; cl < N_REG_CLASSES; cl++)
824 if (cl == NO_REGS || ira_class_translate[cl] != NO_REGS)
825 continue;
826 best_class = NO_REGS;
827 best_cost = INT_MAX;
828 for (i = 0; i < ira_reg_class_cover_size; i++)
830 cover_class = ira_reg_class_cover[i];
831 COPY_HARD_REG_SET (temp_hard_regset,
832 reg_class_contents[cover_class]);
833 AND_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
834 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
835 if (! hard_reg_set_empty_p (temp_hard_regset))
837 min_cost = INT_MAX;
838 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
840 cost = (ira_memory_move_cost[mode][cl][0]
841 + ira_memory_move_cost[mode][cl][1]);
842 if (min_cost > cost)
843 min_cost = cost;
845 if (best_class == NO_REGS || best_cost > min_cost)
847 best_class = cover_class;
848 best_cost = min_cost;
852 ira_class_translate[cl] = best_class;
856 /* Order numbers of cover classes in original target cover class
857 array, -1 for non-cover classes. This is only live during
858 reorder_important_classes. */
859 static int cover_class_order[N_REG_CLASSES];
861 /* The function used to sort the important classes. */
862 static int
863 comp_reg_classes_func (const void *v1p, const void *v2p)
865 enum reg_class cl1 = *(const enum reg_class *) v1p;
866 enum reg_class cl2 = *(const enum reg_class *) v2p;
867 int diff;
869 cl1 = ira_class_translate[cl1];
870 cl2 = ira_class_translate[cl2];
871 if (cl1 != NO_REGS && cl2 != NO_REGS
872 && (diff = cover_class_order[cl1] - cover_class_order[cl2]) != 0)
873 return diff;
874 return (int) cl1 - (int) cl2;
877 /* Reorder important classes according to the order of their cover
878 classes. */
879 static void
880 reorder_important_classes (void)
882 int i;
884 for (i = 0; i < N_REG_CLASSES; i++)
885 cover_class_order[i] = -1;
886 for (i = 0; i < ira_reg_class_cover_size; i++)
887 cover_class_order[ira_reg_class_cover[i]] = i;
888 qsort (ira_important_classes, ira_important_classes_num,
889 sizeof (enum reg_class), comp_reg_classes_func);
892 /* Set up the above reg class relations. */
893 static void
894 setup_reg_class_relations (void)
896 int i, cl1, cl2, cl3;
897 HARD_REG_SET intersection_set, union_set, temp_set2;
898 bool important_class_p[N_REG_CLASSES];
900 memset (important_class_p, 0, sizeof (important_class_p));
901 for (i = 0; i < ira_important_classes_num; i++)
902 important_class_p[ira_important_classes[i]] = true;
903 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
905 ira_reg_class_super_classes[cl1][0] = LIM_REG_CLASSES;
906 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
908 ira_reg_classes_intersect_p[cl1][cl2] = false;
909 ira_reg_class_intersect[cl1][cl2] = NO_REGS;
910 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl1]);
911 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
912 COPY_HARD_REG_SET (temp_set2, reg_class_contents[cl2]);
913 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
914 if (hard_reg_set_empty_p (temp_hard_regset)
915 && hard_reg_set_empty_p (temp_set2))
917 for (i = 0;; i++)
919 cl3 = reg_class_subclasses[cl1][i];
920 if (cl3 == LIM_REG_CLASSES)
921 break;
922 if (reg_class_subset_p (ira_reg_class_intersect[cl1][cl2],
923 (enum reg_class) cl3))
924 ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
926 ira_reg_class_union[cl1][cl2] = reg_class_subunion[cl1][cl2];
927 continue;
929 ira_reg_classes_intersect_p[cl1][cl2]
930 = hard_reg_set_intersect_p (temp_hard_regset, temp_set2);
931 if (important_class_p[cl1] && important_class_p[cl2]
932 && hard_reg_set_subset_p (temp_hard_regset, temp_set2))
934 enum reg_class *p;
936 p = &ira_reg_class_super_classes[cl1][0];
937 while (*p != LIM_REG_CLASSES)
938 p++;
939 *p++ = (enum reg_class) cl2;
940 *p = LIM_REG_CLASSES;
942 ira_reg_class_union[cl1][cl2] = NO_REGS;
943 COPY_HARD_REG_SET (intersection_set, reg_class_contents[cl1]);
944 AND_HARD_REG_SET (intersection_set, reg_class_contents[cl2]);
945 AND_COMPL_HARD_REG_SET (intersection_set, no_unit_alloc_regs);
946 COPY_HARD_REG_SET (union_set, reg_class_contents[cl1]);
947 IOR_HARD_REG_SET (union_set, reg_class_contents[cl2]);
948 AND_COMPL_HARD_REG_SET (union_set, no_unit_alloc_regs);
949 for (i = 0; i < ira_important_classes_num; i++)
951 cl3 = ira_important_classes[i];
952 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl3]);
953 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
954 if (hard_reg_set_subset_p (temp_hard_regset, intersection_set))
956 COPY_HARD_REG_SET
957 (temp_set2,
958 reg_class_contents[(int)
959 ira_reg_class_intersect[cl1][cl2]]);
960 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
961 if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2)
962 /* Ignore unavailable hard registers and prefer
963 smallest class for debugging purposes. */
964 || (hard_reg_set_equal_p (temp_hard_regset, temp_set2)
965 && hard_reg_set_subset_p
966 (reg_class_contents[cl3],
967 reg_class_contents
968 [(int) ira_reg_class_intersect[cl1][cl2]])))
969 ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
971 if (hard_reg_set_subset_p (temp_hard_regset, union_set))
973 COPY_HARD_REG_SET
974 (temp_set2,
975 reg_class_contents[(int) ira_reg_class_union[cl1][cl2]]);
976 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
977 if (ira_reg_class_union[cl1][cl2] == NO_REGS
978 || (hard_reg_set_subset_p (temp_set2, temp_hard_regset)
980 && (! hard_reg_set_equal_p (temp_set2,
981 temp_hard_regset)
982 /* Ignore unavailable hard registers and
983 prefer smallest class for debugging
984 purposes. */
985 || hard_reg_set_subset_p
986 (reg_class_contents[cl3],
987 reg_class_contents
988 [(int) ira_reg_class_union[cl1][cl2]]))))
989 ira_reg_class_union[cl1][cl2] = (enum reg_class) cl3;
996 /* Output all cover classes and the translation map into file F. */
997 static void
998 print_class_cover (FILE *f)
1000 static const char *const reg_class_names[] = REG_CLASS_NAMES;
1001 int i;
1003 fprintf (f, "Class cover:\n");
1004 for (i = 0; i < ira_reg_class_cover_size; i++)
1005 fprintf (f, " %s", reg_class_names[ira_reg_class_cover[i]]);
1006 fprintf (f, "\nClass translation:\n");
1007 for (i = 0; i < N_REG_CLASSES; i++)
1008 fprintf (f, " %s -> %s\n", reg_class_names[i],
1009 reg_class_names[ira_class_translate[i]]);
1012 /* Output all cover classes and the translation map into
1013 stderr. */
1014 void
1015 ira_debug_class_cover (void)
1017 print_class_cover (stderr);
1020 /* Set up different arrays concerning class subsets, cover and
1021 important classes. */
1022 static void
1023 find_reg_class_closure (void)
1025 setup_reg_subclasses ();
1026 setup_cover_and_important_classes ();
1027 setup_class_translate ();
1028 reorder_important_classes ();
1029 setup_reg_class_relations ();
1034 /* Set up the array above. */
1035 static void
1036 setup_hard_regno_cover_class (void)
1038 int i, j;
1039 enum reg_class cl;
1041 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1043 ira_hard_regno_cover_class[i] = NO_REGS;
1044 for (j = 0; j < ira_reg_class_cover_size; j++)
1046 cl = ira_reg_class_cover[j];
1047 if (ira_class_hard_reg_index[cl][i] >= 0)
1049 ira_hard_regno_cover_class[i] = cl;
1050 break;
1059 /* Form IRA_REG_CLASS_NREGS map. */
1060 static void
1061 setup_reg_class_nregs (void)
1063 int cl, m;
1065 for (cl = 0; cl < N_REG_CLASSES; cl++)
1066 for (m = 0; m < MAX_MACHINE_MODE; m++)
1067 ira_reg_class_nregs[cl][m] = CLASS_MAX_NREGS ((enum reg_class) cl,
1068 (enum machine_mode) m);
1073 /* Set up PROHIBITED_CLASS_MODE_REGS. */
1074 static void
1075 setup_prohibited_class_mode_regs (void)
1077 int i, j, k, hard_regno;
1078 enum reg_class cl;
1080 for (i = 0; i < ira_reg_class_cover_size; i++)
1082 cl = ira_reg_class_cover[i];
1083 for (j = 0; j < NUM_MACHINE_MODES; j++)
1085 CLEAR_HARD_REG_SET (prohibited_class_mode_regs[cl][j]);
1086 for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1088 hard_regno = ira_class_hard_regs[cl][k];
1089 if (! HARD_REGNO_MODE_OK (hard_regno, (enum machine_mode) j))
1090 SET_HARD_REG_BIT (prohibited_class_mode_regs[cl][j],
1091 hard_regno);
1099 /* Allocate and initialize IRA_REGISTER_MOVE_COST,
1100 IRA_MAY_MOVE_IN_COST, and IRA_MAY_MOVE_OUT_COST for MODE if it is
1101 not done yet. */
1102 void
1103 ira_init_register_move_cost (enum machine_mode mode)
1105 int cl1, cl2;
1107 ira_assert (ira_register_move_cost[mode] == NULL
1108 && ira_may_move_in_cost[mode] == NULL
1109 && ira_may_move_out_cost[mode] == NULL);
1110 if (move_cost[mode] == NULL)
1111 init_move_cost (mode);
1112 ira_register_move_cost[mode] = move_cost[mode];
1113 /* Don't use ira_allocate because the tables exist out of scope of a
1114 IRA call. */
1115 ira_may_move_in_cost[mode]
1116 = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1117 memcpy (ira_may_move_in_cost[mode], may_move_in_cost[mode],
1118 sizeof (move_table) * N_REG_CLASSES);
1119 ira_may_move_out_cost[mode]
1120 = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1121 memcpy (ira_may_move_out_cost[mode], may_move_out_cost[mode],
1122 sizeof (move_table) * N_REG_CLASSES);
1123 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1125 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1127 if (ira_class_subset_p[cl1][cl2])
1128 ira_may_move_in_cost[mode][cl1][cl2] = 0;
1129 if (ira_class_subset_p[cl2][cl1])
1130 ira_may_move_out_cost[mode][cl1][cl2] = 0;
1137 /* This is called once during compiler work. It sets up
1138 different arrays whose values don't depend on the compiled
1139 function. */
1140 void
1141 ira_init_once (void)
1143 int mode;
1145 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1147 ira_register_move_cost[mode] = NULL;
1148 ira_may_move_in_cost[mode] = NULL;
1149 ira_may_move_out_cost[mode] = NULL;
1151 ira_init_costs_once ();
1154 /* Free ira_register_move_cost, ira_may_move_in_cost, and
1155 ira_may_move_out_cost for each mode. */
1156 static void
1157 free_register_move_costs (void)
1159 int mode;
1161 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1163 if (ira_may_move_in_cost[mode] != NULL)
1164 free (ira_may_move_in_cost[mode]);
1165 if (ira_may_move_out_cost[mode] != NULL)
1166 free (ira_may_move_out_cost[mode]);
1167 ira_register_move_cost[mode] = NULL;
1168 ira_may_move_in_cost[mode] = NULL;
1169 ira_may_move_out_cost[mode] = NULL;
1173 /* This is called every time when register related information is
1174 changed. */
1175 void
1176 ira_init (void)
1178 free_register_move_costs ();
1179 setup_reg_mode_hard_regset ();
1180 setup_alloc_regs (flag_omit_frame_pointer != 0);
1181 setup_class_subset_and_memory_move_costs ();
1182 find_reg_class_closure ();
1183 setup_hard_regno_cover_class ();
1184 setup_reg_class_nregs ();
1185 setup_prohibited_class_mode_regs ();
1186 ira_init_costs ();
1189 /* Function called once at the end of compiler work. */
1190 void
1191 ira_finish_once (void)
1193 ira_finish_costs_once ();
1194 free_register_move_costs ();
1198 #define ira_prohibited_mode_move_regs_initialized_p \
1199 (this_target_ira_int->x_ira_prohibited_mode_move_regs_initialized_p)
1201 /* Set up IRA_PROHIBITED_MODE_MOVE_REGS. */
1202 static void
1203 setup_prohibited_mode_move_regs (void)
1205 int i, j;
1206 rtx test_reg1, test_reg2, move_pat, move_insn;
1208 if (ira_prohibited_mode_move_regs_initialized_p)
1209 return;
1210 ira_prohibited_mode_move_regs_initialized_p = true;
1211 test_reg1 = gen_rtx_REG (VOIDmode, 0);
1212 test_reg2 = gen_rtx_REG (VOIDmode, 0);
1213 move_pat = gen_rtx_SET (VOIDmode, test_reg1, test_reg2);
1214 move_insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, 0, move_pat, -1, 0);
1215 for (i = 0; i < NUM_MACHINE_MODES; i++)
1217 SET_HARD_REG_SET (ira_prohibited_mode_move_regs[i]);
1218 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1220 if (! HARD_REGNO_MODE_OK (j, (enum machine_mode) i))
1221 continue;
1222 SET_REGNO (test_reg1, j);
1223 PUT_MODE (test_reg1, (enum machine_mode) i);
1224 SET_REGNO (test_reg2, j);
1225 PUT_MODE (test_reg2, (enum machine_mode) i);
1226 INSN_CODE (move_insn) = -1;
1227 recog_memoized (move_insn);
1228 if (INSN_CODE (move_insn) < 0)
1229 continue;
1230 extract_insn (move_insn);
1231 if (! constrain_operands (1))
1232 continue;
1233 CLEAR_HARD_REG_BIT (ira_prohibited_mode_move_regs[i], j);
1240 /* Return nonzero if REGNO is a particularly bad choice for reloading X. */
1241 static bool
1242 ira_bad_reload_regno_1 (int regno, rtx x)
1244 int x_regno;
1245 ira_allocno_t a;
1246 ira_object_t obj;
1247 enum reg_class pref;
1249 /* We only deal with pseudo regs. */
1250 if (! x || GET_CODE (x) != REG)
1251 return false;
1253 x_regno = REGNO (x);
1254 if (x_regno < FIRST_PSEUDO_REGISTER)
1255 return false;
1257 /* If the pseudo prefers REGNO explicitly, then do not consider
1258 REGNO a bad spill choice. */
1259 pref = reg_preferred_class (x_regno);
1260 if (reg_class_size[pref] == 1)
1261 return !TEST_HARD_REG_BIT (reg_class_contents[pref], regno);
1263 /* If the pseudo conflicts with REGNO, then we consider REGNO a
1264 poor choice for a reload regno. */
1265 a = ira_regno_allocno_map[x_regno];
1266 obj = ALLOCNO_OBJECT (a);
1267 if (TEST_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), regno))
1268 return true;
1270 return false;
1273 /* Return nonzero if REGNO is a particularly bad choice for reloading
1274 IN or OUT. */
1275 bool
1276 ira_bad_reload_regno (int regno, rtx in, rtx out)
1278 return (ira_bad_reload_regno_1 (regno, in)
1279 || ira_bad_reload_regno_1 (regno, out));
1282 /* Function specific hard registers that can not be used for the
1283 register allocation. */
1284 HARD_REG_SET ira_no_alloc_regs;
1286 /* Return TRUE if *LOC contains an asm. */
1287 static int
1288 insn_contains_asm_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
1290 if ( !*loc)
1291 return FALSE;
1292 if (GET_CODE (*loc) == ASM_OPERANDS)
1293 return TRUE;
1294 return FALSE;
1298 /* Return TRUE if INSN contains an ASM. */
1299 static bool
1300 insn_contains_asm (rtx insn)
1302 return for_each_rtx (&insn, insn_contains_asm_1, NULL);
1305 /* Add register clobbers from asm statements. */
1306 static void
1307 compute_regs_asm_clobbered (void)
1309 basic_block bb;
1311 FOR_EACH_BB (bb)
1313 rtx insn;
1314 FOR_BB_INSNS_REVERSE (bb, insn)
1316 df_ref *def_rec;
1318 if (insn_contains_asm (insn))
1319 for (def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++)
1321 df_ref def = *def_rec;
1322 unsigned int dregno = DF_REF_REGNO (def);
1323 if (dregno < FIRST_PSEUDO_REGISTER)
1325 unsigned int i;
1326 enum machine_mode mode = GET_MODE (DF_REF_REAL_REG (def));
1327 unsigned int end = dregno
1328 + hard_regno_nregs[dregno][mode] - 1;
1330 for (i = dregno; i <= end; ++i)
1331 SET_HARD_REG_BIT(crtl->asm_clobbers, i);
1339 /* Set up ELIMINABLE_REGSET, IRA_NO_ALLOC_REGS, and REGS_EVER_LIVE. */
1340 void
1341 ira_setup_eliminable_regset (void)
1343 #ifdef ELIMINABLE_REGS
1344 int i;
1345 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
1346 #endif
1347 /* FIXME: If EXIT_IGNORE_STACK is set, we will not save and restore
1348 sp for alloca. So we can't eliminate the frame pointer in that
1349 case. At some point, we should improve this by emitting the
1350 sp-adjusting insns for this case. */
1351 int need_fp
1352 = (! flag_omit_frame_pointer
1353 || (cfun->calls_alloca && EXIT_IGNORE_STACK)
1354 /* We need the frame pointer to catch stack overflow exceptions
1355 if the stack pointer is moving. */
1356 || (flag_stack_check && STACK_CHECK_MOVING_SP)
1357 || crtl->accesses_prior_frames
1358 || crtl->stack_realign_needed
1359 || targetm.frame_pointer_required ());
1361 frame_pointer_needed = need_fp;
1363 COPY_HARD_REG_SET (ira_no_alloc_regs, no_unit_alloc_regs);
1364 CLEAR_HARD_REG_SET (eliminable_regset);
1366 compute_regs_asm_clobbered ();
1368 /* Build the regset of all eliminable registers and show we can't
1369 use those that we already know won't be eliminated. */
1370 #ifdef ELIMINABLE_REGS
1371 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
1373 bool cannot_elim
1374 = (! targetm.can_eliminate (eliminables[i].from, eliminables[i].to)
1375 || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp));
1377 if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, eliminables[i].from))
1379 SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
1381 if (cannot_elim)
1382 SET_HARD_REG_BIT (ira_no_alloc_regs, eliminables[i].from);
1384 else if (cannot_elim)
1385 error ("%s cannot be used in asm here",
1386 reg_names[eliminables[i].from]);
1387 else
1388 df_set_regs_ever_live (eliminables[i].from, true);
1390 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1391 if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, HARD_FRAME_POINTER_REGNUM))
1393 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
1394 if (need_fp)
1395 SET_HARD_REG_BIT (ira_no_alloc_regs, HARD_FRAME_POINTER_REGNUM);
1397 else if (need_fp)
1398 error ("%s cannot be used in asm here",
1399 reg_names[HARD_FRAME_POINTER_REGNUM]);
1400 else
1401 df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
1402 #endif
1404 #else
1405 if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, HARD_FRAME_POINTER_REGNUM))
1407 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
1408 if (need_fp)
1409 SET_HARD_REG_BIT (ira_no_alloc_regs, FRAME_POINTER_REGNUM);
1411 else if (need_fp)
1412 error ("%s cannot be used in asm here", reg_names[FRAME_POINTER_REGNUM]);
1413 else
1414 df_set_regs_ever_live (FRAME_POINTER_REGNUM, true);
1415 #endif
1420 /* The length of the following two arrays. */
1421 int ira_reg_equiv_len;
1423 /* The element value is TRUE if the corresponding regno value is
1424 invariant. */
1425 bool *ira_reg_equiv_invariant_p;
1427 /* The element value is equiv constant of given pseudo-register or
1428 NULL_RTX. */
1429 rtx *ira_reg_equiv_const;
1431 /* Set up the two arrays declared above. */
1432 static void
1433 find_reg_equiv_invariant_const (void)
1435 int i;
1436 bool invariant_p;
1437 rtx list, insn, note, constant, x;
1439 for (i = FIRST_PSEUDO_REGISTER; i < reg_equiv_init_size; i++)
1441 constant = NULL_RTX;
1442 invariant_p = false;
1443 for (list = reg_equiv_init[i]; list != NULL_RTX; list = XEXP (list, 1))
1445 insn = XEXP (list, 0);
1446 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
1448 if (note == NULL_RTX)
1449 continue;
1451 x = XEXP (note, 0);
1453 if (! CONSTANT_P (x)
1454 || ! flag_pic || LEGITIMATE_PIC_OPERAND_P (x))
1456 /* It can happen that a REG_EQUIV note contains a MEM
1457 that is not a legitimate memory operand. As later
1458 stages of the reload assume that all addresses found
1459 in the reg_equiv_* arrays were originally legitimate,
1460 we ignore such REG_EQUIV notes. */
1461 if (memory_operand (x, VOIDmode))
1462 invariant_p = MEM_READONLY_P (x);
1463 else if (function_invariant_p (x))
1465 if (GET_CODE (x) == PLUS
1466 || x == frame_pointer_rtx || x == arg_pointer_rtx)
1467 invariant_p = true;
1468 else
1469 constant = x;
1473 ira_reg_equiv_invariant_p[i] = invariant_p;
1474 ira_reg_equiv_const[i] = constant;
1480 /* Vector of substitutions of register numbers,
1481 used to map pseudo regs into hardware regs.
1482 This is set up as a result of register allocation.
1483 Element N is the hard reg assigned to pseudo reg N,
1484 or is -1 if no hard reg was assigned.
1485 If N is a hard reg number, element N is N. */
1486 short *reg_renumber;
1488 /* Set up REG_RENUMBER and CALLER_SAVE_NEEDED (used by reload) from
1489 the allocation found by IRA. */
1490 static void
1491 setup_reg_renumber (void)
1493 int regno, hard_regno;
1494 ira_allocno_t a;
1495 ira_allocno_iterator ai;
1497 caller_save_needed = 0;
1498 FOR_EACH_ALLOCNO (a, ai)
1500 /* There are no caps at this point. */
1501 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
1502 if (! ALLOCNO_ASSIGNED_P (a))
1503 /* It can happen if A is not referenced but partially anticipated
1504 somewhere in a region. */
1505 ALLOCNO_ASSIGNED_P (a) = true;
1506 ira_free_allocno_updated_costs (a);
1507 hard_regno = ALLOCNO_HARD_REGNO (a);
1508 regno = (int) REGNO (ALLOCNO_REG (a));
1509 reg_renumber[regno] = (hard_regno < 0 ? -1 : hard_regno);
1510 if (hard_regno >= 0 && ALLOCNO_CALLS_CROSSED_NUM (a) != 0
1511 && ! ira_hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
1512 call_used_reg_set))
1514 ira_assert (!optimize || flag_caller_saves
1515 || regno >= ira_reg_equiv_len
1516 || ira_reg_equiv_const[regno]
1517 || ira_reg_equiv_invariant_p[regno]);
1518 caller_save_needed = 1;
1523 /* Set up allocno assignment flags for further allocation
1524 improvements. */
1525 static void
1526 setup_allocno_assignment_flags (void)
1528 int hard_regno;
1529 ira_allocno_t a;
1530 ira_allocno_iterator ai;
1532 FOR_EACH_ALLOCNO (a, ai)
1534 if (! ALLOCNO_ASSIGNED_P (a))
1535 /* It can happen if A is not referenced but partially anticipated
1536 somewhere in a region. */
1537 ira_free_allocno_updated_costs (a);
1538 hard_regno = ALLOCNO_HARD_REGNO (a);
1539 /* Don't assign hard registers to allocnos which are destination
1540 of removed store at the end of loop. It has no sense to keep
1541 the same value in different hard registers. It is also
1542 impossible to assign hard registers correctly to such
1543 allocnos because the cost info and info about intersected
1544 calls are incorrect for them. */
1545 ALLOCNO_ASSIGNED_P (a) = (hard_regno >= 0
1546 || ALLOCNO_MEM_OPTIMIZED_DEST_P (a)
1547 || (ALLOCNO_MEMORY_COST (a)
1548 - ALLOCNO_COVER_CLASS_COST (a)) < 0);
1549 ira_assert (hard_regno < 0
1550 || ! ira_hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
1551 reg_class_contents
1552 [ALLOCNO_COVER_CLASS (a)]));
1556 /* Evaluate overall allocation cost and the costs for using hard
1557 registers and memory for allocnos. */
1558 static void
1559 calculate_allocation_cost (void)
1561 int hard_regno, cost;
1562 ira_allocno_t a;
1563 ira_allocno_iterator ai;
1565 ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
1566 FOR_EACH_ALLOCNO (a, ai)
1568 hard_regno = ALLOCNO_HARD_REGNO (a);
1569 ira_assert (hard_regno < 0
1570 || ! ira_hard_reg_not_in_set_p
1571 (hard_regno, ALLOCNO_MODE (a),
1572 reg_class_contents[ALLOCNO_COVER_CLASS (a)]));
1573 if (hard_regno < 0)
1575 cost = ALLOCNO_MEMORY_COST (a);
1576 ira_mem_cost += cost;
1578 else if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
1580 cost = (ALLOCNO_HARD_REG_COSTS (a)
1581 [ira_class_hard_reg_index
1582 [ALLOCNO_COVER_CLASS (a)][hard_regno]]);
1583 ira_reg_cost += cost;
1585 else
1587 cost = ALLOCNO_COVER_CLASS_COST (a);
1588 ira_reg_cost += cost;
1590 ira_overall_cost += cost;
1593 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1595 fprintf (ira_dump_file,
1596 "+++Costs: overall %d, reg %d, mem %d, ld %d, st %d, move %d\n",
1597 ira_overall_cost, ira_reg_cost, ira_mem_cost,
1598 ira_load_cost, ira_store_cost, ira_shuffle_cost);
1599 fprintf (ira_dump_file, "+++ move loops %d, new jumps %d\n",
1600 ira_move_loops_num, ira_additional_jumps_num);
1605 #ifdef ENABLE_IRA_CHECKING
1606 /* Check the correctness of the allocation. We do need this because
1607 of complicated code to transform more one region internal
1608 representation into one region representation. */
1609 static void
1610 check_allocation (void)
1612 ira_allocno_t a;
1613 int hard_regno, nregs;
1614 ira_allocno_iterator ai;
1616 FOR_EACH_ALLOCNO (a, ai)
1618 ira_object_t obj, conflict_obj;
1619 ira_object_conflict_iterator oci;
1621 if (ALLOCNO_CAP_MEMBER (a) != NULL
1622 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
1623 continue;
1624 nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
1625 obj = ALLOCNO_OBJECT (a);
1626 FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1628 ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1629 int conflict_hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
1630 if (conflict_hard_regno >= 0)
1632 int conflict_nregs
1633 = (hard_regno_nregs
1634 [conflict_hard_regno][ALLOCNO_MODE (conflict_a)]);
1635 if ((conflict_hard_regno <= hard_regno
1636 && hard_regno < conflict_hard_regno + conflict_nregs)
1637 || (hard_regno <= conflict_hard_regno
1638 && conflict_hard_regno < hard_regno + nregs))
1640 fprintf (stderr, "bad allocation for %d and %d\n",
1641 ALLOCNO_REGNO (a), ALLOCNO_REGNO (conflict_a));
1642 gcc_unreachable ();
1648 #endif
1650 /* Fix values of array REG_EQUIV_INIT after live range splitting done
1651 by IRA. */
1652 static void
1653 fix_reg_equiv_init (void)
1655 int max_regno = max_reg_num ();
1656 int i, new_regno;
1657 rtx x, prev, next, insn, set;
1659 if (reg_equiv_init_size < max_regno)
1661 reg_equiv_init = GGC_RESIZEVEC (rtx, reg_equiv_init, max_regno);
1662 while (reg_equiv_init_size < max_regno)
1663 reg_equiv_init[reg_equiv_init_size++] = NULL_RTX;
1664 for (i = FIRST_PSEUDO_REGISTER; i < reg_equiv_init_size; i++)
1665 for (prev = NULL_RTX, x = reg_equiv_init[i]; x != NULL_RTX; x = next)
1667 next = XEXP (x, 1);
1668 insn = XEXP (x, 0);
1669 set = single_set (insn);
1670 ira_assert (set != NULL_RTX
1671 && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))));
1672 if (REG_P (SET_DEST (set))
1673 && ((int) REGNO (SET_DEST (set)) == i
1674 || (int) ORIGINAL_REGNO (SET_DEST (set)) == i))
1675 new_regno = REGNO (SET_DEST (set));
1676 else if (REG_P (SET_SRC (set))
1677 && ((int) REGNO (SET_SRC (set)) == i
1678 || (int) ORIGINAL_REGNO (SET_SRC (set)) == i))
1679 new_regno = REGNO (SET_SRC (set));
1680 else
1681 gcc_unreachable ();
1682 if (new_regno == i)
1683 prev = x;
1684 else
1686 if (prev == NULL_RTX)
1687 reg_equiv_init[i] = next;
1688 else
1689 XEXP (prev, 1) = next;
1690 XEXP (x, 1) = reg_equiv_init[new_regno];
1691 reg_equiv_init[new_regno] = x;
1697 #ifdef ENABLE_IRA_CHECKING
1698 /* Print redundant memory-memory copies. */
1699 static void
1700 print_redundant_copies (void)
1702 int hard_regno;
1703 ira_allocno_t a;
1704 ira_copy_t cp, next_cp;
1705 ira_allocno_iterator ai;
1707 FOR_EACH_ALLOCNO (a, ai)
1709 if (ALLOCNO_CAP_MEMBER (a) != NULL)
1710 /* It is a cap. */
1711 continue;
1712 hard_regno = ALLOCNO_HARD_REGNO (a);
1713 if (hard_regno >= 0)
1714 continue;
1715 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1716 if (cp->first == a)
1717 next_cp = cp->next_first_allocno_copy;
1718 else
1720 next_cp = cp->next_second_allocno_copy;
1721 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL
1722 && cp->insn != NULL_RTX
1723 && ALLOCNO_HARD_REGNO (cp->first) == hard_regno)
1724 fprintf (ira_dump_file,
1725 " Redundant move from %d(freq %d):%d\n",
1726 INSN_UID (cp->insn), cp->freq, hard_regno);
1730 #endif
1732 /* Setup preferred and alternative classes for new pseudo-registers
1733 created by IRA starting with START. */
1734 static void
1735 setup_preferred_alternate_classes_for_new_pseudos (int start)
1737 int i, old_regno;
1738 int max_regno = max_reg_num ();
1740 for (i = start; i < max_regno; i++)
1742 old_regno = ORIGINAL_REGNO (regno_reg_rtx[i]);
1743 ira_assert (i != old_regno);
1744 setup_reg_classes (i, reg_preferred_class (old_regno),
1745 reg_alternate_class (old_regno),
1746 reg_cover_class (old_regno));
1747 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
1748 fprintf (ira_dump_file,
1749 " New r%d: setting preferred %s, alternative %s\n",
1750 i, reg_class_names[reg_preferred_class (old_regno)],
1751 reg_class_names[reg_alternate_class (old_regno)]);
1757 /* Regional allocation can create new pseudo-registers. This function
1758 expands some arrays for pseudo-registers. */
1759 static void
1760 expand_reg_info (int old_size)
1762 int i;
1763 int size = max_reg_num ();
1765 resize_reg_info ();
1766 for (i = old_size; i < size; i++)
1767 setup_reg_classes (i, GENERAL_REGS, ALL_REGS, GENERAL_REGS);
1770 /* Return TRUE if there is too high register pressure in the function.
1771 It is used to decide when stack slot sharing is worth to do. */
1772 static bool
1773 too_high_register_pressure_p (void)
1775 int i;
1776 enum reg_class cover_class;
1778 for (i = 0; i < ira_reg_class_cover_size; i++)
1780 cover_class = ira_reg_class_cover[i];
1781 if (ira_loop_tree_root->reg_pressure[cover_class] > 10000)
1782 return true;
1784 return false;
1789 /* Indicate that hard register number FROM was eliminated and replaced with
1790 an offset from hard register number TO. The status of hard registers live
1791 at the start of a basic block is updated by replacing a use of FROM with
1792 a use of TO. */
1794 void
1795 mark_elimination (int from, int to)
1797 basic_block bb;
1799 FOR_EACH_BB (bb)
1801 /* We don't use LIVE info in IRA. */
1802 bitmap r = DF_LR_IN (bb);
1804 if (REGNO_REG_SET_P (r, from))
1806 CLEAR_REGNO_REG_SET (r, from);
1807 SET_REGNO_REG_SET (r, to);
1814 struct equivalence
1816 /* Set when a REG_EQUIV note is found or created. Use to
1817 keep track of what memory accesses might be created later,
1818 e.g. by reload. */
1819 rtx replacement;
1820 rtx *src_p;
1821 /* The list of each instruction which initializes this register. */
1822 rtx init_insns;
1823 /* Loop depth is used to recognize equivalences which appear
1824 to be present within the same loop (or in an inner loop). */
1825 int loop_depth;
1826 /* Nonzero if this had a preexisting REG_EQUIV note. */
1827 int is_arg_equivalence;
1828 /* Set when an attempt should be made to replace a register
1829 with the associated src_p entry. */
1830 char replace;
1833 /* reg_equiv[N] (where N is a pseudo reg number) is the equivalence
1834 structure for that register. */
1835 static struct equivalence *reg_equiv;
1837 /* Used for communication between the following two functions: contains
1838 a MEM that we wish to ensure remains unchanged. */
1839 static rtx equiv_mem;
1841 /* Set nonzero if EQUIV_MEM is modified. */
1842 static int equiv_mem_modified;
1844 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
1845 Called via note_stores. */
1846 static void
1847 validate_equiv_mem_from_store (rtx dest, const_rtx set ATTRIBUTE_UNUSED,
1848 void *data ATTRIBUTE_UNUSED)
1850 if ((REG_P (dest)
1851 && reg_overlap_mentioned_p (dest, equiv_mem))
1852 || (MEM_P (dest)
1853 && true_dependence (dest, VOIDmode, equiv_mem, rtx_varies_p)))
1854 equiv_mem_modified = 1;
1857 /* Verify that no store between START and the death of REG invalidates
1858 MEMREF. MEMREF is invalidated by modifying a register used in MEMREF,
1859 by storing into an overlapping memory location, or with a non-const
1860 CALL_INSN.
1862 Return 1 if MEMREF remains valid. */
1863 static int
1864 validate_equiv_mem (rtx start, rtx reg, rtx memref)
1866 rtx insn;
1867 rtx note;
1869 equiv_mem = memref;
1870 equiv_mem_modified = 0;
1872 /* If the memory reference has side effects or is volatile, it isn't a
1873 valid equivalence. */
1874 if (side_effects_p (memref))
1875 return 0;
1877 for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
1879 if (! INSN_P (insn))
1880 continue;
1882 if (find_reg_note (insn, REG_DEAD, reg))
1883 return 1;
1885 if (CALL_P (insn) && ! MEM_READONLY_P (memref)
1886 && ! RTL_CONST_OR_PURE_CALL_P (insn))
1887 return 0;
1889 note_stores (PATTERN (insn), validate_equiv_mem_from_store, NULL);
1891 /* If a register mentioned in MEMREF is modified via an
1892 auto-increment, we lose the equivalence. Do the same if one
1893 dies; although we could extend the life, it doesn't seem worth
1894 the trouble. */
1896 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1897 if ((REG_NOTE_KIND (note) == REG_INC
1898 || REG_NOTE_KIND (note) == REG_DEAD)
1899 && REG_P (XEXP (note, 0))
1900 && reg_overlap_mentioned_p (XEXP (note, 0), memref))
1901 return 0;
1904 return 0;
1907 /* Returns zero if X is known to be invariant. */
1908 static int
1909 equiv_init_varies_p (rtx x)
1911 RTX_CODE code = GET_CODE (x);
1912 int i;
1913 const char *fmt;
1915 switch (code)
1917 case MEM:
1918 return !MEM_READONLY_P (x) || equiv_init_varies_p (XEXP (x, 0));
1920 case CONST:
1921 case CONST_INT:
1922 case CONST_DOUBLE:
1923 case CONST_FIXED:
1924 case CONST_VECTOR:
1925 case SYMBOL_REF:
1926 case LABEL_REF:
1927 return 0;
1929 case REG:
1930 return reg_equiv[REGNO (x)].replace == 0 && rtx_varies_p (x, 0);
1932 case ASM_OPERANDS:
1933 if (MEM_VOLATILE_P (x))
1934 return 1;
1936 /* Fall through. */
1938 default:
1939 break;
1942 fmt = GET_RTX_FORMAT (code);
1943 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1944 if (fmt[i] == 'e')
1946 if (equiv_init_varies_p (XEXP (x, i)))
1947 return 1;
1949 else if (fmt[i] == 'E')
1951 int j;
1952 for (j = 0; j < XVECLEN (x, i); j++)
1953 if (equiv_init_varies_p (XVECEXP (x, i, j)))
1954 return 1;
1957 return 0;
1960 /* Returns nonzero if X (used to initialize register REGNO) is movable.
1961 X is only movable if the registers it uses have equivalent initializations
1962 which appear to be within the same loop (or in an inner loop) and movable
1963 or if they are not candidates for local_alloc and don't vary. */
1964 static int
1965 equiv_init_movable_p (rtx x, int regno)
1967 int i, j;
1968 const char *fmt;
1969 enum rtx_code code = GET_CODE (x);
1971 switch (code)
1973 case SET:
1974 return equiv_init_movable_p (SET_SRC (x), regno);
1976 case CC0:
1977 case CLOBBER:
1978 return 0;
1980 case PRE_INC:
1981 case PRE_DEC:
1982 case POST_INC:
1983 case POST_DEC:
1984 case PRE_MODIFY:
1985 case POST_MODIFY:
1986 return 0;
1988 case REG:
1989 return (reg_equiv[REGNO (x)].loop_depth >= reg_equiv[regno].loop_depth
1990 && reg_equiv[REGNO (x)].replace)
1991 || (REG_BASIC_BLOCK (REGNO (x)) < NUM_FIXED_BLOCKS && ! rtx_varies_p (x, 0));
1993 case UNSPEC_VOLATILE:
1994 return 0;
1996 case ASM_OPERANDS:
1997 if (MEM_VOLATILE_P (x))
1998 return 0;
2000 /* Fall through. */
2002 default:
2003 break;
2006 fmt = GET_RTX_FORMAT (code);
2007 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2008 switch (fmt[i])
2010 case 'e':
2011 if (! equiv_init_movable_p (XEXP (x, i), regno))
2012 return 0;
2013 break;
2014 case 'E':
2015 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2016 if (! equiv_init_movable_p (XVECEXP (x, i, j), regno))
2017 return 0;
2018 break;
2021 return 1;
2024 /* TRUE if X uses any registers for which reg_equiv[REGNO].replace is true. */
2025 static int
2026 contains_replace_regs (rtx x)
2028 int i, j;
2029 const char *fmt;
2030 enum rtx_code code = GET_CODE (x);
2032 switch (code)
2034 case CONST_INT:
2035 case CONST:
2036 case LABEL_REF:
2037 case SYMBOL_REF:
2038 case CONST_DOUBLE:
2039 case CONST_FIXED:
2040 case CONST_VECTOR:
2041 case PC:
2042 case CC0:
2043 case HIGH:
2044 return 0;
2046 case REG:
2047 return reg_equiv[REGNO (x)].replace;
2049 default:
2050 break;
2053 fmt = GET_RTX_FORMAT (code);
2054 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2055 switch (fmt[i])
2057 case 'e':
2058 if (contains_replace_regs (XEXP (x, i)))
2059 return 1;
2060 break;
2061 case 'E':
2062 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2063 if (contains_replace_regs (XVECEXP (x, i, j)))
2064 return 1;
2065 break;
2068 return 0;
2071 /* TRUE if X references a memory location that would be affected by a store
2072 to MEMREF. */
2073 static int
2074 memref_referenced_p (rtx memref, rtx x)
2076 int i, j;
2077 const char *fmt;
2078 enum rtx_code code = GET_CODE (x);
2080 switch (code)
2082 case CONST_INT:
2083 case CONST:
2084 case LABEL_REF:
2085 case SYMBOL_REF:
2086 case CONST_DOUBLE:
2087 case CONST_FIXED:
2088 case CONST_VECTOR:
2089 case PC:
2090 case CC0:
2091 case HIGH:
2092 case LO_SUM:
2093 return 0;
2095 case REG:
2096 return (reg_equiv[REGNO (x)].replacement
2097 && memref_referenced_p (memref,
2098 reg_equiv[REGNO (x)].replacement));
2100 case MEM:
2101 if (true_dependence (memref, VOIDmode, x, rtx_varies_p))
2102 return 1;
2103 break;
2105 case SET:
2106 /* If we are setting a MEM, it doesn't count (its address does), but any
2107 other SET_DEST that has a MEM in it is referencing the MEM. */
2108 if (MEM_P (SET_DEST (x)))
2110 if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
2111 return 1;
2113 else if (memref_referenced_p (memref, SET_DEST (x)))
2114 return 1;
2116 return memref_referenced_p (memref, SET_SRC (x));
2118 default:
2119 break;
2122 fmt = GET_RTX_FORMAT (code);
2123 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2124 switch (fmt[i])
2126 case 'e':
2127 if (memref_referenced_p (memref, XEXP (x, i)))
2128 return 1;
2129 break;
2130 case 'E':
2131 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2132 if (memref_referenced_p (memref, XVECEXP (x, i, j)))
2133 return 1;
2134 break;
2137 return 0;
2140 /* TRUE if some insn in the range (START, END] references a memory location
2141 that would be affected by a store to MEMREF. */
2142 static int
2143 memref_used_between_p (rtx memref, rtx start, rtx end)
2145 rtx insn;
2147 for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2148 insn = NEXT_INSN (insn))
2150 if (!NONDEBUG_INSN_P (insn))
2151 continue;
2153 if (memref_referenced_p (memref, PATTERN (insn)))
2154 return 1;
2156 /* Nonconst functions may access memory. */
2157 if (CALL_P (insn) && (! RTL_CONST_CALL_P (insn)))
2158 return 1;
2161 return 0;
2164 /* Mark REG as having no known equivalence.
2165 Some instructions might have been processed before and furnished
2166 with REG_EQUIV notes for this register; these notes will have to be
2167 removed.
2168 STORE is the piece of RTL that does the non-constant / conflicting
2169 assignment - a SET, CLOBBER or REG_INC note. It is currently not used,
2170 but needs to be there because this function is called from note_stores. */
2171 static void
2172 no_equiv (rtx reg, const_rtx store ATTRIBUTE_UNUSED, void *data ATTRIBUTE_UNUSED)
2174 int regno;
2175 rtx list;
2177 if (!REG_P (reg))
2178 return;
2179 regno = REGNO (reg);
2180 list = reg_equiv[regno].init_insns;
2181 if (list == const0_rtx)
2182 return;
2183 reg_equiv[regno].init_insns = const0_rtx;
2184 reg_equiv[regno].replacement = NULL_RTX;
2185 /* This doesn't matter for equivalences made for argument registers, we
2186 should keep their initialization insns. */
2187 if (reg_equiv[regno].is_arg_equivalence)
2188 return;
2189 reg_equiv_init[regno] = NULL_RTX;
2190 for (; list; list = XEXP (list, 1))
2192 rtx insn = XEXP (list, 0);
2193 remove_note (insn, find_reg_note (insn, REG_EQUIV, NULL_RTX));
2197 /* In DEBUG_INSN location adjust REGs from CLEARED_REGS bitmap to the
2198 equivalent replacement. */
2200 static rtx
2201 adjust_cleared_regs (rtx loc, const_rtx old_rtx ATTRIBUTE_UNUSED, void *data)
2203 if (REG_P (loc))
2205 bitmap cleared_regs = (bitmap) data;
2206 if (bitmap_bit_p (cleared_regs, REGNO (loc)))
2207 return simplify_replace_fn_rtx (*reg_equiv[REGNO (loc)].src_p,
2208 NULL_RTX, adjust_cleared_regs, data);
2210 return NULL_RTX;
2213 /* Nonzero if we recorded an equivalence for a LABEL_REF. */
2214 static int recorded_label_ref;
2216 /* Find registers that are equivalent to a single value throughout the
2217 compilation (either because they can be referenced in memory or are set once
2218 from a single constant). Lower their priority for a register.
2220 If such a register is only referenced once, try substituting its value
2221 into the using insn. If it succeeds, we can eliminate the register
2222 completely.
2224 Initialize the REG_EQUIV_INIT array of initializing insns.
2226 Return non-zero if jump label rebuilding should be done. */
2227 static int
2228 update_equiv_regs (void)
2230 rtx insn;
2231 basic_block bb;
2232 int loop_depth;
2233 bitmap cleared_regs;
2235 /* We need to keep track of whether or not we recorded a LABEL_REF so
2236 that we know if the jump optimizer needs to be rerun. */
2237 recorded_label_ref = 0;
2239 reg_equiv = XCNEWVEC (struct equivalence, max_regno);
2240 reg_equiv_init = ggc_alloc_cleared_vec_rtx (max_regno);
2241 reg_equiv_init_size = max_regno;
2243 init_alias_analysis ();
2245 /* Scan the insns and find which registers have equivalences. Do this
2246 in a separate scan of the insns because (due to -fcse-follow-jumps)
2247 a register can be set below its use. */
2248 FOR_EACH_BB (bb)
2250 loop_depth = bb->loop_depth;
2252 for (insn = BB_HEAD (bb);
2253 insn != NEXT_INSN (BB_END (bb));
2254 insn = NEXT_INSN (insn))
2256 rtx note;
2257 rtx set;
2258 rtx dest, src;
2259 int regno;
2261 if (! INSN_P (insn))
2262 continue;
2264 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2265 if (REG_NOTE_KIND (note) == REG_INC)
2266 no_equiv (XEXP (note, 0), note, NULL);
2268 set = single_set (insn);
2270 /* If this insn contains more (or less) than a single SET,
2271 only mark all destinations as having no known equivalence. */
2272 if (set == 0)
2274 note_stores (PATTERN (insn), no_equiv, NULL);
2275 continue;
2277 else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2279 int i;
2281 for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
2283 rtx part = XVECEXP (PATTERN (insn), 0, i);
2284 if (part != set)
2285 note_stores (part, no_equiv, NULL);
2289 dest = SET_DEST (set);
2290 src = SET_SRC (set);
2292 /* See if this is setting up the equivalence between an argument
2293 register and its stack slot. */
2294 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
2295 if (note)
2297 gcc_assert (REG_P (dest));
2298 regno = REGNO (dest);
2300 /* Note that we don't want to clear reg_equiv_init even if there
2301 are multiple sets of this register. */
2302 reg_equiv[regno].is_arg_equivalence = 1;
2304 /* Record for reload that this is an equivalencing insn. */
2305 if (rtx_equal_p (src, XEXP (note, 0)))
2306 reg_equiv_init[regno]
2307 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[regno]);
2309 /* Continue normally in case this is a candidate for
2310 replacements. */
2313 if (!optimize)
2314 continue;
2316 /* We only handle the case of a pseudo register being set
2317 once, or always to the same value. */
2318 /* ??? The mn10200 port breaks if we add equivalences for
2319 values that need an ADDRESS_REGS register and set them equivalent
2320 to a MEM of a pseudo. The actual problem is in the over-conservative
2321 handling of INPADDR_ADDRESS / INPUT_ADDRESS / INPUT triples in
2322 calculate_needs, but we traditionally work around this problem
2323 here by rejecting equivalences when the destination is in a register
2324 that's likely spilled. This is fragile, of course, since the
2325 preferred class of a pseudo depends on all instructions that set
2326 or use it. */
2328 if (!REG_P (dest)
2329 || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
2330 || reg_equiv[regno].init_insns == const0_rtx
2331 || (CLASS_LIKELY_SPILLED_P (reg_preferred_class (regno))
2332 && MEM_P (src) && ! reg_equiv[regno].is_arg_equivalence))
2334 /* This might be setting a SUBREG of a pseudo, a pseudo that is
2335 also set somewhere else to a constant. */
2336 note_stores (set, no_equiv, NULL);
2337 continue;
2340 note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
2342 /* cse sometimes generates function invariants, but doesn't put a
2343 REG_EQUAL note on the insn. Since this note would be redundant,
2344 there's no point creating it earlier than here. */
2345 if (! note && ! rtx_varies_p (src, 0))
2346 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
2348 /* Don't bother considering a REG_EQUAL note containing an EXPR_LIST
2349 since it represents a function call */
2350 if (note && GET_CODE (XEXP (note, 0)) == EXPR_LIST)
2351 note = NULL_RTX;
2353 if (DF_REG_DEF_COUNT (regno) != 1
2354 && (! note
2355 || rtx_varies_p (XEXP (note, 0), 0)
2356 || (reg_equiv[regno].replacement
2357 && ! rtx_equal_p (XEXP (note, 0),
2358 reg_equiv[regno].replacement))))
2360 no_equiv (dest, set, NULL);
2361 continue;
2363 /* Record this insn as initializing this register. */
2364 reg_equiv[regno].init_insns
2365 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv[regno].init_insns);
2367 /* If this register is known to be equal to a constant, record that
2368 it is always equivalent to the constant. */
2369 if (DF_REG_DEF_COUNT (regno) == 1
2370 && note && ! rtx_varies_p (XEXP (note, 0), 0))
2372 rtx note_value = XEXP (note, 0);
2373 remove_note (insn, note);
2374 set_unique_reg_note (insn, REG_EQUIV, note_value);
2377 /* If this insn introduces a "constant" register, decrease the priority
2378 of that register. Record this insn if the register is only used once
2379 more and the equivalence value is the same as our source.
2381 The latter condition is checked for two reasons: First, it is an
2382 indication that it may be more efficient to actually emit the insn
2383 as written (if no registers are available, reload will substitute
2384 the equivalence). Secondly, it avoids problems with any registers
2385 dying in this insn whose death notes would be missed.
2387 If we don't have a REG_EQUIV note, see if this insn is loading
2388 a register used only in one basic block from a MEM. If so, and the
2389 MEM remains unchanged for the life of the register, add a REG_EQUIV
2390 note. */
2392 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
2394 if (note == 0 && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
2395 && MEM_P (SET_SRC (set))
2396 && validate_equiv_mem (insn, dest, SET_SRC (set)))
2397 note = set_unique_reg_note (insn, REG_EQUIV, copy_rtx (SET_SRC (set)));
2399 if (note)
2401 int regno = REGNO (dest);
2402 rtx x = XEXP (note, 0);
2404 /* If we haven't done so, record for reload that this is an
2405 equivalencing insn. */
2406 if (!reg_equiv[regno].is_arg_equivalence)
2407 reg_equiv_init[regno]
2408 = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[regno]);
2410 /* Record whether or not we created a REG_EQUIV note for a LABEL_REF.
2411 We might end up substituting the LABEL_REF for uses of the
2412 pseudo here or later. That kind of transformation may turn an
2413 indirect jump into a direct jump, in which case we must rerun the
2414 jump optimizer to ensure that the JUMP_LABEL fields are valid. */
2415 if (GET_CODE (x) == LABEL_REF
2416 || (GET_CODE (x) == CONST
2417 && GET_CODE (XEXP (x, 0)) == PLUS
2418 && (GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF)))
2419 recorded_label_ref = 1;
2421 reg_equiv[regno].replacement = x;
2422 reg_equiv[regno].src_p = &SET_SRC (set);
2423 reg_equiv[regno].loop_depth = loop_depth;
2425 /* Don't mess with things live during setjmp. */
2426 if (REG_LIVE_LENGTH (regno) >= 0 && optimize)
2428 /* Note that the statement below does not affect the priority
2429 in local-alloc! */
2430 REG_LIVE_LENGTH (regno) *= 2;
2432 /* If the register is referenced exactly twice, meaning it is
2433 set once and used once, indicate that the reference may be
2434 replaced by the equivalence we computed above. Do this
2435 even if the register is only used in one block so that
2436 dependencies can be handled where the last register is
2437 used in a different block (i.e. HIGH / LO_SUM sequences)
2438 and to reduce the number of registers alive across
2439 calls. */
2441 if (REG_N_REFS (regno) == 2
2442 && (rtx_equal_p (x, src)
2443 || ! equiv_init_varies_p (src))
2444 && NONJUMP_INSN_P (insn)
2445 && equiv_init_movable_p (PATTERN (insn), regno))
2446 reg_equiv[regno].replace = 1;
2452 if (!optimize)
2453 goto out;
2455 /* A second pass, to gather additional equivalences with memory. This needs
2456 to be done after we know which registers we are going to replace. */
2458 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2460 rtx set, src, dest;
2461 unsigned regno;
2463 if (! INSN_P (insn))
2464 continue;
2466 set = single_set (insn);
2467 if (! set)
2468 continue;
2470 dest = SET_DEST (set);
2471 src = SET_SRC (set);
2473 /* If this sets a MEM to the contents of a REG that is only used
2474 in a single basic block, see if the register is always equivalent
2475 to that memory location and if moving the store from INSN to the
2476 insn that set REG is safe. If so, put a REG_EQUIV note on the
2477 initializing insn.
2479 Don't add a REG_EQUIV note if the insn already has one. The existing
2480 REG_EQUIV is likely more useful than the one we are adding.
2482 If one of the regs in the address has reg_equiv[REGNO].replace set,
2483 then we can't add this REG_EQUIV note. The reg_equiv[REGNO].replace
2484 optimization may move the set of this register immediately before
2485 insn, which puts it after reg_equiv[REGNO].init_insns, and hence
2486 the mention in the REG_EQUIV note would be to an uninitialized
2487 pseudo. */
2489 if (MEM_P (dest) && REG_P (src)
2490 && (regno = REGNO (src)) >= FIRST_PSEUDO_REGISTER
2491 && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
2492 && DF_REG_DEF_COUNT (regno) == 1
2493 && reg_equiv[regno].init_insns != 0
2494 && reg_equiv[regno].init_insns != const0_rtx
2495 && ! find_reg_note (XEXP (reg_equiv[regno].init_insns, 0),
2496 REG_EQUIV, NULL_RTX)
2497 && ! contains_replace_regs (XEXP (dest, 0)))
2499 rtx init_insn = XEXP (reg_equiv[regno].init_insns, 0);
2500 if (validate_equiv_mem (init_insn, src, dest)
2501 && ! memref_used_between_p (dest, init_insn, insn)
2502 /* Attaching a REG_EQUIV note will fail if INIT_INSN has
2503 multiple sets. */
2504 && set_unique_reg_note (init_insn, REG_EQUIV, copy_rtx (dest)))
2506 /* This insn makes the equivalence, not the one initializing
2507 the register. */
2508 reg_equiv_init[regno]
2509 = gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
2510 df_notes_rescan (init_insn);
2515 cleared_regs = BITMAP_ALLOC (NULL);
2516 /* Now scan all regs killed in an insn to see if any of them are
2517 registers only used that once. If so, see if we can replace the
2518 reference with the equivalent form. If we can, delete the
2519 initializing reference and this register will go away. If we
2520 can't replace the reference, and the initializing reference is
2521 within the same loop (or in an inner loop), then move the register
2522 initialization just before the use, so that they are in the same
2523 basic block. */
2524 FOR_EACH_BB_REVERSE (bb)
2526 loop_depth = bb->loop_depth;
2527 for (insn = BB_END (bb);
2528 insn != PREV_INSN (BB_HEAD (bb));
2529 insn = PREV_INSN (insn))
2531 rtx link;
2533 if (! INSN_P (insn))
2534 continue;
2536 /* Don't substitute into a non-local goto, this confuses CFG. */
2537 if (JUMP_P (insn)
2538 && find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
2539 continue;
2541 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2543 if (REG_NOTE_KIND (link) == REG_DEAD
2544 /* Make sure this insn still refers to the register. */
2545 && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
2547 int regno = REGNO (XEXP (link, 0));
2548 rtx equiv_insn;
2550 if (! reg_equiv[regno].replace
2551 || reg_equiv[regno].loop_depth < loop_depth)
2552 continue;
2554 /* reg_equiv[REGNO].replace gets set only when
2555 REG_N_REFS[REGNO] is 2, i.e. the register is set
2556 once and used once. (If it were only set, but not used,
2557 flow would have deleted the setting insns.) Hence
2558 there can only be one insn in reg_equiv[REGNO].init_insns. */
2559 gcc_assert (reg_equiv[regno].init_insns
2560 && !XEXP (reg_equiv[regno].init_insns, 1));
2561 equiv_insn = XEXP (reg_equiv[regno].init_insns, 0);
2563 /* We may not move instructions that can throw, since
2564 that changes basic block boundaries and we are not
2565 prepared to adjust the CFG to match. */
2566 if (can_throw_internal (equiv_insn))
2567 continue;
2569 if (asm_noperands (PATTERN (equiv_insn)) < 0
2570 && validate_replace_rtx (regno_reg_rtx[regno],
2571 *(reg_equiv[regno].src_p), insn))
2573 rtx equiv_link;
2574 rtx last_link;
2575 rtx note;
2577 /* Find the last note. */
2578 for (last_link = link; XEXP (last_link, 1);
2579 last_link = XEXP (last_link, 1))
2582 /* Append the REG_DEAD notes from equiv_insn. */
2583 equiv_link = REG_NOTES (equiv_insn);
2584 while (equiv_link)
2586 note = equiv_link;
2587 equiv_link = XEXP (equiv_link, 1);
2588 if (REG_NOTE_KIND (note) == REG_DEAD)
2590 remove_note (equiv_insn, note);
2591 XEXP (last_link, 1) = note;
2592 XEXP (note, 1) = NULL_RTX;
2593 last_link = note;
2597 remove_death (regno, insn);
2598 SET_REG_N_REFS (regno, 0);
2599 REG_FREQ (regno) = 0;
2600 delete_insn (equiv_insn);
2602 reg_equiv[regno].init_insns
2603 = XEXP (reg_equiv[regno].init_insns, 1);
2605 reg_equiv_init[regno] = NULL_RTX;
2606 bitmap_set_bit (cleared_regs, regno);
2608 /* Move the initialization of the register to just before
2609 INSN. Update the flow information. */
2610 else if (prev_nondebug_insn (insn) != equiv_insn)
2612 rtx new_insn;
2614 new_insn = emit_insn_before (PATTERN (equiv_insn), insn);
2615 REG_NOTES (new_insn) = REG_NOTES (equiv_insn);
2616 REG_NOTES (equiv_insn) = 0;
2617 /* Rescan it to process the notes. */
2618 df_insn_rescan (new_insn);
2620 /* Make sure this insn is recognized before
2621 reload begins, otherwise
2622 eliminate_regs_in_insn will die. */
2623 INSN_CODE (new_insn) = INSN_CODE (equiv_insn);
2625 delete_insn (equiv_insn);
2627 XEXP (reg_equiv[regno].init_insns, 0) = new_insn;
2629 REG_BASIC_BLOCK (regno) = bb->index;
2630 REG_N_CALLS_CROSSED (regno) = 0;
2631 REG_FREQ_CALLS_CROSSED (regno) = 0;
2632 REG_N_THROWING_CALLS_CROSSED (regno) = 0;
2633 REG_LIVE_LENGTH (regno) = 2;
2635 if (insn == BB_HEAD (bb))
2636 BB_HEAD (bb) = PREV_INSN (insn);
2638 reg_equiv_init[regno]
2639 = gen_rtx_INSN_LIST (VOIDmode, new_insn, NULL_RTX);
2640 bitmap_set_bit (cleared_regs, regno);
2647 if (!bitmap_empty_p (cleared_regs))
2649 FOR_EACH_BB (bb)
2651 bitmap_and_compl_into (DF_LIVE_IN (bb), cleared_regs);
2652 bitmap_and_compl_into (DF_LIVE_OUT (bb), cleared_regs);
2653 bitmap_and_compl_into (DF_LR_IN (bb), cleared_regs);
2654 bitmap_and_compl_into (DF_LR_OUT (bb), cleared_regs);
2657 /* Last pass - adjust debug insns referencing cleared regs. */
2658 if (MAY_HAVE_DEBUG_INSNS)
2659 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2660 if (DEBUG_INSN_P (insn))
2662 rtx old_loc = INSN_VAR_LOCATION_LOC (insn);
2663 INSN_VAR_LOCATION_LOC (insn)
2664 = simplify_replace_fn_rtx (old_loc, NULL_RTX,
2665 adjust_cleared_regs,
2666 (void *) cleared_regs);
2667 if (old_loc != INSN_VAR_LOCATION_LOC (insn))
2668 df_insn_rescan (insn);
2672 BITMAP_FREE (cleared_regs);
2674 out:
2675 /* Clean up. */
2677 end_alias_analysis ();
2678 free (reg_equiv);
2679 return recorded_label_ref;
2684 /* Print chain C to FILE. */
2685 static void
2686 print_insn_chain (FILE *file, struct insn_chain *c)
2688 fprintf (file, "insn=%d, ", INSN_UID(c->insn));
2689 bitmap_print (file, &c->live_throughout, "live_throughout: ", ", ");
2690 bitmap_print (file, &c->dead_or_set, "dead_or_set: ", "\n");
2694 /* Print all reload_insn_chains to FILE. */
2695 static void
2696 print_insn_chains (FILE *file)
2698 struct insn_chain *c;
2699 for (c = reload_insn_chain; c ; c = c->next)
2700 print_insn_chain (file, c);
2703 /* Return true if pseudo REGNO should be added to set live_throughout
2704 or dead_or_set of the insn chains for reload consideration. */
2705 static bool
2706 pseudo_for_reload_consideration_p (int regno)
2708 /* Consider spilled pseudos too for IRA because they still have a
2709 chance to get hard-registers in the reload when IRA is used. */
2710 return (reg_renumber[regno] >= 0
2711 || (ira_conflicts_p && flag_ira_share_spill_slots));
2714 /* Init LIVE_SUBREGS[ALLOCNUM] and LIVE_SUBREGS_USED[ALLOCNUM] using
2715 REG to the number of nregs, and INIT_VALUE to get the
2716 initialization. ALLOCNUM need not be the regno of REG. */
2717 static void
2718 init_live_subregs (bool init_value, sbitmap *live_subregs,
2719 int *live_subregs_used, int allocnum, rtx reg)
2721 unsigned int regno = REGNO (SUBREG_REG (reg));
2722 int size = GET_MODE_SIZE (GET_MODE (regno_reg_rtx[regno]));
2724 gcc_assert (size > 0);
2726 /* Been there, done that. */
2727 if (live_subregs_used[allocnum])
2728 return;
2730 /* Create a new one with zeros. */
2731 if (live_subregs[allocnum] == NULL)
2732 live_subregs[allocnum] = sbitmap_alloc (size);
2734 /* If the entire reg was live before blasting into subregs, we need
2735 to init all of the subregs to ones else init to 0. */
2736 if (init_value)
2737 sbitmap_ones (live_subregs[allocnum]);
2738 else
2739 sbitmap_zero (live_subregs[allocnum]);
2741 /* Set the number of bits that we really want. */
2742 live_subregs_used[allocnum] = size;
2745 /* Walk the insns of the current function and build reload_insn_chain,
2746 and record register life information. */
2747 static void
2748 build_insn_chain (void)
2750 unsigned int i;
2751 struct insn_chain **p = &reload_insn_chain;
2752 basic_block bb;
2753 struct insn_chain *c = NULL;
2754 struct insn_chain *next = NULL;
2755 bitmap live_relevant_regs = BITMAP_ALLOC (NULL);
2756 bitmap elim_regset = BITMAP_ALLOC (NULL);
2757 /* live_subregs is a vector used to keep accurate information about
2758 which hardregs are live in multiword pseudos. live_subregs and
2759 live_subregs_used are indexed by pseudo number. The live_subreg
2760 entry for a particular pseudo is only used if the corresponding
2761 element is non zero in live_subregs_used. The value in
2762 live_subregs_used is number of bytes that the pseudo can
2763 occupy. */
2764 sbitmap *live_subregs = XCNEWVEC (sbitmap, max_regno);
2765 int *live_subregs_used = XNEWVEC (int, max_regno);
2767 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2768 if (TEST_HARD_REG_BIT (eliminable_regset, i))
2769 bitmap_set_bit (elim_regset, i);
2770 FOR_EACH_BB_REVERSE (bb)
2772 bitmap_iterator bi;
2773 rtx insn;
2775 CLEAR_REG_SET (live_relevant_regs);
2776 memset (live_subregs_used, 0, max_regno * sizeof (int));
2778 EXECUTE_IF_SET_IN_BITMAP (DF_LR_OUT (bb), 0, i, bi)
2780 if (i >= FIRST_PSEUDO_REGISTER)
2781 break;
2782 bitmap_set_bit (live_relevant_regs, i);
2785 EXECUTE_IF_SET_IN_BITMAP (DF_LR_OUT (bb),
2786 FIRST_PSEUDO_REGISTER, i, bi)
2788 if (pseudo_for_reload_consideration_p (i))
2789 bitmap_set_bit (live_relevant_regs, i);
2792 FOR_BB_INSNS_REVERSE (bb, insn)
2794 if (!NOTE_P (insn) && !BARRIER_P (insn))
2796 unsigned int uid = INSN_UID (insn);
2797 df_ref *def_rec;
2798 df_ref *use_rec;
2800 c = new_insn_chain ();
2801 c->next = next;
2802 next = c;
2803 *p = c;
2804 p = &c->prev;
2806 c->insn = insn;
2807 c->block = bb->index;
2809 if (INSN_P (insn))
2810 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
2812 df_ref def = *def_rec;
2813 unsigned int regno = DF_REF_REGNO (def);
2815 /* Ignore may clobbers because these are generated
2816 from calls. However, every other kind of def is
2817 added to dead_or_set. */
2818 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
2820 if (regno < FIRST_PSEUDO_REGISTER)
2822 if (!fixed_regs[regno])
2823 bitmap_set_bit (&c->dead_or_set, regno);
2825 else if (pseudo_for_reload_consideration_p (regno))
2826 bitmap_set_bit (&c->dead_or_set, regno);
2829 if ((regno < FIRST_PSEUDO_REGISTER
2830 || reg_renumber[regno] >= 0
2831 || ira_conflicts_p)
2832 && (!DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)))
2834 rtx reg = DF_REF_REG (def);
2836 /* We can model subregs, but not if they are
2837 wrapped in ZERO_EXTRACTS. */
2838 if (GET_CODE (reg) == SUBREG
2839 && !DF_REF_FLAGS_IS_SET (def, DF_REF_ZERO_EXTRACT))
2841 unsigned int start = SUBREG_BYTE (reg);
2842 unsigned int last = start
2843 + GET_MODE_SIZE (GET_MODE (reg));
2845 init_live_subregs
2846 (bitmap_bit_p (live_relevant_regs, regno),
2847 live_subregs, live_subregs_used, regno, reg);
2849 if (!DF_REF_FLAGS_IS_SET
2850 (def, DF_REF_STRICT_LOW_PART))
2852 /* Expand the range to cover entire words.
2853 Bytes added here are "don't care". */
2854 start
2855 = start / UNITS_PER_WORD * UNITS_PER_WORD;
2856 last = ((last + UNITS_PER_WORD - 1)
2857 / UNITS_PER_WORD * UNITS_PER_WORD);
2860 /* Ignore the paradoxical bits. */
2861 if ((int)last > live_subregs_used[regno])
2862 last = live_subregs_used[regno];
2864 while (start < last)
2866 RESET_BIT (live_subregs[regno], start);
2867 start++;
2870 if (sbitmap_empty_p (live_subregs[regno]))
2872 live_subregs_used[regno] = 0;
2873 bitmap_clear_bit (live_relevant_regs, regno);
2875 else
2876 /* Set live_relevant_regs here because
2877 that bit has to be true to get us to
2878 look at the live_subregs fields. */
2879 bitmap_set_bit (live_relevant_regs, regno);
2881 else
2883 /* DF_REF_PARTIAL is generated for
2884 subregs, STRICT_LOW_PART, and
2885 ZERO_EXTRACT. We handle the subreg
2886 case above so here we have to keep from
2887 modeling the def as a killing def. */
2888 if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL))
2890 bitmap_clear_bit (live_relevant_regs, regno);
2891 live_subregs_used[regno] = 0;
2897 bitmap_and_compl_into (live_relevant_regs, elim_regset);
2898 bitmap_copy (&c->live_throughout, live_relevant_regs);
2900 if (INSN_P (insn))
2901 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
2903 df_ref use = *use_rec;
2904 unsigned int regno = DF_REF_REGNO (use);
2905 rtx reg = DF_REF_REG (use);
2907 /* DF_REF_READ_WRITE on a use means that this use
2908 is fabricated from a def that is a partial set
2909 to a multiword reg. Here, we only model the
2910 subreg case that is not wrapped in ZERO_EXTRACT
2911 precisely so we do not need to look at the
2912 fabricated use. */
2913 if (DF_REF_FLAGS_IS_SET (use, DF_REF_READ_WRITE)
2914 && !DF_REF_FLAGS_IS_SET (use, DF_REF_ZERO_EXTRACT)
2915 && DF_REF_FLAGS_IS_SET (use, DF_REF_SUBREG))
2916 continue;
2918 /* Add the last use of each var to dead_or_set. */
2919 if (!bitmap_bit_p (live_relevant_regs, regno))
2921 if (regno < FIRST_PSEUDO_REGISTER)
2923 if (!fixed_regs[regno])
2924 bitmap_set_bit (&c->dead_or_set, regno);
2926 else if (pseudo_for_reload_consideration_p (regno))
2927 bitmap_set_bit (&c->dead_or_set, regno);
2930 if (regno < FIRST_PSEUDO_REGISTER
2931 || pseudo_for_reload_consideration_p (regno))
2933 if (GET_CODE (reg) == SUBREG
2934 && !DF_REF_FLAGS_IS_SET (use,
2935 DF_REF_SIGN_EXTRACT
2936 | DF_REF_ZERO_EXTRACT))
2938 unsigned int start = SUBREG_BYTE (reg);
2939 unsigned int last = start
2940 + GET_MODE_SIZE (GET_MODE (reg));
2942 init_live_subregs
2943 (bitmap_bit_p (live_relevant_regs, regno),
2944 live_subregs, live_subregs_used, regno, reg);
2946 /* Ignore the paradoxical bits. */
2947 if ((int)last > live_subregs_used[regno])
2948 last = live_subregs_used[regno];
2950 while (start < last)
2952 SET_BIT (live_subregs[regno], start);
2953 start++;
2956 else
2957 /* Resetting the live_subregs_used is
2958 effectively saying do not use the subregs
2959 because we are reading the whole
2960 pseudo. */
2961 live_subregs_used[regno] = 0;
2962 bitmap_set_bit (live_relevant_regs, regno);
2968 /* FIXME!! The following code is a disaster. Reload needs to see the
2969 labels and jump tables that are just hanging out in between
2970 the basic blocks. See pr33676. */
2971 insn = BB_HEAD (bb);
2973 /* Skip over the barriers and cruft. */
2974 while (insn && (BARRIER_P (insn) || NOTE_P (insn)
2975 || BLOCK_FOR_INSN (insn) == bb))
2976 insn = PREV_INSN (insn);
2978 /* While we add anything except barriers and notes, the focus is
2979 to get the labels and jump tables into the
2980 reload_insn_chain. */
2981 while (insn)
2983 if (!NOTE_P (insn) && !BARRIER_P (insn))
2985 if (BLOCK_FOR_INSN (insn))
2986 break;
2988 c = new_insn_chain ();
2989 c->next = next;
2990 next = c;
2991 *p = c;
2992 p = &c->prev;
2994 /* The block makes no sense here, but it is what the old
2995 code did. */
2996 c->block = bb->index;
2997 c->insn = insn;
2998 bitmap_copy (&c->live_throughout, live_relevant_regs);
3000 insn = PREV_INSN (insn);
3004 for (i = 0; i < (unsigned int) max_regno; i++)
3005 if (live_subregs[i])
3006 free (live_subregs[i]);
3008 reload_insn_chain = c;
3009 *p = NULL;
3011 free (live_subregs);
3012 free (live_subregs_used);
3013 BITMAP_FREE (live_relevant_regs);
3014 BITMAP_FREE (elim_regset);
3016 if (dump_file)
3017 print_insn_chains (dump_file);
3020 /* Allocate memory for reg_equiv_memory_loc. */
3021 static void
3022 init_reg_equiv_memory_loc (void)
3024 max_regno = max_reg_num ();
3026 /* And the reg_equiv_memory_loc array. */
3027 VEC_safe_grow (rtx, gc, reg_equiv_memory_loc_vec, max_regno);
3028 memset (VEC_address (rtx, reg_equiv_memory_loc_vec), 0,
3029 sizeof (rtx) * max_regno);
3030 reg_equiv_memory_loc = VEC_address (rtx, reg_equiv_memory_loc_vec);
3033 /* All natural loops. */
3034 struct loops ira_loops;
3036 /* True if we have allocno conflicts. It is false for non-optimized
3037 mode or when the conflict table is too big. */
3038 bool ira_conflicts_p;
3040 /* This is the main entry of IRA. */
3041 static void
3042 ira (FILE *f)
3044 int overall_cost_before, allocated_reg_info_size;
3045 bool loops_p;
3046 int max_regno_before_ira, ira_max_point_before_emit;
3047 int rebuild_p;
3048 int saved_flag_ira_share_spill_slots;
3049 basic_block bb;
3051 timevar_push (TV_IRA);
3053 if (flag_caller_saves)
3054 init_caller_save ();
3056 if (flag_ira_verbose < 10)
3058 internal_flag_ira_verbose = flag_ira_verbose;
3059 ira_dump_file = f;
3061 else
3063 internal_flag_ira_verbose = flag_ira_verbose - 10;
3064 ira_dump_file = stderr;
3067 ira_conflicts_p = optimize > 0;
3068 setup_prohibited_mode_move_regs ();
3070 df_note_add_problem ();
3072 if (optimize == 1)
3074 df_live_add_problem ();
3075 df_live_set_all_dirty ();
3077 #ifdef ENABLE_CHECKING
3078 df->changeable_flags |= DF_VERIFY_SCHEDULED;
3079 #endif
3080 df_analyze ();
3081 df_clear_flags (DF_NO_INSN_RESCAN);
3082 regstat_init_n_sets_and_refs ();
3083 regstat_compute_ri ();
3085 /* If we are not optimizing, then this is the only place before
3086 register allocation where dataflow is done. And that is needed
3087 to generate these warnings. */
3088 if (warn_clobbered)
3089 generate_setjmp_warnings ();
3091 /* Determine if the current function is a leaf before running IRA
3092 since this can impact optimizations done by the prologue and
3093 epilogue thus changing register elimination offsets. */
3094 current_function_is_leaf = leaf_function_p ();
3096 if (resize_reg_info () && flag_ira_loop_pressure)
3097 ira_set_pseudo_classes (ira_dump_file);
3099 rebuild_p = update_equiv_regs ();
3101 #ifndef IRA_NO_OBSTACK
3102 gcc_obstack_init (&ira_obstack);
3103 #endif
3104 bitmap_obstack_initialize (&ira_bitmap_obstack);
3105 if (optimize)
3107 max_regno = max_reg_num ();
3108 ira_reg_equiv_len = max_regno;
3109 ira_reg_equiv_invariant_p
3110 = (bool *) ira_allocate (max_regno * sizeof (bool));
3111 memset (ira_reg_equiv_invariant_p, 0, max_regno * sizeof (bool));
3112 ira_reg_equiv_const = (rtx *) ira_allocate (max_regno * sizeof (rtx));
3113 memset (ira_reg_equiv_const, 0, max_regno * sizeof (rtx));
3114 find_reg_equiv_invariant_const ();
3115 if (rebuild_p)
3117 timevar_push (TV_JUMP);
3118 rebuild_jump_labels (get_insns ());
3119 purge_all_dead_edges ();
3120 timevar_pop (TV_JUMP);
3124 max_regno_before_ira = allocated_reg_info_size = max_reg_num ();
3125 ira_setup_eliminable_regset ();
3127 ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
3128 ira_load_cost = ira_store_cost = ira_shuffle_cost = 0;
3129 ira_move_loops_num = ira_additional_jumps_num = 0;
3131 ira_assert (current_loops == NULL);
3132 flow_loops_find (&ira_loops);
3133 record_loop_exits ();
3134 current_loops = &ira_loops;
3136 init_reg_equiv_memory_loc ();
3138 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3139 fprintf (ira_dump_file, "Building IRA IR\n");
3140 loops_p = ira_build (optimize
3141 && (flag_ira_region == IRA_REGION_ALL
3142 || flag_ira_region == IRA_REGION_MIXED));
3144 ira_assert (ira_conflicts_p || !loops_p);
3146 saved_flag_ira_share_spill_slots = flag_ira_share_spill_slots;
3147 if (too_high_register_pressure_p ())
3148 /* It is just wasting compiler's time to pack spilled pseudos into
3149 stack slots in this case -- prohibit it. */
3150 flag_ira_share_spill_slots = FALSE;
3152 ira_color ();
3154 ira_max_point_before_emit = ira_max_point;
3156 ira_emit (loops_p);
3158 if (ira_conflicts_p)
3160 max_regno = max_reg_num ();
3162 if (! loops_p)
3163 ira_initiate_assign ();
3164 else
3166 expand_reg_info (allocated_reg_info_size);
3167 setup_preferred_alternate_classes_for_new_pseudos
3168 (allocated_reg_info_size);
3169 allocated_reg_info_size = max_regno;
3171 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3172 fprintf (ira_dump_file, "Flattening IR\n");
3173 ira_flattening (max_regno_before_ira, ira_max_point_before_emit);
3174 /* New insns were generated: add notes and recalculate live
3175 info. */
3176 df_analyze ();
3178 flow_loops_find (&ira_loops);
3179 record_loop_exits ();
3180 current_loops = &ira_loops;
3182 setup_allocno_assignment_flags ();
3183 ira_initiate_assign ();
3184 ira_reassign_conflict_allocnos (max_regno);
3188 setup_reg_renumber ();
3190 calculate_allocation_cost ();
3192 #ifdef ENABLE_IRA_CHECKING
3193 if (ira_conflicts_p)
3194 check_allocation ();
3195 #endif
3197 delete_trivially_dead_insns (get_insns (), max_reg_num ());
3199 init_reg_equiv_memory_loc ();
3201 if (max_regno != max_regno_before_ira)
3203 regstat_free_n_sets_and_refs ();
3204 regstat_free_ri ();
3205 regstat_init_n_sets_and_refs ();
3206 regstat_compute_ri ();
3209 allocate_initial_values (reg_equiv_memory_loc);
3211 overall_cost_before = ira_overall_cost;
3212 if (ira_conflicts_p)
3214 fix_reg_equiv_init ();
3216 #ifdef ENABLE_IRA_CHECKING
3217 print_redundant_copies ();
3218 #endif
3220 ira_spilled_reg_stack_slots_num = 0;
3221 ira_spilled_reg_stack_slots
3222 = ((struct ira_spilled_reg_stack_slot *)
3223 ira_allocate (max_regno
3224 * sizeof (struct ira_spilled_reg_stack_slot)));
3225 memset (ira_spilled_reg_stack_slots, 0,
3226 max_regno * sizeof (struct ira_spilled_reg_stack_slot));
3229 timevar_pop (TV_IRA);
3231 timevar_push (TV_RELOAD);
3232 df_set_flags (DF_NO_INSN_RESCAN);
3233 build_insn_chain ();
3235 reload_completed = !reload (get_insns (), ira_conflicts_p);
3237 finish_subregs_of_mode ();
3239 timevar_pop (TV_RELOAD);
3241 timevar_push (TV_IRA);
3243 if (ira_conflicts_p)
3245 ira_free (ira_spilled_reg_stack_slots);
3247 ira_finish_assign ();
3250 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL
3251 && overall_cost_before != ira_overall_cost)
3252 fprintf (ira_dump_file, "+++Overall after reload %d\n", ira_overall_cost);
3253 ira_destroy ();
3255 flag_ira_share_spill_slots = saved_flag_ira_share_spill_slots;
3257 flow_loops_free (&ira_loops);
3258 free_dominance_info (CDI_DOMINATORS);
3259 FOR_ALL_BB (bb)
3260 bb->loop_father = NULL;
3261 current_loops = NULL;
3263 regstat_free_ri ();
3264 regstat_free_n_sets_and_refs ();
3266 if (optimize)
3268 cleanup_cfg (CLEANUP_EXPENSIVE);
3270 ira_free (ira_reg_equiv_invariant_p);
3271 ira_free (ira_reg_equiv_const);
3274 bitmap_obstack_release (&ira_bitmap_obstack);
3275 #ifndef IRA_NO_OBSTACK
3276 obstack_free (&ira_obstack, NULL);
3277 #endif
3279 /* The code after the reload has changed so much that at this point
3280 we might as well just rescan everything. Not that
3281 df_rescan_all_insns is not going to help here because it does not
3282 touch the artificial uses and defs. */
3283 df_finish_pass (true);
3284 if (optimize > 1)
3285 df_live_add_problem ();
3286 df_scan_alloc (NULL);
3287 df_scan_blocks ();
3289 if (optimize)
3290 df_analyze ();
3292 timevar_pop (TV_IRA);
3297 static bool
3298 gate_ira (void)
3300 return true;
3303 /* Run the integrated register allocator. */
3304 static unsigned int
3305 rest_of_handle_ira (void)
3307 ira (dump_file);
3308 return 0;
3311 struct rtl_opt_pass pass_ira =
3314 RTL_PASS,
3315 "ira", /* name */
3316 gate_ira, /* gate */
3317 rest_of_handle_ira, /* execute */
3318 NULL, /* sub */
3319 NULL, /* next */
3320 0, /* static_pass_number */
3321 TV_NONE, /* tv_id */
3322 0, /* properties_required */
3323 0, /* properties_provided */
3324 0, /* properties_destroyed */
3325 0, /* todo_flags_start */
3326 TODO_dump_func |
3327 TODO_ggc_collect /* todo_flags_finish */