]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/explow.c
2014-10-14 Andrew Pinski <apinski@cavium.com>
[thirdparty/gcc.git] / gcc / explow.c
1 /* Subroutines for manipulating rtx's in semantically interesting ways.
2 Copyright (C) 1987-2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20
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"
44
45 static rtx break_out_memory_refs (rtx);
46
47
48 /* Truncate and perhaps sign-extend C as appropriate for MODE. */
49
50 HOST_WIDE_INT
51 trunc_int_for_mode (HOST_WIDE_INT c, enum machine_mode mode)
52 {
53 int width = GET_MODE_PRECISION (mode);
54
55 /* You want to truncate to a _what_? */
56 gcc_assert (SCALAR_INT_MODE_P (mode));
57
58 /* Canonicalize BImode to 0 and STORE_FLAG_VALUE. */
59 if (mode == BImode)
60 return c & 1 ? STORE_FLAG_VALUE : 0;
61
62 /* Sign-extend for the requested mode. */
63
64 if (width < HOST_BITS_PER_WIDE_INT)
65 {
66 HOST_WIDE_INT sign = 1;
67 sign <<= width - 1;
68 c &= (sign << 1) - 1;
69 c ^= sign;
70 c -= sign;
71 }
72
73 return c;
74 }
75
76 /* Return an rtx for the sum of X and the integer C, given that X has
77 mode MODE. INPLACE is true if X can be modified inplace or false
78 if it must be treated as immutable. */
79
80 rtx
81 plus_constant (enum machine_mode mode, rtx x, HOST_WIDE_INT c,
82 bool inplace)
83 {
84 RTX_CODE code;
85 rtx y;
86 rtx tem;
87 int all_constant = 0;
88
89 gcc_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
90
91 if (c == 0)
92 return x;
93
94 restart:
95
96 code = GET_CODE (x);
97 y = x;
98
99 switch (code)
100 {
101 CASE_CONST_SCALAR_INT:
102 return immed_wide_int_const (wi::add (std::make_pair (x, mode), c),
103 mode);
104 case MEM:
105 /* If this is a reference to the constant pool, try replacing it with
106 a reference to a new constant. If the resulting address isn't
107 valid, don't return it because we have no way to validize it. */
108 if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
109 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
110 {
111 tem = plus_constant (mode, get_pool_constant (XEXP (x, 0)), c);
112 tem = force_const_mem (GET_MODE (x), tem);
113 if (memory_address_p (GET_MODE (tem), XEXP (tem, 0)))
114 return tem;
115 }
116 break;
117
118 case CONST:
119 /* If adding to something entirely constant, set a flag
120 so that we can add a CONST around the result. */
121 if (inplace && shared_const_p (x))
122 inplace = false;
123 x = XEXP (x, 0);
124 all_constant = 1;
125 goto restart;
126
127 case SYMBOL_REF:
128 case LABEL_REF:
129 all_constant = 1;
130 break;
131
132 case PLUS:
133 /* The interesting case is adding the integer to a sum. Look
134 for constant term in the sum and combine with C. For an
135 integer constant term or a constant term that is not an
136 explicit integer, we combine or group them together anyway.
137
138 We may not immediately return from the recursive call here, lest
139 all_constant gets lost. */
140
141 if (CONSTANT_P (XEXP (x, 1)))
142 {
143 rtx term = plus_constant (mode, XEXP (x, 1), c, inplace);
144 if (term == const0_rtx)
145 x = XEXP (x, 0);
146 else if (inplace)
147 XEXP (x, 1) = term;
148 else
149 x = gen_rtx_PLUS (mode, XEXP (x, 0), term);
150 c = 0;
151 }
152 else if (rtx *const_loc = find_constant_term_loc (&y))
153 {
154 if (!inplace)
155 {
156 /* We need to be careful since X may be shared and we can't
157 modify it in place. */
158 x = copy_rtx (x);
159 const_loc = find_constant_term_loc (&x);
160 }
161 *const_loc = plus_constant (mode, *const_loc, c, true);
162 c = 0;
163 }
164 break;
165
166 default:
167 break;
168 }
169
170 if (c != 0)
171 x = gen_rtx_PLUS (mode, x, gen_int_mode (c, mode));
172
173 if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
174 return x;
175 else if (all_constant)
176 return gen_rtx_CONST (mode, x);
177 else
178 return x;
179 }
180 \f
181 /* If X is a sum, return a new sum like X but lacking any constant terms.
182 Add all the removed constant terms into *CONSTPTR.
183 X itself is not altered. The result != X if and only if
184 it is not isomorphic to X. */
185
186 rtx
187 eliminate_constant_term (rtx x, rtx *constptr)
188 {
189 rtx x0, x1;
190 rtx tem;
191
192 if (GET_CODE (x) != PLUS)
193 return x;
194
195 /* First handle constants appearing at this level explicitly. */
196 if (CONST_INT_P (XEXP (x, 1))
197 && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x), *constptr,
198 XEXP (x, 1)))
199 && CONST_INT_P (tem))
200 {
201 *constptr = tem;
202 return eliminate_constant_term (XEXP (x, 0), constptr);
203 }
204
205 tem = const0_rtx;
206 x0 = eliminate_constant_term (XEXP (x, 0), &tem);
207 x1 = eliminate_constant_term (XEXP (x, 1), &tem);
208 if ((x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
209 && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x),
210 *constptr, tem))
211 && CONST_INT_P (tem))
212 {
213 *constptr = tem;
214 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
215 }
216
217 return x;
218 }
219
220 /* Returns a tree for the size of EXP in bytes. */
221
222 static tree
223 tree_expr_size (const_tree exp)
224 {
225 if (DECL_P (exp)
226 && DECL_SIZE_UNIT (exp) != 0)
227 return DECL_SIZE_UNIT (exp);
228 else
229 return size_in_bytes (TREE_TYPE (exp));
230 }
231
232 /* Return an rtx for the size in bytes of the value of EXP. */
233
234 rtx
235 expr_size (tree exp)
236 {
237 tree size;
238
239 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
240 size = TREE_OPERAND (exp, 1);
241 else
242 {
243 size = tree_expr_size (exp);
244 gcc_assert (size);
245 gcc_assert (size == SUBSTITUTE_PLACEHOLDER_IN_EXPR (size, exp));
246 }
247
248 return expand_expr (size, NULL_RTX, TYPE_MODE (sizetype), EXPAND_NORMAL);
249 }
250
251 /* Return a wide integer for the size in bytes of the value of EXP, or -1
252 if the size can vary or is larger than an integer. */
253
254 HOST_WIDE_INT
255 int_expr_size (tree exp)
256 {
257 tree size;
258
259 if (TREE_CODE (exp) == WITH_SIZE_EXPR)
260 size = TREE_OPERAND (exp, 1);
261 else
262 {
263 size = tree_expr_size (exp);
264 gcc_assert (size);
265 }
266
267 if (size == 0 || !tree_fits_shwi_p (size))
268 return -1;
269
270 return tree_to_shwi (size);
271 }
272 \f
273 /* Return a copy of X in which all memory references
274 and all constants that involve symbol refs
275 have been replaced with new temporary registers.
276 Also emit code to load the memory locations and constants
277 into those registers.
278
279 If X contains no such constants or memory references,
280 X itself (not a copy) is returned.
281
282 If a constant is found in the address that is not a legitimate constant
283 in an insn, it is left alone in the hope that it might be valid in the
284 address.
285
286 X may contain no arithmetic except addition, subtraction and multiplication.
287 Values returned by expand_expr with 1 for sum_ok fit this constraint. */
288
289 static rtx
290 break_out_memory_refs (rtx x)
291 {
292 if (MEM_P (x)
293 || (CONSTANT_P (x) && CONSTANT_ADDRESS_P (x)
294 && GET_MODE (x) != VOIDmode))
295 x = force_reg (GET_MODE (x), x);
296 else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
297 || GET_CODE (x) == MULT)
298 {
299 rtx op0 = break_out_memory_refs (XEXP (x, 0));
300 rtx op1 = break_out_memory_refs (XEXP (x, 1));
301
302 if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
303 x = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
304 }
305
306 return x;
307 }
308
309 /* Given X, a memory address in address space AS' pointer mode, convert it to
310 an address in the address space's address mode, or vice versa (TO_MODE says
311 which way). We take advantage of the fact that pointers are not allowed to
312 overflow by commuting arithmetic operations over conversions so that address
313 arithmetic insns can be used. IN_CONST is true if this conversion is inside
314 a CONST. */
315
316 static rtx
317 convert_memory_address_addr_space_1 (enum machine_mode to_mode ATTRIBUTE_UNUSED,
318 rtx x, addr_space_t as ATTRIBUTE_UNUSED,
319 bool in_const)
320 {
321 #ifndef POINTERS_EXTEND_UNSIGNED
322 gcc_assert (GET_MODE (x) == to_mode || GET_MODE (x) == VOIDmode);
323 return x;
324 #else /* defined(POINTERS_EXTEND_UNSIGNED) */
325 enum machine_mode pointer_mode, address_mode, from_mode;
326 rtx temp;
327 enum rtx_code code;
328
329 /* If X already has the right mode, just return it. */
330 if (GET_MODE (x) == to_mode)
331 return x;
332
333 pointer_mode = targetm.addr_space.pointer_mode (as);
334 address_mode = targetm.addr_space.address_mode (as);
335 from_mode = to_mode == pointer_mode ? address_mode : pointer_mode;
336
337 /* Here we handle some special cases. If none of them apply, fall through
338 to the default case. */
339 switch (GET_CODE (x))
340 {
341 CASE_CONST_SCALAR_INT:
342 if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode))
343 code = TRUNCATE;
344 else if (POINTERS_EXTEND_UNSIGNED < 0)
345 break;
346 else if (POINTERS_EXTEND_UNSIGNED > 0)
347 code = ZERO_EXTEND;
348 else
349 code = SIGN_EXTEND;
350 temp = simplify_unary_operation (code, to_mode, x, from_mode);
351 if (temp)
352 return temp;
353 break;
354
355 case SUBREG:
356 if ((SUBREG_PROMOTED_VAR_P (x) || REG_POINTER (SUBREG_REG (x)))
357 && GET_MODE (SUBREG_REG (x)) == to_mode)
358 return SUBREG_REG (x);
359 break;
360
361 case LABEL_REF:
362 temp = gen_rtx_LABEL_REF (to_mode, LABEL_REF_LABEL (x));
363 LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
364 return temp;
365 break;
366
367 case SYMBOL_REF:
368 temp = shallow_copy_rtx (x);
369 PUT_MODE (temp, to_mode);
370 return temp;
371 break;
372
373 case CONST:
374 return gen_rtx_CONST (to_mode,
375 convert_memory_address_addr_space_1
376 (to_mode, XEXP (x, 0), as, true));
377 break;
378
379 case PLUS:
380 case MULT:
381 /* For addition we can safely permute the conversion and addition
382 operation if one operand is a constant and converting the constant
383 does not change it or if one operand is a constant and we are
384 using a ptr_extend instruction (POINTERS_EXTEND_UNSIGNED < 0).
385 We can always safely permute them if we are making the address
386 narrower. Inside a CONST RTL, this is safe for both pointers
387 zero or sign extended as pointers cannot wrap. */
388 if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode)
389 || (GET_CODE (x) == PLUS
390 && CONST_INT_P (XEXP (x, 1))
391 && ((in_const && POINTERS_EXTEND_UNSIGNED != 0)
392 || XEXP (x, 1) == convert_memory_address_addr_space_1
393 (to_mode, XEXP (x, 1), as, in_const)
394 || POINTERS_EXTEND_UNSIGNED < 0)))
395 return gen_rtx_fmt_ee (GET_CODE (x), to_mode,
396 convert_memory_address_addr_space_1
397 (to_mode, XEXP (x, 0), as, in_const),
398 XEXP (x, 1));
399 break;
400
401 default:
402 break;
403 }
404
405 return convert_modes (to_mode, from_mode,
406 x, POINTERS_EXTEND_UNSIGNED);
407 #endif /* defined(POINTERS_EXTEND_UNSIGNED) */
408 }
409
410 /* Given X, a memory address in address space AS' pointer mode, convert it to
411 an address in the address space's address mode, or vice versa (TO_MODE says
412 which way). We take advantage of the fact that pointers are not allowed to
413 overflow by commuting arithmetic operations over conversions so that address
414 arithmetic insns can be used. */
415
416 rtx
417 convert_memory_address_addr_space (enum machine_mode to_mode, rtx x, addr_space_t as)
418 {
419 return convert_memory_address_addr_space_1 (to_mode, x, as, false);
420 }
421 \f
422 /* Return something equivalent to X but valid as a memory address for something
423 of mode MODE in the named address space AS. When X is not itself valid,
424 this works by copying X or subexpressions of it into registers. */
425
426 rtx
427 memory_address_addr_space (enum machine_mode mode, rtx x, addr_space_t as)
428 {
429 rtx oldx = x;
430 enum machine_mode address_mode = targetm.addr_space.address_mode (as);
431
432 x = convert_memory_address_addr_space (address_mode, x, as);
433
434 /* By passing constant addresses through registers
435 we get a chance to cse them. */
436 if (! cse_not_expected && CONSTANT_P (x) && CONSTANT_ADDRESS_P (x))
437 x = force_reg (address_mode, x);
438
439 /* We get better cse by rejecting indirect addressing at this stage.
440 Let the combiner create indirect addresses where appropriate.
441 For now, generate the code so that the subexpressions useful to share
442 are visible. But not if cse won't be done! */
443 else
444 {
445 if (! cse_not_expected && !REG_P (x))
446 x = break_out_memory_refs (x);
447
448 /* At this point, any valid address is accepted. */
449 if (memory_address_addr_space_p (mode, x, as))
450 goto done;
451
452 /* If it was valid before but breaking out memory refs invalidated it,
453 use it the old way. */
454 if (memory_address_addr_space_p (mode, oldx, as))
455 {
456 x = oldx;
457 goto done;
458 }
459
460 /* Perform machine-dependent transformations on X
461 in certain cases. This is not necessary since the code
462 below can handle all possible cases, but machine-dependent
463 transformations can make better code. */
464 {
465 rtx orig_x = x;
466 x = targetm.addr_space.legitimize_address (x, oldx, mode, as);
467 if (orig_x != x && memory_address_addr_space_p (mode, x, as))
468 goto done;
469 }
470
471 /* PLUS and MULT can appear in special ways
472 as the result of attempts to make an address usable for indexing.
473 Usually they are dealt with by calling force_operand, below.
474 But a sum containing constant terms is special
475 if removing them makes the sum a valid address:
476 then we generate that address in a register
477 and index off of it. We do this because it often makes
478 shorter code, and because the addresses thus generated
479 in registers often become common subexpressions. */
480 if (GET_CODE (x) == PLUS)
481 {
482 rtx constant_term = const0_rtx;
483 rtx y = eliminate_constant_term (x, &constant_term);
484 if (constant_term == const0_rtx
485 || ! memory_address_addr_space_p (mode, y, as))
486 x = force_operand (x, NULL_RTX);
487 else
488 {
489 y = gen_rtx_PLUS (GET_MODE (x), copy_to_reg (y), constant_term);
490 if (! memory_address_addr_space_p (mode, y, as))
491 x = force_operand (x, NULL_RTX);
492 else
493 x = y;
494 }
495 }
496
497 else if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
498 x = force_operand (x, NULL_RTX);
499
500 /* If we have a register that's an invalid address,
501 it must be a hard reg of the wrong class. Copy it to a pseudo. */
502 else if (REG_P (x))
503 x = copy_to_reg (x);
504
505 /* Last resort: copy the value to a register, since
506 the register is a valid address. */
507 else
508 x = force_reg (address_mode, x);
509 }
510
511 done:
512
513 gcc_assert (memory_address_addr_space_p (mode, x, as));
514 /* If we didn't change the address, we are done. Otherwise, mark
515 a reg as a pointer if we have REG or REG + CONST_INT. */
516 if (oldx == x)
517 return x;
518 else if (REG_P (x))
519 mark_reg_pointer (x, BITS_PER_UNIT);
520 else if (GET_CODE (x) == PLUS
521 && REG_P (XEXP (x, 0))
522 && CONST_INT_P (XEXP (x, 1)))
523 mark_reg_pointer (XEXP (x, 0), BITS_PER_UNIT);
524
525 /* OLDX may have been the address on a temporary. Update the address
526 to indicate that X is now used. */
527 update_temp_slot_address (oldx, x);
528
529 return x;
530 }
531
532 /* If REF is a MEM with an invalid address, change it into a valid address.
533 Pass through anything else unchanged. REF must be an unshared rtx and
534 the function may modify it in-place. */
535
536 rtx
537 validize_mem (rtx ref)
538 {
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;
545
546 return replace_equiv_address (ref, XEXP (ref, 0), true);
547 }
548
549 /* If X is a memory reference to a member of an object block, try rewriting
550 it to use an anchor instead. Return the new memory reference on success
551 and the old one on failure. */
552
553 rtx
554 use_anchored_address (rtx x)
555 {
556 rtx base;
557 HOST_WIDE_INT offset;
558 enum machine_mode mode;
559
560 if (!flag_section_anchors)
561 return x;
562
563 if (!MEM_P (x))
564 return x;
565
566 /* Split the address into a base and offset. */
567 base = XEXP (x, 0);
568 offset = 0;
569 if (GET_CODE (base) == CONST
570 && GET_CODE (XEXP (base, 0)) == PLUS
571 && CONST_INT_P (XEXP (XEXP (base, 0), 1)))
572 {
573 offset += INTVAL (XEXP (XEXP (base, 0), 1));
574 base = XEXP (XEXP (base, 0), 0);
575 }
576
577 /* Check whether BASE is suitable for anchors. */
578 if (GET_CODE (base) != SYMBOL_REF
579 || !SYMBOL_REF_HAS_BLOCK_INFO_P (base)
580 || SYMBOL_REF_ANCHOR_P (base)
581 || SYMBOL_REF_BLOCK (base) == NULL
582 || !targetm.use_anchors_for_symbol_p (base))
583 return x;
584
585 /* Decide where BASE is going to be. */
586 place_block_symbol (base);
587
588 /* Get the anchor we need to use. */
589 offset += SYMBOL_REF_BLOCK_OFFSET (base);
590 base = get_section_anchor (SYMBOL_REF_BLOCK (base), offset,
591 SYMBOL_REF_TLS_MODEL (base));
592
593 /* Work out the offset from the anchor. */
594 offset -= SYMBOL_REF_BLOCK_OFFSET (base);
595
596 /* If we're going to run a CSE pass, force the anchor into a register.
597 We will then be able to reuse registers for several accesses, if the
598 target costs say that that's worthwhile. */
599 mode = GET_MODE (base);
600 if (!cse_not_expected)
601 base = force_reg (mode, base);
602
603 return replace_equiv_address (x, plus_constant (mode, base, offset));
604 }
605 \f
606 /* Copy the value or contents of X to a new temp reg and return that reg. */
607
608 rtx
609 copy_to_reg (rtx x)
610 {
611 rtx temp = gen_reg_rtx (GET_MODE (x));
612
613 /* If not an operand, must be an address with PLUS and MULT so
614 do the computation. */
615 if (! general_operand (x, VOIDmode))
616 x = force_operand (x, temp);
617
618 if (x != temp)
619 emit_move_insn (temp, x);
620
621 return temp;
622 }
623
624 /* Like copy_to_reg but always give the new register mode Pmode
625 in case X is a constant. */
626
627 rtx
628 copy_addr_to_reg (rtx x)
629 {
630 return copy_to_mode_reg (Pmode, x);
631 }
632
633 /* Like copy_to_reg but always give the new register mode MODE
634 in case X is a constant. */
635
636 rtx
637 copy_to_mode_reg (enum machine_mode mode, rtx x)
638 {
639 rtx temp = gen_reg_rtx (mode);
640
641 /* If not an operand, must be an address with PLUS and MULT so
642 do the computation. */
643 if (! general_operand (x, VOIDmode))
644 x = force_operand (x, temp);
645
646 gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
647 if (x != temp)
648 emit_move_insn (temp, x);
649 return temp;
650 }
651
652 /* Load X into a register if it is not already one.
653 Use mode MODE for the register.
654 X should be valid for mode MODE, but it may be a constant which
655 is valid for all integer modes; that's why caller must specify MODE.
656
657 The caller must not alter the value in the register we return,
658 since we mark it as a "constant" register. */
659
660 rtx
661 force_reg (enum machine_mode mode, rtx x)
662 {
663 rtx temp, set;
664 rtx_insn *insn;
665
666 if (REG_P (x))
667 return x;
668
669 if (general_operand (x, mode))
670 {
671 temp = gen_reg_rtx (mode);
672 insn = emit_move_insn (temp, x);
673 }
674 else
675 {
676 temp = force_operand (x, NULL_RTX);
677 if (REG_P (temp))
678 insn = get_last_insn ();
679 else
680 {
681 rtx temp2 = gen_reg_rtx (mode);
682 insn = emit_move_insn (temp2, temp);
683 temp = temp2;
684 }
685 }
686
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);
695
696 /* Let optimizers know that TEMP is a pointer, and if so, the
697 known alignment of that pointer. */
698 {
699 unsigned align = 0;
700 if (GET_CODE (x) == SYMBOL_REF)
701 {
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));
705 }
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)))
712 {
713 rtx s = XEXP (XEXP (x, 0), 0);
714 rtx c = XEXP (XEXP (x, 0), 1);
715 unsigned sa, ca;
716
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));
720
721 if (INTVAL (c) == 0)
722 align = sa;
723 else
724 {
725 ca = ctz_hwi (INTVAL (c)) * BITS_PER_UNIT;
726 align = MIN (sa, ca);
727 }
728 }
729
730 if (align || (MEM_P (x) && MEM_POINTER (x)))
731 mark_reg_pointer (temp, align);
732 }
733
734 return temp;
735 }
736
737 /* If X is a memory ref, copy its contents to a new temp reg and return
738 that reg. Otherwise, return X. */
739
740 rtx
741 force_not_mem (rtx x)
742 {
743 rtx temp;
744
745 if (!MEM_P (x) || GET_MODE (x) == BLKmode)
746 return x;
747
748 temp = gen_reg_rtx (GET_MODE (x));
749
750 if (MEM_POINTER (x))
751 REG_POINTER (temp) = 1;
752
753 emit_move_insn (temp, x);
754 return temp;
755 }
756
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. */
760
761 rtx
762 copy_to_suggested_reg (rtx x, rtx target, enum machine_mode mode)
763 {
764 rtx temp;
765
766 if (target && REG_P (target))
767 temp = target;
768 else
769 temp = gen_reg_rtx (mode);
770
771 emit_move_insn (temp, x);
772 return temp;
773 }
774 \f
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.
778
779 FOR_RETURN is nonzero if the caller is promoting the return value
780 of FNDECL, else it is for promoting args. */
781
782 enum machine_mode
783 promote_function_mode (const_tree type, enum machine_mode mode, int *punsignedp,
784 const_tree funtype, int for_return)
785 {
786 /* Called without a type node for a libcall. */
787 if (type == NULL_TREE)
788 {
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;
795 }
796
797 switch (TREE_CODE (type))
798 {
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);
804
805 default:
806 return mode;
807 }
808 }
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. */
812
813 enum machine_mode
814 promote_mode (const_tree type ATTRIBUTE_UNUSED, enum machine_mode mode,
815 int *punsignedp ATTRIBUTE_UNUSED)
816 {
817 #ifdef PROMOTE_MODE
818 enum tree_code code;
819 int unsignedp;
820 #endif
821
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;
827
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;
834
835 switch (code)
836 {
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;
843
844 #ifdef POINTERS_EXTEND_UNSIGNED
845 case REFERENCE_TYPE:
846 case POINTER_TYPE:
847 *punsignedp = POINTERS_EXTEND_UNSIGNED;
848 return targetm.addr_space.address_mode
849 (TYPE_ADDR_SPACE (TREE_TYPE (type)));
850 break;
851 #endif
852
853 default:
854 return mode;
855 }
856 #else
857 return mode;
858 #endif
859 }
860
861
862 /* Use one of promote_mode or promote_function_mode to find the promoted
863 mode of DECL. If PUNSIGNEDP is not NULL, store there the unsignedness
864 of DECL after promotion. */
865
866 enum machine_mode
867 promote_decl_mode (const_tree decl, int *punsignedp)
868 {
869 tree type = TREE_TYPE (decl);
870 int unsignedp = TYPE_UNSIGNED (type);
871 enum machine_mode mode = DECL_MODE (decl);
872 enum machine_mode pmode;
873
874 if (TREE_CODE (decl) == RESULT_DECL
875 || TREE_CODE (decl) == PARM_DECL)
876 pmode = promote_function_mode (type, mode, &unsignedp,
877 TREE_TYPE (current_function_decl), 2);
878 else
879 pmode = promote_mode (type, mode, &unsignedp);
880
881 if (punsignedp)
882 *punsignedp = unsignedp;
883 return pmode;
884 }
885
886 \f
887 /* Controls the behaviour of {anti_,}adjust_stack. */
888 static bool suppress_reg_args_size;
889
890 /* A helper for adjust_stack and anti_adjust_stack. */
891
892 static void
893 adjust_stack_1 (rtx adjust, bool anti_p)
894 {
895 rtx temp;
896 rtx_insn *insn;
897
898 #ifndef STACK_GROWS_DOWNWARD
899 /* Hereafter anti_p means subtract_p. */
900 anti_p = !anti_p;
901 #endif
902
903 temp = expand_binop (Pmode,
904 anti_p ? sub_optab : add_optab,
905 stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
906 OPTAB_LIB_WIDEN);
907
908 if (temp != stack_pointer_rtx)
909 insn = emit_move_insn (stack_pointer_rtx, temp);
910 else
911 {
912 insn = get_last_insn ();
913 temp = single_set (insn);
914 gcc_assert (temp != NULL && SET_DEST (temp) == stack_pointer_rtx);
915 }
916
917 if (!suppress_reg_args_size)
918 add_reg_note (insn, REG_ARGS_SIZE, GEN_INT (stack_pointer_delta));
919 }
920
921 /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
922 This pops when ADJUST is positive. ADJUST need not be constant. */
923
924 void
925 adjust_stack (rtx adjust)
926 {
927 if (adjust == const0_rtx)
928 return;
929
930 /* We expect all variable sized adjustments to be multiple of
931 PREFERRED_STACK_BOUNDARY. */
932 if (CONST_INT_P (adjust))
933 stack_pointer_delta -= INTVAL (adjust);
934
935 adjust_stack_1 (adjust, false);
936 }
937
938 /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
939 This pushes when ADJUST is positive. ADJUST need not be constant. */
940
941 void
942 anti_adjust_stack (rtx adjust)
943 {
944 if (adjust == const0_rtx)
945 return;
946
947 /* We expect all variable sized adjustments to be multiple of
948 PREFERRED_STACK_BOUNDARY. */
949 if (CONST_INT_P (adjust))
950 stack_pointer_delta += INTVAL (adjust);
951
952 adjust_stack_1 (adjust, true);
953 }
954
955 /* Round the size of a block to be pushed up to the boundary required
956 by this machine. SIZE is the desired size, which need not be constant. */
957
958 static rtx
959 round_push (rtx size)
960 {
961 rtx align_rtx, alignm1_rtx;
962
963 if (!SUPPORTS_STACK_ALIGNMENT
964 || crtl->preferred_stack_boundary == MAX_SUPPORTED_STACK_ALIGNMENT)
965 {
966 int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
967
968 if (align == 1)
969 return size;
970
971 if (CONST_INT_P (size))
972 {
973 HOST_WIDE_INT new_size = (INTVAL (size) + align - 1) / align * align;
974
975 if (INTVAL (size) != new_size)
976 size = GEN_INT (new_size);
977 return size;
978 }
979
980 align_rtx = GEN_INT (align);
981 alignm1_rtx = GEN_INT (align - 1);
982 }
983 else
984 {
985 /* If crtl->preferred_stack_boundary might still grow, use
986 virtual_preferred_stack_boundary_rtx instead. This will be
987 substituted by the right value in vregs pass and optimized
988 during combine. */
989 align_rtx = virtual_preferred_stack_boundary_rtx;
990 alignm1_rtx = force_operand (plus_constant (Pmode, align_rtx, -1),
991 NULL_RTX);
992 }
993
994 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
995 but we know it can't. So add ourselves and then do
996 TRUNC_DIV_EXPR. */
997 size = expand_binop (Pmode, add_optab, size, alignm1_rtx,
998 NULL_RTX, 1, OPTAB_LIB_WIDEN);
999 size = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, size, align_rtx,
1000 NULL_RTX, 1);
1001 size = expand_mult (Pmode, size, align_rtx, NULL_RTX, 1);
1002
1003 return size;
1004 }
1005 \f
1006 /* Save the stack pointer for the purpose in SAVE_LEVEL. PSAVE is a pointer
1007 to a previously-created save area. If no save area has been allocated,
1008 this function will allocate one. If a save area is specified, it
1009 must be of the proper mode. */
1010
1011 void
1012 emit_stack_save (enum save_level save_level, rtx *psave)
1013 {
1014 rtx sa = *psave;
1015 /* The default is that we use a move insn and save in a Pmode object. */
1016 rtx (*fcn) (rtx, rtx) = gen_move_insn;
1017 enum machine_mode mode = STACK_SAVEAREA_MODE (save_level);
1018
1019 /* See if this machine has anything special to do for this kind of save. */
1020 switch (save_level)
1021 {
1022 #ifdef HAVE_save_stack_block
1023 case SAVE_BLOCK:
1024 if (HAVE_save_stack_block)
1025 fcn = gen_save_stack_block;
1026 break;
1027 #endif
1028 #ifdef HAVE_save_stack_function
1029 case SAVE_FUNCTION:
1030 if (HAVE_save_stack_function)
1031 fcn = gen_save_stack_function;
1032 break;
1033 #endif
1034 #ifdef HAVE_save_stack_nonlocal
1035 case SAVE_NONLOCAL:
1036 if (HAVE_save_stack_nonlocal)
1037 fcn = gen_save_stack_nonlocal;
1038 break;
1039 #endif
1040 default:
1041 break;
1042 }
1043
1044 /* If there is no save area and we have to allocate one, do so. Otherwise
1045 verify the save area is the proper mode. */
1046
1047 if (sa == 0)
1048 {
1049 if (mode != VOIDmode)
1050 {
1051 if (save_level == SAVE_NONLOCAL)
1052 *psave = sa = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
1053 else
1054 *psave = sa = gen_reg_rtx (mode);
1055 }
1056 }
1057
1058 do_pending_stack_adjust ();
1059 if (sa != 0)
1060 sa = validize_mem (sa);
1061 emit_insn (fcn (sa, stack_pointer_rtx));
1062 }
1063
1064 /* Restore the stack pointer for the purpose in SAVE_LEVEL. SA is the save
1065 area made by emit_stack_save. If it is zero, we have nothing to do. */
1066
1067 void
1068 emit_stack_restore (enum save_level save_level, rtx sa)
1069 {
1070 /* The default is that we use a move insn. */
1071 rtx (*fcn) (rtx, rtx) = gen_move_insn;
1072
1073 /* If stack_realign_drap, the x86 backend emits a prologue that aligns both
1074 STACK_POINTER and HARD_FRAME_POINTER.
1075 If stack_realign_fp, the x86 backend emits a prologue that aligns only
1076 STACK_POINTER. This renders the HARD_FRAME_POINTER unusable for accessing
1077 aligned variables, which is reflected in ix86_can_eliminate.
1078 We normally still have the realigned STACK_POINTER that we can use.
1079 But if there is a stack restore still present at reload, it can trigger
1080 mark_not_eliminable for the STACK_POINTER, leaving no way to eliminate
1081 FRAME_POINTER into a hard reg.
1082 To prevent this situation, we force need_drap if we emit a stack
1083 restore. */
1084 if (SUPPORTS_STACK_ALIGNMENT)
1085 crtl->need_drap = true;
1086
1087 /* See if this machine has anything special to do for this kind of save. */
1088 switch (save_level)
1089 {
1090 #ifdef HAVE_restore_stack_block
1091 case SAVE_BLOCK:
1092 if (HAVE_restore_stack_block)
1093 fcn = gen_restore_stack_block;
1094 break;
1095 #endif
1096 #ifdef HAVE_restore_stack_function
1097 case SAVE_FUNCTION:
1098 if (HAVE_restore_stack_function)
1099 fcn = gen_restore_stack_function;
1100 break;
1101 #endif
1102 #ifdef HAVE_restore_stack_nonlocal
1103 case SAVE_NONLOCAL:
1104 if (HAVE_restore_stack_nonlocal)
1105 fcn = gen_restore_stack_nonlocal;
1106 break;
1107 #endif
1108 default:
1109 break;
1110 }
1111
1112 if (sa != 0)
1113 {
1114 sa = validize_mem (sa);
1115 /* These clobbers prevent the scheduler from moving
1116 references to variable arrays below the code
1117 that deletes (pops) the arrays. */
1118 emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1119 emit_clobber (gen_rtx_MEM (BLKmode, stack_pointer_rtx));
1120 }
1121
1122 discard_pending_stack_adjust ();
1123
1124 emit_insn (fcn (stack_pointer_rtx, sa));
1125 }
1126
1127 /* Invoke emit_stack_save on the nonlocal_goto_save_area for the current
1128 function. This function should be called whenever we allocate or
1129 deallocate dynamic stack space. */
1130
1131 void
1132 update_nonlocal_goto_save_area (void)
1133 {
1134 tree t_save;
1135 rtx r_save;
1136
1137 /* The nonlocal_goto_save_area object is an array of N pointers. The
1138 first one is used for the frame pointer save; the rest are sized by
1139 STACK_SAVEAREA_MODE. Create a reference to array index 1, the first
1140 of the stack save area slots. */
1141 t_save = build4 (ARRAY_REF,
1142 TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
1143 cfun->nonlocal_goto_save_area,
1144 integer_one_node, NULL_TREE, NULL_TREE);
1145 r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
1146
1147 emit_stack_save (SAVE_NONLOCAL, &r_save);
1148 }
1149 \f
1150 /* Return an rtx representing the address of an area of memory dynamically
1151 pushed on the stack.
1152
1153 Any required stack pointer alignment is preserved.
1154
1155 SIZE is an rtx representing the size of the area.
1156
1157 SIZE_ALIGN is the alignment (in bits) that we know SIZE has. This
1158 parameter may be zero. If so, a proper value will be extracted
1159 from SIZE if it is constant, otherwise BITS_PER_UNIT will be assumed.
1160
1161 REQUIRED_ALIGN is the alignment (in bits) required for the region
1162 of memory.
1163
1164 If CANNOT_ACCUMULATE is set to TRUE, the caller guarantees that the
1165 stack space allocated by the generated code cannot be added with itself
1166 in the course of the execution of the function. It is always safe to
1167 pass FALSE here and the following criterion is sufficient in order to
1168 pass TRUE: every path in the CFG that starts at the allocation point and
1169 loops to it executes the associated deallocation code. */
1170
1171 rtx
1172 allocate_dynamic_stack_space (rtx size, unsigned size_align,
1173 unsigned required_align, bool cannot_accumulate)
1174 {
1175 HOST_WIDE_INT stack_usage_size = -1;
1176 rtx_code_label *final_label;
1177 rtx final_target, target;
1178 unsigned extra_align = 0;
1179 bool must_align;
1180
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;
1186
1187 /* Otherwise, show we're calling alloca or equivalent. */
1188 cfun->calls_alloca = 1;
1189
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)
1194 {
1195 if (CONST_INT_P (size))
1196 stack_usage_size = INTVAL (size);
1197 else if (REG_P (size))
1198 {
1199 /* Look into the last emitted insn and see if we can deduce
1200 something for the register. */
1201 rtx_insn *insn;
1202 rtx set, note;
1203 insn = get_last_insn ();
1204 if ((set = single_set (insn)) && rtx_equal_p (SET_DEST (set), size))
1205 {
1206 if (CONST_INT_P (SET_SRC (set)))
1207 stack_usage_size = INTVAL (SET_SRC (set));
1208 else if ((note = find_reg_equal_equiv_note (insn))
1209 && CONST_INT_P (XEXP (note, 0)))
1210 stack_usage_size = INTVAL (XEXP (note, 0));
1211 }
1212 }
1213
1214 /* If the size is not constant, we can't say anything. */
1215 if (stack_usage_size == -1)
1216 {
1217 current_function_has_unbounded_dynamic_stack_size = 1;
1218 stack_usage_size = 0;
1219 }
1220 }
1221
1222 /* Ensure the size is in the proper mode. */
1223 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1224 size = convert_to_mode (Pmode, size, 1);
1225
1226 /* Adjust SIZE_ALIGN, if needed. */
1227 if (CONST_INT_P (size))
1228 {
1229 unsigned HOST_WIDE_INT lsb;
1230
1231 lsb = INTVAL (size);
1232 lsb &= -lsb;
1233
1234 /* Watch out for overflow truncating to "unsigned". */
1235 if (lsb > UINT_MAX / BITS_PER_UNIT)
1236 size_align = 1u << (HOST_BITS_PER_INT - 1);
1237 else
1238 size_align = (unsigned)lsb * BITS_PER_UNIT;
1239 }
1240 else if (size_align < BITS_PER_UNIT)
1241 size_align = BITS_PER_UNIT;
1242
1243 /* We can't attempt to minimize alignment necessary, because we don't
1244 know the final value of preferred_stack_boundary yet while executing
1245 this code. */
1246 if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
1247 crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
1248
1249 /* We will need to ensure that the address we return is aligned to
1250 REQUIRED_ALIGN. If STACK_DYNAMIC_OFFSET is defined, we don't
1251 always know its final value at this point in the compilation (it
1252 might depend on the size of the outgoing parameter lists, for
1253 example), so we must align the value to be returned in that case.
1254 (Note that STACK_DYNAMIC_OFFSET will have a default nonzero value if
1255 STACK_POINTER_OFFSET or ACCUMULATE_OUTGOING_ARGS are defined).
1256 We must also do an alignment operation on the returned value if
1257 the stack pointer alignment is less strict than REQUIRED_ALIGN.
1258
1259 If we have to align, we must leave space in SIZE for the hole
1260 that might result from the alignment operation. */
1261
1262 must_align = (crtl->preferred_stack_boundary < required_align);
1263 if (must_align)
1264 {
1265 if (required_align > PREFERRED_STACK_BOUNDARY)
1266 extra_align = PREFERRED_STACK_BOUNDARY;
1267 else if (required_align > STACK_BOUNDARY)
1268 extra_align = STACK_BOUNDARY;
1269 else
1270 extra_align = BITS_PER_UNIT;
1271 }
1272
1273 /* ??? STACK_POINTER_OFFSET is always defined now. */
1274 #if defined (STACK_DYNAMIC_OFFSET) || defined (STACK_POINTER_OFFSET)
1275 must_align = true;
1276 extra_align = BITS_PER_UNIT;
1277 #endif
1278
1279 if (must_align)
1280 {
1281 unsigned extra = (required_align - extra_align) / BITS_PER_UNIT;
1282
1283 size = plus_constant (Pmode, size, extra);
1284 size = force_operand (size, NULL_RTX);
1285
1286 if (flag_stack_usage_info)
1287 stack_usage_size += extra;
1288
1289 if (extra && size_align > extra_align)
1290 size_align = extra_align;
1291 }
1292
1293 /* Round the size to a multiple of the required stack alignment.
1294 Since the stack if presumed to be rounded before this allocation,
1295 this will maintain the required alignment.
1296
1297 If the stack grows downward, we could save an insn by subtracting
1298 SIZE from the stack pointer and then aligning the stack pointer.
1299 The problem with this is that the stack pointer may be unaligned
1300 between the execution of the subtraction and alignment insns and
1301 some machines do not allow this. Even on those that do, some
1302 signal handlers malfunction if a signal should occur between those
1303 insns. Since this is an extremely rare event, we have no reliable
1304 way of knowing which systems have this problem. So we avoid even
1305 momentarily mis-aligning the stack. */
1306 if (size_align % MAX_SUPPORTED_STACK_ALIGNMENT != 0)
1307 {
1308 size = round_push (size);
1309
1310 if (flag_stack_usage_info)
1311 {
1312 int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
1313 stack_usage_size = (stack_usage_size + align - 1) / align * align;
1314 }
1315 }
1316
1317 target = gen_reg_rtx (Pmode);
1318
1319 /* The size is supposed to be fully adjusted at this point so record it
1320 if stack usage info is requested. */
1321 if (flag_stack_usage_info)
1322 {
1323 current_function_dynamic_stack_size += stack_usage_size;
1324
1325 /* ??? This is gross but the only safe stance in the absence
1326 of stack usage oriented flow analysis. */
1327 if (!cannot_accumulate)
1328 current_function_has_unbounded_dynamic_stack_size = 1;
1329 }
1330
1331 final_label = NULL;
1332 final_target = NULL_RTX;
1333
1334 /* If we are splitting the stack, we need to ask the backend whether
1335 there is enough room on the current stack. If there isn't, or if
1336 the backend doesn't know how to tell is, then we need to call a
1337 function to allocate memory in some other way. This memory will
1338 be released when we release the current stack segment. The
1339 effect is that stack allocation becomes less efficient, but at
1340 least it doesn't cause a stack overflow. */
1341 if (flag_split_stack)
1342 {
1343 rtx_code_label *available_label;
1344 rtx ask, space, func;
1345
1346 available_label = NULL;
1347
1348 #ifdef HAVE_split_stack_space_check
1349 if (HAVE_split_stack_space_check)
1350 {
1351 available_label = gen_label_rtx ();
1352
1353 /* This instruction will branch to AVAILABLE_LABEL if there
1354 are SIZE bytes available on the stack. */
1355 emit_insn (gen_split_stack_space_check (size, available_label));
1356 }
1357 #endif
1358
1359 /* The __morestack_allocate_stack_space function will allocate
1360 memory using malloc. If the alignment of the memory returned
1361 by malloc does not meet REQUIRED_ALIGN, we increase SIZE to
1362 make sure we allocate enough space. */
1363 if (MALLOC_ABI_ALIGNMENT >= required_align)
1364 ask = size;
1365 else
1366 {
1367 ask = expand_binop (Pmode, add_optab, size,
1368 gen_int_mode (required_align / BITS_PER_UNIT - 1,
1369 Pmode),
1370 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1371 must_align = true;
1372 }
1373
1374 func = init_one_libfunc ("__morestack_allocate_stack_space");
1375
1376 space = emit_library_call_value (func, target, LCT_NORMAL, Pmode,
1377 1, ask, Pmode);
1378
1379 if (available_label == NULL_RTX)
1380 return space;
1381
1382 final_target = gen_reg_rtx (Pmode);
1383
1384 emit_move_insn (final_target, space);
1385
1386 final_label = gen_label_rtx ();
1387 emit_jump (final_label);
1388
1389 emit_label (available_label);
1390 }
1391
1392 do_pending_stack_adjust ();
1393
1394 /* We ought to be called always on the toplevel and stack ought to be aligned
1395 properly. */
1396 gcc_assert (!(stack_pointer_delta
1397 % (PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT)));
1398
1399 /* If needed, check that we have the required amount of stack. Take into
1400 account what has already been checked. */
1401 if (STACK_CHECK_MOVING_SP)
1402 ;
1403 else if (flag_stack_check == GENERIC_STACK_CHECK)
1404 probe_stack_range (STACK_OLD_CHECK_PROTECT + STACK_CHECK_MAX_FRAME_SIZE,
1405 size);
1406 else if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
1407 probe_stack_range (STACK_CHECK_PROTECT, size);
1408
1409 /* Don't let anti_adjust_stack emit notes. */
1410 suppress_reg_args_size = true;
1411
1412 /* Perform the required allocation from the stack. Some systems do
1413 this differently than simply incrementing/decrementing from the
1414 stack pointer, such as acquiring the space by calling malloc(). */
1415 #ifdef HAVE_allocate_stack
1416 if (HAVE_allocate_stack)
1417 {
1418 struct expand_operand ops[2];
1419 /* We don't have to check against the predicate for operand 0 since
1420 TARGET is known to be a pseudo of the proper mode, which must
1421 be valid for the operand. */
1422 create_fixed_operand (&ops[0], target);
1423 create_convert_operand_to (&ops[1], size, STACK_SIZE_MODE, true);
1424 expand_insn (CODE_FOR_allocate_stack, 2, ops);
1425 }
1426 else
1427 #endif
1428 {
1429 int saved_stack_pointer_delta;
1430
1431 #ifndef STACK_GROWS_DOWNWARD
1432 emit_move_insn (target, virtual_stack_dynamic_rtx);
1433 #endif
1434
1435 /* Check stack bounds if necessary. */
1436 if (crtl->limit_stack)
1437 {
1438 rtx available;
1439 rtx_code_label *space_available = gen_label_rtx ();
1440 #ifdef STACK_GROWS_DOWNWARD
1441 available = expand_binop (Pmode, sub_optab,
1442 stack_pointer_rtx, stack_limit_rtx,
1443 NULL_RTX, 1, OPTAB_WIDEN);
1444 #else
1445 available = expand_binop (Pmode, sub_optab,
1446 stack_limit_rtx, stack_pointer_rtx,
1447 NULL_RTX, 1, OPTAB_WIDEN);
1448 #endif
1449 emit_cmp_and_jump_insns (available, size, GEU, NULL_RTX, Pmode, 1,
1450 space_available);
1451 #ifdef HAVE_trap
1452 if (HAVE_trap)
1453 emit_insn (gen_trap ());
1454 else
1455 #endif
1456 error ("stack limits not supported on this target");
1457 emit_barrier ();
1458 emit_label (space_available);
1459 }
1460
1461 saved_stack_pointer_delta = stack_pointer_delta;
1462
1463 if (flag_stack_check && STACK_CHECK_MOVING_SP)
1464 anti_adjust_stack_and_probe (size, false);
1465 else
1466 anti_adjust_stack (size);
1467
1468 /* Even if size is constant, don't modify stack_pointer_delta.
1469 The constant size alloca should preserve
1470 crtl->preferred_stack_boundary alignment. */
1471 stack_pointer_delta = saved_stack_pointer_delta;
1472
1473 #ifdef STACK_GROWS_DOWNWARD
1474 emit_move_insn (target, virtual_stack_dynamic_rtx);
1475 #endif
1476 }
1477
1478 suppress_reg_args_size = false;
1479
1480 /* Finish up the split stack handling. */
1481 if (final_label != NULL_RTX)
1482 {
1483 gcc_assert (flag_split_stack);
1484 emit_move_insn (final_target, target);
1485 emit_label (final_label);
1486 target = final_target;
1487 }
1488
1489 if (must_align)
1490 {
1491 /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1492 but we know it can't. So add ourselves and then do
1493 TRUNC_DIV_EXPR. */
1494 target = expand_binop (Pmode, add_optab, target,
1495 gen_int_mode (required_align / BITS_PER_UNIT - 1,
1496 Pmode),
1497 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1498 target = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, target,
1499 gen_int_mode (required_align / BITS_PER_UNIT,
1500 Pmode),
1501 NULL_RTX, 1);
1502 target = expand_mult (Pmode, target,
1503 gen_int_mode (required_align / BITS_PER_UNIT,
1504 Pmode),
1505 NULL_RTX, 1);
1506 }
1507
1508 /* Now that we've committed to a return value, mark its alignment. */
1509 mark_reg_pointer (target, required_align);
1510
1511 /* Record the new stack level for nonlocal gotos. */
1512 if (cfun->nonlocal_goto_save_area != 0)
1513 update_nonlocal_goto_save_area ();
1514
1515 return target;
1516 }
1517 \f
1518 /* A front end may want to override GCC's stack checking by providing a
1519 run-time routine to call to check the stack, so provide a mechanism for
1520 calling that routine. */
1521
1522 static GTY(()) rtx stack_check_libfunc;
1523
1524 void
1525 set_stack_check_libfunc (const char *libfunc_name)
1526 {
1527 gcc_assert (stack_check_libfunc == NULL_RTX);
1528 stack_check_libfunc = gen_rtx_SYMBOL_REF (Pmode, libfunc_name);
1529 }
1530 \f
1531 /* Emit one stack probe at ADDRESS, an address within the stack. */
1532
1533 void
1534 emit_stack_probe (rtx address)
1535 {
1536 #ifdef HAVE_probe_stack_address
1537 if (HAVE_probe_stack_address)
1538 emit_insn (gen_probe_stack_address (address));
1539 else
1540 #endif
1541 {
1542 rtx memref = gen_rtx_MEM (word_mode, address);
1543
1544 MEM_VOLATILE_P (memref) = 1;
1545
1546 /* See if we have an insn to probe the stack. */
1547 #ifdef HAVE_probe_stack
1548 if (HAVE_probe_stack)
1549 emit_insn (gen_probe_stack (memref));
1550 else
1551 #endif
1552 emit_move_insn (memref, const0_rtx);
1553 }
1554 }
1555
1556 /* Probe a range of stack addresses from FIRST to FIRST+SIZE, inclusive.
1557 FIRST is a constant and size is a Pmode RTX. These are offsets from
1558 the current stack pointer. STACK_GROWS_DOWNWARD says whether to add
1559 or subtract them from the stack pointer. */
1560
1561 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
1562
1563 #ifdef STACK_GROWS_DOWNWARD
1564 #define STACK_GROW_OP MINUS
1565 #define STACK_GROW_OPTAB sub_optab
1566 #define STACK_GROW_OFF(off) -(off)
1567 #else
1568 #define STACK_GROW_OP PLUS
1569 #define STACK_GROW_OPTAB add_optab
1570 #define STACK_GROW_OFF(off) (off)
1571 #endif
1572
1573 void
1574 probe_stack_range (HOST_WIDE_INT first, rtx size)
1575 {
1576 /* First ensure SIZE is Pmode. */
1577 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1578 size = convert_to_mode (Pmode, size, 1);
1579
1580 /* Next see if we have a function to check the stack. */
1581 if (stack_check_libfunc)
1582 {
1583 rtx addr = memory_address (Pmode,
1584 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1585 stack_pointer_rtx,
1586 plus_constant (Pmode,
1587 size, first)));
1588 emit_library_call (stack_check_libfunc, LCT_NORMAL, VOIDmode, 1, addr,
1589 Pmode);
1590 }
1591
1592 /* Next see if we have an insn to check the stack. */
1593 #ifdef HAVE_check_stack
1594 else if (HAVE_check_stack)
1595 {
1596 struct expand_operand ops[1];
1597 rtx addr = memory_address (Pmode,
1598 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1599 stack_pointer_rtx,
1600 plus_constant (Pmode,
1601 size, first)));
1602 bool success;
1603 create_input_operand (&ops[0], addr, Pmode);
1604 success = maybe_expand_insn (CODE_FOR_check_stack, 1, ops);
1605 gcc_assert (success);
1606 }
1607 #endif
1608
1609 /* Otherwise we have to generate explicit probes. If we have a constant
1610 small number of them to generate, that's the easy case. */
1611 else if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
1612 {
1613 HOST_WIDE_INT isize = INTVAL (size), i;
1614 rtx addr;
1615
1616 /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
1617 it exceeds SIZE. If only one probe is needed, this will not
1618 generate any code. Then probe at FIRST + SIZE. */
1619 for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
1620 {
1621 addr = memory_address (Pmode,
1622 plus_constant (Pmode, stack_pointer_rtx,
1623 STACK_GROW_OFF (first + i)));
1624 emit_stack_probe (addr);
1625 }
1626
1627 addr = memory_address (Pmode,
1628 plus_constant (Pmode, stack_pointer_rtx,
1629 STACK_GROW_OFF (first + isize)));
1630 emit_stack_probe (addr);
1631 }
1632
1633 /* In the variable case, do the same as above, but in a loop. Note that we
1634 must be extra careful with variables wrapping around because we might be
1635 at the very top (or the very bottom) of the address space and we have to
1636 be able to handle this case properly; in particular, we use an equality
1637 test for the loop condition. */
1638 else
1639 {
1640 rtx rounded_size, rounded_size_op, test_addr, last_addr, temp;
1641 rtx_code_label *loop_lab = gen_label_rtx ();
1642 rtx_code_label *end_lab = gen_label_rtx ();
1643
1644 /* Step 1: round SIZE to the previous multiple of the interval. */
1645
1646 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1647 rounded_size
1648 = simplify_gen_binary (AND, Pmode, size,
1649 gen_int_mode (-PROBE_INTERVAL, Pmode));
1650 rounded_size_op = force_operand (rounded_size, NULL_RTX);
1651
1652
1653 /* Step 2: compute initial and final value of the loop counter. */
1654
1655 /* TEST_ADDR = SP + FIRST. */
1656 test_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1657 stack_pointer_rtx,
1658 gen_int_mode (first, Pmode)),
1659 NULL_RTX);
1660
1661 /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE. */
1662 last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1663 test_addr,
1664 rounded_size_op), NULL_RTX);
1665
1666
1667 /* Step 3: the loop
1668
1669 while (TEST_ADDR != LAST_ADDR)
1670 {
1671 TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
1672 probe at TEST_ADDR
1673 }
1674
1675 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
1676 until it is equal to ROUNDED_SIZE. */
1677
1678 emit_label (loop_lab);
1679
1680 /* Jump to END_LAB if TEST_ADDR == LAST_ADDR. */
1681 emit_cmp_and_jump_insns (test_addr, last_addr, EQ, NULL_RTX, Pmode, 1,
1682 end_lab);
1683
1684 /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL. */
1685 temp = expand_binop (Pmode, STACK_GROW_OPTAB, test_addr,
1686 gen_int_mode (PROBE_INTERVAL, Pmode), test_addr,
1687 1, OPTAB_WIDEN);
1688
1689 gcc_assert (temp == test_addr);
1690
1691 /* Probe at TEST_ADDR. */
1692 emit_stack_probe (test_addr);
1693
1694 emit_jump (loop_lab);
1695
1696 emit_label (end_lab);
1697
1698
1699 /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
1700 that SIZE is equal to ROUNDED_SIZE. */
1701
1702 /* TEMP = SIZE - ROUNDED_SIZE. */
1703 temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
1704 if (temp != const0_rtx)
1705 {
1706 rtx addr;
1707
1708 if (CONST_INT_P (temp))
1709 {
1710 /* Use [base + disp} addressing mode if supported. */
1711 HOST_WIDE_INT offset = INTVAL (temp);
1712 addr = memory_address (Pmode,
1713 plus_constant (Pmode, last_addr,
1714 STACK_GROW_OFF (offset)));
1715 }
1716 else
1717 {
1718 /* Manual CSE if the difference is not known at compile-time. */
1719 temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
1720 addr = memory_address (Pmode,
1721 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1722 last_addr, temp));
1723 }
1724
1725 emit_stack_probe (addr);
1726 }
1727 }
1728
1729 /* Make sure nothing is scheduled before we are done. */
1730 emit_insn (gen_blockage ());
1731 }
1732
1733 /* Adjust the stack pointer by minus SIZE (an rtx for a number of bytes)
1734 while probing it. This pushes when SIZE is positive. SIZE need not
1735 be constant. If ADJUST_BACK is true, adjust back the stack pointer
1736 by plus SIZE at the end. */
1737
1738 void
1739 anti_adjust_stack_and_probe (rtx size, bool adjust_back)
1740 {
1741 /* We skip the probe for the first interval + a small dope of 4 words and
1742 probe that many bytes past the specified size to maintain a protection
1743 area at the botton of the stack. */
1744 const int dope = 4 * UNITS_PER_WORD;
1745
1746 /* First ensure SIZE is Pmode. */
1747 if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1748 size = convert_to_mode (Pmode, size, 1);
1749
1750 /* If we have a constant small number of probes to generate, that's the
1751 easy case. */
1752 if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
1753 {
1754 HOST_WIDE_INT isize = INTVAL (size), i;
1755 bool first_probe = true;
1756
1757 /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
1758 values of N from 1 until it exceeds SIZE. If only one probe is
1759 needed, this will not generate any code. Then adjust and probe
1760 to PROBE_INTERVAL + SIZE. */
1761 for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
1762 {
1763 if (first_probe)
1764 {
1765 anti_adjust_stack (GEN_INT (2 * PROBE_INTERVAL + dope));
1766 first_probe = false;
1767 }
1768 else
1769 anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
1770 emit_stack_probe (stack_pointer_rtx);
1771 }
1772
1773 if (first_probe)
1774 anti_adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL + dope));
1775 else
1776 anti_adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL - i));
1777 emit_stack_probe (stack_pointer_rtx);
1778 }
1779
1780 /* In the variable case, do the same as above, but in a loop. Note that we
1781 must be extra careful with variables wrapping around because we might be
1782 at the very top (or the very bottom) of the address space and we have to
1783 be able to handle this case properly; in particular, we use an equality
1784 test for the loop condition. */
1785 else
1786 {
1787 rtx rounded_size, rounded_size_op, last_addr, temp;
1788 rtx_code_label *loop_lab = gen_label_rtx ();
1789 rtx_code_label *end_lab = gen_label_rtx ();
1790
1791
1792 /* Step 1: round SIZE to the previous multiple of the interval. */
1793
1794 /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL */
1795 rounded_size
1796 = simplify_gen_binary (AND, Pmode, size,
1797 gen_int_mode (-PROBE_INTERVAL, Pmode));
1798 rounded_size_op = force_operand (rounded_size, NULL_RTX);
1799
1800
1801 /* Step 2: compute initial and final value of the loop counter. */
1802
1803 /* SP = SP_0 + PROBE_INTERVAL. */
1804 anti_adjust_stack (GEN_INT (PROBE_INTERVAL + dope));
1805
1806 /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE. */
1807 last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1808 stack_pointer_rtx,
1809 rounded_size_op), NULL_RTX);
1810
1811
1812 /* Step 3: the loop
1813
1814 while (SP != LAST_ADDR)
1815 {
1816 SP = SP + PROBE_INTERVAL
1817 probe at SP
1818 }
1819
1820 adjusts SP and probes at PROBE_INTERVAL + N * PROBE_INTERVAL for
1821 values of N from 1 until it is equal to ROUNDED_SIZE. */
1822
1823 emit_label (loop_lab);
1824
1825 /* Jump to END_LAB if SP == LAST_ADDR. */
1826 emit_cmp_and_jump_insns (stack_pointer_rtx, last_addr, EQ, NULL_RTX,
1827 Pmode, 1, end_lab);
1828
1829 /* SP = SP + PROBE_INTERVAL and probe at SP. */
1830 anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
1831 emit_stack_probe (stack_pointer_rtx);
1832
1833 emit_jump (loop_lab);
1834
1835 emit_label (end_lab);
1836
1837
1838 /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
1839 assert at compile-time that SIZE is equal to ROUNDED_SIZE. */
1840
1841 /* TEMP = SIZE - ROUNDED_SIZE. */
1842 temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
1843 if (temp != const0_rtx)
1844 {
1845 /* Manual CSE if the difference is not known at compile-time. */
1846 if (GET_CODE (temp) != CONST_INT)
1847 temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
1848 anti_adjust_stack (temp);
1849 emit_stack_probe (stack_pointer_rtx);
1850 }
1851 }
1852
1853 /* Adjust back and account for the additional first interval. */
1854 if (adjust_back)
1855 adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL + dope));
1856 else
1857 adjust_stack (GEN_INT (PROBE_INTERVAL + dope));
1858 }
1859
1860 /* Return an rtx representing the register or memory location
1861 in which a scalar value of data type VALTYPE
1862 was returned by a function call to function FUNC.
1863 FUNC is a FUNCTION_DECL, FNTYPE a FUNCTION_TYPE node if the precise
1864 function is known, otherwise 0.
1865 OUTGOING is 1 if on a machine with register windows this function
1866 should return the register in which the function will put its result
1867 and 0 otherwise. */
1868
1869 rtx
1870 hard_function_value (const_tree valtype, const_tree func, const_tree fntype,
1871 int outgoing ATTRIBUTE_UNUSED)
1872 {
1873 rtx val;
1874
1875 val = targetm.calls.function_value (valtype, func ? func : fntype, outgoing);
1876
1877 if (REG_P (val)
1878 && GET_MODE (val) == BLKmode)
1879 {
1880 unsigned HOST_WIDE_INT bytes = int_size_in_bytes (valtype);
1881 enum machine_mode tmpmode;
1882
1883 /* int_size_in_bytes can return -1. We don't need a check here
1884 since the value of bytes will then be large enough that no
1885 mode will match anyway. */
1886
1887 for (tmpmode = GET_CLASS_NARROWEST_MODE (MODE_INT);
1888 tmpmode != VOIDmode;
1889 tmpmode = GET_MODE_WIDER_MODE (tmpmode))
1890 {
1891 /* Have we found a large enough mode? */
1892 if (GET_MODE_SIZE (tmpmode) >= bytes)
1893 break;
1894 }
1895
1896 /* No suitable mode found. */
1897 gcc_assert (tmpmode != VOIDmode);
1898
1899 PUT_MODE (val, tmpmode);
1900 }
1901 return val;
1902 }
1903
1904 /* Return an rtx representing the register or memory location
1905 in which a scalar value of mode MODE was returned by a library call. */
1906
1907 rtx
1908 hard_libcall_value (enum machine_mode mode, rtx fun)
1909 {
1910 return targetm.calls.libcall_value (mode, fun);
1911 }
1912
1913 /* Look up the tree code for a given rtx code
1914 to provide the arithmetic operation for REAL_ARITHMETIC.
1915 The function returns an int because the caller may not know
1916 what `enum tree_code' means. */
1917
1918 int
1919 rtx_to_tree_code (enum rtx_code code)
1920 {
1921 enum tree_code tcode;
1922
1923 switch (code)
1924 {
1925 case PLUS:
1926 tcode = PLUS_EXPR;
1927 break;
1928 case MINUS:
1929 tcode = MINUS_EXPR;
1930 break;
1931 case MULT:
1932 tcode = MULT_EXPR;
1933 break;
1934 case DIV:
1935 tcode = RDIV_EXPR;
1936 break;
1937 case SMIN:
1938 tcode = MIN_EXPR;
1939 break;
1940 case SMAX:
1941 tcode = MAX_EXPR;
1942 break;
1943 default:
1944 tcode = LAST_AND_UNUSED_TREE_CODE;
1945 break;
1946 }
1947 return ((int) tcode);
1948 }
1949
1950 #include "gt-explow.h"