OSDN Git Service

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