2008-05-02 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / ira-int.h
blobcf63dde6e2cdd76f15678b984cb28a99aed3da4a
1 /* Integrated Register Allocator (IRA) intercommunication header file.
2 Copyright (C) 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Vladimir Makarov <vmakarov@redhat.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "cfgloop.h"
23 #include "ira.h"
24 #include "alloc-pool.h"
26 #ifdef ENABLE_CHECKING
27 #define ENABLE_IRA_CHECKING
28 #endif
30 #ifdef ENABLE_IRA_CHECKING
31 #define ira_assert(c) gcc_assert (c)
32 #else
33 #define ira_assert(c)
34 #endif
36 /* Compute register frequency from edge frequency FREQ. It is
37 analogous to REG_FREQ_FROM_BB. When optimizing for size, or
38 profile driven feedback is available and the function is never
39 executed, frequency is always equivalent. Otherwise rescale the
40 edge frequency. */
41 #define REG_FREQ_FROM_EDGE_FREQ(freq) \
42 (optimize_size || (flag_branch_probabilities && !ENTRY_BLOCK_PTR->count) \
43 ? REG_FREQ_MAX : (freq * REG_FREQ_MAX / BB_FREQ_MAX) \
44 ? (freq * REG_FREQ_MAX / BB_FREQ_MAX) : 1)
46 /* All natural loops. */
47 extern struct loops ira_loops;
49 /* A modified value of flag `-fira-verbose' used internally. */
50 extern int internal_flag_ira_verbose;
52 /* Dump file of the allocator if it is not NULL. */
53 extern FILE *ira_dump_file;
55 /* Typedefs for pointers to allocno live range, allocno, and copy of
56 allocnos. */
57 typedef struct allocno_live_range *allocno_live_range_t;
58 typedef struct allocno *allocno_t;
59 typedef struct allocno_copy *copy_t;
61 /* Typedef for pointer to the subsequent structure. */
62 typedef struct loop_tree_node *loop_tree_node_t;
64 /* In general case, IRA is a regional allocator. The regions are
65 nested and form a tree. Currently regions are natural loops. The
66 following structure describes loop tree node (representing basic
67 block or loop). We need such tree because the loop tree from
68 cfgloop.h is not convenient for the optimization: basic blocks are
69 not a part of the tree from cfgloop.h. We also use the nodes for
70 storing additional information about basic blocks/loops for the
71 register allocation purposes. */
72 struct loop_tree_node
74 /* The node represents basic block if children == NULL. */
75 basic_block bb; /* NULL for loop. */
76 struct loop *loop; /* NULL for BB. */
77 /* The next node on the same father. */
78 loop_tree_node_t next;
79 /* The first node immediately inside the node. */
80 loop_tree_node_t children;
81 /* The node immediately containing given node. */
82 loop_tree_node_t father;
84 /* Loop level in range [0, ira_loop_tree_height). */
85 int level;
87 /* All the following members are defined only for nodes representing
88 loops. */
90 /* Allocnos in the loop corresponding to their regnos. If it is
91 NULL the loop does not form a separate register allocation region
92 (e.g. because it has abnormal enter/exit edges and we can not put
93 code for register shuffling on the edges if a different
94 allocation is used for a pseudo-register on different sides of
95 the edges). Caps are not in the map (remember we can have more
96 one cap with the same regno in a region). */
97 allocno_t *regno_allocno_map;
99 /* Maximal register pressure inside loop for given register class
100 (defined only for the cover classes). */
101 int reg_pressure[N_REG_CLASSES];
103 /* Numbers of allocnos referred in the loop node. */
104 bitmap mentioned_allocnos;
106 /* Regnos of pseudos modified in the loop node (including its
107 subloops). */
108 bitmap modified_regnos;
110 /* Numbers of allocnos living at the loop borders. */
111 bitmap border_allocnos;
113 /* Numbers of copies referred in the corresponding loop. */
114 bitmap local_copies;
117 /* The root of the loop tree corresponding to the all function. */
118 extern loop_tree_node_t ira_loop_tree_root;
120 /* Height of the loop tree. */
121 extern int ira_loop_tree_height;
123 /* All nodes representing basic blocks are referred through the
124 following array. We can not use basic block member `aux' for this
125 because it is used for insertion of insns on edges. */
126 extern loop_tree_node_t ira_bb_nodes;
128 /* Two access macros to the nodes representing basic blocks. */
129 #if defined ENABLE_IRA_CHECKING && (GCC_VERSION >= 2007)
130 #define IRA_BB_NODE_BY_INDEX(index) __extension__ \
131 (({ loop_tree_node_t _node = (&ira_bb_nodes[index]); \
132 if (_node->children != NULL || _node->loop != NULL || _node->bb == NULL)\
134 fprintf (stderr, \
135 "\n%s: %d: error in %s: it is not a block node\n", \
136 __FILE__, __LINE__, __FUNCTION__); \
137 gcc_unreachable (); \
139 _node; }))
140 #else
141 #define IRA_BB_NODE_BY_INDEX(index) (&ira_bb_nodes[index])
142 #endif
144 #define IRA_BB_NODE(bb) IRA_BB_NODE_BY_INDEX ((bb)->index)
146 /* All nodes representing loops are referred through the following
147 array. */
148 extern loop_tree_node_t ira_loop_nodes;
150 /* Two access macros to the nodes representing loops. */
151 #if defined ENABLE_IRA_CHECKING && (GCC_VERSION >= 2007)
152 #define IRA_LOOP_NODE_BY_INDEX(index) __extension__ \
153 (({ loop_tree_node_t const _node = (&ira_loop_nodes[index]);\
154 if (_node->children == NULL || _node->bb != NULL || _node->loop == NULL)\
156 fprintf (stderr, \
157 "\n%s: %d: error in %s: it is not a loop node\n", \
158 __FILE__, __LINE__, __FUNCTION__); \
159 gcc_unreachable (); \
161 _node; }))
162 #else
163 #define IRA_LOOP_NODE_BY_INDEX(index) (&ira_loop_nodes[index])
164 #endif
166 #define IRA_LOOP_NODE(loop) IRA_LOOP_NODE_BY_INDEX ((loop)->num)
170 /* The structure describes program points where a given allocno lives.
171 To save memory we store allocno conflicts only for the same cover
172 class allocnos which is enough to assign hard registers. To find
173 conflicts for other allocnos (e.g. to assign stack memory slot) we
174 use the live ranges. If the live ranges of two allocnos are
175 intersected, the allocnos are in conflict. */
176 struct allocno_live_range
178 /* Allocno whose live range is described by given structure. */
179 allocno_t allocno;
180 /* Program point range. */
181 int start, finish;
182 /* Next structure describing program points where the allocno
183 lives. */
184 allocno_live_range_t next;
185 /* Pointer to structures with the same start/finish. */
186 allocno_live_range_t start_next, finish_next;
189 /* Program points are enumerated by number from range 0..MAX_POINT-1.
190 There are approximately two times more program points than insns.
191 One program points correspond points between subsequent insns and
192 other ones correspond to points after usage of input operands but
193 before setting the output operands in insns. */
194 extern int max_point;
196 /* Arrays of size MAX_POINT mapping a program point to the allocno
197 live ranges with given start/finish point. */
198 extern allocno_live_range_t *start_point_ranges, *finish_point_ranges;
200 /* A structure representing an allocno (allocation entity). Allocno
201 represents a pseudo-register in an allocation region. If
202 pseudo-register does not live in a region but it lives in the
203 nested regions, it is represented in the region by special allocno
204 called *cap*. There may be more one cap representing the same
205 pseudo-register in region. It means that the corresponding
206 pseudo-register lives in more one non-intersected subregion. */
207 struct allocno
209 /* The allocno order number starting with 0. Each allocno has an
210 unique number and the number is never changed for the
211 allocno. */
212 int num;
213 /* Regno for allocno or cap. */
214 int regno;
215 /* Mode of the allocno which is the mode of the corresponding
216 pseudo-register. */
217 enum machine_mode mode;
218 /* Final rtx representation of the allocno. */
219 rtx reg;
220 /* Hard register assigned to given allocno. Negative value means
221 that memory was allocated to the allocno. During the reload,
222 spilled allocno has value equal to the corresponding stack slot
223 number (0, ...) - 2. Value -1 is used for allocnos spilled by the
224 reload (at this point pseudo-register has only one allocno) which
225 did not get stack slot yet. */
226 int hard_regno;
227 /* Allocnos with the same regno are linked by the following member.
228 Allocnos corresponding to inner loops are first in the list (it
229 corresponds to depth-first traverse of the loops). */
230 allocno_t next_regno_allocno;
231 /* There may be different allocnos with the same regno in different
232 regions. Allocnos are bound to the corresponding loop tree node.
233 Pseudo-register may have only one regular allocno with given loop
234 tree node but more than one cap (see comments above). */
235 loop_tree_node_t loop_tree_node;
236 /* Accumulated usage references of the allocno. Here and below,
237 word 'accumulated' means info for given region and all nested
238 subregions. In this case, 'accumulated' means sum of references
239 of the corresponding pseudo-register in this region and in all
240 nested subregions recursively. */
241 int nrefs;
242 /* Accumulated frequency of usage of the allocno. */
243 int freq;
244 /* Register class which should be used for allocation for given
245 allocno. NO_REGS means that we should use memory. */
246 enum reg_class cover_class;
247 /* Minimal accumulated cost of usage register of the cover class for
248 the allocno. */
249 int cover_class_cost;
250 /* Minimal accumulated, and updated costs of memory for the allocno.
251 At the allocation start, the original and updated costs are
252 equal. The updated cost may be changed after finishing
253 allocation in a region and starting allocation in a subregion.
254 The change reflects the cost of spill/restore code on the
255 subregion border if we assign memory to the pseudo in the
256 subregion. */
257 int memory_cost, updated_memory_cost;
258 /* Accumulated number of points where the allocno lives and there is
259 excess pressure for its class. Excess pressure for a register
260 class at some point means that there are more allocnos of given
261 register class living at the point than number of hard-registers
262 of the class available for the allocation. */
263 int excess_pressure_points_num;
264 /* Copies to other non-conflicting allocnos. The copies can
265 represent move insn or potential move insn usually because of two
266 operand insn constraints. */
267 copy_t allocno_copies;
268 /* It is a allocno (cap) representing given allocno on upper loop tree
269 level. */
270 allocno_t cap;
271 /* It is a link to allocno (cap) on lower loop level represented by
272 given cap. Null if given allocno is not a cap. */
273 allocno_t cap_member;
274 /* Coalesced allocnos form a cyclic list. One allocno given by
275 FIRST_COALESCED_ALLOCNO represents all coalesced allocnos. The
276 list is chained by NEXT_COALESCED_ALLOCNO. */
277 allocno_t first_coalesced_allocno;
278 allocno_t next_coalesced_allocno;
279 /* Pointer to structures describing at what program point the
280 allocno lives. We always maintain the list in such way that *the
281 ranges in the list are not intersected and ordered by decreasing
282 their program points*. */
283 allocno_live_range_t live_ranges;
284 /* Before building conflicts the two member values are
285 correspondingly minimal and maximal points of the accumulated
286 allocno live ranges. After building conflicts the values are
287 correspondingly minimal and maximal conflict ids of allocnos with
288 which given allocno can conflict. */
289 int min, max;
290 /* The unique member value represents given allocno in conflict bit
291 vectors. */
292 int conflict_id;
293 /* Vector of accumulated conflicting allocnos with NULL end marker
294 (if CONFLICT_VEC_P is true) or conflict bit vector otherwise.
295 Only allocnos with the same cover class are in the vector or in
296 the bit vector. */
297 void *conflict_allocno_array;
298 /* Allocated size of the previous array. */
299 unsigned int conflict_allocno_array_size;
300 /* Number of accumulated conflicts in the vector of conflicting
301 allocnos. */
302 int conflict_allocnos_num;
303 /* Initial and accumulated hard registers conflicting with this
304 allocno and as a consequences can not be assigned to the allocno.
305 All non-allocatable hard regs and hard regs of cover classes
306 different from given allocno one are included in the sets. */
307 HARD_REG_SET conflict_hard_regs, total_conflict_hard_regs;
308 /* Accumulated frequency of calls which given allocno
309 intersects. */
310 int call_freq;
311 /* Start index of calls intersected by the allocno in array
312 regno_calls[regno]. */
313 int calls_crossed_start;
314 /* Length of the previous array (number of the intersected calls). */
315 int calls_crossed_num;
316 /* Non NULL if we remove restoring value from given allocno to
317 MEM_OPTIMIZED_DEST at loop exit (see ira-emit.c) because the
318 allocno value is not changed inside the loop. */
319 allocno_t mem_optimized_dest;
320 /* TRUE if the allocno assigned to memory was a destination of
321 removed move (see ira-emit.c) at loop exit because the value of
322 the corresponding pseudo-register is not changed inside the
323 loop. */
324 unsigned int mem_optimized_dest_p : 1;
325 /* TRUE if the corresponding pseudo-register has disjoint live
326 ranges and the other allocnos of the pseudo-register except this
327 one changed REG. */
328 unsigned int somewhere_renamed_p : 1;
329 /* TRUE if allocno with the same REGNO in a subregion has been
330 renamed, in other words, got a new pseudo-register. */
331 unsigned int child_renamed_p : 1;
332 /* During the reload, value TRUE means that we should not reassign a
333 hard register to the allocno got memory earlier. It is set up
334 when we removed memory-memory move insn before each iteration of
335 the reload. */
336 unsigned int dont_reassign_p : 1;
337 #ifdef STACK_REGS
338 /* Set to TRUE if allocno can't be assigned to the stack hard
339 register correspondingly in this region and area including the
340 region and all its subregions recursively. */
341 unsigned int no_stack_reg_p : 1, total_no_stack_reg_p : 1;
342 #endif
343 /* TRUE value means that the allocno was not removed yet from the
344 conflicting graph during colouring. */
345 unsigned int in_graph_p : 1;
346 /* TRUE if a hard register or memory has been assigned to the
347 allocno. */
348 unsigned int assigned_p : 1;
349 /* TRUE if it is put on the stack to make other allocnos
350 colorable. */
351 unsigned int may_be_spilled_p : 1;
352 /* TRUE if conflicts for given allocno are represented by vector of
353 pointers to the conflicting allocnos. Otherwise, we use a bit
354 vector where a bit with given index represents allocno with the
355 same number. */
356 unsigned int conflict_vec_p : 1;
357 /* Array of usage costs (accumulated and the one updated during
358 coloring) for each hard register of the allocno cover class. The
359 member value can be NULL if all costs are the same and equal to
360 COVER_CLASS_COST. For example, the costs of two different hard
361 registers can be different if one hard register is callee-saved
362 and another one is callee-used and the allocno lives through
363 calls. Another example can be case when for some insn the
364 corresponding pseudo-register value should be put in specific
365 register class (e.g. AREG for x86) which is a strict subset of
366 the allocno cover class (GENERAL_REGS for x86). We have updated
367 costs to reflect the situation when the usage cost of a hard
368 register is decreased because the allocno is connected to another
369 allocno by a copy and the another allocno has been assigned to
370 the hard register. */
371 int *hard_reg_costs, *updated_hard_reg_costs;
372 /* Array of decreasing costs (accumulated and the one updated during
373 coloring) for allocnos conflicting with given allocno for hard
374 regno of the allocno cover class. The member value can be NULL
375 if all costs are the same. These costs are used to reflect
376 preferences of other allocnos not assigned yet during assigning
377 to given allocno. */
378 int *conflict_hard_reg_costs, *updated_conflict_hard_reg_costs;
379 /* Number of the same cover class allocnos with TRUE in_graph_p
380 value and conflicting with given allocno during each point of
381 graph coloring. */
382 int left_conflicts_num;
383 /* Number of hard registers of the allocno cover class really
384 available for the allocno allocation. */
385 int available_regs_num;
386 /* Allocnos in a bucket (used in coloring) chained by the following
387 two members. */
388 allocno_t next_bucket_allocno;
389 allocno_t prev_bucket_allocno;
390 /* Used for temporary purposes. */
391 int temp;
394 /* All members of the allocno structures should be accessed only
395 through the following macros. */
396 #define ALLOCNO_NUM(A) ((A)->num)
397 #define ALLOCNO_REGNO(A) ((A)->regno)
398 #define ALLOCNO_REG(A) ((A)->reg)
399 #define ALLOCNO_NEXT_REGNO_ALLOCNO(A) ((A)->next_regno_allocno)
400 #define ALLOCNO_LOOP_TREE_NODE(A) ((A)->loop_tree_node)
401 #define ALLOCNO_CAP(A) ((A)->cap)
402 #define ALLOCNO_CAP_MEMBER(A) ((A)->cap_member)
403 #define ALLOCNO_CONFLICT_ALLOCNO_ARRAY(A) ((A)->conflict_allocno_array)
404 #define ALLOCNO_CONFLICT_ALLOCNO_ARRAY_SIZE(A) \
405 ((A)->conflict_allocno_array_size)
406 #define ALLOCNO_CONFLICT_ALLOCNOS_NUM(A) \
407 ((A)->conflict_allocnos_num)
408 #define ALLOCNO_CONFLICT_HARD_REGS(A) ((A)->conflict_hard_regs)
409 #define ALLOCNO_TOTAL_CONFLICT_HARD_REGS(A) ((A)->total_conflict_hard_regs)
410 #define ALLOCNO_NREFS(A) ((A)->nrefs)
411 #define ALLOCNO_FREQ(A) ((A)->freq)
412 #define ALLOCNO_HARD_REGNO(A) ((A)->hard_regno)
413 #define ALLOCNO_CALL_FREQ(A) ((A)->call_freq)
414 #define ALLOCNO_CALLS_CROSSED_START(A) ((A)->calls_crossed_start)
415 #define ALLOCNO_CALLS_CROSSED_NUM(A) ((A)->calls_crossed_num)
416 #define ALLOCNO_MEM_OPTIMIZED_DEST(A) ((A)->mem_optimized_dest)
417 #define ALLOCNO_MEM_OPTIMIZED_DEST_P(A) ((A)->mem_optimized_dest_p)
418 #define ALLOCNO_SOMEWHERE_RENAMED_P(A) ((A)->somewhere_renamed_p)
419 #define ALLOCNO_CHILD_RENAMED_P(A) ((A)->child_renamed_p)
420 #define ALLOCNO_DONT_REASSIGN_P(A) ((A)->dont_reassign_p)
421 #ifdef STACK_REGS
422 #define ALLOCNO_NO_STACK_REG_P(A) ((A)->no_stack_reg_p)
423 #define ALLOCNO_TOTAL_NO_STACK_REG_P(A) ((A)->total_no_stack_reg_p)
424 #endif
425 #define ALLOCNO_IN_GRAPH_P(A) ((A)->in_graph_p)
426 #define ALLOCNO_ASSIGNED_P(A) ((A)->assigned_p)
427 #define ALLOCNO_MAY_BE_SPILLED_P(A) ((A)->may_be_spilled_p)
428 #define ALLOCNO_CONFLICT_VEC_P(A) ((A)->conflict_vec_p)
429 #define ALLOCNO_MODE(A) ((A)->mode)
430 #define ALLOCNO_COPIES(A) ((A)->allocno_copies)
431 #define ALLOCNO_HARD_REG_COSTS(A) ((A)->hard_reg_costs)
432 #define ALLOCNO_UPDATED_HARD_REG_COSTS(A) ((A)->updated_hard_reg_costs)
433 #define ALLOCNO_CONFLICT_HARD_REG_COSTS(A) \
434 ((A)->conflict_hard_reg_costs)
435 #define ALLOCNO_UPDATED_CONFLICT_HARD_REG_COSTS(A) \
436 ((A)->updated_conflict_hard_reg_costs)
437 #define ALLOCNO_LEFT_CONFLICTS_NUM(A) ((A)->left_conflicts_num)
438 #define ALLOCNO_COVER_CLASS(A) ((A)->cover_class)
439 #define ALLOCNO_COVER_CLASS_COST(A) ((A)->cover_class_cost)
440 #define ALLOCNO_MEMORY_COST(A) ((A)->memory_cost)
441 #define ALLOCNO_UPDATED_MEMORY_COST(A) ((A)->updated_memory_cost)
442 #define ALLOCNO_EXCESS_PRESSURE_POINTS_NUM(A) ((A)->excess_pressure_points_num)
443 #define ALLOCNO_AVAILABLE_REGS_NUM(A) ((A)->available_regs_num)
444 #define ALLOCNO_NEXT_BUCKET_ALLOCNO(A) ((A)->next_bucket_allocno)
445 #define ALLOCNO_PREV_BUCKET_ALLOCNO(A) ((A)->prev_bucket_allocno)
446 #define ALLOCNO_TEMP(A) ((A)->temp)
447 #define ALLOCNO_FIRST_COALESCED_ALLOCNO(A) ((A)->first_coalesced_allocno)
448 #define ALLOCNO_NEXT_COALESCED_ALLOCNO(A) ((A)->next_coalesced_allocno)
449 #define ALLOCNO_LIVE_RANGES(A) ((A)->live_ranges)
450 #define ALLOCNO_MIN(A) ((A)->min)
451 #define ALLOCNO_MAX(A) ((A)->max)
452 #define ALLOCNO_CONFLICT_ID(A) ((A)->conflict_id)
454 /* Map regno -> allocnos with given regno (see comments for
455 allocno member `next_regno_allocno'). */
456 extern allocno_t *regno_allocno_map;
458 /* Array of references to all allocnos. The order number of the
459 allocno corresponds to the index in the array. Removed allocnos
460 have NULL element value. */
461 extern allocno_t *allocnos;
463 /* Sizes of the previous array. */
464 extern int allocnos_num;
466 /* Map conflict id -> allocno with given conflict id (see comments for
467 allocno member `conflict_id'). */
468 extern allocno_t *conflict_id_allocno_map;
470 /* The following structure represents a copy of two allocnos. The
471 copies represent move insns or potential move insns usually because
472 of two operand insn constraints. */
473 struct allocno_copy
475 /* The unique order number of the copy node starting with 0. */
476 int num;
477 /* Allocnos connected by the copy. The first allocno should have
478 smaller order number than the second one. */
479 allocno_t first, second;
480 /* Execution frequency of the copy. */
481 int freq;
482 /* It is an insn which is an origin of the copy. It may be a move
483 insn or insn whose operand should be the same as the result
484 (2-operand insns). To remove register shuffle, we create copies
485 between allocno which is output of an insn and allocno becoming
486 dead in the insn. The member value for the copy created to
487 remove register shuffle is NULL and the frequency is smaller than
488 the corresponding insn execution frequency. */
489 rtx insn;
490 /* All copies with the same allocno as FIRST are linked by the two
491 following members. */
492 copy_t prev_first_allocno_copy, next_first_allocno_copy;
493 /* All copies with the same allocno as SECOND are linked by the two
494 following members. */
495 copy_t prev_second_allocno_copy, next_second_allocno_copy;
496 /* Region from which given copy is originated. */
497 loop_tree_node_t loop_tree_node;
500 /* Array of references to all copies. The order number of the copy
501 corresponds to the index in the array. Removed copies have NULL
502 element value. */
503 extern copy_t *copies;
505 /* Size of the previous array. */
506 extern int copies_num;
508 /* The following structure describes a stack slot used for spilled
509 pseudo-registers. */
510 struct spilled_reg_stack_slot
512 /* pseudo-registers assigned to the stack slot. */
513 regset_head spilled_regs;
514 /* RTL representation of the stack slot. */
515 rtx mem;
516 /* Size of the stack slot. */
517 unsigned int width;
520 /* The number of elements in the following array. */
521 extern int spilled_reg_stack_slots_num;
523 /* The following array contains info about spilled pseudo-registers
524 stack slots used in current function so far. */
525 extern struct spilled_reg_stack_slot *spilled_reg_stack_slots;
527 /* Correspondingly overall cost of the allocation, cost of the
528 allocnos assigned to hard-registers, cost of the allocnos assigned
529 to memory, cost of loads, stores and register move insns generated
530 for pseudo-register live range splitting (see ira-emit.c). */
531 extern int overall_cost;
532 extern int reg_cost, mem_cost;
533 extern int load_cost, store_cost, shuffle_cost;
534 extern int move_loops_num, additional_jumps_num;
536 /* Map: register class x machine mode -> number of hard registers of
537 given class needed to store value of given mode. If the number for
538 some hard-registers of the register class is different, the size
539 will be negative. */
540 extern int reg_class_nregs[N_REG_CLASSES][MAX_MACHINE_MODE];
542 /* Maximal value of the previous array elements. */
543 extern int max_nregs;
545 /* The number of bits in each element of array used to implement a bit
546 vector of allocnos and what type that element has. We use the
547 largest integer format on the host machine. */
548 #define INT_BITS HOST_BITS_PER_WIDE_INT
549 #define INT_TYPE HOST_WIDE_INT
551 /* Set, clear or test bit number I in R, a bit vector of elements with
552 minimal index and maximal index equal correspondingly to MIN and
553 MAX. */
554 #if defined ENABLE_IRA_CHECKING && (GCC_VERSION >= 2007)
556 #define SET_ALLOCNO_SET_BIT(R, I, MIN, MAX) __extension__ \
557 (({ int _min = (MIN), _max = (MAX), _i = (I); \
558 if (_i < _min || _i > _max) \
560 fprintf (stderr, \
561 "\n%s: %d: error in %s: %d not in range [%d,%d]\n", \
562 __FILE__, __LINE__, __FUNCTION__, _i, _min, _max); \
563 gcc_unreachable (); \
565 ((R)[(unsigned) (_i - _min) / INT_BITS] \
566 |= ((INT_TYPE) 1 << ((unsigned) (_i - _min) % INT_BITS))); }))
569 #define CLEAR_ALLOCNO_SET_BIT(R, I, MIN, MAX) __extension__ \
570 (({ int _min = (MIN), _max = (MAX), _i = (I); \
571 if (_i < _min || _i > _max) \
573 fprintf (stderr, \
574 "\n%s: %d: error in %s: %d not in range [%d,%d]\n", \
575 __FILE__, __LINE__, __FUNCTION__, _i, _min, _max); \
576 gcc_unreachable (); \
578 ((R)[(unsigned) (_i - _min) / INT_BITS] \
579 &= ~((INT_TYPE) 1 << ((unsigned) (_i - _min) % INT_BITS))); }))
581 #define TEST_ALLOCNO_SET_BIT(R, I, MIN, MAX) __extension__ \
582 (({ int _min = (MIN), _max = (MAX), _i = (I); \
583 if (_i < _min || _i > _max) \
585 fprintf (stderr, \
586 "\n%s: %d: error in %s: %d not in range [%d,%d]\n", \
587 __FILE__, __LINE__, __FUNCTION__, _i, _min, _max); \
588 gcc_unreachable (); \
590 ((R)[(unsigned) (_i - _min) / INT_BITS] \
591 & ((INT_TYPE) 1 << ((unsigned) (_i - _min) % INT_BITS))); }))
593 #else
595 #define SET_ALLOCNO_SET_BIT(R, I, MIN, MAX) \
596 ((R)[(unsigned) ((I) - (MIN)) / INT_BITS] \
597 |= ((INT_TYPE) 1 << ((unsigned) ((I) - (MIN)) % INT_BITS)))
599 #define CLEAR_ALLOCNO_SET_BIT(R, I, MIN, MAX) \
600 ((R)[(unsigned) ((I) - (MIN)) / INT_BITS] \
601 &= ~((INT_TYPE) 1 << ((unsigned) ((I) - (MIN)) % INT_BITS)))
603 #define TEST_ALLOCNO_SET_BIT(R, I, MIN, MAX) \
604 ((R)[(unsigned) ((I) - (MIN)) / INT_BITS] \
605 & ((INT_TYPE) 1 << ((unsigned) ((I) - (MIN)) % INT_BITS)))
607 #endif
609 /* The iterator for allocno set implemented ed as allocno bit
610 vector. */
611 typedef struct {
613 /* Array containing the allocno bit vector. */
614 INT_TYPE *vec;
616 /* The number of the current element in the vector. */
617 unsigned int word_num;
619 /* The number of bits in the bit vector. */
620 unsigned int nel;
622 /* The current bit index of the bit vector. */
623 unsigned int bit_num;
625 /* Index corresponding to the 1st bit of the bit vector. */
626 int start_val;
628 /* The word of the bit vector currently visited. */
629 unsigned INT_TYPE word;
630 } allocno_set_iterator;
632 /* Initialize the iterator I for allocnos bit vector VEC containing
633 minimal and maximal values MIN and MAX. */
634 static inline void
635 allocno_set_iter_init (allocno_set_iterator *i, INT_TYPE *vec, int min, int max)
637 i->vec = vec;
638 i->word_num = 0;
639 i->nel = max < min ? 0 : max - min + 1;
640 i->start_val = min;
641 i->bit_num = 0;
642 i->word = i->nel == 0 ? 0 : vec[0];
645 /* Return TRUE if we have more allocnos to visit, in which case *N is
646 set to the allocno number to be visited. Otherwise, return
647 FALSE. */
648 static inline int
649 allocno_set_iter_cond (allocno_set_iterator *i, int *n)
651 /* Skip words that are zeros. */
652 for (; i->word == 0; i->word = i->vec[i->word_num])
654 i->word_num++;
655 i->bit_num = i->word_num * INT_BITS;
657 /* If we have reached the end, break. */
658 if (i->bit_num >= i->nel)
659 return FALSE;
662 /* Skip bits that are zero. */
663 for (; (i->word & 1) == 0; i->word >>= 1)
664 i->bit_num++;
666 *n = (int) i->bit_num + i->start_val;
668 return TRUE;
671 /* Advance to the next allocno in the set. */
672 static inline void
673 allocno_set_iter_next (allocno_set_iterator *i)
675 i->word >>= 1;
676 i->bit_num++;
679 /* Loop over all elements of allocno set given by bit vector VEC and
680 their minimal and maximal values MIN and MAX. In each iteration, N
681 is set to the number of next allocno. ITER is an instance of
682 allocno_set_iterator used to iterate the allocnos in the set. */
683 #define FOR_EACH_ALLOCNO_IN_SET(VEC, MIN, MAX, N, ITER) \
684 for (allocno_set_iter_init (&(ITER), (VEC), (MIN), (MAX)); \
685 allocno_set_iter_cond (&(ITER), &(N)); \
686 allocno_set_iter_next (&(ITER)))
688 /* ira.c: */
690 /* Hard regsets whose all bits are correspondingly zero or one. */
691 extern HARD_REG_SET zero_hard_reg_set;
692 extern HARD_REG_SET one_hard_reg_set;
694 /* Map: hard regs X modes -> set of hard registers for storing value
695 of given mode starting with given hard register. */
696 extern HARD_REG_SET reg_mode_hard_regset
697 [FIRST_PSEUDO_REGISTER][NUM_MACHINE_MODES];
699 /* Arrays analogous to macros MEMORY_MOVE_COST and
700 REGISTER_MOVE_COST. */
701 extern short memory_move_cost[MAX_MACHINE_MODE][N_REG_CLASSES][2];
702 extern move_table *register_move_cost[MAX_MACHINE_MODE];
704 /* Similar to may_move_in_cost but it is calculated in IRA instead of
705 regclass. Another difference we take only available hard registers
706 into account to figure out that one register class is a subset of
707 the another one. */
708 extern move_table *register_may_move_in_cost[MAX_MACHINE_MODE];
710 /* Similar to may_move_out_cost but it is calculated in IRA instead of
711 regclass. Another difference we take only available hard registers
712 into account to figure out that one register class is a subset of
713 the another one. */
714 extern move_table *register_may_move_out_cost[MAX_MACHINE_MODE];
716 /* Register class subset relation: TRUE if the first class is a subset
717 of the second one considering only hard registers available for the
718 allocation. */
719 extern int class_subset_p[N_REG_CLASSES][N_REG_CLASSES];
721 /* Array of number of hard registers of given class which are
722 available for the allocation. The order is defined by the
723 allocation order. */
724 extern short class_hard_regs[N_REG_CLASSES][FIRST_PSEUDO_REGISTER];
726 /* The number of elements of the above array for given register
727 class. */
728 extern int class_hard_regs_num[N_REG_CLASSES];
730 /* Index (in class_hard_regs) for given register class and hard
731 register (in general case a hard register can belong to several
732 register classes). The index is negative for hard registers
733 unavailable for the allocation. */
734 extern short class_hard_reg_index[N_REG_CLASSES][FIRST_PSEUDO_REGISTER];
736 /* Function specific hard registers can not be used for the register
737 allocation. */
738 extern HARD_REG_SET no_alloc_regs;
740 /* Number of given class hard registers available for the register
741 allocation for given classes. */
742 extern int available_class_regs[N_REG_CLASSES];
744 /* Array whose values are hard regset of hard registers available for
745 the allocation of given register class whose HARD_REGNO_MODE_OK
746 values for given mode are zero. */
747 extern HARD_REG_SET prohibited_class_mode_regs
748 [N_REG_CLASSES][NUM_MACHINE_MODES];
750 /* Array whose values are hard regset of hard registers for which
751 move of the hard register in given mode into itself is
752 prohibited. */
753 extern HARD_REG_SET prohibited_mode_move_regs[NUM_MACHINE_MODES];
755 /* Number of cover classes. Cover classes is non-intersected register
756 classes containing all hard-registers available for the
757 allocation. */
758 extern int reg_class_cover_size;
760 /* The array containing cover classes (see also comments for macro
761 IRA_COVER_CLASSES). Only first REG_CLASS_COVER_SIZE elements are
762 used for this. */
763 extern enum reg_class reg_class_cover[N_REG_CLASSES];
765 /* The value is number of elements in the subsequent array. */
766 extern int important_classes_num;
768 /* The array containing non-empty classes (including non-empty cover
769 classes) which are subclasses of cover classes. Such classes is
770 important for calculation of the hard register usage costs. */
771 extern enum reg_class important_classes[N_REG_CLASSES];
773 /* The array containing indexes of important classes in the previous
774 array. The array elements are defined only for important
775 classes. */
776 extern int important_class_nums[N_REG_CLASSES];
778 /* Map of all register classes to corresponding cover class containing
779 the given class. If given class is not a subset of a cover class,
780 we translate it into the cheapest cover class. */
781 extern enum reg_class class_translate[N_REG_CLASSES];
783 /* The biggest important class inside of intersection of the two
784 classes (that is calculated taking only hard registers available
785 for allocation into account). If the both classes contain no hard
786 registers available for allocation, the value is calculated with
787 taking all hard-registers including fixed ones into account. */
788 extern enum reg_class reg_class_intersect[N_REG_CLASSES][N_REG_CLASSES];
790 /* The biggest important class inside of union of the two classes
791 (that is calculated taking only hard registers available for
792 allocation into account). If the both classes contain no hard
793 registers available for allocation, the value is calculated with
794 taking all hard-registers including fixed ones into account. In
795 other words, the value is the corresponding reg_class_subunion
796 value. */
797 extern enum reg_class reg_class_union[N_REG_CLASSES][N_REG_CLASSES];
799 extern void set_non_alloc_regs (int);
800 extern void *ira_allocate (size_t);
801 extern void *ira_reallocate (void *, size_t);
802 extern void ira_free (void *addr);
803 extern bitmap ira_allocate_bitmap (void);
804 extern void ira_free_bitmap (bitmap);
805 extern void print_disposition (FILE *);
806 extern void debug_disposition (void);
807 extern void debug_class_cover (void);
808 extern void init_register_move_cost (enum machine_mode);
810 /* The length of the two following arrays. */
811 extern int reg_equiv_len;
813 /* The element value is TRUE if the corresponding regno value is
814 invariant. */
815 extern int *reg_equiv_invariant_p;
817 /* The element value is equiv constant of given pseudo-register or
818 NULL_RTX. */
819 extern rtx *reg_equiv_const;
821 /* ira-build.c */
823 /* The current loop tree node and its regno allocno map. */
824 extern loop_tree_node_t ira_curr_loop_tree_node;
825 extern allocno_t *ira_curr_regno_allocno_map;
827 /* Array of vectors containing calls given pseudo-register lives
828 through. */
829 extern VEC(rtx, heap) **regno_calls;
831 extern int add_regno_call (int, rtx);
833 extern void debug_allocno_copies (allocno_t);
835 extern void traverse_loop_tree (int, loop_tree_node_t,
836 void (*) (loop_tree_node_t),
837 void (*) (loop_tree_node_t));
838 extern allocno_t create_allocno (int, int, loop_tree_node_t);
839 extern void set_allocno_cover_class (allocno_t, enum reg_class);
840 extern int conflict_vector_profitable_p (allocno_t, int);
841 extern void allocate_allocno_conflict_vec (allocno_t, int);
842 extern void allocate_allocno_conflicts (allocno_t, int);
843 extern void add_allocno_conflict (allocno_t, allocno_t);
844 extern void print_expanded_allocno (allocno_t);
845 extern allocno_live_range_t create_allocno_live_range (allocno_t, int, int,
846 allocno_live_range_t);
847 extern void finish_allocno_live_range (allocno_live_range_t);
848 extern void free_allocno_updated_costs (allocno_t);
849 extern copy_t create_copy (allocno_t, allocno_t, int, rtx, loop_tree_node_t);
850 extern void add_allocno_copy_to_list (copy_t);
851 extern void swap_allocno_copy_ends_if_necessary (copy_t);
852 extern void remove_allocno_copy_from_list (copy_t);
853 extern copy_t add_allocno_copy (allocno_t, allocno_t, int, rtx,
854 loop_tree_node_t);
856 extern int *allocate_cost_vector (enum reg_class);
857 extern void free_cost_vector (int *, enum reg_class);
859 extern void ira_flattening (int, int);
860 extern int ira_build (int);
861 extern void ira_destroy (void);
863 /* ira-costs.c */
864 extern void init_ira_costs_once (void);
865 extern void init_ira_costs (void);
866 extern void finish_ira_costs_once (void);
867 extern void ira_costs (void);
868 extern void tune_allocno_costs_and_cover_classes (void);
870 /* ira-lives.c */
872 extern void rebuild_start_finish_chains (void);
873 extern void print_live_range_list (FILE *, allocno_live_range_t);
874 extern void debug_live_range_list (allocno_live_range_t);
875 extern void debug_allocno_live_ranges (allocno_t);
876 extern void debug_live_ranges (void);
877 extern void create_allocno_live_ranges (void);
878 extern void finish_allocno_live_ranges (void);
880 /* ira-conflicts.c */
881 extern int allocno_live_ranges_intersect_p (allocno_t, allocno_t);
882 extern int pseudo_live_ranges_intersect_p (int, int);
883 extern void debug_conflicts (int);
884 extern void ira_build_conflicts (void);
886 /* ira-color.c */
887 extern int loop_edge_freq (loop_tree_node_t, int, int);
888 extern void reassign_conflict_allocnos (int);
889 extern void initiate_ira_assign (void);
890 extern void finish_ira_assign (void);
891 extern void ira_color (void);
892 extern void ira_fast_allocation (void);
894 /* ira-emit.c */
895 extern void ira_emit (int);
899 /* The iterator for all allocnos. */
900 typedef struct {
901 /* The number of the current element in ALLOCNOS. */
902 int n;
903 } allocno_iterator;
905 /* Initialize the iterator I. */
906 static inline void
907 allocno_iter_init (allocno_iterator *i)
909 i->n = 0;
912 /* Return TRUE if we have more allocnos to visit, in which case *A is
913 set to the allocno to be visited. Otherwise, return FALSE. */
914 static inline int
915 allocno_iter_cond (allocno_iterator *i, allocno_t *a)
917 int n;
919 for (n = i->n; n < allocnos_num; n++)
920 if (allocnos[n] != NULL)
922 *a = allocnos[n];
923 i->n = n + 1;
924 return TRUE;
926 return FALSE;
929 /* Loop over all allocnos. In each iteration, A is set to the next
930 allocno. ITER is an instance of allocno_iterator used to iterate
931 the allocnos. */
932 #define FOR_EACH_ALLOCNO(A, ITER) \
933 for (allocno_iter_init (&(ITER)); \
934 allocno_iter_cond (&(ITER), &(A));)
939 /* The iterator for copies. */
940 typedef struct {
941 /* The number of the current element in COPIES. */
942 int n;
943 } copy_iterator;
945 /* Initialize the iterator I. */
946 static inline void
947 copy_iter_init (copy_iterator *i)
949 i->n = 0;
952 /* Return TRUE if we have more copies to visit, in which case *CP is
953 set to the copy to be visited. Otherwise, return FALSE. */
954 static inline int
955 copy_iter_cond (copy_iterator *i, copy_t *cp)
957 int n;
959 for (n = i->n; n < copies_num; n++)
960 if (copies[n] != NULL)
962 *cp = copies[n];
963 i->n = n + 1;
964 return TRUE;
966 return FALSE;
969 /* Loop over all copies. In each iteration, C is set to the next
970 copy. ITER is an instance of copy_iterator used to iterate
971 the copies. */
972 #define FOR_EACH_COPY(C, ITER) \
973 for (copy_iter_init (&(ITER)); \
974 copy_iter_cond (&(ITER), &(C));)
979 /* The iterator for allocno conflicts. */
980 typedef struct {
982 /* TRUE if the conflicts are represented by vector of allocnos. */
983 int allocno_conflict_vec_p;
985 /* The conflict vector or conflict bit vector. */
986 void *vec;
988 /* The number of the current element in the vector (of type
989 allocno_t or INT_TYPE). */
990 unsigned int word_num;
992 /* The bit vector size. It is defined only if
993 ALLOCNO_CONFLICT_VEC_P is FALSE. */
994 unsigned int size;
996 /* The current bit index of bit vector. It is defined only if
997 ALLOCNO_CONFLICT_VEC_P is FALSE. */
998 unsigned int bit_num;
1000 /* Allocno conflict id corresponding to the 1st bit of the bit
1001 vector. It is defined only if ALLOCNO_CONFLICT_VEC_P is
1002 FALSE. */
1003 int base_conflict_id;
1005 /* The word of bit vector currently visited. It is defined only if
1006 ALLOCNO_CONFLICT_VEC_P is FALSE. */
1007 unsigned INT_TYPE word;
1008 } allocno_conflict_iterator;
1010 /* Initialize the iterator I with ALLOCNO conflicts. */
1011 static inline void
1012 allocno_conflict_iter_init (allocno_conflict_iterator *i, allocno_t allocno)
1014 i->allocno_conflict_vec_p = ALLOCNO_CONFLICT_VEC_P (allocno);
1015 i->vec = ALLOCNO_CONFLICT_ALLOCNO_ARRAY (allocno);
1016 i->word_num = 0;
1017 if (i->allocno_conflict_vec_p)
1018 i->size = i->bit_num = i->base_conflict_id = i->word = 0;
1019 else
1021 if (ALLOCNO_MIN (allocno) > ALLOCNO_MAX (allocno))
1022 i->size = 0;
1023 else
1024 i->size = ((ALLOCNO_MAX (allocno) - ALLOCNO_MIN (allocno) + INT_BITS)
1025 / INT_BITS) * sizeof (INT_TYPE);
1026 i->bit_num = 0;
1027 i->base_conflict_id = ALLOCNO_MIN (allocno);
1028 i->word = (i->size == 0 ? 0 : ((INT_TYPE *) i->vec)[0]);
1032 /* Return TRUE if we have more conflicting allocnos to visit, in which
1033 case *A is set to the allocno to be visited. Otherwise, return
1034 FALSE. */
1035 static inline int
1036 allocno_conflict_iter_cond (allocno_conflict_iterator *i, allocno_t *a)
1038 allocno_t conflict_allocno;
1040 if (i->allocno_conflict_vec_p)
1042 conflict_allocno = ((allocno_t *) i->vec)[i->word_num];
1043 if (conflict_allocno == NULL)
1044 return FALSE;
1045 *a = conflict_allocno;
1046 return TRUE;
1048 else
1050 /* Skip words that are zeros. */
1051 for (; i->word == 0; i->word = ((INT_TYPE *) i->vec)[i->word_num])
1053 i->word_num++;
1055 /* If we have reached the end, break. */
1056 if (i->word_num * sizeof (INT_TYPE) >= i->size)
1057 return FALSE;
1059 i->bit_num = i->word_num * INT_BITS;
1062 /* Skip bits that are zero. */
1063 for (; (i->word & 1) == 0; i->word >>= 1)
1064 i->bit_num++;
1066 *a = conflict_id_allocno_map[i->bit_num + i->base_conflict_id];
1068 return TRUE;
1072 /* Advance to the next conflicting allocno. */
1073 static inline void
1074 allocno_conflict_iter_next (allocno_conflict_iterator *i)
1076 if (i->allocno_conflict_vec_p)
1077 i->word_num++;
1078 else
1080 i->word >>= 1;
1081 i->bit_num++;
1085 /* Loop over all allocnos conflicting with ALLOCNO. In each
1086 iteration, A is set to the next conflicting allocno. ITER is an
1087 instance of allocno_conflict_iterator used to iterate the
1088 conflicts. */
1089 #define FOR_EACH_ALLOCNO_CONFLICT(ALLOCNO, A, ITER) \
1090 for (allocno_conflict_iter_init (&(ITER), (ALLOCNO)); \
1091 allocno_conflict_iter_cond (&(ITER), &(A)); \
1092 allocno_conflict_iter_next (&(ITER)))
1096 /* The function returns TRUE if hard registers starting with
1097 HARD_REGNO and containing value of MODE are not in set
1098 HARD_REGSET. */
1099 static inline int
1100 hard_reg_not_in_set_p (int hard_regno, enum machine_mode mode,
1101 HARD_REG_SET hard_regset)
1103 int i;
1105 ira_assert (hard_regno >= 0);
1106 for (i = hard_regno_nregs[hard_regno][mode] - 1; i >= 0; i--)
1107 if (TEST_HARD_REG_BIT (hard_regset, hard_regno + i))
1108 return FALSE;
1109 return TRUE;
1114 /* To save memory we use a lazy approach for allocation and
1115 initialization of the cost vectors. We do this only when it is
1116 really necessary. */
1118 /* Allocate cost vector *VEC for hard registers of COVER_CLASS and
1119 initialize the elements by VAL if it is necessary */
1120 static inline void
1121 allocate_and_set_costs (int **vec, enum reg_class cover_class, int val)
1123 int i, *reg_costs;
1124 int len;
1126 if (*vec != NULL)
1127 return;
1128 *vec = reg_costs = allocate_cost_vector (cover_class);
1129 len = class_hard_regs_num[cover_class];
1130 for (i = 0; i < len; i++)
1131 reg_costs[i] = val;
1134 /* Allocate cost vector *VEC for hard registers of COVER_CLASS and
1135 copy values of vector SRC into the vector if it is necessary */
1136 static inline void
1137 allocate_and_copy_costs (int **vec, enum reg_class cover_class, int *src)
1139 int len;
1141 if (*vec != NULL || src == NULL)
1142 return;
1143 *vec = allocate_cost_vector (cover_class);
1144 len = class_hard_regs_num[cover_class];
1145 memcpy (*vec, src, sizeof (int) * len);
1148 /* Allocate cost vector *VEC for hard registers of COVER_CLASS and
1149 copy values of vector SRC into the vector or initialize it by VAL
1150 (if SRC is null). */
1151 static inline void
1152 allocate_and_set_or_copy_costs (int **vec, enum reg_class cover_class,
1153 int val, int *src)
1155 int i, *reg_costs;
1156 int len;
1158 if (*vec != NULL)
1159 return;
1160 *vec = reg_costs = allocate_cost_vector (cover_class);
1161 len = class_hard_regs_num[cover_class];
1162 if (src != NULL)
1163 memcpy (reg_costs, src, sizeof (int) * len);
1164 else
1166 for (i = 0; i < len; i++)
1167 reg_costs[i] = val;