expr.c (emit_move_insn): Use SCALAR_FLOAT_MODE_P
[official-gcc.git] / gcc / ra-build.c
blobe320e514d905d9658a50f2940d6cf77e0c1158ae
1 /* Graph coloring register allocator
2 Copyright (C) 2001, 2002 Free Software Foundation, Inc.
3 Contributed by Michael Matz <matz@suse.de>
4 and Daniel Berlin <dan@cgsoftware.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under the
9 terms of the GNU General Public License as published by the Free Software
10 Foundation; either version 2, or (at your option) any later version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
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 "rtl.h"
24 #include "tm_p.h"
25 #include "insn-config.h"
26 #include "recog.h"
27 #include "reload.h"
28 #include "function.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "basic-block.h"
32 #include "df.h"
33 #include "output.h"
34 #include "ggc.h"
35 #include "ra.h"
37 /* This file is part of the graph coloring register alloctor.
38 It deals with building the interference graph. When rebuilding
39 the graph for a function after spilling, we rebuild only those
40 parts needed, i.e. it works incrementally.
42 The first part (the functions called from build_web_parts_and_conflicts()
43 ) constructs a web_part for each pseudo register reference in the insn
44 stream, then goes backward from each use, until it reaches defs for that
45 pseudo. While going back it remember seen defs for other registers as
46 conflicts. By connecting the uses and defs, which reach each other, webs
47 (or live ranges) are built conceptually.
49 The second part (make_webs() and childs) deals with converting that
50 structure to the nodes and edges, on which our interference graph is
51 built. For each root web part constructed above, an instance of struct
52 web is created. For all subregs of pseudos, which matter for allocation,
53 a subweb of the corresponding super web is built. Finally all the
54 conflicts noted in the first part (as bitmaps) are transformed into real
55 edges.
57 As part of that process the webs are also classified (their spill cost
58 is calculated, and if they are spillable at all, and if not, for what
59 reason; or if they are rematerializable), and move insns are collected,
60 which are potentially coalescable.
62 The top-level entry of this file (build_i_graph) puts it all together,
63 and leaves us with a complete interference graph, which just has to
64 be colored. */
67 struct curr_use;
69 static unsigned HOST_WIDE_INT rtx_to_undefined PARAMS ((rtx));
70 static bitmap find_sub_conflicts PARAMS ((struct web_part *, unsigned int));
71 static bitmap get_sub_conflicts PARAMS ((struct web_part *, unsigned int));
72 static unsigned int undef_to_size_word PARAMS ((rtx, unsigned HOST_WIDE_INT *));
73 static bitmap undef_to_bitmap PARAMS ((struct web_part *,
74 unsigned HOST_WIDE_INT *));
75 static struct web_part * find_web_part_1 PARAMS ((struct web_part *));
76 static struct web_part * union_web_part_roots
77 PARAMS ((struct web_part *, struct web_part *));
78 static int defuse_overlap_p_1 PARAMS ((rtx, struct curr_use *));
79 static int live_out_1 PARAMS ((struct df *, struct curr_use *, rtx));
80 static int live_out PARAMS ((struct df *, struct curr_use *, rtx));
81 static rtx live_in_edge PARAMS (( struct df *, struct curr_use *, edge));
82 static void live_in PARAMS ((struct df *, struct curr_use *, rtx));
83 static int copy_insn_p PARAMS ((rtx, rtx *, rtx *));
84 static void remember_move PARAMS ((rtx));
85 static void handle_asm_insn PARAMS ((struct df *, rtx));
86 static void prune_hardregs_for_mode PARAMS ((HARD_REG_SET *,
87 enum machine_mode));
88 static void init_one_web_common PARAMS ((struct web *, rtx));
89 static void init_one_web PARAMS ((struct web *, rtx));
90 static void reinit_one_web PARAMS ((struct web *, rtx));
91 static struct web * add_subweb PARAMS ((struct web *, rtx));
92 static struct web * add_subweb_2 PARAMS ((struct web *, unsigned int));
93 static void init_web_parts PARAMS ((struct df *));
94 static void copy_conflict_list PARAMS ((struct web *));
95 static void add_conflict_edge PARAMS ((struct web *, struct web *));
96 static void build_inverse_webs PARAMS ((struct web *));
97 static void copy_web PARAMS ((struct web *, struct web_link **));
98 static void compare_and_free_webs PARAMS ((struct web_link **));
99 static void init_webs_defs_uses PARAMS ((void));
100 static unsigned int parts_to_webs_1 PARAMS ((struct df *, struct web_link **,
101 struct df_link *));
102 static void parts_to_webs PARAMS ((struct df *));
103 static void reset_conflicts PARAMS ((void));
104 #if 0
105 static void check_conflict_numbers PARAMS ((void));
106 #endif
107 static void conflicts_between_webs PARAMS ((struct df *));
108 static void remember_web_was_spilled PARAMS ((struct web *));
109 static void detect_spill_temps PARAMS ((void));
110 static int contains_pseudo PARAMS ((rtx));
111 static int want_to_remat PARAMS ((rtx x));
112 static void detect_remat_webs PARAMS ((void));
113 static void determine_web_costs PARAMS ((void));
114 static void detect_webs_set_in_cond_jump PARAMS ((void));
115 static void make_webs PARAMS ((struct df *));
116 static void moves_to_webs PARAMS ((struct df *));
117 static void connect_rmw_web_parts PARAMS ((struct df *));
118 static void update_regnos_mentioned PARAMS ((void));
119 static void livethrough_conflicts_bb PARAMS ((basic_block));
120 static void init_bb_info PARAMS ((void));
121 static void free_bb_info PARAMS ((void));
122 static void build_web_parts_and_conflicts PARAMS ((struct df *));
125 /* A sbitmap of DF_REF_IDs of uses, which are live over an abnormal
126 edge. */
127 static sbitmap live_over_abnormal;
129 /* To cache if we already saw a certain edge while analyzing one
130 use, we use a tick count incremented per use. */
131 static unsigned int visited_pass;
133 /* A sbitmap of UIDs of move insns, which we already analyzed. */
134 static sbitmap move_handled;
136 /* One such structed is allocated per insn, and traces for the currently
137 analyzed use, which web part belongs to it, and how many bytes of
138 it were still undefined when that insn was reached. */
139 struct visit_trace
141 struct web_part *wp;
142 unsigned HOST_WIDE_INT undefined;
144 /* Indexed by UID. */
145 static struct visit_trace *visit_trace;
147 /* Per basic block we have one such structure, used to speed up
148 the backtracing of uses. */
149 struct ra_bb_info
151 /* The value of visited_pass, as the first insn of this BB was reached
152 the last time. If this equals the current visited_pass, then
153 undefined is valid. Otherwise not. */
154 unsigned int pass;
155 /* The still undefined bytes at that time. The use to which this is
156 relative is the current use. */
157 unsigned HOST_WIDE_INT undefined;
158 /* Bit regno is set, if that regno is mentioned in this BB as a def, or
159 the source of a copy insn. In these cases we can not skip the whole
160 block if we reach it from the end. */
161 bitmap regnos_mentioned;
162 /* If a use reaches the end of a BB, and that BB doesn't mention its
163 regno, we skip the block, and remember the ID of that use
164 as living throughout the whole block. */
165 bitmap live_throughout;
166 /* The content of the aux field before placing a pointer to this
167 structure there. */
168 void *old_aux;
171 /* We need a fast way to describe a certain part of a register.
172 Therefore we put together the size and offset (in bytes) in one
173 integer. */
174 #define BL_TO_WORD(b, l) ((((b) & 0xFFFF) << 16) | ((l) & 0xFFFF))
175 #define BYTE_BEGIN(i) (((unsigned int)(i) >> 16) & 0xFFFF)
176 #define BYTE_LENGTH(i) ((unsigned int)(i) & 0xFFFF)
178 /* For a REG or SUBREG expression X return the size/offset pair
179 as an integer. */
181 unsigned int
182 rtx_to_bits (x)
183 rtx x;
185 unsigned int len, beg;
186 len = GET_MODE_SIZE (GET_MODE (x));
187 beg = (GET_CODE (x) == SUBREG) ? SUBREG_BYTE (x) : 0;
188 return BL_TO_WORD (beg, len);
191 /* X is a REG or SUBREG rtx. Return the bytes it touches as a bitmask. */
193 static unsigned HOST_WIDE_INT
194 rtx_to_undefined (x)
195 rtx x;
197 unsigned int len, beg;
198 unsigned HOST_WIDE_INT ret;
199 len = GET_MODE_SIZE (GET_MODE (x));
200 beg = (GET_CODE (x) == SUBREG) ? SUBREG_BYTE (x) : 0;
201 ret = ~ ((unsigned HOST_WIDE_INT) 0);
202 ret = (~(ret << len)) << beg;
203 return ret;
206 /* We remember if we've analyzed an insn for being a move insn, and if yes
207 between which operands. */
208 struct copy_p_cache
210 int seen;
211 rtx source;
212 rtx target;
215 /* On demand cache, for if insns are copy insns, and if yes, what
216 source/target they have. */
217 static struct copy_p_cache * copy_cache;
219 int *number_seen;
221 /* For INSN, return nonzero, if it's a move insn, we consider to coalesce
222 later, and place the operands in *SOURCE and *TARGET, if they are
223 not NULL. */
225 static int
226 copy_insn_p (insn, source, target)
227 rtx insn;
228 rtx *source;
229 rtx *target;
231 rtx d, s;
232 unsigned int d_regno, s_regno;
233 int uid = INSN_UID (insn);
235 if (!INSN_P (insn))
236 abort ();
238 /* First look, if we already saw this insn. */
239 if (copy_cache[uid].seen)
241 /* And if we saw it, if it's actually a copy insn. */
242 if (copy_cache[uid].seen == 1)
244 if (source)
245 *source = copy_cache[uid].source;
246 if (target)
247 *target = copy_cache[uid].target;
248 return 1;
250 return 0;
253 /* Mark it as seen, but not being a copy insn. */
254 copy_cache[uid].seen = 2;
255 insn = single_set (insn);
256 if (!insn)
257 return 0;
258 d = SET_DEST (insn);
259 s = SET_SRC (insn);
261 /* We recognize moves between subreg's as copy insns. This is used to avoid
262 conflicts of those subwebs. But they are currently _not_ used for
263 coalescing (the check for this is in remember_move() below). */
264 while (GET_CODE (d) == STRICT_LOW_PART)
265 d = XEXP (d, 0);
266 if (GET_CODE (d) != REG
267 && (GET_CODE (d) != SUBREG || GET_CODE (SUBREG_REG (d)) != REG))
268 return 0;
269 while (GET_CODE (s) == STRICT_LOW_PART)
270 s = XEXP (s, 0);
271 if (GET_CODE (s) != REG
272 && (GET_CODE (s) != SUBREG || GET_CODE (SUBREG_REG (s)) != REG))
273 return 0;
275 s_regno = (unsigned) REGNO (GET_CODE (s) == SUBREG ? SUBREG_REG (s) : s);
276 d_regno = (unsigned) REGNO (GET_CODE (d) == SUBREG ? SUBREG_REG (d) : d);
278 /* Copies between hardregs are useless for us, as not coalesable anyway. */
279 if ((s_regno < FIRST_PSEUDO_REGISTER
280 && d_regno < FIRST_PSEUDO_REGISTER)
281 || s_regno >= max_normal_pseudo
282 || d_regno >= max_normal_pseudo)
283 return 0;
285 if (source)
286 *source = s;
287 if (target)
288 *target = d;
290 /* Still mark it as seen, but as a copy insn this time. */
291 copy_cache[uid].seen = 1;
292 copy_cache[uid].source = s;
293 copy_cache[uid].target = d;
294 return 1;
297 /* We build webs, as we process the conflicts. For each use we go upward
298 the insn stream, noting any defs as potentially conflicting with the
299 current use. We stop at defs of the current regno. The conflicts are only
300 potentially, because we may never reach a def, so this is an undefined use,
301 which conflicts with nothing. */
304 /* Given a web part WP, and the location of a reg part SIZE_WORD
305 return the conflict bitmap for that reg part, or NULL if it doesn't
306 exist yet in WP. */
308 static bitmap
309 find_sub_conflicts (wp, size_word)
310 struct web_part *wp;
311 unsigned int size_word;
313 struct tagged_conflict *cl;
314 cl = wp->sub_conflicts;
315 for (; cl; cl = cl->next)
316 if (cl->size_word == size_word)
317 return cl->conflicts;
318 return NULL;
321 /* Similar to find_sub_conflicts(), but creates that bitmap, if it
322 doesn't exist. I.e. this never returns NULL. */
324 static bitmap
325 get_sub_conflicts (wp, size_word)
326 struct web_part *wp;
327 unsigned int size_word;
329 bitmap b = find_sub_conflicts (wp, size_word);
330 if (!b)
332 struct tagged_conflict *cl =
333 (struct tagged_conflict *) ra_alloc (sizeof *cl);
334 cl->conflicts = BITMAP_XMALLOC ();
335 cl->size_word = size_word;
336 cl->next = wp->sub_conflicts;
337 wp->sub_conflicts = cl;
338 b = cl->conflicts;
340 return b;
343 /* Helper table for undef_to_size_word() below for small values
344 of UNDEFINED. Offsets and lengths are byte based. */
345 static struct undef_table_s {
346 unsigned int new_undef;
347 /* size | (byte << 16) */
348 unsigned int size_word;
349 } const undef_table [] = {
350 { 0, BL_TO_WORD (0, 0)}, /* 0 */
351 { 0, BL_TO_WORD (0, 1)},
352 { 0, BL_TO_WORD (1, 1)},
353 { 0, BL_TO_WORD (0, 2)},
354 { 0, BL_TO_WORD (2, 1)}, /* 4 */
355 { 1, BL_TO_WORD (2, 1)},
356 { 2, BL_TO_WORD (2, 1)},
357 { 3, BL_TO_WORD (2, 1)},
358 { 0, BL_TO_WORD (3, 1)}, /* 8 */
359 { 1, BL_TO_WORD (3, 1)},
360 { 2, BL_TO_WORD (3, 1)},
361 { 3, BL_TO_WORD (3, 1)},
362 { 0, BL_TO_WORD (2, 2)}, /* 12 */
363 { 1, BL_TO_WORD (2, 2)},
364 { 2, BL_TO_WORD (2, 2)},
365 { 0, BL_TO_WORD (0, 4)}};
367 /* Interpret *UNDEFINED as bitmask where each bit corresponds to a byte.
368 A set bit means an undefined byte. Factor all undefined bytes into
369 groups, and return a size/ofs pair of consecutive undefined bytes,
370 but according to certain borders. Clear out those bits corrsponding
371 to bytes overlaid by that size/ofs pair. REG is only used for
372 the mode, to detect if it's a floating mode or not.
374 For example: *UNDEFINED size+ofs new *UNDEFINED
375 1111 4+0 0
376 1100 2+2 0
377 1101 2+2 1
378 0001 1+0 0
379 10101 1+4 101
383 static unsigned int
384 undef_to_size_word (reg, undefined)
385 rtx reg;
386 unsigned HOST_WIDE_INT *undefined;
388 /* When only the lower four bits are possibly set, we use
389 a fast lookup table. */
390 if (*undefined <= 15)
392 struct undef_table_s u;
393 u = undef_table[*undefined];
394 *undefined = u.new_undef;
395 return u.size_word;
398 /* Otherwise we handle certain cases directly. */
399 switch (*undefined)
401 case 0x00f0 : *undefined = 0; return BL_TO_WORD (4, 4);
402 case 0x00ff : *undefined = 0; return BL_TO_WORD (0, 8);
403 case 0x0f00 : *undefined = 0; return BL_TO_WORD (8, 4);
404 case 0x0ff0 : *undefined = 0xf0; return BL_TO_WORD (8, 4);
405 case 0x0fff :
406 if (INTEGRAL_MODE_P (GET_MODE (reg)))
407 { *undefined = 0xff; return BL_TO_WORD (8, 4); }
408 else
409 { *undefined = 0; return BL_TO_WORD (0, 12); /* XFmode */ }
410 case 0xf000 : *undefined = 0; return BL_TO_WORD (12, 4);
411 case 0xff00 : *undefined = 0; return BL_TO_WORD (8, 8);
412 case 0xfff0 : *undefined = 0xf0; return BL_TO_WORD (8, 8);
413 case 0xffff : *undefined = 0; return BL_TO_WORD (0, 16);
415 /* And if nothing matched fall back to the general solution.
416 For now unknown undefined bytes are converted to sequences
417 of maximal length 4 bytes. We could make this larger if
418 necessary. */
419 default :
421 unsigned HOST_WIDE_INT u = *undefined;
422 int word;
423 struct undef_table_s tab;
424 for (word = 0; (u & 15) == 0; word += 4)
425 u >>= 4;
426 u = u & 15;
427 tab = undef_table[u];
428 u = tab.new_undef;
429 u = (*undefined & ~((unsigned HOST_WIDE_INT)15 << word))
430 | (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);
435 break;
439 /* Put the above three functions together. For a set of undefined bytes
440 as bitmap *UNDEFINED, look for (create if necessary) and return the
441 corresponding conflict bitmap. Change *UNDEFINED to remove the bytes
442 covered by the part for that bitmap. */
444 static bitmap
445 undef_to_bitmap (wp, undefined)
446 struct web_part *wp;
447 unsigned HOST_WIDE_INT *undefined;
449 unsigned int size_word = undef_to_size_word (DF_REF_REAL_REG (wp->ref),
450 undefined);
451 return get_sub_conflicts (wp, size_word);
454 /* Returns the root of the web part P is a member of. Additionally
455 it compresses the path. P may not be NULL. */
457 static struct web_part *
458 find_web_part_1 (p)
459 struct web_part *p;
461 struct web_part *r = p;
462 struct web_part *p_next;
463 while (r->uplink)
464 r = r->uplink;
465 for (; p != r; p = p_next)
467 p_next = p->uplink;
468 p->uplink = r;
470 return r;
473 /* Fast macro for the common case (WP either being the root itself, or
474 the end of an already compressed path. */
476 #define find_web_part(wp) ((! (wp)->uplink) ? (wp) \
477 : (! (wp)->uplink->uplink) ? (wp)->uplink : find_web_part_1 (wp))
479 /* Unions together the parts R1 resp. R2 is a root of.
480 All dynamic information associated with the parts (number of spanned insns
481 and so on) is also merged.
482 The root of the resulting (possibly larger) web part is returned. */
484 static struct web_part *
485 union_web_part_roots (r1, r2)
486 struct web_part *r1, *r2;
488 if (r1 != r2)
490 /* The new root is the smaller (pointerwise) of both. This is crucial
491 to make the construction of webs from web parts work (so, when
492 scanning all parts, we see the roots before all it's childs).
493 Additionally this ensures, that if the web has a def at all, than
494 the root is a def (because all def parts are before use parts in the
495 web_parts[] array), or put another way, as soon, as the root of a
496 web part is not a def, this is an uninitialized web part. The
497 way we construct the I-graph ensures, that if a web is initialized,
498 then the first part we find (besides trivial 1 item parts) has a
499 def. */
500 if (r1 > r2)
502 struct web_part *h = r1;
503 r1 = r2;
504 r2 = h;
506 r2->uplink = r1;
507 num_webs--;
509 /* Now we merge the dynamic information of R1 and R2. */
510 r1->spanned_deaths += r2->spanned_deaths;
512 if (!r1->sub_conflicts)
513 r1->sub_conflicts = r2->sub_conflicts;
514 else if (r2->sub_conflicts)
515 /* We need to merge the conflict bitmaps from R2 into R1. */
517 struct tagged_conflict *cl1, *cl2;
518 /* First those from R2, which are also contained in R1.
519 We union the bitmaps, and free those from R2, resetting them
520 to 0. */
521 for (cl1 = r1->sub_conflicts; cl1; cl1 = cl1->next)
522 for (cl2 = r2->sub_conflicts; cl2; cl2 = cl2->next)
523 if (cl1->size_word == cl2->size_word)
525 bitmap_operation (cl1->conflicts, cl1->conflicts,
526 cl2->conflicts, BITMAP_IOR);
527 BITMAP_XFREE (cl2->conflicts);
528 cl2->conflicts = NULL;
530 /* Now the conflict lists from R2 which weren't in R1.
531 We simply copy the entries from R2 into R1' list. */
532 for (cl2 = r2->sub_conflicts; cl2;)
534 struct tagged_conflict *cl_next = cl2->next;
535 if (cl2->conflicts)
537 cl2->next = r1->sub_conflicts;
538 r1->sub_conflicts = cl2;
540 cl2 = cl_next;
543 r2->sub_conflicts = NULL;
544 r1->crosses_call |= r2->crosses_call;
546 return r1;
549 /* Convenience macro, that is cabable of unioning also non-roots. */
550 #define union_web_parts(p1, p2) \
551 ((p1 == p2) ? find_web_part (p1) \
552 : union_web_part_roots (find_web_part (p1), find_web_part (p2)))
554 /* Remember that we've handled a given move, so we don't reprocess it. */
556 static void
557 remember_move (insn)
558 rtx insn;
560 if (!TEST_BIT (move_handled, INSN_UID (insn)))
562 rtx s, d;
563 SET_BIT (move_handled, INSN_UID (insn));
564 if (copy_insn_p (insn, &s, &d))
566 /* Some sanity test for the copy insn. */
567 struct df_link *slink = DF_INSN_USES (df, insn);
568 struct df_link *link = DF_INSN_DEFS (df, insn);
569 if (!link || !link->ref || !slink || !slink->ref)
570 abort ();
571 /* The following (link->next != 0) happens when a hardreg
572 is used in wider mode (REG:DI %eax). Then df.* creates
573 a def/use for each hardreg contained therein. We only
574 allow hardregs here. */
575 if (link->next
576 && DF_REF_REGNO (link->next->ref) >= FIRST_PSEUDO_REGISTER)
577 abort ();
579 else
580 abort ();
581 /* XXX for now we don't remember move insns involving any subregs.
582 Those would be difficult to coalesce (we would need to implement
583 handling of all the subwebs in the allocator, including that such
584 subwebs could be source and target of coalesing). */
585 if (GET_CODE (s) == REG && GET_CODE (d) == REG)
587 struct move *m = (struct move *) ra_calloc (sizeof (struct move));
588 struct move_list *ml;
589 m->insn = insn;
590 ml = (struct move_list *) ra_alloc (sizeof (struct move_list));
591 ml->move = m;
592 ml->next = wl_moves;
593 wl_moves = ml;
598 /* This describes the USE currently looked at in the main-loop in
599 build_web_parts_and_conflicts(). */
600 struct curr_use {
601 struct web_part *wp;
602 /* This has a 1-bit for each byte in the USE, which is still undefined. */
603 unsigned HOST_WIDE_INT undefined;
604 /* For easy access. */
605 unsigned int regno;
606 rtx x;
607 /* If some bits of this USE are live over an abnormal edge. */
608 unsigned int live_over_abnormal;
611 /* Returns nonzero iff rtx DEF and USE have bits in common (but see below).
612 It is only called with DEF and USE being (reg:M a) or (subreg:M1 (reg:M2 a)
613 x) rtx's. Furthermore if it's a subreg rtx M1 is at least one word wide,
614 and a is a multi-word pseudo. If DEF or USE are hardregs, they are in
615 wordmode, so we don't need to check for further hardregs which would result
616 from wider references. We are never called with paradoxical subregs.
618 This returns:
619 0 for no common bits,
620 1 if DEF and USE exactly cover the same bytes,
621 2 if the DEF only covers a part of the bits of USE
622 3 if the DEF covers more than the bits of the USE, and
623 4 if both are SUBREG's of different size, but have bytes in common.
624 -1 is a special case, for when DEF and USE refer to the same regno, but
625 have for other reasons no bits in common (can only happen with
626 subregs refering to different words, or to words which already were
627 defined for this USE).
628 Furthermore it modifies use->undefined to clear the bits which get defined
629 by DEF (only for cases with partial overlap).
630 I.e. if bit 1 is set for the result != -1, the USE was completely covered,
631 otherwise a test is needed to track the already defined bytes. */
633 static int
634 defuse_overlap_p_1 (def, use)
635 rtx def;
636 struct curr_use *use;
638 int mode = 0;
639 if (def == use->x)
640 return 1;
641 if (!def)
642 return 0;
643 if (GET_CODE (def) == SUBREG)
645 if (REGNO (SUBREG_REG (def)) != use->regno)
646 return 0;
647 mode |= 1;
649 else if (REGNO (def) != use->regno)
650 return 0;
651 if (GET_CODE (use->x) == SUBREG)
652 mode |= 2;
653 switch (mode)
655 case 0: /* REG, REG */
656 return 1;
657 case 1: /* SUBREG, REG */
659 unsigned HOST_WIDE_INT old_u = use->undefined;
660 use->undefined &= ~ rtx_to_undefined (def);
661 return (old_u != use->undefined) ? 2 : -1;
663 case 2: /* REG, SUBREG */
664 return 3;
665 case 3: /* SUBREG, SUBREG */
666 if (GET_MODE_SIZE (GET_MODE (def)) == GET_MODE_SIZE (GET_MODE (use->x)))
667 /* If the size of both things is the same, the subreg's overlap
668 if they refer to the same word. */
669 if (SUBREG_BYTE (def) == SUBREG_BYTE (use->x))
670 return 1;
671 /* Now the more difficult part: the same regno is refered, but the
672 sizes of the references or the words differ. E.g.
673 (subreg:SI (reg:CDI a) 0) and (subreg:DI (reg:CDI a) 2) do not
674 overlap, wereas the latter overlaps with (subreg:SI (reg:CDI a) 3).
677 unsigned HOST_WIDE_INT old_u;
678 int b1, e1, b2, e2;
679 unsigned int bl1, bl2;
680 bl1 = rtx_to_bits (def);
681 bl2 = rtx_to_bits (use->x);
682 b1 = BYTE_BEGIN (bl1);
683 b2 = BYTE_BEGIN (bl2);
684 e1 = b1 + BYTE_LENGTH (bl1) - 1;
685 e2 = b2 + BYTE_LENGTH (bl2) - 1;
686 if (b1 > e2 || b2 > e1)
687 return -1;
688 old_u = use->undefined;
689 use->undefined &= ~ rtx_to_undefined (def);
690 return (old_u != use->undefined) ? 4 : -1;
692 default:
693 abort ();
697 /* Macro for the common case of either def and use having the same rtx,
698 or based on different regnos. */
699 #define defuse_overlap_p(def, use) \
700 ((def) == (use)->x ? 1 : \
701 (REGNO (GET_CODE (def) == SUBREG \
702 ? SUBREG_REG (def) : def) != use->regno \
703 ? 0 : defuse_overlap_p_1 (def, use)))
706 /* The use USE flows into INSN (backwards). Determine INSNs effect on it,
707 and return nonzero, if (parts of) that USE are also live before it.
708 This also notes conflicts between the USE and all DEFS in that insn,
709 and modifies the undefined bits of USE in case parts of it were set in
710 this insn. */
712 static int
713 live_out_1 (df, use, insn)
714 struct df *df ATTRIBUTE_UNUSED;
715 struct curr_use *use;
716 rtx insn;
718 int defined = 0;
719 int uid = INSN_UID (insn);
720 struct web_part *wp = use->wp;
722 /* Mark, that this insn needs this webpart live. */
723 visit_trace[uid].wp = wp;
724 visit_trace[uid].undefined = use->undefined;
726 if (INSN_P (insn))
728 unsigned int source_regno = ~0;
729 unsigned int regno = use->regno;
730 unsigned HOST_WIDE_INT orig_undef = use->undefined;
731 unsigned HOST_WIDE_INT final_undef = use->undefined;
732 rtx s = NULL;
733 unsigned int n, num_defs = insn_df[uid].num_defs;
734 struct ref **defs = insn_df[uid].defs;
736 /* We want to access the root webpart. */
737 wp = find_web_part (wp);
738 if (GET_CODE (insn) == CALL_INSN)
739 wp->crosses_call = 1;
740 else if (copy_insn_p (insn, &s, NULL))
741 source_regno = REGNO (GET_CODE (s) == SUBREG ? SUBREG_REG (s) : s);
743 /* Look at all DEFS in this insn. */
744 for (n = 0; n < num_defs; n++)
746 struct ref *ref = defs[n];
747 int lap;
749 /* Reset the undefined bits for each iteration, in case this
750 insn has more than one set, and one of them sets this regno.
751 But still the original undefined part conflicts with the other
752 sets. */
753 use->undefined = orig_undef;
754 if ((lap = defuse_overlap_p (DF_REF_REG (ref), use)) != 0)
756 if (lap == -1)
757 /* Same regnos but non-overlapping or already defined bits,
758 so ignore this DEF, or better said make the yet undefined
759 part and this DEF conflicting. */
761 unsigned HOST_WIDE_INT undef;
762 undef = use->undefined;
763 while (undef)
764 bitmap_set_bit (undef_to_bitmap (wp, &undef),
765 DF_REF_ID (ref));
766 continue;
768 if ((lap & 1) != 0)
769 /* The current DEF completely covers the USE, so we can
770 stop traversing the code looking for further DEFs. */
771 defined = 1;
772 else
773 /* We have a partial overlap. */
775 final_undef &= use->undefined;
776 if (final_undef == 0)
777 /* Now the USE is completely defined, which means, that
778 we can stop looking for former DEFs. */
779 defined = 1;
780 /* If this is a partial overlap, which left some bits
781 in USE undefined, we normally would need to create
782 conflicts between that undefined part and the part of
783 this DEF which overlapped with some of the formerly
784 undefined bits. We don't need to do this, because both
785 parts of this DEF (that which overlaps, and that which
786 doesn't) are written together in this one DEF, and can
787 not be colored in a way which would conflict with
788 the USE. This is only true for partial overlap,
789 because only then the DEF and USE have bits in common,
790 which makes the DEF move, if the USE moves, making them
791 aligned.
792 If they have no bits in common (lap == -1), they are
793 really independent. Therefore we there made a
794 conflict above. */
796 /* This is at least a partial overlap, so we need to union
797 the web parts. */
798 wp = union_web_parts (wp, &web_parts[DF_REF_ID (ref)]);
800 else
802 /* The DEF and the USE don't overlap at all, different
803 regnos. I.e. make conflicts between the undefined bits,
804 and that DEF. */
805 unsigned HOST_WIDE_INT undef = use->undefined;
807 if (regno == source_regno)
808 /* This triggers only, when this was a copy insn and the
809 source is at least a part of the USE currently looked at.
810 In this case only the bits of the USE conflict with the
811 DEF, which are not covered by the source of this copy
812 insn, and which are still undefined. I.e. in the best
813 case (the whole reg being the source), _no_ conflicts
814 between that USE and this DEF (the target of the move)
815 are created by this insn (though they might be by
816 others). This is a super case of the normal copy insn
817 only between full regs. */
819 undef &= ~ rtx_to_undefined (s);
821 if (undef)
823 /*struct web_part *cwp;
824 cwp = find_web_part (&web_parts[DF_REF_ID
825 (ref)]);*/
827 /* TODO: somehow instead of noting the ID of the LINK
828 use an ID nearer to the root webpart of that LINK.
829 We can't use the root itself, because we later use the
830 ID to look at the form (reg or subreg, and if yes,
831 which subreg) of this conflict. This means, that we
832 need to remember in the root an ID for each form, and
833 maintaining this, when merging web parts. This makes
834 the bitmaps smaller. */
836 bitmap_set_bit (undef_to_bitmap (wp, &undef),
837 DF_REF_ID (ref));
838 while (undef);
842 if (defined)
843 use->undefined = 0;
844 else
846 /* If this insn doesn't completely define the USE, increment also
847 it's spanned deaths count (if this insn contains a death). */
848 if (uid >= death_insns_max_uid)
849 abort ();
850 if (TEST_BIT (insns_with_deaths, uid))
851 wp->spanned_deaths++;
852 use->undefined = final_undef;
856 return !defined;
859 /* Same as live_out_1() (actually calls it), but caches some information.
860 E.g. if we reached this INSN with the current regno already, and the
861 current undefined bits are a subset of those as we came here, we
862 simply connect the web parts of the USE, and the one cached for this
863 INSN, and additionally return zero, indicating we don't need to traverse
864 this path any longer (all effect were already seen, as we first reached
865 this insn). */
867 static inline int
868 live_out (df, use, insn)
869 struct df *df;
870 struct curr_use *use;
871 rtx insn;
873 unsigned int uid = INSN_UID (insn);
874 if (visit_trace[uid].wp
875 && DF_REF_REGNO (visit_trace[uid].wp->ref) == use->regno
876 && (use->undefined & ~visit_trace[uid].undefined) == 0)
878 union_web_parts (visit_trace[uid].wp, use->wp);
879 /* Don't search any further, as we already were here with this regno. */
880 return 0;
882 else
883 return live_out_1 (df, use, insn);
886 /* The current USE reached a basic block head. The edge E is one
887 of the predecessors edges. This evaluates the effect of the predecessor
888 block onto the USE, and returns the next insn, which should be looked at.
889 This either is the last insn of that pred. block, or the first one.
890 The latter happens, when the pred. block has no possible effect on the
891 USE, except for conflicts. In that case, it's remembered, that the USE
892 is live over that whole block, and it's skipped. Otherwise we simply
893 continue with the last insn of the block.
895 This also determines the effects of abnormal edges, and remembers
896 which uses are live at the end of that basic block. */
898 static rtx
899 live_in_edge (df, use, e)
900 struct df *df;
901 struct curr_use *use;
902 edge e;
904 struct ra_bb_info *info_pred;
905 rtx next_insn;
906 /* Call used hard regs die over an exception edge, ergo
907 they don't reach the predecessor block, so ignore such
908 uses. And also don't set the live_over_abnormal flag
909 for them. */
910 if ((e->flags & EDGE_EH) && use->regno < FIRST_PSEUDO_REGISTER
911 && call_used_regs[use->regno])
912 return NULL_RTX;
913 if (e->flags & EDGE_ABNORMAL)
914 use->live_over_abnormal = 1;
915 bitmap_set_bit (live_at_end[e->src->index], DF_REF_ID (use->wp->ref));
916 info_pred = (struct ra_bb_info *) e->src->aux;
917 next_insn = e->src->end;
919 /* If the last insn of the pred. block doesn't completely define the
920 current use, we need to check the block. */
921 if (live_out (df, use, next_insn))
923 /* If the current regno isn't mentioned anywhere in the whole block,
924 and the complete use is still undefined... */
925 if (!bitmap_bit_p (info_pred->regnos_mentioned, use->regno)
926 && (rtx_to_undefined (use->x) & ~use->undefined) == 0)
928 /* ...we can hop over the whole block and defer conflict
929 creation to later. */
930 bitmap_set_bit (info_pred->live_throughout,
931 DF_REF_ID (use->wp->ref));
932 next_insn = e->src->head;
934 return next_insn;
936 else
937 return NULL_RTX;
940 /* USE flows into the end of the insns preceding INSN. Determine
941 their effects (in live_out()) and possibly loop over the preceding INSN,
942 or call itself recursively on a basic block border. When a topleve
943 call of this function returns the USE is completely analyzed. I.e.
944 its def-use chain (at least) is built, possibly connected with other
945 def-use chains, and all defs during that chain are noted. */
947 static void
948 live_in (df, use, insn)
949 struct df *df;
950 struct curr_use *use;
951 rtx insn;
953 unsigned int loc_vpass = visited_pass;
955 /* Note, that, even _if_ we are called with use->wp a root-part, this might
956 become non-root in the for() loop below (due to live_out() unioning
957 it). So beware, not to change use->wp in a way, for which only root-webs
958 are allowed. */
959 while (1)
961 int uid = INSN_UID (insn);
962 basic_block bb = BLOCK_FOR_INSN (insn);
963 number_seen[uid]++;
965 /* We want to be as fast as possible, so explicitely write
966 this loop. */
967 for (insn = PREV_INSN (insn); insn && !INSN_P (insn);
968 insn = PREV_INSN (insn))
970 if (!insn)
971 return;
972 if (bb != BLOCK_FOR_INSN (insn))
974 edge e;
975 unsigned HOST_WIDE_INT undef = use->undefined;
976 struct ra_bb_info *info = (struct ra_bb_info *) bb->aux;
977 if ((e = bb->pred) == NULL)
978 return;
979 /* We now check, if we already traversed the predecessors of this
980 block for the current pass and the current set of undefined
981 bits. If yes, we don't need to check the predecessors again.
982 So, conceptually this information is tagged to the first
983 insn of a basic block. */
984 if (info->pass == loc_vpass && (undef & ~info->undefined) == 0)
985 return;
986 info->pass = loc_vpass;
987 info->undefined = undef;
988 /* All but the last predecessor are handled recursively. */
989 for (; e->pred_next; e = e->pred_next)
991 insn = live_in_edge (df, use, e);
992 if (insn)
993 live_in (df, use, insn);
994 use->undefined = undef;
996 insn = live_in_edge (df, use, e);
997 if (!insn)
998 return;
1000 else if (!live_out (df, use, insn))
1001 return;
1005 /* Determine all regnos which are mentioned in a basic block, in an
1006 interesting way. Interesting here means either in a def, or as the
1007 source of a move insn. We only look at insns added since the last
1008 pass. */
1010 static void
1011 update_regnos_mentioned ()
1013 int last_uid = last_max_uid;
1014 rtx insn;
1015 basic_block bb;
1016 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
1017 if (INSN_P (insn))
1019 /* Don't look at old insns. */
1020 if (INSN_UID (insn) < last_uid)
1022 /* XXX We should also remember moves over iterations (we already
1023 save the cache, but not the movelist). */
1024 if (copy_insn_p (insn, NULL, NULL))
1025 remember_move (insn);
1027 else if ((bb = BLOCK_FOR_INSN (insn)) != NULL)
1029 rtx source;
1030 struct ra_bb_info *info = (struct ra_bb_info *) bb->aux;
1031 bitmap mentioned = info->regnos_mentioned;
1032 struct df_link *link;
1033 if (copy_insn_p (insn, &source, NULL))
1035 remember_move (insn);
1036 bitmap_set_bit (mentioned,
1037 REGNO (GET_CODE (source) == SUBREG
1038 ? SUBREG_REG (source) : source));
1040 for (link = DF_INSN_DEFS (df, insn); link; link = link->next)
1041 if (link->ref)
1042 bitmap_set_bit (mentioned, DF_REF_REGNO (link->ref));
1047 /* Handle the uses which reach a block end, but were defered due
1048 to it's regno not being mentioned in that block. This adds the
1049 remaining conflicts and updates also the crosses_call and
1050 spanned_deaths members. */
1052 static void
1053 livethrough_conflicts_bb (bb)
1054 basic_block bb;
1056 struct ra_bb_info *info = (struct ra_bb_info *) bb->aux;
1057 rtx insn;
1058 bitmap all_defs;
1059 int first, use_id;
1060 unsigned int deaths = 0;
1061 unsigned int contains_call = 0;
1063 /* If there are no defered uses, just return. */
1064 if ((first = bitmap_first_set_bit (info->live_throughout)) < 0)
1065 return;
1067 /* First collect the IDs of all defs, count the number of death
1068 containing insns, and if there's some call_insn here. */
1069 all_defs = BITMAP_XMALLOC ();
1070 for (insn = bb->head; insn; insn = NEXT_INSN (insn))
1072 if (INSN_P (insn))
1074 unsigned int n;
1075 struct ra_insn_info info;
1077 info = insn_df[INSN_UID (insn)];
1078 for (n = 0; n < info.num_defs; n++)
1079 bitmap_set_bit (all_defs, DF_REF_ID (info.defs[n]));
1080 if (TEST_BIT (insns_with_deaths, INSN_UID (insn)))
1081 deaths++;
1082 if (GET_CODE (insn) == CALL_INSN)
1083 contains_call = 1;
1085 if (insn == bb->end)
1086 break;
1089 /* And now, if we have found anything, make all live_through
1090 uses conflict with all defs, and update their other members. */
1091 if (deaths > 0 || bitmap_first_set_bit (all_defs) >= 0)
1092 EXECUTE_IF_SET_IN_BITMAP (info->live_throughout, first, use_id,
1094 struct web_part *wp = &web_parts[df->def_id + use_id];
1095 unsigned int bl = rtx_to_bits (DF_REF_REG (wp->ref));
1096 bitmap conflicts;
1097 wp = find_web_part (wp);
1098 wp->spanned_deaths += deaths;
1099 wp->crosses_call |= contains_call;
1100 conflicts = get_sub_conflicts (wp, bl);
1101 bitmap_operation (conflicts, conflicts, all_defs, BITMAP_IOR);
1104 BITMAP_XFREE (all_defs);
1107 /* Allocate the per basic block info for traversing the insn stream for
1108 building live ranges. */
1110 static void
1111 init_bb_info ()
1113 basic_block bb;
1114 FOR_ALL_BB (bb)
1116 struct ra_bb_info *info =
1117 (struct ra_bb_info *) xcalloc (1, sizeof *info);
1118 info->regnos_mentioned = BITMAP_XMALLOC ();
1119 info->live_throughout = BITMAP_XMALLOC ();
1120 info->old_aux = bb->aux;
1121 bb->aux = (void *) info;
1125 /* Free that per basic block info. */
1127 static void
1128 free_bb_info ()
1130 basic_block bb;
1131 FOR_ALL_BB (bb)
1133 struct ra_bb_info *info = (struct ra_bb_info *) bb->aux;
1134 BITMAP_XFREE (info->regnos_mentioned);
1135 BITMAP_XFREE (info->live_throughout);
1136 bb->aux = info->old_aux;
1137 free (info);
1141 /* Toplevel function for the first part of this file.
1142 Connect web parts, thereby implicitely building webs, and remember
1143 their conflicts. */
1145 static void
1146 build_web_parts_and_conflicts (df)
1147 struct df *df;
1149 struct df_link *link;
1150 struct curr_use use;
1151 basic_block bb;
1153 number_seen = (int *) xcalloc (get_max_uid (), sizeof (int));
1154 visit_trace = (struct visit_trace *) xcalloc (get_max_uid (),
1155 sizeof (visit_trace[0]));
1156 update_regnos_mentioned ();
1158 /* Here's the main loop.
1159 It goes through all insn's, connects web parts along the way, notes
1160 conflicts between webparts, and remembers move instructions. */
1161 visited_pass = 0;
1162 for (use.regno = 0; use.regno < (unsigned int)max_regno; use.regno++)
1163 if (use.regno >= FIRST_PSEUDO_REGISTER || !fixed_regs[use.regno])
1164 for (link = df->regs[use.regno].uses; link; link = link->next)
1165 if (link->ref)
1167 struct ref *ref = link->ref;
1168 rtx insn = DF_REF_INSN (ref);
1169 /* Only recheck marked or new uses, or uses from hardregs. */
1170 if (use.regno >= FIRST_PSEUDO_REGISTER
1171 && DF_REF_ID (ref) < last_use_id
1172 && !TEST_BIT (last_check_uses, DF_REF_ID (ref)))
1173 continue;
1174 use.wp = &web_parts[df->def_id + DF_REF_ID (ref)];
1175 use.x = DF_REF_REG (ref);
1176 use.live_over_abnormal = 0;
1177 use.undefined = rtx_to_undefined (use.x);
1178 visited_pass++;
1179 live_in (df, &use, insn);
1180 if (use.live_over_abnormal)
1181 SET_BIT (live_over_abnormal, DF_REF_ID (ref));
1184 dump_number_seen ();
1185 FOR_ALL_BB (bb)
1187 struct ra_bb_info *info = (struct ra_bb_info *) bb->aux;
1188 livethrough_conflicts_bb (bb);
1189 bitmap_zero (info->live_throughout);
1190 info->pass = 0;
1192 free (visit_trace);
1193 free (number_seen);
1196 /* Here we look per insn, for DF references being in uses _and_ defs.
1197 This means, in the RTL a (REG xx) expression was seen as a
1198 read/modify/write, as happens for (set (subreg:SI (reg:DI xx)) (...))
1199 e.g. Our code has created two webs for this, as it should. Unfortunately,
1200 as the REG reference is only one time in the RTL we can't color
1201 both webs different (arguably this also would be wrong for a real
1202 read-mod-write instruction), so we must reconnect such webs. */
1204 static void
1205 connect_rmw_web_parts (df)
1206 struct df *df;
1208 unsigned int i;
1210 for (i = 0; i < df->use_id; i++)
1212 struct web_part *wp1 = &web_parts[df->def_id + i];
1213 rtx reg;
1214 struct df_link *link;
1215 if (!wp1->ref)
1216 continue;
1217 /* If it's an uninitialized web, we don't want to connect it to others,
1218 as the read cycle in read-mod-write had probably no effect. */
1219 if (find_web_part (wp1) >= &web_parts[df->def_id])
1220 continue;
1221 reg = DF_REF_REAL_REG (wp1->ref);
1222 link = DF_INSN_DEFS (df, DF_REF_INSN (wp1->ref));
1223 for (; link; link = link->next)
1224 if (reg == DF_REF_REAL_REG (link->ref))
1226 struct web_part *wp2 = &web_parts[DF_REF_ID (link->ref)];
1227 union_web_parts (wp1, wp2);
1232 /* Deletes all hardregs from *S which are not allowed for MODE. */
1234 static void
1235 prune_hardregs_for_mode (s, mode)
1236 HARD_REG_SET *s;
1237 enum machine_mode mode;
1239 AND_HARD_REG_SET (*s, hardregs_for_mode[(int) mode]);
1242 /* Initialize the members of a web, which are deducible from REG. */
1244 static void
1245 init_one_web_common (web, reg)
1246 struct web *web;
1247 rtx reg;
1249 if (GET_CODE (reg) != REG)
1250 abort ();
1251 /* web->id isn't initialized here. */
1252 web->regno = REGNO (reg);
1253 web->orig_x = reg;
1254 if (!web->dlink)
1256 web->dlink = (struct dlist *) ra_calloc (sizeof (struct dlist));
1257 DLIST_WEB (web->dlink) = web;
1259 /* XXX
1260 the former (superunion) doesn't constrain the graph enough. E.g.
1261 on x86 QImode _requires_ QI_REGS, but as alternate class usually
1262 GENERAL_REGS is given. So the graph is not constrained enough,
1263 thinking it has more freedom then it really has, which leads
1264 to repeated spill tryings. OTOH the latter (only using preferred
1265 class) is too constrained, as normally (e.g. with all SImode
1266 pseudos), they can be allocated also in the alternate class.
1267 What we really want, are the _exact_ hard regs allowed, not
1268 just a class. Later. */
1269 /*web->regclass = reg_class_superunion
1270 [reg_preferred_class (web->regno)]
1271 [reg_alternate_class (web->regno)];*/
1272 /*web->regclass = reg_preferred_class (web->regno);*/
1273 web->regclass = reg_class_subunion
1274 [reg_preferred_class (web->regno)] [reg_alternate_class (web->regno)];
1275 web->regclass = reg_preferred_class (web->regno);
1276 if (web->regno < FIRST_PSEUDO_REGISTER)
1278 web->color = web->regno;
1279 put_web (web, PRECOLORED);
1280 web->num_conflicts = UINT_MAX;
1281 web->add_hardregs = 0;
1282 CLEAR_HARD_REG_SET (web->usable_regs);
1283 SET_HARD_REG_BIT (web->usable_regs, web->regno);
1284 web->num_freedom = 1;
1286 else
1288 HARD_REG_SET alternate;
1289 web->color = -1;
1290 put_web (web, INITIAL);
1291 /* add_hardregs is wrong in multi-length classes, e.g.
1292 using a DFmode pseudo on x86 can result in class FLOAT_INT_REGS,
1293 where, if it finally is allocated to GENERAL_REGS it needs two,
1294 if allocated to FLOAT_REGS only one hardreg. XXX */
1295 web->add_hardregs =
1296 CLASS_MAX_NREGS (web->regclass, PSEUDO_REGNO_MODE (web->regno)) - 1;
1297 web->num_conflicts = 0 * web->add_hardregs;
1298 COPY_HARD_REG_SET (web->usable_regs,
1299 reg_class_contents[reg_preferred_class (web->regno)]);
1300 COPY_HARD_REG_SET (alternate,
1301 reg_class_contents[reg_alternate_class (web->regno)]);
1302 IOR_HARD_REG_SET (web->usable_regs, alternate);
1303 /*IOR_HARD_REG_SET (web->usable_regs,
1304 reg_class_contents[reg_alternate_class
1305 (web->regno)]);*/
1306 AND_COMPL_HARD_REG_SET (web->usable_regs, never_use_colors);
1307 prune_hardregs_for_mode (&web->usable_regs,
1308 PSEUDO_REGNO_MODE (web->regno));
1309 #ifdef CLASS_CANNOT_CHANGE_MODE
1310 if (web->mode_changed)
1311 AND_COMPL_HARD_REG_SET (web->usable_regs, reg_class_contents[
1312 (int) CLASS_CANNOT_CHANGE_MODE]);
1313 #endif
1314 web->num_freedom = hard_regs_count (web->usable_regs);
1315 web->num_freedom -= web->add_hardregs;
1316 if (!web->num_freedom)
1317 abort();
1319 COPY_HARD_REG_SET (web->orig_usable_regs, web->usable_regs);
1322 /* Initializes WEBs members from REG or zero them. */
1324 static void
1325 init_one_web (web, reg)
1326 struct web *web;
1327 rtx reg;
1329 memset (web, 0, sizeof (struct web));
1330 init_one_web_common (web, reg);
1331 web->useless_conflicts = BITMAP_XMALLOC ();
1334 /* WEB is an old web, meaning it came from the last pass, and got a
1335 color. We want to remember some of it's info, so zero only some
1336 members. */
1338 static void
1339 reinit_one_web (web, reg)
1340 struct web *web;
1341 rtx reg;
1343 web->old_color = web->color + 1;
1344 init_one_web_common (web, reg);
1345 web->span_deaths = 0;
1346 web->spill_temp = 0;
1347 web->orig_spill_temp = 0;
1348 web->use_my_regs = 0;
1349 web->spill_cost = 0;
1350 web->was_spilled = 0;
1351 web->is_coalesced = 0;
1352 web->artificial = 0;
1353 web->live_over_abnormal = 0;
1354 web->mode_changed = 0;
1355 web->move_related = 0;
1356 web->in_load = 0;
1357 web->target_of_spilled_move = 0;
1358 web->num_aliased = 0;
1359 if (web->type == PRECOLORED)
1361 web->num_defs = 0;
1362 web->num_uses = 0;
1363 web->orig_spill_cost = 0;
1365 CLEAR_HARD_REG_SET (web->bias_colors);
1366 CLEAR_HARD_REG_SET (web->prefer_colors);
1367 web->reg_rtx = NULL;
1368 web->stack_slot = NULL;
1369 web->pattern = NULL;
1370 web->alias = NULL;
1371 if (web->moves)
1372 abort ();
1373 if (!web->useless_conflicts)
1374 abort ();
1377 /* Insert and returns a subweb corresponding to REG into WEB (which
1378 becomes its super web). It must not exist already. */
1380 static struct web *
1381 add_subweb (web, reg)
1382 struct web *web;
1383 rtx reg;
1385 struct web *w;
1386 if (GET_CODE (reg) != SUBREG)
1387 abort ();
1388 w = (struct web *) xmalloc (sizeof (struct web));
1389 /* Copy most content from parent-web. */
1390 *w = *web;
1391 /* And initialize the private stuff. */
1392 w->orig_x = reg;
1393 w->add_hardregs = CLASS_MAX_NREGS (web->regclass, GET_MODE (reg)) - 1;
1394 w->num_conflicts = 0 * w->add_hardregs;
1395 w->num_defs = 0;
1396 w->num_uses = 0;
1397 w->dlink = NULL;
1398 w->parent_web = web;
1399 w->subreg_next = web->subreg_next;
1400 web->subreg_next = w;
1401 return w;
1404 /* Similar to add_subweb(), but instead of relying on a given SUBREG,
1405 we have just a size and an offset of the subpart of the REG rtx.
1406 In difference to add_subweb() this marks the new subweb as artificial. */
1408 static struct web *
1409 add_subweb_2 (web, size_word)
1410 struct web *web;
1411 unsigned int size_word;
1413 /* To get a correct mode for the to be produced subreg, we don't want to
1414 simply do a mode_for_size() for the mode_class of the whole web.
1415 Suppose we deal with a CDImode web, but search for a 8 byte part.
1416 Now mode_for_size() would only search in the class MODE_COMPLEX_INT
1417 and would find CSImode which probably is not what we want. Instead
1418 we want DImode, which is in a completely other class. For this to work
1419 we instead first search the already existing subwebs, and take
1420 _their_ modeclasses as base for a search for ourself. */
1421 rtx ref_rtx = (web->subreg_next ? web->subreg_next : web)->orig_x;
1422 unsigned int size = BYTE_LENGTH (size_word) * BITS_PER_UNIT;
1423 enum machine_mode mode;
1424 mode = mode_for_size (size, GET_MODE_CLASS (GET_MODE (ref_rtx)), 0);
1425 if (mode == BLKmode)
1426 mode = mode_for_size (size, MODE_INT, 0);
1427 if (mode == BLKmode)
1428 abort ();
1429 web = add_subweb (web, gen_rtx_SUBREG (mode, web->orig_x,
1430 BYTE_BEGIN (size_word)));
1431 web->artificial = 1;
1432 return web;
1435 /* Initialize all the web parts we are going to need. */
1437 static void
1438 init_web_parts (df)
1439 struct df *df;
1441 int regno;
1442 unsigned int no;
1443 num_webs = 0;
1444 for (no = 0; no < df->def_id; no++)
1446 if (df->defs[no])
1448 if (no < last_def_id && web_parts[no].ref != df->defs[no])
1449 abort ();
1450 web_parts[no].ref = df->defs[no];
1451 /* Uplink might be set from the last iteration. */
1452 if (!web_parts[no].uplink)
1453 num_webs++;
1455 else
1456 /* The last iteration might have left .ref set, while df_analyse()
1457 removed that ref (due to a removed copy insn) from the df->defs[]
1458 array. As we don't check for that in realloc_web_parts()
1459 we do that here. */
1460 web_parts[no].ref = NULL;
1462 for (no = 0; no < df->use_id; no++)
1464 if (df->uses[no])
1466 if (no < last_use_id
1467 && web_parts[no + df->def_id].ref != df->uses[no])
1468 abort ();
1469 web_parts[no + df->def_id].ref = df->uses[no];
1470 if (!web_parts[no + df->def_id].uplink)
1471 num_webs++;
1473 else
1474 web_parts[no + df->def_id].ref = NULL;
1477 /* We want to have only one web for each precolored register. */
1478 for (regno = 0; regno < FIRST_PSEUDO_REGISTER; regno++)
1480 struct web_part *r1 = NULL;
1481 struct df_link *link;
1482 /* Here once was a test, if there is any DEF at all, and only then to
1483 merge all the parts. This was incorrect, we really also want to have
1484 only one web-part for hardregs, even if there is no explicit DEF. */
1485 /* Link together all defs... */
1486 for (link = df->regs[regno].defs; link; link = link->next)
1487 if (link->ref)
1489 struct web_part *r2 = &web_parts[DF_REF_ID (link->ref)];
1490 if (!r1)
1491 r1 = r2;
1492 else
1493 r1 = union_web_parts (r1, r2);
1495 /* ... and all uses. */
1496 for (link = df->regs[regno].uses; link; link = link->next)
1497 if (link->ref)
1499 struct web_part *r2 = &web_parts[df->def_id
1500 + DF_REF_ID (link->ref)];
1501 if (!r1)
1502 r1 = r2;
1503 else
1504 r1 = union_web_parts (r1, r2);
1509 /* In case we want to remember the conflict list of a WEB, before adding
1510 new conflicts, we copy it here to orig_conflict_list. */
1512 static void
1513 copy_conflict_list (web)
1514 struct web *web;
1516 struct conflict_link *cl;
1517 if (web->orig_conflict_list || web->have_orig_conflicts)
1518 abort ();
1519 web->have_orig_conflicts = 1;
1520 for (cl = web->conflict_list; cl; cl = cl->next)
1522 struct conflict_link *ncl;
1523 ncl = (struct conflict_link *) ra_alloc (sizeof *ncl);
1524 ncl->t = cl->t;
1525 ncl->sub = NULL;
1526 ncl->next = web->orig_conflict_list;
1527 web->orig_conflict_list = ncl;
1528 if (cl->sub)
1530 struct sub_conflict *sl, *nsl;
1531 for (sl = cl->sub; sl; sl = sl->next)
1533 nsl = (struct sub_conflict *) ra_alloc (sizeof *nsl);
1534 nsl->s = sl->s;
1535 nsl->t = sl->t;
1536 nsl->next = ncl->sub;
1537 ncl->sub = nsl;
1543 /* Possibly add an edge from web FROM to TO marking a conflict between
1544 those two. This is one half of marking a complete conflict, which notes
1545 in FROM, that TO is a conflict. Adding TO to FROM's conflicts might
1546 make other conflicts superflous, because the current TO overlaps some web
1547 already being in conflict with FROM. In this case the smaller webs are
1548 deleted from the conflict list. Likewise if TO is overlapped by a web
1549 already in the list, it isn't added at all. Note, that this can only
1550 happen, if SUBREG webs are involved. */
1552 static void
1553 add_conflict_edge (from, to)
1554 struct web *from, *to;
1556 if (from->type != PRECOLORED)
1558 struct web *pfrom = find_web_for_subweb (from);
1559 struct web *pto = find_web_for_subweb (to);
1560 struct sub_conflict *sl;
1561 struct conflict_link *cl = pfrom->conflict_list;
1562 int may_delete = 1;
1564 /* This can happen when subwebs of one web conflict with each
1565 other. In live_out_1() we created such conflicts between yet
1566 undefined webparts and defs of parts which didn't overlap with the
1567 undefined bits. Then later they nevertheless could have merged into
1568 one web, and then we land here. */
1569 if (pfrom == pto)
1570 return;
1571 if (remember_conflicts && !pfrom->have_orig_conflicts)
1572 copy_conflict_list (pfrom);
1573 if (!TEST_BIT (sup_igraph, (pfrom->id * num_webs + pto->id)))
1575 cl = (struct conflict_link *) ra_alloc (sizeof (*cl));
1576 cl->t = pto;
1577 cl->sub = NULL;
1578 cl->next = pfrom->conflict_list;
1579 pfrom->conflict_list = cl;
1580 if (pto->type != SELECT && pto->type != COALESCED)
1581 pfrom->num_conflicts += 1 + pto->add_hardregs;
1582 SET_BIT (sup_igraph, (pfrom->id * num_webs + pto->id));
1583 may_delete = 0;
1585 else
1586 /* We don't need to test for cl==NULL, because at this point
1587 a cl with cl->t==pto is guaranteed to exist. */
1588 while (cl->t != pto)
1589 cl = cl->next;
1590 if (pfrom != from || pto != to)
1592 /* This is a subconflict which should be added.
1593 If we inserted cl in this invocation, we really need to add this
1594 subconflict. If we did _not_ add it here, we only add the
1595 subconflict, if cl already had subconflicts, because otherwise
1596 this indicated, that the whole webs already conflict, which
1597 means we are not interested in this subconflict. */
1598 if (!may_delete || cl->sub != NULL)
1600 sl = (struct sub_conflict *) ra_alloc (sizeof (*sl));
1601 sl->s = from;
1602 sl->t = to;
1603 sl->next = cl->sub;
1604 cl->sub = sl;
1607 else
1608 /* pfrom == from && pto == to means, that we are not interested
1609 anymore in the subconflict list for this pair, because anyway
1610 the whole webs conflict. */
1611 cl->sub = NULL;
1615 /* Record a conflict between two webs, if we haven't recorded it
1616 already. */
1618 void
1619 record_conflict (web1, web2)
1620 struct web *web1, *web2;
1622 unsigned int id1 = web1->id, id2 = web2->id;
1623 unsigned int index = igraph_index (id1, id2);
1624 /* Trivial non-conflict or already recorded conflict. */
1625 if (web1 == web2 || TEST_BIT (igraph, index))
1626 return;
1627 if (id1 == id2)
1628 abort ();
1629 /* As fixed_regs are no targets for allocation, conflicts with them
1630 are pointless. */
1631 if ((web1->regno < FIRST_PSEUDO_REGISTER && fixed_regs[web1->regno])
1632 || (web2->regno < FIRST_PSEUDO_REGISTER && fixed_regs[web2->regno]))
1633 return;
1634 /* Conflicts with hardregs, which are not even a candidate
1635 for this pseudo are also pointless. */
1636 if ((web1->type == PRECOLORED
1637 && ! TEST_HARD_REG_BIT (web2->usable_regs, web1->regno))
1638 || (web2->type == PRECOLORED
1639 && ! TEST_HARD_REG_BIT (web1->usable_regs, web2->regno)))
1640 return;
1641 /* Similar if the set of possible hardregs don't intersect. This iteration
1642 those conflicts are useless (and would make num_conflicts wrong, because
1643 num_freedom is calculated from the set of possible hardregs).
1644 But in presence of spilling and incremental building of the graph we
1645 need to note all uses of webs conflicting with the spilled ones.
1646 Because the set of possible hardregs can change in the next round for
1647 spilled webs, we possibly have then conflicts with webs which would
1648 be excluded now (because then hardregs intersect). But we actually
1649 need to check those uses, and to get hold of them, we need to remember
1650 also webs conflicting with this one, although not conflicting in this
1651 round because of non-intersecting hardregs. */
1652 if (web1->type != PRECOLORED && web2->type != PRECOLORED
1653 && ! hard_regs_intersect_p (&web1->usable_regs, &web2->usable_regs))
1655 struct web *p1 = find_web_for_subweb (web1);
1656 struct web *p2 = find_web_for_subweb (web2);
1657 /* We expect these to be rare enough to justify bitmaps. And because
1658 we have only a special use for it, we note only the superwebs. */
1659 bitmap_set_bit (p1->useless_conflicts, p2->id);
1660 bitmap_set_bit (p2->useless_conflicts, p1->id);
1661 return;
1663 SET_BIT (igraph, index);
1664 add_conflict_edge (web1, web2);
1665 add_conflict_edge (web2, web1);
1668 /* For each web W this produces the missing subwebs Wx, such that it's
1669 possible to exactly specify (W-Wy) for all already existing subwebs Wy. */
1671 static void
1672 build_inverse_webs (web)
1673 struct web *web;
1675 struct web *sweb = web->subreg_next;
1676 unsigned HOST_WIDE_INT undef;
1678 undef = rtx_to_undefined (web->orig_x);
1679 for (; sweb; sweb = sweb->subreg_next)
1680 /* Only create inverses of non-artificial webs. */
1681 if (!sweb->artificial)
1683 unsigned HOST_WIDE_INT bits;
1684 bits = undef & ~ rtx_to_undefined (sweb->orig_x);
1685 while (bits)
1687 unsigned int size_word = undef_to_size_word (web->orig_x, &bits);
1688 if (!find_subweb_2 (web, size_word))
1689 add_subweb_2 (web, size_word);
1694 /* Copies the content of WEB to a new one, and link it into WL.
1695 Used for consistency checking. */
1697 static void
1698 copy_web (web, wl)
1699 struct web *web;
1700 struct web_link **wl;
1702 struct web *cweb = (struct web *) xmalloc (sizeof *cweb);
1703 struct web_link *link = (struct web_link *) ra_alloc (sizeof *link);
1704 link->next = *wl;
1705 *wl = link;
1706 link->web = cweb;
1707 *cweb = *web;
1710 /* Given a list of webs LINK, compare the content of the webs therein
1711 with the global webs of the same ID. For consistency checking. */
1713 static void
1714 compare_and_free_webs (link)
1715 struct web_link **link;
1717 struct web_link *wl;
1718 for (wl = *link; wl; wl = wl->next)
1720 struct web *web1 = wl->web;
1721 struct web *web2 = ID2WEB (web1->id);
1722 if (web1->regno != web2->regno
1723 || web1->crosses_call != web2->crosses_call
1724 || web1->live_over_abnormal != web2->live_over_abnormal
1725 || web1->mode_changed != web2->mode_changed
1726 || !rtx_equal_p (web1->orig_x, web2->orig_x)
1727 || web1->type != web2->type
1728 /* Only compare num_defs/num_uses with non-hardreg webs.
1729 E.g. the number of uses of the framepointer changes due to
1730 inserting spill code. */
1731 || (web1->type != PRECOLORED &&
1732 (web1->num_uses != web2->num_uses
1733 || web1->num_defs != web2->num_defs)))
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 = (struct ref **) xmalloc (web->num_defs *
1777 sizeof (web->defs[0]));
1778 if (web->num_uses)
1779 web->uses = (struct ref **) xmalloc (web->num_uses *
1780 sizeof (web->uses[0]));
1781 def_i = use_i = 0;
1782 for (link = web->temp_refs; link; link = link->next)
1784 if (DF_REF_REG_DEF_P (link->ref))
1785 web->defs[def_i++] = link->ref;
1786 else
1787 web->uses[use_i++] = link->ref;
1789 web->temp_refs = NULL;
1790 if (def_i != web->num_defs || use_i != web->num_uses)
1791 abort ();
1795 /* Called by parts_to_webs(). This creates (or recreates) the webs (and
1796 subwebs) from web parts, gives them IDs (only to super webs), and sets
1797 up use2web and def2web arrays. */
1799 static unsigned int
1800 parts_to_webs_1 (df, copy_webs, all_refs)
1801 struct df *df;
1802 struct web_link **copy_webs;
1803 struct df_link *all_refs;
1805 unsigned int i;
1806 unsigned int webnum;
1807 unsigned int def_id = df->def_id;
1808 unsigned int use_id = df->use_id;
1809 struct web_part *wp_first_use = &web_parts[def_id];
1811 /* For each root web part: create and initialize a new web,
1812 setup def2web[] and use2web[] for all defs and uses, and
1813 id2web for all new webs. */
1815 webnum = 0;
1816 for (i = 0; i < def_id + use_id; i++)
1818 struct web *subweb, *web = 0; /* Initialize web to silence warnings. */
1819 struct web_part *wp = &web_parts[i];
1820 struct ref *ref = wp->ref;
1821 unsigned int ref_id;
1822 rtx reg;
1823 if (!ref)
1824 continue;
1825 ref_id = i;
1826 if (i >= def_id)
1827 ref_id -= def_id;
1828 all_refs[i].ref = ref;
1829 reg = DF_REF_REG (ref);
1830 if (! wp->uplink)
1832 /* If we have a web part root, create a new web. */
1833 unsigned int newid = ~(unsigned)0;
1834 unsigned int old_web = 0;
1836 /* In the first pass, there are no old webs, so unconditionally
1837 allocate a new one. */
1838 if (ra_pass == 1)
1840 web = (struct web *) xmalloc (sizeof (struct web));
1841 newid = last_num_webs++;
1842 init_one_web (web, GET_CODE (reg) == SUBREG
1843 ? SUBREG_REG (reg) : reg);
1845 /* Otherwise, we look for an old web. */
1846 else
1848 /* Remember, that use2web == def2web + def_id.
1849 Ergo is def2web[i] == use2web[i - def_id] for i >= def_id.
1850 So we only need to look into def2web[] array.
1851 Try to look at the web, which formerly belonged to this
1852 def (or use). */
1853 web = def2web[i];
1854 /* Or which belonged to this hardreg. */
1855 if (!web && DF_REF_REGNO (ref) < FIRST_PSEUDO_REGISTER)
1856 web = hardreg2web[DF_REF_REGNO (ref)];
1857 if (web)
1859 /* If we found one, reuse it. */
1860 web = find_web_for_subweb (web);
1861 remove_list (web->dlink, &WEBS(INITIAL));
1862 old_web = 1;
1863 copy_web (web, copy_webs);
1865 else
1867 /* Otherwise use a new one. First from the free list. */
1868 if (WEBS(FREE))
1869 web = DLIST_WEB (pop_list (&WEBS(FREE)));
1870 else
1872 /* Else allocate a new one. */
1873 web = (struct web *) xmalloc (sizeof (struct web));
1874 newid = last_num_webs++;
1877 /* The id is zeroed in init_one_web(). */
1878 if (newid == ~(unsigned)0)
1879 newid = web->id;
1880 if (old_web)
1881 reinit_one_web (web, GET_CODE (reg) == SUBREG
1882 ? SUBREG_REG (reg) : reg);
1883 else
1884 init_one_web (web, GET_CODE (reg) == SUBREG
1885 ? SUBREG_REG (reg) : reg);
1886 web->old_web = (old_web && web->type != PRECOLORED) ? 1 : 0;
1888 web->span_deaths = wp->spanned_deaths;
1889 web->crosses_call = wp->crosses_call;
1890 web->id = newid;
1891 web->temp_refs = NULL;
1892 webnum++;
1893 if (web->regno < FIRST_PSEUDO_REGISTER && !hardreg2web[web->regno])
1894 hardreg2web[web->regno] = web;
1895 else if (web->regno < FIRST_PSEUDO_REGISTER
1896 && hardreg2web[web->regno] != web)
1897 abort ();
1900 /* If this reference already had a web assigned, we are done.
1901 This test better is equivalent to the web being an old web.
1902 Otherwise something is screwed. (This is tested) */
1903 if (def2web[i] != NULL)
1905 web = def2web[i];
1906 web = find_web_for_subweb (web);
1907 /* But if this ref includes a mode change, or was a use live
1908 over an abnormal call, set appropriate flags in the web. */
1909 if ((DF_REF_FLAGS (ref) & DF_REF_MODE_CHANGE) != 0
1910 && web->regno >= FIRST_PSEUDO_REGISTER)
1911 web->mode_changed = 1;
1912 if (i >= def_id
1913 && TEST_BIT (live_over_abnormal, ref_id))
1914 web->live_over_abnormal = 1;
1915 /* And check, that it's not a newly allocated web. This would be
1916 an inconsistency. */
1917 if (!web->old_web || web->type == PRECOLORED)
1918 abort ();
1919 continue;
1921 /* In case this was no web part root, we need to initialize WEB
1922 from the ref2web array belonging to the root. */
1923 if (wp->uplink)
1925 struct web_part *rwp = find_web_part (wp);
1926 unsigned int j = DF_REF_ID (rwp->ref);
1927 if (rwp < wp_first_use)
1928 web = def2web[j];
1929 else
1930 web = use2web[j];
1931 web = find_web_for_subweb (web);
1934 /* Remember all references for a web in a single linked list. */
1935 all_refs[i].next = web->temp_refs;
1936 web->temp_refs = &all_refs[i];
1938 /* And the test, that if def2web[i] was NULL above, that we are _not_
1939 an old web. */
1940 if (web->old_web && web->type != PRECOLORED)
1941 abort ();
1943 /* Possible create a subweb, if this ref was a subreg. */
1944 if (GET_CODE (reg) == SUBREG)
1946 subweb = find_subweb (web, reg);
1947 if (!subweb)
1949 subweb = add_subweb (web, reg);
1950 if (web->old_web)
1951 abort ();
1954 else
1955 subweb = web;
1957 /* And look, if the ref involves an invalid mode change. */
1958 if ((DF_REF_FLAGS (ref) & DF_REF_MODE_CHANGE) != 0
1959 && web->regno >= FIRST_PSEUDO_REGISTER)
1960 web->mode_changed = 1;
1962 /* Setup def2web, or use2web, and increment num_defs or num_uses. */
1963 if (i < def_id)
1965 /* Some sanity checks. */
1966 if (ra_pass > 1)
1968 struct web *compare = def2web[i];
1969 if (i < last_def_id)
1971 if (web->old_web && compare != subweb)
1972 abort ();
1974 if (!web->old_web && compare)
1975 abort ();
1976 if (compare && compare != subweb)
1977 abort ();
1979 def2web[i] = subweb;
1980 web->num_defs++;
1982 else
1984 if (ra_pass > 1)
1986 struct web *compare = use2web[ref_id];
1987 if (ref_id < last_use_id)
1989 if (web->old_web && compare != subweb)
1990 abort ();
1992 if (!web->old_web && compare)
1993 abort ();
1994 if (compare && compare != subweb)
1995 abort ();
1997 use2web[ref_id] = subweb;
1998 web->num_uses++;
1999 if (TEST_BIT (live_over_abnormal, ref_id))
2000 web->live_over_abnormal = 1;
2004 /* We better now have exactly as many webs as we had web part roots. */
2005 if (webnum != num_webs)
2006 abort ();
2008 return webnum;
2011 /* This builds full webs out of web parts, without relating them to each
2012 other (i.e. without creating the conflict edges). */
2014 static void
2015 parts_to_webs (df)
2016 struct df *df;
2018 unsigned int i;
2019 unsigned int webnum;
2020 struct web_link *copy_webs = NULL;
2021 struct dlist *d;
2022 struct df_link *all_refs;
2023 num_subwebs = 0;
2025 /* First build webs and ordinary subwebs. */
2026 all_refs = (struct df_link *) xcalloc (df->def_id + df->use_id,
2027 sizeof (all_refs[0]));
2028 webnum = parts_to_webs_1 (df, &copy_webs, all_refs);
2030 /* Setup the webs for hardregs which are still missing (weren't
2031 mentioned in the code). */
2032 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2033 if (!hardreg2web[i])
2035 struct web *web = (struct web *) xmalloc (sizeof (struct web));
2036 init_one_web (web, gen_rtx_REG (reg_raw_mode[i], i));
2037 web->id = last_num_webs++;
2038 hardreg2web[web->regno] = web;
2040 num_webs = last_num_webs;
2042 /* Now create all artificial subwebs, i.e. those, which do
2043 not correspond to a real subreg in the current function's RTL, but
2044 which nevertheless is a target of a conflict.
2045 XXX we need to merge this loop with the one above, which means, we need
2046 a way to later override the artificiality. Beware: currently
2047 add_subweb_2() relies on the existence of normal subwebs for deducing
2048 a sane mode to use for the artificial subwebs. */
2049 for (i = 0; i < df->def_id + df->use_id; i++)
2051 struct web_part *wp = &web_parts[i];
2052 struct tagged_conflict *cl;
2053 struct web *web;
2054 if (wp->uplink || !wp->ref)
2056 if (wp->sub_conflicts)
2057 abort ();
2058 continue;
2060 web = def2web[i];
2061 web = find_web_for_subweb (web);
2062 for (cl = wp->sub_conflicts; cl; cl = cl->next)
2063 if (!find_subweb_2 (web, cl->size_word))
2064 add_subweb_2 (web, cl->size_word);
2067 /* And now create artificial subwebs needed for representing the inverse
2068 of some subwebs. This also gives IDs to all subwebs. */
2069 webnum = last_num_webs;
2070 for (d = WEBS(INITIAL); d; d = d->next)
2072 struct web *web = DLIST_WEB (d);
2073 if (web->subreg_next)
2075 struct web *sweb;
2076 build_inverse_webs (web);
2077 for (sweb = web->subreg_next; sweb; sweb = sweb->subreg_next)
2078 sweb->id = webnum++;
2082 /* Now that everyone has an ID, we can setup the id2web array. */
2083 id2web = (struct web **) xcalloc (webnum, sizeof (id2web[0]));
2084 for (d = WEBS(INITIAL); d; d = d->next)
2086 struct web *web = DLIST_WEB (d);
2087 ID2WEB (web->id) = web;
2088 for (web = web->subreg_next; web; web = web->subreg_next)
2089 ID2WEB (web->id) = web;
2091 num_subwebs = webnum - last_num_webs;
2092 num_allwebs = num_webs + num_subwebs;
2093 num_webs += num_subwebs;
2095 /* Allocate and clear the conflict graph bitmaps. */
2096 igraph = sbitmap_alloc (num_webs * num_webs / 2);
2097 sup_igraph = sbitmap_alloc (num_webs * num_webs);
2098 sbitmap_zero (igraph);
2099 sbitmap_zero (sup_igraph);
2101 /* Distibute the references to their webs. */
2102 init_webs_defs_uses ();
2103 /* And do some sanity checks if old webs, and those recreated from the
2104 really are the same. */
2105 compare_and_free_webs (&copy_webs);
2106 free (all_refs);
2109 /* This deletes all conflicts to and from webs which need to be renewed
2110 in this pass of the allocator, i.e. those which were spilled in the
2111 last pass. Furthermore it also rebuilds the bitmaps for the remaining
2112 conflicts. */
2114 static void
2115 reset_conflicts ()
2117 unsigned int i;
2118 bitmap newwebs = BITMAP_XMALLOC ();
2119 for (i = 0; i < num_webs - num_subwebs; i++)
2121 struct web *web = ID2WEB (i);
2122 /* Hardreg webs and non-old webs are new webs (which
2123 need rebuilding). */
2124 if (web->type == PRECOLORED || !web->old_web)
2125 bitmap_set_bit (newwebs, web->id);
2128 for (i = 0; i < num_webs - num_subwebs; i++)
2130 struct web *web = ID2WEB (i);
2131 struct conflict_link *cl;
2132 struct conflict_link **pcl;
2133 pcl = &(web->conflict_list);
2135 /* First restore the conflict list to be like it was before
2136 coalescing. */
2137 if (web->have_orig_conflicts)
2139 web->conflict_list = web->orig_conflict_list;
2140 web->orig_conflict_list = NULL;
2142 if (web->orig_conflict_list)
2143 abort ();
2145 /* New non-precolored webs, have no conflict list. */
2146 if (web->type != PRECOLORED && !web->old_web)
2148 *pcl = NULL;
2149 /* Useless conflicts will be rebuilt completely. But check
2150 for cleanlyness, as the web might have come from the
2151 free list. */
2152 if (bitmap_first_set_bit (web->useless_conflicts) >= 0)
2153 abort ();
2155 else
2157 /* Useless conflicts with new webs will be rebuilt if they
2158 are still there. */
2159 bitmap_operation (web->useless_conflicts, web->useless_conflicts,
2160 newwebs, BITMAP_AND_COMPL);
2161 /* Go through all conflicts, and retain those to old webs. */
2162 for (cl = web->conflict_list; cl; cl = cl->next)
2164 if (cl->t->old_web || cl->t->type == PRECOLORED)
2166 *pcl = cl;
2167 pcl = &(cl->next);
2169 /* Also restore the entries in the igraph bitmaps. */
2170 web->num_conflicts += 1 + cl->t->add_hardregs;
2171 SET_BIT (sup_igraph, (web->id * num_webs + cl->t->id));
2172 /* No subconflicts mean full webs conflict. */
2173 if (!cl->sub)
2174 SET_BIT (igraph, igraph_index (web->id, cl->t->id));
2175 else
2176 /* Else only the parts in cl->sub must be in the
2177 bitmap. */
2179 struct sub_conflict *sl;
2180 for (sl = cl->sub; sl; sl = sl->next)
2181 SET_BIT (igraph, igraph_index (sl->s->id, sl->t->id));
2185 *pcl = NULL;
2187 web->have_orig_conflicts = 0;
2189 BITMAP_XFREE (newwebs);
2192 /* For each web check it's num_conflicts member against that
2193 number, as calculated from scratch from all neighbors. */
2195 #if 0
2196 static void
2197 check_conflict_numbers ()
2199 unsigned int i;
2200 for (i = 0; i < num_webs; i++)
2202 struct web *web = ID2WEB (i);
2203 int new_conf = 0 * web->add_hardregs;
2204 struct conflict_link *cl;
2205 for (cl = web->conflict_list; cl; cl = cl->next)
2206 if (cl->t->type != SELECT && cl->t->type != COALESCED)
2207 new_conf += 1 + cl->t->add_hardregs;
2208 if (web->type != PRECOLORED && new_conf != web->num_conflicts)
2209 abort ();
2212 #endif
2214 /* Convert the conflicts between web parts to conflicts between full webs.
2216 This can't be done in parts_to_webs(), because for recording conflicts
2217 between webs we need to know their final usable_regs set, which is used
2218 to discard non-conflicts (between webs having no hard reg in common).
2219 But this is set for spill temporaries only after the webs itself are
2220 built. Until then the usable_regs set is based on the pseudo regno used
2221 in this web, which may contain far less registers than later determined.
2222 This would result in us loosing conflicts (due to record_conflict()
2223 thinking that a web can only be allocated to the current usable_regs,
2224 whereas later this is extended) leading to colorings, where some regs which
2225 in reality conflict get the same color. */
2227 static void
2228 conflicts_between_webs (df)
2229 struct df *df;
2231 unsigned int i;
2232 #ifdef STACK_REGS
2233 struct dlist *d;
2234 #endif
2235 bitmap ignore_defs = BITMAP_XMALLOC ();
2236 unsigned int have_ignored;
2237 unsigned int *pass_cache = (unsigned int *) xcalloc (num_webs, sizeof (int));
2238 unsigned int pass = 0;
2240 if (ra_pass > 1)
2241 reset_conflicts ();
2243 /* It is possible, that in the conflict bitmaps still some defs I are noted,
2244 which have web_parts[I].ref being NULL. This can happen, when from the
2245 last iteration the conflict bitmap for this part wasn't deleted, but a
2246 conflicting move insn was removed. It's DEF is still in the conflict
2247 bitmap, but it doesn't exist anymore in df->defs. To not have to check
2248 it in the tight loop below, we instead remember the ID's of them in a
2249 bitmap, and loop only over IDs which are not in it. */
2250 for (i = 0; i < df->def_id; i++)
2251 if (web_parts[i].ref == NULL)
2252 bitmap_set_bit (ignore_defs, i);
2253 have_ignored = (bitmap_first_set_bit (ignore_defs) >= 0);
2255 /* Now record all conflicts between webs. Note that we only check
2256 the conflict bitmaps of all defs. Conflict bitmaps are only in
2257 webpart roots. If they are in uses, those uses are roots, which
2258 means, that this is an uninitialized web, whose conflicts
2259 don't matter. Nevertheless for hardregs we also need to check uses.
2260 E.g. hardregs used for argument passing have no DEF in the RTL,
2261 but if they have uses, they indeed conflict with all DEFs they
2262 overlap. */
2263 for (i = 0; i < df->def_id + df->use_id; i++)
2265 struct tagged_conflict *cl = web_parts[i].sub_conflicts;
2266 struct web *supweb1;
2267 if (!cl
2268 || (i >= df->def_id
2269 && DF_REF_REGNO (web_parts[i].ref) >= FIRST_PSEUDO_REGISTER))
2270 continue;
2271 supweb1 = def2web[i];
2272 supweb1 = find_web_for_subweb (supweb1);
2273 for (; cl; cl = cl->next)
2274 if (cl->conflicts)
2276 int j;
2277 struct web *web1 = find_subweb_2 (supweb1, cl->size_word);
2278 if (have_ignored)
2279 bitmap_operation (cl->conflicts, cl->conflicts, ignore_defs,
2280 BITMAP_AND_COMPL);
2281 /* We reduce the number of calls to record_conflict() with this
2282 pass thing. record_conflict() itself also has some early-out
2283 optimizations, but here we can use the special properties of
2284 the loop (constant web1) to reduce that even more.
2285 We once used an sbitmap of already handled web indices,
2286 but sbitmaps are slow to clear and bitmaps are slow to
2287 set/test. The current approach needs more memory, but
2288 locality is large. */
2289 pass++;
2291 /* Note, that there are only defs in the conflicts bitset. */
2292 EXECUTE_IF_SET_IN_BITMAP (
2293 cl->conflicts, 0, j,
2295 struct web *web2 = def2web[j];
2296 unsigned int id2 = web2->id;
2297 if (pass_cache[id2] != pass)
2299 pass_cache[id2] = pass;
2300 record_conflict (web1, web2);
2306 free (pass_cache);
2307 BITMAP_XFREE (ignore_defs);
2309 #ifdef STACK_REGS
2310 /* Pseudos can't go in stack regs if they are live at the beginning of
2311 a block that is reached by an abnormal edge. */
2312 for (d = WEBS(INITIAL); d; d = d->next)
2314 struct web *web = DLIST_WEB (d);
2315 int j;
2316 if (web->live_over_abnormal)
2317 for (j = FIRST_STACK_REG; j <= LAST_STACK_REG; j++)
2318 record_conflict (web, hardreg2web[j]);
2320 #endif
2323 /* Remember that a web was spilled, and change some characteristics
2324 accordingly. */
2326 static void
2327 remember_web_was_spilled (web)
2328 struct web *web;
2330 int i;
2331 unsigned int found_size = 0;
2332 int adjust;
2333 web->spill_temp = 1;
2335 /* From now on don't use reg_pref/alt_class (regno) anymore for
2336 this web, but instead usable_regs. We can't use spill_temp for
2337 this, as it might get reset later, when we are coalesced to a
2338 non-spill-temp. In that case we still want to use usable_regs. */
2339 web->use_my_regs = 1;
2341 /* We don't constrain spill temporaries in any way for now.
2342 It's wrong sometimes to have the same constraints or
2343 preferences as the original pseudo, esp. if they were very narrow.
2344 (E.g. there once was a reg wanting class AREG (only one register)
2345 without alternative class. As long, as also the spill-temps for
2346 this pseudo had the same constraints it was spilled over and over.
2347 Ideally we want some constraints also on spill-temps: Because they are
2348 not only loaded/stored, but also worked with, any constraints from insn
2349 alternatives needs applying. Currently this is dealt with by reload, as
2350 many other things, but at some time we want to integrate that
2351 functionality into the allocator. */
2352 if (web->regno >= max_normal_pseudo)
2354 COPY_HARD_REG_SET (web->usable_regs,
2355 reg_class_contents[reg_preferred_class (web->regno)]);
2356 IOR_HARD_REG_SET (web->usable_regs,
2357 reg_class_contents[reg_alternate_class (web->regno)]);
2359 else
2360 COPY_HARD_REG_SET (web->usable_regs,
2361 reg_class_contents[(int) GENERAL_REGS]);
2362 AND_COMPL_HARD_REG_SET (web->usable_regs, never_use_colors);
2363 prune_hardregs_for_mode (&web->usable_regs, PSEUDO_REGNO_MODE (web->regno));
2364 #ifdef CLASS_CANNOT_CHANGE_MODE
2365 if (web->mode_changed)
2366 AND_COMPL_HARD_REG_SET (web->usable_regs, reg_class_contents[
2367 (int) CLASS_CANNOT_CHANGE_MODE]);
2368 #endif
2369 web->num_freedom = hard_regs_count (web->usable_regs);
2370 if (!web->num_freedom)
2371 abort();
2372 COPY_HARD_REG_SET (web->orig_usable_regs, web->usable_regs);
2373 /* Now look for a class, which is subset of our constraints, to
2374 setup add_hardregs, and regclass for debug output. */
2375 web->regclass = NO_REGS;
2376 for (i = (int) ALL_REGS - 1; i > 0; i--)
2378 unsigned int size;
2379 HARD_REG_SET test;
2380 COPY_HARD_REG_SET (test, reg_class_contents[i]);
2381 AND_COMPL_HARD_REG_SET (test, never_use_colors);
2382 GO_IF_HARD_REG_SUBSET (test, web->usable_regs, found);
2383 continue;
2384 found:
2385 /* Measure the actual number of bits which really are overlapping
2386 the target regset, not just the reg_class_size. */
2387 size = hard_regs_count (test);
2388 if (found_size < size)
2390 web->regclass = (enum reg_class) i;
2391 found_size = size;
2395 adjust = 0 * web->add_hardregs;
2396 web->add_hardregs =
2397 CLASS_MAX_NREGS (web->regclass, PSEUDO_REGNO_MODE (web->regno)) - 1;
2398 web->num_freedom -= web->add_hardregs;
2399 if (!web->num_freedom)
2400 abort();
2401 adjust -= 0 * web->add_hardregs;
2402 web->num_conflicts -= adjust;
2405 /* Look at each web, if it is used as spill web. Or better said,
2406 if it will be spillable in this pass. */
2408 static void
2409 detect_spill_temps ()
2411 struct dlist *d;
2412 bitmap already = BITMAP_XMALLOC ();
2414 /* Detect webs used for spill temporaries. */
2415 for (d = WEBS(INITIAL); d; d = d->next)
2417 struct web *web = DLIST_WEB (d);
2419 /* Below only the detection of spill temporaries. We never spill
2420 precolored webs, so those can't be spill temporaries. The code above
2421 (remember_web_was_spilled) can't currently cope with hardregs
2422 anyway. */
2423 if (web->regno < FIRST_PSEUDO_REGISTER)
2424 continue;
2425 /* Uninitialized webs can't be spill-temporaries. */
2426 if (web->num_defs == 0)
2427 continue;
2429 /* A web with only defs and no uses can't be spilled. Nevertheless
2430 it must get a color, as it takes away an register from all webs
2431 live at these defs. So we make it a short web. */
2432 if (web->num_uses == 0)
2433 web->spill_temp = 3;
2434 /* A web which was spilled last time, but for which no insns were
2435 emitted (can happen with IR spilling ignoring sometimes
2436 all deaths). */
2437 else if (web->changed)
2438 web->spill_temp = 1;
2439 /* A spill temporary has one def, one or more uses, all uses
2440 are in one insn, and either the def or use insn was inserted
2441 by the allocator. */
2442 /* XXX not correct currently. There might also be spill temps
2443 involving more than one def. Usually that's an additional
2444 clobber in the using instruction. We might also constrain
2445 ourself to that, instead of like currently marking all
2446 webs involving any spill insns at all. */
2447 else
2449 unsigned int i;
2450 int spill_involved = 0;
2451 for (i = 0; i < web->num_uses && !spill_involved; i++)
2452 if (DF_REF_INSN_UID (web->uses[i]) >= orig_max_uid)
2453 spill_involved = 1;
2454 for (i = 0; i < web->num_defs && !spill_involved; i++)
2455 if (DF_REF_INSN_UID (web->defs[i]) >= orig_max_uid)
2456 spill_involved = 1;
2458 if (spill_involved/* && ra_pass > 2*/)
2460 int num_deaths = web->span_deaths;
2461 /* Mark webs involving at least one spill insn as
2462 spill temps. */
2463 remember_web_was_spilled (web);
2464 /* Search for insns which define and use the web in question
2465 at the same time, i.e. look for rmw insns. If these insns
2466 are also deaths of other webs they might have been counted
2467 as such into web->span_deaths. But because of the rmw nature
2468 of this insn it is no point where a load/reload could be
2469 placed successfully (it would still conflict with the
2470 dead web), so reduce the number of spanned deaths by those
2471 insns. Note that sometimes such deaths are _not_ counted,
2472 so negative values can result. */
2473 bitmap_zero (already);
2474 for (i = 0; i < web->num_defs; i++)
2476 rtx insn = web->defs[i]->insn;
2477 if (TEST_BIT (insns_with_deaths, INSN_UID (insn))
2478 && !bitmap_bit_p (already, INSN_UID (insn)))
2480 unsigned int j;
2481 bitmap_set_bit (already, INSN_UID (insn));
2482 /* Only decrement it once for each insn. */
2483 for (j = 0; j < web->num_uses; j++)
2484 if (web->uses[j]->insn == insn)
2486 num_deaths--;
2487 break;
2491 /* But mark them specially if they could possibly be spilled,
2492 either because they cross some deaths (without the above
2493 mentioned ones) or calls. */
2494 if (web->crosses_call || num_deaths > 0)
2495 web->spill_temp = 1 * 2;
2497 /* A web spanning no deaths can't be spilled either. No loads
2498 would be created for it, ergo no defs. So the insns wouldn't
2499 change making the graph not easier to color. Make this also
2500 a short web. Don't do this if it crosses calls, as these are
2501 also points of reloads. */
2502 else if (web->span_deaths == 0 && !web->crosses_call)
2503 web->spill_temp = 3;
2505 web->orig_spill_temp = web->spill_temp;
2507 BITMAP_XFREE (already);
2510 /* Returns nonzero if the rtx MEM refers somehow to a stack location. */
2513 memref_is_stack_slot (mem)
2514 rtx mem;
2516 rtx ad = XEXP (mem, 0);
2517 rtx x;
2518 if (GET_CODE (ad) != PLUS || GET_CODE (XEXP (ad, 1)) != CONST_INT)
2519 return 0;
2520 x = XEXP (ad, 0);
2521 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
2522 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
2523 || x == stack_pointer_rtx)
2524 return 1;
2525 return 0;
2528 /* Returns nonzero, if rtx X somewhere contains any pseudo register. */
2530 static int
2531 contains_pseudo (x)
2532 rtx x;
2534 const char *fmt;
2535 int i;
2536 if (GET_CODE (x) == SUBREG)
2537 x = SUBREG_REG (x);
2538 if (GET_CODE (x) == REG)
2540 if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
2541 return 1;
2542 else
2543 return 0;
2546 fmt = GET_RTX_FORMAT (GET_CODE (x));
2547 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2548 if (fmt[i] == 'e')
2550 if (contains_pseudo (XEXP (x, i)))
2551 return 1;
2553 else if (fmt[i] == 'E')
2555 int j;
2556 for (j = 0; j < XVECLEN (x, i); j++)
2557 if (contains_pseudo (XVECEXP (x, i, j)))
2558 return 1;
2560 return 0;
2563 /* Returns nonzero, if we are able to rematerialize something with
2564 value X. If it's not a general operand, we test if we can produce
2565 a valid insn which set a pseudo to that value, and that insn doesn't
2566 clobber anything. */
2568 static GTY(()) rtx remat_test_insn;
2569 static int
2570 want_to_remat (x)
2571 rtx x;
2573 int num_clobbers = 0;
2574 int icode;
2576 /* If this is a valid operand, we are OK. If it's VOIDmode, we aren't. */
2577 if (general_operand (x, GET_MODE (x)))
2578 return 1;
2580 /* Otherwise, check if we can make a valid insn from it. First initialize
2581 our test insn if we haven't already. */
2582 if (remat_test_insn == 0)
2584 remat_test_insn
2585 = make_insn_raw (gen_rtx_SET (VOIDmode,
2586 gen_rtx_REG (word_mode,
2587 FIRST_PSEUDO_REGISTER * 2),
2588 const0_rtx));
2589 NEXT_INSN (remat_test_insn) = PREV_INSN (remat_test_insn) = 0;
2592 /* Now make an insn like the one we would make when rematerializing
2593 the value X and see if valid. */
2594 PUT_MODE (SET_DEST (PATTERN (remat_test_insn)), GET_MODE (x));
2595 SET_SRC (PATTERN (remat_test_insn)) = x;
2596 /* XXX For now we don't allow any clobbers to be added, not just no
2597 hardreg clobbers. */
2598 return ((icode = recog (PATTERN (remat_test_insn), remat_test_insn,
2599 &num_clobbers)) >= 0
2600 && (num_clobbers == 0
2601 /*|| ! added_clobbers_hard_reg_p (icode)*/));
2604 /* Look at all webs, if they perhaps are rematerializable.
2605 They are, if all their defs are simple sets to the same value,
2606 and that value is simple enough, and want_to_remat() holds for it. */
2608 static void
2609 detect_remat_webs ()
2611 struct dlist *d;
2612 for (d = WEBS(INITIAL); d; d = d->next)
2614 struct web *web = DLIST_WEB (d);
2615 unsigned int i;
2616 rtx pat = NULL_RTX;
2617 /* Hardregs and useless webs aren't spilled -> no remat necessary.
2618 Defless webs obviously also can't be rematerialized. */
2619 if (web->regno < FIRST_PSEUDO_REGISTER || !web->num_defs
2620 || !web->num_uses)
2621 continue;
2622 for (i = 0; i < web->num_defs; i++)
2624 rtx insn;
2625 rtx set = single_set (insn = DF_REF_INSN (web->defs[i]));
2626 rtx src;
2627 if (!set)
2628 break;
2629 src = SET_SRC (set);
2630 /* When only subregs of the web are set it isn't easily
2631 rematerializable. */
2632 if (!rtx_equal_p (SET_DEST (set), web->orig_x))
2633 break;
2634 /* If we already have a pattern it must be equal to the current. */
2635 if (pat && !rtx_equal_p (pat, src))
2636 break;
2637 /* Don't do the expensive checks multiple times. */
2638 if (pat)
2639 continue;
2640 /* For now we allow only constant sources. */
2641 if ((CONSTANT_P (src)
2642 /* If the whole thing is stable already, it is a source for
2643 remat, no matter how complicated (probably all needed
2644 resources for it are live everywhere, and don't take
2645 additional register resources). */
2646 /* XXX Currently we can't use patterns which contain
2647 pseudos, _even_ if they are stable. The code simply isn't
2648 prepared for that. All those operands can't be spilled (or
2649 the dependent remat webs are not remat anymore), so they
2650 would be oldwebs in the next iteration. But currently
2651 oldwebs can't have their references changed. The
2652 incremental machinery barfs on that. */
2653 || (!rtx_unstable_p (src) && !contains_pseudo (src))
2654 /* Additionally also memrefs to stack-slots are usefull, when
2655 we created them ourself. They might not have set their
2656 unchanging flag set, but nevertheless they are stable across
2657 the livetime in question. */
2658 || (GET_CODE (src) == MEM
2659 && INSN_UID (insn) >= orig_max_uid
2660 && memref_is_stack_slot (src)))
2661 /* And we must be able to construct an insn without
2662 side-effects to actually load that value into a reg. */
2663 && want_to_remat (src))
2664 pat = src;
2665 else
2666 break;
2668 if (pat && i == web->num_defs)
2669 web->pattern = pat;
2673 /* Determine the spill costs of all webs. */
2675 static void
2676 determine_web_costs ()
2678 struct dlist *d;
2679 for (d = WEBS(INITIAL); d; d = d->next)
2681 unsigned int i, num_loads;
2682 int load_cost, store_cost;
2683 unsigned HOST_WIDE_INT w;
2684 struct web *web = DLIST_WEB (d);
2685 if (web->type == PRECOLORED)
2686 continue;
2687 /* Get costs for one load/store. Note that we offset them by 1,
2688 because some patterns have a zero rtx_cost(), but we of course
2689 still need the actual load/store insns. With zero all those
2690 webs would be the same, no matter how often and where
2691 they are used. */
2692 if (web->pattern)
2694 /* This web is rematerializable. Beware, we set store_cost to
2695 zero optimistically assuming, that we indeed don't emit any
2696 stores in the spill-code addition. This might be wrong if
2697 at the point of the load not all needed resources are
2698 available, in which case we emit a stack-based load, for
2699 which we in turn need the according stores. */
2700 load_cost = 1 + rtx_cost (web->pattern, 0);
2701 store_cost = 0;
2703 else
2705 load_cost = 1 + MEMORY_MOVE_COST (GET_MODE (web->orig_x),
2706 web->regclass, 1);
2707 store_cost = 1 + MEMORY_MOVE_COST (GET_MODE (web->orig_x),
2708 web->regclass, 0);
2710 /* We create only loads at deaths, whose number is in span_deaths. */
2711 num_loads = MIN (web->span_deaths, web->num_uses);
2712 for (w = 0, i = 0; i < web->num_uses; i++)
2713 w += DF_REF_BB (web->uses[i])->frequency + 1;
2714 if (num_loads < web->num_uses)
2715 w = (w * num_loads + web->num_uses - 1) / web->num_uses;
2716 web->spill_cost = w * load_cost;
2717 if (store_cost)
2719 for (w = 0, i = 0; i < web->num_defs; i++)
2720 w += DF_REF_BB (web->defs[i])->frequency + 1;
2721 web->spill_cost += w * store_cost;
2723 web->orig_spill_cost = web->spill_cost;
2727 /* Detect webs which are set in a conditional jump insn (possibly a
2728 decrement-and-branch type of insn), and mark them not to be
2729 spillable. The stores for them would need to be placed on edges,
2730 which destroys the CFG. (Somewhen we want to deal with that XXX) */
2732 static void
2733 detect_webs_set_in_cond_jump ()
2735 basic_block bb;
2736 FOR_EACH_BB (bb)
2737 if (GET_CODE (bb->end) == JUMP_INSN)
2739 struct df_link *link;
2740 for (link = DF_INSN_DEFS (df, bb->end); link; link = link->next)
2741 if (link->ref && DF_REF_REGNO (link->ref) >= FIRST_PSEUDO_REGISTER)
2743 struct web *web = def2web[DF_REF_ID (link->ref)];
2744 web->orig_spill_temp = web->spill_temp = 3;
2749 /* Second top-level function of this file.
2750 Converts the connected web parts to full webs. This means, it allocates
2751 all webs, and initializes all fields, including detecting spill
2752 temporaries. It does not distribute moves to their corresponding webs,
2753 though. */
2755 static void
2756 make_webs (df)
2757 struct df *df;
2759 /* First build all the webs itself. They are not related with
2760 others yet. */
2761 parts_to_webs (df);
2762 /* Now detect spill temporaries to initialize their usable_regs set. */
2763 detect_spill_temps ();
2764 detect_webs_set_in_cond_jump ();
2765 /* And finally relate them to each other, meaning to record all possible
2766 conflicts between webs (see the comment there). */
2767 conflicts_between_webs (df);
2768 detect_remat_webs ();
2769 determine_web_costs ();
2772 /* Distribute moves to the corresponding webs. */
2774 static void
2775 moves_to_webs (df)
2776 struct df *df;
2778 struct df_link *link;
2779 struct move_list *ml;
2781 /* Distribute all moves to their corresponding webs, making sure,
2782 each move is in a web maximally one time (happens on some strange
2783 insns). */
2784 for (ml = wl_moves; ml; ml = ml->next)
2786 struct move *m = ml->move;
2787 struct web *web;
2788 struct move_list *newml;
2789 if (!m)
2790 continue;
2791 m->type = WORKLIST;
2792 m->dlink = NULL;
2793 /* Multiple defs/uses can happen in moves involving hard-regs in
2794 a wider mode. For those df.* creates use/def references for each
2795 real hard-reg involved. For coalescing we are interested in
2796 the smallest numbered hard-reg. */
2797 for (link = DF_INSN_DEFS (df, m->insn); link; link = link->next)
2798 if (link->ref)
2800 web = def2web[DF_REF_ID (link->ref)];
2801 web = find_web_for_subweb (web);
2802 if (!m->target_web || web->regno < m->target_web->regno)
2803 m->target_web = web;
2805 for (link = DF_INSN_USES (df, m->insn); link; link = link->next)
2806 if (link->ref)
2808 web = use2web[DF_REF_ID (link->ref)];
2809 web = find_web_for_subweb (web);
2810 if (!m->source_web || web->regno < m->source_web->regno)
2811 m->source_web = web;
2813 if (m->source_web && m->target_web
2814 /* If the usable_regs don't intersect we can't coalesce the two
2815 webs anyway, as this is no simple copy insn (it might even
2816 need an intermediate stack temp to execute this "copy" insn). */
2817 && hard_regs_intersect_p (&m->source_web->usable_regs,
2818 &m->target_web->usable_regs))
2820 if (!flag_ra_optimistic_coalescing)
2822 struct move_list *test = m->source_web->moves;
2823 for (; test && test->move != m; test = test->next);
2824 if (! test)
2826 newml = (struct move_list*)
2827 ra_alloc (sizeof (struct move_list));
2828 newml->move = m;
2829 newml->next = m->source_web->moves;
2830 m->source_web->moves = newml;
2832 test = m->target_web->moves;
2833 for (; test && test->move != m; test = test->next);
2834 if (! test)
2836 newml = (struct move_list*)
2837 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.
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_LETTER (c)];
2982 /* Now make conflicts between this web, and all hardregs, which
2983 are not allowed by the constraints. */
2984 if (nothing_allowed)
2986 /* If we had no real constraints nothing was explicitely
2987 allowed, so we allow the whole class (i.e. we make no
2988 additional conflicts). */
2989 CLEAR_HARD_REG_SET (conflict);
2991 else
2993 COPY_HARD_REG_SET (conflict, usable_regs
2994 [reg_preferred_class (web->regno)]);
2995 IOR_HARD_REG_SET (conflict, usable_regs
2996 [reg_alternate_class (web->regno)]);
2997 AND_COMPL_HARD_REG_SET (conflict, allowed);
2998 /* We can't yet establish these conflicts. Reload must go first
2999 (or better said, we must implement some functionality of reload).
3000 E.g. if some operands must match, and they need the same color
3001 we don't see yet, that they do not conflict (because they match).
3002 For us it looks like two normal references with different DEFs,
3003 so they conflict, and as they both need the same color, the
3004 graph becomes uncolorable. */
3005 #if 0
3006 for (c = 0; c < FIRST_PSEUDO_REGISTER; c++)
3007 if (TEST_HARD_REG_BIT (conflict, c))
3008 record_conflict (web, hardreg2web[c]);
3009 #endif
3011 if (rtl_dump_file)
3013 int c;
3014 ra_debug_msg (DUMP_ASM, " ASM constrain Web %d conflicts with:", web->id);
3015 for (c = 0; c < FIRST_PSEUDO_REGISTER; c++)
3016 if (TEST_HARD_REG_BIT (conflict, c))
3017 ra_debug_msg (DUMP_ASM, " %d", c);
3018 ra_debug_msg (DUMP_ASM, "\n");
3023 /* The real toplevel function in this file.
3024 Build (or rebuilds) the complete interference graph with webs
3025 and conflicts. */
3027 void
3028 build_i_graph (df)
3029 struct df *df;
3031 rtx insn;
3033 init_web_parts (df);
3035 sbitmap_zero (move_handled);
3036 wl_moves = NULL;
3038 build_web_parts_and_conflicts (df);
3040 /* For read-modify-write instructions we may have created two webs.
3041 Reconnect them here. (s.a.) */
3042 connect_rmw_web_parts (df);
3044 /* The webs are conceptually complete now, but still scattered around as
3045 connected web parts. Collect all information and build the webs
3046 including all conflicts between webs (instead web parts). */
3047 make_webs (df);
3048 moves_to_webs (df);
3050 /* Look for additional constraints given by asms. */
3051 for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
3052 handle_asm_insn (df, insn);
3055 /* Allocates or reallocates most memory for the interference graph and
3056 assiciated structures. If it reallocates memory (meaning, this is not
3057 the first pass), this also changes some structures to reflect the
3058 additional entries in various array, and the higher number of
3059 defs and uses. */
3061 void
3062 ra_build_realloc (df)
3063 struct df *df;
3065 struct web_part *last_web_parts = web_parts;
3066 struct web **last_def2web = def2web;
3067 struct web **last_use2web = use2web;
3068 sbitmap last_live_over_abnormal = live_over_abnormal;
3069 unsigned int i;
3070 struct dlist *d;
3071 move_handled = sbitmap_alloc (get_max_uid () );
3072 web_parts = (struct web_part *) xcalloc (df->def_id + df->use_id,
3073 sizeof web_parts[0]);
3074 def2web = (struct web **) xcalloc (df->def_id + df->use_id,
3075 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 = (struct copy_p_cache *)
3166 xcalloc (get_max_uid (), sizeof (copy_cache[0]));
3167 init_bb_info ();
3169 else
3171 copy_cache = (struct copy_p_cache *)
3172 xrealloc (copy_cache, get_max_uid () * sizeof (copy_cache[0]));
3173 memset (&copy_cache[last_max_uid], 0,
3174 (get_max_uid () - last_max_uid) * sizeof (copy_cache[0]));
3178 /* Free up/clear some memory, only needed for one pass. */
3180 void
3181 ra_build_free ()
3183 struct dlist *d;
3184 unsigned int i;
3186 /* Clear the moves associated with a web (we also need to look into
3187 subwebs here). */
3188 for (i = 0; i < num_webs; i++)
3190 struct web *web = ID2WEB (i);
3191 if (!web)
3192 abort ();
3193 if (i >= num_webs - num_subwebs
3194 && (web->conflict_list || web->orig_conflict_list))
3195 abort ();
3196 web->moves = NULL;
3198 /* All webs in the free list have no defs or uses anymore. */
3199 for (d = WEBS(FREE); d; d = d->next)
3201 struct web *web = DLIST_WEB (d);
3202 if (web->defs)
3203 free (web->defs);
3204 web->defs = NULL;
3205 if (web->uses)
3206 free (web->uses);
3207 web->uses = NULL;
3208 /* We can't free the subwebs here, as they are referenced from
3209 def2web[], and possibly needed in the next ra_build_realloc().
3210 We free them there (or in free_all_mem()). */
3213 /* Free all conflict bitmaps from web parts. Note that we clear
3214 _all_ these conflicts, and don't rebuild them next time for uses
3215 which aren't rechecked. This mean, that those conflict bitmaps
3216 only contain the incremental information. The cumulative one
3217 is still contained in the edges of the I-graph, i.e. in
3218 conflict_list (or orig_conflict_list) of the webs. */
3219 for (i = 0; i < df->def_id + df->use_id; i++)
3221 struct tagged_conflict *cl;
3222 for (cl = web_parts[i].sub_conflicts; cl; cl = cl->next)
3224 if (cl->conflicts)
3225 BITMAP_XFREE (cl->conflicts);
3227 web_parts[i].sub_conflicts = NULL;
3230 wl_moves = NULL;
3232 free (id2web);
3233 free (move_handled);
3234 sbitmap_free (sup_igraph);
3235 sbitmap_free (igraph);
3238 /* Free all memory for the interference graph structures. */
3240 void
3241 ra_build_free_all (df)
3242 struct df *df;
3244 unsigned int i;
3246 free_bb_info ();
3247 free (copy_cache);
3248 copy_cache = NULL;
3249 for (i = 0; i < df->def_id + df->use_id; i++)
3251 struct tagged_conflict *cl;
3252 for (cl = web_parts[i].sub_conflicts; cl; cl = cl->next)
3254 if (cl->conflicts)
3255 BITMAP_XFREE (cl->conflicts);
3257 web_parts[i].sub_conflicts = NULL;
3259 sbitmap_free (live_over_abnormal);
3260 free (web_parts);
3261 web_parts = NULL;
3262 if (last_check_uses)
3263 sbitmap_free (last_check_uses);
3264 last_check_uses = NULL;
3265 free (def2web);
3266 use2web = NULL;
3267 def2web = NULL;
3270 #include "gt-ra-build.h"
3273 vim:cinoptions={.5s,g0,p5,t0,(0,^-0.5s,n-0.5s:tw=78:cindent:sw=4: