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