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