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

Last change on this file since 2 was 2, checked in by bird, 23 years ago

Initial revision

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