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