Merge aosp-toolchain/gcc/gcc-4_9 changes.
[official-gcc.git] / gcc-4_9 / gcc / explow.c
blob4e0aedf6bb237a192c0c7a98d73b7b985bfdb6d1
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. */
79 rtx
80 plus_constant (enum machine_mode mode, rtx x, HOST_WIDE_INT c)
82 RTX_CODE code;
83 rtx y;
84 rtx tem;
85 int all_constant = 0;
87 gcc_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
89 if (c == 0)
90 return x;
92 restart:
94 code = GET_CODE (x);
95 y = x;
97 switch (code)
99 case CONST_INT:
100 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
102 double_int di_x = double_int::from_shwi (INTVAL (x));
103 double_int di_c = double_int::from_shwi (c);
105 bool overflow;
106 double_int v = di_x.add_with_sign (di_c, false, &overflow);
107 if (overflow)
108 gcc_unreachable ();
110 return immed_double_int_const (v, mode);
113 return gen_int_mode (UINTVAL (x) + c, mode);
115 case CONST_DOUBLE:
117 double_int di_x = double_int::from_pair (CONST_DOUBLE_HIGH (x),
118 CONST_DOUBLE_LOW (x));
119 double_int di_c = double_int::from_shwi (c);
121 bool overflow;
122 double_int v = di_x.add_with_sign (di_c, false, &overflow);
123 if (overflow)
124 /* Sorry, we have no way to represent overflows this wide.
125 To fix, add constant support wider than CONST_DOUBLE. */
126 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_DOUBLE_INT);
128 return immed_double_int_const (v, mode);
131 case MEM:
132 /* If this is a reference to the constant pool, try replacing it with
133 a reference to a new constant. If the resulting address isn't
134 valid, don't return it because we have no way to validize it. */
135 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
136 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
138 tem = plus_constant (mode, get_pool_constant (XEXP (x, 0)), c);
139 tem = force_const_mem (GET_MODE (x), tem);
140 /* Targets may disallow some constants in the constant pool, thus
141 force_const_mem may return NULL_RTX. */
142 if (tem && memory_address_p (GET_MODE (tem), XEXP (tem, 0)))
143 return tem;
145 break;
147 case CONST:
148 /* If adding to something entirely constant, set a flag
149 so that we can add a CONST around the result. */
150 x = XEXP (x, 0);
151 all_constant = 1;
152 goto restart;
154 case SYMBOL_REF:
155 case LABEL_REF:
156 all_constant = 1;
157 break;
159 case PLUS:
160 /* The interesting case is adding the integer to a sum. Look
161 for constant term in the sum and combine with C. For an
162 integer constant term or a constant term that is not an
163 explicit integer, we combine or group them together anyway.
165 We may not immediately return from the recursive call here, lest
166 all_constant gets lost. */
168 if (CONSTANT_P (XEXP (x, 1)))
170 x = gen_rtx_PLUS (mode, XEXP (x, 0),
171 plus_constant (mode, XEXP (x, 1), c));
172 c = 0;
174 else if (find_constant_term_loc (&y))
176 /* We need to be careful since X may be shared and we can't
177 modify it in place. */
178 rtx copy = copy_rtx (x);
179 rtx *const_loc = find_constant_term_loc (&copy);
181 *const_loc = plus_constant (mode, *const_loc, c);
182 x = copy;
183 c = 0;
185 break;
187 default:
188 break;
191 if (c != 0)
192 x = gen_rtx_PLUS (mode, x, gen_int_mode (c, mode));
194 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
195 return x;
196 else if (all_constant)
197 return gen_rtx_CONST (mode, x);
198 else
199 return x;
202 /* If X is a sum, return a new sum like X but lacking any constant terms.
203 Add all the removed constant terms into *CONSTPTR.
204 X itself is not altered. The result != X if and only if
205 it is not isomorphic to X. */
208 eliminate_constant_term (rtx x, rtx *constptr)
210 rtx x0, x1;
211 rtx tem;
213 if (GET_CODE (x) != PLUS)
214 return x;
216 /* First handle constants appearing at this level explicitly. */
217 if (CONST_INT_P (XEXP (x, 1))
218 && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x), *constptr,
219 XEXP (x, 1)))
220 && CONST_INT_P (tem))
222 *constptr = tem;
223 return eliminate_constant_term (XEXP (x, 0), constptr);
226 tem = const0_rtx;
227 x0 = eliminate_constant_term (XEXP (x, 0), &tem);
228 x1 = eliminate_constant_term (XEXP (x, 1), &tem);
229 if ((x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
230 && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x),
231 *constptr, tem))
232 && CONST_INT_P (tem))
234 *constptr = tem;
235 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
238 return x;
241 /* Returns a tree for the size of EXP in bytes. */
243 static tree
244 tree_expr_size (const_tree exp)
246 if (DECL_P (exp)
247 && DECL_SIZE_UNIT (exp) != 0)
248 return DECL_SIZE_UNIT (exp);
249 else
250 return size_in_bytes (TREE_TYPE (exp));
253 /* Return an rtx for the size in bytes of the value of EXP. */
256 expr_size (tree exp)
258 tree size;
260 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
261 size = TREE_OPERAND (exp, 1);
262 else
264 size = tree_expr_size (exp);
265 gcc_assert (size);
266 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
269 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
272 /* Return a wide integer for the size in bytes of the value of EXP, or -1
273 if the size can vary or is larger than an integer. */
275 HOST_WIDE_INT
276 int_expr_size (tree exp)
278 tree size;
280 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
281 size = TREE_OPERAND (exp, 1);
282 else
284 size = tree_expr_size (exp);
285 gcc_assert (size);
288 if (size == 0 || !tree_fits_shwi_p (size))
289 return -1;
291 return tree_to_shwi (size);
294 /* Return a copy of X in which all memory references
295 and all constants that involve symbol refs
296 have been replaced with new temporary registers.
297 Also emit code to load the memory locations and constants
298 into those registers.
300 If X contains no such constants or memory references,
301 X itself (not a copy) is returned.
303 If a constant is found in the address that is not a legitimate constant
304 in an insn, it is left alone in the hope that it might be valid in the
305 address.
307 X may contain no arithmetic except addition, subtraction and multiplication.
308 Values returned by expand_expr with 1 for sum_ok fit this constraint. */
310 static rtx
311 break_out_memory_refs (rtx x)
313 if (MEM_P (x)
314 || (CONSTANT_P (x) && CONSTANT_ADDRESS_P (x)
315 && GET_MODE (x) != VOIDmode))
316 x = force_reg (GET_MODE (x), x);
317 else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
318 || GET_CODE (x) == MULT)
320 rtx op0 = break_out_memory_refs (XEXP (x, 0));
321 rtx op1 = break_out_memory_refs (XEXP (x, 1));
323 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
324 x = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
327 return x;
330 /* Given X, a memory address in address space AS' pointer mode, convert it to
331 an address in the address space's address mode, or vice versa (TO_MODE says
332 which way). We take advantage of the fact that pointers are not allowed to
333 overflow by commuting arithmetic operations over conversions so that address
334 arithmetic insns can be used. */
337 convert_memory_address_addr_space (enum machine_mode to_mode ATTRIBUTE_UNUSED,
338 rtx x, addr_space_t as ATTRIBUTE_UNUSED)
340 #ifndef POINTERS_EXTEND_UNSIGNED
341 gcc_assert (GET_MODE (x) == to_mode || GET_MODE (x) == VOIDmode);
342 return x;
343 #else /* defined(POINTERS_EXTEND_UNSIGNED) */
344 enum machine_mode pointer_mode, address_mode, from_mode;
345 rtx temp;
346 enum rtx_code code;
348 /* If X already has the right mode, just return it. */
349 if (GET_MODE (x) == to_mode)
350 return x;
352 pointer_mode = targetm.addr_space.pointer_mode (as);
353 address_mode = targetm.addr_space.address_mode (as);
354 from_mode = to_mode == pointer_mode ? address_mode : pointer_mode;
356 /* Here we handle some special cases. If none of them apply, fall through
357 to the default case. */
358 switch (GET_CODE (x))
360 CASE_CONST_SCALAR_INT:
361 if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode))
362 code = TRUNCATE;
363 else if (POINTERS_EXTEND_UNSIGNED < 0)
364 break;
365 else if (POINTERS_EXTEND_UNSIGNED > 0)
366 code = ZERO_EXTEND;
367 else
368 code = SIGN_EXTEND;
369 temp = simplify_unary_operation (code, to_mode, x, from_mode);
370 if (temp)
371 return temp;
372 break;
374 case SUBREG:
375 if ((SUBREG_PROMOTED_VAR_P (x) || REG_POINTER (SUBREG_REG (x)))
376 && GET_MODE (SUBREG_REG (x)) == to_mode)
377 return SUBREG_REG (x);
378 break;
380 case LABEL_REF:
381 temp = gen_rtx_LABEL_REF (to_mode, XEXP (x, 0));
382 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
383 return temp;
384 break;
386 case SYMBOL_REF:
387 temp = shallow_copy_rtx (x);
388 PUT_MODE (temp, to_mode);
389 return temp;
390 break;
392 case CONST:
393 return gen_rtx_CONST (to_mode,
394 convert_memory_address_addr_space
395 (to_mode, XEXP (x, 0), as));
396 break;
398 case PLUS:
399 case MULT:
400 /* FIXME: For addition, we used to permute the conversion and
401 addition operation only if one operand is a constant and
402 converting the constant does not change it or if one operand
403 is a constant and we are using a ptr_extend instruction
404 (POINTERS_EXTEND_UNSIGNED < 0) even if the resulting address
405 may overflow/underflow. We relax the condition to include
406 zero-extend (POINTERS_EXTEND_UNSIGNED > 0) since the other
407 parts of the compiler depend on it. See PR 49721.
409 We can always safely permute them if we are making the address
410 narrower. */
411 if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode)
412 || (GET_CODE (x) == PLUS
413 && CONST_INT_P (XEXP (x, 1))
414 && (POINTERS_EXTEND_UNSIGNED != 0
415 || XEXP (x, 1) == convert_memory_address_addr_space
416 (to_mode, XEXP (x, 1), as))))
417 return gen_rtx_fmt_ee (GET_CODE (x), to_mode,
418 convert_memory_address_addr_space
419 (to_mode, XEXP (x, 0), as),
420 XEXP (x, 1));
421 break;
423 default:
424 break;
427 return convert_modes (to_mode, from_mode,
428 x, POINTERS_EXTEND_UNSIGNED);
429 #endif /* defined(POINTERS_EXTEND_UNSIGNED) */
432 /* Return something equivalent to X but valid as a memory address for something
433 of mode MODE in the named address space AS. When X is not itself valid,
434 this works by copying X or subexpressions of it into registers. */
437 memory_address_addr_space (enum machine_mode mode, rtx x, addr_space_t as)
439 rtx oldx = x;
440 enum machine_mode address_mode = targetm.addr_space.address_mode (as);
442 x = convert_memory_address_addr_space (address_mode, x, as);
444 /* By passing constant addresses through registers
445 we get a chance to cse them. */
446 if (! cse_not_expected && CONSTANT_P (x) && CONSTANT_ADDRESS_P (x))
447 x = force_reg (address_mode, x);
449 /* We get better cse by rejecting indirect addressing at this stage.
450 Let the combiner create indirect addresses where appropriate.
451 For now, generate the code so that the subexpressions useful to share
452 are visible. But not if cse won't be done! */
453 else
455 if (! cse_not_expected && !REG_P (x))
456 x = break_out_memory_refs (x);
458 /* At this point, any valid address is accepted. */
459 if (memory_address_addr_space_p (mode, x, as))
460 goto done;
462 /* If it was valid before but breaking out memory refs invalidated it,
463 use it the old way. */
464 if (memory_address_addr_space_p (mode, oldx, as))
466 x = oldx;
467 goto done;
470 /* Perform machine-dependent transformations on X
471 in certain cases. This is not necessary since the code
472 below can handle all possible cases, but machine-dependent
473 transformations can make better code. */
475 rtx orig_x = x;
476 x = targetm.addr_space.legitimize_address (x, oldx, mode, as);
477 if (orig_x != x && memory_address_addr_space_p (mode, x, as))
478 goto done;
481 /* PLUS and MULT can appear in special ways
482 as the result of attempts to make an address usable for indexing.
483 Usually they are dealt with by calling force_operand, below.
484 But a sum containing constant terms is special
485 if removing them makes the sum a valid address:
486 then we generate that address in a register
487 and index off of it. We do this because it often makes
488 shorter code, and because the addresses thus generated
489 in registers often become common subexpressions. */
490 if (GET_CODE (x) == PLUS)
492 rtx constant_term = const0_rtx;
493 rtx y = eliminate_constant_term (x, &constant_term);
494 if (constant_term == const0_rtx
495 || ! memory_address_addr_space_p (mode, y, as))
496 x = force_operand (x, NULL_RTX);
497 else
499 y = gen_rtx_PLUS (GET_MODE (x), copy_to_reg (y), constant_term);
500 if (! memory_address_addr_space_p (mode, y, as))
501 x = force_operand (x, NULL_RTX);
502 else
503 x = y;
507 else if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
508 x = force_operand (x, NULL_RTX);
510 /* If we have a register that's an invalid address,
511 it must be a hard reg of the wrong class. Copy it to a pseudo. */
512 else if (REG_P (x))
513 x = copy_to_reg (x);
515 /* Last resort: copy the value to a register, since
516 the register is a valid address. */
517 else
518 x = force_reg (address_mode, x);
521 done:
523 gcc_assert (memory_address_addr_space_p (mode, x, as));
524 /* If we didn't change the address, we are done. Otherwise, mark
525 a reg as a pointer if we have REG or REG + CONST_INT. */
526 if (oldx == x)
527 return x;
528 else if (REG_P (x))
529 mark_reg_pointer (x, BITS_PER_UNIT);
530 else if (GET_CODE (x) == PLUS
531 && REG_P (XEXP (x, 0))
532 && CONST_INT_P (XEXP (x, 1)))
533 mark_reg_pointer (XEXP (x, 0), BITS_PER_UNIT);
535 /* OLDX may have been the address on a temporary. Update the address
536 to indicate that X is now used. */
537 update_temp_slot_address (oldx, x);
539 return x;
542 /* Convert a mem ref into one with a valid memory address.
543 Pass through anything else unchanged. */
546 validize_mem (rtx ref)
548 if (!MEM_P (ref))
549 return ref;
550 ref = use_anchored_address (ref);
551 if (memory_address_addr_space_p (GET_MODE (ref), XEXP (ref, 0),
552 MEM_ADDR_SPACE (ref)))
553 return ref;
555 /* Don't alter REF itself, since that is probably a stack slot. */
556 return replace_equiv_address (ref, XEXP (ref, 0));
559 /* If X is a memory reference to a member of an object block, try rewriting
560 it to use an anchor instead. Return the new memory reference on success
561 and the old one on failure. */
564 use_anchored_address (rtx x)
566 rtx base;
567 HOST_WIDE_INT offset;
568 enum machine_mode mode;
570 if (!flag_section_anchors)
571 return x;
573 if (!MEM_P (x))
574 return x;
576 /* Split the address into a base and offset. */
577 base = XEXP (x, 0);
578 offset = 0;
579 if (GET_CODE (base) == CONST
580 && GET_CODE (XEXP (base, 0)) == PLUS
581 && CONST_INT_P (XEXP (XEXP (base, 0), 1)))
583 offset += INTVAL (XEXP (XEXP (base, 0), 1));
584 base = XEXP (XEXP (base, 0), 0);
587 /* Check whether BASE is suitable for anchors. */
588 if (GET_CODE (base) != SYMBOL_REF
589 || !SYMBOL_REF_HAS_BLOCK_INFO_P (base)
590 || SYMBOL_REF_ANCHOR_P (base)
591 || SYMBOL_REF_BLOCK (base) == NULL
592 || !targetm.use_anchors_for_symbol_p (base))
593 return x;
595 /* Decide where BASE is going to be. */
596 place_block_symbol (base);
598 /* Get the anchor we need to use. */
599 offset += SYMBOL_REF_BLOCK_OFFSET (base);
600 base = get_section_anchor (SYMBOL_REF_BLOCK (base), offset,
601 SYMBOL_REF_TLS_MODEL (base));
603 /* Work out the offset from the anchor. */
604 offset -= SYMBOL_REF_BLOCK_OFFSET (base);
606 /* If we're going to run a CSE pass, force the anchor into a register.
607 We will then be able to reuse registers for several accesses, if the
608 target costs say that that's worthwhile. */
609 mode = GET_MODE (base);
610 if (!cse_not_expected)
611 base = force_reg (mode, base);
613 return replace_equiv_address (x, plus_constant (mode, base, offset));
616 /* Copy the value or contents of X to a new temp reg and return that reg. */
619 copy_to_reg (rtx x)
621 rtx temp = gen_reg_rtx (GET_MODE (x));
623 /* If not an operand, must be an address with PLUS and MULT so
624 do the computation. */
625 if (! general_operand (x, VOIDmode))
626 x = force_operand (x, temp);
628 if (x != temp)
629 emit_move_insn (temp, x);
631 return temp;
634 /* Like copy_to_reg but always give the new register mode Pmode
635 in case X is a constant. */
638 copy_addr_to_reg (rtx x)
640 return copy_to_mode_reg (Pmode, x);
643 /* Like copy_to_reg but always give the new register mode MODE
644 in case X is a constant. */
647 copy_to_mode_reg (enum machine_mode mode, rtx x)
649 rtx temp = gen_reg_rtx (mode);
651 /* If not an operand, must be an address with PLUS and MULT so
652 do the computation. */
653 if (! general_operand (x, VOIDmode))
654 x = force_operand (x, temp);
656 gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
657 if (x != temp)
658 emit_move_insn (temp, x);
659 return temp;
662 /* Load X into a register if it is not already one.
663 Use mode MODE for the register.
664 X should be valid for mode MODE, but it may be a constant which
665 is valid for all integer modes; that's why caller must specify MODE.
667 The caller must not alter the value in the register we return,
668 since we mark it as a "constant" register. */
671 force_reg (enum machine_mode mode, rtx x)
673 rtx temp, insn, set;
675 if (REG_P (x))
676 return x;
678 if (general_operand (x, mode))
680 temp = gen_reg_rtx (mode);
681 insn = emit_move_insn (temp, x);
683 else
685 temp = force_operand (x, NULL_RTX);
686 if (REG_P (temp))
687 insn = get_last_insn ();
688 else
690 rtx temp2 = gen_reg_rtx (mode);
691 insn = emit_move_insn (temp2, temp);
692 temp = temp2;
696 /* Let optimizers know that TEMP's value never changes
697 and that X can be substituted for it. Don't get confused
698 if INSN set something else (such as a SUBREG of TEMP). */
699 if (CONSTANT_P (x)
700 && (set = single_set (insn)) != 0
701 && SET_DEST (set) == temp
702 && ! rtx_equal_p (x, SET_SRC (set)))
703 set_unique_reg_note (insn, REG_EQUAL, x);
705 /* Let optimizers know that TEMP is a pointer, and if so, the
706 known alignment of that pointer. */
708 unsigned align = 0;
709 if (GET_CODE (x) == SYMBOL_REF)
711 align = BITS_PER_UNIT;
712 if (SYMBOL_REF_DECL (x) && DECL_P (SYMBOL_REF_DECL (x)))
713 align = DECL_ALIGN (SYMBOL_REF_DECL (x));
715 else if (GET_CODE (x) == LABEL_REF)
716 align = BITS_PER_UNIT;
717 else if (GET_CODE (x) == CONST
718 && GET_CODE (XEXP (x, 0)) == PLUS
719 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
720 && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
722 rtx s = XEXP (XEXP (x, 0), 0);
723 rtx c = XEXP (XEXP (x, 0), 1);
724 unsigned sa, ca;
726 sa = BITS_PER_UNIT;
727 if (SYMBOL_REF_DECL (s) && DECL_P (SYMBOL_REF_DECL (s)))
728 sa = DECL_ALIGN (SYMBOL_REF_DECL (s));
730 if (INTVAL (c) == 0)
731 align = sa;
732 else
734 ca = ctz_hwi (INTVAL (c)) * BITS_PER_UNIT;
735 align = MIN (sa, ca);
739 if (align || (MEM_P (x) && MEM_POINTER (x)))
740 mark_reg_pointer (temp, align);
743 return temp;
746 /* If X is a memory ref, copy its contents to a new temp reg and return
747 that reg. Otherwise, return X. */
750 force_not_mem (rtx x)
752 rtx temp;
754 if (!MEM_P (x) || GET_MODE (x) == BLKmode)
755 return x;
757 temp = gen_reg_rtx (GET_MODE (x));
759 if (MEM_POINTER (x))
760 REG_POINTER (temp) = 1;
762 emit_move_insn (temp, x);
763 return temp;
766 /* Copy X to TARGET (if it's nonzero and a reg)
767 or to a new temp reg and return that reg.
768 MODE is the mode to use for X in case it is a constant. */
771 copy_to_suggested_reg (rtx x, rtx target, enum machine_mode mode)
773 rtx temp;
775 if (target && REG_P (target))
776 temp = target;
777 else
778 temp = gen_reg_rtx (mode);
780 emit_move_insn (temp, x);
781 return temp;
784 /* Return the mode to use to pass or return a scalar of TYPE and MODE.
785 PUNSIGNEDP points to the signedness of the type and may be adjusted
786 to show what signedness to use on extension operations.
788 FOR_RETURN is nonzero if the caller is promoting the return value
789 of FNDECL, else it is for promoting args. */
791 enum machine_mode
792 promote_function_mode (const_tree type, enum machine_mode mode, int *punsignedp,
793 const_tree funtype, int for_return)
795 /* Called without a type node for a libcall. */
796 if (type == NULL_TREE)
798 if (INTEGRAL_MODE_P (mode))
799 return targetm.calls.promote_function_mode (NULL_TREE, mode,
800 punsignedp, funtype,
801 for_return);
802 else
803 return mode;
806 switch (TREE_CODE (type))
808 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
809 case REAL_TYPE: case OFFSET_TYPE: case FIXED_POINT_TYPE:
810 case POINTER_TYPE: case REFERENCE_TYPE:
811 return targetm.calls.promote_function_mode (type, mode, punsignedp, funtype,
812 for_return);
814 default:
815 return mode;
818 /* Return the mode to use to store a scalar of TYPE and MODE.
819 PUNSIGNEDP points to the signedness of the type and may be adjusted
820 to show what signedness to use on extension operations. */
822 enum machine_mode
823 promote_mode (const_tree type ATTRIBUTE_UNUSED, enum machine_mode mode,
824 int *punsignedp ATTRIBUTE_UNUSED)
826 #ifdef PROMOTE_MODE
827 enum tree_code code;
828 int unsignedp;
829 #endif
831 /* For libcalls this is invoked without TYPE from the backends
832 TARGET_PROMOTE_FUNCTION_MODE hooks. Don't do anything in that
833 case. */
834 if (type == NULL_TREE)
835 return mode;
837 /* FIXME: this is the same logic that was there until GCC 4.4, but we
838 probably want to test POINTERS_EXTEND_UNSIGNED even if PROMOTE_MODE
839 is not defined. The affected targets are M32C, S390, SPARC. */
840 #ifdef PROMOTE_MODE
841 code = TREE_CODE (type);
842 unsignedp = *punsignedp;
844 switch (code)
846 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
847 case REAL_TYPE: case OFFSET_TYPE: case FIXED_POINT_TYPE:
848 PROMOTE_MODE (mode, unsignedp, type);
849 *punsignedp = unsignedp;
850 return mode;
851 break;
853 #ifdef POINTERS_EXTEND_UNSIGNED
854 case REFERENCE_TYPE:
855 case POINTER_TYPE:
856 *punsignedp = POINTERS_EXTEND_UNSIGNED;
857 return targetm.addr_space.address_mode
858 (TYPE_ADDR_SPACE (TREE_TYPE (type)));
859 break;
860 #endif
862 default:
863 return mode;
865 #else
866 return mode;
867 #endif
871 /* Use one of promote_mode or promote_function_mode to find the promoted
872 mode of DECL. If PUNSIGNEDP is not NULL, store there the unsignedness
873 of DECL after promotion. */
875 enum machine_mode
876 promote_decl_mode (const_tree decl, int *punsignedp)
878 tree type = TREE_TYPE (decl);
879 int unsignedp = TYPE_UNSIGNED (type);
880 enum machine_mode mode = DECL_MODE (decl);
881 enum machine_mode pmode;
883 if (TREE_CODE (decl) == RESULT_DECL
884 || TREE_CODE (decl) == PARM_DECL)
885 pmode = promote_function_mode (type, mode, &unsignedp,
886 TREE_TYPE (current_function_decl), 2);
887 else
888 pmode = promote_mode (type, mode, &unsignedp);
890 if (punsignedp)
891 *punsignedp = unsignedp;
892 return pmode;
896 /* Controls the behaviour of {anti_,}adjust_stack. */
897 static bool suppress_reg_args_size;
899 /* A helper for adjust_stack and anti_adjust_stack. */
901 static void
902 adjust_stack_1 (rtx adjust, bool anti_p)
904 rtx temp, insn;
906 #ifndef STACK_GROWS_DOWNWARD
907 /* Hereafter anti_p means subtract_p. */
908 anti_p = !anti_p;
909 #endif
911 temp = expand_binop (Pmode,
912 anti_p ? sub_optab : add_optab,
913 stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
914 OPTAB_LIB_WIDEN);
916 if (temp != stack_pointer_rtx)
917 insn = emit_move_insn (stack_pointer_rtx, temp);
918 else
920 insn = get_last_insn ();
921 temp = single_set (insn);
922 gcc_assert (temp != NULL && SET_DEST (temp) == stack_pointer_rtx);
925 if (!suppress_reg_args_size)
926 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
929 /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
930 This pops when ADJUST is positive. ADJUST need not be constant. */
932 void
933 adjust_stack (rtx adjust)
935 if (adjust == const0_rtx)
936 return;
938 /* We expect all variable sized adjustments to be multiple of
939 PREFERRED_STACK_BOUNDARY. */
940 if (CONST_INT_P (adjust))
941 stack_pointer_delta -= INTVAL (adjust);
943 adjust_stack_1 (adjust, false);
946 /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
947 This pushes when ADJUST is positive. ADJUST need not be constant. */
949 void
950 anti_adjust_stack (rtx adjust)
952 if (adjust == const0_rtx)
953 return;
955 /* We expect all variable sized adjustments to be multiple of
956 PREFERRED_STACK_BOUNDARY. */
957 if (CONST_INT_P (adjust))
958 stack_pointer_delta += INTVAL (adjust);
960 adjust_stack_1 (adjust, true);
963 /* Round the size of a block to be pushed up to the boundary required
964 by this machine. SIZE is the desired size, which need not be constant. */
966 static rtx
967 round_push (rtx size)
969 rtx align_rtx, alignm1_rtx;
971 if (!SUPPORTS_STACK_ALIGNMENT
972 || crtl->preferred_stack_boundary == MAX_SUPPORTED_STACK_ALIGNMENT)
974 int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
976 if (align == 1)
977 return size;
979 if (CONST_INT_P (size))
981 HOST_WIDE_INT new_size = (INTVAL (size) + align - 1) / align * align;
983 if (INTVAL (size) != new_size)
984 size = GEN_INT (new_size);
985 return size;
988 align_rtx = GEN_INT (align);
989 alignm1_rtx = GEN_INT (align - 1);
991 else
993 /* If crtl->preferred_stack_boundary might still grow, use
994 virtual_preferred_stack_boundary_rtx instead. This will be
995 substituted by the right value in vregs pass and optimized
996 during combine. */
997 align_rtx = virtual_preferred_stack_boundary_rtx;
998 alignm1_rtx = force_operand (plus_constant (Pmode, align_rtx, -1),
999 NULL_RTX);
1002 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1003 but we know it can't. So add ourselves and then do
1004 TRUNC_DIV_EXPR. */
1005 size = expand_binop (Pmode, add_optab, size, alignm1_rtx,
1006 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1007 size = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, size, align_rtx,
1008 NULL_RTX, 1);
1009 size = expand_mult (Pmode, size, align_rtx, NULL_RTX, 1);
1011 return size;
1014 /* Save the stack pointer for the purpose in SAVE_LEVEL. PSAVE is a pointer
1015 to a previously-created save area. If no save area has been allocated,
1016 this function will allocate one. If a save area is specified, it
1017 must be of the proper mode. */
1019 void
1020 emit_stack_save (enum save_level save_level, rtx *psave)
1022 rtx sa = *psave;
1023 /* The default is that we use a move insn and save in a Pmode object. */
1024 rtx (*fcn) (rtx, rtx) = gen_move_insn;
1025 enum machine_mode mode = STACK_SAVEAREA_MODE (save_level);
1027 /* See if this machine has anything special to do for this kind of save. */
1028 switch (save_level)
1030 #ifdef HAVE_save_stack_block
1031 case SAVE_BLOCK:
1032 if (HAVE_save_stack_block)
1033 fcn = gen_save_stack_block;
1034 break;
1035 #endif
1036 #ifdef HAVE_save_stack_function
1037 case SAVE_FUNCTION:
1038 if (HAVE_save_stack_function)
1039 fcn = gen_save_stack_function;
1040 break;
1041 #endif
1042 #ifdef HAVE_save_stack_nonlocal
1043 case SAVE_NONLOCAL:
1044 if (HAVE_save_stack_nonlocal)
1045 fcn = gen_save_stack_nonlocal;
1046 break;
1047 #endif
1048 default:
1049 break;
1052 /* If there is no save area and we have to allocate one, do so. Otherwise
1053 verify the save area is the proper mode. */
1055 if (sa == 0)
1057 if (mode != VOIDmode)
1059 if (save_level == SAVE_NONLOCAL)
1060 *psave = sa = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
1061 else
1062 *psave = sa = gen_reg_rtx (mode);
1066 do_pending_stack_adjust ();
1067 if (sa != 0)
1068 sa = validize_mem (sa);
1069 emit_insn (fcn (sa, stack_pointer_rtx));
1072 /* Restore the stack pointer for the purpose in SAVE_LEVEL. SA is the save
1073 area made by emit_stack_save. If it is zero, we have nothing to do. */
1075 void
1076 emit_stack_restore (enum save_level save_level, rtx sa)
1078 /* The default is that we use a move insn. */
1079 rtx (*fcn) (rtx, rtx) = gen_move_insn;
1081 /* If stack_realign_drap, the x86 backend emits a prologue that aligns both
1082 STACK_POINTER and HARD_FRAME_POINTER.
1083 If stack_realign_fp, the x86 backend emits a prologue that aligns only
1084 STACK_POINTER. This renders the HARD_FRAME_POINTER unusable for accessing
1085 aligned variables, which is reflected in ix86_can_eliminate.
1086 We normally still have the realigned STACK_POINTER that we can use.
1087 But if there is a stack restore still present at reload, it can trigger
1088 mark_not_eliminable for the STACK_POINTER, leaving no way to eliminate
1089 FRAME_POINTER into a hard reg.
1090 To prevent this situation, we force need_drap if we emit a stack
1091 restore. */
1092 if (SUPPORTS_STACK_ALIGNMENT)
1093 crtl->need_drap = true;
1095 /* See if this machine has anything special to do for this kind of save. */
1096 switch (save_level)
1098 #ifdef HAVE_restore_stack_block
1099 case SAVE_BLOCK:
1100 if (HAVE_restore_stack_block)
1101 fcn = gen_restore_stack_block;
1102 break;
1103 #endif
1104 #ifdef HAVE_restore_stack_function
1105 case SAVE_FUNCTION:
1106 if (HAVE_restore_stack_function)
1107 fcn = gen_restore_stack_function;
1108 break;
1109 #endif
1110 #ifdef HAVE_restore_stack_nonlocal
1111 case SAVE_NONLOCAL:
1112 if (HAVE_restore_stack_nonlocal)
1113 fcn = gen_restore_stack_nonlocal;
1114 break;
1115 #endif
1116 default:
1117 break;
1120 if (sa != 0)
1122 sa = validize_mem (sa);
1123 /* These clobbers prevent the scheduler from moving
1124 references to variable arrays below the code
1125 that deletes (pops) the arrays. */
1126 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1127 emit_clobber (gen_rtx_MEM (BLKmode, stack_pointer_rtx));
1130 discard_pending_stack_adjust ();
1132 emit_insn (fcn (stack_pointer_rtx, sa));
1135 /* Invoke emit_stack_save on the nonlocal_goto_save_area for the current
1136 function. This function should be called whenever we allocate or
1137 deallocate dynamic stack space. */
1139 void
1140 update_nonlocal_goto_save_area (void)
1142 tree t_save;
1143 rtx r_save;
1145 /* The nonlocal_goto_save_area object is an array of N pointers. The
1146 first one is used for the frame pointer save; the rest are sized by
1147 STACK_SAVEAREA_MODE. Create a reference to array index 1, the first
1148 of the stack save area slots. */
1149 t_save = build4 (ARRAY_REF,
1150 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
1151 cfun->nonlocal_goto_save_area,
1152 integer_one_node, NULL_TREE, NULL_TREE);
1153 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
1155 emit_stack_save (SAVE_NONLOCAL, &r_save);
1158 /* Return an rtx representing the address of an area of memory dynamically
1159 pushed on the stack.
1161 Any required stack pointer alignment is preserved.
1163 SIZE is an rtx representing the size of the area.
1165 SIZE_ALIGN is the alignment (in bits) that we know SIZE has. This
1166 parameter may be zero. If so, a proper value will be extracted
1167 from SIZE if it is constant, otherwise BITS_PER_UNIT will be assumed.
1169 REQUIRED_ALIGN is the alignment (in bits) required for the region
1170 of memory.
1172 If CANNOT_ACCUMULATE is set to TRUE, the caller guarantees that the
1173 stack space allocated by the generated code cannot be added with itself
1174 in the course of the execution of the function. It is always safe to
1175 pass FALSE here and the following criterion is sufficient in order to
1176 pass TRUE: every path in the CFG that starts at the allocation point and
1177 loops to it executes the associated deallocation code. */
1180 allocate_dynamic_stack_space (rtx size, unsigned size_align,
1181 unsigned required_align, bool cannot_accumulate)
1183 HOST_WIDE_INT stack_usage_size = -1;
1184 rtx final_label, final_target, target;
1185 unsigned extra_align = 0;
1186 bool must_align;
1188 /* If we're asking for zero bytes, it doesn't matter what we point
1189 to since we can't dereference it. But return a reasonable
1190 address anyway. */
1191 if (size == const0_rtx)
1192 return virtual_stack_dynamic_rtx;
1194 /* Otherwise, show we're calling alloca or equivalent. */
1195 cfun->calls_alloca = 1;
1197 /* If stack usage info is requested, look into the size we are passed.
1198 We need to do so this early to avoid the obfuscation that may be
1199 introduced later by the various alignment operations. */
1200 if (flag_stack_usage_info)
1202 if (CONST_INT_P (size))
1203 stack_usage_size = INTVAL (size);
1204 else if (REG_P (size))
1206 /* Look into the last emitted insn and see if we can deduce
1207 something for the register. */
1208 rtx insn, set, note;
1209 insn = get_last_insn ();
1210 if ((set = single_set (insn)) && rtx_equal_p (SET_DEST (set), size))
1212 if (CONST_INT_P (SET_SRC (set)))
1213 stack_usage_size = INTVAL (SET_SRC (set));
1214 else if ((note = find_reg_equal_equiv_note (insn))
1215 && CONST_INT_P (XEXP (note, 0)))
1216 stack_usage_size = INTVAL (XEXP (note, 0));
1220 /* If the size is not constant, we can't say anything. */
1221 if (stack_usage_size == -1)
1223 current_function_has_unbounded_dynamic_stack_size = 1;
1224 stack_usage_size = 0;
1228 /* Ensure the size is in the proper mode. */
1229 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1230 size = convert_to_mode (Pmode, size, 1);
1232 /* Adjust SIZE_ALIGN, if needed. */
1233 if (CONST_INT_P (size))
1235 unsigned HOST_WIDE_INT lsb;
1237 lsb = INTVAL (size);
1238 lsb &= -lsb;
1240 /* Watch out for overflow truncating to "unsigned". */
1241 if (lsb > UINT_MAX / BITS_PER_UNIT)
1242 size_align = 1u << (HOST_BITS_PER_INT - 1);
1243 else
1244 size_align = (unsigned)lsb * BITS_PER_UNIT;
1246 else if (size_align < BITS_PER_UNIT)
1247 size_align = BITS_PER_UNIT;
1249 /* We can't attempt to minimize alignment necessary, because we don't
1250 know the final value of preferred_stack_boundary yet while executing
1251 this code. */
1252 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
1253 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
1255 /* We will need to ensure that the address we return is aligned to
1256 REQUIRED_ALIGN. If STACK_DYNAMIC_OFFSET is defined, we don't
1257 always know its final value at this point in the compilation (it
1258 might depend on the size of the outgoing parameter lists, for
1259 example), so we must align the value to be returned in that case.
1260 (Note that STACK_DYNAMIC_OFFSET will have a default nonzero value if
1261 STACK_POINTER_OFFSET or ACCUMULATE_OUTGOING_ARGS are defined).
1262 We must also do an alignment operation on the returned value if
1263 the stack pointer alignment is less strict than REQUIRED_ALIGN.
1265 If we have to align, we must leave space in SIZE for the hole
1266 that might result from the alignment operation. */
1268 must_align = (crtl->preferred_stack_boundary < required_align);
1269 if (must_align)
1271 if (required_align > PREFERRED_STACK_BOUNDARY)
1272 extra_align = PREFERRED_STACK_BOUNDARY;
1273 else if (required_align > STACK_BOUNDARY)
1274 extra_align = STACK_BOUNDARY;
1275 else
1276 extra_align = BITS_PER_UNIT;
1279 /* ??? STACK_POINTER_OFFSET is always defined now. */
1280 #if defined (STACK_DYNAMIC_OFFSET) || defined (STACK_POINTER_OFFSET)
1281 must_align = true;
1282 extra_align = BITS_PER_UNIT;
1283 #endif
1285 if (must_align)
1287 unsigned extra = (required_align - extra_align) / BITS_PER_UNIT;
1289 size = plus_constant (Pmode, size, extra);
1290 size = force_operand (size, NULL_RTX);
1292 if (flag_stack_usage_info)
1293 stack_usage_size += extra;
1295 if (extra && size_align > extra_align)
1296 size_align = extra_align;
1299 /* Round the size to a multiple of the required stack alignment.
1300 Since the stack if presumed to be rounded before this allocation,
1301 this will maintain the required alignment.
1303 If the stack grows downward, we could save an insn by subtracting
1304 SIZE from the stack pointer and then aligning the stack pointer.
1305 The problem with this is that the stack pointer may be unaligned
1306 between the execution of the subtraction and alignment insns and
1307 some machines do not allow this. Even on those that do, some
1308 signal handlers malfunction if a signal should occur between those
1309 insns. Since this is an extremely rare event, we have no reliable
1310 way of knowing which systems have this problem. So we avoid even
1311 momentarily mis-aligning the stack. */
1312 if (size_align % MAX_SUPPORTED_STACK_ALIGNMENT != 0)
1314 size = round_push (size);
1316 if (flag_stack_usage_info)
1318 int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
1319 stack_usage_size = (stack_usage_size + align - 1) / align * align;
1323 target = gen_reg_rtx (Pmode);
1325 /* The size is supposed to be fully adjusted at this point so record it
1326 if stack usage info is requested. */
1327 if (flag_stack_usage_info)
1329 current_function_dynamic_stack_size += stack_usage_size;
1331 /* ??? This is gross but the only safe stance in the absence
1332 of stack usage oriented flow analysis. */
1333 if (!cannot_accumulate)
1334 current_function_has_unbounded_dynamic_stack_size = 1;
1337 final_label = NULL_RTX;
1338 final_target = NULL_RTX;
1340 /* If we are splitting the stack, we need to ask the backend whether
1341 there is enough room on the current stack. If there isn't, or if
1342 the backend doesn't know how to tell is, then we need to call a
1343 function to allocate memory in some other way. This memory will
1344 be released when we release the current stack segment. The
1345 effect is that stack allocation becomes less efficient, but at
1346 least it doesn't cause a stack overflow. */
1347 if (flag_split_stack)
1349 rtx available_label, ask, space, func;
1351 available_label = NULL_RTX;
1353 #ifdef HAVE_split_stack_space_check
1354 if (HAVE_split_stack_space_check)
1356 available_label = gen_label_rtx ();
1358 /* This instruction will branch to AVAILABLE_LABEL if there
1359 are SIZE bytes available on the stack. */
1360 emit_insn (gen_split_stack_space_check (size, available_label));
1362 #endif
1364 /* The __morestack_allocate_stack_space function will allocate
1365 memory using malloc. If the alignment of the memory returned
1366 by malloc does not meet REQUIRED_ALIGN, we increase SIZE to
1367 make sure we allocate enough space. */
1368 if (MALLOC_ABI_ALIGNMENT >= required_align)
1369 ask = size;
1370 else
1372 ask = expand_binop (Pmode, add_optab, size,
1373 gen_int_mode (required_align / BITS_PER_UNIT - 1,
1374 Pmode),
1375 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1376 must_align = true;
1379 func = init_one_libfunc ("__morestack_allocate_stack_space");
1381 space = emit_library_call_value (func, target, LCT_NORMAL, Pmode,
1382 1, ask, Pmode);
1384 if (available_label == NULL_RTX)
1385 return space;
1387 final_target = gen_reg_rtx (Pmode);
1389 emit_move_insn (final_target, space);
1391 final_label = gen_label_rtx ();
1392 emit_jump (final_label);
1394 emit_label (available_label);
1397 do_pending_stack_adjust ();
1399 /* We ought to be called always on the toplevel and stack ought to be aligned
1400 properly. */
1401 gcc_assert (!(stack_pointer_delta
1402 % (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)));
1404 /* If needed, check that we have the required amount of stack. Take into
1405 account what has already been checked. */
1406 if (STACK_CHECK_MOVING_SP)
1408 else if (flag_stack_check == GENERIC_STACK_CHECK)
1409 probe_stack_range (STACK_OLD_CHECK_PROTECT + STACK_CHECK_MAX_FRAME_SIZE,
1410 size);
1411 else if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
1412 probe_stack_range (STACK_CHECK_PROTECT, size);
1414 /* Don't let anti_adjust_stack emit notes. */
1415 suppress_reg_args_size = true;
1417 /* Perform the required allocation from the stack. Some systems do
1418 this differently than simply incrementing/decrementing from the
1419 stack pointer, such as acquiring the space by calling malloc(). */
1420 #ifdef HAVE_allocate_stack
1421 if (HAVE_allocate_stack)
1423 struct expand_operand ops[2];
1424 /* We don't have to check against the predicate for operand 0 since
1425 TARGET is known to be a pseudo of the proper mode, which must
1426 be valid for the operand. */
1427 create_fixed_operand (&ops[0], target);
1428 create_convert_operand_to (&ops[1], size, STACK_SIZE_MODE, true);
1429 expand_insn (CODE_FOR_allocate_stack, 2, ops);
1431 else
1432 #endif
1434 int saved_stack_pointer_delta;
1436 #ifndef STACK_GROWS_DOWNWARD
1437 emit_move_insn (target, virtual_stack_dynamic_rtx);
1438 #endif
1440 /* Check stack bounds if necessary. */
1441 if (crtl->limit_stack)
1443 rtx available;
1444 rtx space_available = gen_label_rtx ();
1445 #ifdef STACK_GROWS_DOWNWARD
1446 available = expand_binop (Pmode, sub_optab,
1447 stack_pointer_rtx, stack_limit_rtx,
1448 NULL_RTX, 1, OPTAB_WIDEN);
1449 #else
1450 available = expand_binop (Pmode, sub_optab,
1451 stack_limit_rtx, stack_pointer_rtx,
1452 NULL_RTX, 1, OPTAB_WIDEN);
1453 #endif
1454 emit_cmp_and_jump_insns (available, size, GEU, NULL_RTX, Pmode, 1,
1455 space_available);
1456 #ifdef HAVE_trap
1457 if (HAVE_trap)
1458 emit_insn (gen_trap ());
1459 else
1460 #endif
1461 error ("stack limits not supported on this target");
1462 emit_barrier ();
1463 emit_label (space_available);
1466 saved_stack_pointer_delta = stack_pointer_delta;
1468 if (flag_stack_check && STACK_CHECK_MOVING_SP)
1469 anti_adjust_stack_and_probe (size, false);
1470 else
1471 anti_adjust_stack (size);
1473 /* Even if size is constant, don't modify stack_pointer_delta.
1474 The constant size alloca should preserve
1475 crtl->preferred_stack_boundary alignment. */
1476 stack_pointer_delta = saved_stack_pointer_delta;
1478 #ifdef STACK_GROWS_DOWNWARD
1479 emit_move_insn (target, virtual_stack_dynamic_rtx);
1480 #endif
1483 suppress_reg_args_size = false;
1485 /* Finish up the split stack handling. */
1486 if (final_label != NULL_RTX)
1488 gcc_assert (flag_split_stack);
1489 emit_move_insn (final_target, target);
1490 emit_label (final_label);
1491 target = final_target;
1494 if (must_align)
1496 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1497 but we know it can't. So add ourselves and then do
1498 TRUNC_DIV_EXPR. */
1499 target = expand_binop (Pmode, add_optab, target,
1500 gen_int_mode (required_align / BITS_PER_UNIT - 1,
1501 Pmode),
1502 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1503 target = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, target,
1504 gen_int_mode (required_align / BITS_PER_UNIT,
1505 Pmode),
1506 NULL_RTX, 1);
1507 target = expand_mult (Pmode, target,
1508 gen_int_mode (required_align / BITS_PER_UNIT,
1509 Pmode),
1510 NULL_RTX, 1);
1513 /* Now that we've committed to a return value, mark its alignment. */
1514 mark_reg_pointer (target, required_align);
1516 /* Record the new stack level for nonlocal gotos. */
1517 if (cfun->nonlocal_goto_save_area != 0)
1518 update_nonlocal_goto_save_area ();
1520 return target;
1523 /* A front end may want to override GCC's stack checking by providing a
1524 run-time routine to call to check the stack, so provide a mechanism for
1525 calling that routine. */
1527 static GTY(()) rtx stack_check_libfunc;
1529 void
1530 set_stack_check_libfunc (const char *libfunc_name)
1532 gcc_assert (stack_check_libfunc == NULL_RTX);
1533 stack_check_libfunc = gen_rtx_SYMBOL_REF (Pmode, libfunc_name);
1536 /* Emit one stack probe at ADDRESS, an address within the stack. */
1538 void
1539 emit_stack_probe (rtx address)
1541 #ifdef HAVE_probe_stack_address
1542 if (HAVE_probe_stack_address)
1543 emit_insn (gen_probe_stack_address (address));
1544 else
1545 #endif
1547 rtx memref = gen_rtx_MEM (word_mode, address);
1549 MEM_VOLATILE_P (memref) = 1;
1551 /* See if we have an insn to probe the stack. */
1552 #ifdef HAVE_probe_stack
1553 if (HAVE_probe_stack)
1554 emit_insn (gen_probe_stack (memref));
1555 else
1556 #endif
1557 emit_move_insn (memref, const0_rtx);
1561 /* Probe a range of stack addresses from FIRST to FIRST+SIZE, inclusive.
1562 FIRST is a constant and size is a Pmode RTX. These are offsets from
1563 the current stack pointer. STACK_GROWS_DOWNWARD says whether to add
1564 or subtract them from the stack pointer. */
1566 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
1568 #ifdef STACK_GROWS_DOWNWARD
1569 #define STACK_GROW_OP MINUS
1570 #define STACK_GROW_OPTAB sub_optab
1571 #define STACK_GROW_OFF(off) -(off)
1572 #else
1573 #define STACK_GROW_OP PLUS
1574 #define STACK_GROW_OPTAB add_optab
1575 #define STACK_GROW_OFF(off) (off)
1576 #endif
1578 void
1579 probe_stack_range (HOST_WIDE_INT first, rtx size)
1581 /* First ensure SIZE is Pmode. */
1582 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1583 size = convert_to_mode (Pmode, size, 1);
1585 /* Next see if we have a function to check the stack. */
1586 if (stack_check_libfunc)
1588 rtx addr = memory_address (Pmode,
1589 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1590 stack_pointer_rtx,
1591 plus_constant (Pmode,
1592 size, first)));
1593 emit_library_call (stack_check_libfunc, LCT_NORMAL, VOIDmode, 1, addr,
1594 Pmode);
1597 /* Next see if we have an insn to check the stack. */
1598 #ifdef HAVE_check_stack
1599 else if (HAVE_check_stack)
1601 struct expand_operand ops[1];
1602 rtx addr = memory_address (Pmode,
1603 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1604 stack_pointer_rtx,
1605 plus_constant (Pmode,
1606 size, first)));
1607 bool success;
1608 create_input_operand (&ops[0], addr, Pmode);
1609 success = maybe_expand_insn (CODE_FOR_check_stack, 1, ops);
1610 gcc_assert (success);
1612 #endif
1614 /* Otherwise we have to generate explicit probes. If we have a constant
1615 small number of them to generate, that's the easy case. */
1616 else if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
1618 HOST_WIDE_INT isize = INTVAL (size), i;
1619 rtx addr;
1621 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
1622 it exceeds SIZE. If only one probe is needed, this will not
1623 generate any code. Then probe at FIRST + SIZE. */
1624 for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
1626 addr = memory_address (Pmode,
1627 plus_constant (Pmode, stack_pointer_rtx,
1628 STACK_GROW_OFF (first + i)));
1629 emit_stack_probe (addr);
1632 addr = memory_address (Pmode,
1633 plus_constant (Pmode, stack_pointer_rtx,
1634 STACK_GROW_OFF (first + isize)));
1635 emit_stack_probe (addr);
1638 /* In the variable case, do the same as above, but in a loop. Note that we
1639 must be extra careful with variables wrapping around because we might be
1640 at the very top (or the very bottom) of the address space and we have to
1641 be able to handle this case properly; in particular, we use an equality
1642 test for the loop condition. */
1643 else
1645 rtx rounded_size, rounded_size_op, test_addr, last_addr, temp;
1646 rtx loop_lab = gen_label_rtx ();
1647 rtx end_lab = gen_label_rtx ();
1650 /* Step 1: round SIZE to the previous multiple of the interval. */
1652 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1653 rounded_size
1654 = simplify_gen_binary (AND, Pmode, size,
1655 gen_int_mode (-PROBE_INTERVAL, Pmode));
1656 rounded_size_op = force_operand (rounded_size, NULL_RTX);
1659 /* Step 2: compute initial and final value of the loop counter. */
1661 /* TEST_ADDR = SP + FIRST. */
1662 test_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1663 stack_pointer_rtx,
1664 gen_int_mode (first, Pmode)),
1665 NULL_RTX);
1667 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
1668 last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1669 test_addr,
1670 rounded_size_op), NULL_RTX);
1673 /* Step 3: the loop
1675 while (TEST_ADDR != LAST_ADDR)
1677 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
1678 probe at TEST_ADDR
1681 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
1682 until it is equal to ROUNDED_SIZE. */
1684 emit_label (loop_lab);
1686 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
1687 emit_cmp_and_jump_insns (test_addr, last_addr, EQ, NULL_RTX, Pmode, 1,
1688 end_lab);
1690 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
1691 temp = expand_binop (Pmode, STACK_GROW_OPTAB, test_addr,
1692 gen_int_mode (PROBE_INTERVAL, Pmode), test_addr,
1693 1, OPTAB_WIDEN);
1695 gcc_assert (temp == test_addr);
1697 /* Probe at TEST_ADDR. */
1698 emit_stack_probe (test_addr);
1700 emit_jump (loop_lab);
1702 emit_label (end_lab);
1705 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
1706 that SIZE is equal to ROUNDED_SIZE. */
1708 /* TEMP = SIZE - ROUNDED_SIZE. */
1709 temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
1710 if (temp != const0_rtx)
1712 rtx addr;
1714 if (CONST_INT_P (temp))
1716 /* Use [base + disp} addressing mode if supported. */
1717 HOST_WIDE_INT offset = INTVAL (temp);
1718 addr = memory_address (Pmode,
1719 plus_constant (Pmode, last_addr,
1720 STACK_GROW_OFF (offset)));
1722 else
1724 /* Manual CSE if the difference is not known at compile-time. */
1725 temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
1726 addr = memory_address (Pmode,
1727 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1728 last_addr, temp));
1731 emit_stack_probe (addr);
1735 /* Make sure nothing is scheduled before we are done. */
1736 emit_insn (gen_blockage ());
1739 /* Adjust the stack pointer by minus SIZE (an rtx for a number of bytes)
1740 while probing it. This pushes when SIZE is positive. SIZE need not
1741 be constant. If ADJUST_BACK is true, adjust back the stack pointer
1742 by plus SIZE at the end. */
1744 void
1745 anti_adjust_stack_and_probe (rtx size, bool adjust_back)
1747 /* We skip the probe for the first interval + a small dope of 4 words and
1748 probe that many bytes past the specified size to maintain a protection
1749 area at the botton of the stack. */
1750 const int dope = 4 * UNITS_PER_WORD;
1752 /* First ensure SIZE is Pmode. */
1753 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1754 size = convert_to_mode (Pmode, size, 1);
1756 /* If we have a constant small number of probes to generate, that's the
1757 easy case. */
1758 if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
1760 HOST_WIDE_INT isize = INTVAL (size), i;
1761 bool first_probe = true;
1763 /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
1764 values of N from 1 until it exceeds SIZE. If only one probe is
1765 needed, this will not generate any code. Then adjust and probe
1766 to PROBE_INTERVAL + SIZE. */
1767 for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
1769 if (first_probe)
1771 anti_adjust_stack (GEN_INT (2 * PROBE_INTERVAL + dope));
1772 first_probe = false;
1774 else
1775 anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
1776 emit_stack_probe (stack_pointer_rtx);
1779 if (first_probe)
1780 anti_adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL + dope));
1781 else
1782 anti_adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL - i));
1783 emit_stack_probe (stack_pointer_rtx);
1786 /* In the variable case, do the same as above, but in a loop. Note that we
1787 must be extra careful with variables wrapping around because we might be
1788 at the very top (or the very bottom) of the address space and we have to
1789 be able to handle this case properly; in particular, we use an equality
1790 test for the loop condition. */
1791 else
1793 rtx rounded_size, rounded_size_op, last_addr, temp;
1794 rtx loop_lab = gen_label_rtx ();
1795 rtx end_lab = gen_label_rtx ();
1798 /* Step 1: round SIZE to the previous multiple of the interval. */
1800 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1801 rounded_size
1802 = simplify_gen_binary (AND, Pmode, size,
1803 gen_int_mode (-PROBE_INTERVAL, Pmode));
1804 rounded_size_op = force_operand (rounded_size, NULL_RTX);
1807 /* Step 2: compute initial and final value of the loop counter. */
1809 /* SP = SP_0 + PROBE_INTERVAL. */
1810 anti_adjust_stack (GEN_INT (PROBE_INTERVAL + dope));
1812 /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE. */
1813 last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1814 stack_pointer_rtx,
1815 rounded_size_op), NULL_RTX);
1818 /* Step 3: the loop
1820 while (SP != LAST_ADDR)
1822 SP = SP + PROBE_INTERVAL
1823 probe at SP
1826 adjusts SP and probes at PROBE_INTERVAL + N * PROBE_INTERVAL for
1827 values of N from 1 until it is equal to ROUNDED_SIZE. */
1829 emit_label (loop_lab);
1831 /* Jump to END_LAB if SP == LAST_ADDR. */
1832 emit_cmp_and_jump_insns (stack_pointer_rtx, last_addr, EQ, NULL_RTX,
1833 Pmode, 1, end_lab);
1835 /* SP = SP + PROBE_INTERVAL and probe at SP. */
1836 anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
1837 emit_stack_probe (stack_pointer_rtx);
1839 emit_jump (loop_lab);
1841 emit_label (end_lab);
1844 /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
1845 assert at compile-time that SIZE is equal to ROUNDED_SIZE. */
1847 /* TEMP = SIZE - ROUNDED_SIZE. */
1848 temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
1849 if (temp != const0_rtx)
1851 /* Manual CSE if the difference is not known at compile-time. */
1852 if (GET_CODE (temp) != CONST_INT)
1853 temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
1854 anti_adjust_stack (temp);
1855 emit_stack_probe (stack_pointer_rtx);
1859 /* Adjust back and account for the additional first interval. */
1860 if (adjust_back)
1861 adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL + dope));
1862 else
1863 adjust_stack (GEN_INT (PROBE_INTERVAL + dope));
1866 /* Return an rtx representing the register or memory location
1867 in which a scalar value of data type VALTYPE
1868 was returned by a function call to function FUNC.
1869 FUNC is a FUNCTION_DECL, FNTYPE a FUNCTION_TYPE node if the precise
1870 function is known, otherwise 0.
1871 OUTGOING is 1 if on a machine with register windows this function
1872 should return the register in which the function will put its result
1873 and 0 otherwise. */
1876 hard_function_value (const_tree valtype, const_tree func, const_tree fntype,
1877 int outgoing ATTRIBUTE_UNUSED)
1879 rtx val;
1881 val = targetm.calls.function_value (valtype, func ? func : fntype, outgoing);
1883 if (REG_P (val)
1884 && GET_MODE (val) == BLKmode)
1886 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (valtype);
1887 enum machine_mode tmpmode;
1889 /* int_size_in_bytes can return -1. We don't need a check here
1890 since the value of bytes will then be large enough that no
1891 mode will match anyway. */
1893 for (tmpmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1894 tmpmode != VOIDmode;
1895 tmpmode = GET_MODE_WIDER_MODE (tmpmode))
1897 /* Have we found a large enough mode? */
1898 if (GET_MODE_SIZE (tmpmode) >= bytes)
1899 break;
1902 /* No suitable mode found. */
1903 gcc_assert (tmpmode != VOIDmode);
1905 PUT_MODE (val, tmpmode);
1907 return val;
1910 /* Return an rtx representing the register or memory location
1911 in which a scalar value of mode MODE was returned by a library call. */
1914 hard_libcall_value (enum machine_mode mode, rtx fun)
1916 return targetm.calls.libcall_value (mode, fun);
1919 /* Look up the tree code for a given rtx code
1920 to provide the arithmetic operation for REAL_ARITHMETIC.
1921 The function returns an int because the caller may not know
1922 what `enum tree_code' means. */
1925 rtx_to_tree_code (enum rtx_code code)
1927 enum tree_code tcode;
1929 switch (code)
1931 case PLUS:
1932 tcode = PLUS_EXPR;
1933 break;
1934 case MINUS:
1935 tcode = MINUS_EXPR;
1936 break;
1937 case MULT:
1938 tcode = MULT_EXPR;
1939 break;
1940 case DIV:
1941 tcode = RDIV_EXPR;
1942 break;
1943 case SMIN:
1944 tcode = MIN_EXPR;
1945 break;
1946 case SMAX:
1947 tcode = MAX_EXPR;
1948 break;
1949 default:
1950 tcode = LAST_AND_UNUSED_TREE_CODE;
1951 break;
1953 return ((int) tcode);
1956 #include "gt-explow.h"