Merge trunk version 190524 into gupc branch.
[official-gcc.git] / gcc / explow.c
blob25b5a03bc9cf72c99e68db142c9aa93ce78a3e16
1 /* Subroutines for manipulating rtx's in semantically interesting ways.
2 Copyright (C) 1987, 1991, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
3 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "diagnostic-core.h"
28 #include "rtl.h"
29 #include "tree.h"
30 #include "tm_p.h"
31 #include "flags.h"
32 #include "except.h"
33 #include "function.h"
34 #include "expr.h"
35 #include "optabs.h"
36 #include "libfuncs.h"
37 #include "hard-reg-set.h"
38 #include "insn-config.h"
39 #include "ggc.h"
40 #include "recog.h"
41 #include "langhooks.h"
42 #include "target.h"
43 #include "common/common-target.h"
44 #include "output.h"
46 static rtx break_out_memory_refs (rtx);
49 /* Truncate and perhaps sign-extend C as appropriate for MODE. */
51 HOST_WIDE_INT
52 trunc_int_for_mode (HOST_WIDE_INT c, enum machine_mode mode)
54 int width = GET_MODE_PRECISION (mode);
56 /* You want to truncate to a _what_? */
57 gcc_assert (SCALAR_INT_MODE_P (mode));
59 /* Canonicalize BImode to 0 and STORE_FLAG_VALUE. */
60 if (mode == BImode)
61 return c & 1 ? STORE_FLAG_VALUE : 0;
63 /* Sign-extend for the requested mode. */
65 if (width < HOST_BITS_PER_WIDE_INT)
67 HOST_WIDE_INT sign = 1;
68 sign <<= width - 1;
69 c &= (sign << 1) - 1;
70 c ^= sign;
71 c -= sign;
74 return c;
77 /* Return an rtx for the sum of X and the integer C, given that X has
78 mode MODE. */
80 rtx
81 plus_constant (enum machine_mode mode, rtx x, HOST_WIDE_INT c)
83 RTX_CODE code;
84 rtx y;
85 rtx tem;
86 int all_constant = 0;
88 gcc_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
90 if (c == 0)
91 return x;
93 restart:
95 code = GET_CODE (x);
96 y = x;
98 switch (code)
100 case CONST_INT:
101 if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
103 unsigned HOST_WIDE_INT l1 = INTVAL (x);
104 HOST_WIDE_INT h1 = (l1 >> (HOST_BITS_PER_WIDE_INT - 1)) ? -1 : 0;
105 unsigned HOST_WIDE_INT l2 = c;
106 HOST_WIDE_INT h2 = c < 0 ? -1 : 0;
107 unsigned HOST_WIDE_INT lv;
108 HOST_WIDE_INT hv;
110 if (add_double_with_sign (l1, h1, l2, h2, &lv, &hv, false))
111 gcc_unreachable ();
113 return immed_double_const (lv, hv, VOIDmode);
116 return GEN_INT (INTVAL (x) + c);
118 case CONST_DOUBLE:
120 unsigned HOST_WIDE_INT l1 = CONST_DOUBLE_LOW (x);
121 HOST_WIDE_INT h1 = CONST_DOUBLE_HIGH (x);
122 unsigned HOST_WIDE_INT l2 = c;
123 HOST_WIDE_INT h2 = c < 0 ? -1 : 0;
124 unsigned HOST_WIDE_INT lv;
125 HOST_WIDE_INT hv;
127 if (add_double_with_sign (l1, h1, l2, h2, &lv, &hv, false))
128 /* Sorry, we have no way to represent overflows this wide.
129 To fix, add constant support wider than CONST_DOUBLE. */
130 gcc_assert (GET_MODE_BITSIZE (mode) <= HOST_BITS_PER_DOUBLE_INT);
132 return immed_double_const (lv, hv, VOIDmode);
135 case MEM:
136 /* If this is a reference to the constant pool, try replacing it with
137 a reference to a new constant. If the resulting address isn't
138 valid, don't return it because we have no way to validize it. */
139 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
140 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
142 tem = plus_constant (mode, get_pool_constant (XEXP (x, 0)), c);
143 tem = force_const_mem (GET_MODE (x), tem);
144 if (memory_address_p (GET_MODE (tem), XEXP (tem, 0)))
145 return tem;
147 break;
149 case CONST:
150 /* If adding to something entirely constant, set a flag
151 so that we can add a CONST around the result. */
152 x = XEXP (x, 0);
153 all_constant = 1;
154 goto restart;
156 case SYMBOL_REF:
157 case LABEL_REF:
158 all_constant = 1;
159 break;
161 case PLUS:
162 /* The interesting case is adding the integer to a sum. Look
163 for constant term in the sum and combine with C. For an
164 integer constant term or a constant term that is not an
165 explicit integer, we combine or group them together anyway.
167 We may not immediately return from the recursive call here, lest
168 all_constant gets lost. */
170 if (CONSTANT_P (XEXP (x, 1)))
172 x = gen_rtx_PLUS (mode, XEXP (x, 0),
173 plus_constant (mode, XEXP (x, 1), c));
174 c = 0;
176 else if (find_constant_term_loc (&y))
178 /* We need to be careful since X may be shared and we can't
179 modify it in place. */
180 rtx copy = copy_rtx (x);
181 rtx *const_loc = find_constant_term_loc (&copy);
183 *const_loc = plus_constant (mode, *const_loc, c);
184 x = copy;
185 c = 0;
187 break;
189 default:
190 break;
193 if (c != 0)
194 x = gen_rtx_PLUS (mode, x, GEN_INT (c));
196 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
197 return x;
198 else if (all_constant)
199 return gen_rtx_CONST (mode, x);
200 else
201 return x;
204 /* If X is a sum, return a new sum like X but lacking any constant terms.
205 Add all the removed constant terms into *CONSTPTR.
206 X itself is not altered. The result != X if and only if
207 it is not isomorphic to X. */
210 eliminate_constant_term (rtx x, rtx *constptr)
212 rtx x0, x1;
213 rtx tem;
215 if (GET_CODE (x) != PLUS)
216 return x;
218 /* First handle constants appearing at this level explicitly. */
219 if (CONST_INT_P (XEXP (x, 1))
220 && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x), *constptr,
221 XEXP (x, 1)))
222 && CONST_INT_P (tem))
224 *constptr = tem;
225 return eliminate_constant_term (XEXP (x, 0), constptr);
228 tem = const0_rtx;
229 x0 = eliminate_constant_term (XEXP (x, 0), &tem);
230 x1 = eliminate_constant_term (XEXP (x, 1), &tem);
231 if ((x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
232 && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x),
233 *constptr, tem))
234 && CONST_INT_P (tem))
236 *constptr = tem;
237 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
240 return x;
243 /* Return an rtx for the size in bytes of the value of EXP. */
246 expr_size (tree exp)
248 tree size;
250 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
251 size = TREE_OPERAND (exp, 1);
252 else
254 size = tree_expr_size (exp);
255 gcc_assert (size);
256 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
259 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
262 /* Return a wide integer for the size in bytes of the value of EXP, or -1
263 if the size can vary or is larger than an integer. */
265 HOST_WIDE_INT
266 int_expr_size (tree exp)
268 tree size;
270 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
271 size = TREE_OPERAND (exp, 1);
272 else
274 size = tree_expr_size (exp);
275 gcc_assert (size);
278 if (size == 0 || !host_integerp (size, 0))
279 return -1;
281 return tree_low_cst (size, 0);
284 /* Return a copy of X in which all memory references
285 and all constants that involve symbol refs
286 have been replaced with new temporary registers.
287 Also emit code to load the memory locations and constants
288 into those registers.
290 If X contains no such constants or memory references,
291 X itself (not a copy) is returned.
293 If a constant is found in the address that is not a legitimate constant
294 in an insn, it is left alone in the hope that it might be valid in the
295 address.
297 X may contain no arithmetic except addition, subtraction and multiplication.
298 Values returned by expand_expr with 1 for sum_ok fit this constraint. */
300 static rtx
301 break_out_memory_refs (rtx x)
303 if (MEM_P (x)
304 || (CONSTANT_P (x) && CONSTANT_ADDRESS_P (x)
305 && GET_MODE (x) != VOIDmode))
306 x = force_reg (GET_MODE (x), x);
307 else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
308 || GET_CODE (x) == MULT)
310 rtx op0 = break_out_memory_refs (XEXP (x, 0));
311 rtx op1 = break_out_memory_refs (XEXP (x, 1));
313 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
314 x = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
317 return x;
320 /* Given X, a memory address in address space AS' pointer mode, convert it to
321 an address in the address space's address mode, or vice versa (TO_MODE says
322 which way). We take advantage of the fact that pointers are not allowed to
323 overflow by commuting arithmetic operations over conversions so that address
324 arithmetic insns can be used. */
327 convert_memory_address_addr_space (enum machine_mode to_mode ATTRIBUTE_UNUSED,
328 rtx x, addr_space_t as ATTRIBUTE_UNUSED)
330 #ifndef POINTERS_EXTEND_UNSIGNED
331 gcc_assert (GET_MODE (x) == to_mode || GET_MODE (x) == VOIDmode);
332 return x;
333 #else /* defined(POINTERS_EXTEND_UNSIGNED) */
334 enum machine_mode pointer_mode, address_mode, from_mode;
335 rtx temp;
336 enum rtx_code code;
338 /* If X already has the right mode, just return it. */
339 if (GET_MODE (x) == to_mode)
340 return x;
342 pointer_mode = targetm.addr_space.pointer_mode (as);
343 address_mode = targetm.addr_space.address_mode (as);
344 from_mode = to_mode == pointer_mode ? address_mode : pointer_mode;
346 /* Here we handle some special cases. If none of them apply, fall through
347 to the default case. */
348 switch (GET_CODE (x))
350 case CONST_INT:
351 case CONST_DOUBLE:
352 if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode))
353 code = TRUNCATE;
354 else if (POINTERS_EXTEND_UNSIGNED < 0)
355 break;
356 else if (POINTERS_EXTEND_UNSIGNED > 0)
357 code = ZERO_EXTEND;
358 else
359 code = SIGN_EXTEND;
360 temp = simplify_unary_operation (code, to_mode, x, from_mode);
361 if (temp)
362 return temp;
363 break;
365 case SUBREG:
366 if ((SUBREG_PROMOTED_VAR_P (x) || REG_POINTER (SUBREG_REG (x)))
367 && GET_MODE (SUBREG_REG (x)) == to_mode)
368 return SUBREG_REG (x);
369 break;
371 case LABEL_REF:
372 temp = gen_rtx_LABEL_REF (to_mode, XEXP (x, 0));
373 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
374 return temp;
375 break;
377 case SYMBOL_REF:
378 temp = shallow_copy_rtx (x);
379 PUT_MODE (temp, to_mode);
380 return temp;
381 break;
383 case CONST:
384 return gen_rtx_CONST (to_mode,
385 convert_memory_address_addr_space
386 (to_mode, XEXP (x, 0), as));
387 break;
389 case PLUS:
390 case MULT:
391 /* FIXME: For addition, we used to permute the conversion and
392 addition operation only if one operand is a constant and
393 converting the constant does not change it or if one operand
394 is a constant and we are using a ptr_extend instruction
395 (POINTERS_EXTEND_UNSIGNED < 0) even if the resulting address
396 may overflow/underflow. We relax the condition to include
397 zero-extend (POINTERS_EXTEND_UNSIGNED > 0) since the other
398 parts of the compiler depend on it. See PR 49721.
400 We can always safely permute them if we are making the address
401 narrower. */
402 if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode)
403 || (GET_CODE (x) == PLUS
404 && CONST_INT_P (XEXP (x, 1))
405 && (POINTERS_EXTEND_UNSIGNED != 0
406 || XEXP (x, 1) == convert_memory_address_addr_space
407 (to_mode, XEXP (x, 1), as))))
408 return gen_rtx_fmt_ee (GET_CODE (x), to_mode,
409 convert_memory_address_addr_space
410 (to_mode, XEXP (x, 0), as),
411 XEXP (x, 1));
412 break;
414 default:
415 break;
418 return convert_modes (to_mode, from_mode,
419 x, POINTERS_EXTEND_UNSIGNED);
420 #endif /* defined(POINTERS_EXTEND_UNSIGNED) */
423 /* Return something equivalent to X but valid as a memory address for something
424 of mode MODE in the named address space AS. When X is not itself valid,
425 this works by copying X or subexpressions of it into registers. */
428 memory_address_addr_space (enum machine_mode mode, rtx x, addr_space_t as)
430 rtx oldx = x;
431 enum machine_mode address_mode = targetm.addr_space.address_mode (as);
433 x = convert_memory_address_addr_space (address_mode, x, as);
435 /* By passing constant addresses through registers
436 we get a chance to cse them. */
437 if (! cse_not_expected && CONSTANT_P (x) && CONSTANT_ADDRESS_P (x))
438 x = force_reg (address_mode, x);
440 /* We get better cse by rejecting indirect addressing at this stage.
441 Let the combiner create indirect addresses where appropriate.
442 For now, generate the code so that the subexpressions useful to share
443 are visible. But not if cse won't be done! */
444 else
446 if (! cse_not_expected && !REG_P (x))
447 x = break_out_memory_refs (x);
449 /* At this point, any valid address is accepted. */
450 if (memory_address_addr_space_p (mode, x, as))
451 goto done;
453 /* If it was valid before but breaking out memory refs invalidated it,
454 use it the old way. */
455 if (memory_address_addr_space_p (mode, oldx, as))
457 x = oldx;
458 goto done;
461 /* Perform machine-dependent transformations on X
462 in certain cases. This is not necessary since the code
463 below can handle all possible cases, but machine-dependent
464 transformations can make better code. */
466 rtx orig_x = x;
467 x = targetm.addr_space.legitimize_address (x, oldx, mode, as);
468 if (orig_x != x && memory_address_addr_space_p (mode, x, as))
469 goto done;
472 /* PLUS and MULT can appear in special ways
473 as the result of attempts to make an address usable for indexing.
474 Usually they are dealt with by calling force_operand, below.
475 But a sum containing constant terms is special
476 if removing them makes the sum a valid address:
477 then we generate that address in a register
478 and index off of it. We do this because it often makes
479 shorter code, and because the addresses thus generated
480 in registers often become common subexpressions. */
481 if (GET_CODE (x) == PLUS)
483 rtx constant_term = const0_rtx;
484 rtx y = eliminate_constant_term (x, &constant_term);
485 if (constant_term == const0_rtx
486 || ! memory_address_addr_space_p (mode, y, as))
487 x = force_operand (x, NULL_RTX);
488 else
490 y = gen_rtx_PLUS (GET_MODE (x), copy_to_reg (y), constant_term);
491 if (! memory_address_addr_space_p (mode, y, as))
492 x = force_operand (x, NULL_RTX);
493 else
494 x = y;
498 else if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
499 x = force_operand (x, NULL_RTX);
501 /* If we have a register that's an invalid address,
502 it must be a hard reg of the wrong class. Copy it to a pseudo. */
503 else if (REG_P (x))
504 x = copy_to_reg (x);
506 /* Last resort: copy the value to a register, since
507 the register is a valid address. */
508 else
509 x = force_reg (address_mode, x);
512 done:
514 gcc_assert (memory_address_addr_space_p (mode, x, as));
515 /* If we didn't change the address, we are done. Otherwise, mark
516 a reg as a pointer if we have REG or REG + CONST_INT. */
517 if (oldx == x)
518 return x;
519 else if (REG_P (x))
520 mark_reg_pointer (x, BITS_PER_UNIT);
521 else if (GET_CODE (x) == PLUS
522 && REG_P (XEXP (x, 0))
523 && CONST_INT_P (XEXP (x, 1)))
524 mark_reg_pointer (XEXP (x, 0), BITS_PER_UNIT);
526 /* OLDX may have been the address on a temporary. Update the address
527 to indicate that X is now used. */
528 update_temp_slot_address (oldx, x);
530 return x;
533 /* Convert a mem ref into one with a valid memory address.
534 Pass through anything else unchanged. */
537 validize_mem (rtx ref)
539 if (!MEM_P (ref))
540 return ref;
541 ref = use_anchored_address (ref);
542 if (memory_address_addr_space_p (GET_MODE (ref), XEXP (ref, 0),
543 MEM_ADDR_SPACE (ref)))
544 return ref;
546 /* Don't alter REF itself, since that is probably a stack slot. */
547 return replace_equiv_address (ref, XEXP (ref, 0));
550 /* If X is a memory reference to a member of an object block, try rewriting
551 it to use an anchor instead. Return the new memory reference on success
552 and the old one on failure. */
555 use_anchored_address (rtx x)
557 rtx base;
558 HOST_WIDE_INT offset;
559 enum machine_mode mode;
561 if (!flag_section_anchors)
562 return x;
564 if (!MEM_P (x))
565 return x;
567 /* Split the address into a base and offset. */
568 base = XEXP (x, 0);
569 offset = 0;
570 if (GET_CODE (base) == CONST
571 && GET_CODE (XEXP (base, 0)) == PLUS
572 && CONST_INT_P (XEXP (XEXP (base, 0), 1)))
574 offset += INTVAL (XEXP (XEXP (base, 0), 1));
575 base = XEXP (XEXP (base, 0), 0);
578 /* Check whether BASE is suitable for anchors. */
579 if (GET_CODE (base) != SYMBOL_REF
580 || !SYMBOL_REF_HAS_BLOCK_INFO_P (base)
581 || SYMBOL_REF_ANCHOR_P (base)
582 || SYMBOL_REF_BLOCK (base) == NULL
583 || !targetm.use_anchors_for_symbol_p (base))
584 return x;
586 /* Decide where BASE is going to be. */
587 place_block_symbol (base);
589 /* Get the anchor we need to use. */
590 offset += SYMBOL_REF_BLOCK_OFFSET (base);
591 base = get_section_anchor (SYMBOL_REF_BLOCK (base), offset,
592 SYMBOL_REF_TLS_MODEL (base));
594 /* Work out the offset from the anchor. */
595 offset -= SYMBOL_REF_BLOCK_OFFSET (base);
597 /* If we're going to run a CSE pass, force the anchor into a register.
598 We will then be able to reuse registers for several accesses, if the
599 target costs say that that's worthwhile. */
600 mode = GET_MODE (base);
601 if (!cse_not_expected)
602 base = force_reg (mode, base);
604 return replace_equiv_address (x, plus_constant (mode, base, offset));
607 /* Copy the value or contents of X to a new temp reg and return that reg. */
610 copy_to_reg (rtx x)
612 rtx temp = gen_reg_rtx (GET_MODE (x));
614 /* If not an operand, must be an address with PLUS and MULT so
615 do the computation. */
616 if (! general_operand (x, VOIDmode))
617 x = force_operand (x, temp);
619 if (x != temp)
620 emit_move_insn (temp, x);
622 return temp;
625 /* Like copy_to_reg but always give the new register mode Pmode
626 in case X is a constant. */
629 copy_addr_to_reg (rtx x)
631 return copy_to_mode_reg (Pmode, x);
634 /* Like copy_to_reg but always give the new register mode MODE
635 in case X is a constant. */
638 copy_to_mode_reg (enum machine_mode mode, rtx x)
640 rtx temp = gen_reg_rtx (mode);
642 /* If not an operand, must be an address with PLUS and MULT so
643 do the computation. */
644 if (! general_operand (x, VOIDmode))
645 x = force_operand (x, temp);
647 gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
648 if (x != temp)
649 emit_move_insn (temp, x);
650 return temp;
653 /* Load X into a register if it is not already one.
654 Use mode MODE for the register.
655 X should be valid for mode MODE, but it may be a constant which
656 is valid for all integer modes; that's why caller must specify MODE.
658 The caller must not alter the value in the register we return,
659 since we mark it as a "constant" register. */
662 force_reg (enum machine_mode mode, rtx x)
664 rtx temp, insn, set;
666 if (REG_P (x))
667 return x;
669 if (general_operand (x, mode))
671 temp = gen_reg_rtx (mode);
672 insn = emit_move_insn (temp, x);
674 else
676 temp = force_operand (x, NULL_RTX);
677 if (REG_P (temp))
678 insn = get_last_insn ();
679 else
681 rtx temp2 = gen_reg_rtx (mode);
682 insn = emit_move_insn (temp2, temp);
683 temp = temp2;
687 /* Let optimizers know that TEMP's value never changes
688 and that X can be substituted for it. Don't get confused
689 if INSN set something else (such as a SUBREG of TEMP). */
690 if (CONSTANT_P (x)
691 && (set = single_set (insn)) != 0
692 && SET_DEST (set) == temp
693 && ! rtx_equal_p (x, SET_SRC (set)))
694 set_unique_reg_note (insn, REG_EQUAL, x);
696 /* Let optimizers know that TEMP is a pointer, and if so, the
697 known alignment of that pointer. */
699 unsigned align = 0;
700 if (GET_CODE (x) == SYMBOL_REF)
702 align = BITS_PER_UNIT;
703 if (SYMBOL_REF_DECL (x) && DECL_P (SYMBOL_REF_DECL (x)))
704 align = DECL_ALIGN (SYMBOL_REF_DECL (x));
706 else if (GET_CODE (x) == LABEL_REF)
707 align = BITS_PER_UNIT;
708 else if (GET_CODE (x) == CONST
709 && GET_CODE (XEXP (x, 0)) == PLUS
710 && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
711 && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
713 rtx s = XEXP (XEXP (x, 0), 0);
714 rtx c = XEXP (XEXP (x, 0), 1);
715 unsigned sa, ca;
717 sa = BITS_PER_UNIT;
718 if (SYMBOL_REF_DECL (s) && DECL_P (SYMBOL_REF_DECL (s)))
719 sa = DECL_ALIGN (SYMBOL_REF_DECL (s));
721 if (INTVAL (c) == 0)
722 align = sa;
723 else
725 ca = ctz_hwi (INTVAL (c)) * BITS_PER_UNIT;
726 align = MIN (sa, ca);
730 if (align || (MEM_P (x) && MEM_POINTER (x)))
731 mark_reg_pointer (temp, align);
734 return temp;
737 /* If X is a memory ref, copy its contents to a new temp reg and return
738 that reg. Otherwise, return X. */
741 force_not_mem (rtx x)
743 rtx temp;
745 if (!MEM_P (x) || GET_MODE (x) == BLKmode)
746 return x;
748 temp = gen_reg_rtx (GET_MODE (x));
750 if (MEM_POINTER (x))
751 REG_POINTER (temp) = 1;
753 emit_move_insn (temp, x);
754 return temp;
757 /* Copy X to TARGET (if it's nonzero and a reg)
758 or to a new temp reg and return that reg.
759 MODE is the mode to use for X in case it is a constant. */
762 copy_to_suggested_reg (rtx x, rtx target, enum machine_mode mode)
764 rtx temp;
766 if (target && REG_P (target))
767 temp = target;
768 else
769 temp = gen_reg_rtx (mode);
771 emit_move_insn (temp, x);
772 return temp;
775 /* Return the mode to use to pass or return a scalar of TYPE and MODE.
776 PUNSIGNEDP points to the signedness of the type and may be adjusted
777 to show what signedness to use on extension operations.
779 FOR_RETURN is nonzero if the caller is promoting the return value
780 of FNDECL, else it is for promoting args. */
782 enum machine_mode
783 promote_function_mode (const_tree type, enum machine_mode mode, int *punsignedp,
784 const_tree funtype, int for_return)
786 /* Called without a type node for a libcall. */
787 if (type == NULL_TREE)
789 if (INTEGRAL_MODE_P (mode))
790 return targetm.calls.promote_function_mode (NULL_TREE, mode,
791 punsignedp, funtype,
792 for_return);
793 else
794 return mode;
797 switch (TREE_CODE (type))
799 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
800 case REAL_TYPE: case OFFSET_TYPE: case FIXED_POINT_TYPE:
801 case POINTER_TYPE: case REFERENCE_TYPE:
802 return targetm.calls.promote_function_mode (type, mode, punsignedp, funtype,
803 for_return);
805 default:
806 return mode;
809 /* Return the mode to use to store a scalar of TYPE and MODE.
810 PUNSIGNEDP points to the signedness of the type and may be adjusted
811 to show what signedness to use on extension operations. */
813 enum machine_mode
814 promote_mode (const_tree type ATTRIBUTE_UNUSED, enum machine_mode mode,
815 int *punsignedp ATTRIBUTE_UNUSED)
817 #ifdef PROMOTE_MODE
818 enum tree_code code;
819 int unsignedp;
820 #endif
822 /* For libcalls this is invoked without TYPE from the backends
823 TARGET_PROMOTE_FUNCTION_MODE hooks. Don't do anything in that
824 case. */
825 if (type == NULL_TREE)
826 return mode;
828 /* FIXME: this is the same logic that was there until GCC 4.4, but we
829 probably want to test POINTERS_EXTEND_UNSIGNED even if PROMOTE_MODE
830 is not defined. The affected targets are M32C, S390, SPARC. */
831 #ifdef PROMOTE_MODE
832 code = TREE_CODE (type);
833 unsignedp = *punsignedp;
835 switch (code)
837 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
838 case REAL_TYPE: case OFFSET_TYPE: case FIXED_POINT_TYPE:
839 PROMOTE_MODE (mode, unsignedp, type);
840 *punsignedp = unsignedp;
841 return mode;
842 break;
844 #ifdef POINTERS_EXTEND_UNSIGNED
845 case REFERENCE_TYPE:
846 case POINTER_TYPE:
847 *punsignedp = POINTERS_EXTEND_UNSIGNED;
848 if (upc_shared_type_p (TREE_TYPE (type)))
849 return TYPE_MODE (upc_pts_type_node);
850 return targetm.addr_space.address_mode
851 (TYPE_ADDR_SPACE (TREE_TYPE (type)));
852 break;
853 #endif
855 default:
856 return mode;
858 #else
859 return mode;
860 #endif
864 /* Use one of promote_mode or promote_function_mode to find the promoted
865 mode of DECL. If PUNSIGNEDP is not NULL, store there the unsignedness
866 of DECL after promotion. */
868 enum machine_mode
869 promote_decl_mode (const_tree decl, int *punsignedp)
871 tree type = TREE_TYPE (decl);
872 int unsignedp = TYPE_UNSIGNED (type);
873 enum machine_mode mode = DECL_MODE (decl);
874 enum machine_mode pmode;
876 if (TREE_CODE (decl) == RESULT_DECL
877 || TREE_CODE (decl) == PARM_DECL)
878 pmode = promote_function_mode (type, mode, &unsignedp,
879 TREE_TYPE (current_function_decl), 2);
880 else
881 pmode = promote_mode (type, mode, &unsignedp);
883 if (punsignedp)
884 *punsignedp = unsignedp;
885 return pmode;
889 /* Controls the behaviour of {anti_,}adjust_stack. */
890 static bool suppress_reg_args_size;
892 /* A helper for adjust_stack and anti_adjust_stack. */
894 static void
895 adjust_stack_1 (rtx adjust, bool anti_p)
897 rtx temp, insn;
899 #ifndef STACK_GROWS_DOWNWARD
900 /* Hereafter anti_p means subtract_p. */
901 anti_p = !anti_p;
902 #endif
904 temp = expand_binop (Pmode,
905 anti_p ? sub_optab : add_optab,
906 stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
907 OPTAB_LIB_WIDEN);
909 if (temp != stack_pointer_rtx)
910 insn = emit_move_insn (stack_pointer_rtx, temp);
911 else
913 insn = get_last_insn ();
914 temp = single_set (insn);
915 gcc_assert (temp != NULL && SET_DEST (temp) == stack_pointer_rtx);
918 if (!suppress_reg_args_size)
919 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
922 /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
923 This pops when ADJUST is positive. ADJUST need not be constant. */
925 void
926 adjust_stack (rtx adjust)
928 if (adjust == const0_rtx)
929 return;
931 /* We expect all variable sized adjustments to be multiple of
932 PREFERRED_STACK_BOUNDARY. */
933 if (CONST_INT_P (adjust))
934 stack_pointer_delta -= INTVAL (adjust);
936 adjust_stack_1 (adjust, false);
939 /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
940 This pushes when ADJUST is positive. ADJUST need not be constant. */
942 void
943 anti_adjust_stack (rtx adjust)
945 if (adjust == const0_rtx)
946 return;
948 /* We expect all variable sized adjustments to be multiple of
949 PREFERRED_STACK_BOUNDARY. */
950 if (CONST_INT_P (adjust))
951 stack_pointer_delta += INTVAL (adjust);
953 adjust_stack_1 (adjust, true);
956 /* Round the size of a block to be pushed up to the boundary required
957 by this machine. SIZE is the desired size, which need not be constant. */
959 static rtx
960 round_push (rtx size)
962 rtx align_rtx, alignm1_rtx;
964 if (!SUPPORTS_STACK_ALIGNMENT
965 || crtl->preferred_stack_boundary == MAX_SUPPORTED_STACK_ALIGNMENT)
967 int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
969 if (align == 1)
970 return size;
972 if (CONST_INT_P (size))
974 HOST_WIDE_INT new_size = (INTVAL (size) + align - 1) / align * align;
976 if (INTVAL (size) != new_size)
977 size = GEN_INT (new_size);
978 return size;
981 align_rtx = GEN_INT (align);
982 alignm1_rtx = GEN_INT (align - 1);
984 else
986 /* If crtl->preferred_stack_boundary might still grow, use
987 virtual_preferred_stack_boundary_rtx instead. This will be
988 substituted by the right value in vregs pass and optimized
989 during combine. */
990 align_rtx = virtual_preferred_stack_boundary_rtx;
991 alignm1_rtx = force_operand (plus_constant (Pmode, align_rtx, -1),
992 NULL_RTX);
995 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
996 but we know it can't. So add ourselves and then do
997 TRUNC_DIV_EXPR. */
998 size = expand_binop (Pmode, add_optab, size, alignm1_rtx,
999 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1000 size = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, size, align_rtx,
1001 NULL_RTX, 1);
1002 size = expand_mult (Pmode, size, align_rtx, NULL_RTX, 1);
1004 return size;
1007 /* Save the stack pointer for the purpose in SAVE_LEVEL. PSAVE is a pointer
1008 to a previously-created save area. If no save area has been allocated,
1009 this function will allocate one. If a save area is specified, it
1010 must be of the proper mode. */
1012 void
1013 emit_stack_save (enum save_level save_level, rtx *psave)
1015 rtx sa = *psave;
1016 /* The default is that we use a move insn and save in a Pmode object. */
1017 rtx (*fcn) (rtx, rtx) = gen_move_insn;
1018 enum machine_mode mode = STACK_SAVEAREA_MODE (save_level);
1020 /* See if this machine has anything special to do for this kind of save. */
1021 switch (save_level)
1023 #ifdef HAVE_save_stack_block
1024 case SAVE_BLOCK:
1025 if (HAVE_save_stack_block)
1026 fcn = gen_save_stack_block;
1027 break;
1028 #endif
1029 #ifdef HAVE_save_stack_function
1030 case SAVE_FUNCTION:
1031 if (HAVE_save_stack_function)
1032 fcn = gen_save_stack_function;
1033 break;
1034 #endif
1035 #ifdef HAVE_save_stack_nonlocal
1036 case SAVE_NONLOCAL:
1037 if (HAVE_save_stack_nonlocal)
1038 fcn = gen_save_stack_nonlocal;
1039 break;
1040 #endif
1041 default:
1042 break;
1045 /* If there is no save area and we have to allocate one, do so. Otherwise
1046 verify the save area is the proper mode. */
1048 if (sa == 0)
1050 if (mode != VOIDmode)
1052 if (save_level == SAVE_NONLOCAL)
1053 *psave = sa = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
1054 else
1055 *psave = sa = gen_reg_rtx (mode);
1059 do_pending_stack_adjust ();
1060 if (sa != 0)
1061 sa = validize_mem (sa);
1062 emit_insn (fcn (sa, stack_pointer_rtx));
1065 /* Restore the stack pointer for the purpose in SAVE_LEVEL. SA is the save
1066 area made by emit_stack_save. If it is zero, we have nothing to do. */
1068 void
1069 emit_stack_restore (enum save_level save_level, rtx sa)
1071 /* The default is that we use a move insn. */
1072 rtx (*fcn) (rtx, rtx) = gen_move_insn;
1074 /* If stack_realign_drap, the x86 backend emits a prologue that aligns both
1075 STACK_POINTER and HARD_FRAME_POINTER.
1076 If stack_realign_fp, the x86 backend emits a prologue that aligns only
1077 STACK_POINTER. This renders the HARD_FRAME_POINTER unusable for accessing
1078 aligned variables, which is reflected in ix86_can_eliminate.
1079 We normally still have the realigned STACK_POINTER that we can use.
1080 But if there is a stack restore still present at reload, it can trigger
1081 mark_not_eliminable for the STACK_POINTER, leaving no way to eliminate
1082 FRAME_POINTER into a hard reg.
1083 To prevent this situation, we force need_drap if we emit a stack
1084 restore. */
1085 if (SUPPORTS_STACK_ALIGNMENT)
1086 crtl->need_drap = true;
1088 /* See if this machine has anything special to do for this kind of save. */
1089 switch (save_level)
1091 #ifdef HAVE_restore_stack_block
1092 case SAVE_BLOCK:
1093 if (HAVE_restore_stack_block)
1094 fcn = gen_restore_stack_block;
1095 break;
1096 #endif
1097 #ifdef HAVE_restore_stack_function
1098 case SAVE_FUNCTION:
1099 if (HAVE_restore_stack_function)
1100 fcn = gen_restore_stack_function;
1101 break;
1102 #endif
1103 #ifdef HAVE_restore_stack_nonlocal
1104 case SAVE_NONLOCAL:
1105 if (HAVE_restore_stack_nonlocal)
1106 fcn = gen_restore_stack_nonlocal;
1107 break;
1108 #endif
1109 default:
1110 break;
1113 if (sa != 0)
1115 sa = validize_mem (sa);
1116 /* These clobbers prevent the scheduler from moving
1117 references to variable arrays below the code
1118 that deletes (pops) the arrays. */
1119 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1120 emit_clobber (gen_rtx_MEM (BLKmode, stack_pointer_rtx));
1123 discard_pending_stack_adjust ();
1125 emit_insn (fcn (stack_pointer_rtx, sa));
1128 /* Invoke emit_stack_save on the nonlocal_goto_save_area for the current
1129 function. This function should be called whenever we allocate or
1130 deallocate dynamic stack space. */
1132 void
1133 update_nonlocal_goto_save_area (void)
1135 tree t_save;
1136 rtx r_save;
1138 /* The nonlocal_goto_save_area object is an array of N pointers. The
1139 first one is used for the frame pointer save; the rest are sized by
1140 STACK_SAVEAREA_MODE. Create a reference to array index 1, the first
1141 of the stack save area slots. */
1142 t_save = build4 (ARRAY_REF,
1143 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
1144 cfun->nonlocal_goto_save_area,
1145 integer_one_node, NULL_TREE, NULL_TREE);
1146 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
1148 emit_stack_save (SAVE_NONLOCAL, &r_save);
1151 /* Return an rtx representing the address of an area of memory dynamically
1152 pushed on the stack.
1154 Any required stack pointer alignment is preserved.
1156 SIZE is an rtx representing the size of the area.
1158 SIZE_ALIGN is the alignment (in bits) that we know SIZE has. This
1159 parameter may be zero. If so, a proper value will be extracted
1160 from SIZE if it is constant, otherwise BITS_PER_UNIT will be assumed.
1162 REQUIRED_ALIGN is the alignment (in bits) required for the region
1163 of memory.
1165 If CANNOT_ACCUMULATE is set to TRUE, the caller guarantees that the
1166 stack space allocated by the generated code cannot be added with itself
1167 in the course of the execution of the function. It is always safe to
1168 pass FALSE here and the following criterion is sufficient in order to
1169 pass TRUE: every path in the CFG that starts at the allocation point and
1170 loops to it executes the associated deallocation code. */
1173 allocate_dynamic_stack_space (rtx size, unsigned size_align,
1174 unsigned required_align, bool cannot_accumulate)
1176 HOST_WIDE_INT stack_usage_size = -1;
1177 rtx final_label, final_target, target;
1178 unsigned extra_align = 0;
1179 bool must_align;
1181 /* If we're asking for zero bytes, it doesn't matter what we point
1182 to since we can't dereference it. But return a reasonable
1183 address anyway. */
1184 if (size == const0_rtx)
1185 return virtual_stack_dynamic_rtx;
1187 /* Otherwise, show we're calling alloca or equivalent. */
1188 cfun->calls_alloca = 1;
1190 /* If stack usage info is requested, look into the size we are passed.
1191 We need to do so this early to avoid the obfuscation that may be
1192 introduced later by the various alignment operations. */
1193 if (flag_stack_usage_info)
1195 if (CONST_INT_P (size))
1196 stack_usage_size = INTVAL (size);
1197 else if (REG_P (size))
1199 /* Look into the last emitted insn and see if we can deduce
1200 something for the register. */
1201 rtx insn, set, note;
1202 insn = get_last_insn ();
1203 if ((set = single_set (insn)) && rtx_equal_p (SET_DEST (set), size))
1205 if (CONST_INT_P (SET_SRC (set)))
1206 stack_usage_size = INTVAL (SET_SRC (set));
1207 else if ((note = find_reg_equal_equiv_note (insn))
1208 && CONST_INT_P (XEXP (note, 0)))
1209 stack_usage_size = INTVAL (XEXP (note, 0));
1213 /* If the size is not constant, we can't say anything. */
1214 if (stack_usage_size == -1)
1216 current_function_has_unbounded_dynamic_stack_size = 1;
1217 stack_usage_size = 0;
1221 /* Ensure the size is in the proper mode. */
1222 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1223 size = convert_to_mode (Pmode, size, 1);
1225 /* Adjust SIZE_ALIGN, if needed. */
1226 if (CONST_INT_P (size))
1228 unsigned HOST_WIDE_INT lsb;
1230 lsb = INTVAL (size);
1231 lsb &= -lsb;
1233 /* Watch out for overflow truncating to "unsigned". */
1234 if (lsb > UINT_MAX / BITS_PER_UNIT)
1235 size_align = 1u << (HOST_BITS_PER_INT - 1);
1236 else
1237 size_align = (unsigned)lsb * BITS_PER_UNIT;
1239 else if (size_align < BITS_PER_UNIT)
1240 size_align = BITS_PER_UNIT;
1242 /* We can't attempt to minimize alignment necessary, because we don't
1243 know the final value of preferred_stack_boundary yet while executing
1244 this code. */
1245 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
1246 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
1248 /* We will need to ensure that the address we return is aligned to
1249 REQUIRED_ALIGN. If STACK_DYNAMIC_OFFSET is defined, we don't
1250 always know its final value at this point in the compilation (it
1251 might depend on the size of the outgoing parameter lists, for
1252 example), so we must align the value to be returned in that case.
1253 (Note that STACK_DYNAMIC_OFFSET will have a default nonzero value if
1254 STACK_POINTER_OFFSET or ACCUMULATE_OUTGOING_ARGS are defined).
1255 We must also do an alignment operation on the returned value if
1256 the stack pointer alignment is less strict than REQUIRED_ALIGN.
1258 If we have to align, we must leave space in SIZE for the hole
1259 that might result from the alignment operation. */
1261 must_align = (crtl->preferred_stack_boundary < required_align);
1262 if (must_align)
1264 if (required_align > PREFERRED_STACK_BOUNDARY)
1265 extra_align = PREFERRED_STACK_BOUNDARY;
1266 else if (required_align > STACK_BOUNDARY)
1267 extra_align = STACK_BOUNDARY;
1268 else
1269 extra_align = BITS_PER_UNIT;
1272 /* ??? STACK_POINTER_OFFSET is always defined now. */
1273 #if defined (STACK_DYNAMIC_OFFSET) || defined (STACK_POINTER_OFFSET)
1274 must_align = true;
1275 extra_align = BITS_PER_UNIT;
1276 #endif
1278 if (must_align)
1280 unsigned extra = (required_align - extra_align) / BITS_PER_UNIT;
1282 size = plus_constant (Pmode, size, extra);
1283 size = force_operand (size, NULL_RTX);
1285 if (flag_stack_usage_info)
1286 stack_usage_size += extra;
1288 if (extra && size_align > extra_align)
1289 size_align = extra_align;
1292 /* Round the size to a multiple of the required stack alignment.
1293 Since the stack if presumed to be rounded before this allocation,
1294 this will maintain the required alignment.
1296 If the stack grows downward, we could save an insn by subtracting
1297 SIZE from the stack pointer and then aligning the stack pointer.
1298 The problem with this is that the stack pointer may be unaligned
1299 between the execution of the subtraction and alignment insns and
1300 some machines do not allow this. Even on those that do, some
1301 signal handlers malfunction if a signal should occur between those
1302 insns. Since this is an extremely rare event, we have no reliable
1303 way of knowing which systems have this problem. So we avoid even
1304 momentarily mis-aligning the stack. */
1305 if (size_align % MAX_SUPPORTED_STACK_ALIGNMENT != 0)
1307 size = round_push (size);
1309 if (flag_stack_usage_info)
1311 int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
1312 stack_usage_size = (stack_usage_size + align - 1) / align * align;
1316 target = gen_reg_rtx (Pmode);
1318 /* The size is supposed to be fully adjusted at this point so record it
1319 if stack usage info is requested. */
1320 if (flag_stack_usage_info)
1322 current_function_dynamic_stack_size += stack_usage_size;
1324 /* ??? This is gross but the only safe stance in the absence
1325 of stack usage oriented flow analysis. */
1326 if (!cannot_accumulate)
1327 current_function_has_unbounded_dynamic_stack_size = 1;
1330 final_label = NULL_RTX;
1331 final_target = NULL_RTX;
1333 /* If we are splitting the stack, we need to ask the backend whether
1334 there is enough room on the current stack. If there isn't, or if
1335 the backend doesn't know how to tell is, then we need to call a
1336 function to allocate memory in some other way. This memory will
1337 be released when we release the current stack segment. The
1338 effect is that stack allocation becomes less efficient, but at
1339 least it doesn't cause a stack overflow. */
1340 if (flag_split_stack)
1342 rtx available_label, ask, space, func;
1344 available_label = NULL_RTX;
1346 #ifdef HAVE_split_stack_space_check
1347 if (HAVE_split_stack_space_check)
1349 available_label = gen_label_rtx ();
1351 /* This instruction will branch to AVAILABLE_LABEL if there
1352 are SIZE bytes available on the stack. */
1353 emit_insn (gen_split_stack_space_check (size, available_label));
1355 #endif
1357 /* The __morestack_allocate_stack_space function will allocate
1358 memory using malloc. If the alignment of the memory returned
1359 by malloc does not meet REQUIRED_ALIGN, we increase SIZE to
1360 make sure we allocate enough space. */
1361 if (MALLOC_ABI_ALIGNMENT >= required_align)
1362 ask = size;
1363 else
1365 ask = expand_binop (Pmode, add_optab, size,
1366 GEN_INT (required_align / BITS_PER_UNIT - 1),
1367 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1368 must_align = true;
1371 func = init_one_libfunc ("__morestack_allocate_stack_space");
1373 space = emit_library_call_value (func, target, LCT_NORMAL, Pmode,
1374 1, ask, Pmode);
1376 if (available_label == NULL_RTX)
1377 return space;
1379 final_target = gen_reg_rtx (Pmode);
1381 emit_move_insn (final_target, space);
1383 final_label = gen_label_rtx ();
1384 emit_jump (final_label);
1386 emit_label (available_label);
1389 do_pending_stack_adjust ();
1391 /* We ought to be called always on the toplevel and stack ought to be aligned
1392 properly. */
1393 gcc_assert (!(stack_pointer_delta
1394 % (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)));
1396 /* If needed, check that we have the required amount of stack. Take into
1397 account what has already been checked. */
1398 if (STACK_CHECK_MOVING_SP)
1400 else if (flag_stack_check == GENERIC_STACK_CHECK)
1401 probe_stack_range (STACK_OLD_CHECK_PROTECT + STACK_CHECK_MAX_FRAME_SIZE,
1402 size);
1403 else if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
1404 probe_stack_range (STACK_CHECK_PROTECT, size);
1406 /* Don't let anti_adjust_stack emit notes. */
1407 suppress_reg_args_size = true;
1409 /* Perform the required allocation from the stack. Some systems do
1410 this differently than simply incrementing/decrementing from the
1411 stack pointer, such as acquiring the space by calling malloc(). */
1412 #ifdef HAVE_allocate_stack
1413 if (HAVE_allocate_stack)
1415 struct expand_operand ops[2];
1416 /* We don't have to check against the predicate for operand 0 since
1417 TARGET is known to be a pseudo of the proper mode, which must
1418 be valid for the operand. */
1419 create_fixed_operand (&ops[0], target);
1420 create_convert_operand_to (&ops[1], size, STACK_SIZE_MODE, true);
1421 expand_insn (CODE_FOR_allocate_stack, 2, ops);
1423 else
1424 #endif
1426 int saved_stack_pointer_delta;
1428 #ifndef STACK_GROWS_DOWNWARD
1429 emit_move_insn (target, virtual_stack_dynamic_rtx);
1430 #endif
1432 /* Check stack bounds if necessary. */
1433 if (crtl->limit_stack)
1435 rtx available;
1436 rtx space_available = gen_label_rtx ();
1437 #ifdef STACK_GROWS_DOWNWARD
1438 available = expand_binop (Pmode, sub_optab,
1439 stack_pointer_rtx, stack_limit_rtx,
1440 NULL_RTX, 1, OPTAB_WIDEN);
1441 #else
1442 available = expand_binop (Pmode, sub_optab,
1443 stack_limit_rtx, stack_pointer_rtx,
1444 NULL_RTX, 1, OPTAB_WIDEN);
1445 #endif
1446 emit_cmp_and_jump_insns (available, size, GEU, NULL_RTX, Pmode, 1,
1447 space_available);
1448 #ifdef HAVE_trap
1449 if (HAVE_trap)
1450 emit_insn (gen_trap ());
1451 else
1452 #endif
1453 error ("stack limits not supported on this target");
1454 emit_barrier ();
1455 emit_label (space_available);
1458 saved_stack_pointer_delta = stack_pointer_delta;
1460 if (flag_stack_check && STACK_CHECK_MOVING_SP)
1461 anti_adjust_stack_and_probe (size, false);
1462 else
1463 anti_adjust_stack (size);
1465 /* Even if size is constant, don't modify stack_pointer_delta.
1466 The constant size alloca should preserve
1467 crtl->preferred_stack_boundary alignment. */
1468 stack_pointer_delta = saved_stack_pointer_delta;
1470 #ifdef STACK_GROWS_DOWNWARD
1471 emit_move_insn (target, virtual_stack_dynamic_rtx);
1472 #endif
1475 suppress_reg_args_size = false;
1477 /* Finish up the split stack handling. */
1478 if (final_label != NULL_RTX)
1480 gcc_assert (flag_split_stack);
1481 emit_move_insn (final_target, target);
1482 emit_label (final_label);
1483 target = final_target;
1486 if (must_align)
1488 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1489 but we know it can't. So add ourselves and then do
1490 TRUNC_DIV_EXPR. */
1491 target = expand_binop (Pmode, add_optab, target,
1492 GEN_INT (required_align / BITS_PER_UNIT - 1),
1493 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1494 target = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, target,
1495 GEN_INT (required_align / BITS_PER_UNIT),
1496 NULL_RTX, 1);
1497 target = expand_mult (Pmode, target,
1498 GEN_INT (required_align / BITS_PER_UNIT),
1499 NULL_RTX, 1);
1502 /* Now that we've committed to a return value, mark its alignment. */
1503 mark_reg_pointer (target, required_align);
1505 /* Record the new stack level for nonlocal gotos. */
1506 if (cfun->nonlocal_goto_save_area != 0)
1507 update_nonlocal_goto_save_area ();
1509 return target;
1512 /* A front end may want to override GCC's stack checking by providing a
1513 run-time routine to call to check the stack, so provide a mechanism for
1514 calling that routine. */
1516 static GTY(()) rtx stack_check_libfunc;
1518 void
1519 set_stack_check_libfunc (const char *libfunc_name)
1521 gcc_assert (stack_check_libfunc == NULL_RTX);
1522 stack_check_libfunc = gen_rtx_SYMBOL_REF (Pmode, libfunc_name);
1525 /* Emit one stack probe at ADDRESS, an address within the stack. */
1527 void
1528 emit_stack_probe (rtx address)
1530 #ifdef HAVE_probe_stack_address
1531 if (HAVE_probe_stack_address)
1532 emit_insn (gen_probe_stack_address (address));
1533 else
1534 #endif
1536 rtx memref = gen_rtx_MEM (word_mode, address);
1538 MEM_VOLATILE_P (memref) = 1;
1540 /* See if we have an insn to probe the stack. */
1541 #ifdef HAVE_probe_stack
1542 if (HAVE_probe_stack)
1543 emit_insn (gen_probe_stack (memref));
1544 else
1545 #endif
1546 emit_move_insn (memref, const0_rtx);
1550 /* Probe a range of stack addresses from FIRST to FIRST+SIZE, inclusive.
1551 FIRST is a constant and size is a Pmode RTX. These are offsets from
1552 the current stack pointer. STACK_GROWS_DOWNWARD says whether to add
1553 or subtract them from the stack pointer. */
1555 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
1557 #ifdef STACK_GROWS_DOWNWARD
1558 #define STACK_GROW_OP MINUS
1559 #define STACK_GROW_OPTAB sub_optab
1560 #define STACK_GROW_OFF(off) -(off)
1561 #else
1562 #define STACK_GROW_OP PLUS
1563 #define STACK_GROW_OPTAB add_optab
1564 #define STACK_GROW_OFF(off) (off)
1565 #endif
1567 void
1568 probe_stack_range (HOST_WIDE_INT first, rtx size)
1570 /* First ensure SIZE is Pmode. */
1571 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1572 size = convert_to_mode (Pmode, size, 1);
1574 /* Next see if we have a function to check the stack. */
1575 if (stack_check_libfunc)
1577 rtx addr = memory_address (Pmode,
1578 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1579 stack_pointer_rtx,
1580 plus_constant (Pmode,
1581 size, first)));
1582 emit_library_call (stack_check_libfunc, LCT_NORMAL, VOIDmode, 1, addr,
1583 Pmode);
1586 /* Next see if we have an insn to check the stack. */
1587 #ifdef HAVE_check_stack
1588 else if (HAVE_check_stack)
1590 struct expand_operand ops[1];
1591 rtx addr = memory_address (Pmode,
1592 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1593 stack_pointer_rtx,
1594 plus_constant (Pmode,
1595 size, first)));
1596 bool success;
1597 create_input_operand (&ops[0], addr, Pmode);
1598 success = maybe_expand_insn (CODE_FOR_check_stack, 1, ops);
1599 gcc_assert (success);
1601 #endif
1603 /* Otherwise we have to generate explicit probes. If we have a constant
1604 small number of them to generate, that's the easy case. */
1605 else if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
1607 HOST_WIDE_INT isize = INTVAL (size), i;
1608 rtx addr;
1610 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
1611 it exceeds SIZE. If only one probe is needed, this will not
1612 generate any code. Then probe at FIRST + SIZE. */
1613 for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
1615 addr = memory_address (Pmode,
1616 plus_constant (Pmode, stack_pointer_rtx,
1617 STACK_GROW_OFF (first + i)));
1618 emit_stack_probe (addr);
1621 addr = memory_address (Pmode,
1622 plus_constant (Pmode, stack_pointer_rtx,
1623 STACK_GROW_OFF (first + isize)));
1624 emit_stack_probe (addr);
1627 /* In the variable case, do the same as above, but in a loop. Note that we
1628 must be extra careful with variables wrapping around because we might be
1629 at the very top (or the very bottom) of the address space and we have to
1630 be able to handle this case properly; in particular, we use an equality
1631 test for the loop condition. */
1632 else
1634 rtx rounded_size, rounded_size_op, test_addr, last_addr, temp;
1635 rtx loop_lab = gen_label_rtx ();
1636 rtx end_lab = gen_label_rtx ();
1639 /* Step 1: round SIZE to the previous multiple of the interval. */
1641 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1642 rounded_size
1643 = simplify_gen_binary (AND, Pmode, size, GEN_INT (-PROBE_INTERVAL));
1644 rounded_size_op = force_operand (rounded_size, NULL_RTX);
1647 /* Step 2: compute initial and final value of the loop counter. */
1649 /* TEST_ADDR = SP + FIRST. */
1650 test_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1651 stack_pointer_rtx,
1652 GEN_INT (first)), NULL_RTX);
1654 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
1655 last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1656 test_addr,
1657 rounded_size_op), NULL_RTX);
1660 /* Step 3: the loop
1662 while (TEST_ADDR != LAST_ADDR)
1664 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
1665 probe at TEST_ADDR
1668 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
1669 until it is equal to ROUNDED_SIZE. */
1671 emit_label (loop_lab);
1673 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
1674 emit_cmp_and_jump_insns (test_addr, last_addr, EQ, NULL_RTX, Pmode, 1,
1675 end_lab);
1677 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
1678 temp = expand_binop (Pmode, STACK_GROW_OPTAB, test_addr,
1679 GEN_INT (PROBE_INTERVAL), test_addr,
1680 1, OPTAB_WIDEN);
1682 gcc_assert (temp == test_addr);
1684 /* Probe at TEST_ADDR. */
1685 emit_stack_probe (test_addr);
1687 emit_jump (loop_lab);
1689 emit_label (end_lab);
1692 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
1693 that SIZE is equal to ROUNDED_SIZE. */
1695 /* TEMP = SIZE - ROUNDED_SIZE. */
1696 temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
1697 if (temp != const0_rtx)
1699 rtx addr;
1701 if (CONST_INT_P (temp))
1703 /* Use [base + disp} addressing mode if supported. */
1704 HOST_WIDE_INT offset = INTVAL (temp);
1705 addr = memory_address (Pmode,
1706 plus_constant (Pmode, last_addr,
1707 STACK_GROW_OFF (offset)));
1709 else
1711 /* Manual CSE if the difference is not known at compile-time. */
1712 temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
1713 addr = memory_address (Pmode,
1714 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1715 last_addr, temp));
1718 emit_stack_probe (addr);
1723 /* Adjust the stack pointer by minus SIZE (an rtx for a number of bytes)
1724 while probing it. This pushes when SIZE is positive. SIZE need not
1725 be constant. If ADJUST_BACK is true, adjust back the stack pointer
1726 by plus SIZE at the end. */
1728 void
1729 anti_adjust_stack_and_probe (rtx size, bool adjust_back)
1731 /* We skip the probe for the first interval + a small dope of 4 words and
1732 probe that many bytes past the specified size to maintain a protection
1733 area at the botton of the stack. */
1734 const int dope = 4 * UNITS_PER_WORD;
1736 /* First ensure SIZE is Pmode. */
1737 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1738 size = convert_to_mode (Pmode, size, 1);
1740 /* If we have a constant small number of probes to generate, that's the
1741 easy case. */
1742 if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
1744 HOST_WIDE_INT isize = INTVAL (size), i;
1745 bool first_probe = true;
1747 /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
1748 values of N from 1 until it exceeds SIZE. If only one probe is
1749 needed, this will not generate any code. Then adjust and probe
1750 to PROBE_INTERVAL + SIZE. */
1751 for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
1753 if (first_probe)
1755 anti_adjust_stack (GEN_INT (2 * PROBE_INTERVAL + dope));
1756 first_probe = false;
1758 else
1759 anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
1760 emit_stack_probe (stack_pointer_rtx);
1763 if (first_probe)
1764 anti_adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL + dope));
1765 else
1766 anti_adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL - i));
1767 emit_stack_probe (stack_pointer_rtx);
1770 /* In the variable case, do the same as above, but in a loop. Note that we
1771 must be extra careful with variables wrapping around because we might be
1772 at the very top (or the very bottom) of the address space and we have to
1773 be able to handle this case properly; in particular, we use an equality
1774 test for the loop condition. */
1775 else
1777 rtx rounded_size, rounded_size_op, last_addr, temp;
1778 rtx loop_lab = gen_label_rtx ();
1779 rtx end_lab = gen_label_rtx ();
1782 /* Step 1: round SIZE to the previous multiple of the interval. */
1784 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1785 rounded_size
1786 = simplify_gen_binary (AND, Pmode, size, GEN_INT (-PROBE_INTERVAL));
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"