OSDN Git Service

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