1*38fd1498Szrj /* IRA allocation based on graph coloring.
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 #include "config.h"
22*38fd1498Szrj #include "system.h"
23*38fd1498Szrj #include "coretypes.h"
24*38fd1498Szrj #include "backend.h"
25*38fd1498Szrj #include "target.h"
26*38fd1498Szrj #include "rtl.h"
27*38fd1498Szrj #include "tree.h"
28*38fd1498Szrj #include "predict.h"
29*38fd1498Szrj #include "df.h"
30*38fd1498Szrj #include "memmodel.h"
31*38fd1498Szrj #include "tm_p.h"
32*38fd1498Szrj #include "insn-config.h"
33*38fd1498Szrj #include "regs.h"
34*38fd1498Szrj #include "ira.h"
35*38fd1498Szrj #include "ira-int.h"
36*38fd1498Szrj #include "reload.h"
37*38fd1498Szrj #include "cfgloop.h"
38*38fd1498Szrj
39*38fd1498Szrj typedef struct allocno_hard_regs *allocno_hard_regs_t;
40*38fd1498Szrj
41*38fd1498Szrj /* The structure contains information about hard registers can be
42*38fd1498Szrj assigned to allocnos. Usually it is allocno profitable hard
43*38fd1498Szrj registers but in some cases this set can be a bit different. Major
44*38fd1498Szrj reason of the difference is a requirement to use hard register sets
45*38fd1498Szrj that form a tree or a forest (set of trees), i.e. hard register set
46*38fd1498Szrj of a node should contain hard register sets of its subnodes. */
47*38fd1498Szrj struct allocno_hard_regs
48*38fd1498Szrj {
49*38fd1498Szrj /* Hard registers can be assigned to an allocno. */
50*38fd1498Szrj HARD_REG_SET set;
51*38fd1498Szrj /* Overall (spilling) cost of all allocnos with given register
52*38fd1498Szrj set. */
53*38fd1498Szrj int64_t cost;
54*38fd1498Szrj };
55*38fd1498Szrj
56*38fd1498Szrj typedef struct allocno_hard_regs_node *allocno_hard_regs_node_t;
57*38fd1498Szrj
58*38fd1498Szrj /* A node representing allocno hard registers. Such nodes form a
59*38fd1498Szrj forest (set of trees). Each subnode of given node in the forest
60*38fd1498Szrj refers for hard register set (usually allocno profitable hard
61*38fd1498Szrj register set) which is a subset of one referred from given
62*38fd1498Szrj node. */
63*38fd1498Szrj struct allocno_hard_regs_node
64*38fd1498Szrj {
65*38fd1498Szrj /* Set up number of the node in preorder traversing of the forest. */
66*38fd1498Szrj int preorder_num;
67*38fd1498Szrj /* Used for different calculation like finding conflict size of an
68*38fd1498Szrj allocno. */
69*38fd1498Szrj int check;
70*38fd1498Szrj /* Used for calculation of conflict size of an allocno. The
71*38fd1498Szrj conflict size of the allocno is maximal number of given allocno
72*38fd1498Szrj hard registers needed for allocation of the conflicting allocnos.
73*38fd1498Szrj Given allocno is trivially colored if this number plus the number
74*38fd1498Szrj of hard registers needed for given allocno is not greater than
75*38fd1498Szrj the number of given allocno hard register set. */
76*38fd1498Szrj int conflict_size;
77*38fd1498Szrj /* The number of hard registers given by member hard_regs. */
78*38fd1498Szrj int hard_regs_num;
79*38fd1498Szrj /* The following member is used to form the final forest. */
80*38fd1498Szrj bool used_p;
81*38fd1498Szrj /* Pointer to the corresponding profitable hard registers. */
82*38fd1498Szrj allocno_hard_regs_t hard_regs;
83*38fd1498Szrj /* Parent, first subnode, previous and next node with the same
84*38fd1498Szrj parent in the forest. */
85*38fd1498Szrj allocno_hard_regs_node_t parent, first, prev, next;
86*38fd1498Szrj };
87*38fd1498Szrj
88*38fd1498Szrj /* Info about changing hard reg costs of an allocno. */
89*38fd1498Szrj struct update_cost_record
90*38fd1498Szrj {
91*38fd1498Szrj /* Hard regno for which we changed the cost. */
92*38fd1498Szrj int hard_regno;
93*38fd1498Szrj /* Divisor used when we changed the cost of HARD_REGNO. */
94*38fd1498Szrj int divisor;
95*38fd1498Szrj /* Next record for given allocno. */
96*38fd1498Szrj struct update_cost_record *next;
97*38fd1498Szrj };
98*38fd1498Szrj
99*38fd1498Szrj /* To decrease footprint of ira_allocno structure we store all data
100*38fd1498Szrj needed only for coloring in the following structure. */
101*38fd1498Szrj struct allocno_color_data
102*38fd1498Szrj {
103*38fd1498Szrj /* TRUE value means that the allocno was not removed yet from the
104*38fd1498Szrj conflicting graph during coloring. */
105*38fd1498Szrj unsigned int in_graph_p : 1;
106*38fd1498Szrj /* TRUE if it is put on the stack to make other allocnos
107*38fd1498Szrj colorable. */
108*38fd1498Szrj unsigned int may_be_spilled_p : 1;
109*38fd1498Szrj /* TRUE if the allocno is trivially colorable. */
110*38fd1498Szrj unsigned int colorable_p : 1;
111*38fd1498Szrj /* Number of hard registers of the allocno class really
112*38fd1498Szrj available for the allocno allocation. It is number of the
113*38fd1498Szrj profitable hard regs. */
114*38fd1498Szrj int available_regs_num;
115*38fd1498Szrj /* Sum of frequencies of hard register preferences of all
116*38fd1498Szrj conflicting allocnos which are not the coloring stack yet. */
117*38fd1498Szrj int conflict_allocno_hard_prefs;
118*38fd1498Szrj /* Allocnos in a bucket (used in coloring) chained by the following
119*38fd1498Szrj two members. */
120*38fd1498Szrj ira_allocno_t next_bucket_allocno;
121*38fd1498Szrj ira_allocno_t prev_bucket_allocno;
122*38fd1498Szrj /* Used for temporary purposes. */
123*38fd1498Szrj int temp;
124*38fd1498Szrj /* Used to exclude repeated processing. */
125*38fd1498Szrj int last_process;
126*38fd1498Szrj /* Profitable hard regs available for this pseudo allocation. It
127*38fd1498Szrj means that the set excludes unavailable hard regs and hard regs
128*38fd1498Szrj conflicting with given pseudo. They should be of the allocno
129*38fd1498Szrj class. */
130*38fd1498Szrj HARD_REG_SET profitable_hard_regs;
131*38fd1498Szrj /* The allocno hard registers node. */
132*38fd1498Szrj allocno_hard_regs_node_t hard_regs_node;
133*38fd1498Szrj /* Array of structures allocno_hard_regs_subnode representing
134*38fd1498Szrj given allocno hard registers node (the 1st element in the array)
135*38fd1498Szrj and all its subnodes in the tree (forest) of allocno hard
136*38fd1498Szrj register nodes (see comments above). */
137*38fd1498Szrj int hard_regs_subnodes_start;
138*38fd1498Szrj /* The length of the previous array. */
139*38fd1498Szrj int hard_regs_subnodes_num;
140*38fd1498Szrj /* Records about updating allocno hard reg costs from copies. If
141*38fd1498Szrj the allocno did not get expected hard register, these records are
142*38fd1498Szrj used to restore original hard reg costs of allocnos connected to
143*38fd1498Szrj this allocno by copies. */
144*38fd1498Szrj struct update_cost_record *update_cost_records;
145*38fd1498Szrj /* Threads. We collect allocnos connected by copies into threads
146*38fd1498Szrj and try to assign hard regs to allocnos by threads. */
147*38fd1498Szrj /* Allocno representing all thread. */
148*38fd1498Szrj ira_allocno_t first_thread_allocno;
149*38fd1498Szrj /* Allocnos in thread forms a cycle list through the following
150*38fd1498Szrj member. */
151*38fd1498Szrj ira_allocno_t next_thread_allocno;
152*38fd1498Szrj /* All thread frequency. Defined only for first thread allocno. */
153*38fd1498Szrj int thread_freq;
154*38fd1498Szrj };
155*38fd1498Szrj
156*38fd1498Szrj /* See above. */
157*38fd1498Szrj typedef struct allocno_color_data *allocno_color_data_t;
158*38fd1498Szrj
159*38fd1498Szrj /* Container for storing allocno data concerning coloring. */
160*38fd1498Szrj static allocno_color_data_t allocno_color_data;
161*38fd1498Szrj
162*38fd1498Szrj /* Macro to access the data concerning coloring. */
163*38fd1498Szrj #define ALLOCNO_COLOR_DATA(a) ((allocno_color_data_t) ALLOCNO_ADD_DATA (a))
164*38fd1498Szrj
165*38fd1498Szrj /* Used for finding allocno colorability to exclude repeated allocno
166*38fd1498Szrj processing and for updating preferencing to exclude repeated
167*38fd1498Szrj allocno processing during assignment. */
168*38fd1498Szrj static int curr_allocno_process;
169*38fd1498Szrj
170*38fd1498Szrj /* This file contains code for regional graph coloring, spill/restore
171*38fd1498Szrj code placement optimization, and code helping the reload pass to do
172*38fd1498Szrj a better job. */
173*38fd1498Szrj
174*38fd1498Szrj /* Bitmap of allocnos which should be colored. */
175*38fd1498Szrj static bitmap coloring_allocno_bitmap;
176*38fd1498Szrj
177*38fd1498Szrj /* Bitmap of allocnos which should be taken into account during
178*38fd1498Szrj coloring. In general case it contains allocnos from
179*38fd1498Szrj coloring_allocno_bitmap plus other already colored conflicting
180*38fd1498Szrj allocnos. */
181*38fd1498Szrj static bitmap consideration_allocno_bitmap;
182*38fd1498Szrj
183*38fd1498Szrj /* All allocnos sorted according their priorities. */
184*38fd1498Szrj static ira_allocno_t *sorted_allocnos;
185*38fd1498Szrj
186*38fd1498Szrj /* Vec representing the stack of allocnos used during coloring. */
187*38fd1498Szrj static vec<ira_allocno_t> allocno_stack_vec;
188*38fd1498Szrj
189*38fd1498Szrj /* Helper for qsort comparison callbacks - return a positive integer if
190*38fd1498Szrj X > Y, or a negative value otherwise. Use a conditional expression
191*38fd1498Szrj instead of a difference computation to insulate from possible overflow
192*38fd1498Szrj issues, e.g. X - Y < 0 for some X > 0 and Y < 0. */
193*38fd1498Szrj #define SORTGT(x,y) (((x) > (y)) ? 1 : -1)
194*38fd1498Szrj
195*38fd1498Szrj
196*38fd1498Szrj
197*38fd1498Szrj /* Definition of vector of allocno hard registers. */
198*38fd1498Szrj
199*38fd1498Szrj /* Vector of unique allocno hard registers. */
200*38fd1498Szrj static vec<allocno_hard_regs_t> allocno_hard_regs_vec;
201*38fd1498Szrj
202*38fd1498Szrj struct allocno_hard_regs_hasher : nofree_ptr_hash <allocno_hard_regs>
203*38fd1498Szrj {
204*38fd1498Szrj static inline hashval_t hash (const allocno_hard_regs *);
205*38fd1498Szrj static inline bool equal (const allocno_hard_regs *,
206*38fd1498Szrj const allocno_hard_regs *);
207*38fd1498Szrj };
208*38fd1498Szrj
209*38fd1498Szrj /* Returns hash value for allocno hard registers V. */
210*38fd1498Szrj inline hashval_t
hash(const allocno_hard_regs * hv)211*38fd1498Szrj allocno_hard_regs_hasher::hash (const allocno_hard_regs *hv)
212*38fd1498Szrj {
213*38fd1498Szrj return iterative_hash (&hv->set, sizeof (HARD_REG_SET), 0);
214*38fd1498Szrj }
215*38fd1498Szrj
216*38fd1498Szrj /* Compares allocno hard registers V1 and V2. */
217*38fd1498Szrj inline bool
equal(const allocno_hard_regs * hv1,const allocno_hard_regs * hv2)218*38fd1498Szrj allocno_hard_regs_hasher::equal (const allocno_hard_regs *hv1,
219*38fd1498Szrj const allocno_hard_regs *hv2)
220*38fd1498Szrj {
221*38fd1498Szrj return hard_reg_set_equal_p (hv1->set, hv2->set);
222*38fd1498Szrj }
223*38fd1498Szrj
224*38fd1498Szrj /* Hash table of unique allocno hard registers. */
225*38fd1498Szrj static hash_table<allocno_hard_regs_hasher> *allocno_hard_regs_htab;
226*38fd1498Szrj
227*38fd1498Szrj /* Return allocno hard registers in the hash table equal to HV. */
228*38fd1498Szrj static allocno_hard_regs_t
find_hard_regs(allocno_hard_regs_t hv)229*38fd1498Szrj find_hard_regs (allocno_hard_regs_t hv)
230*38fd1498Szrj {
231*38fd1498Szrj return allocno_hard_regs_htab->find (hv);
232*38fd1498Szrj }
233*38fd1498Szrj
234*38fd1498Szrj /* Insert allocno hard registers HV in the hash table (if it is not
235*38fd1498Szrj there yet) and return the value which in the table. */
236*38fd1498Szrj static allocno_hard_regs_t
insert_hard_regs(allocno_hard_regs_t hv)237*38fd1498Szrj insert_hard_regs (allocno_hard_regs_t hv)
238*38fd1498Szrj {
239*38fd1498Szrj allocno_hard_regs **slot = allocno_hard_regs_htab->find_slot (hv, INSERT);
240*38fd1498Szrj
241*38fd1498Szrj if (*slot == NULL)
242*38fd1498Szrj *slot = hv;
243*38fd1498Szrj return *slot;
244*38fd1498Szrj }
245*38fd1498Szrj
246*38fd1498Szrj /* Initialize data concerning allocno hard registers. */
247*38fd1498Szrj static void
init_allocno_hard_regs(void)248*38fd1498Szrj init_allocno_hard_regs (void)
249*38fd1498Szrj {
250*38fd1498Szrj allocno_hard_regs_vec.create (200);
251*38fd1498Szrj allocno_hard_regs_htab
252*38fd1498Szrj = new hash_table<allocno_hard_regs_hasher> (200);
253*38fd1498Szrj }
254*38fd1498Szrj
255*38fd1498Szrj /* Add (or update info about) allocno hard registers with SET and
256*38fd1498Szrj COST. */
257*38fd1498Szrj static allocno_hard_regs_t
add_allocno_hard_regs(HARD_REG_SET set,int64_t cost)258*38fd1498Szrj add_allocno_hard_regs (HARD_REG_SET set, int64_t cost)
259*38fd1498Szrj {
260*38fd1498Szrj struct allocno_hard_regs temp;
261*38fd1498Szrj allocno_hard_regs_t hv;
262*38fd1498Szrj
263*38fd1498Szrj gcc_assert (! hard_reg_set_empty_p (set));
264*38fd1498Szrj COPY_HARD_REG_SET (temp.set, set);
265*38fd1498Szrj if ((hv = find_hard_regs (&temp)) != NULL)
266*38fd1498Szrj hv->cost += cost;
267*38fd1498Szrj else
268*38fd1498Szrj {
269*38fd1498Szrj hv = ((struct allocno_hard_regs *)
270*38fd1498Szrj ira_allocate (sizeof (struct allocno_hard_regs)));
271*38fd1498Szrj COPY_HARD_REG_SET (hv->set, set);
272*38fd1498Szrj hv->cost = cost;
273*38fd1498Szrj allocno_hard_regs_vec.safe_push (hv);
274*38fd1498Szrj insert_hard_regs (hv);
275*38fd1498Szrj }
276*38fd1498Szrj return hv;
277*38fd1498Szrj }
278*38fd1498Szrj
279*38fd1498Szrj /* Finalize data concerning allocno hard registers. */
280*38fd1498Szrj static void
finish_allocno_hard_regs(void)281*38fd1498Szrj finish_allocno_hard_regs (void)
282*38fd1498Szrj {
283*38fd1498Szrj int i;
284*38fd1498Szrj allocno_hard_regs_t hv;
285*38fd1498Szrj
286*38fd1498Szrj for (i = 0;
287*38fd1498Szrj allocno_hard_regs_vec.iterate (i, &hv);
288*38fd1498Szrj i++)
289*38fd1498Szrj ira_free (hv);
290*38fd1498Szrj delete allocno_hard_regs_htab;
291*38fd1498Szrj allocno_hard_regs_htab = NULL;
292*38fd1498Szrj allocno_hard_regs_vec.release ();
293*38fd1498Szrj }
294*38fd1498Szrj
295*38fd1498Szrj /* Sort hard regs according to their frequency of usage. */
296*38fd1498Szrj static int
allocno_hard_regs_compare(const void * v1p,const void * v2p)297*38fd1498Szrj allocno_hard_regs_compare (const void *v1p, const void *v2p)
298*38fd1498Szrj {
299*38fd1498Szrj allocno_hard_regs_t hv1 = *(const allocno_hard_regs_t *) v1p;
300*38fd1498Szrj allocno_hard_regs_t hv2 = *(const allocno_hard_regs_t *) v2p;
301*38fd1498Szrj
302*38fd1498Szrj if (hv2->cost > hv1->cost)
303*38fd1498Szrj return 1;
304*38fd1498Szrj else if (hv2->cost < hv1->cost)
305*38fd1498Szrj return -1;
306*38fd1498Szrj return SORTGT (allocno_hard_regs_hasher::hash(hv2), allocno_hard_regs_hasher::hash(hv1));
307*38fd1498Szrj }
308*38fd1498Szrj
309*38fd1498Szrj
310*38fd1498Szrj
311*38fd1498Szrj /* Used for finding a common ancestor of two allocno hard registers
312*38fd1498Szrj nodes in the forest. We use the current value of
313*38fd1498Szrj 'node_check_tick' to mark all nodes from one node to the top and
314*38fd1498Szrj then walking up from another node until we find a marked node.
315*38fd1498Szrj
316*38fd1498Szrj It is also used to figure out allocno colorability as a mark that
317*38fd1498Szrj we already reset value of member 'conflict_size' for the forest
318*38fd1498Szrj node corresponding to the processed allocno. */
319*38fd1498Szrj static int node_check_tick;
320*38fd1498Szrj
321*38fd1498Szrj /* Roots of the forest containing hard register sets can be assigned
322*38fd1498Szrj to allocnos. */
323*38fd1498Szrj static allocno_hard_regs_node_t hard_regs_roots;
324*38fd1498Szrj
325*38fd1498Szrj /* Definition of vector of allocno hard register nodes. */
326*38fd1498Szrj
327*38fd1498Szrj /* Vector used to create the forest. */
328*38fd1498Szrj static vec<allocno_hard_regs_node_t> hard_regs_node_vec;
329*38fd1498Szrj
330*38fd1498Szrj /* Create and return allocno hard registers node containing allocno
331*38fd1498Szrj hard registers HV. */
332*38fd1498Szrj static allocno_hard_regs_node_t
create_new_allocno_hard_regs_node(allocno_hard_regs_t hv)333*38fd1498Szrj create_new_allocno_hard_regs_node (allocno_hard_regs_t hv)
334*38fd1498Szrj {
335*38fd1498Szrj allocno_hard_regs_node_t new_node;
336*38fd1498Szrj
337*38fd1498Szrj new_node = ((struct allocno_hard_regs_node *)
338*38fd1498Szrj ira_allocate (sizeof (struct allocno_hard_regs_node)));
339*38fd1498Szrj new_node->check = 0;
340*38fd1498Szrj new_node->hard_regs = hv;
341*38fd1498Szrj new_node->hard_regs_num = hard_reg_set_size (hv->set);
342*38fd1498Szrj new_node->first = NULL;
343*38fd1498Szrj new_node->used_p = false;
344*38fd1498Szrj return new_node;
345*38fd1498Szrj }
346*38fd1498Szrj
347*38fd1498Szrj /* Add allocno hard registers node NEW_NODE to the forest on its level
348*38fd1498Szrj given by ROOTS. */
349*38fd1498Szrj static void
add_new_allocno_hard_regs_node_to_forest(allocno_hard_regs_node_t * roots,allocno_hard_regs_node_t new_node)350*38fd1498Szrj add_new_allocno_hard_regs_node_to_forest (allocno_hard_regs_node_t *roots,
351*38fd1498Szrj allocno_hard_regs_node_t new_node)
352*38fd1498Szrj {
353*38fd1498Szrj new_node->next = *roots;
354*38fd1498Szrj if (new_node->next != NULL)
355*38fd1498Szrj new_node->next->prev = new_node;
356*38fd1498Szrj new_node->prev = NULL;
357*38fd1498Szrj *roots = new_node;
358*38fd1498Szrj }
359*38fd1498Szrj
360*38fd1498Szrj /* Add allocno hard registers HV (or its best approximation if it is
361*38fd1498Szrj not possible) to the forest on its level given by ROOTS. */
362*38fd1498Szrj static void
add_allocno_hard_regs_to_forest(allocno_hard_regs_node_t * roots,allocno_hard_regs_t hv)363*38fd1498Szrj add_allocno_hard_regs_to_forest (allocno_hard_regs_node_t *roots,
364*38fd1498Szrj allocno_hard_regs_t hv)
365*38fd1498Szrj {
366*38fd1498Szrj unsigned int i, start;
367*38fd1498Szrj allocno_hard_regs_node_t node, prev, new_node;
368*38fd1498Szrj HARD_REG_SET temp_set;
369*38fd1498Szrj allocno_hard_regs_t hv2;
370*38fd1498Szrj
371*38fd1498Szrj start = hard_regs_node_vec.length ();
372*38fd1498Szrj for (node = *roots; node != NULL; node = node->next)
373*38fd1498Szrj {
374*38fd1498Szrj if (hard_reg_set_equal_p (hv->set, node->hard_regs->set))
375*38fd1498Szrj return;
376*38fd1498Szrj if (hard_reg_set_subset_p (hv->set, node->hard_regs->set))
377*38fd1498Szrj {
378*38fd1498Szrj add_allocno_hard_regs_to_forest (&node->first, hv);
379*38fd1498Szrj return;
380*38fd1498Szrj }
381*38fd1498Szrj if (hard_reg_set_subset_p (node->hard_regs->set, hv->set))
382*38fd1498Szrj hard_regs_node_vec.safe_push (node);
383*38fd1498Szrj else if (hard_reg_set_intersect_p (hv->set, node->hard_regs->set))
384*38fd1498Szrj {
385*38fd1498Szrj COPY_HARD_REG_SET (temp_set, hv->set);
386*38fd1498Szrj AND_HARD_REG_SET (temp_set, node->hard_regs->set);
387*38fd1498Szrj hv2 = add_allocno_hard_regs (temp_set, hv->cost);
388*38fd1498Szrj add_allocno_hard_regs_to_forest (&node->first, hv2);
389*38fd1498Szrj }
390*38fd1498Szrj }
391*38fd1498Szrj if (hard_regs_node_vec.length ()
392*38fd1498Szrj > start + 1)
393*38fd1498Szrj {
394*38fd1498Szrj /* Create a new node which contains nodes in hard_regs_node_vec. */
395*38fd1498Szrj CLEAR_HARD_REG_SET (temp_set);
396*38fd1498Szrj for (i = start;
397*38fd1498Szrj i < hard_regs_node_vec.length ();
398*38fd1498Szrj i++)
399*38fd1498Szrj {
400*38fd1498Szrj node = hard_regs_node_vec[i];
401*38fd1498Szrj IOR_HARD_REG_SET (temp_set, node->hard_regs->set);
402*38fd1498Szrj }
403*38fd1498Szrj hv = add_allocno_hard_regs (temp_set, hv->cost);
404*38fd1498Szrj new_node = create_new_allocno_hard_regs_node (hv);
405*38fd1498Szrj prev = NULL;
406*38fd1498Szrj for (i = start;
407*38fd1498Szrj i < hard_regs_node_vec.length ();
408*38fd1498Szrj i++)
409*38fd1498Szrj {
410*38fd1498Szrj node = hard_regs_node_vec[i];
411*38fd1498Szrj if (node->prev == NULL)
412*38fd1498Szrj *roots = node->next;
413*38fd1498Szrj else
414*38fd1498Szrj node->prev->next = node->next;
415*38fd1498Szrj if (node->next != NULL)
416*38fd1498Szrj node->next->prev = node->prev;
417*38fd1498Szrj if (prev == NULL)
418*38fd1498Szrj new_node->first = node;
419*38fd1498Szrj else
420*38fd1498Szrj prev->next = node;
421*38fd1498Szrj node->prev = prev;
422*38fd1498Szrj node->next = NULL;
423*38fd1498Szrj prev = node;
424*38fd1498Szrj }
425*38fd1498Szrj add_new_allocno_hard_regs_node_to_forest (roots, new_node);
426*38fd1498Szrj }
427*38fd1498Szrj hard_regs_node_vec.truncate (start);
428*38fd1498Szrj }
429*38fd1498Szrj
430*38fd1498Szrj /* Add allocno hard registers nodes starting with the forest level
431*38fd1498Szrj given by FIRST which contains biggest set inside SET. */
432*38fd1498Szrj static void
collect_allocno_hard_regs_cover(allocno_hard_regs_node_t first,HARD_REG_SET set)433*38fd1498Szrj collect_allocno_hard_regs_cover (allocno_hard_regs_node_t first,
434*38fd1498Szrj HARD_REG_SET set)
435*38fd1498Szrj {
436*38fd1498Szrj allocno_hard_regs_node_t node;
437*38fd1498Szrj
438*38fd1498Szrj ira_assert (first != NULL);
439*38fd1498Szrj for (node = first; node != NULL; node = node->next)
440*38fd1498Szrj if (hard_reg_set_subset_p (node->hard_regs->set, set))
441*38fd1498Szrj hard_regs_node_vec.safe_push (node);
442*38fd1498Szrj else if (hard_reg_set_intersect_p (set, node->hard_regs->set))
443*38fd1498Szrj collect_allocno_hard_regs_cover (node->first, set);
444*38fd1498Szrj }
445*38fd1498Szrj
446*38fd1498Szrj /* Set up field parent as PARENT in all allocno hard registers nodes
447*38fd1498Szrj in forest given by FIRST. */
448*38fd1498Szrj static void
setup_allocno_hard_regs_nodes_parent(allocno_hard_regs_node_t first,allocno_hard_regs_node_t parent)449*38fd1498Szrj setup_allocno_hard_regs_nodes_parent (allocno_hard_regs_node_t first,
450*38fd1498Szrj allocno_hard_regs_node_t parent)
451*38fd1498Szrj {
452*38fd1498Szrj allocno_hard_regs_node_t node;
453*38fd1498Szrj
454*38fd1498Szrj for (node = first; node != NULL; node = node->next)
455*38fd1498Szrj {
456*38fd1498Szrj node->parent = parent;
457*38fd1498Szrj setup_allocno_hard_regs_nodes_parent (node->first, node);
458*38fd1498Szrj }
459*38fd1498Szrj }
460*38fd1498Szrj
461*38fd1498Szrj /* Return allocno hard registers node which is a first common ancestor
462*38fd1498Szrj node of FIRST and SECOND in the forest. */
463*38fd1498Szrj static allocno_hard_regs_node_t
first_common_ancestor_node(allocno_hard_regs_node_t first,allocno_hard_regs_node_t second)464*38fd1498Szrj first_common_ancestor_node (allocno_hard_regs_node_t first,
465*38fd1498Szrj allocno_hard_regs_node_t second)
466*38fd1498Szrj {
467*38fd1498Szrj allocno_hard_regs_node_t node;
468*38fd1498Szrj
469*38fd1498Szrj node_check_tick++;
470*38fd1498Szrj for (node = first; node != NULL; node = node->parent)
471*38fd1498Szrj node->check = node_check_tick;
472*38fd1498Szrj for (node = second; node != NULL; node = node->parent)
473*38fd1498Szrj if (node->check == node_check_tick)
474*38fd1498Szrj return node;
475*38fd1498Szrj return first_common_ancestor_node (second, first);
476*38fd1498Szrj }
477*38fd1498Szrj
478*38fd1498Szrj /* Print hard reg set SET to F. */
479*38fd1498Szrj static void
print_hard_reg_set(FILE * f,HARD_REG_SET set,bool new_line_p)480*38fd1498Szrj print_hard_reg_set (FILE *f, HARD_REG_SET set, bool new_line_p)
481*38fd1498Szrj {
482*38fd1498Szrj int i, start;
483*38fd1498Szrj
484*38fd1498Szrj for (start = -1, i = 0; i < FIRST_PSEUDO_REGISTER; i++)
485*38fd1498Szrj {
486*38fd1498Szrj if (TEST_HARD_REG_BIT (set, i))
487*38fd1498Szrj {
488*38fd1498Szrj if (i == 0 || ! TEST_HARD_REG_BIT (set, i - 1))
489*38fd1498Szrj start = i;
490*38fd1498Szrj }
491*38fd1498Szrj if (start >= 0
492*38fd1498Szrj && (i == FIRST_PSEUDO_REGISTER - 1 || ! TEST_HARD_REG_BIT (set, i)))
493*38fd1498Szrj {
494*38fd1498Szrj if (start == i - 1)
495*38fd1498Szrj fprintf (f, " %d", start);
496*38fd1498Szrj else if (start == i - 2)
497*38fd1498Szrj fprintf (f, " %d %d", start, start + 1);
498*38fd1498Szrj else
499*38fd1498Szrj fprintf (f, " %d-%d", start, i - 1);
500*38fd1498Szrj start = -1;
501*38fd1498Szrj }
502*38fd1498Szrj }
503*38fd1498Szrj if (new_line_p)
504*38fd1498Szrj fprintf (f, "\n");
505*38fd1498Szrj }
506*38fd1498Szrj
507*38fd1498Szrj /* Print allocno hard register subforest given by ROOTS and its LEVEL
508*38fd1498Szrj to F. */
509*38fd1498Szrj static void
print_hard_regs_subforest(FILE * f,allocno_hard_regs_node_t roots,int level)510*38fd1498Szrj print_hard_regs_subforest (FILE *f, allocno_hard_regs_node_t roots,
511*38fd1498Szrj int level)
512*38fd1498Szrj {
513*38fd1498Szrj int i;
514*38fd1498Szrj allocno_hard_regs_node_t node;
515*38fd1498Szrj
516*38fd1498Szrj for (node = roots; node != NULL; node = node->next)
517*38fd1498Szrj {
518*38fd1498Szrj fprintf (f, " ");
519*38fd1498Szrj for (i = 0; i < level * 2; i++)
520*38fd1498Szrj fprintf (f, " ");
521*38fd1498Szrj fprintf (f, "%d:(", node->preorder_num);
522*38fd1498Szrj print_hard_reg_set (f, node->hard_regs->set, false);
523*38fd1498Szrj fprintf (f, ")@%" PRId64"\n", node->hard_regs->cost);
524*38fd1498Szrj print_hard_regs_subforest (f, node->first, level + 1);
525*38fd1498Szrj }
526*38fd1498Szrj }
527*38fd1498Szrj
528*38fd1498Szrj /* Print the allocno hard register forest to F. */
529*38fd1498Szrj static void
print_hard_regs_forest(FILE * f)530*38fd1498Szrj print_hard_regs_forest (FILE *f)
531*38fd1498Szrj {
532*38fd1498Szrj fprintf (f, " Hard reg set forest:\n");
533*38fd1498Szrj print_hard_regs_subforest (f, hard_regs_roots, 1);
534*38fd1498Szrj }
535*38fd1498Szrj
536*38fd1498Szrj /* Print the allocno hard register forest to stderr. */
537*38fd1498Szrj void
ira_debug_hard_regs_forest(void)538*38fd1498Szrj ira_debug_hard_regs_forest (void)
539*38fd1498Szrj {
540*38fd1498Szrj print_hard_regs_forest (stderr);
541*38fd1498Szrj }
542*38fd1498Szrj
543*38fd1498Szrj /* Remove unused allocno hard registers nodes from forest given by its
544*38fd1498Szrj *ROOTS. */
545*38fd1498Szrj static void
remove_unused_allocno_hard_regs_nodes(allocno_hard_regs_node_t * roots)546*38fd1498Szrj remove_unused_allocno_hard_regs_nodes (allocno_hard_regs_node_t *roots)
547*38fd1498Szrj {
548*38fd1498Szrj allocno_hard_regs_node_t node, prev, next, last;
549*38fd1498Szrj
550*38fd1498Szrj for (prev = NULL, node = *roots; node != NULL; node = next)
551*38fd1498Szrj {
552*38fd1498Szrj next = node->next;
553*38fd1498Szrj if (node->used_p)
554*38fd1498Szrj {
555*38fd1498Szrj remove_unused_allocno_hard_regs_nodes (&node->first);
556*38fd1498Szrj prev = node;
557*38fd1498Szrj }
558*38fd1498Szrj else
559*38fd1498Szrj {
560*38fd1498Szrj for (last = node->first;
561*38fd1498Szrj last != NULL && last->next != NULL;
562*38fd1498Szrj last = last->next)
563*38fd1498Szrj ;
564*38fd1498Szrj if (last != NULL)
565*38fd1498Szrj {
566*38fd1498Szrj if (prev == NULL)
567*38fd1498Szrj *roots = node->first;
568*38fd1498Szrj else
569*38fd1498Szrj prev->next = node->first;
570*38fd1498Szrj if (next != NULL)
571*38fd1498Szrj next->prev = last;
572*38fd1498Szrj last->next = next;
573*38fd1498Szrj next = node->first;
574*38fd1498Szrj }
575*38fd1498Szrj else
576*38fd1498Szrj {
577*38fd1498Szrj if (prev == NULL)
578*38fd1498Szrj *roots = next;
579*38fd1498Szrj else
580*38fd1498Szrj prev->next = next;
581*38fd1498Szrj if (next != NULL)
582*38fd1498Szrj next->prev = prev;
583*38fd1498Szrj }
584*38fd1498Szrj ira_free (node);
585*38fd1498Szrj }
586*38fd1498Szrj }
587*38fd1498Szrj }
588*38fd1498Szrj
589*38fd1498Szrj /* Set up fields preorder_num starting with START_NUM in all allocno
590*38fd1498Szrj hard registers nodes in forest given by FIRST. Return biggest set
591*38fd1498Szrj PREORDER_NUM increased by 1. */
592*38fd1498Szrj static int
enumerate_allocno_hard_regs_nodes(allocno_hard_regs_node_t first,allocno_hard_regs_node_t parent,int start_num)593*38fd1498Szrj enumerate_allocno_hard_regs_nodes (allocno_hard_regs_node_t first,
594*38fd1498Szrj allocno_hard_regs_node_t parent,
595*38fd1498Szrj int start_num)
596*38fd1498Szrj {
597*38fd1498Szrj allocno_hard_regs_node_t node;
598*38fd1498Szrj
599*38fd1498Szrj for (node = first; node != NULL; node = node->next)
600*38fd1498Szrj {
601*38fd1498Szrj node->preorder_num = start_num++;
602*38fd1498Szrj node->parent = parent;
603*38fd1498Szrj start_num = enumerate_allocno_hard_regs_nodes (node->first, node,
604*38fd1498Szrj start_num);
605*38fd1498Szrj }
606*38fd1498Szrj return start_num;
607*38fd1498Szrj }
608*38fd1498Szrj
609*38fd1498Szrj /* Number of allocno hard registers nodes in the forest. */
610*38fd1498Szrj static int allocno_hard_regs_nodes_num;
611*38fd1498Szrj
612*38fd1498Szrj /* Table preorder number of allocno hard registers node in the forest
613*38fd1498Szrj -> the allocno hard registers node. */
614*38fd1498Szrj static allocno_hard_regs_node_t *allocno_hard_regs_nodes;
615*38fd1498Szrj
616*38fd1498Szrj /* See below. */
617*38fd1498Szrj typedef struct allocno_hard_regs_subnode *allocno_hard_regs_subnode_t;
618*38fd1498Szrj
619*38fd1498Szrj /* The structure is used to describes all subnodes (not only immediate
620*38fd1498Szrj ones) in the mentioned above tree for given allocno hard register
621*38fd1498Szrj node. The usage of such data accelerates calculation of
622*38fd1498Szrj colorability of given allocno. */
623*38fd1498Szrj struct allocno_hard_regs_subnode
624*38fd1498Szrj {
625*38fd1498Szrj /* The conflict size of conflicting allocnos whose hard register
626*38fd1498Szrj sets are equal sets (plus supersets if given node is given
627*38fd1498Szrj allocno hard registers node) of one in the given node. */
628*38fd1498Szrj int left_conflict_size;
629*38fd1498Szrj /* The summary conflict size of conflicting allocnos whose hard
630*38fd1498Szrj register sets are strict subsets of one in the given node.
631*38fd1498Szrj Overall conflict size is
632*38fd1498Szrj left_conflict_subnodes_size
633*38fd1498Szrj + MIN (max_node_impact - left_conflict_subnodes_size,
634*38fd1498Szrj left_conflict_size)
635*38fd1498Szrj */
636*38fd1498Szrj short left_conflict_subnodes_size;
637*38fd1498Szrj short max_node_impact;
638*38fd1498Szrj };
639*38fd1498Szrj
640*38fd1498Szrj /* Container for hard regs subnodes of all allocnos. */
641*38fd1498Szrj static allocno_hard_regs_subnode_t allocno_hard_regs_subnodes;
642*38fd1498Szrj
643*38fd1498Szrj /* Table (preorder number of allocno hard registers node in the
644*38fd1498Szrj forest, preorder number of allocno hard registers subnode) -> index
645*38fd1498Szrj of the subnode relative to the node. -1 if it is not a
646*38fd1498Szrj subnode. */
647*38fd1498Szrj static int *allocno_hard_regs_subnode_index;
648*38fd1498Szrj
649*38fd1498Szrj /* Setup arrays ALLOCNO_HARD_REGS_NODES and
650*38fd1498Szrj ALLOCNO_HARD_REGS_SUBNODE_INDEX. */
651*38fd1498Szrj static void
setup_allocno_hard_regs_subnode_index(allocno_hard_regs_node_t first)652*38fd1498Szrj setup_allocno_hard_regs_subnode_index (allocno_hard_regs_node_t first)
653*38fd1498Szrj {
654*38fd1498Szrj allocno_hard_regs_node_t node, parent;
655*38fd1498Szrj int index;
656*38fd1498Szrj
657*38fd1498Szrj for (node = first; node != NULL; node = node->next)
658*38fd1498Szrj {
659*38fd1498Szrj allocno_hard_regs_nodes[node->preorder_num] = node;
660*38fd1498Szrj for (parent = node; parent != NULL; parent = parent->parent)
661*38fd1498Szrj {
662*38fd1498Szrj index = parent->preorder_num * allocno_hard_regs_nodes_num;
663*38fd1498Szrj allocno_hard_regs_subnode_index[index + node->preorder_num]
664*38fd1498Szrj = node->preorder_num - parent->preorder_num;
665*38fd1498Szrj }
666*38fd1498Szrj setup_allocno_hard_regs_subnode_index (node->first);
667*38fd1498Szrj }
668*38fd1498Szrj }
669*38fd1498Szrj
670*38fd1498Szrj /* Count all allocno hard registers nodes in tree ROOT. */
671*38fd1498Szrj static int
get_allocno_hard_regs_subnodes_num(allocno_hard_regs_node_t root)672*38fd1498Szrj get_allocno_hard_regs_subnodes_num (allocno_hard_regs_node_t root)
673*38fd1498Szrj {
674*38fd1498Szrj int len = 1;
675*38fd1498Szrj
676*38fd1498Szrj for (root = root->first; root != NULL; root = root->next)
677*38fd1498Szrj len += get_allocno_hard_regs_subnodes_num (root);
678*38fd1498Szrj return len;
679*38fd1498Szrj }
680*38fd1498Szrj
681*38fd1498Szrj /* Build the forest of allocno hard registers nodes and assign each
682*38fd1498Szrj allocno a node from the forest. */
683*38fd1498Szrj static void
form_allocno_hard_regs_nodes_forest(void)684*38fd1498Szrj form_allocno_hard_regs_nodes_forest (void)
685*38fd1498Szrj {
686*38fd1498Szrj unsigned int i, j, size, len;
687*38fd1498Szrj int start;
688*38fd1498Szrj ira_allocno_t a;
689*38fd1498Szrj allocno_hard_regs_t hv;
690*38fd1498Szrj bitmap_iterator bi;
691*38fd1498Szrj HARD_REG_SET temp;
692*38fd1498Szrj allocno_hard_regs_node_t node, allocno_hard_regs_node;
693*38fd1498Szrj allocno_color_data_t allocno_data;
694*38fd1498Szrj
695*38fd1498Szrj node_check_tick = 0;
696*38fd1498Szrj init_allocno_hard_regs ();
697*38fd1498Szrj hard_regs_roots = NULL;
698*38fd1498Szrj hard_regs_node_vec.create (100);
699*38fd1498Szrj for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
700*38fd1498Szrj if (! TEST_HARD_REG_BIT (ira_no_alloc_regs, i))
701*38fd1498Szrj {
702*38fd1498Szrj CLEAR_HARD_REG_SET (temp);
703*38fd1498Szrj SET_HARD_REG_BIT (temp, i);
704*38fd1498Szrj hv = add_allocno_hard_regs (temp, 0);
705*38fd1498Szrj node = create_new_allocno_hard_regs_node (hv);
706*38fd1498Szrj add_new_allocno_hard_regs_node_to_forest (&hard_regs_roots, node);
707*38fd1498Szrj }
708*38fd1498Szrj start = allocno_hard_regs_vec.length ();
709*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
710*38fd1498Szrj {
711*38fd1498Szrj a = ira_allocnos[i];
712*38fd1498Szrj allocno_data = ALLOCNO_COLOR_DATA (a);
713*38fd1498Szrj
714*38fd1498Szrj if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
715*38fd1498Szrj continue;
716*38fd1498Szrj hv = (add_allocno_hard_regs
717*38fd1498Szrj (allocno_data->profitable_hard_regs,
718*38fd1498Szrj ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a)));
719*38fd1498Szrj }
720*38fd1498Szrj SET_HARD_REG_SET (temp);
721*38fd1498Szrj AND_COMPL_HARD_REG_SET (temp, ira_no_alloc_regs);
722*38fd1498Szrj add_allocno_hard_regs (temp, 0);
723*38fd1498Szrj qsort (allocno_hard_regs_vec.address () + start,
724*38fd1498Szrj allocno_hard_regs_vec.length () - start,
725*38fd1498Szrj sizeof (allocno_hard_regs_t), allocno_hard_regs_compare);
726*38fd1498Szrj for (i = start;
727*38fd1498Szrj allocno_hard_regs_vec.iterate (i, &hv);
728*38fd1498Szrj i++)
729*38fd1498Szrj {
730*38fd1498Szrj add_allocno_hard_regs_to_forest (&hard_regs_roots, hv);
731*38fd1498Szrj ira_assert (hard_regs_node_vec.length () == 0);
732*38fd1498Szrj }
733*38fd1498Szrj /* We need to set up parent fields for right work of
734*38fd1498Szrj first_common_ancestor_node. */
735*38fd1498Szrj setup_allocno_hard_regs_nodes_parent (hard_regs_roots, NULL);
736*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
737*38fd1498Szrj {
738*38fd1498Szrj a = ira_allocnos[i];
739*38fd1498Szrj allocno_data = ALLOCNO_COLOR_DATA (a);
740*38fd1498Szrj if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
741*38fd1498Szrj continue;
742*38fd1498Szrj hard_regs_node_vec.truncate (0);
743*38fd1498Szrj collect_allocno_hard_regs_cover (hard_regs_roots,
744*38fd1498Szrj allocno_data->profitable_hard_regs);
745*38fd1498Szrj allocno_hard_regs_node = NULL;
746*38fd1498Szrj for (j = 0; hard_regs_node_vec.iterate (j, &node); j++)
747*38fd1498Szrj allocno_hard_regs_node
748*38fd1498Szrj = (j == 0
749*38fd1498Szrj ? node
750*38fd1498Szrj : first_common_ancestor_node (node, allocno_hard_regs_node));
751*38fd1498Szrj /* That is a temporary storage. */
752*38fd1498Szrj allocno_hard_regs_node->used_p = true;
753*38fd1498Szrj allocno_data->hard_regs_node = allocno_hard_regs_node;
754*38fd1498Szrj }
755*38fd1498Szrj ira_assert (hard_regs_roots->next == NULL);
756*38fd1498Szrj hard_regs_roots->used_p = true;
757*38fd1498Szrj remove_unused_allocno_hard_regs_nodes (&hard_regs_roots);
758*38fd1498Szrj allocno_hard_regs_nodes_num
759*38fd1498Szrj = enumerate_allocno_hard_regs_nodes (hard_regs_roots, NULL, 0);
760*38fd1498Szrj allocno_hard_regs_nodes
761*38fd1498Szrj = ((allocno_hard_regs_node_t *)
762*38fd1498Szrj ira_allocate (allocno_hard_regs_nodes_num
763*38fd1498Szrj * sizeof (allocno_hard_regs_node_t)));
764*38fd1498Szrj size = allocno_hard_regs_nodes_num * allocno_hard_regs_nodes_num;
765*38fd1498Szrj allocno_hard_regs_subnode_index
766*38fd1498Szrj = (int *) ira_allocate (size * sizeof (int));
767*38fd1498Szrj for (i = 0; i < size; i++)
768*38fd1498Szrj allocno_hard_regs_subnode_index[i] = -1;
769*38fd1498Szrj setup_allocno_hard_regs_subnode_index (hard_regs_roots);
770*38fd1498Szrj start = 0;
771*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
772*38fd1498Szrj {
773*38fd1498Szrj a = ira_allocnos[i];
774*38fd1498Szrj allocno_data = ALLOCNO_COLOR_DATA (a);
775*38fd1498Szrj if (hard_reg_set_empty_p (allocno_data->profitable_hard_regs))
776*38fd1498Szrj continue;
777*38fd1498Szrj len = get_allocno_hard_regs_subnodes_num (allocno_data->hard_regs_node);
778*38fd1498Szrj allocno_data->hard_regs_subnodes_start = start;
779*38fd1498Szrj allocno_data->hard_regs_subnodes_num = len;
780*38fd1498Szrj start += len;
781*38fd1498Szrj }
782*38fd1498Szrj allocno_hard_regs_subnodes
783*38fd1498Szrj = ((allocno_hard_regs_subnode_t)
784*38fd1498Szrj ira_allocate (sizeof (struct allocno_hard_regs_subnode) * start));
785*38fd1498Szrj hard_regs_node_vec.release ();
786*38fd1498Szrj }
787*38fd1498Szrj
788*38fd1498Szrj /* Free tree of allocno hard registers nodes given by its ROOT. */
789*38fd1498Szrj static void
finish_allocno_hard_regs_nodes_tree(allocno_hard_regs_node_t root)790*38fd1498Szrj finish_allocno_hard_regs_nodes_tree (allocno_hard_regs_node_t root)
791*38fd1498Szrj {
792*38fd1498Szrj allocno_hard_regs_node_t child, next;
793*38fd1498Szrj
794*38fd1498Szrj for (child = root->first; child != NULL; child = next)
795*38fd1498Szrj {
796*38fd1498Szrj next = child->next;
797*38fd1498Szrj finish_allocno_hard_regs_nodes_tree (child);
798*38fd1498Szrj }
799*38fd1498Szrj ira_free (root);
800*38fd1498Szrj }
801*38fd1498Szrj
802*38fd1498Szrj /* Finish work with the forest of allocno hard registers nodes. */
803*38fd1498Szrj static void
finish_allocno_hard_regs_nodes_forest(void)804*38fd1498Szrj finish_allocno_hard_regs_nodes_forest (void)
805*38fd1498Szrj {
806*38fd1498Szrj allocno_hard_regs_node_t node, next;
807*38fd1498Szrj
808*38fd1498Szrj ira_free (allocno_hard_regs_subnodes);
809*38fd1498Szrj for (node = hard_regs_roots; node != NULL; node = next)
810*38fd1498Szrj {
811*38fd1498Szrj next = node->next;
812*38fd1498Szrj finish_allocno_hard_regs_nodes_tree (node);
813*38fd1498Szrj }
814*38fd1498Szrj ira_free (allocno_hard_regs_nodes);
815*38fd1498Szrj ira_free (allocno_hard_regs_subnode_index);
816*38fd1498Szrj finish_allocno_hard_regs ();
817*38fd1498Szrj }
818*38fd1498Szrj
819*38fd1498Szrj /* Set up left conflict sizes and left conflict subnodes sizes of hard
820*38fd1498Szrj registers subnodes of allocno A. Return TRUE if allocno A is
821*38fd1498Szrj trivially colorable. */
822*38fd1498Szrj static bool
setup_left_conflict_sizes_p(ira_allocno_t a)823*38fd1498Szrj setup_left_conflict_sizes_p (ira_allocno_t a)
824*38fd1498Szrj {
825*38fd1498Szrj int i, k, nobj, start;
826*38fd1498Szrj int conflict_size, left_conflict_subnodes_size, node_preorder_num;
827*38fd1498Szrj allocno_color_data_t data;
828*38fd1498Szrj HARD_REG_SET profitable_hard_regs;
829*38fd1498Szrj allocno_hard_regs_subnode_t subnodes;
830*38fd1498Szrj allocno_hard_regs_node_t node;
831*38fd1498Szrj HARD_REG_SET node_set;
832*38fd1498Szrj
833*38fd1498Szrj nobj = ALLOCNO_NUM_OBJECTS (a);
834*38fd1498Szrj data = ALLOCNO_COLOR_DATA (a);
835*38fd1498Szrj subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
836*38fd1498Szrj COPY_HARD_REG_SET (profitable_hard_regs, data->profitable_hard_regs);
837*38fd1498Szrj node = data->hard_regs_node;
838*38fd1498Szrj node_preorder_num = node->preorder_num;
839*38fd1498Szrj COPY_HARD_REG_SET (node_set, node->hard_regs->set);
840*38fd1498Szrj node_check_tick++;
841*38fd1498Szrj for (k = 0; k < nobj; k++)
842*38fd1498Szrj {
843*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, k);
844*38fd1498Szrj ira_object_t conflict_obj;
845*38fd1498Szrj ira_object_conflict_iterator oci;
846*38fd1498Szrj
847*38fd1498Szrj FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
848*38fd1498Szrj {
849*38fd1498Szrj int size;
850*38fd1498Szrj ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
851*38fd1498Szrj allocno_hard_regs_node_t conflict_node, temp_node;
852*38fd1498Szrj HARD_REG_SET conflict_node_set;
853*38fd1498Szrj allocno_color_data_t conflict_data;
854*38fd1498Szrj
855*38fd1498Szrj conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
856*38fd1498Szrj if (! ALLOCNO_COLOR_DATA (conflict_a)->in_graph_p
857*38fd1498Szrj || ! hard_reg_set_intersect_p (profitable_hard_regs,
858*38fd1498Szrj conflict_data
859*38fd1498Szrj ->profitable_hard_regs))
860*38fd1498Szrj continue;
861*38fd1498Szrj conflict_node = conflict_data->hard_regs_node;
862*38fd1498Szrj COPY_HARD_REG_SET (conflict_node_set, conflict_node->hard_regs->set);
863*38fd1498Szrj if (hard_reg_set_subset_p (node_set, conflict_node_set))
864*38fd1498Szrj temp_node = node;
865*38fd1498Szrj else
866*38fd1498Szrj {
867*38fd1498Szrj ira_assert (hard_reg_set_subset_p (conflict_node_set, node_set));
868*38fd1498Szrj temp_node = conflict_node;
869*38fd1498Szrj }
870*38fd1498Szrj if (temp_node->check != node_check_tick)
871*38fd1498Szrj {
872*38fd1498Szrj temp_node->check = node_check_tick;
873*38fd1498Szrj temp_node->conflict_size = 0;
874*38fd1498Szrj }
875*38fd1498Szrj size = (ira_reg_class_max_nregs
876*38fd1498Szrj [ALLOCNO_CLASS (conflict_a)][ALLOCNO_MODE (conflict_a)]);
877*38fd1498Szrj if (ALLOCNO_NUM_OBJECTS (conflict_a) > 1)
878*38fd1498Szrj /* We will deal with the subwords individually. */
879*38fd1498Szrj size = 1;
880*38fd1498Szrj temp_node->conflict_size += size;
881*38fd1498Szrj }
882*38fd1498Szrj }
883*38fd1498Szrj for (i = 0; i < data->hard_regs_subnodes_num; i++)
884*38fd1498Szrj {
885*38fd1498Szrj allocno_hard_regs_node_t temp_node;
886*38fd1498Szrj
887*38fd1498Szrj temp_node = allocno_hard_regs_nodes[i + node_preorder_num];
888*38fd1498Szrj ira_assert (temp_node->preorder_num == i + node_preorder_num);
889*38fd1498Szrj subnodes[i].left_conflict_size = (temp_node->check != node_check_tick
890*38fd1498Szrj ? 0 : temp_node->conflict_size);
891*38fd1498Szrj if (hard_reg_set_subset_p (temp_node->hard_regs->set,
892*38fd1498Szrj profitable_hard_regs))
893*38fd1498Szrj subnodes[i].max_node_impact = temp_node->hard_regs_num;
894*38fd1498Szrj else
895*38fd1498Szrj {
896*38fd1498Szrj HARD_REG_SET temp_set;
897*38fd1498Szrj int j, n, hard_regno;
898*38fd1498Szrj enum reg_class aclass;
899*38fd1498Szrj
900*38fd1498Szrj COPY_HARD_REG_SET (temp_set, temp_node->hard_regs->set);
901*38fd1498Szrj AND_HARD_REG_SET (temp_set, profitable_hard_regs);
902*38fd1498Szrj aclass = ALLOCNO_CLASS (a);
903*38fd1498Szrj for (n = 0, j = ira_class_hard_regs_num[aclass] - 1; j >= 0; j--)
904*38fd1498Szrj {
905*38fd1498Szrj hard_regno = ira_class_hard_regs[aclass][j];
906*38fd1498Szrj if (TEST_HARD_REG_BIT (temp_set, hard_regno))
907*38fd1498Szrj n++;
908*38fd1498Szrj }
909*38fd1498Szrj subnodes[i].max_node_impact = n;
910*38fd1498Szrj }
911*38fd1498Szrj subnodes[i].left_conflict_subnodes_size = 0;
912*38fd1498Szrj }
913*38fd1498Szrj start = node_preorder_num * allocno_hard_regs_nodes_num;
914*38fd1498Szrj for (i = data->hard_regs_subnodes_num - 1; i > 0; i--)
915*38fd1498Szrj {
916*38fd1498Szrj int size, parent_i;
917*38fd1498Szrj allocno_hard_regs_node_t parent;
918*38fd1498Szrj
919*38fd1498Szrj size = (subnodes[i].left_conflict_subnodes_size
920*38fd1498Szrj + MIN (subnodes[i].max_node_impact
921*38fd1498Szrj - subnodes[i].left_conflict_subnodes_size,
922*38fd1498Szrj subnodes[i].left_conflict_size));
923*38fd1498Szrj parent = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
924*38fd1498Szrj gcc_checking_assert(parent);
925*38fd1498Szrj parent_i
926*38fd1498Szrj = allocno_hard_regs_subnode_index[start + parent->preorder_num];
927*38fd1498Szrj gcc_checking_assert(parent_i >= 0);
928*38fd1498Szrj subnodes[parent_i].left_conflict_subnodes_size += size;
929*38fd1498Szrj }
930*38fd1498Szrj left_conflict_subnodes_size = subnodes[0].left_conflict_subnodes_size;
931*38fd1498Szrj conflict_size
932*38fd1498Szrj = (left_conflict_subnodes_size
933*38fd1498Szrj + MIN (subnodes[0].max_node_impact - left_conflict_subnodes_size,
934*38fd1498Szrj subnodes[0].left_conflict_size));
935*38fd1498Szrj conflict_size += ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)];
936*38fd1498Szrj data->colorable_p = conflict_size <= data->available_regs_num;
937*38fd1498Szrj return data->colorable_p;
938*38fd1498Szrj }
939*38fd1498Szrj
940*38fd1498Szrj /* Update left conflict sizes of hard registers subnodes of allocno A
941*38fd1498Szrj after removing allocno REMOVED_A with SIZE from the conflict graph.
942*38fd1498Szrj Return TRUE if A is trivially colorable. */
943*38fd1498Szrj static bool
update_left_conflict_sizes_p(ira_allocno_t a,ira_allocno_t removed_a,int size)944*38fd1498Szrj update_left_conflict_sizes_p (ira_allocno_t a,
945*38fd1498Szrj ira_allocno_t removed_a, int size)
946*38fd1498Szrj {
947*38fd1498Szrj int i, conflict_size, before_conflict_size, diff, start;
948*38fd1498Szrj int node_preorder_num, parent_i;
949*38fd1498Szrj allocno_hard_regs_node_t node, removed_node, parent;
950*38fd1498Szrj allocno_hard_regs_subnode_t subnodes;
951*38fd1498Szrj allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
952*38fd1498Szrj
953*38fd1498Szrj ira_assert (! data->colorable_p);
954*38fd1498Szrj node = data->hard_regs_node;
955*38fd1498Szrj node_preorder_num = node->preorder_num;
956*38fd1498Szrj removed_node = ALLOCNO_COLOR_DATA (removed_a)->hard_regs_node;
957*38fd1498Szrj ira_assert (hard_reg_set_subset_p (removed_node->hard_regs->set,
958*38fd1498Szrj node->hard_regs->set)
959*38fd1498Szrj || hard_reg_set_subset_p (node->hard_regs->set,
960*38fd1498Szrj removed_node->hard_regs->set));
961*38fd1498Szrj start = node_preorder_num * allocno_hard_regs_nodes_num;
962*38fd1498Szrj i = allocno_hard_regs_subnode_index[start + removed_node->preorder_num];
963*38fd1498Szrj if (i < 0)
964*38fd1498Szrj i = 0;
965*38fd1498Szrj subnodes = allocno_hard_regs_subnodes + data->hard_regs_subnodes_start;
966*38fd1498Szrj before_conflict_size
967*38fd1498Szrj = (subnodes[i].left_conflict_subnodes_size
968*38fd1498Szrj + MIN (subnodes[i].max_node_impact
969*38fd1498Szrj - subnodes[i].left_conflict_subnodes_size,
970*38fd1498Szrj subnodes[i].left_conflict_size));
971*38fd1498Szrj subnodes[i].left_conflict_size -= size;
972*38fd1498Szrj for (;;)
973*38fd1498Szrj {
974*38fd1498Szrj conflict_size
975*38fd1498Szrj = (subnodes[i].left_conflict_subnodes_size
976*38fd1498Szrj + MIN (subnodes[i].max_node_impact
977*38fd1498Szrj - subnodes[i].left_conflict_subnodes_size,
978*38fd1498Szrj subnodes[i].left_conflict_size));
979*38fd1498Szrj if ((diff = before_conflict_size - conflict_size) == 0)
980*38fd1498Szrj break;
981*38fd1498Szrj ira_assert (conflict_size < before_conflict_size);
982*38fd1498Szrj parent = allocno_hard_regs_nodes[i + node_preorder_num]->parent;
983*38fd1498Szrj if (parent == NULL)
984*38fd1498Szrj break;
985*38fd1498Szrj parent_i
986*38fd1498Szrj = allocno_hard_regs_subnode_index[start + parent->preorder_num];
987*38fd1498Szrj if (parent_i < 0)
988*38fd1498Szrj break;
989*38fd1498Szrj i = parent_i;
990*38fd1498Szrj before_conflict_size
991*38fd1498Szrj = (subnodes[i].left_conflict_subnodes_size
992*38fd1498Szrj + MIN (subnodes[i].max_node_impact
993*38fd1498Szrj - subnodes[i].left_conflict_subnodes_size,
994*38fd1498Szrj subnodes[i].left_conflict_size));
995*38fd1498Szrj subnodes[i].left_conflict_subnodes_size -= diff;
996*38fd1498Szrj }
997*38fd1498Szrj if (i != 0
998*38fd1498Szrj || (conflict_size
999*38fd1498Szrj + ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
1000*38fd1498Szrj > data->available_regs_num))
1001*38fd1498Szrj return false;
1002*38fd1498Szrj data->colorable_p = true;
1003*38fd1498Szrj return true;
1004*38fd1498Szrj }
1005*38fd1498Szrj
1006*38fd1498Szrj /* Return true if allocno A has empty profitable hard regs. */
1007*38fd1498Szrj static bool
empty_profitable_hard_regs(ira_allocno_t a)1008*38fd1498Szrj empty_profitable_hard_regs (ira_allocno_t a)
1009*38fd1498Szrj {
1010*38fd1498Szrj allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
1011*38fd1498Szrj
1012*38fd1498Szrj return hard_reg_set_empty_p (data->profitable_hard_regs);
1013*38fd1498Szrj }
1014*38fd1498Szrj
1015*38fd1498Szrj /* Set up profitable hard registers for each allocno being
1016*38fd1498Szrj colored. */
1017*38fd1498Szrj static void
setup_profitable_hard_regs(void)1018*38fd1498Szrj setup_profitable_hard_regs (void)
1019*38fd1498Szrj {
1020*38fd1498Szrj unsigned int i;
1021*38fd1498Szrj int j, k, nobj, hard_regno, nregs, class_size;
1022*38fd1498Szrj ira_allocno_t a;
1023*38fd1498Szrj bitmap_iterator bi;
1024*38fd1498Szrj enum reg_class aclass;
1025*38fd1498Szrj machine_mode mode;
1026*38fd1498Szrj allocno_color_data_t data;
1027*38fd1498Szrj
1028*38fd1498Szrj /* Initial set up from allocno classes and explicitly conflicting
1029*38fd1498Szrj hard regs. */
1030*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1031*38fd1498Szrj {
1032*38fd1498Szrj a = ira_allocnos[i];
1033*38fd1498Szrj if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS)
1034*38fd1498Szrj continue;
1035*38fd1498Szrj data = ALLOCNO_COLOR_DATA (a);
1036*38fd1498Szrj if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL
1037*38fd1498Szrj && ALLOCNO_CLASS_COST (a) > ALLOCNO_MEMORY_COST (a)
1038*38fd1498Szrj /* Do not empty profitable regs for static chain pointer
1039*38fd1498Szrj pseudo when non-local goto is used. */
1040*38fd1498Szrj && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1041*38fd1498Szrj CLEAR_HARD_REG_SET (data->profitable_hard_regs);
1042*38fd1498Szrj else
1043*38fd1498Szrj {
1044*38fd1498Szrj mode = ALLOCNO_MODE (a);
1045*38fd1498Szrj COPY_HARD_REG_SET (data->profitable_hard_regs,
1046*38fd1498Szrj ira_useful_class_mode_regs[aclass][mode]);
1047*38fd1498Szrj nobj = ALLOCNO_NUM_OBJECTS (a);
1048*38fd1498Szrj for (k = 0; k < nobj; k++)
1049*38fd1498Szrj {
1050*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, k);
1051*38fd1498Szrj
1052*38fd1498Szrj AND_COMPL_HARD_REG_SET (data->profitable_hard_regs,
1053*38fd1498Szrj OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
1054*38fd1498Szrj }
1055*38fd1498Szrj }
1056*38fd1498Szrj }
1057*38fd1498Szrj /* Exclude hard regs already assigned for conflicting objects. */
1058*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, i, bi)
1059*38fd1498Szrj {
1060*38fd1498Szrj a = ira_allocnos[i];
1061*38fd1498Szrj if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1062*38fd1498Szrj || ! ALLOCNO_ASSIGNED_P (a)
1063*38fd1498Szrj || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0)
1064*38fd1498Szrj continue;
1065*38fd1498Szrj mode = ALLOCNO_MODE (a);
1066*38fd1498Szrj nregs = hard_regno_nregs (hard_regno, mode);
1067*38fd1498Szrj nobj = ALLOCNO_NUM_OBJECTS (a);
1068*38fd1498Szrj for (k = 0; k < nobj; k++)
1069*38fd1498Szrj {
1070*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, k);
1071*38fd1498Szrj ira_object_t conflict_obj;
1072*38fd1498Szrj ira_object_conflict_iterator oci;
1073*38fd1498Szrj
1074*38fd1498Szrj FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1075*38fd1498Szrj {
1076*38fd1498Szrj ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1077*38fd1498Szrj
1078*38fd1498Szrj /* We can process the conflict allocno repeatedly with
1079*38fd1498Szrj the same result. */
1080*38fd1498Szrj if (nregs == nobj && nregs > 1)
1081*38fd1498Szrj {
1082*38fd1498Szrj int num = OBJECT_SUBWORD (conflict_obj);
1083*38fd1498Szrj
1084*38fd1498Szrj if (REG_WORDS_BIG_ENDIAN)
1085*38fd1498Szrj CLEAR_HARD_REG_BIT
1086*38fd1498Szrj (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1087*38fd1498Szrj hard_regno + nobj - num - 1);
1088*38fd1498Szrj else
1089*38fd1498Szrj CLEAR_HARD_REG_BIT
1090*38fd1498Szrj (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1091*38fd1498Szrj hard_regno + num);
1092*38fd1498Szrj }
1093*38fd1498Szrj else
1094*38fd1498Szrj AND_COMPL_HARD_REG_SET
1095*38fd1498Szrj (ALLOCNO_COLOR_DATA (conflict_a)->profitable_hard_regs,
1096*38fd1498Szrj ira_reg_mode_hard_regset[hard_regno][mode]);
1097*38fd1498Szrj }
1098*38fd1498Szrj }
1099*38fd1498Szrj }
1100*38fd1498Szrj /* Exclude too costly hard regs. */
1101*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
1102*38fd1498Szrj {
1103*38fd1498Szrj int min_cost = INT_MAX;
1104*38fd1498Szrj int *costs;
1105*38fd1498Szrj
1106*38fd1498Szrj a = ira_allocnos[i];
1107*38fd1498Szrj if ((aclass = ALLOCNO_CLASS (a)) == NO_REGS
1108*38fd1498Szrj || empty_profitable_hard_regs (a))
1109*38fd1498Szrj continue;
1110*38fd1498Szrj data = ALLOCNO_COLOR_DATA (a);
1111*38fd1498Szrj mode = ALLOCNO_MODE (a);
1112*38fd1498Szrj if ((costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a)) != NULL
1113*38fd1498Szrj || (costs = ALLOCNO_HARD_REG_COSTS (a)) != NULL)
1114*38fd1498Szrj {
1115*38fd1498Szrj class_size = ira_class_hard_regs_num[aclass];
1116*38fd1498Szrj for (j = 0; j < class_size; j++)
1117*38fd1498Szrj {
1118*38fd1498Szrj hard_regno = ira_class_hard_regs[aclass][j];
1119*38fd1498Szrj if (! TEST_HARD_REG_BIT (data->profitable_hard_regs,
1120*38fd1498Szrj hard_regno))
1121*38fd1498Szrj continue;
1122*38fd1498Szrj if (ALLOCNO_UPDATED_MEMORY_COST (a) < costs[j]
1123*38fd1498Szrj /* Do not remove HARD_REGNO for static chain pointer
1124*38fd1498Szrj pseudo when non-local goto is used. */
1125*38fd1498Szrj && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1126*38fd1498Szrj CLEAR_HARD_REG_BIT (data->profitable_hard_regs,
1127*38fd1498Szrj hard_regno);
1128*38fd1498Szrj else if (min_cost > costs[j])
1129*38fd1498Szrj min_cost = costs[j];
1130*38fd1498Szrj }
1131*38fd1498Szrj }
1132*38fd1498Szrj else if (ALLOCNO_UPDATED_MEMORY_COST (a)
1133*38fd1498Szrj < ALLOCNO_UPDATED_CLASS_COST (a)
1134*38fd1498Szrj /* Do not empty profitable regs for static chain
1135*38fd1498Szrj pointer pseudo when non-local goto is used. */
1136*38fd1498Szrj && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1137*38fd1498Szrj CLEAR_HARD_REG_SET (data->profitable_hard_regs);
1138*38fd1498Szrj if (ALLOCNO_UPDATED_CLASS_COST (a) > min_cost)
1139*38fd1498Szrj ALLOCNO_UPDATED_CLASS_COST (a) = min_cost;
1140*38fd1498Szrj }
1141*38fd1498Szrj }
1142*38fd1498Szrj
1143*38fd1498Szrj
1144*38fd1498Szrj
1145*38fd1498Szrj /* This page contains functions used to choose hard registers for
1146*38fd1498Szrj allocnos. */
1147*38fd1498Szrj
1148*38fd1498Szrj /* Pool for update cost records. */
1149*38fd1498Szrj static object_allocator<update_cost_record> update_cost_record_pool
1150*38fd1498Szrj ("update cost records");
1151*38fd1498Szrj
1152*38fd1498Szrj /* Return new update cost record with given params. */
1153*38fd1498Szrj static struct update_cost_record *
get_update_cost_record(int hard_regno,int divisor,struct update_cost_record * next)1154*38fd1498Szrj get_update_cost_record (int hard_regno, int divisor,
1155*38fd1498Szrj struct update_cost_record *next)
1156*38fd1498Szrj {
1157*38fd1498Szrj struct update_cost_record *record;
1158*38fd1498Szrj
1159*38fd1498Szrj record = update_cost_record_pool.allocate ();
1160*38fd1498Szrj record->hard_regno = hard_regno;
1161*38fd1498Szrj record->divisor = divisor;
1162*38fd1498Szrj record->next = next;
1163*38fd1498Szrj return record;
1164*38fd1498Szrj }
1165*38fd1498Szrj
1166*38fd1498Szrj /* Free memory for all records in LIST. */
1167*38fd1498Szrj static void
free_update_cost_record_list(struct update_cost_record * list)1168*38fd1498Szrj free_update_cost_record_list (struct update_cost_record *list)
1169*38fd1498Szrj {
1170*38fd1498Szrj struct update_cost_record *next;
1171*38fd1498Szrj
1172*38fd1498Szrj while (list != NULL)
1173*38fd1498Szrj {
1174*38fd1498Szrj next = list->next;
1175*38fd1498Szrj update_cost_record_pool.remove (list);
1176*38fd1498Szrj list = next;
1177*38fd1498Szrj }
1178*38fd1498Szrj }
1179*38fd1498Szrj
1180*38fd1498Szrj /* Free memory allocated for all update cost records. */
1181*38fd1498Szrj static void
finish_update_cost_records(void)1182*38fd1498Szrj finish_update_cost_records (void)
1183*38fd1498Szrj {
1184*38fd1498Szrj update_cost_record_pool.release ();
1185*38fd1498Szrj }
1186*38fd1498Szrj
1187*38fd1498Szrj /* Array whose element value is TRUE if the corresponding hard
1188*38fd1498Szrj register was already allocated for an allocno. */
1189*38fd1498Szrj static bool allocated_hardreg_p[FIRST_PSEUDO_REGISTER];
1190*38fd1498Szrj
1191*38fd1498Szrj /* Describes one element in a queue of allocnos whose costs need to be
1192*38fd1498Szrj updated. Each allocno in the queue is known to have an allocno
1193*38fd1498Szrj class. */
1194*38fd1498Szrj struct update_cost_queue_elem
1195*38fd1498Szrj {
1196*38fd1498Szrj /* This element is in the queue iff CHECK == update_cost_check. */
1197*38fd1498Szrj int check;
1198*38fd1498Szrj
1199*38fd1498Szrj /* COST_HOP_DIVISOR**N, where N is the length of the shortest path
1200*38fd1498Szrj connecting this allocno to the one being allocated. */
1201*38fd1498Szrj int divisor;
1202*38fd1498Szrj
1203*38fd1498Szrj /* Allocno from which we are chaining costs of connected allocnos.
1204*38fd1498Szrj It is used not go back in graph of allocnos connected by
1205*38fd1498Szrj copies. */
1206*38fd1498Szrj ira_allocno_t from;
1207*38fd1498Szrj
1208*38fd1498Szrj /* The next allocno in the queue, or null if this is the last element. */
1209*38fd1498Szrj ira_allocno_t next;
1210*38fd1498Szrj };
1211*38fd1498Szrj
1212*38fd1498Szrj /* The first element in a queue of allocnos whose copy costs need to be
1213*38fd1498Szrj updated. Null if the queue is empty. */
1214*38fd1498Szrj static ira_allocno_t update_cost_queue;
1215*38fd1498Szrj
1216*38fd1498Szrj /* The last element in the queue described by update_cost_queue.
1217*38fd1498Szrj Not valid if update_cost_queue is null. */
1218*38fd1498Szrj static struct update_cost_queue_elem *update_cost_queue_tail;
1219*38fd1498Szrj
1220*38fd1498Szrj /* A pool of elements in the queue described by update_cost_queue.
1221*38fd1498Szrj Elements are indexed by ALLOCNO_NUM. */
1222*38fd1498Szrj static struct update_cost_queue_elem *update_cost_queue_elems;
1223*38fd1498Szrj
1224*38fd1498Szrj /* The current value of update_costs_from_copies call count. */
1225*38fd1498Szrj static int update_cost_check;
1226*38fd1498Szrj
1227*38fd1498Szrj /* Allocate and initialize data necessary for function
1228*38fd1498Szrj update_costs_from_copies. */
1229*38fd1498Szrj static void
initiate_cost_update(void)1230*38fd1498Szrj initiate_cost_update (void)
1231*38fd1498Szrj {
1232*38fd1498Szrj size_t size;
1233*38fd1498Szrj
1234*38fd1498Szrj size = ira_allocnos_num * sizeof (struct update_cost_queue_elem);
1235*38fd1498Szrj update_cost_queue_elems
1236*38fd1498Szrj = (struct update_cost_queue_elem *) ira_allocate (size);
1237*38fd1498Szrj memset (update_cost_queue_elems, 0, size);
1238*38fd1498Szrj update_cost_check = 0;
1239*38fd1498Szrj }
1240*38fd1498Szrj
1241*38fd1498Szrj /* Deallocate data used by function update_costs_from_copies. */
1242*38fd1498Szrj static void
finish_cost_update(void)1243*38fd1498Szrj finish_cost_update (void)
1244*38fd1498Szrj {
1245*38fd1498Szrj ira_free (update_cost_queue_elems);
1246*38fd1498Szrj finish_update_cost_records ();
1247*38fd1498Szrj }
1248*38fd1498Szrj
1249*38fd1498Szrj /* When we traverse allocnos to update hard register costs, the cost
1250*38fd1498Szrj divisor will be multiplied by the following macro value for each
1251*38fd1498Szrj hop from given allocno to directly connected allocnos. */
1252*38fd1498Szrj #define COST_HOP_DIVISOR 4
1253*38fd1498Szrj
1254*38fd1498Szrj /* Start a new cost-updating pass. */
1255*38fd1498Szrj static void
start_update_cost(void)1256*38fd1498Szrj start_update_cost (void)
1257*38fd1498Szrj {
1258*38fd1498Szrj update_cost_check++;
1259*38fd1498Szrj update_cost_queue = NULL;
1260*38fd1498Szrj }
1261*38fd1498Szrj
1262*38fd1498Szrj /* Add (ALLOCNO, FROM, DIVISOR) to the end of update_cost_queue, unless
1263*38fd1498Szrj ALLOCNO is already in the queue, or has NO_REGS class. */
1264*38fd1498Szrj static inline void
queue_update_cost(ira_allocno_t allocno,ira_allocno_t from,int divisor)1265*38fd1498Szrj queue_update_cost (ira_allocno_t allocno, ira_allocno_t from, int divisor)
1266*38fd1498Szrj {
1267*38fd1498Szrj struct update_cost_queue_elem *elem;
1268*38fd1498Szrj
1269*38fd1498Szrj elem = &update_cost_queue_elems[ALLOCNO_NUM (allocno)];
1270*38fd1498Szrj if (elem->check != update_cost_check
1271*38fd1498Szrj && ALLOCNO_CLASS (allocno) != NO_REGS)
1272*38fd1498Szrj {
1273*38fd1498Szrj elem->check = update_cost_check;
1274*38fd1498Szrj elem->from = from;
1275*38fd1498Szrj elem->divisor = divisor;
1276*38fd1498Szrj elem->next = NULL;
1277*38fd1498Szrj if (update_cost_queue == NULL)
1278*38fd1498Szrj update_cost_queue = allocno;
1279*38fd1498Szrj else
1280*38fd1498Szrj update_cost_queue_tail->next = allocno;
1281*38fd1498Szrj update_cost_queue_tail = elem;
1282*38fd1498Szrj }
1283*38fd1498Szrj }
1284*38fd1498Szrj
1285*38fd1498Szrj /* Try to remove the first element from update_cost_queue. Return
1286*38fd1498Szrj false if the queue was empty, otherwise make (*ALLOCNO, *FROM,
1287*38fd1498Szrj *DIVISOR) describe the removed element. */
1288*38fd1498Szrj static inline bool
get_next_update_cost(ira_allocno_t * allocno,ira_allocno_t * from,int * divisor)1289*38fd1498Szrj get_next_update_cost (ira_allocno_t *allocno, ira_allocno_t *from, int *divisor)
1290*38fd1498Szrj {
1291*38fd1498Szrj struct update_cost_queue_elem *elem;
1292*38fd1498Szrj
1293*38fd1498Szrj if (update_cost_queue == NULL)
1294*38fd1498Szrj return false;
1295*38fd1498Szrj
1296*38fd1498Szrj *allocno = update_cost_queue;
1297*38fd1498Szrj elem = &update_cost_queue_elems[ALLOCNO_NUM (*allocno)];
1298*38fd1498Szrj *from = elem->from;
1299*38fd1498Szrj *divisor = elem->divisor;
1300*38fd1498Szrj update_cost_queue = elem->next;
1301*38fd1498Szrj return true;
1302*38fd1498Szrj }
1303*38fd1498Szrj
1304*38fd1498Szrj /* Increase costs of HARD_REGNO by UPDATE_COST and conflict cost by
1305*38fd1498Szrj UPDATE_CONFLICT_COST for ALLOCNO. Return true if we really
1306*38fd1498Szrj modified the cost. */
1307*38fd1498Szrj static bool
update_allocno_cost(ira_allocno_t allocno,int hard_regno,int update_cost,int update_conflict_cost)1308*38fd1498Szrj update_allocno_cost (ira_allocno_t allocno, int hard_regno,
1309*38fd1498Szrj int update_cost, int update_conflict_cost)
1310*38fd1498Szrj {
1311*38fd1498Szrj int i;
1312*38fd1498Szrj enum reg_class aclass = ALLOCNO_CLASS (allocno);
1313*38fd1498Szrj
1314*38fd1498Szrj i = ira_class_hard_reg_index[aclass][hard_regno];
1315*38fd1498Szrj if (i < 0)
1316*38fd1498Szrj return false;
1317*38fd1498Szrj ira_allocate_and_set_or_copy_costs
1318*38fd1498Szrj (&ALLOCNO_UPDATED_HARD_REG_COSTS (allocno), aclass,
1319*38fd1498Szrj ALLOCNO_UPDATED_CLASS_COST (allocno),
1320*38fd1498Szrj ALLOCNO_HARD_REG_COSTS (allocno));
1321*38fd1498Szrj ira_allocate_and_set_or_copy_costs
1322*38fd1498Szrj (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno),
1323*38fd1498Szrj aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (allocno));
1324*38fd1498Szrj ALLOCNO_UPDATED_HARD_REG_COSTS (allocno)[i] += update_cost;
1325*38fd1498Szrj ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno)[i] += update_conflict_cost;
1326*38fd1498Szrj return true;
1327*38fd1498Szrj }
1328*38fd1498Szrj
1329*38fd1498Szrj /* Update (decrease if DECR_P) HARD_REGNO cost of allocnos connected
1330*38fd1498Szrj by copies to ALLOCNO to increase chances to remove some copies as
1331*38fd1498Szrj the result of subsequent assignment. Record cost updates if
1332*38fd1498Szrj RECORD_P is true. */
1333*38fd1498Szrj static void
update_costs_from_allocno(ira_allocno_t allocno,int hard_regno,int divisor,bool decr_p,bool record_p)1334*38fd1498Szrj update_costs_from_allocno (ira_allocno_t allocno, int hard_regno,
1335*38fd1498Szrj int divisor, bool decr_p, bool record_p)
1336*38fd1498Szrj {
1337*38fd1498Szrj int cost, update_cost, update_conflict_cost;
1338*38fd1498Szrj machine_mode mode;
1339*38fd1498Szrj enum reg_class rclass, aclass;
1340*38fd1498Szrj ira_allocno_t another_allocno, from = NULL;
1341*38fd1498Szrj ira_copy_t cp, next_cp;
1342*38fd1498Szrj
1343*38fd1498Szrj rclass = REGNO_REG_CLASS (hard_regno);
1344*38fd1498Szrj do
1345*38fd1498Szrj {
1346*38fd1498Szrj mode = ALLOCNO_MODE (allocno);
1347*38fd1498Szrj ira_init_register_move_cost_if_necessary (mode);
1348*38fd1498Szrj for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1349*38fd1498Szrj {
1350*38fd1498Szrj if (cp->first == allocno)
1351*38fd1498Szrj {
1352*38fd1498Szrj next_cp = cp->next_first_allocno_copy;
1353*38fd1498Szrj another_allocno = cp->second;
1354*38fd1498Szrj }
1355*38fd1498Szrj else if (cp->second == allocno)
1356*38fd1498Szrj {
1357*38fd1498Szrj next_cp = cp->next_second_allocno_copy;
1358*38fd1498Szrj another_allocno = cp->first;
1359*38fd1498Szrj }
1360*38fd1498Szrj else
1361*38fd1498Szrj gcc_unreachable ();
1362*38fd1498Szrj
1363*38fd1498Szrj if (another_allocno == from)
1364*38fd1498Szrj continue;
1365*38fd1498Szrj
1366*38fd1498Szrj aclass = ALLOCNO_CLASS (another_allocno);
1367*38fd1498Szrj if (! TEST_HARD_REG_BIT (reg_class_contents[aclass],
1368*38fd1498Szrj hard_regno)
1369*38fd1498Szrj || ALLOCNO_ASSIGNED_P (another_allocno))
1370*38fd1498Szrj continue;
1371*38fd1498Szrj
1372*38fd1498Szrj /* If we have different modes use the smallest one. It is
1373*38fd1498Szrj a sub-register move. It is hard to predict what LRA
1374*38fd1498Szrj will reload (the pseudo or its sub-register) but LRA
1375*38fd1498Szrj will try to minimize the data movement. Also for some
1376*38fd1498Szrj register classes bigger modes might be invalid,
1377*38fd1498Szrj e.g. DImode for AREG on x86. For such cases the
1378*38fd1498Szrj register move cost will be maximal. */
1379*38fd1498Szrj mode = narrower_subreg_mode (mode, ALLOCNO_MODE (cp->second));
1380*38fd1498Szrj
1381*38fd1498Szrj cost = (cp->second == allocno
1382*38fd1498Szrj ? ira_register_move_cost[mode][rclass][aclass]
1383*38fd1498Szrj : ira_register_move_cost[mode][aclass][rclass]);
1384*38fd1498Szrj if (decr_p)
1385*38fd1498Szrj cost = -cost;
1386*38fd1498Szrj
1387*38fd1498Szrj update_conflict_cost = update_cost = cp->freq * cost / divisor;
1388*38fd1498Szrj
1389*38fd1498Szrj if (ALLOCNO_COLOR_DATA (another_allocno) != NULL
1390*38fd1498Szrj && (ALLOCNO_COLOR_DATA (allocno)->first_thread_allocno
1391*38fd1498Szrj != ALLOCNO_COLOR_DATA (another_allocno)->first_thread_allocno))
1392*38fd1498Szrj /* Decrease conflict cost of ANOTHER_ALLOCNO if it is not
1393*38fd1498Szrj in the same allocation thread. */
1394*38fd1498Szrj update_conflict_cost /= COST_HOP_DIVISOR;
1395*38fd1498Szrj
1396*38fd1498Szrj if (update_cost == 0)
1397*38fd1498Szrj continue;
1398*38fd1498Szrj
1399*38fd1498Szrj if (! update_allocno_cost (another_allocno, hard_regno,
1400*38fd1498Szrj update_cost, update_conflict_cost))
1401*38fd1498Szrj continue;
1402*38fd1498Szrj queue_update_cost (another_allocno, allocno, divisor * COST_HOP_DIVISOR);
1403*38fd1498Szrj if (record_p && ALLOCNO_COLOR_DATA (another_allocno) != NULL)
1404*38fd1498Szrj ALLOCNO_COLOR_DATA (another_allocno)->update_cost_records
1405*38fd1498Szrj = get_update_cost_record (hard_regno, divisor,
1406*38fd1498Szrj ALLOCNO_COLOR_DATA (another_allocno)
1407*38fd1498Szrj ->update_cost_records);
1408*38fd1498Szrj }
1409*38fd1498Szrj }
1410*38fd1498Szrj while (get_next_update_cost (&allocno, &from, &divisor));
1411*38fd1498Szrj }
1412*38fd1498Szrj
1413*38fd1498Szrj /* Decrease preferred ALLOCNO hard register costs and costs of
1414*38fd1498Szrj allocnos connected to ALLOCNO through copy. */
1415*38fd1498Szrj static void
update_costs_from_prefs(ira_allocno_t allocno)1416*38fd1498Szrj update_costs_from_prefs (ira_allocno_t allocno)
1417*38fd1498Szrj {
1418*38fd1498Szrj ira_pref_t pref;
1419*38fd1498Szrj
1420*38fd1498Szrj start_update_cost ();
1421*38fd1498Szrj for (pref = ALLOCNO_PREFS (allocno); pref != NULL; pref = pref->next_pref)
1422*38fd1498Szrj update_costs_from_allocno (allocno, pref->hard_regno,
1423*38fd1498Szrj COST_HOP_DIVISOR, true, true);
1424*38fd1498Szrj }
1425*38fd1498Szrj
1426*38fd1498Szrj /* Update (decrease if DECR_P) the cost of allocnos connected to
1427*38fd1498Szrj ALLOCNO through copies to increase chances to remove some copies as
1428*38fd1498Szrj the result of subsequent assignment. ALLOCNO was just assigned to
1429*38fd1498Szrj a hard register. Record cost updates if RECORD_P is true. */
1430*38fd1498Szrj static void
update_costs_from_copies(ira_allocno_t allocno,bool decr_p,bool record_p)1431*38fd1498Szrj update_costs_from_copies (ira_allocno_t allocno, bool decr_p, bool record_p)
1432*38fd1498Szrj {
1433*38fd1498Szrj int hard_regno;
1434*38fd1498Szrj
1435*38fd1498Szrj hard_regno = ALLOCNO_HARD_REGNO (allocno);
1436*38fd1498Szrj ira_assert (hard_regno >= 0 && ALLOCNO_CLASS (allocno) != NO_REGS);
1437*38fd1498Szrj start_update_cost ();
1438*38fd1498Szrj update_costs_from_allocno (allocno, hard_regno, 1, decr_p, record_p);
1439*38fd1498Szrj }
1440*38fd1498Szrj
1441*38fd1498Szrj /* Update conflict_allocno_hard_prefs of allocnos conflicting with
1442*38fd1498Szrj ALLOCNO. */
1443*38fd1498Szrj static void
update_conflict_allocno_hard_prefs(ira_allocno_t allocno)1444*38fd1498Szrj update_conflict_allocno_hard_prefs (ira_allocno_t allocno)
1445*38fd1498Szrj {
1446*38fd1498Szrj int l, nr = ALLOCNO_NUM_OBJECTS (allocno);
1447*38fd1498Szrj
1448*38fd1498Szrj for (l = 0; l < nr; l++)
1449*38fd1498Szrj {
1450*38fd1498Szrj ira_object_t conflict_obj, obj = ALLOCNO_OBJECT (allocno, l);
1451*38fd1498Szrj ira_object_conflict_iterator oci;
1452*38fd1498Szrj
1453*38fd1498Szrj FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1454*38fd1498Szrj {
1455*38fd1498Szrj ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1456*38fd1498Szrj allocno_color_data_t conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
1457*38fd1498Szrj ira_pref_t pref;
1458*38fd1498Szrj
1459*38fd1498Szrj if (!(hard_reg_set_intersect_p
1460*38fd1498Szrj (ALLOCNO_COLOR_DATA (allocno)->profitable_hard_regs,
1461*38fd1498Szrj conflict_data->profitable_hard_regs)))
1462*38fd1498Szrj continue;
1463*38fd1498Szrj for (pref = ALLOCNO_PREFS (allocno);
1464*38fd1498Szrj pref != NULL;
1465*38fd1498Szrj pref = pref->next_pref)
1466*38fd1498Szrj conflict_data->conflict_allocno_hard_prefs += pref->freq;
1467*38fd1498Szrj }
1468*38fd1498Szrj }
1469*38fd1498Szrj }
1470*38fd1498Szrj
1471*38fd1498Szrj /* Restore costs of allocnos connected to ALLOCNO by copies as it was
1472*38fd1498Szrj before updating costs of these allocnos from given allocno. This
1473*38fd1498Szrj is a wise thing to do as if given allocno did not get an expected
1474*38fd1498Szrj hard reg, using smaller cost of the hard reg for allocnos connected
1475*38fd1498Szrj by copies to given allocno becomes actually misleading. Free all
1476*38fd1498Szrj update cost records for ALLOCNO as we don't need them anymore. */
1477*38fd1498Szrj static void
restore_costs_from_copies(ira_allocno_t allocno)1478*38fd1498Szrj restore_costs_from_copies (ira_allocno_t allocno)
1479*38fd1498Szrj {
1480*38fd1498Szrj struct update_cost_record *records, *curr;
1481*38fd1498Szrj
1482*38fd1498Szrj if (ALLOCNO_COLOR_DATA (allocno) == NULL)
1483*38fd1498Szrj return;
1484*38fd1498Szrj records = ALLOCNO_COLOR_DATA (allocno)->update_cost_records;
1485*38fd1498Szrj start_update_cost ();
1486*38fd1498Szrj for (curr = records; curr != NULL; curr = curr->next)
1487*38fd1498Szrj update_costs_from_allocno (allocno, curr->hard_regno,
1488*38fd1498Szrj curr->divisor, true, false);
1489*38fd1498Szrj free_update_cost_record_list (records);
1490*38fd1498Szrj ALLOCNO_COLOR_DATA (allocno)->update_cost_records = NULL;
1491*38fd1498Szrj }
1492*38fd1498Szrj
1493*38fd1498Szrj /* This function updates COSTS (decrease if DECR_P) for hard_registers
1494*38fd1498Szrj of ACLASS by conflict costs of the unassigned allocnos
1495*38fd1498Szrj connected by copies with allocnos in update_cost_queue. This
1496*38fd1498Szrj update increases chances to remove some copies. */
1497*38fd1498Szrj static void
update_conflict_hard_regno_costs(int * costs,enum reg_class aclass,bool decr_p)1498*38fd1498Szrj update_conflict_hard_regno_costs (int *costs, enum reg_class aclass,
1499*38fd1498Szrj bool decr_p)
1500*38fd1498Szrj {
1501*38fd1498Szrj int i, cost, class_size, freq, mult, div, divisor;
1502*38fd1498Szrj int index, hard_regno;
1503*38fd1498Szrj int *conflict_costs;
1504*38fd1498Szrj bool cont_p;
1505*38fd1498Szrj enum reg_class another_aclass;
1506*38fd1498Szrj ira_allocno_t allocno, another_allocno, from;
1507*38fd1498Szrj ira_copy_t cp, next_cp;
1508*38fd1498Szrj
1509*38fd1498Szrj while (get_next_update_cost (&allocno, &from, &divisor))
1510*38fd1498Szrj for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
1511*38fd1498Szrj {
1512*38fd1498Szrj if (cp->first == allocno)
1513*38fd1498Szrj {
1514*38fd1498Szrj next_cp = cp->next_first_allocno_copy;
1515*38fd1498Szrj another_allocno = cp->second;
1516*38fd1498Szrj }
1517*38fd1498Szrj else if (cp->second == allocno)
1518*38fd1498Szrj {
1519*38fd1498Szrj next_cp = cp->next_second_allocno_copy;
1520*38fd1498Szrj another_allocno = cp->first;
1521*38fd1498Szrj }
1522*38fd1498Szrj else
1523*38fd1498Szrj gcc_unreachable ();
1524*38fd1498Szrj
1525*38fd1498Szrj if (another_allocno == from)
1526*38fd1498Szrj continue;
1527*38fd1498Szrj
1528*38fd1498Szrj another_aclass = ALLOCNO_CLASS (another_allocno);
1529*38fd1498Szrj if (! ira_reg_classes_intersect_p[aclass][another_aclass]
1530*38fd1498Szrj || ALLOCNO_ASSIGNED_P (another_allocno)
1531*38fd1498Szrj || ALLOCNO_COLOR_DATA (another_allocno)->may_be_spilled_p)
1532*38fd1498Szrj continue;
1533*38fd1498Szrj class_size = ira_class_hard_regs_num[another_aclass];
1534*38fd1498Szrj ira_allocate_and_copy_costs
1535*38fd1498Szrj (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno),
1536*38fd1498Szrj another_aclass, ALLOCNO_CONFLICT_HARD_REG_COSTS (another_allocno));
1537*38fd1498Szrj conflict_costs
1538*38fd1498Szrj = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (another_allocno);
1539*38fd1498Szrj if (conflict_costs == NULL)
1540*38fd1498Szrj cont_p = true;
1541*38fd1498Szrj else
1542*38fd1498Szrj {
1543*38fd1498Szrj mult = cp->freq;
1544*38fd1498Szrj freq = ALLOCNO_FREQ (another_allocno);
1545*38fd1498Szrj if (freq == 0)
1546*38fd1498Szrj freq = 1;
1547*38fd1498Szrj div = freq * divisor;
1548*38fd1498Szrj cont_p = false;
1549*38fd1498Szrj for (i = class_size - 1; i >= 0; i--)
1550*38fd1498Szrj {
1551*38fd1498Szrj hard_regno = ira_class_hard_regs[another_aclass][i];
1552*38fd1498Szrj ira_assert (hard_regno >= 0);
1553*38fd1498Szrj index = ira_class_hard_reg_index[aclass][hard_regno];
1554*38fd1498Szrj if (index < 0)
1555*38fd1498Szrj continue;
1556*38fd1498Szrj cost = (int) (((int64_t) conflict_costs [i] * mult) / div);
1557*38fd1498Szrj if (cost == 0)
1558*38fd1498Szrj continue;
1559*38fd1498Szrj cont_p = true;
1560*38fd1498Szrj if (decr_p)
1561*38fd1498Szrj cost = -cost;
1562*38fd1498Szrj costs[index] += cost;
1563*38fd1498Szrj }
1564*38fd1498Szrj }
1565*38fd1498Szrj /* Probably 5 hops will be enough. */
1566*38fd1498Szrj if (cont_p
1567*38fd1498Szrj && divisor <= (COST_HOP_DIVISOR
1568*38fd1498Szrj * COST_HOP_DIVISOR
1569*38fd1498Szrj * COST_HOP_DIVISOR
1570*38fd1498Szrj * COST_HOP_DIVISOR))
1571*38fd1498Szrj queue_update_cost (another_allocno, allocno, divisor * COST_HOP_DIVISOR);
1572*38fd1498Szrj }
1573*38fd1498Szrj }
1574*38fd1498Szrj
1575*38fd1498Szrj /* Set up conflicting (through CONFLICT_REGS) for each object of
1576*38fd1498Szrj allocno A and the start allocno profitable regs (through
1577*38fd1498Szrj START_PROFITABLE_REGS). Remember that the start profitable regs
1578*38fd1498Szrj exclude hard regs which can not hold value of mode of allocno A.
1579*38fd1498Szrj This covers mostly cases when multi-register value should be
1580*38fd1498Szrj aligned. */
1581*38fd1498Szrj static inline void
get_conflict_and_start_profitable_regs(ira_allocno_t a,bool retry_p,HARD_REG_SET * conflict_regs,HARD_REG_SET * start_profitable_regs)1582*38fd1498Szrj get_conflict_and_start_profitable_regs (ira_allocno_t a, bool retry_p,
1583*38fd1498Szrj HARD_REG_SET *conflict_regs,
1584*38fd1498Szrj HARD_REG_SET *start_profitable_regs)
1585*38fd1498Szrj {
1586*38fd1498Szrj int i, nwords;
1587*38fd1498Szrj ira_object_t obj;
1588*38fd1498Szrj
1589*38fd1498Szrj nwords = ALLOCNO_NUM_OBJECTS (a);
1590*38fd1498Szrj for (i = 0; i < nwords; i++)
1591*38fd1498Szrj {
1592*38fd1498Szrj obj = ALLOCNO_OBJECT (a, i);
1593*38fd1498Szrj COPY_HARD_REG_SET (conflict_regs[i],
1594*38fd1498Szrj OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
1595*38fd1498Szrj }
1596*38fd1498Szrj if (retry_p)
1597*38fd1498Szrj {
1598*38fd1498Szrj COPY_HARD_REG_SET (*start_profitable_regs,
1599*38fd1498Szrj reg_class_contents[ALLOCNO_CLASS (a)]);
1600*38fd1498Szrj AND_COMPL_HARD_REG_SET (*start_profitable_regs,
1601*38fd1498Szrj ira_prohibited_class_mode_regs
1602*38fd1498Szrj [ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
1603*38fd1498Szrj }
1604*38fd1498Szrj else
1605*38fd1498Szrj COPY_HARD_REG_SET (*start_profitable_regs,
1606*38fd1498Szrj ALLOCNO_COLOR_DATA (a)->profitable_hard_regs);
1607*38fd1498Szrj }
1608*38fd1498Szrj
1609*38fd1498Szrj /* Return true if HARD_REGNO is ok for assigning to allocno A with
1610*38fd1498Szrj PROFITABLE_REGS and whose objects have CONFLICT_REGS. */
1611*38fd1498Szrj static inline bool
check_hard_reg_p(ira_allocno_t a,int hard_regno,HARD_REG_SET * conflict_regs,HARD_REG_SET profitable_regs)1612*38fd1498Szrj check_hard_reg_p (ira_allocno_t a, int hard_regno,
1613*38fd1498Szrj HARD_REG_SET *conflict_regs, HARD_REG_SET profitable_regs)
1614*38fd1498Szrj {
1615*38fd1498Szrj int j, nwords, nregs;
1616*38fd1498Szrj enum reg_class aclass;
1617*38fd1498Szrj machine_mode mode;
1618*38fd1498Szrj
1619*38fd1498Szrj aclass = ALLOCNO_CLASS (a);
1620*38fd1498Szrj mode = ALLOCNO_MODE (a);
1621*38fd1498Szrj if (TEST_HARD_REG_BIT (ira_prohibited_class_mode_regs[aclass][mode],
1622*38fd1498Szrj hard_regno))
1623*38fd1498Szrj return false;
1624*38fd1498Szrj /* Checking only profitable hard regs. */
1625*38fd1498Szrj if (! TEST_HARD_REG_BIT (profitable_regs, hard_regno))
1626*38fd1498Szrj return false;
1627*38fd1498Szrj nregs = hard_regno_nregs (hard_regno, mode);
1628*38fd1498Szrj nwords = ALLOCNO_NUM_OBJECTS (a);
1629*38fd1498Szrj for (j = 0; j < nregs; j++)
1630*38fd1498Szrj {
1631*38fd1498Szrj int k;
1632*38fd1498Szrj int set_to_test_start = 0, set_to_test_end = nwords;
1633*38fd1498Szrj
1634*38fd1498Szrj if (nregs == nwords)
1635*38fd1498Szrj {
1636*38fd1498Szrj if (REG_WORDS_BIG_ENDIAN)
1637*38fd1498Szrj set_to_test_start = nwords - j - 1;
1638*38fd1498Szrj else
1639*38fd1498Szrj set_to_test_start = j;
1640*38fd1498Szrj set_to_test_end = set_to_test_start + 1;
1641*38fd1498Szrj }
1642*38fd1498Szrj for (k = set_to_test_start; k < set_to_test_end; k++)
1643*38fd1498Szrj if (TEST_HARD_REG_BIT (conflict_regs[k], hard_regno + j))
1644*38fd1498Szrj break;
1645*38fd1498Szrj if (k != set_to_test_end)
1646*38fd1498Szrj break;
1647*38fd1498Szrj }
1648*38fd1498Szrj return j == nregs;
1649*38fd1498Szrj }
1650*38fd1498Szrj
1651*38fd1498Szrj /* Return number of registers needed to be saved and restored at
1652*38fd1498Szrj function prologue/epilogue if we allocate HARD_REGNO to hold value
1653*38fd1498Szrj of MODE. */
1654*38fd1498Szrj static int
calculate_saved_nregs(int hard_regno,machine_mode mode)1655*38fd1498Szrj calculate_saved_nregs (int hard_regno, machine_mode mode)
1656*38fd1498Szrj {
1657*38fd1498Szrj int i;
1658*38fd1498Szrj int nregs = 0;
1659*38fd1498Szrj
1660*38fd1498Szrj ira_assert (hard_regno >= 0);
1661*38fd1498Szrj for (i = hard_regno_nregs (hard_regno, mode) - 1; i >= 0; i--)
1662*38fd1498Szrj if (!allocated_hardreg_p[hard_regno + i]
1663*38fd1498Szrj && !TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + i)
1664*38fd1498Szrj && !LOCAL_REGNO (hard_regno + i))
1665*38fd1498Szrj nregs++;
1666*38fd1498Szrj return nregs;
1667*38fd1498Szrj }
1668*38fd1498Szrj
1669*38fd1498Szrj /* Choose a hard register for allocno A. If RETRY_P is TRUE, it means
1670*38fd1498Szrj that the function called from function
1671*38fd1498Szrj `ira_reassign_conflict_allocnos' and `allocno_reload_assign'. In
1672*38fd1498Szrj this case some allocno data are not defined or updated and we
1673*38fd1498Szrj should not touch these data. The function returns true if we
1674*38fd1498Szrj managed to assign a hard register to the allocno.
1675*38fd1498Szrj
1676*38fd1498Szrj To assign a hard register, first of all we calculate all conflict
1677*38fd1498Szrj hard registers which can come from conflicting allocnos with
1678*38fd1498Szrj already assigned hard registers. After that we find first free
1679*38fd1498Szrj hard register with the minimal cost. During hard register cost
1680*38fd1498Szrj calculation we take conflict hard register costs into account to
1681*38fd1498Szrj give a chance for conflicting allocnos to get a better hard
1682*38fd1498Szrj register in the future.
1683*38fd1498Szrj
1684*38fd1498Szrj If the best hard register cost is bigger than cost of memory usage
1685*38fd1498Szrj for the allocno, we don't assign a hard register to given allocno
1686*38fd1498Szrj at all.
1687*38fd1498Szrj
1688*38fd1498Szrj If we assign a hard register to the allocno, we update costs of the
1689*38fd1498Szrj hard register for allocnos connected by copies to improve a chance
1690*38fd1498Szrj to coalesce insns represented by the copies when we assign hard
1691*38fd1498Szrj registers to the allocnos connected by the copies. */
1692*38fd1498Szrj static bool
assign_hard_reg(ira_allocno_t a,bool retry_p)1693*38fd1498Szrj assign_hard_reg (ira_allocno_t a, bool retry_p)
1694*38fd1498Szrj {
1695*38fd1498Szrj HARD_REG_SET conflicting_regs[2], profitable_hard_regs;
1696*38fd1498Szrj int i, j, hard_regno, best_hard_regno, class_size;
1697*38fd1498Szrj int cost, mem_cost, min_cost, full_cost, min_full_cost, nwords, word;
1698*38fd1498Szrj int *a_costs;
1699*38fd1498Szrj enum reg_class aclass;
1700*38fd1498Szrj machine_mode mode;
1701*38fd1498Szrj static int costs[FIRST_PSEUDO_REGISTER], full_costs[FIRST_PSEUDO_REGISTER];
1702*38fd1498Szrj int saved_nregs;
1703*38fd1498Szrj enum reg_class rclass;
1704*38fd1498Szrj int add_cost;
1705*38fd1498Szrj #ifdef STACK_REGS
1706*38fd1498Szrj bool no_stack_reg_p;
1707*38fd1498Szrj #endif
1708*38fd1498Szrj
1709*38fd1498Szrj ira_assert (! ALLOCNO_ASSIGNED_P (a));
1710*38fd1498Szrj get_conflict_and_start_profitable_regs (a, retry_p,
1711*38fd1498Szrj conflicting_regs,
1712*38fd1498Szrj &profitable_hard_regs);
1713*38fd1498Szrj aclass = ALLOCNO_CLASS (a);
1714*38fd1498Szrj class_size = ira_class_hard_regs_num[aclass];
1715*38fd1498Szrj best_hard_regno = -1;
1716*38fd1498Szrj memset (full_costs, 0, sizeof (int) * class_size);
1717*38fd1498Szrj mem_cost = 0;
1718*38fd1498Szrj memset (costs, 0, sizeof (int) * class_size);
1719*38fd1498Szrj memset (full_costs, 0, sizeof (int) * class_size);
1720*38fd1498Szrj #ifdef STACK_REGS
1721*38fd1498Szrj no_stack_reg_p = false;
1722*38fd1498Szrj #endif
1723*38fd1498Szrj if (! retry_p)
1724*38fd1498Szrj start_update_cost ();
1725*38fd1498Szrj mem_cost += ALLOCNO_UPDATED_MEMORY_COST (a);
1726*38fd1498Szrj
1727*38fd1498Szrj ira_allocate_and_copy_costs (&ALLOCNO_UPDATED_HARD_REG_COSTS (a),
1728*38fd1498Szrj aclass, ALLOCNO_HARD_REG_COSTS (a));
1729*38fd1498Szrj a_costs = ALLOCNO_UPDATED_HARD_REG_COSTS (a);
1730*38fd1498Szrj #ifdef STACK_REGS
1731*38fd1498Szrj no_stack_reg_p = no_stack_reg_p || ALLOCNO_TOTAL_NO_STACK_REG_P (a);
1732*38fd1498Szrj #endif
1733*38fd1498Szrj cost = ALLOCNO_UPDATED_CLASS_COST (a);
1734*38fd1498Szrj for (i = 0; i < class_size; i++)
1735*38fd1498Szrj if (a_costs != NULL)
1736*38fd1498Szrj {
1737*38fd1498Szrj costs[i] += a_costs[i];
1738*38fd1498Szrj full_costs[i] += a_costs[i];
1739*38fd1498Szrj }
1740*38fd1498Szrj else
1741*38fd1498Szrj {
1742*38fd1498Szrj costs[i] += cost;
1743*38fd1498Szrj full_costs[i] += cost;
1744*38fd1498Szrj }
1745*38fd1498Szrj nwords = ALLOCNO_NUM_OBJECTS (a);
1746*38fd1498Szrj curr_allocno_process++;
1747*38fd1498Szrj for (word = 0; word < nwords; word++)
1748*38fd1498Szrj {
1749*38fd1498Szrj ira_object_t conflict_obj;
1750*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, word);
1751*38fd1498Szrj ira_object_conflict_iterator oci;
1752*38fd1498Szrj
1753*38fd1498Szrj /* Take preferences of conflicting allocnos into account. */
1754*38fd1498Szrj FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
1755*38fd1498Szrj {
1756*38fd1498Szrj ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
1757*38fd1498Szrj enum reg_class conflict_aclass;
1758*38fd1498Szrj allocno_color_data_t data = ALLOCNO_COLOR_DATA (conflict_a);
1759*38fd1498Szrj
1760*38fd1498Szrj /* Reload can give another class so we need to check all
1761*38fd1498Szrj allocnos. */
1762*38fd1498Szrj if (!retry_p
1763*38fd1498Szrj && ((!ALLOCNO_ASSIGNED_P (conflict_a)
1764*38fd1498Szrj || ALLOCNO_HARD_REGNO (conflict_a) < 0)
1765*38fd1498Szrj && !(hard_reg_set_intersect_p
1766*38fd1498Szrj (profitable_hard_regs,
1767*38fd1498Szrj ALLOCNO_COLOR_DATA
1768*38fd1498Szrj (conflict_a)->profitable_hard_regs))))
1769*38fd1498Szrj {
1770*38fd1498Szrj /* All conflict allocnos are in consideration bitmap
1771*38fd1498Szrj when retry_p is false. It might change in future and
1772*38fd1498Szrj if it happens the assert will be broken. It means
1773*38fd1498Szrj the code should be modified for the new
1774*38fd1498Szrj assumptions. */
1775*38fd1498Szrj ira_assert (bitmap_bit_p (consideration_allocno_bitmap,
1776*38fd1498Szrj ALLOCNO_NUM (conflict_a)));
1777*38fd1498Szrj continue;
1778*38fd1498Szrj }
1779*38fd1498Szrj conflict_aclass = ALLOCNO_CLASS (conflict_a);
1780*38fd1498Szrj ira_assert (ira_reg_classes_intersect_p
1781*38fd1498Szrj [aclass][conflict_aclass]);
1782*38fd1498Szrj if (ALLOCNO_ASSIGNED_P (conflict_a))
1783*38fd1498Szrj {
1784*38fd1498Szrj hard_regno = ALLOCNO_HARD_REGNO (conflict_a);
1785*38fd1498Szrj if (hard_regno >= 0
1786*38fd1498Szrj && (ira_hard_reg_set_intersection_p
1787*38fd1498Szrj (hard_regno, ALLOCNO_MODE (conflict_a),
1788*38fd1498Szrj reg_class_contents[aclass])))
1789*38fd1498Szrj {
1790*38fd1498Szrj int n_objects = ALLOCNO_NUM_OBJECTS (conflict_a);
1791*38fd1498Szrj int conflict_nregs;
1792*38fd1498Szrj
1793*38fd1498Szrj mode = ALLOCNO_MODE (conflict_a);
1794*38fd1498Szrj conflict_nregs = hard_regno_nregs (hard_regno, mode);
1795*38fd1498Szrj if (conflict_nregs == n_objects && conflict_nregs > 1)
1796*38fd1498Szrj {
1797*38fd1498Szrj int num = OBJECT_SUBWORD (conflict_obj);
1798*38fd1498Szrj
1799*38fd1498Szrj if (REG_WORDS_BIG_ENDIAN)
1800*38fd1498Szrj SET_HARD_REG_BIT (conflicting_regs[word],
1801*38fd1498Szrj hard_regno + n_objects - num - 1);
1802*38fd1498Szrj else
1803*38fd1498Szrj SET_HARD_REG_BIT (conflicting_regs[word],
1804*38fd1498Szrj hard_regno + num);
1805*38fd1498Szrj }
1806*38fd1498Szrj else
1807*38fd1498Szrj IOR_HARD_REG_SET
1808*38fd1498Szrj (conflicting_regs[word],
1809*38fd1498Szrj ira_reg_mode_hard_regset[hard_regno][mode]);
1810*38fd1498Szrj if (hard_reg_set_subset_p (profitable_hard_regs,
1811*38fd1498Szrj conflicting_regs[word]))
1812*38fd1498Szrj goto fail;
1813*38fd1498Szrj }
1814*38fd1498Szrj }
1815*38fd1498Szrj else if (! retry_p
1816*38fd1498Szrj && ! ALLOCNO_COLOR_DATA (conflict_a)->may_be_spilled_p
1817*38fd1498Szrj /* Don't process the conflict allocno twice. */
1818*38fd1498Szrj && (ALLOCNO_COLOR_DATA (conflict_a)->last_process
1819*38fd1498Szrj != curr_allocno_process))
1820*38fd1498Szrj {
1821*38fd1498Szrj int k, *conflict_costs;
1822*38fd1498Szrj
1823*38fd1498Szrj ALLOCNO_COLOR_DATA (conflict_a)->last_process
1824*38fd1498Szrj = curr_allocno_process;
1825*38fd1498Szrj ira_allocate_and_copy_costs
1826*38fd1498Szrj (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a),
1827*38fd1498Szrj conflict_aclass,
1828*38fd1498Szrj ALLOCNO_CONFLICT_HARD_REG_COSTS (conflict_a));
1829*38fd1498Szrj conflict_costs
1830*38fd1498Szrj = ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (conflict_a);
1831*38fd1498Szrj if (conflict_costs != NULL)
1832*38fd1498Szrj for (j = class_size - 1; j >= 0; j--)
1833*38fd1498Szrj {
1834*38fd1498Szrj hard_regno = ira_class_hard_regs[aclass][j];
1835*38fd1498Szrj ira_assert (hard_regno >= 0);
1836*38fd1498Szrj k = ira_class_hard_reg_index[conflict_aclass][hard_regno];
1837*38fd1498Szrj if (k < 0
1838*38fd1498Szrj /* If HARD_REGNO is not available for CONFLICT_A,
1839*38fd1498Szrj the conflict would be ignored, since HARD_REGNO
1840*38fd1498Szrj will never be assigned to CONFLICT_A. */
1841*38fd1498Szrj || !TEST_HARD_REG_BIT (data->profitable_hard_regs,
1842*38fd1498Szrj hard_regno))
1843*38fd1498Szrj continue;
1844*38fd1498Szrj full_costs[j] -= conflict_costs[k];
1845*38fd1498Szrj }
1846*38fd1498Szrj queue_update_cost (conflict_a, NULL, COST_HOP_DIVISOR);
1847*38fd1498Szrj
1848*38fd1498Szrj }
1849*38fd1498Szrj }
1850*38fd1498Szrj }
1851*38fd1498Szrj if (! retry_p)
1852*38fd1498Szrj /* Take into account preferences of allocnos connected by copies to
1853*38fd1498Szrj the conflict allocnos. */
1854*38fd1498Szrj update_conflict_hard_regno_costs (full_costs, aclass, true);
1855*38fd1498Szrj
1856*38fd1498Szrj /* Take preferences of allocnos connected by copies into
1857*38fd1498Szrj account. */
1858*38fd1498Szrj if (! retry_p)
1859*38fd1498Szrj {
1860*38fd1498Szrj start_update_cost ();
1861*38fd1498Szrj queue_update_cost (a, NULL, COST_HOP_DIVISOR);
1862*38fd1498Szrj update_conflict_hard_regno_costs (full_costs, aclass, false);
1863*38fd1498Szrj }
1864*38fd1498Szrj min_cost = min_full_cost = INT_MAX;
1865*38fd1498Szrj /* We don't care about giving callee saved registers to allocnos no
1866*38fd1498Szrj living through calls because call clobbered registers are
1867*38fd1498Szrj allocated first (it is usual practice to put them first in
1868*38fd1498Szrj REG_ALLOC_ORDER). */
1869*38fd1498Szrj mode = ALLOCNO_MODE (a);
1870*38fd1498Szrj for (i = 0; i < class_size; i++)
1871*38fd1498Szrj {
1872*38fd1498Szrj hard_regno = ira_class_hard_regs[aclass][i];
1873*38fd1498Szrj #ifdef STACK_REGS
1874*38fd1498Szrj if (no_stack_reg_p
1875*38fd1498Szrj && FIRST_STACK_REG <= hard_regno && hard_regno <= LAST_STACK_REG)
1876*38fd1498Szrj continue;
1877*38fd1498Szrj #endif
1878*38fd1498Szrj if (! check_hard_reg_p (a, hard_regno,
1879*38fd1498Szrj conflicting_regs, profitable_hard_regs))
1880*38fd1498Szrj continue;
1881*38fd1498Szrj cost = costs[i];
1882*38fd1498Szrj full_cost = full_costs[i];
1883*38fd1498Szrj if (!HONOR_REG_ALLOC_ORDER)
1884*38fd1498Szrj {
1885*38fd1498Szrj if ((saved_nregs = calculate_saved_nregs (hard_regno, mode)) != 0)
1886*38fd1498Szrj /* We need to save/restore the hard register in
1887*38fd1498Szrj epilogue/prologue. Therefore we increase the cost. */
1888*38fd1498Szrj {
1889*38fd1498Szrj rclass = REGNO_REG_CLASS (hard_regno);
1890*38fd1498Szrj add_cost = ((ira_memory_move_cost[mode][rclass][0]
1891*38fd1498Szrj + ira_memory_move_cost[mode][rclass][1])
1892*38fd1498Szrj * saved_nregs / hard_regno_nregs (hard_regno,
1893*38fd1498Szrj mode) - 1);
1894*38fd1498Szrj cost += add_cost;
1895*38fd1498Szrj full_cost += add_cost;
1896*38fd1498Szrj }
1897*38fd1498Szrj }
1898*38fd1498Szrj if (min_cost > cost)
1899*38fd1498Szrj min_cost = cost;
1900*38fd1498Szrj if (min_full_cost > full_cost)
1901*38fd1498Szrj {
1902*38fd1498Szrj min_full_cost = full_cost;
1903*38fd1498Szrj best_hard_regno = hard_regno;
1904*38fd1498Szrj ira_assert (hard_regno >= 0);
1905*38fd1498Szrj }
1906*38fd1498Szrj }
1907*38fd1498Szrj if (min_full_cost > mem_cost
1908*38fd1498Szrj /* Do not spill static chain pointer pseudo when non-local goto
1909*38fd1498Szrj is used. */
1910*38fd1498Szrj && ! non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a)))
1911*38fd1498Szrj {
1912*38fd1498Szrj if (! retry_p && internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
1913*38fd1498Szrj fprintf (ira_dump_file, "(memory is more profitable %d vs %d) ",
1914*38fd1498Szrj mem_cost, min_full_cost);
1915*38fd1498Szrj best_hard_regno = -1;
1916*38fd1498Szrj }
1917*38fd1498Szrj fail:
1918*38fd1498Szrj if (best_hard_regno >= 0)
1919*38fd1498Szrj {
1920*38fd1498Szrj for (i = hard_regno_nregs (best_hard_regno, mode) - 1; i >= 0; i--)
1921*38fd1498Szrj allocated_hardreg_p[best_hard_regno + i] = true;
1922*38fd1498Szrj }
1923*38fd1498Szrj if (! retry_p)
1924*38fd1498Szrj restore_costs_from_copies (a);
1925*38fd1498Szrj ALLOCNO_HARD_REGNO (a) = best_hard_regno;
1926*38fd1498Szrj ALLOCNO_ASSIGNED_P (a) = true;
1927*38fd1498Szrj if (best_hard_regno >= 0)
1928*38fd1498Szrj update_costs_from_copies (a, true, ! retry_p);
1929*38fd1498Szrj ira_assert (ALLOCNO_CLASS (a) == aclass);
1930*38fd1498Szrj /* We don't need updated costs anymore. */
1931*38fd1498Szrj ira_free_allocno_updated_costs (a);
1932*38fd1498Szrj return best_hard_regno >= 0;
1933*38fd1498Szrj }
1934*38fd1498Szrj
1935*38fd1498Szrj
1936*38fd1498Szrj
1937*38fd1498Szrj /* An array used to sort copies. */
1938*38fd1498Szrj static ira_copy_t *sorted_copies;
1939*38fd1498Szrj
1940*38fd1498Szrj /* If allocno A is a cap, return non-cap allocno from which A is
1941*38fd1498Szrj created. Otherwise, return A. */
1942*38fd1498Szrj static ira_allocno_t
get_cap_member(ira_allocno_t a)1943*38fd1498Szrj get_cap_member (ira_allocno_t a)
1944*38fd1498Szrj {
1945*38fd1498Szrj ira_allocno_t member;
1946*38fd1498Szrj
1947*38fd1498Szrj while ((member = ALLOCNO_CAP_MEMBER (a)) != NULL)
1948*38fd1498Szrj a = member;
1949*38fd1498Szrj return a;
1950*38fd1498Szrj }
1951*38fd1498Szrj
1952*38fd1498Szrj /* Return TRUE if live ranges of allocnos A1 and A2 intersect. It is
1953*38fd1498Szrj used to find a conflict for new allocnos or allocnos with the
1954*38fd1498Szrj different allocno classes. */
1955*38fd1498Szrj static bool
allocnos_conflict_by_live_ranges_p(ira_allocno_t a1,ira_allocno_t a2)1956*38fd1498Szrj allocnos_conflict_by_live_ranges_p (ira_allocno_t a1, ira_allocno_t a2)
1957*38fd1498Szrj {
1958*38fd1498Szrj rtx reg1, reg2;
1959*38fd1498Szrj int i, j;
1960*38fd1498Szrj int n1 = ALLOCNO_NUM_OBJECTS (a1);
1961*38fd1498Szrj int n2 = ALLOCNO_NUM_OBJECTS (a2);
1962*38fd1498Szrj
1963*38fd1498Szrj if (a1 == a2)
1964*38fd1498Szrj return false;
1965*38fd1498Szrj reg1 = regno_reg_rtx[ALLOCNO_REGNO (a1)];
1966*38fd1498Szrj reg2 = regno_reg_rtx[ALLOCNO_REGNO (a2)];
1967*38fd1498Szrj if (reg1 != NULL && reg2 != NULL
1968*38fd1498Szrj && ORIGINAL_REGNO (reg1) == ORIGINAL_REGNO (reg2))
1969*38fd1498Szrj return false;
1970*38fd1498Szrj
1971*38fd1498Szrj /* We don't keep live ranges for caps because they can be quite big.
1972*38fd1498Szrj Use ranges of non-cap allocno from which caps are created. */
1973*38fd1498Szrj a1 = get_cap_member (a1);
1974*38fd1498Szrj a2 = get_cap_member (a2);
1975*38fd1498Szrj for (i = 0; i < n1; i++)
1976*38fd1498Szrj {
1977*38fd1498Szrj ira_object_t c1 = ALLOCNO_OBJECT (a1, i);
1978*38fd1498Szrj
1979*38fd1498Szrj for (j = 0; j < n2; j++)
1980*38fd1498Szrj {
1981*38fd1498Szrj ira_object_t c2 = ALLOCNO_OBJECT (a2, j);
1982*38fd1498Szrj
1983*38fd1498Szrj if (ira_live_ranges_intersect_p (OBJECT_LIVE_RANGES (c1),
1984*38fd1498Szrj OBJECT_LIVE_RANGES (c2)))
1985*38fd1498Szrj return true;
1986*38fd1498Szrj }
1987*38fd1498Szrj }
1988*38fd1498Szrj return false;
1989*38fd1498Szrj }
1990*38fd1498Szrj
1991*38fd1498Szrj /* The function is used to sort copies according to their execution
1992*38fd1498Szrj frequencies. */
1993*38fd1498Szrj static int
copy_freq_compare_func(const void * v1p,const void * v2p)1994*38fd1498Szrj copy_freq_compare_func (const void *v1p, const void *v2p)
1995*38fd1498Szrj {
1996*38fd1498Szrj ira_copy_t cp1 = *(const ira_copy_t *) v1p, cp2 = *(const ira_copy_t *) v2p;
1997*38fd1498Szrj int pri1, pri2;
1998*38fd1498Szrj
1999*38fd1498Szrj pri1 = cp1->freq;
2000*38fd1498Szrj pri2 = cp2->freq;
2001*38fd1498Szrj if (pri2 - pri1)
2002*38fd1498Szrj return pri2 - pri1;
2003*38fd1498Szrj
2004*38fd1498Szrj /* If frequencies are equal, sort by copies, so that the results of
2005*38fd1498Szrj qsort leave nothing to chance. */
2006*38fd1498Szrj return cp1->num - cp2->num;
2007*38fd1498Szrj }
2008*38fd1498Szrj
2009*38fd1498Szrj
2010*38fd1498Szrj
2011*38fd1498Szrj /* Return true if any allocno from thread of A1 conflicts with any
2012*38fd1498Szrj allocno from thread A2. */
2013*38fd1498Szrj static bool
allocno_thread_conflict_p(ira_allocno_t a1,ira_allocno_t a2)2014*38fd1498Szrj allocno_thread_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
2015*38fd1498Szrj {
2016*38fd1498Szrj ira_allocno_t a, conflict_a;
2017*38fd1498Szrj
2018*38fd1498Szrj for (a = ALLOCNO_COLOR_DATA (a2)->next_thread_allocno;;
2019*38fd1498Szrj a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2020*38fd1498Szrj {
2021*38fd1498Szrj for (conflict_a = ALLOCNO_COLOR_DATA (a1)->next_thread_allocno;;
2022*38fd1498Szrj conflict_a = ALLOCNO_COLOR_DATA (conflict_a)->next_thread_allocno)
2023*38fd1498Szrj {
2024*38fd1498Szrj if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
2025*38fd1498Szrj return true;
2026*38fd1498Szrj if (conflict_a == a1)
2027*38fd1498Szrj break;
2028*38fd1498Szrj }
2029*38fd1498Szrj if (a == a2)
2030*38fd1498Szrj break;
2031*38fd1498Szrj }
2032*38fd1498Szrj return false;
2033*38fd1498Szrj }
2034*38fd1498Szrj
2035*38fd1498Szrj /* Merge two threads given correspondingly by their first allocnos T1
2036*38fd1498Szrj and T2 (more accurately merging T2 into T1). */
2037*38fd1498Szrj static void
merge_threads(ira_allocno_t t1,ira_allocno_t t2)2038*38fd1498Szrj merge_threads (ira_allocno_t t1, ira_allocno_t t2)
2039*38fd1498Szrj {
2040*38fd1498Szrj ira_allocno_t a, next, last;
2041*38fd1498Szrj
2042*38fd1498Szrj gcc_assert (t1 != t2
2043*38fd1498Szrj && ALLOCNO_COLOR_DATA (t1)->first_thread_allocno == t1
2044*38fd1498Szrj && ALLOCNO_COLOR_DATA (t2)->first_thread_allocno == t2);
2045*38fd1498Szrj for (last = t2, a = ALLOCNO_COLOR_DATA (t2)->next_thread_allocno;;
2046*38fd1498Szrj a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2047*38fd1498Szrj {
2048*38fd1498Szrj ALLOCNO_COLOR_DATA (a)->first_thread_allocno = t1;
2049*38fd1498Szrj if (a == t2)
2050*38fd1498Szrj break;
2051*38fd1498Szrj last = a;
2052*38fd1498Szrj }
2053*38fd1498Szrj next = ALLOCNO_COLOR_DATA (t1)->next_thread_allocno;
2054*38fd1498Szrj ALLOCNO_COLOR_DATA (t1)->next_thread_allocno = t2;
2055*38fd1498Szrj ALLOCNO_COLOR_DATA (last)->next_thread_allocno = next;
2056*38fd1498Szrj ALLOCNO_COLOR_DATA (t1)->thread_freq += ALLOCNO_COLOR_DATA (t2)->thread_freq;
2057*38fd1498Szrj }
2058*38fd1498Szrj
2059*38fd1498Szrj /* Create threads by processing CP_NUM copies from sorted copies. We
2060*38fd1498Szrj process the most expensive copies first. */
2061*38fd1498Szrj static void
form_threads_from_copies(int cp_num)2062*38fd1498Szrj form_threads_from_copies (int cp_num)
2063*38fd1498Szrj {
2064*38fd1498Szrj ira_allocno_t a, thread1, thread2;
2065*38fd1498Szrj ira_copy_t cp;
2066*38fd1498Szrj int i, n;
2067*38fd1498Szrj
2068*38fd1498Szrj qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
2069*38fd1498Szrj /* Form threads processing copies, most frequently executed
2070*38fd1498Szrj first. */
2071*38fd1498Szrj for (; cp_num != 0;)
2072*38fd1498Szrj {
2073*38fd1498Szrj for (i = 0; i < cp_num; i++)
2074*38fd1498Szrj {
2075*38fd1498Szrj cp = sorted_copies[i];
2076*38fd1498Szrj thread1 = ALLOCNO_COLOR_DATA (cp->first)->first_thread_allocno;
2077*38fd1498Szrj thread2 = ALLOCNO_COLOR_DATA (cp->second)->first_thread_allocno;
2078*38fd1498Szrj if (thread1 == thread2)
2079*38fd1498Szrj continue;
2080*38fd1498Szrj if (! allocno_thread_conflict_p (thread1, thread2))
2081*38fd1498Szrj {
2082*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2083*38fd1498Szrj fprintf
2084*38fd1498Szrj (ira_dump_file,
2085*38fd1498Szrj " Forming thread by copy %d:a%dr%d-a%dr%d (freq=%d):\n",
2086*38fd1498Szrj cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
2087*38fd1498Szrj ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
2088*38fd1498Szrj cp->freq);
2089*38fd1498Szrj merge_threads (thread1, thread2);
2090*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2091*38fd1498Szrj {
2092*38fd1498Szrj thread1 = ALLOCNO_COLOR_DATA (thread1)->first_thread_allocno;
2093*38fd1498Szrj fprintf (ira_dump_file, " Result (freq=%d): a%dr%d(%d)",
2094*38fd1498Szrj ALLOCNO_COLOR_DATA (thread1)->thread_freq,
2095*38fd1498Szrj ALLOCNO_NUM (thread1), ALLOCNO_REGNO (thread1),
2096*38fd1498Szrj ALLOCNO_FREQ (thread1));
2097*38fd1498Szrj for (a = ALLOCNO_COLOR_DATA (thread1)->next_thread_allocno;
2098*38fd1498Szrj a != thread1;
2099*38fd1498Szrj a = ALLOCNO_COLOR_DATA (a)->next_thread_allocno)
2100*38fd1498Szrj fprintf (ira_dump_file, " a%dr%d(%d)",
2101*38fd1498Szrj ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2102*38fd1498Szrj ALLOCNO_FREQ (a));
2103*38fd1498Szrj fprintf (ira_dump_file, "\n");
2104*38fd1498Szrj }
2105*38fd1498Szrj i++;
2106*38fd1498Szrj break;
2107*38fd1498Szrj }
2108*38fd1498Szrj }
2109*38fd1498Szrj /* Collect the rest of copies. */
2110*38fd1498Szrj for (n = 0; i < cp_num; i++)
2111*38fd1498Szrj {
2112*38fd1498Szrj cp = sorted_copies[i];
2113*38fd1498Szrj if (ALLOCNO_COLOR_DATA (cp->first)->first_thread_allocno
2114*38fd1498Szrj != ALLOCNO_COLOR_DATA (cp->second)->first_thread_allocno)
2115*38fd1498Szrj sorted_copies[n++] = cp;
2116*38fd1498Szrj }
2117*38fd1498Szrj cp_num = n;
2118*38fd1498Szrj }
2119*38fd1498Szrj }
2120*38fd1498Szrj
2121*38fd1498Szrj /* Create threads by processing copies of all alocnos from BUCKET. We
2122*38fd1498Szrj process the most expensive copies first. */
2123*38fd1498Szrj static void
form_threads_from_bucket(ira_allocno_t bucket)2124*38fd1498Szrj form_threads_from_bucket (ira_allocno_t bucket)
2125*38fd1498Szrj {
2126*38fd1498Szrj ira_allocno_t a;
2127*38fd1498Szrj ira_copy_t cp, next_cp;
2128*38fd1498Szrj int cp_num = 0;
2129*38fd1498Szrj
2130*38fd1498Szrj for (a = bucket; a != NULL; a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2131*38fd1498Szrj {
2132*38fd1498Szrj for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2133*38fd1498Szrj {
2134*38fd1498Szrj if (cp->first == a)
2135*38fd1498Szrj {
2136*38fd1498Szrj next_cp = cp->next_first_allocno_copy;
2137*38fd1498Szrj sorted_copies[cp_num++] = cp;
2138*38fd1498Szrj }
2139*38fd1498Szrj else if (cp->second == a)
2140*38fd1498Szrj next_cp = cp->next_second_allocno_copy;
2141*38fd1498Szrj else
2142*38fd1498Szrj gcc_unreachable ();
2143*38fd1498Szrj }
2144*38fd1498Szrj }
2145*38fd1498Szrj form_threads_from_copies (cp_num);
2146*38fd1498Szrj }
2147*38fd1498Szrj
2148*38fd1498Szrj /* Create threads by processing copies of colorable allocno A. We
2149*38fd1498Szrj process most expensive copies first. */
2150*38fd1498Szrj static void
form_threads_from_colorable_allocno(ira_allocno_t a)2151*38fd1498Szrj form_threads_from_colorable_allocno (ira_allocno_t a)
2152*38fd1498Szrj {
2153*38fd1498Szrj ira_allocno_t another_a;
2154*38fd1498Szrj ira_copy_t cp, next_cp;
2155*38fd1498Szrj int cp_num = 0;
2156*38fd1498Szrj
2157*38fd1498Szrj for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
2158*38fd1498Szrj {
2159*38fd1498Szrj if (cp->first == a)
2160*38fd1498Szrj {
2161*38fd1498Szrj next_cp = cp->next_first_allocno_copy;
2162*38fd1498Szrj another_a = cp->second;
2163*38fd1498Szrj }
2164*38fd1498Szrj else if (cp->second == a)
2165*38fd1498Szrj {
2166*38fd1498Szrj next_cp = cp->next_second_allocno_copy;
2167*38fd1498Szrj another_a = cp->first;
2168*38fd1498Szrj }
2169*38fd1498Szrj else
2170*38fd1498Szrj gcc_unreachable ();
2171*38fd1498Szrj if ((! ALLOCNO_COLOR_DATA (another_a)->in_graph_p
2172*38fd1498Szrj && !ALLOCNO_COLOR_DATA (another_a)->may_be_spilled_p)
2173*38fd1498Szrj || ALLOCNO_COLOR_DATA (another_a)->colorable_p)
2174*38fd1498Szrj sorted_copies[cp_num++] = cp;
2175*38fd1498Szrj }
2176*38fd1498Szrj form_threads_from_copies (cp_num);
2177*38fd1498Szrj }
2178*38fd1498Szrj
2179*38fd1498Szrj /* Form initial threads which contain only one allocno. */
2180*38fd1498Szrj static void
init_allocno_threads(void)2181*38fd1498Szrj init_allocno_threads (void)
2182*38fd1498Szrj {
2183*38fd1498Szrj ira_allocno_t a;
2184*38fd1498Szrj unsigned int j;
2185*38fd1498Szrj bitmap_iterator bi;
2186*38fd1498Szrj
2187*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
2188*38fd1498Szrj {
2189*38fd1498Szrj a = ira_allocnos[j];
2190*38fd1498Szrj /* Set up initial thread data: */
2191*38fd1498Szrj ALLOCNO_COLOR_DATA (a)->first_thread_allocno
2192*38fd1498Szrj = ALLOCNO_COLOR_DATA (a)->next_thread_allocno = a;
2193*38fd1498Szrj ALLOCNO_COLOR_DATA (a)->thread_freq = ALLOCNO_FREQ (a);
2194*38fd1498Szrj }
2195*38fd1498Szrj }
2196*38fd1498Szrj
2197*38fd1498Szrj
2198*38fd1498Szrj
2199*38fd1498Szrj /* This page contains the allocator based on the Chaitin-Briggs algorithm. */
2200*38fd1498Szrj
2201*38fd1498Szrj /* Bucket of allocnos that can colored currently without spilling. */
2202*38fd1498Szrj static ira_allocno_t colorable_allocno_bucket;
2203*38fd1498Szrj
2204*38fd1498Szrj /* Bucket of allocnos that might be not colored currently without
2205*38fd1498Szrj spilling. */
2206*38fd1498Szrj static ira_allocno_t uncolorable_allocno_bucket;
2207*38fd1498Szrj
2208*38fd1498Szrj /* The current number of allocnos in the uncolorable_bucket. */
2209*38fd1498Szrj static int uncolorable_allocnos_num;
2210*38fd1498Szrj
2211*38fd1498Szrj /* Return the current spill priority of allocno A. The less the
2212*38fd1498Szrj number, the more preferable the allocno for spilling. */
2213*38fd1498Szrj static inline int
allocno_spill_priority(ira_allocno_t a)2214*38fd1498Szrj allocno_spill_priority (ira_allocno_t a)
2215*38fd1498Szrj {
2216*38fd1498Szrj allocno_color_data_t data = ALLOCNO_COLOR_DATA (a);
2217*38fd1498Szrj
2218*38fd1498Szrj return (data->temp
2219*38fd1498Szrj / (ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
2220*38fd1498Szrj * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]
2221*38fd1498Szrj + 1));
2222*38fd1498Szrj }
2223*38fd1498Szrj
2224*38fd1498Szrj /* Add allocno A to bucket *BUCKET_PTR. A should be not in a bucket
2225*38fd1498Szrj before the call. */
2226*38fd1498Szrj static void
add_allocno_to_bucket(ira_allocno_t a,ira_allocno_t * bucket_ptr)2227*38fd1498Szrj add_allocno_to_bucket (ira_allocno_t a, ira_allocno_t *bucket_ptr)
2228*38fd1498Szrj {
2229*38fd1498Szrj ira_allocno_t first_a;
2230*38fd1498Szrj allocno_color_data_t data;
2231*38fd1498Szrj
2232*38fd1498Szrj if (bucket_ptr == &uncolorable_allocno_bucket
2233*38fd1498Szrj && ALLOCNO_CLASS (a) != NO_REGS)
2234*38fd1498Szrj {
2235*38fd1498Szrj uncolorable_allocnos_num++;
2236*38fd1498Szrj ira_assert (uncolorable_allocnos_num > 0);
2237*38fd1498Szrj }
2238*38fd1498Szrj first_a = *bucket_ptr;
2239*38fd1498Szrj data = ALLOCNO_COLOR_DATA (a);
2240*38fd1498Szrj data->next_bucket_allocno = first_a;
2241*38fd1498Szrj data->prev_bucket_allocno = NULL;
2242*38fd1498Szrj if (first_a != NULL)
2243*38fd1498Szrj ALLOCNO_COLOR_DATA (first_a)->prev_bucket_allocno = a;
2244*38fd1498Szrj *bucket_ptr = a;
2245*38fd1498Szrj }
2246*38fd1498Szrj
2247*38fd1498Szrj /* Compare two allocnos to define which allocno should be pushed first
2248*38fd1498Szrj into the coloring stack. If the return is a negative number, the
2249*38fd1498Szrj allocno given by the first parameter will be pushed first. In this
2250*38fd1498Szrj case such allocno has less priority than the second one and the
2251*38fd1498Szrj hard register will be assigned to it after assignment to the second
2252*38fd1498Szrj one. As the result of such assignment order, the second allocno
2253*38fd1498Szrj has a better chance to get the best hard register. */
2254*38fd1498Szrj static int
bucket_allocno_compare_func(const void * v1p,const void * v2p)2255*38fd1498Szrj bucket_allocno_compare_func (const void *v1p, const void *v2p)
2256*38fd1498Szrj {
2257*38fd1498Szrj ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2258*38fd1498Szrj ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2259*38fd1498Szrj int diff, freq1, freq2, a1_num, a2_num, pref1, pref2;
2260*38fd1498Szrj ira_allocno_t t1 = ALLOCNO_COLOR_DATA (a1)->first_thread_allocno;
2261*38fd1498Szrj ira_allocno_t t2 = ALLOCNO_COLOR_DATA (a2)->first_thread_allocno;
2262*38fd1498Szrj int cl1 = ALLOCNO_CLASS (a1), cl2 = ALLOCNO_CLASS (a2);
2263*38fd1498Szrj
2264*38fd1498Szrj freq1 = ALLOCNO_COLOR_DATA (t1)->thread_freq;
2265*38fd1498Szrj freq2 = ALLOCNO_COLOR_DATA (t2)->thread_freq;
2266*38fd1498Szrj if ((diff = freq1 - freq2) != 0)
2267*38fd1498Szrj return diff;
2268*38fd1498Szrj
2269*38fd1498Szrj if ((diff = ALLOCNO_NUM (t2) - ALLOCNO_NUM (t1)) != 0)
2270*38fd1498Szrj return diff;
2271*38fd1498Szrj
2272*38fd1498Szrj /* Push pseudos requiring less hard registers first. It means that
2273*38fd1498Szrj we will assign pseudos requiring more hard registers first
2274*38fd1498Szrj avoiding creation small holes in free hard register file into
2275*38fd1498Szrj which the pseudos requiring more hard registers can not fit. */
2276*38fd1498Szrj if ((diff = (ira_reg_class_max_nregs[cl1][ALLOCNO_MODE (a1)]
2277*38fd1498Szrj - ira_reg_class_max_nregs[cl2][ALLOCNO_MODE (a2)])) != 0)
2278*38fd1498Szrj return diff;
2279*38fd1498Szrj
2280*38fd1498Szrj freq1 = ALLOCNO_FREQ (a1);
2281*38fd1498Szrj freq2 = ALLOCNO_FREQ (a2);
2282*38fd1498Szrj if ((diff = freq1 - freq2) != 0)
2283*38fd1498Szrj return diff;
2284*38fd1498Szrj
2285*38fd1498Szrj a1_num = ALLOCNO_COLOR_DATA (a1)->available_regs_num;
2286*38fd1498Szrj a2_num = ALLOCNO_COLOR_DATA (a2)->available_regs_num;
2287*38fd1498Szrj if ((diff = a2_num - a1_num) != 0)
2288*38fd1498Szrj return diff;
2289*38fd1498Szrj /* Push allocnos with minimal conflict_allocno_hard_prefs first. */
2290*38fd1498Szrj pref1 = ALLOCNO_COLOR_DATA (a1)->conflict_allocno_hard_prefs;
2291*38fd1498Szrj pref2 = ALLOCNO_COLOR_DATA (a2)->conflict_allocno_hard_prefs;
2292*38fd1498Szrj if ((diff = pref1 - pref2) != 0)
2293*38fd1498Szrj return diff;
2294*38fd1498Szrj return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
2295*38fd1498Szrj }
2296*38fd1498Szrj
2297*38fd1498Szrj /* Sort bucket *BUCKET_PTR and return the result through
2298*38fd1498Szrj BUCKET_PTR. */
2299*38fd1498Szrj static void
sort_bucket(ira_allocno_t * bucket_ptr,int (* compare_func)(const void *,const void *))2300*38fd1498Szrj sort_bucket (ira_allocno_t *bucket_ptr,
2301*38fd1498Szrj int (*compare_func) (const void *, const void *))
2302*38fd1498Szrj {
2303*38fd1498Szrj ira_allocno_t a, head;
2304*38fd1498Szrj int n;
2305*38fd1498Szrj
2306*38fd1498Szrj for (n = 0, a = *bucket_ptr;
2307*38fd1498Szrj a != NULL;
2308*38fd1498Szrj a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2309*38fd1498Szrj sorted_allocnos[n++] = a;
2310*38fd1498Szrj if (n <= 1)
2311*38fd1498Szrj return;
2312*38fd1498Szrj qsort (sorted_allocnos, n, sizeof (ira_allocno_t), compare_func);
2313*38fd1498Szrj head = NULL;
2314*38fd1498Szrj for (n--; n >= 0; n--)
2315*38fd1498Szrj {
2316*38fd1498Szrj a = sorted_allocnos[n];
2317*38fd1498Szrj ALLOCNO_COLOR_DATA (a)->next_bucket_allocno = head;
2318*38fd1498Szrj ALLOCNO_COLOR_DATA (a)->prev_bucket_allocno = NULL;
2319*38fd1498Szrj if (head != NULL)
2320*38fd1498Szrj ALLOCNO_COLOR_DATA (head)->prev_bucket_allocno = a;
2321*38fd1498Szrj head = a;
2322*38fd1498Szrj }
2323*38fd1498Szrj *bucket_ptr = head;
2324*38fd1498Szrj }
2325*38fd1498Szrj
2326*38fd1498Szrj /* Add ALLOCNO to colorable bucket maintaining the order according
2327*38fd1498Szrj their priority. ALLOCNO should be not in a bucket before the
2328*38fd1498Szrj call. */
2329*38fd1498Szrj static void
add_allocno_to_ordered_colorable_bucket(ira_allocno_t allocno)2330*38fd1498Szrj add_allocno_to_ordered_colorable_bucket (ira_allocno_t allocno)
2331*38fd1498Szrj {
2332*38fd1498Szrj ira_allocno_t before, after;
2333*38fd1498Szrj
2334*38fd1498Szrj form_threads_from_colorable_allocno (allocno);
2335*38fd1498Szrj for (before = colorable_allocno_bucket, after = NULL;
2336*38fd1498Szrj before != NULL;
2337*38fd1498Szrj after = before,
2338*38fd1498Szrj before = ALLOCNO_COLOR_DATA (before)->next_bucket_allocno)
2339*38fd1498Szrj if (bucket_allocno_compare_func (&allocno, &before) < 0)
2340*38fd1498Szrj break;
2341*38fd1498Szrj ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno = before;
2342*38fd1498Szrj ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno = after;
2343*38fd1498Szrj if (after == NULL)
2344*38fd1498Szrj colorable_allocno_bucket = allocno;
2345*38fd1498Szrj else
2346*38fd1498Szrj ALLOCNO_COLOR_DATA (after)->next_bucket_allocno = allocno;
2347*38fd1498Szrj if (before != NULL)
2348*38fd1498Szrj ALLOCNO_COLOR_DATA (before)->prev_bucket_allocno = allocno;
2349*38fd1498Szrj }
2350*38fd1498Szrj
2351*38fd1498Szrj /* Delete ALLOCNO from bucket *BUCKET_PTR. It should be there before
2352*38fd1498Szrj the call. */
2353*38fd1498Szrj static void
delete_allocno_from_bucket(ira_allocno_t allocno,ira_allocno_t * bucket_ptr)2354*38fd1498Szrj delete_allocno_from_bucket (ira_allocno_t allocno, ira_allocno_t *bucket_ptr)
2355*38fd1498Szrj {
2356*38fd1498Szrj ira_allocno_t prev_allocno, next_allocno;
2357*38fd1498Szrj
2358*38fd1498Szrj if (bucket_ptr == &uncolorable_allocno_bucket
2359*38fd1498Szrj && ALLOCNO_CLASS (allocno) != NO_REGS)
2360*38fd1498Szrj {
2361*38fd1498Szrj uncolorable_allocnos_num--;
2362*38fd1498Szrj ira_assert (uncolorable_allocnos_num >= 0);
2363*38fd1498Szrj }
2364*38fd1498Szrj prev_allocno = ALLOCNO_COLOR_DATA (allocno)->prev_bucket_allocno;
2365*38fd1498Szrj next_allocno = ALLOCNO_COLOR_DATA (allocno)->next_bucket_allocno;
2366*38fd1498Szrj if (prev_allocno != NULL)
2367*38fd1498Szrj ALLOCNO_COLOR_DATA (prev_allocno)->next_bucket_allocno = next_allocno;
2368*38fd1498Szrj else
2369*38fd1498Szrj {
2370*38fd1498Szrj ira_assert (*bucket_ptr == allocno);
2371*38fd1498Szrj *bucket_ptr = next_allocno;
2372*38fd1498Szrj }
2373*38fd1498Szrj if (next_allocno != NULL)
2374*38fd1498Szrj ALLOCNO_COLOR_DATA (next_allocno)->prev_bucket_allocno = prev_allocno;
2375*38fd1498Szrj }
2376*38fd1498Szrj
2377*38fd1498Szrj /* Put allocno A onto the coloring stack without removing it from its
2378*38fd1498Szrj bucket. Pushing allocno to the coloring stack can result in moving
2379*38fd1498Szrj conflicting allocnos from the uncolorable bucket to the colorable
2380*38fd1498Szrj one. Update conflict_allocno_hard_prefs of the conflicting
2381*38fd1498Szrj allocnos which are not on stack yet. */
2382*38fd1498Szrj static void
push_allocno_to_stack(ira_allocno_t a)2383*38fd1498Szrj push_allocno_to_stack (ira_allocno_t a)
2384*38fd1498Szrj {
2385*38fd1498Szrj enum reg_class aclass;
2386*38fd1498Szrj allocno_color_data_t data, conflict_data;
2387*38fd1498Szrj int size, i, n = ALLOCNO_NUM_OBJECTS (a);
2388*38fd1498Szrj
2389*38fd1498Szrj data = ALLOCNO_COLOR_DATA (a);
2390*38fd1498Szrj data->in_graph_p = false;
2391*38fd1498Szrj allocno_stack_vec.safe_push (a);
2392*38fd1498Szrj aclass = ALLOCNO_CLASS (a);
2393*38fd1498Szrj if (aclass == NO_REGS)
2394*38fd1498Szrj return;
2395*38fd1498Szrj size = ira_reg_class_max_nregs[aclass][ALLOCNO_MODE (a)];
2396*38fd1498Szrj if (n > 1)
2397*38fd1498Szrj {
2398*38fd1498Szrj /* We will deal with the subwords individually. */
2399*38fd1498Szrj gcc_assert (size == ALLOCNO_NUM_OBJECTS (a));
2400*38fd1498Szrj size = 1;
2401*38fd1498Szrj }
2402*38fd1498Szrj for (i = 0; i < n; i++)
2403*38fd1498Szrj {
2404*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, i);
2405*38fd1498Szrj ira_object_t conflict_obj;
2406*38fd1498Szrj ira_object_conflict_iterator oci;
2407*38fd1498Szrj
2408*38fd1498Szrj FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2409*38fd1498Szrj {
2410*38fd1498Szrj ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2411*38fd1498Szrj ira_pref_t pref;
2412*38fd1498Szrj
2413*38fd1498Szrj conflict_data = ALLOCNO_COLOR_DATA (conflict_a);
2414*38fd1498Szrj if (! conflict_data->in_graph_p
2415*38fd1498Szrj || ALLOCNO_ASSIGNED_P (conflict_a)
2416*38fd1498Szrj || !(hard_reg_set_intersect_p
2417*38fd1498Szrj (ALLOCNO_COLOR_DATA (a)->profitable_hard_regs,
2418*38fd1498Szrj conflict_data->profitable_hard_regs)))
2419*38fd1498Szrj continue;
2420*38fd1498Szrj for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
2421*38fd1498Szrj conflict_data->conflict_allocno_hard_prefs -= pref->freq;
2422*38fd1498Szrj if (conflict_data->colorable_p)
2423*38fd1498Szrj continue;
2424*38fd1498Szrj ira_assert (bitmap_bit_p (coloring_allocno_bitmap,
2425*38fd1498Szrj ALLOCNO_NUM (conflict_a)));
2426*38fd1498Szrj if (update_left_conflict_sizes_p (conflict_a, a, size))
2427*38fd1498Szrj {
2428*38fd1498Szrj delete_allocno_from_bucket
2429*38fd1498Szrj (conflict_a, &uncolorable_allocno_bucket);
2430*38fd1498Szrj add_allocno_to_ordered_colorable_bucket (conflict_a);
2431*38fd1498Szrj if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2432*38fd1498Szrj {
2433*38fd1498Szrj fprintf (ira_dump_file, " Making");
2434*38fd1498Szrj ira_print_expanded_allocno (conflict_a);
2435*38fd1498Szrj fprintf (ira_dump_file, " colorable\n");
2436*38fd1498Szrj }
2437*38fd1498Szrj }
2438*38fd1498Szrj
2439*38fd1498Szrj }
2440*38fd1498Szrj }
2441*38fd1498Szrj }
2442*38fd1498Szrj
2443*38fd1498Szrj /* Put ALLOCNO onto the coloring stack and remove it from its bucket.
2444*38fd1498Szrj The allocno is in the colorable bucket if COLORABLE_P is TRUE. */
2445*38fd1498Szrj static void
remove_allocno_from_bucket_and_push(ira_allocno_t allocno,bool colorable_p)2446*38fd1498Szrj remove_allocno_from_bucket_and_push (ira_allocno_t allocno, bool colorable_p)
2447*38fd1498Szrj {
2448*38fd1498Szrj if (colorable_p)
2449*38fd1498Szrj delete_allocno_from_bucket (allocno, &colorable_allocno_bucket);
2450*38fd1498Szrj else
2451*38fd1498Szrj delete_allocno_from_bucket (allocno, &uncolorable_allocno_bucket);
2452*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2453*38fd1498Szrj {
2454*38fd1498Szrj fprintf (ira_dump_file, " Pushing");
2455*38fd1498Szrj ira_print_expanded_allocno (allocno);
2456*38fd1498Szrj if (colorable_p)
2457*38fd1498Szrj fprintf (ira_dump_file, "(cost %d)\n",
2458*38fd1498Szrj ALLOCNO_COLOR_DATA (allocno)->temp);
2459*38fd1498Szrj else
2460*38fd1498Szrj fprintf (ira_dump_file, "(potential spill: %spri=%d, cost=%d)\n",
2461*38fd1498Szrj ALLOCNO_BAD_SPILL_P (allocno) ? "bad spill, " : "",
2462*38fd1498Szrj allocno_spill_priority (allocno),
2463*38fd1498Szrj ALLOCNO_COLOR_DATA (allocno)->temp);
2464*38fd1498Szrj }
2465*38fd1498Szrj if (! colorable_p)
2466*38fd1498Szrj ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p = true;
2467*38fd1498Szrj push_allocno_to_stack (allocno);
2468*38fd1498Szrj }
2469*38fd1498Szrj
2470*38fd1498Szrj /* Put all allocnos from colorable bucket onto the coloring stack. */
2471*38fd1498Szrj static void
push_only_colorable(void)2472*38fd1498Szrj push_only_colorable (void)
2473*38fd1498Szrj {
2474*38fd1498Szrj form_threads_from_bucket (colorable_allocno_bucket);
2475*38fd1498Szrj sort_bucket (&colorable_allocno_bucket, bucket_allocno_compare_func);
2476*38fd1498Szrj for (;colorable_allocno_bucket != NULL;)
2477*38fd1498Szrj remove_allocno_from_bucket_and_push (colorable_allocno_bucket, true);
2478*38fd1498Szrj }
2479*38fd1498Szrj
2480*38fd1498Szrj /* Return the frequency of exit edges (if EXIT_P) or entry from/to the
2481*38fd1498Szrj loop given by its LOOP_NODE. */
2482*38fd1498Szrj int
ira_loop_edge_freq(ira_loop_tree_node_t loop_node,int regno,bool exit_p)2483*38fd1498Szrj ira_loop_edge_freq (ira_loop_tree_node_t loop_node, int regno, bool exit_p)
2484*38fd1498Szrj {
2485*38fd1498Szrj int freq, i;
2486*38fd1498Szrj edge_iterator ei;
2487*38fd1498Szrj edge e;
2488*38fd1498Szrj vec<edge> edges;
2489*38fd1498Szrj
2490*38fd1498Szrj ira_assert (current_loops != NULL && loop_node->loop != NULL
2491*38fd1498Szrj && (regno < 0 || regno >= FIRST_PSEUDO_REGISTER));
2492*38fd1498Szrj freq = 0;
2493*38fd1498Szrj if (! exit_p)
2494*38fd1498Szrj {
2495*38fd1498Szrj FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
2496*38fd1498Szrj if (e->src != loop_node->loop->latch
2497*38fd1498Szrj && (regno < 0
2498*38fd1498Szrj || (bitmap_bit_p (df_get_live_out (e->src), regno)
2499*38fd1498Szrj && bitmap_bit_p (df_get_live_in (e->dest), regno))))
2500*38fd1498Szrj freq += EDGE_FREQUENCY (e);
2501*38fd1498Szrj }
2502*38fd1498Szrj else
2503*38fd1498Szrj {
2504*38fd1498Szrj edges = get_loop_exit_edges (loop_node->loop);
2505*38fd1498Szrj FOR_EACH_VEC_ELT (edges, i, e)
2506*38fd1498Szrj if (regno < 0
2507*38fd1498Szrj || (bitmap_bit_p (df_get_live_out (e->src), regno)
2508*38fd1498Szrj && bitmap_bit_p (df_get_live_in (e->dest), regno)))
2509*38fd1498Szrj freq += EDGE_FREQUENCY (e);
2510*38fd1498Szrj edges.release ();
2511*38fd1498Szrj }
2512*38fd1498Szrj
2513*38fd1498Szrj return REG_FREQ_FROM_EDGE_FREQ (freq);
2514*38fd1498Szrj }
2515*38fd1498Szrj
2516*38fd1498Szrj /* Calculate and return the cost of putting allocno A into memory. */
2517*38fd1498Szrj static int
calculate_allocno_spill_cost(ira_allocno_t a)2518*38fd1498Szrj calculate_allocno_spill_cost (ira_allocno_t a)
2519*38fd1498Szrj {
2520*38fd1498Szrj int regno, cost;
2521*38fd1498Szrj machine_mode mode;
2522*38fd1498Szrj enum reg_class rclass;
2523*38fd1498Szrj ira_allocno_t parent_allocno;
2524*38fd1498Szrj ira_loop_tree_node_t parent_node, loop_node;
2525*38fd1498Szrj
2526*38fd1498Szrj regno = ALLOCNO_REGNO (a);
2527*38fd1498Szrj cost = ALLOCNO_UPDATED_MEMORY_COST (a) - ALLOCNO_UPDATED_CLASS_COST (a);
2528*38fd1498Szrj if (ALLOCNO_CAP (a) != NULL)
2529*38fd1498Szrj return cost;
2530*38fd1498Szrj loop_node = ALLOCNO_LOOP_TREE_NODE (a);
2531*38fd1498Szrj if ((parent_node = loop_node->parent) == NULL)
2532*38fd1498Szrj return cost;
2533*38fd1498Szrj if ((parent_allocno = parent_node->regno_allocno_map[regno]) == NULL)
2534*38fd1498Szrj return cost;
2535*38fd1498Szrj mode = ALLOCNO_MODE (a);
2536*38fd1498Szrj rclass = ALLOCNO_CLASS (a);
2537*38fd1498Szrj if (ALLOCNO_HARD_REGNO (parent_allocno) < 0)
2538*38fd1498Szrj cost -= (ira_memory_move_cost[mode][rclass][0]
2539*38fd1498Szrj * ira_loop_edge_freq (loop_node, regno, true)
2540*38fd1498Szrj + ira_memory_move_cost[mode][rclass][1]
2541*38fd1498Szrj * ira_loop_edge_freq (loop_node, regno, false));
2542*38fd1498Szrj else
2543*38fd1498Szrj {
2544*38fd1498Szrj ira_init_register_move_cost_if_necessary (mode);
2545*38fd1498Szrj cost += ((ira_memory_move_cost[mode][rclass][1]
2546*38fd1498Szrj * ira_loop_edge_freq (loop_node, regno, true)
2547*38fd1498Szrj + ira_memory_move_cost[mode][rclass][0]
2548*38fd1498Szrj * ira_loop_edge_freq (loop_node, regno, false))
2549*38fd1498Szrj - (ira_register_move_cost[mode][rclass][rclass]
2550*38fd1498Szrj * (ira_loop_edge_freq (loop_node, regno, false)
2551*38fd1498Szrj + ira_loop_edge_freq (loop_node, regno, true))));
2552*38fd1498Szrj }
2553*38fd1498Szrj return cost;
2554*38fd1498Szrj }
2555*38fd1498Szrj
2556*38fd1498Szrj /* Used for sorting allocnos for spilling. */
2557*38fd1498Szrj static inline int
allocno_spill_priority_compare(ira_allocno_t a1,ira_allocno_t a2)2558*38fd1498Szrj allocno_spill_priority_compare (ira_allocno_t a1, ira_allocno_t a2)
2559*38fd1498Szrj {
2560*38fd1498Szrj int pri1, pri2, diff;
2561*38fd1498Szrj
2562*38fd1498Szrj /* Avoid spilling static chain pointer pseudo when non-local goto is
2563*38fd1498Szrj used. */
2564*38fd1498Szrj if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))
2565*38fd1498Szrj return 1;
2566*38fd1498Szrj else if (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2)))
2567*38fd1498Szrj return -1;
2568*38fd1498Szrj if (ALLOCNO_BAD_SPILL_P (a1) && ! ALLOCNO_BAD_SPILL_P (a2))
2569*38fd1498Szrj return 1;
2570*38fd1498Szrj if (ALLOCNO_BAD_SPILL_P (a2) && ! ALLOCNO_BAD_SPILL_P (a1))
2571*38fd1498Szrj return -1;
2572*38fd1498Szrj pri1 = allocno_spill_priority (a1);
2573*38fd1498Szrj pri2 = allocno_spill_priority (a2);
2574*38fd1498Szrj if ((diff = pri1 - pri2) != 0)
2575*38fd1498Szrj return diff;
2576*38fd1498Szrj if ((diff
2577*38fd1498Szrj = ALLOCNO_COLOR_DATA (a1)->temp - ALLOCNO_COLOR_DATA (a2)->temp) != 0)
2578*38fd1498Szrj return diff;
2579*38fd1498Szrj return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2580*38fd1498Szrj }
2581*38fd1498Szrj
2582*38fd1498Szrj /* Used for sorting allocnos for spilling. */
2583*38fd1498Szrj static int
allocno_spill_sort_compare(const void * v1p,const void * v2p)2584*38fd1498Szrj allocno_spill_sort_compare (const void *v1p, const void *v2p)
2585*38fd1498Szrj {
2586*38fd1498Szrj ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2587*38fd1498Szrj ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2588*38fd1498Szrj
2589*38fd1498Szrj return allocno_spill_priority_compare (p1, p2);
2590*38fd1498Szrj }
2591*38fd1498Szrj
2592*38fd1498Szrj /* Push allocnos to the coloring stack. The order of allocnos in the
2593*38fd1498Szrj stack defines the order for the subsequent coloring. */
2594*38fd1498Szrj static void
push_allocnos_to_stack(void)2595*38fd1498Szrj push_allocnos_to_stack (void)
2596*38fd1498Szrj {
2597*38fd1498Szrj ira_allocno_t a;
2598*38fd1498Szrj int cost;
2599*38fd1498Szrj
2600*38fd1498Szrj /* Calculate uncolorable allocno spill costs. */
2601*38fd1498Szrj for (a = uncolorable_allocno_bucket;
2602*38fd1498Szrj a != NULL;
2603*38fd1498Szrj a = ALLOCNO_COLOR_DATA (a)->next_bucket_allocno)
2604*38fd1498Szrj if (ALLOCNO_CLASS (a) != NO_REGS)
2605*38fd1498Szrj {
2606*38fd1498Szrj cost = calculate_allocno_spill_cost (a);
2607*38fd1498Szrj /* ??? Remove cost of copies between the coalesced
2608*38fd1498Szrj allocnos. */
2609*38fd1498Szrj ALLOCNO_COLOR_DATA (a)->temp = cost;
2610*38fd1498Szrj }
2611*38fd1498Szrj sort_bucket (&uncolorable_allocno_bucket, allocno_spill_sort_compare);
2612*38fd1498Szrj for (;;)
2613*38fd1498Szrj {
2614*38fd1498Szrj push_only_colorable ();
2615*38fd1498Szrj a = uncolorable_allocno_bucket;
2616*38fd1498Szrj if (a == NULL)
2617*38fd1498Szrj break;
2618*38fd1498Szrj remove_allocno_from_bucket_and_push (a, false);
2619*38fd1498Szrj }
2620*38fd1498Szrj ira_assert (colorable_allocno_bucket == NULL
2621*38fd1498Szrj && uncolorable_allocno_bucket == NULL);
2622*38fd1498Szrj ira_assert (uncolorable_allocnos_num == 0);
2623*38fd1498Szrj }
2624*38fd1498Szrj
2625*38fd1498Szrj /* Pop the coloring stack and assign hard registers to the popped
2626*38fd1498Szrj allocnos. */
2627*38fd1498Szrj static void
pop_allocnos_from_stack(void)2628*38fd1498Szrj pop_allocnos_from_stack (void)
2629*38fd1498Szrj {
2630*38fd1498Szrj ira_allocno_t allocno;
2631*38fd1498Szrj enum reg_class aclass;
2632*38fd1498Szrj
2633*38fd1498Szrj for (;allocno_stack_vec.length () != 0;)
2634*38fd1498Szrj {
2635*38fd1498Szrj allocno = allocno_stack_vec.pop ();
2636*38fd1498Szrj aclass = ALLOCNO_CLASS (allocno);
2637*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2638*38fd1498Szrj {
2639*38fd1498Szrj fprintf (ira_dump_file, " Popping");
2640*38fd1498Szrj ira_print_expanded_allocno (allocno);
2641*38fd1498Szrj fprintf (ira_dump_file, " -- ");
2642*38fd1498Szrj }
2643*38fd1498Szrj if (aclass == NO_REGS)
2644*38fd1498Szrj {
2645*38fd1498Szrj ALLOCNO_HARD_REGNO (allocno) = -1;
2646*38fd1498Szrj ALLOCNO_ASSIGNED_P (allocno) = true;
2647*38fd1498Szrj ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (allocno) == NULL);
2648*38fd1498Szrj ira_assert
2649*38fd1498Szrj (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (allocno) == NULL);
2650*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2651*38fd1498Szrj fprintf (ira_dump_file, "assign memory\n");
2652*38fd1498Szrj }
2653*38fd1498Szrj else if (assign_hard_reg (allocno, false))
2654*38fd1498Szrj {
2655*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2656*38fd1498Szrj fprintf (ira_dump_file, "assign reg %d\n",
2657*38fd1498Szrj ALLOCNO_HARD_REGNO (allocno));
2658*38fd1498Szrj }
2659*38fd1498Szrj else if (ALLOCNO_ASSIGNED_P (allocno))
2660*38fd1498Szrj {
2661*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
2662*38fd1498Szrj fprintf (ira_dump_file, "spill%s\n",
2663*38fd1498Szrj ALLOCNO_COLOR_DATA (allocno)->may_be_spilled_p
2664*38fd1498Szrj ? "" : "!");
2665*38fd1498Szrj }
2666*38fd1498Szrj ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2667*38fd1498Szrj }
2668*38fd1498Szrj }
2669*38fd1498Szrj
2670*38fd1498Szrj /* Set up number of available hard registers for allocno A. */
2671*38fd1498Szrj static void
setup_allocno_available_regs_num(ira_allocno_t a)2672*38fd1498Szrj setup_allocno_available_regs_num (ira_allocno_t a)
2673*38fd1498Szrj {
2674*38fd1498Szrj int i, n, hard_regno, hard_regs_num, nwords;
2675*38fd1498Szrj enum reg_class aclass;
2676*38fd1498Szrj allocno_color_data_t data;
2677*38fd1498Szrj
2678*38fd1498Szrj aclass = ALLOCNO_CLASS (a);
2679*38fd1498Szrj data = ALLOCNO_COLOR_DATA (a);
2680*38fd1498Szrj data->available_regs_num = 0;
2681*38fd1498Szrj if (aclass == NO_REGS)
2682*38fd1498Szrj return;
2683*38fd1498Szrj hard_regs_num = ira_class_hard_regs_num[aclass];
2684*38fd1498Szrj nwords = ALLOCNO_NUM_OBJECTS (a);
2685*38fd1498Szrj for (n = 0, i = hard_regs_num - 1; i >= 0; i--)
2686*38fd1498Szrj {
2687*38fd1498Szrj hard_regno = ira_class_hard_regs[aclass][i];
2688*38fd1498Szrj /* Checking only profitable hard regs. */
2689*38fd1498Szrj if (TEST_HARD_REG_BIT (data->profitable_hard_regs, hard_regno))
2690*38fd1498Szrj n++;
2691*38fd1498Szrj }
2692*38fd1498Szrj data->available_regs_num = n;
2693*38fd1498Szrj if (internal_flag_ira_verbose <= 2 || ira_dump_file == NULL)
2694*38fd1498Szrj return;
2695*38fd1498Szrj fprintf
2696*38fd1498Szrj (ira_dump_file,
2697*38fd1498Szrj " Allocno a%dr%d of %s(%d) has %d avail. regs ",
2698*38fd1498Szrj ALLOCNO_NUM (a), ALLOCNO_REGNO (a),
2699*38fd1498Szrj reg_class_names[aclass], ira_class_hard_regs_num[aclass], n);
2700*38fd1498Szrj print_hard_reg_set (ira_dump_file, data->profitable_hard_regs, false);
2701*38fd1498Szrj fprintf (ira_dump_file, ", %snode: ",
2702*38fd1498Szrj hard_reg_set_equal_p (data->profitable_hard_regs,
2703*38fd1498Szrj data->hard_regs_node->hard_regs->set)
2704*38fd1498Szrj ? "" : "^");
2705*38fd1498Szrj print_hard_reg_set (ira_dump_file,
2706*38fd1498Szrj data->hard_regs_node->hard_regs->set, false);
2707*38fd1498Szrj for (i = 0; i < nwords; i++)
2708*38fd1498Szrj {
2709*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, i);
2710*38fd1498Szrj
2711*38fd1498Szrj if (nwords != 1)
2712*38fd1498Szrj {
2713*38fd1498Szrj if (i != 0)
2714*38fd1498Szrj fprintf (ira_dump_file, ", ");
2715*38fd1498Szrj fprintf (ira_dump_file, " obj %d", i);
2716*38fd1498Szrj }
2717*38fd1498Szrj fprintf (ira_dump_file, " (confl regs = ");
2718*38fd1498Szrj print_hard_reg_set (ira_dump_file, OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2719*38fd1498Szrj false);
2720*38fd1498Szrj fprintf (ira_dump_file, ")");
2721*38fd1498Szrj }
2722*38fd1498Szrj fprintf (ira_dump_file, "\n");
2723*38fd1498Szrj }
2724*38fd1498Szrj
2725*38fd1498Szrj /* Put ALLOCNO in a bucket corresponding to its number and size of its
2726*38fd1498Szrj conflicting allocnos and hard registers. */
2727*38fd1498Szrj static void
put_allocno_into_bucket(ira_allocno_t allocno)2728*38fd1498Szrj put_allocno_into_bucket (ira_allocno_t allocno)
2729*38fd1498Szrj {
2730*38fd1498Szrj ALLOCNO_COLOR_DATA (allocno)->in_graph_p = true;
2731*38fd1498Szrj setup_allocno_available_regs_num (allocno);
2732*38fd1498Szrj if (setup_left_conflict_sizes_p (allocno))
2733*38fd1498Szrj add_allocno_to_bucket (allocno, &colorable_allocno_bucket);
2734*38fd1498Szrj else
2735*38fd1498Szrj add_allocno_to_bucket (allocno, &uncolorable_allocno_bucket);
2736*38fd1498Szrj }
2737*38fd1498Szrj
2738*38fd1498Szrj /* Map: allocno number -> allocno priority. */
2739*38fd1498Szrj static int *allocno_priorities;
2740*38fd1498Szrj
2741*38fd1498Szrj /* Set up priorities for N allocnos in array
2742*38fd1498Szrj CONSIDERATION_ALLOCNOS. */
2743*38fd1498Szrj static void
setup_allocno_priorities(ira_allocno_t * consideration_allocnos,int n)2744*38fd1498Szrj setup_allocno_priorities (ira_allocno_t *consideration_allocnos, int n)
2745*38fd1498Szrj {
2746*38fd1498Szrj int i, length, nrefs, priority, max_priority, mult;
2747*38fd1498Szrj ira_allocno_t a;
2748*38fd1498Szrj
2749*38fd1498Szrj max_priority = 0;
2750*38fd1498Szrj for (i = 0; i < n; i++)
2751*38fd1498Szrj {
2752*38fd1498Szrj a = consideration_allocnos[i];
2753*38fd1498Szrj nrefs = ALLOCNO_NREFS (a);
2754*38fd1498Szrj ira_assert (nrefs >= 0);
2755*38fd1498Szrj mult = floor_log2 (ALLOCNO_NREFS (a)) + 1;
2756*38fd1498Szrj ira_assert (mult >= 0);
2757*38fd1498Szrj allocno_priorities[ALLOCNO_NUM (a)]
2758*38fd1498Szrj = priority
2759*38fd1498Szrj = (mult
2760*38fd1498Szrj * (ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a))
2761*38fd1498Szrj * ira_reg_class_max_nregs[ALLOCNO_CLASS (a)][ALLOCNO_MODE (a)]);
2762*38fd1498Szrj if (priority < 0)
2763*38fd1498Szrj priority = -priority;
2764*38fd1498Szrj if (max_priority < priority)
2765*38fd1498Szrj max_priority = priority;
2766*38fd1498Szrj }
2767*38fd1498Szrj mult = max_priority == 0 ? 1 : INT_MAX / max_priority;
2768*38fd1498Szrj for (i = 0; i < n; i++)
2769*38fd1498Szrj {
2770*38fd1498Szrj a = consideration_allocnos[i];
2771*38fd1498Szrj length = ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2772*38fd1498Szrj if (ALLOCNO_NUM_OBJECTS (a) > 1)
2773*38fd1498Szrj length /= ALLOCNO_NUM_OBJECTS (a);
2774*38fd1498Szrj if (length <= 0)
2775*38fd1498Szrj length = 1;
2776*38fd1498Szrj allocno_priorities[ALLOCNO_NUM (a)]
2777*38fd1498Szrj = allocno_priorities[ALLOCNO_NUM (a)] * mult / length;
2778*38fd1498Szrj }
2779*38fd1498Szrj }
2780*38fd1498Szrj
2781*38fd1498Szrj /* Sort allocnos according to the profit of usage of a hard register
2782*38fd1498Szrj instead of memory for them. */
2783*38fd1498Szrj static int
allocno_cost_compare_func(const void * v1p,const void * v2p)2784*38fd1498Szrj allocno_cost_compare_func (const void *v1p, const void *v2p)
2785*38fd1498Szrj {
2786*38fd1498Szrj ira_allocno_t p1 = *(const ira_allocno_t *) v1p;
2787*38fd1498Szrj ira_allocno_t p2 = *(const ira_allocno_t *) v2p;
2788*38fd1498Szrj int c1, c2;
2789*38fd1498Szrj
2790*38fd1498Szrj c1 = ALLOCNO_UPDATED_MEMORY_COST (p1) - ALLOCNO_UPDATED_CLASS_COST (p1);
2791*38fd1498Szrj c2 = ALLOCNO_UPDATED_MEMORY_COST (p2) - ALLOCNO_UPDATED_CLASS_COST (p2);
2792*38fd1498Szrj if (c1 - c2)
2793*38fd1498Szrj return c1 - c2;
2794*38fd1498Szrj
2795*38fd1498Szrj /* If regs are equally good, sort by allocno numbers, so that the
2796*38fd1498Szrj results of qsort leave nothing to chance. */
2797*38fd1498Szrj return ALLOCNO_NUM (p1) - ALLOCNO_NUM (p2);
2798*38fd1498Szrj }
2799*38fd1498Szrj
2800*38fd1498Szrj /* Return savings on removed copies when ALLOCNO is assigned to
2801*38fd1498Szrj HARD_REGNO. */
2802*38fd1498Szrj static int
allocno_copy_cost_saving(ira_allocno_t allocno,int hard_regno)2803*38fd1498Szrj allocno_copy_cost_saving (ira_allocno_t allocno, int hard_regno)
2804*38fd1498Szrj {
2805*38fd1498Szrj int cost = 0;
2806*38fd1498Szrj machine_mode allocno_mode = ALLOCNO_MODE (allocno);
2807*38fd1498Szrj enum reg_class rclass;
2808*38fd1498Szrj ira_copy_t cp, next_cp;
2809*38fd1498Szrj
2810*38fd1498Szrj rclass = REGNO_REG_CLASS (hard_regno);
2811*38fd1498Szrj if (ira_reg_class_max_nregs[rclass][allocno_mode]
2812*38fd1498Szrj > ira_class_hard_regs_num[rclass])
2813*38fd1498Szrj /* For the above condition the cost can be wrong. Use the allocno
2814*38fd1498Szrj class in this case. */
2815*38fd1498Szrj rclass = ALLOCNO_CLASS (allocno);
2816*38fd1498Szrj for (cp = ALLOCNO_COPIES (allocno); cp != NULL; cp = next_cp)
2817*38fd1498Szrj {
2818*38fd1498Szrj if (cp->first == allocno)
2819*38fd1498Szrj {
2820*38fd1498Szrj next_cp = cp->next_first_allocno_copy;
2821*38fd1498Szrj if (ALLOCNO_HARD_REGNO (cp->second) != hard_regno)
2822*38fd1498Szrj continue;
2823*38fd1498Szrj }
2824*38fd1498Szrj else if (cp->second == allocno)
2825*38fd1498Szrj {
2826*38fd1498Szrj next_cp = cp->next_second_allocno_copy;
2827*38fd1498Szrj if (ALLOCNO_HARD_REGNO (cp->first) != hard_regno)
2828*38fd1498Szrj continue;
2829*38fd1498Szrj }
2830*38fd1498Szrj else
2831*38fd1498Szrj gcc_unreachable ();
2832*38fd1498Szrj cost += cp->freq * ira_register_move_cost[allocno_mode][rclass][rclass];
2833*38fd1498Szrj }
2834*38fd1498Szrj return cost;
2835*38fd1498Szrj }
2836*38fd1498Szrj
2837*38fd1498Szrj /* We used Chaitin-Briggs coloring to assign as many pseudos as
2838*38fd1498Szrj possible to hard registers. Let us try to improve allocation with
2839*38fd1498Szrj cost point of view. This function improves the allocation by
2840*38fd1498Szrj spilling some allocnos and assigning the freed hard registers to
2841*38fd1498Szrj other allocnos if it decreases the overall allocation cost. */
2842*38fd1498Szrj static void
improve_allocation(void)2843*38fd1498Szrj improve_allocation (void)
2844*38fd1498Szrj {
2845*38fd1498Szrj unsigned int i;
2846*38fd1498Szrj int j, k, n, hregno, conflict_hregno, base_cost, class_size, word, nwords;
2847*38fd1498Szrj int check, spill_cost, min_cost, nregs, conflict_nregs, r, best;
2848*38fd1498Szrj bool try_p;
2849*38fd1498Szrj enum reg_class aclass;
2850*38fd1498Szrj machine_mode mode;
2851*38fd1498Szrj int *allocno_costs;
2852*38fd1498Szrj int costs[FIRST_PSEUDO_REGISTER];
2853*38fd1498Szrj HARD_REG_SET conflicting_regs[2], profitable_hard_regs;
2854*38fd1498Szrj ira_allocno_t a;
2855*38fd1498Szrj bitmap_iterator bi;
2856*38fd1498Szrj
2857*38fd1498Szrj /* Don't bother to optimize the code with static chain pointer and
2858*38fd1498Szrj non-local goto in order not to spill the chain pointer
2859*38fd1498Szrj pseudo. */
2860*38fd1498Szrj if (cfun->static_chain_decl && crtl->has_nonlocal_goto)
2861*38fd1498Szrj return;
2862*38fd1498Szrj /* Clear counts used to process conflicting allocnos only once for
2863*38fd1498Szrj each allocno. */
2864*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2865*38fd1498Szrj ALLOCNO_COLOR_DATA (ira_allocnos[i])->temp = 0;
2866*38fd1498Szrj check = n = 0;
2867*38fd1498Szrj /* Process each allocno and try to assign a hard register to it by
2868*38fd1498Szrj spilling some its conflicting allocnos. */
2869*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
2870*38fd1498Szrj {
2871*38fd1498Szrj a = ira_allocnos[i];
2872*38fd1498Szrj ALLOCNO_COLOR_DATA (a)->temp = 0;
2873*38fd1498Szrj if (empty_profitable_hard_regs (a))
2874*38fd1498Szrj continue;
2875*38fd1498Szrj check++;
2876*38fd1498Szrj aclass = ALLOCNO_CLASS (a);
2877*38fd1498Szrj allocno_costs = ALLOCNO_HARD_REG_COSTS (a);
2878*38fd1498Szrj if ((hregno = ALLOCNO_HARD_REGNO (a)) < 0)
2879*38fd1498Szrj base_cost = ALLOCNO_UPDATED_MEMORY_COST (a);
2880*38fd1498Szrj else if (allocno_costs == NULL)
2881*38fd1498Szrj /* It means that assigning a hard register is not profitable
2882*38fd1498Szrj (we don't waste memory for hard register costs in this
2883*38fd1498Szrj case). */
2884*38fd1498Szrj continue;
2885*38fd1498Szrj else
2886*38fd1498Szrj base_cost = (allocno_costs[ira_class_hard_reg_index[aclass][hregno]]
2887*38fd1498Szrj - allocno_copy_cost_saving (a, hregno));
2888*38fd1498Szrj try_p = false;
2889*38fd1498Szrj get_conflict_and_start_profitable_regs (a, false,
2890*38fd1498Szrj conflicting_regs,
2891*38fd1498Szrj &profitable_hard_regs);
2892*38fd1498Szrj class_size = ira_class_hard_regs_num[aclass];
2893*38fd1498Szrj /* Set up cost improvement for usage of each profitable hard
2894*38fd1498Szrj register for allocno A. */
2895*38fd1498Szrj for (j = 0; j < class_size; j++)
2896*38fd1498Szrj {
2897*38fd1498Szrj hregno = ira_class_hard_regs[aclass][j];
2898*38fd1498Szrj if (! check_hard_reg_p (a, hregno,
2899*38fd1498Szrj conflicting_regs, profitable_hard_regs))
2900*38fd1498Szrj continue;
2901*38fd1498Szrj ira_assert (ira_class_hard_reg_index[aclass][hregno] == j);
2902*38fd1498Szrj k = allocno_costs == NULL ? 0 : j;
2903*38fd1498Szrj costs[hregno] = (allocno_costs == NULL
2904*38fd1498Szrj ? ALLOCNO_UPDATED_CLASS_COST (a) : allocno_costs[k]);
2905*38fd1498Szrj costs[hregno] -= allocno_copy_cost_saving (a, hregno);
2906*38fd1498Szrj costs[hregno] -= base_cost;
2907*38fd1498Szrj if (costs[hregno] < 0)
2908*38fd1498Szrj try_p = true;
2909*38fd1498Szrj }
2910*38fd1498Szrj if (! try_p)
2911*38fd1498Szrj /* There is no chance to improve the allocation cost by
2912*38fd1498Szrj assigning hard register to allocno A even without spilling
2913*38fd1498Szrj conflicting allocnos. */
2914*38fd1498Szrj continue;
2915*38fd1498Szrj mode = ALLOCNO_MODE (a);
2916*38fd1498Szrj nwords = ALLOCNO_NUM_OBJECTS (a);
2917*38fd1498Szrj /* Process each allocno conflicting with A and update the cost
2918*38fd1498Szrj improvement for profitable hard registers of A. To use a
2919*38fd1498Szrj hard register for A we need to spill some conflicting
2920*38fd1498Szrj allocnos and that creates penalty for the cost
2921*38fd1498Szrj improvement. */
2922*38fd1498Szrj for (word = 0; word < nwords; word++)
2923*38fd1498Szrj {
2924*38fd1498Szrj ira_object_t conflict_obj;
2925*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, word);
2926*38fd1498Szrj ira_object_conflict_iterator oci;
2927*38fd1498Szrj
2928*38fd1498Szrj FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2929*38fd1498Szrj {
2930*38fd1498Szrj ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2931*38fd1498Szrj
2932*38fd1498Szrj if (ALLOCNO_COLOR_DATA (conflict_a)->temp == check)
2933*38fd1498Szrj /* We already processed this conflicting allocno
2934*38fd1498Szrj because we processed earlier another object of the
2935*38fd1498Szrj conflicting allocno. */
2936*38fd1498Szrj continue;
2937*38fd1498Szrj ALLOCNO_COLOR_DATA (conflict_a)->temp = check;
2938*38fd1498Szrj if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
2939*38fd1498Szrj continue;
2940*38fd1498Szrj spill_cost = ALLOCNO_UPDATED_MEMORY_COST (conflict_a);
2941*38fd1498Szrj k = (ira_class_hard_reg_index
2942*38fd1498Szrj [ALLOCNO_CLASS (conflict_a)][conflict_hregno]);
2943*38fd1498Szrj ira_assert (k >= 0);
2944*38fd1498Szrj if ((allocno_costs = ALLOCNO_HARD_REG_COSTS (conflict_a))
2945*38fd1498Szrj != NULL)
2946*38fd1498Szrj spill_cost -= allocno_costs[k];
2947*38fd1498Szrj else
2948*38fd1498Szrj spill_cost -= ALLOCNO_UPDATED_CLASS_COST (conflict_a);
2949*38fd1498Szrj spill_cost
2950*38fd1498Szrj += allocno_copy_cost_saving (conflict_a, conflict_hregno);
2951*38fd1498Szrj conflict_nregs = hard_regno_nregs (conflict_hregno,
2952*38fd1498Szrj ALLOCNO_MODE (conflict_a));
2953*38fd1498Szrj for (r = conflict_hregno;
2954*38fd1498Szrj r >= 0 && (int) end_hard_regno (mode, r) > conflict_hregno;
2955*38fd1498Szrj r--)
2956*38fd1498Szrj if (check_hard_reg_p (a, r,
2957*38fd1498Szrj conflicting_regs, profitable_hard_regs))
2958*38fd1498Szrj costs[r] += spill_cost;
2959*38fd1498Szrj for (r = conflict_hregno + 1;
2960*38fd1498Szrj r < conflict_hregno + conflict_nregs;
2961*38fd1498Szrj r++)
2962*38fd1498Szrj if (check_hard_reg_p (a, r,
2963*38fd1498Szrj conflicting_regs, profitable_hard_regs))
2964*38fd1498Szrj costs[r] += spill_cost;
2965*38fd1498Szrj }
2966*38fd1498Szrj }
2967*38fd1498Szrj min_cost = INT_MAX;
2968*38fd1498Szrj best = -1;
2969*38fd1498Szrj /* Now we choose hard register for A which results in highest
2970*38fd1498Szrj allocation cost improvement. */
2971*38fd1498Szrj for (j = 0; j < class_size; j++)
2972*38fd1498Szrj {
2973*38fd1498Szrj hregno = ira_class_hard_regs[aclass][j];
2974*38fd1498Szrj if (check_hard_reg_p (a, hregno,
2975*38fd1498Szrj conflicting_regs, profitable_hard_regs)
2976*38fd1498Szrj && min_cost > costs[hregno])
2977*38fd1498Szrj {
2978*38fd1498Szrj best = hregno;
2979*38fd1498Szrj min_cost = costs[hregno];
2980*38fd1498Szrj }
2981*38fd1498Szrj }
2982*38fd1498Szrj if (min_cost >= 0)
2983*38fd1498Szrj /* We are in a situation when assigning any hard register to A
2984*38fd1498Szrj by spilling some conflicting allocnos does not improve the
2985*38fd1498Szrj allocation cost. */
2986*38fd1498Szrj continue;
2987*38fd1498Szrj nregs = hard_regno_nregs (best, mode);
2988*38fd1498Szrj /* Now spill conflicting allocnos which contain a hard register
2989*38fd1498Szrj of A when we assign the best chosen hard register to it. */
2990*38fd1498Szrj for (word = 0; word < nwords; word++)
2991*38fd1498Szrj {
2992*38fd1498Szrj ira_object_t conflict_obj;
2993*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, word);
2994*38fd1498Szrj ira_object_conflict_iterator oci;
2995*38fd1498Szrj
2996*38fd1498Szrj FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
2997*38fd1498Szrj {
2998*38fd1498Szrj ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
2999*38fd1498Szrj
3000*38fd1498Szrj if ((conflict_hregno = ALLOCNO_HARD_REGNO (conflict_a)) < 0)
3001*38fd1498Szrj continue;
3002*38fd1498Szrj conflict_nregs = hard_regno_nregs (conflict_hregno,
3003*38fd1498Szrj ALLOCNO_MODE (conflict_a));
3004*38fd1498Szrj if (best + nregs <= conflict_hregno
3005*38fd1498Szrj || conflict_hregno + conflict_nregs <= best)
3006*38fd1498Szrj /* No intersection. */
3007*38fd1498Szrj continue;
3008*38fd1498Szrj ALLOCNO_HARD_REGNO (conflict_a) = -1;
3009*38fd1498Szrj sorted_allocnos[n++] = conflict_a;
3010*38fd1498Szrj if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3011*38fd1498Szrj fprintf (ira_dump_file, "Spilling a%dr%d for a%dr%d\n",
3012*38fd1498Szrj ALLOCNO_NUM (conflict_a), ALLOCNO_REGNO (conflict_a),
3013*38fd1498Szrj ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
3014*38fd1498Szrj }
3015*38fd1498Szrj }
3016*38fd1498Szrj /* Assign the best chosen hard register to A. */
3017*38fd1498Szrj ALLOCNO_HARD_REGNO (a) = best;
3018*38fd1498Szrj if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3019*38fd1498Szrj fprintf (ira_dump_file, "Assigning %d to a%dr%d\n",
3020*38fd1498Szrj best, ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
3021*38fd1498Szrj }
3022*38fd1498Szrj if (n == 0)
3023*38fd1498Szrj return;
3024*38fd1498Szrj /* We spilled some allocnos to assign their hard registers to other
3025*38fd1498Szrj allocnos. The spilled allocnos are now in array
3026*38fd1498Szrj 'sorted_allocnos'. There is still a possibility that some of the
3027*38fd1498Szrj spilled allocnos can get hard registers. So let us try assign
3028*38fd1498Szrj them hard registers again (just a reminder -- function
3029*38fd1498Szrj 'assign_hard_reg' assigns hard registers only if it is possible
3030*38fd1498Szrj and profitable). We process the spilled allocnos with biggest
3031*38fd1498Szrj benefit to get hard register first -- see function
3032*38fd1498Szrj 'allocno_cost_compare_func'. */
3033*38fd1498Szrj qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
3034*38fd1498Szrj allocno_cost_compare_func);
3035*38fd1498Szrj for (j = 0; j < n; j++)
3036*38fd1498Szrj {
3037*38fd1498Szrj a = sorted_allocnos[j];
3038*38fd1498Szrj ALLOCNO_ASSIGNED_P (a) = false;
3039*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3040*38fd1498Szrj {
3041*38fd1498Szrj fprintf (ira_dump_file, " ");
3042*38fd1498Szrj ira_print_expanded_allocno (a);
3043*38fd1498Szrj fprintf (ira_dump_file, " -- ");
3044*38fd1498Szrj }
3045*38fd1498Szrj if (assign_hard_reg (a, false))
3046*38fd1498Szrj {
3047*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3048*38fd1498Szrj fprintf (ira_dump_file, "assign hard reg %d\n",
3049*38fd1498Szrj ALLOCNO_HARD_REGNO (a));
3050*38fd1498Szrj }
3051*38fd1498Szrj else
3052*38fd1498Szrj {
3053*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3054*38fd1498Szrj fprintf (ira_dump_file, "assign memory\n");
3055*38fd1498Szrj }
3056*38fd1498Szrj }
3057*38fd1498Szrj }
3058*38fd1498Szrj
3059*38fd1498Szrj /* Sort allocnos according to their priorities. */
3060*38fd1498Szrj static int
allocno_priority_compare_func(const void * v1p,const void * v2p)3061*38fd1498Szrj allocno_priority_compare_func (const void *v1p, const void *v2p)
3062*38fd1498Szrj {
3063*38fd1498Szrj ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
3064*38fd1498Szrj ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
3065*38fd1498Szrj int pri1, pri2, diff;
3066*38fd1498Szrj
3067*38fd1498Szrj /* Assign hard reg to static chain pointer pseudo first when
3068*38fd1498Szrj non-local goto is used. */
3069*38fd1498Szrj if ((diff = (non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a2))
3070*38fd1498Szrj - non_spilled_static_chain_regno_p (ALLOCNO_REGNO (a1)))) != 0)
3071*38fd1498Szrj return diff;
3072*38fd1498Szrj pri1 = allocno_priorities[ALLOCNO_NUM (a1)];
3073*38fd1498Szrj pri2 = allocno_priorities[ALLOCNO_NUM (a2)];
3074*38fd1498Szrj if (pri2 != pri1)
3075*38fd1498Szrj return SORTGT (pri2, pri1);
3076*38fd1498Szrj
3077*38fd1498Szrj /* If regs are equally good, sort by allocnos, so that the results of
3078*38fd1498Szrj qsort leave nothing to chance. */
3079*38fd1498Szrj return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
3080*38fd1498Szrj }
3081*38fd1498Szrj
3082*38fd1498Szrj /* Chaitin-Briggs coloring for allocnos in COLORING_ALLOCNO_BITMAP
3083*38fd1498Szrj taking into account allocnos in CONSIDERATION_ALLOCNO_BITMAP. */
3084*38fd1498Szrj static void
color_allocnos(void)3085*38fd1498Szrj color_allocnos (void)
3086*38fd1498Szrj {
3087*38fd1498Szrj unsigned int i, n;
3088*38fd1498Szrj bitmap_iterator bi;
3089*38fd1498Szrj ira_allocno_t a;
3090*38fd1498Szrj
3091*38fd1498Szrj setup_profitable_hard_regs ();
3092*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3093*38fd1498Szrj {
3094*38fd1498Szrj allocno_color_data_t data;
3095*38fd1498Szrj ira_pref_t pref, next_pref;
3096*38fd1498Szrj
3097*38fd1498Szrj a = ira_allocnos[i];
3098*38fd1498Szrj data = ALLOCNO_COLOR_DATA (a);
3099*38fd1498Szrj data->conflict_allocno_hard_prefs = 0;
3100*38fd1498Szrj for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = next_pref)
3101*38fd1498Szrj {
3102*38fd1498Szrj next_pref = pref->next_pref;
3103*38fd1498Szrj if (! ira_hard_reg_in_set_p (pref->hard_regno,
3104*38fd1498Szrj ALLOCNO_MODE (a),
3105*38fd1498Szrj data->profitable_hard_regs))
3106*38fd1498Szrj ira_remove_pref (pref);
3107*38fd1498Szrj }
3108*38fd1498Szrj }
3109*38fd1498Szrj
3110*38fd1498Szrj if (flag_ira_algorithm == IRA_ALGORITHM_PRIORITY)
3111*38fd1498Szrj {
3112*38fd1498Szrj n = 0;
3113*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3114*38fd1498Szrj {
3115*38fd1498Szrj a = ira_allocnos[i];
3116*38fd1498Szrj if (ALLOCNO_CLASS (a) == NO_REGS)
3117*38fd1498Szrj {
3118*38fd1498Szrj ALLOCNO_HARD_REGNO (a) = -1;
3119*38fd1498Szrj ALLOCNO_ASSIGNED_P (a) = true;
3120*38fd1498Szrj ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3121*38fd1498Szrj ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3122*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3123*38fd1498Szrj {
3124*38fd1498Szrj fprintf (ira_dump_file, " Spill");
3125*38fd1498Szrj ira_print_expanded_allocno (a);
3126*38fd1498Szrj fprintf (ira_dump_file, "\n");
3127*38fd1498Szrj }
3128*38fd1498Szrj continue;
3129*38fd1498Szrj }
3130*38fd1498Szrj sorted_allocnos[n++] = a;
3131*38fd1498Szrj }
3132*38fd1498Szrj if (n != 0)
3133*38fd1498Szrj {
3134*38fd1498Szrj setup_allocno_priorities (sorted_allocnos, n);
3135*38fd1498Szrj qsort (sorted_allocnos, n, sizeof (ira_allocno_t),
3136*38fd1498Szrj allocno_priority_compare_func);
3137*38fd1498Szrj for (i = 0; i < n; i++)
3138*38fd1498Szrj {
3139*38fd1498Szrj a = sorted_allocnos[i];
3140*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3141*38fd1498Szrj {
3142*38fd1498Szrj fprintf (ira_dump_file, " ");
3143*38fd1498Szrj ira_print_expanded_allocno (a);
3144*38fd1498Szrj fprintf (ira_dump_file, " -- ");
3145*38fd1498Szrj }
3146*38fd1498Szrj if (assign_hard_reg (a, false))
3147*38fd1498Szrj {
3148*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3149*38fd1498Szrj fprintf (ira_dump_file, "assign hard reg %d\n",
3150*38fd1498Szrj ALLOCNO_HARD_REGNO (a));
3151*38fd1498Szrj }
3152*38fd1498Szrj else
3153*38fd1498Szrj {
3154*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3155*38fd1498Szrj fprintf (ira_dump_file, "assign memory\n");
3156*38fd1498Szrj }
3157*38fd1498Szrj }
3158*38fd1498Szrj }
3159*38fd1498Szrj }
3160*38fd1498Szrj else
3161*38fd1498Szrj {
3162*38fd1498Szrj form_allocno_hard_regs_nodes_forest ();
3163*38fd1498Szrj if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3164*38fd1498Szrj print_hard_regs_forest (ira_dump_file);
3165*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3166*38fd1498Szrj {
3167*38fd1498Szrj a = ira_allocnos[i];
3168*38fd1498Szrj if (ALLOCNO_CLASS (a) != NO_REGS && ! empty_profitable_hard_regs (a))
3169*38fd1498Szrj {
3170*38fd1498Szrj ALLOCNO_COLOR_DATA (a)->in_graph_p = true;
3171*38fd1498Szrj update_costs_from_prefs (a);
3172*38fd1498Szrj update_conflict_allocno_hard_prefs (a);
3173*38fd1498Szrj }
3174*38fd1498Szrj else
3175*38fd1498Szrj {
3176*38fd1498Szrj ALLOCNO_HARD_REGNO (a) = -1;
3177*38fd1498Szrj ALLOCNO_ASSIGNED_P (a) = true;
3178*38fd1498Szrj /* We don't need updated costs anymore. */
3179*38fd1498Szrj ira_free_allocno_updated_costs (a);
3180*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3181*38fd1498Szrj {
3182*38fd1498Szrj fprintf (ira_dump_file, " Spill");
3183*38fd1498Szrj ira_print_expanded_allocno (a);
3184*38fd1498Szrj fprintf (ira_dump_file, "\n");
3185*38fd1498Szrj }
3186*38fd1498Szrj }
3187*38fd1498Szrj }
3188*38fd1498Szrj /* Put the allocnos into the corresponding buckets. */
3189*38fd1498Szrj colorable_allocno_bucket = NULL;
3190*38fd1498Szrj uncolorable_allocno_bucket = NULL;
3191*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, i, bi)
3192*38fd1498Szrj {
3193*38fd1498Szrj a = ira_allocnos[i];
3194*38fd1498Szrj if (ALLOCNO_COLOR_DATA (a)->in_graph_p)
3195*38fd1498Szrj put_allocno_into_bucket (a);
3196*38fd1498Szrj }
3197*38fd1498Szrj push_allocnos_to_stack ();
3198*38fd1498Szrj pop_allocnos_from_stack ();
3199*38fd1498Szrj finish_allocno_hard_regs_nodes_forest ();
3200*38fd1498Szrj }
3201*38fd1498Szrj improve_allocation ();
3202*38fd1498Szrj }
3203*38fd1498Szrj
3204*38fd1498Szrj
3205*38fd1498Szrj
3206*38fd1498Szrj /* Output information about the loop given by its LOOP_TREE_NODE. */
3207*38fd1498Szrj static void
print_loop_title(ira_loop_tree_node_t loop_tree_node)3208*38fd1498Szrj print_loop_title (ira_loop_tree_node_t loop_tree_node)
3209*38fd1498Szrj {
3210*38fd1498Szrj unsigned int j;
3211*38fd1498Szrj bitmap_iterator bi;
3212*38fd1498Szrj ira_loop_tree_node_t subloop_node, dest_loop_node;
3213*38fd1498Szrj edge e;
3214*38fd1498Szrj edge_iterator ei;
3215*38fd1498Szrj
3216*38fd1498Szrj if (loop_tree_node->parent == NULL)
3217*38fd1498Szrj fprintf (ira_dump_file,
3218*38fd1498Szrj "\n Loop 0 (parent -1, header bb%d, depth 0)\n bbs:",
3219*38fd1498Szrj NUM_FIXED_BLOCKS);
3220*38fd1498Szrj else
3221*38fd1498Szrj {
3222*38fd1498Szrj ira_assert (current_loops != NULL && loop_tree_node->loop != NULL);
3223*38fd1498Szrj fprintf (ira_dump_file,
3224*38fd1498Szrj "\n Loop %d (parent %d, header bb%d, depth %d)\n bbs:",
3225*38fd1498Szrj loop_tree_node->loop_num, loop_tree_node->parent->loop_num,
3226*38fd1498Szrj loop_tree_node->loop->header->index,
3227*38fd1498Szrj loop_depth (loop_tree_node->loop));
3228*38fd1498Szrj }
3229*38fd1498Szrj for (subloop_node = loop_tree_node->children;
3230*38fd1498Szrj subloop_node != NULL;
3231*38fd1498Szrj subloop_node = subloop_node->next)
3232*38fd1498Szrj if (subloop_node->bb != NULL)
3233*38fd1498Szrj {
3234*38fd1498Szrj fprintf (ira_dump_file, " %d", subloop_node->bb->index);
3235*38fd1498Szrj FOR_EACH_EDGE (e, ei, subloop_node->bb->succs)
3236*38fd1498Szrj if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
3237*38fd1498Szrj && ((dest_loop_node = IRA_BB_NODE (e->dest)->parent)
3238*38fd1498Szrj != loop_tree_node))
3239*38fd1498Szrj fprintf (ira_dump_file, "(->%d:l%d)",
3240*38fd1498Szrj e->dest->index, dest_loop_node->loop_num);
3241*38fd1498Szrj }
3242*38fd1498Szrj fprintf (ira_dump_file, "\n all:");
3243*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
3244*38fd1498Szrj fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
3245*38fd1498Szrj fprintf (ira_dump_file, "\n modified regnos:");
3246*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->modified_regnos, 0, j, bi)
3247*38fd1498Szrj fprintf (ira_dump_file, " %d", j);
3248*38fd1498Szrj fprintf (ira_dump_file, "\n border:");
3249*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->border_allocnos, 0, j, bi)
3250*38fd1498Szrj fprintf (ira_dump_file, " %dr%d", j, ALLOCNO_REGNO (ira_allocnos[j]));
3251*38fd1498Szrj fprintf (ira_dump_file, "\n Pressure:");
3252*38fd1498Szrj for (j = 0; (int) j < ira_pressure_classes_num; j++)
3253*38fd1498Szrj {
3254*38fd1498Szrj enum reg_class pclass;
3255*38fd1498Szrj
3256*38fd1498Szrj pclass = ira_pressure_classes[j];
3257*38fd1498Szrj if (loop_tree_node->reg_pressure[pclass] == 0)
3258*38fd1498Szrj continue;
3259*38fd1498Szrj fprintf (ira_dump_file, " %s=%d", reg_class_names[pclass],
3260*38fd1498Szrj loop_tree_node->reg_pressure[pclass]);
3261*38fd1498Szrj }
3262*38fd1498Szrj fprintf (ira_dump_file, "\n");
3263*38fd1498Szrj }
3264*38fd1498Szrj
3265*38fd1498Szrj /* Color the allocnos inside loop (in the extreme case it can be all
3266*38fd1498Szrj of the function) given the corresponding LOOP_TREE_NODE. The
3267*38fd1498Szrj function is called for each loop during top-down traverse of the
3268*38fd1498Szrj loop tree. */
3269*38fd1498Szrj static void
color_pass(ira_loop_tree_node_t loop_tree_node)3270*38fd1498Szrj color_pass (ira_loop_tree_node_t loop_tree_node)
3271*38fd1498Szrj {
3272*38fd1498Szrj int regno, hard_regno, index = -1, n;
3273*38fd1498Szrj int cost, exit_freq, enter_freq;
3274*38fd1498Szrj unsigned int j;
3275*38fd1498Szrj bitmap_iterator bi;
3276*38fd1498Szrj machine_mode mode;
3277*38fd1498Szrj enum reg_class rclass, aclass, pclass;
3278*38fd1498Szrj ira_allocno_t a, subloop_allocno;
3279*38fd1498Szrj ira_loop_tree_node_t subloop_node;
3280*38fd1498Szrj
3281*38fd1498Szrj ira_assert (loop_tree_node->bb == NULL);
3282*38fd1498Szrj if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3283*38fd1498Szrj print_loop_title (loop_tree_node);
3284*38fd1498Szrj
3285*38fd1498Szrj bitmap_copy (coloring_allocno_bitmap, loop_tree_node->all_allocnos);
3286*38fd1498Szrj bitmap_copy (consideration_allocno_bitmap, coloring_allocno_bitmap);
3287*38fd1498Szrj n = 0;
3288*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3289*38fd1498Szrj {
3290*38fd1498Szrj a = ira_allocnos[j];
3291*38fd1498Szrj n++;
3292*38fd1498Szrj if (! ALLOCNO_ASSIGNED_P (a))
3293*38fd1498Szrj continue;
3294*38fd1498Szrj bitmap_clear_bit (coloring_allocno_bitmap, ALLOCNO_NUM (a));
3295*38fd1498Szrj }
3296*38fd1498Szrj allocno_color_data
3297*38fd1498Szrj = (allocno_color_data_t) ira_allocate (sizeof (struct allocno_color_data)
3298*38fd1498Szrj * n);
3299*38fd1498Szrj memset (allocno_color_data, 0, sizeof (struct allocno_color_data) * n);
3300*38fd1498Szrj curr_allocno_process = 0;
3301*38fd1498Szrj n = 0;
3302*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3303*38fd1498Szrj {
3304*38fd1498Szrj a = ira_allocnos[j];
3305*38fd1498Szrj ALLOCNO_ADD_DATA (a) = allocno_color_data + n;
3306*38fd1498Szrj n++;
3307*38fd1498Szrj }
3308*38fd1498Szrj init_allocno_threads ();
3309*38fd1498Szrj /* Color all mentioned allocnos including transparent ones. */
3310*38fd1498Szrj color_allocnos ();
3311*38fd1498Szrj /* Process caps. They are processed just once. */
3312*38fd1498Szrj if (flag_ira_region == IRA_REGION_MIXED
3313*38fd1498Szrj || flag_ira_region == IRA_REGION_ALL)
3314*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (loop_tree_node->all_allocnos, 0, j, bi)
3315*38fd1498Szrj {
3316*38fd1498Szrj a = ira_allocnos[j];
3317*38fd1498Szrj if (ALLOCNO_CAP_MEMBER (a) == NULL)
3318*38fd1498Szrj continue;
3319*38fd1498Szrj /* Remove from processing in the next loop. */
3320*38fd1498Szrj bitmap_clear_bit (consideration_allocno_bitmap, j);
3321*38fd1498Szrj rclass = ALLOCNO_CLASS (a);
3322*38fd1498Szrj pclass = ira_pressure_class_translate[rclass];
3323*38fd1498Szrj if (flag_ira_region == IRA_REGION_MIXED
3324*38fd1498Szrj && (loop_tree_node->reg_pressure[pclass]
3325*38fd1498Szrj <= ira_class_hard_regs_num[pclass]))
3326*38fd1498Szrj {
3327*38fd1498Szrj mode = ALLOCNO_MODE (a);
3328*38fd1498Szrj hard_regno = ALLOCNO_HARD_REGNO (a);
3329*38fd1498Szrj if (hard_regno >= 0)
3330*38fd1498Szrj {
3331*38fd1498Szrj index = ira_class_hard_reg_index[rclass][hard_regno];
3332*38fd1498Szrj ira_assert (index >= 0);
3333*38fd1498Szrj }
3334*38fd1498Szrj regno = ALLOCNO_REGNO (a);
3335*38fd1498Szrj subloop_allocno = ALLOCNO_CAP_MEMBER (a);
3336*38fd1498Szrj subloop_node = ALLOCNO_LOOP_TREE_NODE (subloop_allocno);
3337*38fd1498Szrj ira_assert (!ALLOCNO_ASSIGNED_P (subloop_allocno));
3338*38fd1498Szrj ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3339*38fd1498Szrj ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3340*38fd1498Szrj if (hard_regno >= 0)
3341*38fd1498Szrj update_costs_from_copies (subloop_allocno, true, true);
3342*38fd1498Szrj /* We don't need updated costs anymore. */
3343*38fd1498Szrj ira_free_allocno_updated_costs (subloop_allocno);
3344*38fd1498Szrj }
3345*38fd1498Szrj }
3346*38fd1498Szrj /* Update costs of the corresponding allocnos (not caps) in the
3347*38fd1498Szrj subloops. */
3348*38fd1498Szrj for (subloop_node = loop_tree_node->subloops;
3349*38fd1498Szrj subloop_node != NULL;
3350*38fd1498Szrj subloop_node = subloop_node->subloop_next)
3351*38fd1498Szrj {
3352*38fd1498Szrj ira_assert (subloop_node->bb == NULL);
3353*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3354*38fd1498Szrj {
3355*38fd1498Szrj a = ira_allocnos[j];
3356*38fd1498Szrj ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3357*38fd1498Szrj mode = ALLOCNO_MODE (a);
3358*38fd1498Szrj rclass = ALLOCNO_CLASS (a);
3359*38fd1498Szrj pclass = ira_pressure_class_translate[rclass];
3360*38fd1498Szrj hard_regno = ALLOCNO_HARD_REGNO (a);
3361*38fd1498Szrj /* Use hard register class here. ??? */
3362*38fd1498Szrj if (hard_regno >= 0)
3363*38fd1498Szrj {
3364*38fd1498Szrj index = ira_class_hard_reg_index[rclass][hard_regno];
3365*38fd1498Szrj ira_assert (index >= 0);
3366*38fd1498Szrj }
3367*38fd1498Szrj regno = ALLOCNO_REGNO (a);
3368*38fd1498Szrj /* ??? conflict costs */
3369*38fd1498Szrj subloop_allocno = subloop_node->regno_allocno_map[regno];
3370*38fd1498Szrj if (subloop_allocno == NULL
3371*38fd1498Szrj || ALLOCNO_CAP (subloop_allocno) != NULL)
3372*38fd1498Szrj continue;
3373*38fd1498Szrj ira_assert (ALLOCNO_CLASS (subloop_allocno) == rclass);
3374*38fd1498Szrj ira_assert (bitmap_bit_p (subloop_node->all_allocnos,
3375*38fd1498Szrj ALLOCNO_NUM (subloop_allocno)));
3376*38fd1498Szrj if ((flag_ira_region == IRA_REGION_MIXED
3377*38fd1498Szrj && (loop_tree_node->reg_pressure[pclass]
3378*38fd1498Szrj <= ira_class_hard_regs_num[pclass]))
3379*38fd1498Szrj || (pic_offset_table_rtx != NULL
3380*38fd1498Szrj && regno == (int) REGNO (pic_offset_table_rtx))
3381*38fd1498Szrj /* Avoid overlapped multi-registers. Moves between them
3382*38fd1498Szrj might result in wrong code generation. */
3383*38fd1498Szrj || (hard_regno >= 0
3384*38fd1498Szrj && ira_reg_class_max_nregs[pclass][mode] > 1))
3385*38fd1498Szrj {
3386*38fd1498Szrj if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
3387*38fd1498Szrj {
3388*38fd1498Szrj ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3389*38fd1498Szrj ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3390*38fd1498Szrj if (hard_regno >= 0)
3391*38fd1498Szrj update_costs_from_copies (subloop_allocno, true, true);
3392*38fd1498Szrj /* We don't need updated costs anymore. */
3393*38fd1498Szrj ira_free_allocno_updated_costs (subloop_allocno);
3394*38fd1498Szrj }
3395*38fd1498Szrj continue;
3396*38fd1498Szrj }
3397*38fd1498Szrj exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
3398*38fd1498Szrj enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
3399*38fd1498Szrj ira_assert (regno < ira_reg_equiv_len);
3400*38fd1498Szrj if (ira_equiv_no_lvalue_p (regno))
3401*38fd1498Szrj {
3402*38fd1498Szrj if (! ALLOCNO_ASSIGNED_P (subloop_allocno))
3403*38fd1498Szrj {
3404*38fd1498Szrj ALLOCNO_HARD_REGNO (subloop_allocno) = hard_regno;
3405*38fd1498Szrj ALLOCNO_ASSIGNED_P (subloop_allocno) = true;
3406*38fd1498Szrj if (hard_regno >= 0)
3407*38fd1498Szrj update_costs_from_copies (subloop_allocno, true, true);
3408*38fd1498Szrj /* We don't need updated costs anymore. */
3409*38fd1498Szrj ira_free_allocno_updated_costs (subloop_allocno);
3410*38fd1498Szrj }
3411*38fd1498Szrj }
3412*38fd1498Szrj else if (hard_regno < 0)
3413*38fd1498Szrj {
3414*38fd1498Szrj ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
3415*38fd1498Szrj -= ((ira_memory_move_cost[mode][rclass][1] * enter_freq)
3416*38fd1498Szrj + (ira_memory_move_cost[mode][rclass][0] * exit_freq));
3417*38fd1498Szrj }
3418*38fd1498Szrj else
3419*38fd1498Szrj {
3420*38fd1498Szrj aclass = ALLOCNO_CLASS (subloop_allocno);
3421*38fd1498Szrj ira_init_register_move_cost_if_necessary (mode);
3422*38fd1498Szrj cost = (ira_register_move_cost[mode][rclass][rclass]
3423*38fd1498Szrj * (exit_freq + enter_freq));
3424*38fd1498Szrj ira_allocate_and_set_or_copy_costs
3425*38fd1498Szrj (&ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno), aclass,
3426*38fd1498Szrj ALLOCNO_UPDATED_CLASS_COST (subloop_allocno),
3427*38fd1498Szrj ALLOCNO_HARD_REG_COSTS (subloop_allocno));
3428*38fd1498Szrj ira_allocate_and_set_or_copy_costs
3429*38fd1498Szrj (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno),
3430*38fd1498Szrj aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (subloop_allocno));
3431*38fd1498Szrj ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index] -= cost;
3432*38fd1498Szrj ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (subloop_allocno)[index]
3433*38fd1498Szrj -= cost;
3434*38fd1498Szrj if (ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
3435*38fd1498Szrj > ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index])
3436*38fd1498Szrj ALLOCNO_UPDATED_CLASS_COST (subloop_allocno)
3437*38fd1498Szrj = ALLOCNO_UPDATED_HARD_REG_COSTS (subloop_allocno)[index];
3438*38fd1498Szrj ALLOCNO_UPDATED_MEMORY_COST (subloop_allocno)
3439*38fd1498Szrj += (ira_memory_move_cost[mode][rclass][0] * enter_freq
3440*38fd1498Szrj + ira_memory_move_cost[mode][rclass][1] * exit_freq);
3441*38fd1498Szrj }
3442*38fd1498Szrj }
3443*38fd1498Szrj }
3444*38fd1498Szrj ira_free (allocno_color_data);
3445*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (consideration_allocno_bitmap, 0, j, bi)
3446*38fd1498Szrj {
3447*38fd1498Szrj a = ira_allocnos[j];
3448*38fd1498Szrj ALLOCNO_ADD_DATA (a) = NULL;
3449*38fd1498Szrj }
3450*38fd1498Szrj }
3451*38fd1498Szrj
3452*38fd1498Szrj /* Initialize the common data for coloring and calls functions to do
3453*38fd1498Szrj Chaitin-Briggs and regional coloring. */
3454*38fd1498Szrj static void
do_coloring(void)3455*38fd1498Szrj do_coloring (void)
3456*38fd1498Szrj {
3457*38fd1498Szrj coloring_allocno_bitmap = ira_allocate_bitmap ();
3458*38fd1498Szrj if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3459*38fd1498Szrj fprintf (ira_dump_file, "\n**** Allocnos coloring:\n\n");
3460*38fd1498Szrj
3461*38fd1498Szrj ira_traverse_loop_tree (false, ira_loop_tree_root, color_pass, NULL);
3462*38fd1498Szrj
3463*38fd1498Szrj if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
3464*38fd1498Szrj ira_print_disposition (ira_dump_file);
3465*38fd1498Szrj
3466*38fd1498Szrj ira_free_bitmap (coloring_allocno_bitmap);
3467*38fd1498Szrj }
3468*38fd1498Szrj
3469*38fd1498Szrj
3470*38fd1498Szrj
3471*38fd1498Szrj /* Move spill/restore code, which are to be generated in ira-emit.c,
3472*38fd1498Szrj to less frequent points (if it is profitable) by reassigning some
3473*38fd1498Szrj allocnos (in loop with subloops containing in another loop) to
3474*38fd1498Szrj memory which results in longer live-range where the corresponding
3475*38fd1498Szrj pseudo-registers will be in memory. */
3476*38fd1498Szrj static void
move_spill_restore(void)3477*38fd1498Szrj move_spill_restore (void)
3478*38fd1498Szrj {
3479*38fd1498Szrj int cost, regno, hard_regno, hard_regno2, index;
3480*38fd1498Szrj bool changed_p;
3481*38fd1498Szrj int enter_freq, exit_freq;
3482*38fd1498Szrj machine_mode mode;
3483*38fd1498Szrj enum reg_class rclass;
3484*38fd1498Szrj ira_allocno_t a, parent_allocno, subloop_allocno;
3485*38fd1498Szrj ira_loop_tree_node_t parent, loop_node, subloop_node;
3486*38fd1498Szrj ira_allocno_iterator ai;
3487*38fd1498Szrj
3488*38fd1498Szrj for (;;)
3489*38fd1498Szrj {
3490*38fd1498Szrj changed_p = false;
3491*38fd1498Szrj if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3492*38fd1498Szrj fprintf (ira_dump_file, "New iteration of spill/restore move\n");
3493*38fd1498Szrj FOR_EACH_ALLOCNO (a, ai)
3494*38fd1498Szrj {
3495*38fd1498Szrj regno = ALLOCNO_REGNO (a);
3496*38fd1498Szrj loop_node = ALLOCNO_LOOP_TREE_NODE (a);
3497*38fd1498Szrj if (ALLOCNO_CAP_MEMBER (a) != NULL
3498*38fd1498Szrj || ALLOCNO_CAP (a) != NULL
3499*38fd1498Szrj || (hard_regno = ALLOCNO_HARD_REGNO (a)) < 0
3500*38fd1498Szrj || loop_node->children == NULL
3501*38fd1498Szrj /* don't do the optimization because it can create
3502*38fd1498Szrj copies and the reload pass can spill the allocno set
3503*38fd1498Szrj by copy although the allocno will not get memory
3504*38fd1498Szrj slot. */
3505*38fd1498Szrj || ira_equiv_no_lvalue_p (regno)
3506*38fd1498Szrj || !bitmap_bit_p (loop_node->border_allocnos, ALLOCNO_NUM (a))
3507*38fd1498Szrj /* Do not spill static chain pointer pseudo when
3508*38fd1498Szrj non-local goto is used. */
3509*38fd1498Szrj || non_spilled_static_chain_regno_p (regno))
3510*38fd1498Szrj continue;
3511*38fd1498Szrj mode = ALLOCNO_MODE (a);
3512*38fd1498Szrj rclass = ALLOCNO_CLASS (a);
3513*38fd1498Szrj index = ira_class_hard_reg_index[rclass][hard_regno];
3514*38fd1498Szrj ira_assert (index >= 0);
3515*38fd1498Szrj cost = (ALLOCNO_MEMORY_COST (a)
3516*38fd1498Szrj - (ALLOCNO_HARD_REG_COSTS (a) == NULL
3517*38fd1498Szrj ? ALLOCNO_CLASS_COST (a)
3518*38fd1498Szrj : ALLOCNO_HARD_REG_COSTS (a)[index]));
3519*38fd1498Szrj ira_init_register_move_cost_if_necessary (mode);
3520*38fd1498Szrj for (subloop_node = loop_node->subloops;
3521*38fd1498Szrj subloop_node != NULL;
3522*38fd1498Szrj subloop_node = subloop_node->subloop_next)
3523*38fd1498Szrj {
3524*38fd1498Szrj ira_assert (subloop_node->bb == NULL);
3525*38fd1498Szrj subloop_allocno = subloop_node->regno_allocno_map[regno];
3526*38fd1498Szrj if (subloop_allocno == NULL)
3527*38fd1498Szrj continue;
3528*38fd1498Szrj ira_assert (rclass == ALLOCNO_CLASS (subloop_allocno));
3529*38fd1498Szrj /* We have accumulated cost. To get the real cost of
3530*38fd1498Szrj allocno usage in the loop we should subtract costs of
3531*38fd1498Szrj the subloop allocnos. */
3532*38fd1498Szrj cost -= (ALLOCNO_MEMORY_COST (subloop_allocno)
3533*38fd1498Szrj - (ALLOCNO_HARD_REG_COSTS (subloop_allocno) == NULL
3534*38fd1498Szrj ? ALLOCNO_CLASS_COST (subloop_allocno)
3535*38fd1498Szrj : ALLOCNO_HARD_REG_COSTS (subloop_allocno)[index]));
3536*38fd1498Szrj exit_freq = ira_loop_edge_freq (subloop_node, regno, true);
3537*38fd1498Szrj enter_freq = ira_loop_edge_freq (subloop_node, regno, false);
3538*38fd1498Szrj if ((hard_regno2 = ALLOCNO_HARD_REGNO (subloop_allocno)) < 0)
3539*38fd1498Szrj cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3540*38fd1498Szrj + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3541*38fd1498Szrj else
3542*38fd1498Szrj {
3543*38fd1498Szrj cost
3544*38fd1498Szrj += (ira_memory_move_cost[mode][rclass][0] * exit_freq
3545*38fd1498Szrj + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3546*38fd1498Szrj if (hard_regno2 != hard_regno)
3547*38fd1498Szrj cost -= (ira_register_move_cost[mode][rclass][rclass]
3548*38fd1498Szrj * (exit_freq + enter_freq));
3549*38fd1498Szrj }
3550*38fd1498Szrj }
3551*38fd1498Szrj if ((parent = loop_node->parent) != NULL
3552*38fd1498Szrj && (parent_allocno = parent->regno_allocno_map[regno]) != NULL)
3553*38fd1498Szrj {
3554*38fd1498Szrj ira_assert (rclass == ALLOCNO_CLASS (parent_allocno));
3555*38fd1498Szrj exit_freq = ira_loop_edge_freq (loop_node, regno, true);
3556*38fd1498Szrj enter_freq = ira_loop_edge_freq (loop_node, regno, false);
3557*38fd1498Szrj if ((hard_regno2 = ALLOCNO_HARD_REGNO (parent_allocno)) < 0)
3558*38fd1498Szrj cost -= (ira_memory_move_cost[mode][rclass][0] * exit_freq
3559*38fd1498Szrj + ira_memory_move_cost[mode][rclass][1] * enter_freq);
3560*38fd1498Szrj else
3561*38fd1498Szrj {
3562*38fd1498Szrj cost
3563*38fd1498Szrj += (ira_memory_move_cost[mode][rclass][1] * exit_freq
3564*38fd1498Szrj + ira_memory_move_cost[mode][rclass][0] * enter_freq);
3565*38fd1498Szrj if (hard_regno2 != hard_regno)
3566*38fd1498Szrj cost -= (ira_register_move_cost[mode][rclass][rclass]
3567*38fd1498Szrj * (exit_freq + enter_freq));
3568*38fd1498Szrj }
3569*38fd1498Szrj }
3570*38fd1498Szrj if (cost < 0)
3571*38fd1498Szrj {
3572*38fd1498Szrj ALLOCNO_HARD_REGNO (a) = -1;
3573*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3574*38fd1498Szrj {
3575*38fd1498Szrj fprintf
3576*38fd1498Szrj (ira_dump_file,
3577*38fd1498Szrj " Moving spill/restore for a%dr%d up from loop %d",
3578*38fd1498Szrj ALLOCNO_NUM (a), regno, loop_node->loop_num);
3579*38fd1498Szrj fprintf (ira_dump_file, " - profit %d\n", -cost);
3580*38fd1498Szrj }
3581*38fd1498Szrj changed_p = true;
3582*38fd1498Szrj }
3583*38fd1498Szrj }
3584*38fd1498Szrj if (! changed_p)
3585*38fd1498Szrj break;
3586*38fd1498Szrj }
3587*38fd1498Szrj }
3588*38fd1498Szrj
3589*38fd1498Szrj
3590*38fd1498Szrj
3591*38fd1498Szrj /* Update current hard reg costs and current conflict hard reg costs
3592*38fd1498Szrj for allocno A. It is done by processing its copies containing
3593*38fd1498Szrj other allocnos already assigned. */
3594*38fd1498Szrj static void
update_curr_costs(ira_allocno_t a)3595*38fd1498Szrj update_curr_costs (ira_allocno_t a)
3596*38fd1498Szrj {
3597*38fd1498Szrj int i, hard_regno, cost;
3598*38fd1498Szrj machine_mode mode;
3599*38fd1498Szrj enum reg_class aclass, rclass;
3600*38fd1498Szrj ira_allocno_t another_a;
3601*38fd1498Szrj ira_copy_t cp, next_cp;
3602*38fd1498Szrj
3603*38fd1498Szrj ira_free_allocno_updated_costs (a);
3604*38fd1498Szrj ira_assert (! ALLOCNO_ASSIGNED_P (a));
3605*38fd1498Szrj aclass = ALLOCNO_CLASS (a);
3606*38fd1498Szrj if (aclass == NO_REGS)
3607*38fd1498Szrj return;
3608*38fd1498Szrj mode = ALLOCNO_MODE (a);
3609*38fd1498Szrj ira_init_register_move_cost_if_necessary (mode);
3610*38fd1498Szrj for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3611*38fd1498Szrj {
3612*38fd1498Szrj if (cp->first == a)
3613*38fd1498Szrj {
3614*38fd1498Szrj next_cp = cp->next_first_allocno_copy;
3615*38fd1498Szrj another_a = cp->second;
3616*38fd1498Szrj }
3617*38fd1498Szrj else if (cp->second == a)
3618*38fd1498Szrj {
3619*38fd1498Szrj next_cp = cp->next_second_allocno_copy;
3620*38fd1498Szrj another_a = cp->first;
3621*38fd1498Szrj }
3622*38fd1498Szrj else
3623*38fd1498Szrj gcc_unreachable ();
3624*38fd1498Szrj if (! ira_reg_classes_intersect_p[aclass][ALLOCNO_CLASS (another_a)]
3625*38fd1498Szrj || ! ALLOCNO_ASSIGNED_P (another_a)
3626*38fd1498Szrj || (hard_regno = ALLOCNO_HARD_REGNO (another_a)) < 0)
3627*38fd1498Szrj continue;
3628*38fd1498Szrj rclass = REGNO_REG_CLASS (hard_regno);
3629*38fd1498Szrj i = ira_class_hard_reg_index[aclass][hard_regno];
3630*38fd1498Szrj if (i < 0)
3631*38fd1498Szrj continue;
3632*38fd1498Szrj cost = (cp->first == a
3633*38fd1498Szrj ? ira_register_move_cost[mode][rclass][aclass]
3634*38fd1498Szrj : ira_register_move_cost[mode][aclass][rclass]);
3635*38fd1498Szrj ira_allocate_and_set_or_copy_costs
3636*38fd1498Szrj (&ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass, ALLOCNO_CLASS_COST (a),
3637*38fd1498Szrj ALLOCNO_HARD_REG_COSTS (a));
3638*38fd1498Szrj ira_allocate_and_set_or_copy_costs
3639*38fd1498Szrj (&ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
3640*38fd1498Szrj aclass, 0, ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
3641*38fd1498Szrj ALLOCNO_UPDATED_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3642*38fd1498Szrj ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a)[i] -= cp->freq * cost;
3643*38fd1498Szrj }
3644*38fd1498Szrj }
3645*38fd1498Szrj
3646*38fd1498Szrj /* Try to assign hard registers to the unassigned allocnos and
3647*38fd1498Szrj allocnos conflicting with them or conflicting with allocnos whose
3648*38fd1498Szrj regno >= START_REGNO. The function is called after ira_flattening,
3649*38fd1498Szrj so more allocnos (including ones created in ira-emit.c) will have a
3650*38fd1498Szrj chance to get a hard register. We use simple assignment algorithm
3651*38fd1498Szrj based on priorities. */
3652*38fd1498Szrj void
ira_reassign_conflict_allocnos(int start_regno)3653*38fd1498Szrj ira_reassign_conflict_allocnos (int start_regno)
3654*38fd1498Szrj {
3655*38fd1498Szrj int i, allocnos_to_color_num;
3656*38fd1498Szrj ira_allocno_t a;
3657*38fd1498Szrj enum reg_class aclass;
3658*38fd1498Szrj bitmap allocnos_to_color;
3659*38fd1498Szrj ira_allocno_iterator ai;
3660*38fd1498Szrj
3661*38fd1498Szrj allocnos_to_color = ira_allocate_bitmap ();
3662*38fd1498Szrj allocnos_to_color_num = 0;
3663*38fd1498Szrj FOR_EACH_ALLOCNO (a, ai)
3664*38fd1498Szrj {
3665*38fd1498Szrj int n = ALLOCNO_NUM_OBJECTS (a);
3666*38fd1498Szrj
3667*38fd1498Szrj if (! ALLOCNO_ASSIGNED_P (a)
3668*38fd1498Szrj && ! bitmap_bit_p (allocnos_to_color, ALLOCNO_NUM (a)))
3669*38fd1498Szrj {
3670*38fd1498Szrj if (ALLOCNO_CLASS (a) != NO_REGS)
3671*38fd1498Szrj sorted_allocnos[allocnos_to_color_num++] = a;
3672*38fd1498Szrj else
3673*38fd1498Szrj {
3674*38fd1498Szrj ALLOCNO_ASSIGNED_P (a) = true;
3675*38fd1498Szrj ALLOCNO_HARD_REGNO (a) = -1;
3676*38fd1498Szrj ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3677*38fd1498Szrj ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3678*38fd1498Szrj }
3679*38fd1498Szrj bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (a));
3680*38fd1498Szrj }
3681*38fd1498Szrj if (ALLOCNO_REGNO (a) < start_regno
3682*38fd1498Szrj || (aclass = ALLOCNO_CLASS (a)) == NO_REGS)
3683*38fd1498Szrj continue;
3684*38fd1498Szrj for (i = 0; i < n; i++)
3685*38fd1498Szrj {
3686*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, i);
3687*38fd1498Szrj ira_object_t conflict_obj;
3688*38fd1498Szrj ira_object_conflict_iterator oci;
3689*38fd1498Szrj
3690*38fd1498Szrj FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
3691*38fd1498Szrj {
3692*38fd1498Szrj ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
3693*38fd1498Szrj
3694*38fd1498Szrj ira_assert (ira_reg_classes_intersect_p
3695*38fd1498Szrj [aclass][ALLOCNO_CLASS (conflict_a)]);
3696*38fd1498Szrj if (!bitmap_set_bit (allocnos_to_color, ALLOCNO_NUM (conflict_a)))
3697*38fd1498Szrj continue;
3698*38fd1498Szrj sorted_allocnos[allocnos_to_color_num++] = conflict_a;
3699*38fd1498Szrj }
3700*38fd1498Szrj }
3701*38fd1498Szrj }
3702*38fd1498Szrj ira_free_bitmap (allocnos_to_color);
3703*38fd1498Szrj if (allocnos_to_color_num > 1)
3704*38fd1498Szrj {
3705*38fd1498Szrj setup_allocno_priorities (sorted_allocnos, allocnos_to_color_num);
3706*38fd1498Szrj qsort (sorted_allocnos, allocnos_to_color_num, sizeof (ira_allocno_t),
3707*38fd1498Szrj allocno_priority_compare_func);
3708*38fd1498Szrj }
3709*38fd1498Szrj for (i = 0; i < allocnos_to_color_num; i++)
3710*38fd1498Szrj {
3711*38fd1498Szrj a = sorted_allocnos[i];
3712*38fd1498Szrj ALLOCNO_ASSIGNED_P (a) = false;
3713*38fd1498Szrj update_curr_costs (a);
3714*38fd1498Szrj }
3715*38fd1498Szrj for (i = 0; i < allocnos_to_color_num; i++)
3716*38fd1498Szrj {
3717*38fd1498Szrj a = sorted_allocnos[i];
3718*38fd1498Szrj if (assign_hard_reg (a, true))
3719*38fd1498Szrj {
3720*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3721*38fd1498Szrj fprintf
3722*38fd1498Szrj (ira_dump_file,
3723*38fd1498Szrj " Secondary allocation: assign hard reg %d to reg %d\n",
3724*38fd1498Szrj ALLOCNO_HARD_REGNO (a), ALLOCNO_REGNO (a));
3725*38fd1498Szrj }
3726*38fd1498Szrj }
3727*38fd1498Szrj }
3728*38fd1498Szrj
3729*38fd1498Szrj
3730*38fd1498Szrj
3731*38fd1498Szrj /* This page contains functions used to find conflicts using allocno
3732*38fd1498Szrj live ranges. */
3733*38fd1498Szrj
3734*38fd1498Szrj #ifdef ENABLE_IRA_CHECKING
3735*38fd1498Szrj
3736*38fd1498Szrj /* Return TRUE if live ranges of pseudo-registers REGNO1 and REGNO2
3737*38fd1498Szrj intersect. This should be used when there is only one region.
3738*38fd1498Szrj Currently this is used during reload. */
3739*38fd1498Szrj static bool
conflict_by_live_ranges_p(int regno1,int regno2)3740*38fd1498Szrj conflict_by_live_ranges_p (int regno1, int regno2)
3741*38fd1498Szrj {
3742*38fd1498Szrj ira_allocno_t a1, a2;
3743*38fd1498Szrj
3744*38fd1498Szrj ira_assert (regno1 >= FIRST_PSEUDO_REGISTER
3745*38fd1498Szrj && regno2 >= FIRST_PSEUDO_REGISTER);
3746*38fd1498Szrj /* Reg info calculated by dataflow infrastructure can be different
3747*38fd1498Szrj from one calculated by regclass. */
3748*38fd1498Szrj if ((a1 = ira_loop_tree_root->regno_allocno_map[regno1]) == NULL
3749*38fd1498Szrj || (a2 = ira_loop_tree_root->regno_allocno_map[regno2]) == NULL)
3750*38fd1498Szrj return false;
3751*38fd1498Szrj return allocnos_conflict_by_live_ranges_p (a1, a2);
3752*38fd1498Szrj }
3753*38fd1498Szrj
3754*38fd1498Szrj #endif
3755*38fd1498Szrj
3756*38fd1498Szrj
3757*38fd1498Szrj
3758*38fd1498Szrj /* This page contains code to coalesce memory stack slots used by
3759*38fd1498Szrj spilled allocnos. This results in smaller stack frame, better data
3760*38fd1498Szrj locality, and in smaller code for some architectures like
3761*38fd1498Szrj x86/x86_64 where insn size depends on address displacement value.
3762*38fd1498Szrj On the other hand, it can worsen insn scheduling after the RA but
3763*38fd1498Szrj in practice it is less important than smaller stack frames. */
3764*38fd1498Szrj
3765*38fd1498Szrj /* TRUE if we coalesced some allocnos. In other words, if we got
3766*38fd1498Szrj loops formed by members first_coalesced_allocno and
3767*38fd1498Szrj next_coalesced_allocno containing more one allocno. */
3768*38fd1498Szrj static bool allocno_coalesced_p;
3769*38fd1498Szrj
3770*38fd1498Szrj /* Bitmap used to prevent a repeated allocno processing because of
3771*38fd1498Szrj coalescing. */
3772*38fd1498Szrj static bitmap processed_coalesced_allocno_bitmap;
3773*38fd1498Szrj
3774*38fd1498Szrj /* See below. */
3775*38fd1498Szrj typedef struct coalesce_data *coalesce_data_t;
3776*38fd1498Szrj
3777*38fd1498Szrj /* To decrease footprint of ira_allocno structure we store all data
3778*38fd1498Szrj needed only for coalescing in the following structure. */
3779*38fd1498Szrj struct coalesce_data
3780*38fd1498Szrj {
3781*38fd1498Szrj /* Coalesced allocnos form a cyclic list. One allocno given by
3782*38fd1498Szrj FIRST represents all coalesced allocnos. The
3783*38fd1498Szrj list is chained by NEXT. */
3784*38fd1498Szrj ira_allocno_t first;
3785*38fd1498Szrj ira_allocno_t next;
3786*38fd1498Szrj int temp;
3787*38fd1498Szrj };
3788*38fd1498Szrj
3789*38fd1498Szrj /* Container for storing allocno data concerning coalescing. */
3790*38fd1498Szrj static coalesce_data_t allocno_coalesce_data;
3791*38fd1498Szrj
3792*38fd1498Szrj /* Macro to access the data concerning coalescing. */
3793*38fd1498Szrj #define ALLOCNO_COALESCE_DATA(a) ((coalesce_data_t) ALLOCNO_ADD_DATA (a))
3794*38fd1498Szrj
3795*38fd1498Szrj /* Merge two sets of coalesced allocnos given correspondingly by
3796*38fd1498Szrj allocnos A1 and A2 (more accurately merging A2 set into A1
3797*38fd1498Szrj set). */
3798*38fd1498Szrj static void
merge_allocnos(ira_allocno_t a1,ira_allocno_t a2)3799*38fd1498Szrj merge_allocnos (ira_allocno_t a1, ira_allocno_t a2)
3800*38fd1498Szrj {
3801*38fd1498Szrj ira_allocno_t a, first, last, next;
3802*38fd1498Szrj
3803*38fd1498Szrj first = ALLOCNO_COALESCE_DATA (a1)->first;
3804*38fd1498Szrj a = ALLOCNO_COALESCE_DATA (a2)->first;
3805*38fd1498Szrj if (first == a)
3806*38fd1498Szrj return;
3807*38fd1498Szrj for (last = a2, a = ALLOCNO_COALESCE_DATA (a2)->next;;
3808*38fd1498Szrj a = ALLOCNO_COALESCE_DATA (a)->next)
3809*38fd1498Szrj {
3810*38fd1498Szrj ALLOCNO_COALESCE_DATA (a)->first = first;
3811*38fd1498Szrj if (a == a2)
3812*38fd1498Szrj break;
3813*38fd1498Szrj last = a;
3814*38fd1498Szrj }
3815*38fd1498Szrj next = allocno_coalesce_data[ALLOCNO_NUM (first)].next;
3816*38fd1498Szrj allocno_coalesce_data[ALLOCNO_NUM (first)].next = a2;
3817*38fd1498Szrj allocno_coalesce_data[ALLOCNO_NUM (last)].next = next;
3818*38fd1498Szrj }
3819*38fd1498Szrj
3820*38fd1498Szrj /* Return TRUE if there are conflicting allocnos from two sets of
3821*38fd1498Szrj coalesced allocnos given correspondingly by allocnos A1 and A2. We
3822*38fd1498Szrj use live ranges to find conflicts because conflicts are represented
3823*38fd1498Szrj only for allocnos of the same allocno class and during the reload
3824*38fd1498Szrj pass we coalesce allocnos for sharing stack memory slots. */
3825*38fd1498Szrj static bool
coalesced_allocno_conflict_p(ira_allocno_t a1,ira_allocno_t a2)3826*38fd1498Szrj coalesced_allocno_conflict_p (ira_allocno_t a1, ira_allocno_t a2)
3827*38fd1498Szrj {
3828*38fd1498Szrj ira_allocno_t a, conflict_a;
3829*38fd1498Szrj
3830*38fd1498Szrj if (allocno_coalesced_p)
3831*38fd1498Szrj {
3832*38fd1498Szrj bitmap_clear (processed_coalesced_allocno_bitmap);
3833*38fd1498Szrj for (a = ALLOCNO_COALESCE_DATA (a1)->next;;
3834*38fd1498Szrj a = ALLOCNO_COALESCE_DATA (a)->next)
3835*38fd1498Szrj {
3836*38fd1498Szrj bitmap_set_bit (processed_coalesced_allocno_bitmap, ALLOCNO_NUM (a));
3837*38fd1498Szrj if (a == a1)
3838*38fd1498Szrj break;
3839*38fd1498Szrj }
3840*38fd1498Szrj }
3841*38fd1498Szrj for (a = ALLOCNO_COALESCE_DATA (a2)->next;;
3842*38fd1498Szrj a = ALLOCNO_COALESCE_DATA (a)->next)
3843*38fd1498Szrj {
3844*38fd1498Szrj for (conflict_a = ALLOCNO_COALESCE_DATA (a1)->next;;
3845*38fd1498Szrj conflict_a = ALLOCNO_COALESCE_DATA (conflict_a)->next)
3846*38fd1498Szrj {
3847*38fd1498Szrj if (allocnos_conflict_by_live_ranges_p (a, conflict_a))
3848*38fd1498Szrj return true;
3849*38fd1498Szrj if (conflict_a == a1)
3850*38fd1498Szrj break;
3851*38fd1498Szrj }
3852*38fd1498Szrj if (a == a2)
3853*38fd1498Szrj break;
3854*38fd1498Szrj }
3855*38fd1498Szrj return false;
3856*38fd1498Szrj }
3857*38fd1498Szrj
3858*38fd1498Szrj /* The major function for aggressive allocno coalescing. We coalesce
3859*38fd1498Szrj only spilled allocnos. If some allocnos have been coalesced, we
3860*38fd1498Szrj set up flag allocno_coalesced_p. */
3861*38fd1498Szrj static void
coalesce_allocnos(void)3862*38fd1498Szrj coalesce_allocnos (void)
3863*38fd1498Szrj {
3864*38fd1498Szrj ira_allocno_t a;
3865*38fd1498Szrj ira_copy_t cp, next_cp;
3866*38fd1498Szrj unsigned int j;
3867*38fd1498Szrj int i, n, cp_num, regno;
3868*38fd1498Szrj bitmap_iterator bi;
3869*38fd1498Szrj
3870*38fd1498Szrj cp_num = 0;
3871*38fd1498Szrj /* Collect copies. */
3872*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (coloring_allocno_bitmap, 0, j, bi)
3873*38fd1498Szrj {
3874*38fd1498Szrj a = ira_allocnos[j];
3875*38fd1498Szrj regno = ALLOCNO_REGNO (a);
3876*38fd1498Szrj if (! ALLOCNO_ASSIGNED_P (a) || ALLOCNO_HARD_REGNO (a) >= 0
3877*38fd1498Szrj || ira_equiv_no_lvalue_p (regno))
3878*38fd1498Szrj continue;
3879*38fd1498Szrj for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
3880*38fd1498Szrj {
3881*38fd1498Szrj if (cp->first == a)
3882*38fd1498Szrj {
3883*38fd1498Szrj next_cp = cp->next_first_allocno_copy;
3884*38fd1498Szrj regno = ALLOCNO_REGNO (cp->second);
3885*38fd1498Szrj /* For priority coloring we coalesce allocnos only with
3886*38fd1498Szrj the same allocno class not with intersected allocno
3887*38fd1498Szrj classes as it were possible. It is done for
3888*38fd1498Szrj simplicity. */
3889*38fd1498Szrj if ((cp->insn != NULL || cp->constraint_p)
3890*38fd1498Szrj && ALLOCNO_ASSIGNED_P (cp->second)
3891*38fd1498Szrj && ALLOCNO_HARD_REGNO (cp->second) < 0
3892*38fd1498Szrj && ! ira_equiv_no_lvalue_p (regno))
3893*38fd1498Szrj sorted_copies[cp_num++] = cp;
3894*38fd1498Szrj }
3895*38fd1498Szrj else if (cp->second == a)
3896*38fd1498Szrj next_cp = cp->next_second_allocno_copy;
3897*38fd1498Szrj else
3898*38fd1498Szrj gcc_unreachable ();
3899*38fd1498Szrj }
3900*38fd1498Szrj }
3901*38fd1498Szrj qsort (sorted_copies, cp_num, sizeof (ira_copy_t), copy_freq_compare_func);
3902*38fd1498Szrj /* Coalesced copies, most frequently executed first. */
3903*38fd1498Szrj for (; cp_num != 0;)
3904*38fd1498Szrj {
3905*38fd1498Szrj for (i = 0; i < cp_num; i++)
3906*38fd1498Szrj {
3907*38fd1498Szrj cp = sorted_copies[i];
3908*38fd1498Szrj if (! coalesced_allocno_conflict_p (cp->first, cp->second))
3909*38fd1498Szrj {
3910*38fd1498Szrj allocno_coalesced_p = true;
3911*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
3912*38fd1498Szrj fprintf
3913*38fd1498Szrj (ira_dump_file,
3914*38fd1498Szrj " Coalescing copy %d:a%dr%d-a%dr%d (freq=%d)\n",
3915*38fd1498Szrj cp->num, ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
3916*38fd1498Szrj ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second),
3917*38fd1498Szrj cp->freq);
3918*38fd1498Szrj merge_allocnos (cp->first, cp->second);
3919*38fd1498Szrj i++;
3920*38fd1498Szrj break;
3921*38fd1498Szrj }
3922*38fd1498Szrj }
3923*38fd1498Szrj /* Collect the rest of copies. */
3924*38fd1498Szrj for (n = 0; i < cp_num; i++)
3925*38fd1498Szrj {
3926*38fd1498Szrj cp = sorted_copies[i];
3927*38fd1498Szrj if (allocno_coalesce_data[ALLOCNO_NUM (cp->first)].first
3928*38fd1498Szrj != allocno_coalesce_data[ALLOCNO_NUM (cp->second)].first)
3929*38fd1498Szrj sorted_copies[n++] = cp;
3930*38fd1498Szrj }
3931*38fd1498Szrj cp_num = n;
3932*38fd1498Szrj }
3933*38fd1498Szrj }
3934*38fd1498Szrj
3935*38fd1498Szrj /* Usage cost and order number of coalesced allocno set to which
3936*38fd1498Szrj given pseudo register belongs to. */
3937*38fd1498Szrj static int *regno_coalesced_allocno_cost;
3938*38fd1498Szrj static int *regno_coalesced_allocno_num;
3939*38fd1498Szrj
3940*38fd1498Szrj /* Sort pseudos according frequencies of coalesced allocno sets they
3941*38fd1498Szrj belong to (putting most frequently ones first), and according to
3942*38fd1498Szrj coalesced allocno set order numbers. */
3943*38fd1498Szrj static int
coalesced_pseudo_reg_freq_compare(const void * v1p,const void * v2p)3944*38fd1498Szrj coalesced_pseudo_reg_freq_compare (const void *v1p, const void *v2p)
3945*38fd1498Szrj {
3946*38fd1498Szrj const int regno1 = *(const int *) v1p;
3947*38fd1498Szrj const int regno2 = *(const int *) v2p;
3948*38fd1498Szrj int diff;
3949*38fd1498Szrj
3950*38fd1498Szrj if ((diff = (regno_coalesced_allocno_cost[regno2]
3951*38fd1498Szrj - regno_coalesced_allocno_cost[regno1])) != 0)
3952*38fd1498Szrj return diff;
3953*38fd1498Szrj if ((diff = (regno_coalesced_allocno_num[regno1]
3954*38fd1498Szrj - regno_coalesced_allocno_num[regno2])) != 0)
3955*38fd1498Szrj return diff;
3956*38fd1498Szrj return regno1 - regno2;
3957*38fd1498Szrj }
3958*38fd1498Szrj
3959*38fd1498Szrj /* Widest width in which each pseudo reg is referred to (via subreg).
3960*38fd1498Szrj It is used for sorting pseudo registers. */
3961*38fd1498Szrj static machine_mode *regno_max_ref_mode;
3962*38fd1498Szrj
3963*38fd1498Szrj /* Sort pseudos according their slot numbers (putting ones with
3964*38fd1498Szrj smaller numbers first, or last when the frame pointer is not
3965*38fd1498Szrj needed). */
3966*38fd1498Szrj static int
coalesced_pseudo_reg_slot_compare(const void * v1p,const void * v2p)3967*38fd1498Szrj coalesced_pseudo_reg_slot_compare (const void *v1p, const void *v2p)
3968*38fd1498Szrj {
3969*38fd1498Szrj const int regno1 = *(const int *) v1p;
3970*38fd1498Szrj const int regno2 = *(const int *) v2p;
3971*38fd1498Szrj ira_allocno_t a1 = ira_regno_allocno_map[regno1];
3972*38fd1498Szrj ira_allocno_t a2 = ira_regno_allocno_map[regno2];
3973*38fd1498Szrj int diff, slot_num1, slot_num2;
3974*38fd1498Szrj machine_mode mode1, mode2;
3975*38fd1498Szrj
3976*38fd1498Szrj if (a1 == NULL || ALLOCNO_HARD_REGNO (a1) >= 0)
3977*38fd1498Szrj {
3978*38fd1498Szrj if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
3979*38fd1498Szrj return regno1 - regno2;
3980*38fd1498Szrj return 1;
3981*38fd1498Szrj }
3982*38fd1498Szrj else if (a2 == NULL || ALLOCNO_HARD_REGNO (a2) >= 0)
3983*38fd1498Szrj return -1;
3984*38fd1498Szrj slot_num1 = -ALLOCNO_HARD_REGNO (a1);
3985*38fd1498Szrj slot_num2 = -ALLOCNO_HARD_REGNO (a2);
3986*38fd1498Szrj if ((diff = slot_num1 - slot_num2) != 0)
3987*38fd1498Szrj return (frame_pointer_needed
3988*38fd1498Szrj || (!FRAME_GROWS_DOWNWARD) == STACK_GROWS_DOWNWARD ? diff : -diff);
3989*38fd1498Szrj mode1 = wider_subreg_mode (PSEUDO_REGNO_MODE (regno1),
3990*38fd1498Szrj regno_max_ref_mode[regno1]);
3991*38fd1498Szrj mode2 = wider_subreg_mode (PSEUDO_REGNO_MODE (regno2),
3992*38fd1498Szrj regno_max_ref_mode[regno2]);
3993*38fd1498Szrj if ((diff = compare_sizes_for_sort (GET_MODE_SIZE (mode2),
3994*38fd1498Szrj GET_MODE_SIZE (mode1))) != 0)
3995*38fd1498Szrj return diff;
3996*38fd1498Szrj return regno1 - regno2;
3997*38fd1498Szrj }
3998*38fd1498Szrj
3999*38fd1498Szrj /* Setup REGNO_COALESCED_ALLOCNO_COST and REGNO_COALESCED_ALLOCNO_NUM
4000*38fd1498Szrj for coalesced allocno sets containing allocnos with their regnos
4001*38fd1498Szrj given in array PSEUDO_REGNOS of length N. */
4002*38fd1498Szrj static void
setup_coalesced_allocno_costs_and_nums(int * pseudo_regnos,int n)4003*38fd1498Szrj setup_coalesced_allocno_costs_and_nums (int *pseudo_regnos, int n)
4004*38fd1498Szrj {
4005*38fd1498Szrj int i, num, regno, cost;
4006*38fd1498Szrj ira_allocno_t allocno, a;
4007*38fd1498Szrj
4008*38fd1498Szrj for (num = i = 0; i < n; i++)
4009*38fd1498Szrj {
4010*38fd1498Szrj regno = pseudo_regnos[i];
4011*38fd1498Szrj allocno = ira_regno_allocno_map[regno];
4012*38fd1498Szrj if (allocno == NULL)
4013*38fd1498Szrj {
4014*38fd1498Szrj regno_coalesced_allocno_cost[regno] = 0;
4015*38fd1498Szrj regno_coalesced_allocno_num[regno] = ++num;
4016*38fd1498Szrj continue;
4017*38fd1498Szrj }
4018*38fd1498Szrj if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
4019*38fd1498Szrj continue;
4020*38fd1498Szrj num++;
4021*38fd1498Szrj for (cost = 0, a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4022*38fd1498Szrj a = ALLOCNO_COALESCE_DATA (a)->next)
4023*38fd1498Szrj {
4024*38fd1498Szrj cost += ALLOCNO_FREQ (a);
4025*38fd1498Szrj if (a == allocno)
4026*38fd1498Szrj break;
4027*38fd1498Szrj }
4028*38fd1498Szrj for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4029*38fd1498Szrj a = ALLOCNO_COALESCE_DATA (a)->next)
4030*38fd1498Szrj {
4031*38fd1498Szrj regno_coalesced_allocno_num[ALLOCNO_REGNO (a)] = num;
4032*38fd1498Szrj regno_coalesced_allocno_cost[ALLOCNO_REGNO (a)] = cost;
4033*38fd1498Szrj if (a == allocno)
4034*38fd1498Szrj break;
4035*38fd1498Szrj }
4036*38fd1498Szrj }
4037*38fd1498Szrj }
4038*38fd1498Szrj
4039*38fd1498Szrj /* Collect spilled allocnos representing coalesced allocno sets (the
4040*38fd1498Szrj first coalesced allocno). The collected allocnos are returned
4041*38fd1498Szrj through array SPILLED_COALESCED_ALLOCNOS. The function returns the
4042*38fd1498Szrj number of the collected allocnos. The allocnos are given by their
4043*38fd1498Szrj regnos in array PSEUDO_REGNOS of length N. */
4044*38fd1498Szrj static int
collect_spilled_coalesced_allocnos(int * pseudo_regnos,int n,ira_allocno_t * spilled_coalesced_allocnos)4045*38fd1498Szrj collect_spilled_coalesced_allocnos (int *pseudo_regnos, int n,
4046*38fd1498Szrj ira_allocno_t *spilled_coalesced_allocnos)
4047*38fd1498Szrj {
4048*38fd1498Szrj int i, num, regno;
4049*38fd1498Szrj ira_allocno_t allocno;
4050*38fd1498Szrj
4051*38fd1498Szrj for (num = i = 0; i < n; i++)
4052*38fd1498Szrj {
4053*38fd1498Szrj regno = pseudo_regnos[i];
4054*38fd1498Szrj allocno = ira_regno_allocno_map[regno];
4055*38fd1498Szrj if (allocno == NULL || ALLOCNO_HARD_REGNO (allocno) >= 0
4056*38fd1498Szrj || ALLOCNO_COALESCE_DATA (allocno)->first != allocno)
4057*38fd1498Szrj continue;
4058*38fd1498Szrj spilled_coalesced_allocnos[num++] = allocno;
4059*38fd1498Szrj }
4060*38fd1498Szrj return num;
4061*38fd1498Szrj }
4062*38fd1498Szrj
4063*38fd1498Szrj /* Array of live ranges of size IRA_ALLOCNOS_NUM. Live range for
4064*38fd1498Szrj given slot contains live ranges of coalesced allocnos assigned to
4065*38fd1498Szrj given slot. */
4066*38fd1498Szrj static live_range_t *slot_coalesced_allocnos_live_ranges;
4067*38fd1498Szrj
4068*38fd1498Szrj /* Return TRUE if coalesced allocnos represented by ALLOCNO has live
4069*38fd1498Szrj ranges intersected with live ranges of coalesced allocnos assigned
4070*38fd1498Szrj to slot with number N. */
4071*38fd1498Szrj static bool
slot_coalesced_allocno_live_ranges_intersect_p(ira_allocno_t allocno,int n)4072*38fd1498Szrj slot_coalesced_allocno_live_ranges_intersect_p (ira_allocno_t allocno, int n)
4073*38fd1498Szrj {
4074*38fd1498Szrj ira_allocno_t a;
4075*38fd1498Szrj
4076*38fd1498Szrj for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4077*38fd1498Szrj a = ALLOCNO_COALESCE_DATA (a)->next)
4078*38fd1498Szrj {
4079*38fd1498Szrj int i;
4080*38fd1498Szrj int nr = ALLOCNO_NUM_OBJECTS (a);
4081*38fd1498Szrj gcc_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
4082*38fd1498Szrj for (i = 0; i < nr; i++)
4083*38fd1498Szrj {
4084*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, i);
4085*38fd1498Szrj
4086*38fd1498Szrj if (ira_live_ranges_intersect_p
4087*38fd1498Szrj (slot_coalesced_allocnos_live_ranges[n],
4088*38fd1498Szrj OBJECT_LIVE_RANGES (obj)))
4089*38fd1498Szrj return true;
4090*38fd1498Szrj }
4091*38fd1498Szrj if (a == allocno)
4092*38fd1498Szrj break;
4093*38fd1498Szrj }
4094*38fd1498Szrj return false;
4095*38fd1498Szrj }
4096*38fd1498Szrj
4097*38fd1498Szrj /* Update live ranges of slot to which coalesced allocnos represented
4098*38fd1498Szrj by ALLOCNO were assigned. */
4099*38fd1498Szrj static void
setup_slot_coalesced_allocno_live_ranges(ira_allocno_t allocno)4100*38fd1498Szrj setup_slot_coalesced_allocno_live_ranges (ira_allocno_t allocno)
4101*38fd1498Szrj {
4102*38fd1498Szrj int i, n;
4103*38fd1498Szrj ira_allocno_t a;
4104*38fd1498Szrj live_range_t r;
4105*38fd1498Szrj
4106*38fd1498Szrj n = ALLOCNO_COALESCE_DATA (allocno)->temp;
4107*38fd1498Szrj for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4108*38fd1498Szrj a = ALLOCNO_COALESCE_DATA (a)->next)
4109*38fd1498Szrj {
4110*38fd1498Szrj int nr = ALLOCNO_NUM_OBJECTS (a);
4111*38fd1498Szrj gcc_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
4112*38fd1498Szrj for (i = 0; i < nr; i++)
4113*38fd1498Szrj {
4114*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, i);
4115*38fd1498Szrj
4116*38fd1498Szrj r = ira_copy_live_range_list (OBJECT_LIVE_RANGES (obj));
4117*38fd1498Szrj slot_coalesced_allocnos_live_ranges[n]
4118*38fd1498Szrj = ira_merge_live_ranges
4119*38fd1498Szrj (slot_coalesced_allocnos_live_ranges[n], r);
4120*38fd1498Szrj }
4121*38fd1498Szrj if (a == allocno)
4122*38fd1498Szrj break;
4123*38fd1498Szrj }
4124*38fd1498Szrj }
4125*38fd1498Szrj
4126*38fd1498Szrj /* We have coalesced allocnos involving in copies. Coalesce allocnos
4127*38fd1498Szrj further in order to share the same memory stack slot. Allocnos
4128*38fd1498Szrj representing sets of allocnos coalesced before the call are given
4129*38fd1498Szrj in array SPILLED_COALESCED_ALLOCNOS of length NUM. Return TRUE if
4130*38fd1498Szrj some allocnos were coalesced in the function. */
4131*38fd1498Szrj static bool
coalesce_spill_slots(ira_allocno_t * spilled_coalesced_allocnos,int num)4132*38fd1498Szrj coalesce_spill_slots (ira_allocno_t *spilled_coalesced_allocnos, int num)
4133*38fd1498Szrj {
4134*38fd1498Szrj int i, j, n, last_coalesced_allocno_num;
4135*38fd1498Szrj ira_allocno_t allocno, a;
4136*38fd1498Szrj bool merged_p = false;
4137*38fd1498Szrj bitmap set_jump_crosses = regstat_get_setjmp_crosses ();
4138*38fd1498Szrj
4139*38fd1498Szrj slot_coalesced_allocnos_live_ranges
4140*38fd1498Szrj = (live_range_t *) ira_allocate (sizeof (live_range_t) * ira_allocnos_num);
4141*38fd1498Szrj memset (slot_coalesced_allocnos_live_ranges, 0,
4142*38fd1498Szrj sizeof (live_range_t) * ira_allocnos_num);
4143*38fd1498Szrj last_coalesced_allocno_num = 0;
4144*38fd1498Szrj /* Coalesce non-conflicting spilled allocnos preferring most
4145*38fd1498Szrj frequently used. */
4146*38fd1498Szrj for (i = 0; i < num; i++)
4147*38fd1498Szrj {
4148*38fd1498Szrj allocno = spilled_coalesced_allocnos[i];
4149*38fd1498Szrj if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
4150*38fd1498Szrj || bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (allocno))
4151*38fd1498Szrj || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
4152*38fd1498Szrj continue;
4153*38fd1498Szrj for (j = 0; j < i; j++)
4154*38fd1498Szrj {
4155*38fd1498Szrj a = spilled_coalesced_allocnos[j];
4156*38fd1498Szrj n = ALLOCNO_COALESCE_DATA (a)->temp;
4157*38fd1498Szrj if (ALLOCNO_COALESCE_DATA (a)->first == a
4158*38fd1498Szrj && ! bitmap_bit_p (set_jump_crosses, ALLOCNO_REGNO (a))
4159*38fd1498Szrj && ! ira_equiv_no_lvalue_p (ALLOCNO_REGNO (a))
4160*38fd1498Szrj && ! slot_coalesced_allocno_live_ranges_intersect_p (allocno, n))
4161*38fd1498Szrj break;
4162*38fd1498Szrj }
4163*38fd1498Szrj if (j >= i)
4164*38fd1498Szrj {
4165*38fd1498Szrj /* No coalescing: set up number for coalesced allocnos
4166*38fd1498Szrj represented by ALLOCNO. */
4167*38fd1498Szrj ALLOCNO_COALESCE_DATA (allocno)->temp = last_coalesced_allocno_num++;
4168*38fd1498Szrj setup_slot_coalesced_allocno_live_ranges (allocno);
4169*38fd1498Szrj }
4170*38fd1498Szrj else
4171*38fd1498Szrj {
4172*38fd1498Szrj allocno_coalesced_p = true;
4173*38fd1498Szrj merged_p = true;
4174*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4175*38fd1498Szrj fprintf (ira_dump_file,
4176*38fd1498Szrj " Coalescing spilled allocnos a%dr%d->a%dr%d\n",
4177*38fd1498Szrj ALLOCNO_NUM (allocno), ALLOCNO_REGNO (allocno),
4178*38fd1498Szrj ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
4179*38fd1498Szrj ALLOCNO_COALESCE_DATA (allocno)->temp
4180*38fd1498Szrj = ALLOCNO_COALESCE_DATA (a)->temp;
4181*38fd1498Szrj setup_slot_coalesced_allocno_live_ranges (allocno);
4182*38fd1498Szrj merge_allocnos (a, allocno);
4183*38fd1498Szrj ira_assert (ALLOCNO_COALESCE_DATA (a)->first == a);
4184*38fd1498Szrj }
4185*38fd1498Szrj }
4186*38fd1498Szrj for (i = 0; i < ira_allocnos_num; i++)
4187*38fd1498Szrj ira_finish_live_range_list (slot_coalesced_allocnos_live_ranges[i]);
4188*38fd1498Szrj ira_free (slot_coalesced_allocnos_live_ranges);
4189*38fd1498Szrj return merged_p;
4190*38fd1498Szrj }
4191*38fd1498Szrj
4192*38fd1498Szrj /* Sort pseudo-register numbers in array PSEUDO_REGNOS of length N for
4193*38fd1498Szrj subsequent assigning stack slots to them in the reload pass. To do
4194*38fd1498Szrj this we coalesce spilled allocnos first to decrease the number of
4195*38fd1498Szrj memory-memory move insns. This function is called by the
4196*38fd1498Szrj reload. */
4197*38fd1498Szrj void
ira_sort_regnos_for_alter_reg(int * pseudo_regnos,int n,machine_mode * reg_max_ref_mode)4198*38fd1498Szrj ira_sort_regnos_for_alter_reg (int *pseudo_regnos, int n,
4199*38fd1498Szrj machine_mode *reg_max_ref_mode)
4200*38fd1498Szrj {
4201*38fd1498Szrj int max_regno = max_reg_num ();
4202*38fd1498Szrj int i, regno, num, slot_num;
4203*38fd1498Szrj ira_allocno_t allocno, a;
4204*38fd1498Szrj ira_allocno_iterator ai;
4205*38fd1498Szrj ira_allocno_t *spilled_coalesced_allocnos;
4206*38fd1498Szrj
4207*38fd1498Szrj ira_assert (! ira_use_lra_p);
4208*38fd1498Szrj
4209*38fd1498Szrj /* Set up allocnos can be coalesced. */
4210*38fd1498Szrj coloring_allocno_bitmap = ira_allocate_bitmap ();
4211*38fd1498Szrj for (i = 0; i < n; i++)
4212*38fd1498Szrj {
4213*38fd1498Szrj regno = pseudo_regnos[i];
4214*38fd1498Szrj allocno = ira_regno_allocno_map[regno];
4215*38fd1498Szrj if (allocno != NULL)
4216*38fd1498Szrj bitmap_set_bit (coloring_allocno_bitmap, ALLOCNO_NUM (allocno));
4217*38fd1498Szrj }
4218*38fd1498Szrj allocno_coalesced_p = false;
4219*38fd1498Szrj processed_coalesced_allocno_bitmap = ira_allocate_bitmap ();
4220*38fd1498Szrj allocno_coalesce_data
4221*38fd1498Szrj = (coalesce_data_t) ira_allocate (sizeof (struct coalesce_data)
4222*38fd1498Szrj * ira_allocnos_num);
4223*38fd1498Szrj /* Initialize coalesce data for allocnos. */
4224*38fd1498Szrj FOR_EACH_ALLOCNO (a, ai)
4225*38fd1498Szrj {
4226*38fd1498Szrj ALLOCNO_ADD_DATA (a) = allocno_coalesce_data + ALLOCNO_NUM (a);
4227*38fd1498Szrj ALLOCNO_COALESCE_DATA (a)->first = a;
4228*38fd1498Szrj ALLOCNO_COALESCE_DATA (a)->next = a;
4229*38fd1498Szrj }
4230*38fd1498Szrj coalesce_allocnos ();
4231*38fd1498Szrj ira_free_bitmap (coloring_allocno_bitmap);
4232*38fd1498Szrj regno_coalesced_allocno_cost
4233*38fd1498Szrj = (int *) ira_allocate (max_regno * sizeof (int));
4234*38fd1498Szrj regno_coalesced_allocno_num
4235*38fd1498Szrj = (int *) ira_allocate (max_regno * sizeof (int));
4236*38fd1498Szrj memset (regno_coalesced_allocno_num, 0, max_regno * sizeof (int));
4237*38fd1498Szrj setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
4238*38fd1498Szrj /* Sort regnos according frequencies of the corresponding coalesced
4239*38fd1498Szrj allocno sets. */
4240*38fd1498Szrj qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_freq_compare);
4241*38fd1498Szrj spilled_coalesced_allocnos
4242*38fd1498Szrj = (ira_allocno_t *) ira_allocate (ira_allocnos_num
4243*38fd1498Szrj * sizeof (ira_allocno_t));
4244*38fd1498Szrj /* Collect allocnos representing the spilled coalesced allocno
4245*38fd1498Szrj sets. */
4246*38fd1498Szrj num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
4247*38fd1498Szrj spilled_coalesced_allocnos);
4248*38fd1498Szrj if (flag_ira_share_spill_slots
4249*38fd1498Szrj && coalesce_spill_slots (spilled_coalesced_allocnos, num))
4250*38fd1498Szrj {
4251*38fd1498Szrj setup_coalesced_allocno_costs_and_nums (pseudo_regnos, n);
4252*38fd1498Szrj qsort (pseudo_regnos, n, sizeof (int),
4253*38fd1498Szrj coalesced_pseudo_reg_freq_compare);
4254*38fd1498Szrj num = collect_spilled_coalesced_allocnos (pseudo_regnos, n,
4255*38fd1498Szrj spilled_coalesced_allocnos);
4256*38fd1498Szrj }
4257*38fd1498Szrj ira_free_bitmap (processed_coalesced_allocno_bitmap);
4258*38fd1498Szrj allocno_coalesced_p = false;
4259*38fd1498Szrj /* Assign stack slot numbers to spilled allocno sets, use smaller
4260*38fd1498Szrj numbers for most frequently used coalesced allocnos. -1 is
4261*38fd1498Szrj reserved for dynamic search of stack slots for pseudos spilled by
4262*38fd1498Szrj the reload. */
4263*38fd1498Szrj slot_num = 1;
4264*38fd1498Szrj for (i = 0; i < num; i++)
4265*38fd1498Szrj {
4266*38fd1498Szrj allocno = spilled_coalesced_allocnos[i];
4267*38fd1498Szrj if (ALLOCNO_COALESCE_DATA (allocno)->first != allocno
4268*38fd1498Szrj || ALLOCNO_HARD_REGNO (allocno) >= 0
4269*38fd1498Szrj || ira_equiv_no_lvalue_p (ALLOCNO_REGNO (allocno)))
4270*38fd1498Szrj continue;
4271*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4272*38fd1498Szrj fprintf (ira_dump_file, " Slot %d (freq,size):", slot_num);
4273*38fd1498Szrj slot_num++;
4274*38fd1498Szrj for (a = ALLOCNO_COALESCE_DATA (allocno)->next;;
4275*38fd1498Szrj a = ALLOCNO_COALESCE_DATA (a)->next)
4276*38fd1498Szrj {
4277*38fd1498Szrj ira_assert (ALLOCNO_HARD_REGNO (a) < 0);
4278*38fd1498Szrj ALLOCNO_HARD_REGNO (a) = -slot_num;
4279*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4280*38fd1498Szrj {
4281*38fd1498Szrj machine_mode mode = wider_subreg_mode
4282*38fd1498Szrj (PSEUDO_REGNO_MODE (ALLOCNO_REGNO (a)),
4283*38fd1498Szrj reg_max_ref_mode[ALLOCNO_REGNO (a)]);
4284*38fd1498Szrj fprintf (ira_dump_file, " a%dr%d(%d,",
4285*38fd1498Szrj ALLOCNO_NUM (a), ALLOCNO_REGNO (a), ALLOCNO_FREQ (a));
4286*38fd1498Szrj print_dec (GET_MODE_SIZE (mode), ira_dump_file, SIGNED);
4287*38fd1498Szrj fprintf (ira_dump_file, ")\n");
4288*38fd1498Szrj }
4289*38fd1498Szrj
4290*38fd1498Szrj if (a == allocno)
4291*38fd1498Szrj break;
4292*38fd1498Szrj }
4293*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4294*38fd1498Szrj fprintf (ira_dump_file, "\n");
4295*38fd1498Szrj }
4296*38fd1498Szrj ira_spilled_reg_stack_slots_num = slot_num - 1;
4297*38fd1498Szrj ira_free (spilled_coalesced_allocnos);
4298*38fd1498Szrj /* Sort regnos according the slot numbers. */
4299*38fd1498Szrj regno_max_ref_mode = reg_max_ref_mode;
4300*38fd1498Szrj qsort (pseudo_regnos, n, sizeof (int), coalesced_pseudo_reg_slot_compare);
4301*38fd1498Szrj FOR_EACH_ALLOCNO (a, ai)
4302*38fd1498Szrj ALLOCNO_ADD_DATA (a) = NULL;
4303*38fd1498Szrj ira_free (allocno_coalesce_data);
4304*38fd1498Szrj ira_free (regno_coalesced_allocno_num);
4305*38fd1498Szrj ira_free (regno_coalesced_allocno_cost);
4306*38fd1498Szrj }
4307*38fd1498Szrj
4308*38fd1498Szrj
4309*38fd1498Szrj
4310*38fd1498Szrj /* This page contains code used by the reload pass to improve the
4311*38fd1498Szrj final code. */
4312*38fd1498Szrj
4313*38fd1498Szrj /* The function is called from reload to mark changes in the
4314*38fd1498Szrj allocation of REGNO made by the reload. Remember that reg_renumber
4315*38fd1498Szrj reflects the change result. */
4316*38fd1498Szrj void
ira_mark_allocation_change(int regno)4317*38fd1498Szrj ira_mark_allocation_change (int regno)
4318*38fd1498Szrj {
4319*38fd1498Szrj ira_allocno_t a = ira_regno_allocno_map[regno];
4320*38fd1498Szrj int old_hard_regno, hard_regno, cost;
4321*38fd1498Szrj enum reg_class aclass = ALLOCNO_CLASS (a);
4322*38fd1498Szrj
4323*38fd1498Szrj ira_assert (a != NULL);
4324*38fd1498Szrj hard_regno = reg_renumber[regno];
4325*38fd1498Szrj if ((old_hard_regno = ALLOCNO_HARD_REGNO (a)) == hard_regno)
4326*38fd1498Szrj return;
4327*38fd1498Szrj if (old_hard_regno < 0)
4328*38fd1498Szrj cost = -ALLOCNO_MEMORY_COST (a);
4329*38fd1498Szrj else
4330*38fd1498Szrj {
4331*38fd1498Szrj ira_assert (ira_class_hard_reg_index[aclass][old_hard_regno] >= 0);
4332*38fd1498Szrj cost = -(ALLOCNO_HARD_REG_COSTS (a) == NULL
4333*38fd1498Szrj ? ALLOCNO_CLASS_COST (a)
4334*38fd1498Szrj : ALLOCNO_HARD_REG_COSTS (a)
4335*38fd1498Szrj [ira_class_hard_reg_index[aclass][old_hard_regno]]);
4336*38fd1498Szrj update_costs_from_copies (a, false, false);
4337*38fd1498Szrj }
4338*38fd1498Szrj ira_overall_cost -= cost;
4339*38fd1498Szrj ALLOCNO_HARD_REGNO (a) = hard_regno;
4340*38fd1498Szrj if (hard_regno < 0)
4341*38fd1498Szrj {
4342*38fd1498Szrj ALLOCNO_HARD_REGNO (a) = -1;
4343*38fd1498Szrj cost += ALLOCNO_MEMORY_COST (a);
4344*38fd1498Szrj }
4345*38fd1498Szrj else if (ira_class_hard_reg_index[aclass][hard_regno] >= 0)
4346*38fd1498Szrj {
4347*38fd1498Szrj cost += (ALLOCNO_HARD_REG_COSTS (a) == NULL
4348*38fd1498Szrj ? ALLOCNO_CLASS_COST (a)
4349*38fd1498Szrj : ALLOCNO_HARD_REG_COSTS (a)
4350*38fd1498Szrj [ira_class_hard_reg_index[aclass][hard_regno]]);
4351*38fd1498Szrj update_costs_from_copies (a, true, false);
4352*38fd1498Szrj }
4353*38fd1498Szrj else
4354*38fd1498Szrj /* Reload changed class of the allocno. */
4355*38fd1498Szrj cost = 0;
4356*38fd1498Szrj ira_overall_cost += cost;
4357*38fd1498Szrj }
4358*38fd1498Szrj
4359*38fd1498Szrj /* This function is called when reload deletes memory-memory move. In
4360*38fd1498Szrj this case we marks that the allocation of the corresponding
4361*38fd1498Szrj allocnos should be not changed in future. Otherwise we risk to get
4362*38fd1498Szrj a wrong code. */
4363*38fd1498Szrj void
ira_mark_memory_move_deletion(int dst_regno,int src_regno)4364*38fd1498Szrj ira_mark_memory_move_deletion (int dst_regno, int src_regno)
4365*38fd1498Szrj {
4366*38fd1498Szrj ira_allocno_t dst = ira_regno_allocno_map[dst_regno];
4367*38fd1498Szrj ira_allocno_t src = ira_regno_allocno_map[src_regno];
4368*38fd1498Szrj
4369*38fd1498Szrj ira_assert (dst != NULL && src != NULL
4370*38fd1498Szrj && ALLOCNO_HARD_REGNO (dst) < 0
4371*38fd1498Szrj && ALLOCNO_HARD_REGNO (src) < 0);
4372*38fd1498Szrj ALLOCNO_DONT_REASSIGN_P (dst) = true;
4373*38fd1498Szrj ALLOCNO_DONT_REASSIGN_P (src) = true;
4374*38fd1498Szrj }
4375*38fd1498Szrj
4376*38fd1498Szrj /* Try to assign a hard register (except for FORBIDDEN_REGS) to
4377*38fd1498Szrj allocno A and return TRUE in the case of success. */
4378*38fd1498Szrj static bool
allocno_reload_assign(ira_allocno_t a,HARD_REG_SET forbidden_regs)4379*38fd1498Szrj allocno_reload_assign (ira_allocno_t a, HARD_REG_SET forbidden_regs)
4380*38fd1498Szrj {
4381*38fd1498Szrj int hard_regno;
4382*38fd1498Szrj enum reg_class aclass;
4383*38fd1498Szrj int regno = ALLOCNO_REGNO (a);
4384*38fd1498Szrj HARD_REG_SET saved[2];
4385*38fd1498Szrj int i, n;
4386*38fd1498Szrj
4387*38fd1498Szrj n = ALLOCNO_NUM_OBJECTS (a);
4388*38fd1498Szrj for (i = 0; i < n; i++)
4389*38fd1498Szrj {
4390*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, i);
4391*38fd1498Szrj COPY_HARD_REG_SET (saved[i], OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
4392*38fd1498Szrj IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), forbidden_regs);
4393*38fd1498Szrj if (! flag_caller_saves && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
4394*38fd1498Szrj IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
4395*38fd1498Szrj call_used_reg_set);
4396*38fd1498Szrj }
4397*38fd1498Szrj ALLOCNO_ASSIGNED_P (a) = false;
4398*38fd1498Szrj aclass = ALLOCNO_CLASS (a);
4399*38fd1498Szrj update_curr_costs (a);
4400*38fd1498Szrj assign_hard_reg (a, true);
4401*38fd1498Szrj hard_regno = ALLOCNO_HARD_REGNO (a);
4402*38fd1498Szrj reg_renumber[regno] = hard_regno;
4403*38fd1498Szrj if (hard_regno < 0)
4404*38fd1498Szrj ALLOCNO_HARD_REGNO (a) = -1;
4405*38fd1498Szrj else
4406*38fd1498Szrj {
4407*38fd1498Szrj ira_assert (ira_class_hard_reg_index[aclass][hard_regno] >= 0);
4408*38fd1498Szrj ira_overall_cost
4409*38fd1498Szrj -= (ALLOCNO_MEMORY_COST (a)
4410*38fd1498Szrj - (ALLOCNO_HARD_REG_COSTS (a) == NULL
4411*38fd1498Szrj ? ALLOCNO_CLASS_COST (a)
4412*38fd1498Szrj : ALLOCNO_HARD_REG_COSTS (a)[ira_class_hard_reg_index
4413*38fd1498Szrj [aclass][hard_regno]]));
4414*38fd1498Szrj if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0
4415*38fd1498Szrj && ira_hard_reg_set_intersection_p (hard_regno, ALLOCNO_MODE (a),
4416*38fd1498Szrj call_used_reg_set))
4417*38fd1498Szrj {
4418*38fd1498Szrj ira_assert (flag_caller_saves);
4419*38fd1498Szrj caller_save_needed = 1;
4420*38fd1498Szrj }
4421*38fd1498Szrj }
4422*38fd1498Szrj
4423*38fd1498Szrj /* If we found a hard register, modify the RTL for the pseudo
4424*38fd1498Szrj register to show the hard register, and mark the pseudo register
4425*38fd1498Szrj live. */
4426*38fd1498Szrj if (reg_renumber[regno] >= 0)
4427*38fd1498Szrj {
4428*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4429*38fd1498Szrj fprintf (ira_dump_file, ": reassign to %d\n", reg_renumber[regno]);
4430*38fd1498Szrj SET_REGNO (regno_reg_rtx[regno], reg_renumber[regno]);
4431*38fd1498Szrj mark_home_live (regno);
4432*38fd1498Szrj }
4433*38fd1498Szrj else if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4434*38fd1498Szrj fprintf (ira_dump_file, "\n");
4435*38fd1498Szrj for (i = 0; i < n; i++)
4436*38fd1498Szrj {
4437*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, i);
4438*38fd1498Szrj COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), saved[i]);
4439*38fd1498Szrj }
4440*38fd1498Szrj return reg_renumber[regno] >= 0;
4441*38fd1498Szrj }
4442*38fd1498Szrj
4443*38fd1498Szrj /* Sort pseudos according their usage frequencies (putting most
4444*38fd1498Szrj frequently ones first). */
4445*38fd1498Szrj static int
pseudo_reg_compare(const void * v1p,const void * v2p)4446*38fd1498Szrj pseudo_reg_compare (const void *v1p, const void *v2p)
4447*38fd1498Szrj {
4448*38fd1498Szrj int regno1 = *(const int *) v1p;
4449*38fd1498Szrj int regno2 = *(const int *) v2p;
4450*38fd1498Szrj int diff;
4451*38fd1498Szrj
4452*38fd1498Szrj if ((diff = REG_FREQ (regno2) - REG_FREQ (regno1)) != 0)
4453*38fd1498Szrj return diff;
4454*38fd1498Szrj return regno1 - regno2;
4455*38fd1498Szrj }
4456*38fd1498Szrj
4457*38fd1498Szrj /* Try to allocate hard registers to SPILLED_PSEUDO_REGS (there are
4458*38fd1498Szrj NUM of them) or spilled pseudos conflicting with pseudos in
4459*38fd1498Szrj SPILLED_PSEUDO_REGS. Return TRUE and update SPILLED, if the
4460*38fd1498Szrj allocation has been changed. The function doesn't use
4461*38fd1498Szrj BAD_SPILL_REGS and hard registers in PSEUDO_FORBIDDEN_REGS and
4462*38fd1498Szrj PSEUDO_PREVIOUS_REGS for the corresponding pseudos. The function
4463*38fd1498Szrj is called by the reload pass at the end of each reload
4464*38fd1498Szrj iteration. */
4465*38fd1498Szrj bool
ira_reassign_pseudos(int * spilled_pseudo_regs,int num,HARD_REG_SET bad_spill_regs,HARD_REG_SET * pseudo_forbidden_regs,HARD_REG_SET * pseudo_previous_regs,bitmap spilled)4466*38fd1498Szrj ira_reassign_pseudos (int *spilled_pseudo_regs, int num,
4467*38fd1498Szrj HARD_REG_SET bad_spill_regs,
4468*38fd1498Szrj HARD_REG_SET *pseudo_forbidden_regs,
4469*38fd1498Szrj HARD_REG_SET *pseudo_previous_regs,
4470*38fd1498Szrj bitmap spilled)
4471*38fd1498Szrj {
4472*38fd1498Szrj int i, n, regno;
4473*38fd1498Szrj bool changed_p;
4474*38fd1498Szrj ira_allocno_t a;
4475*38fd1498Szrj HARD_REG_SET forbidden_regs;
4476*38fd1498Szrj bitmap temp = BITMAP_ALLOC (NULL);
4477*38fd1498Szrj
4478*38fd1498Szrj /* Add pseudos which conflict with pseudos already in
4479*38fd1498Szrj SPILLED_PSEUDO_REGS to SPILLED_PSEUDO_REGS. This is preferable
4480*38fd1498Szrj to allocating in two steps as some of the conflicts might have
4481*38fd1498Szrj a higher priority than the pseudos passed in SPILLED_PSEUDO_REGS. */
4482*38fd1498Szrj for (i = 0; i < num; i++)
4483*38fd1498Szrj bitmap_set_bit (temp, spilled_pseudo_regs[i]);
4484*38fd1498Szrj
4485*38fd1498Szrj for (i = 0, n = num; i < n; i++)
4486*38fd1498Szrj {
4487*38fd1498Szrj int nr, j;
4488*38fd1498Szrj int regno = spilled_pseudo_regs[i];
4489*38fd1498Szrj bitmap_set_bit (temp, regno);
4490*38fd1498Szrj
4491*38fd1498Szrj a = ira_regno_allocno_map[regno];
4492*38fd1498Szrj nr = ALLOCNO_NUM_OBJECTS (a);
4493*38fd1498Szrj for (j = 0; j < nr; j++)
4494*38fd1498Szrj {
4495*38fd1498Szrj ira_object_t conflict_obj;
4496*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, j);
4497*38fd1498Szrj ira_object_conflict_iterator oci;
4498*38fd1498Szrj
4499*38fd1498Szrj FOR_EACH_OBJECT_CONFLICT (obj, conflict_obj, oci)
4500*38fd1498Szrj {
4501*38fd1498Szrj ira_allocno_t conflict_a = OBJECT_ALLOCNO (conflict_obj);
4502*38fd1498Szrj if (ALLOCNO_HARD_REGNO (conflict_a) < 0
4503*38fd1498Szrj && ! ALLOCNO_DONT_REASSIGN_P (conflict_a)
4504*38fd1498Szrj && bitmap_set_bit (temp, ALLOCNO_REGNO (conflict_a)))
4505*38fd1498Szrj {
4506*38fd1498Szrj spilled_pseudo_regs[num++] = ALLOCNO_REGNO (conflict_a);
4507*38fd1498Szrj /* ?!? This seems wrong. */
4508*38fd1498Szrj bitmap_set_bit (consideration_allocno_bitmap,
4509*38fd1498Szrj ALLOCNO_NUM (conflict_a));
4510*38fd1498Szrj }
4511*38fd1498Szrj }
4512*38fd1498Szrj }
4513*38fd1498Szrj }
4514*38fd1498Szrj
4515*38fd1498Szrj if (num > 1)
4516*38fd1498Szrj qsort (spilled_pseudo_regs, num, sizeof (int), pseudo_reg_compare);
4517*38fd1498Szrj changed_p = false;
4518*38fd1498Szrj /* Try to assign hard registers to pseudos from
4519*38fd1498Szrj SPILLED_PSEUDO_REGS. */
4520*38fd1498Szrj for (i = 0; i < num; i++)
4521*38fd1498Szrj {
4522*38fd1498Szrj regno = spilled_pseudo_regs[i];
4523*38fd1498Szrj COPY_HARD_REG_SET (forbidden_regs, bad_spill_regs);
4524*38fd1498Szrj IOR_HARD_REG_SET (forbidden_regs, pseudo_forbidden_regs[regno]);
4525*38fd1498Szrj IOR_HARD_REG_SET (forbidden_regs, pseudo_previous_regs[regno]);
4526*38fd1498Szrj gcc_assert (reg_renumber[regno] < 0);
4527*38fd1498Szrj a = ira_regno_allocno_map[regno];
4528*38fd1498Szrj ira_mark_allocation_change (regno);
4529*38fd1498Szrj ira_assert (reg_renumber[regno] < 0);
4530*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file != NULL)
4531*38fd1498Szrj fprintf (ira_dump_file,
4532*38fd1498Szrj " Try Assign %d(a%d), cost=%d", regno, ALLOCNO_NUM (a),
4533*38fd1498Szrj ALLOCNO_MEMORY_COST (a)
4534*38fd1498Szrj - ALLOCNO_CLASS_COST (a));
4535*38fd1498Szrj allocno_reload_assign (a, forbidden_regs);
4536*38fd1498Szrj if (reg_renumber[regno] >= 0)
4537*38fd1498Szrj {
4538*38fd1498Szrj CLEAR_REGNO_REG_SET (spilled, regno);
4539*38fd1498Szrj changed_p = true;
4540*38fd1498Szrj }
4541*38fd1498Szrj }
4542*38fd1498Szrj BITMAP_FREE (temp);
4543*38fd1498Szrj return changed_p;
4544*38fd1498Szrj }
4545*38fd1498Szrj
4546*38fd1498Szrj /* The function is called by reload and returns already allocated
4547*38fd1498Szrj stack slot (if any) for REGNO with given INHERENT_SIZE and
4548*38fd1498Szrj TOTAL_SIZE. In the case of failure to find a slot which can be
4549*38fd1498Szrj used for REGNO, the function returns NULL. */
4550*38fd1498Szrj rtx
ira_reuse_stack_slot(int regno,poly_uint64 inherent_size,poly_uint64 total_size)4551*38fd1498Szrj ira_reuse_stack_slot (int regno, poly_uint64 inherent_size,
4552*38fd1498Szrj poly_uint64 total_size)
4553*38fd1498Szrj {
4554*38fd1498Szrj unsigned int i;
4555*38fd1498Szrj int slot_num, best_slot_num;
4556*38fd1498Szrj int cost, best_cost;
4557*38fd1498Szrj ira_copy_t cp, next_cp;
4558*38fd1498Szrj ira_allocno_t another_allocno, allocno = ira_regno_allocno_map[regno];
4559*38fd1498Szrj rtx x;
4560*38fd1498Szrj bitmap_iterator bi;
4561*38fd1498Szrj struct ira_spilled_reg_stack_slot *slot = NULL;
4562*38fd1498Szrj
4563*38fd1498Szrj ira_assert (! ira_use_lra_p);
4564*38fd1498Szrj
4565*38fd1498Szrj ira_assert (known_eq (inherent_size, PSEUDO_REGNO_BYTES (regno))
4566*38fd1498Szrj && known_le (inherent_size, total_size)
4567*38fd1498Szrj && ALLOCNO_HARD_REGNO (allocno) < 0);
4568*38fd1498Szrj if (! flag_ira_share_spill_slots)
4569*38fd1498Szrj return NULL_RTX;
4570*38fd1498Szrj slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4571*38fd1498Szrj if (slot_num != -1)
4572*38fd1498Szrj {
4573*38fd1498Szrj slot = &ira_spilled_reg_stack_slots[slot_num];
4574*38fd1498Szrj x = slot->mem;
4575*38fd1498Szrj }
4576*38fd1498Szrj else
4577*38fd1498Szrj {
4578*38fd1498Szrj best_cost = best_slot_num = -1;
4579*38fd1498Szrj x = NULL_RTX;
4580*38fd1498Szrj /* It means that the pseudo was spilled in the reload pass, try
4581*38fd1498Szrj to reuse a slot. */
4582*38fd1498Szrj for (slot_num = 0;
4583*38fd1498Szrj slot_num < ira_spilled_reg_stack_slots_num;
4584*38fd1498Szrj slot_num++)
4585*38fd1498Szrj {
4586*38fd1498Szrj slot = &ira_spilled_reg_stack_slots[slot_num];
4587*38fd1498Szrj if (slot->mem == NULL_RTX)
4588*38fd1498Szrj continue;
4589*38fd1498Szrj if (maybe_lt (slot->width, total_size)
4590*38fd1498Szrj || maybe_lt (GET_MODE_SIZE (GET_MODE (slot->mem)), inherent_size))
4591*38fd1498Szrj continue;
4592*38fd1498Szrj
4593*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4594*38fd1498Szrj FIRST_PSEUDO_REGISTER, i, bi)
4595*38fd1498Szrj {
4596*38fd1498Szrj another_allocno = ira_regno_allocno_map[i];
4597*38fd1498Szrj if (allocnos_conflict_by_live_ranges_p (allocno,
4598*38fd1498Szrj another_allocno))
4599*38fd1498Szrj goto cont;
4600*38fd1498Szrj }
4601*38fd1498Szrj for (cost = 0, cp = ALLOCNO_COPIES (allocno);
4602*38fd1498Szrj cp != NULL;
4603*38fd1498Szrj cp = next_cp)
4604*38fd1498Szrj {
4605*38fd1498Szrj if (cp->first == allocno)
4606*38fd1498Szrj {
4607*38fd1498Szrj next_cp = cp->next_first_allocno_copy;
4608*38fd1498Szrj another_allocno = cp->second;
4609*38fd1498Szrj }
4610*38fd1498Szrj else if (cp->second == allocno)
4611*38fd1498Szrj {
4612*38fd1498Szrj next_cp = cp->next_second_allocno_copy;
4613*38fd1498Szrj another_allocno = cp->first;
4614*38fd1498Szrj }
4615*38fd1498Szrj else
4616*38fd1498Szrj gcc_unreachable ();
4617*38fd1498Szrj if (cp->insn == NULL_RTX)
4618*38fd1498Szrj continue;
4619*38fd1498Szrj if (bitmap_bit_p (&slot->spilled_regs,
4620*38fd1498Szrj ALLOCNO_REGNO (another_allocno)))
4621*38fd1498Szrj cost += cp->freq;
4622*38fd1498Szrj }
4623*38fd1498Szrj if (cost > best_cost)
4624*38fd1498Szrj {
4625*38fd1498Szrj best_cost = cost;
4626*38fd1498Szrj best_slot_num = slot_num;
4627*38fd1498Szrj }
4628*38fd1498Szrj cont:
4629*38fd1498Szrj ;
4630*38fd1498Szrj }
4631*38fd1498Szrj if (best_cost >= 0)
4632*38fd1498Szrj {
4633*38fd1498Szrj slot_num = best_slot_num;
4634*38fd1498Szrj slot = &ira_spilled_reg_stack_slots[slot_num];
4635*38fd1498Szrj SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4636*38fd1498Szrj x = slot->mem;
4637*38fd1498Szrj ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4638*38fd1498Szrj }
4639*38fd1498Szrj }
4640*38fd1498Szrj if (x != NULL_RTX)
4641*38fd1498Szrj {
4642*38fd1498Szrj ira_assert (known_ge (slot->width, total_size));
4643*38fd1498Szrj #ifdef ENABLE_IRA_CHECKING
4644*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4645*38fd1498Szrj FIRST_PSEUDO_REGISTER, i, bi)
4646*38fd1498Szrj {
4647*38fd1498Szrj ira_assert (! conflict_by_live_ranges_p (regno, i));
4648*38fd1498Szrj }
4649*38fd1498Szrj #endif
4650*38fd1498Szrj SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4651*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file)
4652*38fd1498Szrj {
4653*38fd1498Szrj fprintf (ira_dump_file, " Assigning %d(freq=%d) slot %d of",
4654*38fd1498Szrj regno, REG_FREQ (regno), slot_num);
4655*38fd1498Szrj EXECUTE_IF_SET_IN_BITMAP (&slot->spilled_regs,
4656*38fd1498Szrj FIRST_PSEUDO_REGISTER, i, bi)
4657*38fd1498Szrj {
4658*38fd1498Szrj if ((unsigned) regno != i)
4659*38fd1498Szrj fprintf (ira_dump_file, " %d", i);
4660*38fd1498Szrj }
4661*38fd1498Szrj fprintf (ira_dump_file, "\n");
4662*38fd1498Szrj }
4663*38fd1498Szrj }
4664*38fd1498Szrj return x;
4665*38fd1498Szrj }
4666*38fd1498Szrj
4667*38fd1498Szrj /* This is called by reload every time a new stack slot X with
4668*38fd1498Szrj TOTAL_SIZE was allocated for REGNO. We store this info for
4669*38fd1498Szrj subsequent ira_reuse_stack_slot calls. */
4670*38fd1498Szrj void
ira_mark_new_stack_slot(rtx x,int regno,poly_uint64 total_size)4671*38fd1498Szrj ira_mark_new_stack_slot (rtx x, int regno, poly_uint64 total_size)
4672*38fd1498Szrj {
4673*38fd1498Szrj struct ira_spilled_reg_stack_slot *slot;
4674*38fd1498Szrj int slot_num;
4675*38fd1498Szrj ira_allocno_t allocno;
4676*38fd1498Szrj
4677*38fd1498Szrj ira_assert (! ira_use_lra_p);
4678*38fd1498Szrj
4679*38fd1498Szrj ira_assert (known_le (PSEUDO_REGNO_BYTES (regno), total_size));
4680*38fd1498Szrj allocno = ira_regno_allocno_map[regno];
4681*38fd1498Szrj slot_num = -ALLOCNO_HARD_REGNO (allocno) - 2;
4682*38fd1498Szrj if (slot_num == -1)
4683*38fd1498Szrj {
4684*38fd1498Szrj slot_num = ira_spilled_reg_stack_slots_num++;
4685*38fd1498Szrj ALLOCNO_HARD_REGNO (allocno) = -slot_num - 2;
4686*38fd1498Szrj }
4687*38fd1498Szrj slot = &ira_spilled_reg_stack_slots[slot_num];
4688*38fd1498Szrj INIT_REG_SET (&slot->spilled_regs);
4689*38fd1498Szrj SET_REGNO_REG_SET (&slot->spilled_regs, regno);
4690*38fd1498Szrj slot->mem = x;
4691*38fd1498Szrj slot->width = total_size;
4692*38fd1498Szrj if (internal_flag_ira_verbose > 3 && ira_dump_file)
4693*38fd1498Szrj fprintf (ira_dump_file, " Assigning %d(freq=%d) a new slot %d\n",
4694*38fd1498Szrj regno, REG_FREQ (regno), slot_num);
4695*38fd1498Szrj }
4696*38fd1498Szrj
4697*38fd1498Szrj
4698*38fd1498Szrj /* Return spill cost for pseudo-registers whose numbers are in array
4699*38fd1498Szrj REGNOS (with a negative number as an end marker) for reload with
4700*38fd1498Szrj given IN and OUT for INSN. Return also number points (through
4701*38fd1498Szrj EXCESS_PRESSURE_LIVE_LENGTH) where the pseudo-register lives and
4702*38fd1498Szrj the register pressure is high, number of references of the
4703*38fd1498Szrj pseudo-registers (through NREFS), number of callee-clobbered
4704*38fd1498Szrj hard-registers occupied by the pseudo-registers (through
4705*38fd1498Szrj CALL_USED_COUNT), and the first hard regno occupied by the
4706*38fd1498Szrj pseudo-registers (through FIRST_HARD_REGNO). */
4707*38fd1498Szrj static int
calculate_spill_cost(int * regnos,rtx in,rtx out,rtx_insn * insn,int * excess_pressure_live_length,int * nrefs,int * call_used_count,int * first_hard_regno)4708*38fd1498Szrj calculate_spill_cost (int *regnos, rtx in, rtx out, rtx_insn *insn,
4709*38fd1498Szrj int *excess_pressure_live_length,
4710*38fd1498Szrj int *nrefs, int *call_used_count, int *first_hard_regno)
4711*38fd1498Szrj {
4712*38fd1498Szrj int i, cost, regno, hard_regno, j, count, saved_cost, nregs;
4713*38fd1498Szrj bool in_p, out_p;
4714*38fd1498Szrj int length;
4715*38fd1498Szrj ira_allocno_t a;
4716*38fd1498Szrj
4717*38fd1498Szrj *nrefs = 0;
4718*38fd1498Szrj for (length = count = cost = i = 0;; i++)
4719*38fd1498Szrj {
4720*38fd1498Szrj regno = regnos[i];
4721*38fd1498Szrj if (regno < 0)
4722*38fd1498Szrj break;
4723*38fd1498Szrj *nrefs += REG_N_REFS (regno);
4724*38fd1498Szrj hard_regno = reg_renumber[regno];
4725*38fd1498Szrj ira_assert (hard_regno >= 0);
4726*38fd1498Szrj a = ira_regno_allocno_map[regno];
4727*38fd1498Szrj length += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) / ALLOCNO_NUM_OBJECTS (a);
4728*38fd1498Szrj cost += ALLOCNO_MEMORY_COST (a) - ALLOCNO_CLASS_COST (a);
4729*38fd1498Szrj nregs = hard_regno_nregs (hard_regno, ALLOCNO_MODE (a));
4730*38fd1498Szrj for (j = 0; j < nregs; j++)
4731*38fd1498Szrj if (! TEST_HARD_REG_BIT (call_used_reg_set, hard_regno + j))
4732*38fd1498Szrj break;
4733*38fd1498Szrj if (j == nregs)
4734*38fd1498Szrj count++;
4735*38fd1498Szrj in_p = in && REG_P (in) && (int) REGNO (in) == hard_regno;
4736*38fd1498Szrj out_p = out && REG_P (out) && (int) REGNO (out) == hard_regno;
4737*38fd1498Szrj if ((in_p || out_p)
4738*38fd1498Szrj && find_regno_note (insn, REG_DEAD, hard_regno) != NULL_RTX)
4739*38fd1498Szrj {
4740*38fd1498Szrj saved_cost = 0;
4741*38fd1498Szrj if (in_p)
4742*38fd1498Szrj saved_cost += ira_memory_move_cost
4743*38fd1498Szrj [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][1];
4744*38fd1498Szrj if (out_p)
4745*38fd1498Szrj saved_cost
4746*38fd1498Szrj += ira_memory_move_cost
4747*38fd1498Szrj [ALLOCNO_MODE (a)][ALLOCNO_CLASS (a)][0];
4748*38fd1498Szrj cost -= REG_FREQ_FROM_BB (BLOCK_FOR_INSN (insn)) * saved_cost;
4749*38fd1498Szrj }
4750*38fd1498Szrj }
4751*38fd1498Szrj *excess_pressure_live_length = length;
4752*38fd1498Szrj *call_used_count = count;
4753*38fd1498Szrj hard_regno = -1;
4754*38fd1498Szrj if (regnos[0] >= 0)
4755*38fd1498Szrj {
4756*38fd1498Szrj hard_regno = reg_renumber[regnos[0]];
4757*38fd1498Szrj }
4758*38fd1498Szrj *first_hard_regno = hard_regno;
4759*38fd1498Szrj return cost;
4760*38fd1498Szrj }
4761*38fd1498Szrj
4762*38fd1498Szrj /* Return TRUE if spilling pseudo-registers whose numbers are in array
4763*38fd1498Szrj REGNOS is better than spilling pseudo-registers with numbers in
4764*38fd1498Szrj OTHER_REGNOS for reload with given IN and OUT for INSN. The
4765*38fd1498Szrj function used by the reload pass to make better register spilling
4766*38fd1498Szrj decisions. */
4767*38fd1498Szrj bool
ira_better_spill_reload_regno_p(int * regnos,int * other_regnos,rtx in,rtx out,rtx_insn * insn)4768*38fd1498Szrj ira_better_spill_reload_regno_p (int *regnos, int *other_regnos,
4769*38fd1498Szrj rtx in, rtx out, rtx_insn *insn)
4770*38fd1498Szrj {
4771*38fd1498Szrj int cost, other_cost;
4772*38fd1498Szrj int length, other_length;
4773*38fd1498Szrj int nrefs, other_nrefs;
4774*38fd1498Szrj int call_used_count, other_call_used_count;
4775*38fd1498Szrj int hard_regno, other_hard_regno;
4776*38fd1498Szrj
4777*38fd1498Szrj cost = calculate_spill_cost (regnos, in, out, insn,
4778*38fd1498Szrj &length, &nrefs, &call_used_count, &hard_regno);
4779*38fd1498Szrj other_cost = calculate_spill_cost (other_regnos, in, out, insn,
4780*38fd1498Szrj &other_length, &other_nrefs,
4781*38fd1498Szrj &other_call_used_count,
4782*38fd1498Szrj &other_hard_regno);
4783*38fd1498Szrj if (nrefs == 0 && other_nrefs != 0)
4784*38fd1498Szrj return true;
4785*38fd1498Szrj if (nrefs != 0 && other_nrefs == 0)
4786*38fd1498Szrj return false;
4787*38fd1498Szrj if (cost != other_cost)
4788*38fd1498Szrj return cost < other_cost;
4789*38fd1498Szrj if (length != other_length)
4790*38fd1498Szrj return length > other_length;
4791*38fd1498Szrj #ifdef REG_ALLOC_ORDER
4792*38fd1498Szrj if (hard_regno >= 0 && other_hard_regno >= 0)
4793*38fd1498Szrj return (inv_reg_alloc_order[hard_regno]
4794*38fd1498Szrj < inv_reg_alloc_order[other_hard_regno]);
4795*38fd1498Szrj #else
4796*38fd1498Szrj if (call_used_count != other_call_used_count)
4797*38fd1498Szrj return call_used_count > other_call_used_count;
4798*38fd1498Szrj #endif
4799*38fd1498Szrj return false;
4800*38fd1498Szrj }
4801*38fd1498Szrj
4802*38fd1498Szrj
4803*38fd1498Szrj
4804*38fd1498Szrj /* Allocate and initialize data necessary for assign_hard_reg. */
4805*38fd1498Szrj void
ira_initiate_assign(void)4806*38fd1498Szrj ira_initiate_assign (void)
4807*38fd1498Szrj {
4808*38fd1498Szrj sorted_allocnos
4809*38fd1498Szrj = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4810*38fd1498Szrj * ira_allocnos_num);
4811*38fd1498Szrj consideration_allocno_bitmap = ira_allocate_bitmap ();
4812*38fd1498Szrj initiate_cost_update ();
4813*38fd1498Szrj allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4814*38fd1498Szrj sorted_copies = (ira_copy_t *) ira_allocate (ira_copies_num
4815*38fd1498Szrj * sizeof (ira_copy_t));
4816*38fd1498Szrj }
4817*38fd1498Szrj
4818*38fd1498Szrj /* Deallocate data used by assign_hard_reg. */
4819*38fd1498Szrj void
ira_finish_assign(void)4820*38fd1498Szrj ira_finish_assign (void)
4821*38fd1498Szrj {
4822*38fd1498Szrj ira_free (sorted_allocnos);
4823*38fd1498Szrj ira_free_bitmap (consideration_allocno_bitmap);
4824*38fd1498Szrj finish_cost_update ();
4825*38fd1498Szrj ira_free (allocno_priorities);
4826*38fd1498Szrj ira_free (sorted_copies);
4827*38fd1498Szrj }
4828*38fd1498Szrj
4829*38fd1498Szrj
4830*38fd1498Szrj
4831*38fd1498Szrj /* Entry function doing color-based register allocation. */
4832*38fd1498Szrj static void
color(void)4833*38fd1498Szrj color (void)
4834*38fd1498Szrj {
4835*38fd1498Szrj allocno_stack_vec.create (ira_allocnos_num);
4836*38fd1498Szrj memset (allocated_hardreg_p, 0, sizeof (allocated_hardreg_p));
4837*38fd1498Szrj ira_initiate_assign ();
4838*38fd1498Szrj do_coloring ();
4839*38fd1498Szrj ira_finish_assign ();
4840*38fd1498Szrj allocno_stack_vec.release ();
4841*38fd1498Szrj move_spill_restore ();
4842*38fd1498Szrj }
4843*38fd1498Szrj
4844*38fd1498Szrj
4845*38fd1498Szrj
4846*38fd1498Szrj /* This page contains a simple register allocator without usage of
4847*38fd1498Szrj allocno conflicts. This is used for fast allocation for -O0. */
4848*38fd1498Szrj
4849*38fd1498Szrj /* Do register allocation by not using allocno conflicts. It uses
4850*38fd1498Szrj only allocno live ranges. The algorithm is close to Chow's
4851*38fd1498Szrj priority coloring. */
4852*38fd1498Szrj static void
fast_allocation(void)4853*38fd1498Szrj fast_allocation (void)
4854*38fd1498Szrj {
4855*38fd1498Szrj int i, j, k, num, class_size, hard_regno;
4856*38fd1498Szrj #ifdef STACK_REGS
4857*38fd1498Szrj bool no_stack_reg_p;
4858*38fd1498Szrj #endif
4859*38fd1498Szrj enum reg_class aclass;
4860*38fd1498Szrj machine_mode mode;
4861*38fd1498Szrj ira_allocno_t a;
4862*38fd1498Szrj ira_allocno_iterator ai;
4863*38fd1498Szrj live_range_t r;
4864*38fd1498Szrj HARD_REG_SET conflict_hard_regs, *used_hard_regs;
4865*38fd1498Szrj
4866*38fd1498Szrj sorted_allocnos = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
4867*38fd1498Szrj * ira_allocnos_num);
4868*38fd1498Szrj num = 0;
4869*38fd1498Szrj FOR_EACH_ALLOCNO (a, ai)
4870*38fd1498Szrj sorted_allocnos[num++] = a;
4871*38fd1498Szrj allocno_priorities = (int *) ira_allocate (sizeof (int) * ira_allocnos_num);
4872*38fd1498Szrj setup_allocno_priorities (sorted_allocnos, num);
4873*38fd1498Szrj used_hard_regs = (HARD_REG_SET *) ira_allocate (sizeof (HARD_REG_SET)
4874*38fd1498Szrj * ira_max_point);
4875*38fd1498Szrj for (i = 0; i < ira_max_point; i++)
4876*38fd1498Szrj CLEAR_HARD_REG_SET (used_hard_regs[i]);
4877*38fd1498Szrj qsort (sorted_allocnos, num, sizeof (ira_allocno_t),
4878*38fd1498Szrj allocno_priority_compare_func);
4879*38fd1498Szrj for (i = 0; i < num; i++)
4880*38fd1498Szrj {
4881*38fd1498Szrj int nr, l;
4882*38fd1498Szrj
4883*38fd1498Szrj a = sorted_allocnos[i];
4884*38fd1498Szrj nr = ALLOCNO_NUM_OBJECTS (a);
4885*38fd1498Szrj CLEAR_HARD_REG_SET (conflict_hard_regs);
4886*38fd1498Szrj for (l = 0; l < nr; l++)
4887*38fd1498Szrj {
4888*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, l);
4889*38fd1498Szrj IOR_HARD_REG_SET (conflict_hard_regs,
4890*38fd1498Szrj OBJECT_CONFLICT_HARD_REGS (obj));
4891*38fd1498Szrj for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4892*38fd1498Szrj for (j = r->start; j <= r->finish; j++)
4893*38fd1498Szrj IOR_HARD_REG_SET (conflict_hard_regs, used_hard_regs[j]);
4894*38fd1498Szrj }
4895*38fd1498Szrj aclass = ALLOCNO_CLASS (a);
4896*38fd1498Szrj ALLOCNO_ASSIGNED_P (a) = true;
4897*38fd1498Szrj ALLOCNO_HARD_REGNO (a) = -1;
4898*38fd1498Szrj if (hard_reg_set_subset_p (reg_class_contents[aclass],
4899*38fd1498Szrj conflict_hard_regs))
4900*38fd1498Szrj continue;
4901*38fd1498Szrj mode = ALLOCNO_MODE (a);
4902*38fd1498Szrj #ifdef STACK_REGS
4903*38fd1498Szrj no_stack_reg_p = ALLOCNO_NO_STACK_REG_P (a);
4904*38fd1498Szrj #endif
4905*38fd1498Szrj class_size = ira_class_hard_regs_num[aclass];
4906*38fd1498Szrj for (j = 0; j < class_size; j++)
4907*38fd1498Szrj {
4908*38fd1498Szrj hard_regno = ira_class_hard_regs[aclass][j];
4909*38fd1498Szrj #ifdef STACK_REGS
4910*38fd1498Szrj if (no_stack_reg_p && FIRST_STACK_REG <= hard_regno
4911*38fd1498Szrj && hard_regno <= LAST_STACK_REG)
4912*38fd1498Szrj continue;
4913*38fd1498Szrj #endif
4914*38fd1498Szrj if (ira_hard_reg_set_intersection_p (hard_regno, mode, conflict_hard_regs)
4915*38fd1498Szrj || (TEST_HARD_REG_BIT
4916*38fd1498Szrj (ira_prohibited_class_mode_regs[aclass][mode], hard_regno)))
4917*38fd1498Szrj continue;
4918*38fd1498Szrj ALLOCNO_HARD_REGNO (a) = hard_regno;
4919*38fd1498Szrj for (l = 0; l < nr; l++)
4920*38fd1498Szrj {
4921*38fd1498Szrj ira_object_t obj = ALLOCNO_OBJECT (a, l);
4922*38fd1498Szrj for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
4923*38fd1498Szrj for (k = r->start; k <= r->finish; k++)
4924*38fd1498Szrj IOR_HARD_REG_SET (used_hard_regs[k],
4925*38fd1498Szrj ira_reg_mode_hard_regset[hard_regno][mode]);
4926*38fd1498Szrj }
4927*38fd1498Szrj break;
4928*38fd1498Szrj }
4929*38fd1498Szrj }
4930*38fd1498Szrj ira_free (sorted_allocnos);
4931*38fd1498Szrj ira_free (used_hard_regs);
4932*38fd1498Szrj ira_free (allocno_priorities);
4933*38fd1498Szrj if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
4934*38fd1498Szrj ira_print_disposition (ira_dump_file);
4935*38fd1498Szrj }
4936*38fd1498Szrj
4937*38fd1498Szrj
4938*38fd1498Szrj
4939*38fd1498Szrj /* Entry function doing coloring. */
4940*38fd1498Szrj void
ira_color(void)4941*38fd1498Szrj ira_color (void)
4942*38fd1498Szrj {
4943*38fd1498Szrj ira_allocno_t a;
4944*38fd1498Szrj ira_allocno_iterator ai;
4945*38fd1498Szrj
4946*38fd1498Szrj /* Setup updated costs. */
4947*38fd1498Szrj FOR_EACH_ALLOCNO (a, ai)
4948*38fd1498Szrj {
4949*38fd1498Szrj ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
4950*38fd1498Szrj ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
4951*38fd1498Szrj }
4952*38fd1498Szrj if (ira_conflicts_p)
4953*38fd1498Szrj color ();
4954*38fd1498Szrj else
4955*38fd1498Szrj fast_allocation ();
4956*38fd1498Szrj }
4957