OSDN Git Service

* explow.c: (copy_all_regs): Don't copy HARD_FRAME_POINTER_REGNUM.
[pf3gnuchains/gcc-fork.git] / gcc / explow.c
1 /* Subroutines for manipulating rtx's in semantically interesting ways.
2    Copyright (C) 1987, 1991 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING.  If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
19
20
21 #include "config.h"
22 #include "rtl.h"
23 #include "tree.h"
24 #include "flags.h"
25 #include "expr.h"
26 #include "hard-reg-set.h"
27 #include "insn-config.h"
28 #include "recog.h"
29 #include "insn-flags.h"
30 #include "insn-codes.h"
31
32 /* Return an rtx for the sum of X and the integer C.
33
34    This function should be used via the `plus_constant' macro.  */
35
36 rtx
37 plus_constant_wide (x, c)
38      register rtx x;
39      register HOST_WIDE_INT c;
40 {
41   register RTX_CODE code;
42   register enum machine_mode mode;
43   register rtx tem;
44   int all_constant = 0;
45
46   if (c == 0)
47     return x;
48
49  restart:
50
51   code = GET_CODE (x);
52   mode = GET_MODE (x);
53   switch (code)
54     {
55     case CONST_INT:
56       return GEN_INT (INTVAL (x) + c);
57
58     case CONST_DOUBLE:
59       {
60         HOST_WIDE_INT l1 = CONST_DOUBLE_LOW (x);
61         HOST_WIDE_INT h1 = CONST_DOUBLE_HIGH (x);
62         HOST_WIDE_INT l2 = c;
63         HOST_WIDE_INT h2 = c < 0 ? ~0 : 0;
64         HOST_WIDE_INT lv, hv;
65
66         add_double (l1, h1, l2, h2, &lv, &hv);
67
68         return immed_double_const (lv, hv, VOIDmode);
69       }
70
71     case MEM:
72       /* If this is a reference to the constant pool, try replacing it with
73          a reference to a new constant.  If the resulting address isn't
74          valid, don't return it because we have no way to validize it.  */
75       if (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
76           && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)))
77         {
78           tem
79             = force_const_mem (GET_MODE (x),
80                                plus_constant (get_pool_constant (XEXP (x, 0)),
81                                               c));
82           if (memory_address_p (GET_MODE (tem), XEXP (tem, 0)))
83             return tem;
84         }
85       break;
86
87     case CONST:
88       /* If adding to something entirely constant, set a flag
89          so that we can add a CONST around the result.  */
90       x = XEXP (x, 0);
91       all_constant = 1;
92       goto restart;
93
94     case SYMBOL_REF:
95     case LABEL_REF:
96       all_constant = 1;
97       break;
98
99     case PLUS:
100       /* The interesting case is adding the integer to a sum.
101          Look for constant term in the sum and combine
102          with C.  For an integer constant term, we make a combined
103          integer.  For a constant term that is not an explicit integer,
104          we cannot really combine, but group them together anyway.  
105
106          Use a recursive call in case the remaining operand is something
107          that we handle specially, such as a SYMBOL_REF.  */
108
109       if (GET_CODE (XEXP (x, 1)) == CONST_INT)
110         return plus_constant (XEXP (x, 0), c + INTVAL (XEXP (x, 1)));
111       else if (CONSTANT_P (XEXP (x, 0)))
112         return gen_rtx (PLUS, mode,
113                         plus_constant (XEXP (x, 0), c),
114                         XEXP (x, 1));
115       else if (CONSTANT_P (XEXP (x, 1)))
116         return gen_rtx (PLUS, mode,
117                         XEXP (x, 0),
118                         plus_constant (XEXP (x, 1), c));
119     }
120
121   if (c != 0)
122     x = gen_rtx (PLUS, mode, x, GEN_INT (c));
123
124   if (GET_CODE (x) == SYMBOL_REF || GET_CODE (x) == LABEL_REF)
125     return x;
126   else if (all_constant)
127     return gen_rtx (CONST, mode, x);
128   else
129     return x;
130 }
131
132 /* This is the same as `plus_constant', except that it handles LO_SUM.
133
134    This function should be used via the `plus_constant_for_output' macro.  */
135
136 rtx
137 plus_constant_for_output_wide (x, c)
138      register rtx x;
139      register HOST_WIDE_INT c;
140 {
141   register RTX_CODE code = GET_CODE (x);
142   register enum machine_mode mode = GET_MODE (x);
143   int all_constant = 0;
144
145   if (GET_CODE (x) == LO_SUM)
146     return gen_rtx (LO_SUM, mode, XEXP (x, 0),
147                     plus_constant_for_output (XEXP (x, 1), c));
148
149   else
150     return plus_constant (x, c);
151 }
152 \f
153 /* If X is a sum, return a new sum like X but lacking any constant terms.
154    Add all the removed constant terms into *CONSTPTR.
155    X itself is not altered.  The result != X if and only if
156    it is not isomorphic to X.  */
157
158 rtx
159 eliminate_constant_term (x, constptr)
160      rtx x;
161      rtx *constptr;
162 {
163   register rtx x0, x1;
164   rtx tem;
165
166   if (GET_CODE (x) != PLUS)
167     return x;
168
169   /* First handle constants appearing at this level explicitly.  */
170   if (GET_CODE (XEXP (x, 1)) == CONST_INT
171       && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x), *constptr,
172                                                 XEXP (x, 1)))
173       && GET_CODE (tem) == CONST_INT)
174     {
175       *constptr = tem;
176       return eliminate_constant_term (XEXP (x, 0), constptr);
177     }
178
179   tem = const0_rtx;
180   x0 = eliminate_constant_term (XEXP (x, 0), &tem);
181   x1 = eliminate_constant_term (XEXP (x, 1), &tem);
182   if ((x1 != XEXP (x, 1) || x0 != XEXP (x, 0))
183       && 0 != (tem = simplify_binary_operation (PLUS, GET_MODE (x),
184                                                 *constptr, tem))
185       && GET_CODE (tem) == CONST_INT)
186     {
187       *constptr = tem;
188       return gen_rtx (PLUS, GET_MODE (x), x0, x1);
189     }
190
191   return x;
192 }
193
194 /* Returns the insn that next references REG after INSN, or 0
195    if REG is clobbered before next referenced or we cannot find
196    an insn that references REG in a straight-line piece of code.  */
197
198 rtx
199 find_next_ref (reg, insn)
200      rtx reg;
201      rtx insn;
202 {
203   rtx next;
204
205   for (insn = NEXT_INSN (insn); insn; insn = next)
206     {
207       next = NEXT_INSN (insn);
208       if (GET_CODE (insn) == NOTE)
209         continue;
210       if (GET_CODE (insn) == CODE_LABEL
211           || GET_CODE (insn) == BARRIER)
212         return 0;
213       if (GET_CODE (insn) == INSN
214           || GET_CODE (insn) == JUMP_INSN
215           || GET_CODE (insn) == CALL_INSN)
216         {
217           if (reg_set_p (reg, insn))
218             return 0;
219           if (reg_mentioned_p (reg, PATTERN (insn)))
220             return insn;
221           if (GET_CODE (insn) == JUMP_INSN)
222             {
223               if (simplejump_p (insn))
224                 next = JUMP_LABEL (insn);
225               else
226                 return 0;
227             }
228           if (GET_CODE (insn) == CALL_INSN
229               && REGNO (reg) < FIRST_PSEUDO_REGISTER
230               && call_used_regs[REGNO (reg)])
231             return 0;
232         }
233       else
234         abort ();
235     }
236   return 0;
237 }
238
239 /* Return an rtx for the size in bytes of the value of EXP.  */
240
241 rtx
242 expr_size (exp)
243      tree exp;
244 {
245   return expand_expr (size_in_bytes (TREE_TYPE (exp)),
246                       NULL_RTX, TYPE_MODE (sizetype), 0);
247 }
248 \f
249 /* Return a copy of X in which all memory references
250    and all constants that involve symbol refs
251    have been replaced with new temporary registers.
252    Also emit code to load the memory locations and constants
253    into those registers.
254
255    If X contains no such constants or memory references,
256    X itself (not a copy) is returned.
257
258    If a constant is found in the address that is not a legitimate constant
259    in an insn, it is left alone in the hope that it might be valid in the
260    address.
261
262    X may contain no arithmetic except addition, subtraction and multiplication.
263    Values returned by expand_expr with 1 for sum_ok fit this constraint.  */
264
265 static rtx
266 break_out_memory_refs (x)
267      register rtx x;
268 {
269   if (GET_CODE (x) == MEM
270       || (CONSTANT_P (x) && CONSTANT_ADDRESS_P (x)
271           && GET_MODE (x) != VOIDmode))
272     {
273       register rtx temp = force_reg (GET_MODE (x), x);
274       mark_reg_pointer (temp);
275       x = temp;
276     }
277   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
278            || GET_CODE (x) == MULT)
279     {
280       register rtx op0 = break_out_memory_refs (XEXP (x, 0));
281       register rtx op1 = break_out_memory_refs (XEXP (x, 1));
282       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
283         x = gen_rtx (GET_CODE (x), Pmode, op0, op1);
284     }
285   return x;
286 }
287
288 /* Given a memory address or facsimile X, construct a new address,
289    currently equivalent, that is stable: future stores won't change it.
290
291    X must be composed of constants, register and memory references
292    combined with addition, subtraction and multiplication:
293    in other words, just what you can get from expand_expr if sum_ok is 1.
294
295    Works by making copies of all regs and memory locations used
296    by X and combining them the same way X does.
297    You could also stabilize the reference to this address
298    by copying the address to a register with copy_to_reg;
299    but then you wouldn't get indexed addressing in the reference.  */
300
301 rtx
302 copy_all_regs (x)
303      register rtx x;
304 {
305   if (GET_CODE (x) == REG)
306     {
307       if (REGNO (x) != FRAME_POINTER_REGNUM
308 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
309           && REGNO (x) != HARD_FRAME_POINTER_REGNUM
310 #endif
311           )
312         x = copy_to_reg (x);
313     }
314   else if (GET_CODE (x) == MEM)
315     x = copy_to_reg (x);
316   else if (GET_CODE (x) == PLUS || GET_CODE (x) == MINUS
317            || GET_CODE (x) == MULT)
318     {
319       register rtx op0 = copy_all_regs (XEXP (x, 0));
320       register rtx op1 = copy_all_regs (XEXP (x, 1));
321       if (op0 != XEXP (x, 0) || op1 != XEXP (x, 1))
322         x = gen_rtx (GET_CODE (x), Pmode, op0, op1);
323     }
324   return x;
325 }
326 \f
327 /* Return something equivalent to X but valid as a memory address
328    for something of mode MODE.  When X is not itself valid, this
329    works by copying X or subexpressions of it into registers.  */
330
331 rtx
332 memory_address (mode, x)
333      enum machine_mode mode;
334      register rtx x;
335 {
336   register rtx oldx;
337
338   /* By passing constant addresses thru registers
339      we get a chance to cse them.  */
340   if (! cse_not_expected && CONSTANT_P (x) && CONSTANT_ADDRESS_P (x))
341     return force_reg (Pmode, x);
342
343   /* Accept a QUEUED that refers to a REG
344      even though that isn't a valid address.
345      On attempting to put this in an insn we will call protect_from_queue
346      which will turn it into a REG, which is valid.  */
347   if (GET_CODE (x) == QUEUED
348       && GET_CODE (QUEUED_VAR (x)) == REG)
349     return x;
350
351   /* We get better cse by rejecting indirect addressing at this stage.
352      Let the combiner create indirect addresses where appropriate.
353      For now, generate the code so that the subexpressions useful to share
354      are visible.  But not if cse won't be done!  */
355   oldx = x;
356   if (! cse_not_expected && GET_CODE (x) != REG)
357     x = break_out_memory_refs (x);
358
359   /* At this point, any valid address is accepted.  */
360   GO_IF_LEGITIMATE_ADDRESS (mode, x, win);
361
362   /* If it was valid before but breaking out memory refs invalidated it,
363      use it the old way.  */
364   if (memory_address_p (mode, oldx))
365     goto win2;
366
367   /* Perform machine-dependent transformations on X
368      in certain cases.  This is not necessary since the code
369      below can handle all possible cases, but machine-dependent
370      transformations can make better code.  */
371   LEGITIMIZE_ADDRESS (x, oldx, mode, win);
372
373   /* PLUS and MULT can appear in special ways
374      as the result of attempts to make an address usable for indexing.
375      Usually they are dealt with by calling force_operand, below.
376      But a sum containing constant terms is special
377      if removing them makes the sum a valid address:
378      then we generate that address in a register
379      and index off of it.  We do this because it often makes
380      shorter code, and because the addresses thus generated
381      in registers often become common subexpressions.  */
382   if (GET_CODE (x) == PLUS)
383     {
384       rtx constant_term = const0_rtx;
385       rtx y = eliminate_constant_term (x, &constant_term);
386       if (constant_term == const0_rtx
387           || ! memory_address_p (mode, y))
388         return force_operand (x, NULL_RTX);
389
390       y = gen_rtx (PLUS, GET_MODE (x), copy_to_reg (y), constant_term);
391       if (! memory_address_p (mode, y))
392         return force_operand (x, NULL_RTX);
393       return y;
394     }
395   if (GET_CODE (x) == MULT || GET_CODE (x) == MINUS)
396     return force_operand (x, NULL_RTX);
397
398   /* If we have a register that's an invalid address,
399      it must be a hard reg of the wrong class.  Copy it to a pseudo.  */
400   if (GET_CODE (x) == REG)
401     return copy_to_reg (x);
402
403   /* Last resort: copy the value to a register, since
404      the register is a valid address.  */
405   return force_reg (Pmode, x);
406
407  win2:
408   x = oldx;
409  win:
410   if (flag_force_addr && ! cse_not_expected && GET_CODE (x) != REG
411       /* Don't copy an addr via a reg if it is one of our stack slots.  */
412       && ! (GET_CODE (x) == PLUS
413             && (XEXP (x, 0) == virtual_stack_vars_rtx
414                 || XEXP (x, 0) == virtual_incoming_args_rtx)))
415     {
416       if (general_operand (x, Pmode))
417         return force_reg (Pmode, x);
418       else
419         return force_operand (x, NULL_RTX);
420     }
421   return x;
422 }
423
424 /* Like `memory_address' but pretend `flag_force_addr' is 0.  */
425
426 rtx
427 memory_address_noforce (mode, x)
428      enum machine_mode mode;
429      rtx x;
430 {
431   int ambient_force_addr = flag_force_addr;
432   rtx val;
433
434   flag_force_addr = 0;
435   val = memory_address (mode, x);
436   flag_force_addr = ambient_force_addr;
437   return val;
438 }
439
440 /* Convert a mem ref into one with a valid memory address.
441    Pass through anything else unchanged.  */
442
443 rtx
444 validize_mem (ref)
445      rtx ref;
446 {
447   if (GET_CODE (ref) != MEM)
448     return ref;
449   if (memory_address_p (GET_MODE (ref), XEXP (ref, 0)))
450     return ref;
451   /* Don't alter REF itself, since that is probably a stack slot.  */
452   return change_address (ref, GET_MODE (ref), XEXP (ref, 0));
453 }
454 \f
455 /* Return a modified copy of X with its memory address copied
456    into a temporary register to protect it from side effects.
457    If X is not a MEM, it is returned unchanged (and not copied).
458    Perhaps even if it is a MEM, if there is no need to change it.  */
459
460 rtx
461 stabilize (x)
462      rtx x;
463 {
464   register rtx addr;
465   if (GET_CODE (x) != MEM)
466     return x;
467   addr = XEXP (x, 0);
468   if (rtx_unstable_p (addr))
469     {
470       rtx temp = copy_all_regs (addr);
471       rtx mem;
472       if (GET_CODE (temp) != REG)
473         temp = copy_to_reg (temp);
474       mem = gen_rtx (MEM, GET_MODE (x), temp);
475
476       /* Mark returned memref with in_struct if it's in an array or
477          structure.  Copy const and volatile from original memref.  */
478
479       MEM_IN_STRUCT_P (mem) = MEM_IN_STRUCT_P (x) || GET_CODE (addr) == PLUS;
480       RTX_UNCHANGING_P (mem) = RTX_UNCHANGING_P (x);
481       MEM_VOLATILE_P (mem) = MEM_VOLATILE_P (x);
482       return mem;
483     }
484   return x;
485 }
486 \f
487 /* Copy the value or contents of X to a new temp reg and return that reg.  */
488
489 rtx
490 copy_to_reg (x)
491      rtx x;
492 {
493   register rtx temp = gen_reg_rtx (GET_MODE (x));
494  
495   /* If not an operand, must be an address with PLUS and MULT so
496      do the computation.  */ 
497   if (! general_operand (x, VOIDmode))
498     x = force_operand (x, temp);
499   
500   if (x != temp)
501     emit_move_insn (temp, x);
502
503   return temp;
504 }
505
506 /* Like copy_to_reg but always give the new register mode Pmode
507    in case X is a constant.  */
508
509 rtx
510 copy_addr_to_reg (x)
511      rtx x;
512 {
513   return copy_to_mode_reg (Pmode, x);
514 }
515
516 /* Like copy_to_reg but always give the new register mode MODE
517    in case X is a constant.  */
518
519 rtx
520 copy_to_mode_reg (mode, x)
521      enum machine_mode mode;
522      rtx x;
523 {
524   register rtx temp = gen_reg_rtx (mode);
525   
526   /* If not an operand, must be an address with PLUS and MULT so
527      do the computation.  */ 
528   if (! general_operand (x, VOIDmode))
529     x = force_operand (x, temp);
530
531   if (GET_MODE (x) != mode && GET_MODE (x) != VOIDmode)
532     abort ();
533   if (x != temp)
534     emit_move_insn (temp, x);
535   return temp;
536 }
537
538 /* Load X into a register if it is not already one.
539    Use mode MODE for the register.
540    X should be valid for mode MODE, but it may be a constant which
541    is valid for all integer modes; that's why caller must specify MODE.
542
543    The caller must not alter the value in the register we return,
544    since we mark it as a "constant" register.  */
545
546 rtx
547 force_reg (mode, x)
548      enum machine_mode mode;
549      rtx x;
550 {
551   register rtx temp, insn;
552
553   if (GET_CODE (x) == REG)
554     return x;
555   temp = gen_reg_rtx (mode);
556   insn = emit_move_insn (temp, x);
557   /* Let optimizers know that TEMP's value never changes
558      and that X can be substituted for it.  */
559   if (CONSTANT_P (x))
560     {
561       rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
562
563       if (note)
564         XEXP (note, 0) = x;
565       else
566         REG_NOTES (insn) = gen_rtx (EXPR_LIST, REG_EQUAL, x, REG_NOTES (insn));
567     }
568   return temp;
569 }
570
571 /* If X is a memory ref, copy its contents to a new temp reg and return
572    that reg.  Otherwise, return X.  */
573
574 rtx
575 force_not_mem (x)
576      rtx x;
577 {
578   register rtx temp;
579   if (GET_CODE (x) != MEM || GET_MODE (x) == BLKmode)
580     return x;
581   temp = gen_reg_rtx (GET_MODE (x));
582   emit_move_insn (temp, x);
583   return temp;
584 }
585
586 /* Copy X to TARGET (if it's nonzero and a reg)
587    or to a new temp reg and return that reg.
588    MODE is the mode to use for X in case it is a constant.  */
589
590 rtx
591 copy_to_suggested_reg (x, target, mode)
592      rtx x, target;
593      enum machine_mode mode;
594 {
595   register rtx temp;
596
597   if (target && GET_CODE (target) == REG)
598     temp = target;
599   else
600     temp = gen_reg_rtx (mode);
601
602   emit_move_insn (temp, x);
603   return temp;
604 }
605 \f
606 /* Adjust the stack pointer by ADJUST (an rtx for a number of bytes).
607    This pops when ADJUST is positive.  ADJUST need not be constant.  */
608
609 void
610 adjust_stack (adjust)
611      rtx adjust;
612 {
613   rtx temp;
614   adjust = protect_from_queue (adjust, 0);
615
616   if (adjust == const0_rtx)
617     return;
618
619   temp = expand_binop (Pmode,
620 #ifdef STACK_GROWS_DOWNWARD
621                        add_optab,
622 #else
623                        sub_optab,
624 #endif
625                        stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
626                        OPTAB_LIB_WIDEN);
627
628   if (temp != stack_pointer_rtx)
629     emit_move_insn (stack_pointer_rtx, temp);
630 }
631
632 /* Adjust the stack pointer by minus ADJUST (an rtx for a number of bytes).
633    This pushes when ADJUST is positive.  ADJUST need not be constant.  */
634
635 void
636 anti_adjust_stack (adjust)
637      rtx adjust;
638 {
639   rtx temp;
640   adjust = protect_from_queue (adjust, 0);
641
642   if (adjust == const0_rtx)
643     return;
644
645   temp = expand_binop (Pmode,
646 #ifdef STACK_GROWS_DOWNWARD
647                        sub_optab,
648 #else
649                        add_optab,
650 #endif
651                        stack_pointer_rtx, adjust, stack_pointer_rtx, 0,
652                        OPTAB_LIB_WIDEN);
653
654   if (temp != stack_pointer_rtx)
655     emit_move_insn (stack_pointer_rtx, temp);
656 }
657
658 /* Round the size of a block to be pushed up to the boundary required
659    by this machine.  SIZE is the desired size, which need not be constant.  */
660
661 rtx
662 round_push (size)
663      rtx size;
664 {
665 #ifdef STACK_BOUNDARY
666   int align = STACK_BOUNDARY / BITS_PER_UNIT;
667   if (align == 1)
668     return size;
669   if (GET_CODE (size) == CONST_INT)
670     {
671       int new = (INTVAL (size) + align - 1) / align * align;
672       if (INTVAL (size) != new)
673         size = GEN_INT (new);
674     }
675   else
676     {
677       size = expand_divmod (0, CEIL_DIV_EXPR, Pmode, size, GEN_INT (align),
678                             NULL_RTX, 1);
679       size = expand_mult (Pmode, size, GEN_INT (align), NULL_RTX, 1);
680     }
681 #endif /* STACK_BOUNDARY */
682   return size;
683 }
684 \f
685 /* Save the stack pointer for the purpose in SAVE_LEVEL.  PSAVE is a pointer
686    to a previously-created save area.  If no save area has been allocated,
687    this function will allocate one.  If a save area is specified, it
688    must be of the proper mode.
689
690    The insns are emitted after insn AFTER, if nonzero, otherwise the insns
691    are emitted at the current position.  */
692
693 void
694 emit_stack_save (save_level, psave, after)
695      enum save_level save_level;
696      rtx *psave;
697      rtx after;
698 {
699   rtx sa = *psave;
700   /* The default is that we use a move insn and save in a Pmode object.  */
701   rtx (*fcn) () = gen_move_insn;
702   enum machine_mode mode = Pmode;
703
704   /* See if this machine has anything special to do for this kind of save.  */
705   switch (save_level)
706     {
707 #ifdef HAVE_save_stack_block
708     case SAVE_BLOCK:
709       if (HAVE_save_stack_block)
710         {
711           fcn = gen_save_stack_block;
712           mode = insn_operand_mode[CODE_FOR_save_stack_block][0];
713         }
714       break;
715 #endif
716 #ifdef HAVE_save_stack_function
717     case SAVE_FUNCTION:
718       if (HAVE_save_stack_function)
719         {
720           fcn = gen_save_stack_function;
721           mode = insn_operand_mode[CODE_FOR_save_stack_function][0];
722         }
723       break;
724 #endif
725 #ifdef HAVE_save_stack_nonlocal
726     case SAVE_NONLOCAL:
727       if (HAVE_save_stack_nonlocal)
728         {
729           fcn = gen_save_stack_nonlocal;
730           mode = insn_operand_mode[CODE_FOR_save_stack_nonlocal][0];
731         }
732       break;
733 #endif
734     }
735
736   /* If there is no save area and we have to allocate one, do so.  Otherwise
737      verify the save area is the proper mode.  */
738
739   if (sa == 0)
740     {
741       if (mode != VOIDmode)
742         {
743           if (save_level == SAVE_NONLOCAL)
744             *psave = sa = assign_stack_local (mode, GET_MODE_SIZE (mode), 0);
745           else
746             *psave = sa = gen_reg_rtx (mode);
747         }
748     }
749   else
750     {
751       if (mode == VOIDmode || GET_MODE (sa) != mode)
752         abort ();
753     }
754
755   if (after)
756     {
757       rtx seq;
758
759       start_sequence ();
760       /* We must validize inside the sequence, to ensure that any instructions
761          created by the validize call also get moved to the right place.  */
762       if (sa != 0)
763         sa = validize_mem (sa);
764       emit_insn (fcn (sa, stack_pointer_rtx));
765       seq = gen_sequence ();
766       end_sequence ();
767       emit_insn_after (seq, after);
768     }
769   else
770     {
771       if (sa != 0)
772         sa = validize_mem (sa);
773       emit_insn (fcn (sa, stack_pointer_rtx));
774     }
775 }
776
777 /* Restore the stack pointer for the purpose in SAVE_LEVEL.  SA is the save
778    area made by emit_stack_save.  If it is zero, we have nothing to do. 
779
780    Put any emitted insns after insn AFTER, if nonzero, otherwise at 
781    current position.  */
782
783 void
784 emit_stack_restore (save_level, sa, after)
785      enum save_level save_level;
786      rtx after;
787      rtx sa;
788 {
789   /* The default is that we use a move insn.  */
790   rtx (*fcn) () = gen_move_insn;
791
792   /* See if this machine has anything special to do for this kind of save.  */
793   switch (save_level)
794     {
795 #ifdef HAVE_restore_stack_block
796     case SAVE_BLOCK:
797       if (HAVE_restore_stack_block)
798         fcn = gen_restore_stack_block;
799       break;
800 #endif
801 #ifdef HAVE_restore_stack_function
802     case SAVE_FUNCTION:
803       if (HAVE_restore_stack_function)
804         fcn = gen_restore_stack_function;
805       break;
806 #endif
807 #ifdef HAVE_restore_stack_nonlocal
808
809     case SAVE_NONLOCAL:
810       if (HAVE_restore_stack_nonlocal)
811         fcn = gen_restore_stack_nonlocal;
812       break;
813 #endif
814     }
815
816   if (sa != 0)
817     sa = validize_mem (sa);
818
819   if (after)
820     {
821       rtx seq;
822
823       start_sequence ();
824       emit_insn (fcn (stack_pointer_rtx, sa));
825       seq = gen_sequence ();
826       end_sequence ();
827       emit_insn_after (seq, after);
828     }
829   else
830     emit_insn (fcn (stack_pointer_rtx, sa));
831 }
832 \f
833 /* Return an rtx representing the address of an area of memory dynamically
834    pushed on the stack.  This region of memory is always aligned to
835    a multiple of BIGGEST_ALIGNMENT.
836
837    Any required stack pointer alignment is preserved.
838
839    SIZE is an rtx representing the size of the area.
840    TARGET is a place in which the address can be placed.
841
842    KNOWN_ALIGN is the alignment (in bits) that we know SIZE has.  */
843
844 rtx
845 allocate_dynamic_stack_space (size, target, known_align)
846      rtx size;
847      rtx target;
848      int known_align;
849 {
850   /* Ensure the size is in the proper mode.  */
851   if (GET_MODE (size) != VOIDmode && GET_MODE (size) != Pmode)
852     size = convert_to_mode (Pmode, size, 1);
853
854   /* We will need to ensure that the address we return is aligned to
855      BIGGEST_ALIGNMENT.  If STACK_DYNAMIC_OFFSET is defined, we don't
856      always know its final value at this point in the compilation (it 
857      might depend on the size of the outgoing parameter lists, for
858      example), so we must align the value to be returned in that case.
859      (Note that STACK_DYNAMIC_OFFSET will have a default non-zero value if
860      STACK_POINTER_OFFSET or ACCUMULATE_OUTGOING_ARGS are defined).
861      We must also do an alignment operation on the returned value if
862      the stack pointer alignment is less strict that BIGGEST_ALIGNMENT.
863
864      If we have to align, we must leave space in SIZE for the hole
865      that might result from the alignment operation.  */
866
867 #if defined (STACK_DYNAMIC_OFFSET) || defined(STACK_POINTER_OFFSET) || defined (ALLOCATE_OUTGOING_ARGS)
868 #define MUST_ALIGN
869 #endif
870
871 #if ! defined (MUST_ALIGN) && (!defined(STACK_BOUNDARY) || STACK_BOUNDARY < BIGGEST_ALIGNMENT)
872 #define MUST_ALIGN
873 #endif
874
875 #ifdef MUST_ALIGN
876
877 #if 0 /* It turns out we must always make extra space, if MUST_ALIGN
878          because we must always round the address up at the end,
879          because we don't know whether the dynamic offset
880          will mess up the desired alignment.  */
881   /* If we have to round the address up regardless of known_align,
882      make extra space regardless, also.  */
883   if (known_align % BIGGEST_ALIGNMENT != 0)
884 #endif
885     {
886       if (GET_CODE (size) == CONST_INT)
887         size = GEN_INT (INTVAL (size)
888                         + (BIGGEST_ALIGNMENT / BITS_PER_UNIT - 1));
889       else
890         size = expand_binop (Pmode, add_optab, size,
891                              GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT - 1),
892                              NULL_RTX, 1, OPTAB_LIB_WIDEN);
893     }
894
895 #endif
896
897 #ifdef SETJMP_VIA_SAVE_AREA
898   /* If setjmp restores regs from a save area in the stack frame,
899      avoid clobbering the reg save area.  Note that the offset of
900      virtual_incoming_args_rtx includes the preallocated stack args space.
901      It would be no problem to clobber that, but it's on the wrong side
902      of the old save area.  */
903   {
904     rtx dynamic_offset
905       = expand_binop (Pmode, sub_optab, virtual_stack_dynamic_rtx,
906                       stack_pointer_rtx, NULL_RTX, 1, OPTAB_LIB_WIDEN);
907     size = expand_binop (Pmode, add_optab, size, dynamic_offset,
908                          NULL_RTX, 1, OPTAB_LIB_WIDEN);
909   }
910 #endif /* SETJMP_VIA_SAVE_AREA */
911
912   /* Round the size to a multiple of the required stack alignment.
913      Since the stack if presumed to be rounded before this allocation,
914      this will maintain the required alignment.
915
916      If the stack grows downward, we could save an insn by subtracting
917      SIZE from the stack pointer and then aligning the stack pointer.
918      The problem with this is that the stack pointer may be unaligned
919      between the execution of the subtraction and alignment insns and
920      some machines do not allow this.  Even on those that do, some
921      signal handlers malfunction if a signal should occur between those
922      insns.  Since this is an extremely rare event, we have no reliable
923      way of knowing which systems have this problem.  So we avoid even
924      momentarily mis-aligning the stack.  */
925
926 #ifdef STACK_BOUNDARY
927   /* If we added a variable amount to SIZE,
928      we can no longer assume it is aligned.  */
929 #if !defined (SETJMP_VIA_SAVE_AREA) && !defined (MUST_ALIGN)
930   if (known_align % STACK_BOUNDARY != 0)
931 #endif
932     size = round_push (size);
933 #endif
934
935   do_pending_stack_adjust ();
936
937   /* Don't use a TARGET that isn't a pseudo.  */
938   if (target == 0 || GET_CODE (target) != REG
939       || REGNO (target) < FIRST_PSEUDO_REGISTER)
940     target = gen_reg_rtx (Pmode);
941
942   mark_reg_pointer (target);
943
944 #ifndef STACK_GROWS_DOWNWARD
945   emit_move_insn (target, virtual_stack_dynamic_rtx);
946 #endif
947
948   /* Perform the required allocation from the stack.  Some systems do
949      this differently than simply incrementing/decrementing from the
950      stack pointer.  */
951 #ifdef HAVE_allocate_stack
952   if (HAVE_allocate_stack)
953     {
954       enum machine_mode mode
955         = insn_operand_mode[(int) CODE_FOR_allocate_stack][0];
956
957       if (insn_operand_predicate[(int) CODE_FOR_allocate_stack][0]
958           && ! ((*insn_operand_predicate[(int) CODE_FOR_allocate_stack][0])
959                 (size, mode)))
960         size = copy_to_mode_reg (mode, size);
961
962       emit_insn (gen_allocate_stack (size));
963     }
964   else
965 #endif
966     anti_adjust_stack (size);
967
968 #ifdef STACK_GROWS_DOWNWARD
969   emit_move_insn (target, virtual_stack_dynamic_rtx);
970 #endif
971
972 #ifdef MUST_ALIGN
973 #if 0  /* Even if we know the stack pointer has enough alignment,
974           there's no way to tell whether virtual_stack_dynamic_rtx shares that
975           alignment, so we still need to round the address up.  */
976   if (known_align % BIGGEST_ALIGNMENT != 0)
977 #endif
978     {
979       target = expand_divmod (0, CEIL_DIV_EXPR, Pmode, target,
980                               GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT),
981                               NULL_RTX, 1);
982
983       target = expand_mult (Pmode, target,
984                             GEN_INT (BIGGEST_ALIGNMENT / BITS_PER_UNIT),
985                             NULL_RTX, 1);
986     }
987 #endif
988   
989   /* Some systems require a particular insn to refer to the stack
990      to make the pages exist.  */
991 #ifdef HAVE_probe
992   if (HAVE_probe)
993     emit_insn (gen_probe ());
994 #endif
995
996   return target;
997 }
998 \f
999 /* Return an rtx representing the register or memory location
1000    in which a scalar value of data type VALTYPE
1001    was returned by a function call to function FUNC.
1002    FUNC is a FUNCTION_DECL node if the precise function is known,
1003    otherwise 0.  */
1004
1005 rtx
1006 hard_function_value (valtype, func)
1007      tree valtype;
1008      tree func;
1009 {
1010   return FUNCTION_VALUE (valtype, func);
1011 }
1012
1013 /* Return an rtx representing the register or memory location
1014    in which a scalar value of mode MODE was returned by a library call.  */
1015
1016 rtx
1017 hard_libcall_value (mode)
1018      enum machine_mode mode;
1019 {
1020   return LIBCALL_VALUE (mode);
1021 }
1022
1023 /* Look up the tree code for a given rtx code
1024    to provide the arithmetic operation for REAL_ARITHMETIC.
1025    The function returns an int because the caller may not know
1026    what `enum tree_code' means.  */
1027
1028 int
1029 rtx_to_tree_code (code)
1030      enum rtx_code code;
1031 {
1032   enum tree_code tcode;
1033
1034   switch (code)
1035     {
1036     case PLUS:
1037       tcode = PLUS_EXPR;
1038       break;
1039     case MINUS:
1040       tcode = MINUS_EXPR;
1041       break;
1042     case MULT:
1043       tcode = MULT_EXPR;
1044       break;
1045     case DIV:
1046       tcode = RDIV_EXPR;
1047       break;
1048     case SMIN:
1049       tcode = MIN_EXPR;
1050       break;
1051     case SMAX:
1052       tcode = MAX_EXPR;
1053       break;
1054     default:
1055       tcode = LAST_AND_UNUSED_TREE_CODE;
1056       break;
1057     }
1058   return ((int) tcode);
1059 }