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