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
) <= 2 * HOST_BITS_PER_WIDE_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
;
1176 unsigned extra_align
= 0;
1179 /* If we're asking for zero bytes, it doesn't matter what we point
1180 to since we can't dereference it. But return a reasonable
1182 if (size
== const0_rtx
)
1183 return virtual_stack_dynamic_rtx
;
1185 /* Otherwise, show we're calling alloca or equivalent. */
1186 cfun
->calls_alloca
= 1;
1188 /* If stack usage info is requested, look into the size we are passed.
1189 We need to do so this early to avoid the obfuscation that may be
1190 introduced later by the various alignment operations. */
1191 if (flag_stack_usage_info
)
1193 if (CONST_INT_P (size
))
1194 stack_usage_size
= INTVAL (size
);
1195 else if (REG_P (size
))
1197 /* Look into the last emitted insn and see if we can deduce
1198 something for the register. */
1199 rtx insn
, set
, note
;
1200 insn
= get_last_insn ();
1201 if ((set
= single_set (insn
)) && rtx_equal_p (SET_DEST (set
), size
))
1203 if (CONST_INT_P (SET_SRC (set
)))
1204 stack_usage_size
= INTVAL (SET_SRC (set
));
1205 else if ((note
= find_reg_equal_equiv_note (insn
))
1206 && CONST_INT_P (XEXP (note
, 0)))
1207 stack_usage_size
= INTVAL (XEXP (note
, 0));
1211 /* If the size is not constant, we can't say anything. */
1212 if (stack_usage_size
== -1)
1214 current_function_has_unbounded_dynamic_stack_size
= 1;
1215 stack_usage_size
= 0;
1219 /* Ensure the size is in the proper mode. */
1220 if (GET_MODE (size
) != VOIDmode
&& GET_MODE (size
) != Pmode
)
1221 size
= convert_to_mode (Pmode
, size
, 1);
1223 /* Adjust SIZE_ALIGN, if needed. */
1224 if (CONST_INT_P (size
))
1226 unsigned HOST_WIDE_INT lsb
;
1228 lsb
= INTVAL (size
);
1231 /* Watch out for overflow truncating to "unsigned". */
1232 if (lsb
> UINT_MAX
/ BITS_PER_UNIT
)
1233 size_align
= 1u << (HOST_BITS_PER_INT
- 1);
1235 size_align
= (unsigned)lsb
* BITS_PER_UNIT
;
1237 else if (size_align
< BITS_PER_UNIT
)
1238 size_align
= BITS_PER_UNIT
;
1240 /* We can't attempt to minimize alignment necessary, because we don't
1241 know the final value of preferred_stack_boundary yet while executing
1243 if (crtl
->preferred_stack_boundary
< PREFERRED_STACK_BOUNDARY
)
1244 crtl
->preferred_stack_boundary
= PREFERRED_STACK_BOUNDARY
;
1246 /* We will need to ensure that the address we return is aligned to
1247 REQUIRED_ALIGN. If STACK_DYNAMIC_OFFSET is defined, we don't
1248 always know its final value at this point in the compilation (it
1249 might depend on the size of the outgoing parameter lists, for
1250 example), so we must align the value to be returned in that case.
1251 (Note that STACK_DYNAMIC_OFFSET will have a default nonzero value if
1252 STACK_POINTER_OFFSET or ACCUMULATE_OUTGOING_ARGS are defined).
1253 We must also do an alignment operation on the returned value if
1254 the stack pointer alignment is less strict than REQUIRED_ALIGN.
1256 If we have to align, we must leave space in SIZE for the hole
1257 that might result from the alignment operation. */
1259 must_align
= (crtl
->preferred_stack_boundary
< required_align
);
1262 if (required_align
> PREFERRED_STACK_BOUNDARY
)
1263 extra_align
= PREFERRED_STACK_BOUNDARY
;
1264 else if (required_align
> STACK_BOUNDARY
)
1265 extra_align
= STACK_BOUNDARY
;
1267 extra_align
= BITS_PER_UNIT
;
1270 /* ??? STACK_POINTER_OFFSET is always defined now. */
1271 #if defined (STACK_DYNAMIC_OFFSET) || defined (STACK_POINTER_OFFSET)
1273 extra_align
= BITS_PER_UNIT
;
1278 unsigned extra
= (required_align
- extra_align
) / BITS_PER_UNIT
;
1280 size
= plus_constant (Pmode
, size
, extra
);
1281 size
= force_operand (size
, NULL_RTX
);
1283 if (flag_stack_usage_info
)
1284 stack_usage_size
+= extra
;
1286 if (extra
&& size_align
> extra_align
)
1287 size_align
= extra_align
;
1290 /* Round the size to a multiple of the required stack alignment.
1291 Since the stack if presumed to be rounded before this allocation,
1292 this will maintain the required alignment.
1294 If the stack grows downward, we could save an insn by subtracting
1295 SIZE from the stack pointer and then aligning the stack pointer.
1296 The problem with this is that the stack pointer may be unaligned
1297 between the execution of the subtraction and alignment insns and
1298 some machines do not allow this. Even on those that do, some
1299 signal handlers malfunction if a signal should occur between those
1300 insns. Since this is an extremely rare event, we have no reliable
1301 way of knowing which systems have this problem. So we avoid even
1302 momentarily mis-aligning the stack. */
1303 if (size_align
% MAX_SUPPORTED_STACK_ALIGNMENT
!= 0)
1305 size
= round_push (size
);
1307 if (flag_stack_usage_info
)
1309 int align
= crtl
->preferred_stack_boundary
/ BITS_PER_UNIT
;
1310 stack_usage_size
= (stack_usage_size
+ align
- 1) / align
* align
;
1314 target
= gen_reg_rtx (Pmode
);
1316 /* The size is supposed to be fully adjusted at this point so record it
1317 if stack usage info is requested. */
1318 if (flag_stack_usage_info
)
1320 current_function_dynamic_stack_size
+= stack_usage_size
;
1322 /* ??? This is gross but the only safe stance in the absence
1323 of stack usage oriented flow analysis. */
1324 if (!cannot_accumulate
)
1325 current_function_has_unbounded_dynamic_stack_size
= 1;
1328 final_label
= NULL_RTX
;
1329 final_target
= NULL_RTX
;
1331 /* If we are splitting the stack, we need to ask the backend whether
1332 there is enough room on the current stack. If there isn't, or if
1333 the backend doesn't know how to tell is, then we need to call a
1334 function to allocate memory in some other way. This memory will
1335 be released when we release the current stack segment. The
1336 effect is that stack allocation becomes less efficient, but at
1337 least it doesn't cause a stack overflow. */
1338 if (flag_split_stack
)
1340 rtx available_label
, ask
, space
, func
;
1342 available_label
= NULL_RTX
;
1344 #ifdef HAVE_split_stack_space_check
1345 if (HAVE_split_stack_space_check
)
1347 available_label
= gen_label_rtx ();
1349 /* This instruction will branch to AVAILABLE_LABEL if there
1350 are SIZE bytes available on the stack. */
1351 emit_insn (gen_split_stack_space_check (size
, available_label
));
1355 /* The __morestack_allocate_stack_space function will allocate
1356 memory using malloc. If the alignment of the memory returned
1357 by malloc does not meet REQUIRED_ALIGN, we increase SIZE to
1358 make sure we allocate enough space. */
1359 if (MALLOC_ABI_ALIGNMENT
>= required_align
)
1363 ask
= expand_binop (Pmode
, add_optab
, size
,
1364 GEN_INT (required_align
/ BITS_PER_UNIT
- 1),
1365 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
1369 func
= init_one_libfunc ("__morestack_allocate_stack_space");
1371 space
= emit_library_call_value (func
, target
, LCT_NORMAL
, Pmode
,
1374 if (available_label
== NULL_RTX
)
1377 final_target
= gen_reg_rtx (Pmode
);
1379 emit_move_insn (final_target
, space
);
1381 final_label
= gen_label_rtx ();
1382 emit_jump (final_label
);
1384 emit_label (available_label
);
1387 do_pending_stack_adjust ();
1389 /* We ought to be called always on the toplevel and stack ought to be aligned
1391 gcc_assert (!(stack_pointer_delta
1392 % (PREFERRED_STACK_BOUNDARY
/ BITS_PER_UNIT
)));
1394 /* If needed, check that we have the required amount of stack. Take into
1395 account what has already been checked. */
1396 if (STACK_CHECK_MOVING_SP
)
1398 else if (flag_stack_check
== GENERIC_STACK_CHECK
)
1399 probe_stack_range (STACK_OLD_CHECK_PROTECT
+ STACK_CHECK_MAX_FRAME_SIZE
,
1401 else if (flag_stack_check
== STATIC_BUILTIN_STACK_CHECK
)
1402 probe_stack_range (STACK_CHECK_PROTECT
, size
);
1404 /* Don't let anti_adjust_stack emit notes. */
1405 suppress_reg_args_size
= true;
1407 /* Perform the required allocation from the stack. Some systems do
1408 this differently than simply incrementing/decrementing from the
1409 stack pointer, such as acquiring the space by calling malloc(). */
1410 #ifdef HAVE_allocate_stack
1411 if (HAVE_allocate_stack
)
1413 struct expand_operand ops
[2];
1414 /* We don't have to check against the predicate for operand 0 since
1415 TARGET is known to be a pseudo of the proper mode, which must
1416 be valid for the operand. */
1417 create_fixed_operand (&ops
[0], target
);
1418 create_convert_operand_to (&ops
[1], size
, STACK_SIZE_MODE
, true);
1419 expand_insn (CODE_FOR_allocate_stack
, 2, ops
);
1424 int saved_stack_pointer_delta
;
1426 #ifndef STACK_GROWS_DOWNWARD
1427 emit_move_insn (target
, virtual_stack_dynamic_rtx
);
1430 /* Check stack bounds if necessary. */
1431 if (crtl
->limit_stack
)
1434 rtx space_available
= gen_label_rtx ();
1435 #ifdef STACK_GROWS_DOWNWARD
1436 available
= expand_binop (Pmode
, sub_optab
,
1437 stack_pointer_rtx
, stack_limit_rtx
,
1438 NULL_RTX
, 1, OPTAB_WIDEN
);
1440 available
= expand_binop (Pmode
, sub_optab
,
1441 stack_limit_rtx
, stack_pointer_rtx
,
1442 NULL_RTX
, 1, OPTAB_WIDEN
);
1444 emit_cmp_and_jump_insns (available
, size
, GEU
, NULL_RTX
, Pmode
, 1,
1448 emit_insn (gen_trap ());
1451 error ("stack limits not supported on this target");
1453 emit_label (space_available
);
1456 saved_stack_pointer_delta
= stack_pointer_delta
;
1458 if (flag_stack_check
&& STACK_CHECK_MOVING_SP
)
1459 anti_adjust_stack_and_probe (size
, false);
1461 anti_adjust_stack (size
);
1463 /* Even if size is constant, don't modify stack_pointer_delta.
1464 The constant size alloca should preserve
1465 crtl->preferred_stack_boundary alignment. */
1466 stack_pointer_delta
= saved_stack_pointer_delta
;
1468 #ifdef STACK_GROWS_DOWNWARD
1469 emit_move_insn (target
, virtual_stack_dynamic_rtx
);
1473 suppress_reg_args_size
= false;
1475 /* Finish up the split stack handling. */
1476 if (final_label
!= NULL_RTX
)
1478 gcc_assert (flag_split_stack
);
1479 emit_move_insn (final_target
, target
);
1480 emit_label (final_label
);
1481 target
= final_target
;
1486 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1487 but we know it can't. So add ourselves and then do
1489 target
= expand_binop (Pmode
, add_optab
, target
,
1490 GEN_INT (required_align
/ BITS_PER_UNIT
- 1),
1491 NULL_RTX
, 1, OPTAB_LIB_WIDEN
);
1492 target
= expand_divmod (0, TRUNC_DIV_EXPR
, Pmode
, target
,
1493 GEN_INT (required_align
/ BITS_PER_UNIT
),
1495 target
= expand_mult (Pmode
, target
,
1496 GEN_INT (required_align
/ BITS_PER_UNIT
),
1500 /* Now that we've committed to a return value, mark its alignment. */
1501 mark_reg_pointer (target
, required_align
);
1503 /* Record the new stack level for nonlocal gotos. */
1504 if (cfun
->nonlocal_goto_save_area
!= 0)
1505 update_nonlocal_goto_save_area ();
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 GTY(()) rtx stack_check_libfunc
;
1517 set_stack_check_libfunc (const char *libfunc_name
)
1519 gcc_assert (stack_check_libfunc
== NULL_RTX
);
1520 stack_check_libfunc
= gen_rtx_SYMBOL_REF (Pmode
, libfunc_name
);
1523 /* Emit one stack probe at ADDRESS, an address within the stack. */
1526 emit_stack_probe (rtx address
)
1528 #ifdef HAVE_probe_stack_address
1529 if (HAVE_probe_stack_address
)
1530 emit_insn (gen_probe_stack_address (address
));
1534 rtx memref
= gen_rtx_MEM (word_mode
, address
);
1536 MEM_VOLATILE_P (memref
) = 1;
1538 /* See if we have an insn to probe the stack. */
1539 #ifdef HAVE_probe_stack
1540 if (HAVE_probe_stack
)
1541 emit_insn (gen_probe_stack (memref
));
1544 emit_move_insn (memref
, const0_rtx
);
1548 /* Probe a range of stack addresses from FIRST to FIRST+SIZE, inclusive.
1549 FIRST is a constant and size is a Pmode RTX. These are offsets from
1550 the current stack pointer. STACK_GROWS_DOWNWARD says whether to add
1551 or subtract them from the stack pointer. */
1553 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
1555 #ifdef STACK_GROWS_DOWNWARD
1556 #define STACK_GROW_OP MINUS
1557 #define STACK_GROW_OPTAB sub_optab
1558 #define STACK_GROW_OFF(off) -(off)
1560 #define STACK_GROW_OP PLUS
1561 #define STACK_GROW_OPTAB add_optab
1562 #define STACK_GROW_OFF(off) (off)
1566 probe_stack_range (HOST_WIDE_INT first
, rtx size
)
1568 /* First ensure SIZE is Pmode. */
1569 if (GET_MODE (size
) != VOIDmode
&& GET_MODE (size
) != Pmode
)
1570 size
= convert_to_mode (Pmode
, size
, 1);
1572 /* Next see if we have a function to check the stack. */
1573 if (stack_check_libfunc
)
1575 rtx addr
= memory_address (Pmode
,
1576 gen_rtx_fmt_ee (STACK_GROW_OP
, Pmode
,
1578 plus_constant (Pmode
,
1580 emit_library_call (stack_check_libfunc
, LCT_NORMAL
, VOIDmode
, 1, addr
,
1585 /* Next see if we have an insn to check the stack. */
1586 #ifdef HAVE_check_stack
1587 if (HAVE_check_stack
)
1589 struct expand_operand ops
[1];
1590 rtx addr
= memory_address (Pmode
,
1591 gen_rtx_fmt_ee (STACK_GROW_OP
, Pmode
,
1593 plus_constant (Pmode
,
1596 create_input_operand (&ops
[0], addr
, Pmode
);
1597 if (maybe_expand_insn (CODE_FOR_check_stack
, 1, ops
))
1602 /* Otherwise we have to generate explicit probes. If we have a constant
1603 small number of them to generate, that's the easy case. */
1604 else if (CONST_INT_P (size
) && INTVAL (size
) < 7 * PROBE_INTERVAL
)
1606 HOST_WIDE_INT isize
= INTVAL (size
), i
;
1609 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
1610 it exceeds SIZE. If only one probe is needed, this will not
1611 generate any code. Then probe at FIRST + SIZE. */
1612 for (i
= PROBE_INTERVAL
; i
< isize
; i
+= PROBE_INTERVAL
)
1614 addr
= memory_address (Pmode
,
1615 plus_constant (Pmode
, stack_pointer_rtx
,
1616 STACK_GROW_OFF (first
+ i
)));
1617 emit_stack_probe (addr
);
1620 addr
= memory_address (Pmode
,
1621 plus_constant (Pmode
, stack_pointer_rtx
,
1622 STACK_GROW_OFF (first
+ isize
)));
1623 emit_stack_probe (addr
);
1626 /* In the variable case, do the same as above, but in a loop. Note that we
1627 must be extra careful with variables wrapping around because we might be
1628 at the very top (or the very bottom) of the address space and we have to
1629 be able to handle this case properly; in particular, we use an equality
1630 test for the loop condition. */
1633 rtx rounded_size
, rounded_size_op
, test_addr
, last_addr
, temp
;
1634 rtx loop_lab
= gen_label_rtx ();
1635 rtx end_lab
= gen_label_rtx ();
1638 /* Step 1: round SIZE to the previous multiple of the interval. */
1640 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1642 = simplify_gen_binary (AND
, Pmode
, size
, GEN_INT (-PROBE_INTERVAL
));
1643 rounded_size_op
= force_operand (rounded_size
, NULL_RTX
);
1646 /* Step 2: compute initial and final value of the loop counter. */
1648 /* TEST_ADDR = SP + FIRST. */
1649 test_addr
= force_operand (gen_rtx_fmt_ee (STACK_GROW_OP
, Pmode
,
1651 GEN_INT (first
)), NULL_RTX
);
1653 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
1654 last_addr
= force_operand (gen_rtx_fmt_ee (STACK_GROW_OP
, Pmode
,
1656 rounded_size_op
), NULL_RTX
);
1661 while (TEST_ADDR != LAST_ADDR)
1663 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
1667 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
1668 until it is equal to ROUNDED_SIZE. */
1670 emit_label (loop_lab
);
1672 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
1673 emit_cmp_and_jump_insns (test_addr
, last_addr
, EQ
, NULL_RTX
, Pmode
, 1,
1676 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
1677 temp
= expand_binop (Pmode
, STACK_GROW_OPTAB
, test_addr
,
1678 GEN_INT (PROBE_INTERVAL
), test_addr
,
1681 gcc_assert (temp
== test_addr
);
1683 /* Probe at TEST_ADDR. */
1684 emit_stack_probe (test_addr
);
1686 emit_jump (loop_lab
);
1688 emit_label (end_lab
);
1691 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
1692 that SIZE is equal to ROUNDED_SIZE. */
1694 /* TEMP = SIZE - ROUNDED_SIZE. */
1695 temp
= simplify_gen_binary (MINUS
, Pmode
, size
, rounded_size
);
1696 if (temp
!= const0_rtx
)
1700 if (CONST_INT_P (temp
))
1702 /* Use [base + disp} addressing mode if supported. */
1703 HOST_WIDE_INT offset
= INTVAL (temp
);
1704 addr
= memory_address (Pmode
,
1705 plus_constant (Pmode
, last_addr
,
1706 STACK_GROW_OFF (offset
)));
1710 /* Manual CSE if the difference is not known at compile-time. */
1711 temp
= gen_rtx_MINUS (Pmode
, size
, rounded_size_op
);
1712 addr
= memory_address (Pmode
,
1713 gen_rtx_fmt_ee (STACK_GROW_OP
, Pmode
,
1717 emit_stack_probe (addr
);
1722 /* Adjust the stack pointer by minus SIZE (an rtx for a number of bytes)
1723 while probing it. This pushes when SIZE is positive. SIZE need not
1724 be constant. If ADJUST_BACK is true, adjust back the stack pointer
1725 by plus SIZE at the end. */
1728 anti_adjust_stack_and_probe (rtx size
, bool adjust_back
)
1730 /* We skip the probe for the first interval + a small dope of 4 words and
1731 probe that many bytes past the specified size to maintain a protection
1732 area at the botton of the stack. */
1733 const int dope
= 4 * UNITS_PER_WORD
;
1735 /* First ensure SIZE is Pmode. */
1736 if (GET_MODE (size
) != VOIDmode
&& GET_MODE (size
) != Pmode
)
1737 size
= convert_to_mode (Pmode
, size
, 1);
1739 /* If we have a constant small number of probes to generate, that's the
1741 if (CONST_INT_P (size
) && INTVAL (size
) < 7 * PROBE_INTERVAL
)
1743 HOST_WIDE_INT isize
= INTVAL (size
), i
;
1744 bool first_probe
= true;
1746 /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
1747 values of N from 1 until it exceeds SIZE. If only one probe is
1748 needed, this will not generate any code. Then adjust and probe
1749 to PROBE_INTERVAL + SIZE. */
1750 for (i
= PROBE_INTERVAL
; i
< isize
; i
+= PROBE_INTERVAL
)
1754 anti_adjust_stack (GEN_INT (2 * PROBE_INTERVAL
+ dope
));
1755 first_probe
= false;
1758 anti_adjust_stack (GEN_INT (PROBE_INTERVAL
));
1759 emit_stack_probe (stack_pointer_rtx
);
1763 anti_adjust_stack (plus_constant (Pmode
, size
, PROBE_INTERVAL
+ dope
));
1765 anti_adjust_stack (plus_constant (Pmode
, size
, PROBE_INTERVAL
- i
));
1766 emit_stack_probe (stack_pointer_rtx
);
1769 /* In the variable case, do the same as above, but in a loop. Note that we
1770 must be extra careful with variables wrapping around because we might be
1771 at the very top (or the very bottom) of the address space and we have to
1772 be able to handle this case properly; in particular, we use an equality
1773 test for the loop condition. */
1776 rtx rounded_size
, rounded_size_op
, last_addr
, temp
;
1777 rtx loop_lab
= gen_label_rtx ();
1778 rtx end_lab
= gen_label_rtx ();
1781 /* Step 1: round SIZE to the previous multiple of the interval. */
1783 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1785 = simplify_gen_binary (AND
, Pmode
, size
, GEN_INT (-PROBE_INTERVAL
));
1786 rounded_size_op
= force_operand (rounded_size
, NULL_RTX
);
1789 /* Step 2: compute initial and final value of the loop counter. */
1791 /* SP = SP_0 + PROBE_INTERVAL. */
1792 anti_adjust_stack (GEN_INT (PROBE_INTERVAL
+ dope
));
1794 /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE. */
1795 last_addr
= force_operand (gen_rtx_fmt_ee (STACK_GROW_OP
, Pmode
,
1797 rounded_size_op
), NULL_RTX
);
1802 while (SP != LAST_ADDR)
1804 SP = SP + PROBE_INTERVAL
1808 adjusts SP and probes at PROBE_INTERVAL + N * PROBE_INTERVAL for
1809 values of N from 1 until it is equal to ROUNDED_SIZE. */
1811 emit_label (loop_lab
);
1813 /* Jump to END_LAB if SP == LAST_ADDR. */
1814 emit_cmp_and_jump_insns (stack_pointer_rtx
, last_addr
, EQ
, NULL_RTX
,
1817 /* SP = SP + PROBE_INTERVAL and probe at SP. */
1818 anti_adjust_stack (GEN_INT (PROBE_INTERVAL
));
1819 emit_stack_probe (stack_pointer_rtx
);
1821 emit_jump (loop_lab
);
1823 emit_label (end_lab
);
1826 /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
1827 assert at compile-time that SIZE is equal to ROUNDED_SIZE. */
1829 /* TEMP = SIZE - ROUNDED_SIZE. */
1830 temp
= simplify_gen_binary (MINUS
, Pmode
, size
, rounded_size
);
1831 if (temp
!= const0_rtx
)
1833 /* Manual CSE if the difference is not known at compile-time. */
1834 if (GET_CODE (temp
) != CONST_INT
)
1835 temp
= gen_rtx_MINUS (Pmode
, size
, rounded_size_op
);
1836 anti_adjust_stack (temp
);
1837 emit_stack_probe (stack_pointer_rtx
);
1841 /* Adjust back and account for the additional first interval. */
1843 adjust_stack (plus_constant (Pmode
, size
, PROBE_INTERVAL
+ dope
));
1845 adjust_stack (GEN_INT (PROBE_INTERVAL
+ dope
));
1848 /* Return an rtx representing the register or memory location
1849 in which a scalar value of data type VALTYPE
1850 was returned by a function call to function FUNC.
1851 FUNC is a FUNCTION_DECL, FNTYPE a FUNCTION_TYPE node if the precise
1852 function is known, otherwise 0.
1853 OUTGOING is 1 if on a machine with register windows this function
1854 should return the register in which the function will put its result
1858 hard_function_value (const_tree valtype
, const_tree func
, const_tree fntype
,
1859 int outgoing ATTRIBUTE_UNUSED
)
1863 val
= targetm
.calls
.function_value (valtype
, func
? func
: fntype
, outgoing
);
1866 && GET_MODE (val
) == BLKmode
)
1868 unsigned HOST_WIDE_INT bytes
= int_size_in_bytes (valtype
);
1869 enum machine_mode tmpmode
;
1871 /* int_size_in_bytes can return -1. We don't need a check here
1872 since the value of bytes will then be large enough that no
1873 mode will match anyway. */
1875 for (tmpmode
= GET_CLASS_NARROWEST_MODE (MODE_INT
);
1876 tmpmode
!= VOIDmode
;
1877 tmpmode
= GET_MODE_WIDER_MODE (tmpmode
))
1879 /* Have we found a large enough mode? */
1880 if (GET_MODE_SIZE (tmpmode
) >= bytes
)
1884 /* No suitable mode found. */
1885 gcc_assert (tmpmode
!= VOIDmode
);
1887 PUT_MODE (val
, tmpmode
);
1892 /* Return an rtx representing the register or memory location
1893 in which a scalar value of mode MODE was returned by a library call. */
1896 hard_libcall_value (enum machine_mode mode
, rtx fun
)
1898 return targetm
.calls
.libcall_value (mode
, fun
);
1901 /* Look up the tree code for a given rtx code
1902 to provide the arithmetic operation for REAL_ARITHMETIC.
1903 The function returns an int because the caller may not know
1904 what `enum tree_code' means. */
1907 rtx_to_tree_code (enum rtx_code code
)
1909 enum tree_code tcode
;
1932 tcode
= LAST_AND_UNUSED_TREE_CODE
;
1935 return ((int) tcode
);
1938 #include "gt-explow.h"