Automated renaming of gimple subclasses
[official-gcc.git] / gcc / explow.c
blob8470691d779604cb8bbb0a056348d4ff9b3287de
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, LABEL_REF_LABEL (x));
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, set;
653 rtx_insn *insn;
655 if (REG_P (x))
656 return x;
658 if (general_operand (x, mode))
660 temp = gen_reg_rtx (mode);
661 insn = emit_move_insn (temp, x);
663 else
665 temp = force_operand (x, NULL_RTX);
666 if (REG_P (temp))
667 insn = get_last_insn ();
668 else
670 rtx temp2 = gen_reg_rtx (mode);
671 insn = emit_move_insn (temp2, temp);
672 temp = temp2;
676 /* Let optimizers know that TEMP's value never changes
677 and that X can be substituted for it. Don't get confused
678 if INSN set something else (such as a SUBREG of TEMP). */
679 if (CONSTANT_P (x)
680 && (set = single_set (insn)) != 0
681 && SET_DEST (set) == temp
682 && ! rtx_equal_p (x, SET_SRC (set)))
683 set_unique_reg_note (insn, REG_EQUAL, x);
685 /* Let optimizers know that TEMP is a pointer, and if so, the
686 known alignment of that pointer. */
688 unsigned align = 0;
689 if (GET_CODE (x) == SYMBOL_REF)
691 align = BITS_PER_UNIT;
692 if (SYMBOL_REF_DECL (x) && DECL_P (SYMBOL_REF_DECL (x)))
693 align = DECL_ALIGN (SYMBOL_REF_DECL (x));
695 else if (GET_CODE (x) == LABEL_REF)
696 align = BITS_PER_UNIT;
697 else if (GET_CODE (x) == CONST
698 && GET_CODE (XEXP (x, 0)) == PLUS
699 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
700 && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
702 rtx s = XEXP (XEXP (x, 0), 0);
703 rtx c = XEXP (XEXP (x, 0), 1);
704 unsigned sa, ca;
706 sa = BITS_PER_UNIT;
707 if (SYMBOL_REF_DECL (s) && DECL_P (SYMBOL_REF_DECL (s)))
708 sa = DECL_ALIGN (SYMBOL_REF_DECL (s));
710 if (INTVAL (c) == 0)
711 align = sa;
712 else
714 ca = ctz_hwi (INTVAL (c)) * BITS_PER_UNIT;
715 align = MIN (sa, ca);
719 if (align || (MEM_P (x) && MEM_POINTER (x)))
720 mark_reg_pointer (temp, align);
723 return temp;
726 /* If X is a memory ref, copy its contents to a new temp reg and return
727 that reg. Otherwise, return X. */
730 force_not_mem (rtx x)
732 rtx temp;
734 if (!MEM_P (x) || GET_MODE (x) == BLKmode)
735 return x;
737 temp = gen_reg_rtx (GET_MODE (x));
739 if (MEM_POINTER (x))
740 REG_POINTER (temp) = 1;
742 emit_move_insn (temp, x);
743 return temp;
746 /* Copy X to TARGET (if it's nonzero and a reg)
747 or to a new temp reg and return that reg.
748 MODE is the mode to use for X in case it is a constant. */
751 copy_to_suggested_reg (rtx x, rtx target, enum machine_mode mode)
753 rtx temp;
755 if (target && REG_P (target))
756 temp = target;
757 else
758 temp = gen_reg_rtx (mode);
760 emit_move_insn (temp, x);
761 return temp;
764 /* Return the mode to use to pass or return a scalar of TYPE and MODE.
765 PUNSIGNEDP points to the signedness of the type and may be adjusted
766 to show what signedness to use on extension operations.
768 FOR_RETURN is nonzero if the caller is promoting the return value
769 of FNDECL, else it is for promoting args. */
771 enum machine_mode
772 promote_function_mode (const_tree type, enum machine_mode mode, int *punsignedp,
773 const_tree funtype, int for_return)
775 /* Called without a type node for a libcall. */
776 if (type == NULL_TREE)
778 if (INTEGRAL_MODE_P (mode))
779 return targetm.calls.promote_function_mode (NULL_TREE, mode,
780 punsignedp, funtype,
781 for_return);
782 else
783 return mode;
786 switch (TREE_CODE (type))
788 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
789 case REAL_TYPE: case OFFSET_TYPE: case FIXED_POINT_TYPE:
790 case POINTER_TYPE: case REFERENCE_TYPE:
791 return targetm.calls.promote_function_mode (type, mode, punsignedp, funtype,
792 for_return);
794 default:
795 return mode;
798 /* Return the mode to use to store a scalar of TYPE and MODE.
799 PUNSIGNEDP points to the signedness of the type and may be adjusted
800 to show what signedness to use on extension operations. */
802 enum machine_mode
803 promote_mode (const_tree type ATTRIBUTE_UNUSED, enum machine_mode mode,
804 int *punsignedp ATTRIBUTE_UNUSED)
806 #ifdef PROMOTE_MODE
807 enum tree_code code;
808 int unsignedp;
809 #endif
811 /* For libcalls this is invoked without TYPE from the backends
812 TARGET_PROMOTE_FUNCTION_MODE hooks. Don't do anything in that
813 case. */
814 if (type == NULL_TREE)
815 return mode;
817 /* FIXME: this is the same logic that was there until GCC 4.4, but we
818 probably want to test POINTERS_EXTEND_UNSIGNED even if PROMOTE_MODE
819 is not defined. The affected targets are M32C, S390, SPARC. */
820 #ifdef PROMOTE_MODE
821 code = TREE_CODE (type);
822 unsignedp = *punsignedp;
824 switch (code)
826 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
827 case REAL_TYPE: case OFFSET_TYPE: case FIXED_POINT_TYPE:
828 PROMOTE_MODE (mode, unsignedp, type);
829 *punsignedp = unsignedp;
830 return mode;
831 break;
833 #ifdef POINTERS_EXTEND_UNSIGNED
834 case REFERENCE_TYPE:
835 case POINTER_TYPE:
836 *punsignedp = POINTERS_EXTEND_UNSIGNED;
837 return targetm.addr_space.address_mode
838 (TYPE_ADDR_SPACE (TREE_TYPE (type)));
839 break;
840 #endif
842 default:
843 return mode;
845 #else
846 return mode;
847 #endif
851 /* Use one of promote_mode or promote_function_mode to find the promoted
852 mode of DECL. If PUNSIGNEDP is not NULL, store there the unsignedness
853 of DECL after promotion. */
855 enum machine_mode
856 promote_decl_mode (const_tree decl, int *punsignedp)
858 tree type = TREE_TYPE (decl);
859 int unsignedp = TYPE_UNSIGNED (type);
860 enum machine_mode mode = DECL_MODE (decl);
861 enum machine_mode pmode;
863 if (TREE_CODE (decl) == RESULT_DECL
864 || TREE_CODE (decl) == PARM_DECL)
865 pmode = promote_function_mode (type, mode, &unsignedp,
866 TREE_TYPE (current_function_decl), 2);
867 else
868 pmode = promote_mode (type, mode, &unsignedp);
870 if (punsignedp)
871 *punsignedp = unsignedp;
872 return pmode;
876 /* Controls the behaviour of {anti_,}adjust_stack. */
877 static bool suppress_reg_args_size;
879 /* A helper for adjust_stack and anti_adjust_stack. */
881 static void
882 adjust_stack_1 (rtx adjust, bool anti_p)
884 rtx temp;
885 rtx_insn *insn;
887 #ifndef STACK_GROWS_DOWNWARD
888 /* Hereafter anti_p means subtract_p. */
889 anti_p = !anti_p;
890 #endif
892 temp = expand_binop (Pmode,
893 anti_p ? sub_optab : add_optab,
894 stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
895 OPTAB_LIB_WIDEN);
897 if (temp != stack_pointer_rtx)
898 insn = emit_move_insn (stack_pointer_rtx, temp);
899 else
901 insn = get_last_insn ();
902 temp = single_set (insn);
903 gcc_assert (temp != NULL && SET_DEST (temp) == stack_pointer_rtx);
906 if (!suppress_reg_args_size)
907 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
910 /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
911 This pops when ADJUST is positive. ADJUST need not be constant. */
913 void
914 adjust_stack (rtx adjust)
916 if (adjust == const0_rtx)
917 return;
919 /* We expect all variable sized adjustments to be multiple of
920 PREFERRED_STACK_BOUNDARY. */
921 if (CONST_INT_P (adjust))
922 stack_pointer_delta -= INTVAL (adjust);
924 adjust_stack_1 (adjust, false);
927 /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
928 This pushes when ADJUST is positive. ADJUST need not be constant. */
930 void
931 anti_adjust_stack (rtx adjust)
933 if (adjust == const0_rtx)
934 return;
936 /* We expect all variable sized adjustments to be multiple of
937 PREFERRED_STACK_BOUNDARY. */
938 if (CONST_INT_P (adjust))
939 stack_pointer_delta += INTVAL (adjust);
941 adjust_stack_1 (adjust, true);
944 /* Round the size of a block to be pushed up to the boundary required
945 by this machine. SIZE is the desired size, which need not be constant. */
947 static rtx
948 round_push (rtx size)
950 rtx align_rtx, alignm1_rtx;
952 if (!SUPPORTS_STACK_ALIGNMENT
953 || crtl->preferred_stack_boundary == MAX_SUPPORTED_STACK_ALIGNMENT)
955 int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
957 if (align == 1)
958 return size;
960 if (CONST_INT_P (size))
962 HOST_WIDE_INT new_size = (INTVAL (size) + align - 1) / align * align;
964 if (INTVAL (size) != new_size)
965 size = GEN_INT (new_size);
966 return size;
969 align_rtx = GEN_INT (align);
970 alignm1_rtx = GEN_INT (align - 1);
972 else
974 /* If crtl->preferred_stack_boundary might still grow, use
975 virtual_preferred_stack_boundary_rtx instead. This will be
976 substituted by the right value in vregs pass and optimized
977 during combine. */
978 align_rtx = virtual_preferred_stack_boundary_rtx;
979 alignm1_rtx = force_operand (plus_constant (Pmode, align_rtx, -1),
980 NULL_RTX);
983 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
984 but we know it can't. So add ourselves and then do
985 TRUNC_DIV_EXPR. */
986 size = expand_binop (Pmode, add_optab, size, alignm1_rtx,
987 NULL_RTX, 1, OPTAB_LIB_WIDEN);
988 size = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, size, align_rtx,
989 NULL_RTX, 1);
990 size = expand_mult (Pmode, size, align_rtx, NULL_RTX, 1);
992 return size;
995 /* Save the stack pointer for the purpose in SAVE_LEVEL. PSAVE is a pointer
996 to a previously-created save area. If no save area has been allocated,
997 this function will allocate one. If a save area is specified, it
998 must be of the proper mode. */
1000 void
1001 emit_stack_save (enum save_level save_level, rtx *psave)
1003 rtx sa = *psave;
1004 /* The default is that we use a move insn and save in a Pmode object. */
1005 rtx (*fcn) (rtx, rtx) = gen_move_insn;
1006 enum machine_mode mode = STACK_SAVEAREA_MODE (save_level);
1008 /* See if this machine has anything special to do for this kind of save. */
1009 switch (save_level)
1011 #ifdef HAVE_save_stack_block
1012 case SAVE_BLOCK:
1013 if (HAVE_save_stack_block)
1014 fcn = gen_save_stack_block;
1015 break;
1016 #endif
1017 #ifdef HAVE_save_stack_function
1018 case SAVE_FUNCTION:
1019 if (HAVE_save_stack_function)
1020 fcn = gen_save_stack_function;
1021 break;
1022 #endif
1023 #ifdef HAVE_save_stack_nonlocal
1024 case SAVE_NONLOCAL:
1025 if (HAVE_save_stack_nonlocal)
1026 fcn = gen_save_stack_nonlocal;
1027 break;
1028 #endif
1029 default:
1030 break;
1033 /* If there is no save area and we have to allocate one, do so. Otherwise
1034 verify the save area is the proper mode. */
1036 if (sa == 0)
1038 if (mode != VOIDmode)
1040 if (save_level == SAVE_NONLOCAL)
1041 *psave = sa = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
1042 else
1043 *psave = sa = gen_reg_rtx (mode);
1047 do_pending_stack_adjust ();
1048 if (sa != 0)
1049 sa = validize_mem (sa);
1050 emit_insn (fcn (sa, stack_pointer_rtx));
1053 /* Restore the stack pointer for the purpose in SAVE_LEVEL. SA is the save
1054 area made by emit_stack_save. If it is zero, we have nothing to do. */
1056 void
1057 emit_stack_restore (enum save_level save_level, rtx sa)
1059 /* The default is that we use a move insn. */
1060 rtx (*fcn) (rtx, rtx) = gen_move_insn;
1062 /* If stack_realign_drap, the x86 backend emits a prologue that aligns both
1063 STACK_POINTER and HARD_FRAME_POINTER.
1064 If stack_realign_fp, the x86 backend emits a prologue that aligns only
1065 STACK_POINTER. This renders the HARD_FRAME_POINTER unusable for accessing
1066 aligned variables, which is reflected in ix86_can_eliminate.
1067 We normally still have the realigned STACK_POINTER that we can use.
1068 But if there is a stack restore still present at reload, it can trigger
1069 mark_not_eliminable for the STACK_POINTER, leaving no way to eliminate
1070 FRAME_POINTER into a hard reg.
1071 To prevent this situation, we force need_drap if we emit a stack
1072 restore. */
1073 if (SUPPORTS_STACK_ALIGNMENT)
1074 crtl->need_drap = true;
1076 /* See if this machine has anything special to do for this kind of save. */
1077 switch (save_level)
1079 #ifdef HAVE_restore_stack_block
1080 case SAVE_BLOCK:
1081 if (HAVE_restore_stack_block)
1082 fcn = gen_restore_stack_block;
1083 break;
1084 #endif
1085 #ifdef HAVE_restore_stack_function
1086 case SAVE_FUNCTION:
1087 if (HAVE_restore_stack_function)
1088 fcn = gen_restore_stack_function;
1089 break;
1090 #endif
1091 #ifdef HAVE_restore_stack_nonlocal
1092 case SAVE_NONLOCAL:
1093 if (HAVE_restore_stack_nonlocal)
1094 fcn = gen_restore_stack_nonlocal;
1095 break;
1096 #endif
1097 default:
1098 break;
1101 if (sa != 0)
1103 sa = validize_mem (sa);
1104 /* These clobbers prevent the scheduler from moving
1105 references to variable arrays below the code
1106 that deletes (pops) the arrays. */
1107 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1108 emit_clobber (gen_rtx_MEM (BLKmode, stack_pointer_rtx));
1111 discard_pending_stack_adjust ();
1113 emit_insn (fcn (stack_pointer_rtx, sa));
1116 /* Invoke emit_stack_save on the nonlocal_goto_save_area for the current
1117 function. This function should be called whenever we allocate or
1118 deallocate dynamic stack space. */
1120 void
1121 update_nonlocal_goto_save_area (void)
1123 tree t_save;
1124 rtx r_save;
1126 /* The nonlocal_goto_save_area object is an array of N pointers. The
1127 first one is used for the frame pointer save; the rest are sized by
1128 STACK_SAVEAREA_MODE. Create a reference to array index 1, the first
1129 of the stack save area slots. */
1130 t_save = build4 (ARRAY_REF,
1131 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
1132 cfun->nonlocal_goto_save_area,
1133 integer_one_node, NULL_TREE, NULL_TREE);
1134 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
1136 emit_stack_save (SAVE_NONLOCAL, &r_save);
1139 /* Return an rtx representing the address of an area of memory dynamically
1140 pushed on the stack.
1142 Any required stack pointer alignment is preserved.
1144 SIZE is an rtx representing the size of the area.
1146 SIZE_ALIGN is the alignment (in bits) that we know SIZE has. This
1147 parameter may be zero. If so, a proper value will be extracted
1148 from SIZE if it is constant, otherwise BITS_PER_UNIT will be assumed.
1150 REQUIRED_ALIGN is the alignment (in bits) required for the region
1151 of memory.
1153 If CANNOT_ACCUMULATE is set to TRUE, the caller guarantees that the
1154 stack space allocated by the generated code cannot be added with itself
1155 in the course of the execution of the function. It is always safe to
1156 pass FALSE here and the following criterion is sufficient in order to
1157 pass TRUE: every path in the CFG that starts at the allocation point and
1158 loops to it executes the associated deallocation code. */
1161 allocate_dynamic_stack_space (rtx size, unsigned size_align,
1162 unsigned required_align, bool cannot_accumulate)
1164 HOST_WIDE_INT stack_usage_size = -1;
1165 rtx_code_label *final_label;
1166 rtx final_target, target;
1167 unsigned extra_align = 0;
1168 bool must_align;
1170 /* If we're asking for zero bytes, it doesn't matter what we point
1171 to since we can't dereference it. But return a reasonable
1172 address anyway. */
1173 if (size == const0_rtx)
1174 return virtual_stack_dynamic_rtx;
1176 /* Otherwise, show we're calling alloca or equivalent. */
1177 cfun->calls_alloca = 1;
1179 /* If stack usage info is requested, look into the size we are passed.
1180 We need to do so this early to avoid the obfuscation that may be
1181 introduced later by the various alignment operations. */
1182 if (flag_stack_usage_info)
1184 if (CONST_INT_P (size))
1185 stack_usage_size = INTVAL (size);
1186 else if (REG_P (size))
1188 /* Look into the last emitted insn and see if we can deduce
1189 something for the register. */
1190 rtx_insn *insn;
1191 rtx set, note;
1192 insn = get_last_insn ();
1193 if ((set = single_set (insn)) && rtx_equal_p (SET_DEST (set), size))
1195 if (CONST_INT_P (SET_SRC (set)))
1196 stack_usage_size = INTVAL (SET_SRC (set));
1197 else if ((note = find_reg_equal_equiv_note (insn))
1198 && CONST_INT_P (XEXP (note, 0)))
1199 stack_usage_size = INTVAL (XEXP (note, 0));
1203 /* If the size is not constant, we can't say anything. */
1204 if (stack_usage_size == -1)
1206 current_function_has_unbounded_dynamic_stack_size = 1;
1207 stack_usage_size = 0;
1211 /* Ensure the size is in the proper mode. */
1212 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1213 size = convert_to_mode (Pmode, size, 1);
1215 /* Adjust SIZE_ALIGN, if needed. */
1216 if (CONST_INT_P (size))
1218 unsigned HOST_WIDE_INT lsb;
1220 lsb = INTVAL (size);
1221 lsb &= -lsb;
1223 /* Watch out for overflow truncating to "unsigned". */
1224 if (lsb > UINT_MAX / BITS_PER_UNIT)
1225 size_align = 1u << (HOST_BITS_PER_INT - 1);
1226 else
1227 size_align = (unsigned)lsb * BITS_PER_UNIT;
1229 else if (size_align < BITS_PER_UNIT)
1230 size_align = BITS_PER_UNIT;
1232 /* We can't attempt to minimize alignment necessary, because we don't
1233 know the final value of preferred_stack_boundary yet while executing
1234 this code. */
1235 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
1236 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
1238 /* We will need to ensure that the address we return is aligned to
1239 REQUIRED_ALIGN. If STACK_DYNAMIC_OFFSET is defined, we don't
1240 always know its final value at this point in the compilation (it
1241 might depend on the size of the outgoing parameter lists, for
1242 example), so we must align the value to be returned in that case.
1243 (Note that STACK_DYNAMIC_OFFSET will have a default nonzero value if
1244 STACK_POINTER_OFFSET or ACCUMULATE_OUTGOING_ARGS are defined).
1245 We must also do an alignment operation on the returned value if
1246 the stack pointer alignment is less strict than REQUIRED_ALIGN.
1248 If we have to align, we must leave space in SIZE for the hole
1249 that might result from the alignment operation. */
1251 must_align = (crtl->preferred_stack_boundary < required_align);
1252 if (must_align)
1254 if (required_align > PREFERRED_STACK_BOUNDARY)
1255 extra_align = PREFERRED_STACK_BOUNDARY;
1256 else if (required_align > STACK_BOUNDARY)
1257 extra_align = STACK_BOUNDARY;
1258 else
1259 extra_align = BITS_PER_UNIT;
1262 /* ??? STACK_POINTER_OFFSET is always defined now. */
1263 #if defined (STACK_DYNAMIC_OFFSET) || defined (STACK_POINTER_OFFSET)
1264 must_align = true;
1265 extra_align = BITS_PER_UNIT;
1266 #endif
1268 if (must_align)
1270 unsigned extra = (required_align - extra_align) / BITS_PER_UNIT;
1272 size = plus_constant (Pmode, size, extra);
1273 size = force_operand (size, NULL_RTX);
1275 if (flag_stack_usage_info)
1276 stack_usage_size += extra;
1278 if (extra && size_align > extra_align)
1279 size_align = extra_align;
1282 /* Round the size to a multiple of the required stack alignment.
1283 Since the stack if presumed to be rounded before this allocation,
1284 this will maintain the required alignment.
1286 If the stack grows downward, we could save an insn by subtracting
1287 SIZE from the stack pointer and then aligning the stack pointer.
1288 The problem with this is that the stack pointer may be unaligned
1289 between the execution of the subtraction and alignment insns and
1290 some machines do not allow this. Even on those that do, some
1291 signal handlers malfunction if a signal should occur between those
1292 insns. Since this is an extremely rare event, we have no reliable
1293 way of knowing which systems have this problem. So we avoid even
1294 momentarily mis-aligning the stack. */
1295 if (size_align % MAX_SUPPORTED_STACK_ALIGNMENT != 0)
1297 size = round_push (size);
1299 if (flag_stack_usage_info)
1301 int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
1302 stack_usage_size = (stack_usage_size + align - 1) / align * align;
1306 target = gen_reg_rtx (Pmode);
1308 /* The size is supposed to be fully adjusted at this point so record it
1309 if stack usage info is requested. */
1310 if (flag_stack_usage_info)
1312 current_function_dynamic_stack_size += stack_usage_size;
1314 /* ??? This is gross but the only safe stance in the absence
1315 of stack usage oriented flow analysis. */
1316 if (!cannot_accumulate)
1317 current_function_has_unbounded_dynamic_stack_size = 1;
1320 final_label = NULL;
1321 final_target = NULL_RTX;
1323 /* If we are splitting the stack, we need to ask the backend whether
1324 there is enough room on the current stack. If there isn't, or if
1325 the backend doesn't know how to tell is, then we need to call a
1326 function to allocate memory in some other way. This memory will
1327 be released when we release the current stack segment. The
1328 effect is that stack allocation becomes less efficient, but at
1329 least it doesn't cause a stack overflow. */
1330 if (flag_split_stack)
1332 rtx_code_label *available_label;
1333 rtx ask, space, func;
1335 available_label = NULL;
1337 #ifdef HAVE_split_stack_space_check
1338 if (HAVE_split_stack_space_check)
1340 available_label = gen_label_rtx ();
1342 /* This instruction will branch to AVAILABLE_LABEL if there
1343 are SIZE bytes available on the stack. */
1344 emit_insn (gen_split_stack_space_check (size, available_label));
1346 #endif
1348 /* The __morestack_allocate_stack_space function will allocate
1349 memory using malloc. If the alignment of the memory returned
1350 by malloc does not meet REQUIRED_ALIGN, we increase SIZE to
1351 make sure we allocate enough space. */
1352 if (MALLOC_ABI_ALIGNMENT >= required_align)
1353 ask = size;
1354 else
1356 ask = expand_binop (Pmode, add_optab, size,
1357 gen_int_mode (required_align / BITS_PER_UNIT - 1,
1358 Pmode),
1359 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1360 must_align = true;
1363 func = init_one_libfunc ("__morestack_allocate_stack_space");
1365 space = emit_library_call_value (func, target, LCT_NORMAL, Pmode,
1366 1, ask, Pmode);
1368 if (available_label == NULL_RTX)
1369 return space;
1371 final_target = gen_reg_rtx (Pmode);
1373 emit_move_insn (final_target, space);
1375 final_label = gen_label_rtx ();
1376 emit_jump (final_label);
1378 emit_label (available_label);
1381 do_pending_stack_adjust ();
1383 /* We ought to be called always on the toplevel and stack ought to be aligned
1384 properly. */
1385 gcc_assert (!(stack_pointer_delta
1386 % (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)));
1388 /* If needed, check that we have the required amount of stack. Take into
1389 account what has already been checked. */
1390 if (STACK_CHECK_MOVING_SP)
1392 else if (flag_stack_check == GENERIC_STACK_CHECK)
1393 probe_stack_range (STACK_OLD_CHECK_PROTECT + STACK_CHECK_MAX_FRAME_SIZE,
1394 size);
1395 else if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
1396 probe_stack_range (STACK_CHECK_PROTECT, size);
1398 /* Don't let anti_adjust_stack emit notes. */
1399 suppress_reg_args_size = true;
1401 /* Perform the required allocation from the stack. Some systems do
1402 this differently than simply incrementing/decrementing from the
1403 stack pointer, such as acquiring the space by calling malloc(). */
1404 #ifdef HAVE_allocate_stack
1405 if (HAVE_allocate_stack)
1407 struct expand_operand ops[2];
1408 /* We don't have to check against the predicate for operand 0 since
1409 TARGET is known to be a pseudo of the proper mode, which must
1410 be valid for the operand. */
1411 create_fixed_operand (&ops[0], target);
1412 create_convert_operand_to (&ops[1], size, STACK_SIZE_MODE, true);
1413 expand_insn (CODE_FOR_allocate_stack, 2, ops);
1415 else
1416 #endif
1418 int saved_stack_pointer_delta;
1420 #ifndef STACK_GROWS_DOWNWARD
1421 emit_move_insn (target, virtual_stack_dynamic_rtx);
1422 #endif
1424 /* Check stack bounds if necessary. */
1425 if (crtl->limit_stack)
1427 rtx available;
1428 rtx_code_label *space_available = gen_label_rtx ();
1429 #ifdef STACK_GROWS_DOWNWARD
1430 available = expand_binop (Pmode, sub_optab,
1431 stack_pointer_rtx, stack_limit_rtx,
1432 NULL_RTX, 1, OPTAB_WIDEN);
1433 #else
1434 available = expand_binop (Pmode, sub_optab,
1435 stack_limit_rtx, stack_pointer_rtx,
1436 NULL_RTX, 1, OPTAB_WIDEN);
1437 #endif
1438 emit_cmp_and_jump_insns (available, size, GEU, NULL_RTX, Pmode, 1,
1439 space_available);
1440 #ifdef HAVE_trap
1441 if (HAVE_trap)
1442 emit_insn (gen_trap ());
1443 else
1444 #endif
1445 error ("stack limits not supported on this target");
1446 emit_barrier ();
1447 emit_label (space_available);
1450 saved_stack_pointer_delta = stack_pointer_delta;
1452 if (flag_stack_check && STACK_CHECK_MOVING_SP)
1453 anti_adjust_stack_and_probe (size, false);
1454 else
1455 anti_adjust_stack (size);
1457 /* Even if size is constant, don't modify stack_pointer_delta.
1458 The constant size alloca should preserve
1459 crtl->preferred_stack_boundary alignment. */
1460 stack_pointer_delta = saved_stack_pointer_delta;
1462 #ifdef STACK_GROWS_DOWNWARD
1463 emit_move_insn (target, virtual_stack_dynamic_rtx);
1464 #endif
1467 suppress_reg_args_size = false;
1469 /* Finish up the split stack handling. */
1470 if (final_label != NULL_RTX)
1472 gcc_assert (flag_split_stack);
1473 emit_move_insn (final_target, target);
1474 emit_label (final_label);
1475 target = final_target;
1478 if (must_align)
1480 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1481 but we know it can't. So add ourselves and then do
1482 TRUNC_DIV_EXPR. */
1483 target = expand_binop (Pmode, add_optab, target,
1484 gen_int_mode (required_align / BITS_PER_UNIT - 1,
1485 Pmode),
1486 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1487 target = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, target,
1488 gen_int_mode (required_align / BITS_PER_UNIT,
1489 Pmode),
1490 NULL_RTX, 1);
1491 target = expand_mult (Pmode, target,
1492 gen_int_mode (required_align / BITS_PER_UNIT,
1493 Pmode),
1494 NULL_RTX, 1);
1497 /* Now that we've committed to a return value, mark its alignment. */
1498 mark_reg_pointer (target, required_align);
1500 /* Record the new stack level for nonlocal gotos. */
1501 if (cfun->nonlocal_goto_save_area != 0)
1502 update_nonlocal_goto_save_area ();
1504 return target;
1507 /* A front end may want to override GCC's stack checking by providing a
1508 run-time routine to call to check the stack, so provide a mechanism for
1509 calling that routine. */
1511 static GTY(()) rtx stack_check_libfunc;
1513 void
1514 set_stack_check_libfunc (const char *libfunc_name)
1516 gcc_assert (stack_check_libfunc == NULL_RTX);
1517 stack_check_libfunc = gen_rtx_SYMBOL_REF (Pmode, libfunc_name);
1520 /* Emit one stack probe at ADDRESS, an address within the stack. */
1522 void
1523 emit_stack_probe (rtx address)
1525 #ifdef HAVE_probe_stack_address
1526 if (HAVE_probe_stack_address)
1527 emit_insn (gen_probe_stack_address (address));
1528 else
1529 #endif
1531 rtx memref = gen_rtx_MEM (word_mode, address);
1533 MEM_VOLATILE_P (memref) = 1;
1535 /* See if we have an insn to probe the stack. */
1536 #ifdef HAVE_probe_stack
1537 if (HAVE_probe_stack)
1538 emit_insn (gen_probe_stack (memref));
1539 else
1540 #endif
1541 emit_move_insn (memref, const0_rtx);
1545 /* Probe a range of stack addresses from FIRST to FIRST+SIZE, inclusive.
1546 FIRST is a constant and size is a Pmode RTX. These are offsets from
1547 the current stack pointer. STACK_GROWS_DOWNWARD says whether to add
1548 or subtract them from the stack pointer. */
1550 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
1552 #ifdef STACK_GROWS_DOWNWARD
1553 #define STACK_GROW_OP MINUS
1554 #define STACK_GROW_OPTAB sub_optab
1555 #define STACK_GROW_OFF(off) -(off)
1556 #else
1557 #define STACK_GROW_OP PLUS
1558 #define STACK_GROW_OPTAB add_optab
1559 #define STACK_GROW_OFF(off) (off)
1560 #endif
1562 void
1563 probe_stack_range (HOST_WIDE_INT first, rtx size)
1565 /* First ensure SIZE is Pmode. */
1566 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1567 size = convert_to_mode (Pmode, size, 1);
1569 /* Next see if we have a function to check the stack. */
1570 if (stack_check_libfunc)
1572 rtx addr = memory_address (Pmode,
1573 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1574 stack_pointer_rtx,
1575 plus_constant (Pmode,
1576 size, first)));
1577 emit_library_call (stack_check_libfunc, LCT_NORMAL, VOIDmode, 1, addr,
1578 Pmode);
1581 /* Next see if we have an insn to check the stack. */
1582 #ifdef HAVE_check_stack
1583 else if (HAVE_check_stack)
1585 struct expand_operand ops[1];
1586 rtx addr = memory_address (Pmode,
1587 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1588 stack_pointer_rtx,
1589 plus_constant (Pmode,
1590 size, first)));
1591 bool success;
1592 create_input_operand (&ops[0], addr, Pmode);
1593 success = maybe_expand_insn (CODE_FOR_check_stack, 1, ops);
1594 gcc_assert (success);
1596 #endif
1598 /* Otherwise we have to generate explicit probes. If we have a constant
1599 small number of them to generate, that's the easy case. */
1600 else if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
1602 HOST_WIDE_INT isize = INTVAL (size), i;
1603 rtx addr;
1605 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
1606 it exceeds SIZE. If only one probe is needed, this will not
1607 generate any code. Then probe at FIRST + SIZE. */
1608 for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
1610 addr = memory_address (Pmode,
1611 plus_constant (Pmode, stack_pointer_rtx,
1612 STACK_GROW_OFF (first + i)));
1613 emit_stack_probe (addr);
1616 addr = memory_address (Pmode,
1617 plus_constant (Pmode, stack_pointer_rtx,
1618 STACK_GROW_OFF (first + isize)));
1619 emit_stack_probe (addr);
1622 /* In the variable case, do the same as above, but in a loop. Note that we
1623 must be extra careful with variables wrapping around because we might be
1624 at the very top (or the very bottom) of the address space and we have to
1625 be able to handle this case properly; in particular, we use an equality
1626 test for the loop condition. */
1627 else
1629 rtx rounded_size, rounded_size_op, test_addr, last_addr, temp;
1630 rtx_code_label *loop_lab = gen_label_rtx ();
1631 rtx_code_label *end_lab = gen_label_rtx ();
1633 /* Step 1: round SIZE to the previous multiple of the interval. */
1635 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1636 rounded_size
1637 = simplify_gen_binary (AND, Pmode, size,
1638 gen_int_mode (-PROBE_INTERVAL, Pmode));
1639 rounded_size_op = force_operand (rounded_size, NULL_RTX);
1642 /* Step 2: compute initial and final value of the loop counter. */
1644 /* TEST_ADDR = SP + FIRST. */
1645 test_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1646 stack_pointer_rtx,
1647 gen_int_mode (first, Pmode)),
1648 NULL_RTX);
1650 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
1651 last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1652 test_addr,
1653 rounded_size_op), NULL_RTX);
1656 /* Step 3: the loop
1658 while (TEST_ADDR != LAST_ADDR)
1660 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
1661 probe at TEST_ADDR
1664 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
1665 until it is equal to ROUNDED_SIZE. */
1667 emit_label (loop_lab);
1669 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
1670 emit_cmp_and_jump_insns (test_addr, last_addr, EQ, NULL_RTX, Pmode, 1,
1671 end_lab);
1673 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
1674 temp = expand_binop (Pmode, STACK_GROW_OPTAB, test_addr,
1675 gen_int_mode (PROBE_INTERVAL, Pmode), test_addr,
1676 1, OPTAB_WIDEN);
1678 gcc_assert (temp == test_addr);
1680 /* Probe at TEST_ADDR. */
1681 emit_stack_probe (test_addr);
1683 emit_jump (loop_lab);
1685 emit_label (end_lab);
1688 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
1689 that SIZE is equal to ROUNDED_SIZE. */
1691 /* TEMP = SIZE - ROUNDED_SIZE. */
1692 temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
1693 if (temp != const0_rtx)
1695 rtx addr;
1697 if (CONST_INT_P (temp))
1699 /* Use [base + disp} addressing mode if supported. */
1700 HOST_WIDE_INT offset = INTVAL (temp);
1701 addr = memory_address (Pmode,
1702 plus_constant (Pmode, last_addr,
1703 STACK_GROW_OFF (offset)));
1705 else
1707 /* Manual CSE if the difference is not known at compile-time. */
1708 temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
1709 addr = memory_address (Pmode,
1710 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1711 last_addr, temp));
1714 emit_stack_probe (addr);
1718 /* Make sure nothing is scheduled before we are done. */
1719 emit_insn (gen_blockage ());
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. */
1727 void
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
1740 easy case. */
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)
1752 if (first_probe)
1754 anti_adjust_stack (GEN_INT (2 * PROBE_INTERVAL + dope));
1755 first_probe = false;
1757 else
1758 anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
1759 emit_stack_probe (stack_pointer_rtx);
1762 if (first_probe)
1763 anti_adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL + dope));
1764 else
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. */
1774 else
1776 rtx rounded_size, rounded_size_op, last_addr, temp;
1777 rtx_code_label *loop_lab = gen_label_rtx ();
1778 rtx_code_label *end_lab = gen_label_rtx ();
1781 /* Step 1: round SIZE to the previous multiple of the interval. */
1783 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1784 rounded_size
1785 = simplify_gen_binary (AND, Pmode, size,
1786 gen_int_mode (-PROBE_INTERVAL, Pmode));
1787 rounded_size_op = force_operand (rounded_size, NULL_RTX);
1790 /* Step 2: compute initial and final value of the loop counter. */
1792 /* SP = SP_0 + PROBE_INTERVAL. */
1793 anti_adjust_stack (GEN_INT (PROBE_INTERVAL + dope));
1795 /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE. */
1796 last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1797 stack_pointer_rtx,
1798 rounded_size_op), NULL_RTX);
1801 /* Step 3: the loop
1803 while (SP != LAST_ADDR)
1805 SP = SP + PROBE_INTERVAL
1806 probe at SP
1809 adjusts SP and probes at PROBE_INTERVAL + N * PROBE_INTERVAL for
1810 values of N from 1 until it is equal to ROUNDED_SIZE. */
1812 emit_label (loop_lab);
1814 /* Jump to END_LAB if SP == LAST_ADDR. */
1815 emit_cmp_and_jump_insns (stack_pointer_rtx, last_addr, EQ, NULL_RTX,
1816 Pmode, 1, end_lab);
1818 /* SP = SP + PROBE_INTERVAL and probe at SP. */
1819 anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
1820 emit_stack_probe (stack_pointer_rtx);
1822 emit_jump (loop_lab);
1824 emit_label (end_lab);
1827 /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
1828 assert at compile-time that SIZE is equal to ROUNDED_SIZE. */
1830 /* TEMP = SIZE - ROUNDED_SIZE. */
1831 temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
1832 if (temp != const0_rtx)
1834 /* Manual CSE if the difference is not known at compile-time. */
1835 if (GET_CODE (temp) != CONST_INT)
1836 temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
1837 anti_adjust_stack (temp);
1838 emit_stack_probe (stack_pointer_rtx);
1842 /* Adjust back and account for the additional first interval. */
1843 if (adjust_back)
1844 adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL + dope));
1845 else
1846 adjust_stack (GEN_INT (PROBE_INTERVAL + dope));
1849 /* Return an rtx representing the register or memory location
1850 in which a scalar value of data type VALTYPE
1851 was returned by a function call to function FUNC.
1852 FUNC is a FUNCTION_DECL, FNTYPE a FUNCTION_TYPE node if the precise
1853 function is known, otherwise 0.
1854 OUTGOING is 1 if on a machine with register windows this function
1855 should return the register in which the function will put its result
1856 and 0 otherwise. */
1859 hard_function_value (const_tree valtype, const_tree func, const_tree fntype,
1860 int outgoing ATTRIBUTE_UNUSED)
1862 rtx val;
1864 val = targetm.calls.function_value (valtype, func ? func : fntype, outgoing);
1866 if (REG_P (val)
1867 && GET_MODE (val) == BLKmode)
1869 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (valtype);
1870 enum machine_mode tmpmode;
1872 /* int_size_in_bytes can return -1. We don't need a check here
1873 since the value of bytes will then be large enough that no
1874 mode will match anyway. */
1876 for (tmpmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1877 tmpmode != VOIDmode;
1878 tmpmode = GET_MODE_WIDER_MODE (tmpmode))
1880 /* Have we found a large enough mode? */
1881 if (GET_MODE_SIZE (tmpmode) >= bytes)
1882 break;
1885 /* No suitable mode found. */
1886 gcc_assert (tmpmode != VOIDmode);
1888 PUT_MODE (val, tmpmode);
1890 return val;
1893 /* Return an rtx representing the register or memory location
1894 in which a scalar value of mode MODE was returned by a library call. */
1897 hard_libcall_value (enum machine_mode mode, rtx fun)
1899 return targetm.calls.libcall_value (mode, fun);
1902 /* Look up the tree code for a given rtx code
1903 to provide the arithmetic operation for REAL_ARITHMETIC.
1904 The function returns an int because the caller may not know
1905 what `enum tree_code' means. */
1908 rtx_to_tree_code (enum rtx_code code)
1910 enum tree_code tcode;
1912 switch (code)
1914 case PLUS:
1915 tcode = PLUS_EXPR;
1916 break;
1917 case MINUS:
1918 tcode = MINUS_EXPR;
1919 break;
1920 case MULT:
1921 tcode = MULT_EXPR;
1922 break;
1923 case DIV:
1924 tcode = RDIV_EXPR;
1925 break;
1926 case SMIN:
1927 tcode = MIN_EXPR;
1928 break;
1929 case SMAX:
1930 tcode = MAX_EXPR;
1931 break;
1932 default:
1933 tcode = LAST_AND_UNUSED_TREE_CODE;
1934 break;
1936 return ((int) tcode);
1939 #include "gt-explow.h"