2008-03-06 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ira.c
blobd8d8b7c63e02d771aab4fa2c03e662720f5e2246
1 /* Integrated Register Allocator entry point.
2 Copyright (C) 2006, 2007, 2008
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 /* The integrated register allocator (IRA) is called integrated
24 because register coalescing and register live range splitting are
25 done on-the-fly during coloring. Register coalescing is done by
26 hard register preferencing during hard register assigning. The
27 live range splitting is a byproduct of the regional register
28 allocation.
30 The regional allocation is top-down process. The first we do
31 allocation for all function then we improve it for loops then their
32 subloops and so on. To reduce register shuffling, the same
33 mechanism of hard register prefrencing is used. This approach
34 works as good as Callahan-Koblentz algorithm but it is simpler.
35 We use Chaitin-Briggs coloring for each loop (or function) with
36 optional biased coloring. If pseudo-registers got different
37 location on loop borders we rename them inside the loop and
38 generate pseudo-register move insns. Some optimizations (like
39 removing redundant stores, moving register shuffling to less
40 frequent points, and code duplication reducing) to minimize effect
41 of register shuffling is done
43 If we don't improve register allocation for loops we get classic
44 Chaitin-Briggs coloring (only instead of separate pass of
45 coalescing, we use hard register preferencing).
47 Optionally we implements Chow's priority coloring only for all
48 function. It is quite analogous to the current gcc global register
49 allocator only we use more sophisticated hard register
50 preferencing.
52 Literature is worth to read for better understanding the code:
54 o Preston Briggs, Keith D. Cooper, Linda Torczon. Improvements to
55 Graph Coloring Register Allocation.
57 o David Callahan, Brian Koblenz. Register allocation via
58 hierarchical graph coloring.
60 o Keith Cooper, Anshuman Dasgupta, Jason Eckhardt. Revisiting Graph
61 Coloring Register Allocation: A Study of the Chaitin-Briggs and
62 Callahan-Koblenz Algorithms.
64 o Guei-Yuan Lueh, Thomas Gross, and Ali-Reza Adl-Tabatabai. Global
65 Register Allocation Based on Graph Fusion.
70 #include "config.h"
71 #include "system.h"
72 #include "coretypes.h"
73 #include "tm.h"
74 #include "regs.h"
75 #include "rtl.h"
76 #include "tm_p.h"
77 #include "target.h"
78 #include "flags.h"
79 #include "obstack.h"
80 #include "bitmap.h"
81 #include "hard-reg-set.h"
82 #include "basic-block.h"
83 #include "expr.h"
84 #include "recog.h"
85 #include "params.h"
86 #include "timevar.h"
87 #include "tree-pass.h"
88 #include "output.h"
89 #include "reload.h"
90 #include "errors.h"
91 #include "integrate.h"
92 #include "df.h"
93 #include "ggc.h"
94 #include "ira-int.h"
96 static void setup_inner_mode (void);
97 static void setup_reg_mode_hard_regset (void);
98 static void setup_class_hard_regs (void);
99 static void setup_available_class_regs (void);
100 static void setup_alloc_regs (int);
101 static void setup_class_subset_and_memory_move_costs (void);
102 static void setup_reg_subclasses (void);
103 #ifdef IRA_COVER_CLASSES
104 static void setup_cover_classes (void);
105 static void setup_class_translate (void);
106 static void setup_reg_class_intersect_union (void);
107 #endif
108 static void print_class_cover (FILE *);
109 static void find_reg_class_closure (void);
110 static void setup_reg_class_nregs (void);
111 static void setup_prohibited_class_mode_regs (void);
112 static void setup_prohibited_mode_move_regs (void);
113 static int insn_contains_asm_1 (rtx *, void *);
114 static int insn_contains_asm (rtx);
115 static void compute_regs_asm_clobbered (char *);
116 static void setup_eliminable_regset (void);
117 static void find_reg_equiv_invariant_const (void);
118 static void setup_reg_renumber (void);
119 static void setup_allocno_assignment_flags (void);
120 static void calculate_allocation_cost (void);
121 #ifdef ENABLE_IRA_CHECKING
122 static void check_allocation (void);
123 #endif
124 static void fix_reg_equiv_init (void);
125 #ifdef ENABLE_IRA_CHECKING
126 static void print_redundant_copies (void);
127 #endif
128 static void setup_preferred_alternate_classes (void);
129 static void expand_reg_info (int);
131 static bool gate_ira (void);
132 static unsigned int rest_of_handle_ira (void);
134 /* The flag value used internally. */
135 int internal_flag_ira_verbose;
137 /* Dump file for IRA. */
138 FILE *ira_dump_file;
140 /* Pools for allocnos, copies, allocno live ranges. */
141 alloc_pool allocno_pool, copy_pool, allocno_live_range_pool;
143 /* The number of elements in the following array. */
144 int spilled_reg_stack_slots_num;
146 /* The following array contains description of spilled registers stack
147 slots have been used in the current function so far. */
148 struct spilled_reg_stack_slot *spilled_reg_stack_slots;
150 /* The following variable values are correspondingly overall cost of
151 the allocation, cost of hard register usage for the allocnos, cost
152 of memory usage for the allocnos, cost of loads, stores and register
153 move insns generated for register live range splitting. */
154 int overall_cost;
155 int reg_cost, mem_cost;
156 int load_cost, store_cost, shuffle_cost;
157 int move_loops_num, additional_jumps_num;
159 /* A mode whose value is immediately contained in given mode
160 value. */
161 unsigned char mode_inner_mode [NUM_MACHINE_MODES];
163 /* The following array is a map hard regs X modes -> number registers
164 for store value of given mode starting with given hard
165 register. */
166 HARD_REG_SET reg_mode_hard_regset [FIRST_PSEUDO_REGISTER] [NUM_MACHINE_MODES];
168 /* The following two variables are array analog of macros
169 MEMORY_MOVE_COST and REGISTER_MOVE_COST. */
170 short int memory_move_cost [MAX_MACHINE_MODE] [N_REG_CLASSES] [2];
171 move_table *register_move_cost [MAX_MACHINE_MODE];
173 /* Similar to move_cost, but here we don't have to move if the first
174 index is a subset of the second (taking registers available for
175 allocation into account) so in that case the cost is zero. */
176 move_table *register_may_move_in_cost [MAX_MACHINE_MODE];
178 /* Similar, but here we don't have to move if the first index is a
179 superset of the second (taking registers available for allocation
180 into account) so in that case the cost is zero. */
181 move_table *register_may_move_out_cost [MAX_MACHINE_MODE];
183 /* Nonzero value of element of the following array means that the
184 1st class is a subset of the 2nd class. */
185 int class_subset_p [N_REG_CLASSES] [N_REG_CLASSES];
187 /* Nonzero value of element of the following array means that the
188 1st class is a strict subset of the 2nd class. */
189 int strict_class_subset_p [N_REG_CLASSES] [N_REG_CLASSES];
191 /* Temporary hard reg set used for different calculation. */
192 static HARD_REG_SET temp_hard_regset;
196 /* The function sets up mode_inner_mode array. */
197 static void
198 setup_inner_mode (void)
200 int i;
201 enum machine_mode wider;
203 for (i = 0; i < NUM_MACHINE_MODES; i++)
204 mode_inner_mode [i] = VOIDmode;
205 for (i = 0; i < NUM_MACHINE_MODES; i++)
207 wider = GET_MODE_WIDER_MODE (i);
208 if (wider != VOIDmode)
210 ira_assert (mode_inner_mode [wider] == VOIDmode);
211 mode_inner_mode [wider] = i;
218 /* The function sets up map REG_MODE_HARD_REGSET. */
219 static void
220 setup_reg_mode_hard_regset (void)
222 int i, m, hard_regno;
224 for (m = 0; m < NUM_MACHINE_MODES; m++)
225 for (hard_regno = 0; hard_regno < FIRST_PSEUDO_REGISTER; hard_regno++)
227 CLEAR_HARD_REG_SET (reg_mode_hard_regset [hard_regno] [m]);
228 for (i = hard_regno_nregs [hard_regno] [m] - 1; i >= 0; i--)
229 if (hard_regno + i < FIRST_PSEUDO_REGISTER)
230 SET_HARD_REG_BIT (reg_mode_hard_regset [hard_regno] [m],
231 hard_regno + i);
237 /* Hard registers can not be used for the register allocator for all
238 functions of the current compile unit. */
239 static HARD_REG_SET no_unit_alloc_regs;
241 /* Hard registers which can be used for the allocation of given
242 register class. The order is defined by the allocation order. */
243 short class_hard_regs [N_REG_CLASSES] [FIRST_PSEUDO_REGISTER];
245 /* The size of the above array for given register class. */
246 int class_hard_regs_num [N_REG_CLASSES];
248 /* Index (in class_hard_regs) for given register class and hard
249 register (in general case a hard register can belong to several
250 register classes). */
251 short class_hard_reg_index [N_REG_CLASSES] [FIRST_PSEUDO_REGISTER];
253 /* The function sets up the three arrays declared above. */
254 static void
255 setup_class_hard_regs (void)
257 int cl, i, hard_regno, n;
258 HARD_REG_SET processed_hard_reg_set;
260 ira_assert (SHRT_MAX >= FIRST_PSEUDO_REGISTER);
261 /* We could call ORDER_REGS_FOR_LOCAL_ALLOC here (it is usually
262 putting hard callee-used hard registers first). But our
263 heuristics work better. */
264 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
266 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents [cl]);
267 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
268 CLEAR_HARD_REG_SET (processed_hard_reg_set);
269 for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
271 #ifdef REG_ALLOC_ORDER
272 hard_regno = reg_alloc_order [i];
273 #else
274 hard_regno = i;
275 #endif
276 if (TEST_HARD_REG_BIT (processed_hard_reg_set, hard_regno))
277 continue;
278 SET_HARD_REG_BIT (processed_hard_reg_set, hard_regno);
279 if (! TEST_HARD_REG_BIT (temp_hard_regset, hard_regno))
280 class_hard_reg_index [cl] [hard_regno] = -1;
281 else
283 class_hard_reg_index [cl] [hard_regno] = n;
284 class_hard_regs [cl] [n++] = hard_regno;
287 class_hard_regs_num [cl] = n;
291 /* Number of class hard registers available for the register
292 allocation for given classes. */
293 int available_class_regs [N_REG_CLASSES];
295 /* Function setting up AVAILABLE_CLASS_REGS. */
296 static void
297 setup_available_class_regs (void)
299 int i, j;
301 memset (available_class_regs, 0, sizeof (available_class_regs));
302 for (i = 0; i < N_REG_CLASSES; i++)
304 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents [i]);
305 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
306 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
307 if (TEST_HARD_REG_BIT (temp_hard_regset, j))
308 available_class_regs [i]++;
312 /* The function setting up different global variables defining hard
313 registers for the allocation. It depends on USE_HARD_FRAME_P whose
314 nonzero value means that we can use hard frame pointer for the
315 allocation. */
316 static void
317 setup_alloc_regs (int use_hard_frame_p)
319 COPY_HARD_REG_SET (no_unit_alloc_regs, fixed_reg_set);
320 if (! use_hard_frame_p)
321 SET_HARD_REG_BIT (no_unit_alloc_regs, HARD_FRAME_POINTER_REGNUM);
322 setup_class_hard_regs ();
323 setup_available_class_regs ();
328 /* The function sets up MEMORY_MOVE_COST, REGISTER_MOVE_COST and
329 CLASS_SUBSET_P and STRICT_CLASS_SUBSET_P. */
330 static void
331 setup_class_subset_and_memory_move_costs (void)
333 int cl, cl2;
334 enum machine_mode mode;
335 HARD_REG_SET temp_hard_regset2;
337 for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
339 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
341 memory_move_cost [mode] [cl] [0] = MEMORY_MOVE_COST (mode, cl, 0);
342 memory_move_cost [mode] [cl] [1] = MEMORY_MOVE_COST (mode, cl, 1);
345 for (cl2 = (int) N_REG_CLASSES - 1; cl2 >= 0; cl2--)
347 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents [cl]);
348 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
349 COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents [cl2]);
350 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
351 class_subset_p [cl] [cl2]
352 = hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2);
353 strict_class_subset_p [cl] [cl2] = class_subset_p [cl] [cl2];
354 if (class_subset_p [cl] [cl2]
355 && hard_reg_set_equal_p (temp_hard_regset, temp_hard_regset2))
356 strict_class_subset_p [cl] [cl2] = FALSE;
363 /* Define the following macro if allocation through malloc if
364 preferable. */
365 #define IRA_NO_OBSTACK
367 #ifndef IRA_NO_OBSTACK
368 /* Obstack used for storing all dynamic data (except bitmaps) of the
369 IRA. */
370 static struct obstack ira_obstack;
371 #endif
373 /* Obstack used for storing all bitmaps of the IRA. */
374 static struct bitmap_obstack ira_bitmap_obstack;
376 /* The function allocates memory of size LEN for IRA data. */
377 void *
378 ira_allocate (size_t len)
380 void *res;
382 #ifndef IRA_NO_OBSTACK
383 res = obstack_alloc (&ira_obstack, len);
384 #else
385 res = xmalloc (len);
386 #endif
387 return res;
390 /* The function reallocates memory PTR of size LEN for IRA data. */
391 void *
392 ira_reallocate (void *ptr, size_t len)
394 void *res;
396 #ifndef IRA_NO_OBSTACK
397 res = obstack_alloc (&ira_obstack, len);
398 #else
399 res = xrealloc (ptr, len);
400 #endif
401 return res;
404 /* The function free memory ADDR allocated for IRA data. */
405 void
406 ira_free (void *addr ATTRIBUTE_UNUSED)
408 #ifndef IRA_NO_OBSTACK
409 /* do nothing */
410 #else
411 free (addr);
412 #endif
416 /* The function allocates bitmap for IRA. */
417 bitmap
418 ira_allocate_bitmap (void)
420 return BITMAP_ALLOC (&ira_bitmap_obstack);
423 /* The function frees bitmap B allocated for IRA. */
424 void
425 ira_free_bitmap (bitmap b ATTRIBUTE_UNUSED)
427 /* do nothing */
430 /* The function allocates regset for IRA. */
431 regset
432 ira_allocate_regset (void)
434 return ALLOC_REG_SET (&ira_bitmap_obstack);
437 /* The function frees regset R allocated for IRA. */
438 void
439 ira_free_regset (regset r ATTRIBUTE_UNUSED)
441 /* do nothing */
446 /* The function outputs information about allocation of all allocnos
447 into file F. */
448 void
449 print_disposition (FILE *f)
451 int i, n, max_regno;
452 allocno_t a;
453 basic_block bb;
455 fprintf (f, "Disposition:");
456 max_regno = max_reg_num ();
457 for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
458 for (a = regno_allocno_map [i];
459 a != NULL;
460 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
462 if (n % 4 == 0)
463 fprintf (f, "\n");
464 n++;
465 fprintf (f, " %4d:r%-4d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
466 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
467 fprintf (f, "b%-3d", bb->index);
468 else
469 fprintf (f, "l%-3d", ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
470 if (ALLOCNO_HARD_REGNO (a) >= 0)
471 fprintf (f, " %3d", ALLOCNO_HARD_REGNO (a));
472 else
473 fprintf (f, " mem");
475 fprintf (f, "\n");
478 /* The function outputs information about allocation of all allocnos
479 into stderr. */
480 void
481 debug_disposition (void)
483 print_disposition (stderr);
488 /* For each reg class, table listing all the classes contained in it
489 (excluding the class itself. Non-allocatable registers are
490 excluded from the consideration). */
491 static enum reg_class alloc_reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
493 /* The function initializes the tables of subclasses of each reg
494 class. */
495 static void
496 setup_reg_subclasses (void)
498 int i, j;
499 HARD_REG_SET temp_hard_regset2;
501 for (i = 0; i < N_REG_CLASSES; i++)
502 for (j = 0; j < N_REG_CLASSES; j++)
503 alloc_reg_class_subclasses [i] [j] = LIM_REG_CLASSES;
505 for (i = 0; i < N_REG_CLASSES; i++)
507 if (i == (int) NO_REGS)
508 continue;
510 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents [i]);
511 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
512 if (hard_reg_set_equal_p (temp_hard_regset, zero_hard_reg_set))
513 continue;
514 for (j = 0; j < N_REG_CLASSES; j++)
515 if (i != j)
517 enum reg_class *p;
519 COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents [j]);
520 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
521 if (! hard_reg_set_subset_p (temp_hard_regset,
522 temp_hard_regset2))
523 continue;
524 p = &alloc_reg_class_subclasses [j] [0];
525 while (*p != LIM_REG_CLASSES) p++;
526 *p = (enum reg_class) i;
533 /* The value is size of the subsequent array. */
534 int reg_class_cover_size;
536 /* The array containing cover classes whose hard registers are used
537 for the allocation -- see also comments for macro
538 IRA_COVER_CLASSES. */
539 enum reg_class reg_class_cover [N_REG_CLASSES];
541 /* The value is number of elements in the subsequent array. */
542 int important_classes_num;
544 /* The array containing classes which are subclasses of cover
545 classes. */
546 enum reg_class important_classes [N_REG_CLASSES];
548 /* The array containing order numbers of important classes (they are
549 subclasses of cover classes). */
550 int important_class_nums [N_REG_CLASSES];
552 #ifdef IRA_COVER_CLASSES
554 /* The function checks IRA_COVER_CLASSES and sets the two global
555 variables defined above. */
556 static void
557 setup_cover_classes (void)
559 int i, j;
560 enum reg_class cl;
561 static enum reg_class classes [] = IRA_COVER_CLASSES;
562 HARD_REG_SET temp_hard_regset2;
564 reg_class_cover_size = 0;
565 for (i = 0; (cl = classes [i]) != LIM_REG_CLASSES; i++)
567 for (j = 0; j < i; j++)
568 if (reg_classes_intersect_p (cl, classes [j]))
569 gcc_unreachable ();
570 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents [cl]);
571 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
572 if (! hard_reg_set_equal_p (temp_hard_regset, zero_hard_reg_set))
573 reg_class_cover [reg_class_cover_size++] = cl;
575 important_classes_num = 0;
576 for (cl = 0; cl < N_REG_CLASSES; cl++)
578 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents [cl]);
579 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
580 if (! hard_reg_set_equal_p (temp_hard_regset, zero_hard_reg_set))
581 for (j = 0; j < reg_class_cover_size; j++)
583 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents [cl]);
584 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
585 COPY_HARD_REG_SET (temp_hard_regset2,
586 reg_class_contents [reg_class_cover [j]]);
587 AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
588 if (cl == reg_class_cover [j]
589 || (hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2)
590 && ! hard_reg_set_equal_p (temp_hard_regset,
591 temp_hard_regset2)))
593 important_class_nums [cl] = important_classes_num;
594 important_classes [important_classes_num++] = cl;
599 #endif
601 /* Map of register classes to corresponding cover class containing the
602 given class. If given class is not a subset of a cover class, we
603 translate it into the cheapest cover class. */
604 enum reg_class class_translate [N_REG_CLASSES];
606 #ifdef IRA_COVER_CLASSES
608 /* The function sets up array CLASS_TRANSLATE. */
609 static void
610 setup_class_translate (void)
612 enum reg_class cl, cover_class, best_class, *cl_ptr;
613 enum machine_mode mode;
614 int i, cost, min_cost, best_cost;
616 for (cl = 0; cl < N_REG_CLASSES; cl++)
617 class_translate [cl] = NO_REGS;
618 for (i = 0; i < reg_class_cover_size; i++)
620 cover_class = reg_class_cover [i];
621 for (cl_ptr = &alloc_reg_class_subclasses [cover_class] [0];
622 (cl = *cl_ptr) != LIM_REG_CLASSES;
623 cl_ptr++)
625 if (class_translate [cl] == NO_REGS)
626 class_translate [cl] = cover_class;
627 #ifdef ENABLE_IRA_CHECKING
628 else
630 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents [cl]);
631 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
632 if (! hard_reg_set_subset_p (temp_hard_regset,
633 zero_hard_reg_set))
634 gcc_unreachable ();
636 #endif
638 class_translate [cover_class] = cover_class;
640 /* For classes which are not fully covered by a cover class (in
641 other words covered by more one cover class), use the cheapest
642 cover class. */
643 for (cl = 0; cl < N_REG_CLASSES; cl++)
645 if (cl == NO_REGS || class_translate [cl] != NO_REGS)
646 continue;
647 best_class = NO_REGS;
648 best_cost = INT_MAX;
649 for (i = 0; i < reg_class_cover_size; i++)
651 cover_class = reg_class_cover [i];
652 COPY_HARD_REG_SET (temp_hard_regset,
653 reg_class_contents [cover_class]);
654 AND_HARD_REG_SET (temp_hard_regset, reg_class_contents [cl]);
655 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
656 if (! hard_reg_set_equal_p (temp_hard_regset, zero_hard_reg_set))
658 min_cost = INT_MAX;
659 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
661 cost = (memory_move_cost [mode] [cl] [0]
662 + memory_move_cost [mode] [cl] [1]);
663 if (min_cost > cost)
664 min_cost = cost;
666 if (best_class == NO_REGS || best_cost > min_cost)
668 best_class = cover_class;
669 best_cost = min_cost;
673 class_translate [cl] = best_class;
676 #endif
678 /* The biggest important class inside of intersection of the two classes. */
679 enum reg_class reg_class_intersect [N_REG_CLASSES] [N_REG_CLASSES];
680 /* The biggest important class inside of union of the two classes. */
681 enum reg_class reg_class_union [N_REG_CLASSES] [N_REG_CLASSES];
683 #ifdef IRA_COVER_CLASSES
685 /* The function sets up REG_CLASS_INTERSECT and REG_CLASS_UNION. */
686 static void
687 setup_reg_class_intersect_union (void)
689 int i, cl1, cl2, cl3;
690 HARD_REG_SET intersection_set, union_set, temp_set2;
692 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
694 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
696 reg_class_intersect [cl1] [cl2] = NO_REGS;
697 reg_class_union [cl1] [cl2] = NO_REGS;
698 COPY_HARD_REG_SET (intersection_set, reg_class_contents [cl1]);
699 AND_HARD_REG_SET (intersection_set, reg_class_contents [cl2]);
700 AND_COMPL_HARD_REG_SET (intersection_set, no_unit_alloc_regs);
701 COPY_HARD_REG_SET (union_set, reg_class_contents [cl1]);
702 IOR_HARD_REG_SET (union_set, reg_class_contents [cl2]);
703 AND_COMPL_HARD_REG_SET (union_set, no_unit_alloc_regs);
704 for (i = 0; i < important_classes_num; i++)
706 cl3 = important_classes [i];
707 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents [cl3]);
708 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
709 if (hard_reg_set_subset_p (temp_hard_regset, intersection_set))
711 COPY_HARD_REG_SET
712 (temp_set2,
713 reg_class_contents
714 [(int) reg_class_intersect [cl1] [cl2]]);
715 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
716 if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2))
717 reg_class_intersect [cl1] [cl2] = (enum reg_class) cl3;
719 if (hard_reg_set_subset_p (temp_hard_regset, union_set))
721 COPY_HARD_REG_SET
722 (temp_set2,
723 reg_class_contents
724 [(int) reg_class_union [cl1] [cl2]]);
725 AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
726 if (hard_reg_set_subset_p (temp_set2, temp_hard_regset))
727 reg_class_union [cl1] [cl2] = (enum reg_class) cl3;
734 #endif
736 /* The function outputs all cover classes and the translation map into
737 file F. */
738 static void
739 print_class_cover (FILE *f)
741 static const char *const reg_class_names[] = REG_CLASS_NAMES;
742 int i;
744 fprintf (f, "Class cover:\n");
745 for (i = 0; i < reg_class_cover_size; i++)
746 fprintf (f, " %s", reg_class_names [reg_class_cover [i]]);
747 fprintf (f, "\nClass translation:\n");
748 for (i = 0; i < N_REG_CLASSES; i++)
749 fprintf (f, " %s -> %s\n", reg_class_names [i],
750 reg_class_names [class_translate [i]]);
753 /* The function outputs all cover classes and the translation map into
754 stderr. */
755 void
756 debug_class_cover (void)
758 print_class_cover (stderr);
761 /* Function setting up different arrays concerning class subsets and
762 cover classes. */
763 static void
764 find_reg_class_closure (void)
766 setup_reg_subclasses ();
767 #ifdef IRA_COVER_CLASSES
768 setup_cover_classes ();
769 setup_class_translate ();
770 setup_reg_class_intersect_union ();
771 #endif
776 /* Map: register class x machine mode -> number of hard registers of
777 given class needed to store value of given mode. If the number is
778 different, the size will be negative. */
779 int reg_class_nregs [N_REG_CLASSES] [MAX_MACHINE_MODE];
781 /* Maximal value of the previous array elements. */
782 int max_nregs;
784 /* Function forming REG_CLASS_NREGS map. */
785 static void
786 setup_reg_class_nregs (void)
788 int m;
789 enum reg_class cl;
791 max_nregs = -1;
792 for (cl = 0; cl < N_REG_CLASSES; cl++)
793 for (m = 0; m < MAX_MACHINE_MODE; m++)
795 reg_class_nregs [cl] [m] = CLASS_MAX_NREGS (cl, m);
796 if (max_nregs < reg_class_nregs [cl] [m])
797 max_nregs = reg_class_nregs [cl] [m];
803 /* Array whose values are hard regset of hard registers of given
804 register class whose HARD_REGNO_MODE_OK values are zero. */
805 HARD_REG_SET prohibited_class_mode_regs [N_REG_CLASSES] [NUM_MACHINE_MODES];
807 /* The function setting up PROHIBITED_CLASS_MODE_REGS. */
808 static void
809 setup_prohibited_class_mode_regs (void)
811 int i, j, k, hard_regno;
812 enum reg_class cl;
814 for (i = 0; i < reg_class_cover_size; i++)
816 cl = reg_class_cover [i];
817 for (j = 0; j < NUM_MACHINE_MODES; j++)
819 CLEAR_HARD_REG_SET (prohibited_class_mode_regs [cl] [j]);
820 for (k = class_hard_regs_num [cl] - 1; k >= 0; k--)
822 hard_regno = class_hard_regs [cl] [k];
823 if (! HARD_REGNO_MODE_OK (hard_regno, j))
824 SET_HARD_REG_BIT (prohibited_class_mode_regs [cl] [j],
825 hard_regno);
833 /* The function allocates and initializes REGISTER_MOVE_COST,
834 REGISTER_MAY_MOVE_IN_COST, and REGISTER_MAY_MOVE_OUT_COST for MODE
835 if it is not done yet. */
836 void
837 init_register_move_cost (enum machine_mode mode)
839 int cl1, cl2;
841 ira_assert (register_move_cost [mode] == NULL
842 && register_may_move_in_cost [mode] == NULL
843 && register_may_move_out_cost [mode] == NULL);
844 if (move_cost [mode] == NULL)
845 init_move_cost (mode);
846 register_move_cost [mode] = move_cost [mode];
847 /* Don't use ira_allocate becuase the tables exist out of scope of a
848 IRA call. */
849 register_may_move_in_cost [mode]
850 = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
851 memcpy (register_may_move_in_cost [mode], may_move_in_cost [mode],
852 sizeof (move_table) * N_REG_CLASSES);
853 register_may_move_out_cost [mode]
854 = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
855 memcpy (register_may_move_out_cost [mode], may_move_out_cost [mode],
856 sizeof (move_table) * N_REG_CLASSES);
857 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
859 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
861 if (class_subset_p [cl1] [cl2])
862 register_may_move_in_cost [mode] [cl1] [cl2] = 0;
863 if (class_subset_p [cl2] [cl1])
864 register_may_move_out_cost [mode] [cl1] [cl2] = 0;
871 /* Hard regsets whose all bits are correspondingly zero or one. */
872 HARD_REG_SET zero_hard_reg_set;
873 HARD_REG_SET one_hard_reg_set;
875 /* Function called once during compiler work. It sets up different
876 arrays whose values don't depend on the compiled function. */
877 void
878 init_ira_once (void)
880 enum machine_mode mode;
882 CLEAR_HARD_REG_SET (zero_hard_reg_set);
883 SET_HARD_REG_SET (one_hard_reg_set);
884 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
886 register_move_cost [mode] = NULL;
887 register_may_move_in_cost [mode] = NULL;
888 register_may_move_out_cost [mode] = NULL;
890 setup_inner_mode ();
891 init_ira_costs_once ();
894 /* The function frees register_move_cost, register_may_move_in_cost,
895 and register_may_move_out_cost for each mode. */
896 static void
897 free_register_move_costs (void)
899 enum machine_mode mode;
901 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
903 if (register_may_move_in_cost [mode] != NULL)
904 free (register_may_move_in_cost [mode]);
905 if (register_may_move_out_cost [mode] != NULL)
906 free (register_may_move_out_cost [mode]);
907 register_move_cost [mode] = NULL;
908 register_may_move_in_cost [mode] = NULL;
909 register_may_move_out_cost [mode] = NULL;
913 /* The function called every time when register related information is
914 changed. */
915 void
916 init_ira (void)
918 free_register_move_costs ();
919 setup_reg_mode_hard_regset ();
920 setup_alloc_regs (flag_omit_frame_pointer != 0);
921 setup_class_subset_and_memory_move_costs ();
922 find_reg_class_closure ();
923 setup_reg_class_nregs ();
924 setup_prohibited_class_mode_regs ();
925 init_ira_costs ();
928 /* Function called once at the end of compiler work. */
929 void
930 finish_ira_once (void)
932 finish_ira_costs_once ();
933 free_register_move_costs ();
938 /* Array whose values are hard regset of hard registers for which
939 move of the hard register in given mode into itself is
940 prohibited. */
941 HARD_REG_SET prohibited_mode_move_regs [NUM_MACHINE_MODES];
943 /* Flag of that the above array has been initialized. */
944 static int prohibited_mode_move_regs_initialized_p = FALSE;
946 /* The function setting up PROHIBITED_MODE_MOVE_REGS. */
947 static void
948 setup_prohibited_mode_move_regs (void)
950 int i, j;
951 rtx test_reg1, test_reg2, move_pat, move_insn;
953 if (prohibited_mode_move_regs_initialized_p)
954 return;
955 prohibited_mode_move_regs_initialized_p = TRUE;
956 test_reg1 = gen_rtx_REG (VOIDmode, 0);
957 test_reg2 = gen_rtx_REG (VOIDmode, 0);
958 move_pat = gen_rtx_SET (VOIDmode, test_reg1, test_reg2);
959 move_insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, 0, move_pat, -1, 0);
960 for (i = 0; i < NUM_MACHINE_MODES; i++)
962 SET_HARD_REG_SET (prohibited_mode_move_regs [i]);
963 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
965 if (! HARD_REGNO_MODE_OK (j, i))
966 continue;
967 SET_REGNO (test_reg1, j);
968 PUT_MODE (test_reg1, i);
969 SET_REGNO (test_reg2, j);
970 PUT_MODE (test_reg2, i);
971 INSN_CODE (move_insn) = -1;
972 recog_memoized (move_insn);
973 if (INSN_CODE (move_insn) < 0)
974 continue;
975 extract_insn (move_insn);
976 if (! constrain_operands (1))
977 continue;
978 CLEAR_HARD_REG_BIT (prohibited_mode_move_regs [i], j);
985 /* Function specific hard registers excluded from the allocation. */
986 HARD_REG_SET no_alloc_regs;
988 /* Return true if *LOC contains an asm. */
990 static int
991 insn_contains_asm_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
993 if ( !*loc)
994 return 0;
995 if (GET_CODE (*loc) == ASM_OPERANDS)
996 return 1;
997 return 0;
1001 /* Return true if INSN contains an ASM. */
1003 static int
1004 insn_contains_asm (rtx insn)
1006 return for_each_rtx (&insn, insn_contains_asm_1, NULL);
1009 /* Set up regs_asm_clobbered. */
1010 static void
1011 compute_regs_asm_clobbered (char *regs_asm_clobbered)
1013 basic_block bb;
1015 memset (regs_asm_clobbered, 0, sizeof (char) * FIRST_PSEUDO_REGISTER);
1017 FOR_EACH_BB (bb)
1019 rtx insn;
1020 FOR_BB_INSNS_REVERSE (bb, insn)
1022 struct df_ref **def_rec;
1024 if (insn_contains_asm (insn))
1025 for (def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++)
1027 struct df_ref *def = *def_rec;
1028 unsigned int dregno = DF_REF_REGNO (def);
1029 if (dregno < FIRST_PSEUDO_REGISTER)
1031 unsigned int i;
1032 enum machine_mode mode = GET_MODE (DF_REF_REAL_REG (def));
1033 unsigned int end = dregno
1034 + hard_regno_nregs [dregno] [mode] - 1;
1036 for (i = dregno; i <= end; ++i)
1037 regs_asm_clobbered [i] = 1;
1045 /* The function sets up ELIMINABLE_REGSET, NO_ALLOC_REGS, and
1046 REGS_EVER_LIVE. */
1047 static void
1048 setup_eliminable_regset (void)
1050 int i;
1051 /* Like regs_ever_live, but 1 if a reg is set or clobbered from an
1052 asm. Unlike regs_ever_live, elements of this array corresponding
1053 to eliminable regs like the frame pointer are set if an asm sets
1054 them. */
1055 char *regs_asm_clobbered = alloca (FIRST_PSEUDO_REGISTER * sizeof (char));
1056 #ifdef ELIMINABLE_REGS
1057 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
1058 #endif
1059 int need_fp
1060 = (! flag_omit_frame_pointer
1061 || (current_function_calls_alloca && EXIT_IGNORE_STACK)
1062 || FRAME_POINTER_REQUIRED);
1064 COPY_HARD_REG_SET (no_alloc_regs, no_unit_alloc_regs);
1065 CLEAR_HARD_REG_SET (eliminable_regset);
1067 compute_regs_asm_clobbered (regs_asm_clobbered);
1068 /* Build the regset of all eliminable registers and show we can't
1069 use those that we already know won't be eliminated. */
1070 #ifdef ELIMINABLE_REGS
1071 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
1073 bool cannot_elim
1074 = (! CAN_ELIMINATE (eliminables [i].from, eliminables [i].to)
1075 || (eliminables [i].to == STACK_POINTER_REGNUM && need_fp));
1077 if (! regs_asm_clobbered [eliminables [i].from])
1079 SET_HARD_REG_BIT (eliminable_regset, eliminables [i].from);
1081 if (cannot_elim)
1082 SET_HARD_REG_BIT (no_alloc_regs, eliminables[i].from);
1084 else if (cannot_elim)
1085 error ("%s cannot be used in asm here",
1086 reg_names [eliminables [i].from]);
1087 else
1088 df_set_regs_ever_live (eliminables[i].from, true);
1090 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1091 if (! regs_asm_clobbered [HARD_FRAME_POINTER_REGNUM])
1093 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
1094 if (need_fp)
1095 SET_HARD_REG_BIT (no_alloc_regs, HARD_FRAME_POINTER_REGNUM);
1097 else if (need_fp)
1098 error ("%s cannot be used in asm here",
1099 reg_names [HARD_FRAME_POINTER_REGNUM]);
1100 else
1101 df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
1102 #endif
1104 #else
1105 if (! regs_asm_clobbered [FRAME_POINTER_REGNUM])
1107 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
1108 if (need_fp)
1109 SET_HARD_REG_BIT (no_alloc_regs, FRAME_POINTER_REGNUM);
1111 else if (need_fp)
1112 error ("%s cannot be used in asm here", reg_names [FRAME_POINTER_REGNUM]);
1113 else
1114 df_set_regs_ever_live (FRAME_POINTER_REGNUM, true);
1115 #endif
1120 /* The length of the following two arrays. */
1121 int reg_equiv_len;
1123 /* The element value is nonzero if the corresponding regno value is
1124 invariant. */
1125 int *reg_equiv_invariant_p;
1127 /* The element value is equiv constant or NULL_RTX. */
1128 rtx *reg_equiv_const;
1130 /* The function sets up the two array declaraed above. */
1131 static void
1132 find_reg_equiv_invariant_const (void)
1134 int i, invariant_p;
1135 rtx list, insn, note, constant, x;
1137 for (i = FIRST_PSEUDO_REGISTER; i < reg_equiv_init_size; i++)
1139 constant = NULL_RTX;
1140 invariant_p = FALSE;
1141 for (list = reg_equiv_init [i]; list != NULL_RTX; list = XEXP (list, 1))
1143 insn = XEXP (list, 0);
1144 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
1146 if (note == NULL_RTX)
1147 continue;
1149 x = XEXP (note, 0);
1151 if (! function_invariant_p (x)
1152 || ! flag_pic
1153 /* A function invariant is often CONSTANT_P but may
1154 include a register. We promise to only pass CONSTANT_P
1155 objects to LEGITIMATE_PIC_OPERAND_P. */
1156 || (CONSTANT_P (x) && LEGITIMATE_PIC_OPERAND_P (x)))
1158 /* It can happen that a REG_EQUIV note contains a MEM that
1159 is not a legitimate memory operand. As later stages of
1160 reload assume that all addresses found in the
1161 reg_equiv_* arrays were originally legitimate, we
1162 ignore such REG_EQUIV notes. */
1163 if (memory_operand (x, VOIDmode))
1164 continue;
1165 else if (function_invariant_p (x))
1167 if (GET_CODE (x) == PLUS
1168 || x == frame_pointer_rtx || x == arg_pointer_rtx)
1169 invariant_p = TRUE;
1170 else
1171 constant = x;
1175 reg_equiv_invariant_p [i] = invariant_p;
1176 reg_equiv_const [i] = constant;
1182 /* The function sets up REG_RENUMBER and CALLER_SAVE_NEEDED used by
1183 reload from the allocation found by IRA. */
1184 static void
1185 setup_reg_renumber (void)
1187 int i, regno, hard_regno;
1188 allocno_t a;
1190 caller_save_needed = 0;
1191 for (i = 0; i < allocnos_num; i++)
1193 a = allocnos [i];
1194 /* There are no caps at this point. */
1195 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
1196 if (! ALLOCNO_ASSIGNED_P (a))
1197 ALLOCNO_ASSIGNED_P (a) = TRUE;
1198 ira_assert (ALLOCNO_ASSIGNED_P (a));
1199 hard_regno = ALLOCNO_HARD_REGNO (a);
1200 regno = (int) REGNO (ALLOCNO_REG (a));
1201 reg_renumber [regno] = (hard_regno < 0 ? -1 : hard_regno);
1202 if (hard_regno >= 0 && ALLOCNO_CALLS_CROSSED_NUM (a) != 0
1203 && ! hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
1204 call_used_reg_set))
1206 ira_assert (flag_caller_saves || regno >= reg_equiv_len
1207 || reg_equiv_const [regno]
1208 || reg_equiv_invariant_p [regno]);
1209 caller_save_needed = 1;
1214 /* The function sets up allocno assignment flags for further
1215 allocation improvements. */
1216 static void
1217 setup_allocno_assignment_flags (void)
1219 int i, hard_regno;
1220 allocno_t a;
1222 for (i = 0; i < allocnos_num; i++)
1224 a = allocnos [i];
1225 hard_regno = ALLOCNO_HARD_REGNO (a);
1226 /* Don't assign hard registers to allocnos which are destination
1227 of removed store at the end of loop. It has a few sense to
1228 keep the same value in different hard registers. It is also
1229 impossible to assign hard registers correctly to such
1230 allocnos because the cost info and info about intersected
1231 calls are incorrect for them. */
1232 ALLOCNO_ASSIGNED_P (a) = (hard_regno >= 0
1233 || ALLOCNO_MEM_OPTIMIZED_DEST_P (a));
1234 ira_assert (hard_regno < 0
1235 || ! hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
1236 reg_class_contents
1237 [ALLOCNO_COVER_CLASS (a)]));
1241 /* The function evaluates overall allocation cost and costs for using
1242 registers and memory for allocnos. */
1244 static void
1245 calculate_allocation_cost (void)
1247 int i, hard_regno, cost;
1248 allocno_t a;
1250 overall_cost = reg_cost = mem_cost = 0;
1251 for (i = 0; i < allocnos_num; i++)
1253 a = allocnos [i];
1254 hard_regno = ALLOCNO_HARD_REGNO (a);
1255 ira_assert (hard_regno < 0
1256 || ! hard_reg_not_in_set_p
1257 (hard_regno, ALLOCNO_MODE (a),
1258 reg_class_contents [ALLOCNO_COVER_CLASS (a)]));
1259 if (hard_regno < 0)
1261 cost = ALLOCNO_MEMORY_COST (a);
1262 mem_cost += cost;
1264 else if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
1266 cost = (ALLOCNO_HARD_REG_COSTS (a)
1267 [class_hard_reg_index
1268 [ALLOCNO_COVER_CLASS (a)] [hard_regno]]);
1269 reg_cost += cost;
1271 else
1273 cost = ALLOCNO_COVER_CLASS_COST (a);
1274 reg_cost += cost;
1276 overall_cost += cost;
1279 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1281 fprintf (ira_dump_file,
1282 "+++Costs: overall %d, reg %d, mem %d, ld %d, st %d, move %d\n",
1283 overall_cost, reg_cost, mem_cost,
1284 load_cost, store_cost, shuffle_cost);
1285 fprintf (ira_dump_file, "+++ move loops %d, new jumps %d\n",
1286 move_loops_num, additional_jumps_num);
1291 #ifdef ENABLE_IRA_CHECKING
1292 /* The function checks correctness of the allocation. */
1293 static void
1294 check_allocation (void)
1296 allocno_t a, conflict_a, *allocno_vec;
1297 int i, hard_regno, conflict_hard_regno, j, nregs, conflict_nregs;
1299 for (i = 0; i < allocnos_num; i++)
1301 a = allocnos [i];
1302 if (ALLOCNO_CAP_MEMBER (a) != NULL
1303 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
1304 continue;
1305 nregs = hard_regno_nregs [hard_regno] [ALLOCNO_MODE (a)];
1306 allocno_vec = ALLOCNO_CONFLICT_ALLOCNO_VEC (a);
1307 for (j = 0; (conflict_a = allocno_vec [j]) != NULL; j++)
1308 if ((conflict_hard_regno = ALLOCNO_HARD_REGNO (conflict_a)) >= 0)
1310 conflict_nregs
1311 = (hard_regno_nregs
1312 [conflict_hard_regno] [ALLOCNO_MODE (conflict_a)]);
1313 if ((conflict_hard_regno <= hard_regno
1314 && hard_regno < conflict_hard_regno + conflict_nregs)
1315 || (hard_regno <= conflict_hard_regno
1316 && conflict_hard_regno < hard_regno + nregs))
1318 fprintf (stderr, "bad allocation for %d and %d\n",
1319 ALLOCNO_REGNO (a), ALLOCNO_REGNO (conflict_a));
1320 gcc_unreachable ();
1325 #endif
1327 /* The function fixes values of array REG_EQUIV_INIT after live range
1328 splitting done by IRA. */
1329 static void
1330 fix_reg_equiv_init (void)
1332 int max_regno = max_reg_num ();
1333 int i, new_regno;
1334 rtx x, prev, next, insn, set;
1337 if (reg_equiv_init_size < max_regno)
1339 reg_equiv_init = ggc_realloc (reg_equiv_init, max_regno * sizeof (rtx));
1340 while (reg_equiv_init_size < max_regno)
1341 reg_equiv_init [reg_equiv_init_size++] = NULL_RTX;
1342 for (i = FIRST_PSEUDO_REGISTER; i < reg_equiv_init_size; i++)
1343 for (prev = NULL_RTX, x = reg_equiv_init [i]; x != NULL_RTX; x = next)
1345 next = XEXP (x, 1);
1346 insn = XEXP (x, 0);
1347 set = single_set (insn);
1348 ira_assert (set != NULL_RTX
1349 && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))));
1350 if (REG_P (SET_DEST (set))
1351 && ((int) REGNO (SET_DEST (set)) == i
1352 || (int) ORIGINAL_REGNO (SET_DEST (set)) == i))
1353 new_regno = REGNO (SET_DEST (set));
1354 else if (REG_P (SET_SRC (set))
1355 && ((int) REGNO (SET_SRC (set)) == i
1356 || (int) ORIGINAL_REGNO (SET_SRC (set)) == i))
1357 new_regno = REGNO (SET_SRC (set));
1358 else
1359 gcc_unreachable ();
1360 if (new_regno == i)
1361 prev = x;
1362 else
1364 if (prev == NULL_RTX)
1365 reg_equiv_init [i] = next;
1366 else
1367 XEXP (prev, 1) = next;
1368 XEXP (x, 1) = reg_equiv_init [new_regno];
1369 reg_equiv_init [new_regno] = x;
1375 #ifdef ENABLE_IRA_CHECKING
1376 /* The function prints redundant memory memory copies. */
1377 static void
1378 print_redundant_copies (void)
1380 int i, hard_regno;
1381 allocno_t a;
1382 copy_t cp, next_cp;
1384 for (i = 0; i < allocnos_num; i++)
1386 a = allocnos [i];
1387 if (ALLOCNO_CAP_MEMBER (a) != NULL)
1388 /* It is a cap. */
1389 continue;
1390 hard_regno = ALLOCNO_HARD_REGNO (a);
1391 if (hard_regno >= 0)
1392 continue;
1393 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1394 if (cp->first == a)
1395 next_cp = cp->next_first_allocno_copy;
1396 else
1398 next_cp = cp->next_second_allocno_copy;
1399 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL
1400 && cp->insn != NULL_RTX
1401 && ALLOCNO_HARD_REGNO (cp->first) == hard_regno)
1402 fprintf (ira_dump_file,
1403 " Redundant move from %d(freq %d):%d\n",
1404 INSN_UID (cp->insn), cp->freq, hard_regno);
1408 #endif
1410 /* Setup preferred and alternative classes for pseudo-registers for
1411 other passes. */
1412 static void
1413 setup_preferred_alternate_classes (void)
1415 int i;
1416 enum reg_class cover_class;
1417 allocno_t a;
1419 for (i = 0; i < allocnos_num; i++)
1421 a = allocnos [i];
1422 cover_class = ALLOCNO_COVER_CLASS (a);
1423 if (cover_class == NO_REGS)
1424 cover_class = GENERAL_REGS;
1425 setup_reg_classes (ALLOCNO_REGNO (a), cover_class, NO_REGS);
1431 static void
1432 expand_reg_info (int old_size)
1434 int i;
1435 int size = max_reg_num ();
1437 resize_reg_info ();
1438 for (i = old_size; i < size; i++)
1440 reg_renumber [i] = -1;
1441 setup_reg_classes (i, GENERAL_REGS, ALL_REGS);
1447 /* Map bb index -> order number in the BB chain. */
1448 static int *basic_block_order_nums;
1450 /* The function is used to sort insn chain according insn execution
1451 frequencies. */
1452 static int
1453 chain_freq_compare (const void *v1p, const void *v2p)
1455 struct insn_chain *c1 = *(struct insn_chain **)v1p;
1456 struct insn_chain *c2 = *(struct insn_chain **)v2p;
1457 int diff;
1459 diff = (BASIC_BLOCK (c2->block)->frequency
1460 - BASIC_BLOCK (c1->block)->frequency);
1461 if (diff)
1462 return diff;
1463 return (char *) v1p - (char *) v2p;
1466 /* The function is used to sort insn chain according insn original
1467 order. */
1468 static int
1469 chain_bb_compare (const void *v1p, const void *v2p)
1471 struct insn_chain *c1 = *(struct insn_chain **)v1p;
1472 struct insn_chain *c2 = *(struct insn_chain **)v2p;
1473 int diff;
1475 diff = (basic_block_order_nums [c1->block]
1476 - basic_block_order_nums [c2->block]);
1477 if (diff)
1478 return diff;
1479 return (char *) v1p - (char *) v2p;
1482 /* The function sorts insn chain according insn frequencies (if
1483 FREQ_P) or insn original order. */
1484 void
1485 sort_insn_chain (int freq_p)
1487 struct insn_chain *chain, **chain_arr;
1488 basic_block bb;
1489 int i, n;
1491 for (n = 0, chain = reload_insn_chain; chain != 0; chain = chain->next)
1492 n++;
1493 if (n <= 1)
1494 return;
1495 chain_arr = ira_allocate (n * sizeof (struct insn_chain *));
1496 basic_block_order_nums = ira_allocate (sizeof (int) * last_basic_block);
1497 n = 0;
1498 FOR_EACH_BB (bb)
1500 basic_block_order_nums [bb->index] = n++;
1502 for (n = 0, chain = reload_insn_chain; chain != 0; chain = chain->next)
1503 chain_arr [n++] = chain;
1504 qsort (chain_arr, n, sizeof (struct insn_chain *),
1505 freq_p ? chain_freq_compare : chain_bb_compare);
1506 for (i = 1; i < n - 1; i++)
1508 chain_arr [i]->next = chain_arr [i + 1];
1509 chain_arr [i]->prev = chain_arr [i - 1];
1511 chain_arr [i]->next = NULL;
1512 chain_arr [i]->prev = chain_arr [i - 1];
1513 reload_insn_chain = chain_arr [0];
1514 reload_insn_chain->prev = NULL;
1515 reload_insn_chain->next = chain_arr [1];
1516 ira_free (basic_block_order_nums);
1517 ira_free (chain_arr);
1522 /* All natural loops. */
1523 struct loops ira_loops;
1525 /* This is the main entry of IRA. */
1526 void
1527 ira (FILE *f)
1529 int overall_cost_before, loops_p, allocated_reg_info_size;
1530 int max_regno_before_ira, max_point_before_emit;
1531 int rebuild_p, saved_flag_ira_algorithm;
1532 basic_block bb;
1534 if (flag_ira_verbose < 10)
1536 internal_flag_ira_verbose = flag_ira_verbose;
1537 ira_dump_file = f;
1539 else
1541 internal_flag_ira_verbose = flag_ira_verbose - 10;
1542 ira_dump_file = stderr;
1545 setup_prohibited_mode_move_regs ();
1547 df_note_add_problem ();
1549 if (optimize == 1)
1551 df_live_add_problem ();
1552 df_live_set_all_dirty ();
1554 df_analyze ();
1556 df_clear_flags (DF_NO_INSN_RESCAN);
1558 allocno_pool = create_alloc_pool ("allocnos", sizeof (struct allocno), 100);
1559 copy_pool = create_alloc_pool ("copies", sizeof (struct allocno_copy), 100);
1560 allocno_live_range_pool
1561 = create_alloc_pool ("allocno live ranges",
1562 sizeof (struct allocno_live_range), 100);
1563 regstat_init_n_sets_and_refs ();
1564 regstat_compute_ri ();
1565 rebuild_p = update_equiv_regs ();
1566 regstat_free_n_sets_and_refs ();
1567 regstat_free_ri ();
1569 #ifndef IRA_NO_OBSTACK
1570 gcc_obstack_init (&ira_obstack);
1571 #endif
1572 bitmap_obstack_initialize (&ira_bitmap_obstack);
1574 max_regno = max_reg_num ();
1575 reg_equiv_len = max_regno;
1576 reg_equiv_invariant_p = ira_allocate (max_regno * sizeof (int));
1577 memset (reg_equiv_invariant_p, 0, max_regno * sizeof (int));
1578 reg_equiv_const = ira_allocate (max_regno * sizeof (rtx));
1579 memset (reg_equiv_const, 0, max_regno * sizeof (rtx));
1580 find_reg_equiv_invariant_const ();
1581 if (rebuild_p)
1583 timevar_push (TV_JUMP);
1584 rebuild_jump_labels (get_insns ());
1585 purge_all_dead_edges ();
1586 timevar_pop (TV_JUMP);
1588 max_regno_before_ira = allocated_reg_info_size = max_reg_num ();
1589 allocate_reg_info ();
1590 setup_eliminable_regset ();
1592 overall_cost = reg_cost = mem_cost = 0;
1593 load_cost = store_cost = shuffle_cost = 0;
1594 move_loops_num = additional_jumps_num = 0;
1596 ira_assert (current_loops == NULL);
1597 flow_loops_find (&ira_loops);
1598 current_loops = &ira_loops;
1599 saved_flag_ira_algorithm = flag_ira_algorithm;
1600 if (number_of_loops () > (unsigned) IRA_MAX_LOOPS_NUM)
1601 flag_ira_algorithm = IRA_ALGORITHM_CB;
1603 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1604 fprintf (ira_dump_file, "Building IRA IR\n");
1605 loops_p = ira_build (flag_ira_algorithm == IRA_ALGORITHM_REGIONAL
1606 || flag_ira_algorithm == IRA_ALGORITHM_MIXED);
1607 ira_color ();
1609 max_point_before_emit = max_point;
1611 ira_emit (loops_p);
1613 max_regno = max_reg_num ();
1615 if (! loops_p)
1616 initiate_ira_assign ();
1617 else
1619 expand_reg_info (allocated_reg_info_size);
1620 allocated_reg_info_size = max_regno;
1622 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1623 fprintf (ira_dump_file, "Flattening IR\n");
1624 ira_flattening (max_regno_before_ira, max_point_before_emit);
1625 /* New insns were generated: add notes and recaclulate live
1626 info. */
1627 df_analyze ();
1629 basic_block bb;
1631 FOR_ALL_BB (bb)
1632 bb->loop_father = NULL;
1633 current_loops = NULL;
1635 setup_allocno_assignment_flags ();
1636 initiate_ira_assign ();
1637 reassign_conflict_allocnos (max_regno, FALSE);
1640 setup_reg_renumber ();
1642 calculate_allocation_cost ();
1644 #ifdef ENABLE_IRA_CHECKING
1645 check_allocation ();
1646 #endif
1648 setup_preferred_alternate_classes ();
1650 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1651 max_regno = max_reg_num ();
1653 /* Determine if the current function is a leaf before running IRA
1654 since this can impact optimizations done by the prologue and
1655 epilogue thus changing register elimination offsets. */
1656 current_function_is_leaf = leaf_function_p ();
1658 /* And the reg_equiv_memory_loc array. */
1659 VEC_safe_grow (rtx, gc, reg_equiv_memory_loc_vec, max_regno);
1660 memset (VEC_address (rtx, reg_equiv_memory_loc_vec), 0,
1661 sizeof (rtx) * max_regno);
1662 reg_equiv_memory_loc = VEC_address (rtx, reg_equiv_memory_loc_vec);
1664 allocate_initial_values (reg_equiv_memory_loc);
1666 regstat_init_n_sets_and_refs ();
1667 regstat_compute_ri ();
1669 fix_reg_equiv_init ();
1671 #ifdef ENABLE_IRA_CHECKING
1672 print_redundant_copies ();
1673 #endif
1675 overall_cost_before = overall_cost;
1677 spilled_reg_stack_slots_num = 0;
1678 spilled_reg_stack_slots
1679 = ira_allocate (max_regno * sizeof (struct spilled_reg_stack_slot));
1680 memset (spilled_reg_stack_slots, 0,
1681 max_regno * sizeof (struct spilled_reg_stack_slot));
1683 df_set_flags (DF_NO_INSN_RESCAN);
1684 build_insn_chain ();
1685 sort_insn_chain (TRUE);
1686 reload_completed = ! reload (get_insns (), 1);
1688 ira_free (spilled_reg_stack_slots);
1690 finish_ira_assign ();
1692 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL
1693 && overall_cost_before != overall_cost)
1694 fprintf (ira_dump_file, "+++Overall after reload %d\n", overall_cost);
1696 ira_destroy ();
1698 #if 0
1699 ira_assert (current_loops == &ira_loops);
1700 #endif
1701 flow_loops_free (&ira_loops);
1702 free_dominance_info (CDI_DOMINATORS);
1703 FOR_ALL_BB (bb)
1704 bb->loop_father = NULL;
1705 current_loops = NULL;
1707 flag_ira_algorithm = saved_flag_ira_algorithm;
1709 cleanup_cfg (CLEANUP_EXPENSIVE);
1711 regstat_free_ri ();
1712 regstat_free_n_sets_and_refs ();
1714 ira_free (reg_equiv_invariant_p);
1715 ira_free (reg_equiv_const);
1717 free_alloc_pool (allocno_live_range_pool);
1718 free_alloc_pool (copy_pool);
1719 free_alloc_pool (allocno_pool);
1721 bitmap_obstack_release (&ira_bitmap_obstack);
1722 #ifndef IRA_NO_OBSTACK
1723 obstack_free (&ira_obstack, NULL);
1724 #endif
1726 /* The code after the reload has changed so much that at this point
1727 we might as well just rescan everything. Not that
1728 df_rescan_all_insns is not going to help here because it does not
1729 touch the artificial uses and defs. */
1730 df_finish_pass (true);
1731 if (optimize > 1)
1732 df_live_add_problem ();
1733 df_scan_alloc (NULL);
1734 df_scan_blocks ();
1736 if (optimize)
1737 df_analyze ();
1742 static bool
1743 gate_ira (void)
1745 return flag_ira != 0;
1748 /* Run the integrated register allocator. */
1749 static unsigned int
1750 rest_of_handle_ira (void)
1752 ira (dump_file);
1753 return 0;
1756 struct tree_opt_pass pass_ira =
1758 "ira", /* name */
1759 gate_ira, /* gate */
1760 rest_of_handle_ira, /* execute */
1761 NULL, /* sub */
1762 NULL, /* next */
1763 0, /* static_pass_number */
1764 TV_IRA, /* tv_id */
1765 0, /* properties_required */
1766 0, /* properties_provided */
1767 0, /* properties_destroyed */
1768 0, /* todo_flags_start */
1769 TODO_dump_func |
1770 TODO_ggc_collect, /* todo_flags_finish */
1771 'y' /* letter */