Update version
[official-gcc.git] / gcc / explow.c
blob376c62c2189effbaae6c5e94a51e3d9350e60cc3
1 /* Subroutines for manipulating rtx's in semantically interesting ways.
2 Copyright (C) 1987, 1991, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000 Free Software Foundation, Inc.
5 This file is part of GNU CC.
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "toplev.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "tm_p.h"
29 #include "flags.h"
30 #include "function.h"
31 #include "expr.h"
32 #include "hard-reg-set.h"
33 #include "insn-config.h"
34 #include "recog.h"
36 #if !defined PREFERRED_STACK_BOUNDARY && defined STACK_BOUNDARY
37 #define PREFERRED_STACK_BOUNDARY STACK_BOUNDARY
38 #endif
40 static rtx break_out_memory_refs PARAMS ((rtx));
41 static void emit_stack_probe PARAMS ((rtx));
44 /* Truncate and perhaps sign-extend C as appropriate for MODE. */
46 HOST_WIDE_INT
47 trunc_int_for_mode (c, mode)
48 HOST_WIDE_INT c;
49 enum machine_mode mode;
51 int width = GET_MODE_BITSIZE (mode);
53 /* Canonicalize BImode to 0 and STORE_FLAG_VALUE. */
54 if (mode == BImode)
55 return c & 1 ? STORE_FLAG_VALUE : 0;
57 /* We clear out all bits that don't belong in MODE, unless they and our
58 sign bit are all one. So we get either a reasonable negative
59 value or a reasonable unsigned value. */
61 if (width < HOST_BITS_PER_WIDE_INT
62 && ((c & ((HOST_WIDE_INT) (-1) << (width - 1)))
63 != ((HOST_WIDE_INT) (-1) << (width - 1))))
64 c &= ((HOST_WIDE_INT) 1 << width) - 1;
66 /* If this would be an entire word for the target, but is not for
67 the host, then sign-extend on the host so that the number will look
68 the same way on the host that it would on the target.
70 For example, when building a 64 bit alpha hosted 32 bit sparc
71 targeted compiler, then we want the 32 bit unsigned value -1 to be
72 represented as a 64 bit value -1, and not as 0x00000000ffffffff.
73 The later confuses the sparc backend. */
75 if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT
76 && BITS_PER_WORD == width
77 && (c & ((HOST_WIDE_INT) 1 << (width - 1))))
78 c |= ((HOST_WIDE_INT) (-1) << width);
80 return c;
83 /* Return an rtx for the sum of X and the integer C.
85 This function should be used via the `plus_constant' macro. */
87 rtx
88 plus_constant_wide (x, c)
89 register rtx x;
90 register HOST_WIDE_INT c;
92 register RTX_CODE code;
93 register enum machine_mode mode;
94 register rtx tem;
95 int all_constant = 0;
97 if (c == 0)
98 return x;
100 restart:
102 code = GET_CODE (x);
103 mode = GET_MODE (x);
104 switch (code)
106 case CONST_INT:
107 return GEN_INT (INTVAL (x) + c);
109 case CONST_DOUBLE:
111 unsigned HOST_WIDE_INT l1 = CONST_DOUBLE_LOW (x);
112 HOST_WIDE_INT h1 = CONST_DOUBLE_HIGH (x);
113 unsigned HOST_WIDE_INT l2 = c;
114 HOST_WIDE_INT h2 = c < 0 ? ~0 : 0;
115 unsigned HOST_WIDE_INT lv;
116 HOST_WIDE_INT hv;
118 add_double (l1, h1, l2, h2, &lv, &hv);
120 return immed_double_const (lv, hv, VOIDmode);
123 case MEM:
124 /* If this is a reference to the constant pool, try replacing it with
125 a reference to a new constant. If the resulting address isn't
126 valid, don't return it because we have no way to validize it. */
127 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
128 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
131 = force_const_mem (GET_MODE (x),
132 plus_constant (get_pool_constant (XEXP (x, 0)),
133 c));
134 if (memory_address_p (GET_MODE (tem), XEXP (tem, 0)))
135 return tem;
137 break;
139 case CONST:
140 /* If adding to something entirely constant, set a flag
141 so that we can add a CONST around the result. */
142 x = XEXP (x, 0);
143 all_constant = 1;
144 goto restart;
146 case SYMBOL_REF:
147 case LABEL_REF:
148 all_constant = 1;
149 break;
151 case PLUS:
152 /* The interesting case is adding the integer to a sum.
153 Look for constant term in the sum and combine
154 with C. For an integer constant term, we make a combined
155 integer. For a constant term that is not an explicit integer,
156 we cannot really combine, but group them together anyway.
158 Restart or use a recursive call in case the remaining operand is
159 something that we handle specially, such as a SYMBOL_REF.
161 We may not immediately return from the recursive call here, lest
162 all_constant gets lost. */
164 if (GET_CODE (XEXP (x, 1)) == CONST_INT)
166 c += INTVAL (XEXP (x, 1));
168 if (GET_MODE (x) != VOIDmode)
169 c = trunc_int_for_mode (c, GET_MODE (x));
171 x = XEXP (x, 0);
172 goto restart;
174 else if (CONSTANT_P (XEXP (x, 0)))
176 x = gen_rtx_PLUS (mode,
177 plus_constant (XEXP (x, 0), c),
178 XEXP (x, 1));
179 c = 0;
181 else if (CONSTANT_P (XEXP (x, 1)))
183 x = gen_rtx_PLUS (mode,
184 XEXP (x, 0),
185 plus_constant (XEXP (x, 1), c));
186 c = 0;
188 break;
190 default:
191 break;
194 if (c != 0)
195 x = gen_rtx_PLUS (mode, x, GEN_INT (c));
197 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
198 return x;
199 else if (all_constant)
200 return gen_rtx_CONST (mode, x);
201 else
202 return x;
205 /* This is the same as `plus_constant', except that it handles LO_SUM.
207 This function should be used via the `plus_constant_for_output' macro. */
210 plus_constant_for_output_wide (x, c)
211 register rtx x;
212 register HOST_WIDE_INT c;
214 register enum machine_mode mode = GET_MODE (x);
216 if (GET_CODE (x) == LO_SUM)
217 return gen_rtx_LO_SUM (mode, XEXP (x, 0),
218 plus_constant_for_output (XEXP (x, 1), c));
220 else
221 return plus_constant (x, c);
224 /* If X is a sum, return a new sum like X but lacking any constant terms.
225 Add all the removed constant terms into *CONSTPTR.
226 X itself is not altered. The result != X if and only if
227 it is not isomorphic to X. */
230 eliminate_constant_term (x, constptr)
231 rtx x;
232 rtx *constptr;
234 register rtx x0, x1;
235 rtx tem;
237 if (GET_CODE (x) != PLUS)
238 return x;
240 /* First handle constants appearing at this level explicitly. */
241 if (GET_CODE (XEXP (x, 1)) == CONST_INT
242 && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x), *constptr,
243 XEXP (x, 1)))
244 && GET_CODE (tem) == CONST_INT)
246 *constptr = tem;
247 return eliminate_constant_term (XEXP (x, 0), constptr);
250 tem = const0_rtx;
251 x0 = eliminate_constant_term (XEXP (x, 0), &tem);
252 x1 = eliminate_constant_term (XEXP (x, 1), &tem);
253 if ((x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
254 && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x),
255 *constptr, tem))
256 && GET_CODE (tem) == CONST_INT)
258 *constptr = tem;
259 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
262 return x;
265 /* Returns the insn that next references REG after INSN, or 0
266 if REG is clobbered before next referenced or we cannot find
267 an insn that references REG in a straight-line piece of code. */
270 find_next_ref (reg, insn)
271 rtx reg;
272 rtx insn;
274 rtx next;
276 for (insn = NEXT_INSN (insn); insn; insn = next)
278 next = NEXT_INSN (insn);
279 if (GET_CODE (insn) == NOTE)
280 continue;
281 if (GET_CODE (insn) == CODE_LABEL
282 || GET_CODE (insn) == BARRIER)
283 return 0;
284 if (GET_CODE (insn) == INSN
285 || GET_CODE (insn) == JUMP_INSN
286 || GET_CODE (insn) == CALL_INSN)
288 if (reg_set_p (reg, insn))
289 return 0;
290 if (reg_mentioned_p (reg, PATTERN (insn)))
291 return insn;
292 if (GET_CODE (insn) == JUMP_INSN)
294 if (any_uncondjump_p (insn))
295 next = JUMP_LABEL (insn);
296 else
297 return 0;
299 if (GET_CODE (insn) == CALL_INSN
300 && REGNO (reg) < FIRST_PSEUDO_REGISTER
301 && call_used_regs[REGNO (reg)])
302 return 0;
304 else
305 abort ();
307 return 0;
310 /* Return an rtx for the size in bytes of the value of EXP. */
313 expr_size (exp)
314 tree exp;
316 tree size = size_in_bytes (TREE_TYPE (exp));
318 if (TREE_CODE (size) != INTEGER_CST
319 && contains_placeholder_p (size))
320 size = build (WITH_RECORD_EXPR, sizetype, size, exp);
322 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype),
323 EXPAND_MEMORY_USE_BAD);
326 /* Return a copy of X in which all memory references
327 and all constants that involve symbol refs
328 have been replaced with new temporary registers.
329 Also emit code to load the memory locations and constants
330 into those registers.
332 If X contains no such constants or memory references,
333 X itself (not a copy) is returned.
335 If a constant is found in the address that is not a legitimate constant
336 in an insn, it is left alone in the hope that it might be valid in the
337 address.
339 X may contain no arithmetic except addition, subtraction and multiplication.
340 Values returned by expand_expr with 1 for sum_ok fit this constraint. */
342 static rtx
343 break_out_memory_refs (x)
344 register rtx x;
346 if (GET_CODE (x) == MEM
347 || (CONSTANT_P (x) && CONSTANT_ADDRESS_P (x)
348 && GET_MODE (x) != VOIDmode))
349 x = force_reg (GET_MODE (x), x);
350 else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
351 || GET_CODE (x) == MULT)
353 register rtx op0 = break_out_memory_refs (XEXP (x, 0));
354 register rtx op1 = break_out_memory_refs (XEXP (x, 1));
356 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
357 x = gen_rtx_fmt_ee (GET_CODE (x), Pmode, op0, op1);
360 return x;
363 #ifdef POINTERS_EXTEND_UNSIGNED
365 /* Given X, a memory address in ptr_mode, convert it to an address
366 in Pmode, or vice versa (TO_MODE says which way). We take advantage of
367 the fact that pointers are not allowed to overflow by commuting arithmetic
368 operations over conversions so that address arithmetic insns can be
369 used. */
372 convert_memory_address (to_mode, x)
373 enum machine_mode to_mode;
374 rtx x;
376 enum machine_mode from_mode = to_mode == ptr_mode ? Pmode : ptr_mode;
377 rtx temp;
379 /* Here we handle some special cases. If none of them apply, fall through
380 to the default case. */
381 switch (GET_CODE (x))
383 case CONST_INT:
384 case CONST_DOUBLE:
385 return x;
387 case SUBREG:
388 if (GET_MODE (SUBREG_REG (x)) == to_mode)
389 return SUBREG_REG (x);
390 break;
392 case LABEL_REF:
393 temp = gen_rtx_LABEL_REF (to_mode, XEXP (x, 0));
394 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
395 return temp;
397 case SYMBOL_REF:
398 temp = gen_rtx_SYMBOL_REF (to_mode, XSTR (x, 0));
399 SYMBOL_REF_FLAG (temp) = SYMBOL_REF_FLAG (x);
400 CONSTANT_POOL_ADDRESS_P (temp) = CONSTANT_POOL_ADDRESS_P (x);
401 STRING_POOL_ADDRESS_P (temp) = STRING_POOL_ADDRESS_P (x);
402 return temp;
404 case CONST:
405 return gen_rtx_CONST (to_mode,
406 convert_memory_address (to_mode, XEXP (x, 0)));
408 case PLUS:
409 case MULT:
410 /* For addition the second operand is a small constant, we can safely
411 permute the conversion and addition operation. We can always safely
412 permute them if we are making the address narrower. In addition,
413 always permute the operations if this is a constant. */
414 if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode)
415 || (GET_CODE (x) == PLUS && GET_CODE (XEXP (x, 1)) == CONST_INT
416 && (INTVAL (XEXP (x, 1)) + 20000 < 40000
417 || CONSTANT_P (XEXP (x, 0)))))
418 return gen_rtx_fmt_ee (GET_CODE (x), to_mode,
419 convert_memory_address (to_mode, XEXP (x, 0)),
420 convert_memory_address (to_mode, XEXP (x, 1)));
421 break;
423 default:
424 break;
427 return convert_modes (to_mode, from_mode,
428 x, POINTERS_EXTEND_UNSIGNED);
430 #endif
432 /* Given a memory address or facsimile X, construct a new address,
433 currently equivalent, that is stable: future stores won't change it.
435 X must be composed of constants, register and memory references
436 combined with addition, subtraction and multiplication:
437 in other words, just what you can get from expand_expr if sum_ok is 1.
439 Works by making copies of all regs and memory locations used
440 by X and combining them the same way X does.
441 You could also stabilize the reference to this address
442 by copying the address to a register with copy_to_reg;
443 but then you wouldn't get indexed addressing in the reference. */
446 copy_all_regs (x)
447 register rtx x;
449 if (GET_CODE (x) == REG)
451 if (REGNO (x) != FRAME_POINTER_REGNUM
452 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
453 && REGNO (x) != HARD_FRAME_POINTER_REGNUM
454 #endif
456 x = copy_to_reg (x);
458 else if (GET_CODE (x) == MEM)
459 x = copy_to_reg (x);
460 else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
461 || GET_CODE (x) == MULT)
463 register rtx op0 = copy_all_regs (XEXP (x, 0));
464 register rtx op1 = copy_all_regs (XEXP (x, 1));
465 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
466 x = gen_rtx_fmt_ee (GET_CODE (x), Pmode, op0, op1);
468 return x;
471 /* Return something equivalent to X but valid as a memory address
472 for something of mode MODE. When X is not itself valid, this
473 works by copying X or subexpressions of it into registers. */
476 memory_address (mode, x)
477 enum machine_mode mode;
478 register rtx x;
480 register rtx oldx = x;
482 if (GET_CODE (x) == ADDRESSOF)
483 return x;
485 #ifdef POINTERS_EXTEND_UNSIGNED
486 if (GET_MODE (x) == ptr_mode)
487 x = convert_memory_address (Pmode, x);
488 #endif
490 /* By passing constant addresses thru registers
491 we get a chance to cse them. */
492 if (! cse_not_expected && CONSTANT_P (x) && CONSTANT_ADDRESS_P (x))
493 x = force_reg (Pmode, x);
495 /* Accept a QUEUED that refers to a REG
496 even though that isn't a valid address.
497 On attempting to put this in an insn we will call protect_from_queue
498 which will turn it into a REG, which is valid. */
499 else if (GET_CODE (x) == QUEUED
500 && GET_CODE (QUEUED_VAR (x)) == REG)
503 /* We get better cse by rejecting indirect addressing at this stage.
504 Let the combiner create indirect addresses where appropriate.
505 For now, generate the code so that the subexpressions useful to share
506 are visible. But not if cse won't be done! */
507 else
509 if (! cse_not_expected && GET_CODE (x) != REG)
510 x = break_out_memory_refs (x);
512 /* At this point, any valid address is accepted. */
513 GO_IF_LEGITIMATE_ADDRESS (mode, x, win);
515 /* If it was valid before but breaking out memory refs invalidated it,
516 use it the old way. */
517 if (memory_address_p (mode, oldx))
518 goto win2;
520 /* Perform machine-dependent transformations on X
521 in certain cases. This is not necessary since the code
522 below can handle all possible cases, but machine-dependent
523 transformations can make better code. */
524 LEGITIMIZE_ADDRESS (x, oldx, mode, win);
526 /* PLUS and MULT can appear in special ways
527 as the result of attempts to make an address usable for indexing.
528 Usually they are dealt with by calling force_operand, below.
529 But a sum containing constant terms is special
530 if removing them makes the sum a valid address:
531 then we generate that address in a register
532 and index off of it. We do this because it often makes
533 shorter code, and because the addresses thus generated
534 in registers often become common subexpressions. */
535 if (GET_CODE (x) == PLUS)
537 rtx constant_term = const0_rtx;
538 rtx y = eliminate_constant_term (x, &constant_term);
539 if (constant_term == const0_rtx
540 || ! memory_address_p (mode, y))
541 x = force_operand (x, NULL_RTX);
542 else
544 y = gen_rtx_PLUS (GET_MODE (x), copy_to_reg (y), constant_term);
545 if (! memory_address_p (mode, y))
546 x = force_operand (x, NULL_RTX);
547 else
548 x = y;
552 else if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
553 x = force_operand (x, NULL_RTX);
555 /* If we have a register that's an invalid address,
556 it must be a hard reg of the wrong class. Copy it to a pseudo. */
557 else if (GET_CODE (x) == REG)
558 x = copy_to_reg (x);
560 /* Last resort: copy the value to a register, since
561 the register is a valid address. */
562 else
563 x = force_reg (Pmode, x);
565 goto done;
567 win2:
568 x = oldx;
569 win:
570 if (flag_force_addr && ! cse_not_expected && GET_CODE (x) != REG
571 /* Don't copy an addr via a reg if it is one of our stack slots. */
572 && ! (GET_CODE (x) == PLUS
573 && (XEXP (x, 0) == virtual_stack_vars_rtx
574 || XEXP (x, 0) == virtual_incoming_args_rtx)))
576 if (general_operand (x, Pmode))
577 x = force_reg (Pmode, x);
578 else
579 x = force_operand (x, NULL_RTX);
583 done:
585 /* If we didn't change the address, we are done. Otherwise, mark
586 a reg as a pointer if we have REG or REG + CONST_INT. */
587 if (oldx == x)
588 return x;
589 else if (GET_CODE (x) == REG)
590 mark_reg_pointer (x, BITS_PER_UNIT);
591 else if (GET_CODE (x) == PLUS
592 && GET_CODE (XEXP (x, 0)) == REG
593 && GET_CODE (XEXP (x, 1)) == CONST_INT)
594 mark_reg_pointer (XEXP (x, 0), BITS_PER_UNIT);
596 /* OLDX may have been the address on a temporary. Update the address
597 to indicate that X is now used. */
598 update_temp_slot_address (oldx, x);
600 return x;
603 /* Like `memory_address' but pretend `flag_force_addr' is 0. */
606 memory_address_noforce (mode, x)
607 enum machine_mode mode;
608 rtx x;
610 int ambient_force_addr = flag_force_addr;
611 rtx val;
613 flag_force_addr = 0;
614 val = memory_address (mode, x);
615 flag_force_addr = ambient_force_addr;
616 return val;
619 /* Convert a mem ref into one with a valid memory address.
620 Pass through anything else unchanged. */
623 validize_mem (ref)
624 rtx ref;
626 if (GET_CODE (ref) != MEM)
627 return ref;
628 if (memory_address_p (GET_MODE (ref), XEXP (ref, 0)))
629 return ref;
630 /* Don't alter REF itself, since that is probably a stack slot. */
631 return change_address (ref, GET_MODE (ref), XEXP (ref, 0));
634 /* Given REF, either a MEM or a REG, and T, either the type of X or
635 the expression corresponding to REF, set RTX_UNCHANGING_P if
636 appropriate. */
638 void
639 maybe_set_unchanging (ref, t)
640 rtx ref;
641 tree t;
643 /* We can set RTX_UNCHANGING_P from TREE_READONLY for decls whose
644 initialization is only executed once, or whose initializer always
645 has the same value. Currently we simplify this to PARM_DECLs in the
646 first case, and decls with TREE_CONSTANT initializers in the second. */
647 if ((TREE_READONLY (t) && DECL_P (t)
648 && (TREE_CODE (t) == PARM_DECL
649 || DECL_INITIAL (t) == NULL_TREE
650 || TREE_CONSTANT (DECL_INITIAL (t))))
651 || TREE_CODE_CLASS (TREE_CODE (t)) == 'c')
652 RTX_UNCHANGING_P (ref) = 1;
655 /* Given REF, a MEM, and T, either the type of X or the expression
656 corresponding to REF, set the memory attributes. OBJECTP is nonzero
657 if we are making a new object of this type. */
659 void
660 set_mem_attributes (ref, t, objectp)
661 rtx ref;
662 tree t;
663 int objectp;
665 tree type;
667 /* It can happen that type_for_mode was given a mode for which there
668 is no language-level type. In which case it returns NULL, which
669 we can see here. */
670 if (t == NULL_TREE)
671 return;
673 type = TYPE_P (t) ? t : TREE_TYPE (t);
675 /* Get the alias set from the expression or type (perhaps using a
676 front-end routine) and then copy bits from the type. */
678 /* It is incorrect to set RTX_UNCHANGING_P from TREE_READONLY (type)
679 here, because, in C and C++, the fact that a location is accessed
680 through a const expression does not mean that the value there can
681 never change. */
682 MEM_ALIAS_SET (ref) = get_alias_set (t);
683 MEM_VOLATILE_P (ref) = TYPE_VOLATILE (type);
684 MEM_IN_STRUCT_P (ref) = AGGREGATE_TYPE_P (type);
686 /* If we are making an object of this type, we know that it is a scalar if
687 the type is not an aggregate. */
688 if (objectp && ! AGGREGATE_TYPE_P (type))
689 MEM_SCALAR_P (ref) = 1;
691 /* If T is a type, this is all we can do. Otherwise, we may be able
692 to deduce some more information about the expression. */
693 if (TYPE_P (t))
694 return;
696 maybe_set_unchanging (ref, t);
697 if (TREE_THIS_VOLATILE (t))
698 MEM_VOLATILE_P (ref) = 1;
700 /* Now see if we can say more about whether it's an aggregate or
701 scalar. If we already know it's an aggregate, don't bother. */
702 if (MEM_IN_STRUCT_P (ref))
703 return;
705 /* Now remove any NOPs: they don't change what the underlying object is.
706 Likewise for SAVE_EXPR. */
707 while (TREE_CODE (t) == NOP_EXPR || TREE_CODE (t) == CONVERT_EXPR
708 || TREE_CODE (t) == NON_LVALUE_EXPR || TREE_CODE (t) == SAVE_EXPR)
709 t = TREE_OPERAND (t, 0);
711 /* Since we already know the type isn't an aggregate, if this is a decl,
712 it must be a scalar. Or if it is a reference into an aggregate,
713 this is part of an aggregate. Otherwise we don't know. */
714 if (DECL_P (t))
715 MEM_SCALAR_P (ref) = 1;
716 else if (TREE_CODE (t) == COMPONENT_REF || TREE_CODE (t) == ARRAY_REF
717 || TREE_CODE (t) == BIT_FIELD_REF)
718 MEM_IN_STRUCT_P (ref) = 1;
721 /* Return a modified copy of X with its memory address copied
722 into a temporary register to protect it from side effects.
723 If X is not a MEM, it is returned unchanged (and not copied).
724 Perhaps even if it is a MEM, if there is no need to change it. */
727 stabilize (x)
728 rtx x;
730 register rtx addr;
732 if (GET_CODE (x) != MEM)
733 return x;
735 addr = XEXP (x, 0);
736 if (rtx_unstable_p (addr))
738 rtx temp = force_reg (Pmode, copy_all_regs (addr));
739 rtx mem = gen_rtx_MEM (GET_MODE (x), temp);
741 MEM_COPY_ATTRIBUTES (mem, x);
742 return mem;
744 return x;
747 /* Copy the value or contents of X to a new temp reg and return that reg. */
750 copy_to_reg (x)
751 rtx x;
753 register rtx temp = gen_reg_rtx (GET_MODE (x));
755 /* If not an operand, must be an address with PLUS and MULT so
756 do the computation. */
757 if (! general_operand (x, VOIDmode))
758 x = force_operand (x, temp);
760 if (x != temp)
761 emit_move_insn (temp, x);
763 return temp;
766 /* Like copy_to_reg but always give the new register mode Pmode
767 in case X is a constant. */
770 copy_addr_to_reg (x)
771 rtx x;
773 return copy_to_mode_reg (Pmode, x);
776 /* Like copy_to_reg but always give the new register mode MODE
777 in case X is a constant. */
780 copy_to_mode_reg (mode, x)
781 enum machine_mode mode;
782 rtx x;
784 register rtx temp = gen_reg_rtx (mode);
786 /* If not an operand, must be an address with PLUS and MULT so
787 do the computation. */
788 if (! general_operand (x, VOIDmode))
789 x = force_operand (x, temp);
791 if (GET_MODE (x) != mode && GET_MODE (x) != VOIDmode)
792 abort ();
793 if (x != temp)
794 emit_move_insn (temp, x);
795 return temp;
798 /* Load X into a register if it is not already one.
799 Use mode MODE for the register.
800 X should be valid for mode MODE, but it may be a constant which
801 is valid for all integer modes; that's why caller must specify MODE.
803 The caller must not alter the value in the register we return,
804 since we mark it as a "constant" register. */
807 force_reg (mode, x)
808 enum machine_mode mode;
809 rtx x;
811 register rtx temp, insn, set;
813 if (GET_CODE (x) == REG)
814 return x;
816 temp = gen_reg_rtx (mode);
818 if (! general_operand (x, mode))
819 x = force_operand (x, NULL_RTX);
821 insn = emit_move_insn (temp, x);
823 /* Let optimizers know that TEMP's value never changes
824 and that X can be substituted for it. Don't get confused
825 if INSN set something else (such as a SUBREG of TEMP). */
826 if (CONSTANT_P (x)
827 && (set = single_set (insn)) != 0
828 && SET_DEST (set) == temp)
830 rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
832 if (note)
833 XEXP (note, 0) = x;
834 else
835 REG_NOTES (insn) = gen_rtx_EXPR_LIST (REG_EQUAL, x, REG_NOTES (insn));
837 return temp;
840 /* If X is a memory ref, copy its contents to a new temp reg and return
841 that reg. Otherwise, return X. */
844 force_not_mem (x)
845 rtx x;
847 register rtx temp;
849 if (GET_CODE (x) != MEM || GET_MODE (x) == BLKmode)
850 return x;
852 temp = gen_reg_rtx (GET_MODE (x));
853 emit_move_insn (temp, x);
854 return temp;
857 /* Copy X to TARGET (if it's nonzero and a reg)
858 or to a new temp reg and return that reg.
859 MODE is the mode to use for X in case it is a constant. */
862 copy_to_suggested_reg (x, target, mode)
863 rtx x, target;
864 enum machine_mode mode;
866 register rtx temp;
868 if (target && GET_CODE (target) == REG)
869 temp = target;
870 else
871 temp = gen_reg_rtx (mode);
873 emit_move_insn (temp, x);
874 return temp;
877 /* Return the mode to use to store a scalar of TYPE and MODE.
878 PUNSIGNEDP points to the signedness of the type and may be adjusted
879 to show what signedness to use on extension operations.
881 FOR_CALL is non-zero if this call is promoting args for a call. */
883 enum machine_mode
884 promote_mode (type, mode, punsignedp, for_call)
885 tree type;
886 enum machine_mode mode;
887 int *punsignedp;
888 int for_call ATTRIBUTE_UNUSED;
890 enum tree_code code = TREE_CODE (type);
891 int unsignedp = *punsignedp;
893 #ifdef PROMOTE_FOR_CALL_ONLY
894 if (! for_call)
895 return mode;
896 #endif
898 switch (code)
900 #ifdef PROMOTE_MODE
901 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
902 case CHAR_TYPE: case REAL_TYPE: case OFFSET_TYPE:
903 PROMOTE_MODE (mode, unsignedp, type);
904 break;
905 #endif
907 #ifdef POINTERS_EXTEND_UNSIGNED
908 case REFERENCE_TYPE:
909 case POINTER_TYPE:
910 mode = Pmode;
911 unsignedp = POINTERS_EXTEND_UNSIGNED;
912 break;
913 #endif
915 default:
916 break;
919 *punsignedp = unsignedp;
920 return mode;
923 /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
924 This pops when ADJUST is positive. ADJUST need not be constant. */
926 void
927 adjust_stack (adjust)
928 rtx adjust;
930 rtx temp;
931 adjust = protect_from_queue (adjust, 0);
933 if (adjust == const0_rtx)
934 return;
936 /* We expect all variable sized adjustments to be multiple of
937 PREFERRED_STACK_BOUNDARY. */
938 if (GET_CODE (adjust) == CONST_INT)
939 stack_pointer_delta -= INTVAL (adjust);
941 temp = expand_binop (Pmode,
942 #ifdef STACK_GROWS_DOWNWARD
943 add_optab,
944 #else
945 sub_optab,
946 #endif
947 stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
948 OPTAB_LIB_WIDEN);
950 if (temp != stack_pointer_rtx)
951 emit_move_insn (stack_pointer_rtx, temp);
954 /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
955 This pushes when ADJUST is positive. ADJUST need not be constant. */
957 void
958 anti_adjust_stack (adjust)
959 rtx adjust;
961 rtx temp;
962 adjust = protect_from_queue (adjust, 0);
964 if (adjust == const0_rtx)
965 return;
967 /* We expect all variable sized adjustments to be multiple of
968 PREFERRED_STACK_BOUNDARY. */
969 if (GET_CODE (adjust) == CONST_INT)
970 stack_pointer_delta += INTVAL (adjust);
972 temp = expand_binop (Pmode,
973 #ifdef STACK_GROWS_DOWNWARD
974 sub_optab,
975 #else
976 add_optab,
977 #endif
978 stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
979 OPTAB_LIB_WIDEN);
981 if (temp != stack_pointer_rtx)
982 emit_move_insn (stack_pointer_rtx, temp);
985 /* Round the size of a block to be pushed up to the boundary required
986 by this machine. SIZE is the desired size, which need not be constant. */
989 round_push (size)
990 rtx size;
992 #ifdef PREFERRED_STACK_BOUNDARY
993 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
994 if (align == 1)
995 return size;
996 if (GET_CODE (size) == CONST_INT)
998 int new = (INTVAL (size) + align - 1) / align * align;
999 if (INTVAL (size) != new)
1000 size = GEN_INT (new);
1002 else
1004 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1005 but we know it can't. So add ourselves and then do
1006 TRUNC_DIV_EXPR. */
1007 size = expand_binop (Pmode, add_optab, size, GEN_INT (align - 1),
1008 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1009 size = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, size, GEN_INT (align),
1010 NULL_RTX, 1);
1011 size = expand_mult (Pmode, size, GEN_INT (align), NULL_RTX, 1);
1013 #endif /* PREFERRED_STACK_BOUNDARY */
1014 return size;
1017 /* Save the stack pointer for the purpose in SAVE_LEVEL. PSAVE is a pointer
1018 to a previously-created save area. If no save area has been allocated,
1019 this function will allocate one. If a save area is specified, it
1020 must be of the proper mode.
1022 The insns are emitted after insn AFTER, if nonzero, otherwise the insns
1023 are emitted at the current position. */
1025 void
1026 emit_stack_save (save_level, psave, after)
1027 enum save_level save_level;
1028 rtx *psave;
1029 rtx after;
1031 rtx sa = *psave;
1032 /* The default is that we use a move insn and save in a Pmode object. */
1033 rtx (*fcn) PARAMS ((rtx, rtx)) = gen_move_insn;
1034 enum machine_mode mode = STACK_SAVEAREA_MODE (save_level);
1036 /* See if this machine has anything special to do for this kind of save. */
1037 switch (save_level)
1039 #ifdef HAVE_save_stack_block
1040 case SAVE_BLOCK:
1041 if (HAVE_save_stack_block)
1042 fcn = gen_save_stack_block;
1043 break;
1044 #endif
1045 #ifdef HAVE_save_stack_function
1046 case SAVE_FUNCTION:
1047 if (HAVE_save_stack_function)
1048 fcn = gen_save_stack_function;
1049 break;
1050 #endif
1051 #ifdef HAVE_save_stack_nonlocal
1052 case SAVE_NONLOCAL:
1053 if (HAVE_save_stack_nonlocal)
1054 fcn = gen_save_stack_nonlocal;
1055 break;
1056 #endif
1057 default:
1058 break;
1061 /* If there is no save area and we have to allocate one, do so. Otherwise
1062 verify the save area is the proper mode. */
1064 if (sa == 0)
1066 if (mode != VOIDmode)
1068 if (save_level == SAVE_NONLOCAL)
1069 *psave = sa = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
1070 else
1071 *psave = sa = gen_reg_rtx (mode);
1074 else
1076 if (mode == VOIDmode || GET_MODE (sa) != mode)
1077 abort ();
1080 if (after)
1082 rtx seq;
1084 start_sequence ();
1085 /* We must validize inside the sequence, to ensure that any instructions
1086 created by the validize call also get moved to the right place. */
1087 if (sa != 0)
1088 sa = validize_mem (sa);
1089 emit_insn (fcn (sa, stack_pointer_rtx));
1090 seq = gen_sequence ();
1091 end_sequence ();
1092 emit_insn_after (seq, after);
1094 else
1096 if (sa != 0)
1097 sa = validize_mem (sa);
1098 emit_insn (fcn (sa, stack_pointer_rtx));
1102 /* Restore the stack pointer for the purpose in SAVE_LEVEL. SA is the save
1103 area made by emit_stack_save. If it is zero, we have nothing to do.
1105 Put any emitted insns after insn AFTER, if nonzero, otherwise at
1106 current position. */
1108 void
1109 emit_stack_restore (save_level, sa, after)
1110 enum save_level save_level;
1111 rtx after;
1112 rtx sa;
1114 /* The default is that we use a move insn. */
1115 rtx (*fcn) PARAMS ((rtx, rtx)) = gen_move_insn;
1117 /* See if this machine has anything special to do for this kind of save. */
1118 switch (save_level)
1120 #ifdef HAVE_restore_stack_block
1121 case SAVE_BLOCK:
1122 if (HAVE_restore_stack_block)
1123 fcn = gen_restore_stack_block;
1124 break;
1125 #endif
1126 #ifdef HAVE_restore_stack_function
1127 case SAVE_FUNCTION:
1128 if (HAVE_restore_stack_function)
1129 fcn = gen_restore_stack_function;
1130 break;
1131 #endif
1132 #ifdef HAVE_restore_stack_nonlocal
1133 case SAVE_NONLOCAL:
1134 if (HAVE_restore_stack_nonlocal)
1135 fcn = gen_restore_stack_nonlocal;
1136 break;
1137 #endif
1138 default:
1139 break;
1142 if (sa != 0)
1143 sa = validize_mem (sa);
1145 if (after)
1147 rtx seq;
1149 start_sequence ();
1150 emit_insn (fcn (stack_pointer_rtx, sa));
1151 seq = gen_sequence ();
1152 end_sequence ();
1153 emit_insn_after (seq, after);
1155 else
1156 emit_insn (fcn (stack_pointer_rtx, sa));
1159 #ifdef SETJMP_VIA_SAVE_AREA
1160 /* Optimize RTL generated by allocate_dynamic_stack_space for targets
1161 where SETJMP_VIA_SAVE_AREA is true. The problem is that on these
1162 platforms, the dynamic stack space used can corrupt the original
1163 frame, thus causing a crash if a longjmp unwinds to it. */
1165 void
1166 optimize_save_area_alloca (insns)
1167 rtx insns;
1169 rtx insn;
1171 for (insn = insns; insn; insn = NEXT_INSN(insn))
1173 rtx note;
1175 if (GET_CODE (insn) != INSN)
1176 continue;
1178 for (note = REG_NOTES (insn); note; note = XEXP (note, 1))
1180 if (REG_NOTE_KIND (note) != REG_SAVE_AREA)
1181 continue;
1183 if (!current_function_calls_setjmp)
1185 rtx pat = PATTERN (insn);
1187 /* If we do not see the note in a pattern matching
1188 these precise characteristics, we did something
1189 entirely wrong in allocate_dynamic_stack_space.
1191 Note, one way this could happen is if SETJMP_VIA_SAVE_AREA
1192 was defined on a machine where stacks grow towards higher
1193 addresses.
1195 Right now only supported port with stack that grow upward
1196 is the HPPA and it does not define SETJMP_VIA_SAVE_AREA. */
1197 if (GET_CODE (pat) != SET
1198 || SET_DEST (pat) != stack_pointer_rtx
1199 || GET_CODE (SET_SRC (pat)) != MINUS
1200 || XEXP (SET_SRC (pat), 0) != stack_pointer_rtx)
1201 abort ();
1203 /* This will now be transformed into a (set REG REG)
1204 so we can just blow away all the other notes. */
1205 XEXP (SET_SRC (pat), 1) = XEXP (note, 0);
1206 REG_NOTES (insn) = NULL_RTX;
1208 else
1210 /* setjmp was called, we must remove the REG_SAVE_AREA
1211 note so that later passes do not get confused by its
1212 presence. */
1213 if (note == REG_NOTES (insn))
1215 REG_NOTES (insn) = XEXP (note, 1);
1217 else
1219 rtx srch;
1221 for (srch = REG_NOTES (insn); srch; srch = XEXP (srch, 1))
1222 if (XEXP (srch, 1) == note)
1223 break;
1225 if (srch == NULL_RTX)
1226 abort();
1228 XEXP (srch, 1) = XEXP (note, 1);
1231 /* Once we've seen the note of interest, we need not look at
1232 the rest of them. */
1233 break;
1237 #endif /* SETJMP_VIA_SAVE_AREA */
1239 /* Return an rtx representing the address of an area of memory dynamically
1240 pushed on the stack. This region of memory is always aligned to
1241 a multiple of BIGGEST_ALIGNMENT.
1243 Any required stack pointer alignment is preserved.
1245 SIZE is an rtx representing the size of the area.
1246 TARGET is a place in which the address can be placed.
1248 KNOWN_ALIGN is the alignment (in bits) that we know SIZE has. */
1251 allocate_dynamic_stack_space (size, target, known_align)
1252 rtx size;
1253 rtx target;
1254 int known_align;
1256 #ifdef SETJMP_VIA_SAVE_AREA
1257 rtx setjmpless_size = NULL_RTX;
1258 #endif
1260 /* If we're asking for zero bytes, it doesn't matter what we point
1261 to since we can't dereference it. But return a reasonable
1262 address anyway. */
1263 if (size == const0_rtx)
1264 return virtual_stack_dynamic_rtx;
1266 /* Otherwise, show we're calling alloca or equivalent. */
1267 current_function_calls_alloca = 1;
1269 /* Ensure the size is in the proper mode. */
1270 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1271 size = convert_to_mode (Pmode, size, 1);
1273 /* We can't attempt to minimize alignment necessary, because we don't
1274 know the final value of preferred_stack_boundary yet while executing
1275 this code. */
1276 #ifdef PREFERRED_STACK_BOUNDARY
1277 cfun->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
1278 #endif
1280 /* We will need to ensure that the address we return is aligned to
1281 BIGGEST_ALIGNMENT. If STACK_DYNAMIC_OFFSET is defined, we don't
1282 always know its final value at this point in the compilation (it
1283 might depend on the size of the outgoing parameter lists, for
1284 example), so we must align the value to be returned in that case.
1285 (Note that STACK_DYNAMIC_OFFSET will have a default non-zero value if
1286 STACK_POINTER_OFFSET or ACCUMULATE_OUTGOING_ARGS are defined).
1287 We must also do an alignment operation on the returned value if
1288 the stack pointer alignment is less strict that BIGGEST_ALIGNMENT.
1290 If we have to align, we must leave space in SIZE for the hole
1291 that might result from the alignment operation. */
1293 #if defined (STACK_DYNAMIC_OFFSET) || defined (STACK_POINTER_OFFSET) || ! defined (PREFERRED_STACK_BOUNDARY)
1294 #define MUST_ALIGN 1
1295 #else
1296 #define MUST_ALIGN (PREFERRED_STACK_BOUNDARY < BIGGEST_ALIGNMENT)
1297 #endif
1299 if (MUST_ALIGN)
1300 size
1301 = force_operand (plus_constant (size,
1302 BIGGEST_ALIGNMENT / BITS_PER_UNIT - 1),
1303 NULL_RTX);
1305 #ifdef SETJMP_VIA_SAVE_AREA
1306 /* If setjmp restores regs from a save area in the stack frame,
1307 avoid clobbering the reg save area. Note that the offset of
1308 virtual_incoming_args_rtx includes the preallocated stack args space.
1309 It would be no problem to clobber that, but it's on the wrong side
1310 of the old save area. */
1312 rtx dynamic_offset
1313 = expand_binop (Pmode, sub_optab, virtual_stack_dynamic_rtx,
1314 stack_pointer_rtx, NULL_RTX, 1, OPTAB_LIB_WIDEN);
1316 if (!current_function_calls_setjmp)
1318 int align = PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT;
1320 /* See optimize_save_area_alloca to understand what is being
1321 set up here. */
1323 #if !defined(PREFERRED_STACK_BOUNDARY) || !defined(MUST_ALIGN) || (PREFERRED_STACK_BOUNDARY != BIGGEST_ALIGNMENT)
1324 /* If anyone creates a target with these characteristics, let them
1325 know that our optimization cannot work correctly in such a case. */
1326 abort ();
1327 #endif
1329 if (GET_CODE (size) == CONST_INT)
1331 HOST_WIDE_INT new = INTVAL (size) / align * align;
1333 if (INTVAL (size) != new)
1334 setjmpless_size = GEN_INT (new);
1335 else
1336 setjmpless_size = size;
1338 else
1340 /* Since we know overflow is not possible, we avoid using
1341 CEIL_DIV_EXPR and use TRUNC_DIV_EXPR instead. */
1342 setjmpless_size = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, size,
1343 GEN_INT (align), NULL_RTX, 1);
1344 setjmpless_size = expand_mult (Pmode, setjmpless_size,
1345 GEN_INT (align), NULL_RTX, 1);
1347 /* Our optimization works based upon being able to perform a simple
1348 transformation of this RTL into a (set REG REG) so make sure things
1349 did in fact end up in a REG. */
1350 if (!register_operand (setjmpless_size, Pmode))
1351 setjmpless_size = force_reg (Pmode, setjmpless_size);
1354 size = expand_binop (Pmode, add_optab, size, dynamic_offset,
1355 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1357 #endif /* SETJMP_VIA_SAVE_AREA */
1359 /* Round the size to a multiple of the required stack alignment.
1360 Since the stack if presumed to be rounded before this allocation,
1361 this will maintain the required alignment.
1363 If the stack grows downward, we could save an insn by subtracting
1364 SIZE from the stack pointer and then aligning the stack pointer.
1365 The problem with this is that the stack pointer may be unaligned
1366 between the execution of the subtraction and alignment insns and
1367 some machines do not allow this. Even on those that do, some
1368 signal handlers malfunction if a signal should occur between those
1369 insns. Since this is an extremely rare event, we have no reliable
1370 way of knowing which systems have this problem. So we avoid even
1371 momentarily mis-aligning the stack. */
1373 #ifdef PREFERRED_STACK_BOUNDARY
1374 /* If we added a variable amount to SIZE,
1375 we can no longer assume it is aligned. */
1376 #if !defined (SETJMP_VIA_SAVE_AREA)
1377 if (MUST_ALIGN || known_align % PREFERRED_STACK_BOUNDARY != 0)
1378 #endif
1379 size = round_push (size);
1380 #endif
1382 do_pending_stack_adjust ();
1384 /* We ought to be called always on the toplevel and stack ought to be aligned
1385 propertly. */
1386 #ifdef PREFERRED_STACK_BOUNDARY
1387 if (stack_pointer_delta % (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT))
1388 abort ();
1389 #endif
1391 /* If needed, check that we have the required amount of stack. Take into
1392 account what has already been checked. */
1393 if (flag_stack_check && ! STACK_CHECK_BUILTIN)
1394 probe_stack_range (STACK_CHECK_MAX_FRAME_SIZE + STACK_CHECK_PROTECT, size);
1396 /* Don't use a TARGET that isn't a pseudo or is the wrong mode. */
1397 if (target == 0 || GET_CODE (target) != REG
1398 || REGNO (target) < FIRST_PSEUDO_REGISTER
1399 || GET_MODE (target) != Pmode)
1400 target = gen_reg_rtx (Pmode);
1402 mark_reg_pointer (target, known_align);
1404 /* Perform the required allocation from the stack. Some systems do
1405 this differently than simply incrementing/decrementing from the
1406 stack pointer, such as acquiring the space by calling malloc(). */
1407 #ifdef HAVE_allocate_stack
1408 if (HAVE_allocate_stack)
1410 enum machine_mode mode = STACK_SIZE_MODE;
1411 insn_operand_predicate_fn pred;
1413 pred = insn_data[(int) CODE_FOR_allocate_stack].operand[0].predicate;
1414 if (pred && ! ((*pred) (target, Pmode)))
1415 #ifdef POINTERS_EXTEND_UNSIGNED
1416 target = convert_memory_address (Pmode, target);
1417 #else
1418 target = copy_to_mode_reg (Pmode, target);
1419 #endif
1421 if (mode == VOIDmode)
1422 mode = Pmode;
1424 pred = insn_data[(int) CODE_FOR_allocate_stack].operand[1].predicate;
1425 if (pred && ! ((*pred) (size, mode)))
1426 size = copy_to_mode_reg (mode, size);
1428 emit_insn (gen_allocate_stack (target, size));
1430 else
1431 #endif
1433 #ifndef STACK_GROWS_DOWNWARD
1434 emit_move_insn (target, virtual_stack_dynamic_rtx);
1435 #endif
1437 /* Check stack bounds if necessary. */
1438 if (current_function_limit_stack)
1440 rtx available;
1441 rtx space_available = gen_label_rtx ();
1442 #ifdef STACK_GROWS_DOWNWARD
1443 available = expand_binop (Pmode, sub_optab,
1444 stack_pointer_rtx, stack_limit_rtx,
1445 NULL_RTX, 1, OPTAB_WIDEN);
1446 #else
1447 available = expand_binop (Pmode, sub_optab,
1448 stack_limit_rtx, stack_pointer_rtx,
1449 NULL_RTX, 1, OPTAB_WIDEN);
1450 #endif
1451 emit_cmp_and_jump_insns (available, size, GEU, NULL_RTX, Pmode, 1,
1452 0, space_available);
1453 #ifdef HAVE_trap
1454 if (HAVE_trap)
1455 emit_insn (gen_trap ());
1456 else
1457 #endif
1458 error ("stack limits not supported on this target");
1459 emit_barrier ();
1460 emit_label (space_available);
1463 anti_adjust_stack (size);
1464 #ifdef SETJMP_VIA_SAVE_AREA
1465 if (setjmpless_size != NULL_RTX)
1467 rtx note_target = get_last_insn ();
1469 REG_NOTES (note_target)
1470 = gen_rtx_EXPR_LIST (REG_SAVE_AREA, setjmpless_size,
1471 REG_NOTES (note_target));
1473 #endif /* SETJMP_VIA_SAVE_AREA */
1475 #ifdef STACK_GROWS_DOWNWARD
1476 emit_move_insn (target, virtual_stack_dynamic_rtx);
1477 #endif
1480 if (MUST_ALIGN)
1482 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1483 but we know it can't. So add ourselves and then do
1484 TRUNC_DIV_EXPR. */
1485 target = expand_binop (Pmode, add_optab, target,
1486 GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT - 1),
1487 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1488 target = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, target,
1489 GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT),
1490 NULL_RTX, 1);
1491 target = expand_mult (Pmode, target,
1492 GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT),
1493 NULL_RTX, 1);
1496 /* Some systems require a particular insn to refer to the stack
1497 to make the pages exist. */
1498 #ifdef HAVE_probe
1499 if (HAVE_probe)
1500 emit_insn (gen_probe ());
1501 #endif
1503 /* Record the new stack level for nonlocal gotos. */
1504 if (nonlocal_goto_handler_slots != 0)
1505 emit_stack_save (SAVE_NONLOCAL, &nonlocal_goto_stack_level, NULL_RTX);
1507 return target;
1510 /* A front end may want to override GCC's stack checking by providing a
1511 run-time routine to call to check the stack, so provide a mechanism for
1512 calling that routine. */
1514 static rtx stack_check_libfunc;
1516 void
1517 set_stack_check_libfunc (libfunc)
1518 rtx libfunc;
1520 stack_check_libfunc = libfunc;
1523 /* Emit one stack probe at ADDRESS, an address within the stack. */
1525 static void
1526 emit_stack_probe (address)
1527 rtx address;
1529 rtx memref = gen_rtx_MEM (word_mode, address);
1531 MEM_VOLATILE_P (memref) = 1;
1533 if (STACK_CHECK_PROBE_LOAD)
1534 emit_move_insn (gen_reg_rtx (word_mode), memref);
1535 else
1536 emit_move_insn (memref, const0_rtx);
1539 /* Probe a range of stack addresses from FIRST to FIRST+SIZE, inclusive.
1540 FIRST is a constant and size is a Pmode RTX. These are offsets from the
1541 current stack pointer. STACK_GROWS_DOWNWARD says whether to add or
1542 subtract from the stack. If SIZE is constant, this is done
1543 with a fixed number of probes. Otherwise, we must make a loop. */
1545 #ifdef STACK_GROWS_DOWNWARD
1546 #define STACK_GROW_OP MINUS
1547 #else
1548 #define STACK_GROW_OP PLUS
1549 #endif
1551 void
1552 probe_stack_range (first, size)
1553 HOST_WIDE_INT first;
1554 rtx size;
1556 /* First see if the front end has set up a function for us to call to
1557 check the stack. */
1558 if (stack_check_libfunc != 0)
1560 rtx addr = memory_address (QImode,
1561 gen_rtx (STACK_GROW_OP, Pmode,
1562 stack_pointer_rtx,
1563 plus_constant (size, first)));
1565 #ifdef POINTERS_EXTEND_UNSIGNED
1566 if (GET_MODE (addr) != ptr_mode)
1567 addr = convert_memory_address (ptr_mode, addr);
1568 #endif
1570 emit_library_call (stack_check_libfunc, 0, VOIDmode, 1, addr,
1571 ptr_mode);
1574 /* Next see if we have an insn to check the stack. Use it if so. */
1575 #ifdef HAVE_check_stack
1576 else if (HAVE_check_stack)
1578 insn_operand_predicate_fn pred;
1579 rtx last_addr
1580 = force_operand (gen_rtx_STACK_GROW_OP (Pmode,
1581 stack_pointer_rtx,
1582 plus_constant (size, first)),
1583 NULL_RTX);
1585 pred = insn_data[(int) CODE_FOR_check_stack].operand[0].predicate;
1586 if (pred && ! ((*pred) (last_addr, Pmode)))
1587 last_addr = copy_to_mode_reg (Pmode, last_addr);
1589 emit_insn (gen_check_stack (last_addr));
1591 #endif
1593 /* If we have to generate explicit probes, see if we have a constant
1594 small number of them to generate. If so, that's the easy case. */
1595 else if (GET_CODE (size) == CONST_INT
1596 && INTVAL (size) < 10 * STACK_CHECK_PROBE_INTERVAL)
1598 HOST_WIDE_INT offset;
1600 /* Start probing at FIRST + N * STACK_CHECK_PROBE_INTERVAL
1601 for values of N from 1 until it exceeds LAST. If only one
1602 probe is needed, this will not generate any code. Then probe
1603 at LAST. */
1604 for (offset = first + STACK_CHECK_PROBE_INTERVAL;
1605 offset < INTVAL (size);
1606 offset = offset + STACK_CHECK_PROBE_INTERVAL)
1607 emit_stack_probe (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1608 stack_pointer_rtx,
1609 GEN_INT (offset)));
1611 emit_stack_probe (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1612 stack_pointer_rtx,
1613 plus_constant (size, first)));
1616 /* In the variable case, do the same as above, but in a loop. We emit loop
1617 notes so that loop optimization can be done. */
1618 else
1620 rtx test_addr
1621 = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1622 stack_pointer_rtx,
1623 GEN_INT (first + STACK_CHECK_PROBE_INTERVAL)),
1624 NULL_RTX);
1625 rtx last_addr
1626 = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1627 stack_pointer_rtx,
1628 plus_constant (size, first)),
1629 NULL_RTX);
1630 rtx incr = GEN_INT (STACK_CHECK_PROBE_INTERVAL);
1631 rtx loop_lab = gen_label_rtx ();
1632 rtx test_lab = gen_label_rtx ();
1633 rtx end_lab = gen_label_rtx ();
1634 rtx temp;
1636 if (GET_CODE (test_addr) != REG
1637 || REGNO (test_addr) < FIRST_PSEUDO_REGISTER)
1638 test_addr = force_reg (Pmode, test_addr);
1640 emit_note (NULL_PTR, NOTE_INSN_LOOP_BEG);
1641 emit_jump (test_lab);
1643 emit_label (loop_lab);
1644 emit_stack_probe (test_addr);
1646 emit_note (NULL_PTR, NOTE_INSN_LOOP_CONT);
1648 #ifdef STACK_GROWS_DOWNWARD
1649 #define CMP_OPCODE GTU
1650 temp = expand_binop (Pmode, sub_optab, test_addr, incr, test_addr,
1651 1, OPTAB_WIDEN);
1652 #else
1653 #define CMP_OPCODE LTU
1654 temp = expand_binop (Pmode, add_optab, test_addr, incr, test_addr,
1655 1, OPTAB_WIDEN);
1656 #endif
1658 if (temp != test_addr)
1659 abort ();
1661 emit_label (test_lab);
1662 emit_cmp_and_jump_insns (test_addr, last_addr, CMP_OPCODE,
1663 NULL_RTX, Pmode, 1, 0, loop_lab);
1664 emit_jump (end_lab);
1665 emit_note (NULL_PTR, NOTE_INSN_LOOP_END);
1666 emit_label (end_lab);
1668 emit_stack_probe (last_addr);
1672 /* Return an rtx representing the register or memory location
1673 in which a scalar value of data type VALTYPE
1674 was returned by a function call to function FUNC.
1675 FUNC is a FUNCTION_DECL node if the precise function is known,
1676 otherwise 0.
1677 OUTGOING is 1 if on a machine with register windows this function
1678 should return the register in which the function will put its result
1679 and 0 otherwise. */
1682 hard_function_value (valtype, func, outgoing)
1683 tree valtype;
1684 tree func ATTRIBUTE_UNUSED;
1685 int outgoing ATTRIBUTE_UNUSED;
1687 rtx val;
1689 #ifdef FUNCTION_OUTGOING_VALUE
1690 if (outgoing)
1691 val = FUNCTION_OUTGOING_VALUE (valtype, func);
1692 else
1693 #endif
1694 val = FUNCTION_VALUE (valtype, func);
1696 if (GET_CODE (val) == REG
1697 && GET_MODE (val) == BLKmode)
1699 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (valtype);
1700 enum machine_mode tmpmode;
1702 for (tmpmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1703 tmpmode != VOIDmode;
1704 tmpmode = GET_MODE_WIDER_MODE (tmpmode))
1706 /* Have we found a large enough mode? */
1707 if (GET_MODE_SIZE (tmpmode) >= bytes)
1708 break;
1711 /* No suitable mode found. */
1712 if (tmpmode == VOIDmode)
1713 abort ();
1715 PUT_MODE (val, tmpmode);
1717 return val;
1720 /* Return an rtx representing the register or memory location
1721 in which a scalar value of mode MODE was returned by a library call. */
1724 hard_libcall_value (mode)
1725 enum machine_mode mode;
1727 return LIBCALL_VALUE (mode);
1730 /* Look up the tree code for a given rtx code
1731 to provide the arithmetic operation for REAL_ARITHMETIC.
1732 The function returns an int because the caller may not know
1733 what `enum tree_code' means. */
1736 rtx_to_tree_code (code)
1737 enum rtx_code code;
1739 enum tree_code tcode;
1741 switch (code)
1743 case PLUS:
1744 tcode = PLUS_EXPR;
1745 break;
1746 case MINUS:
1747 tcode = MINUS_EXPR;
1748 break;
1749 case MULT:
1750 tcode = MULT_EXPR;
1751 break;
1752 case DIV:
1753 tcode = RDIV_EXPR;
1754 break;
1755 case SMIN:
1756 tcode = MIN_EXPR;
1757 break;
1758 case SMAX:
1759 tcode = MAX_EXPR;
1760 break;
1761 default:
1762 tcode = LAST_AND_UNUSED_TREE_CODE;
1763 break;
1765 return ((int) tcode);