OSDN Git Service

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