OSDN Git Service

* lower-subreg.c (resolve_clobber): Handle a subreg of a concatn.
[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 "regs.h"
39 #include "tree-pass.h"
40
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
47
48 DEF_VEC_P (bitmap);
49 DEF_VEC_ALLOC_P (bitmap,heap);
50
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.  */
56
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;
60
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;
64
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;
68
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.  */
72
73 static rtx
74 simple_move (rtx insn)
75 {
76   rtx x;
77   rtx set;
78   enum machine_mode mode;
79
80   if (recog_data.n_operands != 2)
81     return NULL_RTX;
82
83   set = single_set (insn);
84   if (!set)
85     return NULL_RTX;
86
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;
98
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;
110
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;
121
122   return set;
123 }
124
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.  */
128
129 static bool
130 find_pseudo_copy (rtx set)
131 {
132   rtx dest = SET_DEST (set);
133   rtx src = SET_SRC (set);
134   unsigned int rd, rs;
135   bitmap b;
136
137   if (!REG_P (dest) || !REG_P (src))
138     return false;
139
140   rd = REGNO (dest);
141   rs = REGNO (src);
142   if (HARD_REGISTER_NUM_P (rd) || HARD_REGISTER_NUM_P (rs))
143     return false;
144
145   if (GET_MODE_SIZE (GET_MODE (dest)) <= UNITS_PER_WORD)
146     return false;
147
148   b = VEC_index (bitmap, reg_copy_graph, rs);
149   if (b == NULL)
150     {
151       b = BITMAP_ALLOC (NULL);
152       VEC_replace (bitmap, reg_copy_graph, rs, b);
153     }
154
155   bitmap_set_bit (b, rd);
156
157   return true;
158 }
159
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.  */
165
166 static void
167 propagate_pseudo_copies (void)
168 {
169   bitmap queue, propagate;
170
171   queue = BITMAP_ALLOC (NULL);
172   propagate = BITMAP_ALLOC (NULL);
173
174   bitmap_copy (queue, decomposable_context);
175   do
176     {
177       bitmap_iterator iter;
178       unsigned int i;
179
180       bitmap_clear (propagate);
181
182       EXECUTE_IF_SET_IN_BITMAP (queue, 0, i, iter)
183         {
184           bitmap b = VEC_index (bitmap, reg_copy_graph, i);
185           if (b)
186             bitmap_ior_and_compl_into (propagate, b, non_decomposable_context);
187         }
188
189       bitmap_and_compl (queue, propagate, decomposable_context);
190       bitmap_ior_into (decomposable_context, propagate);
191     }
192   while (!bitmap_empty_p (queue));
193
194   BITMAP_FREE (queue);
195   BITMAP_FREE (propagate);
196 }
197
198 /* A pointer to one of these values is passed to
199    find_decomposable_subregs via for_each_rtx.  */
200
201 enum classify_move_insn
202 {
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
211 };
212
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.  */
219
220 static int
221 find_decomposable_subregs (rtx *px, void *data)
222 {
223   enum classify_move_insn *pcmi = (enum classify_move_insn *) data;
224   rtx x = *px;
225
226   if (x == NULL_RTX)
227     return 0;
228
229   if (GET_CODE (x) == SUBREG)
230     {
231       rtx inner = SUBREG_REG (x);
232       unsigned int regno, outer_size, inner_size, outer_words, inner_words;
233
234       if (!REG_P (inner))
235         return 0;
236
237       regno = REGNO (inner);
238       if (HARD_REGISTER_NUM_P (regno))
239         return -1;
240
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;
245
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.
249
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.  */
255
256       if (outer_words == 1 && inner_words > 1)
257         {
258           bitmap_set_bit (decomposable_context, regno);
259           return -1;
260         }
261     }
262   else if (GET_CODE (x) == REG)
263     {
264       unsigned int regno;
265
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.
269
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.
277
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.  */
281
282       regno = REGNO (x);
283       if (!HARD_REGISTER_NUM_P (regno)
284           && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
285         {
286           switch (*pcmi)
287             {
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 ();
299             }
300         }
301     }
302
303   return 0;
304 }
305
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.  */
310
311 static void
312 decompose_register (unsigned int regno)
313 {
314   rtx reg;
315   unsigned int words, i;
316   rtvec v;
317
318   reg = regno_reg_rtx[regno];
319
320   regno_reg_rtx[regno] = NULL_RTX;
321   clear_reg_info_regno (regno);
322
323   words = GET_MODE_SIZE (GET_MODE (reg));
324   words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
325
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);
329
330   PUT_CODE (reg, CONCATN);
331   XVEC (reg, 0) = v;
332
333   if (dump_file)
334     {
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);
339     }
340 }
341
342 /* Get a SUBREG of a CONCATN.  */
343
344 static rtx
345 simplify_subreg_concatn (enum machine_mode outermode, rtx op,
346                          unsigned int byte)
347 {
348   unsigned int inner_size;
349   enum machine_mode innermode;
350   rtx part;
351   unsigned int final_offset;
352
353   gcc_assert (GET_CODE (op) == CONCATN);
354   gcc_assert (byte % GET_MODE_SIZE (outermode) == 0);
355
356   innermode = GET_MODE (op);
357   gcc_assert (byte < GET_MODE_SIZE (innermode));
358   gcc_assert (GET_MODE_SIZE (outermode) <= GET_MODE_SIZE (innermode));
359
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;
365
366   return simplify_gen_subreg (outermode, part, GET_MODE (part), final_offset);
367 }
368
369 /* Wrapper around simplify_gen_subreg which handles CONCATN.  */
370
371 static rtx
372 simplify_gen_subreg_concatn (enum machine_mode outermode, rtx op,
373                              enum machine_mode innermode, unsigned int byte)
374 {
375   rtx ret;
376
377   /* We have to handle generating a SUBREG of a SUBREG of a CONCATN.
378      If OP is a SUBREG of a CONCATN, then it must be a simple mode
379      change with the same size and offset 0, or it must extract a
380      part.  We shouldn't see anything else here.  */
381   if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == CONCATN)
382     {
383       rtx op2;
384
385       if ((GET_MODE_SIZE (GET_MODE (op))
386            == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))))
387           && SUBREG_BYTE (op) == 0)
388         return simplify_gen_subreg_concatn (outermode, SUBREG_REG (op),
389                                             GET_MODE (SUBREG_REG (op)), byte);
390
391       op2 = simplify_subreg_concatn (GET_MODE (op), SUBREG_REG (op),
392                                      SUBREG_BYTE (op));
393       if (op2 == NULL_RTX)
394         {
395           /* We don't handle paradoxical subregs here.  */
396           gcc_assert (GET_MODE_SIZE (outermode)
397                       <= GET_MODE_SIZE (GET_MODE (op)));
398           gcc_assert (GET_MODE_SIZE (GET_MODE (op))
399                       <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))));
400           op2 = simplify_subreg_concatn (outermode, SUBREG_REG (op),
401                                          byte + SUBREG_BYTE (op));
402           gcc_assert (op2 != NULL_RTX);
403           return op2;
404         }
405
406       op = op2;
407       gcc_assert (op != NULL_RTX);
408       gcc_assert (innermode == GET_MODE (op));
409     }
410
411   if (GET_CODE (op) == CONCATN)
412     return simplify_subreg_concatn (outermode, op, byte);
413
414   ret = simplify_gen_subreg (outermode, op, innermode, byte);
415
416   /* If we see an insn like (set (reg:DI) (subreg:DI (reg:SI) 0)) then
417      resolve_simple_move will ask for the high part of the paradoxical
418      subreg, which does not have a value.  Just return a zero.  */
419   if (ret == NULL_RTX
420       && GET_CODE (op) == SUBREG
421       && SUBREG_BYTE (op) == 0
422       && (GET_MODE_SIZE (innermode)
423           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op)))))
424     return CONST0_RTX (outermode);
425
426   gcc_assert (ret != NULL_RTX);
427   return ret;
428 }
429
430 /* Return whether we should resolve X into the registers into which it
431    was decomposed.  */
432
433 static bool
434 resolve_reg_p (rtx x)
435 {
436   return GET_CODE (x) == CONCATN;
437 }
438
439 /* Return whether X is a SUBREG of a register which we need to
440    resolve.  */
441
442 static bool
443 resolve_subreg_p (rtx x)
444 {
445   if (GET_CODE (x) != SUBREG)
446     return false;
447   return resolve_reg_p (SUBREG_REG (x));
448 }
449
450 /* This is called via for_each_rtx.  Look for SUBREGs which need to be
451    decomposed.  */
452
453 static int
454 resolve_subreg_use (rtx *px, void *data)
455 {
456   rtx insn = (rtx) data;
457   rtx x = *px;
458
459   if (x == NULL_RTX)
460     return 0;
461
462   if (resolve_subreg_p (x))
463     {
464       x = simplify_subreg_concatn (GET_MODE (x), SUBREG_REG (x),
465                                    SUBREG_BYTE (x));
466
467       /* It is possible for a note to contain a reference which we can
468          decompose.  In this case, return 1 to the caller to indicate
469          that the note must be removed.  */
470       if (!x)
471         {
472           gcc_assert(!insn);
473           return 1;
474         }
475
476       validate_change (insn, px, x, 1);
477       return -1;
478     }
479
480   if (resolve_reg_p (x))
481     {
482       /* Return 1 to the caller to indicate that we found a direct
483          reference to a register which is being decomposed.  This can
484          happen inside notes.  */
485       gcc_assert (!insn);
486       return 1;
487     }
488
489   return 0;
490 }
491
492 /* If there is a REG_LIBCALL note on OLD_START, move it to NEW_START,
493    and link the corresponding REG_RETVAL note to NEW_START.  */
494
495 static void
496 move_libcall_note (rtx old_start, rtx new_start)
497 {
498   rtx note0, note1, end;
499
500   note0 = find_reg_note (old_start, REG_LIBCALL, NULL);
501   if (note0 == NULL_RTX)
502     return;
503
504   remove_note (old_start, note0);
505   end = XEXP (note0, 0);
506   note1 = find_reg_note (end, REG_RETVAL, NULL);
507
508   XEXP (note0, 1) = REG_NOTES (new_start);
509   REG_NOTES (new_start) = note0;
510   XEXP (note1, 0) = new_start;
511 }
512
513 /* Remove any REG_RETVAL note, the corresponding REG_LIBCALL note, and
514    any markers for a no-conflict block.  We have decomposed the
515    registers so the non-conflict is now obvious.  */
516
517 static void
518 remove_retval_note (rtx insn1)
519 {
520   rtx note0, insn0, note1, insn;
521
522   note1 = find_reg_note (insn1, REG_RETVAL, NULL);
523   if (note1 == NULL_RTX)
524     return;
525
526   insn0 = XEXP (note1, 0);
527   note0 = find_reg_note (insn0, REG_LIBCALL, NULL);
528
529   remove_note (insn0, note0);
530   remove_note (insn1, note1);
531
532   for (insn = insn0; insn != insn1; insn = NEXT_INSN (insn))
533     {
534       while (1)
535         {
536           rtx note;
537
538           note = find_reg_note (insn, REG_NO_CONFLICT, NULL);
539           if (note == NULL_RTX)
540             break;
541           remove_note (insn, note);
542         }
543     }
544 }
545
546 /* Resolve any decomposed registers which appear in register notes on
547    INSN.  */
548
549 static void
550 resolve_reg_notes (rtx insn)
551 {
552   rtx *pnote, note;
553
554   note = find_reg_equal_equiv_note (insn);
555   if (note)
556     {
557       if (for_each_rtx (&XEXP (note, 0), resolve_subreg_use, NULL))
558         {
559           remove_note (insn, note);
560           remove_retval_note (insn);
561         }
562     }
563
564   pnote = &REG_NOTES (insn);
565   while (*pnote != NULL_RTX)
566     {
567       bool delete = false;
568
569       note = *pnote;
570       switch (REG_NOTE_KIND (note))
571         {
572         case REG_NO_CONFLICT:
573           if (resolve_reg_p (XEXP (note, 0)))
574             delete = true;
575           break;
576
577         default:
578           break;
579         }
580
581       if (delete)
582         *pnote = XEXP (note, 1);
583       else
584         pnote = &XEXP (note, 1);
585     }
586 }
587
588 /* Return whether X can not be decomposed into subwords.  */
589
590 static bool
591 cannot_decompose_p (rtx x)
592 {
593   if (REG_P (x))
594     {
595       unsigned int regno = REGNO (x);
596
597       if (HARD_REGISTER_NUM_P (regno))
598         return !validate_subreg (word_mode, GET_MODE (x), x, UNITS_PER_WORD);
599       else
600         return bitmap_bit_p (non_decomposable_context, regno);
601     }
602
603   return false;
604 }
605
606 /* Decompose the registers used in a simple move SET within INSN.  If
607    we don't change anything, return INSN, otherwise return the start
608    of the sequence of moves.  */
609
610 static rtx
611 resolve_simple_move (rtx set, rtx insn)
612 {
613   rtx src, dest, real_dest, insns;
614   enum machine_mode orig_mode, dest_mode;
615   unsigned int words;
616   bool pushing;
617
618   src = SET_SRC (set);
619   dest = SET_DEST (set);
620   orig_mode = GET_MODE (dest);
621
622   words = (GET_MODE_SIZE (orig_mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
623   if (words <= 1)
624     return insn;
625
626   start_sequence ();
627
628   /* We have to handle copying from a SUBREG of a decomposed reg where
629      the SUBREG is larger than word size.  Rather than assume that we
630      can take a word_mode SUBREG of the destination, we copy to a new
631      register and then copy that to the destination.  */
632
633   real_dest = NULL_RTX;
634
635   if (GET_CODE (src) == SUBREG
636       && resolve_reg_p (SUBREG_REG (src))
637       && (SUBREG_BYTE (src) != 0
638           || (GET_MODE_SIZE (orig_mode)
639               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))))
640     {
641       real_dest = dest;
642       dest = gen_reg_rtx (orig_mode);
643       if (REG_P (real_dest))
644         REG_ATTRS (dest) = REG_ATTRS (real_dest);
645     }
646
647   /* Similarly if we are copying to a SUBREG of a decomposed reg where
648      the SUBREG is larger than word size.  */
649
650   if (GET_CODE (dest) == SUBREG
651       && resolve_reg_p (SUBREG_REG (dest))
652       && (SUBREG_BYTE (dest) != 0
653           || (GET_MODE_SIZE (orig_mode)
654               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))))
655     {
656       rtx reg, minsn, smove;
657
658       reg = gen_reg_rtx (orig_mode);
659       minsn = emit_move_insn (reg, src);
660       smove = single_set (minsn);
661       gcc_assert (smove != NULL_RTX);
662       resolve_simple_move (smove, minsn);
663       src = reg;
664     }
665
666   /* If we didn't have any big SUBREGS of decomposed registers, and
667      neither side of the move is a register we are decomposing, then
668      we don't have to do anything here.  */
669
670   if (src == SET_SRC (set)
671       && dest == SET_DEST (set)
672       && !resolve_reg_p (src)
673       && !resolve_subreg_p (src)
674       && !resolve_reg_p (dest)
675       && !resolve_subreg_p (dest))
676     {
677       end_sequence ();
678       return insn;
679     }
680
681   /* If SRC is a register which we can't decompose, or has side
682      effects, we need to move via a temporary register.  */
683
684   if (cannot_decompose_p (src)
685       || side_effects_p (src)
686       || GET_CODE (src) == ASM_OPERANDS)
687     {
688       rtx reg;
689
690       reg = gen_reg_rtx (orig_mode);
691       emit_move_insn (reg, src);
692       src = reg;
693     }
694
695   /* If DEST is a register which we can't decompose, or has side
696      effects, we need to first move to a temporary register.  We
697      handle the common case of pushing an operand directly.  We also
698      go through a temporary register if it holds a floating point
699      value.  This gives us better code on systems which can't move
700      data easily between integer and floating point registers.  */
701
702   dest_mode = orig_mode;
703   pushing = push_operand (dest, dest_mode);
704   if (cannot_decompose_p (dest)
705       || (side_effects_p (dest) && !pushing)
706       || (!SCALAR_INT_MODE_P (dest_mode)
707           && !resolve_reg_p (dest)
708           && !resolve_subreg_p (dest)))
709     {
710       if (real_dest == NULL_RTX)
711         real_dest = dest;
712       if (!SCALAR_INT_MODE_P (dest_mode))
713         {
714           dest_mode = mode_for_size (GET_MODE_SIZE (dest_mode) * BITS_PER_UNIT,
715                                      MODE_INT, 0);
716           gcc_assert (dest_mode != BLKmode);
717         }
718       dest = gen_reg_rtx (dest_mode);
719       if (REG_P (real_dest))
720         REG_ATTRS (dest) = REG_ATTRS (real_dest);
721     }
722
723   if (pushing)
724     {
725       unsigned int i, j, jinc;
726
727       gcc_assert (GET_MODE_SIZE (orig_mode) % UNITS_PER_WORD == 0);
728       gcc_assert (GET_CODE (XEXP (dest, 0)) != PRE_MODIFY);
729       gcc_assert (GET_CODE (XEXP (dest, 0)) != POST_MODIFY);
730
731       if (WORDS_BIG_ENDIAN == STACK_GROWS_DOWNWARD)
732         {
733           j = 0;
734           jinc = 1;
735         }
736       else
737         {
738           j = words - 1;
739           jinc = -1;
740         }
741
742       for (i = 0; i < words; ++i, j += jinc)
743         {
744           rtx temp;
745
746           temp = copy_rtx (XEXP (dest, 0));
747           temp = adjust_automodify_address_nv (dest, word_mode, temp,
748                                                j * UNITS_PER_WORD);
749           emit_move_insn (temp,
750                           simplify_gen_subreg_concatn (word_mode, src,
751                                                        orig_mode,
752                                                        j * UNITS_PER_WORD));
753         }
754     }
755   else
756     {
757       unsigned int i;
758
759       if (REG_P (dest) && !HARD_REGISTER_NUM_P (REGNO (dest)))
760         emit_insn (gen_rtx_CLOBBER (VOIDmode, dest));
761
762       for (i = 0; i < words; ++i)
763         emit_move_insn (simplify_gen_subreg_concatn (word_mode, dest,
764                                                      dest_mode,
765                                                      i * UNITS_PER_WORD),
766                         simplify_gen_subreg_concatn (word_mode, src,
767                                                      orig_mode,
768                                                      i * UNITS_PER_WORD));
769     }
770
771   if (real_dest != NULL_RTX)
772     {
773       rtx mdest, minsn, smove;
774
775       if (dest_mode == orig_mode)
776         mdest = dest;
777       else
778         mdest = simplify_gen_subreg (orig_mode, dest, GET_MODE (dest), 0);
779       minsn = emit_move_insn (real_dest, mdest);
780
781       smove = single_set (minsn);
782       gcc_assert (smove != NULL_RTX);
783
784       resolve_simple_move (smove, minsn);
785     }
786
787   insns = get_insns ();
788   end_sequence ();
789
790   emit_insn_before (insns, insn);
791
792   move_libcall_note (insn, insns);
793   remove_retval_note (insn);
794   delete_insn (insn);
795
796   return insns;
797 }
798
799 /* Change a CLOBBER of a decomposed register into a CLOBBER of the
800    component registers.  Return whether we changed something.  */
801
802 static bool
803 resolve_clobber (rtx pat, rtx insn)
804 {
805   rtx reg;
806   enum machine_mode orig_mode;
807   unsigned int words, i;
808
809   reg = XEXP (pat, 0);
810   if (!resolve_reg_p (reg) && !resolve_subreg_p (reg))
811     return false;
812
813   orig_mode = GET_MODE (reg);
814   words = GET_MODE_SIZE (orig_mode);
815   words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
816
817   XEXP (pat, 0) = simplify_gen_subreg_concatn (word_mode, reg, orig_mode, 0);
818   for (i = words - 1; i > 0; --i)
819     {
820       rtx x;
821
822       x = simplify_gen_subreg_concatn (word_mode, reg, orig_mode,
823                                        i * UNITS_PER_WORD);
824       x = gen_rtx_CLOBBER (VOIDmode, x);
825       emit_insn_after (x, insn);
826     }
827
828   return true;
829 }
830
831 /* A USE of a decomposed register is no longer meaningful.  Return
832    whether we changed something.  */
833
834 static bool
835 resolve_use (rtx pat, rtx insn)
836 {
837   if (resolve_reg_p (XEXP (pat, 0)) || resolve_subreg_p (XEXP (pat, 0)))
838     {
839       delete_insn (insn);
840       return true;
841     }
842   return false;
843 }
844
845 /* Look for registers which are always accessed via word-sized SUBREGs
846    or via copies.  Decompose these registers into several word-sized
847    pseudo-registers.  */
848
849 static void
850 decompose_multiword_subregs (bool update_life)
851 {
852   unsigned int max;
853   basic_block bb;
854
855   max = max_reg_num ();
856
857   /* First see if there are any multi-word pseudo-registers.  If there
858      aren't, there is nothing we can do.  This should speed up this
859      pass in the normal case, since it should be faster than scanning
860      all the insns.  */
861   {
862     unsigned int i;
863
864     for (i = FIRST_PSEUDO_REGISTER; i < max; ++i)
865       {
866         if (regno_reg_rtx[i] != NULL
867             && GET_MODE_SIZE (GET_MODE (regno_reg_rtx[i])) > UNITS_PER_WORD)
868           break;
869       }
870     if (i == max)
871       return;
872   }
873
874   /* FIXME: When the dataflow branch is merged, we can change this
875      code to look for each multi-word pseudo-register and to find each
876      insn which sets or uses that register.  That should be faster
877      than scanning all the insns.  */
878
879   decomposable_context = BITMAP_ALLOC (NULL);
880   non_decomposable_context = BITMAP_ALLOC (NULL);
881
882   reg_copy_graph = VEC_alloc (bitmap, heap, max);
883   VEC_safe_grow (bitmap, heap, reg_copy_graph, max);
884   memset (VEC_address (bitmap, reg_copy_graph), 0, sizeof (bitmap) * max);
885
886   FOR_EACH_BB (bb)
887     {
888       rtx insn;
889
890       FOR_BB_INSNS (bb, insn)
891         {
892           rtx set;
893           enum classify_move_insn cmi;
894           int i, n;
895
896           if (!INSN_P (insn)
897               || GET_CODE (PATTERN (insn)) == CLOBBER
898               || GET_CODE (PATTERN (insn)) == USE)
899             continue;
900
901           recog_memoized (insn);
902           extract_insn (insn);
903
904           set = simple_move (insn);
905
906           if (!set)
907             cmi = NOT_SIMPLE_MOVE;
908           else
909             {
910               bool retval;
911
912               retval = find_reg_note (insn, REG_RETVAL, NULL_RTX) != NULL_RTX;
913
914               if (find_pseudo_copy (set) && !retval)
915                 cmi = SIMPLE_PSEUDO_REG_MOVE;
916               else if (retval
917                        && REG_P (SET_SRC (set))
918                        && HARD_REGISTER_P (SET_SRC (set)))
919                 {
920                   rtx note;
921
922                   /* We don't want to decompose an assignment which
923                      copies the value returned by a libcall to a
924                      pseudo-register.  Doing that will lose the RETVAL
925                      note with no real gain.  */
926                   cmi = NOT_SIMPLE_MOVE;
927
928                   /* If we have a RETVAL note, there should be an
929                      EQUAL note.  We don't want to decompose any
930                      registers which that EQUAL note refers to
931                      directly.  If we do, we will no longer know the
932                      value of the libcall.  */
933                   note = find_reg_equal_equiv_note (insn);
934                   if (note != NULL_RTX)
935                     for_each_rtx (&XEXP (note, 0), find_decomposable_subregs,
936                                   &cmi);
937                 }
938               else
939                 cmi = SIMPLE_MOVE;
940             }
941
942           n = recog_data.n_operands;
943           for (i = 0; i < n; ++i)
944             {
945               for_each_rtx (&recog_data.operand[i],
946                             find_decomposable_subregs,
947                             &cmi);
948
949               /* We handle ASM_OPERANDS as a special case to support
950                  things like x86 rdtsc which returns a DImode value.
951                  We can decompose the output, which will certainly be
952                  operand 0, but not the inputs.  */
953
954               if (cmi == SIMPLE_MOVE
955                   && GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
956                 {
957                   gcc_assert (i == 0);
958                   cmi = NOT_SIMPLE_MOVE;
959                 }
960             }
961         }
962     }
963
964   bitmap_and_compl_into (decomposable_context, non_decomposable_context);
965   if (!bitmap_empty_p (decomposable_context))
966     {
967       int hold_no_new_pseudos = no_new_pseudos;
968       int max_regno = max_reg_num ();
969       sbitmap blocks;
970       bitmap_iterator iter;
971       unsigned int regno;
972
973       propagate_pseudo_copies ();
974
975       no_new_pseudos = 0;
976       blocks = sbitmap_alloc (last_basic_block);
977       sbitmap_zero (blocks);
978
979       EXECUTE_IF_SET_IN_BITMAP (decomposable_context, 0, regno, iter)
980         decompose_register (regno);
981
982       FOR_EACH_BB (bb)
983         {
984           rtx insn;
985
986           FOR_BB_INSNS (bb, insn)
987             {
988               rtx next, pat;
989               bool changed;
990
991               if (!INSN_P (insn))
992                 continue;
993
994               next = NEXT_INSN (insn);
995               changed = false;
996
997               pat = PATTERN (insn);
998               if (GET_CODE (pat) == CLOBBER)
999                 {
1000                   if (resolve_clobber (pat, insn))
1001                     changed = true;
1002                 }
1003               else if (GET_CODE (pat) == USE)
1004                 {
1005                   if (resolve_use (pat, insn))
1006                     changed = true;
1007                 }
1008               else
1009                 {
1010                   rtx set;
1011                   int i;
1012
1013                   recog_memoized (insn);
1014                   extract_insn (insn);
1015
1016                   set = simple_move (insn);
1017                   if (set)
1018                     {
1019                       rtx orig_insn = insn;
1020
1021                       insn = resolve_simple_move (set, insn);
1022                       if (insn != orig_insn)
1023                         {
1024                           changed = true;
1025
1026                           recog_memoized (insn);
1027                           extract_insn (insn);
1028                         }
1029                     }
1030
1031                   for (i = recog_data.n_operands - 1; i >= 0; --i)
1032                     for_each_rtx (recog_data.operand_loc[i],
1033                                   resolve_subreg_use,
1034                                   insn);
1035
1036                   resolve_reg_notes (insn);
1037
1038                   if (num_validated_changes () > 0)
1039                     {
1040                       for (i = recog_data.n_dups - 1; i >= 0; --i)
1041                         {
1042                           rtx *pl = recog_data.dup_loc[i];
1043                           int dup_num = recog_data.dup_num[i];
1044                           rtx *px = recog_data.operand_loc[dup_num];
1045
1046                           validate_change (insn, pl, *px, 1);
1047                         }
1048
1049                       i = apply_change_group ();
1050                       gcc_assert (i);
1051
1052                       changed = true;
1053                     }
1054                 }
1055
1056               if (changed)
1057                 {
1058                   SET_BIT (blocks, bb->index);
1059                   reg_scan_update (insn, next, max_regno);
1060                 }
1061             }
1062         }
1063
1064       no_new_pseudos = hold_no_new_pseudos;
1065
1066       if (update_life)
1067         update_life_info (blocks, UPDATE_LIFE_GLOBAL_RM_NOTES,
1068                           PROP_DEATH_NOTES);
1069
1070       sbitmap_free (blocks);
1071     }
1072
1073   {
1074     unsigned int i;
1075     bitmap b;
1076
1077     for (i = 0; VEC_iterate (bitmap, reg_copy_graph, i, b); ++i)
1078       if (b)
1079         BITMAP_FREE (b);
1080   }
1081
1082   VEC_free (bitmap, heap, reg_copy_graph);  
1083
1084   BITMAP_FREE (decomposable_context);
1085   BITMAP_FREE (non_decomposable_context);
1086 }
1087 \f
1088 /* Gate function for lower subreg pass.  */
1089
1090 static bool
1091 gate_handle_lower_subreg (void)
1092 {
1093   return flag_split_wide_types != 0;
1094 }
1095
1096 /* Implement first lower subreg pass.  */
1097
1098 static unsigned int
1099 rest_of_handle_lower_subreg (void)
1100 {
1101   decompose_multiword_subregs (false);
1102   return 0;
1103 }
1104
1105 /* Implement second lower subreg pass.  */
1106
1107 static unsigned int
1108 rest_of_handle_lower_subreg2 (void)
1109 {
1110   decompose_multiword_subregs (true);
1111   return 0;
1112 }
1113
1114 struct tree_opt_pass pass_lower_subreg =
1115 {
1116   "subreg",                             /* name */
1117   gate_handle_lower_subreg,             /* gate */
1118   rest_of_handle_lower_subreg,          /* 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 */
1130 };
1131
1132 struct tree_opt_pass pass_lower_subreg2 =
1133 {
1134   "subreg2",                            /* name */
1135   gate_handle_lower_subreg,             /* gate */
1136   rest_of_handle_lower_subreg2,          /* execute */
1137   NULL,                                 /* sub */
1138   NULL,                                 /* next */
1139   0,                                    /* static_pass_number */
1140   TV_LOWER_SUBREG,                      /* tv_id */
1141   0,                                    /* properties_required */
1142   0,                                    /* properties_provided */
1143   0,                                    /* properties_destroyed */
1144   0,                                    /* todo_flags_start */
1145   TODO_dump_func |
1146   TODO_ggc_collect,                     /* todo_flags_finish */
1147   'U'                                   /* letter */
1148 };