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