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