OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-old/] [gcc-4.2.2/] [gcc/] [regmove.c] - Blame information for rev 816

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.