MipsConstantIslandPass.cpp revision a2e6e6bcf8cc37ad91b130b9d02d9fe951fbb4d1
1//===-- MipsConstantIslandPass.cpp - Emit Pc Relative loads----------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//
11// This pass is used to make Pc relative loads of constants.
12// For now, only Mips16 will use this.
13//
14// Loading constants inline is expensive on Mips16 and it's in general better
15// to place the constant nearby in code space and then it can be loaded with a
16// simple 16 bit load instruction.
17//
18// The constants can be not just numbers but addresses of functions and labels.
19// This can be particularly helpful in static relocation mode for embedded
20// non linux targets.
21//
22//
23
24#define DEBUG_TYPE "mips-constant-islands"
25
26#include "Mips.h"
27#include "MCTargetDesc/MipsBaseInfo.h"
28#include "MipsMachineFunction.h"
29#include "MipsTargetMachine.h"
30#include "llvm/ADT/Statistic.h"
31#include "llvm/CodeGen/MachineBasicBlock.h"
32#include "llvm/CodeGen/MachineFunctionPass.h"
33#include "llvm/CodeGen/MachineInstrBuilder.h"
34#include "llvm/CodeGen/MachineRegisterInfo.h"
35#include "llvm/IR/Function.h"
36#include "llvm/Support/CommandLine.h"
37#include "llvm/Support/Debug.h"
38#include "llvm/Support/InstIterator.h"
39#include "llvm/Support/MathExtras.h"
40#include "llvm/Support/raw_ostream.h"
41#include "llvm/Target/TargetInstrInfo.h"
42#include "llvm/Target/TargetMachine.h"
43#include "llvm/Target/TargetRegisterInfo.h"
44#include "llvm/Support/Format.h"
45#include <algorithm>
46
47using namespace llvm;
48
49STATISTIC(NumCPEs,       "Number of constpool entries");
50STATISTIC(NumSplit,      "Number of uncond branches inserted");
51STATISTIC(NumCBrFixed,   "Number of cond branches fixed");
52STATISTIC(NumUBrFixed,   "Number of uncond branches fixed");
53
54// FIXME: This option should be removed once it has received sufficient testing.
55static cl::opt<bool>
56AlignConstantIslands("mips-align-constant-islands", cl::Hidden, cl::init(true),
57          cl::desc("Align constant islands in code"));
58
59
60// Rather than do make check tests with huge amounts of code, we force
61// the test to use this amount.
62//
63static cl::opt<int> ConstantIslandsSmallOffset(
64  "mips-constant-islands-small-offset",
65  cl::init(0),
66  cl::desc("Make small offsets be this amount for testing purposes"),
67  cl::Hidden);
68
69/// UnknownPadding - Return the worst case padding that could result from
70/// unknown offset bits.  This does not include alignment padding caused by
71/// known offset bits.
72///
73/// @param LogAlign log2(alignment)
74/// @param KnownBits Number of known low offset bits.
75static inline unsigned UnknownPadding(unsigned LogAlign, unsigned KnownBits) {
76  if (KnownBits < LogAlign)
77    return (1u << LogAlign) - (1u << KnownBits);
78  return 0;
79}
80
81namespace {
82
83
84  typedef MachineBasicBlock::iterator Iter;
85  typedef MachineBasicBlock::reverse_iterator ReverseIter;
86
87  /// MipsConstantIslands - Due to limited PC-relative displacements, Mips
88  /// requires constant pool entries to be scattered among the instructions
89  /// inside a function.  To do this, it completely ignores the normal LLVM
90  /// constant pool; instead, it places constants wherever it feels like with
91  /// special instructions.
92  ///
93  /// The terminology used in this pass includes:
94  ///   Islands - Clumps of constants placed in the function.
95  ///   Water   - Potential places where an island could be formed.
96  ///   CPE     - A constant pool entry that has been placed somewhere, which
97  ///             tracks a list of users.
98
99  class MipsConstantIslands : public MachineFunctionPass {
100
101    /// BasicBlockInfo - Information about the offset and size of a single
102    /// basic block.
103    struct BasicBlockInfo {
104      /// Offset - Distance from the beginning of the function to the beginning
105      /// of this basic block.
106      ///
107      /// Offsets are computed assuming worst case padding before an aligned
108      /// block. This means that subtracting basic block offsets always gives a
109      /// conservative estimate of the real distance which may be smaller.
110      ///
111      /// Because worst case padding is used, the computed offset of an aligned
112      /// block may not actually be aligned.
113      unsigned Offset;
114
115      /// Size - Size of the basic block in bytes.  If the block contains
116      /// inline assembly, this is a worst case estimate.
117      ///
118      /// The size does not include any alignment padding whether from the
119      /// beginning of the block, or from an aligned jump table at the end.
120      unsigned Size;
121
122      /// KnownBits - The number of low bits in Offset that are known to be
123      /// exact.  The remaining bits of Offset are an upper bound.
124      uint8_t KnownBits;
125
126      /// Unalign - When non-zero, the block contains instructions (inline asm)
127      /// of unknown size.  The real size may be smaller than Size bytes by a
128      /// multiple of 1 << Unalign.
129      uint8_t Unalign;
130
131      /// PostAlign - When non-zero, the block terminator contains a .align
132      /// directive, so the end of the block is aligned to 1 << PostAlign
133      /// bytes.
134      uint8_t PostAlign;
135
136      BasicBlockInfo() : Offset(0), Size(0), KnownBits(0), Unalign(0),
137        PostAlign(0) {}
138
139      /// Compute the number of known offset bits internally to this block.
140      /// This number should be used to predict worst case padding when
141      /// splitting the block.
142      unsigned internalKnownBits() const {
143        unsigned Bits = Unalign ? Unalign : KnownBits;
144        // If the block size isn't a multiple of the known bits, assume the
145        // worst case padding.
146        if (Size & ((1u << Bits) - 1))
147          Bits = countTrailingZeros(Size);
148        return Bits;
149      }
150
151      /// Compute the offset immediately following this block.  If LogAlign is
152      /// specified, return the offset the successor block will get if it has
153      /// this alignment.
154      unsigned postOffset(unsigned LogAlign = 0) const {
155        unsigned PO = Offset + Size;
156        return PO;
157      }
158
159      /// Compute the number of known low bits of postOffset.  If this block
160      /// contains inline asm, the number of known bits drops to the
161      /// instruction alignment.  An aligned terminator may increase the number
162      /// of know bits.
163      /// If LogAlign is given, also consider the alignment of the next block.
164      unsigned postKnownBits(unsigned LogAlign = 0) const {
165        return std::max(std::max(unsigned(PostAlign), LogAlign),
166                        internalKnownBits());
167      }
168    };
169
170    std::vector<BasicBlockInfo> BBInfo;
171
172    /// WaterList - A sorted list of basic blocks where islands could be placed
173    /// (i.e. blocks that don't fall through to the following block, due
174    /// to a return, unreachable, or unconditional branch).
175    std::vector<MachineBasicBlock*> WaterList;
176
177    /// NewWaterList - The subset of WaterList that was created since the
178    /// previous iteration by inserting unconditional branches.
179    SmallSet<MachineBasicBlock*, 4> NewWaterList;
180
181    typedef std::vector<MachineBasicBlock*>::iterator water_iterator;
182
183    /// CPUser - One user of a constant pool, keeping the machine instruction
184    /// pointer, the constant pool being referenced, and the max displacement
185    /// allowed from the instruction to the CP.  The HighWaterMark records the
186    /// highest basic block where a new CPEntry can be placed.  To ensure this
187    /// pass terminates, the CP entries are initially placed at the end of the
188    /// function and then move monotonically to lower addresses.  The
189    /// exception to this rule is when the current CP entry for a particular
190    /// CPUser is out of range, but there is another CP entry for the same
191    /// constant value in range.  We want to use the existing in-range CP
192    /// entry, but if it later moves out of range, the search for new water
193    /// should resume where it left off.  The HighWaterMark is used to record
194    /// that point.
195    struct CPUser {
196      MachineInstr *MI;
197      MachineInstr *CPEMI;
198      MachineBasicBlock *HighWaterMark;
199    private:
200      unsigned MaxDisp;
201      unsigned LongFormMaxDisp; // mips16 has 16/32 bit instructions
202                                // with different displacements
203      unsigned LongFormOpcode;
204    public:
205      bool NegOk;
206      bool IsSoImm;
207      bool KnownAlignment;
208      CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp,
209             bool neg, bool soimm,
210             unsigned longformmaxdisp, unsigned longformopcode)
211        : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp),
212          LongFormMaxDisp(longformmaxdisp), LongFormOpcode(longformopcode),
213          NegOk(neg), IsSoImm(soimm), KnownAlignment(false)  {
214        HighWaterMark = CPEMI->getParent();
215      }
216      /// getMaxDisp - Returns the maximum displacement supported by MI.
217      /// Correct for unknown alignment.
218      /// Conservatively subtract 2 bytes to handle weird alignment effects.
219      unsigned getMaxDisp() const {
220        unsigned xMaxDisp = ConstantIslandsSmallOffset?
221                            ConstantIslandsSmallOffset: MaxDisp;
222        return (KnownAlignment ? xMaxDisp : xMaxDisp - 2) - 2;
223      }
224      unsigned getLongFormMaxDisp() const {
225        return (KnownAlignment ? LongFormMaxDisp : LongFormMaxDisp - 2) - 2;
226      }
227      unsigned getLongFormOpcode() const {
228          return LongFormOpcode;
229      }
230    };
231
232    /// CPUsers - Keep track of all of the machine instructions that use various
233    /// constant pools and their max displacement.
234    std::vector<CPUser> CPUsers;
235
236  /// CPEntry - One per constant pool entry, keeping the machine instruction
237  /// pointer, the constpool index, and the number of CPUser's which
238  /// reference this entry.
239  struct CPEntry {
240    MachineInstr *CPEMI;
241    unsigned CPI;
242    unsigned RefCount;
243    CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0)
244      : CPEMI(cpemi), CPI(cpi), RefCount(rc) {}
245  };
246
247  /// CPEntries - Keep track of all of the constant pool entry machine
248  /// instructions. For each original constpool index (i.e. those that
249  /// existed upon entry to this pass), it keeps a vector of entries.
250  /// Original elements are cloned as we go along; the clones are
251  /// put in the vector of the original element, but have distinct CPIs.
252  std::vector<std::vector<CPEntry> > CPEntries;
253
254  /// ImmBranch - One per immediate branch, keeping the machine instruction
255  /// pointer, conditional or unconditional, the max displacement,
256  /// and (if isCond is true) the corresponding unconditional branch
257  /// opcode.
258  struct ImmBranch {
259    MachineInstr *MI;
260    unsigned MaxDisp : 31;
261    bool isCond : 1;
262    int UncondBr;
263    ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
264      : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
265  };
266
267  /// ImmBranches - Keep track of all the immediate branch instructions.
268  ///
269  std::vector<ImmBranch> ImmBranches;
270
271  /// HasFarJump - True if any far jump instruction has been emitted during
272  /// the branch fix up pass.
273  bool HasFarJump;
274
275  const TargetMachine &TM;
276  bool IsPIC;
277  unsigned ABI;
278  const MipsSubtarget *STI;
279  const MipsInstrInfo *TII;
280  MipsFunctionInfo *MFI;
281  MachineFunction *MF;
282  MachineConstantPool *MCP;
283
284  unsigned PICLabelUId;
285  bool PrescannedForConstants;
286
287  void initPICLabelUId(unsigned UId) {
288    PICLabelUId = UId;
289  }
290
291
292  unsigned createPICLabelUId() {
293    return PICLabelUId++;
294  }
295
296  public:
297    static char ID;
298    MipsConstantIslands(TargetMachine &tm)
299      : MachineFunctionPass(ID), TM(tm),
300        IsPIC(TM.getRelocationModel() == Reloc::PIC_),
301        ABI(TM.getSubtarget<MipsSubtarget>().getTargetABI()),
302        STI(&TM.getSubtarget<MipsSubtarget>()), MF(0), MCP(0),
303        PrescannedForConstants(false){}
304
305    virtual const char *getPassName() const {
306      return "Mips Constant Islands";
307    }
308
309    bool runOnMachineFunction(MachineFunction &F);
310
311    void doInitialPlacement(std::vector<MachineInstr*> &CPEMIs);
312    CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
313    unsigned getCPELogAlign(const MachineInstr *CPEMI);
314    void initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs);
315    unsigned getOffsetOf(MachineInstr *MI) const;
316    unsigned getUserOffset(CPUser&) const;
317    void dumpBBs();
318    void verify();
319
320    bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
321                         unsigned Disp, bool NegativeOK, bool IsSoImm = false);
322    bool isOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
323                         const CPUser &U);
324
325    bool isLongFormOffsetInRange(unsigned UserOffset, unsigned TrialOffset,
326                                const CPUser &U);
327
328    void computeBlockSize(MachineBasicBlock *MBB);
329    MachineBasicBlock *splitBlockBeforeInstr(MachineInstr *MI);
330    void updateForInsertedWaterBlock(MachineBasicBlock *NewBB);
331    void adjustBBOffsetsAfter(MachineBasicBlock *BB);
332    bool decrementCPEReferenceCount(unsigned CPI, MachineInstr* CPEMI);
333    int findInRangeCPEntry(CPUser& U, unsigned UserOffset);
334    int findLongFormInRangeCPEntry(CPUser& U, unsigned UserOffset);
335    bool findAvailableWater(CPUser&U, unsigned UserOffset,
336                            water_iterator &WaterIter);
337    void createNewWater(unsigned CPUserIndex, unsigned UserOffset,
338                        MachineBasicBlock *&NewMBB);
339    bool handleConstantPoolUser(unsigned CPUserIndex);
340    void removeDeadCPEMI(MachineInstr *CPEMI);
341    bool removeUnusedCPEntries();
342    bool isCPEntryInRange(MachineInstr *MI, unsigned UserOffset,
343                          MachineInstr *CPEMI, unsigned Disp, bool NegOk,
344                          bool DoDump = false);
345    bool isWaterInRange(unsigned UserOffset, MachineBasicBlock *Water,
346                        CPUser &U, unsigned &Growth);
347    bool isBBInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
348    bool fixupImmediateBr(ImmBranch &Br);
349    bool fixupConditionalBr(ImmBranch &Br);
350    bool fixupUnconditionalBr(ImmBranch &Br);
351
352    void prescanForConstants();
353
354  private:
355
356  };
357
358  char MipsConstantIslands::ID = 0;
359} // end of anonymous namespace
360
361
362bool MipsConstantIslands::isLongFormOffsetInRange
363  (unsigned UserOffset, unsigned TrialOffset,
364   const CPUser &U) {
365  return isOffsetInRange(UserOffset, TrialOffset,
366                         U.getLongFormMaxDisp(), U.NegOk, U.IsSoImm);
367}
368
369bool MipsConstantIslands::isOffsetInRange
370  (unsigned UserOffset, unsigned TrialOffset,
371   const CPUser &U) {
372  return isOffsetInRange(UserOffset, TrialOffset,
373                         U.getMaxDisp(), U.NegOk, U.IsSoImm);
374}
375/// print block size and offset information - debugging
376void MipsConstantIslands::dumpBBs() {
377  DEBUG({
378    for (unsigned J = 0, E = BBInfo.size(); J !=E; ++J) {
379      const BasicBlockInfo &BBI = BBInfo[J];
380      dbgs() << format("%08x BB#%u\t", BBI.Offset, J)
381             << " kb=" << unsigned(BBI.KnownBits)
382             << " ua=" << unsigned(BBI.Unalign)
383             << " pa=" << unsigned(BBI.PostAlign)
384             << format(" size=%#x\n", BBInfo[J].Size);
385    }
386  });
387}
388/// createMipsLongBranchPass - Returns a pass that converts branches to long
389/// branches.
390FunctionPass *llvm::createMipsConstantIslandPass(MipsTargetMachine &tm) {
391  return new MipsConstantIslands(tm);
392}
393
394bool MipsConstantIslands::runOnMachineFunction(MachineFunction &mf) {
395  // The intention is for this to be a mips16 only pass for now
396  // FIXME:
397  MF = &mf;
398  MCP = mf.getConstantPool();
399  DEBUG(dbgs() << "constant island machine function " << "\n");
400  if (!TM.getSubtarget<MipsSubtarget>().inMips16Mode() ||
401      !MipsSubtarget::useConstantIslands()) {
402    return false;
403  }
404  TII = (const MipsInstrInfo*)MF->getTarget().getInstrInfo();
405  MFI = MF->getInfo<MipsFunctionInfo>();
406  DEBUG(dbgs() << "constant island processing " << "\n");
407  //
408  // will need to make predermination if there is any constants we need to
409  // put in constant islands. TBD.
410  //
411  if (!PrescannedForConstants) prescanForConstants();
412
413  HasFarJump = false;
414  // This pass invalidates liveness information when it splits basic blocks.
415  MF->getRegInfo().invalidateLiveness();
416
417  // Renumber all of the machine basic blocks in the function, guaranteeing that
418  // the numbers agree with the position of the block in the function.
419  MF->RenumberBlocks();
420
421  bool MadeChange = false;
422
423  // Perform the initial placement of the constant pool entries.  To start with,
424  // we put them all at the end of the function.
425  std::vector<MachineInstr*> CPEMIs;
426  if (!MCP->isEmpty())
427    doInitialPlacement(CPEMIs);
428
429  /// The next UID to take is the first unused one.
430  initPICLabelUId(CPEMIs.size());
431
432  // Do the initial scan of the function, building up information about the
433  // sizes of each block, the location of all the water, and finding all of the
434  // constant pool users.
435  initializeFunctionInfo(CPEMIs);
436  CPEMIs.clear();
437  DEBUG(dumpBBs());
438
439  /// Remove dead constant pool entries.
440  MadeChange |= removeUnusedCPEntries();
441
442  // Iteratively place constant pool entries and fix up branches until there
443  // is no change.
444  unsigned NoCPIters = 0, NoBRIters = 0;
445  (void)NoBRIters;
446  while (true) {
447    DEBUG(dbgs() << "Beginning CP iteration #" << NoCPIters << '\n');
448    bool CPChange = false;
449    for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
450      CPChange |= handleConstantPoolUser(i);
451    if (CPChange && ++NoCPIters > 30)
452      report_fatal_error("Constant Island pass failed to converge!");
453    DEBUG(dumpBBs());
454
455    // Clear NewWaterList now.  If we split a block for branches, it should
456    // appear as "new water" for the next iteration of constant pool placement.
457    NewWaterList.clear();
458
459    DEBUG(dbgs() << "Beginning BR iteration #" << NoBRIters << '\n');
460    bool BRChange = false;
461#ifdef IN_PROGRESS
462    for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
463      BRChange |= fixupImmediateBr(ImmBranches[i]);
464    if (BRChange && ++NoBRIters > 30)
465      report_fatal_error("Branch Fix Up pass failed to converge!");
466    DEBUG(dumpBBs());
467#endif
468    if (!CPChange && !BRChange)
469      break;
470    MadeChange = true;
471  }
472
473  DEBUG(dbgs() << '\n'; dumpBBs());
474
475  BBInfo.clear();
476  WaterList.clear();
477  CPUsers.clear();
478  CPEntries.clear();
479  ImmBranches.clear();
480  return MadeChange;
481}
482
483/// doInitialPlacement - Perform the initial placement of the constant pool
484/// entries.  To start with, we put them all at the end of the function.
485void
486MipsConstantIslands::doInitialPlacement(std::vector<MachineInstr*> &CPEMIs) {
487  // Create the basic block to hold the CPE's.
488  MachineBasicBlock *BB = MF->CreateMachineBasicBlock();
489  MF->push_back(BB);
490
491
492  // MachineConstantPool measures alignment in bytes. We measure in log2(bytes).
493  unsigned MaxAlign = Log2_32(MCP->getConstantPoolAlignment());
494
495  // Mark the basic block as required by the const-pool.
496  // If AlignConstantIslands isn't set, use 4-byte alignment for everything.
497  BB->setAlignment(AlignConstantIslands ? MaxAlign : 2);
498
499  // The function needs to be as aligned as the basic blocks. The linker may
500  // move functions around based on their alignment.
501  MF->ensureAlignment(BB->getAlignment());
502
503  // Order the entries in BB by descending alignment.  That ensures correct
504  // alignment of all entries as long as BB is sufficiently aligned.  Keep
505  // track of the insertion point for each alignment.  We are going to bucket
506  // sort the entries as they are created.
507  SmallVector<MachineBasicBlock::iterator, 8> InsPoint(MaxAlign + 1, BB->end());
508
509  // Add all of the constants from the constant pool to the end block, use an
510  // identity mapping of CPI's to CPE's.
511  const std::vector<MachineConstantPoolEntry> &CPs = MCP->getConstants();
512
513  const DataLayout &TD = *MF->getTarget().getDataLayout();
514  for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
515    unsigned Size = TD.getTypeAllocSize(CPs[i].getType());
516    assert(Size >= 4 && "Too small constant pool entry");
517    unsigned Align = CPs[i].getAlignment();
518    assert(isPowerOf2_32(Align) && "Invalid alignment");
519    // Verify that all constant pool entries are a multiple of their alignment.
520    // If not, we would have to pad them out so that instructions stay aligned.
521    assert((Size % Align) == 0 && "CP Entry not multiple of 4 bytes!");
522
523    // Insert CONSTPOOL_ENTRY before entries with a smaller alignment.
524    unsigned LogAlign = Log2_32(Align);
525    MachineBasicBlock::iterator InsAt = InsPoint[LogAlign];
526
527    MachineInstr *CPEMI =
528      BuildMI(*BB, InsAt, DebugLoc(), TII->get(Mips::CONSTPOOL_ENTRY))
529        .addImm(i).addConstantPoolIndex(i).addImm(Size);
530
531    CPEMIs.push_back(CPEMI);
532
533    // Ensure that future entries with higher alignment get inserted before
534    // CPEMI. This is bucket sort with iterators.
535    for (unsigned a = LogAlign + 1; a <= MaxAlign; ++a)
536      if (InsPoint[a] == InsAt)
537        InsPoint[a] = CPEMI;
538    // Add a new CPEntry, but no corresponding CPUser yet.
539    std::vector<CPEntry> CPEs;
540    CPEs.push_back(CPEntry(CPEMI, i));
541    CPEntries.push_back(CPEs);
542    ++NumCPEs;
543    DEBUG(dbgs() << "Moved CPI#" << i << " to end of function, size = "
544                 << Size << ", align = " << Align <<'\n');
545  }
546  DEBUG(BB->dump());
547}
548
549/// BBHasFallthrough - Return true if the specified basic block can fallthrough
550/// into the block immediately after it.
551static bool BBHasFallthrough(MachineBasicBlock *MBB) {
552  // Get the next machine basic block in the function.
553  MachineFunction::iterator MBBI = MBB;
554  // Can't fall off end of function.
555  if (llvm::next(MBBI) == MBB->getParent()->end())
556    return false;
557
558  MachineBasicBlock *NextBB = llvm::next(MBBI);
559  for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
560       E = MBB->succ_end(); I != E; ++I)
561    if (*I == NextBB)
562      return true;
563
564  return false;
565}
566
567/// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
568/// look up the corresponding CPEntry.
569MipsConstantIslands::CPEntry
570*MipsConstantIslands::findConstPoolEntry(unsigned CPI,
571                                        const MachineInstr *CPEMI) {
572  std::vector<CPEntry> &CPEs = CPEntries[CPI];
573  // Number of entries per constpool index should be small, just do a
574  // linear search.
575  for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
576    if (CPEs[i].CPEMI == CPEMI)
577      return &CPEs[i];
578  }
579  return NULL;
580}
581
582/// getCPELogAlign - Returns the required alignment of the constant pool entry
583/// represented by CPEMI.  Alignment is measured in log2(bytes) units.
584unsigned MipsConstantIslands::getCPELogAlign(const MachineInstr *CPEMI) {
585  assert(CPEMI && CPEMI->getOpcode() == Mips::CONSTPOOL_ENTRY);
586
587  // Everything is 4-byte aligned unless AlignConstantIslands is set.
588  if (!AlignConstantIslands)
589    return 2;
590
591  unsigned CPI = CPEMI->getOperand(1).getIndex();
592  assert(CPI < MCP->getConstants().size() && "Invalid constant pool index.");
593  unsigned Align = MCP->getConstants()[CPI].getAlignment();
594  assert(isPowerOf2_32(Align) && "Invalid CPE alignment");
595  return Log2_32(Align);
596}
597
598/// initializeFunctionInfo - Do the initial scan of the function, building up
599/// information about the sizes of each block, the location of all the water,
600/// and finding all of the constant pool users.
601void MipsConstantIslands::
602initializeFunctionInfo(const std::vector<MachineInstr*> &CPEMIs) {
603  BBInfo.clear();
604  BBInfo.resize(MF->getNumBlockIDs());
605
606  // First thing, compute the size of all basic blocks, and see if the function
607  // has any inline assembly in it. If so, we have to be conservative about
608  // alignment assumptions, as we don't know for sure the size of any
609  // instructions in the inline assembly.
610  for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E; ++I)
611    computeBlockSize(I);
612
613  // The known bits of the entry block offset are determined by the function
614  // alignment.
615  BBInfo.front().KnownBits = MF->getAlignment();
616
617  // Compute block offsets.
618  adjustBBOffsetsAfter(MF->begin());
619
620  // Now go back through the instructions and build up our data structures.
621  for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end();
622       MBBI != E; ++MBBI) {
623    MachineBasicBlock &MBB = *MBBI;
624
625    // If this block doesn't fall through into the next MBB, then this is
626    // 'water' that a constant pool island could be placed.
627    if (!BBHasFallthrough(&MBB))
628      WaterList.push_back(&MBB);
629    for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
630         I != E; ++I) {
631      if (I->isDebugValue())
632        continue;
633
634      int Opc = I->getOpcode();
635      if (I->isBranch()) {
636        bool isCond = false;
637        unsigned Bits = 0;
638        unsigned Scale = 1;
639        int UOpc = Opc;
640
641        switch (Opc) {
642        default:
643          continue;  // Ignore other JT branches
644        }
645        // Record this immediate branch.
646        unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
647        ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc));
648
649      }
650
651
652      if (Opc == Mips::CONSTPOOL_ENTRY)
653        continue;
654
655
656      // Scan the instructions for constant pool operands.
657      for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
658        if (I->getOperand(op).isCPI()) {
659
660          // We found one.  The addressing mode tells us the max displacement
661          // from the PC that this instruction permits.
662
663          // Basic size info comes from the TSFlags field.
664          unsigned Bits = 0;
665          unsigned Scale = 1;
666          bool NegOk = false;
667          bool IsSoImm = false;
668          unsigned LongFormBits = 0;
669          unsigned LongFormScale = 0;
670          unsigned LongFormOpcode = 0;
671          switch (Opc) {
672          default:
673            llvm_unreachable("Unknown addressing mode for CP reference!");
674          case Mips::LwRxPcTcp16:
675            Bits = 8;
676            Scale = 2;
677            LongFormOpcode = Mips::LwRxPcTcpX16;
678            break;
679          case Mips::LwRxPcTcpX16:
680            Bits = 16;
681            Scale = 2;
682            break;
683          }
684          // Remember that this is a user of a CP entry.
685          unsigned CPI = I->getOperand(op).getIndex();
686          MachineInstr *CPEMI = CPEMIs[CPI];
687          unsigned MaxOffs = ((1 << Bits)-1) * Scale;
688          unsigned LongFormMaxOffs = ((1 << LongFormBits)-1) * LongFormScale;
689          CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk,
690                                   IsSoImm, LongFormMaxOffs,
691                                   LongFormOpcode));
692
693          // Increment corresponding CPEntry reference count.
694          CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
695          assert(CPE && "Cannot find a corresponding CPEntry!");
696          CPE->RefCount++;
697
698          // Instructions can only use one CP entry, don't bother scanning the
699          // rest of the operands.
700          break;
701
702        }
703
704    }
705  }
706
707}
708
709/// computeBlockSize - Compute the size and some alignment information for MBB.
710/// This function updates BBInfo directly.
711void MipsConstantIslands::computeBlockSize(MachineBasicBlock *MBB) {
712  BasicBlockInfo &BBI = BBInfo[MBB->getNumber()];
713  BBI.Size = 0;
714  BBI.Unalign = 0;
715  BBI.PostAlign = 0;
716
717  for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
718       ++I)
719    BBI.Size += TII->GetInstSizeInBytes(I);
720
721}
722
723/// getOffsetOf - Return the current offset of the specified machine instruction
724/// from the start of the function.  This offset changes as stuff is moved
725/// around inside the function.
726unsigned MipsConstantIslands::getOffsetOf(MachineInstr *MI) const {
727  MachineBasicBlock *MBB = MI->getParent();
728
729  // The offset is composed of two things: the sum of the sizes of all MBB's
730  // before this instruction's block, and the offset from the start of the block
731  // it is in.
732  unsigned Offset = BBInfo[MBB->getNumber()].Offset;
733
734  // Sum instructions before MI in MBB.
735  for (MachineBasicBlock::iterator I = MBB->begin(); &*I != MI; ++I) {
736    assert(I != MBB->end() && "Didn't find MI in its own basic block?");
737    Offset += TII->GetInstSizeInBytes(I);
738  }
739  return Offset;
740}
741
742/// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
743/// ID.
744static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
745                              const MachineBasicBlock *RHS) {
746  return LHS->getNumber() < RHS->getNumber();
747}
748
749/// updateForInsertedWaterBlock - When a block is newly inserted into the
750/// machine function, it upsets all of the block numbers.  Renumber the blocks
751/// and update the arrays that parallel this numbering.
752void MipsConstantIslands::updateForInsertedWaterBlock
753  (MachineBasicBlock *NewBB) {
754  // Renumber the MBB's to keep them consecutive.
755  NewBB->getParent()->RenumberBlocks(NewBB);
756
757  // Insert an entry into BBInfo to align it properly with the (newly
758  // renumbered) block numbers.
759  BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
760
761  // Next, update WaterList.  Specifically, we need to add NewMBB as having
762  // available water after it.
763  water_iterator IP =
764    std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
765                     CompareMBBNumbers);
766  WaterList.insert(IP, NewBB);
767}
768
769/// getUserOffset - Compute the offset of U.MI as seen by the hardware
770/// displacement computation.  Update U.KnownAlignment to match its current
771/// basic block location.
772unsigned MipsConstantIslands::getUserOffset(CPUser &U) const {
773  unsigned UserOffset = getOffsetOf(U.MI);
774  const BasicBlockInfo &BBI = BBInfo[U.MI->getParent()->getNumber()];
775  unsigned KnownBits = BBI.internalKnownBits();
776
777  // The value read from PC is offset from the actual instruction address.
778
779
780  // Because of inline assembly, we may not know the alignment (mod 4) of U.MI.
781  // Make sure U.getMaxDisp() returns a constrained range.
782  U.KnownAlignment = (KnownBits >= 2);
783
784
785  return UserOffset;
786}
787
788/// Split the basic block containing MI into two blocks, which are joined by
789/// an unconditional branch.  Update data structures and renumber blocks to
790/// account for this change and returns the newly created block.
791MachineBasicBlock *MipsConstantIslands::splitBlockBeforeInstr
792  (MachineInstr *MI) {
793  MachineBasicBlock *OrigBB = MI->getParent();
794
795  // Create a new MBB for the code after the OrigBB.
796  MachineBasicBlock *NewBB =
797    MF->CreateMachineBasicBlock(OrigBB->getBasicBlock());
798  MachineFunction::iterator MBBI = OrigBB; ++MBBI;
799  MF->insert(MBBI, NewBB);
800
801  // Splice the instructions starting with MI over to NewBB.
802  NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
803
804  // Add an unconditional branch from OrigBB to NewBB.
805  // Note the new unconditional branch is not being recorded.
806  // There doesn't seem to be meaningful DebugInfo available; this doesn't
807  // correspond to anything in the source.
808  BuildMI(OrigBB, DebugLoc(), TII->get(Mips::BimmX16)).addMBB(NewBB);
809  ++NumSplit;
810
811  // Update the CFG.  All succs of OrigBB are now succs of NewBB.
812  NewBB->transferSuccessors(OrigBB);
813
814  // OrigBB branches to NewBB.
815  OrigBB->addSuccessor(NewBB);
816
817  // Update internal data structures to account for the newly inserted MBB.
818  // This is almost the same as updateForInsertedWaterBlock, except that
819  // the Water goes after OrigBB, not NewBB.
820  MF->RenumberBlocks(NewBB);
821
822  // Insert an entry into BBInfo to align it properly with the (newly
823  // renumbered) block numbers.
824  BBInfo.insert(BBInfo.begin() + NewBB->getNumber(), BasicBlockInfo());
825
826  // Next, update WaterList.  Specifically, we need to add OrigMBB as having
827  // available water after it (but not if it's already there, which happens
828  // when splitting before a conditional branch that is followed by an
829  // unconditional branch - in that case we want to insert NewBB).
830  water_iterator IP =
831    std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB,
832                     CompareMBBNumbers);
833  MachineBasicBlock* WaterBB = *IP;
834  if (WaterBB == OrigBB)
835    WaterList.insert(llvm::next(IP), NewBB);
836  else
837    WaterList.insert(IP, OrigBB);
838  NewWaterList.insert(OrigBB);
839
840  // Figure out how large the OrigBB is.  As the first half of the original
841  // block, it cannot contain a tablejump.  The size includes
842  // the new jump we added.  (It should be possible to do this without
843  // recounting everything, but it's very confusing, and this is rarely
844  // executed.)
845  computeBlockSize(OrigBB);
846
847  // Figure out how large the NewMBB is.  As the second half of the original
848  // block, it may contain a tablejump.
849  computeBlockSize(NewBB);
850
851  // All BBOffsets following these blocks must be modified.
852  adjustBBOffsetsAfter(OrigBB);
853
854  return NewBB;
855}
856
857
858
859/// isOffsetInRange - Checks whether UserOffset (the location of a constant pool
860/// reference) is within MaxDisp of TrialOffset (a proposed location of a
861/// constant pool entry).
862/// UserOffset is computed by getUserOffset above to include PC adjustments. If
863/// the mod 4 alignment of UserOffset is not known, the uncertainty must be
864/// subtracted from MaxDisp instead. CPUser::getMaxDisp() does that.
865bool MipsConstantIslands::isOffsetInRange(unsigned UserOffset,
866                                         unsigned TrialOffset, unsigned MaxDisp,
867                                         bool NegativeOK, bool IsSoImm) {
868  if (UserOffset <= TrialOffset) {
869    // User before the Trial.
870    if (TrialOffset - UserOffset <= MaxDisp)
871      return true;
872    // FIXME: Make use full range of soimm values.
873  } else if (NegativeOK) {
874    if (UserOffset - TrialOffset <= MaxDisp)
875      return true;
876    // FIXME: Make use full range of soimm values.
877  }
878  return false;
879}
880
881/// isWaterInRange - Returns true if a CPE placed after the specified
882/// Water (a basic block) will be in range for the specific MI.
883///
884/// Compute how much the function will grow by inserting a CPE after Water.
885bool MipsConstantIslands::isWaterInRange(unsigned UserOffset,
886                                        MachineBasicBlock* Water, CPUser &U,
887                                        unsigned &Growth) {
888  unsigned CPELogAlign = getCPELogAlign(U.CPEMI);
889  unsigned CPEOffset = BBInfo[Water->getNumber()].postOffset(CPELogAlign);
890  unsigned NextBlockOffset, NextBlockAlignment;
891  MachineFunction::const_iterator NextBlock = Water;
892  if (++NextBlock == MF->end()) {
893    NextBlockOffset = BBInfo[Water->getNumber()].postOffset();
894    NextBlockAlignment = 0;
895  } else {
896    NextBlockOffset = BBInfo[NextBlock->getNumber()].Offset;
897    NextBlockAlignment = NextBlock->getAlignment();
898  }
899  unsigned Size = U.CPEMI->getOperand(2).getImm();
900  unsigned CPEEnd = CPEOffset + Size;
901
902  // The CPE may be able to hide in the alignment padding before the next
903  // block. It may also cause more padding to be required if it is more aligned
904  // that the next block.
905  if (CPEEnd > NextBlockOffset) {
906    Growth = CPEEnd - NextBlockOffset;
907    // Compute the padding that would go at the end of the CPE to align the next
908    // block.
909    Growth += OffsetToAlignment(CPEEnd, 1u << NextBlockAlignment);
910
911    // If the CPE is to be inserted before the instruction, that will raise
912    // the offset of the instruction. Also account for unknown alignment padding
913    // in blocks between CPE and the user.
914    if (CPEOffset < UserOffset)
915      UserOffset += Growth + UnknownPadding(MF->getAlignment(), CPELogAlign);
916  } else
917    // CPE fits in existing padding.
918    Growth = 0;
919
920  return isOffsetInRange(UserOffset, CPEOffset, U);
921}
922
923/// isCPEntryInRange - Returns true if the distance between specific MI and
924/// specific ConstPool entry instruction can fit in MI's displacement field.
925bool MipsConstantIslands::isCPEntryInRange
926  (MachineInstr *MI, unsigned UserOffset,
927   MachineInstr *CPEMI, unsigned MaxDisp,
928   bool NegOk, bool DoDump) {
929  unsigned CPEOffset  = getOffsetOf(CPEMI);
930
931  if (DoDump) {
932    DEBUG({
933      unsigned Block = MI->getParent()->getNumber();
934      const BasicBlockInfo &BBI = BBInfo[Block];
935      dbgs() << "User of CPE#" << CPEMI->getOperand(0).getImm()
936             << " max delta=" << MaxDisp
937             << format(" insn address=%#x", UserOffset)
938             << " in BB#" << Block << ": "
939             << format("%#x-%x\t", BBI.Offset, BBI.postOffset()) << *MI
940             << format("CPE address=%#x offset=%+d: ", CPEOffset,
941                       int(CPEOffset-UserOffset));
942    });
943  }
944
945  return isOffsetInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
946}
947
948#ifndef NDEBUG
949/// BBIsJumpedOver - Return true of the specified basic block's only predecessor
950/// unconditionally branches to its only successor.
951static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
952  if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
953    return false;
954  MachineBasicBlock *Succ = *MBB->succ_begin();
955  MachineBasicBlock *Pred = *MBB->pred_begin();
956  MachineInstr *PredMI = &Pred->back();
957  if (PredMI->getOpcode() == Mips::BimmX16)
958    return PredMI->getOperand(0).getMBB() == Succ;
959  return false;
960}
961#endif
962
963void MipsConstantIslands::adjustBBOffsetsAfter(MachineBasicBlock *BB) {
964  unsigned BBNum = BB->getNumber();
965  for(unsigned i = BBNum + 1, e = MF->getNumBlockIDs(); i < e; ++i) {
966    // Get the offset and known bits at the end of the layout predecessor.
967    // Include the alignment of the current block.
968    unsigned Offset = BBInfo[i - 1].postOffset();
969    BBInfo[i].Offset = Offset;
970  }
971}
972
973/// decrementCPEReferenceCount - find the constant pool entry with index CPI
974/// and instruction CPEMI, and decrement its refcount.  If the refcount
975/// becomes 0 remove the entry and instruction.  Returns true if we removed
976/// the entry, false if we didn't.
977
978bool MipsConstantIslands::decrementCPEReferenceCount(unsigned CPI,
979                                                    MachineInstr *CPEMI) {
980  // Find the old entry. Eliminate it if it is no longer used.
981  CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
982  assert(CPE && "Unexpected!");
983  if (--CPE->RefCount == 0) {
984    removeDeadCPEMI(CPEMI);
985    CPE->CPEMI = NULL;
986    --NumCPEs;
987    return true;
988  }
989  return false;
990}
991
992/// LookForCPEntryInRange - see if the currently referenced CPE is in range;
993/// if not, see if an in-range clone of the CPE is in range, and if so,
994/// change the data structures so the user references the clone.  Returns:
995/// 0 = no existing entry found
996/// 1 = entry found, and there were no code insertions or deletions
997/// 2 = entry found, and there were code insertions or deletions
998int MipsConstantIslands::findInRangeCPEntry(CPUser& U, unsigned UserOffset)
999{
1000  MachineInstr *UserMI = U.MI;
1001  MachineInstr *CPEMI  = U.CPEMI;
1002
1003  // Check to see if the CPE is already in-range.
1004  if (isCPEntryInRange(UserMI, UserOffset, CPEMI, U.getMaxDisp(), U.NegOk,
1005                       true)) {
1006    DEBUG(dbgs() << "In range\n");
1007    return 1;
1008  }
1009
1010  // No.  Look for previously created clones of the CPE that are in range.
1011  unsigned CPI = CPEMI->getOperand(1).getIndex();
1012  std::vector<CPEntry> &CPEs = CPEntries[CPI];
1013  for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
1014    // We already tried this one
1015    if (CPEs[i].CPEMI == CPEMI)
1016      continue;
1017    // Removing CPEs can leave empty entries, skip
1018    if (CPEs[i].CPEMI == NULL)
1019      continue;
1020    if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.getMaxDisp(),
1021                     U.NegOk)) {
1022      DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#"
1023                   << CPEs[i].CPI << "\n");
1024      // Point the CPUser node to the replacement
1025      U.CPEMI = CPEs[i].CPEMI;
1026      // Change the CPI in the instruction operand to refer to the clone.
1027      for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
1028        if (UserMI->getOperand(j).isCPI()) {
1029          UserMI->getOperand(j).setIndex(CPEs[i].CPI);
1030          break;
1031        }
1032      // Adjust the refcount of the clone...
1033      CPEs[i].RefCount++;
1034      // ...and the original.  If we didn't remove the old entry, none of the
1035      // addresses changed, so we don't need another pass.
1036      return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
1037    }
1038  }
1039  return 0;
1040}
1041
1042/// LookForCPEntryInRange - see if the currently referenced CPE is in range;
1043/// This version checks if the longer form of the instruction can be used to
1044/// to satisfy things.
1045/// if not, see if an in-range clone of the CPE is in range, and if so,
1046/// change the data structures so the user references the clone.  Returns:
1047/// 0 = no existing entry found
1048/// 1 = entry found, and there were no code insertions or deletions
1049/// 2 = entry found, and there were code insertions or deletions
1050int MipsConstantIslands::findLongFormInRangeCPEntry
1051  (CPUser& U, unsigned UserOffset)
1052{
1053  MachineInstr *UserMI = U.MI;
1054  MachineInstr *CPEMI  = U.CPEMI;
1055
1056  // Check to see if the CPE is already in-range.
1057  if (isCPEntryInRange(UserMI, UserOffset, CPEMI,
1058                       U.getLongFormMaxDisp(), U.NegOk,
1059                       true)) {
1060    DEBUG(dbgs() << "In range\n");
1061    UserMI->setDesc(TII->get(U.getLongFormOpcode()));
1062    return 2;  // instruction is longer length now
1063  }
1064
1065  // No.  Look for previously created clones of the CPE that are in range.
1066  unsigned CPI = CPEMI->getOperand(1).getIndex();
1067  std::vector<CPEntry> &CPEs = CPEntries[CPI];
1068  for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
1069    // We already tried this one
1070    if (CPEs[i].CPEMI == CPEMI)
1071      continue;
1072    // Removing CPEs can leave empty entries, skip
1073    if (CPEs[i].CPEMI == NULL)
1074      continue;
1075    if (isCPEntryInRange(UserMI, UserOffset, CPEs[i].CPEMI,
1076                         U.getLongFormMaxDisp(), U.NegOk)) {
1077      DEBUG(dbgs() << "Replacing CPE#" << CPI << " with CPE#"
1078                   << CPEs[i].CPI << "\n");
1079      // Point the CPUser node to the replacement
1080      U.CPEMI = CPEs[i].CPEMI;
1081      // Change the CPI in the instruction operand to refer to the clone.
1082      for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
1083        if (UserMI->getOperand(j).isCPI()) {
1084          UserMI->getOperand(j).setIndex(CPEs[i].CPI);
1085          break;
1086        }
1087      // Adjust the refcount of the clone...
1088      CPEs[i].RefCount++;
1089      // ...and the original.  If we didn't remove the old entry, none of the
1090      // addresses changed, so we don't need another pass.
1091      return decrementCPEReferenceCount(CPI, CPEMI) ? 2 : 1;
1092    }
1093  }
1094  return 0;
1095}
1096
1097/// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
1098/// the specific unconditional branch instruction.
1099static inline unsigned getUnconditionalBrDisp(int Opc) {
1100  switch (Opc) {
1101  case Mips::BimmX16:
1102    return ((1<<16)-1)*2;
1103  default:
1104    break;
1105  }
1106  return ((1<<16)-1)*2;
1107}
1108
1109/// findAvailableWater - Look for an existing entry in the WaterList in which
1110/// we can place the CPE referenced from U so it's within range of U's MI.
1111/// Returns true if found, false if not.  If it returns true, WaterIter
1112/// is set to the WaterList entry.
1113/// To ensure that this pass
1114/// terminates, the CPE location for a particular CPUser is only allowed to
1115/// move to a lower address, so search backward from the end of the list and
1116/// prefer the first water that is in range.
1117bool MipsConstantIslands::findAvailableWater(CPUser &U, unsigned UserOffset,
1118                                      water_iterator &WaterIter) {
1119  if (WaterList.empty())
1120    return false;
1121
1122  unsigned BestGrowth = ~0u;
1123  for (water_iterator IP = prior(WaterList.end()), B = WaterList.begin();;
1124       --IP) {
1125    MachineBasicBlock* WaterBB = *IP;
1126    // Check if water is in range and is either at a lower address than the
1127    // current "high water mark" or a new water block that was created since
1128    // the previous iteration by inserting an unconditional branch.  In the
1129    // latter case, we want to allow resetting the high water mark back to
1130    // this new water since we haven't seen it before.  Inserting branches
1131    // should be relatively uncommon and when it does happen, we want to be
1132    // sure to take advantage of it for all the CPEs near that block, so that
1133    // we don't insert more branches than necessary.
1134    unsigned Growth;
1135    if (isWaterInRange(UserOffset, WaterBB, U, Growth) &&
1136        (WaterBB->getNumber() < U.HighWaterMark->getNumber() ||
1137         NewWaterList.count(WaterBB)) && Growth < BestGrowth) {
1138      // This is the least amount of required padding seen so far.
1139      BestGrowth = Growth;
1140      WaterIter = IP;
1141      DEBUG(dbgs() << "Found water after BB#" << WaterBB->getNumber()
1142                   << " Growth=" << Growth << '\n');
1143
1144      // Keep looking unless it is perfect.
1145      if (BestGrowth == 0)
1146        return true;
1147    }
1148    if (IP == B)
1149      break;
1150  }
1151  return BestGrowth != ~0u;
1152}
1153
1154/// createNewWater - No existing WaterList entry will work for
1155/// CPUsers[CPUserIndex], so create a place to put the CPE.  The end of the
1156/// block is used if in range, and the conditional branch munged so control
1157/// flow is correct.  Otherwise the block is split to create a hole with an
1158/// unconditional branch around it.  In either case NewMBB is set to a
1159/// block following which the new island can be inserted (the WaterList
1160/// is not adjusted).
1161void MipsConstantIslands::createNewWater(unsigned CPUserIndex,
1162                                        unsigned UserOffset,
1163                                        MachineBasicBlock *&NewMBB) {
1164  CPUser &U = CPUsers[CPUserIndex];
1165  MachineInstr *UserMI = U.MI;
1166  MachineInstr *CPEMI  = U.CPEMI;
1167  unsigned CPELogAlign = getCPELogAlign(CPEMI);
1168  MachineBasicBlock *UserMBB = UserMI->getParent();
1169  const BasicBlockInfo &UserBBI = BBInfo[UserMBB->getNumber()];
1170
1171  // If the block does not end in an unconditional branch already, and if the
1172  // end of the block is within range, make new water there.
1173  if (BBHasFallthrough(UserMBB)) {
1174    // Size of branch to insert.
1175    unsigned Delta = 2;
1176    // Compute the offset where the CPE will begin.
1177    unsigned CPEOffset = UserBBI.postOffset(CPELogAlign) + Delta;
1178
1179    if (isOffsetInRange(UserOffset, CPEOffset, U)) {
1180      DEBUG(dbgs() << "Split at end of BB#" << UserMBB->getNumber()
1181            << format(", expected CPE offset %#x\n", CPEOffset));
1182      NewMBB = llvm::next(MachineFunction::iterator(UserMBB));
1183      // Add an unconditional branch from UserMBB to fallthrough block.  Record
1184      // it for branch lengthening; this new branch will not get out of range,
1185      // but if the preceding conditional branch is out of range, the targets
1186      // will be exchanged, and the altered branch may be out of range, so the
1187      // machinery has to know about it.
1188      int UncondBr = Mips::BimmX16;
1189      BuildMI(UserMBB, DebugLoc(), TII->get(UncondBr)).addMBB(NewMBB);
1190      unsigned MaxDisp = getUnconditionalBrDisp(UncondBr);
1191      ImmBranches.push_back(ImmBranch(&UserMBB->back(),
1192                                      MaxDisp, false, UncondBr));
1193      BBInfo[UserMBB->getNumber()].Size += Delta;
1194      adjustBBOffsetsAfter(UserMBB);
1195      return;
1196    }
1197  }
1198
1199  // What a big block.  Find a place within the block to split it.
1200
1201  // Try to split the block so it's fully aligned.  Compute the latest split
1202  // point where we can add a 4-byte branch instruction, and then align to
1203  // LogAlign which is the largest possible alignment in the function.
1204  unsigned LogAlign = MF->getAlignment();
1205  assert(LogAlign >= CPELogAlign && "Over-aligned constant pool entry");
1206  unsigned KnownBits = UserBBI.internalKnownBits();
1207  unsigned UPad = UnknownPadding(LogAlign, KnownBits);
1208  unsigned BaseInsertOffset = UserOffset + U.getMaxDisp() - UPad;
1209  DEBUG(dbgs() << format("Split in middle of big block before %#x",
1210                         BaseInsertOffset));
1211
1212  // The 4 in the following is for the unconditional branch we'll be inserting
1213  // Alignment of the island is handled
1214  // inside isOffsetInRange.
1215  BaseInsertOffset -= 4;
1216
1217  DEBUG(dbgs() << format(", adjusted to %#x", BaseInsertOffset)
1218               << " la=" << LogAlign
1219               << " kb=" << KnownBits
1220               << " up=" << UPad << '\n');
1221
1222  // This could point off the end of the block if we've already got constant
1223  // pool entries following this block; only the last one is in the water list.
1224  // Back past any possible branches (allow for a conditional and a maximally
1225  // long unconditional).
1226  if (BaseInsertOffset + 8 >= UserBBI.postOffset()) {
1227    BaseInsertOffset = UserBBI.postOffset() - UPad - 8;
1228    DEBUG(dbgs() << format("Move inside block: %#x\n", BaseInsertOffset));
1229  }
1230  unsigned EndInsertOffset = BaseInsertOffset + 4 + UPad +
1231    CPEMI->getOperand(2).getImm();
1232  MachineBasicBlock::iterator MI = UserMI;
1233  ++MI;
1234  unsigned CPUIndex = CPUserIndex+1;
1235  unsigned NumCPUsers = CPUsers.size();
1236  //MachineInstr *LastIT = 0;
1237  for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI);
1238       Offset < BaseInsertOffset;
1239       Offset += TII->GetInstSizeInBytes(MI),
1240       MI = llvm::next(MI)) {
1241    assert(MI != UserMBB->end() && "Fell off end of block");
1242    if (CPUIndex < NumCPUsers && CPUsers[CPUIndex].MI == MI) {
1243      CPUser &U = CPUsers[CPUIndex];
1244      if (!isOffsetInRange(Offset, EndInsertOffset, U)) {
1245        // Shift intertion point by one unit of alignment so it is within reach.
1246        BaseInsertOffset -= 1u << LogAlign;
1247        EndInsertOffset  -= 1u << LogAlign;
1248      }
1249      // This is overly conservative, as we don't account for CPEMIs being
1250      // reused within the block, but it doesn't matter much.  Also assume CPEs
1251      // are added in order with alignment padding.  We may eventually be able
1252      // to pack the aligned CPEs better.
1253      EndInsertOffset += U.CPEMI->getOperand(2).getImm();
1254      CPUIndex++;
1255    }
1256  }
1257
1258  --MI;
1259  NewMBB = splitBlockBeforeInstr(MI);
1260}
1261
1262/// handleConstantPoolUser - Analyze the specified user, checking to see if it
1263/// is out-of-range.  If so, pick up the constant pool value and move it some
1264/// place in-range.  Return true if we changed any addresses (thus must run
1265/// another pass of branch lengthening), false otherwise.
1266bool MipsConstantIslands::handleConstantPoolUser(unsigned CPUserIndex) {
1267  CPUser &U = CPUsers[CPUserIndex];
1268  MachineInstr *UserMI = U.MI;
1269  MachineInstr *CPEMI  = U.CPEMI;
1270  unsigned CPI = CPEMI->getOperand(1).getIndex();
1271  unsigned Size = CPEMI->getOperand(2).getImm();
1272  // Compute this only once, it's expensive.
1273  unsigned UserOffset = getUserOffset(U);
1274
1275  // See if the current entry is within range, or there is a clone of it
1276  // in range.
1277  int result = findInRangeCPEntry(U, UserOffset);
1278  if (result==1) return false;
1279  else if (result==2) return true;
1280
1281
1282  // Look for water where we can place this CPE.
1283  MachineBasicBlock *NewIsland = MF->CreateMachineBasicBlock();
1284  MachineBasicBlock *NewMBB;
1285  water_iterator IP;
1286  if (findAvailableWater(U, UserOffset, IP)) {
1287    DEBUG(dbgs() << "Found water in range\n");
1288    MachineBasicBlock *WaterBB = *IP;
1289
1290    // If the original WaterList entry was "new water" on this iteration,
1291    // propagate that to the new island.  This is just keeping NewWaterList
1292    // updated to match the WaterList, which will be updated below.
1293    if (NewWaterList.erase(WaterBB))
1294      NewWaterList.insert(NewIsland);
1295
1296    // The new CPE goes before the following block (NewMBB).
1297    NewMBB = llvm::next(MachineFunction::iterator(WaterBB));
1298
1299  } else {
1300    // No water found.
1301    // we first see if a longer form of the instrucion could have reached
1302    // the constant. in that case we won't bother to split
1303#ifdef IN_PROGRESS
1304    result = findLongFormInRangeCPEntry(U, UserOffset);
1305#endif
1306    DEBUG(dbgs() << "No water found\n");
1307    createNewWater(CPUserIndex, UserOffset, NewMBB);
1308
1309    // splitBlockBeforeInstr adds to WaterList, which is important when it is
1310    // called while handling branches so that the water will be seen on the
1311    // next iteration for constant pools, but in this context, we don't want
1312    // it.  Check for this so it will be removed from the WaterList.
1313    // Also remove any entry from NewWaterList.
1314    MachineBasicBlock *WaterBB = prior(MachineFunction::iterator(NewMBB));
1315    IP = std::find(WaterList.begin(), WaterList.end(), WaterBB);
1316    if (IP != WaterList.end())
1317      NewWaterList.erase(WaterBB);
1318
1319    // We are adding new water.  Update NewWaterList.
1320    NewWaterList.insert(NewIsland);
1321  }
1322
1323  // Remove the original WaterList entry; we want subsequent insertions in
1324  // this vicinity to go after the one we're about to insert.  This
1325  // considerably reduces the number of times we have to move the same CPE
1326  // more than once and is also important to ensure the algorithm terminates.
1327  if (IP != WaterList.end())
1328    WaterList.erase(IP);
1329
1330  // Okay, we know we can put an island before NewMBB now, do it!
1331  MF->insert(NewMBB, NewIsland);
1332
1333  // Update internal data structures to account for the newly inserted MBB.
1334  updateForInsertedWaterBlock(NewIsland);
1335
1336  // Decrement the old entry, and remove it if refcount becomes 0.
1337  decrementCPEReferenceCount(CPI, CPEMI);
1338
1339  // Now that we have an island to add the CPE to, clone the original CPE and
1340  // add it to the island.
1341  U.HighWaterMark = NewIsland;
1342  U.CPEMI = BuildMI(NewIsland, DebugLoc(), TII->get(Mips::CONSTPOOL_ENTRY))
1343                .addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
1344  CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1));
1345  ++NumCPEs;
1346
1347  // Mark the basic block as aligned as required by the const-pool entry.
1348  NewIsland->setAlignment(getCPELogAlign(U.CPEMI));
1349
1350  // Increase the size of the island block to account for the new entry.
1351  BBInfo[NewIsland->getNumber()].Size += Size;
1352  adjustBBOffsetsAfter(llvm::prior(MachineFunction::iterator(NewIsland)));
1353
1354  // No existing clone of this CPE is within range.
1355  // We will be generating a new clone.  Get a UID for it.
1356  unsigned ID = createPICLabelUId();
1357
1358  // Finally, change the CPI in the instruction operand to be ID.
1359  for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
1360    if (UserMI->getOperand(i).isCPI()) {
1361      UserMI->getOperand(i).setIndex(ID);
1362      break;
1363    }
1364
1365  DEBUG(dbgs() << "  Moved CPE to #" << ID << " CPI=" << CPI
1366        << format(" offset=%#x\n", BBInfo[NewIsland->getNumber()].Offset));
1367
1368  return true;
1369}
1370
1371/// removeDeadCPEMI - Remove a dead constant pool entry instruction. Update
1372/// sizes and offsets of impacted basic blocks.
1373void MipsConstantIslands::removeDeadCPEMI(MachineInstr *CPEMI) {
1374  MachineBasicBlock *CPEBB = CPEMI->getParent();
1375  unsigned Size = CPEMI->getOperand(2).getImm();
1376  CPEMI->eraseFromParent();
1377  BBInfo[CPEBB->getNumber()].Size -= Size;
1378  // All succeeding offsets have the current size value added in, fix this.
1379  if (CPEBB->empty()) {
1380    BBInfo[CPEBB->getNumber()].Size = 0;
1381
1382    // This block no longer needs to be aligned.
1383    CPEBB->setAlignment(0);
1384  } else
1385    // Entries are sorted by descending alignment, so realign from the front.
1386    CPEBB->setAlignment(getCPELogAlign(CPEBB->begin()));
1387
1388  adjustBBOffsetsAfter(CPEBB);
1389  // An island has only one predecessor BB and one successor BB. Check if
1390  // this BB's predecessor jumps directly to this BB's successor. This
1391  // shouldn't happen currently.
1392  assert(!BBIsJumpedOver(CPEBB) && "How did this happen?");
1393  // FIXME: remove the empty blocks after all the work is done?
1394}
1395
1396/// removeUnusedCPEntries - Remove constant pool entries whose refcounts
1397/// are zero.
1398bool MipsConstantIslands::removeUnusedCPEntries() {
1399  unsigned MadeChange = false;
1400  for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
1401      std::vector<CPEntry> &CPEs = CPEntries[i];
1402      for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) {
1403        if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) {
1404          removeDeadCPEMI(CPEs[j].CPEMI);
1405          CPEs[j].CPEMI = NULL;
1406          MadeChange = true;
1407        }
1408      }
1409  }
1410  return MadeChange;
1411}
1412
1413/// isBBInRange - Returns true if the distance between specific MI and
1414/// specific BB can fit in MI's displacement field.
1415bool MipsConstantIslands::isBBInRange
1416  (MachineInstr *MI,MachineBasicBlock *DestBB, unsigned MaxDisp) {
1417
1418unsigned PCAdj = 4;
1419
1420  unsigned BrOffset   = getOffsetOf(MI) + PCAdj;
1421  unsigned DestOffset = BBInfo[DestBB->getNumber()].Offset;
1422
1423  DEBUG(dbgs() << "Branch of destination BB#" << DestBB->getNumber()
1424               << " from BB#" << MI->getParent()->getNumber()
1425               << " max delta=" << MaxDisp
1426               << " from " << getOffsetOf(MI) << " to " << DestOffset
1427               << " offset " << int(DestOffset-BrOffset) << "\t" << *MI);
1428
1429  if (BrOffset <= DestOffset) {
1430    // Branch before the Dest.
1431    if (DestOffset-BrOffset <= MaxDisp)
1432      return true;
1433  } else {
1434    if (BrOffset-DestOffset <= MaxDisp)
1435      return true;
1436  }
1437  return false;
1438}
1439
1440/// fixupImmediateBr - Fix up an immediate branch whose destination is too far
1441/// away to fit in its displacement field.
1442bool MipsConstantIslands::fixupImmediateBr(ImmBranch &Br) {
1443  MachineInstr *MI = Br.MI;
1444  MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
1445
1446  // Check to see if the DestBB is already in-range.
1447  if (isBBInRange(MI, DestBB, Br.MaxDisp))
1448    return false;
1449
1450  if (!Br.isCond)
1451    return fixupUnconditionalBr(Br);
1452  return fixupConditionalBr(Br);
1453}
1454
1455/// fixupUnconditionalBr - Fix up an unconditional branch whose destination is
1456/// too far away to fit in its displacement field. If the LR register has been
1457/// spilled in the epilogue, then we can use BL to implement a far jump.
1458/// Otherwise, add an intermediate branch instruction to a branch.
1459bool
1460MipsConstantIslands::fixupUnconditionalBr(ImmBranch &Br) {
1461  MachineInstr *MI = Br.MI;
1462  MachineBasicBlock *MBB = MI->getParent();
1463  // Use BL to implement far jump.
1464  Br.MaxDisp = ((1 << 16)-1) * 2;
1465  MI->setDesc(TII->get(Mips::BimmX16));
1466  BBInfo[MBB->getNumber()].Size += 2;
1467  adjustBBOffsetsAfter(MBB);
1468  HasFarJump = true;
1469  ++NumUBrFixed;
1470
1471  DEBUG(dbgs() << "  Changed B to long jump " << *MI);
1472
1473  return true;
1474}
1475
1476/// fixupConditionalBr - Fix up a conditional branch whose destination is too
1477/// far away to fit in its displacement field. It is converted to an inverse
1478/// conditional branch + an unconditional branch to the destination.
1479bool
1480MipsConstantIslands::fixupConditionalBr(ImmBranch &Br) {
1481  MachineInstr *MI = Br.MI;
1482  MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
1483
1484  // Add an unconditional branch to the destination and invert the branch
1485  // condition to jump over it:
1486  // blt L1
1487  // =>
1488  // bge L2
1489  // b   L1
1490  // L2:
1491  unsigned CCReg = 0;  // FIXME
1492  unsigned CC=0; //FIXME
1493
1494  // If the branch is at the end of its MBB and that has a fall-through block,
1495  // direct the updated conditional branch to the fall-through block. Otherwise,
1496  // split the MBB before the next instruction.
1497  MachineBasicBlock *MBB = MI->getParent();
1498  MachineInstr *BMI = &MBB->back();
1499  bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
1500
1501  ++NumCBrFixed;
1502  if (BMI != MI) {
1503    if (llvm::next(MachineBasicBlock::iterator(MI)) == prior(MBB->end()) &&
1504        BMI->getOpcode() == Br.UncondBr) {
1505      // Last MI in the BB is an unconditional branch. Can we simply invert the
1506      // condition and swap destinations:
1507      // beq L1
1508      // b   L2
1509      // =>
1510      // bne L2
1511      // b   L1
1512      MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB();
1513      if (isBBInRange(MI, NewDest, Br.MaxDisp)) {
1514        DEBUG(dbgs() << "  Invert Bcc condition and swap its destination with "
1515                     << *BMI);
1516        BMI->getOperand(0).setMBB(DestBB);
1517        MI->getOperand(0).setMBB(NewDest);
1518        return true;
1519      }
1520    }
1521  }
1522
1523  if (NeedSplit) {
1524    splitBlockBeforeInstr(MI);
1525    // No need for the branch to the next block. We're adding an unconditional
1526    // branch to the destination.
1527    int delta = TII->GetInstSizeInBytes(&MBB->back());
1528    BBInfo[MBB->getNumber()].Size -= delta;
1529    MBB->back().eraseFromParent();
1530    // BBInfo[SplitBB].Offset is wrong temporarily, fixed below
1531  }
1532  MachineBasicBlock *NextBB = llvm::next(MachineFunction::iterator(MBB));
1533
1534  DEBUG(dbgs() << "  Insert B to BB#" << DestBB->getNumber()
1535               << " also invert condition and change dest. to BB#"
1536               << NextBB->getNumber() << "\n");
1537
1538  // Insert a new conditional branch and a new unconditional branch.
1539  // Also update the ImmBranch as well as adding a new entry for the new branch.
1540  BuildMI(MBB, DebugLoc(), TII->get(MI->getOpcode()))
1541    .addMBB(NextBB).addImm(CC).addReg(CCReg);
1542  Br.MI = &MBB->back();
1543  BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
1544  BuildMI(MBB, DebugLoc(), TII->get(Br.UncondBr)).addMBB(DestBB);
1545  BBInfo[MBB->getNumber()].Size += TII->GetInstSizeInBytes(&MBB->back());
1546  unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
1547  ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
1548
1549  // Remove the old conditional branch.  It may or may not still be in MBB.
1550  BBInfo[MI->getParent()->getNumber()].Size -= TII->GetInstSizeInBytes(MI);
1551  MI->eraseFromParent();
1552  adjustBBOffsetsAfter(MBB);
1553  return true;
1554}
1555
1556
1557void MipsConstantIslands::prescanForConstants() {
1558  unsigned J = 0;
1559  (void)J;
1560  PrescannedForConstants = true;
1561  for (MachineFunction::iterator B =
1562         MF->begin(), E = MF->end(); B != E; ++B) {
1563    for (MachineBasicBlock::instr_iterator I =
1564        B->instr_begin(), EB = B->instr_end(); I != EB; ++I) {
1565      switch(I->getDesc().getOpcode()) {
1566        case Mips::LwConstant32: {
1567          DEBUG(dbgs() << "constant island constant " << *I << "\n");
1568          J = I->getNumOperands();
1569          DEBUG(dbgs() << "num operands " << J  << "\n");
1570          MachineOperand& Literal = I->getOperand(1);
1571          if (Literal.isImm()) {
1572            int64_t V = Literal.getImm();
1573            DEBUG(dbgs() << "literal " << V  << "\n");
1574            Type *Int32Ty =
1575              Type::getInt32Ty(MF->getFunction()->getContext());
1576            const Constant *C = ConstantInt::get(Int32Ty, V);
1577            unsigned index = MCP->getConstantPoolIndex(C, 4);
1578            I->getOperand(2).ChangeToImmediate(index);
1579            DEBUG(dbgs() << "constant island constant " << *I << "\n");
1580            I->setDesc(TII->get(Mips::LwRxPcTcp16));
1581            I->RemoveOperand(1);
1582            I->RemoveOperand(1);
1583            I->addOperand(MachineOperand::CreateCPI(index, 0));
1584            I->addOperand(MachineOperand::CreateImm(4));
1585          }
1586          break;
1587        }
1588        default:
1589          break;
1590      }
1591    }
1592  }
1593}
1594
1595