]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/ira-build.c
* ira-int.h (struct ira_object): New.
[thirdparty/gcc.git] / gcc / ira-build.c
CommitLineData
47dd2e78 1/* Building internal representation for IRA.
1180227d 2 Copyright (C) 2006, 2007, 2008, 2009, 2010
47dd2e78 3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
5
6This file is part of GCC.
7
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
10Software Foundation; either version 3, or (at your option) any later
11version.
12
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
17
18You should have received a copy of the GNU General Public License
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
21
22#include "config.h"
23#include "system.h"
24#include "coretypes.h"
25#include "tm.h"
26#include "rtl.h"
27#include "tm_p.h"
28#include "target.h"
29#include "regs.h"
30#include "flags.h"
31#include "hard-reg-set.h"
32#include "basic-block.h"
33#include "insn-config.h"
34#include "recog.h"
0b205f4c 35#include "diagnostic-core.h"
47dd2e78 36#include "toplev.h"
37#include "params.h"
38#include "df.h"
39#include "output.h"
40#include "reload.h"
41#include "sparseset.h"
42#include "ira-int.h"
06f9d6ef 43#include "emit-rtl.h" /* FIXME: Can go away once crtl is moved to rtl.h. */
47dd2e78 44
45static ira_copy_t find_allocno_copy (ira_allocno_t, ira_allocno_t, rtx,
46 ira_loop_tree_node_t);
47
48/* The root of the loop tree corresponding to the all function. */
49ira_loop_tree_node_t ira_loop_tree_root;
50
51/* Height of the loop tree. */
52int ira_loop_tree_height;
53
54/* All nodes representing basic blocks are referred through the
55 following array. We can not use basic block member `aux' for this
56 because it is used for insertion of insns on edges. */
57ira_loop_tree_node_t ira_bb_nodes;
58
59/* All nodes representing loops are referred through the following
60 array. */
61ira_loop_tree_node_t ira_loop_nodes;
62
48e1416a 63/* Map regno -> allocnos with given regno (see comments for
47dd2e78 64 allocno member `next_regno_allocno'). */
65ira_allocno_t *ira_regno_allocno_map;
66
67/* Array of references to all allocnos. The order number of the
68 allocno corresponds to the index in the array. Removed allocnos
69 have NULL element value. */
70ira_allocno_t *ira_allocnos;
71
72/* Sizes of the previous array. */
73int ira_allocnos_num;
74
ae9587ed 75/* Count of conflict record structures we've created, used when creating
76 a new conflict id. */
77int ira_objects_num;
78
79/* Map a conflict id to its conflict record. */
80ira_object_t *ira_object_id_map;
47dd2e78 81
82/* Array of references to all copies. The order number of the copy
83 corresponds to the index in the array. Removed copies have NULL
84 element value. */
85ira_copy_t *ira_copies;
86
87/* Size of the previous array. */
88int ira_copies_num;
89
90\f
91
92/* LAST_BASIC_BLOCK before generating additional insns because of live
93 range splitting. Emitting insns on a critical edge creates a new
94 basic block. */
95static int last_basic_block_before_change;
96
97/* The following function allocates the loop tree nodes. If LOOPS_P
98 is FALSE, the nodes corresponding to the loops (except the root
99 which corresponds the all function) will be not allocated but nodes
100 will still be allocated for basic blocks. */
101static void
102create_loop_tree_nodes (bool loops_p)
103{
104 unsigned int i, j;
105 int max_regno;
106 bool skip_p;
107 edge_iterator ei;
108 edge e;
109 VEC (edge, heap) *edges;
110 loop_p loop;
111
112 ira_bb_nodes
113 = ((struct ira_loop_tree_node *)
114 ira_allocate (sizeof (struct ira_loop_tree_node) * last_basic_block));
115 last_basic_block_before_change = last_basic_block;
116 for (i = 0; i < (unsigned int) last_basic_block; i++)
117 {
118 ira_bb_nodes[i].regno_allocno_map = NULL;
119 memset (ira_bb_nodes[i].reg_pressure, 0,
120 sizeof (ira_bb_nodes[i].reg_pressure));
2bae4acc 121 ira_bb_nodes[i].all_allocnos = NULL;
47dd2e78 122 ira_bb_nodes[i].modified_regnos = NULL;
123 ira_bb_nodes[i].border_allocnos = NULL;
124 ira_bb_nodes[i].local_copies = NULL;
125 }
126 ira_loop_nodes = ((struct ira_loop_tree_node *)
127 ira_allocate (sizeof (struct ira_loop_tree_node)
128 * VEC_length (loop_p, ira_loops.larray)));
129 max_regno = max_reg_num ();
130 for (i = 0; VEC_iterate (loop_p, ira_loops.larray, i, loop); i++)
131 {
132 if (loop != ira_loops.tree_root)
133 {
134 ira_loop_nodes[i].regno_allocno_map = NULL;
135 if (! loops_p)
136 continue;
137 skip_p = false;
138 FOR_EACH_EDGE (e, ei, loop->header->preds)
139 if (e->src != loop->latch
140 && (e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
141 {
142 skip_p = true;
143 break;
144 }
145 if (skip_p)
146 continue;
147 edges = get_loop_exit_edges (loop);
148 for (j = 0; VEC_iterate (edge, edges, j, e); j++)
149 if ((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
150 {
151 skip_p = true;
152 break;
153 }
154 VEC_free (edge, heap, edges);
155 if (skip_p)
156 continue;
157 }
158 ira_loop_nodes[i].regno_allocno_map
159 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t) * max_regno);
160 memset (ira_loop_nodes[i].regno_allocno_map, 0,
161 sizeof (ira_allocno_t) * max_regno);
162 memset (ira_loop_nodes[i].reg_pressure, 0,
163 sizeof (ira_loop_nodes[i].reg_pressure));
2bae4acc 164 ira_loop_nodes[i].all_allocnos = ira_allocate_bitmap ();
47dd2e78 165 ira_loop_nodes[i].modified_regnos = ira_allocate_bitmap ();
166 ira_loop_nodes[i].border_allocnos = ira_allocate_bitmap ();
167 ira_loop_nodes[i].local_copies = ira_allocate_bitmap ();
168 }
169}
170
171/* The function returns TRUE if there are more one allocation
172 region. */
173static bool
174more_one_region_p (void)
175{
176 unsigned int i;
177 loop_p loop;
178
179 for (i = 0; VEC_iterate (loop_p, ira_loops.larray, i, loop); i++)
180 if (ira_loop_nodes[i].regno_allocno_map != NULL
181 && ira_loop_tree_root != &ira_loop_nodes[i])
182 return true;
183 return false;
184}
185
186/* Free the loop tree node of a loop. */
187static void
188finish_loop_tree_node (ira_loop_tree_node_t loop)
189{
190 if (loop->regno_allocno_map != NULL)
191 {
192 ira_assert (loop->bb == NULL);
193 ira_free_bitmap (loop->local_copies);
194 ira_free_bitmap (loop->border_allocnos);
195 ira_free_bitmap (loop->modified_regnos);
2bae4acc 196 ira_free_bitmap (loop->all_allocnos);
47dd2e78 197 ira_free (loop->regno_allocno_map);
198 loop->regno_allocno_map = NULL;
199 }
200}
201
202/* Free the loop tree nodes. */
203static void
204finish_loop_tree_nodes (void)
205{
206 unsigned int i;
207 loop_p loop;
208
209 for (i = 0; VEC_iterate (loop_p, ira_loops.larray, i, loop); i++)
210 finish_loop_tree_node (&ira_loop_nodes[i]);
211 ira_free (ira_loop_nodes);
212 for (i = 0; i < (unsigned int) last_basic_block_before_change; i++)
213 {
214 if (ira_bb_nodes[i].local_copies != NULL)
215 ira_free_bitmap (ira_bb_nodes[i].local_copies);
216 if (ira_bb_nodes[i].border_allocnos != NULL)
217 ira_free_bitmap (ira_bb_nodes[i].border_allocnos);
218 if (ira_bb_nodes[i].modified_regnos != NULL)
219 ira_free_bitmap (ira_bb_nodes[i].modified_regnos);
2bae4acc 220 if (ira_bb_nodes[i].all_allocnos != NULL)
221 ira_free_bitmap (ira_bb_nodes[i].all_allocnos);
47dd2e78 222 if (ira_bb_nodes[i].regno_allocno_map != NULL)
223 ira_free (ira_bb_nodes[i].regno_allocno_map);
224 }
225 ira_free (ira_bb_nodes);
226}
227
228\f
229
230/* The following recursive function adds LOOP to the loop tree
231 hierarchy. LOOP is added only once. */
232static void
233add_loop_to_tree (struct loop *loop)
234{
235 struct loop *parent;
236 ira_loop_tree_node_t loop_node, parent_node;
237
238 /* We can not use loop node access macros here because of potential
239 checking and because the nodes are not initialized enough
240 yet. */
241 if (loop_outer (loop) != NULL)
242 add_loop_to_tree (loop_outer (loop));
243 if (ira_loop_nodes[loop->num].regno_allocno_map != NULL
244 && ira_loop_nodes[loop->num].children == NULL)
245 {
246 /* We have not added loop node to the tree yet. */
247 loop_node = &ira_loop_nodes[loop->num];
248 loop_node->loop = loop;
249 loop_node->bb = NULL;
250 for (parent = loop_outer (loop);
251 parent != NULL;
252 parent = loop_outer (parent))
253 if (ira_loop_nodes[parent->num].regno_allocno_map != NULL)
254 break;
255 if (parent == NULL)
256 {
257 loop_node->next = NULL;
258 loop_node->subloop_next = NULL;
259 loop_node->parent = NULL;
260 }
261 else
262 {
263 parent_node = &ira_loop_nodes[parent->num];
264 loop_node->next = parent_node->children;
265 parent_node->children = loop_node;
266 loop_node->subloop_next = parent_node->subloops;
267 parent_node->subloops = loop_node;
268 loop_node->parent = parent_node;
269 }
270 }
271}
272
273/* The following recursive function sets up levels of nodes of the
274 tree given its root LOOP_NODE. The enumeration starts with LEVEL.
275 The function returns maximal value of level in the tree + 1. */
276static int
277setup_loop_tree_level (ira_loop_tree_node_t loop_node, int level)
278{
279 int height, max_height;
280 ira_loop_tree_node_t subloop_node;
281
282 ira_assert (loop_node->bb == NULL);
283 loop_node->level = level;
284 max_height = level + 1;
285 for (subloop_node = loop_node->subloops;
286 subloop_node != NULL;
287 subloop_node = subloop_node->subloop_next)
288 {
289 ira_assert (subloop_node->bb == NULL);
290 height = setup_loop_tree_level (subloop_node, level + 1);
291 if (height > max_height)
292 max_height = height;
293 }
294 return max_height;
295}
296
297/* Create the loop tree. The algorithm is designed to provide correct
298 order of loops (they are ordered by their last loop BB) and basic
299 blocks in the chain formed by member next. */
300static void
301form_loop_tree (void)
302{
303 unsigned int i;
304 basic_block bb;
305 struct loop *parent;
306 ira_loop_tree_node_t bb_node, loop_node;
307 loop_p loop;
308
309 /* We can not use loop/bb node access macros because of potential
310 checking and because the nodes are not initialized enough
311 yet. */
312 for (i = 0; VEC_iterate (loop_p, ira_loops.larray, i, loop); i++)
313 if (ira_loop_nodes[i].regno_allocno_map != NULL)
314 {
315 ira_loop_nodes[i].children = NULL;
316 ira_loop_nodes[i].subloops = NULL;
317 }
7e03a244 318 FOR_EACH_BB (bb)
47dd2e78 319 {
320 bb_node = &ira_bb_nodes[bb->index];
321 bb_node->bb = bb;
322 bb_node->loop = NULL;
323 bb_node->subloops = NULL;
324 bb_node->children = NULL;
325 bb_node->subloop_next = NULL;
326 bb_node->next = NULL;
327 for (parent = bb->loop_father;
328 parent != NULL;
329 parent = loop_outer (parent))
330 if (ira_loop_nodes[parent->num].regno_allocno_map != NULL)
331 break;
332 add_loop_to_tree (parent);
333 loop_node = &ira_loop_nodes[parent->num];
334 bb_node->next = loop_node->children;
335 bb_node->parent = loop_node;
336 loop_node->children = bb_node;
337 }
338 ira_loop_tree_root = IRA_LOOP_NODE_BY_INDEX (ira_loops.tree_root->num);
339 ira_loop_tree_height = setup_loop_tree_level (ira_loop_tree_root, 0);
340 ira_assert (ira_loop_tree_root->regno_allocno_map != NULL);
341}
342
343\f
344
345/* Rebuild IRA_REGNO_ALLOCNO_MAP and REGNO_ALLOCNO_MAPs of the loop
346 tree nodes. */
347static void
348rebuild_regno_allocno_maps (void)
349{
350 unsigned int l;
351 int max_regno, regno;
352 ira_allocno_t a;
353 ira_loop_tree_node_t loop_tree_node;
354 loop_p loop;
355 ira_allocno_iterator ai;
356
357 max_regno = max_reg_num ();
358 for (l = 0; VEC_iterate (loop_p, ira_loops.larray, l, loop); l++)
359 if (ira_loop_nodes[l].regno_allocno_map != NULL)
360 {
361 ira_free (ira_loop_nodes[l].regno_allocno_map);
362 ira_loop_nodes[l].regno_allocno_map
363 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
364 * max_regno);
365 memset (ira_loop_nodes[l].regno_allocno_map, 0,
366 sizeof (ira_allocno_t) * max_regno);
367 }
368 ira_free (ira_regno_allocno_map);
369 ira_regno_allocno_map
370 = (ira_allocno_t *) ira_allocate (max_regno * sizeof (ira_allocno_t));
371 memset (ira_regno_allocno_map, 0, max_regno * sizeof (ira_allocno_t));
372 FOR_EACH_ALLOCNO (a, ai)
373 {
374 if (ALLOCNO_CAP_MEMBER (a) != NULL)
375 /* Caps are not in the regno allocno maps. */
376 continue;
377 regno = ALLOCNO_REGNO (a);
378 loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
379 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = ira_regno_allocno_map[regno];
380 ira_regno_allocno_map[regno] = a;
381 if (loop_tree_node->regno_allocno_map[regno] == NULL)
382 /* Remember that we can create temporary allocnos to break
383 cycles in register shuffle. */
384 loop_tree_node->regno_allocno_map[regno] = a;
385 }
386}
47dd2e78 387\f
388
ae9587ed 389/* Pools for allocnos, allocno live ranges and objects. */
390static alloc_pool allocno_pool, live_range_pool, object_pool;
47dd2e78 391
392/* Vec containing references to all created allocnos. It is a
393 container of array allocnos. */
394static VEC(ira_allocno_t,heap) *allocno_vec;
395
ae9587ed 396/* Vec containing references to all created ira_objects. It is a
397 container of ira_object_id_map. */
398static VEC(ira_object_t,heap) *ira_object_id_map_vec;
47dd2e78 399
400/* Initialize data concerning allocnos. */
401static void
402initiate_allocnos (void)
403{
fbff82f4 404 live_range_pool
405 = create_alloc_pool ("live ranges",
406 sizeof (struct live_range), 100);
47dd2e78 407 allocno_pool
408 = create_alloc_pool ("allocnos", sizeof (struct ira_allocno), 100);
ae9587ed 409 object_pool
410 = create_alloc_pool ("objects", sizeof (struct ira_object), 100);
47dd2e78 411 allocno_vec = VEC_alloc (ira_allocno_t, heap, max_reg_num () * 2);
412 ira_allocnos = NULL;
413 ira_allocnos_num = 0;
ae9587ed 414 ira_objects_num = 0;
415 ira_object_id_map_vec
416 = VEC_alloc (ira_object_t, heap, max_reg_num () * 2);
417 ira_object_id_map = NULL;
47dd2e78 418 ira_regno_allocno_map
419 = (ira_allocno_t *) ira_allocate (max_reg_num () * sizeof (ira_allocno_t));
420 memset (ira_regno_allocno_map, 0, max_reg_num () * sizeof (ira_allocno_t));
421}
422
ae9587ed 423/* Create and return an object corresponding to a new allocno A. */
424static ira_object_t
425ira_create_object (ira_allocno_t a)
426{
427 enum reg_class cover_class = ALLOCNO_COVER_CLASS (a);
428 ira_object_t obj = (ira_object_t) pool_alloc (object_pool);
429
430 OBJECT_ALLOCNO (obj) = a;
431 OBJECT_CONFLICT_ID (obj) = ira_objects_num;
432 OBJECT_CONFLICT_VEC_P (obj) = false;
433 OBJECT_CONFLICT_ARRAY (obj) = NULL;
434 OBJECT_NUM_CONFLICTS (obj) = 0;
435 COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj), ira_no_alloc_regs);
436 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj), ira_no_alloc_regs);
437 IOR_COMPL_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
438 reg_class_contents[cover_class]);
439 IOR_COMPL_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
440 reg_class_contents[cover_class]);
441 OBJECT_MIN (obj) = INT_MAX;
442 OBJECT_MAX (obj) = -1;
443
444 VEC_safe_push (ira_object_t, heap, ira_object_id_map_vec, obj);
445 ira_object_id_map
446 = VEC_address (ira_object_t, ira_object_id_map_vec);
447 ira_objects_num = VEC_length (ira_object_t, ira_object_id_map_vec);
448 return obj;
449}
450
47dd2e78 451/* Create and return the allocno corresponding to REGNO in
452 LOOP_TREE_NODE. Add the allocno to the list of allocnos with the
453 same regno if CAP_P is FALSE. */
454ira_allocno_t
455ira_create_allocno (int regno, bool cap_p, ira_loop_tree_node_t loop_tree_node)
456{
457 ira_allocno_t a;
458
459 a = (ira_allocno_t) pool_alloc (allocno_pool);
460 ALLOCNO_REGNO (a) = regno;
461 ALLOCNO_LOOP_TREE_NODE (a) = loop_tree_node;
462 if (! cap_p)
463 {
464 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = ira_regno_allocno_map[regno];
465 ira_regno_allocno_map[regno] = a;
466 if (loop_tree_node->regno_allocno_map[regno] == NULL)
467 /* Remember that we can create temporary allocnos to break
468 cycles in register shuffle on region borders (see
469 ira-emit.c). */
470 loop_tree_node->regno_allocno_map[regno] = a;
471 }
472 ALLOCNO_CAP (a) = NULL;
473 ALLOCNO_CAP_MEMBER (a) = NULL;
474 ALLOCNO_NUM (a) = ira_allocnos_num;
2bae4acc 475 bitmap_set_bit (loop_tree_node->all_allocnos, ALLOCNO_NUM (a));
47dd2e78 476 ALLOCNO_NREFS (a) = 0;
1e2504ec 477 ALLOCNO_FREQ (a) = 0;
47dd2e78 478 ALLOCNO_HARD_REGNO (a) = -1;
479 ALLOCNO_CALL_FREQ (a) = 0;
480 ALLOCNO_CALLS_CROSSED_NUM (a) = 0;
481#ifdef STACK_REGS
482 ALLOCNO_NO_STACK_REG_P (a) = false;
483 ALLOCNO_TOTAL_NO_STACK_REG_P (a) = false;
484#endif
485 ALLOCNO_MEM_OPTIMIZED_DEST (a) = NULL;
486 ALLOCNO_MEM_OPTIMIZED_DEST_P (a) = false;
487 ALLOCNO_SOMEWHERE_RENAMED_P (a) = false;
488 ALLOCNO_CHILD_RENAMED_P (a) = false;
489 ALLOCNO_DONT_REASSIGN_P (a) = false;
68d4bdfb 490 ALLOCNO_BAD_SPILL_P (a) = false;
47dd2e78 491 ALLOCNO_IN_GRAPH_P (a) = false;
492 ALLOCNO_ASSIGNED_P (a) = false;
493 ALLOCNO_MAY_BE_SPILLED_P (a) = false;
494 ALLOCNO_SPLAY_REMOVED_P (a) = false;
47dd2e78 495 ALLOCNO_MODE (a) = (regno < 0 ? VOIDmode : PSEUDO_REGNO_MODE (regno));
496 ALLOCNO_COPIES (a) = NULL;
497 ALLOCNO_HARD_REG_COSTS (a) = NULL;
498 ALLOCNO_CONFLICT_HARD_REG_COSTS (a) = NULL;
499 ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
500 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
951e5184 501 ALLOCNO_LEFT_CONFLICTS_SIZE (a) = -1;
47dd2e78 502 ALLOCNO_COVER_CLASS (a) = NO_REGS;
df07a54c 503 ALLOCNO_UPDATED_COVER_CLASS_COST (a) = 0;
47dd2e78 504 ALLOCNO_COVER_CLASS_COST (a) = 0;
505 ALLOCNO_MEMORY_COST (a) = 0;
506 ALLOCNO_UPDATED_MEMORY_COST (a) = 0;
507 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) = 0;
508 ALLOCNO_NEXT_BUCKET_ALLOCNO (a) = NULL;
509 ALLOCNO_PREV_BUCKET_ALLOCNO (a) = NULL;
510 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = a;
511 ALLOCNO_NEXT_COALESCED_ALLOCNO (a) = a;
512 ALLOCNO_LIVE_RANGES (a) = NULL;
ae9587ed 513
47dd2e78 514 VEC_safe_push (ira_allocno_t, heap, allocno_vec, a);
515 ira_allocnos = VEC_address (ira_allocno_t, allocno_vec);
516 ira_allocnos_num = VEC_length (ira_allocno_t, allocno_vec);
47dd2e78 517 return a;
518}
519
520/* Set up cover class for A and update its conflict hard registers. */
521void
522ira_set_allocno_cover_class (ira_allocno_t a, enum reg_class cover_class)
523{
524 ALLOCNO_COVER_CLASS (a) = cover_class;
ae9587ed 525}
526
527/* Allocate an object for allocno A and set ALLOCNO_OBJECT. */
528void
529ira_create_allocno_object (ira_allocno_t a)
530{
531 ALLOCNO_OBJECT (a) = ira_create_object (a);
532}
533
534/* For each allocno, create the corresponding ALLOCNO_OBJECT structure. */
535static void
536create_allocno_objects (void)
537{
538 ira_allocno_t a;
539 ira_allocno_iterator ai;
540
541 FOR_EACH_ALLOCNO (a, ai)
542 ira_create_allocno_object (a);
47dd2e78 543}
544
b4f5e198 545/* Merge hard register conflicts from allocno FROM into allocno TO. If
546 TOTAL_ONLY is true, we ignore ALLOCNO_CONFLICT_HARD_REGS. */
547static void
548merge_hard_reg_conflicts (ira_allocno_t from, ira_allocno_t to,
549 bool total_only)
550{
ae9587ed 551 ira_object_t from_obj = ALLOCNO_OBJECT (from);
552 ira_object_t to_obj = ALLOCNO_OBJECT (to);
b4f5e198 553 if (!total_only)
ae9587ed 554 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (to_obj),
555 OBJECT_CONFLICT_HARD_REGS (from_obj));
556 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (to_obj),
557 OBJECT_TOTAL_CONFLICT_HARD_REGS (from_obj));
b4f5e198 558#ifdef STACK_REGS
559 if (!total_only && ALLOCNO_NO_STACK_REG_P (from))
560 ALLOCNO_NO_STACK_REG_P (to) = true;
561 if (ALLOCNO_TOTAL_NO_STACK_REG_P (from))
562 ALLOCNO_TOTAL_NO_STACK_REG_P (to) = true;
563#endif
564}
565
ae9587ed 566/* Return TRUE if a conflict vector with NUM elements is more
567 profitable than a conflict bit vector for OBJ. */
47dd2e78 568bool
ae9587ed 569ira_conflict_vector_profitable_p (ira_object_t obj, int num)
47dd2e78 570{
571 int nw;
ae9587ed 572 int max = OBJECT_MAX (obj);
573 int min = OBJECT_MIN (obj);
47dd2e78 574
ae9587ed 575 if (max < min)
576 /* We prefer a bit vector in such case because it does not result
577 in allocation. */
47dd2e78 578 return false;
579
ae9587ed 580 nw = (max - min + IRA_INT_BITS) / IRA_INT_BITS;
581 return (2 * sizeof (ira_object_t) * (num + 1)
47dd2e78 582 < 3 * nw * sizeof (IRA_INT_TYPE));
583}
584
ae9587ed 585/* Allocates and initialize the conflict vector of OBJ for NUM
586 conflicting objects. */
47dd2e78 587void
ae9587ed 588ira_allocate_conflict_vec (ira_object_t obj, int num)
47dd2e78 589{
590 int size;
ae9587ed 591 ira_object_t *vec;
47dd2e78 592
ae9587ed 593 ira_assert (OBJECT_CONFLICT_ARRAY (obj) == NULL);
47dd2e78 594 num++; /* for NULL end marker */
ae9587ed 595 size = sizeof (ira_object_t) * num;
596 OBJECT_CONFLICT_ARRAY (obj) = ira_allocate (size);
597 vec = (ira_object_t *) OBJECT_CONFLICT_ARRAY (obj);
47dd2e78 598 vec[0] = NULL;
ae9587ed 599 OBJECT_NUM_CONFLICTS (obj) = 0;
600 OBJECT_CONFLICT_ARRAY_SIZE (obj) = size;
601 OBJECT_CONFLICT_VEC_P (obj) = true;
47dd2e78 602}
603
ae9587ed 604/* Allocate and initialize the conflict bit vector of OBJ. */
47dd2e78 605static void
ae9587ed 606allocate_conflict_bit_vec (ira_object_t obj)
47dd2e78 607{
608 unsigned int size;
609
ae9587ed 610 ira_assert (OBJECT_CONFLICT_ARRAY (obj) == NULL);
611 size = ((OBJECT_MAX (obj) - OBJECT_MIN (obj) + IRA_INT_BITS)
47dd2e78 612 / IRA_INT_BITS * sizeof (IRA_INT_TYPE));
ae9587ed 613 OBJECT_CONFLICT_ARRAY (obj) = ira_allocate (size);
614 memset (OBJECT_CONFLICT_ARRAY (obj), 0, size);
615 OBJECT_CONFLICT_ARRAY_SIZE (obj) = size;
616 OBJECT_CONFLICT_VEC_P (obj) = false;
47dd2e78 617}
618
619/* Allocate and initialize the conflict vector or conflict bit vector
620 of A for NUM conflicting allocnos whatever is more profitable. */
621void
ae9587ed 622ira_allocate_object_conflicts (ira_object_t a, int num)
47dd2e78 623{
624 if (ira_conflict_vector_profitable_p (a, num))
ae9587ed 625 ira_allocate_conflict_vec (a, num);
47dd2e78 626 else
ae9587ed 627 allocate_conflict_bit_vec (a);
47dd2e78 628}
629
ae9587ed 630/* Add OBJ2 to the conflicts of OBJ1. */
47dd2e78 631static void
ae9587ed 632add_to_conflicts (ira_object_t obj1, ira_object_t obj2)
47dd2e78 633{
634 int num;
635 unsigned int size;
636
ae9587ed 637 if (OBJECT_CONFLICT_VEC_P (obj1))
47dd2e78 638 {
ae9587ed 639 ira_object_t *vec = OBJECT_CONFLICT_VEC (obj1);
640 int curr_num = OBJECT_NUM_CONFLICTS (obj1);
641 num = curr_num + 2;
642 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) < num * sizeof (ira_object_t))
47dd2e78 643 {
ae9587ed 644 ira_object_t *newvec;
47dd2e78 645 size = (3 * num / 2 + 1) * sizeof (ira_allocno_t);
ae9587ed 646 newvec = (ira_object_t *) ira_allocate (size);
647 memcpy (newvec, vec, curr_num * sizeof (ira_object_t));
648 ira_free (vec);
649 vec = newvec;
650 OBJECT_CONFLICT_ARRAY (obj1) = vec;
651 OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
47dd2e78 652 }
ae9587ed 653 vec[num - 2] = obj2;
47dd2e78 654 vec[num - 1] = NULL;
ae9587ed 655 OBJECT_NUM_CONFLICTS (obj1)++;
47dd2e78 656 }
657 else
658 {
659 int nw, added_head_nw, id;
ae9587ed 660 IRA_INT_TYPE *vec = OBJECT_CONFLICT_BITVEC (obj1);
47dd2e78 661
ae9587ed 662 id = OBJECT_CONFLICT_ID (obj2);
663 if (OBJECT_MIN (obj1) > id)
47dd2e78 664 {
665 /* Expand head of the bit vector. */
ae9587ed 666 added_head_nw = (OBJECT_MIN (obj1) - id - 1) / IRA_INT_BITS + 1;
667 nw = (OBJECT_MAX (obj1) - OBJECT_MIN (obj1)) / IRA_INT_BITS + 1;
47dd2e78 668 size = (nw + added_head_nw) * sizeof (IRA_INT_TYPE);
ae9587ed 669 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) >= size)
47dd2e78 670 {
671 memmove ((char *) vec + added_head_nw * sizeof (IRA_INT_TYPE),
672 vec, nw * sizeof (IRA_INT_TYPE));
673 memset (vec, 0, added_head_nw * sizeof (IRA_INT_TYPE));
674 }
675 else
676 {
677 size
678 = (3 * (nw + added_head_nw) / 2 + 1) * sizeof (IRA_INT_TYPE);
679 vec = (IRA_INT_TYPE *) ira_allocate (size);
680 memcpy ((char *) vec + added_head_nw * sizeof (IRA_INT_TYPE),
ae9587ed 681 OBJECT_CONFLICT_ARRAY (obj1), nw * sizeof (IRA_INT_TYPE));
47dd2e78 682 memset (vec, 0, added_head_nw * sizeof (IRA_INT_TYPE));
683 memset ((char *) vec
684 + (nw + added_head_nw) * sizeof (IRA_INT_TYPE),
685 0, size - (nw + added_head_nw) * sizeof (IRA_INT_TYPE));
ae9587ed 686 ira_free (OBJECT_CONFLICT_ARRAY (obj1));
687 OBJECT_CONFLICT_ARRAY (obj1) = vec;
688 OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
47dd2e78 689 }
ae9587ed 690 OBJECT_MIN (obj1) -= added_head_nw * IRA_INT_BITS;
47dd2e78 691 }
ae9587ed 692 else if (OBJECT_MAX (obj1) < id)
47dd2e78 693 {
ae9587ed 694 nw = (id - OBJECT_MIN (obj1)) / IRA_INT_BITS + 1;
47dd2e78 695 size = nw * sizeof (IRA_INT_TYPE);
ae9587ed 696 if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) < size)
47dd2e78 697 {
698 /* Expand tail of the bit vector. */
699 size = (3 * nw / 2 + 1) * sizeof (IRA_INT_TYPE);
700 vec = (IRA_INT_TYPE *) ira_allocate (size);
ae9587ed 701 memcpy (vec, OBJECT_CONFLICT_ARRAY (obj1), OBJECT_CONFLICT_ARRAY_SIZE (obj1));
702 memset ((char *) vec + OBJECT_CONFLICT_ARRAY_SIZE (obj1),
703 0, size - OBJECT_CONFLICT_ARRAY_SIZE (obj1));
704 ira_free (OBJECT_CONFLICT_ARRAY (obj1));
705 OBJECT_CONFLICT_ARRAY (obj1) = vec;
706 OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
47dd2e78 707 }
ae9587ed 708 OBJECT_MAX (obj1) = id;
47dd2e78 709 }
ae9587ed 710 SET_MINMAX_SET_BIT (vec, id, OBJECT_MIN (obj1), OBJECT_MAX (obj1));
47dd2e78 711 }
712}
713
ae9587ed 714/* Add OBJ1 to the conflicts of OBJ2 and vice versa. */
715static void
716ira_add_conflict (ira_object_t obj1, ira_object_t obj2)
47dd2e78 717{
ae9587ed 718 add_to_conflicts (obj1, obj2);
719 add_to_conflicts (obj2, obj1);
47dd2e78 720}
721
ae9587ed 722/* Clear all conflicts of OBJ. */
47dd2e78 723static void
ae9587ed 724clear_conflicts (ira_object_t obj)
47dd2e78 725{
ae9587ed 726 if (OBJECT_CONFLICT_VEC_P (obj))
47dd2e78 727 {
ae9587ed 728 OBJECT_NUM_CONFLICTS (obj) = 0;
729 OBJECT_CONFLICT_VEC (obj)[0] = NULL;
47dd2e78 730 }
ae9587ed 731 else if (OBJECT_CONFLICT_ARRAY_SIZE (obj) != 0)
47dd2e78 732 {
733 int nw;
734
ae9587ed 735 nw = (OBJECT_MAX (obj) - OBJECT_MIN (obj)) / IRA_INT_BITS + 1;
736 memset (OBJECT_CONFLICT_BITVEC (obj), 0, nw * sizeof (IRA_INT_TYPE));
47dd2e78 737 }
738}
739
740/* The array used to find duplications in conflict vectors of
741 allocnos. */
ae9587ed 742static int *conflict_check;
47dd2e78 743
744/* The value used to mark allocation presence in conflict vector of
745 the current allocno. */
ae9587ed 746static int curr_conflict_check_tick;
47dd2e78 747
ae9587ed 748/* Remove duplications in conflict vector of OBJ. */
47dd2e78 749static void
ae9587ed 750compress_conflict_vec (ira_object_t obj)
47dd2e78 751{
ae9587ed 752 ira_object_t *vec, conflict_obj;
47dd2e78 753 int i, j;
754
ae9587ed 755 ira_assert (OBJECT_CONFLICT_VEC_P (obj));
756 vec = OBJECT_CONFLICT_VEC (obj);
757 curr_conflict_check_tick++;
758 for (i = j = 0; (conflict_obj = vec[i]) != NULL; i++)
47dd2e78 759 {
ae9587ed 760 int id = OBJECT_CONFLICT_ID (conflict_obj);
761 if (conflict_check[id] != curr_conflict_check_tick)
47dd2e78 762 {
ae9587ed 763 conflict_check[id] = curr_conflict_check_tick;
764 vec[j++] = conflict_obj;
47dd2e78 765 }
766 }
ae9587ed 767 OBJECT_NUM_CONFLICTS (obj) = j;
47dd2e78 768 vec[j] = NULL;
769}
770
771/* Remove duplications in conflict vectors of all allocnos. */
772static void
773compress_conflict_vecs (void)
774{
775 ira_allocno_t a;
776 ira_allocno_iterator ai;
777
ae9587ed 778 conflict_check = (int *) ira_allocate (sizeof (int) * ira_objects_num);
779 memset (conflict_check, 0, sizeof (int) * ira_objects_num);
780 curr_conflict_check_tick = 0;
47dd2e78 781 FOR_EACH_ALLOCNO (a, ai)
ae9587ed 782 {
783 ira_object_t obj = ALLOCNO_OBJECT (a);
784 if (OBJECT_CONFLICT_VEC_P (obj))
785 compress_conflict_vec (obj);
786 }
787 ira_free (conflict_check);
47dd2e78 788}
789
790/* This recursive function outputs allocno A and if it is a cap the
791 function outputs its members. */
792void
793ira_print_expanded_allocno (ira_allocno_t a)
794{
795 basic_block bb;
796
797 fprintf (ira_dump_file, " a%d(r%d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
798 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
799 fprintf (ira_dump_file, ",b%d", bb->index);
800 else
801 fprintf (ira_dump_file, ",l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
802 if (ALLOCNO_CAP_MEMBER (a) != NULL)
803 {
804 fprintf (ira_dump_file, ":");
805 ira_print_expanded_allocno (ALLOCNO_CAP_MEMBER (a));
806 }
807 fprintf (ira_dump_file, ")");
808}
809
810/* Create and return the cap representing allocno A in the
811 parent loop. */
812static ira_allocno_t
813create_cap_allocno (ira_allocno_t a)
814{
815 ira_allocno_t cap;
816 ira_loop_tree_node_t parent;
817 enum reg_class cover_class;
818
819 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (a) == a
820 && ALLOCNO_NEXT_COALESCED_ALLOCNO (a) == a);
821 parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
822 cap = ira_create_allocno (ALLOCNO_REGNO (a), true, parent);
823 ALLOCNO_MODE (cap) = ALLOCNO_MODE (a);
824 cover_class = ALLOCNO_COVER_CLASS (a);
825 ira_set_allocno_cover_class (cap, cover_class);
ae9587ed 826 ira_create_allocno_object (cap);
47dd2e78 827 ALLOCNO_AVAILABLE_REGS_NUM (cap) = ALLOCNO_AVAILABLE_REGS_NUM (a);
828 ALLOCNO_CAP_MEMBER (cap) = a;
47dd2e78 829 ALLOCNO_CAP (a) = cap;
830 ALLOCNO_COVER_CLASS_COST (cap) = ALLOCNO_COVER_CLASS_COST (a);
831 ALLOCNO_MEMORY_COST (cap) = ALLOCNO_MEMORY_COST (a);
47dd2e78 832 ira_allocate_and_copy_costs
833 (&ALLOCNO_HARD_REG_COSTS (cap), cover_class, ALLOCNO_HARD_REG_COSTS (a));
834 ira_allocate_and_copy_costs
835 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (cap), cover_class,
836 ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
68d4bdfb 837 ALLOCNO_BAD_SPILL_P (cap) = ALLOCNO_BAD_SPILL_P (a);
47dd2e78 838 ALLOCNO_NREFS (cap) = ALLOCNO_NREFS (a);
839 ALLOCNO_FREQ (cap) = ALLOCNO_FREQ (a);
840 ALLOCNO_CALL_FREQ (cap) = ALLOCNO_CALL_FREQ (a);
b4f5e198 841 merge_hard_reg_conflicts (a, cap, false);
47dd2e78 842 ALLOCNO_CALLS_CROSSED_NUM (cap) = ALLOCNO_CALLS_CROSSED_NUM (a);
47dd2e78 843 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
844 {
845 fprintf (ira_dump_file, " Creating cap ");
846 ira_print_expanded_allocno (cap);
847 fprintf (ira_dump_file, "\n");
848 }
849 return cap;
850}
851
852/* Create and return allocno live range with given attributes. */
fbff82f4 853live_range_t
47dd2e78 854ira_create_allocno_live_range (ira_allocno_t a, int start, int finish,
fbff82f4 855 live_range_t next)
47dd2e78 856{
fbff82f4 857 live_range_t p;
47dd2e78 858
fbff82f4 859 p = (live_range_t) pool_alloc (live_range_pool);
47dd2e78 860 p->allocno = a;
861 p->start = start;
862 p->finish = finish;
863 p->next = next;
864 return p;
865}
866
867/* Copy allocno live range R and return the result. */
fbff82f4 868static live_range_t
869copy_allocno_live_range (live_range_t r)
47dd2e78 870{
fbff82f4 871 live_range_t p;
47dd2e78 872
fbff82f4 873 p = (live_range_t) pool_alloc (live_range_pool);
47dd2e78 874 *p = *r;
875 return p;
876}
877
878/* Copy allocno live range list given by its head R and return the
879 result. */
fbff82f4 880live_range_t
881ira_copy_allocno_live_range_list (live_range_t r)
47dd2e78 882{
fbff82f4 883 live_range_t p, first, last;
47dd2e78 884
885 if (r == NULL)
886 return NULL;
887 for (first = last = NULL; r != NULL; r = r->next)
888 {
889 p = copy_allocno_live_range (r);
890 if (first == NULL)
891 first = p;
892 else
893 last->next = p;
894 last = p;
895 }
896 return first;
897}
898
69f8e080 899/* Merge ranges R1 and R2 and returns the result. The function
900 maintains the order of ranges and tries to minimize number of the
901 result ranges. */
fbff82f4 902live_range_t
903ira_merge_allocno_live_ranges (live_range_t r1, live_range_t r2)
69f8e080 904{
fbff82f4 905 live_range_t first, last, temp;
69f8e080 906
907 if (r1 == NULL)
908 return r2;
909 if (r2 == NULL)
910 return r1;
911 for (first = last = NULL; r1 != NULL && r2 != NULL;)
912 {
913 if (r1->start < r2->start)
914 {
915 temp = r1;
916 r1 = r2;
917 r2 = temp;
918 }
919 if (r1->start <= r2->finish + 1)
920 {
921 /* Intersected ranges: merge r1 and r2 into r1. */
922 r1->start = r2->start;
923 if (r1->finish < r2->finish)
924 r1->finish = r2->finish;
925 temp = r2;
926 r2 = r2->next;
927 ira_finish_allocno_live_range (temp);
928 if (r2 == NULL)
929 {
930 /* To try to merge with subsequent ranges in r1. */
931 r2 = r1->next;
932 r1->next = NULL;
933 }
934 }
935 else
936 {
937 /* Add r1 to the result. */
938 if (first == NULL)
939 first = last = r1;
940 else
941 {
942 last->next = r1;
943 last = r1;
944 }
945 r1 = r1->next;
946 if (r1 == NULL)
947 {
948 /* To try to merge with subsequent ranges in r2. */
949 r1 = r2->next;
950 r2->next = NULL;
951 }
952 }
953 }
954 if (r1 != NULL)
955 {
956 if (first == NULL)
957 first = r1;
958 else
959 last->next = r1;
960 ira_assert (r1->next == NULL);
961 }
962 else if (r2 != NULL)
963 {
964 if (first == NULL)
965 first = r2;
966 else
967 last->next = r2;
968 ira_assert (r2->next == NULL);
969 }
970 else
971 {
972 ira_assert (last->next == NULL);
973 }
974 return first;
975}
976
977/* Return TRUE if live ranges R1 and R2 intersect. */
978bool
fbff82f4 979ira_allocno_live_ranges_intersect_p (live_range_t r1, live_range_t r2)
69f8e080 980{
981 /* Remember the live ranges are always kept ordered. */
982 while (r1 != NULL && r2 != NULL)
983 {
984 if (r1->start > r2->finish)
985 r1 = r1->next;
986 else if (r2->start > r1->finish)
987 r2 = r2->next;
988 else
989 return true;
990 }
991 return false;
992}
993
47dd2e78 994/* Free allocno live range R. */
995void
fbff82f4 996ira_finish_allocno_live_range (live_range_t r)
47dd2e78 997{
fbff82f4 998 pool_free (live_range_pool, r);
47dd2e78 999}
1000
69f8e080 1001/* Free list of allocno live ranges starting with R. */
1002void
fbff82f4 1003ira_finish_allocno_live_range_list (live_range_t r)
69f8e080 1004{
fbff82f4 1005 live_range_t next_r;
69f8e080 1006
1007 for (; r != NULL; r = next_r)
1008 {
1009 next_r = r->next;
1010 ira_finish_allocno_live_range (r);
1011 }
1012}
1013
47dd2e78 1014/* Free updated register costs of allocno A. */
1015void
1016ira_free_allocno_updated_costs (ira_allocno_t a)
1017{
1018 enum reg_class cover_class;
1019
1020 cover_class = ALLOCNO_COVER_CLASS (a);
1021 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
1022 ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), cover_class);
1023 ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
1024 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
1025 ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1026 cover_class);
1027 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
1028}
1029
1030/* Free the memory allocated for allocno A. */
1031static void
1032finish_allocno (ira_allocno_t a)
1033{
47dd2e78 1034 enum reg_class cover_class = ALLOCNO_COVER_CLASS (a);
ae9587ed 1035 ira_object_t obj = ALLOCNO_OBJECT (a);
47dd2e78 1036
1037 ira_allocnos[ALLOCNO_NUM (a)] = NULL;
47dd2e78 1038 if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
1039 ira_free_cost_vector (ALLOCNO_HARD_REG_COSTS (a), cover_class);
1040 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL)
1041 ira_free_cost_vector (ALLOCNO_CONFLICT_HARD_REG_COSTS (a), cover_class);
1042 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
1043 ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), cover_class);
1044 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
1045 ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1046 cover_class);
69f8e080 1047 ira_finish_allocno_live_range_list (ALLOCNO_LIVE_RANGES (a));
47dd2e78 1048 pool_free (allocno_pool, a);
ae9587ed 1049
1050 ira_object_id_map[OBJECT_CONFLICT_ID (obj)] = NULL;
1051 if (OBJECT_CONFLICT_ARRAY (obj) != NULL)
1052 ira_free (OBJECT_CONFLICT_ARRAY (obj));
1053 pool_free (object_pool, obj);
47dd2e78 1054}
1055
1056/* Free the memory allocated for all allocnos. */
1057static void
1058finish_allocnos (void)
1059{
1060 ira_allocno_t a;
1061 ira_allocno_iterator ai;
1062
1063 FOR_EACH_ALLOCNO (a, ai)
1064 finish_allocno (a);
1065 ira_free (ira_regno_allocno_map);
ae9587ed 1066 VEC_free (ira_object_t, heap, ira_object_id_map_vec);
47dd2e78 1067 VEC_free (ira_allocno_t, heap, allocno_vec);
1068 free_alloc_pool (allocno_pool);
ae9587ed 1069 free_alloc_pool (object_pool);
fbff82f4 1070 free_alloc_pool (live_range_pool);
47dd2e78 1071}
1072
1073\f
1074
1075/* Pools for copies. */
1076static alloc_pool copy_pool;
1077
1078/* Vec containing references to all created copies. It is a
1079 container of array ira_copies. */
1080static VEC(ira_copy_t,heap) *copy_vec;
1081
1082/* The function initializes data concerning allocno copies. */
1083static void
1084initiate_copies (void)
1085{
1086 copy_pool
1087 = create_alloc_pool ("copies", sizeof (struct ira_allocno_copy), 100);
1088 copy_vec = VEC_alloc (ira_copy_t, heap, get_max_uid ());
1089 ira_copies = NULL;
1090 ira_copies_num = 0;
1091}
1092
1093/* Return copy connecting A1 and A2 and originated from INSN of
1094 LOOP_TREE_NODE if any. */
1095static ira_copy_t
1096find_allocno_copy (ira_allocno_t a1, ira_allocno_t a2, rtx insn,
1097 ira_loop_tree_node_t loop_tree_node)
1098{
1099 ira_copy_t cp, next_cp;
1100 ira_allocno_t another_a;
1101
1102 for (cp = ALLOCNO_COPIES (a1); cp != NULL; cp = next_cp)
1103 {
1104 if (cp->first == a1)
1105 {
1106 next_cp = cp->next_first_allocno_copy;
1107 another_a = cp->second;
1108 }
1109 else if (cp->second == a1)
1110 {
1111 next_cp = cp->next_second_allocno_copy;
1112 another_a = cp->first;
1113 }
1114 else
1115 gcc_unreachable ();
1116 if (another_a == a2 && cp->insn == insn
1117 && cp->loop_tree_node == loop_tree_node)
1118 return cp;
1119 }
1120 return NULL;
1121}
1122
1123/* Create and return copy with given attributes LOOP_TREE_NODE, FIRST,
b7c06809 1124 SECOND, FREQ, CONSTRAINT_P, and INSN. */
47dd2e78 1125ira_copy_t
b7c06809 1126ira_create_copy (ira_allocno_t first, ira_allocno_t second, int freq,
1127 bool constraint_p, rtx insn,
47dd2e78 1128 ira_loop_tree_node_t loop_tree_node)
1129{
1130 ira_copy_t cp;
1131
1132 cp = (ira_copy_t) pool_alloc (copy_pool);
1133 cp->num = ira_copies_num;
1134 cp->first = first;
1135 cp->second = second;
1136 cp->freq = freq;
b7c06809 1137 cp->constraint_p = constraint_p;
47dd2e78 1138 cp->insn = insn;
1139 cp->loop_tree_node = loop_tree_node;
1140 VEC_safe_push (ira_copy_t, heap, copy_vec, cp);
1141 ira_copies = VEC_address (ira_copy_t, copy_vec);
1142 ira_copies_num = VEC_length (ira_copy_t, copy_vec);
1143 return cp;
1144}
1145
1146/* Attach a copy CP to allocnos involved into the copy. */
1147void
1148ira_add_allocno_copy_to_list (ira_copy_t cp)
1149{
1150 ira_allocno_t first = cp->first, second = cp->second;
1151
1152 cp->prev_first_allocno_copy = NULL;
1153 cp->prev_second_allocno_copy = NULL;
1154 cp->next_first_allocno_copy = ALLOCNO_COPIES (first);
1155 if (cp->next_first_allocno_copy != NULL)
1156 {
1157 if (cp->next_first_allocno_copy->first == first)
1158 cp->next_first_allocno_copy->prev_first_allocno_copy = cp;
1159 else
1160 cp->next_first_allocno_copy->prev_second_allocno_copy = cp;
1161 }
1162 cp->next_second_allocno_copy = ALLOCNO_COPIES (second);
1163 if (cp->next_second_allocno_copy != NULL)
1164 {
1165 if (cp->next_second_allocno_copy->second == second)
1166 cp->next_second_allocno_copy->prev_second_allocno_copy = cp;
1167 else
1168 cp->next_second_allocno_copy->prev_first_allocno_copy = cp;
1169 }
1170 ALLOCNO_COPIES (first) = cp;
1171 ALLOCNO_COPIES (second) = cp;
1172}
1173
1174/* Detach a copy CP from allocnos involved into the copy. */
1175void
1176ira_remove_allocno_copy_from_list (ira_copy_t cp)
1177{
1178 ira_allocno_t first = cp->first, second = cp->second;
1179 ira_copy_t prev, next;
1180
1181 next = cp->next_first_allocno_copy;
1182 prev = cp->prev_first_allocno_copy;
1183 if (prev == NULL)
1184 ALLOCNO_COPIES (first) = next;
1185 else if (prev->first == first)
1186 prev->next_first_allocno_copy = next;
1187 else
1188 prev->next_second_allocno_copy = next;
1189 if (next != NULL)
1190 {
1191 if (next->first == first)
1192 next->prev_first_allocno_copy = prev;
1193 else
1194 next->prev_second_allocno_copy = prev;
1195 }
1196 cp->prev_first_allocno_copy = cp->next_first_allocno_copy = NULL;
1197
1198 next = cp->next_second_allocno_copy;
1199 prev = cp->prev_second_allocno_copy;
1200 if (prev == NULL)
1201 ALLOCNO_COPIES (second) = next;
1202 else if (prev->second == second)
1203 prev->next_second_allocno_copy = next;
1204 else
1205 prev->next_first_allocno_copy = next;
1206 if (next != NULL)
1207 {
1208 if (next->second == second)
1209 next->prev_second_allocno_copy = prev;
1210 else
1211 next->prev_first_allocno_copy = prev;
1212 }
1213 cp->prev_second_allocno_copy = cp->next_second_allocno_copy = NULL;
1214}
1215
1216/* Make a copy CP a canonical copy where number of the
1217 first allocno is less than the second one. */
1218void
1219ira_swap_allocno_copy_ends_if_necessary (ira_copy_t cp)
1220{
1221 ira_allocno_t temp;
1222 ira_copy_t temp_cp;
1223
1224 if (ALLOCNO_NUM (cp->first) <= ALLOCNO_NUM (cp->second))
1225 return;
1226
1227 temp = cp->first;
1228 cp->first = cp->second;
1229 cp->second = temp;
1230
1231 temp_cp = cp->prev_first_allocno_copy;
1232 cp->prev_first_allocno_copy = cp->prev_second_allocno_copy;
1233 cp->prev_second_allocno_copy = temp_cp;
1234
1235 temp_cp = cp->next_first_allocno_copy;
1236 cp->next_first_allocno_copy = cp->next_second_allocno_copy;
1237 cp->next_second_allocno_copy = temp_cp;
1238}
1239
1240/* Create (or update frequency if the copy already exists) and return
1241 the copy of allocnos FIRST and SECOND with frequency FREQ
1242 corresponding to move insn INSN (if any) and originated from
1243 LOOP_TREE_NODE. */
1244ira_copy_t
1245ira_add_allocno_copy (ira_allocno_t first, ira_allocno_t second, int freq,
b7c06809 1246 bool constraint_p, rtx insn,
1247 ira_loop_tree_node_t loop_tree_node)
47dd2e78 1248{
1249 ira_copy_t cp;
1250
1251 if ((cp = find_allocno_copy (first, second, insn, loop_tree_node)) != NULL)
1252 {
1253 cp->freq += freq;
1254 return cp;
1255 }
b7c06809 1256 cp = ira_create_copy (first, second, freq, constraint_p, insn,
1257 loop_tree_node);
47dd2e78 1258 ira_assert (first != NULL && second != NULL);
1259 ira_add_allocno_copy_to_list (cp);
1260 ira_swap_allocno_copy_ends_if_necessary (cp);
1261 return cp;
1262}
1263
55c858c5 1264/* Print info about copy CP into file F. */
1265static void
1266print_copy (FILE *f, ira_copy_t cp)
1267{
b7c06809 1268 fprintf (f, " cp%d:a%d(r%d)<->a%d(r%d)@%d:%s\n", cp->num,
55c858c5 1269 ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
b7c06809 1270 ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second), cp->freq,
1271 cp->insn != NULL
1272 ? "move" : cp->constraint_p ? "constraint" : "shuffle");
55c858c5 1273}
1274
1275/* Print info about copy CP into stderr. */
1276void
1277ira_debug_copy (ira_copy_t cp)
1278{
1279 print_copy (stderr, cp);
1280}
1281
1282/* Print info about all copies into file F. */
1283static void
1284print_copies (FILE *f)
1285{
1286 ira_copy_t cp;
1287 ira_copy_iterator ci;
1288
1289 FOR_EACH_COPY (cp, ci)
1290 print_copy (f, cp);
1291}
1292
1293/* Print info about all copies into stderr. */
1294void
1295ira_debug_copies (void)
1296{
1297 print_copies (stderr);
1298}
1299
47dd2e78 1300/* Print info about copies involving allocno A into file F. */
1301static void
1302print_allocno_copies (FILE *f, ira_allocno_t a)
1303{
1304 ira_allocno_t another_a;
1305 ira_copy_t cp, next_cp;
1306
1307 fprintf (f, " a%d(r%d):", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
1308 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1309 {
1310 if (cp->first == a)
1311 {
1312 next_cp = cp->next_first_allocno_copy;
1313 another_a = cp->second;
1314 }
1315 else if (cp->second == a)
1316 {
1317 next_cp = cp->next_second_allocno_copy;
1318 another_a = cp->first;
1319 }
1320 else
1321 gcc_unreachable ();
1322 fprintf (f, " cp%d:a%d(r%d)@%d", cp->num,
1323 ALLOCNO_NUM (another_a), ALLOCNO_REGNO (another_a), cp->freq);
1324 }
1325 fprintf (f, "\n");
1326}
1327
1328/* Print info about copies involving allocno A into stderr. */
1329void
1330ira_debug_allocno_copies (ira_allocno_t a)
1331{
1332 print_allocno_copies (stderr, a);
1333}
1334
1335/* The function frees memory allocated for copy CP. */
1336static void
1337finish_copy (ira_copy_t cp)
1338{
1339 pool_free (copy_pool, cp);
1340}
1341
1342
1343/* Free memory allocated for all copies. */
1344static void
1345finish_copies (void)
1346{
1347 ira_copy_t cp;
1348 ira_copy_iterator ci;
1349
1350 FOR_EACH_COPY (cp, ci)
1351 finish_copy (cp);
1352 VEC_free (ira_copy_t, heap, copy_vec);
1353 free_alloc_pool (copy_pool);
1354}
1355
1356\f
1357
1358/* Pools for cost vectors. It is defined only for cover classes. */
1359static alloc_pool cost_vector_pool[N_REG_CLASSES];
1360
1361/* The function initiates work with hard register cost vectors. It
1362 creates allocation pool for each cover class. */
1363static void
1364initiate_cost_vectors (void)
1365{
1366 int i;
1367 enum reg_class cover_class;
1368
1369 for (i = 0; i < ira_reg_class_cover_size; i++)
1370 {
1371 cover_class = ira_reg_class_cover[i];
1372 cost_vector_pool[cover_class]
1373 = create_alloc_pool ("cost vectors",
1374 sizeof (int)
1375 * ira_class_hard_regs_num[cover_class],
1376 100);
1377 }
1378}
1379
1380/* Allocate and return a cost vector VEC for COVER_CLASS. */
1381int *
1382ira_allocate_cost_vector (enum reg_class cover_class)
1383{
1384 return (int *) pool_alloc (cost_vector_pool[cover_class]);
1385}
1386
1387/* Free a cost vector VEC for COVER_CLASS. */
1388void
1389ira_free_cost_vector (int *vec, enum reg_class cover_class)
1390{
1391 ira_assert (vec != NULL);
1392 pool_free (cost_vector_pool[cover_class], vec);
1393}
1394
1395/* Finish work with hard register cost vectors. Release allocation
1396 pool for each cover class. */
1397static void
1398finish_cost_vectors (void)
1399{
1400 int i;
1401 enum reg_class cover_class;
1402
1403 for (i = 0; i < ira_reg_class_cover_size; i++)
1404 {
1405 cover_class = ira_reg_class_cover[i];
1406 free_alloc_pool (cost_vector_pool[cover_class]);
1407 }
1408}
1409
1410\f
1411
1412/* The current loop tree node and its regno allocno map. */
1413ira_loop_tree_node_t ira_curr_loop_tree_node;
1414ira_allocno_t *ira_curr_regno_allocno_map;
1415
1416/* This recursive function traverses loop tree with root LOOP_NODE
1417 calling non-null functions PREORDER_FUNC and POSTORDER_FUNC
1418 correspondingly in preorder and postorder. The function sets up
1419 IRA_CURR_LOOP_TREE_NODE and IRA_CURR_REGNO_ALLOCNO_MAP. If BB_P,
1420 basic block nodes of LOOP_NODE is also processed (before its
1421 subloop nodes). */
1422void
1423ira_traverse_loop_tree (bool bb_p, ira_loop_tree_node_t loop_node,
1424 void (*preorder_func) (ira_loop_tree_node_t),
1425 void (*postorder_func) (ira_loop_tree_node_t))
1426{
1427 ira_loop_tree_node_t subloop_node;
1428
1429 ira_assert (loop_node->bb == NULL);
1430 ira_curr_loop_tree_node = loop_node;
1431 ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
1432
1433 if (preorder_func != NULL)
1434 (*preorder_func) (loop_node);
48e1416a 1435
47dd2e78 1436 if (bb_p)
1437 for (subloop_node = loop_node->children;
1438 subloop_node != NULL;
1439 subloop_node = subloop_node->next)
1440 if (subloop_node->bb != NULL)
1441 {
1442 if (preorder_func != NULL)
1443 (*preorder_func) (subloop_node);
48e1416a 1444
47dd2e78 1445 if (postorder_func != NULL)
1446 (*postorder_func) (subloop_node);
1447 }
48e1416a 1448
47dd2e78 1449 for (subloop_node = loop_node->subloops;
1450 subloop_node != NULL;
1451 subloop_node = subloop_node->subloop_next)
1452 {
1453 ira_assert (subloop_node->bb == NULL);
1454 ira_traverse_loop_tree (bb_p, subloop_node,
1455 preorder_func, postorder_func);
1456 }
1457
1458 ira_curr_loop_tree_node = loop_node;
1459 ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
1460
1461 if (postorder_func != NULL)
1462 (*postorder_func) (loop_node);
1463}
1464
1465\f
1466
1467/* The basic block currently being processed. */
1468static basic_block curr_bb;
1469
1470/* This recursive function creates allocnos corresponding to
1471 pseudo-registers containing in X. True OUTPUT_P means that X is
1472 a lvalue. */
1473static void
1474create_insn_allocnos (rtx x, bool output_p)
1475{
1476 int i, j;
1477 const char *fmt;
1478 enum rtx_code code = GET_CODE (x);
1479
1480 if (code == REG)
1481 {
1482 int regno;
1483
1484 if ((regno = REGNO (x)) >= FIRST_PSEUDO_REGISTER)
1485 {
1486 ira_allocno_t a;
1487
1488 if ((a = ira_curr_regno_allocno_map[regno]) == NULL)
1489 a = ira_create_allocno (regno, false, ira_curr_loop_tree_node);
48e1416a 1490
47dd2e78 1491 ALLOCNO_NREFS (a)++;
1492 ALLOCNO_FREQ (a) += REG_FREQ_FROM_BB (curr_bb);
47dd2e78 1493 if (output_p)
1494 bitmap_set_bit (ira_curr_loop_tree_node->modified_regnos, regno);
1495 }
1496 return;
1497 }
1498 else if (code == SET)
1499 {
1500 create_insn_allocnos (SET_DEST (x), true);
1501 create_insn_allocnos (SET_SRC (x), false);
1502 return;
1503 }
1504 else if (code == CLOBBER)
1505 {
1506 create_insn_allocnos (XEXP (x, 0), true);
1507 return;
1508 }
1509 else if (code == MEM)
1510 {
1511 create_insn_allocnos (XEXP (x, 0), false);
1512 return;
1513 }
48e1416a 1514 else if (code == PRE_DEC || code == POST_DEC || code == PRE_INC ||
47dd2e78 1515 code == POST_INC || code == POST_MODIFY || code == PRE_MODIFY)
1516 {
1517 create_insn_allocnos (XEXP (x, 0), true);
1518 create_insn_allocnos (XEXP (x, 0), false);
1519 return;
1520 }
1521
1522 fmt = GET_RTX_FORMAT (code);
1523 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1524 {
1525 if (fmt[i] == 'e')
1526 create_insn_allocnos (XEXP (x, i), output_p);
1527 else if (fmt[i] == 'E')
1528 for (j = 0; j < XVECLEN (x, i); j++)
1529 create_insn_allocnos (XVECEXP (x, i, j), output_p);
1530 }
1531}
1532
1533/* Create allocnos corresponding to pseudo-registers living in the
1534 basic block represented by the corresponding loop tree node
1535 BB_NODE. */
1536static void
1537create_bb_allocnos (ira_loop_tree_node_t bb_node)
1538{
1539 basic_block bb;
1540 rtx insn;
1541 unsigned int i;
1542 bitmap_iterator bi;
1543
1544 curr_bb = bb = bb_node->bb;
1545 ira_assert (bb != NULL);
7e03a244 1546 FOR_BB_INSNS_REVERSE (bb, insn)
9845d120 1547 if (NONDEBUG_INSN_P (insn))
47dd2e78 1548 create_insn_allocnos (PATTERN (insn), false);
1549 /* It might be a allocno living through from one subloop to
1550 another. */
2dd81ece 1551 EXECUTE_IF_SET_IN_REG_SET (DF_LR_IN (bb), FIRST_PSEUDO_REGISTER, i, bi)
47dd2e78 1552 if (ira_curr_regno_allocno_map[i] == NULL)
1553 ira_create_allocno (i, false, ira_curr_loop_tree_node);
1554}
1555
1556/* Create allocnos corresponding to pseudo-registers living on edge E
1557 (a loop entry or exit). Also mark the allocnos as living on the
1558 loop border. */
1559static void
1560create_loop_allocnos (edge e)
1561{
1562 unsigned int i;
1563 bitmap live_in_regs, border_allocnos;
1564 bitmap_iterator bi;
1565 ira_loop_tree_node_t parent;
1566
2dd81ece 1567 live_in_regs = DF_LR_IN (e->dest);
47dd2e78 1568 border_allocnos = ira_curr_loop_tree_node->border_allocnos;
2dd81ece 1569 EXECUTE_IF_SET_IN_REG_SET (DF_LR_OUT (e->src),
47dd2e78 1570 FIRST_PSEUDO_REGISTER, i, bi)
1571 if (bitmap_bit_p (live_in_regs, i))
1572 {
1573 if (ira_curr_regno_allocno_map[i] == NULL)
1574 {
1575 /* The order of creations is important for right
1576 ira_regno_allocno_map. */
1577 if ((parent = ira_curr_loop_tree_node->parent) != NULL
1578 && parent->regno_allocno_map[i] == NULL)
1579 ira_create_allocno (i, false, parent);
1580 ira_create_allocno (i, false, ira_curr_loop_tree_node);
1581 }
1582 bitmap_set_bit (border_allocnos,
1583 ALLOCNO_NUM (ira_curr_regno_allocno_map[i]));
1584 }
1585}
1586
1587/* Create allocnos corresponding to pseudo-registers living in loop
1588 represented by the corresponding loop tree node LOOP_NODE. This
1589 function is called by ira_traverse_loop_tree. */
1590static void
1591create_loop_tree_node_allocnos (ira_loop_tree_node_t loop_node)
1592{
1593 if (loop_node->bb != NULL)
1594 create_bb_allocnos (loop_node);
1595 else if (loop_node != ira_loop_tree_root)
1596 {
1597 int i;
1598 edge_iterator ei;
1599 edge e;
1600 VEC (edge, heap) *edges;
1601
1602 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
1603 if (e->src != loop_node->loop->latch)
1604 create_loop_allocnos (e);
48e1416a 1605
47dd2e78 1606 edges = get_loop_exit_edges (loop_node->loop);
1607 for (i = 0; VEC_iterate (edge, edges, i, e); i++)
1608 create_loop_allocnos (e);
1609 VEC_free (edge, heap, edges);
1610 }
1611}
1612
1613/* Propagate information about allocnos modified inside the loop given
1614 by its LOOP_TREE_NODE to its parent. */
1615static void
1616propagate_modified_regnos (ira_loop_tree_node_t loop_tree_node)
1617{
1618 if (loop_tree_node == ira_loop_tree_root)
1619 return;
1620 ira_assert (loop_tree_node->bb == NULL);
1621 bitmap_ior_into (loop_tree_node->parent->modified_regnos,
1622 loop_tree_node->modified_regnos);
1623}
1624
1625/* Propagate new info about allocno A (see comments about accumulated
1626 info in allocno definition) to the corresponding allocno on upper
1627 loop tree level. So allocnos on upper levels accumulate
1628 information about the corresponding allocnos in nested regions.
1629 The new info means allocno info finally calculated in this
1630 file. */
1631static void
1632propagate_allocno_info (void)
1633{
1634 int i;
1635 ira_allocno_t a, parent_a;
1636 ira_loop_tree_node_t parent;
1637 enum reg_class cover_class;
1638
14792f4e 1639 if (flag_ira_region != IRA_REGION_ALL
1640 && flag_ira_region != IRA_REGION_MIXED)
47dd2e78 1641 return;
1642 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1643 for (a = ira_regno_allocno_map[i];
1644 a != NULL;
1645 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1646 if ((parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) != NULL
1647 && (parent_a = parent->regno_allocno_map[i]) != NULL
1648 /* There are no caps yet at this point. So use
1649 border_allocnos to find allocnos for the propagation. */
1650 && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE (a)->border_allocnos,
1651 ALLOCNO_NUM (a)))
1652 {
68d4bdfb 1653 if (! ALLOCNO_BAD_SPILL_P (a))
1654 ALLOCNO_BAD_SPILL_P (parent_a) = false;
47dd2e78 1655 ALLOCNO_NREFS (parent_a) += ALLOCNO_NREFS (a);
1656 ALLOCNO_FREQ (parent_a) += ALLOCNO_FREQ (a);
1657 ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
b4f5e198 1658 merge_hard_reg_conflicts (a, parent_a, true);
47dd2e78 1659 ALLOCNO_CALLS_CROSSED_NUM (parent_a)
1660 += ALLOCNO_CALLS_CROSSED_NUM (a);
1661 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
1662 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
1663 cover_class = ALLOCNO_COVER_CLASS (a);
1664 ira_assert (cover_class == ALLOCNO_COVER_CLASS (parent_a));
1665 ira_allocate_and_accumulate_costs
1666 (&ALLOCNO_HARD_REG_COSTS (parent_a), cover_class,
1667 ALLOCNO_HARD_REG_COSTS (a));
1668 ira_allocate_and_accumulate_costs
1669 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a),
1670 cover_class,
1671 ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
1672 ALLOCNO_COVER_CLASS_COST (parent_a)
1673 += ALLOCNO_COVER_CLASS_COST (a);
1674 ALLOCNO_MEMORY_COST (parent_a) += ALLOCNO_MEMORY_COST (a);
47dd2e78 1675 }
1676}
1677
1678/* Create allocnos corresponding to pseudo-registers in the current
1679 function. Traverse the loop tree for this. */
1680static void
1681create_allocnos (void)
1682{
1683 /* We need to process BB first to correctly link allocnos by member
1684 next_regno_allocno. */
1685 ira_traverse_loop_tree (true, ira_loop_tree_root,
1686 create_loop_tree_node_allocnos, NULL);
1687 if (optimize)
1688 ira_traverse_loop_tree (false, ira_loop_tree_root, NULL,
1689 propagate_modified_regnos);
1690}
1691
1692\f
1693
1694/* The page contains function to remove some regions from a separate
1695 register allocation. We remove regions whose separate allocation
1696 will hardly improve the result. As a result we speed up regional
1697 register allocation. */
1698
47dd2e78 1699/* The function changes allocno in range list given by R onto A. */
1700static void
fbff82f4 1701change_allocno_in_range_list (live_range_t r, ira_allocno_t a)
47dd2e78 1702{
1703 for (; r != NULL; r = r->next)
1704 r->allocno = a;
1705}
1706
b4f5e198 1707/* Move all live ranges associated with allocno FROM to allocno TO. */
1708static void
1709move_allocno_live_ranges (ira_allocno_t from, ira_allocno_t to)
1710{
fbff82f4 1711 live_range_t lr = ALLOCNO_LIVE_RANGES (from);
b4f5e198 1712
1713 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
1714 {
1715 fprintf (ira_dump_file,
1716 " Moving ranges of a%dr%d to a%dr%d: ",
1717 ALLOCNO_NUM (from), ALLOCNO_REGNO (from),
1718 ALLOCNO_NUM (to), ALLOCNO_REGNO (to));
1719 ira_print_live_range_list (ira_dump_file, lr);
1720 }
1721 change_allocno_in_range_list (lr, to);
1722 ALLOCNO_LIVE_RANGES (to)
1723 = ira_merge_allocno_live_ranges (lr, ALLOCNO_LIVE_RANGES (to));
1724 ALLOCNO_LIVE_RANGES (from) = NULL;
1725}
1726
1727/* Copy all live ranges associated with allocno FROM to allocno TO. */
1728static void
1729copy_allocno_live_ranges (ira_allocno_t from, ira_allocno_t to)
1730{
fbff82f4 1731 live_range_t lr = ALLOCNO_LIVE_RANGES (from);
b4f5e198 1732
1733 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
1734 {
1735 fprintf (ira_dump_file,
1736 " Copying ranges of a%dr%d to a%dr%d: ",
1737 ALLOCNO_NUM (from), ALLOCNO_REGNO (from),
1738 ALLOCNO_NUM (to), ALLOCNO_REGNO (to));
1739 ira_print_live_range_list (ira_dump_file, lr);
1740 }
1741 lr = ira_copy_allocno_live_range_list (lr);
1742 change_allocno_in_range_list (lr, to);
1743 ALLOCNO_LIVE_RANGES (to)
1744 = ira_merge_allocno_live_ranges (lr, ALLOCNO_LIVE_RANGES (to));
1745}
1746
47dd2e78 1747/* Return TRUE if NODE represents a loop with low register
1748 pressure. */
1749static bool
1750low_pressure_loop_node_p (ira_loop_tree_node_t node)
1751{
1752 int i;
1753 enum reg_class cover_class;
48e1416a 1754
47dd2e78 1755 if (node->bb != NULL)
1756 return false;
48e1416a 1757
47dd2e78 1758 for (i = 0; i < ira_reg_class_cover_size; i++)
1759 {
1760 cover_class = ira_reg_class_cover[i];
1761 if (node->reg_pressure[cover_class]
1762 > ira_available_class_regs[cover_class])
1763 return false;
1764 }
1765 return true;
1766}
1767
ddf888a5 1768/* Sort loops for marking them for removal. We put already marked
1769 loops first, then less frequent loops next, and then outer loops
1770 next. */
1771static int
1772loop_compare_func (const void *v1p, const void *v2p)
1773{
1774 int diff;
1775 ira_loop_tree_node_t l1 = *(const ira_loop_tree_node_t *) v1p;
1776 ira_loop_tree_node_t l2 = *(const ira_loop_tree_node_t *) v2p;
1777
1778 ira_assert (l1->parent != NULL && l2->parent != NULL);
1779 if (l1->to_remove_p && ! l2->to_remove_p)
1780 return -1;
1781 if (! l1->to_remove_p && l2->to_remove_p)
1782 return 1;
1783 if ((diff = l1->loop->header->frequency - l2->loop->header->frequency) != 0)
1784 return diff;
1785 if ((diff = (int) loop_depth (l1->loop) - (int) loop_depth (l2->loop)) != 0)
1786 return diff;
1787 /* Make sorting stable. */
1788 return l1->loop->num - l2->loop->num;
1789}
1790
1791
1792/* Mark loops which should be removed from regional allocation. We
1793 remove a loop with low register pressure inside another loop with
1794 register pressure. In this case a separate allocation of the loop
1795 hardly helps (for irregular register file architecture it could
1796 help by choosing a better hard register in the loop but we prefer
1797 faster allocation even in this case). We also remove cheap loops
1798 if there are more than IRA_MAX_LOOPS_NUM of them. */
1799static void
1800mark_loops_for_removal (void)
47dd2e78 1801{
ddf888a5 1802 int i, n;
1803 ira_loop_tree_node_t *sorted_loops;
1804 loop_p loop;
1805
1806 sorted_loops
1807 = (ira_loop_tree_node_t *) ira_allocate (sizeof (ira_loop_tree_node_t)
1808 * VEC_length (loop_p,
1809 ira_loops.larray));
1810 for (n = i = 0; VEC_iterate (loop_p, ira_loops.larray, i, loop); i++)
1811 if (ira_loop_nodes[i].regno_allocno_map != NULL)
1812 {
1813 if (ira_loop_nodes[i].parent == NULL)
1814 {
1815 /* Don't remove the root. */
1816 ira_loop_nodes[i].to_remove_p = false;
1817 continue;
1818 }
1819 sorted_loops[n++] = &ira_loop_nodes[i];
1820 ira_loop_nodes[i].to_remove_p
1821 = (low_pressure_loop_node_p (ira_loop_nodes[i].parent)
1822 && low_pressure_loop_node_p (&ira_loop_nodes[i]));
1823 }
1824 qsort (sorted_loops, n, sizeof (ira_loop_tree_node_t), loop_compare_func);
1825 for (i = 0; n - i + 1 > IRA_MAX_LOOPS_NUM; i++)
1826 {
1827 sorted_loops[i]->to_remove_p = true;
1828 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
1829 fprintf
1830 (ira_dump_file,
1831 " Mark loop %d (header %d, freq %d, depth %d) for removal (%s)\n",
1832 sorted_loops[i]->loop->num, sorted_loops[i]->loop->header->index,
1833 sorted_loops[i]->loop->header->frequency,
1834 loop_depth (sorted_loops[i]->loop),
1835 low_pressure_loop_node_p (sorted_loops[i]->parent)
1836 && low_pressure_loop_node_p (sorted_loops[i])
1837 ? "low pressure" : "cheap loop");
1838 }
1839 ira_free (sorted_loops);
47dd2e78 1840}
1841
95c83f01 1842/* Mark all loops but root for removing. */
1843static void
1844mark_all_loops_for_removal (void)
1845{
1846 int i;
1847 loop_p loop;
1848
1849 for (i = 0; VEC_iterate (loop_p, ira_loops.larray, i, loop); i++)
1850 if (ira_loop_nodes[i].regno_allocno_map != NULL)
1851 {
1852 if (ira_loop_nodes[i].parent == NULL)
1853 {
1854 /* Don't remove the root. */
1855 ira_loop_nodes[i].to_remove_p = false;
1856 continue;
1857 }
1858 ira_loop_nodes[i].to_remove_p = true;
1859 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
1860 fprintf
1861 (ira_dump_file,
1862 " Mark loop %d (header %d, freq %d, depth %d) for removal\n",
1863 ira_loop_nodes[i].loop->num,
1864 ira_loop_nodes[i].loop->header->index,
1865 ira_loop_nodes[i].loop->header->frequency,
1866 loop_depth (ira_loop_nodes[i].loop));
1867 }
1868}
ddf888a5 1869
47dd2e78 1870/* Definition of vector of loop tree nodes. */
1871DEF_VEC_P(ira_loop_tree_node_t);
1872DEF_VEC_ALLOC_P(ira_loop_tree_node_t, heap);
1873
1874/* Vec containing references to all removed loop tree nodes. */
1875static VEC(ira_loop_tree_node_t,heap) *removed_loop_vec;
1876
1877/* Vec containing references to all children of loop tree nodes. */
1878static VEC(ira_loop_tree_node_t,heap) *children_vec;
1879
1880/* Remove subregions of NODE if their separate allocation will not
1881 improve the result. */
1882static void
1883remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_node_t node)
1884{
1885 unsigned int start;
1886 bool remove_p;
1887 ira_loop_tree_node_t subnode;
1888
ddf888a5 1889 remove_p = node->to_remove_p;
47dd2e78 1890 if (! remove_p)
1891 VEC_safe_push (ira_loop_tree_node_t, heap, children_vec, node);
1892 start = VEC_length (ira_loop_tree_node_t, children_vec);
1893 for (subnode = node->children; subnode != NULL; subnode = subnode->next)
1894 if (subnode->bb == NULL)
1895 remove_uneccesary_loop_nodes_from_loop_tree (subnode);
1896 else
1897 VEC_safe_push (ira_loop_tree_node_t, heap, children_vec, subnode);
1898 node->children = node->subloops = NULL;
1899 if (remove_p)
1900 {
1901 VEC_safe_push (ira_loop_tree_node_t, heap, removed_loop_vec, node);
1902 return;
1903 }
1904 while (VEC_length (ira_loop_tree_node_t, children_vec) > start)
1905 {
1906 subnode = VEC_pop (ira_loop_tree_node_t, children_vec);
1907 subnode->parent = node;
1908 subnode->next = node->children;
1909 node->children = subnode;
1910 if (subnode->bb == NULL)
1911 {
1912 subnode->subloop_next = node->subloops;
1913 node->subloops = subnode;
1914 }
1915 }
1916}
1917
09c8c6cc 1918/* Return TRUE if NODE is inside PARENT. */
1919static bool
1920loop_is_inside_p (ira_loop_tree_node_t node, ira_loop_tree_node_t parent)
1921{
1922 for (node = node->parent; node != NULL; node = node->parent)
1923 if (node == parent)
1924 return true;
1925 return false;
1926}
1927
1928/* Sort allocnos according to their order in regno allocno list. */
1929static int
1930regno_allocno_order_compare_func (const void *v1p, const void *v2p)
1931{
1932 ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
1933 ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
1934 ira_loop_tree_node_t n1 = ALLOCNO_LOOP_TREE_NODE (a1);
1935 ira_loop_tree_node_t n2 = ALLOCNO_LOOP_TREE_NODE (a2);
1936
1937 if (loop_is_inside_p (n1, n2))
1938 return -1;
1939 else if (loop_is_inside_p (n2, n1))
1940 return 1;
1941 /* If allocnos are equally good, sort by allocno numbers, so that
1942 the results of qsort leave nothing to chance. We put allocnos
1943 with higher number first in the list because it is the original
1944 order for allocnos from loops on the same levels. */
1945 return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
1946}
1947
1948/* This array is used to sort allocnos to restore allocno order in
1949 the regno allocno list. */
1950static ira_allocno_t *regno_allocnos;
1951
1952/* Restore allocno order for REGNO in the regno allocno list. */
1953static void
1954ira_rebuild_regno_allocno_list (int regno)
1955{
1956 int i, n;
1957 ira_allocno_t a;
1958
1959 for (n = 0, a = ira_regno_allocno_map[regno];
1960 a != NULL;
1961 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1962 regno_allocnos[n++] = a;
1963 ira_assert (n > 0);
48e1416a 1964 qsort (regno_allocnos, n, sizeof (ira_allocno_t),
09c8c6cc 1965 regno_allocno_order_compare_func);
1966 for (i = 1; i < n; i++)
1967 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[i - 1]) = regno_allocnos[i];
1968 ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[n - 1]) = NULL;
1969 ira_regno_allocno_map[regno] = regno_allocnos[0];
1970 if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
1971 fprintf (ira_dump_file, " Rebuilding regno allocno list for %d\n", regno);
1972}
1973
95c83f01 1974/* Propagate info from allocno FROM_A to allocno A. */
1975static void
1976propagate_some_info_from_allocno (ira_allocno_t a, ira_allocno_t from_a)
1977{
1978 enum reg_class cover_class;
1979
b4f5e198 1980 merge_hard_reg_conflicts (from_a, a, false);
95c83f01 1981 ALLOCNO_NREFS (a) += ALLOCNO_NREFS (from_a);
1982 ALLOCNO_FREQ (a) += ALLOCNO_FREQ (from_a);
1983 ALLOCNO_CALL_FREQ (a) += ALLOCNO_CALL_FREQ (from_a);
95c83f01 1984 ALLOCNO_CALLS_CROSSED_NUM (a) += ALLOCNO_CALLS_CROSSED_NUM (from_a);
1985 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
1986 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (from_a);
1987 if (! ALLOCNO_BAD_SPILL_P (from_a))
1988 ALLOCNO_BAD_SPILL_P (a) = false;
95c83f01 1989 cover_class = ALLOCNO_COVER_CLASS (from_a);
1990 ira_assert (cover_class == ALLOCNO_COVER_CLASS (a));
1991 ira_allocate_and_accumulate_costs (&ALLOCNO_HARD_REG_COSTS (a), cover_class,
1992 ALLOCNO_HARD_REG_COSTS (from_a));
1993 ira_allocate_and_accumulate_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
1994 cover_class,
1995 ALLOCNO_CONFLICT_HARD_REG_COSTS (from_a));
1996 ALLOCNO_COVER_CLASS_COST (a) += ALLOCNO_COVER_CLASS_COST (from_a);
1997 ALLOCNO_MEMORY_COST (a) += ALLOCNO_MEMORY_COST (from_a);
1998}
1999
47dd2e78 2000/* Remove allocnos from loops removed from the allocation
2001 consideration. */
2002static void
2003remove_unnecessary_allocnos (void)
2004{
2005 int regno;
09c8c6cc 2006 bool merged_p, rebuild_p;
47dd2e78 2007 ira_allocno_t a, prev_a, next_a, parent_a;
2008 ira_loop_tree_node_t a_node, parent;
47dd2e78 2009
2010 merged_p = false;
09c8c6cc 2011 regno_allocnos = NULL;
47dd2e78 2012 for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
09c8c6cc 2013 {
2014 rebuild_p = false;
2015 for (prev_a = NULL, a = ira_regno_allocno_map[regno];
2016 a != NULL;
2017 a = next_a)
2018 {
2019 next_a = ALLOCNO_NEXT_REGNO_ALLOCNO (a);
2020 a_node = ALLOCNO_LOOP_TREE_NODE (a);
2021 if (! a_node->to_remove_p)
2022 prev_a = a;
2023 else
2024 {
2025 for (parent = a_node->parent;
2026 (parent_a = parent->regno_allocno_map[regno]) == NULL
2027 && parent->to_remove_p;
2028 parent = parent->parent)
2029 ;
2030 if (parent_a == NULL)
2031 {
95c83f01 2032 /* There are no allocnos with the same regno in
2033 upper region -- just move the allocno to the
2034 upper region. */
09c8c6cc 2035 prev_a = a;
2036 ALLOCNO_LOOP_TREE_NODE (a) = parent;
2037 parent->regno_allocno_map[regno] = a;
2038 bitmap_set_bit (parent->all_allocnos, ALLOCNO_NUM (a));
2039 rebuild_p = true;
2040 }
2041 else
2042 {
2043 /* Remove the allocno and update info of allocno in
2044 the upper region. */
2045 if (prev_a == NULL)
2046 ira_regno_allocno_map[regno] = next_a;
2047 else
2048 ALLOCNO_NEXT_REGNO_ALLOCNO (prev_a) = next_a;
b4f5e198 2049 move_allocno_live_ranges (a, parent_a);
09c8c6cc 2050 merged_p = true;
95c83f01 2051 propagate_some_info_from_allocno (parent_a, a);
7ec7a88e 2052 /* Remove it from the corresponding regno allocno
2053 map to avoid info propagation of subsequent
2054 allocno into this already removed allocno. */
2055 a_node->regno_allocno_map[regno] = NULL;
09c8c6cc 2056 finish_allocno (a);
2057 }
2058 }
2059 }
2060 if (rebuild_p)
2061 /* We need to restore the order in regno allocno list. */
2062 {
2063 if (regno_allocnos == NULL)
2064 regno_allocnos
2065 = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
2066 * ira_allocnos_num);
2067 ira_rebuild_regno_allocno_list (regno);
2068 }
2069 }
47dd2e78 2070 if (merged_p)
2071 ira_rebuild_start_finish_chains ();
09c8c6cc 2072 if (regno_allocnos != NULL)
2073 ira_free (regno_allocnos);
47dd2e78 2074}
2075
95c83f01 2076/* Remove allocnos from all loops but the root. */
47dd2e78 2077static void
95c83f01 2078remove_low_level_allocnos (void)
47dd2e78 2079{
95c83f01 2080 int regno;
2081 bool merged_p, propagate_p;
2082 ira_allocno_t a, top_a;
2083 ira_loop_tree_node_t a_node, parent;
95c83f01 2084 ira_allocno_iterator ai;
2085
2086 merged_p = false;
2087 FOR_EACH_ALLOCNO (a, ai)
2088 {
2089 a_node = ALLOCNO_LOOP_TREE_NODE (a);
2090 if (a_node == ira_loop_tree_root || ALLOCNO_CAP_MEMBER (a) != NULL)
2091 continue;
2092 regno = ALLOCNO_REGNO (a);
2093 if ((top_a = ira_loop_tree_root->regno_allocno_map[regno]) == NULL)
2094 {
2095 ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
2096 ira_loop_tree_root->regno_allocno_map[regno] = a;
2097 continue;
2098 }
2099 propagate_p = a_node->parent->regno_allocno_map[regno] == NULL;
2100 /* Remove the allocno and update info of allocno in the upper
2101 region. */
b4f5e198 2102 move_allocno_live_ranges (a, top_a);
95c83f01 2103 merged_p = true;
95c83f01 2104 if (propagate_p)
2105 propagate_some_info_from_allocno (top_a, a);
2106 }
2107 FOR_EACH_ALLOCNO (a, ai)
2108 {
2109 a_node = ALLOCNO_LOOP_TREE_NODE (a);
2110 if (a_node == ira_loop_tree_root)
2111 continue;
2112 parent = a_node->parent;
2113 regno = ALLOCNO_REGNO (a);
2114 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2115 ira_assert (ALLOCNO_CAP (a) != NULL);
2116 else if (ALLOCNO_CAP (a) == NULL)
2117 ira_assert (parent->regno_allocno_map[regno] != NULL);
2118 }
2119 FOR_EACH_ALLOCNO (a, ai)
2120 {
2121 regno = ALLOCNO_REGNO (a);
2122 if (ira_loop_tree_root->regno_allocno_map[regno] == a)
2123 {
ae9587ed 2124 ira_object_t obj = ALLOCNO_OBJECT (a);
2125
95c83f01 2126 ira_regno_allocno_map[regno] = a;
2127 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = NULL;
2128 ALLOCNO_CAP_MEMBER (a) = NULL;
ae9587ed 2129 COPY_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
2130 OBJECT_TOTAL_CONFLICT_HARD_REGS (obj));
95c83f01 2131#ifdef STACK_REGS
2132 if (ALLOCNO_TOTAL_NO_STACK_REG_P (a))
2133 ALLOCNO_NO_STACK_REG_P (a) = true;
2134#endif
2135 }
2136 else
2137 finish_allocno (a);
2138 }
2139 if (merged_p)
2140 ira_rebuild_start_finish_chains ();
2141}
2142
2143/* Remove loops from consideration. We remove all loops except for
2144 root if ALL_P or loops for which a separate allocation will not
2145 improve the result. We have to do this after allocno creation and
2146 their costs and cover class evaluation because only after that the
2147 register pressure can be known and is calculated. */
2148static void
2149remove_unnecessary_regions (bool all_p)
2150{
2151 if (all_p)
2152 mark_all_loops_for_removal ();
2153 else
2154 mark_loops_for_removal ();
47dd2e78 2155 children_vec
2156 = VEC_alloc (ira_loop_tree_node_t, heap,
2157 last_basic_block + VEC_length (loop_p, ira_loops.larray));
2158 removed_loop_vec
2159 = VEC_alloc (ira_loop_tree_node_t, heap,
2160 last_basic_block + VEC_length (loop_p, ira_loops.larray));
2161 remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_root) ;
2162 VEC_free (ira_loop_tree_node_t, heap, children_vec);
95c83f01 2163 if (all_p)
2164 remove_low_level_allocnos ();
2165 else
2166 remove_unnecessary_allocnos ();
47dd2e78 2167 while (VEC_length (ira_loop_tree_node_t, removed_loop_vec) > 0)
2168 finish_loop_tree_node (VEC_pop (ira_loop_tree_node_t, removed_loop_vec));
2169 VEC_free (ira_loop_tree_node_t, heap, removed_loop_vec);
2170}
2171
2172\f
2173
68d4bdfb 2174/* At this point true value of allocno attribute bad_spill_p means
2175 that there is an insn where allocno occurs and where the allocno
2176 can not be used as memory. The function updates the attribute, now
2177 it can be true only for allocnos which can not be used as memory in
2178 an insn and in whose live ranges there is other allocno deaths.
2179 Spilling allocnos with true value will not improve the code because
2180 it will not make other allocnos colorable and additional reloads
2181 for the corresponding pseudo will be generated in reload pass for
2182 each insn it occurs.
2183
2184 This is a trick mentioned in one classic article of Chaitin etc
2185 which is frequently omitted in other implementations of RA based on
2186 graph coloring. */
2187static void
2188update_bad_spill_attribute (void)
2189{
2190 int i;
2191 ira_allocno_t a;
2192 ira_allocno_iterator ai;
fbff82f4 2193 live_range_t r;
68d4bdfb 2194 enum reg_class cover_class;
2195 bitmap_head dead_points[N_REG_CLASSES];
2196
2197 for (i = 0; i < ira_reg_class_cover_size; i++)
2198 {
2199 cover_class = ira_reg_class_cover[i];
2200 bitmap_initialize (&dead_points[cover_class], &reg_obstack);
2201 }
2202 FOR_EACH_ALLOCNO (a, ai)
2203 {
2204 cover_class = ALLOCNO_COVER_CLASS (a);
2205 if (cover_class == NO_REGS)
2206 continue;
2207 for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
2208 bitmap_set_bit (&dead_points[cover_class], r->finish);
2209 }
2210 FOR_EACH_ALLOCNO (a, ai)
2211 {
2212 cover_class = ALLOCNO_COVER_CLASS (a);
2213 if (cover_class == NO_REGS)
2214 continue;
2215 if (! ALLOCNO_BAD_SPILL_P (a))
2216 continue;
2217 for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
2218 {
2219 for (i = r->start + 1; i < r->finish; i++)
2220 if (bitmap_bit_p (&dead_points[cover_class], i))
2221 break;
2222 if (i < r->finish)
2223 break;
2224 }
2225 if (r != NULL)
2226 ALLOCNO_BAD_SPILL_P (a) = false;
2227 }
2228 for (i = 0; i < ira_reg_class_cover_size; i++)
2229 {
2230 cover_class = ira_reg_class_cover[i];
2231 bitmap_clear (&dead_points[cover_class]);
2232 }
2233}
2234
2235\f
2236
47dd2e78 2237/* Set up minimal and maximal live range points for allocnos. */
2238static void
2239setup_min_max_allocno_live_range_point (void)
2240{
2241 int i;
2242 ira_allocno_t a, parent_a, cap;
2243 ira_allocno_iterator ai;
fbff82f4 2244 live_range_t r;
47dd2e78 2245 ira_loop_tree_node_t parent;
2246
2247 FOR_EACH_ALLOCNO (a, ai)
2248 {
ae9587ed 2249 ira_object_t obj = ALLOCNO_OBJECT (a);
47dd2e78 2250 r = ALLOCNO_LIVE_RANGES (a);
2251 if (r == NULL)
2252 continue;
ae9587ed 2253 OBJECT_MAX (obj) = r->finish;
47dd2e78 2254 for (; r->next != NULL; r = r->next)
2255 ;
ae9587ed 2256 OBJECT_MIN (obj) = r->start;
47dd2e78 2257 }
2258 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
2259 for (a = ira_regno_allocno_map[i];
2260 a != NULL;
2261 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2262 {
ae9587ed 2263 ira_object_t obj = ALLOCNO_OBJECT (a);
2264 ira_object_t parent_obj;
2265
2266 if (OBJECT_MAX (obj) < 0)
47dd2e78 2267 continue;
2268 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2269 /* Accumulation of range info. */
2270 if (ALLOCNO_CAP (a) != NULL)
2271 {
2272 for (cap = ALLOCNO_CAP (a); cap != NULL; cap = ALLOCNO_CAP (cap))
2273 {
ae9587ed 2274 ira_object_t cap_obj = ALLOCNO_OBJECT (cap);
2275 if (OBJECT_MAX (cap_obj) < OBJECT_MAX (obj))
2276 OBJECT_MAX (cap_obj) = OBJECT_MAX (obj);
2277 if (OBJECT_MIN (cap_obj) > OBJECT_MIN (obj))
2278 OBJECT_MIN (cap_obj) = OBJECT_MIN (obj);
47dd2e78 2279 }
2280 continue;
2281 }
2282 if ((parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) == NULL)
2283 continue;
2284 parent_a = parent->regno_allocno_map[i];
ae9587ed 2285 parent_obj = ALLOCNO_OBJECT (parent_a);
2286 if (OBJECT_MAX (parent_obj) < OBJECT_MAX (obj))
2287 OBJECT_MAX (parent_obj) = OBJECT_MAX (obj);
2288 if (OBJECT_MIN (parent_obj) > OBJECT_MIN (obj))
2289 OBJECT_MIN (parent_obj) = OBJECT_MIN (obj);
47dd2e78 2290 }
2291#ifdef ENABLE_IRA_CHECKING
2292 FOR_EACH_ALLOCNO (a, ai)
2293 {
ae9587ed 2294 ira_object_t obj = ALLOCNO_OBJECT (a);
2295 if ((0 <= OBJECT_MIN (obj) && OBJECT_MIN (obj) <= ira_max_point)
2296 && (0 <= OBJECT_MAX (obj) && OBJECT_MAX (obj) <= ira_max_point))
47dd2e78 2297 continue;
2298 gcc_unreachable ();
2299 }
2300#endif
2301}
2302
2303/* Sort allocnos according to their live ranges. Allocnos with
14792f4e 2304 smaller cover class are put first unless we use priority coloring.
ae9587ed 2305 Allocnos with the same cover class are ordered according their start
14792f4e 2306 (min). Allocnos with the same start are ordered according their
2307 finish (max). */
47dd2e78 2308static int
2309allocno_range_compare_func (const void *v1p, const void *v2p)
2310{
2311 int diff;
ae9587ed 2312 ira_object_t obj1 = *(const ira_object_t *) v1p;
2313 ira_object_t obj2 = *(const ira_object_t *) v2p;
2314 ira_allocno_t a1 = OBJECT_ALLOCNO (obj1);
2315 ira_allocno_t a2 = OBJECT_ALLOCNO (obj2);
47dd2e78 2316
14792f4e 2317 if (flag_ira_algorithm != IRA_ALGORITHM_PRIORITY
2318 && (diff = ALLOCNO_COVER_CLASS (a1) - ALLOCNO_COVER_CLASS (a2)) != 0)
47dd2e78 2319 return diff;
ae9587ed 2320 if ((diff = OBJECT_MIN (obj1) - OBJECT_MIN (obj2)) != 0)
47dd2e78 2321 return diff;
ae9587ed 2322 if ((diff = OBJECT_MAX (obj1) - OBJECT_MAX (obj2)) != 0)
47dd2e78 2323 return diff;
2324 return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2325}
2326
ae9587ed 2327/* Sort ira_object_id_map and set up conflict id of allocnos. */
47dd2e78 2328static void
ae9587ed 2329sort_conflict_id_map (void)
47dd2e78 2330{
2331 int i, num;
2332 ira_allocno_t a;
2333 ira_allocno_iterator ai;
2334
2335 num = 0;
2336 FOR_EACH_ALLOCNO (a, ai)
ae9587ed 2337 ira_object_id_map[num++] = ALLOCNO_OBJECT (a);
2338 qsort (ira_object_id_map, num, sizeof (ira_object_t),
47dd2e78 2339 allocno_range_compare_func);
2340 for (i = 0; i < num; i++)
ae9587ed 2341 {
2342 ira_object_t obj = ira_object_id_map[i];
2343 gcc_assert (obj != NULL);
2344 OBJECT_CONFLICT_ID (obj) = i;
2345 }
2346 for (i = num; i < ira_objects_num; i++)
2347 ira_object_id_map[i] = NULL;
47dd2e78 2348}
2349
2350/* Set up minimal and maximal conflict ids of allocnos with which
2351 given allocno can conflict. */
2352static void
2353setup_min_max_conflict_allocno_ids (void)
2354{
14792f4e 2355 int cover_class;
47dd2e78 2356 int i, j, min, max, start, finish, first_not_finished, filled_area_start;
2357 int *live_range_min, *last_lived;
2358 ira_allocno_t a;
2359
ae9587ed 2360 live_range_min = (int *) ira_allocate (sizeof (int) * ira_objects_num);
47dd2e78 2361 cover_class = -1;
2362 first_not_finished = -1;
ae9587ed 2363 for (i = 0; i < ira_objects_num; i++)
47dd2e78 2364 {
ae9587ed 2365 ira_object_t obj = ira_object_id_map[i];
2366 if (obj == NULL)
47dd2e78 2367 continue;
ae9587ed 2368
2369 a = OBJECT_ALLOCNO (obj);
2370
14792f4e 2371 if (cover_class < 0
2372 || (flag_ira_algorithm != IRA_ALGORITHM_PRIORITY
2373 && cover_class != (int) ALLOCNO_COVER_CLASS (a)))
47dd2e78 2374 {
2375 cover_class = ALLOCNO_COVER_CLASS (a);
2376 min = i;
2377 first_not_finished = i;
2378 }
2379 else
2380 {
ae9587ed 2381 start = OBJECT_MIN (obj);
47dd2e78 2382 /* If we skip an allocno, the allocno with smaller ids will
2383 be also skipped because of the secondary sorting the
2384 range finishes (see function
2385 allocno_range_compare_func). */
2386 while (first_not_finished < i
ae9587ed 2387 && start > OBJECT_MAX (ira_object_id_map
47dd2e78 2388 [first_not_finished]))
2389 first_not_finished++;
2390 min = first_not_finished;
48e1416a 2391 }
47dd2e78 2392 if (min == i)
2393 /* We could increase min further in this case but it is good
2394 enough. */
2395 min++;
ae9587ed 2396 live_range_min[i] = OBJECT_MIN (obj);
2397 OBJECT_MIN (obj) = min;
47dd2e78 2398 }
2399 last_lived = (int *) ira_allocate (sizeof (int) * ira_max_point);
2400 cover_class = -1;
2401 filled_area_start = -1;
ae9587ed 2402 for (i = ira_objects_num - 1; i >= 0; i--)
47dd2e78 2403 {
ae9587ed 2404 ira_object_t obj = ira_object_id_map[i];
2405 if (obj == NULL)
47dd2e78 2406 continue;
ae9587ed 2407
2408 a = OBJECT_ALLOCNO (obj);
14792f4e 2409 if (cover_class < 0
2410 || (flag_ira_algorithm != IRA_ALGORITHM_PRIORITY
2411 && cover_class != (int) ALLOCNO_COVER_CLASS (a)))
47dd2e78 2412 {
2413 cover_class = ALLOCNO_COVER_CLASS (a);
2414 for (j = 0; j < ira_max_point; j++)
2415 last_lived[j] = -1;
2416 filled_area_start = ira_max_point;
2417 }
2418 min = live_range_min[i];
ae9587ed 2419 finish = OBJECT_MAX (obj);
47dd2e78 2420 max = last_lived[finish];
2421 if (max < 0)
2422 /* We could decrease max further in this case but it is good
2423 enough. */
ae9587ed 2424 max = OBJECT_CONFLICT_ID (obj) - 1;
2425 OBJECT_MAX (obj) = max;
47dd2e78 2426 /* In filling, we can go further A range finish to recognize
2427 intersection quickly because if the finish of subsequently
2428 processed allocno (it has smaller conflict id) range is
2429 further A range finish than they are definitely intersected
2430 (the reason for this is the allocnos with bigger conflict id
2431 have their range starts not smaller than allocnos with
2432 smaller ids. */
2433 for (j = min; j < filled_area_start; j++)
2434 last_lived[j] = i;
2435 filled_area_start = min;
2436 }
2437 ira_free (last_lived);
2438 ira_free (live_range_min);
2439}
2440
2441\f
2442
2443static void
2444create_caps (void)
2445{
2446 ira_allocno_t a;
2447 ira_allocno_iterator ai;
2448 ira_loop_tree_node_t loop_tree_node;
2449
2450 FOR_EACH_ALLOCNO (a, ai)
2451 {
2452 if (ALLOCNO_LOOP_TREE_NODE (a) == ira_loop_tree_root)
2453 continue;
2454 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2455 create_cap_allocno (a);
2456 else if (ALLOCNO_CAP (a) == NULL)
2457 {
2458 loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
2459 if (!bitmap_bit_p (loop_tree_node->border_allocnos, ALLOCNO_NUM (a)))
2460 create_cap_allocno (a);
2461 }
2462 }
2463}
2464
2465\f
2466
2467/* The page contains code transforming more one region internal
2468 representation (IR) to one region IR which is necessary for reload.
2469 This transformation is called IR flattening. We might just rebuild
2470 the IR for one region but we don't do it because it takes a lot of
2471 time. */
2472
76b340db 2473/* Map: regno -> allocnos which will finally represent the regno for
2474 IR with one region. */
2475static ira_allocno_t *regno_top_level_allocno_map;
2476
c58db480 2477/* Find the allocno that corresponds to A at a level one higher up in the
2478 loop tree. Returns NULL if A is a cap, or if it has no parent. */
2479ira_allocno_t
2480ira_parent_allocno (ira_allocno_t a)
2481{
2482 ira_loop_tree_node_t parent;
2483
2484 if (ALLOCNO_CAP (a) != NULL)
2485 return NULL;
2486
2487 parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
2488 if (parent == NULL)
2489 return NULL;
2490
2491 return parent->regno_allocno_map[ALLOCNO_REGNO (a)];
2492}
2493
2494/* Find the allocno that corresponds to A at a level one higher up in the
2495 loop tree. If ALLOCNO_CAP is set for A, return that. */
2496ira_allocno_t
2497ira_parent_or_cap_allocno (ira_allocno_t a)
2498{
2499 if (ALLOCNO_CAP (a) != NULL)
2500 return ALLOCNO_CAP (a);
2501
2502 return ira_parent_allocno (a);
2503}
2504
76b340db 2505/* Process all allocnos originated from pseudo REGNO and copy live
e3fe8cc4 2506 ranges, hard reg conflicts, and allocno stack reg attributes from
2507 low level allocnos to final allocnos which are destinations of
2508 removed stores at a loop exit. Return true if we copied live
2509 ranges. */
76b340db 2510static bool
e3fe8cc4 2511copy_info_to_removed_store_destinations (int regno)
76b340db 2512{
8efd2ddb 2513 ira_allocno_t a;
2514 ira_allocno_t parent_a = NULL;
76b340db 2515 ira_loop_tree_node_t parent;
76b340db 2516 bool merged_p;
2517
2518 merged_p = false;
2519 for (a = ira_regno_allocno_map[regno];
2520 a != NULL;
2521 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2522 {
2523 if (a != regno_top_level_allocno_map[REGNO (ALLOCNO_REG (a))])
2524 /* This allocno will be removed. */
2525 continue;
2526 /* Caps will be removed. */
2527 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2528 for (parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
2529 parent != NULL;
2530 parent = parent->parent)
2531 if ((parent_a = parent->regno_allocno_map[regno]) == NULL
2532 || (parent_a == regno_top_level_allocno_map[REGNO (ALLOCNO_REG
2533 (parent_a))]
2534 && ALLOCNO_MEM_OPTIMIZED_DEST_P (parent_a)))
2535 break;
2536 if (parent == NULL || parent_a == NULL)
2537 continue;
b4f5e198 2538 copy_allocno_live_ranges (a, parent_a);
2539 merge_hard_reg_conflicts (a, parent_a, true);
0b1329df 2540 ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
2541 ALLOCNO_CALLS_CROSSED_NUM (parent_a)
2542 += ALLOCNO_CALLS_CROSSED_NUM (a);
2543 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
2544 += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
76b340db 2545 merged_p = true;
2546 }
2547 return merged_p;
47dd2e78 2548}
2549
2550/* Flatten the IR. In other words, this function transforms IR as if
2551 it were built with one region (without loops). We could make it
2552 much simpler by rebuilding IR with one region, but unfortunately it
2553 takes a lot of time. MAX_REGNO_BEFORE_EMIT and
2554 IRA_MAX_POINT_BEFORE_EMIT are correspondingly MAX_REG_NUM () and
2555 IRA_MAX_POINT before emitting insns on the loop borders. */
2556void
2557ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
2558{
2559 int i, j, num;
0b1329df 2560 bool keep_p;
47dd2e78 2561 int hard_regs_num;
76b340db 2562 bool new_pseudos_p, merged_p, mem_dest_p;
47dd2e78 2563 unsigned int n;
2564 enum reg_class cover_class;
2565 ira_allocno_t a, parent_a, first, second, node_first, node_second;
47dd2e78 2566 ira_copy_t cp;
c58db480 2567 ira_loop_tree_node_t node;
fbff82f4 2568 live_range_t r;
47dd2e78 2569 ira_allocno_iterator ai;
2570 ira_copy_iterator ci;
2571 sparseset allocnos_live;
47dd2e78 2572
2573 regno_top_level_allocno_map
2574 = (ira_allocno_t *) ira_allocate (max_reg_num () * sizeof (ira_allocno_t));
2575 memset (regno_top_level_allocno_map, 0,
2576 max_reg_num () * sizeof (ira_allocno_t));
47dd2e78 2577 new_pseudos_p = merged_p = false;
ad305e06 2578 FOR_EACH_ALLOCNO (a, ai)
2579 {
ae9587ed 2580 ira_object_t obj = ALLOCNO_OBJECT (a);
ad305e06 2581 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2582 /* Caps are not in the regno allocno maps and they are never
2583 will be transformed into allocnos existing after IR
2584 flattening. */
2585 continue;
ae9587ed 2586 COPY_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
2587 OBJECT_CONFLICT_HARD_REGS (obj));
ad305e06 2588#ifdef STACK_REGS
2589 ALLOCNO_TOTAL_NO_STACK_REG_P (a) = ALLOCNO_NO_STACK_REG_P (a);
2590#endif
2591 }
47dd2e78 2592 /* Fix final allocno attributes. */
2593 for (i = max_regno_before_emit - 1; i >= FIRST_PSEUDO_REGISTER; i--)
2594 {
ad305e06 2595 mem_dest_p = false;
47dd2e78 2596 for (a = ira_regno_allocno_map[i];
2597 a != NULL;
2598 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2599 {
2600 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2601 if (ALLOCNO_SOMEWHERE_RENAMED_P (a))
2602 new_pseudos_p = true;
c58db480 2603 parent_a = ira_parent_allocno (a);
2604 if (parent_a == NULL)
47dd2e78 2605 {
2606 ALLOCNO_COPIES (a) = NULL;
2607 regno_top_level_allocno_map[REGNO (ALLOCNO_REG (a))] = a;
2608 continue;
2609 }
2610 ira_assert (ALLOCNO_CAP_MEMBER (parent_a) == NULL);
48e1416a 2611
76b340db 2612 if (ALLOCNO_MEM_OPTIMIZED_DEST (a) != NULL)
2613 mem_dest_p = true;
ad305e06 2614 if (REGNO (ALLOCNO_REG (a)) == REGNO (ALLOCNO_REG (parent_a)))
47dd2e78 2615 {
b4f5e198 2616 merge_hard_reg_conflicts (a, parent_a, true);
2617 move_allocno_live_ranges (a, parent_a);
47dd2e78 2618 merged_p = true;
47dd2e78 2619 ALLOCNO_MEM_OPTIMIZED_DEST_P (parent_a)
2620 = (ALLOCNO_MEM_OPTIMIZED_DEST_P (parent_a)
2621 || ALLOCNO_MEM_OPTIMIZED_DEST_P (a));
2622 continue;
2623 }
2624 new_pseudos_p = true;
47dd2e78 2625 for (;;)
2626 {
47dd2e78 2627 ALLOCNO_NREFS (parent_a) -= ALLOCNO_NREFS (a);
2628 ALLOCNO_FREQ (parent_a) -= ALLOCNO_FREQ (a);
0b1329df 2629 ALLOCNO_CALL_FREQ (parent_a) -= ALLOCNO_CALL_FREQ (a);
2630 ALLOCNO_CALLS_CROSSED_NUM (parent_a)
2631 -= ALLOCNO_CALLS_CROSSED_NUM (a);
2632 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
2633 -= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
47dd2e78 2634 ira_assert (ALLOCNO_CALLS_CROSSED_NUM (parent_a) >= 0
2635 && ALLOCNO_NREFS (parent_a) >= 0
2636 && ALLOCNO_FREQ (parent_a) >= 0);
2637 cover_class = ALLOCNO_COVER_CLASS (parent_a);
2638 hard_regs_num = ira_class_hard_regs_num[cover_class];
2639 if (ALLOCNO_HARD_REG_COSTS (a) != NULL
2640 && ALLOCNO_HARD_REG_COSTS (parent_a) != NULL)
2641 for (j = 0; j < hard_regs_num; j++)
2642 ALLOCNO_HARD_REG_COSTS (parent_a)[j]
2643 -= ALLOCNO_HARD_REG_COSTS (a)[j];
2644 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL
2645 && ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a) != NULL)
2646 for (j = 0; j < hard_regs_num; j++)
2647 ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a)[j]
2648 -= ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[j];
2649 ALLOCNO_COVER_CLASS_COST (parent_a)
2650 -= ALLOCNO_COVER_CLASS_COST (a);
2651 ALLOCNO_MEMORY_COST (parent_a) -= ALLOCNO_MEMORY_COST (a);
c58db480 2652 parent_a = ira_parent_allocno (parent_a);
2653 if (parent_a == NULL)
47dd2e78 2654 break;
2655 }
47dd2e78 2656 ALLOCNO_COPIES (a) = NULL;
2657 regno_top_level_allocno_map[REGNO (ALLOCNO_REG (a))] = a;
2658 }
e3fe8cc4 2659 if (mem_dest_p && copy_info_to_removed_store_destinations (i))
76b340db 2660 merged_p = true;
47dd2e78 2661 }
47dd2e78 2662 ira_assert (new_pseudos_p || ira_max_point_before_emit == ira_max_point);
2663 if (merged_p || ira_max_point_before_emit != ira_max_point)
2664 ira_rebuild_start_finish_chains ();
2665 if (new_pseudos_p)
2666 {
2667 /* Rebuild conflicts. */
2668 FOR_EACH_ALLOCNO (a, ai)
2669 {
2670 if (a != regno_top_level_allocno_map[REGNO (ALLOCNO_REG (a))]
2671 || ALLOCNO_CAP_MEMBER (a) != NULL)
2672 continue;
2673 for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
2674 ira_assert (r->allocno == a);
ae9587ed 2675 clear_conflicts (ALLOCNO_OBJECT (a));
47dd2e78 2676 }
2677 allocnos_live = sparseset_alloc (ira_allocnos_num);
2678 for (i = 0; i < ira_max_point; i++)
2679 {
2680 for (r = ira_start_point_ranges[i]; r != NULL; r = r->start_next)
2681 {
2682 a = r->allocno;
2683 if (a != regno_top_level_allocno_map[REGNO (ALLOCNO_REG (a))]
2684 || ALLOCNO_CAP_MEMBER (a) != NULL)
2685 continue;
2686 num = ALLOCNO_NUM (a);
2687 cover_class = ALLOCNO_COVER_CLASS (a);
2688 sparseset_set_bit (allocnos_live, num);
2689 EXECUTE_IF_SET_IN_SPARSESET (allocnos_live, n)
2690 {
2691 ira_allocno_t live_a = ira_allocnos[n];
2692
14792f4e 2693 if (ira_reg_classes_intersect_p
2694 [cover_class][ALLOCNO_COVER_CLASS (live_a)]
47dd2e78 2695 /* Don't set up conflict for the allocno with itself. */
2696 && num != (int) n)
ae9587ed 2697 {
2698 ira_object_t obj = ALLOCNO_OBJECT (a);
2699 ira_object_t live_obj = ALLOCNO_OBJECT (live_a);
2700 ira_add_conflict (obj, live_obj);
2701 }
47dd2e78 2702 }
2703 }
48e1416a 2704
47dd2e78 2705 for (r = ira_finish_point_ranges[i]; r != NULL; r = r->finish_next)
2706 sparseset_clear_bit (allocnos_live, ALLOCNO_NUM (r->allocno));
2707 }
2708 sparseset_free (allocnos_live);
2709 compress_conflict_vecs ();
2710 }
2711 /* Mark some copies for removing and change allocnos in the rest
2712 copies. */
2713 FOR_EACH_COPY (cp, ci)
2714 {
2715 if (ALLOCNO_CAP_MEMBER (cp->first) != NULL
2716 || ALLOCNO_CAP_MEMBER (cp->second) != NULL)
2717 {
2718 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2719 fprintf
2720 (ira_dump_file, " Remove cp%d:%c%dr%d-%c%dr%d\n",
2721 cp->num, ALLOCNO_CAP_MEMBER (cp->first) != NULL ? 'c' : 'a',
2722 ALLOCNO_NUM (cp->first), REGNO (ALLOCNO_REG (cp->first)),
2723 ALLOCNO_CAP_MEMBER (cp->second) != NULL ? 'c' : 'a',
2724 ALLOCNO_NUM (cp->second), REGNO (ALLOCNO_REG (cp->second)));
2725 cp->loop_tree_node = NULL;
2726 continue;
2727 }
2728 first = regno_top_level_allocno_map[REGNO (ALLOCNO_REG (cp->first))];
2729 second = regno_top_level_allocno_map[REGNO (ALLOCNO_REG (cp->second))];
2730 node = cp->loop_tree_node;
2731 if (node == NULL)
2732 keep_p = true; /* It copy generated in ira-emit.c. */
2733 else
2734 {
2735 /* Check that the copy was not propagated from level on
2736 which we will have different pseudos. */
2737 node_first = node->regno_allocno_map[ALLOCNO_REGNO (cp->first)];
2738 node_second = node->regno_allocno_map[ALLOCNO_REGNO (cp->second)];
2739 keep_p = ((REGNO (ALLOCNO_REG (first))
2740 == REGNO (ALLOCNO_REG (node_first)))
2741 && (REGNO (ALLOCNO_REG (second))
2742 == REGNO (ALLOCNO_REG (node_second))));
2743 }
2744 if (keep_p)
2745 {
2746 cp->loop_tree_node = ira_loop_tree_root;
2747 cp->first = first;
2748 cp->second = second;
2749 }
2750 else
2751 {
2752 cp->loop_tree_node = NULL;
2753 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2754 fprintf (ira_dump_file, " Remove cp%d:a%dr%d-a%dr%d\n",
2755 cp->num, ALLOCNO_NUM (cp->first),
2756 REGNO (ALLOCNO_REG (cp->first)), ALLOCNO_NUM (cp->second),
2757 REGNO (ALLOCNO_REG (cp->second)));
2758 }
2759 }
2760 /* Remove unnecessary allocnos on lower levels of the loop tree. */
2761 FOR_EACH_ALLOCNO (a, ai)
2762 {
2763 if (a != regno_top_level_allocno_map[REGNO (ALLOCNO_REG (a))]
2764 || ALLOCNO_CAP_MEMBER (a) != NULL)
2765 {
2766 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2767 fprintf (ira_dump_file, " Remove a%dr%d\n",
2768 ALLOCNO_NUM (a), REGNO (ALLOCNO_REG (a)));
2769 finish_allocno (a);
2770 continue;
2771 }
2772 ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
2773 ALLOCNO_REGNO (a) = REGNO (ALLOCNO_REG (a));
2774 ALLOCNO_CAP (a) = NULL;
df07a54c 2775 /* Restore updated costs for assignments from reload. */
47dd2e78 2776 ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
df07a54c 2777 ALLOCNO_UPDATED_COVER_CLASS_COST (a) = ALLOCNO_COVER_CLASS_COST (a);
47dd2e78 2778 if (! ALLOCNO_ASSIGNED_P (a))
2779 ira_free_allocno_updated_costs (a);
2780 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
2781 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
2782 }
2783 /* Remove unnecessary copies. */
2784 FOR_EACH_COPY (cp, ci)
2785 {
2786 if (cp->loop_tree_node == NULL)
2787 {
2788 ira_copies[cp->num] = NULL;
2789 finish_copy (cp);
2790 continue;
2791 }
2792 ira_assert
2793 (ALLOCNO_LOOP_TREE_NODE (cp->first) == ira_loop_tree_root
2794 && ALLOCNO_LOOP_TREE_NODE (cp->second) == ira_loop_tree_root);
2795 ira_add_allocno_copy_to_list (cp);
2796 ira_swap_allocno_copy_ends_if_necessary (cp);
2797 }
2798 rebuild_regno_allocno_maps ();
7f36fbdf 2799 if (ira_max_point != ira_max_point_before_emit)
2800 ira_compress_allocno_live_ranges ();
47dd2e78 2801 ira_free (regno_top_level_allocno_map);
2802}
2803
2804\f
2805
2806#ifdef ENABLE_IRA_CHECKING
2807/* Check creation of all allocnos. Allocnos on lower levels should
2808 have allocnos or caps on all upper levels. */
2809static void
2810check_allocno_creation (void)
2811{
2812 ira_allocno_t a;
2813 ira_allocno_iterator ai;
2814 ira_loop_tree_node_t loop_tree_node;
2815
2816 FOR_EACH_ALLOCNO (a, ai)
2817 {
2bae4acc 2818 loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
2819 ira_assert (bitmap_bit_p (loop_tree_node->all_allocnos,
2820 ALLOCNO_NUM (a)));
2821 if (loop_tree_node == ira_loop_tree_root)
47dd2e78 2822 continue;
2823 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2bae4acc 2824 ira_assert (ALLOCNO_CAP (a) != NULL);
47dd2e78 2825 else if (ALLOCNO_CAP (a) == NULL)
2bae4acc 2826 ira_assert (loop_tree_node->parent
2827 ->regno_allocno_map[ALLOCNO_REGNO (a)] != NULL
2828 && bitmap_bit_p (loop_tree_node->border_allocnos,
2829 ALLOCNO_NUM (a)));
47dd2e78 2830 }
2831}
2832#endif
2833
1180227d 2834/* Identify allocnos which prefer a register class with a single hard register.
2835 Adjust ALLOCNO_CONFLICT_HARD_REG_COSTS so that conflicting allocnos are
2836 less likely to use the preferred singleton register. */
2837static void
2838update_conflict_hard_reg_costs (void)
2839{
2840 ira_allocno_t a;
2841 ira_allocno_iterator ai;
2842 int i, index, min;
2843
2844 FOR_EACH_ALLOCNO (a, ai)
2845 {
2846 enum reg_class cover_class = ALLOCNO_COVER_CLASS (a);
2847 enum reg_class pref = reg_preferred_class (ALLOCNO_REGNO (a));
2848
2849 if (reg_class_size[pref] != 1)
2850 continue;
2851 index = (ira_class_hard_reg_index[cover_class]
2852 [ira_class_hard_regs[pref][0]]);
2853 if (index < 0)
2854 continue;
2855 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) == NULL
2856 || ALLOCNO_HARD_REG_COSTS (a) == NULL)
2857 continue;
2858 min = INT_MAX;
2859 for (i = ira_class_hard_regs_num[cover_class] - 1; i >= 0; i--)
2860 if (ALLOCNO_HARD_REG_COSTS (a)[i] > ALLOCNO_COVER_CLASS_COST (a)
2861 && min > ALLOCNO_HARD_REG_COSTS (a)[i])
2862 min = ALLOCNO_HARD_REG_COSTS (a)[i];
2863 if (min == INT_MAX)
2864 continue;
2865 ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
2866 cover_class, 0);
2867 ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[index]
2868 -= min - ALLOCNO_COVER_CLASS_COST (a);
2869 }
2870}
2871
47dd2e78 2872/* Create a internal representation (IR) for IRA (allocnos, copies,
2873 loop tree nodes). If LOOPS_P is FALSE the nodes corresponding to
2874 the loops (except the root which corresponds the all function) and
2875 correspondingly allocnos for the loops will be not created. Such
2876 parameter value is used for Chaitin-Briggs coloring. The function
2877 returns TRUE if we generate loop structure (besides nodes
2878 representing all function and the basic blocks) for regional
2879 allocation. A true return means that we really need to flatten IR
2880 before the reload. */
2881bool
2882ira_build (bool loops_p)
2883{
2884 df_analyze ();
2885
2886 initiate_cost_vectors ();
2887 initiate_allocnos ();
2888 initiate_copies ();
2889 create_loop_tree_nodes (loops_p);
2890 form_loop_tree ();
2891 create_allocnos ();
2892 ira_costs ();
ae9587ed 2893 create_allocno_objects ();
47dd2e78 2894 ira_create_allocno_live_ranges ();
95c83f01 2895 remove_unnecessary_regions (false);
7f36fbdf 2896 ira_compress_allocno_live_ranges ();
68d4bdfb 2897 update_bad_spill_attribute ();
47dd2e78 2898 loops_p = more_one_region_p ();
2899 if (loops_p)
2900 {
2901 propagate_allocno_info ();
2902 create_caps ();
2903 }
2904 ira_tune_allocno_costs_and_cover_classes ();
2905#ifdef ENABLE_IRA_CHECKING
2906 check_allocno_creation ();
2907#endif
2908 setup_min_max_allocno_live_range_point ();
ae9587ed 2909 sort_conflict_id_map ();
47dd2e78 2910 setup_min_max_conflict_allocno_ids ();
2911 ira_build_conflicts ();
1180227d 2912 update_conflict_hard_reg_costs ();
95c83f01 2913 if (! ira_conflicts_p)
2914 {
2915 ira_allocno_t a;
2916 ira_allocno_iterator ai;
2917
2918 /* Remove all regions but root one. */
2919 if (loops_p)
2920 {
2921 remove_unnecessary_regions (true);
2922 loops_p = false;
2923 }
2924 /* We don't save hard registers around calls for fast allocation
2925 -- add caller clobbered registers as conflicting ones to
2926 allocno crossing calls. */
2927 FOR_EACH_ALLOCNO (a, ai)
2928 if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
2929 {
ae9587ed 2930 ira_object_t obj = ALLOCNO_OBJECT (a);
2931 IOR_HARD_REG_SET (OBJECT_TOTAL_CONFLICT_HARD_REGS (obj),
95c83f01 2932 call_used_reg_set);
ae9587ed 2933 IOR_HARD_REG_SET (OBJECT_CONFLICT_HARD_REGS (obj),
95c83f01 2934 call_used_reg_set);
2935 }
2936 }
55c858c5 2937 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
2938 print_copies (ira_dump_file);
47dd2e78 2939 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2940 {
2941 int n, nr;
2942 ira_allocno_t a;
fbff82f4 2943 live_range_t r;
47dd2e78 2944 ira_allocno_iterator ai;
2945
2946 n = 0;
2947 FOR_EACH_ALLOCNO (a, ai)
ae9587ed 2948 {
2949 ira_object_t obj = ALLOCNO_OBJECT (a);
2950 n += OBJECT_NUM_CONFLICTS (obj);
2951 }
47dd2e78 2952 nr = 0;
2953 FOR_EACH_ALLOCNO (a, ai)
2954 for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
2955 nr++;
2956 fprintf (ira_dump_file, " regions=%d, blocks=%d, points=%d\n",
2957 VEC_length (loop_p, ira_loops.larray), n_basic_blocks,
2958 ira_max_point);
2959 fprintf (ira_dump_file,
2960 " allocnos=%d, copies=%d, conflicts=%d, ranges=%d\n",
2961 ira_allocnos_num, ira_copies_num, n, nr);
2962 }
2963 return loops_p;
2964}
2965
2966/* Release the data created by function ira_build. */
2967void
2968ira_destroy (void)
2969{
2970 finish_loop_tree_nodes ();
2971 finish_copies ();
2972 finish_allocnos ();
2973 finish_cost_vectors ();
2974 ira_finish_allocno_live_ranges ();
2975}