ira-build.cc revision 1.1.1.1 1 1.1 mrg /* Building internal representation for IRA.
2 1.1 mrg Copyright (C) 2006-2022 Free Software Foundation, Inc.
3 1.1 mrg Contributed by Vladimir Makarov <vmakarov (at) redhat.com>.
4 1.1 mrg
5 1.1 mrg This file is part of GCC.
6 1.1 mrg
7 1.1 mrg GCC is free software; you can redistribute it and/or modify it under
8 1.1 mrg the terms of the GNU General Public License as published by the Free
9 1.1 mrg Software Foundation; either version 3, or (at your option) any later
10 1.1 mrg version.
11 1.1 mrg
12 1.1 mrg GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 1.1 mrg WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 1.1 mrg FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 1.1 mrg for more details.
16 1.1 mrg
17 1.1 mrg You should have received a copy of the GNU General Public License
18 1.1 mrg along with GCC; see the file COPYING3. If not see
19 1.1 mrg <http://www.gnu.org/licenses/>. */
20 1.1 mrg
21 1.1 mrg #include "config.h"
22 1.1 mrg #include "system.h"
23 1.1 mrg #include "coretypes.h"
24 1.1 mrg #include "backend.h"
25 1.1 mrg #include "target.h"
26 1.1 mrg #include "rtl.h"
27 1.1 mrg #include "predict.h"
28 1.1 mrg #include "df.h"
29 1.1 mrg #include "insn-config.h"
30 1.1 mrg #include "regs.h"
31 1.1 mrg #include "memmodel.h"
32 1.1 mrg #include "ira.h"
33 1.1 mrg #include "ira-int.h"
34 1.1 mrg #include "sparseset.h"
35 1.1 mrg #include "cfgloop.h"
36 1.1 mrg
37 1.1 mrg static ira_copy_t find_allocno_copy (ira_allocno_t, ira_allocno_t, rtx_insn *,
38 1.1 mrg ira_loop_tree_node_t);
39 1.1 mrg
40 1.1 mrg /* The root of the loop tree corresponding to the all function. */
41 1.1 mrg ira_loop_tree_node_t ira_loop_tree_root;
42 1.1 mrg
43 1.1 mrg /* Height of the loop tree. */
44 1.1 mrg int ira_loop_tree_height;
45 1.1 mrg
46 1.1 mrg /* All nodes representing basic blocks are referred through the
47 1.1 mrg following array. We cannot use basic block member `aux' for this
48 1.1 mrg because it is used for insertion of insns on edges. */
49 1.1 mrg ira_loop_tree_node_t ira_bb_nodes;
50 1.1 mrg
51 1.1 mrg /* All nodes representing loops are referred through the following
52 1.1 mrg array. */
53 1.1 mrg ira_loop_tree_node_t ira_loop_nodes;
54 1.1 mrg
55 1.1 mrg /* And size of the ira_loop_nodes array. */
56 1.1 mrg unsigned int ira_loop_nodes_count;
57 1.1 mrg
58 1.1 mrg /* Map regno -> allocnos with given regno (see comments for
59 1.1 mrg allocno member `next_regno_allocno'). */
60 1.1 mrg ira_allocno_t *ira_regno_allocno_map;
61 1.1 mrg
62 1.1 mrg /* Array of references to all allocnos. The order number of the
63 1.1 mrg allocno corresponds to the index in the array. Removed allocnos
64 1.1 mrg have NULL element value. */
65 1.1 mrg ira_allocno_t *ira_allocnos;
66 1.1 mrg
67 1.1 mrg /* Sizes of the previous array. */
68 1.1 mrg int ira_allocnos_num;
69 1.1 mrg
70 1.1 mrg /* Count of conflict record structures we've created, used when creating
71 1.1 mrg a new conflict id. */
72 1.1 mrg int ira_objects_num;
73 1.1 mrg
74 1.1 mrg /* Map a conflict id to its conflict record. */
75 1.1 mrg ira_object_t *ira_object_id_map;
76 1.1 mrg
77 1.1 mrg /* Array of references to all allocno preferences. The order number
78 1.1 mrg of the preference corresponds to the index in the array. */
79 1.1 mrg ira_pref_t *ira_prefs;
80 1.1 mrg
81 1.1 mrg /* Size of the previous array. */
82 1.1 mrg int ira_prefs_num;
83 1.1 mrg
84 1.1 mrg /* Array of references to all copies. The order number of the copy
85 1.1 mrg corresponds to the index in the array. Removed copies have NULL
86 1.1 mrg element value. */
87 1.1 mrg ira_copy_t *ira_copies;
88 1.1 mrg
89 1.1 mrg /* Size of the previous array. */
90 1.1 mrg int ira_copies_num;
91 1.1 mrg
92 1.1 mrg
93 1.1 mrg
95 1.1 mrg /* LAST_BASIC_BLOCK before generating additional insns because of live
96 1.1 mrg range splitting. Emitting insns on a critical edge creates a new
97 1.1 mrg basic block. */
98 1.1 mrg static int last_basic_block_before_change;
99 1.1 mrg
100 1.1 mrg /* Initialize some members in loop tree node NODE. Use LOOP_NUM for
101 1.1 mrg the member loop_num. */
102 1.1 mrg static void
103 1.1 mrg init_loop_tree_node (struct ira_loop_tree_node *node, int loop_num)
104 1.1 mrg {
105 1.1 mrg int max_regno = max_reg_num ();
106 1.1 mrg
107 1.1 mrg node->regno_allocno_map
108 1.1 mrg = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t) * max_regno);
109 1.1 mrg memset (node->regno_allocno_map, 0, sizeof (ira_allocno_t) * max_regno);
110 1.1 mrg memset (node->reg_pressure, 0, sizeof (node->reg_pressure));
111 1.1 mrg node->all_allocnos = ira_allocate_bitmap ();
112 1.1 mrg node->modified_regnos = ira_allocate_bitmap ();
113 1.1 mrg node->border_allocnos = ira_allocate_bitmap ();
114 1.1 mrg node->local_copies = ira_allocate_bitmap ();
115 1.1 mrg node->loop_num = loop_num;
116 1.1 mrg node->children = NULL;
117 1.1 mrg node->subloops = NULL;
118 1.1 mrg }
119 1.1 mrg
120 1.1 mrg
121 1.1 mrg /* The following function allocates the loop tree nodes. If
122 1.1 mrg CURRENT_LOOPS is NULL, the nodes corresponding to the loops (except
123 1.1 mrg the root which corresponds the all function) will be not allocated
124 1.1 mrg but nodes will still be allocated for basic blocks. */
125 1.1 mrg static void
126 1.1 mrg create_loop_tree_nodes (void)
127 1.1 mrg {
128 1.1 mrg unsigned int i, j;
129 1.1 mrg bool skip_p;
130 1.1 mrg edge_iterator ei;
131 1.1 mrg edge e;
132 1.1 mrg loop_p loop;
133 1.1 mrg
134 1.1 mrg ira_bb_nodes
135 1.1 mrg = ((struct ira_loop_tree_node *)
136 1.1 mrg ira_allocate (sizeof (struct ira_loop_tree_node)
137 1.1 mrg * last_basic_block_for_fn (cfun)));
138 1.1 mrg last_basic_block_before_change = last_basic_block_for_fn (cfun);
139 1.1 mrg for (i = 0; i < (unsigned int) last_basic_block_for_fn (cfun); i++)
140 1.1 mrg {
141 1.1 mrg ira_bb_nodes[i].regno_allocno_map = NULL;
142 1.1 mrg memset (ira_bb_nodes[i].reg_pressure, 0,
143 1.1 mrg sizeof (ira_bb_nodes[i].reg_pressure));
144 1.1 mrg ira_bb_nodes[i].all_allocnos = NULL;
145 1.1 mrg ira_bb_nodes[i].modified_regnos = NULL;
146 1.1 mrg ira_bb_nodes[i].border_allocnos = NULL;
147 1.1 mrg ira_bb_nodes[i].local_copies = NULL;
148 1.1 mrg }
149 1.1 mrg if (current_loops == NULL)
150 1.1 mrg {
151 1.1 mrg ira_loop_nodes_count = 1;
152 1.1 mrg ira_loop_nodes = ((struct ira_loop_tree_node *)
153 1.1 mrg ira_allocate (sizeof (struct ira_loop_tree_node)));
154 1.1 mrg init_loop_tree_node (ira_loop_nodes, 0);
155 1.1 mrg return;
156 1.1 mrg }
157 1.1 mrg ira_loop_nodes_count = number_of_loops (cfun);
158 1.1 mrg ira_loop_nodes = ((struct ira_loop_tree_node *)
159 1.1 mrg ira_allocate (sizeof (struct ira_loop_tree_node)
160 1.1 mrg * ira_loop_nodes_count));
161 1.1 mrg FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
162 1.1 mrg {
163 1.1 mrg if (loop_outer (loop) != NULL)
164 1.1 mrg {
165 1.1 mrg ira_loop_nodes[i].regno_allocno_map = NULL;
166 1.1 mrg skip_p = false;
167 1.1 mrg FOR_EACH_EDGE (e, ei, loop->header->preds)
168 1.1 mrg if (e->src != loop->latch
169 1.1 mrg && (e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
170 1.1 mrg {
171 1.1 mrg skip_p = true;
172 1.1 mrg break;
173 1.1 mrg }
174 1.1 mrg if (skip_p)
175 1.1 mrg continue;
176 1.1 mrg auto_vec<edge> edges = get_loop_exit_edges (loop);
177 1.1 mrg FOR_EACH_VEC_ELT (edges, j, e)
178 1.1 mrg if ((e->flags & EDGE_ABNORMAL) && EDGE_CRITICAL_P (e))
179 1.1 mrg {
180 1.1 mrg skip_p = true;
181 1.1 mrg break;
182 1.1 mrg }
183 1.1 mrg if (skip_p)
184 1.1 mrg continue;
185 1.1 mrg }
186 1.1 mrg init_loop_tree_node (&ira_loop_nodes[i], loop->num);
187 1.1 mrg }
188 1.1 mrg }
189 1.1 mrg
190 1.1 mrg /* The function returns TRUE if there are more one allocation
191 1.1 mrg region. */
192 1.1 mrg static bool
193 1.1 mrg more_one_region_p (void)
194 1.1 mrg {
195 1.1 mrg unsigned int i;
196 1.1 mrg loop_p loop;
197 1.1 mrg
198 1.1 mrg if (current_loops != NULL)
199 1.1 mrg FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
200 1.1 mrg if (ira_loop_nodes[i].regno_allocno_map != NULL
201 1.1 mrg && ira_loop_tree_root != &ira_loop_nodes[i])
202 1.1 mrg return true;
203 1.1 mrg return false;
204 1.1 mrg }
205 1.1 mrg
206 1.1 mrg /* Free the loop tree node of a loop. */
207 1.1 mrg static void
208 1.1 mrg finish_loop_tree_node (ira_loop_tree_node_t loop)
209 1.1 mrg {
210 1.1 mrg if (loop->regno_allocno_map != NULL)
211 1.1 mrg {
212 1.1 mrg ira_assert (loop->bb == NULL);
213 1.1 mrg ira_free_bitmap (loop->local_copies);
214 1.1 mrg ira_free_bitmap (loop->border_allocnos);
215 1.1 mrg ira_free_bitmap (loop->modified_regnos);
216 1.1 mrg ira_free_bitmap (loop->all_allocnos);
217 1.1 mrg ira_free (loop->regno_allocno_map);
218 1.1 mrg loop->regno_allocno_map = NULL;
219 1.1 mrg }
220 1.1 mrg }
221 1.1 mrg
222 1.1 mrg /* Free the loop tree nodes. */
223 1.1 mrg static void
224 1.1 mrg finish_loop_tree_nodes (void)
225 1.1 mrg {
226 1.1 mrg unsigned int i;
227 1.1 mrg
228 1.1 mrg for (i = 0; i < ira_loop_nodes_count; i++)
229 1.1 mrg finish_loop_tree_node (&ira_loop_nodes[i]);
230 1.1 mrg ira_free (ira_loop_nodes);
231 1.1 mrg for (i = 0; i < (unsigned int) last_basic_block_before_change; i++)
232 1.1 mrg {
233 1.1 mrg if (ira_bb_nodes[i].local_copies != NULL)
234 1.1 mrg ira_free_bitmap (ira_bb_nodes[i].local_copies);
235 1.1 mrg if (ira_bb_nodes[i].border_allocnos != NULL)
236 1.1 mrg ira_free_bitmap (ira_bb_nodes[i].border_allocnos);
237 1.1 mrg if (ira_bb_nodes[i].modified_regnos != NULL)
238 1.1 mrg ira_free_bitmap (ira_bb_nodes[i].modified_regnos);
239 1.1 mrg if (ira_bb_nodes[i].all_allocnos != NULL)
240 1.1 mrg ira_free_bitmap (ira_bb_nodes[i].all_allocnos);
241 1.1 mrg if (ira_bb_nodes[i].regno_allocno_map != NULL)
242 1.1 mrg ira_free (ira_bb_nodes[i].regno_allocno_map);
243 1.1 mrg }
244 1.1 mrg ira_free (ira_bb_nodes);
245 1.1 mrg }
246 1.1 mrg
247 1.1 mrg
248 1.1 mrg
250 1.1 mrg /* The following recursive function adds LOOP to the loop tree
251 1.1 mrg hierarchy. LOOP is added only once. If LOOP is NULL we adding
252 1.1 mrg loop designating the whole function when CFG loops are not
253 1.1 mrg built. */
254 1.1 mrg static void
255 1.1 mrg add_loop_to_tree (class loop *loop)
256 1.1 mrg {
257 1.1 mrg int loop_num;
258 1.1 mrg class loop *parent;
259 1.1 mrg ira_loop_tree_node_t loop_node, parent_node;
260 1.1 mrg
261 1.1 mrg /* We cannot use loop node access macros here because of potential
262 1.1 mrg checking and because the nodes are not initialized enough
263 1.1 mrg yet. */
264 1.1 mrg if (loop != NULL && loop_outer (loop) != NULL)
265 1.1 mrg add_loop_to_tree (loop_outer (loop));
266 1.1 mrg loop_num = loop != NULL ? loop->num : 0;
267 1.1 mrg if (ira_loop_nodes[loop_num].regno_allocno_map != NULL
268 1.1 mrg && ira_loop_nodes[loop_num].children == NULL)
269 1.1 mrg {
270 1.1 mrg /* We have not added loop node to the tree yet. */
271 1.1 mrg loop_node = &ira_loop_nodes[loop_num];
272 1.1 mrg loop_node->loop = loop;
273 1.1 mrg loop_node->bb = NULL;
274 1.1 mrg if (loop == NULL)
275 1.1 mrg parent = NULL;
276 1.1 mrg else
277 1.1 mrg {
278 1.1 mrg for (parent = loop_outer (loop);
279 1.1 mrg parent != NULL;
280 1.1 mrg parent = loop_outer (parent))
281 1.1 mrg if (ira_loop_nodes[parent->num].regno_allocno_map != NULL)
282 1.1 mrg break;
283 1.1 mrg }
284 1.1 mrg if (parent == NULL)
285 1.1 mrg {
286 1.1 mrg loop_node->next = NULL;
287 1.1 mrg loop_node->subloop_next = NULL;
288 1.1 mrg loop_node->parent = NULL;
289 1.1 mrg }
290 1.1 mrg else
291 1.1 mrg {
292 1.1 mrg parent_node = &ira_loop_nodes[parent->num];
293 1.1 mrg loop_node->next = parent_node->children;
294 1.1 mrg parent_node->children = loop_node;
295 1.1 mrg loop_node->subloop_next = parent_node->subloops;
296 1.1 mrg parent_node->subloops = loop_node;
297 1.1 mrg loop_node->parent = parent_node;
298 1.1 mrg }
299 1.1 mrg }
300 1.1 mrg }
301 1.1 mrg
302 1.1 mrg /* The following recursive function sets up levels of nodes of the
303 1.1 mrg tree given its root LOOP_NODE. The enumeration starts with LEVEL.
304 1.1 mrg The function returns maximal value of level in the tree + 1. */
305 1.1 mrg static int
306 1.1 mrg setup_loop_tree_level (ira_loop_tree_node_t loop_node, int level)
307 1.1 mrg {
308 1.1 mrg int height, max_height;
309 1.1 mrg ira_loop_tree_node_t subloop_node;
310 1.1 mrg
311 1.1 mrg ira_assert (loop_node->bb == NULL);
312 1.1 mrg loop_node->level = level;
313 1.1 mrg max_height = level + 1;
314 1.1 mrg for (subloop_node = loop_node->subloops;
315 1.1 mrg subloop_node != NULL;
316 1.1 mrg subloop_node = subloop_node->subloop_next)
317 1.1 mrg {
318 1.1 mrg ira_assert (subloop_node->bb == NULL);
319 1.1 mrg height = setup_loop_tree_level (subloop_node, level + 1);
320 1.1 mrg if (height > max_height)
321 1.1 mrg max_height = height;
322 1.1 mrg }
323 1.1 mrg return max_height;
324 1.1 mrg }
325 1.1 mrg
326 1.1 mrg /* Create the loop tree. The algorithm is designed to provide correct
327 1.1 mrg order of loops (they are ordered by their last loop BB) and basic
328 1.1 mrg blocks in the chain formed by member next. */
329 1.1 mrg static void
330 1.1 mrg form_loop_tree (void)
331 1.1 mrg {
332 1.1 mrg basic_block bb;
333 1.1 mrg class loop *parent;
334 1.1 mrg ira_loop_tree_node_t bb_node, loop_node;
335 1.1 mrg
336 1.1 mrg /* We cannot use loop/bb node access macros because of potential
337 1.1 mrg checking and because the nodes are not initialized enough
338 1.1 mrg yet. */
339 1.1 mrg FOR_EACH_BB_FN (bb, cfun)
340 1.1 mrg {
341 1.1 mrg bb_node = &ira_bb_nodes[bb->index];
342 1.1 mrg bb_node->bb = bb;
343 1.1 mrg bb_node->loop = NULL;
344 1.1 mrg bb_node->subloops = NULL;
345 1.1 mrg bb_node->children = NULL;
346 1.1 mrg bb_node->subloop_next = NULL;
347 1.1 mrg bb_node->next = NULL;
348 1.1 mrg if (current_loops == NULL)
349 1.1 mrg parent = NULL;
350 1.1 mrg else
351 1.1 mrg {
352 1.1 mrg for (parent = bb->loop_father;
353 1.1 mrg parent != NULL;
354 1.1 mrg parent = loop_outer (parent))
355 1.1 mrg if (ira_loop_nodes[parent->num].regno_allocno_map != NULL)
356 1.1 mrg break;
357 1.1 mrg }
358 1.1 mrg add_loop_to_tree (parent);
359 1.1 mrg loop_node = &ira_loop_nodes[parent == NULL ? 0 : parent->num];
360 1.1 mrg bb_node->next = loop_node->children;
361 1.1 mrg bb_node->parent = loop_node;
362 1.1 mrg loop_node->children = bb_node;
363 1.1 mrg }
364 1.1 mrg ira_loop_tree_root = IRA_LOOP_NODE_BY_INDEX (0);
365 1.1 mrg ira_loop_tree_height = setup_loop_tree_level (ira_loop_tree_root, 0);
366 1.1 mrg ira_assert (ira_loop_tree_root->regno_allocno_map != NULL);
367 1.1 mrg }
368 1.1 mrg
369 1.1 mrg
370 1.1 mrg
372 1.1 mrg /* Rebuild IRA_REGNO_ALLOCNO_MAP and REGNO_ALLOCNO_MAPs of the loop
373 1.1 mrg tree nodes. */
374 1.1 mrg static void
375 1.1 mrg rebuild_regno_allocno_maps (void)
376 1.1 mrg {
377 1.1 mrg unsigned int l;
378 1.1 mrg int max_regno, regno;
379 1.1 mrg ira_allocno_t a;
380 1.1 mrg ira_loop_tree_node_t loop_tree_node;
381 1.1 mrg loop_p loop;
382 1.1 mrg ira_allocno_iterator ai;
383 1.1 mrg
384 1.1 mrg ira_assert (current_loops != NULL);
385 1.1 mrg max_regno = max_reg_num ();
386 1.1 mrg FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), l, loop)
387 1.1 mrg if (ira_loop_nodes[l].regno_allocno_map != NULL)
388 1.1 mrg {
389 1.1 mrg ira_free (ira_loop_nodes[l].regno_allocno_map);
390 1.1 mrg ira_loop_nodes[l].regno_allocno_map
391 1.1 mrg = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
392 1.1 mrg * max_regno);
393 1.1 mrg memset (ira_loop_nodes[l].regno_allocno_map, 0,
394 1.1 mrg sizeof (ira_allocno_t) * max_regno);
395 1.1 mrg }
396 1.1 mrg ira_free (ira_regno_allocno_map);
397 1.1 mrg ira_regno_allocno_map
398 1.1 mrg = (ira_allocno_t *) ira_allocate (max_regno * sizeof (ira_allocno_t));
399 1.1 mrg memset (ira_regno_allocno_map, 0, max_regno * sizeof (ira_allocno_t));
400 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
401 1.1 mrg {
402 1.1 mrg if (ALLOCNO_CAP_MEMBER (a) != NULL)
403 1.1 mrg /* Caps are not in the regno allocno maps. */
404 1.1 mrg continue;
405 1.1 mrg regno = ALLOCNO_REGNO (a);
406 1.1 mrg loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
407 1.1 mrg ALLOCNO_NEXT_REGNO_ALLOCNO (a) = ira_regno_allocno_map[regno];
408 1.1 mrg ira_regno_allocno_map[regno] = a;
409 1.1 mrg if (loop_tree_node->regno_allocno_map[regno] == NULL)
410 1.1 mrg /* Remember that we can create temporary allocnos to break
411 1.1 mrg cycles in register shuffle. */
412 1.1 mrg loop_tree_node->regno_allocno_map[regno] = a;
413 1.1 mrg }
414 1.1 mrg }
415 1.1 mrg
416 1.1 mrg
418 1.1 mrg /* Pools for allocnos, allocno live ranges and objects. */
419 1.1 mrg static object_allocator<live_range> live_range_pool ("live ranges");
420 1.1 mrg static object_allocator<ira_allocno> allocno_pool ("allocnos");
421 1.1 mrg static object_allocator<ira_object> object_pool ("objects");
422 1.1 mrg
423 1.1 mrg /* Vec containing references to all created allocnos. It is a
424 1.1 mrg container of array allocnos. */
425 1.1 mrg static vec<ira_allocno_t> allocno_vec;
426 1.1 mrg
427 1.1 mrg /* Vec containing references to all created ira_objects. It is a
428 1.1 mrg container of ira_object_id_map. */
429 1.1 mrg static vec<ira_object_t> ira_object_id_map_vec;
430 1.1 mrg
431 1.1 mrg /* Initialize data concerning allocnos. */
432 1.1 mrg static void
433 1.1 mrg initiate_allocnos (void)
434 1.1 mrg {
435 1.1 mrg allocno_vec.create (max_reg_num () * 2);
436 1.1 mrg ira_allocnos = NULL;
437 1.1 mrg ira_allocnos_num = 0;
438 1.1 mrg ira_objects_num = 0;
439 1.1 mrg ira_object_id_map_vec.create (max_reg_num () * 2);
440 1.1 mrg ira_object_id_map = NULL;
441 1.1 mrg ira_regno_allocno_map
442 1.1 mrg = (ira_allocno_t *) ira_allocate (max_reg_num ()
443 1.1 mrg * sizeof (ira_allocno_t));
444 1.1 mrg memset (ira_regno_allocno_map, 0, max_reg_num () * sizeof (ira_allocno_t));
445 1.1 mrg }
446 1.1 mrg
447 1.1 mrg /* Create and return an object corresponding to a new allocno A. */
448 1.1 mrg static ira_object_t
449 1.1 mrg ira_create_object (ira_allocno_t a, int subword)
450 1.1 mrg {
451 1.1 mrg enum reg_class aclass = ALLOCNO_CLASS (a);
452 1.1 mrg ira_object_t obj = object_pool.allocate ();
453 1.1 mrg
454 1.1 mrg OBJECT_ALLOCNO (obj) = a;
455 1.1 mrg OBJECT_SUBWORD (obj) = subword;
456 1.1 mrg OBJECT_CONFLICT_ID (obj) = ira_objects_num;
457 1.1 mrg OBJECT_CONFLICT_VEC_P (obj) = false;
458 1.1 mrg OBJECT_CONFLICT_ARRAY (obj) = NULL;
459 1.1 mrg OBJECT_NUM_CONFLICTS (obj) = 0;
460 1.1 mrg OBJECT_CONFLICT_HARD_REGS (obj) = ira_no_alloc_regs;
461 1.1 mrg OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) = ira_no_alloc_regs;
462 1.1 mrg OBJECT_CONFLICT_HARD_REGS (obj) |= ~reg_class_contents[aclass];
463 1.1 mrg OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) |= ~reg_class_contents[aclass];
464 1.1 mrg OBJECT_MIN (obj) = INT_MAX;
465 1.1 mrg OBJECT_MAX (obj) = -1;
466 1.1 mrg OBJECT_LIVE_RANGES (obj) = NULL;
467 1.1 mrg
468 1.1 mrg ira_object_id_map_vec.safe_push (obj);
469 1.1 mrg ira_object_id_map
470 1.1 mrg = ira_object_id_map_vec.address ();
471 1.1 mrg ira_objects_num = ira_object_id_map_vec.length ();
472 1.1 mrg
473 1.1 mrg return obj;
474 1.1 mrg }
475 1.1 mrg
476 1.1 mrg /* Create and return the allocno corresponding to REGNO in
477 1.1 mrg LOOP_TREE_NODE. Add the allocno to the list of allocnos with the
478 1.1 mrg same regno if CAP_P is FALSE. */
479 1.1 mrg ira_allocno_t
480 1.1 mrg ira_create_allocno (int regno, bool cap_p,
481 1.1 mrg ira_loop_tree_node_t loop_tree_node)
482 1.1 mrg {
483 1.1 mrg ira_allocno_t a;
484 1.1 mrg
485 1.1 mrg a = allocno_pool.allocate ();
486 1.1 mrg ALLOCNO_REGNO (a) = regno;
487 1.1 mrg ALLOCNO_LOOP_TREE_NODE (a) = loop_tree_node;
488 1.1 mrg if (! cap_p)
489 1.1 mrg {
490 1.1 mrg ALLOCNO_NEXT_REGNO_ALLOCNO (a) = ira_regno_allocno_map[regno];
491 1.1 mrg ira_regno_allocno_map[regno] = a;
492 1.1 mrg if (loop_tree_node->regno_allocno_map[regno] == NULL)
493 1.1 mrg /* Remember that we can create temporary allocnos to break
494 1.1 mrg cycles in register shuffle on region borders (see
495 1.1 mrg ira-emit.cc). */
496 1.1 mrg loop_tree_node->regno_allocno_map[regno] = a;
497 1.1 mrg }
498 1.1 mrg ALLOCNO_CAP (a) = NULL;
499 1.1 mrg ALLOCNO_CAP_MEMBER (a) = NULL;
500 1.1 mrg ALLOCNO_NUM (a) = ira_allocnos_num;
501 1.1 mrg bitmap_set_bit (loop_tree_node->all_allocnos, ALLOCNO_NUM (a));
502 1.1 mrg ALLOCNO_NREFS (a) = 0;
503 1.1 mrg ALLOCNO_FREQ (a) = 0;
504 1.1 mrg ALLOCNO_MIGHT_CONFLICT_WITH_PARENT_P (a) = false;
505 1.1 mrg ALLOCNO_HARD_REGNO (a) = -1;
506 1.1 mrg ALLOCNO_CALL_FREQ (a) = 0;
507 1.1 mrg ALLOCNO_CALLS_CROSSED_NUM (a) = 0;
508 1.1 mrg ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a) = 0;
509 1.1 mrg ALLOCNO_CROSSED_CALLS_ABIS (a) = 0;
510 1.1 mrg CLEAR_HARD_REG_SET (ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a));
511 1.1 mrg #ifdef STACK_REGS
512 1.1 mrg ALLOCNO_NO_STACK_REG_P (a) = false;
513 1.1 mrg ALLOCNO_TOTAL_NO_STACK_REG_P (a) = false;
514 1.1 mrg #endif
515 1.1 mrg ALLOCNO_DONT_REASSIGN_P (a) = false;
516 1.1 mrg ALLOCNO_BAD_SPILL_P (a) = false;
517 1.1 mrg ALLOCNO_ASSIGNED_P (a) = false;
518 1.1 mrg ALLOCNO_MODE (a) = (regno < 0 ? VOIDmode : PSEUDO_REGNO_MODE (regno));
519 1.1 mrg ALLOCNO_WMODE (a) = ALLOCNO_MODE (a);
520 1.1 mrg ALLOCNO_PREFS (a) = NULL;
521 1.1 mrg ALLOCNO_COPIES (a) = NULL;
522 1.1 mrg ALLOCNO_HARD_REG_COSTS (a) = NULL;
523 1.1 mrg ALLOCNO_CONFLICT_HARD_REG_COSTS (a) = NULL;
524 1.1 mrg ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
525 1.1 mrg ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
526 1.1 mrg ALLOCNO_CLASS (a) = NO_REGS;
527 1.1 mrg ALLOCNO_UPDATED_CLASS_COST (a) = 0;
528 1.1 mrg ALLOCNO_CLASS_COST (a) = 0;
529 1.1 mrg ALLOCNO_MEMORY_COST (a) = 0;
530 1.1 mrg ALLOCNO_UPDATED_MEMORY_COST (a) = 0;
531 1.1 mrg ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a) = 0;
532 1.1 mrg ALLOCNO_NUM_OBJECTS (a) = 0;
533 1.1 mrg
534 1.1 mrg ALLOCNO_ADD_DATA (a) = NULL;
535 1.1 mrg allocno_vec.safe_push (a);
536 1.1 mrg ira_allocnos = allocno_vec.address ();
537 1.1 mrg ira_allocnos_num = allocno_vec.length ();
538 1.1 mrg
539 1.1 mrg return a;
540 1.1 mrg }
541 1.1 mrg
542 1.1 mrg /* Set up register class for A and update its conflict hard
543 1.1 mrg registers. */
544 1.1 mrg void
545 1.1 mrg ira_set_allocno_class (ira_allocno_t a, enum reg_class aclass)
546 1.1 mrg {
547 1.1 mrg ira_allocno_object_iterator oi;
548 1.1 mrg ira_object_t obj;
549 1.1 mrg
550 1.1 mrg ALLOCNO_CLASS (a) = aclass;
551 1.1 mrg FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
552 1.1 mrg {
553 1.1 mrg OBJECT_CONFLICT_HARD_REGS (obj) |= ~reg_class_contents[aclass];
554 1.1 mrg OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) |= ~reg_class_contents[aclass];
555 1.1 mrg }
556 1.1 mrg }
557 1.1 mrg
558 1.1 mrg /* Determine the number of objects we should associate with allocno A
559 1.1 mrg and allocate them. */
560 1.1 mrg void
561 1.1 mrg ira_create_allocno_objects (ira_allocno_t a)
562 1.1 mrg {
563 1.1 mrg machine_mode mode = ALLOCNO_MODE (a);
564 1.1 mrg enum reg_class aclass = ALLOCNO_CLASS (a);
565 1.1 mrg int n = ira_reg_class_max_nregs[aclass][mode];
566 1.1 mrg int i;
567 1.1 mrg
568 1.1 mrg if (n != 2 || maybe_ne (GET_MODE_SIZE (mode), n * UNITS_PER_WORD))
569 1.1 mrg n = 1;
570 1.1 mrg
571 1.1 mrg ALLOCNO_NUM_OBJECTS (a) = n;
572 1.1 mrg for (i = 0; i < n; i++)
573 1.1 mrg ALLOCNO_OBJECT (a, i) = ira_create_object (a, i);
574 1.1 mrg }
575 1.1 mrg
576 1.1 mrg /* For each allocno, set ALLOCNO_NUM_OBJECTS and create the
577 1.1 mrg ALLOCNO_OBJECT structures. This must be called after the allocno
578 1.1 mrg classes are known. */
579 1.1 mrg static void
580 1.1 mrg create_allocno_objects (void)
581 1.1 mrg {
582 1.1 mrg ira_allocno_t a;
583 1.1 mrg ira_allocno_iterator ai;
584 1.1 mrg
585 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
586 1.1 mrg ira_create_allocno_objects (a);
587 1.1 mrg }
588 1.1 mrg
589 1.1 mrg /* Merge hard register conflict information for all objects associated with
590 1.1 mrg allocno TO into the corresponding objects associated with FROM.
591 1.1 mrg If TOTAL_ONLY is true, we only merge OBJECT_TOTAL_CONFLICT_HARD_REGS. */
592 1.1 mrg static void
593 1.1 mrg merge_hard_reg_conflicts (ira_allocno_t from, ira_allocno_t to,
594 1.1 mrg bool total_only)
595 1.1 mrg {
596 1.1 mrg int i;
597 1.1 mrg gcc_assert (ALLOCNO_NUM_OBJECTS (to) == ALLOCNO_NUM_OBJECTS (from));
598 1.1 mrg for (i = 0; i < ALLOCNO_NUM_OBJECTS (to); i++)
599 1.1 mrg {
600 1.1 mrg ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
601 1.1 mrg ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
602 1.1 mrg
603 1.1 mrg if (!total_only)
604 1.1 mrg OBJECT_CONFLICT_HARD_REGS (to_obj)
605 1.1 mrg |= OBJECT_CONFLICT_HARD_REGS (from_obj);
606 1.1 mrg OBJECT_TOTAL_CONFLICT_HARD_REGS (to_obj)
607 1.1 mrg |= OBJECT_TOTAL_CONFLICT_HARD_REGS (from_obj);
608 1.1 mrg }
609 1.1 mrg #ifdef STACK_REGS
610 1.1 mrg if (!total_only && ALLOCNO_NO_STACK_REG_P (from))
611 1.1 mrg ALLOCNO_NO_STACK_REG_P (to) = true;
612 1.1 mrg if (ALLOCNO_TOTAL_NO_STACK_REG_P (from))
613 1.1 mrg ALLOCNO_TOTAL_NO_STACK_REG_P (to) = true;
614 1.1 mrg #endif
615 1.1 mrg }
616 1.1 mrg
617 1.1 mrg /* Update hard register conflict information for all objects associated with
618 1.1 mrg A to include the regs in SET. */
619 1.1 mrg void
620 1.1 mrg ior_hard_reg_conflicts (ira_allocno_t a, const_hard_reg_set set)
621 1.1 mrg {
622 1.1 mrg ira_allocno_object_iterator i;
623 1.1 mrg ira_object_t obj;
624 1.1 mrg
625 1.1 mrg FOR_EACH_ALLOCNO_OBJECT (a, obj, i)
626 1.1 mrg {
627 1.1 mrg OBJECT_CONFLICT_HARD_REGS (obj) |= set;
628 1.1 mrg OBJECT_TOTAL_CONFLICT_HARD_REGS (obj) |= set;
629 1.1 mrg }
630 1.1 mrg }
631 1.1 mrg
632 1.1 mrg /* Return TRUE if a conflict vector with NUM elements is more
633 1.1 mrg profitable than a conflict bit vector for OBJ. */
634 1.1 mrg bool
635 1.1 mrg ira_conflict_vector_profitable_p (ira_object_t obj, int num)
636 1.1 mrg {
637 1.1 mrg int nbytes;
638 1.1 mrg int max = OBJECT_MAX (obj);
639 1.1 mrg int min = OBJECT_MIN (obj);
640 1.1 mrg
641 1.1 mrg if (max < min)
642 1.1 mrg /* We prefer a bit vector in such case because it does not result
643 1.1 mrg in allocation. */
644 1.1 mrg return false;
645 1.1 mrg
646 1.1 mrg nbytes = (max - min) / 8 + 1;
647 1.1 mrg STATIC_ASSERT (sizeof (ira_object_t) <= 8);
648 1.1 mrg /* Don't use sizeof (ira_object_t), use constant 8. Size of ira_object_t (a
649 1.1 mrg pointer) is different on 32-bit and 64-bit targets. Usage sizeof
650 1.1 mrg (ira_object_t) can result in different code generation by GCC built as 32-
651 1.1 mrg and 64-bit program. In any case the profitability is just an estimation
652 1.1 mrg and border cases are rare. */
653 1.1 mrg return (2 * 8 /* sizeof (ira_object_t) */ * (num + 1) < 3 * nbytes);
654 1.1 mrg }
655 1.1 mrg
656 1.1 mrg /* Allocates and initialize the conflict vector of OBJ for NUM
657 1.1 mrg conflicting objects. */
658 1.1 mrg void
659 1.1 mrg ira_allocate_conflict_vec (ira_object_t obj, int num)
660 1.1 mrg {
661 1.1 mrg int size;
662 1.1 mrg ira_object_t *vec;
663 1.1 mrg
664 1.1 mrg ira_assert (OBJECT_CONFLICT_ARRAY (obj) == NULL);
665 1.1 mrg num++; /* for NULL end marker */
666 1.1 mrg size = sizeof (ira_object_t) * num;
667 1.1 mrg OBJECT_CONFLICT_ARRAY (obj) = ira_allocate (size);
668 1.1 mrg vec = (ira_object_t *) OBJECT_CONFLICT_ARRAY (obj);
669 1.1 mrg vec[0] = NULL;
670 1.1 mrg OBJECT_NUM_CONFLICTS (obj) = 0;
671 1.1 mrg OBJECT_CONFLICT_ARRAY_SIZE (obj) = size;
672 1.1 mrg OBJECT_CONFLICT_VEC_P (obj) = true;
673 1.1 mrg }
674 1.1 mrg
675 1.1 mrg /* Allocate and initialize the conflict bit vector of OBJ. */
676 1.1 mrg static void
677 1.1 mrg allocate_conflict_bit_vec (ira_object_t obj)
678 1.1 mrg {
679 1.1 mrg unsigned int size;
680 1.1 mrg
681 1.1 mrg ira_assert (OBJECT_CONFLICT_ARRAY (obj) == NULL);
682 1.1 mrg size = ((OBJECT_MAX (obj) - OBJECT_MIN (obj) + IRA_INT_BITS)
683 1.1 mrg / IRA_INT_BITS * sizeof (IRA_INT_TYPE));
684 1.1 mrg OBJECT_CONFLICT_ARRAY (obj) = ira_allocate (size);
685 1.1 mrg memset (OBJECT_CONFLICT_ARRAY (obj), 0, size);
686 1.1 mrg OBJECT_CONFLICT_ARRAY_SIZE (obj) = size;
687 1.1 mrg OBJECT_CONFLICT_VEC_P (obj) = false;
688 1.1 mrg }
689 1.1 mrg
690 1.1 mrg /* Allocate and initialize the conflict vector or conflict bit vector
691 1.1 mrg of OBJ for NUM conflicting allocnos whatever is more profitable. */
692 1.1 mrg void
693 1.1 mrg ira_allocate_object_conflicts (ira_object_t obj, int num)
694 1.1 mrg {
695 1.1 mrg if (ira_conflict_vector_profitable_p (obj, num))
696 1.1 mrg ira_allocate_conflict_vec (obj, num);
697 1.1 mrg else
698 1.1 mrg allocate_conflict_bit_vec (obj);
699 1.1 mrg }
700 1.1 mrg
701 1.1 mrg /* Add OBJ2 to the conflicts of OBJ1. */
702 1.1 mrg static void
703 1.1 mrg add_to_conflicts (ira_object_t obj1, ira_object_t obj2)
704 1.1 mrg {
705 1.1 mrg int num;
706 1.1 mrg unsigned int size;
707 1.1 mrg
708 1.1 mrg if (OBJECT_CONFLICT_VEC_P (obj1))
709 1.1 mrg {
710 1.1 mrg ira_object_t *vec = OBJECT_CONFLICT_VEC (obj1);
711 1.1 mrg int curr_num = OBJECT_NUM_CONFLICTS (obj1);
712 1.1 mrg num = curr_num + 2;
713 1.1 mrg if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) < num * sizeof (ira_object_t))
714 1.1 mrg {
715 1.1 mrg ira_object_t *newvec;
716 1.1 mrg size = (3 * num / 2 + 1) * sizeof (ira_allocno_t);
717 1.1 mrg newvec = (ira_object_t *) ira_allocate (size);
718 1.1 mrg memcpy (newvec, vec, curr_num * sizeof (ira_object_t));
719 1.1 mrg ira_free (vec);
720 1.1 mrg vec = newvec;
721 1.1 mrg OBJECT_CONFLICT_ARRAY (obj1) = vec;
722 1.1 mrg OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
723 1.1 mrg }
724 1.1 mrg vec[num - 2] = obj2;
725 1.1 mrg vec[num - 1] = NULL;
726 1.1 mrg OBJECT_NUM_CONFLICTS (obj1)++;
727 1.1 mrg }
728 1.1 mrg else
729 1.1 mrg {
730 1.1 mrg int nw, added_head_nw, id;
731 1.1 mrg IRA_INT_TYPE *vec = OBJECT_CONFLICT_BITVEC (obj1);
732 1.1 mrg
733 1.1 mrg id = OBJECT_CONFLICT_ID (obj2);
734 1.1 mrg if (OBJECT_MIN (obj1) > id)
735 1.1 mrg {
736 1.1 mrg /* Expand head of the bit vector. */
737 1.1 mrg added_head_nw = (OBJECT_MIN (obj1) - id - 1) / IRA_INT_BITS + 1;
738 1.1 mrg nw = (OBJECT_MAX (obj1) - OBJECT_MIN (obj1)) / IRA_INT_BITS + 1;
739 1.1 mrg size = (nw + added_head_nw) * sizeof (IRA_INT_TYPE);
740 1.1 mrg if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) >= size)
741 1.1 mrg {
742 1.1 mrg memmove ((char *) vec + added_head_nw * sizeof (IRA_INT_TYPE),
743 1.1 mrg vec, nw * sizeof (IRA_INT_TYPE));
744 1.1 mrg memset (vec, 0, added_head_nw * sizeof (IRA_INT_TYPE));
745 1.1 mrg }
746 1.1 mrg else
747 1.1 mrg {
748 1.1 mrg size
749 1.1 mrg = (3 * (nw + added_head_nw) / 2 + 1) * sizeof (IRA_INT_TYPE);
750 1.1 mrg vec = (IRA_INT_TYPE *) ira_allocate (size);
751 1.1 mrg memcpy ((char *) vec + added_head_nw * sizeof (IRA_INT_TYPE),
752 1.1 mrg OBJECT_CONFLICT_ARRAY (obj1), nw * sizeof (IRA_INT_TYPE));
753 1.1 mrg memset (vec, 0, added_head_nw * sizeof (IRA_INT_TYPE));
754 1.1 mrg memset ((char *) vec
755 1.1 mrg + (nw + added_head_nw) * sizeof (IRA_INT_TYPE),
756 1.1 mrg 0, size - (nw + added_head_nw) * sizeof (IRA_INT_TYPE));
757 1.1 mrg ira_free (OBJECT_CONFLICT_ARRAY (obj1));
758 1.1 mrg OBJECT_CONFLICT_ARRAY (obj1) = vec;
759 1.1 mrg OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
760 1.1 mrg }
761 1.1 mrg OBJECT_MIN (obj1) -= added_head_nw * IRA_INT_BITS;
762 1.1 mrg }
763 1.1 mrg else if (OBJECT_MAX (obj1) < id)
764 1.1 mrg {
765 1.1 mrg nw = (id - OBJECT_MIN (obj1)) / IRA_INT_BITS + 1;
766 1.1 mrg size = nw * sizeof (IRA_INT_TYPE);
767 1.1 mrg if (OBJECT_CONFLICT_ARRAY_SIZE (obj1) < size)
768 1.1 mrg {
769 1.1 mrg /* Expand tail of the bit vector. */
770 1.1 mrg size = (3 * nw / 2 + 1) * sizeof (IRA_INT_TYPE);
771 1.1 mrg vec = (IRA_INT_TYPE *) ira_allocate (size);
772 1.1 mrg memcpy (vec, OBJECT_CONFLICT_ARRAY (obj1), OBJECT_CONFLICT_ARRAY_SIZE (obj1));
773 1.1 mrg memset ((char *) vec + OBJECT_CONFLICT_ARRAY_SIZE (obj1),
774 1.1 mrg 0, size - OBJECT_CONFLICT_ARRAY_SIZE (obj1));
775 1.1 mrg ira_free (OBJECT_CONFLICT_ARRAY (obj1));
776 1.1 mrg OBJECT_CONFLICT_ARRAY (obj1) = vec;
777 1.1 mrg OBJECT_CONFLICT_ARRAY_SIZE (obj1) = size;
778 1.1 mrg }
779 1.1 mrg OBJECT_MAX (obj1) = id;
780 1.1 mrg }
781 1.1 mrg SET_MINMAX_SET_BIT (vec, id, OBJECT_MIN (obj1), OBJECT_MAX (obj1));
782 1.1 mrg }
783 1.1 mrg }
784 1.1 mrg
785 1.1 mrg /* Add OBJ1 to the conflicts of OBJ2 and vice versa. */
786 1.1 mrg static void
787 1.1 mrg ira_add_conflict (ira_object_t obj1, ira_object_t obj2)
788 1.1 mrg {
789 1.1 mrg add_to_conflicts (obj1, obj2);
790 1.1 mrg add_to_conflicts (obj2, obj1);
791 1.1 mrg }
792 1.1 mrg
793 1.1 mrg /* Clear all conflicts of OBJ. */
794 1.1 mrg static void
795 1.1 mrg clear_conflicts (ira_object_t obj)
796 1.1 mrg {
797 1.1 mrg if (OBJECT_CONFLICT_VEC_P (obj))
798 1.1 mrg {
799 1.1 mrg OBJECT_NUM_CONFLICTS (obj) = 0;
800 1.1 mrg OBJECT_CONFLICT_VEC (obj)[0] = NULL;
801 1.1 mrg }
802 1.1 mrg else if (OBJECT_CONFLICT_ARRAY_SIZE (obj) != 0)
803 1.1 mrg {
804 1.1 mrg int nw;
805 1.1 mrg
806 1.1 mrg nw = (OBJECT_MAX (obj) - OBJECT_MIN (obj)) / IRA_INT_BITS + 1;
807 1.1 mrg memset (OBJECT_CONFLICT_BITVEC (obj), 0, nw * sizeof (IRA_INT_TYPE));
808 1.1 mrg }
809 1.1 mrg }
810 1.1 mrg
811 1.1 mrg /* The array used to find duplications in conflict vectors of
812 1.1 mrg allocnos. */
813 1.1 mrg static int *conflict_check;
814 1.1 mrg
815 1.1 mrg /* The value used to mark allocation presence in conflict vector of
816 1.1 mrg the current allocno. */
817 1.1 mrg static int curr_conflict_check_tick;
818 1.1 mrg
819 1.1 mrg /* Remove duplications in conflict vector of OBJ. */
820 1.1 mrg static void
821 1.1 mrg compress_conflict_vec (ira_object_t obj)
822 1.1 mrg {
823 1.1 mrg ira_object_t *vec, conflict_obj;
824 1.1 mrg int i, j;
825 1.1 mrg
826 1.1 mrg ira_assert (OBJECT_CONFLICT_VEC_P (obj));
827 1.1 mrg vec = OBJECT_CONFLICT_VEC (obj);
828 1.1 mrg curr_conflict_check_tick++;
829 1.1 mrg for (i = j = 0; (conflict_obj = vec[i]) != NULL; i++)
830 1.1 mrg {
831 1.1 mrg int id = OBJECT_CONFLICT_ID (conflict_obj);
832 1.1 mrg if (conflict_check[id] != curr_conflict_check_tick)
833 1.1 mrg {
834 1.1 mrg conflict_check[id] = curr_conflict_check_tick;
835 1.1 mrg vec[j++] = conflict_obj;
836 1.1 mrg }
837 1.1 mrg }
838 1.1 mrg OBJECT_NUM_CONFLICTS (obj) = j;
839 1.1 mrg vec[j] = NULL;
840 1.1 mrg }
841 1.1 mrg
842 1.1 mrg /* Remove duplications in conflict vectors of all allocnos. */
843 1.1 mrg static void
844 1.1 mrg compress_conflict_vecs (void)
845 1.1 mrg {
846 1.1 mrg ira_object_t obj;
847 1.1 mrg ira_object_iterator oi;
848 1.1 mrg
849 1.1 mrg conflict_check = (int *) ira_allocate (sizeof (int) * ira_objects_num);
850 1.1 mrg memset (conflict_check, 0, sizeof (int) * ira_objects_num);
851 1.1 mrg curr_conflict_check_tick = 0;
852 1.1 mrg FOR_EACH_OBJECT (obj, oi)
853 1.1 mrg {
854 1.1 mrg if (OBJECT_CONFLICT_VEC_P (obj))
855 1.1 mrg compress_conflict_vec (obj);
856 1.1 mrg }
857 1.1 mrg ira_free (conflict_check);
858 1.1 mrg }
859 1.1 mrg
860 1.1 mrg /* This recursive function outputs allocno A and if it is a cap the
861 1.1 mrg function outputs its members. */
862 1.1 mrg void
863 1.1 mrg ira_print_expanded_allocno (ira_allocno_t a)
864 1.1 mrg {
865 1.1 mrg basic_block bb;
866 1.1 mrg
867 1.1 mrg fprintf (ira_dump_file, " a%d(r%d", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
868 1.1 mrg if ((bb = ALLOCNO_LOOP_TREE_NODE (a)->bb) != NULL)
869 1.1 mrg fprintf (ira_dump_file, ",b%d", bb->index);
870 1.1 mrg else
871 1.1 mrg fprintf (ira_dump_file, ",l%d", ALLOCNO_LOOP_TREE_NODE (a)->loop_num);
872 1.1 mrg if (ALLOCNO_CAP_MEMBER (a) != NULL)
873 1.1 mrg {
874 1.1 mrg fprintf (ira_dump_file, ":");
875 1.1 mrg ira_print_expanded_allocno (ALLOCNO_CAP_MEMBER (a));
876 1.1 mrg }
877 1.1 mrg fprintf (ira_dump_file, ")");
878 1.1 mrg }
879 1.1 mrg
880 1.1 mrg /* Create and return the cap representing allocno A in the
881 1.1 mrg parent loop. */
882 1.1 mrg static ira_allocno_t
883 1.1 mrg create_cap_allocno (ira_allocno_t a)
884 1.1 mrg {
885 1.1 mrg ira_allocno_t cap;
886 1.1 mrg ira_loop_tree_node_t parent;
887 1.1 mrg enum reg_class aclass;
888 1.1 mrg
889 1.1 mrg parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
890 1.1 mrg cap = ira_create_allocno (ALLOCNO_REGNO (a), true, parent);
891 1.1 mrg ALLOCNO_MODE (cap) = ALLOCNO_MODE (a);
892 1.1 mrg ALLOCNO_WMODE (cap) = ALLOCNO_WMODE (a);
893 1.1 mrg aclass = ALLOCNO_CLASS (a);
894 1.1 mrg ira_set_allocno_class (cap, aclass);
895 1.1 mrg ira_create_allocno_objects (cap);
896 1.1 mrg ALLOCNO_CAP_MEMBER (cap) = a;
897 1.1 mrg ALLOCNO_CAP (a) = cap;
898 1.1 mrg ALLOCNO_CLASS_COST (cap) = ALLOCNO_CLASS_COST (a);
899 1.1 mrg ALLOCNO_MEMORY_COST (cap) = ALLOCNO_MEMORY_COST (a);
900 1.1 mrg ira_allocate_and_copy_costs
901 1.1 mrg (&ALLOCNO_HARD_REG_COSTS (cap), aclass, ALLOCNO_HARD_REG_COSTS (a));
902 1.1 mrg ira_allocate_and_copy_costs
903 1.1 mrg (&ALLOCNO_CONFLICT_HARD_REG_COSTS (cap), aclass,
904 1.1 mrg ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
905 1.1 mrg ALLOCNO_BAD_SPILL_P (cap) = ALLOCNO_BAD_SPILL_P (a);
906 1.1 mrg ALLOCNO_NREFS (cap) = ALLOCNO_NREFS (a);
907 1.1 mrg ALLOCNO_FREQ (cap) = ALLOCNO_FREQ (a);
908 1.1 mrg ALLOCNO_CALL_FREQ (cap) = ALLOCNO_CALL_FREQ (a);
909 1.1 mrg
910 1.1 mrg merge_hard_reg_conflicts (a, cap, false);
911 1.1 mrg
912 1.1 mrg ALLOCNO_CALLS_CROSSED_NUM (cap) = ALLOCNO_CALLS_CROSSED_NUM (a);
913 1.1 mrg ALLOCNO_CHEAP_CALLS_CROSSED_NUM (cap) = ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
914 1.1 mrg ALLOCNO_CROSSED_CALLS_ABIS (cap) = ALLOCNO_CROSSED_CALLS_ABIS (a);
915 1.1 mrg ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (cap)
916 1.1 mrg = ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a);
917 1.1 mrg if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
918 1.1 mrg {
919 1.1 mrg fprintf (ira_dump_file, " Creating cap ");
920 1.1 mrg ira_print_expanded_allocno (cap);
921 1.1 mrg fprintf (ira_dump_file, "\n");
922 1.1 mrg }
923 1.1 mrg return cap;
924 1.1 mrg }
925 1.1 mrg
926 1.1 mrg /* Create and return a live range for OBJECT with given attributes. */
927 1.1 mrg live_range_t
928 1.1 mrg ira_create_live_range (ira_object_t obj, int start, int finish,
929 1.1 mrg live_range_t next)
930 1.1 mrg {
931 1.1 mrg live_range_t p;
932 1.1 mrg
933 1.1 mrg p = live_range_pool.allocate ();
934 1.1 mrg p->object = obj;
935 1.1 mrg p->start = start;
936 1.1 mrg p->finish = finish;
937 1.1 mrg p->next = next;
938 1.1 mrg return p;
939 1.1 mrg }
940 1.1 mrg
941 1.1 mrg /* Create a new live range for OBJECT and queue it at the head of its
942 1.1 mrg live range list. */
943 1.1 mrg void
944 1.1 mrg ira_add_live_range_to_object (ira_object_t object, int start, int finish)
945 1.1 mrg {
946 1.1 mrg live_range_t p;
947 1.1 mrg p = ira_create_live_range (object, start, finish,
948 1.1 mrg OBJECT_LIVE_RANGES (object));
949 1.1 mrg OBJECT_LIVE_RANGES (object) = p;
950 1.1 mrg }
951 1.1 mrg
952 1.1 mrg /* Copy allocno live range R and return the result. */
953 1.1 mrg static live_range_t
954 1.1 mrg copy_live_range (live_range_t r)
955 1.1 mrg {
956 1.1 mrg live_range_t p;
957 1.1 mrg
958 1.1 mrg p = live_range_pool.allocate ();
959 1.1 mrg *p = *r;
960 1.1 mrg return p;
961 1.1 mrg }
962 1.1 mrg
963 1.1 mrg /* Copy allocno live range list given by its head R and return the
964 1.1 mrg result. */
965 1.1 mrg live_range_t
966 1.1 mrg ira_copy_live_range_list (live_range_t r)
967 1.1 mrg {
968 1.1 mrg live_range_t p, first, last;
969 1.1 mrg
970 1.1 mrg if (r == NULL)
971 1.1 mrg return NULL;
972 1.1 mrg for (first = last = NULL; r != NULL; r = r->next)
973 1.1 mrg {
974 1.1 mrg p = copy_live_range (r);
975 1.1 mrg if (first == NULL)
976 1.1 mrg first = p;
977 1.1 mrg else
978 1.1 mrg last->next = p;
979 1.1 mrg last = p;
980 1.1 mrg }
981 1.1 mrg return first;
982 1.1 mrg }
983 1.1 mrg
984 1.1 mrg /* Merge ranges R1 and R2 and returns the result. The function
985 1.1 mrg maintains the order of ranges and tries to minimize number of the
986 1.1 mrg result ranges. */
987 1.1 mrg live_range_t
988 1.1 mrg ira_merge_live_ranges (live_range_t r1, live_range_t r2)
989 1.1 mrg {
990 1.1 mrg live_range_t first, last;
991 1.1 mrg
992 1.1 mrg if (r1 == NULL)
993 1.1 mrg return r2;
994 1.1 mrg if (r2 == NULL)
995 1.1 mrg return r1;
996 1.1 mrg for (first = last = NULL; r1 != NULL && r2 != NULL;)
997 1.1 mrg {
998 1.1 mrg if (r1->start < r2->start)
999 1.1 mrg std::swap (r1, r2);
1000 1.1 mrg if (r1->start <= r2->finish + 1)
1001 1.1 mrg {
1002 1.1 mrg /* Intersected ranges: merge r1 and r2 into r1. */
1003 1.1 mrg r1->start = r2->start;
1004 1.1 mrg if (r1->finish < r2->finish)
1005 1.1 mrg r1->finish = r2->finish;
1006 1.1 mrg live_range_t temp = r2;
1007 1.1 mrg r2 = r2->next;
1008 1.1 mrg ira_finish_live_range (temp);
1009 1.1 mrg if (r2 == NULL)
1010 1.1 mrg {
1011 1.1 mrg /* To try to merge with subsequent ranges in r1. */
1012 1.1 mrg r2 = r1->next;
1013 1.1 mrg r1->next = NULL;
1014 1.1 mrg }
1015 1.1 mrg }
1016 1.1 mrg else
1017 1.1 mrg {
1018 1.1 mrg /* Add r1 to the result. */
1019 1.1 mrg if (first == NULL)
1020 1.1 mrg first = last = r1;
1021 1.1 mrg else
1022 1.1 mrg {
1023 1.1 mrg last->next = r1;
1024 1.1 mrg last = r1;
1025 1.1 mrg }
1026 1.1 mrg r1 = r1->next;
1027 1.1 mrg if (r1 == NULL)
1028 1.1 mrg {
1029 1.1 mrg /* To try to merge with subsequent ranges in r2. */
1030 1.1 mrg r1 = r2->next;
1031 1.1 mrg r2->next = NULL;
1032 1.1 mrg }
1033 1.1 mrg }
1034 1.1 mrg }
1035 1.1 mrg if (r1 != NULL)
1036 1.1 mrg {
1037 1.1 mrg if (first == NULL)
1038 1.1 mrg first = r1;
1039 1.1 mrg else
1040 1.1 mrg last->next = r1;
1041 1.1 mrg ira_assert (r1->next == NULL);
1042 1.1 mrg }
1043 1.1 mrg else if (r2 != NULL)
1044 1.1 mrg {
1045 1.1 mrg if (first == NULL)
1046 1.1 mrg first = r2;
1047 1.1 mrg else
1048 1.1 mrg last->next = r2;
1049 1.1 mrg ira_assert (r2->next == NULL);
1050 1.1 mrg }
1051 1.1 mrg else
1052 1.1 mrg {
1053 1.1 mrg ira_assert (last->next == NULL);
1054 1.1 mrg }
1055 1.1 mrg return first;
1056 1.1 mrg }
1057 1.1 mrg
1058 1.1 mrg /* Return TRUE if live ranges R1 and R2 intersect. */
1059 1.1 mrg bool
1060 1.1 mrg ira_live_ranges_intersect_p (live_range_t r1, live_range_t r2)
1061 1.1 mrg {
1062 1.1 mrg /* Remember the live ranges are always kept ordered. */
1063 1.1 mrg while (r1 != NULL && r2 != NULL)
1064 1.1 mrg {
1065 1.1 mrg if (r1->start > r2->finish)
1066 1.1 mrg r1 = r1->next;
1067 1.1 mrg else if (r2->start > r1->finish)
1068 1.1 mrg r2 = r2->next;
1069 1.1 mrg else
1070 1.1 mrg return true;
1071 1.1 mrg }
1072 1.1 mrg return false;
1073 1.1 mrg }
1074 1.1 mrg
1075 1.1 mrg /* Free allocno live range R. */
1076 1.1 mrg void
1077 1.1 mrg ira_finish_live_range (live_range_t r)
1078 1.1 mrg {
1079 1.1 mrg live_range_pool.remove (r);
1080 1.1 mrg }
1081 1.1 mrg
1082 1.1 mrg /* Free list of allocno live ranges starting with R. */
1083 1.1 mrg void
1084 1.1 mrg ira_finish_live_range_list (live_range_t r)
1085 1.1 mrg {
1086 1.1 mrg live_range_t next_r;
1087 1.1 mrg
1088 1.1 mrg for (; r != NULL; r = next_r)
1089 1.1 mrg {
1090 1.1 mrg next_r = r->next;
1091 1.1 mrg ira_finish_live_range (r);
1092 1.1 mrg }
1093 1.1 mrg }
1094 1.1 mrg
1095 1.1 mrg /* Free updated register costs of allocno A. */
1096 1.1 mrg void
1097 1.1 mrg ira_free_allocno_updated_costs (ira_allocno_t a)
1098 1.1 mrg {
1099 1.1 mrg enum reg_class aclass;
1100 1.1 mrg
1101 1.1 mrg aclass = ALLOCNO_CLASS (a);
1102 1.1 mrg if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
1103 1.1 mrg ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass);
1104 1.1 mrg ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
1105 1.1 mrg if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
1106 1.1 mrg ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1107 1.1 mrg aclass);
1108 1.1 mrg ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
1109 1.1 mrg }
1110 1.1 mrg
1111 1.1 mrg /* Free and nullify all cost vectors allocated earlier for allocno
1112 1.1 mrg A. */
1113 1.1 mrg static void
1114 1.1 mrg ira_free_allocno_costs (ira_allocno_t a)
1115 1.1 mrg {
1116 1.1 mrg enum reg_class aclass = ALLOCNO_CLASS (a);
1117 1.1 mrg ira_object_t obj;
1118 1.1 mrg ira_allocno_object_iterator oi;
1119 1.1 mrg
1120 1.1 mrg FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
1121 1.1 mrg {
1122 1.1 mrg ira_finish_live_range_list (OBJECT_LIVE_RANGES (obj));
1123 1.1 mrg ira_object_id_map[OBJECT_CONFLICT_ID (obj)] = NULL;
1124 1.1 mrg if (OBJECT_CONFLICT_ARRAY (obj) != NULL)
1125 1.1 mrg ira_free (OBJECT_CONFLICT_ARRAY (obj));
1126 1.1 mrg object_pool.remove (obj);
1127 1.1 mrg }
1128 1.1 mrg
1129 1.1 mrg ira_allocnos[ALLOCNO_NUM (a)] = NULL;
1130 1.1 mrg if (ALLOCNO_HARD_REG_COSTS (a) != NULL)
1131 1.1 mrg ira_free_cost_vector (ALLOCNO_HARD_REG_COSTS (a), aclass);
1132 1.1 mrg if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL)
1133 1.1 mrg ira_free_cost_vector (ALLOCNO_CONFLICT_HARD_REG_COSTS (a), aclass);
1134 1.1 mrg if (ALLOCNO_UPDATED_HARD_REG_COSTS (a) != NULL)
1135 1.1 mrg ira_free_cost_vector (ALLOCNO_UPDATED_HARD_REG_COSTS (a), aclass);
1136 1.1 mrg if (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) != NULL)
1137 1.1 mrg ira_free_cost_vector (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a),
1138 1.1 mrg aclass);
1139 1.1 mrg ALLOCNO_HARD_REG_COSTS (a) = NULL;
1140 1.1 mrg ALLOCNO_CONFLICT_HARD_REG_COSTS (a) = NULL;
1141 1.1 mrg ALLOCNO_UPDATED_HARD_REG_COSTS (a) = NULL;
1142 1.1 mrg ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) = NULL;
1143 1.1 mrg }
1144 1.1 mrg
1145 1.1 mrg /* Free the memory allocated for allocno A. */
1146 1.1 mrg static void
1147 1.1 mrg finish_allocno (ira_allocno_t a)
1148 1.1 mrg {
1149 1.1 mrg ira_free_allocno_costs (a);
1150 1.1 mrg allocno_pool.remove (a);
1151 1.1 mrg }
1152 1.1 mrg
1153 1.1 mrg /* Free the memory allocated for all allocnos. */
1154 1.1 mrg static void
1155 1.1 mrg finish_allocnos (void)
1156 1.1 mrg {
1157 1.1 mrg ira_allocno_t a;
1158 1.1 mrg ira_allocno_iterator ai;
1159 1.1 mrg
1160 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
1161 1.1 mrg finish_allocno (a);
1162 1.1 mrg ira_free (ira_regno_allocno_map);
1163 1.1 mrg ira_object_id_map_vec.release ();
1164 1.1 mrg allocno_vec.release ();
1165 1.1 mrg allocno_pool.release ();
1166 1.1 mrg object_pool.release ();
1167 1.1 mrg live_range_pool.release ();
1168 1.1 mrg }
1169 1.1 mrg
1170 1.1 mrg
1171 1.1 mrg
1173 1.1 mrg /* Pools for allocno preferences. */
1174 1.1 mrg static object_allocator <ira_allocno_pref> pref_pool ("prefs");
1175 1.1 mrg
1176 1.1 mrg /* Vec containing references to all created preferences. It is a
1177 1.1 mrg container of array ira_prefs. */
1178 1.1 mrg static vec<ira_pref_t> pref_vec;
1179 1.1 mrg
1180 1.1 mrg /* The function initializes data concerning allocno prefs. */
1181 1.1 mrg static void
1182 1.1 mrg initiate_prefs (void)
1183 1.1 mrg {
1184 1.1 mrg pref_vec.create (get_max_uid ());
1185 1.1 mrg ira_prefs = NULL;
1186 1.1 mrg ira_prefs_num = 0;
1187 1.1 mrg }
1188 1.1 mrg
1189 1.1 mrg /* Return pref for A and HARD_REGNO if any. */
1190 1.1 mrg static ira_pref_t
1191 1.1 mrg find_allocno_pref (ira_allocno_t a, int hard_regno)
1192 1.1 mrg {
1193 1.1 mrg ira_pref_t pref;
1194 1.1 mrg
1195 1.1 mrg for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
1196 1.1 mrg if (pref->allocno == a && pref->hard_regno == hard_regno)
1197 1.1 mrg return pref;
1198 1.1 mrg return NULL;
1199 1.1 mrg }
1200 1.1 mrg
1201 1.1 mrg /* Create and return pref with given attributes A, HARD_REGNO, and FREQ. */
1202 1.1 mrg ira_pref_t
1203 1.1 mrg ira_create_pref (ira_allocno_t a, int hard_regno, int freq)
1204 1.1 mrg {
1205 1.1 mrg ira_pref_t pref;
1206 1.1 mrg
1207 1.1 mrg pref = pref_pool.allocate ();
1208 1.1 mrg pref->num = ira_prefs_num;
1209 1.1 mrg pref->allocno = a;
1210 1.1 mrg pref->hard_regno = hard_regno;
1211 1.1 mrg pref->freq = freq;
1212 1.1 mrg pref_vec.safe_push (pref);
1213 1.1 mrg ira_prefs = pref_vec.address ();
1214 1.1 mrg ira_prefs_num = pref_vec.length ();
1215 1.1 mrg return pref;
1216 1.1 mrg }
1217 1.1 mrg
1218 1.1 mrg /* Attach a pref PREF to the corresponding allocno. */
1219 1.1 mrg static void
1220 1.1 mrg add_allocno_pref_to_list (ira_pref_t pref)
1221 1.1 mrg {
1222 1.1 mrg ira_allocno_t a = pref->allocno;
1223 1.1 mrg
1224 1.1 mrg pref->next_pref = ALLOCNO_PREFS (a);
1225 1.1 mrg ALLOCNO_PREFS (a) = pref;
1226 1.1 mrg }
1227 1.1 mrg
1228 1.1 mrg /* Create (or update frequency if the pref already exists) the pref of
1229 1.1 mrg allocnos A preferring HARD_REGNO with frequency FREQ. */
1230 1.1 mrg void
1231 1.1 mrg ira_add_allocno_pref (ira_allocno_t a, int hard_regno, int freq)
1232 1.1 mrg {
1233 1.1 mrg ira_pref_t pref;
1234 1.1 mrg
1235 1.1 mrg if (freq <= 0)
1236 1.1 mrg return;
1237 1.1 mrg if ((pref = find_allocno_pref (a, hard_regno)) != NULL)
1238 1.1 mrg {
1239 1.1 mrg pref->freq += freq;
1240 1.1 mrg return;
1241 1.1 mrg }
1242 1.1 mrg pref = ira_create_pref (a, hard_regno, freq);
1243 1.1 mrg ira_assert (a != NULL);
1244 1.1 mrg add_allocno_pref_to_list (pref);
1245 1.1 mrg }
1246 1.1 mrg
1247 1.1 mrg /* Print info about PREF into file F. */
1248 1.1 mrg static void
1249 1.1 mrg print_pref (FILE *f, ira_pref_t pref)
1250 1.1 mrg {
1251 1.1 mrg fprintf (f, " pref%d:a%d(r%d)<-hr%d@%d\n", pref->num,
1252 1.1 mrg ALLOCNO_NUM (pref->allocno), ALLOCNO_REGNO (pref->allocno),
1253 1.1 mrg pref->hard_regno, pref->freq);
1254 1.1 mrg }
1255 1.1 mrg
1256 1.1 mrg /* Print info about PREF into stderr. */
1257 1.1 mrg void
1258 1.1 mrg ira_debug_pref (ira_pref_t pref)
1259 1.1 mrg {
1260 1.1 mrg print_pref (stderr, pref);
1261 1.1 mrg }
1262 1.1 mrg
1263 1.1 mrg /* Print info about all prefs into file F. */
1264 1.1 mrg static void
1265 1.1 mrg print_prefs (FILE *f)
1266 1.1 mrg {
1267 1.1 mrg ira_pref_t pref;
1268 1.1 mrg ira_pref_iterator pi;
1269 1.1 mrg
1270 1.1 mrg FOR_EACH_PREF (pref, pi)
1271 1.1 mrg print_pref (f, pref);
1272 1.1 mrg }
1273 1.1 mrg
1274 1.1 mrg /* Print info about all prefs into stderr. */
1275 1.1 mrg void
1276 1.1 mrg ira_debug_prefs (void)
1277 1.1 mrg {
1278 1.1 mrg print_prefs (stderr);
1279 1.1 mrg }
1280 1.1 mrg
1281 1.1 mrg /* Print info about prefs involving allocno A into file F. */
1282 1.1 mrg static void
1283 1.1 mrg print_allocno_prefs (FILE *f, ira_allocno_t a)
1284 1.1 mrg {
1285 1.1 mrg ira_pref_t pref;
1286 1.1 mrg
1287 1.1 mrg fprintf (f, " a%d(r%d):", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
1288 1.1 mrg for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = pref->next_pref)
1289 1.1 mrg fprintf (f, " pref%d:hr%d@%d", pref->num, pref->hard_regno, pref->freq);
1290 1.1 mrg fprintf (f, "\n");
1291 1.1 mrg }
1292 1.1 mrg
1293 1.1 mrg /* Print info about prefs involving allocno A into stderr. */
1294 1.1 mrg void
1295 1.1 mrg ira_debug_allocno_prefs (ira_allocno_t a)
1296 1.1 mrg {
1297 1.1 mrg print_allocno_prefs (stderr, a);
1298 1.1 mrg }
1299 1.1 mrg
1300 1.1 mrg /* The function frees memory allocated for PREF. */
1301 1.1 mrg static void
1302 1.1 mrg finish_pref (ira_pref_t pref)
1303 1.1 mrg {
1304 1.1 mrg ira_prefs[pref->num] = NULL;
1305 1.1 mrg pref_pool.remove (pref);
1306 1.1 mrg }
1307 1.1 mrg
1308 1.1 mrg /* Remove PREF from the list of allocno prefs and free memory for
1309 1.1 mrg it. */
1310 1.1 mrg void
1311 1.1 mrg ira_remove_pref (ira_pref_t pref)
1312 1.1 mrg {
1313 1.1 mrg ira_pref_t cpref, prev;
1314 1.1 mrg
1315 1.1 mrg if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
1316 1.1 mrg fprintf (ira_dump_file, " Removing pref%d:hr%d@%d\n",
1317 1.1 mrg pref->num, pref->hard_regno, pref->freq);
1318 1.1 mrg for (prev = NULL, cpref = ALLOCNO_PREFS (pref->allocno);
1319 1.1 mrg cpref != NULL;
1320 1.1 mrg prev = cpref, cpref = cpref->next_pref)
1321 1.1 mrg if (cpref == pref)
1322 1.1 mrg break;
1323 1.1 mrg ira_assert (cpref != NULL);
1324 1.1 mrg if (prev == NULL)
1325 1.1 mrg ALLOCNO_PREFS (pref->allocno) = pref->next_pref;
1326 1.1 mrg else
1327 1.1 mrg prev->next_pref = pref->next_pref;
1328 1.1 mrg finish_pref (pref);
1329 1.1 mrg }
1330 1.1 mrg
1331 1.1 mrg /* Remove all prefs of allocno A. */
1332 1.1 mrg void
1333 1.1 mrg ira_remove_allocno_prefs (ira_allocno_t a)
1334 1.1 mrg {
1335 1.1 mrg ira_pref_t pref, next_pref;
1336 1.1 mrg
1337 1.1 mrg for (pref = ALLOCNO_PREFS (a); pref != NULL; pref = next_pref)
1338 1.1 mrg {
1339 1.1 mrg next_pref = pref->next_pref;
1340 1.1 mrg finish_pref (pref);
1341 1.1 mrg }
1342 1.1 mrg ALLOCNO_PREFS (a) = NULL;
1343 1.1 mrg }
1344 1.1 mrg
1345 1.1 mrg /* Free memory allocated for all prefs. */
1346 1.1 mrg static void
1347 1.1 mrg finish_prefs (void)
1348 1.1 mrg {
1349 1.1 mrg ira_pref_t pref;
1350 1.1 mrg ira_pref_iterator pi;
1351 1.1 mrg
1352 1.1 mrg FOR_EACH_PREF (pref, pi)
1353 1.1 mrg finish_pref (pref);
1354 1.1 mrg pref_vec.release ();
1355 1.1 mrg pref_pool.release ();
1356 1.1 mrg }
1357 1.1 mrg
1358 1.1 mrg
1359 1.1 mrg
1361 1.1 mrg /* Pools for copies. */
1362 1.1 mrg static object_allocator<ira_allocno_copy> copy_pool ("copies");
1363 1.1 mrg
1364 1.1 mrg /* Vec containing references to all created copies. It is a
1365 1.1 mrg container of array ira_copies. */
1366 1.1 mrg static vec<ira_copy_t> copy_vec;
1367 1.1 mrg
1368 1.1 mrg /* The function initializes data concerning allocno copies. */
1369 1.1 mrg static void
1370 1.1 mrg initiate_copies (void)
1371 1.1 mrg {
1372 1.1 mrg copy_vec.create (get_max_uid ());
1373 1.1 mrg ira_copies = NULL;
1374 1.1 mrg ira_copies_num = 0;
1375 1.1 mrg }
1376 1.1 mrg
1377 1.1 mrg /* Return copy connecting A1 and A2 and originated from INSN of
1378 1.1 mrg LOOP_TREE_NODE if any. */
1379 1.1 mrg static ira_copy_t
1380 1.1 mrg find_allocno_copy (ira_allocno_t a1, ira_allocno_t a2, rtx_insn *insn,
1381 1.1 mrg ira_loop_tree_node_t loop_tree_node)
1382 1.1 mrg {
1383 1.1 mrg ira_copy_t cp, next_cp;
1384 1.1 mrg ira_allocno_t another_a;
1385 1.1 mrg
1386 1.1 mrg for (cp = ALLOCNO_COPIES (a1); cp != NULL; cp = next_cp)
1387 1.1 mrg {
1388 1.1 mrg if (cp->first == a1)
1389 1.1 mrg {
1390 1.1 mrg next_cp = cp->next_first_allocno_copy;
1391 1.1 mrg another_a = cp->second;
1392 1.1 mrg }
1393 1.1 mrg else if (cp->second == a1)
1394 1.1 mrg {
1395 1.1 mrg next_cp = cp->next_second_allocno_copy;
1396 1.1 mrg another_a = cp->first;
1397 1.1 mrg }
1398 1.1 mrg else
1399 1.1 mrg gcc_unreachable ();
1400 1.1 mrg if (another_a == a2 && cp->insn == insn
1401 1.1 mrg && cp->loop_tree_node == loop_tree_node)
1402 1.1 mrg return cp;
1403 1.1 mrg }
1404 1.1 mrg return NULL;
1405 1.1 mrg }
1406 1.1 mrg
1407 1.1 mrg /* Create and return copy with given attributes LOOP_TREE_NODE, FIRST,
1408 1.1 mrg SECOND, FREQ, CONSTRAINT_P, and INSN. */
1409 1.1 mrg ira_copy_t
1410 1.1 mrg ira_create_copy (ira_allocno_t first, ira_allocno_t second, int freq,
1411 1.1 mrg bool constraint_p, rtx_insn *insn,
1412 1.1 mrg ira_loop_tree_node_t loop_tree_node)
1413 1.1 mrg {
1414 1.1 mrg ira_copy_t cp;
1415 1.1 mrg
1416 1.1 mrg cp = copy_pool.allocate ();
1417 1.1 mrg cp->num = ira_copies_num;
1418 1.1 mrg cp->first = first;
1419 1.1 mrg cp->second = second;
1420 1.1 mrg cp->freq = freq;
1421 1.1 mrg cp->constraint_p = constraint_p;
1422 1.1 mrg cp->insn = insn;
1423 1.1 mrg cp->loop_tree_node = loop_tree_node;
1424 1.1 mrg copy_vec.safe_push (cp);
1425 1.1 mrg ira_copies = copy_vec.address ();
1426 1.1 mrg ira_copies_num = copy_vec.length ();
1427 1.1 mrg return cp;
1428 1.1 mrg }
1429 1.1 mrg
1430 1.1 mrg /* Attach a copy CP to allocnos involved into the copy. */
1431 1.1 mrg static void
1432 1.1 mrg add_allocno_copy_to_list (ira_copy_t cp)
1433 1.1 mrg {
1434 1.1 mrg ira_allocno_t first = cp->first, second = cp->second;
1435 1.1 mrg
1436 1.1 mrg cp->prev_first_allocno_copy = NULL;
1437 1.1 mrg cp->prev_second_allocno_copy = NULL;
1438 1.1 mrg cp->next_first_allocno_copy = ALLOCNO_COPIES (first);
1439 1.1 mrg if (cp->next_first_allocno_copy != NULL)
1440 1.1 mrg {
1441 1.1 mrg if (cp->next_first_allocno_copy->first == first)
1442 1.1 mrg cp->next_first_allocno_copy->prev_first_allocno_copy = cp;
1443 1.1 mrg else
1444 1.1 mrg cp->next_first_allocno_copy->prev_second_allocno_copy = cp;
1445 1.1 mrg }
1446 1.1 mrg cp->next_second_allocno_copy = ALLOCNO_COPIES (second);
1447 1.1 mrg if (cp->next_second_allocno_copy != NULL)
1448 1.1 mrg {
1449 1.1 mrg if (cp->next_second_allocno_copy->second == second)
1450 1.1 mrg cp->next_second_allocno_copy->prev_second_allocno_copy = cp;
1451 1.1 mrg else
1452 1.1 mrg cp->next_second_allocno_copy->prev_first_allocno_copy = cp;
1453 1.1 mrg }
1454 1.1 mrg ALLOCNO_COPIES (first) = cp;
1455 1.1 mrg ALLOCNO_COPIES (second) = cp;
1456 1.1 mrg }
1457 1.1 mrg
1458 1.1 mrg /* Make a copy CP a canonical copy where number of the
1459 1.1 mrg first allocno is less than the second one. */
1460 1.1 mrg static void
1461 1.1 mrg swap_allocno_copy_ends_if_necessary (ira_copy_t cp)
1462 1.1 mrg {
1463 1.1 mrg if (ALLOCNO_NUM (cp->first) <= ALLOCNO_NUM (cp->second))
1464 1.1 mrg return;
1465 1.1 mrg
1466 1.1 mrg std::swap (cp->first, cp->second);
1467 1.1 mrg std::swap (cp->prev_first_allocno_copy, cp->prev_second_allocno_copy);
1468 1.1 mrg std::swap (cp->next_first_allocno_copy, cp->next_second_allocno_copy);
1469 1.1 mrg }
1470 1.1 mrg
1471 1.1 mrg /* Create (or update frequency if the copy already exists) and return
1472 1.1 mrg the copy of allocnos FIRST and SECOND with frequency FREQ
1473 1.1 mrg corresponding to move insn INSN (if any) and originated from
1474 1.1 mrg LOOP_TREE_NODE. */
1475 1.1 mrg ira_copy_t
1476 1.1 mrg ira_add_allocno_copy (ira_allocno_t first, ira_allocno_t second, int freq,
1477 1.1 mrg bool constraint_p, rtx_insn *insn,
1478 1.1 mrg ira_loop_tree_node_t loop_tree_node)
1479 1.1 mrg {
1480 1.1 mrg ira_copy_t cp;
1481 1.1 mrg
1482 1.1 mrg if ((cp = find_allocno_copy (first, second, insn, loop_tree_node)) != NULL)
1483 1.1 mrg {
1484 1.1 mrg cp->freq += freq;
1485 1.1 mrg return cp;
1486 1.1 mrg }
1487 1.1 mrg cp = ira_create_copy (first, second, freq, constraint_p, insn,
1488 1.1 mrg loop_tree_node);
1489 1.1 mrg ira_assert (first != NULL && second != NULL);
1490 1.1 mrg add_allocno_copy_to_list (cp);
1491 1.1 mrg swap_allocno_copy_ends_if_necessary (cp);
1492 1.1 mrg return cp;
1493 1.1 mrg }
1494 1.1 mrg
1495 1.1 mrg /* Print info about copy CP into file F. */
1496 1.1 mrg static void
1497 1.1 mrg print_copy (FILE *f, ira_copy_t cp)
1498 1.1 mrg {
1499 1.1 mrg fprintf (f, " cp%d:a%d(r%d)<->a%d(r%d)@%d:%s\n", cp->num,
1500 1.1 mrg ALLOCNO_NUM (cp->first), ALLOCNO_REGNO (cp->first),
1501 1.1 mrg ALLOCNO_NUM (cp->second), ALLOCNO_REGNO (cp->second), cp->freq,
1502 1.1 mrg cp->insn != NULL
1503 1.1 mrg ? "move" : cp->constraint_p ? "constraint" : "shuffle");
1504 1.1 mrg }
1505 1.1 mrg
1506 1.1 mrg DEBUG_FUNCTION void
1507 1.1 mrg debug (ira_allocno_copy &ref)
1508 1.1 mrg {
1509 1.1 mrg print_copy (stderr, &ref);
1510 1.1 mrg }
1511 1.1 mrg
1512 1.1 mrg DEBUG_FUNCTION void
1513 1.1 mrg debug (ira_allocno_copy *ptr)
1514 1.1 mrg {
1515 1.1 mrg if (ptr)
1516 1.1 mrg debug (*ptr);
1517 1.1 mrg else
1518 1.1 mrg fprintf (stderr, "<nil>\n");
1519 1.1 mrg }
1520 1.1 mrg
1521 1.1 mrg /* Print info about copy CP into stderr. */
1522 1.1 mrg void
1523 1.1 mrg ira_debug_copy (ira_copy_t cp)
1524 1.1 mrg {
1525 1.1 mrg print_copy (stderr, cp);
1526 1.1 mrg }
1527 1.1 mrg
1528 1.1 mrg /* Print info about all copies into file F. */
1529 1.1 mrg static void
1530 1.1 mrg print_copies (FILE *f)
1531 1.1 mrg {
1532 1.1 mrg ira_copy_t cp;
1533 1.1 mrg ira_copy_iterator ci;
1534 1.1 mrg
1535 1.1 mrg FOR_EACH_COPY (cp, ci)
1536 1.1 mrg print_copy (f, cp);
1537 1.1 mrg }
1538 1.1 mrg
1539 1.1 mrg /* Print info about all copies into stderr. */
1540 1.1 mrg void
1541 1.1 mrg ira_debug_copies (void)
1542 1.1 mrg {
1543 1.1 mrg print_copies (stderr);
1544 1.1 mrg }
1545 1.1 mrg
1546 1.1 mrg /* Print info about copies involving allocno A into file F. */
1547 1.1 mrg static void
1548 1.1 mrg print_allocno_copies (FILE *f, ira_allocno_t a)
1549 1.1 mrg {
1550 1.1 mrg ira_allocno_t another_a;
1551 1.1 mrg ira_copy_t cp, next_cp;
1552 1.1 mrg
1553 1.1 mrg fprintf (f, " a%d(r%d):", ALLOCNO_NUM (a), ALLOCNO_REGNO (a));
1554 1.1 mrg for (cp = ALLOCNO_COPIES (a); cp != NULL; cp = next_cp)
1555 1.1 mrg {
1556 1.1 mrg if (cp->first == a)
1557 1.1 mrg {
1558 1.1 mrg next_cp = cp->next_first_allocno_copy;
1559 1.1 mrg another_a = cp->second;
1560 1.1 mrg }
1561 1.1 mrg else if (cp->second == a)
1562 1.1 mrg {
1563 1.1 mrg next_cp = cp->next_second_allocno_copy;
1564 1.1 mrg another_a = cp->first;
1565 1.1 mrg }
1566 1.1 mrg else
1567 1.1 mrg gcc_unreachable ();
1568 1.1 mrg fprintf (f, " cp%d:a%d(r%d)@%d", cp->num,
1569 1.1 mrg ALLOCNO_NUM (another_a), ALLOCNO_REGNO (another_a), cp->freq);
1570 1.1 mrg }
1571 1.1 mrg fprintf (f, "\n");
1572 1.1 mrg }
1573 1.1 mrg
1574 1.1 mrg DEBUG_FUNCTION void
1575 1.1 mrg debug (ira_allocno &ref)
1576 1.1 mrg {
1577 1.1 mrg print_allocno_copies (stderr, &ref);
1578 1.1 mrg }
1579 1.1 mrg
1580 1.1 mrg DEBUG_FUNCTION void
1581 1.1 mrg debug (ira_allocno *ptr)
1582 1.1 mrg {
1583 1.1 mrg if (ptr)
1584 1.1 mrg debug (*ptr);
1585 1.1 mrg else
1586 1.1 mrg fprintf (stderr, "<nil>\n");
1587 1.1 mrg }
1588 1.1 mrg
1589 1.1 mrg
1590 1.1 mrg /* Print info about copies involving allocno A into stderr. */
1591 1.1 mrg void
1592 1.1 mrg ira_debug_allocno_copies (ira_allocno_t a)
1593 1.1 mrg {
1594 1.1 mrg print_allocno_copies (stderr, a);
1595 1.1 mrg }
1596 1.1 mrg
1597 1.1 mrg /* The function frees memory allocated for copy CP. */
1598 1.1 mrg static void
1599 1.1 mrg finish_copy (ira_copy_t cp)
1600 1.1 mrg {
1601 1.1 mrg copy_pool.remove (cp);
1602 1.1 mrg }
1603 1.1 mrg
1604 1.1 mrg
1605 1.1 mrg /* Free memory allocated for all copies. */
1606 1.1 mrg static void
1607 1.1 mrg finish_copies (void)
1608 1.1 mrg {
1609 1.1 mrg ira_copy_t cp;
1610 1.1 mrg ira_copy_iterator ci;
1611 1.1 mrg
1612 1.1 mrg FOR_EACH_COPY (cp, ci)
1613 1.1 mrg finish_copy (cp);
1614 1.1 mrg copy_vec.release ();
1615 1.1 mrg copy_pool.release ();
1616 1.1 mrg }
1617 1.1 mrg
1618 1.1 mrg
1619 1.1 mrg
1621 1.1 mrg /* Pools for cost vectors. It is defined only for allocno classes. */
1622 1.1 mrg static pool_allocator *cost_vector_pool[N_REG_CLASSES];
1623 1.1 mrg
1624 1.1 mrg /* The function initiates work with hard register cost vectors. It
1625 1.1 mrg creates allocation pool for each allocno class. */
1626 1.1 mrg static void
1627 1.1 mrg initiate_cost_vectors (void)
1628 1.1 mrg {
1629 1.1 mrg int i;
1630 1.1 mrg enum reg_class aclass;
1631 1.1 mrg
1632 1.1 mrg for (i = 0; i < ira_allocno_classes_num; i++)
1633 1.1 mrg {
1634 1.1 mrg aclass = ira_allocno_classes[i];
1635 1.1 mrg cost_vector_pool[aclass] = new pool_allocator
1636 1.1 mrg ("cost vectors", sizeof (int) * (ira_class_hard_regs_num[aclass]));
1637 1.1 mrg }
1638 1.1 mrg }
1639 1.1 mrg
1640 1.1 mrg /* Allocate and return a cost vector VEC for ACLASS. */
1641 1.1 mrg int *
1642 1.1 mrg ira_allocate_cost_vector (reg_class_t aclass)
1643 1.1 mrg {
1644 1.1 mrg return (int*) cost_vector_pool[(int) aclass]->allocate ();
1645 1.1 mrg }
1646 1.1 mrg
1647 1.1 mrg /* Free a cost vector VEC for ACLASS. */
1648 1.1 mrg void
1649 1.1 mrg ira_free_cost_vector (int *vec, reg_class_t aclass)
1650 1.1 mrg {
1651 1.1 mrg ira_assert (vec != NULL);
1652 1.1 mrg cost_vector_pool[(int) aclass]->remove (vec);
1653 1.1 mrg }
1654 1.1 mrg
1655 1.1 mrg /* Finish work with hard register cost vectors. Release allocation
1656 1.1 mrg pool for each allocno class. */
1657 1.1 mrg static void
1658 1.1 mrg finish_cost_vectors (void)
1659 1.1 mrg {
1660 1.1 mrg int i;
1661 1.1 mrg enum reg_class aclass;
1662 1.1 mrg
1663 1.1 mrg for (i = 0; i < ira_allocno_classes_num; i++)
1664 1.1 mrg {
1665 1.1 mrg aclass = ira_allocno_classes[i];
1666 1.1 mrg delete cost_vector_pool[aclass];
1667 1.1 mrg }
1668 1.1 mrg }
1669 1.1 mrg
1670 1.1 mrg
1671 1.1 mrg
1673 1.1 mrg /* Compute a post-ordering of the reverse control flow of the loop body
1674 1.1 mrg designated by the children nodes of LOOP_NODE, whose body nodes in
1675 1.1 mrg pre-order are input as LOOP_PREORDER. Return a VEC with a post-order
1676 1.1 mrg of the reverse loop body.
1677 1.1 mrg
1678 1.1 mrg For the post-order of the reverse CFG, we visit the basic blocks in
1679 1.1 mrg LOOP_PREORDER array in the reverse order of where they appear.
1680 1.1 mrg This is important: We do not just want to compute a post-order of
1681 1.1 mrg the reverse CFG, we want to make a best-guess for a visiting order that
1682 1.1 mrg minimizes the number of chain elements per allocno live range. If the
1683 1.1 mrg blocks would be visited in a different order, we would still compute a
1684 1.1 mrg correct post-ordering but it would be less likely that two nodes
1685 1.1 mrg connected by an edge in the CFG are neighbors in the topsort. */
1686 1.1 mrg
1687 1.1 mrg static vec<ira_loop_tree_node_t>
1688 1.1 mrg ira_loop_tree_body_rev_postorder (ira_loop_tree_node_t loop_node ATTRIBUTE_UNUSED,
1689 1.1 mrg const vec<ira_loop_tree_node_t> &loop_preorder)
1690 1.1 mrg {
1691 1.1 mrg vec<ira_loop_tree_node_t> topsort_nodes = vNULL;
1692 1.1 mrg unsigned int n_loop_preorder;
1693 1.1 mrg
1694 1.1 mrg n_loop_preorder = loop_preorder.length ();
1695 1.1 mrg if (n_loop_preorder != 0)
1696 1.1 mrg {
1697 1.1 mrg ira_loop_tree_node_t subloop_node;
1698 1.1 mrg unsigned int i;
1699 1.1 mrg auto_vec<ira_loop_tree_node_t> dfs_stack;
1700 1.1 mrg
1701 1.1 mrg /* This is a bit of strange abuse of the BB_VISITED flag: We use
1702 1.1 mrg the flag to mark blocks we still have to visit to add them to
1703 1.1 mrg our post-order. Define an alias to avoid confusion. */
1704 1.1 mrg #define BB_TO_VISIT BB_VISITED
1705 1.1 mrg
1706 1.1 mrg FOR_EACH_VEC_ELT (loop_preorder, i, subloop_node)
1707 1.1 mrg {
1708 1.1 mrg gcc_checking_assert (! (subloop_node->bb->flags & BB_TO_VISIT));
1709 1.1 mrg subloop_node->bb->flags |= BB_TO_VISIT;
1710 1.1 mrg }
1711 1.1 mrg
1712 1.1 mrg topsort_nodes.create (n_loop_preorder);
1713 1.1 mrg dfs_stack.create (n_loop_preorder);
1714 1.1 mrg
1715 1.1 mrg FOR_EACH_VEC_ELT_REVERSE (loop_preorder, i, subloop_node)
1716 1.1 mrg {
1717 1.1 mrg if (! (subloop_node->bb->flags & BB_TO_VISIT))
1718 1.1 mrg continue;
1719 1.1 mrg
1720 1.1 mrg subloop_node->bb->flags &= ~BB_TO_VISIT;
1721 1.1 mrg dfs_stack.quick_push (subloop_node);
1722 1.1 mrg while (! dfs_stack.is_empty ())
1723 1.1 mrg {
1724 1.1 mrg edge e;
1725 1.1 mrg edge_iterator ei;
1726 1.1 mrg
1727 1.1 mrg ira_loop_tree_node_t n = dfs_stack.last ();
1728 1.1 mrg FOR_EACH_EDGE (e, ei, n->bb->preds)
1729 1.1 mrg {
1730 1.1 mrg ira_loop_tree_node_t pred_node;
1731 1.1 mrg basic_block pred_bb = e->src;
1732 1.1 mrg
1733 1.1 mrg if (e->src == ENTRY_BLOCK_PTR_FOR_FN (cfun))
1734 1.1 mrg continue;
1735 1.1 mrg
1736 1.1 mrg pred_node = IRA_BB_NODE_BY_INDEX (pred_bb->index);
1737 1.1 mrg if (pred_node != n
1738 1.1 mrg && (pred_node->bb->flags & BB_TO_VISIT))
1739 1.1 mrg {
1740 1.1 mrg pred_node->bb->flags &= ~BB_TO_VISIT;
1741 1.1 mrg dfs_stack.quick_push (pred_node);
1742 1.1 mrg }
1743 1.1 mrg }
1744 1.1 mrg if (n == dfs_stack.last ())
1745 1.1 mrg {
1746 1.1 mrg dfs_stack.pop ();
1747 1.1 mrg topsort_nodes.quick_push (n);
1748 1.1 mrg }
1749 1.1 mrg }
1750 1.1 mrg }
1751 1.1 mrg
1752 1.1 mrg #undef BB_TO_VISIT
1753 1.1 mrg }
1754 1.1 mrg
1755 1.1 mrg gcc_assert (topsort_nodes.length () == n_loop_preorder);
1756 1.1 mrg return topsort_nodes;
1757 1.1 mrg }
1758 1.1 mrg
1759 1.1 mrg /* The current loop tree node and its regno allocno map. */
1760 1.1 mrg ira_loop_tree_node_t ira_curr_loop_tree_node;
1761 1.1 mrg ira_allocno_t *ira_curr_regno_allocno_map;
1762 1.1 mrg
1763 1.1 mrg /* This recursive function traverses loop tree with root LOOP_NODE
1764 1.1 mrg calling non-null functions PREORDER_FUNC and POSTORDER_FUNC
1765 1.1 mrg correspondingly in preorder and postorder. The function sets up
1766 1.1 mrg IRA_CURR_LOOP_TREE_NODE and IRA_CURR_REGNO_ALLOCNO_MAP. If BB_P,
1767 1.1 mrg basic block nodes of LOOP_NODE is also processed (before its
1768 1.1 mrg subloop nodes).
1769 1.1 mrg
1770 1.1 mrg If BB_P is set and POSTORDER_FUNC is given, the basic blocks in
1771 1.1 mrg the loop are passed in the *reverse* post-order of the *reverse*
1772 1.1 mrg CFG. This is only used by ira_create_allocno_live_ranges, which
1773 1.1 mrg wants to visit basic blocks in this order to minimize the number
1774 1.1 mrg of elements per live range chain.
1775 1.1 mrg Note that the loop tree nodes are still visited in the normal,
1776 1.1 mrg forward post-order of the loop tree. */
1777 1.1 mrg
1778 1.1 mrg void
1779 1.1 mrg ira_traverse_loop_tree (bool bb_p, ira_loop_tree_node_t loop_node,
1780 1.1 mrg void (*preorder_func) (ira_loop_tree_node_t),
1781 1.1 mrg void (*postorder_func) (ira_loop_tree_node_t))
1782 1.1 mrg {
1783 1.1 mrg ira_loop_tree_node_t subloop_node;
1784 1.1 mrg
1785 1.1 mrg ira_assert (loop_node->bb == NULL);
1786 1.1 mrg ira_curr_loop_tree_node = loop_node;
1787 1.1 mrg ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
1788 1.1 mrg
1789 1.1 mrg if (preorder_func != NULL)
1790 1.1 mrg (*preorder_func) (loop_node);
1791 1.1 mrg
1792 1.1 mrg if (bb_p)
1793 1.1 mrg {
1794 1.1 mrg auto_vec<ira_loop_tree_node_t> loop_preorder;
1795 1.1 mrg unsigned int i;
1796 1.1 mrg
1797 1.1 mrg /* Add all nodes to the set of nodes to visit. The IRA loop tree
1798 1.1 mrg is set up such that nodes in the loop body appear in a pre-order
1799 1.1 mrg of their place in the CFG. */
1800 1.1 mrg for (subloop_node = loop_node->children;
1801 1.1 mrg subloop_node != NULL;
1802 1.1 mrg subloop_node = subloop_node->next)
1803 1.1 mrg if (subloop_node->bb != NULL)
1804 1.1 mrg loop_preorder.safe_push (subloop_node);
1805 1.1 mrg
1806 1.1 mrg if (preorder_func != NULL)
1807 1.1 mrg FOR_EACH_VEC_ELT (loop_preorder, i, subloop_node)
1808 1.1 mrg (*preorder_func) (subloop_node);
1809 1.1 mrg
1810 1.1 mrg if (postorder_func != NULL)
1811 1.1 mrg {
1812 1.1 mrg vec<ira_loop_tree_node_t> loop_rev_postorder =
1813 1.1 mrg ira_loop_tree_body_rev_postorder (loop_node, loop_preorder);
1814 1.1 mrg FOR_EACH_VEC_ELT_REVERSE (loop_rev_postorder, i, subloop_node)
1815 1.1 mrg (*postorder_func) (subloop_node);
1816 1.1 mrg loop_rev_postorder.release ();
1817 1.1 mrg }
1818 1.1 mrg }
1819 1.1 mrg
1820 1.1 mrg for (subloop_node = loop_node->subloops;
1821 1.1 mrg subloop_node != NULL;
1822 1.1 mrg subloop_node = subloop_node->subloop_next)
1823 1.1 mrg {
1824 1.1 mrg ira_assert (subloop_node->bb == NULL);
1825 1.1 mrg ira_traverse_loop_tree (bb_p, subloop_node,
1826 1.1 mrg preorder_func, postorder_func);
1827 1.1 mrg }
1828 1.1 mrg
1829 1.1 mrg ira_curr_loop_tree_node = loop_node;
1830 1.1 mrg ira_curr_regno_allocno_map = ira_curr_loop_tree_node->regno_allocno_map;
1831 1.1 mrg
1832 1.1 mrg if (postorder_func != NULL)
1833 1.1 mrg (*postorder_func) (loop_node);
1834 1.1 mrg }
1835 1.1 mrg
1836 1.1 mrg
1837 1.1 mrg
1839 1.1 mrg /* The basic block currently being processed. */
1840 1.1 mrg static basic_block curr_bb;
1841 1.1 mrg
1842 1.1 mrg /* This recursive function creates allocnos corresponding to
1843 1.1 mrg pseudo-registers containing in X. True OUTPUT_P means that X is
1844 1.1 mrg an lvalue. PARENT corresponds to the parent expression of X. */
1845 1.1 mrg static void
1846 1.1 mrg create_insn_allocnos (rtx x, rtx outer, bool output_p)
1847 1.1 mrg {
1848 1.1 mrg int i, j;
1849 1.1 mrg const char *fmt;
1850 1.1 mrg enum rtx_code code = GET_CODE (x);
1851 1.1 mrg
1852 1.1 mrg if (code == REG)
1853 1.1 mrg {
1854 1.1 mrg int regno;
1855 1.1 mrg
1856 1.1 mrg if ((regno = REGNO (x)) >= FIRST_PSEUDO_REGISTER)
1857 1.1 mrg {
1858 1.1 mrg ira_allocno_t a;
1859 1.1 mrg
1860 1.1 mrg if ((a = ira_curr_regno_allocno_map[regno]) == NULL)
1861 1.1 mrg {
1862 1.1 mrg a = ira_create_allocno (regno, false, ira_curr_loop_tree_node);
1863 1.1 mrg if (outer != NULL && GET_CODE (outer) == SUBREG)
1864 1.1 mrg {
1865 1.1 mrg machine_mode wmode = GET_MODE (outer);
1866 1.1 mrg if (partial_subreg_p (ALLOCNO_WMODE (a), wmode))
1867 1.1 mrg ALLOCNO_WMODE (a) = wmode;
1868 1.1 mrg }
1869 1.1 mrg }
1870 1.1 mrg
1871 1.1 mrg ALLOCNO_NREFS (a)++;
1872 1.1 mrg ALLOCNO_FREQ (a) += REG_FREQ_FROM_BB (curr_bb);
1873 1.1 mrg if (output_p)
1874 1.1 mrg bitmap_set_bit (ira_curr_loop_tree_node->modified_regnos, regno);
1875 1.1 mrg }
1876 1.1 mrg return;
1877 1.1 mrg }
1878 1.1 mrg else if (code == SET)
1879 1.1 mrg {
1880 1.1 mrg create_insn_allocnos (SET_DEST (x), NULL, true);
1881 1.1 mrg create_insn_allocnos (SET_SRC (x), NULL, false);
1882 1.1 mrg return;
1883 1.1 mrg }
1884 1.1 mrg else if (code == CLOBBER)
1885 1.1 mrg {
1886 1.1 mrg create_insn_allocnos (XEXP (x, 0), NULL, true);
1887 1.1 mrg return;
1888 1.1 mrg }
1889 1.1 mrg else if (code == MEM)
1890 1.1 mrg {
1891 1.1 mrg create_insn_allocnos (XEXP (x, 0), NULL, false);
1892 1.1 mrg return;
1893 1.1 mrg }
1894 1.1 mrg else if (code == PRE_DEC || code == POST_DEC || code == PRE_INC ||
1895 1.1 mrg code == POST_INC || code == POST_MODIFY || code == PRE_MODIFY)
1896 1.1 mrg {
1897 1.1 mrg create_insn_allocnos (XEXP (x, 0), NULL, true);
1898 1.1 mrg create_insn_allocnos (XEXP (x, 0), NULL, false);
1899 1.1 mrg return;
1900 1.1 mrg }
1901 1.1 mrg
1902 1.1 mrg fmt = GET_RTX_FORMAT (code);
1903 1.1 mrg for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1904 1.1 mrg {
1905 1.1 mrg if (fmt[i] == 'e')
1906 1.1 mrg create_insn_allocnos (XEXP (x, i), x, output_p);
1907 1.1 mrg else if (fmt[i] == 'E')
1908 1.1 mrg for (j = 0; j < XVECLEN (x, i); j++)
1909 1.1 mrg create_insn_allocnos (XVECEXP (x, i, j), x, output_p);
1910 1.1 mrg }
1911 1.1 mrg }
1912 1.1 mrg
1913 1.1 mrg /* Create allocnos corresponding to pseudo-registers living in the
1914 1.1 mrg basic block represented by the corresponding loop tree node
1915 1.1 mrg BB_NODE. */
1916 1.1 mrg static void
1917 1.1 mrg create_bb_allocnos (ira_loop_tree_node_t bb_node)
1918 1.1 mrg {
1919 1.1 mrg basic_block bb;
1920 1.1 mrg rtx_insn *insn;
1921 1.1 mrg unsigned int i;
1922 1.1 mrg bitmap_iterator bi;
1923 1.1 mrg
1924 1.1 mrg curr_bb = bb = bb_node->bb;
1925 1.1 mrg ira_assert (bb != NULL);
1926 1.1 mrg FOR_BB_INSNS_REVERSE (bb, insn)
1927 1.1 mrg if (NONDEBUG_INSN_P (insn))
1928 1.1 mrg create_insn_allocnos (PATTERN (insn), NULL, false);
1929 1.1 mrg /* It might be a allocno living through from one subloop to
1930 1.1 mrg another. */
1931 1.1 mrg EXECUTE_IF_SET_IN_REG_SET (df_get_live_in (bb), FIRST_PSEUDO_REGISTER, i, bi)
1932 1.1 mrg if (ira_curr_regno_allocno_map[i] == NULL)
1933 1.1 mrg ira_create_allocno (i, false, ira_curr_loop_tree_node);
1934 1.1 mrg }
1935 1.1 mrg
1936 1.1 mrg /* Create allocnos corresponding to pseudo-registers living on edge E
1937 1.1 mrg (a loop entry or exit). Also mark the allocnos as living on the
1938 1.1 mrg loop border. */
1939 1.1 mrg static void
1940 1.1 mrg create_loop_allocnos (edge e)
1941 1.1 mrg {
1942 1.1 mrg unsigned int i;
1943 1.1 mrg bitmap live_in_regs, border_allocnos;
1944 1.1 mrg bitmap_iterator bi;
1945 1.1 mrg ira_loop_tree_node_t parent;
1946 1.1 mrg
1947 1.1 mrg live_in_regs = df_get_live_in (e->dest);
1948 1.1 mrg border_allocnos = ira_curr_loop_tree_node->border_allocnos;
1949 1.1 mrg EXECUTE_IF_SET_IN_REG_SET (df_get_live_out (e->src),
1950 1.1 mrg FIRST_PSEUDO_REGISTER, i, bi)
1951 1.1 mrg if (bitmap_bit_p (live_in_regs, i))
1952 1.1 mrg {
1953 1.1 mrg if (ira_curr_regno_allocno_map[i] == NULL)
1954 1.1 mrg {
1955 1.1 mrg /* The order of creations is important for right
1956 1.1 mrg ira_regno_allocno_map. */
1957 1.1 mrg if ((parent = ira_curr_loop_tree_node->parent) != NULL
1958 1.1 mrg && parent->regno_allocno_map[i] == NULL)
1959 1.1 mrg ira_create_allocno (i, false, parent);
1960 1.1 mrg ira_create_allocno (i, false, ira_curr_loop_tree_node);
1961 1.1 mrg }
1962 1.1 mrg bitmap_set_bit (border_allocnos,
1963 1.1 mrg ALLOCNO_NUM (ira_curr_regno_allocno_map[i]));
1964 1.1 mrg }
1965 1.1 mrg }
1966 1.1 mrg
1967 1.1 mrg /* Create allocnos corresponding to pseudo-registers living in loop
1968 1.1 mrg represented by the corresponding loop tree node LOOP_NODE. This
1969 1.1 mrg function is called by ira_traverse_loop_tree. */
1970 1.1 mrg static void
1971 1.1 mrg create_loop_tree_node_allocnos (ira_loop_tree_node_t loop_node)
1972 1.1 mrg {
1973 1.1 mrg if (loop_node->bb != NULL)
1974 1.1 mrg create_bb_allocnos (loop_node);
1975 1.1 mrg else if (loop_node != ira_loop_tree_root)
1976 1.1 mrg {
1977 1.1 mrg int i;
1978 1.1 mrg edge_iterator ei;
1979 1.1 mrg edge e;
1980 1.1 mrg
1981 1.1 mrg ira_assert (current_loops != NULL);
1982 1.1 mrg FOR_EACH_EDGE (e, ei, loop_node->loop->header->preds)
1983 1.1 mrg if (e->src != loop_node->loop->latch)
1984 1.1 mrg create_loop_allocnos (e);
1985 1.1 mrg
1986 1.1 mrg auto_vec<edge> edges = get_loop_exit_edges (loop_node->loop);
1987 1.1 mrg FOR_EACH_VEC_ELT (edges, i, e)
1988 1.1 mrg create_loop_allocnos (e);
1989 1.1 mrg }
1990 1.1 mrg }
1991 1.1 mrg
1992 1.1 mrg /* Propagate information about allocnos modified inside the loop given
1993 1.1 mrg by its LOOP_TREE_NODE to its parent. */
1994 1.1 mrg static void
1995 1.1 mrg propagate_modified_regnos (ira_loop_tree_node_t loop_tree_node)
1996 1.1 mrg {
1997 1.1 mrg if (loop_tree_node == ira_loop_tree_root)
1998 1.1 mrg return;
1999 1.1 mrg ira_assert (loop_tree_node->bb == NULL);
2000 1.1 mrg bitmap_ior_into (loop_tree_node->parent->modified_regnos,
2001 1.1 mrg loop_tree_node->modified_regnos);
2002 1.1 mrg }
2003 1.1 mrg
2004 1.1 mrg /* Propagate ALLOCNO_HARD_REG_COSTS from A to PARENT_A. Use SPILL_COST
2005 1.1 mrg as the cost of spilling a register throughout A (which we have to do
2006 1.1 mrg for PARENT_A allocations that conflict with A). */
2007 1.1 mrg static void
2008 1.1 mrg ira_propagate_hard_reg_costs (ira_allocno_t parent_a, ira_allocno_t a,
2009 1.1 mrg int spill_cost)
2010 1.1 mrg {
2011 1.1 mrg HARD_REG_SET conflicts = ira_total_conflict_hard_regs (a);
2012 1.1 mrg if (ira_caller_save_loop_spill_p (parent_a, a, spill_cost))
2013 1.1 mrg conflicts |= ira_need_caller_save_regs (a);
2014 1.1 mrg conflicts &= ~ira_total_conflict_hard_regs (parent_a);
2015 1.1 mrg
2016 1.1 mrg auto costs = ALLOCNO_HARD_REG_COSTS (a);
2017 1.1 mrg if (!hard_reg_set_empty_p (conflicts))
2018 1.1 mrg ALLOCNO_MIGHT_CONFLICT_WITH_PARENT_P (a) = true;
2019 1.1 mrg else if (!costs)
2020 1.1 mrg return;
2021 1.1 mrg
2022 1.1 mrg auto aclass = ALLOCNO_CLASS (a);
2023 1.1 mrg ira_allocate_and_set_costs (&ALLOCNO_HARD_REG_COSTS (parent_a),
2024 1.1 mrg aclass, ALLOCNO_CLASS_COST (parent_a));
2025 1.1 mrg auto parent_costs = ALLOCNO_HARD_REG_COSTS (parent_a);
2026 1.1 mrg for (int i = 0; i < ira_class_hard_regs_num[aclass]; ++i)
2027 1.1 mrg if (TEST_HARD_REG_BIT (conflicts, ira_class_hard_regs[aclass][i]))
2028 1.1 mrg parent_costs[i] += spill_cost;
2029 1.1 mrg else if (costs)
2030 1.1 mrg /* The cost to A of allocating this register to PARENT_A can't
2031 1.1 mrg be more than the cost of spilling the register throughout A. */
2032 1.1 mrg parent_costs[i] += MIN (costs[i], spill_cost);
2033 1.1 mrg }
2034 1.1 mrg
2035 1.1 mrg /* Propagate new info about allocno A (see comments about accumulated
2036 1.1 mrg info in allocno definition) to the corresponding allocno on upper
2037 1.1 mrg loop tree level. So allocnos on upper levels accumulate
2038 1.1 mrg information about the corresponding allocnos in nested regions.
2039 1.1 mrg The new info means allocno info finally calculated in this
2040 1.1 mrg file. */
2041 1.1 mrg static void
2042 1.1 mrg propagate_allocno_info (void)
2043 1.1 mrg {
2044 1.1 mrg int i;
2045 1.1 mrg ira_allocno_t a, parent_a;
2046 1.1 mrg ira_loop_tree_node_t parent;
2047 1.1 mrg enum reg_class aclass;
2048 1.1 mrg
2049 1.1 mrg if (flag_ira_region != IRA_REGION_ALL
2050 1.1 mrg && flag_ira_region != IRA_REGION_MIXED)
2051 1.1 mrg return;
2052 1.1 mrg for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
2053 1.1 mrg for (a = ira_regno_allocno_map[i];
2054 1.1 mrg a != NULL;
2055 1.1 mrg a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2056 1.1 mrg if ((parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) != NULL
2057 1.1 mrg && (parent_a = parent->regno_allocno_map[i]) != NULL
2058 1.1 mrg /* There are no caps yet at this point. So use
2059 1.1 mrg border_allocnos to find allocnos for the propagation. */
2060 1.1 mrg && bitmap_bit_p (ALLOCNO_LOOP_TREE_NODE (a)->border_allocnos,
2061 1.1 mrg ALLOCNO_NUM (a)))
2062 1.1 mrg {
2063 1.1 mrg /* Calculate the cost of storing to memory on entry to A's loop,
2064 1.1 mrg referencing as memory within A's loop, and restoring from
2065 1.1 mrg memory on exit from A's loop. */
2066 1.1 mrg ira_loop_border_costs border_costs (a);
2067 1.1 mrg int spill_cost = INT_MAX;
2068 1.1 mrg if (ira_subloop_allocnos_can_differ_p (parent_a))
2069 1.1 mrg spill_cost = (border_costs.spill_inside_loop_cost ()
2070 1.1 mrg + ALLOCNO_MEMORY_COST (a));
2071 1.1 mrg
2072 1.1 mrg if (! ALLOCNO_BAD_SPILL_P (a))
2073 1.1 mrg ALLOCNO_BAD_SPILL_P (parent_a) = false;
2074 1.1 mrg ALLOCNO_NREFS (parent_a) += ALLOCNO_NREFS (a);
2075 1.1 mrg ALLOCNO_FREQ (parent_a) += ALLOCNO_FREQ (a);
2076 1.1 mrg
2077 1.1 mrg /* If A's allocation can differ from PARENT_A's, we can if necessary
2078 1.1 mrg spill PARENT_A on entry to A's loop and restore it afterwards.
2079 1.1 mrg Doing that has cost SPILL_COST. */
2080 1.1 mrg if (!ira_subloop_allocnos_can_differ_p (parent_a))
2081 1.1 mrg merge_hard_reg_conflicts (a, parent_a, true);
2082 1.1 mrg
2083 1.1 mrg if (!ira_caller_save_loop_spill_p (parent_a, a, spill_cost))
2084 1.1 mrg {
2085 1.1 mrg ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
2086 1.1 mrg ALLOCNO_CALLS_CROSSED_NUM (parent_a)
2087 1.1 mrg += ALLOCNO_CALLS_CROSSED_NUM (a);
2088 1.1 mrg ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
2089 1.1 mrg += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
2090 1.1 mrg ALLOCNO_CROSSED_CALLS_ABIS (parent_a)
2091 1.1 mrg |= ALLOCNO_CROSSED_CALLS_ABIS (a);
2092 1.1 mrg ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (parent_a)
2093 1.1 mrg |= ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a);
2094 1.1 mrg }
2095 1.1 mrg ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
2096 1.1 mrg += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
2097 1.1 mrg aclass = ALLOCNO_CLASS (a);
2098 1.1 mrg ira_assert (aclass == ALLOCNO_CLASS (parent_a));
2099 1.1 mrg ira_propagate_hard_reg_costs (parent_a, a, spill_cost);
2100 1.1 mrg ira_allocate_and_accumulate_costs
2101 1.1 mrg (&ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a),
2102 1.1 mrg aclass,
2103 1.1 mrg ALLOCNO_CONFLICT_HARD_REG_COSTS (a));
2104 1.1 mrg /* The cost to A of allocating a register to PARENT_A can't be
2105 1.1 mrg more than the cost of spilling the register throughout A. */
2106 1.1 mrg ALLOCNO_CLASS_COST (parent_a)
2107 1.1 mrg += MIN (ALLOCNO_CLASS_COST (a), spill_cost);
2108 1.1 mrg ALLOCNO_MEMORY_COST (parent_a) += ALLOCNO_MEMORY_COST (a);
2109 1.1 mrg }
2110 1.1 mrg }
2111 1.1 mrg
2112 1.1 mrg /* Create allocnos corresponding to pseudo-registers in the current
2113 1.1 mrg function. Traverse the loop tree for this. */
2114 1.1 mrg static void
2115 1.1 mrg create_allocnos (void)
2116 1.1 mrg {
2117 1.1 mrg /* We need to process BB first to correctly link allocnos by member
2118 1.1 mrg next_regno_allocno. */
2119 1.1 mrg ira_traverse_loop_tree (true, ira_loop_tree_root,
2120 1.1 mrg create_loop_tree_node_allocnos, NULL);
2121 1.1 mrg if (optimize)
2122 1.1 mrg ira_traverse_loop_tree (false, ira_loop_tree_root, NULL,
2123 1.1 mrg propagate_modified_regnos);
2124 1.1 mrg }
2125 1.1 mrg
2126 1.1 mrg
2127 1.1 mrg
2129 1.1 mrg /* The page contains function to remove some regions from a separate
2130 1.1 mrg register allocation. We remove regions whose separate allocation
2131 1.1 mrg will hardly improve the result. As a result we speed up regional
2132 1.1 mrg register allocation. */
2133 1.1 mrg
2134 1.1 mrg /* The function changes the object in range list given by R to OBJ. */
2135 1.1 mrg static void
2136 1.1 mrg change_object_in_range_list (live_range_t r, ira_object_t obj)
2137 1.1 mrg {
2138 1.1 mrg for (; r != NULL; r = r->next)
2139 1.1 mrg r->object = obj;
2140 1.1 mrg }
2141 1.1 mrg
2142 1.1 mrg /* Move all live ranges associated with allocno FROM to allocno TO. */
2143 1.1 mrg static void
2144 1.1 mrg move_allocno_live_ranges (ira_allocno_t from, ira_allocno_t to)
2145 1.1 mrg {
2146 1.1 mrg int i;
2147 1.1 mrg int n = ALLOCNO_NUM_OBJECTS (from);
2148 1.1 mrg
2149 1.1 mrg gcc_assert (n == ALLOCNO_NUM_OBJECTS (to));
2150 1.1 mrg
2151 1.1 mrg for (i = 0; i < n; i++)
2152 1.1 mrg {
2153 1.1 mrg ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
2154 1.1 mrg ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
2155 1.1 mrg live_range_t lr = OBJECT_LIVE_RANGES (from_obj);
2156 1.1 mrg
2157 1.1 mrg if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2158 1.1 mrg {
2159 1.1 mrg fprintf (ira_dump_file,
2160 1.1 mrg " Moving ranges of a%dr%d to a%dr%d: ",
2161 1.1 mrg ALLOCNO_NUM (from), ALLOCNO_REGNO (from),
2162 1.1 mrg ALLOCNO_NUM (to), ALLOCNO_REGNO (to));
2163 1.1 mrg ira_print_live_range_list (ira_dump_file, lr);
2164 1.1 mrg }
2165 1.1 mrg change_object_in_range_list (lr, to_obj);
2166 1.1 mrg OBJECT_LIVE_RANGES (to_obj)
2167 1.1 mrg = ira_merge_live_ranges (lr, OBJECT_LIVE_RANGES (to_obj));
2168 1.1 mrg OBJECT_LIVE_RANGES (from_obj) = NULL;
2169 1.1 mrg }
2170 1.1 mrg }
2171 1.1 mrg
2172 1.1 mrg static void
2173 1.1 mrg copy_allocno_live_ranges (ira_allocno_t from, ira_allocno_t to)
2174 1.1 mrg {
2175 1.1 mrg int i;
2176 1.1 mrg int n = ALLOCNO_NUM_OBJECTS (from);
2177 1.1 mrg
2178 1.1 mrg gcc_assert (n == ALLOCNO_NUM_OBJECTS (to));
2179 1.1 mrg
2180 1.1 mrg for (i = 0; i < n; i++)
2181 1.1 mrg {
2182 1.1 mrg ira_object_t from_obj = ALLOCNO_OBJECT (from, i);
2183 1.1 mrg ira_object_t to_obj = ALLOCNO_OBJECT (to, i);
2184 1.1 mrg live_range_t lr = OBJECT_LIVE_RANGES (from_obj);
2185 1.1 mrg
2186 1.1 mrg if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
2187 1.1 mrg {
2188 1.1 mrg fprintf (ira_dump_file, " Copying ranges of a%dr%d to a%dr%d: ",
2189 1.1 mrg ALLOCNO_NUM (from), ALLOCNO_REGNO (from),
2190 1.1 mrg ALLOCNO_NUM (to), ALLOCNO_REGNO (to));
2191 1.1 mrg ira_print_live_range_list (ira_dump_file, lr);
2192 1.1 mrg }
2193 1.1 mrg lr = ira_copy_live_range_list (lr);
2194 1.1 mrg change_object_in_range_list (lr, to_obj);
2195 1.1 mrg OBJECT_LIVE_RANGES (to_obj)
2196 1.1 mrg = ira_merge_live_ranges (lr, OBJECT_LIVE_RANGES (to_obj));
2197 1.1 mrg }
2198 1.1 mrg }
2199 1.1 mrg
2200 1.1 mrg /* Return TRUE if NODE represents a loop with low register
2201 1.1 mrg pressure. */
2202 1.1 mrg static bool
2203 1.1 mrg low_pressure_loop_node_p (ira_loop_tree_node_t node)
2204 1.1 mrg {
2205 1.1 mrg int i;
2206 1.1 mrg enum reg_class pclass;
2207 1.1 mrg
2208 1.1 mrg if (node->bb != NULL)
2209 1.1 mrg return false;
2210 1.1 mrg
2211 1.1 mrg for (i = 0; i < ira_pressure_classes_num; i++)
2212 1.1 mrg {
2213 1.1 mrg pclass = ira_pressure_classes[i];
2214 1.1 mrg if (node->reg_pressure[pclass] > ira_class_hard_regs_num[pclass]
2215 1.1 mrg && ira_class_hard_regs_num[pclass] > 1)
2216 1.1 mrg return false;
2217 1.1 mrg }
2218 1.1 mrg return true;
2219 1.1 mrg }
2220 1.1 mrg
2221 1.1 mrg #ifdef STACK_REGS
2222 1.1 mrg /* Return TRUE if LOOP has a complex enter or exit edge. We don't
2223 1.1 mrg form a region from such loop if the target use stack register
2224 1.1 mrg because reg-stack.cc cannot deal with such edges. */
2225 1.1 mrg static bool
2226 1.1 mrg loop_with_complex_edge_p (class loop *loop)
2227 1.1 mrg {
2228 1.1 mrg int i;
2229 1.1 mrg edge_iterator ei;
2230 1.1 mrg edge e;
2231 1.1 mrg bool res;
2232 1.1 mrg
2233 1.1 mrg FOR_EACH_EDGE (e, ei, loop->header->preds)
2234 1.1 mrg if (e->flags & EDGE_EH)
2235 1.1 mrg return true;
2236 1.1 mrg auto_vec<edge> edges = get_loop_exit_edges (loop);
2237 1.1 mrg res = false;
2238 1.1 mrg FOR_EACH_VEC_ELT (edges, i, e)
2239 1.1 mrg if (e->flags & EDGE_COMPLEX)
2240 1.1 mrg {
2241 1.1 mrg res = true;
2242 1.1 mrg break;
2243 1.1 mrg }
2244 1.1 mrg return res;
2245 1.1 mrg }
2246 1.1 mrg #endif
2247 1.1 mrg
2248 1.1 mrg /* Sort loops for marking them for removal. We put already marked
2249 1.1 mrg loops first, then less frequent loops next, and then outer loops
2250 1.1 mrg next. */
2251 1.1 mrg static int
2252 1.1 mrg loop_compare_func (const void *v1p, const void *v2p)
2253 1.1 mrg {
2254 1.1 mrg int diff;
2255 1.1 mrg ira_loop_tree_node_t l1 = *(const ira_loop_tree_node_t *) v1p;
2256 1.1 mrg ira_loop_tree_node_t l2 = *(const ira_loop_tree_node_t *) v2p;
2257 1.1 mrg
2258 1.1 mrg ira_assert (l1->parent != NULL && l2->parent != NULL);
2259 1.1 mrg if (l1->to_remove_p && ! l2->to_remove_p)
2260 1.1 mrg return -1;
2261 1.1 mrg if (! l1->to_remove_p && l2->to_remove_p)
2262 1.1 mrg return 1;
2263 1.1 mrg if ((diff = l1->loop->header->count.to_frequency (cfun)
2264 1.1 mrg - l2->loop->header->count.to_frequency (cfun)) != 0)
2265 1.1 mrg return diff;
2266 1.1 mrg if ((diff = (int) loop_depth (l1->loop) - (int) loop_depth (l2->loop)) != 0)
2267 1.1 mrg return diff;
2268 1.1 mrg /* Make sorting stable. */
2269 1.1 mrg return l1->loop_num - l2->loop_num;
2270 1.1 mrg }
2271 1.1 mrg
2272 1.1 mrg /* Mark loops which should be removed from regional allocation. We
2273 1.1 mrg remove a loop with low register pressure inside another loop with
2274 1.1 mrg register pressure. In this case a separate allocation of the loop
2275 1.1 mrg hardly helps (for irregular register file architecture it could
2276 1.1 mrg help by choosing a better hard register in the loop but we prefer
2277 1.1 mrg faster allocation even in this case). We also remove cheap loops
2278 1.1 mrg if there are more than param_ira_max_loops_num of them. Loop with EH
2279 1.1 mrg exit or enter edges are removed too because the allocation might
2280 1.1 mrg require put pseudo moves on the EH edges (we could still do this
2281 1.1 mrg for pseudos with caller saved hard registers in some cases but it
2282 1.1 mrg is impossible to say here or during top-down allocation pass what
2283 1.1 mrg hard register the pseudos get finally). */
2284 1.1 mrg static void
2285 1.1 mrg mark_loops_for_removal (void)
2286 1.1 mrg {
2287 1.1 mrg int i, n;
2288 1.1 mrg ira_loop_tree_node_t *sorted_loops;
2289 1.1 mrg loop_p loop;
2290 1.1 mrg
2291 1.1 mrg ira_assert (current_loops != NULL);
2292 1.1 mrg sorted_loops
2293 1.1 mrg = (ira_loop_tree_node_t *) ira_allocate (sizeof (ira_loop_tree_node_t)
2294 1.1 mrg * number_of_loops (cfun));
2295 1.1 mrg for (n = i = 0; vec_safe_iterate (get_loops (cfun), i, &loop); i++)
2296 1.1 mrg if (ira_loop_nodes[i].regno_allocno_map != NULL)
2297 1.1 mrg {
2298 1.1 mrg if (ira_loop_nodes[i].parent == NULL)
2299 1.1 mrg {
2300 1.1 mrg /* Don't remove the root. */
2301 1.1 mrg ira_loop_nodes[i].to_remove_p = false;
2302 1.1 mrg continue;
2303 1.1 mrg }
2304 1.1 mrg sorted_loops[n++] = &ira_loop_nodes[i];
2305 1.1 mrg ira_loop_nodes[i].to_remove_p
2306 1.1 mrg = ((low_pressure_loop_node_p (ira_loop_nodes[i].parent)
2307 1.1 mrg && low_pressure_loop_node_p (&ira_loop_nodes[i]))
2308 1.1 mrg #ifdef STACK_REGS
2309 1.1 mrg || loop_with_complex_edge_p (ira_loop_nodes[i].loop)
2310 1.1 mrg #endif
2311 1.1 mrg );
2312 1.1 mrg }
2313 1.1 mrg qsort (sorted_loops, n, sizeof (ira_loop_tree_node_t), loop_compare_func);
2314 1.1 mrg for (i = 0; i < n - param_ira_max_loops_num; i++)
2315 1.1 mrg {
2316 1.1 mrg sorted_loops[i]->to_remove_p = true;
2317 1.1 mrg if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2318 1.1 mrg fprintf
2319 1.1 mrg (ira_dump_file,
2320 1.1 mrg " Mark loop %d (header %d, freq %d, depth %d) for removal (%s)\n",
2321 1.1 mrg sorted_loops[i]->loop_num, sorted_loops[i]->loop->header->index,
2322 1.1 mrg sorted_loops[i]->loop->header->count.to_frequency (cfun),
2323 1.1 mrg loop_depth (sorted_loops[i]->loop),
2324 1.1 mrg low_pressure_loop_node_p (sorted_loops[i]->parent)
2325 1.1 mrg && low_pressure_loop_node_p (sorted_loops[i])
2326 1.1 mrg ? "low pressure" : "cheap loop");
2327 1.1 mrg }
2328 1.1 mrg ira_free (sorted_loops);
2329 1.1 mrg }
2330 1.1 mrg
2331 1.1 mrg /* Mark all loops but root for removing. */
2332 1.1 mrg static void
2333 1.1 mrg mark_all_loops_for_removal (void)
2334 1.1 mrg {
2335 1.1 mrg int i;
2336 1.1 mrg loop_p loop;
2337 1.1 mrg
2338 1.1 mrg ira_assert (current_loops != NULL);
2339 1.1 mrg FOR_EACH_VEC_SAFE_ELT (get_loops (cfun), i, loop)
2340 1.1 mrg if (ira_loop_nodes[i].regno_allocno_map != NULL)
2341 1.1 mrg {
2342 1.1 mrg if (ira_loop_nodes[i].parent == NULL)
2343 1.1 mrg {
2344 1.1 mrg /* Don't remove the root. */
2345 1.1 mrg ira_loop_nodes[i].to_remove_p = false;
2346 1.1 mrg continue;
2347 1.1 mrg }
2348 1.1 mrg ira_loop_nodes[i].to_remove_p = true;
2349 1.1 mrg if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2350 1.1 mrg fprintf
2351 1.1 mrg (ira_dump_file,
2352 1.1 mrg " Mark loop %d (header %d, freq %d, depth %d) for removal\n",
2353 1.1 mrg ira_loop_nodes[i].loop_num,
2354 1.1 mrg ira_loop_nodes[i].loop->header->index,
2355 1.1 mrg ira_loop_nodes[i].loop->header->count.to_frequency (cfun),
2356 1.1 mrg loop_depth (ira_loop_nodes[i].loop));
2357 1.1 mrg }
2358 1.1 mrg }
2359 1.1 mrg
2360 1.1 mrg /* Definition of vector of loop tree nodes. */
2361 1.1 mrg
2362 1.1 mrg /* Vec containing references to all removed loop tree nodes. */
2363 1.1 mrg static vec<ira_loop_tree_node_t> removed_loop_vec;
2364 1.1 mrg
2365 1.1 mrg /* Vec containing references to all children of loop tree nodes. */
2366 1.1 mrg static vec<ira_loop_tree_node_t> children_vec;
2367 1.1 mrg
2368 1.1 mrg /* Remove subregions of NODE if their separate allocation will not
2369 1.1 mrg improve the result. */
2370 1.1 mrg static void
2371 1.1 mrg remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_node_t node)
2372 1.1 mrg {
2373 1.1 mrg unsigned int start;
2374 1.1 mrg bool remove_p;
2375 1.1 mrg ira_loop_tree_node_t subnode;
2376 1.1 mrg
2377 1.1 mrg remove_p = node->to_remove_p;
2378 1.1 mrg if (! remove_p)
2379 1.1 mrg children_vec.safe_push (node);
2380 1.1 mrg start = children_vec.length ();
2381 1.1 mrg for (subnode = node->children; subnode != NULL; subnode = subnode->next)
2382 1.1 mrg if (subnode->bb == NULL)
2383 1.1 mrg remove_uneccesary_loop_nodes_from_loop_tree (subnode);
2384 1.1 mrg else
2385 1.1 mrg children_vec.safe_push (subnode);
2386 1.1 mrg node->children = node->subloops = NULL;
2387 1.1 mrg if (remove_p)
2388 1.1 mrg {
2389 1.1 mrg removed_loop_vec.safe_push (node);
2390 1.1 mrg return;
2391 1.1 mrg }
2392 1.1 mrg while (children_vec.length () > start)
2393 1.1 mrg {
2394 1.1 mrg subnode = children_vec.pop ();
2395 1.1 mrg subnode->parent = node;
2396 1.1 mrg subnode->next = node->children;
2397 1.1 mrg node->children = subnode;
2398 1.1 mrg if (subnode->bb == NULL)
2399 1.1 mrg {
2400 1.1 mrg subnode->subloop_next = node->subloops;
2401 1.1 mrg node->subloops = subnode;
2402 1.1 mrg }
2403 1.1 mrg }
2404 1.1 mrg }
2405 1.1 mrg
2406 1.1 mrg /* Return TRUE if NODE is inside PARENT. */
2407 1.1 mrg static bool
2408 1.1 mrg loop_is_inside_p (ira_loop_tree_node_t node, ira_loop_tree_node_t parent)
2409 1.1 mrg {
2410 1.1 mrg for (node = node->parent; node != NULL; node = node->parent)
2411 1.1 mrg if (node == parent)
2412 1.1 mrg return true;
2413 1.1 mrg return false;
2414 1.1 mrg }
2415 1.1 mrg
2416 1.1 mrg /* Sort allocnos according to their order in regno allocno list. */
2417 1.1 mrg static int
2418 1.1 mrg regno_allocno_order_compare_func (const void *v1p, const void *v2p)
2419 1.1 mrg {
2420 1.1 mrg ira_allocno_t a1 = *(const ira_allocno_t *) v1p;
2421 1.1 mrg ira_allocno_t a2 = *(const ira_allocno_t *) v2p;
2422 1.1 mrg ira_loop_tree_node_t n1 = ALLOCNO_LOOP_TREE_NODE (a1);
2423 1.1 mrg ira_loop_tree_node_t n2 = ALLOCNO_LOOP_TREE_NODE (a2);
2424 1.1 mrg
2425 1.1 mrg if (loop_is_inside_p (n1, n2))
2426 1.1 mrg return -1;
2427 1.1 mrg else if (loop_is_inside_p (n2, n1))
2428 1.1 mrg return 1;
2429 1.1 mrg /* If allocnos are equally good, sort by allocno numbers, so that
2430 1.1 mrg the results of qsort leave nothing to chance. We put allocnos
2431 1.1 mrg with higher number first in the list because it is the original
2432 1.1 mrg order for allocnos from loops on the same levels. */
2433 1.1 mrg return ALLOCNO_NUM (a2) - ALLOCNO_NUM (a1);
2434 1.1 mrg }
2435 1.1 mrg
2436 1.1 mrg /* This array is used to sort allocnos to restore allocno order in
2437 1.1 mrg the regno allocno list. */
2438 1.1 mrg static ira_allocno_t *regno_allocnos;
2439 1.1 mrg
2440 1.1 mrg /* Restore allocno order for REGNO in the regno allocno list. */
2441 1.1 mrg static void
2442 1.1 mrg ira_rebuild_regno_allocno_list (int regno)
2443 1.1 mrg {
2444 1.1 mrg int i, n;
2445 1.1 mrg ira_allocno_t a;
2446 1.1 mrg
2447 1.1 mrg for (n = 0, a = ira_regno_allocno_map[regno];
2448 1.1 mrg a != NULL;
2449 1.1 mrg a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2450 1.1 mrg regno_allocnos[n++] = a;
2451 1.1 mrg ira_assert (n > 0);
2452 1.1 mrg qsort (regno_allocnos, n, sizeof (ira_allocno_t),
2453 1.1 mrg regno_allocno_order_compare_func);
2454 1.1 mrg for (i = 1; i < n; i++)
2455 1.1 mrg ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[i - 1]) = regno_allocnos[i];
2456 1.1 mrg ALLOCNO_NEXT_REGNO_ALLOCNO (regno_allocnos[n - 1]) = NULL;
2457 1.1 mrg ira_regno_allocno_map[regno] = regno_allocnos[0];
2458 1.1 mrg if (internal_flag_ira_verbose > 1 && ira_dump_file != NULL)
2459 1.1 mrg fprintf (ira_dump_file, " Rebuilding regno allocno list for %d\n", regno);
2460 1.1 mrg }
2461 1.1 mrg
2462 1.1 mrg /* Propagate info from allocno FROM_A to allocno A. */
2463 1.1 mrg static void
2464 1.1 mrg propagate_some_info_from_allocno (ira_allocno_t a, ira_allocno_t from_a)
2465 1.1 mrg {
2466 1.1 mrg enum reg_class aclass;
2467 1.1 mrg
2468 1.1 mrg merge_hard_reg_conflicts (from_a, a, false);
2469 1.1 mrg ALLOCNO_NREFS (a) += ALLOCNO_NREFS (from_a);
2470 1.1 mrg ALLOCNO_FREQ (a) += ALLOCNO_FREQ (from_a);
2471 1.1 mrg ALLOCNO_CALL_FREQ (a) += ALLOCNO_CALL_FREQ (from_a);
2472 1.1 mrg ALLOCNO_CALLS_CROSSED_NUM (a) += ALLOCNO_CALLS_CROSSED_NUM (from_a);
2473 1.1 mrg ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a)
2474 1.1 mrg += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (from_a);
2475 1.1 mrg ALLOCNO_CROSSED_CALLS_ABIS (a) |= ALLOCNO_CROSSED_CALLS_ABIS (from_a);
2476 1.1 mrg ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a)
2477 1.1 mrg |= ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (from_a);
2478 1.1 mrg
2479 1.1 mrg ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a)
2480 1.1 mrg += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (from_a);
2481 1.1 mrg if (! ALLOCNO_BAD_SPILL_P (from_a))
2482 1.1 mrg ALLOCNO_BAD_SPILL_P (a) = false;
2483 1.1 mrg aclass = ALLOCNO_CLASS (from_a);
2484 1.1 mrg ira_assert (aclass == ALLOCNO_CLASS (a));
2485 1.1 mrg ira_allocate_and_accumulate_costs (&ALLOCNO_HARD_REG_COSTS (a), aclass,
2486 1.1 mrg ALLOCNO_HARD_REG_COSTS (from_a));
2487 1.1 mrg ira_allocate_and_accumulate_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
2488 1.1 mrg aclass,
2489 1.1 mrg ALLOCNO_CONFLICT_HARD_REG_COSTS (from_a));
2490 1.1 mrg ALLOCNO_CLASS_COST (a) += ALLOCNO_CLASS_COST (from_a);
2491 1.1 mrg ALLOCNO_MEMORY_COST (a) += ALLOCNO_MEMORY_COST (from_a);
2492 1.1 mrg }
2493 1.1 mrg
2494 1.1 mrg /* Remove allocnos from loops removed from the allocation
2495 1.1 mrg consideration. */
2496 1.1 mrg static void
2497 1.1 mrg remove_unnecessary_allocnos (void)
2498 1.1 mrg {
2499 1.1 mrg int regno;
2500 1.1 mrg bool merged_p, rebuild_p;
2501 1.1 mrg ira_allocno_t a, prev_a, next_a, parent_a;
2502 1.1 mrg ira_loop_tree_node_t a_node, parent;
2503 1.1 mrg
2504 1.1 mrg merged_p = false;
2505 1.1 mrg regno_allocnos = NULL;
2506 1.1 mrg for (regno = max_reg_num () - 1; regno >= FIRST_PSEUDO_REGISTER; regno--)
2507 1.1 mrg {
2508 1.1 mrg rebuild_p = false;
2509 1.1 mrg for (prev_a = NULL, a = ira_regno_allocno_map[regno];
2510 1.1 mrg a != NULL;
2511 1.1 mrg a = next_a)
2512 1.1 mrg {
2513 1.1 mrg next_a = ALLOCNO_NEXT_REGNO_ALLOCNO (a);
2514 1.1 mrg a_node = ALLOCNO_LOOP_TREE_NODE (a);
2515 1.1 mrg if (! a_node->to_remove_p)
2516 1.1 mrg prev_a = a;
2517 1.1 mrg else
2518 1.1 mrg {
2519 1.1 mrg for (parent = a_node->parent;
2520 1.1 mrg (parent_a = parent->regno_allocno_map[regno]) == NULL
2521 1.1 mrg && parent->to_remove_p;
2522 1.1 mrg parent = parent->parent)
2523 1.1 mrg ;
2524 1.1 mrg if (parent_a == NULL)
2525 1.1 mrg {
2526 1.1 mrg /* There are no allocnos with the same regno in
2527 1.1 mrg upper region -- just move the allocno to the
2528 1.1 mrg upper region. */
2529 1.1 mrg prev_a = a;
2530 1.1 mrg ALLOCNO_LOOP_TREE_NODE (a) = parent;
2531 1.1 mrg parent->regno_allocno_map[regno] = a;
2532 1.1 mrg bitmap_set_bit (parent->all_allocnos, ALLOCNO_NUM (a));
2533 1.1 mrg rebuild_p = true;
2534 1.1 mrg }
2535 1.1 mrg else
2536 1.1 mrg {
2537 1.1 mrg /* Remove the allocno and update info of allocno in
2538 1.1 mrg the upper region. */
2539 1.1 mrg if (prev_a == NULL)
2540 1.1 mrg ira_regno_allocno_map[regno] = next_a;
2541 1.1 mrg else
2542 1.1 mrg ALLOCNO_NEXT_REGNO_ALLOCNO (prev_a) = next_a;
2543 1.1 mrg move_allocno_live_ranges (a, parent_a);
2544 1.1 mrg merged_p = true;
2545 1.1 mrg propagate_some_info_from_allocno (parent_a, a);
2546 1.1 mrg /* Remove it from the corresponding regno allocno
2547 1.1 mrg map to avoid info propagation of subsequent
2548 1.1 mrg allocno into this already removed allocno. */
2549 1.1 mrg a_node->regno_allocno_map[regno] = NULL;
2550 1.1 mrg ira_remove_allocno_prefs (a);
2551 1.1 mrg finish_allocno (a);
2552 1.1 mrg }
2553 1.1 mrg }
2554 1.1 mrg }
2555 1.1 mrg if (rebuild_p)
2556 1.1 mrg /* We need to restore the order in regno allocno list. */
2557 1.1 mrg {
2558 1.1 mrg if (regno_allocnos == NULL)
2559 1.1 mrg regno_allocnos
2560 1.1 mrg = (ira_allocno_t *) ira_allocate (sizeof (ira_allocno_t)
2561 1.1 mrg * ira_allocnos_num);
2562 1.1 mrg ira_rebuild_regno_allocno_list (regno);
2563 1.1 mrg }
2564 1.1 mrg }
2565 1.1 mrg if (merged_p)
2566 1.1 mrg ira_rebuild_start_finish_chains ();
2567 1.1 mrg if (regno_allocnos != NULL)
2568 1.1 mrg ira_free (regno_allocnos);
2569 1.1 mrg }
2570 1.1 mrg
2571 1.1 mrg /* Remove allocnos from all loops but the root. */
2572 1.1 mrg static void
2573 1.1 mrg remove_low_level_allocnos (void)
2574 1.1 mrg {
2575 1.1 mrg int regno;
2576 1.1 mrg bool merged_p, propagate_p;
2577 1.1 mrg ira_allocno_t a, top_a;
2578 1.1 mrg ira_loop_tree_node_t a_node, parent;
2579 1.1 mrg ira_allocno_iterator ai;
2580 1.1 mrg
2581 1.1 mrg merged_p = false;
2582 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
2583 1.1 mrg {
2584 1.1 mrg a_node = ALLOCNO_LOOP_TREE_NODE (a);
2585 1.1 mrg if (a_node == ira_loop_tree_root || ALLOCNO_CAP_MEMBER (a) != NULL)
2586 1.1 mrg continue;
2587 1.1 mrg regno = ALLOCNO_REGNO (a);
2588 1.1 mrg if ((top_a = ira_loop_tree_root->regno_allocno_map[regno]) == NULL)
2589 1.1 mrg {
2590 1.1 mrg ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
2591 1.1 mrg ira_loop_tree_root->regno_allocno_map[regno] = a;
2592 1.1 mrg continue;
2593 1.1 mrg }
2594 1.1 mrg propagate_p = a_node->parent->regno_allocno_map[regno] == NULL;
2595 1.1 mrg /* Remove the allocno and update info of allocno in the upper
2596 1.1 mrg region. */
2597 1.1 mrg move_allocno_live_ranges (a, top_a);
2598 1.1 mrg merged_p = true;
2599 1.1 mrg if (propagate_p)
2600 1.1 mrg propagate_some_info_from_allocno (top_a, a);
2601 1.1 mrg }
2602 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
2603 1.1 mrg {
2604 1.1 mrg a_node = ALLOCNO_LOOP_TREE_NODE (a);
2605 1.1 mrg if (a_node == ira_loop_tree_root)
2606 1.1 mrg continue;
2607 1.1 mrg parent = a_node->parent;
2608 1.1 mrg regno = ALLOCNO_REGNO (a);
2609 1.1 mrg if (ALLOCNO_CAP_MEMBER (a) != NULL)
2610 1.1 mrg ira_assert (ALLOCNO_CAP (a) != NULL);
2611 1.1 mrg else if (ALLOCNO_CAP (a) == NULL)
2612 1.1 mrg ira_assert (parent->regno_allocno_map[regno] != NULL);
2613 1.1 mrg }
2614 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
2615 1.1 mrg {
2616 1.1 mrg regno = ALLOCNO_REGNO (a);
2617 1.1 mrg if (ira_loop_tree_root->regno_allocno_map[regno] == a)
2618 1.1 mrg {
2619 1.1 mrg ira_object_t obj;
2620 1.1 mrg ira_allocno_object_iterator oi;
2621 1.1 mrg
2622 1.1 mrg ira_regno_allocno_map[regno] = a;
2623 1.1 mrg ALLOCNO_NEXT_REGNO_ALLOCNO (a) = NULL;
2624 1.1 mrg ALLOCNO_CAP_MEMBER (a) = NULL;
2625 1.1 mrg FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
2626 1.1 mrg OBJECT_CONFLICT_HARD_REGS (obj)
2627 1.1 mrg = OBJECT_TOTAL_CONFLICT_HARD_REGS (obj);
2628 1.1 mrg #ifdef STACK_REGS
2629 1.1 mrg if (ALLOCNO_TOTAL_NO_STACK_REG_P (a))
2630 1.1 mrg ALLOCNO_NO_STACK_REG_P (a) = true;
2631 1.1 mrg #endif
2632 1.1 mrg }
2633 1.1 mrg else
2634 1.1 mrg {
2635 1.1 mrg ira_remove_allocno_prefs (a);
2636 1.1 mrg finish_allocno (a);
2637 1.1 mrg }
2638 1.1 mrg }
2639 1.1 mrg if (merged_p)
2640 1.1 mrg ira_rebuild_start_finish_chains ();
2641 1.1 mrg }
2642 1.1 mrg
2643 1.1 mrg /* Remove loops from consideration. We remove all loops except for
2644 1.1 mrg root if ALL_P or loops for which a separate allocation will not
2645 1.1 mrg improve the result. We have to do this after allocno creation and
2646 1.1 mrg their costs and allocno class evaluation because only after that
2647 1.1 mrg the register pressure can be known and is calculated. */
2648 1.1 mrg static void
2649 1.1 mrg remove_unnecessary_regions (bool all_p)
2650 1.1 mrg {
2651 1.1 mrg if (current_loops == NULL)
2652 1.1 mrg return;
2653 1.1 mrg if (all_p)
2654 1.1 mrg mark_all_loops_for_removal ();
2655 1.1 mrg else
2656 1.1 mrg mark_loops_for_removal ();
2657 1.1 mrg children_vec.create (last_basic_block_for_fn (cfun)
2658 1.1 mrg + number_of_loops (cfun));
2659 1.1 mrg removed_loop_vec.create (last_basic_block_for_fn (cfun)
2660 1.1 mrg + number_of_loops (cfun));
2661 1.1 mrg remove_uneccesary_loop_nodes_from_loop_tree (ira_loop_tree_root);
2662 1.1 mrg children_vec.release ();
2663 1.1 mrg if (all_p)
2664 1.1 mrg remove_low_level_allocnos ();
2665 1.1 mrg else
2666 1.1 mrg remove_unnecessary_allocnos ();
2667 1.1 mrg while (removed_loop_vec.length () > 0)
2668 1.1 mrg finish_loop_tree_node (removed_loop_vec.pop ());
2669 1.1 mrg removed_loop_vec.release ();
2670 1.1 mrg }
2671 1.1 mrg
2672 1.1 mrg
2673 1.1 mrg
2675 1.1 mrg /* At this point true value of allocno attribute bad_spill_p means
2676 1.1 mrg that there is an insn where allocno occurs and where the allocno
2677 1.1 mrg cannot be used as memory. The function updates the attribute, now
2678 1.1 mrg it can be true only for allocnos which cannot be used as memory in
2679 1.1 mrg an insn and in whose live ranges there is other allocno deaths.
2680 1.1 mrg Spilling allocnos with true value will not improve the code because
2681 1.1 mrg it will not make other allocnos colorable and additional reloads
2682 1.1 mrg for the corresponding pseudo will be generated in reload pass for
2683 1.1 mrg each insn it occurs.
2684 1.1 mrg
2685 1.1 mrg This is a trick mentioned in one classic article of Chaitin etc
2686 1.1 mrg which is frequently omitted in other implementations of RA based on
2687 1.1 mrg graph coloring. */
2688 1.1 mrg static void
2689 1.1 mrg update_bad_spill_attribute (void)
2690 1.1 mrg {
2691 1.1 mrg int i;
2692 1.1 mrg ira_allocno_t a;
2693 1.1 mrg ira_allocno_iterator ai;
2694 1.1 mrg ira_allocno_object_iterator aoi;
2695 1.1 mrg ira_object_t obj;
2696 1.1 mrg live_range_t r;
2697 1.1 mrg enum reg_class aclass;
2698 1.1 mrg bitmap_head dead_points[N_REG_CLASSES];
2699 1.1 mrg
2700 1.1 mrg for (i = 0; i < ira_allocno_classes_num; i++)
2701 1.1 mrg {
2702 1.1 mrg aclass = ira_allocno_classes[i];
2703 1.1 mrg bitmap_initialize (&dead_points[aclass], ®_obstack);
2704 1.1 mrg }
2705 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
2706 1.1 mrg {
2707 1.1 mrg aclass = ALLOCNO_CLASS (a);
2708 1.1 mrg if (aclass == NO_REGS)
2709 1.1 mrg continue;
2710 1.1 mrg FOR_EACH_ALLOCNO_OBJECT (a, obj, aoi)
2711 1.1 mrg for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
2712 1.1 mrg bitmap_set_bit (&dead_points[aclass], r->finish);
2713 1.1 mrg }
2714 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
2715 1.1 mrg {
2716 1.1 mrg aclass = ALLOCNO_CLASS (a);
2717 1.1 mrg if (aclass == NO_REGS)
2718 1.1 mrg continue;
2719 1.1 mrg if (! ALLOCNO_BAD_SPILL_P (a))
2720 1.1 mrg continue;
2721 1.1 mrg FOR_EACH_ALLOCNO_OBJECT (a, obj, aoi)
2722 1.1 mrg {
2723 1.1 mrg for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
2724 1.1 mrg {
2725 1.1 mrg for (i = r->start + 1; i < r->finish; i++)
2726 1.1 mrg if (bitmap_bit_p (&dead_points[aclass], i))
2727 1.1 mrg break;
2728 1.1 mrg if (i < r->finish)
2729 1.1 mrg break;
2730 1.1 mrg }
2731 1.1 mrg if (r != NULL)
2732 1.1 mrg {
2733 1.1 mrg ALLOCNO_BAD_SPILL_P (a) = false;
2734 1.1 mrg break;
2735 1.1 mrg }
2736 1.1 mrg }
2737 1.1 mrg }
2738 1.1 mrg for (i = 0; i < ira_allocno_classes_num; i++)
2739 1.1 mrg {
2740 1.1 mrg aclass = ira_allocno_classes[i];
2741 1.1 mrg bitmap_clear (&dead_points[aclass]);
2742 1.1 mrg }
2743 1.1 mrg }
2744 1.1 mrg
2745 1.1 mrg
2746 1.1 mrg
2748 1.1 mrg /* Set up minimal and maximal live range points for allocnos. */
2749 1.1 mrg static void
2750 1.1 mrg setup_min_max_allocno_live_range_point (void)
2751 1.1 mrg {
2752 1.1 mrg int i;
2753 1.1 mrg ira_allocno_t a, parent_a, cap;
2754 1.1 mrg ira_allocno_iterator ai;
2755 1.1 mrg #ifdef ENABLE_IRA_CHECKING
2756 1.1 mrg ira_object_iterator oi;
2757 1.1 mrg ira_object_t obj;
2758 1.1 mrg #endif
2759 1.1 mrg live_range_t r;
2760 1.1 mrg ira_loop_tree_node_t parent;
2761 1.1 mrg
2762 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
2763 1.1 mrg {
2764 1.1 mrg int n = ALLOCNO_NUM_OBJECTS (a);
2765 1.1 mrg
2766 1.1 mrg for (i = 0; i < n; i++)
2767 1.1 mrg {
2768 1.1 mrg ira_object_t obj = ALLOCNO_OBJECT (a, i);
2769 1.1 mrg r = OBJECT_LIVE_RANGES (obj);
2770 1.1 mrg if (r == NULL)
2771 1.1 mrg continue;
2772 1.1 mrg OBJECT_MAX (obj) = r->finish;
2773 1.1 mrg for (; r->next != NULL; r = r->next)
2774 1.1 mrg ;
2775 1.1 mrg OBJECT_MIN (obj) = r->start;
2776 1.1 mrg }
2777 1.1 mrg }
2778 1.1 mrg for (i = max_reg_num () - 1; i >= FIRST_PSEUDO_REGISTER; i--)
2779 1.1 mrg for (a = ira_regno_allocno_map[i];
2780 1.1 mrg a != NULL;
2781 1.1 mrg a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
2782 1.1 mrg {
2783 1.1 mrg int j;
2784 1.1 mrg int n = ALLOCNO_NUM_OBJECTS (a);
2785 1.1 mrg
2786 1.1 mrg for (j = 0; j < n; j++)
2787 1.1 mrg {
2788 1.1 mrg ira_object_t obj = ALLOCNO_OBJECT (a, j);
2789 1.1 mrg ira_object_t parent_obj;
2790 1.1 mrg
2791 1.1 mrg if (OBJECT_MAX (obj) < 0)
2792 1.1 mrg {
2793 1.1 mrg /* The object is not used and hence does not live. */
2794 1.1 mrg ira_assert (OBJECT_LIVE_RANGES (obj) == NULL);
2795 1.1 mrg OBJECT_MAX (obj) = 0;
2796 1.1 mrg OBJECT_MIN (obj) = 1;
2797 1.1 mrg continue;
2798 1.1 mrg }
2799 1.1 mrg ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
2800 1.1 mrg /* Accumulation of range info. */
2801 1.1 mrg if (ALLOCNO_CAP (a) != NULL)
2802 1.1 mrg {
2803 1.1 mrg for (cap = ALLOCNO_CAP (a); cap != NULL; cap = ALLOCNO_CAP (cap))
2804 1.1 mrg {
2805 1.1 mrg ira_object_t cap_obj = ALLOCNO_OBJECT (cap, j);
2806 1.1 mrg if (OBJECT_MAX (cap_obj) < OBJECT_MAX (obj))
2807 1.1 mrg OBJECT_MAX (cap_obj) = OBJECT_MAX (obj);
2808 1.1 mrg if (OBJECT_MIN (cap_obj) > OBJECT_MIN (obj))
2809 1.1 mrg OBJECT_MIN (cap_obj) = OBJECT_MIN (obj);
2810 1.1 mrg }
2811 1.1 mrg continue;
2812 1.1 mrg }
2813 1.1 mrg if ((parent = ALLOCNO_LOOP_TREE_NODE (a)->parent) == NULL)
2814 1.1 mrg continue;
2815 1.1 mrg parent_a = parent->regno_allocno_map[i];
2816 1.1 mrg parent_obj = ALLOCNO_OBJECT (parent_a, j);
2817 1.1 mrg if (OBJECT_MAX (parent_obj) < OBJECT_MAX (obj))
2818 1.1 mrg OBJECT_MAX (parent_obj) = OBJECT_MAX (obj);
2819 1.1 mrg if (OBJECT_MIN (parent_obj) > OBJECT_MIN (obj))
2820 1.1 mrg OBJECT_MIN (parent_obj) = OBJECT_MIN (obj);
2821 1.1 mrg }
2822 1.1 mrg }
2823 1.1 mrg #ifdef ENABLE_IRA_CHECKING
2824 1.1 mrg FOR_EACH_OBJECT (obj, oi)
2825 1.1 mrg {
2826 1.1 mrg if ((OBJECT_MIN (obj) >= 0 && OBJECT_MIN (obj) <= ira_max_point)
2827 1.1 mrg && (OBJECT_MAX (obj) >= 0 && OBJECT_MAX (obj) <= ira_max_point))
2828 1.1 mrg continue;
2829 1.1 mrg gcc_unreachable ();
2830 1.1 mrg }
2831 1.1 mrg #endif
2832 1.1 mrg }
2833 1.1 mrg
2834 1.1 mrg /* Sort allocnos according to their live ranges. Allocnos with
2835 1.1 mrg smaller allocno class are put first unless we use priority
2836 1.1 mrg coloring. Allocnos with the same class are ordered according
2837 1.1 mrg their start (min). Allocnos with the same start are ordered
2838 1.1 mrg according their finish (max). */
2839 1.1 mrg static int
2840 1.1 mrg object_range_compare_func (const void *v1p, const void *v2p)
2841 1.1 mrg {
2842 1.1 mrg int diff;
2843 1.1 mrg ira_object_t obj1 = *(const ira_object_t *) v1p;
2844 1.1 mrg ira_object_t obj2 = *(const ira_object_t *) v2p;
2845 1.1 mrg ira_allocno_t a1 = OBJECT_ALLOCNO (obj1);
2846 1.1 mrg ira_allocno_t a2 = OBJECT_ALLOCNO (obj2);
2847 1.1 mrg
2848 1.1 mrg if ((diff = OBJECT_MIN (obj1) - OBJECT_MIN (obj2)) != 0)
2849 1.1 mrg return diff;
2850 1.1 mrg if ((diff = OBJECT_MAX (obj1) - OBJECT_MAX (obj2)) != 0)
2851 1.1 mrg return diff;
2852 1.1 mrg return ALLOCNO_NUM (a1) - ALLOCNO_NUM (a2);
2853 1.1 mrg }
2854 1.1 mrg
2855 1.1 mrg /* Sort ira_object_id_map and set up conflict id of allocnos. */
2856 1.1 mrg static void
2857 1.1 mrg sort_conflict_id_map (void)
2858 1.1 mrg {
2859 1.1 mrg int i, num;
2860 1.1 mrg ira_allocno_t a;
2861 1.1 mrg ira_allocno_iterator ai;
2862 1.1 mrg
2863 1.1 mrg num = 0;
2864 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
2865 1.1 mrg {
2866 1.1 mrg ira_allocno_object_iterator oi;
2867 1.1 mrg ira_object_t obj;
2868 1.1 mrg
2869 1.1 mrg FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
2870 1.1 mrg ira_object_id_map[num++] = obj;
2871 1.1 mrg }
2872 1.1 mrg if (num > 1)
2873 1.1 mrg qsort (ira_object_id_map, num, sizeof (ira_object_t),
2874 1.1 mrg object_range_compare_func);
2875 1.1 mrg for (i = 0; i < num; i++)
2876 1.1 mrg {
2877 1.1 mrg ira_object_t obj = ira_object_id_map[i];
2878 1.1 mrg
2879 1.1 mrg gcc_assert (obj != NULL);
2880 1.1 mrg OBJECT_CONFLICT_ID (obj) = i;
2881 1.1 mrg }
2882 1.1 mrg for (i = num; i < ira_objects_num; i++)
2883 1.1 mrg ira_object_id_map[i] = NULL;
2884 1.1 mrg }
2885 1.1 mrg
2886 1.1 mrg /* Set up minimal and maximal conflict ids of allocnos with which
2887 1.1 mrg given allocno can conflict. */
2888 1.1 mrg static void
2889 1.1 mrg setup_min_max_conflict_allocno_ids (void)
2890 1.1 mrg {
2891 1.1 mrg int aclass;
2892 1.1 mrg int i, j, min, max, start, finish, first_not_finished, filled_area_start;
2893 1.1 mrg int *live_range_min, *last_lived;
2894 1.1 mrg int word0_min, word0_max;
2895 1.1 mrg ira_allocno_t a;
2896 1.1 mrg ira_allocno_iterator ai;
2897 1.1 mrg
2898 1.1 mrg live_range_min = (int *) ira_allocate (sizeof (int) * ira_objects_num);
2899 1.1 mrg aclass = -1;
2900 1.1 mrg first_not_finished = -1;
2901 1.1 mrg for (i = 0; i < ira_objects_num; i++)
2902 1.1 mrg {
2903 1.1 mrg ira_object_t obj = ira_object_id_map[i];
2904 1.1 mrg
2905 1.1 mrg if (obj == NULL)
2906 1.1 mrg continue;
2907 1.1 mrg
2908 1.1 mrg a = OBJECT_ALLOCNO (obj);
2909 1.1 mrg
2910 1.1 mrg if (aclass < 0)
2911 1.1 mrg {
2912 1.1 mrg aclass = ALLOCNO_CLASS (a);
2913 1.1 mrg min = i;
2914 1.1 mrg first_not_finished = i;
2915 1.1 mrg }
2916 1.1 mrg else
2917 1.1 mrg {
2918 1.1 mrg start = OBJECT_MIN (obj);
2919 1.1 mrg /* If we skip an allocno, the allocno with smaller ids will
2920 1.1 mrg be also skipped because of the secondary sorting the
2921 1.1 mrg range finishes (see function
2922 1.1 mrg object_range_compare_func). */
2923 1.1 mrg while (first_not_finished < i
2924 1.1 mrg && start > OBJECT_MAX (ira_object_id_map
2925 1.1 mrg [first_not_finished]))
2926 1.1 mrg first_not_finished++;
2927 1.1 mrg min = first_not_finished;
2928 1.1 mrg }
2929 1.1 mrg if (min == i)
2930 1.1 mrg /* We could increase min further in this case but it is good
2931 1.1 mrg enough. */
2932 1.1 mrg min++;
2933 1.1 mrg live_range_min[i] = OBJECT_MIN (obj);
2934 1.1 mrg OBJECT_MIN (obj) = min;
2935 1.1 mrg }
2936 1.1 mrg last_lived = (int *) ira_allocate (sizeof (int) * ira_max_point);
2937 1.1 mrg aclass = -1;
2938 1.1 mrg filled_area_start = -1;
2939 1.1 mrg for (i = ira_objects_num - 1; i >= 0; i--)
2940 1.1 mrg {
2941 1.1 mrg ira_object_t obj = ira_object_id_map[i];
2942 1.1 mrg
2943 1.1 mrg if (obj == NULL)
2944 1.1 mrg continue;
2945 1.1 mrg
2946 1.1 mrg a = OBJECT_ALLOCNO (obj);
2947 1.1 mrg if (aclass < 0)
2948 1.1 mrg {
2949 1.1 mrg aclass = ALLOCNO_CLASS (a);
2950 1.1 mrg for (j = 0; j < ira_max_point; j++)
2951 1.1 mrg last_lived[j] = -1;
2952 1.1 mrg filled_area_start = ira_max_point;
2953 1.1 mrg }
2954 1.1 mrg min = live_range_min[i];
2955 1.1 mrg finish = OBJECT_MAX (obj);
2956 1.1 mrg max = last_lived[finish];
2957 1.1 mrg if (max < 0)
2958 1.1 mrg /* We could decrease max further in this case but it is good
2959 1.1 mrg enough. */
2960 1.1 mrg max = OBJECT_CONFLICT_ID (obj) - 1;
2961 1.1 mrg OBJECT_MAX (obj) = max;
2962 1.1 mrg /* In filling, we can go further A range finish to recognize
2963 1.1 mrg intersection quickly because if the finish of subsequently
2964 1.1 mrg processed allocno (it has smaller conflict id) range is
2965 1.1 mrg further A range finish than they are definitely intersected
2966 1.1 mrg (the reason for this is the allocnos with bigger conflict id
2967 1.1 mrg have their range starts not smaller than allocnos with
2968 1.1 mrg smaller ids. */
2969 1.1 mrg for (j = min; j < filled_area_start; j++)
2970 1.1 mrg last_lived[j] = i;
2971 1.1 mrg filled_area_start = min;
2972 1.1 mrg }
2973 1.1 mrg ira_free (last_lived);
2974 1.1 mrg ira_free (live_range_min);
2975 1.1 mrg
2976 1.1 mrg /* For allocnos with more than one object, we may later record extra conflicts in
2977 1.1 mrg subobject 0 that we cannot really know about here.
2978 1.1 mrg For now, simply widen the min/max range of these subobjects. */
2979 1.1 mrg
2980 1.1 mrg word0_min = INT_MAX;
2981 1.1 mrg word0_max = INT_MIN;
2982 1.1 mrg
2983 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
2984 1.1 mrg {
2985 1.1 mrg int n = ALLOCNO_NUM_OBJECTS (a);
2986 1.1 mrg ira_object_t obj0;
2987 1.1 mrg
2988 1.1 mrg if (n < 2)
2989 1.1 mrg continue;
2990 1.1 mrg obj0 = ALLOCNO_OBJECT (a, 0);
2991 1.1 mrg if (OBJECT_CONFLICT_ID (obj0) < word0_min)
2992 1.1 mrg word0_min = OBJECT_CONFLICT_ID (obj0);
2993 1.1 mrg if (OBJECT_CONFLICT_ID (obj0) > word0_max)
2994 1.1 mrg word0_max = OBJECT_CONFLICT_ID (obj0);
2995 1.1 mrg }
2996 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
2997 1.1 mrg {
2998 1.1 mrg int n = ALLOCNO_NUM_OBJECTS (a);
2999 1.1 mrg ira_object_t obj0;
3000 1.1 mrg
3001 1.1 mrg if (n < 2)
3002 1.1 mrg continue;
3003 1.1 mrg obj0 = ALLOCNO_OBJECT (a, 0);
3004 1.1 mrg if (OBJECT_MIN (obj0) > word0_min)
3005 1.1 mrg OBJECT_MIN (obj0) = word0_min;
3006 1.1 mrg if (OBJECT_MAX (obj0) < word0_max)
3007 1.1 mrg OBJECT_MAX (obj0) = word0_max;
3008 1.1 mrg }
3009 1.1 mrg }
3010 1.1 mrg
3011 1.1 mrg
3012 1.1 mrg
3014 1.1 mrg static void
3015 1.1 mrg create_caps (void)
3016 1.1 mrg {
3017 1.1 mrg ira_allocno_t a;
3018 1.1 mrg ira_allocno_iterator ai;
3019 1.1 mrg ira_loop_tree_node_t loop_tree_node;
3020 1.1 mrg
3021 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
3022 1.1 mrg {
3023 1.1 mrg if (ALLOCNO_LOOP_TREE_NODE (a) == ira_loop_tree_root)
3024 1.1 mrg continue;
3025 1.1 mrg if (ALLOCNO_CAP_MEMBER (a) != NULL)
3026 1.1 mrg create_cap_allocno (a);
3027 1.1 mrg else if (ALLOCNO_CAP (a) == NULL)
3028 1.1 mrg {
3029 1.1 mrg loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
3030 1.1 mrg if (!bitmap_bit_p (loop_tree_node->border_allocnos, ALLOCNO_NUM (a)))
3031 1.1 mrg create_cap_allocno (a);
3032 1.1 mrg }
3033 1.1 mrg }
3034 1.1 mrg }
3035 1.1 mrg
3036 1.1 mrg
3037 1.1 mrg
3039 1.1 mrg /* The page contains code transforming more one region internal
3040 1.1 mrg representation (IR) to one region IR which is necessary for reload.
3041 1.1 mrg This transformation is called IR flattening. We might just rebuild
3042 1.1 mrg the IR for one region but we don't do it because it takes a lot of
3043 1.1 mrg time. */
3044 1.1 mrg
3045 1.1 mrg /* Map: regno -> allocnos which will finally represent the regno for
3046 1.1 mrg IR with one region. */
3047 1.1 mrg static ira_allocno_t *regno_top_level_allocno_map;
3048 1.1 mrg
3049 1.1 mrg /* Find the allocno that corresponds to A at a level one higher up in the
3050 1.1 mrg loop tree. Returns NULL if A is a cap, or if it has no parent. */
3051 1.1 mrg ira_allocno_t
3052 1.1 mrg ira_parent_allocno (ira_allocno_t a)
3053 1.1 mrg {
3054 1.1 mrg ira_loop_tree_node_t parent;
3055 1.1 mrg
3056 1.1 mrg if (ALLOCNO_CAP (a) != NULL)
3057 1.1 mrg return NULL;
3058 1.1 mrg
3059 1.1 mrg parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
3060 1.1 mrg if (parent == NULL)
3061 1.1 mrg return NULL;
3062 1.1 mrg
3063 1.1 mrg return parent->regno_allocno_map[ALLOCNO_REGNO (a)];
3064 1.1 mrg }
3065 1.1 mrg
3066 1.1 mrg /* Find the allocno that corresponds to A at a level one higher up in the
3067 1.1 mrg loop tree. If ALLOCNO_CAP is set for A, return that. */
3068 1.1 mrg ira_allocno_t
3069 1.1 mrg ira_parent_or_cap_allocno (ira_allocno_t a)
3070 1.1 mrg {
3071 1.1 mrg if (ALLOCNO_CAP (a) != NULL)
3072 1.1 mrg return ALLOCNO_CAP (a);
3073 1.1 mrg
3074 1.1 mrg return ira_parent_allocno (a);
3075 1.1 mrg }
3076 1.1 mrg
3077 1.1 mrg /* Process all allocnos originated from pseudo REGNO and copy live
3078 1.1 mrg ranges, hard reg conflicts, and allocno stack reg attributes from
3079 1.1 mrg low level allocnos to final allocnos which are destinations of
3080 1.1 mrg removed stores at a loop exit. Return true if we copied live
3081 1.1 mrg ranges. */
3082 1.1 mrg static bool
3083 1.1 mrg copy_info_to_removed_store_destinations (int regno)
3084 1.1 mrg {
3085 1.1 mrg ira_allocno_t a;
3086 1.1 mrg ira_allocno_t parent_a = NULL;
3087 1.1 mrg ira_loop_tree_node_t parent;
3088 1.1 mrg bool merged_p;
3089 1.1 mrg
3090 1.1 mrg merged_p = false;
3091 1.1 mrg for (a = ira_regno_allocno_map[regno];
3092 1.1 mrg a != NULL;
3093 1.1 mrg a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
3094 1.1 mrg {
3095 1.1 mrg if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))])
3096 1.1 mrg /* This allocno will be removed. */
3097 1.1 mrg continue;
3098 1.1 mrg
3099 1.1 mrg /* Caps will be removed. */
3100 1.1 mrg ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3101 1.1 mrg for (parent = ALLOCNO_LOOP_TREE_NODE (a)->parent;
3102 1.1 mrg parent != NULL;
3103 1.1 mrg parent = parent->parent)
3104 1.1 mrg if ((parent_a = parent->regno_allocno_map[regno]) == NULL
3105 1.1 mrg || (parent_a
3106 1.1 mrg == regno_top_level_allocno_map[REGNO
3107 1.1 mrg (allocno_emit_reg (parent_a))]
3108 1.1 mrg && ALLOCNO_EMIT_DATA (parent_a)->mem_optimized_dest_p))
3109 1.1 mrg break;
3110 1.1 mrg if (parent == NULL || parent_a == NULL)
3111 1.1 mrg continue;
3112 1.1 mrg
3113 1.1 mrg copy_allocno_live_ranges (a, parent_a);
3114 1.1 mrg merge_hard_reg_conflicts (a, parent_a, true);
3115 1.1 mrg
3116 1.1 mrg ALLOCNO_CALL_FREQ (parent_a) += ALLOCNO_CALL_FREQ (a);
3117 1.1 mrg ALLOCNO_CALLS_CROSSED_NUM (parent_a)
3118 1.1 mrg += ALLOCNO_CALLS_CROSSED_NUM (a);
3119 1.1 mrg ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
3120 1.1 mrg += ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
3121 1.1 mrg ALLOCNO_CROSSED_CALLS_ABIS (parent_a)
3122 1.1 mrg |= ALLOCNO_CROSSED_CALLS_ABIS (a);
3123 1.1 mrg ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (parent_a)
3124 1.1 mrg |= ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS (a);
3125 1.1 mrg ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
3126 1.1 mrg += ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
3127 1.1 mrg merged_p = true;
3128 1.1 mrg }
3129 1.1 mrg return merged_p;
3130 1.1 mrg }
3131 1.1 mrg
3132 1.1 mrg /* Flatten the IR. In other words, this function transforms IR as if
3133 1.1 mrg it were built with one region (without loops). We could make it
3134 1.1 mrg much simpler by rebuilding IR with one region, but unfortunately it
3135 1.1 mrg takes a lot of time. MAX_REGNO_BEFORE_EMIT and
3136 1.1 mrg IRA_MAX_POINT_BEFORE_EMIT are correspondingly MAX_REG_NUM () and
3137 1.1 mrg IRA_MAX_POINT before emitting insns on the loop borders. */
3138 1.1 mrg void
3139 1.1 mrg ira_flattening (int max_regno_before_emit, int ira_max_point_before_emit)
3140 1.1 mrg {
3141 1.1 mrg int i, j;
3142 1.1 mrg bool keep_p;
3143 1.1 mrg int hard_regs_num;
3144 1.1 mrg bool new_pseudos_p, merged_p, mem_dest_p;
3145 1.1 mrg unsigned int n;
3146 1.1 mrg enum reg_class aclass;
3147 1.1 mrg ira_allocno_t a, parent_a, first, second, node_first, node_second;
3148 1.1 mrg ira_copy_t cp;
3149 1.1 mrg ira_loop_tree_node_t node;
3150 1.1 mrg live_range_t r;
3151 1.1 mrg ira_allocno_iterator ai;
3152 1.1 mrg ira_copy_iterator ci;
3153 1.1 mrg
3154 1.1 mrg regno_top_level_allocno_map
3155 1.1 mrg = (ira_allocno_t *) ira_allocate (max_reg_num ()
3156 1.1 mrg * sizeof (ira_allocno_t));
3157 1.1 mrg memset (regno_top_level_allocno_map, 0,
3158 1.1 mrg max_reg_num () * sizeof (ira_allocno_t));
3159 1.1 mrg new_pseudos_p = merged_p = false;
3160 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
3161 1.1 mrg {
3162 1.1 mrg ira_allocno_object_iterator oi;
3163 1.1 mrg ira_object_t obj;
3164 1.1 mrg
3165 1.1 mrg if (ALLOCNO_CAP_MEMBER (a) != NULL)
3166 1.1 mrg /* Caps are not in the regno allocno maps and they are never
3167 1.1 mrg will be transformed into allocnos existing after IR
3168 1.1 mrg flattening. */
3169 1.1 mrg continue;
3170 1.1 mrg FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
3171 1.1 mrg OBJECT_TOTAL_CONFLICT_HARD_REGS (obj)
3172 1.1 mrg = OBJECT_CONFLICT_HARD_REGS (obj);
3173 1.1 mrg #ifdef STACK_REGS
3174 1.1 mrg ALLOCNO_TOTAL_NO_STACK_REG_P (a) = ALLOCNO_NO_STACK_REG_P (a);
3175 1.1 mrg #endif
3176 1.1 mrg }
3177 1.1 mrg /* Fix final allocno attributes. */
3178 1.1 mrg for (i = max_regno_before_emit - 1; i >= FIRST_PSEUDO_REGISTER; i--)
3179 1.1 mrg {
3180 1.1 mrg mem_dest_p = false;
3181 1.1 mrg for (a = ira_regno_allocno_map[i];
3182 1.1 mrg a != NULL;
3183 1.1 mrg a = ALLOCNO_NEXT_REGNO_ALLOCNO (a))
3184 1.1 mrg {
3185 1.1 mrg ira_emit_data_t parent_data, data = ALLOCNO_EMIT_DATA (a);
3186 1.1 mrg
3187 1.1 mrg ira_assert (ALLOCNO_CAP_MEMBER (a) == NULL);
3188 1.1 mrg if (data->somewhere_renamed_p)
3189 1.1 mrg new_pseudos_p = true;
3190 1.1 mrg parent_a = ira_parent_allocno (a);
3191 1.1 mrg if (parent_a == NULL)
3192 1.1 mrg {
3193 1.1 mrg ALLOCNO_COPIES (a) = NULL;
3194 1.1 mrg regno_top_level_allocno_map[REGNO (data->reg)] = a;
3195 1.1 mrg continue;
3196 1.1 mrg }
3197 1.1 mrg ira_assert (ALLOCNO_CAP_MEMBER (parent_a) == NULL);
3198 1.1 mrg
3199 1.1 mrg if (data->mem_optimized_dest != NULL)
3200 1.1 mrg mem_dest_p = true;
3201 1.1 mrg parent_data = ALLOCNO_EMIT_DATA (parent_a);
3202 1.1 mrg if (REGNO (data->reg) == REGNO (parent_data->reg))
3203 1.1 mrg {
3204 1.1 mrg merge_hard_reg_conflicts (a, parent_a, true);
3205 1.1 mrg move_allocno_live_ranges (a, parent_a);
3206 1.1 mrg merged_p = true;
3207 1.1 mrg parent_data->mem_optimized_dest_p
3208 1.1 mrg = (parent_data->mem_optimized_dest_p
3209 1.1 mrg || data->mem_optimized_dest_p);
3210 1.1 mrg continue;
3211 1.1 mrg }
3212 1.1 mrg new_pseudos_p = true;
3213 1.1 mrg for (;;)
3214 1.1 mrg {
3215 1.1 mrg ALLOCNO_NREFS (parent_a) -= ALLOCNO_NREFS (a);
3216 1.1 mrg ALLOCNO_FREQ (parent_a) -= ALLOCNO_FREQ (a);
3217 1.1 mrg ALLOCNO_CALL_FREQ (parent_a) -= ALLOCNO_CALL_FREQ (a);
3218 1.1 mrg ALLOCNO_CALLS_CROSSED_NUM (parent_a)
3219 1.1 mrg -= ALLOCNO_CALLS_CROSSED_NUM (a);
3220 1.1 mrg ALLOCNO_CHEAP_CALLS_CROSSED_NUM (parent_a)
3221 1.1 mrg -= ALLOCNO_CHEAP_CALLS_CROSSED_NUM (a);
3222 1.1 mrg /* Assume that ALLOCNO_CROSSED_CALLS_ABIS and
3223 1.1 mrg ALLOCNO_CROSSED_CALLS_CLOBBERED_REGS stay the same.
3224 1.1 mrg We'd need to rebuild the IR to do better. */
3225 1.1 mrg ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (parent_a)
3226 1.1 mrg -= ALLOCNO_EXCESS_PRESSURE_POINTS_NUM (a);
3227 1.1 mrg ira_assert (ALLOCNO_CALLS_CROSSED_NUM (parent_a) >= 0
3228 1.1 mrg && ALLOCNO_NREFS (parent_a) >= 0
3229 1.1 mrg && ALLOCNO_FREQ (parent_a) >= 0);
3230 1.1 mrg aclass = ALLOCNO_CLASS (parent_a);
3231 1.1 mrg hard_regs_num = ira_class_hard_regs_num[aclass];
3232 1.1 mrg if (ALLOCNO_HARD_REG_COSTS (a) != NULL
3233 1.1 mrg && ALLOCNO_HARD_REG_COSTS (parent_a) != NULL)
3234 1.1 mrg for (j = 0; j < hard_regs_num; j++)
3235 1.1 mrg ALLOCNO_HARD_REG_COSTS (parent_a)[j]
3236 1.1 mrg -= ALLOCNO_HARD_REG_COSTS (a)[j];
3237 1.1 mrg if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) != NULL
3238 1.1 mrg && ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a) != NULL)
3239 1.1 mrg for (j = 0; j < hard_regs_num; j++)
3240 1.1 mrg ALLOCNO_CONFLICT_HARD_REG_COSTS (parent_a)[j]
3241 1.1 mrg -= ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[j];
3242 1.1 mrg ALLOCNO_CLASS_COST (parent_a)
3243 1.1 mrg -= ALLOCNO_CLASS_COST (a);
3244 1.1 mrg ALLOCNO_MEMORY_COST (parent_a) -= ALLOCNO_MEMORY_COST (a);
3245 1.1 mrg parent_a = ira_parent_allocno (parent_a);
3246 1.1 mrg if (parent_a == NULL)
3247 1.1 mrg break;
3248 1.1 mrg }
3249 1.1 mrg ALLOCNO_COPIES (a) = NULL;
3250 1.1 mrg regno_top_level_allocno_map[REGNO (data->reg)] = a;
3251 1.1 mrg }
3252 1.1 mrg if (mem_dest_p && copy_info_to_removed_store_destinations (i))
3253 1.1 mrg merged_p = true;
3254 1.1 mrg }
3255 1.1 mrg ira_assert (new_pseudos_p || ira_max_point_before_emit == ira_max_point);
3256 1.1 mrg if (merged_p || ira_max_point_before_emit != ira_max_point)
3257 1.1 mrg ira_rebuild_start_finish_chains ();
3258 1.1 mrg if (new_pseudos_p)
3259 1.1 mrg {
3260 1.1 mrg sparseset objects_live;
3261 1.1 mrg
3262 1.1 mrg /* Rebuild conflicts. */
3263 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
3264 1.1 mrg {
3265 1.1 mrg ira_allocno_object_iterator oi;
3266 1.1 mrg ira_object_t obj;
3267 1.1 mrg
3268 1.1 mrg if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
3269 1.1 mrg || ALLOCNO_CAP_MEMBER (a) != NULL)
3270 1.1 mrg continue;
3271 1.1 mrg FOR_EACH_ALLOCNO_OBJECT (a, obj, oi)
3272 1.1 mrg {
3273 1.1 mrg for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
3274 1.1 mrg ira_assert (r->object == obj);
3275 1.1 mrg clear_conflicts (obj);
3276 1.1 mrg }
3277 1.1 mrg }
3278 1.1 mrg objects_live = sparseset_alloc (ira_objects_num);
3279 1.1 mrg for (i = 0; i < ira_max_point; i++)
3280 1.1 mrg {
3281 1.1 mrg for (r = ira_start_point_ranges[i]; r != NULL; r = r->start_next)
3282 1.1 mrg {
3283 1.1 mrg ira_object_t obj = r->object;
3284 1.1 mrg
3285 1.1 mrg a = OBJECT_ALLOCNO (obj);
3286 1.1 mrg if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
3287 1.1 mrg || ALLOCNO_CAP_MEMBER (a) != NULL)
3288 1.1 mrg continue;
3289 1.1 mrg
3290 1.1 mrg aclass = ALLOCNO_CLASS (a);
3291 1.1 mrg EXECUTE_IF_SET_IN_SPARSESET (objects_live, n)
3292 1.1 mrg {
3293 1.1 mrg ira_object_t live_obj = ira_object_id_map[n];
3294 1.1 mrg ira_allocno_t live_a = OBJECT_ALLOCNO (live_obj);
3295 1.1 mrg enum reg_class live_aclass = ALLOCNO_CLASS (live_a);
3296 1.1 mrg
3297 1.1 mrg if (ira_reg_classes_intersect_p[aclass][live_aclass]
3298 1.1 mrg /* Don't set up conflict for the allocno with itself. */
3299 1.1 mrg && live_a != a)
3300 1.1 mrg ira_add_conflict (obj, live_obj);
3301 1.1 mrg }
3302 1.1 mrg sparseset_set_bit (objects_live, OBJECT_CONFLICT_ID (obj));
3303 1.1 mrg }
3304 1.1 mrg
3305 1.1 mrg for (r = ira_finish_point_ranges[i]; r != NULL; r = r->finish_next)
3306 1.1 mrg sparseset_clear_bit (objects_live, OBJECT_CONFLICT_ID (r->object));
3307 1.1 mrg }
3308 1.1 mrg sparseset_free (objects_live);
3309 1.1 mrg compress_conflict_vecs ();
3310 1.1 mrg }
3311 1.1 mrg /* Mark some copies for removing and change allocnos in the rest
3312 1.1 mrg copies. */
3313 1.1 mrg FOR_EACH_COPY (cp, ci)
3314 1.1 mrg {
3315 1.1 mrg if (ALLOCNO_CAP_MEMBER (cp->first) != NULL
3316 1.1 mrg || ALLOCNO_CAP_MEMBER (cp->second) != NULL)
3317 1.1 mrg {
3318 1.1 mrg if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
3319 1.1 mrg fprintf
3320 1.1 mrg (ira_dump_file, " Remove cp%d:%c%dr%d-%c%dr%d\n",
3321 1.1 mrg cp->num, ALLOCNO_CAP_MEMBER (cp->first) != NULL ? 'c' : 'a',
3322 1.1 mrg ALLOCNO_NUM (cp->first),
3323 1.1 mrg REGNO (allocno_emit_reg (cp->first)),
3324 1.1 mrg ALLOCNO_CAP_MEMBER (cp->second) != NULL ? 'c' : 'a',
3325 1.1 mrg ALLOCNO_NUM (cp->second),
3326 1.1 mrg REGNO (allocno_emit_reg (cp->second)));
3327 1.1 mrg cp->loop_tree_node = NULL;
3328 1.1 mrg continue;
3329 1.1 mrg }
3330 1.1 mrg first
3331 1.1 mrg = regno_top_level_allocno_map[REGNO (allocno_emit_reg (cp->first))];
3332 1.1 mrg second
3333 1.1 mrg = regno_top_level_allocno_map[REGNO (allocno_emit_reg (cp->second))];
3334 1.1 mrg node = cp->loop_tree_node;
3335 1.1 mrg if (node == NULL)
3336 1.1 mrg keep_p = true; /* It copy generated in ira-emit.cc. */
3337 1.1 mrg else
3338 1.1 mrg {
3339 1.1 mrg /* Check that the copy was not propagated from level on
3340 1.1 mrg which we will have different pseudos. */
3341 1.1 mrg node_first = node->regno_allocno_map[ALLOCNO_REGNO (cp->first)];
3342 1.1 mrg node_second = node->regno_allocno_map[ALLOCNO_REGNO (cp->second)];
3343 1.1 mrg keep_p = ((REGNO (allocno_emit_reg (first))
3344 1.1 mrg == REGNO (allocno_emit_reg (node_first)))
3345 1.1 mrg && (REGNO (allocno_emit_reg (second))
3346 1.1 mrg == REGNO (allocno_emit_reg (node_second))));
3347 1.1 mrg }
3348 1.1 mrg if (keep_p)
3349 1.1 mrg {
3350 1.1 mrg cp->loop_tree_node = ira_loop_tree_root;
3351 1.1 mrg cp->first = first;
3352 1.1 mrg cp->second = second;
3353 1.1 mrg }
3354 1.1 mrg else
3355 1.1 mrg {
3356 1.1 mrg cp->loop_tree_node = NULL;
3357 1.1 mrg if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
3358 1.1 mrg fprintf (ira_dump_file, " Remove cp%d:a%dr%d-a%dr%d\n",
3359 1.1 mrg cp->num, ALLOCNO_NUM (cp->first),
3360 1.1 mrg REGNO (allocno_emit_reg (cp->first)),
3361 1.1 mrg ALLOCNO_NUM (cp->second),
3362 1.1 mrg REGNO (allocno_emit_reg (cp->second)));
3363 1.1 mrg }
3364 1.1 mrg }
3365 1.1 mrg /* Remove unnecessary allocnos on lower levels of the loop tree. */
3366 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
3367 1.1 mrg {
3368 1.1 mrg if (a != regno_top_level_allocno_map[REGNO (allocno_emit_reg (a))]
3369 1.1 mrg || ALLOCNO_CAP_MEMBER (a) != NULL)
3370 1.1 mrg {
3371 1.1 mrg if (internal_flag_ira_verbose > 4 && ira_dump_file != NULL)
3372 1.1 mrg fprintf (ira_dump_file, " Remove a%dr%d\n",
3373 1.1 mrg ALLOCNO_NUM (a), REGNO (allocno_emit_reg (a)));
3374 1.1 mrg ira_remove_allocno_prefs (a);
3375 1.1 mrg finish_allocno (a);
3376 1.1 mrg continue;
3377 1.1 mrg }
3378 1.1 mrg ALLOCNO_LOOP_TREE_NODE (a) = ira_loop_tree_root;
3379 1.1 mrg ALLOCNO_REGNO (a) = REGNO (allocno_emit_reg (a));
3380 1.1 mrg ALLOCNO_CAP (a) = NULL;
3381 1.1 mrg /* Restore updated costs for assignments from reload. */
3382 1.1 mrg ALLOCNO_UPDATED_MEMORY_COST (a) = ALLOCNO_MEMORY_COST (a);
3383 1.1 mrg ALLOCNO_UPDATED_CLASS_COST (a) = ALLOCNO_CLASS_COST (a);
3384 1.1 mrg if (! ALLOCNO_ASSIGNED_P (a))
3385 1.1 mrg ira_free_allocno_updated_costs (a);
3386 1.1 mrg ira_assert (ALLOCNO_UPDATED_HARD_REG_COSTS (a) == NULL);
3387 1.1 mrg ira_assert (ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS (a) == NULL);
3388 1.1 mrg }
3389 1.1 mrg /* Remove unnecessary copies. */
3390 1.1 mrg FOR_EACH_COPY (cp, ci)
3391 1.1 mrg {
3392 1.1 mrg if (cp->loop_tree_node == NULL)
3393 1.1 mrg {
3394 1.1 mrg ira_copies[cp->num] = NULL;
3395 1.1 mrg finish_copy (cp);
3396 1.1 mrg continue;
3397 1.1 mrg }
3398 1.1 mrg ira_assert
3399 1.1 mrg (ALLOCNO_LOOP_TREE_NODE (cp->first) == ira_loop_tree_root
3400 1.1 mrg && ALLOCNO_LOOP_TREE_NODE (cp->second) == ira_loop_tree_root);
3401 1.1 mrg add_allocno_copy_to_list (cp);
3402 1.1 mrg swap_allocno_copy_ends_if_necessary (cp);
3403 1.1 mrg }
3404 1.1 mrg rebuild_regno_allocno_maps ();
3405 1.1 mrg if (ira_max_point != ira_max_point_before_emit)
3406 1.1 mrg ira_compress_allocno_live_ranges ();
3407 1.1 mrg ira_free (regno_top_level_allocno_map);
3408 1.1 mrg }
3409 1.1 mrg
3410 1.1 mrg
3411 1.1 mrg
3413 1.1 mrg #ifdef ENABLE_IRA_CHECKING
3414 1.1 mrg /* Check creation of all allocnos. Allocnos on lower levels should
3415 1.1 mrg have allocnos or caps on all upper levels. */
3416 1.1 mrg static void
3417 1.1 mrg check_allocno_creation (void)
3418 1.1 mrg {
3419 1.1 mrg ira_allocno_t a;
3420 1.1 mrg ira_allocno_iterator ai;
3421 1.1 mrg ira_loop_tree_node_t loop_tree_node;
3422 1.1 mrg
3423 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
3424 1.1 mrg {
3425 1.1 mrg loop_tree_node = ALLOCNO_LOOP_TREE_NODE (a);
3426 1.1 mrg ira_assert (bitmap_bit_p (loop_tree_node->all_allocnos,
3427 1.1 mrg ALLOCNO_NUM (a)));
3428 1.1 mrg if (loop_tree_node == ira_loop_tree_root)
3429 1.1 mrg continue;
3430 1.1 mrg if (ALLOCNO_CAP_MEMBER (a) != NULL)
3431 1.1 mrg ira_assert (ALLOCNO_CAP (a) != NULL);
3432 1.1 mrg else if (ALLOCNO_CAP (a) == NULL)
3433 1.1 mrg ira_assert (loop_tree_node->parent
3434 1.1 mrg ->regno_allocno_map[ALLOCNO_REGNO (a)] != NULL
3435 1.1 mrg && bitmap_bit_p (loop_tree_node->border_allocnos,
3436 1.1 mrg ALLOCNO_NUM (a)));
3437 1.1 mrg }
3438 1.1 mrg }
3439 1.1 mrg #endif
3440 1.1 mrg
3441 1.1 mrg /* Identify allocnos which prefer a register class with a single hard register.
3442 1.1 mrg Adjust ALLOCNO_CONFLICT_HARD_REG_COSTS so that conflicting allocnos are
3443 1.1 mrg less likely to use the preferred singleton register. */
3444 1.1 mrg static void
3445 1.1 mrg update_conflict_hard_reg_costs (void)
3446 1.1 mrg {
3447 1.1 mrg ira_allocno_t a;
3448 1.1 mrg ira_allocno_iterator ai;
3449 1.1 mrg int i, index, min;
3450 1.1 mrg
3451 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
3452 1.1 mrg {
3453 1.1 mrg reg_class_t aclass = ALLOCNO_CLASS (a);
3454 1.1 mrg reg_class_t pref = reg_preferred_class (ALLOCNO_REGNO (a));
3455 1.1 mrg int singleton = ira_class_singleton[pref][ALLOCNO_MODE (a)];
3456 1.1 mrg if (singleton < 0)
3457 1.1 mrg continue;
3458 1.1 mrg index = ira_class_hard_reg_index[(int) aclass][singleton];
3459 1.1 mrg if (index < 0)
3460 1.1 mrg continue;
3461 1.1 mrg if (ALLOCNO_CONFLICT_HARD_REG_COSTS (a) == NULL
3462 1.1 mrg || ALLOCNO_HARD_REG_COSTS (a) == NULL)
3463 1.1 mrg continue;
3464 1.1 mrg min = INT_MAX;
3465 1.1 mrg for (i = ira_class_hard_regs_num[(int) aclass] - 1; i >= 0; i--)
3466 1.1 mrg if (ALLOCNO_HARD_REG_COSTS (a)[i] > ALLOCNO_CLASS_COST (a)
3467 1.1 mrg && min > ALLOCNO_HARD_REG_COSTS (a)[i])
3468 1.1 mrg min = ALLOCNO_HARD_REG_COSTS (a)[i];
3469 1.1 mrg if (min == INT_MAX)
3470 1.1 mrg continue;
3471 1.1 mrg ira_allocate_and_set_costs (&ALLOCNO_CONFLICT_HARD_REG_COSTS (a),
3472 1.1 mrg aclass, 0);
3473 1.1 mrg ALLOCNO_CONFLICT_HARD_REG_COSTS (a)[index]
3474 1.1 mrg -= min - ALLOCNO_CLASS_COST (a);
3475 1.1 mrg }
3476 1.1 mrg }
3477 1.1 mrg
3478 1.1 mrg /* Create a internal representation (IR) for IRA (allocnos, copies,
3479 1.1 mrg loop tree nodes). The function returns TRUE if we generate loop
3480 1.1 mrg structure (besides nodes representing all function and the basic
3481 1.1 mrg blocks) for regional allocation. A true return means that we
3482 1.1 mrg really need to flatten IR before the reload. */
3483 1.1 mrg bool
3484 1.1 mrg ira_build (void)
3485 1.1 mrg {
3486 1.1 mrg bool loops_p;
3487 1.1 mrg
3488 1.1 mrg df_analyze ();
3489 1.1 mrg initiate_cost_vectors ();
3490 1.1 mrg initiate_allocnos ();
3491 1.1 mrg initiate_prefs ();
3492 1.1 mrg initiate_copies ();
3493 1.1 mrg create_loop_tree_nodes ();
3494 1.1 mrg form_loop_tree ();
3495 1.1 mrg create_allocnos ();
3496 1.1 mrg ira_costs ();
3497 1.1 mrg create_allocno_objects ();
3498 1.1 mrg ira_create_allocno_live_ranges ();
3499 1.1 mrg remove_unnecessary_regions (false);
3500 1.1 mrg ira_compress_allocno_live_ranges ();
3501 1.1 mrg update_bad_spill_attribute ();
3502 1.1 mrg loops_p = more_one_region_p ();
3503 1.1 mrg if (loops_p)
3504 1.1 mrg {
3505 1.1 mrg propagate_allocno_info ();
3506 1.1 mrg create_caps ();
3507 1.1 mrg }
3508 1.1 mrg ira_tune_allocno_costs ();
3509 1.1 mrg #ifdef ENABLE_IRA_CHECKING
3510 1.1 mrg check_allocno_creation ();
3511 1.1 mrg #endif
3512 1.1 mrg setup_min_max_allocno_live_range_point ();
3513 1.1 mrg sort_conflict_id_map ();
3514 1.1 mrg setup_min_max_conflict_allocno_ids ();
3515 1.1 mrg ira_build_conflicts ();
3516 1.1 mrg update_conflict_hard_reg_costs ();
3517 1.1 mrg if (! ira_conflicts_p)
3518 1.1 mrg {
3519 1.1 mrg ira_allocno_t a;
3520 1.1 mrg ira_allocno_iterator ai;
3521 1.1 mrg
3522 1.1 mrg /* Remove all regions but root one. */
3523 1.1 mrg if (loops_p)
3524 1.1 mrg {
3525 1.1 mrg remove_unnecessary_regions (true);
3526 1.1 mrg loops_p = false;
3527 1.1 mrg }
3528 1.1 mrg /* We don't save hard registers around calls for fast allocation
3529 1.1 mrg -- add caller clobbered registers as conflicting ones to
3530 1.1 mrg allocno crossing calls. */
3531 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
3532 1.1 mrg if (ALLOCNO_CALLS_CROSSED_NUM (a) != 0)
3533 1.1 mrg ior_hard_reg_conflicts (a, ira_need_caller_save_regs (a));
3534 1.1 mrg }
3535 1.1 mrg if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3536 1.1 mrg print_copies (ira_dump_file);
3537 1.1 mrg if (internal_flag_ira_verbose > 2 && ira_dump_file != NULL)
3538 1.1 mrg print_prefs (ira_dump_file);
3539 1.1 mrg if (internal_flag_ira_verbose > 0 && ira_dump_file != NULL)
3540 1.1 mrg {
3541 1.1 mrg int n, nr, nr_big;
3542 1.1 mrg ira_allocno_t a;
3543 1.1 mrg live_range_t r;
3544 1.1 mrg ira_allocno_iterator ai;
3545 1.1 mrg
3546 1.1 mrg n = 0;
3547 1.1 mrg nr = 0;
3548 1.1 mrg nr_big = 0;
3549 1.1 mrg FOR_EACH_ALLOCNO (a, ai)
3550 1.1 mrg {
3551 1.1 mrg int j, nobj = ALLOCNO_NUM_OBJECTS (a);
3552 1.1 mrg
3553 1.1 mrg if (nobj > 1)
3554 1.1 mrg nr_big++;
3555 1.1 mrg for (j = 0; j < nobj; j++)
3556 1.1 mrg {
3557 1.1 mrg ira_object_t obj = ALLOCNO_OBJECT (a, j);
3558 1.1 mrg n += OBJECT_NUM_CONFLICTS (obj);
3559 1.1 mrg for (r = OBJECT_LIVE_RANGES (obj); r != NULL; r = r->next)
3560 1.1 mrg nr++;
3561 1.1 mrg }
3562 1.1 mrg }
3563 1.1 mrg fprintf (ira_dump_file, " regions=%d, blocks=%d, points=%d\n",
3564 1.1 mrg current_loops == NULL ? 1 : number_of_loops (cfun),
3565 1.1 mrg n_basic_blocks_for_fn (cfun), ira_max_point);
3566 1.1 mrg fprintf (ira_dump_file,
3567 1.1 mrg " allocnos=%d (big %d), copies=%d, conflicts=%d, ranges=%d\n",
3568 1.1 mrg ira_allocnos_num, nr_big, ira_copies_num, n, nr);
3569 }
3570 return loops_p;
3571 }
3572
3573 /* Release the data created by function ira_build. */
3574 void
3575 ira_destroy (void)
3576 {
3577 finish_loop_tree_nodes ();
3578 finish_prefs ();
3579 finish_copies ();
3580 finish_allocnos ();
3581 finish_cost_vectors ();
3582 ira_finish_allocno_live_ranges ();
3583 }
3584