]> gcc.gnu.org Git - gcc.git/blob - gcc/emit-rtl.c
*** empty log message ***
[gcc.git] / gcc / emit-rtl.c
1 /* Emit RTL for the GNU C-Compiler expander.
2 Copyright (C) 1987, 1988, 1992 Free Software Foundation, Inc.
3
4 This file is part of GNU CC.
5
6 GNU CC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
10
11 GNU CC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with GNU CC; see the file COPYING. If not, write to
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */
19
20
21 /* Middle-to-low level generation of rtx code and insns.
22
23 This file contains the functions `gen_rtx', `gen_reg_rtx'
24 and `gen_label_rtx' that are the usual ways of creating rtl
25 expressions for most purposes.
26
27 It also has the functions for creating insns and linking
28 them in the doubly-linked chain.
29
30 The patterns of the insns are created by machine-dependent
31 routines in insn-emit.c, which is generated automatically from
32 the machine description. These routines use `gen_rtx' to make
33 the individual rtx's of the pattern; what is machine dependent
34 is the kind of rtx's they make and what arguments they use. */
35
36 #include "config.h"
37 #include <stdio.h>
38 #include "gvarargs.h"
39 #include "rtl.h"
40 #include "flags.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "regs.h"
44 #include "insn-config.h"
45 #include "real.h"
46
47 /* This is reset to LAST_VIRTUAL_REGISTER + 1 at the start of each function.
48 After rtl generation, it is 1 plus the largest register number used. */
49
50 int reg_rtx_no = LAST_VIRTUAL_REGISTER + 1;
51
52 /* This is *not* reset after each function. It gives each CODE_LABEL
53 in the entire compilation a unique label number. */
54
55 static int label_num = 1;
56
57 /* Lowest label number in current function. */
58
59 static int first_label_num;
60
61 /* Highest label number in current function.
62 Zero means use the value of label_num instead.
63 This is nonzero only when belatedly compiling an inline function. */
64
65 static int last_label_num;
66
67 /* Value label_num had when set_new_first_and_last_label_number was called.
68 If label_num has not changed since then, last_label_num is valid. */
69
70 static int base_label_num;
71
72 /* Nonzero means do not generate NOTEs for source line numbers. */
73
74 static int no_line_numbers;
75
76 /* Commonly used rtx's, so that we only need space for one copy.
77 These are initialized once for the entire compilation.
78 All of these except perhaps the floating-point CONST_DOUBLEs
79 are unique; no other rtx-object will be equal to any of these. */
80
81 rtx pc_rtx; /* (PC) */
82 rtx cc0_rtx; /* (CC0) */
83 rtx cc1_rtx; /* (CC1) (not actually used nowadays) */
84 rtx const0_rtx; /* (CONST_INT 0) */
85 rtx const1_rtx; /* (CONST_INT 1) */
86 rtx const2_rtx; /* (CONST_INT 2) */
87 rtx constm1_rtx; /* (CONST_INT -1) */
88 rtx const_true_rtx; /* (CONST_INT STORE_FLAG_VALUE) */
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 REAL_VALUE_TYPE dconst0;
97 REAL_VALUE_TYPE dconst1;
98 REAL_VALUE_TYPE dconst2;
99 REAL_VALUE_TYPE dconstm1;
100
101 /* All references to the following fixed hard registers go through
102 these unique rtl objects. On machines where the frame-pointer and
103 arg-pointer are the same register, they use the same unique object.
104
105 After register allocation, other rtl objects which used to be pseudo-regs
106 may be clobbered to refer to the frame-pointer register.
107 But references that were originally to the frame-pointer can be
108 distinguished from the others because they contain frame_pointer_rtx.
109
110 In an inline procedure, the stack and frame pointer rtxs may not be
111 used for anything else. */
112 rtx stack_pointer_rtx; /* (REG:Pmode STACK_POINTER_REGNUM) */
113 rtx frame_pointer_rtx; /* (REG:Pmode FRAME_POINTER_REGNUM) */
114 rtx arg_pointer_rtx; /* (REG:Pmode ARG_POINTER_REGNUM) */
115 rtx struct_value_rtx; /* (REG:Pmode STRUCT_VALUE_REGNUM) */
116 rtx struct_value_incoming_rtx; /* (REG:Pmode STRUCT_VALUE_INCOMING_REGNUM) */
117 rtx static_chain_rtx; /* (REG:Pmode STATIC_CHAIN_REGNUM) */
118 rtx static_chain_incoming_rtx; /* (REG:Pmode STATIC_CHAIN_INCOMING_REGNUM) */
119 rtx pic_offset_table_rtx; /* (REG:Pmode PIC_OFFSET_TABLE_REGNUM) */
120
121 rtx virtual_incoming_args_rtx; /* (REG:Pmode VIRTUAL_INCOMING_ARGS_REGNUM) */
122 rtx virtual_stack_vars_rtx; /* (REG:Pmode VIRTUAL_STACK_VARS_REGNUM) */
123 rtx virtual_stack_dynamic_rtx; /* (REG:Pmode VIRTUAL_STACK_DYNAMIC_REGNUM) */
124 rtx virtual_outgoing_args_rtx; /* (REG:Pmode VIRTUAL_OUTGOING_ARGS_REGNUM) */
125
126 /* We make one copy of (const_int C) where C is in
127 [- MAX_SAVED_CONST_INT, MAX_SAVED_CONST_INT]
128 to save space during the compilation and simplify comparisons of
129 integers. */
130
131 #define MAX_SAVED_CONST_INT 64
132
133 static rtx const_int_rtx[MAX_SAVED_CONST_INT * 2 + 1];
134
135 /* The ends of the doubly-linked chain of rtl for the current function.
136 Both are reset to null at the start of rtl generation for the function.
137
138 start_sequence saves both of these on `sequence_stack' and then
139 starts a new, nested sequence of insns. */
140
141 static rtx first_insn = NULL;
142 static rtx last_insn = NULL;
143
144 /* INSN_UID for next insn emitted.
145 Reset to 1 for each function compiled. */
146
147 static int cur_insn_uid = 1;
148
149 /* Line number and source file of the last line-number NOTE emitted.
150 This is used to avoid generating duplicates. */
151
152 static int last_linenum = 0;
153 static char *last_filename = 0;
154
155 /* A vector indexed by pseudo reg number. The allocated length
156 of this vector is regno_pointer_flag_length. Since this
157 vector is needed during the expansion phase when the total
158 number of registers in the function is not yet known,
159 it is copied and made bigger when necessary. */
160
161 char *regno_pointer_flag;
162 int regno_pointer_flag_length;
163
164 /* Indexed by pseudo register number, gives the rtx for that pseudo.
165 Allocated in parallel with regno_pointer_flag. */
166
167 rtx *regno_reg_rtx;
168
169 /* Stack of pending (incomplete) sequences saved by `start_sequence'.
170 Each element describes one pending sequence.
171 The main insn-chain is saved in the last element of the chain,
172 unless the chain is empty. */
173
174 struct sequence_stack *sequence_stack;
175
176 /* start_sequence and gen_sequence can make a lot of rtx expressions which are
177 shortly thrown away. We use two mechanisms to prevent this waste:
178
179 First, we keep a list of the expressions used to represent the sequence
180 stack in sequence_element_free_list.
181
182 Second, for sizes up to 5 elements, we keep a SEQUENCE and its associated
183 rtvec for use by gen_sequence. One entry for each size is sufficient
184 because most cases are calls to gen_sequence followed by immediately
185 emitting the SEQUENCE. Reuse is safe since emitting a sequence is
186 destructive on the insn in it anyway and hence can't be redone.
187
188 We do not bother to save this cached data over nested function calls.
189 Instead, we just reinitialize them. */
190
191 #define SEQUENCE_RESULT_SIZE 5
192
193 static struct sequence_stack *sequence_element_free_list;
194 static rtx sequence_result[SEQUENCE_RESULT_SIZE];
195
196 extern int rtx_equal_function_value_matters;
197
198 /* Filename and line number of last line-number note,
199 whether we actually emitted it or not. */
200 extern char *emit_filename;
201 extern int emit_lineno;
202
203 rtx change_address ();
204 void init_emit ();
205 \f
206 /* rtx gen_rtx (code, mode, [element1, ..., elementn])
207 **
208 ** This routine generates an RTX of the size specified by
209 ** <code>, which is an RTX code. The RTX structure is initialized
210 ** from the arguments <element1> through <elementn>, which are
211 ** interpreted according to the specific RTX type's format. The
212 ** special machine mode associated with the rtx (if any) is specified
213 ** in <mode>.
214 **
215 ** gen_rtx() can be invoked in a way which resembles the lisp-like
216 ** rtx it will generate. For example, the following rtx structure:
217 **
218 ** (plus:QI (mem:QI (reg:SI 1))
219 ** (mem:QI (plusw:SI (reg:SI 2) (reg:SI 3))))
220 **
221 ** ...would be generated by the following C code:
222 **
223 ** gen_rtx (PLUS, QImode,
224 ** gen_rtx (MEM, QImode,
225 ** gen_rtx (REG, SImode, 1)),
226 ** gen_rtx (MEM, QImode,
227 ** gen_rtx (PLUS, SImode,
228 ** gen_rtx (REG, SImode, 2),
229 ** gen_rtx (REG, SImode, 3)))),
230 */
231
232 /*VARARGS2*/
233 rtx
234 gen_rtx (va_alist)
235 va_dcl
236 {
237 va_list p;
238 enum rtx_code code;
239 enum machine_mode mode;
240 register int i; /* Array indices... */
241 register char *fmt; /* Current rtx's format... */
242 register rtx rt_val; /* RTX to return to caller... */
243
244 va_start (p);
245 code = va_arg (p, enum rtx_code);
246 mode = va_arg (p, enum machine_mode);
247
248 if (code == CONST_INT)
249 {
250 int arg = va_arg (p, int);
251
252 if (arg >= - MAX_SAVED_CONST_INT && arg <= MAX_SAVED_CONST_INT)
253 return const_int_rtx[arg + MAX_SAVED_CONST_INT];
254
255 if (const_true_rtx && arg == STORE_FLAG_VALUE)
256 return const_true_rtx;
257
258 rt_val = rtx_alloc (code);
259 INTVAL (rt_val) = arg;
260 }
261 else if (code == REG)
262 {
263 int regno = va_arg (p, int);
264
265 /* In case the MD file explicitly references the frame pointer, have
266 all such references point to the same frame pointer. This is used
267 during frame pointer elimination to distinguish the explicit
268 references to these registers from pseudos that happened to be
269 assigned to them.
270
271 If we have eliminated the frame pointer or arg pointer, we will
272 be using it as a normal register, for example as a spill register.
273 In such cases, we might be accessing it in a mode that is not
274 Pmode and therefore cannot use the pre-allocated rtx. */
275
276 if (frame_pointer_rtx && regno == FRAME_POINTER_REGNUM && mode == Pmode)
277 return frame_pointer_rtx;
278 #if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
279 if (arg_pointer_rtx && regno == ARG_POINTER_REGNUM && mode == Pmode)
280 return arg_pointer_rtx;
281 #endif
282 if (stack_pointer_rtx && regno == STACK_POINTER_REGNUM && mode == Pmode)
283 return stack_pointer_rtx;
284 else
285 {
286 rt_val = rtx_alloc (code);
287 rt_val->mode = mode;
288 REGNO (rt_val) = regno;
289 return rt_val;
290 }
291 }
292 else
293 {
294 rt_val = rtx_alloc (code); /* Allocate the storage space. */
295 rt_val->mode = mode; /* Store the machine mode... */
296
297 fmt = GET_RTX_FORMAT (code); /* Find the right format... */
298 for (i = 0; i < GET_RTX_LENGTH (code); i++)
299 {
300 switch (*fmt++)
301 {
302 case '0': /* Unused field. */
303 break;
304
305 case 'i': /* An integer? */
306 XINT (rt_val, i) = va_arg (p, int);
307 break;
308
309 case 's': /* A string? */
310 XSTR (rt_val, i) = va_arg (p, char *);
311 break;
312
313 case 'e': /* An expression? */
314 case 'u': /* An insn? Same except when printing. */
315 XEXP (rt_val, i) = va_arg (p, rtx);
316 break;
317
318 case 'E': /* An RTX vector? */
319 XVEC (rt_val, i) = va_arg (p, rtvec);
320 break;
321
322 default:
323 abort();
324 }
325 }
326 }
327 va_end (p);
328 return rt_val; /* Return the new RTX... */
329 }
330
331 /* gen_rtvec (n, [rt1, ..., rtn])
332 **
333 ** This routine creates an rtvec and stores within it the
334 ** pointers to rtx's which are its arguments.
335 */
336
337 /*VARARGS1*/
338 rtvec
339 gen_rtvec (va_alist)
340 va_dcl
341 {
342 int n, i;
343 va_list p;
344 rtx *vector;
345
346 va_start (p);
347 n = va_arg (p, int);
348
349 if (n == 0)
350 return NULL_RTVEC; /* Don't allocate an empty rtvec... */
351
352 vector = (rtx *) alloca (n * sizeof (rtx));
353 for (i = 0; i < n; i++)
354 vector[i] = va_arg (p, rtx);
355 va_end (p);
356
357 return gen_rtvec_v (n, vector);
358 }
359
360 rtvec
361 gen_rtvec_v (n, argp)
362 int n;
363 rtx *argp;
364 {
365 register int i;
366 register rtvec rt_val;
367
368 if (n == 0)
369 return NULL_RTVEC; /* Don't allocate an empty rtvec... */
370
371 rt_val = rtvec_alloc (n); /* Allocate an rtvec... */
372
373 for (i = 0; i < n; i++)
374 rt_val->elem[i].rtx = *argp++;
375
376 return rt_val;
377 }
378 \f
379 /* Generate a REG rtx for a new pseudo register of mode MODE.
380 This pseudo is assigned the next sequential register number. */
381
382 rtx
383 gen_reg_rtx (mode)
384 enum machine_mode mode;
385 {
386 register rtx val;
387
388 /* Don't let anything called by or after reload create new registers
389 (actually, registers can't be created after flow, but this is a good
390 approximation). */
391
392 if (reload_in_progress || reload_completed)
393 abort ();
394
395 /* Make sure regno_pointer_flag and regno_reg_rtx are large
396 enough to have an element for this pseudo reg number. */
397
398 if (reg_rtx_no == regno_pointer_flag_length)
399 {
400 rtx *new1;
401 char *new =
402 (char *) oballoc (regno_pointer_flag_length * 2);
403 bzero (new, regno_pointer_flag_length * 2);
404 bcopy (regno_pointer_flag, new, regno_pointer_flag_length);
405 regno_pointer_flag = new;
406
407 new1 = (rtx *) oballoc (regno_pointer_flag_length * 2 * sizeof (rtx));
408 bzero (new1, regno_pointer_flag_length * 2 * sizeof (rtx));
409 bcopy (regno_reg_rtx, new1, regno_pointer_flag_length * sizeof (rtx));
410 regno_reg_rtx = new1;
411
412 regno_pointer_flag_length *= 2;
413 }
414
415 val = gen_rtx (REG, mode, reg_rtx_no);
416 regno_reg_rtx[reg_rtx_no++] = val;
417 return val;
418 }
419
420 /* Identify REG as a probable pointer register. */
421
422 void
423 mark_reg_pointer (reg)
424 rtx reg;
425 {
426 REGNO_POINTER_FLAG (REGNO (reg)) = 1;
427 }
428
429 /* Return 1 plus largest pseudo reg number used in the current function. */
430
431 int
432 max_reg_num ()
433 {
434 return reg_rtx_no;
435 }
436
437 /* Return 1 + the largest label number used so far in the current function. */
438
439 int
440 max_label_num ()
441 {
442 if (last_label_num && label_num == base_label_num)
443 return last_label_num;
444 return label_num;
445 }
446
447 /* Return first label number used in this function (if any were used). */
448
449 int
450 get_first_label_num ()
451 {
452 return first_label_num;
453 }
454 \f
455 /* Return a value representing some low-order bits of X, where the number
456 of low-order bits is given by MODE. Note that no conversion is done
457 between floating-point and fixed-point values, rather, the bit
458 representation is returned.
459
460 This function handles the cases in common between gen_lowpart, below,
461 and two variants in cse.c and combine.c. These are the cases that can
462 be safely handled at all points in the compilation.
463
464 If this is not a case we can handle, return 0. */
465
466 rtx
467 gen_lowpart_common (mode, x)
468 enum machine_mode mode;
469 register rtx x;
470 {
471 int word = 0;
472
473 if (GET_MODE (x) == mode)
474 return x;
475
476 /* MODE must occupy no more words than the mode of X. */
477 if (GET_MODE (x) != VOIDmode
478 && ((GET_MODE_SIZE (mode) + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD
479 > ((GET_MODE_SIZE (GET_MODE (x)) + (UNITS_PER_WORD - 1))
480 / UNITS_PER_WORD)))
481 return 0;
482
483 if (WORDS_BIG_ENDIAN && GET_MODE_SIZE (GET_MODE (x)) > UNITS_PER_WORD)
484 word = ((GET_MODE_SIZE (GET_MODE (x))
485 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD))
486 / UNITS_PER_WORD);
487
488 if ((GET_CODE (x) == ZERO_EXTEND || GET_CODE (x) == SIGN_EXTEND)
489 && GET_MODE_CLASS (mode) == MODE_INT)
490 {
491 /* If we are getting the low-order part of something that has been
492 sign- or zero-extended, we can either just use the object being
493 extended or make a narrower extension. If we want an even smaller
494 piece than the size of the object being extended, call ourselves
495 recursively.
496
497 This case is used mostly by combine and cse. */
498
499 if (GET_MODE (XEXP (x, 0)) == mode)
500 return XEXP (x, 0);
501 else if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (XEXP (x, 0))))
502 return gen_lowpart_common (mode, XEXP (x, 0));
503 else if (GET_MODE_SIZE (mode) < GET_MODE_SIZE (GET_MODE (x)))
504 return gen_rtx (GET_CODE (x), mode, XEXP (x, 0));
505 }
506 else if (GET_CODE (x) == SUBREG
507 && (GET_MODE_SIZE (mode) <= UNITS_PER_WORD
508 || GET_MODE_SIZE (mode) == GET_MODE_UNIT_SIZE (GET_MODE (x))))
509 return (GET_MODE (SUBREG_REG (x)) == mode && SUBREG_WORD (x) == 0
510 ? SUBREG_REG (x)
511 : gen_rtx (SUBREG, mode, SUBREG_REG (x), SUBREG_WORD (x)));
512 else if (GET_CODE (x) == REG)
513 {
514 /* If the register is not valid for MODE, return 0. If we don't
515 do this, there is no way to fix up the resulting REG later. */
516 if (REGNO (x) < FIRST_PSEUDO_REGISTER
517 && ! HARD_REGNO_MODE_OK (REGNO (x) + word, mode))
518 return 0;
519 else if (REGNO (x) < FIRST_PSEUDO_REGISTER
520 /* integrate.c can't handle parts of a return value register. */
521 && (! REG_FUNCTION_VALUE_P (x)
522 || ! rtx_equal_function_value_matters))
523 return gen_rtx (REG, mode, REGNO (x) + word);
524 else
525 return gen_rtx (SUBREG, mode, x, word);
526 }
527
528 /* If X is a CONST_INT or a CONST_DOUBLE, extract the appropriate bits
529 from the low-order part of the constant. */
530 else if (GET_MODE_CLASS (mode) == MODE_INT && GET_MODE (x) == VOIDmode
531 && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE))
532 {
533 /* If MODE is twice the host word size, X is already the desired
534 representation. Otherwise, if MODE is wider than a word, we can't
535 do this. If MODE is exactly a word, return just one CONST_INT.
536 If MODE is smaller than a word, clear the bits that don't belong
537 in our mode, unless they and our sign bit are all one. So we get
538 either a reasonable negative value or a reasonable unsigned value
539 for this mode. */
540
541 if (GET_MODE_BITSIZE (mode) == 2 * HOST_BITS_PER_INT)
542 return x;
543 else if (GET_MODE_BITSIZE (mode) > HOST_BITS_PER_INT)
544 return 0;
545 else if (GET_MODE_BITSIZE (mode) == HOST_BITS_PER_INT)
546 return (GET_CODE (x) == CONST_INT ? x
547 : gen_rtx (CONST_INT, VOIDmode, CONST_DOUBLE_LOW (x)));
548 else
549 {
550 /* MODE must be narrower than HOST_BITS_PER_INT. */
551 int width = GET_MODE_BITSIZE (mode);
552 int val = (GET_CODE (x) == CONST_INT ? INTVAL (x)
553 : CONST_DOUBLE_LOW (x));
554
555 if (((val & ((-1) << (width - 1))) != ((-1) << (width - 1))))
556 val &= (1 << width) - 1;
557
558 return (GET_CODE (x) == CONST_INT && INTVAL (x) == val ? x
559 : gen_rtx (CONST_INT, VOIDmode, val));
560 }
561 }
562
563 /* If X is an integral constant but we want it in floating-point, it
564 must be the case that we have a union of an integer and a floating-point
565 value. If the machine-parameters allow it, simulate that union here
566 and return the result. */
567
568 else if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
569 && HOST_BITS_PER_INT == BITS_PER_WORD
570 && GET_MODE_CLASS (mode) == MODE_FLOAT
571 && (GET_CODE (x) == CONST_INT || GET_CODE (x) == CONST_DOUBLE)
572 && GET_MODE (x) == VOIDmode
573 && sizeof (double) * HOST_BITS_PER_CHAR == 2 * HOST_BITS_PER_INT)
574 {
575 union {int i[2]; double d; } u;
576 int low, high;
577
578 if (GET_CODE (x) == CONST_INT)
579 low = INTVAL (x), high = low >> (HOST_BITS_PER_INT -1);
580 else
581 low = CONST_DOUBLE_LOW (x), high = CONST_DOUBLE_HIGH (x);
582
583 #ifdef HOST_WORDS_BIG_ENDIAN
584 u.i[0] = high, u.i[1] = low;
585 #else
586 u.i[0] = low, u.i[1] = high;
587 #endif
588
589 return immed_real_const_1 (u.d, mode);
590 }
591
592 /* Similarly, if this is converting a floating-point value into a
593 two-word integer, we can do this one word at a time and make an
594 integer. Only do this is the host and target parameters are
595 compatible. */
596
597 else if (HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
598 && HOST_BITS_PER_INT == BITS_PER_WORD
599 && GET_MODE_CLASS (mode) == MODE_INT
600 && GET_CODE (x) == CONST_DOUBLE
601 && GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT
602 && GET_MODE_BITSIZE (mode) == 2 * BITS_PER_WORD)
603 {
604 rtx lowpart = operand_subword (x, WORDS_BIG_ENDIAN, 0, GET_MODE (x));
605 rtx highpart = operand_subword (x, ! WORDS_BIG_ENDIAN, 0, GET_MODE (x));
606
607 if (lowpart && GET_CODE (lowpart) == CONST_INT
608 && highpart && GET_CODE (highpart) == CONST_INT)
609 return immed_double_const (INTVAL (lowpart), INTVAL (highpart), mode);
610 }
611
612 /* Otherwise, we can't do this. */
613 return 0;
614 }
615 \f
616 /* Assuming that X is an rtx (e.g., MEM, REG or SUBREG) for a value,
617 return an rtx (MEM, SUBREG, or CONST_INT) that refers to the
618 least-significant part of X.
619 MODE specifies how big a part of X to return;
620 it usually should not be larger than a word.
621 If X is a MEM whose address is a QUEUED, the value may be so also. */
622
623 rtx
624 gen_lowpart (mode, x)
625 enum machine_mode mode;
626 register rtx x;
627 {
628 rtx result = gen_lowpart_common (mode, x);
629
630 if (result)
631 return result;
632 else if (GET_CODE (x) == MEM)
633 {
634 /* The only additional case we can do is MEM. */
635 register int offset = 0;
636 if (WORDS_BIG_ENDIAN)
637 offset = (MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD)
638 - MAX (GET_MODE_SIZE (mode), UNITS_PER_WORD));
639
640 if (BYTES_BIG_ENDIAN)
641 /* Adjust the address so that the address-after-the-data
642 is unchanged. */
643 offset -= (MIN (UNITS_PER_WORD, GET_MODE_SIZE (mode))
644 - MIN (UNITS_PER_WORD, GET_MODE_SIZE (GET_MODE (x))));
645
646 return change_address (x, mode, plus_constant (XEXP (x, 0), offset));
647 }
648 else
649 abort ();
650 }
651
652 /* Return 1 iff X, assumed to be a SUBREG,
653 refers to the least significant part of its containing reg.
654 If X is not a SUBREG, always return 1 (it is its own low part!). */
655
656 int
657 subreg_lowpart_p (x)
658 rtx x;
659 {
660 if (GET_CODE (x) != SUBREG)
661 return 1;
662
663 if (WORDS_BIG_ENDIAN
664 && GET_MODE_SIZE (GET_MODE (SUBREG_REG (x))) > UNITS_PER_WORD)
665 return (SUBREG_WORD (x)
666 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (x)))
667 - MAX (GET_MODE_SIZE (GET_MODE (x)), UNITS_PER_WORD))
668 / UNITS_PER_WORD));
669
670 return SUBREG_WORD (x) == 0;
671 }
672 \f
673 /* Return subword I of operand OP.
674 The word number, I, is interpreted as the word number starting at the
675 low-order address. Word 0 is the low-order word if not WORDS_BIG_ENDIAN,
676 otherwise it is the high-order word.
677
678 If we cannot extract the required word, we return zero. Otherwise, an
679 rtx corresponding to the requested word will be returned.
680
681 VALIDATE_ADDRESS is nonzero if the address should be validated. Before
682 reload has completed, a valid address will always be returned. After
683 reload, if a valid address cannot be returned, we return zero.
684
685 If VALIDATE_ADDRESS is zero, we simply form the required address; validating
686 it is the responsibility of the caller.
687
688 MODE is the mode of OP in case it is a CONST_INT. */
689
690 rtx
691 operand_subword (op, i, validate_address, mode)
692 rtx op;
693 int i;
694 int validate_address;
695 enum machine_mode mode;
696 {
697 int val;
698 int size_ratio = HOST_BITS_PER_INT / BITS_PER_WORD;
699
700 if (mode == VOIDmode)
701 mode = GET_MODE (op);
702
703 if (mode == VOIDmode)
704 abort ();
705
706 /* If OP is narrower than a word or if we want a word outside OP, fail. */
707 if (mode != BLKmode
708 && (GET_MODE_SIZE (mode) < UNITS_PER_WORD
709 || (i + 1) * UNITS_PER_WORD > GET_MODE_SIZE (mode)))
710 return 0;
711
712 /* If OP is already an integer word, return it. */
713 if (GET_MODE_CLASS (mode) == MODE_INT
714 && GET_MODE_SIZE (mode) == UNITS_PER_WORD)
715 return op;
716
717 /* If OP is a REG or SUBREG, we can handle it very simply. */
718 if (GET_CODE (op) == REG)
719 {
720 /* If the register is not valid for MODE, return 0. If we don't
721 do this, there is no way to fix up the resulting REG later. */
722 if (REGNO (op) < FIRST_PSEUDO_REGISTER
723 && ! HARD_REGNO_MODE_OK (REGNO (op) + i, word_mode))
724 return 0;
725 else if (REGNO (op) >= FIRST_PSEUDO_REGISTER
726 || (REG_FUNCTION_VALUE_P (op)
727 && rtx_equal_function_value_matters))
728 return gen_rtx (SUBREG, word_mode, op, i);
729 else
730 return gen_rtx (REG, word_mode, REGNO (op) + i);
731 }
732 else if (GET_CODE (op) == SUBREG)
733 return gen_rtx (SUBREG, word_mode, SUBREG_REG (op), i + SUBREG_WORD (op));
734
735 /* Form a new MEM at the requested address. */
736 if (GET_CODE (op) == MEM)
737 {
738 rtx addr = plus_constant (XEXP (op, 0), i * UNITS_PER_WORD);
739 rtx new;
740
741 if (validate_address)
742 {
743 if (reload_completed)
744 {
745 if (! strict_memory_address_p (word_mode, addr))
746 return 0;
747 }
748 else
749 addr = memory_address (word_mode, addr);
750 }
751
752 new = gen_rtx (MEM, word_mode, addr);
753
754 MEM_VOLATILE_P (new) = MEM_VOLATILE_P (op);
755 MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (op);
756 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (op);
757
758 return new;
759 }
760
761 /* The only remaining cases are when OP is a constant. If the host and
762 target floating formats are the same, handling two-word floating
763 constants are easy. */
764 if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
765 && HOST_BITS_PER_INT == BITS_PER_WORD)
766 || flag_pretend_float)
767 && GET_MODE_CLASS (mode) == MODE_FLOAT
768 && GET_MODE_SIZE (mode) == 2 * UNITS_PER_WORD
769 && GET_CODE (op) == CONST_DOUBLE)
770 return gen_rtx (CONST_INT, VOIDmode,
771 i ^ (WORDS_BIG_ENDIAN !=
772 /* The constant is stored in the host's word-ordering,
773 but we want to access it in the target's word-ordering. */
774 #ifdef HOST_WORDS_BIG_ENDIAN
775 1
776 #else
777 0
778 #endif
779 ) ? CONST_DOUBLE_HIGH (op) : CONST_DOUBLE_LOW (op));
780
781 /* Single word float is a little harder, since single- and double-word
782 values often do not have the same high-order bits. We have already
783 verified that we want the only defined word of the single-word value. */
784 if (((HOST_FLOAT_FORMAT == TARGET_FLOAT_FORMAT
785 && HOST_BITS_PER_INT == BITS_PER_WORD)
786 || flag_pretend_float)
787 && GET_MODE_CLASS (mode) == MODE_FLOAT
788 && GET_MODE_SIZE (mode) == UNITS_PER_WORD
789 && GET_CODE (op) == CONST_DOUBLE)
790 {
791 double d;
792 union {float f; int i; } u;
793
794 REAL_VALUE_FROM_CONST_DOUBLE (d, op);
795
796 u.f = d;
797 return gen_rtx (CONST_INT, VOIDmode, u.i);
798 }
799
800 /* The only remaining cases that we can handle are integers.
801 Convert to proper endianness now since these cases need it.
802 At this point, i == 0 means the low-order word.
803
804 Note that it must be that BITS_PER_WORD <= HOST_BITS_PER_INT.
805 This is because if it were greater, it could only have been two
806 times greater since we do not support making wider constants. In
807 that case, it MODE would have already been the proper size and
808 it would have been handled above. This means we do not have to
809 worry about the case where we would be returning a CONST_DOUBLE. */
810
811 if (GET_MODE_CLASS (mode) != MODE_INT
812 || (GET_CODE (op) != CONST_INT && GET_CODE (op) != CONST_DOUBLE))
813 return 0;
814
815 if (WORDS_BIG_ENDIAN)
816 i = GET_MODE_SIZE (mode) / UNITS_PER_WORD - 1 - i;
817
818 /* Find out which word on the host machine this value is in and get
819 it from the constant. */
820 val = (i / size_ratio == 0
821 ? (GET_CODE (op) == CONST_INT ? INTVAL (op) : CONST_DOUBLE_LOW (op))
822 : (GET_CODE (op) == CONST_INT
823 ? (INTVAL (op) < 0 ? ~0 : 0) : CONST_DOUBLE_HIGH (op)));
824
825 /* If BITS_PER_WORD is smaller than an int, get the appropriate bits. */
826 if (BITS_PER_WORD < HOST_BITS_PER_INT)
827 val = ((val >> ((i % size_ratio) * BITS_PER_WORD))
828 & ((1 << (BITS_PER_WORD % HOST_BITS_PER_INT)) - 1));
829
830 return gen_rtx (CONST_INT, VOIDmode, val);
831 }
832
833 /* Similar to `operand_subword', but never return 0. If we can't extract
834 the required subword, put OP into a register and try again. If that fails,
835 abort. We always validate the address in this case. It is not valid
836 to call this function after reload; it is mostly meant for RTL
837 generation.
838
839 MODE is the mode of OP, in case it is CONST_INT. */
840
841 rtx
842 operand_subword_force (op, i, mode)
843 rtx op;
844 int i;
845 enum machine_mode mode;
846 {
847 rtx result = operand_subword (op, i, 1, mode);
848
849 if (result)
850 return result;
851
852 if (mode != BLKmode && mode != VOIDmode)
853 op = force_reg (mode, op);
854
855 result = operand_subword (op, i, 1, mode);
856 if (result == 0)
857 abort ();
858
859 return result;
860 }
861 \f
862 /* Given a compare instruction, swap the operands.
863 A test instruction is changed into a compare of 0 against the operand. */
864
865 void
866 reverse_comparison (insn)
867 rtx insn;
868 {
869 rtx body = PATTERN (insn);
870 rtx comp;
871
872 if (GET_CODE (body) == SET)
873 comp = SET_SRC (body);
874 else
875 comp = SET_SRC (XVECEXP (body, 0, 0));
876
877 if (GET_CODE (comp) == COMPARE)
878 {
879 rtx op0 = XEXP (comp, 0);
880 rtx op1 = XEXP (comp, 1);
881 XEXP (comp, 0) = op1;
882 XEXP (comp, 1) = op0;
883 }
884 else
885 {
886 rtx new = gen_rtx (COMPARE, VOIDmode,
887 CONST0_RTX (GET_MODE (comp)), comp);
888 if (GET_CODE (body) == SET)
889 SET_SRC (body) = new;
890 else
891 SET_SRC (XVECEXP (body, 0, 0)) = new;
892 }
893 }
894 \f
895 /* Return a memory reference like MEMREF, but with its mode changed
896 to MODE and its address changed to ADDR.
897 (VOIDmode means don't change the mode.
898 NULL for ADDR means don't change the address.) */
899
900 rtx
901 change_address (memref, mode, addr)
902 rtx memref;
903 enum machine_mode mode;
904 rtx addr;
905 {
906 rtx new;
907
908 if (GET_CODE (memref) != MEM)
909 abort ();
910 if (mode == VOIDmode)
911 mode = GET_MODE (memref);
912 if (addr == 0)
913 addr = XEXP (memref, 0);
914
915 /* If reload is in progress or has completed, ADDR must be valid.
916 Otherwise, we can call memory_address to make it valid. */
917 if (reload_completed || reload_in_progress)
918 {
919 if (! memory_address_p (mode, addr))
920 abort ();
921 }
922 else
923 addr = memory_address (mode, addr);
924
925 new = gen_rtx (MEM, mode, addr);
926 MEM_VOLATILE_P (new) = MEM_VOLATILE_P (memref);
927 RTX_UNCHANGING_P (new) = RTX_UNCHANGING_P (memref);
928 MEM_IN_STRUCT_P (new) = MEM_IN_STRUCT_P (memref);
929 return new;
930 }
931 \f
932 /* Return a newly created CODE_LABEL rtx with a unique label number. */
933
934 rtx
935 gen_label_rtx ()
936 {
937 register rtx label = gen_rtx (CODE_LABEL, VOIDmode, 0, 0, 0, label_num++, 0);
938 LABEL_NUSES (label) = 0;
939 return label;
940 }
941 \f
942 /* For procedure integration. */
943
944 /* Return a newly created INLINE_HEADER rtx. Should allocate this
945 from a permanent obstack when the opportunity arises. */
946
947 rtx
948 gen_inline_header_rtx (first_insn, first_parm_insn, first_labelno,
949 last_labelno, max_parm_regnum, max_regnum, args_size,
950 pops_args, stack_slots, function_flags,
951 outgoing_args_size, original_arg_vector,
952 original_decl_initial)
953 rtx first_insn, first_parm_insn;
954 int first_labelno, last_labelno, max_parm_regnum, max_regnum, args_size;
955 int pops_args;
956 rtx stack_slots;
957 int function_flags;
958 int outgoing_args_size;
959 rtvec original_arg_vector;
960 rtx original_decl_initial;
961 {
962 rtx header = gen_rtx (INLINE_HEADER, VOIDmode,
963 cur_insn_uid++, NULL,
964 first_insn, first_parm_insn,
965 first_labelno, last_labelno,
966 max_parm_regnum, max_regnum, args_size, pops_args,
967 stack_slots, function_flags, outgoing_args_size,
968 original_arg_vector, original_decl_initial);
969 return header;
970 }
971
972 /* Install new pointers to the first and last insns in the chain.
973 Used for an inline-procedure after copying the insn chain. */
974
975 void
976 set_new_first_and_last_insn (first, last)
977 rtx first, last;
978 {
979 first_insn = first;
980 last_insn = last;
981 }
982
983 /* Set the range of label numbers found in the current function.
984 This is used when belatedly compiling an inline function. */
985
986 void
987 set_new_first_and_last_label_num (first, last)
988 int first, last;
989 {
990 base_label_num = label_num;
991 first_label_num = first;
992 last_label_num = last;
993 }
994 \f
995 /* Save all variables describing the current status into the structure *P.
996 This is used before starting a nested function. */
997
998 void
999 save_emit_status (p)
1000 struct function *p;
1001 {
1002 p->reg_rtx_no = reg_rtx_no;
1003 p->first_label_num = first_label_num;
1004 p->first_insn = first_insn;
1005 p->last_insn = last_insn;
1006 p->sequence_stack = sequence_stack;
1007 p->cur_insn_uid = cur_insn_uid;
1008 p->last_linenum = last_linenum;
1009 p->last_filename = last_filename;
1010 p->regno_pointer_flag = regno_pointer_flag;
1011 p->regno_pointer_flag_length = regno_pointer_flag_length;
1012 p->regno_reg_rtx = regno_reg_rtx;
1013 }
1014
1015 /* Restore all variables describing the current status from the structure *P.
1016 This is used after a nested function. */
1017
1018 void
1019 restore_emit_status (p)
1020 struct function *p;
1021 {
1022 int i;
1023
1024 reg_rtx_no = p->reg_rtx_no;
1025 first_label_num = p->first_label_num;
1026 first_insn = p->first_insn;
1027 last_insn = p->last_insn;
1028 sequence_stack = p->sequence_stack;
1029 cur_insn_uid = p->cur_insn_uid;
1030 last_linenum = p->last_linenum;
1031 last_filename = p->last_filename;
1032 regno_pointer_flag = p->regno_pointer_flag;
1033 regno_pointer_flag_length = p->regno_pointer_flag_length;
1034 regno_reg_rtx = p->regno_reg_rtx;
1035
1036 /* Clear our cache of rtx expressions for start_sequence and gen_sequence. */
1037 sequence_element_free_list = 0;
1038 for (i = 0; i < SEQUENCE_RESULT_SIZE; i++)
1039 sequence_result[i] = 0;
1040 }
1041 \f
1042 /* Go through all the RTL insn bodies and copy any invalid shared structure.
1043 It does not work to do this twice, because the mark bits set here
1044 are not cleared afterwards. */
1045
1046 void
1047 unshare_all_rtl (insn)
1048 register rtx insn;
1049 {
1050 for (; insn; insn = NEXT_INSN (insn))
1051 if (GET_CODE (insn) == INSN || GET_CODE (insn) == JUMP_INSN
1052 || GET_CODE (insn) == CALL_INSN)
1053 {
1054 PATTERN (insn) = copy_rtx_if_shared (PATTERN (insn));
1055 REG_NOTES (insn) = copy_rtx_if_shared (REG_NOTES (insn));
1056 LOG_LINKS (insn) = copy_rtx_if_shared (LOG_LINKS (insn));
1057 }
1058
1059 /* Make sure the addresses of stack slots found outside the insn chain
1060 (such as, in DECL_RTL of a variable) are not shared
1061 with the insn chain.
1062
1063 This special care is necessary when the stack slot MEM does not
1064 actually appear in the insn chain. If it does appear, its address
1065 is unshared from all else at that point. */
1066
1067 copy_rtx_if_shared (stack_slot_list);
1068 }
1069
1070 /* Mark ORIG as in use, and return a copy of it if it was already in use.
1071 Recursively does the same for subexpressions. */
1072
1073 rtx
1074 copy_rtx_if_shared (orig)
1075 rtx orig;
1076 {
1077 register rtx x = orig;
1078 register int i;
1079 register enum rtx_code code;
1080 register char *format_ptr;
1081 int copied = 0;
1082
1083 if (x == 0)
1084 return 0;
1085
1086 code = GET_CODE (x);
1087
1088 /* These types may be freely shared. */
1089
1090 switch (code)
1091 {
1092 case REG:
1093 case QUEUED:
1094 case CONST_INT:
1095 case CONST_DOUBLE:
1096 case SYMBOL_REF:
1097 case CODE_LABEL:
1098 case PC:
1099 case CC0:
1100 case SCRATCH:
1101 /* SCRATCH must be shared because they represent distinct values. */
1102 return x;
1103
1104 case INSN:
1105 case JUMP_INSN:
1106 case CALL_INSN:
1107 case NOTE:
1108 case LABEL_REF:
1109 case BARRIER:
1110 /* The chain of insns is not being copied. */
1111 return x;
1112
1113 case MEM:
1114 /* A MEM is allowed to be shared if its address is constant
1115 or is a constant plus one of the special registers. */
1116 if (CONSTANT_ADDRESS_P (XEXP (x, 0))
1117 || XEXP (x, 0) == virtual_stack_vars_rtx
1118 || XEXP (x, 0) == virtual_incoming_args_rtx)
1119 return x;
1120
1121 if (GET_CODE (XEXP (x, 0)) == PLUS
1122 && (XEXP (XEXP (x, 0), 0) == virtual_stack_vars_rtx
1123 || XEXP (XEXP (x, 0), 0) == virtual_incoming_args_rtx)
1124 && CONSTANT_ADDRESS_P (XEXP (XEXP (x, 0), 1)))
1125 {
1126 /* This MEM can appear in more than one place,
1127 but its address better not be shared with anything else. */
1128 if (! x->used)
1129 XEXP (x, 0) = copy_rtx_if_shared (XEXP (x, 0));
1130 x->used = 1;
1131 return x;
1132 }
1133 }
1134
1135 /* This rtx may not be shared. If it has already been seen,
1136 replace it with a copy of itself. */
1137
1138 if (x->used)
1139 {
1140 register rtx copy;
1141
1142 copy = rtx_alloc (code);
1143 bcopy (x, copy, (sizeof (*copy) - sizeof (copy->fld)
1144 + sizeof (copy->fld[0]) * GET_RTX_LENGTH (code)));
1145 x = copy;
1146 copied = 1;
1147 }
1148 x->used = 1;
1149
1150 /* Now scan the subexpressions recursively.
1151 We can store any replaced subexpressions directly into X
1152 since we know X is not shared! Any vectors in X
1153 must be copied if X was copied. */
1154
1155 format_ptr = GET_RTX_FORMAT (code);
1156
1157 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1158 {
1159 switch (*format_ptr++)
1160 {
1161 case 'e':
1162 XEXP (x, i) = copy_rtx_if_shared (XEXP (x, i));
1163 break;
1164
1165 case 'E':
1166 if (XVEC (x, i) != NULL)
1167 {
1168 register int j;
1169
1170 if (copied)
1171 XVEC (x, i) = gen_rtvec_v (XVECLEN (x, i), &XVECEXP (x, i, 0));
1172 for (j = 0; j < XVECLEN (x, i); j++)
1173 XVECEXP (x, i, j)
1174 = copy_rtx_if_shared (XVECEXP (x, i, j));
1175 }
1176 break;
1177 }
1178 }
1179 return x;
1180 }
1181
1182 /* Clear all the USED bits in X to allow copy_rtx_if_shared to be used
1183 to look for shared sub-parts. */
1184
1185 void
1186 reset_used_flags (x)
1187 rtx x;
1188 {
1189 register int i, j;
1190 register enum rtx_code code;
1191 register char *format_ptr;
1192 int copied = 0;
1193
1194 if (x == 0)
1195 return;
1196
1197 code = GET_CODE (x);
1198
1199 /* These types may be freely shared so we needn't do any reseting
1200 for them. */
1201
1202 switch (code)
1203 {
1204 case REG:
1205 case QUEUED:
1206 case CONST_INT:
1207 case CONST_DOUBLE:
1208 case SYMBOL_REF:
1209 case CODE_LABEL:
1210 case PC:
1211 case CC0:
1212 return;
1213
1214 case INSN:
1215 case JUMP_INSN:
1216 case CALL_INSN:
1217 case NOTE:
1218 case LABEL_REF:
1219 case BARRIER:
1220 /* The chain of insns is not being copied. */
1221 return;
1222 }
1223
1224 x->used = 0;
1225
1226 format_ptr = GET_RTX_FORMAT (code);
1227 for (i = 0; i < GET_RTX_LENGTH (code); i++)
1228 {
1229 switch (*format_ptr++)
1230 {
1231 case 'e':
1232 reset_used_flags (XEXP (x, i));
1233 break;
1234
1235 case 'E':
1236 for (j = 0; j < XVECLEN (x, i); j++)
1237 reset_used_flags (XVECEXP (x, i, j));
1238 break;
1239 }
1240 }
1241 }
1242 \f
1243 /* Copy X if necessary so that it won't be altered by changes in OTHER.
1244 Return X or the rtx for the pseudo reg the value of X was copied into.
1245 OTHER must be valid as a SET_DEST. */
1246
1247 rtx
1248 make_safe_from (x, other)
1249 rtx x, other;
1250 {
1251 while (1)
1252 switch (GET_CODE (other))
1253 {
1254 case SUBREG:
1255 other = SUBREG_REG (other);
1256 break;
1257 case STRICT_LOW_PART:
1258 case SIGN_EXTEND:
1259 case ZERO_EXTEND:
1260 other = XEXP (other, 0);
1261 break;
1262 default:
1263 goto done;
1264 }
1265 done:
1266 if ((GET_CODE (other) == MEM
1267 && ! CONSTANT_P (x)
1268 && GET_CODE (x) != REG
1269 && GET_CODE (x) != SUBREG)
1270 || (GET_CODE (other) == REG
1271 && (REGNO (other) < FIRST_PSEUDO_REGISTER
1272 || reg_mentioned_p (other, x))))
1273 {
1274 rtx temp = gen_reg_rtx (GET_MODE (x));
1275 emit_move_insn (temp, x);
1276 return temp;
1277 }
1278 return x;
1279 }
1280 \f
1281 /* Emission of insns (adding them to the doubly-linked list). */
1282
1283 /* Return the first insn of the current sequence or current function. */
1284
1285 rtx
1286 get_insns ()
1287 {
1288 return first_insn;
1289 }
1290
1291 /* Return the last insn emitted in current sequence or current function. */
1292
1293 rtx
1294 get_last_insn ()
1295 {
1296 return last_insn;
1297 }
1298
1299 /* Specify a new insn as the last in the chain. */
1300
1301 void
1302 set_last_insn (insn)
1303 rtx insn;
1304 {
1305 if (NEXT_INSN (insn) != 0)
1306 abort ();
1307 last_insn = insn;
1308 }
1309
1310 /* Return the last insn emitted, even if it is in a sequence now pushed. */
1311
1312 rtx
1313 get_last_insn_anywhere ()
1314 {
1315 struct sequence_stack *stack;
1316 if (last_insn)
1317 return last_insn;
1318 for (stack = sequence_stack; stack; stack = stack->next)
1319 if (stack->last != 0)
1320 return stack->last;
1321 return 0;
1322 }
1323
1324 /* Return a number larger than any instruction's uid in this function. */
1325
1326 int
1327 get_max_uid ()
1328 {
1329 return cur_insn_uid;
1330 }
1331 \f
1332 /* Return the next insn. If it is a SEQUENCE, return the first insn
1333 of the sequence. */
1334
1335 rtx
1336 next_insn (insn)
1337 rtx insn;
1338 {
1339 if (insn)
1340 {
1341 insn = NEXT_INSN (insn);
1342 if (insn && GET_CODE (insn) == INSN
1343 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1344 insn = XVECEXP (PATTERN (insn), 0, 0);
1345 }
1346
1347 return insn;
1348 }
1349
1350 /* Return the previous insn. If it is a SEQUENCE, return the last insn
1351 of the sequence. */
1352
1353 rtx
1354 previous_insn (insn)
1355 rtx insn;
1356 {
1357 if (insn)
1358 {
1359 insn = PREV_INSN (insn);
1360 if (insn && GET_CODE (insn) == INSN
1361 && GET_CODE (PATTERN (insn)) == SEQUENCE)
1362 insn = XVECEXP (PATTERN (insn), 0, XVECLEN (PATTERN (insn), 0) - 1);
1363 }
1364
1365 return insn;
1366 }
1367
1368 /* Return the next insn after INSN that is not a NOTE. This routine does not
1369 look inside SEQUENCEs. */
1370
1371 rtx
1372 next_nonnote_insn (insn)
1373 rtx insn;
1374 {
1375 while (insn)
1376 {
1377 insn = NEXT_INSN (insn);
1378 if (insn == 0 || GET_CODE (insn) != NOTE)
1379 break;
1380 }
1381
1382 return insn;
1383 }
1384
1385 /* Return the previous insn before INSN that is not a NOTE. This routine does
1386 not look inside SEQUENCEs. */
1387
1388 rtx
1389 prev_nonnote_insn (insn)
1390 rtx insn;
1391 {
1392 while (insn)
1393 {
1394 insn = PREV_INSN (insn);
1395 if (insn == 0 || GET_CODE (insn) != NOTE)
1396 break;
1397 }
1398
1399 return insn;
1400 }
1401
1402 /* Return the next INSN, CALL_INSN or JUMP_INSN after INSN;
1403 or 0, if there is none. This routine does not look inside
1404 SEQUENCEs. */
1405
1406 rtx
1407 next_real_insn (insn)
1408 rtx insn;
1409 {
1410 while (insn)
1411 {
1412 insn = NEXT_INSN (insn);
1413 if (insn == 0 || GET_CODE (insn) == INSN
1414 || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN)
1415 break;
1416 }
1417
1418 return insn;
1419 }
1420
1421 /* Return the last INSN, CALL_INSN or JUMP_INSN before INSN;
1422 or 0, if there is none. This routine does not look inside
1423 SEQUENCEs. */
1424
1425 rtx
1426 prev_real_insn (insn)
1427 rtx insn;
1428 {
1429 while (insn)
1430 {
1431 insn = PREV_INSN (insn);
1432 if (insn == 0 || GET_CODE (insn) == INSN || GET_CODE (insn) == CALL_INSN
1433 || GET_CODE (insn) == JUMP_INSN)
1434 break;
1435 }
1436
1437 return insn;
1438 }
1439
1440 /* Find the next insn after INSN that really does something. This routine
1441 does not look inside SEQUENCEs. Until reload has completed, this is the
1442 same as next_real_insn. */
1443
1444 rtx
1445 next_active_insn (insn)
1446 rtx insn;
1447 {
1448 while (insn)
1449 {
1450 insn = NEXT_INSN (insn);
1451 if (insn == 0
1452 || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
1453 || (GET_CODE (insn) == INSN
1454 && (! reload_completed
1455 || (GET_CODE (PATTERN (insn)) != USE
1456 && GET_CODE (PATTERN (insn)) != CLOBBER))))
1457 break;
1458 }
1459
1460 return insn;
1461 }
1462
1463 /* Find the last insn before INSN that really does something. This routine
1464 does not look inside SEQUENCEs. Until reload has completed, this is the
1465 same as prev_real_insn. */
1466
1467 rtx
1468 prev_active_insn (insn)
1469 rtx insn;
1470 {
1471 while (insn)
1472 {
1473 insn = PREV_INSN (insn);
1474 if (insn == 0
1475 || GET_CODE (insn) == CALL_INSN || GET_CODE (insn) == JUMP_INSN
1476 || (GET_CODE (insn) == INSN
1477 && (! reload_completed
1478 || (GET_CODE (PATTERN (insn)) != USE
1479 && GET_CODE (PATTERN (insn)) != CLOBBER))))
1480 break;
1481 }
1482
1483 return insn;
1484 }
1485
1486 /* Return the next CODE_LABEL after the insn INSN, or 0 if there is none. */
1487
1488 rtx
1489 next_label (insn)
1490 rtx insn;
1491 {
1492 while (insn)
1493 {
1494 insn = NEXT_INSN (insn);
1495 if (insn == 0 || GET_CODE (insn) == CODE_LABEL)
1496 break;
1497 }
1498
1499 return insn;
1500 }
1501
1502 /* Return the last CODE_LABEL before the insn INSN, or 0 if there is none. */
1503
1504 rtx
1505 prev_label (insn)
1506 rtx insn;
1507 {
1508 while (insn)
1509 {
1510 insn = PREV_INSN (insn);
1511 if (insn == 0 || GET_CODE (insn) == CODE_LABEL)
1512 break;
1513 }
1514
1515 return insn;
1516 }
1517 \f
1518 #ifdef HAVE_cc0
1519 /* INSN uses CC0 and is being moved into a delay slot. Set up REG_CC_SETTER
1520 and REG_CC_USER notes so we can find it. */
1521
1522 void
1523 link_cc0_insns (insn)
1524 rtx insn;
1525 {
1526 rtx user = next_nonnote_insn (insn);
1527
1528 if (GET_CODE (user) == INSN && GET_CODE (PATTERN (user)) == SEQUENCE)
1529 user = XVECEXP (PATTERN (user), 0, 0);
1530
1531 REG_NOTES (user) = gen_rtx (INSN_LIST, REG_CC_SETTER, insn,
1532 REG_NOTES (user));
1533 REG_NOTES (insn) = gen_rtx (INSN_LIST, REG_CC_USER, user, REG_NOTES (insn));
1534 }
1535
1536 /* Return the next insn that uses CC0 after INSN, which is assumed to
1537 set it. This is the inverse of prev_cc0_setter (i.e., prev_cc0_setter
1538 applied to the result of this function should yield INSN).
1539
1540 Normally, this is simply the next insn. However, if a REG_CC_USER note
1541 is present, it contains the insn that uses CC0.
1542
1543 Return 0 if we can't find the insn. */
1544
1545 rtx
1546 next_cc0_user (insn)
1547 rtx insn;
1548 {
1549 rtx note = find_reg_note (insn, REG_CC_USER, 0);
1550
1551 if (note)
1552 return XEXP (note, 0);
1553
1554 insn = next_nonnote_insn (insn);
1555 if (insn && GET_CODE (insn) == INSN && GET_CODE (PATTERN (insn)) == SEQUENCE)
1556 insn = XVECEXP (PATTERN (insn), 0, 0);
1557
1558 if (insn && GET_RTX_CLASS (GET_CODE (insn)) == 'i'
1559 && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
1560 return insn;
1561
1562 return 0;
1563 }
1564
1565 /* Find the insn that set CC0 for INSN. Unless INSN has a REG_CC_SETTER
1566 note, it is the previous insn. */
1567
1568 rtx
1569 prev_cc0_setter (insn)
1570 rtx insn;
1571 {
1572 rtx note = find_reg_note (insn, REG_CC_SETTER, 0);
1573 rtx link;
1574
1575 if (note)
1576 return XEXP (note, 0);
1577
1578 insn = prev_nonnote_insn (insn);
1579 if (! sets_cc0_p (PATTERN (insn)))
1580 abort ();
1581
1582 return insn;
1583 }
1584 #endif
1585 \f
1586 /* Try splitting insns that can be split for better scheduling.
1587 PAT is the pattern which might split.
1588 TRIAL is the insn providing PAT.
1589 BACKWARDS is non-zero if we are scanning insns from last to first.
1590
1591 If this routine succeeds in splitting, it returns the first or last
1592 replacement insn depending on the value of BACKWARDS. Otherwise, it
1593 returns TRIAL. If the insn to be returned can be split, it will be. */
1594
1595 rtx
1596 try_split (pat, trial, backwards)
1597 rtx pat, trial;
1598 int backwards;
1599 {
1600 rtx before = PREV_INSN (trial);
1601 rtx after = NEXT_INSN (trial);
1602 rtx seq = split_insns (pat, trial);
1603 int has_barrier = 0;
1604 rtx tem;
1605
1606 /* If we are splitting a JUMP_INSN, it might be followed by a BARRIER.
1607 We may need to handle this specially. */
1608 if (after && GET_CODE (after) == BARRIER)
1609 {
1610 has_barrier = 1;
1611 after = NEXT_INSN (after);
1612 }
1613
1614 if (seq)
1615 {
1616 /* SEQ can either be a SEQUENCE or the pattern of a single insn.
1617 The latter case will normally arise only when being done so that
1618 it, in turn, will be split (SFmode on the 29k is an example). */
1619 if (GET_CODE (seq) == SEQUENCE)
1620 {
1621 /* If we are splitting a JUMP_INSN, look for the JUMP_INSN in
1622 SEQ and copy our JUMP_LABEL to it. If JUMP_LABEL is non-zero,
1623 increment the usage count so we don't delete the label. */
1624 int i;
1625
1626 if (GET_CODE (trial) == JUMP_INSN)
1627 for (i = XVECLEN (seq, 0) - 1; i >= 0; i--)
1628 if (GET_CODE (XVECEXP (seq, 0, i)) == JUMP_INSN)
1629 {
1630 JUMP_LABEL (XVECEXP (seq, 0, i)) = JUMP_LABEL (trial);
1631
1632 if (JUMP_LABEL (trial))
1633 LABEL_NUSES (JUMP_LABEL (trial))++;
1634 }
1635
1636 tem = emit_insn_after (seq, before);
1637
1638 delete_insn (trial);
1639 if (has_barrier)
1640 emit_barrier_after (tem);
1641 }
1642 /* Avoid infinite loop if the result matches the original pattern. */
1643 else if (rtx_equal_p (seq, pat))
1644 return trial;
1645 else
1646 {
1647 PATTERN (trial) = seq;
1648 INSN_CODE (trial) = -1;
1649 }
1650
1651 /* Set TEM to the insn we should return. */
1652 tem = backwards ? prev_active_insn (after) : next_active_insn (before);
1653 return try_split (PATTERN (tem), tem, backwards);
1654 }
1655
1656 return trial;
1657 }
1658 \f
1659 /* Make and return an INSN rtx, initializing all its slots.
1660 Store PATTERN in the pattern slots.
1661 PAT_FORMALS is an idea that never really went anywhere. */
1662
1663 rtx
1664 make_insn_raw (pattern, pat_formals)
1665 rtx pattern;
1666 rtvec pat_formals;
1667 {
1668 register rtx insn;
1669
1670 insn = rtx_alloc(INSN);
1671 INSN_UID(insn) = cur_insn_uid++;
1672
1673 PATTERN (insn) = pattern;
1674 INSN_CODE (insn) = -1;
1675 LOG_LINKS(insn) = NULL;
1676 REG_NOTES(insn) = NULL;
1677
1678 return insn;
1679 }
1680
1681 /* Like `make_insn' but make a JUMP_INSN instead of an insn. */
1682
1683 static rtx
1684 make_jump_insn_raw (pattern, pat_formals)
1685 rtx pattern;
1686 rtvec pat_formals;
1687 {
1688 register rtx insn;
1689
1690 insn = rtx_alloc(JUMP_INSN);
1691 INSN_UID(insn) = cur_insn_uid++;
1692
1693 PATTERN (insn) = pattern;
1694 INSN_CODE (insn) = -1;
1695 LOG_LINKS(insn) = NULL;
1696 REG_NOTES(insn) = NULL;
1697 JUMP_LABEL(insn) = NULL;
1698
1699 return insn;
1700 }
1701 \f
1702 /* Add INSN to the end of the doubly-linked list.
1703 INSN may be an INSN, JUMP_INSN, CALL_INSN, CODE_LABEL, BARRIER or NOTE. */
1704
1705 void
1706 add_insn (insn)
1707 register rtx insn;
1708 {
1709 PREV_INSN (insn) = last_insn;
1710 NEXT_INSN (insn) = 0;
1711
1712 if (NULL != last_insn)
1713 NEXT_INSN (last_insn) = insn;
1714
1715 if (NULL == first_insn)
1716 first_insn = insn;
1717
1718 last_insn = insn;
1719 }
1720
1721 /* Add INSN into the doubly-linked list after insn AFTER. This should be the
1722 only function called to insert an insn once delay slots have been filled
1723 since only it knows how to update a SEQUENCE. */
1724
1725 void
1726 add_insn_after (insn, after)
1727 rtx insn, after;
1728 {
1729 rtx next = NEXT_INSN (after);
1730
1731 NEXT_INSN (insn) = next;
1732 PREV_INSN (insn) = after;
1733
1734 if (next)
1735 {
1736 PREV_INSN (next) = insn;
1737 if (GET_CODE (next) == INSN && GET_CODE (PATTERN (next)) == SEQUENCE)
1738 PREV_INSN (XVECEXP (PATTERN (next), 0, 0)) = insn;
1739 }
1740 else if (last_insn == after)
1741 last_insn = insn;
1742 else
1743 {
1744 struct sequence_stack *stack = sequence_stack;
1745 /* Scan all pending sequences too. */
1746 for (; stack; stack = stack->next)
1747 if (after == stack->last)
1748 stack->last = insn;
1749 }
1750
1751 NEXT_INSN (after) = insn;
1752 if (GET_CODE (after) == INSN && GET_CODE (PATTERN (after)) == SEQUENCE)
1753 {
1754 rtx sequence = PATTERN (after);
1755 NEXT_INSN (XVECEXP (sequence, 0, XVECLEN (sequence, 0) - 1)) = insn;
1756 }
1757 }
1758
1759 /* Delete all insns made since FROM.
1760 FROM becomes the new last instruction. */
1761
1762 void
1763 delete_insns_since (from)
1764 rtx from;
1765 {
1766 if (from == 0)
1767 first_insn = 0;
1768 else
1769 NEXT_INSN (from) = 0;
1770 last_insn = from;
1771 }
1772
1773 /* Move a consecutive bunch of insns to a different place in the chain.
1774 The insns to be moved are those between FROM and TO.
1775 They are moved to a new position after the insn AFTER.
1776 AFTER must not be FROM or TO or any insn in between.
1777
1778 This function does not know about SEQUENCEs and hence should not be
1779 called after delay-slot filling has been done. */
1780
1781 void
1782 reorder_insns (from, to, after)
1783 rtx from, to, after;
1784 {
1785 /* Splice this bunch out of where it is now. */
1786 if (PREV_INSN (from))
1787 NEXT_INSN (PREV_INSN (from)) = NEXT_INSN (to);
1788 if (NEXT_INSN (to))
1789 PREV_INSN (NEXT_INSN (to)) = PREV_INSN (from);
1790 if (last_insn == to)
1791 last_insn = PREV_INSN (from);
1792 if (first_insn == from)
1793 first_insn = NEXT_INSN (to);
1794
1795 /* Make the new neighbors point to it and it to them. */
1796 if (NEXT_INSN (after))
1797 PREV_INSN (NEXT_INSN (after)) = to;
1798
1799 NEXT_INSN (to) = NEXT_INSN (after);
1800 PREV_INSN (from) = after;
1801 NEXT_INSN (after) = from;
1802 if (after == last_insn)
1803 last_insn = to;
1804 }
1805
1806 /* Return the line note insn preceding INSN. */
1807
1808 static rtx
1809 find_line_note (insn)
1810 rtx insn;
1811 {
1812 if (no_line_numbers)
1813 return 0;
1814
1815 for (; insn; insn = PREV_INSN (insn))
1816 if (GET_CODE (insn) == NOTE
1817 && NOTE_LINE_NUMBER (insn) >= 0)
1818 break;
1819
1820 return insn;
1821 }
1822
1823 /* Like reorder_insns, but inserts line notes to preserve the line numbers
1824 of the moved insns when debugging. This may insert a note between AFTER
1825 and FROM, and another one after TO. */
1826
1827 void
1828 reorder_insns_with_line_notes (from, to, after)
1829 rtx from, to, after;
1830 {
1831 rtx from_line = find_line_note (from);
1832 rtx after_line = find_line_note (after);
1833
1834 reorder_insns (from, to, after);
1835
1836 if (from_line == after_line)
1837 return;
1838
1839 if (from_line)
1840 emit_line_note_after (NOTE_SOURCE_FILE (from_line),
1841 NOTE_LINE_NUMBER (from_line),
1842 after);
1843 if (after_line)
1844 emit_line_note_after (NOTE_SOURCE_FILE (after_line),
1845 NOTE_LINE_NUMBER (after_line),
1846 to);
1847 }
1848 \f
1849 /* Emit an insn of given code and pattern
1850 at a specified place within the doubly-linked list. */
1851
1852 /* Make an instruction with body PATTERN
1853 and output it before the instruction BEFORE. */
1854
1855 rtx
1856 emit_insn_before (pattern, before)
1857 register rtx pattern, before;
1858 {
1859 register rtx insn = before;
1860
1861 if (GET_CODE (pattern) == SEQUENCE)
1862 {
1863 register int i;
1864
1865 for (i = 0; i < XVECLEN (pattern, 0); i++)
1866 {
1867 insn = XVECEXP (pattern, 0, i);
1868 add_insn_after (insn, PREV_INSN (before));
1869 }
1870 if (XVECLEN (pattern, 0) < SEQUENCE_RESULT_SIZE)
1871 sequence_result[XVECLEN (pattern, 0)] = pattern;
1872 }
1873 else
1874 {
1875 insn = make_insn_raw (pattern, 0);
1876 add_insn_after (insn, PREV_INSN (before));
1877 }
1878
1879 return insn;
1880 }
1881
1882 /* Make an instruction with body PATTERN and code JUMP_INSN
1883 and output it before the instruction BEFORE. */
1884
1885 rtx
1886 emit_jump_insn_before (pattern, before)
1887 register rtx pattern, before;
1888 {
1889 register rtx insn;
1890
1891 if (GET_CODE (pattern) == SEQUENCE)
1892 insn = emit_insn_before (pattern, before);
1893 else
1894 {
1895 insn = make_jump_insn_raw (pattern, 0);
1896 add_insn_after (insn, PREV_INSN (before));
1897 }
1898
1899 return insn;
1900 }
1901
1902 /* Make an instruction with body PATTERN and code CALL_INSN
1903 and output it before the instruction BEFORE. */
1904
1905 rtx
1906 emit_call_insn_before (pattern, before)
1907 register rtx pattern, before;
1908 {
1909 rtx insn = emit_insn_before (pattern, before);
1910 PUT_CODE (insn, CALL_INSN);
1911 return insn;
1912 }
1913
1914 /* Make an insn of code BARRIER
1915 and output it before the insn AFTER. */
1916
1917 rtx
1918 emit_barrier_before (before)
1919 register rtx before;
1920 {
1921 register rtx insn = rtx_alloc (BARRIER);
1922
1923 INSN_UID (insn) = cur_insn_uid++;
1924
1925 add_insn_after (insn, PREV_INSN (before));
1926 return insn;
1927 }
1928
1929 /* Emit a note of subtype SUBTYPE before the insn BEFORE. */
1930
1931 rtx
1932 emit_note_before (subtype, before)
1933 int subtype;
1934 rtx before;
1935 {
1936 register rtx note = rtx_alloc (NOTE);
1937 INSN_UID (note) = cur_insn_uid++;
1938 NOTE_SOURCE_FILE (note) = 0;
1939 NOTE_LINE_NUMBER (note) = subtype;
1940
1941 add_insn_after (note, PREV_INSN (before));
1942 return note;
1943 }
1944 \f
1945 /* Make an insn of code INSN with body PATTERN
1946 and output it after the insn AFTER. */
1947
1948 rtx
1949 emit_insn_after (pattern, after)
1950 register rtx pattern, after;
1951 {
1952 register rtx insn = after;
1953
1954 if (GET_CODE (pattern) == SEQUENCE)
1955 {
1956 register int i;
1957
1958 for (i = 0; i < XVECLEN (pattern, 0); i++)
1959 {
1960 insn = XVECEXP (pattern, 0, i);
1961 add_insn_after (insn, after);
1962 after = insn;
1963 }
1964 if (XVECLEN (pattern, 0) < SEQUENCE_RESULT_SIZE)
1965 sequence_result[XVECLEN (pattern, 0)] = pattern;
1966 }
1967 else
1968 {
1969 insn = make_insn_raw (pattern, 0);
1970 add_insn_after (insn, after);
1971 }
1972
1973 return insn;
1974 }
1975
1976 /* Make an insn of code JUMP_INSN with body PATTERN
1977 and output it after the insn AFTER. */
1978
1979 rtx
1980 emit_jump_insn_after (pattern, after)
1981 register rtx pattern, after;
1982 {
1983 register rtx insn;
1984
1985 if (GET_CODE (pattern) == SEQUENCE)
1986 insn = emit_insn_after (pattern, after);
1987 else
1988 {
1989 insn = make_jump_insn_raw (pattern, 0);
1990 add_insn_after (insn, after);
1991 }
1992
1993 return insn;
1994 }
1995
1996 /* Make an insn of code BARRIER
1997 and output it after the insn AFTER. */
1998
1999 rtx
2000 emit_barrier_after (after)
2001 register rtx after;
2002 {
2003 register rtx insn = rtx_alloc (BARRIER);
2004
2005 INSN_UID (insn) = cur_insn_uid++;
2006
2007 add_insn_after (insn, after);
2008 return insn;
2009 }
2010
2011 /* Emit the label LABEL after the insn AFTER. */
2012
2013 rtx
2014 emit_label_after (label, after)
2015 rtx label, after;
2016 {
2017 /* This can be called twice for the same label
2018 as a result of the confusion that follows a syntax error!
2019 So make it harmless. */
2020 if (INSN_UID (label) == 0)
2021 {
2022 INSN_UID (label) = cur_insn_uid++;
2023 add_insn_after (label, after);
2024 }
2025
2026 return label;
2027 }
2028
2029 /* Emit a note of subtype SUBTYPE after the insn AFTER. */
2030
2031 rtx
2032 emit_note_after (subtype, after)
2033 int subtype;
2034 rtx after;
2035 {
2036 register rtx note = rtx_alloc (NOTE);
2037 INSN_UID (note) = cur_insn_uid++;
2038 NOTE_SOURCE_FILE (note) = 0;
2039 NOTE_LINE_NUMBER (note) = subtype;
2040 add_insn_after (note, after);
2041 return note;
2042 }
2043
2044 /* Emit a line note for FILE and LINE after the insn AFTER. */
2045
2046 rtx
2047 emit_line_note_after (file, line, after)
2048 char *file;
2049 int line;
2050 rtx after;
2051 {
2052 register rtx note;
2053
2054 if (no_line_numbers && line > 0)
2055 {
2056 cur_insn_uid++;
2057 return 0;
2058 }
2059
2060 note = rtx_alloc (NOTE);
2061 INSN_UID (note) = cur_insn_uid++;
2062 NOTE_SOURCE_FILE (note) = file;
2063 NOTE_LINE_NUMBER (note) = line;
2064 add_insn_after (note, after);
2065 return note;
2066 }
2067 \f
2068 /* Make an insn of code INSN with pattern PATTERN
2069 and add it to the end of the doubly-linked list.
2070 If PATTERN is a SEQUENCE, take the elements of it
2071 and emit an insn for each element.
2072
2073 Returns the last insn emitted. */
2074
2075 rtx
2076 emit_insn (pattern)
2077 rtx pattern;
2078 {
2079 rtx insn = last_insn;
2080
2081 if (GET_CODE (pattern) == SEQUENCE)
2082 {
2083 register int i;
2084
2085 for (i = 0; i < XVECLEN (pattern, 0); i++)
2086 {
2087 insn = XVECEXP (pattern, 0, i);
2088 add_insn (insn);
2089 }
2090 if (XVECLEN (pattern, 0) < SEQUENCE_RESULT_SIZE)
2091 sequence_result[XVECLEN (pattern, 0)] = pattern;
2092 }
2093 else
2094 {
2095 insn = make_insn_raw (pattern, NULL);
2096 add_insn (insn);
2097 }
2098
2099 return insn;
2100 }
2101
2102 /* Emit the insns in a chain starting with INSN.
2103 Return the last insn emitted. */
2104
2105 rtx
2106 emit_insns (insn)
2107 rtx insn;
2108 {
2109 rtx last = 0;
2110
2111 while (insn)
2112 {
2113 rtx next = NEXT_INSN (insn);
2114 add_insn (insn);
2115 last = insn;
2116 insn = next;
2117 }
2118
2119 return last;
2120 }
2121
2122 /* Emit the insns in a chain starting with INSN and place them in front of
2123 the insn BEFORE. Return the last insn emitted. */
2124
2125 rtx
2126 emit_insns_before (insn, before)
2127 rtx insn;
2128 rtx before;
2129 {
2130 rtx last = 0;
2131
2132 while (insn)
2133 {
2134 rtx next = NEXT_INSN (insn);
2135 add_insn_after (insn, PREV_INSN (before));
2136 last = insn;
2137 insn = next;
2138 }
2139
2140 return last;
2141 }
2142
2143 /* Make an insn of code JUMP_INSN with pattern PATTERN
2144 and add it to the end of the doubly-linked list. */
2145
2146 rtx
2147 emit_jump_insn (pattern)
2148 rtx pattern;
2149 {
2150 if (GET_CODE (pattern) == SEQUENCE)
2151 return emit_insn (pattern);
2152 else
2153 {
2154 register rtx insn = make_jump_insn_raw (pattern, NULL);
2155 add_insn (insn);
2156 return insn;
2157 }
2158 }
2159
2160 /* Make an insn of code CALL_INSN with pattern PATTERN
2161 and add it to the end of the doubly-linked list. */
2162
2163 rtx
2164 emit_call_insn (pattern)
2165 rtx pattern;
2166 {
2167 if (GET_CODE (pattern) == SEQUENCE)
2168 return emit_insn (pattern);
2169 else
2170 {
2171 register rtx insn = make_insn_raw (pattern, NULL);
2172 add_insn (insn);
2173 PUT_CODE (insn, CALL_INSN);
2174 return insn;
2175 }
2176 }
2177
2178 /* Add the label LABEL to the end of the doubly-linked list. */
2179
2180 rtx
2181 emit_label (label)
2182 rtx label;
2183 {
2184 /* This can be called twice for the same label
2185 as a result of the confusion that follows a syntax error!
2186 So make it harmless. */
2187 if (INSN_UID (label) == 0)
2188 {
2189 INSN_UID (label) = cur_insn_uid++;
2190 add_insn (label);
2191 }
2192 return label;
2193 }
2194
2195 /* Make an insn of code BARRIER
2196 and add it to the end of the doubly-linked list. */
2197
2198 rtx
2199 emit_barrier ()
2200 {
2201 register rtx barrier = rtx_alloc (BARRIER);
2202 INSN_UID (barrier) = cur_insn_uid++;
2203 add_insn (barrier);
2204 return barrier;
2205 }
2206
2207 /* Make an insn of code NOTE
2208 with data-fields specified by FILE and LINE
2209 and add it to the end of the doubly-linked list,
2210 but only if line-numbers are desired for debugging info. */
2211
2212 rtx
2213 emit_line_note (file, line)
2214 char *file;
2215 int line;
2216 {
2217 emit_filename = file;
2218 emit_lineno = line;
2219
2220 #if 0
2221 if (no_line_numbers)
2222 return 0;
2223 #endif
2224
2225 return emit_note (file, line);
2226 }
2227
2228 /* Make an insn of code NOTE
2229 with data-fields specified by FILE and LINE
2230 and add it to the end of the doubly-linked list.
2231 If it is a line-number NOTE, omit it if it matches the previous one. */
2232
2233 rtx
2234 emit_note (file, line)
2235 char *file;
2236 int line;
2237 {
2238 register rtx note;
2239
2240 if (line > 0)
2241 {
2242 if (file && last_filename && !strcmp (file, last_filename)
2243 && line == last_linenum)
2244 return 0;
2245 last_filename = file;
2246 last_linenum = line;
2247 }
2248
2249 if (no_line_numbers && line > 0)
2250 {
2251 cur_insn_uid++;
2252 return 0;
2253 }
2254
2255 note = rtx_alloc (NOTE);
2256 INSN_UID (note) = cur_insn_uid++;
2257 NOTE_SOURCE_FILE (note) = file;
2258 NOTE_LINE_NUMBER (note) = line;
2259 add_insn (note);
2260 return note;
2261 }
2262
2263 /* Emit a NOTE, and don't omit it even if LINE it the previous note. */
2264
2265 rtx
2266 emit_line_note_force (file, line)
2267 char *file;
2268 int line;
2269 {
2270 last_linenum = -1;
2271 return emit_line_note (file, line);
2272 }
2273
2274 /* Cause next statement to emit a line note even if the line number
2275 has not changed. This is used at the beginning of a function. */
2276
2277 void
2278 force_next_line_note ()
2279 {
2280 last_linenum = -1;
2281 }
2282 \f
2283 /* Return an indication of which type of insn should have X as a body.
2284 The value is CODE_LABEL, INSN, CALL_INSN or JUMP_INSN. */
2285
2286 enum rtx_code
2287 classify_insn (x)
2288 rtx x;
2289 {
2290 if (GET_CODE (x) == CODE_LABEL)
2291 return CODE_LABEL;
2292 if (GET_CODE (x) == CALL)
2293 return CALL_INSN;
2294 if (GET_CODE (x) == RETURN)
2295 return JUMP_INSN;
2296 if (GET_CODE (x) == SET)
2297 {
2298 if (SET_DEST (x) == pc_rtx)
2299 return JUMP_INSN;
2300 else if (GET_CODE (SET_SRC (x)) == CALL)
2301 return CALL_INSN;
2302 else
2303 return INSN;
2304 }
2305 if (GET_CODE (x) == PARALLEL)
2306 {
2307 register int j;
2308 for (j = XVECLEN (x, 0) - 1; j >= 0; j--)
2309 if (GET_CODE (XVECEXP (x, 0, j)) == CALL)
2310 return CALL_INSN;
2311 else if (GET_CODE (XVECEXP (x, 0, j)) == SET
2312 && SET_DEST (XVECEXP (x, 0, j)) == pc_rtx)
2313 return JUMP_INSN;
2314 else if (GET_CODE (XVECEXP (x, 0, j)) == SET
2315 && GET_CODE (SET_SRC (XVECEXP (x, 0, j))) == CALL)
2316 return CALL_INSN;
2317 }
2318 return INSN;
2319 }
2320
2321 /* Emit the rtl pattern X as an appropriate kind of insn.
2322 If X is a label, it is simply added into the insn chain. */
2323
2324 rtx
2325 emit (x)
2326 rtx x;
2327 {
2328 enum rtx_code code = classify_insn (x);
2329
2330 if (code == CODE_LABEL)
2331 return emit_label (x);
2332 else if (code == INSN)
2333 return emit_insn (x);
2334 else if (code == JUMP_INSN)
2335 {
2336 register rtx insn = emit_jump_insn (x);
2337 if (simplejump_p (insn) || GET_CODE (x) == RETURN)
2338 return emit_barrier ();
2339 return insn;
2340 }
2341 else if (code == CALL_INSN)
2342 return emit_call_insn (x);
2343 else
2344 abort ();
2345 }
2346 \f
2347 /* Begin emitting insns to a sequence which can be packaged in an RTL_EXPR. */
2348
2349 void
2350 start_sequence ()
2351 {
2352 struct sequence_stack *tem;
2353
2354 if (sequence_element_free_list)
2355 {
2356 /* Reuse a previously-saved struct sequence_stack. */
2357 tem = sequence_element_free_list;
2358 sequence_element_free_list = tem->next;
2359 }
2360 else
2361 tem = (struct sequence_stack *) permalloc (sizeof (struct sequence_stack));
2362
2363 tem->next = sequence_stack;
2364 tem->first = first_insn;
2365 tem->last = last_insn;
2366
2367 sequence_stack = tem;
2368
2369 first_insn = 0;
2370 last_insn = 0;
2371 }
2372
2373 /* Set up the insn chain starting with FIRST
2374 as the current sequence, saving the previously current one. */
2375
2376 void
2377 push_to_sequence (first)
2378 rtx first;
2379 {
2380 rtx last;
2381
2382 start_sequence ();
2383
2384 for (last = first; last && NEXT_INSN (last); last = NEXT_INSN (last));
2385
2386 first_insn = first;
2387 last_insn = last;
2388 }
2389
2390 /* After emitting to a sequence, restore previous saved state.
2391
2392 To get the contents of the sequence just made,
2393 you must call `gen_sequence' *before* calling here. */
2394
2395 void
2396 end_sequence ()
2397 {
2398 struct sequence_stack *tem = sequence_stack;
2399
2400 first_insn = tem->first;
2401 last_insn = tem->last;
2402 sequence_stack = tem->next;
2403
2404 tem->next = sequence_element_free_list;
2405 sequence_element_free_list = tem;
2406 }
2407
2408 /* Return 1 if currently emitting into a sequence. */
2409
2410 int
2411 in_sequence_p ()
2412 {
2413 return sequence_stack != 0;
2414 }
2415
2416 /* Generate a SEQUENCE rtx containing the insns already emitted
2417 to the current sequence.
2418
2419 This is how the gen_... function from a DEFINE_EXPAND
2420 constructs the SEQUENCE that it returns. */
2421
2422 rtx
2423 gen_sequence ()
2424 {
2425 rtx result;
2426 rtx tem;
2427 rtvec newvec;
2428 int i;
2429 int len;
2430
2431 /* Count the insns in the chain. */
2432 len = 0;
2433 for (tem = first_insn; tem; tem = NEXT_INSN (tem))
2434 len++;
2435
2436 /* If only one insn, return its pattern rather than a SEQUENCE.
2437 (Now that we cache SEQUENCE expressions, it isn't worth special-casing
2438 the case of an empty list.) */
2439 if (len == 1
2440 && (GET_CODE (first_insn) == INSN
2441 || GET_CODE (first_insn) == JUMP_INSN
2442 || GET_CODE (first_insn) == CALL_INSN))
2443 return PATTERN (first_insn);
2444
2445 /* Put them in a vector. See if we already have a SEQUENCE of the
2446 appropriate length around. */
2447 if (len < SEQUENCE_RESULT_SIZE && (result = sequence_result[len]) != 0)
2448 sequence_result[len] = 0;
2449 else
2450 {
2451 /* Ensure that this rtl goes in saveable_obstack, since we may be
2452 caching it. */
2453 int in_current_obstack = rtl_in_saveable_obstack ();
2454 result = gen_rtx (SEQUENCE, VOIDmode, rtvec_alloc (len));
2455 if (in_current_obstack)
2456 rtl_in_current_obstack ();
2457 }
2458
2459 for (i = 0, tem = first_insn; tem; tem = NEXT_INSN (tem), i++)
2460 XVECEXP (result, 0, i) = tem;
2461
2462 return result;
2463 }
2464 \f
2465 /* Set up regno_reg_rtx, reg_rtx_no and regno_pointer_flag
2466 according to the chain of insns starting with FIRST.
2467
2468 Also set cur_insn_uid to exceed the largest uid in that chain.
2469
2470 This is used when an inline function's rtl is saved
2471 and passed to rest_of_compilation later. */
2472
2473 static void restore_reg_data_1 ();
2474
2475 void
2476 restore_reg_data (first)
2477 rtx first;
2478 {
2479 register rtx insn;
2480 int i;
2481 register int max_uid = 0;
2482
2483 for (insn = first; insn; insn = NEXT_INSN (insn))
2484 {
2485 if (INSN_UID (insn) >= max_uid)
2486 max_uid = INSN_UID (insn);
2487
2488 switch (GET_CODE (insn))
2489 {
2490 case NOTE:
2491 case CODE_LABEL:
2492 case BARRIER:
2493 break;
2494
2495 case JUMP_INSN:
2496 case CALL_INSN:
2497 case INSN:
2498 restore_reg_data_1 (PATTERN (insn));
2499 break;
2500 }
2501 }
2502
2503 /* Don't duplicate the uids already in use. */
2504 cur_insn_uid = max_uid + 1;
2505
2506 /* If any regs are missing, make them up.
2507
2508 ??? word_mode is not necessarily the right mode. Most likely these REGs
2509 are never used. At some point this should be checked. */
2510
2511 for (i = FIRST_PSEUDO_REGISTER; i < reg_rtx_no; i++)
2512 if (regno_reg_rtx[i] == 0)
2513 regno_reg_rtx[i] = gen_rtx (REG, word_mode, i);
2514 }
2515
2516 static void
2517 restore_reg_data_1 (orig)
2518 rtx orig;
2519 {
2520 register rtx x = orig;
2521 register int i;
2522 register enum rtx_code code;
2523 register char *format_ptr;
2524
2525 code = GET_CODE (x);
2526
2527 switch (code)
2528 {
2529 case QUEUED:
2530 case CONST_INT:
2531 case CONST_DOUBLE:
2532 case SYMBOL_REF:
2533 case CODE_LABEL:
2534 case PC:
2535 case CC0:
2536 case LABEL_REF:
2537 return;
2538
2539 case REG:
2540 if (REGNO (x) >= FIRST_PSEUDO_REGISTER)
2541 {
2542 /* Make sure regno_pointer_flag and regno_reg_rtx are large
2543 enough to have an element for this pseudo reg number. */
2544 if (REGNO (x) >= reg_rtx_no)
2545 {
2546 reg_rtx_no = REGNO (x);
2547
2548 if (reg_rtx_no >= regno_pointer_flag_length)
2549 {
2550 int newlen = MAX (regno_pointer_flag_length * 2,
2551 reg_rtx_no + 30);
2552 rtx *new1;
2553 char *new = (char *) oballoc (newlen);
2554 bzero (new, newlen);
2555 bcopy (regno_pointer_flag, new, regno_pointer_flag_length);
2556
2557 new1 = (rtx *) oballoc (newlen * sizeof (rtx));
2558 bzero (new1, newlen * sizeof (rtx));
2559 bcopy (regno_reg_rtx, new1, regno_pointer_flag_length * sizeof (rtx));
2560
2561 regno_pointer_flag = new;
2562 regno_reg_rtx = new1;
2563 regno_pointer_flag_length = newlen;
2564 }
2565 reg_rtx_no ++;
2566 }
2567 regno_reg_rtx[REGNO (x)] = x;
2568 }
2569 return;
2570
2571 case MEM:
2572 if (GET_CODE (XEXP (x, 0)) == REG)
2573 mark_reg_pointer (XEXP (x, 0));
2574 restore_reg_data_1 (XEXP (x, 0));
2575 return;
2576 }
2577
2578 /* Now scan the subexpressions recursively. */
2579
2580 format_ptr = GET_RTX_FORMAT (code);
2581
2582 for (i = 0; i < GET_RTX_LENGTH (code); i++)
2583 {
2584 switch (*format_ptr++)
2585 {
2586 case 'e':
2587 restore_reg_data_1 (XEXP (x, i));
2588 break;
2589
2590 case 'E':
2591 if (XVEC (x, i) != NULL)
2592 {
2593 register int j;
2594
2595 for (j = 0; j < XVECLEN (x, i); j++)
2596 restore_reg_data_1 (XVECEXP (x, i, j));
2597 }
2598 break;
2599 }
2600 }
2601 }
2602 \f
2603 /* Initialize data structures and variables in this file
2604 before generating rtl for each function. */
2605
2606 void
2607 init_emit ()
2608 {
2609 int i;
2610
2611 first_insn = NULL;
2612 last_insn = NULL;
2613 cur_insn_uid = 1;
2614 reg_rtx_no = LAST_VIRTUAL_REGISTER + 1;
2615 last_linenum = 0;
2616 last_filename = 0;
2617 first_label_num = label_num;
2618 last_label_num = 0;
2619
2620 /* Clear the start_sequence/gen_sequence cache. */
2621 sequence_element_free_list = 0;
2622 for (i = 0; i < SEQUENCE_RESULT_SIZE; i++)
2623 sequence_result[i] = 0;
2624
2625 /* Init the tables that describe all the pseudo regs. */
2626
2627 regno_pointer_flag_length = LAST_VIRTUAL_REGISTER + 101;
2628
2629 regno_pointer_flag
2630 = (char *) oballoc (regno_pointer_flag_length);
2631 bzero (regno_pointer_flag, regno_pointer_flag_length);
2632
2633 regno_reg_rtx
2634 = (rtx *) oballoc (regno_pointer_flag_length * sizeof (rtx));
2635 bzero (regno_reg_rtx, regno_pointer_flag_length * sizeof (rtx));
2636
2637 /* Put copies of all the virtual register rtx into regno_reg_rtx. */
2638 regno_reg_rtx[VIRTUAL_INCOMING_ARGS_REGNUM] = virtual_incoming_args_rtx;
2639 regno_reg_rtx[VIRTUAL_STACK_VARS_REGNUM] = virtual_stack_vars_rtx;
2640 regno_reg_rtx[VIRTUAL_STACK_DYNAMIC_REGNUM] = virtual_stack_dynamic_rtx;
2641 regno_reg_rtx[VIRTUAL_OUTGOING_ARGS_REGNUM] = virtual_outgoing_args_rtx;
2642 }
2643
2644 /* Create some permanent unique rtl objects shared between all functions.
2645 LINE_NUMBERS is nonzero if line numbers are to be generated. */
2646
2647 void
2648 init_emit_once (line_numbers)
2649 int line_numbers;
2650 {
2651 int i;
2652 enum machine_mode mode;
2653
2654 no_line_numbers = ! line_numbers;
2655
2656 sequence_stack = NULL;
2657
2658 /* Create the unique rtx's for certain rtx codes and operand values. */
2659
2660 pc_rtx = gen_rtx (PC, VOIDmode);
2661 cc0_rtx = gen_rtx (CC0, VOIDmode);
2662
2663 /* Don't use gen_rtx here since gen_rtx in this case
2664 tries to use these variables. */
2665 for (i = - MAX_SAVED_CONST_INT; i <= MAX_SAVED_CONST_INT; i++)
2666 {
2667 const_int_rtx[i + MAX_SAVED_CONST_INT] = rtx_alloc (CONST_INT);
2668 PUT_MODE (const_int_rtx[i + MAX_SAVED_CONST_INT], VOIDmode);
2669 INTVAL (const_int_rtx[i + MAX_SAVED_CONST_INT]) = i;
2670 }
2671
2672 /* These four calls obtain some of the rtx expressions made above. */
2673 const0_rtx = gen_rtx (CONST_INT, VOIDmode, 0);
2674 const1_rtx = gen_rtx (CONST_INT, VOIDmode, 1);
2675 const2_rtx = gen_rtx (CONST_INT, VOIDmode, 2);
2676 constm1_rtx = gen_rtx (CONST_INT, VOIDmode, -1);
2677
2678 /* This will usually be one of the above constants, but may be a new rtx. */
2679 const_true_rtx = gen_rtx (CONST_INT, VOIDmode, STORE_FLAG_VALUE);
2680
2681 dconst0 = REAL_VALUE_ATOF ("0");
2682 dconst1 = REAL_VALUE_ATOF ("1");
2683 dconst2 = REAL_VALUE_ATOF ("2");
2684 dconstm1 = REAL_VALUE_ATOF ("-1");
2685
2686 for (i = 0; i <= 2; i++)
2687 {
2688 for (mode = GET_CLASS_NARROWEST_MODE (MODE_FLOAT); mode != VOIDmode;
2689 mode = GET_MODE_WIDER_MODE (mode))
2690 {
2691 rtx tem = rtx_alloc (CONST_DOUBLE);
2692 union real_extract u;
2693
2694 bzero (&u, sizeof u); /* Zero any holes in a structure. */
2695 u.d = i == 0 ? dconst0 : i == 1 ? dconst1 : dconst2;
2696
2697 bcopy (&u, &CONST_DOUBLE_LOW (tem), sizeof u);
2698 CONST_DOUBLE_MEM (tem) = cc0_rtx;
2699 PUT_MODE (tem, mode);
2700
2701 const_tiny_rtx[i][(int) mode] = tem;
2702 }
2703
2704 const_tiny_rtx[i][(int) VOIDmode] = gen_rtx (CONST_INT, VOIDmode, i);
2705
2706 for (mode = GET_CLASS_NARROWEST_MODE (MODE_INT); mode != VOIDmode;
2707 mode = GET_MODE_WIDER_MODE (mode))
2708 const_tiny_rtx[i][(int) mode] = gen_rtx (CONST_INT, VOIDmode, i);
2709 }
2710
2711 stack_pointer_rtx = gen_rtx (REG, Pmode, STACK_POINTER_REGNUM);
2712 frame_pointer_rtx = gen_rtx (REG, Pmode, FRAME_POINTER_REGNUM);
2713
2714 if (FRAME_POINTER_REGNUM == ARG_POINTER_REGNUM)
2715 arg_pointer_rtx = frame_pointer_rtx;
2716 else if (STACK_POINTER_REGNUM == ARG_POINTER_REGNUM)
2717 arg_pointer_rtx = stack_pointer_rtx;
2718 else
2719 arg_pointer_rtx = gen_rtx (REG, Pmode, ARG_POINTER_REGNUM);
2720
2721 /* Create the virtual registers. Do so here since the following objects
2722 might reference them. */
2723
2724 virtual_incoming_args_rtx = gen_rtx (REG, Pmode,
2725 VIRTUAL_INCOMING_ARGS_REGNUM);
2726 virtual_stack_vars_rtx = gen_rtx (REG, Pmode,
2727 VIRTUAL_STACK_VARS_REGNUM);
2728 virtual_stack_dynamic_rtx = gen_rtx (REG, Pmode,
2729 VIRTUAL_STACK_DYNAMIC_REGNUM);
2730 virtual_outgoing_args_rtx = gen_rtx (REG, Pmode,
2731 VIRTUAL_OUTGOING_ARGS_REGNUM);
2732
2733 #ifdef STRUCT_VALUE
2734 struct_value_rtx = STRUCT_VALUE;
2735 #else
2736 struct_value_rtx = gen_rtx (REG, Pmode, STRUCT_VALUE_REGNUM);
2737 #endif
2738
2739 #ifdef STRUCT_VALUE_INCOMING
2740 struct_value_incoming_rtx = STRUCT_VALUE_INCOMING;
2741 #else
2742 #ifdef STRUCT_VALUE_INCOMING_REGNUM
2743 struct_value_incoming_rtx
2744 = gen_rtx (REG, Pmode, STRUCT_VALUE_INCOMING_REGNUM);
2745 #else
2746 struct_value_incoming_rtx = struct_value_rtx;
2747 #endif
2748 #endif
2749
2750 #ifdef STATIC_CHAIN_REGNUM
2751 static_chain_rtx = gen_rtx (REG, Pmode, STATIC_CHAIN_REGNUM);
2752
2753 #ifdef STATIC_CHAIN_INCOMING_REGNUM
2754 if (STATIC_CHAIN_INCOMING_REGNUM != STATIC_CHAIN_REGNUM)
2755 static_chain_incoming_rtx = gen_rtx (REG, Pmode, STATIC_CHAIN_INCOMING_REGNUM);
2756 else
2757 #endif
2758 static_chain_incoming_rtx = static_chain_rtx;
2759 #endif
2760
2761 #ifdef STATIC_CHAIN
2762 static_chain_rtx = STATIC_CHAIN;
2763
2764 #ifdef STATIC_CHAIN_INCOMING
2765 static_chain_incoming_rtx = STATIC_CHAIN_INCOMING;
2766 #else
2767 static_chain_incoming_rtx = static_chain_rtx;
2768 #endif
2769 #endif
2770
2771 #ifdef PIC_OFFSET_TABLE_REGNUM
2772 pic_offset_table_rtx = gen_rtx (REG, Pmode, PIC_OFFSET_TABLE_REGNUM);
2773 #endif
2774 }
This page took 0.156248 seconds and 5 git commands to generate.