OSDN Git Service

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