OSDN Git Service

Fix a warning in copy_insn_1
[pf3gnuchains/gcc-fork.git] / gcc / emit-rtl.c
1 /* Emit RTL for the GNU C-Compiler expander.
2    Copyright (C) 1987, 88, 92-97, 1998, 1999 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, 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA.  */
20
21
22 /* Middle-to-low level generation of rtx code and insns.
23
24    This file contains the functions `gen_rtx', `gen_reg_rtx'
25    and `gen_label_rtx' that are the usual ways of creating rtl
26    expressions for most purposes.
27
28    It also has the functions for creating insns and linking
29    them in the doubly-linked chain.
30
31    The patterns of the insns are created by machine-dependent
32    routines in insn-emit.c, which is generated automatically from
33    the machine description.  These routines use `gen_rtx' to make
34    the individual rtx's of the pattern; what is machine dependent
35    is the kind of rtx's they make and what arguments they use.  */
36
37 #include "config.h"
38 #include "system.h"
39 #include "toplev.h"
40 #include "rtl.h"
41 #include "tree.h"
42 #include "tm_p.h"
43 #include "flags.h"
44 #include "function.h"
45 #include "expr.h"
46 #include "regs.h"
47 #include "hard-reg-set.h"
48 #include "insn-config.h"
49 #include "recog.h"
50 #include "real.h"
51 #include "obstack.h"
52 #include "bitmap.h"
53 #include "ggc.h"
54
55 /* Commonly used modes.  */
56
57 enum machine_mode byte_mode;    /* Mode whose width is BITS_PER_UNIT.  */
58 enum machine_mode word_mode;    /* Mode whose width is BITS_PER_WORD.  */
59 enum machine_mode double_mode;  /* Mode whose width is DOUBLE_TYPE_SIZE.  */
60 enum machine_mode ptr_mode;     /* Mode whose width is POINTER_SIZE.  */
61
62
63 /* This is *not* reset after each function.  It gives each CODE_LABEL
64    in the entire compilation a unique label number.  */
65
66 static int label_num = 1;
67
68 /* Highest label number in current function.
69    Zero means use the value of label_num instead.
70    This is nonzero only when belatedly compiling an inline function.  */
71
72 static int last_label_num;
73
74 /* Value label_num had when set_new_first_and_last_label_number was called.
75    If label_num has not changed since then, last_label_num is valid.  */
76
77 static int base_label_num;
78
79 /* Nonzero means do not generate NOTEs for source line numbers.  */
80
81 static int no_line_numbers;
82
83 /* Commonly used rtx's, so that we only need space for one copy.
84    These are initialized once for the entire compilation.
85    All of these except perhaps the floating-point CONST_DOUBLEs
86    are unique; no other rtx-object will be equal to any of these.  */
87
88 rtx global_rtl[GR_MAX];
89
90 /* We record floating-point CONST_DOUBLEs in each floating-point mode for
91    the values of 0, 1, and 2.  For the integer entries and VOIDmode, we
92    record a copy of const[012]_rtx.  */
93
94 rtx const_tiny_rtx[3][(int) MAX_MACHINE_MODE];
95
96 rtx const_true_rtx;
97
98 REAL_VALUE_TYPE dconst0;
99 REAL_VALUE_TYPE dconst1;
100 REAL_VALUE_TYPE dconst2;
101 REAL_VALUE_TYPE dconstm1;
102
103 /* All references to the following fixed hard registers go through
104    these unique rtl objects.  On machines where the frame-pointer and
105    arg-pointer are the same register, they use the same unique object.
106
107    After register allocation, other rtl objects which used to be pseudo-regs
108    may be clobbered to refer to the frame-pointer register.
109    But references that were originally to the frame-pointer can be
110    distinguished from the others because they contain frame_pointer_rtx.
111
112    When to use frame_pointer_rtx and hard_frame_pointer_rtx is a little
113    tricky: until register elimination has taken place hard_frame_pointer_rtx
114    should be used if it is being set, and frame_pointer_rtx otherwise.  After 
115    register elimination hard_frame_pointer_rtx should always be used.
116    On machines where the two registers are same (most) then these are the
117    same.
118
119    In an inline procedure, the stack and frame pointer rtxs may not be
120    used for anything else.  */
121 rtx struct_value_rtx;           /* (REG:Pmode STRUCT_VALUE_REGNUM) */
122 rtx struct_value_incoming_rtx;  /* (REG:Pmode STRUCT_VALUE_INCOMING_REGNUM) */
123 rtx static_chain_rtx;           /* (REG:Pmode STATIC_CHAIN_REGNUM) */
124 rtx static_chain_incoming_rtx;  /* (REG:Pmode STATIC_CHAIN_INCOMING_REGNUM) */
125 rtx pic_offset_table_rtx;       /* (REG:Pmode PIC_OFFSET_TABLE_REGNUM) */
126
127 /* This is used to implement __builtin_return_address for some machines.
128    See for instance the MIPS port.  */
129 rtx return_address_pointer_rtx; /* (REG:Pmode RETURN_ADDRESS_POINTER_REGNUM) */
130
131 /* We make one copy of (const_int C) where C is in
132    [- MAX_SAVED_CONST_INT, MAX_SAVED_CONST_INT]
133    to save space during the compilation and simplify comparisons of
134    integers.  */
135
136 rtx const_int_rtx[MAX_SAVED_CONST_INT * 2 + 1];
137
138 /* start_sequence and gen_sequence can make a lot of rtx expressions which are
139    shortly thrown away.  We use two mechanisms to prevent this waste:
140
141    For sizes up to 5 elements, we keep a SEQUENCE and its associated
142    rtvec for use by gen_sequence.  One entry for each size is
143    sufficient because most cases are calls to gen_sequence followed by
144    immediately emitting the SEQUENCE.  Reuse is safe since emitting a
145    sequence is destructive on the insn in it anyway and hence can't be
146    redone.
147
148    We do not bother to save this cached data over nested function calls.
149    Instead, we just reinitialize them.  */
150
151 #define SEQUENCE_RESULT_SIZE 5
152
153 static rtx sequence_result[SEQUENCE_RESULT_SIZE];
154
155 /* During RTL generation, we also keep a list of free INSN rtl codes.  */
156 static rtx free_insn;
157
158 #define first_insn (current_function->emit->x_first_insn)
159 #define last_insn (current_function->emit->x_last_insn)
160 #define cur_insn_uid (current_function->emit->x_cur_insn_uid)
161 #define last_linenum (current_function->emit->x_last_linenum)
162 #define last_filename (current_function->emit->x_last_filename)
163 #define first_label_num (current_function->emit->x_first_label_num)
164
165 /* This is where the pointer to the obstack being used for RTL is stored.  */
166 extern struct obstack *rtl_obstack;
167
168 static rtx make_jump_insn_raw           PROTO((rtx));
169 static rtx make_call_insn_raw           PROTO((rtx));
170 static rtx find_line_note               PROTO((rtx));
171 static void mark_sequence_stack         PROTO((struct sequence_stack *));
172 \f
173 /* There are some RTL codes that require special attention; the generation
174    functions do the raw handling.  If you add to this list, modify
175    special_rtx in gengenrtl.c as well.  */
176
177 rtx
178 gen_rtx_CONST_INT (mode, arg)
179      enum machine_mode mode;
180      HOST_WIDE_INT arg;
181 {
182   if (arg >= - MAX_SAVED_CONST_INT && arg <= MAX_SAVED_CONST_INT)
183     return const_int_rtx[arg + MAX_SAVED_CONST_INT];
184
185 #if STORE_FLAG_VALUE != 1 && STORE_FLAG_VALUE != -1
186   if (const_true_rtx && arg == STORE_FLAG_VALUE)
187     return const_true_rtx;
188 #endif
189
190   return gen_rtx_raw_CONST_INT (mode, arg);
191 }
192
193 /* CONST_DOUBLEs needs special handling because its length is known
194    only at run-time.  */
195 rtx
196 gen_rtx_CONST_DOUBLE (mode, arg0, arg1, arg2)
197      enum machine_mode mode;
198      rtx arg0;
199      HOST_WIDE_INT arg1, arg2;
200 {
201   rtx r = rtx_alloc (CONST_DOUBLE);
202   int i;
203
204   PUT_MODE (r, mode);
205   XEXP (r, 0) = arg0;
206   X0EXP (r, 1) = NULL_RTX;
207   XWINT (r, 2) = arg1;
208   XWINT (r, 3) = arg2;
209
210   for (i = GET_RTX_LENGTH (CONST_DOUBLE) - 1; i > 3; --i)
211     XWINT (r, i) = 0;
212
213   return r;
214 }
215
216 rtx
217 gen_rtx_REG (mode, regno)
218      enum machine_mode mode;
219      int regno;
220 {
221   /* In case the MD file explicitly references the frame pointer, have
222      all such references point to the same frame pointer.  This is
223      used during frame pointer elimination to distinguish the explicit
224      references to these registers from pseudos that happened to be
225      assigned to them.
226
227      If we have eliminated the frame pointer or arg pointer, we will
228      be using it as a normal register, for example as a spill
229      register.  In such cases, we might be accessing it in a mode that
230      is not Pmode and therefore cannot use the pre-allocated rtx.
231
232      Also don't do this when we are making new REGs in reload, since
233      we don't want to get confused with the real pointers.  */
234
235   if (mode == Pmode && !reload_in_progress)
236     {
237       if (regno == FRAME_POINTER_REGNUM)
238         return frame_pointer_rtx;
239 #if FRAME_POINTER_REGNUM != HARD_FRAME_POINTER_REGNUM
240       if (regno == HARD_FRAME_POINTER_REGNUM)
241         return hard_frame_pointer_rtx;
242 #endif
243 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM && HARD_FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
244       if (regno == ARG_POINTER_REGNUM)
245         return arg_pointer_rtx;
246 #endif
247 #ifdef RETURN_ADDRESS_POINTER_REGNUM
248       if (regno == RETURN_ADDRESS_POINTER_REGNUM)
249         return return_address_pointer_rtx;
250 #endif
251       if (regno == STACK_POINTER_REGNUM)
252         return stack_pointer_rtx;
253     }
254
255   return gen_rtx_raw_REG (mode, regno);
256 }
257
258 rtx
259 gen_rtx_MEM (mode, addr)
260      enum machine_mode mode;
261      rtx addr;
262 {
263   rtx rt = gen_rtx_raw_MEM (mode, addr);
264
265   /* This field is not cleared by the mere allocation of the rtx, so
266      we clear it here.  */
267   MEM_ALIAS_SET (rt) = 0;
268
269   return rt;
270 }
271 \f
272 /* rtx gen_rtx (code, mode, [element1, ..., elementn])
273 **
274 **          This routine generates an RTX of the size specified by
275 **      <code>, which is an RTX code.   The RTX structure is initialized
276 **      from the arguments <element1> through <elementn>, which are
277 **      interpreted according to the specific RTX type's format.   The
278 **      special machine mode associated with the rtx (if any) is specified
279 **      in <mode>.
280 **
281 **          gen_rtx can be invoked in a way which resembles the lisp-like
282 **      rtx it will generate.   For example, the following rtx structure:
283 **
284 **            (plus:QI (mem:QI (reg:SI 1))
285 **                     (mem:QI (plusw:SI (reg:SI 2) (reg:SI 3))))
286 **
287 **              ...would be generated by the following C code:
288 **
289 **              gen_rtx (PLUS, QImode,
290 **                  gen_rtx (MEM, QImode,
291 **                      gen_rtx (REG, SImode, 1)),
292 **                  gen_rtx (MEM, QImode,
293 **                      gen_rtx (PLUS, SImode,
294 **                          gen_rtx (REG, SImode, 2),
295 **                          gen_rtx (REG, SImode, 3)))),
296 */
297
298 /*VARARGS2*/
299 rtx
300 gen_rtx VPROTO((enum rtx_code code, enum machine_mode mode, ...))
301 {
302 #ifndef ANSI_PROTOTYPES
303   enum rtx_code code;
304   enum machine_mode mode;
305 #endif
306   va_list p;
307   register int i;               /* Array indices...                     */
308   register const char *fmt;     /* Current rtx's format...              */
309   register rtx rt_val;          /* RTX to return to caller...           */
310
311   VA_START (p, mode);
312
313 #ifndef ANSI_PROTOTYPES
314   code = va_arg (p, enum rtx_code);
315   mode = va_arg (p, enum machine_mode);
316 #endif
317
318   switch (code)
319     {
320     case CONST_INT:
321       rt_val = gen_rtx_CONST_INT (mode, va_arg (p, HOST_WIDE_INT));
322       break;
323
324     case CONST_DOUBLE:
325       {
326         rtx arg0 = va_arg (p, rtx);
327         HOST_WIDE_INT arg1 = va_arg (p, HOST_WIDE_INT);
328         HOST_WIDE_INT arg2 = va_arg (p, HOST_WIDE_INT);
329         rt_val = gen_rtx_CONST_DOUBLE (mode, arg0, arg1, arg2);
330       }
331       break;
332
333     case REG:
334       rt_val = gen_rtx_REG (mode, va_arg (p, int));
335       break;
336
337     case MEM:
338       rt_val = gen_rtx_MEM (mode, va_arg (p, rtx));
339       break;
340
341     default:
342       rt_val = rtx_alloc (code);        /* Allocate the storage space.  */
343       rt_val->mode = mode;              /* Store the machine mode...  */
344
345       fmt = GET_RTX_FORMAT (code);      /* Find the right format...  */
346       for (i = 0; i < GET_RTX_LENGTH (code); i++)
347         {
348           switch (*fmt++)
349             {
350             case '0':           /* Unused field.  */
351               break;
352
353             case 'i':           /* An integer?  */
354               XINT (rt_val, i) = va_arg (p, int);
355               break;
356
357             case 'w':           /* A wide integer? */
358               XWINT (rt_val, i) = va_arg (p, HOST_WIDE_INT);
359               break;
360
361             case 's':           /* A string?  */
362               XSTR (rt_val, i) = va_arg (p, char *);
363               break;
364
365             case 'e':           /* An expression?  */
366             case 'u':           /* An insn?  Same except when printing.  */
367               XEXP (rt_val, i) = va_arg (p, rtx);
368               break;
369
370             case 'E':           /* An RTX vector?  */
371               XVEC (rt_val, i) = va_arg (p, rtvec);
372               break;
373
374             case 'b':           /* A bitmap? */
375               XBITMAP (rt_val, i) = va_arg (p, bitmap);
376               break;
377
378             case 't':           /* A tree? */
379               XTREE (rt_val, i) = va_arg (p, tree);
380               break;
381
382             default:
383               abort ();
384             }
385         }
386       break;
387     }
388
389   va_end (p);
390   return rt_val;
391 }
392
393 /* gen_rtvec (n, [rt1, ..., rtn])
394 **
395 **          This routine creates an rtvec and stores within it the
396 **      pointers to rtx's which are its arguments.
397 */
398
399 /*VARARGS1*/
400 rtvec
401 gen_rtvec VPROTO((int n, ...))
402 {
403 #ifndef ANSI_PROTOTYPES
404   int n;
405 #endif
406   int i;
407   va_list p;
408   rtx *vector;
409
410   VA_START (p, n);
411
412 #ifndef ANSI_PROTOTYPES
413   n = va_arg (p, int);
414 #endif
415
416   if (n == 0)
417     return NULL_RTVEC;          /* Don't allocate an empty rtvec...     */
418
419   vector = (rtx *) alloca (n * sizeof (rtx));
420
421   for (i = 0; i < n; i++)
422     vector[i] = va_arg (p, rtx);
423   va_end (p);
424
425   return gen_rtvec_v (n, vector);
426 }
427
428 rtvec
429 gen_rtvec_v (n, argp)
430      int n;
431      rtx *argp;
432 {
433   register int i;
434   register rtvec rt_val;
435
436   if (n == 0)
437     return NULL_RTVEC;          /* Don't allocate an empty rtvec...     */
438
439   rt_val = rtvec_alloc (n);     /* Allocate an rtvec...                 */
440
441   for (i = 0; i < n; i++)
442     rt_val->elem[i] = *argp++;
443
444   return rt_val;
445 }
446
447 \f
448 /* Generate a REG rtx for a new pseudo register of mode MODE.
449    This pseudo is assigned the next sequential register number.  */
450
451 rtx
452 gen_reg_rtx (mode)
453      enum machine_mode mode;
454 {
455   struct function *f = current_function;
456   register rtx val;
457
458   /* Don't let anything called after initial flow analysis create new
459      registers.  */
460   if (no_new_pseudos)
461     abort ();
462
463   if (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
464       || GET_MODE_CLASS (mode) == MODE_COMPLEX_INT)
465     {
466       /* For complex modes, don't make a single pseudo.
467          Instead, make a CONCAT of two pseudos.
468          This allows noncontiguous allocation of the real and imaginary parts,
469          which makes much better code.  Besides, allocating DCmode
470          pseudos overstrains reload on some machines like the 386.  */
471       rtx realpart, imagpart;
472       int size = GET_MODE_UNIT_SIZE (mode);
473       enum machine_mode partmode
474         = mode_for_size (size * BITS_PER_UNIT,
475                          (GET_MODE_CLASS (mode) == MODE_COMPLEX_FLOAT
476                           ? MODE_FLOAT : MODE_INT),
477                          0);
478
479       realpart = gen_reg_rtx (partmode);
480       imagpart = gen_reg_rtx (partmode);
481       return gen_rtx_CONCAT (mode, realpart, imagpart);
482     }
483
484   /* Make sure regno_pointer_flag and regno_reg_rtx are large
485      enough to have an element for this pseudo reg number.  */
486
487   if (reg_rtx_no == f->emit->regno_pointer_flag_length)
488     {
489       int old_size = f->emit->regno_pointer_flag_length;
490       rtx *new1;
491       char *new;
492       new = xrealloc (f->emit->regno_pointer_flag, old_size * 2);
493       memset (new + old_size, 0, old_size);
494       f->emit->regno_pointer_flag = new;
495
496       new = xrealloc (f->emit->regno_pointer_align, old_size * 2);
497       memset (new + old_size, 0, old_size);
498       f->emit->regno_pointer_align = new;
499
500       new1 = (rtx *) xrealloc (f->emit->x_regno_reg_rtx,
501                                old_size * 2 * sizeof (rtx));
502       memset (new1 + old_size, 0, old_size * sizeof (rtx));
503       regno_reg_rtx = new1;
504
505       f->emit->regno_pointer_flag_length = old_size * 2;
506     }
507
508   val = gen_rtx_raw_REG (mode, reg_rtx_no);
509   regno_reg_rtx[reg_rtx_no++] = val;
510   return val;
511 }
512
513 /* Identify REG (which may be a CONCAT) as a user register.  */
514
515 void
516 mark_user_reg (reg)
517      rtx reg;
518 {
519   if (GET_CODE (reg) == CONCAT)
520     {
521       REG_USERVAR_P (XEXP (reg, 0)) = 1;
522       REG_USERVAR_P (XEXP (reg, 1)) = 1;
523     }
524   else if (GET_CODE (reg) == REG)
525     REG_USERVAR_P (reg) = 1;
526   else
527     abort ();
528 }
529
530 /* Identify REG as a probable pointer register and show its alignment
531    as ALIGN, if nonzero.  */
532
533 void
534 mark_reg_pointer (reg, align)
535      rtx reg;
536      int align;
537 {
538   if (! REGNO_POINTER_FLAG (REGNO (reg)))
539     {
540       REGNO_POINTER_FLAG (REGNO (reg)) = 1;
541
542       if (align)
543         REGNO_POINTER_ALIGN (REGNO (reg)) = align;
544     }
545   else if (align && align < REGNO_POINTER_ALIGN (REGNO (reg)))
546     /* We can no-longer be sure just how aligned this pointer is */
547     REGNO_POINTER_ALIGN (REGNO (reg)) = align;
548 }
549
550 /* Return 1 plus largest pseudo reg number used in the current function.  */
551
552 int
553 max_reg_num ()
554 {
555   return reg_rtx_no;
556 }
557
558 /* Return 1 + the largest label number used so far in the current function.  */
559
560 int
561 max_label_num ()
562 {
563   if (last_label_num && label_num == base_label_num)
564     return last_label_num;
565   return label_num;
566 }
567
568 /* Return first label number used in this function (if any were used).  */
569
570 int
571 get_first_label_num ()
572 {
573   return first_label_num;
574 }
575 \f
576 /* Return a value representing some low-order bits of X, where the number
577    of low-order bits is given by MODE.  Note that no conversion is done
578    between floating-point and fixed-point values, rather, the bit 
579    representation is returned.
580
581    This function handles the cases in common between gen_lowpart, below,
582    and two variants in cse.c and combine.c.  These are the cases that can
583    be safely handled at all points in the compilation.
584
585    If this is not a case we can handle, return 0.  */
586
587 rtx
588 gen_lowpart_common (mode, x)
589      enum machine_mode mode;
590      register rtx x;
591 {
592   int word = 0;
593
594   if (GET_MODE (x) == mode)
595     return x;
596
597   /* MODE must occupy no more words than the mode of X.  */
598   if (GET_MODE (x) != VOIDmode
599       && ((GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD
600           > ((GET_MODE_SIZE (GET_MODE (x)) + (UNITS_PER_WORD - 1))
601              / UNITS_PER_WORD)))
602     return 0;
603
604   if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
605     word = ((GET_MODE_SIZE (GET_MODE (x))
606              - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
607             / UNITS_PER_WORD);
608
609   if ((GET_CODE (x) == ZERO_EXTEND || GET_CODE (x) == SIGN_EXTEND)
610       && (GET_MODE_CLASS (mode) == MODE_INT
611           || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT))
612     {
613       /* If we are getting the low-order part of something that has been
614          sign- or zero-extended, we can either just use the object being
615          extended or make a narrower extension.  If we want an even smaller
616          piece than the size of the object being extended, call ourselves
617          recursively.
618
619          This case is used mostly by combine and cse.  */
620
621       if (GET_MODE (XEXP (x, 0)) == mode)
622         return XEXP (x, 0);
623       else if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (XEXP (x, 0))))
624         return gen_lowpart_common (mode, XEXP (x, 0));
625       else if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
626         return gen_rtx_fmt_e (GET_CODE (x), mode, XEXP (x, 0));
627     }
628   else if (GET_CODE (x) == SUBREG
629            && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
630                || GET_MODE_SIZE (mode) == GET_MODE_UNIT_SIZE (GET_MODE (x))))
631     return (GET_MODE (SUBREG_REG (x)) == mode && SUBREG_WORD (x) == 0
632             ? SUBREG_REG (x)
633             : gen_rtx_SUBREG (mode, SUBREG_REG (x), SUBREG_WORD (x) + word));
634   else if (GET_CODE (x) == REG)
635     {
636       /* Let the backend decide how many registers to skip.  This is needed
637          in particular for Sparc64 where fp regs are smaller than a word.  */
638       /* ??? Note that subregs are now ambiguous, in that those against
639          pseudos are sized by the Word Size, while those against hard
640          regs are sized by the underlying register size.  Better would be
641          to always interpret the subreg offset parameter as bytes or bits.  */
642
643       if (WORDS_BIG_ENDIAN && REGNO (x) < FIRST_PSEUDO_REGISTER)
644         word = (HARD_REGNO_NREGS (REGNO (x), GET_MODE (x))
645                 - HARD_REGNO_NREGS (REGNO (x), mode));
646
647       /* If the register is not valid for MODE, return 0.  If we don't
648          do this, there is no way to fix up the resulting REG later.  
649          But we do do this if the current REG is not valid for its
650          mode.  This latter is a kludge, but is required due to the
651          way that parameters are passed on some machines, most
652          notably Sparc.  */
653       if (REGNO (x) < FIRST_PSEUDO_REGISTER
654           && ! HARD_REGNO_MODE_OK (REGNO (x) + word, mode)
655           && HARD_REGNO_MODE_OK (REGNO (x), GET_MODE (x)))
656         return 0;
657       else if (REGNO (x) < FIRST_PSEUDO_REGISTER
658                /* integrate.c can't handle parts of a return value register. */
659                && (! REG_FUNCTION_VALUE_P (x)
660                    || ! rtx_equal_function_value_matters)
661 #ifdef CLASS_CANNOT_CHANGE_SIZE
662                && ! (GET_MODE_SIZE (mode) != GET_MODE_SIZE (GET_MODE (x))
663                      && GET_MODE_CLASS (GET_MODE (x)) != MODE_COMPLEX_INT
664                      && GET_MODE_CLASS (GET_MODE (x)) != MODE_COMPLEX_FLOAT
665                      && (TEST_HARD_REG_BIT
666                          (reg_class_contents[(int) CLASS_CANNOT_CHANGE_SIZE],
667                           REGNO (x))))
668 #endif
669                /* We want to keep the stack, frame, and arg pointers
670                   special.  */
671                && x != frame_pointer_rtx
672 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
673                && x != arg_pointer_rtx
674 #endif
675                && x != stack_pointer_rtx)
676         return gen_rtx_REG (mode, REGNO (x) + word);
677       else
678         return gen_rtx_SUBREG (mode, x, word);
679     }
680   /* If X is a CONST_INT or a CONST_DOUBLE, extract the appropriate bits
681      from the low-order part of the constant.  */
682   else if ((GET_MODE_CLASS (mode) == MODE_INT
683             || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
684            && GET_MODE (x) == VOIDmode
685            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
686     {
687       /* If MODE is twice the host word size, X is already the desired
688          representation.  Otherwise, if MODE is wider than a word, we can't
689          do this.  If MODE is exactly a word, return just one CONST_INT.
690          If MODE is smaller than a word, clear the bits that don't belong
691          in our mode, unless they and our sign bit are all one.  So we get
692          either a reasonable negative value or a reasonable unsigned value
693          for this mode.  */
694
695       if (GET_MODE_BITSIZE (mode) >= 2 * HOST_BITS_PER_WIDE_INT)
696         return x;
697       else if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_WIDE_INT)
698         return 0;
699       else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_WIDE_INT)
700         return (GET_CODE (x) == CONST_INT ? x
701                 : GEN_INT (CONST_DOUBLE_LOW (x)));
702       else
703         {
704           /* MODE must be narrower than HOST_BITS_PER_WIDE_INT.  */
705           int width = GET_MODE_BITSIZE (mode);
706           HOST_WIDE_INT val = (GET_CODE (x) == CONST_INT ? INTVAL (x)
707                                : CONST_DOUBLE_LOW (x));
708
709           /* Sign extend to HOST_WIDE_INT.  */
710           val = val << (HOST_BITS_PER_WIDE_INT - width) >> (HOST_BITS_PER_WIDE_INT - width);
711
712           return (GET_CODE (x) == CONST_INT && INTVAL (x) == val ? x
713                   : GEN_INT (val));
714         }
715     }
716
717   /* If X is an integral constant but we want it in floating-point, it
718      must be the case that we have a union of an integer and a floating-point
719      value.  If the machine-parameters allow it, simulate that union here
720      and return the result.  The two-word and single-word cases are 
721      different.  */
722
723   else if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
724              && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
725             || flag_pretend_float)
726            && GET_MODE_CLASS (mode) == MODE_FLOAT
727            && GET_MODE_SIZE (mode) == UNITS_PER_WORD
728            && GET_CODE (x) == CONST_INT
729            && sizeof (float) * HOST_BITS_PER_CHAR == HOST_BITS_PER_WIDE_INT)
730 #ifdef REAL_ARITHMETIC
731     {
732       REAL_VALUE_TYPE r;
733       HOST_WIDE_INT i;
734
735       i = INTVAL (x);
736       r = REAL_VALUE_FROM_TARGET_SINGLE (i);
737       return CONST_DOUBLE_FROM_REAL_VALUE (r, mode);
738     }
739 #else
740     {
741       union {HOST_WIDE_INT i; float d; } u;
742
743       u.i = INTVAL (x);
744       return CONST_DOUBLE_FROM_REAL_VALUE (u.d, mode);
745     }
746 #endif
747   else if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
748              && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
749             || flag_pretend_float)
750            && GET_MODE_CLASS (mode) == MODE_FLOAT
751            && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
752            && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
753            && GET_MODE (x) == VOIDmode
754            && (sizeof (double) * HOST_BITS_PER_CHAR
755                == 2 * HOST_BITS_PER_WIDE_INT))
756 #ifdef REAL_ARITHMETIC
757     {
758       REAL_VALUE_TYPE r;
759       HOST_WIDE_INT i[2];
760       HOST_WIDE_INT low, high;
761
762       if (GET_CODE (x) == CONST_INT)
763         low = INTVAL (x), high = low >> (HOST_BITS_PER_WIDE_INT -1);
764       else
765         low = CONST_DOUBLE_LOW (x), high = CONST_DOUBLE_HIGH (x);
766
767       /* REAL_VALUE_TARGET_DOUBLE takes the addressing order of the
768          target machine.  */
769       if (WORDS_BIG_ENDIAN)
770         i[0] = high, i[1] = low;
771       else
772         i[0] = low, i[1] = high;
773
774       r = REAL_VALUE_FROM_TARGET_DOUBLE (i);
775       return CONST_DOUBLE_FROM_REAL_VALUE (r, mode);
776     }
777 #else
778     {
779       union {HOST_WIDE_INT i[2]; double d; } u;
780       HOST_WIDE_INT low, high;
781
782       if (GET_CODE (x) == CONST_INT)
783         low = INTVAL (x), high = low >> (HOST_BITS_PER_WIDE_INT -1);
784       else
785         low = CONST_DOUBLE_LOW (x), high = CONST_DOUBLE_HIGH (x);
786
787 #ifdef HOST_WORDS_BIG_ENDIAN
788       u.i[0] = high, u.i[1] = low;
789 #else
790       u.i[0] = low, u.i[1] = high;
791 #endif
792
793       return CONST_DOUBLE_FROM_REAL_VALUE (u.d, mode);
794     }
795 #endif
796
797   /* We need an extra case for machines where HOST_BITS_PER_WIDE_INT is the
798      same as sizeof (double) or when sizeof (float) is larger than the
799      size of a word on the target machine.  */
800 #ifdef REAL_ARITHMETIC
801   else if (mode == SFmode && GET_CODE (x) == CONST_INT)
802     {
803       REAL_VALUE_TYPE r;
804       HOST_WIDE_INT i;
805
806       i = INTVAL (x);
807       r = REAL_VALUE_FROM_TARGET_SINGLE (i);
808       return CONST_DOUBLE_FROM_REAL_VALUE (r, mode);
809     }
810   else if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
811              && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
812             || flag_pretend_float)
813            && GET_MODE_CLASS (mode) == MODE_FLOAT
814            && GET_MODE_SIZE (mode) == UNITS_PER_WORD
815            && GET_CODE (x) == CONST_INT
816            && (sizeof (double) * HOST_BITS_PER_CHAR
817                == HOST_BITS_PER_WIDE_INT))
818     {
819       REAL_VALUE_TYPE r;
820       HOST_WIDE_INT i;
821
822       i = INTVAL (x);
823       r = REAL_VALUE_FROM_TARGET_DOUBLE (&i);
824       return CONST_DOUBLE_FROM_REAL_VALUE (r, mode);
825     }
826 #endif
827
828   /* Similarly, if this is converting a floating-point value into a
829      single-word integer.  Only do this is the host and target parameters are
830      compatible.  */
831
832   else if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
833              && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
834             || flag_pretend_float)
835            && (GET_MODE_CLASS (mode) == MODE_INT
836                || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
837            && GET_CODE (x) == CONST_DOUBLE
838            && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT
839            && GET_MODE_BITSIZE (mode) == BITS_PER_WORD)
840     return operand_subword (x, word, 0, GET_MODE (x));
841
842   /* Similarly, if this is converting a floating-point value into a
843      two-word integer, we can do this one word at a time and make an
844      integer.  Only do this is the host and target parameters are
845      compatible.  */
846
847   else if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
848              && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
849             || flag_pretend_float)
850            && (GET_MODE_CLASS (mode) == MODE_INT
851                || GET_MODE_CLASS (mode) == MODE_PARTIAL_INT)
852            && GET_CODE (x) == CONST_DOUBLE
853            && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT
854            && GET_MODE_BITSIZE (mode) == 2 * BITS_PER_WORD)
855     {
856       rtx lowpart
857         = operand_subword (x, word + WORDS_BIG_ENDIAN, 0, GET_MODE (x));
858       rtx highpart
859         = operand_subword (x, word + ! WORDS_BIG_ENDIAN, 0, GET_MODE (x));
860
861       if (lowpart && GET_CODE (lowpart) == CONST_INT
862           && highpart && GET_CODE (highpart) == CONST_INT)
863         return immed_double_const (INTVAL (lowpart), INTVAL (highpart), mode);
864     }
865
866   /* Otherwise, we can't do this.  */
867   return 0;
868 }
869 \f
870 /* Return the real part (which has mode MODE) of a complex value X.
871    This always comes at the low address in memory.  */
872
873 rtx
874 gen_realpart (mode, x)
875      enum machine_mode mode;
876      register rtx x;
877 {
878   if (GET_CODE (x) == CONCAT && GET_MODE (XEXP (x, 0)) == mode)
879     return XEXP (x, 0);
880   else if (WORDS_BIG_ENDIAN
881            && GET_MODE_BITSIZE (mode) < BITS_PER_WORD
882            && REG_P (x)
883            && REGNO (x) < FIRST_PSEUDO_REGISTER)
884     fatal ("Unable to access real part of complex value in a hard register on this target");
885   else if (WORDS_BIG_ENDIAN)
886     return gen_highpart (mode, x);
887   else
888     return gen_lowpart (mode, x);
889 }
890
891 /* Return the imaginary part (which has mode MODE) of a complex value X.
892    This always comes at the high address in memory.  */
893
894 rtx
895 gen_imagpart (mode, x)
896      enum machine_mode mode;
897      register rtx x;
898 {
899   if (GET_CODE (x) == CONCAT && GET_MODE (XEXP (x, 0)) == mode)
900     return XEXP (x, 1);
901   else if (WORDS_BIG_ENDIAN)
902     return gen_lowpart (mode, x);
903   else if (!WORDS_BIG_ENDIAN
904            && GET_MODE_BITSIZE (mode) < BITS_PER_WORD
905            && REG_P (x)
906            && REGNO (x) < FIRST_PSEUDO_REGISTER)
907     fatal ("Unable to access imaginary part of complex value in a hard register on this target");
908   else
909     return gen_highpart (mode, x);
910 }
911
912 /* Return 1 iff X, assumed to be a SUBREG,
913    refers to the real part of the complex value in its containing reg.
914    Complex values are always stored with the real part in the first word,
915    regardless of WORDS_BIG_ENDIAN.  */
916
917 int
918 subreg_realpart_p (x)
919      rtx x;
920 {
921   if (GET_CODE (x) != SUBREG)
922     abort ();
923
924   return SUBREG_WORD (x) * UNITS_PER_WORD < GET_MODE_UNIT_SIZE (GET_MODE (SUBREG_REG (x)));
925 }
926 \f
927 /* Assuming that X is an rtx (e.g., MEM, REG or SUBREG) for a value,
928    return an rtx (MEM, SUBREG, or CONST_INT) that refers to the
929    least-significant part of X.
930    MODE specifies how big a part of X to return;
931    it usually should not be larger than a word.
932    If X is a MEM whose address is a QUEUED, the value may be so also.  */
933
934 rtx
935 gen_lowpart (mode, x)
936      enum machine_mode mode;
937      register rtx x;
938 {
939   rtx result = gen_lowpart_common (mode, x);
940
941   if (result)
942     return result;
943   else if (GET_CODE (x) == REG)
944     {
945       /* Must be a hard reg that's not valid in MODE.  */
946       result = gen_lowpart_common (mode, copy_to_reg (x));
947       if (result == 0)
948         abort ();
949       return result;
950     }
951   else if (GET_CODE (x) == MEM)
952     {
953       /* The only additional case we can do is MEM.  */
954       register int offset = 0;
955       if (WORDS_BIG_ENDIAN)
956         offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
957                   - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
958
959       if (BYTES_BIG_ENDIAN)
960         /* Adjust the address so that the address-after-the-data
961            is unchanged.  */
962         offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
963                    - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
964
965       return change_address (x, mode, plus_constant (XEXP (x, 0), offset));
966     }
967   else if (GET_CODE (x) == ADDRESSOF)
968     return gen_lowpart (mode, force_reg (GET_MODE (x), x));
969   else
970     abort ();
971 }
972
973 /* Like `gen_lowpart', but refer to the most significant part. 
974    This is used to access the imaginary part of a complex number.  */
975
976 rtx
977 gen_highpart (mode, x)
978      enum machine_mode mode;
979      register rtx x;
980 {
981   /* This case loses if X is a subreg.  To catch bugs early,
982      complain if an invalid MODE is used even in other cases.  */
983   if (GET_MODE_SIZE (mode) > UNITS_PER_WORD
984       && GET_MODE_SIZE (mode) != GET_MODE_UNIT_SIZE (GET_MODE (x)))
985     abort ();
986   if (GET_CODE (x) == CONST_DOUBLE
987 #if !(TARGET_FLOAT_FORMAT != HOST_FLOAT_FORMAT || defined (REAL_IS_NOT_DOUBLE))
988       && GET_MODE_CLASS (GET_MODE (x)) != MODE_FLOAT
989 #endif
990       )
991     return GEN_INT (CONST_DOUBLE_HIGH (x) & GET_MODE_MASK (mode));
992   else if (GET_CODE (x) == CONST_INT)
993     {
994       if (HOST_BITS_PER_WIDE_INT <= BITS_PER_WORD)
995         return const0_rtx;
996       return GEN_INT (INTVAL (x) >> (HOST_BITS_PER_WIDE_INT - BITS_PER_WORD));
997     }
998   else if (GET_CODE (x) == MEM)
999     {
1000       register int offset = 0;
1001       if (! WORDS_BIG_ENDIAN)
1002         offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
1003                   - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
1004
1005       if (! BYTES_BIG_ENDIAN
1006           && GET_MODE_SIZE (mode) < UNITS_PER_WORD)
1007         offset -= (GET_MODE_SIZE (mode)
1008                    - MIN (UNITS_PER_WORD,
1009                           GET_MODE_SIZE (GET_MODE (x))));
1010
1011       return change_address (x, mode, plus_constant (XEXP (x, 0), offset));
1012     }
1013   else if (GET_CODE (x) == SUBREG)
1014     {
1015       /* The only time this should occur is when we are looking at a
1016          multi-word item with a SUBREG whose mode is the same as that of the
1017          item.  It isn't clear what we would do if it wasn't.  */
1018       if (SUBREG_WORD (x) != 0)
1019         abort ();
1020       return gen_highpart (mode, SUBREG_REG (x));
1021     }
1022   else if (GET_CODE (x) == REG)
1023     {
1024       int word;
1025
1026       /* Let the backend decide how many registers to skip.  This is needed
1027          in particular for sparc64 where fp regs are smaller than a word.  */
1028       /* ??? Note that subregs are now ambiguous, in that those against
1029          pseudos are sized by the word size, while those against hard
1030          regs are sized by the underlying register size.  Better would be
1031          to always interpret the subreg offset parameter as bytes or bits.  */
1032
1033       if (WORDS_BIG_ENDIAN)
1034         word = 0;
1035       else if (REGNO (x) < FIRST_PSEUDO_REGISTER)
1036         word = (HARD_REGNO_NREGS (REGNO (x), GET_MODE (x))
1037                 - HARD_REGNO_NREGS (REGNO (x), mode));
1038       else
1039         word = ((GET_MODE_SIZE (GET_MODE (x))
1040                  - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
1041                 / UNITS_PER_WORD);
1042
1043       if (REGNO (x) < FIRST_PSEUDO_REGISTER
1044           /* integrate.c can't handle parts of a return value register.  */
1045           && (! REG_FUNCTION_VALUE_P (x)
1046               || ! rtx_equal_function_value_matters)
1047           /* We want to keep the stack, frame, and arg pointers special.  */
1048           && x != frame_pointer_rtx
1049 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1050           && x != arg_pointer_rtx
1051 #endif
1052           && x != stack_pointer_rtx)
1053         return gen_rtx_REG (mode, REGNO (x) + word);
1054       else
1055         return gen_rtx_SUBREG (mode, x, word);
1056     }
1057   else
1058     abort ();
1059 }
1060
1061 /* Return 1 iff X, assumed to be a SUBREG,
1062    refers to the least significant part of its containing reg.
1063    If X is not a SUBREG, always return 1 (it is its own low part!).  */
1064
1065 int
1066 subreg_lowpart_p (x)
1067      rtx x;
1068 {
1069   if (GET_CODE (x) != SUBREG)
1070     return 1;
1071   else if (GET_MODE (SUBREG_REG (x)) == VOIDmode)
1072     return 0;
1073
1074   if (WORDS_BIG_ENDIAN
1075       && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) > UNITS_PER_WORD)
1076     return (SUBREG_WORD (x)
1077             == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
1078                  - MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD))
1079                 / UNITS_PER_WORD));
1080
1081   return SUBREG_WORD (x) == 0;
1082 }
1083 \f
1084 /* Return subword I of operand OP.
1085    The word number, I, is interpreted as the word number starting at the
1086    low-order address.  Word 0 is the low-order word if not WORDS_BIG_ENDIAN,
1087    otherwise it is the high-order word.
1088
1089    If we cannot extract the required word, we return zero.  Otherwise, an
1090    rtx corresponding to the requested word will be returned.
1091
1092    VALIDATE_ADDRESS is nonzero if the address should be validated.  Before
1093    reload has completed, a valid address will always be returned.  After
1094    reload, if a valid address cannot be returned, we return zero.
1095
1096    If VALIDATE_ADDRESS is zero, we simply form the required address; validating
1097    it is the responsibility of the caller.
1098
1099    MODE is the mode of OP in case it is a CONST_INT.  */
1100
1101 rtx
1102 operand_subword (op, i, validate_address, mode)
1103      rtx op;
1104      int i;
1105      int validate_address;
1106      enum machine_mode mode;
1107 {
1108   HOST_WIDE_INT val;
1109   int size_ratio = HOST_BITS_PER_WIDE_INT / BITS_PER_WORD;
1110
1111   if (mode == VOIDmode)
1112     mode = GET_MODE (op);
1113
1114   if (mode == VOIDmode)
1115     abort ();
1116
1117   /* If OP is narrower than a word, fail. */
1118   if (mode != BLKmode
1119       && (GET_MODE_SIZE (mode) < UNITS_PER_WORD))
1120     return 0;
1121
1122   /* If we want a word outside OP, return zero. */
1123   if (mode != BLKmode
1124       && (i + 1) * UNITS_PER_WORD > GET_MODE_SIZE (mode))
1125     return const0_rtx;
1126
1127   /* If OP is already an integer word, return it.  */
1128   if (GET_MODE_CLASS (mode) == MODE_INT
1129       && GET_MODE_SIZE (mode) == UNITS_PER_WORD)
1130     return op;
1131
1132   /* If OP is a REG or SUBREG, we can handle it very simply.  */
1133   if (GET_CODE (op) == REG)
1134     {
1135       /* ??? There is a potential problem with this code.  It does not
1136          properly handle extractions of a subword from a hard register
1137          that is larger than word_mode.  Presumably the check for
1138          HARD_REGNO_MODE_OK catches these most of these cases.  */
1139
1140       /* If OP is a hard register, but OP + I is not a hard register,
1141          then extracting a subword is impossible.
1142
1143          For example, consider if OP is the last hard register and it is
1144          larger than word_mode.  If we wanted word N (for N > 0) because a
1145          part of that hard register was known to contain a useful value,
1146          then OP + I would refer to a pseudo, not the hard register we
1147          actually wanted.  */
1148       if (REGNO (op) < FIRST_PSEUDO_REGISTER
1149           && REGNO (op) + i >= FIRST_PSEUDO_REGISTER)
1150         return 0;
1151
1152       /* If the register is not valid for MODE, return 0.  Note we
1153          have to check both OP and OP + I since they may refer to
1154          different parts of the register file.
1155
1156          Consider if OP refers to the last 96bit FP register and we want
1157          subword 3 because that subword is known to contain a value we
1158          needed.  */
1159       if (REGNO (op) < FIRST_PSEUDO_REGISTER
1160           && (! HARD_REGNO_MODE_OK (REGNO (op), word_mode)
1161               || ! HARD_REGNO_MODE_OK (REGNO (op) + i, word_mode)))
1162         return 0;
1163       else if (REGNO (op) >= FIRST_PSEUDO_REGISTER
1164                || (REG_FUNCTION_VALUE_P (op)
1165                    && rtx_equal_function_value_matters)
1166                /* We want to keep the stack, frame, and arg pointers
1167                   special.  */
1168                || op == frame_pointer_rtx
1169 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
1170                || op == arg_pointer_rtx
1171 #endif
1172                || op == stack_pointer_rtx)
1173         return gen_rtx_SUBREG (word_mode, op, i);
1174       else
1175         return gen_rtx_REG (word_mode, REGNO (op) + i);
1176     }
1177   else if (GET_CODE (op) == SUBREG)
1178     return gen_rtx_SUBREG (word_mode, SUBREG_REG (op), i + SUBREG_WORD (op));
1179   else if (GET_CODE (op) == CONCAT)
1180     {
1181       int partwords = GET_MODE_UNIT_SIZE (GET_MODE (op)) / UNITS_PER_WORD;
1182       if (i < partwords)
1183         return operand_subword (XEXP (op, 0), i, validate_address, mode);
1184       return operand_subword (XEXP (op, 1), i - partwords,
1185                               validate_address, mode);
1186     }
1187
1188   /* Form a new MEM at the requested address.  */
1189   if (GET_CODE (op) == MEM)
1190     {
1191       rtx addr = plus_constant (XEXP (op, 0), i * UNITS_PER_WORD);
1192       rtx new;
1193
1194       if (validate_address)
1195         {
1196           if (reload_completed)
1197             {
1198               if (! strict_memory_address_p (word_mode, addr))
1199                 return 0;
1200             }
1201           else
1202             addr = memory_address (word_mode, addr);
1203         }
1204
1205       new = gen_rtx_MEM (word_mode, addr);
1206
1207       MEM_COPY_ATTRIBUTES (new, op);
1208       RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (op);
1209       MEM_ALIAS_SET (new) = MEM_ALIAS_SET (op);
1210
1211       return new;
1212     }
1213
1214   /* The only remaining cases are when OP is a constant.  If the host and
1215      target floating formats are the same, handling two-word floating
1216      constants are easy.  Note that REAL_VALUE_TO_TARGET_{SINGLE,DOUBLE}
1217      are defined as returning one or two 32 bit values, respectively,
1218      and not values of BITS_PER_WORD bits.  */
1219 #ifdef REAL_ARITHMETIC
1220 /*  The output is some bits, the width of the target machine's word.
1221     A wider-word host can surely hold them in a CONST_INT. A narrower-word
1222     host can't.  */
1223   if (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
1224       && GET_MODE_CLASS (mode) == MODE_FLOAT
1225       && GET_MODE_BITSIZE (mode) == 64
1226       && GET_CODE (op) == CONST_DOUBLE)
1227     {
1228       long k[2];
1229       REAL_VALUE_TYPE rv;
1230
1231       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1232       REAL_VALUE_TO_TARGET_DOUBLE (rv, k);
1233
1234       /* We handle 32-bit and >= 64-bit words here.  Note that the order in
1235          which the words are written depends on the word endianness.
1236          ??? This is a potential portability problem and should
1237          be fixed at some point.
1238
1239          We must excercise caution with the sign bit.  By definition there
1240          are 32 significant bits in K; there may be more in a HOST_WIDE_INT.
1241          Consider a host with a 32-bit long and a 64-bit HOST_WIDE_INT.
1242          So we explicitly mask and sign-extend as necessary.  */
1243       if (BITS_PER_WORD == 32)
1244         {
1245           val = k[i];
1246           val = ((val & 0xffffffff) ^ 0x80000000) - 0x80000000;
1247           return GEN_INT (val);
1248         }
1249 #if HOST_BITS_PER_WIDE_INT >= 64
1250       else if (BITS_PER_WORD >= 64 && i == 0)
1251         {
1252           val = k[! WORDS_BIG_ENDIAN];
1253           val = (((val & 0xffffffff) ^ 0x80000000) - 0x80000000) << 32;
1254           val |= (HOST_WIDE_INT) k[WORDS_BIG_ENDIAN] & 0xffffffff;
1255           return GEN_INT (val);
1256         }
1257 #endif
1258       else if (BITS_PER_WORD == 16)
1259         {
1260           val = k[i >> 1];
1261           if ((i & 1) == !WORDS_BIG_ENDIAN)
1262             val >>= 16;
1263           val &= 0xffff;
1264           return GEN_INT (val);
1265         }
1266       else
1267         abort ();
1268     }
1269   else if (HOST_BITS_PER_WIDE_INT >= BITS_PER_WORD
1270            && GET_MODE_CLASS (mode) == MODE_FLOAT
1271            && GET_MODE_BITSIZE (mode) > 64
1272            && GET_CODE (op) == CONST_DOUBLE)
1273   {
1274     long k[4];
1275     REAL_VALUE_TYPE rv;
1276
1277     REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1278     REAL_VALUE_TO_TARGET_LONG_DOUBLE (rv, k);
1279
1280     if (BITS_PER_WORD == 32)
1281       {
1282         val = k[i];
1283         val = ((val & 0xffffffff) ^ 0x80000000) - 0x80000000;
1284         return GEN_INT (val);
1285       }
1286     else
1287       abort ();
1288   }
1289 #else /* no REAL_ARITHMETIC */
1290   if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
1291         && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1292        || flag_pretend_float)
1293       && GET_MODE_CLASS (mode) == MODE_FLOAT
1294       && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
1295       && GET_CODE (op) == CONST_DOUBLE)
1296     {
1297       /* The constant is stored in the host's word-ordering,
1298          but we want to access it in the target's word-ordering.  Some
1299          compilers don't like a conditional inside macro args, so we have two
1300          copies of the return.  */
1301 #ifdef HOST_WORDS_BIG_ENDIAN
1302       return GEN_INT (i == WORDS_BIG_ENDIAN
1303                       ? CONST_DOUBLE_HIGH (op) : CONST_DOUBLE_LOW (op));
1304 #else
1305       return GEN_INT (i != WORDS_BIG_ENDIAN
1306                       ? CONST_DOUBLE_HIGH (op) : CONST_DOUBLE_LOW (op));
1307 #endif
1308     }
1309 #endif /* no REAL_ARITHMETIC */
1310
1311   /* Single word float is a little harder, since single- and double-word
1312      values often do not have the same high-order bits.  We have already
1313      verified that we want the only defined word of the single-word value.  */
1314 #ifdef REAL_ARITHMETIC
1315   if (GET_MODE_CLASS (mode) == MODE_FLOAT
1316       && GET_MODE_BITSIZE (mode) == 32
1317       && GET_CODE (op) == CONST_DOUBLE)
1318     {
1319       long l;
1320       REAL_VALUE_TYPE rv;
1321
1322       REAL_VALUE_FROM_CONST_DOUBLE (rv, op);
1323       REAL_VALUE_TO_TARGET_SINGLE (rv, l);
1324
1325       /* Sign extend from known 32-bit value to HOST_WIDE_INT.  */
1326       val = l;
1327       val = ((val & 0xffffffff) ^ 0x80000000) - 0x80000000;
1328
1329       if (BITS_PER_WORD == 16)
1330         {
1331           if ((i & 1) == !WORDS_BIG_ENDIAN)
1332             val >>= 16;
1333           val &= 0xffff;
1334         }
1335
1336       return GEN_INT (val);
1337     }
1338 #else
1339   if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
1340         && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1341        || flag_pretend_float)
1342       && sizeof (float) * 8 == HOST_BITS_PER_WIDE_INT
1343       && GET_MODE_CLASS (mode) == MODE_FLOAT
1344       && GET_MODE_SIZE (mode) == UNITS_PER_WORD
1345       && GET_CODE (op) == CONST_DOUBLE)
1346     {
1347       double d;
1348       union {float f; HOST_WIDE_INT i; } u;
1349
1350       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
1351
1352       u.f = d;
1353       return GEN_INT (u.i);
1354     }
1355   if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
1356         && HOST_BITS_PER_WIDE_INT == BITS_PER_WORD)
1357        || flag_pretend_float)
1358       && sizeof (double) * 8 == HOST_BITS_PER_WIDE_INT
1359       && GET_MODE_CLASS (mode) == MODE_FLOAT
1360       && GET_MODE_SIZE (mode) == UNITS_PER_WORD
1361       && GET_CODE (op) == CONST_DOUBLE)
1362     {
1363       double d;
1364       union {double d; HOST_WIDE_INT i; } u;
1365
1366       REAL_VALUE_FROM_CONST_DOUBLE (d, op);
1367
1368       u.d = d;
1369       return GEN_INT (u.i);
1370     }
1371 #endif /* no REAL_ARITHMETIC */
1372       
1373   /* The only remaining cases that we can handle are integers.
1374      Convert to proper endianness now since these cases need it.
1375      At this point, i == 0 means the low-order word.  
1376
1377      We do not want to handle the case when BITS_PER_WORD <= HOST_BITS_PER_INT
1378      in general.  However, if OP is (const_int 0), we can just return
1379      it for any word.  */
1380
1381   if (op == const0_rtx)
1382     return op;
1383
1384   if (GET_MODE_CLASS (mode) != MODE_INT
1385       || (GET_CODE (op) != CONST_INT && GET_CODE (op) != CONST_DOUBLE)
1386       || BITS_PER_WORD > HOST_BITS_PER_WIDE_INT)
1387     return 0;
1388
1389   if (WORDS_BIG_ENDIAN)
1390     i = GET_MODE_SIZE (mode) / UNITS_PER_WORD - 1 - i;
1391
1392   /* Find out which word on the host machine this value is in and get
1393      it from the constant.  */
1394   val = (i / size_ratio == 0
1395          ? (GET_CODE (op) == CONST_INT ? INTVAL (op) : CONST_DOUBLE_LOW (op))
1396          : (GET_CODE (op) == CONST_INT
1397             ? (INTVAL (op) < 0 ? ~0 : 0) : CONST_DOUBLE_HIGH (op)));
1398
1399   /* Get the value we want into the low bits of val.  */
1400   if (BITS_PER_WORD < HOST_BITS_PER_WIDE_INT)
1401     val = ((val >> ((i % size_ratio) * BITS_PER_WORD)));
1402
1403   val = trunc_int_for_mode (val, word_mode);
1404
1405   return GEN_INT (val);
1406 }
1407
1408 /* Similar to `operand_subword', but never return 0.  If we can't extract
1409    the required subword, put OP into a register and try again.  If that fails,
1410    abort.  We always validate the address in this case.  It is not valid
1411    to call this function after reload; it is mostly meant for RTL
1412    generation. 
1413
1414    MODE is the mode of OP, in case it is CONST_INT.  */
1415
1416 rtx
1417 operand_subword_force (op, i, mode)
1418      rtx op;
1419      int i;
1420      enum machine_mode mode;
1421 {
1422   rtx result = operand_subword (op, i, 1, mode);
1423
1424   if (result)
1425     return result;
1426
1427   if (mode != BLKmode && mode != VOIDmode)
1428     {
1429       /* If this is a register which can not be accessed by words, copy it
1430          to a pseudo register.  */
1431       if (GET_CODE (op) == REG)
1432         op = copy_to_reg (op);
1433       else
1434         op = force_reg (mode, op);
1435     }
1436
1437   result = operand_subword (op, i, 1, mode);
1438   if (result == 0)
1439     abort ();
1440
1441   return result;
1442 }
1443 \f
1444 /* Given a compare instruction, swap the operands.
1445    A test instruction is changed into a compare of 0 against the operand.  */
1446
1447 void
1448 reverse_comparison (insn)
1449      rtx insn;
1450 {
1451   rtx body = PATTERN (insn);
1452   rtx comp;
1453
1454   if (GET_CODE (body) == SET)
1455     comp = SET_SRC (body);
1456   else
1457     comp = SET_SRC (XVECEXP (body, 0, 0));
1458
1459   if (GET_CODE (comp) == COMPARE)
1460     {
1461       rtx op0 = XEXP (comp, 0);
1462       rtx op1 = XEXP (comp, 1);
1463       XEXP (comp, 0) = op1;
1464       XEXP (comp, 1) = op0;
1465     }
1466   else
1467     {
1468       rtx new = gen_rtx_COMPARE (VOIDmode,
1469                                  CONST0_RTX (GET_MODE (comp)), comp);
1470       if (GET_CODE (body) == SET)
1471         SET_SRC (body) = new;
1472       else
1473         SET_SRC (XVECEXP (body, 0, 0)) = new;
1474     }
1475 }
1476 \f
1477 /* Return a memory reference like MEMREF, but with its mode changed
1478    to MODE and its address changed to ADDR.
1479    (VOIDmode means don't change the mode.
1480    NULL for ADDR means don't change the address.)  */
1481
1482 rtx
1483 change_address (memref, mode, addr)
1484      rtx memref;
1485      enum machine_mode mode;
1486      rtx addr;
1487 {
1488   rtx new;
1489
1490   if (GET_CODE (memref) != MEM)
1491     abort ();
1492   if (mode == VOIDmode)
1493     mode = GET_MODE (memref);
1494   if (addr == 0)
1495     addr = XEXP (memref, 0);
1496
1497   /* If reload is in progress or has completed, ADDR must be valid.
1498      Otherwise, we can call memory_address to make it valid.  */
1499   if (reload_completed || reload_in_progress)
1500     {
1501       if (! memory_address_p (mode, addr))
1502         abort ();
1503     }
1504   else
1505     addr = memory_address (mode, addr);
1506         
1507   if (rtx_equal_p (addr, XEXP (memref, 0)) && mode == GET_MODE (memref))
1508     return memref;
1509
1510   new = gen_rtx_MEM (mode, addr);
1511   RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (memref);
1512   MEM_COPY_ATTRIBUTES (new, memref);
1513   MEM_ALIAS_SET (new) = MEM_ALIAS_SET (memref);
1514   return new;
1515 }
1516 \f
1517 /* Return a newly created CODE_LABEL rtx with a unique label number.  */
1518
1519 rtx
1520 gen_label_rtx ()
1521 {
1522   register rtx label;
1523
1524   label = gen_rtx_CODE_LABEL (VOIDmode, 0, NULL_RTX,
1525                               NULL_RTX, label_num++, NULL_PTR);
1526
1527   LABEL_NUSES (label) = 0;
1528   return label;
1529 }
1530 \f
1531 /* For procedure integration.  */
1532
1533 /* Install new pointers to the first and last insns in the chain.
1534    Also, set cur_insn_uid to one higher than the last in use.
1535    Used for an inline-procedure after copying the insn chain.  */
1536
1537 void
1538 set_new_first_and_last_insn (first, last)
1539      rtx first, last;
1540 {
1541   rtx insn;
1542
1543   first_insn = first;
1544   last_insn = last;
1545   cur_insn_uid = 0;
1546
1547   for (insn = first; insn; insn = NEXT_INSN (insn))
1548     cur_insn_uid = MAX (cur_insn_uid, INSN_UID (insn));
1549
1550   cur_insn_uid++;
1551 }
1552
1553 /* Set the range of label numbers found in the current function.
1554    This is used when belatedly compiling an inline function.  */
1555
1556 void
1557 set_new_first_and_last_label_num (first, last)
1558      int first, last;
1559 {
1560   base_label_num = label_num;
1561   first_label_num = first;
1562   last_label_num = last;
1563 }
1564
1565 /* Set the last label number found in the current function.
1566    This is used when belatedly compiling an inline function.  */
1567
1568 void
1569 set_new_last_label_num (last)
1570      int last;
1571 {
1572   base_label_num = label_num;
1573   last_label_num = last;
1574 }
1575 \f
1576 /* Restore all variables describing the current status from the structure *P.
1577    This is used after a nested function.  */
1578
1579 void
1580 restore_emit_status (p)
1581      struct function *p;
1582 {
1583   last_label_num = 0;
1584   clear_emit_caches ();
1585 }
1586
1587 /* Clear out all parts of the state in F that can safely be discarded
1588    after the function has been compiled, to let garbage collection
1589    reclaim the memory.  */
1590
1591 void
1592 free_emit_status (f)
1593      struct function *f;
1594 {
1595   free (f->emit->x_regno_reg_rtx);
1596   free (f->emit->regno_pointer_flag);
1597   free (f->emit->regno_pointer_align);
1598   free (f->emit);
1599   f->emit = NULL;
1600 }
1601 \f
1602 /* Go through all the RTL insn bodies and copy any invalid shared structure.
1603    It does not work to do this twice, because the mark bits set here
1604    are not cleared afterwards.  */
1605
1606 void
1607 unshare_all_rtl (insn)
1608      register rtx insn;
1609 {
1610   for (; insn; insn = NEXT_INSN (insn))
1611     if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
1612         || GET_CODE (insn) == CALL_INSN)
1613       {
1614         PATTERN (insn) = copy_rtx_if_shared (PATTERN (insn));
1615         REG_NOTES (insn) = copy_rtx_if_shared (REG_NOTES (insn));
1616         LOG_LINKS (insn) = copy_rtx_if_shared (LOG_LINKS (insn));
1617       }
1618
1619   /* Make sure the addresses of stack slots found outside the insn chain
1620      (such as, in DECL_RTL of a variable) are not shared
1621      with the insn chain.
1622
1623      This special care is necessary when the stack slot MEM does not
1624      actually appear in the insn chain.  If it does appear, its address
1625      is unshared from all else at that point.  */
1626
1627   copy_rtx_if_shared (stack_slot_list);
1628 }
1629
1630 /* Mark ORIG as in use, and return a copy of it if it was already in use.
1631    Recursively does the same for subexpressions.  */
1632
1633 rtx
1634 copy_rtx_if_shared (orig)
1635      rtx orig;
1636 {
1637   register rtx x = orig;
1638   register int i;
1639   register enum rtx_code code;
1640   register const char *format_ptr;
1641   int copied = 0;
1642
1643   if (x == 0)
1644     return 0;
1645
1646   code = GET_CODE (x);
1647
1648   /* These types may be freely shared.  */
1649
1650   switch (code)
1651     {
1652     case REG:
1653     case QUEUED:
1654     case CONST_INT:
1655     case CONST_DOUBLE:
1656     case SYMBOL_REF:
1657     case CODE_LABEL:
1658     case PC:
1659     case CC0:
1660     case SCRATCH:
1661       /* SCRATCH must be shared because they represent distinct values.  */
1662       return x;
1663
1664     case CONST:
1665       /* CONST can be shared if it contains a SYMBOL_REF.  If it contains
1666          a LABEL_REF, it isn't sharable.  */
1667       if (GET_CODE (XEXP (x, 0)) == PLUS
1668           && GET_CODE (XEXP (XEXP (x, 0), 0)) == SYMBOL_REF
1669           && GET_CODE (XEXP (XEXP (x, 0), 1)) == CONST_INT)
1670         return x;
1671       break;
1672
1673     case INSN:
1674     case JUMP_INSN:
1675     case CALL_INSN:
1676     case NOTE:
1677     case BARRIER:
1678       /* The chain of insns is not being copied.  */
1679       return x;
1680
1681     case MEM:
1682       /* A MEM is allowed to be shared if its address is constant.
1683
1684          We used to allow sharing of MEMs which referenced 
1685          virtual_stack_vars_rtx or virtual_incoming_args_rtx, but
1686          that can lose.  instantiate_virtual_regs will not unshare
1687          the MEMs, and combine may change the structure of the address
1688          because it looks safe and profitable in one context, but
1689          in some other context it creates unrecognizable RTL.  */
1690       if (CONSTANT_ADDRESS_P (XEXP (x, 0)))
1691         return x;
1692
1693       break;
1694
1695     default:
1696       break;
1697     }
1698
1699   /* This rtx may not be shared.  If it has already been seen,
1700      replace it with a copy of itself.  */
1701
1702   if (x->used)
1703     {
1704       register rtx copy;
1705
1706       copy = rtx_alloc (code);
1707       bcopy ((char *) x, (char *) copy,
1708              (sizeof (*copy) - sizeof (copy->fld)
1709               + sizeof (copy->fld[0]) * GET_RTX_LENGTH (code)));
1710       x = copy;
1711       copied = 1;
1712     }
1713   x->used = 1;
1714
1715   /* Now scan the subexpressions recursively.
1716      We can store any replaced subexpressions directly into X
1717      since we know X is not shared!  Any vectors in X
1718      must be copied if X was copied.  */
1719
1720   format_ptr = GET_RTX_FORMAT (code);
1721
1722   for (i = 0; i < GET_RTX_LENGTH (code); i++)
1723     {
1724       switch (*format_ptr++)
1725         {
1726         case 'e':
1727           XEXP (x, i) = copy_rtx_if_shared (XEXP (x, i));
1728           break;
1729
1730         case 'E':
1731           if (XVEC (x, i) != NULL)
1732             {
1733               register int j;
1734               int len = XVECLEN (x, i);
1735
1736               if (copied && len > 0)
1737                 XVEC (x, i) = gen_rtvec_v (len, XVEC (x, i)->elem);
1738               for (j = 0; j < len; j++)
1739                 XVECEXP (x, i, j) = copy_rtx_if_shared (XVECEXP (x, i, j));
1740             }
1741           break;
1742         }
1743     }
1744   return x;
1745 }
1746
1747 /* Clear all the USED bits in X to allow copy_rtx_if_shared to be used
1748    to look for shared sub-parts.  */
1749
1750 void
1751 reset_used_flags (x)
1752      rtx x;
1753 {
1754   register int i, j;
1755   register enum rtx_code code;
1756   register const char *format_ptr;
1757
1758   if (x == 0)
1759     return;
1760
1761   code = GET_CODE (x);
1762
1763   /* These types may be freely shared so we needn't do any resetting
1764      for them.  */
1765
1766   switch (code)
1767     {
1768     case REG:
1769     case QUEUED:
1770     case CONST_INT:
1771     case CONST_DOUBLE:
1772     case SYMBOL_REF:
1773     case CODE_LABEL:
1774     case PC:
1775     case CC0:
1776       return;
1777
1778     case INSN:
1779     case JUMP_INSN:
1780     case CALL_INSN:
1781     case NOTE:
1782     case LABEL_REF:
1783     case BARRIER:
1784       /* The chain of insns is not being copied.  */
1785       return;
1786       
1787     default:
1788       break;
1789     }
1790
1791   x->used = 0;
1792
1793   format_ptr = GET_RTX_FORMAT (code);
1794   for (i = 0; i < GET_RTX_LENGTH (code); i++)
1795     {
1796       switch (*format_ptr++)
1797         {
1798         case 'e':
1799           reset_used_flags (XEXP (x, i));
1800           break;
1801
1802         case 'E':
1803           for (j = 0; j < XVECLEN (x, i); j++)
1804             reset_used_flags (XVECEXP (x, i, j));
1805           break;
1806         }
1807     }
1808 }
1809 \f
1810 /* Copy X if necessary so that it won't be altered by changes in OTHER.
1811    Return X or the rtx for the pseudo reg the value of X was copied into.
1812    OTHER must be valid as a SET_DEST.  */
1813
1814 rtx
1815 make_safe_from (x, other)
1816      rtx x, other;
1817 {
1818   while (1)
1819     switch (GET_CODE (other))
1820       {
1821       case SUBREG:
1822         other = SUBREG_REG (other);
1823         break;
1824       case STRICT_LOW_PART:
1825       case SIGN_EXTEND:
1826       case ZERO_EXTEND:
1827         other = XEXP (other, 0);
1828         break;
1829       default:
1830         goto done;
1831       }
1832  done:
1833   if ((GET_CODE (other) == MEM
1834        && ! CONSTANT_P (x)
1835        && GET_CODE (x) != REG
1836        && GET_CODE (x) != SUBREG)
1837       || (GET_CODE (other) == REG
1838           && (REGNO (other) < FIRST_PSEUDO_REGISTER
1839               || reg_mentioned_p (other, x))))
1840     {
1841       rtx temp = gen_reg_rtx (GET_MODE (x));
1842       emit_move_insn (temp, x);
1843       return temp;
1844     }
1845   return x;
1846 }
1847 \f
1848 /* Emission of insns (adding them to the doubly-linked list).  */
1849
1850 /* Return the first insn of the current sequence or current function.  */
1851
1852 rtx
1853 get_insns ()
1854 {
1855   return first_insn;
1856 }
1857
1858 /* Return the last insn emitted in current sequence or current function.  */
1859
1860 rtx
1861 get_last_insn ()
1862 {
1863   return last_insn;
1864 }
1865
1866 /* Specify a new insn as the last in the chain.  */
1867
1868 void
1869 set_last_insn (insn)
1870      rtx insn;
1871 {
1872   if (NEXT_INSN (insn) != 0)
1873     abort ();
1874   last_insn = insn;
1875 }
1876
1877 /* Return the last insn emitted, even if it is in a sequence now pushed.  */
1878
1879 rtx
1880 get_last_insn_anywhere ()
1881 {
1882   struct sequence_stack *stack;
1883   if (last_insn)
1884     return last_insn;
1885   for (stack = seq_stack; stack; stack = stack->next)
1886     if (stack->last != 0)
1887       return stack->last;
1888   return 0;
1889 }
1890
1891 /* Return a number larger than any instruction's uid in this function.  */
1892
1893 int
1894 get_max_uid ()
1895 {
1896   return cur_insn_uid;
1897 }
1898 \f
1899 /* Return the next insn.  If it is a SEQUENCE, return the first insn
1900    of the sequence.  */
1901
1902 rtx
1903 next_insn (insn)
1904      rtx insn;
1905 {
1906   if (insn)
1907     {
1908       insn = NEXT_INSN (insn);
1909       if (insn && GET_CODE (insn) == INSN
1910           && GET_CODE (PATTERN (insn)) == SEQUENCE)
1911         insn = XVECEXP (PATTERN (insn), 0, 0);
1912     }
1913
1914   return insn;
1915 }
1916
1917 /* Return the previous insn.  If it is a SEQUENCE, return the last insn
1918    of the sequence.  */
1919
1920 rtx
1921 previous_insn (insn)
1922      rtx insn;
1923 {
1924   if (insn)
1925     {
1926       insn = PREV_INSN (insn);
1927       if (insn && GET_CODE (insn) == INSN
1928           && GET_CODE (PATTERN (insn)) == SEQUENCE)
1929         insn = XVECEXP (PATTERN (insn), 0, XVECLEN (PATTERN (insn), 0) - 1);
1930     }
1931
1932   return insn;
1933 }
1934
1935 /* Return the next insn after INSN that is not a NOTE.  This routine does not
1936    look inside SEQUENCEs.  */
1937
1938 rtx
1939 next_nonnote_insn (insn)
1940      rtx insn;
1941 {
1942   while (insn)
1943     {
1944       insn = NEXT_INSN (insn);
1945       if (insn == 0 || GET_CODE (insn) != NOTE)
1946         break;
1947     }
1948
1949   return insn;
1950 }
1951
1952 /* Return the previous insn before INSN that is not a NOTE.  This routine does
1953    not look inside SEQUENCEs.  */
1954
1955 rtx
1956 prev_nonnote_insn (insn)
1957      rtx insn;
1958 {
1959   while (insn)
1960     {
1961       insn = PREV_INSN (insn);
1962       if (insn == 0 || GET_CODE (insn) != NOTE)
1963         break;
1964     }
1965
1966   return insn;
1967 }
1968
1969 /* Return the next INSN, CALL_INSN or JUMP_INSN after INSN;
1970    or 0, if there is none.  This routine does not look inside
1971    SEQUENCEs.  */
1972
1973 rtx
1974 next_real_insn (insn)
1975      rtx insn;
1976 {
1977   while (insn)
1978     {
1979       insn = NEXT_INSN (insn);
1980       if (insn == 0 || GET_CODE (insn) == INSN
1981           || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
1982         break;
1983     }
1984
1985   return insn;
1986 }
1987
1988 /* Return the last INSN, CALL_INSN or JUMP_INSN before INSN;
1989    or 0, if there is none.  This routine does not look inside
1990    SEQUENCEs.  */
1991
1992 rtx
1993 prev_real_insn (insn)
1994      rtx insn;
1995 {
1996   while (insn)
1997     {
1998       insn = PREV_INSN (insn);
1999       if (insn == 0 || GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
2000           || GET_CODE (insn) == JUMP_INSN)
2001         break;
2002     }
2003
2004   return insn;
2005 }
2006
2007 /* Find the next insn after INSN that really does something.  This routine
2008    does not look inside SEQUENCEs.  Until reload has completed, this is the
2009    same as next_real_insn.  */
2010
2011 rtx
2012 next_active_insn (insn)
2013      rtx insn;
2014 {
2015   while (insn)
2016     {
2017       insn = NEXT_INSN (insn);
2018       if (insn == 0
2019           || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
2020           || (GET_CODE (insn) == INSN
2021               && (! reload_completed
2022                   || (GET_CODE (PATTERN (insn)) != USE
2023                       && GET_CODE (PATTERN (insn)) != CLOBBER))))
2024         break;
2025     }
2026
2027   return insn;
2028 }
2029
2030 /* Find the last insn before INSN that really does something.  This routine
2031    does not look inside SEQUENCEs.  Until reload has completed, this is the
2032    same as prev_real_insn.  */
2033
2034 rtx
2035 prev_active_insn (insn)
2036      rtx insn;
2037 {
2038   while (insn)
2039     {
2040       insn = PREV_INSN (insn);
2041       if (insn == 0
2042           || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
2043           || (GET_CODE (insn) == INSN
2044               && (! reload_completed
2045                   || (GET_CODE (PATTERN (insn)) != USE
2046                       && GET_CODE (PATTERN (insn)) != CLOBBER))))
2047         break;
2048     }
2049
2050   return insn;
2051 }
2052
2053 /* Return the next CODE_LABEL after the insn INSN, or 0 if there is none.  */
2054
2055 rtx
2056 next_label (insn)
2057      rtx insn;
2058 {
2059   while (insn)
2060     {
2061       insn = NEXT_INSN (insn);
2062       if (insn == 0 || GET_CODE (insn) == CODE_LABEL)
2063         break;
2064     }
2065
2066   return insn;
2067 }
2068
2069 /* Return the last CODE_LABEL before the insn INSN, or 0 if there is none.  */
2070
2071 rtx
2072 prev_label (insn)
2073      rtx insn;
2074 {
2075   while (insn)
2076     {
2077       insn = PREV_INSN (insn);
2078       if (insn == 0 || GET_CODE (insn) == CODE_LABEL)
2079         break;
2080     }
2081
2082   return insn;
2083 }
2084 \f
2085 #ifdef HAVE_cc0
2086 /* INSN uses CC0 and is being moved into a delay slot.  Set up REG_CC_SETTER
2087    and REG_CC_USER notes so we can find it.  */
2088
2089 void
2090 link_cc0_insns (insn)
2091      rtx insn;
2092 {
2093   rtx user = next_nonnote_insn (insn);
2094
2095   if (GET_CODE (user) == INSN && GET_CODE (PATTERN (user)) == SEQUENCE)
2096     user = XVECEXP (PATTERN (user), 0, 0);
2097
2098   REG_NOTES (user) = gen_rtx_INSN_LIST (REG_CC_SETTER, insn,
2099                                         REG_NOTES (user));
2100   REG_NOTES (insn) = gen_rtx_INSN_LIST (REG_CC_USER, user, REG_NOTES (insn));
2101 }
2102
2103 /* Return the next insn that uses CC0 after INSN, which is assumed to
2104    set it.  This is the inverse of prev_cc0_setter (i.e., prev_cc0_setter
2105    applied to the result of this function should yield INSN).
2106
2107    Normally, this is simply the next insn.  However, if a REG_CC_USER note
2108    is present, it contains the insn that uses CC0.
2109
2110    Return 0 if we can't find the insn.  */
2111
2112 rtx
2113 next_cc0_user (insn)
2114      rtx insn;
2115 {
2116   rtx note = find_reg_note (insn, REG_CC_USER, NULL_RTX);
2117
2118   if (note)
2119     return XEXP (note, 0);
2120
2121   insn = next_nonnote_insn (insn);
2122   if (insn && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
2123     insn = XVECEXP (PATTERN (insn), 0, 0);
2124
2125   if (insn && GET_RTX_CLASS (GET_CODE (insn)) == 'i'
2126       && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
2127     return insn;
2128
2129   return 0;
2130 }
2131
2132 /* Find the insn that set CC0 for INSN.  Unless INSN has a REG_CC_SETTER
2133    note, it is the previous insn.  */
2134
2135 rtx
2136 prev_cc0_setter (insn)
2137      rtx insn;
2138 {
2139   rtx note = find_reg_note (insn, REG_CC_SETTER, NULL_RTX);
2140
2141   if (note)
2142     return XEXP (note, 0);
2143
2144   insn = prev_nonnote_insn (insn);
2145   if (! sets_cc0_p (PATTERN (insn)))
2146     abort ();
2147
2148   return insn;
2149 }
2150 #endif
2151 \f
2152 /* Try splitting insns that can be split for better scheduling.
2153    PAT is the pattern which might split.
2154    TRIAL is the insn providing PAT.
2155    LAST is non-zero if we should return the last insn of the sequence produced.
2156
2157    If this routine succeeds in splitting, it returns the first or last
2158    replacement insn depending on the value of LAST.  Otherwise, it
2159    returns TRIAL.  If the insn to be returned can be split, it will be.  */
2160
2161 rtx
2162 try_split (pat, trial, last)
2163      rtx pat, trial;
2164      int last;
2165 {
2166   rtx before = PREV_INSN (trial);
2167   rtx after = NEXT_INSN (trial);
2168   rtx seq = split_insns (pat, trial);
2169   int has_barrier = 0;
2170   rtx tem;
2171
2172   /* If we are splitting a JUMP_INSN, it might be followed by a BARRIER.
2173      We may need to handle this specially.  */
2174   if (after && GET_CODE (after) == BARRIER)
2175     {
2176       has_barrier = 1;
2177       after = NEXT_INSN (after);
2178     }
2179
2180   if (seq)
2181     {
2182       /* SEQ can either be a SEQUENCE or the pattern of a single insn.
2183          The latter case will normally arise only when being done so that
2184          it, in turn, will be split (SFmode on the 29k is an example).  */
2185       if (GET_CODE (seq) == SEQUENCE)
2186         {
2187           /* If we are splitting a JUMP_INSN, look for the JUMP_INSN in
2188              SEQ and copy our JUMP_LABEL to it.  If JUMP_LABEL is non-zero,
2189              increment the usage count so we don't delete the label.  */
2190           int i;
2191
2192           if (GET_CODE (trial) == JUMP_INSN)
2193             for (i = XVECLEN (seq, 0) - 1; i >= 0; i--)
2194               if (GET_CODE (XVECEXP (seq, 0, i)) == JUMP_INSN)
2195                 {
2196                   JUMP_LABEL (XVECEXP (seq, 0, i)) = JUMP_LABEL (trial);
2197
2198                   if (JUMP_LABEL (trial))
2199                     LABEL_NUSES (JUMP_LABEL (trial))++;
2200                 }
2201
2202           tem = emit_insn_after (seq, before);
2203
2204           delete_insn (trial);
2205           if (has_barrier)
2206             emit_barrier_after (tem);
2207
2208           /* Recursively call try_split for each new insn created; by the
2209              time control returns here that insn will be fully split, so
2210              set LAST and continue from the insn after the one returned.
2211              We can't use next_active_insn here since AFTER may be a note.
2212              Ignore deleted insns, which can be occur if not optimizing.  */
2213           for (tem = NEXT_INSN (before); tem != after;
2214                tem = NEXT_INSN (tem))
2215             if (! INSN_DELETED_P (tem)
2216                 && GET_RTX_CLASS (GET_CODE (tem)) == 'i')
2217               tem = try_split (PATTERN (tem), tem, 1);
2218         }
2219       /* Avoid infinite loop if the result matches the original pattern.  */
2220       else if (rtx_equal_p (seq, pat))
2221         return trial;
2222       else
2223         {
2224           PATTERN (trial) = seq;
2225           INSN_CODE (trial) = -1;
2226           try_split (seq, trial, last);
2227         }
2228
2229       /* Return either the first or the last insn, depending on which was
2230          requested.  */
2231       return last ? prev_active_insn (after) : next_active_insn (before);
2232     }
2233
2234   return trial;
2235 }
2236 \f
2237 /* Make and return an INSN rtx, initializing all its slots.
2238    Store PATTERN in the pattern slots.  */
2239
2240 rtx
2241 make_insn_raw (pattern)
2242      rtx pattern;
2243 {
2244   register rtx insn;
2245
2246   /* If in RTL generation phase, see if FREE_INSN can be used.  */
2247   if (!ggc_p && free_insn != 0 && rtx_equal_function_value_matters)
2248     {
2249       insn = free_insn;
2250       free_insn = NEXT_INSN (free_insn);
2251       PUT_CODE (insn, INSN);
2252     }
2253   else
2254     insn = rtx_alloc (INSN);
2255
2256   INSN_UID (insn) = cur_insn_uid++;
2257   PATTERN (insn) = pattern;
2258   INSN_CODE (insn) = -1;
2259   LOG_LINKS (insn) = NULL;
2260   REG_NOTES (insn) = NULL;
2261
2262   return insn;
2263 }
2264
2265 /* Like `make_insn' but make a JUMP_INSN instead of an insn.  */
2266
2267 static rtx
2268 make_jump_insn_raw (pattern)
2269      rtx pattern;
2270 {
2271   register rtx insn;
2272
2273   insn = rtx_alloc (JUMP_INSN);
2274   INSN_UID (insn) = cur_insn_uid++;
2275
2276   PATTERN (insn) = pattern;
2277   INSN_CODE (insn) = -1;
2278   LOG_LINKS (insn) = NULL;
2279   REG_NOTES (insn) = NULL;
2280   JUMP_LABEL (insn) = NULL;
2281
2282   return insn;
2283 }
2284
2285 /* Like `make_insn' but make a CALL_INSN instead of an insn.  */
2286
2287 static rtx
2288 make_call_insn_raw (pattern)
2289      rtx pattern;
2290 {
2291   register rtx insn;
2292
2293   insn = rtx_alloc (CALL_INSN);
2294   INSN_UID (insn) = cur_insn_uid++;
2295
2296   PATTERN (insn) = pattern;
2297   INSN_CODE (insn) = -1;
2298   LOG_LINKS (insn) = NULL;
2299   REG_NOTES (insn) = NULL;
2300   CALL_INSN_FUNCTION_USAGE (insn) = NULL;
2301
2302   return insn;
2303 }
2304 \f
2305 /* Add INSN to the end of the doubly-linked list.
2306    INSN may be an INSN, JUMP_INSN, CALL_INSN, CODE_LABEL, BARRIER or NOTE.  */
2307
2308 void
2309 add_insn (insn)
2310      register rtx insn;
2311 {
2312   PREV_INSN (insn) = last_insn;
2313   NEXT_INSN (insn) = 0;
2314
2315   if (NULL != last_insn)
2316     NEXT_INSN (last_insn) = insn;
2317
2318   if (NULL == first_insn)
2319     first_insn = insn;
2320
2321   last_insn = insn;
2322 }
2323
2324 /* Add INSN into the doubly-linked list after insn AFTER.  This and
2325    the next should be the only functions called to insert an insn once
2326    delay slots have been filled since only they know how to update a
2327    SEQUENCE.  */
2328
2329 void
2330 add_insn_after (insn, after)
2331      rtx insn, after;
2332 {
2333   rtx next = NEXT_INSN (after);
2334
2335   if (optimize && INSN_DELETED_P (after))
2336     abort ();
2337
2338   NEXT_INSN (insn) = next;
2339   PREV_INSN (insn) = after;
2340
2341   if (next)
2342     {
2343       PREV_INSN (next) = insn;
2344       if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
2345         PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = insn;
2346     }
2347   else if (last_insn == after)
2348     last_insn = insn;
2349   else
2350     {
2351       struct sequence_stack *stack = seq_stack;
2352       /* Scan all pending sequences too.  */
2353       for (; stack; stack = stack->next)
2354         if (after == stack->last)
2355           {
2356             stack->last = insn;
2357             break;
2358           }
2359
2360       if (stack == 0)
2361         abort ();
2362     }
2363
2364   NEXT_INSN (after) = insn;
2365   if (GET_CODE (after) == INSN && GET_CODE (PATTERN (after)) == SEQUENCE)
2366     {
2367       rtx sequence = PATTERN (after);
2368       NEXT_INSN (XVECEXP (sequence, 0, XVECLEN (sequence, 0) - 1)) = insn;
2369     }
2370 }
2371
2372 /* Add INSN into the doubly-linked list before insn BEFORE.  This and
2373    the previous should be the only functions called to insert an insn once
2374    delay slots have been filled since only they know how to update a
2375    SEQUENCE.  */
2376
2377 void
2378 add_insn_before (insn, before)
2379      rtx insn, before;
2380 {
2381   rtx prev = PREV_INSN (before);
2382
2383   if (optimize && INSN_DELETED_P (before))
2384     abort ();
2385
2386   PREV_INSN (insn) = prev;
2387   NEXT_INSN (insn) = before;
2388
2389   if (prev)
2390     {
2391       NEXT_INSN (prev) = insn;
2392       if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
2393         {
2394           rtx sequence = PATTERN (prev);
2395           NEXT_INSN (XVECEXP (sequence, 0, XVECLEN (sequence, 0) - 1)) = insn;
2396         }
2397     }
2398   else if (first_insn == before)
2399     first_insn = insn;
2400   else
2401     {
2402       struct sequence_stack *stack = seq_stack;
2403       /* Scan all pending sequences too.  */
2404       for (; stack; stack = stack->next)
2405         if (before == stack->first)
2406           {
2407             stack->first = insn;
2408             break;
2409           }
2410
2411       if (stack == 0)
2412         abort ();
2413     }
2414
2415   PREV_INSN (before) = insn;
2416   if (GET_CODE (before) == INSN && GET_CODE (PATTERN (before)) == SEQUENCE)
2417     PREV_INSN (XVECEXP (PATTERN (before), 0, 0)) = insn;
2418 }
2419
2420 /* Remove an insn from its doubly-linked list.  This function knows how
2421    to handle sequences.  */
2422 void
2423 remove_insn (insn)
2424      rtx insn;
2425 {
2426   rtx next = NEXT_INSN (insn);
2427   rtx prev = PREV_INSN (insn);
2428   if (prev)
2429     {
2430       NEXT_INSN (prev) = next;
2431       if (GET_CODE (prev) == INSN && GET_CODE (PATTERN (prev)) == SEQUENCE)
2432         {
2433           rtx sequence = PATTERN (prev);
2434           NEXT_INSN (XVECEXP (sequence, 0, XVECLEN (sequence, 0) - 1)) = next;
2435         }
2436     }
2437   else if (first_insn == insn)
2438     first_insn = next;
2439   else
2440     {
2441       struct sequence_stack *stack = seq_stack;
2442       /* Scan all pending sequences too.  */
2443       for (; stack; stack = stack->next)
2444         if (insn == stack->first)
2445           {
2446             stack->first = next;
2447             break;
2448           }
2449
2450       if (stack == 0)
2451         abort ();
2452     }
2453
2454   if (next)
2455     {
2456       PREV_INSN (next) = prev;
2457       if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
2458         PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = prev;
2459     }
2460   else if (last_insn == insn)
2461     last_insn = prev;
2462   else
2463     {
2464       struct sequence_stack *stack = seq_stack;
2465       /* Scan all pending sequences too.  */
2466       for (; stack; stack = stack->next)
2467         if (insn == stack->last)
2468           {
2469             stack->last = prev;
2470             break;
2471           }
2472
2473       if (stack == 0)
2474         abort ();
2475     }
2476 }
2477
2478 /* Delete all insns made since FROM.
2479    FROM becomes the new last instruction.  */
2480
2481 void
2482 delete_insns_since (from)
2483      rtx from;
2484 {
2485   if (from == 0)
2486     first_insn = 0;
2487   else
2488     NEXT_INSN (from) = 0;
2489   last_insn = from;
2490 }
2491
2492 /* This function is deprecated, please use sequences instead.
2493
2494    Move a consecutive bunch of insns to a different place in the chain.
2495    The insns to be moved are those between FROM and TO.
2496    They are moved to a new position after the insn AFTER.
2497    AFTER must not be FROM or TO or any insn in between.
2498
2499    This function does not know about SEQUENCEs and hence should not be
2500    called after delay-slot filling has been done.  */
2501
2502 void
2503 reorder_insns (from, to, after)
2504      rtx from, to, after;
2505 {
2506   /* Splice this bunch out of where it is now.  */
2507   if (PREV_INSN (from))
2508     NEXT_INSN (PREV_INSN (from)) = NEXT_INSN (to);
2509   if (NEXT_INSN (to))
2510     PREV_INSN (NEXT_INSN (to)) = PREV_INSN (from);
2511   if (last_insn == to)
2512     last_insn = PREV_INSN (from);
2513   if (first_insn == from)
2514     first_insn = NEXT_INSN (to);
2515
2516   /* Make the new neighbors point to it and it to them.  */
2517   if (NEXT_INSN (after))
2518     PREV_INSN (NEXT_INSN (after)) = to;
2519
2520   NEXT_INSN (to) = NEXT_INSN (after);
2521   PREV_INSN (from) = after;
2522   NEXT_INSN (after) = from;
2523   if (after == last_insn)
2524     last_insn = to;
2525 }
2526
2527 /* Return the line note insn preceding INSN.  */
2528
2529 static rtx
2530 find_line_note (insn)
2531      rtx insn;
2532 {
2533   if (no_line_numbers)
2534     return 0;
2535
2536   for (; insn; insn = PREV_INSN (insn))
2537     if (GET_CODE (insn) == NOTE
2538         && NOTE_LINE_NUMBER (insn) >= 0)
2539       break;
2540
2541   return insn;
2542 }
2543
2544 /* Like reorder_insns, but inserts line notes to preserve the line numbers
2545    of the moved insns when debugging.  This may insert a note between AFTER
2546    and FROM, and another one after TO.  */
2547
2548 void
2549 reorder_insns_with_line_notes (from, to, after)
2550      rtx from, to, after;
2551 {
2552   rtx from_line = find_line_note (from);
2553   rtx after_line = find_line_note (after);
2554
2555   reorder_insns (from, to, after);
2556
2557   if (from_line == after_line)
2558     return;
2559
2560   if (from_line)
2561     emit_line_note_after (NOTE_SOURCE_FILE (from_line),
2562                           NOTE_LINE_NUMBER (from_line),
2563                           after);
2564   if (after_line)
2565     emit_line_note_after (NOTE_SOURCE_FILE (after_line),
2566                           NOTE_LINE_NUMBER (after_line),
2567                           to);
2568 }
2569 \f
2570 /* Emit an insn of given code and pattern
2571    at a specified place within the doubly-linked list.  */
2572
2573 /* Make an instruction with body PATTERN
2574    and output it before the instruction BEFORE.  */
2575
2576 rtx
2577 emit_insn_before (pattern, before)
2578      register rtx pattern, before;
2579 {
2580   register rtx insn = before;
2581
2582   if (GET_CODE (pattern) == SEQUENCE)
2583     {
2584       register int i;
2585
2586       for (i = 0; i < XVECLEN (pattern, 0); i++)
2587         {
2588           insn = XVECEXP (pattern, 0, i);
2589           add_insn_before (insn, before);
2590         }
2591       if (!ggc_p && XVECLEN (pattern, 0) < SEQUENCE_RESULT_SIZE)
2592         sequence_result[XVECLEN (pattern, 0)] = pattern;
2593     }
2594   else
2595     {
2596       insn = make_insn_raw (pattern);
2597       add_insn_before (insn, before);
2598     }
2599
2600   return insn;
2601 }
2602
2603 /* Make an instruction with body PATTERN and code JUMP_INSN
2604    and output it before the instruction BEFORE.  */
2605
2606 rtx
2607 emit_jump_insn_before (pattern, before)
2608      register rtx pattern, before;
2609 {
2610   register rtx insn;
2611
2612   if (GET_CODE (pattern) == SEQUENCE)
2613     insn = emit_insn_before (pattern, before);
2614   else
2615     {
2616       insn = make_jump_insn_raw (pattern);
2617       add_insn_before (insn, before);
2618     }
2619
2620   return insn;
2621 }
2622
2623 /* Make an instruction with body PATTERN and code CALL_INSN
2624    and output it before the instruction BEFORE.  */
2625
2626 rtx
2627 emit_call_insn_before (pattern, before)
2628      register rtx pattern, before;
2629 {
2630   register rtx insn;
2631
2632   if (GET_CODE (pattern) == SEQUENCE)
2633     insn = emit_insn_before (pattern, before);
2634   else
2635     {
2636       insn = make_call_insn_raw (pattern);
2637       add_insn_before (insn, before);
2638       PUT_CODE (insn, CALL_INSN);
2639     }
2640
2641   return insn;
2642 }
2643
2644 /* Make an insn of code BARRIER
2645    and output it before the insn BEFORE.  */
2646
2647 rtx
2648 emit_barrier_before (before)
2649      register rtx before;
2650 {
2651   register rtx insn = rtx_alloc (BARRIER);
2652
2653   INSN_UID (insn) = cur_insn_uid++;
2654
2655   add_insn_before (insn, before);
2656   return insn;
2657 }
2658
2659 /* Emit the label LABEL before the insn BEFORE.  */
2660
2661 rtx
2662 emit_label_before (label, before)
2663      rtx label, before;
2664 {
2665   /* This can be called twice for the same label as a result of the
2666      confusion that follows a syntax error!  So make it harmless.  */
2667   if (INSN_UID (label) == 0)
2668     {
2669       INSN_UID (label) = cur_insn_uid++;
2670       add_insn_before (label, before);
2671     }
2672
2673   return label;
2674 }
2675
2676 /* Emit a note of subtype SUBTYPE before the insn BEFORE.  */
2677
2678 rtx
2679 emit_note_before (subtype, before)
2680      int subtype;
2681      rtx before;
2682 {
2683   register rtx note = rtx_alloc (NOTE);
2684   INSN_UID (note) = cur_insn_uid++;
2685   NOTE_SOURCE_FILE (note) = 0;
2686   NOTE_LINE_NUMBER (note) = subtype;
2687
2688   add_insn_before (note, before);
2689   return note;
2690 }
2691 \f
2692 /* Make an insn of code INSN with body PATTERN
2693    and output it after the insn AFTER.  */
2694
2695 rtx
2696 emit_insn_after (pattern, after)
2697      register rtx pattern, after;
2698 {
2699   register rtx insn = after;
2700
2701   if (GET_CODE (pattern) == SEQUENCE)
2702     {
2703       register int i;
2704
2705       for (i = 0; i < XVECLEN (pattern, 0); i++)
2706         {
2707           insn = XVECEXP (pattern, 0, i);
2708           add_insn_after (insn, after);
2709           after = insn;
2710         }
2711       if (!ggc_p && XVECLEN (pattern, 0) < SEQUENCE_RESULT_SIZE)
2712         sequence_result[XVECLEN (pattern, 0)] = pattern;
2713     }
2714   else
2715     {
2716       insn = make_insn_raw (pattern);
2717       add_insn_after (insn, after);
2718     }
2719
2720   return insn;
2721 }
2722
2723 /* Similar to emit_insn_after, except that line notes are to be inserted so
2724    as to act as if this insn were at FROM.  */
2725
2726 void
2727 emit_insn_after_with_line_notes (pattern, after, from)
2728      rtx pattern, after, from;
2729 {
2730   rtx from_line = find_line_note (from);
2731   rtx after_line = find_line_note (after);
2732   rtx insn = emit_insn_after (pattern, after);
2733
2734   if (from_line)
2735     emit_line_note_after (NOTE_SOURCE_FILE (from_line),
2736                           NOTE_LINE_NUMBER (from_line),
2737                           after);
2738
2739   if (after_line)
2740     emit_line_note_after (NOTE_SOURCE_FILE (after_line),
2741                           NOTE_LINE_NUMBER (after_line),
2742                           insn);
2743 }
2744
2745 /* Make an insn of code JUMP_INSN with body PATTERN
2746    and output it after the insn AFTER.  */
2747
2748 rtx
2749 emit_jump_insn_after (pattern, after)
2750      register rtx pattern, after;
2751 {
2752   register rtx insn;
2753
2754   if (GET_CODE (pattern) == SEQUENCE)
2755     insn = emit_insn_after (pattern, after);
2756   else
2757     {
2758       insn = make_jump_insn_raw (pattern);
2759       add_insn_after (insn, after);
2760     }
2761
2762   return insn;
2763 }
2764
2765 /* Make an insn of code BARRIER
2766    and output it after the insn AFTER.  */
2767
2768 rtx
2769 emit_barrier_after (after)
2770      register rtx after;
2771 {
2772   register rtx insn = rtx_alloc (BARRIER);
2773
2774   INSN_UID (insn) = cur_insn_uid++;
2775
2776   add_insn_after (insn, after);
2777   return insn;
2778 }
2779
2780 /* Emit the label LABEL after the insn AFTER.  */
2781
2782 rtx
2783 emit_label_after (label, after)
2784      rtx label, after;
2785 {
2786   /* This can be called twice for the same label
2787      as a result of the confusion that follows a syntax error!
2788      So make it harmless.  */
2789   if (INSN_UID (label) == 0)
2790     {
2791       INSN_UID (label) = cur_insn_uid++;
2792       add_insn_after (label, after);
2793     }
2794
2795   return label;
2796 }
2797
2798 /* Emit a note of subtype SUBTYPE after the insn AFTER.  */
2799
2800 rtx
2801 emit_note_after (subtype, after)
2802      int subtype;
2803      rtx after;
2804 {
2805   register rtx note = rtx_alloc (NOTE);
2806   INSN_UID (note) = cur_insn_uid++;
2807   NOTE_SOURCE_FILE (note) = 0;
2808   NOTE_LINE_NUMBER (note) = subtype;
2809   add_insn_after (note, after);
2810   return note;
2811 }
2812
2813 /* Emit a line note for FILE and LINE after the insn AFTER.  */
2814
2815 rtx
2816 emit_line_note_after (file, line, after)
2817      char *file;
2818      int line;
2819      rtx after;
2820 {
2821   register rtx note;
2822
2823   if (no_line_numbers && line > 0)
2824     {
2825       cur_insn_uid++;
2826       return 0;
2827     }
2828
2829   note  = rtx_alloc (NOTE);
2830   INSN_UID (note) = cur_insn_uid++;
2831   NOTE_SOURCE_FILE (note) = file;
2832   NOTE_LINE_NUMBER (note) = line;
2833   add_insn_after (note, after);
2834   return note;
2835 }
2836 \f
2837 /* Make an insn of code INSN with pattern PATTERN
2838    and add it to the end of the doubly-linked list.
2839    If PATTERN is a SEQUENCE, take the elements of it
2840    and emit an insn for each element.
2841
2842    Returns the last insn emitted.  */
2843
2844 rtx
2845 emit_insn (pattern)
2846      rtx pattern;
2847 {
2848   rtx insn = last_insn;
2849
2850   if (GET_CODE (pattern) == SEQUENCE)
2851     {
2852       register int i;
2853
2854       for (i = 0; i < XVECLEN (pattern, 0); i++)
2855         {
2856           insn = XVECEXP (pattern, 0, i);
2857           add_insn (insn);
2858         }
2859       if (!ggc_p && XVECLEN (pattern, 0) < SEQUENCE_RESULT_SIZE)
2860         sequence_result[XVECLEN (pattern, 0)] = pattern;
2861     }
2862   else
2863     {
2864       insn = make_insn_raw (pattern);
2865       add_insn (insn);
2866     }
2867
2868   return insn;
2869 }
2870
2871 /* Emit the insns in a chain starting with INSN.
2872    Return the last insn emitted.  */
2873
2874 rtx
2875 emit_insns (insn)
2876      rtx insn;
2877 {
2878   rtx last = 0;
2879
2880   while (insn)
2881     {
2882       rtx next = NEXT_INSN (insn);
2883       add_insn (insn);
2884       last = insn;
2885       insn = next;
2886     }
2887
2888   return last;
2889 }
2890
2891 /* Emit the insns in a chain starting with INSN and place them in front of
2892    the insn BEFORE.  Return the last insn emitted.  */
2893
2894 rtx
2895 emit_insns_before (insn, before)
2896      rtx insn;
2897      rtx before;
2898 {
2899   rtx last = 0;
2900
2901   while (insn)
2902     {
2903       rtx next = NEXT_INSN (insn);
2904       add_insn_before (insn, before);
2905       last = insn;
2906       insn = next;
2907     }
2908
2909   return last;
2910 }
2911
2912 /* Emit the insns in a chain starting with FIRST and place them in back of
2913    the insn AFTER.  Return the last insn emitted.  */
2914
2915 rtx
2916 emit_insns_after (first, after)
2917      register rtx first;
2918      register rtx after;
2919 {
2920   register rtx last;
2921   register rtx after_after;
2922
2923   if (!after)
2924     abort ();
2925
2926   if (!first)
2927     return first;
2928
2929   for (last = first; NEXT_INSN (last); last = NEXT_INSN (last))
2930     continue;
2931
2932   after_after = NEXT_INSN (after);
2933
2934   NEXT_INSN (after) = first;
2935   PREV_INSN (first) = after;
2936   NEXT_INSN (last) = after_after;
2937   if (after_after)
2938     PREV_INSN (after_after) = last;
2939
2940   if (after == last_insn)
2941     last_insn = last;
2942   return last;
2943 }
2944
2945 /* Make an insn of code JUMP_INSN with pattern PATTERN
2946    and add it to the end of the doubly-linked list.  */
2947
2948 rtx
2949 emit_jump_insn (pattern)
2950      rtx pattern;
2951 {
2952   if (GET_CODE (pattern) == SEQUENCE)
2953     return emit_insn (pattern);
2954   else
2955     {
2956       register rtx insn = make_jump_insn_raw (pattern);
2957       add_insn (insn);
2958       return insn;
2959     }
2960 }
2961
2962 /* Make an insn of code CALL_INSN with pattern PATTERN
2963    and add it to the end of the doubly-linked list.  */
2964
2965 rtx
2966 emit_call_insn (pattern)
2967      rtx pattern;
2968 {
2969   if (GET_CODE (pattern) == SEQUENCE)
2970     return emit_insn (pattern);
2971   else
2972     {
2973       register rtx insn = make_call_insn_raw (pattern);
2974       add_insn (insn);
2975       PUT_CODE (insn, CALL_INSN);
2976       return insn;
2977     }
2978 }
2979
2980 /* Add the label LABEL to the end of the doubly-linked list.  */
2981
2982 rtx
2983 emit_label (label)
2984      rtx label;
2985 {
2986   /* This can be called twice for the same label
2987      as a result of the confusion that follows a syntax error!
2988      So make it harmless.  */
2989   if (INSN_UID (label) == 0)
2990     {
2991       INSN_UID (label) = cur_insn_uid++;
2992       add_insn (label);
2993     }
2994   return label;
2995 }
2996
2997 /* Make an insn of code BARRIER
2998    and add it to the end of the doubly-linked list.  */
2999
3000 rtx
3001 emit_barrier ()
3002 {
3003   register rtx barrier = rtx_alloc (BARRIER);
3004   INSN_UID (barrier) = cur_insn_uid++;
3005   add_insn (barrier);
3006   return barrier;
3007 }
3008
3009 /* Make an insn of code NOTE
3010    with data-fields specified by FILE and LINE
3011    and add it to the end of the doubly-linked list,
3012    but only if line-numbers are desired for debugging info.  */
3013
3014 rtx
3015 emit_line_note (file, line)
3016      char *file;
3017      int line;
3018 {
3019   set_file_and_line_for_stmt (file, line);
3020
3021 #if 0
3022   if (no_line_numbers)
3023     return 0;
3024 #endif
3025
3026   return emit_note (file, line);
3027 }
3028
3029 /* Make an insn of code NOTE
3030    with data-fields specified by FILE and LINE
3031    and add it to the end of the doubly-linked list.
3032    If it is a line-number NOTE, omit it if it matches the previous one.  */
3033
3034 rtx
3035 emit_note (file, line)
3036      char *file;
3037      int line;
3038 {
3039   register rtx note;
3040
3041   if (line > 0)
3042     {
3043       if (file && last_filename && !strcmp (file, last_filename)
3044           && line == last_linenum)
3045         return 0;
3046       last_filename = file;
3047       last_linenum = line;
3048     }
3049
3050   if (no_line_numbers && line > 0)
3051     {
3052       cur_insn_uid++;
3053       return 0;
3054     }
3055
3056   note = rtx_alloc (NOTE);
3057   INSN_UID (note) = cur_insn_uid++;
3058   NOTE_SOURCE_FILE (note) = file;
3059   NOTE_LINE_NUMBER (note) = line;
3060   add_insn (note);
3061   return note;
3062 }
3063
3064 /* Emit a NOTE, and don't omit it even if LINE is the previous note.  */
3065
3066 rtx
3067 emit_line_note_force (file, line)
3068      char *file;
3069      int line;
3070 {
3071   last_linenum = -1;
3072   return emit_line_note (file, line);
3073 }
3074
3075 /* Cause next statement to emit a line note even if the line number
3076    has not changed.  This is used at the beginning of a function.  */
3077
3078 void
3079 force_next_line_note ()
3080 {
3081   last_linenum = -1;
3082 }
3083
3084 /* Place a note of KIND on insn INSN with DATUM as the datum. If a
3085    note of this type already exists, remove it first. */
3086
3087 void 
3088 set_unique_reg_note (insn, kind, datum)
3089      rtx insn;
3090      enum reg_note kind;
3091      rtx datum;
3092 {
3093   rtx note = find_reg_note (insn, kind, NULL_RTX);
3094
3095   /* First remove the note if there already is one.  */
3096   if (note) 
3097     remove_note (insn, note);
3098
3099   REG_NOTES (insn) = gen_rtx_EXPR_LIST (kind, datum, REG_NOTES (insn));
3100 }
3101 \f
3102 /* Return an indication of which type of insn should have X as a body.
3103    The value is CODE_LABEL, INSN, CALL_INSN or JUMP_INSN.  */
3104
3105 enum rtx_code
3106 classify_insn (x)
3107      rtx x;
3108 {
3109   if (GET_CODE (x) == CODE_LABEL)
3110     return CODE_LABEL;
3111   if (GET_CODE (x) == CALL)
3112     return CALL_INSN;
3113   if (GET_CODE (x) == RETURN)
3114     return JUMP_INSN;
3115   if (GET_CODE (x) == SET)
3116     {
3117       if (SET_DEST (x) == pc_rtx)
3118         return JUMP_INSN;
3119       else if (GET_CODE (SET_SRC (x)) == CALL)
3120         return CALL_INSN;
3121       else
3122         return INSN;
3123     }
3124   if (GET_CODE (x) == PARALLEL)
3125     {
3126       register int j;
3127       for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
3128         if (GET_CODE (XVECEXP (x, 0, j)) == CALL)
3129           return CALL_INSN;
3130         else if (GET_CODE (XVECEXP (x, 0, j)) == SET
3131                  && SET_DEST (XVECEXP (x, 0, j)) == pc_rtx)
3132           return JUMP_INSN;
3133         else if (GET_CODE (XVECEXP (x, 0, j)) == SET
3134                  && GET_CODE (SET_SRC (XVECEXP (x, 0, j))) == CALL)
3135           return CALL_INSN;
3136     }
3137   return INSN;
3138 }
3139
3140 /* Emit the rtl pattern X as an appropriate kind of insn.
3141    If X is a label, it is simply added into the insn chain.  */
3142
3143 rtx
3144 emit (x)
3145      rtx x;
3146 {
3147   enum rtx_code code = classify_insn (x);
3148
3149   if (code == CODE_LABEL)
3150     return emit_label (x);
3151   else if (code == INSN)
3152     return emit_insn (x);
3153   else if (code == JUMP_INSN)
3154     {
3155       register rtx insn = emit_jump_insn (x);
3156       if (simplejump_p (insn) || GET_CODE (x) == RETURN)
3157         return emit_barrier ();
3158       return insn;
3159     }
3160   else if (code == CALL_INSN)
3161     return emit_call_insn (x);
3162   else
3163     abort ();
3164 }
3165 \f
3166 /* Begin emitting insns to a sequence which can be packaged in an
3167    RTL_EXPR.  If this sequence will contain something that might cause
3168    the compiler to pop arguments to function calls (because those
3169    pops have previously been deferred; see INHIBIT_DEFER_POP for more
3170    details), use do_pending_stack_adjust before calling this function.
3171    That will ensure that the deferred pops are not accidentally
3172    emitted in the middel of this sequence.  */
3173
3174 void
3175 start_sequence ()
3176 {
3177   struct sequence_stack *tem;
3178
3179   tem = (struct sequence_stack *) xmalloc (sizeof (struct sequence_stack));
3180
3181   tem->next = seq_stack;
3182   tem->first = first_insn;
3183   tem->last = last_insn;
3184   tem->sequence_rtl_expr = seq_rtl_expr;
3185
3186   seq_stack = tem;
3187
3188   first_insn = 0;
3189   last_insn = 0;
3190 }
3191
3192 /* Similarly, but indicate that this sequence will be placed in T, an
3193    RTL_EXPR.  See the documentation for start_sequence for more
3194    information about how to use this function.  */
3195
3196 void
3197 start_sequence_for_rtl_expr (t)
3198      tree t;
3199 {
3200   start_sequence ();
3201
3202   seq_rtl_expr = t;
3203 }
3204
3205 /* Set up the insn chain starting with FIRST as the current sequence,
3206    saving the previously current one.  See the documentation for
3207    start_sequence for more information about how to use this function.  */
3208
3209 void
3210 push_to_sequence (first)
3211      rtx first;
3212 {
3213   rtx last;
3214
3215   start_sequence ();
3216
3217   for (last = first; last && NEXT_INSN (last); last = NEXT_INSN (last));
3218
3219   first_insn = first;
3220   last_insn = last;
3221 }
3222
3223 /* Set up the outer-level insn chain
3224    as the current sequence, saving the previously current one.  */
3225
3226 void
3227 push_topmost_sequence ()
3228 {
3229   struct sequence_stack *stack, *top = NULL;
3230
3231   start_sequence ();
3232
3233   for (stack = seq_stack; stack; stack = stack->next)
3234     top = stack;
3235
3236   first_insn = top->first;
3237   last_insn = top->last;
3238   seq_rtl_expr = top->sequence_rtl_expr;
3239 }
3240
3241 /* After emitting to the outer-level insn chain, update the outer-level
3242    insn chain, and restore the previous saved state.  */
3243
3244 void
3245 pop_topmost_sequence ()
3246 {
3247   struct sequence_stack *stack, *top = NULL;
3248
3249   for (stack = seq_stack; stack; stack = stack->next)
3250     top = stack;
3251
3252   top->first = first_insn;
3253   top->last = last_insn;
3254   /* ??? Why don't we save seq_rtl_expr here?  */
3255
3256   end_sequence ();
3257 }
3258
3259 /* After emitting to a sequence, restore previous saved state.
3260
3261    To get the contents of the sequence just made, you must call
3262    `gen_sequence' *before* calling here.  
3263
3264    If the compiler might have deferred popping arguments while
3265    generating this sequence, and this sequence will not be immediately
3266    inserted into the instruction stream, use do_pending_stack_adjust
3267    before calling gen_sequence.  That will ensure that the deferred
3268    pops are inserted into this sequence, and not into some random
3269    location in the instruction stream.  See INHIBIT_DEFER_POP for more
3270    information about deferred popping of arguments.  */
3271
3272 void
3273 end_sequence ()
3274 {
3275   struct sequence_stack *tem = seq_stack;
3276
3277   first_insn = tem->first;
3278   last_insn = tem->last;
3279   seq_rtl_expr = tem->sequence_rtl_expr;
3280   seq_stack = tem->next;
3281
3282   free (tem);
3283 }
3284
3285 /* Return 1 if currently emitting into a sequence.  */
3286
3287 int
3288 in_sequence_p ()
3289 {
3290   return seq_stack != 0;
3291 }
3292
3293 /* Generate a SEQUENCE rtx containing the insns already emitted
3294    to the current sequence.
3295
3296    This is how the gen_... function from a DEFINE_EXPAND
3297    constructs the SEQUENCE that it returns.  */
3298
3299 rtx
3300 gen_sequence ()
3301 {
3302   rtx result;
3303   rtx tem;
3304   int i;
3305   int len;
3306
3307   /* Count the insns in the chain.  */
3308   len = 0;
3309   for (tem = first_insn; tem; tem = NEXT_INSN (tem))
3310     len++;
3311
3312   /* If only one insn, return its pattern rather than a SEQUENCE.
3313      (Now that we cache SEQUENCE expressions, it isn't worth special-casing
3314      the case of an empty list.)  */
3315   if (len == 1
3316       && ! RTX_FRAME_RELATED_P (first_insn)
3317       && (GET_CODE (first_insn) == INSN
3318           || GET_CODE (first_insn) == JUMP_INSN
3319           /* Don't discard the call usage field.  */
3320           || (GET_CODE (first_insn) == CALL_INSN
3321               && CALL_INSN_FUNCTION_USAGE (first_insn) == NULL_RTX)))
3322     {
3323       if (!ggc_p)
3324         {
3325           NEXT_INSN (first_insn) = free_insn;
3326           free_insn = first_insn;
3327         }
3328       return PATTERN (first_insn);
3329     }
3330
3331   /* Put them in a vector.  See if we already have a SEQUENCE of the
3332      appropriate length around.  */
3333   if (!ggc_p && len < SEQUENCE_RESULT_SIZE 
3334       && (result = sequence_result[len]) != 0)
3335     sequence_result[len] = 0;
3336   else
3337     {
3338       /* Ensure that this rtl goes in saveable_obstack, since we may
3339          cache it.  */
3340       push_obstacks_nochange ();
3341       rtl_in_saveable_obstack ();
3342       result = gen_rtx_SEQUENCE (VOIDmode, rtvec_alloc (len));
3343       pop_obstacks ();
3344     }
3345
3346   for (i = 0, tem = first_insn; tem; tem = NEXT_INSN (tem), i++)
3347     XVECEXP (result, 0, i) = tem;
3348
3349   return result;
3350 }
3351 \f
3352 /* Put the various virtual registers into REGNO_REG_RTX.  */
3353
3354 void
3355 init_virtual_regs (es)
3356      struct emit_status *es;
3357 {
3358   rtx *ptr = es->x_regno_reg_rtx;
3359   ptr[VIRTUAL_INCOMING_ARGS_REGNUM] = virtual_incoming_args_rtx;
3360   ptr[VIRTUAL_STACK_VARS_REGNUM] = virtual_stack_vars_rtx;
3361   ptr[VIRTUAL_STACK_DYNAMIC_REGNUM] = virtual_stack_dynamic_rtx;
3362   ptr[VIRTUAL_OUTGOING_ARGS_REGNUM] = virtual_outgoing_args_rtx;
3363   ptr[VIRTUAL_CFA_REGNUM] = virtual_cfa_rtx;
3364 }
3365
3366 void
3367 clear_emit_caches ()
3368 {
3369   int i;
3370
3371   /* Clear the start_sequence/gen_sequence cache.  */
3372   for (i = 0; i < SEQUENCE_RESULT_SIZE; i++)
3373     sequence_result[i] = 0;
3374   free_insn = 0;
3375 }
3376 \f
3377 /* Used by copy_insn_1 to avoid copying SCRATCHes more than once.  */
3378 static rtx copy_insn_scratch_in[MAX_RECOG_OPERANDS];
3379 static rtx copy_insn_scratch_out[MAX_RECOG_OPERANDS];
3380 static int copy_insn_n_scratches;
3381
3382 /* When an insn is being copied by copy_insn_1, this is nonzero if we have
3383    copied an ASM_OPERANDS.
3384    In that case, it is the original input-operand vector.  */
3385 static rtvec orig_asm_operands_vector;
3386
3387 /* When an insn is being copied by copy_insn_1, this is nonzero if we have
3388    copied an ASM_OPERANDS.
3389    In that case, it is the copied input-operand vector.  */
3390 static rtvec copy_asm_operands_vector;
3391
3392 /* Likewise for the constraints vector.  */
3393 static rtvec orig_asm_constraints_vector;
3394 static rtvec copy_asm_constraints_vector;
3395
3396 /* Recursively create a new copy of an rtx for copy_insn.
3397    This function differs from copy_rtx in that it handles SCRATCHes and
3398    ASM_OPERANDs properly.
3399    Normally, this function is not used directly; use copy_insn as front end.
3400    However, you could first copy an insn pattern with copy_insn and then use
3401    this function afterwards to properly copy any REG_NOTEs containing
3402    SCRATCHes.  */
3403
3404 rtx
3405 copy_insn_1 (orig)
3406      register rtx orig;
3407 {
3408   register rtx copy;
3409   register int i, j;
3410   register RTX_CODE code;
3411   register const char *format_ptr;
3412
3413   code = GET_CODE (orig);
3414
3415   switch (code)
3416     {
3417     case REG:
3418     case QUEUED:
3419     case CONST_INT:
3420     case CONST_DOUBLE:
3421     case SYMBOL_REF:
3422     case CODE_LABEL:
3423     case PC:
3424     case CC0:
3425     case ADDRESSOF:
3426       return orig;
3427
3428     case SCRATCH:
3429       for (i = 0; i < copy_insn_n_scratches; i++)
3430         if (copy_insn_scratch_in[i] == orig)
3431           return copy_insn_scratch_out[i];
3432       break;
3433
3434     case CONST:
3435       /* CONST can be shared if it contains a SYMBOL_REF.  If it contains
3436          a LABEL_REF, it isn't sharable.  */
3437       if (GET_CODE (XEXP (orig, 0)) == PLUS
3438           && GET_CODE (XEXP (XEXP (orig, 0), 0)) == SYMBOL_REF
3439           && GET_CODE (XEXP (XEXP (orig, 0), 1)) == CONST_INT)
3440         return orig;
3441       break;
3442       
3443       /* A MEM with a constant address is not sharable.  The problem is that
3444          the constant address may need to be reloaded.  If the mem is shared,
3445          then reloading one copy of this mem will cause all copies to appear
3446          to have been reloaded.  */
3447
3448     default:
3449       break;
3450     }
3451
3452   copy = rtx_alloc (code);
3453
3454   /* Copy the various flags, and other information.  We assume that
3455      all fields need copying, and then clear the fields that should
3456      not be copied.  That is the sensible default behavior, and forces
3457      us to explicitly document why we are *not* copying a flag.  */
3458   memcpy (copy, orig, sizeof (struct rtx_def) - sizeof (rtunion));
3459
3460   /* We do not copy the USED flag, which is used as a mark bit during
3461      walks over the RTL.  */
3462   copy->used = 0;
3463
3464   /* We do not copy JUMP, CALL, or FRAME_RELATED for INSNs.  */
3465   if (GET_RTX_CLASS (code) == 'i')
3466     {
3467       copy->jump = 0;
3468       copy->call = 0;
3469       copy->frame_related = 0;
3470     }
3471   
3472   format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
3473
3474   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
3475     {
3476       switch (*format_ptr++)
3477         {
3478         case 'e':
3479           XEXP (copy, i) = XEXP (orig, i);
3480           if (XEXP (orig, i) != NULL)
3481             XEXP (copy, i) = copy_insn_1 (XEXP (orig, i));
3482           break;
3483
3484         case '0':
3485         case 'u':
3486           XEXP (copy, i) = XEXP (orig, i);
3487           break;
3488
3489         case 'E':
3490         case 'V':
3491           XVEC (copy, i) = XVEC (orig, i);
3492           if (XVEC (orig, i) == orig_asm_constraints_vector)
3493             XVEC (copy, i) = copy_asm_constraints_vector;
3494           else if (XVEC (orig, i) == orig_asm_operands_vector)
3495             XVEC (copy, i) = copy_asm_operands_vector;
3496           else if (XVEC (orig, i) != NULL)
3497             {
3498               XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
3499               for (j = 0; j < XVECLEN (copy, i); j++)
3500                 XVECEXP (copy, i, j) = copy_insn_1 (XVECEXP (orig, i, j));
3501             }
3502           break;
3503
3504         case 'b':
3505           {
3506             bitmap new_bits = BITMAP_OBSTACK_ALLOC (rtl_obstack);
3507             bitmap_copy (new_bits, XBITMAP (orig, i));
3508             XBITMAP (copy, i) = new_bits;
3509             break;
3510           }
3511
3512         case 't':
3513           XTREE (copy, i) = XTREE (orig, i);
3514           break;
3515
3516         case 'w':
3517           XWINT (copy, i) = XWINT (orig, i);
3518           break;
3519
3520         case 'i':
3521           XINT (copy, i) = XINT (orig, i);
3522           break;
3523
3524         case 's':
3525         case 'S':
3526           XSTR (copy, i) = XSTR (orig, i);
3527           break;
3528
3529         default:
3530           abort ();
3531         }
3532     }
3533
3534   if (code == SCRATCH)
3535     {
3536       i = copy_insn_n_scratches++;
3537       if (i >= MAX_RECOG_OPERANDS)
3538         abort ();
3539       copy_insn_scratch_in[i] = orig;
3540       copy_insn_scratch_out[i] = copy;
3541     }
3542   else if (code == ASM_OPERANDS)
3543     {
3544       orig_asm_operands_vector = XVEC (orig, 3);
3545       copy_asm_operands_vector = XVEC (copy, 3);
3546       orig_asm_constraints_vector = XVEC (orig, 4);
3547       copy_asm_constraints_vector = XVEC (copy, 4);
3548     }
3549
3550   return copy;
3551 }
3552
3553 /* Create a new copy of an rtx.
3554    This function differs from copy_rtx in that it handles SCRATCHes and
3555    ASM_OPERANDs properly.
3556    INSN doesn't really have to be a full INSN; it could be just the
3557    pattern.  */
3558 rtx
3559 copy_insn (insn)
3560      rtx insn;
3561 {
3562   copy_insn_n_scratches = 0;
3563   orig_asm_operands_vector = 0;
3564   orig_asm_constraints_vector = 0;
3565   copy_asm_operands_vector = 0;
3566   copy_asm_constraints_vector = 0;
3567   return copy_insn_1 (insn);
3568 }
3569
3570 /* Initialize data structures and variables in this file
3571    before generating rtl for each function.  */
3572
3573 void
3574 init_emit ()
3575 {
3576   struct function *f = current_function;
3577
3578   f->emit = (struct emit_status *) xmalloc (sizeof (struct emit_status));
3579   first_insn = NULL;
3580   last_insn = NULL;
3581   seq_rtl_expr = NULL;
3582   cur_insn_uid = 1;
3583   reg_rtx_no = LAST_VIRTUAL_REGISTER + 1;
3584   last_linenum = 0;
3585   last_filename = 0;
3586   first_label_num = label_num;
3587   last_label_num = 0;
3588   seq_stack = NULL;
3589
3590   clear_emit_caches ();
3591
3592   /* Init the tables that describe all the pseudo regs.  */
3593
3594   f->emit->regno_pointer_flag_length = LAST_VIRTUAL_REGISTER + 101;
3595
3596   f->emit->regno_pointer_flag 
3597     = (char *) xcalloc (f->emit->regno_pointer_flag_length, sizeof (char));
3598
3599   f->emit->regno_pointer_align
3600     = (char *) xcalloc (f->emit->regno_pointer_flag_length,
3601                         sizeof (char));
3602
3603   regno_reg_rtx 
3604     = (rtx *) xcalloc (f->emit->regno_pointer_flag_length * sizeof (rtx),
3605                        sizeof (rtx));
3606
3607   /* Put copies of all the virtual register rtx into regno_reg_rtx.  */
3608   init_virtual_regs (f->emit);
3609
3610   /* Indicate that the virtual registers and stack locations are
3611      all pointers.  */
3612   REGNO_POINTER_FLAG (STACK_POINTER_REGNUM) = 1;
3613   REGNO_POINTER_FLAG (FRAME_POINTER_REGNUM) = 1;
3614   REGNO_POINTER_FLAG (HARD_FRAME_POINTER_REGNUM) = 1;
3615   REGNO_POINTER_FLAG (ARG_POINTER_REGNUM) = 1;
3616
3617   REGNO_POINTER_FLAG (VIRTUAL_INCOMING_ARGS_REGNUM) = 1;
3618   REGNO_POINTER_FLAG (VIRTUAL_STACK_VARS_REGNUM) = 1;
3619   REGNO_POINTER_FLAG (VIRTUAL_STACK_DYNAMIC_REGNUM) = 1;
3620   REGNO_POINTER_FLAG (VIRTUAL_OUTGOING_ARGS_REGNUM) = 1;
3621   REGNO_POINTER_FLAG (VIRTUAL_CFA_REGNUM) = 1;
3622
3623 #ifdef STACK_BOUNDARY
3624   REGNO_POINTER_ALIGN (STACK_POINTER_REGNUM) = STACK_BOUNDARY / BITS_PER_UNIT;
3625   REGNO_POINTER_ALIGN (FRAME_POINTER_REGNUM) = STACK_BOUNDARY / BITS_PER_UNIT;
3626   REGNO_POINTER_ALIGN (HARD_FRAME_POINTER_REGNUM)
3627     = STACK_BOUNDARY / BITS_PER_UNIT;
3628   REGNO_POINTER_ALIGN (ARG_POINTER_REGNUM) = STACK_BOUNDARY / BITS_PER_UNIT;
3629
3630   REGNO_POINTER_ALIGN (VIRTUAL_INCOMING_ARGS_REGNUM)
3631     = STACK_BOUNDARY / BITS_PER_UNIT;
3632   REGNO_POINTER_ALIGN (VIRTUAL_STACK_VARS_REGNUM)
3633     = STACK_BOUNDARY / BITS_PER_UNIT;
3634   REGNO_POINTER_ALIGN (VIRTUAL_STACK_DYNAMIC_REGNUM)
3635     = STACK_BOUNDARY / BITS_PER_UNIT;
3636   REGNO_POINTER_ALIGN (VIRTUAL_OUTGOING_ARGS_REGNUM)
3637     = STACK_BOUNDARY / BITS_PER_UNIT;
3638   REGNO_POINTER_ALIGN (VIRTUAL_CFA_REGNUM) = UNITS_PER_WORD;
3639 #endif
3640
3641 #ifdef INIT_EXPANDERS
3642   INIT_EXPANDERS;
3643 #endif
3644 }
3645
3646 /* Mark SS for GC.  */
3647
3648 static void
3649 mark_sequence_stack (ss)
3650      struct sequence_stack *ss;
3651 {
3652   while (ss)
3653     {
3654       ggc_mark_rtx (ss->first);
3655       ggc_mark_tree (ss->sequence_rtl_expr);
3656       ss = ss->next;
3657     }
3658 }
3659
3660 /* Mark ES for GC.  */
3661
3662 void
3663 mark_emit_status (es)
3664      struct emit_status *es;
3665 {
3666   rtx *r;
3667   int i;
3668
3669   if (es == 0)
3670     return;
3671
3672   for (i = es->regno_pointer_flag_length, r = es->x_regno_reg_rtx;
3673        i > 0; --i, ++r)
3674     ggc_mark_rtx (*r);
3675
3676   mark_sequence_stack (es->sequence_stack);
3677   ggc_mark_tree (es->sequence_rtl_expr);
3678   ggc_mark_rtx (es->x_first_insn);
3679 }
3680
3681 /* Create some permanent unique rtl objects shared between all functions.
3682    LINE_NUMBERS is nonzero if line numbers are to be generated.  */
3683
3684 void
3685 init_emit_once (line_numbers)
3686      int line_numbers;
3687 {
3688   int i;
3689   enum machine_mode mode;
3690   enum machine_mode double_mode;
3691
3692   no_line_numbers = ! line_numbers;
3693
3694   /* Compute the word and byte modes.  */
3695
3696   byte_mode = VOIDmode;
3697   word_mode = VOIDmode;
3698   double_mode = VOIDmode;
3699
3700   for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
3701        mode = GET_MODE_WIDER_MODE (mode))
3702     {
3703       if (GET_MODE_BITSIZE (mode) == BITS_PER_UNIT
3704           && byte_mode == VOIDmode)
3705         byte_mode = mode;
3706
3707       if (GET_MODE_BITSIZE (mode) == BITS_PER_WORD
3708           && word_mode == VOIDmode)
3709         word_mode = mode;
3710     }
3711
3712 #ifndef DOUBLE_TYPE_SIZE
3713 #define DOUBLE_TYPE_SIZE (BITS_PER_WORD * 2)
3714 #endif
3715
3716   for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
3717        mode = GET_MODE_WIDER_MODE (mode))
3718     {
3719       if (GET_MODE_BITSIZE (mode) == DOUBLE_TYPE_SIZE
3720           && double_mode == VOIDmode)
3721         double_mode = mode;
3722     }
3723
3724   ptr_mode = mode_for_size (POINTER_SIZE, GET_MODE_CLASS (Pmode), 0);
3725
3726   /* Assign register numbers to the globally defined register rtx.
3727      This must be done at runtime because the register number field
3728      is in a union and some compilers can't initialize unions.  */
3729
3730   pc_rtx = gen_rtx (PC, VOIDmode);
3731   cc0_rtx = gen_rtx (CC0, VOIDmode);
3732   stack_pointer_rtx = gen_rtx_raw_REG (Pmode, STACK_POINTER_REGNUM);
3733   frame_pointer_rtx = gen_rtx_raw_REG (Pmode, FRAME_POINTER_REGNUM);
3734   if (hard_frame_pointer_rtx == 0)
3735     hard_frame_pointer_rtx = gen_rtx_raw_REG (Pmode, 
3736                                               HARD_FRAME_POINTER_REGNUM);
3737   if (arg_pointer_rtx == 0)
3738     arg_pointer_rtx = gen_rtx_raw_REG (Pmode, ARG_POINTER_REGNUM);
3739   virtual_incoming_args_rtx = 
3740     gen_rtx_raw_REG (Pmode, VIRTUAL_INCOMING_ARGS_REGNUM);
3741   virtual_stack_vars_rtx = 
3742     gen_rtx_raw_REG (Pmode, VIRTUAL_STACK_VARS_REGNUM);
3743   virtual_stack_dynamic_rtx = 
3744     gen_rtx_raw_REG (Pmode, VIRTUAL_STACK_DYNAMIC_REGNUM);
3745   virtual_outgoing_args_rtx = 
3746     gen_rtx_raw_REG (Pmode, VIRTUAL_OUTGOING_ARGS_REGNUM); 
3747   virtual_cfa_rtx = gen_rtx_raw_REG (Pmode, VIRTUAL_CFA_REGNUM);
3748
3749   /* These rtx must be roots if GC is enabled.  */
3750   if (ggc_p)
3751     ggc_add_rtx_root (global_rtl, GR_MAX);
3752
3753 #ifdef INIT_EXPANDERS
3754   /* This is to initialize save_machine_status and restore_machine_status before
3755      the first call to push_function_context_to.  This is needed by the Chill
3756      front end which calls push_function_context_to before the first cal to
3757      init_function_start.  */
3758   INIT_EXPANDERS;
3759 #endif
3760
3761   /* Create the unique rtx's for certain rtx codes and operand values.  */
3762
3763   /* Don't use gen_rtx here since gen_rtx in this case
3764      tries to use these variables.  */
3765   for (i = - MAX_SAVED_CONST_INT; i <= MAX_SAVED_CONST_INT; i++)
3766     const_int_rtx[i + MAX_SAVED_CONST_INT] = 
3767       gen_rtx_raw_CONST_INT (VOIDmode, i);
3768   if (ggc_p)
3769     ggc_add_rtx_root (const_int_rtx, 2 * MAX_SAVED_CONST_INT + 1);
3770
3771   if (STORE_FLAG_VALUE >= - MAX_SAVED_CONST_INT
3772       && STORE_FLAG_VALUE <= MAX_SAVED_CONST_INT)
3773     const_true_rtx = const_int_rtx[STORE_FLAG_VALUE + MAX_SAVED_CONST_INT];
3774   else
3775     const_true_rtx = gen_rtx_CONST_INT (VOIDmode, STORE_FLAG_VALUE);
3776
3777   dconst0 = REAL_VALUE_ATOF ("0", double_mode);
3778   dconst1 = REAL_VALUE_ATOF ("1", double_mode);
3779   dconst2 = REAL_VALUE_ATOF ("2", double_mode);
3780   dconstm1 = REAL_VALUE_ATOF ("-1", double_mode);
3781
3782   for (i = 0; i <= 2; i++)
3783     {
3784       for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
3785            mode = GET_MODE_WIDER_MODE (mode))
3786         {
3787           rtx tem = rtx_alloc (CONST_DOUBLE);
3788           union real_extract u;
3789
3790           bzero ((char *) &u, sizeof u);  /* Zero any holes in a structure.  */
3791           u.d = i == 0 ? dconst0 : i == 1 ? dconst1 : dconst2;
3792
3793           bcopy ((char *) &u, (char *) &CONST_DOUBLE_LOW (tem), sizeof u);
3794           CONST_DOUBLE_MEM (tem) = cc0_rtx;
3795           PUT_MODE (tem, mode);
3796
3797           const_tiny_rtx[i][(int) mode] = tem;
3798         }
3799
3800       const_tiny_rtx[i][(int) VOIDmode] = GEN_INT (i);
3801
3802       for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
3803            mode = GET_MODE_WIDER_MODE (mode))
3804         const_tiny_rtx[i][(int) mode] = GEN_INT (i);
3805
3806       for (mode = GET_CLASS_NARROWEST_MODE (MODE_PARTIAL_INT);
3807            mode != VOIDmode;
3808            mode = GET_MODE_WIDER_MODE (mode))
3809         const_tiny_rtx[i][(int) mode] = GEN_INT (i);
3810     }
3811
3812   for (mode = CCmode; mode < MAX_MACHINE_MODE; ++mode)
3813     if (GET_MODE_CLASS (mode) == MODE_CC)
3814       const_tiny_rtx[0][(int) mode] = const0_rtx;
3815
3816   ggc_add_rtx_root (&const_tiny_rtx[0][0], sizeof(const_tiny_rtx)/sizeof(rtx));
3817   ggc_add_rtx_root (&const_true_rtx, 1);
3818
3819 #ifdef RETURN_ADDRESS_POINTER_REGNUM
3820   return_address_pointer_rtx
3821     = gen_rtx_raw_REG (Pmode, RETURN_ADDRESS_POINTER_REGNUM);
3822 #endif
3823
3824 #ifdef STRUCT_VALUE
3825   struct_value_rtx = STRUCT_VALUE;
3826 #else
3827   struct_value_rtx = gen_rtx_REG (Pmode, STRUCT_VALUE_REGNUM);
3828 #endif
3829
3830 #ifdef STRUCT_VALUE_INCOMING
3831   struct_value_incoming_rtx = STRUCT_VALUE_INCOMING;
3832 #else
3833 #ifdef STRUCT_VALUE_INCOMING_REGNUM
3834   struct_value_incoming_rtx
3835     = gen_rtx_REG (Pmode, STRUCT_VALUE_INCOMING_REGNUM);
3836 #else
3837   struct_value_incoming_rtx = struct_value_rtx;
3838 #endif
3839 #endif
3840
3841 #ifdef STATIC_CHAIN_REGNUM
3842   static_chain_rtx = gen_rtx_REG (Pmode, STATIC_CHAIN_REGNUM);
3843
3844 #ifdef STATIC_CHAIN_INCOMING_REGNUM
3845   if (STATIC_CHAIN_INCOMING_REGNUM != STATIC_CHAIN_REGNUM)
3846     static_chain_incoming_rtx
3847       = gen_rtx_REG (Pmode, STATIC_CHAIN_INCOMING_REGNUM);
3848   else
3849 #endif
3850     static_chain_incoming_rtx = static_chain_rtx;
3851 #endif
3852
3853 #ifdef STATIC_CHAIN
3854   static_chain_rtx = STATIC_CHAIN;
3855
3856 #ifdef STATIC_CHAIN_INCOMING
3857   static_chain_incoming_rtx = STATIC_CHAIN_INCOMING;
3858 #else
3859   static_chain_incoming_rtx = static_chain_rtx;
3860 #endif
3861 #endif
3862
3863 #ifdef PIC_OFFSET_TABLE_REGNUM
3864   pic_offset_table_rtx = gen_rtx_REG (Pmode, PIC_OFFSET_TABLE_REGNUM);
3865 #endif
3866
3867   ggc_add_rtx_root (&pic_offset_table_rtx, 1);
3868   ggc_add_rtx_root (&struct_value_rtx, 1);
3869   ggc_add_rtx_root (&struct_value_incoming_rtx, 1);
3870   ggc_add_rtx_root (&static_chain_rtx, 1);
3871   ggc_add_rtx_root (&static_chain_incoming_rtx, 1);
3872   ggc_add_rtx_root (&return_address_pointer_rtx, 1);
3873 }
3874 \f
3875 /* Query and clear/ restore no_line_numbers.  This is used by the
3876    switch / case handling in stmt.c to give proper line numbers in
3877    warnings about unreachable code.  */
3878
3879 int
3880 force_line_numbers ()
3881 {
3882   int old = no_line_numbers;
3883
3884   no_line_numbers = 0;
3885   if (old)
3886     force_next_line_note ();
3887   return old;
3888 }
3889
3890 void
3891 restore_line_number_status (old_value)
3892      int old_value;
3893 {
3894   no_line_numbers = old_value;
3895 }