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