OSDN Git Service

PR testsuite/50796
[pf3gnuchains/gcc-fork.git] / gcc / ira.c
1 /* Integrated Register Allocator (IRA) entry point.
2    Copyright (C) 2006, 2007, 2008, 2009, 2010, 2011
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 *Allocno class* is a register class used for allocation of
42        given allocno.  It means that only hard register of given
43        register class can be assigned to given allocno.  In reality,
44        even smaller subset of (*profitable*) hard registers can be
45        assigned.  In rare cases, the subset can be even smaller
46        because our modification of Chaitin-Briggs algorithm requires
47        that sets of hard registers can be assigned to allocnos forms a
48        forest, i.e. the sets can be ordered in a way where any
49        previous set is not intersected with given set or is a superset
50        of given set.
51
52      o *Pressure class* is a register class belonging to a set of
53        register classes containing all of the hard-registers available
54        for register allocation.  The set of all pressure classes for a
55        target is defined in the corresponding machine-description file
56        according some criteria.  Register pressure is calculated only
57        for pressure classes and it affects some IRA decisions as
58        forming allocation regions.
59
60      o *Allocno* represents the live range of a pseudo-register in a
61        region.  Besides the obvious attributes like the corresponding
62        pseudo-register number, allocno class, conflicting allocnos and
63        conflicting hard-registers, there are a few allocno attributes
64        which are important for understanding the allocation algorithm:
65
66        - *Live ranges*.  This is a list of ranges of *program points*
67          where the allocno lives.  Program points represent places
68          where a pseudo can be born or become dead (there are
69          approximately two times more program points than the insns)
70          and they are represented by integers starting with 0.  The
71          live ranges are used to find conflicts between allocnos.
72          They also play very important role for the transformation of
73          the IRA internal representation of several regions into a one
74          region representation.  The later is used during the reload
75          pass work because each allocno represents all of the
76          corresponding pseudo-registers.
77
78        - *Hard-register costs*.  This is a vector of size equal to the
79          number of available hard-registers of the allocno class.  The
80          cost of a callee-clobbered hard-register for an allocno is
81          increased by the cost of save/restore code around the calls
82          through the given allocno's life.  If the allocno is a move
83          instruction operand and another operand is a hard-register of
84          the allocno class, the cost of the hard-register is decreased
85          by the move cost.
86
87          When an allocno is assigned, the hard-register with minimal
88          full cost is used.  Initially, a hard-register's full cost is
89          the corresponding value from the hard-register's cost vector.
90          If the allocno is connected by a *copy* (see below) to
91          another allocno which has just received a hard-register, the
92          cost of the hard-register is decreased.  Before choosing a
93          hard-register for an allocno, the allocno's current costs of
94          the hard-registers are modified by the conflict hard-register
95          costs of all of the conflicting allocnos which are not
96          assigned yet.
97
98        - *Conflict hard-register costs*.  This is a vector of the same
99          size as the hard-register costs vector.  To permit an
100          unassigned allocno to get a better hard-register, IRA uses
101          this vector to calculate the final full cost of the
102          available hard-registers.  Conflict hard-register costs of an
103          unassigned allocno are also changed with a change of the
104          hard-register cost of the allocno when a copy involving the
105          allocno is processed as described above.  This is done to
106          show other unassigned allocnos that a given allocno prefers
107          some hard-registers in order to remove the move instruction
108          corresponding to the copy.
109
110      o *Cap*.  If a pseudo-register does not live in a region but
111        lives in a nested region, IRA creates a special allocno called
112        a cap in the outer region.  A region cap is also created for a
113        subregion cap.
114
115      o *Copy*.  Allocnos can be connected by copies.  Copies are used
116        to modify hard-register costs for allocnos during coloring.
117        Such modifications reflects a preference to use the same
118        hard-register for the allocnos connected by copies.  Usually
119        copies are created for move insns (in this case it results in
120        register coalescing).  But IRA also creates copies for operands
121        of an insn which should be assigned to the same hard-register
122        due to constraints in the machine description (it usually
123        results in removing a move generated in reload to satisfy
124        the constraints) and copies referring to the allocno which is
125        the output operand of an instruction and the allocno which is
126        an input operand dying in the instruction (creation of such
127        copies results in less register shuffling).  IRA *does not*
128        create copies between the same register allocnos from different
129        regions because we use another technique for propagating
130        hard-register preference on the borders of regions.
131
132    Allocnos (including caps) for the upper region in the region tree
133    *accumulate* information important for coloring from allocnos with
134    the same pseudo-register from nested regions.  This includes
135    hard-register and memory costs, conflicts with hard-registers,
136    allocno conflicts, allocno copies and more.  *Thus, attributes for
137    allocnos in a region have the same values as if the region had no
138    subregions*.  It means that attributes for allocnos in the
139    outermost region corresponding to the function have the same values
140    as though the allocation used only one region which is the entire
141    function.  It also means that we can look at IRA work as if the
142    first IRA did allocation for all function then it improved the
143    allocation for loops then their subloops and so on.
144
145    IRA major passes are:
146
147      o Building IRA internal representation which consists of the
148        following subpasses:
149
150        * First, IRA builds regions and creates allocnos (file
151          ira-build.c) and initializes most of their attributes.
152
153        * Then IRA finds an allocno class for each allocno and
154          calculates its initial (non-accumulated) cost of memory and
155          each hard-register of its allocno class (file ira-cost.c).
156
157        * IRA creates live ranges of each allocno, calulates register
158          pressure for each pressure class in each region, sets up
159          conflict hard registers for each allocno and info about calls
160          the allocno lives through (file ira-lives.c).
161
162        * IRA removes low register pressure loops from the regions
163          mostly to speed IRA up (file ira-build.c).
164
165        * IRA propagates accumulated allocno info from lower region
166          allocnos to corresponding upper region allocnos (file
167          ira-build.c).
168
169        * IRA creates all caps (file ira-build.c).
170
171        * Having live-ranges of allocnos and their classes, IRA creates
172          conflicting allocnos for each allocno.  Conflicting allocnos
173          are stored as a bit vector or array of pointers to the
174          conflicting allocnos whatever is more profitable (file
175          ira-conflicts.c).  At this point IRA creates allocno copies.
176
177      o Coloring.  Now IRA has all necessary info to start graph coloring
178        process.  It is done in each region on top-down traverse of the
179        region tree (file ira-color.c).  There are following subpasses:
180
181        * Finding profitable hard registers of corresponding allocno
182          class for each allocno.  For example, only callee-saved hard
183          registers are frequently profitable for allocnos living
184          through colors.  If the profitable hard register set of
185          allocno does not form a tree based on subset relation, we use
186          some approximation to form the tree.  This approximation is
187          used to figure out trivial colorability of allocnos.  The
188          approximation is a pretty rare case.
189
190        * Putting allocnos onto the coloring stack.  IRA uses Briggs
191          optimistic coloring which is a major improvement over
192          Chaitin's coloring.  Therefore IRA does not spill allocnos at
193          this point.  There is some freedom in the order of putting
194          allocnos on the stack which can affect the final result of
195          the allocation.  IRA uses some heuristics to improve the
196          order.
197          
198          We also use a modification of Chaitin-Briggs algorithm which
199          works for intersected register classes of allocnos.  To
200          figure out trivial colorability of allocnos, the mentioned
201          above tree of hard register sets is used.  To get an idea how
202          the algorithm works in i386 example, let us consider an
203          allocno to which any general hard register can be assigned.
204          If the allocno conflicts with eight allocnos to which only
205          EAX register can be assigned, given allocno is still
206          trivially colorable because all conflicting allocnos might be
207          assigned only to EAX and all other general hard registers are
208          still free.
209
210          To get an idea of the used trivial colorability criterion, it
211          is also useful to read article "Graph-Coloring Register
212          Allocation for Irregular Architectures" by Michael D. Smith
213          and Glen Holloway.  Major difference between the article
214          approach and approach used in IRA is that Smith's approach
215          takes register classes only from machine description and IRA
216          calculate register classes from intermediate code too
217          (e.g. an explicit usage of hard registers in RTL code for
218          parameter passing can result in creation of additional
219          register classes which contain or exclude the hard
220          registers).  That makes IRA approach useful for improving
221          coloring even for architectures with regular register files
222          and in fact some benchmarking shows the improvement for
223          regular class architectures is even bigger than for irregular
224          ones.  Another difference is that Smith's approach chooses
225          intersection of classes of all insn operands in which a given
226          pseudo occurs.  IRA can use bigger classes if it is still
227          more profitable than memory usage.
228
229        * Popping the allocnos from the stack and assigning them hard
230          registers.  If IRA can not assign a hard register to an
231          allocno and the allocno is coalesced, IRA undoes the
232          coalescing and puts the uncoalesced allocnos onto the stack in
233          the hope that some such allocnos will get a hard register
234          separately.  If IRA fails to assign hard register or memory
235          is more profitable for it, IRA spills the allocno.  IRA
236          assigns the allocno the hard-register with minimal full
237          allocation cost which reflects the cost of usage of the
238          hard-register for the allocno and cost of usage of the
239          hard-register for allocnos conflicting with given allocno.
240
241        * Chaitin-Briggs coloring assigns as many pseudos as possible
242          to hard registers.  After coloringh we try to improve
243          allocation with cost point of view.  We improve the
244          allocation by spilling some allocnos and assigning the freed
245          hard registers to other allocnos if it decreases the overall
246          allocation cost.
247
248        * After allono assigning in the region, IRA modifies the hard
249          register and memory costs for the corresponding allocnos in
250          the subregions to reflect the cost of possible loads, stores,
251          or moves on the border of the region and its subregions.
252          When default regional allocation algorithm is used
253          (-fira-algorithm=mixed), IRA just propagates the assignment
254          for allocnos if the register pressure in the region for the
255          corresponding pressure class is less than number of available
256          hard registers for given pressure class.
257
258      o Spill/restore code moving.  When IRA performs an allocation
259        by traversing regions in top-down order, it does not know what
260        happens below in the region tree.  Therefore, sometimes IRA
261        misses opportunities to perform a better allocation.  A simple
262        optimization tries to improve allocation in a region having
263        subregions and containing in another region.  If the
264        corresponding allocnos in the subregion are spilled, it spills
265        the region allocno if it is profitable.  The optimization
266        implements a simple iterative algorithm performing profitable
267        transformations while they are still possible.  It is fast in
268        practice, so there is no real need for a better time complexity
269        algorithm.
270
271      o Code change.  After coloring, two allocnos representing the
272        same pseudo-register outside and inside a region respectively
273        may be assigned to different locations (hard-registers or
274        memory).  In this case IRA creates and uses a new
275        pseudo-register inside the region and adds code to move allocno
276        values on the region's borders.  This is done during top-down
277        traversal of the regions (file ira-emit.c).  In some
278        complicated cases IRA can create a new allocno to move allocno
279        values (e.g. when a swap of values stored in two hard-registers
280        is needed).  At this stage, the new allocno is marked as
281        spilled.  IRA still creates the pseudo-register and the moves
282        on the region borders even when both allocnos were assigned to
283        the same hard-register.  If the reload pass spills a
284        pseudo-register for some reason, the effect will be smaller
285        because another allocno will still be in the hard-register.  In
286        most cases, this is better then spilling both allocnos.  If
287        reload does not change the allocation for the two
288        pseudo-registers, the trivial move will be removed by
289        post-reload optimizations.  IRA does not generate moves for
290        allocnos assigned to the same hard register when the default
291        regional allocation algorithm is used and the register pressure
292        in the region for the corresponding pressure class is less than
293        number of available hard registers for given pressure class.
294        IRA also does some optimizations to remove redundant stores and
295        to reduce code duplication on the region borders.
296
297      o Flattening internal representation.  After changing code, IRA
298        transforms its internal representation for several regions into
299        one region representation (file ira-build.c).  This process is
300        called IR flattening.  Such process is more complicated than IR
301        rebuilding would be, but is much faster.
302
303      o After IR flattening, IRA tries to assign hard registers to all
304        spilled allocnos.  This is impelemented by a simple and fast
305        priority coloring algorithm (see function
306        ira_reassign_conflict_allocnos::ira-color.c).  Here new allocnos
307        created during the code change pass can be assigned to hard
308        registers.
309
310      o At the end IRA calls the reload pass.  The reload pass
311        communicates with IRA through several functions in file
312        ira-color.c to improve its decisions in
313
314        * sharing stack slots for the spilled pseudos based on IRA info
315          about pseudo-register conflicts.
316
317        * reassigning hard-registers to all spilled pseudos at the end
318          of each reload iteration.
319
320        * choosing a better hard-register to spill based on IRA info
321          about pseudo-register live ranges and the register pressure
322          in places where the pseudo-register lives.
323
324    IRA uses a lot of data representing the target processors.  These
325    data are initilized in file ira.c.
326
327    If function has no loops (or the loops are ignored when
328    -fira-algorithm=CB is used), we have classic Chaitin-Briggs
329    coloring (only instead of separate pass of coalescing, we use hard
330    register preferencing).  In such case, IRA works much faster
331    because many things are not made (like IR flattening, the
332    spill/restore optimization, and the code change).
333
334    Literature is worth to read for better understanding the code:
335
336    o Preston Briggs, Keith D. Cooper, Linda Torczon.  Improvements to
337      Graph Coloring Register Allocation.
338
339    o David Callahan, Brian Koblenz.  Register allocation via
340      hierarchical graph coloring.
341
342    o Keith Cooper, Anshuman Dasgupta, Jason Eckhardt. Revisiting Graph
343      Coloring Register Allocation: A Study of the Chaitin-Briggs and
344      Callahan-Koblenz Algorithms.
345
346    o Guei-Yuan Lueh, Thomas Gross, and Ali-Reza Adl-Tabatabai. Global
347      Register Allocation Based on Graph Fusion.
348
349    o Michael D. Smith and Glenn Holloway.  Graph-Coloring Register
350      Allocation for Irregular Architectures
351
352    o Vladimir Makarov. The Integrated Register Allocator for GCC.
353
354    o Vladimir Makarov.  The top-down register allocator for irregular
355      register file architectures.
356
357 */
358
359
360 #include "config.h"
361 #include "system.h"
362 #include "coretypes.h"
363 #include "tm.h"
364 #include "regs.h"
365 #include "rtl.h"
366 #include "tm_p.h"
367 #include "target.h"
368 #include "flags.h"
369 #include "obstack.h"
370 #include "bitmap.h"
371 #include "hard-reg-set.h"
372 #include "basic-block.h"
373 #include "df.h"
374 #include "expr.h"
375 #include "recog.h"
376 #include "params.h"
377 #include "timevar.h"
378 #include "tree-pass.h"
379 #include "output.h"
380 #include "except.h"
381 #include "reload.h"
382 #include "diagnostic-core.h"
383 #include "integrate.h"
384 #include "ggc.h"
385 #include "ira-int.h"
386 #include "dce.h"
387
388
389 struct target_ira default_target_ira;
390 struct target_ira_int default_target_ira_int;
391 #if SWITCHABLE_TARGET
392 struct target_ira *this_target_ira = &default_target_ira;
393 struct target_ira_int *this_target_ira_int = &default_target_ira_int;
394 #endif
395
396 /* A modified value of flag `-fira-verbose' used internally.  */
397 int internal_flag_ira_verbose;
398
399 /* Dump file of the allocator if it is not NULL.  */
400 FILE *ira_dump_file;
401
402 /* The number of elements in the following array.  */
403 int ira_spilled_reg_stack_slots_num;
404
405 /* The following array contains info about spilled pseudo-registers
406    stack slots used in current function so far.  */
407 struct ira_spilled_reg_stack_slot *ira_spilled_reg_stack_slots;
408
409 /* Correspondingly overall cost of the allocation, cost of the
410    allocnos assigned to hard-registers, cost of the allocnos assigned
411    to memory, cost of loads, stores and register move insns generated
412    for pseudo-register live range splitting (see ira-emit.c).  */
413 int ira_overall_cost;
414 int ira_reg_cost, ira_mem_cost;
415 int ira_load_cost, ira_store_cost, ira_shuffle_cost;
416 int ira_move_loops_num, ira_additional_jumps_num;
417
418 /* All registers that can be eliminated.  */
419
420 HARD_REG_SET eliminable_regset;
421
422 /* Temporary hard reg set used for a different calculation.  */
423 static HARD_REG_SET temp_hard_regset;
424
425 \f
426
427 /* The function sets up the map IRA_REG_MODE_HARD_REGSET.  */
428 static void
429 setup_reg_mode_hard_regset (void)
430 {
431   int i, m, hard_regno;
432
433   for (m = 0; m < NUM_MACHINE_MODES; m++)
434     for (hard_regno = 0; hard_regno < FIRST_PSEUDO_REGISTER; hard_regno++)
435       {
436         CLEAR_HARD_REG_SET (ira_reg_mode_hard_regset[hard_regno][m]);
437         for (i = hard_regno_nregs[hard_regno][m] - 1; i >= 0; i--)
438           if (hard_regno + i < FIRST_PSEUDO_REGISTER)
439             SET_HARD_REG_BIT (ira_reg_mode_hard_regset[hard_regno][m],
440                               hard_regno + i);
441       }
442 }
443
444 \f
445 #define no_unit_alloc_regs \
446   (this_target_ira_int->x_no_unit_alloc_regs)
447
448 /* The function sets up the three arrays declared above.  */
449 static void
450 setup_class_hard_regs (void)
451 {
452   int cl, i, hard_regno, n;
453   HARD_REG_SET processed_hard_reg_set;
454
455   ira_assert (SHRT_MAX >= FIRST_PSEUDO_REGISTER);
456   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
457     {
458       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
459       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
460       CLEAR_HARD_REG_SET (processed_hard_reg_set);
461       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
462         {
463           ira_non_ordered_class_hard_regs[cl][i] = -1;
464           ira_class_hard_reg_index[cl][i] = -1;
465         }
466       for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
467         {
468 #ifdef REG_ALLOC_ORDER
469           hard_regno = reg_alloc_order[i];
470 #else
471           hard_regno = i;
472 #endif
473           if (TEST_HARD_REG_BIT (processed_hard_reg_set, hard_regno))
474             continue;
475           SET_HARD_REG_BIT (processed_hard_reg_set, hard_regno);
476           if (! TEST_HARD_REG_BIT (temp_hard_regset, hard_regno))
477             ira_class_hard_reg_index[cl][hard_regno] = -1;
478           else
479             {
480               ira_class_hard_reg_index[cl][hard_regno] = n;
481               ira_class_hard_regs[cl][n++] = hard_regno;
482             }
483         }
484       ira_class_hard_regs_num[cl] = n;
485       for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
486         if (TEST_HARD_REG_BIT (temp_hard_regset, i))
487           ira_non_ordered_class_hard_regs[cl][n++] = i;
488       ira_assert (ira_class_hard_regs_num[cl] == n);
489     }
490 }
491
492 /* Set up IRA_AVAILABLE_CLASS_REGS.  */
493 static void
494 setup_available_class_regs (void)
495 {
496   int i, j;
497
498   memset (ira_available_class_regs, 0, sizeof (ira_available_class_regs));
499   for (i = 0; i < N_REG_CLASSES; i++)
500     {
501       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
502       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
503       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
504         if (TEST_HARD_REG_BIT (temp_hard_regset, j))
505           ira_available_class_regs[i]++;
506     }
507 }
508
509 /* Set up global variables defining info about hard registers for the
510    allocation.  These depend on USE_HARD_FRAME_P whose TRUE value means
511    that we can use the hard frame pointer for the allocation.  */
512 static void
513 setup_alloc_regs (bool use_hard_frame_p)
514 {
515 #ifdef ADJUST_REG_ALLOC_ORDER
516   ADJUST_REG_ALLOC_ORDER;
517 #endif
518   COPY_HARD_REG_SET (no_unit_alloc_regs, fixed_reg_set);
519   if (! use_hard_frame_p)
520     SET_HARD_REG_BIT (no_unit_alloc_regs, HARD_FRAME_POINTER_REGNUM);
521   setup_class_hard_regs ();
522   setup_available_class_regs ();
523 }
524
525 \f
526
527 #define alloc_reg_class_subclasses \
528   (this_target_ira_int->x_alloc_reg_class_subclasses)
529
530 /* Initialize the table of subclasses of each reg class.  */
531 static void
532 setup_reg_subclasses (void)
533 {
534   int i, j;
535   HARD_REG_SET temp_hard_regset2;
536
537   for (i = 0; i < N_REG_CLASSES; i++)
538     for (j = 0; j < N_REG_CLASSES; j++)
539       alloc_reg_class_subclasses[i][j] = LIM_REG_CLASSES;
540
541   for (i = 0; i < N_REG_CLASSES; i++)
542     {
543       if (i == (int) NO_REGS)
544         continue;
545
546       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
547       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
548       if (hard_reg_set_empty_p (temp_hard_regset))
549         continue;
550       for (j = 0; j < N_REG_CLASSES; j++)
551         if (i != j)
552           {
553             enum reg_class *p;
554
555             COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[j]);
556             AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
557             if (! hard_reg_set_subset_p (temp_hard_regset,
558                                          temp_hard_regset2))
559               continue;
560             p = &alloc_reg_class_subclasses[j][0];
561             while (*p != LIM_REG_CLASSES) p++;
562             *p = (enum reg_class) i;
563           }
564     }
565 }
566
567 \f
568
569 /* Set up IRA_MEMORY_MOVE_COST and IRA_MAX_MEMORY_MOVE_COST.  */
570 static void
571 setup_class_subset_and_memory_move_costs (void)
572 {
573   int cl, cl2, mode, cost;
574   HARD_REG_SET temp_hard_regset2;
575
576   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
577     ira_memory_move_cost[mode][NO_REGS][0]
578       = ira_memory_move_cost[mode][NO_REGS][1] = SHRT_MAX;
579   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
580     {
581       if (cl != (int) NO_REGS)
582         for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
583           {
584             ira_max_memory_move_cost[mode][cl][0]
585               = ira_memory_move_cost[mode][cl][0]
586               = memory_move_cost ((enum machine_mode) mode,
587                                   (reg_class_t) cl, false);
588             ira_max_memory_move_cost[mode][cl][1]
589               = ira_memory_move_cost[mode][cl][1]
590               = memory_move_cost ((enum machine_mode) mode,
591                                   (reg_class_t) cl, true);
592             /* Costs for NO_REGS are used in cost calculation on the
593                1st pass when the preferred register classes are not
594                known yet.  In this case we take the best scenario.  */
595             if (ira_memory_move_cost[mode][NO_REGS][0]
596                 > ira_memory_move_cost[mode][cl][0])
597               ira_max_memory_move_cost[mode][NO_REGS][0]
598                 = ira_memory_move_cost[mode][NO_REGS][0]
599                 = ira_memory_move_cost[mode][cl][0];
600             if (ira_memory_move_cost[mode][NO_REGS][1]
601                 > ira_memory_move_cost[mode][cl][1])
602               ira_max_memory_move_cost[mode][NO_REGS][1]
603                 = ira_memory_move_cost[mode][NO_REGS][1]
604                 = ira_memory_move_cost[mode][cl][1];
605           }
606     }
607   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
608     for (cl2 = (int) N_REG_CLASSES - 1; cl2 >= 0; cl2--)
609       {
610         COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
611         AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
612         COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl2]);
613         AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
614         ira_class_subset_p[cl][cl2]
615           = hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2);
616         if (! hard_reg_set_empty_p (temp_hard_regset2)
617             && hard_reg_set_subset_p (reg_class_contents[cl2],
618                                       reg_class_contents[cl]))
619           for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
620             {
621               cost = ira_memory_move_cost[mode][cl2][0];
622               if (cost > ira_max_memory_move_cost[mode][cl][0])
623                 ira_max_memory_move_cost[mode][cl][0] = cost;
624               cost = ira_memory_move_cost[mode][cl2][1];
625               if (cost > ira_max_memory_move_cost[mode][cl][1])
626                 ira_max_memory_move_cost[mode][cl][1] = cost;
627             }
628       }
629   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
630     for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
631       {
632         ira_memory_move_cost[mode][cl][0]
633           = ira_max_memory_move_cost[mode][cl][0];
634         ira_memory_move_cost[mode][cl][1]
635           = ira_max_memory_move_cost[mode][cl][1];
636       }
637   setup_reg_subclasses ();
638 }
639
640 \f
641
642 /* Define the following macro if allocation through malloc if
643    preferable.  */
644 #define IRA_NO_OBSTACK
645
646 #ifndef IRA_NO_OBSTACK
647 /* Obstack used for storing all dynamic data (except bitmaps) of the
648    IRA.  */
649 static struct obstack ira_obstack;
650 #endif
651
652 /* Obstack used for storing all bitmaps of the IRA.  */
653 static struct bitmap_obstack ira_bitmap_obstack;
654
655 /* Allocate memory of size LEN for IRA data.  */
656 void *
657 ira_allocate (size_t len)
658 {
659   void *res;
660
661 #ifndef IRA_NO_OBSTACK
662   res = obstack_alloc (&ira_obstack, len);
663 #else
664   res = xmalloc (len);
665 #endif
666   return res;
667 }
668
669 /* Free memory ADDR allocated for IRA data.  */
670 void
671 ira_free (void *addr ATTRIBUTE_UNUSED)
672 {
673 #ifndef IRA_NO_OBSTACK
674   /* do nothing */
675 #else
676   free (addr);
677 #endif
678 }
679
680
681 /* Allocate and returns bitmap for IRA.  */
682 bitmap
683 ira_allocate_bitmap (void)
684 {
685   return BITMAP_ALLOC (&ira_bitmap_obstack);
686 }
687
688 /* Free bitmap B allocated for IRA.  */
689 void
690 ira_free_bitmap (bitmap b ATTRIBUTE_UNUSED)
691 {
692   /* do nothing */
693 }
694
695 \f
696
697 /* Output information about allocation of all allocnos (except for
698    caps) into file F.  */
699 void
700 ira_print_disposition (FILE *f)
701 {
702   int i, n, max_regno;
703   ira_allocno_t a;
704   basic_block bb;
705
706   fprintf (f, "Disposition:");
707   max_regno = max_reg_num ();
708   for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
709     for (a = ira_regno_allocno_map[i];
710          a != NULL;
711          a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
712       {
713         if (n % 4 == 0)
714           fprintf (f, "\n");
715         n++;
716         fprintf (f, " %4d:r%-4d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
717         if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
718           fprintf (f, "b%-3d", bb->index);
719         else
720           fprintf (f, "l%-3d", ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
721         if (ALLOCNO_HARD_REGNO (a) >= 0)
722           fprintf (f, " %3d", ALLOCNO_HARD_REGNO (a));
723         else
724           fprintf (f, " mem");
725       }
726   fprintf (f, "\n");
727 }
728
729 /* Outputs information about allocation of all allocnos into
730    stderr.  */
731 void
732 ira_debug_disposition (void)
733 {
734   ira_print_disposition (stderr);
735 }
736
737 \f
738
739 /* Set up ira_stack_reg_pressure_class which is the biggest pressure
740    register class containing stack registers or NO_REGS if there are
741    no stack registers.  To find this class, we iterate through all
742    register pressure classes and choose the first register pressure
743    class containing all the stack registers and having the biggest
744    size.  */
745 static void
746 setup_stack_reg_pressure_class (void)
747 {
748   ira_stack_reg_pressure_class = NO_REGS;
749 #ifdef STACK_REGS
750   {
751     int i, best, size;
752     enum reg_class cl;
753     HARD_REG_SET temp_hard_regset2;
754
755     CLEAR_HARD_REG_SET (temp_hard_regset);
756     for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
757       SET_HARD_REG_BIT (temp_hard_regset, i);
758     best = 0;
759     for (i = 0; i < ira_pressure_classes_num; i++)
760       {
761         cl = ira_pressure_classes[i];
762         COPY_HARD_REG_SET (temp_hard_regset2, temp_hard_regset);
763         AND_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
764         size = hard_reg_set_size (temp_hard_regset2);
765         if (best < size)
766           {
767             best = size;
768             ira_stack_reg_pressure_class = cl;
769           }
770       }
771   }
772 #endif
773 }
774
775 /* Find pressure classes which are register classes for which we
776    calculate register pressure in IRA, register pressure sensitive
777    insn scheduling, and register pressure sensitive loop invariant
778    motion.
779
780    To make register pressure calculation easy, we always use
781    non-intersected register pressure classes.  A move of hard
782    registers from one register pressure class is not more expensive
783    than load and store of the hard registers.  Most likely an allocno
784    class will be a subset of a register pressure class and in many
785    cases a register pressure class.  That makes usage of register
786    pressure classes a good approximation to find a high register
787    pressure.  */
788 static void
789 setup_pressure_classes (void)
790 {
791   int cost, i, n, curr;
792   int cl, cl2;
793   enum reg_class pressure_classes[N_REG_CLASSES];
794   int m;
795   HARD_REG_SET temp_hard_regset2;
796   bool insert_p;
797
798   n = 0;
799   for (cl = 0; cl < N_REG_CLASSES; cl++)
800     {
801       if (ira_available_class_regs[cl] == 0)
802         continue;
803       if (ira_available_class_regs[cl] != 1
804           /* A register class without subclasses may contain a few
805              hard registers and movement between them is costly
806              (e.g. SPARC FPCC registers).  We still should consider it
807              as a candidate for a pressure class.  */
808           && alloc_reg_class_subclasses[cl][0] != LIM_REG_CLASSES)
809         {
810           /* Check that the moves between any hard registers of the
811              current class are not more expensive for a legal mode
812              than load/store of the hard registers of the current
813              class.  Such class is a potential candidate to be a
814              register pressure class.  */
815           for (m = 0; m < NUM_MACHINE_MODES; m++)
816             {
817               COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
818               AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
819               AND_COMPL_HARD_REG_SET (temp_hard_regset,
820                                       ira_prohibited_class_mode_regs[cl][m]);
821               if (hard_reg_set_empty_p (temp_hard_regset))
822                 continue;
823               ira_init_register_move_cost_if_necessary ((enum machine_mode) m);
824               cost = ira_register_move_cost[m][cl][cl];
825               if (cost <= ira_max_memory_move_cost[m][cl][1]
826                   || cost <= ira_max_memory_move_cost[m][cl][0])
827                 break;
828             }
829           if (m >= NUM_MACHINE_MODES)
830             continue;
831         }
832       curr = 0;
833       insert_p = true;
834       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
835       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
836       /* Remove so far added pressure classes which are subset of the
837          current candidate class.  Prefer GENERAL_REGS as a pressure
838          register class to another class containing the same
839          allocatable hard registers.  We do this because machine
840          dependent cost hooks might give wrong costs for the latter
841          class but always give the right cost for the former class
842          (GENERAL_REGS).  */
843       for (i = 0; i < n; i++)
844         {
845           cl2 = pressure_classes[i];
846           COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl2]);
847           AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
848           if (hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2)
849               && (! hard_reg_set_equal_p (temp_hard_regset, temp_hard_regset2)
850                   || cl2 == (int) GENERAL_REGS))
851             {
852               pressure_classes[curr++] = (enum reg_class) cl2;
853               insert_p = false;
854               continue;
855             }
856           if (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset)
857               && (! hard_reg_set_equal_p (temp_hard_regset2, temp_hard_regset)
858                   || cl == (int) GENERAL_REGS))
859             continue;
860           if (hard_reg_set_equal_p (temp_hard_regset2, temp_hard_regset))
861             insert_p = false;
862           pressure_classes[curr++] = (enum reg_class) cl2;
863         }
864       /* If the current candidate is a subset of a so far added
865          pressure class, don't add it to the list of the pressure
866          classes.  */
867       if (insert_p)
868         pressure_classes[curr++] = (enum reg_class) cl;
869       n = curr;
870     }
871 #ifdef ENABLE_IRA_CHECKING
872   {
873     HARD_REG_SET ignore_hard_regs;
874
875     /* Check pressure classes correctness: here we check that hard
876        registers from all register pressure classes contains all hard
877        registers available for the allocation.  */
878     CLEAR_HARD_REG_SET (temp_hard_regset);
879     CLEAR_HARD_REG_SET (temp_hard_regset2);
880     COPY_HARD_REG_SET (ignore_hard_regs, no_unit_alloc_regs);
881     for (cl = 0; cl < LIM_REG_CLASSES; cl++)
882       {
883         /* For some targets (like MIPS with MD_REGS), there are some
884            classes with hard registers available for allocation but
885            not able to hold value of any mode.  */
886         for (m = 0; m < NUM_MACHINE_MODES; m++)
887           if (contains_reg_of_mode[cl][m])
888             break;
889         if (m >= NUM_MACHINE_MODES)
890           {
891             IOR_HARD_REG_SET (ignore_hard_regs, reg_class_contents[cl]);
892             continue;
893           }
894         for (i = 0; i < n; i++)
895           if ((int) pressure_classes[i] == cl)
896             break;
897         IOR_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
898         if (i < n)
899           IOR_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
900       }
901     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
902       /* Some targets (like SPARC with ICC reg) have alocatable regs
903          for which no reg class is defined.  */
904       if (REGNO_REG_CLASS (i) == NO_REGS)
905         SET_HARD_REG_BIT (ignore_hard_regs, i);
906     AND_COMPL_HARD_REG_SET (temp_hard_regset, ignore_hard_regs);
907     AND_COMPL_HARD_REG_SET (temp_hard_regset2, ignore_hard_regs);
908     ira_assert (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset));
909   }
910 #endif
911   ira_pressure_classes_num = 0;
912   for (i = 0; i < n; i++)
913     {
914       cl = (int) pressure_classes[i];
915       ira_reg_pressure_class_p[cl] = true;
916       ira_pressure_classes[ira_pressure_classes_num++] = (enum reg_class) cl;
917     }
918   setup_stack_reg_pressure_class ();
919 }
920
921 /* Set up IRA_ALLOCNO_CLASSES, IRA_ALLOCNO_CLASSES_NUM,
922    IRA_IMPORTANT_CLASSES, and IRA_IMPORTANT_CLASSES_NUM.
923
924    Target may have many subtargets and not all target hard regiters can
925    be used for allocation, e.g. x86 port in 32-bit mode can not use
926    hard registers introduced in x86-64 like r8-r15).  Some classes
927    might have the same allocatable hard registers, e.g.  INDEX_REGS
928    and GENERAL_REGS in x86 port in 32-bit mode.  To decrease different
929    calculations efforts we introduce allocno classes which contain
930    unique non-empty sets of allocatable hard-registers.
931
932    Pseudo class cost calculation in ira-costs.c is very expensive.
933    Therefore we are trying to decrease number of classes involved in
934    such calculation.  Register classes used in the cost calculation
935    are called important classes.  They are allocno classes and other
936    non-empty classes whose allocatable hard register sets are inside
937    of an allocno class hard register set.  From the first sight, it
938    looks like that they are just allocno classes.  It is not true.  In
939    example of x86-port in 32-bit mode, allocno classes will contain
940    GENERAL_REGS but not LEGACY_REGS (because allocatable hard
941    registers are the same for the both classes).  The important
942    classes will contain GENERAL_REGS and LEGACY_REGS.  It is done
943    because a machine description insn constraint may refers for
944    LEGACY_REGS and code in ira-costs.c is mostly base on investigation
945    of the insn constraints.  */
946 static void
947 setup_allocno_and_important_classes (void)
948 {
949   int i, j, n, cl;
950   bool set_p;
951   HARD_REG_SET temp_hard_regset2;
952   static enum reg_class classes[LIM_REG_CLASSES + 1];
953
954   n = 0;
955   /* Collect classes which contain unique sets of allocatable hard
956      registers.  Prefer GENERAL_REGS to other classes containing the
957      same set of hard registers.  */
958   for (i = 0; i < LIM_REG_CLASSES; i++)
959     {
960       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
961       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
962       for (j = 0; j < n; j++)
963         {
964           cl = classes[j];
965           COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
966           AND_COMPL_HARD_REG_SET (temp_hard_regset2,
967                                   no_unit_alloc_regs);
968           if (hard_reg_set_equal_p (temp_hard_regset,
969                                     temp_hard_regset2))
970             break;
971         }
972       if (j >= n)
973         classes[n++] = (enum reg_class) i;
974       else if (i == GENERAL_REGS)
975         /* Prefer general regs.  For i386 example, it means that
976            we prefer GENERAL_REGS over INDEX_REGS or LEGACY_REGS
977            (all of them consists of the same available hard
978            registers).  */
979         classes[j] = (enum reg_class) i;
980     }
981   classes[n] = LIM_REG_CLASSES;
982
983   /* Set up classes which can be used for allocnos as classes
984      conatining non-empty unique sets of allocatable hard
985      registers.  */
986   ira_allocno_classes_num = 0;
987   for (i = 0; (cl = classes[i]) != LIM_REG_CLASSES; i++)
988     {
989       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
990       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
991       if (hard_reg_set_empty_p (temp_hard_regset))
992         continue;
993       ira_allocno_classes[ira_allocno_classes_num++] = (enum reg_class) cl;
994     }
995   ira_important_classes_num = 0;
996   /* Add non-allocno classes containing to non-empty set of
997      allocatable hard regs.  */
998   for (cl = 0; cl < N_REG_CLASSES; cl++)
999     {
1000       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
1001       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1002       if (! hard_reg_set_empty_p (temp_hard_regset))
1003         {
1004           set_p = false;
1005           for (j = 0; j < ira_allocno_classes_num; j++)
1006             {
1007               COPY_HARD_REG_SET (temp_hard_regset2,
1008                                  reg_class_contents[ira_allocno_classes[j]]);
1009               AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
1010               if ((enum reg_class) cl == ira_allocno_classes[j])
1011                 break;
1012               else if (hard_reg_set_subset_p (temp_hard_regset,
1013                                               temp_hard_regset2))
1014                 set_p = true;
1015             }
1016           if (set_p && j >= ira_allocno_classes_num)
1017             ira_important_classes[ira_important_classes_num++]
1018               = (enum reg_class) cl;
1019         }
1020     }
1021   /* Now add allocno classes to the important classes.  */
1022   for (j = 0; j < ira_allocno_classes_num; j++)
1023     ira_important_classes[ira_important_classes_num++]
1024       = ira_allocno_classes[j];
1025   for (cl = 0; cl < N_REG_CLASSES; cl++)
1026     {
1027       ira_reg_allocno_class_p[cl] = false;
1028       ira_reg_pressure_class_p[cl] = false;
1029     }
1030   for (j = 0; j < ira_allocno_classes_num; j++)
1031     ira_reg_allocno_class_p[ira_allocno_classes[j]] = true;
1032   setup_pressure_classes ();
1033 }
1034
1035 /* Setup translation in CLASS_TRANSLATE of all classes into a class
1036    given by array CLASSES of length CLASSES_NUM.  The function is used
1037    make translation any reg class to an allocno class or to an
1038    pressure class.  This translation is necessary for some
1039    calculations when we can use only allocno or pressure classes and
1040    such translation represents an approximate representation of all
1041    classes.
1042
1043    The translation in case when allocatable hard register set of a
1044    given class is subset of allocatable hard register set of a class
1045    in CLASSES is pretty simple.  We use smallest classes from CLASSES
1046    containing a given class.  If allocatable hard register set of a
1047    given class is not a subset of any corresponding set of a class
1048    from CLASSES, we use the cheapest (with load/store point of view)
1049    class from CLASSES whose set intersects with given class set */
1050 static void
1051 setup_class_translate_array (enum reg_class *class_translate,
1052                              int classes_num, enum reg_class *classes)
1053 {
1054   int cl, mode;
1055   enum reg_class aclass, best_class, *cl_ptr;
1056   int i, cost, min_cost, best_cost;
1057
1058   for (cl = 0; cl < N_REG_CLASSES; cl++)
1059     class_translate[cl] = NO_REGS;
1060
1061   for (i = 0; i < classes_num; i++)
1062     {
1063       aclass = classes[i];
1064       for (cl_ptr = &alloc_reg_class_subclasses[aclass][0];
1065            (cl = *cl_ptr) != LIM_REG_CLASSES;
1066            cl_ptr++)
1067         if (class_translate[cl] == NO_REGS)
1068           class_translate[cl] = aclass;
1069       class_translate[aclass] = aclass;
1070     }
1071   /* For classes which are not fully covered by one of given classes
1072      (in other words covered by more one given class), use the
1073      cheapest class.  */
1074   for (cl = 0; cl < N_REG_CLASSES; cl++)
1075     {
1076       if (cl == NO_REGS || class_translate[cl] != NO_REGS)
1077         continue;
1078       best_class = NO_REGS;
1079       best_cost = INT_MAX;
1080       for (i = 0; i < classes_num; i++)
1081         {
1082           aclass = classes[i];
1083           COPY_HARD_REG_SET (temp_hard_regset,
1084                              reg_class_contents[aclass]);
1085           AND_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
1086           AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1087           if (! hard_reg_set_empty_p (temp_hard_regset))
1088             {
1089               min_cost = INT_MAX;
1090               for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1091                 {
1092                   cost = (ira_memory_move_cost[mode][cl][0]
1093                           + ira_memory_move_cost[mode][cl][1]);
1094                   if (min_cost > cost)
1095                     min_cost = cost;
1096                 }
1097               if (best_class == NO_REGS || best_cost > min_cost)
1098                 {
1099                   best_class = aclass;
1100                   best_cost = min_cost;
1101                 }
1102             }
1103         }
1104       class_translate[cl] = best_class;
1105     }
1106 }
1107
1108 /* Set up array IRA_ALLOCNO_CLASS_TRANSLATE and
1109    IRA_PRESSURE_CLASS_TRANSLATE.  */
1110 static void
1111 setup_class_translate (void)
1112 {
1113   setup_class_translate_array (ira_allocno_class_translate,
1114                                ira_allocno_classes_num, ira_allocno_classes);
1115   setup_class_translate_array (ira_pressure_class_translate,
1116                                ira_pressure_classes_num, ira_pressure_classes);
1117 }
1118
1119 /* Order numbers of allocno classes in original target allocno class
1120    array, -1 for non-allocno classes.  */
1121 static int allocno_class_order[N_REG_CLASSES];
1122
1123 /* The function used to sort the important classes.  */
1124 static int
1125 comp_reg_classes_func (const void *v1p, const void *v2p)
1126 {
1127   enum reg_class cl1 = *(const enum reg_class *) v1p;
1128   enum reg_class cl2 = *(const enum reg_class *) v2p;
1129   enum reg_class tcl1, tcl2;
1130   int diff;
1131
1132   tcl1 = ira_allocno_class_translate[cl1];
1133   tcl2 = ira_allocno_class_translate[cl2];
1134   if (tcl1 != NO_REGS && tcl2 != NO_REGS
1135       && (diff = allocno_class_order[tcl1] - allocno_class_order[tcl2]) != 0)
1136     return diff;
1137   return (int) cl1 - (int) cl2;
1138 }
1139
1140 /* For correct work of function setup_reg_class_relation we need to
1141    reorder important classes according to the order of their allocno
1142    classes.  It places important classes containing the same
1143    allocatable hard register set adjacent to each other and allocno
1144    class with the allocatable hard register set right after the other
1145    important classes with the same set.
1146
1147    In example from comments of function
1148    setup_allocno_and_important_classes, it places LEGACY_REGS and
1149    GENERAL_REGS close to each other and GENERAL_REGS is after
1150    LEGACY_REGS.  */
1151 static void
1152 reorder_important_classes (void)
1153 {
1154   int i;
1155
1156   for (i = 0; i < N_REG_CLASSES; i++)
1157     allocno_class_order[i] = -1;
1158   for (i = 0; i < ira_allocno_classes_num; i++)
1159     allocno_class_order[ira_allocno_classes[i]] = i;
1160   qsort (ira_important_classes, ira_important_classes_num,
1161          sizeof (enum reg_class), comp_reg_classes_func);
1162   for (i = 0; i < ira_important_classes_num; i++)
1163     ira_important_class_nums[ira_important_classes[i]] = i;
1164 }
1165
1166 /* Set up IRA_REG_CLASS_SUBUNION, IRA_REG_CLASS_SUPERUNION,
1167    IRA_REG_CLASS_SUPER_CLASSES, IRA_REG_CLASSES_INTERSECT, and
1168    IRA_REG_CLASSES_INTERSECT_P.  For the meaning of the relations,
1169    please see corresponding comments in ira-int.h.  */
1170 static void
1171 setup_reg_class_relations (void)
1172 {
1173   int i, cl1, cl2, cl3;
1174   HARD_REG_SET intersection_set, union_set, temp_set2;
1175   bool important_class_p[N_REG_CLASSES];
1176
1177   memset (important_class_p, 0, sizeof (important_class_p));
1178   for (i = 0; i < ira_important_classes_num; i++)
1179     important_class_p[ira_important_classes[i]] = true;
1180   for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1181     {
1182       ira_reg_class_super_classes[cl1][0] = LIM_REG_CLASSES;
1183       for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1184         {
1185           ira_reg_classes_intersect_p[cl1][cl2] = false;
1186           ira_reg_class_intersect[cl1][cl2] = NO_REGS;
1187           COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl1]);
1188           AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1189           COPY_HARD_REG_SET (temp_set2, reg_class_contents[cl2]);
1190           AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1191           if (hard_reg_set_empty_p (temp_hard_regset)
1192               && hard_reg_set_empty_p (temp_set2))
1193             {
1194               /* The both classes have no allocatable hard registers
1195                  -- take all class hard registers into account and use
1196                  reg_class_subunion and reg_class_superunion.  */
1197               for (i = 0;; i++)
1198                 {
1199                   cl3 = reg_class_subclasses[cl1][i];
1200                   if (cl3 == LIM_REG_CLASSES)
1201                     break;
1202                   if (reg_class_subset_p (ira_reg_class_intersect[cl1][cl2],
1203                                           (enum reg_class) cl3))
1204                     ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1205                 }
1206               ira_reg_class_subunion[cl1][cl2] = reg_class_subunion[cl1][cl2];
1207               ira_reg_class_superunion[cl1][cl2] = reg_class_superunion[cl1][cl2];
1208               continue;
1209             }
1210           ira_reg_classes_intersect_p[cl1][cl2]
1211             = hard_reg_set_intersect_p (temp_hard_regset, temp_set2);
1212           if (important_class_p[cl1] && important_class_p[cl2]
1213               && hard_reg_set_subset_p (temp_hard_regset, temp_set2))
1214             {
1215               /* CL1 and CL2 are important classes and CL1 allocatable
1216                  hard register set is inside of CL2 allocatable hard
1217                  registers -- make CL1 a superset of CL2.  */
1218               enum reg_class *p;
1219
1220               p = &ira_reg_class_super_classes[cl1][0];
1221               while (*p != LIM_REG_CLASSES)
1222                 p++;
1223               *p++ = (enum reg_class) cl2;
1224               *p = LIM_REG_CLASSES;
1225             }
1226           ira_reg_class_subunion[cl1][cl2] = NO_REGS;
1227           ira_reg_class_superunion[cl1][cl2] = NO_REGS;
1228           COPY_HARD_REG_SET (intersection_set, reg_class_contents[cl1]);
1229           AND_HARD_REG_SET (intersection_set, reg_class_contents[cl2]);
1230           AND_COMPL_HARD_REG_SET (intersection_set, no_unit_alloc_regs);
1231           COPY_HARD_REG_SET (union_set, reg_class_contents[cl1]);
1232           IOR_HARD_REG_SET (union_set, reg_class_contents[cl2]);
1233           AND_COMPL_HARD_REG_SET (union_set, no_unit_alloc_regs);
1234           for (i = 0; i < ira_important_classes_num; i++)
1235             {
1236               cl3 = ira_important_classes[i];
1237               COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl3]);
1238               AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1239               if (hard_reg_set_subset_p (temp_hard_regset, intersection_set))
1240                 {
1241                   /* CL3 allocatable hard register set is inside of
1242                      intersection of allocatable hard register sets
1243                      of CL1 and CL2.  */
1244                   COPY_HARD_REG_SET
1245                     (temp_set2,
1246                      reg_class_contents[(int)
1247                                         ira_reg_class_intersect[cl1][cl2]]);
1248                   AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1249                   if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1250                       /* If the allocatable hard register sets are the
1251                          same, prefer GENERAL_REGS or the smallest
1252                          class for debugging purposes.  */
1253                       || (hard_reg_set_equal_p (temp_hard_regset, temp_set2)
1254                           && (cl3 == GENERAL_REGS
1255                               || (ira_reg_class_intersect[cl1][cl2] != GENERAL_REGS
1256                                   && hard_reg_set_subset_p
1257                                      (reg_class_contents[cl3],
1258                                       reg_class_contents
1259                                       [(int) ira_reg_class_intersect[cl1][cl2]])))))
1260                     ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1261                 }
1262               if (hard_reg_set_subset_p (temp_hard_regset, union_set))
1263                 {
1264                   /* CL3 allocatbale hard register set is inside of
1265                      union of allocatable hard register sets of CL1
1266                      and CL2.  */
1267                   COPY_HARD_REG_SET
1268                     (temp_set2,
1269                      reg_class_contents[(int) ira_reg_class_subunion[cl1][cl2]]);
1270                   AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1271                   if (ira_reg_class_subunion[cl1][cl2] == NO_REGS
1272                       || (hard_reg_set_subset_p (temp_set2, temp_hard_regset)
1273                           
1274                           && (! hard_reg_set_equal_p (temp_set2,
1275                                                       temp_hard_regset)
1276                               || cl3 == GENERAL_REGS
1277                               /* If the allocatable hard register sets are the
1278                                  same, prefer GENERAL_REGS or the smallest
1279                                  class for debugging purposes.  */
1280                               || (ira_reg_class_subunion[cl1][cl2] != GENERAL_REGS
1281                                   && hard_reg_set_subset_p
1282                                      (reg_class_contents[cl3],
1283                                       reg_class_contents
1284                                       [(int) ira_reg_class_subunion[cl1][cl2]])))))
1285                     ira_reg_class_subunion[cl1][cl2] = (enum reg_class) cl3;
1286                 }
1287               if (hard_reg_set_subset_p (union_set, temp_hard_regset))
1288                 {
1289                   /* CL3 allocatable hard register set contains union
1290                      of allocatable hard register sets of CL1 and
1291                      CL2.  */
1292                   COPY_HARD_REG_SET
1293                     (temp_set2,
1294                      reg_class_contents[(int) ira_reg_class_superunion[cl1][cl2]]);
1295                   AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1296                   if (ira_reg_class_superunion[cl1][cl2] == NO_REGS
1297                       || (hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1298
1299                           && (! hard_reg_set_equal_p (temp_set2,
1300                                                       temp_hard_regset)
1301                               || cl3 == GENERAL_REGS
1302                               /* If the allocatable hard register sets are the
1303                                  same, prefer GENERAL_REGS or the smallest
1304                                  class for debugging purposes.  */
1305                               || (ira_reg_class_superunion[cl1][cl2] != GENERAL_REGS
1306                                   && hard_reg_set_subset_p
1307                                      (reg_class_contents[cl3],
1308                                       reg_class_contents
1309                                       [(int) ira_reg_class_superunion[cl1][cl2]])))))
1310                     ira_reg_class_superunion[cl1][cl2] = (enum reg_class) cl3;
1311                 }
1312             }
1313         }
1314     }
1315 }
1316
1317 /* Output all possible allocno classes and the translation map into
1318    file F.  */
1319 static void
1320 print_classes (FILE *f, bool pressure_p)
1321 {
1322   int classes_num = (pressure_p
1323                      ? ira_pressure_classes_num : ira_allocno_classes_num);
1324   enum reg_class *classes = (pressure_p
1325                              ? ira_pressure_classes : ira_allocno_classes);
1326   enum reg_class *class_translate = (pressure_p
1327                                      ? ira_pressure_class_translate
1328                                      : ira_allocno_class_translate);
1329   static const char *const reg_class_names[] = REG_CLASS_NAMES;
1330   int i;
1331
1332   fprintf (f, "%s classes:\n", pressure_p ? "Pressure" : "Allocno");
1333   for (i = 0; i < classes_num; i++)
1334     fprintf (f, " %s", reg_class_names[classes[i]]);
1335   fprintf (f, "\nClass translation:\n");
1336   for (i = 0; i < N_REG_CLASSES; i++)
1337     fprintf (f, " %s -> %s\n", reg_class_names[i],
1338              reg_class_names[class_translate[i]]);
1339 }
1340
1341 /* Output all possible allocno and translation classes and the
1342    translation maps into stderr.  */
1343 void
1344 ira_debug_allocno_classes (void)
1345 {
1346   print_classes (stderr, false);
1347   print_classes (stderr, true);
1348 }
1349
1350 /* Set up different arrays concerning class subsets, allocno and
1351    important classes.  */
1352 static void
1353 find_reg_classes (void)
1354 {
1355   setup_allocno_and_important_classes ();
1356   setup_class_translate ();
1357   reorder_important_classes ();
1358   setup_reg_class_relations ();
1359 }
1360
1361 \f
1362
1363 /* Set up the array above.  */
1364 static void
1365 setup_hard_regno_aclass (void)
1366 {
1367   int i;
1368
1369   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1370     {
1371 #if 1
1372       ira_hard_regno_allocno_class[i]
1373         = (TEST_HARD_REG_BIT (no_unit_alloc_regs, i)
1374            ? NO_REGS
1375            : ira_allocno_class_translate[REGNO_REG_CLASS (i)]);
1376 #else
1377       int j;
1378       enum reg_class cl;
1379       ira_hard_regno_allocno_class[i] = NO_REGS;
1380       for (j = 0; j < ira_allocno_classes_num; j++)
1381         {
1382           cl = ira_allocno_classes[j];
1383           if (ira_class_hard_reg_index[cl][i] >= 0)
1384             {
1385               ira_hard_regno_allocno_class[i] = cl;
1386               break;
1387             }
1388         }
1389 #endif
1390     }
1391 }
1392
1393 \f
1394
1395 /* Form IRA_REG_CLASS_MAX_NREGS and IRA_REG_CLASS_MIN_NREGS maps.  */
1396 static void
1397 setup_reg_class_nregs (void)
1398 {
1399   int i, cl, cl2, m;
1400
1401   for (m = 0; m < MAX_MACHINE_MODE; m++)
1402     {
1403       for (cl = 0; cl < N_REG_CLASSES; cl++)
1404         ira_reg_class_max_nregs[cl][m]
1405           = ira_reg_class_min_nregs[cl][m]
1406           = targetm.class_max_nregs ((reg_class_t) cl, (enum machine_mode) m);
1407       for (cl = 0; cl < N_REG_CLASSES; cl++)
1408         for (i = 0;
1409              (cl2 = alloc_reg_class_subclasses[cl][i]) != LIM_REG_CLASSES;
1410              i++)
1411           if (ira_reg_class_min_nregs[cl2][m]
1412               < ira_reg_class_min_nregs[cl][m])
1413             ira_reg_class_min_nregs[cl][m] = ira_reg_class_min_nregs[cl2][m];
1414     }
1415 }
1416
1417 \f
1418
1419 /* Set up IRA_PROHIBITED_CLASS_MODE_REGS.  */
1420 static void
1421 setup_prohibited_class_mode_regs (void)
1422 {
1423   int j, k, hard_regno, cl;
1424
1425   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
1426     {
1427       for (j = 0; j < NUM_MACHINE_MODES; j++)
1428         {
1429           CLEAR_HARD_REG_SET (ira_prohibited_class_mode_regs[cl][j]);
1430           for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1431             {
1432               hard_regno = ira_class_hard_regs[cl][k];
1433               if (! HARD_REGNO_MODE_OK (hard_regno, (enum machine_mode) j))
1434                 SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1435                                   hard_regno);
1436             }
1437         }
1438     }
1439 }
1440
1441 /* Clarify IRA_PROHIBITED_CLASS_MODE_REGS by excluding hard registers
1442    spanning from one register pressure class to another one.  It is
1443    called after defining the pressure classes.  */
1444 static void
1445 clarify_prohibited_class_mode_regs (void)
1446 {
1447   int j, k, hard_regno, cl, pclass, nregs;
1448
1449   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
1450     for (j = 0; j < NUM_MACHINE_MODES; j++)
1451       for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1452         {
1453           hard_regno = ira_class_hard_regs[cl][k];
1454           if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j], hard_regno))
1455             continue;
1456           nregs = hard_regno_nregs[hard_regno][j];
1457           if (hard_regno + nregs > FIRST_PSEUDO_REGISTER)
1458             {
1459               SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1460                                 hard_regno);
1461                continue;
1462             }
1463           pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
1464           for (nregs-- ;nregs >= 0; nregs--)
1465             if (((enum reg_class) pclass
1466                  != ira_pressure_class_translate[REGNO_REG_CLASS
1467                                                  (hard_regno + nregs)]))
1468               {
1469                 SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1470                                   hard_regno);
1471                 break;
1472               }
1473         }
1474 }
1475
1476 \f
1477
1478 /* Allocate and initialize IRA_REGISTER_MOVE_COST,
1479    IRA_MAX_REGISTER_MOVE_COST, IRA_MAY_MOVE_IN_COST,
1480    IRA_MAY_MOVE_OUT_COST, IRA_MAX_MAY_MOVE_IN_COST, and
1481    IRA_MAX_MAY_MOVE_OUT_COST for MODE if it is not done yet.  */
1482 void
1483 ira_init_register_move_cost (enum machine_mode mode)
1484 {
1485   int cl1, cl2, cl3;
1486
1487   ira_assert (ira_register_move_cost[mode] == NULL
1488               && ira_max_register_move_cost[mode] == NULL
1489               && ira_may_move_in_cost[mode] == NULL
1490               && ira_may_move_out_cost[mode] == NULL
1491               && ira_max_may_move_in_cost[mode] == NULL
1492               && ira_max_may_move_out_cost[mode] == NULL);
1493   if (move_cost[mode] == NULL)
1494     init_move_cost (mode);
1495   ira_register_move_cost[mode] = move_cost[mode];
1496   /* Don't use ira_allocate because the tables exist out of scope of a
1497      IRA call.  */
1498   ira_max_register_move_cost[mode]
1499     = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1500   memcpy (ira_max_register_move_cost[mode], ira_register_move_cost[mode],
1501           sizeof (move_table) * N_REG_CLASSES);
1502   for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1503     {
1504       /* Some subclasses are to small to have enough registers to hold
1505          a value of MODE.  Just ignore them.  */
1506       if (ira_reg_class_max_nregs[cl1][mode] > ira_available_class_regs[cl1])
1507         continue;
1508       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl1]);
1509       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1510       if (hard_reg_set_empty_p (temp_hard_regset))
1511         continue;
1512       for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1513         if (hard_reg_set_subset_p (reg_class_contents[cl1],
1514                                    reg_class_contents[cl2]))
1515           for (cl3 = 0; cl3 < N_REG_CLASSES; cl3++)
1516             {
1517               if (ira_max_register_move_cost[mode][cl2][cl3]
1518                   < ira_register_move_cost[mode][cl1][cl3])
1519                 ira_max_register_move_cost[mode][cl2][cl3]
1520                   = ira_register_move_cost[mode][cl1][cl3];
1521               if (ira_max_register_move_cost[mode][cl3][cl2]
1522                   < ira_register_move_cost[mode][cl3][cl1])
1523                 ira_max_register_move_cost[mode][cl3][cl2]
1524                   = ira_register_move_cost[mode][cl3][cl1];
1525             }
1526     }
1527   ira_may_move_in_cost[mode]
1528     = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1529   memcpy (ira_may_move_in_cost[mode], may_move_in_cost[mode],
1530           sizeof (move_table) * N_REG_CLASSES);
1531   ira_may_move_out_cost[mode]
1532     = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1533   memcpy (ira_may_move_out_cost[mode], may_move_out_cost[mode],
1534           sizeof (move_table) * N_REG_CLASSES);
1535   ira_max_may_move_in_cost[mode]
1536     = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1537   memcpy (ira_max_may_move_in_cost[mode], ira_max_register_move_cost[mode],
1538           sizeof (move_table) * N_REG_CLASSES);
1539   ira_max_may_move_out_cost[mode]
1540     = (move_table *) xmalloc (sizeof (move_table) * N_REG_CLASSES);
1541   memcpy (ira_max_may_move_out_cost[mode], ira_max_register_move_cost[mode],
1542           sizeof (move_table) * N_REG_CLASSES);
1543   for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1544     {
1545       for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1546         {
1547           COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl2]);
1548           AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1549           if (hard_reg_set_empty_p (temp_hard_regset))
1550             continue;
1551           if (ira_class_subset_p[cl1][cl2])
1552             ira_may_move_in_cost[mode][cl1][cl2] = 0;
1553           if (ira_class_subset_p[cl2][cl1])
1554             ira_may_move_out_cost[mode][cl1][cl2] = 0;
1555           if (ira_class_subset_p[cl1][cl2])
1556             ira_max_may_move_in_cost[mode][cl1][cl2] = 0;
1557           if (ira_class_subset_p[cl2][cl1])
1558             ira_max_may_move_out_cost[mode][cl1][cl2] = 0;
1559           ira_register_move_cost[mode][cl1][cl2]
1560             = ira_max_register_move_cost[mode][cl1][cl2];
1561           ira_may_move_in_cost[mode][cl1][cl2]
1562             = ira_max_may_move_in_cost[mode][cl1][cl2];
1563           ira_may_move_out_cost[mode][cl1][cl2]
1564             = ira_max_may_move_out_cost[mode][cl1][cl2];
1565         }
1566     }
1567 }
1568
1569 \f
1570
1571 /* This is called once during compiler work.  It sets up
1572    different arrays whose values don't depend on the compiled
1573    function.  */
1574 void
1575 ira_init_once (void)
1576 {
1577   int mode;
1578
1579   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1580     {
1581       ira_register_move_cost[mode] = NULL;
1582       ira_max_register_move_cost[mode] = NULL;
1583       ira_may_move_in_cost[mode] = NULL;
1584       ira_may_move_out_cost[mode] = NULL;
1585       ira_max_may_move_in_cost[mode] = NULL;
1586       ira_max_may_move_out_cost[mode] = NULL;
1587     }
1588   ira_init_costs_once ();
1589 }
1590
1591 /* Free ira_max_register_move_cost, ira_may_move_in_cost,
1592    ira_may_move_out_cost, ira_max_may_move_in_cost, and
1593    ira_max_may_move_out_cost for each mode.  */
1594 static void
1595 free_register_move_costs (void)
1596 {
1597   int mode;
1598
1599   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1600     {
1601       free (ira_max_register_move_cost[mode]);
1602       free (ira_may_move_in_cost[mode]);
1603       free (ira_may_move_out_cost[mode]);
1604       free (ira_max_may_move_in_cost[mode]);
1605       free (ira_max_may_move_out_cost[mode]);
1606       ira_register_move_cost[mode] = NULL;
1607       ira_max_register_move_cost[mode] = NULL;
1608       ira_may_move_in_cost[mode] = NULL;
1609       ira_may_move_out_cost[mode] = NULL;
1610       ira_max_may_move_in_cost[mode] = NULL;
1611       ira_max_may_move_out_cost[mode] = NULL;
1612     }
1613 }
1614
1615 /* This is called every time when register related information is
1616    changed.  */
1617 void
1618 ira_init (void)
1619 {
1620   free_register_move_costs ();
1621   setup_reg_mode_hard_regset ();
1622   setup_alloc_regs (flag_omit_frame_pointer != 0);
1623   setup_class_subset_and_memory_move_costs ();
1624   setup_reg_class_nregs ();
1625   setup_prohibited_class_mode_regs ();
1626   find_reg_classes ();
1627   clarify_prohibited_class_mode_regs ();
1628   setup_hard_regno_aclass ();
1629   ira_init_costs ();
1630 }
1631
1632 /* Function called once at the end of compiler work.  */
1633 void
1634 ira_finish_once (void)
1635 {
1636   ira_finish_costs_once ();
1637   free_register_move_costs ();
1638 }
1639
1640 \f
1641 #define ira_prohibited_mode_move_regs_initialized_p \
1642   (this_target_ira_int->x_ira_prohibited_mode_move_regs_initialized_p)
1643
1644 /* Set up IRA_PROHIBITED_MODE_MOVE_REGS.  */
1645 static void
1646 setup_prohibited_mode_move_regs (void)
1647 {
1648   int i, j;
1649   rtx test_reg1, test_reg2, move_pat, move_insn;
1650
1651   if (ira_prohibited_mode_move_regs_initialized_p)
1652     return;
1653   ira_prohibited_mode_move_regs_initialized_p = true;
1654   test_reg1 = gen_rtx_REG (VOIDmode, 0);
1655   test_reg2 = gen_rtx_REG (VOIDmode, 0);
1656   move_pat = gen_rtx_SET (VOIDmode, test_reg1, test_reg2);
1657   move_insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, 0, move_pat, 0, -1, 0);
1658   for (i = 0; i < NUM_MACHINE_MODES; i++)
1659     {
1660       SET_HARD_REG_SET (ira_prohibited_mode_move_regs[i]);
1661       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1662         {
1663           if (! HARD_REGNO_MODE_OK (j, (enum machine_mode) i))
1664             continue;
1665           SET_REGNO_RAW (test_reg1, j);
1666           PUT_MODE (test_reg1, (enum machine_mode) i);
1667           SET_REGNO_RAW (test_reg2, j);
1668           PUT_MODE (test_reg2, (enum machine_mode) i);
1669           INSN_CODE (move_insn) = -1;
1670           recog_memoized (move_insn);
1671           if (INSN_CODE (move_insn) < 0)
1672             continue;
1673           extract_insn (move_insn);
1674           if (! constrain_operands (1))
1675             continue;
1676           CLEAR_HARD_REG_BIT (ira_prohibited_mode_move_regs[i], j);
1677         }
1678     }
1679 }
1680
1681 \f
1682
1683 /* Return nonzero if REGNO is a particularly bad choice for reloading X.  */
1684 static bool
1685 ira_bad_reload_regno_1 (int regno, rtx x)
1686 {
1687   int x_regno, n, i;
1688   ira_allocno_t a;
1689   enum reg_class pref;
1690
1691   /* We only deal with pseudo regs.  */
1692   if (! x || GET_CODE (x) != REG)
1693     return false;
1694
1695   x_regno = REGNO (x);
1696   if (x_regno < FIRST_PSEUDO_REGISTER)
1697     return false;
1698
1699   /* If the pseudo prefers REGNO explicitly, then do not consider
1700      REGNO a bad spill choice.  */
1701   pref = reg_preferred_class (x_regno);
1702   if (reg_class_size[pref] == 1)
1703     return !TEST_HARD_REG_BIT (reg_class_contents[pref], regno);
1704
1705   /* If the pseudo conflicts with REGNO, then we consider REGNO a
1706      poor choice for a reload regno.  */
1707   a = ira_regno_allocno_map[x_regno];
1708   n = ALLOCNO_NUM_OBJECTS (a);
1709   for (i = 0; i < n; i++)
1710     {
1711       ira_object_t obj = ALLOCNO_OBJECT (a, i);
1712       if (TEST_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), regno))
1713         return true;
1714     }
1715   return false;
1716 }
1717
1718 /* Return nonzero if REGNO is a particularly bad choice for reloading
1719    IN or OUT.  */
1720 bool
1721 ira_bad_reload_regno (int regno, rtx in, rtx out)
1722 {
1723   return (ira_bad_reload_regno_1 (regno, in)
1724           || ira_bad_reload_regno_1 (regno, out));
1725 }
1726
1727 /* Return TRUE if *LOC contains an asm.  */
1728 static int
1729 insn_contains_asm_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
1730 {
1731   if ( !*loc)
1732     return FALSE;
1733   if (GET_CODE (*loc) == ASM_OPERANDS)
1734     return TRUE;
1735   return FALSE;
1736 }
1737
1738
1739 /* Return TRUE if INSN contains an ASM.  */
1740 static bool
1741 insn_contains_asm (rtx insn)
1742 {
1743   return for_each_rtx (&insn, insn_contains_asm_1, NULL);
1744 }
1745
1746 /* Add register clobbers from asm statements.  */
1747 static void
1748 compute_regs_asm_clobbered (void)
1749 {
1750   basic_block bb;
1751
1752   FOR_EACH_BB (bb)
1753     {
1754       rtx insn;
1755       FOR_BB_INSNS_REVERSE (bb, insn)
1756         {
1757           df_ref *def_rec;
1758
1759           if (insn_contains_asm (insn))
1760             for (def_rec = DF_INSN_DEFS (insn); *def_rec; def_rec++)
1761               {
1762                 df_ref def = *def_rec;
1763                 unsigned int dregno = DF_REF_REGNO (def);
1764                 if (HARD_REGISTER_NUM_P (dregno))
1765                   add_to_hard_reg_set (&crtl->asm_clobbers,
1766                                        GET_MODE (DF_REF_REAL_REG (def)),
1767                                        dregno);
1768               }
1769         }
1770     }
1771 }
1772
1773
1774 /* Set up ELIMINABLE_REGSET, IRA_NO_ALLOC_REGS, and REGS_EVER_LIVE.  */
1775 void
1776 ira_setup_eliminable_regset (void)
1777 {
1778 #ifdef ELIMINABLE_REGS
1779   int i;
1780   static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
1781 #endif
1782   /* FIXME: If EXIT_IGNORE_STACK is set, we will not save and restore
1783      sp for alloca.  So we can't eliminate the frame pointer in that
1784      case.  At some point, we should improve this by emitting the
1785      sp-adjusting insns for this case.  */
1786   int need_fp
1787     = (! flag_omit_frame_pointer
1788        || (cfun->calls_alloca && EXIT_IGNORE_STACK)
1789        /* We need the frame pointer to catch stack overflow exceptions
1790           if the stack pointer is moving.  */
1791        || (flag_stack_check && STACK_CHECK_MOVING_SP)
1792        || crtl->accesses_prior_frames
1793        || crtl->stack_realign_needed
1794        || targetm.frame_pointer_required ());
1795
1796   frame_pointer_needed = need_fp;
1797
1798   COPY_HARD_REG_SET (ira_no_alloc_regs, no_unit_alloc_regs);
1799   CLEAR_HARD_REG_SET (eliminable_regset);
1800
1801   compute_regs_asm_clobbered ();
1802
1803   /* Build the regset of all eliminable registers and show we can't
1804      use those that we already know won't be eliminated.  */
1805 #ifdef ELIMINABLE_REGS
1806   for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
1807     {
1808       bool cannot_elim
1809         = (! targetm.can_eliminate (eliminables[i].from, eliminables[i].to)
1810            || (eliminables[i].to == STACK_POINTER_REGNUM && need_fp));
1811
1812       if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, eliminables[i].from))
1813         {
1814             SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
1815
1816             if (cannot_elim)
1817               SET_HARD_REG_BIT (ira_no_alloc_regs, eliminables[i].from);
1818         }
1819       else if (cannot_elim)
1820         error ("%s cannot be used in asm here",
1821                reg_names[eliminables[i].from]);
1822       else
1823         df_set_regs_ever_live (eliminables[i].from, true);
1824     }
1825 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
1826   if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, HARD_FRAME_POINTER_REGNUM))
1827     {
1828       SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
1829       if (need_fp)
1830         SET_HARD_REG_BIT (ira_no_alloc_regs, HARD_FRAME_POINTER_REGNUM);
1831     }
1832   else if (need_fp)
1833     error ("%s cannot be used in asm here",
1834            reg_names[HARD_FRAME_POINTER_REGNUM]);
1835   else
1836     df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
1837 #endif
1838
1839 #else
1840   if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, HARD_FRAME_POINTER_REGNUM))
1841     {
1842       SET_HARD_REG_BIT (eliminable_regset, FRAME_POINTER_REGNUM);
1843       if (need_fp)
1844         SET_HARD_REG_BIT (ira_no_alloc_regs, FRAME_POINTER_REGNUM);
1845     }
1846   else if (need_fp)
1847     error ("%s cannot be used in asm here", reg_names[FRAME_POINTER_REGNUM]);
1848   else
1849     df_set_regs_ever_live (FRAME_POINTER_REGNUM, true);
1850 #endif
1851 }
1852
1853 \f
1854
1855 /* The length of the following two arrays.  */
1856 int ira_reg_equiv_len;
1857
1858 /* The element value is TRUE if the corresponding regno value is
1859    invariant.  */
1860 bool *ira_reg_equiv_invariant_p;
1861
1862 /* The element value is equiv constant of given pseudo-register or
1863    NULL_RTX.  */
1864 rtx *ira_reg_equiv_const;
1865
1866 /* Set up the two arrays declared above.  */
1867 static void
1868 find_reg_equiv_invariant_const (void)
1869 {
1870   unsigned int i;
1871   bool invariant_p;
1872   rtx list, insn, note, constant, x;
1873
1874   for (i = FIRST_PSEUDO_REGISTER; i < VEC_length (reg_equivs_t, reg_equivs); i++)
1875     {
1876       constant = NULL_RTX;
1877       invariant_p = false;
1878       for (list = reg_equiv_init (i); list != NULL_RTX; list = XEXP (list, 1))
1879         {
1880           insn = XEXP (list, 0);
1881           note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
1882
1883           if (note == NULL_RTX)
1884             continue;
1885
1886           x = XEXP (note, 0);
1887
1888           if (! CONSTANT_P (x)
1889               || ! flag_pic || LEGITIMATE_PIC_OPERAND_P (x))
1890             {
1891               /* It can happen that a REG_EQUIV note contains a MEM
1892                  that is not a legitimate memory operand.  As later
1893                  stages of the reload assume that all addresses found
1894                  in the reg_equiv_* arrays were originally legitimate,
1895                  we ignore such REG_EQUIV notes.  */
1896               if (memory_operand (x, VOIDmode))
1897                 invariant_p = MEM_READONLY_P (x);
1898               else if (function_invariant_p (x))
1899                 {
1900                   if (GET_CODE (x) == PLUS
1901                       || x == frame_pointer_rtx || x == arg_pointer_rtx)
1902                     invariant_p = true;
1903                   else
1904                     constant = x;
1905                 }
1906             }
1907         }
1908       ira_reg_equiv_invariant_p[i] = invariant_p;
1909       ira_reg_equiv_const[i] = constant;
1910     }
1911 }
1912
1913 \f
1914
1915 /* Vector of substitutions of register numbers,
1916    used to map pseudo regs into hardware regs.
1917    This is set up as a result of register allocation.
1918    Element N is the hard reg assigned to pseudo reg N,
1919    or is -1 if no hard reg was assigned.
1920    If N is a hard reg number, element N is N.  */
1921 short *reg_renumber;
1922
1923 /* Set up REG_RENUMBER and CALLER_SAVE_NEEDED (used by reload) from
1924    the allocation found by IRA.  */
1925 static void
1926 setup_reg_renumber (void)
1927 {
1928   int regno, hard_regno;
1929   ira_allocno_t a;
1930   ira_allocno_iterator ai;
1931
1932   caller_save_needed = 0;
1933   FOR_EACH_ALLOCNO (a, ai)
1934     {
1935       /* There are no caps at this point.  */
1936       ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
1937       if (! ALLOCNO_ASSIGNED_P (a))
1938         /* It can happen if A is not referenced but partially anticipated
1939            somewhere in a region.  */
1940         ALLOCNO_ASSIGNED_P (a) = true;
1941       ira_free_allocno_updated_costs (a);
1942       hard_regno = ALLOCNO_HARD_REGNO (a);
1943       regno = ALLOCNO_REGNO (a);
1944       reg_renumber[regno] = (hard_regno < 0 ? -1 : hard_regno);
1945       if (hard_regno >= 0)
1946         {
1947           int i, nwords;
1948           enum reg_class pclass;
1949           ira_object_t obj;
1950           
1951           pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
1952           nwords = ALLOCNO_NUM_OBJECTS (a);
1953           for (i = 0; i < nwords; i++)
1954             {
1955               obj = ALLOCNO_OBJECT (a, i);
1956               IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
1957                                       reg_class_contents[pclass]);
1958             }
1959           if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
1960               && ira_hard_reg_set_intersection_p (hard_regno, ALLOCNO_MODE (a),
1961                                                   call_used_reg_set))
1962             {
1963               ira_assert (!optimize || flag_caller_saves
1964                           || regno >= ira_reg_equiv_len
1965                           || ira_reg_equiv_const[regno]
1966                           || ira_reg_equiv_invariant_p[regno]);
1967               caller_save_needed = 1;
1968             }
1969         }
1970     }
1971 }
1972
1973 /* Set up allocno assignment flags for further allocation
1974    improvements.  */
1975 static void
1976 setup_allocno_assignment_flags (void)
1977 {
1978   int hard_regno;
1979   ira_allocno_t a;
1980   ira_allocno_iterator ai;
1981
1982   FOR_EACH_ALLOCNO (a, ai)
1983     {
1984       if (! ALLOCNO_ASSIGNED_P (a))
1985         /* It can happen if A is not referenced but partially anticipated
1986            somewhere in a region.  */
1987         ira_free_allocno_updated_costs (a);
1988       hard_regno = ALLOCNO_HARD_REGNO (a);
1989       /* Don't assign hard registers to allocnos which are destination
1990          of removed store at the end of loop.  It has no sense to keep
1991          the same value in different hard registers.  It is also
1992          impossible to assign hard registers correctly to such
1993          allocnos because the cost info and info about intersected
1994          calls are incorrect for them.  */
1995       ALLOCNO_ASSIGNED_P (a) = (hard_regno >= 0
1996                                 || ALLOCNO_EMIT_DATA (a)->mem_optimized_dest_p
1997                                 || (ALLOCNO_MEMORY_COST (a)
1998                                     - ALLOCNO_CLASS_COST (a)) < 0);
1999       ira_assert
2000         (hard_regno < 0
2001          || ira_hard_reg_in_set_p (hard_regno, ALLOCNO_MODE (a),
2002                                    reg_class_contents[ALLOCNO_CLASS (a)]));
2003     }
2004 }
2005
2006 /* Evaluate overall allocation cost and the costs for using hard
2007    registers and memory for allocnos.  */
2008 static void
2009 calculate_allocation_cost (void)
2010 {
2011   int hard_regno, cost;
2012   ira_allocno_t a;
2013   ira_allocno_iterator ai;
2014
2015   ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
2016   FOR_EACH_ALLOCNO (a, ai)
2017     {
2018       hard_regno = ALLOCNO_HARD_REGNO (a);
2019       ira_assert (hard_regno < 0
2020                   || (ira_hard_reg_in_set_p
2021                       (hard_regno, ALLOCNO_MODE (a),
2022                        reg_class_contents[ALLOCNO_CLASS (a)])));
2023       if (hard_regno < 0)
2024         {
2025           cost = ALLOCNO_MEMORY_COST (a);
2026           ira_mem_cost += cost;
2027         }
2028       else if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
2029         {
2030           cost = (ALLOCNO_HARD_REG_COSTS (a)
2031                   [ira_class_hard_reg_index
2032                    [ALLOCNO_CLASS (a)][hard_regno]]);
2033           ira_reg_cost += cost;
2034         }
2035       else
2036         {
2037           cost = ALLOCNO_CLASS_COST (a);
2038           ira_reg_cost += cost;
2039         }
2040       ira_overall_cost += cost;
2041     }
2042
2043   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2044     {
2045       fprintf (ira_dump_file,
2046                "+++Costs: overall %d, reg %d, mem %d, ld %d, st %d, move %d\n",
2047                ira_overall_cost, ira_reg_cost, ira_mem_cost,
2048                ira_load_cost, ira_store_cost, ira_shuffle_cost);
2049       fprintf (ira_dump_file, "+++       move loops %d, new jumps %d\n",
2050                ira_move_loops_num, ira_additional_jumps_num);
2051     }
2052
2053 }
2054
2055 #ifdef ENABLE_IRA_CHECKING
2056 /* Check the correctness of the allocation.  We do need this because
2057    of complicated code to transform more one region internal
2058    representation into one region representation.  */
2059 static void
2060 check_allocation (void)
2061 {
2062   ira_allocno_t a;
2063   int hard_regno, nregs, conflict_nregs;
2064   ira_allocno_iterator ai;
2065
2066   FOR_EACH_ALLOCNO (a, ai)
2067     {
2068       int n = ALLOCNO_NUM_OBJECTS (a);
2069       int i;
2070
2071       if (ALLOCNO_CAP_MEMBER (a) != NULL
2072           || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
2073         continue;
2074       nregs = hard_regno_nregs[hard_regno][ALLOCNO_MODE (a)];
2075       if (nregs == 1)
2076         /* We allocated a single hard register.  */
2077         n = 1;
2078       else if (n > 1)
2079         /* We allocated multiple hard registers, and we will test
2080            conflicts in a granularity of single hard regs.  */
2081         nregs = 1;
2082
2083       for (i = 0; i < n; i++)
2084         {
2085           ira_object_t obj = ALLOCNO_OBJECT (a, i);
2086           ira_object_t conflict_obj;
2087           ira_object_conflict_iterator oci;
2088           int this_regno = hard_regno;
2089           if (n > 1)
2090             {
2091               if (WORDS_BIG_ENDIAN)
2092                 this_regno += n - i - 1;
2093               else
2094                 this_regno += i;
2095             }
2096           FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2097             {
2098               ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2099               int conflict_hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
2100               if (conflict_hard_regno < 0)
2101                 continue;
2102
2103               conflict_nregs
2104                 = (hard_regno_nregs
2105                    [conflict_hard_regno][ALLOCNO_MODE (conflict_a)]);
2106
2107               if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1
2108                   && conflict_nregs == ALLOCNO_NUM_OBJECTS (conflict_a))
2109                 {
2110                   if (WORDS_BIG_ENDIAN)
2111                     conflict_hard_regno += (ALLOCNO_NUM_OBJECTS (conflict_a)
2112                                             - OBJECT_SUBWORD (conflict_obj) - 1);
2113                   else
2114                     conflict_hard_regno += OBJECT_SUBWORD (conflict_obj);
2115                   conflict_nregs = 1;
2116                 }
2117
2118               if ((conflict_hard_regno <= this_regno
2119                  && this_regno < conflict_hard_regno + conflict_nregs)
2120                 || (this_regno <= conflict_hard_regno
2121                     && conflict_hard_regno < this_regno + nregs))
2122                 {
2123                   fprintf (stderr, "bad allocation for %d and %d\n",
2124                            ALLOCNO_REGNO (a), ALLOCNO_REGNO (conflict_a));
2125                   gcc_unreachable ();
2126                 }
2127             }
2128         }
2129     }
2130 }
2131 #endif
2132
2133 /* Fix values of array REG_EQUIV_INIT after live range splitting done
2134    by IRA.  */
2135 static void
2136 fix_reg_equiv_init (void)
2137 {
2138   unsigned int max_regno = max_reg_num ();
2139   int i, new_regno, max;
2140   rtx x, prev, next, insn, set;
2141
2142   if (VEC_length (reg_equivs_t, reg_equivs) < max_regno)
2143     {
2144       max = VEC_length (reg_equivs_t, reg_equivs);
2145       grow_reg_equivs ();
2146       for (i = FIRST_PSEUDO_REGISTER; i < max; i++)
2147         for (prev = NULL_RTX, x = reg_equiv_init (i);
2148              x != NULL_RTX;
2149              x = next)
2150           {
2151             next = XEXP (x, 1);
2152             insn = XEXP (x, 0);
2153             set = single_set (insn);
2154             ira_assert (set != NULL_RTX
2155                         && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))));
2156             if (REG_P (SET_DEST (set))
2157                 && ((int) REGNO (SET_DEST (set)) == i
2158                     || (int) ORIGINAL_REGNO (SET_DEST (set)) == i))
2159               new_regno = REGNO (SET_DEST (set));
2160             else if (REG_P (SET_SRC (set))
2161                      && ((int) REGNO (SET_SRC (set)) == i
2162                          || (int) ORIGINAL_REGNO (SET_SRC (set)) == i))
2163               new_regno = REGNO (SET_SRC (set));
2164             else
2165               gcc_unreachable ();
2166             if (new_regno == i)
2167               prev = x;
2168             else
2169               {
2170                 if (prev == NULL_RTX)
2171                   reg_equiv_init (i) = next;
2172                 else
2173                   XEXP (prev, 1) = next;
2174                 XEXP (x, 1) = reg_equiv_init (new_regno);
2175                 reg_equiv_init (new_regno) = x;
2176               }
2177           }
2178     }
2179 }
2180
2181 #ifdef ENABLE_IRA_CHECKING
2182 /* Print redundant memory-memory copies.  */
2183 static void
2184 print_redundant_copies (void)
2185 {
2186   int hard_regno;
2187   ira_allocno_t a;
2188   ira_copy_t cp, next_cp;
2189   ira_allocno_iterator ai;
2190
2191   FOR_EACH_ALLOCNO (a, ai)
2192     {
2193       if (ALLOCNO_CAP_MEMBER (a) != NULL)
2194         /* It is a cap. */
2195         continue;
2196       hard_regno = ALLOCNO_HARD_REGNO (a);
2197       if (hard_regno >= 0)
2198         continue;
2199       for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2200         if (cp->first == a)
2201           next_cp = cp->next_first_allocno_copy;
2202         else
2203           {
2204             next_cp = cp->next_second_allocno_copy;
2205             if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL
2206                 && cp->insn != NULL_RTX
2207                 && ALLOCNO_HARD_REGNO (cp->first) == hard_regno)
2208               fprintf (ira_dump_file,
2209                        "        Redundant move from %d(freq %d):%d\n",
2210                        INSN_UID (cp->insn), cp->freq, hard_regno);
2211           }
2212     }
2213 }
2214 #endif
2215
2216 /* Setup preferred and alternative classes for new pseudo-registers
2217    created by IRA starting with START.  */
2218 static void
2219 setup_preferred_alternate_classes_for_new_pseudos (int start)
2220 {
2221   int i, old_regno;
2222   int max_regno = max_reg_num ();
2223
2224   for (i = start; i < max_regno; i++)
2225     {
2226       old_regno = ORIGINAL_REGNO (regno_reg_rtx[i]);
2227       ira_assert (i != old_regno);
2228       setup_reg_classes (i, reg_preferred_class (old_regno),
2229                          reg_alternate_class (old_regno),
2230                          reg_allocno_class (old_regno));
2231       if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2232         fprintf (ira_dump_file,
2233                  "    New r%d: setting preferred %s, alternative %s\n",
2234                  i, reg_class_names[reg_preferred_class (old_regno)],
2235                  reg_class_names[reg_alternate_class (old_regno)]);
2236     }
2237 }
2238
2239 \f
2240
2241 /* Regional allocation can create new pseudo-registers.  This function
2242    expands some arrays for pseudo-registers.  */
2243 static void
2244 expand_reg_info (int old_size)
2245 {
2246   int i;
2247   int size = max_reg_num ();
2248
2249   resize_reg_info ();
2250   for (i = old_size; i < size; i++)
2251     setup_reg_classes (i, GENERAL_REGS, ALL_REGS, GENERAL_REGS);
2252 }
2253
2254 /* Return TRUE if there is too high register pressure in the function.
2255    It is used to decide when stack slot sharing is worth to do.  */
2256 static bool
2257 too_high_register_pressure_p (void)
2258 {
2259   int i;
2260   enum reg_class pclass;
2261
2262   for (i = 0; i < ira_pressure_classes_num; i++)
2263     {
2264       pclass = ira_pressure_classes[i];
2265       if (ira_loop_tree_root->reg_pressure[pclass] > 10000)
2266         return true;
2267     }
2268   return false;
2269 }
2270
2271 \f
2272
2273 /* Indicate that hard register number FROM was eliminated and replaced with
2274    an offset from hard register number TO.  The status of hard registers live
2275    at the start of a basic block is updated by replacing a use of FROM with
2276    a use of TO.  */
2277
2278 void
2279 mark_elimination (int from, int to)
2280 {
2281   basic_block bb;
2282
2283   FOR_EACH_BB (bb)
2284     {
2285       /* We don't use LIVE info in IRA.  */
2286       bitmap r = DF_LR_IN (bb);
2287
2288       if (REGNO_REG_SET_P (r, from))
2289         {
2290           CLEAR_REGNO_REG_SET (r, from);
2291           SET_REGNO_REG_SET (r, to);
2292         }
2293     }
2294 }
2295
2296 \f
2297
2298 struct equivalence
2299 {
2300   /* Set when a REG_EQUIV note is found or created.  Use to
2301      keep track of what memory accesses might be created later,
2302      e.g. by reload.  */
2303   rtx replacement;
2304   rtx *src_p;
2305   /* The list of each instruction which initializes this register.  */
2306   rtx init_insns;
2307   /* Loop depth is used to recognize equivalences which appear
2308      to be present within the same loop (or in an inner loop).  */
2309   int loop_depth;
2310   /* Nonzero if this had a preexisting REG_EQUIV note.  */
2311   int is_arg_equivalence;
2312   /* Set when an attempt should be made to replace a register
2313      with the associated src_p entry.  */
2314   char replace;
2315 };
2316
2317 /* reg_equiv[N] (where N is a pseudo reg number) is the equivalence
2318    structure for that register.  */
2319 static struct equivalence *reg_equiv;
2320
2321 /* Used for communication between the following two functions: contains
2322    a MEM that we wish to ensure remains unchanged.  */
2323 static rtx equiv_mem;
2324
2325 /* Set nonzero if EQUIV_MEM is modified.  */
2326 static int equiv_mem_modified;
2327
2328 /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
2329    Called via note_stores.  */
2330 static void
2331 validate_equiv_mem_from_store (rtx dest, const_rtx set ATTRIBUTE_UNUSED,
2332                                void *data ATTRIBUTE_UNUSED)
2333 {
2334   if ((REG_P (dest)
2335        && reg_overlap_mentioned_p (dest, equiv_mem))
2336       || (MEM_P (dest)
2337           && true_dependence (dest, VOIDmode, equiv_mem, rtx_varies_p)))
2338     equiv_mem_modified = 1;
2339 }
2340
2341 /* Verify that no store between START and the death of REG invalidates
2342    MEMREF.  MEMREF is invalidated by modifying a register used in MEMREF,
2343    by storing into an overlapping memory location, or with a non-const
2344    CALL_INSN.
2345
2346    Return 1 if MEMREF remains valid.  */
2347 static int
2348 validate_equiv_mem (rtx start, rtx reg, rtx memref)
2349 {
2350   rtx insn;
2351   rtx note;
2352
2353   equiv_mem = memref;
2354   equiv_mem_modified = 0;
2355
2356   /* If the memory reference has side effects or is volatile, it isn't a
2357      valid equivalence.  */
2358   if (side_effects_p (memref))
2359     return 0;
2360
2361   for (insn = start; insn && ! equiv_mem_modified; insn = NEXT_INSN (insn))
2362     {
2363       if (! INSN_P (insn))
2364         continue;
2365
2366       if (find_reg_note (insn, REG_DEAD, reg))
2367         return 1;
2368
2369       /* This used to ignore readonly memory and const/pure calls.  The problem
2370          is the equivalent form may reference a pseudo which gets assigned a
2371          call clobbered hard reg.  When we later replace REG with its
2372          equivalent form, the value in the call-clobbered reg has been
2373          changed and all hell breaks loose.  */
2374       if (CALL_P (insn))
2375         return 0;
2376
2377       note_stores (PATTERN (insn), validate_equiv_mem_from_store, NULL);
2378
2379       /* If a register mentioned in MEMREF is modified via an
2380          auto-increment, we lose the equivalence.  Do the same if one
2381          dies; although we could extend the life, it doesn't seem worth
2382          the trouble.  */
2383
2384       for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2385         if ((REG_NOTE_KIND (note) == REG_INC
2386              || REG_NOTE_KIND (note) == REG_DEAD)
2387             && REG_P (XEXP (note, 0))
2388             && reg_overlap_mentioned_p (XEXP (note, 0), memref))
2389           return 0;
2390     }
2391
2392   return 0;
2393 }
2394
2395 /* Returns zero if X is known to be invariant.  */
2396 static int
2397 equiv_init_varies_p (rtx x)
2398 {
2399   RTX_CODE code = GET_CODE (x);
2400   int i;
2401   const char *fmt;
2402
2403   switch (code)
2404     {
2405     case MEM:
2406       return !MEM_READONLY_P (x) || equiv_init_varies_p (XEXP (x, 0));
2407
2408     case CONST:
2409     case CONST_INT:
2410     case CONST_DOUBLE:
2411     case CONST_FIXED:
2412     case CONST_VECTOR:
2413     case SYMBOL_REF:
2414     case LABEL_REF:
2415       return 0;
2416
2417     case REG:
2418       return reg_equiv[REGNO (x)].replace == 0 && rtx_varies_p (x, 0);
2419
2420     case ASM_OPERANDS:
2421       if (MEM_VOLATILE_P (x))
2422         return 1;
2423
2424       /* Fall through.  */
2425
2426     default:
2427       break;
2428     }
2429
2430   fmt = GET_RTX_FORMAT (code);
2431   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2432     if (fmt[i] == 'e')
2433       {
2434         if (equiv_init_varies_p (XEXP (x, i)))
2435           return 1;
2436       }
2437     else if (fmt[i] == 'E')
2438       {
2439         int j;
2440         for (j = 0; j < XVECLEN (x, i); j++)
2441           if (equiv_init_varies_p (XVECEXP (x, i, j)))
2442             return 1;
2443       }
2444
2445   return 0;
2446 }
2447
2448 /* Returns nonzero if X (used to initialize register REGNO) is movable.
2449    X is only movable if the registers it uses have equivalent initializations
2450    which appear to be within the same loop (or in an inner loop) and movable
2451    or if they are not candidates for local_alloc and don't vary.  */
2452 static int
2453 equiv_init_movable_p (rtx x, int regno)
2454 {
2455   int i, j;
2456   const char *fmt;
2457   enum rtx_code code = GET_CODE (x);
2458
2459   switch (code)
2460     {
2461     case SET:
2462       return equiv_init_movable_p (SET_SRC (x), regno);
2463
2464     case CC0:
2465     case CLOBBER:
2466       return 0;
2467
2468     case PRE_INC:
2469     case PRE_DEC:
2470     case POST_INC:
2471     case POST_DEC:
2472     case PRE_MODIFY:
2473     case POST_MODIFY:
2474       return 0;
2475
2476     case REG:
2477       return ((reg_equiv[REGNO (x)].loop_depth >= reg_equiv[regno].loop_depth
2478                && reg_equiv[REGNO (x)].replace)
2479               || (REG_BASIC_BLOCK (REGNO (x)) < NUM_FIXED_BLOCKS
2480                   && ! rtx_varies_p (x, 0)));
2481
2482     case UNSPEC_VOLATILE:
2483       return 0;
2484
2485     case ASM_OPERANDS:
2486       if (MEM_VOLATILE_P (x))
2487         return 0;
2488
2489       /* Fall through.  */
2490
2491     default:
2492       break;
2493     }
2494
2495   fmt = GET_RTX_FORMAT (code);
2496   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2497     switch (fmt[i])
2498       {
2499       case 'e':
2500         if (! equiv_init_movable_p (XEXP (x, i), regno))
2501           return 0;
2502         break;
2503       case 'E':
2504         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2505           if (! equiv_init_movable_p (XVECEXP (x, i, j), regno))
2506             return 0;
2507         break;
2508       }
2509
2510   return 1;
2511 }
2512
2513 /* TRUE if X uses any registers for which reg_equiv[REGNO].replace is
2514    true.  */
2515 static int
2516 contains_replace_regs (rtx x)
2517 {
2518   int i, j;
2519   const char *fmt;
2520   enum rtx_code code = GET_CODE (x);
2521
2522   switch (code)
2523     {
2524     case CONST_INT:
2525     case CONST:
2526     case LABEL_REF:
2527     case SYMBOL_REF:
2528     case CONST_DOUBLE:
2529     case CONST_FIXED:
2530     case CONST_VECTOR:
2531     case PC:
2532     case CC0:
2533     case HIGH:
2534       return 0;
2535
2536     case REG:
2537       return reg_equiv[REGNO (x)].replace;
2538
2539     default:
2540       break;
2541     }
2542
2543   fmt = GET_RTX_FORMAT (code);
2544   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2545     switch (fmt[i])
2546       {
2547       case 'e':
2548         if (contains_replace_regs (XEXP (x, i)))
2549           return 1;
2550         break;
2551       case 'E':
2552         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2553           if (contains_replace_regs (XVECEXP (x, i, j)))
2554             return 1;
2555         break;
2556       }
2557
2558   return 0;
2559 }
2560
2561 /* TRUE if X references a memory location that would be affected by a store
2562    to MEMREF.  */
2563 static int
2564 memref_referenced_p (rtx memref, rtx x)
2565 {
2566   int i, j;
2567   const char *fmt;
2568   enum rtx_code code = GET_CODE (x);
2569
2570   switch (code)
2571     {
2572     case CONST_INT:
2573     case CONST:
2574     case LABEL_REF:
2575     case SYMBOL_REF:
2576     case CONST_DOUBLE:
2577     case CONST_FIXED:
2578     case CONST_VECTOR:
2579     case PC:
2580     case CC0:
2581     case HIGH:
2582     case LO_SUM:
2583       return 0;
2584
2585     case REG:
2586       return (reg_equiv[REGNO (x)].replacement
2587               && memref_referenced_p (memref,
2588                                       reg_equiv[REGNO (x)].replacement));
2589
2590     case MEM:
2591       if (true_dependence (memref, VOIDmode, x, rtx_varies_p))
2592         return 1;
2593       break;
2594
2595     case SET:
2596       /* If we are setting a MEM, it doesn't count (its address does), but any
2597          other SET_DEST that has a MEM in it is referencing the MEM.  */
2598       if (MEM_P (SET_DEST (x)))
2599         {
2600           if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
2601             return 1;
2602         }
2603       else if (memref_referenced_p (memref, SET_DEST (x)))
2604         return 1;
2605
2606       return memref_referenced_p (memref, SET_SRC (x));
2607
2608     default:
2609       break;
2610     }
2611
2612   fmt = GET_RTX_FORMAT (code);
2613   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2614     switch (fmt[i])
2615       {
2616       case 'e':
2617         if (memref_referenced_p (memref, XEXP (x, i)))
2618           return 1;
2619         break;
2620       case 'E':
2621         for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2622           if (memref_referenced_p (memref, XVECEXP (x, i, j)))
2623             return 1;
2624         break;
2625       }
2626
2627   return 0;
2628 }
2629
2630 /* TRUE if some insn in the range (START, END] references a memory location
2631    that would be affected by a store to MEMREF.  */
2632 static int
2633 memref_used_between_p (rtx memref, rtx start, rtx end)
2634 {
2635   rtx insn;
2636
2637   for (insn = NEXT_INSN (start); insn != NEXT_INSN (end);
2638        insn = NEXT_INSN (insn))
2639     {
2640       if (!NONDEBUG_INSN_P (insn))
2641         continue;
2642
2643       if (memref_referenced_p (memref, PATTERN (insn)))
2644         return 1;
2645
2646       /* Nonconst functions may access memory.  */
2647       if (CALL_P (insn) && (! RTL_CONST_CALL_P (insn)))
2648         return 1;
2649     }
2650
2651   return 0;
2652 }
2653
2654 /* Mark REG as having no known equivalence.
2655    Some instructions might have been processed before and furnished
2656    with REG_EQUIV notes for this register; these notes will have to be
2657    removed.
2658    STORE is the piece of RTL that does the non-constant / conflicting
2659    assignment - a SET, CLOBBER or REG_INC note.  It is currently not used,
2660    but needs to be there because this function is called from note_stores.  */
2661 static void
2662 no_equiv (rtx reg, const_rtx store ATTRIBUTE_UNUSED,
2663           void *data ATTRIBUTE_UNUSED)
2664 {
2665   int regno;
2666   rtx list;
2667
2668   if (!REG_P (reg))
2669     return;
2670   regno = REGNO (reg);
2671   list = reg_equiv[regno].init_insns;
2672   if (list == const0_rtx)
2673     return;
2674   reg_equiv[regno].init_insns = const0_rtx;
2675   reg_equiv[regno].replacement = NULL_RTX;
2676   /* This doesn't matter for equivalences made for argument registers, we
2677      should keep their initialization insns.  */
2678   if (reg_equiv[regno].is_arg_equivalence)
2679     return;
2680   reg_equiv_init (regno) = NULL_RTX;
2681   for (; list; list =  XEXP (list, 1))
2682     {
2683       rtx insn = XEXP (list, 0);
2684       remove_note (insn, find_reg_note (insn, REG_EQUIV, NULL_RTX));
2685     }
2686 }
2687
2688 /* In DEBUG_INSN location adjust REGs from CLEARED_REGS bitmap to the
2689    equivalent replacement.  */
2690
2691 static rtx
2692 adjust_cleared_regs (rtx loc, const_rtx old_rtx ATTRIBUTE_UNUSED, void *data)
2693 {
2694   if (REG_P (loc))
2695     {
2696       bitmap cleared_regs = (bitmap) data;
2697       if (bitmap_bit_p (cleared_regs, REGNO (loc)))
2698         return simplify_replace_fn_rtx (*reg_equiv[REGNO (loc)].src_p,
2699                                         NULL_RTX, adjust_cleared_regs, data);
2700     }
2701   return NULL_RTX;
2702 }
2703
2704 /* Nonzero if we recorded an equivalence for a LABEL_REF.  */
2705 static int recorded_label_ref;
2706
2707 /* Find registers that are equivalent to a single value throughout the
2708    compilation (either because they can be referenced in memory or are
2709    set once from a single constant).  Lower their priority for a
2710    register.
2711
2712    If such a register is only referenced once, try substituting its
2713    value into the using insn.  If it succeeds, we can eliminate the
2714    register completely.
2715
2716    Initialize the REG_EQUIV_INIT array of initializing insns.
2717
2718    Return non-zero if jump label rebuilding should be done.  */
2719 static int
2720 update_equiv_regs (void)
2721 {
2722   rtx insn;
2723   basic_block bb;
2724   int loop_depth;
2725   bitmap cleared_regs;
2726
2727   /* We need to keep track of whether or not we recorded a LABEL_REF so
2728      that we know if the jump optimizer needs to be rerun.  */
2729   recorded_label_ref = 0;
2730
2731   reg_equiv = XCNEWVEC (struct equivalence, max_regno);
2732   grow_reg_equivs ();
2733
2734   init_alias_analysis ();
2735
2736   /* Scan the insns and find which registers have equivalences.  Do this
2737      in a separate scan of the insns because (due to -fcse-follow-jumps)
2738      a register can be set below its use.  */
2739   FOR_EACH_BB (bb)
2740     {
2741       loop_depth = bb->loop_depth;
2742
2743       for (insn = BB_HEAD (bb);
2744            insn != NEXT_INSN (BB_END (bb));
2745            insn = NEXT_INSN (insn))
2746         {
2747           rtx note;
2748           rtx set;
2749           rtx dest, src;
2750           int regno;
2751
2752           if (! INSN_P (insn))
2753             continue;
2754
2755           for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
2756             if (REG_NOTE_KIND (note) == REG_INC)
2757               no_equiv (XEXP (note, 0), note, NULL);
2758
2759           set = single_set (insn);
2760
2761           /* If this insn contains more (or less) than a single SET,
2762              only mark all destinations as having no known equivalence.  */
2763           if (set == 0)
2764             {
2765               note_stores (PATTERN (insn), no_equiv, NULL);
2766               continue;
2767             }
2768           else if (GET_CODE (PATTERN (insn)) == PARALLEL)
2769             {
2770               int i;
2771
2772               for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
2773                 {
2774                   rtx part = XVECEXP (PATTERN (insn), 0, i);
2775                   if (part != set)
2776                     note_stores (part, no_equiv, NULL);
2777                 }
2778             }
2779
2780           dest = SET_DEST (set);
2781           src = SET_SRC (set);
2782
2783           /* See if this is setting up the equivalence between an argument
2784              register and its stack slot.  */
2785           note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
2786           if (note)
2787             {
2788               gcc_assert (REG_P (dest));
2789               regno = REGNO (dest);
2790
2791               /* Note that we don't want to clear reg_equiv_init even if there
2792                  are multiple sets of this register.  */
2793               reg_equiv[regno].is_arg_equivalence = 1;
2794
2795               /* Record for reload that this is an equivalencing insn.  */
2796               if (rtx_equal_p (src, XEXP (note, 0)))
2797                 reg_equiv_init (regno)
2798                   = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init (regno));
2799
2800               /* Continue normally in case this is a candidate for
2801                  replacements.  */
2802             }
2803
2804           if (!optimize)
2805             continue;
2806
2807           /* We only handle the case of a pseudo register being set
2808              once, or always to the same value.  */
2809           /* ??? The mn10200 port breaks if we add equivalences for
2810              values that need an ADDRESS_REGS register and set them equivalent
2811              to a MEM of a pseudo.  The actual problem is in the over-conservative
2812              handling of INPADDR_ADDRESS / INPUT_ADDRESS / INPUT triples in
2813              calculate_needs, but we traditionally work around this problem
2814              here by rejecting equivalences when the destination is in a register
2815              that's likely spilled.  This is fragile, of course, since the
2816              preferred class of a pseudo depends on all instructions that set
2817              or use it.  */
2818
2819           if (!REG_P (dest)
2820               || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
2821               || reg_equiv[regno].init_insns == const0_rtx
2822               || (targetm.class_likely_spilled_p (reg_preferred_class (regno))
2823                   && MEM_P (src) && ! reg_equiv[regno].is_arg_equivalence))
2824             {
2825               /* This might be setting a SUBREG of a pseudo, a pseudo that is
2826                  also set somewhere else to a constant.  */
2827               note_stores (set, no_equiv, NULL);
2828               continue;
2829             }
2830
2831           note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
2832
2833           /* cse sometimes generates function invariants, but doesn't put a
2834              REG_EQUAL note on the insn.  Since this note would be redundant,
2835              there's no point creating it earlier than here.  */
2836           if (! note && ! rtx_varies_p (src, 0))
2837             note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
2838
2839           /* Don't bother considering a REG_EQUAL note containing an EXPR_LIST
2840              since it represents a function call */
2841           if (note && GET_CODE (XEXP (note, 0)) == EXPR_LIST)
2842             note = NULL_RTX;
2843
2844           if (DF_REG_DEF_COUNT (regno) != 1
2845               && (! note
2846                   || rtx_varies_p (XEXP (note, 0), 0)
2847                   || (reg_equiv[regno].replacement
2848                       && ! rtx_equal_p (XEXP (note, 0),
2849                                         reg_equiv[regno].replacement))))
2850             {
2851               no_equiv (dest, set, NULL);
2852               continue;
2853             }
2854           /* Record this insn as initializing this register.  */
2855           reg_equiv[regno].init_insns
2856             = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv[regno].init_insns);
2857
2858           /* If this register is known to be equal to a constant, record that
2859              it is always equivalent to the constant.  */
2860           if (DF_REG_DEF_COUNT (regno) == 1
2861               && note && ! rtx_varies_p (XEXP (note, 0), 0))
2862             {
2863               rtx note_value = XEXP (note, 0);
2864               remove_note (insn, note);
2865               set_unique_reg_note (insn, REG_EQUIV, note_value);
2866             }
2867
2868           /* If this insn introduces a "constant" register, decrease the priority
2869              of that register.  Record this insn if the register is only used once
2870              more and the equivalence value is the same as our source.
2871
2872              The latter condition is checked for two reasons:  First, it is an
2873              indication that it may be more efficient to actually emit the insn
2874              as written (if no registers are available, reload will substitute
2875              the equivalence).  Secondly, it avoids problems with any registers
2876              dying in this insn whose death notes would be missed.
2877
2878              If we don't have a REG_EQUIV note, see if this insn is loading
2879              a register used only in one basic block from a MEM.  If so, and the
2880              MEM remains unchanged for the life of the register, add a REG_EQUIV
2881              note.  */
2882
2883           note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
2884
2885           if (note == 0 && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
2886               && MEM_P (SET_SRC (set))
2887               && validate_equiv_mem (insn, dest, SET_SRC (set)))
2888             note = set_unique_reg_note (insn, REG_EQUIV, copy_rtx (SET_SRC (set)));
2889
2890           if (note)
2891             {
2892               int regno = REGNO (dest);
2893               rtx x = XEXP (note, 0);
2894
2895               /* If we haven't done so, record for reload that this is an
2896                  equivalencing insn.  */
2897               if (!reg_equiv[regno].is_arg_equivalence)
2898                 reg_equiv_init (regno)
2899                   = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv_init (regno));
2900
2901               /* Record whether or not we created a REG_EQUIV note for a LABEL_REF.
2902                  We might end up substituting the LABEL_REF for uses of the
2903                  pseudo here or later.  That kind of transformation may turn an
2904                  indirect jump into a direct jump, in which case we must rerun the
2905                  jump optimizer to ensure that the JUMP_LABEL fields are valid.  */
2906               if (GET_CODE (x) == LABEL_REF
2907                   || (GET_CODE (x) == CONST
2908                       && GET_CODE (XEXP (x, 0)) == PLUS
2909                       && (GET_CODE (XEXP (XEXP (x, 0), 0)) == LABEL_REF)))
2910                 recorded_label_ref = 1;
2911
2912               reg_equiv[regno].replacement = x;
2913               reg_equiv[regno].src_p = &SET_SRC (set);
2914               reg_equiv[regno].loop_depth = loop_depth;
2915
2916               /* Don't mess with things live during setjmp.  */
2917               if (REG_LIVE_LENGTH (regno) >= 0 && optimize)
2918                 {
2919                   /* Note that the statement below does not affect the priority
2920                      in local-alloc!  */
2921                   REG_LIVE_LENGTH (regno) *= 2;
2922
2923                   /* If the register is referenced exactly twice, meaning it is
2924                      set once and used once, indicate that the reference may be
2925                      replaced by the equivalence we computed above.  Do this
2926                      even if the register is only used in one block so that
2927                      dependencies can be handled where the last register is
2928                      used in a different block (i.e. HIGH / LO_SUM sequences)
2929                      and to reduce the number of registers alive across
2930                      calls.  */
2931
2932                   if (REG_N_REFS (regno) == 2
2933                       && (rtx_equal_p (x, src)
2934                           || ! equiv_init_varies_p (src))
2935                       && NONJUMP_INSN_P (insn)
2936                       && equiv_init_movable_p (PATTERN (insn), regno))
2937                     reg_equiv[regno].replace = 1;
2938                 }
2939             }
2940         }
2941     }
2942
2943   if (!optimize)
2944     goto out;
2945
2946   /* A second pass, to gather additional equivalences with memory.  This needs
2947      to be done after we know which registers we are going to replace.  */
2948
2949   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2950     {
2951       rtx set, src, dest;
2952       unsigned regno;
2953
2954       if (! INSN_P (insn))
2955         continue;
2956
2957       set = single_set (insn);
2958       if (! set)
2959         continue;
2960
2961       dest = SET_DEST (set);
2962       src = SET_SRC (set);
2963
2964       /* If this sets a MEM to the contents of a REG that is only used
2965          in a single basic block, see if the register is always equivalent
2966          to that memory location and if moving the store from INSN to the
2967          insn that set REG is safe.  If so, put a REG_EQUIV note on the
2968          initializing insn.
2969
2970          Don't add a REG_EQUIV note if the insn already has one.  The existing
2971          REG_EQUIV is likely more useful than the one we are adding.
2972
2973          If one of the regs in the address has reg_equiv[REGNO].replace set,
2974          then we can't add this REG_EQUIV note.  The reg_equiv[REGNO].replace
2975          optimization may move the set of this register immediately before
2976          insn, which puts it after reg_equiv[REGNO].init_insns, and hence
2977          the mention in the REG_EQUIV note would be to an uninitialized
2978          pseudo.  */
2979
2980       if (MEM_P (dest) && REG_P (src)
2981           && (regno = REGNO (src)) >= FIRST_PSEUDO_REGISTER
2982           && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
2983           && DF_REG_DEF_COUNT (regno) == 1
2984           && reg_equiv[regno].init_insns != 0
2985           && reg_equiv[regno].init_insns != const0_rtx
2986           && ! find_reg_note (XEXP (reg_equiv[regno].init_insns, 0),
2987                               REG_EQUIV, NULL_RTX)
2988           && ! contains_replace_regs (XEXP (dest, 0)))
2989         {
2990           rtx init_insn = XEXP (reg_equiv[regno].init_insns, 0);
2991           if (validate_equiv_mem (init_insn, src, dest)
2992               && ! memref_used_between_p (dest, init_insn, insn)
2993               /* Attaching a REG_EQUIV note will fail if INIT_INSN has
2994                  multiple sets.  */
2995               && set_unique_reg_note (init_insn, REG_EQUIV, copy_rtx (dest)))
2996             {
2997               /* This insn makes the equivalence, not the one initializing
2998                  the register.  */
2999               reg_equiv_init (regno)
3000                 = gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
3001               df_notes_rescan (init_insn);
3002             }
3003         }
3004     }
3005
3006   cleared_regs = BITMAP_ALLOC (NULL);
3007   /* Now scan all regs killed in an insn to see if any of them are
3008      registers only used that once.  If so, see if we can replace the
3009      reference with the equivalent form.  If we can, delete the
3010      initializing reference and this register will go away.  If we
3011      can't replace the reference, and the initializing reference is
3012      within the same loop (or in an inner loop), then move the register
3013      initialization just before the use, so that they are in the same
3014      basic block.  */
3015   FOR_EACH_BB_REVERSE (bb)
3016     {
3017       loop_depth = bb->loop_depth;
3018       for (insn = BB_END (bb);
3019            insn != PREV_INSN (BB_HEAD (bb));
3020            insn = PREV_INSN (insn))
3021         {
3022           rtx link;
3023
3024           if (! INSN_P (insn))
3025             continue;
3026
3027           /* Don't substitute into a non-local goto, this confuses CFG.  */
3028           if (JUMP_P (insn)
3029               && find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
3030             continue;
3031
3032           for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
3033             {
3034               if (REG_NOTE_KIND (link) == REG_DEAD
3035                   /* Make sure this insn still refers to the register.  */
3036                   && reg_mentioned_p (XEXP (link, 0), PATTERN (insn)))
3037                 {
3038                   int regno = REGNO (XEXP (link, 0));
3039                   rtx equiv_insn;
3040
3041                   if (! reg_equiv[regno].replace
3042                       || reg_equiv[regno].loop_depth < loop_depth
3043                       /* There is no sense to move insns if we did
3044                          register pressure-sensitive scheduling was
3045                          done because it will not improve allocation
3046                          but worsen insn schedule with a big
3047                          probability.  */
3048                       || (flag_sched_pressure && flag_schedule_insns))
3049                     continue;
3050
3051                   /* reg_equiv[REGNO].replace gets set only when
3052                      REG_N_REFS[REGNO] is 2, i.e. the register is set
3053                      once and used once.  (If it were only set, but not used,
3054                      flow would have deleted the setting insns.)  Hence
3055                      there can only be one insn in reg_equiv[REGNO].init_insns.  */
3056                   gcc_assert (reg_equiv[regno].init_insns
3057                               && !XEXP (reg_equiv[regno].init_insns, 1));
3058                   equiv_insn = XEXP (reg_equiv[regno].init_insns, 0);
3059
3060                   /* We may not move instructions that can throw, since
3061                      that changes basic block boundaries and we are not
3062                      prepared to adjust the CFG to match.  */
3063                   if (can_throw_internal (equiv_insn))
3064                     continue;
3065
3066                   if (asm_noperands (PATTERN (equiv_insn)) < 0
3067                       && validate_replace_rtx (regno_reg_rtx[regno],
3068                                                *(reg_equiv[regno].src_p), insn))
3069                     {
3070                       rtx equiv_link;
3071                       rtx last_link;
3072                       rtx note;
3073
3074                       /* Find the last note.  */
3075                       for (last_link = link; XEXP (last_link, 1);
3076                            last_link = XEXP (last_link, 1))
3077                         ;
3078
3079                       /* Append the REG_DEAD notes from equiv_insn.  */
3080                       equiv_link = REG_NOTES (equiv_insn);
3081                       while (equiv_link)
3082                         {
3083                           note = equiv_link;
3084                           equiv_link = XEXP (equiv_link, 1);
3085                           if (REG_NOTE_KIND (note) == REG_DEAD)
3086                             {
3087                               remove_note (equiv_insn, note);
3088                               XEXP (last_link, 1) = note;
3089                               XEXP (note, 1) = NULL_RTX;
3090                               last_link = note;
3091                             }
3092                         }
3093
3094                       remove_death (regno, insn);
3095                       SET_REG_N_REFS (regno, 0);
3096                       REG_FREQ (regno) = 0;
3097                       delete_insn (equiv_insn);
3098
3099                       reg_equiv[regno].init_insns
3100                         = XEXP (reg_equiv[regno].init_insns, 1);
3101
3102                       reg_equiv_init (regno) = NULL_RTX;
3103                       bitmap_set_bit (cleared_regs, regno);
3104                     }
3105                   /* Move the initialization of the register to just before
3106                      INSN.  Update the flow information.  */
3107                   else if (prev_nondebug_insn (insn) != equiv_insn)
3108                     {
3109                       rtx new_insn;
3110
3111                       new_insn = emit_insn_before (PATTERN (equiv_insn), insn);
3112                       REG_NOTES (new_insn) = REG_NOTES (equiv_insn);
3113                       REG_NOTES (equiv_insn) = 0;
3114                       /* Rescan it to process the notes.  */
3115                       df_insn_rescan (new_insn);
3116
3117                       /* Make sure this insn is recognized before
3118                          reload begins, otherwise
3119                          eliminate_regs_in_insn will die.  */
3120                       INSN_CODE (new_insn) = INSN_CODE (equiv_insn);
3121
3122                       delete_insn (equiv_insn);
3123
3124                       XEXP (reg_equiv[regno].init_insns, 0) = new_insn;
3125
3126                       REG_BASIC_BLOCK (regno) = bb->index;
3127                       REG_N_CALLS_CROSSED (regno) = 0;
3128                       REG_FREQ_CALLS_CROSSED (regno) = 0;
3129                       REG_N_THROWING_CALLS_CROSSED (regno) = 0;
3130                       REG_LIVE_LENGTH (regno) = 2;
3131
3132                       if (insn == BB_HEAD (bb))
3133                         BB_HEAD (bb) = PREV_INSN (insn);
3134
3135                       reg_equiv_init (regno)
3136                         = gen_rtx_INSN_LIST (VOIDmode, new_insn, NULL_RTX);
3137                       bitmap_set_bit (cleared_regs, regno);
3138                     }
3139                 }
3140             }
3141         }
3142     }
3143
3144   if (!bitmap_empty_p (cleared_regs))
3145     {
3146       FOR_EACH_BB (bb)
3147         {
3148           bitmap_and_compl_into (DF_LIVE_IN (bb), cleared_regs);
3149           bitmap_and_compl_into (DF_LIVE_OUT (bb), cleared_regs);
3150           bitmap_and_compl_into (DF_LR_IN (bb), cleared_regs);
3151           bitmap_and_compl_into (DF_LR_OUT (bb), cleared_regs);
3152         }
3153
3154       /* Last pass - adjust debug insns referencing cleared regs.  */
3155       if (MAY_HAVE_DEBUG_INSNS)
3156         for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3157           if (DEBUG_INSN_P (insn))
3158             {
3159               rtx old_loc = INSN_VAR_LOCATION_LOC (insn);
3160               INSN_VAR_LOCATION_LOC (insn)
3161                 = simplify_replace_fn_rtx (old_loc, NULL_RTX,
3162                                            adjust_cleared_regs,
3163                                            (void *) cleared_regs);
3164               if (old_loc != INSN_VAR_LOCATION_LOC (insn))
3165                 df_insn_rescan (insn);
3166             }
3167     }
3168
3169   BITMAP_FREE (cleared_regs);
3170
3171   out:
3172   /* Clean up.  */
3173
3174   end_alias_analysis ();
3175   free (reg_equiv);
3176   return recorded_label_ref;
3177 }
3178
3179 \f
3180
3181 /* Print chain C to FILE.  */
3182 static void
3183 print_insn_chain (FILE *file, struct insn_chain *c)
3184 {
3185   fprintf (file, "insn=%d, ", INSN_UID(c->insn));
3186   bitmap_print (file, &c->live_throughout, "live_throughout: ", ", ");
3187   bitmap_print (file, &c->dead_or_set, "dead_or_set: ", "\n");
3188 }
3189
3190
3191 /* Print all reload_insn_chains to FILE.  */
3192 static void
3193 print_insn_chains (FILE *file)
3194 {
3195   struct insn_chain *c;
3196   for (c = reload_insn_chain; c ; c = c->next)
3197     print_insn_chain (file, c);
3198 }
3199
3200 /* Return true if pseudo REGNO should be added to set live_throughout
3201    or dead_or_set of the insn chains for reload consideration.  */
3202 static bool
3203 pseudo_for_reload_consideration_p (int regno)
3204 {
3205   /* Consider spilled pseudos too for IRA because they still have a
3206      chance to get hard-registers in the reload when IRA is used.  */
3207   return (reg_renumber[regno] >= 0 || ira_conflicts_p);
3208 }
3209
3210 /* Init LIVE_SUBREGS[ALLOCNUM] and LIVE_SUBREGS_USED[ALLOCNUM] using
3211    REG to the number of nregs, and INIT_VALUE to get the
3212    initialization.  ALLOCNUM need not be the regno of REG.  */
3213 static void
3214 init_live_subregs (bool init_value, sbitmap *live_subregs,
3215                    int *live_subregs_used, int allocnum, rtx reg)
3216 {
3217   unsigned int regno = REGNO (SUBREG_REG (reg));
3218   int size = GET_MODE_SIZE (GET_MODE (regno_reg_rtx[regno]));
3219
3220   gcc_assert (size > 0);
3221
3222   /* Been there, done that.  */
3223   if (live_subregs_used[allocnum])
3224     return;
3225
3226   /* Create a new one with zeros.  */
3227   if (live_subregs[allocnum] == NULL)
3228     live_subregs[allocnum] = sbitmap_alloc (size);
3229
3230   /* If the entire reg was live before blasting into subregs, we need
3231      to init all of the subregs to ones else init to 0.  */
3232   if (init_value)
3233     sbitmap_ones (live_subregs[allocnum]);
3234   else
3235     sbitmap_zero (live_subregs[allocnum]);
3236
3237   /* Set the number of bits that we really want.  */
3238   live_subregs_used[allocnum] = size;
3239 }
3240
3241 /* Walk the insns of the current function and build reload_insn_chain,
3242    and record register life information.  */
3243 static void
3244 build_insn_chain (void)
3245 {
3246   unsigned int i;
3247   struct insn_chain **p = &reload_insn_chain;
3248   basic_block bb;
3249   struct insn_chain *c = NULL;
3250   struct insn_chain *next = NULL;
3251   bitmap live_relevant_regs = BITMAP_ALLOC (NULL);
3252   bitmap elim_regset = BITMAP_ALLOC (NULL);
3253   /* live_subregs is a vector used to keep accurate information about
3254      which hardregs are live in multiword pseudos.  live_subregs and
3255      live_subregs_used are indexed by pseudo number.  The live_subreg
3256      entry for a particular pseudo is only used if the corresponding
3257      element is non zero in live_subregs_used.  The value in
3258      live_subregs_used is number of bytes that the pseudo can
3259      occupy.  */
3260   sbitmap *live_subregs = XCNEWVEC (sbitmap, max_regno);
3261   int *live_subregs_used = XNEWVEC (int, max_regno);
3262
3263   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3264     if (TEST_HARD_REG_BIT (eliminable_regset, i))
3265       bitmap_set_bit (elim_regset, i);
3266   FOR_EACH_BB_REVERSE (bb)
3267     {
3268       bitmap_iterator bi;
3269       rtx insn;
3270
3271       CLEAR_REG_SET (live_relevant_regs);
3272       memset (live_subregs_used, 0, max_regno * sizeof (int));
3273
3274       EXECUTE_IF_SET_IN_BITMAP (DF_LR_OUT (bb), 0, i, bi)
3275         {
3276           if (i >= FIRST_PSEUDO_REGISTER)
3277             break;
3278           bitmap_set_bit (live_relevant_regs, i);
3279         }
3280
3281       EXECUTE_IF_SET_IN_BITMAP (DF_LR_OUT (bb),
3282                                 FIRST_PSEUDO_REGISTER, i, bi)
3283         {
3284           if (pseudo_for_reload_consideration_p (i))
3285             bitmap_set_bit (live_relevant_regs, i);
3286         }
3287
3288       FOR_BB_INSNS_REVERSE (bb, insn)
3289         {
3290           if (!NOTE_P (insn) && !BARRIER_P (insn))
3291             {
3292               unsigned int uid = INSN_UID (insn);
3293               df_ref *def_rec;
3294               df_ref *use_rec;
3295
3296               c = new_insn_chain ();
3297               c->next = next;
3298               next = c;
3299               *p = c;
3300               p = &c->prev;
3301
3302               c->insn = insn;
3303               c->block = bb->index;
3304
3305               if (INSN_P (insn))
3306                 for (def_rec = DF_INSN_UID_DEFS (uid); *def_rec; def_rec++)
3307                   {
3308                     df_ref def = *def_rec;
3309                     unsigned int regno = DF_REF_REGNO (def);
3310
3311                     /* Ignore may clobbers because these are generated
3312                        from calls. However, every other kind of def is
3313                        added to dead_or_set.  */
3314                     if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
3315                       {
3316                         if (regno < FIRST_PSEUDO_REGISTER)
3317                           {
3318                             if (!fixed_regs[regno])
3319                               bitmap_set_bit (&c->dead_or_set, regno);
3320                           }
3321                         else if (pseudo_for_reload_consideration_p (regno))
3322                           bitmap_set_bit (&c->dead_or_set, regno);
3323                       }
3324
3325                     if ((regno < FIRST_PSEUDO_REGISTER
3326                          || reg_renumber[regno] >= 0
3327                          || ira_conflicts_p)
3328                         && (!DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)))
3329                       {
3330                         rtx reg = DF_REF_REG (def);
3331
3332                         /* We can model subregs, but not if they are
3333                            wrapped in ZERO_EXTRACTS.  */
3334                         if (GET_CODE (reg) == SUBREG
3335                             && !DF_REF_FLAGS_IS_SET (def, DF_REF_ZERO_EXTRACT))
3336                           {
3337                             unsigned int start = SUBREG_BYTE (reg);
3338                             unsigned int last = start
3339                               + GET_MODE_SIZE (GET_MODE (reg));
3340
3341                             init_live_subregs
3342                               (bitmap_bit_p (live_relevant_regs, regno),
3343                                live_subregs, live_subregs_used, regno, reg);
3344
3345                             if (!DF_REF_FLAGS_IS_SET
3346                                 (def, DF_REF_STRICT_LOW_PART))
3347                               {
3348                                 /* Expand the range to cover entire words.
3349                                    Bytes added here are "don't care".  */
3350                                 start
3351                                   = start / UNITS_PER_WORD * UNITS_PER_WORD;
3352                                 last = ((last + UNITS_PER_WORD - 1)
3353                                         / UNITS_PER_WORD * UNITS_PER_WORD);
3354                               }
3355
3356                             /* Ignore the paradoxical bits.  */
3357                             if ((int)last > live_subregs_used[regno])
3358                               last = live_subregs_used[regno];
3359
3360                             while (start < last)
3361                               {
3362                                 RESET_BIT (live_subregs[regno], start);
3363                                 start++;
3364                               }
3365
3366                             if (sbitmap_empty_p (live_subregs[regno]))
3367                               {
3368                                 live_subregs_used[regno] = 0;
3369                                 bitmap_clear_bit (live_relevant_regs, regno);
3370                               }
3371                             else
3372                               /* Set live_relevant_regs here because
3373                                  that bit has to be true to get us to
3374                                  look at the live_subregs fields.  */
3375                               bitmap_set_bit (live_relevant_regs, regno);
3376                           }
3377                         else
3378                           {
3379                             /* DF_REF_PARTIAL is generated for
3380                                subregs, STRICT_LOW_PART, and
3381                                ZERO_EXTRACT.  We handle the subreg
3382                                case above so here we have to keep from
3383                                modeling the def as a killing def.  */
3384                             if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL))
3385                               {
3386                                 bitmap_clear_bit (live_relevant_regs, regno);
3387                                 live_subregs_used[regno] = 0;
3388                               }
3389                           }
3390                       }
3391                   }
3392
3393               bitmap_and_compl_into (live_relevant_regs, elim_regset);
3394               bitmap_copy (&c->live_throughout, live_relevant_regs);
3395
3396               if (INSN_P (insn))
3397                 for (use_rec = DF_INSN_UID_USES (uid); *use_rec; use_rec++)
3398                   {
3399                     df_ref use = *use_rec;
3400                     unsigned int regno = DF_REF_REGNO (use);
3401                     rtx reg = DF_REF_REG (use);
3402
3403                     /* DF_REF_READ_WRITE on a use means that this use
3404                        is fabricated from a def that is a partial set
3405                        to a multiword reg.  Here, we only model the
3406                        subreg case that is not wrapped in ZERO_EXTRACT
3407                        precisely so we do not need to look at the
3408                        fabricated use. */
3409                     if (DF_REF_FLAGS_IS_SET (use, DF_REF_READ_WRITE)
3410                         && !DF_REF_FLAGS_IS_SET (use, DF_REF_ZERO_EXTRACT)
3411                         && DF_REF_FLAGS_IS_SET (use, DF_REF_SUBREG))
3412                       continue;
3413
3414                     /* Add the last use of each var to dead_or_set.  */
3415                     if (!bitmap_bit_p (live_relevant_regs, regno))
3416                       {
3417                         if (regno < FIRST_PSEUDO_REGISTER)
3418                           {
3419                             if (!fixed_regs[regno])
3420                               bitmap_set_bit (&c->dead_or_set, regno);
3421                           }
3422                         else if (pseudo_for_reload_consideration_p (regno))
3423                           bitmap_set_bit (&c->dead_or_set, regno);
3424                       }
3425
3426                     if (regno < FIRST_PSEUDO_REGISTER
3427                         || pseudo_for_reload_consideration_p (regno))
3428                       {
3429                         if (GET_CODE (reg) == SUBREG
3430                             && !DF_REF_FLAGS_IS_SET (use,
3431                                                      DF_REF_SIGN_EXTRACT
3432                                                      | DF_REF_ZERO_EXTRACT))
3433                           {
3434                             unsigned int start = SUBREG_BYTE (reg);
3435                             unsigned int last = start
3436                               + GET_MODE_SIZE (GET_MODE (reg));
3437
3438                             init_live_subregs
3439                               (bitmap_bit_p (live_relevant_regs, regno),
3440                                live_subregs, live_subregs_used, regno, reg);
3441
3442                             /* Ignore the paradoxical bits.  */
3443                             if ((int)last > live_subregs_used[regno])
3444                               last = live_subregs_used[regno];
3445
3446                             while (start < last)
3447                               {
3448                                 SET_BIT (live_subregs[regno], start);
3449                                 start++;
3450                               }
3451                           }
3452                         else
3453                           /* Resetting the live_subregs_used is
3454                              effectively saying do not use the subregs
3455                              because we are reading the whole
3456                              pseudo.  */
3457                           live_subregs_used[regno] = 0;
3458                         bitmap_set_bit (live_relevant_regs, regno);
3459                       }
3460                   }
3461             }
3462         }
3463
3464       /* FIXME!! The following code is a disaster.  Reload needs to see the
3465          labels and jump tables that are just hanging out in between
3466          the basic blocks.  See pr33676.  */
3467       insn = BB_HEAD (bb);
3468
3469       /* Skip over the barriers and cruft.  */
3470       while (insn && (BARRIER_P (insn) || NOTE_P (insn)
3471                       || BLOCK_FOR_INSN (insn) == bb))
3472         insn = PREV_INSN (insn);
3473
3474       /* While we add anything except barriers and notes, the focus is
3475          to get the labels and jump tables into the
3476          reload_insn_chain.  */
3477       while (insn)
3478         {
3479           if (!NOTE_P (insn) && !BARRIER_P (insn))
3480             {
3481               if (BLOCK_FOR_INSN (insn))
3482                 break;
3483
3484               c = new_insn_chain ();
3485               c->next = next;
3486               next = c;
3487               *p = c;
3488               p = &c->prev;
3489
3490               /* The block makes no sense here, but it is what the old
3491                  code did.  */
3492               c->block = bb->index;
3493               c->insn = insn;
3494               bitmap_copy (&c->live_throughout, live_relevant_regs);
3495             }
3496           insn = PREV_INSN (insn);
3497         }
3498     }
3499
3500   for (i = 0; i < (unsigned int) max_regno; i++)
3501     free (live_subregs[i]);
3502
3503   reload_insn_chain = c;
3504   *p = NULL;
3505
3506   free (live_subregs);
3507   free (live_subregs_used);
3508   BITMAP_FREE (live_relevant_regs);
3509   BITMAP_FREE (elim_regset);
3510
3511   if (dump_file)
3512     print_insn_chains (dump_file);
3513 }
3514
3515 \f
3516
3517 /* All natural loops.  */
3518 struct loops ira_loops;
3519
3520 /* True if we have allocno conflicts.  It is false for non-optimized
3521    mode or when the conflict table is too big.  */
3522 bool ira_conflicts_p;
3523
3524 /* This is the main entry of IRA.  */
3525 static void
3526 ira (FILE *f)
3527 {
3528   int overall_cost_before, allocated_reg_info_size;
3529   bool loops_p;
3530   int max_regno_before_ira, ira_max_point_before_emit;
3531   int rebuild_p;
3532   int saved_flag_ira_share_spill_slots;
3533   basic_block bb;
3534   bool need_dce;
3535
3536   timevar_push (TV_IRA);
3537
3538   if (flag_caller_saves)
3539     init_caller_save ();
3540
3541   if (flag_ira_verbose < 10)
3542     {
3543       internal_flag_ira_verbose = flag_ira_verbose;
3544       ira_dump_file = f;
3545     }
3546   else
3547     {
3548       internal_flag_ira_verbose = flag_ira_verbose - 10;
3549       ira_dump_file = stderr;
3550     }
3551
3552   ira_conflicts_p = optimize > 0;
3553   setup_prohibited_mode_move_regs ();
3554
3555   df_note_add_problem ();
3556
3557   if (optimize == 1)
3558     {
3559       df_live_add_problem ();
3560       df_live_set_all_dirty ();
3561     }
3562 #ifdef ENABLE_CHECKING
3563   df->changeable_flags |= DF_VERIFY_SCHEDULED;
3564 #endif
3565   df_analyze ();
3566   df_clear_flags (DF_NO_INSN_RESCAN);
3567   regstat_init_n_sets_and_refs ();
3568   regstat_compute_ri ();
3569
3570   /* If we are not optimizing, then this is the only place before
3571      register allocation where dataflow is done.  And that is needed
3572      to generate these warnings.  */
3573   if (warn_clobbered)
3574     generate_setjmp_warnings ();
3575
3576   /* Determine if the current function is a leaf before running IRA
3577      since this can impact optimizations done by the prologue and
3578      epilogue thus changing register elimination offsets.  */
3579   current_function_is_leaf = leaf_function_p ();
3580
3581   if (resize_reg_info () && flag_ira_loop_pressure)
3582     ira_set_pseudo_classes (ira_dump_file);
3583
3584   rebuild_p = update_equiv_regs ();
3585
3586 #ifndef IRA_NO_OBSTACK
3587   gcc_obstack_init (&ira_obstack);
3588 #endif
3589   bitmap_obstack_initialize (&ira_bitmap_obstack);
3590   if (optimize)
3591     {
3592       max_regno = max_reg_num ();
3593       ira_reg_equiv_len = max_regno;
3594       ira_reg_equiv_invariant_p
3595         = (bool *) ira_allocate (max_regno * sizeof (bool));
3596       memset (ira_reg_equiv_invariant_p, 0, max_regno * sizeof (bool));
3597       ira_reg_equiv_const = (rtx *) ira_allocate (max_regno * sizeof (rtx));
3598       memset (ira_reg_equiv_const, 0, max_regno * sizeof (rtx));
3599       find_reg_equiv_invariant_const ();
3600       if (rebuild_p)
3601         {
3602           timevar_push (TV_JUMP);
3603           rebuild_jump_labels (get_insns ());
3604           if (purge_all_dead_edges ())
3605             delete_unreachable_blocks ();
3606           timevar_pop (TV_JUMP);
3607         }
3608     }
3609
3610   max_regno_before_ira = allocated_reg_info_size = max_reg_num ();
3611   ira_setup_eliminable_regset ();
3612
3613   ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
3614   ira_load_cost = ira_store_cost = ira_shuffle_cost = 0;
3615   ira_move_loops_num = ira_additional_jumps_num = 0;
3616
3617   ira_assert (current_loops == NULL);
3618   flow_loops_find (&ira_loops);
3619   record_loop_exits ();
3620   current_loops = &ira_loops;
3621
3622   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3623     fprintf (ira_dump_file, "Building IRA IR\n");
3624   loops_p = ira_build (optimize
3625                        && (flag_ira_region == IRA_REGION_ALL
3626                            || flag_ira_region == IRA_REGION_MIXED));
3627
3628   ira_assert (ira_conflicts_p || !loops_p);
3629
3630   saved_flag_ira_share_spill_slots = flag_ira_share_spill_slots;
3631   if (too_high_register_pressure_p () || cfun->calls_setjmp)
3632     /* It is just wasting compiler's time to pack spilled pseudos into
3633        stack slots in this case -- prohibit it.  We also do this if
3634        there is setjmp call because a variable not modified between
3635        setjmp and longjmp the compiler is required to preserve its
3636        value and sharing slots does not guarantee it.  */
3637     flag_ira_share_spill_slots = FALSE;
3638
3639   ira_color ();
3640
3641   ira_max_point_before_emit = ira_max_point;
3642
3643   ira_initiate_emit_data ();
3644
3645   ira_emit (loops_p);
3646
3647   if (ira_conflicts_p)
3648     {
3649       max_regno = max_reg_num ();
3650
3651       if (! loops_p)
3652         ira_initiate_assign ();
3653       else
3654         {
3655           expand_reg_info (allocated_reg_info_size);
3656           setup_preferred_alternate_classes_for_new_pseudos
3657             (allocated_reg_info_size);
3658           allocated_reg_info_size = max_regno;
3659
3660           if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3661             fprintf (ira_dump_file, "Flattening IR\n");
3662           ira_flattening (max_regno_before_ira, ira_max_point_before_emit);
3663           /* New insns were generated: add notes and recalculate live
3664              info.  */
3665           df_analyze ();
3666
3667           flow_loops_find (&ira_loops);
3668           record_loop_exits ();
3669           current_loops = &ira_loops;
3670
3671           setup_allocno_assignment_flags ();
3672           ira_initiate_assign ();
3673           ira_reassign_conflict_allocnos (max_regno);
3674         }
3675     }
3676
3677   ira_finish_emit_data ();
3678
3679   setup_reg_renumber ();
3680
3681   calculate_allocation_cost ();
3682
3683 #ifdef ENABLE_IRA_CHECKING
3684   if (ira_conflicts_p)
3685     check_allocation ();
3686 #endif
3687
3688   if (delete_trivially_dead_insns (get_insns (), max_reg_num ()))
3689     df_analyze ();
3690
3691   if (max_regno != max_regno_before_ira)
3692     {
3693       regstat_free_n_sets_and_refs ();
3694       regstat_free_ri ();
3695       regstat_init_n_sets_and_refs ();
3696       regstat_compute_ri ();
3697     }
3698
3699   overall_cost_before = ira_overall_cost;
3700   if (! ira_conflicts_p)
3701     grow_reg_equivs ();
3702   else
3703     {
3704       fix_reg_equiv_init ();
3705
3706 #ifdef ENABLE_IRA_CHECKING
3707       print_redundant_copies ();
3708 #endif
3709
3710       ira_spilled_reg_stack_slots_num = 0;
3711       ira_spilled_reg_stack_slots
3712         = ((struct ira_spilled_reg_stack_slot *)
3713            ira_allocate (max_regno
3714                          * sizeof (struct ira_spilled_reg_stack_slot)));
3715       memset (ira_spilled_reg_stack_slots, 0,
3716               max_regno * sizeof (struct ira_spilled_reg_stack_slot));
3717     }
3718   allocate_initial_values (reg_equivs);
3719
3720   timevar_pop (TV_IRA);
3721
3722   timevar_push (TV_RELOAD);
3723   df_set_flags (DF_NO_INSN_RESCAN);
3724   build_insn_chain ();
3725
3726   need_dce = reload (get_insns (), ira_conflicts_p);
3727
3728   timevar_pop (TV_RELOAD);
3729
3730   timevar_push (TV_IRA);
3731
3732   if (ira_conflicts_p)
3733     {
3734       ira_free (ira_spilled_reg_stack_slots);
3735
3736       ira_finish_assign ();
3737
3738     }
3739   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL
3740       && overall_cost_before != ira_overall_cost)
3741     fprintf (ira_dump_file, "+++Overall after reload %d\n", ira_overall_cost);
3742   ira_destroy ();
3743
3744   flag_ira_share_spill_slots = saved_flag_ira_share_spill_slots;
3745
3746   flow_loops_free (&ira_loops);
3747   free_dominance_info (CDI_DOMINATORS);
3748   FOR_ALL_BB (bb)
3749     bb->loop_father = NULL;
3750   current_loops = NULL;
3751
3752   regstat_free_ri ();
3753   regstat_free_n_sets_and_refs ();
3754
3755   if (optimize)
3756     {
3757       cleanup_cfg (CLEANUP_EXPENSIVE);
3758
3759       ira_free (ira_reg_equiv_invariant_p);
3760       ira_free (ira_reg_equiv_const);
3761     }
3762
3763   bitmap_obstack_release (&ira_bitmap_obstack);
3764 #ifndef IRA_NO_OBSTACK
3765   obstack_free (&ira_obstack, NULL);
3766 #endif
3767
3768   /* The code after the reload has changed so much that at this point
3769      we might as well just rescan everything.  Note that
3770      df_rescan_all_insns is not going to help here because it does not
3771      touch the artificial uses and defs.  */
3772   df_finish_pass (true);
3773   if (optimize > 1)
3774     df_live_add_problem ();
3775   df_scan_alloc (NULL);
3776   df_scan_blocks ();
3777
3778   if (optimize)
3779     df_analyze ();
3780
3781   if (need_dce && optimize)
3782     run_fast_dce ();
3783
3784   timevar_pop (TV_IRA);
3785 }
3786
3787 \f
3788
3789 static bool
3790 gate_ira (void)
3791 {
3792   return true;
3793 }
3794
3795 /* Run the integrated register allocator.  */
3796 static unsigned int
3797 rest_of_handle_ira (void)
3798 {
3799   ira (dump_file);
3800   return 0;
3801 }
3802
3803 struct rtl_opt_pass pass_ira =
3804 {
3805  {
3806   RTL_PASS,
3807   "ira",                                /* name */
3808   gate_ira,                             /* gate */
3809   rest_of_handle_ira,                   /* execute */
3810   NULL,                                 /* sub */
3811   NULL,                                 /* next */
3812   0,                                    /* static_pass_number */
3813   TV_NONE,                              /* tv_id */
3814   0,                                    /* properties_required */
3815   0,                                    /* properties_provided */
3816   0,                                    /* properties_destroyed */
3817   0,                                    /* todo_flags_start */
3818   TODO_ggc_collect                      /* todo_flags_finish */
3819  }
3820 };