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