xref: /netbsd-src/external/gpl3/gcc.old/dist/gcc/explow.c (revision 8feb0f0b7eaff0608f8350bbfa3098827b4bb91b)
1 /* Subroutines for manipulating rtx's in semantically interesting ways.
2    Copyright (C) 1987-2020 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 "memmodel.h"
29 #include "tm_p.h"
30 #include "expmed.h"
31 #include "profile-count.h"
32 #include "optabs.h"
33 #include "emit-rtl.h"
34 #include "recog.h"
35 #include "diagnostic-core.h"
36 #include "stor-layout.h"
37 #include "langhooks.h"
38 #include "except.h"
39 #include "dojump.h"
40 #include "explow.h"
41 #include "expr.h"
42 #include "stringpool.h"
43 #include "common/common-target.h"
44 #include "output.h"
45 
46 static rtx break_out_memory_refs (rtx);
47 static void anti_adjust_stack_and_probe_stack_clash (rtx);
48 
49 
50 /* Truncate and perhaps sign-extend C as appropriate for MODE.  */
51 
52 HOST_WIDE_INT
trunc_int_for_mode(HOST_WIDE_INT c,machine_mode mode)53 trunc_int_for_mode (HOST_WIDE_INT c, machine_mode mode)
54 {
55   /* Not scalar_int_mode because we also allow pointer bound modes.  */
56   scalar_mode smode = as_a <scalar_mode> (mode);
57   int width = GET_MODE_PRECISION (smode);
58 
59   /* You want to truncate to a _what_?  */
60   gcc_assert (SCALAR_INT_MODE_P (mode));
61 
62   /* Canonicalize BImode to 0 and STORE_FLAG_VALUE.  */
63   if (smode == BImode)
64     return c & 1 ? STORE_FLAG_VALUE : 0;
65 
66   /* Sign-extend for the requested mode.  */
67 
68   if (width < HOST_BITS_PER_WIDE_INT)
69     {
70       HOST_WIDE_INT sign = 1;
71       sign <<= width - 1;
72       c &= (sign << 1) - 1;
73       c ^= sign;
74       c -= sign;
75     }
76 
77   return c;
78 }
79 
80 /* Likewise for polynomial values, using the sign-extended representation
81    for each individual coefficient.  */
82 
83 poly_int64
trunc_int_for_mode(poly_int64 x,machine_mode mode)84 trunc_int_for_mode (poly_int64 x, machine_mode mode)
85 {
86   for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
87     x.coeffs[i] = trunc_int_for_mode (x.coeffs[i], mode);
88   return x;
89 }
90 
91 /* Return an rtx for the sum of X and the integer C, given that X has
92    mode MODE.  INPLACE is true if X can be modified inplace or false
93    if it must be treated as immutable.  */
94 
95 rtx
plus_constant(machine_mode mode,rtx x,poly_int64 c,bool inplace)96 plus_constant (machine_mode mode, rtx x, poly_int64 c, bool inplace)
97 {
98   RTX_CODE code;
99   rtx y;
100   rtx tem;
101   int all_constant = 0;
102 
103   gcc_assert (GET_MODE (x) == VOIDmode || GET_MODE (x) == mode);
104 
105   if (known_eq (c, 0))
106     return x;
107 
108  restart:
109 
110   code = GET_CODE (x);
111   y = x;
112 
113   switch (code)
114     {
115     CASE_CONST_SCALAR_INT:
116       return immed_wide_int_const (wi::add (rtx_mode_t (x, mode), c), mode);
117     case MEM:
118       /* If this is a reference to the constant pool, try replacing it with
119 	 a reference to a new constant.  If the resulting address isn't
120 	 valid, don't return it because we have no way to validize it.  */
121       if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
122 	  && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
123 	{
124 	  rtx cst = get_pool_constant (XEXP (x, 0));
125 
126 	  if (GET_CODE (cst) == CONST_VECTOR
127 	      && GET_MODE_INNER (GET_MODE (cst)) == mode)
128 	    {
129 	      cst = gen_lowpart (mode, cst);
130 	      gcc_assert (cst);
131 	    }
132 	  else if (GET_MODE (cst) == VOIDmode
133 		   && get_pool_mode (XEXP (x, 0)) != mode)
134 	    break;
135 	  if (GET_MODE (cst) == VOIDmode || GET_MODE (cst) == mode)
136 	    {
137 	      tem = plus_constant (mode, cst, c);
138 	      tem = force_const_mem (GET_MODE (x), tem);
139 	      /* Targets may disallow some constants in the constant pool, thus
140 		 force_const_mem may return NULL_RTX.  */
141 	      if (tem && memory_address_p (GET_MODE (tem), XEXP (tem, 0)))
142 		return tem;
143 	    }
144 	}
145       break;
146 
147     case CONST:
148       /* If adding to something entirely constant, set a flag
149 	 so that we can add a CONST around the result.  */
150       if (inplace && shared_const_p (x))
151 	inplace = false;
152       x = XEXP (x, 0);
153       all_constant = 1;
154       goto restart;
155 
156     case SYMBOL_REF:
157     case LABEL_REF:
158       all_constant = 1;
159       break;
160 
161     case PLUS:
162       /* The interesting case is adding the integer to a sum.  Look
163 	 for constant term in the sum and combine with C.  For an
164 	 integer constant term or a constant term that is not an
165 	 explicit integer, we combine or group them together anyway.
166 
167 	 We may not immediately return from the recursive call here, lest
168 	 all_constant gets lost.  */
169 
170       if (CONSTANT_P (XEXP (x, 1)))
171 	{
172 	  rtx term = plus_constant (mode, XEXP (x, 1), c, inplace);
173 	  if (term == const0_rtx)
174 	    x = XEXP (x, 0);
175 	  else if (inplace)
176 	    XEXP (x, 1) = term;
177 	  else
178 	    x = gen_rtx_PLUS (mode, XEXP (x, 0), term);
179 	  c = 0;
180 	}
181       else if (rtx *const_loc = find_constant_term_loc (&y))
182 	{
183 	  if (!inplace)
184 	    {
185 	      /* We need to be careful since X may be shared and we can't
186 		 modify it in place.  */
187 	      x = copy_rtx (x);
188 	      const_loc = find_constant_term_loc (&x);
189 	    }
190 	  *const_loc = plus_constant (mode, *const_loc, c, true);
191 	  c = 0;
192 	}
193       break;
194 
195     default:
196       if (CONST_POLY_INT_P (x))
197 	return immed_wide_int_const (const_poly_int_value (x) + c, mode);
198       break;
199     }
200 
201   if (maybe_ne (c, 0))
202     x = gen_rtx_PLUS (mode, x, gen_int_mode (c, mode));
203 
204   if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
205     return x;
206   else if (all_constant)
207     return gen_rtx_CONST (mode, x);
208   else
209     return x;
210 }
211 
212 /* If X is a sum, return a new sum like X but lacking any constant terms.
213    Add all the removed constant terms into *CONSTPTR.
214    X itself is not altered.  The result != X if and only if
215    it is not isomorphic to X.  */
216 
217 rtx
eliminate_constant_term(rtx x,rtx * constptr)218 eliminate_constant_term (rtx x, rtx *constptr)
219 {
220   rtx x0, x1;
221   rtx tem;
222 
223   if (GET_CODE (x) != PLUS)
224     return x;
225 
226   /* First handle constants appearing at this level explicitly.  */
227   if (CONST_INT_P (XEXP (x, 1))
228       && (tem = simplify_binary_operation (PLUS, GET_MODE (x), *constptr,
229 					   XEXP (x, 1))) != 0
230       && CONST_INT_P (tem))
231     {
232       *constptr = tem;
233       return eliminate_constant_term (XEXP (x, 0), constptr);
234     }
235 
236   tem = const0_rtx;
237   x0 = eliminate_constant_term (XEXP (x, 0), &tem);
238   x1 = eliminate_constant_term (XEXP (x, 1), &tem);
239   if ((x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
240       && (tem = simplify_binary_operation (PLUS, GET_MODE (x),
241 					   *constptr, tem)) != 0
242       && CONST_INT_P (tem))
243     {
244       *constptr = tem;
245       return gen_rtx_PLUS (GET_MODE (x), x0, x1);
246     }
247 
248   return x;
249 }
250 
251 
252 /* Return a copy of X in which all memory references
253    and all constants that involve symbol refs
254    have been replaced with new temporary registers.
255    Also emit code to load the memory locations and constants
256    into those registers.
257 
258    If X contains no such constants or memory references,
259    X itself (not a copy) is returned.
260 
261    If a constant is found in the address that is not a legitimate constant
262    in an insn, it is left alone in the hope that it might be valid in the
263    address.
264 
265    X may contain no arithmetic except addition, subtraction and multiplication.
266    Values returned by expand_expr with 1 for sum_ok fit this constraint.  */
267 
268 static rtx
break_out_memory_refs(rtx x)269 break_out_memory_refs (rtx x)
270 {
271   if (MEM_P (x)
272       || (CONSTANT_P (x) && CONSTANT_ADDRESS_P (x)
273 	  && GET_MODE (x) != VOIDmode))
274     x = force_reg (GET_MODE (x), x);
275   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
276 	   || GET_CODE (x) == MULT)
277     {
278       rtx op0 = break_out_memory_refs (XEXP (x, 0));
279       rtx op1 = break_out_memory_refs (XEXP (x, 1));
280 
281       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
282 	x = simplify_gen_binary (GET_CODE (x), GET_MODE (x), op0, op1);
283     }
284 
285   return x;
286 }
287 
288 /* Given X, a memory address in address space AS' pointer mode, convert it to
289    an address in the address space's address mode, or vice versa (TO_MODE says
290    which way).  We take advantage of the fact that pointers are not allowed to
291    overflow by commuting arithmetic operations over conversions so that address
292    arithmetic insns can be used. IN_CONST is true if this conversion is inside
293    a CONST. NO_EMIT is true if no insns should be emitted, and instead
294    it should return NULL if it can't be simplified without emitting insns.  */
295 
296 rtx
convert_memory_address_addr_space_1(scalar_int_mode to_mode ATTRIBUTE_UNUSED,rtx x,addr_space_t as ATTRIBUTE_UNUSED,bool in_const ATTRIBUTE_UNUSED,bool no_emit ATTRIBUTE_UNUSED)297 convert_memory_address_addr_space_1 (scalar_int_mode to_mode ATTRIBUTE_UNUSED,
298 				     rtx x, addr_space_t as ATTRIBUTE_UNUSED,
299 				     bool in_const ATTRIBUTE_UNUSED,
300 				     bool no_emit ATTRIBUTE_UNUSED)
301 {
302 #ifndef POINTERS_EXTEND_UNSIGNED
303   gcc_assert (GET_MODE (x) == to_mode || GET_MODE (x) == VOIDmode);
304   return x;
305 #else /* defined(POINTERS_EXTEND_UNSIGNED) */
306   scalar_int_mode pointer_mode, address_mode, from_mode;
307   rtx temp;
308   enum rtx_code code;
309 
310   /* If X already has the right mode, just return it.  */
311   if (GET_MODE (x) == to_mode)
312     return x;
313 
314   pointer_mode = targetm.addr_space.pointer_mode (as);
315   address_mode = targetm.addr_space.address_mode (as);
316   from_mode = to_mode == pointer_mode ? address_mode : pointer_mode;
317 
318   /* Here we handle some special cases.  If none of them apply, fall through
319      to the default case.  */
320   switch (GET_CODE (x))
321     {
322     CASE_CONST_SCALAR_INT:
323       if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode))
324 	code = TRUNCATE;
325       else if (POINTERS_EXTEND_UNSIGNED < 0)
326 	break;
327       else if (POINTERS_EXTEND_UNSIGNED > 0)
328 	code = ZERO_EXTEND;
329       else
330 	code = SIGN_EXTEND;
331       temp = simplify_unary_operation (code, to_mode, x, from_mode);
332       if (temp)
333 	return temp;
334       break;
335 
336     case SUBREG:
337       if ((SUBREG_PROMOTED_VAR_P (x) || REG_POINTER (SUBREG_REG (x)))
338 	  && GET_MODE (SUBREG_REG (x)) == to_mode)
339 	return SUBREG_REG (x);
340       break;
341 
342     case LABEL_REF:
343       temp = gen_rtx_LABEL_REF (to_mode, label_ref_label (x));
344       LABEL_REF_NONLOCAL_P (temp) = LABEL_REF_NONLOCAL_P (x);
345       return temp;
346 
347     case SYMBOL_REF:
348       temp = shallow_copy_rtx (x);
349       PUT_MODE (temp, to_mode);
350       return temp;
351 
352     case CONST:
353       temp = convert_memory_address_addr_space_1 (to_mode, XEXP (x, 0), as,
354 						  true, no_emit);
355       return temp ? gen_rtx_CONST (to_mode, temp) : temp;
356 
357     case PLUS:
358     case MULT:
359       /* For addition we can safely permute the conversion and addition
360 	 operation if one operand is a constant and converting the constant
361 	 does not change it or if one operand is a constant and we are
362 	 using a ptr_extend instruction  (POINTERS_EXTEND_UNSIGNED < 0).
363 	 We can always safely permute them if we are making the address
364 	 narrower. Inside a CONST RTL, this is safe for both pointers
365 	 zero or sign extended as pointers cannot wrap. */
366       if (GET_MODE_SIZE (to_mode) < GET_MODE_SIZE (from_mode)
367 	  || (GET_CODE (x) == PLUS
368 	      && CONST_INT_P (XEXP (x, 1))
369 	      && ((in_const && POINTERS_EXTEND_UNSIGNED != 0)
370 		  || XEXP (x, 1) == convert_memory_address_addr_space_1
371 				     (to_mode, XEXP (x, 1), as, in_const,
372 				      no_emit)
373                   || POINTERS_EXTEND_UNSIGNED < 0)))
374 	{
375 	  temp = convert_memory_address_addr_space_1 (to_mode, XEXP (x, 0),
376 						      as, in_const, no_emit);
377 	  return (temp ? gen_rtx_fmt_ee (GET_CODE (x), to_mode,
378 					 temp, XEXP (x, 1))
379 		       : temp);
380 	}
381       break;
382 
383     default:
384       break;
385     }
386 
387   if (no_emit)
388     return NULL_RTX;
389 
390   return convert_modes (to_mode, from_mode,
391 			x, POINTERS_EXTEND_UNSIGNED);
392 #endif /* defined(POINTERS_EXTEND_UNSIGNED) */
393 }
394 
395 /* Given X, a memory address in address space AS' pointer mode, convert it to
396    an address in the address space's address mode, or vice versa (TO_MODE says
397    which way).  We take advantage of the fact that pointers are not allowed to
398    overflow by commuting arithmetic operations over conversions so that address
399    arithmetic insns can be used.  */
400 
401 rtx
convert_memory_address_addr_space(scalar_int_mode to_mode,rtx x,addr_space_t as)402 convert_memory_address_addr_space (scalar_int_mode to_mode, rtx x,
403 				   addr_space_t as)
404 {
405   return convert_memory_address_addr_space_1 (to_mode, x, as, false, false);
406 }
407 
408 
409 /* Return something equivalent to X but valid as a memory address for something
410    of mode MODE in the named address space AS.  When X is not itself valid,
411    this works by copying X or subexpressions of it into registers.  */
412 
413 rtx
memory_address_addr_space(machine_mode mode,rtx x,addr_space_t as)414 memory_address_addr_space (machine_mode mode, rtx x, addr_space_t as)
415 {
416   rtx oldx = x;
417   scalar_int_mode address_mode = targetm.addr_space.address_mode (as);
418 
419   x = convert_memory_address_addr_space (address_mode, x, as);
420 
421   /* By passing constant addresses through registers
422      we get a chance to cse them.  */
423   if (! cse_not_expected && CONSTANT_P (x) && CONSTANT_ADDRESS_P (x))
424     x = force_reg (address_mode, x);
425 
426   /* We get better cse by rejecting indirect addressing at this stage.
427      Let the combiner create indirect addresses where appropriate.
428      For now, generate the code so that the subexpressions useful to share
429      are visible.  But not if cse won't be done!  */
430   else
431     {
432       if (! cse_not_expected && !REG_P (x))
433 	x = break_out_memory_refs (x);
434 
435       /* At this point, any valid address is accepted.  */
436       if (memory_address_addr_space_p (mode, x, as))
437 	goto done;
438 
439       /* If it was valid before but breaking out memory refs invalidated it,
440 	 use it the old way.  */
441       if (memory_address_addr_space_p (mode, oldx, as))
442 	{
443 	  x = oldx;
444 	  goto done;
445 	}
446 
447       /* Perform machine-dependent transformations on X
448 	 in certain cases.  This is not necessary since the code
449 	 below can handle all possible cases, but machine-dependent
450 	 transformations can make better code.  */
451       {
452 	rtx orig_x = x;
453 	x = targetm.addr_space.legitimize_address (x, oldx, mode, as);
454 	if (orig_x != x && memory_address_addr_space_p (mode, x, as))
455 	  goto done;
456       }
457 
458       /* PLUS and MULT can appear in special ways
459 	 as the result of attempts to make an address usable for indexing.
460 	 Usually they are dealt with by calling force_operand, below.
461 	 But a sum containing constant terms is special
462 	 if removing them makes the sum a valid address:
463 	 then we generate that address in a register
464 	 and index off of it.  We do this because it often makes
465 	 shorter code, and because the addresses thus generated
466 	 in registers often become common subexpressions.  */
467       if (GET_CODE (x) == PLUS)
468 	{
469 	  rtx constant_term = const0_rtx;
470 	  rtx y = eliminate_constant_term (x, &constant_term);
471 	  if (constant_term == const0_rtx
472 	      || ! memory_address_addr_space_p (mode, y, as))
473 	    x = force_operand (x, NULL_RTX);
474 	  else
475 	    {
476 	      y = gen_rtx_PLUS (GET_MODE (x), copy_to_reg (y), constant_term);
477 	      if (! memory_address_addr_space_p (mode, y, as))
478 		x = force_operand (x, NULL_RTX);
479 	      else
480 		x = y;
481 	    }
482 	}
483 
484       else if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
485 	x = force_operand (x, NULL_RTX);
486 
487       /* If we have a register that's an invalid address,
488 	 it must be a hard reg of the wrong class.  Copy it to a pseudo.  */
489       else if (REG_P (x))
490 	x = copy_to_reg (x);
491 
492       /* Last resort: copy the value to a register, since
493 	 the register is a valid address.  */
494       else
495 	x = force_reg (address_mode, x);
496     }
497 
498  done:
499 
500   gcc_assert (memory_address_addr_space_p (mode, x, as));
501   /* If we didn't change the address, we are done.  Otherwise, mark
502      a reg as a pointer if we have REG or REG + CONST_INT.  */
503   if (oldx == x)
504     return x;
505   else if (REG_P (x))
506     mark_reg_pointer (x, BITS_PER_UNIT);
507   else if (GET_CODE (x) == PLUS
508 	   && REG_P (XEXP (x, 0))
509 	   && CONST_INT_P (XEXP (x, 1)))
510     mark_reg_pointer (XEXP (x, 0), BITS_PER_UNIT);
511 
512   /* OLDX may have been the address on a temporary.  Update the address
513      to indicate that X is now used.  */
514   update_temp_slot_address (oldx, x);
515 
516   return x;
517 }
518 
519 /* Convert a mem ref into one with a valid memory address.
520    Pass through anything else unchanged.  */
521 
522 rtx
validize_mem(rtx ref)523 validize_mem (rtx ref)
524 {
525   if (!MEM_P (ref))
526     return ref;
527   ref = use_anchored_address (ref);
528   if (memory_address_addr_space_p (GET_MODE (ref), XEXP (ref, 0),
529 				   MEM_ADDR_SPACE (ref)))
530     return ref;
531 
532   /* Don't alter REF itself, since that is probably a stack slot.  */
533   return replace_equiv_address (ref, XEXP (ref, 0));
534 }
535 
536 /* If X is a memory reference to a member of an object block, try rewriting
537    it to use an anchor instead.  Return the new memory reference on success
538    and the old one on failure.  */
539 
540 rtx
use_anchored_address(rtx x)541 use_anchored_address (rtx x)
542 {
543   rtx base;
544   HOST_WIDE_INT offset;
545   machine_mode mode;
546 
547   if (!flag_section_anchors)
548     return x;
549 
550   if (!MEM_P (x))
551     return x;
552 
553   /* Split the address into a base and offset.  */
554   base = XEXP (x, 0);
555   offset = 0;
556   if (GET_CODE (base) == CONST
557       && GET_CODE (XEXP (base, 0)) == PLUS
558       && CONST_INT_P (XEXP (XEXP (base, 0), 1)))
559     {
560       offset += INTVAL (XEXP (XEXP (base, 0), 1));
561       base = XEXP (XEXP (base, 0), 0);
562     }
563 
564   /* Check whether BASE is suitable for anchors.  */
565   if (GET_CODE (base) != SYMBOL_REF
566       || !SYMBOL_REF_HAS_BLOCK_INFO_P (base)
567       || SYMBOL_REF_ANCHOR_P (base)
568       || SYMBOL_REF_BLOCK (base) == NULL
569       || !targetm.use_anchors_for_symbol_p (base))
570     return x;
571 
572   /* Decide where BASE is going to be.  */
573   place_block_symbol (base);
574 
575   /* Get the anchor we need to use.  */
576   offset += SYMBOL_REF_BLOCK_OFFSET (base);
577   base = get_section_anchor (SYMBOL_REF_BLOCK (base), offset,
578 			     SYMBOL_REF_TLS_MODEL (base));
579 
580   /* Work out the offset from the anchor.  */
581   offset -= SYMBOL_REF_BLOCK_OFFSET (base);
582 
583   /* If we're going to run a CSE pass, force the anchor into a register.
584      We will then be able to reuse registers for several accesses, if the
585      target costs say that that's worthwhile.  */
586   mode = GET_MODE (base);
587   if (!cse_not_expected)
588     base = force_reg (mode, base);
589 
590   return replace_equiv_address (x, plus_constant (mode, base, offset));
591 }
592 
593 /* Copy the value or contents of X to a new temp reg and return that reg.  */
594 
595 rtx
copy_to_reg(rtx x)596 copy_to_reg (rtx x)
597 {
598   rtx temp = gen_reg_rtx (GET_MODE (x));
599 
600   /* If not an operand, must be an address with PLUS and MULT so
601      do the computation.  */
602   if (! general_operand (x, VOIDmode))
603     x = force_operand (x, temp);
604 
605   if (x != temp)
606     emit_move_insn (temp, x);
607 
608   return temp;
609 }
610 
611 /* Like copy_to_reg but always give the new register mode Pmode
612    in case X is a constant.  */
613 
614 rtx
copy_addr_to_reg(rtx x)615 copy_addr_to_reg (rtx x)
616 {
617   return copy_to_mode_reg (Pmode, x);
618 }
619 
620 /* Like copy_to_reg but always give the new register mode MODE
621    in case X is a constant.  */
622 
623 rtx
copy_to_mode_reg(machine_mode mode,rtx x)624 copy_to_mode_reg (machine_mode mode, rtx x)
625 {
626   rtx temp = gen_reg_rtx (mode);
627 
628   /* If not an operand, must be an address with PLUS and MULT so
629      do the computation.  */
630   if (! general_operand (x, VOIDmode))
631     x = force_operand (x, temp);
632 
633   gcc_assert (GET_MODE (x) == mode || GET_MODE (x) == VOIDmode);
634   if (x != temp)
635     emit_move_insn (temp, x);
636   return temp;
637 }
638 
639 /* Load X into a register if it is not already one.
640    Use mode MODE for the register.
641    X should be valid for mode MODE, but it may be a constant which
642    is valid for all integer modes; that's why caller must specify MODE.
643 
644    The caller must not alter the value in the register we return,
645    since we mark it as a "constant" register.  */
646 
647 rtx
force_reg(machine_mode mode,rtx x)648 force_reg (machine_mode mode, rtx x)
649 {
650   rtx temp, set;
651   rtx_insn *insn;
652 
653   if (REG_P (x))
654     return x;
655 
656   if (general_operand (x, mode))
657     {
658       temp = gen_reg_rtx (mode);
659       insn = emit_move_insn (temp, x);
660     }
661   else
662     {
663       temp = force_operand (x, NULL_RTX);
664       if (REG_P (temp))
665 	insn = get_last_insn ();
666       else
667 	{
668 	  rtx temp2 = gen_reg_rtx (mode);
669 	  insn = emit_move_insn (temp2, temp);
670 	  temp = temp2;
671 	}
672     }
673 
674   /* Let optimizers know that TEMP's value never changes
675      and that X can be substituted for it.  Don't get confused
676      if INSN set something else (such as a SUBREG of TEMP).  */
677   if (CONSTANT_P (x)
678       && (set = single_set (insn)) != 0
679       && SET_DEST (set) == temp
680       && ! rtx_equal_p (x, SET_SRC (set)))
681     set_unique_reg_note (insn, REG_EQUAL, x);
682 
683   /* Let optimizers know that TEMP is a pointer, and if so, the
684      known alignment of that pointer.  */
685   {
686     unsigned align = 0;
687     if (GET_CODE (x) == SYMBOL_REF)
688       {
689         align = BITS_PER_UNIT;
690 	if (SYMBOL_REF_DECL (x) && DECL_P (SYMBOL_REF_DECL (x)))
691 	  align = DECL_ALIGN (SYMBOL_REF_DECL (x));
692       }
693     else if (GET_CODE (x) == LABEL_REF)
694       align = BITS_PER_UNIT;
695     else if (GET_CODE (x) == CONST
696 	     && GET_CODE (XEXP (x, 0)) == PLUS
697 	     && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
698 	     && CONST_INT_P (XEXP (XEXP (x, 0), 1)))
699       {
700 	rtx s = XEXP (XEXP (x, 0), 0);
701 	rtx c = XEXP (XEXP (x, 0), 1);
702 	unsigned sa, ca;
703 
704 	sa = BITS_PER_UNIT;
705 	if (SYMBOL_REF_DECL (s) && DECL_P (SYMBOL_REF_DECL (s)))
706 	  sa = DECL_ALIGN (SYMBOL_REF_DECL (s));
707 
708 	if (INTVAL (c) == 0)
709 	  align = sa;
710 	else
711 	  {
712 	    ca = ctz_hwi (INTVAL (c)) * BITS_PER_UNIT;
713 	    align = MIN (sa, ca);
714 	  }
715       }
716 
717     if (align || (MEM_P (x) && MEM_POINTER (x)))
718       mark_reg_pointer (temp, align);
719   }
720 
721   return temp;
722 }
723 
724 /* If X is a memory ref, copy its contents to a new temp reg and return
725    that reg.  Otherwise, return X.  */
726 
727 rtx
force_not_mem(rtx x)728 force_not_mem (rtx x)
729 {
730   rtx temp;
731 
732   if (!MEM_P (x) || GET_MODE (x) == BLKmode)
733     return x;
734 
735   temp = gen_reg_rtx (GET_MODE (x));
736 
737   if (MEM_POINTER (x))
738     REG_POINTER (temp) = 1;
739 
740   emit_move_insn (temp, x);
741   return temp;
742 }
743 
744 /* Copy X to TARGET (if it's nonzero and a reg)
745    or to a new temp reg and return that reg.
746    MODE is the mode to use for X in case it is a constant.  */
747 
748 rtx
copy_to_suggested_reg(rtx x,rtx target,machine_mode mode)749 copy_to_suggested_reg (rtx x, rtx target, machine_mode mode)
750 {
751   rtx temp;
752 
753   if (target && REG_P (target))
754     temp = target;
755   else
756     temp = gen_reg_rtx (mode);
757 
758   emit_move_insn (temp, x);
759   return temp;
760 }
761 
762 /* Return the mode to use to pass or return a scalar of TYPE and MODE.
763    PUNSIGNEDP points to the signedness of the type and may be adjusted
764    to show what signedness to use on extension operations.
765 
766    FOR_RETURN is nonzero if the caller is promoting the return value
767    of FNDECL, else it is for promoting args.  */
768 
769 machine_mode
promote_function_mode(const_tree type,machine_mode mode,int * punsignedp,const_tree funtype,int for_return)770 promote_function_mode (const_tree type, machine_mode mode, int *punsignedp,
771 		       const_tree funtype, int for_return)
772 {
773   /* Called without a type node for a libcall.  */
774   if (type == NULL_TREE)
775     {
776       if (INTEGRAL_MODE_P (mode))
777 	return targetm.calls.promote_function_mode (NULL_TREE, mode,
778 						    punsignedp, funtype,
779 						    for_return);
780       else
781 	return mode;
782     }
783 
784   switch (TREE_CODE (type))
785     {
786     case INTEGER_TYPE:   case ENUMERAL_TYPE:   case BOOLEAN_TYPE:
787     case REAL_TYPE:      case OFFSET_TYPE:     case FIXED_POINT_TYPE:
788     case POINTER_TYPE:   case REFERENCE_TYPE:
789       return targetm.calls.promote_function_mode (type, mode, punsignedp, funtype,
790 						  for_return);
791 
792     default:
793       return mode;
794     }
795 }
796 /* Return the mode to use to store a scalar of TYPE and MODE.
797    PUNSIGNEDP points to the signedness of the type and may be adjusted
798    to show what signedness to use on extension operations.  */
799 
800 machine_mode
promote_mode(const_tree type ATTRIBUTE_UNUSED,machine_mode mode,int * punsignedp ATTRIBUTE_UNUSED)801 promote_mode (const_tree type ATTRIBUTE_UNUSED, machine_mode mode,
802 	      int *punsignedp ATTRIBUTE_UNUSED)
803 {
804 #ifdef PROMOTE_MODE
805   enum tree_code code;
806   int unsignedp;
807   scalar_mode smode;
808 #endif
809 
810   /* For libcalls this is invoked without TYPE from the backends
811      TARGET_PROMOTE_FUNCTION_MODE hooks.  Don't do anything in that
812      case.  */
813   if (type == NULL_TREE)
814     return mode;
815 
816   /* FIXME: this is the same logic that was there until GCC 4.4, but we
817      probably want to test POINTERS_EXTEND_UNSIGNED even if PROMOTE_MODE
818      is not defined.  The affected targets are M32C, S390, SPARC.  */
819 #ifdef PROMOTE_MODE
820   code = TREE_CODE (type);
821   unsignedp = *punsignedp;
822 
823   switch (code)
824     {
825     case INTEGER_TYPE:   case ENUMERAL_TYPE:   case BOOLEAN_TYPE:
826     case REAL_TYPE:      case OFFSET_TYPE:     case FIXED_POINT_TYPE:
827       /* Values of these types always have scalar mode.  */
828       smode = as_a <scalar_mode> (mode);
829       PROMOTE_MODE (smode, unsignedp, type);
830       *punsignedp = unsignedp;
831       return smode;
832 
833 #ifdef POINTERS_EXTEND_UNSIGNED
834     case REFERENCE_TYPE:
835     case POINTER_TYPE:
836       *punsignedp = POINTERS_EXTEND_UNSIGNED;
837       return targetm.addr_space.address_mode
838 	       (TYPE_ADDR_SPACE (TREE_TYPE (type)));
839 #endif
840 
841     default:
842       return mode;
843     }
844 #else
845   return mode;
846 #endif
847 }
848 
849 
850 /* Use one of promote_mode or promote_function_mode to find the promoted
851    mode of DECL.  If PUNSIGNEDP is not NULL, store there the unsignedness
852    of DECL after promotion.  */
853 
854 machine_mode
promote_decl_mode(const_tree decl,int * punsignedp)855 promote_decl_mode (const_tree decl, int *punsignedp)
856 {
857   tree type = TREE_TYPE (decl);
858   int unsignedp = TYPE_UNSIGNED (type);
859   machine_mode mode = DECL_MODE (decl);
860   machine_mode pmode;
861 
862   if (TREE_CODE (decl) == RESULT_DECL && !DECL_BY_REFERENCE (decl))
863     pmode = promote_function_mode (type, mode, &unsignedp,
864                                    TREE_TYPE (current_function_decl), 1);
865   else if (TREE_CODE (decl) == RESULT_DECL || TREE_CODE (decl) == PARM_DECL)
866     pmode = promote_function_mode (type, mode, &unsignedp,
867                                    TREE_TYPE (current_function_decl), 2);
868   else
869     pmode = promote_mode (type, mode, &unsignedp);
870 
871   if (punsignedp)
872     *punsignedp = unsignedp;
873   return pmode;
874 }
875 
876 /* Return the promoted mode for name.  If it is a named SSA_NAME, it
877    is the same as promote_decl_mode.  Otherwise, it is the promoted
878    mode of a temp decl of same type as the SSA_NAME, if we had created
879    one.  */
880 
881 machine_mode
promote_ssa_mode(const_tree name,int * punsignedp)882 promote_ssa_mode (const_tree name, int *punsignedp)
883 {
884   gcc_assert (TREE_CODE (name) == SSA_NAME);
885 
886   /* Partitions holding parms and results must be promoted as expected
887      by function.c.  */
888   if (SSA_NAME_VAR (name)
889       && (TREE_CODE (SSA_NAME_VAR (name)) == PARM_DECL
890 	  || TREE_CODE (SSA_NAME_VAR (name)) == RESULT_DECL))
891     {
892       machine_mode mode = promote_decl_mode (SSA_NAME_VAR (name), punsignedp);
893       if (mode != BLKmode)
894 	return mode;
895     }
896 
897   tree type = TREE_TYPE (name);
898   int unsignedp = TYPE_UNSIGNED (type);
899   machine_mode pmode = promote_mode (type, TYPE_MODE (type), &unsignedp);
900   if (punsignedp)
901     *punsignedp = unsignedp;
902 
903   return pmode;
904 }
905 
906 
907 
908 /* Controls the behavior of {anti_,}adjust_stack.  */
909 static bool suppress_reg_args_size;
910 
911 /* A helper for adjust_stack and anti_adjust_stack.  */
912 
913 static void
adjust_stack_1(rtx adjust,bool anti_p)914 adjust_stack_1 (rtx adjust, bool anti_p)
915 {
916   rtx temp;
917   rtx_insn *insn;
918 
919   /* Hereafter anti_p means subtract_p.  */
920   if (!STACK_GROWS_DOWNWARD)
921     anti_p = !anti_p;
922 
923   temp = expand_binop (Pmode,
924 		       anti_p ? sub_optab : add_optab,
925 		       stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
926 		       OPTAB_LIB_WIDEN);
927 
928   if (temp != stack_pointer_rtx)
929     insn = emit_move_insn (stack_pointer_rtx, temp);
930   else
931     {
932       insn = get_last_insn ();
933       temp = single_set (insn);
934       gcc_assert (temp != NULL && SET_DEST (temp) == stack_pointer_rtx);
935     }
936 
937   if (!suppress_reg_args_size)
938     add_args_size_note (insn, stack_pointer_delta);
939 }
940 
941 /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
942    This pops when ADJUST is positive.  ADJUST need not be constant.  */
943 
944 void
adjust_stack(rtx adjust)945 adjust_stack (rtx adjust)
946 {
947   if (adjust == const0_rtx)
948     return;
949 
950   /* We expect all variable sized adjustments to be multiple of
951      PREFERRED_STACK_BOUNDARY.  */
952   poly_int64 const_adjust;
953   if (poly_int_rtx_p (adjust, &const_adjust))
954     stack_pointer_delta -= const_adjust;
955 
956   adjust_stack_1 (adjust, false);
957 }
958 
959 /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
960    This pushes when ADJUST is positive.  ADJUST need not be constant.  */
961 
962 void
anti_adjust_stack(rtx adjust)963 anti_adjust_stack (rtx adjust)
964 {
965   if (adjust == const0_rtx)
966     return;
967 
968   /* We expect all variable sized adjustments to be multiple of
969      PREFERRED_STACK_BOUNDARY.  */
970   poly_int64 const_adjust;
971   if (poly_int_rtx_p (adjust, &const_adjust))
972     stack_pointer_delta += const_adjust;
973 
974   adjust_stack_1 (adjust, true);
975 }
976 
977 /* Round the size of a block to be pushed up to the boundary required
978    by this machine.  SIZE is the desired size, which need not be constant.  */
979 
980 static rtx
round_push(rtx size)981 round_push (rtx size)
982 {
983   rtx align_rtx, alignm1_rtx;
984 
985   if (!SUPPORTS_STACK_ALIGNMENT
986       || crtl->preferred_stack_boundary == MAX_SUPPORTED_STACK_ALIGNMENT)
987     {
988       int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
989 
990       if (align == 1)
991 	return size;
992 
993       if (CONST_INT_P (size))
994 	{
995 	  HOST_WIDE_INT new_size = (INTVAL (size) + align - 1) / align * align;
996 
997 	  if (INTVAL (size) != new_size)
998 	    size = GEN_INT (new_size);
999 	  return size;
1000 	}
1001 
1002       align_rtx = GEN_INT (align);
1003       alignm1_rtx = GEN_INT (align - 1);
1004     }
1005   else
1006     {
1007       /* If crtl->preferred_stack_boundary might still grow, use
1008 	 virtual_preferred_stack_boundary_rtx instead.  This will be
1009 	 substituted by the right value in vregs pass and optimized
1010 	 during combine.  */
1011       align_rtx = virtual_preferred_stack_boundary_rtx;
1012       alignm1_rtx = force_operand (plus_constant (Pmode, align_rtx, -1),
1013 				   NULL_RTX);
1014     }
1015 
1016   /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1017      but we know it can't.  So add ourselves and then do
1018      TRUNC_DIV_EXPR.  */
1019   size = expand_binop (Pmode, add_optab, size, alignm1_rtx,
1020 		       NULL_RTX, 1, OPTAB_LIB_WIDEN);
1021   size = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, size, align_rtx,
1022 			NULL_RTX, 1);
1023   size = expand_mult (Pmode, size, align_rtx, NULL_RTX, 1);
1024 
1025   return size;
1026 }
1027 
1028 /* Save the stack pointer for the purpose in SAVE_LEVEL.  PSAVE is a pointer
1029    to a previously-created save area.  If no save area has been allocated,
1030    this function will allocate one.  If a save area is specified, it
1031    must be of the proper mode.  */
1032 
1033 void
emit_stack_save(enum save_level save_level,rtx * psave)1034 emit_stack_save (enum save_level save_level, rtx *psave)
1035 {
1036   rtx sa = *psave;
1037   /* The default is that we use a move insn and save in a Pmode object.  */
1038   rtx_insn *(*fcn) (rtx, rtx) = gen_move_insn;
1039   machine_mode mode = STACK_SAVEAREA_MODE (save_level);
1040 
1041   /* See if this machine has anything special to do for this kind of save.  */
1042   switch (save_level)
1043     {
1044     case SAVE_BLOCK:
1045       if (targetm.have_save_stack_block ())
1046 	fcn = targetm.gen_save_stack_block;
1047       break;
1048     case SAVE_FUNCTION:
1049       if (targetm.have_save_stack_function ())
1050 	fcn = targetm.gen_save_stack_function;
1051       break;
1052     case SAVE_NONLOCAL:
1053       if (targetm.have_save_stack_nonlocal ())
1054 	fcn = targetm.gen_save_stack_nonlocal;
1055       break;
1056     default:
1057       break;
1058     }
1059 
1060   /* If there is no save area and we have to allocate one, do so.  Otherwise
1061      verify the save area is the proper mode.  */
1062 
1063   if (sa == 0)
1064     {
1065       if (mode != VOIDmode)
1066 	{
1067 	  if (save_level == SAVE_NONLOCAL)
1068 	    *psave = sa = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
1069 	  else
1070 	    *psave = sa = gen_reg_rtx (mode);
1071 	}
1072     }
1073 
1074   do_pending_stack_adjust ();
1075   if (sa != 0)
1076     sa = validize_mem (sa);
1077   emit_insn (fcn (sa, stack_pointer_rtx));
1078 }
1079 
1080 /* Restore the stack pointer for the purpose in SAVE_LEVEL.  SA is the save
1081    area made by emit_stack_save.  If it is zero, we have nothing to do.  */
1082 
1083 void
emit_stack_restore(enum save_level save_level,rtx sa)1084 emit_stack_restore (enum save_level save_level, rtx sa)
1085 {
1086   /* The default is that we use a move insn.  */
1087   rtx_insn *(*fcn) (rtx, rtx) = gen_move_insn;
1088 
1089   /* If stack_realign_drap, the x86 backend emits a prologue that aligns both
1090      STACK_POINTER and HARD_FRAME_POINTER.
1091      If stack_realign_fp, the x86 backend emits a prologue that aligns only
1092      STACK_POINTER. This renders the HARD_FRAME_POINTER unusable for accessing
1093      aligned variables, which is reflected in ix86_can_eliminate.
1094      We normally still have the realigned STACK_POINTER that we can use.
1095      But if there is a stack restore still present at reload, it can trigger
1096      mark_not_eliminable for the STACK_POINTER, leaving no way to eliminate
1097      FRAME_POINTER into a hard reg.
1098      To prevent this situation, we force need_drap if we emit a stack
1099      restore.  */
1100   if (SUPPORTS_STACK_ALIGNMENT)
1101     crtl->need_drap = true;
1102 
1103   /* See if this machine has anything special to do for this kind of save.  */
1104   switch (save_level)
1105     {
1106     case SAVE_BLOCK:
1107       if (targetm.have_restore_stack_block ())
1108 	fcn = targetm.gen_restore_stack_block;
1109       break;
1110     case SAVE_FUNCTION:
1111       if (targetm.have_restore_stack_function ())
1112 	fcn = targetm.gen_restore_stack_function;
1113       break;
1114     case SAVE_NONLOCAL:
1115       if (targetm.have_restore_stack_nonlocal ())
1116 	fcn = targetm.gen_restore_stack_nonlocal;
1117       break;
1118     default:
1119       break;
1120     }
1121 
1122   if (sa != 0)
1123     {
1124       sa = validize_mem (sa);
1125       /* These clobbers prevent the scheduler from moving
1126 	 references to variable arrays below the code
1127 	 that deletes (pops) the arrays.  */
1128       emit_clobber (gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode)));
1129       emit_clobber (gen_rtx_MEM (BLKmode, stack_pointer_rtx));
1130     }
1131 
1132   discard_pending_stack_adjust ();
1133 
1134   emit_insn (fcn (stack_pointer_rtx, sa));
1135 }
1136 
1137 /* Invoke emit_stack_save on the nonlocal_goto_save_area for the current
1138    function.  This should be called whenever we allocate or deallocate
1139    dynamic stack space.  */
1140 
1141 void
update_nonlocal_goto_save_area(void)1142 update_nonlocal_goto_save_area (void)
1143 {
1144   tree t_save;
1145   rtx r_save;
1146 
1147   /* The nonlocal_goto_save_area object is an array of N pointers.  The
1148      first one is used for the frame pointer save; the rest are sized by
1149      STACK_SAVEAREA_MODE.  Create a reference to array index 1, the first
1150      of the stack save area slots.  */
1151   t_save = build4 (ARRAY_REF,
1152 		   TREE_TYPE (TREE_TYPE (cfun->nonlocal_goto_save_area)),
1153 		   cfun->nonlocal_goto_save_area,
1154 		   integer_one_node, NULL_TREE, NULL_TREE);
1155   r_save = expand_expr (t_save, NULL_RTX, VOIDmode, EXPAND_WRITE);
1156 
1157   emit_stack_save (SAVE_NONLOCAL, &r_save);
1158 }
1159 
1160 /* Record a new stack level for the current function.  This should be called
1161    whenever we allocate or deallocate dynamic stack space.  */
1162 
1163 void
record_new_stack_level(void)1164 record_new_stack_level (void)
1165 {
1166   /* Record the new stack level for nonlocal gotos.  */
1167   if (cfun->nonlocal_goto_save_area)
1168     update_nonlocal_goto_save_area ();
1169 
1170   /* Record the new stack level for SJLJ exceptions.  */
1171   if (targetm_common.except_unwind_info (&global_options) == UI_SJLJ)
1172     update_sjlj_context ();
1173 }
1174 
1175 /* Return an rtx doing runtime alignment to REQUIRED_ALIGN on TARGET.  */
1176 
1177 rtx
align_dynamic_address(rtx target,unsigned required_align)1178 align_dynamic_address (rtx target, unsigned required_align)
1179 {
1180   /* CEIL_DIV_EXPR needs to worry about the addition overflowing,
1181      but we know it can't.  So add ourselves and then do
1182      TRUNC_DIV_EXPR.  */
1183   target = expand_binop (Pmode, add_optab, target,
1184 			 gen_int_mode (required_align / BITS_PER_UNIT - 1,
1185 				       Pmode),
1186 			 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1187   target = expand_divmod (0, TRUNC_DIV_EXPR, Pmode, target,
1188 			  gen_int_mode (required_align / BITS_PER_UNIT,
1189 					Pmode),
1190 			  NULL_RTX, 1);
1191   target = expand_mult (Pmode, target,
1192 			gen_int_mode (required_align / BITS_PER_UNIT,
1193 				      Pmode),
1194 			NULL_RTX, 1);
1195 
1196   return target;
1197 }
1198 
1199 /* Return an rtx through *PSIZE, representing the size of an area of memory to
1200    be dynamically pushed on the stack.
1201 
1202    *PSIZE is an rtx representing the size of the area.
1203 
1204    SIZE_ALIGN is the alignment (in bits) that we know SIZE has.  This
1205    parameter may be zero.  If so, a proper value will be extracted
1206    from SIZE if it is constant, otherwise BITS_PER_UNIT will be assumed.
1207 
1208    REQUIRED_ALIGN is the alignment (in bits) required for the region
1209    of memory.
1210 
1211    If PSTACK_USAGE_SIZE is not NULL it points to a value that is increased for
1212    the additional size returned.  */
1213 void
get_dynamic_stack_size(rtx * psize,unsigned size_align,unsigned required_align,HOST_WIDE_INT * pstack_usage_size)1214 get_dynamic_stack_size (rtx *psize, unsigned size_align,
1215 			unsigned required_align,
1216 			HOST_WIDE_INT *pstack_usage_size)
1217 {
1218   rtx size = *psize;
1219 
1220   /* Ensure the size is in the proper mode.  */
1221   if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1222     size = convert_to_mode (Pmode, size, 1);
1223 
1224   if (CONST_INT_P (size))
1225     {
1226       unsigned HOST_WIDE_INT lsb;
1227 
1228       lsb = INTVAL (size);
1229       lsb &= -lsb;
1230 
1231       /* Watch out for overflow truncating to "unsigned".  */
1232       if (lsb > UINT_MAX / BITS_PER_UNIT)
1233 	size_align = 1u << (HOST_BITS_PER_INT - 1);
1234       else
1235 	size_align = (unsigned)lsb * BITS_PER_UNIT;
1236     }
1237   else if (size_align < BITS_PER_UNIT)
1238     size_align = BITS_PER_UNIT;
1239 
1240   /* We can't attempt to minimize alignment necessary, because we don't
1241      know the final value of preferred_stack_boundary yet while executing
1242      this code.  */
1243   if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
1244     crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
1245 
1246   /* We will need to ensure that the address we return is aligned to
1247      REQUIRED_ALIGN.  At this point in the compilation, we don't always
1248      know the final value of the STACK_DYNAMIC_OFFSET used in function.c
1249      (it might depend on the size of the outgoing parameter lists, for
1250      example), so we must preventively align the value.  We leave space
1251      in SIZE for the hole that might result from the alignment operation.  */
1252 
1253   unsigned known_align = REGNO_POINTER_ALIGN (VIRTUAL_STACK_DYNAMIC_REGNUM);
1254   if (known_align == 0)
1255     known_align = BITS_PER_UNIT;
1256   if (required_align > known_align)
1257     {
1258       unsigned extra = (required_align - known_align) / BITS_PER_UNIT;
1259       size = plus_constant (Pmode, size, extra);
1260       size = force_operand (size, NULL_RTX);
1261       if (size_align > known_align)
1262 	size_align = known_align;
1263 
1264       if (flag_stack_usage_info && pstack_usage_size)
1265 	*pstack_usage_size += extra;
1266     }
1267 
1268   /* Round the size to a multiple of the required stack alignment.
1269      Since the stack is presumed to be rounded before this allocation,
1270      this will maintain the required alignment.
1271 
1272      If the stack grows downward, we could save an insn by subtracting
1273      SIZE from the stack pointer and then aligning the stack pointer.
1274      The problem with this is that the stack pointer may be unaligned
1275      between the execution of the subtraction and alignment insns and
1276      some machines do not allow this.  Even on those that do, some
1277      signal handlers malfunction if a signal should occur between those
1278      insns.  Since this is an extremely rare event, we have no reliable
1279      way of knowing which systems have this problem.  So we avoid even
1280      momentarily mis-aligning the stack.  */
1281   if (size_align % MAX_SUPPORTED_STACK_ALIGNMENT != 0)
1282     {
1283       size = round_push (size);
1284 
1285       if (flag_stack_usage_info && pstack_usage_size)
1286 	{
1287 	  int align = crtl->preferred_stack_boundary / BITS_PER_UNIT;
1288 	  *pstack_usage_size =
1289 	    (*pstack_usage_size + align - 1) / align * align;
1290 	}
1291     }
1292 
1293   *psize = size;
1294 }
1295 
1296 /* Return the number of bytes to "protect" on the stack for -fstack-check.
1297 
1298    "protect" in the context of -fstack-check means how many bytes we
1299    should always ensure are available on the stack.  More importantly
1300    this is how many bytes are skipped when probing the stack.
1301 
1302    On some targets we want to reuse the -fstack-check prologue support
1303    to give a degree of protection against stack clashing style attacks.
1304 
1305    In that scenario we do not want to skip bytes before probing as that
1306    would render the stack clash protections useless.
1307 
1308    So we never use STACK_CHECK_PROTECT directly.  Instead we indirect though
1309    this helper which allows us to provide different values for
1310    -fstack-check and -fstack-clash-protection.  */
1311 HOST_WIDE_INT
get_stack_check_protect(void)1312 get_stack_check_protect (void)
1313 {
1314   if (flag_stack_clash_protection)
1315     return 0;
1316  return STACK_CHECK_PROTECT;
1317 }
1318 
1319 /* Return an rtx representing the address of an area of memory dynamically
1320    pushed on the stack.
1321 
1322    Any required stack pointer alignment is preserved.
1323 
1324    SIZE is an rtx representing the size of the area.
1325 
1326    SIZE_ALIGN is the alignment (in bits) that we know SIZE has.  This
1327    parameter may be zero.  If so, a proper value will be extracted
1328    from SIZE if it is constant, otherwise BITS_PER_UNIT will be assumed.
1329 
1330    REQUIRED_ALIGN is the alignment (in bits) required for the region
1331    of memory.
1332 
1333    MAX_SIZE is an upper bound for SIZE, if SIZE is not constant, or -1 if
1334    no such upper bound is known.
1335 
1336    If CANNOT_ACCUMULATE is set to TRUE, the caller guarantees that the
1337    stack space allocated by the generated code cannot be added with itself
1338    in the course of the execution of the function.  It is always safe to
1339    pass FALSE here and the following criterion is sufficient in order to
1340    pass TRUE: every path in the CFG that starts at the allocation point and
1341    loops to it executes the associated deallocation code.  */
1342 
1343 rtx
allocate_dynamic_stack_space(rtx size,unsigned size_align,unsigned required_align,HOST_WIDE_INT max_size,bool cannot_accumulate)1344 allocate_dynamic_stack_space (rtx size, unsigned size_align,
1345 			      unsigned required_align,
1346 			      HOST_WIDE_INT max_size,
1347 			      bool cannot_accumulate)
1348 {
1349   HOST_WIDE_INT stack_usage_size = -1;
1350   rtx_code_label *final_label;
1351   rtx final_target, target;
1352 
1353   /* If we're asking for zero bytes, it doesn't matter what we point
1354      to since we can't dereference it.  But return a reasonable
1355      address anyway.  */
1356   if (size == const0_rtx)
1357     return virtual_stack_dynamic_rtx;
1358 
1359   /* Otherwise, show we're calling alloca or equivalent.  */
1360   cfun->calls_alloca = 1;
1361 
1362   /* If stack usage info is requested, look into the size we are passed.
1363      We need to do so this early to avoid the obfuscation that may be
1364      introduced later by the various alignment operations.  */
1365   if (flag_stack_usage_info)
1366     {
1367       if (CONST_INT_P (size))
1368 	stack_usage_size = INTVAL (size);
1369       else if (REG_P (size))
1370         {
1371 	  /* Look into the last emitted insn and see if we can deduce
1372 	     something for the register.  */
1373 	  rtx_insn *insn;
1374 	  rtx set, note;
1375 	  insn = get_last_insn ();
1376 	  if ((set = single_set (insn)) && rtx_equal_p (SET_DEST (set), size))
1377 	    {
1378 	      if (CONST_INT_P (SET_SRC (set)))
1379 		stack_usage_size = INTVAL (SET_SRC (set));
1380 	      else if ((note = find_reg_equal_equiv_note (insn))
1381 		       && CONST_INT_P (XEXP (note, 0)))
1382 		stack_usage_size = INTVAL (XEXP (note, 0));
1383 	    }
1384 	}
1385 
1386       /* If the size is not constant, try the maximum size.  */
1387       if (stack_usage_size < 0)
1388 	stack_usage_size = max_size;
1389 
1390       /* If the size is still not constant, we can't say anything.  */
1391       if (stack_usage_size < 0)
1392 	{
1393 	  current_function_has_unbounded_dynamic_stack_size = 1;
1394 	  stack_usage_size = 0;
1395 	}
1396     }
1397 
1398   get_dynamic_stack_size (&size, size_align, required_align, &stack_usage_size);
1399 
1400   target = gen_reg_rtx (Pmode);
1401 
1402   /* The size is supposed to be fully adjusted at this point so record it
1403      if stack usage info is requested.  */
1404   if (flag_stack_usage_info)
1405     {
1406       current_function_dynamic_stack_size += stack_usage_size;
1407 
1408       /* ??? This is gross but the only safe stance in the absence
1409 	 of stack usage oriented flow analysis.  */
1410       if (!cannot_accumulate)
1411 	current_function_has_unbounded_dynamic_stack_size = 1;
1412     }
1413 
1414   do_pending_stack_adjust ();
1415 
1416   final_label = NULL;
1417   final_target = NULL_RTX;
1418 
1419   /* If we are splitting the stack, we need to ask the backend whether
1420      there is enough room on the current stack.  If there isn't, or if
1421      the backend doesn't know how to tell is, then we need to call a
1422      function to allocate memory in some other way.  This memory will
1423      be released when we release the current stack segment.  The
1424      effect is that stack allocation becomes less efficient, but at
1425      least it doesn't cause a stack overflow.  */
1426   if (flag_split_stack)
1427     {
1428       rtx_code_label *available_label;
1429       rtx ask, space, func;
1430 
1431       available_label = NULL;
1432 
1433       if (targetm.have_split_stack_space_check ())
1434 	{
1435 	  available_label = gen_label_rtx ();
1436 
1437 	  /* This instruction will branch to AVAILABLE_LABEL if there
1438 	     are SIZE bytes available on the stack.  */
1439 	  emit_insn (targetm.gen_split_stack_space_check
1440 		     (size, available_label));
1441 	}
1442 
1443       /* The __morestack_allocate_stack_space function will allocate
1444 	 memory using malloc.  If the alignment of the memory returned
1445 	 by malloc does not meet REQUIRED_ALIGN, we increase SIZE to
1446 	 make sure we allocate enough space.  */
1447       if (MALLOC_ABI_ALIGNMENT >= required_align)
1448 	ask = size;
1449       else
1450 	ask = expand_binop (Pmode, add_optab, size,
1451 			    gen_int_mode (required_align / BITS_PER_UNIT - 1,
1452 					  Pmode),
1453 			    NULL_RTX, 1, OPTAB_LIB_WIDEN);
1454 
1455       func = init_one_libfunc ("__morestack_allocate_stack_space");
1456 
1457       space = emit_library_call_value (func, target, LCT_NORMAL, Pmode,
1458 				       ask, Pmode);
1459 
1460       if (available_label == NULL_RTX)
1461 	return space;
1462 
1463       final_target = gen_reg_rtx (Pmode);
1464 
1465       emit_move_insn (final_target, space);
1466 
1467       final_label = gen_label_rtx ();
1468       emit_jump (final_label);
1469 
1470       emit_label (available_label);
1471     }
1472 
1473  /* We ought to be called always on the toplevel and stack ought to be aligned
1474     properly.  */
1475   gcc_assert (multiple_p (stack_pointer_delta,
1476 			  PREFERRED_STACK_BOUNDARY / BITS_PER_UNIT));
1477 
1478   /* If needed, check that we have the required amount of stack.  Take into
1479      account what has already been checked.  */
1480   if (STACK_CHECK_MOVING_SP)
1481     ;
1482   else if (flag_stack_check == GENERIC_STACK_CHECK)
1483     probe_stack_range (STACK_OLD_CHECK_PROTECT + STACK_CHECK_MAX_FRAME_SIZE,
1484 		       size);
1485   else if (flag_stack_check == STATIC_BUILTIN_STACK_CHECK)
1486     probe_stack_range (get_stack_check_protect (), size);
1487 
1488   /* Don't let anti_adjust_stack emit notes.  */
1489   suppress_reg_args_size = true;
1490 
1491   /* Perform the required allocation from the stack.  Some systems do
1492      this differently than simply incrementing/decrementing from the
1493      stack pointer, such as acquiring the space by calling malloc().  */
1494   if (targetm.have_allocate_stack ())
1495     {
1496       class expand_operand ops[2];
1497       /* We don't have to check against the predicate for operand 0 since
1498 	 TARGET is known to be a pseudo of the proper mode, which must
1499 	 be valid for the operand.  */
1500       create_fixed_operand (&ops[0], target);
1501       create_convert_operand_to (&ops[1], size, STACK_SIZE_MODE, true);
1502       expand_insn (targetm.code_for_allocate_stack, 2, ops);
1503     }
1504   else
1505     {
1506       poly_int64 saved_stack_pointer_delta;
1507 
1508       if (!STACK_GROWS_DOWNWARD)
1509 	emit_move_insn (target, virtual_stack_dynamic_rtx);
1510 
1511       /* Check stack bounds if necessary.  */
1512       if (crtl->limit_stack)
1513 	{
1514 	  rtx available;
1515 	  rtx_code_label *space_available = gen_label_rtx ();
1516 	  if (STACK_GROWS_DOWNWARD)
1517 	    available = expand_binop (Pmode, sub_optab,
1518 				      stack_pointer_rtx, stack_limit_rtx,
1519 				      NULL_RTX, 1, OPTAB_WIDEN);
1520 	  else
1521 	    available = expand_binop (Pmode, sub_optab,
1522 				      stack_limit_rtx, stack_pointer_rtx,
1523 				      NULL_RTX, 1, OPTAB_WIDEN);
1524 
1525 	  emit_cmp_and_jump_insns (available, size, GEU, NULL_RTX, Pmode, 1,
1526 				   space_available);
1527 	  if (targetm.have_trap ())
1528 	    emit_insn (targetm.gen_trap ());
1529 	  else
1530 	    error ("stack limits not supported on this target");
1531 	  emit_barrier ();
1532 	  emit_label (space_available);
1533 	}
1534 
1535       saved_stack_pointer_delta = stack_pointer_delta;
1536 
1537       if (flag_stack_check && STACK_CHECK_MOVING_SP)
1538 	anti_adjust_stack_and_probe (size, false);
1539       else if (flag_stack_clash_protection)
1540 	anti_adjust_stack_and_probe_stack_clash (size);
1541       else
1542 	anti_adjust_stack (size);
1543 
1544       /* Even if size is constant, don't modify stack_pointer_delta.
1545 	 The constant size alloca should preserve
1546 	 crtl->preferred_stack_boundary alignment.  */
1547       stack_pointer_delta = saved_stack_pointer_delta;
1548 
1549       if (STACK_GROWS_DOWNWARD)
1550 	emit_move_insn (target, virtual_stack_dynamic_rtx);
1551     }
1552 
1553   suppress_reg_args_size = false;
1554 
1555   /* Finish up the split stack handling.  */
1556   if (final_label != NULL_RTX)
1557     {
1558       gcc_assert (flag_split_stack);
1559       emit_move_insn (final_target, target);
1560       emit_label (final_label);
1561       target = final_target;
1562     }
1563 
1564   target = align_dynamic_address (target, required_align);
1565 
1566   /* Now that we've committed to a return value, mark its alignment.  */
1567   mark_reg_pointer (target, required_align);
1568 
1569   /* Record the new stack level.  */
1570   record_new_stack_level ();
1571 
1572   return target;
1573 }
1574 
1575 /* Return an rtx representing the address of an area of memory already
1576    statically pushed onto the stack in the virtual stack vars area.  (It is
1577    assumed that the area is allocated in the function prologue.)
1578 
1579    Any required stack pointer alignment is preserved.
1580 
1581    OFFSET is the offset of the area into the virtual stack vars area.
1582 
1583    REQUIRED_ALIGN is the alignment (in bits) required for the region
1584    of memory.  */
1585 
1586 rtx
get_dynamic_stack_base(poly_int64 offset,unsigned required_align)1587 get_dynamic_stack_base (poly_int64 offset, unsigned required_align)
1588 {
1589   rtx target;
1590 
1591   if (crtl->preferred_stack_boundary < PREFERRED_STACK_BOUNDARY)
1592     crtl->preferred_stack_boundary = PREFERRED_STACK_BOUNDARY;
1593 
1594   target = gen_reg_rtx (Pmode);
1595   emit_move_insn (target, virtual_stack_vars_rtx);
1596   target = expand_binop (Pmode, add_optab, target,
1597 			 gen_int_mode (offset, Pmode),
1598 			 NULL_RTX, 1, OPTAB_LIB_WIDEN);
1599   target = align_dynamic_address (target, required_align);
1600 
1601   /* Now that we've committed to a return value, mark its alignment.  */
1602   mark_reg_pointer (target, required_align);
1603 
1604   return target;
1605 }
1606 
1607 /* A front end may want to override GCC's stack checking by providing a
1608    run-time routine to call to check the stack, so provide a mechanism for
1609    calling that routine.  */
1610 
1611 static GTY(()) rtx stack_check_libfunc;
1612 
1613 void
set_stack_check_libfunc(const char * libfunc_name)1614 set_stack_check_libfunc (const char *libfunc_name)
1615 {
1616   gcc_assert (stack_check_libfunc == NULL_RTX);
1617   stack_check_libfunc = gen_rtx_SYMBOL_REF (Pmode, libfunc_name);
1618   tree ptype
1619     = Pmode == ptr_mode
1620       ? ptr_type_node
1621       : lang_hooks.types.type_for_mode (Pmode, 1);
1622   tree ftype
1623     = build_function_type_list (void_type_node, ptype, NULL_TREE);
1624   tree decl = build_decl (UNKNOWN_LOCATION, FUNCTION_DECL,
1625 			  get_identifier (libfunc_name), ftype);
1626   DECL_EXTERNAL (decl) = 1;
1627   SET_SYMBOL_REF_DECL (stack_check_libfunc, decl);
1628 }
1629 
1630 /* Emit one stack probe at ADDRESS, an address within the stack.  */
1631 
1632 void
emit_stack_probe(rtx address)1633 emit_stack_probe (rtx address)
1634 {
1635   if (targetm.have_probe_stack_address ())
1636     {
1637       class expand_operand ops[1];
1638       insn_code icode = targetm.code_for_probe_stack_address;
1639       create_address_operand (ops, address);
1640       maybe_legitimize_operands (icode, 0, 1, ops);
1641       expand_insn (icode, 1, ops);
1642     }
1643   else
1644     {
1645       rtx memref = gen_rtx_MEM (word_mode, address);
1646 
1647       MEM_VOLATILE_P (memref) = 1;
1648       memref = validize_mem (memref);
1649 
1650       /* See if we have an insn to probe the stack.  */
1651       if (targetm.have_probe_stack ())
1652 	emit_insn (targetm.gen_probe_stack (memref));
1653       else
1654 	emit_move_insn (memref, const0_rtx);
1655     }
1656 }
1657 
1658 /* Probe a range of stack addresses from FIRST to FIRST+SIZE, inclusive.
1659    FIRST is a constant and size is a Pmode RTX.  These are offsets from
1660    the current stack pointer.  STACK_GROWS_DOWNWARD says whether to add
1661    or subtract them from the stack pointer.  */
1662 
1663 #define PROBE_INTERVAL (1 << STACK_CHECK_PROBE_INTERVAL_EXP)
1664 
1665 #if STACK_GROWS_DOWNWARD
1666 #define STACK_GROW_OP MINUS
1667 #define STACK_GROW_OPTAB sub_optab
1668 #define STACK_GROW_OFF(off) -(off)
1669 #else
1670 #define STACK_GROW_OP PLUS
1671 #define STACK_GROW_OPTAB add_optab
1672 #define STACK_GROW_OFF(off) (off)
1673 #endif
1674 
1675 void
probe_stack_range(HOST_WIDE_INT first,rtx size)1676 probe_stack_range (HOST_WIDE_INT first, rtx size)
1677 {
1678   /* First ensure SIZE is Pmode.  */
1679   if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1680     size = convert_to_mode (Pmode, size, 1);
1681 
1682   /* Next see if we have a function to check the stack.  */
1683   if (stack_check_libfunc)
1684     {
1685       rtx addr = memory_address (Pmode,
1686 				 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1687 					         stack_pointer_rtx,
1688 					         plus_constant (Pmode,
1689 								size, first)));
1690       emit_library_call (stack_check_libfunc, LCT_THROW, VOIDmode,
1691 			 addr, Pmode);
1692     }
1693 
1694   /* Next see if we have an insn to check the stack.  */
1695   else if (targetm.have_check_stack ())
1696     {
1697       class expand_operand ops[1];
1698       rtx addr = memory_address (Pmode,
1699 				 gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1700 					         stack_pointer_rtx,
1701 					         plus_constant (Pmode,
1702 								size, first)));
1703       bool success;
1704       create_input_operand (&ops[0], addr, Pmode);
1705       success = maybe_expand_insn (targetm.code_for_check_stack, 1, ops);
1706       gcc_assert (success);
1707     }
1708 
1709   /* Otherwise we have to generate explicit probes.  If we have a constant
1710      small number of them to generate, that's the easy case.  */
1711   else if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
1712     {
1713       HOST_WIDE_INT isize = INTVAL (size), i;
1714       rtx addr;
1715 
1716       /* Probe at FIRST + N * PROBE_INTERVAL for values of N from 1 until
1717 	 it exceeds SIZE.  If only one probe is needed, this will not
1718 	 generate any code.  Then probe at FIRST + SIZE.  */
1719       for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
1720 	{
1721 	  addr = memory_address (Pmode,
1722 				 plus_constant (Pmode, stack_pointer_rtx,
1723 				 		STACK_GROW_OFF (first + i)));
1724 	  emit_stack_probe (addr);
1725 	}
1726 
1727       addr = memory_address (Pmode,
1728 			     plus_constant (Pmode, stack_pointer_rtx,
1729 					    STACK_GROW_OFF (first + isize)));
1730       emit_stack_probe (addr);
1731     }
1732 
1733   /* In the variable case, do the same as above, but in a loop.  Note that we
1734      must be extra careful with variables wrapping around because we might be
1735      at the very top (or the very bottom) of the address space and we have to
1736      be able to handle this case properly; in particular, we use an equality
1737      test for the loop condition.  */
1738   else
1739     {
1740       rtx rounded_size, rounded_size_op, test_addr, last_addr, temp;
1741       rtx_code_label *loop_lab = gen_label_rtx ();
1742       rtx_code_label *end_lab = gen_label_rtx ();
1743 
1744       /* Step 1: round SIZE to the previous multiple of the interval.  */
1745 
1746       /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL  */
1747       rounded_size
1748 	= simplify_gen_binary (AND, Pmode, size,
1749 			       gen_int_mode (-PROBE_INTERVAL, Pmode));
1750       rounded_size_op = force_operand (rounded_size, NULL_RTX);
1751 
1752 
1753       /* Step 2: compute initial and final value of the loop counter.  */
1754 
1755       /* TEST_ADDR = SP + FIRST.  */
1756       test_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1757 					 	 stack_pointer_rtx,
1758 						 gen_int_mode (first, Pmode)),
1759 				 NULL_RTX);
1760 
1761       /* LAST_ADDR = SP + FIRST + ROUNDED_SIZE.  */
1762       last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1763 						 test_addr,
1764 						 rounded_size_op), NULL_RTX);
1765 
1766 
1767       /* Step 3: the loop
1768 
1769 	 while (TEST_ADDR != LAST_ADDR)
1770 	   {
1771 	     TEST_ADDR = TEST_ADDR + PROBE_INTERVAL
1772 	     probe at TEST_ADDR
1773 	   }
1774 
1775 	 probes at FIRST + N * PROBE_INTERVAL for values of N from 1
1776 	 until it is equal to ROUNDED_SIZE.  */
1777 
1778       emit_label (loop_lab);
1779 
1780       /* Jump to END_LAB if TEST_ADDR == LAST_ADDR.  */
1781       emit_cmp_and_jump_insns (test_addr, last_addr, EQ, NULL_RTX, Pmode, 1,
1782 			       end_lab);
1783 
1784       /* TEST_ADDR = TEST_ADDR + PROBE_INTERVAL.  */
1785       temp = expand_binop (Pmode, STACK_GROW_OPTAB, test_addr,
1786 			   gen_int_mode (PROBE_INTERVAL, Pmode), test_addr,
1787 			   1, OPTAB_WIDEN);
1788 
1789       gcc_assert (temp == test_addr);
1790 
1791       /* Probe at TEST_ADDR.  */
1792       emit_stack_probe (test_addr);
1793 
1794       emit_jump (loop_lab);
1795 
1796       emit_label (end_lab);
1797 
1798 
1799       /* Step 4: probe at FIRST + SIZE if we cannot assert at compile-time
1800 	 that SIZE is equal to ROUNDED_SIZE.  */
1801 
1802       /* TEMP = SIZE - ROUNDED_SIZE.  */
1803       temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
1804       if (temp != const0_rtx)
1805 	{
1806 	  rtx addr;
1807 
1808 	  if (CONST_INT_P (temp))
1809 	    {
1810 	      /* Use [base + disp} addressing mode if supported.  */
1811 	      HOST_WIDE_INT offset = INTVAL (temp);
1812 	      addr = memory_address (Pmode,
1813 				     plus_constant (Pmode, last_addr,
1814 						    STACK_GROW_OFF (offset)));
1815 	    }
1816 	  else
1817 	    {
1818 	      /* Manual CSE if the difference is not known at compile-time.  */
1819 	      temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
1820 	      addr = memory_address (Pmode,
1821 				     gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1822 						     last_addr, temp));
1823 	    }
1824 
1825 	  emit_stack_probe (addr);
1826 	}
1827     }
1828 
1829   /* Make sure nothing is scheduled before we are done.  */
1830   emit_insn (gen_blockage ());
1831 }
1832 
1833 /* Compute parameters for stack clash probing a dynamic stack
1834    allocation of SIZE bytes.
1835 
1836    We compute ROUNDED_SIZE, LAST_ADDR, RESIDUAL and PROBE_INTERVAL.
1837 
1838    Additionally we conditionally dump the type of probing that will
1839    be needed given the values computed.  */
1840 
1841 void
compute_stack_clash_protection_loop_data(rtx * rounded_size,rtx * last_addr,rtx * residual,HOST_WIDE_INT * probe_interval,rtx size)1842 compute_stack_clash_protection_loop_data (rtx *rounded_size, rtx *last_addr,
1843 					  rtx *residual,
1844 					  HOST_WIDE_INT *probe_interval,
1845 					  rtx size)
1846 {
1847   /* Round SIZE down to STACK_CLASH_PROTECTION_PROBE_INTERVAL */
1848   *probe_interval
1849     = 1 << param_stack_clash_protection_probe_interval;
1850   *rounded_size = simplify_gen_binary (AND, Pmode, size,
1851 				        GEN_INT (-*probe_interval));
1852 
1853   /* Compute the value of the stack pointer for the last iteration.
1854      It's just SP + ROUNDED_SIZE.  */
1855   rtx rounded_size_op = force_operand (*rounded_size, NULL_RTX);
1856   *last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
1857 					      stack_pointer_rtx,
1858 					      rounded_size_op),
1859 			      NULL_RTX);
1860 
1861   /* Compute any residuals not allocated by the loop above.  Residuals
1862      are just the ROUNDED_SIZE - SIZE.  */
1863   *residual = simplify_gen_binary (MINUS, Pmode, size, *rounded_size);
1864 
1865   /* Dump key information to make writing tests easy.  */
1866   if (dump_file)
1867     {
1868       if (*rounded_size == CONST0_RTX (Pmode))
1869 	fprintf (dump_file,
1870 		 "Stack clash skipped dynamic allocation and probing loop.\n");
1871       else if (CONST_INT_P (*rounded_size)
1872 	       && INTVAL (*rounded_size) <= 4 * *probe_interval)
1873 	fprintf (dump_file,
1874 		 "Stack clash dynamic allocation and probing inline.\n");
1875       else if (CONST_INT_P (*rounded_size))
1876 	fprintf (dump_file,
1877 		 "Stack clash dynamic allocation and probing in "
1878 		 "rotated loop.\n");
1879       else
1880 	fprintf (dump_file,
1881 		 "Stack clash dynamic allocation and probing in loop.\n");
1882 
1883       if (*residual != CONST0_RTX (Pmode))
1884 	fprintf (dump_file,
1885 		 "Stack clash dynamic allocation and probing residuals.\n");
1886       else
1887 	fprintf (dump_file,
1888 		 "Stack clash skipped dynamic allocation and "
1889 		 "probing residuals.\n");
1890     }
1891 }
1892 
1893 /* Emit the start of an allocate/probe loop for stack
1894    clash protection.
1895 
1896    LOOP_LAB and END_LAB are returned for use when we emit the
1897    end of the loop.
1898 
1899    LAST addr is the value for SP which stops the loop.  */
1900 void
emit_stack_clash_protection_probe_loop_start(rtx * loop_lab,rtx * end_lab,rtx last_addr,bool rotated)1901 emit_stack_clash_protection_probe_loop_start (rtx *loop_lab,
1902 					      rtx *end_lab,
1903 					      rtx last_addr,
1904 					      bool rotated)
1905 {
1906   /* Essentially we want to emit any setup code, the top of loop
1907      label and the comparison at the top of the loop.  */
1908   *loop_lab = gen_label_rtx ();
1909   *end_lab = gen_label_rtx ();
1910 
1911   emit_label (*loop_lab);
1912   if (!rotated)
1913     emit_cmp_and_jump_insns (stack_pointer_rtx, last_addr, EQ, NULL_RTX,
1914 			     Pmode, 1, *end_lab);
1915 }
1916 
1917 /* Emit the end of a stack clash probing loop.
1918 
1919    This consists of just the jump back to LOOP_LAB and
1920    emitting END_LOOP after the loop.  */
1921 
1922 void
emit_stack_clash_protection_probe_loop_end(rtx loop_lab,rtx end_loop,rtx last_addr,bool rotated)1923 emit_stack_clash_protection_probe_loop_end (rtx loop_lab, rtx end_loop,
1924 					    rtx last_addr, bool rotated)
1925 {
1926   if (rotated)
1927     emit_cmp_and_jump_insns (stack_pointer_rtx, last_addr, NE, NULL_RTX,
1928 			     Pmode, 1, loop_lab);
1929   else
1930     emit_jump (loop_lab);
1931 
1932   emit_label (end_loop);
1933 
1934 }
1935 
1936 /* Adjust the stack pointer by minus SIZE (an rtx for a number of bytes)
1937    while probing it.  This pushes when SIZE is positive.  SIZE need not
1938    be constant.
1939 
1940    This is subtly different than anti_adjust_stack_and_probe to try and
1941    prevent stack-clash attacks
1942 
1943      1. It must assume no knowledge of the probing state, any allocation
1944 	must probe.
1945 
1946 	Consider the case of a 1 byte alloca in a loop.  If the sum of the
1947 	allocations is large, then this could be used to jump the guard if
1948 	probes were not emitted.
1949 
1950      2. It never skips probes, whereas anti_adjust_stack_and_probe will
1951 	skip probes on the first couple PROBE_INTERVALs on the assumption
1952 	they're done elsewhere.
1953 
1954      3. It only allocates and probes SIZE bytes, it does not need to
1955 	allocate/probe beyond that because this probing style does not
1956 	guarantee signal handling capability if the guard is hit.  */
1957 
1958 static void
anti_adjust_stack_and_probe_stack_clash(rtx size)1959 anti_adjust_stack_and_probe_stack_clash (rtx size)
1960 {
1961   /* First ensure SIZE is Pmode.  */
1962   if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
1963     size = convert_to_mode (Pmode, size, 1);
1964 
1965   /* We can get here with a constant size on some targets.  */
1966   rtx rounded_size, last_addr, residual;
1967   HOST_WIDE_INT probe_interval, probe_range;
1968   bool target_probe_range_p = false;
1969   compute_stack_clash_protection_loop_data (&rounded_size, &last_addr,
1970 					    &residual, &probe_interval, size);
1971 
1972   /* Get the back-end specific probe ranges.  */
1973   probe_range = targetm.stack_clash_protection_alloca_probe_range ();
1974   target_probe_range_p = probe_range != 0;
1975   gcc_assert (probe_range >= 0);
1976 
1977   /* If no back-end specific range defined, default to the top of the newly
1978      allocated range.  */
1979   if (probe_range == 0)
1980     probe_range = probe_interval - GET_MODE_SIZE (word_mode);
1981 
1982   if (rounded_size != CONST0_RTX (Pmode))
1983     {
1984       if (CONST_INT_P (rounded_size)
1985 	  && INTVAL (rounded_size) <= 4 * probe_interval)
1986 	{
1987 	  for (HOST_WIDE_INT i = 0;
1988 	       i < INTVAL (rounded_size);
1989 	       i += probe_interval)
1990 	    {
1991 	      anti_adjust_stack (GEN_INT (probe_interval));
1992 	      /* The prologue does not probe residuals.  Thus the offset
1993 		 here to probe just beyond what the prologue had already
1994 		 allocated.  */
1995 	      emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
1996 					       probe_range));
1997 
1998 	      emit_insn (gen_blockage ());
1999 	    }
2000 	}
2001       else
2002 	{
2003 	  rtx loop_lab, end_loop;
2004 	  bool rotate_loop = CONST_INT_P (rounded_size);
2005 	  emit_stack_clash_protection_probe_loop_start (&loop_lab, &end_loop,
2006 							last_addr, rotate_loop);
2007 
2008 	  anti_adjust_stack (GEN_INT (probe_interval));
2009 
2010 	  /* The prologue does not probe residuals.  Thus the offset here
2011 	     to probe just beyond what the prologue had already
2012 	     allocated.  */
2013 	  emit_stack_probe (plus_constant (Pmode, stack_pointer_rtx,
2014 					   probe_range));
2015 
2016 	  emit_stack_clash_protection_probe_loop_end (loop_lab, end_loop,
2017 						      last_addr, rotate_loop);
2018 	  emit_insn (gen_blockage ());
2019 	}
2020     }
2021 
2022   if (residual != CONST0_RTX (Pmode))
2023     {
2024       rtx label = NULL_RTX;
2025       /* RESIDUAL could be zero at runtime and in that case *sp could
2026 	 hold live data.  Furthermore, we do not want to probe into the
2027 	 red zone.
2028 
2029 	 If TARGET_PROBE_RANGE_P then the target has promised it's safe to
2030 	 probe at offset 0.  In which case we no longer have to check for
2031 	 RESIDUAL == 0.  However we still need to probe at the right offset
2032 	 when RESIDUAL > PROBE_RANGE, in which case we probe at PROBE_RANGE.
2033 
2034 	 If !TARGET_PROBE_RANGE_P then go ahead and just guard the probe at *sp
2035 	 on RESIDUAL != 0 at runtime if RESIDUAL is not a compile time constant.
2036 	 */
2037       anti_adjust_stack (residual);
2038 
2039       if (!CONST_INT_P (residual))
2040 	{
2041 	  label = gen_label_rtx ();
2042 	  rtx_code op = target_probe_range_p ? LT : EQ;
2043 	  rtx probe_cmp_value = target_probe_range_p
2044 	    ? gen_rtx_CONST_INT (GET_MODE (residual), probe_range)
2045 	    : CONST0_RTX (GET_MODE (residual));
2046 
2047 	  if (target_probe_range_p)
2048 	    emit_stack_probe (stack_pointer_rtx);
2049 
2050 	  emit_cmp_and_jump_insns (residual, probe_cmp_value,
2051 				   op, NULL_RTX, Pmode, 1, label);
2052 	}
2053 
2054       rtx x = NULL_RTX;
2055 
2056       /* If RESIDUAL isn't a constant and TARGET_PROBE_RANGE_P then we probe up
2057 	 by the ABI defined safe value.  */
2058       if (!CONST_INT_P (residual) && target_probe_range_p)
2059 	x = GEN_INT (probe_range);
2060       /* If RESIDUAL is a constant but smaller than the ABI defined safe value,
2061 	 we still want to probe up, but the safest amount if a word.  */
2062       else if (target_probe_range_p)
2063 	{
2064 	  if (INTVAL (residual) <= probe_range)
2065 	    x = GEN_INT (GET_MODE_SIZE (word_mode));
2066 	  else
2067 	    x = GEN_INT (probe_range);
2068 	}
2069       else
2070       /* If nothing else, probe at the top of the new allocation.  */
2071 	x = plus_constant (Pmode, residual, -GET_MODE_SIZE (word_mode));
2072 
2073       emit_stack_probe (gen_rtx_PLUS (Pmode, stack_pointer_rtx, x));
2074 
2075       emit_insn (gen_blockage ());
2076       if (!CONST_INT_P (residual))
2077 	  emit_label (label);
2078     }
2079 }
2080 
2081 
2082 /* Adjust the stack pointer by minus SIZE (an rtx for a number of bytes)
2083    while probing it.  This pushes when SIZE is positive.  SIZE need not
2084    be constant.  If ADJUST_BACK is true, adjust back the stack pointer
2085    by plus SIZE at the end.  */
2086 
2087 void
anti_adjust_stack_and_probe(rtx size,bool adjust_back)2088 anti_adjust_stack_and_probe (rtx size, bool adjust_back)
2089 {
2090   /* We skip the probe for the first interval + a small dope of 4 words and
2091      probe that many bytes past the specified size to maintain a protection
2092      area at the botton of the stack.  */
2093   const int dope = 4 * UNITS_PER_WORD;
2094 
2095   /* First ensure SIZE is Pmode.  */
2096   if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
2097     size = convert_to_mode (Pmode, size, 1);
2098 
2099   /* If we have a constant small number of probes to generate, that's the
2100      easy case.  */
2101   if (CONST_INT_P (size) && INTVAL (size) < 7 * PROBE_INTERVAL)
2102     {
2103       HOST_WIDE_INT isize = INTVAL (size), i;
2104       bool first_probe = true;
2105 
2106       /* Adjust SP and probe at PROBE_INTERVAL + N * PROBE_INTERVAL for
2107 	 values of N from 1 until it exceeds SIZE.  If only one probe is
2108 	 needed, this will not generate any code.  Then adjust and probe
2109 	 to PROBE_INTERVAL + SIZE.  */
2110       for (i = PROBE_INTERVAL; i < isize; i += PROBE_INTERVAL)
2111 	{
2112 	  if (first_probe)
2113 	    {
2114 	      anti_adjust_stack (GEN_INT (2 * PROBE_INTERVAL + dope));
2115 	      first_probe = false;
2116 	    }
2117 	  else
2118 	    anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
2119 	  emit_stack_probe (stack_pointer_rtx);
2120 	}
2121 
2122       if (first_probe)
2123 	anti_adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL + dope));
2124       else
2125 	anti_adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL - i));
2126       emit_stack_probe (stack_pointer_rtx);
2127     }
2128 
2129   /* In the variable case, do the same as above, but in a loop.  Note that we
2130      must be extra careful with variables wrapping around because we might be
2131      at the very top (or the very bottom) of the address space and we have to
2132      be able to handle this case properly; in particular, we use an equality
2133      test for the loop condition.  */
2134   else
2135     {
2136       rtx rounded_size, rounded_size_op, last_addr, temp;
2137       rtx_code_label *loop_lab = gen_label_rtx ();
2138       rtx_code_label *end_lab = gen_label_rtx ();
2139 
2140 
2141       /* Step 1: round SIZE to the previous multiple of the interval.  */
2142 
2143       /* ROUNDED_SIZE = SIZE & -PROBE_INTERVAL  */
2144       rounded_size
2145 	= simplify_gen_binary (AND, Pmode, size,
2146 			       gen_int_mode (-PROBE_INTERVAL, Pmode));
2147       rounded_size_op = force_operand (rounded_size, NULL_RTX);
2148 
2149 
2150       /* Step 2: compute initial and final value of the loop counter.  */
2151 
2152       /* SP = SP_0 + PROBE_INTERVAL.  */
2153       anti_adjust_stack (GEN_INT (PROBE_INTERVAL + dope));
2154 
2155       /* LAST_ADDR = SP_0 + PROBE_INTERVAL + ROUNDED_SIZE.  */
2156       last_addr = force_operand (gen_rtx_fmt_ee (STACK_GROW_OP, Pmode,
2157 						 stack_pointer_rtx,
2158 						 rounded_size_op), NULL_RTX);
2159 
2160 
2161       /* Step 3: the loop
2162 
2163 	 while (SP != LAST_ADDR)
2164 	   {
2165 	     SP = SP + PROBE_INTERVAL
2166 	     probe at SP
2167 	   }
2168 
2169 	 adjusts SP and probes at PROBE_INTERVAL + N * PROBE_INTERVAL for
2170 	 values of N from 1 until it is equal to ROUNDED_SIZE.  */
2171 
2172       emit_label (loop_lab);
2173 
2174       /* Jump to END_LAB if SP == LAST_ADDR.  */
2175       emit_cmp_and_jump_insns (stack_pointer_rtx, last_addr, EQ, NULL_RTX,
2176 			       Pmode, 1, end_lab);
2177 
2178       /* SP = SP + PROBE_INTERVAL and probe at SP.  */
2179       anti_adjust_stack (GEN_INT (PROBE_INTERVAL));
2180       emit_stack_probe (stack_pointer_rtx);
2181 
2182       emit_jump (loop_lab);
2183 
2184       emit_label (end_lab);
2185 
2186 
2187       /* Step 4: adjust SP and probe at PROBE_INTERVAL + SIZE if we cannot
2188 	 assert at compile-time that SIZE is equal to ROUNDED_SIZE.  */
2189 
2190       /* TEMP = SIZE - ROUNDED_SIZE.  */
2191       temp = simplify_gen_binary (MINUS, Pmode, size, rounded_size);
2192       if (temp != const0_rtx)
2193 	{
2194 	  /* Manual CSE if the difference is not known at compile-time.  */
2195 	  if (GET_CODE (temp) != CONST_INT)
2196 	    temp = gen_rtx_MINUS (Pmode, size, rounded_size_op);
2197 	  anti_adjust_stack (temp);
2198 	  emit_stack_probe (stack_pointer_rtx);
2199 	}
2200     }
2201 
2202   /* Adjust back and account for the additional first interval.  */
2203   if (adjust_back)
2204     adjust_stack (plus_constant (Pmode, size, PROBE_INTERVAL + dope));
2205   else
2206     adjust_stack (GEN_INT (PROBE_INTERVAL + dope));
2207 }
2208 
2209 /* Return an rtx representing the register or memory location
2210    in which a scalar value of data type VALTYPE
2211    was returned by a function call to function FUNC.
2212    FUNC is a FUNCTION_DECL, FNTYPE a FUNCTION_TYPE node if the precise
2213    function is known, otherwise 0.
2214    OUTGOING is 1 if on a machine with register windows this function
2215    should return the register in which the function will put its result
2216    and 0 otherwise.  */
2217 
2218 rtx
hard_function_value(const_tree valtype,const_tree func,const_tree fntype,int outgoing ATTRIBUTE_UNUSED)2219 hard_function_value (const_tree valtype, const_tree func, const_tree fntype,
2220 		     int outgoing ATTRIBUTE_UNUSED)
2221 {
2222   rtx val;
2223 
2224   val = targetm.calls.function_value (valtype, func ? func : fntype, outgoing);
2225 
2226   if (REG_P (val)
2227       && GET_MODE (val) == BLKmode)
2228     {
2229       unsigned HOST_WIDE_INT bytes = arg_int_size_in_bytes (valtype);
2230       opt_scalar_int_mode tmpmode;
2231 
2232       /* int_size_in_bytes can return -1.  We don't need a check here
2233 	 since the value of bytes will then be large enough that no
2234 	 mode will match anyway.  */
2235 
2236       FOR_EACH_MODE_IN_CLASS (tmpmode, MODE_INT)
2237 	{
2238 	  /* Have we found a large enough mode?  */
2239 	  if (GET_MODE_SIZE (tmpmode.require ()) >= bytes)
2240 	    break;
2241 	}
2242 
2243       PUT_MODE (val, tmpmode.require ());
2244     }
2245   return val;
2246 }
2247 
2248 /* Return an rtx representing the register or memory location
2249    in which a scalar value of mode MODE was returned by a library call.  */
2250 
2251 rtx
hard_libcall_value(machine_mode mode,rtx fun)2252 hard_libcall_value (machine_mode mode, rtx fun)
2253 {
2254   return targetm.calls.libcall_value (mode, fun);
2255 }
2256 
2257 /* Look up the tree code for a given rtx code
2258    to provide the arithmetic operation for real_arithmetic.
2259    The function returns an int because the caller may not know
2260    what `enum tree_code' means.  */
2261 
2262 int
rtx_to_tree_code(enum rtx_code code)2263 rtx_to_tree_code (enum rtx_code code)
2264 {
2265   enum tree_code tcode;
2266 
2267   switch (code)
2268     {
2269     case PLUS:
2270       tcode = PLUS_EXPR;
2271       break;
2272     case MINUS:
2273       tcode = MINUS_EXPR;
2274       break;
2275     case MULT:
2276       tcode = MULT_EXPR;
2277       break;
2278     case DIV:
2279       tcode = RDIV_EXPR;
2280       break;
2281     case SMIN:
2282       tcode = MIN_EXPR;
2283       break;
2284     case SMAX:
2285       tcode = MAX_EXPR;
2286       break;
2287     default:
2288       tcode = LAST_AND_UNUSED_TREE_CODE;
2289       break;
2290     }
2291   return ((int) tcode);
2292 }
2293 
2294 #include "gt-explow.h"
2295