2008-03-03 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ira.c
blob8041a85524f088c3b034109e6b635a41c94d633a
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 element value is nonzero if the corresponding regno value is
1121 invariant. */
1122 int *reg_equiv_invariant_p;
1124 /* The element value is equiv constant or NULL_RTX. */
1125 rtx *reg_equiv_const;
1127 /* The function sets up the two array declaraed above. */
1128 static void
1129 find_reg_equiv_invariant_const (void)
1131 int i, invariant_p;
1132 rtx list, insn, note, constant, x;
1134 for (i = FIRST_PSEUDO_REGISTER; i < reg_equiv_init_size; i++)
1136 constant = NULL_RTX;
1137 invariant_p = FALSE;
1138 for (list = reg_equiv_init [i]; list != NULL_RTX; list = XEXP (list, 1))
1140 insn = XEXP (list, 0);
1141 note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
1143 if (note == NULL_RTX)
1144 continue;
1146 x = XEXP (note, 0);
1148 if (! function_invariant_p (x)
1149 || ! flag_pic
1150 /* A function invariant is often CONSTANT_P but may
1151 include a register. We promise to only pass CONSTANT_P
1152 objects to LEGITIMATE_PIC_OPERAND_P. */
1153 || (CONSTANT_P (x) && LEGITIMATE_PIC_OPERAND_P (x)))
1155 /* It can happen that a REG_EQUIV note contains a MEM that
1156 is not a legitimate memory operand. As later stages of
1157 reload assume that all addresses found in the
1158 reg_equiv_* arrays were originally legitimate, we
1159 ignore such REG_EQUIV notes. */
1160 if (memory_operand (x, VOIDmode))
1161 continue;
1162 else if (function_invariant_p (x))
1164 if (GET_CODE (x) == PLUS
1165 || x == frame_pointer_rtx || x == arg_pointer_rtx)
1166 invariant_p = TRUE;
1167 else
1168 constant = x;
1172 reg_equiv_invariant_p [i] = invariant_p;
1173 reg_equiv_const [i] = constant;
1179 /* The function sets up REG_RENUMBER and CALLER_SAVE_NEEDED used by
1180 reload from the allocation found by IRA. */
1181 static void
1182 setup_reg_renumber (void)
1184 int i, regno, hard_regno;
1185 allocno_t a;
1187 caller_save_needed = 0;
1188 for (i = 0; i < allocnos_num; i++)
1190 a = allocnos [i];
1191 /* There are no caps at this point. */
1192 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
1193 if (! ALLOCNO_ASSIGNED_P (a))
1194 ALLOCNO_ASSIGNED_P (a) = TRUE;
1195 ira_assert (ALLOCNO_ASSIGNED_P (a));
1196 hard_regno = ALLOCNO_HARD_REGNO (a);
1197 regno = (int) REGNO (ALLOCNO_REG (a));
1198 reg_renumber [regno] = (hard_regno < 0 ? -1 : hard_regno);
1199 if (hard_regno >= 0 && ALLOCNO_CALLS_CROSSED_NUM (a) != 0
1200 && ! hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
1201 call_used_reg_set))
1203 ira_assert (flag_caller_saves || reg_equiv_const [regno]
1204 || reg_equiv_invariant_p [regno]);
1205 caller_save_needed = 1;
1210 /* The function sets up allocno assignment flags for further
1211 allocation improvements. */
1212 static void
1213 setup_allocno_assignment_flags (void)
1215 int i, hard_regno;
1216 allocno_t a;
1218 for (i = 0; i < allocnos_num; i++)
1220 a = allocnos [i];
1221 hard_regno = ALLOCNO_HARD_REGNO (a);
1222 /* Don't assign hard registers to allocnos which are destination
1223 of removed store at the end of loop. It has a few sense to
1224 keep the same value in different hard registers. It is also
1225 impossible to assign hard registers correctly to such
1226 allocnos because the cost info and info about intersected
1227 calls are incorrect for them. */
1228 ALLOCNO_ASSIGNED_P (a) = (hard_regno >= 0
1229 || ALLOCNO_MEM_OPTIMIZED_DEST_P (a));
1230 ira_assert (hard_regno < 0
1231 || ! hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
1232 reg_class_contents
1233 [ALLOCNO_COVER_CLASS (a)]));
1237 /* The function evaluates overall allocation cost and costs for using
1238 registers and memory for allocnos. */
1240 static void
1241 calculate_allocation_cost (void)
1243 int i, hard_regno, cost;
1244 allocno_t a;
1246 overall_cost = reg_cost = mem_cost = 0;
1247 for (i = 0; i < allocnos_num; i++)
1249 a = allocnos [i];
1250 hard_regno = ALLOCNO_HARD_REGNO (a);
1251 ira_assert (hard_regno < 0
1252 || ! hard_reg_not_in_set_p
1253 (hard_regno, ALLOCNO_MODE (a),
1254 reg_class_contents [ALLOCNO_COVER_CLASS (a)]));
1255 if (hard_regno < 0)
1257 cost = ALLOCNO_MEMORY_COST (a);
1258 mem_cost += cost;
1260 else if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
1262 cost = (ALLOCNO_HARD_REG_COSTS (a)
1263 [class_hard_reg_index
1264 [ALLOCNO_COVER_CLASS (a)] [hard_regno]]);
1265 reg_cost += cost;
1267 else
1269 cost = ALLOCNO_COVER_CLASS_COST (a);
1270 reg_cost += cost;
1272 overall_cost += cost;
1275 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1277 fprintf (ira_dump_file,
1278 "+++Costs: overall %d, reg %d, mem %d, ld %d, st %d, move %d\n",
1279 overall_cost, reg_cost, mem_cost,
1280 load_cost, store_cost, shuffle_cost);
1281 fprintf (ira_dump_file, "+++ move loops %d, new jumps %d\n",
1282 move_loops_num, additional_jumps_num);
1287 #ifdef ENABLE_IRA_CHECKING
1288 /* The function checks correctness of the allocation. */
1289 static void
1290 check_allocation (void)
1292 allocno_t a, conflict_a, *allocno_vec;
1293 int i, hard_regno, conflict_hard_regno, j, nregs, conflict_nregs;
1295 for (i = 0; i < allocnos_num; i++)
1297 a = allocnos [i];
1298 if (ALLOCNO_CAP_MEMBER (a) != NULL
1299 || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
1300 continue;
1301 nregs = hard_regno_nregs [hard_regno] [ALLOCNO_MODE (a)];
1302 allocno_vec = ALLOCNO_CONFLICT_ALLOCNO_VEC (a);
1303 for (j = 0; (conflict_a = allocno_vec [j]) != NULL; j++)
1304 if ((conflict_hard_regno = ALLOCNO_HARD_REGNO (conflict_a)) >= 0)
1306 conflict_nregs
1307 = (hard_regno_nregs
1308 [conflict_hard_regno] [ALLOCNO_MODE (conflict_a)]);
1309 if ((conflict_hard_regno <= hard_regno
1310 && hard_regno < conflict_hard_regno + conflict_nregs)
1311 || (hard_regno <= conflict_hard_regno
1312 && conflict_hard_regno < hard_regno + nregs))
1314 fprintf (stderr, "bad allocation for %d and %d\n",
1315 ALLOCNO_REGNO (a), ALLOCNO_REGNO (conflict_a));
1316 gcc_unreachable ();
1321 #endif
1323 /* The function fixes values of array REG_EQUIV_INIT after live range
1324 splitting done by IRA. */
1325 static void
1326 fix_reg_equiv_init (void)
1328 int max_regno = max_reg_num ();
1329 int i, new_regno;
1330 rtx x, prev, next, insn, set;
1333 if (reg_equiv_init_size < max_regno)
1335 reg_equiv_init = ggc_realloc (reg_equiv_init, max_regno * sizeof (rtx));
1336 while (reg_equiv_init_size < max_regno)
1337 reg_equiv_init [reg_equiv_init_size++] = NULL_RTX;
1338 for (i = FIRST_PSEUDO_REGISTER; i < reg_equiv_init_size; i++)
1339 for (prev = NULL_RTX, x = reg_equiv_init [i]; x != NULL_RTX; x = next)
1341 next = XEXP (x, 1);
1342 insn = XEXP (x, 0);
1343 set = single_set (insn);
1344 ira_assert (set != NULL_RTX
1345 && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))));
1346 if (REG_P (SET_DEST (set))
1347 && ((int) REGNO (SET_DEST (set)) == i
1348 || (int) ORIGINAL_REGNO (SET_DEST (set)) == i))
1349 new_regno = REGNO (SET_DEST (set));
1350 else if (REG_P (SET_SRC (set))
1351 && ((int) REGNO (SET_SRC (set)) == i
1352 || (int) ORIGINAL_REGNO (SET_SRC (set)) == i))
1353 new_regno = REGNO (SET_SRC (set));
1354 else
1355 gcc_unreachable ();
1356 if (new_regno == i)
1357 prev = x;
1358 else
1360 if (prev == NULL_RTX)
1361 reg_equiv_init [i] = next;
1362 else
1363 XEXP (prev, 1) = next;
1364 XEXP (x, 1) = reg_equiv_init [new_regno];
1365 reg_equiv_init [new_regno] = x;
1371 #ifdef ENABLE_IRA_CHECKING
1372 /* The function prints redundant memory memory copies. */
1373 static void
1374 print_redundant_copies (void)
1376 int i, hard_regno;
1377 allocno_t a;
1378 copy_t cp, next_cp;
1380 for (i = 0; i < allocnos_num; i++)
1382 a = allocnos [i];
1383 if (ALLOCNO_CAP_MEMBER (a) != NULL)
1384 /* It is a cap. */
1385 continue;
1386 hard_regno = ALLOCNO_HARD_REGNO (a);
1387 if (hard_regno >= 0)
1388 continue;
1389 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1390 if (cp->first == a)
1391 next_cp = cp->next_first_allocno_copy;
1392 else
1394 next_cp = cp->next_second_allocno_copy;
1395 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL
1396 && cp->insn != NULL_RTX
1397 && ALLOCNO_HARD_REGNO (cp->first) == hard_regno)
1398 fprintf (ira_dump_file,
1399 " Redundant move from %d(freq %d):%d\n",
1400 INSN_UID (cp->insn), cp->freq, hard_regno);
1404 #endif
1406 /* Setup preferred and alternative classes for pseudo-registers for
1407 other passes. */
1408 static void
1409 setup_preferred_alternate_classes (void)
1411 int i;
1412 enum reg_class cover_class;
1413 allocno_t a;
1415 for (i = 0; i < allocnos_num; i++)
1417 a = allocnos [i];
1418 cover_class = ALLOCNO_COVER_CLASS (a);
1419 if (cover_class == NO_REGS)
1420 cover_class = GENERAL_REGS;
1421 setup_reg_classes (ALLOCNO_REGNO (a), cover_class, NO_REGS);
1427 static void
1428 expand_reg_info (int old_size)
1430 int i;
1431 int size = max_reg_num ();
1433 resize_reg_info ();
1434 for (i = old_size; i < size; i++)
1436 reg_renumber [i] = -1;
1437 setup_reg_classes (i, GENERAL_REGS, ALL_REGS);
1443 /* Map bb index -> order number in the BB chain. */
1444 static int *basic_block_order_nums;
1446 /* The function is used to sort insn chain according insn execution
1447 frequencies. */
1448 static int
1449 chain_freq_compare (const void *v1p, const void *v2p)
1451 struct insn_chain *c1 = *(struct insn_chain **)v1p;
1452 struct insn_chain *c2 = *(struct insn_chain **)v2p;
1453 int diff;
1455 diff = (BASIC_BLOCK (c2->block)->frequency
1456 - BASIC_BLOCK (c1->block)->frequency);
1457 if (diff)
1458 return diff;
1459 return (char *) v1p - (char *) v2p;
1462 /* The function is used to sort insn chain according insn original
1463 order. */
1464 static int
1465 chain_bb_compare (const void *v1p, const void *v2p)
1467 struct insn_chain *c1 = *(struct insn_chain **)v1p;
1468 struct insn_chain *c2 = *(struct insn_chain **)v2p;
1469 int diff;
1471 diff = (basic_block_order_nums [c1->block]
1472 - basic_block_order_nums [c2->block]);
1473 if (diff)
1474 return diff;
1475 return (char *) v1p - (char *) v2p;
1478 /* The function sorts insn chain according insn frequencies (if
1479 FREQ_P) or insn original order. */
1480 void
1481 sort_insn_chain (int freq_p)
1483 struct insn_chain *chain, **chain_arr;
1484 basic_block bb;
1485 int i, n;
1487 for (n = 0, chain = reload_insn_chain; chain != 0; chain = chain->next)
1488 n++;
1489 if (n <= 1)
1490 return;
1491 chain_arr = ira_allocate (n * sizeof (struct insn_chain *));
1492 basic_block_order_nums = ira_allocate (sizeof (int) * last_basic_block);
1493 n = 0;
1494 FOR_EACH_BB (bb)
1496 basic_block_order_nums [bb->index] = n++;
1498 for (n = 0, chain = reload_insn_chain; chain != 0; chain = chain->next)
1499 chain_arr [n++] = chain;
1500 qsort (chain_arr, n, sizeof (struct insn_chain *),
1501 freq_p ? chain_freq_compare : chain_bb_compare);
1502 for (i = 1; i < n - 1; i++)
1504 chain_arr [i]->next = chain_arr [i + 1];
1505 chain_arr [i]->prev = chain_arr [i - 1];
1507 chain_arr [i]->next = NULL;
1508 chain_arr [i]->prev = chain_arr [i - 1];
1509 reload_insn_chain = chain_arr [0];
1510 reload_insn_chain->prev = NULL;
1511 reload_insn_chain->next = chain_arr [1];
1512 ira_free (basic_block_order_nums);
1513 ira_free (chain_arr);
1518 /* All natural loops. */
1519 struct loops ira_loops;
1521 /* This is the main entry of IRA. */
1522 void
1523 ira (FILE *f)
1525 int overall_cost_before, loops_p, allocated_reg_info_size;
1526 int max_regno_before_ira, max_point_before_emit;
1527 int rebuild_p, saved_flag_ira_algorithm;
1528 basic_block bb;
1530 if (flag_ira_verbose < 10)
1532 internal_flag_ira_verbose = flag_ira_verbose;
1533 ira_dump_file = f;
1535 else
1537 internal_flag_ira_verbose = flag_ira_verbose - 10;
1538 ira_dump_file = stderr;
1541 setup_prohibited_mode_move_regs ();
1543 df_note_add_problem ();
1545 if (optimize == 1)
1547 df_live_add_problem ();
1548 df_live_set_all_dirty ();
1550 df_analyze ();
1552 df_clear_flags (DF_NO_INSN_RESCAN);
1554 allocno_pool = create_alloc_pool ("allocnos", sizeof (struct allocno), 100);
1555 copy_pool = create_alloc_pool ("copies", sizeof (struct allocno_copy), 100);
1556 allocno_live_range_pool
1557 = create_alloc_pool ("allocno live ranges",
1558 sizeof (struct allocno_live_range), 100);
1559 regstat_init_n_sets_and_refs ();
1560 regstat_compute_ri ();
1561 rebuild_p = update_equiv_regs ();
1562 regstat_free_n_sets_and_refs ();
1563 regstat_free_ri ();
1565 #ifndef IRA_NO_OBSTACK
1566 gcc_obstack_init (&ira_obstack);
1567 #endif
1568 bitmap_obstack_initialize (&ira_bitmap_obstack);
1570 max_regno = max_reg_num ();
1571 reg_equiv_invariant_p = ira_allocate (max_regno * sizeof (int));
1572 memset (reg_equiv_invariant_p, 0, max_regno * sizeof (int));
1573 reg_equiv_const = ira_allocate (max_regno * sizeof (rtx));
1574 memset (reg_equiv_const, 0, max_regno * sizeof (rtx));
1575 find_reg_equiv_invariant_const ();
1576 if (rebuild_p)
1578 timevar_push (TV_JUMP);
1579 rebuild_jump_labels (get_insns ());
1580 purge_all_dead_edges ();
1581 timevar_pop (TV_JUMP);
1583 max_regno_before_ira = allocated_reg_info_size = max_reg_num ();
1584 allocate_reg_info ();
1585 setup_eliminable_regset ();
1587 overall_cost = reg_cost = mem_cost = 0;
1588 load_cost = store_cost = shuffle_cost = 0;
1589 move_loops_num = additional_jumps_num = 0;
1591 ira_assert (current_loops == NULL);
1592 flow_loops_find (&ira_loops);
1593 current_loops = &ira_loops;
1594 saved_flag_ira_algorithm = flag_ira_algorithm;
1595 if (number_of_loops () > (unsigned) IRA_MAX_LOOPS_NUM)
1596 flag_ira_algorithm = IRA_ALGORITHM_CB;
1598 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1599 fprintf (ira_dump_file, "Building IRA IR\n");
1600 loops_p = ira_build (flag_ira_algorithm == IRA_ALGORITHM_REGIONAL
1601 || flag_ira_algorithm == IRA_ALGORITHM_MIXED);
1602 ira_color ();
1604 max_point_before_emit = max_point;
1606 ira_emit (loops_p);
1608 max_regno = max_reg_num ();
1610 if (! loops_p)
1611 initiate_ira_assign ();
1612 else
1614 expand_reg_info (allocated_reg_info_size);
1615 allocated_reg_info_size = max_regno;
1617 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1618 fprintf (ira_dump_file, "Flattening IR\n");
1619 ira_flattening (max_regno_before_ira, max_point_before_emit);
1620 /* New insns were generated: add notes and recaclulate live
1621 info. */
1622 df_analyze ();
1624 basic_block bb;
1626 FOR_ALL_BB (bb)
1627 bb->loop_father = NULL;
1628 current_loops = NULL;
1630 setup_allocno_assignment_flags ();
1631 initiate_ira_assign ();
1632 reassign_conflict_allocnos (max_regno, FALSE);
1635 setup_reg_renumber ();
1637 calculate_allocation_cost ();
1639 #ifdef ENABLE_IRA_CHECKING
1640 check_allocation ();
1641 #endif
1643 setup_preferred_alternate_classes ();
1645 delete_trivially_dead_insns (get_insns (), max_reg_num ());
1646 max_regno = max_reg_num ();
1648 /* Determine if the current function is a leaf before running IRA
1649 since this can impact optimizations done by the prologue and
1650 epilogue thus changing register elimination offsets. */
1651 current_function_is_leaf = leaf_function_p ();
1653 /* And the reg_equiv_memory_loc array. */
1654 VEC_safe_grow (rtx, gc, reg_equiv_memory_loc_vec, max_regno);
1655 memset (VEC_address (rtx, reg_equiv_memory_loc_vec), 0,
1656 sizeof (rtx) * max_regno);
1657 reg_equiv_memory_loc = VEC_address (rtx, reg_equiv_memory_loc_vec);
1659 allocate_initial_values (reg_equiv_memory_loc);
1661 regstat_init_n_sets_and_refs ();
1662 regstat_compute_ri ();
1664 fix_reg_equiv_init ();
1666 #ifdef ENABLE_IRA_CHECKING
1667 print_redundant_copies ();
1668 #endif
1670 overall_cost_before = overall_cost;
1672 spilled_reg_stack_slots_num = 0;
1673 spilled_reg_stack_slots
1674 = ira_allocate (max_regno * sizeof (struct spilled_reg_stack_slot));
1676 df_set_flags (DF_NO_INSN_RESCAN);
1677 build_insn_chain ();
1678 sort_insn_chain (TRUE);
1679 reload_completed = ! reload (get_insns (), 1);
1681 ira_free (spilled_reg_stack_slots);
1683 finish_ira_assign ();
1685 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL
1686 && overall_cost_before != overall_cost)
1687 fprintf (ira_dump_file, "+++Overall after reload %d\n", overall_cost);
1689 ira_destroy ();
1691 #if 0
1692 ira_assert (current_loops == &ira_loops);
1693 #endif
1694 flow_loops_free (&ira_loops);
1695 free_dominance_info (CDI_DOMINATORS);
1696 FOR_ALL_BB (bb)
1697 bb->loop_father = NULL;
1698 current_loops = NULL;
1700 flag_ira_algorithm = saved_flag_ira_algorithm;
1702 cleanup_cfg (CLEANUP_EXPENSIVE);
1704 regstat_free_ri ();
1705 regstat_free_n_sets_and_refs ();
1707 ira_free (reg_equiv_invariant_p);
1708 ira_free (reg_equiv_const);
1710 free_alloc_pool (allocno_live_range_pool);
1711 free_alloc_pool (copy_pool);
1712 free_alloc_pool (allocno_pool);
1714 bitmap_obstack_release (&ira_bitmap_obstack);
1715 #ifndef IRA_NO_OBSTACK
1716 obstack_free (&ira_obstack, NULL);
1717 #endif
1719 /* The code after the reload has changed so much that at this point
1720 we might as well just rescan everything. Not that
1721 df_rescan_all_insns is not going to help here because it does not
1722 touch the artificial uses and defs. */
1723 df_finish_pass (true);
1724 if (optimize > 1)
1725 df_live_add_problem ();
1726 df_scan_alloc (NULL);
1727 df_scan_blocks ();
1729 if (optimize)
1730 df_analyze ();
1735 static bool
1736 gate_ira (void)
1738 return flag_ira != 0;
1741 /* Run the integrated register allocator. */
1742 static unsigned int
1743 rest_of_handle_ira (void)
1745 ira (dump_file);
1746 return 0;
1749 struct tree_opt_pass pass_ira =
1751 "ira", /* name */
1752 gate_ira, /* gate */
1753 rest_of_handle_ira, /* execute */
1754 NULL, /* sub */
1755 NULL, /* next */
1756 0, /* static_pass_number */
1757 TV_IRA, /* tv_id */
1758 0, /* properties_required */
1759 0, /* properties_provided */
1760 0, /* properties_destroyed */
1761 0, /* todo_flags_start */
1762 TODO_dump_func |
1763 TODO_ggc_collect, /* todo_flags_finish */
1764 'y' /* letter */