OSDN Git Service

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