reload1.c: Add prototype for replace_pseudos_in_call_usage.
[gcc.git] / gcc / regmove.c
1 /* Move registers around to reduce number of move instructions needed.
2 Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3 1999, 2000, 2001 Free Software Foundation, Inc.
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING. If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
21
22
23 /* This module looks for cases where matching constraints would force
24 an instruction to need a reload, and this reload would be a register
25 to register move. It then attempts to change the registers used by the
26 instruction to avoid the move instruction. */
27
28 #include "config.h"
29 #include "system.h"
30 #include "rtl.h" /* stdio.h must precede rtl.h for FFS. */
31 #include "tm_p.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "output.h"
35 #include "regs.h"
36 #include "hard-reg-set.h"
37 #include "flags.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "insn-flags.h"
41 #include "basic-block.h"
42 #include "toplev.h"
43
44 static int perhaps_ends_bb_p PARAMS ((rtx));
45 static int optimize_reg_copy_1 PARAMS ((rtx, rtx, rtx));
46 static void optimize_reg_copy_2 PARAMS ((rtx, rtx, rtx));
47 static void optimize_reg_copy_3 PARAMS ((rtx, rtx, rtx));
48 static rtx gen_add3_insn PARAMS ((rtx, rtx, rtx));
49 static void copy_src_to_dest PARAMS ((rtx, rtx, rtx, int));
50 static int *regmove_bb_head;
51
52 struct match {
53 int with[MAX_RECOG_OPERANDS];
54 enum { READ, WRITE, READWRITE } use[MAX_RECOG_OPERANDS];
55 int commutative[MAX_RECOG_OPERANDS];
56 int early_clobber[MAX_RECOG_OPERANDS];
57 };
58
59 static rtx discover_flags_reg PARAMS ((void));
60 static void mark_flags_life_zones PARAMS ((rtx));
61 static void flags_set_1 PARAMS ((rtx, rtx, void *));
62
63 static int try_auto_increment PARAMS ((rtx, rtx, rtx, rtx, HOST_WIDE_INT, int));
64 static int find_matches PARAMS ((rtx, struct match *));
65 static void replace_in_call_usage PARAMS ((rtx *, int, rtx, rtx));
66 static int fixup_match_1 PARAMS ((rtx, rtx, rtx, rtx, rtx, int, int, int, FILE *))
67 ;
68 static int reg_is_remote_constant_p PARAMS ((rtx, rtx, rtx));
69 static int stable_and_no_regs_but_for_p PARAMS ((rtx, rtx, rtx));
70 static int regclass_compatible_p PARAMS ((int, int));
71 static int replacement_quality PARAMS ((rtx));
72 static int fixup_match_2 PARAMS ((rtx, rtx, rtx, rtx, FILE *));
73
74 /* Return non-zero if registers with CLASS1 and CLASS2 can be merged without
75 causing too much register allocation problems. */
76 static int
77 regclass_compatible_p (class0, class1)
78 int class0, class1;
79 {
80 return (class0 == class1
81 || (reg_class_subset_p (class0, class1)
82 && ! CLASS_LIKELY_SPILLED_P (class0))
83 || (reg_class_subset_p (class1, class0)
84 && ! CLASS_LIKELY_SPILLED_P (class1)));
85 }
86
87 /* Generate and return an insn body to add r1 and c,
88 storing the result in r0. */
89 static rtx
90 gen_add3_insn (r0, r1, c)
91 rtx r0, r1, c;
92 {
93 int icode = (int) add_optab->handlers[(int) GET_MODE (r0)].insn_code;
94
95 if (icode == CODE_FOR_nothing
96 || ! ((*insn_data[icode].operand[0].predicate)
97 (r0, insn_data[icode].operand[0].mode))
98 || ! ((*insn_data[icode].operand[1].predicate)
99 (r1, insn_data[icode].operand[1].mode))
100 || ! ((*insn_data[icode].operand[2].predicate)
101 (c, insn_data[icode].operand[2].mode)))
102 return NULL_RTX;
103
104 return (GEN_FCN (icode) (r0, r1, c));
105 }
106
107
108 /* INC_INSN is an instruction that adds INCREMENT to REG.
109 Try to fold INC_INSN as a post/pre in/decrement into INSN.
110 Iff INC_INSN_SET is nonzero, inc_insn has a destination different from src.
111 Return nonzero for success. */
112 static int
113 try_auto_increment (insn, inc_insn, inc_insn_set, reg, increment, pre)
114 rtx reg, insn, inc_insn ,inc_insn_set;
115 HOST_WIDE_INT increment;
116 int pre;
117 {
118 enum rtx_code inc_code;
119
120 rtx pset = single_set (insn);
121 if (pset)
122 {
123 /* Can't use the size of SET_SRC, we might have something like
124 (sign_extend:SI (mem:QI ... */
125 rtx use = find_use_as_address (pset, reg, 0);
126 if (use != 0 && use != (rtx) 1)
127 {
128 int size = GET_MODE_SIZE (GET_MODE (use));
129 if (0
130 || (HAVE_POST_INCREMENT
131 && pre == 0 && (inc_code = POST_INC, increment == size))
132 || (HAVE_PRE_INCREMENT
133 && pre == 1 && (inc_code = PRE_INC, increment == size))
134 || (HAVE_POST_DECREMENT
135 && pre == 0 && (inc_code = POST_DEC, increment == -size))
136 || (HAVE_PRE_DECREMENT
137 && pre == 1 && (inc_code = PRE_DEC, increment == -size))
138 )
139 {
140 if (inc_insn_set)
141 validate_change
142 (inc_insn,
143 &SET_SRC (inc_insn_set),
144 XEXP (SET_SRC (inc_insn_set), 0), 1);
145 validate_change (insn, &XEXP (use, 0),
146 gen_rtx_fmt_e (inc_code, Pmode, reg), 1);
147 if (apply_change_group ())
148 {
149 REG_NOTES (insn)
150 = gen_rtx_EXPR_LIST (REG_INC,
151 reg, REG_NOTES (insn));
152 if (! inc_insn_set)
153 {
154 PUT_CODE (inc_insn, NOTE);
155 NOTE_LINE_NUMBER (inc_insn) = NOTE_INSN_DELETED;
156 NOTE_SOURCE_FILE (inc_insn) = 0;
157 }
158 return 1;
159 }
160 }
161 }
162 }
163 return 0;
164 }
165 \f
166 /* Determine if the pattern generated by add_optab has a clobber,
167 such as might be issued for a flags hard register. To make the
168 code elsewhere simpler, we handle cc0 in this same framework.
169
170 Return the register if one was discovered. Return NULL_RTX if
171 if no flags were found. Return pc_rtx if we got confused. */
172
173 static rtx
174 discover_flags_reg ()
175 {
176 rtx tmp;
177 tmp = gen_rtx_REG (word_mode, 10000);
178 tmp = gen_add3_insn (tmp, tmp, GEN_INT (2));
179
180 /* If we get something that isn't a simple set, or a
181 [(set ..) (clobber ..)], this whole function will go wrong. */
182 if (GET_CODE (tmp) == SET)
183 return NULL_RTX;
184 else if (GET_CODE (tmp) == PARALLEL)
185 {
186 int found;
187
188 if (XVECLEN (tmp, 0) != 2)
189 return pc_rtx;
190 tmp = XVECEXP (tmp, 0, 1);
191 if (GET_CODE (tmp) != CLOBBER)
192 return pc_rtx;
193 tmp = XEXP (tmp, 0);
194
195 /* Don't do anything foolish if the md wanted to clobber a
196 scratch or something. We only care about hard regs.
197 Moreover we don't like the notion of subregs of hard regs. */
198 if (GET_CODE (tmp) == SUBREG
199 && GET_CODE (SUBREG_REG (tmp)) == REG
200 && REGNO (SUBREG_REG (tmp)) < FIRST_PSEUDO_REGISTER)
201 return pc_rtx;
202 found = (GET_CODE (tmp) == REG && REGNO (tmp) < FIRST_PSEUDO_REGISTER);
203
204 return (found ? tmp : NULL_RTX);
205 }
206
207 return pc_rtx;
208 }
209
210 /* It is a tedious task identifying when the flags register is live and
211 when it is safe to optimize. Since we process the instruction stream
212 multiple times, locate and record these live zones by marking the
213 mode of the instructions --
214
215 QImode is used on the instruction at which the flags becomes live.
216
217 HImode is used within the range (exclusive) that the flags are
218 live. Thus the user of the flags is not marked.
219
220 All other instructions are cleared to VOIDmode. */
221
222 /* Used to communicate with flags_set_1. */
223 static rtx flags_set_1_rtx;
224 static int flags_set_1_set;
225
226 static void
227 mark_flags_life_zones (flags)
228 rtx flags;
229 {
230 int flags_regno;
231 int flags_nregs;
232 int block;
233
234 #ifdef HAVE_cc0
235 /* If we found a flags register on a cc0 host, bail. */
236 if (flags == NULL_RTX)
237 flags = cc0_rtx;
238 else if (flags != cc0_rtx)
239 flags = pc_rtx;
240 #endif
241
242 /* Simple cases first: if no flags, clear all modes. If confusing,
243 mark the entire function as being in a flags shadow. */
244 if (flags == NULL_RTX || flags == pc_rtx)
245 {
246 enum machine_mode mode = (flags ? HImode : VOIDmode);
247 rtx insn;
248 for (insn = get_insns(); insn; insn = NEXT_INSN (insn))
249 PUT_MODE (insn, mode);
250 return;
251 }
252
253 #ifdef HAVE_cc0
254 flags_regno = -1;
255 flags_nregs = 1;
256 #else
257 flags_regno = REGNO (flags);
258 flags_nregs = HARD_REGNO_NREGS (flags_regno, GET_MODE (flags));
259 #endif
260 flags_set_1_rtx = flags;
261
262 /* Process each basic block. */
263 for (block = n_basic_blocks - 1; block >= 0; block--)
264 {
265 rtx insn, end;
266 int live;
267
268 insn = BLOCK_HEAD (block);
269 end = BLOCK_END (block);
270
271 /* Look out for the (unlikely) case of flags being live across
272 basic block boundaries. */
273 live = 0;
274 #ifndef HAVE_cc0
275 {
276 int i;
277 for (i = 0; i < flags_nregs; ++i)
278 live |= REGNO_REG_SET_P (BASIC_BLOCK (block)->global_live_at_start,
279 flags_regno + i);
280 }
281 #endif
282
283 while (1)
284 {
285 /* Process liveness in reverse order of importance --
286 alive, death, birth. This lets more important info
287 overwrite the mode of lesser info. */
288
289 if (INSN_P (insn))
290 {
291 #ifdef HAVE_cc0
292 /* In the cc0 case, death is not marked in reg notes,
293 but is instead the mere use of cc0 when it is alive. */
294 if (live && reg_mentioned_p (cc0_rtx, PATTERN (insn)))
295 live = 0;
296 #else
297 /* In the hard reg case, we watch death notes. */
298 if (live && find_regno_note (insn, REG_DEAD, flags_regno))
299 live = 0;
300 #endif
301 PUT_MODE (insn, (live ? HImode : VOIDmode));
302
303 /* In either case, birth is denoted simply by it's presence
304 as the destination of a set. */
305 flags_set_1_set = 0;
306 note_stores (PATTERN (insn), flags_set_1, NULL);
307 if (flags_set_1_set)
308 {
309 live = 1;
310 PUT_MODE (insn, QImode);
311 }
312 }
313 else
314 PUT_MODE (insn, (live ? HImode : VOIDmode));
315
316 if (insn == end)
317 break;
318 insn = NEXT_INSN (insn);
319 }
320 }
321 }
322
323 /* A subroutine of mark_flags_life_zones, called through note_stores. */
324
325 static void
326 flags_set_1 (x, pat, data)
327 rtx x, pat;
328 void *data ATTRIBUTE_UNUSED;
329 {
330 if (GET_CODE (pat) == SET
331 && reg_overlap_mentioned_p (x, flags_set_1_rtx))
332 flags_set_1_set = 1;
333 }
334 \f
335 static int *regno_src_regno;
336
337 /* Indicate how good a choice REG (which appears as a source) is to replace
338 a destination register with. The higher the returned value, the better
339 the choice. The main objective is to avoid using a register that is
340 a candidate for tying to a hard register, since the output might in
341 turn be a candidate to be tied to a different hard register. */
342 static int
343 replacement_quality(reg)
344 rtx reg;
345 {
346 int src_regno;
347
348 /* Bad if this isn't a register at all. */
349 if (GET_CODE (reg) != REG)
350 return 0;
351
352 /* If this register is not meant to get a hard register,
353 it is a poor choice. */
354 if (REG_LIVE_LENGTH (REGNO (reg)) < 0)
355 return 0;
356
357 src_regno = regno_src_regno[REGNO (reg)];
358
359 /* If it was not copied from another register, it is fine. */
360 if (src_regno < 0)
361 return 3;
362
363 /* Copied from a hard register? */
364 if (src_regno < FIRST_PSEUDO_REGISTER)
365 return 1;
366
367 /* Copied from a pseudo register - not as bad as from a hard register,
368 yet still cumbersome, since the register live length will be lengthened
369 when the registers get tied. */
370 return 2;
371 }
372 \f
373 /* Return 1 if INSN might end a basic block. */
374
375 static int perhaps_ends_bb_p (insn)
376 rtx insn;
377 {
378 switch (GET_CODE (insn))
379 {
380 case CODE_LABEL:
381 case JUMP_INSN:
382 /* These always end a basic block. */
383 return 1;
384
385 case CALL_INSN:
386 /* A CALL_INSN might be the last insn of a basic block, if it is inside
387 an EH region or if there are nonlocal gotos. Note that this test is
388 very conservative. */
389 return flag_exceptions || nonlocal_goto_handler_labels;
390
391 default:
392 /* All others never end a basic block. */
393 return 0;
394 }
395 }
396 \f
397 /* INSN is a copy from SRC to DEST, both registers, and SRC does not die
398 in INSN.
399
400 Search forward to see if SRC dies before either it or DEST is modified,
401 but don't scan past the end of a basic block. If so, we can replace SRC
402 with DEST and let SRC die in INSN.
403
404 This will reduce the number of registers live in that range and may enable
405 DEST to be tied to SRC, thus often saving one register in addition to a
406 register-register copy. */
407
408 static int
409 optimize_reg_copy_1 (insn, dest, src)
410 rtx insn;
411 rtx dest;
412 rtx src;
413 {
414 rtx p, q;
415 rtx note;
416 rtx dest_death = 0;
417 int sregno = REGNO (src);
418 int dregno = REGNO (dest);
419
420 /* We don't want to mess with hard regs if register classes are small. */
421 if (sregno == dregno
422 || (SMALL_REGISTER_CLASSES
423 && (sregno < FIRST_PSEUDO_REGISTER
424 || dregno < FIRST_PSEUDO_REGISTER))
425 /* We don't see all updates to SP if they are in an auto-inc memory
426 reference, so we must disallow this optimization on them. */
427 || sregno == STACK_POINTER_REGNUM || dregno == STACK_POINTER_REGNUM)
428 return 0;
429
430 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
431 {
432 /* ??? We can't scan past the end of a basic block without updating
433 the register lifetime info (REG_DEAD/basic_block_live_at_start). */
434 if (perhaps_ends_bb_p (p))
435 break;
436 else if (! INSN_P (p))
437 continue;
438
439 if (reg_set_p (src, p) || reg_set_p (dest, p)
440 /* Don't change a USE of a register. */
441 || (GET_CODE (PATTERN (p)) == USE
442 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
443 break;
444
445 /* See if all of SRC dies in P. This test is slightly more
446 conservative than it needs to be. */
447 if ((note = find_regno_note (p, REG_DEAD, sregno)) != 0
448 && GET_MODE (XEXP (note, 0)) == GET_MODE (src))
449 {
450 int failed = 0;
451 int d_length = 0;
452 int s_length = 0;
453 int d_n_calls = 0;
454 int s_n_calls = 0;
455
456 /* We can do the optimization. Scan forward from INSN again,
457 replacing regs as we go. Set FAILED if a replacement can't
458 be done. In that case, we can't move the death note for SRC.
459 This should be rare. */
460
461 /* Set to stop at next insn. */
462 for (q = next_real_insn (insn);
463 q != next_real_insn (p);
464 q = next_real_insn (q))
465 {
466 if (reg_overlap_mentioned_p (src, PATTERN (q)))
467 {
468 /* If SRC is a hard register, we might miss some
469 overlapping registers with validate_replace_rtx,
470 so we would have to undo it. We can't if DEST is
471 present in the insn, so fail in that combination
472 of cases. */
473 if (sregno < FIRST_PSEUDO_REGISTER
474 && reg_mentioned_p (dest, PATTERN (q)))
475 failed = 1;
476
477 /* Replace all uses and make sure that the register
478 isn't still present. */
479 else if (validate_replace_rtx (src, dest, q)
480 && (sregno >= FIRST_PSEUDO_REGISTER
481 || ! reg_overlap_mentioned_p (src,
482 PATTERN (q))))
483 ;
484 else
485 {
486 validate_replace_rtx (dest, src, q);
487 failed = 1;
488 }
489 }
490
491 /* For SREGNO, count the total number of insns scanned.
492 For DREGNO, count the total number of insns scanned after
493 passing the death note for DREGNO. */
494 s_length++;
495 if (dest_death)
496 d_length++;
497
498 /* If the insn in which SRC dies is a CALL_INSN, don't count it
499 as a call that has been crossed. Otherwise, count it. */
500 if (q != p && GET_CODE (q) == CALL_INSN)
501 {
502 /* Similarly, total calls for SREGNO, total calls beyond
503 the death note for DREGNO. */
504 s_n_calls++;
505 if (dest_death)
506 d_n_calls++;
507 }
508
509 /* If DEST dies here, remove the death note and save it for
510 later. Make sure ALL of DEST dies here; again, this is
511 overly conservative. */
512 if (dest_death == 0
513 && (dest_death = find_regno_note (q, REG_DEAD, dregno)) != 0)
514 {
515 if (GET_MODE (XEXP (dest_death, 0)) != GET_MODE (dest))
516 failed = 1, dest_death = 0;
517 else
518 remove_note (q, dest_death);
519 }
520 }
521
522 if (! failed)
523 {
524 /* These counters need to be updated if and only if we are
525 going to move the REG_DEAD note. */
526 if (sregno >= FIRST_PSEUDO_REGISTER)
527 {
528 if (REG_LIVE_LENGTH (sregno) >= 0)
529 {
530 REG_LIVE_LENGTH (sregno) -= s_length;
531 /* REG_LIVE_LENGTH is only an approximation after
532 combine if sched is not run, so make sure that we
533 still have a reasonable value. */
534 if (REG_LIVE_LENGTH (sregno) < 2)
535 REG_LIVE_LENGTH (sregno) = 2;
536 }
537
538 REG_N_CALLS_CROSSED (sregno) -= s_n_calls;
539 }
540
541 /* Move death note of SRC from P to INSN. */
542 remove_note (p, note);
543 XEXP (note, 1) = REG_NOTES (insn);
544 REG_NOTES (insn) = note;
545 }
546
547 /* DEST is also dead if INSN has a REG_UNUSED note for DEST. */
548 if (! dest_death
549 && (dest_death = find_regno_note (insn, REG_UNUSED, dregno)))
550 {
551 PUT_REG_NOTE_KIND (dest_death, REG_DEAD);
552 remove_note (insn, dest_death);
553 }
554
555 /* Put death note of DEST on P if we saw it die. */
556 if (dest_death)
557 {
558 XEXP (dest_death, 1) = REG_NOTES (p);
559 REG_NOTES (p) = dest_death;
560
561 if (dregno >= FIRST_PSEUDO_REGISTER)
562 {
563 /* If and only if we are moving the death note for DREGNO,
564 then we need to update its counters. */
565 if (REG_LIVE_LENGTH (dregno) >= 0)
566 REG_LIVE_LENGTH (dregno) += d_length;
567 REG_N_CALLS_CROSSED (dregno) += d_n_calls;
568 }
569 }
570
571 return ! failed;
572 }
573
574 /* If SRC is a hard register which is set or killed in some other
575 way, we can't do this optimization. */
576 else if (sregno < FIRST_PSEUDO_REGISTER
577 && dead_or_set_p (p, src))
578 break;
579 }
580 return 0;
581 }
582 \f
583 /* INSN is a copy of SRC to DEST, in which SRC dies. See if we now have
584 a sequence of insns that modify DEST followed by an insn that sets
585 SRC to DEST in which DEST dies, with no prior modification of DEST.
586 (There is no need to check if the insns in between actually modify
587 DEST. We should not have cases where DEST is not modified, but
588 the optimization is safe if no such modification is detected.)
589 In that case, we can replace all uses of DEST, starting with INSN and
590 ending with the set of SRC to DEST, with SRC. We do not do this
591 optimization if a CALL_INSN is crossed unless SRC already crosses a
592 call or if DEST dies before the copy back to SRC.
593
594 It is assumed that DEST and SRC are pseudos; it is too complicated to do
595 this for hard registers since the substitutions we may make might fail. */
596
597 static void
598 optimize_reg_copy_2 (insn, dest, src)
599 rtx insn;
600 rtx dest;
601 rtx src;
602 {
603 rtx p, q;
604 rtx set;
605 int sregno = REGNO (src);
606 int dregno = REGNO (dest);
607
608 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
609 {
610 /* ??? We can't scan past the end of a basic block without updating
611 the register lifetime info (REG_DEAD/basic_block_live_at_start). */
612 if (perhaps_ends_bb_p (p))
613 break;
614 else if (! INSN_P (p))
615 continue;
616
617 set = single_set (p);
618 if (set && SET_SRC (set) == dest && SET_DEST (set) == src
619 && find_reg_note (p, REG_DEAD, dest))
620 {
621 /* We can do the optimization. Scan forward from INSN again,
622 replacing regs as we go. */
623
624 /* Set to stop at next insn. */
625 for (q = insn; q != NEXT_INSN (p); q = NEXT_INSN (q))
626 if (INSN_P (q))
627 {
628 if (reg_mentioned_p (dest, PATTERN (q)))
629 PATTERN (q) = replace_rtx (PATTERN (q), dest, src);
630
631
632 if (GET_CODE (q) == CALL_INSN)
633 {
634 REG_N_CALLS_CROSSED (dregno)--;
635 REG_N_CALLS_CROSSED (sregno)++;
636 }
637 }
638
639 remove_note (p, find_reg_note (p, REG_DEAD, dest));
640 REG_N_DEATHS (dregno)--;
641 remove_note (insn, find_reg_note (insn, REG_DEAD, src));
642 REG_N_DEATHS (sregno)--;
643 return;
644 }
645
646 if (reg_set_p (src, p)
647 || find_reg_note (p, REG_DEAD, dest)
648 || (GET_CODE (p) == CALL_INSN && REG_N_CALLS_CROSSED (sregno) == 0))
649 break;
650 }
651 }
652 /* INSN is a ZERO_EXTEND or SIGN_EXTEND of SRC to DEST.
653 Look if SRC dies there, and if it is only set once, by loading
654 it from memory. If so, try to encorporate the zero/sign extension
655 into the memory read, change SRC to the mode of DEST, and alter
656 the remaining accesses to use the appropriate SUBREG. This allows
657 SRC and DEST to be tied later. */
658 static void
659 optimize_reg_copy_3 (insn, dest, src)
660 rtx insn;
661 rtx dest;
662 rtx src;
663 {
664 rtx src_reg = XEXP (src, 0);
665 int src_no = REGNO (src_reg);
666 int dst_no = REGNO (dest);
667 rtx p, set, subreg;
668 enum machine_mode old_mode;
669
670 if (src_no < FIRST_PSEUDO_REGISTER
671 || dst_no < FIRST_PSEUDO_REGISTER
672 || ! find_reg_note (insn, REG_DEAD, src_reg)
673 || REG_N_SETS (src_no) != 1)
674 return;
675 for (p = PREV_INSN (insn); p && ! reg_set_p (src_reg, p); p = PREV_INSN (p))
676 /* ??? We can't scan past the end of a basic block without updating
677 the register lifetime info (REG_DEAD/basic_block_live_at_start). */
678 if (perhaps_ends_bb_p (p))
679 break;
680
681 if (! p)
682 return;
683
684 if (! (set = single_set (p))
685 || GET_CODE (SET_SRC (set)) != MEM
686 || SET_DEST (set) != src_reg)
687 return;
688
689 /* Be conserative: although this optimization is also valid for
690 volatile memory references, that could cause trouble in later passes. */
691 if (MEM_VOLATILE_P (SET_SRC (set)))
692 return;
693
694 /* Do not use a SUBREG to truncate from one mode to another if truncation
695 is not a nop. */
696 if (GET_MODE_BITSIZE (GET_MODE (src_reg)) <= GET_MODE_BITSIZE (GET_MODE (src))
697 && !TRULY_NOOP_TRUNCATION (GET_MODE_BITSIZE (GET_MODE (src)),
698 GET_MODE_BITSIZE (GET_MODE (src_reg))))
699 return;
700
701 old_mode = GET_MODE (src_reg);
702 PUT_MODE (src_reg, GET_MODE (src));
703 XEXP (src, 0) = SET_SRC (set);
704
705 /* Include this change in the group so that it's easily undone if
706 one of the changes in the group is invalid. */
707 validate_change (p, &SET_SRC (set), src, 1);
708
709 /* Now walk forward making additional replacements. We want to be able
710 to undo all the changes if a later substitution fails. */
711 subreg = gen_rtx_SUBREG (old_mode, src_reg, 0);
712 while (p = NEXT_INSN (p), p != insn)
713 {
714 if (! INSN_P (p))
715 continue;
716
717 /* Make a tenative change. */
718 validate_replace_rtx_group (src_reg, subreg, p);
719 }
720
721 validate_replace_rtx_group (src, src_reg, insn);
722
723 /* Now see if all the changes are valid. */
724 if (! apply_change_group ())
725 {
726 /* One or more changes were no good. Back out everything. */
727 PUT_MODE (src_reg, old_mode);
728 XEXP (src, 0) = src_reg;
729 }
730 }
731
732 \f
733 /* If we were not able to update the users of src to use dest directly, try
734 instead moving the value to dest directly before the operation. */
735
736 static void
737 copy_src_to_dest (insn, src, dest, old_max_uid)
738 rtx insn;
739 rtx src;
740 rtx dest;
741 int old_max_uid;
742 {
743 rtx seq;
744 rtx link;
745 rtx next;
746 rtx set;
747 rtx move_insn;
748 rtx *p_insn_notes;
749 rtx *p_move_notes;
750 int src_regno;
751 int dest_regno;
752 int bb;
753 int insn_uid;
754 int move_uid;
755
756 /* A REG_LIVE_LENGTH of -1 indicates the register is equivalent to a constant
757 or memory location and is used infrequently; a REG_LIVE_LENGTH of -2 is
758 parameter when there is no frame pointer that is not allocated a register.
759 For now, we just reject them, rather than incrementing the live length. */
760
761 if (GET_CODE (src) == REG
762 && REG_LIVE_LENGTH (REGNO (src)) > 0
763 && GET_CODE (dest) == REG
764 && !RTX_UNCHANGING_P (dest)
765 && REG_LIVE_LENGTH (REGNO (dest)) > 0
766 && (set = single_set (insn)) != NULL_RTX
767 && !reg_mentioned_p (dest, SET_SRC (set))
768 && GET_MODE (src) == GET_MODE (dest))
769 {
770 int old_num_regs = reg_rtx_no;
771
772 /* Generate the src->dest move. */
773 start_sequence ();
774 emit_move_insn (dest, src);
775 seq = gen_sequence ();
776 end_sequence ();
777 /* If this sequence uses new registers, we may not use it. */
778 if (old_num_regs != reg_rtx_no
779 || ! validate_replace_rtx (src, dest, insn))
780 {
781 /* We have to restore reg_rtx_no to its old value, lest
782 recompute_reg_usage will try to compute the usage of the
783 new regs, yet reg_n_info is not valid for them. */
784 reg_rtx_no = old_num_regs;
785 return;
786 }
787 emit_insn_before (seq, insn);
788 move_insn = PREV_INSN (insn);
789 p_move_notes = &REG_NOTES (move_insn);
790 p_insn_notes = &REG_NOTES (insn);
791
792 /* Move any notes mentioning src to the move instruction */
793 for (link = REG_NOTES (insn); link != NULL_RTX; link = next)
794 {
795 next = XEXP (link, 1);
796 if (XEXP (link, 0) == src)
797 {
798 *p_move_notes = link;
799 p_move_notes = &XEXP (link, 1);
800 }
801 else
802 {
803 *p_insn_notes = link;
804 p_insn_notes = &XEXP (link, 1);
805 }
806 }
807
808 *p_move_notes = NULL_RTX;
809 *p_insn_notes = NULL_RTX;
810
811 /* Is the insn the head of a basic block? If so extend it */
812 insn_uid = INSN_UID (insn);
813 move_uid = INSN_UID (move_insn);
814 if (insn_uid < old_max_uid)
815 {
816 bb = regmove_bb_head[insn_uid];
817 if (bb >= 0)
818 {
819 BLOCK_HEAD (bb) = move_insn;
820 regmove_bb_head[insn_uid] = -1;
821 }
822 }
823
824 /* Update the various register tables. */
825 dest_regno = REGNO (dest);
826 REG_N_SETS (dest_regno) ++;
827 REG_LIVE_LENGTH (dest_regno)++;
828 if (REGNO_FIRST_UID (dest_regno) == insn_uid)
829 REGNO_FIRST_UID (dest_regno) = move_uid;
830
831 src_regno = REGNO (src);
832 if (! find_reg_note (move_insn, REG_DEAD, src))
833 REG_LIVE_LENGTH (src_regno)++;
834
835 if (REGNO_FIRST_UID (src_regno) == insn_uid)
836 REGNO_FIRST_UID (src_regno) = move_uid;
837
838 if (REGNO_LAST_UID (src_regno) == insn_uid)
839 REGNO_LAST_UID (src_regno) = move_uid;
840
841 if (REGNO_LAST_NOTE_UID (src_regno) == insn_uid)
842 REGNO_LAST_NOTE_UID (src_regno) = move_uid;
843 }
844 }
845
846 \f
847 /* Return whether REG is set in only one location, and is set to a
848 constant, but is set in a different basic block from INSN (an
849 instructions which uses REG). In this case REG is equivalent to a
850 constant, and we don't want to break that equivalence, because that
851 may increase register pressure and make reload harder. If REG is
852 set in the same basic block as INSN, we don't worry about it,
853 because we'll probably need a register anyhow (??? but what if REG
854 is used in a different basic block as well as this one?). FIRST is
855 the first insn in the function. */
856
857 static int
858 reg_is_remote_constant_p (reg, insn, first)
859 rtx reg;
860 rtx insn;
861 rtx first;
862 {
863 register rtx p;
864
865 if (REG_N_SETS (REGNO (reg)) != 1)
866 return 0;
867
868 /* Look for the set. */
869 for (p = LOG_LINKS (insn); p; p = XEXP (p, 1))
870 {
871 rtx s;
872
873 if (REG_NOTE_KIND (p) != 0)
874 continue;
875 s = single_set (XEXP (p, 0));
876 if (s != 0
877 && GET_CODE (SET_DEST (s)) == REG
878 && REGNO (SET_DEST (s)) == REGNO (reg))
879 {
880 /* The register is set in the same basic block. */
881 return 0;
882 }
883 }
884
885 for (p = first; p && p != insn; p = NEXT_INSN (p))
886 {
887 rtx s;
888
889 if (! INSN_P (p))
890 continue;
891 s = single_set (p);
892 if (s != 0
893 && GET_CODE (SET_DEST (s)) == REG
894 && REGNO (SET_DEST (s)) == REGNO (reg))
895 {
896 /* This is the instruction which sets REG. If there is a
897 REG_EQUAL note, then REG is equivalent to a constant. */
898 if (find_reg_note (p, REG_EQUAL, NULL_RTX))
899 return 1;
900 return 0;
901 }
902 }
903
904 return 0;
905 }
906
907 /* INSN is adding a CONST_INT to a REG. We search backwards looking for
908 another add immediate instruction with the same source and dest registers,
909 and if we find one, we change INSN to an increment, and return 1. If
910 no changes are made, we return 0.
911
912 This changes
913 (set (reg100) (plus reg1 offset1))
914 ...
915 (set (reg100) (plus reg1 offset2))
916 to
917 (set (reg100) (plus reg1 offset1))
918 ...
919 (set (reg100) (plus reg100 offset2-offset1)) */
920
921 /* ??? What does this comment mean? */
922 /* cse disrupts preincrement / postdecrement squences when it finds a
923 hard register as ultimate source, like the frame pointer. */
924
925 static int
926 fixup_match_2 (insn, dst, src, offset, regmove_dump_file)
927 rtx insn, dst, src, offset;
928 FILE *regmove_dump_file;
929 {
930 rtx p, dst_death = 0;
931 int length, num_calls = 0;
932
933 /* If SRC dies in INSN, we'd have to move the death note. This is
934 considered to be very unlikely, so we just skip the optimization
935 in this case. */
936 if (find_regno_note (insn, REG_DEAD, REGNO (src)))
937 return 0;
938
939 /* Scan backward to find the first instruction that sets DST. */
940
941 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
942 {
943 rtx pset;
944
945 /* ??? We can't scan past the end of a basic block without updating
946 the register lifetime info (REG_DEAD/basic_block_live_at_start). */
947 if (perhaps_ends_bb_p (p))
948 break;
949 else if (! INSN_P (p))
950 continue;
951
952 if (find_regno_note (p, REG_DEAD, REGNO (dst)))
953 dst_death = p;
954 if (! dst_death)
955 length++;
956
957 pset = single_set (p);
958 if (pset && SET_DEST (pset) == dst
959 && GET_CODE (SET_SRC (pset)) == PLUS
960 && XEXP (SET_SRC (pset), 0) == src
961 && GET_CODE (XEXP (SET_SRC (pset), 1)) == CONST_INT)
962 {
963 HOST_WIDE_INT newconst
964 = INTVAL (offset) - INTVAL (XEXP (SET_SRC (pset), 1));
965 rtx add = gen_add3_insn (dst, dst, GEN_INT (newconst));
966
967 if (add && validate_change (insn, &PATTERN (insn), add, 0))
968 {
969 /* Remove the death note for DST from DST_DEATH. */
970 if (dst_death)
971 {
972 remove_death (REGNO (dst), dst_death);
973 REG_LIVE_LENGTH (REGNO (dst)) += length;
974 REG_N_CALLS_CROSSED (REGNO (dst)) += num_calls;
975 }
976
977 if (regmove_dump_file)
978 fprintf (regmove_dump_file,
979 "Fixed operand of insn %d.\n",
980 INSN_UID (insn));
981
982 #ifdef AUTO_INC_DEC
983 for (p = PREV_INSN (insn); p; p = PREV_INSN (p))
984 {
985 if (GET_CODE (p) == CODE_LABEL
986 || GET_CODE (p) == JUMP_INSN)
987 break;
988 if (! INSN_P (p))
989 continue;
990 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
991 {
992 if (try_auto_increment (p, insn, 0, dst, newconst, 0))
993 return 1;
994 break;
995 }
996 }
997 for (p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
998 {
999 if (GET_CODE (p) == CODE_LABEL
1000 || GET_CODE (p) == JUMP_INSN)
1001 break;
1002 if (! INSN_P (p))
1003 continue;
1004 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1005 {
1006 try_auto_increment (p, insn, 0, dst, newconst, 1);
1007 break;
1008 }
1009 }
1010 #endif
1011 return 1;
1012 }
1013 }
1014
1015 if (reg_set_p (dst, PATTERN (p)))
1016 break;
1017
1018 /* If we have passed a call instruction, and the
1019 pseudo-reg SRC is not already live across a call,
1020 then don't perform the optimization. */
1021 /* reg_set_p is overly conservative for CALL_INSNS, thinks that all
1022 hard regs are clobbered. Thus, we only use it for src for
1023 non-call insns. */
1024 if (GET_CODE (p) == CALL_INSN)
1025 {
1026 if (! dst_death)
1027 num_calls++;
1028
1029 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1030 break;
1031
1032 if (call_used_regs [REGNO (dst)]
1033 || find_reg_fusage (p, CLOBBER, dst))
1034 break;
1035 }
1036 else if (reg_set_p (src, PATTERN (p)))
1037 break;
1038 }
1039
1040 return 0;
1041 }
1042
1043 void
1044 regmove_optimize (f, nregs, regmove_dump_file)
1045 rtx f;
1046 int nregs;
1047 FILE *regmove_dump_file;
1048 {
1049 int old_max_uid = get_max_uid ();
1050 rtx insn;
1051 struct match match;
1052 int pass;
1053 int i;
1054 rtx copy_src, copy_dst;
1055
1056 /* Find out where a potential flags register is live, and so that we
1057 can supress some optimizations in those zones. */
1058 mark_flags_life_zones (discover_flags_reg ());
1059
1060 regno_src_regno = (int *) xmalloc (sizeof *regno_src_regno * nregs);
1061 for (i = nregs; --i >= 0; ) regno_src_regno[i] = -1;
1062
1063 regmove_bb_head = (int *) xmalloc (sizeof (int) * (old_max_uid + 1));
1064 for (i = old_max_uid; i >= 0; i--) regmove_bb_head[i] = -1;
1065 for (i = 0; i < n_basic_blocks; i++)
1066 regmove_bb_head[INSN_UID (BLOCK_HEAD (i))] = i;
1067
1068 /* A forward/backward pass. Replace output operands with input operands. */
1069
1070 for (pass = 0; pass <= 2; pass++)
1071 {
1072 if (! flag_regmove && pass >= flag_expensive_optimizations)
1073 goto done;
1074
1075 if (regmove_dump_file)
1076 fprintf (regmove_dump_file, "Starting %s pass...\n",
1077 pass ? "backward" : "forward");
1078
1079 for (insn = pass ? get_last_insn () : f; insn;
1080 insn = pass ? PREV_INSN (insn) : NEXT_INSN (insn))
1081 {
1082 rtx set;
1083 int op_no, match_no;
1084
1085 set = single_set (insn);
1086 if (! set)
1087 continue;
1088
1089 if (flag_expensive_optimizations && ! pass
1090 && (GET_CODE (SET_SRC (set)) == SIGN_EXTEND
1091 || GET_CODE (SET_SRC (set)) == ZERO_EXTEND)
1092 && GET_CODE (XEXP (SET_SRC (set), 0)) == REG
1093 && GET_CODE (SET_DEST(set)) == REG)
1094 optimize_reg_copy_3 (insn, SET_DEST (set), SET_SRC (set));
1095
1096 if (flag_expensive_optimizations && ! pass
1097 && GET_CODE (SET_SRC (set)) == REG
1098 && GET_CODE (SET_DEST(set)) == REG)
1099 {
1100 /* If this is a register-register copy where SRC is not dead,
1101 see if we can optimize it. If this optimization succeeds,
1102 it will become a copy where SRC is dead. */
1103 if ((find_reg_note (insn, REG_DEAD, SET_SRC (set))
1104 || optimize_reg_copy_1 (insn, SET_DEST (set), SET_SRC (set)))
1105 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
1106 {
1107 /* Similarly for a pseudo-pseudo copy when SRC is dead. */
1108 if (REGNO (SET_SRC (set)) >= FIRST_PSEUDO_REGISTER)
1109 optimize_reg_copy_2 (insn, SET_DEST (set), SET_SRC (set));
1110 if (regno_src_regno[REGNO (SET_DEST (set))] < 0
1111 && SET_SRC (set) != SET_DEST (set))
1112 {
1113 int srcregno = REGNO (SET_SRC(set));
1114 if (regno_src_regno[srcregno] >= 0)
1115 srcregno = regno_src_regno[srcregno];
1116 regno_src_regno[REGNO (SET_DEST (set))] = srcregno;
1117 }
1118 }
1119 }
1120 if (! flag_regmove)
1121 continue;
1122
1123 if (! find_matches (insn, &match))
1124 continue;
1125
1126 /* Now scan through the operands looking for a source operand
1127 which is supposed to match the destination operand.
1128 Then scan forward for an instruction which uses the dest
1129 operand.
1130 If it dies there, then replace the dest in both operands with
1131 the source operand. */
1132
1133 for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1134 {
1135 rtx src, dst, src_subreg;
1136 enum reg_class src_class, dst_class;
1137
1138 match_no = match.with[op_no];
1139
1140 /* Nothing to do if the two operands aren't supposed to match. */
1141 if (match_no < 0)
1142 continue;
1143
1144 src = recog_data.operand[op_no];
1145 dst = recog_data.operand[match_no];
1146
1147 if (GET_CODE (src) != REG)
1148 continue;
1149
1150 src_subreg = src;
1151 if (GET_CODE (dst) == SUBREG
1152 && GET_MODE_SIZE (GET_MODE (dst))
1153 >= GET_MODE_SIZE (GET_MODE (SUBREG_REG (dst))))
1154 {
1155 src_subreg
1156 = gen_rtx_SUBREG (GET_MODE (SUBREG_REG (dst)),
1157 src, SUBREG_WORD (dst));
1158 dst = SUBREG_REG (dst);
1159 }
1160 if (GET_CODE (dst) != REG
1161 || REGNO (dst) < FIRST_PSEUDO_REGISTER)
1162 continue;
1163
1164 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1165 {
1166 if (match.commutative[op_no] < op_no)
1167 regno_src_regno[REGNO (dst)] = REGNO (src);
1168 continue;
1169 }
1170
1171 if (REG_LIVE_LENGTH (REGNO (src)) < 0)
1172 continue;
1173
1174 /* op_no/src must be a read-only operand, and
1175 match_operand/dst must be a write-only operand. */
1176 if (match.use[op_no] != READ
1177 || match.use[match_no] != WRITE)
1178 continue;
1179
1180 if (match.early_clobber[match_no]
1181 && count_occurrences (PATTERN (insn), src, 0) > 1)
1182 continue;
1183
1184 /* Make sure match_operand is the destination. */
1185 if (recog_data.operand[match_no] != SET_DEST (set))
1186 continue;
1187
1188 /* If the operands already match, then there is nothing to do. */
1189 if (operands_match_p (src, dst))
1190 continue;
1191
1192 /* But in the commutative case, we might find a better match. */
1193 if (match.commutative[op_no] >= 0)
1194 {
1195 rtx comm = recog_data.operand[match.commutative[op_no]];
1196 if (operands_match_p (comm, dst)
1197 && (replacement_quality (comm)
1198 >= replacement_quality (src)))
1199 continue;
1200 }
1201
1202 src_class = reg_preferred_class (REGNO (src));
1203 dst_class = reg_preferred_class (REGNO (dst));
1204 if (! regclass_compatible_p (src_class, dst_class))
1205 continue;
1206
1207 if (fixup_match_1 (insn, set, src, src_subreg, dst, pass,
1208 op_no, match_no,
1209 regmove_dump_file))
1210 break;
1211 }
1212 }
1213 }
1214
1215 /* A backward pass. Replace input operands with output operands. */
1216
1217 if (regmove_dump_file)
1218 fprintf (regmove_dump_file, "Starting backward pass...\n");
1219
1220 for (insn = get_last_insn (); insn; insn = PREV_INSN (insn))
1221 {
1222 if (INSN_P (insn))
1223 {
1224 int op_no, match_no;
1225 int success = 0;
1226
1227 if (! find_matches (insn, &match))
1228 continue;
1229
1230 /* Now scan through the operands looking for a destination operand
1231 which is supposed to match a source operand.
1232 Then scan backward for an instruction which sets the source
1233 operand. If safe, then replace the source operand with the
1234 dest operand in both instructions. */
1235
1236 copy_src = NULL_RTX;
1237 copy_dst = NULL_RTX;
1238 for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1239 {
1240 rtx set, p, src, dst;
1241 rtx src_note, dst_note;
1242 int num_calls = 0;
1243 enum reg_class src_class, dst_class;
1244 int length;
1245
1246 match_no = match.with[op_no];
1247
1248 /* Nothing to do if the two operands aren't supposed to match. */
1249 if (match_no < 0)
1250 continue;
1251
1252 dst = recog_data.operand[match_no];
1253 src = recog_data.operand[op_no];
1254
1255 if (GET_CODE (src) != REG)
1256 continue;
1257
1258 if (GET_CODE (dst) != REG
1259 || REGNO (dst) < FIRST_PSEUDO_REGISTER
1260 || REG_LIVE_LENGTH (REGNO (dst)) < 0)
1261 continue;
1262
1263 /* If the operands already match, then there is nothing to do. */
1264 if (operands_match_p (src, dst))
1265 continue;
1266
1267 if (match.commutative[op_no] >= 0)
1268 {
1269 rtx comm = recog_data.operand[match.commutative[op_no]];
1270 if (operands_match_p (comm, dst))
1271 continue;
1272 }
1273
1274 set = single_set (insn);
1275 if (! set)
1276 continue;
1277
1278 /* match_no/dst must be a write-only operand, and
1279 operand_operand/src must be a read-only operand. */
1280 if (match.use[op_no] != READ
1281 || match.use[match_no] != WRITE)
1282 continue;
1283
1284 if (match.early_clobber[match_no]
1285 && count_occurrences (PATTERN (insn), src, 0) > 1)
1286 continue;
1287
1288 /* Make sure match_no is the destination. */
1289 if (recog_data.operand[match_no] != SET_DEST (set))
1290 continue;
1291
1292 if (REGNO (src) < FIRST_PSEUDO_REGISTER)
1293 {
1294 if (GET_CODE (SET_SRC (set)) == PLUS
1295 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT
1296 && XEXP (SET_SRC (set), 0) == src
1297 && fixup_match_2 (insn, dst, src,
1298 XEXP (SET_SRC (set), 1),
1299 regmove_dump_file))
1300 break;
1301 continue;
1302 }
1303 src_class = reg_preferred_class (REGNO (src));
1304 dst_class = reg_preferred_class (REGNO (dst));
1305 if (! regclass_compatible_p (src_class, dst_class))
1306 {
1307 if (!copy_src)
1308 {
1309 copy_src = src;
1310 copy_dst = dst;
1311 }
1312 continue;
1313 }
1314
1315 /* Can not modify an earlier insn to set dst if this insn
1316 uses an old value in the source. */
1317 if (reg_overlap_mentioned_p (dst, SET_SRC (set)))
1318 {
1319 if (!copy_src)
1320 {
1321 copy_src = src;
1322 copy_dst = dst;
1323 }
1324 continue;
1325 }
1326
1327 if (! (src_note = find_reg_note (insn, REG_DEAD, src)))
1328 {
1329 if (!copy_src)
1330 {
1331 copy_src = src;
1332 copy_dst = dst;
1333 }
1334 continue;
1335 }
1336
1337
1338 /* If src is set once in a different basic block,
1339 and is set equal to a constant, then do not use
1340 it for this optimization, as this would make it
1341 no longer equivalent to a constant. */
1342
1343 if (reg_is_remote_constant_p (src, insn, f))
1344 {
1345 if (!copy_src)
1346 {
1347 copy_src = src;
1348 copy_dst = dst;
1349 }
1350 continue;
1351 }
1352
1353
1354 if (regmove_dump_file)
1355 fprintf (regmove_dump_file,
1356 "Could fix operand %d of insn %d matching operand %d.\n",
1357 op_no, INSN_UID (insn), match_no);
1358
1359 /* Scan backward to find the first instruction that uses
1360 the input operand. If the operand is set here, then
1361 replace it in both instructions with match_no. */
1362
1363 for (length = 0, p = PREV_INSN (insn); p; p = PREV_INSN (p))
1364 {
1365 rtx pset;
1366
1367 /* ??? We can't scan past the end of a basic block without
1368 updating the register lifetime info
1369 (REG_DEAD/basic_block_live_at_start). */
1370 if (perhaps_ends_bb_p (p))
1371 break;
1372 else if (! INSN_P (p))
1373 continue;
1374
1375 length++;
1376
1377 /* ??? See if all of SRC is set in P. This test is much
1378 more conservative than it needs to be. */
1379 pset = single_set (p);
1380 if (pset && SET_DEST (pset) == src)
1381 {
1382 /* We use validate_replace_rtx, in case there
1383 are multiple identical source operands. All of
1384 them have to be changed at the same time. */
1385 if (validate_replace_rtx (src, dst, insn))
1386 {
1387 if (validate_change (p, &SET_DEST (pset),
1388 dst, 0))
1389 success = 1;
1390 else
1391 {
1392 /* Change all source operands back.
1393 This modifies the dst as a side-effect. */
1394 validate_replace_rtx (dst, src, insn);
1395 /* Now make sure the dst is right. */
1396 validate_change (insn,
1397 recog_data.operand_loc[match_no],
1398 dst, 0);
1399 }
1400 }
1401 break;
1402 }
1403
1404 if (reg_overlap_mentioned_p (src, PATTERN (p))
1405 || reg_overlap_mentioned_p (dst, PATTERN (p)))
1406 break;
1407
1408 /* If we have passed a call instruction, and the
1409 pseudo-reg DST is not already live across a call,
1410 then don't perform the optimization. */
1411 if (GET_CODE (p) == CALL_INSN)
1412 {
1413 num_calls++;
1414
1415 if (REG_N_CALLS_CROSSED (REGNO (dst)) == 0)
1416 break;
1417 }
1418 }
1419
1420 if (success)
1421 {
1422 int dstno, srcno;
1423
1424 /* Remove the death note for SRC from INSN. */
1425 remove_note (insn, src_note);
1426 /* Move the death note for SRC to P if it is used
1427 there. */
1428 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1429 {
1430 XEXP (src_note, 1) = REG_NOTES (p);
1431 REG_NOTES (p) = src_note;
1432 }
1433 /* If there is a REG_DEAD note for DST on P, then remove
1434 it, because DST is now set there. */
1435 if ((dst_note = find_reg_note (p, REG_DEAD, dst)))
1436 remove_note (p, dst_note);
1437
1438 dstno = REGNO (dst);
1439 srcno = REGNO (src);
1440
1441 REG_N_SETS (dstno)++;
1442 REG_N_SETS (srcno)--;
1443
1444 REG_N_CALLS_CROSSED (dstno) += num_calls;
1445 REG_N_CALLS_CROSSED (srcno) -= num_calls;
1446
1447 REG_LIVE_LENGTH (dstno) += length;
1448 if (REG_LIVE_LENGTH (srcno) >= 0)
1449 {
1450 REG_LIVE_LENGTH (srcno) -= length;
1451 /* REG_LIVE_LENGTH is only an approximation after
1452 combine if sched is not run, so make sure that we
1453 still have a reasonable value. */
1454 if (REG_LIVE_LENGTH (srcno) < 2)
1455 REG_LIVE_LENGTH (srcno) = 2;
1456 }
1457
1458 if (regmove_dump_file)
1459 fprintf (regmove_dump_file,
1460 "Fixed operand %d of insn %d matching operand %d.\n",
1461 op_no, INSN_UID (insn), match_no);
1462
1463 break;
1464 }
1465 }
1466
1467 /* If we weren't able to replace any of the alternatives, try an
1468 alternative appoach of copying the source to the destination. */
1469 if (!success && copy_src != NULL_RTX)
1470 copy_src_to_dest (insn, copy_src, copy_dst, old_max_uid);
1471
1472 }
1473 }
1474
1475 /* In fixup_match_1, some insns may have been inserted after basic block
1476 ends. Fix that here. */
1477 for (i = 0; i < n_basic_blocks; i++)
1478 {
1479 rtx end = BLOCK_END (i);
1480 rtx new = end;
1481 rtx next = NEXT_INSN (new);
1482 while (next != 0 && INSN_UID (next) >= old_max_uid
1483 && (i == n_basic_blocks - 1 || BLOCK_HEAD (i + 1) != next))
1484 new = next, next = NEXT_INSN (new);
1485 BLOCK_END (i) = new;
1486 }
1487
1488 done:
1489 /* Clean up. */
1490 free (regno_src_regno);
1491 free (regmove_bb_head);
1492 }
1493
1494 /* Returns nonzero if INSN's pattern has matching constraints for any operand.
1495 Returns 0 if INSN can't be recognized, or if the alternative can't be
1496 determined.
1497
1498 Initialize the info in MATCHP based on the constraints. */
1499
1500 static int
1501 find_matches (insn, matchp)
1502 rtx insn;
1503 struct match *matchp;
1504 {
1505 int likely_spilled[MAX_RECOG_OPERANDS];
1506 int op_no;
1507 int any_matches = 0;
1508
1509 extract_insn (insn);
1510 if (! constrain_operands (0))
1511 return 0;
1512
1513 /* Must initialize this before main loop, because the code for
1514 the commutative case may set matches for operands other than
1515 the current one. */
1516 for (op_no = recog_data.n_operands; --op_no >= 0; )
1517 matchp->with[op_no] = matchp->commutative[op_no] = -1;
1518
1519 for (op_no = 0; op_no < recog_data.n_operands; op_no++)
1520 {
1521 const char *p;
1522 char c;
1523 int i = 0;
1524
1525 p = recog_data.constraints[op_no];
1526
1527 likely_spilled[op_no] = 0;
1528 matchp->use[op_no] = READ;
1529 matchp->early_clobber[op_no] = 0;
1530 if (*p == '=')
1531 matchp->use[op_no] = WRITE;
1532 else if (*p == '+')
1533 matchp->use[op_no] = READWRITE;
1534
1535 for (;*p && i < which_alternative; p++)
1536 if (*p == ',')
1537 i++;
1538
1539 while ((c = *p++) != '\0' && c != ',')
1540 switch (c)
1541 {
1542 case '=':
1543 break;
1544 case '+':
1545 break;
1546 case '&':
1547 matchp->early_clobber[op_no] = 1;
1548 break;
1549 case '%':
1550 matchp->commutative[op_no] = op_no + 1;
1551 matchp->commutative[op_no + 1] = op_no;
1552 break;
1553 case '0': case '1': case '2': case '3': case '4':
1554 case '5': case '6': case '7': case '8': case '9':
1555 c -= '0';
1556 if (c < op_no && likely_spilled[(unsigned char) c])
1557 break;
1558 matchp->with[op_no] = c;
1559 any_matches = 1;
1560 if (matchp->commutative[op_no] >= 0)
1561 matchp->with[matchp->commutative[op_no]] = c;
1562 break;
1563 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': case 'h':
1564 case 'j': case 'k': case 'l': case 'p': case 'q': case 't': case 'u':
1565 case 'v': case 'w': case 'x': case 'y': case 'z': case 'A': case 'B':
1566 case 'C': case 'D': case 'W': case 'Y': case 'Z':
1567 if (CLASS_LIKELY_SPILLED_P (REG_CLASS_FROM_LETTER ((unsigned char)c)))
1568 likely_spilled[op_no] = 1;
1569 break;
1570 }
1571 }
1572 return any_matches;
1573 }
1574
1575 /* Try to replace all occurrences of DST_REG with SRC in LOC, that is
1576 assumed to be in INSN. */
1577
1578 static void
1579 replace_in_call_usage (loc, dst_reg, src, insn)
1580 rtx *loc;
1581 int dst_reg;
1582 rtx src;
1583 rtx insn;
1584 {
1585 rtx x = *loc;
1586 enum rtx_code code;
1587 const char *fmt;
1588 int i, j;
1589
1590 if (! x)
1591 return;
1592
1593 code = GET_CODE (x);
1594 if (code == REG)
1595 {
1596 if (REGNO (x) != dst_reg)
1597 return;
1598
1599 validate_change (insn, loc, src, 1);
1600
1601 return;
1602 }
1603
1604 /* Process each of our operands recursively. */
1605 fmt = GET_RTX_FORMAT (code);
1606 for (i = 0; i < GET_RTX_LENGTH (code); i++, fmt++)
1607 if (*fmt == 'e')
1608 replace_in_call_usage (&XEXP (x, i), dst_reg, src, insn);
1609 else if (*fmt == 'E')
1610 for (j = 0; j < XVECLEN (x, i); j++)
1611 replace_in_call_usage (& XVECEXP (x, i, j), dst_reg, src, insn);
1612 }
1613
1614 /* Try to replace output operand DST in SET, with input operand SRC. SET is
1615 the only set in INSN. INSN has just been recognized and constrained.
1616 SRC is operand number OPERAND_NUMBER in INSN.
1617 DST is operand number MATCH_NUMBER in INSN.
1618 If BACKWARD is nonzero, we have been called in a backward pass.
1619 Return nonzero for success. */
1620
1621 static int
1622 fixup_match_1 (insn, set, src, src_subreg, dst, backward, operand_number,
1623 match_number, regmove_dump_file)
1624 rtx insn, set, src, src_subreg, dst;
1625 int backward, operand_number, match_number;
1626 FILE *regmove_dump_file;
1627 {
1628 rtx p;
1629 rtx post_inc = 0, post_inc_set = 0, search_end = 0;
1630 int success = 0;
1631 int num_calls = 0, s_num_calls = 0;
1632 enum rtx_code code = NOTE;
1633 HOST_WIDE_INT insn_const = 0, newconst;
1634 rtx overlap = 0; /* need to move insn ? */
1635 rtx src_note = find_reg_note (insn, REG_DEAD, src), dst_note = NULL_RTX;
1636 int length, s_length;
1637
1638 /* If SRC is marked as unchanging, we may not change it.
1639 ??? Maybe we could get better code by removing the unchanging bit
1640 instead, and changing it back if we don't succeed? */
1641 if (RTX_UNCHANGING_P (src))
1642 return 0;
1643
1644 if (! src_note)
1645 {
1646 /* Look for (set (regX) (op regA constX))
1647 (set (regY) (op regA constY))
1648 and change that to
1649 (set (regA) (op regA constX)).
1650 (set (regY) (op regA constY-constX)).
1651 This works for add and shift operations, if
1652 regA is dead after or set by the second insn. */
1653
1654 code = GET_CODE (SET_SRC (set));
1655 if ((code == PLUS || code == LSHIFTRT
1656 || code == ASHIFT || code == ASHIFTRT)
1657 && XEXP (SET_SRC (set), 0) == src
1658 && GET_CODE (XEXP (SET_SRC (set), 1)) == CONST_INT)
1659 insn_const = INTVAL (XEXP (SET_SRC (set), 1));
1660 else if (! stable_and_no_regs_but_for_p (SET_SRC (set), src, dst))
1661 return 0;
1662 else
1663 /* We might find a src_note while scanning. */
1664 code = NOTE;
1665 }
1666
1667 if (regmove_dump_file)
1668 fprintf (regmove_dump_file,
1669 "Could fix operand %d of insn %d matching operand %d.\n",
1670 operand_number, INSN_UID (insn), match_number);
1671
1672 /* If SRC is equivalent to a constant set in a different basic block,
1673 then do not use it for this optimization. We want the equivalence
1674 so that if we have to reload this register, we can reload the
1675 constant, rather than extending the lifespan of the register. */
1676 if (reg_is_remote_constant_p (src, insn, get_insns ()))
1677 return 0;
1678
1679 /* Scan forward to find the next instruction that
1680 uses the output operand. If the operand dies here,
1681 then replace it in both instructions with
1682 operand_number. */
1683
1684 for (length = s_length = 0, p = NEXT_INSN (insn); p; p = NEXT_INSN (p))
1685 {
1686 if (GET_CODE (p) == CALL_INSN)
1687 replace_in_call_usage (& CALL_INSN_FUNCTION_USAGE (p),
1688 REGNO (dst), src, p);
1689
1690 /* ??? We can't scan past the end of a basic block without updating
1691 the register lifetime info (REG_DEAD/basic_block_live_at_start). */
1692 if (perhaps_ends_bb_p (p))
1693 break;
1694 else if (! INSN_P (p))
1695 continue;
1696
1697 length++;
1698 if (src_note)
1699 s_length++;
1700
1701 if (reg_set_p (src, p) || reg_set_p (dst, p)
1702 || (GET_CODE (PATTERN (p)) == USE
1703 && reg_overlap_mentioned_p (src, XEXP (PATTERN (p), 0))))
1704 break;
1705
1706 /* See if all of DST dies in P. This test is
1707 slightly more conservative than it needs to be. */
1708 if ((dst_note = find_regno_note (p, REG_DEAD, REGNO (dst)))
1709 && (GET_MODE (XEXP (dst_note, 0)) == GET_MODE (dst)))
1710 {
1711 /* If we would be moving INSN, check that we won't move it
1712 into the shadow of a live a live flags register. */
1713 /* ??? We only try to move it in front of P, although
1714 we could move it anywhere between OVERLAP and P. */
1715 if (overlap && GET_MODE (PREV_INSN (p)) != VOIDmode)
1716 break;
1717
1718 if (! src_note)
1719 {
1720 rtx q;
1721 rtx set2 = NULL_RTX;
1722
1723 /* If an optimization is done, the value of SRC while P
1724 is executed will be changed. Check that this is OK. */
1725 if (reg_overlap_mentioned_p (src, PATTERN (p)))
1726 break;
1727 for (q = p; q; q = NEXT_INSN (q))
1728 {
1729 /* ??? We can't scan past the end of a basic block without
1730 updating the register lifetime info
1731 (REG_DEAD/basic_block_live_at_start). */
1732 if (perhaps_ends_bb_p (q))
1733 {
1734 q = 0;
1735 break;
1736 }
1737 else if (! INSN_P (q))
1738 continue;
1739 else if (reg_overlap_mentioned_p (src, PATTERN (q))
1740 || reg_set_p (src, q))
1741 break;
1742 }
1743 if (q)
1744 set2 = single_set (q);
1745 if (! q || ! set2 || GET_CODE (SET_SRC (set2)) != code
1746 || XEXP (SET_SRC (set2), 0) != src
1747 || GET_CODE (XEXP (SET_SRC (set2), 1)) != CONST_INT
1748 || (SET_DEST (set2) != src
1749 && ! find_reg_note (q, REG_DEAD, src)))
1750 {
1751 /* If this is a PLUS, we can still save a register by doing
1752 src += insn_const;
1753 P;
1754 src -= insn_const; .
1755 This also gives opportunities for subsequent
1756 optimizations in the backward pass, so do it there. */
1757 if (code == PLUS && backward
1758 /* Don't do this if we can likely tie DST to SET_DEST
1759 of P later; we can't do this tying here if we got a
1760 hard register. */
1761 && ! (dst_note && ! REG_N_CALLS_CROSSED (REGNO (dst))
1762 && single_set (p)
1763 && GET_CODE (SET_DEST (single_set (p))) == REG
1764 && (REGNO (SET_DEST (single_set (p)))
1765 < FIRST_PSEUDO_REGISTER))
1766 /* We may only emit an insn directly after P if we
1767 are not in the shadow of a live flags register. */
1768 && GET_MODE (p) == VOIDmode)
1769 {
1770 search_end = q;
1771 q = insn;
1772 set2 = set;
1773 newconst = -insn_const;
1774 code = MINUS;
1775 }
1776 else
1777 break;
1778 }
1779 else
1780 {
1781 newconst = INTVAL (XEXP (SET_SRC (set2), 1)) - insn_const;
1782 /* Reject out of range shifts. */
1783 if (code != PLUS
1784 && (newconst < 0
1785 || ((unsigned HOST_WIDE_INT) newconst
1786 >= (GET_MODE_BITSIZE (GET_MODE
1787 (SET_SRC (set2)))))))
1788 break;
1789 if (code == PLUS)
1790 {
1791 post_inc = q;
1792 if (SET_DEST (set2) != src)
1793 post_inc_set = set2;
1794 }
1795 }
1796 /* We use 1 as last argument to validate_change so that all
1797 changes are accepted or rejected together by apply_change_group
1798 when it is called by validate_replace_rtx . */
1799 validate_change (q, &XEXP (SET_SRC (set2), 1),
1800 GEN_INT (newconst), 1);
1801 }
1802 validate_change (insn, recog_data.operand_loc[match_number], src, 1);
1803 if (validate_replace_rtx (dst, src_subreg, p))
1804 success = 1;
1805 break;
1806 }
1807
1808 if (reg_overlap_mentioned_p (dst, PATTERN (p)))
1809 break;
1810 if (! src_note && reg_overlap_mentioned_p (src, PATTERN (p)))
1811 {
1812 /* INSN was already checked to be movable wrt. the registers that it
1813 sets / uses when we found no REG_DEAD note for src on it, but it
1814 still might clobber the flags register. We'll have to check that
1815 we won't insert it into the shadow of a live flags register when
1816 we finally know where we are to move it. */
1817 overlap = p;
1818 src_note = find_reg_note (p, REG_DEAD, src);
1819 }
1820
1821 /* If we have passed a call instruction, and the pseudo-reg SRC is not
1822 already live across a call, then don't perform the optimization. */
1823 if (GET_CODE (p) == CALL_INSN)
1824 {
1825 if (REG_N_CALLS_CROSSED (REGNO (src)) == 0)
1826 break;
1827
1828 num_calls++;
1829
1830 if (src_note)
1831 s_num_calls++;
1832
1833 }
1834 }
1835
1836 if (! success)
1837 return 0;
1838
1839 /* Remove the death note for DST from P. */
1840 remove_note (p, dst_note);
1841 if (code == MINUS)
1842 {
1843 post_inc = emit_insn_after (copy_rtx (PATTERN (insn)), p);
1844 if ((HAVE_PRE_INCREMENT || HAVE_PRE_DECREMENT)
1845 && search_end
1846 && try_auto_increment (search_end, post_inc, 0, src, newconst, 1))
1847 post_inc = 0;
1848 validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (insn_const), 0);
1849 REG_N_SETS (REGNO (src))++;
1850 REG_LIVE_LENGTH (REGNO (src))++;
1851 }
1852 if (overlap)
1853 {
1854 /* The lifetime of src and dest overlap,
1855 but we can change this by moving insn. */
1856 rtx pat = PATTERN (insn);
1857 if (src_note)
1858 remove_note (overlap, src_note);
1859 if ((HAVE_POST_INCREMENT || HAVE_POST_DECREMENT)
1860 && code == PLUS
1861 && try_auto_increment (overlap, insn, 0, src, insn_const, 0))
1862 insn = overlap;
1863 else
1864 {
1865 rtx notes = REG_NOTES (insn);
1866
1867 emit_insn_after_with_line_notes (pat, PREV_INSN (p), insn);
1868 PUT_CODE (insn, NOTE);
1869 NOTE_LINE_NUMBER (insn) = NOTE_INSN_DELETED;
1870 NOTE_SOURCE_FILE (insn) = 0;
1871 /* emit_insn_after_with_line_notes has no
1872 return value, so search for the new insn. */
1873 insn = p;
1874 while (! INSN_P (insn) || PATTERN (insn) != pat)
1875 insn = PREV_INSN (insn);
1876
1877 REG_NOTES (insn) = notes;
1878 }
1879 }
1880 /* Sometimes we'd generate src = const; src += n;
1881 if so, replace the instruction that set src
1882 in the first place. */
1883
1884 if (! overlap && (code == PLUS || code == MINUS))
1885 {
1886 rtx note = find_reg_note (insn, REG_EQUAL, NULL_RTX);
1887 rtx q, set2 = NULL_RTX;
1888 int num_calls2 = 0, s_length2 = 0;
1889
1890 if (note && CONSTANT_P (XEXP (note, 0)))
1891 {
1892 for (q = PREV_INSN (insn); q; q = PREV_INSN(q))
1893 {
1894 /* ??? We can't scan past the end of a basic block without
1895 updating the register lifetime info
1896 (REG_DEAD/basic_block_live_at_start). */
1897 if (perhaps_ends_bb_p (q))
1898 {
1899 q = 0;
1900 break;
1901 }
1902 else if (! INSN_P (q))
1903 continue;
1904
1905 s_length2++;
1906 if (reg_set_p (src, q))
1907 {
1908 set2 = single_set (q);
1909 break;
1910 }
1911 if (reg_overlap_mentioned_p (src, PATTERN (q)))
1912 {
1913 q = 0;
1914 break;
1915 }
1916 if (GET_CODE (p) == CALL_INSN)
1917 num_calls2++;
1918 }
1919 if (q && set2 && SET_DEST (set2) == src && CONSTANT_P (SET_SRC (set2))
1920 && validate_change (insn, &SET_SRC (set), XEXP (note, 0), 0))
1921 {
1922 PUT_CODE (q, NOTE);
1923 NOTE_LINE_NUMBER (q) = NOTE_INSN_DELETED;
1924 NOTE_SOURCE_FILE (q) = 0;
1925 REG_N_SETS (REGNO (src))--;
1926 REG_N_CALLS_CROSSED (REGNO (src)) -= num_calls2;
1927 REG_LIVE_LENGTH (REGNO (src)) -= s_length2;
1928 insn_const = 0;
1929 }
1930 }
1931 }
1932
1933 if ((HAVE_PRE_INCREMENT || HAVE_PRE_DECREMENT)
1934 && (code == PLUS || code == MINUS) && insn_const
1935 && try_auto_increment (p, insn, 0, src, insn_const, 1))
1936 insn = p;
1937 else if ((HAVE_POST_INCREMENT || HAVE_POST_DECREMENT)
1938 && post_inc
1939 && try_auto_increment (p, post_inc, post_inc_set, src, newconst, 0))
1940 post_inc = 0;
1941 /* If post_inc still prevails, try to find an
1942 insn where it can be used as a pre-in/decrement.
1943 If code is MINUS, this was already tried. */
1944 if (post_inc && code == PLUS
1945 /* Check that newconst is likely to be usable
1946 in a pre-in/decrement before starting the search. */
1947 && ((HAVE_PRE_INCREMENT && newconst > 0 && newconst <= MOVE_MAX)
1948 || (HAVE_PRE_DECREMENT && newconst < 0 && newconst >= -MOVE_MAX))
1949 && exact_log2 (newconst))
1950 {
1951 rtx q, inc_dest;
1952
1953 inc_dest = post_inc_set ? SET_DEST (post_inc_set) : src;
1954 for (q = post_inc; (q = NEXT_INSN (q)); )
1955 {
1956 /* ??? We can't scan past the end of a basic block without updating
1957 the register lifetime info
1958 (REG_DEAD/basic_block_live_at_start). */
1959 if (perhaps_ends_bb_p (q))
1960 break;
1961 else if (! INSN_P (q))
1962 continue;
1963 else if (src != inc_dest
1964 && (reg_overlap_mentioned_p (src, PATTERN (q))
1965 || reg_set_p (src, q)))
1966 break;
1967 else if (reg_set_p (inc_dest, q))
1968 break;
1969 else if (reg_overlap_mentioned_p (inc_dest, PATTERN (q)))
1970 {
1971 try_auto_increment (q, post_inc,
1972 post_inc_set, inc_dest, newconst, 1);
1973 break;
1974 }
1975 }
1976 }
1977
1978 /* Move the death note for DST to INSN if it is used
1979 there. */
1980 if (reg_overlap_mentioned_p (dst, PATTERN (insn)))
1981 {
1982 XEXP (dst_note, 1) = REG_NOTES (insn);
1983 REG_NOTES (insn) = dst_note;
1984 }
1985
1986 if (src_note)
1987 {
1988 /* Move the death note for SRC from INSN to P. */
1989 if (! overlap)
1990 remove_note (insn, src_note);
1991 XEXP (src_note, 1) = REG_NOTES (p);
1992 REG_NOTES (p) = src_note;
1993
1994 REG_N_CALLS_CROSSED (REGNO (src)) += s_num_calls;
1995 }
1996
1997 REG_N_SETS (REGNO (src))++;
1998 REG_N_SETS (REGNO (dst))--;
1999
2000 REG_N_CALLS_CROSSED (REGNO (dst)) -= num_calls;
2001
2002 REG_LIVE_LENGTH (REGNO (src)) += s_length;
2003 if (REG_LIVE_LENGTH (REGNO (dst)) >= 0)
2004 {
2005 REG_LIVE_LENGTH (REGNO (dst)) -= length;
2006 /* REG_LIVE_LENGTH is only an approximation after
2007 combine if sched is not run, so make sure that we
2008 still have a reasonable value. */
2009 if (REG_LIVE_LENGTH (REGNO (dst)) < 2)
2010 REG_LIVE_LENGTH (REGNO (dst)) = 2;
2011 }
2012 if (regmove_dump_file)
2013 fprintf (regmove_dump_file,
2014 "Fixed operand %d of insn %d matching operand %d.\n",
2015 operand_number, INSN_UID (insn), match_number);
2016 return 1;
2017 }
2018
2019
2020 /* return nonzero if X is stable and mentions no regsiters but for
2021 mentioning SRC or mentioning / changing DST . If in doubt, presume
2022 it is unstable.
2023 The rationale is that we want to check if we can move an insn easily
2024 while just paying attention to SRC and DST. A register is considered
2025 stable if it has the RTX_UNCHANGING_P bit set, but that would still
2026 leave the burden to update REG_DEAD / REG_UNUSED notes, so we don't
2027 want any registers but SRC and DST. */
2028 static int
2029 stable_and_no_regs_but_for_p (x, src, dst)
2030 rtx x, src, dst;
2031 {
2032 RTX_CODE code = GET_CODE (x);
2033 switch (GET_RTX_CLASS (code))
2034 {
2035 case '<': case '1': case 'c': case '2': case 'b': case '3':
2036 {
2037 int i;
2038 const char *fmt = GET_RTX_FORMAT (code);
2039 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2040 if (fmt[i] == 'e'
2041 && ! stable_and_no_regs_but_for_p (XEXP (x, i), src, dst))
2042 return 0;
2043 return 1;
2044 }
2045 case 'o':
2046 if (code == REG)
2047 return x == src || x == dst;
2048 /* If this is a MEM, look inside - there might be a register hidden in
2049 the address of an unchanging MEM. */
2050 if (code == MEM
2051 && ! stable_and_no_regs_but_for_p (XEXP (x, 0), src, dst))
2052 return 0;
2053 /* fall through */
2054 default:
2055 return ! rtx_unstable_p (x);
2056 }
2057 }
2058 \f
2059 /* Track stack adjustments and stack memory references. Attempt to
2060 reduce the number of stack adjustments by back-propogating across
2061 the memory references.
2062
2063 This is intended primarily for use with targets that do not define
2064 ACCUMULATE_OUTGOING_ARGS. It is of significantly more value to
2065 targets that define PREFERRED_STACK_BOUNDARY more aligned than
2066 STACK_BOUNDARY (e.g. x86), or if not all registers can be pushed
2067 (e.g. x86 fp regs) which would ordinarily have to be implemented
2068 as a sub/mov pair due to restrictions in calls.c.
2069
2070 Propogation stops when any of the insns that need adjusting are
2071 (a) no longer valid because we've exceeded their range, (b) a
2072 non-trivial push instruction, or (c) a call instruction.
2073
2074 Restriction B is based on the assumption that push instructions
2075 are smaller or faster. If a port really wants to remove all
2076 pushes, it should have defined ACCUMULATE_OUTGOING_ARGS. The
2077 one exception that is made is for an add immediately followed
2078 by a push. */
2079
2080 /* This structure records stack memory references between stack adjusting
2081 instructions. */
2082
2083 struct csa_memlist
2084 {
2085 HOST_WIDE_INT sp_offset;
2086 rtx insn, *mem;
2087 struct csa_memlist *next;
2088 };
2089
2090 static int stack_memref_p PARAMS ((rtx));
2091 static rtx single_set_for_csa PARAMS ((rtx));
2092 static void free_csa_memlist PARAMS ((struct csa_memlist *));
2093 static struct csa_memlist *record_one_stack_memref
2094 PARAMS ((rtx, rtx *, struct csa_memlist *));
2095 static int try_apply_stack_adjustment
2096 PARAMS ((rtx, struct csa_memlist *, HOST_WIDE_INT, HOST_WIDE_INT));
2097 static void combine_stack_adjustments_for_block PARAMS ((basic_block));
2098 static int record_stack_memrefs PARAMS ((rtx *, void *));
2099
2100
2101 /* Main entry point for stack adjustment combination. */
2102
2103 void
2104 combine_stack_adjustments ()
2105 {
2106 int i;
2107
2108 for (i = 0; i < n_basic_blocks; ++i)
2109 combine_stack_adjustments_for_block (BASIC_BLOCK (i));
2110 }
2111
2112 /* Recognize a MEM of the form (sp) or (plus sp const). */
2113
2114 static int
2115 stack_memref_p (x)
2116 rtx x;
2117 {
2118 if (GET_CODE (x) != MEM)
2119 return 0;
2120 x = XEXP (x, 0);
2121
2122 if (x == stack_pointer_rtx)
2123 return 1;
2124 if (GET_CODE (x) == PLUS
2125 && XEXP (x, 0) == stack_pointer_rtx
2126 && GET_CODE (XEXP (x, 1)) == CONST_INT)
2127 return 1;
2128
2129 return 0;
2130 }
2131
2132 /* Recognize either normal single_set or the hack in i386.md for
2133 tying fp and sp adjustments. */
2134
2135 static rtx
2136 single_set_for_csa (insn)
2137 rtx insn;
2138 {
2139 int i;
2140 rtx tmp = single_set (insn);
2141 if (tmp)
2142 return tmp;
2143
2144 if (GET_CODE (insn) != INSN
2145 || GET_CODE (PATTERN (insn)) != PARALLEL)
2146 return NULL_RTX;
2147
2148 tmp = PATTERN (insn);
2149 if (GET_CODE (XVECEXP (tmp, 0, 0)) != SET)
2150 return NULL_RTX;
2151
2152 for (i = 1; i < XVECLEN (tmp, 0); ++i)
2153 {
2154 rtx this = XVECEXP (tmp, 0, i);
2155
2156 /* The special case is allowing a no-op set. */
2157 if (GET_CODE (this) == SET
2158 && SET_SRC (this) == SET_DEST (this))
2159 ;
2160 else if (GET_CODE (this) != CLOBBER
2161 && GET_CODE (this) != USE)
2162 return NULL_RTX;
2163 }
2164
2165 return XVECEXP (tmp, 0, 0);
2166 }
2167
2168 /* Free the list of csa_memlist nodes. */
2169
2170 static void
2171 free_csa_memlist (memlist)
2172 struct csa_memlist *memlist;
2173 {
2174 struct csa_memlist *next;
2175 for (; memlist ; memlist = next)
2176 {
2177 next = memlist->next;
2178 free (memlist);
2179 }
2180 }
2181
2182 /* Create a new csa_memlist node from the given memory reference.
2183 It is already known that the memory is stack_memref_p. */
2184
2185 static struct csa_memlist *
2186 record_one_stack_memref (insn, mem, next_memlist)
2187 rtx insn, *mem;
2188 struct csa_memlist *next_memlist;
2189 {
2190 struct csa_memlist *ml;
2191
2192 ml = (struct csa_memlist *) xmalloc (sizeof (*ml));
2193
2194 if (XEXP (*mem, 0) == stack_pointer_rtx)
2195 ml->sp_offset = 0;
2196 else
2197 ml->sp_offset = INTVAL (XEXP (XEXP (*mem, 0), 1));
2198
2199 ml->insn = insn;
2200 ml->mem = mem;
2201 ml->next = next_memlist;
2202
2203 return ml;
2204 }
2205
2206 /* Attempt to apply ADJUST to the stack adjusting insn INSN, as well
2207 as each of the memories in MEMLIST. Return true on success. */
2208
2209 static int
2210 try_apply_stack_adjustment (insn, memlist, new_adjust, delta)
2211 rtx insn;
2212 struct csa_memlist *memlist;
2213 HOST_WIDE_INT new_adjust, delta;
2214 {
2215 struct csa_memlist *ml;
2216 rtx set;
2217
2218 /* We know INSN matches single_set_for_csa, because that's what we
2219 recognized earlier. However, if INSN is not single_set, it is
2220 doing double duty as a barrier for frame pointer memory accesses,
2221 which we are not recording. Therefore, an adjust insn that is not
2222 single_set may not have a positive delta applied. */
2223
2224 if (delta > 0 && ! single_set (insn))
2225 return 0;
2226 set = single_set_for_csa (insn);
2227 validate_change (insn, &XEXP (SET_SRC (set), 1), GEN_INT (new_adjust), 1);
2228
2229 for (ml = memlist; ml ; ml = ml->next)
2230 {
2231 HOST_WIDE_INT c = ml->sp_offset - delta;
2232 rtx new = gen_rtx_MEM (GET_MODE (*ml->mem),
2233 plus_constant (stack_pointer_rtx, c));
2234
2235 /* Don't reference memory below the stack pointer. */
2236 if (c < 0)
2237 {
2238 cancel_changes (0);
2239 return 0;
2240 }
2241
2242 MEM_COPY_ATTRIBUTES (new, *ml->mem);
2243 validate_change (ml->insn, ml->mem, new, 1);
2244 }
2245
2246 if (apply_change_group ())
2247 {
2248 /* Succeeded. Update our knowledge of the memory references. */
2249 for (ml = memlist; ml ; ml = ml->next)
2250 ml->sp_offset -= delta;
2251
2252 return 1;
2253 }
2254 else
2255 return 0;
2256 }
2257
2258 /* Called via for_each_rtx and used to record all stack memory references in
2259 the insn and discard all other stack pointer references. */
2260 struct record_stack_memrefs_data
2261 {
2262 rtx insn;
2263 struct csa_memlist *memlist;
2264 };
2265
2266 static int
2267 record_stack_memrefs (xp, data)
2268 rtx *xp;
2269 void *data;
2270 {
2271 rtx x = *xp;
2272 struct record_stack_memrefs_data *d =
2273 (struct record_stack_memrefs_data *) data;
2274 if (!x)
2275 return 0;
2276 switch (GET_CODE (x))
2277 {
2278 case MEM:
2279 if (!reg_mentioned_p (stack_pointer_rtx, x))
2280 return -1;
2281 /* We are not able to handle correctly all possible memrefs containing
2282 stack pointer, so this check is neccesary. */
2283 if (stack_memref_p (x))
2284 {
2285 d->memlist = record_one_stack_memref (d->insn, xp, d->memlist);
2286 return -1;
2287 }
2288 return 1;
2289 case REG:
2290 /* ??? We want be able to handle non-memory stack pointer references
2291 later. For now just discard all insns refering to stack pointer
2292 outside mem expressions. We would probably want to teach
2293 validate_replace to simplify expressions first. */
2294 if (x == stack_pointer_rtx)
2295 return 1;
2296 break;
2297 default:
2298 break;
2299 }
2300 return 0;
2301 }
2302
2303 /* Subroutine of combine_stack_adjustments, called for each basic block. */
2304
2305 static void
2306 combine_stack_adjustments_for_block (bb)
2307 basic_block bb;
2308 {
2309 HOST_WIDE_INT last_sp_adjust = 0;
2310 rtx last_sp_set = NULL_RTX;
2311 struct csa_memlist *memlist = NULL;
2312 rtx pending_delete;
2313 rtx insn, next;
2314 struct record_stack_memrefs_data data;
2315
2316 for (insn = bb->head; ; insn = next)
2317 {
2318 rtx set;
2319
2320 pending_delete = NULL_RTX;
2321 next = NEXT_INSN (insn);
2322
2323 if (! INSN_P (insn))
2324 goto processed;
2325
2326 set = single_set_for_csa (insn);
2327 if (set)
2328 {
2329 rtx dest = SET_DEST (set);
2330 rtx src = SET_SRC (set);
2331
2332 /* Find constant additions to the stack pointer. */
2333 if (dest == stack_pointer_rtx
2334 && GET_CODE (src) == PLUS
2335 && XEXP (src, 0) == stack_pointer_rtx
2336 && GET_CODE (XEXP (src, 1)) == CONST_INT)
2337 {
2338 HOST_WIDE_INT this_adjust = INTVAL (XEXP (src, 1));
2339
2340 /* If we've not seen an adjustment previously, record
2341 it now and continue. */
2342 if (! last_sp_set)
2343 {
2344 last_sp_set = insn;
2345 last_sp_adjust = this_adjust;
2346 goto processed;
2347 }
2348
2349 /* If not all recorded memrefs can be adjusted, or the
2350 adjustment is now too large for a constant addition,
2351 we cannot merge the two stack adjustments. */
2352 if (! try_apply_stack_adjustment (last_sp_set, memlist,
2353 last_sp_adjust + this_adjust,
2354 this_adjust))
2355 {
2356 free_csa_memlist (memlist);
2357 memlist = NULL;
2358 last_sp_set = insn;
2359 last_sp_adjust = this_adjust;
2360 goto processed;
2361 }
2362
2363 /* It worked! */
2364 pending_delete = insn;
2365 last_sp_adjust += this_adjust;
2366
2367 /* If, by some accident, the adjustments cancel out,
2368 delete both insns and start from scratch. */
2369 if (last_sp_adjust == 0)
2370 {
2371 if (last_sp_set == bb->head)
2372 bb->head = NEXT_INSN (last_sp_set);
2373 flow_delete_insn (last_sp_set);
2374
2375 free_csa_memlist (memlist);
2376 memlist = NULL;
2377 last_sp_set = NULL_RTX;
2378 }
2379
2380 goto processed;
2381 }
2382
2383 /* Find a predecrement of exactly the previous adjustment and
2384 turn it into a direct store. Obviously we can't do this if
2385 there were any intervening uses of the stack pointer. */
2386 if (memlist == NULL
2387 && GET_CODE (dest) == MEM
2388 && ((GET_CODE (XEXP (dest, 0)) == PRE_DEC
2389 && (last_sp_adjust
2390 == (HOST_WIDE_INT) GET_MODE_SIZE (GET_MODE (dest))))
2391 || (GET_CODE (XEXP (dest, 0)) == PRE_MODIFY
2392 && GET_CODE (XEXP (XEXP (dest, 0), 1)) == PLUS
2393 && XEXP (XEXP (XEXP (dest, 0), 1), 0) == stack_pointer_rtx
2394 && (GET_CODE (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2395 == CONST_INT)
2396 && (INTVAL (XEXP (XEXP (XEXP (dest, 0), 1), 1))
2397 == -last_sp_adjust)))
2398 && XEXP (XEXP (dest, 0), 0) == stack_pointer_rtx
2399 && ! reg_mentioned_p (stack_pointer_rtx, src)
2400 && memory_address_p (GET_MODE (dest), stack_pointer_rtx)
2401 && validate_change (insn, &SET_DEST (set),
2402 change_address (dest, VOIDmode,
2403 stack_pointer_rtx), 0))
2404 {
2405 if (last_sp_set == bb->head)
2406 bb->head = NEXT_INSN (last_sp_set);
2407 flow_delete_insn (last_sp_set);
2408
2409 free_csa_memlist (memlist);
2410 memlist = NULL;
2411 last_sp_set = NULL_RTX;
2412 last_sp_adjust = 0;
2413 goto processed;
2414 }
2415 }
2416
2417 data.insn = insn;
2418 data.memlist = memlist;
2419 if (GET_CODE (insn) != CALL_INSN && last_sp_set
2420 && !for_each_rtx (&PATTERN (insn), record_stack_memrefs, &data))
2421 {
2422 memlist = data.memlist;
2423 goto processed;
2424 }
2425 memlist = data.memlist;
2426
2427 /* Otherwise, we were not able to process the instruction.
2428 Do not continue collecting data across such a one. */
2429 if (last_sp_set
2430 && (GET_CODE (insn) == CALL_INSN
2431 || reg_mentioned_p (stack_pointer_rtx, PATTERN (insn))))
2432 {
2433 free_csa_memlist (memlist);
2434 memlist = NULL;
2435 last_sp_set = NULL_RTX;
2436 last_sp_adjust = 0;
2437 }
2438
2439 processed:
2440 if (insn == bb->end)
2441 break;
2442
2443 if (pending_delete)
2444 flow_delete_insn (pending_delete);
2445 }
2446
2447 if (pending_delete)
2448 {
2449 bb->end = PREV_INSN (pending_delete);
2450 flow_delete_insn (pending_delete);
2451 }
2452 }