2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / gcc / ra.h
blob53085e187d0cf3c7728402d20fc50a000a723274
1 /* Graph coloring register allocator
2 Copyright (C) 2001, 2002, 2003 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
15 details.
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
22 and moves. */
23 struct dlist
25 struct dlist *prev;
26 struct dlist *next;
27 union
29 struct web *web;
30 struct move *move;
31 } value;
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). */
38 enum node_type
40 INITIAL = 0, FREE,
41 PRECOLORED,
42 SIMPLIFY, SIMPLIFY_SPILL, SIMPLIFY_FAT, FREEZE, SPILL,
43 SELECT,
44 SPILLED, COALESCED, COLORED,
45 LAST_NODE_TYPE
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;
54 bitmap conflicts;
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
66 uses). */
67 struct web_part
69 /* The def or use for this web part. */
70 struct ref *ref;
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. */
90 struct web
92 /* Unique web ID. */
93 unsigned int id;
95 /* Register number of the live range's variable. */
96 unsigned int regno;
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. */
121 int color;
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. */
124 int old_color;
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
133 select_spill(). */
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:
148 (set (reg:DI x) ...)
149 (set (reg:SI y) ...)
150 (set (subreg:SI (reg:DI x) 0) ...)
151 (use (reg:DI x))
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
155 them. */
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 if some references of this web, where in subreg context,
172 but the actual subreg is already stripped (i.e. we don't know the
173 outer mode of the actual reference). */
174 unsigned int subreg_stripped:1;
176 /* Nonzero, when this web stems from the last pass of the allocator,
177 and all info is still valid (i.e. it wasn't spilled). */
178 unsigned int old_web:1;
180 /* Used in rewrite_program2() to remember webs, which
181 are already marked for (re)loading. */
182 unsigned int in_load:1;
184 /* If in_load is != 0, then this is nonzero, if only one use was seen
185 since insertion in loadlist. Zero if more uses currently need a
186 reload. Used to differentiate between inserting register loads or
187 directly substituting the stackref. */
188 unsigned int one_load:1;
190 /* When using rewrite_program2() this flag gets set if some insns
191 were inserted on behalf of this web. IR spilling might ignore some
192 deaths up to the def, so no code might be emitted and we need not to
193 spill such a web again. */
194 unsigned int changed:1;
196 /* With interference region spilling it's sometimes the case, that a
197 bb border is also an IR border for webs, which were targets of moves,
198 which are already removed due to coalescing. All webs, which are
199 a destination of such a removed move, have this flag set. */
200 unsigned int target_of_spilled_move:1;
202 /* For optimistic coalescing we need to be able to break aliases, which
203 includes restoring conflicts to those before coalescing. This flag
204 is set, if we have a list of conflicts before coalescing. It's needed
205 because that list is lazily constructed only when actually needed. */
206 unsigned int have_orig_conflicts:1;
208 /* Current state of the node. */
209 ENUM_BITFIELD(node_type) type:5;
211 /* A regclass, combined from preferred and alternate class, or calculated
212 from usable_regs. Used only for debugging, and to determine
213 add_hardregs. */
214 ENUM_BITFIELD(reg_class) regclass:10;
216 /* Additional consecutive hardregs needed for this web. */
217 int add_hardregs;
219 /* Number of conflicts currently. */
220 int num_conflicts;
222 /* Numbers of uses and defs, which belong to this web. */
223 unsigned int num_uses;
224 unsigned int num_defs;
226 /* The (reg:M a) or (subreg:M1 (reg:M2 a) x) rtx which this
227 web is based on. This is used to distinguish subreg webs
228 from it's reg parents, and to get hold of the mode. */
229 rtx orig_x;
231 /* If this web is a subweb, this point to the super web. Otherwise
232 it's NULL. */
233 struct web *parent_web;
235 /* If this web is a subweb, but not the last one, this points to the
236 next subweb of the same super web. Otherwise it's NULL. */
237 struct web *subreg_next;
239 /* The set of webs (or subwebs), this web conflicts with. */
240 struct conflict_link *conflict_list;
242 /* If have_orig_conflicts is set this contains a copy of conflict_list,
243 as it was right after building the interference graph.
244 It's used for incremental i-graph building and for breaking
245 coalescings again. */
246 struct conflict_link *orig_conflict_list;
248 /* Bitmap of all conflicts which don't count this pass, because of
249 non-intersecting hardregs of the conflicting webs. See also
250 record_conflict(). */
251 bitmap useless_conflicts;
253 /* Different sets of hard registers, for all usable registers, ... */
254 HARD_REG_SET usable_regs;
255 /* ... the same before coalescing, ... */
256 HARD_REG_SET orig_usable_regs;
257 /* ... colors of all already colored neighbors (used when biased coloring
258 is active), and ... */
259 HARD_REG_SET bias_colors;
260 /* ... colors of PRECOLORED webs this web is connected to by a move. */
261 HARD_REG_SET prefer_colors;
263 /* Number of usable colors in usable_regs. */
264 int num_freedom;
266 /* After successful coloring the graph each web gets a new reg rtx,
267 with which the original uses and defs are replaced. This is it. */
268 rtx reg_rtx;
270 /* While spilling this is the rtx of the home of spilled webs.
271 It can be a mem ref (a stack slot), or a pseudo register. */
272 rtx stack_slot;
274 /* Used in rewrite_program2() to remember the using
275 insn last seen for webs needing (re)loads. */
276 rtx last_use_insn;
278 /* If this web is rematerializable, this contains the RTL pattern
279 usable as source for that. Otherwise it's NULL. */
280 rtx pattern;
282 /* All the defs and uses. There are num_defs, resp.
283 num_uses elements. */
284 struct ref **defs; /* [0..num_defs-1] */
285 struct ref **uses; /* [0..num_uses-1] */
287 /* The web to which this web is aliased (coalesced). If NULL, this
288 web is not coalesced into some other (but might still be a target
289 for other webs). */
290 struct web *alias;
292 /* With iterated coalescing this is a list of active moves this web
293 is involved in. */
294 struct move_list *moves;
296 /* The list implementation. */
297 struct dlist *dlink;
299 /* While building webs, out of web-parts, this holds a (partial)
300 list of all refs for this web seen so far. */
301 struct df_link *temp_refs;
304 /* For implementing a single linked list. */
305 struct web_link
307 struct web_link *next;
308 struct web *web;
311 /* A subconflict is part of a conflict edge to track precisely,
312 which parts of two webs conflict, in case not all of both webs do. */
313 struct sub_conflict
315 /* The next partial conflict. For one such list the parent-web of
316 all the S webs, resp. the parent of all the T webs are constant. */
317 struct sub_conflict *next;
318 struct web *s;
319 struct web *t;
322 /* This represents an edge in the conflict graph. */
323 struct conflict_link
325 struct conflict_link *next;
327 /* The web we conflict with (the Target of the edge). */
328 struct web *t;
330 /* If not the complete source web and T conflict, this points to
331 the list of parts which really conflict. */
332 struct sub_conflict *sub;
335 /* For iterated coalescing the moves can be in these states. */
336 enum move_type
338 WORKLIST, MV_COALESCED, CONSTRAINED, FROZEN, ACTIVE,
339 LAST_MOVE_TYPE
342 /* Structure of a move we are considering coalescing. */
343 struct move
345 rtx insn;
346 struct web *source_web;
347 struct web *target_web;
348 enum move_type type;
349 struct dlist *dlink;
352 /* List of moves. */
353 struct move_list
355 struct move_list *next;
356 struct move *move;
359 /* To have fast access to the defs and uses per insn, we have one such
360 structure per insn. The difference to the normal df.c structures is,
361 that it doesn't contain any NULL refs, which df.c produces in case
362 an insn was modified and it only contains refs to pseudo regs, or to
363 hardregs which matter for allocation, i.e. those not in
364 never_use_colors. */
365 struct ra_insn_info
367 unsigned int num_defs, num_uses;
368 struct ref **defs, **uses;
371 /* The above structures are stored in this array, indexed by UID... */
372 extern struct ra_insn_info *insn_df;
373 /* ... and the size of that array, as we add insn after setting it up. */
374 extern int insn_df_max_uid;
376 /* The interference graph. */
377 extern sbitmap igraph;
378 /* And how to access it. I and J are web indices. If the bit
379 igraph_index(I, J) is set, then they conflict. Note, that
380 if only parts of webs conflict, then also only those parts
381 are noted in the I-graph (i.e. the parent webs not). */
382 #define igraph_index(i, j) ((i) < (j) ? ((j)*((j)-1)/2)+(i) : ((i)*((i)-1)/2)+(j))
383 /* This is the bitmap of all (even partly) conflicting super webs.
384 If bit I*num_webs+J or J*num_webs+I is set, then I and J (both being
385 super web indices) conflict, maybe only partially. Note the
386 asymmetry. */
387 extern sbitmap sup_igraph;
389 /* After the first pass, and when interference region spilling is
390 activated, bit I is set, when the insn with UID I contains some
391 refs to pseudos which die at the insn. */
392 extern sbitmap insns_with_deaths;
393 /* The size of that sbitmap. */
394 extern int death_insns_max_uid;
396 /* All the web-parts. There are exactly as many web-parts as there
397 are register refs in the insn stream. */
398 extern struct web_part *web_parts;
400 /* The number of all webs, including subwebs. */
401 extern unsigned int num_webs;
402 /* The number of just the subwebs. */
403 extern unsigned int num_subwebs;
404 /* The number of all webs, including subwebs. */
405 extern unsigned int num_allwebs;
407 /* For easy access when given a web ID: id2web[W->id] == W. */
408 extern struct web **id2web;
409 /* For each hardreg, the web which represents it. */
410 extern struct web *hardreg2web[FIRST_PSEUDO_REGISTER];
412 /* Given the ID of a df_ref, which represent a DEF, def2web[ID] is
413 the web, to which this def belongs. */
414 extern struct web **def2web;
415 /* The same as def2web, just for uses. */
416 extern struct web **use2web;
418 /* The list of all recognized and coalescable move insns. */
419 extern struct move_list *wl_moves;
422 /* Some parts of the compiler which we run after colorizing
423 clean reg_renumber[], so we need another place for the colors.
424 This is copied to reg_renumber[] just before returning to toplev. */
425 extern short *ra_reg_renumber;
426 /* The size of that array. Some passes after coloring might have created
427 new pseudos, which will get no color. */
428 extern int ra_max_regno;
430 /* The dataflow structure of the current function, while regalloc
431 runs. */
432 extern struct df *df;
434 /* For each basic block B we have a bitmap of DF_REF_ID's of uses,
435 which backward reach the end of B. */
436 extern bitmap *live_at_end;
438 /* One pass is: collecting registers refs, building I-graph, spilling.
439 And this is how often we already ran that for the current function. */
440 extern int ra_pass;
442 /* The maximum pseudo regno, just before register allocation starts.
443 While regalloc runs all pseudos with a larger number represent
444 potentially stack slots or hardregs. I call them stackwebs or
445 stackpseudos. */
446 extern unsigned int max_normal_pseudo;
448 /* One of the fixed colors. It must be < FIRST_PSEUDO_REGISTER, because
449 we sometimes want to check the color against a HARD_REG_SET. It must
450 be >= 0, because negative values mean "no color".
451 This color is used for the above stackwebs, when they can't be colored.
452 I.e. normally they would be spilled, but they already represent
453 stackslots. So they are colored with an invalid color. It has
454 the property that even webs which conflict can have that color at the
455 same time. I.e. a stackweb with that color really represents a
456 stackslot. */
457 extern int an_unusable_color;
459 /* While building the I-graph, every time insn UID is looked at,
460 number_seen[UID] is incremented. For debugging. */
461 extern int *number_seen;
463 /* The different lists on which a web can be (based on the type). */
464 extern struct dlist *web_lists[(int) LAST_NODE_TYPE];
465 #define WEBS(type) (web_lists[(int)(type)])
467 /* The largest DF_REF_ID of defs resp. uses, as it was in the
468 last pass. In the first pass this is zero. Used to distinguish new
469 from old references. */
470 extern unsigned int last_def_id;
471 extern unsigned int last_use_id;
473 /* Similar for UIDs and number of webs. */
474 extern int last_max_uid;
475 extern unsigned int last_num_webs;
477 /* If I is the ID of an old use, and last_check_uses[I] is set,
478 then we must reevaluate it's flow while building the new I-graph. */
479 extern sbitmap last_check_uses;
481 /* If nonzero, record_conflict() saves the current conflict list of
482 webs in orig_conflict_list, when not already done so, and the conflict
483 list is going to be changed. It is set, after initially building the
484 I-graph. I.e. new conflicts due to coalescing trigger that copying. */
485 extern unsigned int remember_conflicts;
487 /* The maximum UID right before calling regalloc().
488 Used to detect any instructions inserted by the allocator. */
489 extern int orig_max_uid;
491 /* A HARD_REG_SET of those color, which can't be used for coalescing.
492 Includes e.g. fixed_regs. */
493 extern HARD_REG_SET never_use_colors;
494 /* For each class C this is reg_class_contents[C] \ never_use_colors. */
495 extern HARD_REG_SET usable_regs[N_REG_CLASSES];
496 /* For each class C the count of hardregs in usable_regs[C]. */
497 extern unsigned int num_free_regs[N_REG_CLASSES];
498 /* For each mode M the hardregs, which are MODE_OK for M, and have
499 enough space behind them to hold an M value. Additionally
500 if reg R is OK for mode M, but it needs two hardregs, then R+1 will
501 also be set here, even if R+1 itself is not OK for M. I.e. this
502 represent the possible resources which could be taken away be a value
503 in mode M. */
504 extern HARD_REG_SET hardregs_for_mode[NUM_MACHINE_MODES];
505 /* The set of hardregs, for which _any_ mode change is invalid. */
506 extern HARD_REG_SET invalid_mode_change_regs;
507 /* For 0 <= I <= 255, the number of bits set in I. Used to calculate
508 the number of set bits in a HARD_REG_SET. */
509 extern unsigned char byte2bitcount[256];
511 /* Expressive helper macros. */
512 #define ID2WEB(I) id2web[I]
513 #define NUM_REGS(W) (((W)->type == PRECOLORED) ? 1 : (W)->num_freedom)
514 #define SUBWEB_P(W) (GET_CODE ((W)->orig_x) == SUBREG)
516 /* Constant usable as debug area to ra_debug_msg. */
517 #define DUMP_COSTS 0x0001
518 #define DUMP_WEBS 0x0002
519 #define DUMP_IGRAPH 0x0004
520 #define DUMP_PROCESS 0x0008
521 #define DUMP_COLORIZE 0x0010
522 #define DUMP_ASM 0x0020
523 #define DUMP_CONSTRAINTS 0x0040
524 #define DUMP_RESULTS 0x0080
525 #define DUMP_DF 0x0100
526 #define DUMP_RTL 0x0200
527 #define DUMP_FINAL_RTL 0x0400
528 #define DUMP_REGCLASS 0x0800
529 #define DUMP_SM 0x1000
530 #define DUMP_LAST_FLOW 0x2000
531 #define DUMP_LAST_RTL 0x4000
532 #define DUMP_REBUILD 0x8000
533 #define DUMP_IGRAPH_M 0x10000
534 #define DUMP_VALIDIFY 0x20000
535 #define DUMP_EVER ((unsigned int)-1)
536 #define DUMP_NEARLY_EVER (DUMP_EVER - DUMP_COSTS - DUMP_IGRAPH_M)
538 /* All the wanted debug levels as ORing of the various DUMP_xxx
539 constants. */
540 extern unsigned int debug_new_regalloc;
542 /* Nonzero means we want biased coloring. */
543 extern int flag_ra_biased;
545 /* Nonzero if we want to use improved (and slow) spilling. This
546 includes also interference region spilling (see below). */
547 extern int flag_ra_improved_spilling;
549 /* Nonzero for using interference region spilling. Zero for improved
550 Chaintin style spilling (only at deaths). */
551 extern int flag_ra_ir_spilling;
553 /* Nonzero if we use optimistic coalescing, zero for iterated
554 coalescing. */
555 extern int flag_ra_optimistic_coalescing;
557 /* Nonzero if we want to break aliases of spilled webs. Forced to
558 nonzero, when flag_ra_optimistic_coalescing is. */
559 extern int flag_ra_break_aliases;
561 /* Nonzero if we want to merge the spill costs of webs which
562 are coalesced. */
563 extern int flag_ra_merge_spill_costs;
565 /* Nonzero if we want to spill at every use, instead of at deaths,
566 or interference region borders. */
567 extern int flag_ra_spill_every_use;
569 /* Nonzero to output all notes in the debug dumps. */
570 extern int flag_ra_dump_notes;
572 extern inline void * ra_alloc (size_t);
573 extern inline void * ra_calloc (size_t);
574 extern int hard_regs_count (HARD_REG_SET);
575 extern rtx ra_emit_move_insn (rtx, rtx);
576 extern void ra_debug_msg (unsigned int, const char *, ...) ATTRIBUTE_PRINTF_2;
577 extern int hard_regs_intersect_p (HARD_REG_SET *, HARD_REG_SET *);
578 extern unsigned int rtx_to_bits (rtx);
579 extern struct web * find_subweb (struct web *, rtx);
580 extern struct web * find_subweb_2 (struct web *, unsigned int);
581 extern struct web * find_web_for_subweb_1 (struct web *);
583 #define find_web_for_subweb(w) (((w)->parent_web) \
584 ? find_web_for_subweb_1 ((w)->parent_web) \
585 : (w))
587 extern void ra_build_realloc (struct df *);
588 extern void ra_build_free (void);
589 extern void ra_build_free_all (struct df *);
590 extern void ra_colorize_init (void);
591 extern void ra_colorize_free_all (void);
592 extern void ra_rewrite_init (void);
594 extern void ra_print_rtx (FILE *, rtx, int);
595 extern void ra_print_rtx_top (FILE *, rtx, int);
596 extern void ra_debug_rtx (rtx);
597 extern void ra_debug_insns (rtx, int);
598 extern void ra_debug_bbi (int);
599 extern void ra_print_rtl_with_bb (FILE *, rtx);
600 extern void dump_igraph (struct df *);
601 extern void dump_igraph_machine (void);
602 extern void dump_constraints (void);
603 extern void dump_cost (unsigned int);
604 extern void dump_graph_cost (unsigned int, const char *);
605 extern void dump_ra (struct df *);
606 extern void dump_number_seen (void);
607 extern void dump_static_insn_cost (FILE *, const char *, const char *);
608 extern void dump_web_conflicts (struct web *);
609 extern void dump_web_insns (struct web*);
610 extern int web_conflicts_p (struct web *, struct web *);
611 extern void debug_hard_reg_set (HARD_REG_SET);
613 extern void remove_list (struct dlist *, struct dlist **);
614 extern struct dlist * pop_list (struct dlist **);
615 extern void record_conflict (struct web *, struct web *);
616 extern int memref_is_stack_slot (rtx);
617 extern void build_i_graph (struct df *);
618 extern void put_web (struct web *, enum node_type);
619 extern void remove_web_from_list (struct web *);
620 extern void reset_lists (void);
621 extern struct web * alias (struct web *);
622 extern void merge_moves (struct web *, struct web *);
623 extern void ra_colorize_graph (struct df *);
625 extern void actual_spill (void);
626 extern void emit_colors (struct df *);
627 extern void delete_moves (void);
628 extern void setup_renumber (int);
629 extern void remove_suspicious_death_notes (void);