gcc/:
[official-gcc.git] / gcc / lower-subreg.c
blob7227624583be0633545695d9fc344ef7402e2a67
1 /* Decompose multiword subregs.
2 Copyright (C) 2007 Free Software Foundation, Inc.
3 Contributed by Richard Henderson <rth@redhat.com>
4 Ian Lance Taylor <iant@google.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "machmode.h"
27 #include "tm.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "timevar.h"
31 #include "flags.h"
32 #include "insn-config.h"
33 #include "obstack.h"
34 #include "basic-block.h"
35 #include "recog.h"
36 #include "bitmap.h"
37 #include "expr.h"
38 #include "regs.h"
39 #include "tree-pass.h"
41 #ifdef STACK_GROWS_DOWNWARD
42 # undef STACK_GROWS_DOWNWARD
43 # define STACK_GROWS_DOWNWARD 1
44 #else
45 # define STACK_GROWS_DOWNWARD 0
46 #endif
48 DEF_VEC_P (bitmap);
49 DEF_VEC_ALLOC_P (bitmap,heap);
51 /* Decompose multi-word pseudo-registers into individual
52 pseudo-registers when possible. This is possible when all the uses
53 of a multi-word register are via SUBREG, or are copies of the
54 register to another location. Breaking apart the register permits
55 more CSE and permits better register allocation. */
57 /* Bit N in this bitmap is set if regno N is used in a context in
58 which we can decompose it. */
59 static bitmap decomposable_context;
61 /* Bit N in this bitmap is set if regno N is used in a context in
62 which it can not be decomposed. */
63 static bitmap non_decomposable_context;
65 /* Bit N in the bitmap in element M of this array is set if there is a
66 copy from reg M to reg N. */
67 static VEC(bitmap,heap) *reg_copy_graph;
69 /* If INSN is a single set between two objects, return the single set.
70 Such an insn can always be decomposed. INSN should have been
71 passed to recog and extract_insn before this is called. */
73 static rtx
74 simple_move (rtx insn)
76 rtx x;
77 rtx set;
78 enum machine_mode mode;
80 if (recog_data.n_operands != 2)
81 return NULL_RTX;
83 set = single_set (insn);
84 if (!set)
85 return NULL_RTX;
87 x = SET_DEST (set);
88 if (x != recog_data.operand[0] && x != recog_data.operand[1])
89 return NULL_RTX;
90 if (GET_CODE (x) == SUBREG)
91 x = SUBREG_REG (x);
92 if (!OBJECT_P (x))
93 return NULL_RTX;
94 if (MEM_P (x)
95 && (MEM_VOLATILE_P (x)
96 || mode_dependent_address_p (XEXP (x, 0))))
97 return NULL_RTX;
99 x = SET_SRC (set);
100 if (x != recog_data.operand[0] && x != recog_data.operand[1])
101 return NULL_RTX;
102 if (GET_CODE (x) == SUBREG)
103 x = SUBREG_REG (x);
104 if (!OBJECT_P (x) && GET_CODE (x) != ASM_OPERANDS)
105 return NULL_RTX;
106 if (MEM_P (x)
107 && (MEM_VOLATILE_P (x)
108 || mode_dependent_address_p (XEXP (x, 0))))
109 return NULL_RTX;
111 /* We try to decompose in integer modes, to avoid generating
112 inefficient code copying between integer and floating point
113 registers. That means that we can't decompose if this is a
114 non-integer mode for which there is no integer mode of the same
115 size. */
116 mode = GET_MODE (SET_SRC (set));
117 if (!SCALAR_INT_MODE_P (mode)
118 && (mode_for_size (GET_MODE_SIZE (mode) * BITS_PER_UNIT, MODE_INT, 0)
119 == BLKmode))
120 return NULL_RTX;
122 return set;
125 /* If SET is a copy from one multi-word pseudo-register to another,
126 record that in reg_copy_graph. Return whether it is such a
127 copy. */
129 static bool
130 find_pseudo_copy (rtx set)
132 rtx dest = SET_DEST (set);
133 rtx src = SET_SRC (set);
134 unsigned int rd, rs;
135 bitmap b;
137 if (!REG_P (dest) || !REG_P (src))
138 return false;
140 rd = REGNO (dest);
141 rs = REGNO (src);
142 if (HARD_REGISTER_NUM_P (rd) || HARD_REGISTER_NUM_P (rs))
143 return false;
145 if (GET_MODE_SIZE (GET_MODE (dest)) <= UNITS_PER_WORD)
146 return false;
148 b = VEC_index (bitmap, reg_copy_graph, rs);
149 if (b == NULL)
151 b = BITMAP_ALLOC (NULL);
152 VEC_replace (bitmap, reg_copy_graph, rs, b);
155 bitmap_set_bit (b, rd);
157 return true;
160 /* Look through the registers in DECOMPOSABLE_CONTEXT. For each case
161 where they are copied to another register, add the register to
162 which they are copied to DECOMPOSABLE_CONTEXT. Use
163 NON_DECOMPOSABLE_CONTEXT to limit this--we don't bother to track
164 copies of registers which are in NON_DECOMPOSABLE_CONTEXT. */
166 static void
167 propagate_pseudo_copies (void)
169 bitmap queue, propagate;
171 queue = BITMAP_ALLOC (NULL);
172 propagate = BITMAP_ALLOC (NULL);
174 bitmap_copy (queue, decomposable_context);
177 bitmap_iterator iter;
178 unsigned int i;
180 bitmap_clear (propagate);
182 EXECUTE_IF_SET_IN_BITMAP (queue, 0, i, iter)
184 bitmap b = VEC_index (bitmap, reg_copy_graph, i);
185 if (b)
186 bitmap_ior_and_compl_into (propagate, b, non_decomposable_context);
189 bitmap_and_compl (queue, propagate, decomposable_context);
190 bitmap_ior_into (decomposable_context, propagate);
192 while (!bitmap_empty_p (queue));
194 BITMAP_FREE (queue);
195 BITMAP_FREE (propagate);
198 /* A pointer to one of these values is passed to
199 find_decomposable_subregs via for_each_rtx. */
201 enum classify_move_insn
203 /* Not a simple move from one location to another. */
204 NOT_SIMPLE_MOVE,
205 /* A simple move from one pseudo-register to another with no
206 REG_RETVAL note. */
207 SIMPLE_PSEUDO_REG_MOVE,
208 /* A simple move involving a non-pseudo-register, or from one
209 pseudo-register to another with a REG_RETVAL note. */
210 SIMPLE_MOVE
213 /* This is called via for_each_rtx. If we find a SUBREG which we
214 could use to decompose a pseudo-register, set a bit in
215 DECOMPOSABLE_CONTEXT. If we find an unadorned register which is
216 not a simple pseudo-register copy, DATA will point at the type of
217 move, and we set a bit in DECOMPOSABLE_CONTEXT or
218 NON_DECOMPOSABLE_CONTEXT as appropriate. */
220 static int
221 find_decomposable_subregs (rtx *px, void *data)
223 enum classify_move_insn *pcmi = (enum classify_move_insn *) data;
224 rtx x = *px;
226 if (x == NULL_RTX)
227 return 0;
229 if (GET_CODE (x) == SUBREG)
231 rtx inner = SUBREG_REG (x);
232 unsigned int regno, outer_size, inner_size, outer_words, inner_words;
234 if (!REG_P (inner))
235 return 0;
237 regno = REGNO (inner);
238 if (HARD_REGISTER_NUM_P (regno))
239 return -1;
241 outer_size = GET_MODE_SIZE (GET_MODE (x));
242 inner_size = GET_MODE_SIZE (GET_MODE (inner));
243 outer_words = (outer_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
244 inner_words = (inner_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
246 /* We only try to decompose single word subregs of multi-word
247 registers. When we find one, we return -1 to avoid iterating
248 over the inner register.
250 ??? This doesn't allow, e.g., DImode subregs of TImode values
251 on 32-bit targets. We would need to record the way the
252 pseudo-register was used, and only decompose if all the uses
253 were the same number and size of pieces. Hopefully this
254 doesn't happen much. */
256 if (outer_words == 1 && inner_words > 1)
258 bitmap_set_bit (decomposable_context, regno);
259 return -1;
262 else if (GET_CODE (x) == REG)
264 unsigned int regno;
266 /* We will see an outer SUBREG before we see the inner REG, so
267 when we see a plain REG here it means a direct reference to
268 the register.
270 If this is not a simple copy from one location to another,
271 then we can not decompose this register. If this is a simple
272 copy from one pseudo-register to another, with no REG_RETVAL
273 note, and the mode is right, then we mark the register as
274 decomposable. Otherwise we don't say anything about this
275 register--it could be decomposed, but whether that would be
276 profitable depends upon how it is used elsewhere.
278 We only set bits in the bitmap for multi-word
279 pseudo-registers, since those are the only ones we care about
280 and it keeps the size of the bitmaps down. */
282 regno = REGNO (x);
283 if (!HARD_REGISTER_NUM_P (regno)
284 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
286 switch (*pcmi)
288 case NOT_SIMPLE_MOVE:
289 bitmap_set_bit (non_decomposable_context, regno);
290 break;
291 case SIMPLE_PSEUDO_REG_MOVE:
292 if (MODES_TIEABLE_P (GET_MODE (x), word_mode))
293 bitmap_set_bit (decomposable_context, regno);
294 break;
295 case SIMPLE_MOVE:
296 break;
297 default:
298 gcc_unreachable ();
303 return 0;
306 /* Decompose REGNO into word-sized components. We smash the REG node
307 in place. This ensures that (1) something goes wrong quickly if we
308 fail to make some replacement, and (2) the debug information inside
309 the symbol table is automatically kept up to date. */
311 static void
312 decompose_register (unsigned int regno)
314 rtx reg;
315 unsigned int words, i;
316 rtvec v;
318 reg = regno_reg_rtx[regno];
320 regno_reg_rtx[regno] = NULL_RTX;
321 clear_reg_info_regno (regno);
323 words = GET_MODE_SIZE (GET_MODE (reg));
324 words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
326 v = rtvec_alloc (words);
327 for (i = 0; i < words; ++i)
328 RTVEC_ELT (v, i) = gen_reg_rtx_offset (reg, word_mode, i * UNITS_PER_WORD);
330 PUT_CODE (reg, CONCATN);
331 XVEC (reg, 0) = v;
333 if (dump_file)
335 fprintf (dump_file, "; Splitting reg %u ->", regno);
336 for (i = 0; i < words; ++i)
337 fprintf (dump_file, " %u", REGNO (XVECEXP (reg, 0, i)));
338 fputc ('\n', dump_file);
342 /* Get a SUBREG of a CONCATN. */
344 static rtx
345 simplify_subreg_concatn (enum machine_mode outermode, rtx op,
346 unsigned int byte)
348 unsigned int inner_size;
349 enum machine_mode innermode;
350 rtx part;
351 unsigned int final_offset;
353 gcc_assert (GET_CODE (op) == CONCATN);
354 gcc_assert (byte % GET_MODE_SIZE (outermode) == 0);
356 innermode = GET_MODE (op);
357 gcc_assert (byte < GET_MODE_SIZE (innermode));
358 gcc_assert (GET_MODE_SIZE (outermode) <= GET_MODE_SIZE (innermode));
360 inner_size = GET_MODE_SIZE (innermode) / XVECLEN (op, 0);
361 part = XVECEXP (op, 0, byte / inner_size);
362 final_offset = byte % inner_size;
363 if (final_offset + GET_MODE_SIZE (outermode) > inner_size)
364 return NULL_RTX;
366 return simplify_gen_subreg (outermode, part, GET_MODE (part), final_offset);
369 /* Wrapper around simplify_gen_subreg which handles CONCATN. */
371 static rtx
372 simplify_gen_subreg_concatn (enum machine_mode outermode, rtx op,
373 enum machine_mode innermode, unsigned int byte)
375 /* We have to handle generating a SUBREG of a SUBREG of a CONCATN.
376 If OP is a SUBREG of a CONCATN, then it must be a simple mode
377 change with the same size and offset 0, or it must extract a
378 part. We shouldn't see anything else here. */
379 if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == CONCATN)
381 rtx op2;
383 if ((GET_MODE_SIZE (GET_MODE (op))
384 == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))))
385 && SUBREG_BYTE (op) == 0)
386 return simplify_gen_subreg_concatn (outermode, SUBREG_REG (op),
387 GET_MODE (SUBREG_REG (op)), byte);
389 op2 = simplify_subreg_concatn (GET_MODE (op), SUBREG_REG (op),
390 SUBREG_BYTE (op));
391 if (op2 == NULL_RTX)
393 /* We don't handle paradoxical subregs here. */
394 gcc_assert (GET_MODE_SIZE (outermode)
395 <= GET_MODE_SIZE (GET_MODE (op)));
396 gcc_assert (GET_MODE_SIZE (GET_MODE (op))
397 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))));
398 op2 = simplify_subreg_concatn (outermode, SUBREG_REG (op),
399 byte + SUBREG_BYTE (op));
400 gcc_assert (op2 != NULL_RTX);
401 return op2;
404 op = op2;
405 gcc_assert (op != NULL_RTX);
406 gcc_assert (innermode == GET_MODE (op));
408 if (GET_CODE (op) == CONCATN)
409 return simplify_subreg_concatn (outermode, op, byte);
410 return simplify_gen_subreg (outermode, op, innermode, byte);
413 /* Return whether we should resolve X into the registers into which it
414 was decomposed. */
416 static bool
417 resolve_reg_p (rtx x)
419 return GET_CODE (x) == CONCATN;
422 /* Return whether X is a SUBREG of a register which we need to
423 resolve. */
425 static bool
426 resolve_subreg_p (rtx x)
428 if (GET_CODE (x) != SUBREG)
429 return false;
430 return resolve_reg_p (SUBREG_REG (x));
433 /* This is called via for_each_rtx. Look for SUBREGs which need to be
434 decomposed. */
436 static int
437 resolve_subreg_use (rtx *px, void *data)
439 rtx insn = (rtx) data;
440 rtx x = *px;
442 if (x == NULL_RTX)
443 return 0;
445 if (resolve_subreg_p (x))
447 x = simplify_subreg_concatn (GET_MODE (x), SUBREG_REG (x),
448 SUBREG_BYTE (x));
450 /* It is possible for a note to contain a reference which we can
451 decompose. In this case, return 1 to the caller to indicate
452 that the note must be removed. */
453 if (!x)
455 gcc_assert(!insn);
456 return 1;
459 validate_change (insn, px, x, 1);
460 return -1;
463 if (resolve_reg_p (x))
465 /* Return 1 to the caller to indicate that we found a direct
466 reference to a register which is being decomposed. This can
467 happen inside notes. */
468 gcc_assert (!insn);
469 return 1;
472 return 0;
475 /* If there is a REG_LIBCALL note on OLD_START, move it to NEW_START,
476 and link the corresponding REG_RETVAL note to NEW_START. */
478 static void
479 move_libcall_note (rtx old_start, rtx new_start)
481 rtx note0, note1, end;
483 note0 = find_reg_note (old_start, REG_LIBCALL, NULL);
484 if (note0 == NULL_RTX)
485 return;
487 remove_note (old_start, note0);
488 end = XEXP (note0, 0);
489 note1 = find_reg_note (end, REG_RETVAL, NULL);
491 XEXP (note0, 1) = REG_NOTES (new_start);
492 REG_NOTES (new_start) = note0;
493 XEXP (note1, 0) = new_start;
496 /* Remove any REG_RETVAL note, the corresponding REG_LIBCALL note, and
497 any markers for a no-conflict block. We have decomposed the
498 registers so the non-conflict is now obvious. */
500 static void
501 remove_retval_note (rtx insn1)
503 rtx note0, insn0, note1, insn;
505 note1 = find_reg_note (insn1, REG_RETVAL, NULL);
506 if (note1 == NULL_RTX)
507 return;
509 insn0 = XEXP (note1, 0);
510 note0 = find_reg_note (insn0, REG_LIBCALL, NULL);
512 remove_note (insn0, note0);
513 remove_note (insn1, note1);
515 for (insn = insn0; insn != insn1; insn = NEXT_INSN (insn))
517 while (1)
519 rtx note;
521 note = find_reg_note (insn, REG_NO_CONFLICT, NULL);
522 if (note == NULL_RTX)
523 break;
524 remove_note (insn, note);
529 /* Resolve any decomposed registers which appear in register notes on
530 INSN. */
532 static void
533 resolve_reg_notes (rtx insn)
535 rtx *pnote, note;
537 note = find_reg_equal_equiv_note (insn);
538 if (note)
540 if (for_each_rtx (&XEXP (note, 0), resolve_subreg_use, NULL))
542 remove_note (insn, note);
543 remove_retval_note (insn);
547 pnote = &REG_NOTES (insn);
548 while (*pnote != NULL_RTX)
550 bool delete = false;
552 note = *pnote;
553 switch (REG_NOTE_KIND (note))
555 case REG_NO_CONFLICT:
556 if (resolve_reg_p (XEXP (note, 0)))
557 delete = true;
558 break;
560 default:
561 break;
564 if (delete)
565 *pnote = XEXP (note, 1);
566 else
567 pnote = &XEXP (note, 1);
571 /* Return whether X can not be decomposed into subwords. */
573 static bool
574 cannot_decompose_p (rtx x)
576 if (REG_P (x))
578 unsigned int regno = REGNO (x);
580 if (HARD_REGISTER_NUM_P (regno))
581 return !validate_subreg (word_mode, GET_MODE (x), x, UNITS_PER_WORD);
582 else
583 return bitmap_bit_p (non_decomposable_context, regno);
586 return false;
589 /* Decompose the registers used in a simple move SET within INSN. If
590 we don't change anything, return INSN, otherwise return the start
591 of the sequence of moves. */
593 static rtx
594 resolve_simple_move (rtx set, rtx insn)
596 rtx src, dest, real_dest, insns;
597 enum machine_mode orig_mode, dest_mode;
598 unsigned int words;
599 bool pushing;
601 src = SET_SRC (set);
602 dest = SET_DEST (set);
603 orig_mode = GET_MODE (dest);
605 words = (GET_MODE_SIZE (orig_mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
606 if (words <= 1)
607 return insn;
609 start_sequence ();
611 /* We have to handle copying from a SUBREG of a decomposed reg where
612 the SUBREG is larger than word size. Rather than assume that we
613 can take a word_mode SUBREG of the destination, we copy to a new
614 register and then copy that to the destination. */
616 real_dest = NULL_RTX;
618 if (GET_CODE (src) == SUBREG
619 && resolve_reg_p (SUBREG_REG (src))
620 && (SUBREG_BYTE (src) != 0
621 || (GET_MODE_SIZE (orig_mode)
622 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))))
624 real_dest = dest;
625 dest = gen_reg_rtx (orig_mode);
626 if (REG_P (real_dest))
627 REG_ATTRS (dest) = REG_ATTRS (real_dest);
630 /* Similarly if we are copying to a SUBREG of a decomposed reg where
631 the SUBREG is larger than word size. */
633 if (GET_CODE (dest) == SUBREG
634 && resolve_reg_p (SUBREG_REG (dest))
635 && (SUBREG_BYTE (dest) != 0
636 || (GET_MODE_SIZE (orig_mode)
637 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))))
639 rtx reg, minsn, smove;
641 reg = gen_reg_rtx (orig_mode);
642 minsn = emit_move_insn (reg, src);
643 smove = single_set (minsn);
644 gcc_assert (smove != NULL_RTX);
645 resolve_simple_move (smove, minsn);
646 src = reg;
649 /* If we didn't have any big SUBREGS of decomposed registers, and
650 neither side of the move is a register we are decomposing, then
651 we don't have to do anything here. */
653 if (src == SET_SRC (set)
654 && dest == SET_DEST (set)
655 && !resolve_reg_p (src)
656 && !resolve_subreg_p (src)
657 && !resolve_reg_p (dest)
658 && !resolve_subreg_p (dest))
660 end_sequence ();
661 return insn;
664 /* If SRC is a register which we can't decompose, or has side
665 effects, we need to move via a temporary register. */
667 if (cannot_decompose_p (src)
668 || side_effects_p (src)
669 || GET_CODE (src) == ASM_OPERANDS)
671 rtx reg;
673 reg = gen_reg_rtx (orig_mode);
674 emit_move_insn (reg, src);
675 src = reg;
678 /* If DEST is a register which we can't decompose, or has side
679 effects, we need to first move to a temporary register. We
680 handle the common case of pushing an operand directly. We also
681 go through a temporary register if it holds a floating point
682 value. This gives us better code on systems which can't move
683 data easily between integer and floating point registers. */
685 dest_mode = orig_mode;
686 pushing = push_operand (dest, dest_mode);
687 if (cannot_decompose_p (dest)
688 || (side_effects_p (dest) && !pushing)
689 || (!SCALAR_INT_MODE_P (dest_mode)
690 && !resolve_reg_p (dest)
691 && !resolve_subreg_p (dest)))
693 if (real_dest == NULL_RTX)
694 real_dest = dest;
695 if (!SCALAR_INT_MODE_P (dest_mode))
697 dest_mode = mode_for_size (GET_MODE_SIZE (dest_mode) * BITS_PER_UNIT,
698 MODE_INT, 0);
699 gcc_assert (dest_mode != BLKmode);
701 dest = gen_reg_rtx (dest_mode);
702 if (REG_P (real_dest))
703 REG_ATTRS (dest) = REG_ATTRS (real_dest);
706 if (pushing)
708 unsigned int i, j, jinc;
710 gcc_assert (GET_MODE_SIZE (orig_mode) % UNITS_PER_WORD == 0);
711 gcc_assert (GET_CODE (XEXP (dest, 0)) != PRE_MODIFY);
712 gcc_assert (GET_CODE (XEXP (dest, 0)) != POST_MODIFY);
714 if (WORDS_BIG_ENDIAN == STACK_GROWS_DOWNWARD)
716 j = 0;
717 jinc = 1;
719 else
721 j = words - 1;
722 jinc = -1;
725 for (i = 0; i < words; ++i, j += jinc)
727 rtx temp;
729 temp = copy_rtx (XEXP (dest, 0));
730 temp = adjust_automodify_address_nv (dest, word_mode, temp,
731 j * UNITS_PER_WORD);
732 emit_move_insn (temp,
733 simplify_gen_subreg_concatn (word_mode, src,
734 orig_mode,
735 j * UNITS_PER_WORD));
738 else
740 unsigned int i;
742 if (REG_P (dest) && !HARD_REGISTER_NUM_P (REGNO (dest)))
743 emit_insn (gen_rtx_CLOBBER (VOIDmode, dest));
745 for (i = 0; i < words; ++i)
746 emit_move_insn (simplify_gen_subreg_concatn (word_mode, dest,
747 dest_mode,
748 i * UNITS_PER_WORD),
749 simplify_gen_subreg_concatn (word_mode, src,
750 orig_mode,
751 i * UNITS_PER_WORD));
754 if (real_dest != NULL_RTX)
756 rtx mdest, minsn, smove;
758 if (dest_mode == orig_mode)
759 mdest = dest;
760 else
761 mdest = simplify_gen_subreg (orig_mode, dest, GET_MODE (dest), 0);
762 minsn = emit_move_insn (real_dest, mdest);
764 smove = single_set (minsn);
765 gcc_assert (smove != NULL_RTX);
767 resolve_simple_move (smove, minsn);
770 insns = get_insns ();
771 end_sequence ();
773 emit_insn_before (insns, insn);
775 move_libcall_note (insn, insns);
776 remove_retval_note (insn);
777 delete_insn (insn);
779 return insns;
782 /* Change a CLOBBER of a decomposed register into a CLOBBER of the
783 component registers. Return whether we changed something. */
785 static bool
786 resolve_clobber (rtx pat, rtx insn)
788 rtx reg;
789 enum machine_mode orig_mode;
790 unsigned int words, i;
792 reg = XEXP (pat, 0);
793 if (!resolve_reg_p (reg))
794 return false;
796 orig_mode = GET_MODE (reg);
797 words = GET_MODE_SIZE (orig_mode);
798 words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
800 XEXP (pat, 0) = simplify_subreg_concatn (word_mode, reg, 0);
801 for (i = words - 1; i > 0; --i)
803 rtx x;
805 x = simplify_subreg_concatn (word_mode, reg, i * UNITS_PER_WORD);
806 x = gen_rtx_CLOBBER (VOIDmode, x);
807 emit_insn_after (x, insn);
810 return true;
813 /* A USE of a decomposed register is no longer meaningful. Return
814 whether we changed something. */
816 static bool
817 resolve_use (rtx pat, rtx insn)
819 if (resolve_reg_p (XEXP (pat, 0)) || resolve_subreg_p (XEXP (pat, 0)))
821 delete_insn (insn);
822 return true;
824 return false;
827 /* Look for registers which are always accessed via word-sized SUBREGs
828 or via copies. Decompose these registers into several word-sized
829 pseudo-registers. */
831 static void
832 decompose_multiword_subregs (bool update_life)
834 unsigned int max;
835 basic_block bb;
837 max = max_reg_num ();
839 /* First see if there are any multi-word pseudo-registers. If there
840 aren't, there is nothing we can do. This should speed up this
841 pass in the normal case, since it should be faster than scanning
842 all the insns. */
844 unsigned int i;
846 for (i = FIRST_PSEUDO_REGISTER; i < max; ++i)
848 if (regno_reg_rtx[i] != NULL
849 && GET_MODE_SIZE (GET_MODE (regno_reg_rtx[i])) > UNITS_PER_WORD)
850 break;
852 if (i == max)
853 return;
856 /* FIXME: When the dataflow branch is merged, we can change this
857 code to look for each multi-word pseudo-register and to find each
858 insn which sets or uses that register. That should be faster
859 than scanning all the insns. */
861 decomposable_context = BITMAP_ALLOC (NULL);
862 non_decomposable_context = BITMAP_ALLOC (NULL);
864 reg_copy_graph = VEC_alloc (bitmap, heap, max);
865 VEC_safe_grow (bitmap, heap, reg_copy_graph, max);
866 memset (VEC_address (bitmap, reg_copy_graph), 0, sizeof (bitmap) * max);
868 FOR_EACH_BB (bb)
870 rtx insn;
872 FOR_BB_INSNS (bb, insn)
874 rtx set;
875 enum classify_move_insn cmi;
876 int i, n;
878 if (!INSN_P (insn)
879 || GET_CODE (PATTERN (insn)) == CLOBBER
880 || GET_CODE (PATTERN (insn)) == USE)
881 continue;
883 recog_memoized (insn);
884 extract_insn (insn);
886 set = simple_move (insn);
888 if (!set)
889 cmi = NOT_SIMPLE_MOVE;
890 else
892 bool retval;
894 retval = find_reg_note (insn, REG_RETVAL, NULL_RTX) != NULL_RTX;
896 if (find_pseudo_copy (set) && !retval)
897 cmi = SIMPLE_PSEUDO_REG_MOVE;
898 else if (retval
899 && REG_P (SET_SRC (set))
900 && HARD_REGISTER_P (SET_SRC (set)))
902 rtx note;
904 /* We don't want to decompose an assignment which
905 copies the value returned by a libcall to a
906 pseudo-register. Doing that will lose the RETVAL
907 note with no real gain. */
908 cmi = NOT_SIMPLE_MOVE;
910 /* If we have a RETVAL note, there should be an
911 EQUAL note. We don't want to decompose any
912 registers which that EQUAL note refers to
913 directly. If we do, we will no longer know the
914 value of the libcall. */
915 note = find_reg_equal_equiv_note (insn);
916 if (note != NULL_RTX)
917 for_each_rtx (&XEXP (note, 0), find_decomposable_subregs,
918 &cmi);
920 else
921 cmi = SIMPLE_MOVE;
924 n = recog_data.n_operands;
925 for (i = 0; i < n; ++i)
927 for_each_rtx (&recog_data.operand[i],
928 find_decomposable_subregs,
929 &cmi);
931 /* We handle ASM_OPERANDS as a special case to support
932 things like x86 rdtsc which returns a DImode value.
933 We can decompose the output, which will certainly be
934 operand 0, but not the inputs. */
936 if (cmi == SIMPLE_MOVE
937 && GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
939 gcc_assert (i == 0);
940 cmi = NOT_SIMPLE_MOVE;
946 bitmap_and_compl_into (decomposable_context, non_decomposable_context);
947 if (!bitmap_empty_p (decomposable_context))
949 int hold_no_new_pseudos = no_new_pseudos;
950 int max_regno = max_reg_num ();
951 sbitmap blocks;
952 bitmap_iterator iter;
953 unsigned int regno;
955 propagate_pseudo_copies ();
957 no_new_pseudos = 0;
958 blocks = sbitmap_alloc (last_basic_block);
959 sbitmap_zero (blocks);
961 EXECUTE_IF_SET_IN_BITMAP (decomposable_context, 0, regno, iter)
962 decompose_register (regno);
964 FOR_EACH_BB (bb)
966 rtx insn;
968 FOR_BB_INSNS (bb, insn)
970 rtx next, pat;
971 bool changed;
973 if (!INSN_P (insn))
974 continue;
976 next = NEXT_INSN (insn);
977 changed = false;
979 pat = PATTERN (insn);
980 if (GET_CODE (pat) == CLOBBER)
982 if (resolve_clobber (pat, insn))
983 changed = true;
985 else if (GET_CODE (pat) == USE)
987 if (resolve_use (pat, insn))
988 changed = true;
990 else
992 rtx set;
993 int i;
995 recog_memoized (insn);
996 extract_insn (insn);
998 set = simple_move (insn);
999 if (set)
1001 rtx orig_insn = insn;
1003 insn = resolve_simple_move (set, insn);
1004 if (insn != orig_insn)
1006 changed = true;
1008 recog_memoized (insn);
1009 extract_insn (insn);
1013 for (i = recog_data.n_operands - 1; i >= 0; --i)
1014 for_each_rtx (recog_data.operand_loc[i],
1015 resolve_subreg_use,
1016 insn);
1018 resolve_reg_notes (insn);
1020 if (num_validated_changes () > 0)
1022 for (i = recog_data.n_dups - 1; i >= 0; --i)
1024 rtx *pl = recog_data.dup_loc[i];
1025 int dup_num = recog_data.dup_num[i];
1026 rtx *px = recog_data.operand_loc[dup_num];
1028 validate_change (insn, pl, *px, 1);
1031 i = apply_change_group ();
1032 gcc_assert (i);
1034 changed = true;
1038 if (changed)
1040 SET_BIT (blocks, bb->index);
1041 reg_scan_update (insn, next, max_regno);
1046 no_new_pseudos = hold_no_new_pseudos;
1048 if (update_life)
1049 update_life_info (blocks, UPDATE_LIFE_GLOBAL_RM_NOTES,
1050 PROP_DEATH_NOTES);
1052 sbitmap_free (blocks);
1056 unsigned int i;
1057 bitmap b;
1059 for (i = 0; VEC_iterate (bitmap, reg_copy_graph, i, b); ++i)
1060 if (b)
1061 BITMAP_FREE (b);
1064 VEC_free (bitmap, heap, reg_copy_graph);
1066 BITMAP_FREE (decomposable_context);
1067 BITMAP_FREE (non_decomposable_context);
1070 /* Gate function for lower subreg pass. */
1072 static bool
1073 gate_handle_lower_subreg (void)
1075 return flag_split_wide_types != 0;
1078 /* Implement first lower subreg pass. */
1080 static unsigned int
1081 rest_of_handle_lower_subreg (void)
1083 decompose_multiword_subregs (false);
1084 return 0;
1087 /* Implement second lower subreg pass. */
1089 static unsigned int
1090 rest_of_handle_lower_subreg2 (void)
1092 decompose_multiword_subregs (true);
1093 return 0;
1096 struct tree_opt_pass pass_lower_subreg =
1098 "subreg", /* name */
1099 gate_handle_lower_subreg, /* gate */
1100 rest_of_handle_lower_subreg, /* execute */
1101 NULL, /* sub */
1102 NULL, /* next */
1103 0, /* static_pass_number */
1104 TV_LOWER_SUBREG, /* tv_id */
1105 0, /* properties_required */
1106 0, /* properties_provided */
1107 0, /* properties_destroyed */
1108 0, /* todo_flags_start */
1109 TODO_dump_func |
1110 TODO_ggc_collect, /* todo_flags_finish */
1111 'u' /* letter */
1114 struct tree_opt_pass pass_lower_subreg2 =
1116 "subreg2", /* name */
1117 gate_handle_lower_subreg, /* gate */
1118 rest_of_handle_lower_subreg2, /* execute */
1119 NULL, /* sub */
1120 NULL, /* next */
1121 0, /* static_pass_number */
1122 TV_LOWER_SUBREG, /* tv_id */
1123 0, /* properties_required */
1124 0, /* properties_provided */
1125 0, /* properties_destroyed */
1126 0, /* todo_flags_start */
1127 TODO_dump_func |
1128 TODO_ggc_collect, /* todo_flags_finish */
1129 'U' /* letter */