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