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