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