]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/explow.c
trans-array.c (gfc_conv_descriptor_data_get): Rename from gfc_conv_descriptor_data.
[thirdparty/gcc.git] / gcc / explow.c
CommitLineData
18ca7dab 1/* Subroutines for manipulating rtx's in semantically interesting ways.
ef58a523 2 Copyright (C) 1987, 1991, 1994, 1995, 1996, 1997, 1998,
ad616de1 3 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
18ca7dab 4
1322177d 5This file is part of GCC.
18ca7dab 6
1322177d
LB
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 2, or (at your option) any later
10version.
18ca7dab 11
1322177d
LB
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15for more details.
18ca7dab
RK
16
17You should have received a copy of the GNU General Public License
1322177d
LB
18along with GCC; see the file COPYING. If not, write to the Free
19Software Foundation, 59 Temple Place - Suite 330, Boston, MA
2002111-1307, USA. */
18ca7dab
RK
21
22
23#include "config.h"
670ee920 24#include "system.h"
4977bab6
ZW
25#include "coretypes.h"
26#include "tm.h"
01198c2f 27#include "toplev.h"
18ca7dab
RK
28#include "rtl.h"
29#include "tree.h"
6baf1cc8 30#include "tm_p.h"
18ca7dab 31#include "flags.h"
49ad7cfa 32#include "function.h"
18ca7dab 33#include "expr.h"
e78d8e51 34#include "optabs.h"
18ca7dab
RK
35#include "hard-reg-set.h"
36#include "insn-config.h"
1d974ca7 37#include "ggc.h"
18ca7dab 38#include "recog.h"
a77a9a18 39#include "langhooks.h"
18ca7dab 40
502b8322
AJ
41static rtx break_out_memory_refs (rtx);
42static void emit_stack_probe (rtx);
7e4ce834
RH
43
44
45/* Truncate and perhaps sign-extend C as appropriate for MODE. */
46
47HOST_WIDE_INT
502b8322 48trunc_int_for_mode (HOST_WIDE_INT c, enum machine_mode mode)
7e4ce834
RH
49{
50 int width = GET_MODE_BITSIZE (mode);
51
71012d97 52 /* You want to truncate to a _what_? */
5b0264cb 53 gcc_assert (SCALAR_INT_MODE_P (mode));
71012d97 54
1f3f36d1
RH
55 /* Canonicalize BImode to 0 and STORE_FLAG_VALUE. */
56 if (mode == BImode)
57 return c & 1 ? STORE_FLAG_VALUE : 0;
58
5b0d91c3
AO
59 /* Sign-extend for the requested mode. */
60
61 if (width < HOST_BITS_PER_WIDE_INT)
62 {
63 HOST_WIDE_INT sign = 1;
64 sign <<= width - 1;
65 c &= (sign << 1) - 1;
66 c ^= sign;
67 c -= sign;
68 }
7e4ce834
RH
69
70 return c;
71}
72
3e95a7cb 73/* Return an rtx for the sum of X and the integer C. */
18ca7dab
RK
74
75rtx
3e95a7cb 76plus_constant (rtx x, HOST_WIDE_INT c)
18ca7dab 77{
b3694847 78 RTX_CODE code;
17ab7c59 79 rtx y;
b3694847
SS
80 enum machine_mode mode;
81 rtx tem;
18ca7dab
RK
82 int all_constant = 0;
83
84 if (c == 0)
85 return x;
86
87 restart:
88
89 code = GET_CODE (x);
90 mode = GET_MODE (x);
17ab7c59
RK
91 y = x;
92
18ca7dab
RK
93 switch (code)
94 {
95 case CONST_INT:
b1ec3c92 96 return GEN_INT (INTVAL (x) + c);
18ca7dab
RK
97
98 case CONST_DOUBLE:
99 {
f9e158c3 100 unsigned HOST_WIDE_INT l1 = CONST_DOUBLE_LOW (x);
b1ec3c92 101 HOST_WIDE_INT h1 = CONST_DOUBLE_HIGH (x);
f9e158c3 102 unsigned HOST_WIDE_INT l2 = c;
b1ec3c92 103 HOST_WIDE_INT h2 = c < 0 ? ~0 : 0;
f9e158c3
JM
104 unsigned HOST_WIDE_INT lv;
105 HOST_WIDE_INT hv;
18ca7dab
RK
106
107 add_double (l1, h1, l2, h2, &lv, &hv);
108
109 return immed_double_const (lv, hv, VOIDmode);
110 }
111
112 case MEM:
113 /* If this is a reference to the constant pool, try replacing it with
114 a reference to a new constant. If the resulting address isn't
115 valid, don't return it because we have no way to validize it. */
116 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
117 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
118 {
119 tem
120 = force_const_mem (GET_MODE (x),
121 plus_constant (get_pool_constant (XEXP (x, 0)),
122 c));
123 if (memory_address_p (GET_MODE (tem), XEXP (tem, 0)))
124 return tem;
125 }
126 break;
127
128 case CONST:
129 /* If adding to something entirely constant, set a flag
130 so that we can add a CONST around the result. */
131 x = XEXP (x, 0);
132 all_constant = 1;
133 goto restart;
134
135 case SYMBOL_REF:
136 case LABEL_REF:
137 all_constant = 1;
138 break;
139
140 case PLUS:
141 /* The interesting case is adding the integer to a sum.
142 Look for constant term in the sum and combine
143 with C. For an integer constant term, we make a combined
144 integer. For a constant term that is not an explicit integer,
d9b3eb63 145 we cannot really combine, but group them together anyway.
e5671f2b 146
03d937fc
R
147 Restart or use a recursive call in case the remaining operand is
148 something that we handle specially, such as a SYMBOL_REF.
149
150 We may not immediately return from the recursive call here, lest
151 all_constant gets lost. */
e5671f2b
RK
152
153 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
03d937fc
R
154 {
155 c += INTVAL (XEXP (x, 1));
7e4ce834
RH
156
157 if (GET_MODE (x) != VOIDmode)
158 c = trunc_int_for_mode (c, GET_MODE (x));
159
03d937fc
R
160 x = XEXP (x, 0);
161 goto restart;
162 }
b72f00af 163 else if (CONSTANT_P (XEXP (x, 1)))
03d937fc 164 {
b72f00af 165 x = gen_rtx_PLUS (mode, XEXP (x, 0), plus_constant (XEXP (x, 1), c));
03d937fc
R
166 c = 0;
167 }
b72f00af 168 else if (find_constant_term_loc (&y))
03d937fc 169 {
b72f00af
RK
170 /* We need to be careful since X may be shared and we can't
171 modify it in place. */
172 rtx copy = copy_rtx (x);
173 rtx *const_loc = find_constant_term_loc (&copy);
174
175 *const_loc = plus_constant (*const_loc, c);
176 x = copy;
03d937fc
R
177 c = 0;
178 }
38a448ca 179 break;
ed8908e7 180
38a448ca
RH
181 default:
182 break;
18ca7dab
RK
183 }
184
185 if (c != 0)
38a448ca 186 x = gen_rtx_PLUS (mode, x, GEN_INT (c));
18ca7dab
RK
187
188 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
189 return x;
190 else if (all_constant)
38a448ca 191 return gen_rtx_CONST (mode, x);
18ca7dab
RK
192 else
193 return x;
194}
18ca7dab
RK
195\f
196/* If X is a sum, return a new sum like X but lacking any constant terms.
197 Add all the removed constant terms into *CONSTPTR.
198 X itself is not altered. The result != X if and only if
199 it is not isomorphic to X. */
200
201rtx
502b8322 202eliminate_constant_term (rtx x, rtx *constptr)
18ca7dab 203{
b3694847 204 rtx x0, x1;
18ca7dab
RK
205 rtx tem;
206
207 if (GET_CODE (x) != PLUS)
208 return x;
209
210 /* First handle constants appearing at this level explicitly. */
211 if (GET_CODE (XEXP (x, 1)) == CONST_INT
212 && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x), *constptr,
213 XEXP (x, 1)))
214 && GET_CODE (tem) == CONST_INT)
215 {
216 *constptr = tem;
217 return eliminate_constant_term (XEXP (x, 0), constptr);
218 }
219
220 tem = const0_rtx;
221 x0 = eliminate_constant_term (XEXP (x, 0), &tem);
222 x1 = eliminate_constant_term (XEXP (x, 1), &tem);
223 if ((x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
224 && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x),
225 *constptr, tem))
226 && GET_CODE (tem) == CONST_INT)
227 {
228 *constptr = tem;
38a448ca 229 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
18ca7dab
RK
230 }
231
232 return x;
233}
234
18ca7dab
RK
235/* Return an rtx for the size in bytes of the value of EXP. */
236
237rtx
502b8322 238expr_size (tree exp)
18ca7dab 239{
d25cee4d
RH
240 tree size;
241
242 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
243 size = TREE_OPERAND (exp, 1);
244 else
245 size = SUBSTITUTE_PLACEHOLDER_IN_EXPR (lang_hooks.expr_size (exp), exp);
99098c66 246
37a08a29 247 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), 0);
18ca7dab 248}
de8920be
JM
249
250/* Return a wide integer for the size in bytes of the value of EXP, or -1
251 if the size can vary or is larger than an integer. */
252
253HOST_WIDE_INT
502b8322 254int_expr_size (tree exp)
de8920be 255{
d25cee4d
RH
256 tree size;
257
258 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
259 size = TREE_OPERAND (exp, 1);
260 else
261 size = lang_hooks.expr_size (exp);
262
263 if (size == 0 || !host_integerp (size, 0))
de8920be
JM
264 return -1;
265
d25cee4d 266 return tree_low_cst (size, 0);
de8920be 267}
18ca7dab
RK
268\f
269/* Return a copy of X in which all memory references
270 and all constants that involve symbol refs
271 have been replaced with new temporary registers.
272 Also emit code to load the memory locations and constants
273 into those registers.
274
275 If X contains no such constants or memory references,
276 X itself (not a copy) is returned.
277
278 If a constant is found in the address that is not a legitimate constant
279 in an insn, it is left alone in the hope that it might be valid in the
280 address.
281
282 X may contain no arithmetic except addition, subtraction and multiplication.
283 Values returned by expand_expr with 1 for sum_ok fit this constraint. */
284
285static rtx
502b8322 286break_out_memory_refs (rtx x)
18ca7dab 287{
3c0cb5de 288 if (MEM_P (x)
cabeca29 289 || (CONSTANT_P (x) && CONSTANT_ADDRESS_P (x)
18ca7dab 290 && GET_MODE (x) != VOIDmode))
2cca6e3f 291 x = force_reg (GET_MODE (x), x);
18ca7dab
RK
292 else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
293 || GET_CODE (x) == MULT)
294 {
b3694847
SS
295 rtx op0 = break_out_memory_refs (XEXP (x, 0));
296 rtx op1 = break_out_memory_refs (XEXP (x, 1));
2cca6e3f 297
18ca7dab 298 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
38a448ca 299 x = gen_rtx_fmt_ee (GET_CODE (x), Pmode, op0, op1);
18ca7dab 300 }
2cca6e3f 301
18ca7dab
RK
302 return x;
303}
304
ea534b63 305/* Given X, a memory address in ptr_mode, convert it to an address
498b529f
RK
306 in Pmode, or vice versa (TO_MODE says which way). We take advantage of
307 the fact that pointers are not allowed to overflow by commuting arithmetic
308 operations over conversions so that address arithmetic insns can be
309 used. */
ea534b63 310
498b529f 311rtx
5ae6cd0d
MM
312convert_memory_address (enum machine_mode to_mode ATTRIBUTE_UNUSED,
313 rtx x)
ea534b63 314{
5ae6cd0d 315#ifndef POINTERS_EXTEND_UNSIGNED
7c137931 316 gcc_assert (GET_MODE (x) == to_mode || GET_MODE (x) == VOIDmode);
5ae6cd0d
MM
317 return x;
318#else /* defined(POINTERS_EXTEND_UNSIGNED) */
319 enum machine_mode from_mode;
498b529f 320 rtx temp;
aa0f70e6 321 enum rtx_code code;
498b529f 322
5ae6cd0d
MM
323 /* If X already has the right mode, just return it. */
324 if (GET_MODE (x) == to_mode)
325 return x;
326
327 from_mode = to_mode == ptr_mode ? Pmode : ptr_mode;
328
0b04ec8c
RK
329 /* Here we handle some special cases. If none of them apply, fall through
330 to the default case. */
ea534b63
RK
331 switch (GET_CODE (x))
332 {
333 case CONST_INT:
334 case CONST_DOUBLE:
aa0f70e6
SE
335 if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode))
336 code = TRUNCATE;
337 else if (POINTERS_EXTEND_UNSIGNED < 0)
338 break;
339 else if (POINTERS_EXTEND_UNSIGNED > 0)
340 code = ZERO_EXTEND;
341 else
342 code = SIGN_EXTEND;
343 temp = simplify_unary_operation (code, to_mode, x, from_mode);
344 if (temp)
345 return temp;
346 break;
498b529f 347
d1405722 348 case SUBREG:
5da4f548 349 if ((SUBREG_PROMOTED_VAR_P (x) || REG_POINTER (SUBREG_REG (x)))
6dd12198 350 && GET_MODE (SUBREG_REG (x)) == to_mode)
d1405722
RK
351 return SUBREG_REG (x);
352 break;
353
ea534b63 354 case LABEL_REF:
5da4f548
SE
355 temp = gen_rtx_LABEL_REF (to_mode, XEXP (x, 0));
356 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
357 return temp;
6dd12198 358 break;
498b529f 359
ea534b63 360 case SYMBOL_REF:
ce02ba25
EC
361 temp = shallow_copy_rtx (x);
362 PUT_MODE (temp, to_mode);
5da4f548 363 return temp;
6dd12198 364 break;
ea534b63 365
498b529f 366 case CONST:
5da4f548
SE
367 return gen_rtx_CONST (to_mode,
368 convert_memory_address (to_mode, XEXP (x, 0)));
6dd12198 369 break;
ea534b63 370
0b04ec8c
RK
371 case PLUS:
372 case MULT:
aa0f70e6
SE
373 /* For addition we can safely permute the conversion and addition
374 operation if one operand is a constant and converting the constant
375 does not change it. We can always safely permute them if we are
376 making the address narrower. */
377 if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode)
378 || (GET_CODE (x) == PLUS
379 && GET_CODE (XEXP (x, 1)) == CONST_INT
380 && XEXP (x, 1) == convert_memory_address (to_mode, XEXP (x, 1))))
d9b3eb63 381 return gen_rtx_fmt_ee (GET_CODE (x), to_mode,
38a448ca 382 convert_memory_address (to_mode, XEXP (x, 0)),
aa0f70e6 383 XEXP (x, 1));
38a448ca 384 break;
d9b3eb63 385
38a448ca
RH
386 default:
387 break;
ea534b63 388 }
0b04ec8c
RK
389
390 return convert_modes (to_mode, from_mode,
391 x, POINTERS_EXTEND_UNSIGNED);
5ae6cd0d 392#endif /* defined(POINTERS_EXTEND_UNSIGNED) */
ea534b63 393}
18ca7dab
RK
394\f
395/* Return something equivalent to X but valid as a memory address
396 for something of mode MODE. When X is not itself valid, this
397 works by copying X or subexpressions of it into registers. */
398
399rtx
502b8322 400memory_address (enum machine_mode mode, rtx x)
18ca7dab 401{
b3694847 402 rtx oldx = x;
18ca7dab 403
5ae6cd0d 404 x = convert_memory_address (Pmode, x);
ea534b63 405
ba228239 406 /* By passing constant addresses through registers
18ca7dab 407 we get a chance to cse them. */
cabeca29 408 if (! cse_not_expected && CONSTANT_P (x) && CONSTANT_ADDRESS_P (x))
18b9ca6f 409 x = force_reg (Pmode, x);
18ca7dab 410
18ca7dab
RK
411 /* We get better cse by rejecting indirect addressing at this stage.
412 Let the combiner create indirect addresses where appropriate.
413 For now, generate the code so that the subexpressions useful to share
414 are visible. But not if cse won't be done! */
18b9ca6f 415 else
18ca7dab 416 {
f8cfc6aa 417 if (! cse_not_expected && !REG_P (x))
18b9ca6f
RK
418 x = break_out_memory_refs (x);
419
420 /* At this point, any valid address is accepted. */
9b305d55
ZW
421 if (memory_address_p (mode, x))
422 goto win;
18b9ca6f
RK
423
424 /* If it was valid before but breaking out memory refs invalidated it,
425 use it the old way. */
426 if (memory_address_p (mode, oldx))
427 goto win2;
428
429 /* Perform machine-dependent transformations on X
430 in certain cases. This is not necessary since the code
431 below can handle all possible cases, but machine-dependent
432 transformations can make better code. */
433 LEGITIMIZE_ADDRESS (x, oldx, mode, win);
434
435 /* PLUS and MULT can appear in special ways
436 as the result of attempts to make an address usable for indexing.
437 Usually they are dealt with by calling force_operand, below.
438 But a sum containing constant terms is special
439 if removing them makes the sum a valid address:
440 then we generate that address in a register
441 and index off of it. We do this because it often makes
442 shorter code, and because the addresses thus generated
443 in registers often become common subexpressions. */
444 if (GET_CODE (x) == PLUS)
445 {
446 rtx constant_term = const0_rtx;
447 rtx y = eliminate_constant_term (x, &constant_term);
448 if (constant_term == const0_rtx
449 || ! memory_address_p (mode, y))
450 x = force_operand (x, NULL_RTX);
451 else
452 {
38a448ca 453 y = gen_rtx_PLUS (GET_MODE (x), copy_to_reg (y), constant_term);
18b9ca6f
RK
454 if (! memory_address_p (mode, y))
455 x = force_operand (x, NULL_RTX);
456 else
457 x = y;
458 }
459 }
18ca7dab 460
e475ed2a 461 else if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
18b9ca6f 462 x = force_operand (x, NULL_RTX);
18ca7dab 463
18b9ca6f
RK
464 /* If we have a register that's an invalid address,
465 it must be a hard reg of the wrong class. Copy it to a pseudo. */
f8cfc6aa 466 else if (REG_P (x))
18b9ca6f
RK
467 x = copy_to_reg (x);
468
469 /* Last resort: copy the value to a register, since
470 the register is a valid address. */
471 else
472 x = force_reg (Pmode, x);
473
474 goto done;
18ca7dab 475
c02a7fbb
RK
476 win2:
477 x = oldx;
478 win:
f8cfc6aa 479 if (flag_force_addr && ! cse_not_expected && !REG_P (x)
c02a7fbb
RK
480 /* Don't copy an addr via a reg if it is one of our stack slots. */
481 && ! (GET_CODE (x) == PLUS
482 && (XEXP (x, 0) == virtual_stack_vars_rtx
483 || XEXP (x, 0) == virtual_incoming_args_rtx)))
484 {
485 if (general_operand (x, Pmode))
486 x = force_reg (Pmode, x);
487 else
488 x = force_operand (x, NULL_RTX);
489 }
18ca7dab 490 }
18b9ca6f
RK
491
492 done:
493
2cca6e3f
RK
494 /* If we didn't change the address, we are done. Otherwise, mark
495 a reg as a pointer if we have REG or REG + CONST_INT. */
496 if (oldx == x)
497 return x;
f8cfc6aa 498 else if (REG_P (x))
bdb429a5 499 mark_reg_pointer (x, BITS_PER_UNIT);
2cca6e3f 500 else if (GET_CODE (x) == PLUS
f8cfc6aa 501 && REG_P (XEXP (x, 0))
2cca6e3f 502 && GET_CODE (XEXP (x, 1)) == CONST_INT)
bdb429a5 503 mark_reg_pointer (XEXP (x, 0), BITS_PER_UNIT);
2cca6e3f 504
18b9ca6f
RK
505 /* OLDX may have been the address on a temporary. Update the address
506 to indicate that X is now used. */
507 update_temp_slot_address (oldx, x);
508
18ca7dab
RK
509 return x;
510}
511
512/* Like `memory_address' but pretend `flag_force_addr' is 0. */
513
514rtx
502b8322 515memory_address_noforce (enum machine_mode mode, rtx x)
18ca7dab
RK
516{
517 int ambient_force_addr = flag_force_addr;
518 rtx val;
519
520 flag_force_addr = 0;
521 val = memory_address (mode, x);
522 flag_force_addr = ambient_force_addr;
523 return val;
524}
525
526/* Convert a mem ref into one with a valid memory address.
527 Pass through anything else unchanged. */
528
529rtx
502b8322 530validize_mem (rtx ref)
18ca7dab 531{
3c0cb5de 532 if (!MEM_P (ref))
18ca7dab 533 return ref;
792760b9
RK
534 if (! (flag_force_addr && CONSTANT_ADDRESS_P (XEXP (ref, 0)))
535 && memory_address_p (GET_MODE (ref), XEXP (ref, 0)))
18ca7dab 536 return ref;
792760b9 537
18ca7dab 538 /* Don't alter REF itself, since that is probably a stack slot. */
792760b9 539 return replace_equiv_address (ref, XEXP (ref, 0));
18ca7dab
RK
540}
541\f
18ca7dab
RK
542/* Copy the value or contents of X to a new temp reg and return that reg. */
543
544rtx
502b8322 545copy_to_reg (rtx x)
18ca7dab 546{
b3694847 547 rtx temp = gen_reg_rtx (GET_MODE (x));
d9b3eb63 548
18ca7dab 549 /* If not an operand, must be an address with PLUS and MULT so
d9b3eb63 550 do the computation. */
18ca7dab
RK
551 if (! general_operand (x, VOIDmode))
552 x = force_operand (x, temp);
d9b3eb63 553
18ca7dab
RK
554 if (x != temp)
555 emit_move_insn (temp, x);
556
557 return temp;
558}
559
560/* Like copy_to_reg but always give the new register mode Pmode
561 in case X is a constant. */
562
563rtx
502b8322 564copy_addr_to_reg (rtx x)
18ca7dab
RK
565{
566 return copy_to_mode_reg (Pmode, x);
567}
568
569/* Like copy_to_reg but always give the new register mode MODE
570 in case X is a constant. */
571
572rtx
502b8322 573copy_to_mode_reg (enum machine_mode mode, rtx x)
18ca7dab 574{
b3694847 575 rtx temp = gen_reg_rtx (mode);
d9b3eb63 576
18ca7dab 577 /* If not an operand, must be an address with PLUS and MULT so
d9b3eb63 578 do the computation. */
18ca7dab
RK
579 if (! general_operand (x, VOIDmode))
580 x = force_operand (x, temp);
581
5b0264cb 582 gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
18ca7dab
RK
583 if (x != temp)
584 emit_move_insn (temp, x);
585 return temp;
586}
587
588/* Load X into a register if it is not already one.
589 Use mode MODE for the register.
590 X should be valid for mode MODE, but it may be a constant which
591 is valid for all integer modes; that's why caller must specify MODE.
592
593 The caller must not alter the value in the register we return,
594 since we mark it as a "constant" register. */
595
596rtx
502b8322 597force_reg (enum machine_mode mode, rtx x)
18ca7dab 598{
b3694847 599 rtx temp, insn, set;
18ca7dab 600
f8cfc6aa 601 if (REG_P (x))
18ca7dab 602 return x;
d9b3eb63 603
e3c8ea67
RH
604 if (general_operand (x, mode))
605 {
606 temp = gen_reg_rtx (mode);
607 insn = emit_move_insn (temp, x);
608 }
609 else
610 {
611 temp = force_operand (x, NULL_RTX);
f8cfc6aa 612 if (REG_P (temp))
e3c8ea67
RH
613 insn = get_last_insn ();
614 else
615 {
616 rtx temp2 = gen_reg_rtx (mode);
617 insn = emit_move_insn (temp2, temp);
618 temp = temp2;
619 }
620 }
62874575 621
18ca7dab 622 /* Let optimizers know that TEMP's value never changes
62874575
RK
623 and that X can be substituted for it. Don't get confused
624 if INSN set something else (such as a SUBREG of TEMP). */
625 if (CONSTANT_P (x)
626 && (set = single_set (insn)) != 0
fd7acc30
RS
627 && SET_DEST (set) == temp
628 && ! rtx_equal_p (x, SET_SRC (set)))
3d238248 629 set_unique_reg_note (insn, REG_EQUAL, x);
e3c8ea67 630
4a4f95d9
RH
631 /* Let optimizers know that TEMP is a pointer, and if so, the
632 known alignment of that pointer. */
633 {
634 unsigned align = 0;
635 if (GET_CODE (x) == SYMBOL_REF)
636 {
637 align = BITS_PER_UNIT;
638 if (SYMBOL_REF_DECL (x) && DECL_P (SYMBOL_REF_DECL (x)))
639 align = DECL_ALIGN (SYMBOL_REF_DECL (x));
640 }
641 else if (GET_CODE (x) == LABEL_REF)
642 align = BITS_PER_UNIT;
643 else if (GET_CODE (x) == CONST
644 && GET_CODE (XEXP (x, 0)) == PLUS
645 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
646 && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
647 {
648 rtx s = XEXP (XEXP (x, 0), 0);
649 rtx c = XEXP (XEXP (x, 0), 1);
650 unsigned sa, ca;
651
652 sa = BITS_PER_UNIT;
653 if (SYMBOL_REF_DECL (s) && DECL_P (SYMBOL_REF_DECL (s)))
654 sa = DECL_ALIGN (SYMBOL_REF_DECL (s));
655
656 ca = exact_log2 (INTVAL (c) & -INTVAL (c)) * BITS_PER_UNIT;
657
658 align = MIN (sa, ca);
659 }
660
661 if (align)
662 mark_reg_pointer (temp, align);
663 }
664
18ca7dab
RK
665 return temp;
666}
667
668/* If X is a memory ref, copy its contents to a new temp reg and return
669 that reg. Otherwise, return X. */
670
671rtx
502b8322 672force_not_mem (rtx x)
18ca7dab 673{
b3694847 674 rtx temp;
fe3439b0 675
3c0cb5de 676 if (!MEM_P (x) || GET_MODE (x) == BLKmode)
18ca7dab 677 return x;
fe3439b0 678
18ca7dab 679 temp = gen_reg_rtx (GET_MODE (x));
f8ad8d7c
ZD
680
681 if (MEM_POINTER (x))
682 REG_POINTER (temp) = 1;
683
18ca7dab
RK
684 emit_move_insn (temp, x);
685 return temp;
686}
687
688/* Copy X to TARGET (if it's nonzero and a reg)
689 or to a new temp reg and return that reg.
690 MODE is the mode to use for X in case it is a constant. */
691
692rtx
502b8322 693copy_to_suggested_reg (rtx x, rtx target, enum machine_mode mode)
18ca7dab 694{
b3694847 695 rtx temp;
18ca7dab 696
f8cfc6aa 697 if (target && REG_P (target))
18ca7dab
RK
698 temp = target;
699 else
700 temp = gen_reg_rtx (mode);
701
702 emit_move_insn (temp, x);
703 return temp;
704}
705\f
9ff65789
RK
706/* Return the mode to use to store a scalar of TYPE and MODE.
707 PUNSIGNEDP points to the signedness of the type and may be adjusted
708 to show what signedness to use on extension operations.
709
cc2902df 710 FOR_CALL is nonzero if this call is promoting args for a call. */
9ff65789 711
d4453b7a 712#if defined(PROMOTE_MODE) && !defined(PROMOTE_FUNCTION_MODE)
f07e9f0f 713#define PROMOTE_FUNCTION_MODE PROMOTE_MODE
d4453b7a
PB
714#endif
715
9ff65789 716enum machine_mode
502b8322
AJ
717promote_mode (tree type, enum machine_mode mode, int *punsignedp,
718 int for_call ATTRIBUTE_UNUSED)
9ff65789
RK
719{
720 enum tree_code code = TREE_CODE (type);
721 int unsignedp = *punsignedp;
722
d4453b7a 723#ifndef PROMOTE_MODE
9ff65789
RK
724 if (! for_call)
725 return mode;
726#endif
727
728 switch (code)
729 {
d4453b7a 730#ifdef PROMOTE_FUNCTION_MODE
9ff65789
RK
731 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
732 case CHAR_TYPE: case REAL_TYPE: case OFFSET_TYPE:
d4453b7a
PB
733#ifdef PROMOTE_MODE
734 if (for_call)
735 {
736#endif
737 PROMOTE_FUNCTION_MODE (mode, unsignedp, type);
738#ifdef PROMOTE_MODE
739 }
740 else
741 {
742 PROMOTE_MODE (mode, unsignedp, type);
743 }
744#endif
9ff65789
RK
745 break;
746#endif
747
ea534b63 748#ifdef POINTERS_EXTEND_UNSIGNED
56a4c9e2 749 case REFERENCE_TYPE:
9ff65789 750 case POINTER_TYPE:
ea534b63
RK
751 mode = Pmode;
752 unsignedp = POINTERS_EXTEND_UNSIGNED;
9ff65789 753 break;
ea534b63 754#endif
d9b3eb63 755
38a448ca
RH
756 default:
757 break;
9ff65789
RK
758 }
759
760 *punsignedp = unsignedp;
761 return mode;
762}
763\f
18ca7dab
RK
764/* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
765 This pops when ADJUST is positive. ADJUST need not be constant. */
766
767void
502b8322 768adjust_stack (rtx adjust)
18ca7dab
RK
769{
770 rtx temp;
18ca7dab
RK
771
772 if (adjust == const0_rtx)
773 return;
774
1503a7ec
JH
775 /* We expect all variable sized adjustments to be multiple of
776 PREFERRED_STACK_BOUNDARY. */
777 if (GET_CODE (adjust) == CONST_INT)
778 stack_pointer_delta -= INTVAL (adjust);
779
18ca7dab
RK
780 temp = expand_binop (Pmode,
781#ifdef STACK_GROWS_DOWNWARD
782 add_optab,
783#else
784 sub_optab,
785#endif
786 stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
787 OPTAB_LIB_WIDEN);
788
789 if (temp != stack_pointer_rtx)
790 emit_move_insn (stack_pointer_rtx, temp);
791}
792
793/* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
794 This pushes when ADJUST is positive. ADJUST need not be constant. */
795
796void
502b8322 797anti_adjust_stack (rtx adjust)
18ca7dab
RK
798{
799 rtx temp;
18ca7dab
RK
800
801 if (adjust == const0_rtx)
802 return;
803
1503a7ec
JH
804 /* We expect all variable sized adjustments to be multiple of
805 PREFERRED_STACK_BOUNDARY. */
806 if (GET_CODE (adjust) == CONST_INT)
807 stack_pointer_delta += INTVAL (adjust);
808
18ca7dab
RK
809 temp = expand_binop (Pmode,
810#ifdef STACK_GROWS_DOWNWARD
811 sub_optab,
812#else
813 add_optab,
814#endif
815 stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
816 OPTAB_LIB_WIDEN);
817
818 if (temp != stack_pointer_rtx)
819 emit_move_insn (stack_pointer_rtx, temp);
820}
821
822/* Round the size of a block to be pushed up to the boundary required
823 by this machine. SIZE is the desired size, which need not be constant. */
824
4dd9b044 825static rtx
502b8322 826round_push (rtx size)
18ca7dab 827{
c795bca9 828 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
41ee3204 829
18ca7dab
RK
830 if (align == 1)
831 return size;
41ee3204 832
18ca7dab
RK
833 if (GET_CODE (size) == CONST_INT)
834 {
41ee3204
OH
835 HOST_WIDE_INT new = (INTVAL (size) + align - 1) / align * align;
836
18ca7dab 837 if (INTVAL (size) != new)
b1ec3c92 838 size = GEN_INT (new);
18ca7dab
RK
839 }
840 else
841 {
5244db05 842 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
0f41302f
MS
843 but we know it can't. So add ourselves and then do
844 TRUNC_DIV_EXPR. */
5244db05
RK
845 size = expand_binop (Pmode, add_optab, size, GEN_INT (align - 1),
846 NULL_RTX, 1, OPTAB_LIB_WIDEN);
847 size = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, size, GEN_INT (align),
b1ec3c92
CH
848 NULL_RTX, 1);
849 size = expand_mult (Pmode, size, GEN_INT (align), NULL_RTX, 1);
18ca7dab 850 }
41ee3204 851
18ca7dab
RK
852 return size;
853}
854\f
59257ff7
RK
855/* Save the stack pointer for the purpose in SAVE_LEVEL. PSAVE is a pointer
856 to a previously-created save area. If no save area has been allocated,
857 this function will allocate one. If a save area is specified, it
858 must be of the proper mode.
859
860 The insns are emitted after insn AFTER, if nonzero, otherwise the insns
861 are emitted at the current position. */
862
863void
502b8322 864emit_stack_save (enum save_level save_level, rtx *psave, rtx after)
59257ff7
RK
865{
866 rtx sa = *psave;
867 /* The default is that we use a move insn and save in a Pmode object. */
502b8322 868 rtx (*fcn) (rtx, rtx) = gen_move_insn;
a260abc9 869 enum machine_mode mode = STACK_SAVEAREA_MODE (save_level);
59257ff7
RK
870
871 /* See if this machine has anything special to do for this kind of save. */
872 switch (save_level)
873 {
874#ifdef HAVE_save_stack_block
875 case SAVE_BLOCK:
876 if (HAVE_save_stack_block)
a260abc9 877 fcn = gen_save_stack_block;
59257ff7
RK
878 break;
879#endif
880#ifdef HAVE_save_stack_function
881 case SAVE_FUNCTION:
882 if (HAVE_save_stack_function)
a260abc9 883 fcn = gen_save_stack_function;
59257ff7
RK
884 break;
885#endif
886#ifdef HAVE_save_stack_nonlocal
887 case SAVE_NONLOCAL:
888 if (HAVE_save_stack_nonlocal)
a260abc9 889 fcn = gen_save_stack_nonlocal;
59257ff7
RK
890 break;
891#endif
38a448ca
RH
892 default:
893 break;
59257ff7
RK
894 }
895
896 /* If there is no save area and we have to allocate one, do so. Otherwise
897 verify the save area is the proper mode. */
898
899 if (sa == 0)
900 {
901 if (mode != VOIDmode)
902 {
903 if (save_level == SAVE_NONLOCAL)
904 *psave = sa = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
905 else
906 *psave = sa = gen_reg_rtx (mode);
907 }
908 }
59257ff7
RK
909
910 if (after)
700f6f98
RK
911 {
912 rtx seq;
913
914 start_sequence ();
a494ed43 915 do_pending_stack_adjust ();
5460015d
JW
916 /* We must validize inside the sequence, to ensure that any instructions
917 created by the validize call also get moved to the right place. */
918 if (sa != 0)
919 sa = validize_mem (sa);
d072107f 920 emit_insn (fcn (sa, stack_pointer_rtx));
2f937369 921 seq = get_insns ();
700f6f98
RK
922 end_sequence ();
923 emit_insn_after (seq, after);
924 }
59257ff7 925 else
5460015d 926 {
a494ed43 927 do_pending_stack_adjust ();
5460015d
JW
928 if (sa != 0)
929 sa = validize_mem (sa);
930 emit_insn (fcn (sa, stack_pointer_rtx));
931 }
59257ff7
RK
932}
933
934/* Restore the stack pointer for the purpose in SAVE_LEVEL. SA is the save
d9b3eb63 935 area made by emit_stack_save. If it is zero, we have nothing to do.
59257ff7 936
d9b3eb63 937 Put any emitted insns after insn AFTER, if nonzero, otherwise at
59257ff7
RK
938 current position. */
939
940void
502b8322 941emit_stack_restore (enum save_level save_level, rtx sa, rtx after)
59257ff7
RK
942{
943 /* The default is that we use a move insn. */
502b8322 944 rtx (*fcn) (rtx, rtx) = gen_move_insn;
59257ff7
RK
945
946 /* See if this machine has anything special to do for this kind of save. */
947 switch (save_level)
948 {
949#ifdef HAVE_restore_stack_block
950 case SAVE_BLOCK:
951 if (HAVE_restore_stack_block)
952 fcn = gen_restore_stack_block;
953 break;
954#endif
955#ifdef HAVE_restore_stack_function
956 case SAVE_FUNCTION:
957 if (HAVE_restore_stack_function)
958 fcn = gen_restore_stack_function;
959 break;
960#endif
961#ifdef HAVE_restore_stack_nonlocal
59257ff7
RK
962 case SAVE_NONLOCAL:
963 if (HAVE_restore_stack_nonlocal)
964 fcn = gen_restore_stack_nonlocal;
965 break;
966#endif
38a448ca
RH
967 default:
968 break;
59257ff7
RK
969 }
970
d072107f 971 if (sa != 0)
260f91c2
DJ
972 {
973 sa = validize_mem (sa);
974 /* These clobbers prevent the scheduler from moving
975 references to variable arrays below the code
4b7e68e7 976 that deletes (pops) the arrays. */
260f91c2 977 emit_insn (gen_rtx_CLOBBER (VOIDmode,
502b8322 978 gen_rtx_MEM (BLKmode,
260f91c2
DJ
979 gen_rtx_SCRATCH (VOIDmode))));
980 emit_insn (gen_rtx_CLOBBER (VOIDmode,
981 gen_rtx_MEM (BLKmode, stack_pointer_rtx)));
982 }
d072107f 983
a494ed43
EB
984 discard_pending_stack_adjust ();
985
59257ff7 986 if (after)
700f6f98
RK
987 {
988 rtx seq;
989
990 start_sequence ();
d072107f 991 emit_insn (fcn (stack_pointer_rtx, sa));
2f937369 992 seq = get_insns ();
700f6f98
RK
993 end_sequence ();
994 emit_insn_after (seq, after);
995 }
59257ff7 996 else
d072107f 997 emit_insn (fcn (stack_pointer_rtx, sa));
59257ff7 998}
6de9cd9a
DN
999
1000/* Invoke emit_stack_save on the nonlocal_goto_save_area for the current
1001 function. This function should be called whenever we allocate or
1002 deallocate dynamic stack space. */
1003
1004void
1005update_nonlocal_goto_save_area (void)
1006{
1007 tree t_save;
1008 rtx r_save;
1009
1010 /* The nonlocal_goto_save_area object is an array of N pointers. The
1011 first one is used for the frame pointer save; the rest are sized by
1012 STACK_SAVEAREA_MODE. Create a reference to array index 1, the first
1013 of the stack save area slots. */
3244e67d
RS
1014 t_save = build4 (ARRAY_REF, ptr_type_node, cfun->nonlocal_goto_save_area,
1015 integer_one_node, NULL_TREE, NULL_TREE);
6de9cd9a
DN
1016 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
1017
1018 emit_stack_save (SAVE_NONLOCAL, &r_save, NULL_RTX);
1019}
59257ff7 1020\f
18ca7dab
RK
1021/* Return an rtx representing the address of an area of memory dynamically
1022 pushed on the stack. This region of memory is always aligned to
1023 a multiple of BIGGEST_ALIGNMENT.
1024
1025 Any required stack pointer alignment is preserved.
1026
1027 SIZE is an rtx representing the size of the area.
091ad0b9
RK
1028 TARGET is a place in which the address can be placed.
1029
1030 KNOWN_ALIGN is the alignment (in bits) that we know SIZE has. */
18ca7dab
RK
1031
1032rtx
502b8322 1033allocate_dynamic_stack_space (rtx size, rtx target, int known_align)
18ca7dab 1034{
15fc0026 1035 /* If we're asking for zero bytes, it doesn't matter what we point
9faa82d8 1036 to since we can't dereference it. But return a reasonable
15fc0026
RK
1037 address anyway. */
1038 if (size == const0_rtx)
1039 return virtual_stack_dynamic_rtx;
1040
1041 /* Otherwise, show we're calling alloca or equivalent. */
1042 current_function_calls_alloca = 1;
1043
18ca7dab
RK
1044 /* Ensure the size is in the proper mode. */
1045 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1046 size = convert_to_mode (Pmode, size, 1);
1047
c2f8b491
JH
1048 /* We can't attempt to minimize alignment necessary, because we don't
1049 know the final value of preferred_stack_boundary yet while executing
1050 this code. */
c2f8b491 1051 cfun->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
c2f8b491 1052
18ca7dab
RK
1053 /* We will need to ensure that the address we return is aligned to
1054 BIGGEST_ALIGNMENT. If STACK_DYNAMIC_OFFSET is defined, we don't
d9b3eb63 1055 always know its final value at this point in the compilation (it
18ca7dab
RK
1056 might depend on the size of the outgoing parameter lists, for
1057 example), so we must align the value to be returned in that case.
cc2902df 1058 (Note that STACK_DYNAMIC_OFFSET will have a default nonzero value if
18ca7dab
RK
1059 STACK_POINTER_OFFSET or ACCUMULATE_OUTGOING_ARGS are defined).
1060 We must also do an alignment operation on the returned value if
1061 the stack pointer alignment is less strict that BIGGEST_ALIGNMENT.
1062
1063 If we have to align, we must leave space in SIZE for the hole
1064 that might result from the alignment operation. */
1065
31cdd499 1066#if defined (STACK_DYNAMIC_OFFSET) || defined (STACK_POINTER_OFFSET)
515a7242
JW
1067#define MUST_ALIGN 1
1068#else
c795bca9 1069#define MUST_ALIGN (PREFERRED_STACK_BOUNDARY < BIGGEST_ALIGNMENT)
18ca7dab
RK
1070#endif
1071
515a7242 1072 if (MUST_ALIGN)
d5457140 1073 size
d9b3eb63 1074 = force_operand (plus_constant (size,
d5457140
RK
1075 BIGGEST_ALIGNMENT / BITS_PER_UNIT - 1),
1076 NULL_RTX);
1d9d04f8 1077
18ca7dab
RK
1078#ifdef SETJMP_VIA_SAVE_AREA
1079 /* If setjmp restores regs from a save area in the stack frame,
1080 avoid clobbering the reg save area. Note that the offset of
1081 virtual_incoming_args_rtx includes the preallocated stack args space.
1082 It would be no problem to clobber that, but it's on the wrong side
d0828b31
DM
1083 of the old save area.
1084
1085 What used to happen is that, since we did not know for sure
1086 whether setjmp() was invoked until after RTL generation, we
1087 would use reg notes to store the "optimized" size and fix things
1088 up later. These days we know this information before we ever
1089 start building RTL so the reg notes are unnecessary. */
1090 if (!current_function_calls_setjmp)
1091 {
1092 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
c9ec4f99 1093
d0828b31
DM
1094 /* ??? Code below assumes that the save area needs maximal
1095 alignment. This constraint may be too strong. */
1096 gcc_assert (PREFERRED_STACK_BOUNDARY == BIGGEST_ALIGNMENT);
c9ec4f99 1097
d0828b31
DM
1098 if (GET_CODE (size) == CONST_INT)
1099 {
1100 HOST_WIDE_INT new = INTVAL (size) / align * align;
1101
1102 if (INTVAL (size) != new)
1103 size = GEN_INT (new);
1104 }
1105 else
1106 {
1107 /* Since we know overflow is not possible, we avoid using
1108 CEIL_DIV_EXPR and use TRUNC_DIV_EXPR instead. */
1109 size = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, size,
1110 GEN_INT (align), NULL_RTX, 1);
1111 size = expand_mult (Pmode, size,
1112 GEN_INT (align), NULL_RTX, 1);
1113 }
1114 }
1115 else
1116 {
1117 rtx dynamic_offset
1118 = expand_binop (Pmode, sub_optab, virtual_stack_dynamic_rtx,
1119 stack_pointer_rtx, NULL_RTX, 1, OPTAB_LIB_WIDEN);
1120
1121 size = expand_binop (Pmode, add_optab, size, dynamic_offset,
1122 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1123 }
18ca7dab
RK
1124#endif /* SETJMP_VIA_SAVE_AREA */
1125
1126 /* Round the size to a multiple of the required stack alignment.
1127 Since the stack if presumed to be rounded before this allocation,
1128 this will maintain the required alignment.
1129
1130 If the stack grows downward, we could save an insn by subtracting
1131 SIZE from the stack pointer and then aligning the stack pointer.
1132 The problem with this is that the stack pointer may be unaligned
1133 between the execution of the subtraction and alignment insns and
1134 some machines do not allow this. Even on those that do, some
1135 signal handlers malfunction if a signal should occur between those
1136 insns. Since this is an extremely rare event, we have no reliable
1137 way of knowing which systems have this problem. So we avoid even
1138 momentarily mis-aligning the stack. */
1139
86b25e81
RS
1140 /* If we added a variable amount to SIZE,
1141 we can no longer assume it is aligned. */
515a7242 1142#if !defined (SETJMP_VIA_SAVE_AREA)
c795bca9 1143 if (MUST_ALIGN || known_align % PREFERRED_STACK_BOUNDARY != 0)
34c9156a 1144#endif
091ad0b9 1145 size = round_push (size);
18ca7dab
RK
1146
1147 do_pending_stack_adjust ();
1148
1503a7ec 1149 /* We ought to be called always on the toplevel and stack ought to be aligned
a1f300c0 1150 properly. */
5b0264cb
NS
1151 gcc_assert (!(stack_pointer_delta
1152 % (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)));
1503a7ec 1153
edff2491
RK
1154 /* If needed, check that we have the required amount of stack. Take into
1155 account what has already been checked. */
1156 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1157 probe_stack_range (STACK_CHECK_MAX_FRAME_SIZE + STACK_CHECK_PROTECT, size);
1158
d5457140 1159 /* Don't use a TARGET that isn't a pseudo or is the wrong mode. */
f8cfc6aa 1160 if (target == 0 || !REG_P (target)
d5457140
RK
1161 || REGNO (target) < FIRST_PSEUDO_REGISTER
1162 || GET_MODE (target) != Pmode)
18ca7dab
RK
1163 target = gen_reg_rtx (Pmode);
1164
bdb429a5 1165 mark_reg_pointer (target, known_align);
3ad69266 1166
18ca7dab
RK
1167 /* Perform the required allocation from the stack. Some systems do
1168 this differently than simply incrementing/decrementing from the
38a448ca 1169 stack pointer, such as acquiring the space by calling malloc(). */
18ca7dab
RK
1170#ifdef HAVE_allocate_stack
1171 if (HAVE_allocate_stack)
1172 {
39403d82 1173 enum machine_mode mode = STACK_SIZE_MODE;
a995e389 1174 insn_operand_predicate_fn pred;
39403d82 1175
4b6c1672
RK
1176 /* We don't have to check against the predicate for operand 0 since
1177 TARGET is known to be a pseudo of the proper mode, which must
1178 be valid for the operand. For operand 1, convert to the
1179 proper mode and validate. */
c5c76735 1180 if (mode == VOIDmode)
4b6c1672 1181 mode = insn_data[(int) CODE_FOR_allocate_stack].operand[1].mode;
c5c76735 1182
a995e389
RH
1183 pred = insn_data[(int) CODE_FOR_allocate_stack].operand[1].predicate;
1184 if (pred && ! ((*pred) (size, mode)))
05d482b9 1185 size = copy_to_mode_reg (mode, convert_to_mode (mode, size, 1));
18ca7dab 1186
38a448ca 1187 emit_insn (gen_allocate_stack (target, size));
18ca7dab
RK
1188 }
1189 else
1190#endif
ea534b63 1191 {
38a448ca
RH
1192#ifndef STACK_GROWS_DOWNWARD
1193 emit_move_insn (target, virtual_stack_dynamic_rtx);
1194#endif
a157febd
GK
1195
1196 /* Check stack bounds if necessary. */
1197 if (current_function_limit_stack)
1198 {
1199 rtx available;
1200 rtx space_available = gen_label_rtx ();
1201#ifdef STACK_GROWS_DOWNWARD
d9b3eb63 1202 available = expand_binop (Pmode, sub_optab,
a157febd
GK
1203 stack_pointer_rtx, stack_limit_rtx,
1204 NULL_RTX, 1, OPTAB_WIDEN);
1205#else
d9b3eb63 1206 available = expand_binop (Pmode, sub_optab,
a157febd
GK
1207 stack_limit_rtx, stack_pointer_rtx,
1208 NULL_RTX, 1, OPTAB_WIDEN);
1209#endif
1210 emit_cmp_and_jump_insns (available, size, GEU, NULL_RTX, Pmode, 1,
a06ef755 1211 space_available);
a157febd
GK
1212#ifdef HAVE_trap
1213 if (HAVE_trap)
1214 emit_insn (gen_trap ());
1215 else
1216#endif
1217 error ("stack limits not supported on this target");
1218 emit_barrier ();
1219 emit_label (space_available);
1220 }
1221
ea534b63 1222 anti_adjust_stack (size);
d5457140 1223
18ca7dab 1224#ifdef STACK_GROWS_DOWNWARD
ca56cd30 1225 emit_move_insn (target, virtual_stack_dynamic_rtx);
18ca7dab 1226#endif
38a448ca 1227 }
18ca7dab 1228
515a7242 1229 if (MUST_ALIGN)
091ad0b9 1230 {
5244db05 1231 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
0f41302f
MS
1232 but we know it can't. So add ourselves and then do
1233 TRUNC_DIV_EXPR. */
0f56a403 1234 target = expand_binop (Pmode, add_optab, target,
5244db05
RK
1235 GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT - 1),
1236 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1237 target = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, target,
b1ec3c92
CH
1238 GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT),
1239 NULL_RTX, 1);
091ad0b9 1240 target = expand_mult (Pmode, target,
b1ec3c92
CH
1241 GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT),
1242 NULL_RTX, 1);
091ad0b9 1243 }
d9b3eb63 1244
15fc0026 1245 /* Record the new stack level for nonlocal gotos. */
6de9cd9a
DN
1246 if (cfun->nonlocal_goto_save_area != 0)
1247 update_nonlocal_goto_save_area ();
15fc0026 1248
18ca7dab
RK
1249 return target;
1250}
1251\f
d9b3eb63 1252/* A front end may want to override GCC's stack checking by providing a
14a774a9
RK
1253 run-time routine to call to check the stack, so provide a mechanism for
1254 calling that routine. */
1255
e2500fed 1256static GTY(()) rtx stack_check_libfunc;
14a774a9
RK
1257
1258void
502b8322 1259set_stack_check_libfunc (rtx libfunc)
14a774a9
RK
1260{
1261 stack_check_libfunc = libfunc;
1262}
1263\f
edff2491
RK
1264/* Emit one stack probe at ADDRESS, an address within the stack. */
1265
1266static void
502b8322 1267emit_stack_probe (rtx address)
edff2491 1268{
38a448ca 1269 rtx memref = gen_rtx_MEM (word_mode, address);
edff2491
RK
1270
1271 MEM_VOLATILE_P (memref) = 1;
1272
1273 if (STACK_CHECK_PROBE_LOAD)
1274 emit_move_insn (gen_reg_rtx (word_mode), memref);
1275 else
1276 emit_move_insn (memref, const0_rtx);
1277}
1278
d9b3eb63 1279/* Probe a range of stack addresses from FIRST to FIRST+SIZE, inclusive.
edff2491
RK
1280 FIRST is a constant and size is a Pmode RTX. These are offsets from the
1281 current stack pointer. STACK_GROWS_DOWNWARD says whether to add or
1282 subtract from the stack. If SIZE is constant, this is done
1283 with a fixed number of probes. Otherwise, we must make a loop. */
1284
1285#ifdef STACK_GROWS_DOWNWARD
1286#define STACK_GROW_OP MINUS
1287#else
1288#define STACK_GROW_OP PLUS
1289#endif
1290
1291void
502b8322 1292probe_stack_range (HOST_WIDE_INT first, rtx size)
edff2491 1293{
4b6c1672
RK
1294 /* First ensure SIZE is Pmode. */
1295 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1296 size = convert_to_mode (Pmode, size, 1);
1297
1298 /* Next see if the front end has set up a function for us to call to
14a774a9
RK
1299 check the stack. */
1300 if (stack_check_libfunc != 0)
f5f5363f
RK
1301 {
1302 rtx addr = memory_address (QImode,
2b3aadfc
RH
1303 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1304 stack_pointer_rtx,
1305 plus_constant (size, first)));
f5f5363f 1306
5ae6cd0d 1307 addr = convert_memory_address (ptr_mode, addr);
1258ee80 1308 emit_library_call (stack_check_libfunc, LCT_NORMAL, VOIDmode, 1, addr,
f5f5363f
RK
1309 ptr_mode);
1310 }
14a774a9
RK
1311
1312 /* Next see if we have an insn to check the stack. Use it if so. */
edff2491 1313#ifdef HAVE_check_stack
14a774a9 1314 else if (HAVE_check_stack)
edff2491 1315 {
a995e389 1316 insn_operand_predicate_fn pred;
38a448ca 1317 rtx last_addr
2b3aadfc
RH
1318 = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1319 stack_pointer_rtx,
1320 plus_constant (size, first)),
38a448ca 1321 NULL_RTX);
edff2491 1322
a995e389
RH
1323 pred = insn_data[(int) CODE_FOR_check_stack].operand[0].predicate;
1324 if (pred && ! ((*pred) (last_addr, Pmode)))
c5c76735 1325 last_addr = copy_to_mode_reg (Pmode, last_addr);
edff2491 1326
c5c76735 1327 emit_insn (gen_check_stack (last_addr));
edff2491
RK
1328 }
1329#endif
1330
1331 /* If we have to generate explicit probes, see if we have a constant
95a086b1 1332 small number of them to generate. If so, that's the easy case. */
14a774a9
RK
1333 else if (GET_CODE (size) == CONST_INT
1334 && INTVAL (size) < 10 * STACK_CHECK_PROBE_INTERVAL)
edff2491
RK
1335 {
1336 HOST_WIDE_INT offset;
1337
1338 /* Start probing at FIRST + N * STACK_CHECK_PROBE_INTERVAL
1339 for values of N from 1 until it exceeds LAST. If only one
1340 probe is needed, this will not generate any code. Then probe
1341 at LAST. */
1342 for (offset = first + STACK_CHECK_PROBE_INTERVAL;
1343 offset < INTVAL (size);
1344 offset = offset + STACK_CHECK_PROBE_INTERVAL)
38a448ca
RH
1345 emit_stack_probe (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1346 stack_pointer_rtx,
1347 GEN_INT (offset)));
edff2491 1348
38a448ca
RH
1349 emit_stack_probe (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1350 stack_pointer_rtx,
1351 plus_constant (size, first)));
edff2491
RK
1352 }
1353
1354 /* In the variable case, do the same as above, but in a loop. We emit loop
1355 notes so that loop optimization can be done. */
1356 else
1357 {
1358 rtx test_addr
38a448ca
RH
1359 = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1360 stack_pointer_rtx,
1361 GEN_INT (first + STACK_CHECK_PROBE_INTERVAL)),
edff2491
RK
1362 NULL_RTX);
1363 rtx last_addr
38a448ca
RH
1364 = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1365 stack_pointer_rtx,
1366 plus_constant (size, first)),
edff2491
RK
1367 NULL_RTX);
1368 rtx incr = GEN_INT (STACK_CHECK_PROBE_INTERVAL);
1369 rtx loop_lab = gen_label_rtx ();
1370 rtx test_lab = gen_label_rtx ();
1371 rtx end_lab = gen_label_rtx ();
1372 rtx temp;
1373
f8cfc6aa 1374 if (!REG_P (test_addr)
edff2491
RK
1375 || REGNO (test_addr) < FIRST_PSEUDO_REGISTER)
1376 test_addr = force_reg (Pmode, test_addr);
1377
edff2491
RK
1378 emit_jump (test_lab);
1379
1380 emit_label (loop_lab);
1381 emit_stack_probe (test_addr);
1382
edff2491
RK
1383#ifdef STACK_GROWS_DOWNWARD
1384#define CMP_OPCODE GTU
1385 temp = expand_binop (Pmode, sub_optab, test_addr, incr, test_addr,
1386 1, OPTAB_WIDEN);
1387#else
1388#define CMP_OPCODE LTU
1389 temp = expand_binop (Pmode, add_optab, test_addr, incr, test_addr,
1390 1, OPTAB_WIDEN);
1391#endif
1392
5b0264cb 1393 gcc_assert (temp == test_addr);
edff2491
RK
1394
1395 emit_label (test_lab);
c5d5d461 1396 emit_cmp_and_jump_insns (test_addr, last_addr, CMP_OPCODE,
a06ef755 1397 NULL_RTX, Pmode, 1, loop_lab);
edff2491 1398 emit_jump (end_lab);
edff2491
RK
1399 emit_label (end_lab);
1400
1401 emit_stack_probe (last_addr);
1402 }
1403}
1404\f
18ca7dab
RK
1405/* Return an rtx representing the register or memory location
1406 in which a scalar value of data type VALTYPE
1407 was returned by a function call to function FUNC.
1408 FUNC is a FUNCTION_DECL node if the precise function is known,
4dc07bd7
JJ
1409 otherwise 0.
1410 OUTGOING is 1 if on a machine with register windows this function
1411 should return the register in which the function will put its result
30f7a378 1412 and 0 otherwise. */
18ca7dab
RK
1413
1414rtx
502b8322
AJ
1415hard_function_value (tree valtype, tree func ATTRIBUTE_UNUSED,
1416 int outgoing ATTRIBUTE_UNUSED)
18ca7dab 1417{
4dc07bd7 1418 rtx val;
770ae6cc 1419
4dc07bd7
JJ
1420#ifdef FUNCTION_OUTGOING_VALUE
1421 if (outgoing)
1422 val = FUNCTION_OUTGOING_VALUE (valtype, func);
1423 else
1424#endif
1425 val = FUNCTION_VALUE (valtype, func);
770ae6cc 1426
f8cfc6aa 1427 if (REG_P (val)
e1a4071f
JL
1428 && GET_MODE (val) == BLKmode)
1429 {
770ae6cc 1430 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (valtype);
e1a4071f 1431 enum machine_mode tmpmode;
770ae6cc 1432
d9b3eb63 1433 /* int_size_in_bytes can return -1. We don't need a check here
535a42b1
NS
1434 since the value of bytes will then be large enough that no
1435 mode will match anyway. */
d9b3eb63 1436
e1a4071f 1437 for (tmpmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
0fb7aeda
KH
1438 tmpmode != VOIDmode;
1439 tmpmode = GET_MODE_WIDER_MODE (tmpmode))
1440 {
1441 /* Have we found a large enough mode? */
1442 if (GET_MODE_SIZE (tmpmode) >= bytes)
1443 break;
1444 }
e1a4071f
JL
1445
1446 /* No suitable mode found. */
5b0264cb 1447 gcc_assert (tmpmode != VOIDmode);
e1a4071f
JL
1448
1449 PUT_MODE (val, tmpmode);
d9b3eb63 1450 }
e1a4071f 1451 return val;
18ca7dab
RK
1452}
1453
1454/* Return an rtx representing the register or memory location
1455 in which a scalar value of mode MODE was returned by a library call. */
1456
1457rtx
502b8322 1458hard_libcall_value (enum machine_mode mode)
18ca7dab
RK
1459{
1460 return LIBCALL_VALUE (mode);
1461}
0c5e217d
RS
1462
1463/* Look up the tree code for a given rtx code
1464 to provide the arithmetic operation for REAL_ARITHMETIC.
1465 The function returns an int because the caller may not know
1466 what `enum tree_code' means. */
1467
1468int
502b8322 1469rtx_to_tree_code (enum rtx_code code)
0c5e217d
RS
1470{
1471 enum tree_code tcode;
1472
1473 switch (code)
1474 {
1475 case PLUS:
1476 tcode = PLUS_EXPR;
1477 break;
1478 case MINUS:
1479 tcode = MINUS_EXPR;
1480 break;
1481 case MULT:
1482 tcode = MULT_EXPR;
1483 break;
1484 case DIV:
1485 tcode = RDIV_EXPR;
1486 break;
1487 case SMIN:
1488 tcode = MIN_EXPR;
1489 break;
1490 case SMAX:
1491 tcode = MAX_EXPR;
1492 break;
1493 default:
1494 tcode = LAST_AND_UNUSED_TREE_CODE;
1495 break;
1496 }
1497 return ((int) tcode);
1498}
e2500fed
GK
1499
1500#include "gt-explow.h"