aarch64 - Set the mode for the unspec in speculation_tracker insn.
[platform/upstream/linaro-gcc.git] / gcc / lower-subreg.c
1 /* Decompose multiword subregs.
2    Copyright (C) 2007-2016 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 "backend.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "cfghooks.h"
29 #include "df.h"
30 #include "tm_p.h"
31 #include "expmed.h"
32 #include "insn-config.h"
33 #include "emit-rtl.h"
34 #include "recog.h"
35 #include "cfgrtl.h"
36 #include "cfgbuild.h"
37 #include "dce.h"
38 #include "expr.h"
39 #include "tree-pass.h"
40 #include "lower-subreg.h"
41 #include "rtl-iter.h"
42
43
44 /* Decompose multi-word pseudo-registers into individual
45    pseudo-registers when possible and profitable.  This is possible
46    when all the uses of a multi-word register are via SUBREG, or are
47    copies of the register to another location.  Breaking apart the
48    register permits more CSE and permits better register allocation.
49    This is profitable if the machine does not have move instructions
50    to do this.
51
52    This pass only splits moves with modes that are wider than
53    word_mode and ASHIFTs, LSHIFTRTs, ASHIFTRTs and ZERO_EXTENDs with
54    integer modes that are twice the width of word_mode.  The latter
55    could be generalized if there was a need to do this, but the trend in
56    architectures is to not need this.
57
58    There are two useful preprocessor defines for use by maintainers:
59
60    #define LOG_COSTS 1
61
62    if you wish to see the actual cost estimates that are being used
63    for each mode wider than word mode and the cost estimates for zero
64    extension and the shifts.   This can be useful when port maintainers
65    are tuning insn rtx costs.
66
67    #define FORCE_LOWERING 1
68
69    if you wish to test the pass with all the transformation forced on.
70    This can be useful for finding bugs in the transformations.  */
71
72 #define LOG_COSTS 0
73 #define FORCE_LOWERING 0
74
75 /* Bit N in this bitmap is set if regno N is used in a context in
76    which we can decompose it.  */
77 static bitmap decomposable_context;
78
79 /* Bit N in this bitmap is set if regno N is used in a context in
80    which it can not be decomposed.  */
81 static bitmap non_decomposable_context;
82
83 /* Bit N in this bitmap is set if regno N is used in a subreg
84    which changes the mode but not the size.  This typically happens
85    when the register accessed as a floating-point value; we want to
86    avoid generating accesses to its subwords in integer modes.  */
87 static bitmap subreg_context;
88
89 /* Bit N in the bitmap in element M of this array is set if there is a
90    copy from reg M to reg N.  */
91 static vec<bitmap> reg_copy_graph;
92
93 struct target_lower_subreg default_target_lower_subreg;
94 #if SWITCHABLE_TARGET
95 struct target_lower_subreg *this_target_lower_subreg
96   = &default_target_lower_subreg;
97 #endif
98
99 #define twice_word_mode \
100   this_target_lower_subreg->x_twice_word_mode
101 #define choices \
102   this_target_lower_subreg->x_choices
103
104 /* RTXes used while computing costs.  */
105 struct cost_rtxes {
106   /* Source and target registers.  */
107   rtx source;
108   rtx target;
109
110   /* A twice_word_mode ZERO_EXTEND of SOURCE.  */
111   rtx zext;
112
113   /* A shift of SOURCE.  */
114   rtx shift;
115
116   /* A SET of TARGET.  */
117   rtx set;
118 };
119
120 /* Return the cost of a CODE shift in mode MODE by OP1 bits, using the
121    rtxes in RTXES.  SPEED_P selects between the speed and size cost.  */
122
123 static int
124 shift_cost (bool speed_p, struct cost_rtxes *rtxes, enum rtx_code code,
125             machine_mode mode, int op1)
126 {
127   PUT_CODE (rtxes->shift, code);
128   PUT_MODE (rtxes->shift, mode);
129   PUT_MODE (rtxes->source, mode);
130   XEXP (rtxes->shift, 1) = GEN_INT (op1);
131   return set_src_cost (rtxes->shift, mode, speed_p);
132 }
133
134 /* For each X in the range [0, BITS_PER_WORD), set SPLITTING[X]
135    to true if it is profitable to split a double-word CODE shift
136    of X + BITS_PER_WORD bits.  SPEED_P says whether we are testing
137    for speed or size profitability.
138
139    Use the rtxes in RTXES to calculate costs.  WORD_MOVE_ZERO_COST is
140    the cost of moving zero into a word-mode register.  WORD_MOVE_COST
141    is the cost of moving between word registers.  */
142
143 static void
144 compute_splitting_shift (bool speed_p, struct cost_rtxes *rtxes,
145                          bool *splitting, enum rtx_code code,
146                          int word_move_zero_cost, int word_move_cost)
147 {
148   int wide_cost, narrow_cost, upper_cost, i;
149
150   for (i = 0; i < BITS_PER_WORD; i++)
151     {
152       wide_cost = shift_cost (speed_p, rtxes, code, twice_word_mode,
153                               i + BITS_PER_WORD);
154       if (i == 0)
155         narrow_cost = word_move_cost;
156       else
157         narrow_cost = shift_cost (speed_p, rtxes, code, word_mode, i);
158
159       if (code != ASHIFTRT)
160         upper_cost = word_move_zero_cost;
161       else if (i == BITS_PER_WORD - 1)
162         upper_cost = word_move_cost;
163       else
164         upper_cost = shift_cost (speed_p, rtxes, code, word_mode,
165                                  BITS_PER_WORD - 1);
166
167       if (LOG_COSTS)
168         fprintf (stderr, "%s %s by %d: original cost %d, split cost %d + %d\n",
169                  GET_MODE_NAME (twice_word_mode), GET_RTX_NAME (code),
170                  i + BITS_PER_WORD, wide_cost, narrow_cost, upper_cost);
171
172       if (FORCE_LOWERING || wide_cost >= narrow_cost + upper_cost)
173         splitting[i] = true;
174     }
175 }
176
177 /* Compute what we should do when optimizing for speed or size; SPEED_P
178    selects which.  Use RTXES for computing costs.  */
179
180 static void
181 compute_costs (bool speed_p, struct cost_rtxes *rtxes)
182 {
183   unsigned int i;
184   int word_move_zero_cost, word_move_cost;
185
186   PUT_MODE (rtxes->target, word_mode);
187   SET_SRC (rtxes->set) = CONST0_RTX (word_mode);
188   word_move_zero_cost = set_rtx_cost (rtxes->set, speed_p);
189
190   SET_SRC (rtxes->set) = rtxes->source;
191   word_move_cost = set_rtx_cost (rtxes->set, speed_p);
192
193   if (LOG_COSTS)
194     fprintf (stderr, "%s move: from zero cost %d, from reg cost %d\n",
195              GET_MODE_NAME (word_mode), word_move_zero_cost, word_move_cost);
196
197   for (i = 0; i < MAX_MACHINE_MODE; i++)
198     {
199       machine_mode mode = (machine_mode) i;
200       int factor = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
201       if (factor > 1)
202         {
203           int mode_move_cost;
204
205           PUT_MODE (rtxes->target, mode);
206           PUT_MODE (rtxes->source, mode);
207           mode_move_cost = set_rtx_cost (rtxes->set, speed_p);
208
209           if (LOG_COSTS)
210             fprintf (stderr, "%s move: original cost %d, split cost %d * %d\n",
211                      GET_MODE_NAME (mode), mode_move_cost,
212                      word_move_cost, factor);
213
214           if (FORCE_LOWERING || mode_move_cost >= word_move_cost * factor)
215             {
216               choices[speed_p].move_modes_to_split[i] = true;
217               choices[speed_p].something_to_do = true;
218             }
219         }
220     }
221
222   /* For the moves and shifts, the only case that is checked is one
223      where the mode of the target is an integer mode twice the width
224      of the word_mode.
225
226      If it is not profitable to split a double word move then do not
227      even consider the shifts or the zero extension.  */
228   if (choices[speed_p].move_modes_to_split[(int) twice_word_mode])
229     {
230       int zext_cost;
231
232       /* The only case here to check to see if moving the upper part with a
233          zero is cheaper than doing the zext itself.  */
234       PUT_MODE (rtxes->source, word_mode);
235       zext_cost = set_src_cost (rtxes->zext, twice_word_mode, speed_p);
236
237       if (LOG_COSTS)
238         fprintf (stderr, "%s %s: original cost %d, split cost %d + %d\n",
239                  GET_MODE_NAME (twice_word_mode), GET_RTX_NAME (ZERO_EXTEND),
240                  zext_cost, word_move_cost, word_move_zero_cost);
241
242       if (FORCE_LOWERING || zext_cost >= word_move_cost + word_move_zero_cost)
243         choices[speed_p].splitting_zext = true;
244
245       compute_splitting_shift (speed_p, rtxes,
246                                choices[speed_p].splitting_ashift, ASHIFT,
247                                word_move_zero_cost, word_move_cost);
248       compute_splitting_shift (speed_p, rtxes,
249                                choices[speed_p].splitting_lshiftrt, LSHIFTRT,
250                                word_move_zero_cost, word_move_cost);
251       compute_splitting_shift (speed_p, rtxes,
252                                choices[speed_p].splitting_ashiftrt, ASHIFTRT,
253                                word_move_zero_cost, word_move_cost);
254     }
255 }
256
257 /* Do one-per-target initialisation.  This involves determining
258    which operations on the machine are profitable.  If none are found,
259    then the pass just returns when called.  */
260
261 void
262 init_lower_subreg (void)
263 {
264   struct cost_rtxes rtxes;
265
266   memset (this_target_lower_subreg, 0, sizeof (*this_target_lower_subreg));
267
268   twice_word_mode = GET_MODE_2XWIDER_MODE (word_mode);
269
270   rtxes.target = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 1);
271   rtxes.source = gen_rtx_REG (word_mode, LAST_VIRTUAL_REGISTER + 2);
272   rtxes.set = gen_rtx_SET (rtxes.target, rtxes.source);
273   rtxes.zext = gen_rtx_ZERO_EXTEND (twice_word_mode, rtxes.source);
274   rtxes.shift = gen_rtx_ASHIFT (twice_word_mode, rtxes.source, const0_rtx);
275
276   if (LOG_COSTS)
277     fprintf (stderr, "\nSize costs\n==========\n\n");
278   compute_costs (false, &rtxes);
279
280   if (LOG_COSTS)
281     fprintf (stderr, "\nSpeed costs\n===========\n\n");
282   compute_costs (true, &rtxes);
283 }
284
285 static bool
286 simple_move_operand (rtx x)
287 {
288   if (GET_CODE (x) == SUBREG)
289     x = SUBREG_REG (x);
290
291   if (!OBJECT_P (x))
292     return false;
293
294   if (GET_CODE (x) == LABEL_REF
295       || GET_CODE (x) == SYMBOL_REF
296       || GET_CODE (x) == HIGH
297       || GET_CODE (x) == CONST)
298     return false;
299
300   if (MEM_P (x)
301       && (MEM_VOLATILE_P (x)
302           || mode_dependent_address_p (XEXP (x, 0), MEM_ADDR_SPACE (x))))
303     return false;
304
305   return true;
306 }
307
308 /* If INSN is a single set between two objects that we want to split,
309    return the single set.  SPEED_P says whether we are optimizing
310    INSN for speed or size.
311
312    INSN should have been passed to recog and extract_insn before this
313    is called.  */
314
315 static rtx
316 simple_move (rtx_insn *insn, bool speed_p)
317 {
318   rtx x;
319   rtx set;
320   machine_mode mode;
321
322   if (recog_data.n_operands != 2)
323     return NULL_RTX;
324
325   set = single_set (insn);
326   if (!set)
327     return NULL_RTX;
328
329   x = SET_DEST (set);
330   if (x != recog_data.operand[0] && x != recog_data.operand[1])
331     return NULL_RTX;
332   if (!simple_move_operand (x))
333     return NULL_RTX;
334
335   x = SET_SRC (set);
336   if (x != recog_data.operand[0] && x != recog_data.operand[1])
337     return NULL_RTX;
338   /* For the src we can handle ASM_OPERANDS, and it is beneficial for
339      things like x86 rdtsc which returns a DImode value.  */
340   if (GET_CODE (x) != ASM_OPERANDS
341       && !simple_move_operand (x))
342     return NULL_RTX;
343
344   /* We try to decompose in integer modes, to avoid generating
345      inefficient code copying between integer and floating point
346      registers.  That means that we can't decompose if this is a
347      non-integer mode for which there is no integer mode of the same
348      size.  */
349   mode = GET_MODE (SET_DEST (set));
350   if (!SCALAR_INT_MODE_P (mode)
351       && (mode_for_size (GET_MODE_SIZE (mode) * BITS_PER_UNIT, MODE_INT, 0)
352           == BLKmode))
353     return NULL_RTX;
354
355   /* Reject PARTIAL_INT modes.  They are used for processor specific
356      purposes and it's probably best not to tamper with them.  */
357   if (GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
358     return NULL_RTX;
359
360   if (!choices[speed_p].move_modes_to_split[(int) mode])
361     return NULL_RTX;
362
363   return set;
364 }
365
366 /* If SET is a copy from one multi-word pseudo-register to another,
367    record that in reg_copy_graph.  Return whether it is such a
368    copy.  */
369
370 static bool
371 find_pseudo_copy (rtx set)
372 {
373   rtx dest = SET_DEST (set);
374   rtx src = SET_SRC (set);
375   unsigned int rd, rs;
376   bitmap b;
377
378   if (!REG_P (dest) || !REG_P (src))
379     return false;
380
381   rd = REGNO (dest);
382   rs = REGNO (src);
383   if (HARD_REGISTER_NUM_P (rd) || HARD_REGISTER_NUM_P (rs))
384     return false;
385
386   b = reg_copy_graph[rs];
387   if (b == NULL)
388     {
389       b = BITMAP_ALLOC (NULL);
390       reg_copy_graph[rs] = b;
391     }
392
393   bitmap_set_bit (b, rd);
394
395   return true;
396 }
397
398 /* Look through the registers in DECOMPOSABLE_CONTEXT.  For each case
399    where they are copied to another register, add the register to
400    which they are copied to DECOMPOSABLE_CONTEXT.  Use
401    NON_DECOMPOSABLE_CONTEXT to limit this--we don't bother to track
402    copies of registers which are in NON_DECOMPOSABLE_CONTEXT.  */
403
404 static void
405 propagate_pseudo_copies (void)
406 {
407   bitmap queue, propagate;
408
409   queue = BITMAP_ALLOC (NULL);
410   propagate = BITMAP_ALLOC (NULL);
411
412   bitmap_copy (queue, decomposable_context);
413   do
414     {
415       bitmap_iterator iter;
416       unsigned int i;
417
418       bitmap_clear (propagate);
419
420       EXECUTE_IF_SET_IN_BITMAP (queue, 0, i, iter)
421         {
422           bitmap b = reg_copy_graph[i];
423           if (b)
424             bitmap_ior_and_compl_into (propagate, b, non_decomposable_context);
425         }
426
427       bitmap_and_compl (queue, propagate, decomposable_context);
428       bitmap_ior_into (decomposable_context, propagate);
429     }
430   while (!bitmap_empty_p (queue));
431
432   BITMAP_FREE (queue);
433   BITMAP_FREE (propagate);
434 }
435
436 /* A pointer to one of these values is passed to
437    find_decomposable_subregs.  */
438
439 enum classify_move_insn
440 {
441   /* Not a simple move from one location to another.  */
442   NOT_SIMPLE_MOVE,
443   /* A simple move we want to decompose.  */
444   DECOMPOSABLE_SIMPLE_MOVE,
445   /* Any other simple move.  */
446   SIMPLE_MOVE
447 };
448
449 /* If we find a SUBREG in *LOC which we could use to decompose a
450    pseudo-register, set a bit in DECOMPOSABLE_CONTEXT.  If we find an
451    unadorned register which is not a simple pseudo-register copy,
452    DATA will point at the type of move, and we set a bit in
453    DECOMPOSABLE_CONTEXT or NON_DECOMPOSABLE_CONTEXT as appropriate.  */
454
455 static void
456 find_decomposable_subregs (rtx *loc, enum classify_move_insn *pcmi)
457 {
458   subrtx_var_iterator::array_type array;
459   FOR_EACH_SUBRTX_VAR (iter, array, *loc, NONCONST)
460     {
461       rtx x = *iter;
462       if (GET_CODE (x) == SUBREG)
463         {
464           rtx inner = SUBREG_REG (x);
465           unsigned int regno, outer_size, inner_size, outer_words, inner_words;
466
467           if (!REG_P (inner))
468             continue;
469
470           regno = REGNO (inner);
471           if (HARD_REGISTER_NUM_P (regno))
472             {
473               iter.skip_subrtxes ();
474               continue;
475             }
476
477           outer_size = GET_MODE_SIZE (GET_MODE (x));
478           inner_size = GET_MODE_SIZE (GET_MODE (inner));
479           outer_words = (outer_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
480           inner_words = (inner_size + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
481
482           /* We only try to decompose single word subregs of multi-word
483              registers.  When we find one, we return -1 to avoid iterating
484              over the inner register.
485
486              ??? This doesn't allow, e.g., DImode subregs of TImode values
487              on 32-bit targets.  We would need to record the way the
488              pseudo-register was used, and only decompose if all the uses
489              were the same number and size of pieces.  Hopefully this
490              doesn't happen much.  */
491
492           if (outer_words == 1 && inner_words > 1)
493             {
494               bitmap_set_bit (decomposable_context, regno);
495               iter.skip_subrtxes ();
496               continue;
497             }
498
499           /* If this is a cast from one mode to another, where the modes
500              have the same size, and they are not tieable, then mark this
501              register as non-decomposable.  If we decompose it we are
502              likely to mess up whatever the backend is trying to do.  */
503           if (outer_words > 1
504               && outer_size == inner_size
505               && !MODES_TIEABLE_P (GET_MODE (x), GET_MODE (inner)))
506             {
507               bitmap_set_bit (non_decomposable_context, regno);
508               bitmap_set_bit (subreg_context, regno);
509               iter.skip_subrtxes ();
510               continue;
511             }
512         }
513       else if (REG_P (x))
514         {
515           unsigned int regno;
516
517           /* We will see an outer SUBREG before we see the inner REG, so
518              when we see a plain REG here it means a direct reference to
519              the register.
520
521              If this is not a simple copy from one location to another,
522              then we can not decompose this register.  If this is a simple
523              copy we want to decompose, and the mode is right,
524              then we mark the register as decomposable.
525              Otherwise we don't say anything about this register --
526              it could be decomposed, but whether that would be
527              profitable depends upon how it is used elsewhere.
528
529              We only set bits in the bitmap for multi-word
530              pseudo-registers, since those are the only ones we care about
531              and it keeps the size of the bitmaps down.  */
532
533           regno = REGNO (x);
534           if (!HARD_REGISTER_NUM_P (regno)
535               && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
536             {
537               switch (*pcmi)
538                 {
539                 case NOT_SIMPLE_MOVE:
540                   bitmap_set_bit (non_decomposable_context, regno);
541                   break;
542                 case DECOMPOSABLE_SIMPLE_MOVE:
543                   if (MODES_TIEABLE_P (GET_MODE (x), word_mode))
544                     bitmap_set_bit (decomposable_context, regno);
545                   break;
546                 case SIMPLE_MOVE:
547                   break;
548                 default:
549                   gcc_unreachable ();
550                 }
551             }
552         }
553       else if (MEM_P (x))
554         {
555           enum classify_move_insn cmi_mem = NOT_SIMPLE_MOVE;
556
557           /* Any registers used in a MEM do not participate in a
558              SIMPLE_MOVE or DECOMPOSABLE_SIMPLE_MOVE.  Do our own recursion
559              here, and return -1 to block the parent's recursion.  */
560           find_decomposable_subregs (&XEXP (x, 0), &cmi_mem);
561           iter.skip_subrtxes ();
562         }
563     }
564 }
565
566 /* Decompose REGNO into word-sized components.  We smash the REG node
567    in place.  This ensures that (1) something goes wrong quickly if we
568    fail to make some replacement, and (2) the debug information inside
569    the symbol table is automatically kept up to date.  */
570
571 static void
572 decompose_register (unsigned int regno)
573 {
574   rtx reg;
575   unsigned int words, i;
576   rtvec v;
577
578   reg = regno_reg_rtx[regno];
579
580   regno_reg_rtx[regno] = NULL_RTX;
581
582   words = GET_MODE_SIZE (GET_MODE (reg));
583   words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
584
585   v = rtvec_alloc (words);
586   for (i = 0; i < words; ++i)
587     RTVEC_ELT (v, i) = gen_reg_rtx_offset (reg, word_mode, i * UNITS_PER_WORD);
588
589   PUT_CODE (reg, CONCATN);
590   XVEC (reg, 0) = v;
591
592   if (dump_file)
593     {
594       fprintf (dump_file, "; Splitting reg %u ->", regno);
595       for (i = 0; i < words; ++i)
596         fprintf (dump_file, " %u", REGNO (XVECEXP (reg, 0, i)));
597       fputc ('\n', dump_file);
598     }
599 }
600
601 /* Get a SUBREG of a CONCATN.  */
602
603 static rtx
604 simplify_subreg_concatn (machine_mode outermode, rtx op,
605                          unsigned int byte)
606 {
607   unsigned int inner_size;
608   machine_mode innermode, partmode;
609   rtx part;
610   unsigned int final_offset;
611
612   gcc_assert (GET_CODE (op) == CONCATN);
613   gcc_assert (byte % GET_MODE_SIZE (outermode) == 0);
614
615   innermode = GET_MODE (op);
616   gcc_assert (byte < GET_MODE_SIZE (innermode));
617   if (GET_MODE_SIZE (outermode) > GET_MODE_SIZE (innermode))
618     return NULL_RTX;
619
620   inner_size = GET_MODE_SIZE (innermode) / XVECLEN (op, 0);
621   part = XVECEXP (op, 0, byte / inner_size);
622   partmode = GET_MODE (part);
623
624   /* VECTOR_CSTs in debug expressions are expanded into CONCATN instead of
625      regular CONST_VECTORs.  They have vector or integer modes, depending
626      on the capabilities of the target.  Cope with them.  */
627   if (partmode == VOIDmode && VECTOR_MODE_P (innermode))
628     partmode = GET_MODE_INNER (innermode);
629   else if (partmode == VOIDmode)
630     {
631       enum mode_class mclass = GET_MODE_CLASS (innermode);
632       partmode = mode_for_size (inner_size * BITS_PER_UNIT, mclass, 0);
633     }
634
635   final_offset = byte % inner_size;
636   if (final_offset + GET_MODE_SIZE (outermode) > inner_size)
637     return NULL_RTX;
638
639   return simplify_gen_subreg (outermode, part, partmode, final_offset);
640 }
641
642 /* Wrapper around simplify_gen_subreg which handles CONCATN.  */
643
644 static rtx
645 simplify_gen_subreg_concatn (machine_mode outermode, rtx op,
646                              machine_mode innermode, unsigned int byte)
647 {
648   rtx ret;
649
650   /* We have to handle generating a SUBREG of a SUBREG of a CONCATN.
651      If OP is a SUBREG of a CONCATN, then it must be a simple mode
652      change with the same size and offset 0, or it must extract a
653      part.  We shouldn't see anything else here.  */
654   if (GET_CODE (op) == SUBREG && GET_CODE (SUBREG_REG (op)) == CONCATN)
655     {
656       rtx op2;
657
658       if ((GET_MODE_SIZE (GET_MODE (op))
659            == GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))))
660           && SUBREG_BYTE (op) == 0)
661         return simplify_gen_subreg_concatn (outermode, SUBREG_REG (op),
662                                             GET_MODE (SUBREG_REG (op)), byte);
663
664       op2 = simplify_subreg_concatn (GET_MODE (op), SUBREG_REG (op),
665                                      SUBREG_BYTE (op));
666       if (op2 == NULL_RTX)
667         {
668           /* We don't handle paradoxical subregs here.  */
669           gcc_assert (GET_MODE_SIZE (outermode)
670                       <= GET_MODE_SIZE (GET_MODE (op)));
671           gcc_assert (GET_MODE_SIZE (GET_MODE (op))
672                       <= GET_MODE_SIZE (GET_MODE (SUBREG_REG (op))));
673           op2 = simplify_subreg_concatn (outermode, SUBREG_REG (op),
674                                          byte + SUBREG_BYTE (op));
675           gcc_assert (op2 != NULL_RTX);
676           return op2;
677         }
678
679       op = op2;
680       gcc_assert (op != NULL_RTX);
681       gcc_assert (innermode == GET_MODE (op));
682     }
683
684   if (GET_CODE (op) == CONCATN)
685     return simplify_subreg_concatn (outermode, op, byte);
686
687   ret = simplify_gen_subreg (outermode, op, innermode, byte);
688
689   /* If we see an insn like (set (reg:DI) (subreg:DI (reg:SI) 0)) then
690      resolve_simple_move will ask for the high part of the paradoxical
691      subreg, which does not have a value.  Just return a zero.  */
692   if (ret == NULL_RTX
693       && GET_CODE (op) == SUBREG
694       && SUBREG_BYTE (op) == 0
695       && (GET_MODE_SIZE (innermode)
696           > GET_MODE_SIZE (GET_MODE (SUBREG_REG (op)))))
697     return CONST0_RTX (outermode);
698
699   gcc_assert (ret != NULL_RTX);
700   return ret;
701 }
702
703 /* Return whether we should resolve X into the registers into which it
704    was decomposed.  */
705
706 static bool
707 resolve_reg_p (rtx x)
708 {
709   return GET_CODE (x) == CONCATN;
710 }
711
712 /* Return whether X is a SUBREG of a register which we need to
713    resolve.  */
714
715 static bool
716 resolve_subreg_p (rtx x)
717 {
718   if (GET_CODE (x) != SUBREG)
719     return false;
720   return resolve_reg_p (SUBREG_REG (x));
721 }
722
723 /* Look for SUBREGs in *LOC which need to be decomposed.  */
724
725 static bool
726 resolve_subreg_use (rtx *loc, rtx insn)
727 {
728   subrtx_ptr_iterator::array_type array;
729   FOR_EACH_SUBRTX_PTR (iter, array, loc, NONCONST)
730     {
731       rtx *loc = *iter;
732       rtx x = *loc;
733       if (resolve_subreg_p (x))
734         {
735           x = simplify_subreg_concatn (GET_MODE (x), SUBREG_REG (x),
736                                        SUBREG_BYTE (x));
737
738           /* It is possible for a note to contain a reference which we can
739              decompose.  In this case, return 1 to the caller to indicate
740              that the note must be removed.  */
741           if (!x)
742             {
743               gcc_assert (!insn);
744               return true;
745             }
746
747           validate_change (insn, loc, x, 1);
748           iter.skip_subrtxes ();
749         }
750       else if (resolve_reg_p (x))
751         /* Return 1 to the caller to indicate that we found a direct
752            reference to a register which is being decomposed.  This can
753            happen inside notes, multiword shift or zero-extend
754            instructions.  */
755         return true;
756     }
757
758   return false;
759 }
760
761 /* Resolve any decomposed registers which appear in register notes on
762    INSN.  */
763
764 static void
765 resolve_reg_notes (rtx_insn *insn)
766 {
767   rtx *pnote, note;
768
769   note = find_reg_equal_equiv_note (insn);
770   if (note)
771     {
772       int old_count = num_validated_changes ();
773       if (resolve_subreg_use (&XEXP (note, 0), NULL_RTX))
774         remove_note (insn, note);
775       else
776         if (old_count != num_validated_changes ())
777           df_notes_rescan (insn);
778     }
779
780   pnote = &REG_NOTES (insn);
781   while (*pnote != NULL_RTX)
782     {
783       bool del = false;
784
785       note = *pnote;
786       switch (REG_NOTE_KIND (note))
787         {
788         case REG_DEAD:
789         case REG_UNUSED:
790           if (resolve_reg_p (XEXP (note, 0)))
791             del = true;
792           break;
793
794         default:
795           break;
796         }
797
798       if (del)
799         *pnote = XEXP (note, 1);
800       else
801         pnote = &XEXP (note, 1);
802     }
803 }
804
805 /* Return whether X can be decomposed into subwords.  */
806
807 static bool
808 can_decompose_p (rtx x)
809 {
810   if (REG_P (x))
811     {
812       unsigned int regno = REGNO (x);
813
814       if (HARD_REGISTER_NUM_P (regno))
815         {
816           unsigned int byte, num_bytes;
817
818           num_bytes = GET_MODE_SIZE (GET_MODE (x));
819           for (byte = 0; byte < num_bytes; byte += UNITS_PER_WORD)
820             if (simplify_subreg_regno (regno, GET_MODE (x), byte, word_mode) < 0)
821               return false;
822           return true;
823         }
824       else
825         return !bitmap_bit_p (subreg_context, regno);
826     }
827
828   return true;
829 }
830
831 /* Decompose the registers used in a simple move SET within INSN.  If
832    we don't change anything, return INSN, otherwise return the start
833    of the sequence of moves.  */
834
835 static rtx_insn *
836 resolve_simple_move (rtx set, rtx_insn *insn)
837 {
838   rtx src, dest, real_dest;
839   rtx_insn *insns;
840   machine_mode orig_mode, dest_mode;
841   unsigned int words;
842   bool pushing;
843
844   src = SET_SRC (set);
845   dest = SET_DEST (set);
846   orig_mode = GET_MODE (dest);
847
848   words = (GET_MODE_SIZE (orig_mode) + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
849   gcc_assert (words > 1);
850
851   start_sequence ();
852
853   /* We have to handle copying from a SUBREG of a decomposed reg where
854      the SUBREG is larger than word size.  Rather than assume that we
855      can take a word_mode SUBREG of the destination, we copy to a new
856      register and then copy that to the destination.  */
857
858   real_dest = NULL_RTX;
859
860   if (GET_CODE (src) == SUBREG
861       && resolve_reg_p (SUBREG_REG (src))
862       && (SUBREG_BYTE (src) != 0
863           || (GET_MODE_SIZE (orig_mode)
864               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (src))))))
865     {
866       real_dest = dest;
867       dest = gen_reg_rtx (orig_mode);
868       if (REG_P (real_dest))
869         REG_ATTRS (dest) = REG_ATTRS (real_dest);
870     }
871
872   /* Similarly if we are copying to a SUBREG of a decomposed reg where
873      the SUBREG is larger than word size.  */
874
875   if (GET_CODE (dest) == SUBREG
876       && resolve_reg_p (SUBREG_REG (dest))
877       && (SUBREG_BYTE (dest) != 0
878           || (GET_MODE_SIZE (orig_mode)
879               != GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest))))))
880     {
881       rtx reg, smove;
882       rtx_insn *minsn;
883
884       reg = gen_reg_rtx (orig_mode);
885       minsn = emit_move_insn (reg, src);
886       smove = single_set (minsn);
887       gcc_assert (smove != NULL_RTX);
888       resolve_simple_move (smove, minsn);
889       src = reg;
890     }
891
892   /* If we didn't have any big SUBREGS of decomposed registers, and
893      neither side of the move is a register we are decomposing, then
894      we don't have to do anything here.  */
895
896   if (src == SET_SRC (set)
897       && dest == SET_DEST (set)
898       && !resolve_reg_p (src)
899       && !resolve_subreg_p (src)
900       && !resolve_reg_p (dest)
901       && !resolve_subreg_p (dest))
902     {
903       end_sequence ();
904       return insn;
905     }
906
907   /* It's possible for the code to use a subreg of a decomposed
908      register while forming an address.  We need to handle that before
909      passing the address to emit_move_insn.  We pass NULL_RTX as the
910      insn parameter to resolve_subreg_use because we can not validate
911      the insn yet.  */
912   if (MEM_P (src) || MEM_P (dest))
913     {
914       int acg;
915
916       if (MEM_P (src))
917         resolve_subreg_use (&XEXP (src, 0), NULL_RTX);
918       if (MEM_P (dest))
919         resolve_subreg_use (&XEXP (dest, 0), NULL_RTX);
920       acg = apply_change_group ();
921       gcc_assert (acg);
922     }
923
924   /* If SRC is a register which we can't decompose, or has side
925      effects, we need to move via a temporary register.  */
926
927   if (!can_decompose_p (src)
928       || side_effects_p (src)
929       || GET_CODE (src) == ASM_OPERANDS)
930     {
931       rtx reg;
932
933       reg = gen_reg_rtx (orig_mode);
934
935       if (AUTO_INC_DEC)
936         {
937           rtx move = emit_move_insn (reg, src);
938           if (MEM_P (src))
939             {
940               rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
941               if (note)
942                 add_reg_note (move, REG_INC, XEXP (note, 0));
943             }
944         }
945       else
946         emit_move_insn (reg, src);
947
948       src = reg;
949     }
950
951   /* If DEST is a register which we can't decompose, or has side
952      effects, we need to first move to a temporary register.  We
953      handle the common case of pushing an operand directly.  We also
954      go through a temporary register if it holds a floating point
955      value.  This gives us better code on systems which can't move
956      data easily between integer and floating point registers.  */
957
958   dest_mode = orig_mode;
959   pushing = push_operand (dest, dest_mode);
960   if (!can_decompose_p (dest)
961       || (side_effects_p (dest) && !pushing)
962       || (!SCALAR_INT_MODE_P (dest_mode)
963           && !resolve_reg_p (dest)
964           && !resolve_subreg_p (dest)))
965     {
966       if (real_dest == NULL_RTX)
967         real_dest = dest;
968       if (!SCALAR_INT_MODE_P (dest_mode))
969         {
970           dest_mode = mode_for_size (GET_MODE_SIZE (dest_mode) * BITS_PER_UNIT,
971                                      MODE_INT, 0);
972           gcc_assert (dest_mode != BLKmode);
973         }
974       dest = gen_reg_rtx (dest_mode);
975       if (REG_P (real_dest))
976         REG_ATTRS (dest) = REG_ATTRS (real_dest);
977     }
978
979   if (pushing)
980     {
981       unsigned int i, j, jinc;
982
983       gcc_assert (GET_MODE_SIZE (orig_mode) % UNITS_PER_WORD == 0);
984       gcc_assert (GET_CODE (XEXP (dest, 0)) != PRE_MODIFY);
985       gcc_assert (GET_CODE (XEXP (dest, 0)) != POST_MODIFY);
986
987       if (WORDS_BIG_ENDIAN == STACK_GROWS_DOWNWARD)
988         {
989           j = 0;
990           jinc = 1;
991         }
992       else
993         {
994           j = words - 1;
995           jinc = -1;
996         }
997
998       for (i = 0; i < words; ++i, j += jinc)
999         {
1000           rtx temp;
1001
1002           temp = copy_rtx (XEXP (dest, 0));
1003           temp = adjust_automodify_address_nv (dest, word_mode, temp,
1004                                                j * UNITS_PER_WORD);
1005           emit_move_insn (temp,
1006                           simplify_gen_subreg_concatn (word_mode, src,
1007                                                        orig_mode,
1008                                                        j * UNITS_PER_WORD));
1009         }
1010     }
1011   else
1012     {
1013       unsigned int i;
1014
1015       if (REG_P (dest) && !HARD_REGISTER_NUM_P (REGNO (dest)))
1016         emit_clobber (dest);
1017
1018       for (i = 0; i < words; ++i)
1019         emit_move_insn (simplify_gen_subreg_concatn (word_mode, dest,
1020                                                      dest_mode,
1021                                                      i * UNITS_PER_WORD),
1022                         simplify_gen_subreg_concatn (word_mode, src,
1023                                                      orig_mode,
1024                                                      i * UNITS_PER_WORD));
1025     }
1026
1027   if (real_dest != NULL_RTX)
1028     {
1029       rtx mdest, smove;
1030       rtx_insn *minsn;
1031
1032       if (dest_mode == orig_mode)
1033         mdest = dest;
1034       else
1035         mdest = simplify_gen_subreg (orig_mode, dest, GET_MODE (dest), 0);
1036       minsn = emit_move_insn (real_dest, mdest);
1037
1038   if (AUTO_INC_DEC && MEM_P (real_dest)
1039       && !(resolve_reg_p (real_dest) || resolve_subreg_p (real_dest)))
1040     {
1041       rtx note = find_reg_note (insn, REG_INC, NULL_RTX);
1042       if (note)
1043         add_reg_note (minsn, REG_INC, XEXP (note, 0));
1044     }
1045
1046       smove = single_set (minsn);
1047       gcc_assert (smove != NULL_RTX);
1048
1049       resolve_simple_move (smove, minsn);
1050     }
1051
1052   insns = get_insns ();
1053   end_sequence ();
1054
1055   copy_reg_eh_region_note_forward (insn, insns, NULL_RTX);
1056
1057   emit_insn_before (insns, insn);
1058
1059   /* If we get here via self-recursion, then INSN is not yet in the insns
1060      chain and delete_insn will fail.  We only want to remove INSN from the
1061      current sequence.  See PR56738.  */
1062   if (in_sequence_p ())
1063     remove_insn (insn);
1064   else
1065     delete_insn (insn);
1066
1067   return insns;
1068 }
1069
1070 /* Change a CLOBBER of a decomposed register into a CLOBBER of the
1071    component registers.  Return whether we changed something.  */
1072
1073 static bool
1074 resolve_clobber (rtx pat, rtx_insn *insn)
1075 {
1076   rtx reg;
1077   machine_mode orig_mode;
1078   unsigned int words, i;
1079   int ret;
1080
1081   reg = XEXP (pat, 0);
1082   if (!resolve_reg_p (reg) && !resolve_subreg_p (reg))
1083     return false;
1084
1085   orig_mode = GET_MODE (reg);
1086   words = GET_MODE_SIZE (orig_mode);
1087   words = (words + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
1088
1089   ret = validate_change (NULL_RTX, &XEXP (pat, 0),
1090                          simplify_gen_subreg_concatn (word_mode, reg,
1091                                                       orig_mode, 0),
1092                          0);
1093   df_insn_rescan (insn);
1094   gcc_assert (ret != 0);
1095
1096   for (i = words - 1; i > 0; --i)
1097     {
1098       rtx x;
1099
1100       x = simplify_gen_subreg_concatn (word_mode, reg, orig_mode,
1101                                        i * UNITS_PER_WORD);
1102       x = gen_rtx_CLOBBER (VOIDmode, x);
1103       emit_insn_after (x, insn);
1104     }
1105
1106   resolve_reg_notes (insn);
1107
1108   return true;
1109 }
1110
1111 /* A USE of a decomposed register is no longer meaningful.  Return
1112    whether we changed something.  */
1113
1114 static bool
1115 resolve_use (rtx pat, rtx_insn *insn)
1116 {
1117   if (resolve_reg_p (XEXP (pat, 0)) || resolve_subreg_p (XEXP (pat, 0)))
1118     {
1119       delete_insn (insn);
1120       return true;
1121     }
1122
1123   resolve_reg_notes (insn);
1124
1125   return false;
1126 }
1127
1128 /* A VAR_LOCATION can be simplified.  */
1129
1130 static void
1131 resolve_debug (rtx_insn *insn)
1132 {
1133   subrtx_ptr_iterator::array_type array;
1134   FOR_EACH_SUBRTX_PTR (iter, array, &PATTERN (insn), NONCONST)
1135     {
1136       rtx *loc = *iter;
1137       rtx x = *loc;
1138       if (resolve_subreg_p (x))
1139         {
1140           x = simplify_subreg_concatn (GET_MODE (x), SUBREG_REG (x),
1141                                        SUBREG_BYTE (x));
1142
1143           if (x)
1144             *loc = x;
1145           else
1146             x = copy_rtx (*loc);
1147         }
1148       if (resolve_reg_p (x))
1149         *loc = copy_rtx (x);
1150     }
1151
1152   df_insn_rescan (insn);
1153
1154   resolve_reg_notes (insn);
1155 }
1156
1157 /* Check if INSN is a decomposable multiword-shift or zero-extend and
1158    set the decomposable_context bitmap accordingly.  SPEED_P is true
1159    if we are optimizing INSN for speed rather than size.  Return true
1160    if INSN is decomposable.  */
1161
1162 static bool
1163 find_decomposable_shift_zext (rtx_insn *insn, bool speed_p)
1164 {
1165   rtx set;
1166   rtx op;
1167   rtx op_operand;
1168
1169   set = single_set (insn);
1170   if (!set)
1171     return false;
1172
1173   op = SET_SRC (set);
1174   if (GET_CODE (op) != ASHIFT
1175       && GET_CODE (op) != LSHIFTRT
1176       && GET_CODE (op) != ASHIFTRT
1177       && GET_CODE (op) != ZERO_EXTEND)
1178     return false;
1179
1180   op_operand = XEXP (op, 0);
1181   if (!REG_P (SET_DEST (set)) || !REG_P (op_operand)
1182       || HARD_REGISTER_NUM_P (REGNO (SET_DEST (set)))
1183       || HARD_REGISTER_NUM_P (REGNO (op_operand))
1184       || GET_MODE (op) != twice_word_mode)
1185     return false;
1186
1187   if (GET_CODE (op) == ZERO_EXTEND)
1188     {
1189       if (GET_MODE (op_operand) != word_mode
1190           || !choices[speed_p].splitting_zext)
1191         return false;
1192     }
1193   else /* left or right shift */
1194     {
1195       bool *splitting = (GET_CODE (op) == ASHIFT
1196                          ? choices[speed_p].splitting_ashift
1197                          : GET_CODE (op) == ASHIFTRT
1198                          ? choices[speed_p].splitting_ashiftrt
1199                          : choices[speed_p].splitting_lshiftrt);
1200       if (!CONST_INT_P (XEXP (op, 1))
1201           || !IN_RANGE (INTVAL (XEXP (op, 1)), BITS_PER_WORD,
1202                         2 * BITS_PER_WORD - 1)
1203           || !splitting[INTVAL (XEXP (op, 1)) - BITS_PER_WORD])
1204         return false;
1205
1206       bitmap_set_bit (decomposable_context, REGNO (op_operand));
1207     }
1208
1209   bitmap_set_bit (decomposable_context, REGNO (SET_DEST (set)));
1210
1211   return true;
1212 }
1213
1214 /* Decompose a more than word wide shift (in INSN) of a multiword
1215    pseudo or a multiword zero-extend of a wordmode pseudo into a move
1216    and 'set to zero' insn.  Return a pointer to the new insn when a
1217    replacement was done.  */
1218
1219 static rtx_insn *
1220 resolve_shift_zext (rtx_insn *insn)
1221 {
1222   rtx set;
1223   rtx op;
1224   rtx op_operand;
1225   rtx_insn *insns;
1226   rtx src_reg, dest_reg, dest_upper, upper_src = NULL_RTX;
1227   int src_reg_num, dest_reg_num, offset1, offset2, src_offset;
1228
1229   set = single_set (insn);
1230   if (!set)
1231     return NULL;
1232
1233   op = SET_SRC (set);
1234   if (GET_CODE (op) != ASHIFT
1235       && GET_CODE (op) != LSHIFTRT
1236       && GET_CODE (op) != ASHIFTRT
1237       && GET_CODE (op) != ZERO_EXTEND)
1238     return NULL;
1239
1240   op_operand = XEXP (op, 0);
1241
1242   /* We can tear this operation apart only if the regs were already
1243      torn apart.  */
1244   if (!resolve_reg_p (SET_DEST (set)) && !resolve_reg_p (op_operand))
1245     return NULL;
1246
1247   /* src_reg_num is the number of the word mode register which we
1248      are operating on.  For a left shift and a zero_extend on little
1249      endian machines this is register 0.  */
1250   src_reg_num = (GET_CODE (op) == LSHIFTRT || GET_CODE (op) == ASHIFTRT)
1251                 ? 1 : 0;
1252
1253   if (WORDS_BIG_ENDIAN
1254       && GET_MODE_SIZE (GET_MODE (op_operand)) > UNITS_PER_WORD)
1255     src_reg_num = 1 - src_reg_num;
1256
1257   if (GET_CODE (op) == ZERO_EXTEND)
1258     dest_reg_num = WORDS_BIG_ENDIAN ? 1 : 0;
1259   else
1260     dest_reg_num = 1 - src_reg_num;
1261
1262   offset1 = UNITS_PER_WORD * dest_reg_num;
1263   offset2 = UNITS_PER_WORD * (1 - dest_reg_num);
1264   src_offset = UNITS_PER_WORD * src_reg_num;
1265
1266   start_sequence ();
1267
1268   dest_reg = simplify_gen_subreg_concatn (word_mode, SET_DEST (set),
1269                                           GET_MODE (SET_DEST (set)),
1270                                           offset1);
1271   dest_upper = simplify_gen_subreg_concatn (word_mode, SET_DEST (set),
1272                                             GET_MODE (SET_DEST (set)),
1273                                             offset2);
1274   src_reg = simplify_gen_subreg_concatn (word_mode, op_operand,
1275                                          GET_MODE (op_operand),
1276                                          src_offset);
1277   if (GET_CODE (op) == ASHIFTRT
1278       && INTVAL (XEXP (op, 1)) != 2 * BITS_PER_WORD - 1)
1279     upper_src = expand_shift (RSHIFT_EXPR, word_mode, copy_rtx (src_reg),
1280                               BITS_PER_WORD - 1, NULL_RTX, 0);
1281
1282   if (GET_CODE (op) != ZERO_EXTEND)
1283     {
1284       int shift_count = INTVAL (XEXP (op, 1));
1285       if (shift_count > BITS_PER_WORD)
1286         src_reg = expand_shift (GET_CODE (op) == ASHIFT ?
1287                                 LSHIFT_EXPR : RSHIFT_EXPR,
1288                                 word_mode, src_reg,
1289                                 shift_count - BITS_PER_WORD,
1290                                 dest_reg, GET_CODE (op) != ASHIFTRT);
1291     }
1292
1293   if (dest_reg != src_reg)
1294     emit_move_insn (dest_reg, src_reg);
1295   if (GET_CODE (op) != ASHIFTRT)
1296     emit_move_insn (dest_upper, CONST0_RTX (word_mode));
1297   else if (INTVAL (XEXP (op, 1)) == 2 * BITS_PER_WORD - 1)
1298     emit_move_insn (dest_upper, copy_rtx (src_reg));
1299   else
1300     emit_move_insn (dest_upper, upper_src);
1301   insns = get_insns ();
1302
1303   end_sequence ();
1304
1305   emit_insn_before (insns, insn);
1306
1307   if (dump_file)
1308     {
1309       rtx_insn *in;
1310       fprintf (dump_file, "; Replacing insn: %d with insns: ", INSN_UID (insn));
1311       for (in = insns; in != insn; in = NEXT_INSN (in))
1312         fprintf (dump_file, "%d ", INSN_UID (in));
1313       fprintf (dump_file, "\n");
1314     }
1315
1316   delete_insn (insn);
1317   return insns;
1318 }
1319
1320 /* Print to dump_file a description of what we're doing with shift code CODE.
1321    SPLITTING[X] is true if we are splitting shifts by X + BITS_PER_WORD.  */
1322
1323 static void
1324 dump_shift_choices (enum rtx_code code, bool *splitting)
1325 {
1326   int i;
1327   const char *sep;
1328
1329   fprintf (dump_file,
1330            "  Splitting mode %s for %s lowering with shift amounts = ",
1331            GET_MODE_NAME (twice_word_mode), GET_RTX_NAME (code));
1332   sep = "";
1333   for (i = 0; i < BITS_PER_WORD; i++)
1334     if (splitting[i])
1335       {
1336         fprintf (dump_file, "%s%d", sep, i + BITS_PER_WORD);
1337         sep = ",";
1338       }
1339   fprintf (dump_file, "\n");
1340 }
1341
1342 /* Print to dump_file a description of what we're doing when optimizing
1343    for speed or size; SPEED_P says which.  DESCRIPTION is a description
1344    of the SPEED_P choice.  */
1345
1346 static void
1347 dump_choices (bool speed_p, const char *description)
1348 {
1349   unsigned int i;
1350
1351   fprintf (dump_file, "Choices when optimizing for %s:\n", description);
1352
1353   for (i = 0; i < MAX_MACHINE_MODE; i++)
1354     if (GET_MODE_SIZE ((machine_mode) i) > UNITS_PER_WORD)
1355       fprintf (dump_file, "  %s mode %s for copy lowering.\n",
1356                choices[speed_p].move_modes_to_split[i]
1357                ? "Splitting"
1358                : "Skipping",
1359                GET_MODE_NAME ((machine_mode) i));
1360
1361   fprintf (dump_file, "  %s mode %s for zero_extend lowering.\n",
1362            choices[speed_p].splitting_zext ? "Splitting" : "Skipping",
1363            GET_MODE_NAME (twice_word_mode));
1364
1365   dump_shift_choices (ASHIFT, choices[speed_p].splitting_ashift);
1366   dump_shift_choices (LSHIFTRT, choices[speed_p].splitting_lshiftrt);
1367   dump_shift_choices (ASHIFTRT, choices[speed_p].splitting_ashiftrt);
1368   fprintf (dump_file, "\n");
1369 }
1370
1371 /* Look for registers which are always accessed via word-sized SUBREGs
1372    or -if DECOMPOSE_COPIES is true- via copies.  Decompose these
1373    registers into several word-sized pseudo-registers.  */
1374
1375 static void
1376 decompose_multiword_subregs (bool decompose_copies)
1377 {
1378   unsigned int max;
1379   basic_block bb;
1380   bool speed_p;
1381
1382   if (dump_file)
1383     {
1384       dump_choices (false, "size");
1385       dump_choices (true, "speed");
1386     }
1387
1388   /* Check if this target even has any modes to consider lowering.   */
1389   if (!choices[false].something_to_do && !choices[true].something_to_do)
1390     {
1391       if (dump_file)
1392         fprintf (dump_file, "Nothing to do!\n");
1393       return;
1394     }
1395
1396   max = max_reg_num ();
1397
1398   /* First see if there are any multi-word pseudo-registers.  If there
1399      aren't, there is nothing we can do.  This should speed up this
1400      pass in the normal case, since it should be faster than scanning
1401      all the insns.  */
1402   {
1403     unsigned int i;
1404     bool useful_modes_seen = false;
1405
1406     for (i = FIRST_PSEUDO_REGISTER; i < max; ++i)
1407       if (regno_reg_rtx[i] != NULL)
1408         {
1409           machine_mode mode = GET_MODE (regno_reg_rtx[i]);
1410           if (choices[false].move_modes_to_split[(int) mode]
1411               || choices[true].move_modes_to_split[(int) mode])
1412             {
1413               useful_modes_seen = true;
1414               break;
1415             }
1416         }
1417
1418     if (!useful_modes_seen)
1419       {
1420         if (dump_file)
1421           fprintf (dump_file, "Nothing to lower in this function.\n");
1422         return;
1423       }
1424   }
1425
1426   if (df)
1427     {
1428       df_set_flags (DF_DEFER_INSN_RESCAN);
1429       run_word_dce ();
1430     }
1431
1432   /* FIXME: It may be possible to change this code to look for each
1433      multi-word pseudo-register and to find each insn which sets or
1434      uses that register.  That should be faster than scanning all the
1435      insns.  */
1436
1437   decomposable_context = BITMAP_ALLOC (NULL);
1438   non_decomposable_context = BITMAP_ALLOC (NULL);
1439   subreg_context = BITMAP_ALLOC (NULL);
1440
1441   reg_copy_graph.create (max);
1442   reg_copy_graph.safe_grow_cleared (max);
1443   memset (reg_copy_graph.address (), 0, sizeof (bitmap) * max);
1444
1445   speed_p = optimize_function_for_speed_p (cfun);
1446   FOR_EACH_BB_FN (bb, cfun)
1447     {
1448       rtx_insn *insn;
1449
1450       FOR_BB_INSNS (bb, insn)
1451         {
1452           rtx set;
1453           enum classify_move_insn cmi;
1454           int i, n;
1455
1456           if (!INSN_P (insn)
1457               || GET_CODE (PATTERN (insn)) == CLOBBER
1458               || GET_CODE (PATTERN (insn)) == USE)
1459             continue;
1460
1461           recog_memoized (insn);
1462
1463           if (find_decomposable_shift_zext (insn, speed_p))
1464             continue;
1465
1466           extract_insn (insn);
1467
1468           set = simple_move (insn, speed_p);
1469
1470           if (!set)
1471             cmi = NOT_SIMPLE_MOVE;
1472           else
1473             {
1474               /* We mark pseudo-to-pseudo copies as decomposable during the
1475                  second pass only.  The first pass is so early that there is
1476                  good chance such moves will be optimized away completely by
1477                  subsequent optimizations anyway.
1478
1479                  However, we call find_pseudo_copy even during the first pass
1480                  so as to properly set up the reg_copy_graph.  */
1481               if (find_pseudo_copy (set))
1482                 cmi = decompose_copies? DECOMPOSABLE_SIMPLE_MOVE : SIMPLE_MOVE;
1483               else
1484                 cmi = SIMPLE_MOVE;
1485             }
1486
1487           n = recog_data.n_operands;
1488           for (i = 0; i < n; ++i)
1489             {
1490               find_decomposable_subregs (&recog_data.operand[i], &cmi);
1491
1492               /* We handle ASM_OPERANDS as a special case to support
1493                  things like x86 rdtsc which returns a DImode value.
1494                  We can decompose the output, which will certainly be
1495                  operand 0, but not the inputs.  */
1496
1497               if (cmi == SIMPLE_MOVE
1498                   && GET_CODE (SET_SRC (set)) == ASM_OPERANDS)
1499                 {
1500                   gcc_assert (i == 0);
1501                   cmi = NOT_SIMPLE_MOVE;
1502                 }
1503             }
1504         }
1505     }
1506
1507   bitmap_and_compl_into (decomposable_context, non_decomposable_context);
1508   if (!bitmap_empty_p (decomposable_context))
1509     {
1510       sbitmap sub_blocks;
1511       unsigned int i;
1512       sbitmap_iterator sbi;
1513       bitmap_iterator iter;
1514       unsigned int regno;
1515
1516       propagate_pseudo_copies ();
1517
1518       sub_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun));
1519       bitmap_clear (sub_blocks);
1520
1521       EXECUTE_IF_SET_IN_BITMAP (decomposable_context, 0, regno, iter)
1522         decompose_register (regno);
1523
1524       FOR_EACH_BB_FN (bb, cfun)
1525         {
1526           rtx_insn *insn;
1527
1528           FOR_BB_INSNS (bb, insn)
1529             {
1530               rtx pat;
1531
1532               if (!INSN_P (insn))
1533                 continue;
1534
1535               pat = PATTERN (insn);
1536               if (GET_CODE (pat) == CLOBBER)
1537                 resolve_clobber (pat, insn);
1538               else if (GET_CODE (pat) == USE)
1539                 resolve_use (pat, insn);
1540               else if (DEBUG_INSN_P (insn))
1541                 resolve_debug (insn);
1542               else
1543                 {
1544                   rtx set;
1545                   int i;
1546
1547                   recog_memoized (insn);
1548                   extract_insn (insn);
1549
1550                   set = simple_move (insn, speed_p);
1551                   if (set)
1552                     {
1553                       rtx_insn *orig_insn = insn;
1554                       bool cfi = control_flow_insn_p (insn);
1555
1556                       /* We can end up splitting loads to multi-word pseudos
1557                          into separate loads to machine word size pseudos.
1558                          When this happens, we first had one load that can
1559                          throw, and after resolve_simple_move we'll have a
1560                          bunch of loads (at least two).  All those loads may
1561                          trap if we can have non-call exceptions, so they
1562                          all will end the current basic block.  We split the
1563                          block after the outer loop over all insns, but we
1564                          make sure here that we will be able to split the
1565                          basic block and still produce the correct control
1566                          flow graph for it.  */
1567                       gcc_assert (!cfi
1568                                   || (cfun->can_throw_non_call_exceptions
1569                                       && can_throw_internal (insn)));
1570
1571                       insn = resolve_simple_move (set, insn);
1572                       if (insn != orig_insn)
1573                         {
1574                           recog_memoized (insn);
1575                           extract_insn (insn);
1576
1577                           if (cfi)
1578                             bitmap_set_bit (sub_blocks, bb->index);
1579                         }
1580                     }
1581                   else
1582                     {
1583                       rtx_insn *decomposed_shift;
1584
1585                       decomposed_shift = resolve_shift_zext (insn);
1586                       if (decomposed_shift != NULL_RTX)
1587                         {
1588                           insn = decomposed_shift;
1589                           recog_memoized (insn);
1590                           extract_insn (insn);
1591                         }
1592                     }
1593
1594                   for (i = recog_data.n_operands - 1; i >= 0; --i)
1595                     resolve_subreg_use (recog_data.operand_loc[i], insn);
1596
1597                   resolve_reg_notes (insn);
1598
1599                   if (num_validated_changes () > 0)
1600                     {
1601                       for (i = recog_data.n_dups - 1; i >= 0; --i)
1602                         {
1603                           rtx *pl = recog_data.dup_loc[i];
1604                           int dup_num = recog_data.dup_num[i];
1605                           rtx *px = recog_data.operand_loc[dup_num];
1606
1607                           validate_unshare_change (insn, pl, *px, 1);
1608                         }
1609
1610                       i = apply_change_group ();
1611                       gcc_assert (i);
1612                     }
1613                 }
1614             }
1615         }
1616
1617       /* If we had insns to split that caused control flow insns in the middle
1618          of a basic block, split those blocks now.  Note that we only handle
1619          the case where splitting a load has caused multiple possibly trapping
1620          loads to appear.  */
1621       EXECUTE_IF_SET_IN_BITMAP (sub_blocks, 0, i, sbi)
1622         {
1623           rtx_insn *insn, *end;
1624           edge fallthru;
1625
1626           bb = BASIC_BLOCK_FOR_FN (cfun, i);
1627           insn = BB_HEAD (bb);
1628           end = BB_END (bb);
1629
1630           while (insn != end)
1631             {
1632               if (control_flow_insn_p (insn))
1633                 {
1634                   /* Split the block after insn.  There will be a fallthru
1635                      edge, which is OK so we keep it.  We have to create the
1636                      exception edges ourselves.  */
1637                   fallthru = split_block (bb, insn);
1638                   rtl_make_eh_edge (NULL, bb, BB_END (bb));
1639                   bb = fallthru->dest;
1640                   insn = BB_HEAD (bb);
1641                 }
1642               else
1643                 insn = NEXT_INSN (insn);
1644             }
1645         }
1646
1647       sbitmap_free (sub_blocks);
1648     }
1649
1650   {
1651     unsigned int i;
1652     bitmap b;
1653
1654     FOR_EACH_VEC_ELT (reg_copy_graph, i, b)
1655       if (b)
1656         BITMAP_FREE (b);
1657   }
1658
1659   reg_copy_graph.release ();
1660
1661   BITMAP_FREE (decomposable_context);
1662   BITMAP_FREE (non_decomposable_context);
1663   BITMAP_FREE (subreg_context);
1664 }
1665 \f
1666 /* Implement first lower subreg pass.  */
1667
1668 namespace {
1669
1670 const pass_data pass_data_lower_subreg =
1671 {
1672   RTL_PASS, /* type */
1673   "subreg1", /* name */
1674   OPTGROUP_NONE, /* optinfo_flags */
1675   TV_LOWER_SUBREG, /* tv_id */
1676   0, /* properties_required */
1677   0, /* properties_provided */
1678   0, /* properties_destroyed */
1679   0, /* todo_flags_start */
1680   0, /* todo_flags_finish */
1681 };
1682
1683 class pass_lower_subreg : public rtl_opt_pass
1684 {
1685 public:
1686   pass_lower_subreg (gcc::context *ctxt)
1687     : rtl_opt_pass (pass_data_lower_subreg, ctxt)
1688   {}
1689
1690   /* opt_pass methods: */
1691   virtual bool gate (function *) { return flag_split_wide_types != 0; }
1692   virtual unsigned int execute (function *)
1693     {
1694       decompose_multiword_subregs (false);
1695       return 0;
1696     }
1697
1698 }; // class pass_lower_subreg
1699
1700 } // anon namespace
1701
1702 rtl_opt_pass *
1703 make_pass_lower_subreg (gcc::context *ctxt)
1704 {
1705   return new pass_lower_subreg (ctxt);
1706 }
1707
1708 /* Implement second lower subreg pass.  */
1709
1710 namespace {
1711
1712 const pass_data pass_data_lower_subreg2 =
1713 {
1714   RTL_PASS, /* type */
1715   "subreg2", /* name */
1716   OPTGROUP_NONE, /* optinfo_flags */
1717   TV_LOWER_SUBREG, /* tv_id */
1718   0, /* properties_required */
1719   0, /* properties_provided */
1720   0, /* properties_destroyed */
1721   0, /* todo_flags_start */
1722   TODO_df_finish, /* todo_flags_finish */
1723 };
1724
1725 class pass_lower_subreg2 : public rtl_opt_pass
1726 {
1727 public:
1728   pass_lower_subreg2 (gcc::context *ctxt)
1729     : rtl_opt_pass (pass_data_lower_subreg2, ctxt)
1730   {}
1731
1732   /* opt_pass methods: */
1733   virtual bool gate (function *) { return flag_split_wide_types != 0; }
1734   virtual unsigned int execute (function *)
1735     {
1736       decompose_multiword_subregs (true);
1737       return 0;
1738     }
1739
1740 }; // class pass_lower_subreg2
1741
1742 } // anon namespace
1743
1744 rtl_opt_pass *
1745 make_pass_lower_subreg2 (gcc::context *ctxt)
1746 {
1747   return new pass_lower_subreg2 (ctxt);
1748 }