ARMConstantIslandPass.cpp revision 40efc251cd7a52dd2375ec95ee38b1be4572178f
1//===-- ARMConstantIslandPass.cpp - ARM constant islands --------*- C++ -*-===//
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// This file contains a pass that splits the constant pool up into 'islands'
11// which are scattered through-out the function.  This is required due to the
12// limited pc-relative displacements that ARM has.
13//
14//===----------------------------------------------------------------------===//
15
16#define DEBUG_TYPE "arm-cp-islands"
17#include "ARM.h"
18#include "ARMAddressingModes.h"
19#include "ARMMachineFunctionInfo.h"
20#include "ARMInstrInfo.h"
21#include "llvm/CodeGen/MachineConstantPool.h"
22#include "llvm/CodeGen/MachineFunctionPass.h"
23#include "llvm/CodeGen/MachineInstrBuilder.h"
24#include "llvm/Target/TargetData.h"
25#include "llvm/Target/TargetMachine.h"
26#include "llvm/Support/Compiler.h"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/ADT/SmallVector.h"
30#include "llvm/ADT/STLExtras.h"
31#include "llvm/ADT/Statistic.h"
32using namespace llvm;
33
34STATISTIC(NumCPEs,     "Number of constpool entries");
35STATISTIC(NumSplit,    "Number of uncond branches inserted");
36STATISTIC(NumCBrFixed, "Number of cond branches fixed");
37STATISTIC(NumUBrFixed, "Number of uncond branches fixed");
38
39namespace {
40  /// ARMConstantIslands - Due to limited PC-relative displacements, ARM
41  /// requires constant pool entries to be scattered among the instructions
42  /// inside a function.  To do this, it completely ignores the normal LLVM
43  /// constant pool; instead, it places constants wherever it feels like with
44  /// special instructions.
45  ///
46  /// The terminology used in this pass includes:
47  ///   Islands - Clumps of constants placed in the function.
48  ///   Water   - Potential places where an island could be formed.
49  ///   CPE     - A constant pool entry that has been placed somewhere, which
50  ///             tracks a list of users.
51  class VISIBILITY_HIDDEN ARMConstantIslands : public MachineFunctionPass {
52    /// BBSizes - The size of each MachineBasicBlock in bytes of code, indexed
53    /// by MBB Number.  The two-byte pads required for Thumb alignment are
54    /// counted as part of the following block (i.e., the offset and size for
55    /// a padded block will both be ==2 mod 4).
56    std::vector<unsigned> BBSizes;
57
58    /// BBOffsets - the offset of each MBB in bytes, starting from 0.
59    /// The two-byte pads required for Thumb alignment are counted as part of
60    /// the following block.
61    std::vector<unsigned> BBOffsets;
62
63    /// WaterList - A sorted list of basic blocks where islands could be placed
64    /// (i.e. blocks that don't fall through to the following block, due
65    /// to a return, unreachable, or unconditional branch).
66    std::vector<MachineBasicBlock*> WaterList;
67
68    /// CPUser - One user of a constant pool, keeping the machine instruction
69    /// pointer, the constant pool being referenced, and the max displacement
70    /// allowed from the instruction to the CP.
71    struct CPUser {
72      MachineInstr *MI;
73      MachineInstr *CPEMI;
74      unsigned MaxDisp;
75      bool NegOk;
76      bool IsSoImm;
77      CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp,
78             bool neg, bool soimm)
79        : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp), NegOk(neg), IsSoImm(soimm) {}
80    };
81
82    /// CPUsers - Keep track of all of the machine instructions that use various
83    /// constant pools and their max displacement.
84    std::vector<CPUser> CPUsers;
85
86    /// CPEntry - One per constant pool entry, keeping the machine instruction
87    /// pointer, the constpool index, and the number of CPUser's which
88    /// reference this entry.
89    struct CPEntry {
90      MachineInstr *CPEMI;
91      unsigned CPI;
92      unsigned RefCount;
93      CPEntry(MachineInstr *cpemi, unsigned cpi, unsigned rc = 0)
94        : CPEMI(cpemi), CPI(cpi), RefCount(rc) {}
95    };
96
97    /// CPEntries - Keep track of all of the constant pool entry machine
98    /// instructions. For each original constpool index (i.e. those that
99    /// existed upon entry to this pass), it keeps a vector of entries.
100    /// Original elements are cloned as we go along; the clones are
101    /// put in the vector of the original element, but have distinct CPIs.
102    std::vector<std::vector<CPEntry> > CPEntries;
103
104    /// ImmBranch - One per immediate branch, keeping the machine instruction
105    /// pointer, conditional or unconditional, the max displacement,
106    /// and (if isCond is true) the corresponding unconditional branch
107    /// opcode.
108    struct ImmBranch {
109      MachineInstr *MI;
110      unsigned MaxDisp : 31;
111      bool isCond : 1;
112      int UncondBr;
113      ImmBranch(MachineInstr *mi, unsigned maxdisp, bool cond, int ubr)
114        : MI(mi), MaxDisp(maxdisp), isCond(cond), UncondBr(ubr) {}
115    };
116
117    /// ImmBranches - Keep track of all the immediate branch instructions.
118    ///
119    std::vector<ImmBranch> ImmBranches;
120
121    /// PushPopMIs - Keep track of all the Thumb push / pop instructions.
122    ///
123    SmallVector<MachineInstr*, 4> PushPopMIs;
124
125    /// HasFarJump - True if any far jump instruction has been emitted during
126    /// the branch fix up pass.
127    bool HasFarJump;
128
129    const TargetInstrInfo *TII;
130    ARMFunctionInfo *AFI;
131    bool isThumb;
132    bool isThumb1;
133    bool isThumb2;
134  public:
135    static char ID;
136    ARMConstantIslands() : MachineFunctionPass(&ID) {}
137
138    virtual bool runOnMachineFunction(MachineFunction &Fn);
139
140    virtual const char *getPassName() const {
141      return "ARM constant island placement and branch shortening pass";
142    }
143
144  private:
145    void DoInitialPlacement(MachineFunction &Fn,
146                            std::vector<MachineInstr*> &CPEMIs);
147    CPEntry *findConstPoolEntry(unsigned CPI, const MachineInstr *CPEMI);
148    void InitialFunctionScan(MachineFunction &Fn,
149                             const std::vector<MachineInstr*> &CPEMIs);
150    MachineBasicBlock *SplitBlockBeforeInstr(MachineInstr *MI);
151    void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB);
152    void AdjustBBOffsetsAfter(MachineBasicBlock *BB, int delta);
153    bool DecrementOldEntry(unsigned CPI, MachineInstr* CPEMI);
154    int LookForExistingCPEntry(CPUser& U, unsigned UserOffset);
155    bool LookForWater(CPUser&U, unsigned UserOffset,
156                      MachineBasicBlock** NewMBB);
157    MachineBasicBlock* AcceptWater(MachineBasicBlock *WaterBB,
158                        std::vector<MachineBasicBlock*>::iterator IP);
159    void CreateNewWater(unsigned CPUserIndex, unsigned UserOffset,
160                      MachineBasicBlock** NewMBB);
161    bool HandleConstantPoolUser(MachineFunction &Fn, unsigned CPUserIndex);
162    void RemoveDeadCPEMI(MachineInstr *CPEMI);
163    bool RemoveUnusedCPEntries();
164    bool CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
165                      MachineInstr *CPEMI, unsigned Disp, bool NegOk,
166                      bool DoDump = false);
167    bool WaterIsInRange(unsigned UserOffset, MachineBasicBlock *Water,
168                        CPUser &U);
169    bool OffsetIsInRange(unsigned UserOffset, unsigned TrialOffset,
170                         unsigned Disp, bool NegativeOK, bool IsSoImm = false);
171    bool BBIsInRange(MachineInstr *MI, MachineBasicBlock *BB, unsigned Disp);
172    bool FixUpImmediateBr(MachineFunction &Fn, ImmBranch &Br);
173    bool FixUpConditionalBr(MachineFunction &Fn, ImmBranch &Br);
174    bool FixUpUnconditionalBr(MachineFunction &Fn, ImmBranch &Br);
175    bool UndoLRSpillRestore();
176
177    unsigned GetOffsetOf(MachineInstr *MI) const;
178    void dumpBBs();
179    void verify(MachineFunction &Fn);
180  };
181  char ARMConstantIslands::ID = 0;
182}
183
184/// verify - check BBOffsets, BBSizes, alignment of islands
185void ARMConstantIslands::verify(MachineFunction &Fn) {
186  assert(BBOffsets.size() == BBSizes.size());
187  for (unsigned i = 1, e = BBOffsets.size(); i != e; ++i)
188    assert(BBOffsets[i-1]+BBSizes[i-1] == BBOffsets[i]);
189  if (!isThumb)
190    return;
191#ifndef NDEBUG
192  for (MachineFunction::iterator MBBI = Fn.begin(), E = Fn.end();
193       MBBI != E; ++MBBI) {
194    MachineBasicBlock *MBB = MBBI;
195    if (!MBB->empty() &&
196        MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) {
197      unsigned MBBId = MBB->getNumber();
198      assert((BBOffsets[MBBId]%4 == 0 && BBSizes[MBBId]%4 == 0) ||
199             (BBOffsets[MBBId]%4 != 0 && BBSizes[MBBId]%4 != 0));
200    }
201  }
202#endif
203}
204
205/// print block size and offset information - debugging
206void ARMConstantIslands::dumpBBs() {
207  for (unsigned J = 0, E = BBOffsets.size(); J !=E; ++J) {
208    DOUT << "block " << J << " offset " << BBOffsets[J] <<
209                            " size " << BBSizes[J] << "\n";
210  }
211}
212
213/// createARMConstantIslandPass - returns an instance of the constpool
214/// island pass.
215FunctionPass *llvm::createARMConstantIslandPass() {
216  return new ARMConstantIslands();
217}
218
219bool ARMConstantIslands::runOnMachineFunction(MachineFunction &Fn) {
220  MachineConstantPool &MCP = *Fn.getConstantPool();
221
222  TII = Fn.getTarget().getInstrInfo();
223  AFI = Fn.getInfo<ARMFunctionInfo>();
224  isThumb = AFI->isThumbFunction();
225  isThumb1 = AFI->isThumb1OnlyFunction();
226  isThumb2 = AFI->isThumb2Function();
227
228  HasFarJump = false;
229
230  // Renumber all of the machine basic blocks in the function, guaranteeing that
231  // the numbers agree with the position of the block in the function.
232  Fn.RenumberBlocks();
233
234  // Thumb1 functions containing constant pools get 2-byte alignment.
235  // This is so we can keep exact track of where the alignment padding goes.
236
237  // Set default. Thumb1 function is 1-byte aligned, ARM and Thumb2 are 2-byte
238  // aligned.
239  AFI->setAlign(isThumb1 ? 1U : 2U);
240
241  // Perform the initial placement of the constant pool entries.  To start with,
242  // we put them all at the end of the function.
243  std::vector<MachineInstr*> CPEMIs;
244  if (!MCP.isEmpty()) {
245    DoInitialPlacement(Fn, CPEMIs);
246    if (isThumb1)
247      AFI->setAlign(2U);
248  }
249
250  /// The next UID to take is the first unused one.
251  AFI->initConstPoolEntryUId(CPEMIs.size());
252
253  // Do the initial scan of the function, building up information about the
254  // sizes of each block, the location of all the water, and finding all of the
255  // constant pool users.
256  InitialFunctionScan(Fn, CPEMIs);
257  CPEMIs.clear();
258
259  /// Remove dead constant pool entries.
260  RemoveUnusedCPEntries();
261
262  // Iteratively place constant pool entries and fix up branches until there
263  // is no change.
264  bool MadeChange = false;
265  while (true) {
266    bool Change = false;
267    for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
268      Change |= HandleConstantPoolUser(Fn, i);
269    DEBUG(dumpBBs());
270    for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
271      Change |= FixUpImmediateBr(Fn, ImmBranches[i]);
272    DEBUG(dumpBBs());
273    if (!Change)
274      break;
275    MadeChange = true;
276  }
277
278  // After a while, this might be made debug-only, but it is not expensive.
279  verify(Fn);
280
281  // If LR has been forced spilled and no far jumps (i.e. BL) has been issued.
282  // Undo the spill / restore of LR if possible.
283  if (!HasFarJump && AFI->isLRSpilledForFarJump() && isThumb)
284    MadeChange |= UndoLRSpillRestore();
285
286  BBSizes.clear();
287  BBOffsets.clear();
288  WaterList.clear();
289  CPUsers.clear();
290  CPEntries.clear();
291  ImmBranches.clear();
292  PushPopMIs.clear();
293
294  return MadeChange;
295}
296
297/// DoInitialPlacement - Perform the initial placement of the constant pool
298/// entries.  To start with, we put them all at the end of the function.
299void ARMConstantIslands::DoInitialPlacement(MachineFunction &Fn,
300                                        std::vector<MachineInstr*> &CPEMIs) {
301  // Create the basic block to hold the CPE's.
302  MachineBasicBlock *BB = Fn.CreateMachineBasicBlock();
303  Fn.push_back(BB);
304
305  // Add all of the constants from the constant pool to the end block, use an
306  // identity mapping of CPI's to CPE's.
307  const std::vector<MachineConstantPoolEntry> &CPs =
308    Fn.getConstantPool()->getConstants();
309
310  const TargetData &TD = *Fn.getTarget().getTargetData();
311  for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
312    unsigned Size = TD.getTypeAllocSize(CPs[i].getType());
313    // Verify that all constant pool entries are a multiple of 4 bytes.  If not,
314    // we would have to pad them out or something so that instructions stay
315    // aligned.
316    assert((Size & 3) == 0 && "CP Entry not multiple of 4 bytes!");
317    MachineInstr *CPEMI =
318      BuildMI(BB, DebugLoc::getUnknownLoc(), TII->get(ARM::CONSTPOOL_ENTRY))
319                           .addImm(i).addConstantPoolIndex(i).addImm(Size);
320    CPEMIs.push_back(CPEMI);
321
322    // Add a new CPEntry, but no corresponding CPUser yet.
323    std::vector<CPEntry> CPEs;
324    CPEs.push_back(CPEntry(CPEMI, i));
325    CPEntries.push_back(CPEs);
326    NumCPEs++;
327    DOUT << "Moved CPI#" << i << " to end of function as #" << i << "\n";
328  }
329}
330
331/// BBHasFallthrough - Return true if the specified basic block can fallthrough
332/// into the block immediately after it.
333static bool BBHasFallthrough(MachineBasicBlock *MBB) {
334  // Get the next machine basic block in the function.
335  MachineFunction::iterator MBBI = MBB;
336  if (next(MBBI) == MBB->getParent()->end())  // Can't fall off end of function.
337    return false;
338
339  MachineBasicBlock *NextBB = next(MBBI);
340  for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
341       E = MBB->succ_end(); I != E; ++I)
342    if (*I == NextBB)
343      return true;
344
345  return false;
346}
347
348/// findConstPoolEntry - Given the constpool index and CONSTPOOL_ENTRY MI,
349/// look up the corresponding CPEntry.
350ARMConstantIslands::CPEntry
351*ARMConstantIslands::findConstPoolEntry(unsigned CPI,
352                                        const MachineInstr *CPEMI) {
353  std::vector<CPEntry> &CPEs = CPEntries[CPI];
354  // Number of entries per constpool index should be small, just do a
355  // linear search.
356  for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
357    if (CPEs[i].CPEMI == CPEMI)
358      return &CPEs[i];
359  }
360  return NULL;
361}
362
363/// InitialFunctionScan - Do the initial scan of the function, building up
364/// information about the sizes of each block, the location of all the water,
365/// and finding all of the constant pool users.
366void ARMConstantIslands::InitialFunctionScan(MachineFunction &Fn,
367                                 const std::vector<MachineInstr*> &CPEMIs) {
368  unsigned Offset = 0;
369  for (MachineFunction::iterator MBBI = Fn.begin(), E = Fn.end();
370       MBBI != E; ++MBBI) {
371    MachineBasicBlock &MBB = *MBBI;
372
373    // If this block doesn't fall through into the next MBB, then this is
374    // 'water' that a constant pool island could be placed.
375    if (!BBHasFallthrough(&MBB))
376      WaterList.push_back(&MBB);
377
378    unsigned MBBSize = 0;
379    for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
380         I != E; ++I) {
381      // Add instruction size to MBBSize.
382      MBBSize += TII->GetInstSizeInBytes(I);
383
384      int Opc = I->getOpcode();
385      if (I->getDesc().isBranch()) {
386        bool isCond = false;
387        unsigned Bits = 0;
388        unsigned Scale = 1;
389        int UOpc = Opc;
390        switch (Opc) {
391        case ARM::tBR_JTr:
392        case ARM::t2BR_JTr:
393        case ARM::t2BR_JTm:
394        case ARM::t2BR_JTadd:
395          // A Thumb table jump may involve padding; for the offsets to
396          // be right, functions containing these must be 4-byte aligned.
397          AFI->setAlign(2U);
398          if ((Offset+MBBSize)%4 != 0)
399            MBBSize += 2;           // padding
400          continue;   // Does not get an entry in ImmBranches
401        default:
402          continue;  // Ignore other JT branches
403        case ARM::Bcc:
404          isCond = true;
405          UOpc = ARM::B;
406          // Fallthrough
407        case ARM::B:
408          Bits = 24;
409          Scale = 4;
410          break;
411        case ARM::tBcc:
412          isCond = true;
413          UOpc = ARM::tB;
414          Bits = 8;
415          Scale = 2;
416          break;
417        case ARM::tB:
418          Bits = 11;
419          Scale = 2;
420          break;
421        case ARM::t2Bcc:
422          isCond = true;
423          UOpc = ARM::t2B;
424          Bits = 20;
425          Scale = 2;
426          break;
427        case ARM::t2B:
428          Bits = 24;
429          Scale = 2;
430          break;
431        }
432
433        // Record this immediate branch.
434        unsigned MaxOffs = ((1 << (Bits-1))-1) * Scale;
435        ImmBranches.push_back(ImmBranch(I, MaxOffs, isCond, UOpc));
436      }
437
438      if (Opc == ARM::tPUSH || Opc == ARM::tPOP_RET)
439        PushPopMIs.push_back(I);
440
441      if (Opc == ARM::CONSTPOOL_ENTRY)
442        continue;
443
444      // Scan the instructions for constant pool operands.
445      for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
446        if (I->getOperand(op).isCPI()) {
447          // We found one.  The addressing mode tells us the max displacement
448          // from the PC that this instruction permits.
449
450          // Basic size info comes from the TSFlags field.
451          unsigned Bits = 0;
452          unsigned Scale = 1;
453          bool NegOk = false;
454          bool IsSoImm = false;
455
456          switch (Opc) {
457          default:
458            llvm_unreachable("Unknown addressing mode for CP reference!");
459            break;
460
461          // Taking the address of a CP entry.
462          case ARM::LEApcrel:
463            // This takes a SoImm, which is 8 bit immediate rotated. We'll
464            // pretend the maximum offset is 255 * 4. Since each instruction
465            // 4 byte wide, this is always correct. We'llc heck for other
466            // displacements that fits in a SoImm as well.
467            Bits = 8;
468            Scale = 4;
469            NegOk = true;
470            IsSoImm = true;
471            break;
472          case ARM::t2LEApcrel:
473            Bits = 12;
474            NegOk = true;
475            break;
476          case ARM::tLEApcrel:
477            Bits = 8;
478            Scale = 4;
479            break;
480
481          case ARM::LDR:
482          case ARM::LDRcp:
483          case ARM::t2LDRpci:
484            Bits = 12;  // +-offset_12
485            NegOk = true;
486            break;
487
488          case ARM::tLDRpci:
489          case ARM::tLDRcp:
490            Bits = 8;
491            Scale = 4;  // +(offset_8*4)
492            break;
493
494          case ARM::FLDD:
495          case ARM::FLDS:
496            Bits = 8;
497            Scale = 4;  // +-(offset_8*4)
498            NegOk = true;
499            break;
500          }
501
502          // Remember that this is a user of a CP entry.
503          unsigned CPI = I->getOperand(op).getIndex();
504          MachineInstr *CPEMI = CPEMIs[CPI];
505          unsigned MaxOffs = ((1 << Bits)-1) * Scale;
506          CPUsers.push_back(CPUser(I, CPEMI, MaxOffs, NegOk, IsSoImm));
507
508          // Increment corresponding CPEntry reference count.
509          CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
510          assert(CPE && "Cannot find a corresponding CPEntry!");
511          CPE->RefCount++;
512
513          // Instructions can only use one CP entry, don't bother scanning the
514          // rest of the operands.
515          break;
516        }
517    }
518
519    // In thumb mode, if this block is a constpool island, we may need padding
520    // so it's aligned on 4 byte boundary.
521    if (isThumb &&
522        !MBB.empty() &&
523        MBB.begin()->getOpcode() == ARM::CONSTPOOL_ENTRY &&
524        (Offset%4) != 0)
525      MBBSize += 2;
526
527    BBSizes.push_back(MBBSize);
528    BBOffsets.push_back(Offset);
529    Offset += MBBSize;
530  }
531}
532
533/// GetOffsetOf - Return the current offset of the specified machine instruction
534/// from the start of the function.  This offset changes as stuff is moved
535/// around inside the function.
536unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const {
537  MachineBasicBlock *MBB = MI->getParent();
538
539  // The offset is composed of two things: the sum of the sizes of all MBB's
540  // before this instruction's block, and the offset from the start of the block
541  // it is in.
542  unsigned Offset = BBOffsets[MBB->getNumber()];
543
544  // If we're looking for a CONSTPOOL_ENTRY in Thumb, see if this block has
545  // alignment padding, and compensate if so.
546  if (isThumb &&
547      MI->getOpcode() == ARM::CONSTPOOL_ENTRY &&
548      Offset%4 != 0)
549    Offset += 2;
550
551  // Sum instructions before MI in MBB.
552  for (MachineBasicBlock::iterator I = MBB->begin(); ; ++I) {
553    assert(I != MBB->end() && "Didn't find MI in its own basic block?");
554    if (&*I == MI) return Offset;
555    Offset += TII->GetInstSizeInBytes(I);
556  }
557}
558
559/// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
560/// ID.
561static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
562                              const MachineBasicBlock *RHS) {
563  return LHS->getNumber() < RHS->getNumber();
564}
565
566/// UpdateForInsertedWaterBlock - When a block is newly inserted into the
567/// machine function, it upsets all of the block numbers.  Renumber the blocks
568/// and update the arrays that parallel this numbering.
569void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
570  // Renumber the MBB's to keep them consequtive.
571  NewBB->getParent()->RenumberBlocks(NewBB);
572
573  // Insert a size into BBSizes to align it properly with the (newly
574  // renumbered) block numbers.
575  BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0);
576
577  // Likewise for BBOffsets.
578  BBOffsets.insert(BBOffsets.begin()+NewBB->getNumber(), 0);
579
580  // Next, update WaterList.  Specifically, we need to add NewMBB as having
581  // available water after it.
582  std::vector<MachineBasicBlock*>::iterator IP =
583    std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
584                     CompareMBBNumbers);
585  WaterList.insert(IP, NewBB);
586}
587
588
589/// Split the basic block containing MI into two blocks, which are joined by
590/// an unconditional branch.  Update datastructures and renumber blocks to
591/// account for this change and returns the newly created block.
592MachineBasicBlock *ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) {
593  MachineBasicBlock *OrigBB = MI->getParent();
594  MachineFunction &MF = *OrigBB->getParent();
595
596  // Create a new MBB for the code after the OrigBB.
597  MachineBasicBlock *NewBB =
598    MF.CreateMachineBasicBlock(OrigBB->getBasicBlock());
599  MachineFunction::iterator MBBI = OrigBB; ++MBBI;
600  MF.insert(MBBI, NewBB);
601
602  // Splice the instructions starting with MI over to NewBB.
603  NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
604
605  // Add an unconditional branch from OrigBB to NewBB.
606  // Note the new unconditional branch is not being recorded.
607  // There doesn't seem to be meaningful DebugInfo available; this doesn't
608  // correspond to anything in the source.
609  unsigned Opc = isThumb ? (isThumb2 ? ARM::t2B : ARM::tB) : ARM::B;
610  BuildMI(OrigBB, DebugLoc::getUnknownLoc(), TII->get(Opc)).addMBB(NewBB);
611  NumSplit++;
612
613  // Update the CFG.  All succs of OrigBB are now succs of NewBB.
614  while (!OrigBB->succ_empty()) {
615    MachineBasicBlock *Succ = *OrigBB->succ_begin();
616    OrigBB->removeSuccessor(Succ);
617    NewBB->addSuccessor(Succ);
618
619    // This pass should be run after register allocation, so there should be no
620    // PHI nodes to update.
621    assert((Succ->empty() || Succ->begin()->getOpcode() != TargetInstrInfo::PHI)
622           && "PHI nodes should be eliminated by now!");
623  }
624
625  // OrigBB branches to NewBB.
626  OrigBB->addSuccessor(NewBB);
627
628  // Update internal data structures to account for the newly inserted MBB.
629  // This is almost the same as UpdateForInsertedWaterBlock, except that
630  // the Water goes after OrigBB, not NewBB.
631  MF.RenumberBlocks(NewBB);
632
633  // Insert a size into BBSizes to align it properly with the (newly
634  // renumbered) block numbers.
635  BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0);
636
637  // Likewise for BBOffsets.
638  BBOffsets.insert(BBOffsets.begin()+NewBB->getNumber(), 0);
639
640  // Next, update WaterList.  Specifically, we need to add OrigMBB as having
641  // available water after it (but not if it's already there, which happens
642  // when splitting before a conditional branch that is followed by an
643  // unconditional branch - in that case we want to insert NewBB).
644  std::vector<MachineBasicBlock*>::iterator IP =
645    std::lower_bound(WaterList.begin(), WaterList.end(), OrigBB,
646                     CompareMBBNumbers);
647  MachineBasicBlock* WaterBB = *IP;
648  if (WaterBB == OrigBB)
649    WaterList.insert(next(IP), NewBB);
650  else
651    WaterList.insert(IP, OrigBB);
652
653  // Figure out how large the first NewMBB is.  (It cannot
654  // contain a constpool_entry or tablejump.)
655  unsigned NewBBSize = 0;
656  for (MachineBasicBlock::iterator I = NewBB->begin(), E = NewBB->end();
657       I != E; ++I)
658    NewBBSize += TII->GetInstSizeInBytes(I);
659
660  unsigned OrigBBI = OrigBB->getNumber();
661  unsigned NewBBI = NewBB->getNumber();
662  // Set the size of NewBB in BBSizes.
663  BBSizes[NewBBI] = NewBBSize;
664
665  // We removed instructions from UserMBB, subtract that off from its size.
666  // Add 2 or 4 to the block to count the unconditional branch we added to it.
667  unsigned delta = isThumb1 ? 2 : 4;
668  BBSizes[OrigBBI] -= NewBBSize - delta;
669
670  // ...and adjust BBOffsets for NewBB accordingly.
671  BBOffsets[NewBBI] = BBOffsets[OrigBBI] + BBSizes[OrigBBI];
672
673  // All BBOffsets following these blocks must be modified.
674  AdjustBBOffsetsAfter(NewBB, delta);
675
676  return NewBB;
677}
678
679/// OffsetIsInRange - Checks whether UserOffset (the location of a constant pool
680/// reference) is within MaxDisp of TrialOffset (a proposed location of a
681/// constant pool entry).
682bool ARMConstantIslands::OffsetIsInRange(unsigned UserOffset,
683                                         unsigned TrialOffset, unsigned MaxDisp,
684                                         bool NegativeOK, bool IsSoImm) {
685  // On Thumb offsets==2 mod 4 are rounded down by the hardware for
686  // purposes of the displacement computation; compensate for that here.
687  // Effectively, the valid range of displacements is 2 bytes smaller for such
688  // references.
689  if (isThumb && UserOffset%4 !=0)
690    UserOffset -= 2;
691  // CPEs will be rounded up to a multiple of 4.
692  if (isThumb && TrialOffset%4 != 0)
693    TrialOffset += 2;
694
695  if (UserOffset <= TrialOffset) {
696    // User before the Trial.
697    if (TrialOffset - UserOffset <= MaxDisp)
698      return true;
699    // FIXME: Make use full range of soimm values.
700  } else if (NegativeOK) {
701    if (UserOffset - TrialOffset <= MaxDisp)
702      return true;
703    // FIXME: Make use full range of soimm values.
704  }
705  return false;
706}
707
708/// WaterIsInRange - Returns true if a CPE placed after the specified
709/// Water (a basic block) will be in range for the specific MI.
710
711bool ARMConstantIslands::WaterIsInRange(unsigned UserOffset,
712                                        MachineBasicBlock* Water, CPUser &U) {
713  unsigned MaxDisp = U.MaxDisp;
714  unsigned CPEOffset = BBOffsets[Water->getNumber()] +
715                       BBSizes[Water->getNumber()];
716
717  // If the CPE is to be inserted before the instruction, that will raise
718  // the offset of the instruction.  (Currently applies only to ARM, so
719  // no alignment compensation attempted here.)
720  if (CPEOffset < UserOffset)
721    UserOffset += U.CPEMI->getOperand(2).getImm();
722
723  return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, U.NegOk, U.IsSoImm);
724}
725
726/// CPEIsInRange - Returns true if the distance between specific MI and
727/// specific ConstPool entry instruction can fit in MI's displacement field.
728bool ARMConstantIslands::CPEIsInRange(MachineInstr *MI, unsigned UserOffset,
729                                      MachineInstr *CPEMI, unsigned MaxDisp,
730                                      bool NegOk, bool DoDump) {
731  unsigned CPEOffset  = GetOffsetOf(CPEMI);
732  assert(CPEOffset%4 == 0 && "Misaligned CPE");
733
734  if (DoDump) {
735    DOUT << "User of CPE#" << CPEMI->getOperand(0).getImm()
736         << " max delta=" << MaxDisp
737         << " insn address=" << UserOffset
738         << " CPE address=" << CPEOffset
739         << " offset=" << int(CPEOffset-UserOffset) << "\t" << *MI;
740  }
741
742  return OffsetIsInRange(UserOffset, CPEOffset, MaxDisp, NegOk);
743}
744
745#ifndef NDEBUG
746/// BBIsJumpedOver - Return true of the specified basic block's only predecessor
747/// unconditionally branches to its only successor.
748static bool BBIsJumpedOver(MachineBasicBlock *MBB) {
749  if (MBB->pred_size() != 1 || MBB->succ_size() != 1)
750    return false;
751
752  MachineBasicBlock *Succ = *MBB->succ_begin();
753  MachineBasicBlock *Pred = *MBB->pred_begin();
754  MachineInstr *PredMI = &Pred->back();
755  if (PredMI->getOpcode() == ARM::B || PredMI->getOpcode() == ARM::tB
756      || PredMI->getOpcode() == ARM::t2B)
757    return PredMI->getOperand(0).getMBB() == Succ;
758  return false;
759}
760#endif // NDEBUG
761
762void ARMConstantIslands::AdjustBBOffsetsAfter(MachineBasicBlock *BB,
763                                              int delta) {
764  MachineFunction::iterator MBBI = BB; MBBI = next(MBBI);
765  for(unsigned i = BB->getNumber()+1, e = BB->getParent()->getNumBlockIDs();
766      i < e; ++i) {
767    BBOffsets[i] += delta;
768    // If some existing blocks have padding, adjust the padding as needed, a
769    // bit tricky.  delta can be negative so don't use % on that.
770    if (!isThumb)
771      continue;
772    MachineBasicBlock *MBB = MBBI;
773    if (!MBB->empty()) {
774      // Constant pool entries require padding.
775      if (MBB->begin()->getOpcode() == ARM::CONSTPOOL_ENTRY) {
776        unsigned oldOffset = BBOffsets[i] - delta;
777        if (oldOffset%4==0 && BBOffsets[i]%4!=0) {
778          // add new padding
779          BBSizes[i] += 2;
780          delta += 2;
781        } else if (oldOffset%4!=0 && BBOffsets[i]%4==0) {
782          // remove existing padding
783          BBSizes[i] -=2;
784          delta -= 2;
785        }
786      }
787      // Thumb1 jump tables require padding.  They should be at the end;
788      // following unconditional branches are removed by AnalyzeBranch.
789      MachineInstr *ThumbJTMI = prior(MBB->end());
790      if (ThumbJTMI->getOpcode() == ARM::tBR_JTr ||
791          ThumbJTMI->getOpcode() == ARM::t2BR_JTr ||
792          ThumbJTMI->getOpcode() == ARM::t2BR_JTm ||
793          ThumbJTMI->getOpcode() == ARM::t2BR_JTadd) {
794        unsigned newMIOffset = GetOffsetOf(ThumbJTMI);
795        unsigned oldMIOffset = newMIOffset - delta;
796        if (oldMIOffset%4 == 0 && newMIOffset%4 != 0) {
797          // remove existing padding
798          BBSizes[i] -= 2;
799          delta -= 2;
800        } else if (oldMIOffset%4 != 0 && newMIOffset%4 == 0) {
801          // add new padding
802          BBSizes[i] += 2;
803          delta += 2;
804        }
805      }
806      if (delta==0)
807        return;
808    }
809    MBBI = next(MBBI);
810  }
811}
812
813/// DecrementOldEntry - find the constant pool entry with index CPI
814/// and instruction CPEMI, and decrement its refcount.  If the refcount
815/// becomes 0 remove the entry and instruction.  Returns true if we removed
816/// the entry, false if we didn't.
817
818bool ARMConstantIslands::DecrementOldEntry(unsigned CPI, MachineInstr *CPEMI) {
819  // Find the old entry. Eliminate it if it is no longer used.
820  CPEntry *CPE = findConstPoolEntry(CPI, CPEMI);
821  assert(CPE && "Unexpected!");
822  if (--CPE->RefCount == 0) {
823    RemoveDeadCPEMI(CPEMI);
824    CPE->CPEMI = NULL;
825    NumCPEs--;
826    return true;
827  }
828  return false;
829}
830
831/// LookForCPEntryInRange - see if the currently referenced CPE is in range;
832/// if not, see if an in-range clone of the CPE is in range, and if so,
833/// change the data structures so the user references the clone.  Returns:
834/// 0 = no existing entry found
835/// 1 = entry found, and there were no code insertions or deletions
836/// 2 = entry found, and there were code insertions or deletions
837int ARMConstantIslands::LookForExistingCPEntry(CPUser& U, unsigned UserOffset)
838{
839  MachineInstr *UserMI = U.MI;
840  MachineInstr *CPEMI  = U.CPEMI;
841
842  // Check to see if the CPE is already in-range.
843  if (CPEIsInRange(UserMI, UserOffset, CPEMI, U.MaxDisp, U.NegOk, true)) {
844    DOUT << "In range\n";
845    return 1;
846  }
847
848  // No.  Look for previously created clones of the CPE that are in range.
849  unsigned CPI = CPEMI->getOperand(1).getIndex();
850  std::vector<CPEntry> &CPEs = CPEntries[CPI];
851  for (unsigned i = 0, e = CPEs.size(); i != e; ++i) {
852    // We already tried this one
853    if (CPEs[i].CPEMI == CPEMI)
854      continue;
855    // Removing CPEs can leave empty entries, skip
856    if (CPEs[i].CPEMI == NULL)
857      continue;
858    if (CPEIsInRange(UserMI, UserOffset, CPEs[i].CPEMI, U.MaxDisp, U.NegOk)) {
859      DOUT << "Replacing CPE#" << CPI << " with CPE#" << CPEs[i].CPI << "\n";
860      // Point the CPUser node to the replacement
861      U.CPEMI = CPEs[i].CPEMI;
862      // Change the CPI in the instruction operand to refer to the clone.
863      for (unsigned j = 0, e = UserMI->getNumOperands(); j != e; ++j)
864        if (UserMI->getOperand(j).isCPI()) {
865          UserMI->getOperand(j).setIndex(CPEs[i].CPI);
866          break;
867        }
868      // Adjust the refcount of the clone...
869      CPEs[i].RefCount++;
870      // ...and the original.  If we didn't remove the old entry, none of the
871      // addresses changed, so we don't need another pass.
872      return DecrementOldEntry(CPI, CPEMI) ? 2 : 1;
873    }
874  }
875  return 0;
876}
877
878/// getUnconditionalBrDisp - Returns the maximum displacement that can fit in
879/// the specific unconditional branch instruction.
880static inline unsigned getUnconditionalBrDisp(int Opc) {
881  switch (Opc) {
882  case ARM::tB:
883    return ((1<<10)-1)*2;
884  case ARM::t2B:
885    return ((1<<23)-1)*2;
886  default:
887    break;
888  }
889
890  return ((1<<23)-1)*4;
891}
892
893/// AcceptWater - Small amount of common code factored out of the following.
894
895MachineBasicBlock* ARMConstantIslands::AcceptWater(MachineBasicBlock *WaterBB,
896                          std::vector<MachineBasicBlock*>::iterator IP) {
897  DOUT << "found water in range\n";
898  // Remove the original WaterList entry; we want subsequent
899  // insertions in this vicinity to go after the one we're
900  // about to insert.  This considerably reduces the number
901  // of times we have to move the same CPE more than once.
902  WaterList.erase(IP);
903  // CPE goes before following block (NewMBB).
904  return next(MachineFunction::iterator(WaterBB));
905}
906
907/// LookForWater - look for an existing entry in the WaterList in which
908/// we can place the CPE referenced from U so it's within range of U's MI.
909/// Returns true if found, false if not.  If it returns true, *NewMBB
910/// is set to the WaterList entry.
911/// For ARM, we prefer the water that's farthest away. For Thumb, prefer
912/// water that will not introduce padding to water that will; within each
913/// group, prefer the water that's farthest away.
914bool ARMConstantIslands::LookForWater(CPUser &U, unsigned UserOffset,
915                                      MachineBasicBlock** NewMBB) {
916  std::vector<MachineBasicBlock*>::iterator IPThatWouldPad;
917  MachineBasicBlock* WaterBBThatWouldPad = NULL;
918  if (!WaterList.empty()) {
919    for (std::vector<MachineBasicBlock*>::iterator IP = prior(WaterList.end()),
920           B = WaterList.begin();; --IP) {
921      MachineBasicBlock* WaterBB = *IP;
922      if (WaterIsInRange(UserOffset, WaterBB, U)) {
923        unsigned WBBId = WaterBB->getNumber();
924        if (isThumb &&
925            (BBOffsets[WBBId] + BBSizes[WBBId])%4 != 0) {
926          // This is valid Water, but would introduce padding.  Remember
927          // it in case we don't find any Water that doesn't do this.
928          if (!WaterBBThatWouldPad) {
929            WaterBBThatWouldPad = WaterBB;
930            IPThatWouldPad = IP;
931          }
932        } else {
933          *NewMBB = AcceptWater(WaterBB, IP);
934          return true;
935        }
936      }
937      if (IP == B)
938        break;
939    }
940  }
941  if (isThumb && WaterBBThatWouldPad) {
942    *NewMBB = AcceptWater(WaterBBThatWouldPad, IPThatWouldPad);
943    return true;
944  }
945  return false;
946}
947
948/// CreateNewWater - No existing WaterList entry will work for
949/// CPUsers[CPUserIndex], so create a place to put the CPE.  The end of the
950/// block is used if in range, and the conditional branch munged so control
951/// flow is correct.  Otherwise the block is split to create a hole with an
952/// unconditional branch around it.  In either case *NewMBB is set to a
953/// block following which the new island can be inserted (the WaterList
954/// is not adjusted).
955
956void ARMConstantIslands::CreateNewWater(unsigned CPUserIndex,
957                        unsigned UserOffset, MachineBasicBlock** NewMBB) {
958  CPUser &U = CPUsers[CPUserIndex];
959  MachineInstr *UserMI = U.MI;
960  MachineInstr *CPEMI  = U.CPEMI;
961  MachineBasicBlock *UserMBB = UserMI->getParent();
962  unsigned OffsetOfNextBlock = BBOffsets[UserMBB->getNumber()] +
963                               BBSizes[UserMBB->getNumber()];
964  assert(OffsetOfNextBlock== BBOffsets[UserMBB->getNumber()+1]);
965
966  // If the use is at the end of the block, or the end of the block
967  // is within range, make new water there.  (The addition below is
968  // for the unconditional branch we will be adding:  4 bytes on ARM + Thumb2,
969  // 2 on Thumb1.  Possible Thumb1 alignment padding is allowed for
970  // inside OffsetIsInRange.
971  // If the block ends in an unconditional branch already, it is water,
972  // and is known to be out of range, so we'll always be adding a branch.)
973  if (&UserMBB->back() == UserMI ||
974      OffsetIsInRange(UserOffset, OffsetOfNextBlock + (isThumb1 ? 2: 4),
975                      U.MaxDisp, U.NegOk, U.IsSoImm)) {
976    DOUT << "Split at end of block\n";
977    if (&UserMBB->back() == UserMI)
978      assert(BBHasFallthrough(UserMBB) && "Expected a fallthrough BB!");
979    *NewMBB = next(MachineFunction::iterator(UserMBB));
980    // Add an unconditional branch from UserMBB to fallthrough block.
981    // Record it for branch lengthening; this new branch will not get out of
982    // range, but if the preceding conditional branch is out of range, the
983    // targets will be exchanged, and the altered branch may be out of
984    // range, so the machinery has to know about it.
985    int UncondBr = isThumb ? ((isThumb2) ? ARM::t2B : ARM::tB) : ARM::B;
986    BuildMI(UserMBB, DebugLoc::getUnknownLoc(),
987            TII->get(UncondBr)).addMBB(*NewMBB);
988    unsigned MaxDisp = getUnconditionalBrDisp(UncondBr);
989    ImmBranches.push_back(ImmBranch(&UserMBB->back(),
990                          MaxDisp, false, UncondBr));
991    int delta = isThumb1 ? 2 : 4;
992    BBSizes[UserMBB->getNumber()] += delta;
993    AdjustBBOffsetsAfter(UserMBB, delta);
994  } else {
995    // What a big block.  Find a place within the block to split it.
996    // This is a little tricky on Thumb1 since instructions are 2 bytes
997    // and constant pool entries are 4 bytes: if instruction I references
998    // island CPE, and instruction I+1 references CPE', it will
999    // not work well to put CPE as far forward as possible, since then
1000    // CPE' cannot immediately follow it (that location is 2 bytes
1001    // farther away from I+1 than CPE was from I) and we'd need to create
1002    // a new island.  So, we make a first guess, then walk through the
1003    // instructions between the one currently being looked at and the
1004    // possible insertion point, and make sure any other instructions
1005    // that reference CPEs will be able to use the same island area;
1006    // if not, we back up the insertion point.
1007
1008    // The 4 in the following is for the unconditional branch we'll be
1009    // inserting (allows for long branch on Thumb1).  Alignment of the
1010    // island is handled inside OffsetIsInRange.
1011    unsigned BaseInsertOffset = UserOffset + U.MaxDisp -4;
1012    // This could point off the end of the block if we've already got
1013    // constant pool entries following this block; only the last one is
1014    // in the water list.  Back past any possible branches (allow for a
1015    // conditional and a maximally long unconditional).
1016    if (BaseInsertOffset >= BBOffsets[UserMBB->getNumber()+1])
1017      BaseInsertOffset = BBOffsets[UserMBB->getNumber()+1] -
1018                              (isThumb1 ? 6 : 8);
1019    unsigned EndInsertOffset = BaseInsertOffset +
1020           CPEMI->getOperand(2).getImm();
1021    MachineBasicBlock::iterator MI = UserMI;
1022    ++MI;
1023    unsigned CPUIndex = CPUserIndex+1;
1024    for (unsigned Offset = UserOffset+TII->GetInstSizeInBytes(UserMI);
1025         Offset < BaseInsertOffset;
1026         Offset += TII->GetInstSizeInBytes(MI),
1027            MI = next(MI)) {
1028      if (CPUIndex < CPUsers.size() && CPUsers[CPUIndex].MI == MI) {
1029        CPUser &U = CPUsers[CPUIndex];
1030        if (!OffsetIsInRange(Offset, EndInsertOffset,
1031                             U.MaxDisp, U.NegOk, U.IsSoImm)) {
1032          BaseInsertOffset -= (isThumb1 ? 2 : 4);
1033          EndInsertOffset  -= (isThumb1 ? 2 : 4);
1034        }
1035        // This is overly conservative, as we don't account for CPEMIs
1036        // being reused within the block, but it doesn't matter much.
1037        EndInsertOffset += CPUsers[CPUIndex].CPEMI->getOperand(2).getImm();
1038        CPUIndex++;
1039      }
1040    }
1041    DOUT << "Split in middle of big block\n";
1042    *NewMBB = SplitBlockBeforeInstr(prior(MI));
1043  }
1044}
1045
1046/// HandleConstantPoolUser - Analyze the specified user, checking to see if it
1047/// is out-of-range.  If so, pick up the constant pool value and move it some
1048/// place in-range.  Return true if we changed any addresses (thus must run
1049/// another pass of branch lengthening), false otherwise.
1050bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction &Fn,
1051                                                unsigned CPUserIndex) {
1052  CPUser &U = CPUsers[CPUserIndex];
1053  MachineInstr *UserMI = U.MI;
1054  MachineInstr *CPEMI  = U.CPEMI;
1055  unsigned CPI = CPEMI->getOperand(1).getIndex();
1056  unsigned Size = CPEMI->getOperand(2).getImm();
1057  MachineBasicBlock *NewMBB;
1058  // Compute this only once, it's expensive.  The 4 or 8 is the value the
1059  // hardware keeps in the PC (2 insns ahead of the reference).
1060  unsigned UserOffset = GetOffsetOf(UserMI) + (isThumb ? 4 : 8);
1061
1062  // See if the current entry is within range, or there is a clone of it
1063  // in range.
1064  int result = LookForExistingCPEntry(U, UserOffset);
1065  if (result==1) return false;
1066  else if (result==2) return true;
1067
1068  // No existing clone of this CPE is within range.
1069  // We will be generating a new clone.  Get a UID for it.
1070  unsigned ID = AFI->createConstPoolEntryUId();
1071
1072  // Look for water where we can place this CPE.  We look for the farthest one
1073  // away that will work.  Forward references only for now (although later
1074  // we might find some that are backwards).
1075
1076  if (!LookForWater(U, UserOffset, &NewMBB)) {
1077    // No water found.
1078    DOUT << "No water found\n";
1079    CreateNewWater(CPUserIndex, UserOffset, &NewMBB);
1080  }
1081
1082  // Okay, we know we can put an island before NewMBB now, do it!
1083  MachineBasicBlock *NewIsland = Fn.CreateMachineBasicBlock();
1084  Fn.insert(NewMBB, NewIsland);
1085
1086  // Update internal data structures to account for the newly inserted MBB.
1087  UpdateForInsertedWaterBlock(NewIsland);
1088
1089  // Decrement the old entry, and remove it if refcount becomes 0.
1090  DecrementOldEntry(CPI, CPEMI);
1091
1092  // Now that we have an island to add the CPE to, clone the original CPE and
1093  // add it to the island.
1094  U.CPEMI = BuildMI(NewIsland, DebugLoc::getUnknownLoc(),
1095                    TII->get(ARM::CONSTPOOL_ENTRY))
1096                .addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
1097  CPEntries[CPI].push_back(CPEntry(U.CPEMI, ID, 1));
1098  NumCPEs++;
1099
1100  BBOffsets[NewIsland->getNumber()] = BBOffsets[NewMBB->getNumber()];
1101  // Compensate for .align 2 in thumb mode.
1102  if (isThumb && BBOffsets[NewIsland->getNumber()]%4 != 0)
1103    Size += 2;
1104  // Increase the size of the island block to account for the new entry.
1105  BBSizes[NewIsland->getNumber()] += Size;
1106  AdjustBBOffsetsAfter(NewIsland, Size);
1107
1108  // Finally, change the CPI in the instruction operand to be ID.
1109  for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
1110    if (UserMI->getOperand(i).isCPI()) {
1111      UserMI->getOperand(i).setIndex(ID);
1112      break;
1113    }
1114
1115  DOUT << "  Moved CPE to #" << ID << " CPI=" << CPI << "\t" << *UserMI;
1116
1117  return true;
1118}
1119
1120/// RemoveDeadCPEMI - Remove a dead constant pool entry instruction. Update
1121/// sizes and offsets of impacted basic blocks.
1122void ARMConstantIslands::RemoveDeadCPEMI(MachineInstr *CPEMI) {
1123  MachineBasicBlock *CPEBB = CPEMI->getParent();
1124  unsigned Size = CPEMI->getOperand(2).getImm();
1125  CPEMI->eraseFromParent();
1126  BBSizes[CPEBB->getNumber()] -= Size;
1127  // All succeeding offsets have the current size value added in, fix this.
1128  if (CPEBB->empty()) {
1129    // In thumb1 mode, the size of island may be padded by two to compensate for
1130    // the alignment requirement.  Then it will now be 2 when the block is
1131    // empty, so fix this.
1132    // All succeeding offsets have the current size value added in, fix this.
1133    if (BBSizes[CPEBB->getNumber()] != 0) {
1134      Size += BBSizes[CPEBB->getNumber()];
1135      BBSizes[CPEBB->getNumber()] = 0;
1136    }
1137  }
1138  AdjustBBOffsetsAfter(CPEBB, -Size);
1139  // An island has only one predecessor BB and one successor BB. Check if
1140  // this BB's predecessor jumps directly to this BB's successor. This
1141  // shouldn't happen currently.
1142  assert(!BBIsJumpedOver(CPEBB) && "How did this happen?");
1143  // FIXME: remove the empty blocks after all the work is done?
1144}
1145
1146/// RemoveUnusedCPEntries - Remove constant pool entries whose refcounts
1147/// are zero.
1148bool ARMConstantIslands::RemoveUnusedCPEntries() {
1149  unsigned MadeChange = false;
1150  for (unsigned i = 0, e = CPEntries.size(); i != e; ++i) {
1151      std::vector<CPEntry> &CPEs = CPEntries[i];
1152      for (unsigned j = 0, ee = CPEs.size(); j != ee; ++j) {
1153        if (CPEs[j].RefCount == 0 && CPEs[j].CPEMI) {
1154          RemoveDeadCPEMI(CPEs[j].CPEMI);
1155          CPEs[j].CPEMI = NULL;
1156          MadeChange = true;
1157        }
1158      }
1159  }
1160  return MadeChange;
1161}
1162
1163/// BBIsInRange - Returns true if the distance between specific MI and
1164/// specific BB can fit in MI's displacement field.
1165bool ARMConstantIslands::BBIsInRange(MachineInstr *MI,MachineBasicBlock *DestBB,
1166                                     unsigned MaxDisp) {
1167  unsigned PCAdj      = isThumb ? 4 : 8;
1168  unsigned BrOffset   = GetOffsetOf(MI) + PCAdj;
1169  unsigned DestOffset = BBOffsets[DestBB->getNumber()];
1170
1171  DOUT << "Branch of destination BB#" << DestBB->getNumber()
1172       << " from BB#" << MI->getParent()->getNumber()
1173       << " max delta=" << MaxDisp
1174       << " from " << GetOffsetOf(MI) << " to " << DestOffset
1175       << " offset " << int(DestOffset-BrOffset) << "\t" << *MI;
1176
1177  if (BrOffset <= DestOffset) {
1178    // Branch before the Dest.
1179    if (DestOffset-BrOffset <= MaxDisp)
1180      return true;
1181  } else {
1182    if (BrOffset-DestOffset <= MaxDisp)
1183      return true;
1184  }
1185  return false;
1186}
1187
1188/// FixUpImmediateBr - Fix up an immediate branch whose destination is too far
1189/// away to fit in its displacement field.
1190bool ARMConstantIslands::FixUpImmediateBr(MachineFunction &Fn, ImmBranch &Br) {
1191  MachineInstr *MI = Br.MI;
1192  MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
1193
1194  // Check to see if the DestBB is already in-range.
1195  if (BBIsInRange(MI, DestBB, Br.MaxDisp))
1196    return false;
1197
1198  if (!Br.isCond)
1199    return FixUpUnconditionalBr(Fn, Br);
1200  return FixUpConditionalBr(Fn, Br);
1201}
1202
1203/// FixUpUnconditionalBr - Fix up an unconditional branch whose destination is
1204/// too far away to fit in its displacement field. If the LR register has been
1205/// spilled in the epilogue, then we can use BL to implement a far jump.
1206/// Otherwise, add an intermediate branch instruction to a branch.
1207bool
1208ARMConstantIslands::FixUpUnconditionalBr(MachineFunction &Fn, ImmBranch &Br) {
1209  MachineInstr *MI = Br.MI;
1210  MachineBasicBlock *MBB = MI->getParent();
1211  assert(isThumb && !isThumb2 && "Expected a Thumb1 function!");
1212
1213  // Use BL to implement far jump.
1214  Br.MaxDisp = (1 << 21) * 2;
1215  MI->setDesc(TII->get(ARM::tBfar));
1216  BBSizes[MBB->getNumber()] += 2;
1217  AdjustBBOffsetsAfter(MBB, 2);
1218  HasFarJump = true;
1219  NumUBrFixed++;
1220
1221  DOUT << "  Changed B to long jump " << *MI;
1222
1223  return true;
1224}
1225
1226/// FixUpConditionalBr - Fix up a conditional branch whose destination is too
1227/// far away to fit in its displacement field. It is converted to an inverse
1228/// conditional branch + an unconditional branch to the destination.
1229bool
1230ARMConstantIslands::FixUpConditionalBr(MachineFunction &Fn, ImmBranch &Br) {
1231  MachineInstr *MI = Br.MI;
1232  MachineBasicBlock *DestBB = MI->getOperand(0).getMBB();
1233
1234  // Add an unconditional branch to the destination and invert the branch
1235  // condition to jump over it:
1236  // blt L1
1237  // =>
1238  // bge L2
1239  // b   L1
1240  // L2:
1241  ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImm();
1242  CC = ARMCC::getOppositeCondition(CC);
1243  unsigned CCReg = MI->getOperand(2).getReg();
1244
1245  // If the branch is at the end of its MBB and that has a fall-through block,
1246  // direct the updated conditional branch to the fall-through block. Otherwise,
1247  // split the MBB before the next instruction.
1248  MachineBasicBlock *MBB = MI->getParent();
1249  MachineInstr *BMI = &MBB->back();
1250  bool NeedSplit = (BMI != MI) || !BBHasFallthrough(MBB);
1251
1252  NumCBrFixed++;
1253  if (BMI != MI) {
1254    if (next(MachineBasicBlock::iterator(MI)) == prior(MBB->end()) &&
1255        BMI->getOpcode() == Br.UncondBr) {
1256      // Last MI in the BB is an unconditional branch. Can we simply invert the
1257      // condition and swap destinations:
1258      // beq L1
1259      // b   L2
1260      // =>
1261      // bne L2
1262      // b   L1
1263      MachineBasicBlock *NewDest = BMI->getOperand(0).getMBB();
1264      if (BBIsInRange(MI, NewDest, Br.MaxDisp)) {
1265        DOUT << "  Invert Bcc condition and swap its destination with " << *BMI;
1266        BMI->getOperand(0).setMBB(DestBB);
1267        MI->getOperand(0).setMBB(NewDest);
1268        MI->getOperand(1).setImm(CC);
1269        return true;
1270      }
1271    }
1272  }
1273
1274  if (NeedSplit) {
1275    SplitBlockBeforeInstr(MI);
1276    // No need for the branch to the next block. We're adding an unconditional
1277    // branch to the destination.
1278    int delta = TII->GetInstSizeInBytes(&MBB->back());
1279    BBSizes[MBB->getNumber()] -= delta;
1280    MachineBasicBlock* SplitBB = next(MachineFunction::iterator(MBB));
1281    AdjustBBOffsetsAfter(SplitBB, -delta);
1282    MBB->back().eraseFromParent();
1283    // BBOffsets[SplitBB] is wrong temporarily, fixed below
1284  }
1285  MachineBasicBlock *NextBB = next(MachineFunction::iterator(MBB));
1286
1287  DOUT << "  Insert B to BB#" << DestBB->getNumber()
1288       << " also invert condition and change dest. to BB#"
1289       << NextBB->getNumber() << "\n";
1290
1291  // Insert a new conditional branch and a new unconditional branch.
1292  // Also update the ImmBranch as well as adding a new entry for the new branch.
1293  BuildMI(MBB, DebugLoc::getUnknownLoc(),
1294          TII->get(MI->getOpcode()))
1295    .addMBB(NextBB).addImm(CC).addReg(CCReg);
1296  Br.MI = &MBB->back();
1297  BBSizes[MBB->getNumber()] += TII->GetInstSizeInBytes(&MBB->back());
1298  BuildMI(MBB, DebugLoc::getUnknownLoc(), TII->get(Br.UncondBr)).addMBB(DestBB);
1299  BBSizes[MBB->getNumber()] += TII->GetInstSizeInBytes(&MBB->back());
1300  unsigned MaxDisp = getUnconditionalBrDisp(Br.UncondBr);
1301  ImmBranches.push_back(ImmBranch(&MBB->back(), MaxDisp, false, Br.UncondBr));
1302
1303  // Remove the old conditional branch.  It may or may not still be in MBB.
1304  BBSizes[MI->getParent()->getNumber()] -= TII->GetInstSizeInBytes(MI);
1305  MI->eraseFromParent();
1306
1307  // The net size change is an addition of one unconditional branch.
1308  int delta = TII->GetInstSizeInBytes(&MBB->back());
1309  AdjustBBOffsetsAfter(MBB, delta);
1310  return true;
1311}
1312
1313/// UndoLRSpillRestore - Remove Thumb push / pop instructions that only spills
1314/// LR / restores LR to pc.
1315bool ARMConstantIslands::UndoLRSpillRestore() {
1316  bool MadeChange = false;
1317  for (unsigned i = 0, e = PushPopMIs.size(); i != e; ++i) {
1318    MachineInstr *MI = PushPopMIs[i];
1319    if (MI->getOpcode() == ARM::tPOP_RET &&
1320        MI->getOperand(0).getReg() == ARM::PC &&
1321        MI->getNumExplicitOperands() == 1) {
1322      BuildMI(MI->getParent(), MI->getDebugLoc(), TII->get(ARM::tBX_RET));
1323      MI->eraseFromParent();
1324      MadeChange = true;
1325    }
1326  }
1327  return MadeChange;
1328}
1329