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