1 /* Subroutines for manipulating rtx's in semantically interesting ways.
2 Copyright (C) 1987, 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
25 #include "coretypes.h"
27 #include "diagnostic-core.h"
37 #include "hard-reg-set.h"
38 #include "insn-config.h"
41 #include "langhooks.h"
43 #include "common/common-target.h"
46 static rtx
break_out_memory_refs (rtx
);
49 /* Truncate and perhaps sign-extend C as appropriate for MODE. */
52 trunc_int_for_mode (HOST_WIDE_INT c
, enum machine_mode mode
)
54 int width
= GET_MODE_PRECISION (mode
);
56 /* You want to truncate to a _what_? */
57 gcc_assert (SCALAR_INT_MODE_P (mode
));
59 /* Canonicalize BImode to 0 and STORE_FLAG_VALUE. */
61 return c
& 1 ? STORE_FLAG_VALUE
: 0;
63 /* Sign-extend for the requested mode. */
65 if (width
< HOST_BITS_PER_WIDE_INT
)
67 HOST_WIDE_INT sign
= 1;
77 /* Return an rtx for the sum of X and the integer C, given that X has
81 plus_constant (enum machine_mode mode
, rtx x
, HOST_WIDE_INT c
)
88 gcc_assert (GET_MODE (x
) == VOIDmode
|| GET_MODE (x
) == mode
);
101 if (GET_MODE_BITSIZE (mode
) > HOST_BITS_PER_WIDE_INT
)
103 unsigned HOST_WIDE_INT l1
= INTVAL (x
);
104 HOST_WIDE_INT h1
= (l1
>> (HOST_BITS_PER_WIDE_INT
- 1)) ? -1 : 0;
105 unsigned HOST_WIDE_INT l2
= c
;
106 HOST_WIDE_INT h2
= c
< 0 ? -1 : 0;
107 unsigned HOST_WIDE_INT lv
;
110 if (add_double_with_sign (l1
, h1
, l2
, h2
, &lv
, &hv
, false))
113 return immed_double_const (lv
, hv
, VOIDmode
);
116 return GEN_INT (INTVAL (x
) + c
);
120 unsigned HOST_WIDE_INT l1
= CONST_DOUBLE_LOW (x
);
121 HOST_WIDE_INT h1
= CONST_DOUBLE_HIGH (x
);
122 unsigned HOST_WIDE_INT l2
= c
;
123 HOST_WIDE_INT h2
= c
< 0 ? -1 : 0;
124 unsigned HOST_WIDE_INT lv
;
127 if (add_double_with_sign (l1
, h1
, l2
, h2
, &lv
, &hv
, false))
128 /* Sorry, we have no way to represent overflows this wide.
129 To fix, add constant support wider than CONST_DOUBLE. */
130 gcc_assert (GET_MODE_BITSIZE (mode
) <= HOST_BITS_PER_DOUBLE_INT
);
132 return immed_double_const (lv
, hv
, VOIDmode
);
136 /* If this is a reference to the constant pool, try replacing it with
137 a reference to a new constant. If the resulting address isn't
138 valid, don't return it because we have no way to validize it. */
139 if (GET_CODE (XEXP (x
, 0)) == SYMBOL_REF
140 && CONSTANT_POOL_ADDRESS_P (XEXP (x
, 0)))
142 tem
= plus_constant (mode
, get_pool_constant (XEXP (x
, 0)), c
);
143 tem
= force_const_mem (GET_MODE (x
), tem
);
144 if (memory_address_p (GET_MODE (tem
), XEXP (tem
, 0)))
150 /* If adding to something entirely constant, set a flag
151 so that we can add a CONST around the result. */
162 /* The interesting case is adding the integer to a sum. Look
163 for constant term in the sum and combine with C. For an
164 integer constant term or a constant term that is not an
165 explicit integer, we combine or group them together anyway.
167 We may not immediately return from the recursive call here, lest
168 all_constant gets lost. */
170 if (CONSTANT_P (XEXP (x
, 1)))
172 x
= gen_rtx_PLUS (mode
, XEXP (x
, 0),
173 plus_constant (mode
, XEXP (x
, 1), c
));
176 else if (find_constant_term_loc (&y
))
178 /* We need to be careful since X may be shared and we can't
179 modify it in place. */
180 rtx copy
= copy_rtx (x
);
181 rtx
*const_loc
= find_constant_term_loc (©
);
183 *const_loc
= plus_constant (mode
, *const_loc
, c
);
194 x
= gen_rtx_PLUS (mode
, x
, GEN_INT (c
));
196 if (GET_CODE (x
) == SYMBOL_REF
|| GET_CODE (x
) == LABEL_REF
)
198 else if (all_constant
)
199 return gen_rtx_CONST (mode
, x
);
204 /* If X is a sum, return a new sum like X but lacking any constant terms.
205 Add all the removed constant terms into *CONSTPTR.
206 X itself is not altered. The result != X if and only if
207 it is not isomorphic to X. */
210 eliminate_constant_term (rtx x
, rtx
*constptr
)
215 if (GET_CODE (x
) != PLUS
)
218 /* First handle constants appearing at this level explicitly. */
219 if (CONST_INT_P (XEXP (x
, 1))
220 && 0 != (tem
= simplify_binary_operation (PLUS
, GET_MODE (x
), *constptr
,
222 && CONST_INT_P (tem
))
225 return eliminate_constant_term (XEXP (x
, 0), constptr
);
229 x0
= eliminate_constant_term (XEXP (x
, 0), &tem
);
230 x1
= eliminate_constant_term (XEXP (x
, 1), &tem
);
231 if ((x1
!= XEXP (x
, 1) || x0
!= XEXP (x
, 0))
232 && 0 != (tem
= simplify_binary_operation (PLUS
, GET_MODE (x
),
234 && CONST_INT_P (tem
))
237 return gen_rtx_PLUS (GET_MODE (x
), x0
, x1
);
243 /* Return an rtx for the size in bytes of the value of EXP. */
250 if (TREE_CODE (exp
) == WITH_SIZE_EXPR
)
251 size
= TREE_OPERAND (exp
, 1);
254 size
= tree_expr_size (exp
);
256 gcc_assert (size
== SUBSTITUTE_PLACEHOLDER_IN_EXPR (size
, exp
));
259 return expand_expr (size
, NULL_RTX
, TYPE_MODE (sizetype
), EXPAND_NORMAL
);
262 /* Return a wide integer for the size in bytes of the value of EXP, or -1
263 if the size can vary or is larger than an integer. */
266 int_expr_size (tree exp
)
270 if (TREE_CODE (exp
) == WITH_SIZE_EXPR
)
271 size
= TREE_OPERAND (exp
, 1);
274 size
= tree_expr_size (exp
);
278 if (size
== 0 || !host_integerp (size
, 0))
281 return tree_low_cst (size
, 0);
284 /* Return a copy of X in which all memory references
285 and all constants that involve symbol refs
286 have been replaced with new temporary registers.
287 Also emit code to load the memory locations and constants
288 into those registers.
290 If X contains no such constants or memory references,
291 X itself (not a copy) is returned.
293 If a constant is found in the address that is not a legitimate constant
294 in an insn, it is left alone in the hope that it might be valid in the
297 X may contain no arithmetic except addition, subtraction and multiplication.
298 Values returned by expand_expr with 1 for sum_ok fit this constraint. */
301 break_out_memory_refs (rtx x
)
304 || (CONSTANT_P (x
) && CONSTANT_ADDRESS_P (x
)
305 && GET_MODE (x
) != VOIDmode
))
306 x
= force_reg (GET_MODE (x
), x
);
307 else if (GET_CODE (x
) == PLUS
|| GET_CODE (x
) == MINUS
308 || GET_CODE (x
) == MULT
)
310 rtx op0
= break_out_memory_refs (XEXP (x
, 0));
311 rtx op1
= break_out_memory_refs (XEXP (x
, 1));
313 if (op0
!= XEXP (x
, 0) || op1
!= XEXP (x
, 1))
314 x
= simplify_gen_binary (GET_CODE (x
), GET_MODE (x
), op0
, op1
);
320 /* Given X, a memory address in address space AS' pointer mode, convert it to
321 an address in the address space's address mode, or vice versa (TO_MODE says
322 which way). We take advantage of the fact that pointers are not allowed to
323 overflow by commuting arithmetic operations over conversions so that address
324 arithmetic insns can be used. */
327 convert_memory_address_addr_space (enum machine_mode to_mode ATTRIBUTE_UNUSED
,
328 rtx x
, addr_space_t as ATTRIBUTE_UNUSED
)
330 #ifndef POINTERS_EXTEND_UNSIGNED
331 gcc_assert (GET_MODE (x
) == to_mode
|| GET_MODE (x
) == VOIDmode
);
333 #else /* defined(POINTERS_EXTEND_UNSIGNED) */
334 enum machine_mode pointer_mode
, address_mode
, from_mode
;
338 /* If X already has the right mode, just return it. */
339 if (GET_MODE (x
) == to_mode
)
342 pointer_mode
= targetm
.addr_space
.pointer_mode (as
);
343 address_mode
= targetm
.addr_space
.address_mode (as
);
344 from_mode
= to_mode
== pointer_mode
? address_mode
: pointer_mode
;
346 /* Here we handle some special cases. If none of them apply, fall through
347 to the default case. */
348 switch (GET_CODE (x
))
352 if (GET_MODE_SIZE (to_mode
) < GET_MODE_SIZE (from_mode
))
354 else if (POINTERS_EXTEND_UNSIGNED
< 0)
356 else if (POINTERS_EXTEND_UNSIGNED
> 0)
360 temp
= simplify_unary_operation (code
, to_mode
, x
, from_mode
);
366 if ((SUBREG_PROMOTED_VAR_P (x
) || REG_POINTER (SUBREG_REG (x
)))
367 && GET_MODE (SUBREG_REG (x
)) == to_mode
)
368 return SUBREG_REG (x
);
372 temp
= gen_rtx_LABEL_REF (to_mode
, XEXP (x
, 0));
373 LABEL_REF_NONLOCAL_P (temp
) = LABEL_REF_NONLOCAL_P (x
);
378 temp
= shallow_copy_rtx (x
);
379 PUT_MODE (temp
, to_mode
);
384 return gen_rtx_CONST (to_mode
,
385 convert_memory_address_addr_space
386 (to_mode
, XEXP (x
, 0), as
));
391 /* FIXME: For addition, we used to permute the conversion and
392 addition operation only if one operand is a constant and
393 converting the constant does not change it or if one operand
394 is a constant and we are using a ptr_extend instruction
395 (POINTERS_EXTEND_UNSIGNED < 0) even if the resulting address
396 may overflow/underflow. We relax the condition to include
397 zero-extend (POINTERS_EXTEND_UNSIGNED > 0) since the other
398 parts of the compiler depend on it. See PR 49721.
400 We can always safely permute them if we are making the address
402 if (GET_MODE_SIZE (to_mode
) < GET_MODE_SIZE (from_mode
)
403 || (GET_CODE (x
) == PLUS
404 && CONST_INT_P (XEXP (x
, 1))
405 && (POINTERS_EXTEND_UNSIGNED
!= 0
406 || XEXP (x
, 1) == convert_memory_address_addr_space
407 (to_mode
, XEXP (x
, 1), as
))))
408 return gen_rtx_fmt_ee (GET_CODE (x
), to_mode
,
409 convert_memory_address_addr_space
410 (to_mode
, XEXP (x
, 0), as
),
418 return convert_modes (to_mode
, from_mode
,
419 x
, POINTERS_EXTEND_UNSIGNED
);
420 #endif /* defined(POINTERS_EXTEND_UNSIGNED) */
423 /* Return something equivalent to X but valid as a memory address for something
424 of mode MODE in the named address space AS. When X is not itself valid,
425 this works by copying X or subexpressions of it into registers. */
428 memory_address_addr_space (enum machine_mode mode
, rtx x
, addr_space_t as
)
431 enum machine_mode address_mode
= targetm
.addr_space
.address_mode (as
);
433 x
= convert_memory_address_addr_space (address_mode
, x
, as
);
435 /* By passing constant addresses through registers
436 we get a chance to cse them. */
437 if (! cse_not_expected
&& CONSTANT_P (x
) && CONSTANT_ADDRESS_P (x
))
438 x
= force_reg (address_mode
, x
);
440 /* We get better cse by rejecting indirect addressing at this stage.
441 Let the combiner create indirect addresses where appropriate.
442 For now, generate the code so that the subexpressions useful to share
443 are visible. But not if cse won't be done! */
446 if (! cse_not_expected
&& !REG_P (x
))
447 x
= break_out_memory_refs (x
);
449 /* At this point, any valid address is accepted. */
450 if (memory_address_addr_space_p (mode
, x
, as
))
453 /* If it was valid before but breaking out memory refs invalidated it,
454 use it the old way. */
455 if (memory_address_addr_space_p (mode
, oldx
, as
))
461 /* Perform machine-dependent transformations on X
462 in certain cases. This is not necessary since the code
463 below can handle all possible cases, but machine-dependent
464 transformations can make better code. */
467 x
= targetm
.addr_space
.legitimize_address (x
, oldx
, mode
, as
);
468 if (orig_x
!= x
&& memory_address_addr_space_p (mode
, x
, as
))
472 /* PLUS and MULT can appear in special ways
473 as the result of attempts to make an address usable for indexing.
474 Usually they are dealt with by calling force_operand, below.
475 But a sum containing constant terms is special
476 if removing them makes the sum a valid address:
477 then we generate that address in a register
478 and index off of it. We do this because it often makes
479 shorter code, and because the addresses thus generated
480 in registers often become common subexpressions. */
481 if (GET_CODE (x
) == PLUS
)
483 rtx constant_term
= const0_rtx
;
484 rtx y
= eliminate_constant_term (x
, &constant_term
);
485 if (constant_term
== const0_rtx
486 || ! memory_address_addr_space_p (mode
, y
, as
))
487 x
= force_operand (x
, NULL_RTX
);
490 y
= gen_rtx_PLUS (GET_MODE (x
), copy_to_reg (y
), constant_term
);
491 if (! memory_address_addr_space_p (mode
, y
, as
))
492 x
= force_operand (x
, NULL_RTX
);
498 else if (GET_CODE (x
) == MULT
|| GET_CODE (x
) == MINUS
)
499 x
= force_operand (x
, NULL_RTX
);
501 /* If we have a register that's an invalid address,
502 it must be a hard reg of the wrong class. Copy it to a pseudo. */
506 /* Last resort: copy the value to a register, since
507 the register is a valid address. */
509 x
= force_reg (address_mode
, x
);
514 gcc_assert (memory_address_addr_space_p (mode
, x
, as
));
515 /* If we didn't change the address, we are done. Otherwise, mark
516 a reg as a pointer if we have REG or REG + CONST_INT. */
520 mark_reg_pointer (x
, BITS_PER_UNIT
);
521 else if (GET_CODE (x
) == PLUS
522 && REG_P (XEXP (x
, 0))
523 && CONST_INT_P (XEXP (x
, 1)))
524 mark_reg_pointer (XEXP (x
, 0), BITS_PER_UNIT
);
526 /* OLDX may have been the address on a temporary. Update the address
527 to indicate that X is now used. */
528 update_temp_slot_address (oldx
, x
);
533 /* Convert a mem ref into one with a valid memory address.
534 Pass through anything else unchanged. */
537 validize_mem (rtx ref
)
541 ref
= use_anchored_address (ref
);
542 if (memory_address_addr_space_p (GET_MODE (ref
), XEXP (ref
, 0),
543 MEM_ADDR_SPACE (ref
)))
546 /* Don't alter REF itself, since that is probably a stack slot. */
547 return replace_equiv_address (ref
, XEXP (ref
, 0));
550 /* If X is a memory reference to a member of an object block, try rewriting
551 it to use an anchor instead. Return the new memory reference on success
552 and the old one on failure. */
555 use_anchored_address (rtx x
)
558 HOST_WIDE_INT offset
;
559 enum machine_mode mode
;
561 if (!flag_section_anchors
)
567 /* Split the address into a base and offset. */
570 if (GET_CODE (base
) == CONST
571 && GET_CODE (XEXP (base
, 0)) == PLUS
572 && CONST_INT_P (XEXP (XEXP (base
, 0), 1)))
574 offset
+= INTVAL (XEXP (XEXP (base
, 0), 1));
575 base
= XEXP (XEXP (base
, 0), 0);
578 /* Check whether BASE is suitable for anchors. */
579 if (GET_CODE (base
) != SYMBOL_REF
580 || !SYMBOL_REF_HAS_BLOCK_INFO_P (base
)
581 || SYMBOL_REF_ANCHOR_P (base
)
582 || SYMBOL_REF_BLOCK (base
) == NULL
583 || !targetm
.use_anchors_for_symbol_p (base
))
586 /* Decide where BASE is going to be. */
587 place_block_symbol (base
);
589 /* Get the anchor we need to use. */
590 offset
+= SYMBOL_REF_BLOCK_OFFSET (base
);
591 base
= get_section_anchor (SYMBOL_REF_BLOCK (base
), offset
,
592 SYMBOL_REF_TLS_MODEL (base
));
594 /* Work out the offset from the anchor. */
595 offset
-= SYMBOL_REF_BLOCK_OFFSET (base
);
597 /* If we're going to run a CSE pass, force the anchor into a register.
598 We will then be able to reuse registers for several accesses, if the
599 target costs say that that's worthwhile. */
600 mode
= GET_MODE (base
);
601 if (!cse_not_expected
)
602 base
= force_reg (mode
, base
);
604 return replace_equiv_address (x
, plus_constant (mode
, base
, offset
));
607 /* Copy the value or contents of X to a new temp reg and return that reg. */
612 rtx temp
= gen_reg_rtx (GET_MODE (x
));
614 /* If not an operand, must be an address with PLUS and MULT so
615 do the computation. */
616 if (! general_operand (x
, VOIDmode
))
617 x
= force_operand (x
, temp
);
620 emit_move_insn (temp
, x
);
625 /* Like copy_to_reg but always give the new register mode Pmode
626 in case X is a constant. */
629 copy_addr_to_reg (rtx x
)
631 return copy_to_mode_reg (Pmode
, x
);
634 /* Like copy_to_reg but always give the new register mode MODE
635 in case X is a constant. */
638 copy_to_mode_reg (enum machine_mode mode
, rtx x
)
640 rtx temp
= gen_reg_rtx (mode
);
642 /* If not an operand, must be an address with PLUS and MULT so
643 do the computation. */
644 if (! general_operand (x
, VOIDmode
))
645 x
= force_operand (x
, temp
);
647 gcc_assert (GET_MODE (x
) == mode
|| GET_MODE (x
) == VOIDmode
);
649 emit_move_insn (temp
, x
);
653 /* Load X into a register if it is not already one.
654 Use mode MODE for the register.
655 X should be valid for mode MODE, but it may be a constant which
656 is valid for all integer modes; that's why caller must specify MODE.
658 The caller must not alter the value in the register we return,
659 since we mark it as a "constant" register. */
662 force_reg (enum machine_mode mode
, rtx x
)
669 if (general_operand (x
, mode
))
671 temp
= gen_reg_rtx (mode
);
672 insn
= emit_move_insn (temp
, x
);
676 temp
= force_operand (x
, NULL_RTX
);
678 insn
= get_last_insn ();
681 rtx temp2
= gen_reg_rtx (mode
);
682 insn
= emit_move_insn (temp2
, temp
);
687 /* Let optimizers know that TEMP's value never changes
688 and that X can be substituted for it. Don't get confused
689 if INSN set something else (such as a SUBREG of TEMP). */
691 && (set
= single_set (insn
)) != 0
692 && SET_DEST (set
) == temp
693 && ! rtx_equal_p (x
, SET_SRC (set
)))
694 set_unique_reg_note (insn
, REG_EQUAL
, x
);
696 /* Let optimizers know that TEMP is a pointer, and if so, the
697 known alignment of that pointer. */
700 if (GET_CODE (x
) == SYMBOL_REF
)
702 align
= BITS_PER_UNIT
;
703 if (SYMBOL_REF_DECL (x
) && DECL_P (SYMBOL_REF_DECL (x
)))
704 align
= DECL_ALIGN (SYMBOL_REF_DECL (x
));
706 else if (GET_CODE (x
) == LABEL_REF
)
707 align
= BITS_PER_UNIT
;
708 else if (GET_CODE (x
) == CONST
709 && GET_CODE (XEXP (x
, 0)) == PLUS
710 && GET_CODE (XEXP (XEXP (x
, 0), 0)) == SYMBOL_REF
711 && CONST_INT_P (XEXP (XEXP (x
, 0), 1)))
713 rtx s
= XEXP (XEXP (x
, 0), 0);
714 rtx c
= XEXP (XEXP (x
, 0), 1);
718 if (SYMBOL_REF_DECL (s
) && DECL_P (SYMBOL_REF_DECL (s
)))
719 sa
= DECL_ALIGN (SYMBOL_REF_DECL (s
));
725 ca
= ctz_hwi (INTVAL (c
)) * BITS_PER_UNIT
;
726 align
= MIN (sa
, ca
);
730 if (align
|| (MEM_P (x
) && MEM_POINTER (x
)))
731 mark_reg_pointer (temp
, align
);
737 /* If X is a memory ref, copy its contents to a new temp reg and return
738 that reg. Otherwise, return X. */
741 force_not_mem (rtx x
)
745 if (!MEM_P (x
) || GET_MODE (x
) == BLKmode
)
748 temp
= gen_reg_rtx (GET_MODE (x
));
751 REG_POINTER (temp
) = 1;
753 emit_move_insn (temp
, x
);
757 /* Copy X to TARGET (if it's nonzero and a reg)
758 or to a new temp reg and return that reg.
759 MODE is the mode to use for X in case it is a constant. */
762 copy_to_suggested_reg (rtx x
, rtx target
, enum machine_mode mode
)
766 if (target
&& REG_P (target
))
769 temp
= gen_reg_rtx (mode
);
771 emit_move_insn (temp
, x
);
775 /* Return the mode to use to pass or return a scalar of TYPE and MODE.
776 PUNSIGNEDP points to the signedness of the type and may be adjusted
777 to show what signedness to use on extension operations.
779 FOR_RETURN is nonzero if the caller is promoting the return value
780 of FNDECL, else it is for promoting args. */
783 promote_function_mode (const_tree type
, enum machine_mode mode
, int *punsignedp
,
784 const_tree funtype
, int for_return
)
786 /* Called without a type node for a libcall. */
787 if (type
== NULL_TREE
)
789 if (INTEGRAL_MODE_P (mode
))
790 return targetm
.calls
.promote_function_mode (NULL_TREE
, mode
,
797 switch (TREE_CODE (type
))
799 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
800 case REAL_TYPE
: case OFFSET_TYPE
: case FIXED_POINT_TYPE
:
801 case POINTER_TYPE
: case REFERENCE_TYPE
:
802 return targetm
.calls
.promote_function_mode (type
, mode
, punsignedp
, funtype
,
809 /* Return the mode to use to store a scalar of TYPE and MODE.
810 PUNSIGNEDP points to the signedness of the type and may be adjusted
811 to show what signedness to use on extension operations. */
814 promote_mode (const_tree type ATTRIBUTE_UNUSED
, enum machine_mode mode
,
815 int *punsignedp ATTRIBUTE_UNUSED
)
822 /* For libcalls this is invoked without TYPE from the backends
823 TARGET_PROMOTE_FUNCTION_MODE hooks. Don't do anything in that
825 if (type
== NULL_TREE
)
828 /* FIXME: this is the same logic that was there until GCC 4.4, but we
829 probably want to test POINTERS_EXTEND_UNSIGNED even if PROMOTE_MODE
830 is not defined. The affected targets are M32C, S390, SPARC. */
832 code
= TREE_CODE (type
);
833 unsignedp
= *punsignedp
;
837 case INTEGER_TYPE
: case ENUMERAL_TYPE
: case BOOLEAN_TYPE
:
838 case REAL_TYPE
: case OFFSET_TYPE
: case FIXED_POINT_TYPE
:
839 PROMOTE_MODE (mode
, unsignedp
, type
);
840 *punsignedp
= unsignedp
;
844 #ifdef POINTERS_EXTEND_UNSIGNED
847 *punsignedp
= POINTERS_EXTEND_UNSIGNED
;
848 return targetm
.addr_space
.address_mode
849 (TYPE_ADDR_SPACE (TREE_TYPE (type
)));
862 /* Use one of promote_mode or promote_function_mode to find the promoted
863 mode of DECL. If PUNSIGNEDP is not NULL, store there the unsignedness
864 of DECL after promotion. */
867 promote_decl_mode (const_tree decl
, int *punsignedp
)
869 tree type
= TREE_TYPE (decl
);
870 int unsignedp
= TYPE_UNSIGNED (type
);
871 enum machine_mode mode
= DECL_MODE (decl
);
872 enum machine_mode pmode
;
874 if (TREE_CODE (decl
) == RESULT_DECL
875 || TREE_CODE (decl
) == PARM_DECL
)
876 pmode
= promote_function_mode (type
, mode
, &unsignedp
,
877 TREE_TYPE (current_function_decl
), 2);
879 pmode
= promote_mode (type
, mode
, &unsignedp
);
882 *punsignedp
= unsignedp
;
887 /* Controls the behaviour of {anti_,}adjust_stack. */
888 static bool suppress_reg_args_size
;
890 /* A helper for adjust_stack and anti_adjust_stack. */
893 adjust_stack_1 (rtx adjust
, bool anti_p
)
897 #ifndef STACK_GROWS_DOWNWARD
898 /* Hereafter anti_p means subtract_p. */
902 temp
= expand_binop (Pmode
,
903 anti_p
? sub_optab
: add_optab
,
904 stack_pointer_rtx
, adjust
, stack_pointer_rtx
, 0,
907 if (temp
!= stack_pointer_rtx
)
908 insn
= emit_move_insn (stack_pointer_rtx
, temp
);
911 insn
= get_last_insn ();
912 temp
= single_set (insn
);
913 gcc_assert (temp
!= NULL
&& SET_DEST (temp
) == stack_pointer_rtx
);
916 if (!suppress_reg_args_size
)
917 add_reg_note (insn
, REG_ARGS_SIZE
, GEN_INT (stack_pointer_delta
));
920 /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
921 This pops when ADJUST is positive. ADJUST need not be constant. */
924 adjust_stack (rtx adjust
)
926 if (adjust
== const0_rtx
)
929 /* We expect all variable sized adjustments to be multiple of
930 PREFERRED_STACK_BOUNDARY. */
931 if (CONST_INT_P (adjust
))
932 stack_pointer_delta
-= INTVAL (adjust
);
934 adjust_stack_1 (adjust
, false);
937 /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
938 This pushes when ADJUST is positive. ADJUST need not be constant. */
941 anti_adjust_stack (rtx adjust
)
943 if (adjust
== const0_rtx
)
946 /* We expect all variable sized adjustments to be multiple of
947 PREFERRED_STACK_BOUNDARY. */
948 if (CONST_INT_P (adjust
))
949 stack_pointer_delta
+= INTVAL (adjust
);
951 adjust_stack_1 (adjust
, true);
954 /* Round the size of a block to be pushed up to the boundary required
955 by this machine. SIZE is the desired size, which need not be constant. */
958 round_push (rtx size
)
960 rtx align_rtx
, alignm1_rtx
;
962 if (!SUPPORTS_STACK_ALIGNMENT
963 || crtl
->preferred_stack_boundary
== MAX_SUPPORTED_STACK_ALIGNMENT
)
965 int align
= crtl
->preferred_stack_boundary
/ BITS_PER_UNIT
;
970 if (CONST_INT_P (size
))
972 HOST_WIDE_INT new_size
= (INTVAL (size
) + align
- 1) / align
* align
;
974 if (INTVAL (size
) != new_size
)
975 size
= GEN_INT (new_size
);
979 align_rtx
= GEN_INT (align
);
980 alignm1_rtx
= GEN_INT (align
- 1);
984 /* If crtl->preferred_stack_boundary might still grow, use
985 virtual_preferred_stack_boundary_rtx instead. This will be
986 substituted by the right value in vregs pass and optimized
988 align_rtx
= virtual_preferred_stack_boundary_rtx
;
989 alignm1_rtx
= force_operand (plus_constant (Pmode
, align_rtx
, -1),
993 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
994 but we know it can't. So add ourselves and then do
996 size
= expand_binop (Pmode
, add_optab
, size
, alignm1_rtx
,
997 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
998 size
= expand_divmod (0, TRUNC_DIV_EXPR
, Pmode
, size
, align_rtx
,
1000 size
= expand_mult (Pmode
, size
, align_rtx
, NULL_RTX
, 1);
1005 /* Save the stack pointer for the purpose in SAVE_LEVEL. PSAVE is a pointer
1006 to a previously-created save area. If no save area has been allocated,
1007 this function will allocate one. If a save area is specified, it
1008 must be of the proper mode. */
1011 emit_stack_save (enum save_level save_level
, rtx
*psave
)
1014 /* The default is that we use a move insn and save in a Pmode object. */
1015 rtx (*fcn
) (rtx
, rtx
) = gen_move_insn
;
1016 enum machine_mode mode
= STACK_SAVEAREA_MODE (save_level
);
1018 /* See if this machine has anything special to do for this kind of save. */
1021 #ifdef HAVE_save_stack_block
1023 if (HAVE_save_stack_block
)
1024 fcn
= gen_save_stack_block
;
1027 #ifdef HAVE_save_stack_function
1029 if (HAVE_save_stack_function
)
1030 fcn
= gen_save_stack_function
;
1033 #ifdef HAVE_save_stack_nonlocal
1035 if (HAVE_save_stack_nonlocal
)
1036 fcn
= gen_save_stack_nonlocal
;
1043 /* If there is no save area and we have to allocate one, do so. Otherwise
1044 verify the save area is the proper mode. */
1048 if (mode
!= VOIDmode
)
1050 if (save_level
== SAVE_NONLOCAL
)
1051 *psave
= sa
= assign_stack_local (mode
, GET_MODE_SIZE (mode
), 0);
1053 *psave
= sa
= gen_reg_rtx (mode
);
1057 do_pending_stack_adjust ();
1059 sa
= validize_mem (sa
);
1060 emit_insn (fcn (sa
, stack_pointer_rtx
));
1063 /* Restore the stack pointer for the purpose in SAVE_LEVEL. SA is the save
1064 area made by emit_stack_save. If it is zero, we have nothing to do. */
1067 emit_stack_restore (enum save_level save_level
, rtx sa
)
1069 /* The default is that we use a move insn. */
1070 rtx (*fcn
) (rtx
, rtx
) = gen_move_insn
;
1072 /* If stack_realign_drap, the x86 backend emits a prologue that aligns both
1073 STACK_POINTER and HARD_FRAME_POINTER.
1074 If stack_realign_fp, the x86 backend emits a prologue that aligns only
1075 STACK_POINTER. This renders the HARD_FRAME_POINTER unusable for accessing
1076 aligned variables, which is reflected in ix86_can_eliminate.
1077 We normally still have the realigned STACK_POINTER that we can use.
1078 But if there is a stack restore still present at reload, it can trigger
1079 mark_not_eliminable for the STACK_POINTER, leaving no way to eliminate
1080 FRAME_POINTER into a hard reg.
1081 To prevent this situation, we force need_drap if we emit a stack
1083 if (SUPPORTS_STACK_ALIGNMENT
)
1084 crtl
->need_drap
= true;
1086 /* See if this machine has anything special to do for this kind of save. */
1089 #ifdef HAVE_restore_stack_block
1091 if (HAVE_restore_stack_block
)
1092 fcn
= gen_restore_stack_block
;
1095 #ifdef HAVE_restore_stack_function
1097 if (HAVE_restore_stack_function
)
1098 fcn
= gen_restore_stack_function
;
1101 #ifdef HAVE_restore_stack_nonlocal
1103 if (HAVE_restore_stack_nonlocal
)
1104 fcn
= gen_restore_stack_nonlocal
;
1113 sa
= validize_mem (sa
);
1114 /* These clobbers prevent the scheduler from moving
1115 references to variable arrays below the code
1116 that deletes (pops) the arrays. */
1117 emit_clobber (gen_rtx_MEM (BLKmode
, gen_rtx_SCRATCH (VOIDmode
)));
1118 emit_clobber (gen_rtx_MEM (BLKmode
, stack_pointer_rtx
));
1121 discard_pending_stack_adjust ();
1123 emit_insn (fcn (stack_pointer_rtx
, sa
));
1126 /* Invoke emit_stack_save on the nonlocal_goto_save_area for the current
1127 function. This function should be called whenever we allocate or
1128 deallocate dynamic stack space. */
1131 update_nonlocal_goto_save_area (void)
1136 /* The nonlocal_goto_save_area object is an array of N pointers. The
1137 first one is used for the frame pointer save; the rest are sized by
1138 STACK_SAVEAREA_MODE. Create a reference to array index 1, the first
1139 of the stack save area slots. */
1140 t_save
= build4 (ARRAY_REF
,
1141 TREE_TYPE (TREE_TYPE (cfun
->nonlocal_goto_save_area
)),
1142 cfun
->nonlocal_goto_save_area
,
1143 integer_one_node
, NULL_TREE
, NULL_TREE
);
1144 r_save
= expand_expr (t_save
, NULL_RTX
, VOIDmode
, EXPAND_WRITE
);
1146 emit_stack_save (SAVE_NONLOCAL
, &r_save
);
1149 /* Return an rtx representing the address of an area of memory dynamically
1150 pushed on the stack.
1152 Any required stack pointer alignment is preserved.
1154 SIZE is an rtx representing the size of the area.
1156 SIZE_ALIGN is the alignment (in bits) that we know SIZE has. This
1157 parameter may be zero. If so, a proper value will be extracted
1158 from SIZE if it is constant, otherwise BITS_PER_UNIT will be assumed.
1160 REQUIRED_ALIGN is the alignment (in bits) required for the region
1163 If CANNOT_ACCUMULATE is set to TRUE, the caller guarantees that the
1164 stack space allocated by the generated code cannot be added with itself
1165 in the course of the execution of the function. It is always safe to
1166 pass FALSE here and the following criterion is sufficient in order to
1167 pass TRUE: every path in the CFG that starts at the allocation point and
1168 loops to it executes the associated deallocation code. */
1171 allocate_dynamic_stack_space (rtx size
, unsigned size_align
,
1172 unsigned required_align
, bool cannot_accumulate
)
1174 HOST_WIDE_INT stack_usage_size
= -1;
1175 rtx final_label
, final_target
, target
;
1178 /* If we're asking for zero bytes, it doesn't matter what we point
1179 to since we can't dereference it. But return a reasonable
1181 if (size
== const0_rtx
)
1182 return virtual_stack_dynamic_rtx
;
1184 /* Otherwise, show we're calling alloca or equivalent. */
1185 cfun
->calls_alloca
= 1;
1187 /* If stack usage info is requested, look into the size we are passed.
1188 We need to do so this early to avoid the obfuscation that may be
1189 introduced later by the various alignment operations. */
1190 if (flag_stack_usage_info
)
1192 if (CONST_INT_P (size
))
1193 stack_usage_size
= INTVAL (size
);
1194 else if (REG_P (size
))
1196 /* Look into the last emitted insn and see if we can deduce
1197 something for the register. */
1198 rtx insn
, set
, note
;
1199 insn
= get_last_insn ();
1200 if ((set
= single_set (insn
)) && rtx_equal_p (SET_DEST (set
), size
))
1202 if (CONST_INT_P (SET_SRC (set
)))
1203 stack_usage_size
= INTVAL (SET_SRC (set
));
1204 else if ((note
= find_reg_equal_equiv_note (insn
))
1205 && CONST_INT_P (XEXP (note
, 0)))
1206 stack_usage_size
= INTVAL (XEXP (note
, 0));
1210 /* If the size is not constant, we can't say anything. */
1211 if (stack_usage_size
== -1)
1213 current_function_has_unbounded_dynamic_stack_size
= 1;
1214 stack_usage_size
= 0;
1218 /* Ensure the size is in the proper mode. */
1219 if (GET_MODE (size
) != VOIDmode
&& GET_MODE (size
) != Pmode
)
1220 size
= convert_to_mode (Pmode
, size
, 1);
1222 /* Adjust SIZE_ALIGN, if needed. */
1223 if (CONST_INT_P (size
))
1225 unsigned HOST_WIDE_INT lsb
;
1227 lsb
= INTVAL (size
);
1230 /* Watch out for overflow truncating to "unsigned". */
1231 if (lsb
> UINT_MAX
/ BITS_PER_UNIT
)
1232 size_align
= 1u << (HOST_BITS_PER_INT
- 1);
1234 size_align
= (unsigned)lsb
* BITS_PER_UNIT
;
1236 else if (size_align
< BITS_PER_UNIT
)
1237 size_align
= BITS_PER_UNIT
;
1239 /* We will need to ensure that the address we return is aligned to
1240 REQUIRED_ALIGN. If that alignment is no larger than
1241 PREFERRED_STACK_BOUNDARY, we can handle everything without an
1242 explicit alignment. */
1243 if (required_align
<= PREFERRED_STACK_BOUNDARY
)
1245 if (crtl
->preferred_stack_boundary
< required_align
)
1246 crtl
->preferred_stack_boundary
= required_align
;
1247 if (crtl
->max_dynamic_stack_alignment
< required_align
)
1248 crtl
->max_dynamic_stack_alignment
= required_align
;
1253 unsigned extra
, extra_align
;
1255 crtl
->preferred_stack_boundary
= PREFERRED_STACK_BOUNDARY
;
1256 crtl
->max_dynamic_stack_alignment
= PREFERRED_STACK_BOUNDARY
;
1258 extra_align
= PREFERRED_STACK_BOUNDARY
;
1259 extra
= (required_align
- extra_align
) / BITS_PER_UNIT
;
1261 size
= plus_constant (Pmode
, size
, extra
);
1262 size
= force_operand (size
, NULL_RTX
);
1264 if (flag_stack_usage_info
)
1265 stack_usage_size
+= extra
;
1266 if (extra
&& size_align
> extra_align
)
1267 size_align
= extra_align
;
1271 /* Round the size to a multiple of the required stack alignment.
1272 Since the stack is presumed to be rounded before this allocation,
1273 this will maintain the required alignment.
1275 If the stack grows downward, we could save an insn by subtracting
1276 SIZE from the stack pointer and then aligning the stack pointer.
1277 The problem with this is that the stack pointer may be unaligned
1278 between the execution of the subtraction and alignment insns and
1279 some machines do not allow this. Even on those that do, some
1280 signal handlers malfunction if a signal should occur between those
1281 insns. Since this is an extremely rare event, we have no reliable
1282 way of knowing which systems have this problem. So we avoid even
1283 momentarily mis-aligning the stack. */
1284 if (size_align
% MAX_SUPPORTED_STACK_ALIGNMENT
!= 0)
1286 size
= round_push (size
);
1288 if (flag_stack_usage_info
)
1290 int align
= crtl
->preferred_stack_boundary
/ BITS_PER_UNIT
;
1291 stack_usage_size
= (stack_usage_size
+ align
- 1) / align
* align
;
1295 target
= gen_reg_rtx (Pmode
);
1297 /* The size is supposed to be fully adjusted at this point so record it
1298 if stack usage info is requested. */
1299 if (flag_stack_usage_info
)
1301 current_function_dynamic_stack_size
+= stack_usage_size
;
1303 /* ??? This is gross but the only safe stance in the absence
1304 of stack usage oriented flow analysis. */
1305 if (!cannot_accumulate
)
1306 current_function_has_unbounded_dynamic_stack_size
= 1;
1309 final_label
= NULL_RTX
;
1310 final_target
= NULL_RTX
;
1312 /* If we are splitting the stack, we need to ask the backend whether
1313 there is enough room on the current stack. If there isn't, or if
1314 the backend doesn't know how to tell is, then we need to call a
1315 function to allocate memory in some other way. This memory will
1316 be released when we release the current stack segment. The
1317 effect is that stack allocation becomes less efficient, but at
1318 least it doesn't cause a stack overflow. */
1319 if (flag_split_stack
)
1321 rtx available_label
, ask
, space
, func
;
1323 available_label
= NULL_RTX
;
1325 #ifdef HAVE_split_stack_space_check
1326 if (HAVE_split_stack_space_check
)
1328 available_label
= gen_label_rtx ();
1330 /* This instruction will branch to AVAILABLE_LABEL if there
1331 are SIZE bytes available on the stack. */
1332 emit_insn (gen_split_stack_space_check (size
, available_label
));
1336 /* The __morestack_allocate_stack_space function will allocate
1337 memory using malloc. If the alignment of the memory returned
1338 by malloc does not meet REQUIRED_ALIGN, we increase SIZE to
1339 make sure we allocate enough space. */
1340 if (MALLOC_ABI_ALIGNMENT
>= required_align
)
1344 ask
= expand_binop (Pmode
, add_optab
, size
,
1345 GEN_INT (required_align
/ BITS_PER_UNIT
- 1),
1346 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
1350 func
= init_one_libfunc ("__morestack_allocate_stack_space");
1352 space
= emit_library_call_value (func
, target
, LCT_NORMAL
, Pmode
,
1355 if (available_label
== NULL_RTX
)
1358 final_target
= gen_reg_rtx (Pmode
);
1360 emit_move_insn (final_target
, space
);
1362 final_label
= gen_label_rtx ();
1363 emit_jump (final_label
);
1365 emit_label (available_label
);
1368 do_pending_stack_adjust ();
1370 /* We ought to be called always on the toplevel and stack ought to be aligned
1372 gcc_assert (!(stack_pointer_delta
1373 % (PREFERRED_STACK_BOUNDARY
/ BITS_PER_UNIT
)));
1375 /* If needed, check that we have the required amount of stack. Take into
1376 account what has already been checked. */
1377 if (STACK_CHECK_MOVING_SP
)
1379 else if (flag_stack_check
== GENERIC_STACK_CHECK
)
1380 probe_stack_range (STACK_OLD_CHECK_PROTECT
+ STACK_CHECK_MAX_FRAME_SIZE
,
1382 else if (flag_stack_check
== STATIC_BUILTIN_STACK_CHECK
)
1383 probe_stack_range (STACK_CHECK_PROTECT
, size
);
1385 /* Don't let anti_adjust_stack emit notes. */
1386 suppress_reg_args_size
= true;
1388 /* Perform the required allocation from the stack. Some systems do
1389 this differently than simply incrementing/decrementing from the
1390 stack pointer, such as acquiring the space by calling malloc(). */
1391 #ifdef HAVE_allocate_stack
1392 if (HAVE_allocate_stack
)
1394 struct expand_operand ops
[2];
1395 /* We don't have to check against the predicate for operand 0 since
1396 TARGET is known to be a pseudo of the proper mode, which must
1397 be valid for the operand. */
1398 create_fixed_operand (&ops
[0], target
);
1399 create_convert_operand_to (&ops
[1], size
, STACK_SIZE_MODE
, true);
1400 expand_insn (CODE_FOR_allocate_stack
, 2, ops
);
1405 int saved_stack_pointer_delta
;
1407 #ifndef STACK_GROWS_DOWNWARD
1408 emit_move_insn (target
, virtual_stack_dynamic_rtx
);
1411 /* Check stack bounds if necessary. */
1412 if (crtl
->limit_stack
)
1415 rtx space_available
= gen_label_rtx ();
1416 #ifdef STACK_GROWS_DOWNWARD
1417 available
= expand_binop (Pmode
, sub_optab
,
1418 stack_pointer_rtx
, stack_limit_rtx
,
1419 NULL_RTX
, 1, OPTAB_WIDEN
);
1421 available
= expand_binop (Pmode
, sub_optab
,
1422 stack_limit_rtx
, stack_pointer_rtx
,
1423 NULL_RTX
, 1, OPTAB_WIDEN
);
1425 emit_cmp_and_jump_insns (available
, size
, GEU
, NULL_RTX
, Pmode
, 1,
1429 emit_insn (gen_trap ());
1432 error ("stack limits not supported on this target");
1434 emit_label (space_available
);
1437 saved_stack_pointer_delta
= stack_pointer_delta
;
1439 if (flag_stack_check
&& STACK_CHECK_MOVING_SP
)
1440 anti_adjust_stack_and_probe (size
, false);
1442 anti_adjust_stack (size
);
1444 /* Even if size is constant, don't modify stack_pointer_delta.
1445 The constant size alloca should preserve
1446 crtl->preferred_stack_boundary alignment. */
1447 stack_pointer_delta
= saved_stack_pointer_delta
;
1449 #ifdef STACK_GROWS_DOWNWARD
1450 emit_move_insn (target
, virtual_stack_dynamic_rtx
);
1454 suppress_reg_args_size
= false;
1456 /* Finish up the split stack handling. */
1457 if (final_label
!= NULL_RTX
)
1459 gcc_assert (flag_split_stack
);
1460 emit_move_insn (final_target
, target
);
1461 emit_label (final_label
);
1462 target
= final_target
;
1467 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1468 but we know it can't. So add ourselves and then do
1470 target
= expand_binop (Pmode
, add_optab
, target
,
1471 GEN_INT (required_align
/ BITS_PER_UNIT
- 1),
1472 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
1473 target
= expand_divmod (0, TRUNC_DIV_EXPR
, Pmode
, target
,
1474 GEN_INT (required_align
/ BITS_PER_UNIT
),
1476 target
= expand_mult (Pmode
, target
,
1477 GEN_INT (required_align
/ BITS_PER_UNIT
),
1481 /* Now that we've committed to a return value, mark its alignment. */
1482 mark_reg_pointer (target
, required_align
);
1484 /* Record the new stack level for nonlocal gotos. */
1485 if (cfun
->nonlocal_goto_save_area
!= 0)
1486 update_nonlocal_goto_save_area ();
1491 /* A front end may want to override GCC's stack checking by providing a
1492 run-time routine to call to check the stack, so provide a mechanism for
1493 calling that routine. */
1495 static GTY(()) rtx stack_check_libfunc
;
1498 set_stack_check_libfunc (const char *libfunc_name
)
1500 gcc_assert (stack_check_libfunc
== NULL_RTX
);
1501 stack_check_libfunc
= gen_rtx_SYMBOL_REF (Pmode
, libfunc_name
);
1504 /* Emit one stack probe at ADDRESS, an address within the stack. */
1507 emit_stack_probe (rtx address
)
1509 #ifdef HAVE_probe_stack_address
1510 if (HAVE_probe_stack_address
)
1511 emit_insn (gen_probe_stack_address (address
));
1515 rtx memref
= gen_rtx_MEM (word_mode
, address
);
1517 MEM_VOLATILE_P (memref
) = 1;
1519 /* See if we have an insn to probe the stack. */
1520 #ifdef HAVE_probe_stack
1521 if (HAVE_probe_stack
)
1522 emit_insn (gen_probe_stack (memref
));
1525 emit_move_insn (memref
, const0_rtx
);
1529 /* Probe a range of stack addresses from FIRST to FIRST+SIZE, inclusive.
1530 FIRST is a constant and size is a Pmode RTX. These are offsets from
1531 the current stack pointer. STACK_GROWS_DOWNWARD says whether to add
1532 or subtract them from the stack pointer. */
1534 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
1536 #ifdef STACK_GROWS_DOWNWARD
1537 #define STACK_GROW_OP MINUS
1538 #define STACK_GROW_OPTAB sub_optab
1539 #define STACK_GROW_OFF(off) -(off)
1541 #define STACK_GROW_OP PLUS
1542 #define STACK_GROW_OPTAB add_optab
1543 #define STACK_GROW_OFF(off) (off)
1547 probe_stack_range (HOST_WIDE_INT first
, rtx size
)
1549 /* First ensure SIZE is Pmode. */
1550 if (GET_MODE (size
) != VOIDmode
&& GET_MODE (size
) != Pmode
)
1551 size
= convert_to_mode (Pmode
, size
, 1);
1553 /* Next see if we have a function to check the stack. */
1554 if (stack_check_libfunc
)
1556 rtx addr
= memory_address (Pmode
,
1557 gen_rtx_fmt_ee (STACK_GROW_OP
, Pmode
,
1559 plus_constant (Pmode
,
1561 emit_library_call (stack_check_libfunc
, LCT_NORMAL
, VOIDmode
, 1, addr
,
1565 /* Next see if we have an insn to check the stack. */
1566 #ifdef HAVE_check_stack
1567 else if (HAVE_check_stack
)
1569 struct expand_operand ops
[1];
1570 rtx addr
= memory_address (Pmode
,
1571 gen_rtx_fmt_ee (STACK_GROW_OP
, Pmode
,
1573 plus_constant (Pmode
,
1576 create_input_operand (&ops
[0], addr
, Pmode
);
1577 success
= maybe_expand_insn (CODE_FOR_check_stack
, 1, ops
);
1578 gcc_assert (success
);
1582 /* Otherwise we have to generate explicit probes. If we have a constant
1583 small number of them to generate, that's the easy case. */
1584 else if (CONST_INT_P (size
) && INTVAL (size
) < 7 * PROBE_INTERVAL
)
1586 HOST_WIDE_INT isize
= INTVAL (size
), i
;
1589 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
1590 it exceeds SIZE. If only one probe is needed, this will not
1591 generate any code. Then probe at FIRST + SIZE. */
1592 for (i
= PROBE_INTERVAL
; i
< isize
; i
+= PROBE_INTERVAL
)
1594 addr
= memory_address (Pmode
,
1595 plus_constant (Pmode
, stack_pointer_rtx
,
1596 STACK_GROW_OFF (first
+ i
)));
1597 emit_stack_probe (addr
);
1600 addr
= memory_address (Pmode
,
1601 plus_constant (Pmode
, stack_pointer_rtx
,
1602 STACK_GROW_OFF (first
+ isize
)));
1603 emit_stack_probe (addr
);
1606 /* In the variable case, do the same as above, but in a loop. Note that we
1607 must be extra careful with variables wrapping around because we might be
1608 at the very top (or the very bottom) of the address space and we have to
1609 be able to handle this case properly; in particular, we use an equality
1610 test for the loop condition. */
1613 rtx rounded_size
, rounded_size_op
, test_addr
, last_addr
, temp
;
1614 rtx loop_lab
= gen_label_rtx ();
1615 rtx end_lab
= gen_label_rtx ();
1618 /* Step 1: round SIZE to the previous multiple of the interval. */
1620 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1622 = simplify_gen_binary (AND
, Pmode
, size
, GEN_INT (-PROBE_INTERVAL
));
1623 rounded_size_op
= force_operand (rounded_size
, NULL_RTX
);
1626 /* Step 2: compute initial and final value of the loop counter. */
1628 /* TEST_ADDR = SP + FIRST. */
1629 test_addr
= force_operand (gen_rtx_fmt_ee (STACK_GROW_OP
, Pmode
,
1631 GEN_INT (first
)), NULL_RTX
);
1633 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
1634 last_addr
= force_operand (gen_rtx_fmt_ee (STACK_GROW_OP
, Pmode
,
1636 rounded_size_op
), NULL_RTX
);
1641 while (TEST_ADDR != LAST_ADDR)
1643 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
1647 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
1648 until it is equal to ROUNDED_SIZE. */
1650 emit_label (loop_lab
);
1652 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
1653 emit_cmp_and_jump_insns (test_addr
, last_addr
, EQ
, NULL_RTX
, Pmode
, 1,
1656 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
1657 temp
= expand_binop (Pmode
, STACK_GROW_OPTAB
, test_addr
,
1658 GEN_INT (PROBE_INTERVAL
), test_addr
,
1661 gcc_assert (temp
== test_addr
);
1663 /* Probe at TEST_ADDR. */
1664 emit_stack_probe (test_addr
);
1666 emit_jump (loop_lab
);
1668 emit_label (end_lab
);
1671 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
1672 that SIZE is equal to ROUNDED_SIZE. */
1674 /* TEMP = SIZE - ROUNDED_SIZE. */
1675 temp
= simplify_gen_binary (MINUS
, Pmode
, size
, rounded_size
);
1676 if (temp
!= const0_rtx
)
1680 if (CONST_INT_P (temp
))
1682 /* Use [base + disp} addressing mode if supported. */
1683 HOST_WIDE_INT offset
= INTVAL (temp
);
1684 addr
= memory_address (Pmode
,
1685 plus_constant (Pmode
, last_addr
,
1686 STACK_GROW_OFF (offset
)));
1690 /* Manual CSE if the difference is not known at compile-time. */
1691 temp
= gen_rtx_MINUS (Pmode
, size
, rounded_size_op
);
1692 addr
= memory_address (Pmode
,
1693 gen_rtx_fmt_ee (STACK_GROW_OP
, Pmode
,
1697 emit_stack_probe (addr
);
1702 /* Adjust the stack pointer by minus SIZE (an rtx for a number of bytes)
1703 while probing it. This pushes when SIZE is positive. SIZE need not
1704 be constant. If ADJUST_BACK is true, adjust back the stack pointer
1705 by plus SIZE at the end. */
1708 anti_adjust_stack_and_probe (rtx size
, bool adjust_back
)
1710 /* We skip the probe for the first interval + a small dope of 4 words and
1711 probe that many bytes past the specified size to maintain a protection
1712 area at the botton of the stack. */
1713 const int dope
= 4 * UNITS_PER_WORD
;
1715 /* First ensure SIZE is Pmode. */
1716 if (GET_MODE (size
) != VOIDmode
&& GET_MODE (size
) != Pmode
)
1717 size
= convert_to_mode (Pmode
, size
, 1);
1719 /* If we have a constant small number of probes to generate, that's the
1721 if (CONST_INT_P (size
) && INTVAL (size
) < 7 * PROBE_INTERVAL
)
1723 HOST_WIDE_INT isize
= INTVAL (size
), i
;
1724 bool first_probe
= true;
1726 /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
1727 values of N from 1 until it exceeds SIZE. If only one probe is
1728 needed, this will not generate any code. Then adjust and probe
1729 to PROBE_INTERVAL + SIZE. */
1730 for (i
= PROBE_INTERVAL
; i
< isize
; i
+= PROBE_INTERVAL
)
1734 anti_adjust_stack (GEN_INT (2 * PROBE_INTERVAL
+ dope
));
1735 first_probe
= false;
1738 anti_adjust_stack (GEN_INT (PROBE_INTERVAL
));
1739 emit_stack_probe (stack_pointer_rtx
);
1743 anti_adjust_stack (plus_constant (Pmode
, size
, PROBE_INTERVAL
+ dope
));
1745 anti_adjust_stack (plus_constant (Pmode
, size
, PROBE_INTERVAL
- i
));
1746 emit_stack_probe (stack_pointer_rtx
);
1749 /* In the variable case, do the same as above, but in a loop. Note that we
1750 must be extra careful with variables wrapping around because we might be
1751 at the very top (or the very bottom) of the address space and we have to
1752 be able to handle this case properly; in particular, we use an equality
1753 test for the loop condition. */
1756 rtx rounded_size
, rounded_size_op
, last_addr
, temp
;
1757 rtx loop_lab
= gen_label_rtx ();
1758 rtx end_lab
= gen_label_rtx ();
1761 /* Step 1: round SIZE to the previous multiple of the interval. */
1763 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1765 = simplify_gen_binary (AND
, Pmode
, size
, GEN_INT (-PROBE_INTERVAL
));
1766 rounded_size_op
= force_operand (rounded_size
, NULL_RTX
);
1769 /* Step 2: compute initial and final value of the loop counter. */
1771 /* SP = SP_0 + PROBE_INTERVAL. */
1772 anti_adjust_stack (GEN_INT (PROBE_INTERVAL
+ dope
));
1774 /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE. */
1775 last_addr
= force_operand (gen_rtx_fmt_ee (STACK_GROW_OP
, Pmode
,
1777 rounded_size_op
), NULL_RTX
);
1782 while (SP != LAST_ADDR)
1784 SP = SP + PROBE_INTERVAL
1788 adjusts SP and probes at PROBE_INTERVAL + N * PROBE_INTERVAL for
1789 values of N from 1 until it is equal to ROUNDED_SIZE. */
1791 emit_label (loop_lab
);
1793 /* Jump to END_LAB if SP == LAST_ADDR. */
1794 emit_cmp_and_jump_insns (stack_pointer_rtx
, last_addr
, EQ
, NULL_RTX
,
1797 /* SP = SP + PROBE_INTERVAL and probe at SP. */
1798 anti_adjust_stack (GEN_INT (PROBE_INTERVAL
));
1799 emit_stack_probe (stack_pointer_rtx
);
1801 emit_jump (loop_lab
);
1803 emit_label (end_lab
);
1806 /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
1807 assert at compile-time that SIZE is equal to ROUNDED_SIZE. */
1809 /* TEMP = SIZE - ROUNDED_SIZE. */
1810 temp
= simplify_gen_binary (MINUS
, Pmode
, size
, rounded_size
);
1811 if (temp
!= const0_rtx
)
1813 /* Manual CSE if the difference is not known at compile-time. */
1814 if (GET_CODE (temp
) != CONST_INT
)
1815 temp
= gen_rtx_MINUS (Pmode
, size
, rounded_size_op
);
1816 anti_adjust_stack (temp
);
1817 emit_stack_probe (stack_pointer_rtx
);
1821 /* Adjust back and account for the additional first interval. */
1823 adjust_stack (plus_constant (Pmode
, size
, PROBE_INTERVAL
+ dope
));
1825 adjust_stack (GEN_INT (PROBE_INTERVAL
+ dope
));
1828 /* Return an rtx representing the register or memory location
1829 in which a scalar value of data type VALTYPE
1830 was returned by a function call to function FUNC.
1831 FUNC is a FUNCTION_DECL, FNTYPE a FUNCTION_TYPE node if the precise
1832 function is known, otherwise 0.
1833 OUTGOING is 1 if on a machine with register windows this function
1834 should return the register in which the function will put its result
1838 hard_function_value (const_tree valtype
, const_tree func
, const_tree fntype
,
1839 int outgoing ATTRIBUTE_UNUSED
)
1843 val
= targetm
.calls
.function_value (valtype
, func
? func
: fntype
, outgoing
);
1846 && GET_MODE (val
) == BLKmode
)
1848 unsigned HOST_WIDE_INT bytes
= int_size_in_bytes (valtype
);
1849 enum machine_mode tmpmode
;
1851 /* int_size_in_bytes can return -1. We don't need a check here
1852 since the value of bytes will then be large enough that no
1853 mode will match anyway. */
1855 for (tmpmode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
1856 tmpmode
!= VOIDmode
;
1857 tmpmode
= GET_MODE_WIDER_MODE (tmpmode
))
1859 /* Have we found a large enough mode? */
1860 if (GET_MODE_SIZE (tmpmode
) >= bytes
)
1864 /* No suitable mode found. */
1865 gcc_assert (tmpmode
!= VOIDmode
);
1867 PUT_MODE (val
, tmpmode
);
1872 /* Return an rtx representing the register or memory location
1873 in which a scalar value of mode MODE was returned by a library call. */
1876 hard_libcall_value (enum machine_mode mode
, rtx fun
)
1878 return targetm
.calls
.libcall_value (mode
, fun
);
1881 /* Look up the tree code for a given rtx code
1882 to provide the arithmetic operation for REAL_ARITHMETIC.
1883 The function returns an int because the caller may not know
1884 what `enum tree_code' means. */
1887 rtx_to_tree_code (enum rtx_code code
)
1889 enum tree_code tcode
;
1912 tcode
= LAST_AND_UNUSED_TREE_CODE
;
1915 return ((int) tcode
);
1918 #include "gt-explow.h"