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