OSDN Git Service

* config/rs6000/rs6000.c (function_arg_advance): For 32-bit ELF ABI,
[pf3gnuchains/gcc-fork.git] / gcc / lower-subreg.c
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>
5
6 This file is part of GCC.
7
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.
12
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.
17
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.  */
22
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 "except.h"
39 #include "regs.h"
40 #include "tree-pass.h"
41
42 #ifdef STACK_GROWS_DOWNWARD
43 # undef STACK_GROWS_DOWNWARD
44 # define STACK_GROWS_DOWNWARD 1
45 #else
46 # define STACK_GROWS_DOWNWARD 0
47 #endif
48
49 DEF_VEC_P (bitmap);
50 DEF_VEC_ALLOC_P (bitmap,heap);
51
52 /* Decompose multi-word pseudo-registers into individual
53    pseudo-registers when possible.  This is possible when all the uses
54    of a multi-word register are via SUBREG, or are copies of the
55    register to another location.  Breaking apart the register permits
56    more CSE and permits better register allocation.  */
57
58 /* Bit N in this bitmap is set if regno N is used in a context in
59    which we can decompose it.  */
60 static bitmap decomposable_context;
61
62 /* Bit N in this bitmap is set if regno N is used in a context in
63    which it can not be decomposed.  */
64 static bitmap non_decomposable_context;
65
66 /* Bit N in the bitmap in element M of this array is set if there is a
67    copy from reg M to reg N.  */
68 static VEC(bitmap,heap) *reg_copy_graph;
69
70 /* Return whether X is a simple object which we can take a word_mode
71    subreg of.  */
72
73 static bool
74 simple_move_operand (rtx x)
75 {
76   if (GET_CODE (x) == SUBREG)
77     x = SUBREG_REG (x);
78
79   if (!OBJECT_P (x))
80     return false;
81
82   if (GET_CODE (x) == LABEL_REF
83       || GET_CODE (x) == SYMBOL_REF
84       || GET_CODE (x) == HIGH
85       || GET_CODE (x) == CONST)
86     return false;
87
88   if (MEM_P (x)
89       && (MEM_VOLATILE_P (x)
90           || mode_dependent_address_p (XEXP (x, 0))))
91     return false;
92
93   return true;
94 }
95
96 /* If INSN is a single set between two objects, return the single set.
97    Such an insn can always be decomposed.  INSN should have been
98    passed to recog and extract_insn before this is called.  */
99
100 static rtx
101 simple_move (rtx insn)
102 {
103   rtx x;
104   rtx set;
105   enum machine_mode mode;
106
107   if (recog_data.n_operands != 2)
108     return NULL_RTX;
109
110   set = single_set (insn);
111   if (!set)
112     return NULL_RTX;
113
114   x = SET_DEST (set);
115   if (x != recog_data.operand[0] && x != recog_data.operand[1])
116     return NULL_RTX;
117   if (!simple_move_operand (x))
118     return NULL_RTX;
119
120   x = SET_SRC (set);
121   if (x != recog_data.operand[0] && x != recog_data.operand[1])
122     return NULL_RTX;
123   /* For the src we can handle ASM_OPERANDS, and it is beneficial for
124      things like x86 rdtsc which returns a DImode value.  */
125   if (GET_CODE (x) != ASM_OPERANDS
126       && !simple_move_operand (x))
127     return NULL_RTX;
128
129   /* We try to decompose in integer modes, to avoid generating
130      inefficient code copying between integer and floating point
131      registers.  That means that we can't decompose if this is a
132      non-integer mode for which there is no integer mode of the same
133      size.  */
134   mode = GET_MODE (SET_SRC (set));
135   if (!SCALAR_INT_MODE_P (mode)
136       && (mode_for_size (GET_MODE_SIZE (mode) * BITS_PER_UNIT, MODE_INT, 0)
137           == BLKmode))
138     return NULL_RTX;
139
140   /* Reject PARTIAL_INT modes.  They are used for processor specific
141      purposes and it's probably best not to tamper with them.  */
142   if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
143     return NULL_RTX;
144
145   return set;
146 }
147
148 /* If SET is a copy from one multi-word pseudo-register to another,
149    record that in reg_copy_graph.  Return whether it is such a
150    copy.  */
151
152 static bool
153 find_pseudo_copy (rtx set)
154 {
155   rtx dest = SET_DEST (set);
156   rtx src = SET_SRC (set);
157   unsigned int rd, rs;
158   bitmap b;
159
160   if (!REG_P (dest) || !REG_P (src))
161     return false;
162
163   rd = REGNO (dest);
164   rs = REGNO (src);
165   if (HARD_REGISTER_NUM_P (rd) || HARD_REGISTER_NUM_P (rs))
166     return false;
167
168   if (GET_MODE_SIZE (GET_MODE (dest)) <= UNITS_PER_WORD)
169     return false;
170
171   b = VEC_index (bitmap, reg_copy_graph, rs);
172   if (b == NULL)
173     {
174       b = BITMAP_ALLOC (NULL);
175       VEC_replace (bitmap, reg_copy_graph, rs, b);
176     }
177
178   bitmap_set_bit (b, rd);
179
180   return true;
181 }
182
183 /* Look through the registers in DECOMPOSABLE_CONTEXT.  For each case
184    where they are copied to another register, add the register to
185    which they are copied to DECOMPOSABLE_CONTEXT.  Use
186    NON_DECOMPOSABLE_CONTEXT to limit this--we don't bother to track
187    copies of registers which are in NON_DECOMPOSABLE_CONTEXT.  */
188
189 static void
190 propagate_pseudo_copies (void)
191 {
192   bitmap queue, propagate;
193
194   queue = BITMAP_ALLOC (NULL);
195   propagate = BITMAP_ALLOC (NULL);
196
197   bitmap_copy (queue, decomposable_context);
198   do
199     {
200       bitmap_iterator iter;
201       unsigned int i;
202
203       bitmap_clear (propagate);
204
205       EXECUTE_IF_SET_IN_BITMAP (queue, 0, i, iter)
206         {
207           bitmap b = VEC_index (bitmap, reg_copy_graph, i);
208           if (b)
209             bitmap_ior_and_compl_into (propagate, b, non_decomposable_context);
210         }
211
212       bitmap_and_compl (queue, propagate, decomposable_context);
213       bitmap_ior_into (decomposable_context, propagate);
214     }
215   while (!bitmap_empty_p (queue));
216
217   BITMAP_FREE (queue);
218   BITMAP_FREE (propagate);
219 }
220
221 /* A pointer to one of these values is passed to
222    find_decomposable_subregs via for_each_rtx.  */
223
224 enum classify_move_insn
225 {
226   /* Not a simple move from one location to another.  */
227   NOT_SIMPLE_MOVE,
228   /* A simple move from one pseudo-register to another with no
229      REG_RETVAL note.  */
230   SIMPLE_PSEUDO_REG_MOVE,
231   /* A simple move involving a non-pseudo-register, or from one
232      pseudo-register to another with a REG_RETVAL note.  */
233   SIMPLE_MOVE
234 };
235
236 /* This is called via for_each_rtx.  If we find a SUBREG which we
237    could use to decompose a pseudo-register, set a bit in
238    DECOMPOSABLE_CONTEXT.  If we find an unadorned register which is
239    not a simple pseudo-register copy, DATA will point at the type of
240    move, and we set a bit in DECOMPOSABLE_CONTEXT or
241    NON_DECOMPOSABLE_CONTEXT as appropriate.  */
242
243 static int
244 find_decomposable_subregs (rtx *px, void *data)
245 {
246   enum classify_move_insn *pcmi = (enum classify_move_insn *) data;
247   rtx x = *px;
248
249   if (x == NULL_RTX)
250     return 0;
251
252   if (GET_CODE (x) == SUBREG)
253     {
254       rtx inner = SUBREG_REG (x);
255       unsigned int regno, outer_size, inner_size, outer_words, inner_words;
256
257       if (!REG_P (inner))
258         return 0;
259
260       regno = REGNO (inner);
261       if (HARD_REGISTER_NUM_P (regno))
262         return -1;
263
264       outer_size = GET_MODE_SIZE (GET_MODE (x));
265       inner_size = GET_MODE_SIZE (GET_MODE (inner));
266       outer_words = (outer_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
267       inner_words = (inner_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
268
269       /* We only try to decompose single word subregs of multi-word
270          registers.  When we find one, we return -1 to avoid iterating
271          over the inner register.
272
273          ??? This doesn't allow, e.g., DImode subregs of TImode values
274          on 32-bit targets.  We would need to record the way the
275          pseudo-register was used, and only decompose if all the uses
276          were the same number and size of pieces.  Hopefully this
277          doesn't happen much.  */
278
279       if (outer_words == 1 && inner_words > 1)
280         {
281           bitmap_set_bit (decomposable_context, regno);
282           return -1;
283         }
284     }
285   else if (REG_P (x))
286     {
287       unsigned int regno;
288
289       /* We will see an outer SUBREG before we see the inner REG, so
290          when we see a plain REG here it means a direct reference to
291          the register.
292
293          If this is not a simple copy from one location to another,
294          then we can not decompose this register.  If this is a simple
295          copy from one pseudo-register to another, with no REG_RETVAL
296          note, and the mode is right, then we mark the register as
297          decomposable.  Otherwise we don't say anything about this
298          register--it could be decomposed, but whether that would be
299          profitable depends upon how it is used elsewhere.
300
301          We only set bits in the bitmap for multi-word
302          pseudo-registers, since those are the only ones we care about
303          and it keeps the size of the bitmaps down.  */
304
305       regno = REGNO (x);
306       if (!HARD_REGISTER_NUM_P (regno)
307           && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
308         {
309           switch (*pcmi)
310             {
311             case NOT_SIMPLE_MOVE:
312               bitmap_set_bit (non_decomposable_context, regno);
313               break;
314             case SIMPLE_PSEUDO_REG_MOVE:
315               if (MODES_TIEABLE_P (GET_MODE (x), word_mode))
316                 bitmap_set_bit (decomposable_context, regno);
317               break;
318             case SIMPLE_MOVE:
319               break;
320             default:
321               gcc_unreachable ();
322             }
323         }
324     }
325   else if (MEM_P (x))
326     {
327       enum classify_move_insn cmi_mem = NOT_SIMPLE_MOVE;
328
329       /* Any registers used in a MEM do not participate in a
330          SIMPLE_MOVE or SIMPLE_PSEUDO_REG_MOVE.  Do our own recursion
331          here, and return -1 to block the parent's recursion.  */
332       for_each_rtx (&XEXP (x, 0), find_decomposable_subregs, &cmi_mem);
333       return -1;
334     }
335
336   return 0;
337 }
338
339 /* Decompose REGNO into word-sized components.  We smash the REG node
340    in place.  This ensures that (1) something goes wrong quickly if we
341    fail to make some replacement, and (2) the debug information inside
342    the symbol table is automatically kept up to date.  */
343
344 static void
345 decompose_register (unsigned int regno)
346 {
347   rtx reg;
348   unsigned int words, i;
349   rtvec v;
350
351   reg = regno_reg_rtx[regno];
352
353   regno_reg_rtx[regno] = NULL_RTX;
354   clear_reg_info_regno (regno);
355
356   words = GET_MODE_SIZE (GET_MODE (reg));
357   words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
358
359   v = rtvec_alloc (words);
360   for (i = 0; i < words; ++i)
361     RTVEC_ELT (v, i) = gen_reg_rtx_offset (reg, word_mode, i * UNITS_PER_WORD);
362
363   PUT_CODE (reg, CONCATN);
364   XVEC (reg, 0) = v;
365
366   if (dump_file)
367     {
368       fprintf (dump_file, "; Splitting reg %u ->", regno);
369       for (i = 0; i < words; ++i)
370         fprintf (dump_file, " %u", REGNO (XVECEXP (reg, 0, i)));
371       fputc ('\n', dump_file);
372     }
373 }
374
375 /* Get a SUBREG of a CONCATN.  */
376
377 static rtx
378 simplify_subreg_concatn (enum machine_mode outermode, rtx op,
379                          unsigned int byte)
380 {
381   unsigned int inner_size;
382   enum machine_mode innermode;
383   rtx part;
384   unsigned int final_offset;
385
386   gcc_assert (GET_CODE (op) == CONCATN);
387   gcc_assert (byte % GET_MODE_SIZE (outermode) == 0);
388
389   innermode = GET_MODE (op);
390   gcc_assert (byte < GET_MODE_SIZE (innermode));
391   gcc_assert (GET_MODE_SIZE (outermode) <= GET_MODE_SIZE (innermode));
392
393   inner_size = GET_MODE_SIZE (innermode) / XVECLEN (op, 0);
394   part = XVECEXP (op, 0, byte / inner_size);
395   final_offset = byte % inner_size;
396   if (final_offset + GET_MODE_SIZE (outermode) > inner_size)
397     return NULL_RTX;
398
399   return simplify_gen_subreg (outermode, part, GET_MODE (part), final_offset);
400 }
401
402 /* Wrapper around simplify_gen_subreg which handles CONCATN.  */
403
404 static rtx
405 simplify_gen_subreg_concatn (enum machine_mode outermode, rtx op,
406                              enum machine_mode innermode, unsigned int byte)
407 {
408   rtx ret;
409
410   /* We have to handle generating a SUBREG of a SUBREG of a CONCATN.
411      If OP is a SUBREG of a CONCATN, then it must be a simple mode
412      change with the same size and offset 0, or it must extract a
413      part.  We shouldn't see anything else here.  */
414   if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == CONCATN)
415     {
416       rtx op2;
417
418       if ((GET_MODE_SIZE (GET_MODE (op))
419            == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))))
420           && SUBREG_BYTE (op) == 0)
421         return simplify_gen_subreg_concatn (outermode, SUBREG_REG (op),
422                                             GET_MODE (SUBREG_REG (op)), byte);
423
424       op2 = simplify_subreg_concatn (GET_MODE (op), SUBREG_REG (op),
425                                      SUBREG_BYTE (op));
426       if (op2 == NULL_RTX)
427         {
428           /* We don't handle paradoxical subregs here.  */
429           gcc_assert (GET_MODE_SIZE (outermode)
430                       <= GET_MODE_SIZE (GET_MODE (op)));
431           gcc_assert (GET_MODE_SIZE (GET_MODE (op))
432                       <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))));
433           op2 = simplify_subreg_concatn (outermode, SUBREG_REG (op),
434                                          byte + SUBREG_BYTE (op));
435           gcc_assert (op2 != NULL_RTX);
436           return op2;
437         }
438
439       op = op2;
440       gcc_assert (op != NULL_RTX);
441       gcc_assert (innermode == GET_MODE (op));
442     }
443
444   if (GET_CODE (op) == CONCATN)
445     return simplify_subreg_concatn (outermode, op, byte);
446
447   ret = simplify_gen_subreg (outermode, op, innermode, byte);
448
449   /* If we see an insn like (set (reg:DI) (subreg:DI (reg:SI) 0)) then
450      resolve_simple_move will ask for the high part of the paradoxical
451      subreg, which does not have a value.  Just return a zero.  */
452   if (ret == NULL_RTX
453       && GET_CODE (op) == SUBREG
454       && SUBREG_BYTE (op) == 0
455       && (GET_MODE_SIZE (innermode)
456           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op)))))
457     return CONST0_RTX (outermode);
458
459   gcc_assert (ret != NULL_RTX);
460   return ret;
461 }
462
463 /* Return whether we should resolve X into the registers into which it
464    was decomposed.  */
465
466 static bool
467 resolve_reg_p (rtx x)
468 {
469   return GET_CODE (x) == CONCATN;
470 }
471
472 /* Return whether X is a SUBREG of a register which we need to
473    resolve.  */
474
475 static bool
476 resolve_subreg_p (rtx x)
477 {
478   if (GET_CODE (x) != SUBREG)
479     return false;
480   return resolve_reg_p (SUBREG_REG (x));
481 }
482
483 /* This is called via for_each_rtx.  Look for SUBREGs which need to be
484    decomposed.  */
485
486 static int
487 resolve_subreg_use (rtx *px, void *data)
488 {
489   rtx insn = (rtx) data;
490   rtx x = *px;
491
492   if (x == NULL_RTX)
493     return 0;
494
495   if (resolve_subreg_p (x))
496     {
497       x = simplify_subreg_concatn (GET_MODE (x), SUBREG_REG (x),
498                                    SUBREG_BYTE (x));
499
500       /* It is possible for a note to contain a reference which we can
501          decompose.  In this case, return 1 to the caller to indicate
502          that the note must be removed.  */
503       if (!x)
504         {
505           gcc_assert (!insn);
506           return 1;
507         }
508
509       validate_change (insn, px, x, 1);
510       return -1;
511     }
512
513   if (resolve_reg_p (x))
514     {
515       /* Return 1 to the caller to indicate that we found a direct
516          reference to a register which is being decomposed.  This can
517          happen inside notes.  */
518       gcc_assert (!insn);
519       return 1;
520     }
521
522   return 0;
523 }
524
525 /* We are deleting INSN.  Move any EH_REGION notes to INSNS.  */
526
527 static void
528 move_eh_region_note (rtx insn, rtx insns)
529 {
530   rtx note, p;
531
532   note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
533   if (note == NULL_RTX)
534     return;
535
536   gcc_assert (CALL_P (insn)
537               || (flag_non_call_exceptions && may_trap_p (PATTERN (insn))));
538
539   for (p = insns; p != NULL_RTX; p = NEXT_INSN (p))
540     {
541       if (CALL_P (p)
542           || (flag_non_call_exceptions
543               && INSN_P (p)
544               && may_trap_p (PATTERN (p))))
545         REG_NOTES (p) = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (note, 0),
546                                            REG_NOTES (p));
547     }
548 }
549
550 /* If there is a REG_LIBCALL note on OLD_START, move it to NEW_START,
551    and link the corresponding REG_RETVAL note to NEW_START.  */
552
553 static void
554 move_libcall_note (rtx old_start, rtx new_start)
555 {
556   rtx note0, note1, end;
557
558   note0 = find_reg_note (old_start, REG_LIBCALL, NULL);
559   if (note0 == NULL_RTX)
560     return;
561
562   remove_note (old_start, note0);
563   end = XEXP (note0, 0);
564   note1 = find_reg_note (end, REG_RETVAL, NULL);
565
566   XEXP (note0, 1) = REG_NOTES (new_start);
567   REG_NOTES (new_start) = note0;
568   XEXP (note1, 0) = new_start;
569 }
570
571 /* Remove any REG_RETVAL note, the corresponding REG_LIBCALL note, and
572    any markers for a no-conflict block.  We have decomposed the
573    registers so the non-conflict is now obvious.  */
574
575 static void
576 remove_retval_note (rtx insn1)
577 {
578   rtx note0, insn0, note1, insn;
579
580   note1 = find_reg_note (insn1, REG_RETVAL, NULL);
581   if (note1 == NULL_RTX)
582     return;
583
584   insn0 = XEXP (note1, 0);
585   note0 = find_reg_note (insn0, REG_LIBCALL, NULL);
586
587   remove_note (insn0, note0);
588   remove_note (insn1, note1);
589
590   for (insn = insn0; insn != insn1; insn = NEXT_INSN (insn))
591     {
592       while (1)
593         {
594           rtx note;
595
596           note = find_reg_note (insn, REG_NO_CONFLICT, NULL);
597           if (note == NULL_RTX)
598             break;
599           remove_note (insn, note);
600         }
601     }
602 }
603
604 /* Resolve any decomposed registers which appear in register notes on
605    INSN.  */
606
607 static void
608 resolve_reg_notes (rtx insn)
609 {
610   rtx *pnote, note;
611
612   note = find_reg_equal_equiv_note (insn);
613   if (note)
614     {
615       if (for_each_rtx (&XEXP (note, 0), resolve_subreg_use, NULL))
616         {
617           remove_note (insn, note);
618           remove_retval_note (insn);
619         }
620     }
621
622   pnote = &REG_NOTES (insn);
623   while (*pnote != NULL_RTX)
624     {
625       bool delete = false;
626
627       note = *pnote;
628       switch (REG_NOTE_KIND (note))
629         {
630         case REG_NO_CONFLICT:
631           if (resolve_reg_p (XEXP (note, 0)))
632             delete = true;
633           break;
634
635         default:
636           break;
637         }
638
639       if (delete)
640         *pnote = XEXP (note, 1);
641       else
642         pnote = &XEXP (note, 1);
643     }
644 }
645
646 /* Return whether X can be decomposed into subwords.  */
647
648 static bool
649 can_decompose_p (rtx x)
650 {
651   if (REG_P (x))
652     {
653       unsigned int regno = REGNO (x);
654
655       if (HARD_REGISTER_NUM_P (regno))
656         return (validate_subreg (word_mode, GET_MODE (x), x, UNITS_PER_WORD)
657                 && HARD_REGNO_MODE_OK (regno, word_mode));
658       else
659         return !bitmap_bit_p (non_decomposable_context, regno);
660     }
661
662   return true;
663 }
664
665 /* Decompose the registers used in a simple move SET within INSN.  If
666    we don't change anything, return INSN, otherwise return the start
667    of the sequence of moves.  */
668
669 static rtx
670 resolve_simple_move (rtx set, rtx insn)
671 {
672   rtx src, dest, real_dest, insns;
673   enum machine_mode orig_mode, dest_mode;
674   unsigned int words;
675   bool pushing;
676
677   src = SET_SRC (set);
678   dest = SET_DEST (set);
679   orig_mode = GET_MODE (dest);
680
681   words = (GET_MODE_SIZE (orig_mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
682   if (words <= 1)
683     return insn;
684
685   start_sequence ();
686
687   /* We have to handle copying from a SUBREG of a decomposed reg where
688      the SUBREG is larger than word size.  Rather than assume that we
689      can take a word_mode SUBREG of the destination, we copy to a new
690      register and then copy that to the destination.  */
691
692   real_dest = NULL_RTX;
693
694   if (GET_CODE (src) == SUBREG
695       && resolve_reg_p (SUBREG_REG (src))
696       && (SUBREG_BYTE (src) != 0
697           || (GET_MODE_SIZE (orig_mode)
698               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))))
699     {
700       real_dest = dest;
701       dest = gen_reg_rtx (orig_mode);
702       if (REG_P (real_dest))
703         REG_ATTRS (dest) = REG_ATTRS (real_dest);
704     }
705
706   /* Similarly if we are copying to a SUBREG of a decomposed reg where
707      the SUBREG is larger than word size.  */
708
709   if (GET_CODE (dest) == SUBREG
710       && resolve_reg_p (SUBREG_REG (dest))
711       && (SUBREG_BYTE (dest) != 0
712           || (GET_MODE_SIZE (orig_mode)
713               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))))
714     {
715       rtx reg, minsn, smove;
716
717       reg = gen_reg_rtx (orig_mode);
718       minsn = emit_move_insn (reg, src);
719       smove = single_set (minsn);
720       gcc_assert (smove != NULL_RTX);
721       resolve_simple_move (smove, minsn);
722       src = reg;
723     }
724
725   /* If we didn't have any big SUBREGS of decomposed registers, and
726      neither side of the move is a register we are decomposing, then
727      we don't have to do anything here.  */
728
729   if (src == SET_SRC (set)
730       && dest == SET_DEST (set)
731       && !resolve_reg_p (src)
732       && !resolve_subreg_p (src)
733       && !resolve_reg_p (dest)
734       && !resolve_subreg_p (dest))
735     {
736       end_sequence ();
737       return insn;
738     }
739
740   /* It's possible for the code to use a subreg of a decomposed
741      register while forming an address.  We need to handle that before
742      passing the address to emit_move_insn.  We pass NULL_RTX as the
743      insn parameter to resolve_subreg_use because we can not validate
744      the insn yet.  */
745   if (MEM_P (src) || MEM_P (dest))
746     {
747       int acg;
748
749       if (MEM_P (src))
750         for_each_rtx (&XEXP (src, 0), resolve_subreg_use, NULL_RTX);
751       if (MEM_P (dest))
752         for_each_rtx (&XEXP (dest, 0), resolve_subreg_use, NULL_RTX);
753       acg = apply_change_group ();
754       gcc_assert (acg);
755     }
756
757   /* If SRC is a register which we can't decompose, or has side
758      effects, we need to move via a temporary register.  */
759
760   if (!can_decompose_p (src)
761       || side_effects_p (src)
762       || GET_CODE (src) == ASM_OPERANDS)
763     {
764       rtx reg;
765
766       reg = gen_reg_rtx (orig_mode);
767       emit_move_insn (reg, src);
768       src = reg;
769     }
770
771   /* If DEST is a register which we can't decompose, or has side
772      effects, we need to first move to a temporary register.  We
773      handle the common case of pushing an operand directly.  We also
774      go through a temporary register if it holds a floating point
775      value.  This gives us better code on systems which can't move
776      data easily between integer and floating point registers.  */
777
778   dest_mode = orig_mode;
779   pushing = push_operand (dest, dest_mode);
780   if (!can_decompose_p (dest)
781       || (side_effects_p (dest) && !pushing)
782       || (!SCALAR_INT_MODE_P (dest_mode)
783           && !resolve_reg_p (dest)
784           && !resolve_subreg_p (dest)))
785     {
786       if (real_dest == NULL_RTX)
787         real_dest = dest;
788       if (!SCALAR_INT_MODE_P (dest_mode))
789         {
790           dest_mode = mode_for_size (GET_MODE_SIZE (dest_mode) * BITS_PER_UNIT,
791                                      MODE_INT, 0);
792           gcc_assert (dest_mode != BLKmode);
793         }
794       dest = gen_reg_rtx (dest_mode);
795       if (REG_P (real_dest))
796         REG_ATTRS (dest) = REG_ATTRS (real_dest);
797     }
798
799   if (pushing)
800     {
801       unsigned int i, j, jinc;
802
803       gcc_assert (GET_MODE_SIZE (orig_mode) % UNITS_PER_WORD == 0);
804       gcc_assert (GET_CODE (XEXP (dest, 0)) != PRE_MODIFY);
805       gcc_assert (GET_CODE (XEXP (dest, 0)) != POST_MODIFY);
806
807       if (WORDS_BIG_ENDIAN == STACK_GROWS_DOWNWARD)
808         {
809           j = 0;
810           jinc = 1;
811         }
812       else
813         {
814           j = words - 1;
815           jinc = -1;
816         }
817
818       for (i = 0; i < words; ++i, j += jinc)
819         {
820           rtx temp;
821
822           temp = copy_rtx (XEXP (dest, 0));
823           temp = adjust_automodify_address_nv (dest, word_mode, temp,
824                                                j * UNITS_PER_WORD);
825           emit_move_insn (temp,
826                           simplify_gen_subreg_concatn (word_mode, src,
827                                                        orig_mode,
828                                                        j * UNITS_PER_WORD));
829         }
830     }
831   else
832     {
833       unsigned int i;
834
835       if (REG_P (dest) && !HARD_REGISTER_NUM_P (REGNO (dest)))
836         emit_insn (gen_rtx_CLOBBER (VOIDmode, dest));
837
838       for (i = 0; i < words; ++i)
839         emit_move_insn (simplify_gen_subreg_concatn (word_mode, dest,
840                                                      dest_mode,
841                                                      i * UNITS_PER_WORD),
842                         simplify_gen_subreg_concatn (word_mode, src,
843                                                      orig_mode,
844                                                      i * UNITS_PER_WORD));
845     }
846
847   if (real_dest != NULL_RTX)
848     {
849       rtx mdest, minsn, smove;
850
851       if (dest_mode == orig_mode)
852         mdest = dest;
853       else
854         mdest = simplify_gen_subreg (orig_mode, dest, GET_MODE (dest), 0);
855       minsn = emit_move_insn (real_dest, mdest);
856
857       smove = single_set (minsn);
858       gcc_assert (smove != NULL_RTX);
859
860       resolve_simple_move (smove, minsn);
861     }
862
863   insns = get_insns ();
864   end_sequence ();
865
866   move_eh_region_note (insn, insns);
867
868   emit_insn_before (insns, insn);
869
870   move_libcall_note (insn, insns);
871   remove_retval_note (insn);
872   delete_insn (insn);
873
874   return insns;
875 }
876
877 /* Change a CLOBBER of a decomposed register into a CLOBBER of the
878    component registers.  Return whether we changed something.  */
879
880 static bool
881 resolve_clobber (rtx pat, rtx insn)
882 {
883   rtx reg;
884   enum machine_mode orig_mode;
885   unsigned int words, i;
886   int ret;
887
888   reg = XEXP (pat, 0);
889   if (!resolve_reg_p (reg) && !resolve_subreg_p (reg))
890     return false;
891
892   orig_mode = GET_MODE (reg);
893   words = GET_MODE_SIZE (orig_mode);
894   words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
895
896   ret = validate_change (NULL_RTX, &XEXP (pat, 0),
897                          simplify_gen_subreg_concatn (word_mode, reg,
898                                                       orig_mode, 0),
899                          0);
900   gcc_assert (ret != 0);
901
902   for (i = words - 1; i > 0; --i)
903     {
904       rtx x;
905
906       x = simplify_gen_subreg_concatn (word_mode, reg, orig_mode,
907                                        i * UNITS_PER_WORD);
908       x = gen_rtx_CLOBBER (VOIDmode, x);
909       emit_insn_after (x, insn);
910     }
911
912   return true;
913 }
914
915 /* A USE of a decomposed register is no longer meaningful.  Return
916    whether we changed something.  */
917
918 static bool
919 resolve_use (rtx pat, rtx insn)
920 {
921   if (resolve_reg_p (XEXP (pat, 0)) || resolve_subreg_p (XEXP (pat, 0)))
922     {
923       delete_insn (insn);
924       return true;
925     }
926   return false;
927 }
928
929 /* Look for registers which are always accessed via word-sized SUBREGs
930    or via copies.  Decompose these registers into several word-sized
931    pseudo-registers.  */
932
933 static void
934 decompose_multiword_subregs (bool update_life)
935 {
936   unsigned int max;
937   basic_block bb;
938
939   max = max_reg_num ();
940
941   /* First see if there are any multi-word pseudo-registers.  If there
942      aren't, there is nothing we can do.  This should speed up this
943      pass in the normal case, since it should be faster than scanning
944      all the insns.  */
945   {
946     unsigned int i;
947
948     for (i = FIRST_PSEUDO_REGISTER; i < max; ++i)
949       {
950         if (regno_reg_rtx[i] != NULL
951             && GET_MODE_SIZE (GET_MODE (regno_reg_rtx[i])) > UNITS_PER_WORD)
952           break;
953       }
954     if (i == max)
955       return;
956   }
957
958   /* FIXME: When the dataflow branch is merged, we can change this
959      code to look for each multi-word pseudo-register and to find each
960      insn which sets or uses that register.  That should be faster
961      than scanning all the insns.  */
962
963   decomposable_context = BITMAP_ALLOC (NULL);
964   non_decomposable_context = BITMAP_ALLOC (NULL);
965
966   reg_copy_graph = VEC_alloc (bitmap, heap, max);
967   VEC_safe_grow (bitmap, heap, reg_copy_graph, max);
968   memset (VEC_address (bitmap, reg_copy_graph), 0, sizeof (bitmap) * max);
969
970   FOR_EACH_BB (bb)
971     {
972       rtx insn;
973
974       FOR_BB_INSNS (bb, insn)
975         {
976           rtx set;
977           enum classify_move_insn cmi;
978           int i, n;
979
980           if (!INSN_P (insn)
981               || GET_CODE (PATTERN (insn)) == CLOBBER
982               || GET_CODE (PATTERN (insn)) == USE)
983             continue;
984
985           recog_memoized (insn);
986           extract_insn (insn);
987
988           set = simple_move (insn);
989
990           if (!set)
991             cmi = NOT_SIMPLE_MOVE;
992           else
993             {
994               bool retval;
995
996               retval = find_reg_note (insn, REG_RETVAL, NULL_RTX) != NULL_RTX;
997
998               if (find_pseudo_copy (set) && !retval)
999                 cmi = SIMPLE_PSEUDO_REG_MOVE;
1000               else if (retval
1001                        && REG_P (SET_SRC (set))
1002                        && HARD_REGISTER_P (SET_SRC (set)))
1003                 {
1004                   rtx note;
1005
1006                   /* We don't want to decompose an assignment which
1007                      copies the value returned by a libcall to a
1008                      pseudo-register.  Doing that will lose the RETVAL
1009                      note with no real gain.  */
1010                   cmi = NOT_SIMPLE_MOVE;
1011
1012                   /* If we have a RETVAL note, there should be an
1013                      EQUAL note.  We don't want to decompose any
1014                      registers which that EQUAL note refers to
1015                      directly.  If we do, we will no longer know the
1016                      value of the libcall.  */
1017                   note = find_reg_equal_equiv_note (insn);
1018                   if (note != NULL_RTX)
1019                     for_each_rtx (&XEXP (note, 0), find_decomposable_subregs,
1020                                   &cmi);
1021                 }
1022               else
1023                 cmi = SIMPLE_MOVE;
1024             }
1025
1026           n = recog_data.n_operands;
1027           for (i = 0; i < n; ++i)
1028             {
1029               for_each_rtx (&recog_data.operand[i],
1030                             find_decomposable_subregs,
1031                             &cmi);
1032
1033               /* We handle ASM_OPERANDS as a special case to support
1034                  things like x86 rdtsc which returns a DImode value.
1035                  We can decompose the output, which will certainly be
1036                  operand 0, but not the inputs.  */
1037
1038               if (cmi == SIMPLE_MOVE
1039                   && GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1040                 {
1041                   gcc_assert (i == 0);
1042                   cmi = NOT_SIMPLE_MOVE;
1043                 }
1044             }
1045         }
1046     }
1047
1048   bitmap_and_compl_into (decomposable_context, non_decomposable_context);
1049   if (!bitmap_empty_p (decomposable_context))
1050     {
1051       int hold_no_new_pseudos = no_new_pseudos;
1052       int max_regno = max_reg_num ();
1053       sbitmap life_blocks;
1054       sbitmap sub_blocks;
1055       unsigned int i;
1056       sbitmap_iterator sbi;
1057       bitmap_iterator iter;
1058       unsigned int regno;
1059
1060       propagate_pseudo_copies ();
1061
1062       no_new_pseudos = 0;
1063       life_blocks = sbitmap_alloc (last_basic_block);
1064       sbitmap_zero (life_blocks);
1065       sub_blocks = sbitmap_alloc (last_basic_block);
1066       sbitmap_zero (sub_blocks);
1067
1068       EXECUTE_IF_SET_IN_BITMAP (decomposable_context, 0, regno, iter)
1069         decompose_register (regno);
1070
1071       FOR_EACH_BB (bb)
1072         {
1073           rtx insn;
1074
1075           FOR_BB_INSNS (bb, insn)
1076             {
1077               rtx next, pat;
1078               bool changed;
1079
1080               if (!INSN_P (insn))
1081                 continue;
1082
1083               next = NEXT_INSN (insn);
1084               changed = false;
1085
1086               pat = PATTERN (insn);
1087               if (GET_CODE (pat) == CLOBBER)
1088                 {
1089                   if (resolve_clobber (pat, insn))
1090                     changed = true;
1091                 }
1092               else if (GET_CODE (pat) == USE)
1093                 {
1094                   if (resolve_use (pat, insn))
1095                     changed = true;
1096                 }
1097               else
1098                 {
1099                   rtx set;
1100                   int i;
1101
1102                   recog_memoized (insn);
1103                   extract_insn (insn);
1104
1105                   set = simple_move (insn);
1106                   if (set)
1107                     {
1108                       rtx orig_insn = insn;
1109                       bool cfi = control_flow_insn_p (insn);
1110
1111                       /* We can end up splitting loads to multi-word pseudos
1112                          into separate loads to machine word size pseudos.
1113                          When this happens, we first had one load that can
1114                          throw, and after resolve_simple_move we'll have a
1115                          bunch of loads (at least two).  All those loads may
1116                          trap if we can have non-call exceptions, so they
1117                          all will end the current basic block.  We split the
1118                          block after the outer loop over all insns, but we
1119                          make sure here that we will be able to split the
1120                          basic block and still produce the correct control
1121                          flow graph for it.  */
1122                       gcc_assert (!cfi
1123                                   || (flag_non_call_exceptions
1124                                       && can_throw_internal (insn)));
1125
1126                       insn = resolve_simple_move (set, insn);
1127                       if (insn != orig_insn)
1128                         {
1129                           changed = true;
1130
1131                           recog_memoized (insn);
1132                           extract_insn (insn);
1133
1134                           if (cfi)
1135                             SET_BIT (sub_blocks, bb->index);
1136                         }
1137                     }
1138
1139                   for (i = recog_data.n_operands - 1; i >= 0; --i)
1140                     for_each_rtx (recog_data.operand_loc[i],
1141                                   resolve_subreg_use,
1142                                   insn);
1143
1144                   resolve_reg_notes (insn);
1145
1146                   if (num_validated_changes () > 0)
1147                     {
1148                       for (i = recog_data.n_dups - 1; i >= 0; --i)
1149                         {
1150                           rtx *pl = recog_data.dup_loc[i];
1151                           int dup_num = recog_data.dup_num[i];
1152                           rtx *px = recog_data.operand_loc[dup_num];
1153
1154                           validate_change (insn, pl, *px, 1);
1155                         }
1156
1157                       i = apply_change_group ();
1158                       gcc_assert (i);
1159
1160                       changed = true;
1161                     }
1162                 }
1163
1164               if (changed)
1165                 {
1166                   SET_BIT (life_blocks, bb->index);
1167                   reg_scan_update (insn, next, max_regno);
1168                 }
1169             }
1170         }
1171
1172       no_new_pseudos = hold_no_new_pseudos;
1173
1174       if (update_life)
1175         update_life_info (life_blocks, UPDATE_LIFE_GLOBAL_RM_NOTES,
1176                           PROP_DEATH_NOTES);
1177
1178       /* If we had insns to split that caused control flow insns in the middle
1179          of a basic block, split those blocks now.  Note that we only handle
1180          the case where splitting a load has caused multiple possibly trapping
1181          loads to appear.  */
1182       EXECUTE_IF_SET_IN_SBITMAP (sub_blocks, 0, i, sbi)
1183         {
1184           rtx insn, end;
1185           edge fallthru;
1186
1187           bb = BASIC_BLOCK (i);
1188           insn = BB_HEAD (bb);
1189           end = BB_END (bb);
1190
1191           while (insn != end)
1192             {
1193               if (control_flow_insn_p (insn))
1194                 {
1195                   /* Split the block after insn.  There will be a fallthru
1196                      edge, which is OK so we keep it.  We have to create the
1197                      exception edges ourselves.  */
1198                   fallthru = split_block (bb, insn);
1199                   rtl_make_eh_edge (NULL, bb, BB_END (bb));
1200                   bb = fallthru->dest;
1201                   insn = BB_HEAD (bb);
1202                 }
1203               else
1204                 insn = NEXT_INSN (insn);
1205             }
1206         }
1207
1208       sbitmap_free (life_blocks);
1209       sbitmap_free (sub_blocks);
1210     }
1211
1212   {
1213     unsigned int i;
1214     bitmap b;
1215
1216     for (i = 0; VEC_iterate (bitmap, reg_copy_graph, i, b); ++i)
1217       if (b)
1218         BITMAP_FREE (b);
1219   }
1220
1221   VEC_free (bitmap, heap, reg_copy_graph);  
1222
1223   BITMAP_FREE (decomposable_context);
1224   BITMAP_FREE (non_decomposable_context);
1225 }
1226 \f
1227 /* Gate function for lower subreg pass.  */
1228
1229 static bool
1230 gate_handle_lower_subreg (void)
1231 {
1232   return flag_split_wide_types != 0;
1233 }
1234
1235 /* Implement first lower subreg pass.  */
1236
1237 static unsigned int
1238 rest_of_handle_lower_subreg (void)
1239 {
1240   decompose_multiword_subregs (false);
1241   return 0;
1242 }
1243
1244 /* Implement second lower subreg pass.  */
1245
1246 static unsigned int
1247 rest_of_handle_lower_subreg2 (void)
1248 {
1249   decompose_multiword_subregs (true);
1250   return 0;
1251 }
1252
1253 struct tree_opt_pass pass_lower_subreg =
1254 {
1255   "subreg",                             /* name */
1256   gate_handle_lower_subreg,             /* gate */
1257   rest_of_handle_lower_subreg,          /* execute */
1258   NULL,                                 /* sub */
1259   NULL,                                 /* next */
1260   0,                                    /* static_pass_number */
1261   TV_LOWER_SUBREG,                      /* tv_id */
1262   0,                                    /* properties_required */
1263   0,                                    /* properties_provided */
1264   0,                                    /* properties_destroyed */
1265   0,                                    /* todo_flags_start */
1266   TODO_dump_func |
1267   TODO_ggc_collect |
1268   TODO_verify_flow,                     /* todo_flags_finish */
1269   'u'                                   /* letter */
1270 };
1271
1272 struct tree_opt_pass pass_lower_subreg2 =
1273 {
1274   "subreg2",                            /* name */
1275   gate_handle_lower_subreg,             /* gate */
1276   rest_of_handle_lower_subreg2,          /* execute */
1277   NULL,                                 /* sub */
1278   NULL,                                 /* next */
1279   0,                                    /* static_pass_number */
1280   TV_LOWER_SUBREG,                      /* tv_id */
1281   0,                                    /* properties_required */
1282   0,                                    /* properties_provided */
1283   0,                                    /* properties_destroyed */
1284   0,                                    /* todo_flags_start */
1285   TODO_dump_func |
1286   TODO_ggc_collect |
1287   TODO_verify_flow,                     /* todo_flags_finish */
1288   'U'                                   /* letter */
1289 };