OSDN Git Service

* include/tr1_impl/array (at): Do not use builtin_expect.
[pf3gnuchains/gcc-fork.git] / gcc / ira.c
1 /* Integrated Register Allocator (IRA) entry point.
2    Copyright (C) 2006, 2007, 2008, 2009
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 "except.h"
320 #include "reload.h"
321 #include "errors.h"
322 #include "integrate.h"
323 #include "df.h"
324 #include "ggc.h"
325 #include "ira-int.h"
326
327
328 /* A modified value of flag `-fira-verbose' used internally.  */
329 int internal_flag_ira_verbose;
330
331 /* Dump file of the allocator if it is not NULL.  */
332 FILE *ira_dump_file;
333
334 /* Pools for allocnos, copies, allocno live ranges.  */
335 alloc_pool allocno_pool, copy_pool, allocno_live_range_pool;
336
337 /* The number of elements in the following array.  */
338 int ira_spilled_reg_stack_slots_num;
339
340 /* The following array contains info about spilled pseudo-registers
341    stack slots used in current function so far.  */
342 struct ira_spilled_reg_stack_slot *ira_spilled_reg_stack_slots;
343
344 /* Correspondingly overall cost of the allocation, cost of the
345    allocnos assigned to hard-registers, cost of the allocnos assigned
346    to memory, cost of loads, stores and register move insns generated
347    for pseudo-register live range splitting (see ira-emit.c).  */
348 int ira_overall_cost;
349 int ira_reg_cost, ira_mem_cost;
350 int ira_load_cost, ira_store_cost, ira_shuffle_cost;
351 int ira_move_loops_num, ira_additional_jumps_num;
352
353 /* All registers that can be eliminated.  */
354
355 HARD_REG_SET eliminable_regset;
356
357 /* Map: hard regs X modes -> set of hard registers for storing value
358    of given mode starting with given hard register.  */
359 HARD_REG_SET ira_reg_mode_hard_regset[FIRST_PSEUDO_REGISTER][NUM_MACHINE_MODES];
360
361 /* The following two variables are array analogs of the macros
362    MEMORY_MOVE_COST and REGISTER_MOVE_COST.  */
363 short int ira_memory_move_cost[MAX_MACHINE_MODE][N_REG_CLASSES][2];
364 move_table *ira_register_move_cost[MAX_MACHINE_MODE];
365
366 /* Similar to may_move_in_cost but it is calculated in IRA instead of
367    regclass.  Another difference is that we take only available hard
368    registers into account to figure out that one register class is a
369    subset of the another one.  */
370 move_table *ira_may_move_in_cost[MAX_MACHINE_MODE];
371
372 /* Similar to may_move_out_cost but it is calculated in IRA instead of
373    regclass.  Another difference is that we take only available hard
374    registers into account to figure out that one register class is a
375    subset of the another one.  */
376 move_table *ira_may_move_out_cost[MAX_MACHINE_MODE];
377
378 /* Register class subset relation: TRUE if the first class is a subset
379    of the second one considering only hard registers available for the
380    allocation.  */
381 int ira_class_subset_p[N_REG_CLASSES][N_REG_CLASSES];
382
383 /* Temporary hard reg set used for a different calculation.  */
384 static HARD_REG_SET temp_hard_regset;
385
386 \f
387
388 /* The function sets up the map IRA_REG_MODE_HARD_REGSET.  */
389 static void
390 setup_reg_mode_hard_regset (void)
391 {
392   int i, m, hard_regno;
393
394   for (m = 0; m < NUM_MACHINE_MODES; m++)
395     for (hard_regno = 0; hard_regno < FIRST_PSEUDO_REGISTER; hard_regno++)
396       {
397         CLEAR_HARD_REG_SET (ira_reg_mode_hard_regset[hard_regno][m]);
398         for (i = hard_regno_nregs[hard_regno][m] - 1; i >= 0; i--)
399           if (hard_regno + i < FIRST_PSEUDO_REGISTER)
400             SET_HARD_REG_BIT (ira_reg_mode_hard_regset[hard_regno][m],
401                               hard_regno + i);
402       }
403 }
404
405 \f
406
407 /* Hard registers that can not be used for the register allocator for
408    all functions of the current compilation unit.  */
409 static HARD_REG_SET no_unit_alloc_regs;
410
411 /* Array of the number of hard registers of given class which are
412    available for allocation.  The order is defined by the
413    allocation order.  */
414 short ira_class_hard_regs[N_REG_CLASSES][FIRST_PSEUDO_REGISTER];
415
416 /* The number of elements of the above array for given register
417    class.  */
418 int ira_class_hard_regs_num[N_REG_CLASSES];
419
420 /* Index (in ira_class_hard_regs) for given register class and hard
421    register (in general case a hard register can belong to several
422    register classes).  The index is negative for hard registers
423    unavailable for the allocation. */
424 short ira_class_hard_reg_index[N_REG_CLASSES][FIRST_PSEUDO_REGISTER];
425
426 /* The function sets up the three arrays declared above.  */
427 static void
428 setup_class_hard_regs (void)
429 {
430   int cl, i, hard_regno, n;
431   HARD_REG_SET processed_hard_reg_set;
432
433   ira_assert (SHRT_MAX >= FIRST_PSEUDO_REGISTER);
434   /* We could call ORDER_REGS_FOR_LOCAL_ALLOC here (it is usually
435      putting hard callee-used hard registers first).  But our
436      heuristics work better.  */
437   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
438     {
439       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
440       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
441       CLEAR_HARD_REG_SET (processed_hard_reg_set);
442       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
443         ira_class_hard_reg_index[cl][0] = -1;
444       for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
445         {
446 #ifdef REG_ALLOC_ORDER
447           hard_regno = reg_alloc_order[i];
448 #else
449           hard_regno = i;
450 #endif    
451           if (TEST_HARD_REG_BIT (processed_hard_reg_set, hard_regno))
452             continue;
453           SET_HARD_REG_BIT (processed_hard_reg_set, hard_regno);
454           if (! TEST_HARD_REG_BIT (temp_hard_regset, hard_regno))
455             ira_class_hard_reg_index[cl][hard_regno] = -1;
456           else
457             {
458               ira_class_hard_reg_index[cl][hard_regno] = n;
459               ira_class_hard_regs[cl][n++] = hard_regno;
460             }
461         }
462       ira_class_hard_regs_num[cl] = n;
463     }
464 }
465
466 /* Number of given class hard registers available for the register
467    allocation for given classes.  */
468 int ira_available_class_regs[N_REG_CLASSES];
469
470 /* Set up IRA_AVAILABLE_CLASS_REGS.  */
471 static void
472 setup_available_class_regs (void)
473 {
474   int i, j;
475
476   memset (ira_available_class_regs, 0, sizeof (ira_available_class_regs));
477   for (i = 0; i < N_REG_CLASSES; i++)
478     {
479       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
480       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
481       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
482         if (TEST_HARD_REG_BIT (temp_hard_regset, j))
483           ira_available_class_regs[i]++;
484     }
485 }
486
487 /* Set up global variables defining info about hard registers for the
488    allocation.  These depend on USE_HARD_FRAME_P whose TRUE value means
489    that we can use the hard frame pointer for the allocation.  */
490 static void
491 setup_alloc_regs (bool use_hard_frame_p)
492 {
493   COPY_HARD_REG_SET (no_unit_alloc_regs, fixed_reg_set);
494   if (! use_hard_frame_p)
495     SET_HARD_REG_BIT (no_unit_alloc_regs, HARD_FRAME_POINTER_REGNUM);
496   setup_class_hard_regs ();
497   setup_available_class_regs ();
498 }
499
500 \f
501
502 /* Set up IRA_MEMORY_MOVE_COST, IRA_REGISTER_MOVE_COST.  */
503 static void
504 setup_class_subset_and_memory_move_costs (void)
505 {
506   int cl, cl2;
507   enum machine_mode mode;
508   HARD_REG_SET temp_hard_regset2;
509
510   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
511     ira_memory_move_cost[mode][NO_REGS][0]
512       = ira_memory_move_cost[mode][NO_REGS][1] = SHRT_MAX;
513   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
514     {
515       if (cl != (int) NO_REGS)
516         for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
517           {
518             ira_memory_move_cost[mode][cl][0] =
519               MEMORY_MOVE_COST (mode, (enum reg_class) cl, 0);
520             ira_memory_move_cost[mode][cl][1] =
521               MEMORY_MOVE_COST (mode, (enum reg_class) cl, 1);
522             /* Costs for NO_REGS are used in cost calculation on the
523                1st pass when the preferred register classes are not
524                known yet.  In this case we take the best scenario.  */
525             if (ira_memory_move_cost[mode][NO_REGS][0]
526                 > ira_memory_move_cost[mode][cl][0])
527               ira_memory_move_cost[mode][NO_REGS][0]
528                 = ira_memory_move_cost[mode][cl][0];
529             if (ira_memory_move_cost[mode][NO_REGS][1]
530                 > ira_memory_move_cost[mode][cl][1])
531               ira_memory_move_cost[mode][NO_REGS][1]
532                 = ira_memory_move_cost[mode][cl][1];
533           }
534       for (cl2 = (int) N_REG_CLASSES - 1; cl2 >= 0; cl2--)
535         {
536           COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
537           AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
538           COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl2]);
539           AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
540           ira_class_subset_p[cl][cl2]
541             = hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2);
542         }
543     }
544 }
545
546 \f
547
548 /* Define the following macro if allocation through malloc if
549    preferable.  */
550 #define IRA_NO_OBSTACK
551
552 #ifndef IRA_NO_OBSTACK
553 /* Obstack used for storing all dynamic data (except bitmaps) of the
554    IRA.  */
555 static struct obstack ira_obstack;
556 #endif
557
558 /* Obstack used for storing all bitmaps of the IRA.  */
559 static struct bitmap_obstack ira_bitmap_obstack;
560
561 /* Allocate memory of size LEN for IRA data.  */
562 void *
563 ira_allocate (size_t len)
564 {
565   void *res;
566
567 #ifndef IRA_NO_OBSTACK
568   res = obstack_alloc (&ira_obstack, len);
569 #else
570   res = xmalloc (len);
571 #endif
572   return res;
573 }
574
575 /* Reallocate memory PTR of size LEN for IRA data.  */
576 void *
577 ira_reallocate (void *ptr, size_t len)
578 {
579   void *res;
580
581 #ifndef IRA_NO_OBSTACK
582   res = obstack_alloc (&ira_obstack, len);
583 #else
584   res = xrealloc (ptr, len);
585 #endif
586   return res;
587 }
588
589 /* Free memory ADDR allocated for IRA data.  */
590 void
591 ira_free (void *addr ATTRIBUTE_UNUSED)
592 {
593 #ifndef IRA_NO_OBSTACK
594   /* do nothing */
595 #else
596   free (addr);
597 #endif
598 }
599
600
601 /* Allocate and returns bitmap for IRA.  */
602 bitmap
603 ira_allocate_bitmap (void)
604 {
605   return BITMAP_ALLOC (&ira_bitmap_obstack);
606 }
607
608 /* Free bitmap B allocated for IRA.  */
609 void
610 ira_free_bitmap (bitmap b ATTRIBUTE_UNUSED)
611 {
612   /* do nothing */
613 }
614
615 \f
616
617 /* Output information about allocation of all allocnos (except for
618    caps) into file F.  */
619 void
620 ira_print_disposition (FILE *f)
621 {
622   int i, n, max_regno;
623   ira_allocno_t a;
624   basic_block bb;
625
626   fprintf (f, "Disposition:");
627   max_regno = max_reg_num ();
628   for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
629     for (a = ira_regno_allocno_map[i];
630          a != NULL;
631          a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
632       {
633         if (n % 4 == 0)
634           fprintf (f, "\n");
635         n++;
636         fprintf (f, " %4d:r%-4d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
637         if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
638           fprintf (f, "b%-3d", bb->index);
639         else
640           fprintf (f, "l%-3d", ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
641         if (ALLOCNO_HARD_REGNO (a) >= 0)
642           fprintf (f, " %3d", ALLOCNO_HARD_REGNO (a));
643         else
644           fprintf (f, " mem");
645       }
646   fprintf (f, "\n");
647 }
648
649 /* Outputs information about allocation of all allocnos into
650    stderr.  */
651 void
652 ira_debug_disposition (void)
653 {
654   ira_print_disposition (stderr);
655 }
656
657 \f
658
659 /* For each reg class, table listing all the classes contained in it
660    (excluding the class itself.  Non-allocatable registers are
661    excluded from the consideration).  */
662 static enum reg_class alloc_reg_class_subclasses[N_REG_CLASSES][N_REG_CLASSES];
663
664 /* Initialize the table of subclasses of each reg class.  */
665 static void
666 setup_reg_subclasses (void)
667 {
668   int i, j;
669   HARD_REG_SET temp_hard_regset2;
670
671   for (i = 0; i < N_REG_CLASSES; i++)
672     for (j = 0; j < N_REG_CLASSES; j++)
673       alloc_reg_class_subclasses[i][j] = LIM_REG_CLASSES;
674
675   for (i = 0; i < N_REG_CLASSES; i++)
676     {
677       if (i == (int) NO_REGS)
678         continue;
679
680       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
681       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
682       if (hard_reg_set_empty_p (temp_hard_regset))
683         continue;
684       for (j = 0; j < N_REG_CLASSES; j++)
685         if (i != j)
686           {
687             enum reg_class *p;
688
689             COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[j]);
690             AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
691             if (! hard_reg_set_subset_p (temp_hard_regset,
692                                          temp_hard_regset2))
693               continue;
694             p = &alloc_reg_class_subclasses[j][0];
695             while (*p != LIM_REG_CLASSES) p++;
696             *p = (enum reg_class) i;
697           }
698     }
699 }
700
701 \f
702
703 /* Number of cover classes.  Cover classes is non-intersected register
704    classes containing all hard-registers available for the
705    allocation.  */
706 int ira_reg_class_cover_size;
707
708 /* The array containing cover classes (see also comments for macro
709    IRA_COVER_CLASSES).  Only first IRA_REG_CLASS_COVER_SIZE elements are
710    used for this.  */
711 enum reg_class ira_reg_class_cover[N_REG_CLASSES];
712
713 /* The number of elements in the subsequent array.  */
714 int ira_important_classes_num;
715
716 /* The array containing non-empty classes (including non-empty cover
717    classes) which are subclasses of cover classes.  Such classes is
718    important for calculation of the hard register usage costs.  */
719 enum reg_class ira_important_classes[N_REG_CLASSES];
720
721 /* The array containing indexes of important classes in the previous
722    array.  The array elements are defined only for important
723    classes.  */
724 int ira_important_class_nums[N_REG_CLASSES];
725
726 /* Set the four global variables defined above.  */
727 static void
728 setup_cover_and_important_classes (void)
729 {
730   int i, j, n;
731   bool set_p, eq_p;
732   enum reg_class cl;
733   const enum reg_class *cover_classes;
734   HARD_REG_SET temp_hard_regset2;
735   static enum reg_class classes[LIM_REG_CLASSES + 1];
736
737   if (targetm.ira_cover_classes == NULL)
738     cover_classes = NULL;
739   else
740     cover_classes = targetm.ira_cover_classes ();
741   if (cover_classes == NULL)
742     ira_assert (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY);
743   else
744     {
745       for (i = 0; (cl = cover_classes[i]) != LIM_REG_CLASSES; i++)
746         classes[i] = cl;
747       classes[i] = LIM_REG_CLASSES;
748     }
749
750   if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
751     {
752       n = 0;
753       for (i = 0; i <= LIM_REG_CLASSES; i++)
754         {
755           if (i == NO_REGS)
756             continue;
757 #ifdef CONSTRAINT__LIMIT
758           for (j = 0; j < CONSTRAINT__LIMIT; j++)
759             if ((int) regclass_for_constraint (j) == i)
760               break;
761           if (j < CONSTRAINT__LIMIT)
762             {
763               classes[n++] = i;
764               continue;
765             }
766 #endif
767           COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
768           AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
769           for (j = 0; j < LIM_REG_CLASSES; j++)
770             {
771               if (i == j)
772                 continue;
773               COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[j]);
774               AND_COMPL_HARD_REG_SET (temp_hard_regset2,
775                                       no_unit_alloc_regs);
776               if (hard_reg_set_equal_p (temp_hard_regset,
777                                         temp_hard_regset2))
778                     break;
779             }
780           if (j >= i)
781             classes[n++] = i;
782         }
783       classes[n] = LIM_REG_CLASSES;
784     }
785
786   ira_reg_class_cover_size = 0;
787   for (i = 0; (cl = classes[i]) != LIM_REG_CLASSES; i++)
788     {
789       for (j = 0; j < i; j++)
790         if (flag_ira_algorithm != IRA_ALGORITHM_PRIORITY
791             && reg_classes_intersect_p (cl, classes[j]))
792           gcc_unreachable ();
793       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
794       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
795       if (! hard_reg_set_empty_p (temp_hard_regset))
796         ira_reg_class_cover[ira_reg_class_cover_size++] = cl;
797     }
798   ira_important_classes_num = 0;
799   for (cl = 0; cl < N_REG_CLASSES; cl++)
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       if (! hard_reg_set_empty_p (temp_hard_regset))
804         {
805           set_p = eq_p = false;
806           for (j = 0; j < ira_reg_class_cover_size; j++)
807             {
808               COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
809               AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
810               COPY_HARD_REG_SET (temp_hard_regset2,
811                                  reg_class_contents[ira_reg_class_cover[j]]);
812               AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
813               if (cl == ira_reg_class_cover[j])
814                 {
815                   eq_p = false;
816                   set_p = true;
817                   break;
818                 }
819               else if (hard_reg_set_equal_p (temp_hard_regset,
820                                              temp_hard_regset2))
821                 eq_p = true;
822               else if (hard_reg_set_subset_p (temp_hard_regset,
823                                               temp_hard_regset2))
824                 set_p = true;
825             }
826           if (set_p && ! eq_p)
827             {
828               ira_important_class_nums[cl] = ira_important_classes_num;
829               ira_important_classes[ira_important_classes_num++] = cl;
830             }
831         }
832     }
833 }
834
835 /* Map of all register classes to corresponding cover class containing
836    the given class.  If given class is not a subset of a cover class,
837    we translate it into the cheapest cover class.  */
838 enum reg_class ira_class_translate[N_REG_CLASSES];
839
840 /* Set up array IRA_CLASS_TRANSLATE.  */
841 static void
842 setup_class_translate (void)
843 {
844   enum reg_class cl, cover_class, best_class, *cl_ptr;
845   enum machine_mode mode;
846   int i, cost, min_cost, best_cost;
847
848   for (cl = 0; cl < N_REG_CLASSES; cl++)
849     ira_class_translate[cl] = NO_REGS;
850   
851   if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
852     for (cl = 0; cl < LIM_REG_CLASSES; cl++)
853       {
854         COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
855         AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
856         for (i = 0; i < ira_reg_class_cover_size; i++)
857           {
858             HARD_REG_SET temp_hard_regset2;
859             
860             cover_class = ira_reg_class_cover[i];
861             COPY_HARD_REG_SET (temp_hard_regset2,
862                                reg_class_contents[cover_class]);
863             AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
864             if (hard_reg_set_equal_p (temp_hard_regset, temp_hard_regset2))
865               ira_class_translate[cl] = cover_class;
866           }
867       }
868   for (i = 0; i < ira_reg_class_cover_size; i++)
869     {
870       cover_class = ira_reg_class_cover[i];
871       if (flag_ira_algorithm != IRA_ALGORITHM_PRIORITY)
872         for (cl_ptr = &alloc_reg_class_subclasses[cover_class][0];
873              (cl = *cl_ptr) != LIM_REG_CLASSES;
874              cl_ptr++)
875           {
876             if (ira_class_translate[cl] == NO_REGS)
877               ira_class_translate[cl] = cover_class;
878 #ifdef ENABLE_IRA_CHECKING
879             else
880               {
881                 COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
882                 AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
883                 if (! hard_reg_set_empty_p (temp_hard_regset))
884                   gcc_unreachable ();
885               }
886 #endif
887           }
888       ira_class_translate[cover_class] = cover_class;
889     }
890   /* For classes which are not fully covered by a cover class (in
891      other words covered by more one cover class), use the cheapest
892      cover class.  */
893   for (cl = 0; cl < N_REG_CLASSES; cl++)
894     {
895       if (cl == NO_REGS || ira_class_translate[cl] != NO_REGS)
896         continue;
897       best_class = NO_REGS;
898       best_cost = INT_MAX;
899       for (i = 0; i < ira_reg_class_cover_size; i++)
900         {
901           cover_class = ira_reg_class_cover[i];
902           COPY_HARD_REG_SET (temp_hard_regset,
903                              reg_class_contents[cover_class]);
904           AND_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
905           AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
906           if (! hard_reg_set_empty_p (temp_hard_regset))
907             {
908               min_cost = INT_MAX;
909               for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
910                 {
911                   cost = (ira_memory_move_cost[mode][cl][0]
912                           + ira_memory_move_cost[mode][cl][1]);
913                   if (min_cost > cost)
914                     min_cost = cost;
915                 }
916               if (best_class == NO_REGS || best_cost > min_cost)
917                 {
918                   best_class = cover_class;
919                   best_cost = min_cost;
920                 }
921             }
922         }
923       ira_class_translate[cl] = best_class;
924     }
925 }
926
927 /* The biggest important reg_class inside of intersection of the two
928    reg_classes (that is calculated taking only hard registers
929    available for allocation into account).  If the both reg_classes
930    contain no hard registers available for allocation, the value is
931    calculated by taking all hard-registers including fixed ones into
932    account.  */
933 enum reg_class ira_reg_class_intersect[N_REG_CLASSES][N_REG_CLASSES];
934
935 /* True if the two classes (that is calculated taking only hard
936    registers available for allocation into account) are
937    intersected.  */
938 bool ira_reg_classes_intersect_p[N_REG_CLASSES][N_REG_CLASSES];
939
940 /* Important classes with end marker LIM_REG_CLASSES which are
941    supersets with given important class (the first index).  That
942    includes given class itself.  This is calculated taking only hard
943    registers available for allocation into account.  */
944 enum reg_class ira_reg_class_super_classes[N_REG_CLASSES][N_REG_CLASSES];
945
946 /* The biggest important reg_class inside of union of the two
947    reg_classes (that is calculated taking only hard registers
948    available for allocation into account).  If the both reg_classes
949    contain no hard registers available for allocation, the value is
950    calculated by taking all hard-registers including fixed ones into
951    account.  In other words, the value is the corresponding
952    reg_class_subunion value.  */
953 enum reg_class ira_reg_class_union[N_REG_CLASSES][N_REG_CLASSES];
954
955 /* Set up the above reg class relations.  */
956 static void
957 setup_reg_class_relations (void)
958 {
959   int i, cl1, cl2, cl3;
960   HARD_REG_SET intersection_set, union_set, temp_set2;
961   bool important_class_p[N_REG_CLASSES];
962
963   memset (important_class_p, 0, sizeof (important_class_p));
964   for (i = 0; i < ira_important_classes_num; i++)
965     important_class_p[ira_important_classes[i]] = true;
966   for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
967     {
968       ira_reg_class_super_classes[cl1][0] = LIM_REG_CLASSES;
969       for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
970         {
971           ira_reg_classes_intersect_p[cl1][cl2] = false;
972           ira_reg_class_intersect[cl1][cl2] = NO_REGS;
973           COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl1]);
974           AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
975           COPY_HARD_REG_SET (temp_set2, reg_class_contents[cl2]);
976           AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
977           if (hard_reg_set_empty_p (temp_hard_regset)
978               && hard_reg_set_empty_p (temp_set2))
979             {
980               for (i = 0;; i++)
981                 {
982                   cl3 = reg_class_subclasses[cl1][i];
983                   if (cl3 == LIM_REG_CLASSES)
984                     break;
985                   if (reg_class_subset_p (ira_reg_class_intersect[cl1][cl2],
986                                           (enum reg_class) cl3))
987                     ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
988                 }
989               ira_reg_class_union[cl1][cl2] = reg_class_subunion[cl1][cl2];
990               continue;
991             }
992           ira_reg_classes_intersect_p[cl1][cl2]
993             = hard_reg_set_intersect_p (temp_hard_regset, temp_set2);
994           if (important_class_p[cl1] && important_class_p[cl2]
995               && hard_reg_set_subset_p (temp_hard_regset, temp_set2))
996             {
997               enum reg_class *p;
998
999               p = &ira_reg_class_super_classes[cl1][0];
1000               while (*p != LIM_REG_CLASSES)
1001                 p++;
1002               *p++ = (enum reg_class) cl2;
1003               *p = LIM_REG_CLASSES;
1004             }
1005           ira_reg_class_union[cl1][cl2] = NO_REGS;
1006           COPY_HARD_REG_SET (intersection_set, reg_class_contents[cl1]);
1007           AND_HARD_REG_SET (intersection_set, reg_class_contents[cl2]);
1008           AND_COMPL_HARD_REG_SET (intersection_set, no_unit_alloc_regs);
1009           COPY_HARD_REG_SET (union_set, reg_class_contents[cl1]);
1010           IOR_HARD_REG_SET (union_set, reg_class_contents[cl2]);
1011           AND_COMPL_HARD_REG_SET (union_set, no_unit_alloc_regs);
1012           for (i = 0; i < ira_important_classes_num; i++)
1013             {
1014               cl3 = ira_important_classes[i];
1015               COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl3]);
1016               AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1017               if (hard_reg_set_subset_p (temp_hard_regset, intersection_set))
1018                 {
1019                   COPY_HARD_REG_SET
1020                     (temp_set2,
1021                      reg_class_contents[(int)
1022                                         ira_reg_class_intersect[cl1][cl2]]);
1023                   AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1024                   if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1025                       /* Ignore unavailable hard registers and prefer
1026                          smallest class for debugging purposes.  */
1027                       || (hard_reg_set_equal_p (temp_hard_regset, temp_set2)
1028                           && hard_reg_set_subset_p
1029                              (reg_class_contents[cl3],
1030                               reg_class_contents
1031                               [(int) ira_reg_class_intersect[cl1][cl2]])))
1032                     ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1033                 }
1034               if (hard_reg_set_subset_p (temp_hard_regset, union_set))
1035                 {
1036                   COPY_HARD_REG_SET
1037                     (temp_set2,
1038                      reg_class_contents[(int) ira_reg_class_union[cl1][cl2]]);
1039                   AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1040                   if (ira_reg_class_union[cl1][cl2] == NO_REGS
1041                       || (hard_reg_set_subset_p (temp_set2, temp_hard_regset)
1042                       
1043                           && (! hard_reg_set_equal_p (temp_set2,
1044                                                       temp_hard_regset)
1045                               /* Ignore unavailable hard registers and
1046                                  prefer smallest class for debugging
1047                                  purposes.  */
1048                               || hard_reg_set_subset_p
1049                                  (reg_class_contents[cl3],
1050                                   reg_class_contents
1051                                   [(int) ira_reg_class_union[cl1][cl2]]))))
1052                     ira_reg_class_union[cl1][cl2] = (enum reg_class) cl3;
1053                 }
1054             }
1055         }
1056     }
1057 }
1058
1059 /* Output all cover classes and the translation map into file F.  */
1060 static void
1061 print_class_cover (FILE *f)
1062 {
1063   static const char *const reg_class_names[] = REG_CLASS_NAMES;
1064   int i;
1065
1066   fprintf (f, "Class cover:\n");
1067   for (i = 0; i < ira_reg_class_cover_size; i++)
1068     fprintf (f, " %s", reg_class_names[ira_reg_class_cover[i]]);
1069   fprintf (f, "\nClass translation:\n");
1070   for (i = 0; i < N_REG_CLASSES; i++)
1071     fprintf (f, " %s -> %s\n", reg_class_names[i],
1072              reg_class_names[ira_class_translate[i]]);
1073 }
1074
1075 /* Output all cover classes and the translation map into
1076    stderr.  */
1077 void
1078 ira_debug_class_cover (void)
1079 {
1080   print_class_cover (stderr);
1081 }
1082
1083 /* Set up different arrays concerning class subsets, cover and
1084    important classes.  */
1085 static void
1086 find_reg_class_closure (void)
1087 {
1088   setup_reg_subclasses ();
1089   setup_cover_and_important_classes ();
1090   setup_class_translate ();
1091   setup_reg_class_relations ();
1092 }
1093
1094 \f
1095
1096 /* Map: hard register number -> cover class it belongs to.  If the
1097    corresponding class is NO_REGS, the hard register is not available
1098    for allocation.  */
1099 enum reg_class ira_hard_regno_cover_class[FIRST_PSEUDO_REGISTER];
1100
1101 /* Set up the array above.  */
1102 static void
1103 setup_hard_regno_cover_class (void)
1104 {
1105   int i, j;
1106   enum reg_class cl;
1107
1108   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1109     {
1110       ira_hard_regno_cover_class[i] = NO_REGS;
1111       for (j = 0; j < ira_reg_class_cover_size; j++)
1112         {
1113           cl = ira_reg_class_cover[j];
1114           if (ira_class_hard_reg_index[cl][i] >= 0)
1115             {
1116               ira_hard_regno_cover_class[i] = cl;
1117               break;
1118             }
1119         }
1120             
1121     }
1122 }
1123
1124 \f
1125
1126 /* Map: register class x machine mode -> number of hard registers of
1127    given class needed to store value of given mode.  If the number is
1128    different, the size will be negative.  */
1129 int ira_reg_class_nregs[N_REG_CLASSES][MAX_MACHINE_MODE];
1130
1131 /* Maximal value of the previous array elements.  */
1132 int ira_max_nregs;
1133
1134 /* Form IRA_REG_CLASS_NREGS map.  */
1135 static void
1136 setup_reg_class_nregs (void)
1137 {
1138   int cl, m;
1139
1140   ira_max_nregs = -1;
1141   for (cl = 0; cl < N_REG_CLASSES; cl++)
1142     for (m = 0; m < MAX_MACHINE_MODE; m++)
1143       {
1144         ira_reg_class_nregs[cl][m] = CLASS_MAX_NREGS ((enum reg_class) cl,
1145                                                       (enum machine_mode) m);
1146         if (ira_max_nregs < ira_reg_class_nregs[cl][m])
1147           ira_max_nregs = ira_reg_class_nregs[cl][m];
1148       }
1149 }
1150
1151 \f
1152
1153 /* Array whose values are hard regset of hard registers available for
1154    the allocation of given register class whose HARD_REGNO_MODE_OK
1155    values for given mode are zero.  */
1156 HARD_REG_SET prohibited_class_mode_regs[N_REG_CLASSES][NUM_MACHINE_MODES];
1157
1158 /* Set up PROHIBITED_CLASS_MODE_REGS.  */
1159 static void
1160 setup_prohibited_class_mode_regs (void)
1161 {
1162   int i, j, k, hard_regno;
1163   enum reg_class cl;
1164
1165   for (i = 0; i < ira_reg_class_cover_size; i++)
1166     {
1167       cl = ira_reg_class_cover[i];
1168       for (j = 0; j < NUM_MACHINE_MODES; j++)
1169         {
1170           CLEAR_HARD_REG_SET (prohibited_class_mode_regs[cl][j]);
1171           for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1172             {
1173               hard_regno = ira_class_hard_regs[cl][k];
1174               if (! HARD_REGNO_MODE_OK (hard_regno, (enum machine_mode) j))
1175                 SET_HARD_REG_BIT (prohibited_class_mode_regs[cl][j],
1176                                   hard_regno);
1177             }
1178         }
1179     }
1180 }
1181
1182 \f
1183
1184 /* Allocate and initialize IRA_REGISTER_MOVE_COST,
1185    IRA_MAY_MOVE_IN_COST, and IRA_MAY_MOVE_OUT_COST for MODE if it is
1186    not done yet.  */
1187 void
1188 ira_init_register_move_cost (enum machine_mode mode)
1189 {
1190   int cl1, cl2;
1191
1192   ira_assert (ira_register_move_cost[mode] == NULL
1193               && ira_may_move_in_cost[mode] == NULL
1194               && ira_may_move_out_cost[mode] == NULL);
1195   if (move_cost[mode] == NULL)
1196     init_move_cost (mode);
1197   ira_register_move_cost[mode] = move_cost[mode];
1198   /* Don't use ira_allocate because the tables exist out of scope of a
1199      IRA call.  */
1200   ira_may_move_in_cost[mode]
1201     = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1202   memcpy (ira_may_move_in_cost[mode], may_move_in_cost[mode],
1203           sizeof (move_table) * N_REG_CLASSES);
1204   ira_may_move_out_cost[mode]
1205     = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1206   memcpy (ira_may_move_out_cost[mode], may_move_out_cost[mode],
1207           sizeof (move_table) * N_REG_CLASSES);
1208   for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1209     {
1210       for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1211         {
1212           if (ira_class_subset_p[cl1][cl2])
1213             ira_may_move_in_cost[mode][cl1][cl2] = 0;
1214           if (ira_class_subset_p[cl2][cl1])
1215             ira_may_move_out_cost[mode][cl1][cl2] = 0;
1216         }
1217     }
1218 }
1219
1220 \f
1221
1222 /* This is called once during compiler work.  It sets up
1223    different arrays whose values don't depend on the compiled
1224    function.  */
1225 void
1226 ira_init_once (void)
1227 {
1228   enum machine_mode mode;
1229
1230   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1231     {
1232       ira_register_move_cost[mode] = NULL;
1233       ira_may_move_in_cost[mode] = NULL;
1234       ira_may_move_out_cost[mode] = NULL;
1235     }
1236   ira_init_costs_once ();
1237 }
1238
1239 /* Free ira_register_move_cost, ira_may_move_in_cost, and
1240    ira_may_move_out_cost for each mode.  */
1241 static void
1242 free_register_move_costs (void)
1243 {
1244   enum machine_mode mode;
1245
1246   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1247     {
1248       if (ira_may_move_in_cost[mode] != NULL)
1249         free (ira_may_move_in_cost[mode]);
1250       if (ira_may_move_out_cost[mode] != NULL)
1251         free (ira_may_move_out_cost[mode]);
1252       ira_register_move_cost[mode] = NULL;
1253       ira_may_move_in_cost[mode] = NULL;
1254       ira_may_move_out_cost[mode] = NULL;
1255     }
1256 }
1257
1258 /* This is called every time when register related information is
1259    changed.  */
1260 void
1261 ira_init (void)
1262 {
1263   free_register_move_costs ();
1264   setup_reg_mode_hard_regset ();
1265   setup_alloc_regs (flag_omit_frame_pointer != 0);
1266   setup_class_subset_and_memory_move_costs ();
1267   find_reg_class_closure ();
1268   setup_hard_regno_cover_class ();
1269   setup_reg_class_nregs ();
1270   setup_prohibited_class_mode_regs ();
1271   ira_init_costs ();
1272 }
1273
1274 /* Function called once at the end of compiler work.  */
1275 void
1276 ira_finish_once (void)
1277 {
1278   ira_finish_costs_once ();
1279   free_register_move_costs ();
1280 }
1281
1282 \f
1283
1284 /* Array whose values are hard regset of hard registers for which
1285    move of the hard register in given mode into itself is
1286    prohibited.  */
1287 HARD_REG_SET ira_prohibited_mode_move_regs[NUM_MACHINE_MODES];
1288
1289 /* Flag of that the above array has been initialized.  */
1290 static bool ira_prohibited_mode_move_regs_initialized_p = false;
1291
1292 /* Set up IRA_PROHIBITED_MODE_MOVE_REGS.  */
1293 static void
1294 setup_prohibited_mode_move_regs (void)
1295 {
1296   int i, j;
1297   rtx test_reg1, test_reg2, move_pat, move_insn;
1298
1299   if (ira_prohibited_mode_move_regs_initialized_p)
1300     return;
1301   ira_prohibited_mode_move_regs_initialized_p = true;
1302   test_reg1 = gen_rtx_REG (VOIDmode, 0);
1303   test_reg2 = gen_rtx_REG (VOIDmode, 0);
1304   move_pat = gen_rtx_SET (VOIDmode, test_reg1, test_reg2);
1305   move_insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, 0, move_pat, -1, 0);
1306   for (i = 0; i < NUM_MACHINE_MODES; i++)
1307     {
1308       SET_HARD_REG_SET (ira_prohibited_mode_move_regs[i]);
1309       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1310         {
1311           if (! HARD_REGNO_MODE_OK (j, (enum machine_mode) i))
1312             continue;
1313           SET_REGNO (test_reg1, j);
1314           PUT_MODE (test_reg1, i);
1315           SET_REGNO (test_reg2, j);
1316           PUT_MODE (test_reg2, i);
1317           INSN_CODE (move_insn) = -1;
1318           recog_memoized (move_insn);
1319           if (INSN_CODE (move_insn) < 0)
1320             continue;
1321           extract_insn (move_insn);
1322           if (! constrain_operands (1))
1323             continue;
1324           CLEAR_HARD_REG_BIT (ira_prohibited_mode_move_regs[i], j);
1325         }
1326     }
1327 }
1328
1329 \f
1330
1331 /* Function specific hard registers that can not be used for the
1332    register allocation.  */
1333 HARD_REG_SET ira_no_alloc_regs;
1334
1335 /* Return TRUE if *LOC contains an asm.  */
1336 static int
1337 insn_contains_asm_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
1338 {
1339   if ( !*loc)
1340     return FALSE;
1341   if (GET_CODE (*loc) == ASM_OPERANDS)
1342     return TRUE;
1343   return FALSE;
1344 }
1345
1346
1347 /* Return TRUE if INSN contains an ASM.  */
1348 static bool
1349 insn_contains_asm (rtx insn)
1350 {
1351   return for_each_rtx (&insn, insn_contains_asm_1, NULL);
1352 }
1353
1354 /* Set up regs_asm_clobbered.  */
1355 static void
1356 compute_regs_asm_clobbered (char *regs_asm_clobbered)
1357 {
1358   basic_block bb;
1359
1360   memset (regs_asm_clobbered, 0, sizeof (char) * FIRST_PSEUDO_REGISTER);
1361   
1362   FOR_EACH_BB (bb)
1363     {
1364       rtx insn;
1365       FOR_BB_INSNS_REVERSE (bb, insn)
1366         {
1367           df_ref *def_rec;
1368
1369           if (insn_contains_asm (insn))
1370             for (def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++)
1371               {
1372                 df_ref def = *def_rec;
1373                 unsigned int dregno = DF_REF_REGNO (def);
1374                 if (dregno < FIRST_PSEUDO_REGISTER)
1375                   {
1376                     unsigned int i;
1377                     enum machine_mode mode = GET_MODE (DF_REF_REAL_REG (def));
1378                     unsigned int end = dregno 
1379                       + hard_regno_nregs[dregno][mode] - 1;
1380
1381                     for (i = dregno; i <= end; ++i)
1382                       regs_asm_clobbered[i] = 1;
1383                   }
1384               }
1385         }
1386     }
1387 }
1388
1389
1390 /* Set up ELIMINABLE_REGSET, IRA_NO_ALLOC_REGS, and REGS_EVER_LIVE.  */
1391 static void
1392 setup_eliminable_regset (void)
1393 {
1394   /* Like regs_ever_live, but 1 if a reg is set or clobbered from an
1395      asm.  Unlike regs_ever_live, elements of this array corresponding
1396      to eliminable regs (like the frame pointer) are set if an asm
1397      sets them.  */
1398   char *regs_asm_clobbered
1399     = (char *) alloca (FIRST_PSEUDO_REGISTER * sizeof (char));
1400 #ifdef ELIMINABLE_REGS
1401   int i;
1402   static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
1403 #endif
1404   /* FIXME: If EXIT_IGNORE_STACK is set, we will not save and restore
1405      sp for alloca.  So we can't eliminate the frame pointer in that
1406      case.  At some point, we should improve this by emitting the
1407      sp-adjusting insns for this case.  */
1408   int need_fp
1409     = (! flag_omit_frame_pointer
1410        || (cfun->calls_alloca && EXIT_IGNORE_STACK)
1411        || crtl->accesses_prior_frames
1412        || crtl->stack_realign_needed
1413        || FRAME_POINTER_REQUIRED);
1414
1415   frame_pointer_needed = need_fp;
1416
1417   COPY_HARD_REG_SET (ira_no_alloc_regs, no_unit_alloc_regs);
1418   CLEAR_HARD_REG_SET (eliminable_regset);
1419
1420   compute_regs_asm_clobbered (regs_asm_clobbered);
1421   /* Build the regset of all eliminable registers and show we can't
1422      use those that we already know won't be eliminated.  */
1423 #ifdef ELIMINABLE_REGS
1424   for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
1425     {
1426       bool cannot_elim
1427         = (! CAN_ELIMINATE (eliminables[i].from, eliminables[i].to)
1428            || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp));
1429
1430       if (! regs_asm_clobbered[eliminables[i].from])
1431         {
1432             SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
1433
1434             if (cannot_elim)
1435               SET_HARD_REG_BIT (ira_no_alloc_regs, eliminables[i].from);
1436         }
1437       else if (cannot_elim)
1438         error ("%s cannot be used in asm here",
1439                reg_names[eliminables[i].from]);
1440       else
1441         df_set_regs_ever_live (eliminables[i].from, true);
1442     }
1443 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
1444   if (! regs_asm_clobbered[HARD_FRAME_POINTER_REGNUM])
1445     {
1446       SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
1447       if (need_fp)
1448         SET_HARD_REG_BIT (ira_no_alloc_regs, HARD_FRAME_POINTER_REGNUM);
1449     }
1450   else if (need_fp)
1451     error ("%s cannot be used in asm here",
1452            reg_names[HARD_FRAME_POINTER_REGNUM]);
1453   else
1454     df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
1455 #endif
1456
1457 #else
1458   if (! regs_asm_clobbered[FRAME_POINTER_REGNUM])
1459     {
1460       SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
1461       if (need_fp)
1462         SET_HARD_REG_BIT (ira_no_alloc_regs, FRAME_POINTER_REGNUM);
1463     }
1464   else if (need_fp)
1465     error ("%s cannot be used in asm here", reg_names[FRAME_POINTER_REGNUM]);
1466   else
1467     df_set_regs_ever_live (FRAME_POINTER_REGNUM, true);
1468 #endif
1469 }
1470
1471 \f
1472
1473 /* The length of the following two arrays.  */
1474 int ira_reg_equiv_len;
1475
1476 /* The element value is TRUE if the corresponding regno value is
1477    invariant.  */
1478 bool *ira_reg_equiv_invariant_p;
1479
1480 /* The element value is equiv constant of given pseudo-register or
1481    NULL_RTX.  */
1482 rtx *ira_reg_equiv_const;
1483
1484 /* Set up the two arrays declared above.  */
1485 static void
1486 find_reg_equiv_invariant_const (void)
1487 {
1488   int i;
1489   bool invariant_p;
1490   rtx list, insn, note, constant, x;
1491
1492   for (i = FIRST_PSEUDO_REGISTER; i < reg_equiv_init_size; i++)
1493     {
1494       constant = NULL_RTX;
1495       invariant_p = false;
1496       for (list = reg_equiv_init[i]; list != NULL_RTX; list = XEXP (list, 1))
1497         {
1498           insn = XEXP (list, 0);
1499           note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
1500           
1501           if (note == NULL_RTX)
1502             continue;
1503
1504           x = XEXP (note, 0);
1505           
1506           if (! function_invariant_p (x)
1507               || ! flag_pic
1508               /* A function invariant is often CONSTANT_P but may
1509                  include a register.  We promise to only pass CONSTANT_P
1510                  objects to LEGITIMATE_PIC_OPERAND_P.  */
1511               || (CONSTANT_P (x) && LEGITIMATE_PIC_OPERAND_P (x)))
1512             {
1513               /* It can happen that a REG_EQUIV note contains a MEM
1514                  that is not a legitimate memory operand.  As later
1515                  stages of the reload assume that all addresses found
1516                  in the reg_equiv_* arrays were originally legitimate,
1517                  we ignore such REG_EQUIV notes.  */
1518               if (memory_operand (x, VOIDmode))
1519                 invariant_p = MEM_READONLY_P (x);
1520               else if (function_invariant_p (x))
1521                 {
1522                   if (GET_CODE (x) == PLUS
1523                       || x == frame_pointer_rtx || x == arg_pointer_rtx)
1524                     invariant_p = true;
1525                   else
1526                     constant = x;
1527                 }
1528             }
1529         }
1530       ira_reg_equiv_invariant_p[i] = invariant_p;
1531       ira_reg_equiv_const[i] = constant;
1532     }
1533 }
1534
1535 \f
1536
1537 /* Vector of substitutions of register numbers,
1538    used to map pseudo regs into hardware regs.
1539    This is set up as a result of register allocation.
1540    Element N is the hard reg assigned to pseudo reg N,
1541    or is -1 if no hard reg was assigned.
1542    If N is a hard reg number, element N is N.  */
1543 short *reg_renumber;
1544
1545 /* Set up REG_RENUMBER and CALLER_SAVE_NEEDED (used by reload) from
1546    the allocation found by IRA.  */
1547 static void
1548 setup_reg_renumber (void)
1549 {
1550   int regno, hard_regno;
1551   ira_allocno_t a;
1552   ira_allocno_iterator ai;
1553
1554   caller_save_needed = 0;
1555   FOR_EACH_ALLOCNO (a, ai)
1556     {
1557       /* There are no caps at this point.  */
1558       ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
1559       if (! ALLOCNO_ASSIGNED_P (a))
1560         /* It can happen if A is not referenced but partially anticipated
1561            somewhere in a region.  */
1562         ALLOCNO_ASSIGNED_P (a) = true;
1563       ira_free_allocno_updated_costs (a);
1564       hard_regno = ALLOCNO_HARD_REGNO (a);
1565       regno = (int) REGNO (ALLOCNO_REG (a));
1566       reg_renumber[regno] = (hard_regno < 0 ? -1 : hard_regno);
1567       if (hard_regno >= 0 && ALLOCNO_CALLS_CROSSED_NUM (a) != 0
1568           && ! ira_hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
1569                                           call_used_reg_set))
1570         {
1571           ira_assert (!optimize || flag_caller_saves
1572                       || regno >= ira_reg_equiv_len
1573                       || ira_reg_equiv_const[regno]
1574                       || ira_reg_equiv_invariant_p[regno]);
1575           caller_save_needed = 1;
1576         }
1577     }
1578 }
1579
1580 /* Set up allocno assignment flags for further allocation
1581    improvements.  */
1582 static void
1583 setup_allocno_assignment_flags (void)
1584 {
1585   int hard_regno;
1586   ira_allocno_t a;
1587   ira_allocno_iterator ai;
1588
1589   FOR_EACH_ALLOCNO (a, ai)
1590     {
1591       if (! ALLOCNO_ASSIGNED_P (a))
1592         /* It can happen if A is not referenced but partially anticipated
1593            somewhere in a region.  */
1594         ira_free_allocno_updated_costs (a);
1595       hard_regno = ALLOCNO_HARD_REGNO (a);
1596       /* Don't assign hard registers to allocnos which are destination
1597          of removed store at the end of loop.  It has no sense to keep
1598          the same value in different hard registers.  It is also
1599          impossible to assign hard registers correctly to such
1600          allocnos because the cost info and info about intersected
1601          calls are incorrect for them.  */
1602       ALLOCNO_ASSIGNED_P (a) = (hard_regno >= 0
1603                                 || ALLOCNO_MEM_OPTIMIZED_DEST_P (a)
1604                                 || (ALLOCNO_MEMORY_COST (a)
1605                                     - ALLOCNO_COVER_CLASS_COST (a)) < 0);
1606       ira_assert (hard_regno < 0
1607                   || ! ira_hard_reg_not_in_set_p (hard_regno, ALLOCNO_MODE (a),
1608                                                   reg_class_contents
1609                                                   [ALLOCNO_COVER_CLASS (a)]));
1610     }
1611 }
1612
1613 /* Evaluate overall allocation cost and the costs for using hard
1614    registers and memory for allocnos.  */
1615 static void
1616 calculate_allocation_cost (void)
1617 {
1618   int hard_regno, cost;
1619   ira_allocno_t a;
1620   ira_allocno_iterator ai;
1621
1622   ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
1623   FOR_EACH_ALLOCNO (a, ai)
1624     {
1625       hard_regno = ALLOCNO_HARD_REGNO (a);
1626       ira_assert (hard_regno < 0
1627                   || ! ira_hard_reg_not_in_set_p
1628                        (hard_regno, ALLOCNO_MODE (a),
1629                         reg_class_contents[ALLOCNO_COVER_CLASS (a)])); 
1630       if (hard_regno < 0)
1631         {
1632           cost = ALLOCNO_MEMORY_COST (a);
1633           ira_mem_cost += cost;
1634         }
1635       else if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
1636         {
1637           cost = (ALLOCNO_HARD_REG_COSTS (a)
1638                   [ira_class_hard_reg_index
1639                    [ALLOCNO_COVER_CLASS (a)][hard_regno]]);
1640           ira_reg_cost += cost;
1641         }
1642       else
1643         {
1644           cost = ALLOCNO_COVER_CLASS_COST (a);
1645           ira_reg_cost += cost;
1646         }
1647       ira_overall_cost += cost;
1648     }
1649
1650   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
1651     {
1652       fprintf (ira_dump_file,
1653                "+++Costs: overall %d, reg %d, mem %d, ld %d, st %d, move %d\n",
1654                ira_overall_cost, ira_reg_cost, ira_mem_cost,
1655                ira_load_cost, ira_store_cost, ira_shuffle_cost);
1656       fprintf (ira_dump_file, "+++       move loops %d, new jumps %d\n",
1657                ira_move_loops_num, ira_additional_jumps_num);
1658     }
1659
1660 }
1661
1662 #ifdef ENABLE_IRA_CHECKING
1663 /* Check the correctness of the allocation.  We do need this because
1664    of complicated code to transform more one region internal
1665    representation into one region representation.  */
1666 static void
1667 check_allocation (void)
1668 {
1669   ira_allocno_t a, conflict_a;
1670   int hard_regno, conflict_hard_regno, nregs, conflict_nregs;
1671   ira_allocno_conflict_iterator aci;
1672   ira_allocno_iterator ai;
1673
1674   FOR_EACH_ALLOCNO (a, ai)
1675     {
1676       if (ALLOCNO_CAP_MEMBER (a) != NULL
1677           || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
1678         continue;
1679       nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
1680       FOR_EACH_ALLOCNO_CONFLICT (a, conflict_a, aci)
1681         if ((conflict_hard_regno = ALLOCNO_HARD_REGNO (conflict_a)) >= 0)
1682           {
1683             conflict_nregs
1684               = (hard_regno_nregs
1685                  [conflict_hard_regno][ALLOCNO_MODE (conflict_a)]);
1686             if ((conflict_hard_regno <= hard_regno
1687                  && hard_regno < conflict_hard_regno + conflict_nregs)
1688                 || (hard_regno <= conflict_hard_regno
1689                     && conflict_hard_regno < hard_regno + nregs))
1690               {
1691                 fprintf (stderr, "bad allocation for %d and %d\n",
1692                          ALLOCNO_REGNO (a), ALLOCNO_REGNO (conflict_a));
1693                 gcc_unreachable ();
1694               }
1695           }
1696     }
1697 }
1698 #endif
1699
1700 /* Fix values of array REG_EQUIV_INIT after live range splitting done
1701    by IRA.  */
1702 static void
1703 fix_reg_equiv_init (void)
1704 {
1705   int max_regno = max_reg_num ();
1706   int i, new_regno;
1707   rtx x, prev, next, insn, set;
1708   
1709   if (reg_equiv_init_size < max_regno)
1710     {
1711       reg_equiv_init
1712         = (rtx *) ggc_realloc (reg_equiv_init, max_regno * sizeof (rtx));
1713       while (reg_equiv_init_size < max_regno)
1714         reg_equiv_init[reg_equiv_init_size++] = NULL_RTX;
1715       for (i = FIRST_PSEUDO_REGISTER; i < reg_equiv_init_size; i++)
1716         for (prev = NULL_RTX, x = reg_equiv_init[i]; x != NULL_RTX; x = next)
1717           {
1718             next = XEXP (x, 1);
1719             insn = XEXP (x, 0);
1720             set = single_set (insn);
1721             ira_assert (set != NULL_RTX
1722                         && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))));
1723             if (REG_P (SET_DEST (set))
1724                 && ((int) REGNO (SET_DEST (set)) == i
1725                     || (int) ORIGINAL_REGNO (SET_DEST (set)) == i))
1726               new_regno = REGNO (SET_DEST (set));
1727             else if (REG_P (SET_SRC (set))
1728                      && ((int) REGNO (SET_SRC (set)) == i
1729                          || (int) ORIGINAL_REGNO (SET_SRC (set)) == i))
1730               new_regno = REGNO (SET_SRC (set));
1731             else
1732               gcc_unreachable ();
1733             if (new_regno == i)
1734               prev = x;
1735             else
1736               {
1737                 if (prev == NULL_RTX)
1738                   reg_equiv_init[i] = next;
1739                 else
1740                   XEXP (prev, 1) = next;
1741                 XEXP (x, 1) = reg_equiv_init[new_regno];
1742                 reg_equiv_init[new_regno] = x;
1743               }
1744           }
1745     }
1746 }
1747
1748 #ifdef ENABLE_IRA_CHECKING
1749 /* Print redundant memory-memory copies.  */
1750 static void
1751 print_redundant_copies (void)
1752 {
1753   int hard_regno;
1754   ira_allocno_t a;
1755   ira_copy_t cp, next_cp;
1756   ira_allocno_iterator ai;
1757   
1758   FOR_EACH_ALLOCNO (a, ai)
1759     {
1760       if (ALLOCNO_CAP_MEMBER (a) != NULL)
1761         /* It is a cap. */
1762         continue;
1763       hard_regno = ALLOCNO_HARD_REGNO (a);
1764       if (hard_regno >= 0)
1765         continue;
1766       for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1767         if (cp->first == a)
1768           next_cp = cp->next_first_allocno_copy;
1769         else
1770           {
1771             next_cp = cp->next_second_allocno_copy;
1772             if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL
1773                 && cp->insn != NULL_RTX
1774                 && ALLOCNO_HARD_REGNO (cp->first) == hard_regno)
1775               fprintf (ira_dump_file,
1776                        "        Redundant move from %d(freq %d):%d\n",
1777                        INSN_UID (cp->insn), cp->freq, hard_regno);
1778           }
1779     }
1780 }
1781 #endif
1782
1783 /* Setup preferred and alternative classes for new pseudo-registers
1784    created by IRA starting with START.  */
1785 static void
1786 setup_preferred_alternate_classes_for_new_pseudos (int start)
1787 {
1788   int i, old_regno;
1789   int max_regno = max_reg_num ();
1790
1791   for (i = start; i < max_regno; i++)
1792     {
1793       old_regno = ORIGINAL_REGNO (regno_reg_rtx[i]);
1794       ira_assert (i != old_regno); 
1795       setup_reg_classes (i, reg_preferred_class (old_regno),
1796                          reg_alternate_class (old_regno));
1797       if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
1798         fprintf (ira_dump_file,
1799                  "    New r%d: setting preferred %s, alternative %s\n",
1800                  i, reg_class_names[reg_preferred_class (old_regno)],
1801                  reg_class_names[reg_alternate_class (old_regno)]);
1802     }
1803 }
1804
1805 \f
1806
1807 /* Regional allocation can create new pseudo-registers.  This function
1808    expands some arrays for pseudo-registers.  */
1809 static void
1810 expand_reg_info (int old_size)
1811 {
1812   int i;
1813   int size = max_reg_num ();
1814
1815   resize_reg_info ();
1816   for (i = old_size; i < size; i++)
1817     {
1818       reg_renumber[i] = -1;
1819       setup_reg_classes (i, GENERAL_REGS, ALL_REGS);
1820     }
1821 }
1822
1823 /* Return TRUE if there is too high register pressure in the function.
1824    It is used to decide when stack slot sharing is worth to do.  */
1825 static bool
1826 too_high_register_pressure_p (void)
1827 {
1828   int i;
1829   enum reg_class cover_class;
1830   
1831   for (i = 0; i < ira_reg_class_cover_size; i++)
1832     {
1833       cover_class = ira_reg_class_cover[i];
1834       if (ira_loop_tree_root->reg_pressure[cover_class] > 10000)
1835         return true;
1836     }
1837   return false;
1838 }
1839
1840 \f
1841
1842 /* Indicate that hard register number FROM was eliminated and replaced with
1843    an offset from hard register number TO.  The status of hard registers live
1844    at the start of a basic block is updated by replacing a use of FROM with
1845    a use of TO.  */
1846
1847 void
1848 mark_elimination (int from, int to)
1849 {
1850   basic_block bb;
1851
1852   FOR_EACH_BB (bb)
1853     {
1854       /* We don't use LIVE info in IRA.  */
1855       regset r = DF_LR_IN (bb);
1856
1857       if (REGNO_REG_SET_P (r, from))
1858         {
1859           CLEAR_REGNO_REG_SET (r, from);
1860           SET_REGNO_REG_SET (r, to);
1861         }
1862     }
1863 }
1864
1865 \f
1866
1867 struct equivalence
1868 {
1869   /* Set when a REG_EQUIV note is found or created.  Use to
1870      keep track of what memory accesses might be created later,
1871      e.g. by reload.  */
1872   rtx replacement;
1873   rtx *src_p;
1874   /* The list of each instruction which initializes this register.  */
1875   rtx init_insns;
1876   /* Loop depth is used to recognize equivalences which appear
1877      to be present within the same loop (or in an inner loop).  */
1878   int loop_depth;
1879   /* Nonzero if this had a preexisting REG_EQUIV note.  */
1880   int is_arg_equivalence;
1881   /* Set when an attempt should be made to replace a register
1882      with the associated src_p entry.  */
1883   char replace;
1884 };
1885
1886 /* reg_equiv[N] (where N is a pseudo reg number) is the equivalence
1887    structure for that register.  */
1888 static struct equivalence *reg_equiv;
1889
1890 /* Used for communication between the following two functions: contains
1891    a MEM that we wish to ensure remains unchanged.  */
1892 static rtx equiv_mem;
1893
1894 /* Set nonzero if EQUIV_MEM is modified.  */
1895 static int equiv_mem_modified;
1896
1897 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
1898    Called via note_stores.  */
1899 static void
1900 validate_equiv_mem_from_store (rtx dest, const_rtx set ATTRIBUTE_UNUSED,
1901                                void *data ATTRIBUTE_UNUSED)
1902 {
1903   if ((REG_P (dest)
1904        && reg_overlap_mentioned_p (dest, equiv_mem))
1905       || (MEM_P (dest)
1906           && true_dependence (dest, VOIDmode, equiv_mem, rtx_varies_p)))
1907     equiv_mem_modified = 1;
1908 }
1909
1910 /* Verify that no store between START and the death of REG invalidates
1911    MEMREF.  MEMREF is invalidated by modifying a register used in MEMREF,
1912    by storing into an overlapping memory location, or with a non-const
1913    CALL_INSN.
1914
1915    Return 1 if MEMREF remains valid.  */
1916 static int
1917 validate_equiv_mem (rtx start, rtx reg, rtx memref)
1918 {
1919   rtx insn;
1920   rtx note;
1921
1922   equiv_mem = memref;
1923   equiv_mem_modified = 0;
1924
1925   /* If the memory reference has side effects or is volatile, it isn't a
1926      valid equivalence.  */
1927   if (side_effects_p (memref))
1928     return 0;
1929
1930   for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
1931     {
1932       if (! INSN_P (insn))
1933         continue;
1934
1935       if (find_reg_note (insn, REG_DEAD, reg))
1936         return 1;
1937
1938       if (CALL_P (insn) && ! MEM_READONLY_P (memref)
1939           && ! RTL_CONST_OR_PURE_CALL_P (insn))
1940         return 0;
1941
1942       note_stores (PATTERN (insn), validate_equiv_mem_from_store, NULL);
1943
1944       /* If a register mentioned in MEMREF is modified via an
1945          auto-increment, we lose the equivalence.  Do the same if one
1946          dies; although we could extend the life, it doesn't seem worth
1947          the trouble.  */
1948
1949       for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1950         if ((REG_NOTE_KIND (note) == REG_INC
1951              || REG_NOTE_KIND (note) == REG_DEAD)
1952             && REG_P (XEXP (note, 0))
1953             && reg_overlap_mentioned_p (XEXP (note, 0), memref))
1954           return 0;
1955     }
1956
1957   return 0;
1958 }
1959
1960 /* Returns zero if X is known to be invariant.  */
1961 static int
1962 equiv_init_varies_p (rtx x)
1963 {
1964   RTX_CODE code = GET_CODE (x);
1965   int i;
1966   const char *fmt;
1967
1968   switch (code)
1969     {
1970     case MEM:
1971       return !MEM_READONLY_P (x) || equiv_init_varies_p (XEXP (x, 0));
1972
1973     case CONST:
1974     case CONST_INT:
1975     case CONST_DOUBLE:
1976     case CONST_FIXED:
1977     case CONST_VECTOR:
1978     case SYMBOL_REF:
1979     case LABEL_REF:
1980       return 0;
1981
1982     case REG:
1983       return reg_equiv[REGNO (x)].replace == 0 && rtx_varies_p (x, 0);
1984
1985     case ASM_OPERANDS:
1986       if (MEM_VOLATILE_P (x))
1987         return 1;
1988
1989       /* Fall through.  */
1990
1991     default:
1992       break;
1993     }
1994
1995   fmt = GET_RTX_FORMAT (code);
1996   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1997     if (fmt[i] == 'e')
1998       {
1999         if (equiv_init_varies_p (XEXP (x, i)))
2000           return 1;
2001       }
2002     else if (fmt[i] == 'E')
2003       {
2004         int j;
2005         for (j = 0; j < XVECLEN (x, i); j++)
2006           if (equiv_init_varies_p (XVECEXP (x, i, j)))
2007             return 1;
2008       }
2009
2010   return 0;
2011 }
2012
2013 /* Returns nonzero if X (used to initialize register REGNO) is movable.
2014    X is only movable if the registers it uses have equivalent initializations
2015    which appear to be within the same loop (or in an inner loop) and movable
2016    or if they are not candidates for local_alloc and don't vary.  */
2017 static int
2018 equiv_init_movable_p (rtx x, int regno)
2019 {
2020   int i, j;
2021   const char *fmt;
2022   enum rtx_code code = GET_CODE (x);
2023
2024   switch (code)
2025     {
2026     case SET:
2027       return equiv_init_movable_p (SET_SRC (x), regno);
2028
2029     case CC0:
2030     case CLOBBER:
2031       return 0;
2032
2033     case PRE_INC:
2034     case PRE_DEC:
2035     case POST_INC:
2036     case POST_DEC:
2037     case PRE_MODIFY:
2038     case POST_MODIFY:
2039       return 0;
2040
2041     case REG:
2042       return (reg_equiv[REGNO (x)].loop_depth >= reg_equiv[regno].loop_depth
2043               && reg_equiv[REGNO (x)].replace)
2044              || (REG_BASIC_BLOCK (REGNO (x)) < NUM_FIXED_BLOCKS && ! rtx_varies_p (x, 0));
2045
2046     case UNSPEC_VOLATILE:
2047       return 0;
2048
2049     case ASM_OPERANDS:
2050       if (MEM_VOLATILE_P (x))
2051         return 0;
2052
2053       /* Fall through.  */
2054
2055     default:
2056       break;
2057     }
2058
2059   fmt = GET_RTX_FORMAT (code);
2060   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2061     switch (fmt[i])
2062       {
2063       case 'e':
2064         if (! equiv_init_movable_p (XEXP (x, i), regno))
2065           return 0;
2066         break;
2067       case 'E':
2068         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2069           if (! equiv_init_movable_p (XVECEXP (x, i, j), regno))
2070             return 0;
2071         break;
2072       }
2073
2074   return 1;
2075 }
2076
2077 /* TRUE if X uses any registers for which reg_equiv[REGNO].replace is true.  */
2078 static int
2079 contains_replace_regs (rtx x)
2080 {
2081   int i, j;
2082   const char *fmt;
2083   enum rtx_code code = GET_CODE (x);
2084
2085   switch (code)
2086     {
2087     case CONST_INT:
2088     case CONST:
2089     case LABEL_REF:
2090     case SYMBOL_REF:
2091     case CONST_DOUBLE:
2092     case CONST_FIXED:
2093     case CONST_VECTOR:
2094     case PC:
2095     case CC0:
2096     case HIGH:
2097       return 0;
2098
2099     case REG:
2100       return reg_equiv[REGNO (x)].replace;
2101
2102     default:
2103       break;
2104     }
2105
2106   fmt = GET_RTX_FORMAT (code);
2107   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2108     switch (fmt[i])
2109       {
2110       case 'e':
2111         if (contains_replace_regs (XEXP (x, i)))
2112           return 1;
2113         break;
2114       case 'E':
2115         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2116           if (contains_replace_regs (XVECEXP (x, i, j)))
2117             return 1;
2118         break;
2119       }
2120
2121   return 0;
2122 }
2123
2124 /* TRUE if X references a memory location that would be affected by a store
2125    to MEMREF.  */
2126 static int
2127 memref_referenced_p (rtx memref, rtx x)
2128 {
2129   int i, j;
2130   const char *fmt;
2131   enum rtx_code code = GET_CODE (x);
2132
2133   switch (code)
2134     {
2135     case CONST_INT:
2136     case CONST:
2137     case LABEL_REF:
2138     case SYMBOL_REF:
2139     case CONST_DOUBLE:
2140     case CONST_FIXED:
2141     case CONST_VECTOR:
2142     case PC:
2143     case CC0:
2144     case HIGH:
2145     case LO_SUM:
2146       return 0;
2147
2148     case REG:
2149       return (reg_equiv[REGNO (x)].replacement
2150               && memref_referenced_p (memref,
2151                                       reg_equiv[REGNO (x)].replacement));
2152
2153     case MEM:
2154       if (true_dependence (memref, VOIDmode, x, rtx_varies_p))
2155         return 1;
2156       break;
2157
2158     case SET:
2159       /* If we are setting a MEM, it doesn't count (its address does), but any
2160          other SET_DEST that has a MEM in it is referencing the MEM.  */
2161       if (MEM_P (SET_DEST (x)))
2162         {
2163           if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
2164             return 1;
2165         }
2166       else if (memref_referenced_p (memref, SET_DEST (x)))
2167         return 1;
2168
2169       return memref_referenced_p (memref, SET_SRC (x));
2170
2171     default:
2172       break;
2173     }
2174
2175   fmt = GET_RTX_FORMAT (code);
2176   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2177     switch (fmt[i])
2178       {
2179       case 'e':
2180         if (memref_referenced_p (memref, XEXP (x, i)))
2181           return 1;
2182         break;
2183       case 'E':
2184         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2185           if (memref_referenced_p (memref, XVECEXP (x, i, j)))
2186             return 1;
2187         break;
2188       }
2189
2190   return 0;
2191 }
2192
2193 /* TRUE if some insn in the range (START, END] references a memory location
2194    that would be affected by a store to MEMREF.  */
2195 static int
2196 memref_used_between_p (rtx memref, rtx start, rtx end)
2197 {
2198   rtx insn;
2199
2200   for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2201        insn = NEXT_INSN (insn))
2202     {
2203       if (!INSN_P (insn))
2204         continue;
2205       
2206       if (memref_referenced_p (memref, PATTERN (insn)))
2207         return 1;
2208
2209       /* Nonconst functions may access memory.  */
2210       if (CALL_P (insn) && (! RTL_CONST_CALL_P (insn)))
2211         return 1;
2212     }
2213
2214   return 0;
2215 }
2216
2217 /* Mark REG as having no known equivalence.
2218    Some instructions might have been processed before and furnished
2219    with REG_EQUIV notes for this register; these notes will have to be
2220    removed.
2221    STORE is the piece of RTL that does the non-constant / conflicting
2222    assignment - a SET, CLOBBER or REG_INC note.  It is currently not used,
2223    but needs to be there because this function is called from note_stores.  */
2224 static void
2225 no_equiv (rtx reg, const_rtx store ATTRIBUTE_UNUSED, void *data ATTRIBUTE_UNUSED)
2226 {
2227   int regno;
2228   rtx list;
2229
2230   if (!REG_P (reg))
2231     return;
2232   regno = REGNO (reg);
2233   list = reg_equiv[regno].init_insns;
2234   if (list == const0_rtx)
2235     return;
2236   reg_equiv[regno].init_insns = const0_rtx;
2237   reg_equiv[regno].replacement = NULL_RTX;
2238   /* This doesn't matter for equivalences made for argument registers, we
2239      should keep their initialization insns.  */
2240   if (reg_equiv[regno].is_arg_equivalence)
2241     return;
2242   reg_equiv_init[regno] = NULL_RTX;
2243   for (; list; list =  XEXP (list, 1))
2244     {
2245       rtx insn = XEXP (list, 0);
2246       remove_note (insn, find_reg_note (insn, REG_EQUIV, NULL_RTX));
2247     }
2248 }
2249
2250 /* Nonzero if we recorded an equivalence for a LABEL_REF.  */
2251 static int recorded_label_ref;
2252
2253 /* Find registers that are equivalent to a single value throughout the
2254    compilation (either because they can be referenced in memory or are set once
2255    from a single constant).  Lower their priority for a register.
2256
2257    If such a register is only referenced once, try substituting its value
2258    into the using insn.  If it succeeds, we can eliminate the register
2259    completely.
2260
2261    Initialize the REG_EQUIV_INIT array of initializing insns.
2262
2263    Return non-zero if jump label rebuilding should be done.  */
2264 static int
2265 update_equiv_regs (void)
2266 {
2267   rtx insn;
2268   basic_block bb;
2269   int loop_depth;
2270   bitmap cleared_regs;
2271   
2272   /* We need to keep track of whether or not we recorded a LABEL_REF so
2273      that we know if the jump optimizer needs to be rerun.  */
2274   recorded_label_ref = 0;
2275
2276   reg_equiv = XCNEWVEC (struct equivalence, max_regno);
2277   reg_equiv_init = GGC_CNEWVEC (rtx, max_regno);
2278   reg_equiv_init_size = max_regno;
2279
2280   init_alias_analysis ();
2281
2282   /* Scan the insns and find which registers have equivalences.  Do this
2283      in a separate scan of the insns because (due to -fcse-follow-jumps)
2284      a register can be set below its use.  */
2285   FOR_EACH_BB (bb)
2286     {
2287       loop_depth = bb->loop_depth;
2288
2289       for (insn = BB_HEAD (bb);
2290            insn != NEXT_INSN (BB_END (bb));
2291            insn = NEXT_INSN (insn))
2292         {
2293           rtx note;
2294           rtx set;
2295           rtx dest, src;
2296           int regno;
2297
2298           if (! INSN_P (insn))
2299             continue;
2300
2301           for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2302             if (REG_NOTE_KIND (note) == REG_INC)
2303               no_equiv (XEXP (note, 0), note, NULL);
2304
2305           set = single_set (insn);
2306
2307           /* If this insn contains more (or less) than a single SET,
2308              only mark all destinations as having no known equivalence.  */
2309           if (set == 0)
2310             {
2311               note_stores (PATTERN (insn), no_equiv, NULL);
2312               continue;
2313             }
2314           else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2315             {
2316               int i;
2317
2318               for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
2319                 {
2320                   rtx part = XVECEXP (PATTERN (insn), 0, i);
2321                   if (part != set)
2322                     note_stores (part, no_equiv, NULL);
2323                 }
2324             }
2325
2326           dest = SET_DEST (set);
2327           src = SET_SRC (set);
2328
2329           /* See if this is setting up the equivalence between an argument
2330              register and its stack slot.  */
2331           note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
2332           if (note)
2333             {
2334               gcc_assert (REG_P (dest));
2335               regno = REGNO (dest);
2336
2337               /* Note that we don't want to clear reg_equiv_init even if there
2338                  are multiple sets of this register.  */
2339               reg_equiv[regno].is_arg_equivalence = 1;
2340
2341               /* Record for reload that this is an equivalencing insn.  */
2342               if (rtx_equal_p (src, XEXP (note, 0)))
2343                 reg_equiv_init[regno]
2344                   = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[regno]);
2345
2346               /* Continue normally in case this is a candidate for
2347                  replacements.  */
2348             }
2349
2350           if (!optimize)
2351             continue;
2352
2353           /* We only handle the case of a pseudo register being set
2354              once, or always to the same value.  */
2355           /* ??? The mn10200 port breaks if we add equivalences for
2356              values that need an ADDRESS_REGS register and set them equivalent
2357              to a MEM of a pseudo.  The actual problem is in the over-conservative
2358              handling of INPADDR_ADDRESS / INPUT_ADDRESS / INPUT triples in
2359              calculate_needs, but we traditionally work around this problem
2360              here by rejecting equivalences when the destination is in a register
2361              that's likely spilled.  This is fragile, of course, since the
2362              preferred class of a pseudo depends on all instructions that set
2363              or use it.  */
2364
2365           if (!REG_P (dest)
2366               || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
2367               || reg_equiv[regno].init_insns == const0_rtx
2368               || (CLASS_LIKELY_SPILLED_P (reg_preferred_class (regno))
2369                   && MEM_P (src) && ! reg_equiv[regno].is_arg_equivalence))
2370             {
2371               /* This might be setting a SUBREG of a pseudo, a pseudo that is
2372                  also set somewhere else to a constant.  */
2373               note_stores (set, no_equiv, NULL);
2374               continue;
2375             }
2376
2377           note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
2378
2379           /* cse sometimes generates function invariants, but doesn't put a
2380              REG_EQUAL note on the insn.  Since this note would be redundant,
2381              there's no point creating it earlier than here.  */
2382           if (! note && ! rtx_varies_p (src, 0))
2383             note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
2384
2385           /* Don't bother considering a REG_EQUAL note containing an EXPR_LIST
2386              since it represents a function call */
2387           if (note && GET_CODE (XEXP (note, 0)) == EXPR_LIST)
2388             note = NULL_RTX;
2389
2390           if (DF_REG_DEF_COUNT (regno) != 1
2391               && (! note
2392                   || rtx_varies_p (XEXP (note, 0), 0)
2393                   || (reg_equiv[regno].replacement
2394                       && ! rtx_equal_p (XEXP (note, 0),
2395                                         reg_equiv[regno].replacement))))
2396             {
2397               no_equiv (dest, set, NULL);
2398               continue;
2399             }
2400           /* Record this insn as initializing this register.  */
2401           reg_equiv[regno].init_insns
2402             = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv[regno].init_insns);
2403
2404           /* If this register is known to be equal to a constant, record that
2405              it is always equivalent to the constant.  */
2406           if (DF_REG_DEF_COUNT (regno) == 1
2407               && note && ! rtx_varies_p (XEXP (note, 0), 0))
2408             {
2409               rtx note_value = XEXP (note, 0);
2410               remove_note (insn, note);
2411               set_unique_reg_note (insn, REG_EQUIV, note_value);
2412             }
2413
2414           /* If this insn introduces a "constant" register, decrease the priority
2415              of that register.  Record this insn if the register is only used once
2416              more and the equivalence value is the same as our source.
2417
2418              The latter condition is checked for two reasons:  First, it is an
2419              indication that it may be more efficient to actually emit the insn
2420              as written (if no registers are available, reload will substitute
2421              the equivalence).  Secondly, it avoids problems with any registers
2422              dying in this insn whose death notes would be missed.
2423
2424              If we don't have a REG_EQUIV note, see if this insn is loading
2425              a register used only in one basic block from a MEM.  If so, and the
2426              MEM remains unchanged for the life of the register, add a REG_EQUIV
2427              note.  */
2428
2429           note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
2430
2431           if (note == 0 && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
2432               && MEM_P (SET_SRC (set))
2433               && validate_equiv_mem (insn, dest, SET_SRC (set)))
2434             note = set_unique_reg_note (insn, REG_EQUIV, copy_rtx (SET_SRC (set)));
2435
2436           if (note)
2437             {
2438               int regno = REGNO (dest);
2439               rtx x = XEXP (note, 0);
2440
2441               /* If we haven't done so, record for reload that this is an
2442                  equivalencing insn.  */
2443               if (!reg_equiv[regno].is_arg_equivalence)
2444                 reg_equiv_init[regno]
2445                   = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init[regno]);
2446
2447               /* Record whether or not we created a REG_EQUIV note for a LABEL_REF.
2448                  We might end up substituting the LABEL_REF for uses of the
2449                  pseudo here or later.  That kind of transformation may turn an
2450                  indirect jump into a direct jump, in which case we must rerun the
2451                  jump optimizer to ensure that the JUMP_LABEL fields are valid.  */
2452               if (GET_CODE (x) == LABEL_REF
2453                   || (GET_CODE (x) == CONST
2454                       && GET_CODE (XEXP (x, 0)) == PLUS
2455                       && (GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF)))
2456                 recorded_label_ref = 1;
2457
2458               reg_equiv[regno].replacement = x;
2459               reg_equiv[regno].src_p = &SET_SRC (set);
2460               reg_equiv[regno].loop_depth = loop_depth;
2461
2462               /* Don't mess with things live during setjmp.  */
2463               if (REG_LIVE_LENGTH (regno) >= 0 && optimize)
2464                 {
2465                   /* Note that the statement below does not affect the priority
2466                      in local-alloc!  */
2467                   REG_LIVE_LENGTH (regno) *= 2;
2468
2469                   /* If the register is referenced exactly twice, meaning it is
2470                      set once and used once, indicate that the reference may be
2471                      replaced by the equivalence we computed above.  Do this
2472                      even if the register is only used in one block so that
2473                      dependencies can be handled where the last register is
2474                      used in a different block (i.e. HIGH / LO_SUM sequences)
2475                      and to reduce the number of registers alive across
2476                      calls.  */
2477
2478                   if (REG_N_REFS (regno) == 2
2479                       && (rtx_equal_p (x, src)
2480                           || ! equiv_init_varies_p (src))
2481                       && NONJUMP_INSN_P (insn)
2482                       && equiv_init_movable_p (PATTERN (insn), regno))
2483                     reg_equiv[regno].replace = 1;
2484                 }
2485             }
2486         }
2487     }
2488
2489   if (!optimize)
2490     goto out;
2491
2492   /* A second pass, to gather additional equivalences with memory.  This needs
2493      to be done after we know which registers we are going to replace.  */
2494
2495   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2496     {
2497       rtx set, src, dest;
2498       unsigned regno;
2499
2500       if (! INSN_P (insn))
2501         continue;
2502
2503       set = single_set (insn);
2504       if (! set)
2505         continue;
2506
2507       dest = SET_DEST (set);
2508       src = SET_SRC (set);
2509
2510       /* If this sets a MEM to the contents of a REG that is only used
2511          in a single basic block, see if the register is always equivalent
2512          to that memory location and if moving the store from INSN to the
2513          insn that set REG is safe.  If so, put a REG_EQUIV note on the
2514          initializing insn.
2515
2516          Don't add a REG_EQUIV note if the insn already has one.  The existing
2517          REG_EQUIV is likely more useful than the one we are adding.
2518
2519          If one of the regs in the address has reg_equiv[REGNO].replace set,
2520          then we can't add this REG_EQUIV note.  The reg_equiv[REGNO].replace
2521          optimization may move the set of this register immediately before
2522          insn, which puts it after reg_equiv[REGNO].init_insns, and hence
2523          the mention in the REG_EQUIV note would be to an uninitialized
2524          pseudo.  */
2525
2526       if (MEM_P (dest) && REG_P (src)
2527           && (regno = REGNO (src)) >= FIRST_PSEUDO_REGISTER
2528           && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
2529           && DF_REG_DEF_COUNT (regno) == 1
2530           && reg_equiv[regno].init_insns != 0
2531           && reg_equiv[regno].init_insns != const0_rtx
2532           && ! find_reg_note (XEXP (reg_equiv[regno].init_insns, 0),
2533                               REG_EQUIV, NULL_RTX)
2534           && ! contains_replace_regs (XEXP (dest, 0)))
2535         {
2536           rtx init_insn = XEXP (reg_equiv[regno].init_insns, 0);
2537           if (validate_equiv_mem (init_insn, src, dest)
2538               && ! memref_used_between_p (dest, init_insn, insn)
2539               /* Attaching a REG_EQUIV note will fail if INIT_INSN has
2540                  multiple sets.  */
2541               && set_unique_reg_note (init_insn, REG_EQUIV, copy_rtx (dest)))
2542             {
2543               /* This insn makes the equivalence, not the one initializing
2544                  the register.  */
2545               reg_equiv_init[regno]
2546                 = gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
2547               df_notes_rescan (init_insn);
2548             }
2549         }
2550     }
2551
2552   cleared_regs = BITMAP_ALLOC (NULL);
2553   /* Now scan all regs killed in an insn to see if any of them are
2554      registers only used that once.  If so, see if we can replace the
2555      reference with the equivalent form.  If we can, delete the
2556      initializing reference and this register will go away.  If we
2557      can't replace the reference, and the initializing reference is
2558      within the same loop (or in an inner loop), then move the register
2559      initialization just before the use, so that they are in the same
2560      basic block.  */
2561   FOR_EACH_BB_REVERSE (bb)
2562     {
2563       loop_depth = bb->loop_depth;
2564       for (insn = BB_END (bb);
2565            insn != PREV_INSN (BB_HEAD (bb));
2566            insn = PREV_INSN (insn))
2567         {
2568           rtx link;
2569
2570           if (! INSN_P (insn))
2571             continue;
2572
2573           /* Don't substitute into a non-local goto, this confuses CFG.  */
2574           if (JUMP_P (insn)
2575               && find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
2576             continue;
2577
2578           for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
2579             {
2580               if (REG_NOTE_KIND (link) == REG_DEAD
2581                   /* Make sure this insn still refers to the register.  */
2582                   && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
2583                 {
2584                   int regno = REGNO (XEXP (link, 0));
2585                   rtx equiv_insn;
2586
2587                   if (! reg_equiv[regno].replace
2588                       || reg_equiv[regno].loop_depth < loop_depth)
2589                     continue;
2590
2591                   /* reg_equiv[REGNO].replace gets set only when
2592                      REG_N_REFS[REGNO] is 2, i.e. the register is set
2593                      once and used once.  (If it were only set, but not used,
2594                      flow would have deleted the setting insns.)  Hence
2595                      there can only be one insn in reg_equiv[REGNO].init_insns.  */
2596                   gcc_assert (reg_equiv[regno].init_insns
2597                               && !XEXP (reg_equiv[regno].init_insns, 1));
2598                   equiv_insn = XEXP (reg_equiv[regno].init_insns, 0);
2599
2600                   /* We may not move instructions that can throw, since
2601                      that changes basic block boundaries and we are not
2602                      prepared to adjust the CFG to match.  */
2603                   if (can_throw_internal (equiv_insn))
2604                     continue;
2605
2606                   if (asm_noperands (PATTERN (equiv_insn)) < 0
2607                       && validate_replace_rtx (regno_reg_rtx[regno],
2608                                                *(reg_equiv[regno].src_p), insn))
2609                     {
2610                       rtx equiv_link;
2611                       rtx last_link;
2612                       rtx note;
2613
2614                       /* Find the last note.  */
2615                       for (last_link = link; XEXP (last_link, 1);
2616                            last_link = XEXP (last_link, 1))
2617                         ;
2618
2619                       /* Append the REG_DEAD notes from equiv_insn.  */
2620                       equiv_link = REG_NOTES (equiv_insn);
2621                       while (equiv_link)
2622                         {
2623                           note = equiv_link;
2624                           equiv_link = XEXP (equiv_link, 1);
2625                           if (REG_NOTE_KIND (note) == REG_DEAD)
2626                             {
2627                               remove_note (equiv_insn, note);
2628                               XEXP (last_link, 1) = note;
2629                               XEXP (note, 1) = NULL_RTX;
2630                               last_link = note;
2631                             }
2632                         }
2633
2634                       remove_death (regno, insn);
2635                       SET_REG_N_REFS (regno, 0);
2636                       REG_FREQ (regno) = 0;
2637                       delete_insn (equiv_insn);
2638
2639                       reg_equiv[regno].init_insns
2640                         = XEXP (reg_equiv[regno].init_insns, 1);
2641
2642                       reg_equiv_init[regno] = NULL_RTX;
2643                       bitmap_set_bit (cleared_regs, regno);
2644                     }
2645                   /* Move the initialization of the register to just before
2646                      INSN.  Update the flow information.  */
2647                   else if (PREV_INSN (insn) != equiv_insn)
2648                     {
2649                       rtx new_insn;
2650
2651                       new_insn = emit_insn_before (PATTERN (equiv_insn), insn);
2652                       REG_NOTES (new_insn) = REG_NOTES (equiv_insn);
2653                       REG_NOTES (equiv_insn) = 0;
2654                       /* Rescan it to process the notes.  */
2655                       df_insn_rescan (new_insn);
2656
2657                       /* Make sure this insn is recognized before
2658                          reload begins, otherwise
2659                          eliminate_regs_in_insn will die.  */
2660                       INSN_CODE (new_insn) = INSN_CODE (equiv_insn);
2661
2662                       delete_insn (equiv_insn);
2663
2664                       XEXP (reg_equiv[regno].init_insns, 0) = new_insn;
2665
2666                       REG_BASIC_BLOCK (regno) = bb->index;
2667                       REG_N_CALLS_CROSSED (regno) = 0;
2668                       REG_FREQ_CALLS_CROSSED (regno) = 0;
2669                       REG_N_THROWING_CALLS_CROSSED (regno) = 0;
2670                       REG_LIVE_LENGTH (regno) = 2;
2671
2672                       if (insn == BB_HEAD (bb))
2673                         BB_HEAD (bb) = PREV_INSN (insn);
2674
2675                       reg_equiv_init[regno]
2676                         = gen_rtx_INSN_LIST (VOIDmode, new_insn, NULL_RTX);
2677                       bitmap_set_bit (cleared_regs, regno);
2678                     }
2679                 }
2680             }
2681         }
2682     }
2683
2684   if (!bitmap_empty_p (cleared_regs))
2685     FOR_EACH_BB (bb)
2686       {
2687         bitmap_and_compl_into (DF_LIVE_IN (bb), cleared_regs);
2688         bitmap_and_compl_into (DF_LIVE_OUT (bb), cleared_regs);
2689         bitmap_and_compl_into (DF_LR_IN (bb), cleared_regs);
2690         bitmap_and_compl_into (DF_LR_OUT (bb), cleared_regs);
2691       }
2692
2693   BITMAP_FREE (cleared_regs);
2694
2695   out:
2696   /* Clean up.  */
2697
2698   end_alias_analysis ();
2699   free (reg_equiv);
2700   return recorded_label_ref;
2701 }
2702
2703 \f
2704
2705 /* Print chain C to FILE.  */
2706 static void
2707 print_insn_chain (FILE *file, struct insn_chain *c)
2708 {
2709   fprintf (file, "insn=%d, ", INSN_UID(c->insn));
2710   bitmap_print (file, &c->live_throughout, "live_throughout: ", ", ");
2711   bitmap_print (file, &c->dead_or_set, "dead_or_set: ", "\n");
2712 }
2713
2714
2715 /* Print all reload_insn_chains to FILE.  */
2716 static void
2717 print_insn_chains (FILE *file)
2718 {
2719   struct insn_chain *c;
2720   for (c = reload_insn_chain; c ; c = c->next)
2721     print_insn_chain (file, c);
2722 }
2723
2724 /* Return true if pseudo REGNO should be added to set live_throughout
2725    or dead_or_set of the insn chains for reload consideration.  */
2726 static bool
2727 pseudo_for_reload_consideration_p (int regno)
2728 {
2729   /* Consider spilled pseudos too for IRA because they still have a
2730      chance to get hard-registers in the reload when IRA is used.  */
2731   return (reg_renumber[regno] >= 0
2732           || (ira_conflicts_p && flag_ira_share_spill_slots));
2733 }
2734
2735 /* Init LIVE_SUBREGS[ALLOCNUM] and LIVE_SUBREGS_USED[ALLOCNUM] using
2736    REG to the number of nregs, and INIT_VALUE to get the
2737    initialization.  ALLOCNUM need not be the regno of REG.  */
2738 static void
2739 init_live_subregs (bool init_value, sbitmap *live_subregs,
2740                    int *live_subregs_used, int allocnum, rtx reg)
2741 {
2742   unsigned int regno = REGNO (SUBREG_REG (reg));
2743   int size = GET_MODE_SIZE (GET_MODE (regno_reg_rtx[regno]));
2744
2745   gcc_assert (size > 0);
2746
2747   /* Been there, done that.  */
2748   if (live_subregs_used[allocnum])
2749     return;
2750
2751   /* Create a new one with zeros.  */
2752   if (live_subregs[allocnum] == NULL)
2753     live_subregs[allocnum] = sbitmap_alloc (size);
2754
2755   /* If the entire reg was live before blasting into subregs, we need
2756      to init all of the subregs to ones else init to 0.  */
2757   if (init_value)
2758     sbitmap_ones (live_subregs[allocnum]);
2759   else 
2760     sbitmap_zero (live_subregs[allocnum]);
2761
2762   /* Set the number of bits that we really want.  */
2763   live_subregs_used[allocnum] = size;
2764 }
2765
2766 /* Walk the insns of the current function and build reload_insn_chain,
2767    and record register life information.  */
2768 static void
2769 build_insn_chain (void)
2770 {
2771   unsigned int i;
2772   struct insn_chain **p = &reload_insn_chain;
2773   basic_block bb;
2774   struct insn_chain *c = NULL;
2775   struct insn_chain *next = NULL;
2776   bitmap live_relevant_regs = BITMAP_ALLOC (NULL);
2777   bitmap elim_regset = BITMAP_ALLOC (NULL);
2778   /* live_subregs is a vector used to keep accurate information about
2779      which hardregs are live in multiword pseudos.  live_subregs and
2780      live_subregs_used are indexed by pseudo number.  The live_subreg
2781      entry for a particular pseudo is only used if the corresponding
2782      element is non zero in live_subregs_used.  The value in
2783      live_subregs_used is number of bytes that the pseudo can
2784      occupy.  */
2785   sbitmap *live_subregs = XCNEWVEC (sbitmap, max_regno);
2786   int *live_subregs_used = XNEWVEC (int, max_regno);
2787
2788   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2789     if (TEST_HARD_REG_BIT (eliminable_regset, i))
2790       bitmap_set_bit (elim_regset, i);
2791   FOR_EACH_BB_REVERSE (bb)
2792     {
2793       bitmap_iterator bi;
2794       rtx insn;
2795       
2796       CLEAR_REG_SET (live_relevant_regs);
2797       memset (live_subregs_used, 0, max_regno * sizeof (int));
2798       
2799       EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb), 0, i, bi)
2800         {
2801           if (i >= FIRST_PSEUDO_REGISTER)
2802             break;
2803           bitmap_set_bit (live_relevant_regs, i);
2804         }
2805
2806       EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb),
2807                                 FIRST_PSEUDO_REGISTER, i, bi)
2808         {
2809           if (pseudo_for_reload_consideration_p (i))
2810             bitmap_set_bit (live_relevant_regs, i);
2811         }
2812
2813       FOR_BB_INSNS_REVERSE (bb, insn)
2814         {
2815           if (!NOTE_P (insn) && !BARRIER_P (insn))
2816             {
2817               unsigned int uid = INSN_UID (insn);
2818               df_ref *def_rec;
2819               df_ref *use_rec;
2820
2821               c = new_insn_chain ();
2822               c->next = next;
2823               next = c;
2824               *p = c;
2825               p = &c->prev;
2826               
2827               c->insn = insn;
2828               c->block = bb->index;
2829
2830               if (INSN_P (insn))
2831                 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
2832                   {
2833                     df_ref def = *def_rec;
2834                     unsigned int regno = DF_REF_REGNO (def);
2835                     
2836                     /* Ignore may clobbers because these are generated
2837                        from calls. However, every other kind of def is
2838                        added to dead_or_set.  */
2839                     if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
2840                       {
2841                         if (regno < FIRST_PSEUDO_REGISTER)
2842                           {
2843                             if (!fixed_regs[regno])
2844                               bitmap_set_bit (&c->dead_or_set, regno);
2845                           }
2846                         else if (pseudo_for_reload_consideration_p (regno))
2847                           bitmap_set_bit (&c->dead_or_set, regno);
2848                       }
2849
2850                     if ((regno < FIRST_PSEUDO_REGISTER
2851                          || reg_renumber[regno] >= 0
2852                          || ira_conflicts_p)
2853                         && (!DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)))
2854                       {
2855                         rtx reg = DF_REF_REG (def);
2856
2857                         /* We can model subregs, but not if they are
2858                            wrapped in ZERO_EXTRACTS.  */
2859                         if (GET_CODE (reg) == SUBREG
2860                             && !DF_REF_FLAGS_IS_SET (def, DF_REF_ZERO_EXTRACT))
2861                           {
2862                             unsigned int start = SUBREG_BYTE (reg);
2863                             unsigned int last = start 
2864                               + GET_MODE_SIZE (GET_MODE (reg));
2865
2866                             init_live_subregs
2867                               (bitmap_bit_p (live_relevant_regs, regno), 
2868                                live_subregs, live_subregs_used, regno, reg);
2869
2870                             if (!DF_REF_FLAGS_IS_SET
2871                                 (def, DF_REF_STRICT_LOW_PART))
2872                               {
2873                                 /* Expand the range to cover entire words.
2874                                    Bytes added here are "don't care".  */
2875                                 start
2876                                   = start / UNITS_PER_WORD * UNITS_PER_WORD;
2877                                 last = ((last + UNITS_PER_WORD - 1)
2878                                         / UNITS_PER_WORD * UNITS_PER_WORD);
2879                               }
2880
2881                             /* Ignore the paradoxical bits.  */
2882                             if ((int)last > live_subregs_used[regno])
2883                               last = live_subregs_used[regno];
2884
2885                             while (start < last)
2886                               {
2887                                 RESET_BIT (live_subregs[regno], start);
2888                                 start++;
2889                               }
2890                             
2891                             if (sbitmap_empty_p (live_subregs[regno]))
2892                               {
2893                                 live_subregs_used[regno] = 0;
2894                                 bitmap_clear_bit (live_relevant_regs, regno);
2895                               }
2896                             else
2897                               /* Set live_relevant_regs here because
2898                                  that bit has to be true to get us to
2899                                  look at the live_subregs fields.  */
2900                               bitmap_set_bit (live_relevant_regs, regno);
2901                           }
2902                         else
2903                           {
2904                             /* DF_REF_PARTIAL is generated for
2905                                subregs, STRICT_LOW_PART, and
2906                                ZERO_EXTRACT.  We handle the subreg
2907                                case above so here we have to keep from
2908                                modeling the def as a killing def.  */
2909                             if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL))
2910                               {
2911                                 bitmap_clear_bit (live_relevant_regs, regno);
2912                                 live_subregs_used[regno] = 0;
2913                               }
2914                           }
2915                       }
2916                   }
2917           
2918               bitmap_and_compl_into (live_relevant_regs, elim_regset);
2919               bitmap_copy (&c->live_throughout, live_relevant_regs);
2920
2921               if (INSN_P (insn))
2922                 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
2923                   {
2924                     df_ref use = *use_rec;
2925                     unsigned int regno = DF_REF_REGNO (use);
2926                     rtx reg = DF_REF_REG (use);
2927                     
2928                     /* DF_REF_READ_WRITE on a use means that this use
2929                        is fabricated from a def that is a partial set
2930                        to a multiword reg.  Here, we only model the
2931                        subreg case that is not wrapped in ZERO_EXTRACT
2932                        precisely so we do not need to look at the
2933                        fabricated use. */
2934                     if (DF_REF_FLAGS_IS_SET (use, DF_REF_READ_WRITE) 
2935                         && !DF_REF_FLAGS_IS_SET (use, DF_REF_ZERO_EXTRACT) 
2936                         && DF_REF_FLAGS_IS_SET (use, DF_REF_SUBREG))
2937                       continue;
2938                     
2939                     /* Add the last use of each var to dead_or_set.  */
2940                     if (!bitmap_bit_p (live_relevant_regs, regno))
2941                       {
2942                         if (regno < FIRST_PSEUDO_REGISTER)
2943                           {
2944                             if (!fixed_regs[regno])
2945                               bitmap_set_bit (&c->dead_or_set, regno);
2946                           }
2947                         else if (pseudo_for_reload_consideration_p (regno))
2948                           bitmap_set_bit (&c->dead_or_set, regno);
2949                       }
2950                     
2951                     if (regno < FIRST_PSEUDO_REGISTER
2952                         || pseudo_for_reload_consideration_p (regno))
2953                       {
2954                         if (GET_CODE (reg) == SUBREG
2955                             && !DF_REF_FLAGS_IS_SET (use,
2956                                                      DF_REF_SIGN_EXTRACT
2957                                                      | DF_REF_ZERO_EXTRACT)) 
2958                           {
2959                             unsigned int start = SUBREG_BYTE (reg);
2960                             unsigned int last = start 
2961                               + GET_MODE_SIZE (GET_MODE (reg));
2962                             
2963                             init_live_subregs
2964                               (bitmap_bit_p (live_relevant_regs, regno), 
2965                                live_subregs, live_subregs_used, regno, reg);
2966                             
2967                             /* Ignore the paradoxical bits.  */
2968                             if ((int)last > live_subregs_used[regno])
2969                               last = live_subregs_used[regno];
2970
2971                             while (start < last)
2972                               {
2973                                 SET_BIT (live_subregs[regno], start);
2974                                 start++;
2975                               }
2976                           }
2977                         else
2978                           /* Resetting the live_subregs_used is
2979                              effectively saying do not use the subregs
2980                              because we are reading the whole
2981                              pseudo.  */
2982                           live_subregs_used[regno] = 0;
2983                         bitmap_set_bit (live_relevant_regs, regno);
2984                       }
2985                   }
2986             }
2987         }
2988
2989       /* FIXME!! The following code is a disaster.  Reload needs to see the
2990          labels and jump tables that are just hanging out in between
2991          the basic blocks.  See pr33676.  */
2992       insn = BB_HEAD (bb);
2993       
2994       /* Skip over the barriers and cruft.  */
2995       while (insn && (BARRIER_P (insn) || NOTE_P (insn) 
2996                       || BLOCK_FOR_INSN (insn) == bb))
2997         insn = PREV_INSN (insn);
2998       
2999       /* While we add anything except barriers and notes, the focus is
3000          to get the labels and jump tables into the
3001          reload_insn_chain.  */
3002       while (insn)
3003         {
3004           if (!NOTE_P (insn) && !BARRIER_P (insn))
3005             {
3006               if (BLOCK_FOR_INSN (insn))
3007                 break;
3008               
3009               c = new_insn_chain ();
3010               c->next = next;
3011               next = c;
3012               *p = c;
3013               p = &c->prev;
3014               
3015               /* The block makes no sense here, but it is what the old
3016                  code did.  */
3017               c->block = bb->index;
3018               c->insn = insn;
3019               bitmap_copy (&c->live_throughout, live_relevant_regs);
3020             }     
3021           insn = PREV_INSN (insn);
3022         }
3023     }
3024
3025   for (i = 0; i < (unsigned int) max_regno; i++)
3026     if (live_subregs[i])
3027       free (live_subregs[i]);
3028
3029   reload_insn_chain = c;
3030   *p = NULL;
3031
3032   free (live_subregs);
3033   free (live_subregs_used);
3034   BITMAP_FREE (live_relevant_regs);
3035   BITMAP_FREE (elim_regset);
3036
3037   if (dump_file)
3038     print_insn_chains (dump_file);
3039 }
3040
3041 \f
3042
3043 /* All natural loops.  */
3044 struct loops ira_loops;
3045
3046 /* True if we have allocno conflicts.  It is false for non-optimized
3047    mode or when the conflict table is too big.  */
3048 bool ira_conflicts_p;
3049
3050 /* This is the main entry of IRA.  */
3051 static void
3052 ira (FILE *f)
3053 {
3054   int overall_cost_before, allocated_reg_info_size;
3055   bool loops_p;
3056   int max_regno_before_ira, ira_max_point_before_emit;
3057   int rebuild_p;
3058   int saved_flag_ira_share_spill_slots;
3059   basic_block bb;
3060
3061   timevar_push (TV_IRA);
3062
3063   if (flag_ira_verbose < 10)
3064     {
3065       internal_flag_ira_verbose = flag_ira_verbose;
3066       ira_dump_file = f;
3067     }
3068   else
3069     {
3070       internal_flag_ira_verbose = flag_ira_verbose - 10;
3071       ira_dump_file = stderr;
3072     }
3073
3074   ira_conflicts_p = optimize > 0;
3075   setup_prohibited_mode_move_regs ();
3076
3077   df_note_add_problem ();
3078
3079   if (optimize == 1)
3080     {
3081       df_live_add_problem ();
3082       df_live_set_all_dirty ();
3083     }
3084 #ifdef ENABLE_CHECKING
3085   df->changeable_flags |= DF_VERIFY_SCHEDULED;
3086 #endif
3087   df_analyze ();
3088   df_clear_flags (DF_NO_INSN_RESCAN);
3089   regstat_init_n_sets_and_refs ();
3090   regstat_compute_ri ();
3091
3092   /* If we are not optimizing, then this is the only place before
3093      register allocation where dataflow is done.  And that is needed
3094      to generate these warnings.  */
3095   if (warn_clobbered)
3096     generate_setjmp_warnings ();
3097
3098   /* Determine if the current function is a leaf before running IRA
3099      since this can impact optimizations done by the prologue and
3100      epilogue thus changing register elimination offsets.  */
3101   current_function_is_leaf = leaf_function_p ();
3102
3103   rebuild_p = update_equiv_regs ();
3104
3105 #ifndef IRA_NO_OBSTACK
3106   gcc_obstack_init (&ira_obstack);
3107 #endif
3108   bitmap_obstack_initialize (&ira_bitmap_obstack);
3109   if (optimize)
3110     {      
3111       max_regno = max_reg_num ();
3112       ira_reg_equiv_len = max_regno;
3113       ira_reg_equiv_invariant_p
3114         = (bool *) ira_allocate (max_regno * sizeof (bool));
3115       memset (ira_reg_equiv_invariant_p, 0, max_regno * sizeof (bool));
3116       ira_reg_equiv_const = (rtx *) ira_allocate (max_regno * sizeof (rtx));
3117       memset (ira_reg_equiv_const, 0, max_regno * sizeof (rtx));
3118       find_reg_equiv_invariant_const ();
3119       if (rebuild_p)
3120         {
3121           timevar_push (TV_JUMP);
3122           rebuild_jump_labels (get_insns ());
3123           purge_all_dead_edges ();
3124           timevar_pop (TV_JUMP);
3125         }
3126     }
3127
3128   max_regno_before_ira = allocated_reg_info_size = max_reg_num ();
3129   allocate_reg_info ();
3130   setup_eliminable_regset ();
3131       
3132   ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
3133   ira_load_cost = ira_store_cost = ira_shuffle_cost = 0;
3134   ira_move_loops_num = ira_additional_jumps_num = 0;
3135   
3136   ira_assert (current_loops == NULL);
3137   flow_loops_find (&ira_loops);
3138   current_loops = &ira_loops;
3139       
3140   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3141     fprintf (ira_dump_file, "Building IRA IR\n");
3142   loops_p = ira_build (optimize
3143                        && (flag_ira_region == IRA_REGION_ALL
3144                            || flag_ira_region == IRA_REGION_MIXED));
3145   
3146   ira_assert (ira_conflicts_p || !loops_p);
3147
3148   saved_flag_ira_share_spill_slots = flag_ira_share_spill_slots;
3149   if (too_high_register_pressure_p ())
3150     /* It is just wasting compiler's time to pack spilled pseudos into
3151        stack slots in this case -- prohibit it.  */ 
3152     flag_ira_share_spill_slots = FALSE;
3153
3154   ira_color ();
3155       
3156   ira_max_point_before_emit = ira_max_point;
3157       
3158   ira_emit (loops_p);
3159   
3160   if (ira_conflicts_p)
3161     {
3162       max_regno = max_reg_num ();
3163       
3164       if (! loops_p)
3165         ira_initiate_assign ();
3166       else
3167         {
3168           expand_reg_info (allocated_reg_info_size);
3169           setup_preferred_alternate_classes_for_new_pseudos
3170             (allocated_reg_info_size);
3171           allocated_reg_info_size = max_regno;
3172           
3173           if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3174             fprintf (ira_dump_file, "Flattening IR\n");
3175           ira_flattening (max_regno_before_ira, ira_max_point_before_emit);
3176           /* New insns were generated: add notes and recalculate live
3177              info.  */
3178           df_analyze ();
3179           
3180           flow_loops_find (&ira_loops);
3181           current_loops = &ira_loops;
3182
3183           setup_allocno_assignment_flags ();
3184           ira_initiate_assign ();
3185           ira_reassign_conflict_allocnos (max_regno);
3186         }
3187     }
3188
3189   setup_reg_renumber ();
3190   
3191   calculate_allocation_cost ();
3192   
3193 #ifdef ENABLE_IRA_CHECKING
3194   if (ira_conflicts_p)
3195     check_allocation ();
3196 #endif
3197       
3198   delete_trivially_dead_insns (get_insns (), max_reg_num ());
3199   max_regno = max_reg_num ();
3200   
3201   /* And the reg_equiv_memory_loc array.  */
3202   VEC_safe_grow (rtx, gc, reg_equiv_memory_loc_vec, max_regno);
3203   memset (VEC_address (rtx, reg_equiv_memory_loc_vec), 0,
3204           sizeof (rtx) * max_regno);
3205   reg_equiv_memory_loc = VEC_address (rtx, reg_equiv_memory_loc_vec);
3206
3207   if (max_regno != max_regno_before_ira)
3208     {
3209       regstat_free_n_sets_and_refs ();
3210       regstat_free_ri ();
3211       regstat_init_n_sets_and_refs ();
3212       regstat_compute_ri ();
3213     }
3214
3215   allocate_initial_values (reg_equiv_memory_loc);
3216
3217   overall_cost_before = ira_overall_cost;
3218   if (ira_conflicts_p)
3219     {
3220       fix_reg_equiv_init ();
3221       
3222 #ifdef ENABLE_IRA_CHECKING
3223       print_redundant_copies ();
3224 #endif
3225
3226       ira_spilled_reg_stack_slots_num = 0;
3227       ira_spilled_reg_stack_slots
3228         = ((struct ira_spilled_reg_stack_slot *)
3229            ira_allocate (max_regno
3230                          * sizeof (struct ira_spilled_reg_stack_slot)));
3231       memset (ira_spilled_reg_stack_slots, 0,
3232               max_regno * sizeof (struct ira_spilled_reg_stack_slot));
3233     }
3234   
3235   timevar_pop (TV_IRA);
3236
3237   timevar_push (TV_RELOAD);
3238   df_set_flags (DF_NO_INSN_RESCAN);
3239   build_insn_chain ();
3240
3241   reload_completed = !reload (get_insns (), ira_conflicts_p);
3242
3243   timevar_pop (TV_RELOAD);
3244
3245   timevar_push (TV_IRA);
3246
3247   if (ira_conflicts_p)
3248     {
3249       ira_free (ira_spilled_reg_stack_slots);
3250       
3251       ira_finish_assign ();
3252       
3253     }  
3254   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL
3255       && overall_cost_before != ira_overall_cost)
3256     fprintf (ira_dump_file, "+++Overall after reload %d\n", ira_overall_cost);
3257   ira_destroy ();
3258   
3259   flag_ira_share_spill_slots = saved_flag_ira_share_spill_slots;
3260
3261   flow_loops_free (&ira_loops);
3262   free_dominance_info (CDI_DOMINATORS);
3263   FOR_ALL_BB (bb)
3264     bb->loop_father = NULL;
3265   current_loops = NULL;
3266
3267   regstat_free_ri ();
3268   regstat_free_n_sets_and_refs ();
3269       
3270   if (optimize)
3271     {
3272       cleanup_cfg (CLEANUP_EXPENSIVE);
3273       
3274       ira_free (ira_reg_equiv_invariant_p);
3275       ira_free (ira_reg_equiv_const);
3276     }
3277
3278   bitmap_obstack_release (&ira_bitmap_obstack);
3279 #ifndef IRA_NO_OBSTACK
3280   obstack_free (&ira_obstack, NULL);
3281 #endif
3282
3283   /* The code after the reload has changed so much that at this point
3284      we might as well just rescan everything.  Not that
3285      df_rescan_all_insns is not going to help here because it does not
3286      touch the artificial uses and defs.  */
3287   df_finish_pass (true);
3288   if (optimize > 1)
3289     df_live_add_problem ();
3290   df_scan_alloc (NULL);
3291   df_scan_blocks ();
3292
3293   if (optimize)
3294     df_analyze ();
3295
3296   timevar_pop (TV_IRA);
3297 }
3298
3299 \f
3300
3301 static bool
3302 gate_ira (void)
3303 {
3304   return true;
3305 }
3306
3307 /* Run the integrated register allocator.  */
3308 static unsigned int
3309 rest_of_handle_ira (void)
3310 {
3311   ira (dump_file);
3312   return 0;
3313 }
3314
3315 struct rtl_opt_pass pass_ira =
3316 {
3317  {
3318   RTL_PASS,
3319   "ira",                                /* name */
3320   gate_ira,                             /* gate */
3321   rest_of_handle_ira,                   /* execute */
3322   NULL,                                 /* sub */
3323   NULL,                                 /* next */
3324   0,                                    /* static_pass_number */
3325   TV_NONE,                              /* tv_id */
3326   0,                                    /* properties_required */
3327   0,                                    /* properties_provided */
3328   0,                                    /* properties_destroyed */
3329   0,                                    /* todo_flags_start */
3330   TODO_dump_func |
3331   TODO_ggc_collect                      /* todo_flags_finish */
3332  }
3333 };