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