Daily bump.
[gcc.git] / gcc / explow.c
1 /* Subroutines for manipulating rtx's in semantically interesting ways.
2 Copyright (C) 1987-2021 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
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "target.h"
25 #include "function.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "memmodel.h"
29 #include "tm_p.h"
30 #include "optabs.h"
31 #include "expmed.h"
32 #include "profile-count.h"
33 #include "emit-rtl.h"
34 #include "recog.h"
35 #include "diagnostic-core.h"
36 #include "stor-layout.h"
37 #include "except.h"
38 #include "dojump.h"
39 #include "explow.h"
40 #include "expr.h"
41 #include "stringpool.h"
42 #include "common/common-target.h"
43 #include "output.h"
44
45 static rtx break_out_memory_refs (rtx);
46
47
48 /* Truncate and perhaps sign-extend C as appropriate for MODE. */
49
50 HOST_WIDE_INT
51 trunc_int_for_mode (HOST_WIDE_INT c, machine_mode mode)
52 {
53 /* Not scalar_int_mode because we also allow pointer bound modes. */
54 scalar_mode smode = as_a <scalar_mode> (mode);
55 int width = GET_MODE_PRECISION (smode);
56
57 /* You want to truncate to a _what_? */
58 gcc_assert (SCALAR_INT_MODE_P (mode));
59
60 /* Canonicalize BImode to 0 and STORE_FLAG_VALUE. */
61 if (smode == BImode)
62 return c & 1 ? STORE_FLAG_VALUE : 0;
63
64 /* Sign-extend for the requested mode. */
65
66 if (width < HOST_BITS_PER_WIDE_INT)
67 {
68 HOST_WIDE_INT sign = 1;
69 sign <<= width - 1;
70 c &= (sign << 1) - 1;
71 c ^= sign;
72 c -= sign;
73 }
74
75 return c;
76 }
77
78 /* Likewise for polynomial values, using the sign-extended representation
79 for each individual coefficient. */
80
81 poly_int64
82 trunc_int_for_mode (poly_int64 x, machine_mode mode)
83 {
84 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
85 x.coeffs[i] = trunc_int_for_mode (x.coeffs[i], mode);
86 return x;
87 }
88
89 /* Return an rtx for the sum of X and the integer C, given that X has
90 mode MODE. INPLACE is true if X can be modified inplace or false
91 if it must be treated as immutable. */
92
93 rtx
94 plus_constant (machine_mode mode, rtx x, poly_int64 c, bool inplace)
95 {
96 RTX_CODE code;
97 rtx y;
98 rtx tem;
99 int all_constant = 0;
100
101 gcc_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
102
103 if (known_eq (c, 0))
104 return x;
105
106 restart:
107
108 code = GET_CODE (x);
109 y = x;
110
111 switch (code)
112 {
113 CASE_CONST_SCALAR_INT:
114 return immed_wide_int_const (wi::add (rtx_mode_t (x, mode), c), mode);
115 case MEM:
116 /* If this is a reference to the constant pool, try replacing it with
117 a reference to a new constant. If the resulting address isn't
118 valid, don't return it because we have no way to validize it. */
119 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
120 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
121 {
122 rtx cst = get_pool_constant (XEXP (x, 0));
123
124 if (GET_CODE (cst) == CONST_VECTOR
125 && GET_MODE_INNER (GET_MODE (cst)) == mode)
126 {
127 cst = gen_lowpart (mode, cst);
128 gcc_assert (cst);
129 }
130 else if (GET_MODE (cst) == VOIDmode
131 && get_pool_mode (XEXP (x, 0)) != mode)
132 break;
133 if (GET_MODE (cst) == VOIDmode || GET_MODE (cst) == mode)
134 {
135 tem = plus_constant (mode, cst, c);
136 tem = force_const_mem (GET_MODE (x), tem);
137 /* Targets may disallow some constants in the constant pool, thus
138 force_const_mem may return NULL_RTX. */
139 if (tem && memory_address_p (GET_MODE (tem), XEXP (tem, 0)))
140 return tem;
141 }
142 }
143 break;
144
145 case CONST:
146 /* If adding to something entirely constant, set a flag
147 so that we can add a CONST around the result. */
148 if (inplace && shared_const_p (x))
149 inplace = false;
150 x = XEXP (x, 0);
151 all_constant = 1;
152 goto restart;
153
154 case SYMBOL_REF:
155 case LABEL_REF:
156 all_constant = 1;
157 break;
158
159 case PLUS:
160 /* The interesting case is adding the integer to a sum. Look
161 for constant term in the sum and combine with C. For an
162 integer constant term or a constant term that is not an
163 explicit integer, we combine or group them together anyway.
164
165 We may not immediately return from the recursive call here, lest
166 all_constant gets lost. */
167
168 if (CONSTANT_P (XEXP (x, 1)))
169 {
170 rtx term = plus_constant (mode, XEXP (x, 1), c, inplace);
171 if (term == const0_rtx)
172 x = XEXP (x, 0);
173 else if (inplace)
174 XEXP (x, 1) = term;
175 else
176 x = gen_rtx_PLUS (mode, XEXP (x, 0), term);
177 c = 0;
178 }
179 else if (rtx *const_loc = find_constant_term_loc (&y))
180 {
181 if (!inplace)
182 {
183 /* We need to be careful since X may be shared and we can't
184 modify it in place. */
185 x = copy_rtx (x);
186 const_loc = find_constant_term_loc (&x);
187 }
188 *const_loc = plus_constant (mode, *const_loc, c, true);
189 c = 0;
190 }
191 break;
192
193 default:
194 if (CONST_POLY_INT_P (x))
195 return immed_wide_int_const (const_poly_int_value (x) + c, mode);
196 break;
197 }
198
199 if (maybe_ne (c, 0))
200 x = gen_rtx_PLUS (mode, x, gen_int_mode (c, mode));
201
202 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
203 return x;
204 else if (all_constant)
205 return gen_rtx_CONST (mode, x);
206 else
207 return x;
208 }
209 \f
210 /* If X is a sum, return a new sum like X but lacking any constant terms.
211 Add all the removed constant terms into *CONSTPTR.
212 X itself is not altered. The result != X if and only if
213 it is not isomorphic to X. */
214
215 rtx
216 eliminate_constant_term (rtx x, rtx *constptr)
217 {
218 rtx x0, x1;
219 rtx tem;
220
221 if (GET_CODE (x) != PLUS)
222 return x;
223
224 /* First handle constants appearing at this level explicitly. */
225 if (CONST_INT_P (XEXP (x, 1))
226 && (tem = simplify_binary_operation (PLUS, GET_MODE (x), *constptr,
227 XEXP (x, 1))) != 0
228 && CONST_INT_P (tem))
229 {
230 *constptr = tem;
231 return eliminate_constant_term (XEXP (x, 0), constptr);
232 }
233
234 tem = const0_rtx;
235 x0 = eliminate_constant_term (XEXP (x, 0), &tem);
236 x1 = eliminate_constant_term (XEXP (x, 1), &tem);
237 if ((x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
238 && (tem = simplify_binary_operation (PLUS, GET_MODE (x),
239 *constptr, tem)) != 0
240 && CONST_INT_P (tem))
241 {
242 *constptr = tem;
243 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
244 }
245
246 return x;
247 }
248
249 \f
250 /* Return a copy of X in which all memory references
251 and all constants that involve symbol refs
252 have been replaced with new temporary registers.
253 Also emit code to load the memory locations and constants
254 into those registers.
255
256 If X contains no such constants or memory references,
257 X itself (not a copy) is returned.
258
259 If a constant is found in the address that is not a legitimate constant
260 in an insn, it is left alone in the hope that it might be valid in the
261 address.
262
263 X may contain no arithmetic except addition, subtraction and multiplication.
264 Values returned by expand_expr with 1 for sum_ok fit this constraint. */
265
266 static rtx
267 break_out_memory_refs (rtx x)
268 {
269 if (MEM_P (x)
270 || (CONSTANT_P (x) && CONSTANT_ADDRESS_P (x)
271 && GET_MODE (x) != VOIDmode))
272 x = force_reg (GET_MODE (x), x);
273 else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
274 || GET_CODE (x) == MULT)
275 {
276 rtx op0 = break_out_memory_refs (XEXP (x, 0));
277 rtx op1 = break_out_memory_refs (XEXP (x, 1));
278
279 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
280 x = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
281 }
282
283 return x;
284 }
285
286 /* Given X, a memory address in address space AS' pointer mode, convert it to
287 an address in the address space's address mode, or vice versa (TO_MODE says
288 which way). We take advantage of the fact that pointers are not allowed to
289 overflow by commuting arithmetic operations over conversions so that address
290 arithmetic insns can be used. IN_CONST is true if this conversion is inside
291 a CONST. NO_EMIT is true if no insns should be emitted, and instead
292 it should return NULL if it can't be simplified without emitting insns. */
293
294 rtx
295 convert_memory_address_addr_space_1 (scalar_int_mode to_mode ATTRIBUTE_UNUSED,
296 rtx x, addr_space_t as ATTRIBUTE_UNUSED,
297 bool in_const ATTRIBUTE_UNUSED,
298 bool no_emit ATTRIBUTE_UNUSED)
299 {
300 #ifndef POINTERS_EXTEND_UNSIGNED
301 gcc_assert (GET_MODE (x) == to_mode || GET_MODE (x) == VOIDmode);
302 return x;
303 #else /* defined(POINTERS_EXTEND_UNSIGNED) */
304 scalar_int_mode pointer_mode, address_mode, from_mode;
305 rtx temp;
306 enum rtx_code code;
307
308 /* If X already has the right mode, just return it. */
309 if (GET_MODE (x) == to_mode)
310 return x;
311
312 pointer_mode = targetm.addr_space.pointer_mode (as);
313 address_mode = targetm.addr_space.address_mode (as);
314 from_mode = to_mode == pointer_mode ? address_mode : pointer_mode;
315
316 /* Here we handle some special cases. If none of them apply, fall through
317 to the default case. */
318 switch (GET_CODE (x))
319 {
320 CASE_CONST_SCALAR_INT:
321 if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode))
322 code = TRUNCATE;
323 else if (POINTERS_EXTEND_UNSIGNED < 0)
324 break;
325 else if (POINTERS_EXTEND_UNSIGNED > 0)
326 code = ZERO_EXTEND;
327 else
328 code = SIGN_EXTEND;
329 temp = simplify_unary_operation (code, to_mode, x, from_mode);
330 if (temp)
331 return temp;
332 break;
333
334 case SUBREG:
335 if ((SUBREG_PROMOTED_VAR_P (x) || REG_POINTER (SUBREG_REG (x)))
336 && GET_MODE (SUBREG_REG (x)) == to_mode)
337 return SUBREG_REG (x);
338 break;
339
340 case LABEL_REF:
341 temp = gen_rtx_LABEL_REF (to_mode, label_ref_label (x));
342 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
343 return temp;
344
345 case SYMBOL_REF:
346 temp = shallow_copy_rtx (x);
347 PUT_MODE (temp, to_mode);
348 return temp;
349
350 case CONST:
351 temp = convert_memory_address_addr_space_1 (to_mode, XEXP (x, 0), as,
352 true, no_emit);
353 return temp ? gen_rtx_CONST (to_mode, temp) : temp;
354
355 case PLUS:
356 case MULT:
357 /* For addition we can safely permute the conversion and addition
358 operation if one operand is a constant and converting the constant
359 does not change it or if one operand is a constant and we are
360 using a ptr_extend instruction (POINTERS_EXTEND_UNSIGNED < 0).
361 We can always safely permute them if we are making the address
362 narrower. Inside a CONST RTL, this is safe for both pointers
363 zero or sign extended as pointers cannot wrap. */
364 if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode)
365 || (GET_CODE (x) == PLUS
366 && CONST_INT_P (XEXP (x, 1))
367 && ((in_const && POINTERS_EXTEND_UNSIGNED != 0)
368 || XEXP (x, 1) == convert_memory_address_addr_space_1
369 (to_mode, XEXP (x, 1), as, in_const,
370 no_emit)
371 || POINTERS_EXTEND_UNSIGNED < 0)))
372 {
373 temp = convert_memory_address_addr_space_1 (to_mode, XEXP (x, 0),
374 as, in_const, no_emit);
375 return (temp ? gen_rtx_fmt_ee (GET_CODE (x), to_mode,
376 temp, XEXP (x, 1))
377 : temp);
378 }
379 break;
380
381 case UNSPEC:
382 /* Assume that all UNSPECs in a constant address can be converted
383 operand-by-operand. We could add a target hook if some targets
384 require different behavior. */
385 if (in_const && GET_MODE (x) == from_mode)
386 {
387 unsigned int n = XVECLEN (x, 0);
388 rtvec v = gen_rtvec (n);
389 for (unsigned int i = 0; i < n; ++i)
390 {
391 rtx op = XVECEXP (x, 0, i);
392 if (GET_MODE (op) == from_mode)
393 op = convert_memory_address_addr_space_1 (to_mode, op, as,
394 in_const, no_emit);
395 RTVEC_ELT (v, i) = op;
396 }
397 return gen_rtx_UNSPEC (to_mode, v, XINT (x, 1));
398 }
399 break;
400
401 default:
402 break;
403 }
404
405 if (no_emit)
406 return NULL_RTX;
407
408 return convert_modes (to_mode, from_mode,
409 x, POINTERS_EXTEND_UNSIGNED);
410 #endif /* defined(POINTERS_EXTEND_UNSIGNED) */
411 }
412
413 /* Given X, a memory address in address space AS' pointer mode, convert it to
414 an address in the address space's address mode, or vice versa (TO_MODE says
415 which way). We take advantage of the fact that pointers are not allowed to
416 overflow by commuting arithmetic operations over conversions so that address
417 arithmetic insns can be used. */
418
419 rtx
420 convert_memory_address_addr_space (scalar_int_mode to_mode, rtx x,
421 addr_space_t as)
422 {
423 return convert_memory_address_addr_space_1 (to_mode, x, as, false, false);
424 }
425 \f
426
427 /* Return something equivalent to X but valid as a memory address for something
428 of mode MODE in the named address space AS. When X is not itself valid,
429 this works by copying X or subexpressions of it into registers. */
430
431 rtx
432 memory_address_addr_space (machine_mode mode, rtx x, addr_space_t as)
433 {
434 rtx oldx = x;
435 scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
436
437 x = convert_memory_address_addr_space (address_mode, x, as);
438
439 /* By passing constant addresses through registers
440 we get a chance to cse them. */
441 if (! cse_not_expected && CONSTANT_P (x) && CONSTANT_ADDRESS_P (x))
442 x = force_reg (address_mode, x);
443
444 /* We get better cse by rejecting indirect addressing at this stage.
445 Let the combiner create indirect addresses where appropriate.
446 For now, generate the code so that the subexpressions useful to share
447 are visible. But not if cse won't be done! */
448 else
449 {
450 if (! cse_not_expected && !REG_P (x))
451 x = break_out_memory_refs (x);
452
453 /* At this point, any valid address is accepted. */
454 if (memory_address_addr_space_p (mode, x, as))
455 goto done;
456
457 /* If it was valid before but breaking out memory refs invalidated it,
458 use it the old way. */
459 if (memory_address_addr_space_p (mode, oldx, as))
460 {
461 x = oldx;
462 goto done;
463 }
464
465 /* Perform machine-dependent transformations on X
466 in certain cases. This is not necessary since the code
467 below can handle all possible cases, but machine-dependent
468 transformations can make better code. */
469 {
470 rtx orig_x = x;
471 x = targetm.addr_space.legitimize_address (x, oldx, mode, as);
472 if (orig_x != x && memory_address_addr_space_p (mode, x, as))
473 goto done;
474 }
475
476 /* PLUS and MULT can appear in special ways
477 as the result of attempts to make an address usable for indexing.
478 Usually they are dealt with by calling force_operand, below.
479 But a sum containing constant terms is special
480 if removing them makes the sum a valid address:
481 then we generate that address in a register
482 and index off of it. We do this because it often makes
483 shorter code, and because the addresses thus generated
484 in registers often become common subexpressions. */
485 if (GET_CODE (x) == PLUS)
486 {
487 rtx constant_term = const0_rtx;
488 rtx y = eliminate_constant_term (x, &constant_term);
489 if (constant_term == const0_rtx
490 || ! memory_address_addr_space_p (mode, y, as))
491 x = force_operand (x, NULL_RTX);
492 else
493 {
494 y = gen_rtx_PLUS (GET_MODE (x), copy_to_reg (y), constant_term);
495 if (! memory_address_addr_space_p (mode, y, as))
496 x = force_operand (x, NULL_RTX);
497 else
498 x = y;
499 }
500 }
501
502 else if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
503 x = force_operand (x, NULL_RTX);
504
505 /* If we have a register that's an invalid address,
506 it must be a hard reg of the wrong class. Copy it to a pseudo. */
507 else if (REG_P (x))
508 x = copy_to_reg (x);
509
510 /* Last resort: copy the value to a register, since
511 the register is a valid address. */
512 else
513 x = force_reg (address_mode, x);
514 }
515
516 done:
517
518 gcc_assert (memory_address_addr_space_p (mode, x, as));
519 /* If we didn't change the address, we are done. Otherwise, mark
520 a reg as a pointer if we have REG or REG + CONST_INT. */
521 if (oldx == x)
522 return x;
523 else if (REG_P (x))
524 mark_reg_pointer (x, BITS_PER_UNIT);
525 else if (GET_CODE (x) == PLUS
526 && REG_P (XEXP (x, 0))
527 && CONST_INT_P (XEXP (x, 1)))
528 mark_reg_pointer (XEXP (x, 0), BITS_PER_UNIT);
529
530 /* OLDX may have been the address on a temporary. Update the address
531 to indicate that X is now used. */
532 update_temp_slot_address (oldx, x);
533
534 return x;
535 }
536
537 /* Convert a mem ref into one with a valid memory address.
538 Pass through anything else unchanged. */
539
540 rtx
541 validize_mem (rtx ref)
542 {
543 if (!MEM_P (ref))
544 return ref;
545 ref = use_anchored_address (ref);
546 if (memory_address_addr_space_p (GET_MODE (ref), XEXP (ref, 0),
547 MEM_ADDR_SPACE (ref)))
548 return ref;
549
550 /* Don't alter REF itself, since that is probably a stack slot. */
551 return replace_equiv_address (ref, XEXP (ref, 0));
552 }
553
554 /* If X is a memory reference to a member of an object block, try rewriting
555 it to use an anchor instead. Return the new memory reference on success
556 and the old one on failure. */
557
558 rtx
559 use_anchored_address (rtx x)
560 {
561 rtx base;
562 HOST_WIDE_INT offset;
563 machine_mode mode;
564
565 if (!flag_section_anchors)
566 return x;
567
568 if (!MEM_P (x))
569 return x;
570
571 /* Split the address into a base and offset. */
572 base = XEXP (x, 0);
573 offset = 0;
574 if (GET_CODE (base) == CONST
575 && GET_CODE (XEXP (base, 0)) == PLUS
576 && CONST_INT_P (XEXP (XEXP (base, 0), 1)))
577 {
578 offset += INTVAL (XEXP (XEXP (base, 0), 1));
579 base = XEXP (XEXP (base, 0), 0);
580 }
581
582 /* Check whether BASE is suitable for anchors. */
583 if (GET_CODE (base) != SYMBOL_REF
584 || !SYMBOL_REF_HAS_BLOCK_INFO_P (base)
585 || SYMBOL_REF_ANCHOR_P (base)
586 || SYMBOL_REF_BLOCK (base) == NULL
587 || !targetm.use_anchors_for_symbol_p (base))
588 return x;
589
590 /* Decide where BASE is going to be. */
591 place_block_symbol (base);
592
593 /* Get the anchor we need to use. */
594 offset += SYMBOL_REF_BLOCK_OFFSET (base);
595 base = get_section_anchor (SYMBOL_REF_BLOCK (base), offset,
596 SYMBOL_REF_TLS_MODEL (base));
597
598 /* Work out the offset from the anchor. */
599 offset -= SYMBOL_REF_BLOCK_OFFSET (base);
600
601 /* If we're going to run a CSE pass, force the anchor into a register.
602 We will then be able to reuse registers for several accesses, if the
603 target costs say that that's worthwhile. */
604 mode = GET_MODE (base);
605 if (!cse_not_expected)
606 base = force_reg (mode, base);
607
608 return replace_equiv_address (x, plus_constant (mode, base, offset));
609 }
610 \f
611 /* Copy the value or contents of X to a new temp reg and return that reg. */
612
613 rtx
614 copy_to_reg (rtx x)
615 {
616 rtx temp = gen_reg_rtx (GET_MODE (x));
617
618 /* If not an operand, must be an address with PLUS and MULT so
619 do the computation. */
620 if (! general_operand (x, VOIDmode))
621 x = force_operand (x, temp);
622
623 if (x != temp)
624 emit_move_insn (temp, x);
625
626 return temp;
627 }
628
629 /* Like copy_to_reg but always give the new register mode Pmode
630 in case X is a constant. */
631
632 rtx
633 copy_addr_to_reg (rtx x)
634 {
635 return copy_to_mode_reg (Pmode, x);
636 }
637
638 /* Like copy_to_reg but always give the new register mode MODE
639 in case X is a constant. */
640
641 rtx
642 copy_to_mode_reg (machine_mode mode, rtx x)
643 {
644 rtx temp = gen_reg_rtx (mode);
645
646 /* If not an operand, must be an address with PLUS and MULT so
647 do the computation. */
648 if (! general_operand (x, VOIDmode))
649 x = force_operand (x, temp);
650
651 gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
652 if (x != temp)
653 emit_move_insn (temp, x);
654 return temp;
655 }
656
657 /* Load X into a register if it is not already one.
658 Use mode MODE for the register.
659 X should be valid for mode MODE, but it may be a constant which
660 is valid for all integer modes; that's why caller must specify MODE.
661
662 The caller must not alter the value in the register we return,
663 since we mark it as a "constant" register. */
664
665 rtx
666 force_reg (machine_mode mode, rtx x)
667 {
668 rtx temp, set;
669 rtx_insn *insn;
670
671 if (REG_P (x))
672 return x;
673
674 if (general_operand (x, mode))
675 {
676 temp = gen_reg_rtx (mode);
677 insn = emit_move_insn (temp, x);
678 }
679 else
680 {
681 temp = force_operand (x, NULL_RTX);
682 if (REG_P (temp))
683 insn = get_last_insn ();
684 else
685 {
686 rtx temp2 = gen_reg_rtx (mode);
687 insn = emit_move_insn (temp2, temp);
688 temp = temp2;
689 }
690 }
691
692 /* Let optimizers know that TEMP's value never changes
693 and that X can be substituted for it. Don't get confused
694 if INSN set something else (such as a SUBREG of TEMP). */
695 if (CONSTANT_P (x)
696 && (set = single_set (insn)) != 0
697 && SET_DEST (set) == temp
698 && ! rtx_equal_p (x, SET_SRC (set)))
699 set_unique_reg_note (insn, REG_EQUAL, x);
700
701 /* Let optimizers know that TEMP is a pointer, and if so, the
702 known alignment of that pointer. */
703 {
704 unsigned align = 0;
705 if (GET_CODE (x) == SYMBOL_REF)
706 {
707 align = BITS_PER_UNIT;
708 if (SYMBOL_REF_DECL (x) && DECL_P (SYMBOL_REF_DECL (x)))
709 align = DECL_ALIGN (SYMBOL_REF_DECL (x));
710 }
711 else if (GET_CODE (x) == LABEL_REF)
712 align = BITS_PER_UNIT;
713 else if (GET_CODE (x) == CONST
714 && GET_CODE (XEXP (x, 0)) == PLUS
715 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
716 && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
717 {
718 rtx s = XEXP (XEXP (x, 0), 0);
719 rtx c = XEXP (XEXP (x, 0), 1);
720 unsigned sa, ca;
721
722 sa = BITS_PER_UNIT;
723 if (SYMBOL_REF_DECL (s) && DECL_P (SYMBOL_REF_DECL (s)))
724 sa = DECL_ALIGN (SYMBOL_REF_DECL (s));
725
726 if (INTVAL (c) == 0)
727 align = sa;
728 else
729 {
730 ca = ctz_hwi (INTVAL (c)) * BITS_PER_UNIT;
731 align = MIN (sa, ca);
732 }
733 }
734
735 if (align || (MEM_P (x) && MEM_POINTER (x)))
736 mark_reg_pointer (temp, align);
737 }
738
739 return temp;
740 }
741
742 /* If X is a memory ref, copy its contents to a new temp reg and return
743 that reg. Otherwise, return X. */
744
745 rtx
746 force_not_mem (rtx x)
747 {
748 rtx temp;
749
750 if (!MEM_P (x) || GET_MODE (x) == BLKmode)
751 return x;
752
753 temp = gen_reg_rtx (GET_MODE (x));
754
755 if (MEM_POINTER (x))
756 REG_POINTER (temp) = 1;
757
758 emit_move_insn (temp, x);
759 return temp;
760 }
761
762 /* Copy X to TARGET (if it's nonzero and a reg)
763 or to a new temp reg and return that reg.
764 MODE is the mode to use for X in case it is a constant. */
765
766 rtx
767 copy_to_suggested_reg (rtx x, rtx target, machine_mode mode)
768 {
769 rtx temp;
770
771 if (target && REG_P (target))
772 temp = target;
773 else
774 temp = gen_reg_rtx (mode);
775
776 emit_move_insn (temp, x);
777 return temp;
778 }
779 \f
780 /* Return the mode to use to pass or return a scalar of TYPE and MODE.
781 PUNSIGNEDP points to the signedness of the type and may be adjusted
782 to show what signedness to use on extension operations.
783
784 FOR_RETURN is nonzero if the caller is promoting the return value
785 of FNDECL, else it is for promoting args. */
786
787 machine_mode
788 promote_function_mode (const_tree type, machine_mode mode, int *punsignedp,
789 const_tree funtype, int for_return)
790 {
791 /* Called without a type node for a libcall. */
792 if (type == NULL_TREE)
793 {
794 if (INTEGRAL_MODE_P (mode))
795 return targetm.calls.promote_function_mode (NULL_TREE, mode,
796 punsignedp, funtype,
797 for_return);
798 else
799 return mode;
800 }
801
802 switch (TREE_CODE (type))
803 {
804 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
805 case REAL_TYPE: case OFFSET_TYPE: case FIXED_POINT_TYPE:
806 case POINTER_TYPE: case REFERENCE_TYPE:
807 return targetm.calls.promote_function_mode (type, mode, punsignedp, funtype,
808 for_return);
809
810 default:
811 return mode;
812 }
813 }
814 /* Return the mode to use to store a scalar of TYPE and MODE.
815 PUNSIGNEDP points to the signedness of the type and may be adjusted
816 to show what signedness to use on extension operations. */
817
818 machine_mode
819 promote_mode (const_tree type ATTRIBUTE_UNUSED, machine_mode mode,
820 int *punsignedp ATTRIBUTE_UNUSED)
821 {
822 #ifdef PROMOTE_MODE
823 enum tree_code code;
824 int unsignedp;
825 scalar_mode smode;
826 #endif
827
828 /* For libcalls this is invoked without TYPE from the backends
829 TARGET_PROMOTE_FUNCTION_MODE hooks. Don't do anything in that
830 case. */
831 if (type == NULL_TREE)
832 return mode;
833
834 /* FIXME: this is the same logic that was there until GCC 4.4, but we
835 probably want to test POINTERS_EXTEND_UNSIGNED even if PROMOTE_MODE
836 is not defined. The affected targets are M32C, S390, SPARC. */
837 #ifdef PROMOTE_MODE
838 code = TREE_CODE (type);
839 unsignedp = *punsignedp;
840
841 switch (code)
842 {
843 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
844 case REAL_TYPE: case OFFSET_TYPE: case FIXED_POINT_TYPE:
845 /* Values of these types always have scalar mode. */
846 smode = as_a <scalar_mode> (mode);
847 PROMOTE_MODE (smode, unsignedp, type);
848 *punsignedp = unsignedp;
849 return smode;
850
851 #ifdef POINTERS_EXTEND_UNSIGNED
852 case REFERENCE_TYPE:
853 case POINTER_TYPE:
854 *punsignedp = POINTERS_EXTEND_UNSIGNED;
855 return targetm.addr_space.address_mode
856 (TYPE_ADDR_SPACE (TREE_TYPE (type)));
857 #endif
858
859 default:
860 return mode;
861 }
862 #else
863 return mode;
864 #endif
865 }
866
867
868 /* Use one of promote_mode or promote_function_mode to find the promoted
869 mode of DECL. If PUNSIGNEDP is not NULL, store there the unsignedness
870 of DECL after promotion. */
871
872 machine_mode
873 promote_decl_mode (const_tree decl, int *punsignedp)
874 {
875 tree type = TREE_TYPE (decl);
876 int unsignedp = TYPE_UNSIGNED (type);
877 machine_mode mode = DECL_MODE (decl);
878 machine_mode pmode;
879
880 if (TREE_CODE (decl) == RESULT_DECL && !DECL_BY_REFERENCE (decl))
881 pmode = promote_function_mode (type, mode, &unsignedp,
882 TREE_TYPE (current_function_decl), 1);
883 else if (TREE_CODE (decl) == RESULT_DECL || TREE_CODE (decl) == PARM_DECL)
884 pmode = promote_function_mode (type, mode, &unsignedp,
885 TREE_TYPE (current_function_decl), 2);
886 else
887 pmode = promote_mode (type, mode, &unsignedp);
888
889 if (punsignedp)
890 *punsignedp = unsignedp;
891 return pmode;
892 }
893
894 /* Return the promoted mode for name. If it is a named SSA_NAME, it
895 is the same as promote_decl_mode. Otherwise, it is the promoted
896 mode of a temp decl of same type as the SSA_NAME, if we had created
897 one. */
898
899 machine_mode
900 promote_ssa_mode (const_tree name, int *punsignedp)
901 {
902 gcc_assert (TREE_CODE (name) == SSA_NAME);
903
904 /* Partitions holding parms and results must be promoted as expected
905 by function.c. */
906 if (SSA_NAME_VAR (name)
907 && (TREE_CODE (SSA_NAME_VAR (name)) == PARM_DECL
908 || TREE_CODE (SSA_NAME_VAR (name)) == RESULT_DECL))
909 {
910 machine_mode mode = promote_decl_mode (SSA_NAME_VAR (name), punsignedp);
911 if (mode != BLKmode)
912 return mode;
913 }
914
915 tree type = TREE_TYPE (name);
916 int unsignedp = TYPE_UNSIGNED (type);
917 machine_mode pmode = promote_mode (type, TYPE_MODE (type), &unsignedp);
918 if (punsignedp)
919 *punsignedp = unsignedp;
920
921 return pmode;
922 }
923
924
925 \f
926 /* Controls the behavior of {anti_,}adjust_stack. */
927 static bool suppress_reg_args_size;
928
929 /* A helper for adjust_stack and anti_adjust_stack. */
930
931 static void
932 adjust_stack_1 (rtx adjust, bool anti_p)
933 {
934 rtx temp;
935 rtx_insn *insn;
936
937 /* Hereafter anti_p means subtract_p. */
938 if (!STACK_GROWS_DOWNWARD)
939 anti_p = !anti_p;
940
941 temp = expand_binop (Pmode,
942 anti_p ? sub_optab : add_optab,
943 stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
944 OPTAB_LIB_WIDEN);
945
946 if (temp != stack_pointer_rtx)
947 insn = emit_move_insn (stack_pointer_rtx, temp);
948 else
949 {
950 insn = get_last_insn ();
951 temp = single_set (insn);
952 gcc_assert (temp != NULL && SET_DEST (temp) == stack_pointer_rtx);
953 }
954
955 if (!suppress_reg_args_size)
956 add_args_size_note (insn, stack_pointer_delta);
957 }
958
959 /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
960 This pops when ADJUST is positive. ADJUST need not be constant. */
961
962 void
963 adjust_stack (rtx adjust)
964 {
965 if (adjust == const0_rtx)
966 return;
967
968 /* We expect all variable sized adjustments to be multiple of
969 PREFERRED_STACK_BOUNDARY. */
970 poly_int64 const_adjust;
971 if (poly_int_rtx_p (adjust, &const_adjust))
972 stack_pointer_delta -= const_adjust;
973
974 adjust_stack_1 (adjust, false);
975 }
976
977 /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
978 This pushes when ADJUST is positive. ADJUST need not be constant. */
979
980 void
981 anti_adjust_stack (rtx adjust)
982 {
983 if (adjust == const0_rtx)
984 return;
985
986 /* We expect all variable sized adjustments to be multiple of
987 PREFERRED_STACK_BOUNDARY. */
988 poly_int64 const_adjust;
989 if (poly_int_rtx_p (adjust, &const_adjust))
990 stack_pointer_delta += const_adjust;
991
992 adjust_stack_1 (adjust, true);
993 }
994
995 /* Round the size of a block to be pushed up to the boundary required
996 by this machine. SIZE is the desired size, which need not be constant. */
997
998 static rtx
999 round_push (rtx size)
1000 {
1001 rtx align_rtx, alignm1_rtx;
1002
1003 if (!SUPPORTS_STACK_ALIGNMENT
1004 || crtl->preferred_stack_boundary == MAX_SUPPORTED_STACK_ALIGNMENT)
1005 {
1006 int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
1007
1008 if (align == 1)
1009 return size;
1010
1011 if (CONST_INT_P (size))
1012 {
1013 HOST_WIDE_INT new_size = (INTVAL (size) + align - 1) / align * align;
1014
1015 if (INTVAL (size) != new_size)
1016 size = GEN_INT (new_size);
1017 return size;
1018 }
1019
1020 align_rtx = GEN_INT (align);
1021 alignm1_rtx = GEN_INT (align - 1);
1022 }
1023 else
1024 {
1025 /* If crtl->preferred_stack_boundary might still grow, use
1026 virtual_preferred_stack_boundary_rtx instead. This will be
1027 substituted by the right value in vregs pass and optimized
1028 during combine. */
1029 align_rtx = virtual_preferred_stack_boundary_rtx;
1030 alignm1_rtx = force_operand (plus_constant (Pmode, align_rtx, -1),
1031 NULL_RTX);
1032 }
1033
1034 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1035 but we know it can't. So add ourselves and then do
1036 TRUNC_DIV_EXPR. */
1037 size = expand_binop (Pmode, add_optab, size, alignm1_rtx,
1038 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1039 size = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, size, align_rtx,
1040 NULL_RTX, 1);
1041 size = expand_mult (Pmode, size, align_rtx, NULL_RTX, 1);
1042
1043 return size;
1044 }
1045 \f
1046 /* Save the stack pointer for the purpose in SAVE_LEVEL. PSAVE is a pointer
1047 to a previously-created save area. If no save area has been allocated,
1048 this function will allocate one. If a save area is specified, it
1049 must be of the proper mode. */
1050
1051 void
1052 emit_stack_save (enum save_level save_level, rtx *psave)
1053 {
1054 rtx sa = *psave;
1055 /* The default is that we use a move insn and save in a Pmode object. */
1056 rtx_insn *(*fcn) (rtx, rtx) = gen_move_insn;
1057 machine_mode mode = STACK_SAVEAREA_MODE (save_level);
1058
1059 /* See if this machine has anything special to do for this kind of save. */
1060 switch (save_level)
1061 {
1062 case SAVE_BLOCK:
1063 if (targetm.have_save_stack_block ())
1064 fcn = targetm.gen_save_stack_block;
1065 break;
1066 case SAVE_FUNCTION:
1067 if (targetm.have_save_stack_function ())
1068 fcn = targetm.gen_save_stack_function;
1069 break;
1070 case SAVE_NONLOCAL:
1071 if (targetm.have_save_stack_nonlocal ())
1072 fcn = targetm.gen_save_stack_nonlocal;
1073 break;
1074 default:
1075 break;
1076 }
1077
1078 /* If there is no save area and we have to allocate one, do so. Otherwise
1079 verify the save area is the proper mode. */
1080
1081 if (sa == 0)
1082 {
1083 if (mode != VOIDmode)
1084 {
1085 if (save_level == SAVE_NONLOCAL)
1086 *psave = sa = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
1087 else
1088 *psave = sa = gen_reg_rtx (mode);
1089 }
1090 }
1091
1092 do_pending_stack_adjust ();
1093 if (sa != 0)
1094 sa = validize_mem (sa);
1095 emit_insn (fcn (sa, stack_pointer_rtx));
1096 }
1097
1098 /* Restore the stack pointer for the purpose in SAVE_LEVEL. SA is the save
1099 area made by emit_stack_save. If it is zero, we have nothing to do. */
1100
1101 void
1102 emit_stack_restore (enum save_level save_level, rtx sa)
1103 {
1104 /* The default is that we use a move insn. */
1105 rtx_insn *(*fcn) (rtx, rtx) = gen_move_insn;
1106
1107 /* If stack_realign_drap, the x86 backend emits a prologue that aligns both
1108 STACK_POINTER and HARD_FRAME_POINTER.
1109 If stack_realign_fp, the x86 backend emits a prologue that aligns only
1110 STACK_POINTER. This renders the HARD_FRAME_POINTER unusable for accessing
1111 aligned variables, which is reflected in ix86_can_eliminate.
1112 We normally still have the realigned STACK_POINTER that we can use.
1113 But if there is a stack restore still present at reload, it can trigger
1114 mark_not_eliminable for the STACK_POINTER, leaving no way to eliminate
1115 FRAME_POINTER into a hard reg.
1116 To prevent this situation, we force need_drap if we emit a stack
1117 restore. */
1118 if (SUPPORTS_STACK_ALIGNMENT)
1119 crtl->need_drap = true;
1120
1121 /* See if this machine has anything special to do for this kind of save. */
1122 switch (save_level)
1123 {
1124 case SAVE_BLOCK:
1125 if (targetm.have_restore_stack_block ())
1126 fcn = targetm.gen_restore_stack_block;
1127 break;
1128 case SAVE_FUNCTION:
1129 if (targetm.have_restore_stack_function ())
1130 fcn = targetm.gen_restore_stack_function;
1131 break;
1132 case SAVE_NONLOCAL:
1133 if (targetm.have_restore_stack_nonlocal ())
1134 fcn = targetm.gen_restore_stack_nonlocal;
1135 break;
1136 default:
1137 break;
1138 }
1139
1140 if (sa != 0)
1141 {
1142 sa = validize_mem (sa);
1143 /* These clobbers prevent the scheduler from moving
1144 references to variable arrays below the code
1145 that deletes (pops) the arrays. */
1146 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1147 emit_clobber (gen_rtx_MEM (BLKmode, stack_pointer_rtx));
1148 }
1149
1150 discard_pending_stack_adjust ();
1151
1152 emit_insn (fcn (stack_pointer_rtx, sa));
1153 }
1154
1155 /* Invoke emit_stack_save on the nonlocal_goto_save_area for the current
1156 function. This should be called whenever we allocate or deallocate
1157 dynamic stack space. */
1158
1159 void
1160 update_nonlocal_goto_save_area (void)
1161 {
1162 tree t_save;
1163 rtx r_save;
1164
1165 /* The nonlocal_goto_save_area object is an array of N pointers. The
1166 first one is used for the frame pointer save; the rest are sized by
1167 STACK_SAVEAREA_MODE. Create a reference to array index 1, the first
1168 of the stack save area slots. */
1169 t_save = build4 (ARRAY_REF,
1170 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
1171 cfun->nonlocal_goto_save_area,
1172 integer_one_node, NULL_TREE, NULL_TREE);
1173 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
1174
1175 emit_stack_save (SAVE_NONLOCAL, &r_save);
1176 }
1177
1178 /* Record a new stack level for the current function. This should be called
1179 whenever we allocate or deallocate dynamic stack space. */
1180
1181 void
1182 record_new_stack_level (void)
1183 {
1184 /* Record the new stack level for nonlocal gotos. */
1185 if (cfun->nonlocal_goto_save_area)
1186 update_nonlocal_goto_save_area ();
1187
1188 /* Record the new stack level for SJLJ exceptions. */
1189 if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
1190 update_sjlj_context ();
1191 }
1192
1193 /* Return an rtx doing runtime alignment to REQUIRED_ALIGN on TARGET. */
1194
1195 rtx
1196 align_dynamic_address (rtx target, unsigned required_align)
1197 {
1198 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1199 but we know it can't. So add ourselves and then do
1200 TRUNC_DIV_EXPR. */
1201 target = expand_binop (Pmode, add_optab, target,
1202 gen_int_mode (required_align / BITS_PER_UNIT - 1,
1203 Pmode),
1204 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1205 target = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, target,
1206 gen_int_mode (required_align / BITS_PER_UNIT,
1207 Pmode),
1208 NULL_RTX, 1);
1209 target = expand_mult (Pmode, target,
1210 gen_int_mode (required_align / BITS_PER_UNIT,
1211 Pmode),
1212 NULL_RTX, 1);
1213
1214 return target;
1215 }
1216
1217 /* Return an rtx through *PSIZE, representing the size of an area of memory to
1218 be dynamically pushed on the stack.
1219
1220 *PSIZE is an rtx representing the size of the area.
1221
1222 SIZE_ALIGN is the alignment (in bits) that we know SIZE has. This
1223 parameter may be zero. If so, a proper value will be extracted
1224 from SIZE if it is constant, otherwise BITS_PER_UNIT will be assumed.
1225
1226 REQUIRED_ALIGN is the alignment (in bits) required for the region
1227 of memory.
1228
1229 If PSTACK_USAGE_SIZE is not NULL it points to a value that is increased for
1230 the additional size returned. */
1231 void
1232 get_dynamic_stack_size (rtx *psize, unsigned size_align,
1233 unsigned required_align,
1234 HOST_WIDE_INT *pstack_usage_size)
1235 {
1236 rtx size = *psize;
1237
1238 /* Ensure the size is in the proper mode. */
1239 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1240 size = convert_to_mode (Pmode, size, 1);
1241
1242 if (CONST_INT_P (size))
1243 {
1244 unsigned HOST_WIDE_INT lsb;
1245
1246 lsb = INTVAL (size);
1247 lsb &= -lsb;
1248
1249 /* Watch out for overflow truncating to "unsigned". */
1250 if (lsb > UINT_MAX / BITS_PER_UNIT)
1251 size_align = 1u << (HOST_BITS_PER_INT - 1);
1252 else
1253 size_align = (unsigned)lsb * BITS_PER_UNIT;
1254 }
1255 else if (size_align < BITS_PER_UNIT)
1256 size_align = BITS_PER_UNIT;
1257
1258 /* We can't attempt to minimize alignment necessary, because we don't
1259 know the final value of preferred_stack_boundary yet while executing
1260 this code. */
1261 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
1262 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
1263
1264 /* We will need to ensure that the address we return is aligned to
1265 REQUIRED_ALIGN. At this point in the compilation, we don't always
1266 know the final value of the STACK_DYNAMIC_OFFSET used in function.c
1267 (it might depend on the size of the outgoing parameter lists, for
1268 example), so we must preventively align the value. We leave space
1269 in SIZE for the hole that might result from the alignment operation. */
1270
1271 unsigned known_align = REGNO_POINTER_ALIGN (VIRTUAL_STACK_DYNAMIC_REGNUM);
1272 if (known_align == 0)
1273 known_align = BITS_PER_UNIT;
1274 if (required_align > known_align)
1275 {
1276 unsigned extra = (required_align - known_align) / BITS_PER_UNIT;
1277 size = plus_constant (Pmode, size, extra);
1278 size = force_operand (size, NULL_RTX);
1279 if (size_align > known_align)
1280 size_align = known_align;
1281
1282 if (flag_stack_usage_info && pstack_usage_size)
1283 *pstack_usage_size += extra;
1284 }
1285
1286 /* Round the size to a multiple of the required stack alignment.
1287 Since the stack is presumed to be rounded before this allocation,
1288 this will maintain the required alignment.
1289
1290 If the stack grows downward, we could save an insn by subtracting
1291 SIZE from the stack pointer and then aligning the stack pointer.
1292 The problem with this is that the stack pointer may be unaligned
1293 between the execution of the subtraction and alignment insns and
1294 some machines do not allow this. Even on those that do, some
1295 signal handlers malfunction if a signal should occur between those
1296 insns. Since this is an extremely rare event, we have no reliable
1297 way of knowing which systems have this problem. So we avoid even
1298 momentarily mis-aligning the stack. */
1299 if (size_align % MAX_SUPPORTED_STACK_ALIGNMENT != 0)
1300 {
1301 size = round_push (size);
1302
1303 if (flag_stack_usage_info && pstack_usage_size)
1304 {
1305 int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
1306 *pstack_usage_size =
1307 (*pstack_usage_size + align - 1) / align * align;
1308 }
1309 }
1310
1311 *psize = size;
1312 }
1313
1314 /* Return the number of bytes to "protect" on the stack for -fstack-check.
1315
1316 "protect" in the context of -fstack-check means how many bytes we need
1317 to always ensure are available on the stack; as a consequence, this is
1318 also how many bytes are first skipped when probing the stack.
1319
1320 On some targets we want to reuse the -fstack-check prologue support
1321 to give a degree of protection against stack clashing style attacks.
1322
1323 In that scenario we do not want to skip bytes before probing as that
1324 would render the stack clash protections useless.
1325
1326 So we never use STACK_CHECK_PROTECT directly. Instead we indirectly
1327 use it through this helper, which allows to provide different values
1328 for -fstack-check and -fstack-clash-protection. */
1329
1330 HOST_WIDE_INT
1331 get_stack_check_protect (void)
1332 {
1333 if (flag_stack_clash_protection)
1334 return 0;
1335
1336 return STACK_CHECK_PROTECT;
1337 }
1338
1339 /* Return an rtx representing the address of an area of memory dynamically
1340 pushed on the stack.
1341
1342 Any required stack pointer alignment is preserved.
1343
1344 SIZE is an rtx representing the size of the area.
1345
1346 SIZE_ALIGN is the alignment (in bits) that we know SIZE has. This
1347 parameter may be zero. If so, a proper value will be extracted
1348 from SIZE if it is constant, otherwise BITS_PER_UNIT will be assumed.
1349
1350 REQUIRED_ALIGN is the alignment (in bits) required for the region
1351 of memory.
1352
1353 MAX_SIZE is an upper bound for SIZE, if SIZE is not constant, or -1 if
1354 no such upper bound is known.
1355
1356 If CANNOT_ACCUMULATE is set to TRUE, the caller guarantees that the
1357 stack space allocated by the generated code cannot be added with itself
1358 in the course of the execution of the function. It is always safe to
1359 pass FALSE here and the following criterion is sufficient in order to
1360 pass TRUE: every path in the CFG that starts at the allocation point and
1361 loops to it executes the associated deallocation code. */
1362
1363 rtx
1364 allocate_dynamic_stack_space (rtx size, unsigned size_align,
1365 unsigned required_align,
1366 HOST_WIDE_INT max_size,
1367 bool cannot_accumulate)
1368 {
1369 HOST_WIDE_INT stack_usage_size = -1;
1370 rtx_code_label *final_label;
1371 rtx final_target, target;
1372
1373 /* If we're asking for zero bytes, it doesn't matter what we point
1374 to since we can't dereference it. But return a reasonable
1375 address anyway. */
1376 if (size == const0_rtx)
1377 return virtual_stack_dynamic_rtx;
1378
1379 /* Otherwise, show we're calling alloca or equivalent. */
1380 cfun->calls_alloca = 1;
1381
1382 /* If stack usage info is requested, look into the size we are passed.
1383 We need to do so this early to avoid the obfuscation that may be
1384 introduced later by the various alignment operations. */
1385 if (flag_stack_usage_info)
1386 {
1387 if (CONST_INT_P (size))
1388 stack_usage_size = INTVAL (size);
1389 else if (REG_P (size))
1390 {
1391 /* Look into the last emitted insn and see if we can deduce
1392 something for the register. */
1393 rtx_insn *insn;
1394 rtx set, note;
1395 insn = get_last_insn ();
1396 if ((set = single_set (insn)) && rtx_equal_p (SET_DEST (set), size))
1397 {
1398 if (CONST_INT_P (SET_SRC (set)))
1399 stack_usage_size = INTVAL (SET_SRC (set));
1400 else if ((note = find_reg_equal_equiv_note (insn))
1401 && CONST_INT_P (XEXP (note, 0)))
1402 stack_usage_size = INTVAL (XEXP (note, 0));
1403 }
1404 }
1405
1406 /* If the size is not constant, try the maximum size. */
1407 if (stack_usage_size < 0)
1408 stack_usage_size = max_size;
1409
1410 /* If the size is still not constant, we can't say anything. */
1411 if (stack_usage_size < 0)
1412 {
1413 current_function_has_unbounded_dynamic_stack_size = 1;
1414 stack_usage_size = 0;
1415 }
1416 }
1417
1418 get_dynamic_stack_size (&size, size_align, required_align, &stack_usage_size);
1419
1420 target = gen_reg_rtx (Pmode);
1421
1422 /* The size is supposed to be fully adjusted at this point so record it
1423 if stack usage info is requested. */
1424 if (flag_stack_usage_info)
1425 {
1426 current_function_dynamic_stack_size += stack_usage_size;
1427
1428 /* ??? This is gross but the only safe stance in the absence
1429 of stack usage oriented flow analysis. */
1430 if (!cannot_accumulate)
1431 current_function_has_unbounded_dynamic_stack_size = 1;
1432 }
1433
1434 do_pending_stack_adjust ();
1435
1436 final_label = NULL;
1437 final_target = NULL_RTX;
1438
1439 /* If we are splitting the stack, we need to ask the backend whether
1440 there is enough room on the current stack. If there isn't, or if
1441 the backend doesn't know how to tell is, then we need to call a
1442 function to allocate memory in some other way. This memory will
1443 be released when we release the current stack segment. The
1444 effect is that stack allocation becomes less efficient, but at
1445 least it doesn't cause a stack overflow. */
1446 if (flag_split_stack)
1447 {
1448 rtx_code_label *available_label;
1449 rtx ask, space, func;
1450
1451 available_label = NULL;
1452
1453 if (targetm.have_split_stack_space_check ())
1454 {
1455 available_label = gen_label_rtx ();
1456
1457 /* This instruction will branch to AVAILABLE_LABEL if there
1458 are SIZE bytes available on the stack. */
1459 emit_insn (targetm.gen_split_stack_space_check
1460 (size, available_label));
1461 }
1462
1463 /* The __morestack_allocate_stack_space function will allocate
1464 memory using malloc. If the alignment of the memory returned
1465 by malloc does not meet REQUIRED_ALIGN, we increase SIZE to
1466 make sure we allocate enough space. */
1467 if (MALLOC_ABI_ALIGNMENT >= required_align)
1468 ask = size;
1469 else
1470 ask = expand_binop (Pmode, add_optab, size,
1471 gen_int_mode (required_align / BITS_PER_UNIT - 1,
1472 Pmode),
1473 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1474
1475 func = init_one_libfunc ("__morestack_allocate_stack_space");
1476
1477 space = emit_library_call_value (func, target, LCT_NORMAL, Pmode,
1478 ask, Pmode);
1479
1480 if (available_label == NULL_RTX)
1481 return space;
1482
1483 final_target = gen_reg_rtx (Pmode);
1484
1485 emit_move_insn (final_target, space);
1486
1487 final_label = gen_label_rtx ();
1488 emit_jump (final_label);
1489
1490 emit_label (available_label);
1491 }
1492
1493 /* We ought to be called always on the toplevel and stack ought to be aligned
1494 properly. */
1495 gcc_assert (multiple_p (stack_pointer_delta,
1496 PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT));
1497
1498 /* If needed, check that we have the required amount of stack. Take into
1499 account what has already been checked. */
1500 if (STACK_CHECK_MOVING_SP)
1501 ;
1502 else if (flag_stack_check == GENERIC_STACK_CHECK)
1503 probe_stack_range (STACK_OLD_CHECK_PROTECT + STACK_CHECK_MAX_FRAME_SIZE,
1504 size);
1505 else if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
1506 probe_stack_range (get_stack_check_protect (), size);
1507
1508 /* Don't let anti_adjust_stack emit notes. */
1509 suppress_reg_args_size = true;
1510
1511 /* Perform the required allocation from the stack. Some systems do
1512 this differently than simply incrementing/decrementing from the
1513 stack pointer, such as acquiring the space by calling malloc(). */
1514 if (targetm.have_allocate_stack ())
1515 {
1516 class expand_operand ops[2];
1517 /* We don't have to check against the predicate for operand 0 since
1518 TARGET is known to be a pseudo of the proper mode, which must
1519 be valid for the operand. */
1520 create_fixed_operand (&ops[0], target);
1521 create_convert_operand_to (&ops[1], size, STACK_SIZE_MODE, true);
1522 expand_insn (targetm.code_for_allocate_stack, 2, ops);
1523 }
1524 else
1525 {
1526 poly_int64 saved_stack_pointer_delta;
1527
1528 if (!STACK_GROWS_DOWNWARD)
1529 emit_move_insn (target, virtual_stack_dynamic_rtx);
1530
1531 /* Check stack bounds if necessary. */
1532 if (crtl->limit_stack)
1533 {
1534 rtx available;
1535 rtx_code_label *space_available = gen_label_rtx ();
1536 if (STACK_GROWS_DOWNWARD)
1537 available = expand_binop (Pmode, sub_optab,
1538 stack_pointer_rtx, stack_limit_rtx,
1539 NULL_RTX, 1, OPTAB_WIDEN);
1540 else
1541 available = expand_binop (Pmode, sub_optab,
1542 stack_limit_rtx, stack_pointer_rtx,
1543 NULL_RTX, 1, OPTAB_WIDEN);
1544
1545 emit_cmp_and_jump_insns (available, size, GEU, NULL_RTX, Pmode, 1,
1546 space_available);
1547 if (targetm.have_trap ())
1548 emit_insn (targetm.gen_trap ());
1549 else
1550 error ("stack limits not supported on this target");
1551 emit_barrier ();
1552 emit_label (space_available);
1553 }
1554
1555 saved_stack_pointer_delta = stack_pointer_delta;
1556
1557 /* If stack checking or stack clash protection is requested,
1558 then probe the stack while allocating space from it. */
1559 if (flag_stack_check && STACK_CHECK_MOVING_SP)
1560 anti_adjust_stack_and_probe (size, false);
1561 else if (flag_stack_clash_protection)
1562 anti_adjust_stack_and_probe_stack_clash (size);
1563 else
1564 anti_adjust_stack (size);
1565
1566 /* Even if size is constant, don't modify stack_pointer_delta.
1567 The constant size alloca should preserve
1568 crtl->preferred_stack_boundary alignment. */
1569 stack_pointer_delta = saved_stack_pointer_delta;
1570
1571 if (STACK_GROWS_DOWNWARD)
1572 emit_move_insn (target, virtual_stack_dynamic_rtx);
1573 }
1574
1575 suppress_reg_args_size = false;
1576
1577 /* Finish up the split stack handling. */
1578 if (final_label != NULL_RTX)
1579 {
1580 gcc_assert (flag_split_stack);
1581 emit_move_insn (final_target, target);
1582 emit_label (final_label);
1583 target = final_target;
1584 }
1585
1586 target = align_dynamic_address (target, required_align);
1587
1588 /* Now that we've committed to a return value, mark its alignment. */
1589 mark_reg_pointer (target, required_align);
1590
1591 /* Record the new stack level. */
1592 record_new_stack_level ();
1593
1594 return target;
1595 }
1596
1597 /* Return an rtx representing the address of an area of memory already
1598 statically pushed onto the stack in the virtual stack vars area. (It is
1599 assumed that the area is allocated in the function prologue.)
1600
1601 Any required stack pointer alignment is preserved.
1602
1603 OFFSET is the offset of the area into the virtual stack vars area.
1604
1605 REQUIRED_ALIGN is the alignment (in bits) required for the region
1606 of memory.
1607
1608 BASE is the rtx of the base of this virtual stack vars area.
1609 The only time this is not `virtual_stack_vars_rtx` is when tagging pointers
1610 on the stack. */
1611
1612 rtx
1613 get_dynamic_stack_base (poly_int64 offset, unsigned required_align, rtx base)
1614 {
1615 rtx target;
1616
1617 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
1618 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
1619
1620 target = gen_reg_rtx (Pmode);
1621 emit_move_insn (target, base);
1622 target = expand_binop (Pmode, add_optab, target,
1623 gen_int_mode (offset, Pmode),
1624 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1625 target = align_dynamic_address (target, required_align);
1626
1627 /* Now that we've committed to a return value, mark its alignment. */
1628 mark_reg_pointer (target, required_align);
1629
1630 return target;
1631 }
1632 \f
1633 /* A front end may want to override GCC's stack checking by providing a
1634 run-time routine to call to check the stack, so provide a mechanism for
1635 calling that routine. */
1636
1637 static GTY(()) rtx stack_check_libfunc;
1638
1639 void
1640 set_stack_check_libfunc (const char *libfunc_name)
1641 {
1642 gcc_assert (stack_check_libfunc == NULL_RTX);
1643 stack_check_libfunc = gen_rtx_SYMBOL_REF (Pmode, libfunc_name);
1644 tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
1645 get_identifier (libfunc_name), void_type_node);
1646 DECL_EXTERNAL (decl) = 1;
1647 SET_SYMBOL_REF_DECL (stack_check_libfunc, decl);
1648 }
1649 \f
1650 /* Emit one stack probe at ADDRESS, an address within the stack. */
1651
1652 void
1653 emit_stack_probe (rtx address)
1654 {
1655 if (targetm.have_probe_stack_address ())
1656 {
1657 class expand_operand ops[1];
1658 insn_code icode = targetm.code_for_probe_stack_address;
1659 create_address_operand (ops, address);
1660 maybe_legitimize_operands (icode, 0, 1, ops);
1661 expand_insn (icode, 1, ops);
1662 }
1663 else
1664 {
1665 rtx memref = gen_rtx_MEM (word_mode, address);
1666
1667 MEM_VOLATILE_P (memref) = 1;
1668 memref = validize_mem (memref);
1669
1670 /* See if we have an insn to probe the stack. */
1671 if (targetm.have_probe_stack ())
1672 emit_insn (targetm.gen_probe_stack (memref));
1673 else
1674 emit_move_insn (memref, const0_rtx);
1675 }
1676 }
1677
1678 /* Probe a range of stack addresses from FIRST to FIRST+SIZE, inclusive.
1679 FIRST is a constant and size is a Pmode RTX. These are offsets from
1680 the current stack pointer. STACK_GROWS_DOWNWARD says whether to add
1681 or subtract them from the stack pointer. */
1682
1683 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
1684
1685 #if STACK_GROWS_DOWNWARD
1686 #define STACK_GROW_OP MINUS
1687 #define STACK_GROW_OPTAB sub_optab
1688 #define STACK_GROW_OFF(off) -(off)
1689 #else
1690 #define STACK_GROW_OP PLUS
1691 #define STACK_GROW_OPTAB add_optab
1692 #define STACK_GROW_OFF(off) (off)
1693 #endif
1694
1695 void
1696 probe_stack_range (HOST_WIDE_INT first, rtx size)
1697 {
1698 /* First ensure SIZE is Pmode. */
1699 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1700 size = convert_to_mode (Pmode, size, 1);
1701
1702 /* Next see if we have a function to check the stack. */
1703 if (stack_check_libfunc)
1704 {
1705 rtx addr = memory_address (Pmode,
1706 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1707 stack_pointer_rtx,
1708 plus_constant (Pmode,
1709 size, first)));
1710 emit_library_call (stack_check_libfunc, LCT_THROW, VOIDmode,
1711 addr, Pmode);
1712 }
1713
1714 /* Next see if we have an insn to check the stack. */
1715 else if (targetm.have_check_stack ())
1716 {
1717 class expand_operand ops[1];
1718 rtx addr = memory_address (Pmode,
1719 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1720 stack_pointer_rtx,
1721 plus_constant (Pmode,
1722 size, first)));
1723 bool success;
1724 create_input_operand (&ops[0], addr, Pmode);
1725 success = maybe_expand_insn (targetm.code_for_check_stack, 1, ops);
1726 gcc_assert (success);
1727 }
1728
1729 /* Otherwise we have to generate explicit probes. If we have a constant
1730 small number of them to generate, that's the easy case. */
1731 else if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
1732 {
1733 HOST_WIDE_INT isize = INTVAL (size), i;
1734 rtx addr;
1735
1736 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
1737 it exceeds SIZE. If only one probe is needed, this will not
1738 generate any code. Then probe at FIRST + SIZE. */
1739 for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
1740 {
1741 addr = memory_address (Pmode,
1742 plus_constant (Pmode, stack_pointer_rtx,
1743 STACK_GROW_OFF (first + i)));
1744 emit_stack_probe (addr);
1745 }
1746
1747 addr = memory_address (Pmode,
1748 plus_constant (Pmode, stack_pointer_rtx,
1749 STACK_GROW_OFF (first + isize)));
1750 emit_stack_probe (addr);
1751 }
1752
1753 /* In the variable case, do the same as above, but in a loop. Note that we
1754 must be extra careful with variables wrapping around because we might be
1755 at the very top (or the very bottom) of the address space and we have to
1756 be able to handle this case properly; in particular, we use an equality
1757 test for the loop condition. */
1758 else
1759 {
1760 rtx rounded_size, rounded_size_op, test_addr, last_addr, temp;
1761 rtx_code_label *loop_lab = gen_label_rtx ();
1762 rtx_code_label *end_lab = gen_label_rtx ();
1763
1764 /* Step 1: round SIZE to the previous multiple of the interval. */
1765
1766 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1767 rounded_size
1768 = simplify_gen_binary (AND, Pmode, size,
1769 gen_int_mode (-PROBE_INTERVAL, Pmode));
1770 rounded_size_op = force_operand (rounded_size, NULL_RTX);
1771
1772
1773 /* Step 2: compute initial and final value of the loop counter. */
1774
1775 /* TEST_ADDR = SP + FIRST. */
1776 test_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1777 stack_pointer_rtx,
1778 gen_int_mode (first, Pmode)),
1779 NULL_RTX);
1780
1781 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
1782 last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1783 test_addr,
1784 rounded_size_op), NULL_RTX);
1785
1786
1787 /* Step 3: the loop
1788
1789 while (TEST_ADDR != LAST_ADDR)
1790 {
1791 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
1792 probe at TEST_ADDR
1793 }
1794
1795 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
1796 until it is equal to ROUNDED_SIZE. */
1797
1798 emit_label (loop_lab);
1799
1800 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
1801 emit_cmp_and_jump_insns (test_addr, last_addr, EQ, NULL_RTX, Pmode, 1,
1802 end_lab);
1803
1804 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
1805 temp = expand_binop (Pmode, STACK_GROW_OPTAB, test_addr,
1806 gen_int_mode (PROBE_INTERVAL, Pmode), test_addr,
1807 1, OPTAB_WIDEN);
1808
1809 gcc_assert (temp == test_addr);
1810
1811 /* Probe at TEST_ADDR. */
1812 emit_stack_probe (test_addr);
1813
1814 emit_jump (loop_lab);
1815
1816 emit_label (end_lab);
1817
1818
1819 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
1820 that SIZE is equal to ROUNDED_SIZE. */
1821
1822 /* TEMP = SIZE - ROUNDED_SIZE. */
1823 temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
1824 if (temp != const0_rtx)
1825 {
1826 rtx addr;
1827
1828 if (CONST_INT_P (temp))
1829 {
1830 /* Use [base + disp} addressing mode if supported. */
1831 HOST_WIDE_INT offset = INTVAL (temp);
1832 addr = memory_address (Pmode,
1833 plus_constant (Pmode, last_addr,
1834 STACK_GROW_OFF (offset)));
1835 }
1836 else
1837 {
1838 /* Manual CSE if the difference is not known at compile-time. */
1839 temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
1840 addr = memory_address (Pmode,
1841 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1842 last_addr, temp));
1843 }
1844
1845 emit_stack_probe (addr);
1846 }
1847 }
1848
1849 /* Make sure nothing is scheduled before we are done. */
1850 emit_insn (gen_blockage ());
1851 }
1852
1853 /* Compute parameters for stack clash probing a dynamic stack
1854 allocation of SIZE bytes.
1855
1856 We compute ROUNDED_SIZE, LAST_ADDR, RESIDUAL and PROBE_INTERVAL.
1857
1858 Additionally we conditionally dump the type of probing that will
1859 be needed given the values computed. */
1860
1861 void
1862 compute_stack_clash_protection_loop_data (rtx *rounded_size, rtx *last_addr,
1863 rtx *residual,
1864 HOST_WIDE_INT *probe_interval,
1865 rtx size)
1866 {
1867 /* Round SIZE down to STACK_CLASH_PROTECTION_PROBE_INTERVAL */
1868 *probe_interval
1869 = 1 << param_stack_clash_protection_probe_interval;
1870 *rounded_size = simplify_gen_binary (AND, Pmode, size,
1871 GEN_INT (-*probe_interval));
1872
1873 /* Compute the value of the stack pointer for the last iteration.
1874 It's just SP + ROUNDED_SIZE. */
1875 rtx rounded_size_op = force_operand (*rounded_size, NULL_RTX);
1876 *last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1877 stack_pointer_rtx,
1878 rounded_size_op),
1879 NULL_RTX);
1880
1881 /* Compute any residuals not allocated by the loop above. Residuals
1882 are just the ROUNDED_SIZE - SIZE. */
1883 *residual = simplify_gen_binary (MINUS, Pmode, size, *rounded_size);
1884
1885 /* Dump key information to make writing tests easy. */
1886 if (dump_file)
1887 {
1888 if (*rounded_size == CONST0_RTX (Pmode))
1889 fprintf (dump_file,
1890 "Stack clash skipped dynamic allocation and probing loop.\n");
1891 else if (CONST_INT_P (*rounded_size)
1892 && INTVAL (*rounded_size) <= 4 * *probe_interval)
1893 fprintf (dump_file,
1894 "Stack clash dynamic allocation and probing inline.\n");
1895 else if (CONST_INT_P (*rounded_size))
1896 fprintf (dump_file,
1897 "Stack clash dynamic allocation and probing in "
1898 "rotated loop.\n");
1899 else
1900 fprintf (dump_file,
1901 "Stack clash dynamic allocation and probing in loop.\n");
1902
1903 if (*residual != CONST0_RTX (Pmode))
1904 fprintf (dump_file,
1905 "Stack clash dynamic allocation and probing residuals.\n");
1906 else
1907 fprintf (dump_file,
1908 "Stack clash skipped dynamic allocation and "
1909 "probing residuals.\n");
1910 }
1911 }
1912
1913 /* Emit the start of an allocate/probe loop for stack
1914 clash protection.
1915
1916 LOOP_LAB and END_LAB are returned for use when we emit the
1917 end of the loop.
1918
1919 LAST addr is the value for SP which stops the loop. */
1920 void
1921 emit_stack_clash_protection_probe_loop_start (rtx *loop_lab,
1922 rtx *end_lab,
1923 rtx last_addr,
1924 bool rotated)
1925 {
1926 /* Essentially we want to emit any setup code, the top of loop
1927 label and the comparison at the top of the loop. */
1928 *loop_lab = gen_label_rtx ();
1929 *end_lab = gen_label_rtx ();
1930
1931 emit_label (*loop_lab);
1932 if (!rotated)
1933 emit_cmp_and_jump_insns (stack_pointer_rtx, last_addr, EQ, NULL_RTX,
1934 Pmode, 1, *end_lab);
1935 }
1936
1937 /* Emit the end of a stack clash probing loop.
1938
1939 This consists of just the jump back to LOOP_LAB and
1940 emitting END_LOOP after the loop. */
1941
1942 void
1943 emit_stack_clash_protection_probe_loop_end (rtx loop_lab, rtx end_loop,
1944 rtx last_addr, bool rotated)
1945 {
1946 if (rotated)
1947 emit_cmp_and_jump_insns (stack_pointer_rtx, last_addr, NE, NULL_RTX,
1948 Pmode, 1, loop_lab);
1949 else
1950 emit_jump (loop_lab);
1951
1952 emit_label (end_loop);
1953
1954 }
1955
1956 /* Adjust the stack pointer by minus SIZE (an rtx for a number of bytes)
1957 while probing it. This pushes when SIZE is positive. SIZE need not
1958 be constant.
1959
1960 This is subtly different than anti_adjust_stack_and_probe to try and
1961 prevent stack-clash attacks
1962
1963 1. It must assume no knowledge of the probing state, any allocation
1964 must probe.
1965
1966 Consider the case of a 1 byte alloca in a loop. If the sum of the
1967 allocations is large, then this could be used to jump the guard if
1968 probes were not emitted.
1969
1970 2. It never skips probes, whereas anti_adjust_stack_and_probe will
1971 skip the probe on the first PROBE_INTERVAL on the assumption it
1972 was already done in the prologue and in previous allocations.
1973
1974 3. It only allocates and probes SIZE bytes, it does not need to
1975 allocate/probe beyond that because this probing style does not
1976 guarantee signal handling capability if the guard is hit. */
1977
1978 void
1979 anti_adjust_stack_and_probe_stack_clash (rtx size)
1980 {
1981 /* First ensure SIZE is Pmode. */
1982 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1983 size = convert_to_mode (Pmode, size, 1);
1984
1985 /* We can get here with a constant size on some targets. */
1986 rtx rounded_size, last_addr, residual;
1987 HOST_WIDE_INT probe_interval, probe_range;
1988 bool target_probe_range_p = false;
1989 compute_stack_clash_protection_loop_data (&rounded_size, &last_addr,
1990 &residual, &probe_interval, size);
1991
1992 /* Get the back-end specific probe ranges. */
1993 probe_range = targetm.stack_clash_protection_alloca_probe_range ();
1994 target_probe_range_p = probe_range != 0;
1995 gcc_assert (probe_range >= 0);
1996
1997 /* If no back-end specific range defined, default to the top of the newly
1998 allocated range. */
1999 if (probe_range == 0)
2000 probe_range = probe_interval - GET_MODE_SIZE (word_mode);
2001
2002 if (rounded_size != CONST0_RTX (Pmode))
2003 {
2004 if (CONST_INT_P (rounded_size)
2005 && INTVAL (rounded_size) <= 4 * probe_interval)
2006 {
2007 for (HOST_WIDE_INT i = 0;
2008 i < INTVAL (rounded_size);
2009 i += probe_interval)
2010 {
2011 anti_adjust_stack (GEN_INT (probe_interval));
2012 /* The prologue does not probe residuals. Thus the offset
2013 here to probe just beyond what the prologue had already
2014 allocated. */
2015 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
2016 probe_range));
2017
2018 emit_insn (gen_blockage ());
2019 }
2020 }
2021 else
2022 {
2023 rtx loop_lab, end_loop;
2024 bool rotate_loop = CONST_INT_P (rounded_size);
2025 emit_stack_clash_protection_probe_loop_start (&loop_lab, &end_loop,
2026 last_addr, rotate_loop);
2027
2028 anti_adjust_stack (GEN_INT (probe_interval));
2029
2030 /* The prologue does not probe residuals. Thus the offset here
2031 to probe just beyond what the prologue had already
2032 allocated. */
2033 emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
2034 probe_range));
2035
2036 emit_stack_clash_protection_probe_loop_end (loop_lab, end_loop,
2037 last_addr, rotate_loop);
2038 emit_insn (gen_blockage ());
2039 }
2040 }
2041
2042 if (residual != CONST0_RTX (Pmode))
2043 {
2044 rtx label = NULL_RTX;
2045 /* RESIDUAL could be zero at runtime and in that case *sp could
2046 hold live data. Furthermore, we do not want to probe into the
2047 red zone.
2048
2049 If TARGET_PROBE_RANGE_P then the target has promised it's safe to
2050 probe at offset 0. In which case we no longer have to check for
2051 RESIDUAL == 0. However we still need to probe at the right offset
2052 when RESIDUAL > PROBE_RANGE, in which case we probe at PROBE_RANGE.
2053
2054 If !TARGET_PROBE_RANGE_P then go ahead and just guard the probe at *sp
2055 on RESIDUAL != 0 at runtime if RESIDUAL is not a compile time constant.
2056 */
2057 anti_adjust_stack (residual);
2058
2059 if (!CONST_INT_P (residual))
2060 {
2061 label = gen_label_rtx ();
2062 rtx_code op = target_probe_range_p ? LT : EQ;
2063 rtx probe_cmp_value = target_probe_range_p
2064 ? gen_rtx_CONST_INT (GET_MODE (residual), probe_range)
2065 : CONST0_RTX (GET_MODE (residual));
2066
2067 if (target_probe_range_p)
2068 emit_stack_probe (stack_pointer_rtx);
2069
2070 emit_cmp_and_jump_insns (residual, probe_cmp_value,
2071 op, NULL_RTX, Pmode, 1, label);
2072 }
2073
2074 rtx x = NULL_RTX;
2075
2076 /* If RESIDUAL isn't a constant and TARGET_PROBE_RANGE_P then we probe up
2077 by the ABI defined safe value. */
2078 if (!CONST_INT_P (residual) && target_probe_range_p)
2079 x = GEN_INT (probe_range);
2080 /* If RESIDUAL is a constant but smaller than the ABI defined safe value,
2081 we still want to probe up, but the safest amount if a word. */
2082 else if (target_probe_range_p)
2083 {
2084 if (INTVAL (residual) <= probe_range)
2085 x = GEN_INT (GET_MODE_SIZE (word_mode));
2086 else
2087 x = GEN_INT (probe_range);
2088 }
2089 else
2090 /* If nothing else, probe at the top of the new allocation. */
2091 x = plus_constant (Pmode, residual, -GET_MODE_SIZE (word_mode));
2092
2093 emit_stack_probe (gen_rtx_PLUS (Pmode, stack_pointer_rtx, x));
2094
2095 emit_insn (gen_blockage ());
2096 if (!CONST_INT_P (residual))
2097 emit_label (label);
2098 }
2099 }
2100
2101
2102 /* Adjust the stack pointer by minus SIZE (an rtx for a number of bytes)
2103 while probing it. This pushes when SIZE is positive. SIZE need not
2104 be constant. If ADJUST_BACK is true, adjust back the stack pointer
2105 by plus SIZE at the end. */
2106
2107 void
2108 anti_adjust_stack_and_probe (rtx size, bool adjust_back)
2109 {
2110 /* We skip the probe for the first interval + a small dope of 4 words and
2111 probe that many bytes past the specified size to maintain a protection
2112 area at the botton of the stack. */
2113 const int dope = 4 * UNITS_PER_WORD;
2114
2115 /* First ensure SIZE is Pmode. */
2116 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
2117 size = convert_to_mode (Pmode, size, 1);
2118
2119 /* If we have a constant small number of probes to generate, that's the
2120 easy case. */
2121 if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
2122 {
2123 HOST_WIDE_INT isize = INTVAL (size), i;
2124 bool first_probe = true;
2125
2126 /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
2127 values of N from 1 until it exceeds SIZE. If only one probe is
2128 needed, this will not generate any code. Then adjust and probe
2129 to PROBE_INTERVAL + SIZE. */
2130 for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
2131 {
2132 if (first_probe)
2133 {
2134 anti_adjust_stack (GEN_INT (2 * PROBE_INTERVAL + dope));
2135 first_probe = false;
2136 }
2137 else
2138 anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
2139 emit_stack_probe (stack_pointer_rtx);
2140 }
2141
2142 if (first_probe)
2143 anti_adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL + dope));
2144 else
2145 anti_adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL - i));
2146 emit_stack_probe (stack_pointer_rtx);
2147 }
2148
2149 /* In the variable case, do the same as above, but in a loop. Note that we
2150 must be extra careful with variables wrapping around because we might be
2151 at the very top (or the very bottom) of the address space and we have to
2152 be able to handle this case properly; in particular, we use an equality
2153 test for the loop condition. */
2154 else
2155 {
2156 rtx rounded_size, rounded_size_op, last_addr, temp;
2157 rtx_code_label *loop_lab = gen_label_rtx ();
2158 rtx_code_label *end_lab = gen_label_rtx ();
2159
2160
2161 /* Step 1: round SIZE to the previous multiple of the interval. */
2162
2163 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
2164 rounded_size
2165 = simplify_gen_binary (AND, Pmode, size,
2166 gen_int_mode (-PROBE_INTERVAL, Pmode));
2167 rounded_size_op = force_operand (rounded_size, NULL_RTX);
2168
2169
2170 /* Step 2: compute initial and final value of the loop counter. */
2171
2172 /* SP = SP_0 + PROBE_INTERVAL. */
2173 anti_adjust_stack (GEN_INT (PROBE_INTERVAL + dope));
2174
2175 /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE. */
2176 last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
2177 stack_pointer_rtx,
2178 rounded_size_op), NULL_RTX);
2179
2180
2181 /* Step 3: the loop
2182
2183 while (SP != LAST_ADDR)
2184 {
2185 SP = SP + PROBE_INTERVAL
2186 probe at SP
2187 }
2188
2189 adjusts SP and probes at PROBE_INTERVAL + N * PROBE_INTERVAL for
2190 values of N from 1 until it is equal to ROUNDED_SIZE. */
2191
2192 emit_label (loop_lab);
2193
2194 /* Jump to END_LAB if SP == LAST_ADDR. */
2195 emit_cmp_and_jump_insns (stack_pointer_rtx, last_addr, EQ, NULL_RTX,
2196 Pmode, 1, end_lab);
2197
2198 /* SP = SP + PROBE_INTERVAL and probe at SP. */
2199 anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
2200 emit_stack_probe (stack_pointer_rtx);
2201
2202 emit_jump (loop_lab);
2203
2204 emit_label (end_lab);
2205
2206
2207 /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
2208 assert at compile-time that SIZE is equal to ROUNDED_SIZE. */
2209
2210 /* TEMP = SIZE - ROUNDED_SIZE. */
2211 temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
2212 if (temp != const0_rtx)
2213 {
2214 /* Manual CSE if the difference is not known at compile-time. */
2215 if (GET_CODE (temp) != CONST_INT)
2216 temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
2217 anti_adjust_stack (temp);
2218 emit_stack_probe (stack_pointer_rtx);
2219 }
2220 }
2221
2222 /* Adjust back and account for the additional first interval. */
2223 if (adjust_back)
2224 adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL + dope));
2225 else
2226 adjust_stack (GEN_INT (PROBE_INTERVAL + dope));
2227 }
2228
2229 /* Return an rtx representing the register or memory location
2230 in which a scalar value of data type VALTYPE
2231 was returned by a function call to function FUNC.
2232 FUNC is a FUNCTION_DECL, FNTYPE a FUNCTION_TYPE node if the precise
2233 function is known, otherwise 0.
2234 OUTGOING is 1 if on a machine with register windows this function
2235 should return the register in which the function will put its result
2236 and 0 otherwise. */
2237
2238 rtx
2239 hard_function_value (const_tree valtype, const_tree func, const_tree fntype,
2240 int outgoing ATTRIBUTE_UNUSED)
2241 {
2242 rtx val;
2243
2244 val = targetm.calls.function_value (valtype, func ? func : fntype, outgoing);
2245
2246 if (REG_P (val)
2247 && GET_MODE (val) == BLKmode)
2248 {
2249 unsigned HOST_WIDE_INT bytes = arg_int_size_in_bytes (valtype);
2250 opt_scalar_int_mode tmpmode;
2251
2252 /* int_size_in_bytes can return -1. We don't need a check here
2253 since the value of bytes will then be large enough that no
2254 mode will match anyway. */
2255
2256 FOR_EACH_MODE_IN_CLASS (tmpmode, MODE_INT)
2257 {
2258 /* Have we found a large enough mode? */
2259 if (GET_MODE_SIZE (tmpmode.require ()) >= bytes)
2260 break;
2261 }
2262
2263 PUT_MODE (val, tmpmode.require ());
2264 }
2265 return val;
2266 }
2267
2268 /* Return an rtx representing the register or memory location
2269 in which a scalar value of mode MODE was returned by a library call. */
2270
2271 rtx
2272 hard_libcall_value (machine_mode mode, rtx fun)
2273 {
2274 return targetm.calls.libcall_value (mode, fun);
2275 }
2276
2277 /* Look up the tree code for a given rtx code
2278 to provide the arithmetic operation for real_arithmetic.
2279 The function returns an int because the caller may not know
2280 what `enum tree_code' means. */
2281
2282 int
2283 rtx_to_tree_code (enum rtx_code code)
2284 {
2285 enum tree_code tcode;
2286
2287 switch (code)
2288 {
2289 case PLUS:
2290 tcode = PLUS_EXPR;
2291 break;
2292 case MINUS:
2293 tcode = MINUS_EXPR;
2294 break;
2295 case MULT:
2296 tcode = MULT_EXPR;
2297 break;
2298 case DIV:
2299 tcode = RDIV_EXPR;
2300 break;
2301 case SMIN:
2302 tcode = MIN_EXPR;
2303 break;
2304 case SMAX:
2305 tcode = MAX_EXPR;
2306 break;
2307 default:
2308 tcode = LAST_AND_UNUSED_TREE_CODE;
2309 break;
2310 }
2311 return ((int) tcode);
2312 }
2313
2314 #include "gt-explow.h"