Makefile.in (stmp-docobjdir): New target; ensure $docobjdir exists.
[official-gcc.git] / gcc / ra-build.c
blobd00369ec14e0a47a91b91709300c58f02c7d6e0e
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 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tm_p.h"
27 #include "insn-config.h"
28 #include "recog.h"
29 #include "reload.h"
30 #include "function.h"
31 #include "regs.h"
32 #include "hard-reg-set.h"
33 #include "basic-block.h"
34 #include "df.h"
35 #include "output.h"
36 #include "ggc.h"
37 #include "ra.h"
39 /* This file is part of the graph coloring register allocator.
40 It deals with building the interference graph. When rebuilding
41 the graph for a function after spilling, we rebuild only those
42 parts needed, i.e. it works incrementally.
44 The first part (the functions called from build_web_parts_and_conflicts()
45 ) constructs a web_part for each pseudo register reference in the insn
46 stream, then goes backward from each use, until it reaches defs for that
47 pseudo. While going back it remember seen defs for other registers as
48 conflicts. By connecting the uses and defs, which reach each other, webs
49 (or live ranges) are built conceptually.
51 The second part (make_webs() and children) deals with converting that
52 structure to the nodes and edges, on which our interference graph is
53 built. For each root web part constructed above, an instance of struct
54 web is created. For all subregs of pseudos, which matter for allocation,
55 a subweb of the corresponding super web is built. Finally all the
56 conflicts noted in the first part (as bitmaps) are transformed into real
57 edges.
59 As part of that process the webs are also classified (their spill cost
60 is calculated, and if they are spillable at all, and if not, for what
61 reason; or if they are rematerializable), and move insns are collected,
62 which are potentially coalescable.
64 The top-level entry of this file (build_i_graph) puts it all together,
65 and leaves us with a complete interference graph, which just has to
66 be colored. */
69 struct curr_use;
71 static unsigned HOST_WIDE_INT rtx_to_undefined PARAMS ((rtx));
72 static bitmap find_sub_conflicts PARAMS ((struct web_part *, unsigned int));
73 static bitmap get_sub_conflicts PARAMS ((struct web_part *, unsigned int));
74 static unsigned int undef_to_size_word PARAMS ((rtx, unsigned HOST_WIDE_INT *));
75 static bitmap undef_to_bitmap PARAMS ((struct web_part *,
76 unsigned HOST_WIDE_INT *));
77 static struct web_part * find_web_part_1 PARAMS ((struct web_part *));
78 static struct web_part * union_web_part_roots
79 PARAMS ((struct web_part *, struct web_part *));
80 static int defuse_overlap_p_1 PARAMS ((rtx, struct curr_use *));
81 static int live_out_1 PARAMS ((struct df *, struct curr_use *, rtx));
82 static int live_out PARAMS ((struct df *, struct curr_use *, rtx));
83 static rtx live_in_edge PARAMS (( struct df *, struct curr_use *, edge));
84 static void live_in PARAMS ((struct df *, struct curr_use *, rtx));
85 static int copy_insn_p PARAMS ((rtx, rtx *, rtx *));
86 static void remember_move PARAMS ((rtx));
87 static void handle_asm_insn PARAMS ((struct df *, rtx));
88 static void prune_hardregs_for_mode PARAMS ((HARD_REG_SET *,
89 enum machine_mode));
90 static void init_one_web_common PARAMS ((struct web *, rtx));
91 static void init_one_web PARAMS ((struct web *, rtx));
92 static void reinit_one_web PARAMS ((struct web *, rtx));
93 static struct web * add_subweb PARAMS ((struct web *, rtx));
94 static struct web * add_subweb_2 PARAMS ((struct web *, unsigned int));
95 static void init_web_parts PARAMS ((struct df *));
96 static void copy_conflict_list PARAMS ((struct web *));
97 static void add_conflict_edge PARAMS ((struct web *, struct web *));
98 static void build_inverse_webs PARAMS ((struct web *));
99 static void copy_web PARAMS ((struct web *, struct web_link **));
100 static void compare_and_free_webs PARAMS ((struct web_link **));
101 static void init_webs_defs_uses PARAMS ((void));
102 static unsigned int parts_to_webs_1 PARAMS ((struct df *, struct web_link **,
103 struct df_link *));
104 static void parts_to_webs PARAMS ((struct df *));
105 static void reset_conflicts PARAMS ((void));
106 #if 0
107 static void check_conflict_numbers PARAMS ((void));
108 #endif
109 static void conflicts_between_webs PARAMS ((struct df *));
110 static void remember_web_was_spilled PARAMS ((struct web *));
111 static void detect_spill_temps PARAMS ((void));
112 static int contains_pseudo PARAMS ((rtx));
113 static int want_to_remat PARAMS ((rtx x));
114 static void detect_remat_webs PARAMS ((void));
115 static void determine_web_costs PARAMS ((void));
116 static void detect_webs_set_in_cond_jump PARAMS ((void));
117 static void make_webs PARAMS ((struct df *));
118 static void moves_to_webs PARAMS ((struct df *));
119 static void connect_rmw_web_parts PARAMS ((struct df *));
120 static void update_regnos_mentioned PARAMS ((void));
121 static void livethrough_conflicts_bb PARAMS ((basic_block));
122 static void init_bb_info PARAMS ((void));
123 static void free_bb_info PARAMS ((void));
124 static void build_web_parts_and_conflicts PARAMS ((struct df *));
127 /* A sbitmap of DF_REF_IDs of uses, which are live over an abnormal
128 edge. */
129 static sbitmap live_over_abnormal;
131 /* To cache if we already saw a certain edge while analyzing one
132 use, we use a tick count incremented per use. */
133 static unsigned int visited_pass;
135 /* A sbitmap of UIDs of move insns, which we already analyzed. */
136 static sbitmap move_handled;
138 /* One such structed is allocated per insn, and traces for the currently
139 analyzed use, which web part belongs to it, and how many bytes of
140 it were still undefined when that insn was reached. */
141 struct visit_trace
143 struct web_part *wp;
144 unsigned HOST_WIDE_INT undefined;
146 /* Indexed by UID. */
147 static struct visit_trace *visit_trace;
149 /* Per basic block we have one such structure, used to speed up
150 the backtracing of uses. */
151 struct ra_bb_info
153 /* The value of visited_pass, as the first insn of this BB was reached
154 the last time. If this equals the current visited_pass, then
155 undefined is valid. Otherwise not. */
156 unsigned int pass;
157 /* The still undefined bytes at that time. The use to which this is
158 relative is the current use. */
159 unsigned HOST_WIDE_INT undefined;
160 /* Bit regno is set, if that regno is mentioned in this BB as a def, or
161 the source of a copy insn. In these cases we can not skip the whole
162 block if we reach it from the end. */
163 bitmap regnos_mentioned;
164 /* If a use reaches the end of a BB, and that BB doesn't mention its
165 regno, we skip the block, and remember the ID of that use
166 as living throughout the whole block. */
167 bitmap live_throughout;
168 /* The content of the aux field before placing a pointer to this
169 structure there. */
170 void *old_aux;
173 /* We need a fast way to describe a certain part of a register.
174 Therefore we put together the size and offset (in bytes) in one
175 integer. */
176 #define BL_TO_WORD(b, l) ((((b) & 0xFFFF) << 16) | ((l) & 0xFFFF))
177 #define BYTE_BEGIN(i) (((unsigned int)(i) >> 16) & 0xFFFF)
178 #define BYTE_LENGTH(i) ((unsigned int)(i) & 0xFFFF)
180 /* For a REG or SUBREG expression X return the size/offset pair
181 as an integer. */
183 unsigned int
184 rtx_to_bits (x)
185 rtx x;
187 unsigned int len, beg;
188 len = GET_MODE_SIZE (GET_MODE (x));
189 beg = (GET_CODE (x) == SUBREG) ? SUBREG_BYTE (x) : 0;
190 return BL_TO_WORD (beg, len);
193 /* X is a REG or SUBREG rtx. Return the bytes it touches as a bitmask. */
195 static unsigned HOST_WIDE_INT
196 rtx_to_undefined (x)
197 rtx x;
199 unsigned int len, beg;
200 unsigned HOST_WIDE_INT ret;
201 len = GET_MODE_SIZE (GET_MODE (x));
202 beg = (GET_CODE (x) == SUBREG) ? SUBREG_BYTE (x) : 0;
203 ret = ~ ((unsigned HOST_WIDE_INT) 0);
204 ret = (~(ret << len)) << beg;
205 return ret;
208 /* We remember if we've analyzed an insn for being a move insn, and if yes
209 between which operands. */
210 struct copy_p_cache
212 int seen;
213 rtx source;
214 rtx target;
217 /* On demand cache, for if insns are copy insns, and if yes, what
218 source/target they have. */
219 static struct copy_p_cache * copy_cache;
221 int *number_seen;
223 /* For INSN, return nonzero, if it's a move insn, we consider to coalesce
224 later, and place the operands in *SOURCE and *TARGET, if they are
225 not NULL. */
227 static int
228 copy_insn_p (insn, source, target)
229 rtx insn;
230 rtx *source;
231 rtx *target;
233 rtx d, s;
234 unsigned int d_regno, s_regno;
235 int uid = INSN_UID (insn);
237 if (!INSN_P (insn))
238 abort ();
240 /* First look, if we already saw this insn. */
241 if (copy_cache[uid].seen)
243 /* And if we saw it, if it's actually a copy insn. */
244 if (copy_cache[uid].seen == 1)
246 if (source)
247 *source = copy_cache[uid].source;
248 if (target)
249 *target = copy_cache[uid].target;
250 return 1;
252 return 0;
255 /* Mark it as seen, but not being a copy insn. */
256 copy_cache[uid].seen = 2;
257 insn = single_set (insn);
258 if (!insn)
259 return 0;
260 d = SET_DEST (insn);
261 s = SET_SRC (insn);
263 /* We recognize moves between subreg's as copy insns. This is used to avoid
264 conflicts of those subwebs. But they are currently _not_ used for
265 coalescing (the check for this is in remember_move() below). */
266 while (GET_CODE (d) == STRICT_LOW_PART)
267 d = XEXP (d, 0);
268 if (GET_CODE (d) != REG
269 && (GET_CODE (d) != SUBREG || GET_CODE (SUBREG_REG (d)) != REG))
270 return 0;
271 while (GET_CODE (s) == STRICT_LOW_PART)
272 s = XEXP (s, 0);
273 if (GET_CODE (s) != REG
274 && (GET_CODE (s) != SUBREG || GET_CODE (SUBREG_REG (s)) != REG))
275 return 0;
277 s_regno = (unsigned) REGNO (GET_CODE (s) == SUBREG ? SUBREG_REG (s) : s);
278 d_regno = (unsigned) REGNO (GET_CODE (d) == SUBREG ? SUBREG_REG (d) : d);
280 /* Copies between hardregs are useless for us, as not coalesable anyway. */
281 if ((s_regno < FIRST_PSEUDO_REGISTER
282 && d_regno < FIRST_PSEUDO_REGISTER)
283 || s_regno >= max_normal_pseudo
284 || d_regno >= max_normal_pseudo)
285 return 0;
287 if (source)
288 *source = s;
289 if (target)
290 *target = d;
292 /* Still mark it as seen, but as a copy insn this time. */
293 copy_cache[uid].seen = 1;
294 copy_cache[uid].source = s;
295 copy_cache[uid].target = d;
296 return 1;
299 /* We build webs, as we process the conflicts. For each use we go upward
300 the insn stream, noting any defs as potentially conflicting with the
301 current use. We stop at defs of the current regno. The conflicts are only
302 potentially, because we may never reach a def, so this is an undefined use,
303 which conflicts with nothing. */
306 /* Given a web part WP, and the location of a reg part SIZE_WORD
307 return the conflict bitmap for that reg part, or NULL if it doesn't
308 exist yet in WP. */
310 static bitmap
311 find_sub_conflicts (wp, size_word)
312 struct web_part *wp;
313 unsigned int size_word;
315 struct tagged_conflict *cl;
316 cl = wp->sub_conflicts;
317 for (; cl; cl = cl->next)
318 if (cl->size_word == size_word)
319 return cl->conflicts;
320 return NULL;
323 /* Similar to find_sub_conflicts(), but creates that bitmap, if it
324 doesn't exist. I.e. this never returns NULL. */
326 static bitmap
327 get_sub_conflicts (wp, size_word)
328 struct web_part *wp;
329 unsigned int size_word;
331 bitmap b = find_sub_conflicts (wp, size_word);
332 if (!b)
334 struct tagged_conflict *cl = ra_alloc (sizeof *cl);
335 cl->conflicts = BITMAP_XMALLOC ();
336 cl->size_word = size_word;
337 cl->next = wp->sub_conflicts;
338 wp->sub_conflicts = cl;
339 b = cl->conflicts;
341 return b;
344 /* Helper table for undef_to_size_word() below for small values
345 of UNDEFINED. Offsets and lengths are byte based. */
346 static struct undef_table_s {
347 unsigned int new_undef;
348 /* size | (byte << 16) */
349 unsigned int size_word;
350 } const undef_table [] = {
351 { 0, BL_TO_WORD (0, 0)}, /* 0 */
352 { 0, BL_TO_WORD (0, 1)},
353 { 0, BL_TO_WORD (1, 1)},
354 { 0, BL_TO_WORD (0, 2)},
355 { 0, BL_TO_WORD (2, 1)}, /* 4 */
356 { 1, BL_TO_WORD (2, 1)},
357 { 2, BL_TO_WORD (2, 1)},
358 { 3, BL_TO_WORD (2, 1)},
359 { 0, BL_TO_WORD (3, 1)}, /* 8 */
360 { 1, BL_TO_WORD (3, 1)},
361 { 2, BL_TO_WORD (3, 1)},
362 { 3, BL_TO_WORD (3, 1)},
363 { 0, BL_TO_WORD (2, 2)}, /* 12 */
364 { 1, BL_TO_WORD (2, 2)},
365 { 2, BL_TO_WORD (2, 2)},
366 { 0, BL_TO_WORD (0, 4)}};
368 /* Interpret *UNDEFINED as bitmask where each bit corresponds to a byte.
369 A set bit means an undefined byte. Factor all undefined bytes into
370 groups, and return a size/ofs pair of consecutive undefined bytes,
371 but according to certain borders. Clear out those bits corresponding
372 to bytes overlaid by that size/ofs pair. REG is only used for
373 the mode, to detect if it's a floating mode or not.
375 For example: *UNDEFINED size+ofs new *UNDEFINED
376 1111 4+0 0
377 1100 2+2 0
378 1101 2+2 1
379 0001 1+0 0
380 10101 1+4 101
384 static unsigned int
385 undef_to_size_word (reg, undefined)
386 rtx reg;
387 unsigned HOST_WIDE_INT *undefined;
389 /* When only the lower four bits are possibly set, we use
390 a fast lookup table. */
391 if (*undefined <= 15)
393 struct undef_table_s u;
394 u = undef_table[*undefined];
395 *undefined = u.new_undef;
396 return u.size_word;
399 /* Otherwise we handle certain cases directly. */
400 if (*undefined <= 0xffff)
401 switch ((int) *undefined)
403 case 0x00f0 : *undefined = 0; return BL_TO_WORD (4, 4);
404 case 0x00ff : *undefined = 0; return BL_TO_WORD (0, 8);
405 case 0x0f00 : *undefined = 0; return BL_TO_WORD (8, 4);
406 case 0x0ff0 : *undefined = 0xf0; return BL_TO_WORD (8, 4);
407 case 0x0fff :
408 if (INTEGRAL_MODE_P (GET_MODE (reg)))
409 { *undefined = 0xff; return BL_TO_WORD (8, 4); }
410 else
411 { *undefined = 0; return BL_TO_WORD (0, 12); /* XFmode */ }
412 case 0xf000 : *undefined = 0; return BL_TO_WORD (12, 4);
413 case 0xff00 : *undefined = 0; return BL_TO_WORD (8, 8);
414 case 0xfff0 : *undefined = 0xf0; return BL_TO_WORD (8, 8);
415 case 0xffff : *undefined = 0; return BL_TO_WORD (0, 16);
418 /* And if nothing matched fall back to the general solution. For
419 now unknown undefined bytes are converted to sequences of maximal
420 length 4 bytes. We could make this larger if necessary. */
422 unsigned HOST_WIDE_INT u = *undefined;
423 int word;
424 struct undef_table_s tab;
425 for (word = 0; (u & 15) == 0; word += 4)
426 u >>= 4;
427 u = u & 15;
428 tab = undef_table[u];
429 u = tab.new_undef;
430 u = (*undefined & ~((unsigned HOST_WIDE_INT)15 << word)) | (u << word);
431 *undefined = u;
432 /* Size remains the same, only the begin is moved up move bytes. */
433 return tab.size_word + BL_TO_WORD (word, 0);
437 /* Put the above three functions together. For a set of undefined bytes
438 as bitmap *UNDEFINED, look for (create if necessary) and return the
439 corresponding conflict bitmap. Change *UNDEFINED to remove the bytes
440 covered by the part for that bitmap. */
442 static bitmap
443 undef_to_bitmap (wp, undefined)
444 struct web_part *wp;
445 unsigned HOST_WIDE_INT *undefined;
447 unsigned int size_word = undef_to_size_word (DF_REF_REAL_REG (wp->ref),
448 undefined);
449 return get_sub_conflicts (wp, size_word);
452 /* Returns the root of the web part P is a member of. Additionally
453 it compresses the path. P may not be NULL. */
455 static struct web_part *
456 find_web_part_1 (p)
457 struct web_part *p;
459 struct web_part *r = p;
460 struct web_part *p_next;
461 while (r->uplink)
462 r = r->uplink;
463 for (; p != r; p = p_next)
465 p_next = p->uplink;
466 p->uplink = r;
468 return r;
471 /* Fast macro for the common case (WP either being the root itself, or
472 the end of an already compressed path. */
474 #define find_web_part(wp) ((! (wp)->uplink) ? (wp) \
475 : (! (wp)->uplink->uplink) ? (wp)->uplink : find_web_part_1 (wp))
477 /* Unions together the parts R1 resp. R2 is a root of.
478 All dynamic information associated with the parts (number of spanned insns
479 and so on) is also merged.
480 The root of the resulting (possibly larger) web part is returned. */
482 static struct web_part *
483 union_web_part_roots (r1, r2)
484 struct web_part *r1, *r2;
486 if (r1 != r2)
488 /* The new root is the smaller (pointerwise) of both. This is crucial
489 to make the construction of webs from web parts work (so, when
490 scanning all parts, we see the roots before all its children).
491 Additionally this ensures, that if the web has a def at all, than
492 the root is a def (because all def parts are before use parts in the
493 web_parts[] array), or put another way, as soon, as the root of a
494 web part is not a def, this is an uninitialized web part. The
495 way we construct the I-graph ensures, that if a web is initialized,
496 then the first part we find (besides trivial 1 item parts) has a
497 def. */
498 if (r1 > r2)
500 struct web_part *h = r1;
501 r1 = r2;
502 r2 = h;
504 r2->uplink = r1;
505 num_webs--;
507 /* Now we merge the dynamic information of R1 and R2. */
508 r1->spanned_deaths += r2->spanned_deaths;
510 if (!r1->sub_conflicts)
511 r1->sub_conflicts = r2->sub_conflicts;
512 else if (r2->sub_conflicts)
513 /* We need to merge the conflict bitmaps from R2 into R1. */
515 struct tagged_conflict *cl1, *cl2;
516 /* First those from R2, which are also contained in R1.
517 We union the bitmaps, and free those from R2, resetting them
518 to 0. */
519 for (cl1 = r1->sub_conflicts; cl1; cl1 = cl1->next)
520 for (cl2 = r2->sub_conflicts; cl2; cl2 = cl2->next)
521 if (cl1->size_word == cl2->size_word)
523 bitmap_operation (cl1->conflicts, cl1->conflicts,
524 cl2->conflicts, BITMAP_IOR);
525 BITMAP_XFREE (cl2->conflicts);
526 cl2->conflicts = NULL;
528 /* Now the conflict lists from R2 which weren't in R1.
529 We simply copy the entries from R2 into R1' list. */
530 for (cl2 = r2->sub_conflicts; cl2;)
532 struct tagged_conflict *cl_next = cl2->next;
533 if (cl2->conflicts)
535 cl2->next = r1->sub_conflicts;
536 r1->sub_conflicts = cl2;
538 cl2 = cl_next;
541 r2->sub_conflicts = NULL;
542 r1->crosses_call |= r2->crosses_call;
544 return r1;
547 /* Convenience macro, that is capable of unioning also non-roots. */
548 #define union_web_parts(p1, p2) \
549 ((p1 == p2) ? find_web_part (p1) \
550 : union_web_part_roots (find_web_part (p1), find_web_part (p2)))
552 /* Remember that we've handled a given move, so we don't reprocess it. */
554 static void
555 remember_move (insn)
556 rtx insn;
558 if (!TEST_BIT (move_handled, INSN_UID (insn)))
560 rtx s, d;
561 SET_BIT (move_handled, INSN_UID (insn));
562 if (copy_insn_p (insn, &s, &d))
564 /* Some sanity test for the copy insn. */
565 struct df_link *slink = DF_INSN_USES (df, insn);
566 struct df_link *link = DF_INSN_DEFS (df, insn);
567 if (!link || !link->ref || !slink || !slink->ref)
568 abort ();
569 /* The following (link->next != 0) happens when a hardreg
570 is used in wider mode (REG:DI %eax). Then df.* creates
571 a def/use for each hardreg contained therein. We only
572 allow hardregs here. */
573 if (link->next
574 && DF_REF_REGNO (link->next->ref) >= FIRST_PSEUDO_REGISTER)
575 abort ();
577 else
578 abort ();
579 /* XXX for now we don't remember move insns involving any subregs.
580 Those would be difficult to coalesce (we would need to implement
581 handling of all the subwebs in the allocator, including that such
582 subwebs could be source and target of coalescing). */
583 if (GET_CODE (s) == REG && GET_CODE (d) == REG)
585 struct move *m = ra_calloc (sizeof (struct move));
586 struct move_list *ml;
587 m->insn = insn;
588 ml = ra_alloc (sizeof (struct move_list));
589 ml->move = m;
590 ml->next = wl_moves;
591 wl_moves = ml;
596 /* This describes the USE currently looked at in the main-loop in
597 build_web_parts_and_conflicts(). */
598 struct curr_use {
599 struct web_part *wp;
600 /* This has a 1-bit for each byte in the USE, which is still undefined. */
601 unsigned HOST_WIDE_INT undefined;
602 /* For easy access. */
603 unsigned int regno;
604 rtx x;
605 /* If some bits of this USE are live over an abnormal edge. */
606 unsigned int live_over_abnormal;
609 /* Returns nonzero iff rtx DEF and USE have bits in common (but see below).
610 It is only called with DEF and USE being (reg:M a) or (subreg:M1 (reg:M2 a)
611 x) rtx's. Furthermore if it's a subreg rtx M1 is at least one word wide,
612 and a is a multi-word pseudo. If DEF or USE are hardregs, they are in
613 word_mode, so we don't need to check for further hardregs which would result
614 from wider references. We are never called with paradoxical subregs.
616 This returns:
617 0 for no common bits,
618 1 if DEF and USE exactly cover the same bytes,
619 2 if the DEF only covers a part of the bits of USE
620 3 if the DEF covers more than the bits of the USE, and
621 4 if both are SUBREG's of different size, but have bytes in common.
622 -1 is a special case, for when DEF and USE refer to the same regno, but
623 have for other reasons no bits in common (can only happen with
624 subregs referring to different words, or to words which already were
625 defined for this USE).
626 Furthermore it modifies use->undefined to clear the bits which get defined
627 by DEF (only for cases with partial overlap).
628 I.e. if bit 1 is set for the result != -1, the USE was completely covered,
629 otherwise a test is needed to track the already defined bytes. */
631 static int
632 defuse_overlap_p_1 (def, use)
633 rtx def;
634 struct curr_use *use;
636 int mode = 0;
637 if (def == use->x)
638 return 1;
639 if (!def)
640 return 0;
641 if (GET_CODE (def) == SUBREG)
643 if (REGNO (SUBREG_REG (def)) != use->regno)
644 return 0;
645 mode |= 1;
647 else if (REGNO (def) != use->regno)
648 return 0;
649 if (GET_CODE (use->x) == SUBREG)
650 mode |= 2;
651 switch (mode)
653 case 0: /* REG, REG */
654 return 1;
655 case 1: /* SUBREG, REG */
657 unsigned HOST_WIDE_INT old_u = use->undefined;
658 use->undefined &= ~ rtx_to_undefined (def);
659 return (old_u != use->undefined) ? 2 : -1;
661 case 2: /* REG, SUBREG */
662 return 3;
663 case 3: /* SUBREG, SUBREG */
664 if (GET_MODE_SIZE (GET_MODE (def)) == GET_MODE_SIZE (GET_MODE (use->x)))
665 /* If the size of both things is the same, the subreg's overlap
666 if they refer to the same word. */
667 if (SUBREG_BYTE (def) == SUBREG_BYTE (use->x))
668 return 1;
669 /* Now the more difficult part: the same regno is referred, but the
670 sizes of the references or the words differ. E.g.
671 (subreg:SI (reg:CDI a) 0) and (subreg:DI (reg:CDI a) 2) do not
672 overlap, whereas the latter overlaps with (subreg:SI (reg:CDI a) 3).
675 unsigned HOST_WIDE_INT old_u;
676 int b1, e1, b2, e2;
677 unsigned int bl1, bl2;
678 bl1 = rtx_to_bits (def);
679 bl2 = rtx_to_bits (use->x);
680 b1 = BYTE_BEGIN (bl1);
681 b2 = BYTE_BEGIN (bl2);
682 e1 = b1 + BYTE_LENGTH (bl1) - 1;
683 e2 = b2 + BYTE_LENGTH (bl2) - 1;
684 if (b1 > e2 || b2 > e1)
685 return -1;
686 old_u = use->undefined;
687 use->undefined &= ~ rtx_to_undefined (def);
688 return (old_u != use->undefined) ? 4 : -1;
690 default:
691 abort ();
695 /* Macro for the common case of either def and use having the same rtx,
696 or based on different regnos. */
697 #define defuse_overlap_p(def, use) \
698 ((def) == (use)->x ? 1 : \
699 (REGNO (GET_CODE (def) == SUBREG \
700 ? SUBREG_REG (def) : def) != use->regno \
701 ? 0 : defuse_overlap_p_1 (def, use)))
704 /* The use USE flows into INSN (backwards). Determine INSNs effect on it,
705 and return nonzero, if (parts of) that USE are also live before it.
706 This also notes conflicts between the USE and all DEFS in that insn,
707 and modifies the undefined bits of USE in case parts of it were set in
708 this insn. */
710 static int
711 live_out_1 (df, use, insn)
712 struct df *df ATTRIBUTE_UNUSED;
713 struct curr_use *use;
714 rtx insn;
716 int defined = 0;
717 int uid = INSN_UID (insn);
718 struct web_part *wp = use->wp;
720 /* Mark, that this insn needs this webpart live. */
721 visit_trace[uid].wp = wp;
722 visit_trace[uid].undefined = use->undefined;
724 if (INSN_P (insn))
726 unsigned int source_regno = ~0;
727 unsigned int regno = use->regno;
728 unsigned HOST_WIDE_INT orig_undef = use->undefined;
729 unsigned HOST_WIDE_INT final_undef = use->undefined;
730 rtx s = NULL;
731 unsigned int n, num_defs = insn_df[uid].num_defs;
732 struct ref **defs = insn_df[uid].defs;
734 /* We want to access the root webpart. */
735 wp = find_web_part (wp);
736 if (GET_CODE (insn) == CALL_INSN)
737 wp->crosses_call = 1;
738 else if (copy_insn_p (insn, &s, NULL))
739 source_regno = REGNO (GET_CODE (s) == SUBREG ? SUBREG_REG (s) : s);
741 /* Look at all DEFS in this insn. */
742 for (n = 0; n < num_defs; n++)
744 struct ref *ref = defs[n];
745 int lap;
747 /* Reset the undefined bits for each iteration, in case this
748 insn has more than one set, and one of them sets this regno.
749 But still the original undefined part conflicts with the other
750 sets. */
751 use->undefined = orig_undef;
752 if ((lap = defuse_overlap_p (DF_REF_REG (ref), use)) != 0)
754 if (lap == -1)
755 /* Same regnos but non-overlapping or already defined bits,
756 so ignore this DEF, or better said make the yet undefined
757 part and this DEF conflicting. */
759 unsigned HOST_WIDE_INT undef;
760 undef = use->undefined;
761 while (undef)
762 bitmap_set_bit (undef_to_bitmap (wp, &undef),
763 DF_REF_ID (ref));
764 continue;
766 if ((lap & 1) != 0)
767 /* The current DEF completely covers the USE, so we can
768 stop traversing the code looking for further DEFs. */
769 defined = 1;
770 else
771 /* We have a partial overlap. */
773 final_undef &= use->undefined;
774 if (final_undef == 0)
775 /* Now the USE is completely defined, which means, that
776 we can stop looking for former DEFs. */
777 defined = 1;
778 /* If this is a partial overlap, which left some bits
779 in USE undefined, we normally would need to create
780 conflicts between that undefined part and the part of
781 this DEF which overlapped with some of the formerly
782 undefined bits. We don't need to do this, because both
783 parts of this DEF (that which overlaps, and that which
784 doesn't) are written together in this one DEF, and can
785 not be colored in a way which would conflict with
786 the USE. This is only true for partial overlap,
787 because only then the DEF and USE have bits in common,
788 which makes the DEF move, if the USE moves, making them
789 aligned.
790 If they have no bits in common (lap == -1), they are
791 really independent. Therefore we there made a
792 conflict above. */
794 /* This is at least a partial overlap, so we need to union
795 the web parts. */
796 wp = union_web_parts (wp, &web_parts[DF_REF_ID (ref)]);
798 else
800 /* The DEF and the USE don't overlap at all, different
801 regnos. I.e. make conflicts between the undefined bits,
802 and that DEF. */
803 unsigned HOST_WIDE_INT undef = use->undefined;
805 if (regno == source_regno)
806 /* This triggers only, when this was a copy insn and the
807 source is at least a part of the USE currently looked at.
808 In this case only the bits of the USE conflict with the
809 DEF, which are not covered by the source of this copy
810 insn, and which are still undefined. I.e. in the best
811 case (the whole reg being the source), _no_ conflicts
812 between that USE and this DEF (the target of the move)
813 are created by this insn (though they might be by
814 others). This is a super case of the normal copy insn
815 only between full regs. */
817 undef &= ~ rtx_to_undefined (s);
819 if (undef)
821 /*struct web_part *cwp;
822 cwp = find_web_part (&web_parts[DF_REF_ID
823 (ref)]);*/
825 /* TODO: somehow instead of noting the ID of the LINK
826 use an ID nearer to the root webpart of that LINK.
827 We can't use the root itself, because we later use the
828 ID to look at the form (reg or subreg, and if yes,
829 which subreg) of this conflict. This means, that we
830 need to remember in the root an ID for each form, and
831 maintaining this, when merging web parts. This makes
832 the bitmaps smaller. */
834 bitmap_set_bit (undef_to_bitmap (wp, &undef),
835 DF_REF_ID (ref));
836 while (undef);
840 if (defined)
841 use->undefined = 0;
842 else
844 /* If this insn doesn't completely define the USE, increment also
845 it's spanned deaths count (if this insn contains a death). */
846 if (uid >= death_insns_max_uid)
847 abort ();
848 if (TEST_BIT (insns_with_deaths, uid))
849 wp->spanned_deaths++;
850 use->undefined = final_undef;
854 return !defined;
857 /* Same as live_out_1() (actually calls it), but caches some information.
858 E.g. if we reached this INSN with the current regno already, and the
859 current undefined bits are a subset of those as we came here, we
860 simply connect the web parts of the USE, and the one cached for this
861 INSN, and additionally return zero, indicating we don't need to traverse
862 this path any longer (all effect were already seen, as we first reached
863 this insn). */
865 static inline int
866 live_out (df, use, insn)
867 struct df *df;
868 struct curr_use *use;
869 rtx insn;
871 unsigned int uid = INSN_UID (insn);
872 if (visit_trace[uid].wp
873 && DF_REF_REGNO (visit_trace[uid].wp->ref) == use->regno
874 && (use->undefined & ~visit_trace[uid].undefined) == 0)
876 union_web_parts (visit_trace[uid].wp, use->wp);
877 /* Don't search any further, as we already were here with this regno. */
878 return 0;
880 else
881 return live_out_1 (df, use, insn);
884 /* The current USE reached a basic block head. The edge E is one
885 of the predecessors edges. This evaluates the effect of the predecessor
886 block onto the USE, and returns the next insn, which should be looked at.
887 This either is the last insn of that pred. block, or the first one.
888 The latter happens, when the pred. block has no possible effect on the
889 USE, except for conflicts. In that case, it's remembered, that the USE
890 is live over that whole block, and it's skipped. Otherwise we simply
891 continue with the last insn of the block.
893 This also determines the effects of abnormal edges, and remembers
894 which uses are live at the end of that basic block. */
896 static rtx
897 live_in_edge (df, use, e)
898 struct df *df;
899 struct curr_use *use;
900 edge e;
902 struct ra_bb_info *info_pred;
903 rtx next_insn;
904 /* Call used hard regs die over an exception edge, ergo
905 they don't reach the predecessor block, so ignore such
906 uses. And also don't set the live_over_abnormal flag
907 for them. */
908 if ((e->flags & EDGE_EH) && use->regno < FIRST_PSEUDO_REGISTER
909 && call_used_regs[use->regno])
910 return NULL_RTX;
911 if (e->flags & EDGE_ABNORMAL)
912 use->live_over_abnormal = 1;
913 bitmap_set_bit (live_at_end[e->src->index], DF_REF_ID (use->wp->ref));
914 info_pred = (struct ra_bb_info *) e->src->aux;
915 next_insn = e->src->end;
917 /* If the last insn of the pred. block doesn't completely define the
918 current use, we need to check the block. */
919 if (live_out (df, use, next_insn))
921 /* If the current regno isn't mentioned anywhere in the whole block,
922 and the complete use is still undefined... */
923 if (!bitmap_bit_p (info_pred->regnos_mentioned, use->regno)
924 && (rtx_to_undefined (use->x) & ~use->undefined) == 0)
926 /* ...we can hop over the whole block and defer conflict
927 creation to later. */
928 bitmap_set_bit (info_pred->live_throughout,
929 DF_REF_ID (use->wp->ref));
930 next_insn = e->src->head;
932 return next_insn;
934 else
935 return NULL_RTX;
938 /* USE flows into the end of the insns preceding INSN. Determine
939 their effects (in live_out()) and possibly loop over the preceding INSN,
940 or call itself recursively on a basic block border. When a topleve
941 call of this function returns the USE is completely analyzed. I.e.
942 its def-use chain (at least) is built, possibly connected with other
943 def-use chains, and all defs during that chain are noted. */
945 static void
946 live_in (df, use, insn)
947 struct df *df;
948 struct curr_use *use;
949 rtx insn;
951 unsigned int loc_vpass = visited_pass;
953 /* Note, that, even _if_ we are called with use->wp a root-part, this might
954 become non-root in the for() loop below (due to live_out() unioning
955 it). So beware, not to change use->wp in a way, for which only root-webs
956 are allowed. */
957 while (1)
959 int uid = INSN_UID (insn);
960 basic_block bb = BLOCK_FOR_INSN (insn);
961 number_seen[uid]++;
963 /* We want to be as fast as possible, so explicitly write
964 this loop. */
965 for (insn = PREV_INSN (insn); insn && !INSN_P (insn);
966 insn = PREV_INSN (insn))
968 if (!insn)
969 return;
970 if (bb != BLOCK_FOR_INSN (insn))
972 edge e;
973 unsigned HOST_WIDE_INT undef = use->undefined;
974 struct ra_bb_info *info = (struct ra_bb_info *) bb->aux;
975 if ((e = bb->pred) == NULL)
976 return;
977 /* We now check, if we already traversed the predecessors of this
978 block for the current pass and the current set of undefined
979 bits. If yes, we don't need to check the predecessors again.
980 So, conceptually this information is tagged to the first
981 insn of a basic block. */
982 if (info->pass == loc_vpass && (undef & ~info->undefined) == 0)
983 return;
984 info->pass = loc_vpass;
985 info->undefined = undef;
986 /* All but the last predecessor are handled recursively. */
987 for (; e->pred_next; e = e->pred_next)
989 insn = live_in_edge (df, use, e);
990 if (insn)
991 live_in (df, use, insn);
992 use->undefined = undef;
994 insn = live_in_edge (df, use, e);
995 if (!insn)
996 return;
998 else if (!live_out (df, use, insn))
999 return;
1003 /* Determine all regnos which are mentioned in a basic block, in an
1004 interesting way. Interesting here means either in a def, or as the
1005 source of a move insn. We only look at insns added since the last
1006 pass. */
1008 static void
1009 update_regnos_mentioned ()
1011 int last_uid = last_max_uid;
1012 rtx insn;
1013 basic_block bb;
1014 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1015 if (INSN_P (insn))
1017 /* Don't look at old insns. */
1018 if (INSN_UID (insn) < last_uid)
1020 /* XXX We should also remember moves over iterations (we already
1021 save the cache, but not the movelist). */
1022 if (copy_insn_p (insn, NULL, NULL))
1023 remember_move (insn);
1025 else if ((bb = BLOCK_FOR_INSN (insn)) != NULL)
1027 rtx source;
1028 struct ra_bb_info *info = (struct ra_bb_info *) bb->aux;
1029 bitmap mentioned = info->regnos_mentioned;
1030 struct df_link *link;
1031 if (copy_insn_p (insn, &source, NULL))
1033 remember_move (insn);
1034 bitmap_set_bit (mentioned,
1035 REGNO (GET_CODE (source) == SUBREG
1036 ? SUBREG_REG (source) : source));
1038 for (link = DF_INSN_DEFS (df, insn); link; link = link->next)
1039 if (link->ref)
1040 bitmap_set_bit (mentioned, DF_REF_REGNO (link->ref));
1045 /* Handle the uses which reach a block end, but were deferred due
1046 to it's regno not being mentioned in that block. This adds the
1047 remaining conflicts and updates also the crosses_call and
1048 spanned_deaths members. */
1050 static void
1051 livethrough_conflicts_bb (bb)
1052 basic_block bb;
1054 struct ra_bb_info *info = (struct ra_bb_info *) bb->aux;
1055 rtx insn;
1056 bitmap all_defs;
1057 int first, use_id;
1058 unsigned int deaths = 0;
1059 unsigned int contains_call = 0;
1061 /* If there are no deferred uses, just return. */
1062 if ((first = bitmap_first_set_bit (info->live_throughout)) < 0)
1063 return;
1065 /* First collect the IDs of all defs, count the number of death
1066 containing insns, and if there's some call_insn here. */
1067 all_defs = BITMAP_XMALLOC ();
1068 for (insn = bb->head; insn; insn = NEXT_INSN (insn))
1070 if (INSN_P (insn))
1072 unsigned int n;
1073 struct ra_insn_info info;
1075 info = insn_df[INSN_UID (insn)];
1076 for (n = 0; n < info.num_defs; n++)
1077 bitmap_set_bit (all_defs, DF_REF_ID (info.defs[n]));
1078 if (TEST_BIT (insns_with_deaths, INSN_UID (insn)))
1079 deaths++;
1080 if (GET_CODE (insn) == CALL_INSN)
1081 contains_call = 1;
1083 if (insn == bb->end)
1084 break;
1087 /* And now, if we have found anything, make all live_through
1088 uses conflict with all defs, and update their other members. */
1089 if (deaths > 0 || bitmap_first_set_bit (all_defs) >= 0)
1090 EXECUTE_IF_SET_IN_BITMAP (info->live_throughout, first, use_id,
1092 struct web_part *wp = &web_parts[df->def_id + use_id];
1093 unsigned int bl = rtx_to_bits (DF_REF_REG (wp->ref));
1094 bitmap conflicts;
1095 wp = find_web_part (wp);
1096 wp->spanned_deaths += deaths;
1097 wp->crosses_call |= contains_call;
1098 conflicts = get_sub_conflicts (wp, bl);
1099 bitmap_operation (conflicts, conflicts, all_defs, BITMAP_IOR);
1102 BITMAP_XFREE (all_defs);
1105 /* Allocate the per basic block info for traversing the insn stream for
1106 building live ranges. */
1108 static void
1109 init_bb_info ()
1111 basic_block bb;
1112 FOR_ALL_BB (bb)
1114 struct ra_bb_info *info = xcalloc (1, sizeof *info);
1115 info->regnos_mentioned = BITMAP_XMALLOC ();
1116 info->live_throughout = BITMAP_XMALLOC ();
1117 info->old_aux = bb->aux;
1118 bb->aux = (void *) info;
1122 /* Free that per basic block info. */
1124 static void
1125 free_bb_info ()
1127 basic_block bb;
1128 FOR_ALL_BB (bb)
1130 struct ra_bb_info *info = (struct ra_bb_info *) bb->aux;
1131 BITMAP_XFREE (info->regnos_mentioned);
1132 BITMAP_XFREE (info->live_throughout);
1133 bb->aux = info->old_aux;
1134 free (info);
1138 /* Toplevel function for the first part of this file.
1139 Connect web parts, thereby implicitly building webs, and remember
1140 their conflicts. */
1142 static void
1143 build_web_parts_and_conflicts (df)
1144 struct df *df;
1146 struct df_link *link;
1147 struct curr_use use;
1148 basic_block bb;
1150 number_seen = xcalloc (get_max_uid (), sizeof (int));
1151 visit_trace = xcalloc (get_max_uid (), sizeof (visit_trace[0]));
1152 update_regnos_mentioned ();
1154 /* Here's the main loop.
1155 It goes through all insn's, connects web parts along the way, notes
1156 conflicts between webparts, and remembers move instructions. */
1157 visited_pass = 0;
1158 for (use.regno = 0; use.regno < (unsigned int)max_regno; use.regno++)
1159 if (use.regno >= FIRST_PSEUDO_REGISTER || !fixed_regs[use.regno])
1160 for (link = df->regs[use.regno].uses; link; link = link->next)
1161 if (link->ref)
1163 struct ref *ref = link->ref;
1164 rtx insn = DF_REF_INSN (ref);
1165 /* Only recheck marked or new uses, or uses from hardregs. */
1166 if (use.regno >= FIRST_PSEUDO_REGISTER
1167 && DF_REF_ID (ref) < last_use_id
1168 && !TEST_BIT (last_check_uses, DF_REF_ID (ref)))
1169 continue;
1170 use.wp = &web_parts[df->def_id + DF_REF_ID (ref)];
1171 use.x = DF_REF_REG (ref);
1172 use.live_over_abnormal = 0;
1173 use.undefined = rtx_to_undefined (use.x);
1174 visited_pass++;
1175 live_in (df, &use, insn);
1176 if (use.live_over_abnormal)
1177 SET_BIT (live_over_abnormal, DF_REF_ID (ref));
1180 dump_number_seen ();
1181 FOR_ALL_BB (bb)
1183 struct ra_bb_info *info = (struct ra_bb_info *) bb->aux;
1184 livethrough_conflicts_bb (bb);
1185 bitmap_zero (info->live_throughout);
1186 info->pass = 0;
1188 free (visit_trace);
1189 free (number_seen);
1192 /* Here we look per insn, for DF references being in uses _and_ defs.
1193 This means, in the RTL a (REG xx) expression was seen as a
1194 read/modify/write, as happens for (set (subreg:SI (reg:DI xx)) (...))
1195 e.g. Our code has created two webs for this, as it should. Unfortunately,
1196 as the REG reference is only one time in the RTL we can't color
1197 both webs different (arguably this also would be wrong for a real
1198 read-mod-write instruction), so we must reconnect such webs. */
1200 static void
1201 connect_rmw_web_parts (df)
1202 struct df *df;
1204 unsigned int i;
1206 for (i = 0; i < df->use_id; i++)
1208 struct web_part *wp1 = &web_parts[df->def_id + i];
1209 rtx reg;
1210 struct df_link *link;
1211 if (!wp1->ref)
1212 continue;
1213 /* If it's an uninitialized web, we don't want to connect it to others,
1214 as the read cycle in read-mod-write had probably no effect. */
1215 if (find_web_part (wp1) >= &web_parts[df->def_id])
1216 continue;
1217 reg = DF_REF_REAL_REG (wp1->ref);
1218 link = DF_INSN_DEFS (df, DF_REF_INSN (wp1->ref));
1219 for (; link; link = link->next)
1220 if (reg == DF_REF_REAL_REG (link->ref))
1222 struct web_part *wp2 = &web_parts[DF_REF_ID (link->ref)];
1223 union_web_parts (wp1, wp2);
1228 /* Deletes all hardregs from *S which are not allowed for MODE. */
1230 static void
1231 prune_hardregs_for_mode (s, mode)
1232 HARD_REG_SET *s;
1233 enum machine_mode mode;
1235 AND_HARD_REG_SET (*s, hardregs_for_mode[(int) mode]);
1238 /* Initialize the members of a web, which are deducible from REG. */
1240 static void
1241 init_one_web_common (web, reg)
1242 struct web *web;
1243 rtx reg;
1245 if (GET_CODE (reg) != REG)
1246 abort ();
1247 /* web->id isn't initialized here. */
1248 web->regno = REGNO (reg);
1249 web->orig_x = reg;
1250 if (!web->dlink)
1252 web->dlink = ra_calloc (sizeof (struct dlist));
1253 DLIST_WEB (web->dlink) = web;
1255 /* XXX
1256 the former (superunion) doesn't constrain the graph enough. E.g.
1257 on x86 QImode _requires_ QI_REGS, but as alternate class usually
1258 GENERAL_REGS is given. So the graph is not constrained enough,
1259 thinking it has more freedom then it really has, which leads
1260 to repeated spill tryings. OTOH the latter (only using preferred
1261 class) is too constrained, as normally (e.g. with all SImode
1262 pseudos), they can be allocated also in the alternate class.
1263 What we really want, are the _exact_ hard regs allowed, not
1264 just a class. Later. */
1265 /*web->regclass = reg_class_superunion
1266 [reg_preferred_class (web->regno)]
1267 [reg_alternate_class (web->regno)];*/
1268 /*web->regclass = reg_preferred_class (web->regno);*/
1269 web->regclass = reg_class_subunion
1270 [reg_preferred_class (web->regno)] [reg_alternate_class (web->regno)];
1271 web->regclass = reg_preferred_class (web->regno);
1272 if (web->regno < FIRST_PSEUDO_REGISTER)
1274 web->color = web->regno;
1275 put_web (web, PRECOLORED);
1276 web->num_conflicts = UINT_MAX;
1277 web->add_hardregs = 0;
1278 CLEAR_HARD_REG_SET (web->usable_regs);
1279 SET_HARD_REG_BIT (web->usable_regs, web->regno);
1280 web->num_freedom = 1;
1282 else
1284 HARD_REG_SET alternate;
1285 web->color = -1;
1286 put_web (web, INITIAL);
1287 /* add_hardregs is wrong in multi-length classes, e.g.
1288 using a DFmode pseudo on x86 can result in class FLOAT_INT_REGS,
1289 where, if it finally is allocated to GENERAL_REGS it needs two,
1290 if allocated to FLOAT_REGS only one hardreg. XXX */
1291 web->add_hardregs =
1292 CLASS_MAX_NREGS (web->regclass, PSEUDO_REGNO_MODE (web->regno)) - 1;
1293 web->num_conflicts = 0 * web->add_hardregs;
1294 COPY_HARD_REG_SET (web->usable_regs,
1295 reg_class_contents[reg_preferred_class (web->regno)]);
1296 COPY_HARD_REG_SET (alternate,
1297 reg_class_contents[reg_alternate_class (web->regno)]);
1298 IOR_HARD_REG_SET (web->usable_regs, alternate);
1299 /*IOR_HARD_REG_SET (web->usable_regs,
1300 reg_class_contents[reg_alternate_class
1301 (web->regno)]);*/
1302 AND_COMPL_HARD_REG_SET (web->usable_regs, never_use_colors);
1303 prune_hardregs_for_mode (&web->usable_regs,
1304 PSEUDO_REGNO_MODE (web->regno));
1305 #ifdef CANNOT_CHANGE_MODE_CLASS
1306 if (web->mode_changed)
1307 AND_COMPL_HARD_REG_SET (web->usable_regs, invalid_mode_change_regs);
1308 #endif
1309 web->num_freedom = hard_regs_count (web->usable_regs);
1310 web->num_freedom -= web->add_hardregs;
1311 if (!web->num_freedom)
1312 abort();
1314 COPY_HARD_REG_SET (web->orig_usable_regs, web->usable_regs);
1317 /* Initializes WEBs members from REG or zero them. */
1319 static void
1320 init_one_web (web, reg)
1321 struct web *web;
1322 rtx reg;
1324 memset (web, 0, sizeof (struct web));
1325 init_one_web_common (web, reg);
1326 web->useless_conflicts = BITMAP_XMALLOC ();
1329 /* WEB is an old web, meaning it came from the last pass, and got a
1330 color. We want to remember some of it's info, so zero only some
1331 members. */
1333 static void
1334 reinit_one_web (web, reg)
1335 struct web *web;
1336 rtx reg;
1338 web->old_color = web->color + 1;
1339 init_one_web_common (web, reg);
1340 web->span_deaths = 0;
1341 web->spill_temp = 0;
1342 web->orig_spill_temp = 0;
1343 web->use_my_regs = 0;
1344 web->spill_cost = 0;
1345 web->was_spilled = 0;
1346 web->is_coalesced = 0;
1347 web->artificial = 0;
1348 web->live_over_abnormal = 0;
1349 web->mode_changed = 0;
1350 web->subreg_stripped = 0;
1351 web->move_related = 0;
1352 web->in_load = 0;
1353 web->target_of_spilled_move = 0;
1354 web->num_aliased = 0;
1355 if (web->type == PRECOLORED)
1357 web->num_defs = 0;
1358 web->num_uses = 0;
1359 web->orig_spill_cost = 0;
1361 CLEAR_HARD_REG_SET (web->bias_colors);
1362 CLEAR_HARD_REG_SET (web->prefer_colors);
1363 web->reg_rtx = NULL;
1364 web->stack_slot = NULL;
1365 web->pattern = NULL;
1366 web->alias = NULL;
1367 if (web->moves)
1368 abort ();
1369 if (!web->useless_conflicts)
1370 abort ();
1373 /* Insert and returns a subweb corresponding to REG into WEB (which
1374 becomes its super web). It must not exist already. */
1376 static struct web *
1377 add_subweb (web, reg)
1378 struct web *web;
1379 rtx reg;
1381 struct web *w;
1382 if (GET_CODE (reg) != SUBREG)
1383 abort ();
1384 w = xmalloc (sizeof (struct web));
1385 /* Copy most content from parent-web. */
1386 *w = *web;
1387 /* And initialize the private stuff. */
1388 w->orig_x = reg;
1389 w->add_hardregs = CLASS_MAX_NREGS (web->regclass, GET_MODE (reg)) - 1;
1390 w->num_conflicts = 0 * w->add_hardregs;
1391 w->num_defs = 0;
1392 w->num_uses = 0;
1393 w->dlink = NULL;
1394 w->parent_web = web;
1395 w->subreg_next = web->subreg_next;
1396 web->subreg_next = w;
1397 return w;
1400 /* Similar to add_subweb(), but instead of relying on a given SUBREG,
1401 we have just a size and an offset of the subpart of the REG rtx.
1402 In difference to add_subweb() this marks the new subweb as artificial. */
1404 static struct web *
1405 add_subweb_2 (web, size_word)
1406 struct web *web;
1407 unsigned int size_word;
1409 /* To get a correct mode for the to be produced subreg, we don't want to
1410 simply do a mode_for_size() for the mode_class of the whole web.
1411 Suppose we deal with a CDImode web, but search for a 8 byte part.
1412 Now mode_for_size() would only search in the class MODE_COMPLEX_INT
1413 and would find CSImode which probably is not what we want. Instead
1414 we want DImode, which is in a completely other class. For this to work
1415 we instead first search the already existing subwebs, and take
1416 _their_ modeclasses as base for a search for ourself. */
1417 rtx ref_rtx = (web->subreg_next ? web->subreg_next : web)->orig_x;
1418 unsigned int size = BYTE_LENGTH (size_word) * BITS_PER_UNIT;
1419 enum machine_mode mode;
1420 mode = mode_for_size (size, GET_MODE_CLASS (GET_MODE (ref_rtx)), 0);
1421 if (mode == BLKmode)
1422 mode = mode_for_size (size, MODE_INT, 0);
1423 if (mode == BLKmode)
1424 abort ();
1425 web = add_subweb (web, gen_rtx_SUBREG (mode, web->orig_x,
1426 BYTE_BEGIN (size_word)));
1427 web->artificial = 1;
1428 return web;
1431 /* Initialize all the web parts we are going to need. */
1433 static void
1434 init_web_parts (df)
1435 struct df *df;
1437 int regno;
1438 unsigned int no;
1439 num_webs = 0;
1440 for (no = 0; no < df->def_id; no++)
1442 if (df->defs[no])
1444 if (no < last_def_id && web_parts[no].ref != df->defs[no])
1445 abort ();
1446 web_parts[no].ref = df->defs[no];
1447 /* Uplink might be set from the last iteration. */
1448 if (!web_parts[no].uplink)
1449 num_webs++;
1451 else
1452 /* The last iteration might have left .ref set, while df_analyse()
1453 removed that ref (due to a removed copy insn) from the df->defs[]
1454 array. As we don't check for that in realloc_web_parts()
1455 we do that here. */
1456 web_parts[no].ref = NULL;
1458 for (no = 0; no < df->use_id; no++)
1460 if (df->uses[no])
1462 if (no < last_use_id
1463 && web_parts[no + df->def_id].ref != df->uses[no])
1464 abort ();
1465 web_parts[no + df->def_id].ref = df->uses[no];
1466 if (!web_parts[no + df->def_id].uplink)
1467 num_webs++;
1469 else
1470 web_parts[no + df->def_id].ref = NULL;
1473 /* We want to have only one web for each precolored register. */
1474 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1476 struct web_part *r1 = NULL;
1477 struct df_link *link;
1478 /* Here once was a test, if there is any DEF at all, and only then to
1479 merge all the parts. This was incorrect, we really also want to have
1480 only one web-part for hardregs, even if there is no explicit DEF. */
1481 /* Link together all defs... */
1482 for (link = df->regs[regno].defs; link; link = link->next)
1483 if (link->ref)
1485 struct web_part *r2 = &web_parts[DF_REF_ID (link->ref)];
1486 if (!r1)
1487 r1 = r2;
1488 else
1489 r1 = union_web_parts (r1, r2);
1491 /* ... and all uses. */
1492 for (link = df->regs[regno].uses; link; link = link->next)
1493 if (link->ref)
1495 struct web_part *r2 = &web_parts[df->def_id
1496 + DF_REF_ID (link->ref)];
1497 if (!r1)
1498 r1 = r2;
1499 else
1500 r1 = union_web_parts (r1, r2);
1505 /* In case we want to remember the conflict list of a WEB, before adding
1506 new conflicts, we copy it here to orig_conflict_list. */
1508 static void
1509 copy_conflict_list (web)
1510 struct web *web;
1512 struct conflict_link *cl;
1513 if (web->orig_conflict_list || web->have_orig_conflicts)
1514 abort ();
1515 web->have_orig_conflicts = 1;
1516 for (cl = web->conflict_list; cl; cl = cl->next)
1518 struct conflict_link *ncl;
1519 ncl = ra_alloc (sizeof *ncl);
1520 ncl->t = cl->t;
1521 ncl->sub = NULL;
1522 ncl->next = web->orig_conflict_list;
1523 web->orig_conflict_list = ncl;
1524 if (cl->sub)
1526 struct sub_conflict *sl, *nsl;
1527 for (sl = cl->sub; sl; sl = sl->next)
1529 nsl = ra_alloc (sizeof *nsl);
1530 nsl->s = sl->s;
1531 nsl->t = sl->t;
1532 nsl->next = ncl->sub;
1533 ncl->sub = nsl;
1539 /* Possibly add an edge from web FROM to TO marking a conflict between
1540 those two. This is one half of marking a complete conflict, which notes
1541 in FROM, that TO is a conflict. Adding TO to FROM's conflicts might
1542 make other conflicts superfluous, because the current TO overlaps some web
1543 already being in conflict with FROM. In this case the smaller webs are
1544 deleted from the conflict list. Likewise if TO is overlapped by a web
1545 already in the list, it isn't added at all. Note, that this can only
1546 happen, if SUBREG webs are involved. */
1548 static void
1549 add_conflict_edge (from, to)
1550 struct web *from, *to;
1552 if (from->type != PRECOLORED)
1554 struct web *pfrom = find_web_for_subweb (from);
1555 struct web *pto = find_web_for_subweb (to);
1556 struct sub_conflict *sl;
1557 struct conflict_link *cl = pfrom->conflict_list;
1558 int may_delete = 1;
1560 /* This can happen when subwebs of one web conflict with each
1561 other. In live_out_1() we created such conflicts between yet
1562 undefined webparts and defs of parts which didn't overlap with the
1563 undefined bits. Then later they nevertheless could have merged into
1564 one web, and then we land here. */
1565 if (pfrom == pto)
1566 return;
1567 if (remember_conflicts && !pfrom->have_orig_conflicts)
1568 copy_conflict_list (pfrom);
1569 if (!TEST_BIT (sup_igraph, (pfrom->id * num_webs + pto->id)))
1571 cl = ra_alloc (sizeof (*cl));
1572 cl->t = pto;
1573 cl->sub = NULL;
1574 cl->next = pfrom->conflict_list;
1575 pfrom->conflict_list = cl;
1576 if (pto->type != SELECT && pto->type != COALESCED)
1577 pfrom->num_conflicts += 1 + pto->add_hardregs;
1578 SET_BIT (sup_igraph, (pfrom->id * num_webs + pto->id));
1579 may_delete = 0;
1581 else
1582 /* We don't need to test for cl==NULL, because at this point
1583 a cl with cl->t==pto is guaranteed to exist. */
1584 while (cl->t != pto)
1585 cl = cl->next;
1586 if (pfrom != from || pto != to)
1588 /* This is a subconflict which should be added.
1589 If we inserted cl in this invocation, we really need to add this
1590 subconflict. If we did _not_ add it here, we only add the
1591 subconflict, if cl already had subconflicts, because otherwise
1592 this indicated, that the whole webs already conflict, which
1593 means we are not interested in this subconflict. */
1594 if (!may_delete || cl->sub != NULL)
1596 sl = ra_alloc (sizeof (*sl));
1597 sl->s = from;
1598 sl->t = to;
1599 sl->next = cl->sub;
1600 cl->sub = sl;
1603 else
1604 /* pfrom == from && pto == to means, that we are not interested
1605 anymore in the subconflict list for this pair, because anyway
1606 the whole webs conflict. */
1607 cl->sub = NULL;
1611 /* Record a conflict between two webs, if we haven't recorded it
1612 already. */
1614 void
1615 record_conflict (web1, web2)
1616 struct web *web1, *web2;
1618 unsigned int id1 = web1->id, id2 = web2->id;
1619 unsigned int index = igraph_index (id1, id2);
1620 /* Trivial non-conflict or already recorded conflict. */
1621 if (web1 == web2 || TEST_BIT (igraph, index))
1622 return;
1623 if (id1 == id2)
1624 abort ();
1625 /* As fixed_regs are no targets for allocation, conflicts with them
1626 are pointless. */
1627 if ((web1->regno < FIRST_PSEUDO_REGISTER && fixed_regs[web1->regno])
1628 || (web2->regno < FIRST_PSEUDO_REGISTER && fixed_regs[web2->regno]))
1629 return;
1630 /* Conflicts with hardregs, which are not even a candidate
1631 for this pseudo are also pointless. */
1632 if ((web1->type == PRECOLORED
1633 && ! TEST_HARD_REG_BIT (web2->usable_regs, web1->regno))
1634 || (web2->type == PRECOLORED
1635 && ! TEST_HARD_REG_BIT (web1->usable_regs, web2->regno)))
1636 return;
1637 /* Similar if the set of possible hardregs don't intersect. This iteration
1638 those conflicts are useless (and would make num_conflicts wrong, because
1639 num_freedom is calculated from the set of possible hardregs).
1640 But in presence of spilling and incremental building of the graph we
1641 need to note all uses of webs conflicting with the spilled ones.
1642 Because the set of possible hardregs can change in the next round for
1643 spilled webs, we possibly have then conflicts with webs which would
1644 be excluded now (because then hardregs intersect). But we actually
1645 need to check those uses, and to get hold of them, we need to remember
1646 also webs conflicting with this one, although not conflicting in this
1647 round because of non-intersecting hardregs. */
1648 if (web1->type != PRECOLORED && web2->type != PRECOLORED
1649 && ! hard_regs_intersect_p (&web1->usable_regs, &web2->usable_regs))
1651 struct web *p1 = find_web_for_subweb (web1);
1652 struct web *p2 = find_web_for_subweb (web2);
1653 /* We expect these to be rare enough to justify bitmaps. And because
1654 we have only a special use for it, we note only the superwebs. */
1655 bitmap_set_bit (p1->useless_conflicts, p2->id);
1656 bitmap_set_bit (p2->useless_conflicts, p1->id);
1657 return;
1659 SET_BIT (igraph, index);
1660 add_conflict_edge (web1, web2);
1661 add_conflict_edge (web2, web1);
1664 /* For each web W this produces the missing subwebs Wx, such that it's
1665 possible to exactly specify (W-Wy) for all already existing subwebs Wy. */
1667 static void
1668 build_inverse_webs (web)
1669 struct web *web;
1671 struct web *sweb = web->subreg_next;
1672 unsigned HOST_WIDE_INT undef;
1674 undef = rtx_to_undefined (web->orig_x);
1675 for (; sweb; sweb = sweb->subreg_next)
1676 /* Only create inverses of non-artificial webs. */
1677 if (!sweb->artificial)
1679 unsigned HOST_WIDE_INT bits;
1680 bits = undef & ~ rtx_to_undefined (sweb->orig_x);
1681 while (bits)
1683 unsigned int size_word = undef_to_size_word (web->orig_x, &bits);
1684 if (!find_subweb_2 (web, size_word))
1685 add_subweb_2 (web, size_word);
1690 /* Copies the content of WEB to a new one, and link it into WL.
1691 Used for consistency checking. */
1693 static void
1694 copy_web (web, wl)
1695 struct web *web;
1696 struct web_link **wl;
1698 struct web *cweb = xmalloc (sizeof *cweb);
1699 struct web_link *link = ra_alloc (sizeof *link);
1700 link->next = *wl;
1701 *wl = link;
1702 link->web = cweb;
1703 *cweb = *web;
1706 /* Given a list of webs LINK, compare the content of the webs therein
1707 with the global webs of the same ID. For consistency checking. */
1709 static void
1710 compare_and_free_webs (link)
1711 struct web_link **link;
1713 struct web_link *wl;
1714 for (wl = *link; wl; wl = wl->next)
1716 struct web *web1 = wl->web;
1717 struct web *web2 = ID2WEB (web1->id);
1718 if (web1->regno != web2->regno
1719 || web1->mode_changed != web2->mode_changed
1720 || !rtx_equal_p (web1->orig_x, web2->orig_x)
1721 || web1->type != web2->type
1722 /* Only compare num_defs/num_uses with non-hardreg webs.
1723 E.g. the number of uses of the framepointer changes due to
1724 inserting spill code. */
1725 || (web1->type != PRECOLORED
1726 && (web1->num_uses != web2->num_uses
1727 || web1->num_defs != web2->num_defs))
1728 /* Similarly, if the framepointer was unreferenced originally
1729 but we added spills, these fields may not match. */
1730 || (web1->type != PRECOLORED
1731 && web1->crosses_call != web2->crosses_call)
1732 || (web1->type != PRECOLORED
1733 && web1->live_over_abnormal != web2->live_over_abnormal))
1734 abort ();
1735 if (web1->type != PRECOLORED)
1737 unsigned int i;
1738 for (i = 0; i < web1->num_defs; i++)
1739 if (web1->defs[i] != web2->defs[i])
1740 abort ();
1741 for (i = 0; i < web1->num_uses; i++)
1742 if (web1->uses[i] != web2->uses[i])
1743 abort ();
1745 if (web1->type == PRECOLORED)
1747 if (web1->defs)
1748 free (web1->defs);
1749 if (web1->uses)
1750 free (web1->uses);
1752 free (web1);
1754 *link = NULL;
1757 /* Setup and fill uses[] and defs[] arrays of the webs. */
1759 static void
1760 init_webs_defs_uses ()
1762 struct dlist *d;
1763 for (d = WEBS(INITIAL); d; d = d->next)
1765 struct web *web = DLIST_WEB (d);
1766 unsigned int def_i, use_i;
1767 struct df_link *link;
1768 if (web->old_web)
1769 continue;
1770 if (web->type == PRECOLORED)
1772 web->num_defs = web->num_uses = 0;
1773 continue;
1775 if (web->num_defs)
1776 web->defs = xmalloc (web->num_defs * sizeof (web->defs[0]));
1777 if (web->num_uses)
1778 web->uses = xmalloc (web->num_uses * sizeof (web->uses[0]));
1779 def_i = use_i = 0;
1780 for (link = web->temp_refs; link; link = link->next)
1782 if (DF_REF_REG_DEF_P (link->ref))
1783 web->defs[def_i++] = link->ref;
1784 else
1785 web->uses[use_i++] = link->ref;
1787 web->temp_refs = NULL;
1788 if (def_i != web->num_defs || use_i != web->num_uses)
1789 abort ();
1793 /* Called by parts_to_webs(). This creates (or recreates) the webs (and
1794 subwebs) from web parts, gives them IDs (only to super webs), and sets
1795 up use2web and def2web arrays. */
1797 static unsigned int
1798 parts_to_webs_1 (df, copy_webs, all_refs)
1799 struct df *df;
1800 struct web_link **copy_webs;
1801 struct df_link *all_refs;
1803 unsigned int i;
1804 unsigned int webnum;
1805 unsigned int def_id = df->def_id;
1806 unsigned int use_id = df->use_id;
1807 struct web_part *wp_first_use = &web_parts[def_id];
1809 /* For each root web part: create and initialize a new web,
1810 setup def2web[] and use2web[] for all defs and uses, and
1811 id2web for all new webs. */
1813 webnum = 0;
1814 for (i = 0; i < def_id + use_id; i++)
1816 struct web *subweb, *web = 0; /* Initialize web to silence warnings. */
1817 struct web_part *wp = &web_parts[i];
1818 struct ref *ref = wp->ref;
1819 unsigned int ref_id;
1820 rtx reg;
1821 if (!ref)
1822 continue;
1823 ref_id = i;
1824 if (i >= def_id)
1825 ref_id -= def_id;
1826 all_refs[i].ref = ref;
1827 reg = DF_REF_REG (ref);
1828 if (! wp->uplink)
1830 /* If we have a web part root, create a new web. */
1831 unsigned int newid = ~(unsigned)0;
1832 unsigned int old_web = 0;
1834 /* In the first pass, there are no old webs, so unconditionally
1835 allocate a new one. */
1836 if (ra_pass == 1)
1838 web = xmalloc (sizeof (struct web));
1839 newid = last_num_webs++;
1840 init_one_web (web, GET_CODE (reg) == SUBREG
1841 ? SUBREG_REG (reg) : reg);
1843 /* Otherwise, we look for an old web. */
1844 else
1846 /* Remember, that use2web == def2web + def_id.
1847 Ergo is def2web[i] == use2web[i - def_id] for i >= def_id.
1848 So we only need to look into def2web[] array.
1849 Try to look at the web, which formerly belonged to this
1850 def (or use). */
1851 web = def2web[i];
1852 /* Or which belonged to this hardreg. */
1853 if (!web && DF_REF_REGNO (ref) < FIRST_PSEUDO_REGISTER)
1854 web = hardreg2web[DF_REF_REGNO (ref)];
1855 if (web)
1857 /* If we found one, reuse it. */
1858 web = find_web_for_subweb (web);
1859 remove_list (web->dlink, &WEBS(INITIAL));
1860 old_web = 1;
1861 copy_web (web, copy_webs);
1863 else
1865 /* Otherwise use a new one. First from the free list. */
1866 if (WEBS(FREE))
1867 web = DLIST_WEB (pop_list (&WEBS(FREE)));
1868 else
1870 /* Else allocate a new one. */
1871 web = xmalloc (sizeof (struct web));
1872 newid = last_num_webs++;
1875 /* The id is zeroed in init_one_web(). */
1876 if (newid == ~(unsigned)0)
1877 newid = web->id;
1878 if (old_web)
1879 reinit_one_web (web, GET_CODE (reg) == SUBREG
1880 ? SUBREG_REG (reg) : reg);
1881 else
1882 init_one_web (web, GET_CODE (reg) == SUBREG
1883 ? SUBREG_REG (reg) : reg);
1884 web->old_web = (old_web && web->type != PRECOLORED) ? 1 : 0;
1886 web->span_deaths = wp->spanned_deaths;
1887 web->crosses_call = wp->crosses_call;
1888 web->id = newid;
1889 web->temp_refs = NULL;
1890 webnum++;
1891 if (web->regno < FIRST_PSEUDO_REGISTER && !hardreg2web[web->regno])
1892 hardreg2web[web->regno] = web;
1893 else if (web->regno < FIRST_PSEUDO_REGISTER
1894 && hardreg2web[web->regno] != web)
1895 abort ();
1898 /* If this reference already had a web assigned, we are done.
1899 This test better is equivalent to the web being an old web.
1900 Otherwise something is screwed. (This is tested) */
1901 if (def2web[i] != NULL)
1903 web = def2web[i];
1904 web = find_web_for_subweb (web);
1905 /* But if this ref includes a mode change, or was a use live
1906 over an abnormal call, set appropriate flags in the web. */
1907 if ((DF_REF_FLAGS (ref) & DF_REF_MODE_CHANGE) != 0
1908 && web->regno >= FIRST_PSEUDO_REGISTER)
1909 web->mode_changed = 1;
1910 if ((DF_REF_FLAGS (ref) & DF_REF_STRIPPED) != 0
1911 && web->regno >= FIRST_PSEUDO_REGISTER)
1912 web->subreg_stripped = 1;
1913 if (i >= def_id
1914 && TEST_BIT (live_over_abnormal, ref_id))
1915 web->live_over_abnormal = 1;
1916 /* And check, that it's not a newly allocated web. This would be
1917 an inconsistency. */
1918 if (!web->old_web || web->type == PRECOLORED)
1919 abort ();
1920 continue;
1922 /* In case this was no web part root, we need to initialize WEB
1923 from the ref2web array belonging to the root. */
1924 if (wp->uplink)
1926 struct web_part *rwp = find_web_part (wp);
1927 unsigned int j = DF_REF_ID (rwp->ref);
1928 if (rwp < wp_first_use)
1929 web = def2web[j];
1930 else
1931 web = use2web[j];
1932 web = find_web_for_subweb (web);
1935 /* Remember all references for a web in a single linked list. */
1936 all_refs[i].next = web->temp_refs;
1937 web->temp_refs = &all_refs[i];
1939 /* And the test, that if def2web[i] was NULL above, that we are _not_
1940 an old web. */
1941 if (web->old_web && web->type != PRECOLORED)
1942 abort ();
1944 /* Possible create a subweb, if this ref was a subreg. */
1945 if (GET_CODE (reg) == SUBREG)
1947 subweb = find_subweb (web, reg);
1948 if (!subweb)
1950 subweb = add_subweb (web, reg);
1951 if (web->old_web)
1952 abort ();
1955 else
1956 subweb = web;
1958 /* And look, if the ref involves an invalid mode change. */
1959 if ((DF_REF_FLAGS (ref) & DF_REF_MODE_CHANGE) != 0
1960 && web->regno >= FIRST_PSEUDO_REGISTER)
1961 web->mode_changed = 1;
1962 if ((DF_REF_FLAGS (ref) & DF_REF_STRIPPED) != 0
1963 && web->regno >= FIRST_PSEUDO_REGISTER)
1964 web->subreg_stripped = 1;
1966 /* Setup def2web, or use2web, and increment num_defs or num_uses. */
1967 if (i < def_id)
1969 /* Some sanity checks. */
1970 if (ra_pass > 1)
1972 struct web *compare = def2web[i];
1973 if (i < last_def_id)
1975 if (web->old_web && compare != subweb)
1976 abort ();
1978 if (!web->old_web && compare)
1979 abort ();
1980 if (compare && compare != subweb)
1981 abort ();
1983 def2web[i] = subweb;
1984 web->num_defs++;
1986 else
1988 if (ra_pass > 1)
1990 struct web *compare = use2web[ref_id];
1991 if (ref_id < last_use_id)
1993 if (web->old_web && compare != subweb)
1994 abort ();
1996 if (!web->old_web && compare)
1997 abort ();
1998 if (compare && compare != subweb)
1999 abort ();
2001 use2web[ref_id] = subweb;
2002 web->num_uses++;
2003 if (TEST_BIT (live_over_abnormal, ref_id))
2004 web->live_over_abnormal = 1;
2008 /* We better now have exactly as many webs as we had web part roots. */
2009 if (webnum != num_webs)
2010 abort ();
2012 return webnum;
2015 /* This builds full webs out of web parts, without relating them to each
2016 other (i.e. without creating the conflict edges). */
2018 static void
2019 parts_to_webs (df)
2020 struct df *df;
2022 unsigned int i;
2023 unsigned int webnum;
2024 struct web_link *copy_webs = NULL;
2025 struct dlist *d;
2026 struct df_link *all_refs;
2027 num_subwebs = 0;
2029 /* First build webs and ordinary subwebs. */
2030 all_refs = xcalloc (df->def_id + df->use_id, sizeof (all_refs[0]));
2031 webnum = parts_to_webs_1 (df, &copy_webs, all_refs);
2033 /* Setup the webs for hardregs which are still missing (weren't
2034 mentioned in the code). */
2035 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2036 if (!hardreg2web[i])
2038 struct web *web = xmalloc (sizeof (struct web));
2039 init_one_web (web, gen_rtx_REG (reg_raw_mode[i], i));
2040 web->id = last_num_webs++;
2041 hardreg2web[web->regno] = web;
2043 num_webs = last_num_webs;
2045 /* Now create all artificial subwebs, i.e. those, which do
2046 not correspond to a real subreg in the current function's RTL, but
2047 which nevertheless is a target of a conflict.
2048 XXX we need to merge this loop with the one above, which means, we need
2049 a way to later override the artificiality. Beware: currently
2050 add_subweb_2() relies on the existence of normal subwebs for deducing
2051 a sane mode to use for the artificial subwebs. */
2052 for (i = 0; i < df->def_id + df->use_id; i++)
2054 struct web_part *wp = &web_parts[i];
2055 struct tagged_conflict *cl;
2056 struct web *web;
2057 if (wp->uplink || !wp->ref)
2059 if (wp->sub_conflicts)
2060 abort ();
2061 continue;
2063 web = def2web[i];
2064 web = find_web_for_subweb (web);
2065 for (cl = wp->sub_conflicts; cl; cl = cl->next)
2066 if (!find_subweb_2 (web, cl->size_word))
2067 add_subweb_2 (web, cl->size_word);
2070 /* And now create artificial subwebs needed for representing the inverse
2071 of some subwebs. This also gives IDs to all subwebs. */
2072 webnum = last_num_webs;
2073 for (d = WEBS(INITIAL); d; d = d->next)
2075 struct web *web = DLIST_WEB (d);
2076 if (web->subreg_next)
2078 struct web *sweb;
2079 build_inverse_webs (web);
2080 for (sweb = web->subreg_next; sweb; sweb = sweb->subreg_next)
2081 sweb->id = webnum++;
2085 /* Now that everyone has an ID, we can setup the id2web array. */
2086 id2web = xcalloc (webnum, sizeof (id2web[0]));
2087 for (d = WEBS(INITIAL); d; d = d->next)
2089 struct web *web = DLIST_WEB (d);
2090 ID2WEB (web->id) = web;
2091 for (web = web->subreg_next; web; web = web->subreg_next)
2092 ID2WEB (web->id) = web;
2094 num_subwebs = webnum - last_num_webs;
2095 num_allwebs = num_webs + num_subwebs;
2096 num_webs += num_subwebs;
2098 /* Allocate and clear the conflict graph bitmaps. */
2099 igraph = sbitmap_alloc (num_webs * num_webs / 2);
2100 sup_igraph = sbitmap_alloc (num_webs * num_webs);
2101 sbitmap_zero (igraph);
2102 sbitmap_zero (sup_igraph);
2104 /* Distribute the references to their webs. */
2105 init_webs_defs_uses ();
2106 /* And do some sanity checks if old webs, and those recreated from the
2107 really are the same. */
2108 compare_and_free_webs (&copy_webs);
2109 free (all_refs);
2112 /* This deletes all conflicts to and from webs which need to be renewed
2113 in this pass of the allocator, i.e. those which were spilled in the
2114 last pass. Furthermore it also rebuilds the bitmaps for the remaining
2115 conflicts. */
2117 static void
2118 reset_conflicts ()
2120 unsigned int i;
2121 bitmap newwebs = BITMAP_XMALLOC ();
2122 for (i = 0; i < num_webs - num_subwebs; i++)
2124 struct web *web = ID2WEB (i);
2125 /* Hardreg webs and non-old webs are new webs (which
2126 need rebuilding). */
2127 if (web->type == PRECOLORED || !web->old_web)
2128 bitmap_set_bit (newwebs, web->id);
2131 for (i = 0; i < num_webs - num_subwebs; i++)
2133 struct web *web = ID2WEB (i);
2134 struct conflict_link *cl;
2135 struct conflict_link **pcl;
2136 pcl = &(web->conflict_list);
2138 /* First restore the conflict list to be like it was before
2139 coalescing. */
2140 if (web->have_orig_conflicts)
2142 web->conflict_list = web->orig_conflict_list;
2143 web->orig_conflict_list = NULL;
2145 if (web->orig_conflict_list)
2146 abort ();
2148 /* New non-precolored webs, have no conflict list. */
2149 if (web->type != PRECOLORED && !web->old_web)
2151 *pcl = NULL;
2152 /* Useless conflicts will be rebuilt completely. But check
2153 for cleanliness, as the web might have come from the
2154 free list. */
2155 if (bitmap_first_set_bit (web->useless_conflicts) >= 0)
2156 abort ();
2158 else
2160 /* Useless conflicts with new webs will be rebuilt if they
2161 are still there. */
2162 bitmap_operation (web->useless_conflicts, web->useless_conflicts,
2163 newwebs, BITMAP_AND_COMPL);
2164 /* Go through all conflicts, and retain those to old webs. */
2165 for (cl = web->conflict_list; cl; cl = cl->next)
2167 if (cl->t->old_web || cl->t->type == PRECOLORED)
2169 *pcl = cl;
2170 pcl = &(cl->next);
2172 /* Also restore the entries in the igraph bitmaps. */
2173 web->num_conflicts += 1 + cl->t->add_hardregs;
2174 SET_BIT (sup_igraph, (web->id * num_webs + cl->t->id));
2175 /* No subconflicts mean full webs conflict. */
2176 if (!cl->sub)
2177 SET_BIT (igraph, igraph_index (web->id, cl->t->id));
2178 else
2179 /* Else only the parts in cl->sub must be in the
2180 bitmap. */
2182 struct sub_conflict *sl;
2183 for (sl = cl->sub; sl; sl = sl->next)
2184 SET_BIT (igraph, igraph_index (sl->s->id, sl->t->id));
2188 *pcl = NULL;
2190 web->have_orig_conflicts = 0;
2192 BITMAP_XFREE (newwebs);
2195 /* For each web check it's num_conflicts member against that
2196 number, as calculated from scratch from all neighbors. */
2198 #if 0
2199 static void
2200 check_conflict_numbers ()
2202 unsigned int i;
2203 for (i = 0; i < num_webs; i++)
2205 struct web *web = ID2WEB (i);
2206 int new_conf = 0 * web->add_hardregs;
2207 struct conflict_link *cl;
2208 for (cl = web->conflict_list; cl; cl = cl->next)
2209 if (cl->t->type != SELECT && cl->t->type != COALESCED)
2210 new_conf += 1 + cl->t->add_hardregs;
2211 if (web->type != PRECOLORED && new_conf != web->num_conflicts)
2212 abort ();
2215 #endif
2217 /* Convert the conflicts between web parts to conflicts between full webs.
2219 This can't be done in parts_to_webs(), because for recording conflicts
2220 between webs we need to know their final usable_regs set, which is used
2221 to discard non-conflicts (between webs having no hard reg in common).
2222 But this is set for spill temporaries only after the webs itself are
2223 built. Until then the usable_regs set is based on the pseudo regno used
2224 in this web, which may contain far less registers than later determined.
2225 This would result in us loosing conflicts (due to record_conflict()
2226 thinking that a web can only be allocated to the current usable_regs,
2227 whereas later this is extended) leading to colorings, where some regs which
2228 in reality conflict get the same color. */
2230 static void
2231 conflicts_between_webs (df)
2232 struct df *df;
2234 unsigned int i;
2235 #ifdef STACK_REGS
2236 struct dlist *d;
2237 #endif
2238 bitmap ignore_defs = BITMAP_XMALLOC ();
2239 unsigned int have_ignored;
2240 unsigned int *pass_cache = xcalloc (num_webs, sizeof (int));
2241 unsigned int pass = 0;
2243 if (ra_pass > 1)
2244 reset_conflicts ();
2246 /* It is possible, that in the conflict bitmaps still some defs I are noted,
2247 which have web_parts[I].ref being NULL. This can happen, when from the
2248 last iteration the conflict bitmap for this part wasn't deleted, but a
2249 conflicting move insn was removed. It's DEF is still in the conflict
2250 bitmap, but it doesn't exist anymore in df->defs. To not have to check
2251 it in the tight loop below, we instead remember the ID's of them in a
2252 bitmap, and loop only over IDs which are not in it. */
2253 for (i = 0; i < df->def_id; i++)
2254 if (web_parts[i].ref == NULL)
2255 bitmap_set_bit (ignore_defs, i);
2256 have_ignored = (bitmap_first_set_bit (ignore_defs) >= 0);
2258 /* Now record all conflicts between webs. Note that we only check
2259 the conflict bitmaps of all defs. Conflict bitmaps are only in
2260 webpart roots. If they are in uses, those uses are roots, which
2261 means, that this is an uninitialized web, whose conflicts
2262 don't matter. Nevertheless for hardregs we also need to check uses.
2263 E.g. hardregs used for argument passing have no DEF in the RTL,
2264 but if they have uses, they indeed conflict with all DEFs they
2265 overlap. */
2266 for (i = 0; i < df->def_id + df->use_id; i++)
2268 struct tagged_conflict *cl = web_parts[i].sub_conflicts;
2269 struct web *supweb1;
2270 if (!cl
2271 || (i >= df->def_id
2272 && DF_REF_REGNO (web_parts[i].ref) >= FIRST_PSEUDO_REGISTER))
2273 continue;
2274 supweb1 = def2web[i];
2275 supweb1 = find_web_for_subweb (supweb1);
2276 for (; cl; cl = cl->next)
2277 if (cl->conflicts)
2279 int j;
2280 struct web *web1 = find_subweb_2 (supweb1, cl->size_word);
2281 if (have_ignored)
2282 bitmap_operation (cl->conflicts, cl->conflicts, ignore_defs,
2283 BITMAP_AND_COMPL);
2284 /* We reduce the number of calls to record_conflict() with this
2285 pass thing. record_conflict() itself also has some early-out
2286 optimizations, but here we can use the special properties of
2287 the loop (constant web1) to reduce that even more.
2288 We once used an sbitmap of already handled web indices,
2289 but sbitmaps are slow to clear and bitmaps are slow to
2290 set/test. The current approach needs more memory, but
2291 locality is large. */
2292 pass++;
2294 /* Note, that there are only defs in the conflicts bitset. */
2295 EXECUTE_IF_SET_IN_BITMAP (
2296 cl->conflicts, 0, j,
2298 struct web *web2 = def2web[j];
2299 unsigned int id2 = web2->id;
2300 if (pass_cache[id2] != pass)
2302 pass_cache[id2] = pass;
2303 record_conflict (web1, web2);
2309 free (pass_cache);
2310 BITMAP_XFREE (ignore_defs);
2312 #ifdef STACK_REGS
2313 /* Pseudos can't go in stack regs if they are live at the beginning of
2314 a block that is reached by an abnormal edge. */
2315 for (d = WEBS(INITIAL); d; d = d->next)
2317 struct web *web = DLIST_WEB (d);
2318 int j;
2319 if (web->live_over_abnormal)
2320 for (j = FIRST_STACK_REG; j <= LAST_STACK_REG; j++)
2321 record_conflict (web, hardreg2web[j]);
2323 #endif
2326 /* Remember that a web was spilled, and change some characteristics
2327 accordingly. */
2329 static void
2330 remember_web_was_spilled (web)
2331 struct web *web;
2333 int i;
2334 unsigned int found_size = 0;
2335 int adjust;
2336 web->spill_temp = 1;
2338 /* From now on don't use reg_pref/alt_class (regno) anymore for
2339 this web, but instead usable_regs. We can't use spill_temp for
2340 this, as it might get reset later, when we are coalesced to a
2341 non-spill-temp. In that case we still want to use usable_regs. */
2342 web->use_my_regs = 1;
2344 /* We don't constrain spill temporaries in any way for now.
2345 It's wrong sometimes to have the same constraints or
2346 preferences as the original pseudo, esp. if they were very narrow.
2347 (E.g. there once was a reg wanting class AREG (only one register)
2348 without alternative class. As long, as also the spill-temps for
2349 this pseudo had the same constraints it was spilled over and over.
2350 Ideally we want some constraints also on spill-temps: Because they are
2351 not only loaded/stored, but also worked with, any constraints from insn
2352 alternatives needs applying. Currently this is dealt with by reload, as
2353 many other things, but at some time we want to integrate that
2354 functionality into the allocator. */
2355 if (web->regno >= max_normal_pseudo)
2357 COPY_HARD_REG_SET (web->usable_regs,
2358 reg_class_contents[reg_preferred_class (web->regno)]);
2359 IOR_HARD_REG_SET (web->usable_regs,
2360 reg_class_contents[reg_alternate_class (web->regno)]);
2362 else
2363 COPY_HARD_REG_SET (web->usable_regs,
2364 reg_class_contents[(int) GENERAL_REGS]);
2365 AND_COMPL_HARD_REG_SET (web->usable_regs, never_use_colors);
2366 prune_hardregs_for_mode (&web->usable_regs, PSEUDO_REGNO_MODE (web->regno));
2367 #ifdef CANNOT_CHANGE_MODE_CLASS
2368 if (web->mode_changed)
2369 AND_COMPL_HARD_REG_SET (web->usable_regs, invalid_mode_change_regs);
2370 #endif
2371 web->num_freedom = hard_regs_count (web->usable_regs);
2372 if (!web->num_freedom)
2373 abort();
2374 COPY_HARD_REG_SET (web->orig_usable_regs, web->usable_regs);
2375 /* Now look for a class, which is subset of our constraints, to
2376 setup add_hardregs, and regclass for debug output. */
2377 web->regclass = NO_REGS;
2378 for (i = (int) ALL_REGS - 1; i > 0; i--)
2380 unsigned int size;
2381 HARD_REG_SET test;
2382 COPY_HARD_REG_SET (test, reg_class_contents[i]);
2383 AND_COMPL_HARD_REG_SET (test, never_use_colors);
2384 GO_IF_HARD_REG_SUBSET (test, web->usable_regs, found);
2385 continue;
2386 found:
2387 /* Measure the actual number of bits which really are overlapping
2388 the target regset, not just the reg_class_size. */
2389 size = hard_regs_count (test);
2390 if (found_size < size)
2392 web->regclass = (enum reg_class) i;
2393 found_size = size;
2397 adjust = 0 * web->add_hardregs;
2398 web->add_hardregs =
2399 CLASS_MAX_NREGS (web->regclass, PSEUDO_REGNO_MODE (web->regno)) - 1;
2400 web->num_freedom -= web->add_hardregs;
2401 if (!web->num_freedom)
2402 abort();
2403 adjust -= 0 * web->add_hardregs;
2404 web->num_conflicts -= adjust;
2407 /* Look at each web, if it is used as spill web. Or better said,
2408 if it will be spillable in this pass. */
2410 static void
2411 detect_spill_temps ()
2413 struct dlist *d;
2414 bitmap already = BITMAP_XMALLOC ();
2416 /* Detect webs used for spill temporaries. */
2417 for (d = WEBS(INITIAL); d; d = d->next)
2419 struct web *web = DLIST_WEB (d);
2421 /* Below only the detection of spill temporaries. We never spill
2422 precolored webs, so those can't be spill temporaries. The code above
2423 (remember_web_was_spilled) can't currently cope with hardregs
2424 anyway. */
2425 if (web->regno < FIRST_PSEUDO_REGISTER)
2426 continue;
2427 /* Uninitialized webs can't be spill-temporaries. */
2428 if (web->num_defs == 0)
2429 continue;
2431 /* A web with only defs and no uses can't be spilled. Nevertheless
2432 it must get a color, as it takes away an register from all webs
2433 live at these defs. So we make it a short web. */
2434 if (web->num_uses == 0)
2435 web->spill_temp = 3;
2436 /* A web which was spilled last time, but for which no insns were
2437 emitted (can happen with IR spilling ignoring sometimes
2438 all deaths). */
2439 else if (web->changed)
2440 web->spill_temp = 1;
2441 /* A spill temporary has one def, one or more uses, all uses
2442 are in one insn, and either the def or use insn was inserted
2443 by the allocator. */
2444 /* XXX not correct currently. There might also be spill temps
2445 involving more than one def. Usually that's an additional
2446 clobber in the using instruction. We might also constrain
2447 ourself to that, instead of like currently marking all
2448 webs involving any spill insns at all. */
2449 else
2451 unsigned int i;
2452 int spill_involved = 0;
2453 for (i = 0; i < web->num_uses && !spill_involved; i++)
2454 if (DF_REF_INSN_UID (web->uses[i]) >= orig_max_uid)
2455 spill_involved = 1;
2456 for (i = 0; i < web->num_defs && !spill_involved; i++)
2457 if (DF_REF_INSN_UID (web->defs[i]) >= orig_max_uid)
2458 spill_involved = 1;
2460 if (spill_involved/* && ra_pass > 2*/)
2462 int num_deaths = web->span_deaths;
2463 /* Mark webs involving at least one spill insn as
2464 spill temps. */
2465 remember_web_was_spilled (web);
2466 /* Search for insns which define and use the web in question
2467 at the same time, i.e. look for rmw insns. If these insns
2468 are also deaths of other webs they might have been counted
2469 as such into web->span_deaths. But because of the rmw nature
2470 of this insn it is no point where a load/reload could be
2471 placed successfully (it would still conflict with the
2472 dead web), so reduce the number of spanned deaths by those
2473 insns. Note that sometimes such deaths are _not_ counted,
2474 so negative values can result. */
2475 bitmap_zero (already);
2476 for (i = 0; i < web->num_defs; i++)
2478 rtx insn = web->defs[i]->insn;
2479 if (TEST_BIT (insns_with_deaths, INSN_UID (insn))
2480 && !bitmap_bit_p (already, INSN_UID (insn)))
2482 unsigned int j;
2483 bitmap_set_bit (already, INSN_UID (insn));
2484 /* Only decrement it once for each insn. */
2485 for (j = 0; j < web->num_uses; j++)
2486 if (web->uses[j]->insn == insn)
2488 num_deaths--;
2489 break;
2493 /* But mark them specially if they could possibly be spilled,
2494 either because they cross some deaths (without the above
2495 mentioned ones) or calls. */
2496 if (web->crosses_call || num_deaths > 0)
2497 web->spill_temp = 1 * 2;
2499 /* A web spanning no deaths can't be spilled either. No loads
2500 would be created for it, ergo no defs. So the insns wouldn't
2501 change making the graph not easier to color. Make this also
2502 a short web. Don't do this if it crosses calls, as these are
2503 also points of reloads. */
2504 else if (web->span_deaths == 0 && !web->crosses_call)
2505 web->spill_temp = 3;
2507 web->orig_spill_temp = web->spill_temp;
2509 BITMAP_XFREE (already);
2512 /* Returns nonzero if the rtx MEM refers somehow to a stack location. */
2515 memref_is_stack_slot (mem)
2516 rtx mem;
2518 rtx ad = XEXP (mem, 0);
2519 rtx x;
2520 if (GET_CODE (ad) != PLUS || GET_CODE (XEXP (ad, 1)) != CONST_INT)
2521 return 0;
2522 x = XEXP (ad, 0);
2523 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
2524 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
2525 || x == stack_pointer_rtx)
2526 return 1;
2527 return 0;
2530 /* Returns nonzero, if rtx X somewhere contains any pseudo register. */
2532 static int
2533 contains_pseudo (x)
2534 rtx x;
2536 const char *fmt;
2537 int i;
2538 if (GET_CODE (x) == SUBREG)
2539 x = SUBREG_REG (x);
2540 if (GET_CODE (x) == REG)
2542 if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
2543 return 1;
2544 else
2545 return 0;
2548 fmt = GET_RTX_FORMAT (GET_CODE (x));
2549 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2550 if (fmt[i] == 'e')
2552 if (contains_pseudo (XEXP (x, i)))
2553 return 1;
2555 else if (fmt[i] == 'E')
2557 int j;
2558 for (j = 0; j < XVECLEN (x, i); j++)
2559 if (contains_pseudo (XVECEXP (x, i, j)))
2560 return 1;
2562 return 0;
2565 /* Returns nonzero, if we are able to rematerialize something with
2566 value X. If it's not a general operand, we test if we can produce
2567 a valid insn which set a pseudo to that value, and that insn doesn't
2568 clobber anything. */
2570 static GTY(()) rtx remat_test_insn;
2571 static int
2572 want_to_remat (x)
2573 rtx x;
2575 int num_clobbers = 0;
2576 int icode;
2578 /* If this is a valid operand, we are OK. If it's VOIDmode, we aren't. */
2579 if (general_operand (x, GET_MODE (x)))
2580 return 1;
2582 /* Otherwise, check if we can make a valid insn from it. First initialize
2583 our test insn if we haven't already. */
2584 if (remat_test_insn == 0)
2586 remat_test_insn
2587 = make_insn_raw (gen_rtx_SET (VOIDmode,
2588 gen_rtx_REG (word_mode,
2589 FIRST_PSEUDO_REGISTER * 2),
2590 const0_rtx));
2591 NEXT_INSN (remat_test_insn) = PREV_INSN (remat_test_insn) = 0;
2594 /* Now make an insn like the one we would make when rematerializing
2595 the value X and see if valid. */
2596 PUT_MODE (SET_DEST (PATTERN (remat_test_insn)), GET_MODE (x));
2597 SET_SRC (PATTERN (remat_test_insn)) = x;
2598 /* XXX For now we don't allow any clobbers to be added, not just no
2599 hardreg clobbers. */
2600 return ((icode = recog (PATTERN (remat_test_insn), remat_test_insn,
2601 &num_clobbers)) >= 0
2602 && (num_clobbers == 0
2603 /*|| ! added_clobbers_hard_reg_p (icode)*/));
2606 /* Look at all webs, if they perhaps are rematerializable.
2607 They are, if all their defs are simple sets to the same value,
2608 and that value is simple enough, and want_to_remat() holds for it. */
2610 static void
2611 detect_remat_webs ()
2613 struct dlist *d;
2614 for (d = WEBS(INITIAL); d; d = d->next)
2616 struct web *web = DLIST_WEB (d);
2617 unsigned int i;
2618 rtx pat = NULL_RTX;
2619 /* Hardregs and useless webs aren't spilled -> no remat necessary.
2620 Defless webs obviously also can't be rematerialized. */
2621 if (web->regno < FIRST_PSEUDO_REGISTER || !web->num_defs
2622 || !web->num_uses)
2623 continue;
2624 for (i = 0; i < web->num_defs; i++)
2626 rtx insn;
2627 rtx set = single_set (insn = DF_REF_INSN (web->defs[i]));
2628 rtx src;
2629 if (!set)
2630 break;
2631 src = SET_SRC (set);
2632 /* When only subregs of the web are set it isn't easily
2633 rematerializable. */
2634 if (!rtx_equal_p (SET_DEST (set), web->orig_x))
2635 break;
2636 /* If we already have a pattern it must be equal to the current. */
2637 if (pat && !rtx_equal_p (pat, src))
2638 break;
2639 /* Don't do the expensive checks multiple times. */
2640 if (pat)
2641 continue;
2642 /* For now we allow only constant sources. */
2643 if ((CONSTANT_P (src)
2644 /* If the whole thing is stable already, it is a source for
2645 remat, no matter how complicated (probably all needed
2646 resources for it are live everywhere, and don't take
2647 additional register resources). */
2648 /* XXX Currently we can't use patterns which contain
2649 pseudos, _even_ if they are stable. The code simply isn't
2650 prepared for that. All those operands can't be spilled (or
2651 the dependent remat webs are not remat anymore), so they
2652 would be oldwebs in the next iteration. But currently
2653 oldwebs can't have their references changed. The
2654 incremental machinery barfs on that. */
2655 || (!rtx_unstable_p (src) && !contains_pseudo (src))
2656 /* Additionally also memrefs to stack-slots are useful, when
2657 we created them ourself. They might not have set their
2658 unchanging flag set, but nevertheless they are stable across
2659 the livetime in question. */
2660 || (GET_CODE (src) == MEM
2661 && INSN_UID (insn) >= orig_max_uid
2662 && memref_is_stack_slot (src)))
2663 /* And we must be able to construct an insn without
2664 side-effects to actually load that value into a reg. */
2665 && want_to_remat (src))
2666 pat = src;
2667 else
2668 break;
2670 if (pat && i == web->num_defs)
2671 web->pattern = pat;
2675 /* Determine the spill costs of all webs. */
2677 static void
2678 determine_web_costs ()
2680 struct dlist *d;
2681 for (d = WEBS(INITIAL); d; d = d->next)
2683 unsigned int i, num_loads;
2684 int load_cost, store_cost;
2685 unsigned HOST_WIDE_INT w;
2686 struct web *web = DLIST_WEB (d);
2687 if (web->type == PRECOLORED)
2688 continue;
2689 /* Get costs for one load/store. Note that we offset them by 1,
2690 because some patterns have a zero rtx_cost(), but we of course
2691 still need the actual load/store insns. With zero all those
2692 webs would be the same, no matter how often and where
2693 they are used. */
2694 if (web->pattern)
2696 /* This web is rematerializable. Beware, we set store_cost to
2697 zero optimistically assuming, that we indeed don't emit any
2698 stores in the spill-code addition. This might be wrong if
2699 at the point of the load not all needed resources are
2700 available, in which case we emit a stack-based load, for
2701 which we in turn need the according stores. */
2702 load_cost = 1 + rtx_cost (web->pattern, 0);
2703 store_cost = 0;
2705 else
2707 load_cost = 1 + MEMORY_MOVE_COST (GET_MODE (web->orig_x),
2708 web->regclass, 1);
2709 store_cost = 1 + MEMORY_MOVE_COST (GET_MODE (web->orig_x),
2710 web->regclass, 0);
2712 /* We create only loads at deaths, whose number is in span_deaths. */
2713 num_loads = MIN (web->span_deaths, web->num_uses);
2714 for (w = 0, i = 0; i < web->num_uses; i++)
2715 w += DF_REF_BB (web->uses[i])->frequency + 1;
2716 if (num_loads < web->num_uses)
2717 w = (w * num_loads + web->num_uses - 1) / web->num_uses;
2718 web->spill_cost = w * load_cost;
2719 if (store_cost)
2721 for (w = 0, i = 0; i < web->num_defs; i++)
2722 w += DF_REF_BB (web->defs[i])->frequency + 1;
2723 web->spill_cost += w * store_cost;
2725 web->orig_spill_cost = web->spill_cost;
2729 /* Detect webs which are set in a conditional jump insn (possibly a
2730 decrement-and-branch type of insn), and mark them not to be
2731 spillable. The stores for them would need to be placed on edges,
2732 which destroys the CFG. (Somewhen we want to deal with that XXX) */
2734 static void
2735 detect_webs_set_in_cond_jump ()
2737 basic_block bb;
2738 FOR_EACH_BB (bb)
2739 if (GET_CODE (bb->end) == JUMP_INSN)
2741 struct df_link *link;
2742 for (link = DF_INSN_DEFS (df, bb->end); link; link = link->next)
2743 if (link->ref && DF_REF_REGNO (link->ref) >= FIRST_PSEUDO_REGISTER)
2745 struct web *web = def2web[DF_REF_ID (link->ref)];
2746 web->orig_spill_temp = web->spill_temp = 3;
2751 /* Second top-level function of this file.
2752 Converts the connected web parts to full webs. This means, it allocates
2753 all webs, and initializes all fields, including detecting spill
2754 temporaries. It does not distribute moves to their corresponding webs,
2755 though. */
2757 static void
2758 make_webs (df)
2759 struct df *df;
2761 /* First build all the webs itself. They are not related with
2762 others yet. */
2763 parts_to_webs (df);
2764 /* Now detect spill temporaries to initialize their usable_regs set. */
2765 detect_spill_temps ();
2766 detect_webs_set_in_cond_jump ();
2767 /* And finally relate them to each other, meaning to record all possible
2768 conflicts between webs (see the comment there). */
2769 conflicts_between_webs (df);
2770 detect_remat_webs ();
2771 determine_web_costs ();
2774 /* Distribute moves to the corresponding webs. */
2776 static void
2777 moves_to_webs (df)
2778 struct df *df;
2780 struct df_link *link;
2781 struct move_list *ml;
2783 /* Distribute all moves to their corresponding webs, making sure,
2784 each move is in a web maximally one time (happens on some strange
2785 insns). */
2786 for (ml = wl_moves; ml; ml = ml->next)
2788 struct move *m = ml->move;
2789 struct web *web;
2790 struct move_list *newml;
2791 if (!m)
2792 continue;
2793 m->type = WORKLIST;
2794 m->dlink = NULL;
2795 /* Multiple defs/uses can happen in moves involving hard-regs in
2796 a wider mode. For those df.* creates use/def references for each
2797 real hard-reg involved. For coalescing we are interested in
2798 the smallest numbered hard-reg. */
2799 for (link = DF_INSN_DEFS (df, m->insn); link; link = link->next)
2800 if (link->ref)
2802 web = def2web[DF_REF_ID (link->ref)];
2803 web = find_web_for_subweb (web);
2804 if (!m->target_web || web->regno < m->target_web->regno)
2805 m->target_web = web;
2807 for (link = DF_INSN_USES (df, m->insn); link; link = link->next)
2808 if (link->ref)
2810 web = use2web[DF_REF_ID (link->ref)];
2811 web = find_web_for_subweb (web);
2812 if (!m->source_web || web->regno < m->source_web->regno)
2813 m->source_web = web;
2815 if (m->source_web && m->target_web
2816 /* If the usable_regs don't intersect we can't coalesce the two
2817 webs anyway, as this is no simple copy insn (it might even
2818 need an intermediate stack temp to execute this "copy" insn). */
2819 && hard_regs_intersect_p (&m->source_web->usable_regs,
2820 &m->target_web->usable_regs))
2822 if (!flag_ra_optimistic_coalescing)
2824 struct move_list *test = m->source_web->moves;
2825 for (; test && test->move != m; test = test->next);
2826 if (! test)
2828 newml = ra_alloc (sizeof (struct move_list));
2829 newml->move = m;
2830 newml->next = m->source_web->moves;
2831 m->source_web->moves = newml;
2833 test = m->target_web->moves;
2834 for (; test && test->move != m; test = test->next);
2835 if (! test)
2837 newml = ra_alloc (sizeof (struct move_list));
2838 newml->move = m;
2839 newml->next = m->target_web->moves;
2840 m->target_web->moves = newml;
2844 else
2845 /* Delete this move. */
2846 ml->move = NULL;
2850 /* Handle tricky asm insns.
2851 Supposed to create conflicts to hardregs which aren't allowed in
2852 the constraints. Doesn't actually do that, as it might confuse
2853 and constrain the allocator too much. */
2855 static void
2856 handle_asm_insn (df, insn)
2857 struct df *df;
2858 rtx insn;
2860 const char *constraints[MAX_RECOG_OPERANDS];
2861 enum machine_mode operand_mode[MAX_RECOG_OPERANDS];
2862 int i, noperands, in_output;
2863 HARD_REG_SET clobbered, allowed, conflict;
2864 rtx pat;
2865 if (! INSN_P (insn)
2866 || (noperands = asm_noperands (PATTERN (insn))) < 0)
2867 return;
2868 pat = PATTERN (insn);
2869 CLEAR_HARD_REG_SET (clobbered);
2871 if (GET_CODE (pat) == PARALLEL)
2872 for (i = 0; i < XVECLEN (pat, 0); i++)
2874 rtx t = XVECEXP (pat, 0, i);
2875 if (GET_CODE (t) == CLOBBER && GET_CODE (XEXP (t, 0)) == REG
2876 && REGNO (XEXP (t, 0)) < FIRST_PSEUDO_REGISTER)
2877 SET_HARD_REG_BIT (clobbered, REGNO (XEXP (t, 0)));
2880 decode_asm_operands (pat, recog_data.operand, recog_data.operand_loc,
2881 constraints, operand_mode);
2882 in_output = 1;
2883 for (i = 0; i < noperands; i++)
2885 const char *p = constraints[i];
2886 int cls = (int) NO_REGS;
2887 struct df_link *link;
2888 rtx reg;
2889 struct web *web;
2890 int nothing_allowed = 1;
2891 reg = recog_data.operand[i];
2893 /* Look, if the constraints apply to a pseudo reg, and not to
2894 e.g. a mem. */
2895 while (GET_CODE (reg) == SUBREG
2896 || GET_CODE (reg) == ZERO_EXTRACT
2897 || GET_CODE (reg) == SIGN_EXTRACT
2898 || GET_CODE (reg) == STRICT_LOW_PART)
2899 reg = XEXP (reg, 0);
2900 if (GET_CODE (reg) != REG || REGNO (reg) < FIRST_PSEUDO_REGISTER)
2901 continue;
2903 /* Search the web corresponding to this operand. We depend on
2904 that decode_asm_operands() places the output operands
2905 before the input operands. */
2906 while (1)
2908 if (in_output)
2909 link = df->insns[INSN_UID (insn)].defs;
2910 else
2911 link = df->insns[INSN_UID (insn)].uses;
2912 while (link && link->ref && DF_REF_REAL_REG (link->ref) != reg)
2913 link = link->next;
2914 if (!link || !link->ref)
2916 if (in_output)
2917 in_output = 0;
2918 else
2919 abort ();
2921 else
2922 break;
2924 if (in_output)
2925 web = def2web[DF_REF_ID (link->ref)];
2926 else
2927 web = use2web[DF_REF_ID (link->ref)];
2928 reg = DF_REF_REG (link->ref);
2930 /* Find the constraints, noting the allowed hardregs in allowed. */
2931 CLEAR_HARD_REG_SET (allowed);
2932 while (1)
2934 char c = *p;
2936 if (c == '\0' || c == ',' || c == '#')
2938 /* End of one alternative - mark the regs in the current
2939 class, and reset the class. */
2940 p++;
2941 IOR_HARD_REG_SET (allowed, reg_class_contents[cls]);
2942 if (cls != NO_REGS)
2943 nothing_allowed = 0;
2944 cls = NO_REGS;
2945 if (c == '#')
2946 do {
2947 c = *p++;
2948 } while (c != '\0' && c != ',');
2949 if (c == '\0')
2950 break;
2951 continue;
2954 switch (c)
2956 case '=': case '+': case '*': case '%': case '?': case '!':
2957 case '0': case '1': case '2': case '3': case '4': case 'm':
2958 case '<': case '>': case 'V': case 'o': case '&': case 'E':
2959 case 'F': case 's': case 'i': case 'n': case 'X': case 'I':
2960 case 'J': case 'K': case 'L': case 'M': case 'N': case 'O':
2961 case 'P':
2962 break;
2964 case 'p':
2965 cls = (int) reg_class_subunion[cls][(int) BASE_REG_CLASS];
2966 nothing_allowed = 0;
2967 break;
2969 case 'g':
2970 case 'r':
2971 cls = (int) reg_class_subunion[cls][(int) GENERAL_REGS];
2972 nothing_allowed = 0;
2973 break;
2975 default:
2976 cls =
2977 (int) reg_class_subunion[cls][(int)
2978 REG_CLASS_FROM_CONSTRAINT (c,
2979 p)];
2981 p += CONSTRAINT_LEN (c, p);
2984 /* Now make conflicts between this web, and all hardregs, which
2985 are not allowed by the constraints. */
2986 if (nothing_allowed)
2988 /* If we had no real constraints nothing was explicitly
2989 allowed, so we allow the whole class (i.e. we make no
2990 additional conflicts). */
2991 CLEAR_HARD_REG_SET (conflict);
2993 else
2995 COPY_HARD_REG_SET (conflict, usable_regs
2996 [reg_preferred_class (web->regno)]);
2997 IOR_HARD_REG_SET (conflict, usable_regs
2998 [reg_alternate_class (web->regno)]);
2999 AND_COMPL_HARD_REG_SET (conflict, allowed);
3000 /* We can't yet establish these conflicts. Reload must go first
3001 (or better said, we must implement some functionality of reload).
3002 E.g. if some operands must match, and they need the same color
3003 we don't see yet, that they do not conflict (because they match).
3004 For us it looks like two normal references with different DEFs,
3005 so they conflict, and as they both need the same color, the
3006 graph becomes uncolorable. */
3007 #if 0
3008 for (c = 0; c < FIRST_PSEUDO_REGISTER; c++)
3009 if (TEST_HARD_REG_BIT (conflict, c))
3010 record_conflict (web, hardreg2web[c]);
3011 #endif
3013 if (rtl_dump_file)
3015 int c;
3016 ra_debug_msg (DUMP_ASM, " ASM constrain Web %d conflicts with:", web->id);
3017 for (c = 0; c < FIRST_PSEUDO_REGISTER; c++)
3018 if (TEST_HARD_REG_BIT (conflict, c))
3019 ra_debug_msg (DUMP_ASM, " %d", c);
3020 ra_debug_msg (DUMP_ASM, "\n");
3025 /* The real toplevel function in this file.
3026 Build (or rebuilds) the complete interference graph with webs
3027 and conflicts. */
3029 void
3030 build_i_graph (df)
3031 struct df *df;
3033 rtx insn;
3035 init_web_parts (df);
3037 sbitmap_zero (move_handled);
3038 wl_moves = NULL;
3040 build_web_parts_and_conflicts (df);
3042 /* For read-modify-write instructions we may have created two webs.
3043 Reconnect them here. (s.a.) */
3044 connect_rmw_web_parts (df);
3046 /* The webs are conceptually complete now, but still scattered around as
3047 connected web parts. Collect all information and build the webs
3048 including all conflicts between webs (instead web parts). */
3049 make_webs (df);
3050 moves_to_webs (df);
3052 /* Look for additional constraints given by asms. */
3053 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3054 handle_asm_insn (df, insn);
3057 /* Allocates or reallocates most memory for the interference graph and
3058 associated structures. If it reallocates memory (meaning, this is not
3059 the first pass), this also changes some structures to reflect the
3060 additional entries in various array, and the higher number of
3061 defs and uses. */
3063 void
3064 ra_build_realloc (df)
3065 struct df *df;
3067 struct web_part *last_web_parts = web_parts;
3068 struct web **last_def2web = def2web;
3069 struct web **last_use2web = use2web;
3070 sbitmap last_live_over_abnormal = live_over_abnormal;
3071 unsigned int i;
3072 struct dlist *d;
3073 move_handled = sbitmap_alloc (get_max_uid () );
3074 web_parts = xcalloc (df->def_id + df->use_id, sizeof web_parts[0]);
3075 def2web = xcalloc (df->def_id + df->use_id, sizeof def2web[0]);
3076 use2web = &def2web[df->def_id];
3077 live_over_abnormal = sbitmap_alloc (df->use_id);
3078 sbitmap_zero (live_over_abnormal);
3080 /* First go through all old defs and uses. */
3081 for (i = 0; i < last_def_id + last_use_id; i++)
3083 /* And relocate them to the new array. This is made ugly by the
3084 fact, that defs and uses are placed consecutive into one array. */
3085 struct web_part *dest = &web_parts[i < last_def_id
3086 ? i : (df->def_id + i - last_def_id)];
3087 struct web_part *up;
3088 *dest = last_web_parts[i];
3089 up = dest->uplink;
3090 dest->uplink = NULL;
3092 /* Also relocate the uplink to point into the new array. */
3093 if (up && up->ref)
3095 unsigned int id = DF_REF_ID (up->ref);
3096 if (up < &last_web_parts[last_def_id])
3098 if (df->defs[id])
3099 dest->uplink = &web_parts[DF_REF_ID (up->ref)];
3101 else if (df->uses[id])
3102 dest->uplink = &web_parts[df->def_id + DF_REF_ID (up->ref)];
3106 /* Also set up the def2web and use2web arrays, from the last pass.i
3107 Remember also the state of live_over_abnormal. */
3108 for (i = 0; i < last_def_id; i++)
3110 struct web *web = last_def2web[i];
3111 if (web)
3113 web = find_web_for_subweb (web);
3114 if (web->type != FREE && web->type != PRECOLORED)
3115 def2web[i] = last_def2web[i];
3118 for (i = 0; i < last_use_id; i++)
3120 struct web *web = last_use2web[i];
3121 if (web)
3123 web = find_web_for_subweb (web);
3124 if (web->type != FREE && web->type != PRECOLORED)
3125 use2web[i] = last_use2web[i];
3127 if (TEST_BIT (last_live_over_abnormal, i))
3128 SET_BIT (live_over_abnormal, i);
3131 /* We don't have any subwebs for now. Somewhen we might want to
3132 remember them too, instead of recreating all of them every time.
3133 The problem is, that which subwebs we need, depends also on what
3134 other webs and subwebs exist, and which conflicts are there.
3135 OTOH it should be no problem, if we had some more subwebs than strictly
3136 needed. Later. */
3137 for (d = WEBS(FREE); d; d = d->next)
3139 struct web *web = DLIST_WEB (d);
3140 struct web *wnext;
3141 for (web = web->subreg_next; web; web = wnext)
3143 wnext = web->subreg_next;
3144 free (web);
3146 DLIST_WEB (d)->subreg_next = NULL;
3149 /* The uses we anyway are going to check, are not yet live over an abnormal
3150 edge. In fact, they might actually not anymore, due to added
3151 loads. */
3152 if (last_check_uses)
3153 sbitmap_difference (live_over_abnormal, live_over_abnormal,
3154 last_check_uses);
3156 if (last_def_id || last_use_id)
3158 sbitmap_free (last_live_over_abnormal);
3159 free (last_web_parts);
3160 free (last_def2web);
3162 if (!last_max_uid)
3164 /* Setup copy cache, for copy_insn_p (). */
3165 copy_cache = xcalloc (get_max_uid (), sizeof (copy_cache[0]));
3166 init_bb_info ();
3168 else
3170 copy_cache = xrealloc (copy_cache, get_max_uid () * sizeof (copy_cache[0]));
3171 memset (&copy_cache[last_max_uid], 0,
3172 (get_max_uid () - last_max_uid) * sizeof (copy_cache[0]));
3176 /* Free up/clear some memory, only needed for one pass. */
3178 void
3179 ra_build_free ()
3181 struct dlist *d;
3182 unsigned int i;
3184 /* Clear the moves associated with a web (we also need to look into
3185 subwebs here). */
3186 for (i = 0; i < num_webs; i++)
3188 struct web *web = ID2WEB (i);
3189 if (!web)
3190 abort ();
3191 if (i >= num_webs - num_subwebs
3192 && (web->conflict_list || web->orig_conflict_list))
3193 abort ();
3194 web->moves = NULL;
3196 /* All webs in the free list have no defs or uses anymore. */
3197 for (d = WEBS(FREE); d; d = d->next)
3199 struct web *web = DLIST_WEB (d);
3200 if (web->defs)
3201 free (web->defs);
3202 web->defs = NULL;
3203 if (web->uses)
3204 free (web->uses);
3205 web->uses = NULL;
3206 /* We can't free the subwebs here, as they are referenced from
3207 def2web[], and possibly needed in the next ra_build_realloc().
3208 We free them there (or in free_all_mem()). */
3211 /* Free all conflict bitmaps from web parts. Note that we clear
3212 _all_ these conflicts, and don't rebuild them next time for uses
3213 which aren't rechecked. This mean, that those conflict bitmaps
3214 only contain the incremental information. The cumulative one
3215 is still contained in the edges of the I-graph, i.e. in
3216 conflict_list (or orig_conflict_list) of the webs. */
3217 for (i = 0; i < df->def_id + df->use_id; i++)
3219 struct tagged_conflict *cl;
3220 for (cl = web_parts[i].sub_conflicts; cl; cl = cl->next)
3222 if (cl->conflicts)
3223 BITMAP_XFREE (cl->conflicts);
3225 web_parts[i].sub_conflicts = NULL;
3228 wl_moves = NULL;
3230 free (id2web);
3231 free (move_handled);
3232 sbitmap_free (sup_igraph);
3233 sbitmap_free (igraph);
3236 /* Free all memory for the interference graph structures. */
3238 void
3239 ra_build_free_all (df)
3240 struct df *df;
3242 unsigned int i;
3244 free_bb_info ();
3245 free (copy_cache);
3246 copy_cache = NULL;
3247 for (i = 0; i < df->def_id + df->use_id; i++)
3249 struct tagged_conflict *cl;
3250 for (cl = web_parts[i].sub_conflicts; cl; cl = cl->next)
3252 if (cl->conflicts)
3253 BITMAP_XFREE (cl->conflicts);
3255 web_parts[i].sub_conflicts = NULL;
3257 sbitmap_free (live_over_abnormal);
3258 free (web_parts);
3259 web_parts = NULL;
3260 if (last_check_uses)
3261 sbitmap_free (last_check_uses);
3262 last_check_uses = NULL;
3263 free (def2web);
3264 use2web = NULL;
3265 def2web = NULL;
3268 #include "gt-ra-build.h"
3271 vim:cinoptions={.5s,g0,p5,t0,(0,^-0.5s,n-0.5s:tw=78:cindent:sw=4: