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