[gcc/]
[official-gcc.git] / gcc / explow.c
blob92c4e574dcb8517edfe486b7e9bf8674b4cab6ba
1 /* Subroutines for manipulating rtx's in semantically interesting ways.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "diagnostic-core.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "stor-layout.h"
29 #include "tm_p.h"
30 #include "flags.h"
31 #include "except.h"
32 #include "function.h"
33 #include "expr.h"
34 #include "optabs.h"
35 #include "libfuncs.h"
36 #include "hard-reg-set.h"
37 #include "insn-config.h"
38 #include "ggc.h"
39 #include "recog.h"
40 #include "langhooks.h"
41 #include "target.h"
42 #include "common/common-target.h"
43 #include "output.h"
45 static rtx break_out_memory_refs (rtx);
48 /* Truncate and perhaps sign-extend C as appropriate for MODE. */
50 HOST_WIDE_INT
51 trunc_int_for_mode (HOST_WIDE_INT c, enum machine_mode mode)
53 int width = GET_MODE_PRECISION (mode);
55 /* You want to truncate to a _what_? */
56 gcc_assert (SCALAR_INT_MODE_P (mode));
58 /* Canonicalize BImode to 0 and STORE_FLAG_VALUE. */
59 if (mode == BImode)
60 return c & 1 ? STORE_FLAG_VALUE : 0;
62 /* Sign-extend for the requested mode. */
64 if (width < HOST_BITS_PER_WIDE_INT)
66 HOST_WIDE_INT sign = 1;
67 sign <<= width - 1;
68 c &= (sign << 1) - 1;
69 c ^= sign;
70 c -= sign;
73 return c;
76 /* Return an rtx for the sum of X and the integer C, given that X has
77 mode MODE. INPLACE is true if X can be modified inplace or false
78 if it must be treated as immutable. */
80 rtx
81 plus_constant (enum machine_mode mode, rtx x, HOST_WIDE_INT c,
82 bool inplace)
84 RTX_CODE code;
85 rtx y;
86 rtx tem;
87 int all_constant = 0;
89 gcc_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
91 if (c == 0)
92 return x;
94 restart:
96 code = GET_CODE (x);
97 y = x;
99 switch (code)
101 CASE_CONST_SCALAR_INT:
102 return immed_wide_int_const (wi::add (std::make_pair (x, mode), c),
103 mode);
104 case MEM:
105 /* If this is a reference to the constant pool, try replacing it with
106 a reference to a new constant. If the resulting address isn't
107 valid, don't return it because we have no way to validize it. */
108 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
109 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
111 tem = plus_constant (mode, get_pool_constant (XEXP (x, 0)), c);
112 tem = force_const_mem (GET_MODE (x), tem);
113 if (memory_address_p (GET_MODE (tem), XEXP (tem, 0)))
114 return tem;
116 break;
118 case CONST:
119 /* If adding to something entirely constant, set a flag
120 so that we can add a CONST around the result. */
121 if (inplace && shared_const_p (x))
122 inplace = false;
123 x = XEXP (x, 0);
124 all_constant = 1;
125 goto restart;
127 case SYMBOL_REF:
128 case LABEL_REF:
129 all_constant = 1;
130 break;
132 case PLUS:
133 /* The interesting case is adding the integer to a sum. Look
134 for constant term in the sum and combine with C. For an
135 integer constant term or a constant term that is not an
136 explicit integer, we combine or group them together anyway.
138 We may not immediately return from the recursive call here, lest
139 all_constant gets lost. */
141 if (CONSTANT_P (XEXP (x, 1)))
143 rtx term = plus_constant (mode, XEXP (x, 1), c, inplace);
144 if (term == const0_rtx)
145 x = XEXP (x, 0);
146 else if (inplace)
147 XEXP (x, 1) = term;
148 else
149 x = gen_rtx_PLUS (mode, XEXP (x, 0), term);
150 c = 0;
152 else if (rtx *const_loc = find_constant_term_loc (&y))
154 if (!inplace)
156 /* We need to be careful since X may be shared and we can't
157 modify it in place. */
158 x = copy_rtx (x);
159 const_loc = find_constant_term_loc (&x);
161 *const_loc = plus_constant (mode, *const_loc, c, true);
162 c = 0;
164 break;
166 default:
167 break;
170 if (c != 0)
171 x = gen_rtx_PLUS (mode, x, gen_int_mode (c, mode));
173 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
174 return x;
175 else if (all_constant)
176 return gen_rtx_CONST (mode, x);
177 else
178 return x;
181 /* If X is a sum, return a new sum like X but lacking any constant terms.
182 Add all the removed constant terms into *CONSTPTR.
183 X itself is not altered. The result != X if and only if
184 it is not isomorphic to X. */
187 eliminate_constant_term (rtx x, rtx *constptr)
189 rtx x0, x1;
190 rtx tem;
192 if (GET_CODE (x) != PLUS)
193 return x;
195 /* First handle constants appearing at this level explicitly. */
196 if (CONST_INT_P (XEXP (x, 1))
197 && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x), *constptr,
198 XEXP (x, 1)))
199 && CONST_INT_P (tem))
201 *constptr = tem;
202 return eliminate_constant_term (XEXP (x, 0), constptr);
205 tem = const0_rtx;
206 x0 = eliminate_constant_term (XEXP (x, 0), &tem);
207 x1 = eliminate_constant_term (XEXP (x, 1), &tem);
208 if ((x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
209 && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x),
210 *constptr, tem))
211 && CONST_INT_P (tem))
213 *constptr = tem;
214 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
217 return x;
220 /* Returns a tree for the size of EXP in bytes. */
222 static tree
223 tree_expr_size (const_tree exp)
225 if (DECL_P (exp)
226 && DECL_SIZE_UNIT (exp) != 0)
227 return DECL_SIZE_UNIT (exp);
228 else
229 return size_in_bytes (TREE_TYPE (exp));
232 /* Return an rtx for the size in bytes of the value of EXP. */
235 expr_size (tree exp)
237 tree size;
239 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
240 size = TREE_OPERAND (exp, 1);
241 else
243 size = tree_expr_size (exp);
244 gcc_assert (size);
245 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
248 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
251 /* Return a wide integer for the size in bytes of the value of EXP, or -1
252 if the size can vary or is larger than an integer. */
254 HOST_WIDE_INT
255 int_expr_size (tree exp)
257 tree size;
259 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
260 size = TREE_OPERAND (exp, 1);
261 else
263 size = tree_expr_size (exp);
264 gcc_assert (size);
267 if (size == 0 || !tree_fits_shwi_p (size))
268 return -1;
270 return tree_to_shwi (size);
273 /* Return a copy of X in which all memory references
274 and all constants that involve symbol refs
275 have been replaced with new temporary registers.
276 Also emit code to load the memory locations and constants
277 into those registers.
279 If X contains no such constants or memory references,
280 X itself (not a copy) is returned.
282 If a constant is found in the address that is not a legitimate constant
283 in an insn, it is left alone in the hope that it might be valid in the
284 address.
286 X may contain no arithmetic except addition, subtraction and multiplication.
287 Values returned by expand_expr with 1 for sum_ok fit this constraint. */
289 static rtx
290 break_out_memory_refs (rtx x)
292 if (MEM_P (x)
293 || (CONSTANT_P (x) && CONSTANT_ADDRESS_P (x)
294 && GET_MODE (x) != VOIDmode))
295 x = force_reg (GET_MODE (x), x);
296 else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
297 || GET_CODE (x) == MULT)
299 rtx op0 = break_out_memory_refs (XEXP (x, 0));
300 rtx op1 = break_out_memory_refs (XEXP (x, 1));
302 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
303 x = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
306 return x;
309 /* Given X, a memory address in address space AS' pointer mode, convert it to
310 an address in the address space's address mode, or vice versa (TO_MODE says
311 which way). We take advantage of the fact that pointers are not allowed to
312 overflow by commuting arithmetic operations over conversions so that address
313 arithmetic insns can be used. */
316 convert_memory_address_addr_space (enum machine_mode to_mode ATTRIBUTE_UNUSED,
317 rtx x, addr_space_t as ATTRIBUTE_UNUSED)
319 #ifndef POINTERS_EXTEND_UNSIGNED
320 gcc_assert (GET_MODE (x) == to_mode || GET_MODE (x) == VOIDmode);
321 return x;
322 #else /* defined(POINTERS_EXTEND_UNSIGNED) */
323 enum machine_mode pointer_mode, address_mode, from_mode;
324 rtx temp;
325 enum rtx_code code;
327 /* If X already has the right mode, just return it. */
328 if (GET_MODE (x) == to_mode)
329 return x;
331 pointer_mode = targetm.addr_space.pointer_mode (as);
332 address_mode = targetm.addr_space.address_mode (as);
333 from_mode = to_mode == pointer_mode ? address_mode : pointer_mode;
335 /* Here we handle some special cases. If none of them apply, fall through
336 to the default case. */
337 switch (GET_CODE (x))
339 CASE_CONST_SCALAR_INT:
340 if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode))
341 code = TRUNCATE;
342 else if (POINTERS_EXTEND_UNSIGNED < 0)
343 break;
344 else if (POINTERS_EXTEND_UNSIGNED > 0)
345 code = ZERO_EXTEND;
346 else
347 code = SIGN_EXTEND;
348 temp = simplify_unary_operation (code, to_mode, x, from_mode);
349 if (temp)
350 return temp;
351 break;
353 case SUBREG:
354 if ((SUBREG_PROMOTED_VAR_P (x) || REG_POINTER (SUBREG_REG (x)))
355 && GET_MODE (SUBREG_REG (x)) == to_mode)
356 return SUBREG_REG (x);
357 break;
359 case LABEL_REF:
360 temp = gen_rtx_LABEL_REF (to_mode, XEXP (x, 0));
361 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
362 return temp;
363 break;
365 case SYMBOL_REF:
366 temp = shallow_copy_rtx (x);
367 PUT_MODE (temp, to_mode);
368 return temp;
369 break;
371 case CONST:
372 return gen_rtx_CONST (to_mode,
373 convert_memory_address_addr_space
374 (to_mode, XEXP (x, 0), as));
375 break;
377 case PLUS:
378 case MULT:
379 /* FIXME: For addition, we used to permute the conversion and
380 addition operation only if one operand is a constant and
381 converting the constant does not change it or if one operand
382 is a constant and we are using a ptr_extend instruction
383 (POINTERS_EXTEND_UNSIGNED < 0) even if the resulting address
384 may overflow/underflow. We relax the condition to include
385 zero-extend (POINTERS_EXTEND_UNSIGNED > 0) since the other
386 parts of the compiler depend on it. See PR 49721.
388 We can always safely permute them if we are making the address
389 narrower. */
390 if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode)
391 || (GET_CODE (x) == PLUS
392 && CONST_INT_P (XEXP (x, 1))
393 && (POINTERS_EXTEND_UNSIGNED != 0
394 || XEXP (x, 1) == convert_memory_address_addr_space
395 (to_mode, XEXP (x, 1), as))))
396 return gen_rtx_fmt_ee (GET_CODE (x), to_mode,
397 convert_memory_address_addr_space
398 (to_mode, XEXP (x, 0), as),
399 XEXP (x, 1));
400 break;
402 default:
403 break;
406 return convert_modes (to_mode, from_mode,
407 x, POINTERS_EXTEND_UNSIGNED);
408 #endif /* defined(POINTERS_EXTEND_UNSIGNED) */
411 /* Return something equivalent to X but valid as a memory address for something
412 of mode MODE in the named address space AS. When X is not itself valid,
413 this works by copying X or subexpressions of it into registers. */
416 memory_address_addr_space (enum machine_mode mode, rtx x, addr_space_t as)
418 rtx oldx = x;
419 enum machine_mode address_mode = targetm.addr_space.address_mode (as);
421 x = convert_memory_address_addr_space (address_mode, x, as);
423 /* By passing constant addresses through registers
424 we get a chance to cse them. */
425 if (! cse_not_expected && CONSTANT_P (x) && CONSTANT_ADDRESS_P (x))
426 x = force_reg (address_mode, x);
428 /* We get better cse by rejecting indirect addressing at this stage.
429 Let the combiner create indirect addresses where appropriate.
430 For now, generate the code so that the subexpressions useful to share
431 are visible. But not if cse won't be done! */
432 else
434 if (! cse_not_expected && !REG_P (x))
435 x = break_out_memory_refs (x);
437 /* At this point, any valid address is accepted. */
438 if (memory_address_addr_space_p (mode, x, as))
439 goto done;
441 /* If it was valid before but breaking out memory refs invalidated it,
442 use it the old way. */
443 if (memory_address_addr_space_p (mode, oldx, as))
445 x = oldx;
446 goto done;
449 /* Perform machine-dependent transformations on X
450 in certain cases. This is not necessary since the code
451 below can handle all possible cases, but machine-dependent
452 transformations can make better code. */
454 rtx orig_x = x;
455 x = targetm.addr_space.legitimize_address (x, oldx, mode, as);
456 if (orig_x != x && memory_address_addr_space_p (mode, x, as))
457 goto done;
460 /* PLUS and MULT can appear in special ways
461 as the result of attempts to make an address usable for indexing.
462 Usually they are dealt with by calling force_operand, below.
463 But a sum containing constant terms is special
464 if removing them makes the sum a valid address:
465 then we generate that address in a register
466 and index off of it. We do this because it often makes
467 shorter code, and because the addresses thus generated
468 in registers often become common subexpressions. */
469 if (GET_CODE (x) == PLUS)
471 rtx constant_term = const0_rtx;
472 rtx y = eliminate_constant_term (x, &constant_term);
473 if (constant_term == const0_rtx
474 || ! memory_address_addr_space_p (mode, y, as))
475 x = force_operand (x, NULL_RTX);
476 else
478 y = gen_rtx_PLUS (GET_MODE (x), copy_to_reg (y), constant_term);
479 if (! memory_address_addr_space_p (mode, y, as))
480 x = force_operand (x, NULL_RTX);
481 else
482 x = y;
486 else if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
487 x = force_operand (x, NULL_RTX);
489 /* If we have a register that's an invalid address,
490 it must be a hard reg of the wrong class. Copy it to a pseudo. */
491 else if (REG_P (x))
492 x = copy_to_reg (x);
494 /* Last resort: copy the value to a register, since
495 the register is a valid address. */
496 else
497 x = force_reg (address_mode, x);
500 done:
502 gcc_assert (memory_address_addr_space_p (mode, x, as));
503 /* If we didn't change the address, we are done. Otherwise, mark
504 a reg as a pointer if we have REG or REG + CONST_INT. */
505 if (oldx == x)
506 return x;
507 else if (REG_P (x))
508 mark_reg_pointer (x, BITS_PER_UNIT);
509 else if (GET_CODE (x) == PLUS
510 && REG_P (XEXP (x, 0))
511 && CONST_INT_P (XEXP (x, 1)))
512 mark_reg_pointer (XEXP (x, 0), BITS_PER_UNIT);
514 /* OLDX may have been the address on a temporary. Update the address
515 to indicate that X is now used. */
516 update_temp_slot_address (oldx, x);
518 return x;
521 /* If REF is a MEM with an invalid address, change it into a valid address.
522 Pass through anything else unchanged. REF must be an unshared rtx and
523 the function may modify it in-place. */
526 validize_mem (rtx ref)
528 if (!MEM_P (ref))
529 return ref;
530 ref = use_anchored_address (ref);
531 if (memory_address_addr_space_p (GET_MODE (ref), XEXP (ref, 0),
532 MEM_ADDR_SPACE (ref)))
533 return ref;
535 return replace_equiv_address (ref, XEXP (ref, 0), true);
538 /* If X is a memory reference to a member of an object block, try rewriting
539 it to use an anchor instead. Return the new memory reference on success
540 and the old one on failure. */
543 use_anchored_address (rtx x)
545 rtx base;
546 HOST_WIDE_INT offset;
547 enum machine_mode mode;
549 if (!flag_section_anchors)
550 return x;
552 if (!MEM_P (x))
553 return x;
555 /* Split the address into a base and offset. */
556 base = XEXP (x, 0);
557 offset = 0;
558 if (GET_CODE (base) == CONST
559 && GET_CODE (XEXP (base, 0)) == PLUS
560 && CONST_INT_P (XEXP (XEXP (base, 0), 1)))
562 offset += INTVAL (XEXP (XEXP (base, 0), 1));
563 base = XEXP (XEXP (base, 0), 0);
566 /* Check whether BASE is suitable for anchors. */
567 if (GET_CODE (base) != SYMBOL_REF
568 || !SYMBOL_REF_HAS_BLOCK_INFO_P (base)
569 || SYMBOL_REF_ANCHOR_P (base)
570 || SYMBOL_REF_BLOCK (base) == NULL
571 || !targetm.use_anchors_for_symbol_p (base))
572 return x;
574 /* Decide where BASE is going to be. */
575 place_block_symbol (base);
577 /* Get the anchor we need to use. */
578 offset += SYMBOL_REF_BLOCK_OFFSET (base);
579 base = get_section_anchor (SYMBOL_REF_BLOCK (base), offset,
580 SYMBOL_REF_TLS_MODEL (base));
582 /* Work out the offset from the anchor. */
583 offset -= SYMBOL_REF_BLOCK_OFFSET (base);
585 /* If we're going to run a CSE pass, force the anchor into a register.
586 We will then be able to reuse registers for several accesses, if the
587 target costs say that that's worthwhile. */
588 mode = GET_MODE (base);
589 if (!cse_not_expected)
590 base = force_reg (mode, base);
592 return replace_equiv_address (x, plus_constant (mode, base, offset));
595 /* Copy the value or contents of X to a new temp reg and return that reg. */
598 copy_to_reg (rtx x)
600 rtx temp = gen_reg_rtx (GET_MODE (x));
602 /* If not an operand, must be an address with PLUS and MULT so
603 do the computation. */
604 if (! general_operand (x, VOIDmode))
605 x = force_operand (x, temp);
607 if (x != temp)
608 emit_move_insn (temp, x);
610 return temp;
613 /* Like copy_to_reg but always give the new register mode Pmode
614 in case X is a constant. */
617 copy_addr_to_reg (rtx x)
619 return copy_to_mode_reg (Pmode, x);
622 /* Like copy_to_reg but always give the new register mode MODE
623 in case X is a constant. */
626 copy_to_mode_reg (enum machine_mode mode, rtx x)
628 rtx temp = gen_reg_rtx (mode);
630 /* If not an operand, must be an address with PLUS and MULT so
631 do the computation. */
632 if (! general_operand (x, VOIDmode))
633 x = force_operand (x, temp);
635 gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
636 if (x != temp)
637 emit_move_insn (temp, x);
638 return temp;
641 /* Load X into a register if it is not already one.
642 Use mode MODE for the register.
643 X should be valid for mode MODE, but it may be a constant which
644 is valid for all integer modes; that's why caller must specify MODE.
646 The caller must not alter the value in the register we return,
647 since we mark it as a "constant" register. */
650 force_reg (enum machine_mode mode, rtx x)
652 rtx temp, insn, set;
654 if (REG_P (x))
655 return x;
657 if (general_operand (x, mode))
659 temp = gen_reg_rtx (mode);
660 insn = emit_move_insn (temp, x);
662 else
664 temp = force_operand (x, NULL_RTX);
665 if (REG_P (temp))
666 insn = get_last_insn ();
667 else
669 rtx temp2 = gen_reg_rtx (mode);
670 insn = emit_move_insn (temp2, temp);
671 temp = temp2;
675 /* Let optimizers know that TEMP's value never changes
676 and that X can be substituted for it. Don't get confused
677 if INSN set something else (such as a SUBREG of TEMP). */
678 if (CONSTANT_P (x)
679 && (set = single_set (insn)) != 0
680 && SET_DEST (set) == temp
681 && ! rtx_equal_p (x, SET_SRC (set)))
682 set_unique_reg_note (insn, REG_EQUAL, x);
684 /* Let optimizers know that TEMP is a pointer, and if so, the
685 known alignment of that pointer. */
687 unsigned align = 0;
688 if (GET_CODE (x) == SYMBOL_REF)
690 align = BITS_PER_UNIT;
691 if (SYMBOL_REF_DECL (x) && DECL_P (SYMBOL_REF_DECL (x)))
692 align = DECL_ALIGN (SYMBOL_REF_DECL (x));
694 else if (GET_CODE (x) == LABEL_REF)
695 align = BITS_PER_UNIT;
696 else if (GET_CODE (x) == CONST
697 && GET_CODE (XEXP (x, 0)) == PLUS
698 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
699 && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
701 rtx s = XEXP (XEXP (x, 0), 0);
702 rtx c = XEXP (XEXP (x, 0), 1);
703 unsigned sa, ca;
705 sa = BITS_PER_UNIT;
706 if (SYMBOL_REF_DECL (s) && DECL_P (SYMBOL_REF_DECL (s)))
707 sa = DECL_ALIGN (SYMBOL_REF_DECL (s));
709 if (INTVAL (c) == 0)
710 align = sa;
711 else
713 ca = ctz_hwi (INTVAL (c)) * BITS_PER_UNIT;
714 align = MIN (sa, ca);
718 if (align || (MEM_P (x) && MEM_POINTER (x)))
719 mark_reg_pointer (temp, align);
722 return temp;
725 /* If X is a memory ref, copy its contents to a new temp reg and return
726 that reg. Otherwise, return X. */
729 force_not_mem (rtx x)
731 rtx temp;
733 if (!MEM_P (x) || GET_MODE (x) == BLKmode)
734 return x;
736 temp = gen_reg_rtx (GET_MODE (x));
738 if (MEM_POINTER (x))
739 REG_POINTER (temp) = 1;
741 emit_move_insn (temp, x);
742 return temp;
745 /* Copy X to TARGET (if it's nonzero and a reg)
746 or to a new temp reg and return that reg.
747 MODE is the mode to use for X in case it is a constant. */
750 copy_to_suggested_reg (rtx x, rtx target, enum machine_mode mode)
752 rtx temp;
754 if (target && REG_P (target))
755 temp = target;
756 else
757 temp = gen_reg_rtx (mode);
759 emit_move_insn (temp, x);
760 return temp;
763 /* Return the mode to use to pass or return a scalar of TYPE and MODE.
764 PUNSIGNEDP points to the signedness of the type and may be adjusted
765 to show what signedness to use on extension operations.
767 FOR_RETURN is nonzero if the caller is promoting the return value
768 of FNDECL, else it is for promoting args. */
770 enum machine_mode
771 promote_function_mode (const_tree type, enum machine_mode mode, int *punsignedp,
772 const_tree funtype, int for_return)
774 /* Called without a type node for a libcall. */
775 if (type == NULL_TREE)
777 if (INTEGRAL_MODE_P (mode))
778 return targetm.calls.promote_function_mode (NULL_TREE, mode,
779 punsignedp, funtype,
780 for_return);
781 else
782 return mode;
785 switch (TREE_CODE (type))
787 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
788 case REAL_TYPE: case OFFSET_TYPE: case FIXED_POINT_TYPE:
789 case POINTER_TYPE: case REFERENCE_TYPE:
790 return targetm.calls.promote_function_mode (type, mode, punsignedp, funtype,
791 for_return);
793 default:
794 return mode;
797 /* Return the mode to use to store a scalar of TYPE and MODE.
798 PUNSIGNEDP points to the signedness of the type and may be adjusted
799 to show what signedness to use on extension operations. */
801 enum machine_mode
802 promote_mode (const_tree type ATTRIBUTE_UNUSED, enum machine_mode mode,
803 int *punsignedp ATTRIBUTE_UNUSED)
805 #ifdef PROMOTE_MODE
806 enum tree_code code;
807 int unsignedp;
808 #endif
810 /* For libcalls this is invoked without TYPE from the backends
811 TARGET_PROMOTE_FUNCTION_MODE hooks. Don't do anything in that
812 case. */
813 if (type == NULL_TREE)
814 return mode;
816 /* FIXME: this is the same logic that was there until GCC 4.4, but we
817 probably want to test POINTERS_EXTEND_UNSIGNED even if PROMOTE_MODE
818 is not defined. The affected targets are M32C, S390, SPARC. */
819 #ifdef PROMOTE_MODE
820 code = TREE_CODE (type);
821 unsignedp = *punsignedp;
823 switch (code)
825 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
826 case REAL_TYPE: case OFFSET_TYPE: case FIXED_POINT_TYPE:
827 PROMOTE_MODE (mode, unsignedp, type);
828 *punsignedp = unsignedp;
829 return mode;
830 break;
832 #ifdef POINTERS_EXTEND_UNSIGNED
833 case REFERENCE_TYPE:
834 case POINTER_TYPE:
835 *punsignedp = POINTERS_EXTEND_UNSIGNED;
836 return targetm.addr_space.address_mode
837 (TYPE_ADDR_SPACE (TREE_TYPE (type)));
838 break;
839 #endif
841 default:
842 return mode;
844 #else
845 return mode;
846 #endif
850 /* Use one of promote_mode or promote_function_mode to find the promoted
851 mode of DECL. If PUNSIGNEDP is not NULL, store there the unsignedness
852 of DECL after promotion. */
854 enum machine_mode
855 promote_decl_mode (const_tree decl, int *punsignedp)
857 tree type = TREE_TYPE (decl);
858 int unsignedp = TYPE_UNSIGNED (type);
859 enum machine_mode mode = DECL_MODE (decl);
860 enum machine_mode pmode;
862 if (TREE_CODE (decl) == RESULT_DECL
863 || TREE_CODE (decl) == PARM_DECL)
864 pmode = promote_function_mode (type, mode, &unsignedp,
865 TREE_TYPE (current_function_decl), 2);
866 else
867 pmode = promote_mode (type, mode, &unsignedp);
869 if (punsignedp)
870 *punsignedp = unsignedp;
871 return pmode;
875 /* Controls the behaviour of {anti_,}adjust_stack. */
876 static bool suppress_reg_args_size;
878 /* A helper for adjust_stack and anti_adjust_stack. */
880 static void
881 adjust_stack_1 (rtx adjust, bool anti_p)
883 rtx temp, insn;
885 #ifndef STACK_GROWS_DOWNWARD
886 /* Hereafter anti_p means subtract_p. */
887 anti_p = !anti_p;
888 #endif
890 temp = expand_binop (Pmode,
891 anti_p ? sub_optab : add_optab,
892 stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
893 OPTAB_LIB_WIDEN);
895 if (temp != stack_pointer_rtx)
896 insn = emit_move_insn (stack_pointer_rtx, temp);
897 else
899 insn = get_last_insn ();
900 temp = single_set (insn);
901 gcc_assert (temp != NULL && SET_DEST (temp) == stack_pointer_rtx);
904 if (!suppress_reg_args_size)
905 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
908 /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
909 This pops when ADJUST is positive. ADJUST need not be constant. */
911 void
912 adjust_stack (rtx adjust)
914 if (adjust == const0_rtx)
915 return;
917 /* We expect all variable sized adjustments to be multiple of
918 PREFERRED_STACK_BOUNDARY. */
919 if (CONST_INT_P (adjust))
920 stack_pointer_delta -= INTVAL (adjust);
922 adjust_stack_1 (adjust, false);
925 /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
926 This pushes when ADJUST is positive. ADJUST need not be constant. */
928 void
929 anti_adjust_stack (rtx adjust)
931 if (adjust == const0_rtx)
932 return;
934 /* We expect all variable sized adjustments to be multiple of
935 PREFERRED_STACK_BOUNDARY. */
936 if (CONST_INT_P (adjust))
937 stack_pointer_delta += INTVAL (adjust);
939 adjust_stack_1 (adjust, true);
942 /* Round the size of a block to be pushed up to the boundary required
943 by this machine. SIZE is the desired size, which need not be constant. */
945 static rtx
946 round_push (rtx size)
948 rtx align_rtx, alignm1_rtx;
950 if (!SUPPORTS_STACK_ALIGNMENT
951 || crtl->preferred_stack_boundary == MAX_SUPPORTED_STACK_ALIGNMENT)
953 int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
955 if (align == 1)
956 return size;
958 if (CONST_INT_P (size))
960 HOST_WIDE_INT new_size = (INTVAL (size) + align - 1) / align * align;
962 if (INTVAL (size) != new_size)
963 size = GEN_INT (new_size);
964 return size;
967 align_rtx = GEN_INT (align);
968 alignm1_rtx = GEN_INT (align - 1);
970 else
972 /* If crtl->preferred_stack_boundary might still grow, use
973 virtual_preferred_stack_boundary_rtx instead. This will be
974 substituted by the right value in vregs pass and optimized
975 during combine. */
976 align_rtx = virtual_preferred_stack_boundary_rtx;
977 alignm1_rtx = force_operand (plus_constant (Pmode, align_rtx, -1),
978 NULL_RTX);
981 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
982 but we know it can't. So add ourselves and then do
983 TRUNC_DIV_EXPR. */
984 size = expand_binop (Pmode, add_optab, size, alignm1_rtx,
985 NULL_RTX, 1, OPTAB_LIB_WIDEN);
986 size = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, size, align_rtx,
987 NULL_RTX, 1);
988 size = expand_mult (Pmode, size, align_rtx, NULL_RTX, 1);
990 return size;
993 /* Save the stack pointer for the purpose in SAVE_LEVEL. PSAVE is a pointer
994 to a previously-created save area. If no save area has been allocated,
995 this function will allocate one. If a save area is specified, it
996 must be of the proper mode. */
998 void
999 emit_stack_save (enum save_level save_level, rtx *psave)
1001 rtx sa = *psave;
1002 /* The default is that we use a move insn and save in a Pmode object. */
1003 rtx (*fcn) (rtx, rtx) = gen_move_insn;
1004 enum machine_mode mode = STACK_SAVEAREA_MODE (save_level);
1006 /* See if this machine has anything special to do for this kind of save. */
1007 switch (save_level)
1009 #ifdef HAVE_save_stack_block
1010 case SAVE_BLOCK:
1011 if (HAVE_save_stack_block)
1012 fcn = gen_save_stack_block;
1013 break;
1014 #endif
1015 #ifdef HAVE_save_stack_function
1016 case SAVE_FUNCTION:
1017 if (HAVE_save_stack_function)
1018 fcn = gen_save_stack_function;
1019 break;
1020 #endif
1021 #ifdef HAVE_save_stack_nonlocal
1022 case SAVE_NONLOCAL:
1023 if (HAVE_save_stack_nonlocal)
1024 fcn = gen_save_stack_nonlocal;
1025 break;
1026 #endif
1027 default:
1028 break;
1031 /* If there is no save area and we have to allocate one, do so. Otherwise
1032 verify the save area is the proper mode. */
1034 if (sa == 0)
1036 if (mode != VOIDmode)
1038 if (save_level == SAVE_NONLOCAL)
1039 *psave = sa = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
1040 else
1041 *psave = sa = gen_reg_rtx (mode);
1045 do_pending_stack_adjust ();
1046 if (sa != 0)
1047 sa = validize_mem (sa);
1048 emit_insn (fcn (sa, stack_pointer_rtx));
1051 /* Restore the stack pointer for the purpose in SAVE_LEVEL. SA is the save
1052 area made by emit_stack_save. If it is zero, we have nothing to do. */
1054 void
1055 emit_stack_restore (enum save_level save_level, rtx sa)
1057 /* The default is that we use a move insn. */
1058 rtx (*fcn) (rtx, rtx) = gen_move_insn;
1060 /* If stack_realign_drap, the x86 backend emits a prologue that aligns both
1061 STACK_POINTER and HARD_FRAME_POINTER.
1062 If stack_realign_fp, the x86 backend emits a prologue that aligns only
1063 STACK_POINTER. This renders the HARD_FRAME_POINTER unusable for accessing
1064 aligned variables, which is reflected in ix86_can_eliminate.
1065 We normally still have the realigned STACK_POINTER that we can use.
1066 But if there is a stack restore still present at reload, it can trigger
1067 mark_not_eliminable for the STACK_POINTER, leaving no way to eliminate
1068 FRAME_POINTER into a hard reg.
1069 To prevent this situation, we force need_drap if we emit a stack
1070 restore. */
1071 if (SUPPORTS_STACK_ALIGNMENT)
1072 crtl->need_drap = true;
1074 /* See if this machine has anything special to do for this kind of save. */
1075 switch (save_level)
1077 #ifdef HAVE_restore_stack_block
1078 case SAVE_BLOCK:
1079 if (HAVE_restore_stack_block)
1080 fcn = gen_restore_stack_block;
1081 break;
1082 #endif
1083 #ifdef HAVE_restore_stack_function
1084 case SAVE_FUNCTION:
1085 if (HAVE_restore_stack_function)
1086 fcn = gen_restore_stack_function;
1087 break;
1088 #endif
1089 #ifdef HAVE_restore_stack_nonlocal
1090 case SAVE_NONLOCAL:
1091 if (HAVE_restore_stack_nonlocal)
1092 fcn = gen_restore_stack_nonlocal;
1093 break;
1094 #endif
1095 default:
1096 break;
1099 if (sa != 0)
1101 sa = validize_mem (sa);
1102 /* These clobbers prevent the scheduler from moving
1103 references to variable arrays below the code
1104 that deletes (pops) the arrays. */
1105 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1106 emit_clobber (gen_rtx_MEM (BLKmode, stack_pointer_rtx));
1109 discard_pending_stack_adjust ();
1111 emit_insn (fcn (stack_pointer_rtx, sa));
1114 /* Invoke emit_stack_save on the nonlocal_goto_save_area for the current
1115 function. This function should be called whenever we allocate or
1116 deallocate dynamic stack space. */
1118 void
1119 update_nonlocal_goto_save_area (void)
1121 tree t_save;
1122 rtx r_save;
1124 /* The nonlocal_goto_save_area object is an array of N pointers. The
1125 first one is used for the frame pointer save; the rest are sized by
1126 STACK_SAVEAREA_MODE. Create a reference to array index 1, the first
1127 of the stack save area slots. */
1128 t_save = build4 (ARRAY_REF,
1129 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
1130 cfun->nonlocal_goto_save_area,
1131 integer_one_node, NULL_TREE, NULL_TREE);
1132 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
1134 emit_stack_save (SAVE_NONLOCAL, &r_save);
1137 /* Return an rtx representing the address of an area of memory dynamically
1138 pushed on the stack.
1140 Any required stack pointer alignment is preserved.
1142 SIZE is an rtx representing the size of the area.
1144 SIZE_ALIGN is the alignment (in bits) that we know SIZE has. This
1145 parameter may be zero. If so, a proper value will be extracted
1146 from SIZE if it is constant, otherwise BITS_PER_UNIT will be assumed.
1148 REQUIRED_ALIGN is the alignment (in bits) required for the region
1149 of memory.
1151 If CANNOT_ACCUMULATE is set to TRUE, the caller guarantees that the
1152 stack space allocated by the generated code cannot be added with itself
1153 in the course of the execution of the function. It is always safe to
1154 pass FALSE here and the following criterion is sufficient in order to
1155 pass TRUE: every path in the CFG that starts at the allocation point and
1156 loops to it executes the associated deallocation code. */
1159 allocate_dynamic_stack_space (rtx size, unsigned size_align,
1160 unsigned required_align, bool cannot_accumulate)
1162 HOST_WIDE_INT stack_usage_size = -1;
1163 rtx final_label, final_target, target;
1164 unsigned extra_align = 0;
1165 bool must_align;
1167 /* If we're asking for zero bytes, it doesn't matter what we point
1168 to since we can't dereference it. But return a reasonable
1169 address anyway. */
1170 if (size == const0_rtx)
1171 return virtual_stack_dynamic_rtx;
1173 /* Otherwise, show we're calling alloca or equivalent. */
1174 cfun->calls_alloca = 1;
1176 /* If stack usage info is requested, look into the size we are passed.
1177 We need to do so this early to avoid the obfuscation that may be
1178 introduced later by the various alignment operations. */
1179 if (flag_stack_usage_info)
1181 if (CONST_INT_P (size))
1182 stack_usage_size = INTVAL (size);
1183 else if (REG_P (size))
1185 /* Look into the last emitted insn and see if we can deduce
1186 something for the register. */
1187 rtx insn, set, note;
1188 insn = get_last_insn ();
1189 if ((set = single_set (insn)) && rtx_equal_p (SET_DEST (set), size))
1191 if (CONST_INT_P (SET_SRC (set)))
1192 stack_usage_size = INTVAL (SET_SRC (set));
1193 else if ((note = find_reg_equal_equiv_note (insn))
1194 && CONST_INT_P (XEXP (note, 0)))
1195 stack_usage_size = INTVAL (XEXP (note, 0));
1199 /* If the size is not constant, we can't say anything. */
1200 if (stack_usage_size == -1)
1202 current_function_has_unbounded_dynamic_stack_size = 1;
1203 stack_usage_size = 0;
1207 /* Ensure the size is in the proper mode. */
1208 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1209 size = convert_to_mode (Pmode, size, 1);
1211 /* Adjust SIZE_ALIGN, if needed. */
1212 if (CONST_INT_P (size))
1214 unsigned HOST_WIDE_INT lsb;
1216 lsb = INTVAL (size);
1217 lsb &= -lsb;
1219 /* Watch out for overflow truncating to "unsigned". */
1220 if (lsb > UINT_MAX / BITS_PER_UNIT)
1221 size_align = 1u << (HOST_BITS_PER_INT - 1);
1222 else
1223 size_align = (unsigned)lsb * BITS_PER_UNIT;
1225 else if (size_align < BITS_PER_UNIT)
1226 size_align = BITS_PER_UNIT;
1228 /* We can't attempt to minimize alignment necessary, because we don't
1229 know the final value of preferred_stack_boundary yet while executing
1230 this code. */
1231 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
1232 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
1234 /* We will need to ensure that the address we return is aligned to
1235 REQUIRED_ALIGN. If STACK_DYNAMIC_OFFSET is defined, we don't
1236 always know its final value at this point in the compilation (it
1237 might depend on the size of the outgoing parameter lists, for
1238 example), so we must align the value to be returned in that case.
1239 (Note that STACK_DYNAMIC_OFFSET will have a default nonzero value if
1240 STACK_POINTER_OFFSET or ACCUMULATE_OUTGOING_ARGS are defined).
1241 We must also do an alignment operation on the returned value if
1242 the stack pointer alignment is less strict than REQUIRED_ALIGN.
1244 If we have to align, we must leave space in SIZE for the hole
1245 that might result from the alignment operation. */
1247 must_align = (crtl->preferred_stack_boundary < required_align);
1248 if (must_align)
1250 if (required_align > PREFERRED_STACK_BOUNDARY)
1251 extra_align = PREFERRED_STACK_BOUNDARY;
1252 else if (required_align > STACK_BOUNDARY)
1253 extra_align = STACK_BOUNDARY;
1254 else
1255 extra_align = BITS_PER_UNIT;
1258 /* ??? STACK_POINTER_OFFSET is always defined now. */
1259 #if defined (STACK_DYNAMIC_OFFSET) || defined (STACK_POINTER_OFFSET)
1260 must_align = true;
1261 extra_align = BITS_PER_UNIT;
1262 #endif
1264 if (must_align)
1266 unsigned extra = (required_align - extra_align) / BITS_PER_UNIT;
1268 size = plus_constant (Pmode, size, extra);
1269 size = force_operand (size, NULL_RTX);
1271 if (flag_stack_usage_info)
1272 stack_usage_size += extra;
1274 if (extra && size_align > extra_align)
1275 size_align = extra_align;
1278 /* Round the size to a multiple of the required stack alignment.
1279 Since the stack if presumed to be rounded before this allocation,
1280 this will maintain the required alignment.
1282 If the stack grows downward, we could save an insn by subtracting
1283 SIZE from the stack pointer and then aligning the stack pointer.
1284 The problem with this is that the stack pointer may be unaligned
1285 between the execution of the subtraction and alignment insns and
1286 some machines do not allow this. Even on those that do, some
1287 signal handlers malfunction if a signal should occur between those
1288 insns. Since this is an extremely rare event, we have no reliable
1289 way of knowing which systems have this problem. So we avoid even
1290 momentarily mis-aligning the stack. */
1291 if (size_align % MAX_SUPPORTED_STACK_ALIGNMENT != 0)
1293 size = round_push (size);
1295 if (flag_stack_usage_info)
1297 int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
1298 stack_usage_size = (stack_usage_size + align - 1) / align * align;
1302 target = gen_reg_rtx (Pmode);
1304 /* The size is supposed to be fully adjusted at this point so record it
1305 if stack usage info is requested. */
1306 if (flag_stack_usage_info)
1308 current_function_dynamic_stack_size += stack_usage_size;
1310 /* ??? This is gross but the only safe stance in the absence
1311 of stack usage oriented flow analysis. */
1312 if (!cannot_accumulate)
1313 current_function_has_unbounded_dynamic_stack_size = 1;
1316 final_label = NULL_RTX;
1317 final_target = NULL_RTX;
1319 /* If we are splitting the stack, we need to ask the backend whether
1320 there is enough room on the current stack. If there isn't, or if
1321 the backend doesn't know how to tell is, then we need to call a
1322 function to allocate memory in some other way. This memory will
1323 be released when we release the current stack segment. The
1324 effect is that stack allocation becomes less efficient, but at
1325 least it doesn't cause a stack overflow. */
1326 if (flag_split_stack)
1328 rtx available_label, ask, space, func;
1330 available_label = NULL_RTX;
1332 #ifdef HAVE_split_stack_space_check
1333 if (HAVE_split_stack_space_check)
1335 available_label = gen_label_rtx ();
1337 /* This instruction will branch to AVAILABLE_LABEL if there
1338 are SIZE bytes available on the stack. */
1339 emit_insn (gen_split_stack_space_check (size, available_label));
1341 #endif
1343 /* The __morestack_allocate_stack_space function will allocate
1344 memory using malloc. If the alignment of the memory returned
1345 by malloc does not meet REQUIRED_ALIGN, we increase SIZE to
1346 make sure we allocate enough space. */
1347 if (MALLOC_ABI_ALIGNMENT >= required_align)
1348 ask = size;
1349 else
1351 ask = expand_binop (Pmode, add_optab, size,
1352 gen_int_mode (required_align / BITS_PER_UNIT - 1,
1353 Pmode),
1354 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1355 must_align = true;
1358 func = init_one_libfunc ("__morestack_allocate_stack_space");
1360 space = emit_library_call_value (func, target, LCT_NORMAL, Pmode,
1361 1, ask, Pmode);
1363 if (available_label == NULL_RTX)
1364 return space;
1366 final_target = gen_reg_rtx (Pmode);
1368 emit_move_insn (final_target, space);
1370 final_label = gen_label_rtx ();
1371 emit_jump (final_label);
1373 emit_label (available_label);
1376 do_pending_stack_adjust ();
1378 /* We ought to be called always on the toplevel and stack ought to be aligned
1379 properly. */
1380 gcc_assert (!(stack_pointer_delta
1381 % (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)));
1383 /* If needed, check that we have the required amount of stack. Take into
1384 account what has already been checked. */
1385 if (STACK_CHECK_MOVING_SP)
1387 else if (flag_stack_check == GENERIC_STACK_CHECK)
1388 probe_stack_range (STACK_OLD_CHECK_PROTECT + STACK_CHECK_MAX_FRAME_SIZE,
1389 size);
1390 else if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
1391 probe_stack_range (STACK_CHECK_PROTECT, size);
1393 /* Don't let anti_adjust_stack emit notes. */
1394 suppress_reg_args_size = true;
1396 /* Perform the required allocation from the stack. Some systems do
1397 this differently than simply incrementing/decrementing from the
1398 stack pointer, such as acquiring the space by calling malloc(). */
1399 #ifdef HAVE_allocate_stack
1400 if (HAVE_allocate_stack)
1402 struct expand_operand ops[2];
1403 /* We don't have to check against the predicate for operand 0 since
1404 TARGET is known to be a pseudo of the proper mode, which must
1405 be valid for the operand. */
1406 create_fixed_operand (&ops[0], target);
1407 create_convert_operand_to (&ops[1], size, STACK_SIZE_MODE, true);
1408 expand_insn (CODE_FOR_allocate_stack, 2, ops);
1410 else
1411 #endif
1413 int saved_stack_pointer_delta;
1415 #ifndef STACK_GROWS_DOWNWARD
1416 emit_move_insn (target, virtual_stack_dynamic_rtx);
1417 #endif
1419 /* Check stack bounds if necessary. */
1420 if (crtl->limit_stack)
1422 rtx available;
1423 rtx space_available = gen_label_rtx ();
1424 #ifdef STACK_GROWS_DOWNWARD
1425 available = expand_binop (Pmode, sub_optab,
1426 stack_pointer_rtx, stack_limit_rtx,
1427 NULL_RTX, 1, OPTAB_WIDEN);
1428 #else
1429 available = expand_binop (Pmode, sub_optab,
1430 stack_limit_rtx, stack_pointer_rtx,
1431 NULL_RTX, 1, OPTAB_WIDEN);
1432 #endif
1433 emit_cmp_and_jump_insns (available, size, GEU, NULL_RTX, Pmode, 1,
1434 space_available);
1435 #ifdef HAVE_trap
1436 if (HAVE_trap)
1437 emit_insn (gen_trap ());
1438 else
1439 #endif
1440 error ("stack limits not supported on this target");
1441 emit_barrier ();
1442 emit_label (space_available);
1445 saved_stack_pointer_delta = stack_pointer_delta;
1447 if (flag_stack_check && STACK_CHECK_MOVING_SP)
1448 anti_adjust_stack_and_probe (size, false);
1449 else
1450 anti_adjust_stack (size);
1452 /* Even if size is constant, don't modify stack_pointer_delta.
1453 The constant size alloca should preserve
1454 crtl->preferred_stack_boundary alignment. */
1455 stack_pointer_delta = saved_stack_pointer_delta;
1457 #ifdef STACK_GROWS_DOWNWARD
1458 emit_move_insn (target, virtual_stack_dynamic_rtx);
1459 #endif
1462 suppress_reg_args_size = false;
1464 /* Finish up the split stack handling. */
1465 if (final_label != NULL_RTX)
1467 gcc_assert (flag_split_stack);
1468 emit_move_insn (final_target, target);
1469 emit_label (final_label);
1470 target = final_target;
1473 if (must_align)
1475 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1476 but we know it can't. So add ourselves and then do
1477 TRUNC_DIV_EXPR. */
1478 target = expand_binop (Pmode, add_optab, target,
1479 gen_int_mode (required_align / BITS_PER_UNIT - 1,
1480 Pmode),
1481 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1482 target = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, target,
1483 gen_int_mode (required_align / BITS_PER_UNIT,
1484 Pmode),
1485 NULL_RTX, 1);
1486 target = expand_mult (Pmode, target,
1487 gen_int_mode (required_align / BITS_PER_UNIT,
1488 Pmode),
1489 NULL_RTX, 1);
1492 /* Now that we've committed to a return value, mark its alignment. */
1493 mark_reg_pointer (target, required_align);
1495 /* Record the new stack level for nonlocal gotos. */
1496 if (cfun->nonlocal_goto_save_area != 0)
1497 update_nonlocal_goto_save_area ();
1499 return target;
1502 /* A front end may want to override GCC's stack checking by providing a
1503 run-time routine to call to check the stack, so provide a mechanism for
1504 calling that routine. */
1506 static GTY(()) rtx stack_check_libfunc;
1508 void
1509 set_stack_check_libfunc (const char *libfunc_name)
1511 gcc_assert (stack_check_libfunc == NULL_RTX);
1512 stack_check_libfunc = gen_rtx_SYMBOL_REF (Pmode, libfunc_name);
1515 /* Emit one stack probe at ADDRESS, an address within the stack. */
1517 void
1518 emit_stack_probe (rtx address)
1520 #ifdef HAVE_probe_stack_address
1521 if (HAVE_probe_stack_address)
1522 emit_insn (gen_probe_stack_address (address));
1523 else
1524 #endif
1526 rtx memref = gen_rtx_MEM (word_mode, address);
1528 MEM_VOLATILE_P (memref) = 1;
1530 /* See if we have an insn to probe the stack. */
1531 #ifdef HAVE_probe_stack
1532 if (HAVE_probe_stack)
1533 emit_insn (gen_probe_stack (memref));
1534 else
1535 #endif
1536 emit_move_insn (memref, const0_rtx);
1540 /* Probe a range of stack addresses from FIRST to FIRST+SIZE, inclusive.
1541 FIRST is a constant and size is a Pmode RTX. These are offsets from
1542 the current stack pointer. STACK_GROWS_DOWNWARD says whether to add
1543 or subtract them from the stack pointer. */
1545 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
1547 #ifdef STACK_GROWS_DOWNWARD
1548 #define STACK_GROW_OP MINUS
1549 #define STACK_GROW_OPTAB sub_optab
1550 #define STACK_GROW_OFF(off) -(off)
1551 #else
1552 #define STACK_GROW_OP PLUS
1553 #define STACK_GROW_OPTAB add_optab
1554 #define STACK_GROW_OFF(off) (off)
1555 #endif
1557 void
1558 probe_stack_range (HOST_WIDE_INT first, rtx size)
1560 /* First ensure SIZE is Pmode. */
1561 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1562 size = convert_to_mode (Pmode, size, 1);
1564 /* Next see if we have a function to check the stack. */
1565 if (stack_check_libfunc)
1567 rtx addr = memory_address (Pmode,
1568 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1569 stack_pointer_rtx,
1570 plus_constant (Pmode,
1571 size, first)));
1572 emit_library_call (stack_check_libfunc, LCT_NORMAL, VOIDmode, 1, addr,
1573 Pmode);
1576 /* Next see if we have an insn to check the stack. */
1577 #ifdef HAVE_check_stack
1578 else if (HAVE_check_stack)
1580 struct expand_operand ops[1];
1581 rtx addr = memory_address (Pmode,
1582 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1583 stack_pointer_rtx,
1584 plus_constant (Pmode,
1585 size, first)));
1586 bool success;
1587 create_input_operand (&ops[0], addr, Pmode);
1588 success = maybe_expand_insn (CODE_FOR_check_stack, 1, ops);
1589 gcc_assert (success);
1591 #endif
1593 /* Otherwise we have to generate explicit probes. If we have a constant
1594 small number of them to generate, that's the easy case. */
1595 else if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
1597 HOST_WIDE_INT isize = INTVAL (size), i;
1598 rtx addr;
1600 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
1601 it exceeds SIZE. If only one probe is needed, this will not
1602 generate any code. Then probe at FIRST + SIZE. */
1603 for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
1605 addr = memory_address (Pmode,
1606 plus_constant (Pmode, stack_pointer_rtx,
1607 STACK_GROW_OFF (first + i)));
1608 emit_stack_probe (addr);
1611 addr = memory_address (Pmode,
1612 plus_constant (Pmode, stack_pointer_rtx,
1613 STACK_GROW_OFF (first + isize)));
1614 emit_stack_probe (addr);
1617 /* In the variable case, do the same as above, but in a loop. Note that we
1618 must be extra careful with variables wrapping around because we might be
1619 at the very top (or the very bottom) of the address space and we have to
1620 be able to handle this case properly; in particular, we use an equality
1621 test for the loop condition. */
1622 else
1624 rtx rounded_size, rounded_size_op, test_addr, last_addr, temp;
1625 rtx loop_lab = gen_label_rtx ();
1626 rtx end_lab = gen_label_rtx ();
1629 /* Step 1: round SIZE to the previous multiple of the interval. */
1631 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1632 rounded_size
1633 = simplify_gen_binary (AND, Pmode, size,
1634 gen_int_mode (-PROBE_INTERVAL, Pmode));
1635 rounded_size_op = force_operand (rounded_size, NULL_RTX);
1638 /* Step 2: compute initial and final value of the loop counter. */
1640 /* TEST_ADDR = SP + FIRST. */
1641 test_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1642 stack_pointer_rtx,
1643 gen_int_mode (first, Pmode)),
1644 NULL_RTX);
1646 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
1647 last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1648 test_addr,
1649 rounded_size_op), NULL_RTX);
1652 /* Step 3: the loop
1654 while (TEST_ADDR != LAST_ADDR)
1656 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
1657 probe at TEST_ADDR
1660 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
1661 until it is equal to ROUNDED_SIZE. */
1663 emit_label (loop_lab);
1665 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
1666 emit_cmp_and_jump_insns (test_addr, last_addr, EQ, NULL_RTX, Pmode, 1,
1667 end_lab);
1669 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
1670 temp = expand_binop (Pmode, STACK_GROW_OPTAB, test_addr,
1671 gen_int_mode (PROBE_INTERVAL, Pmode), test_addr,
1672 1, OPTAB_WIDEN);
1674 gcc_assert (temp == test_addr);
1676 /* Probe at TEST_ADDR. */
1677 emit_stack_probe (test_addr);
1679 emit_jump (loop_lab);
1681 emit_label (end_lab);
1684 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
1685 that SIZE is equal to ROUNDED_SIZE. */
1687 /* TEMP = SIZE - ROUNDED_SIZE. */
1688 temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
1689 if (temp != const0_rtx)
1691 rtx addr;
1693 if (CONST_INT_P (temp))
1695 /* Use [base + disp} addressing mode if supported. */
1696 HOST_WIDE_INT offset = INTVAL (temp);
1697 addr = memory_address (Pmode,
1698 plus_constant (Pmode, last_addr,
1699 STACK_GROW_OFF (offset)));
1701 else
1703 /* Manual CSE if the difference is not known at compile-time. */
1704 temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
1705 addr = memory_address (Pmode,
1706 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1707 last_addr, temp));
1710 emit_stack_probe (addr);
1714 /* Make sure nothing is scheduled before we are done. */
1715 emit_insn (gen_blockage ());
1718 /* Adjust the stack pointer by minus SIZE (an rtx for a number of bytes)
1719 while probing it. This pushes when SIZE is positive. SIZE need not
1720 be constant. If ADJUST_BACK is true, adjust back the stack pointer
1721 by plus SIZE at the end. */
1723 void
1724 anti_adjust_stack_and_probe (rtx size, bool adjust_back)
1726 /* We skip the probe for the first interval + a small dope of 4 words and
1727 probe that many bytes past the specified size to maintain a protection
1728 area at the botton of the stack. */
1729 const int dope = 4 * UNITS_PER_WORD;
1731 /* First ensure SIZE is Pmode. */
1732 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1733 size = convert_to_mode (Pmode, size, 1);
1735 /* If we have a constant small number of probes to generate, that's the
1736 easy case. */
1737 if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
1739 HOST_WIDE_INT isize = INTVAL (size), i;
1740 bool first_probe = true;
1742 /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
1743 values of N from 1 until it exceeds SIZE. If only one probe is
1744 needed, this will not generate any code. Then adjust and probe
1745 to PROBE_INTERVAL + SIZE. */
1746 for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
1748 if (first_probe)
1750 anti_adjust_stack (GEN_INT (2 * PROBE_INTERVAL + dope));
1751 first_probe = false;
1753 else
1754 anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
1755 emit_stack_probe (stack_pointer_rtx);
1758 if (first_probe)
1759 anti_adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL + dope));
1760 else
1761 anti_adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL - i));
1762 emit_stack_probe (stack_pointer_rtx);
1765 /* In the variable case, do the same as above, but in a loop. Note that we
1766 must be extra careful with variables wrapping around because we might be
1767 at the very top (or the very bottom) of the address space and we have to
1768 be able to handle this case properly; in particular, we use an equality
1769 test for the loop condition. */
1770 else
1772 rtx rounded_size, rounded_size_op, last_addr, temp;
1773 rtx loop_lab = gen_label_rtx ();
1774 rtx end_lab = gen_label_rtx ();
1777 /* Step 1: round SIZE to the previous multiple of the interval. */
1779 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1780 rounded_size
1781 = simplify_gen_binary (AND, Pmode, size,
1782 gen_int_mode (-PROBE_INTERVAL, Pmode));
1783 rounded_size_op = force_operand (rounded_size, NULL_RTX);
1786 /* Step 2: compute initial and final value of the loop counter. */
1788 /* SP = SP_0 + PROBE_INTERVAL. */
1789 anti_adjust_stack (GEN_INT (PROBE_INTERVAL + dope));
1791 /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE. */
1792 last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1793 stack_pointer_rtx,
1794 rounded_size_op), NULL_RTX);
1797 /* Step 3: the loop
1799 while (SP != LAST_ADDR)
1801 SP = SP + PROBE_INTERVAL
1802 probe at SP
1805 adjusts SP and probes at PROBE_INTERVAL + N * PROBE_INTERVAL for
1806 values of N from 1 until it is equal to ROUNDED_SIZE. */
1808 emit_label (loop_lab);
1810 /* Jump to END_LAB if SP == LAST_ADDR. */
1811 emit_cmp_and_jump_insns (stack_pointer_rtx, last_addr, EQ, NULL_RTX,
1812 Pmode, 1, end_lab);
1814 /* SP = SP + PROBE_INTERVAL and probe at SP. */
1815 anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
1816 emit_stack_probe (stack_pointer_rtx);
1818 emit_jump (loop_lab);
1820 emit_label (end_lab);
1823 /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
1824 assert at compile-time that SIZE is equal to ROUNDED_SIZE. */
1826 /* TEMP = SIZE - ROUNDED_SIZE. */
1827 temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
1828 if (temp != const0_rtx)
1830 /* Manual CSE if the difference is not known at compile-time. */
1831 if (GET_CODE (temp) != CONST_INT)
1832 temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
1833 anti_adjust_stack (temp);
1834 emit_stack_probe (stack_pointer_rtx);
1838 /* Adjust back and account for the additional first interval. */
1839 if (adjust_back)
1840 adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL + dope));
1841 else
1842 adjust_stack (GEN_INT (PROBE_INTERVAL + dope));
1845 /* Return an rtx representing the register or memory location
1846 in which a scalar value of data type VALTYPE
1847 was returned by a function call to function FUNC.
1848 FUNC is a FUNCTION_DECL, FNTYPE a FUNCTION_TYPE node if the precise
1849 function is known, otherwise 0.
1850 OUTGOING is 1 if on a machine with register windows this function
1851 should return the register in which the function will put its result
1852 and 0 otherwise. */
1855 hard_function_value (const_tree valtype, const_tree func, const_tree fntype,
1856 int outgoing ATTRIBUTE_UNUSED)
1858 rtx val;
1860 val = targetm.calls.function_value (valtype, func ? func : fntype, outgoing);
1862 if (REG_P (val)
1863 && GET_MODE (val) == BLKmode)
1865 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (valtype);
1866 enum machine_mode tmpmode;
1868 /* int_size_in_bytes can return -1. We don't need a check here
1869 since the value of bytes will then be large enough that no
1870 mode will match anyway. */
1872 for (tmpmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1873 tmpmode != VOIDmode;
1874 tmpmode = GET_MODE_WIDER_MODE (tmpmode))
1876 /* Have we found a large enough mode? */
1877 if (GET_MODE_SIZE (tmpmode) >= bytes)
1878 break;
1881 /* No suitable mode found. */
1882 gcc_assert (tmpmode != VOIDmode);
1884 PUT_MODE (val, tmpmode);
1886 return val;
1889 /* Return an rtx representing the register or memory location
1890 in which a scalar value of mode MODE was returned by a library call. */
1893 hard_libcall_value (enum machine_mode mode, rtx fun)
1895 return targetm.calls.libcall_value (mode, fun);
1898 /* Look up the tree code for a given rtx code
1899 to provide the arithmetic operation for REAL_ARITHMETIC.
1900 The function returns an int because the caller may not know
1901 what `enum tree_code' means. */
1904 rtx_to_tree_code (enum rtx_code code)
1906 enum tree_code tcode;
1908 switch (code)
1910 case PLUS:
1911 tcode = PLUS_EXPR;
1912 break;
1913 case MINUS:
1914 tcode = MINUS_EXPR;
1915 break;
1916 case MULT:
1917 tcode = MULT_EXPR;
1918 break;
1919 case DIV:
1920 tcode = RDIV_EXPR;
1921 break;
1922 case SMIN:
1923 tcode = MIN_EXPR;
1924 break;
1925 case SMAX:
1926 tcode = MAX_EXPR;
1927 break;
1928 default:
1929 tcode = LAST_AND_UNUSED_TREE_CODE;
1930 break;
1932 return ((int) tcode);
1935 #include "gt-explow.h"