* config/alpha/alpha.c (alpha_add_builtins): New Helper function.
[official-gcc.git] / gcc / lower-subreg.c
blob62bfea0a100760a156f5fafa76d1163a3f797ab0
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 /* Return whether X is a simple object which we can take a word_mode
70 subreg of. */
72 static bool
73 simple_move_operand (rtx x)
75 if (GET_CODE (x) == SUBREG)
76 x = SUBREG_REG (x);
78 if (!OBJECT_P (x))
79 return false;
81 if (GET_CODE (x) == LABEL_REF
82 || GET_CODE (x) == SYMBOL_REF
83 || GET_CODE (x) == HIGH)
84 return false;
86 if (MEM_P (x)
87 && (MEM_VOLATILE_P (x)
88 || mode_dependent_address_p (XEXP (x, 0))))
89 return false;
91 return true;
94 /* If INSN is a single set between two objects, return the single set.
95 Such an insn can always be decomposed. INSN should have been
96 passed to recog and extract_insn before this is called. */
98 static rtx
99 simple_move (rtx insn)
101 rtx x;
102 rtx set;
103 enum machine_mode mode;
105 if (recog_data.n_operands != 2)
106 return NULL_RTX;
108 set = single_set (insn);
109 if (!set)
110 return NULL_RTX;
112 x = SET_DEST (set);
113 if (x != recog_data.operand[0] && x != recog_data.operand[1])
114 return NULL_RTX;
115 if (!simple_move_operand (x))
116 return NULL_RTX;
118 x = SET_SRC (set);
119 if (x != recog_data.operand[0] && x != recog_data.operand[1])
120 return NULL_RTX;
121 /* For the src we can handle ASM_OPERANDS, and it is beneficial for
122 things like x86 rdtsc which returns a DImode value. */
123 if (GET_CODE (x) != ASM_OPERANDS
124 && !simple_move_operand (x))
125 return NULL_RTX;
127 /* We try to decompose in integer modes, to avoid generating
128 inefficient code copying between integer and floating point
129 registers. That means that we can't decompose if this is a
130 non-integer mode for which there is no integer mode of the same
131 size. */
132 mode = GET_MODE (SET_SRC (set));
133 if (!SCALAR_INT_MODE_P (mode)
134 && (mode_for_size (GET_MODE_SIZE (mode) * BITS_PER_UNIT, MODE_INT, 0)
135 == BLKmode))
136 return NULL_RTX;
138 return set;
141 /* If SET is a copy from one multi-word pseudo-register to another,
142 record that in reg_copy_graph. Return whether it is such a
143 copy. */
145 static bool
146 find_pseudo_copy (rtx set)
148 rtx dest = SET_DEST (set);
149 rtx src = SET_SRC (set);
150 unsigned int rd, rs;
151 bitmap b;
153 if (!REG_P (dest) || !REG_P (src))
154 return false;
156 rd = REGNO (dest);
157 rs = REGNO (src);
158 if (HARD_REGISTER_NUM_P (rd) || HARD_REGISTER_NUM_P (rs))
159 return false;
161 if (GET_MODE_SIZE (GET_MODE (dest)) <= UNITS_PER_WORD)
162 return false;
164 b = VEC_index (bitmap, reg_copy_graph, rs);
165 if (b == NULL)
167 b = BITMAP_ALLOC (NULL);
168 VEC_replace (bitmap, reg_copy_graph, rs, b);
171 bitmap_set_bit (b, rd);
173 return true;
176 /* Look through the registers in DECOMPOSABLE_CONTEXT. For each case
177 where they are copied to another register, add the register to
178 which they are copied to DECOMPOSABLE_CONTEXT. Use
179 NON_DECOMPOSABLE_CONTEXT to limit this--we don't bother to track
180 copies of registers which are in NON_DECOMPOSABLE_CONTEXT. */
182 static void
183 propagate_pseudo_copies (void)
185 bitmap queue, propagate;
187 queue = BITMAP_ALLOC (NULL);
188 propagate = BITMAP_ALLOC (NULL);
190 bitmap_copy (queue, decomposable_context);
193 bitmap_iterator iter;
194 unsigned int i;
196 bitmap_clear (propagate);
198 EXECUTE_IF_SET_IN_BITMAP (queue, 0, i, iter)
200 bitmap b = VEC_index (bitmap, reg_copy_graph, i);
201 if (b)
202 bitmap_ior_and_compl_into (propagate, b, non_decomposable_context);
205 bitmap_and_compl (queue, propagate, decomposable_context);
206 bitmap_ior_into (decomposable_context, propagate);
208 while (!bitmap_empty_p (queue));
210 BITMAP_FREE (queue);
211 BITMAP_FREE (propagate);
214 /* A pointer to one of these values is passed to
215 find_decomposable_subregs via for_each_rtx. */
217 enum classify_move_insn
219 /* Not a simple move from one location to another. */
220 NOT_SIMPLE_MOVE,
221 /* A simple move from one pseudo-register to another with no
222 REG_RETVAL note. */
223 SIMPLE_PSEUDO_REG_MOVE,
224 /* A simple move involving a non-pseudo-register, or from one
225 pseudo-register to another with a REG_RETVAL note. */
226 SIMPLE_MOVE
229 /* This is called via for_each_rtx. If we find a SUBREG which we
230 could use to decompose a pseudo-register, set a bit in
231 DECOMPOSABLE_CONTEXT. If we find an unadorned register which is
232 not a simple pseudo-register copy, DATA will point at the type of
233 move, and we set a bit in DECOMPOSABLE_CONTEXT or
234 NON_DECOMPOSABLE_CONTEXT as appropriate. */
236 static int
237 find_decomposable_subregs (rtx *px, void *data)
239 enum classify_move_insn *pcmi = (enum classify_move_insn *) data;
240 rtx x = *px;
242 if (x == NULL_RTX)
243 return 0;
245 if (GET_CODE (x) == SUBREG)
247 rtx inner = SUBREG_REG (x);
248 unsigned int regno, outer_size, inner_size, outer_words, inner_words;
250 if (!REG_P (inner))
251 return 0;
253 regno = REGNO (inner);
254 if (HARD_REGISTER_NUM_P (regno))
255 return -1;
257 outer_size = GET_MODE_SIZE (GET_MODE (x));
258 inner_size = GET_MODE_SIZE (GET_MODE (inner));
259 outer_words = (outer_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
260 inner_words = (inner_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
262 /* We only try to decompose single word subregs of multi-word
263 registers. When we find one, we return -1 to avoid iterating
264 over the inner register.
266 ??? This doesn't allow, e.g., DImode subregs of TImode values
267 on 32-bit targets. We would need to record the way the
268 pseudo-register was used, and only decompose if all the uses
269 were the same number and size of pieces. Hopefully this
270 doesn't happen much. */
272 if (outer_words == 1 && inner_words > 1)
274 bitmap_set_bit (decomposable_context, regno);
275 return -1;
278 else if (REG_P (x))
280 unsigned int regno;
282 /* We will see an outer SUBREG before we see the inner REG, so
283 when we see a plain REG here it means a direct reference to
284 the register.
286 If this is not a simple copy from one location to another,
287 then we can not decompose this register. If this is a simple
288 copy from one pseudo-register to another, with no REG_RETVAL
289 note, and the mode is right, then we mark the register as
290 decomposable. Otherwise we don't say anything about this
291 register--it could be decomposed, but whether that would be
292 profitable depends upon how it is used elsewhere.
294 We only set bits in the bitmap for multi-word
295 pseudo-registers, since those are the only ones we care about
296 and it keeps the size of the bitmaps down. */
298 regno = REGNO (x);
299 if (!HARD_REGISTER_NUM_P (regno)
300 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
302 switch (*pcmi)
304 case NOT_SIMPLE_MOVE:
305 bitmap_set_bit (non_decomposable_context, regno);
306 break;
307 case SIMPLE_PSEUDO_REG_MOVE:
308 if (MODES_TIEABLE_P (GET_MODE (x), word_mode))
309 bitmap_set_bit (decomposable_context, regno);
310 break;
311 case SIMPLE_MOVE:
312 break;
313 default:
314 gcc_unreachable ();
318 else if (MEM_P (x))
320 enum classify_move_insn cmi_mem = NOT_SIMPLE_MOVE;
322 /* Any registers used in a MEM do not participate in a
323 SIMPLE_MOVE or SIMPLE_PSEUDO_REG_MOVE. Do our own recursion
324 here, and return -1 to block the parent's recursion. */
325 for_each_rtx (&XEXP (x, 0), find_decomposable_subregs, &cmi_mem);
326 return -1;
329 return 0;
332 /* Decompose REGNO into word-sized components. We smash the REG node
333 in place. This ensures that (1) something goes wrong quickly if we
334 fail to make some replacement, and (2) the debug information inside
335 the symbol table is automatically kept up to date. */
337 static void
338 decompose_register (unsigned int regno)
340 rtx reg;
341 unsigned int words, i;
342 rtvec v;
344 reg = regno_reg_rtx[regno];
346 regno_reg_rtx[regno] = NULL_RTX;
347 clear_reg_info_regno (regno);
349 words = GET_MODE_SIZE (GET_MODE (reg));
350 words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
352 v = rtvec_alloc (words);
353 for (i = 0; i < words; ++i)
354 RTVEC_ELT (v, i) = gen_reg_rtx_offset (reg, word_mode, i * UNITS_PER_WORD);
356 PUT_CODE (reg, CONCATN);
357 XVEC (reg, 0) = v;
359 if (dump_file)
361 fprintf (dump_file, "; Splitting reg %u ->", regno);
362 for (i = 0; i < words; ++i)
363 fprintf (dump_file, " %u", REGNO (XVECEXP (reg, 0, i)));
364 fputc ('\n', dump_file);
368 /* Get a SUBREG of a CONCATN. */
370 static rtx
371 simplify_subreg_concatn (enum machine_mode outermode, rtx op,
372 unsigned int byte)
374 unsigned int inner_size;
375 enum machine_mode innermode;
376 rtx part;
377 unsigned int final_offset;
379 gcc_assert (GET_CODE (op) == CONCATN);
380 gcc_assert (byte % GET_MODE_SIZE (outermode) == 0);
382 innermode = GET_MODE (op);
383 gcc_assert (byte < GET_MODE_SIZE (innermode));
384 gcc_assert (GET_MODE_SIZE (outermode) <= GET_MODE_SIZE (innermode));
386 inner_size = GET_MODE_SIZE (innermode) / XVECLEN (op, 0);
387 part = XVECEXP (op, 0, byte / inner_size);
388 final_offset = byte % inner_size;
389 if (final_offset + GET_MODE_SIZE (outermode) > inner_size)
390 return NULL_RTX;
392 return simplify_gen_subreg (outermode, part, GET_MODE (part), final_offset);
395 /* Wrapper around simplify_gen_subreg which handles CONCATN. */
397 static rtx
398 simplify_gen_subreg_concatn (enum machine_mode outermode, rtx op,
399 enum machine_mode innermode, unsigned int byte)
401 rtx ret;
403 /* We have to handle generating a SUBREG of a SUBREG of a CONCATN.
404 If OP is a SUBREG of a CONCATN, then it must be a simple mode
405 change with the same size and offset 0, or it must extract a
406 part. We shouldn't see anything else here. */
407 if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == CONCATN)
409 rtx op2;
411 if ((GET_MODE_SIZE (GET_MODE (op))
412 == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))))
413 && SUBREG_BYTE (op) == 0)
414 return simplify_gen_subreg_concatn (outermode, SUBREG_REG (op),
415 GET_MODE (SUBREG_REG (op)), byte);
417 op2 = simplify_subreg_concatn (GET_MODE (op), SUBREG_REG (op),
418 SUBREG_BYTE (op));
419 if (op2 == NULL_RTX)
421 /* We don't handle paradoxical subregs here. */
422 gcc_assert (GET_MODE_SIZE (outermode)
423 <= GET_MODE_SIZE (GET_MODE (op)));
424 gcc_assert (GET_MODE_SIZE (GET_MODE (op))
425 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))));
426 op2 = simplify_subreg_concatn (outermode, SUBREG_REG (op),
427 byte + SUBREG_BYTE (op));
428 gcc_assert (op2 != NULL_RTX);
429 return op2;
432 op = op2;
433 gcc_assert (op != NULL_RTX);
434 gcc_assert (innermode == GET_MODE (op));
437 if (GET_CODE (op) == CONCATN)
438 return simplify_subreg_concatn (outermode, op, byte);
440 ret = simplify_gen_subreg (outermode, op, innermode, byte);
442 /* If we see an insn like (set (reg:DI) (subreg:DI (reg:SI) 0)) then
443 resolve_simple_move will ask for the high part of the paradoxical
444 subreg, which does not have a value. Just return a zero. */
445 if (ret == NULL_RTX
446 && GET_CODE (op) == SUBREG
447 && SUBREG_BYTE (op) == 0
448 && (GET_MODE_SIZE (innermode)
449 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op)))))
450 return CONST0_RTX (outermode);
452 gcc_assert (ret != NULL_RTX);
453 return ret;
456 /* Return whether we should resolve X into the registers into which it
457 was decomposed. */
459 static bool
460 resolve_reg_p (rtx x)
462 return GET_CODE (x) == CONCATN;
465 /* Return whether X is a SUBREG of a register which we need to
466 resolve. */
468 static bool
469 resolve_subreg_p (rtx x)
471 if (GET_CODE (x) != SUBREG)
472 return false;
473 return resolve_reg_p (SUBREG_REG (x));
476 /* This is called via for_each_rtx. Look for SUBREGs which need to be
477 decomposed. */
479 static int
480 resolve_subreg_use (rtx *px, void *data)
482 rtx insn = (rtx) data;
483 rtx x = *px;
485 if (x == NULL_RTX)
486 return 0;
488 if (resolve_subreg_p (x))
490 x = simplify_subreg_concatn (GET_MODE (x), SUBREG_REG (x),
491 SUBREG_BYTE (x));
493 /* It is possible for a note to contain a reference which we can
494 decompose. In this case, return 1 to the caller to indicate
495 that the note must be removed. */
496 if (!x)
498 gcc_assert(!insn);
499 return 1;
502 validate_change (insn, px, x, 1);
503 return -1;
506 if (resolve_reg_p (x))
508 /* Return 1 to the caller to indicate that we found a direct
509 reference to a register which is being decomposed. This can
510 happen inside notes. */
511 gcc_assert (!insn);
512 return 1;
515 return 0;
518 /* If there is a REG_LIBCALL note on OLD_START, move it to NEW_START,
519 and link the corresponding REG_RETVAL note to NEW_START. */
521 static void
522 move_libcall_note (rtx old_start, rtx new_start)
524 rtx note0, note1, end;
526 note0 = find_reg_note (old_start, REG_LIBCALL, NULL);
527 if (note0 == NULL_RTX)
528 return;
530 remove_note (old_start, note0);
531 end = XEXP (note0, 0);
532 note1 = find_reg_note (end, REG_RETVAL, NULL);
534 XEXP (note0, 1) = REG_NOTES (new_start);
535 REG_NOTES (new_start) = note0;
536 XEXP (note1, 0) = new_start;
539 /* Remove any REG_RETVAL note, the corresponding REG_LIBCALL note, and
540 any markers for a no-conflict block. We have decomposed the
541 registers so the non-conflict is now obvious. */
543 static void
544 remove_retval_note (rtx insn1)
546 rtx note0, insn0, note1, insn;
548 note1 = find_reg_note (insn1, REG_RETVAL, NULL);
549 if (note1 == NULL_RTX)
550 return;
552 insn0 = XEXP (note1, 0);
553 note0 = find_reg_note (insn0, REG_LIBCALL, NULL);
555 remove_note (insn0, note0);
556 remove_note (insn1, note1);
558 for (insn = insn0; insn != insn1; insn = NEXT_INSN (insn))
560 while (1)
562 rtx note;
564 note = find_reg_note (insn, REG_NO_CONFLICT, NULL);
565 if (note == NULL_RTX)
566 break;
567 remove_note (insn, note);
572 /* Resolve any decomposed registers which appear in register notes on
573 INSN. */
575 static void
576 resolve_reg_notes (rtx insn)
578 rtx *pnote, note;
580 note = find_reg_equal_equiv_note (insn);
581 if (note)
583 if (for_each_rtx (&XEXP (note, 0), resolve_subreg_use, NULL))
585 remove_note (insn, note);
586 remove_retval_note (insn);
590 pnote = &REG_NOTES (insn);
591 while (*pnote != NULL_RTX)
593 bool delete = false;
595 note = *pnote;
596 switch (REG_NOTE_KIND (note))
598 case REG_NO_CONFLICT:
599 if (resolve_reg_p (XEXP (note, 0)))
600 delete = true;
601 break;
603 default:
604 break;
607 if (delete)
608 *pnote = XEXP (note, 1);
609 else
610 pnote = &XEXP (note, 1);
614 /* Return whether X can be decomposed into subwords. */
616 static bool
617 can_decompose_p (rtx x)
619 if (REG_P (x))
621 unsigned int regno = REGNO (x);
623 if (HARD_REGISTER_NUM_P (regno))
624 return (validate_subreg (word_mode, GET_MODE (x), x, UNITS_PER_WORD)
625 && HARD_REGNO_MODE_OK (regno, word_mode));
626 else
627 return !bitmap_bit_p (non_decomposable_context, regno);
630 return true;
633 /* Decompose the registers used in a simple move SET within INSN. If
634 we don't change anything, return INSN, otherwise return the start
635 of the sequence of moves. */
637 static rtx
638 resolve_simple_move (rtx set, rtx insn)
640 rtx src, dest, real_dest, insns;
641 enum machine_mode orig_mode, dest_mode;
642 unsigned int words;
643 bool pushing;
645 src = SET_SRC (set);
646 dest = SET_DEST (set);
647 orig_mode = GET_MODE (dest);
649 words = (GET_MODE_SIZE (orig_mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
650 if (words <= 1)
651 return insn;
653 start_sequence ();
655 /* We have to handle copying from a SUBREG of a decomposed reg where
656 the SUBREG is larger than word size. Rather than assume that we
657 can take a word_mode SUBREG of the destination, we copy to a new
658 register and then copy that to the destination. */
660 real_dest = NULL_RTX;
662 if (GET_CODE (src) == SUBREG
663 && resolve_reg_p (SUBREG_REG (src))
664 && (SUBREG_BYTE (src) != 0
665 || (GET_MODE_SIZE (orig_mode)
666 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))))
668 real_dest = dest;
669 dest = gen_reg_rtx (orig_mode);
670 if (REG_P (real_dest))
671 REG_ATTRS (dest) = REG_ATTRS (real_dest);
674 /* Similarly if we are copying to a SUBREG of a decomposed reg where
675 the SUBREG is larger than word size. */
677 if (GET_CODE (dest) == SUBREG
678 && resolve_reg_p (SUBREG_REG (dest))
679 && (SUBREG_BYTE (dest) != 0
680 || (GET_MODE_SIZE (orig_mode)
681 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))))
683 rtx reg, minsn, smove;
685 reg = gen_reg_rtx (orig_mode);
686 minsn = emit_move_insn (reg, src);
687 smove = single_set (minsn);
688 gcc_assert (smove != NULL_RTX);
689 resolve_simple_move (smove, minsn);
690 src = reg;
693 /* If we didn't have any big SUBREGS of decomposed registers, and
694 neither side of the move is a register we are decomposing, then
695 we don't have to do anything here. */
697 if (src == SET_SRC (set)
698 && dest == SET_DEST (set)
699 && !resolve_reg_p (src)
700 && !resolve_subreg_p (src)
701 && !resolve_reg_p (dest)
702 && !resolve_subreg_p (dest))
704 end_sequence ();
705 return insn;
708 /* If SRC is a register which we can't decompose, or has side
709 effects, we need to move via a temporary register. */
711 if (!can_decompose_p (src)
712 || side_effects_p (src)
713 || GET_CODE (src) == ASM_OPERANDS)
715 rtx reg;
717 reg = gen_reg_rtx (orig_mode);
718 emit_move_insn (reg, src);
719 src = reg;
722 /* If DEST is a register which we can't decompose, or has side
723 effects, we need to first move to a temporary register. We
724 handle the common case of pushing an operand directly. We also
725 go through a temporary register if it holds a floating point
726 value. This gives us better code on systems which can't move
727 data easily between integer and floating point registers. */
729 dest_mode = orig_mode;
730 pushing = push_operand (dest, dest_mode);
731 if (!can_decompose_p (dest)
732 || (side_effects_p (dest) && !pushing)
733 || (!SCALAR_INT_MODE_P (dest_mode)
734 && !resolve_reg_p (dest)
735 && !resolve_subreg_p (dest)))
737 if (real_dest == NULL_RTX)
738 real_dest = dest;
739 if (!SCALAR_INT_MODE_P (dest_mode))
741 dest_mode = mode_for_size (GET_MODE_SIZE (dest_mode) * BITS_PER_UNIT,
742 MODE_INT, 0);
743 gcc_assert (dest_mode != BLKmode);
745 dest = gen_reg_rtx (dest_mode);
746 if (REG_P (real_dest))
747 REG_ATTRS (dest) = REG_ATTRS (real_dest);
750 if (pushing)
752 unsigned int i, j, jinc;
754 gcc_assert (GET_MODE_SIZE (orig_mode) % UNITS_PER_WORD == 0);
755 gcc_assert (GET_CODE (XEXP (dest, 0)) != PRE_MODIFY);
756 gcc_assert (GET_CODE (XEXP (dest, 0)) != POST_MODIFY);
758 if (WORDS_BIG_ENDIAN == STACK_GROWS_DOWNWARD)
760 j = 0;
761 jinc = 1;
763 else
765 j = words - 1;
766 jinc = -1;
769 for (i = 0; i < words; ++i, j += jinc)
771 rtx temp;
773 temp = copy_rtx (XEXP (dest, 0));
774 temp = adjust_automodify_address_nv (dest, word_mode, temp,
775 j * UNITS_PER_WORD);
776 emit_move_insn (temp,
777 simplify_gen_subreg_concatn (word_mode, src,
778 orig_mode,
779 j * UNITS_PER_WORD));
782 else
784 unsigned int i;
786 if (REG_P (dest) && !HARD_REGISTER_NUM_P (REGNO (dest)))
787 emit_insn (gen_rtx_CLOBBER (VOIDmode, dest));
789 for (i = 0; i < words; ++i)
790 emit_move_insn (simplify_gen_subreg_concatn (word_mode, dest,
791 dest_mode,
792 i * UNITS_PER_WORD),
793 simplify_gen_subreg_concatn (word_mode, src,
794 orig_mode,
795 i * UNITS_PER_WORD));
798 if (real_dest != NULL_RTX)
800 rtx mdest, minsn, smove;
802 if (dest_mode == orig_mode)
803 mdest = dest;
804 else
805 mdest = simplify_gen_subreg (orig_mode, dest, GET_MODE (dest), 0);
806 minsn = emit_move_insn (real_dest, mdest);
808 smove = single_set (minsn);
809 gcc_assert (smove != NULL_RTX);
811 resolve_simple_move (smove, minsn);
814 insns = get_insns ();
815 end_sequence ();
817 emit_insn_before (insns, insn);
819 move_libcall_note (insn, insns);
820 remove_retval_note (insn);
821 delete_insn (insn);
823 return insns;
826 /* Change a CLOBBER of a decomposed register into a CLOBBER of the
827 component registers. Return whether we changed something. */
829 static bool
830 resolve_clobber (rtx pat, rtx insn)
832 rtx reg;
833 enum machine_mode orig_mode;
834 unsigned int words, i;
836 reg = XEXP (pat, 0);
837 if (!resolve_reg_p (reg) && !resolve_subreg_p (reg))
838 return false;
840 orig_mode = GET_MODE (reg);
841 words = GET_MODE_SIZE (orig_mode);
842 words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
844 XEXP (pat, 0) = simplify_gen_subreg_concatn (word_mode, reg, orig_mode, 0);
845 for (i = words - 1; i > 0; --i)
847 rtx x;
849 x = simplify_gen_subreg_concatn (word_mode, reg, orig_mode,
850 i * UNITS_PER_WORD);
851 x = gen_rtx_CLOBBER (VOIDmode, x);
852 emit_insn_after (x, insn);
855 return true;
858 /* A USE of a decomposed register is no longer meaningful. Return
859 whether we changed something. */
861 static bool
862 resolve_use (rtx pat, rtx insn)
864 if (resolve_reg_p (XEXP (pat, 0)) || resolve_subreg_p (XEXP (pat, 0)))
866 delete_insn (insn);
867 return true;
869 return false;
872 /* Look for registers which are always accessed via word-sized SUBREGs
873 or via copies. Decompose these registers into several word-sized
874 pseudo-registers. */
876 static void
877 decompose_multiword_subregs (bool update_life)
879 unsigned int max;
880 basic_block bb;
882 max = max_reg_num ();
884 /* First see if there are any multi-word pseudo-registers. If there
885 aren't, there is nothing we can do. This should speed up this
886 pass in the normal case, since it should be faster than scanning
887 all the insns. */
889 unsigned int i;
891 for (i = FIRST_PSEUDO_REGISTER; i < max; ++i)
893 if (regno_reg_rtx[i] != NULL
894 && GET_MODE_SIZE (GET_MODE (regno_reg_rtx[i])) > UNITS_PER_WORD)
895 break;
897 if (i == max)
898 return;
901 /* FIXME: When the dataflow branch is merged, we can change this
902 code to look for each multi-word pseudo-register and to find each
903 insn which sets or uses that register. That should be faster
904 than scanning all the insns. */
906 decomposable_context = BITMAP_ALLOC (NULL);
907 non_decomposable_context = BITMAP_ALLOC (NULL);
909 reg_copy_graph = VEC_alloc (bitmap, heap, max);
910 VEC_safe_grow (bitmap, heap, reg_copy_graph, max);
911 memset (VEC_address (bitmap, reg_copy_graph), 0, sizeof (bitmap) * max);
913 FOR_EACH_BB (bb)
915 rtx insn;
917 FOR_BB_INSNS (bb, insn)
919 rtx set;
920 enum classify_move_insn cmi;
921 int i, n;
923 if (!INSN_P (insn)
924 || GET_CODE (PATTERN (insn)) == CLOBBER
925 || GET_CODE (PATTERN (insn)) == USE)
926 continue;
928 recog_memoized (insn);
929 extract_insn (insn);
931 set = simple_move (insn);
933 if (!set)
934 cmi = NOT_SIMPLE_MOVE;
935 else
937 bool retval;
939 retval = find_reg_note (insn, REG_RETVAL, NULL_RTX) != NULL_RTX;
941 if (find_pseudo_copy (set) && !retval)
942 cmi = SIMPLE_PSEUDO_REG_MOVE;
943 else if (retval
944 && REG_P (SET_SRC (set))
945 && HARD_REGISTER_P (SET_SRC (set)))
947 rtx note;
949 /* We don't want to decompose an assignment which
950 copies the value returned by a libcall to a
951 pseudo-register. Doing that will lose the RETVAL
952 note with no real gain. */
953 cmi = NOT_SIMPLE_MOVE;
955 /* If we have a RETVAL note, there should be an
956 EQUAL note. We don't want to decompose any
957 registers which that EQUAL note refers to
958 directly. If we do, we will no longer know the
959 value of the libcall. */
960 note = find_reg_equal_equiv_note (insn);
961 if (note != NULL_RTX)
962 for_each_rtx (&XEXP (note, 0), find_decomposable_subregs,
963 &cmi);
965 else
966 cmi = SIMPLE_MOVE;
969 n = recog_data.n_operands;
970 for (i = 0; i < n; ++i)
972 for_each_rtx (&recog_data.operand[i],
973 find_decomposable_subregs,
974 &cmi);
976 /* We handle ASM_OPERANDS as a special case to support
977 things like x86 rdtsc which returns a DImode value.
978 We can decompose the output, which will certainly be
979 operand 0, but not the inputs. */
981 if (cmi == SIMPLE_MOVE
982 && GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
984 gcc_assert (i == 0);
985 cmi = NOT_SIMPLE_MOVE;
991 bitmap_and_compl_into (decomposable_context, non_decomposable_context);
992 if (!bitmap_empty_p (decomposable_context))
994 int hold_no_new_pseudos = no_new_pseudos;
995 int max_regno = max_reg_num ();
996 sbitmap blocks;
997 bitmap_iterator iter;
998 unsigned int regno;
1000 propagate_pseudo_copies ();
1002 no_new_pseudos = 0;
1003 blocks = sbitmap_alloc (last_basic_block);
1004 sbitmap_zero (blocks);
1006 EXECUTE_IF_SET_IN_BITMAP (decomposable_context, 0, regno, iter)
1007 decompose_register (regno);
1009 FOR_EACH_BB (bb)
1011 rtx insn;
1013 FOR_BB_INSNS (bb, insn)
1015 rtx next, pat;
1016 bool changed;
1018 if (!INSN_P (insn))
1019 continue;
1021 next = NEXT_INSN (insn);
1022 changed = false;
1024 pat = PATTERN (insn);
1025 if (GET_CODE (pat) == CLOBBER)
1027 if (resolve_clobber (pat, insn))
1028 changed = true;
1030 else if (GET_CODE (pat) == USE)
1032 if (resolve_use (pat, insn))
1033 changed = true;
1035 else
1037 rtx set;
1038 int i;
1040 recog_memoized (insn);
1041 extract_insn (insn);
1043 set = simple_move (insn);
1044 if (set)
1046 rtx orig_insn = insn;
1048 insn = resolve_simple_move (set, insn);
1049 if (insn != orig_insn)
1051 changed = true;
1053 recog_memoized (insn);
1054 extract_insn (insn);
1058 for (i = recog_data.n_operands - 1; i >= 0; --i)
1059 for_each_rtx (recog_data.operand_loc[i],
1060 resolve_subreg_use,
1061 insn);
1063 resolve_reg_notes (insn);
1065 if (num_validated_changes () > 0)
1067 for (i = recog_data.n_dups - 1; i >= 0; --i)
1069 rtx *pl = recog_data.dup_loc[i];
1070 int dup_num = recog_data.dup_num[i];
1071 rtx *px = recog_data.operand_loc[dup_num];
1073 validate_change (insn, pl, *px, 1);
1076 i = apply_change_group ();
1077 gcc_assert (i);
1079 changed = true;
1083 if (changed)
1085 SET_BIT (blocks, bb->index);
1086 reg_scan_update (insn, next, max_regno);
1091 no_new_pseudos = hold_no_new_pseudos;
1093 if (update_life)
1094 update_life_info (blocks, UPDATE_LIFE_GLOBAL_RM_NOTES,
1095 PROP_DEATH_NOTES);
1097 sbitmap_free (blocks);
1101 unsigned int i;
1102 bitmap b;
1104 for (i = 0; VEC_iterate (bitmap, reg_copy_graph, i, b); ++i)
1105 if (b)
1106 BITMAP_FREE (b);
1109 VEC_free (bitmap, heap, reg_copy_graph);
1111 BITMAP_FREE (decomposable_context);
1112 BITMAP_FREE (non_decomposable_context);
1115 /* Gate function for lower subreg pass. */
1117 static bool
1118 gate_handle_lower_subreg (void)
1120 return flag_split_wide_types != 0;
1123 /* Implement first lower subreg pass. */
1125 static unsigned int
1126 rest_of_handle_lower_subreg (void)
1128 decompose_multiword_subregs (false);
1129 return 0;
1132 /* Implement second lower subreg pass. */
1134 static unsigned int
1135 rest_of_handle_lower_subreg2 (void)
1137 decompose_multiword_subregs (true);
1138 return 0;
1141 struct tree_opt_pass pass_lower_subreg =
1143 "subreg", /* name */
1144 gate_handle_lower_subreg, /* gate */
1145 rest_of_handle_lower_subreg, /* execute */
1146 NULL, /* sub */
1147 NULL, /* next */
1148 0, /* static_pass_number */
1149 TV_LOWER_SUBREG, /* tv_id */
1150 0, /* properties_required */
1151 0, /* properties_provided */
1152 0, /* properties_destroyed */
1153 0, /* todo_flags_start */
1154 TODO_dump_func |
1155 TODO_ggc_collect, /* todo_flags_finish */
1156 'u' /* letter */
1159 struct tree_opt_pass pass_lower_subreg2 =
1161 "subreg2", /* name */
1162 gate_handle_lower_subreg, /* gate */
1163 rest_of_handle_lower_subreg2, /* execute */
1164 NULL, /* sub */
1165 NULL, /* next */
1166 0, /* static_pass_number */
1167 TV_LOWER_SUBREG, /* tv_id */
1168 0, /* properties_required */
1169 0, /* properties_provided */
1170 0, /* properties_destroyed */
1171 0, /* todo_flags_start */
1172 TODO_dump_func |
1173 TODO_ggc_collect, /* todo_flags_finish */
1174 'U' /* letter */