OSDN Git Service

Daily bump.
[pf3gnuchains/gcc-fork.git] / gcc / ira.c
1 /* Integrated Register Allocator (IRA) entry point.
2    Copyright (C) 2006, 2007, 2008
3    Free Software Foundation, Inc.
4    Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
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.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 /* The integrated register allocator (IRA) is a
23    regional register allocator performing graph coloring on a top-down
24    traversal of nested regions.  Graph coloring in a region is based
25    on Chaitin-Briggs algorithm.  It is called integrated because
26    register coalescing, register live range splitting, and choosing a
27    better hard register are done on-the-fly during coloring.  Register
28    coalescing and choosing a cheaper hard register is done by hard
29    register preferencing during hard register assigning.  The live
30    range splitting is a byproduct of the regional register allocation.
31
32    Major IRA notions are:
33
34      o *Region* is a part of CFG where graph coloring based on
35        Chaitin-Briggs algorithm is done.  IRA can work on any set of
36        nested CFG regions forming a tree.  Currently the regions are
37        the entire function for the root region and natural loops for
38        the other regions.  Therefore data structure representing a
39        region is called loop_tree_node.
40
41      o *Cover class* is a register class belonging to a set of
42        non-intersecting register classes containing all of the
43        hard-registers available for register allocation.  The set of
44        all cover classes for a target is defined in the corresponding
45        machine-description file according some criteria.  Such notion
46        is needed because Chaitin-Briggs algorithm works on
47        non-intersected register classes.
48
49      o *Allocno* represents the live range of a pseudo-register in a
50        region.  Besides the obvious attributes like the corresponding
51        pseudo-register number, cover class, conflicting allocnos and
52        conflicting hard-registers, there are a few allocno attributes
53        which are important for understanding the allocation algorithm:
54
55        - *Live ranges*.  This is a list of ranges of *program
56          points* where the allocno lives.  Program points represent
57          places where a pseudo can be born or become dead (there are
58          approximately two times more program points than the insns)
59          and they are represented by integers starting with 0.  The
60          live ranges are used to find conflicts between allocnos of
61          different cover classes.  They also play very important role
62          for the transformation of the IRA internal representation of
63          several regions into a one region representation.  The later is
64          used during the reload pass work because each allocno
65          represents all of the corresponding pseudo-registers.
66
67        - *Hard-register costs*.  This is a vector of size equal to the
68          number of available hard-registers of the allocno's cover
69          class.  The cost of a callee-clobbered hard-register for an
70          allocno is increased by the cost of save/restore code around
71          the calls through the given allocno's life.  If the allocno
72          is a move instruction operand and another operand is a
73          hard-register of the allocno's cover class, the cost of the
74          hard-register is decreased by the move cost.
75
76          When an allocno is assigned, the hard-register with minimal
77          full cost is used.  Initially, a hard-register's full cost is
78          the corresponding value from the hard-register's cost vector.
79          If the allocno is connected by a *copy* (see below) to
80          another allocno which has just received a hard-register, the
81          cost of the hard-register is decreased.  Before choosing a
82          hard-register for an allocno, the allocno's current costs of
83          the hard-registers are modified by the conflict hard-register
84          costs of all of the conflicting allocnos which are not
85          assigned yet.
86
87        - *Conflict hard-register costs*.  This is a vector of the same
88          size as the hard-register costs vector.  To permit an
89          unassigned allocno to get a better hard-register, IRA uses
90          this vector to calculate the final full cost of the
91          available hard-registers.  Conflict hard-register costs of an
92          unassigned allocno are also changed with a change of the
93          hard-register cost of the allocno when a copy involving the
94          allocno is processed as described above.  This is done to
95          show other unassigned allocnos that a given allocno prefers
96          some hard-registers in order to remove the move instruction
97          corresponding to the copy.
98
99      o *Cap*.  If a pseudo-register does not live in a region but
100        lives in a nested region, IRA creates a special allocno called
101        a cap in the outer region.  A region cap is also created for a
102        subregion cap.
103
104      o *Copy*.  Allocnos can be connected by copies.  Copies are used
105        to modify hard-register costs for allocnos during coloring.
106        Such modifications reflects a preference to use the same
107        hard-register for the allocnos connected by copies.  Usually
108        copies are created for move insns (in this case it results in
109        register coalescing).  But IRA also creates copies for operands
110        of an insn which should be assigned to the same hard-register
111        due to constraints in the machine description (it usually
112        results in removing a move generated in reload to satisfy
113        the constraints) and copies referring to the allocno which is
114        the output operand of an instruction and the allocno which is
115        an input operand dying in the instruction (creation of such
116        copies results in less register shuffling).  IRA *does not*
117        create copies between the same register allocnos from different
118        regions because we use another technique for propagating
119        hard-register preference on the borders of regions.
120
121    Allocnos (including caps) for the upper region in the region tree
122    *accumulate* information important for coloring from allocnos with
123    the same pseudo-register from nested regions.  This includes
124    hard-register and memory costs, conflicts with hard-registers,
125    allocno conflicts, allocno copies and more.  *Thus, attributes for
126    allocnos in a region have the same values as if the region had no
127    subregions*.  It means that attributes for allocnos in the
128    outermost region corresponding to the function have the same values
129    as though the allocation used only one region which is the entire
130    function.  It also means that we can look at IRA work as if the
131    first IRA did allocation for all function then it improved the
132    allocation for loops then their subloops and so on.
133
134    IRA major passes are:
135
136      o Building IRA internal representation which consists of the
137        following subpasses:
138
139        * First, IRA builds regions and creates allocnos (file
140          ira-build.c) and initializes most of their attributes.
141
142        * Then IRA finds a cover class for each allocno and calculates
143          its initial (non-accumulated) cost of memory and each
144          hard-register of its cover class (file ira-cost.c).
145
146        * IRA creates live ranges of each allocno, calulates register
147          pressure for each cover class in each region, sets up
148          conflict hard registers for each allocno and info about calls
149          the allocno lives through (file ira-lives.c).
150
151        * IRA removes low register pressure loops from the regions
152          mostly to speed IRA up (file ira-build.c).
153
154        * IRA propagates accumulated allocno info from lower region
155          allocnos to corresponding upper region allocnos (file
156          ira-build.c).
157
158        * IRA creates all caps (file ira-build.c).
159
160        * Having live-ranges of allocnos and their cover classes, IRA
161          creates conflicting allocnos of the same cover class for each
162          allocno.  Conflicting allocnos are stored as a bit vector or
163          array of pointers to the conflicting allocnos whatever is
164          more profitable (file ira-conflicts.c).  At this point IRA
165          creates allocno copies.
166
167      o Coloring.  Now IRA has all necessary info to start graph coloring
168        process.  It is done in each region on top-down traverse of the
169        region tree (file ira-color.c).  There are following subpasses:
170         
171        * Optional aggressive coalescing of allocnos in the region.
172
173        * Putting allocnos onto the coloring stack.  IRA uses Briggs
174          optimistic coloring which is a major improvement over
175          Chaitin's coloring.  Therefore IRA does not spill allocnos at
176          this point.  There is some freedom in the order of putting
177          allocnos on the stack which can affect the final result of
178          the allocation.  IRA uses some heuristics to improve the order.
179
180        * Popping the allocnos from the stack and assigning them hard
181          registers.  If IRA can not assign a hard register to an
182          allocno and the allocno is coalesced, IRA undoes the
183          coalescing and puts the uncoalesced allocnos onto the stack in
184          the hope that some such allocnos will get a hard register
185          separately.  If IRA fails to assign hard register or memory
186          is more profitable for it, IRA spills the allocno.  IRA
187          assigns the allocno the hard-register with minimal full
188          allocation cost which reflects the cost of usage of the
189          hard-register for the allocno and cost of usage of the
190          hard-register for allocnos conflicting with given allocno.
191
192        * After allono assigning in the region, IRA modifies the hard
193          register and memory costs for the corresponding allocnos in
194          the subregions to reflect the cost of possible loads, stores,
195          or moves on the border of the region and its subregions.
196          When default regional allocation algorithm is used
197          (-fira-algorithm=mixed), IRA just propagates the assignment
198          for allocnos if the register pressure in the region for the
199          corresponding cover class is less than number of available
200          hard registers for given cover class.
201
202      o Spill/restore code moving.  When IRA performs an allocation
203        by traversing regions in top-down order, it does not know what
204        happens below in the region tree.  Therefore, sometimes IRA
205        misses opportunities to perform a better allocation.  A simple
206        optimization tries to improve allocation in a region having
207        subregions and containing in another region.  If the
208        corresponding allocnos in the subregion are spilled, it spills
209        the region allocno if it is profitable.  The optimization
210        implements a simple iterative algorithm performing profitable
211        transformations while they are still possible.  It is fast in
212        practice, so there is no real need for a better time complexity
213        algorithm.
214
215      o Code change.  After coloring, two allocnos representing the same
216        pseudo-register outside and inside a region respectively may be
217        assigned to different locations (hard-registers or memory).  In
218        this case IRA creates and uses a new pseudo-register inside the
219        region and adds code to move allocno values on the region's
220        borders.  This is done during top-down traversal of the regions
221        (file ira-emit.c).  In some complicated cases IRA can create a
222        new allocno to move allocno values (e.g. when a swap of values
223        stored in two hard-registers is needed).  At this stage, the
224        new allocno is marked as spilled.  IRA still creates the
225        pseudo-register and the moves on the region borders even when
226        both allocnos were assigned to the same hard-register.  If the
227        reload pass spills a pseudo-register for some reason, the
228        effect will be smaller because another allocno will still be in
229        the hard-register.  In most cases, this is better then spilling
230        both allocnos.  If reload does not change the allocation
231        for the two pseudo-registers, the trivial move will be removed
232        by post-reload optimizations.  IRA does not generate moves for
233        allocnos assigned to the same hard register when the default
234        regional allocation algorithm is used and the register pressure
235        in the region for the corresponding allocno cover class is less
236        than number of available hard registers for given cover class.
237        IRA also does some optimizations to remove redundant stores and
238        to reduce code duplication on the region borders.
239
240      o Flattening internal representation.  After changing code, IRA
241        transforms its internal representation for several regions into
242        one region representation (file ira-build.c).  This process is
243        called IR flattening.  Such process is more complicated than IR
244        rebuilding would be, but is much faster.
245
246      o After IR flattening, IRA tries to assign hard registers to all
247        spilled allocnos.  This is impelemented by a simple and fast
248        priority coloring algorithm (see function
249        ira_reassign_conflict_allocnos::ira-color.c).  Here new allocnos
250        created during the code change pass can be assigned to hard
251        registers.
252
253      o At the end IRA calls the reload pass.  The reload pass
254        communicates with IRA through several functions in file
255        ira-color.c to improve its decisions in
256
257        * sharing stack slots for the spilled pseudos based on IRA info
258          about pseudo-register conflicts.
259
260        * reassigning hard-registers to all spilled pseudos at the end
261          of each reload iteration.
262
263        * choosing a better hard-register to spill based on IRA info
264          about pseudo-register live ranges and the register pressure
265          in places where the pseudo-register lives.
266
267    IRA uses a lot of data representing the target processors.  These
268    data are initilized in file ira.c.
269
270    If function has no loops (or the loops are ignored when
271    -fira-algorithm=CB is used), we have classic Chaitin-Briggs
272    coloring (only instead of separate pass of coalescing, we use hard
273    register preferencing).  In such case, IRA works much faster
274    because many things are not made (like IR flattening, the
275    spill/restore optimization, and the code change).
276
277    Literature is worth to read for better understanding the code:
278
279    o Preston Briggs, Keith D. Cooper, Linda Torczon.  Improvements to
280      Graph Coloring Register Allocation.
281
282    o David Callahan, Brian Koblenz.  Register allocation via
283      hierarchical graph coloring.
284
285    o Keith Cooper, Anshuman Dasgupta, Jason Eckhardt. Revisiting Graph
286      Coloring Register Allocation: A Study of the Chaitin-Briggs and
287      Callahan-Koblenz Algorithms.
288
289    o Guei-Yuan Lueh, Thomas Gross, and Ali-Reza Adl-Tabatabai. Global
290      Register Allocation Based on Graph Fusion.
291
292    o Vladimir Makarov. The Integrated Register Allocator for GCC.
293
294    o Vladimir Makarov.  The top-down register allocator for irregular
295      register file architectures.
296
297 */
298
299
300 #include "config.h"
301 #include "system.h"
302 #include "coretypes.h"
303 #include "tm.h"
304 #include "regs.h"
305 #include "rtl.h"
306 #include "tm_p.h"
307 #include "target.h"
308 #include "flags.h"
309 #include "obstack.h"
310 #include "bitmap.h"
311 #include "hard-reg-set.h"
312 #include "basic-block.h"
313 #include "expr.h"
314 #include "recog.h"
315 #include "params.h"
316 #include "timevar.h"
317 #include "tree-pass.h"
318 #include "output.h"
319 #include "reload.h"
320 #include "errors.h"
321 #include "integrate.h"
322 #include "df.h"
323 #include "ggc.h"
324 #include "ira-int.h"
325
326
327 /* A modified value of flag `-fira-verbose' used internally.  */
328 int internal_flag_ira_verbose;
329
330 /* Dump file of the allocator if it is not NULL.  */
331 FILE *ira_dump_file;
332
333 /* Pools for allocnos, copies, allocno live ranges.  */
334 alloc_pool allocno_pool, copy_pool, allocno_live_range_pool;
335
336 /* The number of elements in the following array.  */
337 int ira_spilled_reg_stack_slots_num;
338
339 /* The following array contains info about spilled pseudo-registers
340    stack slots used in current function so far.  */
341 struct ira_spilled_reg_stack_slot *ira_spilled_reg_stack_slots;
342
343 /* Correspondingly overall cost of the allocation, cost of the
344    allocnos assigned to hard-registers, cost of the allocnos assigned
345    to memory, cost of loads, stores and register move insns generated
346    for pseudo-register live range splitting (see ira-emit.c).  */
347 int ira_overall_cost;
348 int ira_reg_cost, ira_mem_cost;
349 int ira_load_cost, ira_store_cost, ira_shuffle_cost;
350 int ira_move_loops_num, ira_additional_jumps_num;
351
352 /* Map: hard regs X modes -> set of hard registers for storing value
353    of given mode starting with given hard register.  */
354 HARD_REG_SET ira_reg_mode_hard_regset[FIRST_PSEUDO_REGISTER][NUM_MACHINE_MODES];
355
356 /* The following two variables are array analogs of the macros
357    MEMORY_MOVE_COST and REGISTER_MOVE_COST.  */
358 short int ira_memory_move_cost[MAX_MACHINE_MODE][N_REG_CLASSES][2];
359 move_table *ira_register_move_cost[MAX_MACHINE_MODE];
360
361 /* Similar to may_move_in_cost but it is calculated in IRA instead of
362    regclass.  Another difference is that we take only available hard
363    registers into account to figure out that one register class is a
364    subset of the another one.  */
365 move_table *ira_may_move_in_cost[MAX_MACHINE_MODE];
366
367 /* Similar to may_move_out_cost but it is calculated in IRA instead of
368    regclass.  Another difference is that we take only available hard
369    registers into account to figure out that one register class is a
370    subset of the another one.  */
371 move_table *ira_may_move_out_cost[MAX_MACHINE_MODE];
372
373 /* Register class subset relation: TRUE if the first class is a subset
374    of the second one considering only hard registers available for the
375    allocation.  */
376 int ira_class_subset_p[N_REG_CLASSES][N_REG_CLASSES];
377
378 /* Temporary hard reg set used for a different calculation.  */
379 static HARD_REG_SET temp_hard_regset;
380
381 \f
382
383 /* The function sets up the map IRA_REG_MODE_HARD_REGSET.  */
384 static void
385 setup_reg_mode_hard_regset (void)
386 {
387   int i, m, hard_regno;
388
389   for (m = 0; m < NUM_MACHINE_MODES; m++)
390     for (hard_regno = 0; hard_regno < FIRST_PSEUDO_REGISTER; hard_regno++)
391       {
392         CLEAR_HARD_REG_SET (ira_reg_mode_hard_regset[hard_regno][m]);
393         for (i = hard_regno_nregs[hard_regno][m] - 1; i >= 0; i--)
394           if (hard_regno + i < FIRST_PSEUDO_REGISTER)
395             SET_HARD_REG_BIT (ira_reg_mode_hard_regset[hard_regno][m],
396                               hard_regno + i);
397       }
398 }
399
400 \f
401
402 /* Hard registers that can not be used for the register allocator for
403    all functions of the current compilation unit.  */
404 static HARD_REG_SET no_unit_alloc_regs;
405
406 /* Array of the number of hard registers of given class which are
407    available for allocation.  The order is defined by the
408    allocation order.  */
409 short ira_class_hard_regs[N_REG_CLASSES][FIRST_PSEUDO_REGISTER];
410
411 /* The number of elements of the above array for given register
412    class.  */
413 int ira_class_hard_regs_num[N_REG_CLASSES];
414
415 /* Index (in ira_class_hard_regs) for given register class and hard
416    register (in general case a hard register can belong to several
417    register classes).  The index is negative for hard registers
418    unavailable for the allocation. */
419 short ira_class_hard_reg_index[N_REG_CLASSES][FIRST_PSEUDO_REGISTER];
420
421 /* The function sets up the three arrays declared above.  */
422 static void
423 setup_class_hard_regs (void)
424 {
425   int cl, i, hard_regno, n;
426   HARD_REG_SET processed_hard_reg_set;
427
428   ira_assert (SHRT_MAX >= FIRST_PSEUDO_REGISTER);
429   /* We could call ORDER_REGS_FOR_LOCAL_ALLOC here (it is usually
430      putting hard callee-used hard registers first).  But our
431      heuristics work better.  */
432   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
433     {
434       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
435       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
436       CLEAR_HARD_REG_SET (processed_hard_reg_set);
437       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
438         ira_class_hard_reg_index[cl][0] = -1;
439       for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
440         {
441 #ifdef REG_ALLOC_ORDER
442           hard_regno = reg_alloc_order[i];
443 #else
444           hard_regno = i;
445 #endif    
446           if (TEST_HARD_REG_BIT (processed_hard_reg_set, hard_regno))
447             continue;
448           SET_HARD_REG_BIT (processed_hard_reg_set, hard_regno);
449           if (! TEST_HARD_REG_BIT (temp_hard_regset, hard_regno))
450             ira_class_hard_reg_index[cl][hard_regno] = -1;
451           else
452             {
453               ira_class_hard_reg_index[cl][hard_regno] = n;
454               ira_class_hard_regs[cl][n++] = hard_regno;
455             }
456         }
457       ira_class_hard_regs_num[cl] = n;
458     }
459 }
460
461 /* Number of given class hard registers available for the register
462    allocation for given classes.  */
463 int ira_available_class_regs[N_REG_CLASSES];
464
465 /* Set up IRA_AVAILABLE_CLASS_REGS.  */
466 static void
467 setup_available_class_regs (void)
468 {
469   int i, j;
470
471   memset (ira_available_class_regs, 0, sizeof (ira_available_class_regs));
472   for (i = 0; i < N_REG_CLASSES; i++)
473     {
474       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
475       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
476       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
477         if (TEST_HARD_REG_BIT (temp_hard_regset, j))
478           ira_available_class_regs[i]++;
479     }
480 }
481
482 /* Set up global variables defining info about hard registers for the
483    allocation.  These depend on USE_HARD_FRAME_P whose TRUE value means
484    that we can use the hard frame pointer for the allocation.  */
485 static void
486 setup_alloc_regs (bool use_hard_frame_p)
487 {
488   COPY_HARD_REG_SET (no_unit_alloc_regs, fixed_reg_set);
489   if (! use_hard_frame_p)
490     SET_HARD_REG_BIT (no_unit_alloc_regs, HARD_FRAME_POINTER_REGNUM);
491   setup_class_hard_regs ();
492   setup_available_class_regs ();
493 }
494
495 \f
496
497 /* Set up IRA_MEMORY_MOVE_COST, IRA_REGISTER_MOVE_COST.  */
498 static void
499 setup_class_subset_and_memory_move_costs (void)
500 {
501   int cl, cl2;
502   enum machine_mode mode;
503   HARD_REG_SET temp_hard_regset2;
504
505   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
506     ira_memory_move_cost[mode][NO_REGS][0]
507       = ira_memory_move_cost[mode][NO_REGS][1] = SHRT_MAX;
508   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
509     {
510       if (cl != (int) NO_REGS)
511         for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
512           {
513             ira_memory_move_cost[mode][cl][0] = MEMORY_MOVE_COST (mode, cl, 0);
514             ira_memory_move_cost[mode][cl][1] = MEMORY_MOVE_COST (mode, cl, 1);
515             /* Costs for NO_REGS are used in cost calculation on the
516                1st pass when the preferred register classes are not
517                known yet.  In this case we take the best scenario.  */
518             if (ira_memory_move_cost[mode][NO_REGS][0]
519                 > ira_memory_move_cost[mode][cl][0])
520               ira_memory_move_cost[mode][NO_REGS][0]
521                 = ira_memory_move_cost[mode][cl][0];
522             if (ira_memory_move_cost[mode][NO_REGS][1]
523                 > ira_memory_move_cost[mode][cl][1])
524               ira_memory_move_cost[mode][NO_REGS][1]
525                 = ira_memory_move_cost[mode][cl][1];
526           }
527       for (cl2 = (int) N_REG_CLASSES - 1; cl2 >= 0; cl2--)
528         {
529           COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
530           AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
531           COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl2]);
532           AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
533           ira_class_subset_p[cl][cl2]
534             = hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2);
535         }
536     }
537 }
538
539 \f
540
541 /* Define the following macro if allocation through malloc if
542    preferable.  */
543 #define IRA_NO_OBSTACK
544
545 #ifndef IRA_NO_OBSTACK
546 /* Obstack used for storing all dynamic data (except bitmaps) of the
547    IRA.  */
548 static struct obstack ira_obstack;
549 #endif
550
551 /* Obstack used for storing all bitmaps of the IRA.  */
552 static struct bitmap_obstack ira_bitmap_obstack;
553
554 /* Allocate memory of size LEN for IRA data.  */
555 void *
556 ira_allocate (size_t len)
557 {
558   void *res;
559
560 #ifndef IRA_NO_OBSTACK
561   res = obstack_alloc (&ira_obstack, len);
562 #else
563   res = xmalloc (len);
564 #endif
565   return res;
566 }
567
568 /* Reallocate memory PTR of size LEN for IRA data.  */
569 void *
570 ira_reallocate (void *ptr, size_t len)
571 {
572   void *res;
573
574 #ifndef IRA_NO_OBSTACK
575   res = obstack_alloc (&ira_obstack, len);
576 #else
577   res = xrealloc (ptr, len);
578 #endif
579   return res;
580 }
581
582 /* Free memory ADDR allocated for IRA data.  */
583 void
584 ira_free (void *addr ATTRIBUTE_UNUSED)
585 {
586 #ifndef IRA_NO_OBSTACK
587   /* do nothing */
588 #else
589   free (addr);
590 #endif
591 }
592
593
594 /* Allocate and returns bitmap for IRA.  */
595 bitmap
596 ira_allocate_bitmap (void)
597 {
598   return BITMAP_ALLOC (&ira_bitmap_obstack);
599 }
600
601 /* Free bitmap B allocated for IRA.  */
602 void
603 ira_free_bitmap (bitmap b ATTRIBUTE_UNUSED)
604 {
605   /* do nothing */
606 }
607
608 \f
609
610 /* Output information about allocation of all allocnos (except for
611    caps) into file F.  */
612 void
613 ira_print_disposition (FILE *f)
614 {
615   int i, n, max_regno;
616   ira_allocno_t a;
617   basic_block bb;
618
619   fprintf (f, "Disposition:");
620   max_regno = max_reg_num ();
621   for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
622     for (a = ira_regno_allocno_map[i];
623          a != NULL;
624          a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
625       {
626         if (n % 4 == 0)
627           fprintf (f, "\n");
628         n++;
629         fprintf (f, " %4d:r%-4d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
630         if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
631           fprintf (f, "b%-3d", bb->index);
632         else
633           fprintf (f, "l%-3d", ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
634         if (ALLOCNO_HARD_REGNO (a) >= 0)
635           fprintf (f, " %3d", ALLOCNO_HARD_REGNO (a));
636         else
637           fprintf (f, " mem");
638       }
639   fprintf (f, "\n");
640 }
641
642 /* Outputs information about allocation of all allocnos into
643    stderr.  */
644 void
645 ira_debug_disposition (void)
646 {
647   ira_print_disposition (stderr);
648 }
649
650 \f
651
652 /* For each reg class, table listing all the classes contained in it
653    (excluding the class itself.  Non-allocatable registers are
654    excluded from the consideration).  */
655 static enum reg_class alloc_reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
656
657 /* Initialize the table of subclasses of each reg class.  */
658 static void
659 setup_reg_subclasses (void)
660 {
661   int i, j;
662   HARD_REG_SET temp_hard_regset2;
663
664   for (i = 0; i < N_REG_CLASSES; i++)
665     for (j = 0; j < N_REG_CLASSES; j++)
666       alloc_reg_class_subclasses[i][j] = LIM_REG_CLASSES;
667
668   for (i = 0; i < N_REG_CLASSES; i++)
669     {
670       if (i == (int) NO_REGS)
671         continue;
672
673       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
674       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
675       if (hard_reg_set_empty_p (temp_hard_regset))
676         continue;
677       for (j = 0; j < N_REG_CLASSES; j++)
678         if (i != j)
679           {
680             enum reg_class *p;
681
682             COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[j]);
683             AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
684             if (! hard_reg_set_subset_p (temp_hard_regset,
685                                          temp_hard_regset2))
686               continue;
687             p = &alloc_reg_class_subclasses[j][0];
688             while (*p != LIM_REG_CLASSES) p++;
689             *p = (enum reg_class) i;
690           }
691     }
692 }
693
694 \f
695
696 /* Number of cover classes.  Cover classes is non-intersected register
697    classes containing all hard-registers available for the
698    allocation.  */
699 int ira_reg_class_cover_size;
700
701 /* The array containing cover classes (see also comments for macro
702    IRA_COVER_CLASSES).  Only first IRA_REG_CLASS_COVER_SIZE elements are
703    used for this.  */
704 enum reg_class ira_reg_class_cover[N_REG_CLASSES];
705
706 /* The number of elements in the subsequent array.  */
707 int ira_important_classes_num;
708
709 /* The array containing non-empty classes (including non-empty cover
710    classes) which are subclasses of cover classes.  Such classes is
711    important for calculation of the hard register usage costs.  */
712 enum reg_class ira_important_classes[N_REG_CLASSES];
713
714 /* The array containing indexes of important classes in the previous
715    array.  The array elements are defined only for important
716    classes.  */
717 int ira_important_class_nums[N_REG_CLASSES];
718
719 /* Set the four global variables defined above.  */
720 static void
721 setup_cover_and_important_classes (void)
722 {
723   int i, j, n;
724   bool set_p, eq_p;
725   enum reg_class cl;
726   const enum reg_class *cover_classes;
727   HARD_REG_SET temp_hard_regset2;
728   static enum reg_class classes[LIM_REG_CLASSES + 1];
729
730   if (targetm.ira_cover_classes == NULL)
731     cover_classes = NULL;
732   else
733     cover_classes = targetm.ira_cover_classes ();
734   if (cover_classes == NULL)
735     ira_assert (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY);
736   else
737     {
738       for (i = 0; (cl = cover_classes[i]) != LIM_REG_CLASSES; i++)
739         classes[i] = cl;
740       classes[i] = LIM_REG_CLASSES;
741     }
742
743   if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
744     {
745       n = 0;
746       for (i = 0; i <= LIM_REG_CLASSES; i++)
747         {
748           if (i == NO_REGS)
749             continue;
750 #ifdef CONSTRAINT__LIMIT
751           for (j = 0; j < CONSTRAINT__LIMIT; j++)
752             if ((int) regclass_for_constraint (j) == i)
753               break;
754           if (j < CONSTRAINT__LIMIT)
755             {
756               classes[n++] = i;
757               continue;
758             }
759 #endif
760           COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
761           AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
762           for (j = 0; j < LIM_REG_CLASSES; j++)
763             {
764               if (i == j)
765                 continue;
766               COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[j]);
767               AND_COMPL_HARD_REG_SET (temp_hard_regset2,
768                                       no_unit_alloc_regs);
769               if (hard_reg_set_equal_p (temp_hard_regset,
770                                         temp_hard_regset2))
771                     break;
772             }
773           if (j >= i)
774             classes[n++] = i;
775         }
776       classes[n] = LIM_REG_CLASSES;
777     }
778
779   ira_reg_class_cover_size = 0;
780   for (i = 0; (cl = classes[i]) != LIM_REG_CLASSES; i++)
781     {
782       for (j = 0; j < i; j++)
783         if (flag_ira_algorithm != IRA_ALGORITHM_PRIORITY
784             && reg_classes_intersect_p (cl, classes[j]))
785           gcc_unreachable ();
786       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
787       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
788       if (! hard_reg_set_empty_p (temp_hard_regset))
789         ira_reg_class_cover[ira_reg_class_cover_size++] = cl;
790     }
791   ira_important_classes_num = 0;
792   for (cl = 0; cl < N_REG_CLASSES; cl++)
793     {
794       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
795       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
796       if (! hard_reg_set_empty_p (temp_hard_regset))
797         {
798           set_p = eq_p = false;
799           for (j = 0; j < ira_reg_class_cover_size; j++)
800             {
801               COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
802               AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
803               COPY_HARD_REG_SET (temp_hard_regset2,
804                                  reg_class_contents[ira_reg_class_cover[j]]);
805               AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
806               if (cl == ira_reg_class_cover[j])
807                 {
808                   eq_p = false;
809                   set_p = true;
810                   break;
811                 }
812               else if (hard_reg_set_equal_p (temp_hard_regset,
813                                              temp_hard_regset2))
814                 eq_p = true;
815               else if (hard_reg_set_subset_p (temp_hard_regset,
816                                               temp_hard_regset2))
817                 set_p = true;
818             }
819           if (set_p && ! eq_p)
820             {
821               ira_important_class_nums[cl] = ira_important_classes_num;
822               ira_important_classes[ira_important_classes_num++] = cl;
823             }
824         }
825     }
826 }
827
828 /* Map of all register classes to corresponding cover class containing
829    the given class.  If given class is not a subset of a cover class,
830    we translate it into the cheapest cover class.  */
831 enum reg_class ira_class_translate[N_REG_CLASSES];
832
833 /* Set up array IRA_CLASS_TRANSLATE.  */
834 static void
835 setup_class_translate (void)
836 {
837   enum reg_class cl, cover_class, best_class, *cl_ptr;
838   enum machine_mode mode;
839   int i, cost, min_cost, best_cost;
840
841   for (cl = 0; cl < N_REG_CLASSES; cl++)
842     ira_class_translate[cl] = NO_REGS;
843   
844   if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
845     for (cl = 0; cl < LIM_REG_CLASSES; cl++)
846       {
847         COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
848         AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
849         for (i = 0; i < ira_reg_class_cover_size; i++)
850           {
851             HARD_REG_SET temp_hard_regset2;
852             
853             cover_class = ira_reg_class_cover[i];
854             COPY_HARD_REG_SET (temp_hard_regset2,
855                                reg_class_contents[cover_class]);
856             AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
857             if (hard_reg_set_equal_p (temp_hard_regset, temp_hard_regset2))
858               ira_class_translate[cl] = cover_class;
859           }
860       }
861   for (i = 0; i < ira_reg_class_cover_size; i++)
862     {
863       cover_class = ira_reg_class_cover[i];
864       if (flag_ira_algorithm != IRA_ALGORITHM_PRIORITY)
865         for (cl_ptr = &alloc_reg_class_subclasses[cover_class][0];
866              (cl = *cl_ptr) != LIM_REG_CLASSES;
867              cl_ptr++)
868           {
869             if (ira_class_translate[cl] == NO_REGS)
870               ira_class_translate[cl] = cover_class;
871 #ifdef ENABLE_IRA_CHECKING
872             else
873               {
874                 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
875                 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
876                 if (! hard_reg_set_empty_p (temp_hard_regset))
877                   gcc_unreachable ();
878               }
879 #endif
880           }
881       ira_class_translate[cover_class] = cover_class;
882     }
883   /* For classes which are not fully covered by a cover class (in
884      other words covered by more one cover class), use the cheapest
885      cover class.  */
886   for (cl = 0; cl < N_REG_CLASSES; cl++)
887     {
888       if (cl == NO_REGS || ira_class_translate[cl] != NO_REGS)
889         continue;
890       best_class = NO_REGS;
891       best_cost = INT_MAX;
892       for (i = 0; i < ira_reg_class_cover_size; i++)
893         {
894           cover_class = ira_reg_class_cover[i];
895           COPY_HARD_REG_SET (temp_hard_regset,
896                              reg_class_contents[cover_class]);
897           AND_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
898           AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
899           if (! hard_reg_set_empty_p (temp_hard_regset))
900             {
901               min_cost = INT_MAX;
902               for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
903                 {
904                   cost = (ira_memory_move_cost[mode][cl][0]
905                           + ira_memory_move_cost[mode][cl][1]);
906                   if (min_cost > cost)
907                     min_cost = cost;
908                 }
909               if (best_class == NO_REGS || best_cost > min_cost)
910                 {
911                   best_class = cover_class;
912                   best_cost = min_cost;
913                 }
914             }
915         }
916       ira_class_translate[cl] = best_class;
917     }
918 }
919
920 /* The biggest important reg_class inside of intersection of the two
921    reg_classes (that is calculated taking only hard registers
922    available for allocation into account).  If the both reg_classes
923    contain no hard registers available for allocation, the value is
924    calculated by taking all hard-registers including fixed ones into
925    account.  */
926 enum reg_class ira_reg_class_intersect[N_REG_CLASSES][N_REG_CLASSES];
927
928 /* True if the two classes (that is calculated taking only hard
929    registers available for allocation into account) are
930    intersected.  */
931 bool ira_reg_classes_intersect_p[N_REG_CLASSES][N_REG_CLASSES];
932
933 /* Important classes with end marker LIM_REG_CLASSES which are
934    supersets with given important class (the first index).  That
935    includes given class itself.  This is calculated taking only hard
936    registers available for allocation into account.  */
937 enum reg_class ira_reg_class_super_classes[N_REG_CLASSES][N_REG_CLASSES];
938
939 /* The biggest important reg_class inside of union of the two
940    reg_classes (that is calculated taking only hard registers
941    available for allocation into account).  If the both reg_classes
942    contain no hard registers available for allocation, the value is
943    calculated by taking all hard-registers including fixed ones into
944    account.  In other words, the value is the corresponding
945    reg_class_subunion value.  */
946 enum reg_class ira_reg_class_union[N_REG_CLASSES][N_REG_CLASSES];
947
948 /* Set up the above reg class relations.  */
949 static void
950 setup_reg_class_relations (void)
951 {
952   int i, cl1, cl2, cl3;
953   HARD_REG_SET intersection_set, union_set, temp_set2;
954   bool important_class_p[N_REG_CLASSES];
955
956   memset (important_class_p, 0, sizeof (important_class_p));
957   for (i = 0; i < ira_important_classes_num; i++)
958     important_class_p[ira_important_classes[i]] = true;
959   for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
960     {
961       ira_reg_class_super_classes[cl1][0] = LIM_REG_CLASSES;
962       for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
963         {
964           ira_reg_classes_intersect_p[cl1][cl2] = false;
965           ira_reg_class_intersect[cl1][cl2] = NO_REGS;
966           COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl1]);
967           AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
968           COPY_HARD_REG_SET (temp_set2, reg_class_contents[cl2]);
969           AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
970           if (hard_reg_set_empty_p (temp_hard_regset)
971               && hard_reg_set_empty_p (temp_set2))
972             {
973               for (i = 0;; i++)
974                 {
975                   cl3 = reg_class_subclasses[cl1][i];
976                   if (cl3 == LIM_REG_CLASSES)
977                     break;
978                   if (reg_class_subset_p (ira_reg_class_intersect[cl1][cl2],
979                                           cl3))
980                     ira_reg_class_intersect[cl1][cl2] = cl3;
981                 }
982               ira_reg_class_union[cl1][cl2] = reg_class_subunion[cl1][cl2];
983               continue;
984             }
985           ira_reg_classes_intersect_p[cl1][cl2]
986             = hard_reg_set_intersect_p (temp_hard_regset, temp_set2);
987           if (important_class_p[cl1] && important_class_p[cl2]
988               && hard_reg_set_subset_p (temp_hard_regset, temp_set2))
989             {
990               enum reg_class *p;
991
992               p = &ira_reg_class_super_classes[cl1][0];
993               while (*p != LIM_REG_CLASSES)
994                 p++;
995               *p++ = (enum reg_class) cl2;
996               *p = LIM_REG_CLASSES;
997             }
998           ira_reg_class_union[cl1][cl2] = NO_REGS;
999           COPY_HARD_REG_SET (intersection_set, reg_class_contents[cl1]);
1000           AND_HARD_REG_SET (intersection_set, reg_class_contents[cl2]);
1001           AND_COMPL_HARD_REG_SET (intersection_set, no_unit_alloc_regs);
1002           COPY_HARD_REG_SET (union_set, reg_class_contents[cl1]);
1003           IOR_HARD_REG_SET (union_set, reg_class_contents[cl2]);
1004           AND_COMPL_HARD_REG_SET (union_set, no_unit_alloc_regs);
1005           for (i = 0; i < ira_important_classes_num; i++)
1006             {
1007               cl3 = ira_important_classes[i];
1008               COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl3]);
1009               AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1010               if (hard_reg_set_subset_p (temp_hard_regset, intersection_set))
1011                 {
1012                   COPY_HARD_REG_SET
1013                     (temp_set2,
1014                      reg_class_contents[(int)
1015                                         ira_reg_class_intersect[cl1][cl2]]);
1016                   AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1017                   if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1018                       /* Ignore unavailable hard registers and prefer
1019                          smallest class for debugging purposes.  */
1020                       || (hard_reg_set_equal_p (temp_hard_regset, temp_set2)
1021                           && hard_reg_set_subset_p
1022                              (reg_class_contents[cl3],
1023                               reg_class_contents
1024                               [(int) ira_reg_class_intersect[cl1][cl2]])))
1025                     ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1026                 }
1027               if (hard_reg_set_subset_p (temp_hard_regset, union_set))
1028                 {
1029                   COPY_HARD_REG_SET
1030                     (temp_set2,
1031                      reg_class_contents[(int) ira_reg_class_union[cl1][cl2]]);
1032                   AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1033                   if (ira_reg_class_union[cl1][cl2] == NO_REGS
1034                       || (hard_reg_set_subset_p (temp_set2, temp_hard_regset)
1035                       
1036                           && (! hard_reg_set_equal_p (temp_set2,
1037                                                       temp_hard_regset)
1038                               /* Ignore unavailable hard registers and
1039                                  prefer smallest class for debugging
1040                                  purposes.  */
1041                               || hard_reg_set_subset_p
1042                                  (reg_class_contents[cl3],
1043                                   reg_class_contents
1044                                   [(int) ira_reg_class_union[cl1][cl2]]))))
1045                     ira_reg_class_union[cl1][cl2] = (enum reg_class) cl3;
1046                 }
1047             }
1048         }
1049     }
1050 }
1051
1052 /* Output all cover classes and the translation map into file F.  */
1053 static void
1054 print_class_cover (FILE *f)
1055 {
1056   static const char *const reg_class_names[] = REG_CLASS_NAMES;
1057   int i;
1058
1059   fprintf (f, "Class cover:\n");
1060   for (i = 0; i < ira_reg_class_cover_size; i++)
1061     fprintf (f, " %s", reg_class_names[ira_reg_class_cover[i]]);
1062   fprintf (f, "\nClass translation:\n");
1063   for (i = 0; i < N_REG_CLASSES; i++)
1064     fprintf (f, " %s -> %s\n", reg_class_names[i],
1065              reg_class_names[ira_class_translate[i]]);
1066 }
1067
1068 /* Output all cover classes and the translation map into
1069    stderr.  */
1070 void
1071 ira_debug_class_cover (void)
1072 {
1073   print_class_cover (stderr);
1074 }
1075
1076 /* Set up different arrays concerning class subsets, cover and
1077    important classes.  */
1078 static void
1079 find_reg_class_closure (void)
1080 {
1081   setup_reg_subclasses ();
1082   setup_cover_and_important_classes ();
1083   setup_class_translate ();
1084   setup_reg_class_relations ();
1085 }
1086
1087 \f
1088
1089 /* Map: hard register number -> cover class it belongs to.  If the
1090    corresponding class is NO_REGS, the hard register is not available
1091    for allocation.  */
1092 enum reg_class ira_hard_regno_cover_class[FIRST_PSEUDO_REGISTER];
1093
1094 /* Set up the array above.  */
1095 static void
1096 setup_hard_regno_cover_class (void)
1097 {
1098   int i, j;
1099   enum reg_class cl;
1100
1101   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1102     {
1103       ira_hard_regno_cover_class[i] = NO_REGS;
1104       for (j = 0; j < ira_reg_class_cover_size; j++)
1105         {
1106           cl = ira_reg_class_cover[j];
1107           if (ira_class_hard_reg_index[cl][i] >= 0)
1108             {
1109               ira_hard_regno_cover_class[i] = cl;
1110               break;
1111             }
1112         }
1113             
1114     }
1115 }
1116
1117 \f
1118
1119 /* Map: register class x machine mode -> number of hard registers of
1120    given class needed to store value of given mode.  If the number is
1121    different, the size will be negative.  */
1122 int ira_reg_class_nregs[N_REG_CLASSES][MAX_MACHINE_MODE];
1123
1124 /* Maximal value of the previous array elements.  */
1125 int ira_max_nregs;
1126
1127 /* Form IRA_REG_CLASS_NREGS map.  */
1128 static void
1129 setup_reg_class_nregs (void)
1130 {
1131   int m;
1132   enum reg_class cl;
1133
1134   ira_max_nregs = -1;
1135   for (cl = 0; cl < N_REG_CLASSES; cl++)
1136     for (m = 0; m < MAX_MACHINE_MODE; m++)
1137       {
1138         ira_reg_class_nregs[cl][m] = CLASS_MAX_NREGS (cl, m);
1139         if (ira_max_nregs < ira_reg_class_nregs[cl][m])
1140           ira_max_nregs = ira_reg_class_nregs[cl][m];
1141       }
1142 }
1143
1144 \f
1145
1146 /* Array whose values are hard regset of hard registers available for
1147    the allocation of given register class whose HARD_REGNO_MODE_OK
1148    values for given mode are zero.  */
1149 HARD_REG_SET prohibited_class_mode_regs[N_REG_CLASSES][NUM_MACHINE_MODES];
1150
1151 /* Set up PROHIBITED_CLASS_MODE_REGS.  */
1152 static void
1153 setup_prohibited_class_mode_regs (void)
1154 {
1155   int i, j, k, hard_regno;
1156   enum reg_class cl;
1157
1158   for (i = 0; i < ira_reg_class_cover_size; i++)
1159     {
1160       cl = ira_reg_class_cover[i];
1161       for (j = 0; j < NUM_MACHINE_MODES; j++)
1162         {
1163           CLEAR_HARD_REG_SET (prohibited_class_mode_regs[cl][j]);
1164           for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1165             {
1166               hard_regno = ira_class_hard_regs[cl][k];
1167               if (! HARD_REGNO_MODE_OK (hard_regno, j))
1168                 SET_HARD_REG_BIT (prohibited_class_mode_regs[cl][j],
1169                                   hard_regno);
1170             }
1171         }
1172     }
1173 }
1174
1175 \f
1176
1177 /* Allocate and initialize IRA_REGISTER_MOVE_COST,
1178    IRA_MAY_MOVE_IN_COST, and IRA_MAY_MOVE_OUT_COST for MODE if it is
1179    not done yet.  */
1180 void
1181 ira_init_register_move_cost (enum machine_mode mode)
1182 {
1183   int cl1, cl2;
1184
1185   ira_assert (ira_register_move_cost[mode] == NULL
1186               && ira_may_move_in_cost[mode] == NULL
1187               && ira_may_move_out_cost[mode] == NULL);
1188   if (move_cost[mode] == NULL)
1189     init_move_cost (mode);
1190   ira_register_move_cost[mode] = move_cost[mode];
1191   /* Don't use ira_allocate because the tables exist out of scope of a
1192      IRA call.  */
1193   ira_may_move_in_cost[mode]
1194     = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1195   memcpy (ira_may_move_in_cost[mode], may_move_in_cost[mode],
1196           sizeof (move_table) * N_REG_CLASSES);
1197   ira_may_move_out_cost[mode]
1198     = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1199   memcpy (ira_may_move_out_cost[mode], may_move_out_cost[mode],
1200           sizeof (move_table) * N_REG_CLASSES);
1201   for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1202     {
1203       for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1204         {
1205           if (ira_class_subset_p[cl1][cl2])
1206             ira_may_move_in_cost[mode][cl1][cl2] = 0;
1207           if (ira_class_subset_p[cl2][cl1])
1208             ira_may_move_out_cost[mode][cl1][cl2] = 0;
1209         }
1210     }
1211 }
1212
1213 \f
1214
1215 /* This is called once during compiler work.  It sets up
1216    different arrays whose values don't depend on the compiled
1217    function.  */
1218 void
1219 ira_init_once (void)
1220 {
1221   enum machine_mode mode;
1222
1223   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1224     {
1225       ira_register_move_cost[mode] = NULL;
1226       ira_may_move_in_cost[mode] = NULL;
1227       ira_may_move_out_cost[mode] = NULL;
1228     }
1229   ira_init_costs_once ();
1230 }
1231
1232 /* Free ira_register_move_cost, ira_may_move_in_cost, and
1233    ira_may_move_out_cost for each mode.  */
1234 static void
1235 free_register_move_costs (void)
1236 {
1237   enum machine_mode mode;
1238
1239   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1240     {
1241       if (ira_may_move_in_cost[mode] != NULL)
1242         free (ira_may_move_in_cost[mode]);
1243       if (ira_may_move_out_cost[mode] != NULL)
1244         free (ira_may_move_out_cost[mode]);
1245       ira_register_move_cost[mode] = NULL;
1246       ira_may_move_in_cost[mode] = NULL;
1247       ira_may_move_out_cost[mode] = NULL;
1248     }
1249 }
1250
1251 /* This is called every time when register related information is
1252    changed.  */
1253 void
1254 ira_init (void)
1255 {
1256   free_register_move_costs ();
1257   setup_reg_mode_hard_regset ();
1258   setup_alloc_regs (flag_omit_frame_pointer != 0);
1259   setup_class_subset_and_memory_move_costs ();
1260   find_reg_class_closure ();
1261   setup_hard_regno_cover_class ();
1262   setup_reg_class_nregs ();
1263   setup_prohibited_class_mode_regs ();
1264   ira_init_costs ();
1265 }
1266
1267 /* Function called once at the end of compiler work.  */
1268 void
1269 ira_finish_once (void)
1270 {
1271   ira_finish_costs_once ();
1272   free_register_move_costs ();
1273 }
1274
1275 \f
1276
1277 /* Array whose values are hard regset of hard registers for which
1278    move of the hard register in given mode into itself is
1279    prohibited.  */
1280 HARD_REG_SET ira_prohibited_mode_move_regs[NUM_MACHINE_MODES];
1281
1282 /* Flag of that the above array has been initialized.  */
1283 static bool ira_prohibited_mode_move_regs_initialized_p = false;
1284
1285 /* Set up IRA_PROHIBITED_MODE_MOVE_REGS.  */
1286 static void
1287 setup_prohibited_mode_move_regs (void)
1288 {
1289   int i, j;
1290   rtx test_reg1, test_reg2, move_pat, move_insn;
1291
1292   if (ira_prohibited_mode_move_regs_initialized_p)
1293     return;
1294   ira_prohibited_mode_move_regs_initialized_p = true;
1295   test_reg1 = gen_rtx_REG (VOIDmode, 0);
1296   test_reg2 = gen_rtx_REG (VOIDmode, 0);
1297   move_pat = gen_rtx_SET (VOIDmode, test_reg1, test_reg2);
1298   move_insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, 0, move_pat, -1, 0);
1299   for (i = 0; i < NUM_MACHINE_MODES; i++)
1300     {
1301       SET_HARD_REG_SET (ira_prohibited_mode_move_regs[i]);
1302       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1303         {
1304           if (! HARD_REGNO_MODE_OK (j, i))
1305             continue;
1306           SET_REGNO (test_reg1, j);
1307           PUT_MODE (test_reg1, i);
1308           SET_REGNO (test_reg2, j);
1309           PUT_MODE (test_reg2, i);
1310           INSN_CODE (move_insn) = -1;
1311           recog_memoized (move_insn);
1312           if (INSN_CODE (move_insn) < 0)
1313             continue;
1314           extract_insn (move_insn);
1315           if (! constrain_operands (1))
1316             continue;
1317           CLEAR_HARD_REG_BIT (ira_prohibited_mode_move_regs[i], j);
1318         }
1319     }
1320 }
1321
1322 \f
1323
1324 /* Function specific hard registers that can not be used for the
1325    register allocation.  */
1326 HARD_REG_SET ira_no_alloc_regs;
1327
1328 /* Return TRUE if *LOC contains an asm.  */
1329 static int
1330 insn_contains_asm_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
1331 {
1332   if ( !*loc)
1333     return FALSE;
1334   if (GET_CODE (*loc) == ASM_OPERANDS)
1335     return TRUE;
1336   return FALSE;
1337 }
1338
1339
1340 /* Return TRUE if INSN contains an ASM.  */
1341 static bool
1342 insn_contains_asm (rtx insn)
1343 {
1344   return for_each_rtx (&insn, insn_contains_asm_1, NULL);
1345 }
1346
1347 /* Set up regs_asm_clobbered.  */
1348 static void
1349 compute_regs_asm_clobbered (char *regs_asm_clobbered)
1350 {
1351   basic_block bb;
1352
1353   memset (regs_asm_clobbered, 0, sizeof (char) * FIRST_PSEUDO_REGISTER);
1354   
1355   FOR_EACH_BB (bb)
1356     {
1357       rtx insn;
1358       FOR_BB_INSNS_REVERSE (bb, insn)
1359         {
1360           df_ref *def_rec;
1361
1362           if (insn_contains_asm (insn))
1363             for (def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++)
1364               {
1365                 df_ref def = *def_rec;
1366                 unsigned int dregno = DF_REF_REGNO (def);
1367                 if (dregno < FIRST_PSEUDO_REGISTER)
1368                   {
1369                     unsigned int i;
1370                     enum machine_mode mode = GET_MODE (DF_REF_REAL_REG (def));
1371                     unsigned int end = dregno 
1372                       + hard_regno_nregs[dregno][mode] - 1;
1373
1374                     for (i = dregno; i <= end; ++i)
1375                       regs_asm_clobbered[i] = 1;
1376                   }
1377               }
1378         }
1379     }
1380 }
1381
1382
1383 /* Set up ELIMINABLE_REGSET, IRA_NO_ALLOC_REGS, and REGS_EVER_LIVE.  */
1384 static void
1385 setup_eliminable_regset (void)
1386 {
1387   /* Like regs_ever_live, but 1 if a reg is set or clobbered from an
1388      asm.  Unlike regs_ever_live, elements of this array corresponding
1389      to eliminable regs (like the frame pointer) are set if an asm
1390      sets them.  */
1391   char *regs_asm_clobbered
1392     = (char *) alloca (FIRST_PSEUDO_REGISTER * sizeof (char));
1393 #ifdef ELIMINABLE_REGS
1394   int i;
1395   static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
1396 #endif
1397   /* FIXME: If EXIT_IGNORE_STACK is set, we will not save and restore
1398      sp for alloca.  So we can't eliminate the frame pointer in that
1399      case.  At some point, we should improve this by emitting the
1400      sp-adjusting insns for this case.  */
1401   int need_fp
1402     = (! flag_omit_frame_pointer
1403        || (cfun->calls_alloca && EXIT_IGNORE_STACK)
1404        || crtl->accesses_prior_frames
1405        || crtl->stack_realign_needed
1406        || FRAME_POINTER_REQUIRED);
1407
1408   frame_pointer_needed = need_fp;
1409
1410   COPY_HARD_REG_SET (ira_no_alloc_regs, no_unit_alloc_regs);
1411   CLEAR_HARD_REG_SET (eliminable_regset);
1412
1413   compute_regs_asm_clobbered (regs_asm_clobbered);
1414   /* Build the regset of all eliminable registers and show we can't
1415      use those that we already know won't be eliminated.  */
1416 #ifdef ELIMINABLE_REGS
1417   for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
1418     {
1419       bool cannot_elim
1420         = (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
1421            || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp));
1422
1423       if (! regs_asm_clobbered[eliminables[i].from])
1424         {
1425             SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
1426
1427             if (cannot_elim)
1428               SET_HARD_REG_BIT (ira_no_alloc_regs, eliminables[i].from);
1429         }
1430       else if (cannot_elim)
1431         error ("%s cannot be used in asm here",
1432                reg_names[eliminables[i].from]);
1433       else
1434         df_set_regs_ever_live (eliminables[i].from, true);
1435     }
1436 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1437   if (! regs_asm_clobbered[HARD_FRAME_POINTER_REGNUM])
1438     {
1439       SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
1440       if (need_fp)
1441         SET_HARD_REG_BIT (ira_no_alloc_regs, HARD_FRAME_POINTER_REGNUM);
1442     }
1443   else if (need_fp)
1444     error ("%s cannot be used in asm here",
1445            reg_names[HARD_FRAME_POINTER_REGNUM]);
1446   else
1447     df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
1448 #endif
1449
1450 #else
1451   if (! regs_asm_clobbered[FRAME_POINTER_REGNUM])
1452     {
1453       SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
1454       if (need_fp)
1455         SET_HARD_REG_BIT (ira_no_alloc_regs, FRAME_POINTER_REGNUM);
1456     }
1457   else if (need_fp)
1458     error ("%s cannot be used in asm here", reg_names[FRAME_POINTER_REGNUM]);
1459   else
1460     df_set_regs_ever_live (FRAME_POINTER_REGNUM, true);
1461 #endif
1462 }
1463
1464 \f
1465
1466 /* The length of the following two arrays.  */
1467 int ira_reg_equiv_len;
1468
1469 /* The element value is TRUE if the corresponding regno value is
1470    invariant.  */
1471 bool *ira_reg_equiv_invariant_p;
1472
1473 /* The element value is equiv constant of given pseudo-register or
1474    NULL_RTX.  */
1475 rtx *ira_reg_equiv_const;
1476
1477 /* Set up the two arrays declared above.  */
1478 static void
1479 find_reg_equiv_invariant_const (void)
1480 {
1481   int i;
1482   bool invariant_p;
1483   rtx list, insn, note, constant, x;
1484
1485   for (i = FIRST_PSEUDO_REGISTER; i < reg_equiv_init_size; i++)
1486     {
1487       constant = NULL_RTX;
1488       invariant_p = false;
1489       for (list = reg_equiv_init[i]; list != NULL_RTX; list = XEXP (list, 1))
1490         {
1491           insn = XEXP (list, 0);
1492           note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
1493           
1494           if (note == NULL_RTX)
1495             continue;
1496
1497           x = XEXP (note, 0);
1498           
1499           if (! function_invariant_p (x)
1500               || ! flag_pic
1501               /* A function invariant is often CONSTANT_P but may
1502                  include a register.  We promise to only pass CONSTANT_P
1503                  objects to LEGITIMATE_PIC_OPERAND_P.  */
1504               || (CONSTANT_P (x) && LEGITIMATE_PIC_OPERAND_P (x)))
1505             {
1506               /* It can happen that a REG_EQUIV note contains a MEM
1507                  that is not a legitimate memory operand.  As later
1508                  stages of the reload assume that all addresses found
1509                  in the reg_equiv_* arrays were originally legitimate,
1510                  we ignore such REG_EQUIV notes.  */
1511               if (memory_operand (x, VOIDmode))
1512                 invariant_p = MEM_READONLY_P (x);
1513               else if (function_invariant_p (x))
1514                 {
1515                   if (GET_CODE (x) == PLUS
1516                       || x == frame_pointer_rtx || x == arg_pointer_rtx)
1517                     invariant_p = true;
1518                   else
1519                     constant = x;
1520                 }
1521             }
1522         }
1523       ira_reg_equiv_invariant_p[i] = invariant_p;
1524       ira_reg_equiv_const[i] = constant;
1525     }
1526 }
1527
1528 \f
1529
1530 /* Set up REG_RENUMBER and CALLER_SAVE_NEEDED (used by reload) from
1531    the allocation found by IRA.  */
1532 static void
1533 setup_reg_renumber (void)
1534 {
1535   int regno, hard_regno;
1536   ira_allocno_t a;
1537   ira_allocno_iterator ai;
1538
1539   caller_save_needed = 0;
1540   FOR_EACH_ALLOCNO (a, ai)
1541     {
1542       /* There are no caps at this point.  */
1543       ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
1544       if (! ALLOCNO_ASSIGNED_P (a))
1545         /* It can happen if A is not referenced but partially anticipated
1546            somewhere in a region.  */
1547         ALLOCNO_ASSIGNED_P (a) = true;
1548       ira_free_allocno_updated_costs (a);
1549       hard_regno = ALLOCNO_HARD_REGNO (a);
1550       regno = (int) REGNO (ALLOCNO_REG (a));
1551       reg_renumber[regno] = (hard_regno < 0 ? -1 : hard_regno);
1552       if (hard_regno >= 0 && ALLOCNO_CALLS_CROSSED_NUM (a) != 0
1553           && ! ira_hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
1554                                           call_used_reg_set))
1555         {
1556           ira_assert (!optimize || flag_caller_saves
1557                       || regno >= ira_reg_equiv_len
1558                       || ira_reg_equiv_const[regno]
1559                       || ira_reg_equiv_invariant_p[regno]);
1560           caller_save_needed = 1;
1561         }
1562     }
1563 }
1564
1565 /* Set up allocno assignment flags for further allocation
1566    improvements.  */
1567 static void
1568 setup_allocno_assignment_flags (void)
1569 {
1570   int hard_regno;
1571   ira_allocno_t a;
1572   ira_allocno_iterator ai;
1573
1574   FOR_EACH_ALLOCNO (a, ai)
1575     {
1576       if (! ALLOCNO_ASSIGNED_P (a))
1577         /* It can happen if A is not referenced but partially anticipated
1578            somewhere in a region.  */
1579         ira_free_allocno_updated_costs (a);
1580       hard_regno = ALLOCNO_HARD_REGNO (a);
1581       /* Don't assign hard registers to allocnos which are destination
1582          of removed store at the end of loop.  It has no sense to keep
1583          the same value in different hard registers.  It is also
1584          impossible to assign hard registers correctly to such
1585          allocnos because the cost info and info about intersected
1586          calls are incorrect for them.  */
1587       ALLOCNO_ASSIGNED_P (a) = (hard_regno >= 0
1588                                 || ALLOCNO_MEM_OPTIMIZED_DEST_P (a)
1589                                 || (ALLOCNO_MEMORY_COST (a)
1590                                     - ALLOCNO_COVER_CLASS_COST (a)) < 0);
1591       ira_assert (hard_regno < 0
1592                   || ! ira_hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
1593                                                   reg_class_contents
1594                                                   [ALLOCNO_COVER_CLASS (a)]));
1595     }
1596 }
1597
1598 /* Evaluate overall allocation cost and the costs for using hard
1599    registers and memory for allocnos.  */
1600 static void
1601 calculate_allocation_cost (void)
1602 {
1603   int hard_regno, cost;
1604   ira_allocno_t a;
1605   ira_allocno_iterator ai;
1606
1607   ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
1608   FOR_EACH_ALLOCNO (a, ai)
1609     {
1610       hard_regno = ALLOCNO_HARD_REGNO (a);
1611       ira_assert (hard_regno < 0
1612                   || ! ira_hard_reg_not_in_set_p
1613                        (hard_regno, ALLOCNO_MODE (a),
1614                         reg_class_contents[ALLOCNO_COVER_CLASS (a)])); 
1615       if (hard_regno < 0)
1616         {
1617           cost = ALLOCNO_MEMORY_COST (a);
1618           ira_mem_cost += cost;
1619         }
1620       else if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
1621         {
1622           cost = (ALLOCNO_HARD_REG_COSTS (a)
1623                   [ira_class_hard_reg_index
1624                    [ALLOCNO_COVER_CLASS (a)][hard_regno]]);
1625           ira_reg_cost += cost;
1626         }
1627       else
1628         {
1629           cost = ALLOCNO_COVER_CLASS_COST (a);
1630           ira_reg_cost += cost;
1631         }
1632       ira_overall_cost += cost;
1633     }
1634
1635   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1636     {
1637       fprintf (ira_dump_file,
1638                "+++Costs: overall %d, reg %d, mem %d, ld %d, st %d, move %d\n",
1639                ira_overall_cost, ira_reg_cost, ira_mem_cost,
1640                ira_load_cost, ira_store_cost, ira_shuffle_cost);
1641       fprintf (ira_dump_file, "+++       move loops %d, new jumps %d\n",
1642                ira_move_loops_num, ira_additional_jumps_num);
1643     }
1644
1645 }
1646
1647 #ifdef ENABLE_IRA_CHECKING
1648 /* Check the correctness of the allocation.  We do need this because
1649    of complicated code to transform more one region internal
1650    representation into one region representation.  */
1651 static void
1652 check_allocation (void)
1653 {
1654   ira_allocno_t a, conflict_a;
1655   int hard_regno, conflict_hard_regno, nregs, conflict_nregs;
1656   ira_allocno_conflict_iterator aci;
1657   ira_allocno_iterator ai;
1658
1659   FOR_EACH_ALLOCNO (a, ai)
1660     {
1661       if (ALLOCNO_CAP_MEMBER (a) != NULL
1662           || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
1663         continue;
1664       nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
1665       FOR_EACH_ALLOCNO_CONFLICT (a, conflict_a, aci)
1666         if ((conflict_hard_regno = ALLOCNO_HARD_REGNO (conflict_a)) >= 0)
1667           {
1668             conflict_nregs
1669               = (hard_regno_nregs
1670                  [conflict_hard_regno][ALLOCNO_MODE (conflict_a)]);
1671             if ((conflict_hard_regno <= hard_regno
1672                  && hard_regno < conflict_hard_regno + conflict_nregs)
1673                 || (hard_regno <= conflict_hard_regno
1674                     && conflict_hard_regno < hard_regno + nregs))
1675               {
1676                 fprintf (stderr, "bad allocation for %d and %d\n",
1677                          ALLOCNO_REGNO (a), ALLOCNO_REGNO (conflict_a));
1678                 gcc_unreachable ();
1679               }
1680           }
1681     }
1682 }
1683 #endif
1684
1685 /* Fix values of array REG_EQUIV_INIT after live range splitting done
1686    by IRA.  */
1687 static void
1688 fix_reg_equiv_init (void)
1689 {
1690   int max_regno = max_reg_num ();
1691   int i, new_regno;
1692   rtx x, prev, next, insn, set;
1693   
1694   if (reg_equiv_init_size < max_regno)
1695     {
1696       reg_equiv_init
1697         = (rtx *) ggc_realloc (reg_equiv_init, max_regno * sizeof (rtx));
1698       while (reg_equiv_init_size < max_regno)
1699         reg_equiv_init[reg_equiv_init_size++] = NULL_RTX;
1700       for (i = FIRST_PSEUDO_REGISTER; i < reg_equiv_init_size; i++)
1701         for (prev = NULL_RTX, x = reg_equiv_init[i]; x != NULL_RTX; x = next)
1702           {
1703             next = XEXP (x, 1);
1704             insn = XEXP (x, 0);
1705             set = single_set (insn);
1706             ira_assert (set != NULL_RTX
1707                         && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))));
1708             if (REG_P (SET_DEST (set))
1709                 && ((int) REGNO (SET_DEST (set)) == i
1710                     || (int) ORIGINAL_REGNO (SET_DEST (set)) == i))
1711               new_regno = REGNO (SET_DEST (set));
1712             else if (REG_P (SET_SRC (set))
1713                      && ((int) REGNO (SET_SRC (set)) == i
1714                          || (int) ORIGINAL_REGNO (SET_SRC (set)) == i))
1715               new_regno = REGNO (SET_SRC (set));
1716             else
1717               gcc_unreachable ();
1718             if (new_regno == i)
1719               prev = x;
1720             else
1721               {
1722                 if (prev == NULL_RTX)
1723                   reg_equiv_init[i] = next;
1724                 else
1725                   XEXP (prev, 1) = next;
1726                 XEXP (x, 1) = reg_equiv_init[new_regno];
1727                 reg_equiv_init[new_regno] = x;
1728               }
1729           }
1730     }
1731 }
1732
1733 #ifdef ENABLE_IRA_CHECKING
1734 /* Print redundant memory-memory copies.  */
1735 static void
1736 print_redundant_copies (void)
1737 {
1738   int hard_regno;
1739   ira_allocno_t a;
1740   ira_copy_t cp, next_cp;
1741   ira_allocno_iterator ai;
1742   
1743   FOR_EACH_ALLOCNO (a, ai)
1744     {
1745       if (ALLOCNO_CAP_MEMBER (a) != NULL)
1746         /* It is a cap. */
1747         continue;
1748       hard_regno = ALLOCNO_HARD_REGNO (a);
1749       if (hard_regno >= 0)
1750         continue;
1751       for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1752         if (cp->first == a)
1753           next_cp = cp->next_first_allocno_copy;
1754         else
1755           {
1756             next_cp = cp->next_second_allocno_copy;
1757             if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL
1758                 && cp->insn != NULL_RTX
1759                 && ALLOCNO_HARD_REGNO (cp->first) == hard_regno)
1760               fprintf (ira_dump_file,
1761                        "        Redundant move from %d(freq %d):%d\n",
1762                        INSN_UID (cp->insn), cp->freq, hard_regno);
1763           }
1764     }
1765 }
1766 #endif
1767
1768 /* Setup preferred and alternative classes for new pseudo-registers
1769    created by IRA starting with START.  */
1770 static void
1771 setup_preferred_alternate_classes_for_new_pseudos (int start)
1772 {
1773   int i, old_regno;
1774   int max_regno = max_reg_num ();
1775
1776   for (i = start; i < max_regno; i++)
1777     {
1778       old_regno = ORIGINAL_REGNO (regno_reg_rtx[i]);
1779       ira_assert (i != old_regno); 
1780       setup_reg_classes (i, reg_preferred_class (old_regno),
1781                          reg_alternate_class (old_regno));
1782       if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
1783         fprintf (ira_dump_file,
1784                  "    New r%d: setting preferred %s, alternative %s\n",
1785                  i, reg_class_names[reg_preferred_class (old_regno)],
1786                  reg_class_names[reg_alternate_class (old_regno)]);
1787     }
1788 }
1789
1790 \f
1791
1792 /* Regional allocation can create new pseudo-registers.  This function
1793    expands some arrays for pseudo-registers.  */
1794 static void
1795 expand_reg_info (int old_size)
1796 {
1797   int i;
1798   int size = max_reg_num ();
1799
1800   resize_reg_info ();
1801   for (i = old_size; i < size; i++)
1802     {
1803       reg_renumber[i] = -1;
1804       setup_reg_classes (i, GENERAL_REGS, ALL_REGS);
1805     }
1806 }
1807
1808 /* Return TRUE if there is too high register pressure in the function.
1809    It is used to decide when stack slot sharing is worth to do.  */
1810 static bool
1811 too_high_register_pressure_p (void)
1812 {
1813   int i;
1814   enum reg_class cover_class;
1815   
1816   for (i = 0; i < ira_reg_class_cover_size; i++)
1817     {
1818       cover_class = ira_reg_class_cover[i];
1819       if (ira_loop_tree_root->reg_pressure[cover_class] > 10000)
1820         return true;
1821     }
1822   return false;
1823 }
1824
1825 \f
1826
1827 /* All natural loops.  */
1828 struct loops ira_loops;
1829
1830 /* This is the main entry of IRA.  */
1831 static void
1832 ira (FILE *f)
1833 {
1834   int overall_cost_before, allocated_reg_info_size;
1835   bool loops_p;
1836   int max_regno_before_ira, ira_max_point_before_emit;
1837   int rebuild_p;
1838   int saved_flag_ira_share_spill_slots;
1839   basic_block bb;
1840
1841   timevar_push (TV_IRA);
1842
1843   if (flag_ira_verbose < 10)
1844     {
1845       internal_flag_ira_verbose = flag_ira_verbose;
1846       ira_dump_file = f;
1847     }
1848   else
1849     {
1850       internal_flag_ira_verbose = flag_ira_verbose - 10;
1851       ira_dump_file = stderr;
1852     }
1853
1854   setup_prohibited_mode_move_regs ();
1855
1856   df_note_add_problem ();
1857
1858   if (optimize == 1)
1859     {
1860       df_live_add_problem ();
1861       df_live_set_all_dirty ();
1862     }
1863 #ifdef ENABLE_CHECKING
1864   df->changeable_flags |= DF_VERIFY_SCHEDULED;
1865 #endif
1866   df_analyze ();
1867   df_clear_flags (DF_NO_INSN_RESCAN);
1868   regstat_init_n_sets_and_refs ();
1869   regstat_compute_ri ();
1870
1871   /* If we are not optimizing, then this is the only place before
1872      register allocation where dataflow is done.  And that is needed
1873      to generate these warnings.  */
1874   if (warn_clobbered)
1875     generate_setjmp_warnings ();
1876
1877   rebuild_p = update_equiv_regs ();
1878
1879 #ifndef IRA_NO_OBSTACK
1880   gcc_obstack_init (&ira_obstack);
1881 #endif
1882   bitmap_obstack_initialize (&ira_bitmap_obstack);
1883   if (optimize)
1884     {      
1885       max_regno = max_reg_num ();
1886       ira_reg_equiv_len = max_regno;
1887       ira_reg_equiv_invariant_p
1888         = (bool *) ira_allocate (max_regno * sizeof (bool));
1889       memset (ira_reg_equiv_invariant_p, 0, max_regno * sizeof (bool));
1890       ira_reg_equiv_const = (rtx *) ira_allocate (max_regno * sizeof (rtx));
1891       memset (ira_reg_equiv_const, 0, max_regno * sizeof (rtx));
1892       find_reg_equiv_invariant_const ();
1893       if (rebuild_p)
1894         {
1895           timevar_push (TV_JUMP);
1896           rebuild_jump_labels (get_insns ());
1897           purge_all_dead_edges ();
1898           timevar_pop (TV_JUMP);
1899         }
1900     }
1901
1902   max_regno_before_ira = allocated_reg_info_size = max_reg_num ();
1903   allocate_reg_info ();
1904   setup_eliminable_regset ();
1905       
1906   ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
1907   ira_load_cost = ira_store_cost = ira_shuffle_cost = 0;
1908   ira_move_loops_num = ira_additional_jumps_num = 0;
1909   
1910   ira_assert (current_loops == NULL);
1911   flow_loops_find (&ira_loops);
1912   current_loops = &ira_loops;
1913       
1914   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1915     fprintf (ira_dump_file, "Building IRA IR\n");
1916   loops_p = ira_build (optimize
1917                        && (flag_ira_region == IRA_REGION_ALL
1918                            || flag_ira_region == IRA_REGION_MIXED));
1919
1920   saved_flag_ira_share_spill_slots = flag_ira_share_spill_slots;
1921   if (too_high_register_pressure_p ())
1922     /* It is just wasting compiler's time to pack spilled pseudos into
1923        stack slots in this case -- prohibit it.  */ 
1924     flag_ira_share_spill_slots = FALSE;
1925
1926   ira_color ();
1927       
1928   ira_max_point_before_emit = ira_max_point;
1929       
1930   ira_emit (loops_p);
1931   
1932   if (optimize)
1933     {
1934       max_regno = max_reg_num ();
1935       
1936       if (! loops_p)
1937         ira_initiate_assign ();
1938       else
1939         {
1940           expand_reg_info (allocated_reg_info_size);
1941           setup_preferred_alternate_classes_for_new_pseudos
1942             (allocated_reg_info_size);
1943           allocated_reg_info_size = max_regno;
1944           
1945           if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1946             fprintf (ira_dump_file, "Flattening IR\n");
1947           ira_flattening (max_regno_before_ira, ira_max_point_before_emit);
1948           /* New insns were generated: add notes and recalculate live
1949              info.  */
1950           df_analyze ();
1951           
1952           flow_loops_find (&ira_loops);
1953           current_loops = &ira_loops;
1954
1955           setup_allocno_assignment_flags ();
1956           ira_initiate_assign ();
1957           ira_reassign_conflict_allocnos (max_regno);
1958         }
1959     }
1960
1961   setup_reg_renumber ();
1962   
1963   calculate_allocation_cost ();
1964   
1965 #ifdef ENABLE_IRA_CHECKING
1966   if (optimize)
1967     check_allocation ();
1968 #endif
1969       
1970   delete_trivially_dead_insns (get_insns (), max_reg_num ());
1971   max_regno = max_reg_num ();
1972   
1973   /* Determine if the current function is a leaf before running IRA
1974      since this can impact optimizations done by the prologue and
1975      epilogue thus changing register elimination offsets.  */
1976   current_function_is_leaf = leaf_function_p ();
1977   
1978   /* And the reg_equiv_memory_loc array.  */
1979   VEC_safe_grow (rtx, gc, reg_equiv_memory_loc_vec, max_regno);
1980   memset (VEC_address (rtx, reg_equiv_memory_loc_vec), 0,
1981           sizeof (rtx) * max_regno);
1982   reg_equiv_memory_loc = VEC_address (rtx, reg_equiv_memory_loc_vec);
1983
1984   if (max_regno != max_regno_before_ira)
1985     {
1986       regstat_free_n_sets_and_refs ();
1987       regstat_free_ri ();
1988       regstat_init_n_sets_and_refs ();
1989       regstat_compute_ri ();
1990     }
1991
1992   allocate_initial_values (reg_equiv_memory_loc);
1993
1994   overall_cost_before = ira_overall_cost;
1995   if (optimize)
1996     {
1997       fix_reg_equiv_init ();
1998       
1999 #ifdef ENABLE_IRA_CHECKING
2000       print_redundant_copies ();
2001 #endif
2002
2003       ira_spilled_reg_stack_slots_num = 0;
2004       ira_spilled_reg_stack_slots
2005         = ((struct ira_spilled_reg_stack_slot *)
2006            ira_allocate (max_regno
2007                          * sizeof (struct ira_spilled_reg_stack_slot)));
2008       memset (ira_spilled_reg_stack_slots, 0,
2009               max_regno * sizeof (struct ira_spilled_reg_stack_slot));
2010     }
2011   
2012   timevar_pop (TV_IRA);
2013
2014   timevar_push (TV_RELOAD);
2015   df_set_flags (DF_NO_INSN_RESCAN);
2016   build_insn_chain ();
2017
2018   reload_completed = !reload (get_insns (), optimize > 0);
2019
2020   timevar_pop (TV_RELOAD);
2021
2022   timevar_push (TV_IRA);
2023
2024   if (optimize)
2025     {
2026       ira_free (ira_spilled_reg_stack_slots);
2027       
2028       ira_finish_assign ();
2029       
2030     }  
2031   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL
2032       && overall_cost_before != ira_overall_cost)
2033     fprintf (ira_dump_file, "+++Overall after reload %d\n", ira_overall_cost);
2034   ira_destroy ();
2035   
2036   flag_ira_share_spill_slots = saved_flag_ira_share_spill_slots;
2037
2038   flow_loops_free (&ira_loops);
2039   free_dominance_info (CDI_DOMINATORS);
2040   FOR_ALL_BB (bb)
2041     bb->loop_father = NULL;
2042   current_loops = NULL;
2043
2044   regstat_free_ri ();
2045   regstat_free_n_sets_and_refs ();
2046       
2047   if (optimize)
2048     {
2049       cleanup_cfg (CLEANUP_EXPENSIVE);
2050       
2051       ira_free (ira_reg_equiv_invariant_p);
2052       ira_free (ira_reg_equiv_const);
2053     }
2054
2055   bitmap_obstack_release (&ira_bitmap_obstack);
2056 #ifndef IRA_NO_OBSTACK
2057   obstack_free (&ira_obstack, NULL);
2058 #endif
2059
2060   /* The code after the reload has changed so much that at this point
2061      we might as well just rescan everything.  Not that
2062      df_rescan_all_insns is not going to help here because it does not
2063      touch the artificial uses and defs.  */
2064   df_finish_pass (true);
2065   if (optimize > 1)
2066     df_live_add_problem ();
2067   df_scan_alloc (NULL);
2068   df_scan_blocks ();
2069
2070   if (optimize)
2071     df_analyze ();
2072
2073   timevar_pop (TV_IRA);
2074 }
2075
2076 \f
2077
2078 static bool
2079 gate_ira (void)
2080 {
2081   return flag_ira != 0;
2082 }
2083
2084 /* Run the integrated register allocator.  */
2085 static unsigned int
2086 rest_of_handle_ira (void)
2087 {
2088   ira (dump_file);
2089   return 0;
2090 }
2091
2092 struct rtl_opt_pass pass_ira =
2093 {
2094  {
2095   RTL_PASS,
2096   "ira",                                /* name */
2097   gate_ira,                             /* gate */
2098   rest_of_handle_ira,                   /* execute */
2099   NULL,                                 /* sub */
2100   NULL,                                 /* next */
2101   0,                                    /* static_pass_number */
2102   0,                                    /* tv_id */
2103   0,                                    /* properties_required */
2104   0,                                    /* properties_provided */
2105   0,                                    /* properties_destroyed */
2106   0,                                    /* todo_flags_start */
2107   TODO_dump_func |
2108   TODO_ggc_collect                      /* todo_flags_finish */
2109  }
2110 };