1 /* Graph coloring register allocator
2 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Michael Matz <matz@suse.de>
4 and Daniel Berlin <dan@cgsoftware.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2, or (at your option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
17 You should have received a copy of the GNU General Public License along
18 with GCC; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
21 /* Double linked list to implement the per-type lists of webs
33 /* Simple helper macros for ease of misuse. */
34 #define DLIST_WEB(l) ((l)->value.web)
35 #define DLIST_MOVE(l) ((l)->value.move)
37 /* Classification of a given node (i.e. what state it's in). */
42 SIMPLIFY
, SIMPLIFY_SPILL
, SIMPLIFY_FAT
, FREEZE
, SPILL
,
44 SPILLED
, COALESCED
, COLORED
,
48 /* A list of conflict bitmaps, factorized on the exact part of
49 the source, which conflicts with the DEFs, whose ID are noted in
50 the bitmap. This is used while building web-parts with conflicts. */
51 struct tagged_conflict
53 struct tagged_conflict
*next
;
56 /* If the part of source identified by size S, byteoffset O conflicts,
57 then size_word == S | (O << 16). */
58 unsigned int size_word
;
61 /* Such a structure is allocated initially for each def and use.
62 In the process of building the interference graph web parts are
63 connected together, if they have common instructions and reference the
64 same register. That way live ranges are build (by connecting defs and
65 uses) and implicitly complete webs (by connecting web parts in common
69 /* The def or use for this web part. */
71 /* The uplink implementing the disjoint set. */
72 struct web_part
*uplink
;
74 /* Here dynamic information associated with each def/use is saved.
75 This all is only valid for root web parts (uplink==NULL).
76 That's the information we need to merge, if web parts are unioned. */
78 /* Number of spanned insns containing any deaths. */
79 unsigned int spanned_deaths
;
80 /* The list of bitmaps of DEF ID's with which this part conflicts. */
81 struct tagged_conflict
*sub_conflicts
;
82 /* If there's any call_insn, while this part is live. */
83 unsigned int crosses_call
: 1;
86 /* Web structure used to store info about connected live ranges.
87 This represents the nodes of the interference graph, which gets
88 colored. It can also hold subwebs, which are contained in webs
89 and represent subregs. */
95 /* Register number of the live range's variable. */
98 /* How many insns containing deaths do we span? */
99 unsigned int span_deaths
;
101 /* Spill_temp indicates if this web was part of a web spilled in the
102 last iteration, or or reasons why this shouldn't be spilled again.
103 1 spill web, can't be spilled.
104 2 big spill web (live over some deaths). Discouraged, but not
105 impossible to spill again.
106 3 short web (spans no deaths), can't be spilled. */
107 unsigned int spill_temp
;
109 /* When coalescing we might change spill_temp. If breaking aliases we
110 need to restore it. */
111 unsigned int orig_spill_temp
;
113 /* Cost of spilling. */
114 unsigned HOST_WIDE_INT spill_cost
;
115 unsigned HOST_WIDE_INT orig_spill_cost
;
117 /* How many webs are aliased to us? */
118 unsigned int num_aliased
;
120 /* The color we got. This is a hardreg number. */
122 /* 1 + the color this web got in the last pass. If it hadn't got a color,
123 or we are in the first pass, or this web is a new one, this is zero. */
126 /* Now follow some flags characterizing the web. */
128 /* Nonzero, if we should use usable_regs for this web, instead of
129 preferred_class() or alternate_class(). */
130 unsigned int use_my_regs
:1;
132 /* Nonzero if we selected this web as possible spill candidate in
134 unsigned int was_spilled
:1;
136 /* We need to distinguish also webs which are targets of coalescing
137 (all x with some y, so that x==alias(y)), but the alias field is
138 only set for sources of coalescing. This flag is set for all webs
139 involved in coalescing in some way. */
140 unsigned int is_coalesced
:1;
142 /* Nonzero, if this web (or subweb) doesn't correspond with any of
143 the current functions actual use of reg rtx. Happens e.g. with
144 conflicts to a web, of which only a part was still undefined at the
145 point of that conflict. In this case we construct a subweb
146 representing these yet undefined bits to have a target for the
147 conflict. Suppose e.g. this sequence:
150 (set (subreg:SI (reg:DI x) 0) ...)
152 Here x only partly conflicts with y. Namely only (subreg:SI (reg:DI x)
153 1) conflicts with it, but this rtx doesn't show up in the program. For
154 such things an "artificial" subweb is built, and this flag is true for
156 unsigned int artificial
:1;
158 /* Nonzero if we span a call_insn. */
159 unsigned int crosses_call
:1;
161 /* Wether the web is involved in a move insn. */
162 unsigned int move_related
:1;
164 /* 1 when this web (or parts thereof) are live over an abnormal edge. */
165 unsigned int live_over_abnormal
:1;
167 /* Nonzero if this web is used in subregs where the mode change
168 was illegal for hardregs in CLASS_CANNOT_CHANGE_MODE. */
169 unsigned int mode_changed
:1;
171 /* Nonzero, when this web stems from the last pass of the allocator,
172 and all info is still valid (i.e. it wasn't spilled). */
173 unsigned int old_web
:1;
175 /* Used in rewrite_program2() to remember webs, which
176 are already marked for (re)loading. */
177 unsigned int in_load
:1;
179 /* If in_load is != 0, then this is nonzero, if only one use was seen
180 since insertion in loadlist. Zero if more uses currently need a
181 reload. Used to differentiate between inserting register loads or
182 directly substituting the stackref. */
183 unsigned int one_load
:1;
185 /* When using rewrite_program2() this flag gets set if some insns
186 were inserted on behalf of this web. IR spilling might ignore some
187 deaths up to the def, so no code might be emitted and we need not to
188 spill such a web again. */
189 unsigned int changed
:1;
191 /* With interference region spilling it's sometimes the case, that a
192 bb border is also an IR border for webs, which were targets of moves,
193 which are already removed due to coalescing. All webs, which are
194 a destination of such a removed move, have this flag set. */
195 unsigned int target_of_spilled_move
:1;
197 /* For optimistic coalescing we need to be able to break aliases, which
198 includes restoring conflicts to those before coalescing. This flag
199 is set, if we have a list of conflicts before coalescing. It's needed
200 because that list is lazily constructed only when actually needed. */
201 unsigned int have_orig_conflicts
:1;
203 /* Current state of the node. */
204 ENUM_BITFIELD(node_type
) type
:5;
206 /* A regclass, combined from preferred and alternate class, or calculated
207 from usable_regs. Used only for debugging, and to determine
209 ENUM_BITFIELD(reg_class
) regclass
:10;
211 /* Additional consecutive hardregs needed for this web. */
214 /* Number of conflicts currently. */
217 /* Numbers of uses and defs, which belong to this web. */
218 unsigned int num_uses
;
219 unsigned int num_defs
;
221 /* The (reg:M a) or (subreg:M1 (reg:M2 a) x) rtx which this
222 web is based on. This is used to distinguish subreg webs
223 from it's reg parents, and to get hold of the mode. */
226 /* If this web is a subweb, this point to the super web. Otherwise
228 struct web
*parent_web
;
230 /* If this web is a subweb, but not the last one, this points to the
231 next subweb of the same super web. Otherwise it's NULL. */
232 struct web
*subreg_next
;
234 /* The set of webs (or subwebs), this web conflicts with. */
235 struct conflict_link
*conflict_list
;
237 /* If have_orig_conflicts is set this contains a copy of conflict_list,
238 as it was right after building the interference graph.
239 It's used for incremental i-graph building and for breaking
240 coalescings again. */
241 struct conflict_link
*orig_conflict_list
;
243 /* Bitmap of all conflicts which don't count this pass, because of
244 non-intersecting hardregs of the conflicting webs. See also
245 record_conflict(). */
246 bitmap useless_conflicts
;
248 /* Different sets of hard registers, for all usable registers, ... */
249 HARD_REG_SET usable_regs
;
250 /* ... the same before coalescing, ... */
251 HARD_REG_SET orig_usable_regs
;
252 /* ... colors of all already colored neighbors (used when biased coloring
253 is active), and ... */
254 HARD_REG_SET bias_colors
;
255 /* ... colors of PRECOLORED webs this web is connected to by a move. */
256 HARD_REG_SET prefer_colors
;
258 /* Number of usable colors in usable_regs. */
261 /* After successfull coloring the graph each web gets a new reg rtx,
262 with which the original uses and defs are replaced. This is it. */
265 /* While spilling this is the rtx of the home of spilled webs.
266 It can be a mem ref (a stack slot), or a pseudo register. */
269 /* Used in rewrite_program2() to remember the using
270 insn last seen for webs needing (re)loads. */
273 /* If this web is rematerializable, this contains the RTL pattern
274 usable as source for that. Otherwise it's NULL. */
277 /* All the defs and uses. There are num_defs, resp.
278 num_uses elements. */
279 struct ref
**defs
; /* [0..num_defs-1] */
280 struct ref
**uses
; /* [0..num_uses-1] */
282 /* The web to which this web is aliased (coalesced). If NULL, this
283 web is not coalesced into some other (but might still be a target
287 /* With iterated coalescing this is a list of active moves this web
289 struct move_list
*moves
;
291 /* The list implementation. */
294 /* While building webs, out of web-parts, this holds a (partial)
295 list of all refs for this web seen so far. */
296 struct df_link
*temp_refs
;
299 /* For implementing a single linked list. */
302 struct web_link
*next
;
306 /* A subconflict is part of a conflict edge to track precisely,
307 which parts of two webs conflict, in case not all of both webs do. */
310 /* The next partial conflict. For one such list the parent-web of
311 all the S webs, resp. the parent of all the T webs are constant. */
312 struct sub_conflict
*next
;
317 /* This represents an edge in the conflict graph. */
320 struct conflict_link
*next
;
322 /* The web we conflict with (the Target of the edge). */
325 /* If not the complete source web and T conflict, this points to
326 the list of parts which really conflict. */
327 struct sub_conflict
*sub
;
330 /* For iterated coalescing the moves can be in these states. */
333 WORKLIST
, MV_COALESCED
, CONSTRAINED
, FROZEN
, ACTIVE
,
337 /* Structure of a move we are considering coalescing. */
341 struct web
*source_web
;
342 struct web
*target_web
;
350 struct move_list
*next
;
354 /* To have fast access to the defs and uses per insn, we have one such
355 structure per insn. The difference to the normal df.c structures is,
356 that it doesn't contain any NULL refs, which df.c produces in case
357 an insn was modified and it only contains refs to pseudo regs, or to
358 hardregs which matter for allocation, i.e. those not in
362 unsigned int num_defs
, num_uses
;
363 struct ref
**defs
, **uses
;
366 /* The above structures are stored in this array, indexed by UID... */
367 extern struct ra_insn_info
*insn_df
;
368 /* ... and the size of that array, as we add insn after setting it up. */
369 extern int insn_df_max_uid
;
371 /* The interference graph. */
372 extern sbitmap igraph
;
373 /* And how to access it. I and J are web indices. If the bit
374 igraph_index(I, J) is set, then they conflict. Note, that
375 if only parts of webs conflict, then also only those parts
376 are noted in the I-graph (i.e. the parent webs not). */
377 #define igraph_index(i, j) ((i) < (j) ? ((j)*((j)-1)/2)+(i) : ((i)*((i)-1)/2)+(j))
378 /* This is the bitmap of all (even partly) conflicting super webs.
379 If bit I*num_webs+J or J*num_webs+I is set, then I and J (both being
380 super web indices) conflict, maybe only partially. Note the
382 extern sbitmap sup_igraph
;
384 /* After the first pass, and when interference region spilling is
385 activated, bit I is set, when the insn with UID I contains some
386 refs to pseudos which die at the insn. */
387 extern sbitmap insns_with_deaths
;
388 /* The size of that sbitmap. */
389 extern int death_insns_max_uid
;
391 /* All the web-parts. There are exactly as many web-parts as there
392 are register refs in the insn stream. */
393 extern struct web_part
*web_parts
;
395 /* The number of all webs, including subwebs. */
396 extern unsigned int num_webs
;
397 /* The number of just the subwebs. */
398 extern unsigned int num_subwebs
;
399 /* The number of all webs, including subwebs. */
400 extern unsigned int num_allwebs
;
402 /* For easy access when given a web ID: id2web[W->id] == W. */
403 extern struct web
**id2web
;
404 /* For each hardreg, the web which represents it. */
405 extern struct web
*hardreg2web
[FIRST_PSEUDO_REGISTER
];
407 /* Given the ID of a df_ref, which represent a DEF, def2web[ID] is
408 the web, to which this def belongs. */
409 extern struct web
**def2web
;
410 /* The same as def2web, just for uses. */
411 extern struct web
**use2web
;
413 /* The list of all recognized and coalescable move insns. */
414 extern struct move_list
*wl_moves
;
417 /* Some parts of the compiler which we run after colorizing
418 clean reg_renumber[], so we need another place for the colors.
419 This is copied to reg_renumber[] just before returning to toplev. */
420 extern short *ra_reg_renumber
;
421 /* The size of that array. Some passes after coloring might have created
422 new pseudos, which will get no color. */
423 extern int ra_max_regno
;
425 /* The dataflow structure of the current function, while regalloc
427 extern struct df
*df
;
429 /* For each basic block B we have a bitmap of DF_REF_ID's of uses,
430 which backward reach the end of B. */
431 extern bitmap
*live_at_end
;
433 /* One pass is: collecting registers refs, building I-graph, spilling.
434 And this is how often we already ran that for the current function. */
437 /* The maximum pseudo regno, just before register allocation starts.
438 While regalloc runs all pseudos with a larger number represent
439 potentially stack slots or hardregs. I call them stackwebs or
441 extern unsigned int max_normal_pseudo
;
443 /* One of the fixed colors. It must be < FIRST_PSEUDO_REGISTER, because
444 we sometimes want to check the color against a HARD_REG_SET. It must
445 be >= 0, because negative values mean "no color".
446 This color is used for the above stackwebs, when they can't be colored.
447 I.e. normally they would be spilled, but they already represent
448 stackslots. So they are colored with an invalid color. It has
449 the property that even webs which conflict can have that color at the
450 same time. I.e. a stackweb with that color really represents a
452 extern int an_unusable_color
;
454 /* While building the I-graph, every time insn UID is looked at,
455 number_seen[UID] is incremented. For debugging. */
456 extern int *number_seen
;
458 /* The different lists on which a web can be (based on the type). */
459 extern struct dlist
*web_lists
[(int) LAST_NODE_TYPE
];
460 #define WEBS(type) (web_lists[(int)(type)])
462 /* The largest DF_REF_ID of defs resp. uses, as it was in the
463 last pass. In the first pass this is zero. Used to distinguish new
464 from old refrences. */
465 extern unsigned int last_def_id
;
466 extern unsigned int last_use_id
;
468 /* Similar for UIDs and number of webs. */
469 extern int last_max_uid
;
470 extern unsigned int last_num_webs
;
472 /* If I is the ID of an old use, and last_check_uses[I] is set,
473 then we must reevaluate it's flow while building the new I-graph. */
474 extern sbitmap last_check_uses
;
476 /* If nonzero, record_conflict() saves the current conflict list of
477 webs in orig_conflict_list, when not already done so, and the conflict
478 list is going to be changed. It is set, after initially building the
479 I-graph. I.e. new conflicts due to coalescing trigger that copying. */
480 extern unsigned int remember_conflicts
;
482 /* The maximum UID right before calling regalloc().
483 Used to detect any instructions inserted by the allocator. */
484 extern int orig_max_uid
;
486 /* A HARD_REG_SET of those color, which can't be used for coalescing.
487 Includes e.g. fixed_regs. */
488 extern HARD_REG_SET never_use_colors
;
489 /* For each class C this is reg_class_contents[C] \ never_use_colors. */
490 extern HARD_REG_SET usable_regs
[N_REG_CLASSES
];
491 /* For each class C the count of hardregs in usable_regs[C]. */
492 extern unsigned int num_free_regs
[N_REG_CLASSES
];
493 /* For each mode M the hardregs, which are MODE_OK for M, and have
494 enough space behind them to hold an M value. Additionally
495 if reg R is OK for mode M, but it needs two hardregs, then R+1 will
496 also be set here, even if R+1 itself is not OK for M. I.e. this
497 represent the possible resources which could be taken away be a value
499 extern HARD_REG_SET hardregs_for_mode
[NUM_MACHINE_MODES
];
500 /* For 0 <= I <= 255, the number of bits set in I. Used to calculate
501 the number of set bits in a HARD_REG_SET. */
502 extern unsigned char byte2bitcount
[256];
504 /* Expressive helper macros. */
505 #define ID2WEB(I) id2web[I]
506 #define NUM_REGS(W) (((W)->type == PRECOLORED) ? 1 : (W)->num_freedom)
507 #define SUBWEB_P(W) (GET_CODE ((W)->orig_x) == SUBREG)
509 /* Constant usable as debug area to ra_debug_msg. */
510 #define DUMP_COSTS 0x0001
511 #define DUMP_WEBS 0x0002
512 #define DUMP_IGRAPH 0x0004
513 #define DUMP_PROCESS 0x0008
514 #define DUMP_COLORIZE 0x0010
515 #define DUMP_ASM 0x0020
516 #define DUMP_CONSTRAINTS 0x0040
517 #define DUMP_RESULTS 0x0080
518 #define DUMP_DF 0x0100
519 #define DUMP_RTL 0x0200
520 #define DUMP_FINAL_RTL 0x0400
521 #define DUMP_REGCLASS 0x0800
522 #define DUMP_SM 0x1000
523 #define DUMP_LAST_FLOW 0x2000
524 #define DUMP_LAST_RTL 0x4000
525 #define DUMP_REBUILD 0x8000
526 #define DUMP_IGRAPH_M 0x10000
527 #define DUMP_VALIDIFY 0x20000
528 #define DUMP_EVER ((unsigned int)-1)
529 #define DUMP_NEARLY_EVER (DUMP_EVER - DUMP_COSTS - DUMP_IGRAPH_M)
531 /* All the wanted debug levels as ORing of the various DUMP_xxx
533 extern unsigned int debug_new_regalloc
;
535 /* Nonzero means we want biased coloring. */
536 extern int flag_ra_biased
;
538 /* Nonzero if we want to use improved (and slow) spilling. This
539 includes also interference region spilling (see below). */
540 extern int flag_ra_improved_spilling
;
542 /* Nonzero for using interference region spilling. Zero for improved
543 Chaintin style spilling (only at deaths). */
544 extern int flag_ra_ir_spilling
;
546 /* Nonzero if we use optimistic coalescing, zero for iterated
548 extern int flag_ra_optimistic_coalescing
;
550 /* Nonzero if we want to break aliases of spilled webs. Forced to
551 nonzero, when flag_ra_optimistic_coalescing is. */
552 extern int flag_ra_break_aliases
;
554 /* Nonzero if we want to merge the spill costs of webs which
556 extern int flag_ra_merge_spill_costs
;
558 /* Nonzero if we want to spill at every use, instead of at deaths,
559 or intereference region borders. */
560 extern int flag_ra_spill_every_use
;
562 /* Nonzero to output all notes in the debug dumps. */
563 extern int flag_ra_dump_notes
;
565 extern inline void * ra_alloc
PARAMS ((size_t));
566 extern inline void * ra_calloc
PARAMS ((size_t));
567 extern int hard_regs_count
PARAMS ((HARD_REG_SET
));
568 extern rtx ra_emit_move_insn
PARAMS ((rtx
, rtx
));
569 extern void ra_debug_msg
PARAMS ((unsigned int,
570 const char *, ...)) ATTRIBUTE_PRINTF_2
;
571 extern int hard_regs_intersect_p
PARAMS ((HARD_REG_SET
*, HARD_REG_SET
*));
572 extern unsigned int rtx_to_bits
PARAMS ((rtx
));
573 extern struct web
* find_subweb
PARAMS ((struct web
*, rtx
));
574 extern struct web
* find_subweb_2
PARAMS ((struct web
*, unsigned int));
575 extern struct web
* find_web_for_subweb_1
PARAMS ((struct web
*));
577 #define find_web_for_subweb(w) (((w)->parent_web) \
578 ? find_web_for_subweb_1 ((w)->parent_web) \
581 extern void ra_build_realloc
PARAMS ((struct df
*));
582 extern void ra_build_free
PARAMS ((void));
583 extern void ra_build_free_all
PARAMS ((struct df
*));
584 extern void ra_colorize_init
PARAMS ((void));
585 extern void ra_colorize_free_all
PARAMS ((void));
586 extern void ra_rewrite_init
PARAMS ((void));
588 extern void ra_print_rtx
PARAMS ((FILE *, rtx
, int));
589 extern void ra_print_rtx_top
PARAMS ((FILE *, rtx
, int));
590 extern void ra_debug_rtx
PARAMS ((rtx
));
591 extern void ra_debug_insns
PARAMS ((rtx
, int));
592 extern void ra_debug_bbi
PARAMS ((int));
593 extern void ra_print_rtl_with_bb
PARAMS ((FILE *, rtx
));
594 extern void dump_igraph
PARAMS ((struct df
*));
595 extern void dump_igraph_machine
PARAMS ((void));
596 extern void dump_constraints
PARAMS ((void));
597 extern void dump_cost
PARAMS ((unsigned int));
598 extern void dump_graph_cost
PARAMS ((unsigned int, const char *));
599 extern void dump_ra
PARAMS ((struct df
*));
600 extern void dump_number_seen
PARAMS ((void));
601 extern void dump_static_insn_cost
PARAMS ((FILE *, const char *,
603 extern void dump_web_conflicts
PARAMS ((struct web
*));
604 extern void dump_web_insns
PARAMS ((struct web
*));
605 extern int web_conflicts_p
PARAMS ((struct web
*, struct web
*));
606 extern void debug_hard_reg_set
PARAMS ((HARD_REG_SET
));
608 extern void remove_list
PARAMS ((struct dlist
*, struct dlist
**));
609 extern struct dlist
* pop_list
PARAMS ((struct dlist
**));
610 extern void record_conflict
PARAMS ((struct web
*, struct web
*));
611 extern int memref_is_stack_slot
PARAMS ((rtx
));
612 extern void build_i_graph
PARAMS ((struct df
*));
613 extern void put_web
PARAMS ((struct web
*, enum node_type
));
614 extern void remove_web_from_list
PARAMS ((struct web
*));
615 extern void reset_lists
PARAMS ((void));
616 extern struct web
* alias
PARAMS ((struct web
*));
617 extern void merge_moves
PARAMS ((struct web
*, struct web
*));
618 extern void ra_colorize_graph
PARAMS ((struct df
*));
620 extern void actual_spill
PARAMS ((void));
621 extern void emit_colors
PARAMS ((struct df
*));
622 extern void delete_moves
PARAMS ((void));
623 extern void setup_renumber
PARAMS ((int));
624 extern void remove_suspicious_death_notes
PARAMS ((void));