2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ira-build.c
blobfa651fb7ef947cc4867387a26aeecdca6cc035a5
1 /* Building internal representation for IRA.
2 Copyright (C) 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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"
35 #include "toplev.h"
36 #include "params.h"
37 #include "df.h"
38 #include "output.h"
39 #include "reload.h"
40 #include "sparseset.h"
41 #include "ira-int.h"
43 static void create_loop_tree_nodes (int);
44 static void finish_loop_tree_node (loop_tree_node_t);
45 static void finish_loop_tree_nodes (void);
46 static void add_loop_to_tree (struct loop *);
47 static int setup_loop_tree_level (loop_tree_node_t, int);
48 static void form_loop_tree (void);
50 static void rebuild_regno_allocno_maps (void);
52 static void initiate_calls (void);
53 static void expand_calls (void);
54 static void compress_calls (void);
55 static void finish_calls (void);
57 static void initiate_allocnos (void);
58 static void allocate_allocno_conflict_bit_vec (allocno_t);
59 static void add_to_allocno_conflicts (allocno_t, allocno_t);
60 static void clear_allocno_conflicts (allocno_t a);
61 static void compress_allocno_conflict_vec (allocno_t);
62 static void compress_conflict_vecs (void);
63 static allocno_t create_cap_allocno (allocno_t);
64 static void propagate_info_to_cap (allocno_t);
65 static allocno_live_range_t copy_allocno_live_range (allocno_live_range_t);
66 static allocno_live_range_t
67 copy_allocno_live_range_list (allocno_live_range_t);
68 static void finish_allocno (allocno_t);
69 static void finish_allocnos (void);
71 static void initiate_copies (void);
72 static copy_t find_allocno_copy (allocno_t, allocno_t, rtx, loop_tree_node_t);
73 static void print_allocno_copies (FILE *, allocno_t);
74 static void finish_copy (copy_t);
75 static void finish_copies (void);
77 static void initiate_cost_vectors (void);
78 static void finish_cost_vectors (void);
80 static void create_insn_allocnos (rtx, int);
81 static void create_bb_allocnos (loop_tree_node_t);
82 static void create_loop_allocnos (edge);
83 static void create_loop_tree_node_allocnos (loop_tree_node_t);
84 static void create_allocnos (void);
86 static void setup_min_max_allocno_live_range_point (void);
87 static int allocno_range_compare_func (const void *, const void *);
88 static void sort_conflict_id_allocno_map (void);
89 static void setup_min_max_conflict_allocno_ids (void);
91 static void create_loop_tree_node_caps (loop_tree_node_t);
92 static void propagate_info_to_loop_tree_node_caps (loop_tree_node_t);
93 static allocno_live_range_t merge_ranges (allocno_live_range_t,
94 allocno_live_range_t);
95 static loop_tree_node_t common_loop_tree_node_dominator (loop_tree_node_t,
96 loop_tree_node_t);
97 static void change_allocno_in_range_list (allocno_live_range_t, allocno_t);
100 /* The root of the loop tree corresponding to the all function. */
101 loop_tree_node_t ira_loop_tree_root;
103 /* Height of the loop tree. */
104 int ira_loop_tree_height;
106 /* All nodes representing basic blocks are referred through the
107 following array. We can not use basic block member `aux' for this
108 because it is used for insertion of insns on edges. */
109 loop_tree_node_t ira_bb_nodes;
111 /* All nodes representing loops are referred through the following
112 array. */
113 loop_tree_node_t ira_loop_nodes;
115 /* Map regno -> allocnos with given regno (see comments for
116 allocno member `next_regno_allocno'). */
117 allocno_t *regno_allocno_map;
119 /* Array of references to all allocnos. The order number of the
120 allocno corresponds to the index in the array. Removed allocnos
121 have NULL element value. */
122 allocno_t *allocnos;
124 /* Sizes of the previous array. */
125 int allocnos_num;
127 /* Map conflict id -> allocno with given conflict id (see comments for
128 allocno member `conflict_id'). */
129 allocno_t *conflict_id_allocno_map;
131 /* Array of references to all copies. The order number of the copy
132 corresponds to the index in the array. Removed copies have NULL
133 element value. */
134 copy_t *copies;
136 /* Size of the previous array. */
137 int copies_num;
139 /* Bitmap of allocnos used for different purposes. */
140 static bitmap allocnos_bitmap;
144 /* LAST_BASIC_BLOCK before generating additional insns because of live
145 range splitting. Emitting insns on a critical edge creates a new
146 basic block. */
147 static int last_basic_block_before_change;
149 /* The following function allocates the loop tree nodes. If LOOPS_P
150 is FALSE, the nodes corresponding to the loops (except the root
151 which corresponds the all function) will be not allocated but nodes
152 will still be allocated for basic blocks. */
153 static void
154 create_loop_tree_nodes (int loops_p)
156 unsigned int i, j;
157 int max_regno, skip_p;
158 edge_iterator ei;
159 edge e;
160 VEC (edge, heap) *edges;
161 loop_p loop;
163 ira_bb_nodes
164 = ira_allocate (sizeof (struct loop_tree_node) * last_basic_block);
165 last_basic_block_before_change = last_basic_block;
166 for (i = 0; i < (unsigned int) last_basic_block; i++)
168 ira_bb_nodes[i].regno_allocno_map = NULL;
169 memset (ira_bb_nodes[i].reg_pressure, 0,
170 sizeof (ira_bb_nodes[i].reg_pressure));
171 ira_bb_nodes[i].mentioned_allocnos = NULL;
172 ira_bb_nodes[i].modified_regnos = NULL;
173 ira_bb_nodes[i].border_allocnos = NULL;
174 ira_bb_nodes[i].local_copies = NULL;
176 ira_loop_nodes = ira_allocate (sizeof (struct loop_tree_node)
177 * VEC_length (loop_p, ira_loops.larray));
178 max_regno = max_reg_num ();
179 for (i = 0; VEC_iterate (loop_p, ira_loops.larray, i, loop); i++)
181 if (loop != ira_loops.tree_root)
183 ira_loop_nodes[i].regno_allocno_map = NULL;
184 if (! loops_p)
185 continue;
186 skip_p = FALSE;
187 FOR_EACH_EDGE (e, ei, loop->header->preds)
188 if (e->src != loop->latch
189 && (e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
191 skip_p = TRUE;
192 break;
194 if (skip_p)
195 continue;
196 edges = get_loop_exit_edges (loop);
197 for (j = 0; VEC_iterate (edge, edges, j, e); j++)
198 if ((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
200 skip_p = TRUE;
201 break;
203 VEC_free (edge, heap, edges);
204 if (skip_p)
205 continue;
207 ira_loop_nodes[i].regno_allocno_map
208 = ira_allocate (sizeof (allocno_t) * max_regno);
209 memset (ira_loop_nodes[i].regno_allocno_map, 0,
210 sizeof (allocno_t) * max_regno);
211 memset (ira_loop_nodes[i].reg_pressure, 0,
212 sizeof (ira_loop_nodes[i].reg_pressure));
213 ira_loop_nodes[i].mentioned_allocnos = ira_allocate_bitmap ();
214 ira_loop_nodes[i].modified_regnos = ira_allocate_bitmap ();
215 ira_loop_nodes[i].border_allocnos = ira_allocate_bitmap ();
216 ira_loop_nodes[i].local_copies = ira_allocate_bitmap ();
220 /* The function frees the loop tree node of a loop. */
221 static void
222 finish_loop_tree_node (loop_tree_node_t loop)
224 if (loop->regno_allocno_map != NULL)
226 ira_assert (loop->bb == NULL);
227 ira_free_bitmap (loop->local_copies);
228 ira_free_bitmap (loop->border_allocnos);
229 ira_free_bitmap (loop->modified_regnos);
230 ira_free_bitmap (loop->mentioned_allocnos);
231 ira_free (loop->regno_allocno_map);
232 loop->regno_allocno_map = NULL;
236 /* The function frees the loop tree nodes. */
237 static void
238 finish_loop_tree_nodes (void)
240 unsigned int i;
241 loop_p loop;
243 for (i = 0; VEC_iterate (loop_p, ira_loops.larray, i, loop); i++)
244 finish_loop_tree_node (&ira_loop_nodes[i]);
245 ira_free (ira_loop_nodes);
246 for (i = 0; i < (unsigned int) last_basic_block_before_change; i++)
248 if (ira_bb_nodes[i].local_copies != NULL)
249 ira_free_bitmap (ira_bb_nodes[i].local_copies);
250 if (ira_bb_nodes[i].border_allocnos != NULL)
251 ira_free_bitmap (ira_bb_nodes[i].border_allocnos);
252 if (ira_bb_nodes[i].modified_regnos != NULL)
253 ira_free_bitmap (ira_bb_nodes[i].modified_regnos);
254 if (ira_bb_nodes[i].mentioned_allocnos != NULL)
255 ira_free_bitmap (ira_bb_nodes[i].mentioned_allocnos);
256 if (ira_bb_nodes[i].regno_allocno_map != NULL)
257 ira_free (ira_bb_nodes[i].regno_allocno_map);
259 ira_free (ira_bb_nodes);
264 /* The following recursive function adds loop to the loop tree
265 hierarchy. The loop is added only once. */
266 static void
267 add_loop_to_tree (struct loop *loop)
269 struct loop *father;
270 loop_tree_node_t loop_node, father_node;
272 /* We can not use loop node access macros here because of potential
273 checking and because the nodes are not initialized enough
274 yet. */
275 if (loop_outer (loop) != NULL)
276 add_loop_to_tree (loop_outer (loop));
277 if (ira_loop_nodes[loop->num].regno_allocno_map != NULL
278 && ira_loop_nodes[loop->num].children == NULL)
280 /* We have not added loop node to the tree yet. */
281 loop_node = &ira_loop_nodes[loop->num];
282 loop_node->loop = loop;
283 loop_node->bb = NULL;
284 for (father = loop_outer (loop);
285 father != NULL;
286 father = loop_outer (father))
287 if (ira_loop_nodes[father->num].regno_allocno_map != NULL)
288 break;
289 if (father == NULL)
291 loop_node->next = NULL;
292 loop_node->subloop_next = NULL;
293 loop_node->father = NULL;
295 else
297 father_node = &ira_loop_nodes[father->num];
298 loop_node->next = father_node->children;
299 father_node->children = loop_node;
300 loop_node->subloop_next = father_node->subloops;
301 father_node->subloops = loop_node;
302 loop_node->father = father_node;
307 /* The recursive function sets up levels of nodes of the tree given
308 its root LOOP_NODE. The enumeration starts with LEVEL. The
309 function returns maximal value of level in the tree + 1. */
310 static int
311 setup_loop_tree_level (loop_tree_node_t loop_node, int level)
313 int height, max_height;
314 loop_tree_node_t subloop_node;
316 ira_assert (loop_node->bb == NULL);
317 loop_node->level = level;
318 max_height = level + 1;
319 for (subloop_node = loop_node->subloops;
320 subloop_node != NULL;
321 subloop_node = subloop_node->subloop_next)
323 ira_assert (subloop_node->bb == NULL);
324 height = setup_loop_tree_level (subloop_node, level + 1);
325 if (height > max_height)
326 max_height = height;
328 return max_height;
331 /* The following function creates the loop tree. The algorithm is
332 designed to provide correct order of loops (they are ordered by
333 their last loop BB) and basic blocks in the chain formed by member
334 next. */
335 static void
336 form_loop_tree (void)
338 unsigned int i;
339 basic_block bb;
340 struct loop *father;
341 loop_tree_node_t bb_node, loop_node;
342 loop_p loop;
344 /* We can not use loop/bb node access macros because of potential
345 checking and because the nodes are not initialized enough
346 yet. */
347 for (i = 0; VEC_iterate (loop_p, ira_loops.larray, i, loop); i++)
348 if (ira_loop_nodes[i].regno_allocno_map != NULL)
350 ira_loop_nodes[i].children = NULL;
351 ira_loop_nodes[i].subloops = NULL;
353 FOR_EACH_BB_REVERSE (bb)
355 bb_node = &ira_bb_nodes[bb->index];
356 bb_node->bb = bb;
357 bb_node->loop = NULL;
358 bb_node->subloops = NULL;
359 bb_node->children = NULL;
360 bb_node->subloop_next = NULL;
361 bb_node->next = NULL;
362 for (father = bb->loop_father;
363 father != NULL;
364 father = loop_outer (father))
365 if (ira_loop_nodes[father->num].regno_allocno_map != NULL)
366 break;
367 add_loop_to_tree (father);
368 loop_node = &ira_loop_nodes[father->num];
369 bb_node->next = loop_node->children;
370 bb_node->father = loop_node;
371 loop_node->children = bb_node;
373 ira_loop_tree_root = IRA_LOOP_NODE_BY_INDEX (ira_loops.tree_root->num);
374 ira_loop_tree_height = setup_loop_tree_level (ira_loop_tree_root, 0);
375 ira_assert (ira_loop_tree_root->regno_allocno_map != NULL);
380 /* The function rebuilds REGNO_ALLOCNO_MAP and REGNO_ALLOCNO_MAPs of
381 the loops using. */
382 static void
383 rebuild_regno_allocno_maps (void)
385 unsigned int l;
386 int max_regno, regno;
387 allocno_t a;
388 loop_tree_node_t loop_tree_node;
389 loop_p loop;
390 allocno_iterator ai;
392 max_regno = max_reg_num ();
393 for (l = 0; VEC_iterate (loop_p, ira_loops.larray, l, loop); l++)
394 if (ira_loop_nodes[l].regno_allocno_map != NULL)
396 ira_free (ira_loop_nodes[l].regno_allocno_map);
397 ira_loop_nodes[l].regno_allocno_map
398 = ira_allocate (sizeof (allocno_t) * max_regno);
399 memset (ira_loop_nodes[l].regno_allocno_map, 0,
400 sizeof (allocno_t) * max_regno);
402 ira_free (regno_allocno_map);
403 regno_allocno_map = ira_allocate (max_regno * sizeof (allocno_t));
404 memset (regno_allocno_map, 0, max_regno * sizeof (allocno_t));
405 FOR_EACH_ALLOCNO (a, ai)
407 if (ALLOCNO_CAP_MEMBER (a) != NULL)
408 /* Caps are not in the regno allocno maps. */
409 continue;
410 regno = ALLOCNO_REGNO (a);
411 loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
412 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = regno_allocno_map[regno];
413 regno_allocno_map[regno] = a;
414 if (loop_tree_node->regno_allocno_map[regno] == NULL)
415 /* Remember that we can create temporary allocnos to break
416 cycles in register shuffle. */
417 loop_tree_node->regno_allocno_map[regno] = a;
423 /* Array of vectors containing calls given pseudo-register lives
424 through. */
425 VEC(rtx, heap) **regno_calls;
427 /* The length of the previous array. */
428 static int regno_calls_num;
430 /* The function initializes array of vectors containing calls
431 intersected by live ranges of pseudo-registers. */
432 static void
433 initiate_calls (void)
435 regno_calls_num = max_reg_num () * 3 / 2;
436 regno_calls = ira_allocate (regno_calls_num * sizeof (VEC(rtx, heap) *));
437 memset (regno_calls, 0, regno_calls_num * sizeof (VEC(rtx, heap) *));
440 /* The function expands array of the vectors containing calls for
441 pseudo-registers. */
442 static void
443 expand_calls (void)
445 int max_regno = max_reg_num ();
447 if (max_regno > regno_calls_num)
449 regno_calls = ira_reallocate (regno_calls,
450 max_regno * sizeof (VEC(rtx, heap) *));
451 memset (regno_calls + regno_calls_num, 0,
452 (max_regno - regno_calls_num) * sizeof (VEC(rtx, heap) *));
453 regno_calls_num = max_regno;
457 /* The function removes NULL elements from vectors containing calls
458 intersected by live ranges of pseudo-registers. */
459 static void
460 compress_calls (void)
462 int regno, curr, length, last;
463 rtx *allocno_calls;
465 for (regno = 0; regno < regno_calls_num; regno++)
467 allocno_calls = VEC_address (rtx, regno_calls[regno]);
468 length = VEC_length (rtx, regno_calls[regno]);
469 for (last = curr = 0; curr < length; curr++)
470 if (allocno_calls[curr] != NULL_RTX)
471 allocno_calls[last++] = allocno_calls[curr];
472 VEC_truncate (rtx, regno_calls[regno], last);
476 /* The function adds CALL to REGNO's vector of intersected calls and
477 returns the element index in the vector. */
479 add_regno_call (int regno, rtx call)
481 int result;
483 gcc_assert (regno < regno_calls_num);
484 if (regno_calls[regno] == NULL)
485 regno_calls[regno] = VEC_alloc (rtx, heap, 10);
486 result = VEC_length (rtx, regno_calls[regno]);
487 VEC_safe_push (rtx, heap, regno_calls[regno], call);
488 return result;
491 /* The function frees array of vectors containing calls intersected by
492 live ranges of pseudo-registers. */
493 static void
494 finish_calls (void)
496 int i;
498 for (i = 0; i < regno_calls_num; i++)
499 VEC_free (rtx, heap, regno_calls[i]);
500 ira_free (regno_calls);
505 /* Pools for allocnos and allocno live ranges. */
506 static alloc_pool allocno_pool, allocno_live_range_pool;
508 /* Vec containing references to all created allocnos. It is a
509 container of array allocnos. */
510 static VEC(allocno_t,heap) *allocno_vec;
512 /* Vec containing references to all created allocnos. It is a
513 container of conflict_id_allocno_map. */
514 static VEC(allocno_t,heap) *conflict_id_allocno_map_vec;
516 /* The function initializes data concerning allocnos. */
517 static void
518 initiate_allocnos (void)
520 allocno_live_range_pool
521 = create_alloc_pool ("allocno live ranges",
522 sizeof (struct allocno_live_range), 100);
523 allocno_pool = create_alloc_pool ("allocnos", sizeof (struct allocno), 100);
524 allocno_vec = VEC_alloc (allocno_t, heap, max_reg_num () * 2);
525 allocnos = NULL;
526 allocnos_num = 0;
527 conflict_id_allocno_map_vec
528 = VEC_alloc (allocno_t, heap, max_reg_num () * 2);
529 conflict_id_allocno_map = NULL;
530 regno_allocno_map = ira_allocate (max_reg_num () * sizeof (allocno_t));
531 memset (regno_allocno_map, 0, max_reg_num () * sizeof (allocno_t));
534 /* The function creates and returns allocno corresponding to REGNO in
535 LOOP_TREE_NODE. Add the allocno to the list of allocnos with the
536 same regno if CAP_P is FALSE. */
537 allocno_t
538 create_allocno (int regno, int cap_p, loop_tree_node_t loop_tree_node)
540 allocno_t a;
542 a = pool_alloc (allocno_pool);
543 ALLOCNO_REGNO (a) = regno;
544 ALLOCNO_LOOP_TREE_NODE (a) = loop_tree_node;
545 if (! cap_p)
547 ALLOCNO_NEXT_REGNO_ALLOCNO (a) = regno_allocno_map[regno];
548 regno_allocno_map[regno] = a;
549 if (loop_tree_node->regno_allocno_map[regno] == NULL)
550 /* Remember that we can create temporary allocnos to break
551 cycles in register shuffle on region borders (see
552 ira-emit.c). */
553 loop_tree_node->regno_allocno_map[regno] = a;
555 ALLOCNO_CAP (a) = NULL;
556 ALLOCNO_CAP_MEMBER (a) = NULL;
557 ALLOCNO_NUM (a) = allocnos_num;
558 ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a) = NULL;
559 ALLOCNO_CONFLICT_ALLOCNOS_NUM (a) = 0;
560 COPY_HARD_REG_SET (ALLOCNO_CONFLICT_HARD_REGS (a), no_alloc_regs);
561 COPY_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a), no_alloc_regs);
562 ALLOCNO_NREFS (a) = 0;
563 ALLOCNO_FREQ (a) = 1;
564 ALLOCNO_HARD_REGNO (a) = -1;
565 ALLOCNO_CALL_FREQ (a) = 0;
566 ALLOCNO_CALLS_CROSSED_NUM (a) = 0;
567 ALLOCNO_CALLS_CROSSED_START (a) = -1;
568 #ifdef STACK_REGS
569 ALLOCNO_NO_STACK_REG_P (a) = FALSE;
570 ALLOCNO_TOTAL_NO_STACK_REG_P (a) = FALSE;
571 #endif
572 ALLOCNO_MEM_OPTIMIZED_DEST (a) = NULL;
573 ALLOCNO_MEM_OPTIMIZED_DEST_P (a) = FALSE;
574 ALLOCNO_SOMEWHERE_RENAMED_P (a) = FALSE;
575 ALLOCNO_CHILD_RENAMED_P (a) = FALSE;
576 ALLOCNO_DONT_REASSIGN_P (a) = FALSE;
577 ALLOCNO_IN_GRAPH_P (a) = FALSE;
578 ALLOCNO_ASSIGNED_P (a) = FALSE;
579 ALLOCNO_MAY_BE_SPILLED_P (a) = FALSE;
580 ALLOCNO_SPLAY_REMOVED_P (a) = FALSE;
581 ALLOCNO_CONFLICT_VEC_P (a) = FALSE;
582 ALLOCNO_MODE (a) = (regno < 0 ? VOIDmode : PSEUDO_REGNO_MODE (regno));
583 ALLOCNO_COPIES (a) = NULL;
584 ALLOCNO_HARD_REG_COSTS (a) = NULL;
585 ALLOCNO_CONFLICT_HARD_REG_COSTS (a) = NULL;
586 ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
587 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
588 ALLOCNO_LEFT_CONFLICTS_NUM (a) = -1;
589 ALLOCNO_COVER_CLASS (a) = NO_REGS;
590 ALLOCNO_COVER_CLASS_COST (a) = 0;
591 ALLOCNO_MEMORY_COST (a) = 0;
592 ALLOCNO_UPDATED_MEMORY_COST (a) = 0;
593 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) = 0;
594 ALLOCNO_NEXT_BUCKET_ALLOCNO (a) = NULL;
595 ALLOCNO_PREV_BUCKET_ALLOCNO (a) = NULL;
596 ALLOCNO_FIRST_COALESCED_ALLOCNO (a) = a;
597 ALLOCNO_NEXT_COALESCED_ALLOCNO (a) = a;
598 ALLOCNO_LIVE_RANGES (a) = NULL;
599 ALLOCNO_MIN (a) = INT_MAX;
600 ALLOCNO_MAX (a) = -1;
601 ALLOCNO_CONFLICT_ID (a) = allocnos_num;
602 VEC_safe_push (allocno_t, heap, allocno_vec, a);
603 allocnos = VEC_address (allocno_t, allocno_vec);
604 allocnos_num = VEC_length (allocno_t, allocno_vec);
605 VEC_safe_push (allocno_t, heap, conflict_id_allocno_map_vec, a);
606 conflict_id_allocno_map
607 = VEC_address (allocno_t, conflict_id_allocno_map_vec);
608 return a;
611 /* Set up cover class for A and update its conflict hard registers. */
612 void
613 set_allocno_cover_class (allocno_t a, enum reg_class cover_class)
615 ALLOCNO_COVER_CLASS (a) = cover_class;
616 IOR_COMPL_HARD_REG_SET (ALLOCNO_CONFLICT_HARD_REGS (a),
617 reg_class_contents[cover_class]);
618 IOR_COMPL_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a),
619 reg_class_contents[cover_class]);
622 /* The function returns TRUE if conflict vector with NUM elements is
623 more profitable than conflict bit vector for A. */
625 conflict_vector_profitable_p (allocno_t a, int num)
627 int nw;
629 if (ALLOCNO_MAX (a) < ALLOCNO_MIN (a))
630 /* We prefer bit vector in such case because it does not result in
631 allocation. */
632 return FALSE;
634 nw = (ALLOCNO_MAX (a) - ALLOCNO_MIN (a) + INT_BITS) / INT_BITS;
635 return 2 * sizeof (allocno_t) * (num + 1) < 3 * nw * sizeof (INT_TYPE);
638 /* The function allocates and initializes conflict vector of A for NUM
639 conflicting allocnos. */
640 void
641 allocate_allocno_conflict_vec (allocno_t a, int num)
643 int size;
644 allocno_t *vec;
646 ira_assert (ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a) == NULL);
647 num++; /* for NULL end marker */
648 size = sizeof (allocno_t) * num;
649 vec = ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a) = ira_allocate (size);
650 vec[0] = NULL;
651 ALLOCNO_CONFLICT_ALLOCNOS_NUM (a) = 0;
652 ALLOCNO_CONFLICT_ALLOCNO_ARRAY_SIZE (a) = size;
653 ALLOCNO_CONFLICT_VEC_P (a) = TRUE;
656 /* The function allocates and initializes conflict bit vector of A. */
657 static void
658 allocate_allocno_conflict_bit_vec (allocno_t a)
660 unsigned int size;
662 ira_assert (ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a) == NULL);
663 size = ((ALLOCNO_MAX (a) - ALLOCNO_MIN (a) + INT_BITS)
664 / INT_BITS * sizeof (INT_TYPE));
665 ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a) = ira_allocate (size);
666 memset (ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a), 0, size);
667 ALLOCNO_CONFLICT_ALLOCNO_ARRAY_SIZE (a) = size;
668 ALLOCNO_CONFLICT_VEC_P (a) = FALSE;
671 /* The function allocates and initializes conflict vector or conflict
672 bit vector of A for NUM conflicting allocnos whatever is more
673 profitable. */
674 void
675 allocate_allocno_conflicts (allocno_t a, int num)
677 if (conflict_vector_profitable_p (a, num))
678 allocate_allocno_conflict_vec (a, num);
679 else
680 allocate_allocno_conflict_bit_vec (a);
683 /* The function adds A2 to the conflicts of A1. */
684 static void
685 add_to_allocno_conflicts (allocno_t a1, allocno_t a2)
687 int num;
688 unsigned int size;
690 if (ALLOCNO_CONFLICT_VEC_P (a1))
692 allocno_t *vec;
694 num = ALLOCNO_CONFLICT_ALLOCNOS_NUM (a1) + 2;
695 if (ALLOCNO_CONFLICT_ALLOCNO_ARRAY_SIZE (a1)
696 >= num * sizeof (allocno_t))
697 vec = ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a1);
698 else
700 size = (3 * num / 2 + 1) * sizeof (allocno_t);
701 vec = ira_allocate (size);
702 memcpy (vec, ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a1),
703 sizeof (allocno_t) * ALLOCNO_CONFLICT_ALLOCNOS_NUM (a1));
704 ira_free (ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a1));
705 ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a1) = vec;
706 ALLOCNO_CONFLICT_ALLOCNO_ARRAY_SIZE (a1) = size;
708 vec[num - 2] = a2;
709 vec[num - 1] = NULL;
710 ALLOCNO_CONFLICT_ALLOCNOS_NUM (a1)++;
712 else
714 int nw, added_head_nw, id;
715 INT_TYPE *vec;
717 id = ALLOCNO_CONFLICT_ID (a2);
718 vec = ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a1);
719 if (ALLOCNO_MIN (a1) > id)
721 /* Expand head of the bit vector. */
722 added_head_nw = (ALLOCNO_MIN (a1) - id - 1) / INT_BITS + 1;
723 nw = (ALLOCNO_MAX (a1) - ALLOCNO_MIN (a1)) / INT_BITS + 1;
724 size = (nw + added_head_nw) * sizeof (INT_TYPE);
725 if (ALLOCNO_CONFLICT_ALLOCNO_ARRAY_SIZE (a1) >= size)
727 memmove ((char *) vec + added_head_nw * sizeof (INT_TYPE),
728 vec, nw * sizeof (INT_TYPE));
729 memset (vec, 0, added_head_nw * sizeof (INT_TYPE));
731 else
733 size = (3 * (nw + added_head_nw) / 2 + 1) * sizeof (INT_TYPE);
734 vec = ira_allocate (size);
735 memcpy
736 ((char *) vec + added_head_nw * sizeof (INT_TYPE),
737 ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a1), nw * sizeof (INT_TYPE));
738 memset (vec, 0, added_head_nw * sizeof (INT_TYPE));
739 memset ((char *) vec + (nw + added_head_nw) * sizeof (INT_TYPE),
740 0, size - (nw + added_head_nw) * sizeof (INT_TYPE));
741 ira_free (ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a1));
742 ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a1) = vec;
743 ALLOCNO_CONFLICT_ALLOCNO_ARRAY_SIZE (a1) = size;
745 ALLOCNO_MIN (a1) -= added_head_nw * INT_BITS;
747 else if (ALLOCNO_MAX (a1) < id)
749 nw = (id - ALLOCNO_MIN (a1)) / INT_BITS + 1;
750 size = nw * sizeof (INT_TYPE);
751 if (ALLOCNO_CONFLICT_ALLOCNO_ARRAY_SIZE (a1) < size)
753 /* Expand tail of the bit vector. */
754 size = (3 * nw / 2 + 1) * sizeof (INT_TYPE);
755 vec = ira_allocate (size);
756 memcpy (vec, ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a1),
757 ALLOCNO_CONFLICT_ALLOCNO_ARRAY_SIZE (a1));
758 memset ((char *) vec + ALLOCNO_CONFLICT_ALLOCNO_ARRAY_SIZE (a1),
759 0, size - ALLOCNO_CONFLICT_ALLOCNO_ARRAY_SIZE (a1));
760 ira_free (ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a1));
761 ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a1) = vec;
762 ALLOCNO_CONFLICT_ALLOCNO_ARRAY_SIZE (a1) = size;
764 ALLOCNO_MAX (a1) = id;
766 SET_ALLOCNO_SET_BIT (vec, id, ALLOCNO_MIN (a1), ALLOCNO_MAX (a1));
770 /* The function adds A1 to the conflicts of A2 and vise versa. */
771 void
772 add_allocno_conflict (allocno_t a1, allocno_t a2)
774 add_to_allocno_conflicts (a1, a2);
775 add_to_allocno_conflicts (a2, a1);
778 /* The function clears all conflicts of allocno A. */
779 static void
780 clear_allocno_conflicts (allocno_t a)
782 if (ALLOCNO_CONFLICT_VEC_P (a))
784 ALLOCNO_CONFLICT_ALLOCNOS_NUM (a) = 0;
785 ((allocno_t *) ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a))[0] = NULL;
787 else if (ALLOCNO_CONFLICT_ALLOCNO_ARRAY_SIZE (a) != 0)
789 int nw;
791 nw = (ALLOCNO_MAX (a) - ALLOCNO_MIN (a)) / INT_BITS + 1;
792 memset (ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a), 0, nw * sizeof (INT_TYPE));
796 /* The array used to find duplications in conflict vectors of
797 allocnos. */
798 static int *allocno_conflict_check;
800 /* The value used to mark allocation presence in conflict vector of
801 the current allocno. */
802 static int curr_allocno_conflict_check_tick;
804 /* The function removes duplications in conflict vector of A. */
805 static void
806 compress_allocno_conflict_vec (allocno_t a)
808 allocno_t *vec, conflict_a;
809 int i, j;
811 ira_assert (ALLOCNO_CONFLICT_VEC_P (a));
812 vec = ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a);
813 curr_allocno_conflict_check_tick++;
814 for (i = j = 0; (conflict_a = vec[i]) != NULL; i++)
816 if (allocno_conflict_check[ALLOCNO_NUM (conflict_a)]
817 != curr_allocno_conflict_check_tick)
819 allocno_conflict_check[ALLOCNO_NUM (conflict_a)]
820 = curr_allocno_conflict_check_tick;
821 vec[j++] = conflict_a;
824 ALLOCNO_CONFLICT_ALLOCNOS_NUM (a) = j;
825 vec[j] = NULL;
828 /* The function removes duplications in conflict vectors of all
829 allocnos. */
830 static void
831 compress_conflict_vecs (void)
833 allocno_t a;
834 allocno_iterator ai;
836 allocno_conflict_check = ira_allocate (sizeof (int) * allocnos_num);
837 memset (allocno_conflict_check, 0, sizeof (int) * allocnos_num);
838 curr_allocno_conflict_check_tick = 0;
839 FOR_EACH_ALLOCNO (a, ai)
840 if (ALLOCNO_CONFLICT_VEC_P (a))
841 compress_allocno_conflict_vec (a);
842 ira_free (allocno_conflict_check);
845 /* This recursive function outputs allocno A and if it is a cap the
846 function outputs its members. */
847 void
848 print_expanded_allocno (allocno_t a)
850 basic_block bb;
852 fprintf (ira_dump_file, " a%d(r%d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
853 if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
854 fprintf (ira_dump_file, ",b%d", bb->index);
855 else
856 fprintf (ira_dump_file, ",l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop->num);
857 if (ALLOCNO_CAP_MEMBER (a) != NULL)
859 fprintf (ira_dump_file, ":");
860 print_expanded_allocno (ALLOCNO_CAP_MEMBER (a));
862 fprintf (ira_dump_file, ")");
865 /* The function creates and returns cap representing allocno A in the
866 father loop. */
867 static allocno_t
868 create_cap_allocno (allocno_t a)
870 allocno_t cap;
871 loop_tree_node_t father;
873 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (a) == a
874 && ALLOCNO_NEXT_COALESCED_ALLOCNO (a) == a);
875 father = ALLOCNO_LOOP_TREE_NODE (a)->father;
876 cap = create_allocno (ALLOCNO_REGNO (a), TRUE, father);
877 ALLOCNO_MODE (cap) = ALLOCNO_MODE (a);
878 set_allocno_cover_class (cap, ALLOCNO_COVER_CLASS (a));
879 ALLOCNO_AVAILABLE_REGS_NUM (cap) = ALLOCNO_AVAILABLE_REGS_NUM (a);
880 ALLOCNO_CAP_MEMBER (cap) = a;
881 bitmap_set_bit (father->mentioned_allocnos, ALLOCNO_NUM (cap));
882 ALLOCNO_CAP (a) = cap;
883 ALLOCNO_COVER_CLASS_COST (cap) = ALLOCNO_COVER_CLASS_COST (a);
884 ALLOCNO_MEMORY_COST (cap) = ALLOCNO_MEMORY_COST (a);
885 ALLOCNO_UPDATED_MEMORY_COST (cap) = ALLOCNO_UPDATED_MEMORY_COST (a);
886 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
888 fprintf (ira_dump_file, " Creating cap ");
889 print_expanded_allocno (cap);
890 fprintf (ira_dump_file, "\n");
892 return cap;
895 /* The function propagates info to CAP from its member. We propagate
896 info which is not available at the cap creation time. */
897 static void
898 propagate_info_to_cap (allocno_t cap)
900 int regno, conflicts_num;
901 enum reg_class cover_class;
902 allocno_t a, conflict_allocno, conflict_father_allocno;
903 allocno_t another_a, father_a;
904 loop_tree_node_t father;
905 copy_t cp, next_cp;
906 allocno_conflict_iterator aci;
908 ira_assert (ALLOCNO_FIRST_COALESCED_ALLOCNO (cap) == cap
909 && ALLOCNO_NEXT_COALESCED_ALLOCNO (cap) == cap
910 && ALLOCNO_CONFLICT_ALLOCNO_ARRAY (cap) == NULL
911 && ALLOCNO_CALLS_CROSSED_NUM (cap) == 0);
912 a = ALLOCNO_CAP_MEMBER (cap);
913 father = ALLOCNO_LOOP_TREE_NODE (cap);
914 cover_class = ALLOCNO_COVER_CLASS (cap);
915 allocate_and_copy_costs
916 (&ALLOCNO_HARD_REG_COSTS (cap), cover_class, ALLOCNO_HARD_REG_COSTS (a));
917 allocate_and_copy_costs
918 (&ALLOCNO_CONFLICT_HARD_REG_COSTS (cap), cover_class,
919 ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
920 ALLOCNO_NREFS (cap) = ALLOCNO_NREFS (a);
921 ALLOCNO_FREQ (cap) = ALLOCNO_FREQ (a);
922 ALLOCNO_CALL_FREQ (cap) = ALLOCNO_CALL_FREQ (a);
923 IOR_HARD_REG_SET (ALLOCNO_CONFLICT_HARD_REGS (cap),
924 ALLOCNO_CONFLICT_HARD_REGS (a));
925 IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (cap),
926 ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a));
927 ALLOCNO_CALLS_CROSSED_NUM (cap) = ALLOCNO_CALLS_CROSSED_NUM (a);
928 ALLOCNO_CALLS_CROSSED_START (cap) = ALLOCNO_CALLS_CROSSED_START (a);
929 #ifdef STACK_REGS
930 ALLOCNO_NO_STACK_REG_P (cap) = ALLOCNO_NO_STACK_REG_P (a);
931 ALLOCNO_TOTAL_NO_STACK_REG_P (cap) = ALLOCNO_TOTAL_NO_STACK_REG_P (a);
932 #endif
933 /* Add copies to the cap. */
934 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
936 if (cp->first == a)
938 next_cp = cp->next_first_allocno_copy;
939 another_a = cp->second;
941 else if (cp->second == a)
943 next_cp = cp->next_second_allocno_copy;
944 another_a = cp->first;
946 else
947 gcc_unreachable ();
948 father_a = father->regno_allocno_map[ALLOCNO_REGNO (another_a)];
949 if (father_a == NULL)
950 father_a = ALLOCNO_CAP (another_a);
951 if (father_a != NULL
952 && find_allocno_copy (cap, father_a,
953 cp->insn, cp->loop_tree_node) == NULL)
954 /* Upper level allocno might be not existing because it is not
955 mentioned or lived on the region border. It is just living
956 on BB start of the loop. */
957 add_allocno_copy (cap, father_a, cp->freq, cp->insn,
958 cp->loop_tree_node);
960 if (ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a) != NULL)
962 conflicts_num = 0;
963 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_allocno, aci)
964 conflicts_num++;
965 allocate_allocno_conflicts (cap, conflicts_num);
966 FOR_EACH_ALLOCNO_CONFLICT (a, conflict_allocno, aci)
968 regno = ALLOCNO_REGNO (conflict_allocno);
969 conflict_father_allocno = father->regno_allocno_map[regno];
970 if (conflict_father_allocno == NULL)
971 conflict_father_allocno = ALLOCNO_CAP (conflict_allocno);
972 if (conflict_father_allocno != NULL
973 && (ALLOCNO_CONFLICT_ALLOCNO_ARRAY (conflict_father_allocno)
974 != NULL))
975 add_allocno_conflict (cap, conflict_father_allocno);
978 if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
980 fprintf (ira_dump_file, " Propagate info to cap ");
981 print_expanded_allocno (cap);
982 if (ALLOCNO_CONFLICT_ALLOCNO_ARRAY (cap) != NULL)
984 fprintf (ira_dump_file, "\n Conflicts:");
985 FOR_EACH_ALLOCNO_CONFLICT (cap, conflict_allocno, aci)
987 fprintf (ira_dump_file, " a%d(r%d,",
988 ALLOCNO_NUM (conflict_allocno),
989 ALLOCNO_REGNO (conflict_allocno));
990 ira_assert
991 (ALLOCNO_LOOP_TREE_NODE (conflict_allocno)->bb == NULL);
992 fprintf (ira_dump_file, "l%d)",
993 ALLOCNO_LOOP_TREE_NODE (conflict_allocno)->loop->num);
996 fprintf (ira_dump_file, "\n");
1001 /* Create and return allocno live range with given attributes. */
1002 allocno_live_range_t
1003 create_allocno_live_range (allocno_t a, int start, int finish,
1004 allocno_live_range_t next)
1006 allocno_live_range_t p;
1008 p = pool_alloc (allocno_live_range_pool);
1009 p->allocno = a;
1010 p->start = start;
1011 p->finish = finish;
1012 p->next = next;
1013 return p;
1016 /* Copy allocno live range R and return the result. */
1017 static allocno_live_range_t
1018 copy_allocno_live_range (allocno_live_range_t r)
1020 allocno_live_range_t p;
1022 p = pool_alloc (allocno_live_range_pool);
1023 *p = *r;
1024 return p;
1027 /* Copy allocno live range list given by its head R and return the
1028 result. */
1029 static allocno_live_range_t
1030 copy_allocno_live_range_list (allocno_live_range_t r)
1032 allocno_live_range_t p, first, last;
1034 if (r == NULL)
1035 return NULL;
1036 for (first = last = NULL; r != NULL; r = r->next)
1038 p = copy_allocno_live_range (r);
1039 if (first == NULL)
1040 first = p;
1041 else
1042 last->next = p;
1043 last = p;
1045 return first;
1048 /* Free allocno live range R. */
1049 void
1050 finish_allocno_live_range (allocno_live_range_t r)
1052 pool_free (allocno_live_range_pool, r);
1055 /* Free updated register costs of allocno A. */
1056 void
1057 free_allocno_updated_costs (allocno_t a)
1059 enum reg_class cover_class;
1061 cover_class = ALLOCNO_COVER_CLASS (a);
1062 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
1063 free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), cover_class);
1064 ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
1065 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
1066 free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1067 cover_class);
1068 ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
1071 /* The function frees memory allocated for allocno A. */
1072 static void
1073 finish_allocno (allocno_t a)
1075 allocno_live_range_t r, next_r;
1076 enum reg_class cover_class = ALLOCNO_COVER_CLASS (a);
1078 allocnos[ALLOCNO_NUM (a)] = NULL;
1079 conflict_id_allocno_map[ALLOCNO_CONFLICT_ID (a)] = NULL;
1080 if (ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a) != NULL)
1081 ira_free (ALLOCNO_CONFLICT_ALLOCNO_ARRAY (a));
1082 if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
1083 free_cost_vector (ALLOCNO_HARD_REG_COSTS (a), cover_class);
1084 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL)
1085 free_cost_vector (ALLOCNO_CONFLICT_HARD_REG_COSTS (a), cover_class);
1086 if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
1087 free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), cover_class);
1088 if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
1089 free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1090 cover_class);
1091 for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = next_r)
1093 next_r = r->next;
1094 finish_allocno_live_range (r);
1096 pool_free (allocno_pool, a);
1099 /* The function frees memory allocated for all allocnos. */
1100 static void
1101 finish_allocnos (void)
1103 allocno_t a;
1104 allocno_iterator ai;
1106 FOR_EACH_ALLOCNO (a, ai)
1107 finish_allocno (a);
1108 ira_free (regno_allocno_map);
1109 VEC_free (allocno_t, heap, conflict_id_allocno_map_vec);
1110 VEC_free (allocno_t, heap, allocno_vec);
1111 free_alloc_pool (allocno_pool);
1112 free_alloc_pool (allocno_live_range_pool);
1117 /* Pools for copies. */
1118 static alloc_pool copy_pool;
1120 /* Vec containing references to all created copies. It is a
1121 container of array copies. */
1122 static VEC(copy_t,heap) *copy_vec;
1124 /* The function initializes data concerning allocno copies. */
1125 static void
1126 initiate_copies (void)
1128 copy_pool = create_alloc_pool ("copies", sizeof (struct allocno_copy), 100);
1129 copy_vec = VEC_alloc (copy_t, heap, get_max_uid ());
1130 copies = NULL;
1131 copies_num = 0;
1134 /* Return copy connecting A1 and A2 and originated from INSN of
1135 LOOP_TREE_NODE if any. */
1136 static copy_t
1137 find_allocno_copy (allocno_t a1, allocno_t a2, rtx insn,
1138 loop_tree_node_t loop_tree_node)
1140 copy_t cp, next_cp;
1141 allocno_t another_a;
1143 for (cp = ALLOCNO_COPIES (a1); cp != NULL; cp = next_cp)
1145 if (cp->first == a1)
1147 next_cp = cp->next_first_allocno_copy;
1148 another_a = cp->second;
1150 else if (cp->second == a1)
1152 next_cp = cp->next_second_allocno_copy;
1153 another_a = cp->first;
1155 else
1156 gcc_unreachable ();
1157 if (another_a == a2 && cp->insn == insn
1158 && cp->loop_tree_node == loop_tree_node)
1159 return cp;
1161 return NULL;
1164 /* The function creates and returns created in LOOP_TREE_NODE copy of
1165 allocnos FIRST and SECOND with frequency FREQ, insn INSN. */
1166 copy_t
1167 create_copy (allocno_t first, allocno_t second, int freq, rtx insn,
1168 loop_tree_node_t loop_tree_node)
1170 copy_t cp;
1172 cp = pool_alloc (copy_pool);
1173 cp->num = copies_num;
1174 cp->first = first;
1175 cp->second = second;
1176 cp->freq = freq;
1177 cp->insn = insn;
1178 cp->loop_tree_node = loop_tree_node;
1179 VEC_safe_push (copy_t, heap, copy_vec, cp);
1180 copies = VEC_address (copy_t, copy_vec);
1181 copies_num = VEC_length (copy_t, copy_vec);
1182 return cp;
1185 /* The function attaches copy CP to allocnos involved into the copy. */
1186 void
1187 add_allocno_copy_to_list (copy_t cp)
1189 allocno_t first = cp->first, second = cp->second;
1191 cp->prev_first_allocno_copy = NULL;
1192 cp->prev_second_allocno_copy = NULL;
1193 cp->next_first_allocno_copy = ALLOCNO_COPIES (first);
1194 if (cp->next_first_allocno_copy != NULL)
1196 if (cp->next_first_allocno_copy->first == first)
1197 cp->next_first_allocno_copy->prev_first_allocno_copy = cp;
1198 else
1199 cp->next_first_allocno_copy->prev_second_allocno_copy = cp;
1201 cp->next_second_allocno_copy = ALLOCNO_COPIES (second);
1202 if (cp->next_second_allocno_copy != NULL)
1204 if (cp->next_second_allocno_copy->second == second)
1205 cp->next_second_allocno_copy->prev_second_allocno_copy = cp;
1206 else
1207 cp->next_second_allocno_copy->prev_first_allocno_copy = cp;
1209 ALLOCNO_COPIES (first) = cp;
1210 ALLOCNO_COPIES (second) = cp;
1213 /* The function detaches copy CP from allocnos involved into the copy. */
1214 void
1215 remove_allocno_copy_from_list (copy_t cp)
1217 allocno_t first = cp->first, second = cp->second;
1218 copy_t prev, next;
1220 next = cp->next_first_allocno_copy;
1221 prev = cp->prev_first_allocno_copy;
1222 if (prev == NULL)
1223 ALLOCNO_COPIES (first) = next;
1224 else if (prev->first == first)
1225 prev->next_first_allocno_copy = next;
1226 else
1227 prev->next_second_allocno_copy = next;
1228 if (next != NULL)
1230 if (next->first == first)
1231 next->prev_first_allocno_copy = prev;
1232 else
1233 next->prev_second_allocno_copy = prev;
1235 cp->prev_first_allocno_copy = cp->next_first_allocno_copy = NULL;
1237 next = cp->next_second_allocno_copy;
1238 prev = cp->prev_second_allocno_copy;
1239 if (prev == NULL)
1240 ALLOCNO_COPIES (second) = next;
1241 else if (prev->second == second)
1242 prev->next_second_allocno_copy = next;
1243 else
1244 prev->next_first_allocno_copy = next;
1245 if (next != NULL)
1247 if (next->second == second)
1248 next->prev_second_allocno_copy = prev;
1249 else
1250 next->prev_first_allocno_copy = prev;
1252 cp->prev_second_allocno_copy = cp->next_second_allocno_copy = NULL;
1255 /* The function makes copy CP a canonical copy where number of the
1256 first allocno is less than the second one. */
1257 void
1258 swap_allocno_copy_ends_if_necessary (copy_t cp)
1260 allocno_t temp;
1261 copy_t temp_cp;
1263 if (ALLOCNO_NUM (cp->first) <= ALLOCNO_NUM (cp->second))
1264 return;
1266 temp = cp->first;
1267 cp->first = cp->second;
1268 cp->second = temp;
1270 temp_cp = cp->prev_first_allocno_copy;
1271 cp->prev_first_allocno_copy = cp->prev_second_allocno_copy;
1272 cp->prev_second_allocno_copy = temp_cp;
1274 temp_cp = cp->next_first_allocno_copy;
1275 cp->next_first_allocno_copy = cp->next_second_allocno_copy;
1276 cp->next_second_allocno_copy = temp_cp;
1279 /* The function creates (or updates frequency if the copy already
1280 exists) and returns the copy of allocnos FIRST and SECOND with
1281 frequency FREQ corresponding to move insn INSN (if any) and
1282 originated from LOOP_TREE_NODE. */
1283 copy_t
1284 add_allocno_copy (allocno_t first, allocno_t second, int freq, rtx insn,
1285 loop_tree_node_t loop_tree_node)
1287 copy_t cp;
1289 if ((cp = find_allocno_copy (first, second, insn, loop_tree_node)) != NULL)
1291 cp->freq += freq;
1292 return cp;
1294 cp = create_copy (first, second, freq, insn, loop_tree_node);
1295 ira_assert (first != NULL && second != NULL);
1296 add_allocno_copy_to_list (cp);
1297 swap_allocno_copy_ends_if_necessary (cp);
1298 return cp;
1301 /* Print info about copies involving allocno A into file F. */
1302 static void
1303 print_allocno_copies (FILE *f, allocno_t a)
1305 allocno_t another_a;
1306 copy_t cp, next_cp;
1308 fprintf (f, " a%d(r%d):", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
1309 for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1311 if (cp->first == a)
1313 next_cp = cp->next_first_allocno_copy;
1314 another_a = cp->second;
1316 else if (cp->second == a)
1318 next_cp = cp->next_second_allocno_copy;
1319 another_a = cp->first;
1321 else
1322 gcc_unreachable ();
1323 fprintf (f, " cp%d:a%d(r%d)@%d", cp->num,
1324 ALLOCNO_NUM (another_a), ALLOCNO_REGNO (another_a), cp->freq);
1326 fprintf (f, "\n");
1329 /* Print info about copies involving allocno A into stderr. */
1330 void
1331 debug_allocno_copies (allocno_t a)
1333 print_allocno_copies (stderr, a);
1336 /* The function frees memory allocated for copy CP. */
1337 static void
1338 finish_copy (copy_t cp)
1340 pool_free (copy_pool, cp);
1344 /* The function frees memory allocated for all copies. */
1345 static void
1346 finish_copies (void)
1348 copy_t cp;
1349 copy_iterator ci;
1351 FOR_EACH_COPY (cp, ci)
1352 finish_copy (cp);
1353 VEC_free (copy_t, heap, copy_vec);
1354 free_alloc_pool (copy_pool);
1359 /* Pools for cost vectors. It is defined only for cover classes. */
1360 static alloc_pool cost_vector_pool[N_REG_CLASSES];
1362 /* The function initiates work with hard register cost vectors. It
1363 creates allocation pool for each cover class. */
1364 static void
1365 initiate_cost_vectors (void)
1367 int i;
1368 enum reg_class cover_class;
1370 for (i = 0; i < reg_class_cover_size; i++)
1372 cover_class = reg_class_cover[i];
1373 cost_vector_pool[cover_class]
1374 = create_alloc_pool ("cost vectors",
1375 sizeof (int) * class_hard_regs_num[cover_class],
1376 100);
1380 /* The function allocates and returns cost vector VEC for
1381 COVER_CLASS. */
1382 int *
1383 allocate_cost_vector (enum reg_class cover_class)
1385 return pool_alloc (cost_vector_pool[cover_class]);
1388 /* The function frees cost vector VEC for COVER_CLASS. */
1389 void
1390 free_cost_vector (int *vec, enum reg_class cover_class)
1392 ira_assert (vec != NULL);
1393 pool_free (cost_vector_pool[cover_class], vec);
1396 /* The function finishes work with hard register cost vectors. It
1397 releases allocation pool for each cover class. */
1398 static void
1399 finish_cost_vectors (void)
1401 int i;
1402 enum reg_class cover_class;
1404 for (i = 0; i < reg_class_cover_size; i++)
1406 cover_class = reg_class_cover[i];
1407 free_alloc_pool (cost_vector_pool[cover_class]);
1413 /* The current loop tree node and its regno allocno map. */
1414 loop_tree_node_t ira_curr_loop_tree_node;
1415 allocno_t *ira_curr_regno_allocno_map;
1417 /* The recursive function traverses loop tree with root LOOP_NODE
1418 calling non-null functions PREORDER_FUNC and POSTORDER_FUNC
1419 correspondingly in preorder and postorder. The function sets up
1420 IRA_CURR_LOOP_TREE_NODE and IRA_CURR_REGNO_ALLOCNO_MAP. If BB_P,
1421 basic block nodes of LOOP_NODE is also processed (before its
1422 subloop nodes). */
1423 void
1424 traverse_loop_tree (int bb_p, loop_tree_node_t loop_node,
1425 void (*preorder_func) (loop_tree_node_t),
1426 void (*postorder_func) (loop_tree_node_t))
1428 loop_tree_node_t subloop_node;
1430 ira_assert (loop_node->bb == NULL);
1431 ira_curr_loop_tree_node = loop_node;
1432 ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
1434 if (preorder_func != NULL)
1435 (*preorder_func) (loop_node);
1437 if (bb_p)
1438 for (subloop_node = loop_node->children;
1439 subloop_node != NULL;
1440 subloop_node = subloop_node->next)
1441 if (subloop_node->bb != NULL)
1443 if (preorder_func != NULL)
1444 (*preorder_func) (subloop_node);
1446 if (postorder_func != NULL)
1447 (*postorder_func) (subloop_node);
1450 for (subloop_node = loop_node->subloops;
1451 subloop_node != NULL;
1452 subloop_node = subloop_node->subloop_next)
1454 ira_assert (subloop_node->bb == NULL);
1455 traverse_loop_tree (bb_p, subloop_node,
1456 preorder_func, postorder_func);
1459 ira_curr_loop_tree_node = loop_node;
1460 ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
1462 if (postorder_func != NULL)
1463 (*postorder_func) (loop_node);
1468 /* The basic block currently being processed. */
1469 static basic_block curr_bb;
1471 /* This recursive function creates allocnos corresponding to
1472 pseudo-registers containing in X. Nonzero OUTPUT_P means that X is
1473 a lvalue. */
1474 static void
1475 create_insn_allocnos (rtx x, int output_p)
1477 int i, j;
1478 const char *fmt;
1479 enum rtx_code code = GET_CODE (x);
1481 if (code == REG)
1483 int regno;
1485 if ((regno = REGNO (x)) >= FIRST_PSEUDO_REGISTER)
1487 allocno_t a;
1489 if ((a = ira_curr_regno_allocno_map[regno]) == NULL)
1490 a = create_allocno (regno, FALSE, ira_curr_loop_tree_node);
1492 ALLOCNO_NREFS (a)++;
1493 ALLOCNO_FREQ (a) += REG_FREQ_FROM_BB (curr_bb);
1494 bitmap_set_bit (ira_curr_loop_tree_node->mentioned_allocnos,
1495 ALLOCNO_NUM (a));
1496 if (output_p)
1497 bitmap_set_bit (ira_curr_loop_tree_node->modified_regnos, regno);
1499 return;
1501 else if (code == SET)
1503 create_insn_allocnos (SET_DEST (x), TRUE);
1504 create_insn_allocnos (SET_SRC (x), FALSE);
1505 return;
1507 else if (code == CLOBBER)
1509 create_insn_allocnos (XEXP (x, 0), TRUE);
1510 return;
1512 else if (code == MEM)
1514 create_insn_allocnos (XEXP (x, 0), FALSE);
1515 return;
1517 else if (code == PRE_DEC || code == POST_DEC || code == PRE_INC ||
1518 code == POST_INC || code == POST_MODIFY || code == PRE_MODIFY)
1520 create_insn_allocnos (XEXP (x, 0), TRUE);
1521 create_insn_allocnos (XEXP (x, 0), FALSE);
1522 return;
1525 fmt = GET_RTX_FORMAT (code);
1526 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1528 if (fmt[i] == 'e')
1529 create_insn_allocnos (XEXP (x, i), output_p);
1530 else if (fmt[i] == 'E')
1531 for (j = 0; j < XVECLEN (x, i); j++)
1532 create_insn_allocnos (XVECEXP (x, i, j), output_p);
1536 /* The function creates allocnos corresponding to pseudo-registers
1537 living in the basic block represented by the corresponding loop
1538 tree node BB_NODE. */
1539 static void
1540 create_bb_allocnos (loop_tree_node_t bb_node)
1542 basic_block bb;
1543 rtx insn;
1544 unsigned int i;
1545 bitmap_iterator bi;
1547 curr_bb = bb = bb_node->bb;
1548 ira_assert (bb != NULL);
1549 FOR_BB_INSNS (bb, insn)
1550 if (INSN_P (insn))
1551 create_insn_allocnos (PATTERN (insn), FALSE);
1552 /* It might be a allocno living through from one subloop to
1553 another. */
1554 EXECUTE_IF_SET_IN_REG_SET (DF_LR_IN (bb), FIRST_PSEUDO_REGISTER, i, bi)
1555 if (ira_curr_regno_allocno_map[i] == NULL)
1556 create_allocno (i, FALSE, ira_curr_loop_tree_node);
1559 /* The function creates allocnos corresponding to pseudo-registers
1560 living on edge E (a loop entry or exit). It also marks the
1561 allocnos as living on the loop border. */
1562 static void
1563 create_loop_allocnos (edge e)
1565 unsigned int i;
1566 bitmap live_in_regs, border_allocnos;
1567 bitmap_iterator bi;
1569 live_in_regs = DF_LR_IN (e->dest);
1570 border_allocnos = ira_curr_loop_tree_node->border_allocnos;
1571 EXECUTE_IF_SET_IN_REG_SET (DF_LR_OUT (e->src),
1572 FIRST_PSEUDO_REGISTER, i, bi)
1573 if (bitmap_bit_p (live_in_regs, i))
1575 if (ira_curr_regno_allocno_map[i] == NULL)
1576 create_allocno (i, FALSE, ira_curr_loop_tree_node);
1577 bitmap_set_bit (border_allocnos,
1578 ALLOCNO_NUM (ira_curr_regno_allocno_map[i]));
1582 /* The function creates allocnos corresponding to pseudo-registers
1583 living in loop represented by the corresponding loop tree node
1584 LOOP_NODE. The function is called by traverse_loop_tree. */
1585 static void
1586 create_loop_tree_node_allocnos (loop_tree_node_t loop_node)
1588 if (loop_node->bb != NULL)
1589 create_bb_allocnos (loop_node);
1590 else if (loop_node != ira_loop_tree_root)
1592 int i;
1593 edge_iterator ei;
1594 edge e;
1595 VEC (edge, heap) *edges;
1597 FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
1598 if (e->src != loop_node->loop->latch)
1599 create_loop_allocnos (e);
1601 edges = get_loop_exit_edges (loop_node->loop);
1602 for (i = 0; VEC_iterate (edge, edges, i, e); i++)
1603 create_loop_allocnos (e);
1604 VEC_free (edge, heap, edges);
1608 /* The function creates allocnos corresponding to pseudo-registers in
1609 the current function. The function traverses the loop tree for
1610 this. */
1611 static void
1612 create_allocnos (void)
1614 int i;
1615 allocno_t a, father_a;
1616 loop_tree_node_t father;
1618 /* We need to process BB first to correctly link allocnos by member
1619 next_regno_allocno. */
1620 traverse_loop_tree (TRUE, ira_loop_tree_root,
1621 create_loop_tree_node_allocnos, NULL);
1622 if (flag_ira_algorithm != IRA_ALGORITHM_REGIONAL
1623 && flag_ira_algorithm != IRA_ALGORITHM_MIXED)
1624 return;
1625 /* Propagate number of references and frequencies for regional
1626 register allocation. */
1627 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1628 for (a = regno_allocno_map[i];
1629 a != NULL;
1630 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1631 if ((father = ALLOCNO_LOOP_TREE_NODE (a)->father) != NULL
1632 && (father_a = father->regno_allocno_map[i]) != NULL)
1634 ALLOCNO_NREFS (father_a) += ALLOCNO_NREFS (a);
1635 ALLOCNO_FREQ (father_a) += ALLOCNO_FREQ (a);
1641 /* The function sets up minimal and maximal live range points for
1642 allocnos. */
1643 static void
1644 setup_min_max_allocno_live_range_point (void)
1646 int i;
1647 allocno_t a, father_a, cap;
1648 allocno_iterator ai;
1649 allocno_live_range_t r;
1650 loop_tree_node_t father;
1652 FOR_EACH_ALLOCNO (a, ai)
1654 r = ALLOCNO_LIVE_RANGES (a);
1655 if (r == NULL)
1656 continue;
1657 ALLOCNO_MAX (a) = r->finish;
1658 for (; r->next != NULL; r = r->next)
1660 ALLOCNO_MIN (a) = r->start;
1662 for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
1663 for (a = regno_allocno_map[i];
1664 a != NULL;
1665 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
1667 if (ALLOCNO_MAX (a) < 0)
1668 continue;
1669 ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
1670 /* Accumulation of range info. */
1671 if ((father = ALLOCNO_LOOP_TREE_NODE (a)->father) == NULL
1672 || (father_a = father->regno_allocno_map[i]) == NULL)
1674 for (cap = ALLOCNO_CAP (a); cap != NULL; cap = ALLOCNO_CAP (cap))
1676 if (ALLOCNO_MAX (cap) < ALLOCNO_MAX (a))
1677 ALLOCNO_MAX (cap) = ALLOCNO_MAX (a);
1678 if (ALLOCNO_MIN (cap) > ALLOCNO_MIN (a))
1679 ALLOCNO_MIN (cap) = ALLOCNO_MIN (a);
1681 continue;
1683 if (ALLOCNO_MAX (father_a) < ALLOCNO_MAX (a))
1684 ALLOCNO_MAX (father_a) = ALLOCNO_MAX (a);
1685 if (ALLOCNO_MIN (father_a) > ALLOCNO_MIN (a))
1686 ALLOCNO_MIN (father_a) = ALLOCNO_MIN (a);
1688 #ifdef ENABLE_IRA_CHECKING
1689 FOR_EACH_ALLOCNO (a, ai)
1691 if ((0 <= ALLOCNO_MIN (a) && ALLOCNO_MIN (a) <= max_point)
1692 && (0 <= ALLOCNO_MAX (a) && ALLOCNO_MAX (a) <= max_point))
1693 continue;
1694 gcc_unreachable ();
1696 #endif
1699 /* The function is used to sort allocnos according to their live
1700 ranges. Allocnos with smaller cover class are put first. Allocnos
1701 with the same cove class are ordered according their start (min).
1702 Allocnos with the same start are ordered according their finish
1703 (max). */
1704 static int
1705 allocno_range_compare_func (const void *v1p, const void *v2p)
1707 int diff;
1708 allocno_t a1 = *(const allocno_t *) v1p, a2 = *(const allocno_t *) v2p;
1710 if ((diff = ALLOCNO_COVER_CLASS (a1) - ALLOCNO_COVER_CLASS (a2)) != 0)
1711 return diff;
1712 if ((diff = ALLOCNO_MIN (a1) - ALLOCNO_MIN (a2)) != 0)
1713 return diff;
1714 return ALLOCNO_MAX (a1) - ALLOCNO_MAX (a2);
1717 /* The function sorts conflict_id_allocno_map and sets up conflict id
1718 of allocnos. */
1719 static void
1720 sort_conflict_id_allocno_map (void)
1722 int i, num;
1723 allocno_t a;
1724 allocno_iterator ai;
1726 num = 0;
1727 FOR_EACH_ALLOCNO (a, ai)
1728 conflict_id_allocno_map[num++] = a;
1729 qsort (conflict_id_allocno_map, num, sizeof (allocno_t),
1730 allocno_range_compare_func);
1731 for (i = 0; i < num; i++)
1732 if ((a = conflict_id_allocno_map[i]) != NULL)
1733 ALLOCNO_CONFLICT_ID (a) = i;
1734 for (i = num; i < allocnos_num; i++)
1735 conflict_id_allocno_map[i] = NULL;
1738 /* The function sets up minimal and maximal conflict ids of allocnos
1739 with which given allocno can conflict. */
1740 static void
1741 setup_min_max_conflict_allocno_ids (void)
1743 enum reg_class cover_class;
1744 int i, j, min, max, start, finish, first_not_finished, filled_area_start;
1745 int *live_range_min, *last_lived;
1746 allocno_t a;
1748 live_range_min = ira_allocate (sizeof (int) * allocnos_num);
1749 cover_class = -1;
1750 first_not_finished = -1;
1751 for (i = 0; i < allocnos_num; i++)
1753 a = conflict_id_allocno_map[i];
1754 if (a == NULL)
1755 continue;
1756 if (cover_class != ALLOCNO_COVER_CLASS (a))
1758 cover_class = ALLOCNO_COVER_CLASS (a);
1759 min = i;
1760 first_not_finished = i;
1762 else
1764 start = ALLOCNO_MIN (a);
1765 /* If we skip an allocno, the allocno with smaller ids will
1766 be also skipped because of the secondary sorting the
1767 range finishes (see function
1768 allocno_range_compare_func). */
1769 while (first_not_finished < i
1770 && start
1771 > ALLOCNO_MAX (conflict_id_allocno_map[first_not_finished]))
1772 first_not_finished++;
1773 min = first_not_finished;
1775 if (min == i)
1776 /* We could increase min further in this case but it is good
1777 enough. */
1778 min++;
1779 live_range_min[i] = ALLOCNO_MIN (a);
1780 ALLOCNO_MIN (a) = min;
1782 last_lived = ira_allocate (sizeof (int) * max_point);
1783 cover_class = -1;
1784 filled_area_start = -1;
1785 for (i = allocnos_num - 1; i >= 0; i--)
1787 a = conflict_id_allocno_map[i];
1788 if (a == NULL)
1789 continue;
1790 if (cover_class != ALLOCNO_COVER_CLASS (a))
1792 cover_class = ALLOCNO_COVER_CLASS (a);
1793 for (j = 0; j < max_point; j++)
1794 last_lived[j] = -1;
1795 filled_area_start = max_point;
1797 min = live_range_min[i];
1798 finish = ALLOCNO_MAX (a);
1799 max = last_lived[finish];
1800 if (max < 0)
1801 /* We could decrease max further in this case but it is good
1802 enough. */
1803 max = ALLOCNO_CONFLICT_ID (a) - 1;
1804 ALLOCNO_MAX (a) = max;
1805 /* In filling, we can go further A range finish to recognize
1806 intersection quickly because if the finish of subsequently
1807 processed allocno (it has smaller conflict id) range is
1808 further A range finish than they are definitely intersected
1809 (the reason for this is the allocnos with bigger conflict id
1810 have their range starts not smaller than allocnos with
1811 smaller ids. */
1812 for (j = min; j < filled_area_start; j++)
1813 last_lived[j] = i;
1814 filled_area_start = min;
1816 ira_free (last_lived);
1817 ira_free (live_range_min);
1822 /* The function creates caps representing allocnos living only inside
1823 the loop given by LOOP_NODE on higher loop level. */
1824 static void
1825 create_loop_tree_node_caps (loop_tree_node_t loop_node)
1827 unsigned int i;
1828 bitmap_iterator bi;
1829 loop_tree_node_t father;
1831 if (loop_node == ira_loop_tree_root)
1832 return;
1833 ira_assert (loop_node->bb == NULL);
1834 bitmap_and_compl (allocnos_bitmap, loop_node->mentioned_allocnos,
1835 loop_node->border_allocnos);
1836 father = loop_node->father;
1837 EXECUTE_IF_SET_IN_BITMAP (allocnos_bitmap, 0, i, bi)
1838 if (father->regno_allocno_map[ALLOCNO_REGNO (allocnos[i])] == NULL)
1839 create_cap_allocno (allocnos[i]);
1842 /* The function propagate info (not available at the cap creation
1843 time) to caps mentioned in LOOP_NODE. */
1844 static void
1845 propagate_info_to_loop_tree_node_caps (loop_tree_node_t loop_node)
1847 unsigned int i;
1848 bitmap_iterator bi;
1849 allocno_t a;
1851 ira_assert (loop_node->bb == NULL);
1852 EXECUTE_IF_SET_IN_BITMAP (loop_node->mentioned_allocnos, 0, i, bi)
1854 a = allocnos[i];
1855 if (ALLOCNO_CAP_MEMBER (a) != NULL)
1856 propagate_info_to_cap (a);
1862 /* The page contains code transforming more one region internal
1863 representation (IR) to one region IR which is necessary for the
1864 reload. This transformation is called IR flattening. We might
1865 just rebuild the IR for one region but we don't do it because it
1866 takes a lot of time. */
1868 /* The function merges ranges R1 and R2 and returns the result. The
1869 function maintains the order of ranges and tries to minimize number
1870 of the result ranges. */
1871 static allocno_live_range_t
1872 merge_ranges (allocno_live_range_t r1, allocno_live_range_t r2)
1874 allocno_live_range_t first, last, temp;
1876 if (r1 == NULL)
1877 return r2;
1878 if (r2 == NULL)
1879 return r1;
1880 for (first = last = NULL; r1 != NULL && r2 != NULL;)
1882 if (r1->start < r2->start)
1884 temp = r1;
1885 r1 = r2;
1886 r2 = temp;
1888 if (r1->start <= r2->finish + 1)
1890 /* Intersected ranges: merge r1 and r2 into r1. */
1891 r1->start = r2->start;
1892 if (r1->finish < r2->finish)
1893 r1->finish = r2->finish;
1894 temp = r2;
1895 r2 = r2->next;
1896 finish_allocno_live_range (temp);
1897 if (r2 == NULL)
1899 /* To try to merge with subsequent ranges in r1. */
1900 r2 = r1->next;
1901 r1->next = NULL;
1904 else
1906 /* Add r1 to the result. */
1907 if (first == NULL)
1908 first = last = r1;
1909 else
1911 last->next = r1;
1912 last = r1;
1914 r1 = r1->next;
1915 if (r1 == NULL)
1917 /* To try to merge with subsequent ranges in r2. */
1918 r1 = r2->next;
1919 r2->next = NULL;
1923 if (r1 != NULL)
1925 if (first == NULL)
1926 first = r1;
1927 else
1928 last->next = r1;
1929 ira_assert (r1->next == NULL);
1931 else if (r2 != NULL)
1933 if (first == NULL)
1934 first = r2;
1935 else
1936 last->next = r2;
1937 ira_assert (r2->next == NULL);
1939 else
1941 ira_assert (last->next == NULL);
1943 return first;
1946 /* This recursive function returns immediate common dominator of two
1947 loop tree nodes N1 and N2. */
1948 static loop_tree_node_t
1949 common_loop_tree_node_dominator (loop_tree_node_t n1, loop_tree_node_t n2)
1951 ira_assert (n1 != NULL && n2 != NULL);
1952 if (n1 == n2)
1953 return n1;
1954 if (n1->level < n2->level)
1955 return common_loop_tree_node_dominator (n1, n2->father);
1956 else if (n1->level > n2->level)
1957 return common_loop_tree_node_dominator (n1->father, n2);
1958 else
1959 return common_loop_tree_node_dominator (n1->father, n2->father);
1962 /* The function changes allocno in range list given by R onto A. */
1963 static void
1964 change_allocno_in_range_list (allocno_live_range_t r, allocno_t a)
1966 for (; r != NULL; r = r->next)
1967 r->allocno = a;
1970 /* The function flattens IR. In other words, the function transforms
1971 IR as it were build with one region (without loops). We could make
1972 it much simpler by rebuilding IR with one region, but unfortunately
1973 it takes a lot of time. MAX_REGNO_BEFORE_EMIT and
1974 MAX_POINT_BEFORE_EMIT are correspondingly MAX_REG_NUM () and
1975 MAX_POINT before emitting insns on the loop borders. */
1976 void
1977 ira_flattening (int max_regno_before_emit, int max_point_before_emit)
1979 int i, j, num, propagate_p, stop_p, keep_p;
1980 int hard_regs_num, new_allocnos_p, renamed_p, merged_p;
1981 unsigned int n;
1982 enum reg_class cover_class;
1983 rtx call, *allocno_calls;
1984 allocno_t a, father_a, first, second, node_first, node_second;
1985 allocno_t dominator_a;
1986 copy_t cp;
1987 loop_tree_node_t father, node, dominator;
1988 allocno_live_range_t r;
1989 allocno_iterator ai;
1990 copy_iterator ci;
1991 sparseset allocnos_live;
1992 /* Map: regno -> allocnos which will finally represent the regno for
1993 IR with one region. */
1994 allocno_t *regno_top_level_allocno_map;
1996 regno_top_level_allocno_map
1997 = ira_allocate (max_reg_num () * sizeof (allocno_t));
1998 memset (regno_top_level_allocno_map, 0, max_reg_num () * sizeof (allocno_t));
1999 expand_calls ();
2000 new_allocnos_p = renamed_p = merged_p = FALSE;
2001 /* Fix final allocno attributes. */
2002 for (i = max_regno_before_emit - 1; i >= FIRST_PSEUDO_REGISTER; i--)
2004 propagate_p = FALSE;
2005 for (a = regno_allocno_map[i];
2006 a != NULL;
2007 a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2009 if (ALLOCNO_CAP_MEMBER (a) != NULL)
2010 continue;
2011 if (ALLOCNO_SOMEWHERE_RENAMED_P (a))
2012 renamed_p = TRUE;
2013 if ((unsigned int) ALLOCNO_REGNO (a) != REGNO (ALLOCNO_REG (a))
2014 && ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
2016 allocno_calls = (VEC_address (rtx,
2017 regno_calls[ALLOCNO_REGNO (a)])
2018 + ALLOCNO_CALLS_CROSSED_START (a));
2019 for (j = ALLOCNO_CALLS_CROSSED_NUM (a) - 1; j >= 0; j--)
2021 call = allocno_calls[j];
2022 if (call == NULL_RTX)
2023 continue;
2024 add_regno_call (REGNO (ALLOCNO_REG (a)), call);
2025 allocno_calls[j] = NULL_RTX;
2028 if ((father = ALLOCNO_LOOP_TREE_NODE (a)->father) == NULL
2029 || ((father_a = father->regno_allocno_map[ALLOCNO_REGNO (a)])
2030 == NULL))
2032 ALLOCNO_COPIES (a) = NULL;
2033 regno_top_level_allocno_map[REGNO (ALLOCNO_REG (a))] = a;
2034 continue;
2036 ira_assert (ALLOCNO_CAP_MEMBER (father_a) == NULL);
2037 if (propagate_p)
2039 COPY_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (father_a),
2040 ALLOCNO_CONFLICT_HARD_REGS (father_a));
2041 IOR_HARD_REG_SET (ALLOCNO_TOTAL_CONFLICT_HARD_REGS (father_a),
2042 ALLOCNO_TOTAL_CONFLICT_HARD_REGS (a));
2043 #ifdef STACK_REGS
2044 ALLOCNO_TOTAL_NO_STACK_REG_P (father_a)
2045 = (ALLOCNO_NO_STACK_REG_P (father_a)
2046 || ALLOCNO_TOTAL_NO_STACK_REG_P (a));
2047 #endif
2049 if (REGNO (ALLOCNO_REG (a)) == REGNO (ALLOCNO_REG (father_a)))
2051 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2053 fprintf (ira_dump_file,
2054 " Moving ranges of a%dr%d to a%dr%d: ",
2055 ALLOCNO_NUM (a), REGNO (ALLOCNO_REG (a)),
2056 ALLOCNO_NUM (father_a),
2057 REGNO (ALLOCNO_REG (father_a)));
2058 print_live_range_list (ira_dump_file,
2059 ALLOCNO_LIVE_RANGES (a));
2061 change_allocno_in_range_list (ALLOCNO_LIVE_RANGES (a), father_a);
2062 ALLOCNO_LIVE_RANGES (father_a)
2063 = merge_ranges (ALLOCNO_LIVE_RANGES (a),
2064 ALLOCNO_LIVE_RANGES (father_a));
2065 merged_p = TRUE;
2066 ALLOCNO_LIVE_RANGES (a) = NULL;
2067 ALLOCNO_MEM_OPTIMIZED_DEST_P (father_a)
2068 = (ALLOCNO_MEM_OPTIMIZED_DEST_P (father_a)
2069 || ALLOCNO_MEM_OPTIMIZED_DEST_P (a));
2070 continue;
2072 new_allocnos_p = TRUE;
2073 propagate_p = TRUE;
2074 first = ALLOCNO_MEM_OPTIMIZED_DEST (a) == NULL ? NULL : a;
2075 for (;;)
2077 if (first == NULL
2078 && ALLOCNO_MEM_OPTIMIZED_DEST (father_a) != NULL)
2079 first = father_a;
2080 ALLOCNO_NREFS (father_a) -= ALLOCNO_NREFS (a);
2081 ALLOCNO_FREQ (father_a) -= ALLOCNO_FREQ (a);
2082 ALLOCNO_CALL_FREQ (father_a) -= ALLOCNO_CALL_FREQ (a);
2083 cover_class = ALLOCNO_COVER_CLASS (father_a);
2084 hard_regs_num = class_hard_regs_num[cover_class];
2085 if (ALLOCNO_HARD_REG_COSTS (a) != NULL
2086 && ALLOCNO_HARD_REG_COSTS (father_a) != NULL)
2087 for (j = 0; j < hard_regs_num; j++)
2088 ALLOCNO_HARD_REG_COSTS (father_a)[j]
2089 -= ALLOCNO_HARD_REG_COSTS (a)[j];
2090 if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL
2091 && ALLOCNO_CONFLICT_HARD_REG_COSTS (father_a) != NULL)
2092 for (j = 0; j < hard_regs_num; j++)
2093 ALLOCNO_CONFLICT_HARD_REG_COSTS (father_a)[j]
2094 -= ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[j];
2095 ALLOCNO_COVER_CLASS_COST (father_a)
2096 -= ALLOCNO_COVER_CLASS_COST (a);
2097 ALLOCNO_MEMORY_COST (father_a) -= ALLOCNO_MEMORY_COST (a);
2098 ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (father_a)
2099 -= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2100 if ((father = ALLOCNO_LOOP_TREE_NODE (father_a)->father) == NULL
2101 || (father_a = (father->regno_allocno_map
2102 [ALLOCNO_REGNO (father_a)])) == NULL)
2103 break;
2105 if (first != NULL)
2107 father_a = ALLOCNO_MEM_OPTIMIZED_DEST (first);
2108 dominator = common_loop_tree_node_dominator
2109 (ALLOCNO_LOOP_TREE_NODE (father_a),
2110 ALLOCNO_LOOP_TREE_NODE (first));
2111 dominator_a = dominator->regno_allocno_map[ALLOCNO_REGNO (a)];
2112 ira_assert (father_a != NULL);
2113 stop_p = first != a;
2114 /* Remember that exit can be to a grandparent (not only
2115 to a parent) or a child of the grandparent. */
2116 for (first = a;;)
2118 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2120 fprintf
2121 (ira_dump_file,
2122 " Coping ranges of a%dr%d to a%dr%d: ",
2123 ALLOCNO_NUM (first), REGNO (ALLOCNO_REG (first)),
2124 ALLOCNO_NUM (father_a),
2125 REGNO (ALLOCNO_REG (father_a)));
2126 print_live_range_list (ira_dump_file,
2127 ALLOCNO_LIVE_RANGES (first));
2129 r = copy_allocno_live_range_list (ALLOCNO_LIVE_RANGES
2130 (first));
2131 change_allocno_in_range_list (r, father_a);
2132 ALLOCNO_LIVE_RANGES (father_a)
2133 = merge_ranges (r, ALLOCNO_LIVE_RANGES (father_a));
2134 merged_p = TRUE;
2135 if (stop_p)
2136 break;
2137 father = ALLOCNO_LOOP_TREE_NODE (first)->father;
2138 ira_assert (father != NULL);
2139 first = father->regno_allocno_map[ALLOCNO_REGNO (a)];
2140 ira_assert (first != NULL);
2141 if (first == dominator_a)
2142 break;
2145 ALLOCNO_COPIES (a) = NULL;
2146 regno_top_level_allocno_map[REGNO (ALLOCNO_REG (a))] = a;
2149 ira_assert (new_allocnos_p || renamed_p
2150 || max_point_before_emit == max_point);
2151 if (new_allocnos_p)
2153 /* Fix final allocnos attributes concerning calls. */
2154 compress_calls ();
2155 FOR_EACH_ALLOCNO (a, ai)
2157 if (a != regno_top_level_allocno_map[REGNO (ALLOCNO_REG (a))]
2158 || ALLOCNO_CAP_MEMBER (a) != NULL)
2159 continue;
2160 ALLOCNO_CALLS_CROSSED_START (a) = 0;
2161 ALLOCNO_CALLS_CROSSED_NUM (a)
2162 = VEC_length (rtx, regno_calls[REGNO (ALLOCNO_REG (a))]);
2165 if (merged_p || max_point_before_emit != max_point)
2166 rebuild_start_finish_chains ();
2167 /* We should rebuild conflicts even if there are no new allocnos in
2168 situation when a pseudo used locally in loops and locally in the
2169 subloop because some allocnos are in conflict with the subloop
2170 allocno and they finally should be in conflict with the loop
2171 allocno. */
2172 if (new_allocnos_p || renamed_p)
2174 /* Rebuild conflicts. */
2175 FOR_EACH_ALLOCNO (a, ai)
2177 if (a != regno_top_level_allocno_map[REGNO (ALLOCNO_REG (a))]
2178 || ALLOCNO_CAP_MEMBER (a) != NULL)
2179 continue;
2180 for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
2181 ira_assert (r->allocno == a);
2182 clear_allocno_conflicts (a);
2184 allocnos_live = sparseset_alloc (allocnos_num);
2185 for (i = 0; i < max_point; i++)
2187 for (r = start_point_ranges[i]; r != NULL; r = r->start_next)
2189 a = r->allocno;
2190 if (a != regno_top_level_allocno_map[REGNO (ALLOCNO_REG (a))]
2191 || ALLOCNO_CAP_MEMBER (a) != NULL)
2192 continue;
2193 num = ALLOCNO_NUM (a);
2194 cover_class = ALLOCNO_COVER_CLASS (a);
2195 sparseset_set_bit (allocnos_live, num);
2196 EXECUTE_IF_SET_IN_SPARSESET (allocnos_live, n)
2198 allocno_t live_a = allocnos[n];
2200 if (cover_class == ALLOCNO_COVER_CLASS (live_a)
2201 /* Don't set up conflict for the allocno with itself. */
2202 && num != (int) n)
2203 add_allocno_conflict (a, live_a);
2207 for (r = finish_point_ranges[i]; r != NULL; r = r->finish_next)
2208 sparseset_clear_bit (allocnos_live, ALLOCNO_NUM (r->allocno));
2210 sparseset_free (allocnos_live);
2211 compress_conflict_vecs ();
2213 /* Mark some copies for removing and change allocnos in the rest
2214 copies. */
2215 FOR_EACH_COPY (cp, ci)
2217 if (ALLOCNO_CAP_MEMBER (cp->first) != NULL
2218 || ALLOCNO_CAP_MEMBER (cp->second) != NULL)
2220 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2221 fprintf
2222 (ira_dump_file, " Remove cp%d:%c%dr%d-%c%dr%d\n",
2223 cp->num, ALLOCNO_CAP_MEMBER (cp->first) != NULL ? 'c' : 'a',
2224 ALLOCNO_NUM (cp->first), REGNO (ALLOCNO_REG (cp->first)),
2225 ALLOCNO_CAP_MEMBER (cp->second) != NULL ? 'c' : 'a',
2226 ALLOCNO_NUM (cp->second), REGNO (ALLOCNO_REG (cp->second)));
2227 cp->loop_tree_node = NULL;
2228 continue;
2230 first = regno_top_level_allocno_map[REGNO (ALLOCNO_REG (cp->first))];
2231 second = regno_top_level_allocno_map[REGNO (ALLOCNO_REG (cp->second))];
2232 node = cp->loop_tree_node;
2233 if (node == NULL)
2234 keep_p = TRUE; /* It copy generated in ira-emit.c. */
2235 else
2237 /* Check that the copy was not propagated from level on
2238 which we will have different pseudos. */
2239 node_first = node->regno_allocno_map[ALLOCNO_REGNO (cp->first)];
2240 node_second = node->regno_allocno_map[ALLOCNO_REGNO (cp->second)];
2241 keep_p = ((REGNO (ALLOCNO_REG (first))
2242 == REGNO (ALLOCNO_REG (node_first)))
2243 && (REGNO (ALLOCNO_REG (second))
2244 == REGNO (ALLOCNO_REG (node_second))));
2246 if (keep_p)
2248 cp->loop_tree_node = ira_loop_tree_root;
2249 cp->first = first;
2250 cp->second = second;
2252 else
2254 cp->loop_tree_node = NULL;
2255 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2256 fprintf (ira_dump_file, " Remove cp%d:a%dr%d-a%dr%d\n",
2257 cp->num, ALLOCNO_NUM (cp->first),
2258 REGNO (ALLOCNO_REG (cp->first)), ALLOCNO_NUM (cp->second),
2259 REGNO (ALLOCNO_REG (cp->second)));
2262 /* Remove unnecessary allocnos on lower levels of the loop tree. */
2263 FOR_EACH_ALLOCNO (a, ai)
2265 if (a != regno_top_level_allocno_map[REGNO (ALLOCNO_REG (a))]
2266 || ALLOCNO_CAP_MEMBER (a) != NULL)
2268 if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2269 fprintf (ira_dump_file, " Remove a%dr%d\n",
2270 ALLOCNO_NUM (a), REGNO (ALLOCNO_REG (a)));
2271 finish_allocno (a);
2272 continue;
2274 ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
2275 ALLOCNO_REGNO (a) = REGNO (ALLOCNO_REG (a));
2276 ALLOCNO_CAP (a) = NULL;
2277 ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
2278 if (! ALLOCNO_ASSIGNED_P (a))
2279 free_allocno_updated_costs (a);
2280 ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
2281 ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
2283 /* Remove unnecessary copies. */
2284 FOR_EACH_COPY (cp, ci)
2286 if (cp->loop_tree_node == NULL)
2288 copies[cp->num] = NULL;
2289 finish_copy (cp);
2290 continue;
2292 ira_assert
2293 (ALLOCNO_LOOP_TREE_NODE (cp->first) == ira_loop_tree_root
2294 && ALLOCNO_LOOP_TREE_NODE (cp->second) == ira_loop_tree_root);
2295 add_allocno_copy_to_list (cp);
2296 swap_allocno_copy_ends_if_necessary (cp);
2298 rebuild_regno_allocno_maps ();
2299 ira_free (regno_top_level_allocno_map);
2304 #if 0
2306 static int all_loops = 0, high_pressure_loops = 0;
2308 static void
2309 calculate_high_pressure_loops (loop_tree_node_t loop_node,
2310 int *all_loops, int *high_pressure_loops)
2312 loop_tree_node_t subloop_node;
2313 int i;
2314 enum reg_class class;
2316 (*all_loops)++;
2317 for (i = 0; i < reg_class_cover_size; i++)
2319 class = reg_class_cover[i];
2320 if (loop_node->reg_pressure[class] > available_class_regs[class]
2321 || (loop_node->father != NULL
2322 && loop_node->father->reg_pressure[class] > available_class_regs[class]))
2323 break;
2325 if (i < reg_class_cover_size)
2326 (*high_pressure_loops)++;
2327 for (subloop_node = loop_node->subloops;
2328 subloop_node != NULL;
2329 subloop_node = subloop_node->subloop_next)
2331 ira_assert (subloop_node->bb == NULL);
2332 calculate_high_pressure_loops (subloop_node,
2333 all_loops, high_pressure_loops);
2336 #endif
2338 /* This entry function creates internal representation (IR) for IRA
2339 (allocnos, copies, loop tree nodes). If LOOPS_P is FALSE the nodes
2340 corresponding to the loops (except the root which corresponds the
2341 all function) and correspondingly allocnos for the loops will be
2342 not created. Such parameter value is used for Chaitin-Briggs
2343 coloring. The function returns TRUE if we generate loop structure
2344 (besides nodes representing all function and the basic blocks) for
2345 regional allocation. It means that we really need to flatten IR
2346 before the reload. */
2348 ira_build (int loops_p)
2350 unsigned int i;
2351 loop_p loop;
2353 df_analyze ();
2355 allocnos_bitmap = ira_allocate_bitmap ();
2356 CLEAR_HARD_REG_SET (crtl->emit.call_used_regs);
2357 initiate_calls ();
2358 initiate_cost_vectors ();
2359 initiate_allocnos ();
2360 initiate_copies ();
2361 create_loop_tree_nodes (loops_p);
2362 form_loop_tree ();
2363 create_allocnos ();
2364 ira_costs ();
2365 create_allocno_live_ranges ();
2367 if (optimize && (flag_ira_algorithm == IRA_ALGORITHM_REGIONAL
2368 || flag_ira_algorithm == IRA_ALGORITHM_MIXED))
2370 bitmap_clear (allocnos_bitmap);
2371 traverse_loop_tree (FALSE, ira_loop_tree_root, NULL,
2372 create_loop_tree_node_caps);
2374 setup_min_max_allocno_live_range_point ();
2375 sort_conflict_id_allocno_map ();
2376 setup_min_max_conflict_allocno_ids ();
2377 ira_build_conflicts ();
2378 if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
2380 int n, nr;
2381 allocno_t a;
2382 allocno_live_range_t r;
2383 allocno_iterator ai;
2385 n = 0;
2386 FOR_EACH_ALLOCNO (a, ai)
2387 n += ALLOCNO_CONFLICT_ALLOCNOS_NUM (a);
2388 nr = 0;
2389 FOR_EACH_ALLOCNO (a, ai)
2390 for (r = ALLOCNO_LIVE_RANGES (a); r != NULL; r = r->next)
2391 nr++;
2392 fprintf (ira_dump_file, " regions=%d, blocks=%d, points=%d\n",
2393 VEC_length (loop_p, ira_loops.larray), n_basic_blocks,
2394 max_point);
2395 fprintf (ira_dump_file,
2396 " allocnos=%d, copies=%d, conflicts=%d, ranges=%d\n",
2397 allocnos_num, copies_num, n, nr);
2399 if (optimize)
2401 if (flag_ira_algorithm == IRA_ALGORITHM_REGIONAL
2402 || flag_ira_algorithm == IRA_ALGORITHM_MIXED)
2403 traverse_loop_tree (FALSE, ira_loop_tree_root, NULL,
2404 propagate_info_to_loop_tree_node_caps);
2405 tune_allocno_costs_and_cover_classes ();
2406 if (flag_ira_algorithm == IRA_ALGORITHM_REGIONAL
2407 || flag_ira_algorithm == IRA_ALGORITHM_MIXED)
2409 for (i = 0; VEC_iterate (loop_p, ira_loops.larray, i, loop); i++)
2410 if (ira_loop_nodes[i].regno_allocno_map != NULL
2411 && ira_loop_tree_root != &ira_loop_nodes[i])
2412 return TRUE;
2415 return FALSE;
2418 /* The function releases data created by function ira_build. */
2419 void
2420 ira_destroy (void)
2422 finish_loop_tree_nodes ();
2423 finish_copies ();
2424 finish_allocnos ();
2425 finish_calls ();
2426 finish_cost_vectors ();
2427 finish_allocno_live_ranges ();
2428 ira_free_bitmap (allocnos_bitmap);