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