]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/rtlanal.c
cse.c (fold_rtx): Use simplify_subreg.
[thirdparty/gcc.git] / gcc / rtlanal.c
CommitLineData
2c88418c 1/* Analyze RTL for C-Compiler
af841dbd 2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
f759eb8b 3 1999, 2000, 2001 Free Software Foundation, Inc.
2c88418c
RS
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12GNU CC is distributed in the hope that it will be useful,
13but WITHOUT ANY WARRANTY; without even the implied warranty of
14MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15GNU General Public License for more details.
16
17You should have received a copy of the GNU General Public License
18along with GNU CC; see the file COPYING. If not, write to
e99215a3
RK
19the Free Software Foundation, 59 Temple Place - Suite 330,
20Boston, MA 02111-1307, USA. */
2c88418c
RS
21
22
23#include "config.h"
670ee920 24#include "system.h"
e35b9579 25#include "toplev.h"
2c88418c 26#include "rtl.h"
3335f1d9 27#include "hard-reg-set.h"
2c88418c 28
e2373f95 29/* Forward declarations */
91b2d119 30static void set_of_1 PARAMS ((rtx, rtx, void *));
4eb00163 31static void insn_dependent_p_1 PARAMS ((rtx, rtx, void *));
fce7e199 32static int computed_jump_p_1 PARAMS ((rtx));
2a1777af 33
2c88418c
RS
34/* Bit flags that specify the machine subtype we are compiling for.
35 Bits are tested using macros TARGET_... defined in the tm.h file
36 and set by `-m...' switches. Must be defined in rtlanal.c. */
37
38int target_flags;
39\f
40/* Return 1 if the value of X is unstable
41 (would be different at a different point in the program).
42 The frame pointer, arg pointer, etc. are considered stable
43 (within one function) and so is anything marked `unchanging'. */
44
45int
46rtx_unstable_p (x)
47 rtx x;
48{
49 register RTX_CODE code = GET_CODE (x);
50 register int i;
6f7d635c 51 register const char *fmt;
2c88418c 52
ae0fb1b9
JW
53 switch (code)
54 {
55 case MEM:
56 return ! RTX_UNCHANGING_P (x) || rtx_unstable_p (XEXP (x, 0));
2c88418c 57
ae0fb1b9
JW
58 case QUEUED:
59 return 1;
2c88418c 60
ae0fb1b9
JW
61 case CONST:
62 case CONST_INT:
63 case CONST_DOUBLE:
64 case SYMBOL_REF:
65 case LABEL_REF:
66 return 0;
2c88418c 67
ae0fb1b9
JW
68 case REG:
69 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
c0fc376b 70 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3335f1d9
JL
71 /* The arg pointer varies if it is not a fixed register. */
72 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM])
73 || RTX_UNCHANGING_P (x))
c0fc376b
RH
74 return 0;
75#ifndef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
76 /* ??? When call-clobbered, the value is stable modulo the restore
77 that must happen after a call. This currently screws up local-alloc
78 into believing that the restore is not needed. */
79 if (x == pic_offset_table_rtx)
80 return 0;
81#endif
82 return 1;
ae0fb1b9
JW
83
84 case ASM_OPERANDS:
85 if (MEM_VOLATILE_P (x))
86 return 1;
87
88 /* FALLTHROUGH */
89
90 default:
91 break;
92 }
2c88418c
RS
93
94 fmt = GET_RTX_FORMAT (code);
95 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
96 if (fmt[i] == 'e')
9c82ac6b
JW
97 {
98 if (rtx_unstable_p (XEXP (x, i)))
99 return 1;
100 }
101 else if (fmt[i] == 'E')
102 {
103 int j;
104 for (j = 0; j < XVECLEN (x, i); j++)
105 if (rtx_unstable_p (XVECEXP (x, i, j)))
106 return 1;
107 }
108
2c88418c
RS
109 return 0;
110}
111
112/* Return 1 if X has a value that can vary even between two
113 executions of the program. 0 means X can be compared reliably
114 against certain constants or near-constants.
e38fe8e0
BS
115 FOR_ALIAS is nonzero if we are called from alias analysis; if it is
116 zero, we are slightly more conservative.
2c88418c
RS
117 The frame pointer and the arg pointer are considered constant. */
118
119int
e38fe8e0 120rtx_varies_p (x, for_alias)
2c88418c 121 rtx x;
e38fe8e0 122 int for_alias;
2c88418c
RS
123{
124 register RTX_CODE code = GET_CODE (x);
125 register int i;
6f7d635c 126 register const char *fmt;
2c88418c
RS
127
128 switch (code)
129 {
130 case MEM:
e38fe8e0 131 return ! RTX_UNCHANGING_P (x) || rtx_varies_p (XEXP (x, 0), for_alias);
55efb413 132
2c88418c
RS
133 case QUEUED:
134 return 1;
135
136 case CONST:
137 case CONST_INT:
138 case CONST_DOUBLE:
139 case SYMBOL_REF:
140 case LABEL_REF:
141 return 0;
142
143 case REG:
144 /* Note that we have to test for the actual rtx used for the frame
145 and arg pointers and not just the register number in case we have
146 eliminated the frame and/or arg pointer and are using it
147 for pseudos. */
c0fc376b 148 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
3335f1d9
JL
149 /* The arg pointer varies if it is not a fixed register. */
150 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
c0fc376b 151 return 0;
e38fe8e0
BS
152 if (x == pic_offset_table_rtx
153#ifdef PIC_OFFSET_TABLE_REG_CALL_CLOBBERED
154 /* ??? When call-clobbered, the value is stable modulo the restore
155 that must happen after a call. This currently screws up
156 local-alloc into believing that the restore is not needed, so we
157 must return 0 only if we are called from alias analysis. */
158 && for_alias
c0fc376b 159#endif
e38fe8e0
BS
160 )
161 return 0;
c0fc376b 162 return 1;
2c88418c
RS
163
164 case LO_SUM:
165 /* The operand 0 of a LO_SUM is considered constant
e7d96a83
JW
166 (in fact it is related specifically to operand 1)
167 during alias analysis. */
168 return (! for_alias && rtx_varies_p (XEXP (x, 0), for_alias))
169 || rtx_varies_p (XEXP (x, 1), for_alias);
e9a25f70 170
ae0fb1b9
JW
171 case ASM_OPERANDS:
172 if (MEM_VOLATILE_P (x))
173 return 1;
174
175 /* FALLTHROUGH */
176
e9a25f70
JL
177 default:
178 break;
2c88418c
RS
179 }
180
181 fmt = GET_RTX_FORMAT (code);
182 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
183 if (fmt[i] == 'e')
9c82ac6b 184 {
e38fe8e0 185 if (rtx_varies_p (XEXP (x, i), for_alias))
9c82ac6b
JW
186 return 1;
187 }
188 else if (fmt[i] == 'E')
189 {
190 int j;
191 for (j = 0; j < XVECLEN (x, i); j++)
e38fe8e0 192 if (rtx_varies_p (XVECEXP (x, i, j), for_alias))
9c82ac6b
JW
193 return 1;
194 }
195
2c88418c
RS
196 return 0;
197}
198
199/* Return 0 if the use of X as an address in a MEM can cause a trap. */
200
e38fe8e0 201int
2c88418c
RS
202rtx_addr_can_trap_p (x)
203 register rtx x;
204{
205 register enum rtx_code code = GET_CODE (x);
206
207 switch (code)
208 {
209 case SYMBOL_REF:
ff0b6b99
FS
210 return SYMBOL_REF_WEAK (x);
211
2c88418c 212 case LABEL_REF:
2c88418c
RS
213 return 0;
214
215 case REG:
216 /* As in rtx_varies_p, we have to use the actual rtx, not reg number. */
4f73495e
RH
217 if (x == frame_pointer_rtx || x == hard_frame_pointer_rtx
218 || x == stack_pointer_rtx
219 /* The arg pointer varies if it is not a fixed register. */
220 || (x == arg_pointer_rtx && fixed_regs[ARG_POINTER_REGNUM]))
221 return 0;
222 /* All of the virtual frame registers are stack references. */
223 if (REGNO (x) >= FIRST_VIRTUAL_REGISTER
224 && REGNO (x) <= LAST_VIRTUAL_REGISTER)
225 return 0;
226 return 1;
2c88418c
RS
227
228 case CONST:
229 return rtx_addr_can_trap_p (XEXP (x, 0));
230
231 case PLUS:
232 /* An address is assumed not to trap if it is an address that can't
55efb413
JW
233 trap plus a constant integer or it is the pic register plus a
234 constant. */
235 return ! ((! rtx_addr_can_trap_p (XEXP (x, 0))
236 && GET_CODE (XEXP (x, 1)) == CONST_INT)
237 || (XEXP (x, 0) == pic_offset_table_rtx
238 && CONSTANT_P (XEXP (x, 1))));
2c88418c
RS
239
240 case LO_SUM:
4f73495e 241 case PRE_MODIFY:
2c88418c 242 return rtx_addr_can_trap_p (XEXP (x, 1));
4f73495e
RH
243
244 case PRE_DEC:
245 case PRE_INC:
246 case POST_DEC:
247 case POST_INC:
248 case POST_MODIFY:
249 return rtx_addr_can_trap_p (XEXP (x, 0));
250
e9a25f70
JL
251 default:
252 break;
2c88418c
RS
253 }
254
255 /* If it isn't one of the case above, it can cause a trap. */
256 return 1;
257}
258
259/* Return 1 if X refers to a memory location whose address
260 cannot be compared reliably with constant addresses,
e38fe8e0
BS
261 or if X refers to a BLKmode memory object.
262 FOR_ALIAS is nonzero if we are called from alias analysis; if it is
263 zero, we are slightly more conservative. */
2c88418c
RS
264
265int
e38fe8e0 266rtx_addr_varies_p (x, for_alias)
2c88418c 267 rtx x;
e38fe8e0 268 int for_alias;
2c88418c
RS
269{
270 register enum rtx_code code;
271 register int i;
6f7d635c 272 register const char *fmt;
2c88418c
RS
273
274 if (x == 0)
275 return 0;
276
277 code = GET_CODE (x);
278 if (code == MEM)
e38fe8e0 279 return GET_MODE (x) == BLKmode || rtx_varies_p (XEXP (x, 0), for_alias);
2c88418c
RS
280
281 fmt = GET_RTX_FORMAT (code);
282 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
283 if (fmt[i] == 'e')
833c0b26 284 {
e38fe8e0 285 if (rtx_addr_varies_p (XEXP (x, i), for_alias))
833c0b26
RK
286 return 1;
287 }
288 else if (fmt[i] == 'E')
289 {
290 int j;
291 for (j = 0; j < XVECLEN (x, i); j++)
e38fe8e0 292 if (rtx_addr_varies_p (XVECEXP (x, i, j), for_alias))
833c0b26
RK
293 return 1;
294 }
2c88418c
RS
295 return 0;
296}
297\f
298/* Return the value of the integer term in X, if one is apparent;
299 otherwise return 0.
300 Only obvious integer terms are detected.
301 This is used in cse.c with the `related_value' field.*/
302
c166a311 303HOST_WIDE_INT
2c88418c
RS
304get_integer_term (x)
305 rtx x;
306{
307 if (GET_CODE (x) == CONST)
308 x = XEXP (x, 0);
309
310 if (GET_CODE (x) == MINUS
311 && GET_CODE (XEXP (x, 1)) == CONST_INT)
312 return - INTVAL (XEXP (x, 1));
313 if (GET_CODE (x) == PLUS
314 && GET_CODE (XEXP (x, 1)) == CONST_INT)
315 return INTVAL (XEXP (x, 1));
316 return 0;
317}
318
319/* If X is a constant, return the value sans apparent integer term;
320 otherwise return 0.
321 Only obvious integer terms are detected. */
322
323rtx
324get_related_value (x)
325 rtx x;
326{
327 if (GET_CODE (x) != CONST)
328 return 0;
329 x = XEXP (x, 0);
330 if (GET_CODE (x) == PLUS
331 && GET_CODE (XEXP (x, 1)) == CONST_INT)
332 return XEXP (x, 0);
333 else if (GET_CODE (x) == MINUS
334 && GET_CODE (XEXP (x, 1)) == CONST_INT)
335 return XEXP (x, 0);
336 return 0;
337}
338\f
4b983fdc
RH
339/* Return the number of places FIND appears within X. If COUNT_DEST is
340 zero, we do not count occurrences inside the destination of a SET. */
341
342int
343count_occurrences (x, find, count_dest)
344 rtx x, find;
345 int count_dest;
346{
347 int i, j;
348 enum rtx_code code;
349 const char *format_ptr;
350 int count;
351
352 if (x == find)
353 return 1;
354
355 code = GET_CODE (x);
356
357 switch (code)
358 {
359 case REG:
360 case CONST_INT:
361 case CONST_DOUBLE:
362 case SYMBOL_REF:
363 case CODE_LABEL:
364 case PC:
365 case CC0:
366 return 0;
367
368 case MEM:
369 if (GET_CODE (find) == MEM && rtx_equal_p (x, find))
370 return 1;
371 break;
372
373 case SET:
374 if (SET_DEST (x) == find && ! count_dest)
375 return count_occurrences (SET_SRC (x), find, count_dest);
376 break;
377
378 default:
379 break;
380 }
381
382 format_ptr = GET_RTX_FORMAT (code);
383 count = 0;
384
385 for (i = 0; i < GET_RTX_LENGTH (code); i++)
386 {
387 switch (*format_ptr++)
388 {
389 case 'e':
390 count += count_occurrences (XEXP (x, i), find, count_dest);
391 break;
392
393 case 'E':
394 for (j = 0; j < XVECLEN (x, i); j++)
395 count += count_occurrences (XVECEXP (x, i, j), find, count_dest);
396 break;
397 }
398 }
399 return count;
400}
401\f
2c88418c
RS
402/* Nonzero if register REG appears somewhere within IN.
403 Also works if REG is not a register; in this case it checks
404 for a subexpression of IN that is Lisp "equal" to REG. */
405
406int
407reg_mentioned_p (reg, in)
408 register rtx reg, in;
409{
6f7d635c 410 register const char *fmt;
2c88418c
RS
411 register int i;
412 register enum rtx_code code;
413
414 if (in == 0)
415 return 0;
416
417 if (reg == in)
418 return 1;
419
420 if (GET_CODE (in) == LABEL_REF)
421 return reg == XEXP (in, 0);
422
423 code = GET_CODE (in);
424
425 switch (code)
426 {
427 /* Compare registers by number. */
428 case REG:
429 return GET_CODE (reg) == REG && REGNO (in) == REGNO (reg);
430
431 /* These codes have no constituent expressions
432 and are unique. */
433 case SCRATCH:
434 case CC0:
435 case PC:
436 return 0;
437
438 case CONST_INT:
439 return GET_CODE (reg) == CONST_INT && INTVAL (in) == INTVAL (reg);
440
441 case CONST_DOUBLE:
442 /* These are kept unique for a given value. */
443 return 0;
e9a25f70
JL
444
445 default:
446 break;
2c88418c
RS
447 }
448
449 if (GET_CODE (reg) == code && rtx_equal_p (reg, in))
450 return 1;
451
452 fmt = GET_RTX_FORMAT (code);
453
454 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
455 {
456 if (fmt[i] == 'E')
457 {
458 register int j;
459 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
460 if (reg_mentioned_p (reg, XVECEXP (in, i, j)))
461 return 1;
462 }
463 else if (fmt[i] == 'e'
464 && reg_mentioned_p (reg, XEXP (in, i)))
465 return 1;
466 }
467 return 0;
468}
469\f
470/* Return 1 if in between BEG and END, exclusive of BEG and END, there is
471 no CODE_LABEL insn. */
472
473int
474no_labels_between_p (beg, end)
475 rtx beg, end;
476{
477 register rtx p;
478 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
479 if (GET_CODE (p) == CODE_LABEL)
480 return 0;
481 return 1;
482}
483
3ec2b590
R
484/* Return 1 if in between BEG and END, exclusive of BEG and END, there is
485 no JUMP_INSN insn. */
486
487int
488no_jumps_between_p (beg, end)
489 rtx beg, end;
490{
491 register rtx p;
492 for (p = NEXT_INSN (beg); p != end; p = NEXT_INSN (p))
493 if (GET_CODE (p) == JUMP_INSN)
494 return 0;
495 return 1;
496}
497
2c88418c
RS
498/* Nonzero if register REG is used in an insn between
499 FROM_INSN and TO_INSN (exclusive of those two). */
500
501int
502reg_used_between_p (reg, from_insn, to_insn)
503 rtx reg, from_insn, to_insn;
504{
505 register rtx insn;
506
507 if (from_insn == to_insn)
508 return 0;
509
510 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
2c3c49de 511 if (INSN_P (insn)
8f3e7a26
RK
512 && (reg_overlap_mentioned_p (reg, PATTERN (insn))
513 || (GET_CODE (insn) == CALL_INSN
514 && (find_reg_fusage (insn, USE, reg)
515 || find_reg_fusage (insn, CLOBBER, reg)))))
2c88418c
RS
516 return 1;
517 return 0;
518}
519\f
520/* Nonzero if the old value of X, a register, is referenced in BODY. If X
521 is entirely replaced by a new value and the only use is as a SET_DEST,
522 we do not consider it a reference. */
523
524int
525reg_referenced_p (x, body)
526 rtx x;
527 rtx body;
528{
529 int i;
530
531 switch (GET_CODE (body))
532 {
533 case SET:
534 if (reg_overlap_mentioned_p (x, SET_SRC (body)))
535 return 1;
536
537 /* If the destination is anything other than CC0, PC, a REG or a SUBREG
538 of a REG that occupies all of the REG, the insn references X if
539 it is mentioned in the destination. */
540 if (GET_CODE (SET_DEST (body)) != CC0
541 && GET_CODE (SET_DEST (body)) != PC
542 && GET_CODE (SET_DEST (body)) != REG
543 && ! (GET_CODE (SET_DEST (body)) == SUBREG
544 && GET_CODE (SUBREG_REG (SET_DEST (body))) == REG
545 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (body))))
546 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
547 == ((GET_MODE_SIZE (GET_MODE (SET_DEST (body)))
548 + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)))
549 && reg_overlap_mentioned_p (x, SET_DEST (body)))
550 return 1;
e9a25f70 551 return 0;
2c88418c
RS
552
553 case ASM_OPERANDS:
554 for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
555 if (reg_overlap_mentioned_p (x, ASM_OPERANDS_INPUT (body, i)))
556 return 1;
e9a25f70 557 return 0;
2c88418c
RS
558
559 case CALL:
560 case USE:
14a774a9 561 case IF_THEN_ELSE:
2c88418c
RS
562 return reg_overlap_mentioned_p (x, body);
563
564 case TRAP_IF:
565 return reg_overlap_mentioned_p (x, TRAP_CONDITION (body));
566
2ac4fed0
RK
567 case UNSPEC:
568 case UNSPEC_VOLATILE:
2f9fb4c2
R
569 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
570 if (reg_overlap_mentioned_p (x, XVECEXP (body, 0, i)))
571 return 1;
572 return 0;
573
2c88418c
RS
574 case PARALLEL:
575 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
576 if (reg_referenced_p (x, XVECEXP (body, 0, i)))
577 return 1;
e9a25f70
JL
578 return 0;
579
0d3ffb5a
GK
580 case CLOBBER:
581 if (GET_CODE (XEXP (body, 0)) == MEM)
582 if (reg_overlap_mentioned_p (x, XEXP (XEXP (body, 0), 0)))
583 return 1;
584 return 0;
585
0c99ec5c
RH
586 case COND_EXEC:
587 if (reg_overlap_mentioned_p (x, COND_EXEC_TEST (body)))
588 return 1;
589 return reg_referenced_p (x, COND_EXEC_CODE (body));
590
e9a25f70
JL
591 default:
592 return 0;
2c88418c 593 }
2c88418c
RS
594}
595
596/* Nonzero if register REG is referenced in an insn between
597 FROM_INSN and TO_INSN (exclusive of those two). Sets of REG do
0f41302f 598 not count. */
2c88418c
RS
599
600int
601reg_referenced_between_p (reg, from_insn, to_insn)
602 rtx reg, from_insn, to_insn;
603{
604 register rtx insn;
605
606 if (from_insn == to_insn)
607 return 0;
608
609 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
2c3c49de 610 if (INSN_P (insn)
8f3e7a26
RK
611 && (reg_referenced_p (reg, PATTERN (insn))
612 || (GET_CODE (insn) == CALL_INSN
613 && find_reg_fusage (insn, USE, reg))))
2c88418c
RS
614 return 1;
615 return 0;
616}
617\f
618/* Nonzero if register REG is set or clobbered in an insn between
619 FROM_INSN and TO_INSN (exclusive of those two). */
620
621int
622reg_set_between_p (reg, from_insn, to_insn)
623 rtx reg, from_insn, to_insn;
624{
625 register rtx insn;
626
627 if (from_insn == to_insn)
628 return 0;
629
630 for (insn = NEXT_INSN (from_insn); insn != to_insn; insn = NEXT_INSN (insn))
2c3c49de 631 if (INSN_P (insn) && reg_set_p (reg, insn))
2c88418c
RS
632 return 1;
633 return 0;
634}
635
636/* Internals of reg_set_between_p. */
2c88418c
RS
637int
638reg_set_p (reg, insn)
639 rtx reg, insn;
640{
641 rtx body = insn;
642
643 /* We can be passed an insn or part of one. If we are passed an insn,
644 check if a side-effect of the insn clobbers REG. */
2c3c49de 645 if (INSN_P (insn))
2c88418c
RS
646 {
647 if (FIND_REG_INC_NOTE (insn, reg)
648 || (GET_CODE (insn) == CALL_INSN
649 /* We'd like to test call_used_regs here, but rtlanal.c can't
650 reference that variable due to its use in genattrtab. So
8f3e7a26
RK
651 we'll just be more conservative.
652
653 ??? Unless we could ensure that the CALL_INSN_FUNCTION_USAGE
654 information holds all clobbered registers. */
2c88418c
RS
655 && ((GET_CODE (reg) == REG
656 && REGNO (reg) < FIRST_PSEUDO_REGISTER)
8f3e7a26
RK
657 || GET_CODE (reg) == MEM
658 || find_reg_fusage (insn, CLOBBER, reg))))
2c88418c
RS
659 return 1;
660
661 body = PATTERN (insn);
662 }
663
91b2d119 664 return set_of (reg, insn) != NULL_RTX;
2c88418c
RS
665}
666
a2e1a0bf
RH
667/* Similar to reg_set_between_p, but check all registers in X. Return 0
668 only if none of them are modified between START and END. Do not
669 consider non-registers one way or the other. */
670
671int
672regs_set_between_p (x, start, end)
673 rtx x;
674 rtx start, end;
675{
676 enum rtx_code code = GET_CODE (x);
6f7d635c 677 const char *fmt;
a2e1a0bf
RH
678 int i, j;
679
680 switch (code)
681 {
682 case CONST_INT:
683 case CONST_DOUBLE:
684 case CONST:
685 case SYMBOL_REF:
686 case LABEL_REF:
687 case PC:
688 case CC0:
689 return 0;
690
691 case REG:
692 return reg_set_between_p (x, start, end);
693
694 default:
695 break;
696 }
697
698 fmt = GET_RTX_FORMAT (code);
699 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
700 {
701 if (fmt[i] == 'e' && regs_set_between_p (XEXP (x, i), start, end))
702 return 1;
703
704 else if (fmt[i] == 'E')
705 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
706 if (regs_set_between_p (XVECEXP (x, i, j), start, end))
707 return 1;
708 }
709
710 return 0;
711}
712
2c88418c
RS
713/* Similar to reg_set_between_p, but check all registers in X. Return 0
714 only if none of them are modified between START and END. Return 1 if
715 X contains a MEM; this routine does not perform any memory aliasing. */
716
717int
718modified_between_p (x, start, end)
719 rtx x;
720 rtx start, end;
721{
722 enum rtx_code code = GET_CODE (x);
6f7d635c 723 const char *fmt;
f8163c92 724 int i, j;
2c88418c
RS
725
726 switch (code)
727 {
728 case CONST_INT:
729 case CONST_DOUBLE:
730 case CONST:
731 case SYMBOL_REF:
732 case LABEL_REF:
733 return 0;
734
735 case PC:
736 case CC0:
737 return 1;
738
739 case MEM:
740 /* If the memory is not constant, assume it is modified. If it is
741 constant, we still have to check the address. */
742 if (! RTX_UNCHANGING_P (x))
743 return 1;
744 break;
745
746 case REG:
747 return reg_set_between_p (x, start, end);
e9a25f70
JL
748
749 default:
750 break;
2c88418c
RS
751 }
752
753 fmt = GET_RTX_FORMAT (code);
754 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
f8163c92
RK
755 {
756 if (fmt[i] == 'e' && modified_between_p (XEXP (x, i), start, end))
757 return 1;
758
d4757e6a 759 else if (fmt[i] == 'E')
f8163c92
RK
760 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
761 if (modified_between_p (XVECEXP (x, i, j), start, end))
762 return 1;
763 }
764
765 return 0;
766}
767
768/* Similar to reg_set_p, but check all registers in X. Return 0 only if none
769 of them are modified in INSN. Return 1 if X contains a MEM; this routine
770 does not perform any memory aliasing. */
771
772int
773modified_in_p (x, insn)
774 rtx x;
775 rtx insn;
776{
777 enum rtx_code code = GET_CODE (x);
6f7d635c 778 const char *fmt;
f8163c92
RK
779 int i, j;
780
781 switch (code)
782 {
783 case CONST_INT:
784 case CONST_DOUBLE:
785 case CONST:
786 case SYMBOL_REF:
787 case LABEL_REF:
788 return 0;
789
790 case PC:
791 case CC0:
2c88418c
RS
792 return 1;
793
f8163c92
RK
794 case MEM:
795 /* If the memory is not constant, assume it is modified. If it is
796 constant, we still have to check the address. */
797 if (! RTX_UNCHANGING_P (x))
798 return 1;
799 break;
800
801 case REG:
802 return reg_set_p (x, insn);
e9a25f70
JL
803
804 default:
805 break;
f8163c92
RK
806 }
807
808 fmt = GET_RTX_FORMAT (code);
809 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
810 {
811 if (fmt[i] == 'e' && modified_in_p (XEXP (x, i), insn))
812 return 1;
813
d4757e6a 814 else if (fmt[i] == 'E')
f8163c92
RK
815 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
816 if (modified_in_p (XVECEXP (x, i, j), insn))
817 return 1;
818 }
819
2c88418c
RS
820 return 0;
821}
a8393d5d 822
4eb00163 823/* Return true if anything in insn X is (anti,output,true) dependent on
a8393d5d
RH
824 anything in insn Y. */
825
826int
4eb00163 827insn_dependent_p (x, y)
a8393d5d
RH
828 rtx x, y;
829{
830 rtx tmp;
831
832 if (! INSN_P (x) || ! INSN_P (y))
833 abort ();
834
835 tmp = PATTERN (y);
4eb00163 836 note_stores (PATTERN (x), insn_dependent_p_1, &tmp);
a8393d5d
RH
837 if (tmp == NULL_RTX)
838 return 1;
839
840 tmp = PATTERN (x);
4eb00163 841 note_stores (PATTERN (y), insn_dependent_p_1, &tmp);
a8393d5d
RH
842 if (tmp == NULL_RTX)
843 return 1;
844
845 return 0;
846}
847
4eb00163 848/* A helper routine for insn_dependent_p called through note_stores. */
a8393d5d
RH
849
850static void
4eb00163 851insn_dependent_p_1 (x, pat, data)
a8393d5d
RH
852 rtx x;
853 rtx pat ATTRIBUTE_UNUSED;
854 void *data;
855{
856 rtx * pinsn = (rtx *) data;
857
858 if (*pinsn && reg_mentioned_p (x, *pinsn))
859 *pinsn = NULL_RTX;
860}
2c88418c 861\f
91b2d119
JH
862/* Helper function for set_of. */
863struct set_of_data
864 {
865 rtx found;
866 rtx pat;
867 };
868
869static void
870set_of_1 (x, pat, data1)
871 rtx x;
872 rtx pat;
873 void *data1;
874{
875 struct set_of_data *data = (struct set_of_data *) (data1);
876 if (rtx_equal_p (x, data->pat)
877 || (GET_CODE (x) != MEM && reg_overlap_mentioned_p (data->pat, x)))
878 data->found = pat;
879}
880
881/* Give an INSN, return a SET or CLOBBER expression that does modify PAT
882 (eighter directly or via STRICT_LOW_PART and similar modifiers). */
883rtx
884set_of (pat, insn)
885 rtx pat, insn;
886{
887 struct set_of_data data;
888 data.found = NULL_RTX;
889 data.pat = pat;
890 note_stores (INSN_P (insn) ? PATTERN (insn) : insn, set_of_1, &data);
891 return data.found;
892}
893\f
2c88418c
RS
894/* Given an INSN, return a SET expression if this insn has only a single SET.
895 It may also have CLOBBERs, USEs, or SET whose output
896 will not be used, which we ignore. */
897
898rtx
2130b7fb
BS
899single_set_2 (insn, pat)
900 rtx insn, pat;
2c88418c 901{
c9b89a21
JH
902 rtx set = NULL;
903 int set_verified = 1;
2c88418c 904 int i;
c9b89a21 905
b1cdafbb 906 if (GET_CODE (pat) == PARALLEL)
2c88418c 907 {
c9b89a21 908 for (i = 0; i < XVECLEN (pat, 0); i++)
b1cdafbb 909 {
c9b89a21
JH
910 rtx sub = XVECEXP (pat, 0, i);
911 switch (GET_CODE (sub))
912 {
913 case USE:
914 case CLOBBER:
915 break;
916
917 case SET:
918 /* We can consider insns having multiple sets, where all
919 but one are dead as single set insns. In common case
920 only single set is present in the pattern so we want
921 to avoid checking for REG_UNUSED notes unless neccesary.
922
923 When we reach set first time, we just expect this is
924 the single set we are looking for and only when more
925 sets are found in the insn, we check them. */
926 if (!set_verified)
927 {
928 if (find_reg_note (insn, REG_UNUSED, SET_DEST (set))
929 && !side_effects_p (set))
930 set = NULL;
931 else
932 set_verified = 1;
933 }
934 if (!set)
935 set = sub, set_verified = 0;
936 else if (!find_reg_note (insn, REG_UNUSED, SET_DEST (sub))
937 || side_effects_p (sub))
938 return NULL_RTX;
939 break;
940
941 default:
942 return NULL_RTX;
943 }
787ccee0 944 }
2c88418c 945 }
c9b89a21 946 return set;
2c88418c 947}
941c63ac
JL
948
949/* Given an INSN, return nonzero if it has more than one SET, else return
950 zero. */
951
5f7d3786 952int
941c63ac
JL
953multiple_sets (insn)
954 rtx insn;
955{
cae8acdd 956 int found;
941c63ac
JL
957 int i;
958
959 /* INSN must be an insn. */
2c3c49de 960 if (! INSN_P (insn))
941c63ac
JL
961 return 0;
962
963 /* Only a PARALLEL can have multiple SETs. */
964 if (GET_CODE (PATTERN (insn)) == PARALLEL)
965 {
966 for (i = 0, found = 0; i < XVECLEN (PATTERN (insn), 0); i++)
967 if (GET_CODE (XVECEXP (PATTERN (insn), 0, i)) == SET)
968 {
969 /* If we have already found a SET, then return now. */
970 if (found)
971 return 1;
972 else
973 found = 1;
974 }
975 }
976
977 /* Either zero or one SET. */
978 return 0;
979}
2c88418c 980\f
7142e318
JW
981/* Return nonzero if the destination of SET equals the source
982 and there are no side effects. */
983
984int
985set_noop_p (set)
986 rtx set;
987{
988 rtx src = SET_SRC (set);
989 rtx dst = SET_DEST (set);
990
991 if (side_effects_p (src) || side_effects_p (dst))
992 return 0;
993
994 if (GET_CODE (dst) == MEM && GET_CODE (src) == MEM)
995 return rtx_equal_p (dst, src);
996
997 if (GET_CODE (dst) == SIGN_EXTRACT
998 || GET_CODE (dst) == ZERO_EXTRACT)
999 return rtx_equal_p (XEXP (dst, 0), src)
1000 && ! BYTES_BIG_ENDIAN && XEXP (dst, 2) == const0_rtx;
1001
1002 if (GET_CODE (dst) == STRICT_LOW_PART)
1003 dst = XEXP (dst, 0);
1004
1005 if (GET_CODE (src) == SUBREG && GET_CODE (dst) == SUBREG)
1006 {
1007 if (SUBREG_BYTE (src) != SUBREG_BYTE (dst))
1008 return 0;
1009 src = SUBREG_REG (src);
1010 dst = SUBREG_REG (dst);
1011 }
1012
1013 return (GET_CODE (src) == REG && GET_CODE (dst) == REG
1014 && REGNO (src) == REGNO (dst));
1015}
1016
63be01fb
JW
1017/* Return the last thing that X was assigned from before *PINSN. If VALID_TO
1018 is not NULL_RTX then verify that the object is not modified up to VALID_TO.
1019 If the object was modified, if we hit a partial assignment to X, or hit a
1020 CODE_LABEL first, return X. If we found an assignment, update *PINSN to
1021 point to it. ALLOW_HWREG is set to 1 if hardware registers are allowed to
1022 be the src. */
2c88418c
RS
1023
1024rtx
89d3d442 1025find_last_value (x, pinsn, valid_to, allow_hwreg)
2c88418c
RS
1026 rtx x;
1027 rtx *pinsn;
1028 rtx valid_to;
89d3d442 1029 int allow_hwreg;
2c88418c
RS
1030{
1031 rtx p;
1032
1033 for (p = PREV_INSN (*pinsn); p && GET_CODE (p) != CODE_LABEL;
1034 p = PREV_INSN (p))
2c3c49de 1035 if (INSN_P (p))
2c88418c
RS
1036 {
1037 rtx set = single_set (p);
c166a311 1038 rtx note = find_reg_note (p, REG_EQUAL, NULL_RTX);
2c88418c
RS
1039
1040 if (set && rtx_equal_p (x, SET_DEST (set)))
1041 {
1042 rtx src = SET_SRC (set);
1043
1044 if (note && GET_CODE (XEXP (note, 0)) != EXPR_LIST)
1045 src = XEXP (note, 0);
1046
63be01fb
JW
1047 if ((valid_to == NULL_RTX
1048 || ! modified_between_p (src, PREV_INSN (p), valid_to))
2c88418c
RS
1049 /* Reject hard registers because we don't usually want
1050 to use them; we'd rather use a pseudo. */
89d3d442
AM
1051 && (! (GET_CODE (src) == REG
1052 && REGNO (src) < FIRST_PSEUDO_REGISTER) || allow_hwreg))
2c88418c
RS
1053 {
1054 *pinsn = p;
1055 return src;
1056 }
1057 }
1058
1059 /* If set in non-simple way, we don't have a value. */
1060 if (reg_set_p (x, p))
1061 break;
1062 }
1063
1064 return x;
1065}
1066\f
1067/* Return nonzero if register in range [REGNO, ENDREGNO)
1068 appears either explicitly or implicitly in X
1069 other than being stored into.
1070
1071 References contained within the substructure at LOC do not count.
1072 LOC may be zero, meaning don't ignore anything. */
1073
1074int
1075refers_to_regno_p (regno, endregno, x, loc)
770ae6cc 1076 unsigned int regno, endregno;
2c88418c
RS
1077 rtx x;
1078 rtx *loc;
1079{
770ae6cc
RK
1080 int i;
1081 unsigned int x_regno;
1082 RTX_CODE code;
1083 const char *fmt;
2c88418c
RS
1084
1085 repeat:
1086 /* The contents of a REG_NONNEG note is always zero, so we must come here
1087 upon repeat in case the last REG_NOTE is a REG_NONNEG note. */
1088 if (x == 0)
1089 return 0;
1090
1091 code = GET_CODE (x);
1092
1093 switch (code)
1094 {
1095 case REG:
770ae6cc 1096 x_regno = REGNO (x);
f8163c92
RK
1097
1098 /* If we modifying the stack, frame, or argument pointer, it will
1099 clobber a virtual register. In fact, we could be more precise,
1100 but it isn't worth it. */
770ae6cc 1101 if ((x_regno == STACK_POINTER_REGNUM
f8163c92 1102#if FRAME_POINTER_REGNUM != ARG_POINTER_REGNUM
770ae6cc 1103 || x_regno == ARG_POINTER_REGNUM
f8163c92 1104#endif
770ae6cc 1105 || x_regno == FRAME_POINTER_REGNUM)
f8163c92
RK
1106 && regno >= FIRST_VIRTUAL_REGISTER && regno <= LAST_VIRTUAL_REGISTER)
1107 return 1;
1108
770ae6cc
RK
1109 return (endregno > x_regno
1110 && regno < x_regno + (x_regno < FIRST_PSEUDO_REGISTER
1111 ? HARD_REGNO_NREGS (x_regno, GET_MODE (x))
2c88418c
RS
1112 : 1));
1113
1114 case SUBREG:
1115 /* If this is a SUBREG of a hard reg, we can see exactly which
1116 registers are being modified. Otherwise, handle normally. */
1117 if (GET_CODE (SUBREG_REG (x)) == REG
1118 && REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER)
1119 {
ddef6bc7 1120 unsigned int inner_regno = subreg_regno (x);
770ae6cc 1121 unsigned int inner_endregno
2c88418c
RS
1122 = inner_regno + (inner_regno < FIRST_PSEUDO_REGISTER
1123 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
1124
1125 return endregno > inner_regno && regno < inner_endregno;
1126 }
1127 break;
1128
1129 case CLOBBER:
1130 case SET:
1131 if (&SET_DEST (x) != loc
1132 /* Note setting a SUBREG counts as referring to the REG it is in for
1133 a pseudo but not for hard registers since we can
1134 treat each word individually. */
1135 && ((GET_CODE (SET_DEST (x)) == SUBREG
1136 && loc != &SUBREG_REG (SET_DEST (x))
1137 && GET_CODE (SUBREG_REG (SET_DEST (x))) == REG
1138 && REGNO (SUBREG_REG (SET_DEST (x))) >= FIRST_PSEUDO_REGISTER
1139 && refers_to_regno_p (regno, endregno,
1140 SUBREG_REG (SET_DEST (x)), loc))
1141 || (GET_CODE (SET_DEST (x)) != REG
1142 && refers_to_regno_p (regno, endregno, SET_DEST (x), loc))))
1143 return 1;
1144
1145 if (code == CLOBBER || loc == &SET_SRC (x))
1146 return 0;
1147 x = SET_SRC (x);
1148 goto repeat;
e9a25f70
JL
1149
1150 default:
1151 break;
2c88418c
RS
1152 }
1153
1154 /* X does not match, so try its subexpressions. */
1155
1156 fmt = GET_RTX_FORMAT (code);
1157 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1158 {
1159 if (fmt[i] == 'e' && loc != &XEXP (x, i))
1160 {
1161 if (i == 0)
1162 {
1163 x = XEXP (x, 0);
1164 goto repeat;
1165 }
1166 else
1167 if (refers_to_regno_p (regno, endregno, XEXP (x, i), loc))
1168 return 1;
1169 }
1170 else if (fmt[i] == 'E')
1171 {
1172 register int j;
1173 for (j = XVECLEN (x, i) - 1; j >=0; j--)
1174 if (loc != &XVECEXP (x, i, j)
1175 && refers_to_regno_p (regno, endregno, XVECEXP (x, i, j), loc))
1176 return 1;
1177 }
1178 }
1179 return 0;
1180}
1181
1182/* Nonzero if modifying X will affect IN. If X is a register or a SUBREG,
1183 we check if any register number in X conflicts with the relevant register
1184 numbers. If X is a constant, return 0. If X is a MEM, return 1 iff IN
1185 contains a MEM (we don't bother checking for memory addresses that can't
1186 conflict because we expect this to be a rare case. */
1187
1188int
1189reg_overlap_mentioned_p (x, in)
1190 rtx x, in;
1191{
770ae6cc 1192 unsigned int regno, endregno;
2c88418c 1193
b98b49ac
JL
1194 /* Overly conservative. */
1195 if (GET_CODE (x) == STRICT_LOW_PART)
1196 x = XEXP (x, 0);
1197
1198 /* If either argument is a constant, then modifying X can not affect IN. */
1199 if (CONSTANT_P (x) || CONSTANT_P (in))
1200 return 0;
0c99ec5c
RH
1201
1202 switch (GET_CODE (x))
2c88418c 1203 {
0c99ec5c 1204 case SUBREG:
2c88418c
RS
1205 regno = REGNO (SUBREG_REG (x));
1206 if (regno < FIRST_PSEUDO_REGISTER)
ddef6bc7 1207 regno = subreg_regno (x);
0c99ec5c 1208 goto do_reg;
2c88418c 1209
0c99ec5c
RH
1210 case REG:
1211 regno = REGNO (x);
1212 do_reg:
1213 endregno = regno + (regno < FIRST_PSEUDO_REGISTER
1214 ? HARD_REGNO_NREGS (regno, GET_MODE (x)) : 1);
9714cf43 1215 return refers_to_regno_p (regno, endregno, in, (rtx*)0);
2c88418c 1216
0c99ec5c
RH
1217 case MEM:
1218 {
1219 const char *fmt;
1220 int i;
2c88418c 1221
0c99ec5c 1222 if (GET_CODE (in) == MEM)
2c88418c
RS
1223 return 1;
1224
0c99ec5c
RH
1225 fmt = GET_RTX_FORMAT (GET_CODE (in));
1226 for (i = GET_RTX_LENGTH (GET_CODE (in)) - 1; i >= 0; i--)
1227 if (fmt[i] == 'e' && reg_overlap_mentioned_p (x, XEXP (in, i)))
1228 return 1;
c0222c21 1229
0c99ec5c
RH
1230 return 0;
1231 }
1232
1233 case SCRATCH:
1234 case PC:
1235 case CC0:
1236 return reg_mentioned_p (x, in);
1237
1238 case PARALLEL:
37ceff9d 1239 {
90d036a0 1240 int i;
37ceff9d
RH
1241
1242 /* If any register in here refers to it we return true. */
7193d1dc
RK
1243 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1244 if (XEXP (XVECEXP (x, 0, i), 0) != 0
1245 && reg_overlap_mentioned_p (XEXP (XVECEXP (x, 0, i), 0), in))
90d036a0 1246 return 1;
7193d1dc 1247 return 0;
37ceff9d 1248 }
2c88418c 1249
0c99ec5c
RH
1250 default:
1251 break;
1252 }
2c88418c 1253
0c99ec5c 1254 abort ();
2c88418c
RS
1255}
1256\f
2c88418c
RS
1257/* Return the last value to which REG was set prior to INSN. If we can't
1258 find it easily, return 0.
1259
4d9d7d9d
RK
1260 We only return a REG, SUBREG, or constant because it is too hard to
1261 check if a MEM remains unchanged. */
2c88418c
RS
1262
1263rtx
1264reg_set_last (x, insn)
1265 rtx x;
1266 rtx insn;
1267{
1268 rtx orig_insn = insn;
1269
2c88418c
RS
1270 /* Scan backwards until reg_set_last_1 changed one of the above flags.
1271 Stop when we reach a label or X is a hard reg and we reach a
1272 CALL_INSN (if reg_set_last_last_regno is a hard reg).
1273
1274 If we find a set of X, ensure that its SET_SRC remains unchanged. */
1275
6b02c316
RS
1276 /* We compare with <= here, because reg_set_last_last_regno
1277 is actually the number of the first reg *not* in X. */
2c88418c
RS
1278 for (;
1279 insn && GET_CODE (insn) != CODE_LABEL
1280 && ! (GET_CODE (insn) == CALL_INSN
91b2d119 1281 && REGNO (x) <= FIRST_PSEUDO_REGISTER);
2c88418c 1282 insn = PREV_INSN (insn))
2c3c49de 1283 if (INSN_P (insn))
2c88418c 1284 {
91b2d119
JH
1285 rtx set = set_of (x, insn);
1286 /* OK, this function modify our register. See if we understand it. */
1287 if (set)
2c88418c 1288 {
91b2d119
JH
1289 rtx last_value;
1290 if (GET_CODE (set) != SET || SET_DEST (set) != x)
1291 return 0;
1292 last_value = SET_SRC (x);
1293 if (CONSTANT_P (last_value)
1294 || ((GET_CODE (last_value) == REG
1295 || GET_CODE (last_value) == SUBREG)
1296 && ! reg_set_between_p (last_value,
ce9c8df2 1297 insn, orig_insn)))
91b2d119 1298 return last_value;
2c88418c
RS
1299 else
1300 return 0;
1301 }
1302 }
1303
1304 return 0;
1305}
1306\f
2c88418c
RS
1307/* Call FUN on each register or MEM that is stored into or clobbered by X.
1308 (X would be the pattern of an insn).
1309 FUN receives two arguments:
1310 the REG, MEM, CC0 or PC being stored in or clobbered,
1311 the SET or CLOBBER rtx that does the store.
1312
1313 If the item being stored in or clobbered is a SUBREG of a hard register,
1314 the SUBREG will be passed. */
1315
1316void
84832317 1317note_stores (x, fun, data)
2c88418c 1318 register rtx x;
cdadb1dd 1319 void (*fun) PARAMS ((rtx, rtx, void *));
84832317 1320 void *data;
2c88418c 1321{
90d036a0
RK
1322 int i;
1323
0c99ec5c
RH
1324 if (GET_CODE (x) == COND_EXEC)
1325 x = COND_EXEC_CODE (x);
90d036a0 1326
0c99ec5c 1327 if (GET_CODE (x) == SET || GET_CODE (x) == CLOBBER)
2c88418c
RS
1328 {
1329 register rtx dest = SET_DEST (x);
90d036a0 1330
2c88418c
RS
1331 while ((GET_CODE (dest) == SUBREG
1332 && (GET_CODE (SUBREG_REG (dest)) != REG
1333 || REGNO (SUBREG_REG (dest)) >= FIRST_PSEUDO_REGISTER))
1334 || GET_CODE (dest) == ZERO_EXTRACT
1335 || GET_CODE (dest) == SIGN_EXTRACT
1336 || GET_CODE (dest) == STRICT_LOW_PART)
1337 dest = XEXP (dest, 0);
86465af7 1338
7193d1dc
RK
1339 /* If we have a PARALLEL, SET_DEST is a list of EXPR_LIST expressions,
1340 each of whose first operand is a register. We can't know what
1341 precisely is being set in these cases, so make up a CLOBBER to pass
1342 to the function. */
1343 if (GET_CODE (dest) == PARALLEL)
1344 {
1345 for (i = XVECLEN (dest, 0) - 1; i >= 0; i--)
1346 if (XEXP (XVECEXP (dest, 0, i), 0) != 0)
1347 (*fun) (XEXP (XVECEXP (dest, 0, i), 0),
1348 gen_rtx_CLOBBER (VOIDmode,
1349 XEXP (XVECEXP (dest, 0, i), 0)),
1350 data);
1351 }
86465af7 1352 else
84832317 1353 (*fun) (dest, x, data);
2c88418c 1354 }
770ae6cc 1355
90d036a0
RK
1356 else if (GET_CODE (x) == PARALLEL)
1357 for (i = XVECLEN (x, 0) - 1; i >= 0; i--)
1358 note_stores (XVECEXP (x, 0, i), fun, data);
2c88418c
RS
1359}
1360\f
e2373f95
RK
1361/* Like notes_stores, but call FUN for each expression that is being
1362 referenced in PBODY, a pointer to the PATTERN of an insn. We only call
1363 FUN for each expression, not any interior subexpressions. FUN receives a
1364 pointer to the expression and the DATA passed to this function.
1365
1366 Note that this is not quite the same test as that done in reg_referenced_p
1367 since that considers something as being referenced if it is being
1368 partially set, while we do not. */
1369
1370void
1371note_uses (pbody, fun, data)
1372 rtx *pbody;
1373 void (*fun) PARAMS ((rtx *, void *));
1374 void *data;
1375{
1376 rtx body = *pbody;
1377 int i;
1378
1379 switch (GET_CODE (body))
1380 {
1381 case COND_EXEC:
1382 (*fun) (&COND_EXEC_TEST (body), data);
1383 note_uses (&COND_EXEC_CODE (body), fun, data);
1384 return;
1385
1386 case PARALLEL:
1387 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1388 note_uses (&XVECEXP (body, 0, i), fun, data);
1389 return;
1390
1391 case USE:
1392 (*fun) (&XEXP (body, 0), data);
1393 return;
1394
1395 case ASM_OPERANDS:
1396 for (i = ASM_OPERANDS_INPUT_LENGTH (body) - 1; i >= 0; i--)
1397 (*fun) (&ASM_OPERANDS_INPUT (body, i), data);
1398 return;
1399
1400 case TRAP_IF:
1401 (*fun) (&TRAP_CONDITION (body), data);
1402 return;
1403
1404 case UNSPEC:
1405 case UNSPEC_VOLATILE:
1406 for (i = XVECLEN (body, 0) - 1; i >= 0; i--)
1407 (*fun) (&XVECEXP (body, 0, i), data);
1408 return;
1409
1410 case CLOBBER:
1411 if (GET_CODE (XEXP (body, 0)) == MEM)
1412 (*fun) (&XEXP (XEXP (body, 0), 0), data);
1413 return;
1414
1415 case SET:
1416 {
1417 rtx dest = SET_DEST (body);
1418
1419 /* For sets we replace everything in source plus registers in memory
1420 expression in store and operands of a ZERO_EXTRACT. */
1421 (*fun) (&SET_SRC (body), data);
1422
1423 if (GET_CODE (dest) == ZERO_EXTRACT)
1424 {
1425 (*fun) (&XEXP (dest, 1), data);
1426 (*fun) (&XEXP (dest, 2), data);
1427 }
1428
1429 while (GET_CODE (dest) == SUBREG || GET_CODE (dest) == STRICT_LOW_PART)
1430 dest = XEXP (dest, 0);
1431
1432 if (GET_CODE (dest) == MEM)
1433 (*fun) (&XEXP (dest, 0), data);
1434 }
1435 return;
1436
1437 default:
1438 /* All the other possibilities never store. */
1439 (*fun) (pbody, data);
1440 return;
1441 }
1442}
1443\f
2c88418c
RS
1444/* Return nonzero if X's old contents don't survive after INSN.
1445 This will be true if X is (cc0) or if X is a register and
1446 X dies in INSN or because INSN entirely sets X.
1447
1448 "Entirely set" means set directly and not through a SUBREG,
1449 ZERO_EXTRACT or SIGN_EXTRACT, so no trace of the old contents remains.
1450 Likewise, REG_INC does not count.
1451
1452 REG may be a hard or pseudo reg. Renumbering is not taken into account,
1453 but for this use that makes no difference, since regs don't overlap
1454 during their lifetimes. Therefore, this function may be used
1455 at any time after deaths have been computed (in flow.c).
1456
1457 If REG is a hard reg that occupies multiple machine registers, this
1458 function will only return 1 if each of those registers will be replaced
1459 by INSN. */
1460
1461int
1462dead_or_set_p (insn, x)
1463 rtx insn;
1464 rtx x;
1465{
770ae6cc
RK
1466 unsigned int regno, last_regno;
1467 unsigned int i;
2c88418c
RS
1468
1469 /* Can't use cc0_rtx below since this file is used by genattrtab.c. */
1470 if (GET_CODE (x) == CC0)
1471 return 1;
1472
1473 if (GET_CODE (x) != REG)
1474 abort ();
1475
1476 regno = REGNO (x);
1477 last_regno = (regno >= FIRST_PSEUDO_REGISTER ? regno
1478 : regno + HARD_REGNO_NREGS (regno, GET_MODE (x)) - 1);
1479
1480 for (i = regno; i <= last_regno; i++)
1481 if (! dead_or_set_regno_p (insn, i))
1482 return 0;
1483
1484 return 1;
1485}
1486
1487/* Utility function for dead_or_set_p to check an individual register. Also
1488 called from flow.c. */
1489
1490int
1491dead_or_set_regno_p (insn, test_regno)
1492 rtx insn;
770ae6cc 1493 unsigned int test_regno;
2c88418c 1494{
770ae6cc 1495 unsigned int regno, endregno;
8c8de5fc 1496 rtx pattern;
2c88418c 1497
0a2287bf
RH
1498 /* See if there is a death note for something that includes TEST_REGNO. */
1499 if (find_regno_note (insn, REG_DEAD, test_regno))
1500 return 1;
2c88418c 1501
8f3e7a26
RK
1502 if (GET_CODE (insn) == CALL_INSN
1503 && find_regno_fusage (insn, CLOBBER, test_regno))
1504 return 1;
1505
0c99ec5c
RH
1506 pattern = PATTERN (insn);
1507
1508 if (GET_CODE (pattern) == COND_EXEC)
1509 pattern = COND_EXEC_CODE (pattern);
1510
1511 if (GET_CODE (pattern) == SET)
2c88418c
RS
1512 {
1513 rtx dest = SET_DEST (PATTERN (insn));
1514
1515 /* A value is totally replaced if it is the destination or the
1516 destination is a SUBREG of REGNO that does not change the number of
1517 words in it. */
6764d250 1518 if (GET_CODE (dest) == SUBREG
2c88418c
RS
1519 && (((GET_MODE_SIZE (GET_MODE (dest))
1520 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1521 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1522 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1523 dest = SUBREG_REG (dest);
1524
1525 if (GET_CODE (dest) != REG)
1526 return 0;
1527
1528 regno = REGNO (dest);
1529 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1530 : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1531
1532 return (test_regno >= regno && test_regno < endregno);
1533 }
0c99ec5c 1534 else if (GET_CODE (pattern) == PARALLEL)
2c88418c
RS
1535 {
1536 register int i;
1537
0c99ec5c 1538 for (i = XVECLEN (pattern, 0) - 1; i >= 0; i--)
2c88418c 1539 {
0c99ec5c
RH
1540 rtx body = XVECEXP (pattern, 0, i);
1541
1542 if (GET_CODE (body) == COND_EXEC)
1543 body = COND_EXEC_CODE (body);
2c88418c
RS
1544
1545 if (GET_CODE (body) == SET || GET_CODE (body) == CLOBBER)
1546 {
1547 rtx dest = SET_DEST (body);
1548
1549 if (GET_CODE (dest) == SUBREG
1550 && (((GET_MODE_SIZE (GET_MODE (dest))
1551 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)
1552 == ((GET_MODE_SIZE (GET_MODE (SUBREG_REG (dest)))
1553 + UNITS_PER_WORD - 1) / UNITS_PER_WORD)))
1554 dest = SUBREG_REG (dest);
1555
1556 if (GET_CODE (dest) != REG)
1557 continue;
1558
1559 regno = REGNO (dest);
1560 endregno = (regno >= FIRST_PSEUDO_REGISTER ? regno + 1
1561 : regno + HARD_REGNO_NREGS (regno, GET_MODE (dest)));
1562
1563 if (test_regno >= regno && test_regno < endregno)
1564 return 1;
1565 }
1566 }
1567 }
1568
1569 return 0;
1570}
1571
1572/* Return the reg-note of kind KIND in insn INSN, if there is one.
1573 If DATUM is nonzero, look for one whose datum is DATUM. */
1574
1575rtx
1576find_reg_note (insn, kind, datum)
1577 rtx insn;
1578 enum reg_note kind;
1579 rtx datum;
1580{
1581 register rtx link;
1582
ae78d276 1583 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
2c3c49de 1584 if (! INSN_P (insn))
ae78d276
MM
1585 return 0;
1586
2c88418c
RS
1587 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1588 if (REG_NOTE_KIND (link) == kind
1589 && (datum == 0 || datum == XEXP (link, 0)))
1590 return link;
1591 return 0;
1592}
1593
1594/* Return the reg-note of kind KIND in insn INSN which applies to register
99309f3b
RK
1595 number REGNO, if any. Return 0 if there is no such reg-note. Note that
1596 the REGNO of this NOTE need not be REGNO if REGNO is a hard register;
1597 it might be the case that the note overlaps REGNO. */
2c88418c
RS
1598
1599rtx
1600find_regno_note (insn, kind, regno)
1601 rtx insn;
1602 enum reg_note kind;
770ae6cc 1603 unsigned int regno;
2c88418c
RS
1604{
1605 register rtx link;
1606
ae78d276 1607 /* Ignore anything that is not an INSN, JUMP_INSN or CALL_INSN. */
2c3c49de 1608 if (! INSN_P (insn))
ae78d276
MM
1609 return 0;
1610
2c88418c
RS
1611 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1612 if (REG_NOTE_KIND (link) == kind
1613 /* Verify that it is a register, so that scratch and MEM won't cause a
1614 problem here. */
1615 && GET_CODE (XEXP (link, 0)) == REG
99309f3b
RK
1616 && REGNO (XEXP (link, 0)) <= regno
1617 && ((REGNO (XEXP (link, 0))
1618 + (REGNO (XEXP (link, 0)) >= FIRST_PSEUDO_REGISTER ? 1
1619 : HARD_REGNO_NREGS (REGNO (XEXP (link, 0)),
1620 GET_MODE (XEXP (link, 0)))))
1621 > regno))
2c88418c
RS
1622 return link;
1623 return 0;
1624}
8f3e7a26 1625
d9c695ff
RK
1626/* Return a REG_EQUIV or REG_EQUAL note if insn has only a single set and
1627 has such a note. */
1628
1629rtx
1630find_reg_equal_equiv_note (insn)
1631 rtx insn;
1632{
1633 rtx note;
1634
1635 if (single_set (insn) == 0)
1636 return 0;
1637 else if ((note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
1638 return note;
1639 else
1640 return find_reg_note (insn, REG_EQUAL, NULL_RTX);
1641}
1642
8f3e7a26
RK
1643/* Return true if DATUM, or any overlap of DATUM, of kind CODE is found
1644 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1645
1646int
1647find_reg_fusage (insn, code, datum)
1648 rtx insn;
1649 enum rtx_code code;
1650 rtx datum;
1651{
1652 /* If it's not a CALL_INSN, it can't possibly have a
1653 CALL_INSN_FUNCTION_USAGE field, so don't bother checking. */
1654 if (GET_CODE (insn) != CALL_INSN)
1655 return 0;
1656
1657 if (! datum)
1658 abort();
1659
1660 if (GET_CODE (datum) != REG)
1661 {
1662 register rtx link;
1663
1664 for (link = CALL_INSN_FUNCTION_USAGE (insn);
1665 link;
1666 link = XEXP (link, 1))
1667 if (GET_CODE (XEXP (link, 0)) == code
1668 && rtx_equal_p (datum, SET_DEST (XEXP (link, 0))))
1669 return 1;
1670 }
1671 else
1672 {
770ae6cc 1673 unsigned int regno = REGNO (datum);
8f3e7a26
RK
1674
1675 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1676 to pseudo registers, so don't bother checking. */
1677
1678 if (regno < FIRST_PSEUDO_REGISTER)
1679 {
770ae6cc
RK
1680 unsigned int end_regno
1681 = regno + HARD_REGNO_NREGS (regno, GET_MODE (datum));
1682 unsigned int i;
8f3e7a26
RK
1683
1684 for (i = regno; i < end_regno; i++)
1685 if (find_regno_fusage (insn, code, i))
1686 return 1;
1687 }
1688 }
1689
1690 return 0;
1691}
1692
1693/* Return true if REGNO, or any overlap of REGNO, of kind CODE is found
1694 in the CALL_INSN_FUNCTION_USAGE information of INSN. */
1695
1696int
1697find_regno_fusage (insn, code, regno)
1698 rtx insn;
1699 enum rtx_code code;
770ae6cc 1700 unsigned int regno;
8f3e7a26
RK
1701{
1702 register rtx link;
1703
1704 /* CALL_INSN_FUNCTION_USAGE information cannot contain references
1705 to pseudo registers, so don't bother checking. */
1706
1707 if (regno >= FIRST_PSEUDO_REGISTER
1708 || GET_CODE (insn) != CALL_INSN )
1709 return 0;
1710
1711 for (link = CALL_INSN_FUNCTION_USAGE (insn); link; link = XEXP (link, 1))
83ab3839 1712 {
770ae6cc
RK
1713 unsigned int regnote;
1714 rtx op, reg;
83ab3839
RH
1715
1716 if (GET_CODE (op = XEXP (link, 0)) == code
1717 && GET_CODE (reg = XEXP (op, 0)) == REG
1718 && (regnote = REGNO (reg)) <= regno
1719 && regnote + HARD_REGNO_NREGS (regnote, GET_MODE (reg)) > regno)
1720 return 1;
1721 }
8f3e7a26
RK
1722
1723 return 0;
1724}
2c88418c
RS
1725\f
1726/* Remove register note NOTE from the REG_NOTES of INSN. */
1727
1728void
1729remove_note (insn, note)
2c88418c 1730 register rtx insn;
49c3bb12 1731 register rtx note;
2c88418c
RS
1732{
1733 register rtx link;
1734
49c3bb12
RH
1735 if (note == NULL_RTX)
1736 return;
1737
2c88418c
RS
1738 if (REG_NOTES (insn) == note)
1739 {
1740 REG_NOTES (insn) = XEXP (note, 1);
1741 return;
1742 }
1743
1744 for (link = REG_NOTES (insn); link; link = XEXP (link, 1))
1745 if (XEXP (link, 1) == note)
1746 {
1747 XEXP (link, 1) = XEXP (note, 1);
1748 return;
1749 }
1750
1751 abort ();
1752}
55a98783 1753
dd248abd
RK
1754/* Search LISTP (an EXPR_LIST) for an entry whose first operand is NODE and
1755 remove that entry from the list if it is found.
55a98783 1756
dd248abd 1757 A simple equality test is used to determine if NODE matches. */
55a98783
JL
1758
1759void
1760remove_node_from_expr_list (node, listp)
1761 rtx node;
1762 rtx *listp;
1763{
1764 rtx temp = *listp;
1765 rtx prev = NULL_RTX;
1766
1767 while (temp)
1768 {
1769 if (node == XEXP (temp, 0))
1770 {
1771 /* Splice the node out of the list. */
1772 if (prev)
1773 XEXP (prev, 1) = XEXP (temp, 1);
1774 else
1775 *listp = XEXP (temp, 1);
1776
1777 return;
1778 }
dd248abd
RK
1779
1780 prev = temp;
55a98783
JL
1781 temp = XEXP (temp, 1);
1782 }
1783}
2c88418c 1784\f
2b067faf
RS
1785/* Nonzero if X contains any volatile instructions. These are instructions
1786 which may cause unpredictable machine state instructions, and thus no
1787 instructions should be moved or combined across them. This includes
1788 only volatile asms and UNSPEC_VOLATILE instructions. */
1789
1790int
1791volatile_insn_p (x)
1792 rtx x;
1793{
1794 register RTX_CODE code;
1795
1796 code = GET_CODE (x);
1797 switch (code)
1798 {
1799 case LABEL_REF:
1800 case SYMBOL_REF:
1801 case CONST_INT:
1802 case CONST:
1803 case CONST_DOUBLE:
1804 case CC0:
1805 case PC:
1806 case REG:
1807 case SCRATCH:
1808 case CLOBBER:
1809 case ASM_INPUT:
1810 case ADDR_VEC:
1811 case ADDR_DIFF_VEC:
1812 case CALL:
1813 case MEM:
1814 return 0;
1815
1816 case UNSPEC_VOLATILE:
1817 /* case TRAP_IF: This isn't clear yet. */
1818 return 1;
1819
1820 case ASM_OPERANDS:
1821 if (MEM_VOLATILE_P (x))
1822 return 1;
e9a25f70
JL
1823
1824 default:
1825 break;
2b067faf
RS
1826 }
1827
1828 /* Recursively scan the operands of this expression. */
1829
1830 {
6f7d635c 1831 register const char *fmt = GET_RTX_FORMAT (code);
2b067faf
RS
1832 register int i;
1833
1834 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1835 {
1836 if (fmt[i] == 'e')
1837 {
31001f72 1838 if (volatile_insn_p (XEXP (x, i)))
2b067faf
RS
1839 return 1;
1840 }
d4757e6a 1841 else if (fmt[i] == 'E')
2b067faf
RS
1842 {
1843 register int j;
1844 for (j = 0; j < XVECLEN (x, i); j++)
31001f72 1845 if (volatile_insn_p (XVECEXP (x, i, j)))
2b067faf
RS
1846 return 1;
1847 }
1848 }
1849 }
1850 return 0;
1851}
1852
2c88418c 1853/* Nonzero if X contains any volatile memory references
2ac4fed0 1854 UNSPEC_VOLATILE operations or volatile ASM_OPERANDS expressions. */
2c88418c
RS
1855
1856int
1857volatile_refs_p (x)
1858 rtx x;
1859{
1860 register RTX_CODE code;
1861
1862 code = GET_CODE (x);
1863 switch (code)
1864 {
1865 case LABEL_REF:
1866 case SYMBOL_REF:
1867 case CONST_INT:
1868 case CONST:
1869 case CONST_DOUBLE:
1870 case CC0:
1871 case PC:
1872 case REG:
1873 case SCRATCH:
1874 case CLOBBER:
1875 case ASM_INPUT:
1876 case ADDR_VEC:
1877 case ADDR_DIFF_VEC:
1878 return 0;
1879
1880 case CALL:
2ac4fed0 1881 case UNSPEC_VOLATILE:
2c88418c
RS
1882 /* case TRAP_IF: This isn't clear yet. */
1883 return 1;
1884
1885 case MEM:
1886 case ASM_OPERANDS:
1887 if (MEM_VOLATILE_P (x))
1888 return 1;
e9a25f70
JL
1889
1890 default:
1891 break;
2c88418c
RS
1892 }
1893
1894 /* Recursively scan the operands of this expression. */
1895
1896 {
6f7d635c 1897 register const char *fmt = GET_RTX_FORMAT (code);
2c88418c
RS
1898 register int i;
1899
1900 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1901 {
1902 if (fmt[i] == 'e')
1903 {
1904 if (volatile_refs_p (XEXP (x, i)))
1905 return 1;
1906 }
d4757e6a 1907 else if (fmt[i] == 'E')
2c88418c
RS
1908 {
1909 register int j;
1910 for (j = 0; j < XVECLEN (x, i); j++)
1911 if (volatile_refs_p (XVECEXP (x, i, j)))
1912 return 1;
1913 }
1914 }
1915 }
1916 return 0;
1917}
1918
1919/* Similar to above, except that it also rejects register pre- and post-
1920 incrementing. */
1921
1922int
1923side_effects_p (x)
1924 rtx x;
1925{
1926 register RTX_CODE code;
1927
1928 code = GET_CODE (x);
1929 switch (code)
1930 {
1931 case LABEL_REF:
1932 case SYMBOL_REF:
1933 case CONST_INT:
1934 case CONST:
1935 case CONST_DOUBLE:
1936 case CC0:
1937 case PC:
1938 case REG:
1939 case SCRATCH:
1940 case ASM_INPUT:
1941 case ADDR_VEC:
1942 case ADDR_DIFF_VEC:
1943 return 0;
1944
1945 case CLOBBER:
1946 /* Reject CLOBBER with a non-VOID mode. These are made by combine.c
1947 when some combination can't be done. If we see one, don't think
1948 that we can simplify the expression. */
1949 return (GET_MODE (x) != VOIDmode);
1950
1951 case PRE_INC:
1952 case PRE_DEC:
1953 case POST_INC:
1954 case POST_DEC:
1fb9c5cd
MH
1955 case PRE_MODIFY:
1956 case POST_MODIFY:
2c88418c 1957 case CALL:
2ac4fed0 1958 case UNSPEC_VOLATILE:
2c88418c
RS
1959 /* case TRAP_IF: This isn't clear yet. */
1960 return 1;
1961
1962 case MEM:
1963 case ASM_OPERANDS:
1964 if (MEM_VOLATILE_P (x))
1965 return 1;
e9a25f70
JL
1966
1967 default:
1968 break;
2c88418c
RS
1969 }
1970
1971 /* Recursively scan the operands of this expression. */
1972
1973 {
6f7d635c 1974 register const char *fmt = GET_RTX_FORMAT (code);
2c88418c
RS
1975 register int i;
1976
1977 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1978 {
1979 if (fmt[i] == 'e')
1980 {
1981 if (side_effects_p (XEXP (x, i)))
1982 return 1;
1983 }
d4757e6a 1984 else if (fmt[i] == 'E')
2c88418c
RS
1985 {
1986 register int j;
1987 for (j = 0; j < XVECLEN (x, i); j++)
1988 if (side_effects_p (XVECEXP (x, i, j)))
1989 return 1;
1990 }
1991 }
1992 }
1993 return 0;
1994}
1995\f
1996/* Return nonzero if evaluating rtx X might cause a trap. */
1997
1998int
1999may_trap_p (x)
2000 rtx x;
2001{
2002 int i;
2003 enum rtx_code code;
6f7d635c 2004 const char *fmt;
2c88418c
RS
2005
2006 if (x == 0)
2007 return 0;
2008 code = GET_CODE (x);
2009 switch (code)
2010 {
2011 /* Handle these cases quickly. */
2012 case CONST_INT:
2013 case CONST_DOUBLE:
2014 case SYMBOL_REF:
2015 case LABEL_REF:
2016 case CONST:
2017 case PC:
2018 case CC0:
2019 case REG:
2020 case SCRATCH:
2021 return 0;
2022
22aa60a1 2023 case ASM_INPUT:
2ac4fed0 2024 case UNSPEC_VOLATILE:
2c88418c
RS
2025 case TRAP_IF:
2026 return 1;
2027
22aa60a1
RH
2028 case ASM_OPERANDS:
2029 return MEM_VOLATILE_P (x);
2030
2c88418c
RS
2031 /* Memory ref can trap unless it's a static var or a stack slot. */
2032 case MEM:
2033 return rtx_addr_can_trap_p (XEXP (x, 0));
2034
2035 /* Division by a non-constant might trap. */
2036 case DIV:
2037 case MOD:
2038 case UDIV:
2039 case UMOD:
e9a25f70
JL
2040 if (! CONSTANT_P (XEXP (x, 1))
2041 || GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2c88418c
RS
2042 return 1;
2043 /* This was const0_rtx, but by not using that,
2044 we can link this file into other programs. */
2045 if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) == 0)
2046 return 1;
e9a25f70
JL
2047 break;
2048
b278301b
RK
2049 case EXPR_LIST:
2050 /* An EXPR_LIST is used to represent a function call. This
2051 certainly may trap. */
2052 return 1;
e9a25f70 2053
734508ea
JW
2054 case GE:
2055 case GT:
2056 case LE:
2057 case LT:
55143861 2058 case COMPARE:
734508ea
JW
2059 /* Some floating point comparisons may trap. */
2060 /* ??? There is no machine independent way to check for tests that trap
2061 when COMPARE is used, though many targets do make this distinction.
2062 For instance, sparc uses CCFPE for compares which generate exceptions
2063 and CCFP for compares which do not generate exceptions. */
55143861
JJ
2064 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2065 return 1;
2066 /* But often the compare has some CC mode, so check operand
2067 modes as well. */
2068 if (GET_MODE_CLASS (GET_MODE (XEXP (x, 0))) == MODE_FLOAT
2069 || GET_MODE_CLASS (GET_MODE (XEXP (x, 1))) == MODE_FLOAT)
2070 return 1;
2071 break;
2072
05cc23e8
RH
2073 case NEG:
2074 case ABS:
2075 /* These operations don't trap even with floating point. */
2076 break;
2077
2c88418c
RS
2078 default:
2079 /* Any floating arithmetic may trap. */
2080 if (GET_MODE_CLASS (GET_MODE (x)) == MODE_FLOAT)
2081 return 1;
2082 }
2083
2084 fmt = GET_RTX_FORMAT (code);
2085 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2086 {
2087 if (fmt[i] == 'e')
2088 {
2089 if (may_trap_p (XEXP (x, i)))
2090 return 1;
2091 }
2092 else if (fmt[i] == 'E')
2093 {
2094 register int j;
2095 for (j = 0; j < XVECLEN (x, i); j++)
2096 if (may_trap_p (XVECEXP (x, i, j)))
2097 return 1;
2098 }
2099 }
2100 return 0;
2101}
2102\f
2103/* Return nonzero if X contains a comparison that is not either EQ or NE,
2104 i.e., an inequality. */
2105
2106int
2107inequality_comparisons_p (x)
2108 rtx x;
2109{
6f7d635c 2110 register const char *fmt;
2c88418c
RS
2111 register int len, i;
2112 register enum rtx_code code = GET_CODE (x);
2113
2114 switch (code)
2115 {
2116 case REG:
2117 case SCRATCH:
2118 case PC:
2119 case CC0:
2120 case CONST_INT:
2121 case CONST_DOUBLE:
2122 case CONST:
2123 case LABEL_REF:
2124 case SYMBOL_REF:
2125 return 0;
2126
2127 case LT:
2128 case LTU:
2129 case GT:
2130 case GTU:
2131 case LE:
2132 case LEU:
2133 case GE:
2134 case GEU:
2135 return 1;
e9a25f70
JL
2136
2137 default:
2138 break;
2c88418c
RS
2139 }
2140
2141 len = GET_RTX_LENGTH (code);
2142 fmt = GET_RTX_FORMAT (code);
2143
2144 for (i = 0; i < len; i++)
2145 {
2146 if (fmt[i] == 'e')
2147 {
2148 if (inequality_comparisons_p (XEXP (x, i)))
2149 return 1;
2150 }
2151 else if (fmt[i] == 'E')
2152 {
2153 register int j;
2154 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2155 if (inequality_comparisons_p (XVECEXP (x, i, j)))
2156 return 1;
2157 }
2158 }
2159
2160 return 0;
2161}
2162\f
1ed0205e
VM
2163/* Replace any occurrence of FROM in X with TO. The function does
2164 not enter into CONST_DOUBLE for the replace.
2c88418c
RS
2165
2166 Note that copying is not done so X must not be shared unless all copies
2167 are to be modified. */
2168
2169rtx
2170replace_rtx (x, from, to)
2171 rtx x, from, to;
2172{
2173 register int i, j;
6f7d635c 2174 register const char *fmt;
2c88418c 2175
1ed0205e
VM
2176 /* The following prevents loops occurrence when we change MEM in
2177 CONST_DOUBLE onto the same CONST_DOUBLE. */
2178 if (x != 0 && GET_CODE (x) == CONST_DOUBLE)
2179 return x;
2180
2c88418c
RS
2181 if (x == from)
2182 return to;
2183
2184 /* Allow this function to make replacements in EXPR_LISTs. */
2185 if (x == 0)
2186 return 0;
2187
2188 fmt = GET_RTX_FORMAT (GET_CODE (x));
2189 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2190 {
2191 if (fmt[i] == 'e')
2192 XEXP (x, i) = replace_rtx (XEXP (x, i), from, to);
2193 else if (fmt[i] == 'E')
2194 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2195 XVECEXP (x, i, j) = replace_rtx (XVECEXP (x, i, j), from, to);
2196 }
2197
2198 return x;
2199}
2200\f
2201/* Throughout the rtx X, replace many registers according to REG_MAP.
2202 Return the replacement for X (which may be X with altered contents).
2203 REG_MAP[R] is the replacement for register R, or 0 for don't replace.
2204 NREGS is the length of REG_MAP; regs >= NREGS are not mapped.
2205
2206 We only support REG_MAP entries of REG or SUBREG. Also, hard registers
2207 should not be mapped to pseudos or vice versa since validate_change
2208 is not called.
2209
2210 If REPLACE_DEST is 1, replacements are also done in destinations;
2211 otherwise, only sources are replaced. */
2212
2213rtx
2214replace_regs (x, reg_map, nregs, replace_dest)
2215 rtx x;
2216 rtx *reg_map;
770ae6cc 2217 unsigned int nregs;
2c88418c
RS
2218 int replace_dest;
2219{
2220 register enum rtx_code code;
2221 register int i;
6f7d635c 2222 register const char *fmt;
2c88418c
RS
2223
2224 if (x == 0)
2225 return x;
2226
2227 code = GET_CODE (x);
2228 switch (code)
2229 {
2230 case SCRATCH:
2231 case PC:
2232 case CC0:
2233 case CONST_INT:
2234 case CONST_DOUBLE:
2235 case CONST:
2236 case SYMBOL_REF:
2237 case LABEL_REF:
2238 return x;
2239
2240 case REG:
2241 /* Verify that the register has an entry before trying to access it. */
2242 if (REGNO (x) < nregs && reg_map[REGNO (x)] != 0)
3eb8f14c
JW
2243 {
2244 /* SUBREGs can't be shared. Always return a copy to ensure that if
2245 this replacement occurs more than once then each instance will
2246 get distinct rtx. */
2247 if (GET_CODE (reg_map[REGNO (x)]) == SUBREG)
2248 return copy_rtx (reg_map[REGNO (x)]);
2249 return reg_map[REGNO (x)];
2250 }
2c88418c
RS
2251 return x;
2252
2253 case SUBREG:
2254 /* Prevent making nested SUBREGs. */
2255 if (GET_CODE (SUBREG_REG (x)) == REG && REGNO (SUBREG_REG (x)) < nregs
2256 && reg_map[REGNO (SUBREG_REG (x))] != 0
2257 && GET_CODE (reg_map[REGNO (SUBREG_REG (x))]) == SUBREG)
2258 {
2259 rtx map_val = reg_map[REGNO (SUBREG_REG (x))];
2260 rtx map_inner = SUBREG_REG (map_val);
2261
2262 if (GET_MODE (x) == GET_MODE (map_inner))
2263 return map_inner;
2264 else
2265 {
ddef6bc7
JJ
2266 int final_offset = SUBREG_BYTE (x) + SUBREG_BYTE (map_val);
2267
2268 /* When working with REG SUBREGs the rule is that the byte
2269 offset must be a multiple of the SUBREG's mode. */
2270 final_offset = (final_offset / GET_MODE_SIZE (GET_MODE (x)));
2271 final_offset = (final_offset * GET_MODE_SIZE (GET_MODE (x)));
2272
2c88418c
RS
2273 /* We cannot call gen_rtx here since we may be linked with
2274 genattrtab.c. */
2275 /* Let's try clobbering the incoming SUBREG and see
2276 if this is really safe. */
2277 SUBREG_REG (x) = map_inner;
ddef6bc7 2278 SUBREG_BYTE (x) = final_offset;
2c88418c
RS
2279 return x;
2280#if 0
2281 rtx new = rtx_alloc (SUBREG);
ddef6bc7
JJ
2282 int final_offset = SUBREG_BYTE (x) + SUBREG_BYTE (map_val);
2283
2284 /* When working with REG SUBREGs the rule is that the byte
2285 offset must be a multiple of the SUBREG's mode. */
2286 final_offset = (final_offset / GET_MODE_SIZE (GET_MODE (x)));
2287 final_offset = (final_offset * GET_MODE_SIZE (GET_MODE (x)));
2288
2c88418c
RS
2289 PUT_MODE (new, GET_MODE (x));
2290 SUBREG_REG (new) = map_inner;
ddef6bc7 2291 SUBREG_BYTE (new) = final_offset;
2c88418c
RS
2292#endif
2293 }
2294 }
2295 break;
2296
2297 case SET:
2298 if (replace_dest)
2299 SET_DEST (x) = replace_regs (SET_DEST (x), reg_map, nregs, 0);
2300
2301 else if (GET_CODE (SET_DEST (x)) == MEM
2302 || GET_CODE (SET_DEST (x)) == STRICT_LOW_PART)
2303 /* Even if we are not to replace destinations, replace register if it
2304 is CONTAINED in destination (destination is memory or
2305 STRICT_LOW_PART). */
2306 XEXP (SET_DEST (x), 0) = replace_regs (XEXP (SET_DEST (x), 0),
2307 reg_map, nregs, 0);
2308 else if (GET_CODE (SET_DEST (x)) == ZERO_EXTRACT)
2309 /* Similarly, for ZERO_EXTRACT we replace all operands. */
2310 break;
2311
2312 SET_SRC (x) = replace_regs (SET_SRC (x), reg_map, nregs, 0);
2313 return x;
e9a25f70
JL
2314
2315 default:
2316 break;
2c88418c
RS
2317 }
2318
2319 fmt = GET_RTX_FORMAT (code);
2320 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2321 {
2322 if (fmt[i] == 'e')
2323 XEXP (x, i) = replace_regs (XEXP (x, i), reg_map, nregs, replace_dest);
d4757e6a 2324 else if (fmt[i] == 'E')
2c88418c
RS
2325 {
2326 register int j;
2327 for (j = 0; j < XVECLEN (x, i); j++)
2328 XVECEXP (x, i, j) = replace_regs (XVECEXP (x, i, j), reg_map,
2329 nregs, replace_dest);
2330 }
2331 }
2332 return x;
2333}
2a1777af 2334
fce7e199
RH
2335/* A subroutine of computed_jump_p, return 1 if X contains a REG or MEM or
2336 constant that is not in the constant pool and not in the condition
2337 of an IF_THEN_ELSE. */
2a1777af
JL
2338
2339static int
fce7e199 2340computed_jump_p_1 (x)
2a1777af
JL
2341 rtx x;
2342{
2343 enum rtx_code code = GET_CODE (x);
2344 int i, j;
6f7d635c 2345 const char *fmt;
2a1777af
JL
2346
2347 switch (code)
2348 {
2a1777af
JL
2349 case LABEL_REF:
2350 case PC:
2351 return 0;
2352
fce7e199
RH
2353 case CONST:
2354 case CONST_INT:
2355 case CONST_DOUBLE:
2356 case SYMBOL_REF:
2a1777af
JL
2357 case REG:
2358 return 1;
2359
2360 case MEM:
2361 return ! (GET_CODE (XEXP (x, 0)) == SYMBOL_REF
2362 && CONSTANT_POOL_ADDRESS_P (XEXP (x, 0)));
2363
2364 case IF_THEN_ELSE:
fce7e199
RH
2365 return (computed_jump_p_1 (XEXP (x, 1))
2366 || computed_jump_p_1 (XEXP (x, 2)));
1d300e19
KG
2367
2368 default:
2369 break;
2a1777af
JL
2370 }
2371
2372 fmt = GET_RTX_FORMAT (code);
2373 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2374 {
2375 if (fmt[i] == 'e'
fce7e199 2376 && computed_jump_p_1 (XEXP (x, i)))
2a1777af
JL
2377 return 1;
2378
d4757e6a 2379 else if (fmt[i] == 'E')
2a1777af 2380 for (j = 0; j < XVECLEN (x, i); j++)
fce7e199 2381 if (computed_jump_p_1 (XVECEXP (x, i, j)))
2a1777af
JL
2382 return 1;
2383 }
2384
2385 return 0;
2386}
2387
2388/* Return nonzero if INSN is an indirect jump (aka computed jump).
2389
2390 Tablejumps and casesi insns are not considered indirect jumps;
4eb00163 2391 we can recognize them by a (use (label_ref)). */
2a1777af
JL
2392
2393int
2394computed_jump_p (insn)
2395 rtx insn;
2396{
2397 int i;
2398 if (GET_CODE (insn) == JUMP_INSN)
2399 {
2400 rtx pat = PATTERN (insn);
2a1777af 2401
f759eb8b
AO
2402 if (find_reg_note (insn, REG_LABEL, NULL_RTX))
2403 return 0;
2404 else if (GET_CODE (pat) == PARALLEL)
2a1777af
JL
2405 {
2406 int len = XVECLEN (pat, 0);
2407 int has_use_labelref = 0;
2408
2409 for (i = len - 1; i >= 0; i--)
2410 if (GET_CODE (XVECEXP (pat, 0, i)) == USE
2411 && (GET_CODE (XEXP (XVECEXP (pat, 0, i), 0))
2412 == LABEL_REF))
2413 has_use_labelref = 1;
2414
2415 if (! has_use_labelref)
2416 for (i = len - 1; i >= 0; i--)
2417 if (GET_CODE (XVECEXP (pat, 0, i)) == SET
2418 && SET_DEST (XVECEXP (pat, 0, i)) == pc_rtx
fce7e199 2419 && computed_jump_p_1 (SET_SRC (XVECEXP (pat, 0, i))))
2a1777af
JL
2420 return 1;
2421 }
2422 else if (GET_CODE (pat) == SET
2423 && SET_DEST (pat) == pc_rtx
fce7e199 2424 && computed_jump_p_1 (SET_SRC (pat)))
2a1777af
JL
2425 return 1;
2426 }
2427 return 0;
2428}
ccc2d6d0
MM
2429
2430/* Traverse X via depth-first search, calling F for each
2431 sub-expression (including X itself). F is also passed the DATA.
2432 If F returns -1, do not traverse sub-expressions, but continue
2433 traversing the rest of the tree. If F ever returns any other
2434 non-zero value, stop the traversal, and return the value returned
2435 by F. Otherwise, return 0. This function does not traverse inside
2436 tree structure that contains RTX_EXPRs, or into sub-expressions
2437 whose format code is `0' since it is not known whether or not those
2438 codes are actually RTL.
2439
2440 This routine is very general, and could (should?) be used to
2441 implement many of the other routines in this file. */
2442
ae0b51ef
JL
2443int
2444for_each_rtx (x, f, data)
ef30399b 2445 rtx *x;
ccc2d6d0 2446 rtx_function f;
ef30399b 2447 void *data;
ccc2d6d0
MM
2448{
2449 int result;
2450 int length;
6f7d635c 2451 const char* format;
ccc2d6d0
MM
2452 int i;
2453
2454 /* Call F on X. */
2455 result = (*f)(x, data);
2456 if (result == -1)
2457 /* Do not traverse sub-expressions. */
2458 return 0;
2459 else if (result != 0)
2460 /* Stop the traversal. */
2461 return result;
2462
2463 if (*x == NULL_RTX)
2464 /* There are no sub-expressions. */
2465 return 0;
2466
2467 length = GET_RTX_LENGTH (GET_CODE (*x));
2468 format = GET_RTX_FORMAT (GET_CODE (*x));
2469
2470 for (i = 0; i < length; ++i)
2471 {
2472 switch (format[i])
2473 {
2474 case 'e':
2475 result = for_each_rtx (&XEXP (*x, i), f, data);
2476 if (result != 0)
2477 return result;
2478 break;
2479
2480 case 'V':
2481 case 'E':
2482 if (XVEC (*x, i) != 0)
2483 {
2484 int j;
2485 for (j = 0; j < XVECLEN (*x, i); ++j)
2486 {
2487 result = for_each_rtx (&XVECEXP (*x, i, j), f, data);
2488 if (result != 0)
2489 return result;
2490 }
2491 }
2492 break;
2493
2494 default:
2495 /* Nothing to do. */
2496 break;
2497 }
2498
2499 }
2500
2501 return 0;
2502}
3ec2b590 2503
777b1b71
RH
2504/* Searches X for any reference to REGNO, returning the rtx of the
2505 reference found if any. Otherwise, returns NULL_RTX. */
2506
2507rtx
2508regno_use_in (regno, x)
770ae6cc 2509 unsigned int regno;
777b1b71
RH
2510 rtx x;
2511{
6f7d635c 2512 register const char *fmt;
777b1b71
RH
2513 int i, j;
2514 rtx tem;
2515
2516 if (GET_CODE (x) == REG && REGNO (x) == regno)
2517 return x;
2518
2519 fmt = GET_RTX_FORMAT (GET_CODE (x));
2520 for (i = GET_RTX_LENGTH (GET_CODE (x)) - 1; i >= 0; i--)
2521 {
2522 if (fmt[i] == 'e')
2523 {
2524 if ((tem = regno_use_in (regno, XEXP (x, i))))
2525 return tem;
2526 }
2527 else if (fmt[i] == 'E')
2528 for (j = XVECLEN (x, i) - 1; j >= 0; j--)
2529 if ((tem = regno_use_in (regno , XVECEXP (x, i, j))))
2530 return tem;
2531 }
2532
2533 return NULL_RTX;
2534}
2dfa9a87
MH
2535
2536
2537/* Return 1 if X is an autoincrement side effect and the register is
2538 not the stack pointer. */
2539int
2540auto_inc_p (x)
2541 rtx x;
2542{
2543 switch (GET_CODE (x))
2544 {
2545 case PRE_INC:
2546 case POST_INC:
2547 case PRE_DEC:
2548 case POST_DEC:
2549 case PRE_MODIFY:
2550 case POST_MODIFY:
2551 /* There are no REG_INC notes for SP. */
2552 if (XEXP (x, 0) != stack_pointer_rtx)
2553 return 1;
2554 default:
2555 break;
2556 }
2557 return 0;
2558}
3b10cf4b
MM
2559
2560/* Return 1 if the sequence of instructions beginning with FROM and up
2561 to and including TO is safe to move. If NEW_TO is non-NULL, and
2562 the sequence is not already safe to move, but can be easily
2563 extended to a sequence which is safe, then NEW_TO will point to the
7015a814
MM
2564 end of the extended sequence.
2565
2566 For now, this function only checks that the region contains whole
5bea1ccf 2567 exception regions, but it could be extended to check additional
7015a814 2568 conditions as well. */
3b10cf4b
MM
2569
2570int
2571insns_safe_to_move_p (from, to, new_to)
2572 rtx from;
2573 rtx to;
2574 rtx *new_to;
2575{
2576 int eh_region_count = 0;
2577 int past_to_p = 0;
2578 rtx r = from;
2579
7015a814
MM
2580 /* By default, assume the end of the region will be what was
2581 suggested. */
2582 if (new_to)
2583 *new_to = to;
2584
3b10cf4b
MM
2585 while (r)
2586 {
2587 if (GET_CODE (r) == NOTE)
2588 {
2589 switch (NOTE_LINE_NUMBER (r))
2590 {
2591 case NOTE_INSN_EH_REGION_BEG:
2592 ++eh_region_count;
2593 break;
2594
2595 case NOTE_INSN_EH_REGION_END:
2596 if (eh_region_count == 0)
2597 /* This sequence of instructions contains the end of
2598 an exception region, but not he beginning. Moving
2599 it will cause chaos. */
2600 return 0;
2601
2602 --eh_region_count;
2603 break;
2604
2605 default:
2606 break;
2607 }
2608 }
2609 else if (past_to_p)
2610 /* If we've passed TO, and we see a non-note instruction, we
2611 can't extend the sequence to a movable sequence. */
2612 return 0;
2613
2614 if (r == to)
2615 {
2616 if (!new_to)
2617 /* It's OK to move the sequence if there were matched sets of
2618 exception region notes. */
2619 return eh_region_count == 0;
2620
2621 past_to_p = 1;
2622 }
2623
2624 /* It's OK to move the sequence if there were matched sets of
2625 exception region notes. */
2626 if (past_to_p && eh_region_count == 0)
2627 {
2628 *new_to = r;
2629 return 1;
2630 }
2631
2632 /* Go to the next instruction. */
2633 r = NEXT_INSN (r);
2634 }
2635
2636 return 0;
2637}
db7ba742
R
2638
2639/* Return non-zero if IN contains a piece of rtl that has the address LOC */
2640int
2641loc_mentioned_in_p (loc, in)
2642 rtx *loc, in;
2643{
2644 enum rtx_code code = GET_CODE (in);
2645 const char *fmt = GET_RTX_FORMAT (code);
2646 int i, j;
2647
2648 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2649 {
2650 if (loc == &in->fld[i].rtx)
2651 return 1;
2652 if (fmt[i] == 'e')
2653 {
2654 if (loc_mentioned_in_p (loc, XEXP (in, i)))
2655 return 1;
2656 }
2657 else if (fmt[i] == 'E')
2658 for (j = XVECLEN (in, i) - 1; j >= 0; j--)
2659 if (loc_mentioned_in_p (loc, XVECEXP (in, i, j)))
2660 return 1;
2661 }
2662 return 0;
2663}
ddef6bc7
JJ
2664
2665/* This function returns the regno offset of a subreg expression.
2666 xregno - A regno of an inner hard subreg_reg (or what will become one).
2667 xmode - The mode of xregno.
2668 offset - The byte offset.
2669 ymode - The mode of a top level SUBREG (or what may become one).
2670 RETURN - The regno offset which would be used.
2671 This function can be overridden by defining SUBREG_REGNO_OFFSET,
2672 taking the same parameters. */
2673unsigned int
2674subreg_regno_offset (xregno, xmode, offset, ymode)
2675 unsigned int xregno;
2676 enum machine_mode xmode;
2677 unsigned int offset;
2678 enum machine_mode ymode;
2679{
2680 unsigned ret;
2681 int nregs_xmode, nregs_ymode;
2682 int mode_multiple, nregs_multiple;
2683 int y_offset;
2684
2685/* Check for an override, and use it instead. */
2686#ifdef SUBREG_REGNO_OFFSET
2687 ret = SUBREG_REGNO_OFFSET (xregno, xmode, offset, ymode)
2688#else
2689 if (xregno >= FIRST_PSEUDO_REGISTER)
2690 abort ();
2691
2692 nregs_xmode = HARD_REGNO_NREGS (xregno, xmode);
2693 nregs_ymode = HARD_REGNO_NREGS (xregno, ymode);
2694 if (offset == 0 || nregs_xmode == nregs_ymode)
2695 return 0;
2696
2697 /* size of ymode must not be greater than the size of xmode. */
2698 mode_multiple = GET_MODE_SIZE (xmode) / GET_MODE_SIZE (ymode);
2699 if (mode_multiple == 0)
2700 abort ();
2701
2702 y_offset = offset / GET_MODE_SIZE (ymode);
2703 nregs_multiple = nregs_xmode / nregs_ymode;
2704 ret = (y_offset / (mode_multiple / nregs_multiple)) * nregs_ymode;
2705#endif
2706
2707 return ret;
2708}
2709
2710/* Return the final regno that a subreg expression refers to. */
2711unsigned int
2712subreg_regno (x)
2713 rtx x;
2714{
2715 unsigned int ret;
2716 rtx subreg = SUBREG_REG (x);
2717 int regno = REGNO (subreg);
2718
2719 ret = regno + subreg_regno_offset (regno,
2720 GET_MODE (subreg),
2721 SUBREG_BYTE (x),
2722 GET_MODE (x));
2723 return ret;
2724
2725}