source: trunk/src/gcc/gcc/regmove.c@ 1392

Last change on this file since 1392 was 1392, checked in by bird, 21 years ago

This commit was generated by cvs2svn to compensate for changes in r1391,
which included commits to RCS files with non-trunk default branches.

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