re PR bootstrap/53249 (Bootstrap failure)
[platform/upstream/gcc.git] / gcc / expr.c
1 /* Convert tree expression to rtl instructions, for GNU compiler.
2    Copyright (C) 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3    2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011,
4    2012 Free Software Foundation, Inc.
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 "tm.h"
26 #include "machmode.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "flags.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "except.h"
33 #include "function.h"
34 #include "insn-config.h"
35 #include "insn-attr.h"
36 /* Include expr.h after insn-config.h so we get HAVE_conditional_move.  */
37 #include "expr.h"
38 #include "optabs.h"
39 #include "libfuncs.h"
40 #include "recog.h"
41 #include "reload.h"
42 #include "output.h"
43 #include "typeclass.h"
44 #include "toplev.h"
45 #include "langhooks.h"
46 #include "intl.h"
47 #include "tm_p.h"
48 #include "tree-iterator.h"
49 #include "tree-pass.h"
50 #include "tree-flow.h"
51 #include "target.h"
52 #include "common/common-target.h"
53 #include "timevar.h"
54 #include "df.h"
55 #include "diagnostic.h"
56 #include "ssaexpand.h"
57 #include "target-globals.h"
58 #include "params.h"
59
60 /* Decide whether a function's arguments should be processed
61    from first to last or from last to first.
62
63    They should if the stack and args grow in opposite directions, but
64    only if we have push insns.  */
65
66 #ifdef PUSH_ROUNDING
67
68 #ifndef PUSH_ARGS_REVERSED
69 #if defined (STACK_GROWS_DOWNWARD) != defined (ARGS_GROW_DOWNWARD)
70 #define PUSH_ARGS_REVERSED      /* If it's last to first.  */
71 #endif
72 #endif
73
74 #endif
75
76 #ifndef STACK_PUSH_CODE
77 #ifdef STACK_GROWS_DOWNWARD
78 #define STACK_PUSH_CODE PRE_DEC
79 #else
80 #define STACK_PUSH_CODE PRE_INC
81 #endif
82 #endif
83
84
85 /* If this is nonzero, we do not bother generating VOLATILE
86    around volatile memory references, and we are willing to
87    output indirect addresses.  If cse is to follow, we reject
88    indirect addresses so a useful potential cse is generated;
89    if it is used only once, instruction combination will produce
90    the same indirect address eventually.  */
91 int cse_not_expected;
92
93 /* This structure is used by move_by_pieces to describe the move to
94    be performed.  */
95 struct move_by_pieces_d
96 {
97   rtx to;
98   rtx to_addr;
99   int autinc_to;
100   int explicit_inc_to;
101   rtx from;
102   rtx from_addr;
103   int autinc_from;
104   int explicit_inc_from;
105   unsigned HOST_WIDE_INT len;
106   HOST_WIDE_INT offset;
107   int reverse;
108 };
109
110 /* This structure is used by store_by_pieces to describe the clear to
111    be performed.  */
112
113 struct store_by_pieces_d
114 {
115   rtx to;
116   rtx to_addr;
117   int autinc_to;
118   int explicit_inc_to;
119   unsigned HOST_WIDE_INT len;
120   HOST_WIDE_INT offset;
121   rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode);
122   void *constfundata;
123   int reverse;
124 };
125
126 static void move_by_pieces_1 (rtx (*) (rtx, ...), enum machine_mode,
127                               struct move_by_pieces_d *);
128 static bool block_move_libcall_safe_for_call_parm (void);
129 static bool emit_block_move_via_movmem (rtx, rtx, rtx, unsigned, unsigned, HOST_WIDE_INT);
130 static tree emit_block_move_libcall_fn (int);
131 static void emit_block_move_via_loop (rtx, rtx, rtx, unsigned);
132 static rtx clear_by_pieces_1 (void *, HOST_WIDE_INT, enum machine_mode);
133 static void clear_by_pieces (rtx, unsigned HOST_WIDE_INT, unsigned int);
134 static void store_by_pieces_1 (struct store_by_pieces_d *, unsigned int);
135 static void store_by_pieces_2 (rtx (*) (rtx, ...), enum machine_mode,
136                                struct store_by_pieces_d *);
137 static tree clear_storage_libcall_fn (int);
138 static rtx compress_float_constant (rtx, rtx);
139 static rtx get_subtarget (rtx);
140 static void store_constructor_field (rtx, unsigned HOST_WIDE_INT,
141                                      HOST_WIDE_INT, enum machine_mode,
142                                      tree, tree, int, alias_set_type);
143 static void store_constructor (tree, rtx, int, HOST_WIDE_INT);
144 static rtx store_field (rtx, HOST_WIDE_INT, HOST_WIDE_INT,
145                         unsigned HOST_WIDE_INT, unsigned HOST_WIDE_INT,
146                         enum machine_mode,
147                         tree, tree, alias_set_type, bool);
148
149 static unsigned HOST_WIDE_INT highest_pow2_factor_for_target (const_tree, const_tree);
150
151 static int is_aligning_offset (const_tree, const_tree);
152 static void expand_operands (tree, tree, rtx, rtx*, rtx*,
153                              enum expand_modifier);
154 static rtx reduce_to_bit_field_precision (rtx, rtx, tree);
155 static rtx do_store_flag (sepops, rtx, enum machine_mode);
156 #ifdef PUSH_ROUNDING
157 static void emit_single_push_insn (enum machine_mode, rtx, tree);
158 #endif
159 static void do_tablejump (rtx, enum machine_mode, rtx, rtx, rtx);
160 static rtx const_vector_from_tree (tree);
161 static void write_complex_part (rtx, rtx, bool);
162
163 /* This macro is used to determine whether move_by_pieces should be called
164    to perform a structure copy.  */
165 #ifndef MOVE_BY_PIECES_P
166 #define MOVE_BY_PIECES_P(SIZE, ALIGN) \
167   (move_by_pieces_ninsns (SIZE, ALIGN, MOVE_MAX_PIECES + 1) \
168    < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
169 #endif
170
171 /* This macro is used to determine whether clear_by_pieces should be
172    called to clear storage.  */
173 #ifndef CLEAR_BY_PIECES_P
174 #define CLEAR_BY_PIECES_P(SIZE, ALIGN) \
175   (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
176    < (unsigned int) CLEAR_RATIO (optimize_insn_for_speed_p ()))
177 #endif
178
179 /* This macro is used to determine whether store_by_pieces should be
180    called to "memset" storage with byte values other than zero.  */
181 #ifndef SET_BY_PIECES_P
182 #define SET_BY_PIECES_P(SIZE, ALIGN) \
183   (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
184    < (unsigned int) SET_RATIO (optimize_insn_for_speed_p ()))
185 #endif
186
187 /* This macro is used to determine whether store_by_pieces should be
188    called to "memcpy" storage when the source is a constant string.  */
189 #ifndef STORE_BY_PIECES_P
190 #define STORE_BY_PIECES_P(SIZE, ALIGN) \
191   (move_by_pieces_ninsns (SIZE, ALIGN, STORE_MAX_PIECES + 1) \
192    < (unsigned int) MOVE_RATIO (optimize_insn_for_speed_p ()))
193 #endif
194
195 /* SLOW_UNALIGNED_ACCESS is nonzero if unaligned accesses are very slow.  */
196
197 #ifndef SLOW_UNALIGNED_ACCESS
198 #define SLOW_UNALIGNED_ACCESS(MODE, ALIGN) STRICT_ALIGNMENT
199 #endif
200 \f
201 /* This is run to set up which modes can be used
202    directly in memory and to initialize the block move optab.  It is run
203    at the beginning of compilation and when the target is reinitialized.  */
204
205 void
206 init_expr_target (void)
207 {
208   rtx insn, pat;
209   enum machine_mode mode;
210   int num_clobbers;
211   rtx mem, mem1;
212   rtx reg;
213
214   /* Try indexing by frame ptr and try by stack ptr.
215      It is known that on the Convex the stack ptr isn't a valid index.
216      With luck, one or the other is valid on any machine.  */
217   mem = gen_rtx_MEM (VOIDmode, stack_pointer_rtx);
218   mem1 = gen_rtx_MEM (VOIDmode, frame_pointer_rtx);
219
220   /* A scratch register we can modify in-place below to avoid
221      useless RTL allocations.  */
222   reg = gen_rtx_REG (VOIDmode, -1);
223
224   insn = rtx_alloc (INSN);
225   pat = gen_rtx_SET (VOIDmode, NULL_RTX, NULL_RTX);
226   PATTERN (insn) = pat;
227
228   for (mode = VOIDmode; (int) mode < NUM_MACHINE_MODES;
229        mode = (enum machine_mode) ((int) mode + 1))
230     {
231       int regno;
232
233       direct_load[(int) mode] = direct_store[(int) mode] = 0;
234       PUT_MODE (mem, mode);
235       PUT_MODE (mem1, mode);
236       PUT_MODE (reg, mode);
237
238       /* See if there is some register that can be used in this mode and
239          directly loaded or stored from memory.  */
240
241       if (mode != VOIDmode && mode != BLKmode)
242         for (regno = 0; regno < FIRST_PSEUDO_REGISTER
243              && (direct_load[(int) mode] == 0 || direct_store[(int) mode] == 0);
244              regno++)
245           {
246             if (! HARD_REGNO_MODE_OK (regno, mode))
247               continue;
248
249             SET_REGNO (reg, regno);
250
251             SET_SRC (pat) = mem;
252             SET_DEST (pat) = reg;
253             if (recog (pat, insn, &num_clobbers) >= 0)
254               direct_load[(int) mode] = 1;
255
256             SET_SRC (pat) = mem1;
257             SET_DEST (pat) = reg;
258             if (recog (pat, insn, &num_clobbers) >= 0)
259               direct_load[(int) mode] = 1;
260
261             SET_SRC (pat) = reg;
262             SET_DEST (pat) = mem;
263             if (recog (pat, insn, &num_clobbers) >= 0)
264               direct_store[(int) mode] = 1;
265
266             SET_SRC (pat) = reg;
267             SET_DEST (pat) = mem1;
268             if (recog (pat, insn, &num_clobbers) >= 0)
269               direct_store[(int) mode] = 1;
270           }
271     }
272
273   mem = gen_rtx_MEM (VOIDmode, gen_rtx_raw_REG (Pmode, 10000));
274
275   for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
276        mode = GET_MODE_WIDER_MODE (mode))
277     {
278       enum machine_mode srcmode;
279       for (srcmode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); srcmode != mode;
280            srcmode = GET_MODE_WIDER_MODE (srcmode))
281         {
282           enum insn_code ic;
283
284           ic = can_extend_p (mode, srcmode, 0);
285           if (ic == CODE_FOR_nothing)
286             continue;
287
288           PUT_MODE (mem, srcmode);
289
290           if (insn_operand_matches (ic, 1, mem))
291             float_extend_from_mem[mode][srcmode] = true;
292         }
293     }
294 }
295
296 /* This is run at the start of compiling a function.  */
297
298 void
299 init_expr (void)
300 {
301   memset (&crtl->expr, 0, sizeof (crtl->expr));
302 }
303 \f
304 /* Copy data from FROM to TO, where the machine modes are not the same.
305    Both modes may be integer, or both may be floating, or both may be
306    fixed-point.
307    UNSIGNEDP should be nonzero if FROM is an unsigned type.
308    This causes zero-extension instead of sign-extension.  */
309
310 void
311 convert_move (rtx to, rtx from, int unsignedp)
312 {
313   enum machine_mode to_mode = GET_MODE (to);
314   enum machine_mode from_mode = GET_MODE (from);
315   int to_real = SCALAR_FLOAT_MODE_P (to_mode);
316   int from_real = SCALAR_FLOAT_MODE_P (from_mode);
317   enum insn_code code;
318   rtx libcall;
319
320   /* rtx code for making an equivalent value.  */
321   enum rtx_code equiv_code = (unsignedp < 0 ? UNKNOWN
322                               : (unsignedp ? ZERO_EXTEND : SIGN_EXTEND));
323
324
325   gcc_assert (to_real == from_real);
326   gcc_assert (to_mode != BLKmode);
327   gcc_assert (from_mode != BLKmode);
328
329   /* If the source and destination are already the same, then there's
330      nothing to do.  */
331   if (to == from)
332     return;
333
334   /* If FROM is a SUBREG that indicates that we have already done at least
335      the required extension, strip it.  We don't handle such SUBREGs as
336      TO here.  */
337
338   if (GET_CODE (from) == SUBREG && SUBREG_PROMOTED_VAR_P (from)
339       && (GET_MODE_PRECISION (GET_MODE (SUBREG_REG (from)))
340           >= GET_MODE_PRECISION (to_mode))
341       && SUBREG_PROMOTED_UNSIGNED_P (from) == unsignedp)
342     from = gen_lowpart (to_mode, from), from_mode = to_mode;
343
344   gcc_assert (GET_CODE (to) != SUBREG || !SUBREG_PROMOTED_VAR_P (to));
345
346   if (to_mode == from_mode
347       || (from_mode == VOIDmode && CONSTANT_P (from)))
348     {
349       emit_move_insn (to, from);
350       return;
351     }
352
353   if (VECTOR_MODE_P (to_mode) || VECTOR_MODE_P (from_mode))
354     {
355       gcc_assert (GET_MODE_BITSIZE (from_mode) == GET_MODE_BITSIZE (to_mode));
356
357       if (VECTOR_MODE_P (to_mode))
358         from = simplify_gen_subreg (to_mode, from, GET_MODE (from), 0);
359       else
360         to = simplify_gen_subreg (from_mode, to, GET_MODE (to), 0);
361
362       emit_move_insn (to, from);
363       return;
364     }
365
366   if (GET_CODE (to) == CONCAT && GET_CODE (from) == CONCAT)
367     {
368       convert_move (XEXP (to, 0), XEXP (from, 0), unsignedp);
369       convert_move (XEXP (to, 1), XEXP (from, 1), unsignedp);
370       return;
371     }
372
373   if (to_real)
374     {
375       rtx value, insns;
376       convert_optab tab;
377
378       gcc_assert ((GET_MODE_PRECISION (from_mode)
379                    != GET_MODE_PRECISION (to_mode))
380                   || (DECIMAL_FLOAT_MODE_P (from_mode)
381                       != DECIMAL_FLOAT_MODE_P (to_mode)));
382
383       if (GET_MODE_PRECISION (from_mode) == GET_MODE_PRECISION (to_mode))
384         /* Conversion between decimal float and binary float, same size.  */
385         tab = DECIMAL_FLOAT_MODE_P (from_mode) ? trunc_optab : sext_optab;
386       else if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode))
387         tab = sext_optab;
388       else
389         tab = trunc_optab;
390
391       /* Try converting directly if the insn is supported.  */
392
393       code = convert_optab_handler (tab, to_mode, from_mode);
394       if (code != CODE_FOR_nothing)
395         {
396           emit_unop_insn (code, to, from,
397                           tab == sext_optab ? FLOAT_EXTEND : FLOAT_TRUNCATE);
398           return;
399         }
400
401       /* Otherwise use a libcall.  */
402       libcall = convert_optab_libfunc (tab, to_mode, from_mode);
403
404       /* Is this conversion implemented yet?  */
405       gcc_assert (libcall);
406
407       start_sequence ();
408       value = emit_library_call_value (libcall, NULL_RTX, LCT_CONST, to_mode,
409                                        1, from, from_mode);
410       insns = get_insns ();
411       end_sequence ();
412       emit_libcall_block (insns, to, value,
413                           tab == trunc_optab ? gen_rtx_FLOAT_TRUNCATE (to_mode,
414                                                                        from)
415                           : gen_rtx_FLOAT_EXTEND (to_mode, from));
416       return;
417     }
418
419   /* Handle pointer conversion.  */                     /* SPEE 900220.  */
420   /* Targets are expected to provide conversion insns between PxImode and
421      xImode for all MODE_PARTIAL_INT modes they use, but no others.  */
422   if (GET_MODE_CLASS (to_mode) == MODE_PARTIAL_INT)
423     {
424       enum machine_mode full_mode
425         = smallest_mode_for_size (GET_MODE_BITSIZE (to_mode), MODE_INT);
426
427       gcc_assert (convert_optab_handler (trunc_optab, to_mode, full_mode)
428                   != CODE_FOR_nothing);
429
430       if (full_mode != from_mode)
431         from = convert_to_mode (full_mode, from, unsignedp);
432       emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, full_mode),
433                       to, from, UNKNOWN);
434       return;
435     }
436   if (GET_MODE_CLASS (from_mode) == MODE_PARTIAL_INT)
437     {
438       rtx new_from;
439       enum machine_mode full_mode
440         = smallest_mode_for_size (GET_MODE_BITSIZE (from_mode), MODE_INT);
441       convert_optab ctab = unsignedp ? zext_optab : sext_optab;
442       enum insn_code icode;
443
444       icode = convert_optab_handler (ctab, full_mode, from_mode);
445       gcc_assert (icode != CODE_FOR_nothing);
446
447       if (to_mode == full_mode)
448         {
449           emit_unop_insn (icode, to, from, UNKNOWN);
450           return;
451         }
452
453       new_from = gen_reg_rtx (full_mode);
454       emit_unop_insn (icode, new_from, from, UNKNOWN);
455
456       /* else proceed to integer conversions below.  */
457       from_mode = full_mode;
458       from = new_from;
459     }
460
461    /* Make sure both are fixed-point modes or both are not.  */
462    gcc_assert (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode) ==
463                ALL_SCALAR_FIXED_POINT_MODE_P (to_mode));
464    if (ALL_SCALAR_FIXED_POINT_MODE_P (from_mode))
465     {
466       /* If we widen from_mode to to_mode and they are in the same class,
467          we won't saturate the result.
468          Otherwise, always saturate the result to play safe.  */
469       if (GET_MODE_CLASS (from_mode) == GET_MODE_CLASS (to_mode)
470           && GET_MODE_SIZE (from_mode) < GET_MODE_SIZE (to_mode))
471         expand_fixed_convert (to, from, 0, 0);
472       else
473         expand_fixed_convert (to, from, 0, 1);
474       return;
475     }
476
477   /* Now both modes are integers.  */
478
479   /* Handle expanding beyond a word.  */
480   if (GET_MODE_PRECISION (from_mode) < GET_MODE_PRECISION (to_mode)
481       && GET_MODE_PRECISION (to_mode) > BITS_PER_WORD)
482     {
483       rtx insns;
484       rtx lowpart;
485       rtx fill_value;
486       rtx lowfrom;
487       int i;
488       enum machine_mode lowpart_mode;
489       int nwords = CEIL (GET_MODE_SIZE (to_mode), UNITS_PER_WORD);
490
491       /* Try converting directly if the insn is supported.  */
492       if ((code = can_extend_p (to_mode, from_mode, unsignedp))
493           != CODE_FOR_nothing)
494         {
495           /* If FROM is a SUBREG, put it into a register.  Do this
496              so that we always generate the same set of insns for
497              better cse'ing; if an intermediate assignment occurred,
498              we won't be doing the operation directly on the SUBREG.  */
499           if (optimize > 0 && GET_CODE (from) == SUBREG)
500             from = force_reg (from_mode, from);
501           emit_unop_insn (code, to, from, equiv_code);
502           return;
503         }
504       /* Next, try converting via full word.  */
505       else if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD
506                && ((code = can_extend_p (to_mode, word_mode, unsignedp))
507                    != CODE_FOR_nothing))
508         {
509           rtx word_to = gen_reg_rtx (word_mode);
510           if (REG_P (to))
511             {
512               if (reg_overlap_mentioned_p (to, from))
513                 from = force_reg (from_mode, from);
514               emit_clobber (to);
515             }
516           convert_move (word_to, from, unsignedp);
517           emit_unop_insn (code, to, word_to, equiv_code);
518           return;
519         }
520
521       /* No special multiword conversion insn; do it by hand.  */
522       start_sequence ();
523
524       /* Since we will turn this into a no conflict block, we must ensure
525          that the source does not overlap the target.  */
526
527       if (reg_overlap_mentioned_p (to, from))
528         from = force_reg (from_mode, from);
529
530       /* Get a copy of FROM widened to a word, if necessary.  */
531       if (GET_MODE_PRECISION (from_mode) < BITS_PER_WORD)
532         lowpart_mode = word_mode;
533       else
534         lowpart_mode = from_mode;
535
536       lowfrom = convert_to_mode (lowpart_mode, from, unsignedp);
537
538       lowpart = gen_lowpart (lowpart_mode, to);
539       emit_move_insn (lowpart, lowfrom);
540
541       /* Compute the value to put in each remaining word.  */
542       if (unsignedp)
543         fill_value = const0_rtx;
544       else
545         fill_value = emit_store_flag (gen_reg_rtx (word_mode),
546                                       LT, lowfrom, const0_rtx,
547                                       VOIDmode, 0, -1);
548
549       /* Fill the remaining words.  */
550       for (i = GET_MODE_SIZE (lowpart_mode) / UNITS_PER_WORD; i < nwords; i++)
551         {
552           int index = (WORDS_BIG_ENDIAN ? nwords - i - 1 : i);
553           rtx subword = operand_subword (to, index, 1, to_mode);
554
555           gcc_assert (subword);
556
557           if (fill_value != subword)
558             emit_move_insn (subword, fill_value);
559         }
560
561       insns = get_insns ();
562       end_sequence ();
563
564       emit_insn (insns);
565       return;
566     }
567
568   /* Truncating multi-word to a word or less.  */
569   if (GET_MODE_PRECISION (from_mode) > BITS_PER_WORD
570       && GET_MODE_PRECISION (to_mode) <= BITS_PER_WORD)
571     {
572       if (!((MEM_P (from)
573              && ! MEM_VOLATILE_P (from)
574              && direct_load[(int) to_mode]
575              && ! mode_dependent_address_p (XEXP (from, 0)))
576             || REG_P (from)
577             || GET_CODE (from) == SUBREG))
578         from = force_reg (from_mode, from);
579       convert_move (to, gen_lowpart (word_mode, from), 0);
580       return;
581     }
582
583   /* Now follow all the conversions between integers
584      no more than a word long.  */
585
586   /* For truncation, usually we can just refer to FROM in a narrower mode.  */
587   if (GET_MODE_BITSIZE (to_mode) < GET_MODE_BITSIZE (from_mode)
588       && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, from_mode))
589     {
590       if (!((MEM_P (from)
591              && ! MEM_VOLATILE_P (from)
592              && direct_load[(int) to_mode]
593              && ! mode_dependent_address_p (XEXP (from, 0)))
594             || REG_P (from)
595             || GET_CODE (from) == SUBREG))
596         from = force_reg (from_mode, from);
597       if (REG_P (from) && REGNO (from) < FIRST_PSEUDO_REGISTER
598           && ! HARD_REGNO_MODE_OK (REGNO (from), to_mode))
599         from = copy_to_reg (from);
600       emit_move_insn (to, gen_lowpart (to_mode, from));
601       return;
602     }
603
604   /* Handle extension.  */
605   if (GET_MODE_PRECISION (to_mode) > GET_MODE_PRECISION (from_mode))
606     {
607       /* Convert directly if that works.  */
608       if ((code = can_extend_p (to_mode, from_mode, unsignedp))
609           != CODE_FOR_nothing)
610         {
611           emit_unop_insn (code, to, from, equiv_code);
612           return;
613         }
614       else
615         {
616           enum machine_mode intermediate;
617           rtx tmp;
618           int shift_amount;
619
620           /* Search for a mode to convert via.  */
621           for (intermediate = from_mode; intermediate != VOIDmode;
622                intermediate = GET_MODE_WIDER_MODE (intermediate))
623             if (((can_extend_p (to_mode, intermediate, unsignedp)
624                   != CODE_FOR_nothing)
625                  || (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (intermediate)
626                      && TRULY_NOOP_TRUNCATION_MODES_P (to_mode, intermediate)))
627                 && (can_extend_p (intermediate, from_mode, unsignedp)
628                     != CODE_FOR_nothing))
629               {
630                 convert_move (to, convert_to_mode (intermediate, from,
631                                                    unsignedp), unsignedp);
632                 return;
633               }
634
635           /* No suitable intermediate mode.
636              Generate what we need with shifts.  */
637           shift_amount = (GET_MODE_PRECISION (to_mode)
638                           - GET_MODE_PRECISION (from_mode));
639           from = gen_lowpart (to_mode, force_reg (from_mode, from));
640           tmp = expand_shift (LSHIFT_EXPR, to_mode, from, shift_amount,
641                               to, unsignedp);
642           tmp = expand_shift (RSHIFT_EXPR, to_mode, tmp, shift_amount,
643                               to, unsignedp);
644           if (tmp != to)
645             emit_move_insn (to, tmp);
646           return;
647         }
648     }
649
650   /* Support special truncate insns for certain modes.  */
651   if (convert_optab_handler (trunc_optab, to_mode,
652                              from_mode) != CODE_FOR_nothing)
653     {
654       emit_unop_insn (convert_optab_handler (trunc_optab, to_mode, from_mode),
655                       to, from, UNKNOWN);
656       return;
657     }
658
659   /* Handle truncation of volatile memrefs, and so on;
660      the things that couldn't be truncated directly,
661      and for which there was no special instruction.
662
663      ??? Code above formerly short-circuited this, for most integer
664      mode pairs, with a force_reg in from_mode followed by a recursive
665      call to this routine.  Appears always to have been wrong.  */
666   if (GET_MODE_PRECISION (to_mode) < GET_MODE_PRECISION (from_mode))
667     {
668       rtx temp = force_reg (to_mode, gen_lowpart (to_mode, from));
669       emit_move_insn (to, temp);
670       return;
671     }
672
673   /* Mode combination is not recognized.  */
674   gcc_unreachable ();
675 }
676
677 /* Return an rtx for a value that would result
678    from converting X to mode MODE.
679    Both X and MODE may be floating, or both integer.
680    UNSIGNEDP is nonzero if X is an unsigned value.
681    This can be done by referring to a part of X in place
682    or by copying to a new temporary with conversion.  */
683
684 rtx
685 convert_to_mode (enum machine_mode mode, rtx x, int unsignedp)
686 {
687   return convert_modes (mode, VOIDmode, x, unsignedp);
688 }
689
690 /* Return an rtx for a value that would result
691    from converting X from mode OLDMODE to mode MODE.
692    Both modes may be floating, or both integer.
693    UNSIGNEDP is nonzero if X is an unsigned value.
694
695    This can be done by referring to a part of X in place
696    or by copying to a new temporary with conversion.
697
698    You can give VOIDmode for OLDMODE, if you are sure X has a nonvoid mode.  */
699
700 rtx
701 convert_modes (enum machine_mode mode, enum machine_mode oldmode, rtx x, int unsignedp)
702 {
703   rtx temp;
704
705   /* If FROM is a SUBREG that indicates that we have already done at least
706      the required extension, strip it.  */
707
708   if (GET_CODE (x) == SUBREG && SUBREG_PROMOTED_VAR_P (x)
709       && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) >= GET_MODE_SIZE (mode)
710       && SUBREG_PROMOTED_UNSIGNED_P (x) == unsignedp)
711     x = gen_lowpart (mode, x);
712
713   if (GET_MODE (x) != VOIDmode)
714     oldmode = GET_MODE (x);
715
716   if (mode == oldmode)
717     return x;
718
719   /* There is one case that we must handle specially: If we are converting
720      a CONST_INT into a mode whose size is twice HOST_BITS_PER_WIDE_INT and
721      we are to interpret the constant as unsigned, gen_lowpart will do
722      the wrong if the constant appears negative.  What we want to do is
723      make the high-order word of the constant zero, not all ones.  */
724
725   if (unsignedp && GET_MODE_CLASS (mode) == MODE_INT
726       && GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_WIDE_INT
727       && CONST_INT_P (x) && INTVAL (x) < 0)
728     {
729       double_int val = uhwi_to_double_int (INTVAL (x));
730
731       /* We need to zero extend VAL.  */
732       if (oldmode != VOIDmode)
733         val = double_int_zext (val, GET_MODE_BITSIZE (oldmode));
734
735       return immed_double_int_const (val, mode);
736     }
737
738   /* We can do this with a gen_lowpart if both desired and current modes
739      are integer, and this is either a constant integer, a register, or a
740      non-volatile MEM.  Except for the constant case where MODE is no
741      wider than HOST_BITS_PER_WIDE_INT, we must be narrowing the operand.  */
742
743   if ((CONST_INT_P (x)
744        && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT)
745       || (GET_MODE_CLASS (mode) == MODE_INT
746           && GET_MODE_CLASS (oldmode) == MODE_INT
747           && (GET_CODE (x) == CONST_DOUBLE
748               || (GET_MODE_PRECISION (mode) <= GET_MODE_PRECISION (oldmode)
749                   && ((MEM_P (x) && ! MEM_VOLATILE_P (x)
750                        && direct_load[(int) mode])
751                       || (REG_P (x)
752                           && (! HARD_REGISTER_P (x)
753                               || HARD_REGNO_MODE_OK (REGNO (x), mode))
754                           && TRULY_NOOP_TRUNCATION_MODES_P (mode,
755                                                             GET_MODE (x))))))))
756     {
757       /* ?? If we don't know OLDMODE, we have to assume here that
758          X does not need sign- or zero-extension.   This may not be
759          the case, but it's the best we can do.  */
760       if (CONST_INT_P (x) && oldmode != VOIDmode
761           && GET_MODE_PRECISION (mode) > GET_MODE_PRECISION (oldmode))
762         {
763           HOST_WIDE_INT val = INTVAL (x);
764
765           /* We must sign or zero-extend in this case.  Start by
766              zero-extending, then sign extend if we need to.  */
767           val &= GET_MODE_MASK (oldmode);
768           if (! unsignedp
769               && val_signbit_known_set_p (oldmode, val))
770             val |= ~GET_MODE_MASK (oldmode);
771
772           return gen_int_mode (val, mode);
773         }
774
775       return gen_lowpart (mode, x);
776     }
777
778   /* Converting from integer constant into mode is always equivalent to an
779      subreg operation.  */
780   if (VECTOR_MODE_P (mode) && GET_MODE (x) == VOIDmode)
781     {
782       gcc_assert (GET_MODE_BITSIZE (mode) == GET_MODE_BITSIZE (oldmode));
783       return simplify_gen_subreg (mode, x, oldmode, 0);
784     }
785
786   temp = gen_reg_rtx (mode);
787   convert_move (temp, x, unsignedp);
788   return temp;
789 }
790 \f
791 /* Return the largest alignment we can use for doing a move (or store)
792    of MAX_PIECES.  ALIGN is the largest alignment we could use.  */
793
794 static unsigned int
795 alignment_for_piecewise_move (unsigned int max_pieces, unsigned int align)
796 {
797   enum machine_mode tmode;
798
799   tmode = mode_for_size (max_pieces * BITS_PER_UNIT, MODE_INT, 1);
800   if (align >= GET_MODE_ALIGNMENT (tmode))
801     align = GET_MODE_ALIGNMENT (tmode);
802   else
803     {
804       enum machine_mode tmode, xmode;
805
806       for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT), xmode = tmode;
807            tmode != VOIDmode;
808            xmode = tmode, tmode = GET_MODE_WIDER_MODE (tmode))
809         if (GET_MODE_SIZE (tmode) > max_pieces
810             || SLOW_UNALIGNED_ACCESS (tmode, align))
811           break;
812
813       align = MAX (align, GET_MODE_ALIGNMENT (xmode));
814     }
815
816   return align;
817 }
818
819 /* Return the widest integer mode no wider than SIZE.  If no such mode
820    can be found, return VOIDmode.  */
821
822 static enum machine_mode
823 widest_int_mode_for_size (unsigned int size)
824 {
825   enum machine_mode tmode, mode = VOIDmode;
826
827   for (tmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
828        tmode != VOIDmode; tmode = GET_MODE_WIDER_MODE (tmode))
829     if (GET_MODE_SIZE (tmode) < size)
830       mode = tmode;
831
832   return mode;
833 }
834
835 /* STORE_MAX_PIECES is the number of bytes at a time that we can
836    store efficiently.  Due to internal GCC limitations, this is
837    MOVE_MAX_PIECES limited by the number of bytes GCC can represent
838    for an immediate constant.  */
839
840 #define STORE_MAX_PIECES  MIN (MOVE_MAX_PIECES, 2 * sizeof (HOST_WIDE_INT))
841
842 /* Determine whether the LEN bytes can be moved by using several move
843    instructions.  Return nonzero if a call to move_by_pieces should
844    succeed.  */
845
846 int
847 can_move_by_pieces (unsigned HOST_WIDE_INT len,
848                     unsigned int align ATTRIBUTE_UNUSED)
849 {
850   return MOVE_BY_PIECES_P (len, align);
851 }
852
853 /* Generate several move instructions to copy LEN bytes from block FROM to
854    block TO.  (These are MEM rtx's with BLKmode).
855
856    If PUSH_ROUNDING is defined and TO is NULL, emit_single_push_insn is
857    used to push FROM to the stack.
858
859    ALIGN is maximum stack alignment we can assume.
860
861    If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
862    mempcpy, and if ENDP is 2 return memory the end minus one byte ala
863    stpcpy.  */
864
865 rtx
866 move_by_pieces (rtx to, rtx from, unsigned HOST_WIDE_INT len,
867                 unsigned int align, int endp)
868 {
869   struct move_by_pieces_d data;
870   enum machine_mode to_addr_mode;
871   enum machine_mode from_addr_mode = get_address_mode (from);
872   rtx to_addr, from_addr = XEXP (from, 0);
873   unsigned int max_size = MOVE_MAX_PIECES + 1;
874   enum insn_code icode;
875
876   align = MIN (to ? MEM_ALIGN (to) : align, MEM_ALIGN (from));
877
878   data.offset = 0;
879   data.from_addr = from_addr;
880   if (to)
881     {
882       to_addr_mode = get_address_mode (to);
883       to_addr = XEXP (to, 0);
884       data.to = to;
885       data.autinc_to
886         = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
887            || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
888       data.reverse
889         = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
890     }
891   else
892     {
893       to_addr_mode = VOIDmode;
894       to_addr = NULL_RTX;
895       data.to = NULL_RTX;
896       data.autinc_to = 1;
897 #ifdef STACK_GROWS_DOWNWARD
898       data.reverse = 1;
899 #else
900       data.reverse = 0;
901 #endif
902     }
903   data.to_addr = to_addr;
904   data.from = from;
905   data.autinc_from
906     = (GET_CODE (from_addr) == PRE_INC || GET_CODE (from_addr) == PRE_DEC
907        || GET_CODE (from_addr) == POST_INC
908        || GET_CODE (from_addr) == POST_DEC);
909
910   data.explicit_inc_from = 0;
911   data.explicit_inc_to = 0;
912   if (data.reverse) data.offset = len;
913   data.len = len;
914
915   /* If copying requires more than two move insns,
916      copy addresses to registers (to make displacements shorter)
917      and use post-increment if available.  */
918   if (!(data.autinc_from && data.autinc_to)
919       && move_by_pieces_ninsns (len, align, max_size) > 2)
920     {
921       /* Find the mode of the largest move...
922          MODE might not be used depending on the definitions of the
923          USE_* macros below.  */
924       enum machine_mode mode ATTRIBUTE_UNUSED
925         = widest_int_mode_for_size (max_size);
926
927       if (USE_LOAD_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_from)
928         {
929           data.from_addr = copy_to_mode_reg (from_addr_mode,
930                                              plus_constant (from_addr_mode,
931                                                             from_addr, len));
932           data.autinc_from = 1;
933           data.explicit_inc_from = -1;
934         }
935       if (USE_LOAD_POST_INCREMENT (mode) && ! data.autinc_from)
936         {
937           data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
938           data.autinc_from = 1;
939           data.explicit_inc_from = 1;
940         }
941       if (!data.autinc_from && CONSTANT_P (from_addr))
942         data.from_addr = copy_to_mode_reg (from_addr_mode, from_addr);
943       if (USE_STORE_PRE_DECREMENT (mode) && data.reverse && ! data.autinc_to)
944         {
945           data.to_addr = copy_to_mode_reg (to_addr_mode,
946                                            plus_constant (to_addr_mode,
947                                                           to_addr, len));
948           data.autinc_to = 1;
949           data.explicit_inc_to = -1;
950         }
951       if (USE_STORE_POST_INCREMENT (mode) && ! data.reverse && ! data.autinc_to)
952         {
953           data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
954           data.autinc_to = 1;
955           data.explicit_inc_to = 1;
956         }
957       if (!data.autinc_to && CONSTANT_P (to_addr))
958         data.to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
959     }
960
961   align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
962
963   /* First move what we can in the largest integer mode, then go to
964      successively smaller modes.  */
965
966   while (max_size > 1)
967     {
968       enum machine_mode mode = widest_int_mode_for_size (max_size);
969
970       if (mode == VOIDmode)
971         break;
972
973       icode = optab_handler (mov_optab, mode);
974       if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
975         move_by_pieces_1 (GEN_FCN (icode), mode, &data);
976
977       max_size = GET_MODE_SIZE (mode);
978     }
979
980   /* The code above should have handled everything.  */
981   gcc_assert (!data.len);
982
983   if (endp)
984     {
985       rtx to1;
986
987       gcc_assert (!data.reverse);
988       if (data.autinc_to)
989         {
990           if (endp == 2)
991             {
992               if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
993                 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
994               else
995                 data.to_addr = copy_to_mode_reg (to_addr_mode,
996                                                  plus_constant (to_addr_mode,
997                                                                 data.to_addr,
998                                                                 -1));
999             }
1000           to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
1001                                            data.offset);
1002         }
1003       else
1004         {
1005           if (endp == 2)
1006             --data.offset;
1007           to1 = adjust_address (data.to, QImode, data.offset);
1008         }
1009       return to1;
1010     }
1011   else
1012     return data.to;
1013 }
1014
1015 /* Return number of insns required to move L bytes by pieces.
1016    ALIGN (in bits) is maximum alignment we can assume.  */
1017
1018 unsigned HOST_WIDE_INT
1019 move_by_pieces_ninsns (unsigned HOST_WIDE_INT l, unsigned int align,
1020                        unsigned int max_size)
1021 {
1022   unsigned HOST_WIDE_INT n_insns = 0;
1023
1024   align = alignment_for_piecewise_move (MOVE_MAX_PIECES, align);
1025
1026   while (max_size > 1)
1027     {
1028       enum machine_mode mode;
1029       enum insn_code icode;
1030
1031       mode = widest_int_mode_for_size (max_size);
1032
1033       if (mode == VOIDmode)
1034         break;
1035
1036       icode = optab_handler (mov_optab, mode);
1037       if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
1038         n_insns += l / GET_MODE_SIZE (mode), l %= GET_MODE_SIZE (mode);
1039
1040       max_size = GET_MODE_SIZE (mode);
1041     }
1042
1043   gcc_assert (!l);
1044   return n_insns;
1045 }
1046
1047 /* Subroutine of move_by_pieces.  Move as many bytes as appropriate
1048    with move instructions for mode MODE.  GENFUN is the gen_... function
1049    to make a move insn for that mode.  DATA has all the other info.  */
1050
1051 static void
1052 move_by_pieces_1 (rtx (*genfun) (rtx, ...), enum machine_mode mode,
1053                   struct move_by_pieces_d *data)
1054 {
1055   unsigned int size = GET_MODE_SIZE (mode);
1056   rtx to1 = NULL_RTX, from1;
1057
1058   while (data->len >= size)
1059     {
1060       if (data->reverse)
1061         data->offset -= size;
1062
1063       if (data->to)
1064         {
1065           if (data->autinc_to)
1066             to1 = adjust_automodify_address (data->to, mode, data->to_addr,
1067                                              data->offset);
1068           else
1069             to1 = adjust_address (data->to, mode, data->offset);
1070         }
1071
1072       if (data->autinc_from)
1073         from1 = adjust_automodify_address (data->from, mode, data->from_addr,
1074                                            data->offset);
1075       else
1076         from1 = adjust_address (data->from, mode, data->offset);
1077
1078       if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
1079         emit_insn (gen_add2_insn (data->to_addr,
1080                                   GEN_INT (-(HOST_WIDE_INT)size)));
1081       if (HAVE_PRE_DECREMENT && data->explicit_inc_from < 0)
1082         emit_insn (gen_add2_insn (data->from_addr,
1083                                   GEN_INT (-(HOST_WIDE_INT)size)));
1084
1085       if (data->to)
1086         emit_insn ((*genfun) (to1, from1));
1087       else
1088         {
1089 #ifdef PUSH_ROUNDING
1090           emit_single_push_insn (mode, from1, NULL);
1091 #else
1092           gcc_unreachable ();
1093 #endif
1094         }
1095
1096       if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
1097         emit_insn (gen_add2_insn (data->to_addr, GEN_INT (size)));
1098       if (HAVE_POST_INCREMENT && data->explicit_inc_from > 0)
1099         emit_insn (gen_add2_insn (data->from_addr, GEN_INT (size)));
1100
1101       if (! data->reverse)
1102         data->offset += size;
1103
1104       data->len -= size;
1105     }
1106 }
1107 \f
1108 /* Emit code to move a block Y to a block X.  This may be done with
1109    string-move instructions, with multiple scalar move instructions,
1110    or with a library call.
1111
1112    Both X and Y must be MEM rtx's (perhaps inside VOLATILE) with mode BLKmode.
1113    SIZE is an rtx that says how long they are.
1114    ALIGN is the maximum alignment we can assume they have.
1115    METHOD describes what kind of copy this is, and what mechanisms may be used.
1116
1117    Return the address of the new block, if memcpy is called and returns it,
1118    0 otherwise.  */
1119
1120 rtx
1121 emit_block_move_hints (rtx x, rtx y, rtx size, enum block_op_methods method,
1122                        unsigned int expected_align, HOST_WIDE_INT expected_size)
1123 {
1124   bool may_use_call;
1125   rtx retval = 0;
1126   unsigned int align;
1127
1128   gcc_assert (size);
1129   if (CONST_INT_P (size)
1130       && INTVAL (size) == 0)
1131     return 0;
1132
1133   switch (method)
1134     {
1135     case BLOCK_OP_NORMAL:
1136     case BLOCK_OP_TAILCALL:
1137       may_use_call = true;
1138       break;
1139
1140     case BLOCK_OP_CALL_PARM:
1141       may_use_call = block_move_libcall_safe_for_call_parm ();
1142
1143       /* Make inhibit_defer_pop nonzero around the library call
1144          to force it to pop the arguments right away.  */
1145       NO_DEFER_POP;
1146       break;
1147
1148     case BLOCK_OP_NO_LIBCALL:
1149       may_use_call = false;
1150       break;
1151
1152     default:
1153       gcc_unreachable ();
1154     }
1155
1156   gcc_assert (MEM_P (x) && MEM_P (y));
1157   align = MIN (MEM_ALIGN (x), MEM_ALIGN (y));
1158   gcc_assert (align >= BITS_PER_UNIT);
1159
1160   /* Make sure we've got BLKmode addresses; store_one_arg can decide that
1161      block copy is more efficient for other large modes, e.g. DCmode.  */
1162   x = adjust_address (x, BLKmode, 0);
1163   y = adjust_address (y, BLKmode, 0);
1164
1165   /* Set MEM_SIZE as appropriate for this block copy.  The main place this
1166      can be incorrect is coming from __builtin_memcpy.  */
1167   if (CONST_INT_P (size))
1168     {
1169       x = shallow_copy_rtx (x);
1170       y = shallow_copy_rtx (y);
1171       set_mem_size (x, INTVAL (size));
1172       set_mem_size (y, INTVAL (size));
1173     }
1174
1175   if (CONST_INT_P (size) && MOVE_BY_PIECES_P (INTVAL (size), align))
1176     move_by_pieces (x, y, INTVAL (size), align, 0);
1177   else if (emit_block_move_via_movmem (x, y, size, align,
1178                                        expected_align, expected_size))
1179     ;
1180   else if (may_use_call
1181            && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (x))
1182            && ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (y)))
1183     {
1184       /* Since x and y are passed to a libcall, mark the corresponding
1185          tree EXPR as addressable.  */
1186       tree y_expr = MEM_EXPR (y);
1187       tree x_expr = MEM_EXPR (x);
1188       if (y_expr)
1189         mark_addressable (y_expr);
1190       if (x_expr)
1191         mark_addressable (x_expr);
1192       retval = emit_block_move_via_libcall (x, y, size,
1193                                             method == BLOCK_OP_TAILCALL);
1194     }
1195
1196   else
1197     emit_block_move_via_loop (x, y, size, align);
1198
1199   if (method == BLOCK_OP_CALL_PARM)
1200     OK_DEFER_POP;
1201
1202   return retval;
1203 }
1204
1205 rtx
1206 emit_block_move (rtx x, rtx y, rtx size, enum block_op_methods method)
1207 {
1208   return emit_block_move_hints (x, y, size, method, 0, -1);
1209 }
1210
1211 /* A subroutine of emit_block_move.  Returns true if calling the
1212    block move libcall will not clobber any parameters which may have
1213    already been placed on the stack.  */
1214
1215 static bool
1216 block_move_libcall_safe_for_call_parm (void)
1217 {
1218 #if defined (REG_PARM_STACK_SPACE)
1219   tree fn;
1220 #endif
1221
1222   /* If arguments are pushed on the stack, then they're safe.  */
1223   if (PUSH_ARGS)
1224     return true;
1225
1226   /* If registers go on the stack anyway, any argument is sure to clobber
1227      an outgoing argument.  */
1228 #if defined (REG_PARM_STACK_SPACE)
1229   fn = emit_block_move_libcall_fn (false);
1230   /* Avoid set but not used warning if *REG_PARM_STACK_SPACE doesn't
1231      depend on its argument.  */
1232   (void) fn;
1233   if (OUTGOING_REG_PARM_STACK_SPACE ((!fn ? NULL_TREE : TREE_TYPE (fn)))
1234       && REG_PARM_STACK_SPACE (fn) != 0)
1235     return false;
1236 #endif
1237
1238   /* If any argument goes in memory, then it might clobber an outgoing
1239      argument.  */
1240   {
1241     CUMULATIVE_ARGS args_so_far_v;
1242     cumulative_args_t args_so_far;
1243     tree fn, arg;
1244
1245     fn = emit_block_move_libcall_fn (false);
1246     INIT_CUMULATIVE_ARGS (args_so_far_v, TREE_TYPE (fn), NULL_RTX, 0, 3);
1247     args_so_far = pack_cumulative_args (&args_so_far_v);
1248
1249     arg = TYPE_ARG_TYPES (TREE_TYPE (fn));
1250     for ( ; arg != void_list_node ; arg = TREE_CHAIN (arg))
1251       {
1252         enum machine_mode mode = TYPE_MODE (TREE_VALUE (arg));
1253         rtx tmp = targetm.calls.function_arg (args_so_far, mode,
1254                                               NULL_TREE, true);
1255         if (!tmp || !REG_P (tmp))
1256           return false;
1257         if (targetm.calls.arg_partial_bytes (args_so_far, mode, NULL, 1))
1258           return false;
1259         targetm.calls.function_arg_advance (args_so_far, mode,
1260                                             NULL_TREE, true);
1261       }
1262   }
1263   return true;
1264 }
1265
1266 /* A subroutine of emit_block_move.  Expand a movmem pattern;
1267    return true if successful.  */
1268
1269 static bool
1270 emit_block_move_via_movmem (rtx x, rtx y, rtx size, unsigned int align,
1271                             unsigned int expected_align, HOST_WIDE_INT expected_size)
1272 {
1273   int save_volatile_ok = volatile_ok;
1274   enum machine_mode mode;
1275
1276   if (expected_align < align)
1277     expected_align = align;
1278
1279   /* Since this is a move insn, we don't care about volatility.  */
1280   volatile_ok = 1;
1281
1282   /* Try the most limited insn first, because there's no point
1283      including more than one in the machine description unless
1284      the more limited one has some advantage.  */
1285
1286   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
1287        mode = GET_MODE_WIDER_MODE (mode))
1288     {
1289       enum insn_code code = direct_optab_handler (movmem_optab, mode);
1290
1291       if (code != CODE_FOR_nothing
1292           /* We don't need MODE to be narrower than BITS_PER_HOST_WIDE_INT
1293              here because if SIZE is less than the mode mask, as it is
1294              returned by the macro, it will definitely be less than the
1295              actual mode mask.  */
1296           && ((CONST_INT_P (size)
1297                && ((unsigned HOST_WIDE_INT) INTVAL (size)
1298                    <= (GET_MODE_MASK (mode) >> 1)))
1299               || GET_MODE_BITSIZE (mode) >= BITS_PER_WORD))
1300         {
1301           struct expand_operand ops[6];
1302           unsigned int nops;
1303
1304           /* ??? When called via emit_block_move_for_call, it'd be
1305              nice if there were some way to inform the backend, so
1306              that it doesn't fail the expansion because it thinks
1307              emitting the libcall would be more efficient.  */
1308           nops = insn_data[(int) code].n_generator_args;
1309           gcc_assert (nops == 4 || nops == 6);
1310
1311           create_fixed_operand (&ops[0], x);
1312           create_fixed_operand (&ops[1], y);
1313           /* The check above guarantees that this size conversion is valid.  */
1314           create_convert_operand_to (&ops[2], size, mode, true);
1315           create_integer_operand (&ops[3], align / BITS_PER_UNIT);
1316           if (nops == 6)
1317             {
1318               create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
1319               create_integer_operand (&ops[5], expected_size);
1320             }
1321           if (maybe_expand_insn (code, nops, ops))
1322             {
1323               volatile_ok = save_volatile_ok;
1324               return true;
1325             }
1326         }
1327     }
1328
1329   volatile_ok = save_volatile_ok;
1330   return false;
1331 }
1332
1333 /* A subroutine of emit_block_move.  Expand a call to memcpy.
1334    Return the return value from memcpy, 0 otherwise.  */
1335
1336 rtx
1337 emit_block_move_via_libcall (rtx dst, rtx src, rtx size, bool tailcall)
1338 {
1339   rtx dst_addr, src_addr;
1340   tree call_expr, fn, src_tree, dst_tree, size_tree;
1341   enum machine_mode size_mode;
1342   rtx retval;
1343
1344   /* Emit code to copy the addresses of DST and SRC and SIZE into new
1345      pseudos.  We can then place those new pseudos into a VAR_DECL and
1346      use them later.  */
1347
1348   dst_addr = copy_addr_to_reg (XEXP (dst, 0));
1349   src_addr = copy_addr_to_reg (XEXP (src, 0));
1350
1351   dst_addr = convert_memory_address (ptr_mode, dst_addr);
1352   src_addr = convert_memory_address (ptr_mode, src_addr);
1353
1354   dst_tree = make_tree (ptr_type_node, dst_addr);
1355   src_tree = make_tree (ptr_type_node, src_addr);
1356
1357   size_mode = TYPE_MODE (sizetype);
1358
1359   size = convert_to_mode (size_mode, size, 1);
1360   size = copy_to_mode_reg (size_mode, size);
1361
1362   /* It is incorrect to use the libcall calling conventions to call
1363      memcpy in this context.  This could be a user call to memcpy and
1364      the user may wish to examine the return value from memcpy.  For
1365      targets where libcalls and normal calls have different conventions
1366      for returning pointers, we could end up generating incorrect code.  */
1367
1368   size_tree = make_tree (sizetype, size);
1369
1370   fn = emit_block_move_libcall_fn (true);
1371   call_expr = build_call_expr (fn, 3, dst_tree, src_tree, size_tree);
1372   CALL_EXPR_TAILCALL (call_expr) = tailcall;
1373
1374   retval = expand_normal (call_expr);
1375
1376   return retval;
1377 }
1378
1379 /* A subroutine of emit_block_move_via_libcall.  Create the tree node
1380    for the function we use for block copies.  */
1381
1382 static GTY(()) tree block_move_fn;
1383
1384 void
1385 init_block_move_fn (const char *asmspec)
1386 {
1387   if (!block_move_fn)
1388     {
1389       tree args, fn;
1390
1391       fn = get_identifier ("memcpy");
1392       args = build_function_type_list (ptr_type_node, ptr_type_node,
1393                                        const_ptr_type_node, sizetype,
1394                                        NULL_TREE);
1395
1396       fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
1397       DECL_EXTERNAL (fn) = 1;
1398       TREE_PUBLIC (fn) = 1;
1399       DECL_ARTIFICIAL (fn) = 1;
1400       TREE_NOTHROW (fn) = 1;
1401       DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
1402       DECL_VISIBILITY_SPECIFIED (fn) = 1;
1403
1404       block_move_fn = fn;
1405     }
1406
1407   if (asmspec)
1408     set_user_assembler_name (block_move_fn, asmspec);
1409 }
1410
1411 static tree
1412 emit_block_move_libcall_fn (int for_call)
1413 {
1414   static bool emitted_extern;
1415
1416   if (!block_move_fn)
1417     init_block_move_fn (NULL);
1418
1419   if (for_call && !emitted_extern)
1420     {
1421       emitted_extern = true;
1422       make_decl_rtl (block_move_fn);
1423     }
1424
1425   return block_move_fn;
1426 }
1427
1428 /* A subroutine of emit_block_move.  Copy the data via an explicit
1429    loop.  This is used only when libcalls are forbidden.  */
1430 /* ??? It'd be nice to copy in hunks larger than QImode.  */
1431
1432 static void
1433 emit_block_move_via_loop (rtx x, rtx y, rtx size,
1434                           unsigned int align ATTRIBUTE_UNUSED)
1435 {
1436   rtx cmp_label, top_label, iter, x_addr, y_addr, tmp;
1437   enum machine_mode x_addr_mode = get_address_mode (x);
1438   enum machine_mode y_addr_mode = get_address_mode (y);
1439   enum machine_mode iter_mode;
1440
1441   iter_mode = GET_MODE (size);
1442   if (iter_mode == VOIDmode)
1443     iter_mode = word_mode;
1444
1445   top_label = gen_label_rtx ();
1446   cmp_label = gen_label_rtx ();
1447   iter = gen_reg_rtx (iter_mode);
1448
1449   emit_move_insn (iter, const0_rtx);
1450
1451   x_addr = force_operand (XEXP (x, 0), NULL_RTX);
1452   y_addr = force_operand (XEXP (y, 0), NULL_RTX);
1453   do_pending_stack_adjust ();
1454
1455   emit_jump (cmp_label);
1456   emit_label (top_label);
1457
1458   tmp = convert_modes (x_addr_mode, iter_mode, iter, true);
1459   x_addr = gen_rtx_PLUS (x_addr_mode, x_addr, tmp);
1460
1461   if (x_addr_mode != y_addr_mode)
1462     tmp = convert_modes (y_addr_mode, iter_mode, iter, true);
1463   y_addr = gen_rtx_PLUS (y_addr_mode, y_addr, tmp);
1464
1465   x = change_address (x, QImode, x_addr);
1466   y = change_address (y, QImode, y_addr);
1467
1468   emit_move_insn (x, y);
1469
1470   tmp = expand_simple_binop (iter_mode, PLUS, iter, const1_rtx, iter,
1471                              true, OPTAB_LIB_WIDEN);
1472   if (tmp != iter)
1473     emit_move_insn (iter, tmp);
1474
1475   emit_label (cmp_label);
1476
1477   emit_cmp_and_jump_insns (iter, size, LT, NULL_RTX, iter_mode,
1478                            true, top_label);
1479 }
1480 \f
1481 /* Copy all or part of a value X into registers starting at REGNO.
1482    The number of registers to be filled is NREGS.  */
1483
1484 void
1485 move_block_to_reg (int regno, rtx x, int nregs, enum machine_mode mode)
1486 {
1487   int i;
1488 #ifdef HAVE_load_multiple
1489   rtx pat;
1490   rtx last;
1491 #endif
1492
1493   if (nregs == 0)
1494     return;
1495
1496   if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
1497     x = validize_mem (force_const_mem (mode, x));
1498
1499   /* See if the machine can do this with a load multiple insn.  */
1500 #ifdef HAVE_load_multiple
1501   if (HAVE_load_multiple)
1502     {
1503       last = get_last_insn ();
1504       pat = gen_load_multiple (gen_rtx_REG (word_mode, regno), x,
1505                                GEN_INT (nregs));
1506       if (pat)
1507         {
1508           emit_insn (pat);
1509           return;
1510         }
1511       else
1512         delete_insns_since (last);
1513     }
1514 #endif
1515
1516   for (i = 0; i < nregs; i++)
1517     emit_move_insn (gen_rtx_REG (word_mode, regno + i),
1518                     operand_subword_force (x, i, mode));
1519 }
1520
1521 /* Copy all or part of a BLKmode value X out of registers starting at REGNO.
1522    The number of registers to be filled is NREGS.  */
1523
1524 void
1525 move_block_from_reg (int regno, rtx x, int nregs)
1526 {
1527   int i;
1528
1529   if (nregs == 0)
1530     return;
1531
1532   /* See if the machine can do this with a store multiple insn.  */
1533 #ifdef HAVE_store_multiple
1534   if (HAVE_store_multiple)
1535     {
1536       rtx last = get_last_insn ();
1537       rtx pat = gen_store_multiple (x, gen_rtx_REG (word_mode, regno),
1538                                     GEN_INT (nregs));
1539       if (pat)
1540         {
1541           emit_insn (pat);
1542           return;
1543         }
1544       else
1545         delete_insns_since (last);
1546     }
1547 #endif
1548
1549   for (i = 0; i < nregs; i++)
1550     {
1551       rtx tem = operand_subword (x, i, 1, BLKmode);
1552
1553       gcc_assert (tem);
1554
1555       emit_move_insn (tem, gen_rtx_REG (word_mode, regno + i));
1556     }
1557 }
1558
1559 /* Generate a PARALLEL rtx for a new non-consecutive group of registers from
1560    ORIG, where ORIG is a non-consecutive group of registers represented by
1561    a PARALLEL.  The clone is identical to the original except in that the
1562    original set of registers is replaced by a new set of pseudo registers.
1563    The new set has the same modes as the original set.  */
1564
1565 rtx
1566 gen_group_rtx (rtx orig)
1567 {
1568   int i, length;
1569   rtx *tmps;
1570
1571   gcc_assert (GET_CODE (orig) == PARALLEL);
1572
1573   length = XVECLEN (orig, 0);
1574   tmps = XALLOCAVEC (rtx, length);
1575
1576   /* Skip a NULL entry in first slot.  */
1577   i = XEXP (XVECEXP (orig, 0, 0), 0) ? 0 : 1;
1578
1579   if (i)
1580     tmps[0] = 0;
1581
1582   for (; i < length; i++)
1583     {
1584       enum machine_mode mode = GET_MODE (XEXP (XVECEXP (orig, 0, i), 0));
1585       rtx offset = XEXP (XVECEXP (orig, 0, i), 1);
1586
1587       tmps[i] = gen_rtx_EXPR_LIST (VOIDmode, gen_reg_rtx (mode), offset);
1588     }
1589
1590   return gen_rtx_PARALLEL (GET_MODE (orig), gen_rtvec_v (length, tmps));
1591 }
1592
1593 /* A subroutine of emit_group_load.  Arguments as for emit_group_load,
1594    except that values are placed in TMPS[i], and must later be moved
1595    into corresponding XEXP (XVECEXP (DST, 0, i), 0) element.  */
1596
1597 static void
1598 emit_group_load_1 (rtx *tmps, rtx dst, rtx orig_src, tree type, int ssize)
1599 {
1600   rtx src;
1601   int start, i;
1602   enum machine_mode m = GET_MODE (orig_src);
1603
1604   gcc_assert (GET_CODE (dst) == PARALLEL);
1605
1606   if (m != VOIDmode
1607       && !SCALAR_INT_MODE_P (m)
1608       && !MEM_P (orig_src)
1609       && GET_CODE (orig_src) != CONCAT)
1610     {
1611       enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_src));
1612       if (imode == BLKmode)
1613         src = assign_stack_temp (GET_MODE (orig_src), ssize, 0);
1614       else
1615         src = gen_reg_rtx (imode);
1616       if (imode != BLKmode)
1617         src = gen_lowpart (GET_MODE (orig_src), src);
1618       emit_move_insn (src, orig_src);
1619       /* ...and back again.  */
1620       if (imode != BLKmode)
1621         src = gen_lowpart (imode, src);
1622       emit_group_load_1 (tmps, dst, src, type, ssize);
1623       return;
1624     }
1625
1626   /* Check for a NULL entry, used to indicate that the parameter goes
1627      both on the stack and in registers.  */
1628   if (XEXP (XVECEXP (dst, 0, 0), 0))
1629     start = 0;
1630   else
1631     start = 1;
1632
1633   /* Process the pieces.  */
1634   for (i = start; i < XVECLEN (dst, 0); i++)
1635     {
1636       enum machine_mode mode = GET_MODE (XEXP (XVECEXP (dst, 0, i), 0));
1637       HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (dst, 0, i), 1));
1638       unsigned int bytelen = GET_MODE_SIZE (mode);
1639       int shift = 0;
1640
1641       /* Handle trailing fragments that run over the size of the struct.  */
1642       if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
1643         {
1644           /* Arrange to shift the fragment to where it belongs.
1645              extract_bit_field loads to the lsb of the reg.  */
1646           if (
1647 #ifdef BLOCK_REG_PADDING
1648               BLOCK_REG_PADDING (GET_MODE (orig_src), type, i == start)
1649               == (BYTES_BIG_ENDIAN ? upward : downward)
1650 #else
1651               BYTES_BIG_ENDIAN
1652 #endif
1653               )
1654             shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
1655           bytelen = ssize - bytepos;
1656           gcc_assert (bytelen > 0);
1657         }
1658
1659       /* If we won't be loading directly from memory, protect the real source
1660          from strange tricks we might play; but make sure that the source can
1661          be loaded directly into the destination.  */
1662       src = orig_src;
1663       if (!MEM_P (orig_src)
1664           && (!CONSTANT_P (orig_src)
1665               || (GET_MODE (orig_src) != mode
1666                   && GET_MODE (orig_src) != VOIDmode)))
1667         {
1668           if (GET_MODE (orig_src) == VOIDmode)
1669             src = gen_reg_rtx (mode);
1670           else
1671             src = gen_reg_rtx (GET_MODE (orig_src));
1672
1673           emit_move_insn (src, orig_src);
1674         }
1675
1676       /* Optimize the access just a bit.  */
1677       if (MEM_P (src)
1678           && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (src))
1679               || MEM_ALIGN (src) >= GET_MODE_ALIGNMENT (mode))
1680           && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
1681           && bytelen == GET_MODE_SIZE (mode))
1682         {
1683           tmps[i] = gen_reg_rtx (mode);
1684           emit_move_insn (tmps[i], adjust_address (src, mode, bytepos));
1685         }
1686       else if (COMPLEX_MODE_P (mode)
1687                && GET_MODE (src) == mode
1688                && bytelen == GET_MODE_SIZE (mode))
1689         /* Let emit_move_complex do the bulk of the work.  */
1690         tmps[i] = src;
1691       else if (GET_CODE (src) == CONCAT)
1692         {
1693           unsigned int slen = GET_MODE_SIZE (GET_MODE (src));
1694           unsigned int slen0 = GET_MODE_SIZE (GET_MODE (XEXP (src, 0)));
1695
1696           if ((bytepos == 0 && bytelen == slen0)
1697               || (bytepos != 0 && bytepos + bytelen <= slen))
1698             {
1699               /* The following assumes that the concatenated objects all
1700                  have the same size.  In this case, a simple calculation
1701                  can be used to determine the object and the bit field
1702                  to be extracted.  */
1703               tmps[i] = XEXP (src, bytepos / slen0);
1704               if (! CONSTANT_P (tmps[i])
1705                   && (!REG_P (tmps[i]) || GET_MODE (tmps[i]) != mode))
1706                 tmps[i] = extract_bit_field (tmps[i], bytelen * BITS_PER_UNIT,
1707                                              (bytepos % slen0) * BITS_PER_UNIT,
1708                                              1, false, NULL_RTX, mode, mode);
1709             }
1710           else
1711             {
1712               rtx mem;
1713
1714               gcc_assert (!bytepos);
1715               mem = assign_stack_temp (GET_MODE (src), slen, 0);
1716               emit_move_insn (mem, src);
1717               tmps[i] = extract_bit_field (mem, bytelen * BITS_PER_UNIT,
1718                                            0, 1, false, NULL_RTX, mode, mode);
1719             }
1720         }
1721       /* FIXME: A SIMD parallel will eventually lead to a subreg of a
1722          SIMD register, which is currently broken.  While we get GCC
1723          to emit proper RTL for these cases, let's dump to memory.  */
1724       else if (VECTOR_MODE_P (GET_MODE (dst))
1725                && REG_P (src))
1726         {
1727           int slen = GET_MODE_SIZE (GET_MODE (src));
1728           rtx mem;
1729
1730           mem = assign_stack_temp (GET_MODE (src), slen, 0);
1731           emit_move_insn (mem, src);
1732           tmps[i] = adjust_address (mem, mode, (int) bytepos);
1733         }
1734       else if (CONSTANT_P (src) && GET_MODE (dst) != BLKmode
1735                && XVECLEN (dst, 0) > 1)
1736         tmps[i] = simplify_gen_subreg (mode, src, GET_MODE(dst), bytepos);
1737       else if (CONSTANT_P (src))
1738         {
1739           HOST_WIDE_INT len = (HOST_WIDE_INT) bytelen;
1740
1741           if (len == ssize)
1742             tmps[i] = src;
1743           else
1744             {
1745               rtx first, second;
1746
1747               gcc_assert (2 * len == ssize);
1748               split_double (src, &first, &second);
1749               if (i)
1750                 tmps[i] = second;
1751               else
1752                 tmps[i] = first;
1753             }
1754         }
1755       else if (REG_P (src) && GET_MODE (src) == mode)
1756         tmps[i] = src;
1757       else
1758         tmps[i] = extract_bit_field (src, bytelen * BITS_PER_UNIT,
1759                                      bytepos * BITS_PER_UNIT, 1, false, NULL_RTX,
1760                                      mode, mode);
1761
1762       if (shift)
1763         tmps[i] = expand_shift (LSHIFT_EXPR, mode, tmps[i],
1764                                 shift, tmps[i], 0);
1765     }
1766 }
1767
1768 /* Emit code to move a block SRC of type TYPE to a block DST,
1769    where DST is non-consecutive registers represented by a PARALLEL.
1770    SSIZE represents the total size of block ORIG_SRC in bytes, or -1
1771    if not known.  */
1772
1773 void
1774 emit_group_load (rtx dst, rtx src, tree type, int ssize)
1775 {
1776   rtx *tmps;
1777   int i;
1778
1779   tmps = XALLOCAVEC (rtx, XVECLEN (dst, 0));
1780   emit_group_load_1 (tmps, dst, src, type, ssize);
1781
1782   /* Copy the extracted pieces into the proper (probable) hard regs.  */
1783   for (i = 0; i < XVECLEN (dst, 0); i++)
1784     {
1785       rtx d = XEXP (XVECEXP (dst, 0, i), 0);
1786       if (d == NULL)
1787         continue;
1788       emit_move_insn (d, tmps[i]);
1789     }
1790 }
1791
1792 /* Similar, but load SRC into new pseudos in a format that looks like
1793    PARALLEL.  This can later be fed to emit_group_move to get things
1794    in the right place.  */
1795
1796 rtx
1797 emit_group_load_into_temps (rtx parallel, rtx src, tree type, int ssize)
1798 {
1799   rtvec vec;
1800   int i;
1801
1802   vec = rtvec_alloc (XVECLEN (parallel, 0));
1803   emit_group_load_1 (&RTVEC_ELT (vec, 0), parallel, src, type, ssize);
1804
1805   /* Convert the vector to look just like the original PARALLEL, except
1806      with the computed values.  */
1807   for (i = 0; i < XVECLEN (parallel, 0); i++)
1808     {
1809       rtx e = XVECEXP (parallel, 0, i);
1810       rtx d = XEXP (e, 0);
1811
1812       if (d)
1813         {
1814           d = force_reg (GET_MODE (d), RTVEC_ELT (vec, i));
1815           e = alloc_EXPR_LIST (REG_NOTE_KIND (e), d, XEXP (e, 1));
1816         }
1817       RTVEC_ELT (vec, i) = e;
1818     }
1819
1820   return gen_rtx_PARALLEL (GET_MODE (parallel), vec);
1821 }
1822
1823 /* Emit code to move a block SRC to block DST, where SRC and DST are
1824    non-consecutive groups of registers, each represented by a PARALLEL.  */
1825
1826 void
1827 emit_group_move (rtx dst, rtx src)
1828 {
1829   int i;
1830
1831   gcc_assert (GET_CODE (src) == PARALLEL
1832               && GET_CODE (dst) == PARALLEL
1833               && XVECLEN (src, 0) == XVECLEN (dst, 0));
1834
1835   /* Skip first entry if NULL.  */
1836   for (i = XEXP (XVECEXP (src, 0, 0), 0) ? 0 : 1; i < XVECLEN (src, 0); i++)
1837     emit_move_insn (XEXP (XVECEXP (dst, 0, i), 0),
1838                     XEXP (XVECEXP (src, 0, i), 0));
1839 }
1840
1841 /* Move a group of registers represented by a PARALLEL into pseudos.  */
1842
1843 rtx
1844 emit_group_move_into_temps (rtx src)
1845 {
1846   rtvec vec = rtvec_alloc (XVECLEN (src, 0));
1847   int i;
1848
1849   for (i = 0; i < XVECLEN (src, 0); i++)
1850     {
1851       rtx e = XVECEXP (src, 0, i);
1852       rtx d = XEXP (e, 0);
1853
1854       if (d)
1855         e = alloc_EXPR_LIST (REG_NOTE_KIND (e), copy_to_reg (d), XEXP (e, 1));
1856       RTVEC_ELT (vec, i) = e;
1857     }
1858
1859   return gen_rtx_PARALLEL (GET_MODE (src), vec);
1860 }
1861
1862 /* Emit code to move a block SRC to a block ORIG_DST of type TYPE,
1863    where SRC is non-consecutive registers represented by a PARALLEL.
1864    SSIZE represents the total size of block ORIG_DST, or -1 if not
1865    known.  */
1866
1867 void
1868 emit_group_store (rtx orig_dst, rtx src, tree type ATTRIBUTE_UNUSED, int ssize)
1869 {
1870   rtx *tmps, dst;
1871   int start, finish, i;
1872   enum machine_mode m = GET_MODE (orig_dst);
1873
1874   gcc_assert (GET_CODE (src) == PARALLEL);
1875
1876   if (!SCALAR_INT_MODE_P (m)
1877       && !MEM_P (orig_dst) && GET_CODE (orig_dst) != CONCAT)
1878     {
1879       enum machine_mode imode = int_mode_for_mode (GET_MODE (orig_dst));
1880       if (imode == BLKmode)
1881         dst = assign_stack_temp (GET_MODE (orig_dst), ssize, 0);
1882       else
1883         dst = gen_reg_rtx (imode);
1884       emit_group_store (dst, src, type, ssize);
1885       if (imode != BLKmode)
1886         dst = gen_lowpart (GET_MODE (orig_dst), dst);
1887       emit_move_insn (orig_dst, dst);
1888       return;
1889     }
1890
1891   /* Check for a NULL entry, used to indicate that the parameter goes
1892      both on the stack and in registers.  */
1893   if (XEXP (XVECEXP (src, 0, 0), 0))
1894     start = 0;
1895   else
1896     start = 1;
1897   finish = XVECLEN (src, 0);
1898
1899   tmps = XALLOCAVEC (rtx, finish);
1900
1901   /* Copy the (probable) hard regs into pseudos.  */
1902   for (i = start; i < finish; i++)
1903     {
1904       rtx reg = XEXP (XVECEXP (src, 0, i), 0);
1905       if (!REG_P (reg) || REGNO (reg) < FIRST_PSEUDO_REGISTER)
1906         {
1907           tmps[i] = gen_reg_rtx (GET_MODE (reg));
1908           emit_move_insn (tmps[i], reg);
1909         }
1910       else
1911         tmps[i] = reg;
1912     }
1913
1914   /* If we won't be storing directly into memory, protect the real destination
1915      from strange tricks we might play.  */
1916   dst = orig_dst;
1917   if (GET_CODE (dst) == PARALLEL)
1918     {
1919       rtx temp;
1920
1921       /* We can get a PARALLEL dst if there is a conditional expression in
1922          a return statement.  In that case, the dst and src are the same,
1923          so no action is necessary.  */
1924       if (rtx_equal_p (dst, src))
1925         return;
1926
1927       /* It is unclear if we can ever reach here, but we may as well handle
1928          it.  Allocate a temporary, and split this into a store/load to/from
1929          the temporary.  */
1930
1931       temp = assign_stack_temp (GET_MODE (dst), ssize, 0);
1932       emit_group_store (temp, src, type, ssize);
1933       emit_group_load (dst, temp, type, ssize);
1934       return;
1935     }
1936   else if (!MEM_P (dst) && GET_CODE (dst) != CONCAT)
1937     {
1938       enum machine_mode outer = GET_MODE (dst);
1939       enum machine_mode inner;
1940       HOST_WIDE_INT bytepos;
1941       bool done = false;
1942       rtx temp;
1943
1944       if (!REG_P (dst) || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1945         dst = gen_reg_rtx (outer);
1946
1947       /* Make life a bit easier for combine.  */
1948       /* If the first element of the vector is the low part
1949          of the destination mode, use a paradoxical subreg to
1950          initialize the destination.  */
1951       if (start < finish)
1952         {
1953           inner = GET_MODE (tmps[start]);
1954           bytepos = subreg_lowpart_offset (inner, outer);
1955           if (INTVAL (XEXP (XVECEXP (src, 0, start), 1)) == bytepos)
1956             {
1957               temp = simplify_gen_subreg (outer, tmps[start],
1958                                           inner, 0);
1959               if (temp)
1960                 {
1961                   emit_move_insn (dst, temp);
1962                   done = true;
1963                   start++;
1964                 }
1965             }
1966         }
1967
1968       /* If the first element wasn't the low part, try the last.  */
1969       if (!done
1970           && start < finish - 1)
1971         {
1972           inner = GET_MODE (tmps[finish - 1]);
1973           bytepos = subreg_lowpart_offset (inner, outer);
1974           if (INTVAL (XEXP (XVECEXP (src, 0, finish - 1), 1)) == bytepos)
1975             {
1976               temp = simplify_gen_subreg (outer, tmps[finish - 1],
1977                                           inner, 0);
1978               if (temp)
1979                 {
1980                   emit_move_insn (dst, temp);
1981                   done = true;
1982                   finish--;
1983                 }
1984             }
1985         }
1986
1987       /* Otherwise, simply initialize the result to zero.  */
1988       if (!done)
1989         emit_move_insn (dst, CONST0_RTX (outer));
1990     }
1991
1992   /* Process the pieces.  */
1993   for (i = start; i < finish; i++)
1994     {
1995       HOST_WIDE_INT bytepos = INTVAL (XEXP (XVECEXP (src, 0, i), 1));
1996       enum machine_mode mode = GET_MODE (tmps[i]);
1997       unsigned int bytelen = GET_MODE_SIZE (mode);
1998       unsigned int adj_bytelen = bytelen;
1999       rtx dest = dst;
2000
2001       /* Handle trailing fragments that run over the size of the struct.  */
2002       if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2003         adj_bytelen = ssize - bytepos;
2004
2005       if (GET_CODE (dst) == CONCAT)
2006         {
2007           if (bytepos + adj_bytelen
2008               <= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2009             dest = XEXP (dst, 0);
2010           else if (bytepos >= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0))))
2011             {
2012               bytepos -= GET_MODE_SIZE (GET_MODE (XEXP (dst, 0)));
2013               dest = XEXP (dst, 1);
2014             }
2015           else
2016             {
2017               enum machine_mode dest_mode = GET_MODE (dest);
2018               enum machine_mode tmp_mode = GET_MODE (tmps[i]);
2019
2020               gcc_assert (bytepos == 0 && XVECLEN (src, 0));
2021
2022               if (GET_MODE_ALIGNMENT (dest_mode)
2023                   >= GET_MODE_ALIGNMENT (tmp_mode))
2024                 {
2025                   dest = assign_stack_temp (dest_mode,
2026                                             GET_MODE_SIZE (dest_mode),
2027                                             0);
2028                   emit_move_insn (adjust_address (dest,
2029                                                   tmp_mode,
2030                                                   bytepos),
2031                                   tmps[i]);
2032                   dst = dest;
2033                 }
2034               else
2035                 {
2036                   dest = assign_stack_temp (tmp_mode,
2037                                             GET_MODE_SIZE (tmp_mode),
2038                                             0);
2039                   emit_move_insn (dest, tmps[i]);
2040                   dst = adjust_address (dest, dest_mode, bytepos);
2041                 }
2042               break;
2043             }
2044         }
2045
2046       if (ssize >= 0 && bytepos + (HOST_WIDE_INT) bytelen > ssize)
2047         {
2048           /* store_bit_field always takes its value from the lsb.
2049              Move the fragment to the lsb if it's not already there.  */
2050           if (
2051 #ifdef BLOCK_REG_PADDING
2052               BLOCK_REG_PADDING (GET_MODE (orig_dst), type, i == start)
2053               == (BYTES_BIG_ENDIAN ? upward : downward)
2054 #else
2055               BYTES_BIG_ENDIAN
2056 #endif
2057               )
2058             {
2059               int shift = (bytelen - (ssize - bytepos)) * BITS_PER_UNIT;
2060               tmps[i] = expand_shift (RSHIFT_EXPR, mode, tmps[i],
2061                                       shift, tmps[i], 0);
2062             }
2063           bytelen = adj_bytelen;
2064         }
2065
2066       /* Optimize the access just a bit.  */
2067       if (MEM_P (dest)
2068           && (! SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (dest))
2069               || MEM_ALIGN (dest) >= GET_MODE_ALIGNMENT (mode))
2070           && bytepos * BITS_PER_UNIT % GET_MODE_ALIGNMENT (mode) == 0
2071           && bytelen == GET_MODE_SIZE (mode))
2072         emit_move_insn (adjust_address (dest, mode, bytepos), tmps[i]);
2073       else
2074         store_bit_field (dest, bytelen * BITS_PER_UNIT, bytepos * BITS_PER_UNIT,
2075                          0, 0, mode, tmps[i]);
2076     }
2077
2078   /* Copy from the pseudo into the (probable) hard reg.  */
2079   if (orig_dst != dst)
2080     emit_move_insn (orig_dst, dst);
2081 }
2082
2083 /* Generate code to copy a BLKmode object of TYPE out of a
2084    set of registers starting with SRCREG into TGTBLK.  If TGTBLK
2085    is null, a stack temporary is created.  TGTBLK is returned.
2086
2087    The purpose of this routine is to handle functions that return
2088    BLKmode structures in registers.  Some machines (the PA for example)
2089    want to return all small structures in registers regardless of the
2090    structure's alignment.  */
2091
2092 rtx
2093 copy_blkmode_from_reg (rtx tgtblk, rtx srcreg, tree type)
2094 {
2095   unsigned HOST_WIDE_INT bytes = int_size_in_bytes (type);
2096   rtx src = NULL, dst = NULL;
2097   unsigned HOST_WIDE_INT bitsize = MIN (TYPE_ALIGN (type), BITS_PER_WORD);
2098   unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0;
2099   enum machine_mode copy_mode;
2100
2101   if (tgtblk == 0)
2102     {
2103       tgtblk = assign_temp (build_qualified_type (type,
2104                                                   (TYPE_QUALS (type)
2105                                                    | TYPE_QUAL_CONST)),
2106                             0, 1, 1);
2107       preserve_temp_slots (tgtblk);
2108     }
2109
2110   /* This code assumes srcreg is at least a full word.  If it isn't, copy it
2111      into a new pseudo which is a full word.  */
2112
2113   if (GET_MODE (srcreg) != BLKmode
2114       && GET_MODE_SIZE (GET_MODE (srcreg)) < UNITS_PER_WORD)
2115     srcreg = convert_to_mode (word_mode, srcreg, TYPE_UNSIGNED (type));
2116
2117   /* If the structure doesn't take up a whole number of words, see whether
2118      SRCREG is padded on the left or on the right.  If it's on the left,
2119      set PADDING_CORRECTION to the number of bits to skip.
2120
2121      In most ABIs, the structure will be returned at the least end of
2122      the register, which translates to right padding on little-endian
2123      targets and left padding on big-endian targets.  The opposite
2124      holds if the structure is returned at the most significant
2125      end of the register.  */
2126   if (bytes % UNITS_PER_WORD != 0
2127       && (targetm.calls.return_in_msb (type)
2128           ? !BYTES_BIG_ENDIAN
2129           : BYTES_BIG_ENDIAN))
2130     padding_correction
2131       = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD) * BITS_PER_UNIT));
2132
2133   /* Copy the structure BITSIZE bits at a time.  If the target lives in
2134      memory, take care of not reading/writing past its end by selecting
2135      a copy mode suited to BITSIZE.  This should always be possible given
2136      how it is computed.
2137
2138      We could probably emit more efficient code for machines which do not use
2139      strict alignment, but it doesn't seem worth the effort at the current
2140      time.  */
2141
2142   copy_mode = word_mode;
2143   if (MEM_P (tgtblk))
2144     {
2145       enum machine_mode mem_mode = mode_for_size (bitsize, MODE_INT, 1);
2146       if (mem_mode != BLKmode)
2147         copy_mode = mem_mode;
2148     }
2149
2150   for (bitpos = 0, xbitpos = padding_correction;
2151        bitpos < bytes * BITS_PER_UNIT;
2152        bitpos += bitsize, xbitpos += bitsize)
2153     {
2154       /* We need a new source operand each time xbitpos is on a
2155          word boundary and when xbitpos == padding_correction
2156          (the first time through).  */
2157       if (xbitpos % BITS_PER_WORD == 0
2158           || xbitpos == padding_correction)
2159         src = operand_subword_force (srcreg, xbitpos / BITS_PER_WORD,
2160                                      GET_MODE (srcreg));
2161
2162       /* We need a new destination operand each time bitpos is on
2163          a word boundary.  */
2164       if (bitpos % BITS_PER_WORD == 0)
2165         dst = operand_subword (tgtblk, bitpos / BITS_PER_WORD, 1, BLKmode);
2166
2167       /* Use xbitpos for the source extraction (right justified) and
2168          bitpos for the destination store (left justified).  */
2169       store_bit_field (dst, bitsize, bitpos % BITS_PER_WORD, 0, 0, copy_mode,
2170                        extract_bit_field (src, bitsize,
2171                                           xbitpos % BITS_PER_WORD, 1, false,
2172                                           NULL_RTX, copy_mode, copy_mode));
2173     }
2174
2175   return tgtblk;
2176 }
2177
2178 /* Copy BLKmode value SRC into a register of mode MODE.  Return the
2179    register if it contains any data, otherwise return null.
2180
2181    This is used on targets that return BLKmode values in registers.  */
2182
2183 rtx
2184 copy_blkmode_to_reg (enum machine_mode mode, tree src)
2185 {
2186   int i, n_regs;
2187   unsigned HOST_WIDE_INT bitpos, xbitpos, padding_correction = 0, bytes;
2188   unsigned int bitsize;
2189   rtx *dst_words, dst, x, src_word = NULL_RTX, dst_word = NULL_RTX;
2190   enum machine_mode dst_mode;
2191
2192   gcc_assert (TYPE_MODE (TREE_TYPE (src)) == BLKmode);
2193
2194   x = expand_normal (src);
2195
2196   bytes = int_size_in_bytes (TREE_TYPE (src));
2197   if (bytes == 0)
2198     return NULL_RTX;
2199
2200   /* If the structure doesn't take up a whole number of words, see
2201      whether the register value should be padded on the left or on
2202      the right.  Set PADDING_CORRECTION to the number of padding
2203      bits needed on the left side.
2204
2205      In most ABIs, the structure will be returned at the least end of
2206      the register, which translates to right padding on little-endian
2207      targets and left padding on big-endian targets.  The opposite
2208      holds if the structure is returned at the most significant
2209      end of the register.  */
2210   if (bytes % UNITS_PER_WORD != 0
2211       && (targetm.calls.return_in_msb (TREE_TYPE (src))
2212           ? !BYTES_BIG_ENDIAN
2213           : BYTES_BIG_ENDIAN))
2214     padding_correction = (BITS_PER_WORD - ((bytes % UNITS_PER_WORD)
2215                                            * BITS_PER_UNIT));
2216
2217   n_regs = (bytes + UNITS_PER_WORD - 1) / UNITS_PER_WORD;
2218   dst_words = XALLOCAVEC (rtx, n_regs);
2219   bitsize = MIN (TYPE_ALIGN (TREE_TYPE (src)), BITS_PER_WORD);
2220
2221   /* Copy the structure BITSIZE bits at a time.  */
2222   for (bitpos = 0, xbitpos = padding_correction;
2223        bitpos < bytes * BITS_PER_UNIT;
2224        bitpos += bitsize, xbitpos += bitsize)
2225     {
2226       /* We need a new destination pseudo each time xbitpos is
2227          on a word boundary and when xbitpos == padding_correction
2228          (the first time through).  */
2229       if (xbitpos % BITS_PER_WORD == 0
2230           || xbitpos == padding_correction)
2231         {
2232           /* Generate an appropriate register.  */
2233           dst_word = gen_reg_rtx (word_mode);
2234           dst_words[xbitpos / BITS_PER_WORD] = dst_word;
2235
2236           /* Clear the destination before we move anything into it.  */
2237           emit_move_insn (dst_word, CONST0_RTX (word_mode));
2238         }
2239
2240       /* We need a new source operand each time bitpos is on a word
2241          boundary.  */
2242       if (bitpos % BITS_PER_WORD == 0)
2243         src_word = operand_subword_force (x, bitpos / BITS_PER_WORD, BLKmode);
2244
2245       /* Use bitpos for the source extraction (left justified) and
2246          xbitpos for the destination store (right justified).  */
2247       store_bit_field (dst_word, bitsize, xbitpos % BITS_PER_WORD,
2248                        0, 0, word_mode,
2249                        extract_bit_field (src_word, bitsize,
2250                                           bitpos % BITS_PER_WORD, 1, false,
2251                                           NULL_RTX, word_mode, word_mode));
2252     }
2253
2254   if (mode == BLKmode)
2255     {
2256       /* Find the smallest integer mode large enough to hold the
2257          entire structure.  */
2258       for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT);
2259            mode != VOIDmode;
2260            mode = GET_MODE_WIDER_MODE (mode))
2261         /* Have we found a large enough mode?  */
2262         if (GET_MODE_SIZE (mode) >= bytes)
2263           break;
2264
2265       /* A suitable mode should have been found.  */
2266       gcc_assert (mode != VOIDmode);
2267     }
2268
2269   if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (word_mode))
2270     dst_mode = word_mode;
2271   else
2272     dst_mode = mode;
2273   dst = gen_reg_rtx (dst_mode);
2274
2275   for (i = 0; i < n_regs; i++)
2276     emit_move_insn (operand_subword (dst, i, 0, dst_mode), dst_words[i]);
2277
2278   if (mode != dst_mode)
2279     dst = gen_lowpart (mode, dst);
2280
2281   return dst;
2282 }
2283
2284 /* Add a USE expression for REG to the (possibly empty) list pointed
2285    to by CALL_FUSAGE.  REG must denote a hard register.  */
2286
2287 void
2288 use_reg_mode (rtx *call_fusage, rtx reg, enum machine_mode mode)
2289 {
2290   gcc_assert (REG_P (reg) && REGNO (reg) < FIRST_PSEUDO_REGISTER);
2291
2292   *call_fusage
2293     = gen_rtx_EXPR_LIST (mode, gen_rtx_USE (VOIDmode, reg), *call_fusage);
2294 }
2295
2296 /* Add USE expressions to *CALL_FUSAGE for each of NREGS consecutive regs,
2297    starting at REGNO.  All of these registers must be hard registers.  */
2298
2299 void
2300 use_regs (rtx *call_fusage, int regno, int nregs)
2301 {
2302   int i;
2303
2304   gcc_assert (regno + nregs <= FIRST_PSEUDO_REGISTER);
2305
2306   for (i = 0; i < nregs; i++)
2307     use_reg (call_fusage, regno_reg_rtx[regno + i]);
2308 }
2309
2310 /* Add USE expressions to *CALL_FUSAGE for each REG contained in the
2311    PARALLEL REGS.  This is for calls that pass values in multiple
2312    non-contiguous locations.  The Irix 6 ABI has examples of this.  */
2313
2314 void
2315 use_group_regs (rtx *call_fusage, rtx regs)
2316 {
2317   int i;
2318
2319   for (i = 0; i < XVECLEN (regs, 0); i++)
2320     {
2321       rtx reg = XEXP (XVECEXP (regs, 0, i), 0);
2322
2323       /* A NULL entry means the parameter goes both on the stack and in
2324          registers.  This can also be a MEM for targets that pass values
2325          partially on the stack and partially in registers.  */
2326       if (reg != 0 && REG_P (reg))
2327         use_reg (call_fusage, reg);
2328     }
2329 }
2330
2331 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2332    assigment and the code of the expresion on the RHS is CODE.  Return
2333    NULL otherwise.  */
2334
2335 static gimple
2336 get_def_for_expr (tree name, enum tree_code code)
2337 {
2338   gimple def_stmt;
2339
2340   if (TREE_CODE (name) != SSA_NAME)
2341     return NULL;
2342
2343   def_stmt = get_gimple_for_ssa_name (name);
2344   if (!def_stmt
2345       || gimple_assign_rhs_code (def_stmt) != code)
2346     return NULL;
2347
2348   return def_stmt;
2349 }
2350
2351 /* Return the defining gimple statement for SSA_NAME NAME if it is an
2352    assigment and the class of the expresion on the RHS is CLASS.  Return
2353    NULL otherwise.  */
2354
2355 static gimple
2356 get_def_for_expr_class (tree name, enum tree_code_class tclass)
2357 {
2358   gimple def_stmt;
2359
2360   if (TREE_CODE (name) != SSA_NAME)
2361     return NULL;
2362
2363   def_stmt = get_gimple_for_ssa_name (name);
2364   if (!def_stmt
2365       || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) != tclass)
2366     return NULL;
2367
2368   return def_stmt;
2369 }
2370 \f
2371
2372 /* Determine whether the LEN bytes generated by CONSTFUN can be
2373    stored to memory using several move instructions.  CONSTFUNDATA is
2374    a pointer which will be passed as argument in every CONSTFUN call.
2375    ALIGN is maximum alignment we can assume.  MEMSETP is true if this is
2376    a memset operation and false if it's a copy of a constant string.
2377    Return nonzero if a call to store_by_pieces should succeed.  */
2378
2379 int
2380 can_store_by_pieces (unsigned HOST_WIDE_INT len,
2381                      rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
2382                      void *constfundata, unsigned int align, bool memsetp)
2383 {
2384   unsigned HOST_WIDE_INT l;
2385   unsigned int max_size;
2386   HOST_WIDE_INT offset = 0;
2387   enum machine_mode mode;
2388   enum insn_code icode;
2389   int reverse;
2390   /* cst is set but not used if LEGITIMATE_CONSTANT doesn't use it.  */
2391   rtx cst ATTRIBUTE_UNUSED;
2392
2393   if (len == 0)
2394     return 1;
2395
2396   if (! (memsetp
2397          ? SET_BY_PIECES_P (len, align)
2398          : STORE_BY_PIECES_P (len, align)))
2399     return 0;
2400
2401   align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2402
2403   /* We would first store what we can in the largest integer mode, then go to
2404      successively smaller modes.  */
2405
2406   for (reverse = 0;
2407        reverse <= (HAVE_PRE_DECREMENT || HAVE_POST_DECREMENT);
2408        reverse++)
2409     {
2410       l = len;
2411       max_size = STORE_MAX_PIECES + 1;
2412       while (max_size > 1)
2413         {
2414           mode = widest_int_mode_for_size (max_size);
2415
2416           if (mode == VOIDmode)
2417             break;
2418
2419           icode = optab_handler (mov_optab, mode);
2420           if (icode != CODE_FOR_nothing
2421               && align >= GET_MODE_ALIGNMENT (mode))
2422             {
2423               unsigned int size = GET_MODE_SIZE (mode);
2424
2425               while (l >= size)
2426                 {
2427                   if (reverse)
2428                     offset -= size;
2429
2430                   cst = (*constfun) (constfundata, offset, mode);
2431                   if (!targetm.legitimate_constant_p (mode, cst))
2432                     return 0;
2433
2434                   if (!reverse)
2435                     offset += size;
2436
2437                   l -= size;
2438                 }
2439             }
2440
2441           max_size = GET_MODE_SIZE (mode);
2442         }
2443
2444       /* The code above should have handled everything.  */
2445       gcc_assert (!l);
2446     }
2447
2448   return 1;
2449 }
2450
2451 /* Generate several move instructions to store LEN bytes generated by
2452    CONSTFUN to block TO.  (A MEM rtx with BLKmode).  CONSTFUNDATA is a
2453    pointer which will be passed as argument in every CONSTFUN call.
2454    ALIGN is maximum alignment we can assume.  MEMSETP is true if this is
2455    a memset operation and false if it's a copy of a constant string.
2456    If ENDP is 0 return to, if ENDP is 1 return memory at the end ala
2457    mempcpy, and if ENDP is 2 return memory the end minus one byte ala
2458    stpcpy.  */
2459
2460 rtx
2461 store_by_pieces (rtx to, unsigned HOST_WIDE_INT len,
2462                  rtx (*constfun) (void *, HOST_WIDE_INT, enum machine_mode),
2463                  void *constfundata, unsigned int align, bool memsetp, int endp)
2464 {
2465   enum machine_mode to_addr_mode = get_address_mode (to);
2466   struct store_by_pieces_d data;
2467
2468   if (len == 0)
2469     {
2470       gcc_assert (endp != 2);
2471       return to;
2472     }
2473
2474   gcc_assert (memsetp
2475               ? SET_BY_PIECES_P (len, align)
2476               : STORE_BY_PIECES_P (len, align));
2477   data.constfun = constfun;
2478   data.constfundata = constfundata;
2479   data.len = len;
2480   data.to = to;
2481   store_by_pieces_1 (&data, align);
2482   if (endp)
2483     {
2484       rtx to1;
2485
2486       gcc_assert (!data.reverse);
2487       if (data.autinc_to)
2488         {
2489           if (endp == 2)
2490             {
2491               if (HAVE_POST_INCREMENT && data.explicit_inc_to > 0)
2492                 emit_insn (gen_add2_insn (data.to_addr, constm1_rtx));
2493               else
2494                 data.to_addr = copy_to_mode_reg (to_addr_mode,
2495                                                  plus_constant (to_addr_mode,
2496                                                                 data.to_addr,
2497                                                                 -1));
2498             }
2499           to1 = adjust_automodify_address (data.to, QImode, data.to_addr,
2500                                            data.offset);
2501         }
2502       else
2503         {
2504           if (endp == 2)
2505             --data.offset;
2506           to1 = adjust_address (data.to, QImode, data.offset);
2507         }
2508       return to1;
2509     }
2510   else
2511     return data.to;
2512 }
2513
2514 /* Generate several move instructions to clear LEN bytes of block TO.  (A MEM
2515    rtx with BLKmode).  ALIGN is maximum alignment we can assume.  */
2516
2517 static void
2518 clear_by_pieces (rtx to, unsigned HOST_WIDE_INT len, unsigned int align)
2519 {
2520   struct store_by_pieces_d data;
2521
2522   if (len == 0)
2523     return;
2524
2525   data.constfun = clear_by_pieces_1;
2526   data.constfundata = NULL;
2527   data.len = len;
2528   data.to = to;
2529   store_by_pieces_1 (&data, align);
2530 }
2531
2532 /* Callback routine for clear_by_pieces.
2533    Return const0_rtx unconditionally.  */
2534
2535 static rtx
2536 clear_by_pieces_1 (void *data ATTRIBUTE_UNUSED,
2537                    HOST_WIDE_INT offset ATTRIBUTE_UNUSED,
2538                    enum machine_mode mode ATTRIBUTE_UNUSED)
2539 {
2540   return const0_rtx;
2541 }
2542
2543 /* Subroutine of clear_by_pieces and store_by_pieces.
2544    Generate several move instructions to store LEN bytes of block TO.  (A MEM
2545    rtx with BLKmode).  ALIGN is maximum alignment we can assume.  */
2546
2547 static void
2548 store_by_pieces_1 (struct store_by_pieces_d *data ATTRIBUTE_UNUSED,
2549                    unsigned int align ATTRIBUTE_UNUSED)
2550 {
2551   enum machine_mode to_addr_mode = get_address_mode (data->to);
2552   rtx to_addr = XEXP (data->to, 0);
2553   unsigned int max_size = STORE_MAX_PIECES + 1;
2554   enum insn_code icode;
2555
2556   data->offset = 0;
2557   data->to_addr = to_addr;
2558   data->autinc_to
2559     = (GET_CODE (to_addr) == PRE_INC || GET_CODE (to_addr) == PRE_DEC
2560        || GET_CODE (to_addr) == POST_INC || GET_CODE (to_addr) == POST_DEC);
2561
2562   data->explicit_inc_to = 0;
2563   data->reverse
2564     = (GET_CODE (to_addr) == PRE_DEC || GET_CODE (to_addr) == POST_DEC);
2565   if (data->reverse)
2566     data->offset = data->len;
2567
2568   /* If storing requires more than two move insns,
2569      copy addresses to registers (to make displacements shorter)
2570      and use post-increment if available.  */
2571   if (!data->autinc_to
2572       && move_by_pieces_ninsns (data->len, align, max_size) > 2)
2573     {
2574       /* Determine the main mode we'll be using.
2575          MODE might not be used depending on the definitions of the
2576          USE_* macros below.  */
2577       enum machine_mode mode ATTRIBUTE_UNUSED
2578         = widest_int_mode_for_size (max_size);
2579
2580       if (USE_STORE_PRE_DECREMENT (mode) && data->reverse && ! data->autinc_to)
2581         {
2582           data->to_addr = copy_to_mode_reg (to_addr_mode,
2583                                             plus_constant (to_addr_mode,
2584                                                            to_addr,
2585                                                            data->len));
2586           data->autinc_to = 1;
2587           data->explicit_inc_to = -1;
2588         }
2589
2590       if (USE_STORE_POST_INCREMENT (mode) && ! data->reverse
2591           && ! data->autinc_to)
2592         {
2593           data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2594           data->autinc_to = 1;
2595           data->explicit_inc_to = 1;
2596         }
2597
2598       if ( !data->autinc_to && CONSTANT_P (to_addr))
2599         data->to_addr = copy_to_mode_reg (to_addr_mode, to_addr);
2600     }
2601
2602   align = alignment_for_piecewise_move (STORE_MAX_PIECES, align);
2603
2604   /* First store what we can in the largest integer mode, then go to
2605      successively smaller modes.  */
2606
2607   while (max_size > 1)
2608     {
2609       enum machine_mode mode = widest_int_mode_for_size (max_size);
2610
2611       if (mode == VOIDmode)
2612         break;
2613
2614       icode = optab_handler (mov_optab, mode);
2615       if (icode != CODE_FOR_nothing && align >= GET_MODE_ALIGNMENT (mode))
2616         store_by_pieces_2 (GEN_FCN (icode), mode, data);
2617
2618       max_size = GET_MODE_SIZE (mode);
2619     }
2620
2621   /* The code above should have handled everything.  */
2622   gcc_assert (!data->len);
2623 }
2624
2625 /* Subroutine of store_by_pieces_1.  Store as many bytes as appropriate
2626    with move instructions for mode MODE.  GENFUN is the gen_... function
2627    to make a move insn for that mode.  DATA has all the other info.  */
2628
2629 static void
2630 store_by_pieces_2 (rtx (*genfun) (rtx, ...), enum machine_mode mode,
2631                    struct store_by_pieces_d *data)
2632 {
2633   unsigned int size = GET_MODE_SIZE (mode);
2634   rtx to1, cst;
2635
2636   while (data->len >= size)
2637     {
2638       if (data->reverse)
2639         data->offset -= size;
2640
2641       if (data->autinc_to)
2642         to1 = adjust_automodify_address (data->to, mode, data->to_addr,
2643                                          data->offset);
2644       else
2645         to1 = adjust_address (data->to, mode, data->offset);
2646
2647       if (HAVE_PRE_DECREMENT && data->explicit_inc_to < 0)
2648         emit_insn (gen_add2_insn (data->to_addr,
2649                                   GEN_INT (-(HOST_WIDE_INT) size)));
2650
2651       cst = (*data->constfun) (data->constfundata, data->offset, mode);
2652       emit_insn ((*genfun) (to1, cst));
2653
2654       if (HAVE_POST_INCREMENT && data->explicit_inc_to > 0)
2655         emit_insn (gen_add2_insn (data->to_addr, GEN_INT (size)));
2656
2657       if (! data->reverse)
2658         data->offset += size;
2659
2660       data->len -= size;
2661     }
2662 }
2663 \f
2664 /* Write zeros through the storage of OBJECT.  If OBJECT has BLKmode, SIZE is
2665    its length in bytes.  */
2666
2667 rtx
2668 clear_storage_hints (rtx object, rtx size, enum block_op_methods method,
2669                      unsigned int expected_align, HOST_WIDE_INT expected_size)
2670 {
2671   enum machine_mode mode = GET_MODE (object);
2672   unsigned int align;
2673
2674   gcc_assert (method == BLOCK_OP_NORMAL || method == BLOCK_OP_TAILCALL);
2675
2676   /* If OBJECT is not BLKmode and SIZE is the same size as its mode,
2677      just move a zero.  Otherwise, do this a piece at a time.  */
2678   if (mode != BLKmode
2679       && CONST_INT_P (size)
2680       && INTVAL (size) == (HOST_WIDE_INT) GET_MODE_SIZE (mode))
2681     {
2682       rtx zero = CONST0_RTX (mode);
2683       if (zero != NULL)
2684         {
2685           emit_move_insn (object, zero);
2686           return NULL;
2687         }
2688
2689       if (COMPLEX_MODE_P (mode))
2690         {
2691           zero = CONST0_RTX (GET_MODE_INNER (mode));
2692           if (zero != NULL)
2693             {
2694               write_complex_part (object, zero, 0);
2695               write_complex_part (object, zero, 1);
2696               return NULL;
2697             }
2698         }
2699     }
2700
2701   if (size == const0_rtx)
2702     return NULL;
2703
2704   align = MEM_ALIGN (object);
2705
2706   if (CONST_INT_P (size)
2707       && CLEAR_BY_PIECES_P (INTVAL (size), align))
2708     clear_by_pieces (object, INTVAL (size), align);
2709   else if (set_storage_via_setmem (object, size, const0_rtx, align,
2710                                    expected_align, expected_size))
2711     ;
2712   else if (ADDR_SPACE_GENERIC_P (MEM_ADDR_SPACE (object)))
2713     return set_storage_via_libcall (object, size, const0_rtx,
2714                                     method == BLOCK_OP_TAILCALL);
2715   else
2716     gcc_unreachable ();
2717
2718   return NULL;
2719 }
2720
2721 rtx
2722 clear_storage (rtx object, rtx size, enum block_op_methods method)
2723 {
2724   return clear_storage_hints (object, size, method, 0, -1);
2725 }
2726
2727
2728 /* A subroutine of clear_storage.  Expand a call to memset.
2729    Return the return value of memset, 0 otherwise.  */
2730
2731 rtx
2732 set_storage_via_libcall (rtx object, rtx size, rtx val, bool tailcall)
2733 {
2734   tree call_expr, fn, object_tree, size_tree, val_tree;
2735   enum machine_mode size_mode;
2736   rtx retval;
2737
2738   /* Emit code to copy OBJECT and SIZE into new pseudos.  We can then
2739      place those into new pseudos into a VAR_DECL and use them later.  */
2740
2741   object = copy_addr_to_reg (XEXP (object, 0));
2742
2743   size_mode = TYPE_MODE (sizetype);
2744   size = convert_to_mode (size_mode, size, 1);
2745   size = copy_to_mode_reg (size_mode, size);
2746
2747   /* It is incorrect to use the libcall calling conventions to call
2748      memset in this context.  This could be a user call to memset and
2749      the user may wish to examine the return value from memset.  For
2750      targets where libcalls and normal calls have different conventions
2751      for returning pointers, we could end up generating incorrect code.  */
2752
2753   object_tree = make_tree (ptr_type_node, object);
2754   if (!CONST_INT_P (val))
2755     val = convert_to_mode (TYPE_MODE (integer_type_node), val, 1);
2756   size_tree = make_tree (sizetype, size);
2757   val_tree = make_tree (integer_type_node, val);
2758
2759   fn = clear_storage_libcall_fn (true);
2760   call_expr = build_call_expr (fn, 3, object_tree, val_tree, size_tree);
2761   CALL_EXPR_TAILCALL (call_expr) = tailcall;
2762
2763   retval = expand_normal (call_expr);
2764
2765   return retval;
2766 }
2767
2768 /* A subroutine of set_storage_via_libcall.  Create the tree node
2769    for the function we use for block clears.  */
2770
2771 tree block_clear_fn;
2772
2773 void
2774 init_block_clear_fn (const char *asmspec)
2775 {
2776   if (!block_clear_fn)
2777     {
2778       tree fn, args;
2779
2780       fn = get_identifier ("memset");
2781       args = build_function_type_list (ptr_type_node, ptr_type_node,
2782                                        integer_type_node, sizetype,
2783                                        NULL_TREE);
2784
2785       fn = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL, fn, args);
2786       DECL_EXTERNAL (fn) = 1;
2787       TREE_PUBLIC (fn) = 1;
2788       DECL_ARTIFICIAL (fn) = 1;
2789       TREE_NOTHROW (fn) = 1;
2790       DECL_VISIBILITY (fn) = VISIBILITY_DEFAULT;
2791       DECL_VISIBILITY_SPECIFIED (fn) = 1;
2792
2793       block_clear_fn = fn;
2794     }
2795
2796   if (asmspec)
2797     set_user_assembler_name (block_clear_fn, asmspec);
2798 }
2799
2800 static tree
2801 clear_storage_libcall_fn (int for_call)
2802 {
2803   static bool emitted_extern;
2804
2805   if (!block_clear_fn)
2806     init_block_clear_fn (NULL);
2807
2808   if (for_call && !emitted_extern)
2809     {
2810       emitted_extern = true;
2811       make_decl_rtl (block_clear_fn);
2812     }
2813
2814   return block_clear_fn;
2815 }
2816 \f
2817 /* Expand a setmem pattern; return true if successful.  */
2818
2819 bool
2820 set_storage_via_setmem (rtx object, rtx size, rtx val, unsigned int align,
2821                         unsigned int expected_align, HOST_WIDE_INT expected_size)
2822 {
2823   /* Try the most limited insn first, because there's no point
2824      including more than one in the machine description unless
2825      the more limited one has some advantage.  */
2826
2827   enum machine_mode mode;
2828
2829   if (expected_align < align)
2830     expected_align = align;
2831
2832   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2833        mode = GET_MODE_WIDER_MODE (mode))
2834     {
2835       enum insn_code code = direct_optab_handler (setmem_optab, mode);
2836
2837       if (code != CODE_FOR_nothing
2838           /* We don't need MODE to be narrower than
2839              BITS_PER_HOST_WIDE_INT here because if SIZE is less than
2840              the mode mask, as it is returned by the macro, it will
2841              definitely be less than the actual mode mask.  */
2842           && ((CONST_INT_P (size)
2843                && ((unsigned HOST_WIDE_INT) INTVAL (size)
2844                    <= (GET_MODE_MASK (mode) >> 1)))
2845               || GET_MODE_BITSIZE (mode) >= BITS_PER_WORD))
2846         {
2847           struct expand_operand ops[6];
2848           unsigned int nops;
2849
2850           nops = insn_data[(int) code].n_generator_args;
2851           gcc_assert (nops == 4 || nops == 6);
2852
2853           create_fixed_operand (&ops[0], object);
2854           /* The check above guarantees that this size conversion is valid.  */
2855           create_convert_operand_to (&ops[1], size, mode, true);
2856           create_convert_operand_from (&ops[2], val, byte_mode, true);
2857           create_integer_operand (&ops[3], align / BITS_PER_UNIT);
2858           if (nops == 6)
2859             {
2860               create_integer_operand (&ops[4], expected_align / BITS_PER_UNIT);
2861               create_integer_operand (&ops[5], expected_size);
2862             }
2863           if (maybe_expand_insn (code, nops, ops))
2864             return true;
2865         }
2866     }
2867
2868   return false;
2869 }
2870
2871 \f
2872 /* Write to one of the components of the complex value CPLX.  Write VAL to
2873    the real part if IMAG_P is false, and the imaginary part if its true.  */
2874
2875 static void
2876 write_complex_part (rtx cplx, rtx val, bool imag_p)
2877 {
2878   enum machine_mode cmode;
2879   enum machine_mode imode;
2880   unsigned ibitsize;
2881
2882   if (GET_CODE (cplx) == CONCAT)
2883     {
2884       emit_move_insn (XEXP (cplx, imag_p), val);
2885       return;
2886     }
2887
2888   cmode = GET_MODE (cplx);
2889   imode = GET_MODE_INNER (cmode);
2890   ibitsize = GET_MODE_BITSIZE (imode);
2891
2892   /* For MEMs simplify_gen_subreg may generate an invalid new address
2893      because, e.g., the original address is considered mode-dependent
2894      by the target, which restricts simplify_subreg from invoking
2895      adjust_address_nv.  Instead of preparing fallback support for an
2896      invalid address, we call adjust_address_nv directly.  */
2897   if (MEM_P (cplx))
2898     {
2899       emit_move_insn (adjust_address_nv (cplx, imode,
2900                                          imag_p ? GET_MODE_SIZE (imode) : 0),
2901                       val);
2902       return;
2903     }
2904
2905   /* If the sub-object is at least word sized, then we know that subregging
2906      will work.  This special case is important, since store_bit_field
2907      wants to operate on integer modes, and there's rarely an OImode to
2908      correspond to TCmode.  */
2909   if (ibitsize >= BITS_PER_WORD
2910       /* For hard regs we have exact predicates.  Assume we can split
2911          the original object if it spans an even number of hard regs.
2912          This special case is important for SCmode on 64-bit platforms
2913          where the natural size of floating-point regs is 32-bit.  */
2914       || (REG_P (cplx)
2915           && REGNO (cplx) < FIRST_PSEUDO_REGISTER
2916           && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
2917     {
2918       rtx part = simplify_gen_subreg (imode, cplx, cmode,
2919                                       imag_p ? GET_MODE_SIZE (imode) : 0);
2920       if (part)
2921         {
2922           emit_move_insn (part, val);
2923           return;
2924         }
2925       else
2926         /* simplify_gen_subreg may fail for sub-word MEMs.  */
2927         gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
2928     }
2929
2930   store_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0, 0, 0, imode, val);
2931 }
2932
2933 /* Extract one of the components of the complex value CPLX.  Extract the
2934    real part if IMAG_P is false, and the imaginary part if it's true.  */
2935
2936 static rtx
2937 read_complex_part (rtx cplx, bool imag_p)
2938 {
2939   enum machine_mode cmode, imode;
2940   unsigned ibitsize;
2941
2942   if (GET_CODE (cplx) == CONCAT)
2943     return XEXP (cplx, imag_p);
2944
2945   cmode = GET_MODE (cplx);
2946   imode = GET_MODE_INNER (cmode);
2947   ibitsize = GET_MODE_BITSIZE (imode);
2948
2949   /* Special case reads from complex constants that got spilled to memory.  */
2950   if (MEM_P (cplx) && GET_CODE (XEXP (cplx, 0)) == SYMBOL_REF)
2951     {
2952       tree decl = SYMBOL_REF_DECL (XEXP (cplx, 0));
2953       if (decl && TREE_CODE (decl) == COMPLEX_CST)
2954         {
2955           tree part = imag_p ? TREE_IMAGPART (decl) : TREE_REALPART (decl);
2956           if (CONSTANT_CLASS_P (part))
2957             return expand_expr (part, NULL_RTX, imode, EXPAND_NORMAL);
2958         }
2959     }
2960
2961   /* For MEMs simplify_gen_subreg may generate an invalid new address
2962      because, e.g., the original address is considered mode-dependent
2963      by the target, which restricts simplify_subreg from invoking
2964      adjust_address_nv.  Instead of preparing fallback support for an
2965      invalid address, we call adjust_address_nv directly.  */
2966   if (MEM_P (cplx))
2967     return adjust_address_nv (cplx, imode,
2968                               imag_p ? GET_MODE_SIZE (imode) : 0);
2969
2970   /* If the sub-object is at least word sized, then we know that subregging
2971      will work.  This special case is important, since extract_bit_field
2972      wants to operate on integer modes, and there's rarely an OImode to
2973      correspond to TCmode.  */
2974   if (ibitsize >= BITS_PER_WORD
2975       /* For hard regs we have exact predicates.  Assume we can split
2976          the original object if it spans an even number of hard regs.
2977          This special case is important for SCmode on 64-bit platforms
2978          where the natural size of floating-point regs is 32-bit.  */
2979       || (REG_P (cplx)
2980           && REGNO (cplx) < FIRST_PSEUDO_REGISTER
2981           && hard_regno_nregs[REGNO (cplx)][cmode] % 2 == 0))
2982     {
2983       rtx ret = simplify_gen_subreg (imode, cplx, cmode,
2984                                      imag_p ? GET_MODE_SIZE (imode) : 0);
2985       if (ret)
2986         return ret;
2987       else
2988         /* simplify_gen_subreg may fail for sub-word MEMs.  */
2989         gcc_assert (MEM_P (cplx) && ibitsize < BITS_PER_WORD);
2990     }
2991
2992   return extract_bit_field (cplx, ibitsize, imag_p ? ibitsize : 0,
2993                             true, false, NULL_RTX, imode, imode);
2994 }
2995 \f
2996 /* A subroutine of emit_move_insn_1.  Yet another lowpart generator.
2997    NEW_MODE and OLD_MODE are the same size.  Return NULL if X cannot be
2998    represented in NEW_MODE.  If FORCE is true, this will never happen, as
2999    we'll force-create a SUBREG if needed.  */
3000
3001 static rtx
3002 emit_move_change_mode (enum machine_mode new_mode,
3003                        enum machine_mode old_mode, rtx x, bool force)
3004 {
3005   rtx ret;
3006
3007   if (push_operand (x, GET_MODE (x)))
3008     {
3009       ret = gen_rtx_MEM (new_mode, XEXP (x, 0));
3010       MEM_COPY_ATTRIBUTES (ret, x);
3011     }
3012   else if (MEM_P (x))
3013     {
3014       /* We don't have to worry about changing the address since the
3015          size in bytes is supposed to be the same.  */
3016       if (reload_in_progress)
3017         {
3018           /* Copy the MEM to change the mode and move any
3019              substitutions from the old MEM to the new one.  */
3020           ret = adjust_address_nv (x, new_mode, 0);
3021           copy_replacements (x, ret);
3022         }
3023       else
3024         ret = adjust_address (x, new_mode, 0);
3025     }
3026   else
3027     {
3028       /* Note that we do want simplify_subreg's behavior of validating
3029          that the new mode is ok for a hard register.  If we were to use
3030          simplify_gen_subreg, we would create the subreg, but would
3031          probably run into the target not being able to implement it.  */
3032       /* Except, of course, when FORCE is true, when this is exactly what
3033          we want.  Which is needed for CCmodes on some targets.  */
3034       if (force)
3035         ret = simplify_gen_subreg (new_mode, x, old_mode, 0);
3036       else
3037         ret = simplify_subreg (new_mode, x, old_mode, 0);
3038     }
3039
3040   return ret;
3041 }
3042
3043 /* A subroutine of emit_move_insn_1.  Generate a move from Y into X using
3044    an integer mode of the same size as MODE.  Returns the instruction
3045    emitted, or NULL if such a move could not be generated.  */
3046
3047 static rtx
3048 emit_move_via_integer (enum machine_mode mode, rtx x, rtx y, bool force)
3049 {
3050   enum machine_mode imode;
3051   enum insn_code code;
3052
3053   /* There must exist a mode of the exact size we require.  */
3054   imode = int_mode_for_mode (mode);
3055   if (imode == BLKmode)
3056     return NULL_RTX;
3057
3058   /* The target must support moves in this mode.  */
3059   code = optab_handler (mov_optab, imode);
3060   if (code == CODE_FOR_nothing)
3061     return NULL_RTX;
3062
3063   x = emit_move_change_mode (imode, mode, x, force);
3064   if (x == NULL_RTX)
3065     return NULL_RTX;
3066   y = emit_move_change_mode (imode, mode, y, force);
3067   if (y == NULL_RTX)
3068     return NULL_RTX;
3069   return emit_insn (GEN_FCN (code) (x, y));
3070 }
3071
3072 /* A subroutine of emit_move_insn_1.  X is a push_operand in MODE.
3073    Return an equivalent MEM that does not use an auto-increment.  */
3074
3075 static rtx
3076 emit_move_resolve_push (enum machine_mode mode, rtx x)
3077 {
3078   enum rtx_code code = GET_CODE (XEXP (x, 0));
3079   HOST_WIDE_INT adjust;
3080   rtx temp;
3081
3082   adjust = GET_MODE_SIZE (mode);
3083 #ifdef PUSH_ROUNDING
3084   adjust = PUSH_ROUNDING (adjust);
3085 #endif
3086   if (code == PRE_DEC || code == POST_DEC)
3087     adjust = -adjust;
3088   else if (code == PRE_MODIFY || code == POST_MODIFY)
3089     {
3090       rtx expr = XEXP (XEXP (x, 0), 1);
3091       HOST_WIDE_INT val;
3092
3093       gcc_assert (GET_CODE (expr) == PLUS || GET_CODE (expr) == MINUS);
3094       gcc_assert (CONST_INT_P (XEXP (expr, 1)));
3095       val = INTVAL (XEXP (expr, 1));
3096       if (GET_CODE (expr) == MINUS)
3097         val = -val;
3098       gcc_assert (adjust == val || adjust == -val);
3099       adjust = val;
3100     }
3101
3102   /* Do not use anti_adjust_stack, since we don't want to update
3103      stack_pointer_delta.  */
3104   temp = expand_simple_binop (Pmode, PLUS, stack_pointer_rtx,
3105                               GEN_INT (adjust), stack_pointer_rtx,
3106                               0, OPTAB_LIB_WIDEN);
3107   if (temp != stack_pointer_rtx)
3108     emit_move_insn (stack_pointer_rtx, temp);
3109
3110   switch (code)
3111     {
3112     case PRE_INC:
3113     case PRE_DEC:
3114     case PRE_MODIFY:
3115       temp = stack_pointer_rtx;
3116       break;
3117     case POST_INC:
3118     case POST_DEC:
3119     case POST_MODIFY:
3120       temp = plus_constant (Pmode, stack_pointer_rtx, -adjust);
3121       break;
3122     default:
3123       gcc_unreachable ();
3124     }
3125
3126   return replace_equiv_address (x, temp);
3127 }
3128
3129 /* A subroutine of emit_move_complex.  Generate a move from Y into X.
3130    X is known to satisfy push_operand, and MODE is known to be complex.
3131    Returns the last instruction emitted.  */
3132
3133 rtx
3134 emit_move_complex_push (enum machine_mode mode, rtx x, rtx y)
3135 {
3136   enum machine_mode submode = GET_MODE_INNER (mode);
3137   bool imag_first;
3138
3139 #ifdef PUSH_ROUNDING
3140   unsigned int submodesize = GET_MODE_SIZE (submode);
3141
3142   /* In case we output to the stack, but the size is smaller than the
3143      machine can push exactly, we need to use move instructions.  */
3144   if (PUSH_ROUNDING (submodesize) != submodesize)
3145     {
3146       x = emit_move_resolve_push (mode, x);
3147       return emit_move_insn (x, y);
3148     }
3149 #endif
3150
3151   /* Note that the real part always precedes the imag part in memory
3152      regardless of machine's endianness.  */
3153   switch (GET_CODE (XEXP (x, 0)))
3154     {
3155     case PRE_DEC:
3156     case POST_DEC:
3157       imag_first = true;
3158       break;
3159     case PRE_INC:
3160     case POST_INC:
3161       imag_first = false;
3162       break;
3163     default:
3164       gcc_unreachable ();
3165     }
3166
3167   emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3168                   read_complex_part (y, imag_first));
3169   return emit_move_insn (gen_rtx_MEM (submode, XEXP (x, 0)),
3170                          read_complex_part (y, !imag_first));
3171 }
3172
3173 /* A subroutine of emit_move_complex.  Perform the move from Y to X
3174    via two moves of the parts.  Returns the last instruction emitted.  */
3175
3176 rtx
3177 emit_move_complex_parts (rtx x, rtx y)
3178 {
3179   /* Show the output dies here.  This is necessary for SUBREGs
3180      of pseudos since we cannot track their lifetimes correctly;
3181      hard regs shouldn't appear here except as return values.  */
3182   if (!reload_completed && !reload_in_progress
3183       && REG_P (x) && !reg_overlap_mentioned_p (x, y))
3184     emit_clobber (x);
3185
3186   write_complex_part (x, read_complex_part (y, false), false);
3187   write_complex_part (x, read_complex_part (y, true), true);
3188
3189   return get_last_insn ();
3190 }
3191
3192 /* A subroutine of emit_move_insn_1.  Generate a move from Y into X.
3193    MODE is known to be complex.  Returns the last instruction emitted.  */
3194
3195 static rtx
3196 emit_move_complex (enum machine_mode mode, rtx x, rtx y)
3197 {
3198   bool try_int;
3199
3200   /* Need to take special care for pushes, to maintain proper ordering
3201      of the data, and possibly extra padding.  */
3202   if (push_operand (x, mode))
3203     return emit_move_complex_push (mode, x, y);
3204
3205   /* See if we can coerce the target into moving both values at once.  */
3206
3207   /* Move floating point as parts.  */
3208   if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
3209       && optab_handler (mov_optab, GET_MODE_INNER (mode)) != CODE_FOR_nothing)
3210     try_int = false;
3211   /* Not possible if the values are inherently not adjacent.  */
3212   else if (GET_CODE (x) == CONCAT || GET_CODE (y) == CONCAT)
3213     try_int = false;
3214   /* Is possible if both are registers (or subregs of registers).  */
3215   else if (register_operand (x, mode) && register_operand (y, mode))
3216     try_int = true;
3217   /* If one of the operands is a memory, and alignment constraints
3218      are friendly enough, we may be able to do combined memory operations.
3219      We do not attempt this if Y is a constant because that combination is
3220      usually better with the by-parts thing below.  */
3221   else if ((MEM_P (x) ? !CONSTANT_P (y) : MEM_P (y))
3222            && (!STRICT_ALIGNMENT
3223                || get_mode_alignment (mode) == BIGGEST_ALIGNMENT))
3224     try_int = true;
3225   else
3226     try_int = false;
3227
3228   if (try_int)
3229     {
3230       rtx ret;
3231
3232       /* For memory to memory moves, optimal behavior can be had with the
3233          existing block move logic.  */
3234       if (MEM_P (x) && MEM_P (y))
3235         {
3236           emit_block_move (x, y, GEN_INT (GET_MODE_SIZE (mode)),
3237                            BLOCK_OP_NO_LIBCALL);
3238           return get_last_insn ();
3239         }
3240
3241       ret = emit_move_via_integer (mode, x, y, true);
3242       if (ret)
3243         return ret;
3244     }
3245
3246   return emit_move_complex_parts (x, y);
3247 }
3248
3249 /* A subroutine of emit_move_insn_1.  Generate a move from Y into X.
3250    MODE is known to be MODE_CC.  Returns the last instruction emitted.  */
3251
3252 static rtx
3253 emit_move_ccmode (enum machine_mode mode, rtx x, rtx y)
3254 {
3255   rtx ret;
3256
3257   /* Assume all MODE_CC modes are equivalent; if we have movcc, use it.  */
3258   if (mode != CCmode)
3259     {
3260       enum insn_code code = optab_handler (mov_optab, CCmode);
3261       if (code != CODE_FOR_nothing)
3262         {
3263           x = emit_move_change_mode (CCmode, mode, x, true);
3264           y = emit_move_change_mode (CCmode, mode, y, true);
3265           return emit_insn (GEN_FCN (code) (x, y));
3266         }
3267     }
3268
3269   /* Otherwise, find the MODE_INT mode of the same width.  */
3270   ret = emit_move_via_integer (mode, x, y, false);
3271   gcc_assert (ret != NULL);
3272   return ret;
3273 }
3274
3275 /* Return true if word I of OP lies entirely in the
3276    undefined bits of a paradoxical subreg.  */
3277
3278 static bool
3279 undefined_operand_subword_p (const_rtx op, int i)
3280 {
3281   enum machine_mode innermode, innermostmode;
3282   int offset;
3283   if (GET_CODE (op) != SUBREG)
3284     return false;
3285   innermode = GET_MODE (op);
3286   innermostmode = GET_MODE (SUBREG_REG (op));
3287   offset = i * UNITS_PER_WORD + SUBREG_BYTE (op);
3288   /* The SUBREG_BYTE represents offset, as if the value were stored in
3289      memory, except for a paradoxical subreg where we define
3290      SUBREG_BYTE to be 0; undo this exception as in
3291      simplify_subreg.  */
3292   if (SUBREG_BYTE (op) == 0
3293       && GET_MODE_SIZE (innermostmode) < GET_MODE_SIZE (innermode))
3294     {
3295       int difference = (GET_MODE_SIZE (innermostmode) - GET_MODE_SIZE (innermode));
3296       if (WORDS_BIG_ENDIAN)
3297         offset += (difference / UNITS_PER_WORD) * UNITS_PER_WORD;
3298       if (BYTES_BIG_ENDIAN)
3299         offset += difference % UNITS_PER_WORD;
3300     }
3301   if (offset >= GET_MODE_SIZE (innermostmode)
3302       || offset <= -GET_MODE_SIZE (word_mode))
3303     return true;
3304   return false;
3305 }
3306
3307 /* A subroutine of emit_move_insn_1.  Generate a move from Y into X.
3308    MODE is any multi-word or full-word mode that lacks a move_insn
3309    pattern.  Note that you will get better code if you define such
3310    patterns, even if they must turn into multiple assembler instructions.  */
3311
3312 static rtx
3313 emit_move_multi_word (enum machine_mode mode, rtx x, rtx y)
3314 {
3315   rtx last_insn = 0;
3316   rtx seq, inner;
3317   bool need_clobber;
3318   int i;
3319
3320   gcc_assert (GET_MODE_SIZE (mode) >= UNITS_PER_WORD);
3321
3322   /* If X is a push on the stack, do the push now and replace
3323      X with a reference to the stack pointer.  */
3324   if (push_operand (x, mode))
3325     x = emit_move_resolve_push (mode, x);
3326
3327   /* If we are in reload, see if either operand is a MEM whose address
3328      is scheduled for replacement.  */
3329   if (reload_in_progress && MEM_P (x)
3330       && (inner = find_replacement (&XEXP (x, 0))) != XEXP (x, 0))
3331     x = replace_equiv_address_nv (x, inner);
3332   if (reload_in_progress && MEM_P (y)
3333       && (inner = find_replacement (&XEXP (y, 0))) != XEXP (y, 0))
3334     y = replace_equiv_address_nv (y, inner);
3335
3336   start_sequence ();
3337
3338   need_clobber = false;
3339   for (i = 0;
3340        i < (GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD;
3341        i++)
3342     {
3343       rtx xpart = operand_subword (x, i, 1, mode);
3344       rtx ypart;
3345
3346       /* Do not generate code for a move if it would come entirely
3347          from the undefined bits of a paradoxical subreg.  */
3348       if (undefined_operand_subword_p (y, i))
3349         continue;
3350
3351       ypart = operand_subword (y, i, 1, mode);
3352
3353       /* If we can't get a part of Y, put Y into memory if it is a
3354          constant.  Otherwise, force it into a register.  Then we must
3355          be able to get a part of Y.  */
3356       if (ypart == 0 && CONSTANT_P (y))
3357         {
3358           y = use_anchored_address (force_const_mem (mode, y));
3359           ypart = operand_subword (y, i, 1, mode);
3360         }
3361       else if (ypart == 0)
3362         ypart = operand_subword_force (y, i, mode);
3363
3364       gcc_assert (xpart && ypart);
3365
3366       need_clobber |= (GET_CODE (xpart) == SUBREG);
3367
3368       last_insn = emit_move_insn (xpart, ypart);
3369     }
3370
3371   seq = get_insns ();
3372   end_sequence ();
3373
3374   /* Show the output dies here.  This is necessary for SUBREGs
3375      of pseudos since we cannot track their lifetimes correctly;
3376      hard regs shouldn't appear here except as return values.
3377      We never want to emit such a clobber after reload.  */
3378   if (x != y
3379       && ! (reload_in_progress || reload_completed)
3380       && need_clobber != 0)
3381     emit_clobber (x);
3382
3383   emit_insn (seq);
3384
3385   return last_insn;
3386 }
3387
3388 /* Low level part of emit_move_insn.
3389    Called just like emit_move_insn, but assumes X and Y
3390    are basically valid.  */
3391
3392 rtx
3393 emit_move_insn_1 (rtx x, rtx y)
3394 {
3395   enum machine_mode mode = GET_MODE (x);
3396   enum insn_code code;
3397
3398   gcc_assert ((unsigned int) mode < (unsigned int) MAX_MACHINE_MODE);
3399
3400   code = optab_handler (mov_optab, mode);
3401   if (code != CODE_FOR_nothing)
3402     return emit_insn (GEN_FCN (code) (x, y));
3403
3404   /* Expand complex moves by moving real part and imag part.  */
3405   if (COMPLEX_MODE_P (mode))
3406     return emit_move_complex (mode, x, y);
3407
3408   if (GET_MODE_CLASS (mode) == MODE_DECIMAL_FLOAT
3409       || ALL_FIXED_POINT_MODE_P (mode))
3410     {
3411       rtx result = emit_move_via_integer (mode, x, y, true);
3412
3413       /* If we can't find an integer mode, use multi words.  */
3414       if (result)
3415         return result;
3416       else
3417         return emit_move_multi_word (mode, x, y);
3418     }
3419
3420   if (GET_MODE_CLASS (mode) == MODE_CC)
3421     return emit_move_ccmode (mode, x, y);
3422
3423   /* Try using a move pattern for the corresponding integer mode.  This is
3424      only safe when simplify_subreg can convert MODE constants into integer
3425      constants.  At present, it can only do this reliably if the value
3426      fits within a HOST_WIDE_INT.  */
3427   if (!CONSTANT_P (y) || GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_WIDE_INT)
3428     {
3429       rtx ret = emit_move_via_integer (mode, x, y, false);
3430       if (ret)
3431         return ret;
3432     }
3433
3434   return emit_move_multi_word (mode, x, y);
3435 }
3436
3437 /* Generate code to copy Y into X.
3438    Both Y and X must have the same mode, except that
3439    Y can be a constant with VOIDmode.
3440    This mode cannot be BLKmode; use emit_block_move for that.
3441
3442    Return the last instruction emitted.  */
3443
3444 rtx
3445 emit_move_insn (rtx x, rtx y)
3446 {
3447   enum machine_mode mode = GET_MODE (x);
3448   rtx y_cst = NULL_RTX;
3449   rtx last_insn, set;
3450
3451   gcc_assert (mode != BLKmode
3452               && (GET_MODE (y) == mode || GET_MODE (y) == VOIDmode));
3453
3454   if (CONSTANT_P (y))
3455     {
3456       if (optimize
3457           && SCALAR_FLOAT_MODE_P (GET_MODE (x))
3458           && (last_insn = compress_float_constant (x, y)))
3459         return last_insn;
3460
3461       y_cst = y;
3462
3463       if (!targetm.legitimate_constant_p (mode, y))
3464         {
3465           y = force_const_mem (mode, y);
3466
3467           /* If the target's cannot_force_const_mem prevented the spill,
3468              assume that the target's move expanders will also take care
3469              of the non-legitimate constant.  */
3470           if (!y)
3471             y = y_cst;
3472           else
3473             y = use_anchored_address (y);
3474         }
3475     }
3476
3477   /* If X or Y are memory references, verify that their addresses are valid
3478      for the machine.  */
3479   if (MEM_P (x)
3480       && (! memory_address_addr_space_p (GET_MODE (x), XEXP (x, 0),
3481                                          MEM_ADDR_SPACE (x))
3482           && ! push_operand (x, GET_MODE (x))))
3483     x = validize_mem (x);
3484
3485   if (MEM_P (y)
3486       && ! memory_address_addr_space_p (GET_MODE (y), XEXP (y, 0),
3487                                         MEM_ADDR_SPACE (y)))
3488     y = validize_mem (y);
3489
3490   gcc_assert (mode != BLKmode);
3491
3492   last_insn = emit_move_insn_1 (x, y);
3493
3494   if (y_cst && REG_P (x)
3495       && (set = single_set (last_insn)) != NULL_RTX
3496       && SET_DEST (set) == x
3497       && ! rtx_equal_p (y_cst, SET_SRC (set)))
3498     set_unique_reg_note (last_insn, REG_EQUAL, copy_rtx (y_cst));
3499
3500   return last_insn;
3501 }
3502
3503 /* If Y is representable exactly in a narrower mode, and the target can
3504    perform the extension directly from constant or memory, then emit the
3505    move as an extension.  */
3506
3507 static rtx
3508 compress_float_constant (rtx x, rtx y)
3509 {
3510   enum machine_mode dstmode = GET_MODE (x);
3511   enum machine_mode orig_srcmode = GET_MODE (y);
3512   enum machine_mode srcmode;
3513   REAL_VALUE_TYPE r;
3514   int oldcost, newcost;
3515   bool speed = optimize_insn_for_speed_p ();
3516
3517   REAL_VALUE_FROM_CONST_DOUBLE (r, y);
3518
3519   if (targetm.legitimate_constant_p (dstmode, y))
3520     oldcost = set_src_cost (y, speed);
3521   else
3522     oldcost = set_src_cost (force_const_mem (dstmode, y), speed);
3523
3524   for (srcmode = GET_CLASS_NARROWEST_MODE (GET_MODE_CLASS (orig_srcmode));
3525        srcmode != orig_srcmode;
3526        srcmode = GET_MODE_WIDER_MODE (srcmode))
3527     {
3528       enum insn_code ic;
3529       rtx trunc_y, last_insn;
3530
3531       /* Skip if the target can't extend this way.  */
3532       ic = can_extend_p (dstmode, srcmode, 0);
3533       if (ic == CODE_FOR_nothing)
3534         continue;
3535
3536       /* Skip if the narrowed value isn't exact.  */
3537       if (! exact_real_truncate (srcmode, &r))
3538         continue;
3539
3540       trunc_y = CONST_DOUBLE_FROM_REAL_VALUE (r, srcmode);
3541
3542       if (targetm.legitimate_constant_p (srcmode, trunc_y))
3543         {
3544           /* Skip if the target needs extra instructions to perform
3545              the extension.  */
3546           if (!insn_operand_matches (ic, 1, trunc_y))
3547             continue;
3548           /* This is valid, but may not be cheaper than the original. */
3549           newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3550                                   speed);
3551           if (oldcost < newcost)
3552             continue;
3553         }
3554       else if (float_extend_from_mem[dstmode][srcmode])
3555         {
3556           trunc_y = force_const_mem (srcmode, trunc_y);
3557           /* This is valid, but may not be cheaper than the original. */
3558           newcost = set_src_cost (gen_rtx_FLOAT_EXTEND (dstmode, trunc_y),
3559                                   speed);
3560           if (oldcost < newcost)
3561             continue;
3562           trunc_y = validize_mem (trunc_y);
3563         }
3564       else
3565         continue;
3566
3567       /* For CSE's benefit, force the compressed constant pool entry
3568          into a new pseudo.  This constant may be used in different modes,
3569          and if not, combine will put things back together for us.  */
3570       trunc_y = force_reg (srcmode, trunc_y);
3571       emit_unop_insn (ic, x, trunc_y, UNKNOWN);
3572       last_insn = get_last_insn ();
3573
3574       if (REG_P (x))
3575         set_unique_reg_note (last_insn, REG_EQUAL, y);
3576
3577       return last_insn;
3578     }
3579
3580   return NULL_RTX;
3581 }
3582 \f
3583 /* Pushing data onto the stack.  */
3584
3585 /* Push a block of length SIZE (perhaps variable)
3586    and return an rtx to address the beginning of the block.
3587    The value may be virtual_outgoing_args_rtx.
3588
3589    EXTRA is the number of bytes of padding to push in addition to SIZE.
3590    BELOW nonzero means this padding comes at low addresses;
3591    otherwise, the padding comes at high addresses.  */
3592
3593 rtx
3594 push_block (rtx size, int extra, int below)
3595 {
3596   rtx temp;
3597
3598   size = convert_modes (Pmode, ptr_mode, size, 1);
3599   if (CONSTANT_P (size))
3600     anti_adjust_stack (plus_constant (Pmode, size, extra));
3601   else if (REG_P (size) && extra == 0)
3602     anti_adjust_stack (size);
3603   else
3604     {
3605       temp = copy_to_mode_reg (Pmode, size);
3606       if (extra != 0)
3607         temp = expand_binop (Pmode, add_optab, temp, GEN_INT (extra),
3608                              temp, 0, OPTAB_LIB_WIDEN);
3609       anti_adjust_stack (temp);
3610     }
3611
3612 #ifndef STACK_GROWS_DOWNWARD
3613   if (0)
3614 #else
3615   if (1)
3616 #endif
3617     {
3618       temp = virtual_outgoing_args_rtx;
3619       if (extra != 0 && below)
3620         temp = plus_constant (Pmode, temp, extra);
3621     }
3622   else
3623     {
3624       if (CONST_INT_P (size))
3625         temp = plus_constant (Pmode, virtual_outgoing_args_rtx,
3626                               -INTVAL (size) - (below ? 0 : extra));
3627       else if (extra != 0 && !below)
3628         temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3629                              negate_rtx (Pmode, plus_constant (Pmode, size,
3630                                                                extra)));
3631       else
3632         temp = gen_rtx_PLUS (Pmode, virtual_outgoing_args_rtx,
3633                              negate_rtx (Pmode, size));
3634     }
3635
3636   return memory_address (GET_CLASS_NARROWEST_MODE (MODE_INT), temp);
3637 }
3638
3639 /* A utility routine that returns the base of an auto-inc memory, or NULL.  */
3640
3641 static rtx
3642 mem_autoinc_base (rtx mem)
3643 {
3644   if (MEM_P (mem))
3645     {
3646       rtx addr = XEXP (mem, 0);
3647       if (GET_RTX_CLASS (GET_CODE (addr)) == RTX_AUTOINC)
3648         return XEXP (addr, 0);
3649     }
3650   return NULL;
3651 }
3652
3653 /* A utility routine used here, in reload, and in try_split.  The insns
3654    after PREV up to and including LAST are known to adjust the stack,
3655    with a final value of END_ARGS_SIZE.  Iterate backward from LAST
3656    placing notes as appropriate.  PREV may be NULL, indicating the
3657    entire insn sequence prior to LAST should be scanned.
3658
3659    The set of allowed stack pointer modifications is small:
3660      (1) One or more auto-inc style memory references (aka pushes),
3661      (2) One or more addition/subtraction with the SP as destination,
3662      (3) A single move insn with the SP as destination,
3663      (4) A call_pop insn,
3664      (5) Noreturn call insns if !ACCUMULATE_OUTGOING_ARGS.
3665
3666    Insns in the sequence that do not modify the SP are ignored,
3667    except for noreturn calls.
3668
3669    The return value is the amount of adjustment that can be trivially
3670    verified, via immediate operand or auto-inc.  If the adjustment
3671    cannot be trivially extracted, the return value is INT_MIN.  */
3672
3673 HOST_WIDE_INT
3674 find_args_size_adjust (rtx insn)
3675 {
3676   rtx dest, set, pat;
3677   int i;
3678
3679   pat = PATTERN (insn);
3680   set = NULL;
3681
3682   /* Look for a call_pop pattern.  */
3683   if (CALL_P (insn))
3684     {
3685       /* We have to allow non-call_pop patterns for the case
3686          of emit_single_push_insn of a TLS address.  */
3687       if (GET_CODE (pat) != PARALLEL)
3688         return 0;
3689
3690       /* All call_pop have a stack pointer adjust in the parallel.
3691          The call itself is always first, and the stack adjust is
3692          usually last, so search from the end.  */
3693       for (i = XVECLEN (pat, 0) - 1; i > 0; --i)
3694         {
3695           set = XVECEXP (pat, 0, i);
3696           if (GET_CODE (set) != SET)
3697             continue;
3698           dest = SET_DEST (set);
3699           if (dest == stack_pointer_rtx)
3700             break;
3701         }
3702       /* We'd better have found the stack pointer adjust.  */
3703       if (i == 0)
3704         return 0;
3705       /* Fall through to process the extracted SET and DEST
3706          as if it was a standalone insn.  */
3707     }
3708   else if (GET_CODE (pat) == SET)
3709     set = pat;
3710   else if ((set = single_set (insn)) != NULL)
3711     ;
3712   else if (GET_CODE (pat) == PARALLEL)
3713     {
3714       /* ??? Some older ports use a parallel with a stack adjust
3715          and a store for a PUSH_ROUNDING pattern, rather than a
3716          PRE/POST_MODIFY rtx.  Don't force them to update yet...  */
3717       /* ??? See h8300 and m68k, pushqi1.  */
3718       for (i = XVECLEN (pat, 0) - 1; i >= 0; --i)
3719         {
3720           set = XVECEXP (pat, 0, i);
3721           if (GET_CODE (set) != SET)
3722             continue;
3723           dest = SET_DEST (set);
3724           if (dest == stack_pointer_rtx)
3725             break;
3726
3727           /* We do not expect an auto-inc of the sp in the parallel.  */
3728           gcc_checking_assert (mem_autoinc_base (dest) != stack_pointer_rtx);
3729           gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3730                                != stack_pointer_rtx);
3731         }
3732       if (i < 0)
3733         return 0;
3734     }
3735   else
3736     return 0;
3737
3738   dest = SET_DEST (set);
3739
3740   /* Look for direct modifications of the stack pointer.  */
3741   if (REG_P (dest) && REGNO (dest) == STACK_POINTER_REGNUM)
3742     {
3743       /* Look for a trivial adjustment, otherwise assume nothing.  */
3744       /* Note that the SPU restore_stack_block pattern refers to
3745          the stack pointer in V4SImode.  Consider that non-trivial.  */
3746       if (SCALAR_INT_MODE_P (GET_MODE (dest))
3747           && GET_CODE (SET_SRC (set)) == PLUS
3748           && XEXP (SET_SRC (set), 0) == stack_pointer_rtx
3749           && CONST_INT_P (XEXP (SET_SRC (set), 1)))
3750         return INTVAL (XEXP (SET_SRC (set), 1));
3751       /* ??? Reload can generate no-op moves, which will be cleaned
3752          up later.  Recognize it and continue searching.  */
3753       else if (rtx_equal_p (dest, SET_SRC (set)))
3754         return 0;
3755       else
3756         return HOST_WIDE_INT_MIN;
3757     }
3758   else
3759     {
3760       rtx mem, addr;
3761
3762       /* Otherwise only think about autoinc patterns.  */
3763       if (mem_autoinc_base (dest) == stack_pointer_rtx)
3764         {
3765           mem = dest;
3766           gcc_checking_assert (mem_autoinc_base (SET_SRC (set))
3767                                != stack_pointer_rtx);
3768         }
3769       else if (mem_autoinc_base (SET_SRC (set)) == stack_pointer_rtx)
3770         mem = SET_SRC (set);
3771       else
3772         return 0;
3773
3774       addr = XEXP (mem, 0);
3775       switch (GET_CODE (addr))
3776         {
3777         case PRE_INC:
3778         case POST_INC:
3779           return GET_MODE_SIZE (GET_MODE (mem));
3780         case PRE_DEC:
3781         case POST_DEC:
3782           return -GET_MODE_SIZE (GET_MODE (mem));
3783         case PRE_MODIFY:
3784         case POST_MODIFY:
3785           addr = XEXP (addr, 1);
3786           gcc_assert (GET_CODE (addr) == PLUS);
3787           gcc_assert (XEXP (addr, 0) == stack_pointer_rtx);
3788           gcc_assert (CONST_INT_P (XEXP (addr, 1)));
3789           return INTVAL (XEXP (addr, 1));
3790         default:
3791           gcc_unreachable ();
3792         }
3793     }
3794 }
3795
3796 int
3797 fixup_args_size_notes (rtx prev, rtx last, int end_args_size)
3798 {
3799   int args_size = end_args_size;
3800   bool saw_unknown = false;
3801   rtx insn;
3802
3803   for (insn = last; insn != prev; insn = PREV_INSN (insn))
3804     {
3805       HOST_WIDE_INT this_delta;
3806
3807       if (!NONDEBUG_INSN_P (insn))
3808         continue;
3809
3810       this_delta = find_args_size_adjust (insn);
3811       if (this_delta == 0)
3812         {
3813           if (!CALL_P (insn)
3814               || ACCUMULATE_OUTGOING_ARGS
3815               || find_reg_note (insn, REG_NORETURN, NULL_RTX) == NULL_RTX)
3816             continue;
3817         }
3818
3819       gcc_assert (!saw_unknown);
3820       if (this_delta == HOST_WIDE_INT_MIN)
3821         saw_unknown = true;
3822
3823       add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (args_size));
3824 #ifdef STACK_GROWS_DOWNWARD
3825       this_delta = -this_delta;
3826 #endif
3827       args_size -= this_delta;
3828     }
3829
3830   return saw_unknown ? INT_MIN : args_size;
3831 }
3832
3833 #ifdef PUSH_ROUNDING
3834 /* Emit single push insn.  */
3835
3836 static void
3837 emit_single_push_insn_1 (enum machine_mode mode, rtx x, tree type)
3838 {
3839   rtx dest_addr;
3840   unsigned rounded_size = PUSH_ROUNDING (GET_MODE_SIZE (mode));
3841   rtx dest;
3842   enum insn_code icode;
3843
3844   stack_pointer_delta += PUSH_ROUNDING (GET_MODE_SIZE (mode));
3845   /* If there is push pattern, use it.  Otherwise try old way of throwing
3846      MEM representing push operation to move expander.  */
3847   icode = optab_handler (push_optab, mode);
3848   if (icode != CODE_FOR_nothing)
3849     {
3850       struct expand_operand ops[1];
3851
3852       create_input_operand (&ops[0], x, mode);
3853       if (maybe_expand_insn (icode, 1, ops))
3854         return;
3855     }
3856   if (GET_MODE_SIZE (mode) == rounded_size)
3857     dest_addr = gen_rtx_fmt_e (STACK_PUSH_CODE, Pmode, stack_pointer_rtx);
3858   /* If we are to pad downward, adjust the stack pointer first and
3859      then store X into the stack location using an offset.  This is
3860      because emit_move_insn does not know how to pad; it does not have
3861      access to type.  */
3862   else if (FUNCTION_ARG_PADDING (mode, type) == downward)
3863     {
3864       unsigned padding_size = rounded_size - GET_MODE_SIZE (mode);
3865       HOST_WIDE_INT offset;
3866
3867       emit_move_insn (stack_pointer_rtx,
3868                       expand_binop (Pmode,
3869 #ifdef STACK_GROWS_DOWNWARD
3870                                     sub_optab,
3871 #else
3872                                     add_optab,
3873 #endif
3874                                     stack_pointer_rtx,
3875                                     GEN_INT (rounded_size),
3876                                     NULL_RTX, 0, OPTAB_LIB_WIDEN));
3877
3878       offset = (HOST_WIDE_INT) padding_size;
3879 #ifdef STACK_GROWS_DOWNWARD
3880       if (STACK_PUSH_CODE == POST_DEC)
3881         /* We have already decremented the stack pointer, so get the
3882            previous value.  */
3883         offset += (HOST_WIDE_INT) rounded_size;
3884 #else
3885       if (STACK_PUSH_CODE == POST_INC)
3886         /* We have already incremented the stack pointer, so get the
3887            previous value.  */
3888         offset -= (HOST_WIDE_INT) rounded_size;
3889 #endif
3890       dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx, GEN_INT (offset));
3891     }
3892   else
3893     {
3894 #ifdef STACK_GROWS_DOWNWARD
3895       /* ??? This seems wrong if STACK_PUSH_CODE == POST_DEC.  */
3896       dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3897                                 GEN_INT (-(HOST_WIDE_INT) rounded_size));
3898 #else
3899       /* ??? This seems wrong if STACK_PUSH_CODE == POST_INC.  */
3900       dest_addr = gen_rtx_PLUS (Pmode, stack_pointer_rtx,
3901                                 GEN_INT (rounded_size));
3902 #endif
3903       dest_addr = gen_rtx_PRE_MODIFY (Pmode, stack_pointer_rtx, dest_addr);
3904     }
3905
3906   dest = gen_rtx_MEM (mode, dest_addr);
3907
3908   if (type != 0)
3909     {
3910       set_mem_attributes (dest, type, 1);
3911
3912       if (flag_optimize_sibling_calls)
3913         /* Function incoming arguments may overlap with sibling call
3914            outgoing arguments and we cannot allow reordering of reads
3915            from function arguments with stores to outgoing arguments
3916            of sibling calls.  */
3917         set_mem_alias_set (dest, 0);
3918     }
3919   emit_move_insn (dest, x);
3920 }
3921
3922 /* Emit and annotate a single push insn.  */
3923
3924 static void
3925 emit_single_push_insn (enum machine_mode mode, rtx x, tree type)
3926 {
3927   int delta, old_delta = stack_pointer_delta;
3928   rtx prev = get_last_insn ();
3929   rtx last;
3930
3931   emit_single_push_insn_1 (mode, x, type);
3932
3933   last = get_last_insn ();
3934
3935   /* Notice the common case where we emitted exactly one insn.  */
3936   if (PREV_INSN (last) == prev)
3937     {
3938       add_reg_note (last, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
3939       return;
3940     }
3941
3942   delta = fixup_args_size_notes (prev, last, stack_pointer_delta);
3943   gcc_assert (delta == INT_MIN || delta == old_delta);
3944 }
3945 #endif
3946
3947 /* Generate code to push X onto the stack, assuming it has mode MODE and
3948    type TYPE.
3949    MODE is redundant except when X is a CONST_INT (since they don't
3950    carry mode info).
3951    SIZE is an rtx for the size of data to be copied (in bytes),
3952    needed only if X is BLKmode.
3953
3954    ALIGN (in bits) is maximum alignment we can assume.
3955
3956    If PARTIAL and REG are both nonzero, then copy that many of the first
3957    bytes of X into registers starting with REG, and push the rest of X.
3958    The amount of space pushed is decreased by PARTIAL bytes.
3959    REG must be a hard register in this case.
3960    If REG is zero but PARTIAL is not, take any all others actions for an
3961    argument partially in registers, but do not actually load any
3962    registers.
3963
3964    EXTRA is the amount in bytes of extra space to leave next to this arg.
3965    This is ignored if an argument block has already been allocated.
3966
3967    On a machine that lacks real push insns, ARGS_ADDR is the address of
3968    the bottom of the argument block for this call.  We use indexing off there
3969    to store the arg.  On machines with push insns, ARGS_ADDR is 0 when a
3970    argument block has not been preallocated.
3971
3972    ARGS_SO_FAR is the size of args previously pushed for this call.
3973
3974    REG_PARM_STACK_SPACE is nonzero if functions require stack space
3975    for arguments passed in registers.  If nonzero, it will be the number
3976    of bytes required.  */
3977
3978 void
3979 emit_push_insn (rtx x, enum machine_mode mode, tree type, rtx size,
3980                 unsigned int align, int partial, rtx reg, int extra,
3981                 rtx args_addr, rtx args_so_far, int reg_parm_stack_space,
3982                 rtx alignment_pad)
3983 {
3984   rtx xinner;
3985   enum direction stack_direction
3986 #ifdef STACK_GROWS_DOWNWARD
3987     = downward;
3988 #else
3989     = upward;
3990 #endif
3991
3992   /* Decide where to pad the argument: `downward' for below,
3993      `upward' for above, or `none' for don't pad it.
3994      Default is below for small data on big-endian machines; else above.  */
3995   enum direction where_pad = FUNCTION_ARG_PADDING (mode, type);
3996
3997   /* Invert direction if stack is post-decrement.
3998      FIXME: why?  */
3999   if (STACK_PUSH_CODE == POST_DEC)
4000     if (where_pad != none)
4001       where_pad = (where_pad == downward ? upward : downward);
4002
4003   xinner = x;
4004
4005   if (mode == BLKmode
4006       || (STRICT_ALIGNMENT && align < GET_MODE_ALIGNMENT (mode)))
4007     {
4008       /* Copy a block into the stack, entirely or partially.  */
4009
4010       rtx temp;
4011       int used;
4012       int offset;
4013       int skip;
4014
4015       offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4016       used = partial - offset;
4017
4018       if (mode != BLKmode)
4019         {
4020           /* A value is to be stored in an insufficiently aligned
4021              stack slot; copy via a suitably aligned slot if
4022              necessary.  */
4023           size = GEN_INT (GET_MODE_SIZE (mode));
4024           if (!MEM_P (xinner))
4025             {
4026               temp = assign_temp (type, 0, 1, 1);
4027               emit_move_insn (temp, xinner);
4028               xinner = temp;
4029             }
4030         }
4031
4032       gcc_assert (size);
4033
4034       /* USED is now the # of bytes we need not copy to the stack
4035          because registers will take care of them.  */
4036
4037       if (partial != 0)
4038         xinner = adjust_address (xinner, BLKmode, used);
4039
4040       /* If the partial register-part of the arg counts in its stack size,
4041          skip the part of stack space corresponding to the registers.
4042          Otherwise, start copying to the beginning of the stack space,
4043          by setting SKIP to 0.  */
4044       skip = (reg_parm_stack_space == 0) ? 0 : used;
4045
4046 #ifdef PUSH_ROUNDING
4047       /* Do it with several push insns if that doesn't take lots of insns
4048          and if there is no difficulty with push insns that skip bytes
4049          on the stack for alignment purposes.  */
4050       if (args_addr == 0
4051           && PUSH_ARGS
4052           && CONST_INT_P (size)
4053           && skip == 0
4054           && MEM_ALIGN (xinner) >= align
4055           && (MOVE_BY_PIECES_P ((unsigned) INTVAL (size) - used, align))
4056           /* Here we avoid the case of a structure whose weak alignment
4057              forces many pushes of a small amount of data,
4058              and such small pushes do rounding that causes trouble.  */
4059           && ((! SLOW_UNALIGNED_ACCESS (word_mode, align))
4060               || align >= BIGGEST_ALIGNMENT
4061               || (PUSH_ROUNDING (align / BITS_PER_UNIT)
4062                   == (align / BITS_PER_UNIT)))
4063           && (HOST_WIDE_INT) PUSH_ROUNDING (INTVAL (size)) == INTVAL (size))
4064         {
4065           /* Push padding now if padding above and stack grows down,
4066              or if padding below and stack grows up.
4067              But if space already allocated, this has already been done.  */
4068           if (extra && args_addr == 0
4069               && where_pad != none && where_pad != stack_direction)
4070             anti_adjust_stack (GEN_INT (extra));
4071
4072           move_by_pieces (NULL, xinner, INTVAL (size) - used, align, 0);
4073         }
4074       else
4075 #endif /* PUSH_ROUNDING  */
4076         {
4077           rtx target;
4078
4079           /* Otherwise make space on the stack and copy the data
4080              to the address of that space.  */
4081
4082           /* Deduct words put into registers from the size we must copy.  */
4083           if (partial != 0)
4084             {
4085               if (CONST_INT_P (size))
4086                 size = GEN_INT (INTVAL (size) - used);
4087               else
4088                 size = expand_binop (GET_MODE (size), sub_optab, size,
4089                                      GEN_INT (used), NULL_RTX, 0,
4090                                      OPTAB_LIB_WIDEN);
4091             }
4092
4093           /* Get the address of the stack space.
4094              In this case, we do not deal with EXTRA separately.
4095              A single stack adjust will do.  */
4096           if (! args_addr)
4097             {
4098               temp = push_block (size, extra, where_pad == downward);
4099               extra = 0;
4100             }
4101           else if (CONST_INT_P (args_so_far))
4102             temp = memory_address (BLKmode,
4103                                    plus_constant (Pmode, args_addr,
4104                                                   skip + INTVAL (args_so_far)));
4105           else
4106             temp = memory_address (BLKmode,
4107                                    plus_constant (Pmode,
4108                                                   gen_rtx_PLUS (Pmode,
4109                                                                 args_addr,
4110                                                                 args_so_far),
4111                                                   skip));
4112
4113           if (!ACCUMULATE_OUTGOING_ARGS)
4114             {
4115               /* If the source is referenced relative to the stack pointer,
4116                  copy it to another register to stabilize it.  We do not need
4117                  to do this if we know that we won't be changing sp.  */
4118
4119               if (reg_mentioned_p (virtual_stack_dynamic_rtx, temp)
4120                   || reg_mentioned_p (virtual_outgoing_args_rtx, temp))
4121                 temp = copy_to_reg (temp);
4122             }
4123
4124           target = gen_rtx_MEM (BLKmode, temp);
4125
4126           /* We do *not* set_mem_attributes here, because incoming arguments
4127              may overlap with sibling call outgoing arguments and we cannot
4128              allow reordering of reads from function arguments with stores
4129              to outgoing arguments of sibling calls.  We do, however, want
4130              to record the alignment of the stack slot.  */
4131           /* ALIGN may well be better aligned than TYPE, e.g. due to
4132              PARM_BOUNDARY.  Assume the caller isn't lying.  */
4133           set_mem_align (target, align);
4134
4135           emit_block_move (target, xinner, size, BLOCK_OP_CALL_PARM);
4136         }
4137     }
4138   else if (partial > 0)
4139     {
4140       /* Scalar partly in registers.  */
4141
4142       int size = GET_MODE_SIZE (mode) / UNITS_PER_WORD;
4143       int i;
4144       int not_stack;
4145       /* # bytes of start of argument
4146          that we must make space for but need not store.  */
4147       int offset = partial % (PARM_BOUNDARY / BITS_PER_UNIT);
4148       int args_offset = INTVAL (args_so_far);
4149       int skip;
4150
4151       /* Push padding now if padding above and stack grows down,
4152          or if padding below and stack grows up.
4153          But if space already allocated, this has already been done.  */
4154       if (extra && args_addr == 0
4155           && where_pad != none && where_pad != stack_direction)
4156         anti_adjust_stack (GEN_INT (extra));
4157
4158       /* If we make space by pushing it, we might as well push
4159          the real data.  Otherwise, we can leave OFFSET nonzero
4160          and leave the space uninitialized.  */
4161       if (args_addr == 0)
4162         offset = 0;
4163
4164       /* Now NOT_STACK gets the number of words that we don't need to
4165          allocate on the stack.  Convert OFFSET to words too.  */
4166       not_stack = (partial - offset) / UNITS_PER_WORD;
4167       offset /= UNITS_PER_WORD;
4168
4169       /* If the partial register-part of the arg counts in its stack size,
4170          skip the part of stack space corresponding to the registers.
4171          Otherwise, start copying to the beginning of the stack space,
4172          by setting SKIP to 0.  */
4173       skip = (reg_parm_stack_space == 0) ? 0 : not_stack;
4174
4175       if (CONSTANT_P (x) && !targetm.legitimate_constant_p (mode, x))
4176         x = validize_mem (force_const_mem (mode, x));
4177
4178       /* If X is a hard register in a non-integer mode, copy it into a pseudo;
4179          SUBREGs of such registers are not allowed.  */
4180       if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER
4181            && GET_MODE_CLASS (GET_MODE (x)) != MODE_INT))
4182         x = copy_to_reg (x);
4183
4184       /* Loop over all the words allocated on the stack for this arg.  */
4185       /* We can do it by words, because any scalar bigger than a word
4186          has a size a multiple of a word.  */
4187 #ifndef PUSH_ARGS_REVERSED
4188       for (i = not_stack; i < size; i++)
4189 #else
4190       for (i = size - 1; i >= not_stack; i--)
4191 #endif
4192         if (i >= not_stack + offset)
4193           emit_push_insn (operand_subword_force (x, i, mode),
4194                           word_mode, NULL_TREE, NULL_RTX, align, 0, NULL_RTX,
4195                           0, args_addr,
4196                           GEN_INT (args_offset + ((i - not_stack + skip)
4197                                                   * UNITS_PER_WORD)),
4198                           reg_parm_stack_space, alignment_pad);
4199     }
4200   else
4201     {
4202       rtx addr;
4203       rtx dest;
4204
4205       /* Push padding now if padding above and stack grows down,
4206          or if padding below and stack grows up.
4207          But if space already allocated, this has already been done.  */
4208       if (extra && args_addr == 0
4209           && where_pad != none && where_pad != stack_direction)
4210         anti_adjust_stack (GEN_INT (extra));
4211
4212 #ifdef PUSH_ROUNDING
4213       if (args_addr == 0 && PUSH_ARGS)
4214         emit_single_push_insn (mode, x, type);
4215       else
4216 #endif
4217         {
4218           if (CONST_INT_P (args_so_far))
4219             addr
4220               = memory_address (mode,
4221                                 plus_constant (Pmode, args_addr,
4222                                                INTVAL (args_so_far)));
4223           else
4224             addr = memory_address (mode, gen_rtx_PLUS (Pmode, args_addr,
4225                                                        args_so_far));
4226           dest = gen_rtx_MEM (mode, addr);
4227
4228           /* We do *not* set_mem_attributes here, because incoming arguments
4229              may overlap with sibling call outgoing arguments and we cannot
4230              allow reordering of reads from function arguments with stores
4231              to outgoing arguments of sibling calls.  We do, however, want
4232              to record the alignment of the stack slot.  */
4233           /* ALIGN may well be better aligned than TYPE, e.g. due to
4234              PARM_BOUNDARY.  Assume the caller isn't lying.  */
4235           set_mem_align (dest, align);
4236
4237           emit_move_insn (dest, x);
4238         }
4239     }
4240
4241   /* If part should go in registers, copy that part
4242      into the appropriate registers.  Do this now, at the end,
4243      since mem-to-mem copies above may do function calls.  */
4244   if (partial > 0 && reg != 0)
4245     {
4246       /* Handle calls that pass values in multiple non-contiguous locations.
4247          The Irix 6 ABI has examples of this.  */
4248       if (GET_CODE (reg) == PARALLEL)
4249         emit_group_load (reg, x, type, -1);
4250       else
4251         {
4252           gcc_assert (partial % UNITS_PER_WORD == 0);
4253           move_block_to_reg (REGNO (reg), x, partial / UNITS_PER_WORD, mode);
4254         }
4255     }
4256
4257   if (extra && args_addr == 0 && where_pad == stack_direction)
4258     anti_adjust_stack (GEN_INT (extra));
4259
4260   if (alignment_pad && args_addr == 0)
4261     anti_adjust_stack (alignment_pad);
4262 }
4263 \f
4264 /* Return X if X can be used as a subtarget in a sequence of arithmetic
4265    operations.  */
4266
4267 static rtx
4268 get_subtarget (rtx x)
4269 {
4270   return (optimize
4271           || x == 0
4272            /* Only registers can be subtargets.  */
4273            || !REG_P (x)
4274            /* Don't use hard regs to avoid extending their life.  */
4275            || REGNO (x) < FIRST_PSEUDO_REGISTER
4276           ? 0 : x);
4277 }
4278
4279 /* A subroutine of expand_assignment.  Optimize FIELD op= VAL, where
4280    FIELD is a bitfield.  Returns true if the optimization was successful,
4281    and there's nothing else to do.  */
4282
4283 static bool
4284 optimize_bitfield_assignment_op (unsigned HOST_WIDE_INT bitsize,
4285                                  unsigned HOST_WIDE_INT bitpos,
4286                                  unsigned HOST_WIDE_INT bitregion_start,
4287                                  unsigned HOST_WIDE_INT bitregion_end,
4288                                  enum machine_mode mode1, rtx str_rtx,
4289                                  tree to, tree src)
4290 {
4291   enum machine_mode str_mode = GET_MODE (str_rtx);
4292   unsigned int str_bitsize = GET_MODE_BITSIZE (str_mode);
4293   tree op0, op1;
4294   rtx value, result;
4295   optab binop;
4296   gimple srcstmt;
4297   enum tree_code code;
4298
4299   if (mode1 != VOIDmode
4300       || bitsize >= BITS_PER_WORD
4301       || str_bitsize > BITS_PER_WORD
4302       || TREE_SIDE_EFFECTS (to)
4303       || TREE_THIS_VOLATILE (to))
4304     return false;
4305
4306   STRIP_NOPS (src);
4307   if (TREE_CODE (src) != SSA_NAME)
4308     return false;
4309   if (TREE_CODE (TREE_TYPE (src)) != INTEGER_TYPE)
4310     return false;
4311
4312   srcstmt = get_gimple_for_ssa_name (src);
4313   if (!srcstmt
4314       || TREE_CODE_CLASS (gimple_assign_rhs_code (srcstmt)) != tcc_binary)
4315     return false;
4316
4317   code = gimple_assign_rhs_code (srcstmt);
4318
4319   op0 = gimple_assign_rhs1 (srcstmt);
4320
4321   /* If OP0 is an SSA_NAME, then we want to walk the use-def chain
4322      to find its initialization.  Hopefully the initialization will
4323      be from a bitfield load.  */
4324   if (TREE_CODE (op0) == SSA_NAME)
4325     {
4326       gimple op0stmt = get_gimple_for_ssa_name (op0);
4327
4328       /* We want to eventually have OP0 be the same as TO, which
4329          should be a bitfield.  */
4330       if (!op0stmt
4331           || !is_gimple_assign (op0stmt)
4332           || gimple_assign_rhs_code (op0stmt) != TREE_CODE (to))
4333         return false;
4334       op0 = gimple_assign_rhs1 (op0stmt);
4335     }
4336
4337   op1 = gimple_assign_rhs2 (srcstmt);
4338
4339   if (!operand_equal_p (to, op0, 0))
4340     return false;
4341
4342   if (MEM_P (str_rtx))
4343     {
4344       unsigned HOST_WIDE_INT offset1;
4345
4346       if (str_bitsize == 0 || str_bitsize > BITS_PER_WORD)
4347         str_mode = word_mode;
4348       str_mode = get_best_mode (bitsize, bitpos,
4349                                 bitregion_start, bitregion_end,
4350                                 MEM_ALIGN (str_rtx), str_mode, 0);
4351       if (str_mode == VOIDmode)
4352         return false;
4353       str_bitsize = GET_MODE_BITSIZE (str_mode);
4354
4355       offset1 = bitpos;
4356       bitpos %= str_bitsize;
4357       offset1 = (offset1 - bitpos) / BITS_PER_UNIT;
4358       str_rtx = adjust_address (str_rtx, str_mode, offset1);
4359     }
4360   else if (!REG_P (str_rtx) && GET_CODE (str_rtx) != SUBREG)
4361     return false;
4362
4363   /* If the bit field covers the whole REG/MEM, store_field
4364      will likely generate better code.  */
4365   if (bitsize >= str_bitsize)
4366     return false;
4367
4368   /* We can't handle fields split across multiple entities.  */
4369   if (bitpos + bitsize > str_bitsize)
4370     return false;
4371
4372   if (BYTES_BIG_ENDIAN)
4373     bitpos = str_bitsize - bitpos - bitsize;
4374
4375   switch (code)
4376     {
4377     case PLUS_EXPR:
4378     case MINUS_EXPR:
4379       /* For now, just optimize the case of the topmost bitfield
4380          where we don't need to do any masking and also
4381          1 bit bitfields where xor can be used.
4382          We might win by one instruction for the other bitfields
4383          too if insv/extv instructions aren't used, so that
4384          can be added later.  */
4385       if (bitpos + bitsize != str_bitsize
4386           && (bitsize != 1 || TREE_CODE (op1) != INTEGER_CST))
4387         break;
4388
4389       value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4390       value = convert_modes (str_mode,
4391                              TYPE_MODE (TREE_TYPE (op1)), value,
4392                              TYPE_UNSIGNED (TREE_TYPE (op1)));
4393
4394       /* We may be accessing data outside the field, which means
4395          we can alias adjacent data.  */
4396       if (MEM_P (str_rtx))
4397         {
4398           str_rtx = shallow_copy_rtx (str_rtx);
4399           set_mem_alias_set (str_rtx, 0);
4400           set_mem_expr (str_rtx, 0);
4401         }
4402
4403       binop = code == PLUS_EXPR ? add_optab : sub_optab;
4404       if (bitsize == 1 && bitpos + bitsize != str_bitsize)
4405         {
4406           value = expand_and (str_mode, value, const1_rtx, NULL);
4407           binop = xor_optab;
4408         }
4409       value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4410       result = expand_binop (str_mode, binop, str_rtx,
4411                              value, str_rtx, 1, OPTAB_WIDEN);
4412       if (result != str_rtx)
4413         emit_move_insn (str_rtx, result);
4414       return true;
4415
4416     case BIT_IOR_EXPR:
4417     case BIT_XOR_EXPR:
4418       if (TREE_CODE (op1) != INTEGER_CST)
4419         break;
4420       value = expand_expr (op1, NULL_RTX, str_mode, EXPAND_NORMAL);
4421       value = convert_modes (str_mode,
4422                              TYPE_MODE (TREE_TYPE (op1)), value,
4423                              TYPE_UNSIGNED (TREE_TYPE (op1)));
4424
4425       /* We may be accessing data outside the field, which means
4426          we can alias adjacent data.  */
4427       if (MEM_P (str_rtx))
4428         {
4429           str_rtx = shallow_copy_rtx (str_rtx);
4430           set_mem_alias_set (str_rtx, 0);
4431           set_mem_expr (str_rtx, 0);
4432         }
4433
4434       binop = code == BIT_IOR_EXPR ? ior_optab : xor_optab;
4435       if (bitpos + bitsize != str_bitsize)
4436         {
4437           rtx mask = GEN_INT (((unsigned HOST_WIDE_INT) 1 << bitsize) - 1);
4438           value = expand_and (str_mode, value, mask, NULL_RTX);
4439         }
4440       value = expand_shift (LSHIFT_EXPR, str_mode, value, bitpos, NULL_RTX, 1);
4441       result = expand_binop (str_mode, binop, str_rtx,
4442                              value, str_rtx, 1, OPTAB_WIDEN);
4443       if (result != str_rtx)
4444         emit_move_insn (str_rtx, result);
4445       return true;
4446
4447     default:
4448       break;
4449     }
4450
4451   return false;
4452 }
4453
4454 /* In the C++ memory model, consecutive bit fields in a structure are
4455    considered one memory location.
4456
4457    Given a COMPONENT_REF EXP at position (BITPOS, OFFSET), this function
4458    returns the bit range of consecutive bits in which this COMPONENT_REF
4459    belongs.  The values are returned in *BITSTART and *BITEND.  *BITPOS
4460    and *OFFSET may be adjusted in the process.
4461
4462    If the access does not need to be restricted, 0 is returned in both
4463    *BITSTART and *BITEND.  */
4464
4465 static void
4466 get_bit_range (unsigned HOST_WIDE_INT *bitstart,
4467                unsigned HOST_WIDE_INT *bitend,
4468                tree exp,
4469                HOST_WIDE_INT *bitpos,
4470                tree *offset)
4471 {
4472   HOST_WIDE_INT bitoffset;
4473   tree field, repr;
4474
4475   gcc_assert (TREE_CODE (exp) == COMPONENT_REF);
4476
4477   field = TREE_OPERAND (exp, 1);
4478   repr = DECL_BIT_FIELD_REPRESENTATIVE (field);
4479   /* If we do not have a DECL_BIT_FIELD_REPRESENTATIVE there is no
4480      need to limit the range we can access.  */
4481   if (!repr)
4482     {
4483       *bitstart = *bitend = 0;
4484       return;
4485     }
4486
4487   /* If we have a DECL_BIT_FIELD_REPRESENTATIVE but the enclosing record is
4488      part of a larger bit field, then the representative does not serve any
4489      useful purpose.  This can occur in Ada.  */
4490   if (handled_component_p (TREE_OPERAND (exp, 0)))
4491     {
4492       enum machine_mode rmode;
4493       HOST_WIDE_INT rbitsize, rbitpos;
4494       tree roffset;
4495       int unsignedp;
4496       int volatilep = 0;
4497       get_inner_reference (TREE_OPERAND (exp, 0), &rbitsize, &rbitpos,
4498                            &roffset, &rmode, &unsignedp, &volatilep, false);
4499       if ((rbitpos % BITS_PER_UNIT) != 0)
4500         {
4501           *bitstart = *bitend = 0;
4502           return;
4503         }
4504     }
4505
4506   /* Compute the adjustment to bitpos from the offset of the field
4507      relative to the representative.  DECL_FIELD_OFFSET of field and
4508      repr are the same by construction if they are not constants,
4509      see finish_bitfield_layout.  */
4510   if (host_integerp (DECL_FIELD_OFFSET (field), 1)
4511       && host_integerp (DECL_FIELD_OFFSET (repr), 1))
4512     bitoffset = (tree_low_cst (DECL_FIELD_OFFSET (field), 1)
4513                  - tree_low_cst (DECL_FIELD_OFFSET (repr), 1)) * BITS_PER_UNIT;
4514   else
4515     bitoffset = 0;
4516   bitoffset += (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
4517                 - tree_low_cst (DECL_FIELD_BIT_OFFSET (repr), 1));
4518
4519   /* If the adjustment is larger than bitpos, we would have a negative bit
4520      position for the lower bound and this may wreak havoc later.  This can
4521      occur only if we have a non-null offset, so adjust offset and bitpos
4522      to make the lower bound non-negative.  */
4523   if (bitoffset > *bitpos)
4524     {
4525       HOST_WIDE_INT adjust = bitoffset - *bitpos;
4526
4527       gcc_assert ((adjust % BITS_PER_UNIT) == 0);
4528       gcc_assert (*offset != NULL_TREE);
4529
4530       *bitpos += adjust;
4531       *offset
4532         = size_binop (MINUS_EXPR, *offset, size_int (adjust / BITS_PER_UNIT));
4533       *bitstart = 0;
4534     }
4535   else
4536     *bitstart = *bitpos - bitoffset;
4537
4538   *bitend = *bitstart + tree_low_cst (DECL_SIZE (repr), 1) - 1;
4539 }
4540
4541 /* Returns true if the MEM_REF REF refers to an object that does not
4542    reside in memory and has non-BLKmode.  */
4543
4544 static bool
4545 mem_ref_refers_to_non_mem_p (tree ref)
4546 {
4547   tree base = TREE_OPERAND (ref, 0);
4548   if (TREE_CODE (base) != ADDR_EXPR)
4549     return false;
4550   base = TREE_OPERAND (base, 0);
4551   return (DECL_P (base)
4552           && !TREE_ADDRESSABLE (base)
4553           && DECL_MODE (base) != BLKmode
4554           && DECL_RTL_SET_P (base)
4555           && !MEM_P (DECL_RTL (base)));
4556 }
4557
4558 /* Expand an assignment that stores the value of FROM into TO.  If NONTEMPORAL
4559    is true, try generating a nontemporal store.  */
4560
4561 void
4562 expand_assignment (tree to, tree from, bool nontemporal)
4563 {
4564   rtx to_rtx = 0;
4565   rtx result;
4566   enum machine_mode mode;
4567   unsigned int align;
4568   enum insn_code icode;
4569
4570   /* Don't crash if the lhs of the assignment was erroneous.  */
4571   if (TREE_CODE (to) == ERROR_MARK)
4572     {
4573       expand_normal (from);
4574       return;
4575     }
4576
4577   /* Optimize away no-op moves without side-effects.  */
4578   if (operand_equal_p (to, from, 0))
4579     return;
4580
4581   /* Handle misaligned stores.  */
4582   mode = TYPE_MODE (TREE_TYPE (to));
4583   if ((TREE_CODE (to) == MEM_REF
4584        || TREE_CODE (to) == TARGET_MEM_REF)
4585       && mode != BLKmode
4586       && !mem_ref_refers_to_non_mem_p (to)
4587       && ((align = get_object_or_type_alignment (to))
4588           < GET_MODE_ALIGNMENT (mode))
4589       && (((icode = optab_handler (movmisalign_optab, mode))
4590            != CODE_FOR_nothing)
4591           || SLOW_UNALIGNED_ACCESS (mode, align)))
4592     {
4593       rtx reg, mem;
4594
4595       reg = expand_expr (from, NULL_RTX, VOIDmode, EXPAND_NORMAL);
4596       reg = force_not_mem (reg);
4597       mem = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4598
4599       if (icode != CODE_FOR_nothing)
4600         {
4601           struct expand_operand ops[2];
4602
4603           create_fixed_operand (&ops[0], mem);
4604           create_input_operand (&ops[1], reg, mode);
4605           /* The movmisalign<mode> pattern cannot fail, else the assignment
4606              would silently be omitted.  */
4607           expand_insn (icode, 2, ops);
4608         }
4609       else
4610         store_bit_field (mem, GET_MODE_BITSIZE (mode),
4611                          0, 0, 0, mode, reg);
4612       return;
4613     }
4614
4615   /* Assignment of a structure component needs special treatment
4616      if the structure component's rtx is not simply a MEM.
4617      Assignment of an array element at a constant index, and assignment of
4618      an array element in an unaligned packed structure field, has the same
4619      problem.  Same for (partially) storing into a non-memory object.  */
4620   if (handled_component_p (to)
4621       || (TREE_CODE (to) == MEM_REF
4622           && mem_ref_refers_to_non_mem_p (to))
4623       || TREE_CODE (TREE_TYPE (to)) == ARRAY_TYPE)
4624     {
4625       enum machine_mode mode1;
4626       HOST_WIDE_INT bitsize, bitpos;
4627       unsigned HOST_WIDE_INT bitregion_start = 0;
4628       unsigned HOST_WIDE_INT bitregion_end = 0;
4629       tree offset;
4630       int unsignedp;
4631       int volatilep = 0;
4632       tree tem;
4633       bool misalignp;
4634       rtx mem = NULL_RTX;
4635
4636       push_temp_slots ();
4637       tem = get_inner_reference (to, &bitsize, &bitpos, &offset, &mode1,
4638                                  &unsignedp, &volatilep, true);
4639
4640       if (TREE_CODE (to) == COMPONENT_REF
4641           && DECL_BIT_FIELD_TYPE (TREE_OPERAND (to, 1)))
4642         get_bit_range (&bitregion_start, &bitregion_end, to, &bitpos, &offset);
4643
4644       /* If we are going to use store_bit_field and extract_bit_field,
4645          make sure to_rtx will be safe for multiple use.  */
4646       mode = TYPE_MODE (TREE_TYPE (tem));
4647       if (TREE_CODE (tem) == MEM_REF
4648           && mode != BLKmode
4649           && ((align = get_object_or_type_alignment (tem))
4650               < GET_MODE_ALIGNMENT (mode))
4651           && ((icode = optab_handler (movmisalign_optab, mode))
4652               != CODE_FOR_nothing))
4653         {
4654           struct expand_operand ops[2];
4655
4656           misalignp = true;
4657           to_rtx = gen_reg_rtx (mode);
4658           mem = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4659
4660           /* If the misaligned store doesn't overwrite all bits, perform
4661              rmw cycle on MEM.  */
4662           if (bitsize != GET_MODE_BITSIZE (mode))
4663             {
4664               create_input_operand (&ops[0], to_rtx, mode);
4665               create_fixed_operand (&ops[1], mem);
4666               /* The movmisalign<mode> pattern cannot fail, else the assignment
4667                  would silently be omitted.  */
4668               expand_insn (icode, 2, ops);
4669
4670               mem = copy_rtx (mem);
4671             }
4672         }
4673       else
4674         {
4675           misalignp = false;
4676           to_rtx = expand_expr (tem, NULL_RTX, VOIDmode, EXPAND_WRITE);
4677         }
4678
4679       /* If the bitfield is volatile, we want to access it in the
4680          field's mode, not the computed mode.
4681          If a MEM has VOIDmode (external with incomplete type),
4682          use BLKmode for it instead.  */
4683       if (MEM_P (to_rtx))
4684         {
4685           if (volatilep && flag_strict_volatile_bitfields > 0)
4686             to_rtx = adjust_address (to_rtx, mode1, 0);
4687           else if (GET_MODE (to_rtx) == VOIDmode)
4688             to_rtx = adjust_address (to_rtx, BLKmode, 0);
4689         }
4690  
4691       if (offset != 0)
4692         {
4693           enum machine_mode address_mode;
4694           rtx offset_rtx;
4695
4696           if (!MEM_P (to_rtx))
4697             {
4698               /* We can get constant negative offsets into arrays with broken
4699                  user code.  Translate this to a trap instead of ICEing.  */
4700               gcc_assert (TREE_CODE (offset) == INTEGER_CST);
4701               expand_builtin_trap ();
4702               to_rtx = gen_rtx_MEM (BLKmode, const0_rtx);
4703             }
4704
4705           offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode, EXPAND_SUM);
4706           address_mode = get_address_mode (to_rtx);
4707           if (GET_MODE (offset_rtx) != address_mode)
4708             offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
4709
4710           /* A constant address in TO_RTX can have VOIDmode, we must not try
4711              to call force_reg for that case.  Avoid that case.  */
4712           if (MEM_P (to_rtx)
4713               && GET_MODE (to_rtx) == BLKmode
4714               && GET_MODE (XEXP (to_rtx, 0)) != VOIDmode
4715               && bitsize > 0
4716               && (bitpos % bitsize) == 0
4717               && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
4718               && MEM_ALIGN (to_rtx) == GET_MODE_ALIGNMENT (mode1))
4719             {
4720               to_rtx = adjust_address (to_rtx, mode1, bitpos / BITS_PER_UNIT);
4721               bitpos = 0;
4722             }
4723
4724           to_rtx = offset_address (to_rtx, offset_rtx,
4725                                    highest_pow2_factor_for_target (to,
4726                                                                    offset));
4727         }
4728
4729       /* No action is needed if the target is not a memory and the field
4730          lies completely outside that target.  This can occur if the source
4731          code contains an out-of-bounds access to a small array.  */
4732       if (!MEM_P (to_rtx)
4733           && GET_MODE (to_rtx) != BLKmode
4734           && (unsigned HOST_WIDE_INT) bitpos
4735              >= GET_MODE_PRECISION (GET_MODE (to_rtx)))
4736         {
4737           expand_normal (from);
4738           result = NULL;
4739         }
4740       /* Handle expand_expr of a complex value returning a CONCAT.  */
4741       else if (GET_CODE (to_rtx) == CONCAT)
4742         {
4743           unsigned short mode_bitsize = GET_MODE_BITSIZE (GET_MODE (to_rtx));
4744           if (COMPLEX_MODE_P (TYPE_MODE (TREE_TYPE (from)))
4745               && bitpos == 0
4746               && bitsize == mode_bitsize)
4747             result = store_expr (from, to_rtx, false, nontemporal);
4748           else if (bitsize == mode_bitsize / 2
4749                    && (bitpos == 0 || bitpos == mode_bitsize / 2))
4750             result = store_expr (from, XEXP (to_rtx, bitpos != 0), false,
4751                                  nontemporal);
4752           else if (bitpos + bitsize <= mode_bitsize / 2)
4753             result = store_field (XEXP (to_rtx, 0), bitsize, bitpos,
4754                                   bitregion_start, bitregion_end,
4755                                   mode1, from, TREE_TYPE (tem),
4756                                   get_alias_set (to), nontemporal);
4757           else if (bitpos >= mode_bitsize / 2)
4758             result = store_field (XEXP (to_rtx, 1), bitsize,
4759                                   bitpos - mode_bitsize / 2,
4760                                   bitregion_start, bitregion_end,
4761                                   mode1, from,
4762                                   TREE_TYPE (tem), get_alias_set (to),
4763                                   nontemporal);
4764           else if (bitpos == 0 && bitsize == mode_bitsize)
4765             {
4766               rtx from_rtx;
4767               result = expand_normal (from);
4768               from_rtx = simplify_gen_subreg (GET_MODE (to_rtx), result,
4769                                               TYPE_MODE (TREE_TYPE (from)), 0);
4770               emit_move_insn (XEXP (to_rtx, 0),
4771                               read_complex_part (from_rtx, false));
4772               emit_move_insn (XEXP (to_rtx, 1),
4773                               read_complex_part (from_rtx, true));
4774             }
4775           else
4776             {
4777               rtx temp = assign_stack_temp (GET_MODE (to_rtx),
4778                                             GET_MODE_SIZE (GET_MODE (to_rtx)),
4779                                             0);
4780               write_complex_part (temp, XEXP (to_rtx, 0), false);
4781               write_complex_part (temp, XEXP (to_rtx, 1), true);
4782               result = store_field (temp, bitsize, bitpos,
4783                                     bitregion_start, bitregion_end,
4784                                     mode1, from,
4785                                     TREE_TYPE (tem), get_alias_set (to),
4786                                     nontemporal);
4787               emit_move_insn (XEXP (to_rtx, 0), read_complex_part (temp, false));
4788               emit_move_insn (XEXP (to_rtx, 1), read_complex_part (temp, true));
4789             }
4790         }
4791       else
4792         {
4793           if (MEM_P (to_rtx))
4794             {
4795               /* If the field is at offset zero, we could have been given the
4796                  DECL_RTX of the parent struct.  Don't munge it.  */
4797               to_rtx = shallow_copy_rtx (to_rtx);
4798
4799               set_mem_attributes_minus_bitpos (to_rtx, to, 0, bitpos);
4800
4801               /* Deal with volatile and readonly fields.  The former is only
4802                  done for MEM.  Also set MEM_KEEP_ALIAS_SET_P if needed.  */
4803               if (volatilep)
4804                 MEM_VOLATILE_P (to_rtx) = 1;
4805               if (component_uses_parent_alias_set (to))
4806                 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
4807             }
4808
4809           if (optimize_bitfield_assignment_op (bitsize, bitpos,
4810                                                bitregion_start, bitregion_end,
4811                                                mode1,
4812                                                to_rtx, to, from))
4813             result = NULL;
4814           else
4815             result = store_field (to_rtx, bitsize, bitpos,
4816                                   bitregion_start, bitregion_end,
4817                                   mode1, from,
4818                                   TREE_TYPE (tem), get_alias_set (to),
4819                                   nontemporal);
4820         }
4821
4822       if (misalignp)
4823         {
4824           struct expand_operand ops[2];
4825
4826           create_fixed_operand (&ops[0], mem);
4827           create_input_operand (&ops[1], to_rtx, mode);
4828           /* The movmisalign<mode> pattern cannot fail, else the assignment
4829              would silently be omitted.  */
4830           expand_insn (icode, 2, ops);
4831         }
4832
4833       if (result)
4834         preserve_temp_slots (result);
4835       free_temp_slots ();
4836       pop_temp_slots ();
4837       return;
4838     }
4839
4840   /* If the rhs is a function call and its value is not an aggregate,
4841      call the function before we start to compute the lhs.
4842      This is needed for correct code for cases such as
4843      val = setjmp (buf) on machines where reference to val
4844      requires loading up part of an address in a separate insn.
4845
4846      Don't do this if TO is a VAR_DECL or PARM_DECL whose DECL_RTL is REG
4847      since it might be a promoted variable where the zero- or sign- extension
4848      needs to be done.  Handling this in the normal way is safe because no
4849      computation is done before the call.  The same is true for SSA names.  */
4850   if (TREE_CODE (from) == CALL_EXPR && ! aggregate_value_p (from, from)
4851       && COMPLETE_TYPE_P (TREE_TYPE (from))
4852       && TREE_CODE (TYPE_SIZE (TREE_TYPE (from))) == INTEGER_CST
4853       && ! (((TREE_CODE (to) == VAR_DECL
4854               || TREE_CODE (to) == PARM_DECL
4855               || TREE_CODE (to) == RESULT_DECL)
4856              && REG_P (DECL_RTL (to)))
4857             || TREE_CODE (to) == SSA_NAME))
4858     {
4859       rtx value;
4860
4861       push_temp_slots ();
4862       value = expand_normal (from);
4863       if (to_rtx == 0)
4864         to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4865
4866       /* Handle calls that return values in multiple non-contiguous locations.
4867          The Irix 6 ABI has examples of this.  */
4868       if (GET_CODE (to_rtx) == PARALLEL)
4869         emit_group_load (to_rtx, value, TREE_TYPE (from),
4870                          int_size_in_bytes (TREE_TYPE (from)));
4871       else if (GET_MODE (to_rtx) == BLKmode)
4872         emit_block_move (to_rtx, value, expr_size (from), BLOCK_OP_NORMAL);
4873       else
4874         {
4875           if (POINTER_TYPE_P (TREE_TYPE (to)))
4876             value = convert_memory_address_addr_space
4877                       (GET_MODE (to_rtx), value,
4878                        TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (to))));
4879
4880           emit_move_insn (to_rtx, value);
4881         }
4882       preserve_temp_slots (to_rtx);
4883       free_temp_slots ();
4884       pop_temp_slots ();
4885       return;
4886     }
4887
4888   /* Ordinary treatment.  Expand TO to get a REG or MEM rtx.  */
4889   to_rtx = expand_expr (to, NULL_RTX, VOIDmode, EXPAND_WRITE);
4890
4891   /* Don't move directly into a return register.  */
4892   if (TREE_CODE (to) == RESULT_DECL
4893       && (REG_P (to_rtx) || GET_CODE (to_rtx) == PARALLEL))
4894     {
4895       rtx temp;
4896
4897       push_temp_slots ();
4898       if (REG_P (to_rtx) && TYPE_MODE (TREE_TYPE (from)) == BLKmode)
4899         temp = copy_blkmode_to_reg (GET_MODE (to_rtx), from);
4900       else
4901         temp = expand_expr (from, NULL_RTX, GET_MODE (to_rtx), EXPAND_NORMAL);
4902
4903       if (GET_CODE (to_rtx) == PARALLEL)
4904         emit_group_load (to_rtx, temp, TREE_TYPE (from),
4905                          int_size_in_bytes (TREE_TYPE (from)));
4906       else if (temp)
4907         emit_move_insn (to_rtx, temp);
4908
4909       preserve_temp_slots (to_rtx);
4910       free_temp_slots ();
4911       pop_temp_slots ();
4912       return;
4913     }
4914
4915   /* In case we are returning the contents of an object which overlaps
4916      the place the value is being stored, use a safe function when copying
4917      a value through a pointer into a structure value return block.  */
4918   if (TREE_CODE (to) == RESULT_DECL
4919       && TREE_CODE (from) == INDIRECT_REF
4920       && ADDR_SPACE_GENERIC_P
4921            (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (from, 0)))))
4922       && refs_may_alias_p (to, from)
4923       && cfun->returns_struct
4924       && !cfun->returns_pcc_struct)
4925     {
4926       rtx from_rtx, size;
4927
4928       push_temp_slots ();
4929       size = expr_size (from);
4930       from_rtx = expand_normal (from);
4931
4932       emit_library_call (memmove_libfunc, LCT_NORMAL,
4933                          VOIDmode, 3, XEXP (to_rtx, 0), Pmode,
4934                          XEXP (from_rtx, 0), Pmode,
4935                          convert_to_mode (TYPE_MODE (sizetype),
4936                                           size, TYPE_UNSIGNED (sizetype)),
4937                          TYPE_MODE (sizetype));
4938
4939       preserve_temp_slots (to_rtx);
4940       free_temp_slots ();
4941       pop_temp_slots ();
4942       return;
4943     }
4944
4945   /* Compute FROM and store the value in the rtx we got.  */
4946
4947   push_temp_slots ();
4948   result = store_expr (from, to_rtx, 0, nontemporal);
4949   preserve_temp_slots (result);
4950   free_temp_slots ();
4951   pop_temp_slots ();
4952   return;
4953 }
4954
4955 /* Emits nontemporal store insn that moves FROM to TO.  Returns true if this
4956    succeeded, false otherwise.  */
4957
4958 bool
4959 emit_storent_insn (rtx to, rtx from)
4960 {
4961   struct expand_operand ops[2];
4962   enum machine_mode mode = GET_MODE (to);
4963   enum insn_code code = optab_handler (storent_optab, mode);
4964
4965   if (code == CODE_FOR_nothing)
4966     return false;
4967
4968   create_fixed_operand (&ops[0], to);
4969   create_input_operand (&ops[1], from, mode);
4970   return maybe_expand_insn (code, 2, ops);
4971 }
4972
4973 /* Generate code for computing expression EXP,
4974    and storing the value into TARGET.
4975
4976    If the mode is BLKmode then we may return TARGET itself.
4977    It turns out that in BLKmode it doesn't cause a problem.
4978    because C has no operators that could combine two different
4979    assignments into the same BLKmode object with different values
4980    with no sequence point.  Will other languages need this to
4981    be more thorough?
4982
4983    If CALL_PARAM_P is nonzero, this is a store into a call param on the
4984    stack, and block moves may need to be treated specially.
4985
4986    If NONTEMPORAL is true, try using a nontemporal store instruction.  */
4987
4988 rtx
4989 store_expr (tree exp, rtx target, int call_param_p, bool nontemporal)
4990 {
4991   rtx temp;
4992   rtx alt_rtl = NULL_RTX;
4993   location_t loc = EXPR_LOCATION (exp);
4994
4995   if (VOID_TYPE_P (TREE_TYPE (exp)))
4996     {
4997       /* C++ can generate ?: expressions with a throw expression in one
4998          branch and an rvalue in the other. Here, we resolve attempts to
4999          store the throw expression's nonexistent result.  */
5000       gcc_assert (!call_param_p);
5001       expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
5002       return NULL_RTX;
5003     }
5004   if (TREE_CODE (exp) == COMPOUND_EXPR)
5005     {
5006       /* Perform first part of compound expression, then assign from second
5007          part.  */
5008       expand_expr (TREE_OPERAND (exp, 0), const0_rtx, VOIDmode,
5009                    call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5010       return store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5011                          nontemporal);
5012     }
5013   else if (TREE_CODE (exp) == COND_EXPR && GET_MODE (target) == BLKmode)
5014     {
5015       /* For conditional expression, get safe form of the target.  Then
5016          test the condition, doing the appropriate assignment on either
5017          side.  This avoids the creation of unnecessary temporaries.
5018          For non-BLKmode, it is more efficient not to do this.  */
5019
5020       rtx lab1 = gen_label_rtx (), lab2 = gen_label_rtx ();
5021
5022       do_pending_stack_adjust ();
5023       NO_DEFER_POP;
5024       jumpifnot (TREE_OPERAND (exp, 0), lab1, -1);
5025       store_expr (TREE_OPERAND (exp, 1), target, call_param_p,
5026                   nontemporal);
5027       emit_jump_insn (gen_jump (lab2));
5028       emit_barrier ();
5029       emit_label (lab1);
5030       store_expr (TREE_OPERAND (exp, 2), target, call_param_p,
5031                   nontemporal);
5032       emit_label (lab2);
5033       OK_DEFER_POP;
5034
5035       return NULL_RTX;
5036     }
5037   else if (GET_CODE (target) == SUBREG && SUBREG_PROMOTED_VAR_P (target))
5038     /* If this is a scalar in a register that is stored in a wider mode
5039        than the declared mode, compute the result into its declared mode
5040        and then convert to the wider mode.  Our value is the computed
5041        expression.  */
5042     {
5043       rtx inner_target = 0;
5044
5045       /* We can do the conversion inside EXP, which will often result
5046          in some optimizations.  Do the conversion in two steps: first
5047          change the signedness, if needed, then the extend.  But don't
5048          do this if the type of EXP is a subtype of something else
5049          since then the conversion might involve more than just
5050          converting modes.  */
5051       if (INTEGRAL_TYPE_P (TREE_TYPE (exp))
5052           && TREE_TYPE (TREE_TYPE (exp)) == 0
5053           && GET_MODE_PRECISION (GET_MODE (target))
5054              == TYPE_PRECISION (TREE_TYPE (exp)))
5055         {
5056           if (TYPE_UNSIGNED (TREE_TYPE (exp))
5057               != SUBREG_PROMOTED_UNSIGNED_P (target))
5058             {
5059               /* Some types, e.g. Fortran's logical*4, won't have a signed
5060                  version, so use the mode instead.  */
5061               tree ntype
5062                 = (signed_or_unsigned_type_for
5063                    (SUBREG_PROMOTED_UNSIGNED_P (target), TREE_TYPE (exp)));
5064               if (ntype == NULL)
5065                 ntype = lang_hooks.types.type_for_mode
5066                   (TYPE_MODE (TREE_TYPE (exp)),
5067                    SUBREG_PROMOTED_UNSIGNED_P (target));
5068
5069               exp = fold_convert_loc (loc, ntype, exp);
5070             }
5071
5072           exp = fold_convert_loc (loc, lang_hooks.types.type_for_mode
5073                                   (GET_MODE (SUBREG_REG (target)),
5074                                    SUBREG_PROMOTED_UNSIGNED_P (target)),
5075                                   exp);
5076
5077           inner_target = SUBREG_REG (target);
5078         }
5079
5080       temp = expand_expr (exp, inner_target, VOIDmode,
5081                           call_param_p ? EXPAND_STACK_PARM : EXPAND_NORMAL);
5082
5083       /* If TEMP is a VOIDmode constant, use convert_modes to make
5084          sure that we properly convert it.  */
5085       if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode)
5086         {
5087           temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5088                                 temp, SUBREG_PROMOTED_UNSIGNED_P (target));
5089           temp = convert_modes (GET_MODE (SUBREG_REG (target)),
5090                                 GET_MODE (target), temp,
5091                                 SUBREG_PROMOTED_UNSIGNED_P (target));
5092         }
5093
5094       convert_move (SUBREG_REG (target), temp,
5095                     SUBREG_PROMOTED_UNSIGNED_P (target));
5096
5097       return NULL_RTX;
5098     }
5099   else if ((TREE_CODE (exp) == STRING_CST
5100             || (TREE_CODE (exp) == MEM_REF
5101                 && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
5102                 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
5103                    == STRING_CST
5104                 && integer_zerop (TREE_OPERAND (exp, 1))))
5105            && !nontemporal && !call_param_p
5106            && MEM_P (target))
5107     {
5108       /* Optimize initialization of an array with a STRING_CST.  */
5109       HOST_WIDE_INT exp_len, str_copy_len;
5110       rtx dest_mem;
5111       tree str = TREE_CODE (exp) == STRING_CST
5112                  ? exp : TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
5113
5114       exp_len = int_expr_size (exp);
5115       if (exp_len <= 0)
5116         goto normal_expr;
5117
5118       if (TREE_STRING_LENGTH (str) <= 0)
5119         goto normal_expr;
5120
5121       str_copy_len = strlen (TREE_STRING_POINTER (str));
5122       if (str_copy_len < TREE_STRING_LENGTH (str) - 1)
5123         goto normal_expr;
5124
5125       str_copy_len = TREE_STRING_LENGTH (str);
5126       if ((STORE_MAX_PIECES & (STORE_MAX_PIECES - 1)) == 0
5127           && TREE_STRING_POINTER (str)[TREE_STRING_LENGTH (str) - 1] == '\0')
5128         {
5129           str_copy_len += STORE_MAX_PIECES - 1;
5130           str_copy_len &= ~(STORE_MAX_PIECES - 1);
5131         }
5132       str_copy_len = MIN (str_copy_len, exp_len);
5133       if (!can_store_by_pieces (str_copy_len, builtin_strncpy_read_str,
5134                                 CONST_CAST (char *, TREE_STRING_POINTER (str)),
5135                                 MEM_ALIGN (target), false))
5136         goto normal_expr;
5137
5138       dest_mem = target;
5139
5140       dest_mem = store_by_pieces (dest_mem,
5141                                   str_copy_len, builtin_strncpy_read_str,
5142                                   CONST_CAST (char *,
5143                                               TREE_STRING_POINTER (str)),
5144                                   MEM_ALIGN (target), false,
5145                                   exp_len > str_copy_len ? 1 : 0);
5146       if (exp_len > str_copy_len)
5147         clear_storage (adjust_address (dest_mem, BLKmode, 0),
5148                        GEN_INT (exp_len - str_copy_len),
5149                        BLOCK_OP_NORMAL);
5150       return NULL_RTX;
5151     }
5152   else
5153     {
5154       rtx tmp_target;
5155
5156   normal_expr:
5157       /* If we want to use a nontemporal store, force the value to
5158          register first.  */
5159       tmp_target = nontemporal ? NULL_RTX : target;
5160       temp = expand_expr_real (exp, tmp_target, GET_MODE (target),
5161                                (call_param_p
5162                                 ? EXPAND_STACK_PARM : EXPAND_NORMAL),
5163                                &alt_rtl);
5164     }
5165
5166   /* If TEMP is a VOIDmode constant and the mode of the type of EXP is not
5167      the same as that of TARGET, adjust the constant.  This is needed, for
5168      example, in case it is a CONST_DOUBLE and we want only a word-sized
5169      value.  */
5170   if (CONSTANT_P (temp) && GET_MODE (temp) == VOIDmode
5171       && TREE_CODE (exp) != ERROR_MARK
5172       && GET_MODE (target) != TYPE_MODE (TREE_TYPE (exp)))
5173     temp = convert_modes (GET_MODE (target), TYPE_MODE (TREE_TYPE (exp)),
5174                           temp, TYPE_UNSIGNED (TREE_TYPE (exp)));
5175
5176   /* If value was not generated in the target, store it there.
5177      Convert the value to TARGET's type first if necessary and emit the
5178      pending incrementations that have been queued when expanding EXP.
5179      Note that we cannot emit the whole queue blindly because this will
5180      effectively disable the POST_INC optimization later.
5181
5182      If TEMP and TARGET compare equal according to rtx_equal_p, but
5183      one or both of them are volatile memory refs, we have to distinguish
5184      two cases:
5185      - expand_expr has used TARGET.  In this case, we must not generate
5186        another copy.  This can be detected by TARGET being equal according
5187        to == .
5188      - expand_expr has not used TARGET - that means that the source just
5189        happens to have the same RTX form.  Since temp will have been created
5190        by expand_expr, it will compare unequal according to == .
5191        We must generate a copy in this case, to reach the correct number
5192        of volatile memory references.  */
5193
5194   if ((! rtx_equal_p (temp, target)
5195        || (temp != target && (side_effects_p (temp)
5196                               || side_effects_p (target))))
5197       && TREE_CODE (exp) != ERROR_MARK
5198       /* If store_expr stores a DECL whose DECL_RTL(exp) == TARGET,
5199          but TARGET is not valid memory reference, TEMP will differ
5200          from TARGET although it is really the same location.  */
5201       && !(alt_rtl
5202            && rtx_equal_p (alt_rtl, target)
5203            && !side_effects_p (alt_rtl)
5204            && !side_effects_p (target))
5205       /* If there's nothing to copy, don't bother.  Don't call
5206          expr_size unless necessary, because some front-ends (C++)
5207          expr_size-hook must not be given objects that are not
5208          supposed to be bit-copied or bit-initialized.  */
5209       && expr_size (exp) != const0_rtx)
5210     {
5211       if (GET_MODE (temp) != GET_MODE (target)
5212           && GET_MODE (temp) != VOIDmode)
5213         {
5214           int unsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
5215           if (GET_MODE (target) == BLKmode
5216               && GET_MODE (temp) == BLKmode)
5217             emit_block_move (target, temp, expr_size (exp),
5218                              (call_param_p
5219                               ? BLOCK_OP_CALL_PARM
5220                               : BLOCK_OP_NORMAL));
5221           else if (GET_MODE (target) == BLKmode)
5222             store_bit_field (target, INTVAL (expr_size (exp)) * BITS_PER_UNIT,
5223                              0, 0, 0, GET_MODE (temp), temp);
5224           else
5225             convert_move (target, temp, unsignedp);
5226         }
5227
5228       else if (GET_MODE (temp) == BLKmode && TREE_CODE (exp) == STRING_CST)
5229         {
5230           /* Handle copying a string constant into an array.  The string
5231              constant may be shorter than the array.  So copy just the string's
5232              actual length, and clear the rest.  First get the size of the data
5233              type of the string, which is actually the size of the target.  */
5234           rtx size = expr_size (exp);
5235
5236           if (CONST_INT_P (size)
5237               && INTVAL (size) < TREE_STRING_LENGTH (exp))
5238             emit_block_move (target, temp, size,
5239                              (call_param_p
5240                               ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5241           else
5242             {
5243               enum machine_mode pointer_mode
5244                 = targetm.addr_space.pointer_mode (MEM_ADDR_SPACE (target));
5245               enum machine_mode address_mode = get_address_mode (target);
5246
5247               /* Compute the size of the data to copy from the string.  */
5248               tree copy_size
5249                 = size_binop_loc (loc, MIN_EXPR,
5250                                   make_tree (sizetype, size),
5251                                   size_int (TREE_STRING_LENGTH (exp)));
5252               rtx copy_size_rtx
5253                 = expand_expr (copy_size, NULL_RTX, VOIDmode,
5254                                (call_param_p
5255                                 ? EXPAND_STACK_PARM : EXPAND_NORMAL));
5256               rtx label = 0;
5257
5258               /* Copy that much.  */
5259               copy_size_rtx = convert_to_mode (pointer_mode, copy_size_rtx,
5260                                                TYPE_UNSIGNED (sizetype));
5261               emit_block_move (target, temp, copy_size_rtx,
5262                                (call_param_p
5263                                 ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5264
5265               /* Figure out how much is left in TARGET that we have to clear.
5266                  Do all calculations in pointer_mode.  */
5267               if (CONST_INT_P (copy_size_rtx))
5268                 {
5269                   size = plus_constant (address_mode, size,
5270                                         -INTVAL (copy_size_rtx));
5271                   target = adjust_address (target, BLKmode,
5272                                            INTVAL (copy_size_rtx));
5273                 }
5274               else
5275                 {
5276                   size = expand_binop (TYPE_MODE (sizetype), sub_optab, size,
5277                                        copy_size_rtx, NULL_RTX, 0,
5278                                        OPTAB_LIB_WIDEN);
5279
5280                   if (GET_MODE (copy_size_rtx) != address_mode)
5281                     copy_size_rtx = convert_to_mode (address_mode,
5282                                                      copy_size_rtx,
5283                                                      TYPE_UNSIGNED (sizetype));
5284
5285                   target = offset_address (target, copy_size_rtx,
5286                                            highest_pow2_factor (copy_size));
5287                   label = gen_label_rtx ();
5288                   emit_cmp_and_jump_insns (size, const0_rtx, LT, NULL_RTX,
5289                                            GET_MODE (size), 0, label);
5290                 }
5291
5292               if (size != const0_rtx)
5293                 clear_storage (target, size, BLOCK_OP_NORMAL);
5294
5295               if (label)
5296                 emit_label (label);
5297             }
5298         }
5299       /* Handle calls that return values in multiple non-contiguous locations.
5300          The Irix 6 ABI has examples of this.  */
5301       else if (GET_CODE (target) == PARALLEL)
5302         emit_group_load (target, temp, TREE_TYPE (exp),
5303                          int_size_in_bytes (TREE_TYPE (exp)));
5304       else if (GET_MODE (temp) == BLKmode)
5305         emit_block_move (target, temp, expr_size (exp),
5306                          (call_param_p
5307                           ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
5308       else if (nontemporal
5309                && emit_storent_insn (target, temp))
5310         /* If we managed to emit a nontemporal store, there is nothing else to
5311            do.  */
5312         ;
5313       else
5314         {
5315           temp = force_operand (temp, target);
5316           if (temp != target)
5317             emit_move_insn (target, temp);
5318         }
5319     }
5320
5321   return NULL_RTX;
5322 }
5323 \f
5324 /* Return true if field F of structure TYPE is a flexible array.  */
5325
5326 static bool
5327 flexible_array_member_p (const_tree f, const_tree type)
5328 {
5329   const_tree tf;
5330
5331   tf = TREE_TYPE (f);
5332   return (DECL_CHAIN (f) == NULL
5333           && TREE_CODE (tf) == ARRAY_TYPE
5334           && TYPE_DOMAIN (tf)
5335           && TYPE_MIN_VALUE (TYPE_DOMAIN (tf))
5336           && integer_zerop (TYPE_MIN_VALUE (TYPE_DOMAIN (tf)))
5337           && !TYPE_MAX_VALUE (TYPE_DOMAIN (tf))
5338           && int_size_in_bytes (type) >= 0);
5339 }
5340
5341 /* If FOR_CTOR_P, return the number of top-level elements that a constructor
5342    must have in order for it to completely initialize a value of type TYPE.
5343    Return -1 if the number isn't known.
5344
5345    If !FOR_CTOR_P, return an estimate of the number of scalars in TYPE.  */
5346
5347 static HOST_WIDE_INT
5348 count_type_elements (const_tree type, bool for_ctor_p)
5349 {
5350   switch (TREE_CODE (type))
5351     {
5352     case ARRAY_TYPE:
5353       {
5354         tree nelts;
5355
5356         nelts = array_type_nelts (type);
5357         if (nelts && host_integerp (nelts, 1))
5358           {
5359             unsigned HOST_WIDE_INT n;
5360
5361             n = tree_low_cst (nelts, 1) + 1;
5362             if (n == 0 || for_ctor_p)
5363               return n;
5364             else
5365               return n * count_type_elements (TREE_TYPE (type), false);
5366           }
5367         return for_ctor_p ? -1 : 1;
5368       }
5369
5370     case RECORD_TYPE:
5371       {
5372         unsigned HOST_WIDE_INT n;
5373         tree f;
5374
5375         n = 0;
5376         for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5377           if (TREE_CODE (f) == FIELD_DECL)
5378             {
5379               if (!for_ctor_p)
5380                 n += count_type_elements (TREE_TYPE (f), false);
5381               else if (!flexible_array_member_p (f, type))
5382                 /* Don't count flexible arrays, which are not supposed
5383                    to be initialized.  */
5384                 n += 1;
5385             }
5386
5387         return n;
5388       }
5389
5390     case UNION_TYPE:
5391     case QUAL_UNION_TYPE:
5392       {
5393         tree f;
5394         HOST_WIDE_INT n, m;
5395
5396         gcc_assert (!for_ctor_p);
5397         /* Estimate the number of scalars in each field and pick the
5398            maximum.  Other estimates would do instead; the idea is simply
5399            to make sure that the estimate is not sensitive to the ordering
5400            of the fields.  */
5401         n = 1;
5402         for (f = TYPE_FIELDS (type); f ; f = DECL_CHAIN (f))
5403           if (TREE_CODE (f) == FIELD_DECL)
5404             {
5405               m = count_type_elements (TREE_TYPE (f), false);
5406               /* If the field doesn't span the whole union, add an extra
5407                  scalar for the rest.  */
5408               if (simple_cst_equal (TYPE_SIZE (TREE_TYPE (f)),
5409                                     TYPE_SIZE (type)) != 1)
5410                 m++;
5411               if (n < m)
5412                 n = m;
5413             }
5414         return n;
5415       }
5416
5417     case COMPLEX_TYPE:
5418       return 2;
5419
5420     case VECTOR_TYPE:
5421       return TYPE_VECTOR_SUBPARTS (type);
5422
5423     case INTEGER_TYPE:
5424     case REAL_TYPE:
5425     case FIXED_POINT_TYPE:
5426     case ENUMERAL_TYPE:
5427     case BOOLEAN_TYPE:
5428     case POINTER_TYPE:
5429     case OFFSET_TYPE:
5430     case REFERENCE_TYPE:
5431     case NULLPTR_TYPE:
5432       return 1;
5433
5434     case ERROR_MARK:
5435       return 0;
5436
5437     case VOID_TYPE:
5438     case METHOD_TYPE:
5439     case FUNCTION_TYPE:
5440     case LANG_TYPE:
5441     default:
5442       gcc_unreachable ();
5443     }
5444 }
5445
5446 /* Helper for categorize_ctor_elements.  Identical interface.  */
5447
5448 static bool
5449 categorize_ctor_elements_1 (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5450                             HOST_WIDE_INT *p_init_elts, bool *p_complete)
5451 {
5452   unsigned HOST_WIDE_INT idx;
5453   HOST_WIDE_INT nz_elts, init_elts, num_fields;
5454   tree value, purpose, elt_type;
5455
5456   /* Whether CTOR is a valid constant initializer, in accordance with what
5457      initializer_constant_valid_p does.  If inferred from the constructor
5458      elements, true until proven otherwise.  */
5459   bool const_from_elts_p = constructor_static_from_elts_p (ctor);
5460   bool const_p = const_from_elts_p ? true : TREE_STATIC (ctor);
5461
5462   nz_elts = 0;
5463   init_elts = 0;
5464   num_fields = 0;
5465   elt_type = NULL_TREE;
5466
5467   FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), idx, purpose, value)
5468     {
5469       HOST_WIDE_INT mult = 1;
5470
5471       if (TREE_CODE (purpose) == RANGE_EXPR)
5472         {
5473           tree lo_index = TREE_OPERAND (purpose, 0);
5474           tree hi_index = TREE_OPERAND (purpose, 1);
5475
5476           if (host_integerp (lo_index, 1) && host_integerp (hi_index, 1))
5477             mult = (tree_low_cst (hi_index, 1)
5478                     - tree_low_cst (lo_index, 1) + 1);
5479         }
5480       num_fields += mult;
5481       elt_type = TREE_TYPE (value);
5482
5483       switch (TREE_CODE (value))
5484         {
5485         case CONSTRUCTOR:
5486           {
5487             HOST_WIDE_INT nz = 0, ic = 0;
5488
5489             bool const_elt_p = categorize_ctor_elements_1 (value, &nz, &ic,
5490                                                            p_complete);
5491
5492             nz_elts += mult * nz;
5493             init_elts += mult * ic;
5494
5495             if (const_from_elts_p && const_p)
5496               const_p = const_elt_p;
5497           }
5498           break;
5499
5500         case INTEGER_CST:
5501         case REAL_CST:
5502         case FIXED_CST:
5503           if (!initializer_zerop (value))
5504             nz_elts += mult;
5505           init_elts += mult;
5506           break;
5507
5508         case STRING_CST:
5509           nz_elts += mult * TREE_STRING_LENGTH (value);
5510           init_elts += mult * TREE_STRING_LENGTH (value);
5511           break;
5512
5513         case COMPLEX_CST:
5514           if (!initializer_zerop (TREE_REALPART (value)))
5515             nz_elts += mult;
5516           if (!initializer_zerop (TREE_IMAGPART (value)))
5517             nz_elts += mult;
5518           init_elts += mult;
5519           break;
5520
5521         case VECTOR_CST:
5522           {
5523             unsigned i;
5524             for (i = 0; i < VECTOR_CST_NELTS (value); ++i)
5525               {
5526                 tree v = VECTOR_CST_ELT (value, i);
5527                 if (!initializer_zerop (v))
5528                   nz_elts += mult;
5529                 init_elts += mult;
5530               }
5531           }
5532           break;
5533
5534         default:
5535           {
5536             HOST_WIDE_INT tc = count_type_elements (elt_type, false);
5537             nz_elts += mult * tc;
5538             init_elts += mult * tc;
5539
5540             if (const_from_elts_p && const_p)
5541               const_p = initializer_constant_valid_p (value, elt_type)
5542                         != NULL_TREE;
5543           }
5544           break;
5545         }
5546     }
5547
5548   if (*p_complete && !complete_ctor_at_level_p (TREE_TYPE (ctor),
5549                                                 num_fields, elt_type))
5550     *p_complete = false;
5551
5552   *p_nz_elts += nz_elts;
5553   *p_init_elts += init_elts;
5554
5555   return const_p;
5556 }
5557
5558 /* Examine CTOR to discover:
5559    * how many scalar fields are set to nonzero values,
5560      and place it in *P_NZ_ELTS;
5561    * how many scalar fields in total are in CTOR,
5562      and place it in *P_ELT_COUNT.
5563    * whether the constructor is complete -- in the sense that every
5564      meaningful byte is explicitly given a value --
5565      and place it in *P_COMPLETE.
5566
5567    Return whether or not CTOR is a valid static constant initializer, the same
5568    as "initializer_constant_valid_p (CTOR, TREE_TYPE (CTOR)) != 0".  */
5569
5570 bool
5571 categorize_ctor_elements (const_tree ctor, HOST_WIDE_INT *p_nz_elts,
5572                           HOST_WIDE_INT *p_init_elts, bool *p_complete)
5573 {
5574   *p_nz_elts = 0;
5575   *p_init_elts = 0;
5576   *p_complete = true;
5577
5578   return categorize_ctor_elements_1 (ctor, p_nz_elts, p_init_elts, p_complete);
5579 }
5580
5581 /* TYPE is initialized by a constructor with NUM_ELTS elements, the last
5582    of which had type LAST_TYPE.  Each element was itself a complete
5583    initializer, in the sense that every meaningful byte was explicitly
5584    given a value.  Return true if the same is true for the constructor
5585    as a whole.  */
5586
5587 bool
5588 complete_ctor_at_level_p (const_tree type, HOST_WIDE_INT num_elts,
5589                           const_tree last_type)
5590 {
5591   if (TREE_CODE (type) == UNION_TYPE
5592       || TREE_CODE (type) == QUAL_UNION_TYPE)
5593     {
5594       if (num_elts == 0)
5595         return false;
5596
5597       gcc_assert (num_elts == 1 && last_type);
5598
5599       /* ??? We could look at each element of the union, and find the
5600          largest element.  Which would avoid comparing the size of the
5601          initialized element against any tail padding in the union.
5602          Doesn't seem worth the effort...  */
5603       return simple_cst_equal (TYPE_SIZE (type), TYPE_SIZE (last_type)) == 1;
5604     }
5605
5606   return count_type_elements (type, true) == num_elts;
5607 }
5608
5609 /* Return 1 if EXP contains mostly (3/4)  zeros.  */
5610
5611 static int
5612 mostly_zeros_p (const_tree exp)
5613 {
5614   if (TREE_CODE (exp) == CONSTRUCTOR)
5615     {
5616       HOST_WIDE_INT nz_elts, init_elts;
5617       bool complete_p;
5618
5619       categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5620       return !complete_p || nz_elts < init_elts / 4;
5621     }
5622
5623   return initializer_zerop (exp);
5624 }
5625
5626 /* Return 1 if EXP contains all zeros.  */
5627
5628 static int
5629 all_zeros_p (const_tree exp)
5630 {
5631   if (TREE_CODE (exp) == CONSTRUCTOR)
5632     {
5633       HOST_WIDE_INT nz_elts, init_elts;
5634       bool complete_p;
5635
5636       categorize_ctor_elements (exp, &nz_elts, &init_elts, &complete_p);
5637       return nz_elts == 0;
5638     }
5639
5640   return initializer_zerop (exp);
5641 }
5642 \f
5643 /* Helper function for store_constructor.
5644    TARGET, BITSIZE, BITPOS, MODE, EXP are as for store_field.
5645    TYPE is the type of the CONSTRUCTOR, not the element type.
5646    CLEARED is as for store_constructor.
5647    ALIAS_SET is the alias set to use for any stores.
5648
5649    This provides a recursive shortcut back to store_constructor when it isn't
5650    necessary to go through store_field.  This is so that we can pass through
5651    the cleared field to let store_constructor know that we may not have to
5652    clear a substructure if the outer structure has already been cleared.  */
5653
5654 static void
5655 store_constructor_field (rtx target, unsigned HOST_WIDE_INT bitsize,
5656                          HOST_WIDE_INT bitpos, enum machine_mode mode,
5657                          tree exp, tree type, int cleared,
5658                          alias_set_type alias_set)
5659 {
5660   if (TREE_CODE (exp) == CONSTRUCTOR
5661       /* We can only call store_constructor recursively if the size and
5662          bit position are on a byte boundary.  */
5663       && bitpos % BITS_PER_UNIT == 0
5664       && (bitsize > 0 && bitsize % BITS_PER_UNIT == 0)
5665       /* If we have a nonzero bitpos for a register target, then we just
5666          let store_field do the bitfield handling.  This is unlikely to
5667          generate unnecessary clear instructions anyways.  */
5668       && (bitpos == 0 || MEM_P (target)))
5669     {
5670       if (MEM_P (target))
5671         target
5672           = adjust_address (target,
5673                             GET_MODE (target) == BLKmode
5674                             || 0 != (bitpos
5675                                      % GET_MODE_ALIGNMENT (GET_MODE (target)))
5676                             ? BLKmode : VOIDmode, bitpos / BITS_PER_UNIT);
5677
5678
5679       /* Update the alias set, if required.  */
5680       if (MEM_P (target) && ! MEM_KEEP_ALIAS_SET_P (target)
5681           && MEM_ALIAS_SET (target) != 0)
5682         {
5683           target = copy_rtx (target);
5684           set_mem_alias_set (target, alias_set);
5685         }
5686
5687       store_constructor (exp, target, cleared, bitsize / BITS_PER_UNIT);
5688     }
5689   else
5690     store_field (target, bitsize, bitpos, 0, 0, mode, exp, type, alias_set,
5691                  false);
5692 }
5693
5694 /* Store the value of constructor EXP into the rtx TARGET.
5695    TARGET is either a REG or a MEM; we know it cannot conflict, since
5696    safe_from_p has been called.
5697    CLEARED is true if TARGET is known to have been zero'd.
5698    SIZE is the number of bytes of TARGET we are allowed to modify: this
5699    may not be the same as the size of EXP if we are assigning to a field
5700    which has been packed to exclude padding bits.  */
5701
5702 static void
5703 store_constructor (tree exp, rtx target, int cleared, HOST_WIDE_INT size)
5704 {
5705   tree type = TREE_TYPE (exp);
5706 #ifdef WORD_REGISTER_OPERATIONS
5707   HOST_WIDE_INT exp_size = int_size_in_bytes (type);
5708 #endif
5709
5710   switch (TREE_CODE (type))
5711     {
5712     case RECORD_TYPE:
5713     case UNION_TYPE:
5714     case QUAL_UNION_TYPE:
5715       {
5716         unsigned HOST_WIDE_INT idx;
5717         tree field, value;
5718
5719         /* If size is zero or the target is already cleared, do nothing.  */
5720         if (size == 0 || cleared)
5721           cleared = 1;
5722         /* We either clear the aggregate or indicate the value is dead.  */
5723         else if ((TREE_CODE (type) == UNION_TYPE
5724                   || TREE_CODE (type) == QUAL_UNION_TYPE)
5725                  && ! CONSTRUCTOR_ELTS (exp))
5726           /* If the constructor is empty, clear the union.  */
5727           {
5728             clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
5729             cleared = 1;
5730           }
5731
5732         /* If we are building a static constructor into a register,
5733            set the initial value as zero so we can fold the value into
5734            a constant.  But if more than one register is involved,
5735            this probably loses.  */
5736         else if (REG_P (target) && TREE_STATIC (exp)
5737                  && GET_MODE_SIZE (GET_MODE (target)) <= UNITS_PER_WORD)
5738           {
5739             emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
5740             cleared = 1;
5741           }
5742
5743         /* If the constructor has fewer fields than the structure or
5744            if we are initializing the structure to mostly zeros, clear
5745            the whole structure first.  Don't do this if TARGET is a
5746            register whose mode size isn't equal to SIZE since
5747            clear_storage can't handle this case.  */
5748         else if (size > 0
5749                  && (((int)VEC_length (constructor_elt, CONSTRUCTOR_ELTS (exp))
5750                       != fields_length (type))
5751                      || mostly_zeros_p (exp))
5752                  && (!REG_P (target)
5753                      || ((HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (target))
5754                          == size)))
5755           {
5756             clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
5757             cleared = 1;
5758           }
5759
5760         if (REG_P (target) && !cleared)
5761           emit_clobber (target);
5762
5763         /* Store each element of the constructor into the
5764            corresponding field of TARGET.  */
5765         FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, field, value)
5766           {
5767             enum machine_mode mode;
5768             HOST_WIDE_INT bitsize;
5769             HOST_WIDE_INT bitpos = 0;
5770             tree offset;
5771             rtx to_rtx = target;
5772
5773             /* Just ignore missing fields.  We cleared the whole
5774                structure, above, if any fields are missing.  */
5775             if (field == 0)
5776               continue;
5777
5778             if (cleared && initializer_zerop (value))
5779               continue;
5780
5781             if (host_integerp (DECL_SIZE (field), 1))
5782               bitsize = tree_low_cst (DECL_SIZE (field), 1);
5783             else
5784               bitsize = -1;
5785
5786             mode = DECL_MODE (field);
5787             if (DECL_BIT_FIELD (field))
5788               mode = VOIDmode;
5789
5790             offset = DECL_FIELD_OFFSET (field);
5791             if (host_integerp (offset, 0)
5792                 && host_integerp (bit_position (field), 0))
5793               {
5794                 bitpos = int_bit_position (field);
5795                 offset = 0;
5796               }
5797             else
5798               bitpos = tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 0);
5799
5800             if (offset)
5801               {
5802                 enum machine_mode address_mode;
5803                 rtx offset_rtx;
5804
5805                 offset
5806                   = SUBSTITUTE_PLACEHOLDER_IN_EXPR (offset,
5807                                                     make_tree (TREE_TYPE (exp),
5808                                                                target));
5809
5810                 offset_rtx = expand_normal (offset);
5811                 gcc_assert (MEM_P (to_rtx));
5812
5813                 address_mode = get_address_mode (to_rtx);
5814                 if (GET_MODE (offset_rtx) != address_mode)
5815                   offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
5816
5817                 to_rtx = offset_address (to_rtx, offset_rtx,
5818                                          highest_pow2_factor (offset));
5819               }
5820
5821 #ifdef WORD_REGISTER_OPERATIONS
5822             /* If this initializes a field that is smaller than a
5823                word, at the start of a word, try to widen it to a full
5824                word.  This special case allows us to output C++ member
5825                function initializations in a form that the optimizers
5826                can understand.  */
5827             if (REG_P (target)
5828                 && bitsize < BITS_PER_WORD
5829                 && bitpos % BITS_PER_WORD == 0
5830                 && GET_MODE_CLASS (mode) == MODE_INT
5831                 && TREE_CODE (value) == INTEGER_CST
5832                 && exp_size >= 0
5833                 && bitpos + BITS_PER_WORD <= exp_size * BITS_PER_UNIT)
5834               {
5835                 tree type = TREE_TYPE (value);
5836
5837                 if (TYPE_PRECISION (type) < BITS_PER_WORD)
5838                   {
5839                     type = lang_hooks.types.type_for_mode
5840                       (word_mode, TYPE_UNSIGNED (type));
5841                     value = fold_convert (type, value);
5842                   }
5843
5844                 if (BYTES_BIG_ENDIAN)
5845                   value
5846                    = fold_build2 (LSHIFT_EXPR, type, value,
5847                                    build_int_cst (type,
5848                                                   BITS_PER_WORD - bitsize));
5849                 bitsize = BITS_PER_WORD;
5850                 mode = word_mode;
5851               }
5852 #endif
5853
5854             if (MEM_P (to_rtx) && !MEM_KEEP_ALIAS_SET_P (to_rtx)
5855                 && DECL_NONADDRESSABLE_P (field))
5856               {
5857                 to_rtx = copy_rtx (to_rtx);
5858                 MEM_KEEP_ALIAS_SET_P (to_rtx) = 1;
5859               }
5860
5861             store_constructor_field (to_rtx, bitsize, bitpos, mode,
5862                                      value, type, cleared,
5863                                      get_alias_set (TREE_TYPE (field)));
5864           }
5865         break;
5866       }
5867     case ARRAY_TYPE:
5868       {
5869         tree value, index;
5870         unsigned HOST_WIDE_INT i;
5871         int need_to_clear;
5872         tree domain;
5873         tree elttype = TREE_TYPE (type);
5874         int const_bounds_p;
5875         HOST_WIDE_INT minelt = 0;
5876         HOST_WIDE_INT maxelt = 0;
5877
5878         domain = TYPE_DOMAIN (type);
5879         const_bounds_p = (TYPE_MIN_VALUE (domain)
5880                           && TYPE_MAX_VALUE (domain)
5881                           && host_integerp (TYPE_MIN_VALUE (domain), 0)
5882                           && host_integerp (TYPE_MAX_VALUE (domain), 0));
5883
5884         /* If we have constant bounds for the range of the type, get them.  */
5885         if (const_bounds_p)
5886           {
5887             minelt = tree_low_cst (TYPE_MIN_VALUE (domain), 0);
5888             maxelt = tree_low_cst (TYPE_MAX_VALUE (domain), 0);
5889           }
5890
5891         /* If the constructor has fewer elements than the array, clear
5892            the whole array first.  Similarly if this is static
5893            constructor of a non-BLKmode object.  */
5894         if (cleared)
5895           need_to_clear = 0;
5896         else if (REG_P (target) && TREE_STATIC (exp))
5897           need_to_clear = 1;
5898         else
5899           {
5900             unsigned HOST_WIDE_INT idx;
5901             tree index, value;
5902             HOST_WIDE_INT count = 0, zero_count = 0;
5903             need_to_clear = ! const_bounds_p;
5904
5905             /* This loop is a more accurate version of the loop in
5906                mostly_zeros_p (it handles RANGE_EXPR in an index).  It
5907                is also needed to check for missing elements.  */
5908             FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), idx, index, value)
5909               {
5910                 HOST_WIDE_INT this_node_count;
5911
5912                 if (need_to_clear)
5913                   break;
5914
5915                 if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
5916                   {
5917                     tree lo_index = TREE_OPERAND (index, 0);
5918                     tree hi_index = TREE_OPERAND (index, 1);
5919
5920                     if (! host_integerp (lo_index, 1)
5921                         || ! host_integerp (hi_index, 1))
5922                       {
5923                         need_to_clear = 1;
5924                         break;
5925                       }
5926
5927                     this_node_count = (tree_low_cst (hi_index, 1)
5928                                        - tree_low_cst (lo_index, 1) + 1);
5929                   }
5930                 else
5931                   this_node_count = 1;
5932
5933                 count += this_node_count;
5934                 if (mostly_zeros_p (value))
5935                   zero_count += this_node_count;
5936               }
5937
5938             /* Clear the entire array first if there are any missing
5939                elements, or if the incidence of zero elements is >=
5940                75%.  */
5941             if (! need_to_clear
5942                 && (count < maxelt - minelt + 1
5943                     || 4 * zero_count >= 3 * count))
5944               need_to_clear = 1;
5945           }
5946
5947         if (need_to_clear && size > 0)
5948           {
5949             if (REG_P (target))
5950               emit_move_insn (target,  CONST0_RTX (GET_MODE (target)));
5951             else
5952               clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
5953             cleared = 1;
5954           }
5955
5956         if (!cleared && REG_P (target))
5957           /* Inform later passes that the old value is dead.  */
5958           emit_clobber (target);
5959
5960         /* Store each element of the constructor into the
5961            corresponding element of TARGET, determined by counting the
5962            elements.  */
5963         FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (exp), i, index, value)
5964           {
5965             enum machine_mode mode;
5966             HOST_WIDE_INT bitsize;
5967             HOST_WIDE_INT bitpos;
5968             rtx xtarget = target;
5969
5970             if (cleared && initializer_zerop (value))
5971               continue;
5972
5973             mode = TYPE_MODE (elttype);
5974             if (mode == BLKmode)
5975               bitsize = (host_integerp (TYPE_SIZE (elttype), 1)
5976                          ? tree_low_cst (TYPE_SIZE (elttype), 1)
5977                          : -1);
5978             else
5979               bitsize = GET_MODE_BITSIZE (mode);
5980
5981             if (index != NULL_TREE && TREE_CODE (index) == RANGE_EXPR)
5982               {
5983                 tree lo_index = TREE_OPERAND (index, 0);
5984                 tree hi_index = TREE_OPERAND (index, 1);
5985                 rtx index_r, pos_rtx;
5986                 HOST_WIDE_INT lo, hi, count;
5987                 tree position;
5988
5989                 /* If the range is constant and "small", unroll the loop.  */
5990                 if (const_bounds_p
5991                     && host_integerp (lo_index, 0)
5992                     && host_integerp (hi_index, 0)
5993                     && (lo = tree_low_cst (lo_index, 0),
5994                         hi = tree_low_cst (hi_index, 0),
5995                         count = hi - lo + 1,
5996                         (!MEM_P (target)
5997                          || count <= 2
5998                          || (host_integerp (TYPE_SIZE (elttype), 1)
5999                              && (tree_low_cst (TYPE_SIZE (elttype), 1) * count
6000                                  <= 40 * 8)))))
6001                   {
6002                     lo -= minelt;  hi -= minelt;
6003                     for (; lo <= hi; lo++)
6004                       {
6005                         bitpos = lo * tree_low_cst (TYPE_SIZE (elttype), 0);
6006
6007                         if (MEM_P (target)
6008                             && !MEM_KEEP_ALIAS_SET_P (target)
6009                             && TREE_CODE (type) == ARRAY_TYPE
6010                             && TYPE_NONALIASED_COMPONENT (type))
6011                           {
6012                             target = copy_rtx (target);
6013                             MEM_KEEP_ALIAS_SET_P (target) = 1;
6014                           }
6015
6016                         store_constructor_field
6017                           (target, bitsize, bitpos, mode, value, type, cleared,
6018                            get_alias_set (elttype));
6019                       }
6020                   }
6021                 else
6022                   {
6023                     rtx loop_start = gen_label_rtx ();
6024                     rtx loop_end = gen_label_rtx ();
6025                     tree exit_cond;
6026
6027                     expand_normal (hi_index);
6028
6029                     index = build_decl (EXPR_LOCATION (exp),
6030                                         VAR_DECL, NULL_TREE, domain);
6031                     index_r = gen_reg_rtx (promote_decl_mode (index, NULL));
6032                     SET_DECL_RTL (index, index_r);
6033                     store_expr (lo_index, index_r, 0, false);
6034
6035                     /* Build the head of the loop.  */
6036                     do_pending_stack_adjust ();
6037                     emit_label (loop_start);
6038
6039                     /* Assign value to element index.  */
6040                     position =
6041                       fold_convert (ssizetype,
6042                                     fold_build2 (MINUS_EXPR,
6043                                                  TREE_TYPE (index),
6044                                                  index,
6045                                                  TYPE_MIN_VALUE (domain)));
6046
6047                     position =
6048                         size_binop (MULT_EXPR, position,
6049                                     fold_convert (ssizetype,
6050                                                   TYPE_SIZE_UNIT (elttype)));
6051
6052                     pos_rtx = expand_normal (position);
6053                     xtarget = offset_address (target, pos_rtx,
6054                                               highest_pow2_factor (position));
6055                     xtarget = adjust_address (xtarget, mode, 0);
6056                     if (TREE_CODE (value) == CONSTRUCTOR)
6057                       store_constructor (value, xtarget, cleared,
6058                                          bitsize / BITS_PER_UNIT);
6059                     else
6060                       store_expr (value, xtarget, 0, false);
6061
6062                     /* Generate a conditional jump to exit the loop.  */
6063                     exit_cond = build2 (LT_EXPR, integer_type_node,
6064                                         index, hi_index);
6065                     jumpif (exit_cond, loop_end, -1);
6066
6067                     /* Update the loop counter, and jump to the head of
6068                        the loop.  */
6069                     expand_assignment (index,
6070                                        build2 (PLUS_EXPR, TREE_TYPE (index),
6071                                                index, integer_one_node),
6072                                        false);
6073
6074                     emit_jump (loop_start);
6075
6076                     /* Build the end of the loop.  */
6077                     emit_label (loop_end);
6078                   }
6079               }
6080             else if ((index != 0 && ! host_integerp (index, 0))
6081                      || ! host_integerp (TYPE_SIZE (elttype), 1))
6082               {
6083                 tree position;
6084
6085                 if (index == 0)
6086                   index = ssize_int (1);
6087
6088                 if (minelt)
6089                   index = fold_convert (ssizetype,
6090                                         fold_build2 (MINUS_EXPR,
6091                                                      TREE_TYPE (index),
6092                                                      index,
6093                                                      TYPE_MIN_VALUE (domain)));
6094
6095                 position =
6096                   size_binop (MULT_EXPR, index,
6097                               fold_convert (ssizetype,
6098                                             TYPE_SIZE_UNIT (elttype)));
6099                 xtarget = offset_address (target,
6100                                           expand_normal (position),
6101                                           highest_pow2_factor (position));
6102                 xtarget = adjust_address (xtarget, mode, 0);
6103                 store_expr (value, xtarget, 0, false);
6104               }
6105             else
6106               {
6107                 if (index != 0)
6108                   bitpos = ((tree_low_cst (index, 0) - minelt)
6109                             * tree_low_cst (TYPE_SIZE (elttype), 1));
6110                 else
6111                   bitpos = (i * tree_low_cst (TYPE_SIZE (elttype), 1));
6112
6113                 if (MEM_P (target) && !MEM_KEEP_ALIAS_SET_P (target)
6114                     && TREE_CODE (type) == ARRAY_TYPE
6115                     && TYPE_NONALIASED_COMPONENT (type))
6116                   {
6117                     target = copy_rtx (target);
6118                     MEM_KEEP_ALIAS_SET_P (target) = 1;
6119                   }
6120                 store_constructor_field (target, bitsize, bitpos, mode, value,
6121                                          type, cleared, get_alias_set (elttype));
6122               }
6123           }
6124         break;
6125       }
6126
6127     case VECTOR_TYPE:
6128       {
6129         unsigned HOST_WIDE_INT idx;
6130         constructor_elt *ce;
6131         int i;
6132         int need_to_clear;
6133         int icode = 0;
6134         tree elttype = TREE_TYPE (type);
6135         int elt_size = tree_low_cst (TYPE_SIZE (elttype), 1);
6136         enum machine_mode eltmode = TYPE_MODE (elttype);
6137         HOST_WIDE_INT bitsize;
6138         HOST_WIDE_INT bitpos;
6139         rtvec vector = NULL;
6140         unsigned n_elts;
6141         alias_set_type alias;
6142
6143         gcc_assert (eltmode != BLKmode);
6144
6145         n_elts = TYPE_VECTOR_SUBPARTS (type);
6146         if (REG_P (target) && VECTOR_MODE_P (GET_MODE (target)))
6147           {
6148             enum machine_mode mode = GET_MODE (target);
6149
6150             icode = (int) optab_handler (vec_init_optab, mode);
6151             if (icode != CODE_FOR_nothing)
6152               {
6153                 unsigned int i;
6154
6155                 vector = rtvec_alloc (n_elts);
6156                 for (i = 0; i < n_elts; i++)
6157                   RTVEC_ELT (vector, i) = CONST0_RTX (GET_MODE_INNER (mode));
6158               }
6159           }
6160
6161         /* If the constructor has fewer elements than the vector,
6162            clear the whole array first.  Similarly if this is static
6163            constructor of a non-BLKmode object.  */
6164         if (cleared)
6165           need_to_clear = 0;
6166         else if (REG_P (target) && TREE_STATIC (exp))
6167           need_to_clear = 1;
6168         else
6169           {
6170             unsigned HOST_WIDE_INT count = 0, zero_count = 0;
6171             tree value;
6172
6173             FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
6174               {
6175                 int n_elts_here = tree_low_cst
6176                   (int_const_binop (TRUNC_DIV_EXPR,
6177                                     TYPE_SIZE (TREE_TYPE (value)),
6178                                     TYPE_SIZE (elttype)), 1);
6179
6180                 count += n_elts_here;
6181                 if (mostly_zeros_p (value))
6182                   zero_count += n_elts_here;
6183               }
6184
6185             /* Clear the entire vector first if there are any missing elements,
6186                or if the incidence of zero elements is >= 75%.  */
6187             need_to_clear = (count < n_elts || 4 * zero_count >= 3 * count);
6188           }
6189
6190         if (need_to_clear && size > 0 && !vector)
6191           {
6192             if (REG_P (target))
6193               emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6194             else
6195               clear_storage (target, GEN_INT (size), BLOCK_OP_NORMAL);
6196             cleared = 1;
6197           }
6198
6199         /* Inform later passes that the old value is dead.  */
6200         if (!cleared && !vector && REG_P (target))
6201           emit_move_insn (target, CONST0_RTX (GET_MODE (target)));
6202
6203         if (MEM_P (target))
6204           alias = MEM_ALIAS_SET (target);
6205         else
6206           alias = get_alias_set (elttype);
6207
6208         /* Store each element of the constructor into the corresponding
6209            element of TARGET, determined by counting the elements.  */
6210         for (idx = 0, i = 0;
6211              VEC_iterate (constructor_elt, CONSTRUCTOR_ELTS (exp), idx, ce);
6212              idx++, i += bitsize / elt_size)
6213           {
6214             HOST_WIDE_INT eltpos;
6215             tree value = ce->value;
6216
6217             bitsize = tree_low_cst (TYPE_SIZE (TREE_TYPE (value)), 1);
6218             if (cleared && initializer_zerop (value))
6219               continue;
6220
6221             if (ce->index)
6222               eltpos = tree_low_cst (ce->index, 1);
6223             else
6224               eltpos = i;
6225
6226             if (vector)
6227               {
6228                 /* Vector CONSTRUCTORs should only be built from smaller
6229                    vectors in the case of BLKmode vectors.  */
6230                 gcc_assert (TREE_CODE (TREE_TYPE (value)) != VECTOR_TYPE);
6231                 RTVEC_ELT (vector, eltpos)
6232                   = expand_normal (value);
6233               }
6234             else
6235               {
6236                 enum machine_mode value_mode =
6237                   TREE_CODE (TREE_TYPE (value)) == VECTOR_TYPE
6238                   ? TYPE_MODE (TREE_TYPE (value))
6239                   : eltmode;
6240                 bitpos = eltpos * elt_size;
6241                 store_constructor_field (target, bitsize, bitpos,
6242                                          value_mode, value, type,
6243                                          cleared, alias);
6244               }
6245           }
6246
6247         if (vector)
6248           emit_insn (GEN_FCN (icode)
6249                      (target,
6250                       gen_rtx_PARALLEL (GET_MODE (target), vector)));
6251         break;
6252       }
6253
6254     default:
6255       gcc_unreachable ();
6256     }
6257 }
6258
6259 /* Store the value of EXP (an expression tree)
6260    into a subfield of TARGET which has mode MODE and occupies
6261    BITSIZE bits, starting BITPOS bits from the start of TARGET.
6262    If MODE is VOIDmode, it means that we are storing into a bit-field.
6263
6264    BITREGION_START is bitpos of the first bitfield in this region.
6265    BITREGION_END is the bitpos of the ending bitfield in this region.
6266    These two fields are 0, if the C++ memory model does not apply,
6267    or we are not interested in keeping track of bitfield regions.
6268
6269    Always return const0_rtx unless we have something particular to
6270    return.
6271
6272    TYPE is the type of the underlying object,
6273
6274    ALIAS_SET is the alias set for the destination.  This value will
6275    (in general) be different from that for TARGET, since TARGET is a
6276    reference to the containing structure.
6277
6278    If NONTEMPORAL is true, try generating a nontemporal store.  */
6279
6280 static rtx
6281 store_field (rtx target, HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos,
6282              unsigned HOST_WIDE_INT bitregion_start,
6283              unsigned HOST_WIDE_INT bitregion_end,
6284              enum machine_mode mode, tree exp, tree type,
6285              alias_set_type alias_set, bool nontemporal)
6286 {
6287   if (TREE_CODE (exp) == ERROR_MARK)
6288     return const0_rtx;
6289
6290   /* If we have nothing to store, do nothing unless the expression has
6291      side-effects.  */
6292   if (bitsize == 0)
6293     return expand_expr (exp, const0_rtx, VOIDmode, EXPAND_NORMAL);
6294
6295   /* If we are storing into an unaligned field of an aligned union that is
6296      in a register, we may have the mode of TARGET being an integer mode but
6297      MODE == BLKmode.  In that case, get an aligned object whose size and
6298      alignment are the same as TARGET and store TARGET into it (we can avoid
6299      the store if the field being stored is the entire width of TARGET).  Then
6300      call ourselves recursively to store the field into a BLKmode version of
6301      that object.  Finally, load from the object into TARGET.  This is not
6302      very efficient in general, but should only be slightly more expensive
6303      than the otherwise-required unaligned accesses.  Perhaps this can be
6304      cleaned up later.  It's tempting to make OBJECT readonly, but it's set
6305      twice, once with emit_move_insn and once via store_field.  */
6306
6307   if (mode == BLKmode
6308       && (REG_P (target) || GET_CODE (target) == SUBREG))
6309     {
6310       rtx object = assign_temp (type, 0, 1, 1);
6311       rtx blk_object = adjust_address (object, BLKmode, 0);
6312
6313       if (bitsize != (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (target)))
6314         emit_move_insn (object, target);
6315
6316       store_field (blk_object, bitsize, bitpos,
6317                    bitregion_start, bitregion_end,
6318                    mode, exp, type, MEM_ALIAS_SET (blk_object), nontemporal);
6319
6320       emit_move_insn (target, object);
6321
6322       /* We want to return the BLKmode version of the data.  */
6323       return blk_object;
6324     }
6325
6326   if (GET_CODE (target) == CONCAT)
6327     {
6328       /* We're storing into a struct containing a single __complex.  */
6329
6330       gcc_assert (!bitpos);
6331       return store_expr (exp, target, 0, nontemporal);
6332     }
6333
6334   /* If the structure is in a register or if the component
6335      is a bit field, we cannot use addressing to access it.
6336      Use bit-field techniques or SUBREG to store in it.  */
6337
6338   if (mode == VOIDmode
6339       || (mode != BLKmode && ! direct_store[(int) mode]
6340           && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
6341           && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT)
6342       || REG_P (target)
6343       || GET_CODE (target) == SUBREG
6344       /* If the field isn't aligned enough to store as an ordinary memref,
6345          store it as a bit field.  */
6346       || (mode != BLKmode
6347           && ((((MEM_ALIGN (target) < GET_MODE_ALIGNMENT (mode))
6348                 || bitpos % GET_MODE_ALIGNMENT (mode))
6349                && SLOW_UNALIGNED_ACCESS (mode, MEM_ALIGN (target)))
6350               || (bitpos % BITS_PER_UNIT != 0)))
6351       || (bitsize >= 0 && mode != BLKmode
6352           && GET_MODE_BITSIZE (mode) > bitsize)
6353       /* If the RHS and field are a constant size and the size of the
6354          RHS isn't the same size as the bitfield, we must use bitfield
6355          operations.  */
6356       || (bitsize >= 0
6357           && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
6358           && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) != 0)
6359       /* If we are expanding a MEM_REF of a non-BLKmode non-addressable
6360          decl we must use bitfield operations.  */
6361       || (bitsize >= 0
6362           && TREE_CODE (exp) == MEM_REF
6363           && TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR
6364           && DECL_P (TREE_OPERAND (TREE_OPERAND (exp, 0), 0))
6365           && !TREE_ADDRESSABLE (TREE_OPERAND (TREE_OPERAND (exp, 0),0 ))
6366           && DECL_MODE (TREE_OPERAND (TREE_OPERAND (exp, 0), 0)) != BLKmode))
6367     {
6368       rtx temp;
6369       gimple nop_def;
6370
6371       /* If EXP is a NOP_EXPR of precision less than its mode, then that
6372          implies a mask operation.  If the precision is the same size as
6373          the field we're storing into, that mask is redundant.  This is
6374          particularly common with bit field assignments generated by the
6375          C front end.  */
6376       nop_def = get_def_for_expr (exp, NOP_EXPR);
6377       if (nop_def)
6378         {
6379           tree type = TREE_TYPE (exp);
6380           if (INTEGRAL_TYPE_P (type)
6381               && TYPE_PRECISION (type) < GET_MODE_BITSIZE (TYPE_MODE (type))
6382               && bitsize == TYPE_PRECISION (type))
6383             {
6384               tree op = gimple_assign_rhs1 (nop_def);
6385               type = TREE_TYPE (op);
6386               if (INTEGRAL_TYPE_P (type) && TYPE_PRECISION (type) >= bitsize)
6387                 exp = op;
6388             }
6389         }
6390
6391       temp = expand_normal (exp);
6392
6393       /* If BITSIZE is narrower than the size of the type of EXP
6394          we will be narrowing TEMP.  Normally, what's wanted are the
6395          low-order bits.  However, if EXP's type is a record and this is
6396          big-endian machine, we want the upper BITSIZE bits.  */
6397       if (BYTES_BIG_ENDIAN && GET_MODE_CLASS (GET_MODE (temp)) == MODE_INT
6398           && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (temp))
6399           && TREE_CODE (TREE_TYPE (exp)) == RECORD_TYPE)
6400         temp = expand_shift (RSHIFT_EXPR, GET_MODE (temp), temp,
6401                              GET_MODE_BITSIZE (GET_MODE (temp)) - bitsize,
6402                              NULL_RTX, 1);
6403
6404       /* Unless MODE is VOIDmode or BLKmode, convert TEMP to MODE.  */
6405       if (mode != VOIDmode && mode != BLKmode
6406           && mode != TYPE_MODE (TREE_TYPE (exp)))
6407         temp = convert_modes (mode, TYPE_MODE (TREE_TYPE (exp)), temp, 1);
6408
6409       /* If the modes of TEMP and TARGET are both BLKmode, both
6410          must be in memory and BITPOS must be aligned on a byte
6411          boundary.  If so, we simply do a block copy.  Likewise
6412          for a BLKmode-like TARGET.  */
6413       if (GET_MODE (temp) == BLKmode
6414           && (GET_MODE (target) == BLKmode
6415               || (MEM_P (target)
6416                   && GET_MODE_CLASS (GET_MODE (target)) == MODE_INT
6417                   && (bitpos % BITS_PER_UNIT) == 0
6418                   && (bitsize % BITS_PER_UNIT) == 0)))
6419         {
6420           gcc_assert (MEM_P (target) && MEM_P (temp)
6421                       && (bitpos % BITS_PER_UNIT) == 0);
6422
6423           target = adjust_address (target, VOIDmode, bitpos / BITS_PER_UNIT);
6424           emit_block_move (target, temp,
6425                            GEN_INT ((bitsize + BITS_PER_UNIT - 1)
6426                                     / BITS_PER_UNIT),
6427                            BLOCK_OP_NORMAL);
6428
6429           return const0_rtx;
6430         }
6431
6432       /* Store the value in the bitfield.  */
6433       store_bit_field (target, bitsize, bitpos,
6434                        bitregion_start, bitregion_end,
6435                        mode, temp);
6436
6437       return const0_rtx;
6438     }
6439   else
6440     {
6441       /* Now build a reference to just the desired component.  */
6442       rtx to_rtx = adjust_address (target, mode, bitpos / BITS_PER_UNIT);
6443
6444       if (to_rtx == target)
6445         to_rtx = copy_rtx (to_rtx);
6446
6447       if (!MEM_KEEP_ALIAS_SET_P (to_rtx) && MEM_ALIAS_SET (to_rtx) != 0)
6448         set_mem_alias_set (to_rtx, alias_set);
6449
6450       return store_expr (exp, to_rtx, 0, nontemporal);
6451     }
6452 }
6453 \f
6454 /* Given an expression EXP that may be a COMPONENT_REF, a BIT_FIELD_REF,
6455    an ARRAY_REF, or an ARRAY_RANGE_REF, look for nested operations of these
6456    codes and find the ultimate containing object, which we return.
6457
6458    We set *PBITSIZE to the size in bits that we want, *PBITPOS to the
6459    bit position, and *PUNSIGNEDP to the signedness of the field.
6460    If the position of the field is variable, we store a tree
6461    giving the variable offset (in units) in *POFFSET.
6462    This offset is in addition to the bit position.
6463    If the position is not variable, we store 0 in *POFFSET.
6464
6465    If any of the extraction expressions is volatile,
6466    we store 1 in *PVOLATILEP.  Otherwise we don't change that.
6467
6468    If the field is a non-BLKmode bit-field, *PMODE is set to VOIDmode.
6469    Otherwise, it is a mode that can be used to access the field.
6470
6471    If the field describes a variable-sized object, *PMODE is set to
6472    BLKmode and *PBITSIZE is set to -1.  An access cannot be made in
6473    this case, but the address of the object can be found.
6474
6475    If KEEP_ALIGNING is true and the target is STRICT_ALIGNMENT, we don't
6476    look through nodes that serve as markers of a greater alignment than
6477    the one that can be deduced from the expression.  These nodes make it
6478    possible for front-ends to prevent temporaries from being created by
6479    the middle-end on alignment considerations.  For that purpose, the
6480    normal operating mode at high-level is to always pass FALSE so that
6481    the ultimate containing object is really returned; moreover, the
6482    associated predicate handled_component_p will always return TRUE
6483    on these nodes, thus indicating that they are essentially handled
6484    by get_inner_reference.  TRUE should only be passed when the caller
6485    is scanning the expression in order to build another representation
6486    and specifically knows how to handle these nodes; as such, this is
6487    the normal operating mode in the RTL expanders.  */
6488
6489 tree
6490 get_inner_reference (tree exp, HOST_WIDE_INT *pbitsize,
6491                      HOST_WIDE_INT *pbitpos, tree *poffset,
6492                      enum machine_mode *pmode, int *punsignedp,
6493                      int *pvolatilep, bool keep_aligning)
6494 {
6495   tree size_tree = 0;
6496   enum machine_mode mode = VOIDmode;
6497   bool blkmode_bitfield = false;
6498   tree offset = size_zero_node;
6499   double_int bit_offset = double_int_zero;
6500
6501   /* First get the mode, signedness, and size.  We do this from just the
6502      outermost expression.  */
6503   *pbitsize = -1;
6504   if (TREE_CODE (exp) == COMPONENT_REF)
6505     {
6506       tree field = TREE_OPERAND (exp, 1);
6507       size_tree = DECL_SIZE (field);
6508       if (!DECL_BIT_FIELD (field))
6509         mode = DECL_MODE (field);
6510       else if (DECL_MODE (field) == BLKmode)
6511         blkmode_bitfield = true;
6512       else if (TREE_THIS_VOLATILE (exp)
6513                && flag_strict_volatile_bitfields > 0)
6514         /* Volatile bitfields should be accessed in the mode of the
6515              field's type, not the mode computed based on the bit
6516              size.  */
6517         mode = TYPE_MODE (DECL_BIT_FIELD_TYPE (field));
6518
6519       *punsignedp = DECL_UNSIGNED (field);
6520     }
6521   else if (TREE_CODE (exp) == BIT_FIELD_REF)
6522     {
6523       size_tree = TREE_OPERAND (exp, 1);
6524       *punsignedp = (! INTEGRAL_TYPE_P (TREE_TYPE (exp))
6525                      || TYPE_UNSIGNED (TREE_TYPE (exp)));
6526
6527       /* For vector types, with the correct size of access, use the mode of
6528          inner type.  */
6529       if (TREE_CODE (TREE_TYPE (TREE_OPERAND (exp, 0))) == VECTOR_TYPE
6530           && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)))
6531           && tree_int_cst_equal (size_tree, TYPE_SIZE (TREE_TYPE (exp))))
6532         mode = TYPE_MODE (TREE_TYPE (exp));
6533     }
6534   else
6535     {
6536       mode = TYPE_MODE (TREE_TYPE (exp));
6537       *punsignedp = TYPE_UNSIGNED (TREE_TYPE (exp));
6538
6539       if (mode == BLKmode)
6540         size_tree = TYPE_SIZE (TREE_TYPE (exp));
6541       else
6542         *pbitsize = GET_MODE_BITSIZE (mode);
6543     }
6544
6545   if (size_tree != 0)
6546     {
6547       if (! host_integerp (size_tree, 1))
6548         mode = BLKmode, *pbitsize = -1;
6549       else
6550         *pbitsize = tree_low_cst (size_tree, 1);
6551     }
6552
6553   /* Compute cumulative bit-offset for nested component-refs and array-refs,
6554      and find the ultimate containing object.  */
6555   while (1)
6556     {
6557       switch (TREE_CODE (exp))
6558         {
6559         case BIT_FIELD_REF:
6560           bit_offset
6561             = double_int_add (bit_offset,
6562                               tree_to_double_int (TREE_OPERAND (exp, 2)));
6563           break;
6564
6565         case COMPONENT_REF:
6566           {
6567             tree field = TREE_OPERAND (exp, 1);
6568             tree this_offset = component_ref_field_offset (exp);
6569
6570             /* If this field hasn't been filled in yet, don't go past it.
6571                This should only happen when folding expressions made during
6572                type construction.  */
6573             if (this_offset == 0)
6574               break;
6575
6576             offset = size_binop (PLUS_EXPR, offset, this_offset);
6577             bit_offset = double_int_add (bit_offset,
6578                                          tree_to_double_int
6579                                            (DECL_FIELD_BIT_OFFSET (field)));
6580
6581             /* ??? Right now we don't do anything with DECL_OFFSET_ALIGN.  */
6582           }
6583           break;
6584
6585         case ARRAY_REF:
6586         case ARRAY_RANGE_REF:
6587           {
6588             tree index = TREE_OPERAND (exp, 1);
6589             tree low_bound = array_ref_low_bound (exp);
6590             tree unit_size = array_ref_element_size (exp);
6591
6592             /* We assume all arrays have sizes that are a multiple of a byte.
6593                First subtract the lower bound, if any, in the type of the
6594                index, then convert to sizetype and multiply by the size of
6595                the array element.  */
6596             if (! integer_zerop (low_bound))
6597               index = fold_build2 (MINUS_EXPR, TREE_TYPE (index),
6598                                    index, low_bound);
6599
6600             offset = size_binop (PLUS_EXPR, offset,
6601                                  size_binop (MULT_EXPR,
6602                                              fold_convert (sizetype, index),
6603                                              unit_size));
6604           }
6605           break;
6606
6607         case REALPART_EXPR:
6608           break;
6609
6610         case IMAGPART_EXPR:
6611           bit_offset = double_int_add (bit_offset,
6612                                        uhwi_to_double_int (*pbitsize));
6613           break;
6614
6615         case VIEW_CONVERT_EXPR:
6616           if (keep_aligning && STRICT_ALIGNMENT
6617               && (TYPE_ALIGN (TREE_TYPE (exp))
6618                > TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0))))
6619               && (TYPE_ALIGN (TREE_TYPE (TREE_OPERAND (exp, 0)))
6620                   < BIGGEST_ALIGNMENT)
6621               && (TYPE_ALIGN_OK (TREE_TYPE (exp))
6622                   || TYPE_ALIGN_OK (TREE_TYPE (TREE_OPERAND (exp, 0)))))
6623             goto done;
6624           break;
6625
6626         case MEM_REF:
6627           /* Hand back the decl for MEM[&decl, off].  */
6628           if (TREE_CODE (TREE_OPERAND (exp, 0)) == ADDR_EXPR)
6629             {
6630               tree off = TREE_OPERAND (exp, 1);
6631               if (!integer_zerop (off))
6632                 {
6633                   double_int boff, coff = mem_ref_offset (exp);
6634                   boff = double_int_lshift (coff,
6635                                             BITS_PER_UNIT == 8
6636                                             ? 3 : exact_log2 (BITS_PER_UNIT),
6637                                             HOST_BITS_PER_DOUBLE_INT, true);
6638                   bit_offset = double_int_add (bit_offset, boff);
6639                 }
6640               exp = TREE_OPERAND (TREE_OPERAND (exp, 0), 0);
6641             }
6642           goto done;
6643
6644         default:
6645           goto done;
6646         }
6647
6648       /* If any reference in the chain is volatile, the effect is volatile.  */
6649       if (TREE_THIS_VOLATILE (exp))
6650         *pvolatilep = 1;
6651
6652       exp = TREE_OPERAND (exp, 0);
6653     }
6654  done:
6655
6656   /* If OFFSET is constant, see if we can return the whole thing as a
6657      constant bit position.  Make sure to handle overflow during
6658      this conversion.  */
6659   if (TREE_CODE (offset) == INTEGER_CST)
6660     {
6661       double_int tem = tree_to_double_int (offset);
6662       tem = double_int_sext (tem, TYPE_PRECISION (sizetype));
6663       tem = double_int_lshift (tem,
6664                                BITS_PER_UNIT == 8
6665                                ? 3 : exact_log2 (BITS_PER_UNIT),
6666                                HOST_BITS_PER_DOUBLE_INT, true);
6667       tem = double_int_add (tem, bit_offset);
6668       if (double_int_fits_in_shwi_p (tem))
6669         {
6670           *pbitpos = double_int_to_shwi (tem);
6671           *poffset = offset = NULL_TREE;
6672         }
6673     }
6674
6675   /* Otherwise, split it up.  */
6676   if (offset)
6677     {
6678       /* Avoid returning a negative bitpos as this may wreak havoc later.  */
6679       if (double_int_negative_p (bit_offset))
6680         {
6681           double_int mask
6682             = double_int_mask (BITS_PER_UNIT == 8
6683                                ? 3 : exact_log2 (BITS_PER_UNIT));
6684           double_int tem = double_int_and_not (bit_offset, mask);
6685           /* TEM is the bitpos rounded to BITS_PER_UNIT towards -Inf.
6686              Subtract it to BIT_OFFSET and add it (scaled) to OFFSET.  */
6687           bit_offset = double_int_sub (bit_offset, tem);
6688           tem = double_int_rshift (tem,
6689                                    BITS_PER_UNIT == 8
6690                                    ? 3 : exact_log2 (BITS_PER_UNIT),
6691                                    HOST_BITS_PER_DOUBLE_INT, true);
6692           offset = size_binop (PLUS_EXPR, offset,
6693                                double_int_to_tree (sizetype, tem));
6694         }
6695
6696       *pbitpos = double_int_to_shwi (bit_offset);
6697       *poffset = offset;
6698     }
6699
6700   /* We can use BLKmode for a byte-aligned BLKmode bitfield.  */
6701   if (mode == VOIDmode
6702       && blkmode_bitfield
6703       && (*pbitpos % BITS_PER_UNIT) == 0
6704       && (*pbitsize % BITS_PER_UNIT) == 0)
6705     *pmode = BLKmode;
6706   else
6707     *pmode = mode;
6708
6709   return exp;
6710 }
6711
6712 /* Given an expression EXP that may be a COMPONENT_REF, an ARRAY_REF or an
6713    ARRAY_RANGE_REF, look for whether EXP or any nested component-refs within
6714    EXP is marked as PACKED.  */
6715
6716 bool
6717 contains_packed_reference (const_tree exp)
6718 {
6719   bool packed_p = false;
6720
6721   while (1)
6722     {
6723       switch (TREE_CODE (exp))
6724         {
6725         case COMPONENT_REF:
6726           {
6727             tree field = TREE_OPERAND (exp, 1);
6728             packed_p = DECL_PACKED (field)
6729                        || TYPE_PACKED (TREE_TYPE (field))
6730                        || TYPE_PACKED (TREE_TYPE (exp));
6731             if (packed_p)
6732               goto done;
6733           }
6734           break;
6735
6736         case BIT_FIELD_REF:
6737         case ARRAY_REF:
6738         case ARRAY_RANGE_REF:
6739         case REALPART_EXPR:
6740         case IMAGPART_EXPR:
6741         case VIEW_CONVERT_EXPR:
6742           break;
6743
6744         default:
6745           goto done;
6746         }
6747       exp = TREE_OPERAND (exp, 0);
6748     }
6749  done:
6750   return packed_p;
6751 }
6752
6753 /* Return a tree of sizetype representing the size, in bytes, of the element
6754    of EXP, an ARRAY_REF or an ARRAY_RANGE_REF.  */
6755
6756 tree
6757 array_ref_element_size (tree exp)
6758 {
6759   tree aligned_size = TREE_OPERAND (exp, 3);
6760   tree elmt_type = TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0)));
6761   location_t loc = EXPR_LOCATION (exp);
6762
6763   /* If a size was specified in the ARRAY_REF, it's the size measured
6764      in alignment units of the element type.  So multiply by that value.  */
6765   if (aligned_size)
6766     {
6767       /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6768          sizetype from another type of the same width and signedness.  */
6769       if (TREE_TYPE (aligned_size) != sizetype)
6770         aligned_size = fold_convert_loc (loc, sizetype, aligned_size);
6771       return size_binop_loc (loc, MULT_EXPR, aligned_size,
6772                              size_int (TYPE_ALIGN_UNIT (elmt_type)));
6773     }
6774
6775   /* Otherwise, take the size from that of the element type.  Substitute
6776      any PLACEHOLDER_EXPR that we have.  */
6777   else
6778     return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_SIZE_UNIT (elmt_type), exp);
6779 }
6780
6781 /* Return a tree representing the lower bound of the array mentioned in
6782    EXP, an ARRAY_REF or an ARRAY_RANGE_REF.  */
6783
6784 tree
6785 array_ref_low_bound (tree exp)
6786 {
6787   tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6788
6789   /* If a lower bound is specified in EXP, use it.  */
6790   if (TREE_OPERAND (exp, 2))
6791     return TREE_OPERAND (exp, 2);
6792
6793   /* Otherwise, if there is a domain type and it has a lower bound, use it,
6794      substituting for a PLACEHOLDER_EXPR as needed.  */
6795   if (domain_type && TYPE_MIN_VALUE (domain_type))
6796     return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MIN_VALUE (domain_type), exp);
6797
6798   /* Otherwise, return a zero of the appropriate type.  */
6799   return build_int_cst (TREE_TYPE (TREE_OPERAND (exp, 1)), 0);
6800 }
6801
6802 /* Returns true if REF is an array reference to an array at the end of
6803    a structure.  If this is the case, the array may be allocated larger
6804    than its upper bound implies.  */
6805
6806 bool
6807 array_at_struct_end_p (tree ref)
6808 {
6809   if (TREE_CODE (ref) != ARRAY_REF
6810       && TREE_CODE (ref) != ARRAY_RANGE_REF)
6811     return false;
6812
6813   while (handled_component_p (ref))
6814     {
6815       /* If the reference chain contains a component reference to a
6816          non-union type and there follows another field the reference
6817          is not at the end of a structure.  */
6818       if (TREE_CODE (ref) == COMPONENT_REF
6819           && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 0))) == RECORD_TYPE)
6820         {
6821           tree nextf = DECL_CHAIN (TREE_OPERAND (ref, 1));
6822           while (nextf && TREE_CODE (nextf) != FIELD_DECL)
6823             nextf = DECL_CHAIN (nextf);
6824           if (nextf)
6825             return false;
6826         }
6827
6828       ref = TREE_OPERAND (ref, 0);
6829     }
6830
6831   /* If the reference is based on a declared entity, the size of the array
6832      is constrained by its given domain.  */
6833   if (DECL_P (ref))
6834     return false;
6835
6836   return true;
6837 }
6838
6839 /* Return a tree representing the upper bound of the array mentioned in
6840    EXP, an ARRAY_REF or an ARRAY_RANGE_REF.  */
6841
6842 tree
6843 array_ref_up_bound (tree exp)
6844 {
6845   tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (exp, 0)));
6846
6847   /* If there is a domain type and it has an upper bound, use it, substituting
6848      for a PLACEHOLDER_EXPR as needed.  */
6849   if (domain_type && TYPE_MAX_VALUE (domain_type))
6850     return SUBSTITUTE_PLACEHOLDER_IN_EXPR (TYPE_MAX_VALUE (domain_type), exp);
6851
6852   /* Otherwise fail.  */
6853   return NULL_TREE;
6854 }
6855
6856 /* Return a tree representing the offset, in bytes, of the field referenced
6857    by EXP.  This does not include any offset in DECL_FIELD_BIT_OFFSET.  */
6858
6859 tree
6860 component_ref_field_offset (tree exp)
6861 {
6862   tree aligned_offset = TREE_OPERAND (exp, 2);
6863   tree field = TREE_OPERAND (exp, 1);
6864   location_t loc = EXPR_LOCATION (exp);
6865
6866   /* If an offset was specified in the COMPONENT_REF, it's the offset measured
6867      in units of DECL_OFFSET_ALIGN / BITS_PER_UNIT.  So multiply by that
6868      value.  */
6869   if (aligned_offset)
6870     {
6871       /* ??? tree_ssa_useless_type_conversion will eliminate casts to
6872          sizetype from another type of the same width and signedness.  */
6873       if (TREE_TYPE (aligned_offset) != sizetype)
6874         aligned_offset = fold_convert_loc (loc, sizetype, aligned_offset);
6875       return size_binop_loc (loc, MULT_EXPR, aligned_offset,
6876                              size_int (DECL_OFFSET_ALIGN (field)
6877                                        / BITS_PER_UNIT));
6878     }
6879
6880   /* Otherwise, take the offset from that of the field.  Substitute
6881      any PLACEHOLDER_EXPR that we have.  */
6882   else
6883     return SUBSTITUTE_PLACEHOLDER_IN_EXPR (DECL_FIELD_OFFSET (field), exp);
6884 }
6885
6886 /* Alignment in bits the TARGET of an assignment may be assumed to have.  */
6887
6888 static unsigned HOST_WIDE_INT
6889 target_align (const_tree target)
6890 {
6891   /* We might have a chain of nested references with intermediate misaligning
6892      bitfields components, so need to recurse to find out.  */
6893
6894   unsigned HOST_WIDE_INT this_align, outer_align;
6895
6896   switch (TREE_CODE (target))
6897     {
6898     case BIT_FIELD_REF:
6899       return 1;
6900
6901     case COMPONENT_REF:
6902       this_align = DECL_ALIGN (TREE_OPERAND (target, 1));
6903       outer_align = target_align (TREE_OPERAND (target, 0));
6904       return MIN (this_align, outer_align);
6905
6906     case ARRAY_REF:
6907     case ARRAY_RANGE_REF:
6908       this_align = TYPE_ALIGN (TREE_TYPE (target));
6909       outer_align = target_align (TREE_OPERAND (target, 0));
6910       return MIN (this_align, outer_align);
6911
6912     CASE_CONVERT:
6913     case NON_LVALUE_EXPR:
6914     case VIEW_CONVERT_EXPR:
6915       this_align = TYPE_ALIGN (TREE_TYPE (target));
6916       outer_align = target_align (TREE_OPERAND (target, 0));
6917       return MAX (this_align, outer_align);
6918
6919     default:
6920       return TYPE_ALIGN (TREE_TYPE (target));
6921     }
6922 }
6923
6924 \f
6925 /* Given an rtx VALUE that may contain additions and multiplications, return
6926    an equivalent value that just refers to a register, memory, or constant.
6927    This is done by generating instructions to perform the arithmetic and
6928    returning a pseudo-register containing the value.
6929
6930    The returned value may be a REG, SUBREG, MEM or constant.  */
6931
6932 rtx
6933 force_operand (rtx value, rtx target)
6934 {
6935   rtx op1, op2;
6936   /* Use subtarget as the target for operand 0 of a binary operation.  */
6937   rtx subtarget = get_subtarget (target);
6938   enum rtx_code code = GET_CODE (value);
6939
6940   /* Check for subreg applied to an expression produced by loop optimizer.  */
6941   if (code == SUBREG
6942       && !REG_P (SUBREG_REG (value))
6943       && !MEM_P (SUBREG_REG (value)))
6944     {
6945       value
6946         = simplify_gen_subreg (GET_MODE (value),
6947                                force_reg (GET_MODE (SUBREG_REG (value)),
6948                                           force_operand (SUBREG_REG (value),
6949                                                          NULL_RTX)),
6950                                GET_MODE (SUBREG_REG (value)),
6951                                SUBREG_BYTE (value));
6952       code = GET_CODE (value);
6953     }
6954
6955   /* Check for a PIC address load.  */
6956   if ((code == PLUS || code == MINUS)
6957       && XEXP (value, 0) == pic_offset_table_rtx
6958       && (GET_CODE (XEXP (value, 1)) == SYMBOL_REF
6959           || GET_CODE (XEXP (value, 1)) == LABEL_REF
6960           || GET_CODE (XEXP (value, 1)) == CONST))
6961     {
6962       if (!subtarget)
6963         subtarget = gen_reg_rtx (GET_MODE (value));
6964       emit_move_insn (subtarget, value);
6965       return subtarget;
6966     }
6967
6968   if (ARITHMETIC_P (value))
6969     {
6970       op2 = XEXP (value, 1);
6971       if (!CONSTANT_P (op2) && !(REG_P (op2) && op2 != subtarget))
6972         subtarget = 0;
6973       if (code == MINUS && CONST_INT_P (op2))
6974         {
6975           code = PLUS;
6976           op2 = negate_rtx (GET_MODE (value), op2);
6977         }
6978
6979       /* Check for an addition with OP2 a constant integer and our first
6980          operand a PLUS of a virtual register and something else.  In that
6981          case, we want to emit the sum of the virtual register and the
6982          constant first and then add the other value.  This allows virtual
6983          register instantiation to simply modify the constant rather than
6984          creating another one around this addition.  */
6985       if (code == PLUS && CONST_INT_P (op2)
6986           && GET_CODE (XEXP (value, 0)) == PLUS
6987           && REG_P (XEXP (XEXP (value, 0), 0))
6988           && REGNO (XEXP (XEXP (value, 0), 0)) >= FIRST_VIRTUAL_REGISTER
6989           && REGNO (XEXP (XEXP (value, 0), 0)) <= LAST_VIRTUAL_REGISTER)
6990         {
6991           rtx temp = expand_simple_binop (GET_MODE (value), code,
6992                                           XEXP (XEXP (value, 0), 0), op2,
6993                                           subtarget, 0, OPTAB_LIB_WIDEN);
6994           return expand_simple_binop (GET_MODE (value), code, temp,
6995                                       force_operand (XEXP (XEXP (value,
6996                                                                  0), 1), 0),
6997                                       target, 0, OPTAB_LIB_WIDEN);
6998         }
6999
7000       op1 = force_operand (XEXP (value, 0), subtarget);
7001       op2 = force_operand (op2, NULL_RTX);
7002       switch (code)
7003         {
7004         case MULT:
7005           return expand_mult (GET_MODE (value), op1, op2, target, 1);
7006         case DIV:
7007           if (!INTEGRAL_MODE_P (GET_MODE (value)))
7008             return expand_simple_binop (GET_MODE (value), code, op1, op2,
7009                                         target, 1, OPTAB_LIB_WIDEN);
7010           else
7011             return expand_divmod (0,
7012                                   FLOAT_MODE_P (GET_MODE (value))
7013                                   ? RDIV_EXPR : TRUNC_DIV_EXPR,
7014                                   GET_MODE (value), op1, op2, target, 0);
7015         case MOD:
7016           return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7017                                 target, 0);
7018         case UDIV:
7019           return expand_divmod (0, TRUNC_DIV_EXPR, GET_MODE (value), op1, op2,
7020                                 target, 1);
7021         case UMOD:
7022           return expand_divmod (1, TRUNC_MOD_EXPR, GET_MODE (value), op1, op2,
7023                                 target, 1);
7024         case ASHIFTRT:
7025           return expand_simple_binop (GET_MODE (value), code, op1, op2,
7026                                       target, 0, OPTAB_LIB_WIDEN);
7027         default:
7028           return expand_simple_binop (GET_MODE (value), code, op1, op2,
7029                                       target, 1, OPTAB_LIB_WIDEN);
7030         }
7031     }
7032   if (UNARY_P (value))
7033     {
7034       if (!target)
7035         target = gen_reg_rtx (GET_MODE (value));
7036       op1 = force_operand (XEXP (value, 0), NULL_RTX);
7037       switch (code)
7038         {
7039         case ZERO_EXTEND:
7040         case SIGN_EXTEND:
7041         case TRUNCATE:
7042         case FLOAT_EXTEND:
7043         case FLOAT_TRUNCATE:
7044           convert_move (target, op1, code == ZERO_EXTEND);
7045           return target;
7046
7047         case FIX:
7048         case UNSIGNED_FIX:
7049           expand_fix (target, op1, code == UNSIGNED_FIX);
7050           return target;
7051
7052         case FLOAT:
7053         case UNSIGNED_FLOAT:
7054           expand_float (target, op1, code == UNSIGNED_FLOAT);
7055           return target;
7056
7057         default:
7058           return expand_simple_unop (GET_MODE (value), code, op1, target, 0);
7059         }
7060     }
7061
7062 #ifdef INSN_SCHEDULING
7063   /* On machines that have insn scheduling, we want all memory reference to be
7064      explicit, so we need to deal with such paradoxical SUBREGs.  */
7065   if (paradoxical_subreg_p (value) && MEM_P (SUBREG_REG (value)))
7066     value
7067       = simplify_gen_subreg (GET_MODE (value),
7068                              force_reg (GET_MODE (SUBREG_REG (value)),
7069                                         force_operand (SUBREG_REG (value),
7070                                                        NULL_RTX)),
7071                              GET_MODE (SUBREG_REG (value)),
7072                              SUBREG_BYTE (value));
7073 #endif
7074
7075   return value;
7076 }
7077 \f
7078 /* Subroutine of expand_expr: return nonzero iff there is no way that
7079    EXP can reference X, which is being modified.  TOP_P is nonzero if this
7080    call is going to be used to determine whether we need a temporary
7081    for EXP, as opposed to a recursive call to this function.
7082
7083    It is always safe for this routine to return zero since it merely
7084    searches for optimization opportunities.  */
7085
7086 int
7087 safe_from_p (const_rtx x, tree exp, int top_p)
7088 {
7089   rtx exp_rtl = 0;
7090   int i, nops;
7091
7092   if (x == 0
7093       /* If EXP has varying size, we MUST use a target since we currently
7094          have no way of allocating temporaries of variable size
7095          (except for arrays that have TYPE_ARRAY_MAX_SIZE set).
7096          So we assume here that something at a higher level has prevented a
7097          clash.  This is somewhat bogus, but the best we can do.  Only
7098          do this when X is BLKmode and when we are at the top level.  */
7099       || (top_p && TREE_TYPE (exp) != 0 && COMPLETE_TYPE_P (TREE_TYPE (exp))
7100           && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) != INTEGER_CST
7101           && (TREE_CODE (TREE_TYPE (exp)) != ARRAY_TYPE
7102               || TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)) == NULL_TREE
7103               || TREE_CODE (TYPE_ARRAY_MAX_SIZE (TREE_TYPE (exp)))
7104               != INTEGER_CST)
7105           && GET_MODE (x) == BLKmode)
7106       /* If X is in the outgoing argument area, it is always safe.  */
7107       || (MEM_P (x)
7108           && (XEXP (x, 0) == virtual_outgoing_args_rtx
7109               || (GET_CODE (XEXP (x, 0)) == PLUS
7110                   && XEXP (XEXP (x, 0), 0) == virtual_outgoing_args_rtx))))
7111     return 1;
7112
7113   /* If this is a subreg of a hard register, declare it unsafe, otherwise,
7114      find the underlying pseudo.  */
7115   if (GET_CODE (x) == SUBREG)
7116     {
7117       x = SUBREG_REG (x);
7118       if (REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7119         return 0;
7120     }
7121
7122   /* Now look at our tree code and possibly recurse.  */
7123   switch (TREE_CODE_CLASS (TREE_CODE (exp)))
7124     {
7125     case tcc_declaration:
7126       exp_rtl = DECL_RTL_IF_SET (exp);
7127       break;
7128
7129     case tcc_constant:
7130       return 1;
7131
7132     case tcc_exceptional:
7133       if (TREE_CODE (exp) == TREE_LIST)
7134         {
7135           while (1)
7136             {
7137               if (TREE_VALUE (exp) && !safe_from_p (x, TREE_VALUE (exp), 0))
7138                 return 0;
7139               exp = TREE_CHAIN (exp);
7140               if (!exp)
7141                 return 1;
7142               if (TREE_CODE (exp) != TREE_LIST)
7143                 return safe_from_p (x, exp, 0);
7144             }
7145         }
7146       else if (TREE_CODE (exp) == CONSTRUCTOR)
7147         {
7148           constructor_elt *ce;
7149           unsigned HOST_WIDE_INT idx;
7150
7151           FOR_EACH_VEC_ELT (constructor_elt, CONSTRUCTOR_ELTS (exp), idx, ce)
7152             if ((ce->index != NULL_TREE && !safe_from_p (x, ce->index, 0))
7153                 || !safe_from_p (x, ce->value, 0))
7154               return 0;
7155           return 1;
7156         }
7157       else if (TREE_CODE (exp) == ERROR_MARK)
7158         return 1;       /* An already-visited SAVE_EXPR? */
7159       else
7160         return 0;
7161
7162     case tcc_statement:
7163       /* The only case we look at here is the DECL_INITIAL inside a
7164          DECL_EXPR.  */
7165       return (TREE_CODE (exp) != DECL_EXPR
7166               || TREE_CODE (DECL_EXPR_DECL (exp)) != VAR_DECL
7167               || !DECL_INITIAL (DECL_EXPR_DECL (exp))
7168               || safe_from_p (x, DECL_INITIAL (DECL_EXPR_DECL (exp)), 0));
7169
7170     case tcc_binary:
7171     case tcc_comparison:
7172       if (!safe_from_p (x, TREE_OPERAND (exp, 1), 0))
7173         return 0;
7174       /* Fall through.  */
7175
7176     case tcc_unary:
7177       return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7178
7179     case tcc_expression:
7180     case tcc_reference:
7181     case tcc_vl_exp:
7182       /* Now do code-specific tests.  EXP_RTL is set to any rtx we find in
7183          the expression.  If it is set, we conflict iff we are that rtx or
7184          both are in memory.  Otherwise, we check all operands of the
7185          expression recursively.  */
7186
7187       switch (TREE_CODE (exp))
7188         {
7189         case ADDR_EXPR:
7190           /* If the operand is static or we are static, we can't conflict.
7191              Likewise if we don't conflict with the operand at all.  */
7192           if (staticp (TREE_OPERAND (exp, 0))
7193               || TREE_STATIC (exp)
7194               || safe_from_p (x, TREE_OPERAND (exp, 0), 0))
7195             return 1;
7196
7197           /* Otherwise, the only way this can conflict is if we are taking
7198              the address of a DECL a that address if part of X, which is
7199              very rare.  */
7200           exp = TREE_OPERAND (exp, 0);
7201           if (DECL_P (exp))
7202             {
7203               if (!DECL_RTL_SET_P (exp)
7204                   || !MEM_P (DECL_RTL (exp)))
7205                 return 0;
7206               else
7207                 exp_rtl = XEXP (DECL_RTL (exp), 0);
7208             }
7209           break;
7210
7211         case MEM_REF:
7212           if (MEM_P (x)
7213               && alias_sets_conflict_p (MEM_ALIAS_SET (x),
7214                                         get_alias_set (exp)))
7215             return 0;
7216           break;
7217
7218         case CALL_EXPR:
7219           /* Assume that the call will clobber all hard registers and
7220              all of memory.  */
7221           if ((REG_P (x) && REGNO (x) < FIRST_PSEUDO_REGISTER)
7222               || MEM_P (x))
7223             return 0;
7224           break;
7225
7226         case WITH_CLEANUP_EXPR:
7227         case CLEANUP_POINT_EXPR:
7228           /* Lowered by gimplify.c.  */
7229           gcc_unreachable ();
7230
7231         case SAVE_EXPR:
7232           return safe_from_p (x, TREE_OPERAND (exp, 0), 0);
7233
7234         default:
7235           break;
7236         }
7237
7238       /* If we have an rtx, we do not need to scan our operands.  */
7239       if (exp_rtl)
7240         break;
7241
7242       nops = TREE_OPERAND_LENGTH (exp);
7243       for (i = 0; i < nops; i++)
7244         if (TREE_OPERAND (exp, i) != 0
7245             && ! safe_from_p (x, TREE_OPERAND (exp, i), 0))
7246           return 0;
7247
7248       break;
7249
7250     case tcc_type:
7251       /* Should never get a type here.  */
7252       gcc_unreachable ();
7253     }
7254
7255   /* If we have an rtl, find any enclosed object.  Then see if we conflict
7256      with it.  */
7257   if (exp_rtl)
7258     {
7259       if (GET_CODE (exp_rtl) == SUBREG)
7260         {
7261           exp_rtl = SUBREG_REG (exp_rtl);
7262           if (REG_P (exp_rtl)
7263               && REGNO (exp_rtl) < FIRST_PSEUDO_REGISTER)
7264             return 0;
7265         }
7266
7267       /* If the rtl is X, then it is not safe.  Otherwise, it is unless both
7268          are memory and they conflict.  */
7269       return ! (rtx_equal_p (x, exp_rtl)
7270                 || (MEM_P (x) && MEM_P (exp_rtl)
7271                     && true_dependence (exp_rtl, VOIDmode, x)));
7272     }
7273
7274   /* If we reach here, it is safe.  */
7275   return 1;
7276 }
7277
7278 \f
7279 /* Return the highest power of two that EXP is known to be a multiple of.
7280    This is used in updating alignment of MEMs in array references.  */
7281
7282 unsigned HOST_WIDE_INT
7283 highest_pow2_factor (const_tree exp)
7284 {
7285   unsigned HOST_WIDE_INT c0, c1;
7286
7287   switch (TREE_CODE (exp))
7288     {
7289     case INTEGER_CST:
7290       /* We can find the lowest bit that's a one.  If the low
7291          HOST_BITS_PER_WIDE_INT bits are zero, return BIGGEST_ALIGNMENT.
7292          We need to handle this case since we can find it in a COND_EXPR,
7293          a MIN_EXPR, or a MAX_EXPR.  If the constant overflows, we have an
7294          erroneous program, so return BIGGEST_ALIGNMENT to avoid any
7295          later ICE.  */
7296       if (TREE_OVERFLOW (exp))
7297         return BIGGEST_ALIGNMENT;
7298       else
7299         {
7300           /* Note: tree_low_cst is intentionally not used here,
7301              we don't care about the upper bits.  */
7302           c0 = TREE_INT_CST_LOW (exp);
7303           c0 &= -c0;
7304           return c0 ? c0 : BIGGEST_ALIGNMENT;
7305         }
7306       break;
7307
7308     case PLUS_EXPR:  case MINUS_EXPR:  case MIN_EXPR:  case MAX_EXPR:
7309       c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
7310       c1 = highest_pow2_factor (TREE_OPERAND (exp, 1));
7311       return MIN (c0, c1);
7312
7313     case MULT_EXPR:
7314       c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
7315       c1 = highest_pow2_factor (TREE_OPERAND (exp, 1));
7316       return c0 * c1;
7317
7318     case ROUND_DIV_EXPR:  case TRUNC_DIV_EXPR:  case FLOOR_DIV_EXPR:
7319     case CEIL_DIV_EXPR:
7320       if (integer_pow2p (TREE_OPERAND (exp, 1))
7321           && host_integerp (TREE_OPERAND (exp, 1), 1))
7322         {
7323           c0 = highest_pow2_factor (TREE_OPERAND (exp, 0));
7324           c1 = tree_low_cst (TREE_OPERAND (exp, 1), 1);
7325           return MAX (1, c0 / c1);
7326         }
7327       break;
7328
7329     case BIT_AND_EXPR:
7330       /* The highest power of two of a bit-and expression is the maximum of
7331          that of its operands.  We typically get here for a complex LHS and
7332          a constant negative power of two on the RHS to force an explicit
7333          alignment, so don't bother looking at the LHS.  */
7334       return highest_pow2_factor (TREE_OPERAND (exp, 1));
7335
7336     CASE_CONVERT:
7337     case SAVE_EXPR:
7338       return highest_pow2_factor (TREE_OPERAND (exp, 0));
7339
7340     case COMPOUND_EXPR:
7341       return highest_pow2_factor (TREE_OPERAND (exp, 1));
7342
7343     case COND_EXPR:
7344       c0 = highest_pow2_factor (TREE_OPERAND (exp, 1));
7345       c1 = highest_pow2_factor (TREE_OPERAND (exp, 2));
7346       return MIN (c0, c1);
7347
7348     default:
7349       break;
7350     }
7351
7352   return 1;
7353 }
7354
7355 /* Similar, except that the alignment requirements of TARGET are
7356    taken into account.  Assume it is at least as aligned as its
7357    type, unless it is a COMPONENT_REF in which case the layout of
7358    the structure gives the alignment.  */
7359
7360 static unsigned HOST_WIDE_INT
7361 highest_pow2_factor_for_target (const_tree target, const_tree exp)
7362 {
7363   unsigned HOST_WIDE_INT talign = target_align (target) / BITS_PER_UNIT;
7364   unsigned HOST_WIDE_INT factor = highest_pow2_factor (exp);
7365
7366   return MAX (factor, talign);
7367 }
7368 \f
7369 /* Convert the tree comparision code TCODE to the rtl one where the
7370    signedness is UNSIGNEDP.  */
7371
7372 static enum rtx_code
7373 convert_tree_comp_to_rtx (enum tree_code tcode, int unsignedp)
7374 {
7375   enum rtx_code code;
7376   switch (tcode)
7377     {
7378     case EQ_EXPR:
7379       code = EQ;
7380       break;
7381     case NE_EXPR:
7382       code = NE;
7383       break;
7384     case LT_EXPR:
7385       code = unsignedp ? LTU : LT;
7386       break;
7387     case LE_EXPR:
7388       code = unsignedp ? LEU : LE;
7389       break;
7390     case GT_EXPR:
7391       code = unsignedp ? GTU : GT;
7392       break;
7393     case GE_EXPR:
7394       code = unsignedp ? GEU : GE;
7395       break;
7396     case UNORDERED_EXPR:
7397       code = UNORDERED;
7398       break;
7399     case ORDERED_EXPR:
7400       code = ORDERED;
7401       break;
7402     case UNLT_EXPR:
7403       code = UNLT;
7404       break;
7405     case UNLE_EXPR:
7406       code = UNLE;
7407       break;
7408     case UNGT_EXPR:
7409       code = UNGT;
7410       break;
7411     case UNGE_EXPR:
7412       code = UNGE;
7413       break;
7414     case UNEQ_EXPR:
7415       code = UNEQ;
7416       break;
7417     case LTGT_EXPR:
7418       code = LTGT;
7419       break;
7420
7421     default:
7422       gcc_unreachable ();
7423     }
7424   return code;
7425 }
7426
7427 /* Subroutine of expand_expr.  Expand the two operands of a binary
7428    expression EXP0 and EXP1 placing the results in OP0 and OP1.
7429    The value may be stored in TARGET if TARGET is nonzero.  The
7430    MODIFIER argument is as documented by expand_expr.  */
7431
7432 static void
7433 expand_operands (tree exp0, tree exp1, rtx target, rtx *op0, rtx *op1,
7434                  enum expand_modifier modifier)
7435 {
7436   if (! safe_from_p (target, exp1, 1))
7437     target = 0;
7438   if (operand_equal_p (exp0, exp1, 0))
7439     {
7440       *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7441       *op1 = copy_rtx (*op0);
7442     }
7443   else
7444     {
7445       /* If we need to preserve evaluation order, copy exp0 into its own
7446          temporary variable so that it can't be clobbered by exp1.  */
7447       if (flag_evaluation_order && TREE_SIDE_EFFECTS (exp1))
7448         exp0 = save_expr (exp0);
7449       *op0 = expand_expr (exp0, target, VOIDmode, modifier);
7450       *op1 = expand_expr (exp1, NULL_RTX, VOIDmode, modifier);
7451     }
7452 }
7453
7454 \f
7455 /* Return a MEM that contains constant EXP.  DEFER is as for
7456    output_constant_def and MODIFIER is as for expand_expr.  */
7457
7458 static rtx
7459 expand_expr_constant (tree exp, int defer, enum expand_modifier modifier)
7460 {
7461   rtx mem;
7462
7463   mem = output_constant_def (exp, defer);
7464   if (modifier != EXPAND_INITIALIZER)
7465     mem = use_anchored_address (mem);
7466   return mem;
7467 }
7468
7469 /* A subroutine of expand_expr_addr_expr.  Evaluate the address of EXP.
7470    The TARGET, TMODE and MODIFIER arguments are as for expand_expr.  */
7471
7472 static rtx
7473 expand_expr_addr_expr_1 (tree exp, rtx target, enum machine_mode tmode,
7474                          enum expand_modifier modifier, addr_space_t as)
7475 {
7476   rtx result, subtarget;
7477   tree inner, offset;
7478   HOST_WIDE_INT bitsize, bitpos;
7479   int volatilep, unsignedp;
7480   enum machine_mode mode1;
7481
7482   /* If we are taking the address of a constant and are at the top level,
7483      we have to use output_constant_def since we can't call force_const_mem
7484      at top level.  */
7485   /* ??? This should be considered a front-end bug.  We should not be
7486      generating ADDR_EXPR of something that isn't an LVALUE.  The only
7487      exception here is STRING_CST.  */
7488   if (CONSTANT_CLASS_P (exp))
7489     {
7490       result = XEXP (expand_expr_constant (exp, 0, modifier), 0);
7491       if (modifier < EXPAND_SUM)
7492         result = force_operand (result, target);
7493       return result;
7494     }
7495
7496   /* Everything must be something allowed by is_gimple_addressable.  */
7497   switch (TREE_CODE (exp))
7498     {
7499     case INDIRECT_REF:
7500       /* This case will happen via recursion for &a->b.  */
7501       return expand_expr (TREE_OPERAND (exp, 0), target, tmode, modifier);
7502
7503     case MEM_REF:
7504       {
7505         tree tem = TREE_OPERAND (exp, 0);
7506         if (!integer_zerop (TREE_OPERAND (exp, 1)))
7507           tem = fold_build_pointer_plus (tem, TREE_OPERAND (exp, 1));
7508         return expand_expr (tem, target, tmode, modifier);
7509       }
7510
7511     case CONST_DECL:
7512       /* Expand the initializer like constants above.  */
7513       result = XEXP (expand_expr_constant (DECL_INITIAL (exp),
7514                                            0, modifier), 0);
7515       if (modifier < EXPAND_SUM)
7516         result = force_operand (result, target);
7517       return result;
7518
7519     case REALPART_EXPR:
7520       /* The real part of the complex number is always first, therefore
7521          the address is the same as the address of the parent object.  */
7522       offset = 0;
7523       bitpos = 0;
7524       inner = TREE_OPERAND (exp, 0);
7525       break;
7526
7527     case IMAGPART_EXPR:
7528       /* The imaginary part of the complex number is always second.
7529          The expression is therefore always offset by the size of the
7530          scalar type.  */
7531       offset = 0;
7532       bitpos = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (exp)));
7533       inner = TREE_OPERAND (exp, 0);
7534       break;
7535
7536     default:
7537       /* If the object is a DECL, then expand it for its rtl.  Don't bypass
7538          expand_expr, as that can have various side effects; LABEL_DECLs for
7539          example, may not have their DECL_RTL set yet.  Expand the rtl of
7540          CONSTRUCTORs too, which should yield a memory reference for the
7541          constructor's contents.  Assume language specific tree nodes can
7542          be expanded in some interesting way.  */
7543       gcc_assert (TREE_CODE (exp) < LAST_AND_UNUSED_TREE_CODE);
7544       if (DECL_P (exp)
7545           || TREE_CODE (exp) == CONSTRUCTOR
7546           || TREE_CODE (exp) == COMPOUND_LITERAL_EXPR)
7547         {
7548           result = expand_expr (exp, target, tmode,
7549                                 modifier == EXPAND_INITIALIZER
7550                                 ? EXPAND_INITIALIZER : EXPAND_CONST_ADDRESS);
7551
7552           /* If the DECL isn't in memory, then the DECL wasn't properly
7553              marked TREE_ADDRESSABLE, which will be either a front-end
7554              or a tree optimizer bug.  */
7555
7556           if (TREE_ADDRESSABLE (exp)
7557               && ! MEM_P (result)
7558               && ! targetm.calls.allocate_stack_slots_for_args())
7559             {
7560               error ("local frame unavailable (naked function?)");
7561               return result;
7562             }
7563           else
7564             gcc_assert (MEM_P (result));
7565           result = XEXP (result, 0);
7566
7567           /* ??? Is this needed anymore?  */
7568           if (DECL_P (exp))
7569             TREE_USED (exp) = 1;
7570
7571           if (modifier != EXPAND_INITIALIZER
7572               && modifier != EXPAND_CONST_ADDRESS
7573               && modifier != EXPAND_SUM)
7574             result = force_operand (result, target);
7575           return result;
7576         }
7577
7578       /* Pass FALSE as the last argument to get_inner_reference although
7579          we are expanding to RTL.  The rationale is that we know how to
7580          handle "aligning nodes" here: we can just bypass them because
7581          they won't change the final object whose address will be returned
7582          (they actually exist only for that purpose).  */
7583       inner = get_inner_reference (exp, &bitsize, &bitpos, &offset,
7584                                    &mode1, &unsignedp, &volatilep, false);
7585       break;
7586     }
7587
7588   /* We must have made progress.  */
7589   gcc_assert (inner != exp);
7590
7591   subtarget = offset || bitpos ? NULL_RTX : target;
7592   /* For VIEW_CONVERT_EXPR, where the outer alignment is bigger than
7593      inner alignment, force the inner to be sufficiently aligned.  */
7594   if (CONSTANT_CLASS_P (inner)
7595       && TYPE_ALIGN (TREE_TYPE (inner)) < TYPE_ALIGN (TREE_TYPE (exp)))
7596     {
7597       inner = copy_node (inner);
7598       TREE_TYPE (inner) = copy_node (TREE_TYPE (inner));
7599       TYPE_ALIGN (TREE_TYPE (inner)) = TYPE_ALIGN (TREE_TYPE (exp));
7600       TYPE_USER_ALIGN (TREE_TYPE (inner)) = 1;
7601     }
7602   result = expand_expr_addr_expr_1 (inner, subtarget, tmode, modifier, as);
7603
7604   if (offset)
7605     {
7606       rtx tmp;
7607
7608       if (modifier != EXPAND_NORMAL)
7609         result = force_operand (result, NULL);
7610       tmp = expand_expr (offset, NULL_RTX, tmode,
7611                          modifier == EXPAND_INITIALIZER
7612                           ? EXPAND_INITIALIZER : EXPAND_NORMAL);
7613
7614       result = convert_memory_address_addr_space (tmode, result, as);
7615       tmp = convert_memory_address_addr_space (tmode, tmp, as);
7616
7617       if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
7618         result = simplify_gen_binary (PLUS, tmode, result, tmp);
7619       else
7620         {
7621           subtarget = bitpos ? NULL_RTX : target;
7622           result = expand_simple_binop (tmode, PLUS, result, tmp, subtarget,
7623                                         1, OPTAB_LIB_WIDEN);
7624         }
7625     }
7626
7627   if (bitpos)
7628     {
7629       /* Someone beforehand should have rejected taking the address
7630          of such an object.  */
7631       gcc_assert ((bitpos % BITS_PER_UNIT) == 0);
7632
7633       result = plus_constant (tmode, result, bitpos / BITS_PER_UNIT);
7634       if (modifier < EXPAND_SUM)
7635         result = force_operand (result, target);
7636     }
7637
7638   return result;
7639 }
7640
7641 /* A subroutine of expand_expr.  Evaluate EXP, which is an ADDR_EXPR.
7642    The TARGET, TMODE and MODIFIER arguments are as for expand_expr.  */
7643
7644 static rtx
7645 expand_expr_addr_expr (tree exp, rtx target, enum machine_mode tmode,
7646                        enum expand_modifier modifier)
7647 {
7648   addr_space_t as = ADDR_SPACE_GENERIC;
7649   enum machine_mode address_mode = Pmode;
7650   enum machine_mode pointer_mode = ptr_mode;
7651   enum machine_mode rmode;
7652   rtx result;
7653
7654   /* Target mode of VOIDmode says "whatever's natural".  */
7655   if (tmode == VOIDmode)
7656     tmode = TYPE_MODE (TREE_TYPE (exp));
7657
7658   if (POINTER_TYPE_P (TREE_TYPE (exp)))
7659     {
7660       as = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (exp)));
7661       address_mode = targetm.addr_space.address_mode (as);
7662       pointer_mode = targetm.addr_space.pointer_mode (as);
7663     }
7664
7665   /* We can get called with some Weird Things if the user does silliness
7666      like "(short) &a".  In that case, convert_memory_address won't do
7667      the right thing, so ignore the given target mode.  */
7668   if (tmode != address_mode && tmode != pointer_mode)
7669     tmode = address_mode;
7670
7671   result = expand_expr_addr_expr_1 (TREE_OPERAND (exp, 0), target,
7672                                     tmode, modifier, as);
7673
7674   /* Despite expand_expr claims concerning ignoring TMODE when not
7675      strictly convenient, stuff breaks if we don't honor it.  Note
7676      that combined with the above, we only do this for pointer modes.  */
7677   rmode = GET_MODE (result);
7678   if (rmode == VOIDmode)
7679     rmode = tmode;
7680   if (rmode != tmode)
7681     result = convert_memory_address_addr_space (tmode, result, as);
7682
7683   return result;
7684 }
7685
7686 /* Generate code for computing CONSTRUCTOR EXP.
7687    An rtx for the computed value is returned.  If AVOID_TEMP_MEM
7688    is TRUE, instead of creating a temporary variable in memory
7689    NULL is returned and the caller needs to handle it differently.  */
7690
7691 static rtx
7692 expand_constructor (tree exp, rtx target, enum expand_modifier modifier,
7693                     bool avoid_temp_mem)
7694 {
7695   tree type = TREE_TYPE (exp);
7696   enum machine_mode mode = TYPE_MODE (type);
7697
7698   /* Try to avoid creating a temporary at all.  This is possible
7699      if all of the initializer is zero.
7700      FIXME: try to handle all [0..255] initializers we can handle
7701      with memset.  */
7702   if (TREE_STATIC (exp)
7703       && !TREE_ADDRESSABLE (exp)
7704       && target != 0 && mode == BLKmode
7705       && all_zeros_p (exp))
7706     {
7707       clear_storage (target, expr_size (exp), BLOCK_OP_NORMAL);
7708       return target;
7709     }
7710
7711   /* All elts simple constants => refer to a constant in memory.  But
7712      if this is a non-BLKmode mode, let it store a field at a time
7713      since that should make a CONST_INT or CONST_DOUBLE when we
7714      fold.  Likewise, if we have a target we can use, it is best to
7715      store directly into the target unless the type is large enough
7716      that memcpy will be used.  If we are making an initializer and
7717      all operands are constant, put it in memory as well.
7718
7719      FIXME: Avoid trying to fill vector constructors piece-meal.
7720      Output them with output_constant_def below unless we're sure
7721      they're zeros.  This should go away when vector initializers
7722      are treated like VECTOR_CST instead of arrays.  */
7723   if ((TREE_STATIC (exp)
7724        && ((mode == BLKmode
7725             && ! (target != 0 && safe_from_p (target, exp, 1)))
7726                   || TREE_ADDRESSABLE (exp)
7727                   || (host_integerp (TYPE_SIZE_UNIT (type), 1)
7728                       && (! MOVE_BY_PIECES_P
7729                                      (tree_low_cst (TYPE_SIZE_UNIT (type), 1),
7730                                       TYPE_ALIGN (type)))
7731                       && ! mostly_zeros_p (exp))))
7732       || ((modifier == EXPAND_INITIALIZER || modifier == EXPAND_CONST_ADDRESS)
7733           && TREE_CONSTANT (exp)))
7734     {
7735       rtx constructor;
7736
7737       if (avoid_temp_mem)
7738         return NULL_RTX;
7739
7740       constructor = expand_expr_constant (exp, 1, modifier);
7741
7742       if (modifier != EXPAND_CONST_ADDRESS
7743           && modifier != EXPAND_INITIALIZER
7744           && modifier != EXPAND_SUM)
7745         constructor = validize_mem (constructor);
7746
7747       return constructor;
7748     }
7749
7750   /* Handle calls that pass values in multiple non-contiguous
7751      locations.  The Irix 6 ABI has examples of this.  */
7752   if (target == 0 || ! safe_from_p (target, exp, 1)
7753       || GET_CODE (target) == PARALLEL || modifier == EXPAND_STACK_PARM)
7754     {
7755       if (avoid_temp_mem)
7756         return NULL_RTX;
7757
7758       target
7759         = assign_temp (build_qualified_type (type, (TYPE_QUALS (type)
7760                                                     | (TREE_READONLY (exp)
7761                                                        * TYPE_QUAL_CONST))),
7762                        0, TREE_ADDRESSABLE (exp), 1);
7763     }
7764
7765   store_constructor (exp, target, 0, int_expr_size (exp));
7766   return target;
7767 }
7768
7769
7770 /* expand_expr: generate code for computing expression EXP.
7771    An rtx for the computed value is returned.  The value is never null.
7772    In the case of a void EXP, const0_rtx is returned.
7773
7774    The value may be stored in TARGET if TARGET is nonzero.
7775    TARGET is just a suggestion; callers must assume that
7776    the rtx returned may not be the same as TARGET.
7777
7778    If TARGET is CONST0_RTX, it means that the value will be ignored.
7779
7780    If TMODE is not VOIDmode, it suggests generating the
7781    result in mode TMODE.  But this is done only when convenient.
7782    Otherwise, TMODE is ignored and the value generated in its natural mode.
7783    TMODE is just a suggestion; callers must assume that
7784    the rtx returned may not have mode TMODE.
7785
7786    Note that TARGET may have neither TMODE nor MODE.  In that case, it
7787    probably will not be used.
7788
7789    If MODIFIER is EXPAND_SUM then when EXP is an addition
7790    we can return an rtx of the form (MULT (REG ...) (CONST_INT ...))
7791    or a nest of (PLUS ...) and (MINUS ...) where the terms are
7792    products as above, or REG or MEM, or constant.
7793    Ordinarily in such cases we would output mul or add instructions
7794    and then return a pseudo reg containing the sum.
7795
7796    EXPAND_INITIALIZER is much like EXPAND_SUM except that
7797    it also marks a label as absolutely required (it can't be dead).
7798    It also makes a ZERO_EXTEND or SIGN_EXTEND instead of emitting extend insns.
7799    This is used for outputting expressions used in initializers.
7800
7801    EXPAND_CONST_ADDRESS says that it is okay to return a MEM
7802    with a constant address even if that address is not normally legitimate.
7803    EXPAND_INITIALIZER and EXPAND_SUM also have this effect.
7804
7805    EXPAND_STACK_PARM is used when expanding to a TARGET on the stack for
7806    a call parameter.  Such targets require special care as we haven't yet
7807    marked TARGET so that it's safe from being trashed by libcalls.  We
7808    don't want to use TARGET for anything but the final result;
7809    Intermediate values must go elsewhere.   Additionally, calls to
7810    emit_block_move will be flagged with BLOCK_OP_CALL_PARM.
7811
7812    If EXP is a VAR_DECL whose DECL_RTL was a MEM with an invalid
7813    address, and ALT_RTL is non-NULL, then *ALT_RTL is set to the
7814    DECL_RTL of the VAR_DECL.  *ALT_RTL is also set if EXP is a
7815    COMPOUND_EXPR whose second argument is such a VAR_DECL, and so on
7816    recursively.  */
7817
7818 rtx
7819 expand_expr_real (tree exp, rtx target, enum machine_mode tmode,
7820                   enum expand_modifier modifier, rtx *alt_rtl)
7821 {
7822   rtx ret;
7823
7824   /* Handle ERROR_MARK before anybody tries to access its type.  */
7825   if (TREE_CODE (exp) == ERROR_MARK
7826       || (TREE_CODE (TREE_TYPE (exp)) == ERROR_MARK))
7827     {
7828       ret = CONST0_RTX (tmode);
7829       return ret ? ret : const0_rtx;
7830     }
7831
7832   /* If this is an expression of some kind and it has an associated line
7833      number, then emit the line number before expanding the expression.
7834
7835      We need to save and restore the file and line information so that
7836      errors discovered during expansion are emitted with the right
7837      information.  It would be better of the diagnostic routines
7838      used the file/line information embedded in the tree nodes rather
7839      than globals.  */
7840   if (cfun && EXPR_HAS_LOCATION (exp))
7841     {
7842       location_t saved_location = input_location;
7843       location_t saved_curr_loc = get_curr_insn_source_location ();
7844       tree saved_block = get_curr_insn_block ();
7845       input_location = EXPR_LOCATION (exp);
7846       set_curr_insn_source_location (input_location);
7847
7848       /* Record where the insns produced belong.  */
7849       set_curr_insn_block (TREE_BLOCK (exp));
7850
7851       ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl);
7852
7853       input_location = saved_location;
7854       set_curr_insn_block (saved_block);
7855       set_curr_insn_source_location (saved_curr_loc);
7856     }
7857   else
7858     {
7859       ret = expand_expr_real_1 (exp, target, tmode, modifier, alt_rtl);
7860     }
7861
7862   return ret;
7863 }
7864
7865 /* Try to expand the conditional expression which is represented by
7866    TREEOP0 ? TREEOP1 : TREEOP2 using conditonal moves.  If succeseds
7867    return the rtl reg which repsents the result.  Otherwise return
7868    NULL_RTL.  */
7869
7870 static rtx
7871 expand_cond_expr_using_cmove (tree treeop0 ATTRIBUTE_UNUSED,
7872                               tree treeop1 ATTRIBUTE_UNUSED,
7873                               tree treeop2 ATTRIBUTE_UNUSED)
7874 {
7875 #ifdef HAVE_conditional_move
7876   rtx insn;
7877   rtx op00, op01, op1, op2;
7878   enum rtx_code comparison_code;
7879   enum machine_mode comparison_mode;
7880   gimple srcstmt;
7881   rtx temp;
7882   tree type = TREE_TYPE (treeop1);
7883   int unsignedp = TYPE_UNSIGNED (type);
7884   enum machine_mode mode = TYPE_MODE (type);
7885
7886   temp = assign_temp (type, 0, 0, 1);
7887
7888   /* If we cannot do a conditional move on the mode, try doing it
7889      with the promoted mode. */
7890   if (!can_conditionally_move_p (mode))
7891     mode = promote_mode (type, mode, &unsignedp);
7892
7893   if (!can_conditionally_move_p (mode))
7894     return NULL_RTX;
7895
7896   start_sequence ();
7897   expand_operands (treeop1, treeop2,
7898                    temp, &op1, &op2, EXPAND_NORMAL);
7899
7900   if (TREE_CODE (treeop0) == SSA_NAME
7901       && (srcstmt = get_def_for_expr_class (treeop0, tcc_comparison)))
7902     {
7903       tree type = TREE_TYPE (gimple_assign_rhs1 (srcstmt));
7904       enum tree_code cmpcode = gimple_assign_rhs_code (srcstmt);
7905       op00 = expand_normal (gimple_assign_rhs1 (srcstmt));
7906       op01 = expand_normal (gimple_assign_rhs2 (srcstmt));
7907       comparison_mode = TYPE_MODE (type);
7908       unsignedp = TYPE_UNSIGNED (type);
7909       comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7910     }
7911   else if (TREE_CODE_CLASS (TREE_CODE (treeop0)) == tcc_comparison)
7912     {
7913       tree type = TREE_TYPE (TREE_OPERAND (treeop0, 0));
7914       enum tree_code cmpcode = TREE_CODE (treeop0);
7915       op00 = expand_normal (TREE_OPERAND (treeop0, 0));
7916       op01 = expand_normal (TREE_OPERAND (treeop0, 1));
7917       unsignedp = TYPE_UNSIGNED (type);
7918       comparison_mode = TYPE_MODE (type);
7919       comparison_code = convert_tree_comp_to_rtx (cmpcode, unsignedp);
7920     }
7921   else
7922     {
7923       op00 = expand_normal (treeop0);
7924       op01 = const0_rtx;
7925       comparison_code = NE;
7926       comparison_mode = TYPE_MODE (TREE_TYPE (treeop0));
7927     }
7928
7929   if (GET_MODE (op1) != mode)
7930     op1 = gen_lowpart (mode, op1);
7931
7932   if (GET_MODE (op2) != mode)
7933     op2 = gen_lowpart (mode, op2);
7934
7935   /* Try to emit the conditional move.  */
7936   insn = emit_conditional_move (temp, comparison_code,
7937                                 op00, op01, comparison_mode,
7938                                 op1, op2, mode,
7939                                 unsignedp);
7940
7941   /* If we could do the conditional move, emit the sequence,
7942      and return.  */
7943   if (insn)
7944     {
7945       rtx seq = get_insns ();
7946       end_sequence ();
7947       emit_insn (seq);
7948       return temp;
7949     }
7950
7951   /* Otherwise discard the sequence and fall back to code with
7952      branches.  */
7953   end_sequence ();
7954 #endif
7955   return NULL_RTX;
7956 }
7957
7958 rtx
7959 expand_expr_real_2 (sepops ops, rtx target, enum machine_mode tmode,
7960                     enum expand_modifier modifier)
7961 {
7962   rtx op0, op1, op2, temp;
7963   tree type;
7964   int unsignedp;
7965   enum machine_mode mode;
7966   enum tree_code code = ops->code;
7967   optab this_optab;
7968   rtx subtarget, original_target;
7969   int ignore;
7970   bool reduce_bit_field;
7971   location_t loc = ops->location;
7972   tree treeop0, treeop1, treeop2;
7973 #define REDUCE_BIT_FIELD(expr)  (reduce_bit_field                         \
7974                                  ? reduce_to_bit_field_precision ((expr), \
7975                                                                   target, \
7976                                                                   type)   \
7977                                  : (expr))
7978
7979   type = ops->type;
7980   mode = TYPE_MODE (type);
7981   unsignedp = TYPE_UNSIGNED (type);
7982
7983   treeop0 = ops->op0;
7984   treeop1 = ops->op1;
7985   treeop2 = ops->op2;
7986
7987   /* We should be called only on simple (binary or unary) expressions,
7988      exactly those that are valid in gimple expressions that aren't
7989      GIMPLE_SINGLE_RHS (or invalid).  */
7990   gcc_assert (get_gimple_rhs_class (code) == GIMPLE_UNARY_RHS
7991               || get_gimple_rhs_class (code) == GIMPLE_BINARY_RHS
7992               || get_gimple_rhs_class (code) == GIMPLE_TERNARY_RHS);
7993
7994   ignore = (target == const0_rtx
7995             || ((CONVERT_EXPR_CODE_P (code)
7996                  || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
7997                 && TREE_CODE (type) == VOID_TYPE));
7998
7999   /* We should be called only if we need the result.  */
8000   gcc_assert (!ignore);
8001
8002   /* An operation in what may be a bit-field type needs the
8003      result to be reduced to the precision of the bit-field type,
8004      which is narrower than that of the type's mode.  */
8005   reduce_bit_field = (INTEGRAL_TYPE_P (type)
8006                       && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
8007
8008   if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
8009     target = 0;
8010
8011   /* Use subtarget as the target for operand 0 of a binary operation.  */
8012   subtarget = get_subtarget (target);
8013   original_target = target;
8014
8015   switch (code)
8016     {
8017     case NON_LVALUE_EXPR:
8018     case PAREN_EXPR:
8019     CASE_CONVERT:
8020       if (treeop0 == error_mark_node)
8021         return const0_rtx;
8022
8023       if (TREE_CODE (type) == UNION_TYPE)
8024         {
8025           tree valtype = TREE_TYPE (treeop0);
8026
8027           /* If both input and output are BLKmode, this conversion isn't doing
8028              anything except possibly changing memory attribute.  */
8029           if (mode == BLKmode && TYPE_MODE (valtype) == BLKmode)
8030             {
8031               rtx result = expand_expr (treeop0, target, tmode,
8032                                         modifier);
8033
8034               result = copy_rtx (result);
8035               set_mem_attributes (result, type, 0);
8036               return result;
8037             }
8038
8039           if (target == 0)
8040             {
8041               if (TYPE_MODE (type) != BLKmode)
8042                 target = gen_reg_rtx (TYPE_MODE (type));
8043               else
8044                 target = assign_temp (type, 0, 1, 1);
8045             }
8046
8047           if (MEM_P (target))
8048             /* Store data into beginning of memory target.  */
8049             store_expr (treeop0,
8050                         adjust_address (target, TYPE_MODE (valtype), 0),
8051                         modifier == EXPAND_STACK_PARM,
8052                         false);
8053
8054           else
8055             {
8056               gcc_assert (REG_P (target));
8057
8058               /* Store this field into a union of the proper type.  */
8059               store_field (target,
8060                            MIN ((int_size_in_bytes (TREE_TYPE
8061                                                     (treeop0))
8062                                  * BITS_PER_UNIT),
8063                                 (HOST_WIDE_INT) GET_MODE_BITSIZE (mode)),
8064                            0, 0, 0, TYPE_MODE (valtype), treeop0,
8065                            type, 0, false);
8066             }
8067
8068           /* Return the entire union.  */
8069           return target;
8070         }
8071
8072       if (mode == TYPE_MODE (TREE_TYPE (treeop0)))
8073         {
8074           op0 = expand_expr (treeop0, target, VOIDmode,
8075                              modifier);
8076
8077           /* If the signedness of the conversion differs and OP0 is
8078              a promoted SUBREG, clear that indication since we now
8079              have to do the proper extension.  */
8080           if (TYPE_UNSIGNED (TREE_TYPE (treeop0)) != unsignedp
8081               && GET_CODE (op0) == SUBREG)
8082             SUBREG_PROMOTED_VAR_P (op0) = 0;
8083
8084           return REDUCE_BIT_FIELD (op0);
8085         }
8086
8087       op0 = expand_expr (treeop0, NULL_RTX, mode,
8088                          modifier == EXPAND_SUM ? EXPAND_NORMAL : modifier);
8089       if (GET_MODE (op0) == mode)
8090         ;
8091
8092       /* If OP0 is a constant, just convert it into the proper mode.  */
8093       else if (CONSTANT_P (op0))
8094         {
8095           tree inner_type = TREE_TYPE (treeop0);
8096           enum machine_mode inner_mode = GET_MODE (op0);
8097
8098           if (inner_mode == VOIDmode)
8099             inner_mode = TYPE_MODE (inner_type);
8100
8101           if (modifier == EXPAND_INITIALIZER)
8102             op0 = simplify_gen_subreg (mode, op0, inner_mode,
8103                                        subreg_lowpart_offset (mode,
8104                                                               inner_mode));
8105           else
8106             op0=  convert_modes (mode, inner_mode, op0,
8107                                  TYPE_UNSIGNED (inner_type));
8108         }
8109
8110       else if (modifier == EXPAND_INITIALIZER)
8111         op0 = gen_rtx_fmt_e (unsignedp ? ZERO_EXTEND : SIGN_EXTEND, mode, op0);
8112
8113       else if (target == 0)
8114         op0 = convert_to_mode (mode, op0,
8115                                TYPE_UNSIGNED (TREE_TYPE
8116                                               (treeop0)));
8117       else
8118         {
8119           convert_move (target, op0,
8120                         TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8121           op0 = target;
8122         }
8123
8124       return REDUCE_BIT_FIELD (op0);
8125
8126     case ADDR_SPACE_CONVERT_EXPR:
8127       {
8128         tree treeop0_type = TREE_TYPE (treeop0);
8129         addr_space_t as_to;
8130         addr_space_t as_from;
8131
8132         gcc_assert (POINTER_TYPE_P (type));
8133         gcc_assert (POINTER_TYPE_P (treeop0_type));
8134
8135         as_to = TYPE_ADDR_SPACE (TREE_TYPE (type));
8136         as_from = TYPE_ADDR_SPACE (TREE_TYPE (treeop0_type));
8137
8138         /* Conversions between pointers to the same address space should
8139            have been implemented via CONVERT_EXPR / NOP_EXPR.  */
8140         gcc_assert (as_to != as_from);
8141
8142         /* Ask target code to handle conversion between pointers
8143            to overlapping address spaces.  */
8144         if (targetm.addr_space.subset_p (as_to, as_from)
8145             || targetm.addr_space.subset_p (as_from, as_to))
8146           {
8147             op0 = expand_expr (treeop0, NULL_RTX, VOIDmode, modifier);
8148             op0 = targetm.addr_space.convert (op0, treeop0_type, type);
8149             gcc_assert (op0);
8150             return op0;
8151           }
8152
8153         /* For disjoint address spaces, converting anything but
8154            a null pointer invokes undefined behaviour.  We simply
8155            always return a null pointer here.  */
8156         return CONST0_RTX (mode);
8157       }
8158
8159     case POINTER_PLUS_EXPR:
8160       /* Even though the sizetype mode and the pointer's mode can be different
8161          expand is able to handle this correctly and get the correct result out
8162          of the PLUS_EXPR code.  */
8163       /* Make sure to sign-extend the sizetype offset in a POINTER_PLUS_EXPR
8164          if sizetype precision is smaller than pointer precision.  */
8165       if (TYPE_PRECISION (sizetype) < TYPE_PRECISION (type))
8166         treeop1 = fold_convert_loc (loc, type,
8167                                     fold_convert_loc (loc, ssizetype,
8168                                                       treeop1));
8169       /* If sizetype precision is larger than pointer precision, truncate the
8170          offset to have matching modes.  */
8171       else if (TYPE_PRECISION (sizetype) > TYPE_PRECISION (type))
8172         treeop1 = fold_convert_loc (loc, type, treeop1);
8173
8174     case PLUS_EXPR:
8175       /* If we are adding a constant, a VAR_DECL that is sp, fp, or ap, and
8176          something else, make sure we add the register to the constant and
8177          then to the other thing.  This case can occur during strength
8178          reduction and doing it this way will produce better code if the
8179          frame pointer or argument pointer is eliminated.
8180
8181          fold-const.c will ensure that the constant is always in the inner
8182          PLUS_EXPR, so the only case we need to do anything about is if
8183          sp, ap, or fp is our second argument, in which case we must swap
8184          the innermost first argument and our second argument.  */
8185
8186       if (TREE_CODE (treeop0) == PLUS_EXPR
8187           && TREE_CODE (TREE_OPERAND (treeop0, 1)) == INTEGER_CST
8188           && TREE_CODE (treeop1) == VAR_DECL
8189           && (DECL_RTL (treeop1) == frame_pointer_rtx
8190               || DECL_RTL (treeop1) == stack_pointer_rtx
8191               || DECL_RTL (treeop1) == arg_pointer_rtx))
8192         {
8193           tree t = treeop1;
8194
8195           treeop1 = TREE_OPERAND (treeop0, 0);
8196           TREE_OPERAND (treeop0, 0) = t;
8197         }
8198
8199       /* If the result is to be ptr_mode and we are adding an integer to
8200          something, we might be forming a constant.  So try to use
8201          plus_constant.  If it produces a sum and we can't accept it,
8202          use force_operand.  This allows P = &ARR[const] to generate
8203          efficient code on machines where a SYMBOL_REF is not a valid
8204          address.
8205
8206          If this is an EXPAND_SUM call, always return the sum.  */
8207       if (modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER
8208           || (mode == ptr_mode && (unsignedp || ! flag_trapv)))
8209         {
8210           if (modifier == EXPAND_STACK_PARM)
8211             target = 0;
8212           if (TREE_CODE (treeop0) == INTEGER_CST
8213               && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8214               && TREE_CONSTANT (treeop1))
8215             {
8216               rtx constant_part;
8217
8218               op1 = expand_expr (treeop1, subtarget, VOIDmode,
8219                                  EXPAND_SUM);
8220               /* Use immed_double_const to ensure that the constant is
8221                  truncated according to the mode of OP1, then sign extended
8222                  to a HOST_WIDE_INT.  Using the constant directly can result
8223                  in non-canonical RTL in a 64x32 cross compile.  */
8224               constant_part
8225                 = immed_double_const (TREE_INT_CST_LOW (treeop0),
8226                                       (HOST_WIDE_INT) 0,
8227                                       TYPE_MODE (TREE_TYPE (treeop1)));
8228               op1 = plus_constant (mode, op1, INTVAL (constant_part));
8229               if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8230                 op1 = force_operand (op1, target);
8231               return REDUCE_BIT_FIELD (op1);
8232             }
8233
8234           else if (TREE_CODE (treeop1) == INTEGER_CST
8235                    && GET_MODE_PRECISION (mode) <= HOST_BITS_PER_WIDE_INT
8236                    && TREE_CONSTANT (treeop0))
8237             {
8238               rtx constant_part;
8239
8240               op0 = expand_expr (treeop0, subtarget, VOIDmode,
8241                                  (modifier == EXPAND_INITIALIZER
8242                                  ? EXPAND_INITIALIZER : EXPAND_SUM));
8243               if (! CONSTANT_P (op0))
8244                 {
8245                   op1 = expand_expr (treeop1, NULL_RTX,
8246                                      VOIDmode, modifier);
8247                   /* Return a PLUS if modifier says it's OK.  */
8248                   if (modifier == EXPAND_SUM
8249                       || modifier == EXPAND_INITIALIZER)
8250                     return simplify_gen_binary (PLUS, mode, op0, op1);
8251                   goto binop2;
8252                 }
8253               /* Use immed_double_const to ensure that the constant is
8254                  truncated according to the mode of OP1, then sign extended
8255                  to a HOST_WIDE_INT.  Using the constant directly can result
8256                  in non-canonical RTL in a 64x32 cross compile.  */
8257               constant_part
8258                 = immed_double_const (TREE_INT_CST_LOW (treeop1),
8259                                       (HOST_WIDE_INT) 0,
8260                                       TYPE_MODE (TREE_TYPE (treeop0)));
8261               op0 = plus_constant (mode, op0, INTVAL (constant_part));
8262               if (modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8263                 op0 = force_operand (op0, target);
8264               return REDUCE_BIT_FIELD (op0);
8265             }
8266         }
8267
8268       /* Use TER to expand pointer addition of a negated value
8269          as pointer subtraction.  */
8270       if ((POINTER_TYPE_P (TREE_TYPE (treeop0))
8271            || (TREE_CODE (TREE_TYPE (treeop0)) == VECTOR_TYPE
8272                && POINTER_TYPE_P (TREE_TYPE (TREE_TYPE (treeop0)))))
8273           && TREE_CODE (treeop1) == SSA_NAME
8274           && TYPE_MODE (TREE_TYPE (treeop0))
8275              == TYPE_MODE (TREE_TYPE (treeop1)))
8276         {
8277           gimple def = get_def_for_expr (treeop1, NEGATE_EXPR);
8278           if (def)
8279             {
8280               treeop1 = gimple_assign_rhs1 (def);
8281               code = MINUS_EXPR;
8282               goto do_minus;
8283             }
8284         }
8285
8286       /* No sense saving up arithmetic to be done
8287          if it's all in the wrong mode to form part of an address.
8288          And force_operand won't know whether to sign-extend or
8289          zero-extend.  */
8290       if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8291           || mode != ptr_mode)
8292         {
8293           expand_operands (treeop0, treeop1,
8294                            subtarget, &op0, &op1, EXPAND_NORMAL);
8295           if (op0 == const0_rtx)
8296             return op1;
8297           if (op1 == const0_rtx)
8298             return op0;
8299           goto binop2;
8300         }
8301
8302       expand_operands (treeop0, treeop1,
8303                        subtarget, &op0, &op1, modifier);
8304       return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8305
8306     case MINUS_EXPR:
8307     do_minus:
8308       /* For initializers, we are allowed to return a MINUS of two
8309          symbolic constants.  Here we handle all cases when both operands
8310          are constant.  */
8311       /* Handle difference of two symbolic constants,
8312          for the sake of an initializer.  */
8313       if ((modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
8314           && really_constant_p (treeop0)
8315           && really_constant_p (treeop1))
8316         {
8317           expand_operands (treeop0, treeop1,
8318                            NULL_RTX, &op0, &op1, modifier);
8319
8320           /* If the last operand is a CONST_INT, use plus_constant of
8321              the negated constant.  Else make the MINUS.  */
8322           if (CONST_INT_P (op1))
8323             return REDUCE_BIT_FIELD (plus_constant (mode, op0,
8324                                                     -INTVAL (op1)));
8325           else
8326             return REDUCE_BIT_FIELD (gen_rtx_MINUS (mode, op0, op1));
8327         }
8328
8329       /* No sense saving up arithmetic to be done
8330          if it's all in the wrong mode to form part of an address.
8331          And force_operand won't know whether to sign-extend or
8332          zero-extend.  */
8333       if ((modifier != EXPAND_SUM && modifier != EXPAND_INITIALIZER)
8334           || mode != ptr_mode)
8335         goto binop;
8336
8337       expand_operands (treeop0, treeop1,
8338                        subtarget, &op0, &op1, modifier);
8339
8340       /* Convert A - const to A + (-const).  */
8341       if (CONST_INT_P (op1))
8342         {
8343           op1 = negate_rtx (mode, op1);
8344           return REDUCE_BIT_FIELD (simplify_gen_binary (PLUS, mode, op0, op1));
8345         }
8346
8347       goto binop2;
8348
8349     case WIDEN_MULT_PLUS_EXPR:
8350     case WIDEN_MULT_MINUS_EXPR:
8351       expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8352       op2 = expand_normal (treeop2);
8353       target = expand_widen_pattern_expr (ops, op0, op1, op2,
8354                                           target, unsignedp);
8355       return target;
8356
8357     case WIDEN_MULT_EXPR:
8358       /* If first operand is constant, swap them.
8359          Thus the following special case checks need only
8360          check the second operand.  */
8361       if (TREE_CODE (treeop0) == INTEGER_CST)
8362         {
8363           tree t1 = treeop0;
8364           treeop0 = treeop1;
8365           treeop1 = t1;
8366         }
8367
8368       /* First, check if we have a multiplication of one signed and one
8369          unsigned operand.  */
8370       if (TREE_CODE (treeop1) != INTEGER_CST
8371           && (TYPE_UNSIGNED (TREE_TYPE (treeop0))
8372               != TYPE_UNSIGNED (TREE_TYPE (treeop1))))
8373         {
8374           enum machine_mode innermode = TYPE_MODE (TREE_TYPE (treeop0));
8375           this_optab = usmul_widen_optab;
8376           if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8377                 != CODE_FOR_nothing)
8378             {
8379               if (TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8380                 expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8381                                  EXPAND_NORMAL);
8382               else
8383                 expand_operands (treeop0, treeop1, NULL_RTX, &op1, &op0,
8384                                  EXPAND_NORMAL);
8385               goto binop3;
8386             }
8387         }
8388       /* Check for a multiplication with matching signedness.  */
8389       else if ((TREE_CODE (treeop1) == INTEGER_CST
8390                 && int_fits_type_p (treeop1, TREE_TYPE (treeop0)))
8391                || (TYPE_UNSIGNED (TREE_TYPE (treeop1))
8392                    == TYPE_UNSIGNED (TREE_TYPE (treeop0))))
8393         {
8394           tree op0type = TREE_TYPE (treeop0);
8395           enum machine_mode innermode = TYPE_MODE (op0type);
8396           bool zextend_p = TYPE_UNSIGNED (op0type);
8397           optab other_optab = zextend_p ? smul_widen_optab : umul_widen_optab;
8398           this_optab = zextend_p ? umul_widen_optab : smul_widen_optab;
8399
8400           if (TREE_CODE (treeop0) != INTEGER_CST)
8401             {
8402               if (find_widening_optab_handler (this_optab, mode, innermode, 0)
8403                     != CODE_FOR_nothing)
8404                 {
8405                   expand_operands (treeop0, treeop1, NULL_RTX, &op0, &op1,
8406                                    EXPAND_NORMAL);
8407                   temp = expand_widening_mult (mode, op0, op1, target,
8408                                                unsignedp, this_optab);
8409                   return REDUCE_BIT_FIELD (temp);
8410                 }
8411               if (find_widening_optab_handler (other_optab, mode, innermode, 0)
8412                     != CODE_FOR_nothing
8413                   && innermode == word_mode)
8414                 {
8415                   rtx htem, hipart;
8416                   op0 = expand_normal (treeop0);
8417                   if (TREE_CODE (treeop1) == INTEGER_CST)
8418                     op1 = convert_modes (innermode, mode,
8419                                          expand_normal (treeop1), unsignedp);
8420                   else
8421                     op1 = expand_normal (treeop1);
8422                   temp = expand_binop (mode, other_optab, op0, op1, target,
8423                                        unsignedp, OPTAB_LIB_WIDEN);
8424                   hipart = gen_highpart (innermode, temp);
8425                   htem = expand_mult_highpart_adjust (innermode, hipart,
8426                                                       op0, op1, hipart,
8427                                                       zextend_p);
8428                   if (htem != hipart)
8429                     emit_move_insn (hipart, htem);
8430                   return REDUCE_BIT_FIELD (temp);
8431                 }
8432             }
8433         }
8434       treeop0 = fold_build1 (CONVERT_EXPR, type, treeop0);
8435       treeop1 = fold_build1 (CONVERT_EXPR, type, treeop1);
8436       expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8437       return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8438
8439     case FMA_EXPR:
8440       {
8441         optab opt = fma_optab;
8442         gimple def0, def2;
8443
8444         /* If there is no insn for FMA, emit it as __builtin_fma{,f,l}
8445            call.  */
8446         if (optab_handler (fma_optab, mode) == CODE_FOR_nothing)
8447           {
8448             tree fn = mathfn_built_in (TREE_TYPE (treeop0), BUILT_IN_FMA);
8449             tree call_expr;
8450
8451             gcc_assert (fn != NULL_TREE);
8452             call_expr = build_call_expr (fn, 3, treeop0, treeop1, treeop2);
8453             return expand_builtin (call_expr, target, subtarget, mode, false);
8454           }
8455
8456         def0 = get_def_for_expr (treeop0, NEGATE_EXPR);
8457         def2 = get_def_for_expr (treeop2, NEGATE_EXPR);
8458
8459         op0 = op2 = NULL;
8460
8461         if (def0 && def2
8462             && optab_handler (fnms_optab, mode) != CODE_FOR_nothing)
8463           {
8464             opt = fnms_optab;
8465             op0 = expand_normal (gimple_assign_rhs1 (def0));
8466             op2 = expand_normal (gimple_assign_rhs1 (def2));
8467           }
8468         else if (def0
8469                  && optab_handler (fnma_optab, mode) != CODE_FOR_nothing)
8470           {
8471             opt = fnma_optab;
8472             op0 = expand_normal (gimple_assign_rhs1 (def0));
8473           }
8474         else if (def2
8475                  && optab_handler (fms_optab, mode) != CODE_FOR_nothing)
8476           {
8477             opt = fms_optab;
8478             op2 = expand_normal (gimple_assign_rhs1 (def2));
8479           }
8480
8481         if (op0 == NULL)
8482           op0 = expand_expr (treeop0, subtarget, VOIDmode, EXPAND_NORMAL);
8483         if (op2 == NULL)
8484           op2 = expand_normal (treeop2);
8485         op1 = expand_normal (treeop1);
8486
8487         return expand_ternary_op (TYPE_MODE (type), opt,
8488                                   op0, op1, op2, target, 0);
8489       }
8490
8491     case MULT_EXPR:
8492       /* If this is a fixed-point operation, then we cannot use the code
8493          below because "expand_mult" doesn't support sat/no-sat fixed-point
8494          multiplications.   */
8495       if (ALL_FIXED_POINT_MODE_P (mode))
8496         goto binop;
8497
8498       /* If first operand is constant, swap them.
8499          Thus the following special case checks need only
8500          check the second operand.  */
8501       if (TREE_CODE (treeop0) == INTEGER_CST)
8502         {
8503           tree t1 = treeop0;
8504           treeop0 = treeop1;
8505           treeop1 = t1;
8506         }
8507
8508       /* Attempt to return something suitable for generating an
8509          indexed address, for machines that support that.  */
8510
8511       if (modifier == EXPAND_SUM && mode == ptr_mode
8512           && host_integerp (treeop1, 0))
8513         {
8514           tree exp1 = treeop1;
8515
8516           op0 = expand_expr (treeop0, subtarget, VOIDmode,
8517                              EXPAND_SUM);
8518
8519           if (!REG_P (op0))
8520             op0 = force_operand (op0, NULL_RTX);
8521           if (!REG_P (op0))
8522             op0 = copy_to_mode_reg (mode, op0);
8523
8524           return REDUCE_BIT_FIELD (gen_rtx_MULT (mode, op0,
8525                                gen_int_mode (tree_low_cst (exp1, 0),
8526                                              TYPE_MODE (TREE_TYPE (exp1)))));
8527         }
8528
8529       if (modifier == EXPAND_STACK_PARM)
8530         target = 0;
8531
8532       expand_operands (treeop0, treeop1, subtarget, &op0, &op1, EXPAND_NORMAL);
8533       return REDUCE_BIT_FIELD (expand_mult (mode, op0, op1, target, unsignedp));
8534
8535     case TRUNC_DIV_EXPR:
8536     case FLOOR_DIV_EXPR:
8537     case CEIL_DIV_EXPR:
8538     case ROUND_DIV_EXPR:
8539     case EXACT_DIV_EXPR:
8540       /* If this is a fixed-point operation, then we cannot use the code
8541          below because "expand_divmod" doesn't support sat/no-sat fixed-point
8542          divisions.   */
8543       if (ALL_FIXED_POINT_MODE_P (mode))
8544         goto binop;
8545
8546       if (modifier == EXPAND_STACK_PARM)
8547         target = 0;
8548       /* Possible optimization: compute the dividend with EXPAND_SUM
8549          then if the divisor is constant can optimize the case
8550          where some terms of the dividend have coeffs divisible by it.  */
8551       expand_operands (treeop0, treeop1,
8552                        subtarget, &op0, &op1, EXPAND_NORMAL);
8553       return expand_divmod (0, code, mode, op0, op1, target, unsignedp);
8554
8555     case RDIV_EXPR:
8556       goto binop;
8557
8558     case TRUNC_MOD_EXPR:
8559     case FLOOR_MOD_EXPR:
8560     case CEIL_MOD_EXPR:
8561     case ROUND_MOD_EXPR:
8562       if (modifier == EXPAND_STACK_PARM)
8563         target = 0;
8564       expand_operands (treeop0, treeop1,
8565                        subtarget, &op0, &op1, EXPAND_NORMAL);
8566       return expand_divmod (1, code, mode, op0, op1, target, unsignedp);
8567
8568     case FIXED_CONVERT_EXPR:
8569       op0 = expand_normal (treeop0);
8570       if (target == 0 || modifier == EXPAND_STACK_PARM)
8571         target = gen_reg_rtx (mode);
8572
8573       if ((TREE_CODE (TREE_TYPE (treeop0)) == INTEGER_TYPE
8574            && TYPE_UNSIGNED (TREE_TYPE (treeop0)))
8575           || (TREE_CODE (type) == INTEGER_TYPE && TYPE_UNSIGNED (type)))
8576         expand_fixed_convert (target, op0, 1, TYPE_SATURATING (type));
8577       else
8578         expand_fixed_convert (target, op0, 0, TYPE_SATURATING (type));
8579       return target;
8580
8581     case FIX_TRUNC_EXPR:
8582       op0 = expand_normal (treeop0);
8583       if (target == 0 || modifier == EXPAND_STACK_PARM)
8584         target = gen_reg_rtx (mode);
8585       expand_fix (target, op0, unsignedp);
8586       return target;
8587
8588     case FLOAT_EXPR:
8589       op0 = expand_normal (treeop0);
8590       if (target == 0 || modifier == EXPAND_STACK_PARM)
8591         target = gen_reg_rtx (mode);
8592       /* expand_float can't figure out what to do if FROM has VOIDmode.
8593          So give it the correct mode.  With -O, cse will optimize this.  */
8594       if (GET_MODE (op0) == VOIDmode)
8595         op0 = copy_to_mode_reg (TYPE_MODE (TREE_TYPE (treeop0)),
8596                                 op0);
8597       expand_float (target, op0,
8598                     TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8599       return target;
8600
8601     case NEGATE_EXPR:
8602       op0 = expand_expr (treeop0, subtarget,
8603                          VOIDmode, EXPAND_NORMAL);
8604       if (modifier == EXPAND_STACK_PARM)
8605         target = 0;
8606       temp = expand_unop (mode,
8607                           optab_for_tree_code (NEGATE_EXPR, type,
8608                                                optab_default),
8609                           op0, target, 0);
8610       gcc_assert (temp);
8611       return REDUCE_BIT_FIELD (temp);
8612
8613     case ABS_EXPR:
8614       op0 = expand_expr (treeop0, subtarget,
8615                          VOIDmode, EXPAND_NORMAL);
8616       if (modifier == EXPAND_STACK_PARM)
8617         target = 0;
8618
8619       /* ABS_EXPR is not valid for complex arguments.  */
8620       gcc_assert (GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
8621                   && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT);
8622
8623       /* Unsigned abs is simply the operand.  Testing here means we don't
8624          risk generating incorrect code below.  */
8625       if (TYPE_UNSIGNED (type))
8626         return op0;
8627
8628       return expand_abs (mode, op0, target, unsignedp,
8629                          safe_from_p (target, treeop0, 1));
8630
8631     case MAX_EXPR:
8632     case MIN_EXPR:
8633       target = original_target;
8634       if (target == 0
8635           || modifier == EXPAND_STACK_PARM
8636           || (MEM_P (target) && MEM_VOLATILE_P (target))
8637           || GET_MODE (target) != mode
8638           || (REG_P (target)
8639               && REGNO (target) < FIRST_PSEUDO_REGISTER))
8640         target = gen_reg_rtx (mode);
8641       expand_operands (treeop0, treeop1,
8642                        target, &op0, &op1, EXPAND_NORMAL);
8643
8644       /* First try to do it with a special MIN or MAX instruction.
8645          If that does not win, use a conditional jump to select the proper
8646          value.  */
8647       this_optab = optab_for_tree_code (code, type, optab_default);
8648       temp = expand_binop (mode, this_optab, op0, op1, target, unsignedp,
8649                            OPTAB_WIDEN);
8650       if (temp != 0)
8651         return temp;
8652
8653       /* At this point, a MEM target is no longer useful; we will get better
8654          code without it.  */
8655
8656       if (! REG_P (target))
8657         target = gen_reg_rtx (mode);
8658
8659       /* If op1 was placed in target, swap op0 and op1.  */
8660       if (target != op0 && target == op1)
8661         {
8662           temp = op0;
8663           op0 = op1;
8664           op1 = temp;
8665         }
8666
8667       /* We generate better code and avoid problems with op1 mentioning
8668          target by forcing op1 into a pseudo if it isn't a constant.  */
8669       if (! CONSTANT_P (op1))
8670         op1 = force_reg (mode, op1);
8671
8672       {
8673         enum rtx_code comparison_code;
8674         rtx cmpop1 = op1;
8675
8676         if (code == MAX_EXPR)
8677           comparison_code = unsignedp ? GEU : GE;
8678         else
8679           comparison_code = unsignedp ? LEU : LE;
8680
8681         /* Canonicalize to comparisons against 0.  */
8682         if (op1 == const1_rtx)
8683           {
8684             /* Converting (a >= 1 ? a : 1) into (a > 0 ? a : 1)
8685                or (a != 0 ? a : 1) for unsigned.
8686                For MIN we are safe converting (a <= 1 ? a : 1)
8687                into (a <= 0 ? a : 1)  */
8688             cmpop1 = const0_rtx;
8689             if (code == MAX_EXPR)
8690               comparison_code = unsignedp ? NE : GT;
8691           }
8692         if (op1 == constm1_rtx && !unsignedp)
8693           {
8694             /* Converting (a >= -1 ? a : -1) into (a >= 0 ? a : -1)
8695                and (a <= -1 ? a : -1) into (a < 0 ? a : -1) */
8696             cmpop1 = const0_rtx;
8697             if (code == MIN_EXPR)
8698               comparison_code = LT;
8699           }
8700 #ifdef HAVE_conditional_move
8701         /* Use a conditional move if possible.  */
8702         if (can_conditionally_move_p (mode))
8703           {
8704             rtx insn;
8705
8706             /* ??? Same problem as in expmed.c: emit_conditional_move
8707                forces a stack adjustment via compare_from_rtx, and we
8708                lose the stack adjustment if the sequence we are about
8709                to create is discarded.  */
8710             do_pending_stack_adjust ();
8711
8712             start_sequence ();
8713
8714             /* Try to emit the conditional move.  */
8715             insn = emit_conditional_move (target, comparison_code,
8716                                           op0, cmpop1, mode,
8717                                           op0, op1, mode,
8718                                           unsignedp);
8719
8720             /* If we could do the conditional move, emit the sequence,
8721                and return.  */
8722             if (insn)
8723               {
8724                 rtx seq = get_insns ();
8725                 end_sequence ();
8726                 emit_insn (seq);
8727                 return target;
8728               }
8729
8730             /* Otherwise discard the sequence and fall back to code with
8731                branches.  */
8732             end_sequence ();
8733           }
8734 #endif
8735         if (target != op0)
8736           emit_move_insn (target, op0);
8737
8738         temp = gen_label_rtx ();
8739         do_compare_rtx_and_jump (target, cmpop1, comparison_code,
8740                                  unsignedp, mode, NULL_RTX, NULL_RTX, temp,
8741                                  -1);
8742       }
8743       emit_move_insn (target, op1);
8744       emit_label (temp);
8745       return target;
8746
8747     case BIT_NOT_EXPR:
8748       op0 = expand_expr (treeop0, subtarget,
8749                          VOIDmode, EXPAND_NORMAL);
8750       if (modifier == EXPAND_STACK_PARM)
8751         target = 0;
8752       /* In case we have to reduce the result to bitfield precision
8753          for unsigned bitfield expand this as XOR with a proper constant
8754          instead.  */
8755       if (reduce_bit_field && TYPE_UNSIGNED (type))
8756         temp = expand_binop (mode, xor_optab, op0,
8757                              immed_double_int_const
8758                                (double_int_mask (TYPE_PRECISION (type)), mode),
8759                              target, 1, OPTAB_LIB_WIDEN);
8760       else
8761         temp = expand_unop (mode, one_cmpl_optab, op0, target, 1);
8762       gcc_assert (temp);
8763       return temp;
8764
8765       /* ??? Can optimize bitwise operations with one arg constant.
8766          Can optimize (a bitwise1 n) bitwise2 (a bitwise3 b)
8767          and (a bitwise1 b) bitwise2 b (etc)
8768          but that is probably not worth while.  */
8769
8770     case BIT_AND_EXPR:
8771     case BIT_IOR_EXPR:
8772     case BIT_XOR_EXPR:
8773       goto binop;
8774
8775     case LROTATE_EXPR:
8776     case RROTATE_EXPR:
8777       gcc_assert (VECTOR_MODE_P (TYPE_MODE (type))
8778                   || (GET_MODE_PRECISION (TYPE_MODE (type))
8779                       == TYPE_PRECISION (type)));
8780       /* fall through */
8781
8782     case LSHIFT_EXPR:
8783     case RSHIFT_EXPR:
8784       /* If this is a fixed-point operation, then we cannot use the code
8785          below because "expand_shift" doesn't support sat/no-sat fixed-point
8786          shifts.   */
8787       if (ALL_FIXED_POINT_MODE_P (mode))
8788         goto binop;
8789
8790       if (! safe_from_p (subtarget, treeop1, 1))
8791         subtarget = 0;
8792       if (modifier == EXPAND_STACK_PARM)
8793         target = 0;
8794       op0 = expand_expr (treeop0, subtarget,
8795                          VOIDmode, EXPAND_NORMAL);
8796       temp = expand_variable_shift (code, mode, op0, treeop1, target,
8797                                     unsignedp);
8798       if (code == LSHIFT_EXPR)
8799         temp = REDUCE_BIT_FIELD (temp);
8800       return temp;
8801
8802       /* Could determine the answer when only additive constants differ.  Also,
8803          the addition of one can be handled by changing the condition.  */
8804     case LT_EXPR:
8805     case LE_EXPR:
8806     case GT_EXPR:
8807     case GE_EXPR:
8808     case EQ_EXPR:
8809     case NE_EXPR:
8810     case UNORDERED_EXPR:
8811     case ORDERED_EXPR:
8812     case UNLT_EXPR:
8813     case UNLE_EXPR:
8814     case UNGT_EXPR:
8815     case UNGE_EXPR:
8816     case UNEQ_EXPR:
8817     case LTGT_EXPR:
8818       temp = do_store_flag (ops,
8819                             modifier != EXPAND_STACK_PARM ? target : NULL_RTX,
8820                             tmode != VOIDmode ? tmode : mode);
8821       if (temp)
8822         return temp;
8823
8824       /* Use a compare and a jump for BLKmode comparisons, or for function
8825          type comparisons is HAVE_canonicalize_funcptr_for_compare.  */
8826
8827       if ((target == 0
8828            || modifier == EXPAND_STACK_PARM
8829            || ! safe_from_p (target, treeop0, 1)
8830            || ! safe_from_p (target, treeop1, 1)
8831            /* Make sure we don't have a hard reg (such as function's return
8832               value) live across basic blocks, if not optimizing.  */
8833            || (!optimize && REG_P (target)
8834                && REGNO (target) < FIRST_PSEUDO_REGISTER)))
8835         target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
8836
8837       emit_move_insn (target, const0_rtx);
8838
8839       op1 = gen_label_rtx ();
8840       jumpifnot_1 (code, treeop0, treeop1, op1, -1);
8841
8842       if (TYPE_PRECISION (type) == 1 && !TYPE_UNSIGNED (type))
8843         emit_move_insn (target, constm1_rtx);
8844       else
8845         emit_move_insn (target, const1_rtx);
8846
8847       emit_label (op1);
8848       return target;
8849
8850     case COMPLEX_EXPR:
8851       /* Get the rtx code of the operands.  */
8852       op0 = expand_normal (treeop0);
8853       op1 = expand_normal (treeop1);
8854
8855       if (!target)
8856         target = gen_reg_rtx (TYPE_MODE (type));
8857
8858       /* Move the real (op0) and imaginary (op1) parts to their location.  */
8859       write_complex_part (target, op0, false);
8860       write_complex_part (target, op1, true);
8861
8862       return target;
8863
8864     case WIDEN_SUM_EXPR:
8865       {
8866         tree oprnd0 = treeop0;
8867         tree oprnd1 = treeop1;
8868
8869         expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8870         target = expand_widen_pattern_expr (ops, op0, NULL_RTX, op1,
8871                                             target, unsignedp);
8872         return target;
8873       }
8874
8875     case REDUC_MAX_EXPR:
8876     case REDUC_MIN_EXPR:
8877     case REDUC_PLUS_EXPR:
8878       {
8879         op0 = expand_normal (treeop0);
8880         this_optab = optab_for_tree_code (code, type, optab_default);
8881         temp = expand_unop (mode, this_optab, op0, target, unsignedp);
8882         gcc_assert (temp);
8883         return temp;
8884       }
8885
8886     case VEC_LSHIFT_EXPR:
8887     case VEC_RSHIFT_EXPR:
8888       {
8889         target = expand_vec_shift_expr (ops, target);
8890         return target;
8891       }
8892
8893     case VEC_UNPACK_HI_EXPR:
8894     case VEC_UNPACK_LO_EXPR:
8895       {
8896         op0 = expand_normal (treeop0);
8897         temp = expand_widen_pattern_expr (ops, op0, NULL_RTX, NULL_RTX,
8898                                           target, unsignedp);
8899         gcc_assert (temp);
8900         return temp;
8901       }
8902
8903     case VEC_UNPACK_FLOAT_HI_EXPR:
8904     case VEC_UNPACK_FLOAT_LO_EXPR:
8905       {
8906         op0 = expand_normal (treeop0);
8907         /* The signedness is determined from input operand.  */
8908         temp = expand_widen_pattern_expr
8909           (ops, op0, NULL_RTX, NULL_RTX,
8910            target, TYPE_UNSIGNED (TREE_TYPE (treeop0)));
8911
8912         gcc_assert (temp);
8913         return temp;
8914       }
8915
8916     case VEC_WIDEN_MULT_HI_EXPR:
8917     case VEC_WIDEN_MULT_LO_EXPR:
8918       {
8919         tree oprnd0 = treeop0;
8920         tree oprnd1 = treeop1;
8921
8922         expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8923         target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
8924                                             target, unsignedp);
8925         gcc_assert (target);
8926         return target;
8927       }
8928
8929     case VEC_WIDEN_LSHIFT_HI_EXPR:
8930     case VEC_WIDEN_LSHIFT_LO_EXPR:
8931       {
8932         tree oprnd0 = treeop0;
8933         tree oprnd1 = treeop1;
8934
8935         expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8936         target = expand_widen_pattern_expr (ops, op0, op1, NULL_RTX,
8937                                             target, unsignedp);
8938         gcc_assert (target);
8939         return target;
8940       }
8941
8942     case VEC_PACK_TRUNC_EXPR:
8943     case VEC_PACK_SAT_EXPR:
8944     case VEC_PACK_FIX_TRUNC_EXPR:
8945       mode = TYPE_MODE (TREE_TYPE (treeop0));
8946       goto binop;
8947
8948     case VEC_PERM_EXPR:
8949       expand_operands (treeop0, treeop1, target, &op0, &op1, EXPAND_NORMAL);
8950       op2 = expand_normal (treeop2);
8951
8952       /* Careful here: if the target doesn't support integral vector modes,
8953          a constant selection vector could wind up smooshed into a normal
8954          integral constant.  */
8955       if (CONSTANT_P (op2) && GET_CODE (op2) != CONST_VECTOR)
8956         {
8957           tree sel_type = TREE_TYPE (treeop2);
8958           enum machine_mode vmode
8959             = mode_for_vector (TYPE_MODE (TREE_TYPE (sel_type)),
8960                                TYPE_VECTOR_SUBPARTS (sel_type));
8961           gcc_assert (GET_MODE_CLASS (vmode) == MODE_VECTOR_INT);
8962           op2 = simplify_subreg (vmode, op2, TYPE_MODE (sel_type), 0);
8963           gcc_assert (op2 && GET_CODE (op2) == CONST_VECTOR);
8964         }
8965       else
8966         gcc_assert (GET_MODE_CLASS (GET_MODE (op2)) == MODE_VECTOR_INT);
8967
8968       temp = expand_vec_perm (mode, op0, op1, op2, target);
8969       gcc_assert (temp);
8970       return temp;
8971
8972     case DOT_PROD_EXPR:
8973       {
8974         tree oprnd0 = treeop0;
8975         tree oprnd1 = treeop1;
8976         tree oprnd2 = treeop2;
8977         rtx op2;
8978
8979         expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8980         op2 = expand_normal (oprnd2);
8981         target = expand_widen_pattern_expr (ops, op0, op1, op2,
8982                                             target, unsignedp);
8983         return target;
8984       }
8985
8986     case REALIGN_LOAD_EXPR:
8987       {
8988         tree oprnd0 = treeop0;
8989         tree oprnd1 = treeop1;
8990         tree oprnd2 = treeop2;
8991         rtx op2;
8992
8993         this_optab = optab_for_tree_code (code, type, optab_default);
8994         expand_operands (oprnd0, oprnd1, NULL_RTX, &op0, &op1, EXPAND_NORMAL);
8995         op2 = expand_normal (oprnd2);
8996         temp = expand_ternary_op (mode, this_optab, op0, op1, op2,
8997                                   target, unsignedp);
8998         gcc_assert (temp);
8999         return temp;
9000       }
9001
9002     case COND_EXPR:
9003       /* A COND_EXPR with its type being VOID_TYPE represents a
9004          conditional jump and is handled in
9005          expand_gimple_cond_expr.  */
9006       gcc_assert (!VOID_TYPE_P (type));
9007
9008       /* Note that COND_EXPRs whose type is a structure or union
9009          are required to be constructed to contain assignments of
9010          a temporary variable, so that we can evaluate them here
9011          for side effect only.  If type is void, we must do likewise.  */
9012
9013       gcc_assert (!TREE_ADDRESSABLE (type)
9014                   && !ignore
9015                   && TREE_TYPE (treeop1) != void_type_node
9016                   && TREE_TYPE (treeop2) != void_type_node);
9017
9018       temp = expand_cond_expr_using_cmove (treeop0, treeop1, treeop2);
9019       if (temp)
9020         return temp;
9021
9022       /* If we are not to produce a result, we have no target.  Otherwise,
9023          if a target was specified use it; it will not be used as an
9024          intermediate target unless it is safe.  If no target, use a
9025          temporary.  */
9026
9027       if (modifier != EXPAND_STACK_PARM
9028           && original_target
9029           && safe_from_p (original_target, treeop0, 1)
9030           && GET_MODE (original_target) == mode
9031           && !MEM_P (original_target))
9032         temp = original_target;
9033       else
9034         temp = assign_temp (type, 0, 0, 1);
9035
9036       do_pending_stack_adjust ();
9037       NO_DEFER_POP;
9038       op0 = gen_label_rtx ();
9039       op1 = gen_label_rtx ();
9040       jumpifnot (treeop0, op0, -1);
9041       store_expr (treeop1, temp,
9042                   modifier == EXPAND_STACK_PARM,
9043                   false);
9044
9045       emit_jump_insn (gen_jump (op1));
9046       emit_barrier ();
9047       emit_label (op0);
9048       store_expr (treeop2, temp,
9049                   modifier == EXPAND_STACK_PARM,
9050                   false);
9051
9052       emit_label (op1);
9053       OK_DEFER_POP;
9054       return temp;
9055
9056     case VEC_COND_EXPR:
9057       target = expand_vec_cond_expr (type, treeop0, treeop1, treeop2, target);
9058       return target;
9059
9060     default:
9061       gcc_unreachable ();
9062     }
9063
9064   /* Here to do an ordinary binary operator.  */
9065  binop:
9066   expand_operands (treeop0, treeop1,
9067                    subtarget, &op0, &op1, EXPAND_NORMAL);
9068  binop2:
9069   this_optab = optab_for_tree_code (code, type, optab_default);
9070  binop3:
9071   if (modifier == EXPAND_STACK_PARM)
9072     target = 0;
9073   temp = expand_binop (mode, this_optab, op0, op1, target,
9074                        unsignedp, OPTAB_LIB_WIDEN);
9075   gcc_assert (temp);
9076   /* Bitwise operations do not need bitfield reduction as we expect their
9077      operands being properly truncated.  */
9078   if (code == BIT_XOR_EXPR
9079       || code == BIT_AND_EXPR
9080       || code == BIT_IOR_EXPR)
9081     return temp;
9082   return REDUCE_BIT_FIELD (temp);
9083 }
9084 #undef REDUCE_BIT_FIELD
9085
9086 rtx
9087 expand_expr_real_1 (tree exp, rtx target, enum machine_mode tmode,
9088                     enum expand_modifier modifier, rtx *alt_rtl)
9089 {
9090   rtx op0, op1, temp, decl_rtl;
9091   tree type;
9092   int unsignedp;
9093   enum machine_mode mode;
9094   enum tree_code code = TREE_CODE (exp);
9095   rtx subtarget, original_target;
9096   int ignore;
9097   tree context;
9098   bool reduce_bit_field;
9099   location_t loc = EXPR_LOCATION (exp);
9100   struct separate_ops ops;
9101   tree treeop0, treeop1, treeop2;
9102   tree ssa_name = NULL_TREE;
9103   gimple g;
9104
9105   type = TREE_TYPE (exp);
9106   mode = TYPE_MODE (type);
9107   unsignedp = TYPE_UNSIGNED (type);
9108
9109   treeop0 = treeop1 = treeop2 = NULL_TREE;
9110   if (!VL_EXP_CLASS_P (exp))
9111     switch (TREE_CODE_LENGTH (code))
9112       {
9113         default:
9114         case 3: treeop2 = TREE_OPERAND (exp, 2);
9115         case 2: treeop1 = TREE_OPERAND (exp, 1);
9116         case 1: treeop0 = TREE_OPERAND (exp, 0);
9117         case 0: break;
9118       }
9119   ops.code = code;
9120   ops.type = type;
9121   ops.op0 = treeop0;
9122   ops.op1 = treeop1;
9123   ops.op2 = treeop2;
9124   ops.location = loc;
9125
9126   ignore = (target == const0_rtx
9127             || ((CONVERT_EXPR_CODE_P (code)
9128                  || code == COND_EXPR || code == VIEW_CONVERT_EXPR)
9129                 && TREE_CODE (type) == VOID_TYPE));
9130
9131   /* An operation in what may be a bit-field type needs the
9132      result to be reduced to the precision of the bit-field type,
9133      which is narrower than that of the type's mode.  */
9134   reduce_bit_field = (!ignore
9135                       && INTEGRAL_TYPE_P (type)
9136                       && GET_MODE_PRECISION (mode) > TYPE_PRECISION (type));
9137
9138   /* If we are going to ignore this result, we need only do something
9139      if there is a side-effect somewhere in the expression.  If there
9140      is, short-circuit the most common cases here.  Note that we must
9141      not call expand_expr with anything but const0_rtx in case this
9142      is an initial expansion of a size that contains a PLACEHOLDER_EXPR.  */
9143
9144   if (ignore)
9145     {
9146       if (! TREE_SIDE_EFFECTS (exp))
9147         return const0_rtx;
9148
9149       /* Ensure we reference a volatile object even if value is ignored, but
9150          don't do this if all we are doing is taking its address.  */
9151       if (TREE_THIS_VOLATILE (exp)
9152           && TREE_CODE (exp) != FUNCTION_DECL
9153           && mode != VOIDmode && mode != BLKmode
9154           && modifier != EXPAND_CONST_ADDRESS)
9155         {
9156           temp = expand_expr (exp, NULL_RTX, VOIDmode, modifier);
9157           if (MEM_P (temp))
9158             copy_to_reg (temp);
9159           return const0_rtx;
9160         }
9161
9162       if (TREE_CODE_CLASS (code) == tcc_unary
9163           || code == COMPONENT_REF || code == INDIRECT_REF)
9164         return expand_expr (treeop0, const0_rtx, VOIDmode,
9165                             modifier);
9166
9167       else if (TREE_CODE_CLASS (code) == tcc_binary
9168                || TREE_CODE_CLASS (code) == tcc_comparison
9169                || code == ARRAY_REF || code == ARRAY_RANGE_REF)
9170         {
9171           expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9172           expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9173           return const0_rtx;
9174         }
9175       else if (code == BIT_FIELD_REF)
9176         {
9177           expand_expr (treeop0, const0_rtx, VOIDmode, modifier);
9178           expand_expr (treeop1, const0_rtx, VOIDmode, modifier);
9179           expand_expr (treeop2, const0_rtx, VOIDmode, modifier);
9180           return const0_rtx;
9181         }
9182
9183       target = 0;
9184     }
9185
9186   if (reduce_bit_field && modifier == EXPAND_STACK_PARM)
9187     target = 0;
9188
9189   /* Use subtarget as the target for operand 0 of a binary operation.  */
9190   subtarget = get_subtarget (target);
9191   original_target = target;
9192
9193   switch (code)
9194     {
9195     case LABEL_DECL:
9196       {
9197         tree function = decl_function_context (exp);
9198
9199         temp = label_rtx (exp);
9200         temp = gen_rtx_LABEL_REF (Pmode, temp);
9201
9202         if (function != current_function_decl
9203             && function != 0)
9204           LABEL_REF_NONLOCAL_P (temp) = 1;
9205
9206         temp = gen_rtx_MEM (FUNCTION_MODE, temp);
9207         return temp;
9208       }
9209
9210     case SSA_NAME:
9211       /* ??? ivopts calls expander, without any preparation from
9212          out-of-ssa.  So fake instructions as if this was an access to the
9213          base variable.  This unnecessarily allocates a pseudo, see how we can
9214          reuse it, if partition base vars have it set already.  */
9215       if (!currently_expanding_to_rtl)
9216         return expand_expr_real_1 (SSA_NAME_VAR (exp), target, tmode, modifier,
9217                                    NULL);
9218
9219       g = get_gimple_for_ssa_name (exp);
9220       /* For EXPAND_INITIALIZER try harder to get something simpler.  */
9221       if (g == NULL
9222           && modifier == EXPAND_INITIALIZER
9223           && !SSA_NAME_IS_DEFAULT_DEF (exp)
9224           && (optimize || DECL_IGNORED_P (SSA_NAME_VAR (exp)))
9225           && stmt_is_replaceable_p (SSA_NAME_DEF_STMT (exp)))
9226         g = SSA_NAME_DEF_STMT (exp);
9227       if (g)
9228         {
9229           rtx r = expand_expr_real (gimple_assign_rhs_to_tree (g), target,
9230                                     tmode, modifier, NULL);
9231           if (REG_P (r) && !REG_EXPR (r))
9232             set_reg_attrs_for_decl_rtl (SSA_NAME_VAR (exp), r);
9233           return r;
9234         }
9235
9236       ssa_name = exp;
9237       decl_rtl = get_rtx_for_ssa_name (ssa_name);
9238       exp = SSA_NAME_VAR (ssa_name);
9239       goto expand_decl_rtl;
9240
9241     case PARM_DECL:
9242     case VAR_DECL:
9243       /* If a static var's type was incomplete when the decl was written,
9244          but the type is complete now, lay out the decl now.  */
9245       if (DECL_SIZE (exp) == 0
9246           && COMPLETE_OR_UNBOUND_ARRAY_TYPE_P (TREE_TYPE (exp))
9247           && (TREE_STATIC (exp) || DECL_EXTERNAL (exp)))
9248         layout_decl (exp, 0);
9249
9250       /* ... fall through ...  */
9251
9252     case FUNCTION_DECL:
9253     case RESULT_DECL:
9254       decl_rtl = DECL_RTL (exp);
9255     expand_decl_rtl:
9256       gcc_assert (decl_rtl);
9257       decl_rtl = copy_rtx (decl_rtl);
9258       /* Record writes to register variables.  */
9259       if (modifier == EXPAND_WRITE
9260           && REG_P (decl_rtl)
9261           && HARD_REGISTER_P (decl_rtl))
9262         add_to_hard_reg_set (&crtl->asm_clobbers,
9263                              GET_MODE (decl_rtl), REGNO (decl_rtl));
9264
9265       /* Ensure variable marked as used even if it doesn't go through
9266          a parser.  If it hasn't be used yet, write out an external
9267          definition.  */
9268       TREE_USED (exp) = 1;
9269
9270       /* Show we haven't gotten RTL for this yet.  */
9271       temp = 0;
9272
9273       /* Variables inherited from containing functions should have
9274          been lowered by this point.  */
9275       context = decl_function_context (exp);
9276       gcc_assert (!context
9277                   || context == current_function_decl
9278                   || TREE_STATIC (exp)
9279                   || DECL_EXTERNAL (exp)
9280                   /* ??? C++ creates functions that are not TREE_STATIC.  */
9281                   || TREE_CODE (exp) == FUNCTION_DECL);
9282
9283       /* This is the case of an array whose size is to be determined
9284          from its initializer, while the initializer is still being parsed.
9285          See expand_decl.  */
9286
9287       if (MEM_P (decl_rtl) && REG_P (XEXP (decl_rtl, 0)))
9288         temp = validize_mem (decl_rtl);
9289
9290       /* If DECL_RTL is memory, we are in the normal case and the
9291          address is not valid, get the address into a register.  */
9292
9293       else if (MEM_P (decl_rtl) && modifier != EXPAND_INITIALIZER)
9294         {
9295           if (alt_rtl)
9296             *alt_rtl = decl_rtl;
9297           decl_rtl = use_anchored_address (decl_rtl);
9298           if (modifier != EXPAND_CONST_ADDRESS
9299               && modifier != EXPAND_SUM
9300               && !memory_address_addr_space_p (DECL_MODE (exp),
9301                                                XEXP (decl_rtl, 0),
9302                                                MEM_ADDR_SPACE (decl_rtl)))
9303             temp = replace_equiv_address (decl_rtl,
9304                                           copy_rtx (XEXP (decl_rtl, 0)));
9305         }
9306
9307       /* If we got something, return it.  But first, set the alignment
9308          if the address is a register.  */
9309       if (temp != 0)
9310         {
9311           if (MEM_P (temp) && REG_P (XEXP (temp, 0)))
9312             mark_reg_pointer (XEXP (temp, 0), DECL_ALIGN (exp));
9313
9314           return temp;
9315         }
9316
9317       /* If the mode of DECL_RTL does not match that of the decl,
9318          there are two cases: we are dealing with a BLKmode value
9319          that is returned in a register, or we are dealing with
9320          a promoted value.  In the latter case, return a SUBREG
9321          of the wanted mode, but mark it so that we know that it
9322          was already extended.  */
9323       if (REG_P (decl_rtl)
9324           && DECL_MODE (exp) != BLKmode
9325           && GET_MODE (decl_rtl) != DECL_MODE (exp))
9326         {
9327           enum machine_mode pmode;
9328
9329           /* Get the signedness to be used for this variable.  Ensure we get
9330              the same mode we got when the variable was declared.  */
9331           if (code == SSA_NAME
9332               && (g = SSA_NAME_DEF_STMT (ssa_name))
9333               && gimple_code (g) == GIMPLE_CALL)
9334             {
9335               gcc_assert (!gimple_call_internal_p (g));
9336               pmode = promote_function_mode (type, mode, &unsignedp,
9337                                              gimple_call_fntype (g),
9338                                              2);
9339             }
9340           else
9341             pmode = promote_decl_mode (exp, &unsignedp);
9342           gcc_assert (GET_MODE (decl_rtl) == pmode);
9343
9344           temp = gen_lowpart_SUBREG (mode, decl_rtl);
9345           SUBREG_PROMOTED_VAR_P (temp) = 1;
9346           SUBREG_PROMOTED_UNSIGNED_SET (temp, unsignedp);
9347           return temp;
9348         }
9349
9350       return decl_rtl;
9351
9352     case INTEGER_CST:
9353       temp = immed_double_const (TREE_INT_CST_LOW (exp),
9354                                  TREE_INT_CST_HIGH (exp), mode);
9355
9356       return temp;
9357
9358     case VECTOR_CST:
9359       {
9360         tree tmp = NULL_TREE;
9361         if (GET_MODE_CLASS (mode) == MODE_VECTOR_INT
9362             || GET_MODE_CLASS (mode) == MODE_VECTOR_FLOAT
9363             || GET_MODE_CLASS (mode) == MODE_VECTOR_FRACT
9364             || GET_MODE_CLASS (mode) == MODE_VECTOR_UFRACT
9365             || GET_MODE_CLASS (mode) == MODE_VECTOR_ACCUM
9366             || GET_MODE_CLASS (mode) == MODE_VECTOR_UACCUM)
9367           return const_vector_from_tree (exp);
9368         if (GET_MODE_CLASS (mode) == MODE_INT)
9369           {
9370             tree type_for_mode = lang_hooks.types.type_for_mode (mode, 1);
9371             if (type_for_mode)
9372               tmp = fold_unary_loc (loc, VIEW_CONVERT_EXPR, type_for_mode, exp);
9373           }
9374         if (!tmp)
9375           {
9376             VEC(constructor_elt,gc) *v;
9377             unsigned i;
9378             v = VEC_alloc (constructor_elt, gc, VECTOR_CST_NELTS (exp));
9379             for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
9380               CONSTRUCTOR_APPEND_ELT (v, NULL_TREE, VECTOR_CST_ELT (exp, i));
9381             tmp = build_constructor (type, v);
9382           }
9383         return expand_expr (tmp, ignore ? const0_rtx : target,
9384                             tmode, modifier);
9385       }
9386
9387     case CONST_DECL:
9388       return expand_expr (DECL_INITIAL (exp), target, VOIDmode, modifier);
9389
9390     case REAL_CST:
9391       /* If optimized, generate immediate CONST_DOUBLE
9392          which will be turned into memory by reload if necessary.
9393
9394          We used to force a register so that loop.c could see it.  But
9395          this does not allow gen_* patterns to perform optimizations with
9396          the constants.  It also produces two insns in cases like "x = 1.0;".
9397          On most machines, floating-point constants are not permitted in
9398          many insns, so we'd end up copying it to a register in any case.
9399
9400          Now, we do the copying in expand_binop, if appropriate.  */
9401       return CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (exp),
9402                                            TYPE_MODE (TREE_TYPE (exp)));
9403
9404     case FIXED_CST:
9405       return CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (exp),
9406                                            TYPE_MODE (TREE_TYPE (exp)));
9407
9408     case COMPLEX_CST:
9409       /* Handle evaluating a complex constant in a CONCAT target.  */
9410       if (original_target && GET_CODE (original_target) == CONCAT)
9411         {
9412           enum machine_mode mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (exp)));
9413           rtx rtarg, itarg;
9414
9415           rtarg = XEXP (original_target, 0);
9416           itarg = XEXP (original_target, 1);
9417
9418           /* Move the real and imaginary parts separately.  */
9419           op0 = expand_expr (TREE_REALPART (exp), rtarg, mode, EXPAND_NORMAL);
9420           op1 = expand_expr (TREE_IMAGPART (exp), itarg, mode, EXPAND_NORMAL);
9421
9422           if (op0 != rtarg)
9423             emit_move_insn (rtarg, op0);
9424           if (op1 != itarg)
9425             emit_move_insn (itarg, op1);
9426
9427           return original_target;
9428         }
9429
9430       /* ... fall through ...  */
9431
9432     case STRING_CST:
9433       temp = expand_expr_constant (exp, 1, modifier);
9434
9435       /* temp contains a constant address.
9436          On RISC machines where a constant address isn't valid,
9437          make some insns to get that address into a register.  */
9438       if (modifier != EXPAND_CONST_ADDRESS
9439           && modifier != EXPAND_INITIALIZER
9440           && modifier != EXPAND_SUM
9441           && ! memory_address_addr_space_p (mode, XEXP (temp, 0),
9442                                             MEM_ADDR_SPACE (temp)))
9443         return replace_equiv_address (temp,
9444                                       copy_rtx (XEXP (temp, 0)));
9445       return temp;
9446
9447     case SAVE_EXPR:
9448       {
9449         tree val = treeop0;
9450         rtx ret = expand_expr_real_1 (val, target, tmode, modifier, alt_rtl);
9451
9452         if (!SAVE_EXPR_RESOLVED_P (exp))
9453           {
9454             /* We can indeed still hit this case, typically via builtin
9455                expanders calling save_expr immediately before expanding
9456                something.  Assume this means that we only have to deal
9457                with non-BLKmode values.  */
9458             gcc_assert (GET_MODE (ret) != BLKmode);
9459
9460             val = build_decl (EXPR_LOCATION (exp),
9461                               VAR_DECL, NULL, TREE_TYPE (exp));
9462             DECL_ARTIFICIAL (val) = 1;
9463             DECL_IGNORED_P (val) = 1;
9464             treeop0 = val;
9465             TREE_OPERAND (exp, 0) = treeop0;
9466             SAVE_EXPR_RESOLVED_P (exp) = 1;
9467
9468             if (!CONSTANT_P (ret))
9469               ret = copy_to_reg (ret);
9470             SET_DECL_RTL (val, ret);
9471           }
9472
9473         return ret;
9474       }
9475
9476
9477     case CONSTRUCTOR:
9478       /* If we don't need the result, just ensure we evaluate any
9479          subexpressions.  */
9480       if (ignore)
9481         {
9482           unsigned HOST_WIDE_INT idx;
9483           tree value;
9484
9485           FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (exp), idx, value)
9486             expand_expr (value, const0_rtx, VOIDmode, EXPAND_NORMAL);
9487
9488           return const0_rtx;
9489         }
9490
9491       return expand_constructor (exp, target, modifier, false);
9492
9493     case TARGET_MEM_REF:
9494       {
9495         addr_space_t as
9496           = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9497         struct mem_address addr;
9498         enum insn_code icode;
9499         unsigned int align;
9500
9501         get_address_description (exp, &addr);
9502         op0 = addr_for_mem_ref (&addr, as, true);
9503         op0 = memory_address_addr_space (mode, op0, as);
9504         temp = gen_rtx_MEM (mode, op0);
9505         set_mem_attributes (temp, exp, 0);
9506         set_mem_addr_space (temp, as);
9507         align = get_object_or_type_alignment (exp);
9508         if (modifier != EXPAND_WRITE
9509             && mode != BLKmode
9510             && align < GET_MODE_ALIGNMENT (mode)
9511             /* If the target does not have special handling for unaligned
9512                loads of mode then it can use regular moves for them.  */
9513             && ((icode = optab_handler (movmisalign_optab, mode))
9514                 != CODE_FOR_nothing))
9515           {
9516             struct expand_operand ops[2];
9517
9518             /* We've already validated the memory, and we're creating a
9519                new pseudo destination.  The predicates really can't fail,
9520                nor can the generator.  */
9521             create_output_operand (&ops[0], NULL_RTX, mode);
9522             create_fixed_operand (&ops[1], temp);
9523             expand_insn (icode, 2, ops);
9524             return ops[0].value;
9525           }
9526         return temp;
9527       }
9528
9529     case MEM_REF:
9530       {
9531         addr_space_t as
9532           = TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (TREE_OPERAND (exp, 0))));
9533         enum machine_mode address_mode;
9534         tree base = TREE_OPERAND (exp, 0);
9535         gimple def_stmt;
9536         enum insn_code icode;
9537         unsigned align;
9538         /* Handle expansion of non-aliased memory with non-BLKmode.  That
9539            might end up in a register.  */
9540         if (mem_ref_refers_to_non_mem_p (exp))
9541           {
9542             HOST_WIDE_INT offset = mem_ref_offset (exp).low;
9543             tree bit_offset;
9544             tree bftype;
9545             base = TREE_OPERAND (base, 0);
9546             if (offset == 0
9547                 && host_integerp (TYPE_SIZE (TREE_TYPE (exp)), 1)
9548                 && (GET_MODE_BITSIZE (DECL_MODE (base))
9549                     == TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (exp)))))
9550               return expand_expr (build1 (VIEW_CONVERT_EXPR,
9551                                           TREE_TYPE (exp), base),
9552                                   target, tmode, modifier);
9553             bit_offset = bitsize_int (offset * BITS_PER_UNIT);
9554             bftype = TREE_TYPE (base);
9555             if (TYPE_MODE (TREE_TYPE (exp)) != BLKmode)
9556               bftype = TREE_TYPE (exp);
9557             else
9558               {
9559                 temp = assign_stack_temp (DECL_MODE (base),
9560                                           GET_MODE_SIZE (DECL_MODE (base)),
9561                                           0);
9562                 store_expr (base, temp, 0, false);
9563                 temp = adjust_address (temp, BLKmode, offset);
9564                 set_mem_size (temp, int_size_in_bytes (TREE_TYPE (exp)));
9565                 return temp;
9566               }
9567             return expand_expr (build3 (BIT_FIELD_REF, bftype,
9568                                         base,
9569                                         TYPE_SIZE (TREE_TYPE (exp)),
9570                                         bit_offset),
9571                                 target, tmode, modifier);
9572           }
9573         address_mode = targetm.addr_space.address_mode (as);
9574         base = TREE_OPERAND (exp, 0);
9575         if ((def_stmt = get_def_for_expr (base, BIT_AND_EXPR)))
9576           {
9577             tree mask = gimple_assign_rhs2 (def_stmt);
9578             base = build2 (BIT_AND_EXPR, TREE_TYPE (base),
9579                            gimple_assign_rhs1 (def_stmt), mask);
9580             TREE_OPERAND (exp, 0) = base;
9581           }
9582         align = get_object_or_type_alignment (exp);
9583         op0 = expand_expr (base, NULL_RTX, VOIDmode, EXPAND_SUM);
9584         op0 = memory_address_addr_space (address_mode, op0, as);
9585         if (!integer_zerop (TREE_OPERAND (exp, 1)))
9586           {
9587             rtx off
9588               = immed_double_int_const (mem_ref_offset (exp), address_mode);
9589             op0 = simplify_gen_binary (PLUS, address_mode, op0, off);
9590           }
9591         op0 = memory_address_addr_space (mode, op0, as);
9592         temp = gen_rtx_MEM (mode, op0);
9593         set_mem_attributes (temp, exp, 0);
9594         set_mem_addr_space (temp, as);
9595         if (TREE_THIS_VOLATILE (exp))
9596           MEM_VOLATILE_P (temp) = 1;
9597         if (modifier != EXPAND_WRITE
9598             && mode != BLKmode
9599             && align < GET_MODE_ALIGNMENT (mode))
9600           {
9601             if ((icode = optab_handler (movmisalign_optab, mode))
9602                 != CODE_FOR_nothing)
9603               {
9604                 struct expand_operand ops[2];
9605
9606                 /* We've already validated the memory, and we're creating a
9607                    new pseudo destination.  The predicates really can't fail,
9608                    nor can the generator.  */
9609                 create_output_operand (&ops[0], NULL_RTX, mode);
9610                 create_fixed_operand (&ops[1], temp);
9611                 expand_insn (icode, 2, ops);
9612                 return ops[0].value;
9613               }
9614             else if (SLOW_UNALIGNED_ACCESS (mode, align))
9615               temp = extract_bit_field (temp, GET_MODE_BITSIZE (mode),
9616                                         0, TYPE_UNSIGNED (TREE_TYPE (exp)),
9617                                         true, (modifier == EXPAND_STACK_PARM
9618                                                ? NULL_RTX : target),
9619                                         mode, mode);
9620           }
9621         return temp;
9622       }
9623
9624     case ARRAY_REF:
9625
9626       {
9627         tree array = treeop0;
9628         tree index = treeop1;
9629
9630         /* Fold an expression like: "foo"[2].
9631            This is not done in fold so it won't happen inside &.
9632            Don't fold if this is for wide characters since it's too
9633            difficult to do correctly and this is a very rare case.  */
9634
9635         if (modifier != EXPAND_CONST_ADDRESS
9636             && modifier != EXPAND_INITIALIZER
9637             && modifier != EXPAND_MEMORY)
9638           {
9639             tree t = fold_read_from_constant_string (exp);
9640
9641             if (t)
9642               return expand_expr (t, target, tmode, modifier);
9643           }
9644
9645         /* If this is a constant index into a constant array,
9646            just get the value from the array.  Handle both the cases when
9647            we have an explicit constructor and when our operand is a variable
9648            that was declared const.  */
9649
9650         if (modifier != EXPAND_CONST_ADDRESS
9651             && modifier != EXPAND_INITIALIZER
9652             && modifier != EXPAND_MEMORY
9653             && TREE_CODE (array) == CONSTRUCTOR
9654             && ! TREE_SIDE_EFFECTS (array)
9655             && TREE_CODE (index) == INTEGER_CST)
9656           {
9657             unsigned HOST_WIDE_INT ix;
9658             tree field, value;
9659
9660             FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (array), ix,
9661                                       field, value)
9662               if (tree_int_cst_equal (field, index))
9663                 {
9664                   if (!TREE_SIDE_EFFECTS (value))
9665                     return expand_expr (fold (value), target, tmode, modifier);
9666                   break;
9667                 }
9668           }
9669
9670         else if (optimize >= 1
9671                  && modifier != EXPAND_CONST_ADDRESS
9672                  && modifier != EXPAND_INITIALIZER
9673                  && modifier != EXPAND_MEMORY
9674                  && TREE_READONLY (array) && ! TREE_SIDE_EFFECTS (array)
9675                  && TREE_CODE (array) == VAR_DECL && DECL_INITIAL (array)
9676                  && TREE_CODE (DECL_INITIAL (array)) != ERROR_MARK
9677                  && const_value_known_p (array))
9678           {
9679             if (TREE_CODE (index) == INTEGER_CST)
9680               {
9681                 tree init = DECL_INITIAL (array);
9682
9683                 if (TREE_CODE (init) == CONSTRUCTOR)
9684                   {
9685                     unsigned HOST_WIDE_INT ix;
9686                     tree field, value;
9687
9688                     FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (init), ix,
9689                                               field, value)
9690                       if (tree_int_cst_equal (field, index))
9691                         {
9692                           if (TREE_SIDE_EFFECTS (value))
9693                             break;
9694
9695                           if (TREE_CODE (value) == CONSTRUCTOR)
9696                             {
9697                               /* If VALUE is a CONSTRUCTOR, this
9698                                  optimization is only useful if
9699                                  this doesn't store the CONSTRUCTOR
9700                                  into memory.  If it does, it is more
9701                                  efficient to just load the data from
9702                                  the array directly.  */
9703                               rtx ret = expand_constructor (value, target,
9704                                                             modifier, true);
9705                               if (ret == NULL_RTX)
9706                                 break;
9707                             }
9708
9709                           return expand_expr (fold (value), target, tmode,
9710                                               modifier);
9711                         }
9712                   }
9713                 else if(TREE_CODE (init) == STRING_CST)
9714                   {
9715                     tree index1 = index;
9716                     tree low_bound = array_ref_low_bound (exp);
9717                     index1 = fold_convert_loc (loc, sizetype,
9718                                                treeop1);
9719
9720                     /* Optimize the special-case of a zero lower bound.
9721
9722                        We convert the low_bound to sizetype to avoid some problems
9723                        with constant folding.  (E.g. suppose the lower bound is 1,
9724                        and its mode is QI.  Without the conversion,l (ARRAY
9725                        +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
9726                        +INDEX), which becomes (ARRAY+255+INDEX).  Opps!)  */
9727
9728                     if (! integer_zerop (low_bound))
9729                       index1 = size_diffop_loc (loc, index1,
9730                                             fold_convert_loc (loc, sizetype,
9731                                                               low_bound));
9732
9733                     if (0 > compare_tree_int (index1,
9734                                               TREE_STRING_LENGTH (init)))
9735                       {
9736                         tree type = TREE_TYPE (TREE_TYPE (init));
9737                         enum machine_mode mode = TYPE_MODE (type);
9738
9739                         if (GET_MODE_CLASS (mode) == MODE_INT
9740                             && GET_MODE_SIZE (mode) == 1)
9741                           return gen_int_mode (TREE_STRING_POINTER (init)
9742                                                [TREE_INT_CST_LOW (index1)],
9743                                                mode);
9744                       }
9745                   }
9746               }
9747           }
9748       }
9749       goto normal_inner_ref;
9750
9751     case COMPONENT_REF:
9752       /* If the operand is a CONSTRUCTOR, we can just extract the
9753          appropriate field if it is present.  */
9754       if (TREE_CODE (treeop0) == CONSTRUCTOR)
9755         {
9756           unsigned HOST_WIDE_INT idx;
9757           tree field, value;
9758
9759           FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (treeop0),
9760                                     idx, field, value)
9761             if (field == treeop1
9762                 /* We can normally use the value of the field in the
9763                    CONSTRUCTOR.  However, if this is a bitfield in
9764                    an integral mode that we can fit in a HOST_WIDE_INT,
9765                    we must mask only the number of bits in the bitfield,
9766                    since this is done implicitly by the constructor.  If
9767                    the bitfield does not meet either of those conditions,
9768                    we can't do this optimization.  */
9769                 && (! DECL_BIT_FIELD (field)
9770                     || ((GET_MODE_CLASS (DECL_MODE (field)) == MODE_INT)
9771                         && (GET_MODE_PRECISION (DECL_MODE (field))
9772                             <= HOST_BITS_PER_WIDE_INT))))
9773               {
9774                 if (DECL_BIT_FIELD (field)
9775                     && modifier == EXPAND_STACK_PARM)
9776                   target = 0;
9777                 op0 = expand_expr (value, target, tmode, modifier);
9778                 if (DECL_BIT_FIELD (field))
9779                   {
9780                     HOST_WIDE_INT bitsize = TREE_INT_CST_LOW (DECL_SIZE (field));
9781                     enum machine_mode imode = TYPE_MODE (TREE_TYPE (field));
9782
9783                     if (TYPE_UNSIGNED (TREE_TYPE (field)))
9784                       {
9785                         op1 = GEN_INT (((HOST_WIDE_INT) 1 << bitsize) - 1);
9786                         op0 = expand_and (imode, op0, op1, target);
9787                       }
9788                     else
9789                       {
9790                         int count = GET_MODE_PRECISION (imode) - bitsize;
9791
9792                         op0 = expand_shift (LSHIFT_EXPR, imode, op0, count,
9793                                             target, 0);
9794                         op0 = expand_shift (RSHIFT_EXPR, imode, op0, count,
9795                                             target, 0);
9796                       }
9797                   }
9798
9799                 return op0;
9800               }
9801         }
9802       goto normal_inner_ref;
9803
9804     case BIT_FIELD_REF:
9805     case ARRAY_RANGE_REF:
9806     normal_inner_ref:
9807       {
9808         enum machine_mode mode1, mode2;
9809         HOST_WIDE_INT bitsize, bitpos;
9810         tree offset;
9811         int volatilep = 0, must_force_mem;
9812         bool packedp = false;
9813         tree tem = get_inner_reference (exp, &bitsize, &bitpos, &offset,
9814                                         &mode1, &unsignedp, &volatilep, true);
9815         rtx orig_op0, memloc;
9816         bool mem_attrs_from_type = false;
9817
9818         /* If we got back the original object, something is wrong.  Perhaps
9819            we are evaluating an expression too early.  In any event, don't
9820            infinitely recurse.  */
9821         gcc_assert (tem != exp);
9822
9823         if (TYPE_PACKED (TREE_TYPE (TREE_OPERAND (exp, 0)))
9824             || (TREE_CODE (TREE_OPERAND (exp, 1)) == FIELD_DECL
9825                 && DECL_PACKED (TREE_OPERAND (exp, 1))))
9826           packedp = true;
9827
9828         /* If TEM's type is a union of variable size, pass TARGET to the inner
9829            computation, since it will need a temporary and TARGET is known
9830            to have to do.  This occurs in unchecked conversion in Ada.  */
9831         orig_op0 = op0
9832           = expand_expr (tem,
9833                          (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
9834                           && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
9835                               != INTEGER_CST)
9836                           && modifier != EXPAND_STACK_PARM
9837                           ? target : NULL_RTX),
9838                          VOIDmode,
9839                          (modifier == EXPAND_INITIALIZER
9840                           || modifier == EXPAND_CONST_ADDRESS
9841                           || modifier == EXPAND_STACK_PARM)
9842                          ? modifier : EXPAND_NORMAL);
9843
9844
9845         /* If the bitfield is volatile, we want to access it in the
9846            field's mode, not the computed mode.
9847            If a MEM has VOIDmode (external with incomplete type),
9848            use BLKmode for it instead.  */
9849         if (MEM_P (op0))
9850           {
9851             if (volatilep && flag_strict_volatile_bitfields > 0)
9852               op0 = adjust_address (op0, mode1, 0);
9853             else if (GET_MODE (op0) == VOIDmode)
9854               op0 = adjust_address (op0, BLKmode, 0);
9855           }
9856
9857         mode2
9858           = CONSTANT_P (op0) ? TYPE_MODE (TREE_TYPE (tem)) : GET_MODE (op0);
9859
9860         /* If we have either an offset, a BLKmode result, or a reference
9861            outside the underlying object, we must force it to memory.
9862            Such a case can occur in Ada if we have unchecked conversion
9863            of an expression from a scalar type to an aggregate type or
9864            for an ARRAY_RANGE_REF whose type is BLKmode, or if we were
9865            passed a partially uninitialized object or a view-conversion
9866            to a larger size.  */
9867         must_force_mem = (offset
9868                           || mode1 == BLKmode
9869                           || bitpos + bitsize > GET_MODE_BITSIZE (mode2));
9870
9871         /* Handle CONCAT first.  */
9872         if (GET_CODE (op0) == CONCAT && !must_force_mem)
9873           {
9874             if (bitpos == 0
9875                 && bitsize == GET_MODE_BITSIZE (GET_MODE (op0)))
9876               return op0;
9877             if (bitpos == 0
9878                 && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9879                 && bitsize)
9880               {
9881                 op0 = XEXP (op0, 0);
9882                 mode2 = GET_MODE (op0);
9883               }
9884             else if (bitpos == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 0)))
9885                      && bitsize == GET_MODE_BITSIZE (GET_MODE (XEXP (op0, 1)))
9886                      && bitpos
9887                      && bitsize)
9888               {
9889                 op0 = XEXP (op0, 1);
9890                 bitpos = 0;
9891                 mode2 = GET_MODE (op0);
9892               }
9893             else
9894               /* Otherwise force into memory.  */
9895               must_force_mem = 1;
9896           }
9897
9898         /* If this is a constant, put it in a register if it is a legitimate
9899            constant and we don't need a memory reference.  */
9900         if (CONSTANT_P (op0)
9901             && mode2 != BLKmode
9902             && targetm.legitimate_constant_p (mode2, op0)
9903             && !must_force_mem)
9904           op0 = force_reg (mode2, op0);
9905
9906         /* Otherwise, if this is a constant, try to force it to the constant
9907            pool.  Note that back-ends, e.g. MIPS, may refuse to do so if it
9908            is a legitimate constant.  */
9909         else if (CONSTANT_P (op0) && (memloc = force_const_mem (mode2, op0)))
9910           op0 = validize_mem (memloc);
9911
9912         /* Otherwise, if this is a constant or the object is not in memory
9913            and need be, put it there.  */
9914         else if (CONSTANT_P (op0) || (!MEM_P (op0) && must_force_mem))
9915           {
9916             tree nt = build_qualified_type (TREE_TYPE (tem),
9917                                             (TYPE_QUALS (TREE_TYPE (tem))
9918                                              | TYPE_QUAL_CONST));
9919             memloc = assign_temp (nt, 1, 1, 1);
9920             emit_move_insn (memloc, op0);
9921             op0 = memloc;
9922             mem_attrs_from_type = true;
9923           }
9924
9925         if (offset)
9926           {
9927             enum machine_mode address_mode;
9928             rtx offset_rtx = expand_expr (offset, NULL_RTX, VOIDmode,
9929                                           EXPAND_SUM);
9930
9931             gcc_assert (MEM_P (op0));
9932
9933             address_mode = get_address_mode (op0);
9934             if (GET_MODE (offset_rtx) != address_mode)
9935               offset_rtx = convert_to_mode (address_mode, offset_rtx, 0);
9936
9937             if (GET_MODE (op0) == BLKmode
9938                 /* A constant address in OP0 can have VOIDmode, we must
9939                    not try to call force_reg in that case.  */
9940                 && GET_MODE (XEXP (op0, 0)) != VOIDmode
9941                 && bitsize != 0
9942                 && (bitpos % bitsize) == 0
9943                 && (bitsize % GET_MODE_ALIGNMENT (mode1)) == 0
9944                 && MEM_ALIGN (op0) == GET_MODE_ALIGNMENT (mode1))
9945               {
9946                 op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
9947                 bitpos = 0;
9948               }
9949
9950             op0 = offset_address (op0, offset_rtx,
9951                                   highest_pow2_factor (offset));
9952           }
9953
9954         /* If OFFSET is making OP0 more aligned than BIGGEST_ALIGNMENT,
9955            record its alignment as BIGGEST_ALIGNMENT.  */
9956         if (MEM_P (op0) && bitpos == 0 && offset != 0
9957             && is_aligning_offset (offset, tem))
9958           set_mem_align (op0, BIGGEST_ALIGNMENT);
9959
9960         /* Don't forget about volatility even if this is a bitfield.  */
9961         if (MEM_P (op0) && volatilep && ! MEM_VOLATILE_P (op0))
9962           {
9963             if (op0 == orig_op0)
9964               op0 = copy_rtx (op0);
9965
9966             MEM_VOLATILE_P (op0) = 1;
9967           }
9968
9969         /* In cases where an aligned union has an unaligned object
9970            as a field, we might be extracting a BLKmode value from
9971            an integer-mode (e.g., SImode) object.  Handle this case
9972            by doing the extract into an object as wide as the field
9973            (which we know to be the width of a basic mode), then
9974            storing into memory, and changing the mode to BLKmode.  */
9975         if (mode1 == VOIDmode
9976             || REG_P (op0) || GET_CODE (op0) == SUBREG
9977             || (mode1 != BLKmode && ! direct_load[(int) mode1]
9978                 && GET_MODE_CLASS (mode) != MODE_COMPLEX_INT
9979                 && GET_MODE_CLASS (mode) != MODE_COMPLEX_FLOAT
9980                 && modifier != EXPAND_CONST_ADDRESS
9981                 && modifier != EXPAND_INITIALIZER)
9982             /* If the field is volatile, we always want an aligned
9983                access.  Do this in following two situations:
9984                1. the access is not already naturally
9985                aligned, otherwise "normal" (non-bitfield) volatile fields
9986                become non-addressable.
9987                2. the bitsize is narrower than the access size. Need
9988                to extract bitfields from the access.  */
9989             || (volatilep && flag_strict_volatile_bitfields > 0
9990                 && (bitpos % GET_MODE_ALIGNMENT (mode) != 0 
9991                     || (mode1 != BLKmode
9992                         && bitsize < GET_MODE_SIZE (mode1) * BITS_PER_UNIT)))
9993             /* If the field isn't aligned enough to fetch as a memref,
9994                fetch it as a bit field.  */
9995             || (mode1 != BLKmode
9996                 && (((TYPE_ALIGN (TREE_TYPE (tem)) < GET_MODE_ALIGNMENT (mode)
9997                       || (bitpos % GET_MODE_ALIGNMENT (mode) != 0)
9998                       || (MEM_P (op0)
9999                           && (MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode1)
10000                               || (bitpos % GET_MODE_ALIGNMENT (mode1) != 0))))
10001                      && ((modifier == EXPAND_CONST_ADDRESS
10002                           || modifier == EXPAND_INITIALIZER)
10003                          ? STRICT_ALIGNMENT
10004                          : SLOW_UNALIGNED_ACCESS (mode1, MEM_ALIGN (op0))))
10005                     || (bitpos % BITS_PER_UNIT != 0)))
10006             /* If the type and the field are a constant size and the
10007                size of the type isn't the same size as the bitfield,
10008                we must use bitfield operations.  */
10009             || (bitsize >= 0
10010                 && TYPE_SIZE (TREE_TYPE (exp))
10011                 && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10012                 && 0 != compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)),
10013                                           bitsize)))
10014           {
10015             enum machine_mode ext_mode = mode;
10016
10017             if (ext_mode == BLKmode
10018                 && ! (target != 0 && MEM_P (op0)
10019                       && MEM_P (target)
10020                       && bitpos % BITS_PER_UNIT == 0))
10021               ext_mode = mode_for_size (bitsize, MODE_INT, 1);
10022
10023             if (ext_mode == BLKmode)
10024               {
10025                 if (target == 0)
10026                   target = assign_temp (type, 0, 1, 1);
10027
10028                 if (bitsize == 0)
10029                   return target;
10030
10031                 /* In this case, BITPOS must start at a byte boundary and
10032                    TARGET, if specified, must be a MEM.  */
10033                 gcc_assert (MEM_P (op0)
10034                             && (!target || MEM_P (target))
10035                             && !(bitpos % BITS_PER_UNIT));
10036
10037                 emit_block_move (target,
10038                                  adjust_address (op0, VOIDmode,
10039                                                  bitpos / BITS_PER_UNIT),
10040                                  GEN_INT ((bitsize + BITS_PER_UNIT - 1)
10041                                           / BITS_PER_UNIT),
10042                                  (modifier == EXPAND_STACK_PARM
10043                                   ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10044
10045                 return target;
10046               }
10047
10048             op0 = validize_mem (op0);
10049
10050             if (MEM_P (op0) && REG_P (XEXP (op0, 0)))
10051               mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10052
10053             op0 = extract_bit_field (op0, bitsize, bitpos, unsignedp, packedp,
10054                                      (modifier == EXPAND_STACK_PARM
10055                                       ? NULL_RTX : target),
10056                                      ext_mode, ext_mode);
10057
10058             /* If the result is a record type and BITSIZE is narrower than
10059                the mode of OP0, an integral mode, and this is a big endian
10060                machine, we must put the field into the high-order bits.  */
10061             if (TREE_CODE (type) == RECORD_TYPE && BYTES_BIG_ENDIAN
10062                 && GET_MODE_CLASS (GET_MODE (op0)) == MODE_INT
10063                 && bitsize < (HOST_WIDE_INT) GET_MODE_BITSIZE (GET_MODE (op0)))
10064               op0 = expand_shift (LSHIFT_EXPR, GET_MODE (op0), op0,
10065                                   GET_MODE_BITSIZE (GET_MODE (op0))
10066                                   - bitsize, op0, 1);
10067
10068             /* If the result type is BLKmode, store the data into a temporary
10069                of the appropriate type, but with the mode corresponding to the
10070                mode for the data we have (op0's mode).  It's tempting to make
10071                this a constant type, since we know it's only being stored once,
10072                but that can cause problems if we are taking the address of this
10073                COMPONENT_REF because the MEM of any reference via that address
10074                will have flags corresponding to the type, which will not
10075                necessarily be constant.  */
10076             if (mode == BLKmode)
10077               {
10078                 rtx new_rtx;
10079
10080                 new_rtx = assign_stack_temp_for_type (ext_mode,
10081                                                    GET_MODE_BITSIZE (ext_mode),
10082                                                    0, type);
10083                 emit_move_insn (new_rtx, op0);
10084                 op0 = copy_rtx (new_rtx);
10085                 PUT_MODE (op0, BLKmode);
10086               }
10087
10088             return op0;
10089           }
10090
10091         /* If the result is BLKmode, use that to access the object
10092            now as well.  */
10093         if (mode == BLKmode)
10094           mode1 = BLKmode;
10095
10096         /* Get a reference to just this component.  */
10097         if (modifier == EXPAND_CONST_ADDRESS
10098             || modifier == EXPAND_SUM || modifier == EXPAND_INITIALIZER)
10099           op0 = adjust_address_nv (op0, mode1, bitpos / BITS_PER_UNIT);
10100         else
10101           op0 = adjust_address (op0, mode1, bitpos / BITS_PER_UNIT);
10102
10103         if (op0 == orig_op0)
10104           op0 = copy_rtx (op0);
10105
10106         /* If op0 is a temporary because of forcing to memory, pass only the
10107            type to set_mem_attributes so that the original expression is never
10108            marked as ADDRESSABLE through MEM_EXPR of the temporary.  */
10109         if (mem_attrs_from_type)
10110           set_mem_attributes (op0, type, 0);
10111         else
10112           set_mem_attributes (op0, exp, 0);
10113
10114         if (REG_P (XEXP (op0, 0)))
10115           mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10116
10117         MEM_VOLATILE_P (op0) |= volatilep;
10118         if (mode == mode1 || mode1 == BLKmode || mode1 == tmode
10119             || modifier == EXPAND_CONST_ADDRESS
10120             || modifier == EXPAND_INITIALIZER)
10121           return op0;
10122         else if (target == 0)
10123           target = gen_reg_rtx (tmode != VOIDmode ? tmode : mode);
10124
10125         convert_move (target, op0, unsignedp);
10126         return target;
10127       }
10128
10129     case OBJ_TYPE_REF:
10130       return expand_expr (OBJ_TYPE_REF_EXPR (exp), target, tmode, modifier);
10131
10132     case CALL_EXPR:
10133       /* All valid uses of __builtin_va_arg_pack () are removed during
10134          inlining.  */
10135       if (CALL_EXPR_VA_ARG_PACK (exp))
10136         error ("%Kinvalid use of %<__builtin_va_arg_pack ()%>", exp);
10137       {
10138         tree fndecl = get_callee_fndecl (exp), attr;
10139
10140         if (fndecl
10141             && (attr = lookup_attribute ("error",
10142                                          DECL_ATTRIBUTES (fndecl))) != NULL)
10143           error ("%Kcall to %qs declared with attribute error: %s",
10144                  exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10145                  TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10146         if (fndecl
10147             && (attr = lookup_attribute ("warning",
10148                                          DECL_ATTRIBUTES (fndecl))) != NULL)
10149           warning_at (tree_nonartificial_location (exp),
10150                       0, "%Kcall to %qs declared with attribute warning: %s",
10151                       exp, identifier_to_locale (lang_hooks.decl_printable_name (fndecl, 1)),
10152                       TREE_STRING_POINTER (TREE_VALUE (TREE_VALUE (attr))));
10153
10154         /* Check for a built-in function.  */
10155         if (fndecl && DECL_BUILT_IN (fndecl))
10156           {
10157             gcc_assert (DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_FRONTEND);
10158             return expand_builtin (exp, target, subtarget, tmode, ignore);
10159           }
10160       }
10161       return expand_call (exp, target, ignore);
10162
10163     case VIEW_CONVERT_EXPR:
10164       op0 = NULL_RTX;
10165
10166       /* If we are converting to BLKmode, try to avoid an intermediate
10167          temporary by fetching an inner memory reference.  */
10168       if (mode == BLKmode
10169           && TREE_CODE (TYPE_SIZE (TREE_TYPE (exp))) == INTEGER_CST
10170           && TYPE_MODE (TREE_TYPE (treeop0)) != BLKmode
10171           && handled_component_p (treeop0))
10172       {
10173         enum machine_mode mode1;
10174         HOST_WIDE_INT bitsize, bitpos;
10175         tree offset;
10176         int unsignedp;
10177         int volatilep = 0;
10178         tree tem
10179           = get_inner_reference (treeop0, &bitsize, &bitpos,
10180                                  &offset, &mode1, &unsignedp, &volatilep,
10181                                  true);
10182         rtx orig_op0;
10183
10184         /* ??? We should work harder and deal with non-zero offsets.  */
10185         if (!offset
10186             && (bitpos % BITS_PER_UNIT) == 0
10187             && bitsize >= 0
10188             && compare_tree_int (TYPE_SIZE (TREE_TYPE (exp)), bitsize) == 0)
10189           {
10190             /* See the normal_inner_ref case for the rationale.  */
10191             orig_op0
10192               = expand_expr (tem,
10193                              (TREE_CODE (TREE_TYPE (tem)) == UNION_TYPE
10194                               && (TREE_CODE (TYPE_SIZE (TREE_TYPE (tem)))
10195                                   != INTEGER_CST)
10196                               && modifier != EXPAND_STACK_PARM
10197                               ? target : NULL_RTX),
10198                              VOIDmode,
10199                              (modifier == EXPAND_INITIALIZER
10200                               || modifier == EXPAND_CONST_ADDRESS
10201                               || modifier == EXPAND_STACK_PARM)
10202                              ? modifier : EXPAND_NORMAL);
10203
10204             if (MEM_P (orig_op0))
10205               {
10206                 op0 = orig_op0;
10207
10208                 /* Get a reference to just this component.  */
10209                 if (modifier == EXPAND_CONST_ADDRESS
10210                     || modifier == EXPAND_SUM
10211                     || modifier == EXPAND_INITIALIZER)
10212                   op0 = adjust_address_nv (op0, mode, bitpos / BITS_PER_UNIT);
10213                 else
10214                   op0 = adjust_address (op0, mode, bitpos / BITS_PER_UNIT);
10215
10216                 if (op0 == orig_op0)
10217                   op0 = copy_rtx (op0);
10218
10219                 set_mem_attributes (op0, treeop0, 0);
10220                 if (REG_P (XEXP (op0, 0)))
10221                   mark_reg_pointer (XEXP (op0, 0), MEM_ALIGN (op0));
10222
10223                 MEM_VOLATILE_P (op0) |= volatilep;
10224               }
10225           }
10226       }
10227
10228       if (!op0)
10229         op0 = expand_expr (treeop0,
10230                            NULL_RTX, VOIDmode, modifier);
10231
10232       /* If the input and output modes are both the same, we are done.  */
10233       if (mode == GET_MODE (op0))
10234         ;
10235       /* If neither mode is BLKmode, and both modes are the same size
10236          then we can use gen_lowpart.  */
10237       else if (mode != BLKmode && GET_MODE (op0) != BLKmode
10238                && (GET_MODE_PRECISION (mode)
10239                    == GET_MODE_PRECISION (GET_MODE (op0)))
10240                && !COMPLEX_MODE_P (GET_MODE (op0)))
10241         {
10242           if (GET_CODE (op0) == SUBREG)
10243             op0 = force_reg (GET_MODE (op0), op0);
10244           temp = gen_lowpart_common (mode, op0);
10245           if (temp)
10246             op0 = temp;
10247           else
10248             {
10249               if (!REG_P (op0) && !MEM_P (op0))
10250                 op0 = force_reg (GET_MODE (op0), op0);
10251               op0 = gen_lowpart (mode, op0);
10252             }
10253         }
10254       /* If both types are integral, convert from one mode to the other.  */
10255       else if (INTEGRAL_TYPE_P (type) && INTEGRAL_TYPE_P (TREE_TYPE (treeop0)))
10256         op0 = convert_modes (mode, GET_MODE (op0), op0,
10257                              TYPE_UNSIGNED (TREE_TYPE (treeop0)));
10258       /* As a last resort, spill op0 to memory, and reload it in a
10259          different mode.  */
10260       else if (!MEM_P (op0))
10261         {
10262           /* If the operand is not a MEM, force it into memory.  Since we
10263              are going to be changing the mode of the MEM, don't call
10264              force_const_mem for constants because we don't allow pool
10265              constants to change mode.  */
10266           tree inner_type = TREE_TYPE (treeop0);
10267
10268           gcc_assert (!TREE_ADDRESSABLE (exp));
10269
10270           if (target == 0 || GET_MODE (target) != TYPE_MODE (inner_type))
10271             target
10272               = assign_stack_temp_for_type
10273                 (TYPE_MODE (inner_type),
10274                  GET_MODE_SIZE (TYPE_MODE (inner_type)), 0, inner_type);
10275
10276           emit_move_insn (target, op0);
10277           op0 = target;
10278         }
10279
10280       /* At this point, OP0 is in the correct mode.  If the output type is
10281          such that the operand is known to be aligned, indicate that it is.
10282          Otherwise, we need only be concerned about alignment for non-BLKmode
10283          results.  */
10284       if (MEM_P (op0))
10285         {
10286           enum insn_code icode;
10287
10288           op0 = copy_rtx (op0);
10289
10290           if (TYPE_ALIGN_OK (type))
10291             set_mem_align (op0, MAX (MEM_ALIGN (op0), TYPE_ALIGN (type)));
10292           else if (mode != BLKmode
10293                    && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode)
10294                    /* If the target does have special handling for unaligned
10295                       loads of mode then use them.  */
10296                    && ((icode = optab_handler (movmisalign_optab, mode))
10297                        != CODE_FOR_nothing))
10298             {
10299               rtx reg, insn;
10300
10301               op0 = adjust_address (op0, mode, 0);
10302               /* We've already validated the memory, and we're creating a
10303                  new pseudo destination.  The predicates really can't
10304                  fail.  */
10305               reg = gen_reg_rtx (mode);
10306
10307               /* Nor can the insn generator.  */
10308               insn = GEN_FCN (icode) (reg, op0);
10309               emit_insn (insn);
10310               return reg;
10311             }
10312           else if (STRICT_ALIGNMENT
10313                    && mode != BLKmode
10314                    && MEM_ALIGN (op0) < GET_MODE_ALIGNMENT (mode))
10315             {
10316               tree inner_type = TREE_TYPE (treeop0);
10317               HOST_WIDE_INT temp_size
10318                 = MAX (int_size_in_bytes (inner_type),
10319                        (HOST_WIDE_INT) GET_MODE_SIZE (mode));
10320               rtx new_rtx
10321                 = assign_stack_temp_for_type (mode, temp_size, 0, type);
10322               rtx new_with_op0_mode
10323                 = adjust_address (new_rtx, GET_MODE (op0), 0);
10324
10325               gcc_assert (!TREE_ADDRESSABLE (exp));
10326
10327               if (GET_MODE (op0) == BLKmode)
10328                 emit_block_move (new_with_op0_mode, op0,
10329                                  GEN_INT (GET_MODE_SIZE (mode)),
10330                                  (modifier == EXPAND_STACK_PARM
10331                                   ? BLOCK_OP_CALL_PARM : BLOCK_OP_NORMAL));
10332               else
10333                 emit_move_insn (new_with_op0_mode, op0);
10334
10335               op0 = new_rtx;
10336             }
10337
10338           op0 = adjust_address (op0, mode, 0);
10339         }
10340
10341       return op0;
10342
10343     case MODIFY_EXPR:
10344       {
10345         tree lhs = treeop0;
10346         tree rhs = treeop1;
10347         gcc_assert (ignore);
10348
10349         /* Check for |= or &= of a bitfield of size one into another bitfield
10350            of size 1.  In this case, (unless we need the result of the
10351            assignment) we can do this more efficiently with a
10352            test followed by an assignment, if necessary.
10353
10354            ??? At this point, we can't get a BIT_FIELD_REF here.  But if
10355            things change so we do, this code should be enhanced to
10356            support it.  */
10357         if (TREE_CODE (lhs) == COMPONENT_REF
10358             && (TREE_CODE (rhs) == BIT_IOR_EXPR
10359                 || TREE_CODE (rhs) == BIT_AND_EXPR)
10360             && TREE_OPERAND (rhs, 0) == lhs
10361             && TREE_CODE (TREE_OPERAND (rhs, 1)) == COMPONENT_REF
10362             && integer_onep (DECL_SIZE (TREE_OPERAND (lhs, 1)))
10363             && integer_onep (DECL_SIZE (TREE_OPERAND (TREE_OPERAND (rhs, 1), 1))))
10364           {
10365             rtx label = gen_label_rtx ();
10366             int value = TREE_CODE (rhs) == BIT_IOR_EXPR;
10367             do_jump (TREE_OPERAND (rhs, 1),
10368                      value ? label : 0,
10369                      value ? 0 : label, -1);
10370             expand_assignment (lhs, build_int_cst (TREE_TYPE (rhs), value),
10371                                MOVE_NONTEMPORAL (exp));
10372             do_pending_stack_adjust ();
10373             emit_label (label);
10374             return const0_rtx;
10375           }
10376
10377         expand_assignment (lhs, rhs, MOVE_NONTEMPORAL (exp));
10378         return const0_rtx;
10379       }
10380
10381     case ADDR_EXPR:
10382       return expand_expr_addr_expr (exp, target, tmode, modifier);
10383
10384     case REALPART_EXPR:
10385       op0 = expand_normal (treeop0);
10386       return read_complex_part (op0, false);
10387
10388     case IMAGPART_EXPR:
10389       op0 = expand_normal (treeop0);
10390       return read_complex_part (op0, true);
10391
10392     case RETURN_EXPR:
10393     case LABEL_EXPR:
10394     case GOTO_EXPR:
10395     case SWITCH_EXPR:
10396     case ASM_EXPR:
10397       /* Expanded in cfgexpand.c.  */
10398       gcc_unreachable ();
10399
10400     case TRY_CATCH_EXPR:
10401     case CATCH_EXPR:
10402     case EH_FILTER_EXPR:
10403     case TRY_FINALLY_EXPR:
10404       /* Lowered by tree-eh.c.  */
10405       gcc_unreachable ();
10406
10407     case WITH_CLEANUP_EXPR:
10408     case CLEANUP_POINT_EXPR:
10409     case TARGET_EXPR:
10410     case CASE_LABEL_EXPR:
10411     case VA_ARG_EXPR:
10412     case BIND_EXPR:
10413     case INIT_EXPR:
10414     case CONJ_EXPR:
10415     case COMPOUND_EXPR:
10416     case PREINCREMENT_EXPR:
10417     case PREDECREMENT_EXPR:
10418     case POSTINCREMENT_EXPR:
10419     case POSTDECREMENT_EXPR:
10420     case LOOP_EXPR:
10421     case EXIT_EXPR:
10422       /* Lowered by gimplify.c.  */
10423       gcc_unreachable ();
10424
10425     case FDESC_EXPR:
10426       /* Function descriptors are not valid except for as
10427          initialization constants, and should not be expanded.  */
10428       gcc_unreachable ();
10429
10430     case WITH_SIZE_EXPR:
10431       /* WITH_SIZE_EXPR expands to its first argument.  The caller should
10432          have pulled out the size to use in whatever context it needed.  */
10433       return expand_expr_real (treeop0, original_target, tmode,
10434                                modifier, alt_rtl);
10435
10436     case COMPOUND_LITERAL_EXPR:
10437       {
10438         /* Initialize the anonymous variable declared in the compound
10439            literal, then return the variable.  */
10440         tree decl = COMPOUND_LITERAL_EXPR_DECL (exp);
10441
10442         /* Create RTL for this variable.  */
10443         if (!DECL_RTL_SET_P (decl))
10444           {
10445             if (DECL_HARD_REGISTER (decl))
10446               /* The user specified an assembler name for this variable.
10447                  Set that up now.  */
10448               rest_of_decl_compilation (decl, 0, 0);
10449             else
10450               expand_decl (decl);
10451           }
10452
10453         return expand_expr_real (decl, original_target, tmode,
10454                                  modifier, alt_rtl);
10455       }
10456
10457     default:
10458       return expand_expr_real_2 (&ops, target, tmode, modifier);
10459     }
10460 }
10461 \f
10462 /* Subroutine of above: reduce EXP to the precision of TYPE (in the
10463    signedness of TYPE), possibly returning the result in TARGET.  */
10464 static rtx
10465 reduce_to_bit_field_precision (rtx exp, rtx target, tree type)
10466 {
10467   HOST_WIDE_INT prec = TYPE_PRECISION (type);
10468   if (target && GET_MODE (target) != GET_MODE (exp))
10469     target = 0;
10470   /* For constant values, reduce using build_int_cst_type. */
10471   if (CONST_INT_P (exp))
10472     {
10473       HOST_WIDE_INT value = INTVAL (exp);
10474       tree t = build_int_cst_type (type, value);
10475       return expand_expr (t, target, VOIDmode, EXPAND_NORMAL);
10476     }
10477   else if (TYPE_UNSIGNED (type))
10478     {
10479       rtx mask = immed_double_int_const (double_int_mask (prec),
10480                                          GET_MODE (exp));
10481       return expand_and (GET_MODE (exp), exp, mask, target);
10482     }
10483   else
10484     {
10485       int count = GET_MODE_PRECISION (GET_MODE (exp)) - prec;
10486       exp = expand_shift (LSHIFT_EXPR, GET_MODE (exp),
10487                           exp, count, target, 0);
10488       return expand_shift (RSHIFT_EXPR, GET_MODE (exp),
10489                            exp, count, target, 0);
10490     }
10491 }
10492 \f
10493 /* Subroutine of above: returns 1 if OFFSET corresponds to an offset that
10494    when applied to the address of EXP produces an address known to be
10495    aligned more than BIGGEST_ALIGNMENT.  */
10496
10497 static int
10498 is_aligning_offset (const_tree offset, const_tree exp)
10499 {
10500   /* Strip off any conversions.  */
10501   while (CONVERT_EXPR_P (offset))
10502     offset = TREE_OPERAND (offset, 0);
10503
10504   /* We must now have a BIT_AND_EXPR with a constant that is one less than
10505      power of 2 and which is larger than BIGGEST_ALIGNMENT.  */
10506   if (TREE_CODE (offset) != BIT_AND_EXPR
10507       || !host_integerp (TREE_OPERAND (offset, 1), 1)
10508       || compare_tree_int (TREE_OPERAND (offset, 1),
10509                            BIGGEST_ALIGNMENT / BITS_PER_UNIT) <= 0
10510       || !exact_log2 (tree_low_cst (TREE_OPERAND (offset, 1), 1) + 1) < 0)
10511     return 0;
10512
10513   /* Look at the first operand of BIT_AND_EXPR and strip any conversion.
10514      It must be NEGATE_EXPR.  Then strip any more conversions.  */
10515   offset = TREE_OPERAND (offset, 0);
10516   while (CONVERT_EXPR_P (offset))
10517     offset = TREE_OPERAND (offset, 0);
10518
10519   if (TREE_CODE (offset) != NEGATE_EXPR)
10520     return 0;
10521
10522   offset = TREE_OPERAND (offset, 0);
10523   while (CONVERT_EXPR_P (offset))
10524     offset = TREE_OPERAND (offset, 0);
10525
10526   /* This must now be the address of EXP.  */
10527   return TREE_CODE (offset) == ADDR_EXPR && TREE_OPERAND (offset, 0) == exp;
10528 }
10529 \f
10530 /* Return the tree node if an ARG corresponds to a string constant or zero
10531    if it doesn't.  If we return nonzero, set *PTR_OFFSET to the offset
10532    in bytes within the string that ARG is accessing.  The type of the
10533    offset will be `sizetype'.  */
10534
10535 tree
10536 string_constant (tree arg, tree *ptr_offset)
10537 {
10538   tree array, offset, lower_bound;
10539   STRIP_NOPS (arg);
10540
10541   if (TREE_CODE (arg) == ADDR_EXPR)
10542     {
10543       if (TREE_CODE (TREE_OPERAND (arg, 0)) == STRING_CST)
10544         {
10545           *ptr_offset = size_zero_node;
10546           return TREE_OPERAND (arg, 0);
10547         }
10548       else if (TREE_CODE (TREE_OPERAND (arg, 0)) == VAR_DECL)
10549         {
10550           array = TREE_OPERAND (arg, 0);
10551           offset = size_zero_node;
10552         }
10553       else if (TREE_CODE (TREE_OPERAND (arg, 0)) == ARRAY_REF)
10554         {
10555           array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10556           offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10557           if (TREE_CODE (array) != STRING_CST
10558               && TREE_CODE (array) != VAR_DECL)
10559             return 0;
10560
10561           /* Check if the array has a nonzero lower bound.  */
10562           lower_bound = array_ref_low_bound (TREE_OPERAND (arg, 0));
10563           if (!integer_zerop (lower_bound))
10564             {
10565               /* If the offset and base aren't both constants, return 0.  */
10566               if (TREE_CODE (lower_bound) != INTEGER_CST)
10567                 return 0;
10568               if (TREE_CODE (offset) != INTEGER_CST)
10569                 return 0;
10570               /* Adjust offset by the lower bound.  */
10571               offset = size_diffop (fold_convert (sizetype, offset),
10572                                     fold_convert (sizetype, lower_bound));
10573             }
10574         }
10575       else if (TREE_CODE (TREE_OPERAND (arg, 0)) == MEM_REF)
10576         {
10577           array = TREE_OPERAND (TREE_OPERAND (arg, 0), 0);
10578           offset = TREE_OPERAND (TREE_OPERAND (arg, 0), 1);
10579           if (TREE_CODE (array) != ADDR_EXPR)
10580             return 0;
10581           array = TREE_OPERAND (array, 0);
10582           if (TREE_CODE (array) != STRING_CST
10583               && TREE_CODE (array) != VAR_DECL)
10584             return 0;
10585         }
10586       else
10587         return 0;
10588     }
10589   else if (TREE_CODE (arg) == PLUS_EXPR || TREE_CODE (arg) == POINTER_PLUS_EXPR)
10590     {
10591       tree arg0 = TREE_OPERAND (arg, 0);
10592       tree arg1 = TREE_OPERAND (arg, 1);
10593
10594       STRIP_NOPS (arg0);
10595       STRIP_NOPS (arg1);
10596
10597       if (TREE_CODE (arg0) == ADDR_EXPR
10598           && (TREE_CODE (TREE_OPERAND (arg0, 0)) == STRING_CST
10599               || TREE_CODE (TREE_OPERAND (arg0, 0)) == VAR_DECL))
10600         {
10601           array = TREE_OPERAND (arg0, 0);
10602           offset = arg1;
10603         }
10604       else if (TREE_CODE (arg1) == ADDR_EXPR
10605                && (TREE_CODE (TREE_OPERAND (arg1, 0)) == STRING_CST
10606                    || TREE_CODE (TREE_OPERAND (arg1, 0)) == VAR_DECL))
10607         {
10608           array = TREE_OPERAND (arg1, 0);
10609           offset = arg0;
10610         }
10611       else
10612         return 0;
10613     }
10614   else
10615     return 0;
10616
10617   if (TREE_CODE (array) == STRING_CST)
10618     {
10619       *ptr_offset = fold_convert (sizetype, offset);
10620       return array;
10621     }
10622   else if (TREE_CODE (array) == VAR_DECL
10623            || TREE_CODE (array) == CONST_DECL)
10624     {
10625       int length;
10626
10627       /* Variables initialized to string literals can be handled too.  */
10628       if (!const_value_known_p (array)
10629           || !DECL_INITIAL (array)
10630           || TREE_CODE (DECL_INITIAL (array)) != STRING_CST)
10631         return 0;
10632
10633       /* Avoid const char foo[4] = "abcde";  */
10634       if (DECL_SIZE_UNIT (array) == NULL_TREE
10635           || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
10636           || (length = TREE_STRING_LENGTH (DECL_INITIAL (array))) <= 0
10637           || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
10638         return 0;
10639
10640       /* If variable is bigger than the string literal, OFFSET must be constant
10641          and inside of the bounds of the string literal.  */
10642       offset = fold_convert (sizetype, offset);
10643       if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
10644           && (! host_integerp (offset, 1)
10645               || compare_tree_int (offset, length) >= 0))
10646         return 0;
10647
10648       *ptr_offset = offset;
10649       return DECL_INITIAL (array);
10650     }
10651
10652   return 0;
10653 }
10654 \f
10655 /* Generate code to calculate OPS, and exploded expression
10656    using a store-flag instruction and return an rtx for the result.
10657    OPS reflects a comparison.
10658
10659    If TARGET is nonzero, store the result there if convenient.
10660
10661    Return zero if there is no suitable set-flag instruction
10662    available on this machine.
10663
10664    Once expand_expr has been called on the arguments of the comparison,
10665    we are committed to doing the store flag, since it is not safe to
10666    re-evaluate the expression.  We emit the store-flag insn by calling
10667    emit_store_flag, but only expand the arguments if we have a reason
10668    to believe that emit_store_flag will be successful.  If we think that
10669    it will, but it isn't, we have to simulate the store-flag with a
10670    set/jump/set sequence.  */
10671
10672 static rtx
10673 do_store_flag (sepops ops, rtx target, enum machine_mode mode)
10674 {
10675   enum rtx_code code;
10676   tree arg0, arg1, type;
10677   tree tem;
10678   enum machine_mode operand_mode;
10679   int unsignedp;
10680   rtx op0, op1;
10681   rtx subtarget = target;
10682   location_t loc = ops->location;
10683
10684   arg0 = ops->op0;
10685   arg1 = ops->op1;
10686
10687   /* Don't crash if the comparison was erroneous.  */
10688   if (arg0 == error_mark_node || arg1 == error_mark_node)
10689     return const0_rtx;
10690
10691   type = TREE_TYPE (arg0);
10692   operand_mode = TYPE_MODE (type);
10693   unsignedp = TYPE_UNSIGNED (type);
10694
10695   /* We won't bother with BLKmode store-flag operations because it would mean
10696      passing a lot of information to emit_store_flag.  */
10697   if (operand_mode == BLKmode)
10698     return 0;
10699
10700   /* We won't bother with store-flag operations involving function pointers
10701      when function pointers must be canonicalized before comparisons.  */
10702 #ifdef HAVE_canonicalize_funcptr_for_compare
10703   if (HAVE_canonicalize_funcptr_for_compare
10704       && ((TREE_CODE (TREE_TYPE (arg0)) == POINTER_TYPE
10705            && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg0)))
10706                == FUNCTION_TYPE))
10707           || (TREE_CODE (TREE_TYPE (arg1)) == POINTER_TYPE
10708               && (TREE_CODE (TREE_TYPE (TREE_TYPE (arg1)))
10709                   == FUNCTION_TYPE))))
10710     return 0;
10711 #endif
10712
10713   STRIP_NOPS (arg0);
10714   STRIP_NOPS (arg1);
10715   
10716   /* For vector typed comparisons emit code to generate the desired
10717      all-ones or all-zeros mask.  Conveniently use the VEC_COND_EXPR
10718      expander for this.  */
10719   if (TREE_CODE (ops->type) == VECTOR_TYPE)
10720     {
10721       tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
10722       tree if_true = constant_boolean_node (true, ops->type);
10723       tree if_false = constant_boolean_node (false, ops->type);
10724       return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
10725     }
10726
10727   /* For vector typed comparisons emit code to generate the desired
10728      all-ones or all-zeros mask.  Conveniently use the VEC_COND_EXPR
10729      expander for this.  */
10730   if (TREE_CODE (ops->type) == VECTOR_TYPE)
10731     {
10732       tree ifexp = build2 (ops->code, ops->type, arg0, arg1);
10733       tree if_true = constant_boolean_node (true, ops->type);
10734       tree if_false = constant_boolean_node (false, ops->type);
10735       return expand_vec_cond_expr (ops->type, ifexp, if_true, if_false, target);
10736     }
10737
10738   /* Get the rtx comparison code to use.  We know that EXP is a comparison
10739      operation of some type.  Some comparisons against 1 and -1 can be
10740      converted to comparisons with zero.  Do so here so that the tests
10741      below will be aware that we have a comparison with zero.   These
10742      tests will not catch constants in the first operand, but constants
10743      are rarely passed as the first operand.  */
10744
10745   switch (ops->code)
10746     {
10747     case EQ_EXPR:
10748       code = EQ;
10749       break;
10750     case NE_EXPR:
10751       code = NE;
10752       break;
10753     case LT_EXPR:
10754       if (integer_onep (arg1))
10755         arg1 = integer_zero_node, code = unsignedp ? LEU : LE;
10756       else
10757         code = unsignedp ? LTU : LT;
10758       break;
10759     case LE_EXPR:
10760       if (! unsignedp && integer_all_onesp (arg1))
10761         arg1 = integer_zero_node, code = LT;
10762       else
10763         code = unsignedp ? LEU : LE;
10764       break;
10765     case GT_EXPR:
10766       if (! unsignedp && integer_all_onesp (arg1))
10767         arg1 = integer_zero_node, code = GE;
10768       else
10769         code = unsignedp ? GTU : GT;
10770       break;
10771     case GE_EXPR:
10772       if (integer_onep (arg1))
10773         arg1 = integer_zero_node, code = unsignedp ? GTU : GT;
10774       else
10775         code = unsignedp ? GEU : GE;
10776       break;
10777
10778     case UNORDERED_EXPR:
10779       code = UNORDERED;
10780       break;
10781     case ORDERED_EXPR:
10782       code = ORDERED;
10783       break;
10784     case UNLT_EXPR:
10785       code = UNLT;
10786       break;
10787     case UNLE_EXPR:
10788       code = UNLE;
10789       break;
10790     case UNGT_EXPR:
10791       code = UNGT;
10792       break;
10793     case UNGE_EXPR:
10794       code = UNGE;
10795       break;
10796     case UNEQ_EXPR:
10797       code = UNEQ;
10798       break;
10799     case LTGT_EXPR:
10800       code = LTGT;
10801       break;
10802
10803     default:
10804       gcc_unreachable ();
10805     }
10806
10807   /* Put a constant second.  */
10808   if (TREE_CODE (arg0) == REAL_CST || TREE_CODE (arg0) == INTEGER_CST
10809       || TREE_CODE (arg0) == FIXED_CST)
10810     {
10811       tem = arg0; arg0 = arg1; arg1 = tem;
10812       code = swap_condition (code);
10813     }
10814
10815   /* If this is an equality or inequality test of a single bit, we can
10816      do this by shifting the bit being tested to the low-order bit and
10817      masking the result with the constant 1.  If the condition was EQ,
10818      we xor it with 1.  This does not require an scc insn and is faster
10819      than an scc insn even if we have it.
10820
10821      The code to make this transformation was moved into fold_single_bit_test,
10822      so we just call into the folder and expand its result.  */
10823
10824   if ((code == NE || code == EQ)
10825       && integer_zerop (arg1)
10826       && (TYPE_PRECISION (ops->type) != 1 || TYPE_UNSIGNED (ops->type)))
10827     {
10828       gimple srcstmt = get_def_for_expr (arg0, BIT_AND_EXPR);
10829       if (srcstmt
10830           && integer_pow2p (gimple_assign_rhs2 (srcstmt)))
10831         {
10832           enum tree_code tcode = code == NE ? NE_EXPR : EQ_EXPR;
10833           tree type = lang_hooks.types.type_for_mode (mode, unsignedp);
10834           tree temp = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg1),
10835                                        gimple_assign_rhs1 (srcstmt),
10836                                        gimple_assign_rhs2 (srcstmt));
10837           temp = fold_single_bit_test (loc, tcode, temp, arg1, type);
10838           if (temp)
10839             return expand_expr (temp, target, VOIDmode, EXPAND_NORMAL);
10840         }
10841     }
10842
10843   if (! get_subtarget (target)
10844       || GET_MODE (subtarget) != operand_mode)
10845     subtarget = 0;
10846
10847   expand_operands (arg0, arg1, subtarget, &op0, &op1, EXPAND_NORMAL);
10848
10849   if (target == 0)
10850     target = gen_reg_rtx (mode);
10851
10852   /* Try a cstore if possible.  */
10853   return emit_store_flag_force (target, code, op0, op1,
10854                                 operand_mode, unsignedp,
10855                                 (TYPE_PRECISION (ops->type) == 1
10856                                  && !TYPE_UNSIGNED (ops->type)) ? -1 : 1);
10857 }
10858 \f
10859
10860 /* Stubs in case we haven't got a casesi insn.  */
10861 #ifndef HAVE_casesi
10862 # define HAVE_casesi 0
10863 # define gen_casesi(a, b, c, d, e) (0)
10864 # define CODE_FOR_casesi CODE_FOR_nothing
10865 #endif
10866
10867 /* Attempt to generate a casesi instruction.  Returns 1 if successful,
10868    0 otherwise (i.e. if there is no casesi instruction).  */
10869 int
10870 try_casesi (tree index_type, tree index_expr, tree minval, tree range,
10871             rtx table_label ATTRIBUTE_UNUSED, rtx default_label,
10872             rtx fallback_label ATTRIBUTE_UNUSED)
10873 {
10874   struct expand_operand ops[5];
10875   enum machine_mode index_mode = SImode;
10876   rtx op1, op2, index;
10877
10878   if (! HAVE_casesi)
10879     return 0;
10880
10881   /* Convert the index to SImode.  */
10882   if (GET_MODE_BITSIZE (TYPE_MODE (index_type)) > GET_MODE_BITSIZE (index_mode))
10883     {
10884       enum machine_mode omode = TYPE_MODE (index_type);
10885       rtx rangertx = expand_normal (range);
10886
10887       /* We must handle the endpoints in the original mode.  */
10888       index_expr = build2 (MINUS_EXPR, index_type,
10889                            index_expr, minval);
10890       minval = integer_zero_node;
10891       index = expand_normal (index_expr);
10892       if (default_label)
10893         emit_cmp_and_jump_insns (rangertx, index, LTU, NULL_RTX,
10894                                  omode, 1, default_label);
10895       /* Now we can safely truncate.  */
10896       index = convert_to_mode (index_mode, index, 0);
10897     }
10898   else
10899     {
10900       if (TYPE_MODE (index_type) != index_mode)
10901         {
10902           index_type = lang_hooks.types.type_for_mode (index_mode, 0);
10903           index_expr = fold_convert (index_type, index_expr);
10904         }
10905
10906       index = expand_normal (index_expr);
10907     }
10908
10909   do_pending_stack_adjust ();
10910
10911   op1 = expand_normal (minval);
10912   op2 = expand_normal (range);
10913
10914   create_input_operand (&ops[0], index, index_mode);
10915   create_convert_operand_from_type (&ops[1], op1, TREE_TYPE (minval));
10916   create_convert_operand_from_type (&ops[2], op2, TREE_TYPE (range));
10917   create_fixed_operand (&ops[3], table_label);
10918   create_fixed_operand (&ops[4], (default_label
10919                                   ? default_label
10920                                   : fallback_label));
10921   expand_jump_insn (CODE_FOR_casesi, 5, ops);
10922   return 1;
10923 }
10924
10925 /* Attempt to generate a tablejump instruction; same concept.  */
10926 #ifndef HAVE_tablejump
10927 #define HAVE_tablejump 0
10928 #define gen_tablejump(x, y) (0)
10929 #endif
10930
10931 /* Subroutine of the next function.
10932
10933    INDEX is the value being switched on, with the lowest value
10934    in the table already subtracted.
10935    MODE is its expected mode (needed if INDEX is constant).
10936    RANGE is the length of the jump table.
10937    TABLE_LABEL is a CODE_LABEL rtx for the table itself.
10938
10939    DEFAULT_LABEL is a CODE_LABEL rtx to jump to if the
10940    index value is out of range.  */
10941
10942 static void
10943 do_tablejump (rtx index, enum machine_mode mode, rtx range, rtx table_label,
10944               rtx default_label)
10945 {
10946   rtx temp, vector;
10947
10948   if (INTVAL (range) > cfun->cfg->max_jumptable_ents)
10949     cfun->cfg->max_jumptable_ents = INTVAL (range);
10950
10951   /* Do an unsigned comparison (in the proper mode) between the index
10952      expression and the value which represents the length of the range.
10953      Since we just finished subtracting the lower bound of the range
10954      from the index expression, this comparison allows us to simultaneously
10955      check that the original index expression value is both greater than
10956      or equal to the minimum value of the range and less than or equal to
10957      the maximum value of the range.  */
10958
10959   if (default_label)
10960     emit_cmp_and_jump_insns (index, range, GTU, NULL_RTX, mode, 1,
10961                              default_label);
10962
10963   /* If index is in range, it must fit in Pmode.
10964      Convert to Pmode so we can index with it.  */
10965   if (mode != Pmode)
10966     index = convert_to_mode (Pmode, index, 1);
10967
10968   /* Don't let a MEM slip through, because then INDEX that comes
10969      out of PIC_CASE_VECTOR_ADDRESS won't be a valid address,
10970      and break_out_memory_refs will go to work on it and mess it up.  */
10971 #ifdef PIC_CASE_VECTOR_ADDRESS
10972   if (flag_pic && !REG_P (index))
10973     index = copy_to_mode_reg (Pmode, index);
10974 #endif
10975
10976   /* ??? The only correct use of CASE_VECTOR_MODE is the one inside the
10977      GET_MODE_SIZE, because this indicates how large insns are.  The other
10978      uses should all be Pmode, because they are addresses.  This code
10979      could fail if addresses and insns are not the same size.  */
10980   index = gen_rtx_PLUS (Pmode,
10981                         gen_rtx_MULT (Pmode, index,
10982                                       GEN_INT (GET_MODE_SIZE (CASE_VECTOR_MODE))),
10983                         gen_rtx_LABEL_REF (Pmode, table_label));
10984 #ifdef PIC_CASE_VECTOR_ADDRESS
10985   if (flag_pic)
10986     index = PIC_CASE_VECTOR_ADDRESS (index);
10987   else
10988 #endif
10989     index = memory_address (CASE_VECTOR_MODE, index);
10990   temp = gen_reg_rtx (CASE_VECTOR_MODE);
10991   vector = gen_const_mem (CASE_VECTOR_MODE, index);
10992   convert_move (temp, vector, 0);
10993
10994   emit_jump_insn (gen_tablejump (temp, table_label));
10995
10996   /* If we are generating PIC code or if the table is PC-relative, the
10997      table and JUMP_INSN must be adjacent, so don't output a BARRIER.  */
10998   if (! CASE_VECTOR_PC_RELATIVE && ! flag_pic)
10999     emit_barrier ();
11000 }
11001
11002 int
11003 try_tablejump (tree index_type, tree index_expr, tree minval, tree range,
11004                rtx table_label, rtx default_label)
11005 {
11006   rtx index;
11007
11008   if (! HAVE_tablejump)
11009     return 0;
11010
11011   index_expr = fold_build2 (MINUS_EXPR, index_type,
11012                             fold_convert (index_type, index_expr),
11013                             fold_convert (index_type, minval));
11014   index = expand_normal (index_expr);
11015   do_pending_stack_adjust ();
11016
11017   do_tablejump (index, TYPE_MODE (index_type),
11018                 convert_modes (TYPE_MODE (index_type),
11019                                TYPE_MODE (TREE_TYPE (range)),
11020                                expand_normal (range),
11021                                TYPE_UNSIGNED (TREE_TYPE (range))),
11022                 table_label, default_label);
11023   return 1;
11024 }
11025
11026 /* Return a CONST_VECTOR rtx for a VECTOR_CST tree.  */
11027 static rtx
11028 const_vector_from_tree (tree exp)
11029 {
11030   rtvec v;
11031   unsigned i;
11032   int units;
11033   tree elt;
11034   enum machine_mode inner, mode;
11035
11036   mode = TYPE_MODE (TREE_TYPE (exp));
11037
11038   if (initializer_zerop (exp))
11039     return CONST0_RTX (mode);
11040
11041   units = GET_MODE_NUNITS (mode);
11042   inner = GET_MODE_INNER (mode);
11043
11044   v = rtvec_alloc (units);
11045
11046   for (i = 0; i < VECTOR_CST_NELTS (exp); ++i)
11047     {
11048       elt = VECTOR_CST_ELT (exp, i);
11049
11050       if (TREE_CODE (elt) == REAL_CST)
11051         RTVEC_ELT (v, i) = CONST_DOUBLE_FROM_REAL_VALUE (TREE_REAL_CST (elt),
11052                                                          inner);
11053       else if (TREE_CODE (elt) == FIXED_CST)
11054         RTVEC_ELT (v, i) = CONST_FIXED_FROM_FIXED_VALUE (TREE_FIXED_CST (elt),
11055                                                          inner);
11056       else
11057         RTVEC_ELT (v, i) = immed_double_int_const (tree_to_double_int (elt),
11058                                                    inner);
11059     }
11060
11061   return gen_rtx_CONST_VECTOR (mode, v);
11062 }
11063
11064 /* Build a decl for a personality function given a language prefix.  */
11065
11066 tree
11067 build_personality_function (const char *lang)
11068 {
11069   const char *unwind_and_version;
11070   tree decl, type;
11071   char *name;
11072
11073   switch (targetm_common.except_unwind_info (&global_options))
11074     {
11075     case UI_NONE:
11076       return NULL;
11077     case UI_SJLJ:
11078       unwind_and_version = "_sj0";
11079       break;
11080     case UI_DWARF2:
11081     case UI_TARGET:
11082       unwind_and_version = "_v0";
11083       break;
11084     default:
11085       gcc_unreachable ();
11086     }
11087
11088   name = ACONCAT (("__", lang, "_personality", unwind_and_version, NULL));
11089
11090   type = build_function_type_list (integer_type_node, integer_type_node,
11091                                    long_long_unsigned_type_node,
11092                                    ptr_type_node, ptr_type_node, NULL_TREE);
11093   decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
11094                      get_identifier (name), type);
11095   DECL_ARTIFICIAL (decl) = 1;
11096   DECL_EXTERNAL (decl) = 1;
11097   TREE_PUBLIC (decl) = 1;
11098
11099   /* Zap the nonsensical SYMBOL_REF_DECL for this.  What we're left with
11100      are the flags assigned by targetm.encode_section_info.  */
11101   SET_SYMBOL_REF_DECL (XEXP (DECL_RTL (decl), 0), NULL);
11102
11103   return decl;
11104 }
11105
11106 /* Extracts the personality function of DECL and returns the corresponding
11107    libfunc.  */
11108
11109 rtx
11110 get_personality_function (tree decl)
11111 {
11112   tree personality = DECL_FUNCTION_PERSONALITY (decl);
11113   enum eh_personality_kind pk;
11114
11115   pk = function_needs_eh_personality (DECL_STRUCT_FUNCTION (decl));
11116   if (pk == eh_personality_none)
11117     return NULL;
11118
11119   if (!personality
11120       && pk == eh_personality_any)
11121     personality = lang_hooks.eh_personality ();
11122
11123   if (pk == eh_personality_lang)
11124     gcc_assert (personality != NULL_TREE);
11125
11126   return XEXP (DECL_RTL (personality), 0);
11127 }
11128
11129 #include "gt-expr.h"