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