xref: /dragonfly/contrib/gcc-8.0/gcc/ira.c (revision 38fd1498)
1*38fd1498Szrj /* Integrated Register Allocator (IRA) entry point.
2*38fd1498Szrj    Copyright (C) 2006-2018 Free Software Foundation, Inc.
3*38fd1498Szrj    Contributed by Vladimir Makarov <vmakarov@redhat.com>.
4*38fd1498Szrj 
5*38fd1498Szrj This file is part of GCC.
6*38fd1498Szrj 
7*38fd1498Szrj GCC is free software; you can redistribute it and/or modify it under
8*38fd1498Szrj the terms of the GNU General Public License as published by the Free
9*38fd1498Szrj Software Foundation; either version 3, or (at your option) any later
10*38fd1498Szrj version.
11*38fd1498Szrj 
12*38fd1498Szrj GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*38fd1498Szrj WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*38fd1498Szrj FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15*38fd1498Szrj for more details.
16*38fd1498Szrj 
17*38fd1498Szrj You should have received a copy of the GNU General Public License
18*38fd1498Szrj along with GCC; see the file COPYING3.  If not see
19*38fd1498Szrj <http://www.gnu.org/licenses/>.  */
20*38fd1498Szrj 
21*38fd1498Szrj /* The integrated register allocator (IRA) is a
22*38fd1498Szrj    regional register allocator performing graph coloring on a top-down
23*38fd1498Szrj    traversal of nested regions.  Graph coloring in a region is based
24*38fd1498Szrj    on Chaitin-Briggs algorithm.  It is called integrated because
25*38fd1498Szrj    register coalescing, register live range splitting, and choosing a
26*38fd1498Szrj    better hard register are done on-the-fly during coloring.  Register
27*38fd1498Szrj    coalescing and choosing a cheaper hard register is done by hard
28*38fd1498Szrj    register preferencing during hard register assigning.  The live
29*38fd1498Szrj    range splitting is a byproduct of the regional register allocation.
30*38fd1498Szrj 
31*38fd1498Szrj    Major IRA notions are:
32*38fd1498Szrj 
33*38fd1498Szrj      o *Region* is a part of CFG where graph coloring based on
34*38fd1498Szrj        Chaitin-Briggs algorithm is done.  IRA can work on any set of
35*38fd1498Szrj        nested CFG regions forming a tree.  Currently the regions are
36*38fd1498Szrj        the entire function for the root region and natural loops for
37*38fd1498Szrj        the other regions.  Therefore data structure representing a
38*38fd1498Szrj        region is called loop_tree_node.
39*38fd1498Szrj 
40*38fd1498Szrj      o *Allocno class* is a register class used for allocation of
41*38fd1498Szrj        given allocno.  It means that only hard register of given
42*38fd1498Szrj        register class can be assigned to given allocno.  In reality,
43*38fd1498Szrj        even smaller subset of (*profitable*) hard registers can be
44*38fd1498Szrj        assigned.  In rare cases, the subset can be even smaller
45*38fd1498Szrj        because our modification of Chaitin-Briggs algorithm requires
46*38fd1498Szrj        that sets of hard registers can be assigned to allocnos forms a
47*38fd1498Szrj        forest, i.e. the sets can be ordered in a way where any
48*38fd1498Szrj        previous set is not intersected with given set or is a superset
49*38fd1498Szrj        of given set.
50*38fd1498Szrj 
51*38fd1498Szrj      o *Pressure class* is a register class belonging to a set of
52*38fd1498Szrj        register classes containing all of the hard-registers available
53*38fd1498Szrj        for register allocation.  The set of all pressure classes for a
54*38fd1498Szrj        target is defined in the corresponding machine-description file
55*38fd1498Szrj        according some criteria.  Register pressure is calculated only
56*38fd1498Szrj        for pressure classes and it affects some IRA decisions as
57*38fd1498Szrj        forming allocation regions.
58*38fd1498Szrj 
59*38fd1498Szrj      o *Allocno* represents the live range of a pseudo-register in a
60*38fd1498Szrj        region.  Besides the obvious attributes like the corresponding
61*38fd1498Szrj        pseudo-register number, allocno class, conflicting allocnos and
62*38fd1498Szrj        conflicting hard-registers, there are a few allocno attributes
63*38fd1498Szrj        which are important for understanding the allocation algorithm:
64*38fd1498Szrj 
65*38fd1498Szrj        - *Live ranges*.  This is a list of ranges of *program points*
66*38fd1498Szrj          where the allocno lives.  Program points represent places
67*38fd1498Szrj          where a pseudo can be born or become dead (there are
68*38fd1498Szrj          approximately two times more program points than the insns)
69*38fd1498Szrj          and they are represented by integers starting with 0.  The
70*38fd1498Szrj          live ranges are used to find conflicts between allocnos.
71*38fd1498Szrj          They also play very important role for the transformation of
72*38fd1498Szrj          the IRA internal representation of several regions into a one
73*38fd1498Szrj          region representation.  The later is used during the reload
74*38fd1498Szrj          pass work because each allocno represents all of the
75*38fd1498Szrj          corresponding pseudo-registers.
76*38fd1498Szrj 
77*38fd1498Szrj        - *Hard-register costs*.  This is a vector of size equal to the
78*38fd1498Szrj          number of available hard-registers of the allocno class.  The
79*38fd1498Szrj          cost of a callee-clobbered hard-register for an allocno is
80*38fd1498Szrj          increased by the cost of save/restore code around the calls
81*38fd1498Szrj          through the given allocno's life.  If the allocno is a move
82*38fd1498Szrj          instruction operand and another operand is a hard-register of
83*38fd1498Szrj          the allocno class, the cost of the hard-register is decreased
84*38fd1498Szrj          by the move cost.
85*38fd1498Szrj 
86*38fd1498Szrj          When an allocno is assigned, the hard-register with minimal
87*38fd1498Szrj          full cost is used.  Initially, a hard-register's full cost is
88*38fd1498Szrj          the corresponding value from the hard-register's cost vector.
89*38fd1498Szrj          If the allocno is connected by a *copy* (see below) to
90*38fd1498Szrj          another allocno which has just received a hard-register, the
91*38fd1498Szrj          cost of the hard-register is decreased.  Before choosing a
92*38fd1498Szrj          hard-register for an allocno, the allocno's current costs of
93*38fd1498Szrj          the hard-registers are modified by the conflict hard-register
94*38fd1498Szrj          costs of all of the conflicting allocnos which are not
95*38fd1498Szrj          assigned yet.
96*38fd1498Szrj 
97*38fd1498Szrj        - *Conflict hard-register costs*.  This is a vector of the same
98*38fd1498Szrj          size as the hard-register costs vector.  To permit an
99*38fd1498Szrj          unassigned allocno to get a better hard-register, IRA uses
100*38fd1498Szrj          this vector to calculate the final full cost of the
101*38fd1498Szrj          available hard-registers.  Conflict hard-register costs of an
102*38fd1498Szrj          unassigned allocno are also changed with a change of the
103*38fd1498Szrj          hard-register cost of the allocno when a copy involving the
104*38fd1498Szrj          allocno is processed as described above.  This is done to
105*38fd1498Szrj          show other unassigned allocnos that a given allocno prefers
106*38fd1498Szrj          some hard-registers in order to remove the move instruction
107*38fd1498Szrj          corresponding to the copy.
108*38fd1498Szrj 
109*38fd1498Szrj      o *Cap*.  If a pseudo-register does not live in a region but
110*38fd1498Szrj        lives in a nested region, IRA creates a special allocno called
111*38fd1498Szrj        a cap in the outer region.  A region cap is also created for a
112*38fd1498Szrj        subregion cap.
113*38fd1498Szrj 
114*38fd1498Szrj      o *Copy*.  Allocnos can be connected by copies.  Copies are used
115*38fd1498Szrj        to modify hard-register costs for allocnos during coloring.
116*38fd1498Szrj        Such modifications reflects a preference to use the same
117*38fd1498Szrj        hard-register for the allocnos connected by copies.  Usually
118*38fd1498Szrj        copies are created for move insns (in this case it results in
119*38fd1498Szrj        register coalescing).  But IRA also creates copies for operands
120*38fd1498Szrj        of an insn which should be assigned to the same hard-register
121*38fd1498Szrj        due to constraints in the machine description (it usually
122*38fd1498Szrj        results in removing a move generated in reload to satisfy
123*38fd1498Szrj        the constraints) and copies referring to the allocno which is
124*38fd1498Szrj        the output operand of an instruction and the allocno which is
125*38fd1498Szrj        an input operand dying in the instruction (creation of such
126*38fd1498Szrj        copies results in less register shuffling).  IRA *does not*
127*38fd1498Szrj        create copies between the same register allocnos from different
128*38fd1498Szrj        regions because we use another technique for propagating
129*38fd1498Szrj        hard-register preference on the borders of regions.
130*38fd1498Szrj 
131*38fd1498Szrj    Allocnos (including caps) for the upper region in the region tree
132*38fd1498Szrj    *accumulate* information important for coloring from allocnos with
133*38fd1498Szrj    the same pseudo-register from nested regions.  This includes
134*38fd1498Szrj    hard-register and memory costs, conflicts with hard-registers,
135*38fd1498Szrj    allocno conflicts, allocno copies and more.  *Thus, attributes for
136*38fd1498Szrj    allocnos in a region have the same values as if the region had no
137*38fd1498Szrj    subregions*.  It means that attributes for allocnos in the
138*38fd1498Szrj    outermost region corresponding to the function have the same values
139*38fd1498Szrj    as though the allocation used only one region which is the entire
140*38fd1498Szrj    function.  It also means that we can look at IRA work as if the
141*38fd1498Szrj    first IRA did allocation for all function then it improved the
142*38fd1498Szrj    allocation for loops then their subloops and so on.
143*38fd1498Szrj 
144*38fd1498Szrj    IRA major passes are:
145*38fd1498Szrj 
146*38fd1498Szrj      o Building IRA internal representation which consists of the
147*38fd1498Szrj        following subpasses:
148*38fd1498Szrj 
149*38fd1498Szrj        * First, IRA builds regions and creates allocnos (file
150*38fd1498Szrj          ira-build.c) and initializes most of their attributes.
151*38fd1498Szrj 
152*38fd1498Szrj        * Then IRA finds an allocno class for each allocno and
153*38fd1498Szrj          calculates its initial (non-accumulated) cost of memory and
154*38fd1498Szrj          each hard-register of its allocno class (file ira-cost.c).
155*38fd1498Szrj 
156*38fd1498Szrj        * IRA creates live ranges of each allocno, calculates register
157*38fd1498Szrj          pressure for each pressure class in each region, sets up
158*38fd1498Szrj          conflict hard registers for each allocno and info about calls
159*38fd1498Szrj          the allocno lives through (file ira-lives.c).
160*38fd1498Szrj 
161*38fd1498Szrj        * IRA removes low register pressure loops from the regions
162*38fd1498Szrj          mostly to speed IRA up (file ira-build.c).
163*38fd1498Szrj 
164*38fd1498Szrj        * IRA propagates accumulated allocno info from lower region
165*38fd1498Szrj          allocnos to corresponding upper region allocnos (file
166*38fd1498Szrj          ira-build.c).
167*38fd1498Szrj 
168*38fd1498Szrj        * IRA creates all caps (file ira-build.c).
169*38fd1498Szrj 
170*38fd1498Szrj        * Having live-ranges of allocnos and their classes, IRA creates
171*38fd1498Szrj          conflicting allocnos for each allocno.  Conflicting allocnos
172*38fd1498Szrj          are stored as a bit vector or array of pointers to the
173*38fd1498Szrj          conflicting allocnos whatever is more profitable (file
174*38fd1498Szrj          ira-conflicts.c).  At this point IRA creates allocno copies.
175*38fd1498Szrj 
176*38fd1498Szrj      o Coloring.  Now IRA has all necessary info to start graph coloring
177*38fd1498Szrj        process.  It is done in each region on top-down traverse of the
178*38fd1498Szrj        region tree (file ira-color.c).  There are following subpasses:
179*38fd1498Szrj 
180*38fd1498Szrj        * Finding profitable hard registers of corresponding allocno
181*38fd1498Szrj          class for each allocno.  For example, only callee-saved hard
182*38fd1498Szrj          registers are frequently profitable for allocnos living
183*38fd1498Szrj          through colors.  If the profitable hard register set of
184*38fd1498Szrj          allocno does not form a tree based on subset relation, we use
185*38fd1498Szrj          some approximation to form the tree.  This approximation is
186*38fd1498Szrj          used to figure out trivial colorability of allocnos.  The
187*38fd1498Szrj          approximation is a pretty rare case.
188*38fd1498Szrj 
189*38fd1498Szrj        * Putting allocnos onto the coloring stack.  IRA uses Briggs
190*38fd1498Szrj          optimistic coloring which is a major improvement over
191*38fd1498Szrj          Chaitin's coloring.  Therefore IRA does not spill allocnos at
192*38fd1498Szrj          this point.  There is some freedom in the order of putting
193*38fd1498Szrj          allocnos on the stack which can affect the final result of
194*38fd1498Szrj          the allocation.  IRA uses some heuristics to improve the
195*38fd1498Szrj          order.  The major one is to form *threads* from colorable
196*38fd1498Szrj          allocnos and push them on the stack by threads.  Thread is a
197*38fd1498Szrj          set of non-conflicting colorable allocnos connected by
198*38fd1498Szrj          copies.  The thread contains allocnos from the colorable
199*38fd1498Szrj          bucket or colorable allocnos already pushed onto the coloring
200*38fd1498Szrj          stack.  Pushing thread allocnos one after another onto the
201*38fd1498Szrj          stack increases chances of removing copies when the allocnos
202*38fd1498Szrj          get the same hard reg.
203*38fd1498Szrj 
204*38fd1498Szrj 	 We also use a modification of Chaitin-Briggs algorithm which
205*38fd1498Szrj          works for intersected register classes of allocnos.  To
206*38fd1498Szrj          figure out trivial colorability of allocnos, the mentioned
207*38fd1498Szrj          above tree of hard register sets is used.  To get an idea how
208*38fd1498Szrj          the algorithm works in i386 example, let us consider an
209*38fd1498Szrj          allocno to which any general hard register can be assigned.
210*38fd1498Szrj          If the allocno conflicts with eight allocnos to which only
211*38fd1498Szrj          EAX register can be assigned, given allocno is still
212*38fd1498Szrj          trivially colorable because all conflicting allocnos might be
213*38fd1498Szrj          assigned only to EAX and all other general hard registers are
214*38fd1498Szrj          still free.
215*38fd1498Szrj 
216*38fd1498Szrj 	 To get an idea of the used trivial colorability criterion, it
217*38fd1498Szrj 	 is also useful to read article "Graph-Coloring Register
218*38fd1498Szrj 	 Allocation for Irregular Architectures" by Michael D. Smith
219*38fd1498Szrj 	 and Glen Holloway.  Major difference between the article
220*38fd1498Szrj 	 approach and approach used in IRA is that Smith's approach
221*38fd1498Szrj 	 takes register classes only from machine description and IRA
222*38fd1498Szrj 	 calculate register classes from intermediate code too
223*38fd1498Szrj 	 (e.g. an explicit usage of hard registers in RTL code for
224*38fd1498Szrj 	 parameter passing can result in creation of additional
225*38fd1498Szrj 	 register classes which contain or exclude the hard
226*38fd1498Szrj 	 registers).  That makes IRA approach useful for improving
227*38fd1498Szrj 	 coloring even for architectures with regular register files
228*38fd1498Szrj 	 and in fact some benchmarking shows the improvement for
229*38fd1498Szrj 	 regular class architectures is even bigger than for irregular
230*38fd1498Szrj 	 ones.  Another difference is that Smith's approach chooses
231*38fd1498Szrj 	 intersection of classes of all insn operands in which a given
232*38fd1498Szrj 	 pseudo occurs.  IRA can use bigger classes if it is still
233*38fd1498Szrj 	 more profitable than memory usage.
234*38fd1498Szrj 
235*38fd1498Szrj        * Popping the allocnos from the stack and assigning them hard
236*38fd1498Szrj          registers.  If IRA can not assign a hard register to an
237*38fd1498Szrj          allocno and the allocno is coalesced, IRA undoes the
238*38fd1498Szrj          coalescing and puts the uncoalesced allocnos onto the stack in
239*38fd1498Szrj          the hope that some such allocnos will get a hard register
240*38fd1498Szrj          separately.  If IRA fails to assign hard register or memory
241*38fd1498Szrj          is more profitable for it, IRA spills the allocno.  IRA
242*38fd1498Szrj          assigns the allocno the hard-register with minimal full
243*38fd1498Szrj          allocation cost which reflects the cost of usage of the
244*38fd1498Szrj          hard-register for the allocno and cost of usage of the
245*38fd1498Szrj          hard-register for allocnos conflicting with given allocno.
246*38fd1498Szrj 
247*38fd1498Szrj        * Chaitin-Briggs coloring assigns as many pseudos as possible
248*38fd1498Szrj          to hard registers.  After coloring we try to improve
249*38fd1498Szrj          allocation with cost point of view.  We improve the
250*38fd1498Szrj          allocation by spilling some allocnos and assigning the freed
251*38fd1498Szrj          hard registers to other allocnos if it decreases the overall
252*38fd1498Szrj          allocation cost.
253*38fd1498Szrj 
254*38fd1498Szrj        * After allocno assigning in the region, IRA modifies the hard
255*38fd1498Szrj          register and memory costs for the corresponding allocnos in
256*38fd1498Szrj          the subregions to reflect the cost of possible loads, stores,
257*38fd1498Szrj          or moves on the border of the region and its subregions.
258*38fd1498Szrj          When default regional allocation algorithm is used
259*38fd1498Szrj          (-fira-algorithm=mixed), IRA just propagates the assignment
260*38fd1498Szrj          for allocnos if the register pressure in the region for the
261*38fd1498Szrj          corresponding pressure class is less than number of available
262*38fd1498Szrj          hard registers for given pressure class.
263*38fd1498Szrj 
264*38fd1498Szrj      o Spill/restore code moving.  When IRA performs an allocation
265*38fd1498Szrj        by traversing regions in top-down order, it does not know what
266*38fd1498Szrj        happens below in the region tree.  Therefore, sometimes IRA
267*38fd1498Szrj        misses opportunities to perform a better allocation.  A simple
268*38fd1498Szrj        optimization tries to improve allocation in a region having
269*38fd1498Szrj        subregions and containing in another region.  If the
270*38fd1498Szrj        corresponding allocnos in the subregion are spilled, it spills
271*38fd1498Szrj        the region allocno if it is profitable.  The optimization
272*38fd1498Szrj        implements a simple iterative algorithm performing profitable
273*38fd1498Szrj        transformations while they are still possible.  It is fast in
274*38fd1498Szrj        practice, so there is no real need for a better time complexity
275*38fd1498Szrj        algorithm.
276*38fd1498Szrj 
277*38fd1498Szrj      o Code change.  After coloring, two allocnos representing the
278*38fd1498Szrj        same pseudo-register outside and inside a region respectively
279*38fd1498Szrj        may be assigned to different locations (hard-registers or
280*38fd1498Szrj        memory).  In this case IRA creates and uses a new
281*38fd1498Szrj        pseudo-register inside the region and adds code to move allocno
282*38fd1498Szrj        values on the region's borders.  This is done during top-down
283*38fd1498Szrj        traversal of the regions (file ira-emit.c).  In some
284*38fd1498Szrj        complicated cases IRA can create a new allocno to move allocno
285*38fd1498Szrj        values (e.g. when a swap of values stored in two hard-registers
286*38fd1498Szrj        is needed).  At this stage, the new allocno is marked as
287*38fd1498Szrj        spilled.  IRA still creates the pseudo-register and the moves
288*38fd1498Szrj        on the region borders even when both allocnos were assigned to
289*38fd1498Szrj        the same hard-register.  If the reload pass spills a
290*38fd1498Szrj        pseudo-register for some reason, the effect will be smaller
291*38fd1498Szrj        because another allocno will still be in the hard-register.  In
292*38fd1498Szrj        most cases, this is better then spilling both allocnos.  If
293*38fd1498Szrj        reload does not change the allocation for the two
294*38fd1498Szrj        pseudo-registers, the trivial move will be removed by
295*38fd1498Szrj        post-reload optimizations.  IRA does not generate moves for
296*38fd1498Szrj        allocnos assigned to the same hard register when the default
297*38fd1498Szrj        regional allocation algorithm is used and the register pressure
298*38fd1498Szrj        in the region for the corresponding pressure class is less than
299*38fd1498Szrj        number of available hard registers for given pressure class.
300*38fd1498Szrj        IRA also does some optimizations to remove redundant stores and
301*38fd1498Szrj        to reduce code duplication on the region borders.
302*38fd1498Szrj 
303*38fd1498Szrj      o Flattening internal representation.  After changing code, IRA
304*38fd1498Szrj        transforms its internal representation for several regions into
305*38fd1498Szrj        one region representation (file ira-build.c).  This process is
306*38fd1498Szrj        called IR flattening.  Such process is more complicated than IR
307*38fd1498Szrj        rebuilding would be, but is much faster.
308*38fd1498Szrj 
309*38fd1498Szrj      o After IR flattening, IRA tries to assign hard registers to all
310*38fd1498Szrj        spilled allocnos.  This is implemented by a simple and fast
311*38fd1498Szrj        priority coloring algorithm (see function
312*38fd1498Szrj        ira_reassign_conflict_allocnos::ira-color.c).  Here new allocnos
313*38fd1498Szrj        created during the code change pass can be assigned to hard
314*38fd1498Szrj        registers.
315*38fd1498Szrj 
316*38fd1498Szrj      o At the end IRA calls the reload pass.  The reload pass
317*38fd1498Szrj        communicates with IRA through several functions in file
318*38fd1498Szrj        ira-color.c to improve its decisions in
319*38fd1498Szrj 
320*38fd1498Szrj        * sharing stack slots for the spilled pseudos based on IRA info
321*38fd1498Szrj          about pseudo-register conflicts.
322*38fd1498Szrj 
323*38fd1498Szrj        * reassigning hard-registers to all spilled pseudos at the end
324*38fd1498Szrj          of each reload iteration.
325*38fd1498Szrj 
326*38fd1498Szrj        * choosing a better hard-register to spill based on IRA info
327*38fd1498Szrj          about pseudo-register live ranges and the register pressure
328*38fd1498Szrj          in places where the pseudo-register lives.
329*38fd1498Szrj 
330*38fd1498Szrj    IRA uses a lot of data representing the target processors.  These
331*38fd1498Szrj    data are initialized in file ira.c.
332*38fd1498Szrj 
333*38fd1498Szrj    If function has no loops (or the loops are ignored when
334*38fd1498Szrj    -fira-algorithm=CB is used), we have classic Chaitin-Briggs
335*38fd1498Szrj    coloring (only instead of separate pass of coalescing, we use hard
336*38fd1498Szrj    register preferencing).  In such case, IRA works much faster
337*38fd1498Szrj    because many things are not made (like IR flattening, the
338*38fd1498Szrj    spill/restore optimization, and the code change).
339*38fd1498Szrj 
340*38fd1498Szrj    Literature is worth to read for better understanding the code:
341*38fd1498Szrj 
342*38fd1498Szrj    o Preston Briggs, Keith D. Cooper, Linda Torczon.  Improvements to
343*38fd1498Szrj      Graph Coloring Register Allocation.
344*38fd1498Szrj 
345*38fd1498Szrj    o David Callahan, Brian Koblenz.  Register allocation via
346*38fd1498Szrj      hierarchical graph coloring.
347*38fd1498Szrj 
348*38fd1498Szrj    o Keith Cooper, Anshuman Dasgupta, Jason Eckhardt. Revisiting Graph
349*38fd1498Szrj      Coloring Register Allocation: A Study of the Chaitin-Briggs and
350*38fd1498Szrj      Callahan-Koblenz Algorithms.
351*38fd1498Szrj 
352*38fd1498Szrj    o Guei-Yuan Lueh, Thomas Gross, and Ali-Reza Adl-Tabatabai. Global
353*38fd1498Szrj      Register Allocation Based on Graph Fusion.
354*38fd1498Szrj 
355*38fd1498Szrj    o Michael D. Smith and Glenn Holloway.  Graph-Coloring Register
356*38fd1498Szrj      Allocation for Irregular Architectures
357*38fd1498Szrj 
358*38fd1498Szrj    o Vladimir Makarov. The Integrated Register Allocator for GCC.
359*38fd1498Szrj 
360*38fd1498Szrj    o Vladimir Makarov.  The top-down register allocator for irregular
361*38fd1498Szrj      register file architectures.
362*38fd1498Szrj 
363*38fd1498Szrj */
364*38fd1498Szrj 
365*38fd1498Szrj 
366*38fd1498Szrj #include "config.h"
367*38fd1498Szrj #include "system.h"
368*38fd1498Szrj #include "coretypes.h"
369*38fd1498Szrj #include "backend.h"
370*38fd1498Szrj #include "target.h"
371*38fd1498Szrj #include "rtl.h"
372*38fd1498Szrj #include "tree.h"
373*38fd1498Szrj #include "df.h"
374*38fd1498Szrj #include "memmodel.h"
375*38fd1498Szrj #include "tm_p.h"
376*38fd1498Szrj #include "insn-config.h"
377*38fd1498Szrj #include "regs.h"
378*38fd1498Szrj #include "ira.h"
379*38fd1498Szrj #include "ira-int.h"
380*38fd1498Szrj #include "diagnostic-core.h"
381*38fd1498Szrj #include "cfgrtl.h"
382*38fd1498Szrj #include "cfgbuild.h"
383*38fd1498Szrj #include "cfgcleanup.h"
384*38fd1498Szrj #include "expr.h"
385*38fd1498Szrj #include "tree-pass.h"
386*38fd1498Szrj #include "output.h"
387*38fd1498Szrj #include "reload.h"
388*38fd1498Szrj #include "cfgloop.h"
389*38fd1498Szrj #include "lra.h"
390*38fd1498Szrj #include "dce.h"
391*38fd1498Szrj #include "dbgcnt.h"
392*38fd1498Szrj #include "rtl-iter.h"
393*38fd1498Szrj #include "shrink-wrap.h"
394*38fd1498Szrj #include "print-rtl.h"
395*38fd1498Szrj 
396*38fd1498Szrj struct target_ira default_target_ira;
397*38fd1498Szrj struct target_ira_int default_target_ira_int;
398*38fd1498Szrj #if SWITCHABLE_TARGET
399*38fd1498Szrj struct target_ira *this_target_ira = &default_target_ira;
400*38fd1498Szrj struct target_ira_int *this_target_ira_int = &default_target_ira_int;
401*38fd1498Szrj #endif
402*38fd1498Szrj 
403*38fd1498Szrj /* A modified value of flag `-fira-verbose' used internally.  */
404*38fd1498Szrj int internal_flag_ira_verbose;
405*38fd1498Szrj 
406*38fd1498Szrj /* Dump file of the allocator if it is not NULL.  */
407*38fd1498Szrj FILE *ira_dump_file;
408*38fd1498Szrj 
409*38fd1498Szrj /* The number of elements in the following array.  */
410*38fd1498Szrj int ira_spilled_reg_stack_slots_num;
411*38fd1498Szrj 
412*38fd1498Szrj /* The following array contains info about spilled pseudo-registers
413*38fd1498Szrj    stack slots used in current function so far.  */
414*38fd1498Szrj struct ira_spilled_reg_stack_slot *ira_spilled_reg_stack_slots;
415*38fd1498Szrj 
416*38fd1498Szrj /* Correspondingly overall cost of the allocation, overall cost before
417*38fd1498Szrj    reload, cost of the allocnos assigned to hard-registers, cost of
418*38fd1498Szrj    the allocnos assigned to memory, cost of loads, stores and register
419*38fd1498Szrj    move insns generated for pseudo-register live range splitting (see
420*38fd1498Szrj    ira-emit.c).  */
421*38fd1498Szrj int64_t ira_overall_cost, overall_cost_before;
422*38fd1498Szrj int64_t ira_reg_cost, ira_mem_cost;
423*38fd1498Szrj int64_t ira_load_cost, ira_store_cost, ira_shuffle_cost;
424*38fd1498Szrj int ira_move_loops_num, ira_additional_jumps_num;
425*38fd1498Szrj 
426*38fd1498Szrj /* All registers that can be eliminated.  */
427*38fd1498Szrj 
428*38fd1498Szrj HARD_REG_SET eliminable_regset;
429*38fd1498Szrj 
430*38fd1498Szrj /* Value of max_reg_num () before IRA work start.  This value helps
431*38fd1498Szrj    us to recognize a situation when new pseudos were created during
432*38fd1498Szrj    IRA work.  */
433*38fd1498Szrj static int max_regno_before_ira;
434*38fd1498Szrj 
435*38fd1498Szrj /* Temporary hard reg set used for a different calculation.  */
436*38fd1498Szrj static HARD_REG_SET temp_hard_regset;
437*38fd1498Szrj 
438*38fd1498Szrj #define last_mode_for_init_move_cost \
439*38fd1498Szrj   (this_target_ira_int->x_last_mode_for_init_move_cost)
440*38fd1498Szrj 
441*38fd1498Szrj 
442*38fd1498Szrj /* The function sets up the map IRA_REG_MODE_HARD_REGSET.  */
443*38fd1498Szrj static void
setup_reg_mode_hard_regset(void)444*38fd1498Szrj setup_reg_mode_hard_regset (void)
445*38fd1498Szrj {
446*38fd1498Szrj   int i, m, hard_regno;
447*38fd1498Szrj 
448*38fd1498Szrj   for (m = 0; m < NUM_MACHINE_MODES; m++)
449*38fd1498Szrj     for (hard_regno = 0; hard_regno < FIRST_PSEUDO_REGISTER; hard_regno++)
450*38fd1498Szrj       {
451*38fd1498Szrj 	CLEAR_HARD_REG_SET (ira_reg_mode_hard_regset[hard_regno][m]);
452*38fd1498Szrj 	for (i = hard_regno_nregs (hard_regno, (machine_mode) m) - 1;
453*38fd1498Szrj 	     i >= 0; i--)
454*38fd1498Szrj 	  if (hard_regno + i < FIRST_PSEUDO_REGISTER)
455*38fd1498Szrj 	    SET_HARD_REG_BIT (ira_reg_mode_hard_regset[hard_regno][m],
456*38fd1498Szrj 			      hard_regno + i);
457*38fd1498Szrj       }
458*38fd1498Szrj }
459*38fd1498Szrj 
460*38fd1498Szrj 
461*38fd1498Szrj #define no_unit_alloc_regs \
462*38fd1498Szrj   (this_target_ira_int->x_no_unit_alloc_regs)
463*38fd1498Szrj 
464*38fd1498Szrj /* The function sets up the three arrays declared above.  */
465*38fd1498Szrj static void
setup_class_hard_regs(void)466*38fd1498Szrj setup_class_hard_regs (void)
467*38fd1498Szrj {
468*38fd1498Szrj   int cl, i, hard_regno, n;
469*38fd1498Szrj   HARD_REG_SET processed_hard_reg_set;
470*38fd1498Szrj 
471*38fd1498Szrj   ira_assert (SHRT_MAX >= FIRST_PSEUDO_REGISTER);
472*38fd1498Szrj   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
473*38fd1498Szrj     {
474*38fd1498Szrj       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
475*38fd1498Szrj       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
476*38fd1498Szrj       CLEAR_HARD_REG_SET (processed_hard_reg_set);
477*38fd1498Szrj       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
478*38fd1498Szrj 	{
479*38fd1498Szrj 	  ira_non_ordered_class_hard_regs[cl][i] = -1;
480*38fd1498Szrj 	  ira_class_hard_reg_index[cl][i] = -1;
481*38fd1498Szrj 	}
482*38fd1498Szrj       for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
483*38fd1498Szrj 	{
484*38fd1498Szrj #ifdef REG_ALLOC_ORDER
485*38fd1498Szrj 	  hard_regno = reg_alloc_order[i];
486*38fd1498Szrj #else
487*38fd1498Szrj 	  hard_regno = i;
488*38fd1498Szrj #endif
489*38fd1498Szrj 	  if (TEST_HARD_REG_BIT (processed_hard_reg_set, hard_regno))
490*38fd1498Szrj 	    continue;
491*38fd1498Szrj 	  SET_HARD_REG_BIT (processed_hard_reg_set, hard_regno);
492*38fd1498Szrj       	  if (! TEST_HARD_REG_BIT (temp_hard_regset, hard_regno))
493*38fd1498Szrj 	    ira_class_hard_reg_index[cl][hard_regno] = -1;
494*38fd1498Szrj 	  else
495*38fd1498Szrj 	    {
496*38fd1498Szrj 	      ira_class_hard_reg_index[cl][hard_regno] = n;
497*38fd1498Szrj 	      ira_class_hard_regs[cl][n++] = hard_regno;
498*38fd1498Szrj 	    }
499*38fd1498Szrj 	}
500*38fd1498Szrj       ira_class_hard_regs_num[cl] = n;
501*38fd1498Szrj       for (n = 0, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
502*38fd1498Szrj 	if (TEST_HARD_REG_BIT (temp_hard_regset, i))
503*38fd1498Szrj 	  ira_non_ordered_class_hard_regs[cl][n++] = i;
504*38fd1498Szrj       ira_assert (ira_class_hard_regs_num[cl] == n);
505*38fd1498Szrj     }
506*38fd1498Szrj }
507*38fd1498Szrj 
508*38fd1498Szrj /* Set up global variables defining info about hard registers for the
509*38fd1498Szrj    allocation.  These depend on USE_HARD_FRAME_P whose TRUE value means
510*38fd1498Szrj    that we can use the hard frame pointer for the allocation.  */
511*38fd1498Szrj static void
setup_alloc_regs(bool use_hard_frame_p)512*38fd1498Szrj setup_alloc_regs (bool use_hard_frame_p)
513*38fd1498Szrj {
514*38fd1498Szrj #ifdef ADJUST_REG_ALLOC_ORDER
515*38fd1498Szrj   ADJUST_REG_ALLOC_ORDER;
516*38fd1498Szrj #endif
517*38fd1498Szrj   COPY_HARD_REG_SET (no_unit_alloc_regs, fixed_nonglobal_reg_set);
518*38fd1498Szrj   if (! use_hard_frame_p)
519*38fd1498Szrj     SET_HARD_REG_BIT (no_unit_alloc_regs, HARD_FRAME_POINTER_REGNUM);
520*38fd1498Szrj   setup_class_hard_regs ();
521*38fd1498Szrj }
522*38fd1498Szrj 
523*38fd1498Szrj 
524*38fd1498Szrj 
525*38fd1498Szrj #define alloc_reg_class_subclasses \
526*38fd1498Szrj   (this_target_ira_int->x_alloc_reg_class_subclasses)
527*38fd1498Szrj 
528*38fd1498Szrj /* Initialize the table of subclasses of each reg class.  */
529*38fd1498Szrj static void
setup_reg_subclasses(void)530*38fd1498Szrj setup_reg_subclasses (void)
531*38fd1498Szrj {
532*38fd1498Szrj   int i, j;
533*38fd1498Szrj   HARD_REG_SET temp_hard_regset2;
534*38fd1498Szrj 
535*38fd1498Szrj   for (i = 0; i < N_REG_CLASSES; i++)
536*38fd1498Szrj     for (j = 0; j < N_REG_CLASSES; j++)
537*38fd1498Szrj       alloc_reg_class_subclasses[i][j] = LIM_REG_CLASSES;
538*38fd1498Szrj 
539*38fd1498Szrj   for (i = 0; i < N_REG_CLASSES; i++)
540*38fd1498Szrj     {
541*38fd1498Szrj       if (i == (int) NO_REGS)
542*38fd1498Szrj 	continue;
543*38fd1498Szrj 
544*38fd1498Szrj       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
545*38fd1498Szrj       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
546*38fd1498Szrj       if (hard_reg_set_empty_p (temp_hard_regset))
547*38fd1498Szrj 	continue;
548*38fd1498Szrj       for (j = 0; j < N_REG_CLASSES; j++)
549*38fd1498Szrj 	if (i != j)
550*38fd1498Szrj 	  {
551*38fd1498Szrj 	    enum reg_class *p;
552*38fd1498Szrj 
553*38fd1498Szrj 	    COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[j]);
554*38fd1498Szrj 	    AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
555*38fd1498Szrj 	    if (! hard_reg_set_subset_p (temp_hard_regset,
556*38fd1498Szrj 					 temp_hard_regset2))
557*38fd1498Szrj 	      continue;
558*38fd1498Szrj 	    p = &alloc_reg_class_subclasses[j][0];
559*38fd1498Szrj 	    while (*p != LIM_REG_CLASSES) p++;
560*38fd1498Szrj 	    *p = (enum reg_class) i;
561*38fd1498Szrj 	  }
562*38fd1498Szrj     }
563*38fd1498Szrj }
564*38fd1498Szrj 
565*38fd1498Szrj 
566*38fd1498Szrj 
567*38fd1498Szrj /* Set up IRA_MEMORY_MOVE_COST and IRA_MAX_MEMORY_MOVE_COST.  */
568*38fd1498Szrj static void
setup_class_subset_and_memory_move_costs(void)569*38fd1498Szrj setup_class_subset_and_memory_move_costs (void)
570*38fd1498Szrj {
571*38fd1498Szrj   int cl, cl2, mode, cost;
572*38fd1498Szrj   HARD_REG_SET temp_hard_regset2;
573*38fd1498Szrj 
574*38fd1498Szrj   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
575*38fd1498Szrj     ira_memory_move_cost[mode][NO_REGS][0]
576*38fd1498Szrj       = ira_memory_move_cost[mode][NO_REGS][1] = SHRT_MAX;
577*38fd1498Szrj   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
578*38fd1498Szrj     {
579*38fd1498Szrj       if (cl != (int) NO_REGS)
580*38fd1498Szrj 	for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
581*38fd1498Szrj 	  {
582*38fd1498Szrj 	    ira_max_memory_move_cost[mode][cl][0]
583*38fd1498Szrj 	      = ira_memory_move_cost[mode][cl][0]
584*38fd1498Szrj 	      = memory_move_cost ((machine_mode) mode,
585*38fd1498Szrj 				  (reg_class_t) cl, false);
586*38fd1498Szrj 	    ira_max_memory_move_cost[mode][cl][1]
587*38fd1498Szrj 	      = ira_memory_move_cost[mode][cl][1]
588*38fd1498Szrj 	      = memory_move_cost ((machine_mode) mode,
589*38fd1498Szrj 				  (reg_class_t) cl, true);
590*38fd1498Szrj 	    /* Costs for NO_REGS are used in cost calculation on the
591*38fd1498Szrj 	       1st pass when the preferred register classes are not
592*38fd1498Szrj 	       known yet.  In this case we take the best scenario.  */
593*38fd1498Szrj 	    if (ira_memory_move_cost[mode][NO_REGS][0]
594*38fd1498Szrj 		> ira_memory_move_cost[mode][cl][0])
595*38fd1498Szrj 	      ira_max_memory_move_cost[mode][NO_REGS][0]
596*38fd1498Szrj 		= ira_memory_move_cost[mode][NO_REGS][0]
597*38fd1498Szrj 		= ira_memory_move_cost[mode][cl][0];
598*38fd1498Szrj 	    if (ira_memory_move_cost[mode][NO_REGS][1]
599*38fd1498Szrj 		> ira_memory_move_cost[mode][cl][1])
600*38fd1498Szrj 	      ira_max_memory_move_cost[mode][NO_REGS][1]
601*38fd1498Szrj 		= ira_memory_move_cost[mode][NO_REGS][1]
602*38fd1498Szrj 		= ira_memory_move_cost[mode][cl][1];
603*38fd1498Szrj 	  }
604*38fd1498Szrj     }
605*38fd1498Szrj   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
606*38fd1498Szrj     for (cl2 = (int) N_REG_CLASSES - 1; cl2 >= 0; cl2--)
607*38fd1498Szrj       {
608*38fd1498Szrj 	COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
609*38fd1498Szrj 	AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
610*38fd1498Szrj 	COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl2]);
611*38fd1498Szrj 	AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
612*38fd1498Szrj 	ira_class_subset_p[cl][cl2]
613*38fd1498Szrj 	  = hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2);
614*38fd1498Szrj 	if (! hard_reg_set_empty_p (temp_hard_regset2)
615*38fd1498Szrj 	    && hard_reg_set_subset_p (reg_class_contents[cl2],
616*38fd1498Szrj 				      reg_class_contents[cl]))
617*38fd1498Szrj 	  for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
618*38fd1498Szrj 	    {
619*38fd1498Szrj 	      cost = ira_memory_move_cost[mode][cl2][0];
620*38fd1498Szrj 	      if (cost > ira_max_memory_move_cost[mode][cl][0])
621*38fd1498Szrj 		ira_max_memory_move_cost[mode][cl][0] = cost;
622*38fd1498Szrj 	      cost = ira_memory_move_cost[mode][cl2][1];
623*38fd1498Szrj 	      if (cost > ira_max_memory_move_cost[mode][cl][1])
624*38fd1498Szrj 		ira_max_memory_move_cost[mode][cl][1] = cost;
625*38fd1498Szrj 	    }
626*38fd1498Szrj       }
627*38fd1498Szrj   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
628*38fd1498Szrj     for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
629*38fd1498Szrj       {
630*38fd1498Szrj 	ira_memory_move_cost[mode][cl][0]
631*38fd1498Szrj 	  = ira_max_memory_move_cost[mode][cl][0];
632*38fd1498Szrj 	ira_memory_move_cost[mode][cl][1]
633*38fd1498Szrj 	  = ira_max_memory_move_cost[mode][cl][1];
634*38fd1498Szrj       }
635*38fd1498Szrj   setup_reg_subclasses ();
636*38fd1498Szrj }
637*38fd1498Szrj 
638*38fd1498Szrj 
639*38fd1498Szrj 
640*38fd1498Szrj /* Define the following macro if allocation through malloc if
641*38fd1498Szrj    preferable.  */
642*38fd1498Szrj #define IRA_NO_OBSTACK
643*38fd1498Szrj 
644*38fd1498Szrj #ifndef IRA_NO_OBSTACK
645*38fd1498Szrj /* Obstack used for storing all dynamic data (except bitmaps) of the
646*38fd1498Szrj    IRA.  */
647*38fd1498Szrj static struct obstack ira_obstack;
648*38fd1498Szrj #endif
649*38fd1498Szrj 
650*38fd1498Szrj /* Obstack used for storing all bitmaps of the IRA.  */
651*38fd1498Szrj static struct bitmap_obstack ira_bitmap_obstack;
652*38fd1498Szrj 
653*38fd1498Szrj /* Allocate memory of size LEN for IRA data.  */
654*38fd1498Szrj void *
ira_allocate(size_t len)655*38fd1498Szrj ira_allocate (size_t len)
656*38fd1498Szrj {
657*38fd1498Szrj   void *res;
658*38fd1498Szrj 
659*38fd1498Szrj #ifndef IRA_NO_OBSTACK
660*38fd1498Szrj   res = obstack_alloc (&ira_obstack, len);
661*38fd1498Szrj #else
662*38fd1498Szrj   res = xmalloc (len);
663*38fd1498Szrj #endif
664*38fd1498Szrj   return res;
665*38fd1498Szrj }
666*38fd1498Szrj 
667*38fd1498Szrj /* Free memory ADDR allocated for IRA data.  */
668*38fd1498Szrj void
ira_free(void * addr ATTRIBUTE_UNUSED)669*38fd1498Szrj ira_free (void *addr ATTRIBUTE_UNUSED)
670*38fd1498Szrj {
671*38fd1498Szrj #ifndef IRA_NO_OBSTACK
672*38fd1498Szrj   /* do nothing */
673*38fd1498Szrj #else
674*38fd1498Szrj   free (addr);
675*38fd1498Szrj #endif
676*38fd1498Szrj }
677*38fd1498Szrj 
678*38fd1498Szrj 
679*38fd1498Szrj /* Allocate and returns bitmap for IRA.  */
680*38fd1498Szrj bitmap
ira_allocate_bitmap(void)681*38fd1498Szrj ira_allocate_bitmap (void)
682*38fd1498Szrj {
683*38fd1498Szrj   return BITMAP_ALLOC (&ira_bitmap_obstack);
684*38fd1498Szrj }
685*38fd1498Szrj 
686*38fd1498Szrj /* Free bitmap B allocated for IRA.  */
687*38fd1498Szrj void
ira_free_bitmap(bitmap b ATTRIBUTE_UNUSED)688*38fd1498Szrj ira_free_bitmap (bitmap b ATTRIBUTE_UNUSED)
689*38fd1498Szrj {
690*38fd1498Szrj   /* do nothing */
691*38fd1498Szrj }
692*38fd1498Szrj 
693*38fd1498Szrj 
694*38fd1498Szrj 
695*38fd1498Szrj /* Output information about allocation of all allocnos (except for
696*38fd1498Szrj    caps) into file F.  */
697*38fd1498Szrj void
ira_print_disposition(FILE * f)698*38fd1498Szrj ira_print_disposition (FILE *f)
699*38fd1498Szrj {
700*38fd1498Szrj   int i, n, max_regno;
701*38fd1498Szrj   ira_allocno_t a;
702*38fd1498Szrj   basic_block bb;
703*38fd1498Szrj 
704*38fd1498Szrj   fprintf (f, "Disposition:");
705*38fd1498Szrj   max_regno = max_reg_num ();
706*38fd1498Szrj   for (n = 0, i = FIRST_PSEUDO_REGISTER; i < max_regno; i++)
707*38fd1498Szrj     for (a = ira_regno_allocno_map[i];
708*38fd1498Szrj 	 a != NULL;
709*38fd1498Szrj 	 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
710*38fd1498Szrj       {
711*38fd1498Szrj 	if (n % 4 == 0)
712*38fd1498Szrj 	  fprintf (f, "\n");
713*38fd1498Szrj 	n++;
714*38fd1498Szrj 	fprintf (f, " %4d:r%-4d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
715*38fd1498Szrj 	if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
716*38fd1498Szrj 	  fprintf (f, "b%-3d", bb->index);
717*38fd1498Szrj 	else
718*38fd1498Szrj 	  fprintf (f, "l%-3d", ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
719*38fd1498Szrj 	if (ALLOCNO_HARD_REGNO (a) >= 0)
720*38fd1498Szrj 	  fprintf (f, " %3d", ALLOCNO_HARD_REGNO (a));
721*38fd1498Szrj 	else
722*38fd1498Szrj 	  fprintf (f, " mem");
723*38fd1498Szrj       }
724*38fd1498Szrj   fprintf (f, "\n");
725*38fd1498Szrj }
726*38fd1498Szrj 
727*38fd1498Szrj /* Outputs information about allocation of all allocnos into
728*38fd1498Szrj    stderr.  */
729*38fd1498Szrj void
ira_debug_disposition(void)730*38fd1498Szrj ira_debug_disposition (void)
731*38fd1498Szrj {
732*38fd1498Szrj   ira_print_disposition (stderr);
733*38fd1498Szrj }
734*38fd1498Szrj 
735*38fd1498Szrj 
736*38fd1498Szrj 
737*38fd1498Szrj /* Set up ira_stack_reg_pressure_class which is the biggest pressure
738*38fd1498Szrj    register class containing stack registers or NO_REGS if there are
739*38fd1498Szrj    no stack registers.  To find this class, we iterate through all
740*38fd1498Szrj    register pressure classes and choose the first register pressure
741*38fd1498Szrj    class containing all the stack registers and having the biggest
742*38fd1498Szrj    size.  */
743*38fd1498Szrj static void
setup_stack_reg_pressure_class(void)744*38fd1498Szrj setup_stack_reg_pressure_class (void)
745*38fd1498Szrj {
746*38fd1498Szrj   ira_stack_reg_pressure_class = NO_REGS;
747*38fd1498Szrj #ifdef STACK_REGS
748*38fd1498Szrj   {
749*38fd1498Szrj     int i, best, size;
750*38fd1498Szrj     enum reg_class cl;
751*38fd1498Szrj     HARD_REG_SET temp_hard_regset2;
752*38fd1498Szrj 
753*38fd1498Szrj     CLEAR_HARD_REG_SET (temp_hard_regset);
754*38fd1498Szrj     for (i = FIRST_STACK_REG; i <= LAST_STACK_REG; i++)
755*38fd1498Szrj       SET_HARD_REG_BIT (temp_hard_regset, i);
756*38fd1498Szrj     best = 0;
757*38fd1498Szrj     for (i = 0; i < ira_pressure_classes_num; i++)
758*38fd1498Szrj       {
759*38fd1498Szrj 	cl = ira_pressure_classes[i];
760*38fd1498Szrj 	COPY_HARD_REG_SET (temp_hard_regset2, temp_hard_regset);
761*38fd1498Szrj 	AND_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
762*38fd1498Szrj 	size = hard_reg_set_size (temp_hard_regset2);
763*38fd1498Szrj 	if (best < size)
764*38fd1498Szrj 	  {
765*38fd1498Szrj 	    best = size;
766*38fd1498Szrj 	    ira_stack_reg_pressure_class = cl;
767*38fd1498Szrj 	  }
768*38fd1498Szrj       }
769*38fd1498Szrj   }
770*38fd1498Szrj #endif
771*38fd1498Szrj }
772*38fd1498Szrj 
773*38fd1498Szrj /* Find pressure classes which are register classes for which we
774*38fd1498Szrj    calculate register pressure in IRA, register pressure sensitive
775*38fd1498Szrj    insn scheduling, and register pressure sensitive loop invariant
776*38fd1498Szrj    motion.
777*38fd1498Szrj 
778*38fd1498Szrj    To make register pressure calculation easy, we always use
779*38fd1498Szrj    non-intersected register pressure classes.  A move of hard
780*38fd1498Szrj    registers from one register pressure class is not more expensive
781*38fd1498Szrj    than load and store of the hard registers.  Most likely an allocno
782*38fd1498Szrj    class will be a subset of a register pressure class and in many
783*38fd1498Szrj    cases a register pressure class.  That makes usage of register
784*38fd1498Szrj    pressure classes a good approximation to find a high register
785*38fd1498Szrj    pressure.  */
786*38fd1498Szrj static void
setup_pressure_classes(void)787*38fd1498Szrj setup_pressure_classes (void)
788*38fd1498Szrj {
789*38fd1498Szrj   int cost, i, n, curr;
790*38fd1498Szrj   int cl, cl2;
791*38fd1498Szrj   enum reg_class pressure_classes[N_REG_CLASSES];
792*38fd1498Szrj   int m;
793*38fd1498Szrj   HARD_REG_SET temp_hard_regset2;
794*38fd1498Szrj   bool insert_p;
795*38fd1498Szrj 
796*38fd1498Szrj   if (targetm.compute_pressure_classes)
797*38fd1498Szrj     n = targetm.compute_pressure_classes (pressure_classes);
798*38fd1498Szrj   else
799*38fd1498Szrj     {
800*38fd1498Szrj       n = 0;
801*38fd1498Szrj       for (cl = 0; cl < N_REG_CLASSES; cl++)
802*38fd1498Szrj 	{
803*38fd1498Szrj 	  if (ira_class_hard_regs_num[cl] == 0)
804*38fd1498Szrj 	    continue;
805*38fd1498Szrj 	  if (ira_class_hard_regs_num[cl] != 1
806*38fd1498Szrj 	      /* A register class without subclasses may contain a few
807*38fd1498Szrj 		 hard registers and movement between them is costly
808*38fd1498Szrj 		 (e.g. SPARC FPCC registers).  We still should consider it
809*38fd1498Szrj 		 as a candidate for a pressure class.  */
810*38fd1498Szrj 	      && alloc_reg_class_subclasses[cl][0] < cl)
811*38fd1498Szrj 	    {
812*38fd1498Szrj 	      /* Check that the moves between any hard registers of the
813*38fd1498Szrj 		 current class are not more expensive for a legal mode
814*38fd1498Szrj 		 than load/store of the hard registers of the current
815*38fd1498Szrj 		 class.  Such class is a potential candidate to be a
816*38fd1498Szrj 		 register pressure class.  */
817*38fd1498Szrj 	      for (m = 0; m < NUM_MACHINE_MODES; m++)
818*38fd1498Szrj 		{
819*38fd1498Szrj 		  COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
820*38fd1498Szrj 		  AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
821*38fd1498Szrj 		  AND_COMPL_HARD_REG_SET (temp_hard_regset,
822*38fd1498Szrj 					  ira_prohibited_class_mode_regs[cl][m]);
823*38fd1498Szrj 		  if (hard_reg_set_empty_p (temp_hard_regset))
824*38fd1498Szrj 		    continue;
825*38fd1498Szrj 		  ira_init_register_move_cost_if_necessary ((machine_mode) m);
826*38fd1498Szrj 		  cost = ira_register_move_cost[m][cl][cl];
827*38fd1498Szrj 		  if (cost <= ira_max_memory_move_cost[m][cl][1]
828*38fd1498Szrj 		      || cost <= ira_max_memory_move_cost[m][cl][0])
829*38fd1498Szrj 		    break;
830*38fd1498Szrj 		}
831*38fd1498Szrj 	      if (m >= NUM_MACHINE_MODES)
832*38fd1498Szrj 		continue;
833*38fd1498Szrj 	    }
834*38fd1498Szrj 	  curr = 0;
835*38fd1498Szrj 	  insert_p = true;
836*38fd1498Szrj 	  COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
837*38fd1498Szrj 	  AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
838*38fd1498Szrj 	  /* Remove so far added pressure classes which are subset of the
839*38fd1498Szrj 	     current candidate class.  Prefer GENERAL_REGS as a pressure
840*38fd1498Szrj 	     register class to another class containing the same
841*38fd1498Szrj 	     allocatable hard registers.  We do this because machine
842*38fd1498Szrj 	     dependent cost hooks might give wrong costs for the latter
843*38fd1498Szrj 	     class but always give the right cost for the former class
844*38fd1498Szrj 	     (GENERAL_REGS).  */
845*38fd1498Szrj 	  for (i = 0; i < n; i++)
846*38fd1498Szrj 	    {
847*38fd1498Szrj 	      cl2 = pressure_classes[i];
848*38fd1498Szrj 	      COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl2]);
849*38fd1498Szrj 	      AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
850*38fd1498Szrj 	      if (hard_reg_set_subset_p (temp_hard_regset, temp_hard_regset2)
851*38fd1498Szrj 		  && (! hard_reg_set_equal_p (temp_hard_regset,
852*38fd1498Szrj 					      temp_hard_regset2)
853*38fd1498Szrj 		      || cl2 == (int) GENERAL_REGS))
854*38fd1498Szrj 		{
855*38fd1498Szrj 		  pressure_classes[curr++] = (enum reg_class) cl2;
856*38fd1498Szrj 		  insert_p = false;
857*38fd1498Szrj 		  continue;
858*38fd1498Szrj 		}
859*38fd1498Szrj 	      if (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset)
860*38fd1498Szrj 		  && (! hard_reg_set_equal_p (temp_hard_regset2,
861*38fd1498Szrj 					      temp_hard_regset)
862*38fd1498Szrj 		      || cl == (int) GENERAL_REGS))
863*38fd1498Szrj 		continue;
864*38fd1498Szrj 	      if (hard_reg_set_equal_p (temp_hard_regset2, temp_hard_regset))
865*38fd1498Szrj 		insert_p = false;
866*38fd1498Szrj 	      pressure_classes[curr++] = (enum reg_class) cl2;
867*38fd1498Szrj 	    }
868*38fd1498Szrj 	  /* If the current candidate is a subset of a so far added
869*38fd1498Szrj 	     pressure class, don't add it to the list of the pressure
870*38fd1498Szrj 	     classes.  */
871*38fd1498Szrj 	  if (insert_p)
872*38fd1498Szrj 	    pressure_classes[curr++] = (enum reg_class) cl;
873*38fd1498Szrj 	  n = curr;
874*38fd1498Szrj 	}
875*38fd1498Szrj     }
876*38fd1498Szrj #ifdef ENABLE_IRA_CHECKING
877*38fd1498Szrj   {
878*38fd1498Szrj     HARD_REG_SET ignore_hard_regs;
879*38fd1498Szrj 
880*38fd1498Szrj     /* Check pressure classes correctness: here we check that hard
881*38fd1498Szrj        registers from all register pressure classes contains all hard
882*38fd1498Szrj        registers available for the allocation.  */
883*38fd1498Szrj     CLEAR_HARD_REG_SET (temp_hard_regset);
884*38fd1498Szrj     CLEAR_HARD_REG_SET (temp_hard_regset2);
885*38fd1498Szrj     COPY_HARD_REG_SET (ignore_hard_regs, no_unit_alloc_regs);
886*38fd1498Szrj     for (cl = 0; cl < LIM_REG_CLASSES; cl++)
887*38fd1498Szrj       {
888*38fd1498Szrj 	/* For some targets (like MIPS with MD_REGS), there are some
889*38fd1498Szrj 	   classes with hard registers available for allocation but
890*38fd1498Szrj 	   not able to hold value of any mode.  */
891*38fd1498Szrj 	for (m = 0; m < NUM_MACHINE_MODES; m++)
892*38fd1498Szrj 	  if (contains_reg_of_mode[cl][m])
893*38fd1498Szrj 	    break;
894*38fd1498Szrj 	if (m >= NUM_MACHINE_MODES)
895*38fd1498Szrj 	  {
896*38fd1498Szrj 	    IOR_HARD_REG_SET (ignore_hard_regs, reg_class_contents[cl]);
897*38fd1498Szrj 	    continue;
898*38fd1498Szrj 	  }
899*38fd1498Szrj 	for (i = 0; i < n; i++)
900*38fd1498Szrj 	  if ((int) pressure_classes[i] == cl)
901*38fd1498Szrj 	    break;
902*38fd1498Szrj 	IOR_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
903*38fd1498Szrj 	if (i < n)
904*38fd1498Szrj 	  IOR_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
905*38fd1498Szrj       }
906*38fd1498Szrj     for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
907*38fd1498Szrj       /* Some targets (like SPARC with ICC reg) have allocatable regs
908*38fd1498Szrj 	 for which no reg class is defined.  */
909*38fd1498Szrj       if (REGNO_REG_CLASS (i) == NO_REGS)
910*38fd1498Szrj 	SET_HARD_REG_BIT (ignore_hard_regs, i);
911*38fd1498Szrj     AND_COMPL_HARD_REG_SET (temp_hard_regset, ignore_hard_regs);
912*38fd1498Szrj     AND_COMPL_HARD_REG_SET (temp_hard_regset2, ignore_hard_regs);
913*38fd1498Szrj     ira_assert (hard_reg_set_subset_p (temp_hard_regset2, temp_hard_regset));
914*38fd1498Szrj   }
915*38fd1498Szrj #endif
916*38fd1498Szrj   ira_pressure_classes_num = 0;
917*38fd1498Szrj   for (i = 0; i < n; i++)
918*38fd1498Szrj     {
919*38fd1498Szrj       cl = (int) pressure_classes[i];
920*38fd1498Szrj       ira_reg_pressure_class_p[cl] = true;
921*38fd1498Szrj       ira_pressure_classes[ira_pressure_classes_num++] = (enum reg_class) cl;
922*38fd1498Szrj     }
923*38fd1498Szrj   setup_stack_reg_pressure_class ();
924*38fd1498Szrj }
925*38fd1498Szrj 
926*38fd1498Szrj /* Set up IRA_UNIFORM_CLASS_P.  Uniform class is a register class
927*38fd1498Szrj    whose register move cost between any registers of the class is the
928*38fd1498Szrj    same as for all its subclasses.  We use the data to speed up the
929*38fd1498Szrj    2nd pass of calculations of allocno costs.  */
930*38fd1498Szrj static void
setup_uniform_class_p(void)931*38fd1498Szrj setup_uniform_class_p (void)
932*38fd1498Szrj {
933*38fd1498Szrj   int i, cl, cl2, m;
934*38fd1498Szrj 
935*38fd1498Szrj   for (cl = 0; cl < N_REG_CLASSES; cl++)
936*38fd1498Szrj     {
937*38fd1498Szrj       ira_uniform_class_p[cl] = false;
938*38fd1498Szrj       if (ira_class_hard_regs_num[cl] == 0)
939*38fd1498Szrj 	continue;
940*38fd1498Szrj       /* We can not use alloc_reg_class_subclasses here because move
941*38fd1498Szrj 	 cost hooks does not take into account that some registers are
942*38fd1498Szrj 	 unavailable for the subtarget.  E.g. for i686, INT_SSE_REGS
943*38fd1498Szrj 	 is element of alloc_reg_class_subclasses for GENERAL_REGS
944*38fd1498Szrj 	 because SSE regs are unavailable.  */
945*38fd1498Szrj       for (i = 0; (cl2 = reg_class_subclasses[cl][i]) != LIM_REG_CLASSES; i++)
946*38fd1498Szrj 	{
947*38fd1498Szrj 	  if (ira_class_hard_regs_num[cl2] == 0)
948*38fd1498Szrj 	    continue;
949*38fd1498Szrj       	  for (m = 0; m < NUM_MACHINE_MODES; m++)
950*38fd1498Szrj 	    if (contains_reg_of_mode[cl][m] && contains_reg_of_mode[cl2][m])
951*38fd1498Szrj 	      {
952*38fd1498Szrj 		ira_init_register_move_cost_if_necessary ((machine_mode) m);
953*38fd1498Szrj 		if (ira_register_move_cost[m][cl][cl]
954*38fd1498Szrj 		    != ira_register_move_cost[m][cl2][cl2])
955*38fd1498Szrj 		  break;
956*38fd1498Szrj 	      }
957*38fd1498Szrj 	  if (m < NUM_MACHINE_MODES)
958*38fd1498Szrj 	    break;
959*38fd1498Szrj 	}
960*38fd1498Szrj       if (cl2 == LIM_REG_CLASSES)
961*38fd1498Szrj 	ira_uniform_class_p[cl] = true;
962*38fd1498Szrj     }
963*38fd1498Szrj }
964*38fd1498Szrj 
965*38fd1498Szrj /* Set up IRA_ALLOCNO_CLASSES, IRA_ALLOCNO_CLASSES_NUM,
966*38fd1498Szrj    IRA_IMPORTANT_CLASSES, and IRA_IMPORTANT_CLASSES_NUM.
967*38fd1498Szrj 
968*38fd1498Szrj    Target may have many subtargets and not all target hard registers can
969*38fd1498Szrj    be used for allocation, e.g. x86 port in 32-bit mode can not use
970*38fd1498Szrj    hard registers introduced in x86-64 like r8-r15).  Some classes
971*38fd1498Szrj    might have the same allocatable hard registers, e.g.  INDEX_REGS
972*38fd1498Szrj    and GENERAL_REGS in x86 port in 32-bit mode.  To decrease different
973*38fd1498Szrj    calculations efforts we introduce allocno classes which contain
974*38fd1498Szrj    unique non-empty sets of allocatable hard-registers.
975*38fd1498Szrj 
976*38fd1498Szrj    Pseudo class cost calculation in ira-costs.c is very expensive.
977*38fd1498Szrj    Therefore we are trying to decrease number of classes involved in
978*38fd1498Szrj    such calculation.  Register classes used in the cost calculation
979*38fd1498Szrj    are called important classes.  They are allocno classes and other
980*38fd1498Szrj    non-empty classes whose allocatable hard register sets are inside
981*38fd1498Szrj    of an allocno class hard register set.  From the first sight, it
982*38fd1498Szrj    looks like that they are just allocno classes.  It is not true.  In
983*38fd1498Szrj    example of x86-port in 32-bit mode, allocno classes will contain
984*38fd1498Szrj    GENERAL_REGS but not LEGACY_REGS (because allocatable hard
985*38fd1498Szrj    registers are the same for the both classes).  The important
986*38fd1498Szrj    classes will contain GENERAL_REGS and LEGACY_REGS.  It is done
987*38fd1498Szrj    because a machine description insn constraint may refers for
988*38fd1498Szrj    LEGACY_REGS and code in ira-costs.c is mostly base on investigation
989*38fd1498Szrj    of the insn constraints.  */
990*38fd1498Szrj static void
setup_allocno_and_important_classes(void)991*38fd1498Szrj setup_allocno_and_important_classes (void)
992*38fd1498Szrj {
993*38fd1498Szrj   int i, j, n, cl;
994*38fd1498Szrj   bool set_p;
995*38fd1498Szrj   HARD_REG_SET temp_hard_regset2;
996*38fd1498Szrj   static enum reg_class classes[LIM_REG_CLASSES + 1];
997*38fd1498Szrj 
998*38fd1498Szrj   n = 0;
999*38fd1498Szrj   /* Collect classes which contain unique sets of allocatable hard
1000*38fd1498Szrj      registers.  Prefer GENERAL_REGS to other classes containing the
1001*38fd1498Szrj      same set of hard registers.  */
1002*38fd1498Szrj   for (i = 0; i < LIM_REG_CLASSES; i++)
1003*38fd1498Szrj     {
1004*38fd1498Szrj       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[i]);
1005*38fd1498Szrj       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1006*38fd1498Szrj       for (j = 0; j < n; j++)
1007*38fd1498Szrj 	{
1008*38fd1498Szrj 	  cl = classes[j];
1009*38fd1498Szrj 	  COPY_HARD_REG_SET (temp_hard_regset2, reg_class_contents[cl]);
1010*38fd1498Szrj 	  AND_COMPL_HARD_REG_SET (temp_hard_regset2,
1011*38fd1498Szrj 				  no_unit_alloc_regs);
1012*38fd1498Szrj 	  if (hard_reg_set_equal_p (temp_hard_regset,
1013*38fd1498Szrj 				    temp_hard_regset2))
1014*38fd1498Szrj 	    break;
1015*38fd1498Szrj 	}
1016*38fd1498Szrj       if (j >= n || targetm.additional_allocno_class_p (i))
1017*38fd1498Szrj 	classes[n++] = (enum reg_class) i;
1018*38fd1498Szrj       else if (i == GENERAL_REGS)
1019*38fd1498Szrj 	/* Prefer general regs.  For i386 example, it means that
1020*38fd1498Szrj 	   we prefer GENERAL_REGS over INDEX_REGS or LEGACY_REGS
1021*38fd1498Szrj 	   (all of them consists of the same available hard
1022*38fd1498Szrj 	   registers).  */
1023*38fd1498Szrj 	classes[j] = (enum reg_class) i;
1024*38fd1498Szrj     }
1025*38fd1498Szrj   classes[n] = LIM_REG_CLASSES;
1026*38fd1498Szrj 
1027*38fd1498Szrj   /* Set up classes which can be used for allocnos as classes
1028*38fd1498Szrj      containing non-empty unique sets of allocatable hard
1029*38fd1498Szrj      registers.  */
1030*38fd1498Szrj   ira_allocno_classes_num = 0;
1031*38fd1498Szrj   for (i = 0; (cl = classes[i]) != LIM_REG_CLASSES; i++)
1032*38fd1498Szrj     if (ira_class_hard_regs_num[cl] > 0)
1033*38fd1498Szrj       ira_allocno_classes[ira_allocno_classes_num++] = (enum reg_class) cl;
1034*38fd1498Szrj   ira_important_classes_num = 0;
1035*38fd1498Szrj   /* Add non-allocno classes containing to non-empty set of
1036*38fd1498Szrj      allocatable hard regs.  */
1037*38fd1498Szrj   for (cl = 0; cl < N_REG_CLASSES; cl++)
1038*38fd1498Szrj     if (ira_class_hard_regs_num[cl] > 0)
1039*38fd1498Szrj       {
1040*38fd1498Szrj 	COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
1041*38fd1498Szrj 	AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1042*38fd1498Szrj 	set_p = false;
1043*38fd1498Szrj 	for (j = 0; j < ira_allocno_classes_num; j++)
1044*38fd1498Szrj 	  {
1045*38fd1498Szrj 	    COPY_HARD_REG_SET (temp_hard_regset2,
1046*38fd1498Szrj 			       reg_class_contents[ira_allocno_classes[j]]);
1047*38fd1498Szrj 	    AND_COMPL_HARD_REG_SET (temp_hard_regset2, no_unit_alloc_regs);
1048*38fd1498Szrj 	    if ((enum reg_class) cl == ira_allocno_classes[j])
1049*38fd1498Szrj 	      break;
1050*38fd1498Szrj 	    else if (hard_reg_set_subset_p (temp_hard_regset,
1051*38fd1498Szrj 					    temp_hard_regset2))
1052*38fd1498Szrj 	      set_p = true;
1053*38fd1498Szrj 	  }
1054*38fd1498Szrj 	if (set_p && j >= ira_allocno_classes_num)
1055*38fd1498Szrj 	  ira_important_classes[ira_important_classes_num++]
1056*38fd1498Szrj 	    = (enum reg_class) cl;
1057*38fd1498Szrj       }
1058*38fd1498Szrj   /* Now add allocno classes to the important classes.  */
1059*38fd1498Szrj   for (j = 0; j < ira_allocno_classes_num; j++)
1060*38fd1498Szrj     ira_important_classes[ira_important_classes_num++]
1061*38fd1498Szrj       = ira_allocno_classes[j];
1062*38fd1498Szrj   for (cl = 0; cl < N_REG_CLASSES; cl++)
1063*38fd1498Szrj     {
1064*38fd1498Szrj       ira_reg_allocno_class_p[cl] = false;
1065*38fd1498Szrj       ira_reg_pressure_class_p[cl] = false;
1066*38fd1498Szrj     }
1067*38fd1498Szrj   for (j = 0; j < ira_allocno_classes_num; j++)
1068*38fd1498Szrj     ira_reg_allocno_class_p[ira_allocno_classes[j]] = true;
1069*38fd1498Szrj   setup_pressure_classes ();
1070*38fd1498Szrj   setup_uniform_class_p ();
1071*38fd1498Szrj }
1072*38fd1498Szrj 
1073*38fd1498Szrj /* Setup translation in CLASS_TRANSLATE of all classes into a class
1074*38fd1498Szrj    given by array CLASSES of length CLASSES_NUM.  The function is used
1075*38fd1498Szrj    make translation any reg class to an allocno class or to an
1076*38fd1498Szrj    pressure class.  This translation is necessary for some
1077*38fd1498Szrj    calculations when we can use only allocno or pressure classes and
1078*38fd1498Szrj    such translation represents an approximate representation of all
1079*38fd1498Szrj    classes.
1080*38fd1498Szrj 
1081*38fd1498Szrj    The translation in case when allocatable hard register set of a
1082*38fd1498Szrj    given class is subset of allocatable hard register set of a class
1083*38fd1498Szrj    in CLASSES is pretty simple.  We use smallest classes from CLASSES
1084*38fd1498Szrj    containing a given class.  If allocatable hard register set of a
1085*38fd1498Szrj    given class is not a subset of any corresponding set of a class
1086*38fd1498Szrj    from CLASSES, we use the cheapest (with load/store point of view)
1087*38fd1498Szrj    class from CLASSES whose set intersects with given class set.  */
1088*38fd1498Szrj static void
setup_class_translate_array(enum reg_class * class_translate,int classes_num,enum reg_class * classes)1089*38fd1498Szrj setup_class_translate_array (enum reg_class *class_translate,
1090*38fd1498Szrj 			     int classes_num, enum reg_class *classes)
1091*38fd1498Szrj {
1092*38fd1498Szrj   int cl, mode;
1093*38fd1498Szrj   enum reg_class aclass, best_class, *cl_ptr;
1094*38fd1498Szrj   int i, cost, min_cost, best_cost;
1095*38fd1498Szrj 
1096*38fd1498Szrj   for (cl = 0; cl < N_REG_CLASSES; cl++)
1097*38fd1498Szrj     class_translate[cl] = NO_REGS;
1098*38fd1498Szrj 
1099*38fd1498Szrj   for (i = 0; i < classes_num; i++)
1100*38fd1498Szrj     {
1101*38fd1498Szrj       aclass = classes[i];
1102*38fd1498Szrj       for (cl_ptr = &alloc_reg_class_subclasses[aclass][0];
1103*38fd1498Szrj 	   (cl = *cl_ptr) != LIM_REG_CLASSES;
1104*38fd1498Szrj 	   cl_ptr++)
1105*38fd1498Szrj 	if (class_translate[cl] == NO_REGS)
1106*38fd1498Szrj 	  class_translate[cl] = aclass;
1107*38fd1498Szrj       class_translate[aclass] = aclass;
1108*38fd1498Szrj     }
1109*38fd1498Szrj   /* For classes which are not fully covered by one of given classes
1110*38fd1498Szrj      (in other words covered by more one given class), use the
1111*38fd1498Szrj      cheapest class.  */
1112*38fd1498Szrj   for (cl = 0; cl < N_REG_CLASSES; cl++)
1113*38fd1498Szrj     {
1114*38fd1498Szrj       if (cl == NO_REGS || class_translate[cl] != NO_REGS)
1115*38fd1498Szrj 	continue;
1116*38fd1498Szrj       best_class = NO_REGS;
1117*38fd1498Szrj       best_cost = INT_MAX;
1118*38fd1498Szrj       for (i = 0; i < classes_num; i++)
1119*38fd1498Szrj 	{
1120*38fd1498Szrj 	  aclass = classes[i];
1121*38fd1498Szrj 	  COPY_HARD_REG_SET (temp_hard_regset,
1122*38fd1498Szrj 			     reg_class_contents[aclass]);
1123*38fd1498Szrj 	  AND_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
1124*38fd1498Szrj 	  AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1125*38fd1498Szrj 	  if (! hard_reg_set_empty_p (temp_hard_regset))
1126*38fd1498Szrj 	    {
1127*38fd1498Szrj 	      min_cost = INT_MAX;
1128*38fd1498Szrj 	      for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1129*38fd1498Szrj 		{
1130*38fd1498Szrj 		  cost = (ira_memory_move_cost[mode][aclass][0]
1131*38fd1498Szrj 			  + ira_memory_move_cost[mode][aclass][1]);
1132*38fd1498Szrj 		  if (min_cost > cost)
1133*38fd1498Szrj 		    min_cost = cost;
1134*38fd1498Szrj 		}
1135*38fd1498Szrj 	      if (best_class == NO_REGS || best_cost > min_cost)
1136*38fd1498Szrj 		{
1137*38fd1498Szrj 		  best_class = aclass;
1138*38fd1498Szrj 		  best_cost = min_cost;
1139*38fd1498Szrj 		}
1140*38fd1498Szrj 	    }
1141*38fd1498Szrj 	}
1142*38fd1498Szrj       class_translate[cl] = best_class;
1143*38fd1498Szrj     }
1144*38fd1498Szrj }
1145*38fd1498Szrj 
1146*38fd1498Szrj /* Set up array IRA_ALLOCNO_CLASS_TRANSLATE and
1147*38fd1498Szrj    IRA_PRESSURE_CLASS_TRANSLATE.  */
1148*38fd1498Szrj static void
setup_class_translate(void)1149*38fd1498Szrj setup_class_translate (void)
1150*38fd1498Szrj {
1151*38fd1498Szrj   setup_class_translate_array (ira_allocno_class_translate,
1152*38fd1498Szrj 			       ira_allocno_classes_num, ira_allocno_classes);
1153*38fd1498Szrj   setup_class_translate_array (ira_pressure_class_translate,
1154*38fd1498Szrj 			       ira_pressure_classes_num, ira_pressure_classes);
1155*38fd1498Szrj }
1156*38fd1498Szrj 
1157*38fd1498Szrj /* Order numbers of allocno classes in original target allocno class
1158*38fd1498Szrj    array, -1 for non-allocno classes.  */
1159*38fd1498Szrj static int allocno_class_order[N_REG_CLASSES];
1160*38fd1498Szrj 
1161*38fd1498Szrj /* The function used to sort the important classes.  */
1162*38fd1498Szrj static int
comp_reg_classes_func(const void * v1p,const void * v2p)1163*38fd1498Szrj comp_reg_classes_func (const void *v1p, const void *v2p)
1164*38fd1498Szrj {
1165*38fd1498Szrj   enum reg_class cl1 = *(const enum reg_class *) v1p;
1166*38fd1498Szrj   enum reg_class cl2 = *(const enum reg_class *) v2p;
1167*38fd1498Szrj   enum reg_class tcl1, tcl2;
1168*38fd1498Szrj   int diff;
1169*38fd1498Szrj 
1170*38fd1498Szrj   tcl1 = ira_allocno_class_translate[cl1];
1171*38fd1498Szrj   tcl2 = ira_allocno_class_translate[cl2];
1172*38fd1498Szrj   if (tcl1 != NO_REGS && tcl2 != NO_REGS
1173*38fd1498Szrj       && (diff = allocno_class_order[tcl1] - allocno_class_order[tcl2]) != 0)
1174*38fd1498Szrj     return diff;
1175*38fd1498Szrj   return (int) cl1 - (int) cl2;
1176*38fd1498Szrj }
1177*38fd1498Szrj 
1178*38fd1498Szrj /* For correct work of function setup_reg_class_relation we need to
1179*38fd1498Szrj    reorder important classes according to the order of their allocno
1180*38fd1498Szrj    classes.  It places important classes containing the same
1181*38fd1498Szrj    allocatable hard register set adjacent to each other and allocno
1182*38fd1498Szrj    class with the allocatable hard register set right after the other
1183*38fd1498Szrj    important classes with the same set.
1184*38fd1498Szrj 
1185*38fd1498Szrj    In example from comments of function
1186*38fd1498Szrj    setup_allocno_and_important_classes, it places LEGACY_REGS and
1187*38fd1498Szrj    GENERAL_REGS close to each other and GENERAL_REGS is after
1188*38fd1498Szrj    LEGACY_REGS.  */
1189*38fd1498Szrj static void
reorder_important_classes(void)1190*38fd1498Szrj reorder_important_classes (void)
1191*38fd1498Szrj {
1192*38fd1498Szrj   int i;
1193*38fd1498Szrj 
1194*38fd1498Szrj   for (i = 0; i < N_REG_CLASSES; i++)
1195*38fd1498Szrj     allocno_class_order[i] = -1;
1196*38fd1498Szrj   for (i = 0; i < ira_allocno_classes_num; i++)
1197*38fd1498Szrj     allocno_class_order[ira_allocno_classes[i]] = i;
1198*38fd1498Szrj   qsort (ira_important_classes, ira_important_classes_num,
1199*38fd1498Szrj 	 sizeof (enum reg_class), comp_reg_classes_func);
1200*38fd1498Szrj   for (i = 0; i < ira_important_classes_num; i++)
1201*38fd1498Szrj     ira_important_class_nums[ira_important_classes[i]] = i;
1202*38fd1498Szrj }
1203*38fd1498Szrj 
1204*38fd1498Szrj /* Set up IRA_REG_CLASS_SUBUNION, IRA_REG_CLASS_SUPERUNION,
1205*38fd1498Szrj    IRA_REG_CLASS_SUPER_CLASSES, IRA_REG_CLASSES_INTERSECT, and
1206*38fd1498Szrj    IRA_REG_CLASSES_INTERSECT_P.  For the meaning of the relations,
1207*38fd1498Szrj    please see corresponding comments in ira-int.h.  */
1208*38fd1498Szrj static void
setup_reg_class_relations(void)1209*38fd1498Szrj setup_reg_class_relations (void)
1210*38fd1498Szrj {
1211*38fd1498Szrj   int i, cl1, cl2, cl3;
1212*38fd1498Szrj   HARD_REG_SET intersection_set, union_set, temp_set2;
1213*38fd1498Szrj   bool important_class_p[N_REG_CLASSES];
1214*38fd1498Szrj 
1215*38fd1498Szrj   memset (important_class_p, 0, sizeof (important_class_p));
1216*38fd1498Szrj   for (i = 0; i < ira_important_classes_num; i++)
1217*38fd1498Szrj     important_class_p[ira_important_classes[i]] = true;
1218*38fd1498Szrj   for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1219*38fd1498Szrj     {
1220*38fd1498Szrj       ira_reg_class_super_classes[cl1][0] = LIM_REG_CLASSES;
1221*38fd1498Szrj       for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1222*38fd1498Szrj 	{
1223*38fd1498Szrj 	  ira_reg_classes_intersect_p[cl1][cl2] = false;
1224*38fd1498Szrj 	  ira_reg_class_intersect[cl1][cl2] = NO_REGS;
1225*38fd1498Szrj 	  ira_reg_class_subset[cl1][cl2] = NO_REGS;
1226*38fd1498Szrj 	  COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl1]);
1227*38fd1498Szrj 	  AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1228*38fd1498Szrj 	  COPY_HARD_REG_SET (temp_set2, reg_class_contents[cl2]);
1229*38fd1498Szrj 	  AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1230*38fd1498Szrj 	  if (hard_reg_set_empty_p (temp_hard_regset)
1231*38fd1498Szrj 	      && hard_reg_set_empty_p (temp_set2))
1232*38fd1498Szrj 	    {
1233*38fd1498Szrj 	      /* The both classes have no allocatable hard registers
1234*38fd1498Szrj 		 -- take all class hard registers into account and use
1235*38fd1498Szrj 		 reg_class_subunion and reg_class_superunion.  */
1236*38fd1498Szrj 	      for (i = 0;; i++)
1237*38fd1498Szrj 		{
1238*38fd1498Szrj 		  cl3 = reg_class_subclasses[cl1][i];
1239*38fd1498Szrj 		  if (cl3 == LIM_REG_CLASSES)
1240*38fd1498Szrj 		    break;
1241*38fd1498Szrj 		  if (reg_class_subset_p (ira_reg_class_intersect[cl1][cl2],
1242*38fd1498Szrj 					  (enum reg_class) cl3))
1243*38fd1498Szrj 		    ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1244*38fd1498Szrj 		}
1245*38fd1498Szrj 	      ira_reg_class_subunion[cl1][cl2] = reg_class_subunion[cl1][cl2];
1246*38fd1498Szrj 	      ira_reg_class_superunion[cl1][cl2] = reg_class_superunion[cl1][cl2];
1247*38fd1498Szrj 	      continue;
1248*38fd1498Szrj 	    }
1249*38fd1498Szrj 	  ira_reg_classes_intersect_p[cl1][cl2]
1250*38fd1498Szrj 	    = hard_reg_set_intersect_p (temp_hard_regset, temp_set2);
1251*38fd1498Szrj 	  if (important_class_p[cl1] && important_class_p[cl2]
1252*38fd1498Szrj 	      && hard_reg_set_subset_p (temp_hard_regset, temp_set2))
1253*38fd1498Szrj 	    {
1254*38fd1498Szrj 	      /* CL1 and CL2 are important classes and CL1 allocatable
1255*38fd1498Szrj 		 hard register set is inside of CL2 allocatable hard
1256*38fd1498Szrj 		 registers -- make CL1 a superset of CL2.  */
1257*38fd1498Szrj 	      enum reg_class *p;
1258*38fd1498Szrj 
1259*38fd1498Szrj 	      p = &ira_reg_class_super_classes[cl1][0];
1260*38fd1498Szrj 	      while (*p != LIM_REG_CLASSES)
1261*38fd1498Szrj 		p++;
1262*38fd1498Szrj 	      *p++ = (enum reg_class) cl2;
1263*38fd1498Szrj 	      *p = LIM_REG_CLASSES;
1264*38fd1498Szrj 	    }
1265*38fd1498Szrj 	  ira_reg_class_subunion[cl1][cl2] = NO_REGS;
1266*38fd1498Szrj 	  ira_reg_class_superunion[cl1][cl2] = NO_REGS;
1267*38fd1498Szrj 	  COPY_HARD_REG_SET (intersection_set, reg_class_contents[cl1]);
1268*38fd1498Szrj 	  AND_HARD_REG_SET (intersection_set, reg_class_contents[cl2]);
1269*38fd1498Szrj 	  AND_COMPL_HARD_REG_SET (intersection_set, no_unit_alloc_regs);
1270*38fd1498Szrj 	  COPY_HARD_REG_SET (union_set, reg_class_contents[cl1]);
1271*38fd1498Szrj 	  IOR_HARD_REG_SET (union_set, reg_class_contents[cl2]);
1272*38fd1498Szrj 	  AND_COMPL_HARD_REG_SET (union_set, no_unit_alloc_regs);
1273*38fd1498Szrj 	  for (cl3 = 0; cl3 < N_REG_CLASSES; cl3++)
1274*38fd1498Szrj 	    {
1275*38fd1498Szrj 	      COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl3]);
1276*38fd1498Szrj 	      AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1277*38fd1498Szrj 	      if (hard_reg_set_subset_p (temp_hard_regset, intersection_set))
1278*38fd1498Szrj 		{
1279*38fd1498Szrj 		  /* CL3 allocatable hard register set is inside of
1280*38fd1498Szrj 		     intersection of allocatable hard register sets
1281*38fd1498Szrj 		     of CL1 and CL2.  */
1282*38fd1498Szrj 		  if (important_class_p[cl3])
1283*38fd1498Szrj 		    {
1284*38fd1498Szrj 		      COPY_HARD_REG_SET
1285*38fd1498Szrj 			(temp_set2,
1286*38fd1498Szrj 			 reg_class_contents
1287*38fd1498Szrj 			 [(int) ira_reg_class_intersect[cl1][cl2]]);
1288*38fd1498Szrj 		      AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1289*38fd1498Szrj 		      if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1290*38fd1498Szrj 			  /* If the allocatable hard register sets are
1291*38fd1498Szrj 			     the same, prefer GENERAL_REGS or the
1292*38fd1498Szrj 			     smallest class for debugging
1293*38fd1498Szrj 			     purposes.  */
1294*38fd1498Szrj 			  || (hard_reg_set_equal_p (temp_hard_regset, temp_set2)
1295*38fd1498Szrj 			      && (cl3 == GENERAL_REGS
1296*38fd1498Szrj 				  || ((ira_reg_class_intersect[cl1][cl2]
1297*38fd1498Szrj 				       != GENERAL_REGS)
1298*38fd1498Szrj 				      && hard_reg_set_subset_p
1299*38fd1498Szrj 				         (reg_class_contents[cl3],
1300*38fd1498Szrj 					  reg_class_contents
1301*38fd1498Szrj 					  [(int)
1302*38fd1498Szrj 					   ira_reg_class_intersect[cl1][cl2]])))))
1303*38fd1498Szrj 			ira_reg_class_intersect[cl1][cl2] = (enum reg_class) cl3;
1304*38fd1498Szrj 		    }
1305*38fd1498Szrj 		  COPY_HARD_REG_SET
1306*38fd1498Szrj 		    (temp_set2,
1307*38fd1498Szrj 		     reg_class_contents[(int) ira_reg_class_subset[cl1][cl2]]);
1308*38fd1498Szrj 		  AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1309*38fd1498Szrj 		  if (! hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1310*38fd1498Szrj 		      /* Ignore unavailable hard registers and prefer
1311*38fd1498Szrj 			 smallest class for debugging purposes.  */
1312*38fd1498Szrj 		      || (hard_reg_set_equal_p (temp_hard_regset, temp_set2)
1313*38fd1498Szrj 			  && hard_reg_set_subset_p
1314*38fd1498Szrj 			     (reg_class_contents[cl3],
1315*38fd1498Szrj 			      reg_class_contents
1316*38fd1498Szrj 			      [(int) ira_reg_class_subset[cl1][cl2]])))
1317*38fd1498Szrj 		    ira_reg_class_subset[cl1][cl2] = (enum reg_class) cl3;
1318*38fd1498Szrj 		}
1319*38fd1498Szrj 	      if (important_class_p[cl3]
1320*38fd1498Szrj 		  && hard_reg_set_subset_p (temp_hard_regset, union_set))
1321*38fd1498Szrj 		{
1322*38fd1498Szrj 		  /* CL3 allocatable hard register set is inside of
1323*38fd1498Szrj 		     union of allocatable hard register sets of CL1
1324*38fd1498Szrj 		     and CL2.  */
1325*38fd1498Szrj 		  COPY_HARD_REG_SET
1326*38fd1498Szrj 		    (temp_set2,
1327*38fd1498Szrj 		     reg_class_contents[(int) ira_reg_class_subunion[cl1][cl2]]);
1328*38fd1498Szrj 		  AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1329*38fd1498Szrj 	 	  if (ira_reg_class_subunion[cl1][cl2] == NO_REGS
1330*38fd1498Szrj 		      || (hard_reg_set_subset_p (temp_set2, temp_hard_regset)
1331*38fd1498Szrj 
1332*38fd1498Szrj 			  && (! hard_reg_set_equal_p (temp_set2,
1333*38fd1498Szrj 						      temp_hard_regset)
1334*38fd1498Szrj 			      || cl3 == GENERAL_REGS
1335*38fd1498Szrj 			      /* If the allocatable hard register sets are the
1336*38fd1498Szrj 				 same, prefer GENERAL_REGS or the smallest
1337*38fd1498Szrj 				 class for debugging purposes.  */
1338*38fd1498Szrj 			      || (ira_reg_class_subunion[cl1][cl2] != GENERAL_REGS
1339*38fd1498Szrj 				  && hard_reg_set_subset_p
1340*38fd1498Szrj 				     (reg_class_contents[cl3],
1341*38fd1498Szrj 				      reg_class_contents
1342*38fd1498Szrj 				      [(int) ira_reg_class_subunion[cl1][cl2]])))))
1343*38fd1498Szrj 		    ira_reg_class_subunion[cl1][cl2] = (enum reg_class) cl3;
1344*38fd1498Szrj 		}
1345*38fd1498Szrj 	      if (hard_reg_set_subset_p (union_set, temp_hard_regset))
1346*38fd1498Szrj 		{
1347*38fd1498Szrj 		  /* CL3 allocatable hard register set contains union
1348*38fd1498Szrj 		     of allocatable hard register sets of CL1 and
1349*38fd1498Szrj 		     CL2.  */
1350*38fd1498Szrj 		  COPY_HARD_REG_SET
1351*38fd1498Szrj 		    (temp_set2,
1352*38fd1498Szrj 		     reg_class_contents[(int) ira_reg_class_superunion[cl1][cl2]]);
1353*38fd1498Szrj 		  AND_COMPL_HARD_REG_SET (temp_set2, no_unit_alloc_regs);
1354*38fd1498Szrj 	 	  if (ira_reg_class_superunion[cl1][cl2] == NO_REGS
1355*38fd1498Szrj 		      || (hard_reg_set_subset_p (temp_hard_regset, temp_set2)
1356*38fd1498Szrj 
1357*38fd1498Szrj 			  && (! hard_reg_set_equal_p (temp_set2,
1358*38fd1498Szrj 						      temp_hard_regset)
1359*38fd1498Szrj 			      || cl3 == GENERAL_REGS
1360*38fd1498Szrj 			      /* If the allocatable hard register sets are the
1361*38fd1498Szrj 				 same, prefer GENERAL_REGS or the smallest
1362*38fd1498Szrj 				 class for debugging purposes.  */
1363*38fd1498Szrj 			      || (ira_reg_class_superunion[cl1][cl2] != GENERAL_REGS
1364*38fd1498Szrj 				  && hard_reg_set_subset_p
1365*38fd1498Szrj 				     (reg_class_contents[cl3],
1366*38fd1498Szrj 				      reg_class_contents
1367*38fd1498Szrj 				      [(int) ira_reg_class_superunion[cl1][cl2]])))))
1368*38fd1498Szrj 		    ira_reg_class_superunion[cl1][cl2] = (enum reg_class) cl3;
1369*38fd1498Szrj 		}
1370*38fd1498Szrj 	    }
1371*38fd1498Szrj 	}
1372*38fd1498Szrj     }
1373*38fd1498Szrj }
1374*38fd1498Szrj 
1375*38fd1498Szrj /* Output all uniform and important classes into file F.  */
1376*38fd1498Szrj static void
print_uniform_and_important_classes(FILE * f)1377*38fd1498Szrj print_uniform_and_important_classes (FILE *f)
1378*38fd1498Szrj {
1379*38fd1498Szrj   int i, cl;
1380*38fd1498Szrj 
1381*38fd1498Szrj   fprintf (f, "Uniform classes:\n");
1382*38fd1498Szrj   for (cl = 0; cl < N_REG_CLASSES; cl++)
1383*38fd1498Szrj     if (ira_uniform_class_p[cl])
1384*38fd1498Szrj       fprintf (f, " %s", reg_class_names[cl]);
1385*38fd1498Szrj   fprintf (f, "\nImportant classes:\n");
1386*38fd1498Szrj   for (i = 0; i < ira_important_classes_num; i++)
1387*38fd1498Szrj     fprintf (f, " %s", reg_class_names[ira_important_classes[i]]);
1388*38fd1498Szrj   fprintf (f, "\n");
1389*38fd1498Szrj }
1390*38fd1498Szrj 
1391*38fd1498Szrj /* Output all possible allocno or pressure classes and their
1392*38fd1498Szrj    translation map into file F.  */
1393*38fd1498Szrj static void
print_translated_classes(FILE * f,bool pressure_p)1394*38fd1498Szrj print_translated_classes (FILE *f, bool pressure_p)
1395*38fd1498Szrj {
1396*38fd1498Szrj   int classes_num = (pressure_p
1397*38fd1498Szrj 		     ? ira_pressure_classes_num : ira_allocno_classes_num);
1398*38fd1498Szrj   enum reg_class *classes = (pressure_p
1399*38fd1498Szrj 			     ? ira_pressure_classes : ira_allocno_classes);
1400*38fd1498Szrj   enum reg_class *class_translate = (pressure_p
1401*38fd1498Szrj 				     ? ira_pressure_class_translate
1402*38fd1498Szrj 				     : ira_allocno_class_translate);
1403*38fd1498Szrj   int i;
1404*38fd1498Szrj 
1405*38fd1498Szrj   fprintf (f, "%s classes:\n", pressure_p ? "Pressure" : "Allocno");
1406*38fd1498Szrj   for (i = 0; i < classes_num; i++)
1407*38fd1498Szrj     fprintf (f, " %s", reg_class_names[classes[i]]);
1408*38fd1498Szrj   fprintf (f, "\nClass translation:\n");
1409*38fd1498Szrj   for (i = 0; i < N_REG_CLASSES; i++)
1410*38fd1498Szrj     fprintf (f, " %s -> %s\n", reg_class_names[i],
1411*38fd1498Szrj 	     reg_class_names[class_translate[i]]);
1412*38fd1498Szrj }
1413*38fd1498Szrj 
1414*38fd1498Szrj /* Output all possible allocno and translation classes and the
1415*38fd1498Szrj    translation maps into stderr.  */
1416*38fd1498Szrj void
ira_debug_allocno_classes(void)1417*38fd1498Szrj ira_debug_allocno_classes (void)
1418*38fd1498Szrj {
1419*38fd1498Szrj   print_uniform_and_important_classes (stderr);
1420*38fd1498Szrj   print_translated_classes (stderr, false);
1421*38fd1498Szrj   print_translated_classes (stderr, true);
1422*38fd1498Szrj }
1423*38fd1498Szrj 
1424*38fd1498Szrj /* Set up different arrays concerning class subsets, allocno and
1425*38fd1498Szrj    important classes.  */
1426*38fd1498Szrj static void
find_reg_classes(void)1427*38fd1498Szrj find_reg_classes (void)
1428*38fd1498Szrj {
1429*38fd1498Szrj   setup_allocno_and_important_classes ();
1430*38fd1498Szrj   setup_class_translate ();
1431*38fd1498Szrj   reorder_important_classes ();
1432*38fd1498Szrj   setup_reg_class_relations ();
1433*38fd1498Szrj }
1434*38fd1498Szrj 
1435*38fd1498Szrj 
1436*38fd1498Szrj 
1437*38fd1498Szrj /* Set up the array above.  */
1438*38fd1498Szrj static void
setup_hard_regno_aclass(void)1439*38fd1498Szrj setup_hard_regno_aclass (void)
1440*38fd1498Szrj {
1441*38fd1498Szrj   int i;
1442*38fd1498Szrj 
1443*38fd1498Szrj   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1444*38fd1498Szrj     {
1445*38fd1498Szrj #if 1
1446*38fd1498Szrj       ira_hard_regno_allocno_class[i]
1447*38fd1498Szrj 	= (TEST_HARD_REG_BIT (no_unit_alloc_regs, i)
1448*38fd1498Szrj 	   ? NO_REGS
1449*38fd1498Szrj 	   : ira_allocno_class_translate[REGNO_REG_CLASS (i)]);
1450*38fd1498Szrj #else
1451*38fd1498Szrj       int j;
1452*38fd1498Szrj       enum reg_class cl;
1453*38fd1498Szrj       ira_hard_regno_allocno_class[i] = NO_REGS;
1454*38fd1498Szrj       for (j = 0; j < ira_allocno_classes_num; j++)
1455*38fd1498Szrj  	{
1456*38fd1498Szrj 	  cl = ira_allocno_classes[j];
1457*38fd1498Szrj  	  if (ira_class_hard_reg_index[cl][i] >= 0)
1458*38fd1498Szrj  	    {
1459*38fd1498Szrj 	      ira_hard_regno_allocno_class[i] = cl;
1460*38fd1498Szrj  	      break;
1461*38fd1498Szrj  	    }
1462*38fd1498Szrj  	}
1463*38fd1498Szrj #endif
1464*38fd1498Szrj     }
1465*38fd1498Szrj }
1466*38fd1498Szrj 
1467*38fd1498Szrj 
1468*38fd1498Szrj 
1469*38fd1498Szrj /* Form IRA_REG_CLASS_MAX_NREGS and IRA_REG_CLASS_MIN_NREGS maps.  */
1470*38fd1498Szrj static void
setup_reg_class_nregs(void)1471*38fd1498Szrj setup_reg_class_nregs (void)
1472*38fd1498Szrj {
1473*38fd1498Szrj   int i, cl, cl2, m;
1474*38fd1498Szrj 
1475*38fd1498Szrj   for (m = 0; m < MAX_MACHINE_MODE; m++)
1476*38fd1498Szrj     {
1477*38fd1498Szrj       for (cl = 0; cl < N_REG_CLASSES; cl++)
1478*38fd1498Szrj 	ira_reg_class_max_nregs[cl][m]
1479*38fd1498Szrj 	  = ira_reg_class_min_nregs[cl][m]
1480*38fd1498Szrj 	  = targetm.class_max_nregs ((reg_class_t) cl, (machine_mode) m);
1481*38fd1498Szrj       for (cl = 0; cl < N_REG_CLASSES; cl++)
1482*38fd1498Szrj 	for (i = 0;
1483*38fd1498Szrj 	     (cl2 = alloc_reg_class_subclasses[cl][i]) != LIM_REG_CLASSES;
1484*38fd1498Szrj 	     i++)
1485*38fd1498Szrj 	  if (ira_reg_class_min_nregs[cl2][m]
1486*38fd1498Szrj 	      < ira_reg_class_min_nregs[cl][m])
1487*38fd1498Szrj 	    ira_reg_class_min_nregs[cl][m] = ira_reg_class_min_nregs[cl2][m];
1488*38fd1498Szrj     }
1489*38fd1498Szrj }
1490*38fd1498Szrj 
1491*38fd1498Szrj 
1492*38fd1498Szrj 
1493*38fd1498Szrj /* Set up IRA_PROHIBITED_CLASS_MODE_REGS and IRA_CLASS_SINGLETON.
1494*38fd1498Szrj    This function is called once IRA_CLASS_HARD_REGS has been initialized.  */
1495*38fd1498Szrj static void
setup_prohibited_class_mode_regs(void)1496*38fd1498Szrj setup_prohibited_class_mode_regs (void)
1497*38fd1498Szrj {
1498*38fd1498Szrj   int j, k, hard_regno, cl, last_hard_regno, count;
1499*38fd1498Szrj 
1500*38fd1498Szrj   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
1501*38fd1498Szrj     {
1502*38fd1498Szrj       COPY_HARD_REG_SET (temp_hard_regset, reg_class_contents[cl]);
1503*38fd1498Szrj       AND_COMPL_HARD_REG_SET (temp_hard_regset, no_unit_alloc_regs);
1504*38fd1498Szrj       for (j = 0; j < NUM_MACHINE_MODES; j++)
1505*38fd1498Szrj 	{
1506*38fd1498Szrj 	  count = 0;
1507*38fd1498Szrj 	  last_hard_regno = -1;
1508*38fd1498Szrj 	  CLEAR_HARD_REG_SET (ira_prohibited_class_mode_regs[cl][j]);
1509*38fd1498Szrj 	  for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1510*38fd1498Szrj 	    {
1511*38fd1498Szrj 	      hard_regno = ira_class_hard_regs[cl][k];
1512*38fd1498Szrj 	      if (!targetm.hard_regno_mode_ok (hard_regno, (machine_mode) j))
1513*38fd1498Szrj 		SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1514*38fd1498Szrj 				  hard_regno);
1515*38fd1498Szrj 	      else if (in_hard_reg_set_p (temp_hard_regset,
1516*38fd1498Szrj 					  (machine_mode) j, hard_regno))
1517*38fd1498Szrj 		{
1518*38fd1498Szrj 		  last_hard_regno = hard_regno;
1519*38fd1498Szrj 		  count++;
1520*38fd1498Szrj 		}
1521*38fd1498Szrj 	    }
1522*38fd1498Szrj 	  ira_class_singleton[cl][j] = (count == 1 ? last_hard_regno : -1);
1523*38fd1498Szrj 	}
1524*38fd1498Szrj     }
1525*38fd1498Szrj }
1526*38fd1498Szrj 
1527*38fd1498Szrj /* Clarify IRA_PROHIBITED_CLASS_MODE_REGS by excluding hard registers
1528*38fd1498Szrj    spanning from one register pressure class to another one.  It is
1529*38fd1498Szrj    called after defining the pressure classes.  */
1530*38fd1498Szrj static void
clarify_prohibited_class_mode_regs(void)1531*38fd1498Szrj clarify_prohibited_class_mode_regs (void)
1532*38fd1498Szrj {
1533*38fd1498Szrj   int j, k, hard_regno, cl, pclass, nregs;
1534*38fd1498Szrj 
1535*38fd1498Szrj   for (cl = (int) N_REG_CLASSES - 1; cl >= 0; cl--)
1536*38fd1498Szrj     for (j = 0; j < NUM_MACHINE_MODES; j++)
1537*38fd1498Szrj       {
1538*38fd1498Szrj 	CLEAR_HARD_REG_SET (ira_useful_class_mode_regs[cl][j]);
1539*38fd1498Szrj 	for (k = ira_class_hard_regs_num[cl] - 1; k >= 0; k--)
1540*38fd1498Szrj 	  {
1541*38fd1498Szrj 	    hard_regno = ira_class_hard_regs[cl][k];
1542*38fd1498Szrj 	    if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j], hard_regno))
1543*38fd1498Szrj 	      continue;
1544*38fd1498Szrj 	    nregs = hard_regno_nregs (hard_regno, (machine_mode) j);
1545*38fd1498Szrj 	    if (hard_regno + nregs > FIRST_PSEUDO_REGISTER)
1546*38fd1498Szrj 	      {
1547*38fd1498Szrj 		SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1548*38fd1498Szrj 				  hard_regno);
1549*38fd1498Szrj 		 continue;
1550*38fd1498Szrj 	      }
1551*38fd1498Szrj 	    pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
1552*38fd1498Szrj 	    for (nregs-- ;nregs >= 0; nregs--)
1553*38fd1498Szrj 	      if (((enum reg_class) pclass
1554*38fd1498Szrj 		   != ira_pressure_class_translate[REGNO_REG_CLASS
1555*38fd1498Szrj 						   (hard_regno + nregs)]))
1556*38fd1498Szrj 		{
1557*38fd1498Szrj 		  SET_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1558*38fd1498Szrj 				    hard_regno);
1559*38fd1498Szrj 		  break;
1560*38fd1498Szrj 		}
1561*38fd1498Szrj 	    if (!TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[cl][j],
1562*38fd1498Szrj 				    hard_regno))
1563*38fd1498Szrj 	      add_to_hard_reg_set (&ira_useful_class_mode_regs[cl][j],
1564*38fd1498Szrj 				   (machine_mode) j, hard_regno);
1565*38fd1498Szrj 	  }
1566*38fd1498Szrj       }
1567*38fd1498Szrj }
1568*38fd1498Szrj 
1569*38fd1498Szrj /* Allocate and initialize IRA_REGISTER_MOVE_COST, IRA_MAY_MOVE_IN_COST
1570*38fd1498Szrj    and IRA_MAY_MOVE_OUT_COST for MODE.  */
1571*38fd1498Szrj void
ira_init_register_move_cost(machine_mode mode)1572*38fd1498Szrj ira_init_register_move_cost (machine_mode mode)
1573*38fd1498Szrj {
1574*38fd1498Szrj   static unsigned short last_move_cost[N_REG_CLASSES][N_REG_CLASSES];
1575*38fd1498Szrj   bool all_match = true;
1576*38fd1498Szrj   unsigned int cl1, cl2;
1577*38fd1498Szrj 
1578*38fd1498Szrj   ira_assert (ira_register_move_cost[mode] == NULL
1579*38fd1498Szrj 	      && ira_may_move_in_cost[mode] == NULL
1580*38fd1498Szrj 	      && ira_may_move_out_cost[mode] == NULL);
1581*38fd1498Szrj   /* Note that we might be asked about the move costs of modes that
1582*38fd1498Szrj      cannot be stored in any hard register, for example if an inline
1583*38fd1498Szrj      asm tries to create a register operand with an impossible mode.
1584*38fd1498Szrj      We therefore can't assert have_regs_of_mode[mode] here.  */
1585*38fd1498Szrj   for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1586*38fd1498Szrj     for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1587*38fd1498Szrj       {
1588*38fd1498Szrj 	int cost;
1589*38fd1498Szrj 	if (!contains_reg_of_mode[cl1][mode]
1590*38fd1498Szrj 	    || !contains_reg_of_mode[cl2][mode])
1591*38fd1498Szrj 	  {
1592*38fd1498Szrj 	    if ((ira_reg_class_max_nregs[cl1][mode]
1593*38fd1498Szrj 		 > ira_class_hard_regs_num[cl1])
1594*38fd1498Szrj 		|| (ira_reg_class_max_nregs[cl2][mode]
1595*38fd1498Szrj 		    > ira_class_hard_regs_num[cl2]))
1596*38fd1498Szrj 	      cost = 65535;
1597*38fd1498Szrj 	    else
1598*38fd1498Szrj 	      cost = (ira_memory_move_cost[mode][cl1][0]
1599*38fd1498Szrj 		      + ira_memory_move_cost[mode][cl2][1]) * 2;
1600*38fd1498Szrj 	  }
1601*38fd1498Szrj 	else
1602*38fd1498Szrj 	  {
1603*38fd1498Szrj 	    cost = register_move_cost (mode, (enum reg_class) cl1,
1604*38fd1498Szrj 				       (enum reg_class) cl2);
1605*38fd1498Szrj 	    ira_assert (cost < 65535);
1606*38fd1498Szrj 	  }
1607*38fd1498Szrj 	all_match &= (last_move_cost[cl1][cl2] == cost);
1608*38fd1498Szrj 	last_move_cost[cl1][cl2] = cost;
1609*38fd1498Szrj       }
1610*38fd1498Szrj   if (all_match && last_mode_for_init_move_cost != -1)
1611*38fd1498Szrj     {
1612*38fd1498Szrj       ira_register_move_cost[mode]
1613*38fd1498Szrj 	= ira_register_move_cost[last_mode_for_init_move_cost];
1614*38fd1498Szrj       ira_may_move_in_cost[mode]
1615*38fd1498Szrj 	= ira_may_move_in_cost[last_mode_for_init_move_cost];
1616*38fd1498Szrj       ira_may_move_out_cost[mode]
1617*38fd1498Szrj 	= ira_may_move_out_cost[last_mode_for_init_move_cost];
1618*38fd1498Szrj       return;
1619*38fd1498Szrj     }
1620*38fd1498Szrj   last_mode_for_init_move_cost = mode;
1621*38fd1498Szrj   ira_register_move_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
1622*38fd1498Szrj   ira_may_move_in_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
1623*38fd1498Szrj   ira_may_move_out_cost[mode] = XNEWVEC (move_table, N_REG_CLASSES);
1624*38fd1498Szrj   for (cl1 = 0; cl1 < N_REG_CLASSES; cl1++)
1625*38fd1498Szrj     for (cl2 = 0; cl2 < N_REG_CLASSES; cl2++)
1626*38fd1498Szrj       {
1627*38fd1498Szrj 	int cost;
1628*38fd1498Szrj 	enum reg_class *p1, *p2;
1629*38fd1498Szrj 
1630*38fd1498Szrj 	if (last_move_cost[cl1][cl2] == 65535)
1631*38fd1498Szrj 	  {
1632*38fd1498Szrj 	    ira_register_move_cost[mode][cl1][cl2] = 65535;
1633*38fd1498Szrj 	    ira_may_move_in_cost[mode][cl1][cl2] = 65535;
1634*38fd1498Szrj 	    ira_may_move_out_cost[mode][cl1][cl2] = 65535;
1635*38fd1498Szrj 	  }
1636*38fd1498Szrj 	else
1637*38fd1498Szrj 	  {
1638*38fd1498Szrj 	    cost = last_move_cost[cl1][cl2];
1639*38fd1498Szrj 
1640*38fd1498Szrj 	    for (p2 = &reg_class_subclasses[cl2][0];
1641*38fd1498Szrj 		 *p2 != LIM_REG_CLASSES; p2++)
1642*38fd1498Szrj 	      if (ira_class_hard_regs_num[*p2] > 0
1643*38fd1498Szrj 		  && (ira_reg_class_max_nregs[*p2][mode]
1644*38fd1498Szrj 		      <= ira_class_hard_regs_num[*p2]))
1645*38fd1498Szrj 		cost = MAX (cost, ira_register_move_cost[mode][cl1][*p2]);
1646*38fd1498Szrj 
1647*38fd1498Szrj 	    for (p1 = &reg_class_subclasses[cl1][0];
1648*38fd1498Szrj 		 *p1 != LIM_REG_CLASSES; p1++)
1649*38fd1498Szrj 	      if (ira_class_hard_regs_num[*p1] > 0
1650*38fd1498Szrj 		  && (ira_reg_class_max_nregs[*p1][mode]
1651*38fd1498Szrj 		      <= ira_class_hard_regs_num[*p1]))
1652*38fd1498Szrj 		cost = MAX (cost, ira_register_move_cost[mode][*p1][cl2]);
1653*38fd1498Szrj 
1654*38fd1498Szrj 	    ira_assert (cost <= 65535);
1655*38fd1498Szrj 	    ira_register_move_cost[mode][cl1][cl2] = cost;
1656*38fd1498Szrj 
1657*38fd1498Szrj 	    if (ira_class_subset_p[cl1][cl2])
1658*38fd1498Szrj 	      ira_may_move_in_cost[mode][cl1][cl2] = 0;
1659*38fd1498Szrj 	    else
1660*38fd1498Szrj 	      ira_may_move_in_cost[mode][cl1][cl2] = cost;
1661*38fd1498Szrj 
1662*38fd1498Szrj 	    if (ira_class_subset_p[cl2][cl1])
1663*38fd1498Szrj 	      ira_may_move_out_cost[mode][cl1][cl2] = 0;
1664*38fd1498Szrj 	    else
1665*38fd1498Szrj 	      ira_may_move_out_cost[mode][cl1][cl2] = cost;
1666*38fd1498Szrj 	  }
1667*38fd1498Szrj       }
1668*38fd1498Szrj }
1669*38fd1498Szrj 
1670*38fd1498Szrj 
1671*38fd1498Szrj 
1672*38fd1498Szrj /* This is called once during compiler work.  It sets up
1673*38fd1498Szrj    different arrays whose values don't depend on the compiled
1674*38fd1498Szrj    function.  */
1675*38fd1498Szrj void
ira_init_once(void)1676*38fd1498Szrj ira_init_once (void)
1677*38fd1498Szrj {
1678*38fd1498Szrj   ira_init_costs_once ();
1679*38fd1498Szrj   lra_init_once ();
1680*38fd1498Szrj 
1681*38fd1498Szrj   ira_use_lra_p = targetm.lra_p ();
1682*38fd1498Szrj }
1683*38fd1498Szrj 
1684*38fd1498Szrj /* Free ira_max_register_move_cost, ira_may_move_in_cost and
1685*38fd1498Szrj    ira_may_move_out_cost for each mode.  */
1686*38fd1498Szrj void
free_register_move_costs(void)1687*38fd1498Szrj target_ira_int::free_register_move_costs (void)
1688*38fd1498Szrj {
1689*38fd1498Szrj   int mode, i;
1690*38fd1498Szrj 
1691*38fd1498Szrj   /* Reset move_cost and friends, making sure we only free shared
1692*38fd1498Szrj      table entries once.  */
1693*38fd1498Szrj   for (mode = 0; mode < MAX_MACHINE_MODE; mode++)
1694*38fd1498Szrj     if (x_ira_register_move_cost[mode])
1695*38fd1498Szrj       {
1696*38fd1498Szrj 	for (i = 0;
1697*38fd1498Szrj 	     i < mode && (x_ira_register_move_cost[i]
1698*38fd1498Szrj 			  != x_ira_register_move_cost[mode]);
1699*38fd1498Szrj 	     i++)
1700*38fd1498Szrj 	  ;
1701*38fd1498Szrj 	if (i == mode)
1702*38fd1498Szrj 	  {
1703*38fd1498Szrj 	    free (x_ira_register_move_cost[mode]);
1704*38fd1498Szrj 	    free (x_ira_may_move_in_cost[mode]);
1705*38fd1498Szrj 	    free (x_ira_may_move_out_cost[mode]);
1706*38fd1498Szrj 	  }
1707*38fd1498Szrj       }
1708*38fd1498Szrj   memset (x_ira_register_move_cost, 0, sizeof x_ira_register_move_cost);
1709*38fd1498Szrj   memset (x_ira_may_move_in_cost, 0, sizeof x_ira_may_move_in_cost);
1710*38fd1498Szrj   memset (x_ira_may_move_out_cost, 0, sizeof x_ira_may_move_out_cost);
1711*38fd1498Szrj   last_mode_for_init_move_cost = -1;
1712*38fd1498Szrj }
1713*38fd1498Szrj 
~target_ira_int()1714*38fd1498Szrj target_ira_int::~target_ira_int ()
1715*38fd1498Szrj {
1716*38fd1498Szrj   free_ira_costs ();
1717*38fd1498Szrj   free_register_move_costs ();
1718*38fd1498Szrj }
1719*38fd1498Szrj 
1720*38fd1498Szrj /* This is called every time when register related information is
1721*38fd1498Szrj    changed.  */
1722*38fd1498Szrj void
ira_init(void)1723*38fd1498Szrj ira_init (void)
1724*38fd1498Szrj {
1725*38fd1498Szrj   this_target_ira_int->free_register_move_costs ();
1726*38fd1498Szrj   setup_reg_mode_hard_regset ();
1727*38fd1498Szrj   setup_alloc_regs (flag_omit_frame_pointer != 0);
1728*38fd1498Szrj   setup_class_subset_and_memory_move_costs ();
1729*38fd1498Szrj   setup_reg_class_nregs ();
1730*38fd1498Szrj   setup_prohibited_class_mode_regs ();
1731*38fd1498Szrj   find_reg_classes ();
1732*38fd1498Szrj   clarify_prohibited_class_mode_regs ();
1733*38fd1498Szrj   setup_hard_regno_aclass ();
1734*38fd1498Szrj   ira_init_costs ();
1735*38fd1498Szrj }
1736*38fd1498Szrj 
1737*38fd1498Szrj 
1738*38fd1498Szrj #define ira_prohibited_mode_move_regs_initialized_p \
1739*38fd1498Szrj   (this_target_ira_int->x_ira_prohibited_mode_move_regs_initialized_p)
1740*38fd1498Szrj 
1741*38fd1498Szrj /* Set up IRA_PROHIBITED_MODE_MOVE_REGS.  */
1742*38fd1498Szrj static void
setup_prohibited_mode_move_regs(void)1743*38fd1498Szrj setup_prohibited_mode_move_regs (void)
1744*38fd1498Szrj {
1745*38fd1498Szrj   int i, j;
1746*38fd1498Szrj   rtx test_reg1, test_reg2, move_pat;
1747*38fd1498Szrj   rtx_insn *move_insn;
1748*38fd1498Szrj 
1749*38fd1498Szrj   if (ira_prohibited_mode_move_regs_initialized_p)
1750*38fd1498Szrj     return;
1751*38fd1498Szrj   ira_prohibited_mode_move_regs_initialized_p = true;
1752*38fd1498Szrj   test_reg1 = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
1753*38fd1498Szrj   test_reg2 = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 2);
1754*38fd1498Szrj   move_pat = gen_rtx_SET (test_reg1, test_reg2);
1755*38fd1498Szrj   move_insn = gen_rtx_INSN (VOIDmode, 0, 0, 0, move_pat, 0, -1, 0);
1756*38fd1498Szrj   for (i = 0; i < NUM_MACHINE_MODES; i++)
1757*38fd1498Szrj     {
1758*38fd1498Szrj       SET_HARD_REG_SET (ira_prohibited_mode_move_regs[i]);
1759*38fd1498Szrj       for (j = 0; j < FIRST_PSEUDO_REGISTER; j++)
1760*38fd1498Szrj 	{
1761*38fd1498Szrj 	  if (!targetm.hard_regno_mode_ok (j, (machine_mode) i))
1762*38fd1498Szrj 	    continue;
1763*38fd1498Szrj 	  set_mode_and_regno (test_reg1, (machine_mode) i, j);
1764*38fd1498Szrj 	  set_mode_and_regno (test_reg2, (machine_mode) i, j);
1765*38fd1498Szrj 	  INSN_CODE (move_insn) = -1;
1766*38fd1498Szrj 	  recog_memoized (move_insn);
1767*38fd1498Szrj 	  if (INSN_CODE (move_insn) < 0)
1768*38fd1498Szrj 	    continue;
1769*38fd1498Szrj 	  extract_insn (move_insn);
1770*38fd1498Szrj 	  /* We don't know whether the move will be in code that is optimized
1771*38fd1498Szrj 	     for size or speed, so consider all enabled alternatives.  */
1772*38fd1498Szrj 	  if (! constrain_operands (1, get_enabled_alternatives (move_insn)))
1773*38fd1498Szrj 	    continue;
1774*38fd1498Szrj 	  CLEAR_HARD_REG_BIT (ira_prohibited_mode_move_regs[i], j);
1775*38fd1498Szrj 	}
1776*38fd1498Szrj     }
1777*38fd1498Szrj }
1778*38fd1498Szrj 
1779*38fd1498Szrj 
1780*38fd1498Szrj 
1781*38fd1498Szrj /* Setup possible alternatives in ALTS for INSN.  */
1782*38fd1498Szrj void
ira_setup_alts(rtx_insn * insn,HARD_REG_SET & alts)1783*38fd1498Szrj ira_setup_alts (rtx_insn *insn, HARD_REG_SET &alts)
1784*38fd1498Szrj {
1785*38fd1498Szrj   /* MAP nalt * nop -> start of constraints for given operand and
1786*38fd1498Szrj      alternative.  */
1787*38fd1498Szrj   static vec<const char *> insn_constraints;
1788*38fd1498Szrj   int nop, nalt;
1789*38fd1498Szrj   bool curr_swapped;
1790*38fd1498Szrj   const char *p;
1791*38fd1498Szrj   int commutative = -1;
1792*38fd1498Szrj 
1793*38fd1498Szrj   extract_insn (insn);
1794*38fd1498Szrj   alternative_mask preferred = get_preferred_alternatives (insn);
1795*38fd1498Szrj   CLEAR_HARD_REG_SET (alts);
1796*38fd1498Szrj   insn_constraints.release ();
1797*38fd1498Szrj   insn_constraints.safe_grow_cleared (recog_data.n_operands
1798*38fd1498Szrj 				      * recog_data.n_alternatives + 1);
1799*38fd1498Szrj   /* Check that the hard reg set is enough for holding all
1800*38fd1498Szrj      alternatives.  It is hard to imagine the situation when the
1801*38fd1498Szrj      assertion is wrong.  */
1802*38fd1498Szrj   ira_assert (recog_data.n_alternatives
1803*38fd1498Szrj 	      <= (int) MAX (sizeof (HARD_REG_ELT_TYPE) * CHAR_BIT,
1804*38fd1498Szrj 			    FIRST_PSEUDO_REGISTER));
1805*38fd1498Szrj   for (curr_swapped = false;; curr_swapped = true)
1806*38fd1498Szrj     {
1807*38fd1498Szrj       /* Calculate some data common for all alternatives to speed up the
1808*38fd1498Szrj 	 function.  */
1809*38fd1498Szrj       for (nop = 0; nop < recog_data.n_operands; nop++)
1810*38fd1498Szrj 	{
1811*38fd1498Szrj 	  for (nalt = 0, p = recog_data.constraints[nop];
1812*38fd1498Szrj 	       nalt < recog_data.n_alternatives;
1813*38fd1498Szrj 	       nalt++)
1814*38fd1498Szrj 	    {
1815*38fd1498Szrj 	      insn_constraints[nop * recog_data.n_alternatives + nalt] = p;
1816*38fd1498Szrj 	      while (*p && *p != ',')
1817*38fd1498Szrj 		{
1818*38fd1498Szrj 		  /* We only support one commutative marker, the first
1819*38fd1498Szrj 		     one.  We already set commutative above.  */
1820*38fd1498Szrj 		  if (*p == '%' && commutative < 0)
1821*38fd1498Szrj 		    commutative = nop;
1822*38fd1498Szrj 		  p++;
1823*38fd1498Szrj 		}
1824*38fd1498Szrj 	      if (*p)
1825*38fd1498Szrj 		p++;
1826*38fd1498Szrj 	    }
1827*38fd1498Szrj 	}
1828*38fd1498Szrj       for (nalt = 0; nalt < recog_data.n_alternatives; nalt++)
1829*38fd1498Szrj 	{
1830*38fd1498Szrj 	  if (!TEST_BIT (preferred, nalt)
1831*38fd1498Szrj 	      || TEST_HARD_REG_BIT (alts, nalt))
1832*38fd1498Szrj 	    continue;
1833*38fd1498Szrj 
1834*38fd1498Szrj 	  for (nop = 0; nop < recog_data.n_operands; nop++)
1835*38fd1498Szrj 	    {
1836*38fd1498Szrj 	      int c, len;
1837*38fd1498Szrj 
1838*38fd1498Szrj 	      rtx op = recog_data.operand[nop];
1839*38fd1498Szrj 	      p = insn_constraints[nop * recog_data.n_alternatives + nalt];
1840*38fd1498Szrj 	      if (*p == 0 || *p == ',')
1841*38fd1498Szrj 		continue;
1842*38fd1498Szrj 
1843*38fd1498Szrj 	      do
1844*38fd1498Szrj 		switch (c = *p, len = CONSTRAINT_LEN (c, p), c)
1845*38fd1498Szrj 		  {
1846*38fd1498Szrj 		  case '#':
1847*38fd1498Szrj 		  case ',':
1848*38fd1498Szrj 		    c = '\0';
1849*38fd1498Szrj 		    /* FALLTHRU */
1850*38fd1498Szrj 		  case '\0':
1851*38fd1498Szrj 		    len = 0;
1852*38fd1498Szrj 		    break;
1853*38fd1498Szrj 
1854*38fd1498Szrj 		  case '%':
1855*38fd1498Szrj 		    /* The commutative modifier is handled above.  */
1856*38fd1498Szrj 		    break;
1857*38fd1498Szrj 
1858*38fd1498Szrj 		  case '0':  case '1':  case '2':  case '3':  case '4':
1859*38fd1498Szrj 		  case '5':  case '6':  case '7':  case '8':  case '9':
1860*38fd1498Szrj 		    goto op_success;
1861*38fd1498Szrj 		    break;
1862*38fd1498Szrj 
1863*38fd1498Szrj 		  case 'g':
1864*38fd1498Szrj 		    goto op_success;
1865*38fd1498Szrj 		    break;
1866*38fd1498Szrj 
1867*38fd1498Szrj 		  default:
1868*38fd1498Szrj 		    {
1869*38fd1498Szrj 		      enum constraint_num cn = lookup_constraint (p);
1870*38fd1498Szrj 		      switch (get_constraint_type (cn))
1871*38fd1498Szrj 			{
1872*38fd1498Szrj 			case CT_REGISTER:
1873*38fd1498Szrj 			  if (reg_class_for_constraint (cn) != NO_REGS)
1874*38fd1498Szrj 			    goto op_success;
1875*38fd1498Szrj 			  break;
1876*38fd1498Szrj 
1877*38fd1498Szrj 			case CT_CONST_INT:
1878*38fd1498Szrj 			  if (CONST_INT_P (op)
1879*38fd1498Szrj 			      && (insn_const_int_ok_for_constraint
1880*38fd1498Szrj 				  (INTVAL (op), cn)))
1881*38fd1498Szrj 			    goto op_success;
1882*38fd1498Szrj 			  break;
1883*38fd1498Szrj 
1884*38fd1498Szrj 			case CT_ADDRESS:
1885*38fd1498Szrj 			case CT_MEMORY:
1886*38fd1498Szrj 			case CT_SPECIAL_MEMORY:
1887*38fd1498Szrj 			  goto op_success;
1888*38fd1498Szrj 
1889*38fd1498Szrj 			case CT_FIXED_FORM:
1890*38fd1498Szrj 			  if (constraint_satisfied_p (op, cn))
1891*38fd1498Szrj 			    goto op_success;
1892*38fd1498Szrj 			  break;
1893*38fd1498Szrj 			}
1894*38fd1498Szrj 		      break;
1895*38fd1498Szrj 		    }
1896*38fd1498Szrj 		  }
1897*38fd1498Szrj 	      while (p += len, c);
1898*38fd1498Szrj 	      break;
1899*38fd1498Szrj 	    op_success:
1900*38fd1498Szrj 	      ;
1901*38fd1498Szrj 	    }
1902*38fd1498Szrj 	  if (nop >= recog_data.n_operands)
1903*38fd1498Szrj 	    SET_HARD_REG_BIT (alts, nalt);
1904*38fd1498Szrj 	}
1905*38fd1498Szrj       if (commutative < 0)
1906*38fd1498Szrj 	break;
1907*38fd1498Szrj       /* Swap forth and back to avoid changing recog_data.  */
1908*38fd1498Szrj       std::swap (recog_data.operand[commutative],
1909*38fd1498Szrj 		 recog_data.operand[commutative + 1]);
1910*38fd1498Szrj       if (curr_swapped)
1911*38fd1498Szrj 	break;
1912*38fd1498Szrj     }
1913*38fd1498Szrj }
1914*38fd1498Szrj 
1915*38fd1498Szrj /* Return the number of the output non-early clobber operand which
1916*38fd1498Szrj    should be the same in any case as operand with number OP_NUM (or
1917*38fd1498Szrj    negative value if there is no such operand).  The function takes
1918*38fd1498Szrj    only really possible alternatives into consideration.  */
1919*38fd1498Szrj int
ira_get_dup_out_num(int op_num,HARD_REG_SET & alts)1920*38fd1498Szrj ira_get_dup_out_num (int op_num, HARD_REG_SET &alts)
1921*38fd1498Szrj {
1922*38fd1498Szrj   int curr_alt, c, original, dup;
1923*38fd1498Szrj   bool ignore_p, use_commut_op_p;
1924*38fd1498Szrj   const char *str;
1925*38fd1498Szrj 
1926*38fd1498Szrj   if (op_num < 0 || recog_data.n_alternatives == 0)
1927*38fd1498Szrj     return -1;
1928*38fd1498Szrj   /* We should find duplications only for input operands.  */
1929*38fd1498Szrj   if (recog_data.operand_type[op_num] != OP_IN)
1930*38fd1498Szrj     return -1;
1931*38fd1498Szrj   str = recog_data.constraints[op_num];
1932*38fd1498Szrj   use_commut_op_p = false;
1933*38fd1498Szrj   for (;;)
1934*38fd1498Szrj     {
1935*38fd1498Szrj       rtx op = recog_data.operand[op_num];
1936*38fd1498Szrj 
1937*38fd1498Szrj       for (curr_alt = 0, ignore_p = !TEST_HARD_REG_BIT (alts, curr_alt),
1938*38fd1498Szrj 	   original = -1;;)
1939*38fd1498Szrj 	{
1940*38fd1498Szrj 	  c = *str;
1941*38fd1498Szrj 	  if (c == '\0')
1942*38fd1498Szrj 	    break;
1943*38fd1498Szrj 	  if (c == '#')
1944*38fd1498Szrj 	    ignore_p = true;
1945*38fd1498Szrj 	  else if (c == ',')
1946*38fd1498Szrj 	    {
1947*38fd1498Szrj 	      curr_alt++;
1948*38fd1498Szrj 	      ignore_p = !TEST_HARD_REG_BIT (alts, curr_alt);
1949*38fd1498Szrj 	    }
1950*38fd1498Szrj 	  else if (! ignore_p)
1951*38fd1498Szrj 	    switch (c)
1952*38fd1498Szrj 	      {
1953*38fd1498Szrj 	      case 'g':
1954*38fd1498Szrj 		goto fail;
1955*38fd1498Szrj 	      default:
1956*38fd1498Szrj 		{
1957*38fd1498Szrj 		  enum constraint_num cn = lookup_constraint (str);
1958*38fd1498Szrj 		  enum reg_class cl = reg_class_for_constraint (cn);
1959*38fd1498Szrj 		  if (cl != NO_REGS
1960*38fd1498Szrj 		      && !targetm.class_likely_spilled_p (cl))
1961*38fd1498Szrj 		    goto fail;
1962*38fd1498Szrj 		  if (constraint_satisfied_p (op, cn))
1963*38fd1498Szrj 		    goto fail;
1964*38fd1498Szrj 		  break;
1965*38fd1498Szrj 		}
1966*38fd1498Szrj 
1967*38fd1498Szrj 	      case '0': case '1': case '2': case '3': case '4':
1968*38fd1498Szrj 	      case '5': case '6': case '7': case '8': case '9':
1969*38fd1498Szrj 		if (original != -1 && original != c)
1970*38fd1498Szrj 		  goto fail;
1971*38fd1498Szrj 		original = c;
1972*38fd1498Szrj 		break;
1973*38fd1498Szrj 	      }
1974*38fd1498Szrj 	  str += CONSTRAINT_LEN (c, str);
1975*38fd1498Szrj 	}
1976*38fd1498Szrj       if (original == -1)
1977*38fd1498Szrj 	goto fail;
1978*38fd1498Szrj       dup = -1;
1979*38fd1498Szrj       for (ignore_p = false, str = recog_data.constraints[original - '0'];
1980*38fd1498Szrj 	   *str != 0;
1981*38fd1498Szrj 	   str++)
1982*38fd1498Szrj 	if (ignore_p)
1983*38fd1498Szrj 	  {
1984*38fd1498Szrj 	    if (*str == ',')
1985*38fd1498Szrj 	      ignore_p = false;
1986*38fd1498Szrj 	  }
1987*38fd1498Szrj 	else if (*str == '#')
1988*38fd1498Szrj 	  ignore_p = true;
1989*38fd1498Szrj 	else if (! ignore_p)
1990*38fd1498Szrj 	  {
1991*38fd1498Szrj 	    if (*str == '=')
1992*38fd1498Szrj 	      dup = original - '0';
1993*38fd1498Szrj 	    /* It is better ignore an alternative with early clobber.  */
1994*38fd1498Szrj 	    else if (*str == '&')
1995*38fd1498Szrj 	      goto fail;
1996*38fd1498Szrj 	  }
1997*38fd1498Szrj       if (dup >= 0)
1998*38fd1498Szrj 	return dup;
1999*38fd1498Szrj     fail:
2000*38fd1498Szrj       if (use_commut_op_p)
2001*38fd1498Szrj 	break;
2002*38fd1498Szrj       use_commut_op_p = true;
2003*38fd1498Szrj       if (recog_data.constraints[op_num][0] == '%')
2004*38fd1498Szrj 	str = recog_data.constraints[op_num + 1];
2005*38fd1498Szrj       else if (op_num > 0 && recog_data.constraints[op_num - 1][0] == '%')
2006*38fd1498Szrj 	str = recog_data.constraints[op_num - 1];
2007*38fd1498Szrj       else
2008*38fd1498Szrj 	break;
2009*38fd1498Szrj     }
2010*38fd1498Szrj   return -1;
2011*38fd1498Szrj }
2012*38fd1498Szrj 
2013*38fd1498Szrj 
2014*38fd1498Szrj 
2015*38fd1498Szrj /* Search forward to see if the source register of a copy insn dies
2016*38fd1498Szrj    before either it or the destination register is modified, but don't
2017*38fd1498Szrj    scan past the end of the basic block.  If so, we can replace the
2018*38fd1498Szrj    source with the destination and let the source die in the copy
2019*38fd1498Szrj    insn.
2020*38fd1498Szrj 
2021*38fd1498Szrj    This will reduce the number of registers live in that range and may
2022*38fd1498Szrj    enable the destination and the source coalescing, thus often saving
2023*38fd1498Szrj    one register in addition to a register-register copy.  */
2024*38fd1498Szrj 
2025*38fd1498Szrj static void
decrease_live_ranges_number(void)2026*38fd1498Szrj decrease_live_ranges_number (void)
2027*38fd1498Szrj {
2028*38fd1498Szrj   basic_block bb;
2029*38fd1498Szrj   rtx_insn *insn;
2030*38fd1498Szrj   rtx set, src, dest, dest_death, note;
2031*38fd1498Szrj   rtx_insn *p, *q;
2032*38fd1498Szrj   int sregno, dregno;
2033*38fd1498Szrj 
2034*38fd1498Szrj   if (! flag_expensive_optimizations)
2035*38fd1498Szrj     return;
2036*38fd1498Szrj 
2037*38fd1498Szrj   if (ira_dump_file)
2038*38fd1498Szrj     fprintf (ira_dump_file, "Starting decreasing number of live ranges...\n");
2039*38fd1498Szrj 
2040*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
2041*38fd1498Szrj     FOR_BB_INSNS (bb, insn)
2042*38fd1498Szrj       {
2043*38fd1498Szrj 	set = single_set (insn);
2044*38fd1498Szrj 	if (! set)
2045*38fd1498Szrj 	  continue;
2046*38fd1498Szrj 	src = SET_SRC (set);
2047*38fd1498Szrj 	dest = SET_DEST (set);
2048*38fd1498Szrj 	if (! REG_P (src) || ! REG_P (dest)
2049*38fd1498Szrj 	    || find_reg_note (insn, REG_DEAD, src))
2050*38fd1498Szrj 	  continue;
2051*38fd1498Szrj 	sregno = REGNO (src);
2052*38fd1498Szrj 	dregno = REGNO (dest);
2053*38fd1498Szrj 
2054*38fd1498Szrj 	/* We don't want to mess with hard regs if register classes
2055*38fd1498Szrj 	   are small.  */
2056*38fd1498Szrj 	if (sregno == dregno
2057*38fd1498Szrj 	    || (targetm.small_register_classes_for_mode_p (GET_MODE (src))
2058*38fd1498Szrj 		&& (sregno < FIRST_PSEUDO_REGISTER
2059*38fd1498Szrj 		    || dregno < FIRST_PSEUDO_REGISTER))
2060*38fd1498Szrj 	    /* We don't see all updates to SP if they are in an
2061*38fd1498Szrj 	       auto-inc memory reference, so we must disallow this
2062*38fd1498Szrj 	       optimization on them.  */
2063*38fd1498Szrj 	    || sregno == STACK_POINTER_REGNUM
2064*38fd1498Szrj 	    || dregno == STACK_POINTER_REGNUM)
2065*38fd1498Szrj 	  continue;
2066*38fd1498Szrj 
2067*38fd1498Szrj 	dest_death = NULL_RTX;
2068*38fd1498Szrj 
2069*38fd1498Szrj 	for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
2070*38fd1498Szrj 	  {
2071*38fd1498Szrj 	    if (! INSN_P (p))
2072*38fd1498Szrj 	      continue;
2073*38fd1498Szrj 	    if (BLOCK_FOR_INSN (p) != bb)
2074*38fd1498Szrj 	      break;
2075*38fd1498Szrj 
2076*38fd1498Szrj 	    if (reg_set_p (src, p) || reg_set_p (dest, p)
2077*38fd1498Szrj 		/* If SRC is an asm-declared register, it must not be
2078*38fd1498Szrj 		   replaced in any asm.  Unfortunately, the REG_EXPR
2079*38fd1498Szrj 		   tree for the asm variable may be absent in the SRC
2080*38fd1498Szrj 		   rtx, so we can't check the actual register
2081*38fd1498Szrj 		   declaration easily (the asm operand will have it,
2082*38fd1498Szrj 		   though).  To avoid complicating the test for a rare
2083*38fd1498Szrj 		   case, we just don't perform register replacement
2084*38fd1498Szrj 		   for a hard reg mentioned in an asm.  */
2085*38fd1498Szrj 		|| (sregno < FIRST_PSEUDO_REGISTER
2086*38fd1498Szrj 		    && asm_noperands (PATTERN (p)) >= 0
2087*38fd1498Szrj 		    && reg_overlap_mentioned_p (src, PATTERN (p)))
2088*38fd1498Szrj 		/* Don't change hard registers used by a call.  */
2089*38fd1498Szrj 		|| (CALL_P (p) && sregno < FIRST_PSEUDO_REGISTER
2090*38fd1498Szrj 		    && find_reg_fusage (p, USE, src))
2091*38fd1498Szrj 		/* Don't change a USE of a register.  */
2092*38fd1498Szrj 		|| (GET_CODE (PATTERN (p)) == USE
2093*38fd1498Szrj 		    && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
2094*38fd1498Szrj 	      break;
2095*38fd1498Szrj 
2096*38fd1498Szrj 	    /* See if all of SRC dies in P.  This test is slightly
2097*38fd1498Szrj 	       more conservative than it needs to be.  */
2098*38fd1498Szrj 	    if ((note = find_regno_note (p, REG_DEAD, sregno))
2099*38fd1498Szrj 		&& GET_MODE (XEXP (note, 0)) == GET_MODE (src))
2100*38fd1498Szrj 	      {
2101*38fd1498Szrj 		int failed = 0;
2102*38fd1498Szrj 
2103*38fd1498Szrj 		/* We can do the optimization.  Scan forward from INSN
2104*38fd1498Szrj 		   again, replacing regs as we go.  Set FAILED if a
2105*38fd1498Szrj 		   replacement can't be done.  In that case, we can't
2106*38fd1498Szrj 		   move the death note for SRC.  This should be
2107*38fd1498Szrj 		   rare.  */
2108*38fd1498Szrj 
2109*38fd1498Szrj 		/* Set to stop at next insn.  */
2110*38fd1498Szrj 		for (q = next_real_insn (insn);
2111*38fd1498Szrj 		     q != next_real_insn (p);
2112*38fd1498Szrj 		     q = next_real_insn (q))
2113*38fd1498Szrj 		  {
2114*38fd1498Szrj 		    if (reg_overlap_mentioned_p (src, PATTERN (q)))
2115*38fd1498Szrj 		      {
2116*38fd1498Szrj 			/* If SRC is a hard register, we might miss
2117*38fd1498Szrj 			   some overlapping registers with
2118*38fd1498Szrj 			   validate_replace_rtx, so we would have to
2119*38fd1498Szrj 			   undo it.  We can't if DEST is present in
2120*38fd1498Szrj 			   the insn, so fail in that combination of
2121*38fd1498Szrj 			   cases.  */
2122*38fd1498Szrj 			if (sregno < FIRST_PSEUDO_REGISTER
2123*38fd1498Szrj 			    && reg_mentioned_p (dest, PATTERN (q)))
2124*38fd1498Szrj 			  failed = 1;
2125*38fd1498Szrj 
2126*38fd1498Szrj 			/* Attempt to replace all uses.  */
2127*38fd1498Szrj 			else if (!validate_replace_rtx (src, dest, q))
2128*38fd1498Szrj 			  failed = 1;
2129*38fd1498Szrj 
2130*38fd1498Szrj 			/* If this succeeded, but some part of the
2131*38fd1498Szrj 			   register is still present, undo the
2132*38fd1498Szrj 			   replacement.  */
2133*38fd1498Szrj 			else if (sregno < FIRST_PSEUDO_REGISTER
2134*38fd1498Szrj 				 && reg_overlap_mentioned_p (src, PATTERN (q)))
2135*38fd1498Szrj 			  {
2136*38fd1498Szrj 			    validate_replace_rtx (dest, src, q);
2137*38fd1498Szrj 			    failed = 1;
2138*38fd1498Szrj 			  }
2139*38fd1498Szrj 		      }
2140*38fd1498Szrj 
2141*38fd1498Szrj 		    /* If DEST dies here, remove the death note and
2142*38fd1498Szrj 		       save it for later.  Make sure ALL of DEST dies
2143*38fd1498Szrj 		       here; again, this is overly conservative.  */
2144*38fd1498Szrj 		    if (! dest_death
2145*38fd1498Szrj 			&& (dest_death = find_regno_note (q, REG_DEAD, dregno)))
2146*38fd1498Szrj 		      {
2147*38fd1498Szrj 			if (GET_MODE (XEXP (dest_death, 0)) == GET_MODE (dest))
2148*38fd1498Szrj 			  remove_note (q, dest_death);
2149*38fd1498Szrj 			else
2150*38fd1498Szrj 			  {
2151*38fd1498Szrj 			    failed = 1;
2152*38fd1498Szrj 			    dest_death = 0;
2153*38fd1498Szrj 			  }
2154*38fd1498Szrj 		      }
2155*38fd1498Szrj 		  }
2156*38fd1498Szrj 
2157*38fd1498Szrj 		if (! failed)
2158*38fd1498Szrj 		  {
2159*38fd1498Szrj 		    /* Move death note of SRC from P to INSN.  */
2160*38fd1498Szrj 		    remove_note (p, note);
2161*38fd1498Szrj 		    XEXP (note, 1) = REG_NOTES (insn);
2162*38fd1498Szrj 		    REG_NOTES (insn) = note;
2163*38fd1498Szrj 		  }
2164*38fd1498Szrj 
2165*38fd1498Szrj 		/* DEST is also dead if INSN has a REG_UNUSED note for
2166*38fd1498Szrj 		   DEST.  */
2167*38fd1498Szrj 		if (! dest_death
2168*38fd1498Szrj 		    && (dest_death
2169*38fd1498Szrj 			= find_regno_note (insn, REG_UNUSED, dregno)))
2170*38fd1498Szrj 		  {
2171*38fd1498Szrj 		    PUT_REG_NOTE_KIND (dest_death, REG_DEAD);
2172*38fd1498Szrj 		    remove_note (insn, dest_death);
2173*38fd1498Szrj 		  }
2174*38fd1498Szrj 
2175*38fd1498Szrj 		/* Put death note of DEST on P if we saw it die.  */
2176*38fd1498Szrj 		if (dest_death)
2177*38fd1498Szrj 		  {
2178*38fd1498Szrj 		    XEXP (dest_death, 1) = REG_NOTES (p);
2179*38fd1498Szrj 		    REG_NOTES (p) = dest_death;
2180*38fd1498Szrj 		  }
2181*38fd1498Szrj 		break;
2182*38fd1498Szrj 	      }
2183*38fd1498Szrj 
2184*38fd1498Szrj 	    /* If SRC is a hard register which is set or killed in
2185*38fd1498Szrj 	       some other way, we can't do this optimization.  */
2186*38fd1498Szrj 	    else if (sregno < FIRST_PSEUDO_REGISTER && dead_or_set_p (p, src))
2187*38fd1498Szrj 	      break;
2188*38fd1498Szrj 	  }
2189*38fd1498Szrj       }
2190*38fd1498Szrj }
2191*38fd1498Szrj 
2192*38fd1498Szrj 
2193*38fd1498Szrj 
2194*38fd1498Szrj /* Return nonzero if REGNO is a particularly bad choice for reloading X.  */
2195*38fd1498Szrj static bool
ira_bad_reload_regno_1(int regno,rtx x)2196*38fd1498Szrj ira_bad_reload_regno_1 (int regno, rtx x)
2197*38fd1498Szrj {
2198*38fd1498Szrj   int x_regno, n, i;
2199*38fd1498Szrj   ira_allocno_t a;
2200*38fd1498Szrj   enum reg_class pref;
2201*38fd1498Szrj 
2202*38fd1498Szrj   /* We only deal with pseudo regs.  */
2203*38fd1498Szrj   if (! x || GET_CODE (x) != REG)
2204*38fd1498Szrj     return false;
2205*38fd1498Szrj 
2206*38fd1498Szrj   x_regno = REGNO (x);
2207*38fd1498Szrj   if (x_regno < FIRST_PSEUDO_REGISTER)
2208*38fd1498Szrj     return false;
2209*38fd1498Szrj 
2210*38fd1498Szrj   /* If the pseudo prefers REGNO explicitly, then do not consider
2211*38fd1498Szrj      REGNO a bad spill choice.  */
2212*38fd1498Szrj   pref = reg_preferred_class (x_regno);
2213*38fd1498Szrj   if (reg_class_size[pref] == 1)
2214*38fd1498Szrj     return !TEST_HARD_REG_BIT (reg_class_contents[pref], regno);
2215*38fd1498Szrj 
2216*38fd1498Szrj   /* If the pseudo conflicts with REGNO, then we consider REGNO a
2217*38fd1498Szrj      poor choice for a reload regno.  */
2218*38fd1498Szrj   a = ira_regno_allocno_map[x_regno];
2219*38fd1498Szrj   n = ALLOCNO_NUM_OBJECTS (a);
2220*38fd1498Szrj   for (i = 0; i < n; i++)
2221*38fd1498Szrj     {
2222*38fd1498Szrj       ira_object_t obj = ALLOCNO_OBJECT (a, i);
2223*38fd1498Szrj       if (TEST_HARD_REG_BIT (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), regno))
2224*38fd1498Szrj 	return true;
2225*38fd1498Szrj     }
2226*38fd1498Szrj   return false;
2227*38fd1498Szrj }
2228*38fd1498Szrj 
2229*38fd1498Szrj /* Return nonzero if REGNO is a particularly bad choice for reloading
2230*38fd1498Szrj    IN or OUT.  */
2231*38fd1498Szrj bool
ira_bad_reload_regno(int regno,rtx in,rtx out)2232*38fd1498Szrj ira_bad_reload_regno (int regno, rtx in, rtx out)
2233*38fd1498Szrj {
2234*38fd1498Szrj   return (ira_bad_reload_regno_1 (regno, in)
2235*38fd1498Szrj 	  || ira_bad_reload_regno_1 (regno, out));
2236*38fd1498Szrj }
2237*38fd1498Szrj 
2238*38fd1498Szrj /* Add register clobbers from asm statements.  */
2239*38fd1498Szrj static void
compute_regs_asm_clobbered(void)2240*38fd1498Szrj compute_regs_asm_clobbered (void)
2241*38fd1498Szrj {
2242*38fd1498Szrj   basic_block bb;
2243*38fd1498Szrj 
2244*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
2245*38fd1498Szrj     {
2246*38fd1498Szrj       rtx_insn *insn;
2247*38fd1498Szrj       FOR_BB_INSNS_REVERSE (bb, insn)
2248*38fd1498Szrj 	{
2249*38fd1498Szrj 	  df_ref def;
2250*38fd1498Szrj 
2251*38fd1498Szrj 	  if (NONDEBUG_INSN_P (insn) && asm_noperands (PATTERN (insn)) >= 0)
2252*38fd1498Szrj 	    FOR_EACH_INSN_DEF (def, insn)
2253*38fd1498Szrj 	      {
2254*38fd1498Szrj 		unsigned int dregno = DF_REF_REGNO (def);
2255*38fd1498Szrj 		if (HARD_REGISTER_NUM_P (dregno))
2256*38fd1498Szrj 		  add_to_hard_reg_set (&crtl->asm_clobbers,
2257*38fd1498Szrj 				       GET_MODE (DF_REF_REAL_REG (def)),
2258*38fd1498Szrj 				       dregno);
2259*38fd1498Szrj 	      }
2260*38fd1498Szrj 	}
2261*38fd1498Szrj     }
2262*38fd1498Szrj }
2263*38fd1498Szrj 
2264*38fd1498Szrj 
2265*38fd1498Szrj /* Set up ELIMINABLE_REGSET, IRA_NO_ALLOC_REGS, and
2266*38fd1498Szrj    REGS_EVER_LIVE.  */
2267*38fd1498Szrj void
ira_setup_eliminable_regset(void)2268*38fd1498Szrj ira_setup_eliminable_regset (void)
2269*38fd1498Szrj {
2270*38fd1498Szrj   int i;
2271*38fd1498Szrj   static const struct {const int from, to; } eliminables[] = ELIMINABLE_REGS;
2272*38fd1498Szrj 
2273*38fd1498Szrj   /* Setup is_leaf as frame_pointer_required may use it.  This function
2274*38fd1498Szrj      is called by sched_init before ira if scheduling is enabled.  */
2275*38fd1498Szrj   crtl->is_leaf = leaf_function_p ();
2276*38fd1498Szrj 
2277*38fd1498Szrj   /* FIXME: If EXIT_IGNORE_STACK is set, we will not save and restore
2278*38fd1498Szrj      sp for alloca.  So we can't eliminate the frame pointer in that
2279*38fd1498Szrj      case.  At some point, we should improve this by emitting the
2280*38fd1498Szrj      sp-adjusting insns for this case.  */
2281*38fd1498Szrj   frame_pointer_needed
2282*38fd1498Szrj     = (! flag_omit_frame_pointer
2283*38fd1498Szrj        || (cfun->calls_alloca && EXIT_IGNORE_STACK)
2284*38fd1498Szrj        /* We need the frame pointer to catch stack overflow exceptions if
2285*38fd1498Szrj 	  the stack pointer is moving (as for the alloca case just above).  */
2286*38fd1498Szrj        || (STACK_CHECK_MOVING_SP
2287*38fd1498Szrj 	   && flag_stack_check
2288*38fd1498Szrj 	   && flag_exceptions
2289*38fd1498Szrj 	   && cfun->can_throw_non_call_exceptions)
2290*38fd1498Szrj        || crtl->accesses_prior_frames
2291*38fd1498Szrj        || (SUPPORTS_STACK_ALIGNMENT && crtl->stack_realign_needed)
2292*38fd1498Szrj        || targetm.frame_pointer_required ());
2293*38fd1498Szrj 
2294*38fd1498Szrj     /* The chance that FRAME_POINTER_NEEDED is changed from inspecting
2295*38fd1498Szrj        RTL is very small.  So if we use frame pointer for RA and RTL
2296*38fd1498Szrj        actually prevents this, we will spill pseudos assigned to the
2297*38fd1498Szrj        frame pointer in LRA.  */
2298*38fd1498Szrj 
2299*38fd1498Szrj   if (frame_pointer_needed)
2300*38fd1498Szrj     df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
2301*38fd1498Szrj 
2302*38fd1498Szrj   COPY_HARD_REG_SET (ira_no_alloc_regs, no_unit_alloc_regs);
2303*38fd1498Szrj   CLEAR_HARD_REG_SET (eliminable_regset);
2304*38fd1498Szrj 
2305*38fd1498Szrj   compute_regs_asm_clobbered ();
2306*38fd1498Szrj 
2307*38fd1498Szrj   /* Build the regset of all eliminable registers and show we can't
2308*38fd1498Szrj      use those that we already know won't be eliminated.  */
2309*38fd1498Szrj   for (i = 0; i < (int) ARRAY_SIZE (eliminables); i++)
2310*38fd1498Szrj     {
2311*38fd1498Szrj       bool cannot_elim
2312*38fd1498Szrj 	= (! targetm.can_eliminate (eliminables[i].from, eliminables[i].to)
2313*38fd1498Szrj 	   || (eliminables[i].to == STACK_POINTER_REGNUM && frame_pointer_needed));
2314*38fd1498Szrj 
2315*38fd1498Szrj       if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, eliminables[i].from))
2316*38fd1498Szrj 	{
2317*38fd1498Szrj 	    SET_HARD_REG_BIT (eliminable_regset, eliminables[i].from);
2318*38fd1498Szrj 
2319*38fd1498Szrj 	    if (cannot_elim)
2320*38fd1498Szrj 	      SET_HARD_REG_BIT (ira_no_alloc_regs, eliminables[i].from);
2321*38fd1498Szrj 	}
2322*38fd1498Szrj       else if (cannot_elim)
2323*38fd1498Szrj 	error ("%s cannot be used in asm here",
2324*38fd1498Szrj 	       reg_names[eliminables[i].from]);
2325*38fd1498Szrj       else
2326*38fd1498Szrj 	df_set_regs_ever_live (eliminables[i].from, true);
2327*38fd1498Szrj     }
2328*38fd1498Szrj   if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
2329*38fd1498Szrj     {
2330*38fd1498Szrj       if (!TEST_HARD_REG_BIT (crtl->asm_clobbers, HARD_FRAME_POINTER_REGNUM))
2331*38fd1498Szrj 	{
2332*38fd1498Szrj 	  SET_HARD_REG_BIT (eliminable_regset, HARD_FRAME_POINTER_REGNUM);
2333*38fd1498Szrj 	  if (frame_pointer_needed)
2334*38fd1498Szrj 	    SET_HARD_REG_BIT (ira_no_alloc_regs, HARD_FRAME_POINTER_REGNUM);
2335*38fd1498Szrj 	}
2336*38fd1498Szrj       else if (frame_pointer_needed)
2337*38fd1498Szrj 	error ("%s cannot be used in asm here",
2338*38fd1498Szrj 	       reg_names[HARD_FRAME_POINTER_REGNUM]);
2339*38fd1498Szrj       else
2340*38fd1498Szrj 	df_set_regs_ever_live (HARD_FRAME_POINTER_REGNUM, true);
2341*38fd1498Szrj     }
2342*38fd1498Szrj }
2343*38fd1498Szrj 
2344*38fd1498Szrj 
2345*38fd1498Szrj 
2346*38fd1498Szrj /* Vector of substitutions of register numbers,
2347*38fd1498Szrj    used to map pseudo regs into hardware regs.
2348*38fd1498Szrj    This is set up as a result of register allocation.
2349*38fd1498Szrj    Element N is the hard reg assigned to pseudo reg N,
2350*38fd1498Szrj    or is -1 if no hard reg was assigned.
2351*38fd1498Szrj    If N is a hard reg number, element N is N.  */
2352*38fd1498Szrj short *reg_renumber;
2353*38fd1498Szrj 
2354*38fd1498Szrj /* Set up REG_RENUMBER and CALLER_SAVE_NEEDED (used by reload) from
2355*38fd1498Szrj    the allocation found by IRA.  */
2356*38fd1498Szrj static void
setup_reg_renumber(void)2357*38fd1498Szrj setup_reg_renumber (void)
2358*38fd1498Szrj {
2359*38fd1498Szrj   int regno, hard_regno;
2360*38fd1498Szrj   ira_allocno_t a;
2361*38fd1498Szrj   ira_allocno_iterator ai;
2362*38fd1498Szrj 
2363*38fd1498Szrj   caller_save_needed = 0;
2364*38fd1498Szrj   FOR_EACH_ALLOCNO (a, ai)
2365*38fd1498Szrj     {
2366*38fd1498Szrj       if (ira_use_lra_p && ALLOCNO_CAP_MEMBER (a) != NULL)
2367*38fd1498Szrj 	continue;
2368*38fd1498Szrj       /* There are no caps at this point.  */
2369*38fd1498Szrj       ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2370*38fd1498Szrj       if (! ALLOCNO_ASSIGNED_P (a))
2371*38fd1498Szrj 	/* It can happen if A is not referenced but partially anticipated
2372*38fd1498Szrj 	   somewhere in a region.  */
2373*38fd1498Szrj 	ALLOCNO_ASSIGNED_P (a) = true;
2374*38fd1498Szrj       ira_free_allocno_updated_costs (a);
2375*38fd1498Szrj       hard_regno = ALLOCNO_HARD_REGNO (a);
2376*38fd1498Szrj       regno = ALLOCNO_REGNO (a);
2377*38fd1498Szrj       reg_renumber[regno] = (hard_regno < 0 ? -1 : hard_regno);
2378*38fd1498Szrj       if (hard_regno >= 0)
2379*38fd1498Szrj 	{
2380*38fd1498Szrj 	  int i, nwords;
2381*38fd1498Szrj 	  enum reg_class pclass;
2382*38fd1498Szrj 	  ira_object_t obj;
2383*38fd1498Szrj 
2384*38fd1498Szrj 	  pclass = ira_pressure_class_translate[REGNO_REG_CLASS (hard_regno)];
2385*38fd1498Szrj 	  nwords = ALLOCNO_NUM_OBJECTS (a);
2386*38fd1498Szrj 	  for (i = 0; i < nwords; i++)
2387*38fd1498Szrj 	    {
2388*38fd1498Szrj 	      obj = ALLOCNO_OBJECT (a, i);
2389*38fd1498Szrj 	      IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2390*38fd1498Szrj 				      reg_class_contents[pclass]);
2391*38fd1498Szrj 	    }
2392*38fd1498Szrj 	  if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
2393*38fd1498Szrj 	      && ira_hard_reg_set_intersection_p (hard_regno, ALLOCNO_MODE (a),
2394*38fd1498Szrj 						  call_used_reg_set))
2395*38fd1498Szrj 	    {
2396*38fd1498Szrj 	      ira_assert (!optimize || flag_caller_saves
2397*38fd1498Szrj 			  || (ALLOCNO_CALLS_CROSSED_NUM (a)
2398*38fd1498Szrj 			      == ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a))
2399*38fd1498Szrj 			  || regno >= ira_reg_equiv_len
2400*38fd1498Szrj 			  || ira_equiv_no_lvalue_p (regno));
2401*38fd1498Szrj 	      caller_save_needed = 1;
2402*38fd1498Szrj 	    }
2403*38fd1498Szrj 	}
2404*38fd1498Szrj     }
2405*38fd1498Szrj }
2406*38fd1498Szrj 
2407*38fd1498Szrj /* Set up allocno assignment flags for further allocation
2408*38fd1498Szrj    improvements.  */
2409*38fd1498Szrj static void
setup_allocno_assignment_flags(void)2410*38fd1498Szrj setup_allocno_assignment_flags (void)
2411*38fd1498Szrj {
2412*38fd1498Szrj   int hard_regno;
2413*38fd1498Szrj   ira_allocno_t a;
2414*38fd1498Szrj   ira_allocno_iterator ai;
2415*38fd1498Szrj 
2416*38fd1498Szrj   FOR_EACH_ALLOCNO (a, ai)
2417*38fd1498Szrj     {
2418*38fd1498Szrj       if (! ALLOCNO_ASSIGNED_P (a))
2419*38fd1498Szrj 	/* It can happen if A is not referenced but partially anticipated
2420*38fd1498Szrj 	   somewhere in a region.  */
2421*38fd1498Szrj 	ira_free_allocno_updated_costs (a);
2422*38fd1498Szrj       hard_regno = ALLOCNO_HARD_REGNO (a);
2423*38fd1498Szrj       /* Don't assign hard registers to allocnos which are destination
2424*38fd1498Szrj 	 of removed store at the end of loop.  It has no sense to keep
2425*38fd1498Szrj 	 the same value in different hard registers.  It is also
2426*38fd1498Szrj 	 impossible to assign hard registers correctly to such
2427*38fd1498Szrj 	 allocnos because the cost info and info about intersected
2428*38fd1498Szrj 	 calls are incorrect for them.  */
2429*38fd1498Szrj       ALLOCNO_ASSIGNED_P (a) = (hard_regno >= 0
2430*38fd1498Szrj 				|| ALLOCNO_EMIT_DATA (a)->mem_optimized_dest_p
2431*38fd1498Szrj 				|| (ALLOCNO_MEMORY_COST (a)
2432*38fd1498Szrj 				    - ALLOCNO_CLASS_COST (a)) < 0);
2433*38fd1498Szrj       ira_assert
2434*38fd1498Szrj 	(hard_regno < 0
2435*38fd1498Szrj 	 || ira_hard_reg_in_set_p (hard_regno, ALLOCNO_MODE (a),
2436*38fd1498Szrj 				   reg_class_contents[ALLOCNO_CLASS (a)]));
2437*38fd1498Szrj     }
2438*38fd1498Szrj }
2439*38fd1498Szrj 
2440*38fd1498Szrj /* Evaluate overall allocation cost and the costs for using hard
2441*38fd1498Szrj    registers and memory for allocnos.  */
2442*38fd1498Szrj static void
calculate_allocation_cost(void)2443*38fd1498Szrj calculate_allocation_cost (void)
2444*38fd1498Szrj {
2445*38fd1498Szrj   int hard_regno, cost;
2446*38fd1498Szrj   ira_allocno_t a;
2447*38fd1498Szrj   ira_allocno_iterator ai;
2448*38fd1498Szrj 
2449*38fd1498Szrj   ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
2450*38fd1498Szrj   FOR_EACH_ALLOCNO (a, ai)
2451*38fd1498Szrj     {
2452*38fd1498Szrj       hard_regno = ALLOCNO_HARD_REGNO (a);
2453*38fd1498Szrj       ira_assert (hard_regno < 0
2454*38fd1498Szrj 		  || (ira_hard_reg_in_set_p
2455*38fd1498Szrj 		      (hard_regno, ALLOCNO_MODE (a),
2456*38fd1498Szrj 		       reg_class_contents[ALLOCNO_CLASS (a)])));
2457*38fd1498Szrj       if (hard_regno < 0)
2458*38fd1498Szrj 	{
2459*38fd1498Szrj 	  cost = ALLOCNO_MEMORY_COST (a);
2460*38fd1498Szrj 	  ira_mem_cost += cost;
2461*38fd1498Szrj 	}
2462*38fd1498Szrj       else if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
2463*38fd1498Szrj 	{
2464*38fd1498Szrj 	  cost = (ALLOCNO_HARD_REG_COSTS (a)
2465*38fd1498Szrj 		  [ira_class_hard_reg_index
2466*38fd1498Szrj 		   [ALLOCNO_CLASS (a)][hard_regno]]);
2467*38fd1498Szrj 	  ira_reg_cost += cost;
2468*38fd1498Szrj 	}
2469*38fd1498Szrj       else
2470*38fd1498Szrj 	{
2471*38fd1498Szrj 	  cost = ALLOCNO_CLASS_COST (a);
2472*38fd1498Szrj 	  ira_reg_cost += cost;
2473*38fd1498Szrj 	}
2474*38fd1498Szrj       ira_overall_cost += cost;
2475*38fd1498Szrj     }
2476*38fd1498Szrj 
2477*38fd1498Szrj   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2478*38fd1498Szrj     {
2479*38fd1498Szrj       fprintf (ira_dump_file,
2480*38fd1498Szrj 	       "+++Costs: overall %" PRId64
2481*38fd1498Szrj 	       ", reg %" PRId64
2482*38fd1498Szrj 	       ", mem %" PRId64
2483*38fd1498Szrj 	       ", ld %" PRId64
2484*38fd1498Szrj 	       ", st %" PRId64
2485*38fd1498Szrj 	       ", move %" PRId64,
2486*38fd1498Szrj 	       ira_overall_cost, ira_reg_cost, ira_mem_cost,
2487*38fd1498Szrj 	       ira_load_cost, ira_store_cost, ira_shuffle_cost);
2488*38fd1498Szrj       fprintf (ira_dump_file, "\n+++       move loops %d, new jumps %d\n",
2489*38fd1498Szrj 	       ira_move_loops_num, ira_additional_jumps_num);
2490*38fd1498Szrj     }
2491*38fd1498Szrj 
2492*38fd1498Szrj }
2493*38fd1498Szrj 
2494*38fd1498Szrj #ifdef ENABLE_IRA_CHECKING
2495*38fd1498Szrj /* Check the correctness of the allocation.  We do need this because
2496*38fd1498Szrj    of complicated code to transform more one region internal
2497*38fd1498Szrj    representation into one region representation.  */
2498*38fd1498Szrj static void
check_allocation(void)2499*38fd1498Szrj check_allocation (void)
2500*38fd1498Szrj {
2501*38fd1498Szrj   ira_allocno_t a;
2502*38fd1498Szrj   int hard_regno, nregs, conflict_nregs;
2503*38fd1498Szrj   ira_allocno_iterator ai;
2504*38fd1498Szrj 
2505*38fd1498Szrj   FOR_EACH_ALLOCNO (a, ai)
2506*38fd1498Szrj     {
2507*38fd1498Szrj       int n = ALLOCNO_NUM_OBJECTS (a);
2508*38fd1498Szrj       int i;
2509*38fd1498Szrj 
2510*38fd1498Szrj       if (ALLOCNO_CAP_MEMBER (a) != NULL
2511*38fd1498Szrj 	  || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
2512*38fd1498Szrj 	continue;
2513*38fd1498Szrj       nregs = hard_regno_nregs (hard_regno, ALLOCNO_MODE (a));
2514*38fd1498Szrj       if (nregs == 1)
2515*38fd1498Szrj 	/* We allocated a single hard register.  */
2516*38fd1498Szrj 	n = 1;
2517*38fd1498Szrj       else if (n > 1)
2518*38fd1498Szrj 	/* We allocated multiple hard registers, and we will test
2519*38fd1498Szrj 	   conflicts in a granularity of single hard regs.  */
2520*38fd1498Szrj 	nregs = 1;
2521*38fd1498Szrj 
2522*38fd1498Szrj       for (i = 0; i < n; i++)
2523*38fd1498Szrj 	{
2524*38fd1498Szrj 	  ira_object_t obj = ALLOCNO_OBJECT (a, i);
2525*38fd1498Szrj 	  ira_object_t conflict_obj;
2526*38fd1498Szrj 	  ira_object_conflict_iterator oci;
2527*38fd1498Szrj 	  int this_regno = hard_regno;
2528*38fd1498Szrj 	  if (n > 1)
2529*38fd1498Szrj 	    {
2530*38fd1498Szrj 	      if (REG_WORDS_BIG_ENDIAN)
2531*38fd1498Szrj 		this_regno += n - i - 1;
2532*38fd1498Szrj 	      else
2533*38fd1498Szrj 		this_regno += i;
2534*38fd1498Szrj 	    }
2535*38fd1498Szrj 	  FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2536*38fd1498Szrj 	    {
2537*38fd1498Szrj 	      ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2538*38fd1498Szrj 	      int conflict_hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
2539*38fd1498Szrj 	      if (conflict_hard_regno < 0)
2540*38fd1498Szrj 		continue;
2541*38fd1498Szrj 
2542*38fd1498Szrj 	      conflict_nregs = hard_regno_nregs (conflict_hard_regno,
2543*38fd1498Szrj 						 ALLOCNO_MODE (conflict_a));
2544*38fd1498Szrj 
2545*38fd1498Szrj 	      if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1
2546*38fd1498Szrj 		  && conflict_nregs == ALLOCNO_NUM_OBJECTS (conflict_a))
2547*38fd1498Szrj 		{
2548*38fd1498Szrj 		  if (REG_WORDS_BIG_ENDIAN)
2549*38fd1498Szrj 		    conflict_hard_regno += (ALLOCNO_NUM_OBJECTS (conflict_a)
2550*38fd1498Szrj 					    - OBJECT_SUBWORD (conflict_obj) - 1);
2551*38fd1498Szrj 		  else
2552*38fd1498Szrj 		    conflict_hard_regno += OBJECT_SUBWORD (conflict_obj);
2553*38fd1498Szrj 		  conflict_nregs = 1;
2554*38fd1498Szrj 		}
2555*38fd1498Szrj 
2556*38fd1498Szrj 	      if ((conflict_hard_regno <= this_regno
2557*38fd1498Szrj 		 && this_regno < conflict_hard_regno + conflict_nregs)
2558*38fd1498Szrj 		|| (this_regno <= conflict_hard_regno
2559*38fd1498Szrj 		    && conflict_hard_regno < this_regno + nregs))
2560*38fd1498Szrj 		{
2561*38fd1498Szrj 		  fprintf (stderr, "bad allocation for %d and %d\n",
2562*38fd1498Szrj 			   ALLOCNO_REGNO (a), ALLOCNO_REGNO (conflict_a));
2563*38fd1498Szrj 		  gcc_unreachable ();
2564*38fd1498Szrj 		}
2565*38fd1498Szrj 	    }
2566*38fd1498Szrj 	}
2567*38fd1498Szrj     }
2568*38fd1498Szrj }
2569*38fd1498Szrj #endif
2570*38fd1498Szrj 
2571*38fd1498Szrj /* Allocate REG_EQUIV_INIT.  Set up it from IRA_REG_EQUIV which should
2572*38fd1498Szrj    be already calculated.  */
2573*38fd1498Szrj static void
setup_reg_equiv_init(void)2574*38fd1498Szrj setup_reg_equiv_init (void)
2575*38fd1498Szrj {
2576*38fd1498Szrj   int i;
2577*38fd1498Szrj   int max_regno = max_reg_num ();
2578*38fd1498Szrj 
2579*38fd1498Szrj   for (i = 0; i < max_regno; i++)
2580*38fd1498Szrj     reg_equiv_init (i) = ira_reg_equiv[i].init_insns;
2581*38fd1498Szrj }
2582*38fd1498Szrj 
2583*38fd1498Szrj /* Update equiv regno from movement of FROM_REGNO to TO_REGNO.  INSNS
2584*38fd1498Szrj    are insns which were generated for such movement.  It is assumed
2585*38fd1498Szrj    that FROM_REGNO and TO_REGNO always have the same value at the
2586*38fd1498Szrj    point of any move containing such registers. This function is used
2587*38fd1498Szrj    to update equiv info for register shuffles on the region borders
2588*38fd1498Szrj    and for caller save/restore insns.  */
2589*38fd1498Szrj void
ira_update_equiv_info_by_shuffle_insn(int to_regno,int from_regno,rtx_insn * insns)2590*38fd1498Szrj ira_update_equiv_info_by_shuffle_insn (int to_regno, int from_regno, rtx_insn *insns)
2591*38fd1498Szrj {
2592*38fd1498Szrj   rtx_insn *insn;
2593*38fd1498Szrj   rtx x, note;
2594*38fd1498Szrj 
2595*38fd1498Szrj   if (! ira_reg_equiv[from_regno].defined_p
2596*38fd1498Szrj       && (! ira_reg_equiv[to_regno].defined_p
2597*38fd1498Szrj 	  || ((x = ira_reg_equiv[to_regno].memory) != NULL_RTX
2598*38fd1498Szrj 	      && ! MEM_READONLY_P (x))))
2599*38fd1498Szrj     return;
2600*38fd1498Szrj   insn = insns;
2601*38fd1498Szrj   if (NEXT_INSN (insn) != NULL_RTX)
2602*38fd1498Szrj     {
2603*38fd1498Szrj       if (! ira_reg_equiv[to_regno].defined_p)
2604*38fd1498Szrj 	{
2605*38fd1498Szrj 	  ira_assert (ira_reg_equiv[to_regno].init_insns == NULL_RTX);
2606*38fd1498Szrj 	  return;
2607*38fd1498Szrj 	}
2608*38fd1498Szrj       ira_reg_equiv[to_regno].defined_p = false;
2609*38fd1498Szrj       ira_reg_equiv[to_regno].memory
2610*38fd1498Szrj 	= ira_reg_equiv[to_regno].constant
2611*38fd1498Szrj 	= ira_reg_equiv[to_regno].invariant
2612*38fd1498Szrj 	= ira_reg_equiv[to_regno].init_insns = NULL;
2613*38fd1498Szrj       if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2614*38fd1498Szrj 	fprintf (ira_dump_file,
2615*38fd1498Szrj 		 "      Invalidating equiv info for reg %d\n", to_regno);
2616*38fd1498Szrj       return;
2617*38fd1498Szrj     }
2618*38fd1498Szrj   /* It is possible that FROM_REGNO still has no equivalence because
2619*38fd1498Szrj      in shuffles to_regno<-from_regno and from_regno<-to_regno the 2nd
2620*38fd1498Szrj      insn was not processed yet.  */
2621*38fd1498Szrj   if (ira_reg_equiv[from_regno].defined_p)
2622*38fd1498Szrj     {
2623*38fd1498Szrj       ira_reg_equiv[to_regno].defined_p = true;
2624*38fd1498Szrj       if ((x = ira_reg_equiv[from_regno].memory) != NULL_RTX)
2625*38fd1498Szrj 	{
2626*38fd1498Szrj 	  ira_assert (ira_reg_equiv[from_regno].invariant == NULL_RTX
2627*38fd1498Szrj 		      && ira_reg_equiv[from_regno].constant == NULL_RTX);
2628*38fd1498Szrj 	  ira_assert (ira_reg_equiv[to_regno].memory == NULL_RTX
2629*38fd1498Szrj 		      || rtx_equal_p (ira_reg_equiv[to_regno].memory, x));
2630*38fd1498Szrj 	  ira_reg_equiv[to_regno].memory = x;
2631*38fd1498Szrj 	  if (! MEM_READONLY_P (x))
2632*38fd1498Szrj 	    /* We don't add the insn to insn init list because memory
2633*38fd1498Szrj 	       equivalence is just to say what memory is better to use
2634*38fd1498Szrj 	       when the pseudo is spilled.  */
2635*38fd1498Szrj 	    return;
2636*38fd1498Szrj 	}
2637*38fd1498Szrj       else if ((x = ira_reg_equiv[from_regno].constant) != NULL_RTX)
2638*38fd1498Szrj 	{
2639*38fd1498Szrj 	  ira_assert (ira_reg_equiv[from_regno].invariant == NULL_RTX);
2640*38fd1498Szrj 	  ira_assert (ira_reg_equiv[to_regno].constant == NULL_RTX
2641*38fd1498Szrj 		      || rtx_equal_p (ira_reg_equiv[to_regno].constant, x));
2642*38fd1498Szrj 	  ira_reg_equiv[to_regno].constant = x;
2643*38fd1498Szrj 	}
2644*38fd1498Szrj       else
2645*38fd1498Szrj 	{
2646*38fd1498Szrj 	  x = ira_reg_equiv[from_regno].invariant;
2647*38fd1498Szrj 	  ira_assert (x != NULL_RTX);
2648*38fd1498Szrj 	  ira_assert (ira_reg_equiv[to_regno].invariant == NULL_RTX
2649*38fd1498Szrj 		      || rtx_equal_p (ira_reg_equiv[to_regno].invariant, x));
2650*38fd1498Szrj 	  ira_reg_equiv[to_regno].invariant = x;
2651*38fd1498Szrj 	}
2652*38fd1498Szrj       if (find_reg_note (insn, REG_EQUIV, x) == NULL_RTX)
2653*38fd1498Szrj 	{
2654*38fd1498Szrj 	  note = set_unique_reg_note (insn, REG_EQUIV, copy_rtx (x));
2655*38fd1498Szrj 	  gcc_assert (note != NULL_RTX);
2656*38fd1498Szrj 	  if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2657*38fd1498Szrj 	    {
2658*38fd1498Szrj 	      fprintf (ira_dump_file,
2659*38fd1498Szrj 		       "      Adding equiv note to insn %u for reg %d ",
2660*38fd1498Szrj 		       INSN_UID (insn), to_regno);
2661*38fd1498Szrj 	      dump_value_slim (ira_dump_file, x, 1);
2662*38fd1498Szrj 	      fprintf (ira_dump_file, "\n");
2663*38fd1498Szrj 	    }
2664*38fd1498Szrj 	}
2665*38fd1498Szrj     }
2666*38fd1498Szrj   ira_reg_equiv[to_regno].init_insns
2667*38fd1498Szrj     = gen_rtx_INSN_LIST (VOIDmode, insn,
2668*38fd1498Szrj 			 ira_reg_equiv[to_regno].init_insns);
2669*38fd1498Szrj   if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2670*38fd1498Szrj     fprintf (ira_dump_file,
2671*38fd1498Szrj 	     "      Adding equiv init move insn %u to reg %d\n",
2672*38fd1498Szrj 	     INSN_UID (insn), to_regno);
2673*38fd1498Szrj }
2674*38fd1498Szrj 
2675*38fd1498Szrj /* Fix values of array REG_EQUIV_INIT after live range splitting done
2676*38fd1498Szrj    by IRA.  */
2677*38fd1498Szrj static void
fix_reg_equiv_init(void)2678*38fd1498Szrj fix_reg_equiv_init (void)
2679*38fd1498Szrj {
2680*38fd1498Szrj   int max_regno = max_reg_num ();
2681*38fd1498Szrj   int i, new_regno, max;
2682*38fd1498Szrj   rtx set;
2683*38fd1498Szrj   rtx_insn_list *x, *next, *prev;
2684*38fd1498Szrj   rtx_insn *insn;
2685*38fd1498Szrj 
2686*38fd1498Szrj   if (max_regno_before_ira < max_regno)
2687*38fd1498Szrj     {
2688*38fd1498Szrj       max = vec_safe_length (reg_equivs);
2689*38fd1498Szrj       grow_reg_equivs ();
2690*38fd1498Szrj       for (i = FIRST_PSEUDO_REGISTER; i < max; i++)
2691*38fd1498Szrj 	for (prev = NULL, x = reg_equiv_init (i);
2692*38fd1498Szrj 	     x != NULL_RTX;
2693*38fd1498Szrj 	     x = next)
2694*38fd1498Szrj 	  {
2695*38fd1498Szrj 	    next = x->next ();
2696*38fd1498Szrj 	    insn = x->insn ();
2697*38fd1498Szrj 	    set = single_set (insn);
2698*38fd1498Szrj 	    ira_assert (set != NULL_RTX
2699*38fd1498Szrj 			&& (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))));
2700*38fd1498Szrj 	    if (REG_P (SET_DEST (set))
2701*38fd1498Szrj 		&& ((int) REGNO (SET_DEST (set)) == i
2702*38fd1498Szrj 		    || (int) ORIGINAL_REGNO (SET_DEST (set)) == i))
2703*38fd1498Szrj 	      new_regno = REGNO (SET_DEST (set));
2704*38fd1498Szrj 	    else if (REG_P (SET_SRC (set))
2705*38fd1498Szrj 		     && ((int) REGNO (SET_SRC (set)) == i
2706*38fd1498Szrj 			 || (int) ORIGINAL_REGNO (SET_SRC (set)) == i))
2707*38fd1498Szrj 	      new_regno = REGNO (SET_SRC (set));
2708*38fd1498Szrj 	    else
2709*38fd1498Szrj  	      gcc_unreachable ();
2710*38fd1498Szrj 	    if (new_regno == i)
2711*38fd1498Szrj 	      prev = x;
2712*38fd1498Szrj 	    else
2713*38fd1498Szrj 	      {
2714*38fd1498Szrj 		/* Remove the wrong list element.  */
2715*38fd1498Szrj 		if (prev == NULL_RTX)
2716*38fd1498Szrj 		  reg_equiv_init (i) = next;
2717*38fd1498Szrj 		else
2718*38fd1498Szrj 		  XEXP (prev, 1) = next;
2719*38fd1498Szrj 		XEXP (x, 1) = reg_equiv_init (new_regno);
2720*38fd1498Szrj 		reg_equiv_init (new_regno) = x;
2721*38fd1498Szrj 	      }
2722*38fd1498Szrj 	  }
2723*38fd1498Szrj     }
2724*38fd1498Szrj }
2725*38fd1498Szrj 
2726*38fd1498Szrj #ifdef ENABLE_IRA_CHECKING
2727*38fd1498Szrj /* Print redundant memory-memory copies.  */
2728*38fd1498Szrj static void
print_redundant_copies(void)2729*38fd1498Szrj print_redundant_copies (void)
2730*38fd1498Szrj {
2731*38fd1498Szrj   int hard_regno;
2732*38fd1498Szrj   ira_allocno_t a;
2733*38fd1498Szrj   ira_copy_t cp, next_cp;
2734*38fd1498Szrj   ira_allocno_iterator ai;
2735*38fd1498Szrj 
2736*38fd1498Szrj   FOR_EACH_ALLOCNO (a, ai)
2737*38fd1498Szrj     {
2738*38fd1498Szrj       if (ALLOCNO_CAP_MEMBER (a) != NULL)
2739*38fd1498Szrj 	/* It is a cap.  */
2740*38fd1498Szrj 	continue;
2741*38fd1498Szrj       hard_regno = ALLOCNO_HARD_REGNO (a);
2742*38fd1498Szrj       if (hard_regno >= 0)
2743*38fd1498Szrj 	continue;
2744*38fd1498Szrj       for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2745*38fd1498Szrj 	if (cp->first == a)
2746*38fd1498Szrj 	  next_cp = cp->next_first_allocno_copy;
2747*38fd1498Szrj 	else
2748*38fd1498Szrj 	  {
2749*38fd1498Szrj 	    next_cp = cp->next_second_allocno_copy;
2750*38fd1498Szrj 	    if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL
2751*38fd1498Szrj 		&& cp->insn != NULL_RTX
2752*38fd1498Szrj 		&& ALLOCNO_HARD_REGNO (cp->first) == hard_regno)
2753*38fd1498Szrj 	      fprintf (ira_dump_file,
2754*38fd1498Szrj 		       "        Redundant move from %d(freq %d):%d\n",
2755*38fd1498Szrj 		       INSN_UID (cp->insn), cp->freq, hard_regno);
2756*38fd1498Szrj 	  }
2757*38fd1498Szrj     }
2758*38fd1498Szrj }
2759*38fd1498Szrj #endif
2760*38fd1498Szrj 
2761*38fd1498Szrj /* Setup preferred and alternative classes for new pseudo-registers
2762*38fd1498Szrj    created by IRA starting with START.  */
2763*38fd1498Szrj static void
setup_preferred_alternate_classes_for_new_pseudos(int start)2764*38fd1498Szrj setup_preferred_alternate_classes_for_new_pseudos (int start)
2765*38fd1498Szrj {
2766*38fd1498Szrj   int i, old_regno;
2767*38fd1498Szrj   int max_regno = max_reg_num ();
2768*38fd1498Szrj 
2769*38fd1498Szrj   for (i = start; i < max_regno; i++)
2770*38fd1498Szrj     {
2771*38fd1498Szrj       old_regno = ORIGINAL_REGNO (regno_reg_rtx[i]);
2772*38fd1498Szrj       ira_assert (i != old_regno);
2773*38fd1498Szrj       setup_reg_classes (i, reg_preferred_class (old_regno),
2774*38fd1498Szrj 			 reg_alternate_class (old_regno),
2775*38fd1498Szrj 			 reg_allocno_class (old_regno));
2776*38fd1498Szrj       if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2777*38fd1498Szrj 	fprintf (ira_dump_file,
2778*38fd1498Szrj 		 "    New r%d: setting preferred %s, alternative %s\n",
2779*38fd1498Szrj 		 i, reg_class_names[reg_preferred_class (old_regno)],
2780*38fd1498Szrj 		 reg_class_names[reg_alternate_class (old_regno)]);
2781*38fd1498Szrj     }
2782*38fd1498Szrj }
2783*38fd1498Szrj 
2784*38fd1498Szrj 
2785*38fd1498Szrj /* The number of entries allocated in reg_info.  */
2786*38fd1498Szrj static int allocated_reg_info_size;
2787*38fd1498Szrj 
2788*38fd1498Szrj /* Regional allocation can create new pseudo-registers.  This function
2789*38fd1498Szrj    expands some arrays for pseudo-registers.  */
2790*38fd1498Szrj static void
expand_reg_info(void)2791*38fd1498Szrj expand_reg_info (void)
2792*38fd1498Szrj {
2793*38fd1498Szrj   int i;
2794*38fd1498Szrj   int size = max_reg_num ();
2795*38fd1498Szrj 
2796*38fd1498Szrj   resize_reg_info ();
2797*38fd1498Szrj   for (i = allocated_reg_info_size; i < size; i++)
2798*38fd1498Szrj     setup_reg_classes (i, GENERAL_REGS, ALL_REGS, GENERAL_REGS);
2799*38fd1498Szrj   setup_preferred_alternate_classes_for_new_pseudos (allocated_reg_info_size);
2800*38fd1498Szrj   allocated_reg_info_size = size;
2801*38fd1498Szrj }
2802*38fd1498Szrj 
2803*38fd1498Szrj /* Return TRUE if there is too high register pressure in the function.
2804*38fd1498Szrj    It is used to decide when stack slot sharing is worth to do.  */
2805*38fd1498Szrj static bool
too_high_register_pressure_p(void)2806*38fd1498Szrj too_high_register_pressure_p (void)
2807*38fd1498Szrj {
2808*38fd1498Szrj   int i;
2809*38fd1498Szrj   enum reg_class pclass;
2810*38fd1498Szrj 
2811*38fd1498Szrj   for (i = 0; i < ira_pressure_classes_num; i++)
2812*38fd1498Szrj     {
2813*38fd1498Szrj       pclass = ira_pressure_classes[i];
2814*38fd1498Szrj       if (ira_loop_tree_root->reg_pressure[pclass] > 10000)
2815*38fd1498Szrj 	return true;
2816*38fd1498Szrj     }
2817*38fd1498Szrj   return false;
2818*38fd1498Szrj }
2819*38fd1498Szrj 
2820*38fd1498Szrj 
2821*38fd1498Szrj 
2822*38fd1498Szrj /* Indicate that hard register number FROM was eliminated and replaced with
2823*38fd1498Szrj    an offset from hard register number TO.  The status of hard registers live
2824*38fd1498Szrj    at the start of a basic block is updated by replacing a use of FROM with
2825*38fd1498Szrj    a use of TO.  */
2826*38fd1498Szrj 
2827*38fd1498Szrj void
mark_elimination(int from,int to)2828*38fd1498Szrj mark_elimination (int from, int to)
2829*38fd1498Szrj {
2830*38fd1498Szrj   basic_block bb;
2831*38fd1498Szrj   bitmap r;
2832*38fd1498Szrj 
2833*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
2834*38fd1498Szrj     {
2835*38fd1498Szrj       r = DF_LR_IN (bb);
2836*38fd1498Szrj       if (bitmap_bit_p (r, from))
2837*38fd1498Szrj 	{
2838*38fd1498Szrj 	  bitmap_clear_bit (r, from);
2839*38fd1498Szrj 	  bitmap_set_bit (r, to);
2840*38fd1498Szrj 	}
2841*38fd1498Szrj       if (! df_live)
2842*38fd1498Szrj         continue;
2843*38fd1498Szrj       r = DF_LIVE_IN (bb);
2844*38fd1498Szrj       if (bitmap_bit_p (r, from))
2845*38fd1498Szrj 	{
2846*38fd1498Szrj 	  bitmap_clear_bit (r, from);
2847*38fd1498Szrj 	  bitmap_set_bit (r, to);
2848*38fd1498Szrj 	}
2849*38fd1498Szrj     }
2850*38fd1498Szrj }
2851*38fd1498Szrj 
2852*38fd1498Szrj 
2853*38fd1498Szrj 
2854*38fd1498Szrj /* The length of the following array.  */
2855*38fd1498Szrj int ira_reg_equiv_len;
2856*38fd1498Szrj 
2857*38fd1498Szrj /* Info about equiv. info for each register.  */
2858*38fd1498Szrj struct ira_reg_equiv_s *ira_reg_equiv;
2859*38fd1498Szrj 
2860*38fd1498Szrj /* Expand ira_reg_equiv if necessary.  */
2861*38fd1498Szrj void
ira_expand_reg_equiv(void)2862*38fd1498Szrj ira_expand_reg_equiv (void)
2863*38fd1498Szrj {
2864*38fd1498Szrj   int old = ira_reg_equiv_len;
2865*38fd1498Szrj 
2866*38fd1498Szrj   if (ira_reg_equiv_len > max_reg_num ())
2867*38fd1498Szrj     return;
2868*38fd1498Szrj   ira_reg_equiv_len = max_reg_num () * 3 / 2 + 1;
2869*38fd1498Szrj   ira_reg_equiv
2870*38fd1498Szrj     = (struct ira_reg_equiv_s *) xrealloc (ira_reg_equiv,
2871*38fd1498Szrj 					 ira_reg_equiv_len
2872*38fd1498Szrj 					 * sizeof (struct ira_reg_equiv_s));
2873*38fd1498Szrj   gcc_assert (old < ira_reg_equiv_len);
2874*38fd1498Szrj   memset (ira_reg_equiv + old, 0,
2875*38fd1498Szrj 	  sizeof (struct ira_reg_equiv_s) * (ira_reg_equiv_len - old));
2876*38fd1498Szrj }
2877*38fd1498Szrj 
2878*38fd1498Szrj static void
init_reg_equiv(void)2879*38fd1498Szrj init_reg_equiv (void)
2880*38fd1498Szrj {
2881*38fd1498Szrj   ira_reg_equiv_len = 0;
2882*38fd1498Szrj   ira_reg_equiv = NULL;
2883*38fd1498Szrj   ira_expand_reg_equiv ();
2884*38fd1498Szrj }
2885*38fd1498Szrj 
2886*38fd1498Szrj static void
finish_reg_equiv(void)2887*38fd1498Szrj finish_reg_equiv (void)
2888*38fd1498Szrj {
2889*38fd1498Szrj   free (ira_reg_equiv);
2890*38fd1498Szrj }
2891*38fd1498Szrj 
2892*38fd1498Szrj 
2893*38fd1498Szrj 
2894*38fd1498Szrj struct equivalence
2895*38fd1498Szrj {
2896*38fd1498Szrj   /* Set when a REG_EQUIV note is found or created.  Use to
2897*38fd1498Szrj      keep track of what memory accesses might be created later,
2898*38fd1498Szrj      e.g. by reload.  */
2899*38fd1498Szrj   rtx replacement;
2900*38fd1498Szrj   rtx *src_p;
2901*38fd1498Szrj 
2902*38fd1498Szrj   /* The list of each instruction which initializes this register.
2903*38fd1498Szrj 
2904*38fd1498Szrj      NULL indicates we know nothing about this register's equivalence
2905*38fd1498Szrj      properties.
2906*38fd1498Szrj 
2907*38fd1498Szrj      An INSN_LIST with a NULL insn indicates this pseudo is already
2908*38fd1498Szrj      known to not have a valid equivalence.  */
2909*38fd1498Szrj   rtx_insn_list *init_insns;
2910*38fd1498Szrj 
2911*38fd1498Szrj   /* Loop depth is used to recognize equivalences which appear
2912*38fd1498Szrj      to be present within the same loop (or in an inner loop).  */
2913*38fd1498Szrj   short loop_depth;
2914*38fd1498Szrj   /* Nonzero if this had a preexisting REG_EQUIV note.  */
2915*38fd1498Szrj   unsigned char is_arg_equivalence : 1;
2916*38fd1498Szrj   /* Set when an attempt should be made to replace a register
2917*38fd1498Szrj      with the associated src_p entry.  */
2918*38fd1498Szrj   unsigned char replace : 1;
2919*38fd1498Szrj   /* Set if this register has no known equivalence.  */
2920*38fd1498Szrj   unsigned char no_equiv : 1;
2921*38fd1498Szrj   /* Set if this register is mentioned in a paradoxical subreg.  */
2922*38fd1498Szrj   unsigned char pdx_subregs : 1;
2923*38fd1498Szrj };
2924*38fd1498Szrj 
2925*38fd1498Szrj /* reg_equiv[N] (where N is a pseudo reg number) is the equivalence
2926*38fd1498Szrj    structure for that register.  */
2927*38fd1498Szrj static struct equivalence *reg_equiv;
2928*38fd1498Szrj 
2929*38fd1498Szrj /* Used for communication between the following two functions.  */
2930*38fd1498Szrj struct equiv_mem_data
2931*38fd1498Szrj {
2932*38fd1498Szrj   /* A MEM that we wish to ensure remains unchanged.  */
2933*38fd1498Szrj   rtx equiv_mem;
2934*38fd1498Szrj 
2935*38fd1498Szrj   /* Set true if EQUIV_MEM is modified.  */
2936*38fd1498Szrj   bool equiv_mem_modified;
2937*38fd1498Szrj };
2938*38fd1498Szrj 
2939*38fd1498Szrj /* If EQUIV_MEM is modified by modifying DEST, indicate that it is modified.
2940*38fd1498Szrj    Called via note_stores.  */
2941*38fd1498Szrj static void
validate_equiv_mem_from_store(rtx dest,const_rtx set ATTRIBUTE_UNUSED,void * data)2942*38fd1498Szrj validate_equiv_mem_from_store (rtx dest, const_rtx set ATTRIBUTE_UNUSED,
2943*38fd1498Szrj 			       void *data)
2944*38fd1498Szrj {
2945*38fd1498Szrj   struct equiv_mem_data *info = (struct equiv_mem_data *) data;
2946*38fd1498Szrj 
2947*38fd1498Szrj   if ((REG_P (dest)
2948*38fd1498Szrj        && reg_overlap_mentioned_p (dest, info->equiv_mem))
2949*38fd1498Szrj       || (MEM_P (dest)
2950*38fd1498Szrj 	  && anti_dependence (info->equiv_mem, dest)))
2951*38fd1498Szrj     info->equiv_mem_modified = true;
2952*38fd1498Szrj }
2953*38fd1498Szrj 
2954*38fd1498Szrj enum valid_equiv { valid_none, valid_combine, valid_reload };
2955*38fd1498Szrj 
2956*38fd1498Szrj /* Verify that no store between START and the death of REG invalidates
2957*38fd1498Szrj    MEMREF.  MEMREF is invalidated by modifying a register used in MEMREF,
2958*38fd1498Szrj    by storing into an overlapping memory location, or with a non-const
2959*38fd1498Szrj    CALL_INSN.
2960*38fd1498Szrj 
2961*38fd1498Szrj    Return VALID_RELOAD if MEMREF remains valid for both reload and
2962*38fd1498Szrj    combine_and_move insns, VALID_COMBINE if only valid for
2963*38fd1498Szrj    combine_and_move_insns, and VALID_NONE otherwise.  */
2964*38fd1498Szrj static enum valid_equiv
validate_equiv_mem(rtx_insn * start,rtx reg,rtx memref)2965*38fd1498Szrj validate_equiv_mem (rtx_insn *start, rtx reg, rtx memref)
2966*38fd1498Szrj {
2967*38fd1498Szrj   rtx_insn *insn;
2968*38fd1498Szrj   rtx note;
2969*38fd1498Szrj   struct equiv_mem_data info = { memref, false };
2970*38fd1498Szrj   enum valid_equiv ret = valid_reload;
2971*38fd1498Szrj 
2972*38fd1498Szrj   /* If the memory reference has side effects or is volatile, it isn't a
2973*38fd1498Szrj      valid equivalence.  */
2974*38fd1498Szrj   if (side_effects_p (memref))
2975*38fd1498Szrj     return valid_none;
2976*38fd1498Szrj 
2977*38fd1498Szrj   for (insn = start; insn; insn = NEXT_INSN (insn))
2978*38fd1498Szrj     {
2979*38fd1498Szrj       if (!INSN_P (insn))
2980*38fd1498Szrj 	continue;
2981*38fd1498Szrj 
2982*38fd1498Szrj       if (find_reg_note (insn, REG_DEAD, reg))
2983*38fd1498Szrj 	return ret;
2984*38fd1498Szrj 
2985*38fd1498Szrj       if (CALL_P (insn))
2986*38fd1498Szrj 	{
2987*38fd1498Szrj 	  /* We can combine a reg def from one insn into a reg use in
2988*38fd1498Szrj 	     another over a call if the memory is readonly or the call
2989*38fd1498Szrj 	     const/pure.  However, we can't set reg_equiv notes up for
2990*38fd1498Szrj 	     reload over any call.  The problem is the equivalent form
2991*38fd1498Szrj 	     may reference a pseudo which gets assigned a call
2992*38fd1498Szrj 	     clobbered hard reg.  When we later replace REG with its
2993*38fd1498Szrj 	     equivalent form, the value in the call-clobbered reg has
2994*38fd1498Szrj 	     been changed and all hell breaks loose.  */
2995*38fd1498Szrj 	  ret = valid_combine;
2996*38fd1498Szrj 	  if (!MEM_READONLY_P (memref)
2997*38fd1498Szrj 	      && !RTL_CONST_OR_PURE_CALL_P (insn))
2998*38fd1498Szrj 	    return valid_none;
2999*38fd1498Szrj 	}
3000*38fd1498Szrj 
3001*38fd1498Szrj       note_stores (PATTERN (insn), validate_equiv_mem_from_store, &info);
3002*38fd1498Szrj       if (info.equiv_mem_modified)
3003*38fd1498Szrj 	return valid_none;
3004*38fd1498Szrj 
3005*38fd1498Szrj       /* If a register mentioned in MEMREF is modified via an
3006*38fd1498Szrj 	 auto-increment, we lose the equivalence.  Do the same if one
3007*38fd1498Szrj 	 dies; although we could extend the life, it doesn't seem worth
3008*38fd1498Szrj 	 the trouble.  */
3009*38fd1498Szrj 
3010*38fd1498Szrj       for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3011*38fd1498Szrj 	if ((REG_NOTE_KIND (note) == REG_INC
3012*38fd1498Szrj 	     || REG_NOTE_KIND (note) == REG_DEAD)
3013*38fd1498Szrj 	    && REG_P (XEXP (note, 0))
3014*38fd1498Szrj 	    && reg_overlap_mentioned_p (XEXP (note, 0), memref))
3015*38fd1498Szrj 	  return valid_none;
3016*38fd1498Szrj     }
3017*38fd1498Szrj 
3018*38fd1498Szrj   return valid_none;
3019*38fd1498Szrj }
3020*38fd1498Szrj 
3021*38fd1498Szrj /* Returns zero if X is known to be invariant.  */
3022*38fd1498Szrj static int
equiv_init_varies_p(rtx x)3023*38fd1498Szrj equiv_init_varies_p (rtx x)
3024*38fd1498Szrj {
3025*38fd1498Szrj   RTX_CODE code = GET_CODE (x);
3026*38fd1498Szrj   int i;
3027*38fd1498Szrj   const char *fmt;
3028*38fd1498Szrj 
3029*38fd1498Szrj   switch (code)
3030*38fd1498Szrj     {
3031*38fd1498Szrj     case MEM:
3032*38fd1498Szrj       return !MEM_READONLY_P (x) || equiv_init_varies_p (XEXP (x, 0));
3033*38fd1498Szrj 
3034*38fd1498Szrj     case CONST:
3035*38fd1498Szrj     CASE_CONST_ANY:
3036*38fd1498Szrj     case SYMBOL_REF:
3037*38fd1498Szrj     case LABEL_REF:
3038*38fd1498Szrj       return 0;
3039*38fd1498Szrj 
3040*38fd1498Szrj     case REG:
3041*38fd1498Szrj       return reg_equiv[REGNO (x)].replace == 0 && rtx_varies_p (x, 0);
3042*38fd1498Szrj 
3043*38fd1498Szrj     case ASM_OPERANDS:
3044*38fd1498Szrj       if (MEM_VOLATILE_P (x))
3045*38fd1498Szrj 	return 1;
3046*38fd1498Szrj 
3047*38fd1498Szrj       /* Fall through.  */
3048*38fd1498Szrj 
3049*38fd1498Szrj     default:
3050*38fd1498Szrj       break;
3051*38fd1498Szrj     }
3052*38fd1498Szrj 
3053*38fd1498Szrj   fmt = GET_RTX_FORMAT (code);
3054*38fd1498Szrj   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3055*38fd1498Szrj     if (fmt[i] == 'e')
3056*38fd1498Szrj       {
3057*38fd1498Szrj 	if (equiv_init_varies_p (XEXP (x, i)))
3058*38fd1498Szrj 	  return 1;
3059*38fd1498Szrj       }
3060*38fd1498Szrj     else if (fmt[i] == 'E')
3061*38fd1498Szrj       {
3062*38fd1498Szrj 	int j;
3063*38fd1498Szrj 	for (j = 0; j < XVECLEN (x, i); j++)
3064*38fd1498Szrj 	  if (equiv_init_varies_p (XVECEXP (x, i, j)))
3065*38fd1498Szrj 	    return 1;
3066*38fd1498Szrj       }
3067*38fd1498Szrj 
3068*38fd1498Szrj   return 0;
3069*38fd1498Szrj }
3070*38fd1498Szrj 
3071*38fd1498Szrj /* Returns nonzero if X (used to initialize register REGNO) is movable.
3072*38fd1498Szrj    X is only movable if the registers it uses have equivalent initializations
3073*38fd1498Szrj    which appear to be within the same loop (or in an inner loop) and movable
3074*38fd1498Szrj    or if they are not candidates for local_alloc and don't vary.  */
3075*38fd1498Szrj static int
equiv_init_movable_p(rtx x,int regno)3076*38fd1498Szrj equiv_init_movable_p (rtx x, int regno)
3077*38fd1498Szrj {
3078*38fd1498Szrj   int i, j;
3079*38fd1498Szrj   const char *fmt;
3080*38fd1498Szrj   enum rtx_code code = GET_CODE (x);
3081*38fd1498Szrj 
3082*38fd1498Szrj   switch (code)
3083*38fd1498Szrj     {
3084*38fd1498Szrj     case SET:
3085*38fd1498Szrj       return equiv_init_movable_p (SET_SRC (x), regno);
3086*38fd1498Szrj 
3087*38fd1498Szrj     case CC0:
3088*38fd1498Szrj     case CLOBBER:
3089*38fd1498Szrj       return 0;
3090*38fd1498Szrj 
3091*38fd1498Szrj     case PRE_INC:
3092*38fd1498Szrj     case PRE_DEC:
3093*38fd1498Szrj     case POST_INC:
3094*38fd1498Szrj     case POST_DEC:
3095*38fd1498Szrj     case PRE_MODIFY:
3096*38fd1498Szrj     case POST_MODIFY:
3097*38fd1498Szrj       return 0;
3098*38fd1498Szrj 
3099*38fd1498Szrj     case REG:
3100*38fd1498Szrj       return ((reg_equiv[REGNO (x)].loop_depth >= reg_equiv[regno].loop_depth
3101*38fd1498Szrj 	       && reg_equiv[REGNO (x)].replace)
3102*38fd1498Szrj 	      || (REG_BASIC_BLOCK (REGNO (x)) < NUM_FIXED_BLOCKS
3103*38fd1498Szrj 		  && ! rtx_varies_p (x, 0)));
3104*38fd1498Szrj 
3105*38fd1498Szrj     case UNSPEC_VOLATILE:
3106*38fd1498Szrj       return 0;
3107*38fd1498Szrj 
3108*38fd1498Szrj     case ASM_OPERANDS:
3109*38fd1498Szrj       if (MEM_VOLATILE_P (x))
3110*38fd1498Szrj 	return 0;
3111*38fd1498Szrj 
3112*38fd1498Szrj       /* Fall through.  */
3113*38fd1498Szrj 
3114*38fd1498Szrj     default:
3115*38fd1498Szrj       break;
3116*38fd1498Szrj     }
3117*38fd1498Szrj 
3118*38fd1498Szrj   fmt = GET_RTX_FORMAT (code);
3119*38fd1498Szrj   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3120*38fd1498Szrj     switch (fmt[i])
3121*38fd1498Szrj       {
3122*38fd1498Szrj       case 'e':
3123*38fd1498Szrj 	if (! equiv_init_movable_p (XEXP (x, i), regno))
3124*38fd1498Szrj 	  return 0;
3125*38fd1498Szrj 	break;
3126*38fd1498Szrj       case 'E':
3127*38fd1498Szrj 	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3128*38fd1498Szrj 	  if (! equiv_init_movable_p (XVECEXP (x, i, j), regno))
3129*38fd1498Szrj 	    return 0;
3130*38fd1498Szrj 	break;
3131*38fd1498Szrj       }
3132*38fd1498Szrj 
3133*38fd1498Szrj   return 1;
3134*38fd1498Szrj }
3135*38fd1498Szrj 
3136*38fd1498Szrj /* TRUE if X references a memory location that would be affected by a store
3137*38fd1498Szrj    to MEMREF.  */
3138*38fd1498Szrj static int
memref_referenced_p(rtx memref,rtx x)3139*38fd1498Szrj memref_referenced_p (rtx memref, rtx x)
3140*38fd1498Szrj {
3141*38fd1498Szrj   int i, j;
3142*38fd1498Szrj   const char *fmt;
3143*38fd1498Szrj   enum rtx_code code = GET_CODE (x);
3144*38fd1498Szrj 
3145*38fd1498Szrj   switch (code)
3146*38fd1498Szrj     {
3147*38fd1498Szrj     case CONST:
3148*38fd1498Szrj     case LABEL_REF:
3149*38fd1498Szrj     case SYMBOL_REF:
3150*38fd1498Szrj     CASE_CONST_ANY:
3151*38fd1498Szrj     case PC:
3152*38fd1498Szrj     case CC0:
3153*38fd1498Szrj     case HIGH:
3154*38fd1498Szrj     case LO_SUM:
3155*38fd1498Szrj       return 0;
3156*38fd1498Szrj 
3157*38fd1498Szrj     case REG:
3158*38fd1498Szrj       return (reg_equiv[REGNO (x)].replacement
3159*38fd1498Szrj 	      && memref_referenced_p (memref,
3160*38fd1498Szrj 				      reg_equiv[REGNO (x)].replacement));
3161*38fd1498Szrj 
3162*38fd1498Szrj     case MEM:
3163*38fd1498Szrj       if (true_dependence (memref, VOIDmode, x))
3164*38fd1498Szrj 	return 1;
3165*38fd1498Szrj       break;
3166*38fd1498Szrj 
3167*38fd1498Szrj     case SET:
3168*38fd1498Szrj       /* If we are setting a MEM, it doesn't count (its address does), but any
3169*38fd1498Szrj 	 other SET_DEST that has a MEM in it is referencing the MEM.  */
3170*38fd1498Szrj       if (MEM_P (SET_DEST (x)))
3171*38fd1498Szrj 	{
3172*38fd1498Szrj 	  if (memref_referenced_p (memref, XEXP (SET_DEST (x), 0)))
3173*38fd1498Szrj 	    return 1;
3174*38fd1498Szrj 	}
3175*38fd1498Szrj       else if (memref_referenced_p (memref, SET_DEST (x)))
3176*38fd1498Szrj 	return 1;
3177*38fd1498Szrj 
3178*38fd1498Szrj       return memref_referenced_p (memref, SET_SRC (x));
3179*38fd1498Szrj 
3180*38fd1498Szrj     default:
3181*38fd1498Szrj       break;
3182*38fd1498Szrj     }
3183*38fd1498Szrj 
3184*38fd1498Szrj   fmt = GET_RTX_FORMAT (code);
3185*38fd1498Szrj   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
3186*38fd1498Szrj     switch (fmt[i])
3187*38fd1498Szrj       {
3188*38fd1498Szrj       case 'e':
3189*38fd1498Szrj 	if (memref_referenced_p (memref, XEXP (x, i)))
3190*38fd1498Szrj 	  return 1;
3191*38fd1498Szrj 	break;
3192*38fd1498Szrj       case 'E':
3193*38fd1498Szrj 	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
3194*38fd1498Szrj 	  if (memref_referenced_p (memref, XVECEXP (x, i, j)))
3195*38fd1498Szrj 	    return 1;
3196*38fd1498Szrj 	break;
3197*38fd1498Szrj       }
3198*38fd1498Szrj 
3199*38fd1498Szrj   return 0;
3200*38fd1498Szrj }
3201*38fd1498Szrj 
3202*38fd1498Szrj /* TRUE if some insn in the range (START, END] references a memory location
3203*38fd1498Szrj    that would be affected by a store to MEMREF.
3204*38fd1498Szrj 
3205*38fd1498Szrj    Callers should not call this routine if START is after END in the
3206*38fd1498Szrj    RTL chain.  */
3207*38fd1498Szrj 
3208*38fd1498Szrj static int
memref_used_between_p(rtx memref,rtx_insn * start,rtx_insn * end)3209*38fd1498Szrj memref_used_between_p (rtx memref, rtx_insn *start, rtx_insn *end)
3210*38fd1498Szrj {
3211*38fd1498Szrj   rtx_insn *insn;
3212*38fd1498Szrj 
3213*38fd1498Szrj   for (insn = NEXT_INSN (start);
3214*38fd1498Szrj        insn && insn != NEXT_INSN (end);
3215*38fd1498Szrj        insn = NEXT_INSN (insn))
3216*38fd1498Szrj     {
3217*38fd1498Szrj       if (!NONDEBUG_INSN_P (insn))
3218*38fd1498Szrj 	continue;
3219*38fd1498Szrj 
3220*38fd1498Szrj       if (memref_referenced_p (memref, PATTERN (insn)))
3221*38fd1498Szrj 	return 1;
3222*38fd1498Szrj 
3223*38fd1498Szrj       /* Nonconst functions may access memory.  */
3224*38fd1498Szrj       if (CALL_P (insn) && (! RTL_CONST_CALL_P (insn)))
3225*38fd1498Szrj 	return 1;
3226*38fd1498Szrj     }
3227*38fd1498Szrj 
3228*38fd1498Szrj   gcc_assert (insn == NEXT_INSN (end));
3229*38fd1498Szrj   return 0;
3230*38fd1498Szrj }
3231*38fd1498Szrj 
3232*38fd1498Szrj /* Mark REG as having no known equivalence.
3233*38fd1498Szrj    Some instructions might have been processed before and furnished
3234*38fd1498Szrj    with REG_EQUIV notes for this register; these notes will have to be
3235*38fd1498Szrj    removed.
3236*38fd1498Szrj    STORE is the piece of RTL that does the non-constant / conflicting
3237*38fd1498Szrj    assignment - a SET, CLOBBER or REG_INC note.  It is currently not used,
3238*38fd1498Szrj    but needs to be there because this function is called from note_stores.  */
3239*38fd1498Szrj static void
no_equiv(rtx reg,const_rtx store ATTRIBUTE_UNUSED,void * data ATTRIBUTE_UNUSED)3240*38fd1498Szrj no_equiv (rtx reg, const_rtx store ATTRIBUTE_UNUSED,
3241*38fd1498Szrj 	  void *data ATTRIBUTE_UNUSED)
3242*38fd1498Szrj {
3243*38fd1498Szrj   int regno;
3244*38fd1498Szrj   rtx_insn_list *list;
3245*38fd1498Szrj 
3246*38fd1498Szrj   if (!REG_P (reg))
3247*38fd1498Szrj     return;
3248*38fd1498Szrj   regno = REGNO (reg);
3249*38fd1498Szrj   reg_equiv[regno].no_equiv = 1;
3250*38fd1498Szrj   list = reg_equiv[regno].init_insns;
3251*38fd1498Szrj   if (list && list->insn () == NULL)
3252*38fd1498Szrj     return;
3253*38fd1498Szrj   reg_equiv[regno].init_insns = gen_rtx_INSN_LIST (VOIDmode, NULL_RTX, NULL);
3254*38fd1498Szrj   reg_equiv[regno].replacement = NULL_RTX;
3255*38fd1498Szrj   /* This doesn't matter for equivalences made for argument registers, we
3256*38fd1498Szrj      should keep their initialization insns.  */
3257*38fd1498Szrj   if (reg_equiv[regno].is_arg_equivalence)
3258*38fd1498Szrj     return;
3259*38fd1498Szrj   ira_reg_equiv[regno].defined_p = false;
3260*38fd1498Szrj   ira_reg_equiv[regno].init_insns = NULL;
3261*38fd1498Szrj   for (; list; list = list->next ())
3262*38fd1498Szrj     {
3263*38fd1498Szrj       rtx_insn *insn = list->insn ();
3264*38fd1498Szrj       remove_note (insn, find_reg_note (insn, REG_EQUIV, NULL_RTX));
3265*38fd1498Szrj     }
3266*38fd1498Szrj }
3267*38fd1498Szrj 
3268*38fd1498Szrj /* Check whether the SUBREG is a paradoxical subreg and set the result
3269*38fd1498Szrj    in PDX_SUBREGS.  */
3270*38fd1498Szrj 
3271*38fd1498Szrj static void
set_paradoxical_subreg(rtx_insn * insn)3272*38fd1498Szrj set_paradoxical_subreg (rtx_insn *insn)
3273*38fd1498Szrj {
3274*38fd1498Szrj   subrtx_iterator::array_type array;
3275*38fd1498Szrj   FOR_EACH_SUBRTX (iter, array, PATTERN (insn), NONCONST)
3276*38fd1498Szrj     {
3277*38fd1498Szrj       const_rtx subreg = *iter;
3278*38fd1498Szrj       if (GET_CODE (subreg) == SUBREG)
3279*38fd1498Szrj 	{
3280*38fd1498Szrj 	  const_rtx reg = SUBREG_REG (subreg);
3281*38fd1498Szrj 	  if (REG_P (reg) && paradoxical_subreg_p (subreg))
3282*38fd1498Szrj 	    reg_equiv[REGNO (reg)].pdx_subregs = true;
3283*38fd1498Szrj 	}
3284*38fd1498Szrj     }
3285*38fd1498Szrj }
3286*38fd1498Szrj 
3287*38fd1498Szrj /* In DEBUG_INSN location adjust REGs from CLEARED_REGS bitmap to the
3288*38fd1498Szrj    equivalent replacement.  */
3289*38fd1498Szrj 
3290*38fd1498Szrj static rtx
adjust_cleared_regs(rtx loc,const_rtx old_rtx ATTRIBUTE_UNUSED,void * data)3291*38fd1498Szrj adjust_cleared_regs (rtx loc, const_rtx old_rtx ATTRIBUTE_UNUSED, void *data)
3292*38fd1498Szrj {
3293*38fd1498Szrj   if (REG_P (loc))
3294*38fd1498Szrj     {
3295*38fd1498Szrj       bitmap cleared_regs = (bitmap) data;
3296*38fd1498Szrj       if (bitmap_bit_p (cleared_regs, REGNO (loc)))
3297*38fd1498Szrj 	return simplify_replace_fn_rtx (copy_rtx (*reg_equiv[REGNO (loc)].src_p),
3298*38fd1498Szrj 					NULL_RTX, adjust_cleared_regs, data);
3299*38fd1498Szrj     }
3300*38fd1498Szrj   return NULL_RTX;
3301*38fd1498Szrj }
3302*38fd1498Szrj 
3303*38fd1498Szrj /* Given register REGNO is set only once, return true if the defining
3304*38fd1498Szrj    insn dominates all uses.  */
3305*38fd1498Szrj 
3306*38fd1498Szrj static bool
def_dominates_uses(int regno)3307*38fd1498Szrj def_dominates_uses (int regno)
3308*38fd1498Szrj {
3309*38fd1498Szrj   df_ref def = DF_REG_DEF_CHAIN (regno);
3310*38fd1498Szrj 
3311*38fd1498Szrj   struct df_insn_info *def_info = DF_REF_INSN_INFO (def);
3312*38fd1498Szrj   /* If this is an artificial def (eh handler regs, hard frame pointer
3313*38fd1498Szrj      for non-local goto, regs defined on function entry) then def_info
3314*38fd1498Szrj      is NULL and the reg is always live before any use.  We might
3315*38fd1498Szrj      reasonably return true in that case, but since the only call
3316*38fd1498Szrj      of this function is currently here in ira.c when we are looking
3317*38fd1498Szrj      at a defining insn we can't have an artificial def as that would
3318*38fd1498Szrj      bump DF_REG_DEF_COUNT.  */
3319*38fd1498Szrj   gcc_assert (DF_REG_DEF_COUNT (regno) == 1 && def_info != NULL);
3320*38fd1498Szrj 
3321*38fd1498Szrj   rtx_insn *def_insn = DF_REF_INSN (def);
3322*38fd1498Szrj   basic_block def_bb = BLOCK_FOR_INSN (def_insn);
3323*38fd1498Szrj 
3324*38fd1498Szrj   for (df_ref use = DF_REG_USE_CHAIN (regno);
3325*38fd1498Szrj        use;
3326*38fd1498Szrj        use = DF_REF_NEXT_REG (use))
3327*38fd1498Szrj     {
3328*38fd1498Szrj       struct df_insn_info *use_info = DF_REF_INSN_INFO (use);
3329*38fd1498Szrj       /* Only check real uses, not artificial ones.  */
3330*38fd1498Szrj       if (use_info)
3331*38fd1498Szrj 	{
3332*38fd1498Szrj 	  rtx_insn *use_insn = DF_REF_INSN (use);
3333*38fd1498Szrj 	  if (!DEBUG_INSN_P (use_insn))
3334*38fd1498Szrj 	    {
3335*38fd1498Szrj 	      basic_block use_bb = BLOCK_FOR_INSN (use_insn);
3336*38fd1498Szrj 	      if (use_bb != def_bb
3337*38fd1498Szrj 		  ? !dominated_by_p (CDI_DOMINATORS, use_bb, def_bb)
3338*38fd1498Szrj 		  : DF_INSN_INFO_LUID (use_info) < DF_INSN_INFO_LUID (def_info))
3339*38fd1498Szrj 		return false;
3340*38fd1498Szrj 	    }
3341*38fd1498Szrj 	}
3342*38fd1498Szrj     }
3343*38fd1498Szrj   return true;
3344*38fd1498Szrj }
3345*38fd1498Szrj 
3346*38fd1498Szrj /* Find registers that are equivalent to a single value throughout the
3347*38fd1498Szrj    compilation (either because they can be referenced in memory or are
3348*38fd1498Szrj    set once from a single constant).  Lower their priority for a
3349*38fd1498Szrj    register.
3350*38fd1498Szrj 
3351*38fd1498Szrj    If such a register is only referenced once, try substituting its
3352*38fd1498Szrj    value into the using insn.  If it succeeds, we can eliminate the
3353*38fd1498Szrj    register completely.
3354*38fd1498Szrj 
3355*38fd1498Szrj    Initialize init_insns in ira_reg_equiv array.  */
3356*38fd1498Szrj static void
update_equiv_regs(void)3357*38fd1498Szrj update_equiv_regs (void)
3358*38fd1498Szrj {
3359*38fd1498Szrj   rtx_insn *insn;
3360*38fd1498Szrj   basic_block bb;
3361*38fd1498Szrj 
3362*38fd1498Szrj   /* Scan insns and set pdx_subregs if the reg is used in a
3363*38fd1498Szrj      paradoxical subreg.  Don't set such reg equivalent to a mem,
3364*38fd1498Szrj      because lra will not substitute such equiv memory in order to
3365*38fd1498Szrj      prevent access beyond allocated memory for paradoxical memory subreg.  */
3366*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
3367*38fd1498Szrj     FOR_BB_INSNS (bb, insn)
3368*38fd1498Szrj       if (NONDEBUG_INSN_P (insn))
3369*38fd1498Szrj 	set_paradoxical_subreg (insn);
3370*38fd1498Szrj 
3371*38fd1498Szrj   /* Scan the insns and find which registers have equivalences.  Do this
3372*38fd1498Szrj      in a separate scan of the insns because (due to -fcse-follow-jumps)
3373*38fd1498Szrj      a register can be set below its use.  */
3374*38fd1498Szrj   bitmap setjmp_crosses = regstat_get_setjmp_crosses ();
3375*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
3376*38fd1498Szrj     {
3377*38fd1498Szrj       int loop_depth = bb_loop_depth (bb);
3378*38fd1498Szrj 
3379*38fd1498Szrj       for (insn = BB_HEAD (bb);
3380*38fd1498Szrj 	   insn != NEXT_INSN (BB_END (bb));
3381*38fd1498Szrj 	   insn = NEXT_INSN (insn))
3382*38fd1498Szrj 	{
3383*38fd1498Szrj 	  rtx note;
3384*38fd1498Szrj 	  rtx set;
3385*38fd1498Szrj 	  rtx dest, src;
3386*38fd1498Szrj 	  int regno;
3387*38fd1498Szrj 
3388*38fd1498Szrj 	  if (! INSN_P (insn))
3389*38fd1498Szrj 	    continue;
3390*38fd1498Szrj 
3391*38fd1498Szrj 	  for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
3392*38fd1498Szrj 	    if (REG_NOTE_KIND (note) == REG_INC)
3393*38fd1498Szrj 	      no_equiv (XEXP (note, 0), note, NULL);
3394*38fd1498Szrj 
3395*38fd1498Szrj 	  set = single_set (insn);
3396*38fd1498Szrj 
3397*38fd1498Szrj 	  /* If this insn contains more (or less) than a single SET,
3398*38fd1498Szrj 	     only mark all destinations as having no known equivalence.  */
3399*38fd1498Szrj 	  if (set == NULL_RTX
3400*38fd1498Szrj 	      || side_effects_p (SET_SRC (set)))
3401*38fd1498Szrj 	    {
3402*38fd1498Szrj 	      note_stores (PATTERN (insn), no_equiv, NULL);
3403*38fd1498Szrj 	      continue;
3404*38fd1498Szrj 	    }
3405*38fd1498Szrj 	  else if (GET_CODE (PATTERN (insn)) == PARALLEL)
3406*38fd1498Szrj 	    {
3407*38fd1498Szrj 	      int i;
3408*38fd1498Szrj 
3409*38fd1498Szrj 	      for (i = XVECLEN (PATTERN (insn), 0) - 1; i >= 0; i--)
3410*38fd1498Szrj 		{
3411*38fd1498Szrj 		  rtx part = XVECEXP (PATTERN (insn), 0, i);
3412*38fd1498Szrj 		  if (part != set)
3413*38fd1498Szrj 		    note_stores (part, no_equiv, NULL);
3414*38fd1498Szrj 		}
3415*38fd1498Szrj 	    }
3416*38fd1498Szrj 
3417*38fd1498Szrj 	  dest = SET_DEST (set);
3418*38fd1498Szrj 	  src = SET_SRC (set);
3419*38fd1498Szrj 
3420*38fd1498Szrj 	  /* See if this is setting up the equivalence between an argument
3421*38fd1498Szrj 	     register and its stack slot.  */
3422*38fd1498Szrj 	  note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
3423*38fd1498Szrj 	  if (note)
3424*38fd1498Szrj 	    {
3425*38fd1498Szrj 	      gcc_assert (REG_P (dest));
3426*38fd1498Szrj 	      regno = REGNO (dest);
3427*38fd1498Szrj 
3428*38fd1498Szrj 	      /* Note that we don't want to clear init_insns in
3429*38fd1498Szrj 		 ira_reg_equiv even if there are multiple sets of this
3430*38fd1498Szrj 		 register.  */
3431*38fd1498Szrj 	      reg_equiv[regno].is_arg_equivalence = 1;
3432*38fd1498Szrj 
3433*38fd1498Szrj 	      /* The insn result can have equivalence memory although
3434*38fd1498Szrj 		 the equivalence is not set up by the insn.  We add
3435*38fd1498Szrj 		 this insn to init insns as it is a flag for now that
3436*38fd1498Szrj 		 regno has an equivalence.  We will remove the insn
3437*38fd1498Szrj 		 from init insn list later.  */
3438*38fd1498Szrj 	      if (rtx_equal_p (src, XEXP (note, 0)) || MEM_P (XEXP (note, 0)))
3439*38fd1498Szrj 		ira_reg_equiv[regno].init_insns
3440*38fd1498Szrj 		  = gen_rtx_INSN_LIST (VOIDmode, insn,
3441*38fd1498Szrj 				       ira_reg_equiv[regno].init_insns);
3442*38fd1498Szrj 
3443*38fd1498Szrj 	      /* Continue normally in case this is a candidate for
3444*38fd1498Szrj 		 replacements.  */
3445*38fd1498Szrj 	    }
3446*38fd1498Szrj 
3447*38fd1498Szrj 	  if (!optimize)
3448*38fd1498Szrj 	    continue;
3449*38fd1498Szrj 
3450*38fd1498Szrj 	  /* We only handle the case of a pseudo register being set
3451*38fd1498Szrj 	     once, or always to the same value.  */
3452*38fd1498Szrj 	  /* ??? The mn10200 port breaks if we add equivalences for
3453*38fd1498Szrj 	     values that need an ADDRESS_REGS register and set them equivalent
3454*38fd1498Szrj 	     to a MEM of a pseudo.  The actual problem is in the over-conservative
3455*38fd1498Szrj 	     handling of INPADDR_ADDRESS / INPUT_ADDRESS / INPUT triples in
3456*38fd1498Szrj 	     calculate_needs, but we traditionally work around this problem
3457*38fd1498Szrj 	     here by rejecting equivalences when the destination is in a register
3458*38fd1498Szrj 	     that's likely spilled.  This is fragile, of course, since the
3459*38fd1498Szrj 	     preferred class of a pseudo depends on all instructions that set
3460*38fd1498Szrj 	     or use it.  */
3461*38fd1498Szrj 
3462*38fd1498Szrj 	  if (!REG_P (dest)
3463*38fd1498Szrj 	      || (regno = REGNO (dest)) < FIRST_PSEUDO_REGISTER
3464*38fd1498Szrj 	      || (reg_equiv[regno].init_insns
3465*38fd1498Szrj 		  && reg_equiv[regno].init_insns->insn () == NULL)
3466*38fd1498Szrj 	      || (targetm.class_likely_spilled_p (reg_preferred_class (regno))
3467*38fd1498Szrj 		  && MEM_P (src) && ! reg_equiv[regno].is_arg_equivalence))
3468*38fd1498Szrj 	    {
3469*38fd1498Szrj 	      /* This might be setting a SUBREG of a pseudo, a pseudo that is
3470*38fd1498Szrj 		 also set somewhere else to a constant.  */
3471*38fd1498Szrj 	      note_stores (set, no_equiv, NULL);
3472*38fd1498Szrj 	      continue;
3473*38fd1498Szrj 	    }
3474*38fd1498Szrj 
3475*38fd1498Szrj 	  /* Don't set reg mentioned in a paradoxical subreg
3476*38fd1498Szrj 	     equivalent to a mem.  */
3477*38fd1498Szrj 	  if (MEM_P (src) && reg_equiv[regno].pdx_subregs)
3478*38fd1498Szrj 	    {
3479*38fd1498Szrj 	      note_stores (set, no_equiv, NULL);
3480*38fd1498Szrj 	      continue;
3481*38fd1498Szrj 	    }
3482*38fd1498Szrj 
3483*38fd1498Szrj 	  note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
3484*38fd1498Szrj 
3485*38fd1498Szrj 	  /* cse sometimes generates function invariants, but doesn't put a
3486*38fd1498Szrj 	     REG_EQUAL note on the insn.  Since this note would be redundant,
3487*38fd1498Szrj 	     there's no point creating it earlier than here.  */
3488*38fd1498Szrj 	  if (! note && ! rtx_varies_p (src, 0))
3489*38fd1498Szrj 	    note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
3490*38fd1498Szrj 
3491*38fd1498Szrj 	  /* Don't bother considering a REG_EQUAL note containing an EXPR_LIST
3492*38fd1498Szrj 	     since it represents a function call.  */
3493*38fd1498Szrj 	  if (note && GET_CODE (XEXP (note, 0)) == EXPR_LIST)
3494*38fd1498Szrj 	    note = NULL_RTX;
3495*38fd1498Szrj 
3496*38fd1498Szrj 	  if (DF_REG_DEF_COUNT (regno) != 1)
3497*38fd1498Szrj 	    {
3498*38fd1498Szrj 	      bool equal_p = true;
3499*38fd1498Szrj 	      rtx_insn_list *list;
3500*38fd1498Szrj 
3501*38fd1498Szrj 	      /* If we have already processed this pseudo and determined it
3502*38fd1498Szrj 		 can not have an equivalence, then honor that decision.  */
3503*38fd1498Szrj 	      if (reg_equiv[regno].no_equiv)
3504*38fd1498Szrj 		continue;
3505*38fd1498Szrj 
3506*38fd1498Szrj 	      if (! note
3507*38fd1498Szrj 		  || rtx_varies_p (XEXP (note, 0), 0)
3508*38fd1498Szrj 		  || (reg_equiv[regno].replacement
3509*38fd1498Szrj 		      && ! rtx_equal_p (XEXP (note, 0),
3510*38fd1498Szrj 					reg_equiv[regno].replacement)))
3511*38fd1498Szrj 		{
3512*38fd1498Szrj 		  no_equiv (dest, set, NULL);
3513*38fd1498Szrj 		  continue;
3514*38fd1498Szrj 		}
3515*38fd1498Szrj 
3516*38fd1498Szrj 	      list = reg_equiv[regno].init_insns;
3517*38fd1498Szrj 	      for (; list; list = list->next ())
3518*38fd1498Szrj 		{
3519*38fd1498Szrj 		  rtx note_tmp;
3520*38fd1498Szrj 		  rtx_insn *insn_tmp;
3521*38fd1498Szrj 
3522*38fd1498Szrj 		  insn_tmp = list->insn ();
3523*38fd1498Szrj 		  note_tmp = find_reg_note (insn_tmp, REG_EQUAL, NULL_RTX);
3524*38fd1498Szrj 		  gcc_assert (note_tmp);
3525*38fd1498Szrj 		  if (! rtx_equal_p (XEXP (note, 0), XEXP (note_tmp, 0)))
3526*38fd1498Szrj 		    {
3527*38fd1498Szrj 		      equal_p = false;
3528*38fd1498Szrj 		      break;
3529*38fd1498Szrj 		    }
3530*38fd1498Szrj 		}
3531*38fd1498Szrj 
3532*38fd1498Szrj 	      if (! equal_p)
3533*38fd1498Szrj 		{
3534*38fd1498Szrj 		  no_equiv (dest, set, NULL);
3535*38fd1498Szrj 		  continue;
3536*38fd1498Szrj 		}
3537*38fd1498Szrj 	    }
3538*38fd1498Szrj 
3539*38fd1498Szrj 	  /* Record this insn as initializing this register.  */
3540*38fd1498Szrj 	  reg_equiv[regno].init_insns
3541*38fd1498Szrj 	    = gen_rtx_INSN_LIST (VOIDmode, insn, reg_equiv[regno].init_insns);
3542*38fd1498Szrj 
3543*38fd1498Szrj 	  /* If this register is known to be equal to a constant, record that
3544*38fd1498Szrj 	     it is always equivalent to the constant.
3545*38fd1498Szrj 	     Note that it is possible to have a register use before
3546*38fd1498Szrj 	     the def in loops (see gcc.c-torture/execute/pr79286.c)
3547*38fd1498Szrj 	     where the reg is undefined on first use.  If the def insn
3548*38fd1498Szrj 	     won't trap we can use it as an equivalence, effectively
3549*38fd1498Szrj 	     choosing the "undefined" value for the reg to be the
3550*38fd1498Szrj 	     same as the value set by the def.  */
3551*38fd1498Szrj 	  if (DF_REG_DEF_COUNT (regno) == 1
3552*38fd1498Szrj 	      && note
3553*38fd1498Szrj 	      && !rtx_varies_p (XEXP (note, 0), 0)
3554*38fd1498Szrj 	      && (!may_trap_or_fault_p (XEXP (note, 0))
3555*38fd1498Szrj 		  || def_dominates_uses (regno)))
3556*38fd1498Szrj 	    {
3557*38fd1498Szrj 	      rtx note_value = XEXP (note, 0);
3558*38fd1498Szrj 	      remove_note (insn, note);
3559*38fd1498Szrj 	      set_unique_reg_note (insn, REG_EQUIV, note_value);
3560*38fd1498Szrj 	    }
3561*38fd1498Szrj 
3562*38fd1498Szrj 	  /* If this insn introduces a "constant" register, decrease the priority
3563*38fd1498Szrj 	     of that register.  Record this insn if the register is only used once
3564*38fd1498Szrj 	     more and the equivalence value is the same as our source.
3565*38fd1498Szrj 
3566*38fd1498Szrj 	     The latter condition is checked for two reasons:  First, it is an
3567*38fd1498Szrj 	     indication that it may be more efficient to actually emit the insn
3568*38fd1498Szrj 	     as written (if no registers are available, reload will substitute
3569*38fd1498Szrj 	     the equivalence).  Secondly, it avoids problems with any registers
3570*38fd1498Szrj 	     dying in this insn whose death notes would be missed.
3571*38fd1498Szrj 
3572*38fd1498Szrj 	     If we don't have a REG_EQUIV note, see if this insn is loading
3573*38fd1498Szrj 	     a register used only in one basic block from a MEM.  If so, and the
3574*38fd1498Szrj 	     MEM remains unchanged for the life of the register, add a REG_EQUIV
3575*38fd1498Szrj 	     note.  */
3576*38fd1498Szrj 	  note = find_reg_note (insn, REG_EQUIV, NULL_RTX);
3577*38fd1498Szrj 
3578*38fd1498Szrj 	  rtx replacement = NULL_RTX;
3579*38fd1498Szrj 	  if (note)
3580*38fd1498Szrj 	    replacement = XEXP (note, 0);
3581*38fd1498Szrj 	  else if (REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
3582*38fd1498Szrj 		   && MEM_P (SET_SRC (set)))
3583*38fd1498Szrj 	    {
3584*38fd1498Szrj 	      enum valid_equiv validity;
3585*38fd1498Szrj 	      validity = validate_equiv_mem (insn, dest, SET_SRC (set));
3586*38fd1498Szrj 	      if (validity != valid_none)
3587*38fd1498Szrj 		{
3588*38fd1498Szrj 		  replacement = copy_rtx (SET_SRC (set));
3589*38fd1498Szrj 		  if (validity == valid_reload)
3590*38fd1498Szrj 		    note = set_unique_reg_note (insn, REG_EQUIV, replacement);
3591*38fd1498Szrj 		}
3592*38fd1498Szrj 	    }
3593*38fd1498Szrj 
3594*38fd1498Szrj 	  /* If we haven't done so, record for reload that this is an
3595*38fd1498Szrj 	     equivalencing insn.  */
3596*38fd1498Szrj 	  if (note && !reg_equiv[regno].is_arg_equivalence)
3597*38fd1498Szrj 	    ira_reg_equiv[regno].init_insns
3598*38fd1498Szrj 	      = gen_rtx_INSN_LIST (VOIDmode, insn,
3599*38fd1498Szrj 				   ira_reg_equiv[regno].init_insns);
3600*38fd1498Szrj 
3601*38fd1498Szrj 	  if (replacement)
3602*38fd1498Szrj 	    {
3603*38fd1498Szrj 	      reg_equiv[regno].replacement = replacement;
3604*38fd1498Szrj 	      reg_equiv[regno].src_p = &SET_SRC (set);
3605*38fd1498Szrj 	      reg_equiv[regno].loop_depth = (short) loop_depth;
3606*38fd1498Szrj 
3607*38fd1498Szrj 	      /* Don't mess with things live during setjmp.  */
3608*38fd1498Szrj 	      if (optimize && !bitmap_bit_p (setjmp_crosses, regno))
3609*38fd1498Szrj 		{
3610*38fd1498Szrj 		  /* If the register is referenced exactly twice, meaning it is
3611*38fd1498Szrj 		     set once and used once, indicate that the reference may be
3612*38fd1498Szrj 		     replaced by the equivalence we computed above.  Do this
3613*38fd1498Szrj 		     even if the register is only used in one block so that
3614*38fd1498Szrj 		     dependencies can be handled where the last register is
3615*38fd1498Szrj 		     used in a different block (i.e. HIGH / LO_SUM sequences)
3616*38fd1498Szrj 		     and to reduce the number of registers alive across
3617*38fd1498Szrj 		     calls.  */
3618*38fd1498Szrj 
3619*38fd1498Szrj 		  if (REG_N_REFS (regno) == 2
3620*38fd1498Szrj 		      && (rtx_equal_p (replacement, src)
3621*38fd1498Szrj 			  || ! equiv_init_varies_p (src))
3622*38fd1498Szrj 		      && NONJUMP_INSN_P (insn)
3623*38fd1498Szrj 		      && equiv_init_movable_p (PATTERN (insn), regno))
3624*38fd1498Szrj 		    reg_equiv[regno].replace = 1;
3625*38fd1498Szrj 		}
3626*38fd1498Szrj 	    }
3627*38fd1498Szrj 	}
3628*38fd1498Szrj     }
3629*38fd1498Szrj }
3630*38fd1498Szrj 
3631*38fd1498Szrj /* For insns that set a MEM to the contents of a REG that is only used
3632*38fd1498Szrj    in a single basic block, see if the register is always equivalent
3633*38fd1498Szrj    to that memory location and if moving the store from INSN to the
3634*38fd1498Szrj    insn that sets REG is safe.  If so, put a REG_EQUIV note on the
3635*38fd1498Szrj    initializing insn.  */
3636*38fd1498Szrj static void
add_store_equivs(void)3637*38fd1498Szrj add_store_equivs (void)
3638*38fd1498Szrj {
3639*38fd1498Szrj   auto_bitmap seen_insns;
3640*38fd1498Szrj 
3641*38fd1498Szrj   for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
3642*38fd1498Szrj     {
3643*38fd1498Szrj       rtx set, src, dest;
3644*38fd1498Szrj       unsigned regno;
3645*38fd1498Szrj       rtx_insn *init_insn;
3646*38fd1498Szrj 
3647*38fd1498Szrj       bitmap_set_bit (seen_insns, INSN_UID (insn));
3648*38fd1498Szrj 
3649*38fd1498Szrj       if (! INSN_P (insn))
3650*38fd1498Szrj 	continue;
3651*38fd1498Szrj 
3652*38fd1498Szrj       set = single_set (insn);
3653*38fd1498Szrj       if (! set)
3654*38fd1498Szrj 	continue;
3655*38fd1498Szrj 
3656*38fd1498Szrj       dest = SET_DEST (set);
3657*38fd1498Szrj       src = SET_SRC (set);
3658*38fd1498Szrj 
3659*38fd1498Szrj       /* Don't add a REG_EQUIV note if the insn already has one.  The existing
3660*38fd1498Szrj 	 REG_EQUIV is likely more useful than the one we are adding.  */
3661*38fd1498Szrj       if (MEM_P (dest) && REG_P (src)
3662*38fd1498Szrj 	  && (regno = REGNO (src)) >= FIRST_PSEUDO_REGISTER
3663*38fd1498Szrj 	  && REG_BASIC_BLOCK (regno) >= NUM_FIXED_BLOCKS
3664*38fd1498Szrj 	  && DF_REG_DEF_COUNT (regno) == 1
3665*38fd1498Szrj 	  && ! reg_equiv[regno].pdx_subregs
3666*38fd1498Szrj 	  && reg_equiv[regno].init_insns != NULL
3667*38fd1498Szrj 	  && (init_insn = reg_equiv[regno].init_insns->insn ()) != 0
3668*38fd1498Szrj 	  && bitmap_bit_p (seen_insns, INSN_UID (init_insn))
3669*38fd1498Szrj 	  && ! find_reg_note (init_insn, REG_EQUIV, NULL_RTX)
3670*38fd1498Szrj 	  && validate_equiv_mem (init_insn, src, dest) == valid_reload
3671*38fd1498Szrj 	  && ! memref_used_between_p (dest, init_insn, insn)
3672*38fd1498Szrj 	  /* Attaching a REG_EQUIV note will fail if INIT_INSN has
3673*38fd1498Szrj 	     multiple sets.  */
3674*38fd1498Szrj 	  && set_unique_reg_note (init_insn, REG_EQUIV, copy_rtx (dest)))
3675*38fd1498Szrj 	{
3676*38fd1498Szrj 	  /* This insn makes the equivalence, not the one initializing
3677*38fd1498Szrj 	     the register.  */
3678*38fd1498Szrj 	  ira_reg_equiv[regno].init_insns
3679*38fd1498Szrj 	    = gen_rtx_INSN_LIST (VOIDmode, insn, NULL_RTX);
3680*38fd1498Szrj 	  df_notes_rescan (init_insn);
3681*38fd1498Szrj 	  if (dump_file)
3682*38fd1498Szrj 	    fprintf (dump_file,
3683*38fd1498Szrj 		     "Adding REG_EQUIV to insn %d for source of insn %d\n",
3684*38fd1498Szrj 		     INSN_UID (init_insn),
3685*38fd1498Szrj 		     INSN_UID (insn));
3686*38fd1498Szrj 	}
3687*38fd1498Szrj     }
3688*38fd1498Szrj }
3689*38fd1498Szrj 
3690*38fd1498Szrj /* Scan all regs killed in an insn to see if any of them are registers
3691*38fd1498Szrj    only used that once.  If so, see if we can replace the reference
3692*38fd1498Szrj    with the equivalent form.  If we can, delete the initializing
3693*38fd1498Szrj    reference and this register will go away.  If we can't replace the
3694*38fd1498Szrj    reference, and the initializing reference is within the same loop
3695*38fd1498Szrj    (or in an inner loop), then move the register initialization just
3696*38fd1498Szrj    before the use, so that they are in the same basic block.  */
3697*38fd1498Szrj static void
combine_and_move_insns(void)3698*38fd1498Szrj combine_and_move_insns (void)
3699*38fd1498Szrj {
3700*38fd1498Szrj   auto_bitmap cleared_regs;
3701*38fd1498Szrj   int max = max_reg_num ();
3702*38fd1498Szrj 
3703*38fd1498Szrj   for (int regno = FIRST_PSEUDO_REGISTER; regno < max; regno++)
3704*38fd1498Szrj     {
3705*38fd1498Szrj       if (!reg_equiv[regno].replace)
3706*38fd1498Szrj 	continue;
3707*38fd1498Szrj 
3708*38fd1498Szrj       rtx_insn *use_insn = 0;
3709*38fd1498Szrj       for (df_ref use = DF_REG_USE_CHAIN (regno);
3710*38fd1498Szrj 	   use;
3711*38fd1498Szrj 	   use = DF_REF_NEXT_REG (use))
3712*38fd1498Szrj 	if (DF_REF_INSN_INFO (use))
3713*38fd1498Szrj 	  {
3714*38fd1498Szrj 	    if (DEBUG_INSN_P (DF_REF_INSN (use)))
3715*38fd1498Szrj 	      continue;
3716*38fd1498Szrj 	    gcc_assert (!use_insn);
3717*38fd1498Szrj 	    use_insn = DF_REF_INSN (use);
3718*38fd1498Szrj 	  }
3719*38fd1498Szrj       gcc_assert (use_insn);
3720*38fd1498Szrj 
3721*38fd1498Szrj       /* Don't substitute into jumps.  indirect_jump_optimize does
3722*38fd1498Szrj 	 this for anything we are prepared to handle.  */
3723*38fd1498Szrj       if (JUMP_P (use_insn))
3724*38fd1498Szrj 	continue;
3725*38fd1498Szrj 
3726*38fd1498Szrj       /* Also don't substitute into a conditional trap insn -- it can become
3727*38fd1498Szrj 	 an unconditional trap, and that is a flow control insn.  */
3728*38fd1498Szrj       if (GET_CODE (PATTERN (use_insn)) == TRAP_IF)
3729*38fd1498Szrj 	continue;
3730*38fd1498Szrj 
3731*38fd1498Szrj       df_ref def = DF_REG_DEF_CHAIN (regno);
3732*38fd1498Szrj       gcc_assert (DF_REG_DEF_COUNT (regno) == 1 && DF_REF_INSN_INFO (def));
3733*38fd1498Szrj       rtx_insn *def_insn = DF_REF_INSN (def);
3734*38fd1498Szrj 
3735*38fd1498Szrj       /* We may not move instructions that can throw, since that
3736*38fd1498Szrj 	 changes basic block boundaries and we are not prepared to
3737*38fd1498Szrj 	 adjust the CFG to match.  */
3738*38fd1498Szrj       if (can_throw_internal (def_insn))
3739*38fd1498Szrj 	continue;
3740*38fd1498Szrj 
3741*38fd1498Szrj       basic_block use_bb = BLOCK_FOR_INSN (use_insn);
3742*38fd1498Szrj       basic_block def_bb = BLOCK_FOR_INSN (def_insn);
3743*38fd1498Szrj       if (bb_loop_depth (use_bb) > bb_loop_depth (def_bb))
3744*38fd1498Szrj 	continue;
3745*38fd1498Szrj 
3746*38fd1498Szrj       if (asm_noperands (PATTERN (def_insn)) < 0
3747*38fd1498Szrj 	  && validate_replace_rtx (regno_reg_rtx[regno],
3748*38fd1498Szrj 				   *reg_equiv[regno].src_p, use_insn))
3749*38fd1498Szrj 	{
3750*38fd1498Szrj 	  rtx link;
3751*38fd1498Szrj 	  /* Append the REG_DEAD notes from def_insn.  */
3752*38fd1498Szrj 	  for (rtx *p = &REG_NOTES (def_insn); (link = *p) != 0; )
3753*38fd1498Szrj 	    {
3754*38fd1498Szrj 	      if (REG_NOTE_KIND (XEXP (link, 0)) == REG_DEAD)
3755*38fd1498Szrj 		{
3756*38fd1498Szrj 		  *p = XEXP (link, 1);
3757*38fd1498Szrj 		  XEXP (link, 1) = REG_NOTES (use_insn);
3758*38fd1498Szrj 		  REG_NOTES (use_insn) = link;
3759*38fd1498Szrj 		}
3760*38fd1498Szrj 	      else
3761*38fd1498Szrj 		p = &XEXP (link, 1);
3762*38fd1498Szrj 	    }
3763*38fd1498Szrj 
3764*38fd1498Szrj 	  remove_death (regno, use_insn);
3765*38fd1498Szrj 	  SET_REG_N_REFS (regno, 0);
3766*38fd1498Szrj 	  REG_FREQ (regno) = 0;
3767*38fd1498Szrj 	  df_ref use;
3768*38fd1498Szrj 	  FOR_EACH_INSN_USE (use, def_insn)
3769*38fd1498Szrj 	    {
3770*38fd1498Szrj 	      unsigned int use_regno = DF_REF_REGNO (use);
3771*38fd1498Szrj 	      if (!HARD_REGISTER_NUM_P (use_regno))
3772*38fd1498Szrj 		reg_equiv[use_regno].replace = 0;
3773*38fd1498Szrj 	    }
3774*38fd1498Szrj 
3775*38fd1498Szrj 	  delete_insn (def_insn);
3776*38fd1498Szrj 
3777*38fd1498Szrj 	  reg_equiv[regno].init_insns = NULL;
3778*38fd1498Szrj 	  ira_reg_equiv[regno].init_insns = NULL;
3779*38fd1498Szrj 	  bitmap_set_bit (cleared_regs, regno);
3780*38fd1498Szrj 	}
3781*38fd1498Szrj 
3782*38fd1498Szrj       /* Move the initialization of the register to just before
3783*38fd1498Szrj 	 USE_INSN.  Update the flow information.  */
3784*38fd1498Szrj       else if (prev_nondebug_insn (use_insn) != def_insn)
3785*38fd1498Szrj 	{
3786*38fd1498Szrj 	  rtx_insn *new_insn;
3787*38fd1498Szrj 
3788*38fd1498Szrj 	  new_insn = emit_insn_before (PATTERN (def_insn), use_insn);
3789*38fd1498Szrj 	  REG_NOTES (new_insn) = REG_NOTES (def_insn);
3790*38fd1498Szrj 	  REG_NOTES (def_insn) = 0;
3791*38fd1498Szrj 	  /* Rescan it to process the notes.  */
3792*38fd1498Szrj 	  df_insn_rescan (new_insn);
3793*38fd1498Szrj 
3794*38fd1498Szrj 	  /* Make sure this insn is recognized before reload begins,
3795*38fd1498Szrj 	     otherwise eliminate_regs_in_insn will die.  */
3796*38fd1498Szrj 	  INSN_CODE (new_insn) = INSN_CODE (def_insn);
3797*38fd1498Szrj 
3798*38fd1498Szrj 	  delete_insn (def_insn);
3799*38fd1498Szrj 
3800*38fd1498Szrj 	  XEXP (reg_equiv[regno].init_insns, 0) = new_insn;
3801*38fd1498Szrj 
3802*38fd1498Szrj 	  REG_BASIC_BLOCK (regno) = use_bb->index;
3803*38fd1498Szrj 	  REG_N_CALLS_CROSSED (regno) = 0;
3804*38fd1498Szrj 
3805*38fd1498Szrj 	  if (use_insn == BB_HEAD (use_bb))
3806*38fd1498Szrj 	    BB_HEAD (use_bb) = new_insn;
3807*38fd1498Szrj 
3808*38fd1498Szrj 	  /* We know regno dies in use_insn, but inside a loop
3809*38fd1498Szrj 	     REG_DEAD notes might be missing when def_insn was in
3810*38fd1498Szrj 	     another basic block.  However, when we move def_insn into
3811*38fd1498Szrj 	     this bb we'll definitely get a REG_DEAD note and reload
3812*38fd1498Szrj 	     will see the death.  It's possible that update_equiv_regs
3813*38fd1498Szrj 	     set up an equivalence referencing regno for a reg set by
3814*38fd1498Szrj 	     use_insn, when regno was seen as non-local.  Now that
3815*38fd1498Szrj 	     regno is local to this block, and dies, such an
3816*38fd1498Szrj 	     equivalence is invalid.  */
3817*38fd1498Szrj 	  if (find_reg_note (use_insn, REG_EQUIV, regno_reg_rtx[regno]))
3818*38fd1498Szrj 	    {
3819*38fd1498Szrj 	      rtx set = single_set (use_insn);
3820*38fd1498Szrj 	      if (set && REG_P (SET_DEST (set)))
3821*38fd1498Szrj 		no_equiv (SET_DEST (set), set, NULL);
3822*38fd1498Szrj 	    }
3823*38fd1498Szrj 
3824*38fd1498Szrj 	  ira_reg_equiv[regno].init_insns
3825*38fd1498Szrj 	    = gen_rtx_INSN_LIST (VOIDmode, new_insn, NULL_RTX);
3826*38fd1498Szrj 	  bitmap_set_bit (cleared_regs, regno);
3827*38fd1498Szrj 	}
3828*38fd1498Szrj     }
3829*38fd1498Szrj 
3830*38fd1498Szrj   if (!bitmap_empty_p (cleared_regs))
3831*38fd1498Szrj     {
3832*38fd1498Szrj       basic_block bb;
3833*38fd1498Szrj 
3834*38fd1498Szrj       FOR_EACH_BB_FN (bb, cfun)
3835*38fd1498Szrj 	{
3836*38fd1498Szrj 	  bitmap_and_compl_into (DF_LR_IN (bb), cleared_regs);
3837*38fd1498Szrj 	  bitmap_and_compl_into (DF_LR_OUT (bb), cleared_regs);
3838*38fd1498Szrj 	  if (!df_live)
3839*38fd1498Szrj 	    continue;
3840*38fd1498Szrj 	  bitmap_and_compl_into (DF_LIVE_IN (bb), cleared_regs);
3841*38fd1498Szrj 	  bitmap_and_compl_into (DF_LIVE_OUT (bb), cleared_regs);
3842*38fd1498Szrj 	}
3843*38fd1498Szrj 
3844*38fd1498Szrj       /* Last pass - adjust debug insns referencing cleared regs.  */
3845*38fd1498Szrj       if (MAY_HAVE_DEBUG_BIND_INSNS)
3846*38fd1498Szrj 	for (rtx_insn *insn = get_insns (); insn; insn = NEXT_INSN (insn))
3847*38fd1498Szrj 	  if (DEBUG_BIND_INSN_P (insn))
3848*38fd1498Szrj 	    {
3849*38fd1498Szrj 	      rtx old_loc = INSN_VAR_LOCATION_LOC (insn);
3850*38fd1498Szrj 	      INSN_VAR_LOCATION_LOC (insn)
3851*38fd1498Szrj 		= simplify_replace_fn_rtx (old_loc, NULL_RTX,
3852*38fd1498Szrj 					   adjust_cleared_regs,
3853*38fd1498Szrj 					   (void *) cleared_regs);
3854*38fd1498Szrj 	      if (old_loc != INSN_VAR_LOCATION_LOC (insn))
3855*38fd1498Szrj 		df_insn_rescan (insn);
3856*38fd1498Szrj 	    }
3857*38fd1498Szrj     }
3858*38fd1498Szrj }
3859*38fd1498Szrj 
3860*38fd1498Szrj /* A pass over indirect jumps, converting simple cases to direct jumps.
3861*38fd1498Szrj    Combine does this optimization too, but only within a basic block.  */
3862*38fd1498Szrj static void
indirect_jump_optimize(void)3863*38fd1498Szrj indirect_jump_optimize (void)
3864*38fd1498Szrj {
3865*38fd1498Szrj   basic_block bb;
3866*38fd1498Szrj   bool rebuild_p = false;
3867*38fd1498Szrj 
3868*38fd1498Szrj   FOR_EACH_BB_REVERSE_FN (bb, cfun)
3869*38fd1498Szrj     {
3870*38fd1498Szrj       rtx_insn *insn = BB_END (bb);
3871*38fd1498Szrj       if (!JUMP_P (insn)
3872*38fd1498Szrj 	  || find_reg_note (insn, REG_NON_LOCAL_GOTO, NULL_RTX))
3873*38fd1498Szrj 	continue;
3874*38fd1498Szrj 
3875*38fd1498Szrj       rtx x = pc_set (insn);
3876*38fd1498Szrj       if (!x || !REG_P (SET_SRC (x)))
3877*38fd1498Szrj 	continue;
3878*38fd1498Szrj 
3879*38fd1498Szrj       int regno = REGNO (SET_SRC (x));
3880*38fd1498Szrj       if (DF_REG_DEF_COUNT (regno) == 1)
3881*38fd1498Szrj 	{
3882*38fd1498Szrj 	  df_ref def = DF_REG_DEF_CHAIN (regno);
3883*38fd1498Szrj 	  if (!DF_REF_IS_ARTIFICIAL (def))
3884*38fd1498Szrj 	    {
3885*38fd1498Szrj 	      rtx_insn *def_insn = DF_REF_INSN (def);
3886*38fd1498Szrj 	      rtx lab = NULL_RTX;
3887*38fd1498Szrj 	      rtx set = single_set (def_insn);
3888*38fd1498Szrj 	      if (set && GET_CODE (SET_SRC (set)) == LABEL_REF)
3889*38fd1498Szrj 		lab = SET_SRC (set);
3890*38fd1498Szrj 	      else
3891*38fd1498Szrj 		{
3892*38fd1498Szrj 		  rtx eqnote = find_reg_note (def_insn, REG_EQUAL, NULL_RTX);
3893*38fd1498Szrj 		  if (eqnote && GET_CODE (XEXP (eqnote, 0)) == LABEL_REF)
3894*38fd1498Szrj 		    lab = XEXP (eqnote, 0);
3895*38fd1498Szrj 		}
3896*38fd1498Szrj 	      if (lab && validate_replace_rtx (SET_SRC (x), lab, insn))
3897*38fd1498Szrj 		rebuild_p = true;
3898*38fd1498Szrj 	    }
3899*38fd1498Szrj 	}
3900*38fd1498Szrj     }
3901*38fd1498Szrj 
3902*38fd1498Szrj   if (rebuild_p)
3903*38fd1498Szrj     {
3904*38fd1498Szrj       timevar_push (TV_JUMP);
3905*38fd1498Szrj       rebuild_jump_labels (get_insns ());
3906*38fd1498Szrj       if (purge_all_dead_edges ())
3907*38fd1498Szrj 	delete_unreachable_blocks ();
3908*38fd1498Szrj       timevar_pop (TV_JUMP);
3909*38fd1498Szrj     }
3910*38fd1498Szrj }
3911*38fd1498Szrj 
3912*38fd1498Szrj /* Set up fields memory, constant, and invariant from init_insns in
3913*38fd1498Szrj    the structures of array ira_reg_equiv.  */
3914*38fd1498Szrj static void
setup_reg_equiv(void)3915*38fd1498Szrj setup_reg_equiv (void)
3916*38fd1498Szrj {
3917*38fd1498Szrj   int i;
3918*38fd1498Szrj   rtx_insn_list *elem, *prev_elem, *next_elem;
3919*38fd1498Szrj   rtx_insn *insn;
3920*38fd1498Szrj   rtx set, x;
3921*38fd1498Szrj 
3922*38fd1498Szrj   for (i = FIRST_PSEUDO_REGISTER; i < ira_reg_equiv_len; i++)
3923*38fd1498Szrj     for (prev_elem = NULL, elem = ira_reg_equiv[i].init_insns;
3924*38fd1498Szrj 	 elem;
3925*38fd1498Szrj 	 prev_elem = elem, elem = next_elem)
3926*38fd1498Szrj       {
3927*38fd1498Szrj 	next_elem = elem->next ();
3928*38fd1498Szrj 	insn = elem->insn ();
3929*38fd1498Szrj 	set = single_set (insn);
3930*38fd1498Szrj 
3931*38fd1498Szrj 	/* Init insns can set up equivalence when the reg is a destination or
3932*38fd1498Szrj 	   a source (in this case the destination is memory).  */
3933*38fd1498Szrj 	if (set != 0 && (REG_P (SET_DEST (set)) || REG_P (SET_SRC (set))))
3934*38fd1498Szrj 	  {
3935*38fd1498Szrj 	    if ((x = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != NULL)
3936*38fd1498Szrj 	      {
3937*38fd1498Szrj 		x = XEXP (x, 0);
3938*38fd1498Szrj 		if (REG_P (SET_DEST (set))
3939*38fd1498Szrj 		    && REGNO (SET_DEST (set)) == (unsigned int) i
3940*38fd1498Szrj 		    && ! rtx_equal_p (SET_SRC (set), x) && MEM_P (x))
3941*38fd1498Szrj 		  {
3942*38fd1498Szrj 		    /* This insn reporting the equivalence but
3943*38fd1498Szrj 		       actually not setting it.  Remove it from the
3944*38fd1498Szrj 		       list.  */
3945*38fd1498Szrj 		    if (prev_elem == NULL)
3946*38fd1498Szrj 		      ira_reg_equiv[i].init_insns = next_elem;
3947*38fd1498Szrj 		    else
3948*38fd1498Szrj 		      XEXP (prev_elem, 1) = next_elem;
3949*38fd1498Szrj 		    elem = prev_elem;
3950*38fd1498Szrj 		  }
3951*38fd1498Szrj 	      }
3952*38fd1498Szrj 	    else if (REG_P (SET_DEST (set))
3953*38fd1498Szrj 		     && REGNO (SET_DEST (set)) == (unsigned int) i)
3954*38fd1498Szrj 	      x = SET_SRC (set);
3955*38fd1498Szrj 	    else
3956*38fd1498Szrj 	      {
3957*38fd1498Szrj 		gcc_assert (REG_P (SET_SRC (set))
3958*38fd1498Szrj 			    && REGNO (SET_SRC (set)) == (unsigned int) i);
3959*38fd1498Szrj 		x = SET_DEST (set);
3960*38fd1498Szrj 	      }
3961*38fd1498Szrj 	    if (! function_invariant_p (x)
3962*38fd1498Szrj 		|| ! flag_pic
3963*38fd1498Szrj 		/* A function invariant is often CONSTANT_P but may
3964*38fd1498Szrj 		   include a register.  We promise to only pass
3965*38fd1498Szrj 		   CONSTANT_P objects to LEGITIMATE_PIC_OPERAND_P.  */
3966*38fd1498Szrj 		|| (CONSTANT_P (x) && LEGITIMATE_PIC_OPERAND_P (x)))
3967*38fd1498Szrj 	      {
3968*38fd1498Szrj 		/* It can happen that a REG_EQUIV note contains a MEM
3969*38fd1498Szrj 		   that is not a legitimate memory operand.  As later
3970*38fd1498Szrj 		   stages of reload assume that all addresses found in
3971*38fd1498Szrj 		   the lra_regno_equiv_* arrays were originally
3972*38fd1498Szrj 		   legitimate, we ignore such REG_EQUIV notes.  */
3973*38fd1498Szrj 		if (memory_operand (x, VOIDmode))
3974*38fd1498Szrj 		  {
3975*38fd1498Szrj 		    ira_reg_equiv[i].defined_p = true;
3976*38fd1498Szrj 		    ira_reg_equiv[i].memory = x;
3977*38fd1498Szrj 		    continue;
3978*38fd1498Szrj 		  }
3979*38fd1498Szrj 		else if (function_invariant_p (x))
3980*38fd1498Szrj 		  {
3981*38fd1498Szrj 		    machine_mode mode;
3982*38fd1498Szrj 
3983*38fd1498Szrj 		    mode = GET_MODE (SET_DEST (set));
3984*38fd1498Szrj 		    if (GET_CODE (x) == PLUS
3985*38fd1498Szrj 			|| x == frame_pointer_rtx || x == arg_pointer_rtx)
3986*38fd1498Szrj 		      /* This is PLUS of frame pointer and a constant,
3987*38fd1498Szrj 			 or fp, or argp.  */
3988*38fd1498Szrj 		      ira_reg_equiv[i].invariant = x;
3989*38fd1498Szrj 		    else if (targetm.legitimate_constant_p (mode, x))
3990*38fd1498Szrj 		      ira_reg_equiv[i].constant = x;
3991*38fd1498Szrj 		    else
3992*38fd1498Szrj 		      {
3993*38fd1498Szrj 			ira_reg_equiv[i].memory = force_const_mem (mode, x);
3994*38fd1498Szrj 			if (ira_reg_equiv[i].memory == NULL_RTX)
3995*38fd1498Szrj 			  {
3996*38fd1498Szrj 			    ira_reg_equiv[i].defined_p = false;
3997*38fd1498Szrj 			    ira_reg_equiv[i].init_insns = NULL;
3998*38fd1498Szrj 			    break;
3999*38fd1498Szrj 			  }
4000*38fd1498Szrj 		      }
4001*38fd1498Szrj 		    ira_reg_equiv[i].defined_p = true;
4002*38fd1498Szrj 		    continue;
4003*38fd1498Szrj 		  }
4004*38fd1498Szrj 	      }
4005*38fd1498Szrj 	  }
4006*38fd1498Szrj 	ira_reg_equiv[i].defined_p = false;
4007*38fd1498Szrj 	ira_reg_equiv[i].init_insns = NULL;
4008*38fd1498Szrj 	break;
4009*38fd1498Szrj       }
4010*38fd1498Szrj }
4011*38fd1498Szrj 
4012*38fd1498Szrj 
4013*38fd1498Szrj 
4014*38fd1498Szrj /* Print chain C to FILE.  */
4015*38fd1498Szrj static void
print_insn_chain(FILE * file,struct insn_chain * c)4016*38fd1498Szrj print_insn_chain (FILE *file, struct insn_chain *c)
4017*38fd1498Szrj {
4018*38fd1498Szrj   fprintf (file, "insn=%d, ", INSN_UID (c->insn));
4019*38fd1498Szrj   bitmap_print (file, &c->live_throughout, "live_throughout: ", ", ");
4020*38fd1498Szrj   bitmap_print (file, &c->dead_or_set, "dead_or_set: ", "\n");
4021*38fd1498Szrj }
4022*38fd1498Szrj 
4023*38fd1498Szrj 
4024*38fd1498Szrj /* Print all reload_insn_chains to FILE.  */
4025*38fd1498Szrj static void
print_insn_chains(FILE * file)4026*38fd1498Szrj print_insn_chains (FILE *file)
4027*38fd1498Szrj {
4028*38fd1498Szrj   struct insn_chain *c;
4029*38fd1498Szrj   for (c = reload_insn_chain; c ; c = c->next)
4030*38fd1498Szrj     print_insn_chain (file, c);
4031*38fd1498Szrj }
4032*38fd1498Szrj 
4033*38fd1498Szrj /* Return true if pseudo REGNO should be added to set live_throughout
4034*38fd1498Szrj    or dead_or_set of the insn chains for reload consideration.  */
4035*38fd1498Szrj static bool
pseudo_for_reload_consideration_p(int regno)4036*38fd1498Szrj pseudo_for_reload_consideration_p (int regno)
4037*38fd1498Szrj {
4038*38fd1498Szrj   /* Consider spilled pseudos too for IRA because they still have a
4039*38fd1498Szrj      chance to get hard-registers in the reload when IRA is used.  */
4040*38fd1498Szrj   return (reg_renumber[regno] >= 0 || ira_conflicts_p);
4041*38fd1498Szrj }
4042*38fd1498Szrj 
4043*38fd1498Szrj /* Return true if we can track the individual bytes of subreg X.
4044*38fd1498Szrj    When returning true, set *OUTER_SIZE to the number of bytes in
4045*38fd1498Szrj    X itself, *INNER_SIZE to the number of bytes in the inner register
4046*38fd1498Szrj    and *START to the offset of the first byte.  */
4047*38fd1498Szrj static bool
get_subreg_tracking_sizes(rtx x,HOST_WIDE_INT * outer_size,HOST_WIDE_INT * inner_size,HOST_WIDE_INT * start)4048*38fd1498Szrj get_subreg_tracking_sizes (rtx x, HOST_WIDE_INT *outer_size,
4049*38fd1498Szrj 			   HOST_WIDE_INT *inner_size, HOST_WIDE_INT *start)
4050*38fd1498Szrj {
4051*38fd1498Szrj   rtx reg = regno_reg_rtx[REGNO (SUBREG_REG (x))];
4052*38fd1498Szrj   return (GET_MODE_SIZE (GET_MODE (x)).is_constant (outer_size)
4053*38fd1498Szrj 	  && GET_MODE_SIZE (GET_MODE (reg)).is_constant (inner_size)
4054*38fd1498Szrj 	  && SUBREG_BYTE (x).is_constant (start));
4055*38fd1498Szrj }
4056*38fd1498Szrj 
4057*38fd1498Szrj /* Init LIVE_SUBREGS[ALLOCNUM] and LIVE_SUBREGS_USED[ALLOCNUM] for
4058*38fd1498Szrj    a register with SIZE bytes, making the register live if INIT_VALUE.  */
4059*38fd1498Szrj static void
init_live_subregs(bool init_value,sbitmap * live_subregs,bitmap live_subregs_used,int allocnum,int size)4060*38fd1498Szrj init_live_subregs (bool init_value, sbitmap *live_subregs,
4061*38fd1498Szrj 		   bitmap live_subregs_used, int allocnum, int size)
4062*38fd1498Szrj {
4063*38fd1498Szrj   gcc_assert (size > 0);
4064*38fd1498Szrj 
4065*38fd1498Szrj   /* Been there, done that.  */
4066*38fd1498Szrj   if (bitmap_bit_p (live_subregs_used, allocnum))
4067*38fd1498Szrj     return;
4068*38fd1498Szrj 
4069*38fd1498Szrj   /* Create a new one.  */
4070*38fd1498Szrj   if (live_subregs[allocnum] == NULL)
4071*38fd1498Szrj     live_subregs[allocnum] = sbitmap_alloc (size);
4072*38fd1498Szrj 
4073*38fd1498Szrj   /* If the entire reg was live before blasting into subregs, we need
4074*38fd1498Szrj      to init all of the subregs to ones else init to 0.  */
4075*38fd1498Szrj   if (init_value)
4076*38fd1498Szrj     bitmap_ones (live_subregs[allocnum]);
4077*38fd1498Szrj   else
4078*38fd1498Szrj     bitmap_clear (live_subregs[allocnum]);
4079*38fd1498Szrj 
4080*38fd1498Szrj   bitmap_set_bit (live_subregs_used, allocnum);
4081*38fd1498Szrj }
4082*38fd1498Szrj 
4083*38fd1498Szrj /* Walk the insns of the current function and build reload_insn_chain,
4084*38fd1498Szrj    and record register life information.  */
4085*38fd1498Szrj static void
build_insn_chain(void)4086*38fd1498Szrj build_insn_chain (void)
4087*38fd1498Szrj {
4088*38fd1498Szrj   unsigned int i;
4089*38fd1498Szrj   struct insn_chain **p = &reload_insn_chain;
4090*38fd1498Szrj   basic_block bb;
4091*38fd1498Szrj   struct insn_chain *c = NULL;
4092*38fd1498Szrj   struct insn_chain *next = NULL;
4093*38fd1498Szrj   auto_bitmap live_relevant_regs;
4094*38fd1498Szrj   auto_bitmap elim_regset;
4095*38fd1498Szrj   /* live_subregs is a vector used to keep accurate information about
4096*38fd1498Szrj      which hardregs are live in multiword pseudos.  live_subregs and
4097*38fd1498Szrj      live_subregs_used are indexed by pseudo number.  The live_subreg
4098*38fd1498Szrj      entry for a particular pseudo is only used if the corresponding
4099*38fd1498Szrj      element is non zero in live_subregs_used.  The sbitmap size of
4100*38fd1498Szrj      live_subreg[allocno] is number of bytes that the pseudo can
4101*38fd1498Szrj      occupy.  */
4102*38fd1498Szrj   sbitmap *live_subregs = XCNEWVEC (sbitmap, max_regno);
4103*38fd1498Szrj   auto_bitmap live_subregs_used;
4104*38fd1498Szrj 
4105*38fd1498Szrj   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
4106*38fd1498Szrj     if (TEST_HARD_REG_BIT (eliminable_regset, i))
4107*38fd1498Szrj       bitmap_set_bit (elim_regset, i);
4108*38fd1498Szrj   FOR_EACH_BB_REVERSE_FN (bb, cfun)
4109*38fd1498Szrj     {
4110*38fd1498Szrj       bitmap_iterator bi;
4111*38fd1498Szrj       rtx_insn *insn;
4112*38fd1498Szrj 
4113*38fd1498Szrj       CLEAR_REG_SET (live_relevant_regs);
4114*38fd1498Szrj       bitmap_clear (live_subregs_used);
4115*38fd1498Szrj 
4116*38fd1498Szrj       EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb), 0, i, bi)
4117*38fd1498Szrj 	{
4118*38fd1498Szrj 	  if (i >= FIRST_PSEUDO_REGISTER)
4119*38fd1498Szrj 	    break;
4120*38fd1498Szrj 	  bitmap_set_bit (live_relevant_regs, i);
4121*38fd1498Szrj 	}
4122*38fd1498Szrj 
4123*38fd1498Szrj       EXECUTE_IF_SET_IN_BITMAP (df_get_live_out (bb),
4124*38fd1498Szrj 				FIRST_PSEUDO_REGISTER, i, bi)
4125*38fd1498Szrj 	{
4126*38fd1498Szrj 	  if (pseudo_for_reload_consideration_p (i))
4127*38fd1498Szrj 	    bitmap_set_bit (live_relevant_regs, i);
4128*38fd1498Szrj 	}
4129*38fd1498Szrj 
4130*38fd1498Szrj       FOR_BB_INSNS_REVERSE (bb, insn)
4131*38fd1498Szrj 	{
4132*38fd1498Szrj 	  if (!NOTE_P (insn) && !BARRIER_P (insn))
4133*38fd1498Szrj 	    {
4134*38fd1498Szrj 	      struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4135*38fd1498Szrj 	      df_ref def, use;
4136*38fd1498Szrj 
4137*38fd1498Szrj 	      c = new_insn_chain ();
4138*38fd1498Szrj 	      c->next = next;
4139*38fd1498Szrj 	      next = c;
4140*38fd1498Szrj 	      *p = c;
4141*38fd1498Szrj 	      p = &c->prev;
4142*38fd1498Szrj 
4143*38fd1498Szrj 	      c->insn = insn;
4144*38fd1498Szrj 	      c->block = bb->index;
4145*38fd1498Szrj 
4146*38fd1498Szrj 	      if (NONDEBUG_INSN_P (insn))
4147*38fd1498Szrj 		FOR_EACH_INSN_INFO_DEF (def, insn_info)
4148*38fd1498Szrj 		  {
4149*38fd1498Szrj 		    unsigned int regno = DF_REF_REGNO (def);
4150*38fd1498Szrj 
4151*38fd1498Szrj 		    /* Ignore may clobbers because these are generated
4152*38fd1498Szrj 		       from calls. However, every other kind of def is
4153*38fd1498Szrj 		       added to dead_or_set.  */
4154*38fd1498Szrj 		    if (!DF_REF_FLAGS_IS_SET (def, DF_REF_MAY_CLOBBER))
4155*38fd1498Szrj 		      {
4156*38fd1498Szrj 			if (regno < FIRST_PSEUDO_REGISTER)
4157*38fd1498Szrj 			  {
4158*38fd1498Szrj 			    if (!fixed_regs[regno])
4159*38fd1498Szrj 			      bitmap_set_bit (&c->dead_or_set, regno);
4160*38fd1498Szrj 			  }
4161*38fd1498Szrj 			else if (pseudo_for_reload_consideration_p (regno))
4162*38fd1498Szrj 			  bitmap_set_bit (&c->dead_or_set, regno);
4163*38fd1498Szrj 		      }
4164*38fd1498Szrj 
4165*38fd1498Szrj 		    if ((regno < FIRST_PSEUDO_REGISTER
4166*38fd1498Szrj 			 || reg_renumber[regno] >= 0
4167*38fd1498Szrj 			 || ira_conflicts_p)
4168*38fd1498Szrj 			&& (!DF_REF_FLAGS_IS_SET (def, DF_REF_CONDITIONAL)))
4169*38fd1498Szrj 		      {
4170*38fd1498Szrj 			rtx reg = DF_REF_REG (def);
4171*38fd1498Szrj 			HOST_WIDE_INT outer_size, inner_size, start;
4172*38fd1498Szrj 
4173*38fd1498Szrj 			/* We can usually track the liveness of individual
4174*38fd1498Szrj 			   bytes within a subreg.  The only exceptions are
4175*38fd1498Szrj 			   subregs wrapped in ZERO_EXTRACTs and subregs whose
4176*38fd1498Szrj 			   size is not known; in those cases we need to be
4177*38fd1498Szrj 			   conservative and treat the definition as a partial
4178*38fd1498Szrj 			   definition of the full register rather than a full
4179*38fd1498Szrj 			   definition of a specific part of the register.  */
4180*38fd1498Szrj 			if (GET_CODE (reg) == SUBREG
4181*38fd1498Szrj 			    && !DF_REF_FLAGS_IS_SET (def, DF_REF_ZERO_EXTRACT)
4182*38fd1498Szrj 			    && get_subreg_tracking_sizes (reg, &outer_size,
4183*38fd1498Szrj 							  &inner_size, &start))
4184*38fd1498Szrj 			  {
4185*38fd1498Szrj 			    HOST_WIDE_INT last = start + outer_size;
4186*38fd1498Szrj 
4187*38fd1498Szrj 			    init_live_subregs
4188*38fd1498Szrj 			      (bitmap_bit_p (live_relevant_regs, regno),
4189*38fd1498Szrj 			       live_subregs, live_subregs_used, regno,
4190*38fd1498Szrj 			       inner_size);
4191*38fd1498Szrj 
4192*38fd1498Szrj 			    if (!DF_REF_FLAGS_IS_SET
4193*38fd1498Szrj 				(def, DF_REF_STRICT_LOW_PART))
4194*38fd1498Szrj 			      {
4195*38fd1498Szrj 				/* Expand the range to cover entire words.
4196*38fd1498Szrj 				   Bytes added here are "don't care".  */
4197*38fd1498Szrj 				start
4198*38fd1498Szrj 				  = start / UNITS_PER_WORD * UNITS_PER_WORD;
4199*38fd1498Szrj 				last = ((last + UNITS_PER_WORD - 1)
4200*38fd1498Szrj 					/ UNITS_PER_WORD * UNITS_PER_WORD);
4201*38fd1498Szrj 			      }
4202*38fd1498Szrj 
4203*38fd1498Szrj 			    /* Ignore the paradoxical bits.  */
4204*38fd1498Szrj 			    if (last > SBITMAP_SIZE (live_subregs[regno]))
4205*38fd1498Szrj 			      last = SBITMAP_SIZE (live_subregs[regno]);
4206*38fd1498Szrj 
4207*38fd1498Szrj 			    while (start < last)
4208*38fd1498Szrj 			      {
4209*38fd1498Szrj 				bitmap_clear_bit (live_subregs[regno], start);
4210*38fd1498Szrj 				start++;
4211*38fd1498Szrj 			      }
4212*38fd1498Szrj 
4213*38fd1498Szrj 			    if (bitmap_empty_p (live_subregs[regno]))
4214*38fd1498Szrj 			      {
4215*38fd1498Szrj 				bitmap_clear_bit (live_subregs_used, regno);
4216*38fd1498Szrj 				bitmap_clear_bit (live_relevant_regs, regno);
4217*38fd1498Szrj 			      }
4218*38fd1498Szrj 			    else
4219*38fd1498Szrj 			      /* Set live_relevant_regs here because
4220*38fd1498Szrj 				 that bit has to be true to get us to
4221*38fd1498Szrj 				 look at the live_subregs fields.  */
4222*38fd1498Szrj 			      bitmap_set_bit (live_relevant_regs, regno);
4223*38fd1498Szrj 			  }
4224*38fd1498Szrj 			else
4225*38fd1498Szrj 			  {
4226*38fd1498Szrj 			    /* DF_REF_PARTIAL is generated for
4227*38fd1498Szrj 			       subregs, STRICT_LOW_PART, and
4228*38fd1498Szrj 			       ZERO_EXTRACT.  We handle the subreg
4229*38fd1498Szrj 			       case above so here we have to keep from
4230*38fd1498Szrj 			       modeling the def as a killing def.  */
4231*38fd1498Szrj 			    if (!DF_REF_FLAGS_IS_SET (def, DF_REF_PARTIAL))
4232*38fd1498Szrj 			      {
4233*38fd1498Szrj 				bitmap_clear_bit (live_subregs_used, regno);
4234*38fd1498Szrj 				bitmap_clear_bit (live_relevant_regs, regno);
4235*38fd1498Szrj 			      }
4236*38fd1498Szrj 			  }
4237*38fd1498Szrj 		      }
4238*38fd1498Szrj 		  }
4239*38fd1498Szrj 
4240*38fd1498Szrj 	      bitmap_and_compl_into (live_relevant_regs, elim_regset);
4241*38fd1498Szrj 	      bitmap_copy (&c->live_throughout, live_relevant_regs);
4242*38fd1498Szrj 
4243*38fd1498Szrj 	      if (NONDEBUG_INSN_P (insn))
4244*38fd1498Szrj 		FOR_EACH_INSN_INFO_USE (use, insn_info)
4245*38fd1498Szrj 		  {
4246*38fd1498Szrj 		    unsigned int regno = DF_REF_REGNO (use);
4247*38fd1498Szrj 		    rtx reg = DF_REF_REG (use);
4248*38fd1498Szrj 
4249*38fd1498Szrj 		    /* DF_REF_READ_WRITE on a use means that this use
4250*38fd1498Szrj 		       is fabricated from a def that is a partial set
4251*38fd1498Szrj 		       to a multiword reg.  Here, we only model the
4252*38fd1498Szrj 		       subreg case that is not wrapped in ZERO_EXTRACT
4253*38fd1498Szrj 		       precisely so we do not need to look at the
4254*38fd1498Szrj 		       fabricated use.  */
4255*38fd1498Szrj 		    if (DF_REF_FLAGS_IS_SET (use, DF_REF_READ_WRITE)
4256*38fd1498Szrj 			&& !DF_REF_FLAGS_IS_SET (use, DF_REF_ZERO_EXTRACT)
4257*38fd1498Szrj 			&& DF_REF_FLAGS_IS_SET (use, DF_REF_SUBREG))
4258*38fd1498Szrj 		      continue;
4259*38fd1498Szrj 
4260*38fd1498Szrj 		    /* Add the last use of each var to dead_or_set.  */
4261*38fd1498Szrj 		    if (!bitmap_bit_p (live_relevant_regs, regno))
4262*38fd1498Szrj 		      {
4263*38fd1498Szrj 			if (regno < FIRST_PSEUDO_REGISTER)
4264*38fd1498Szrj 			  {
4265*38fd1498Szrj 			    if (!fixed_regs[regno])
4266*38fd1498Szrj 			      bitmap_set_bit (&c->dead_or_set, regno);
4267*38fd1498Szrj 			  }
4268*38fd1498Szrj 			else if (pseudo_for_reload_consideration_p (regno))
4269*38fd1498Szrj 			  bitmap_set_bit (&c->dead_or_set, regno);
4270*38fd1498Szrj 		      }
4271*38fd1498Szrj 
4272*38fd1498Szrj 		    if (regno < FIRST_PSEUDO_REGISTER
4273*38fd1498Szrj 			|| pseudo_for_reload_consideration_p (regno))
4274*38fd1498Szrj 		      {
4275*38fd1498Szrj 			HOST_WIDE_INT outer_size, inner_size, start;
4276*38fd1498Szrj 			if (GET_CODE (reg) == SUBREG
4277*38fd1498Szrj 			    && !DF_REF_FLAGS_IS_SET (use,
4278*38fd1498Szrj 						     DF_REF_SIGN_EXTRACT
4279*38fd1498Szrj 						     | DF_REF_ZERO_EXTRACT)
4280*38fd1498Szrj 			    && get_subreg_tracking_sizes (reg, &outer_size,
4281*38fd1498Szrj 							  &inner_size, &start))
4282*38fd1498Szrj 			  {
4283*38fd1498Szrj 			    HOST_WIDE_INT last = start + outer_size;
4284*38fd1498Szrj 
4285*38fd1498Szrj 			    init_live_subregs
4286*38fd1498Szrj 			      (bitmap_bit_p (live_relevant_regs, regno),
4287*38fd1498Szrj 			       live_subregs, live_subregs_used, regno,
4288*38fd1498Szrj 			       inner_size);
4289*38fd1498Szrj 
4290*38fd1498Szrj 			    /* Ignore the paradoxical bits.  */
4291*38fd1498Szrj 			    if (last > SBITMAP_SIZE (live_subregs[regno]))
4292*38fd1498Szrj 			      last = SBITMAP_SIZE (live_subregs[regno]);
4293*38fd1498Szrj 
4294*38fd1498Szrj 			    while (start < last)
4295*38fd1498Szrj 			      {
4296*38fd1498Szrj 				bitmap_set_bit (live_subregs[regno], start);
4297*38fd1498Szrj 				start++;
4298*38fd1498Szrj 			      }
4299*38fd1498Szrj 			  }
4300*38fd1498Szrj 			else
4301*38fd1498Szrj 			  /* Resetting the live_subregs_used is
4302*38fd1498Szrj 			     effectively saying do not use the subregs
4303*38fd1498Szrj 			     because we are reading the whole
4304*38fd1498Szrj 			     pseudo.  */
4305*38fd1498Szrj 			  bitmap_clear_bit (live_subregs_used, regno);
4306*38fd1498Szrj 			bitmap_set_bit (live_relevant_regs, regno);
4307*38fd1498Szrj 		      }
4308*38fd1498Szrj 		  }
4309*38fd1498Szrj 	    }
4310*38fd1498Szrj 	}
4311*38fd1498Szrj 
4312*38fd1498Szrj       /* FIXME!! The following code is a disaster.  Reload needs to see the
4313*38fd1498Szrj 	 labels and jump tables that are just hanging out in between
4314*38fd1498Szrj 	 the basic blocks.  See pr33676.  */
4315*38fd1498Szrj       insn = BB_HEAD (bb);
4316*38fd1498Szrj 
4317*38fd1498Szrj       /* Skip over the barriers and cruft.  */
4318*38fd1498Szrj       while (insn && (BARRIER_P (insn) || NOTE_P (insn)
4319*38fd1498Szrj 		      || BLOCK_FOR_INSN (insn) == bb))
4320*38fd1498Szrj 	insn = PREV_INSN (insn);
4321*38fd1498Szrj 
4322*38fd1498Szrj       /* While we add anything except barriers and notes, the focus is
4323*38fd1498Szrj 	 to get the labels and jump tables into the
4324*38fd1498Szrj 	 reload_insn_chain.  */
4325*38fd1498Szrj       while (insn)
4326*38fd1498Szrj 	{
4327*38fd1498Szrj 	  if (!NOTE_P (insn) && !BARRIER_P (insn))
4328*38fd1498Szrj 	    {
4329*38fd1498Szrj 	      if (BLOCK_FOR_INSN (insn))
4330*38fd1498Szrj 		break;
4331*38fd1498Szrj 
4332*38fd1498Szrj 	      c = new_insn_chain ();
4333*38fd1498Szrj 	      c->next = next;
4334*38fd1498Szrj 	      next = c;
4335*38fd1498Szrj 	      *p = c;
4336*38fd1498Szrj 	      p = &c->prev;
4337*38fd1498Szrj 
4338*38fd1498Szrj 	      /* The block makes no sense here, but it is what the old
4339*38fd1498Szrj 		 code did.  */
4340*38fd1498Szrj 	      c->block = bb->index;
4341*38fd1498Szrj 	      c->insn = insn;
4342*38fd1498Szrj 	      bitmap_copy (&c->live_throughout, live_relevant_regs);
4343*38fd1498Szrj 	    }
4344*38fd1498Szrj 	  insn = PREV_INSN (insn);
4345*38fd1498Szrj 	}
4346*38fd1498Szrj     }
4347*38fd1498Szrj 
4348*38fd1498Szrj   reload_insn_chain = c;
4349*38fd1498Szrj   *p = NULL;
4350*38fd1498Szrj 
4351*38fd1498Szrj   for (i = 0; i < (unsigned int) max_regno; i++)
4352*38fd1498Szrj     if (live_subregs[i] != NULL)
4353*38fd1498Szrj       sbitmap_free (live_subregs[i]);
4354*38fd1498Szrj   free (live_subregs);
4355*38fd1498Szrj 
4356*38fd1498Szrj   if (dump_file)
4357*38fd1498Szrj     print_insn_chains (dump_file);
4358*38fd1498Szrj }
4359*38fd1498Szrj 
4360*38fd1498Szrj /* Examine the rtx found in *LOC, which is read or written to as determined
4361*38fd1498Szrj    by TYPE.  Return false if we find a reason why an insn containing this
4362*38fd1498Szrj    rtx should not be moved (such as accesses to non-constant memory), true
4363*38fd1498Szrj    otherwise.  */
4364*38fd1498Szrj static bool
rtx_moveable_p(rtx * loc,enum op_type type)4365*38fd1498Szrj rtx_moveable_p (rtx *loc, enum op_type type)
4366*38fd1498Szrj {
4367*38fd1498Szrj   const char *fmt;
4368*38fd1498Szrj   rtx x = *loc;
4369*38fd1498Szrj   enum rtx_code code = GET_CODE (x);
4370*38fd1498Szrj   int i, j;
4371*38fd1498Szrj 
4372*38fd1498Szrj   code = GET_CODE (x);
4373*38fd1498Szrj   switch (code)
4374*38fd1498Szrj     {
4375*38fd1498Szrj     case CONST:
4376*38fd1498Szrj     CASE_CONST_ANY:
4377*38fd1498Szrj     case SYMBOL_REF:
4378*38fd1498Szrj     case LABEL_REF:
4379*38fd1498Szrj       return true;
4380*38fd1498Szrj 
4381*38fd1498Szrj     case PC:
4382*38fd1498Szrj       return type == OP_IN;
4383*38fd1498Szrj 
4384*38fd1498Szrj     case CC0:
4385*38fd1498Szrj       return false;
4386*38fd1498Szrj 
4387*38fd1498Szrj     case REG:
4388*38fd1498Szrj       if (x == frame_pointer_rtx)
4389*38fd1498Szrj 	return true;
4390*38fd1498Szrj       if (HARD_REGISTER_P (x))
4391*38fd1498Szrj 	return false;
4392*38fd1498Szrj 
4393*38fd1498Szrj       return true;
4394*38fd1498Szrj 
4395*38fd1498Szrj     case MEM:
4396*38fd1498Szrj       if (type == OP_IN && MEM_READONLY_P (x))
4397*38fd1498Szrj 	return rtx_moveable_p (&XEXP (x, 0), OP_IN);
4398*38fd1498Szrj       return false;
4399*38fd1498Szrj 
4400*38fd1498Szrj     case SET:
4401*38fd1498Szrj       return (rtx_moveable_p (&SET_SRC (x), OP_IN)
4402*38fd1498Szrj 	      && rtx_moveable_p (&SET_DEST (x), OP_OUT));
4403*38fd1498Szrj 
4404*38fd1498Szrj     case STRICT_LOW_PART:
4405*38fd1498Szrj       return rtx_moveable_p (&XEXP (x, 0), OP_OUT);
4406*38fd1498Szrj 
4407*38fd1498Szrj     case ZERO_EXTRACT:
4408*38fd1498Szrj     case SIGN_EXTRACT:
4409*38fd1498Szrj       return (rtx_moveable_p (&XEXP (x, 0), type)
4410*38fd1498Szrj 	      && rtx_moveable_p (&XEXP (x, 1), OP_IN)
4411*38fd1498Szrj 	      && rtx_moveable_p (&XEXP (x, 2), OP_IN));
4412*38fd1498Szrj 
4413*38fd1498Szrj     case CLOBBER:
4414*38fd1498Szrj       return rtx_moveable_p (&SET_DEST (x), OP_OUT);
4415*38fd1498Szrj 
4416*38fd1498Szrj     case UNSPEC_VOLATILE:
4417*38fd1498Szrj       /* It is a bad idea to consider insns with such rtl
4418*38fd1498Szrj 	 as moveable ones.  The insn scheduler also considers them as barrier
4419*38fd1498Szrj 	 for a reason.  */
4420*38fd1498Szrj       return false;
4421*38fd1498Szrj 
4422*38fd1498Szrj     case ASM_OPERANDS:
4423*38fd1498Szrj       /* The same is true for volatile asm: it has unknown side effects, it
4424*38fd1498Szrj          cannot be moved at will.  */
4425*38fd1498Szrj       if (MEM_VOLATILE_P (x))
4426*38fd1498Szrj 	return false;
4427*38fd1498Szrj 
4428*38fd1498Szrj     default:
4429*38fd1498Szrj       break;
4430*38fd1498Szrj     }
4431*38fd1498Szrj 
4432*38fd1498Szrj   fmt = GET_RTX_FORMAT (code);
4433*38fd1498Szrj   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
4434*38fd1498Szrj     {
4435*38fd1498Szrj       if (fmt[i] == 'e')
4436*38fd1498Szrj 	{
4437*38fd1498Szrj 	  if (!rtx_moveable_p (&XEXP (x, i), type))
4438*38fd1498Szrj 	    return false;
4439*38fd1498Szrj 	}
4440*38fd1498Szrj       else if (fmt[i] == 'E')
4441*38fd1498Szrj 	for (j = XVECLEN (x, i) - 1; j >= 0; j--)
4442*38fd1498Szrj 	  {
4443*38fd1498Szrj 	    if (!rtx_moveable_p (&XVECEXP (x, i, j), type))
4444*38fd1498Szrj 	      return false;
4445*38fd1498Szrj 	  }
4446*38fd1498Szrj     }
4447*38fd1498Szrj   return true;
4448*38fd1498Szrj }
4449*38fd1498Szrj 
4450*38fd1498Szrj /* A wrapper around dominated_by_p, which uses the information in UID_LUID
4451*38fd1498Szrj    to give dominance relationships between two insns I1 and I2.  */
4452*38fd1498Szrj static bool
insn_dominated_by_p(rtx i1,rtx i2,int * uid_luid)4453*38fd1498Szrj insn_dominated_by_p (rtx i1, rtx i2, int *uid_luid)
4454*38fd1498Szrj {
4455*38fd1498Szrj   basic_block bb1 = BLOCK_FOR_INSN (i1);
4456*38fd1498Szrj   basic_block bb2 = BLOCK_FOR_INSN (i2);
4457*38fd1498Szrj 
4458*38fd1498Szrj   if (bb1 == bb2)
4459*38fd1498Szrj     return uid_luid[INSN_UID (i2)] < uid_luid[INSN_UID (i1)];
4460*38fd1498Szrj   return dominated_by_p (CDI_DOMINATORS, bb1, bb2);
4461*38fd1498Szrj }
4462*38fd1498Szrj 
4463*38fd1498Szrj /* Record the range of register numbers added by find_moveable_pseudos.  */
4464*38fd1498Szrj int first_moveable_pseudo, last_moveable_pseudo;
4465*38fd1498Szrj 
4466*38fd1498Szrj /* These two vectors hold data for every register added by
4467*38fd1498Szrj    find_movable_pseudos, with index 0 holding data for the
4468*38fd1498Szrj    first_moveable_pseudo.  */
4469*38fd1498Szrj /* The original home register.  */
4470*38fd1498Szrj static vec<rtx> pseudo_replaced_reg;
4471*38fd1498Szrj 
4472*38fd1498Szrj /* Look for instances where we have an instruction that is known to increase
4473*38fd1498Szrj    register pressure, and whose result is not used immediately.  If it is
4474*38fd1498Szrj    possible to move the instruction downwards to just before its first use,
4475*38fd1498Szrj    split its lifetime into two ranges.  We create a new pseudo to compute the
4476*38fd1498Szrj    value, and emit a move instruction just before the first use.  If, after
4477*38fd1498Szrj    register allocation, the new pseudo remains unallocated, the function
4478*38fd1498Szrj    move_unallocated_pseudos then deletes the move instruction and places
4479*38fd1498Szrj    the computation just before the first use.
4480*38fd1498Szrj 
4481*38fd1498Szrj    Such a move is safe and profitable if all the input registers remain live
4482*38fd1498Szrj    and unchanged between the original computation and its first use.  In such
4483*38fd1498Szrj    a situation, the computation is known to increase register pressure, and
4484*38fd1498Szrj    moving it is known to at least not worsen it.
4485*38fd1498Szrj 
4486*38fd1498Szrj    We restrict moves to only those cases where a register remains unallocated,
4487*38fd1498Szrj    in order to avoid interfering too much with the instruction schedule.  As
4488*38fd1498Szrj    an exception, we may move insns which only modify their input register
4489*38fd1498Szrj    (typically induction variables), as this increases the freedom for our
4490*38fd1498Szrj    intended transformation, and does not limit the second instruction
4491*38fd1498Szrj    scheduler pass.  */
4492*38fd1498Szrj 
4493*38fd1498Szrj static void
find_moveable_pseudos(void)4494*38fd1498Szrj find_moveable_pseudos (void)
4495*38fd1498Szrj {
4496*38fd1498Szrj   unsigned i;
4497*38fd1498Szrj   int max_regs = max_reg_num ();
4498*38fd1498Szrj   int max_uid = get_max_uid ();
4499*38fd1498Szrj   basic_block bb;
4500*38fd1498Szrj   int *uid_luid = XNEWVEC (int, max_uid);
4501*38fd1498Szrj   rtx_insn **closest_uses = XNEWVEC (rtx_insn *, max_regs);
4502*38fd1498Szrj   /* A set of registers which are live but not modified throughout a block.  */
4503*38fd1498Szrj   bitmap_head *bb_transp_live = XNEWVEC (bitmap_head,
4504*38fd1498Szrj 					 last_basic_block_for_fn (cfun));
4505*38fd1498Szrj   /* A set of registers which only exist in a given basic block.  */
4506*38fd1498Szrj   bitmap_head *bb_local = XNEWVEC (bitmap_head,
4507*38fd1498Szrj 				   last_basic_block_for_fn (cfun));
4508*38fd1498Szrj   /* A set of registers which are set once, in an instruction that can be
4509*38fd1498Szrj      moved freely downwards, but are otherwise transparent to a block.  */
4510*38fd1498Szrj   bitmap_head *bb_moveable_reg_sets = XNEWVEC (bitmap_head,
4511*38fd1498Szrj 					       last_basic_block_for_fn (cfun));
4512*38fd1498Szrj   auto_bitmap live, used, set, interesting, unusable_as_input;
4513*38fd1498Szrj   bitmap_iterator bi;
4514*38fd1498Szrj 
4515*38fd1498Szrj   first_moveable_pseudo = max_regs;
4516*38fd1498Szrj   pseudo_replaced_reg.release ();
4517*38fd1498Szrj   pseudo_replaced_reg.safe_grow_cleared (max_regs);
4518*38fd1498Szrj 
4519*38fd1498Szrj   df_analyze ();
4520*38fd1498Szrj   calculate_dominance_info (CDI_DOMINATORS);
4521*38fd1498Szrj 
4522*38fd1498Szrj   i = 0;
4523*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
4524*38fd1498Szrj     {
4525*38fd1498Szrj       rtx_insn *insn;
4526*38fd1498Szrj       bitmap transp = bb_transp_live + bb->index;
4527*38fd1498Szrj       bitmap moveable = bb_moveable_reg_sets + bb->index;
4528*38fd1498Szrj       bitmap local = bb_local + bb->index;
4529*38fd1498Szrj 
4530*38fd1498Szrj       bitmap_initialize (local, 0);
4531*38fd1498Szrj       bitmap_initialize (transp, 0);
4532*38fd1498Szrj       bitmap_initialize (moveable, 0);
4533*38fd1498Szrj       bitmap_copy (live, df_get_live_out (bb));
4534*38fd1498Szrj       bitmap_and_into (live, df_get_live_in (bb));
4535*38fd1498Szrj       bitmap_copy (transp, live);
4536*38fd1498Szrj       bitmap_clear (moveable);
4537*38fd1498Szrj       bitmap_clear (live);
4538*38fd1498Szrj       bitmap_clear (used);
4539*38fd1498Szrj       bitmap_clear (set);
4540*38fd1498Szrj       FOR_BB_INSNS (bb, insn)
4541*38fd1498Szrj 	if (NONDEBUG_INSN_P (insn))
4542*38fd1498Szrj 	  {
4543*38fd1498Szrj 	    df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4544*38fd1498Szrj 	    df_ref def, use;
4545*38fd1498Szrj 
4546*38fd1498Szrj 	    uid_luid[INSN_UID (insn)] = i++;
4547*38fd1498Szrj 
4548*38fd1498Szrj 	    def = df_single_def (insn_info);
4549*38fd1498Szrj 	    use = df_single_use (insn_info);
4550*38fd1498Szrj 	    if (use
4551*38fd1498Szrj 		&& def
4552*38fd1498Szrj 		&& DF_REF_REGNO (use) == DF_REF_REGNO (def)
4553*38fd1498Szrj 		&& !bitmap_bit_p (set, DF_REF_REGNO (use))
4554*38fd1498Szrj 		&& rtx_moveable_p (&PATTERN (insn), OP_IN))
4555*38fd1498Szrj 	      {
4556*38fd1498Szrj 		unsigned regno = DF_REF_REGNO (use);
4557*38fd1498Szrj 		bitmap_set_bit (moveable, regno);
4558*38fd1498Szrj 		bitmap_set_bit (set, regno);
4559*38fd1498Szrj 		bitmap_set_bit (used, regno);
4560*38fd1498Szrj 		bitmap_clear_bit (transp, regno);
4561*38fd1498Szrj 		continue;
4562*38fd1498Szrj 	      }
4563*38fd1498Szrj 	    FOR_EACH_INSN_INFO_USE (use, insn_info)
4564*38fd1498Szrj 	      {
4565*38fd1498Szrj 		unsigned regno = DF_REF_REGNO (use);
4566*38fd1498Szrj 		bitmap_set_bit (used, regno);
4567*38fd1498Szrj 		if (bitmap_clear_bit (moveable, regno))
4568*38fd1498Szrj 		  bitmap_clear_bit (transp, regno);
4569*38fd1498Szrj 	      }
4570*38fd1498Szrj 
4571*38fd1498Szrj 	    FOR_EACH_INSN_INFO_DEF (def, insn_info)
4572*38fd1498Szrj 	      {
4573*38fd1498Szrj 		unsigned regno = DF_REF_REGNO (def);
4574*38fd1498Szrj 		bitmap_set_bit (set, regno);
4575*38fd1498Szrj 		bitmap_clear_bit (transp, regno);
4576*38fd1498Szrj 		bitmap_clear_bit (moveable, regno);
4577*38fd1498Szrj 	      }
4578*38fd1498Szrj 	  }
4579*38fd1498Szrj     }
4580*38fd1498Szrj 
4581*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
4582*38fd1498Szrj     {
4583*38fd1498Szrj       bitmap local = bb_local + bb->index;
4584*38fd1498Szrj       rtx_insn *insn;
4585*38fd1498Szrj 
4586*38fd1498Szrj       FOR_BB_INSNS (bb, insn)
4587*38fd1498Szrj 	if (NONDEBUG_INSN_P (insn))
4588*38fd1498Szrj 	  {
4589*38fd1498Szrj 	    df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
4590*38fd1498Szrj 	    rtx_insn *def_insn;
4591*38fd1498Szrj 	    rtx closest_use, note;
4592*38fd1498Szrj 	    df_ref def, use;
4593*38fd1498Szrj 	    unsigned regno;
4594*38fd1498Szrj 	    bool all_dominated, all_local;
4595*38fd1498Szrj 	    machine_mode mode;
4596*38fd1498Szrj 
4597*38fd1498Szrj 	    def = df_single_def (insn_info);
4598*38fd1498Szrj 	    /* There must be exactly one def in this insn.  */
4599*38fd1498Szrj 	    if (!def || !single_set (insn))
4600*38fd1498Szrj 	      continue;
4601*38fd1498Szrj 	    /* This must be the only definition of the reg.  We also limit
4602*38fd1498Szrj 	       which modes we deal with so that we can assume we can generate
4603*38fd1498Szrj 	       move instructions.  */
4604*38fd1498Szrj 	    regno = DF_REF_REGNO (def);
4605*38fd1498Szrj 	    mode = GET_MODE (DF_REF_REG (def));
4606*38fd1498Szrj 	    if (DF_REG_DEF_COUNT (regno) != 1
4607*38fd1498Szrj 		|| !DF_REF_INSN_INFO (def)
4608*38fd1498Szrj 		|| HARD_REGISTER_NUM_P (regno)
4609*38fd1498Szrj 		|| DF_REG_EQ_USE_COUNT (regno) > 0
4610*38fd1498Szrj 		|| (!INTEGRAL_MODE_P (mode) && !FLOAT_MODE_P (mode)))
4611*38fd1498Szrj 	      continue;
4612*38fd1498Szrj 	    def_insn = DF_REF_INSN (def);
4613*38fd1498Szrj 
4614*38fd1498Szrj 	    for (note = REG_NOTES (def_insn); note; note = XEXP (note, 1))
4615*38fd1498Szrj 	      if (REG_NOTE_KIND (note) == REG_EQUIV && MEM_P (XEXP (note, 0)))
4616*38fd1498Szrj 		break;
4617*38fd1498Szrj 
4618*38fd1498Szrj 	    if (note)
4619*38fd1498Szrj 	      {
4620*38fd1498Szrj 		if (dump_file)
4621*38fd1498Szrj 		  fprintf (dump_file, "Ignoring reg %d, has equiv memory\n",
4622*38fd1498Szrj 			   regno);
4623*38fd1498Szrj 		bitmap_set_bit (unusable_as_input, regno);
4624*38fd1498Szrj 		continue;
4625*38fd1498Szrj 	      }
4626*38fd1498Szrj 
4627*38fd1498Szrj 	    use = DF_REG_USE_CHAIN (regno);
4628*38fd1498Szrj 	    all_dominated = true;
4629*38fd1498Szrj 	    all_local = true;
4630*38fd1498Szrj 	    closest_use = NULL_RTX;
4631*38fd1498Szrj 	    for (; use; use = DF_REF_NEXT_REG (use))
4632*38fd1498Szrj 	      {
4633*38fd1498Szrj 		rtx_insn *insn;
4634*38fd1498Szrj 		if (!DF_REF_INSN_INFO (use))
4635*38fd1498Szrj 		  {
4636*38fd1498Szrj 		    all_dominated = false;
4637*38fd1498Szrj 		    all_local = false;
4638*38fd1498Szrj 		    break;
4639*38fd1498Szrj 		  }
4640*38fd1498Szrj 		insn = DF_REF_INSN (use);
4641*38fd1498Szrj 		if (DEBUG_INSN_P (insn))
4642*38fd1498Szrj 		  continue;
4643*38fd1498Szrj 		if (BLOCK_FOR_INSN (insn) != BLOCK_FOR_INSN (def_insn))
4644*38fd1498Szrj 		  all_local = false;
4645*38fd1498Szrj 		if (!insn_dominated_by_p (insn, def_insn, uid_luid))
4646*38fd1498Szrj 		  all_dominated = false;
4647*38fd1498Szrj 		if (closest_use != insn && closest_use != const0_rtx)
4648*38fd1498Szrj 		  {
4649*38fd1498Szrj 		    if (closest_use == NULL_RTX)
4650*38fd1498Szrj 		      closest_use = insn;
4651*38fd1498Szrj 		    else if (insn_dominated_by_p (closest_use, insn, uid_luid))
4652*38fd1498Szrj 		      closest_use = insn;
4653*38fd1498Szrj 		    else if (!insn_dominated_by_p (insn, closest_use, uid_luid))
4654*38fd1498Szrj 		      closest_use = const0_rtx;
4655*38fd1498Szrj 		  }
4656*38fd1498Szrj 	      }
4657*38fd1498Szrj 	    if (!all_dominated)
4658*38fd1498Szrj 	      {
4659*38fd1498Szrj 		if (dump_file)
4660*38fd1498Szrj 		  fprintf (dump_file, "Reg %d not all uses dominated by set\n",
4661*38fd1498Szrj 			   regno);
4662*38fd1498Szrj 		continue;
4663*38fd1498Szrj 	      }
4664*38fd1498Szrj 	    if (all_local)
4665*38fd1498Szrj 	      bitmap_set_bit (local, regno);
4666*38fd1498Szrj 	    if (closest_use == const0_rtx || closest_use == NULL
4667*38fd1498Szrj 		|| next_nonnote_nondebug_insn (def_insn) == closest_use)
4668*38fd1498Szrj 	      {
4669*38fd1498Szrj 		if (dump_file)
4670*38fd1498Szrj 		  fprintf (dump_file, "Reg %d uninteresting%s\n", regno,
4671*38fd1498Szrj 			   closest_use == const0_rtx || closest_use == NULL
4672*38fd1498Szrj 			   ? " (no unique first use)" : "");
4673*38fd1498Szrj 		continue;
4674*38fd1498Szrj 	      }
4675*38fd1498Szrj 	    if (HAVE_cc0 && reg_referenced_p (cc0_rtx, PATTERN (closest_use)))
4676*38fd1498Szrj 	      {
4677*38fd1498Szrj 		if (dump_file)
4678*38fd1498Szrj 		  fprintf (dump_file, "Reg %d: closest user uses cc0\n",
4679*38fd1498Szrj 			   regno);
4680*38fd1498Szrj 		continue;
4681*38fd1498Szrj 	      }
4682*38fd1498Szrj 
4683*38fd1498Szrj 	    bitmap_set_bit (interesting, regno);
4684*38fd1498Szrj 	    /* If we get here, we know closest_use is a non-NULL insn
4685*38fd1498Szrj 	       (as opposed to const_0_rtx).  */
4686*38fd1498Szrj 	    closest_uses[regno] = as_a <rtx_insn *> (closest_use);
4687*38fd1498Szrj 
4688*38fd1498Szrj 	    if (dump_file && (all_local || all_dominated))
4689*38fd1498Szrj 	      {
4690*38fd1498Szrj 		fprintf (dump_file, "Reg %u:", regno);
4691*38fd1498Szrj 		if (all_local)
4692*38fd1498Szrj 		  fprintf (dump_file, " local to bb %d", bb->index);
4693*38fd1498Szrj 		if (all_dominated)
4694*38fd1498Szrj 		  fprintf (dump_file, " def dominates all uses");
4695*38fd1498Szrj 		if (closest_use != const0_rtx)
4696*38fd1498Szrj 		  fprintf (dump_file, " has unique first use");
4697*38fd1498Szrj 		fputs ("\n", dump_file);
4698*38fd1498Szrj 	      }
4699*38fd1498Szrj 	  }
4700*38fd1498Szrj     }
4701*38fd1498Szrj 
4702*38fd1498Szrj   EXECUTE_IF_SET_IN_BITMAP (interesting, 0, i, bi)
4703*38fd1498Szrj     {
4704*38fd1498Szrj       df_ref def = DF_REG_DEF_CHAIN (i);
4705*38fd1498Szrj       rtx_insn *def_insn = DF_REF_INSN (def);
4706*38fd1498Szrj       basic_block def_block = BLOCK_FOR_INSN (def_insn);
4707*38fd1498Szrj       bitmap def_bb_local = bb_local + def_block->index;
4708*38fd1498Szrj       bitmap def_bb_moveable = bb_moveable_reg_sets + def_block->index;
4709*38fd1498Szrj       bitmap def_bb_transp = bb_transp_live + def_block->index;
4710*38fd1498Szrj       bool local_to_bb_p = bitmap_bit_p (def_bb_local, i);
4711*38fd1498Szrj       rtx_insn *use_insn = closest_uses[i];
4712*38fd1498Szrj       df_ref use;
4713*38fd1498Szrj       bool all_ok = true;
4714*38fd1498Szrj       bool all_transp = true;
4715*38fd1498Szrj 
4716*38fd1498Szrj       if (!REG_P (DF_REF_REG (def)))
4717*38fd1498Szrj 	continue;
4718*38fd1498Szrj 
4719*38fd1498Szrj       if (!local_to_bb_p)
4720*38fd1498Szrj 	{
4721*38fd1498Szrj 	  if (dump_file)
4722*38fd1498Szrj 	    fprintf (dump_file, "Reg %u not local to one basic block\n",
4723*38fd1498Szrj 		     i);
4724*38fd1498Szrj 	  continue;
4725*38fd1498Szrj 	}
4726*38fd1498Szrj       if (reg_equiv_init (i) != NULL_RTX)
4727*38fd1498Szrj 	{
4728*38fd1498Szrj 	  if (dump_file)
4729*38fd1498Szrj 	    fprintf (dump_file, "Ignoring reg %u with equiv init insn\n",
4730*38fd1498Szrj 		     i);
4731*38fd1498Szrj 	  continue;
4732*38fd1498Szrj 	}
4733*38fd1498Szrj       if (!rtx_moveable_p (&PATTERN (def_insn), OP_IN))
4734*38fd1498Szrj 	{
4735*38fd1498Szrj 	  if (dump_file)
4736*38fd1498Szrj 	    fprintf (dump_file, "Found def insn %d for %d to be not moveable\n",
4737*38fd1498Szrj 		     INSN_UID (def_insn), i);
4738*38fd1498Szrj 	  continue;
4739*38fd1498Szrj 	}
4740*38fd1498Szrj       if (dump_file)
4741*38fd1498Szrj 	fprintf (dump_file, "Examining insn %d, def for %d\n",
4742*38fd1498Szrj 		 INSN_UID (def_insn), i);
4743*38fd1498Szrj       FOR_EACH_INSN_USE (use, def_insn)
4744*38fd1498Szrj 	{
4745*38fd1498Szrj 	  unsigned regno = DF_REF_REGNO (use);
4746*38fd1498Szrj 	  if (bitmap_bit_p (unusable_as_input, regno))
4747*38fd1498Szrj 	    {
4748*38fd1498Szrj 	      all_ok = false;
4749*38fd1498Szrj 	      if (dump_file)
4750*38fd1498Szrj 		fprintf (dump_file, "  found unusable input reg %u.\n", regno);
4751*38fd1498Szrj 	      break;
4752*38fd1498Szrj 	    }
4753*38fd1498Szrj 	  if (!bitmap_bit_p (def_bb_transp, regno))
4754*38fd1498Szrj 	    {
4755*38fd1498Szrj 	      if (bitmap_bit_p (def_bb_moveable, regno)
4756*38fd1498Szrj 		  && !control_flow_insn_p (use_insn)
4757*38fd1498Szrj 		  && (!HAVE_cc0 || !sets_cc0_p (use_insn)))
4758*38fd1498Szrj 		{
4759*38fd1498Szrj 		  if (modified_between_p (DF_REF_REG (use), def_insn, use_insn))
4760*38fd1498Szrj 		    {
4761*38fd1498Szrj 		      rtx_insn *x = NEXT_INSN (def_insn);
4762*38fd1498Szrj 		      while (!modified_in_p (DF_REF_REG (use), x))
4763*38fd1498Szrj 			{
4764*38fd1498Szrj 			  gcc_assert (x != use_insn);
4765*38fd1498Szrj 			  x = NEXT_INSN (x);
4766*38fd1498Szrj 			}
4767*38fd1498Szrj 		      if (dump_file)
4768*38fd1498Szrj 			fprintf (dump_file, "  input reg %u modified but insn %d moveable\n",
4769*38fd1498Szrj 				 regno, INSN_UID (x));
4770*38fd1498Szrj 		      emit_insn_after (PATTERN (x), use_insn);
4771*38fd1498Szrj 		      set_insn_deleted (x);
4772*38fd1498Szrj 		    }
4773*38fd1498Szrj 		  else
4774*38fd1498Szrj 		    {
4775*38fd1498Szrj 		      if (dump_file)
4776*38fd1498Szrj 			fprintf (dump_file, "  input reg %u modified between def and use\n",
4777*38fd1498Szrj 				 regno);
4778*38fd1498Szrj 		      all_transp = false;
4779*38fd1498Szrj 		    }
4780*38fd1498Szrj 		}
4781*38fd1498Szrj 	      else
4782*38fd1498Szrj 		all_transp = false;
4783*38fd1498Szrj 	    }
4784*38fd1498Szrj 	}
4785*38fd1498Szrj       if (!all_ok)
4786*38fd1498Szrj 	continue;
4787*38fd1498Szrj       if (!dbg_cnt (ira_move))
4788*38fd1498Szrj 	break;
4789*38fd1498Szrj       if (dump_file)
4790*38fd1498Szrj 	fprintf (dump_file, "  all ok%s\n", all_transp ? " and transp" : "");
4791*38fd1498Szrj 
4792*38fd1498Szrj       if (all_transp)
4793*38fd1498Szrj 	{
4794*38fd1498Szrj 	  rtx def_reg = DF_REF_REG (def);
4795*38fd1498Szrj 	  rtx newreg = ira_create_new_reg (def_reg);
4796*38fd1498Szrj 	  if (validate_change (def_insn, DF_REF_REAL_LOC (def), newreg, 0))
4797*38fd1498Szrj 	    {
4798*38fd1498Szrj 	      unsigned nregno = REGNO (newreg);
4799*38fd1498Szrj 	      emit_insn_before (gen_move_insn (def_reg, newreg), use_insn);
4800*38fd1498Szrj 	      nregno -= max_regs;
4801*38fd1498Szrj 	      pseudo_replaced_reg[nregno] = def_reg;
4802*38fd1498Szrj 	    }
4803*38fd1498Szrj 	}
4804*38fd1498Szrj     }
4805*38fd1498Szrj 
4806*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
4807*38fd1498Szrj     {
4808*38fd1498Szrj       bitmap_clear (bb_local + bb->index);
4809*38fd1498Szrj       bitmap_clear (bb_transp_live + bb->index);
4810*38fd1498Szrj       bitmap_clear (bb_moveable_reg_sets + bb->index);
4811*38fd1498Szrj     }
4812*38fd1498Szrj   free (uid_luid);
4813*38fd1498Szrj   free (closest_uses);
4814*38fd1498Szrj   free (bb_local);
4815*38fd1498Szrj   free (bb_transp_live);
4816*38fd1498Szrj   free (bb_moveable_reg_sets);
4817*38fd1498Szrj 
4818*38fd1498Szrj   last_moveable_pseudo = max_reg_num ();
4819*38fd1498Szrj 
4820*38fd1498Szrj   fix_reg_equiv_init ();
4821*38fd1498Szrj   expand_reg_info ();
4822*38fd1498Szrj   regstat_free_n_sets_and_refs ();
4823*38fd1498Szrj   regstat_free_ri ();
4824*38fd1498Szrj   regstat_init_n_sets_and_refs ();
4825*38fd1498Szrj   regstat_compute_ri ();
4826*38fd1498Szrj   free_dominance_info (CDI_DOMINATORS);
4827*38fd1498Szrj }
4828*38fd1498Szrj 
4829*38fd1498Szrj /* If SET pattern SET is an assignment from a hard register to a pseudo which
4830*38fd1498Szrj    is live at CALL_DOM (if non-NULL, otherwise this check is omitted), return
4831*38fd1498Szrj    the destination.  Otherwise return NULL.  */
4832*38fd1498Szrj 
4833*38fd1498Szrj static rtx
interesting_dest_for_shprep_1(rtx set,basic_block call_dom)4834*38fd1498Szrj interesting_dest_for_shprep_1 (rtx set, basic_block call_dom)
4835*38fd1498Szrj {
4836*38fd1498Szrj   rtx src = SET_SRC (set);
4837*38fd1498Szrj   rtx dest = SET_DEST (set);
4838*38fd1498Szrj   if (!REG_P (src) || !HARD_REGISTER_P (src)
4839*38fd1498Szrj       || !REG_P (dest) || HARD_REGISTER_P (dest)
4840*38fd1498Szrj       || (call_dom && !bitmap_bit_p (df_get_live_in (call_dom), REGNO (dest))))
4841*38fd1498Szrj     return NULL;
4842*38fd1498Szrj   return dest;
4843*38fd1498Szrj }
4844*38fd1498Szrj 
4845*38fd1498Szrj /* If insn is interesting for parameter range-splitting shrink-wrapping
4846*38fd1498Szrj    preparation, i.e. it is a single set from a hard register to a pseudo, which
4847*38fd1498Szrj    is live at CALL_DOM (if non-NULL, otherwise this check is omitted), or a
4848*38fd1498Szrj    parallel statement with only one such statement, return the destination.
4849*38fd1498Szrj    Otherwise return NULL.  */
4850*38fd1498Szrj 
4851*38fd1498Szrj static rtx
interesting_dest_for_shprep(rtx_insn * insn,basic_block call_dom)4852*38fd1498Szrj interesting_dest_for_shprep (rtx_insn *insn, basic_block call_dom)
4853*38fd1498Szrj {
4854*38fd1498Szrj   if (!INSN_P (insn))
4855*38fd1498Szrj     return NULL;
4856*38fd1498Szrj   rtx pat = PATTERN (insn);
4857*38fd1498Szrj   if (GET_CODE (pat) == SET)
4858*38fd1498Szrj     return interesting_dest_for_shprep_1 (pat, call_dom);
4859*38fd1498Szrj 
4860*38fd1498Szrj   if (GET_CODE (pat) != PARALLEL)
4861*38fd1498Szrj     return NULL;
4862*38fd1498Szrj   rtx ret = NULL;
4863*38fd1498Szrj   for (int i = 0; i < XVECLEN (pat, 0); i++)
4864*38fd1498Szrj     {
4865*38fd1498Szrj       rtx sub = XVECEXP (pat, 0, i);
4866*38fd1498Szrj       if (GET_CODE (sub) == USE || GET_CODE (sub) == CLOBBER)
4867*38fd1498Szrj 	continue;
4868*38fd1498Szrj       if (GET_CODE (sub) != SET
4869*38fd1498Szrj 	  || side_effects_p (sub))
4870*38fd1498Szrj 	return NULL;
4871*38fd1498Szrj       rtx dest = interesting_dest_for_shprep_1 (sub, call_dom);
4872*38fd1498Szrj       if (dest && ret)
4873*38fd1498Szrj 	return NULL;
4874*38fd1498Szrj       if (dest)
4875*38fd1498Szrj 	ret = dest;
4876*38fd1498Szrj     }
4877*38fd1498Szrj   return ret;
4878*38fd1498Szrj }
4879*38fd1498Szrj 
4880*38fd1498Szrj /* Split live ranges of pseudos that are loaded from hard registers in the
4881*38fd1498Szrj    first BB in a BB that dominates all non-sibling call if such a BB can be
4882*38fd1498Szrj    found and is not in a loop.  Return true if the function has made any
4883*38fd1498Szrj    changes.  */
4884*38fd1498Szrj 
4885*38fd1498Szrj static bool
split_live_ranges_for_shrink_wrap(void)4886*38fd1498Szrj split_live_ranges_for_shrink_wrap (void)
4887*38fd1498Szrj {
4888*38fd1498Szrj   basic_block bb, call_dom = NULL;
4889*38fd1498Szrj   basic_block first = single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun));
4890*38fd1498Szrj   rtx_insn *insn, *last_interesting_insn = NULL;
4891*38fd1498Szrj   auto_bitmap need_new, reachable;
4892*38fd1498Szrj   vec<basic_block> queue;
4893*38fd1498Szrj 
4894*38fd1498Szrj   if (!SHRINK_WRAPPING_ENABLED)
4895*38fd1498Szrj     return false;
4896*38fd1498Szrj 
4897*38fd1498Szrj   queue.create (n_basic_blocks_for_fn (cfun));
4898*38fd1498Szrj 
4899*38fd1498Szrj   FOR_EACH_BB_FN (bb, cfun)
4900*38fd1498Szrj     FOR_BB_INSNS (bb, insn)
4901*38fd1498Szrj       if (CALL_P (insn) && !SIBLING_CALL_P (insn))
4902*38fd1498Szrj 	{
4903*38fd1498Szrj 	  if (bb == first)
4904*38fd1498Szrj 	    {
4905*38fd1498Szrj 	      queue.release ();
4906*38fd1498Szrj 	      return false;
4907*38fd1498Szrj 	    }
4908*38fd1498Szrj 
4909*38fd1498Szrj 	  bitmap_set_bit (need_new, bb->index);
4910*38fd1498Szrj 	  bitmap_set_bit (reachable, bb->index);
4911*38fd1498Szrj 	  queue.quick_push (bb);
4912*38fd1498Szrj 	  break;
4913*38fd1498Szrj 	}
4914*38fd1498Szrj 
4915*38fd1498Szrj   if (queue.is_empty ())
4916*38fd1498Szrj     {
4917*38fd1498Szrj       queue.release ();
4918*38fd1498Szrj       return false;
4919*38fd1498Szrj     }
4920*38fd1498Szrj 
4921*38fd1498Szrj   while (!queue.is_empty ())
4922*38fd1498Szrj     {
4923*38fd1498Szrj       edge e;
4924*38fd1498Szrj       edge_iterator ei;
4925*38fd1498Szrj 
4926*38fd1498Szrj       bb = queue.pop ();
4927*38fd1498Szrj       FOR_EACH_EDGE (e, ei, bb->succs)
4928*38fd1498Szrj 	if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
4929*38fd1498Szrj 	    && bitmap_set_bit (reachable, e->dest->index))
4930*38fd1498Szrj 	  queue.quick_push (e->dest);
4931*38fd1498Szrj     }
4932*38fd1498Szrj   queue.release ();
4933*38fd1498Szrj 
4934*38fd1498Szrj   FOR_BB_INSNS (first, insn)
4935*38fd1498Szrj     {
4936*38fd1498Szrj       rtx dest = interesting_dest_for_shprep (insn, NULL);
4937*38fd1498Szrj       if (!dest)
4938*38fd1498Szrj 	continue;
4939*38fd1498Szrj 
4940*38fd1498Szrj       if (DF_REG_DEF_COUNT (REGNO (dest)) > 1)
4941*38fd1498Szrj 	return false;
4942*38fd1498Szrj 
4943*38fd1498Szrj       for (df_ref use = DF_REG_USE_CHAIN (REGNO(dest));
4944*38fd1498Szrj 	   use;
4945*38fd1498Szrj 	   use = DF_REF_NEXT_REG (use))
4946*38fd1498Szrj 	{
4947*38fd1498Szrj 	  int ubbi = DF_REF_BB (use)->index;
4948*38fd1498Szrj 	  if (bitmap_bit_p (reachable, ubbi))
4949*38fd1498Szrj 	    bitmap_set_bit (need_new, ubbi);
4950*38fd1498Szrj 	}
4951*38fd1498Szrj       last_interesting_insn = insn;
4952*38fd1498Szrj     }
4953*38fd1498Szrj 
4954*38fd1498Szrj   if (!last_interesting_insn)
4955*38fd1498Szrj     return false;
4956*38fd1498Szrj 
4957*38fd1498Szrj   call_dom = nearest_common_dominator_for_set (CDI_DOMINATORS, need_new);
4958*38fd1498Szrj   if (call_dom == first)
4959*38fd1498Szrj     return false;
4960*38fd1498Szrj 
4961*38fd1498Szrj   loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
4962*38fd1498Szrj   while (bb_loop_depth (call_dom) > 0)
4963*38fd1498Szrj     call_dom = get_immediate_dominator (CDI_DOMINATORS, call_dom);
4964*38fd1498Szrj   loop_optimizer_finalize ();
4965*38fd1498Szrj 
4966*38fd1498Szrj   if (call_dom == first)
4967*38fd1498Szrj     return false;
4968*38fd1498Szrj 
4969*38fd1498Szrj   calculate_dominance_info (CDI_POST_DOMINATORS);
4970*38fd1498Szrj   if (dominated_by_p (CDI_POST_DOMINATORS, first, call_dom))
4971*38fd1498Szrj     {
4972*38fd1498Szrj       free_dominance_info (CDI_POST_DOMINATORS);
4973*38fd1498Szrj       return false;
4974*38fd1498Szrj     }
4975*38fd1498Szrj   free_dominance_info (CDI_POST_DOMINATORS);
4976*38fd1498Szrj 
4977*38fd1498Szrj   if (dump_file)
4978*38fd1498Szrj     fprintf (dump_file, "Will split live ranges of parameters at BB %i\n",
4979*38fd1498Szrj 	     call_dom->index);
4980*38fd1498Szrj 
4981*38fd1498Szrj   bool ret = false;
4982*38fd1498Szrj   FOR_BB_INSNS (first, insn)
4983*38fd1498Szrj     {
4984*38fd1498Szrj       rtx dest = interesting_dest_for_shprep (insn, call_dom);
4985*38fd1498Szrj       if (!dest || dest == pic_offset_table_rtx)
4986*38fd1498Szrj 	continue;
4987*38fd1498Szrj 
4988*38fd1498Szrj       bool need_newreg = false;
4989*38fd1498Szrj       df_ref use, next;
4990*38fd1498Szrj       for (use = DF_REG_USE_CHAIN (REGNO (dest)); use; use = next)
4991*38fd1498Szrj 	{
4992*38fd1498Szrj 	  rtx_insn *uin = DF_REF_INSN (use);
4993*38fd1498Szrj 	  next = DF_REF_NEXT_REG (use);
4994*38fd1498Szrj 
4995*38fd1498Szrj 	  if (DEBUG_INSN_P (uin))
4996*38fd1498Szrj 	    continue;
4997*38fd1498Szrj 
4998*38fd1498Szrj 	  basic_block ubb = BLOCK_FOR_INSN (uin);
4999*38fd1498Szrj 	  if (ubb == call_dom
5000*38fd1498Szrj 	      || dominated_by_p (CDI_DOMINATORS, ubb, call_dom))
5001*38fd1498Szrj 	    {
5002*38fd1498Szrj 	      need_newreg = true;
5003*38fd1498Szrj 	      break;
5004*38fd1498Szrj 	    }
5005*38fd1498Szrj 	}
5006*38fd1498Szrj 
5007*38fd1498Szrj       if (need_newreg)
5008*38fd1498Szrj 	{
5009*38fd1498Szrj 	  rtx newreg = ira_create_new_reg (dest);
5010*38fd1498Szrj 
5011*38fd1498Szrj 	  for (use = DF_REG_USE_CHAIN (REGNO (dest)); use; use = next)
5012*38fd1498Szrj 	    {
5013*38fd1498Szrj 	      rtx_insn *uin = DF_REF_INSN (use);
5014*38fd1498Szrj 	      next = DF_REF_NEXT_REG (use);
5015*38fd1498Szrj 
5016*38fd1498Szrj 	      basic_block ubb = BLOCK_FOR_INSN (uin);
5017*38fd1498Szrj 	      if (ubb == call_dom
5018*38fd1498Szrj 		  || dominated_by_p (CDI_DOMINATORS, ubb, call_dom))
5019*38fd1498Szrj 		validate_change (uin, DF_REF_REAL_LOC (use), newreg, true);
5020*38fd1498Szrj 	    }
5021*38fd1498Szrj 
5022*38fd1498Szrj 	  rtx_insn *new_move = gen_move_insn (newreg, dest);
5023*38fd1498Szrj 	  emit_insn_after (new_move, bb_note (call_dom));
5024*38fd1498Szrj 	  if (dump_file)
5025*38fd1498Szrj 	    {
5026*38fd1498Szrj 	      fprintf (dump_file, "Split live-range of register ");
5027*38fd1498Szrj 	      print_rtl_single (dump_file, dest);
5028*38fd1498Szrj 	    }
5029*38fd1498Szrj 	  ret = true;
5030*38fd1498Szrj 	}
5031*38fd1498Szrj 
5032*38fd1498Szrj       if (insn == last_interesting_insn)
5033*38fd1498Szrj 	break;
5034*38fd1498Szrj     }
5035*38fd1498Szrj   apply_change_group ();
5036*38fd1498Szrj   return ret;
5037*38fd1498Szrj }
5038*38fd1498Szrj 
5039*38fd1498Szrj /* Perform the second half of the transformation started in
5040*38fd1498Szrj    find_moveable_pseudos.  We look for instances where the newly introduced
5041*38fd1498Szrj    pseudo remains unallocated, and remove it by moving the definition to
5042*38fd1498Szrj    just before its use, replacing the move instruction generated by
5043*38fd1498Szrj    find_moveable_pseudos.  */
5044*38fd1498Szrj static void
move_unallocated_pseudos(void)5045*38fd1498Szrj move_unallocated_pseudos (void)
5046*38fd1498Szrj {
5047*38fd1498Szrj   int i;
5048*38fd1498Szrj   for (i = first_moveable_pseudo; i < last_moveable_pseudo; i++)
5049*38fd1498Szrj     if (reg_renumber[i] < 0)
5050*38fd1498Szrj       {
5051*38fd1498Szrj 	int idx = i - first_moveable_pseudo;
5052*38fd1498Szrj 	rtx other_reg = pseudo_replaced_reg[idx];
5053*38fd1498Szrj 	rtx_insn *def_insn = DF_REF_INSN (DF_REG_DEF_CHAIN (i));
5054*38fd1498Szrj 	/* The use must follow all definitions of OTHER_REG, so we can
5055*38fd1498Szrj 	   insert the new definition immediately after any of them.  */
5056*38fd1498Szrj 	df_ref other_def = DF_REG_DEF_CHAIN (REGNO (other_reg));
5057*38fd1498Szrj 	rtx_insn *move_insn = DF_REF_INSN (other_def);
5058*38fd1498Szrj 	rtx_insn *newinsn = emit_insn_after (PATTERN (def_insn), move_insn);
5059*38fd1498Szrj 	rtx set;
5060*38fd1498Szrj 	int success;
5061*38fd1498Szrj 
5062*38fd1498Szrj 	if (dump_file)
5063*38fd1498Szrj 	  fprintf (dump_file, "moving def of %d (insn %d now) ",
5064*38fd1498Szrj 		   REGNO (other_reg), INSN_UID (def_insn));
5065*38fd1498Szrj 
5066*38fd1498Szrj 	delete_insn (move_insn);
5067*38fd1498Szrj 	while ((other_def = DF_REG_DEF_CHAIN (REGNO (other_reg))))
5068*38fd1498Szrj 	  delete_insn (DF_REF_INSN (other_def));
5069*38fd1498Szrj 	delete_insn (def_insn);
5070*38fd1498Szrj 
5071*38fd1498Szrj 	set = single_set (newinsn);
5072*38fd1498Szrj 	success = validate_change (newinsn, &SET_DEST (set), other_reg, 0);
5073*38fd1498Szrj 	gcc_assert (success);
5074*38fd1498Szrj 	if (dump_file)
5075*38fd1498Szrj 	  fprintf (dump_file, " %d) rather than keep unallocated replacement %d\n",
5076*38fd1498Szrj 		   INSN_UID (newinsn), i);
5077*38fd1498Szrj 	SET_REG_N_REFS (i, 0);
5078*38fd1498Szrj       }
5079*38fd1498Szrj }
5080*38fd1498Szrj 
5081*38fd1498Szrj /* If the backend knows where to allocate pseudos for hard
5082*38fd1498Szrj    register initial values, register these allocations now.  */
5083*38fd1498Szrj static void
allocate_initial_values(void)5084*38fd1498Szrj allocate_initial_values (void)
5085*38fd1498Szrj {
5086*38fd1498Szrj   if (targetm.allocate_initial_value)
5087*38fd1498Szrj     {
5088*38fd1498Szrj       rtx hreg, preg, x;
5089*38fd1498Szrj       int i, regno;
5090*38fd1498Szrj 
5091*38fd1498Szrj       for (i = 0; HARD_REGISTER_NUM_P (i); i++)
5092*38fd1498Szrj 	{
5093*38fd1498Szrj 	  if (! initial_value_entry (i, &hreg, &preg))
5094*38fd1498Szrj 	    break;
5095*38fd1498Szrj 
5096*38fd1498Szrj 	  x = targetm.allocate_initial_value (hreg);
5097*38fd1498Szrj 	  regno = REGNO (preg);
5098*38fd1498Szrj 	  if (x && REG_N_SETS (regno) <= 1)
5099*38fd1498Szrj 	    {
5100*38fd1498Szrj 	      if (MEM_P (x))
5101*38fd1498Szrj 		reg_equiv_memory_loc (regno) = x;
5102*38fd1498Szrj 	      else
5103*38fd1498Szrj 		{
5104*38fd1498Szrj 		  basic_block bb;
5105*38fd1498Szrj 		  int new_regno;
5106*38fd1498Szrj 
5107*38fd1498Szrj 		  gcc_assert (REG_P (x));
5108*38fd1498Szrj 		  new_regno = REGNO (x);
5109*38fd1498Szrj 		  reg_renumber[regno] = new_regno;
5110*38fd1498Szrj 		  /* Poke the regno right into regno_reg_rtx so that even
5111*38fd1498Szrj 		     fixed regs are accepted.  */
5112*38fd1498Szrj 		  SET_REGNO (preg, new_regno);
5113*38fd1498Szrj 		  /* Update global register liveness information.  */
5114*38fd1498Szrj 		  FOR_EACH_BB_FN (bb, cfun)
5115*38fd1498Szrj 		    {
5116*38fd1498Szrj 		      if (REGNO_REG_SET_P (df_get_live_in (bb), regno))
5117*38fd1498Szrj 			SET_REGNO_REG_SET (df_get_live_in (bb), new_regno);
5118*38fd1498Szrj 		      if (REGNO_REG_SET_P (df_get_live_out (bb), regno))
5119*38fd1498Szrj 			SET_REGNO_REG_SET (df_get_live_out (bb), new_regno);
5120*38fd1498Szrj 		    }
5121*38fd1498Szrj 		}
5122*38fd1498Szrj 	    }
5123*38fd1498Szrj 	}
5124*38fd1498Szrj 
5125*38fd1498Szrj       gcc_checking_assert (! initial_value_entry (FIRST_PSEUDO_REGISTER,
5126*38fd1498Szrj 						  &hreg, &preg));
5127*38fd1498Szrj     }
5128*38fd1498Szrj }
5129*38fd1498Szrj 
5130*38fd1498Szrj 
5131*38fd1498Szrj /* True when we use LRA instead of reload pass for the current
5132*38fd1498Szrj    function.  */
5133*38fd1498Szrj bool ira_use_lra_p;
5134*38fd1498Szrj 
5135*38fd1498Szrj /* True if we have allocno conflicts.  It is false for non-optimized
5136*38fd1498Szrj    mode or when the conflict table is too big.  */
5137*38fd1498Szrj bool ira_conflicts_p;
5138*38fd1498Szrj 
5139*38fd1498Szrj /* Saved between IRA and reload.  */
5140*38fd1498Szrj static int saved_flag_ira_share_spill_slots;
5141*38fd1498Szrj 
5142*38fd1498Szrj /* This is the main entry of IRA.  */
5143*38fd1498Szrj static void
ira(FILE * f)5144*38fd1498Szrj ira (FILE *f)
5145*38fd1498Szrj {
5146*38fd1498Szrj   bool loops_p;
5147*38fd1498Szrj   int ira_max_point_before_emit;
5148*38fd1498Szrj   bool saved_flag_caller_saves = flag_caller_saves;
5149*38fd1498Szrj   enum ira_region saved_flag_ira_region = flag_ira_region;
5150*38fd1498Szrj 
5151*38fd1498Szrj   clear_bb_flags ();
5152*38fd1498Szrj 
5153*38fd1498Szrj   /* Determine if the current function is a leaf before running IRA
5154*38fd1498Szrj      since this can impact optimizations done by the prologue and
5155*38fd1498Szrj      epilogue thus changing register elimination offsets.
5156*38fd1498Szrj      Other target callbacks may use crtl->is_leaf too, including
5157*38fd1498Szrj      SHRINK_WRAPPING_ENABLED, so initialize as early as possible.  */
5158*38fd1498Szrj   crtl->is_leaf = leaf_function_p ();
5159*38fd1498Szrj 
5160*38fd1498Szrj   /* Perform target specific PIC register initialization.  */
5161*38fd1498Szrj   targetm.init_pic_reg ();
5162*38fd1498Szrj 
5163*38fd1498Szrj   ira_conflicts_p = optimize > 0;
5164*38fd1498Szrj 
5165*38fd1498Szrj   /* If there are too many pseudos and/or basic blocks (e.g. 10K
5166*38fd1498Szrj      pseudos and 10K blocks or 100K pseudos and 1K blocks), we will
5167*38fd1498Szrj      use simplified and faster algorithms in LRA.  */
5168*38fd1498Szrj   lra_simple_p
5169*38fd1498Szrj     = (ira_use_lra_p
5170*38fd1498Szrj        && max_reg_num () >= (1 << 26) / last_basic_block_for_fn (cfun));
5171*38fd1498Szrj   if (lra_simple_p)
5172*38fd1498Szrj     {
5173*38fd1498Szrj       /* It permits to skip live range splitting in LRA.  */
5174*38fd1498Szrj       flag_caller_saves = false;
5175*38fd1498Szrj       /* There is no sense to do regional allocation when we use
5176*38fd1498Szrj 	 simplified LRA.  */
5177*38fd1498Szrj       flag_ira_region = IRA_REGION_ONE;
5178*38fd1498Szrj       ira_conflicts_p = false;
5179*38fd1498Szrj     }
5180*38fd1498Szrj 
5181*38fd1498Szrj #ifndef IRA_NO_OBSTACK
5182*38fd1498Szrj   gcc_obstack_init (&ira_obstack);
5183*38fd1498Szrj #endif
5184*38fd1498Szrj   bitmap_obstack_initialize (&ira_bitmap_obstack);
5185*38fd1498Szrj 
5186*38fd1498Szrj   /* LRA uses its own infrastructure to handle caller save registers.  */
5187*38fd1498Szrj   if (flag_caller_saves && !ira_use_lra_p)
5188*38fd1498Szrj     init_caller_save ();
5189*38fd1498Szrj 
5190*38fd1498Szrj   if (flag_ira_verbose < 10)
5191*38fd1498Szrj     {
5192*38fd1498Szrj       internal_flag_ira_verbose = flag_ira_verbose;
5193*38fd1498Szrj       ira_dump_file = f;
5194*38fd1498Szrj     }
5195*38fd1498Szrj   else
5196*38fd1498Szrj     {
5197*38fd1498Szrj       internal_flag_ira_verbose = flag_ira_verbose - 10;
5198*38fd1498Szrj       ira_dump_file = stderr;
5199*38fd1498Szrj     }
5200*38fd1498Szrj 
5201*38fd1498Szrj   setup_prohibited_mode_move_regs ();
5202*38fd1498Szrj   decrease_live_ranges_number ();
5203*38fd1498Szrj   df_note_add_problem ();
5204*38fd1498Szrj 
5205*38fd1498Szrj   /* DF_LIVE can't be used in the register allocator, too many other
5206*38fd1498Szrj      parts of the compiler depend on using the "classic" liveness
5207*38fd1498Szrj      interpretation of the DF_LR problem.  See PR38711.
5208*38fd1498Szrj      Remove the problem, so that we don't spend time updating it in
5209*38fd1498Szrj      any of the df_analyze() calls during IRA/LRA.  */
5210*38fd1498Szrj   if (optimize > 1)
5211*38fd1498Szrj     df_remove_problem (df_live);
5212*38fd1498Szrj   gcc_checking_assert (df_live == NULL);
5213*38fd1498Szrj 
5214*38fd1498Szrj   if (flag_checking)
5215*38fd1498Szrj     df->changeable_flags |= DF_VERIFY_SCHEDULED;
5216*38fd1498Szrj 
5217*38fd1498Szrj   df_analyze ();
5218*38fd1498Szrj 
5219*38fd1498Szrj   init_reg_equiv ();
5220*38fd1498Szrj   if (ira_conflicts_p)
5221*38fd1498Szrj     {
5222*38fd1498Szrj       calculate_dominance_info (CDI_DOMINATORS);
5223*38fd1498Szrj 
5224*38fd1498Szrj       if (split_live_ranges_for_shrink_wrap ())
5225*38fd1498Szrj 	df_analyze ();
5226*38fd1498Szrj 
5227*38fd1498Szrj       free_dominance_info (CDI_DOMINATORS);
5228*38fd1498Szrj     }
5229*38fd1498Szrj 
5230*38fd1498Szrj   df_clear_flags (DF_NO_INSN_RESCAN);
5231*38fd1498Szrj 
5232*38fd1498Szrj   indirect_jump_optimize ();
5233*38fd1498Szrj   if (delete_trivially_dead_insns (get_insns (), max_reg_num ()))
5234*38fd1498Szrj     df_analyze ();
5235*38fd1498Szrj 
5236*38fd1498Szrj   regstat_init_n_sets_and_refs ();
5237*38fd1498Szrj   regstat_compute_ri ();
5238*38fd1498Szrj 
5239*38fd1498Szrj   /* If we are not optimizing, then this is the only place before
5240*38fd1498Szrj      register allocation where dataflow is done.  And that is needed
5241*38fd1498Szrj      to generate these warnings.  */
5242*38fd1498Szrj   if (warn_clobbered)
5243*38fd1498Szrj     generate_setjmp_warnings ();
5244*38fd1498Szrj 
5245*38fd1498Szrj   if (resize_reg_info () && flag_ira_loop_pressure)
5246*38fd1498Szrj     ira_set_pseudo_classes (true, ira_dump_file);
5247*38fd1498Szrj 
5248*38fd1498Szrj   init_alias_analysis ();
5249*38fd1498Szrj   loop_optimizer_init (AVOID_CFG_MODIFICATIONS);
5250*38fd1498Szrj   reg_equiv = XCNEWVEC (struct equivalence, max_reg_num ());
5251*38fd1498Szrj   update_equiv_regs ();
5252*38fd1498Szrj 
5253*38fd1498Szrj   /* Don't move insns if live range shrinkage or register
5254*38fd1498Szrj      pressure-sensitive scheduling were done because it will not
5255*38fd1498Szrj      improve allocation but likely worsen insn scheduling.  */
5256*38fd1498Szrj   if (optimize
5257*38fd1498Szrj       && !flag_live_range_shrinkage
5258*38fd1498Szrj       && !(flag_sched_pressure && flag_schedule_insns))
5259*38fd1498Szrj     combine_and_move_insns ();
5260*38fd1498Szrj 
5261*38fd1498Szrj   /* Gather additional equivalences with memory.  */
5262*38fd1498Szrj   if (optimize)
5263*38fd1498Szrj     add_store_equivs ();
5264*38fd1498Szrj 
5265*38fd1498Szrj   loop_optimizer_finalize ();
5266*38fd1498Szrj   free_dominance_info (CDI_DOMINATORS);
5267*38fd1498Szrj   end_alias_analysis ();
5268*38fd1498Szrj   free (reg_equiv);
5269*38fd1498Szrj 
5270*38fd1498Szrj   setup_reg_equiv ();
5271*38fd1498Szrj   grow_reg_equivs ();
5272*38fd1498Szrj   setup_reg_equiv_init ();
5273*38fd1498Szrj 
5274*38fd1498Szrj   allocated_reg_info_size = max_reg_num ();
5275*38fd1498Szrj 
5276*38fd1498Szrj   /* It is not worth to do such improvement when we use a simple
5277*38fd1498Szrj      allocation because of -O0 usage or because the function is too
5278*38fd1498Szrj      big.  */
5279*38fd1498Szrj   if (ira_conflicts_p)
5280*38fd1498Szrj     find_moveable_pseudos ();
5281*38fd1498Szrj 
5282*38fd1498Szrj   max_regno_before_ira = max_reg_num ();
5283*38fd1498Szrj   ira_setup_eliminable_regset ();
5284*38fd1498Szrj 
5285*38fd1498Szrj   ira_overall_cost = ira_reg_cost = ira_mem_cost = 0;
5286*38fd1498Szrj   ira_load_cost = ira_store_cost = ira_shuffle_cost = 0;
5287*38fd1498Szrj   ira_move_loops_num = ira_additional_jumps_num = 0;
5288*38fd1498Szrj 
5289*38fd1498Szrj   ira_assert (current_loops == NULL);
5290*38fd1498Szrj   if (flag_ira_region == IRA_REGION_ALL || flag_ira_region == IRA_REGION_MIXED)
5291*38fd1498Szrj     loop_optimizer_init (AVOID_CFG_MODIFICATIONS | LOOPS_HAVE_RECORDED_EXITS);
5292*38fd1498Szrj 
5293*38fd1498Szrj   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
5294*38fd1498Szrj     fprintf (ira_dump_file, "Building IRA IR\n");
5295*38fd1498Szrj   loops_p = ira_build ();
5296*38fd1498Szrj 
5297*38fd1498Szrj   ira_assert (ira_conflicts_p || !loops_p);
5298*38fd1498Szrj 
5299*38fd1498Szrj   saved_flag_ira_share_spill_slots = flag_ira_share_spill_slots;
5300*38fd1498Szrj   if (too_high_register_pressure_p () || cfun->calls_setjmp)
5301*38fd1498Szrj     /* It is just wasting compiler's time to pack spilled pseudos into
5302*38fd1498Szrj        stack slots in this case -- prohibit it.  We also do this if
5303*38fd1498Szrj        there is setjmp call because a variable not modified between
5304*38fd1498Szrj        setjmp and longjmp the compiler is required to preserve its
5305*38fd1498Szrj        value and sharing slots does not guarantee it.  */
5306*38fd1498Szrj     flag_ira_share_spill_slots = FALSE;
5307*38fd1498Szrj 
5308*38fd1498Szrj   ira_color ();
5309*38fd1498Szrj 
5310*38fd1498Szrj   ira_max_point_before_emit = ira_max_point;
5311*38fd1498Szrj 
5312*38fd1498Szrj   ira_initiate_emit_data ();
5313*38fd1498Szrj 
5314*38fd1498Szrj   ira_emit (loops_p);
5315*38fd1498Szrj 
5316*38fd1498Szrj   max_regno = max_reg_num ();
5317*38fd1498Szrj   if (ira_conflicts_p)
5318*38fd1498Szrj     {
5319*38fd1498Szrj       if (! loops_p)
5320*38fd1498Szrj 	{
5321*38fd1498Szrj 	  if (! ira_use_lra_p)
5322*38fd1498Szrj 	    ira_initiate_assign ();
5323*38fd1498Szrj 	}
5324*38fd1498Szrj       else
5325*38fd1498Szrj 	{
5326*38fd1498Szrj 	  expand_reg_info ();
5327*38fd1498Szrj 
5328*38fd1498Szrj 	  if (ira_use_lra_p)
5329*38fd1498Szrj 	    {
5330*38fd1498Szrj 	      ira_allocno_t a;
5331*38fd1498Szrj 	      ira_allocno_iterator ai;
5332*38fd1498Szrj 
5333*38fd1498Szrj 	      FOR_EACH_ALLOCNO (a, ai)
5334*38fd1498Szrj                 {
5335*38fd1498Szrj                   int old_regno = ALLOCNO_REGNO (a);
5336*38fd1498Szrj                   int new_regno = REGNO (ALLOCNO_EMIT_DATA (a)->reg);
5337*38fd1498Szrj 
5338*38fd1498Szrj                   ALLOCNO_REGNO (a) = new_regno;
5339*38fd1498Szrj 
5340*38fd1498Szrj                   if (old_regno != new_regno)
5341*38fd1498Szrj                     setup_reg_classes (new_regno, reg_preferred_class (old_regno),
5342*38fd1498Szrj                                        reg_alternate_class (old_regno),
5343*38fd1498Szrj                                        reg_allocno_class (old_regno));
5344*38fd1498Szrj                 }
5345*38fd1498Szrj 	    }
5346*38fd1498Szrj 	  else
5347*38fd1498Szrj 	    {
5348*38fd1498Szrj 	      if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
5349*38fd1498Szrj 		fprintf (ira_dump_file, "Flattening IR\n");
5350*38fd1498Szrj 	      ira_flattening (max_regno_before_ira, ira_max_point_before_emit);
5351*38fd1498Szrj 	    }
5352*38fd1498Szrj 	  /* New insns were generated: add notes and recalculate live
5353*38fd1498Szrj 	     info.  */
5354*38fd1498Szrj 	  df_analyze ();
5355*38fd1498Szrj 
5356*38fd1498Szrj 	  /* ??? Rebuild the loop tree, but why?  Does the loop tree
5357*38fd1498Szrj 	     change if new insns were generated?  Can that be handled
5358*38fd1498Szrj 	     by updating the loop tree incrementally?  */
5359*38fd1498Szrj 	  loop_optimizer_finalize ();
5360*38fd1498Szrj 	  free_dominance_info (CDI_DOMINATORS);
5361*38fd1498Szrj 	  loop_optimizer_init (AVOID_CFG_MODIFICATIONS
5362*38fd1498Szrj 			       | LOOPS_HAVE_RECORDED_EXITS);
5363*38fd1498Szrj 
5364*38fd1498Szrj 	  if (! ira_use_lra_p)
5365*38fd1498Szrj 	    {
5366*38fd1498Szrj 	      setup_allocno_assignment_flags ();
5367*38fd1498Szrj 	      ira_initiate_assign ();
5368*38fd1498Szrj 	      ira_reassign_conflict_allocnos (max_regno);
5369*38fd1498Szrj 	    }
5370*38fd1498Szrj 	}
5371*38fd1498Szrj     }
5372*38fd1498Szrj 
5373*38fd1498Szrj   ira_finish_emit_data ();
5374*38fd1498Szrj 
5375*38fd1498Szrj   setup_reg_renumber ();
5376*38fd1498Szrj 
5377*38fd1498Szrj   calculate_allocation_cost ();
5378*38fd1498Szrj 
5379*38fd1498Szrj #ifdef ENABLE_IRA_CHECKING
5380*38fd1498Szrj   if (ira_conflicts_p && ! ira_use_lra_p)
5381*38fd1498Szrj     /* Opposite to reload pass, LRA does not use any conflict info
5382*38fd1498Szrj        from IRA.  We don't rebuild conflict info for LRA (through
5383*38fd1498Szrj        ira_flattening call) and can not use the check here.  We could
5384*38fd1498Szrj        rebuild this info for LRA in the check mode but there is a risk
5385*38fd1498Szrj        that code generated with the check and without it will be a bit
5386*38fd1498Szrj        different.  Calling ira_flattening in any mode would be a
5387*38fd1498Szrj        wasting CPU time.  So do not check the allocation for LRA.  */
5388*38fd1498Szrj     check_allocation ();
5389*38fd1498Szrj #endif
5390*38fd1498Szrj 
5391*38fd1498Szrj   if (max_regno != max_regno_before_ira)
5392*38fd1498Szrj     {
5393*38fd1498Szrj       regstat_free_n_sets_and_refs ();
5394*38fd1498Szrj       regstat_free_ri ();
5395*38fd1498Szrj       regstat_init_n_sets_and_refs ();
5396*38fd1498Szrj       regstat_compute_ri ();
5397*38fd1498Szrj     }
5398*38fd1498Szrj 
5399*38fd1498Szrj   overall_cost_before = ira_overall_cost;
5400*38fd1498Szrj   if (! ira_conflicts_p)
5401*38fd1498Szrj     grow_reg_equivs ();
5402*38fd1498Szrj   else
5403*38fd1498Szrj     {
5404*38fd1498Szrj       fix_reg_equiv_init ();
5405*38fd1498Szrj 
5406*38fd1498Szrj #ifdef ENABLE_IRA_CHECKING
5407*38fd1498Szrj       print_redundant_copies ();
5408*38fd1498Szrj #endif
5409*38fd1498Szrj       if (! ira_use_lra_p)
5410*38fd1498Szrj 	{
5411*38fd1498Szrj 	  ira_spilled_reg_stack_slots_num = 0;
5412*38fd1498Szrj 	  ira_spilled_reg_stack_slots
5413*38fd1498Szrj 	    = ((struct ira_spilled_reg_stack_slot *)
5414*38fd1498Szrj 	       ira_allocate (max_regno
5415*38fd1498Szrj 			     * sizeof (struct ira_spilled_reg_stack_slot)));
5416*38fd1498Szrj 	  memset (ira_spilled_reg_stack_slots, 0,
5417*38fd1498Szrj 		  max_regno * sizeof (struct ira_spilled_reg_stack_slot));
5418*38fd1498Szrj 	}
5419*38fd1498Szrj     }
5420*38fd1498Szrj   allocate_initial_values ();
5421*38fd1498Szrj 
5422*38fd1498Szrj   /* See comment for find_moveable_pseudos call.  */
5423*38fd1498Szrj   if (ira_conflicts_p)
5424*38fd1498Szrj     move_unallocated_pseudos ();
5425*38fd1498Szrj 
5426*38fd1498Szrj   /* Restore original values.  */
5427*38fd1498Szrj   if (lra_simple_p)
5428*38fd1498Szrj     {
5429*38fd1498Szrj       flag_caller_saves = saved_flag_caller_saves;
5430*38fd1498Szrj       flag_ira_region = saved_flag_ira_region;
5431*38fd1498Szrj     }
5432*38fd1498Szrj }
5433*38fd1498Szrj 
5434*38fd1498Szrj static void
do_reload(void)5435*38fd1498Szrj do_reload (void)
5436*38fd1498Szrj {
5437*38fd1498Szrj   basic_block bb;
5438*38fd1498Szrj   bool need_dce;
5439*38fd1498Szrj   unsigned pic_offset_table_regno = INVALID_REGNUM;
5440*38fd1498Szrj 
5441*38fd1498Szrj   if (flag_ira_verbose < 10)
5442*38fd1498Szrj     ira_dump_file = dump_file;
5443*38fd1498Szrj 
5444*38fd1498Szrj   /* If pic_offset_table_rtx is a pseudo register, then keep it so
5445*38fd1498Szrj      after reload to avoid possible wrong usages of hard reg assigned
5446*38fd1498Szrj      to it.  */
5447*38fd1498Szrj   if (pic_offset_table_rtx
5448*38fd1498Szrj       && REGNO (pic_offset_table_rtx) >= FIRST_PSEUDO_REGISTER)
5449*38fd1498Szrj     pic_offset_table_regno = REGNO (pic_offset_table_rtx);
5450*38fd1498Szrj 
5451*38fd1498Szrj   timevar_push (TV_RELOAD);
5452*38fd1498Szrj   if (ira_use_lra_p)
5453*38fd1498Szrj     {
5454*38fd1498Szrj       if (current_loops != NULL)
5455*38fd1498Szrj 	{
5456*38fd1498Szrj 	  loop_optimizer_finalize ();
5457*38fd1498Szrj 	  free_dominance_info (CDI_DOMINATORS);
5458*38fd1498Szrj 	}
5459*38fd1498Szrj       FOR_ALL_BB_FN (bb, cfun)
5460*38fd1498Szrj 	bb->loop_father = NULL;
5461*38fd1498Szrj       current_loops = NULL;
5462*38fd1498Szrj 
5463*38fd1498Szrj       ira_destroy ();
5464*38fd1498Szrj 
5465*38fd1498Szrj       lra (ira_dump_file);
5466*38fd1498Szrj       /* ???!!! Move it before lra () when we use ira_reg_equiv in
5467*38fd1498Szrj 	 LRA.  */
5468*38fd1498Szrj       vec_free (reg_equivs);
5469*38fd1498Szrj       reg_equivs = NULL;
5470*38fd1498Szrj       need_dce = false;
5471*38fd1498Szrj     }
5472*38fd1498Szrj   else
5473*38fd1498Szrj     {
5474*38fd1498Szrj       df_set_flags (DF_NO_INSN_RESCAN);
5475*38fd1498Szrj       build_insn_chain ();
5476*38fd1498Szrj 
5477*38fd1498Szrj       need_dce = reload (get_insns (), ira_conflicts_p);
5478*38fd1498Szrj     }
5479*38fd1498Szrj 
5480*38fd1498Szrj   timevar_pop (TV_RELOAD);
5481*38fd1498Szrj 
5482*38fd1498Szrj   timevar_push (TV_IRA);
5483*38fd1498Szrj 
5484*38fd1498Szrj   if (ira_conflicts_p && ! ira_use_lra_p)
5485*38fd1498Szrj     {
5486*38fd1498Szrj       ira_free (ira_spilled_reg_stack_slots);
5487*38fd1498Szrj       ira_finish_assign ();
5488*38fd1498Szrj     }
5489*38fd1498Szrj 
5490*38fd1498Szrj   if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL
5491*38fd1498Szrj       && overall_cost_before != ira_overall_cost)
5492*38fd1498Szrj     fprintf (ira_dump_file, "+++Overall after reload %" PRId64 "\n",
5493*38fd1498Szrj 	     ira_overall_cost);
5494*38fd1498Szrj 
5495*38fd1498Szrj   flag_ira_share_spill_slots = saved_flag_ira_share_spill_slots;
5496*38fd1498Szrj 
5497*38fd1498Szrj   if (! ira_use_lra_p)
5498*38fd1498Szrj     {
5499*38fd1498Szrj       ira_destroy ();
5500*38fd1498Szrj       if (current_loops != NULL)
5501*38fd1498Szrj 	{
5502*38fd1498Szrj 	  loop_optimizer_finalize ();
5503*38fd1498Szrj 	  free_dominance_info (CDI_DOMINATORS);
5504*38fd1498Szrj 	}
5505*38fd1498Szrj       FOR_ALL_BB_FN (bb, cfun)
5506*38fd1498Szrj 	bb->loop_father = NULL;
5507*38fd1498Szrj       current_loops = NULL;
5508*38fd1498Szrj 
5509*38fd1498Szrj       regstat_free_ri ();
5510*38fd1498Szrj       regstat_free_n_sets_and_refs ();
5511*38fd1498Szrj     }
5512*38fd1498Szrj 
5513*38fd1498Szrj   if (optimize)
5514*38fd1498Szrj     cleanup_cfg (CLEANUP_EXPENSIVE);
5515*38fd1498Szrj 
5516*38fd1498Szrj   finish_reg_equiv ();
5517*38fd1498Szrj 
5518*38fd1498Szrj   bitmap_obstack_release (&ira_bitmap_obstack);
5519*38fd1498Szrj #ifndef IRA_NO_OBSTACK
5520*38fd1498Szrj   obstack_free (&ira_obstack, NULL);
5521*38fd1498Szrj #endif
5522*38fd1498Szrj 
5523*38fd1498Szrj   /* The code after the reload has changed so much that at this point
5524*38fd1498Szrj      we might as well just rescan everything.  Note that
5525*38fd1498Szrj      df_rescan_all_insns is not going to help here because it does not
5526*38fd1498Szrj      touch the artificial uses and defs.  */
5527*38fd1498Szrj   df_finish_pass (true);
5528*38fd1498Szrj   df_scan_alloc (NULL);
5529*38fd1498Szrj   df_scan_blocks ();
5530*38fd1498Szrj 
5531*38fd1498Szrj   if (optimize > 1)
5532*38fd1498Szrj     {
5533*38fd1498Szrj       df_live_add_problem ();
5534*38fd1498Szrj       df_live_set_all_dirty ();
5535*38fd1498Szrj     }
5536*38fd1498Szrj 
5537*38fd1498Szrj   if (optimize)
5538*38fd1498Szrj     df_analyze ();
5539*38fd1498Szrj 
5540*38fd1498Szrj   if (need_dce && optimize)
5541*38fd1498Szrj     run_fast_dce ();
5542*38fd1498Szrj 
5543*38fd1498Szrj   /* Diagnose uses of the hard frame pointer when it is used as a global
5544*38fd1498Szrj      register.  Often we can get away with letting the user appropriate
5545*38fd1498Szrj      the frame pointer, but we should let them know when code generation
5546*38fd1498Szrj      makes that impossible.  */
5547*38fd1498Szrj   if (global_regs[HARD_FRAME_POINTER_REGNUM] && frame_pointer_needed)
5548*38fd1498Szrj     {
5549*38fd1498Szrj       tree decl = global_regs_decl[HARD_FRAME_POINTER_REGNUM];
5550*38fd1498Szrj       error_at (DECL_SOURCE_LOCATION (current_function_decl),
5551*38fd1498Szrj                 "frame pointer required, but reserved");
5552*38fd1498Szrj       inform (DECL_SOURCE_LOCATION (decl), "for %qD", decl);
5553*38fd1498Szrj     }
5554*38fd1498Szrj 
5555*38fd1498Szrj   /* If we are doing generic stack checking, give a warning if this
5556*38fd1498Szrj      function's frame size is larger than we expect.  */
5557*38fd1498Szrj   if (flag_stack_check == GENERIC_STACK_CHECK)
5558*38fd1498Szrj     {
5559*38fd1498Szrj       poly_int64 size = get_frame_size () + STACK_CHECK_FIXED_FRAME_SIZE;
5560*38fd1498Szrj 
5561*38fd1498Szrj       for (int i = 0; i < FIRST_PSEUDO_REGISTER; i++)
5562*38fd1498Szrj 	if (df_regs_ever_live_p (i) && !fixed_regs[i] && call_used_regs[i])
5563*38fd1498Szrj 	  size += UNITS_PER_WORD;
5564*38fd1498Szrj 
5565*38fd1498Szrj       if (constant_lower_bound (size) > STACK_CHECK_MAX_FRAME_SIZE)
5566*38fd1498Szrj 	warning (0, "frame size too large for reliable stack checking");
5567*38fd1498Szrj     }
5568*38fd1498Szrj 
5569*38fd1498Szrj   if (pic_offset_table_regno != INVALID_REGNUM)
5570*38fd1498Szrj     pic_offset_table_rtx = gen_rtx_REG (Pmode, pic_offset_table_regno);
5571*38fd1498Szrj 
5572*38fd1498Szrj   timevar_pop (TV_IRA);
5573*38fd1498Szrj }
5574*38fd1498Szrj 
5575*38fd1498Szrj /* Run the integrated register allocator.  */
5576*38fd1498Szrj 
5577*38fd1498Szrj namespace {
5578*38fd1498Szrj 
5579*38fd1498Szrj const pass_data pass_data_ira =
5580*38fd1498Szrj {
5581*38fd1498Szrj   RTL_PASS, /* type */
5582*38fd1498Szrj   "ira", /* name */
5583*38fd1498Szrj   OPTGROUP_NONE, /* optinfo_flags */
5584*38fd1498Szrj   TV_IRA, /* tv_id */
5585*38fd1498Szrj   0, /* properties_required */
5586*38fd1498Szrj   0, /* properties_provided */
5587*38fd1498Szrj   0, /* properties_destroyed */
5588*38fd1498Szrj   0, /* todo_flags_start */
5589*38fd1498Szrj   TODO_do_not_ggc_collect, /* todo_flags_finish */
5590*38fd1498Szrj };
5591*38fd1498Szrj 
5592*38fd1498Szrj class pass_ira : public rtl_opt_pass
5593*38fd1498Szrj {
5594*38fd1498Szrj public:
pass_ira(gcc::context * ctxt)5595*38fd1498Szrj   pass_ira (gcc::context *ctxt)
5596*38fd1498Szrj     : rtl_opt_pass (pass_data_ira, ctxt)
5597*38fd1498Szrj   {}
5598*38fd1498Szrj 
5599*38fd1498Szrj   /* opt_pass methods: */
gate(function *)5600*38fd1498Szrj   virtual bool gate (function *)
5601*38fd1498Szrj     {
5602*38fd1498Szrj       return !targetm.no_register_allocation;
5603*38fd1498Szrj     }
execute(function *)5604*38fd1498Szrj   virtual unsigned int execute (function *)
5605*38fd1498Szrj     {
5606*38fd1498Szrj       ira (dump_file);
5607*38fd1498Szrj       return 0;
5608*38fd1498Szrj     }
5609*38fd1498Szrj 
5610*38fd1498Szrj }; // class pass_ira
5611*38fd1498Szrj 
5612*38fd1498Szrj } // anon namespace
5613*38fd1498Szrj 
5614*38fd1498Szrj rtl_opt_pass *
make_pass_ira(gcc::context * ctxt)5615*38fd1498Szrj make_pass_ira (gcc::context *ctxt)
5616*38fd1498Szrj {
5617*38fd1498Szrj   return new pass_ira (ctxt);
5618*38fd1498Szrj }
5619*38fd1498Szrj 
5620*38fd1498Szrj namespace {
5621*38fd1498Szrj 
5622*38fd1498Szrj const pass_data pass_data_reload =
5623*38fd1498Szrj {
5624*38fd1498Szrj   RTL_PASS, /* type */
5625*38fd1498Szrj   "reload", /* name */
5626*38fd1498Szrj   OPTGROUP_NONE, /* optinfo_flags */
5627*38fd1498Szrj   TV_RELOAD, /* tv_id */
5628*38fd1498Szrj   0, /* properties_required */
5629*38fd1498Szrj   0, /* properties_provided */
5630*38fd1498Szrj   0, /* properties_destroyed */
5631*38fd1498Szrj   0, /* todo_flags_start */
5632*38fd1498Szrj   0, /* todo_flags_finish */
5633*38fd1498Szrj };
5634*38fd1498Szrj 
5635*38fd1498Szrj class pass_reload : public rtl_opt_pass
5636*38fd1498Szrj {
5637*38fd1498Szrj public:
pass_reload(gcc::context * ctxt)5638*38fd1498Szrj   pass_reload (gcc::context *ctxt)
5639*38fd1498Szrj     : rtl_opt_pass (pass_data_reload, ctxt)
5640*38fd1498Szrj   {}
5641*38fd1498Szrj 
5642*38fd1498Szrj   /* opt_pass methods: */
gate(function *)5643*38fd1498Szrj   virtual bool gate (function *)
5644*38fd1498Szrj     {
5645*38fd1498Szrj       return !targetm.no_register_allocation;
5646*38fd1498Szrj     }
execute(function *)5647*38fd1498Szrj   virtual unsigned int execute (function *)
5648*38fd1498Szrj     {
5649*38fd1498Szrj       do_reload ();
5650*38fd1498Szrj       return 0;
5651*38fd1498Szrj     }
5652*38fd1498Szrj 
5653*38fd1498Szrj }; // class pass_reload
5654*38fd1498Szrj 
5655*38fd1498Szrj } // anon namespace
5656*38fd1498Szrj 
5657*38fd1498Szrj rtl_opt_pass *
make_pass_reload(gcc::context * ctxt)5658*38fd1498Szrj make_pass_reload (gcc::context *ctxt)
5659*38fd1498Szrj {
5660*38fd1498Szrj   return new pass_reload (ctxt);
5661*38fd1498Szrj }
5662