OSDN Git Service

* gcc.dg/vect/costmodel/spu/costmodel-vect-iv-9.c: Add noinline
[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 3, 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 COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "machmode.h"
26 #include "tm.h"
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "timevar.h"
30 #include "flags.h"
31 #include "insn-config.h"
32 #include "obstack.h"
33 #include "basic-block.h"
34 #include "recog.h"
35 #include "bitmap.h"
36 #include "expr.h"
37 #include "except.h"
38 #include "regs.h"
39 #include "tree-pass.h"
40 #include "df.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.  */
229   SIMPLE_PSEUDO_REG_MOVE,
230   /* A simple move involving a non-pseudo-register.  */
231   SIMPLE_MOVE
232 };
233
234 /* This is called via for_each_rtx.  If we find a SUBREG which we
235    could use to decompose a pseudo-register, set a bit in
236    DECOMPOSABLE_CONTEXT.  If we find an unadorned register which is
237    not a simple pseudo-register copy, DATA will point at the type of
238    move, and we set a bit in DECOMPOSABLE_CONTEXT or
239    NON_DECOMPOSABLE_CONTEXT as appropriate.  */
240
241 static int
242 find_decomposable_subregs (rtx *px, void *data)
243 {
244   enum classify_move_insn *pcmi = (enum classify_move_insn *) data;
245   rtx x = *px;
246
247   if (x == NULL_RTX)
248     return 0;
249
250   if (GET_CODE (x) == SUBREG)
251     {
252       rtx inner = SUBREG_REG (x);
253       unsigned int regno, outer_size, inner_size, outer_words, inner_words;
254
255       if (!REG_P (inner))
256         return 0;
257
258       regno = REGNO (inner);
259       if (HARD_REGISTER_NUM_P (regno))
260         return -1;
261
262       outer_size = GET_MODE_SIZE (GET_MODE (x));
263       inner_size = GET_MODE_SIZE (GET_MODE (inner));
264       outer_words = (outer_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
265       inner_words = (inner_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
266
267       /* We only try to decompose single word subregs of multi-word
268          registers.  When we find one, we return -1 to avoid iterating
269          over the inner register.
270
271          ??? This doesn't allow, e.g., DImode subregs of TImode values
272          on 32-bit targets.  We would need to record the way the
273          pseudo-register was used, and only decompose if all the uses
274          were the same number and size of pieces.  Hopefully this
275          doesn't happen much.  */
276
277       if (outer_words == 1 && inner_words > 1)
278         {
279           bitmap_set_bit (decomposable_context, regno);
280           return -1;
281         }
282
283       /* If this is a cast from one mode to another, where the modes
284          have the same size, and they are not tieable, then mark this
285          register as non-decomposable.  If we decompose it we are
286          likely to mess up whatever the backend is trying to do.  */
287       if (outer_words > 1
288           && outer_size == inner_size
289           && !MODES_TIEABLE_P (GET_MODE (x), GET_MODE (inner)))
290         {
291           bitmap_set_bit (non_decomposable_context, regno);
292           return -1;
293         }
294     }
295   else if (REG_P (x))
296     {
297       unsigned int regno;
298
299       /* We will see an outer SUBREG before we see the inner REG, so
300          when we see a plain REG here it means a direct reference to
301          the register.
302
303          If this is not a simple copy from one location to another,
304          then we can not decompose this register.  If this is a simple
305          copy from one pseudo-register to another, and the mode is right
306          then we mark the register as decomposable.
307          Otherwise we don't say anything about this register --
308          it could be decomposed, but whether that would be
309          profitable depends upon how it is used elsewhere.
310
311          We only set bits in the bitmap for multi-word
312          pseudo-registers, since those are the only ones we care about
313          and it keeps the size of the bitmaps down.  */
314
315       regno = REGNO (x);
316       if (!HARD_REGISTER_NUM_P (regno)
317           && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
318         {
319           switch (*pcmi)
320             {
321             case NOT_SIMPLE_MOVE:
322               bitmap_set_bit (non_decomposable_context, regno);
323               break;
324             case SIMPLE_PSEUDO_REG_MOVE:
325               if (MODES_TIEABLE_P (GET_MODE (x), word_mode))
326                 bitmap_set_bit (decomposable_context, regno);
327               break;
328             case SIMPLE_MOVE:
329               break;
330             default:
331               gcc_unreachable ();
332             }
333         }
334     }
335   else if (MEM_P (x))
336     {
337       enum classify_move_insn cmi_mem = NOT_SIMPLE_MOVE;
338
339       /* Any registers used in a MEM do not participate in a
340          SIMPLE_MOVE or SIMPLE_PSEUDO_REG_MOVE.  Do our own recursion
341          here, and return -1 to block the parent's recursion.  */
342       for_each_rtx (&XEXP (x, 0), find_decomposable_subregs, &cmi_mem);
343       return -1;
344     }
345
346   return 0;
347 }
348
349 /* Decompose REGNO into word-sized components.  We smash the REG node
350    in place.  This ensures that (1) something goes wrong quickly if we
351    fail to make some replacement, and (2) the debug information inside
352    the symbol table is automatically kept up to date.  */
353
354 static void
355 decompose_register (unsigned int regno)
356 {
357   rtx reg;
358   unsigned int words, i;
359   rtvec v;
360
361   reg = regno_reg_rtx[regno];
362
363   regno_reg_rtx[regno] = NULL_RTX;
364
365   words = GET_MODE_SIZE (GET_MODE (reg));
366   words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
367
368   v = rtvec_alloc (words);
369   for (i = 0; i < words; ++i)
370     RTVEC_ELT (v, i) = gen_reg_rtx_offset (reg, word_mode, i * UNITS_PER_WORD);
371
372   PUT_CODE (reg, CONCATN);
373   XVEC (reg, 0) = v;
374
375   if (dump_file)
376     {
377       fprintf (dump_file, "; Splitting reg %u ->", regno);
378       for (i = 0; i < words; ++i)
379         fprintf (dump_file, " %u", REGNO (XVECEXP (reg, 0, i)));
380       fputc ('\n', dump_file);
381     }
382 }
383
384 /* Get a SUBREG of a CONCATN.  */
385
386 static rtx
387 simplify_subreg_concatn (enum machine_mode outermode, rtx op,
388                          unsigned int byte)
389 {
390   unsigned int inner_size;
391   enum machine_mode innermode;
392   rtx part;
393   unsigned int final_offset;
394
395   gcc_assert (GET_CODE (op) == CONCATN);
396   gcc_assert (byte % GET_MODE_SIZE (outermode) == 0);
397
398   innermode = GET_MODE (op);
399   gcc_assert (byte < GET_MODE_SIZE (innermode));
400   gcc_assert (GET_MODE_SIZE (outermode) <= GET_MODE_SIZE (innermode));
401
402   inner_size = GET_MODE_SIZE (innermode) / XVECLEN (op, 0);
403   part = XVECEXP (op, 0, byte / inner_size);
404   final_offset = byte % inner_size;
405   if (final_offset + GET_MODE_SIZE (outermode) > inner_size)
406     return NULL_RTX;
407
408   return simplify_gen_subreg (outermode, part, GET_MODE (part), final_offset);
409 }
410
411 /* Wrapper around simplify_gen_subreg which handles CONCATN.  */
412
413 static rtx
414 simplify_gen_subreg_concatn (enum machine_mode outermode, rtx op,
415                              enum machine_mode innermode, unsigned int byte)
416 {
417   rtx ret;
418
419   /* We have to handle generating a SUBREG of a SUBREG of a CONCATN.
420      If OP is a SUBREG of a CONCATN, then it must be a simple mode
421      change with the same size and offset 0, or it must extract a
422      part.  We shouldn't see anything else here.  */
423   if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == CONCATN)
424     {
425       rtx op2;
426
427       if ((GET_MODE_SIZE (GET_MODE (op))
428            == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))))
429           && SUBREG_BYTE (op) == 0)
430         return simplify_gen_subreg_concatn (outermode, SUBREG_REG (op),
431                                             GET_MODE (SUBREG_REG (op)), byte);
432
433       op2 = simplify_subreg_concatn (GET_MODE (op), SUBREG_REG (op),
434                                      SUBREG_BYTE (op));
435       if (op2 == NULL_RTX)
436         {
437           /* We don't handle paradoxical subregs here.  */
438           gcc_assert (GET_MODE_SIZE (outermode)
439                       <= GET_MODE_SIZE (GET_MODE (op)));
440           gcc_assert (GET_MODE_SIZE (GET_MODE (op))
441                       <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))));
442           op2 = simplify_subreg_concatn (outermode, SUBREG_REG (op),
443                                          byte + SUBREG_BYTE (op));
444           gcc_assert (op2 != NULL_RTX);
445           return op2;
446         }
447
448       op = op2;
449       gcc_assert (op != NULL_RTX);
450       gcc_assert (innermode == GET_MODE (op));
451     }
452
453   if (GET_CODE (op) == CONCATN)
454     return simplify_subreg_concatn (outermode, op, byte);
455
456   ret = simplify_gen_subreg (outermode, op, innermode, byte);
457
458   /* If we see an insn like (set (reg:DI) (subreg:DI (reg:SI) 0)) then
459      resolve_simple_move will ask for the high part of the paradoxical
460      subreg, which does not have a value.  Just return a zero.  */
461   if (ret == NULL_RTX
462       && GET_CODE (op) == SUBREG
463       && SUBREG_BYTE (op) == 0
464       && (GET_MODE_SIZE (innermode)
465           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op)))))
466     return CONST0_RTX (outermode);
467
468   gcc_assert (ret != NULL_RTX);
469   return ret;
470 }
471
472 /* Return whether we should resolve X into the registers into which it
473    was decomposed.  */
474
475 static bool
476 resolve_reg_p (rtx x)
477 {
478   return GET_CODE (x) == CONCATN;
479 }
480
481 /* Return whether X is a SUBREG of a register which we need to
482    resolve.  */
483
484 static bool
485 resolve_subreg_p (rtx x)
486 {
487   if (GET_CODE (x) != SUBREG)
488     return false;
489   return resolve_reg_p (SUBREG_REG (x));
490 }
491
492 /* This is called via for_each_rtx.  Look for SUBREGs which need to be
493    decomposed.  */
494
495 static int
496 resolve_subreg_use (rtx *px, void *data)
497 {
498   rtx insn = (rtx) data;
499   rtx x = *px;
500
501   if (x == NULL_RTX)
502     return 0;
503
504   if (resolve_subreg_p (x))
505     {
506       x = simplify_subreg_concatn (GET_MODE (x), SUBREG_REG (x),
507                                    SUBREG_BYTE (x));
508
509       /* It is possible for a note to contain a reference which we can
510          decompose.  In this case, return 1 to the caller to indicate
511          that the note must be removed.  */
512       if (!x)
513         {
514           gcc_assert (!insn);
515           return 1;
516         }
517
518       validate_change (insn, px, x, 1);
519       return -1;
520     }
521
522   if (resolve_reg_p (x))
523     {
524       /* Return 1 to the caller to indicate that we found a direct
525          reference to a register which is being decomposed.  This can
526          happen inside notes, multiword shift or zero-extend
527          instructions.  */
528       return 1;
529     }
530
531   return 0;
532 }
533
534 /* We are deleting INSN.  Move any EH_REGION notes to INSNS.  */
535
536 static void
537 move_eh_region_note (rtx insn, rtx insns)
538 {
539   rtx note, p;
540
541   note = find_reg_note (insn, REG_EH_REGION, NULL_RTX);
542   if (note == NULL_RTX)
543     return;
544
545   gcc_assert (CALL_P (insn)
546               || (flag_non_call_exceptions && may_trap_p (PATTERN (insn))));
547
548   for (p = insns; p != NULL_RTX; p = NEXT_INSN (p))
549     {
550       if (CALL_P (p)
551           || (flag_non_call_exceptions
552               && INSN_P (p)
553               && may_trap_p (PATTERN (p))))
554         REG_NOTES (p) = gen_rtx_EXPR_LIST (REG_EH_REGION, XEXP (note, 0),
555                                            REG_NOTES (p));
556     }
557 }
558
559 /* Resolve any decomposed registers which appear in register notes on
560    INSN.  */
561
562 static void
563 resolve_reg_notes (rtx insn)
564 {
565   rtx *pnote, note;
566
567   note = find_reg_equal_equiv_note (insn);
568   if (note)
569     {
570       int old_count = num_validated_changes ();
571       if (for_each_rtx (&XEXP (note, 0), resolve_subreg_use, NULL))
572         remove_note (insn, note);
573       else
574         if (old_count != num_validated_changes ())
575           df_notes_rescan (insn);
576     }
577
578   pnote = &REG_NOTES (insn);
579   while (*pnote != NULL_RTX)
580     {
581       bool delete = false;
582
583       note = *pnote;
584       switch (REG_NOTE_KIND (note))
585         {
586         case REG_DEAD:
587         case REG_UNUSED:
588           if (resolve_reg_p (XEXP (note, 0)))
589             delete = true;
590           break;
591
592         default:
593           break;
594         }
595
596       if (delete)
597         *pnote = XEXP (note, 1);
598       else
599         pnote = &XEXP (note, 1);
600     }
601 }
602
603 /* Return whether X can be decomposed into subwords.  */
604
605 static bool
606 can_decompose_p (rtx x)
607 {
608   if (REG_P (x))
609     {
610       unsigned int regno = REGNO (x);
611
612       if (HARD_REGISTER_NUM_P (regno))
613         return (validate_subreg (word_mode, GET_MODE (x), x, UNITS_PER_WORD)
614                 && HARD_REGNO_MODE_OK (regno, word_mode));
615       else
616         return !bitmap_bit_p (non_decomposable_context, regno);
617     }
618
619   return true;
620 }
621
622 /* Decompose the registers used in a simple move SET within INSN.  If
623    we don't change anything, return INSN, otherwise return the start
624    of the sequence of moves.  */
625
626 static rtx
627 resolve_simple_move (rtx set, rtx insn)
628 {
629   rtx src, dest, real_dest, insns;
630   enum machine_mode orig_mode, dest_mode;
631   unsigned int words;
632   bool pushing;
633
634   src = SET_SRC (set);
635   dest = SET_DEST (set);
636   orig_mode = GET_MODE (dest);
637
638   words = (GET_MODE_SIZE (orig_mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
639   if (words <= 1)
640     return insn;
641
642   start_sequence ();
643
644   /* We have to handle copying from a SUBREG of a decomposed reg where
645      the SUBREG is larger than word size.  Rather than assume that we
646      can take a word_mode SUBREG of the destination, we copy to a new
647      register and then copy that to the destination.  */
648
649   real_dest = NULL_RTX;
650
651   if (GET_CODE (src) == SUBREG
652       && resolve_reg_p (SUBREG_REG (src))
653       && (SUBREG_BYTE (src) != 0
654           || (GET_MODE_SIZE (orig_mode)
655               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))))
656     {
657       real_dest = dest;
658       dest = gen_reg_rtx (orig_mode);
659       if (REG_P (real_dest))
660         REG_ATTRS (dest) = REG_ATTRS (real_dest);
661     }
662
663   /* Similarly if we are copying to a SUBREG of a decomposed reg where
664      the SUBREG is larger than word size.  */
665
666   if (GET_CODE (dest) == SUBREG
667       && resolve_reg_p (SUBREG_REG (dest))
668       && (SUBREG_BYTE (dest) != 0
669           || (GET_MODE_SIZE (orig_mode)
670               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))))
671     {
672       rtx reg, minsn, smove;
673
674       reg = gen_reg_rtx (orig_mode);
675       minsn = emit_move_insn (reg, src);
676       smove = single_set (minsn);
677       gcc_assert (smove != NULL_RTX);
678       resolve_simple_move (smove, minsn);
679       src = reg;
680     }
681
682   /* If we didn't have any big SUBREGS of decomposed registers, and
683      neither side of the move is a register we are decomposing, then
684      we don't have to do anything here.  */
685
686   if (src == SET_SRC (set)
687       && dest == SET_DEST (set)
688       && !resolve_reg_p (src)
689       && !resolve_subreg_p (src)
690       && !resolve_reg_p (dest)
691       && !resolve_subreg_p (dest))
692     {
693       end_sequence ();
694       return insn;
695     }
696
697   /* It's possible for the code to use a subreg of a decomposed
698      register while forming an address.  We need to handle that before
699      passing the address to emit_move_insn.  We pass NULL_RTX as the
700      insn parameter to resolve_subreg_use because we can not validate
701      the insn yet.  */
702   if (MEM_P (src) || MEM_P (dest))
703     {
704       int acg;
705
706       if (MEM_P (src))
707         for_each_rtx (&XEXP (src, 0), resolve_subreg_use, NULL_RTX);
708       if (MEM_P (dest))
709         for_each_rtx (&XEXP (dest, 0), resolve_subreg_use, NULL_RTX);
710       acg = apply_change_group ();
711       gcc_assert (acg);
712     }
713
714   /* If SRC is a register which we can't decompose, or has side
715      effects, we need to move via a temporary register.  */
716
717   if (!can_decompose_p (src)
718       || side_effects_p (src)
719       || GET_CODE (src) == ASM_OPERANDS)
720     {
721       rtx reg;
722
723       reg = gen_reg_rtx (orig_mode);
724       emit_move_insn (reg, src);
725       src = reg;
726     }
727
728   /* If DEST is a register which we can't decompose, or has side
729      effects, we need to first move to a temporary register.  We
730      handle the common case of pushing an operand directly.  We also
731      go through a temporary register if it holds a floating point
732      value.  This gives us better code on systems which can't move
733      data easily between integer and floating point registers.  */
734
735   dest_mode = orig_mode;
736   pushing = push_operand (dest, dest_mode);
737   if (!can_decompose_p (dest)
738       || (side_effects_p (dest) && !pushing)
739       || (!SCALAR_INT_MODE_P (dest_mode)
740           && !resolve_reg_p (dest)
741           && !resolve_subreg_p (dest)))
742     {
743       if (real_dest == NULL_RTX)
744         real_dest = dest;
745       if (!SCALAR_INT_MODE_P (dest_mode))
746         {
747           dest_mode = mode_for_size (GET_MODE_SIZE (dest_mode) * BITS_PER_UNIT,
748                                      MODE_INT, 0);
749           gcc_assert (dest_mode != BLKmode);
750         }
751       dest = gen_reg_rtx (dest_mode);
752       if (REG_P (real_dest))
753         REG_ATTRS (dest) = REG_ATTRS (real_dest);
754     }
755
756   if (pushing)
757     {
758       unsigned int i, j, jinc;
759
760       gcc_assert (GET_MODE_SIZE (orig_mode) % UNITS_PER_WORD == 0);
761       gcc_assert (GET_CODE (XEXP (dest, 0)) != PRE_MODIFY);
762       gcc_assert (GET_CODE (XEXP (dest, 0)) != POST_MODIFY);
763
764       if (WORDS_BIG_ENDIAN == STACK_GROWS_DOWNWARD)
765         {
766           j = 0;
767           jinc = 1;
768         }
769       else
770         {
771           j = words - 1;
772           jinc = -1;
773         }
774
775       for (i = 0; i < words; ++i, j += jinc)
776         {
777           rtx temp;
778
779           temp = copy_rtx (XEXP (dest, 0));
780           temp = adjust_automodify_address_nv (dest, word_mode, temp,
781                                                j * UNITS_PER_WORD);
782           emit_move_insn (temp,
783                           simplify_gen_subreg_concatn (word_mode, src,
784                                                        orig_mode,
785                                                        j * UNITS_PER_WORD));
786         }
787     }
788   else
789     {
790       unsigned int i;
791
792       if (REG_P (dest) && !HARD_REGISTER_NUM_P (REGNO (dest)))
793         emit_clobber (dest);
794
795       for (i = 0; i < words; ++i)
796         emit_move_insn (simplify_gen_subreg_concatn (word_mode, dest,
797                                                      dest_mode,
798                                                      i * UNITS_PER_WORD),
799                         simplify_gen_subreg_concatn (word_mode, src,
800                                                      orig_mode,
801                                                      i * UNITS_PER_WORD));
802     }
803
804   if (real_dest != NULL_RTX)
805     {
806       rtx mdest, minsn, smove;
807
808       if (dest_mode == orig_mode)
809         mdest = dest;
810       else
811         mdest = simplify_gen_subreg (orig_mode, dest, GET_MODE (dest), 0);
812       minsn = emit_move_insn (real_dest, mdest);
813
814       smove = single_set (minsn);
815       gcc_assert (smove != NULL_RTX);
816
817       resolve_simple_move (smove, minsn);
818     }
819
820   insns = get_insns ();
821   end_sequence ();
822
823   move_eh_region_note (insn, insns);
824
825   emit_insn_before (insns, insn);
826
827   delete_insn (insn);
828
829   return insns;
830 }
831
832 /* Change a CLOBBER of a decomposed register into a CLOBBER of the
833    component registers.  Return whether we changed something.  */
834
835 static bool
836 resolve_clobber (rtx pat, rtx insn)
837 {
838   rtx reg;
839   enum machine_mode orig_mode;
840   unsigned int words, i;
841   int ret;
842
843   reg = XEXP (pat, 0);
844   if (!resolve_reg_p (reg) && !resolve_subreg_p (reg))
845     return false;
846
847   orig_mode = GET_MODE (reg);
848   words = GET_MODE_SIZE (orig_mode);
849   words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
850
851   ret = validate_change (NULL_RTX, &XEXP (pat, 0),
852                          simplify_gen_subreg_concatn (word_mode, reg,
853                                                       orig_mode, 0),
854                          0);
855   df_insn_rescan (insn);
856   gcc_assert (ret != 0);
857
858   for (i = words - 1; i > 0; --i)
859     {
860       rtx x;
861
862       x = simplify_gen_subreg_concatn (word_mode, reg, orig_mode,
863                                        i * UNITS_PER_WORD);
864       x = gen_rtx_CLOBBER (VOIDmode, x);
865       emit_insn_after (x, insn);
866     }
867
868   resolve_reg_notes (insn);
869
870   return true;
871 }
872
873 /* A USE of a decomposed register is no longer meaningful.  Return
874    whether we changed something.  */
875
876 static bool
877 resolve_use (rtx pat, rtx insn)
878 {
879   if (resolve_reg_p (XEXP (pat, 0)) || resolve_subreg_p (XEXP (pat, 0)))
880     {
881       delete_insn (insn);
882       return true;
883     }
884
885   resolve_reg_notes (insn);
886
887   return false;
888 }
889
890 /* Checks if INSN is a decomposable multiword-shift or zero-extend and
891    sets the decomposable_context bitmap accordingly.  A non-zero value
892    is returned if a decomposable insn has been found.  */
893
894 static int
895 find_decomposable_shift_zext (rtx insn)
896 {
897   rtx set;
898   rtx op;
899   rtx op_operand;
900
901   set = single_set (insn);
902   if (!set)
903     return 0;
904
905   op = SET_SRC (set);
906   if (GET_CODE (op) != ASHIFT
907       && GET_CODE (op) != LSHIFTRT
908       && GET_CODE (op) != ZERO_EXTEND)
909     return 0;
910
911   op_operand = XEXP (op, 0);
912   if (!REG_P (SET_DEST (set)) || !REG_P (op_operand)
913       || HARD_REGISTER_NUM_P (REGNO (SET_DEST (set)))
914       || HARD_REGISTER_NUM_P (REGNO (op_operand))
915       || !SCALAR_INT_MODE_P (GET_MODE (op)))
916     return 0;
917
918   if (GET_CODE (op) == ZERO_EXTEND)
919     {
920       if (GET_MODE (op_operand) != word_mode
921           || GET_MODE_BITSIZE (GET_MODE (op)) != 2 * BITS_PER_WORD)
922         return 0;
923     }
924   else /* left or right shift */
925     {
926       if (GET_CODE (XEXP (op, 1)) != CONST_INT
927           || INTVAL (XEXP (op, 1)) < BITS_PER_WORD
928           || GET_MODE_BITSIZE (GET_MODE (op_operand)) != 2 * BITS_PER_WORD)
929         return 0;
930     }
931
932   bitmap_set_bit (decomposable_context, REGNO (SET_DEST (set)));
933
934   if (GET_CODE (op) != ZERO_EXTEND)
935     bitmap_set_bit (decomposable_context, REGNO (op_operand));
936
937   return 1;
938 }
939
940 /* Decompose a more than word wide shift (in INSN) of a multiword
941    pseudo or a multiword zero-extend of a wordmode pseudo into a move
942    and 'set to zero' insn.  Return a pointer to the new insn when a
943    replacement was done.  */
944
945 static rtx
946 resolve_shift_zext (rtx insn)
947 {
948   rtx set;
949   rtx op;
950   rtx op_operand;
951   rtx insns;
952   rtx src_reg, dest_reg, dest_zero;
953   int src_reg_num, dest_reg_num, offset1, offset2, src_offset;
954
955   set = single_set (insn);
956   if (!set)
957     return NULL_RTX;
958
959   op = SET_SRC (set);
960   if (GET_CODE (op) != ASHIFT
961       && GET_CODE (op) != LSHIFTRT
962       && GET_CODE (op) != ZERO_EXTEND)
963     return NULL_RTX;
964
965   op_operand = XEXP (op, 0);
966
967   if (!resolve_reg_p (SET_DEST (set)) && !resolve_reg_p (op_operand))
968     return NULL_RTX;
969
970   /* src_reg_num is the number of the word mode register which we
971      are operating on.  For a left shift and a zero_extend on little
972      endian machines this is register 0.  */
973   src_reg_num = GET_CODE (op) == LSHIFTRT ? 1 : 0;
974
975   if (WORDS_BIG_ENDIAN
976       && GET_MODE_SIZE (GET_MODE (op_operand)) > UNITS_PER_WORD)
977     src_reg_num = 1 - src_reg_num;
978
979   if (GET_CODE (op) == ZERO_EXTEND)
980     dest_reg_num = WORDS_BIG_ENDIAN ? 1 : 0;
981   else
982     dest_reg_num = 1 - src_reg_num;
983
984   offset1 = UNITS_PER_WORD * dest_reg_num;
985   offset2 = UNITS_PER_WORD * (1 - dest_reg_num);
986   src_offset = UNITS_PER_WORD * src_reg_num;
987
988   if (WORDS_BIG_ENDIAN != BYTES_BIG_ENDIAN)
989     {
990       offset1 += UNITS_PER_WORD - 1;
991       offset2 += UNITS_PER_WORD - 1;
992       src_offset += UNITS_PER_WORD - 1;
993     }
994
995   start_sequence ();
996
997   dest_reg = simplify_gen_subreg_concatn (word_mode, SET_DEST (set),
998                                           GET_MODE (SET_DEST (set)),
999                                           offset1);
1000   dest_zero = simplify_gen_subreg_concatn (word_mode, SET_DEST (set),
1001                                            GET_MODE (SET_DEST (set)),
1002                                            offset2);
1003   src_reg = simplify_gen_subreg_concatn (word_mode, op_operand,
1004                                          GET_MODE (op_operand),
1005                                          src_offset);
1006   if (GET_CODE (op) != ZERO_EXTEND)
1007     {
1008       int shift_count = INTVAL (XEXP (op, 1));
1009       if (shift_count > BITS_PER_WORD)
1010         src_reg = expand_shift (GET_CODE (op) == ASHIFT ?
1011                                 LSHIFT_EXPR : RSHIFT_EXPR,
1012                                 word_mode, src_reg,
1013                                 build_int_cst (NULL_TREE,
1014                                                shift_count - BITS_PER_WORD),
1015                                 dest_reg, 1);
1016     }
1017
1018   if (dest_reg != src_reg)
1019     emit_move_insn (dest_reg, src_reg);
1020   emit_move_insn (dest_zero, CONST0_RTX (word_mode));
1021   insns = get_insns ();
1022
1023   end_sequence ();
1024
1025   emit_insn_before (insns, insn);
1026
1027   if (dump_file)
1028     {
1029       rtx in;
1030       fprintf (dump_file, "; Replacing insn: %d with insns: ", INSN_UID (insn));
1031       for (in = insns; in != insn; in = NEXT_INSN (in))
1032         fprintf (dump_file, "%d ", INSN_UID (in));
1033       fprintf (dump_file, "\n");
1034     }
1035
1036   delete_insn (insn);
1037   return insns;
1038 }
1039
1040 /* Look for registers which are always accessed via word-sized SUBREGs
1041    or via copies.  Decompose these registers into several word-sized
1042    pseudo-registers.  */
1043
1044 static void
1045 decompose_multiword_subregs (void)
1046 {
1047   unsigned int max;
1048   basic_block bb;
1049
1050   if (df)
1051     df_set_flags (DF_DEFER_INSN_RESCAN);
1052
1053   max = max_reg_num ();
1054
1055   /* First see if there are any multi-word pseudo-registers.  If there
1056      aren't, there is nothing we can do.  This should speed up this
1057      pass in the normal case, since it should be faster than scanning
1058      all the insns.  */
1059   {
1060     unsigned int i;
1061
1062     for (i = FIRST_PSEUDO_REGISTER; i < max; ++i)
1063       {
1064         if (regno_reg_rtx[i] != NULL
1065             && GET_MODE_SIZE (GET_MODE (regno_reg_rtx[i])) > UNITS_PER_WORD)
1066           break;
1067       }
1068     if (i == max)
1069       return;
1070   }
1071
1072   /* FIXME: When the dataflow branch is merged, we can change this
1073      code to look for each multi-word pseudo-register and to find each
1074      insn which sets or uses that register.  That should be faster
1075      than scanning all the insns.  */
1076
1077   decomposable_context = BITMAP_ALLOC (NULL);
1078   non_decomposable_context = BITMAP_ALLOC (NULL);
1079
1080   reg_copy_graph = VEC_alloc (bitmap, heap, max);
1081   VEC_safe_grow (bitmap, heap, reg_copy_graph, max);
1082   memset (VEC_address (bitmap, reg_copy_graph), 0, sizeof (bitmap) * max);
1083
1084   FOR_EACH_BB (bb)
1085     {
1086       rtx insn;
1087
1088       FOR_BB_INSNS (bb, insn)
1089         {
1090           rtx set;
1091           enum classify_move_insn cmi;
1092           int i, n;
1093
1094           if (!INSN_P (insn)
1095               || GET_CODE (PATTERN (insn)) == CLOBBER
1096               || GET_CODE (PATTERN (insn)) == USE)
1097             continue;
1098
1099           if (find_decomposable_shift_zext (insn))
1100             continue;
1101
1102           recog_memoized (insn);
1103           extract_insn (insn);
1104
1105           set = simple_move (insn);
1106
1107           if (!set)
1108             cmi = NOT_SIMPLE_MOVE;
1109           else
1110             {
1111               if (find_pseudo_copy (set))
1112                 cmi = SIMPLE_PSEUDO_REG_MOVE;
1113               else
1114                 cmi = SIMPLE_MOVE;
1115             }
1116
1117           n = recog_data.n_operands;
1118           for (i = 0; i < n; ++i)
1119             {
1120               for_each_rtx (&recog_data.operand[i],
1121                             find_decomposable_subregs,
1122                             &cmi);
1123
1124               /* We handle ASM_OPERANDS as a special case to support
1125                  things like x86 rdtsc which returns a DImode value.
1126                  We can decompose the output, which will certainly be
1127                  operand 0, but not the inputs.  */
1128
1129               if (cmi == SIMPLE_MOVE
1130                   && GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1131                 {
1132                   gcc_assert (i == 0);
1133                   cmi = NOT_SIMPLE_MOVE;
1134                 }
1135             }
1136         }
1137     }
1138
1139   bitmap_and_compl_into (decomposable_context, non_decomposable_context);
1140   if (!bitmap_empty_p (decomposable_context))
1141     {
1142       sbitmap sub_blocks;
1143       unsigned int i;
1144       sbitmap_iterator sbi;
1145       bitmap_iterator iter;
1146       unsigned int regno;
1147
1148       propagate_pseudo_copies ();
1149
1150       sub_blocks = sbitmap_alloc (last_basic_block);
1151       sbitmap_zero (sub_blocks);
1152
1153       EXECUTE_IF_SET_IN_BITMAP (decomposable_context, 0, regno, iter)
1154         decompose_register (regno);
1155
1156       FOR_EACH_BB (bb)
1157         {
1158           rtx insn;
1159
1160           FOR_BB_INSNS (bb, insn)
1161             {
1162               rtx next, pat;
1163
1164               if (!INSN_P (insn))
1165                 continue;
1166
1167               next = NEXT_INSN (insn);
1168
1169               pat = PATTERN (insn);
1170               if (GET_CODE (pat) == CLOBBER)
1171                 resolve_clobber (pat, insn);
1172               else if (GET_CODE (pat) == USE)
1173                 resolve_use (pat, insn);
1174               else
1175                 {
1176                   rtx set;
1177                   int i;
1178
1179                   recog_memoized (insn);
1180                   extract_insn (insn);
1181
1182                   set = simple_move (insn);
1183                   if (set)
1184                     {
1185                       rtx orig_insn = insn;
1186                       bool cfi = control_flow_insn_p (insn);
1187
1188                       /* We can end up splitting loads to multi-word pseudos
1189                          into separate loads to machine word size pseudos.
1190                          When this happens, we first had one load that can
1191                          throw, and after resolve_simple_move we'll have a
1192                          bunch of loads (at least two).  All those loads may
1193                          trap if we can have non-call exceptions, so they
1194                          all will end the current basic block.  We split the
1195                          block after the outer loop over all insns, but we
1196                          make sure here that we will be able to split the
1197                          basic block and still produce the correct control
1198                          flow graph for it.  */
1199                       gcc_assert (!cfi
1200                                   || (flag_non_call_exceptions
1201                                       && can_throw_internal (insn)));
1202
1203                       insn = resolve_simple_move (set, insn);
1204                       if (insn != orig_insn)
1205                         {
1206                           recog_memoized (insn);
1207                           extract_insn (insn);
1208
1209                           if (cfi)
1210                             SET_BIT (sub_blocks, bb->index);
1211                         }
1212                     }
1213                   else
1214                     {
1215                       rtx decomposed_shift;
1216
1217                       decomposed_shift = resolve_shift_zext (insn);
1218                       if (decomposed_shift != NULL_RTX)
1219                         {
1220                           insn = decomposed_shift;
1221                           recog_memoized (insn);
1222                           extract_insn (insn);
1223                         }
1224                     }
1225
1226                   for (i = recog_data.n_operands - 1; i >= 0; --i)
1227                     for_each_rtx (recog_data.operand_loc[i],
1228                                   resolve_subreg_use,
1229                                   insn);
1230
1231                   resolve_reg_notes (insn);
1232
1233                   if (num_validated_changes () > 0)
1234                     {
1235                       for (i = recog_data.n_dups - 1; i >= 0; --i)
1236                         {
1237                           rtx *pl = recog_data.dup_loc[i];
1238                           int dup_num = recog_data.dup_num[i];
1239                           rtx *px = recog_data.operand_loc[dup_num];
1240
1241                           validate_unshare_change (insn, pl, *px, 1);
1242                         }
1243
1244                       i = apply_change_group ();
1245                       gcc_assert (i);
1246                     }
1247                 }
1248             }
1249         }
1250
1251       /* If we had insns to split that caused control flow insns in the middle
1252          of a basic block, split those blocks now.  Note that we only handle
1253          the case where splitting a load has caused multiple possibly trapping
1254          loads to appear.  */
1255       EXECUTE_IF_SET_IN_SBITMAP (sub_blocks, 0, i, sbi)
1256         {
1257           rtx insn, end;
1258           edge fallthru;
1259
1260           bb = BASIC_BLOCK (i);
1261           insn = BB_HEAD (bb);
1262           end = BB_END (bb);
1263
1264           while (insn != end)
1265             {
1266               if (control_flow_insn_p (insn))
1267                 {
1268                   /* Split the block after insn.  There will be a fallthru
1269                      edge, which is OK so we keep it.  We have to create the
1270                      exception edges ourselves.  */
1271                   fallthru = split_block (bb, insn);
1272                   rtl_make_eh_edge (NULL, bb, BB_END (bb));
1273                   bb = fallthru->dest;
1274                   insn = BB_HEAD (bb);
1275                 }
1276               else
1277                 insn = NEXT_INSN (insn);
1278             }
1279         }
1280
1281       sbitmap_free (sub_blocks);
1282     }
1283
1284   {
1285     unsigned int i;
1286     bitmap b;
1287
1288     for (i = 0; VEC_iterate (bitmap, reg_copy_graph, i, b); ++i)
1289       if (b)
1290         BITMAP_FREE (b);
1291   }
1292
1293   VEC_free (bitmap, heap, reg_copy_graph);  
1294
1295   BITMAP_FREE (decomposable_context);
1296   BITMAP_FREE (non_decomposable_context);
1297 }
1298 \f
1299 /* Gate function for lower subreg pass.  */
1300
1301 static bool
1302 gate_handle_lower_subreg (void)
1303 {
1304   return flag_split_wide_types != 0;
1305 }
1306
1307 /* Implement first lower subreg pass.  */
1308
1309 static unsigned int
1310 rest_of_handle_lower_subreg (void)
1311 {
1312   decompose_multiword_subregs ();
1313   return 0;
1314 }
1315
1316 /* Implement second lower subreg pass.  */
1317
1318 static unsigned int
1319 rest_of_handle_lower_subreg2 (void)
1320 {
1321   decompose_multiword_subregs ();
1322   return 0;
1323 }
1324
1325 struct rtl_opt_pass pass_lower_subreg =
1326 {
1327  {
1328   RTL_PASS,
1329   "subreg",                             /* name */
1330   gate_handle_lower_subreg,             /* gate */
1331   rest_of_handle_lower_subreg,          /* execute */
1332   NULL,                                 /* sub */
1333   NULL,                                 /* next */
1334   0,                                    /* static_pass_number */
1335   TV_LOWER_SUBREG,                      /* tv_id */
1336   0,                                    /* properties_required */
1337   0,                                    /* properties_provided */
1338   0,                                    /* properties_destroyed */
1339   0,                                    /* todo_flags_start */
1340   TODO_dump_func |
1341   TODO_ggc_collect |
1342   TODO_verify_flow                      /* todo_flags_finish */
1343  }
1344 };
1345
1346 struct rtl_opt_pass pass_lower_subreg2 =
1347 {
1348  {
1349   RTL_PASS,
1350   "subreg2",                            /* name */
1351   gate_handle_lower_subreg,             /* gate */
1352   rest_of_handle_lower_subreg2,          /* execute */
1353   NULL,                                 /* sub */
1354   NULL,                                 /* next */
1355   0,                                    /* static_pass_number */
1356   TV_LOWER_SUBREG,                      /* tv_id */
1357   0,                                    /* properties_required */
1358   0,                                    /* properties_provided */
1359   0,                                    /* properties_destroyed */
1360   0,                                    /* todo_flags_start */
1361   TODO_df_finish | TODO_verify_rtl_sharing |
1362   TODO_dump_func |
1363   TODO_ggc_collect |
1364   TODO_verify_flow                      /* todo_flags_finish */
1365  }
1366 };