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