2008-02-20 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ira.c
blob4b7529cd4eece1a2e9777fea7a9d6602e82e55ec
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, j, 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_hard_regset, temp_set2))
727 reg_class_union [cl1] [cl2] = (enum reg_class) cl3;
732 /* Fix reg_class_union for cover classes: prefer the first cover
733 class. */
734 for (i = 0; i < reg_class_cover_size; i++)
736 cl1 = reg_class_cover [i];
737 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents [cl1]);
738 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
739 if (hard_reg_set_equal_p (temp_hard_regset, zero_hard_reg_set))
740 continue;
741 for (j = i + 1; j < reg_class_cover_size; j++)
743 cl2 = reg_class_cover [j];
744 reg_class_union [cl1] [cl2] = reg_class_union [cl2] [cl1] = cl1;
749 #endif
751 /* The function outputs all cover classes and the translation map into
752 file F. */
753 static void
754 print_class_cover (FILE *f)
756 static const char *const reg_class_names[] = REG_CLASS_NAMES;
757 int i;
759 fprintf (f, "Class cover:\n");
760 for (i = 0; i < reg_class_cover_size; i++)
761 fprintf (f, " %s", reg_class_names [reg_class_cover [i]]);
762 fprintf (f, "\nClass translation:\n");
763 for (i = 0; i < N_REG_CLASSES; i++)
764 fprintf (f, " %s -> %s\n", reg_class_names [i],
765 reg_class_names [class_translate [i]]);
768 /* The function outputs all cover classes and the translation map into
769 stderr. */
770 void
771 debug_class_cover (void)
773 print_class_cover (stderr);
776 /* Function setting up different arrays concerning class subsets and
777 cover classes. */
778 static void
779 find_reg_class_closure (void)
781 setup_reg_subclasses ();
782 #ifdef IRA_COVER_CLASSES
783 setup_cover_classes ();
784 setup_class_translate ();
785 setup_reg_class_intersect_union ();
786 #endif
791 /* Map: register class x machine mode -> number of hard registers of
792 given class needed to store value of given mode. If the number is
793 different, the size will be negative. */
794 int reg_class_nregs [N_REG_CLASSES] [MAX_MACHINE_MODE];
796 /* Maximal value of the previous array elements. */
797 int max_nregs;
799 /* Function forming REG_CLASS_NREGS map. */
800 static void
801 setup_reg_class_nregs (void)
803 int m;
804 enum reg_class cl;
806 max_nregs = -1;
807 for (cl = 0; cl < N_REG_CLASSES; cl++)
808 for (m = 0; m < MAX_MACHINE_MODE; m++)
810 reg_class_nregs [cl] [m] = CLASS_MAX_NREGS (cl, m);
811 if (max_nregs < reg_class_nregs [cl] [m])
812 max_nregs = reg_class_nregs [cl] [m];
818 /* Array whose values are hard regset of hard registers of given
819 register class whose HARD_REGNO_MODE_OK values are zero. */
820 HARD_REG_SET prohibited_class_mode_regs [N_REG_CLASSES] [NUM_MACHINE_MODES];
822 /* The function setting up PROHIBITED_CLASS_MODE_REGS. */
823 static void
824 setup_prohibited_class_mode_regs (void)
826 int i, j, k, hard_regno;
827 enum reg_class cl;
829 for (i = 0; i < reg_class_cover_size; i++)
831 cl = reg_class_cover [i];
832 for (j = 0; j < NUM_MACHINE_MODES; j++)
834 CLEAR_HARD_REG_SET (prohibited_class_mode_regs [cl] [j]);
835 for (k = class_hard_regs_num [cl] - 1; k >= 0; k--)
837 hard_regno = class_hard_regs [cl] [k];
838 if (! HARD_REGNO_MODE_OK (hard_regno, j))
839 SET_HARD_REG_BIT (prohibited_class_mode_regs [cl] [j],
840 hard_regno);
848 /* The function allocates and initializes REGISTER_MOVE_COST,
849 REGISTER_MAY_MOVE_IN_COST, and REGISTER_MAY_MOVE_OUT_COST for MODE
850 if it is not done yet. */
851 void
852 init_register_move_cost (enum machine_mode mode)
854 int cl1, cl2;
856 ira_assert (register_move_cost [mode] == NULL
857 && register_may_move_in_cost [mode] == NULL
858 && register_may_move_out_cost [mode] == NULL);
859 if (move_cost [mode] == NULL)
860 init_move_cost (mode);
861 register_move_cost [mode] = move_cost [mode];
862 /* Don't use ira_allocate becuase the tables exist out of scope of a
863 IRA call. */
864 register_may_move_in_cost [mode]
865 = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
866 memcpy (register_may_move_in_cost [mode], may_move_in_cost [mode],
867 sizeof (move_table) * N_REG_CLASSES);
868 register_may_move_out_cost [mode]
869 = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
870 memcpy (register_may_move_out_cost [mode], may_move_out_cost [mode],
871 sizeof (move_table) * N_REG_CLASSES);
872 for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
874 for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
876 if (class_subset_p [cl1] [cl2])
877 register_may_move_in_cost [mode] [cl1] [cl2] = 0;
878 if (class_subset_p [cl2] [cl1])
879 register_may_move_out_cost [mode] [cl1] [cl2] = 0;
886 /* Hard regsets whose all bits are correspondingly zero or one. */
887 HARD_REG_SET zero_hard_reg_set;
888 HARD_REG_SET one_hard_reg_set;
890 /* Function called once during compiler work. It sets up different
891 arrays whose values don't depend on the compiled function. */
892 void
893 init_ira_once (void)
895 enum machine_mode mode;
897 CLEAR_HARD_REG_SET (zero_hard_reg_set);
898 SET_HARD_REG_SET (one_hard_reg_set);
899 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
901 register_move_cost [mode] = NULL;
902 register_may_move_in_cost [mode] = NULL;
903 register_may_move_out_cost [mode] = NULL;
905 setup_inner_mode ();
906 init_ira_costs_once ();
909 /* The function frees register_move_cost, register_may_move_in_cost,
910 and register_may_move_out_cost for each mode. */
911 static void
912 free_register_move_costs (void)
914 enum machine_mode mode;
916 for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
918 if (register_may_move_in_cost [mode] != NULL)
919 free (register_may_move_in_cost [mode]);
920 if (register_may_move_out_cost [mode] != NULL)
921 free (register_may_move_out_cost [mode]);
922 register_move_cost [mode] = NULL;
923 register_may_move_in_cost [mode] = NULL;
924 register_may_move_out_cost [mode] = NULL;
928 /* The function called every time when register related information is
929 changed. */
930 void
931 init_ira (void)
933 free_register_move_costs ();
934 setup_reg_mode_hard_regset ();
935 setup_alloc_regs (flag_omit_frame_pointer != 0);
936 setup_class_subset_and_memory_move_costs ();
937 find_reg_class_closure ();
938 setup_reg_class_nregs ();
939 setup_prohibited_class_mode_regs ();
940 init_ira_costs ();
943 /* Function called once at the end of compiler work. */
944 void
945 finish_ira_once (void)
947 finish_ira_costs_once ();
948 free_register_move_costs ();
953 /* Array whose values are hard regset of hard registers for which
954 move of the hard register in given mode into itself is
955 prohibited. */
956 HARD_REG_SET prohibited_mode_move_regs [NUM_MACHINE_MODES];
958 /* Flag of that the above array has been initialized. */
959 static int prohibited_mode_move_regs_initialized_p = FALSE;
961 /* The function setting up PROHIBITED_MODE_MOVE_REGS. */
962 static void
963 setup_prohibited_mode_move_regs (void)
965 int i, j;
966 rtx test_reg1, test_reg2, move_pat, move_insn;
968 if (prohibited_mode_move_regs_initialized_p)
969 return;
970 prohibited_mode_move_regs_initialized_p = TRUE;
971 test_reg1 = gen_rtx_REG (VOIDmode, 0);
972 test_reg2 = gen_rtx_REG (VOIDmode, 0);
973 move_pat = gen_rtx_SET (VOIDmode, test_reg1, test_reg2);
974 move_insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, 0, move_pat, -1, 0);
975 for (i = 0; i < NUM_MACHINE_MODES; i++)
977 SET_HARD_REG_SET (prohibited_mode_move_regs [i]);
978 for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
980 if (! HARD_REGNO_MODE_OK (j, i))
981 continue;
982 SET_REGNO (test_reg1, j);
983 PUT_MODE (test_reg1, i);
984 SET_REGNO (test_reg2, j);
985 PUT_MODE (test_reg2, i);
986 INSN_CODE (move_insn) = -1;
987 recog_memoized (move_insn);
988 if (INSN_CODE (move_insn) < 0)
989 continue;
990 extract_insn (move_insn);
991 if (! constrain_operands (1))
992 continue;
993 CLEAR_HARD_REG_BIT (prohibited_mode_move_regs [i], j);
1000 /* Function specific hard registers excluded from the allocation. */
1001 HARD_REG_SET no_alloc_regs;
1003 /* Return true if *LOC contains an asm. */
1005 static int
1006 insn_contains_asm_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
1008 if ( !*loc)
1009 return 0;
1010 if (GET_CODE (*loc) == ASM_OPERANDS)
1011 return 1;
1012 return 0;
1016 /* Return true if INSN contains an ASM. */
1018 static int
1019 insn_contains_asm (rtx insn)
1021 return for_each_rtx (&insn, insn_contains_asm_1, NULL);
1024 /* Set up regs_asm_clobbered. */
1025 static void
1026 compute_regs_asm_clobbered (char *regs_asm_clobbered)
1028 basic_block bb;
1030 memset (regs_asm_clobbered, 0, sizeof (char) * FIRST_PSEUDO_REGISTER);
1032 FOR_EACH_BB (bb)
1034 rtx insn;
1035 FOR_BB_INSNS_REVERSE (bb, insn)
1037 struct df_ref **def_rec;
1039 if (insn_contains_asm (insn))
1040 for (def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++)
1042 struct df_ref *def = *def_rec;
1043 unsigned int dregno = DF_REF_REGNO (def);
1044 if (dregno < FIRST_PSEUDO_REGISTER)
1046 unsigned int i;
1047 enum machine_mode mode = GET_MODE (DF_REF_REAL_REG (def));
1048 unsigned int end = dregno
1049 + hard_regno_nregs [dregno] [mode] - 1;
1051 for (i = dregno; i <= end; ++i)
1052 regs_asm_clobbered [i] = 1;
1060 /* The function sets up ELIMINABLE_REGSET, NO_ALLOC_REGS, and
1061 REGS_EVER_LIVE. */
1062 static void
1063 setup_eliminable_regset (void)
1065 int i;
1066 /* Like regs_ever_live, but 1 if a reg is set or clobbered from an
1067 asm. Unlike regs_ever_live, elements of this array corresponding
1068 to eliminable regs like the frame pointer are set if an asm sets
1069 them. */
1070 char *regs_asm_clobbered = alloca (FIRST_PSEUDO_REGISTER * sizeof (char));
1071 #ifdef ELIMINABLE_REGS
1072 static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
1073 #endif
1074 int need_fp
1075 = (! flag_omit_frame_pointer
1076 || (current_function_calls_alloca && EXIT_IGNORE_STACK)
1077 || FRAME_POINTER_REQUIRED);
1079 COPY_HARD_REG_SET (no_alloc_regs, no_unit_alloc_regs);
1080 CLEAR_HARD_REG_SET (eliminable_regset);
1082 compute_regs_asm_clobbered (regs_asm_clobbered);
1083 /* Build the regset of all eliminable registers and show we can't
1084 use those that we already know won't be eliminated. */
1085 #ifdef ELIMINABLE_REGS
1086 for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
1088 bool cannot_elim
1089 = (! CAN_ELIMINATE (eliminables [i].from, eliminables [i].to)
1090 || (eliminables [i].to == STACK_POINTER_REGNUM && need_fp));
1092 if (! regs_asm_clobbered [eliminables [i].from])
1094 SET_HARD_REG_BIT (eliminable_regset, eliminables [i].from);
1096 if (cannot_elim)
1097 SET_HARD_REG_BIT (no_alloc_regs, eliminables[i].from);
1099 else if (cannot_elim)
1100 error ("%s cannot be used in asm here",
1101 reg_names [eliminables [i].from]);
1102 else
1103 df_set_regs_ever_live (eliminables[i].from, true);
1105 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1106 if (! regs_asm_clobbered [HARD_FRAME_POINTER_REGNUM])
1108 SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
1109 if (need_fp)
1110 SET_HARD_REG_BIT (no_alloc_regs, HARD_FRAME_POINTER_REGNUM);
1112 else if (need_fp)
1113 error ("%s cannot be used in asm here",
1114 reg_names [HARD_FRAME_POINTER_REGNUM]);
1115 else
1116 df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
1117 #endif
1119 #else
1120 if (! regs_asm_clobbered [FRAME_POINTER_REGNUM])
1122 SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
1123 if (need_fp)
1124 SET_HARD_REG_BIT (no_alloc_regs, FRAME_POINTER_REGNUM);
1126 else if (need_fp)
1127 error ("%s cannot be used in asm here", reg_names [FRAME_POINTER_REGNUM]);
1128 else
1129 df_set_regs_ever_live (FRAME_POINTER_REGNUM, true);
1130 #endif
1135 /* The element value is nonzero if the corresponding regno value is
1136 invariant. */
1137 int *reg_equiv_invariant_p;
1139 /* The element value is equiv constant or NULL_RTX. */
1140 rtx *reg_equiv_const;
1142 /* The function sets up the two array declaraed above. */
1143 static void
1144 find_reg_equiv_invariant_const (void)
1146 int i, invariant_p;
1147 rtx list, insn, note, constant, x;
1149 for (i = FIRST_PSEUDO_REGISTER; i < reg_equiv_init_size; i++)
1151 constant = NULL_RTX;
1152 invariant_p = FALSE;
1153 for (list = reg_equiv_init [i]; list != NULL_RTX; list = XEXP (list, 1))
1155 insn = XEXP (list, 0);
1156 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
1158 if (note == NULL_RTX)
1159 continue;
1161 x = XEXP (note, 0);
1163 if (! function_invariant_p (x)
1164 || ! flag_pic
1165 /* A function invariant is often CONSTANT_P but may
1166 include a register. We promise to only pass CONSTANT_P
1167 objects to LEGITIMATE_PIC_OPERAND_P. */
1168 || (CONSTANT_P (x) && LEGITIMATE_PIC_OPERAND_P (x)))
1170 /* It can happen that a REG_EQUIV note contains a MEM that
1171 is not a legitimate memory operand. As later stages of
1172 reload assume that all addresses found in the
1173 reg_equiv_* arrays were originally legitimate, we
1174 ignore such REG_EQUIV notes. */
1175 if (memory_operand (x, VOIDmode))
1176 continue;
1177 else if (function_invariant_p (x))
1179 if (GET_CODE (x) == PLUS
1180 || x == frame_pointer_rtx || x == arg_pointer_rtx)
1181 invariant_p = TRUE;
1182 else
1183 constant = x;
1187 reg_equiv_invariant_p [i] = invariant_p;
1188 reg_equiv_const [i] = constant;
1194 /* The function sets up REG_RENUMBER and CALLER_SAVE_NEEDED used by
1195 reload from the allocation found by IRA. */
1196 static void
1197 setup_reg_renumber (void)
1199 int i, regno, hard_regno;
1200 allocno_t a;
1202 caller_save_needed = 0;
1203 for (i = 0; i < allocnos_num; i++)
1205 a = allocnos [i];
1206 /* There are no caps at this point. */
1207 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
1208 if (! ALLOCNO_ASSIGNED_P (a))
1209 ALLOCNO_ASSIGNED_P (a) = TRUE;
1210 ira_assert (ALLOCNO_ASSIGNED_P (a));
1211 hard_regno = ALLOCNO_HARD_REGNO (a);
1212 regno = (int) REGNO (ALLOCNO_REG (a));
1213 reg_renumber [regno] = (hard_regno < 0 ? -1 : hard_regno);
1214 if (hard_regno >= 0 && ALLOCNO_CALLS_CROSSED_NUM (a) != 0
1215 && ! hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
1216 call_used_reg_set))
1218 ira_assert (flag_caller_saves || reg_equiv_const [regno]
1219 || reg_equiv_invariant_p [regno]);
1220 caller_save_needed = 1;
1225 /* The function sets up allocno assignment flags for further
1226 allocation improvements. */
1227 static void
1228 setup_allocno_assignment_flags (void)
1230 int i, hard_regno;
1231 allocno_t a;
1233 for (i = 0; i < allocnos_num; i++)
1235 a = allocnos [i];
1236 hard_regno = ALLOCNO_HARD_REGNO (a);
1237 /* Don't assign hard registers to allocnos which are destination
1238 of removed store at the end of loop. It has a few sense to
1239 keep the same value in different hard registers. It is also
1240 impossible to assign hard registers correctly to such
1241 allocnos because the cost info and info about intersected
1242 calls are incorrect for them. */
1243 ALLOCNO_ASSIGNED_P (a) = (hard_regno >= 0
1244 || ALLOCNO_MEM_OPTIMIZED_DEST_P (a));
1245 ira_assert (hard_regno < 0
1246 || ! hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
1247 reg_class_contents
1248 [ALLOCNO_COVER_CLASS (a)]));
1252 /* The function evaluates overall allocation cost and costs for using
1253 registers and memory for allocnos. */
1255 static void
1256 calculate_allocation_cost (void)
1258 int i, hard_regno, cost;
1259 allocno_t a;
1261 overall_cost = reg_cost = mem_cost = 0;
1262 for (i = 0; i < allocnos_num; i++)
1264 a = allocnos [i];
1265 hard_regno = ALLOCNO_HARD_REGNO (a);
1266 ira_assert (hard_regno < 0
1267 || ! hard_reg_not_in_set_p
1268 (hard_regno, ALLOCNO_MODE (a),
1269 reg_class_contents [ALLOCNO_COVER_CLASS (a)]));
1270 if (hard_regno < 0)
1272 cost = ALLOCNO_UPDATED_MEMORY_COST (a);
1273 mem_cost += cost;
1275 else if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
1277 cost = (ALLOCNO_HARD_REG_COSTS (a)
1278 [class_hard_reg_index
1279 [ALLOCNO_COVER_CLASS (a)] [hard_regno]]);
1280 reg_cost += cost;
1282 else
1284 cost = ALLOCNO_COVER_CLASS_COST (a);
1285 reg_cost += cost;
1287 overall_cost += cost;
1290 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1292 fprintf (ira_dump_file,
1293 "+++Costs: overall %d, reg %d, mem %d, ld %d, st %d, move %d\n",
1294 overall_cost, reg_cost, mem_cost,
1295 load_cost, store_cost, shuffle_cost);
1296 fprintf (ira_dump_file, "+++ move loops %d, new jumps %d\n",
1297 move_loops_num, additional_jumps_num);
1302 #ifdef ENABLE_IRA_CHECKING
1303 /* The function checks correctness of the allocation. */
1304 static void
1305 check_allocation (void)
1307 allocno_t a, conflict_a, *allocno_vec;
1308 int i, hard_regno, conflict_hard_regno, j, nregs, conflict_nregs;
1310 for (i = 0; i < allocnos_num; i++)
1312 a = allocnos [i];
1313 if (ALLOCNO_CAP_MEMBER (a) != NULL
1314 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
1315 continue;
1316 nregs = hard_regno_nregs [hard_regno] [ALLOCNO_MODE (a)];
1317 allocno_vec = ALLOCNO_CONFLICT_ALLOCNO_VEC (a);
1318 for (j = 0; (conflict_a = allocno_vec [j]) != NULL; j++)
1319 if ((conflict_hard_regno = ALLOCNO_HARD_REGNO (conflict_a)) >= 0)
1321 conflict_nregs
1322 = (hard_regno_nregs
1323 [conflict_hard_regno] [ALLOCNO_MODE (conflict_a)]);
1324 if ((conflict_hard_regno <= hard_regno
1325 && hard_regno < conflict_hard_regno + conflict_nregs)
1326 || (hard_regno <= conflict_hard_regno
1327 && conflict_hard_regno < hard_regno + nregs))
1329 fprintf (stderr, "bad allocation for %d and %d\n",
1330 ALLOCNO_REGNO (a), ALLOCNO_REGNO (conflict_a));
1331 gcc_unreachable ();
1336 #endif
1338 /* The function fixes values of array REG_EQUIV_INIT after live range
1339 splitting done by IRA. */
1340 static void
1341 fix_reg_equiv_init (void)
1343 int max_regno = max_reg_num ();
1344 int i, new_regno;
1345 rtx x, prev, next, insn, set;
1348 if (reg_equiv_init_size < max_regno)
1350 reg_equiv_init = ggc_realloc (reg_equiv_init, max_regno * sizeof (rtx));
1351 while (reg_equiv_init_size < max_regno)
1352 reg_equiv_init [reg_equiv_init_size++] = NULL_RTX;
1353 for (i = FIRST_PSEUDO_REGISTER; i < reg_equiv_init_size; i++)
1354 for (prev = NULL_RTX, x = reg_equiv_init [i]; x != NULL_RTX; x = next)
1356 next = XEXP (x, 1);
1357 insn = XEXP (x, 0);
1358 set = single_set (insn);
1359 ira_assert (set != NULL_RTX
1360 && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))));
1361 if (REG_P (SET_DEST (set))
1362 && ((int) REGNO (SET_DEST (set)) == i
1363 || (int) ORIGINAL_REGNO (SET_DEST (set)) == i))
1364 new_regno = REGNO (SET_DEST (set));
1365 else if (REG_P (SET_SRC (set))
1366 && ((int) REGNO (SET_SRC (set)) == i
1367 || (int) ORIGINAL_REGNO (SET_SRC (set)) == i))
1368 new_regno = REGNO (SET_SRC (set));
1369 else
1370 gcc_unreachable ();
1371 if (new_regno == i)
1372 prev = x;
1373 else
1375 if (prev == NULL_RTX)
1376 reg_equiv_init [i] = next;
1377 else
1378 XEXP (prev, 1) = next;
1379 XEXP (x, 1) = reg_equiv_init [new_regno];
1380 reg_equiv_init [new_regno] = x;
1386 #ifdef ENABLE_IRA_CHECKING
1387 /* The function prints redundant memory memory copies. */
1388 static void
1389 print_redundant_copies (void)
1391 int i, hard_regno;
1392 allocno_t a;
1393 copy_t cp, next_cp;
1395 for (i = 0; i < allocnos_num; i++)
1397 a = allocnos [i];
1398 if (ALLOCNO_CAP_MEMBER (a) != NULL)
1399 /* It is a cap. */
1400 continue;
1401 hard_regno = ALLOCNO_HARD_REGNO (a);
1402 if (hard_regno >= 0)
1403 continue;
1404 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1405 if (cp->first == a)
1406 next_cp = cp->next_first_allocno_copy;
1407 else
1409 next_cp = cp->next_second_allocno_copy;
1410 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL
1411 && cp->insn != NULL_RTX
1412 && ALLOCNO_HARD_REGNO (cp->first) == hard_regno)
1413 fprintf (ira_dump_file,
1414 " Redundant move from %d(freq %d):%d\n",
1415 INSN_UID (cp->insn), cp->freq, hard_regno);
1419 #endif
1421 /* Setup preferred and alternative classes for pseudo-registers for
1422 other passes. */
1423 static void
1424 setup_preferred_alternate_classes (void)
1426 int i;
1427 enum reg_class cover_class;
1428 allocno_t a;
1430 for (i = 0; i < allocnos_num; i++)
1432 a = allocnos [i];
1433 cover_class = ALLOCNO_COVER_CLASS (a);
1434 if (cover_class == NO_REGS)
1435 cover_class = GENERAL_REGS;
1436 setup_reg_classes (ALLOCNO_REGNO (a), cover_class, NO_REGS);
1442 static void
1443 expand_reg_info (int old_size)
1445 int i;
1446 int size = max_reg_num ();
1448 resize_reg_info ();
1449 for (i = old_size; i < size; i++)
1451 reg_renumber [i] = -1;
1452 setup_reg_classes (i, GENERAL_REGS, ALL_REGS);
1458 /* Map bb index -> order number in the BB chain. */
1459 static int *basic_block_order_nums;
1461 /* The function is used to sort insn chain according insn execution
1462 frequencies. */
1463 static int
1464 chain_freq_compare (const void *v1p, const void *v2p)
1466 struct insn_chain *c1 = *(struct insn_chain **)v1p;
1467 struct insn_chain *c2 = *(struct insn_chain **)v2p;
1468 int diff;
1470 diff = (BASIC_BLOCK (c2->block)->frequency
1471 - BASIC_BLOCK (c1->block)->frequency);
1472 if (diff)
1473 return diff;
1474 return (char *) v1p - (char *) v2p;
1477 /* The function is used to sort insn chain according insn original
1478 order. */
1479 static int
1480 chain_bb_compare (const void *v1p, const void *v2p)
1482 struct insn_chain *c1 = *(struct insn_chain **)v1p;
1483 struct insn_chain *c2 = *(struct insn_chain **)v2p;
1484 int diff;
1486 diff = (basic_block_order_nums [c1->block]
1487 - basic_block_order_nums [c2->block]);
1488 if (diff)
1489 return diff;
1490 return (char *) v1p - (char *) v2p;
1493 /* The function sorts insn chain according insn frequencies (if
1494 FREQ_P) or insn original order. */
1495 void
1496 sort_insn_chain (int freq_p)
1498 struct insn_chain *chain, **chain_arr;
1499 basic_block bb;
1500 int i, n;
1502 for (n = 0, chain = reload_insn_chain; chain != 0; chain = chain->next)
1503 n++;
1504 if (n <= 1)
1505 return;
1506 chain_arr = ira_allocate (n * sizeof (struct insn_chain *));
1507 basic_block_order_nums = ira_allocate (sizeof (int) * last_basic_block);
1508 n = 0;
1509 FOR_EACH_BB (bb)
1511 basic_block_order_nums [bb->index] = n++;
1513 for (n = 0, chain = reload_insn_chain; chain != 0; chain = chain->next)
1514 chain_arr [n++] = chain;
1515 qsort (chain_arr, n, sizeof (struct insn_chain *),
1516 freq_p ? chain_freq_compare : chain_bb_compare);
1517 for (i = 1; i < n - 1; i++)
1519 chain_arr [i]->next = chain_arr [i + 1];
1520 chain_arr [i]->prev = chain_arr [i - 1];
1522 chain_arr [i]->next = NULL;
1523 chain_arr [i]->prev = chain_arr [i - 1];
1524 reload_insn_chain = chain_arr [0];
1525 reload_insn_chain->prev = NULL;
1526 reload_insn_chain->next = chain_arr [1];
1527 ira_free (basic_block_order_nums);
1528 ira_free (chain_arr);
1534 /* This is the main entry of IRA. */
1535 void
1536 ira (FILE *f)
1538 int overall_cost_before, loops_p, allocated_reg_info_size;
1539 int max_regno_before_ira, max_point_before_emit;
1540 int rebuild_p;
1542 if (flag_ira_verbose < 10)
1544 internal_flag_ira_verbose = flag_ira_verbose;
1545 ira_dump_file = f;
1547 else
1549 internal_flag_ira_verbose = flag_ira_verbose - 10;
1550 ira_dump_file = stderr;
1553 setup_prohibited_mode_move_regs ();
1555 df_note_add_problem ();
1557 if (optimize == 1)
1559 df_live_add_problem ();
1560 df_live_set_all_dirty ();
1562 df_analyze ();
1564 df_clear_flags (DF_NO_INSN_RESCAN);
1566 allocno_pool = create_alloc_pool ("allocnos", sizeof (struct allocno), 100);
1567 copy_pool = create_alloc_pool ("copies", sizeof (struct allocno_copy), 100);
1568 allocno_live_range_pool
1569 = create_alloc_pool ("allocno live ranges",
1570 sizeof (struct allocno_live_range), 100);
1571 regstat_init_n_sets_and_refs ();
1572 regstat_compute_ri ();
1573 rebuild_p = update_equiv_regs ();
1574 regstat_free_n_sets_and_refs ();
1575 regstat_free_ri ();
1577 #ifndef IRA_NO_OBSTACK
1578 gcc_obstack_init (&ira_obstack);
1579 #endif
1580 bitmap_obstack_initialize (&ira_bitmap_obstack);
1582 max_regno = max_reg_num ();
1583 reg_equiv_invariant_p = ira_allocate (max_regno * sizeof (int));
1584 memset (reg_equiv_invariant_p, 0, max_regno * sizeof (int));
1585 reg_equiv_const = ira_allocate (max_regno * sizeof (rtx));
1586 memset (reg_equiv_const, 0, max_regno * sizeof (rtx));
1587 find_reg_equiv_invariant_const ();
1588 if (rebuild_p)
1590 timevar_push (TV_JUMP);
1591 rebuild_jump_labels (get_insns ());
1592 purge_all_dead_edges ();
1593 timevar_pop (TV_JUMP);
1595 max_regno_before_ira = allocated_reg_info_size = max_reg_num ();
1596 allocate_reg_info ();
1597 setup_eliminable_regset ();
1599 overall_cost = reg_cost = mem_cost = 0;
1600 load_cost = store_cost = shuffle_cost = 0;
1601 move_loops_num = additional_jumps_num = 0;
1602 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1603 fprintf (ira_dump_file, "Building IRA IR\n");
1604 loops_p = ira_build (flag_ira_algorithm == IRA_ALGORITHM_REGIONAL
1605 || flag_ira_algorithm == IRA_ALGORITHM_MIXED);
1606 ira_color ();
1608 max_point_before_emit = max_point;
1610 ira_emit (loops_p);
1612 max_regno = max_reg_num ();
1614 if (! loops_p)
1615 initiate_ira_assign ();
1616 else
1618 expand_reg_info (allocated_reg_info_size);
1619 allocated_reg_info_size = max_regno;
1621 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1622 fprintf (ira_dump_file, "Flattening IR\n");
1623 ira_flattening (max_regno_before_ira, max_point_before_emit);
1624 /* New insns were generated: add notes and recaclulate live
1625 info. */
1626 df_analyze ();
1628 basic_block bb;
1630 FOR_ALL_BB (bb)
1631 bb->loop_father = NULL;
1632 current_loops = NULL;
1634 setup_allocno_assignment_flags ();
1635 initiate_ira_assign ();
1636 reassign_conflict_allocnos (max_regno, FALSE);
1639 setup_reg_renumber ();
1641 calculate_allocation_cost ();
1643 #ifdef ENABLE_IRA_CHECKING
1644 check_allocation ();
1645 #endif
1647 setup_preferred_alternate_classes ();
1649 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1650 max_regno = max_reg_num ();
1652 /* Determine if the current function is a leaf before running IRA
1653 since this can impact optimizations done by the prologue and
1654 epilogue thus changing register elimination offsets. */
1655 current_function_is_leaf = leaf_function_p ();
1657 /* And the reg_equiv_memory_loc array. */
1658 VEC_safe_grow (rtx, gc, reg_equiv_memory_loc_vec, max_regno);
1659 memset (VEC_address (rtx, reg_equiv_memory_loc_vec), 0,
1660 sizeof (rtx) * max_regno);
1661 reg_equiv_memory_loc = VEC_address (rtx, reg_equiv_memory_loc_vec);
1663 allocate_initial_values (reg_equiv_memory_loc);
1665 regstat_init_n_sets_and_refs ();
1666 regstat_compute_ri ();
1668 fix_reg_equiv_init ();
1670 #ifdef ENABLE_IRA_CHECKING
1671 print_redundant_copies ();
1672 #endif
1674 overall_cost_before = overall_cost;
1676 spilled_reg_stack_slots_num = 0;
1677 spilled_reg_stack_slots
1678 = ira_allocate (max_regno * sizeof (struct spilled_reg_stack_slot));
1680 df_set_flags (DF_NO_INSN_RESCAN);
1681 build_insn_chain ();
1682 sort_insn_chain (TRUE);
1683 reload_completed = ! reload (get_insns (), 1);
1685 ira_free (spilled_reg_stack_slots);
1687 finish_ira_assign ();
1689 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL
1690 && overall_cost_before != overall_cost)
1691 fprintf (ira_dump_file, "+++Overall after reload %d\n", overall_cost);
1693 ira_destroy ();
1695 cleanup_cfg (CLEANUP_EXPENSIVE);
1697 regstat_free_ri ();
1698 regstat_free_n_sets_and_refs ();
1700 ira_free (reg_equiv_invariant_p);
1701 ira_free (reg_equiv_const);
1703 free_alloc_pool (allocno_live_range_pool);
1704 free_alloc_pool (copy_pool);
1705 free_alloc_pool (allocno_pool);
1707 bitmap_obstack_release (&ira_bitmap_obstack);
1708 #ifndef IRA_NO_OBSTACK
1709 obstack_free (&ira_obstack, NULL);
1710 #endif
1712 /* The code after the reload has changed so much that at this point
1713 we might as well just rescan everything. Not that
1714 df_rescan_all_insns is not going to help here because it does not
1715 touch the artificial uses and defs. */
1716 df_finish_pass (true);
1717 if (optimize > 1)
1718 df_live_add_problem ();
1719 df_scan_alloc (NULL);
1720 df_scan_blocks ();
1722 if (optimize)
1723 df_analyze ();
1728 static bool
1729 gate_ira (void)
1731 return flag_ira != 0;
1734 /* Run the integrated register allocator. */
1735 static unsigned int
1736 rest_of_handle_ira (void)
1738 ira (dump_file);
1739 return 0;
1742 struct tree_opt_pass pass_ira =
1744 "ira", /* name */
1745 gate_ira, /* gate */
1746 rest_of_handle_ira, /* execute */
1747 NULL, /* sub */
1748 NULL, /* next */
1749 0, /* static_pass_number */
1750 TV_IRA, /* tv_id */
1751 0, /* properties_required */
1752 0, /* properties_provided */
1753 0, /* properties_destroyed */
1754 0, /* todo_flags_start */
1755 TODO_dump_func |
1756 TODO_ggc_collect, /* todo_flags_finish */
1757 'y' /* letter */