diagnostic-show-locus.c: remove unused field from class colorizer
[official-gcc.git] / gcc / lower-subreg.c
blob1ab1c71211f5bcea98c6718a8532e7f73a078adf
1 /* Decompose multiword subregs.
2 Copyright (C) 2007-2017 Free Software Foundation, Inc.
3 Contributed by Richard Henderson <rth@redhat.com>
4 Ian Lance Taylor <iant@google.com>
6 This file is part of GCC.
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.
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.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "cfghooks.h"
29 #include "df.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "expmed.h"
33 #include "insn-config.h"
34 #include "emit-rtl.h"
35 #include "recog.h"
36 #include "cfgrtl.h"
37 #include "cfgbuild.h"
38 #include "dce.h"
39 #include "expr.h"
40 #include "tree-pass.h"
41 #include "lower-subreg.h"
42 #include "rtl-iter.h"
45 /* Decompose multi-word pseudo-registers into individual
46 pseudo-registers when possible and profitable. This is possible
47 when all the uses of a multi-word register are via SUBREG, or are
48 copies of the register to another location. Breaking apart the
49 register permits more CSE and permits better register allocation.
50 This is profitable if the machine does not have move instructions
51 to do this.
53 This pass only splits moves with modes that are wider than
54 word_mode and ASHIFTs, LSHIFTRTs, ASHIFTRTs and ZERO_EXTENDs with
55 integer modes that are twice the width of word_mode. The latter
56 could be generalized if there was a need to do this, but the trend in
57 architectures is to not need this.
59 There are two useful preprocessor defines for use by maintainers:
61 #define LOG_COSTS 1
63 if you wish to see the actual cost estimates that are being used
64 for each mode wider than word mode and the cost estimates for zero
65 extension and the shifts. This can be useful when port maintainers
66 are tuning insn rtx costs.
68 #define FORCE_LOWERING 1
70 if you wish to test the pass with all the transformation forced on.
71 This can be useful for finding bugs in the transformations. */
73 #define LOG_COSTS 0
74 #define FORCE_LOWERING 0
76 /* Bit N in this bitmap is set if regno N is used in a context in
77 which we can decompose it. */
78 static bitmap decomposable_context;
80 /* Bit N in this bitmap is set if regno N is used in a context in
81 which it can not be decomposed. */
82 static bitmap non_decomposable_context;
84 /* Bit N in this bitmap is set if regno N is used in a subreg
85 which changes the mode but not the size. This typically happens
86 when the register accessed as a floating-point value; we want to
87 avoid generating accesses to its subwords in integer modes. */
88 static bitmap subreg_context;
90 /* Bit N in the bitmap in element M of this array is set if there is a
91 copy from reg M to reg N. */
92 static vec<bitmap> reg_copy_graph;
94 struct target_lower_subreg default_target_lower_subreg;
95 #if SWITCHABLE_TARGET
96 struct target_lower_subreg *this_target_lower_subreg
97 = &default_target_lower_subreg;
98 #endif
100 #define twice_word_mode \
101 this_target_lower_subreg->x_twice_word_mode
102 #define choices \
103 this_target_lower_subreg->x_choices
105 /* RTXes used while computing costs. */
106 struct cost_rtxes {
107 /* Source and target registers. */
108 rtx source;
109 rtx target;
111 /* A twice_word_mode ZERO_EXTEND of SOURCE. */
112 rtx zext;
114 /* A shift of SOURCE. */
115 rtx shift;
117 /* A SET of TARGET. */
118 rtx set;
121 /* Return the cost of a CODE shift in mode MODE by OP1 bits, using the
122 rtxes in RTXES. SPEED_P selects between the speed and size cost. */
124 static int
125 shift_cost (bool speed_p, struct cost_rtxes *rtxes, enum rtx_code code,
126 machine_mode mode, int op1)
128 PUT_CODE (rtxes->shift, code);
129 PUT_MODE (rtxes->shift, mode);
130 PUT_MODE (rtxes->source, mode);
131 XEXP (rtxes->shift, 1) = GEN_INT (op1);
132 return set_src_cost (rtxes->shift, mode, speed_p);
135 /* For each X in the range [0, BITS_PER_WORD), set SPLITTING[X]
136 to true if it is profitable to split a double-word CODE shift
137 of X + BITS_PER_WORD bits. SPEED_P says whether we are testing
138 for speed or size profitability.
140 Use the rtxes in RTXES to calculate costs. WORD_MOVE_ZERO_COST is
141 the cost of moving zero into a word-mode register. WORD_MOVE_COST
142 is the cost of moving between word registers. */
144 static void
145 compute_splitting_shift (bool speed_p, struct cost_rtxes *rtxes,
146 bool *splitting, enum rtx_code code,
147 int word_move_zero_cost, int word_move_cost)
149 int wide_cost, narrow_cost, upper_cost, i;
151 for (i = 0; i < BITS_PER_WORD; i++)
153 wide_cost = shift_cost (speed_p, rtxes, code, twice_word_mode,
154 i + BITS_PER_WORD);
155 if (i == 0)
156 narrow_cost = word_move_cost;
157 else
158 narrow_cost = shift_cost (speed_p, rtxes, code, word_mode, i);
160 if (code != ASHIFTRT)
161 upper_cost = word_move_zero_cost;
162 else if (i == BITS_PER_WORD - 1)
163 upper_cost = word_move_cost;
164 else
165 upper_cost = shift_cost (speed_p, rtxes, code, word_mode,
166 BITS_PER_WORD - 1);
168 if (LOG_COSTS)
169 fprintf (stderr, "%s %s by %d: original cost %d, split cost %d + %d\n",
170 GET_MODE_NAME (twice_word_mode), GET_RTX_NAME (code),
171 i + BITS_PER_WORD, wide_cost, narrow_cost, upper_cost);
173 if (FORCE_LOWERING || wide_cost >= narrow_cost + upper_cost)
174 splitting[i] = true;
178 /* Compute what we should do when optimizing for speed or size; SPEED_P
179 selects which. Use RTXES for computing costs. */
181 static void
182 compute_costs (bool speed_p, struct cost_rtxes *rtxes)
184 unsigned int i;
185 int word_move_zero_cost, word_move_cost;
187 PUT_MODE (rtxes->target, word_mode);
188 SET_SRC (rtxes->set) = CONST0_RTX (word_mode);
189 word_move_zero_cost = set_rtx_cost (rtxes->set, speed_p);
191 SET_SRC (rtxes->set) = rtxes->source;
192 word_move_cost = set_rtx_cost (rtxes->set, speed_p);
194 if (LOG_COSTS)
195 fprintf (stderr, "%s move: from zero cost %d, from reg cost %d\n",
196 GET_MODE_NAME (word_mode), word_move_zero_cost, word_move_cost);
198 for (i = 0; i < MAX_MACHINE_MODE; i++)
200 machine_mode mode = (machine_mode) i;
201 int factor = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
202 if (factor > 1)
204 int mode_move_cost;
206 PUT_MODE (rtxes->target, mode);
207 PUT_MODE (rtxes->source, mode);
208 mode_move_cost = set_rtx_cost (rtxes->set, speed_p);
210 if (LOG_COSTS)
211 fprintf (stderr, "%s move: original cost %d, split cost %d * %d\n",
212 GET_MODE_NAME (mode), mode_move_cost,
213 word_move_cost, factor);
215 if (FORCE_LOWERING || mode_move_cost >= word_move_cost * factor)
217 choices[speed_p].move_modes_to_split[i] = true;
218 choices[speed_p].something_to_do = true;
223 /* For the moves and shifts, the only case that is checked is one
224 where the mode of the target is an integer mode twice the width
225 of the word_mode.
227 If it is not profitable to split a double word move then do not
228 even consider the shifts or the zero extension. */
229 if (choices[speed_p].move_modes_to_split[(int) twice_word_mode])
231 int zext_cost;
233 /* The only case here to check to see if moving the upper part with a
234 zero is cheaper than doing the zext itself. */
235 PUT_MODE (rtxes->source, word_mode);
236 zext_cost = set_src_cost (rtxes->zext, twice_word_mode, speed_p);
238 if (LOG_COSTS)
239 fprintf (stderr, "%s %s: original cost %d, split cost %d + %d\n",
240 GET_MODE_NAME (twice_word_mode), GET_RTX_NAME (ZERO_EXTEND),
241 zext_cost, word_move_cost, word_move_zero_cost);
243 if (FORCE_LOWERING || zext_cost >= word_move_cost + word_move_zero_cost)
244 choices[speed_p].splitting_zext = true;
246 compute_splitting_shift (speed_p, rtxes,
247 choices[speed_p].splitting_ashift, ASHIFT,
248 word_move_zero_cost, word_move_cost);
249 compute_splitting_shift (speed_p, rtxes,
250 choices[speed_p].splitting_lshiftrt, LSHIFTRT,
251 word_move_zero_cost, word_move_cost);
252 compute_splitting_shift (speed_p, rtxes,
253 choices[speed_p].splitting_ashiftrt, ASHIFTRT,
254 word_move_zero_cost, word_move_cost);
258 /* Do one-per-target initialisation. This involves determining
259 which operations on the machine are profitable. If none are found,
260 then the pass just returns when called. */
262 void
263 init_lower_subreg (void)
265 struct cost_rtxes rtxes;
267 memset (this_target_lower_subreg, 0, sizeof (*this_target_lower_subreg));
269 twice_word_mode = GET_MODE_2XWIDER_MODE (word_mode);
271 rtxes.target = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
272 rtxes.source = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 2);
273 rtxes.set = gen_rtx_SET (rtxes.target, rtxes.source);
274 rtxes.zext = gen_rtx_ZERO_EXTEND (twice_word_mode, rtxes.source);
275 rtxes.shift = gen_rtx_ASHIFT (twice_word_mode, rtxes.source, const0_rtx);
277 if (LOG_COSTS)
278 fprintf (stderr, "\nSize costs\n==========\n\n");
279 compute_costs (false, &rtxes);
281 if (LOG_COSTS)
282 fprintf (stderr, "\nSpeed costs\n===========\n\n");
283 compute_costs (true, &rtxes);
286 static bool
287 simple_move_operand (rtx x)
289 if (GET_CODE (x) == SUBREG)
290 x = SUBREG_REG (x);
292 if (!OBJECT_P (x))
293 return false;
295 if (GET_CODE (x) == LABEL_REF
296 || GET_CODE (x) == SYMBOL_REF
297 || GET_CODE (x) == HIGH
298 || GET_CODE (x) == CONST)
299 return false;
301 if (MEM_P (x)
302 && (MEM_VOLATILE_P (x)
303 || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x))))
304 return false;
306 return true;
309 /* If INSN is a single set between two objects that we want to split,
310 return the single set. SPEED_P says whether we are optimizing
311 INSN for speed or size.
313 INSN should have been passed to recog and extract_insn before this
314 is called. */
316 static rtx
317 simple_move (rtx_insn *insn, bool speed_p)
319 rtx x;
320 rtx set;
321 machine_mode mode;
323 if (recog_data.n_operands != 2)
324 return NULL_RTX;
326 set = single_set (insn);
327 if (!set)
328 return NULL_RTX;
330 x = SET_DEST (set);
331 if (x != recog_data.operand[0] && x != recog_data.operand[1])
332 return NULL_RTX;
333 if (!simple_move_operand (x))
334 return NULL_RTX;
336 x = SET_SRC (set);
337 if (x != recog_data.operand[0] && x != recog_data.operand[1])
338 return NULL_RTX;
339 /* For the src we can handle ASM_OPERANDS, and it is beneficial for
340 things like x86 rdtsc which returns a DImode value. */
341 if (GET_CODE (x) != ASM_OPERANDS
342 && !simple_move_operand (x))
343 return NULL_RTX;
345 /* We try to decompose in integer modes, to avoid generating
346 inefficient code copying between integer and floating point
347 registers. That means that we can't decompose if this is a
348 non-integer mode for which there is no integer mode of the same
349 size. */
350 mode = GET_MODE (SET_DEST (set));
351 if (!SCALAR_INT_MODE_P (mode)
352 && (mode_for_size (GET_MODE_SIZE (mode) * BITS_PER_UNIT, MODE_INT, 0)
353 == BLKmode))
354 return NULL_RTX;
356 /* Reject PARTIAL_INT modes. They are used for processor specific
357 purposes and it's probably best not to tamper with them. */
358 if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
359 return NULL_RTX;
361 if (!choices[speed_p].move_modes_to_split[(int) mode])
362 return NULL_RTX;
364 return set;
367 /* If SET is a copy from one multi-word pseudo-register to another,
368 record that in reg_copy_graph. Return whether it is such a
369 copy. */
371 static bool
372 find_pseudo_copy (rtx set)
374 rtx dest = SET_DEST (set);
375 rtx src = SET_SRC (set);
376 unsigned int rd, rs;
377 bitmap b;
379 if (!REG_P (dest) || !REG_P (src))
380 return false;
382 rd = REGNO (dest);
383 rs = REGNO (src);
384 if (HARD_REGISTER_NUM_P (rd) || HARD_REGISTER_NUM_P (rs))
385 return false;
387 b = reg_copy_graph[rs];
388 if (b == NULL)
390 b = BITMAP_ALLOC (NULL);
391 reg_copy_graph[rs] = b;
394 bitmap_set_bit (b, rd);
396 return true;
399 /* Look through the registers in DECOMPOSABLE_CONTEXT. For each case
400 where they are copied to another register, add the register to
401 which they are copied to DECOMPOSABLE_CONTEXT. Use
402 NON_DECOMPOSABLE_CONTEXT to limit this--we don't bother to track
403 copies of registers which are in NON_DECOMPOSABLE_CONTEXT. */
405 static void
406 propagate_pseudo_copies (void)
408 auto_bitmap queue, propagate;
410 bitmap_copy (queue, decomposable_context);
413 bitmap_iterator iter;
414 unsigned int i;
416 bitmap_clear (propagate);
418 EXECUTE_IF_SET_IN_BITMAP (queue, 0, i, iter)
420 bitmap b = reg_copy_graph[i];
421 if (b)
422 bitmap_ior_and_compl_into (propagate, b, non_decomposable_context);
425 bitmap_and_compl (queue, propagate, decomposable_context);
426 bitmap_ior_into (decomposable_context, propagate);
428 while (!bitmap_empty_p (queue));
431 /* A pointer to one of these values is passed to
432 find_decomposable_subregs. */
434 enum classify_move_insn
436 /* Not a simple move from one location to another. */
437 NOT_SIMPLE_MOVE,
438 /* A simple move we want to decompose. */
439 DECOMPOSABLE_SIMPLE_MOVE,
440 /* Any other simple move. */
441 SIMPLE_MOVE
444 /* If we find a SUBREG in *LOC which we could use to decompose a
445 pseudo-register, set a bit in DECOMPOSABLE_CONTEXT. If we find an
446 unadorned register which is not a simple pseudo-register copy,
447 DATA will point at the type of move, and we set a bit in
448 DECOMPOSABLE_CONTEXT or NON_DECOMPOSABLE_CONTEXT as appropriate. */
450 static void
451 find_decomposable_subregs (rtx *loc, enum classify_move_insn *pcmi)
453 subrtx_var_iterator::array_type array;
454 FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
456 rtx x = *iter;
457 if (GET_CODE (x) == SUBREG)
459 rtx inner = SUBREG_REG (x);
460 unsigned int regno, outer_size, inner_size, outer_words, inner_words;
462 if (!REG_P (inner))
463 continue;
465 regno = REGNO (inner);
466 if (HARD_REGISTER_NUM_P (regno))
468 iter.skip_subrtxes ();
469 continue;
472 outer_size = GET_MODE_SIZE (GET_MODE (x));
473 inner_size = GET_MODE_SIZE (GET_MODE (inner));
474 outer_words = (outer_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
475 inner_words = (inner_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
477 /* We only try to decompose single word subregs of multi-word
478 registers. When we find one, we return -1 to avoid iterating
479 over the inner register.
481 ??? This doesn't allow, e.g., DImode subregs of TImode values
482 on 32-bit targets. We would need to record the way the
483 pseudo-register was used, and only decompose if all the uses
484 were the same number and size of pieces. Hopefully this
485 doesn't happen much. */
487 if (outer_words == 1 && inner_words > 1)
489 bitmap_set_bit (decomposable_context, regno);
490 iter.skip_subrtxes ();
491 continue;
494 /* If this is a cast from one mode to another, where the modes
495 have the same size, and they are not tieable, then mark this
496 register as non-decomposable. If we decompose it we are
497 likely to mess up whatever the backend is trying to do. */
498 if (outer_words > 1
499 && outer_size == inner_size
500 && !MODES_TIEABLE_P (GET_MODE (x), GET_MODE (inner)))
502 bitmap_set_bit (non_decomposable_context, regno);
503 bitmap_set_bit (subreg_context, regno);
504 iter.skip_subrtxes ();
505 continue;
508 else if (REG_P (x))
510 unsigned int regno;
512 /* We will see an outer SUBREG before we see the inner REG, so
513 when we see a plain REG here it means a direct reference to
514 the register.
516 If this is not a simple copy from one location to another,
517 then we can not decompose this register. If this is a simple
518 copy we want to decompose, and the mode is right,
519 then we mark the register as decomposable.
520 Otherwise we don't say anything about this register --
521 it could be decomposed, but whether that would be
522 profitable depends upon how it is used elsewhere.
524 We only set bits in the bitmap for multi-word
525 pseudo-registers, since those are the only ones we care about
526 and it keeps the size of the bitmaps down. */
528 regno = REGNO (x);
529 if (!HARD_REGISTER_NUM_P (regno)
530 && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
532 switch (*pcmi)
534 case NOT_SIMPLE_MOVE:
535 bitmap_set_bit (non_decomposable_context, regno);
536 break;
537 case DECOMPOSABLE_SIMPLE_MOVE:
538 if (MODES_TIEABLE_P (GET_MODE (x), word_mode))
539 bitmap_set_bit (decomposable_context, regno);
540 break;
541 case SIMPLE_MOVE:
542 break;
543 default:
544 gcc_unreachable ();
548 else if (MEM_P (x))
550 enum classify_move_insn cmi_mem = NOT_SIMPLE_MOVE;
552 /* Any registers used in a MEM do not participate in a
553 SIMPLE_MOVE or DECOMPOSABLE_SIMPLE_MOVE. Do our own recursion
554 here, and return -1 to block the parent's recursion. */
555 find_decomposable_subregs (&XEXP (x, 0), &cmi_mem);
556 iter.skip_subrtxes ();
561 /* Decompose REGNO into word-sized components. We smash the REG node
562 in place. This ensures that (1) something goes wrong quickly if we
563 fail to make some replacement, and (2) the debug information inside
564 the symbol table is automatically kept up to date. */
566 static void
567 decompose_register (unsigned int regno)
569 rtx reg;
570 unsigned int words, i;
571 rtvec v;
573 reg = regno_reg_rtx[regno];
575 regno_reg_rtx[regno] = NULL_RTX;
577 words = GET_MODE_SIZE (GET_MODE (reg));
578 words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
580 v = rtvec_alloc (words);
581 for (i = 0; i < words; ++i)
582 RTVEC_ELT (v, i) = gen_reg_rtx_offset (reg, word_mode, i * UNITS_PER_WORD);
584 PUT_CODE (reg, CONCATN);
585 XVEC (reg, 0) = v;
587 if (dump_file)
589 fprintf (dump_file, "; Splitting reg %u ->", regno);
590 for (i = 0; i < words; ++i)
591 fprintf (dump_file, " %u", REGNO (XVECEXP (reg, 0, i)));
592 fputc ('\n', dump_file);
596 /* Get a SUBREG of a CONCATN. */
598 static rtx
599 simplify_subreg_concatn (machine_mode outermode, rtx op,
600 unsigned int byte)
602 unsigned int inner_size;
603 machine_mode innermode, partmode;
604 rtx part;
605 unsigned int final_offset;
607 gcc_assert (GET_CODE (op) == CONCATN);
608 gcc_assert (byte % GET_MODE_SIZE (outermode) == 0);
610 innermode = GET_MODE (op);
611 gcc_assert (byte < GET_MODE_SIZE (innermode));
612 if (GET_MODE_SIZE (outermode) > GET_MODE_SIZE (innermode))
613 return NULL_RTX;
615 inner_size = GET_MODE_SIZE (innermode) / XVECLEN (op, 0);
616 part = XVECEXP (op, 0, byte / inner_size);
617 partmode = GET_MODE (part);
619 /* VECTOR_CSTs in debug expressions are expanded into CONCATN instead of
620 regular CONST_VECTORs. They have vector or integer modes, depending
621 on the capabilities of the target. Cope with them. */
622 if (partmode == VOIDmode && VECTOR_MODE_P (innermode))
623 partmode = GET_MODE_INNER (innermode);
624 else if (partmode == VOIDmode)
626 enum mode_class mclass = GET_MODE_CLASS (innermode);
627 partmode = mode_for_size (inner_size * BITS_PER_UNIT, mclass, 0);
630 final_offset = byte % inner_size;
631 if (final_offset + GET_MODE_SIZE (outermode) > inner_size)
632 return NULL_RTX;
634 return simplify_gen_subreg (outermode, part, partmode, final_offset);
637 /* Wrapper around simplify_gen_subreg which handles CONCATN. */
639 static rtx
640 simplify_gen_subreg_concatn (machine_mode outermode, rtx op,
641 machine_mode innermode, unsigned int byte)
643 rtx ret;
645 /* We have to handle generating a SUBREG of a SUBREG of a CONCATN.
646 If OP is a SUBREG of a CONCATN, then it must be a simple mode
647 change with the same size and offset 0, or it must extract a
648 part. We shouldn't see anything else here. */
649 if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == CONCATN)
651 rtx op2;
653 if ((GET_MODE_SIZE (GET_MODE (op))
654 == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))))
655 && SUBREG_BYTE (op) == 0)
656 return simplify_gen_subreg_concatn (outermode, SUBREG_REG (op),
657 GET_MODE (SUBREG_REG (op)), byte);
659 op2 = simplify_subreg_concatn (GET_MODE (op), SUBREG_REG (op),
660 SUBREG_BYTE (op));
661 if (op2 == NULL_RTX)
663 /* We don't handle paradoxical subregs here. */
664 gcc_assert (GET_MODE_SIZE (outermode)
665 <= GET_MODE_SIZE (GET_MODE (op)));
666 gcc_assert (GET_MODE_SIZE (GET_MODE (op))
667 <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))));
668 op2 = simplify_subreg_concatn (outermode, SUBREG_REG (op),
669 byte + SUBREG_BYTE (op));
670 gcc_assert (op2 != NULL_RTX);
671 return op2;
674 op = op2;
675 gcc_assert (op != NULL_RTX);
676 gcc_assert (innermode == GET_MODE (op));
679 if (GET_CODE (op) == CONCATN)
680 return simplify_subreg_concatn (outermode, op, byte);
682 ret = simplify_gen_subreg (outermode, op, innermode, byte);
684 /* If we see an insn like (set (reg:DI) (subreg:DI (reg:SI) 0)) then
685 resolve_simple_move will ask for the high part of the paradoxical
686 subreg, which does not have a value. Just return a zero. */
687 if (ret == NULL_RTX
688 && GET_CODE (op) == SUBREG
689 && SUBREG_BYTE (op) == 0
690 && (GET_MODE_SIZE (innermode)
691 > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op)))))
692 return CONST0_RTX (outermode);
694 gcc_assert (ret != NULL_RTX);
695 return ret;
698 /* Return whether we should resolve X into the registers into which it
699 was decomposed. */
701 static bool
702 resolve_reg_p (rtx x)
704 return GET_CODE (x) == CONCATN;
707 /* Return whether X is a SUBREG of a register which we need to
708 resolve. */
710 static bool
711 resolve_subreg_p (rtx x)
713 if (GET_CODE (x) != SUBREG)
714 return false;
715 return resolve_reg_p (SUBREG_REG (x));
718 /* Look for SUBREGs in *LOC which need to be decomposed. */
720 static bool
721 resolve_subreg_use (rtx *loc, rtx insn)
723 subrtx_ptr_iterator::array_type array;
724 FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
726 rtx *loc = *iter;
727 rtx x = *loc;
728 if (resolve_subreg_p (x))
730 x = simplify_subreg_concatn (GET_MODE (x), SUBREG_REG (x),
731 SUBREG_BYTE (x));
733 /* It is possible for a note to contain a reference which we can
734 decompose. In this case, return 1 to the caller to indicate
735 that the note must be removed. */
736 if (!x)
738 gcc_assert (!insn);
739 return true;
742 validate_change (insn, loc, x, 1);
743 iter.skip_subrtxes ();
745 else if (resolve_reg_p (x))
746 /* Return 1 to the caller to indicate that we found a direct
747 reference to a register which is being decomposed. This can
748 happen inside notes, multiword shift or zero-extend
749 instructions. */
750 return true;
753 return false;
756 /* Resolve any decomposed registers which appear in register notes on
757 INSN. */
759 static void
760 resolve_reg_notes (rtx_insn *insn)
762 rtx *pnote, note;
764 note = find_reg_equal_equiv_note (insn);
765 if (note)
767 int old_count = num_validated_changes ();
768 if (resolve_subreg_use (&XEXP (note, 0), NULL_RTX))
769 remove_note (insn, note);
770 else
771 if (old_count != num_validated_changes ())
772 df_notes_rescan (insn);
775 pnote = &REG_NOTES (insn);
776 while (*pnote != NULL_RTX)
778 bool del = false;
780 note = *pnote;
781 switch (REG_NOTE_KIND (note))
783 case REG_DEAD:
784 case REG_UNUSED:
785 if (resolve_reg_p (XEXP (note, 0)))
786 del = true;
787 break;
789 default:
790 break;
793 if (del)
794 *pnote = XEXP (note, 1);
795 else
796 pnote = &XEXP (note, 1);
800 /* Return whether X can be decomposed into subwords. */
802 static bool
803 can_decompose_p (rtx x)
805 if (REG_P (x))
807 unsigned int regno = REGNO (x);
809 if (HARD_REGISTER_NUM_P (regno))
811 unsigned int byte, num_bytes;
813 num_bytes = GET_MODE_SIZE (GET_MODE (x));
814 for (byte = 0; byte < num_bytes; byte += UNITS_PER_WORD)
815 if (simplify_subreg_regno (regno, GET_MODE (x), byte, word_mode) < 0)
816 return false;
817 return true;
819 else
820 return !bitmap_bit_p (subreg_context, regno);
823 return true;
826 /* Decompose the registers used in a simple move SET within INSN. If
827 we don't change anything, return INSN, otherwise return the start
828 of the sequence of moves. */
830 static rtx_insn *
831 resolve_simple_move (rtx set, rtx_insn *insn)
833 rtx src, dest, real_dest;
834 rtx_insn *insns;
835 machine_mode orig_mode, dest_mode;
836 unsigned int words;
837 bool pushing;
839 src = SET_SRC (set);
840 dest = SET_DEST (set);
841 orig_mode = GET_MODE (dest);
843 words = (GET_MODE_SIZE (orig_mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
844 gcc_assert (words > 1);
846 start_sequence ();
848 /* We have to handle copying from a SUBREG of a decomposed reg where
849 the SUBREG is larger than word size. Rather than assume that we
850 can take a word_mode SUBREG of the destination, we copy to a new
851 register and then copy that to the destination. */
853 real_dest = NULL_RTX;
855 if (GET_CODE (src) == SUBREG
856 && resolve_reg_p (SUBREG_REG (src))
857 && (SUBREG_BYTE (src) != 0
858 || (GET_MODE_SIZE (orig_mode)
859 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))))
861 real_dest = dest;
862 dest = gen_reg_rtx (orig_mode);
863 if (REG_P (real_dest))
864 REG_ATTRS (dest) = REG_ATTRS (real_dest);
867 /* Similarly if we are copying to a SUBREG of a decomposed reg where
868 the SUBREG is larger than word size. */
870 if (GET_CODE (dest) == SUBREG
871 && resolve_reg_p (SUBREG_REG (dest))
872 && (SUBREG_BYTE (dest) != 0
873 || (GET_MODE_SIZE (orig_mode)
874 != GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))))
876 rtx reg, smove;
877 rtx_insn *minsn;
879 reg = gen_reg_rtx (orig_mode);
880 minsn = emit_move_insn (reg, src);
881 smove = single_set (minsn);
882 gcc_assert (smove != NULL_RTX);
883 resolve_simple_move (smove, minsn);
884 src = reg;
887 /* If we didn't have any big SUBREGS of decomposed registers, and
888 neither side of the move is a register we are decomposing, then
889 we don't have to do anything here. */
891 if (src == SET_SRC (set)
892 && dest == SET_DEST (set)
893 && !resolve_reg_p (src)
894 && !resolve_subreg_p (src)
895 && !resolve_reg_p (dest)
896 && !resolve_subreg_p (dest))
898 end_sequence ();
899 return insn;
902 /* It's possible for the code to use a subreg of a decomposed
903 register while forming an address. We need to handle that before
904 passing the address to emit_move_insn. We pass NULL_RTX as the
905 insn parameter to resolve_subreg_use because we can not validate
906 the insn yet. */
907 if (MEM_P (src) || MEM_P (dest))
909 int acg;
911 if (MEM_P (src))
912 resolve_subreg_use (&XEXP (src, 0), NULL_RTX);
913 if (MEM_P (dest))
914 resolve_subreg_use (&XEXP (dest, 0), NULL_RTX);
915 acg = apply_change_group ();
916 gcc_assert (acg);
919 /* If SRC is a register which we can't decompose, or has side
920 effects, we need to move via a temporary register. */
922 if (!can_decompose_p (src)
923 || side_effects_p (src)
924 || GET_CODE (src) == ASM_OPERANDS)
926 rtx reg;
928 reg = gen_reg_rtx (orig_mode);
930 if (AUTO_INC_DEC)
932 rtx_insn *move = emit_move_insn (reg, src);
933 if (MEM_P (src))
935 rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
936 if (note)
937 add_reg_note (move, REG_INC, XEXP (note, 0));
940 else
941 emit_move_insn (reg, src);
943 src = reg;
946 /* If DEST is a register which we can't decompose, or has side
947 effects, we need to first move to a temporary register. We
948 handle the common case of pushing an operand directly. We also
949 go through a temporary register if it holds a floating point
950 value. This gives us better code on systems which can't move
951 data easily between integer and floating point registers. */
953 dest_mode = orig_mode;
954 pushing = push_operand (dest, dest_mode);
955 if (!can_decompose_p (dest)
956 || (side_effects_p (dest) && !pushing)
957 || (!SCALAR_INT_MODE_P (dest_mode)
958 && !resolve_reg_p (dest)
959 && !resolve_subreg_p (dest)))
961 if (real_dest == NULL_RTX)
962 real_dest = dest;
963 if (!SCALAR_INT_MODE_P (dest_mode))
965 dest_mode = mode_for_size (GET_MODE_SIZE (dest_mode) * BITS_PER_UNIT,
966 MODE_INT, 0);
967 gcc_assert (dest_mode != BLKmode);
969 dest = gen_reg_rtx (dest_mode);
970 if (REG_P (real_dest))
971 REG_ATTRS (dest) = REG_ATTRS (real_dest);
974 if (pushing)
976 unsigned int i, j, jinc;
978 gcc_assert (GET_MODE_SIZE (orig_mode) % UNITS_PER_WORD == 0);
979 gcc_assert (GET_CODE (XEXP (dest, 0)) != PRE_MODIFY);
980 gcc_assert (GET_CODE (XEXP (dest, 0)) != POST_MODIFY);
982 if (WORDS_BIG_ENDIAN == STACK_GROWS_DOWNWARD)
984 j = 0;
985 jinc = 1;
987 else
989 j = words - 1;
990 jinc = -1;
993 for (i = 0; i < words; ++i, j += jinc)
995 rtx temp;
997 temp = copy_rtx (XEXP (dest, 0));
998 temp = adjust_automodify_address_nv (dest, word_mode, temp,
999 j * UNITS_PER_WORD);
1000 emit_move_insn (temp,
1001 simplify_gen_subreg_concatn (word_mode, src,
1002 orig_mode,
1003 j * UNITS_PER_WORD));
1006 else
1008 unsigned int i;
1010 if (REG_P (dest) && !HARD_REGISTER_NUM_P (REGNO (dest)))
1011 emit_clobber (dest);
1013 for (i = 0; i < words; ++i)
1014 emit_move_insn (simplify_gen_subreg_concatn (word_mode, dest,
1015 dest_mode,
1016 i * UNITS_PER_WORD),
1017 simplify_gen_subreg_concatn (word_mode, src,
1018 orig_mode,
1019 i * UNITS_PER_WORD));
1022 if (real_dest != NULL_RTX)
1024 rtx mdest, smove;
1025 rtx_insn *minsn;
1027 if (dest_mode == orig_mode)
1028 mdest = dest;
1029 else
1030 mdest = simplify_gen_subreg (orig_mode, dest, GET_MODE (dest), 0);
1031 minsn = emit_move_insn (real_dest, mdest);
1033 if (AUTO_INC_DEC && MEM_P (real_dest)
1034 && !(resolve_reg_p (real_dest) || resolve_subreg_p (real_dest)))
1036 rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
1037 if (note)
1038 add_reg_note (minsn, REG_INC, XEXP (note, 0));
1041 smove = single_set (minsn);
1042 gcc_assert (smove != NULL_RTX);
1044 resolve_simple_move (smove, minsn);
1047 insns = get_insns ();
1048 end_sequence ();
1050 copy_reg_eh_region_note_forward (insn, insns, NULL_RTX);
1052 emit_insn_before (insns, insn);
1054 /* If we get here via self-recursion, then INSN is not yet in the insns
1055 chain and delete_insn will fail. We only want to remove INSN from the
1056 current sequence. See PR56738. */
1057 if (in_sequence_p ())
1058 remove_insn (insn);
1059 else
1060 delete_insn (insn);
1062 return insns;
1065 /* Change a CLOBBER of a decomposed register into a CLOBBER of the
1066 component registers. Return whether we changed something. */
1068 static bool
1069 resolve_clobber (rtx pat, rtx_insn *insn)
1071 rtx reg;
1072 machine_mode orig_mode;
1073 unsigned int words, i;
1074 int ret;
1076 reg = XEXP (pat, 0);
1077 if (!resolve_reg_p (reg) && !resolve_subreg_p (reg))
1078 return false;
1080 orig_mode = GET_MODE (reg);
1081 words = GET_MODE_SIZE (orig_mode);
1082 words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1084 ret = validate_change (NULL_RTX, &XEXP (pat, 0),
1085 simplify_gen_subreg_concatn (word_mode, reg,
1086 orig_mode, 0),
1088 df_insn_rescan (insn);
1089 gcc_assert (ret != 0);
1091 for (i = words - 1; i > 0; --i)
1093 rtx x;
1095 x = simplify_gen_subreg_concatn (word_mode, reg, orig_mode,
1096 i * UNITS_PER_WORD);
1097 x = gen_rtx_CLOBBER (VOIDmode, x);
1098 emit_insn_after (x, insn);
1101 resolve_reg_notes (insn);
1103 return true;
1106 /* A USE of a decomposed register is no longer meaningful. Return
1107 whether we changed something. */
1109 static bool
1110 resolve_use (rtx pat, rtx_insn *insn)
1112 if (resolve_reg_p (XEXP (pat, 0)) || resolve_subreg_p (XEXP (pat, 0)))
1114 delete_insn (insn);
1115 return true;
1118 resolve_reg_notes (insn);
1120 return false;
1123 /* A VAR_LOCATION can be simplified. */
1125 static void
1126 resolve_debug (rtx_insn *insn)
1128 subrtx_ptr_iterator::array_type array;
1129 FOR_EACH_SUBRTX_PTR (iter, array, &PATTERN (insn), NONCONST)
1131 rtx *loc = *iter;
1132 rtx x = *loc;
1133 if (resolve_subreg_p (x))
1135 x = simplify_subreg_concatn (GET_MODE (x), SUBREG_REG (x),
1136 SUBREG_BYTE (x));
1138 if (x)
1139 *loc = x;
1140 else
1141 x = copy_rtx (*loc);
1143 if (resolve_reg_p (x))
1144 *loc = copy_rtx (x);
1147 df_insn_rescan (insn);
1149 resolve_reg_notes (insn);
1152 /* Check if INSN is a decomposable multiword-shift or zero-extend and
1153 set the decomposable_context bitmap accordingly. SPEED_P is true
1154 if we are optimizing INSN for speed rather than size. Return true
1155 if INSN is decomposable. */
1157 static bool
1158 find_decomposable_shift_zext (rtx_insn *insn, bool speed_p)
1160 rtx set;
1161 rtx op;
1162 rtx op_operand;
1164 set = single_set (insn);
1165 if (!set)
1166 return false;
1168 op = SET_SRC (set);
1169 if (GET_CODE (op) != ASHIFT
1170 && GET_CODE (op) != LSHIFTRT
1171 && GET_CODE (op) != ASHIFTRT
1172 && GET_CODE (op) != ZERO_EXTEND)
1173 return false;
1175 op_operand = XEXP (op, 0);
1176 if (!REG_P (SET_DEST (set)) || !REG_P (op_operand)
1177 || HARD_REGISTER_NUM_P (REGNO (SET_DEST (set)))
1178 || HARD_REGISTER_NUM_P (REGNO (op_operand))
1179 || GET_MODE (op) != twice_word_mode)
1180 return false;
1182 if (GET_CODE (op) == ZERO_EXTEND)
1184 if (GET_MODE (op_operand) != word_mode
1185 || !choices[speed_p].splitting_zext)
1186 return false;
1188 else /* left or right shift */
1190 bool *splitting = (GET_CODE (op) == ASHIFT
1191 ? choices[speed_p].splitting_ashift
1192 : GET_CODE (op) == ASHIFTRT
1193 ? choices[speed_p].splitting_ashiftrt
1194 : choices[speed_p].splitting_lshiftrt);
1195 if (!CONST_INT_P (XEXP (op, 1))
1196 || !IN_RANGE (INTVAL (XEXP (op, 1)), BITS_PER_WORD,
1197 2 * BITS_PER_WORD - 1)
1198 || !splitting[INTVAL (XEXP (op, 1)) - BITS_PER_WORD])
1199 return false;
1201 bitmap_set_bit (decomposable_context, REGNO (op_operand));
1204 bitmap_set_bit (decomposable_context, REGNO (SET_DEST (set)));
1206 return true;
1209 /* Decompose a more than word wide shift (in INSN) of a multiword
1210 pseudo or a multiword zero-extend of a wordmode pseudo into a move
1211 and 'set to zero' insn. Return a pointer to the new insn when a
1212 replacement was done. */
1214 static rtx_insn *
1215 resolve_shift_zext (rtx_insn *insn)
1217 rtx set;
1218 rtx op;
1219 rtx op_operand;
1220 rtx_insn *insns;
1221 rtx src_reg, dest_reg, dest_upper, upper_src = NULL_RTX;
1222 int src_reg_num, dest_reg_num, offset1, offset2, src_offset;
1224 set = single_set (insn);
1225 if (!set)
1226 return NULL;
1228 op = SET_SRC (set);
1229 if (GET_CODE (op) != ASHIFT
1230 && GET_CODE (op) != LSHIFTRT
1231 && GET_CODE (op) != ASHIFTRT
1232 && GET_CODE (op) != ZERO_EXTEND)
1233 return NULL;
1235 op_operand = XEXP (op, 0);
1237 /* We can tear this operation apart only if the regs were already
1238 torn apart. */
1239 if (!resolve_reg_p (SET_DEST (set)) && !resolve_reg_p (op_operand))
1240 return NULL;
1242 /* src_reg_num is the number of the word mode register which we
1243 are operating on. For a left shift and a zero_extend on little
1244 endian machines this is register 0. */
1245 src_reg_num = (GET_CODE (op) == LSHIFTRT || GET_CODE (op) == ASHIFTRT)
1246 ? 1 : 0;
1248 if (WORDS_BIG_ENDIAN
1249 && GET_MODE_SIZE (GET_MODE (op_operand)) > UNITS_PER_WORD)
1250 src_reg_num = 1 - src_reg_num;
1252 if (GET_CODE (op) == ZERO_EXTEND)
1253 dest_reg_num = WORDS_BIG_ENDIAN ? 1 : 0;
1254 else
1255 dest_reg_num = 1 - src_reg_num;
1257 offset1 = UNITS_PER_WORD * dest_reg_num;
1258 offset2 = UNITS_PER_WORD * (1 - dest_reg_num);
1259 src_offset = UNITS_PER_WORD * src_reg_num;
1261 start_sequence ();
1263 dest_reg = simplify_gen_subreg_concatn (word_mode, SET_DEST (set),
1264 GET_MODE (SET_DEST (set)),
1265 offset1);
1266 dest_upper = simplify_gen_subreg_concatn (word_mode, SET_DEST (set),
1267 GET_MODE (SET_DEST (set)),
1268 offset2);
1269 src_reg = simplify_gen_subreg_concatn (word_mode, op_operand,
1270 GET_MODE (op_operand),
1271 src_offset);
1272 if (GET_CODE (op) == ASHIFTRT
1273 && INTVAL (XEXP (op, 1)) != 2 * BITS_PER_WORD - 1)
1274 upper_src = expand_shift (RSHIFT_EXPR, word_mode, copy_rtx (src_reg),
1275 BITS_PER_WORD - 1, NULL_RTX, 0);
1277 if (GET_CODE (op) != ZERO_EXTEND)
1279 int shift_count = INTVAL (XEXP (op, 1));
1280 if (shift_count > BITS_PER_WORD)
1281 src_reg = expand_shift (GET_CODE (op) == ASHIFT ?
1282 LSHIFT_EXPR : RSHIFT_EXPR,
1283 word_mode, src_reg,
1284 shift_count - BITS_PER_WORD,
1285 dest_reg, GET_CODE (op) != ASHIFTRT);
1288 if (dest_reg != src_reg)
1289 emit_move_insn (dest_reg, src_reg);
1290 if (GET_CODE (op) != ASHIFTRT)
1291 emit_move_insn (dest_upper, CONST0_RTX (word_mode));
1292 else if (INTVAL (XEXP (op, 1)) == 2 * BITS_PER_WORD - 1)
1293 emit_move_insn (dest_upper, copy_rtx (src_reg));
1294 else
1295 emit_move_insn (dest_upper, upper_src);
1296 insns = get_insns ();
1298 end_sequence ();
1300 emit_insn_before (insns, insn);
1302 if (dump_file)
1304 rtx_insn *in;
1305 fprintf (dump_file, "; Replacing insn: %d with insns: ", INSN_UID (insn));
1306 for (in = insns; in != insn; in = NEXT_INSN (in))
1307 fprintf (dump_file, "%d ", INSN_UID (in));
1308 fprintf (dump_file, "\n");
1311 delete_insn (insn);
1312 return insns;
1315 /* Print to dump_file a description of what we're doing with shift code CODE.
1316 SPLITTING[X] is true if we are splitting shifts by X + BITS_PER_WORD. */
1318 static void
1319 dump_shift_choices (enum rtx_code code, bool *splitting)
1321 int i;
1322 const char *sep;
1324 fprintf (dump_file,
1325 " Splitting mode %s for %s lowering with shift amounts = ",
1326 GET_MODE_NAME (twice_word_mode), GET_RTX_NAME (code));
1327 sep = "";
1328 for (i = 0; i < BITS_PER_WORD; i++)
1329 if (splitting[i])
1331 fprintf (dump_file, "%s%d", sep, i + BITS_PER_WORD);
1332 sep = ",";
1334 fprintf (dump_file, "\n");
1337 /* Print to dump_file a description of what we're doing when optimizing
1338 for speed or size; SPEED_P says which. DESCRIPTION is a description
1339 of the SPEED_P choice. */
1341 static void
1342 dump_choices (bool speed_p, const char *description)
1344 unsigned int i;
1346 fprintf (dump_file, "Choices when optimizing for %s:\n", description);
1348 for (i = 0; i < MAX_MACHINE_MODE; i++)
1349 if (GET_MODE_SIZE ((machine_mode) i) > UNITS_PER_WORD)
1350 fprintf (dump_file, " %s mode %s for copy lowering.\n",
1351 choices[speed_p].move_modes_to_split[i]
1352 ? "Splitting"
1353 : "Skipping",
1354 GET_MODE_NAME ((machine_mode) i));
1356 fprintf (dump_file, " %s mode %s for zero_extend lowering.\n",
1357 choices[speed_p].splitting_zext ? "Splitting" : "Skipping",
1358 GET_MODE_NAME (twice_word_mode));
1360 dump_shift_choices (ASHIFT, choices[speed_p].splitting_ashift);
1361 dump_shift_choices (LSHIFTRT, choices[speed_p].splitting_lshiftrt);
1362 dump_shift_choices (ASHIFTRT, choices[speed_p].splitting_ashiftrt);
1363 fprintf (dump_file, "\n");
1366 /* Look for registers which are always accessed via word-sized SUBREGs
1367 or -if DECOMPOSE_COPIES is true- via copies. Decompose these
1368 registers into several word-sized pseudo-registers. */
1370 static void
1371 decompose_multiword_subregs (bool decompose_copies)
1373 unsigned int max;
1374 basic_block bb;
1375 bool speed_p;
1377 if (dump_file)
1379 dump_choices (false, "size");
1380 dump_choices (true, "speed");
1383 /* Check if this target even has any modes to consider lowering. */
1384 if (!choices[false].something_to_do && !choices[true].something_to_do)
1386 if (dump_file)
1387 fprintf (dump_file, "Nothing to do!\n");
1388 return;
1391 max = max_reg_num ();
1393 /* First see if there are any multi-word pseudo-registers. If there
1394 aren't, there is nothing we can do. This should speed up this
1395 pass in the normal case, since it should be faster than scanning
1396 all the insns. */
1398 unsigned int i;
1399 bool useful_modes_seen = false;
1401 for (i = FIRST_PSEUDO_REGISTER; i < max; ++i)
1402 if (regno_reg_rtx[i] != NULL)
1404 machine_mode mode = GET_MODE (regno_reg_rtx[i]);
1405 if (choices[false].move_modes_to_split[(int) mode]
1406 || choices[true].move_modes_to_split[(int) mode])
1408 useful_modes_seen = true;
1409 break;
1413 if (!useful_modes_seen)
1415 if (dump_file)
1416 fprintf (dump_file, "Nothing to lower in this function.\n");
1417 return;
1421 if (df)
1423 df_set_flags (DF_DEFER_INSN_RESCAN);
1424 run_word_dce ();
1427 /* FIXME: It may be possible to change this code to look for each
1428 multi-word pseudo-register and to find each insn which sets or
1429 uses that register. That should be faster than scanning all the
1430 insns. */
1432 decomposable_context = BITMAP_ALLOC (NULL);
1433 non_decomposable_context = BITMAP_ALLOC (NULL);
1434 subreg_context = BITMAP_ALLOC (NULL);
1436 reg_copy_graph.create (max);
1437 reg_copy_graph.safe_grow_cleared (max);
1438 memset (reg_copy_graph.address (), 0, sizeof (bitmap) * max);
1440 speed_p = optimize_function_for_speed_p (cfun);
1441 FOR_EACH_BB_FN (bb, cfun)
1443 rtx_insn *insn;
1445 FOR_BB_INSNS (bb, insn)
1447 rtx set;
1448 enum classify_move_insn cmi;
1449 int i, n;
1451 if (!INSN_P (insn)
1452 || GET_CODE (PATTERN (insn)) == CLOBBER
1453 || GET_CODE (PATTERN (insn)) == USE)
1454 continue;
1456 recog_memoized (insn);
1458 if (find_decomposable_shift_zext (insn, speed_p))
1459 continue;
1461 extract_insn (insn);
1463 set = simple_move (insn, speed_p);
1465 if (!set)
1466 cmi = NOT_SIMPLE_MOVE;
1467 else
1469 /* We mark pseudo-to-pseudo copies as decomposable during the
1470 second pass only. The first pass is so early that there is
1471 good chance such moves will be optimized away completely by
1472 subsequent optimizations anyway.
1474 However, we call find_pseudo_copy even during the first pass
1475 so as to properly set up the reg_copy_graph. */
1476 if (find_pseudo_copy (set))
1477 cmi = decompose_copies? DECOMPOSABLE_SIMPLE_MOVE : SIMPLE_MOVE;
1478 else
1479 cmi = SIMPLE_MOVE;
1482 n = recog_data.n_operands;
1483 for (i = 0; i < n; ++i)
1485 find_decomposable_subregs (&recog_data.operand[i], &cmi);
1487 /* We handle ASM_OPERANDS as a special case to support
1488 things like x86 rdtsc which returns a DImode value.
1489 We can decompose the output, which will certainly be
1490 operand 0, but not the inputs. */
1492 if (cmi == SIMPLE_MOVE
1493 && GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1495 gcc_assert (i == 0);
1496 cmi = NOT_SIMPLE_MOVE;
1502 bitmap_and_compl_into (decomposable_context, non_decomposable_context);
1503 if (!bitmap_empty_p (decomposable_context))
1505 unsigned int i;
1506 sbitmap_iterator sbi;
1507 bitmap_iterator iter;
1508 unsigned int regno;
1510 propagate_pseudo_copies ();
1512 auto_sbitmap sub_blocks (last_basic_block_for_fn (cfun));
1513 bitmap_clear (sub_blocks);
1515 EXECUTE_IF_SET_IN_BITMAP (decomposable_context, 0, regno, iter)
1516 decompose_register (regno);
1518 FOR_EACH_BB_FN (bb, cfun)
1520 rtx_insn *insn;
1522 FOR_BB_INSNS (bb, insn)
1524 rtx pat;
1526 if (!INSN_P (insn))
1527 continue;
1529 pat = PATTERN (insn);
1530 if (GET_CODE (pat) == CLOBBER)
1531 resolve_clobber (pat, insn);
1532 else if (GET_CODE (pat) == USE)
1533 resolve_use (pat, insn);
1534 else if (DEBUG_INSN_P (insn))
1535 resolve_debug (insn);
1536 else
1538 rtx set;
1539 int i;
1541 recog_memoized (insn);
1542 extract_insn (insn);
1544 set = simple_move (insn, speed_p);
1545 if (set)
1547 rtx_insn *orig_insn = insn;
1548 bool cfi = control_flow_insn_p (insn);
1550 /* We can end up splitting loads to multi-word pseudos
1551 into separate loads to machine word size pseudos.
1552 When this happens, we first had one load that can
1553 throw, and after resolve_simple_move we'll have a
1554 bunch of loads (at least two). All those loads may
1555 trap if we can have non-call exceptions, so they
1556 all will end the current basic block. We split the
1557 block after the outer loop over all insns, but we
1558 make sure here that we will be able to split the
1559 basic block and still produce the correct control
1560 flow graph for it. */
1561 gcc_assert (!cfi
1562 || (cfun->can_throw_non_call_exceptions
1563 && can_throw_internal (insn)));
1565 insn = resolve_simple_move (set, insn);
1566 if (insn != orig_insn)
1568 recog_memoized (insn);
1569 extract_insn (insn);
1571 if (cfi)
1572 bitmap_set_bit (sub_blocks, bb->index);
1575 else
1577 rtx_insn *decomposed_shift;
1579 decomposed_shift = resolve_shift_zext (insn);
1580 if (decomposed_shift != NULL_RTX)
1582 insn = decomposed_shift;
1583 recog_memoized (insn);
1584 extract_insn (insn);
1588 for (i = recog_data.n_operands - 1; i >= 0; --i)
1589 resolve_subreg_use (recog_data.operand_loc[i], insn);
1591 resolve_reg_notes (insn);
1593 if (num_validated_changes () > 0)
1595 for (i = recog_data.n_dups - 1; i >= 0; --i)
1597 rtx *pl = recog_data.dup_loc[i];
1598 int dup_num = recog_data.dup_num[i];
1599 rtx *px = recog_data.operand_loc[dup_num];
1601 validate_unshare_change (insn, pl, *px, 1);
1604 i = apply_change_group ();
1605 gcc_assert (i);
1611 /* If we had insns to split that caused control flow insns in the middle
1612 of a basic block, split those blocks now. Note that we only handle
1613 the case where splitting a load has caused multiple possibly trapping
1614 loads to appear. */
1615 EXECUTE_IF_SET_IN_BITMAP (sub_blocks, 0, i, sbi)
1617 rtx_insn *insn, *end;
1618 edge fallthru;
1620 bb = BASIC_BLOCK_FOR_FN (cfun, i);
1621 insn = BB_HEAD (bb);
1622 end = BB_END (bb);
1624 while (insn != end)
1626 if (control_flow_insn_p (insn))
1628 /* Split the block after insn. There will be a fallthru
1629 edge, which is OK so we keep it. We have to create the
1630 exception edges ourselves. */
1631 fallthru = split_block (bb, insn);
1632 rtl_make_eh_edge (NULL, bb, BB_END (bb));
1633 bb = fallthru->dest;
1634 insn = BB_HEAD (bb);
1636 else
1637 insn = NEXT_INSN (insn);
1643 unsigned int i;
1644 bitmap b;
1646 FOR_EACH_VEC_ELT (reg_copy_graph, i, b)
1647 if (b)
1648 BITMAP_FREE (b);
1651 reg_copy_graph.release ();
1653 BITMAP_FREE (decomposable_context);
1654 BITMAP_FREE (non_decomposable_context);
1655 BITMAP_FREE (subreg_context);
1658 /* Implement first lower subreg pass. */
1660 namespace {
1662 const pass_data pass_data_lower_subreg =
1664 RTL_PASS, /* type */
1665 "subreg1", /* name */
1666 OPTGROUP_NONE, /* optinfo_flags */
1667 TV_LOWER_SUBREG, /* tv_id */
1668 0, /* properties_required */
1669 0, /* properties_provided */
1670 0, /* properties_destroyed */
1671 0, /* todo_flags_start */
1672 0, /* todo_flags_finish */
1675 class pass_lower_subreg : public rtl_opt_pass
1677 public:
1678 pass_lower_subreg (gcc::context *ctxt)
1679 : rtl_opt_pass (pass_data_lower_subreg, ctxt)
1682 /* opt_pass methods: */
1683 virtual bool gate (function *) { return flag_split_wide_types != 0; }
1684 virtual unsigned int execute (function *)
1686 decompose_multiword_subregs (false);
1687 return 0;
1690 }; // class pass_lower_subreg
1692 } // anon namespace
1694 rtl_opt_pass *
1695 make_pass_lower_subreg (gcc::context *ctxt)
1697 return new pass_lower_subreg (ctxt);
1700 /* Implement second lower subreg pass. */
1702 namespace {
1704 const pass_data pass_data_lower_subreg2 =
1706 RTL_PASS, /* type */
1707 "subreg2", /* name */
1708 OPTGROUP_NONE, /* optinfo_flags */
1709 TV_LOWER_SUBREG, /* tv_id */
1710 0, /* properties_required */
1711 0, /* properties_provided */
1712 0, /* properties_destroyed */
1713 0, /* todo_flags_start */
1714 TODO_df_finish, /* todo_flags_finish */
1717 class pass_lower_subreg2 : public rtl_opt_pass
1719 public:
1720 pass_lower_subreg2 (gcc::context *ctxt)
1721 : rtl_opt_pass (pass_data_lower_subreg2, ctxt)
1724 /* opt_pass methods: */
1725 virtual bool gate (function *) { return flag_split_wide_types != 0; }
1726 virtual unsigned int execute (function *)
1728 decompose_multiword_subregs (true);
1729 return 0;
1732 }; // class pass_lower_subreg2
1734 } // anon namespace
1736 rtl_opt_pass *
1737 make_pass_lower_subreg2 (gcc::context *ctxt)
1739 return new pass_lower_subreg2 (ctxt);