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