Doh. Skip JT branches.
[oota-llvm.git] / lib / Target / ARM / ARMConstantIslandPass.cpp
1 //===-- ARMConstantIslandPass.cpp - ARM constant islands --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by Chris Lattner and is distributed under the
6 // University of Illinois Open Source 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 "ARMMachineFunctionInfo.h"
19 #include "ARMInstrInfo.h"
20 #include "llvm/CodeGen/MachineConstantPool.h"
21 #include "llvm/CodeGen/MachineFunctionPass.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineJumpTableInfo.h"
24 #include "llvm/Target/TargetAsmInfo.h"
25 #include "llvm/Target/TargetData.h"
26 #include "llvm/Target/TargetMachine.h"
27 #include "llvm/Support/Compiler.h"
28 #include "llvm/Support/Debug.h"
29 #include "llvm/ADT/STLExtras.h"
30 #include "llvm/ADT/Statistic.h"
31 #include <iostream>
32 using namespace llvm;
33
34 STATISTIC(NumSplit, "Number of uncond branches inserted");
35
36 namespace {
37   /// ARMConstantIslands - Due to limited pc-relative displacements, ARM
38   /// requires constant pool entries to be scattered among the instructions
39   /// inside a function.  To do this, it completely ignores the normal LLVM
40   /// constant pool, instead, it places constants where-ever it feels like with
41   /// special instructions.
42   ///
43   /// The terminology used in this pass includes:
44   ///   Islands - Clumps of constants placed in the function.
45   ///   Water   - Potential places where an island could be formed.
46   ///   CPE     - A constant pool entry that has been placed somewhere, which
47   ///             tracks a list of users.
48   class VISIBILITY_HIDDEN ARMConstantIslands : public MachineFunctionPass {
49     /// NextUID - Assign unique ID's to CPE's.
50     unsigned NextUID;
51     
52     /// BBSizes - The size of each MachineBasicBlock in bytes of code, indexed
53     /// by MBB Number.
54     std::vector<unsigned> BBSizes;
55     
56     /// WaterList - A sorted list of basic blocks where islands could be placed
57     /// (i.e. blocks that don't fall through to the following block, due
58     /// to a return, unreachable, or unconditional branch).
59     std::vector<MachineBasicBlock*> WaterList;
60     
61     /// CPUser - One user of a constant pool, keeping the machine instruction
62     /// pointer, the constant pool being referenced, and the max displacement
63     /// allowed from the instruction to the CP.
64     struct CPUser {
65       MachineInstr *MI;
66       MachineInstr *CPEMI;
67       unsigned MaxDisp;
68       CPUser(MachineInstr *mi, MachineInstr *cpemi, unsigned maxdisp)
69         : MI(mi), CPEMI(cpemi), MaxDisp(maxdisp) {}
70     };
71     
72     /// CPUsers - Keep track of all of the machine instructions that use various
73     /// constant pools and their max displacement.
74     std::vector<CPUser> CPUsers;
75     
76     /// ImmBranch - One per immediate branch, keeping the machine instruction
77     /// pointer, conditional or unconditional, the max displacement,
78     /// and (if isCond is true) the corresponding unconditional branch
79     /// opcode.
80     struct ImmBranch {
81       MachineInstr *MI;
82       bool isCond;
83       int UncondBr;
84       unsigned MaxDisp;
85       ImmBranch(MachineInstr *mi, bool cond, int ubr, unsigned maxdisp)
86         : MI(mi), isCond(cond), UncondBr(ubr), MaxDisp(maxdisp) {}
87     };
88
89     /// Branches - Keep track of all the immediate branche instructions.
90     ///
91     std::vector<ImmBranch> ImmBranches;
92
93     const TargetInstrInfo *TII;
94     const TargetAsmInfo   *TAI;
95   public:
96     virtual bool runOnMachineFunction(MachineFunction &Fn);
97
98     virtual const char *getPassName() const {
99       return "ARM constant island placement and branch shortening pass";
100     }
101     
102   private:
103     void DoInitialPlacement(MachineFunction &Fn,
104                             std::vector<MachineInstr*> &CPEMIs);
105     void InitialFunctionScan(MachineFunction &Fn,
106                              const std::vector<MachineInstr*> &CPEMIs);
107     void SplitBlockBeforeInstr(MachineInstr *MI);
108     void UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB);
109     bool HandleConstantPoolUser(MachineFunction &Fn, CPUser &U);
110     bool ShortenImmediateBranch(MachineFunction &Fn, ImmBranch &Br);
111
112     unsigned GetInstSize(MachineInstr *MI) const;
113     unsigned GetOffsetOf(MachineInstr *MI) const;
114     unsigned GetOffsetOf(MachineBasicBlock *MBB) const;
115   };
116 }
117
118 /// createARMConstantIslandPass - returns an instance of the constpool
119 /// island pass.
120 FunctionPass *llvm::createARMConstantIslandPass() {
121   return new ARMConstantIslands();
122 }
123
124 bool ARMConstantIslands::runOnMachineFunction(MachineFunction &Fn) {
125   // If there are no constants, there is nothing to do.
126   MachineConstantPool &MCP = *Fn.getConstantPool();
127   if (MCP.isEmpty()) return false;
128   
129   TII = Fn.getTarget().getInstrInfo();
130   TAI = Fn.getTarget().getTargetAsmInfo();
131   
132   // Renumber all of the machine basic blocks in the function, guaranteeing that
133   // the numbers agree with the position of the block in the function.
134   Fn.RenumberBlocks();
135
136   // Perform the initial placement of the constant pool entries.  To start with,
137   // we put them all at the end of the function.
138   std::vector<MachineInstr*> CPEMIs;
139   DoInitialPlacement(Fn, CPEMIs);
140   
141   /// The next UID to take is the first unused one.
142   NextUID = CPEMIs.size();
143   
144   // Do the initial scan of the function, building up information about the
145   // sizes of each block, the location of all the water, and finding all of the
146   // constant pool users.
147   InitialFunctionScan(Fn, CPEMIs);
148   CPEMIs.clear();
149   
150   // Iteratively place constant pool entries until there is no change.
151   bool MadeChange;
152   do {
153     MadeChange = false;
154     for (unsigned i = 0, e = CPUsers.size(); i != e; ++i)
155       MadeChange |= HandleConstantPoolUser(Fn, CPUsers[i]);
156     for (unsigned i = 0, e = ImmBranches.size(); i != e; ++i)
157       MadeChange |= ShortenImmediateBranch(Fn, ImmBranches[i]);
158   } while (MadeChange);
159   
160   BBSizes.clear();
161   WaterList.clear();
162   CPUsers.clear();
163   ImmBranches.clear();
164     
165   return true;
166 }
167
168 /// DoInitialPlacement - Perform the initial placement of the constant pool
169 /// entries.  To start with, we put them all at the end of the function.
170 void ARMConstantIslands::DoInitialPlacement(MachineFunction &Fn,
171                                             std::vector<MachineInstr*> &CPEMIs){
172   // Create the basic block to hold the CPE's.
173   MachineBasicBlock *BB = new MachineBasicBlock();
174   Fn.getBasicBlockList().push_back(BB);
175   
176   // Add all of the constants from the constant pool to the end block, use an
177   // identity mapping of CPI's to CPE's.
178   const std::vector<MachineConstantPoolEntry> &CPs =
179     Fn.getConstantPool()->getConstants();
180   
181   const TargetData &TD = *Fn.getTarget().getTargetData();
182   for (unsigned i = 0, e = CPs.size(); i != e; ++i) {
183     unsigned Size = TD.getTypeSize(CPs[i].getType());
184     // Verify that all constant pool entries are a multiple of 4 bytes.  If not,
185     // we would have to pad them out or something so that instructions stay
186     // aligned.
187     assert((Size & 3) == 0 && "CP Entry not multiple of 4 bytes!");
188     MachineInstr *CPEMI =
189       BuildMI(BB, TII->get(ARM::CONSTPOOL_ENTRY))
190                            .addImm(i).addConstantPoolIndex(i).addImm(Size);
191     CPEMIs.push_back(CPEMI);
192     DEBUG(std::cerr << "Moved CPI#" << i << " to end of function as #"
193                     << i << "\n");
194   }
195 }
196
197 /// BBHasFallthrough - Return true of the specified basic block can fallthrough
198 /// into the block immediately after it.
199 static bool BBHasFallthrough(MachineBasicBlock *MBB) {
200   // Get the next machine basic block in the function.
201   MachineFunction::iterator MBBI = MBB;
202   if (next(MBBI) == MBB->getParent()->end())  // Can't fall off end of function.
203     return false;
204   
205   MachineBasicBlock *NextBB = next(MBBI);
206   for (MachineBasicBlock::succ_iterator I = MBB->succ_begin(),
207        E = MBB->succ_end(); I != E; ++I)
208     if (*I == NextBB)
209       return true;
210   
211   return false;
212 }
213
214 /// InitialFunctionScan - Do the initial scan of the function, building up
215 /// information about the sizes of each block, the location of all the water,
216 /// and finding all of the constant pool users.
217 void ARMConstantIslands::InitialFunctionScan(MachineFunction &Fn,
218                                      const std::vector<MachineInstr*> &CPEMIs) {
219   for (MachineFunction::iterator MBBI = Fn.begin(), E = Fn.end();
220        MBBI != E; ++MBBI) {
221     MachineBasicBlock &MBB = *MBBI;
222     
223     // If this block doesn't fall through into the next MBB, then this is
224     // 'water' that a constant pool island could be placed.
225     if (!BBHasFallthrough(&MBB))
226       WaterList.push_back(&MBB);
227     
228     unsigned MBBSize = 0;
229     for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
230          I != E; ++I) {
231       // Add instruction size to MBBSize.
232       MBBSize += GetInstSize(I);
233
234       int Opc = I->getOpcode();
235       if (TII->isBranch(Opc)) {
236         bool isCond = false;
237         unsigned Bits = 0;
238         unsigned Scale = 1;
239         int UOpc = Opc;
240         switch (Opc) {
241         default:
242           continue;  // Ignore JT branches
243         case ARM::Bcc:
244           isCond = true;
245           UOpc = ARM::B;
246           // Fallthrough
247         case ARM::B:
248           Bits = 24;
249           Scale = 4;
250           break;
251         case ARM::tBcc:
252           isCond = true;
253           UOpc = ARM::tB;
254           Bits = 8;
255           Scale = 2;
256           break;
257         case ARM::tB:
258           Bits = 11;
259           Scale = 2;
260           break;
261         }
262         unsigned MaxDisp = (1 << (Bits-1)) * Scale;
263         ImmBranches.push_back(ImmBranch(I, isCond, UOpc, MaxDisp));
264       }
265
266       // Scan the instructions for constant pool operands.
267       for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
268         if (I->getOperand(op).isConstantPoolIndex()) {
269           // We found one.  The addressing mode tells us the max displacement
270           // from the PC that this instruction permits.
271           unsigned MaxOffs = 0;
272           
273           // Basic size info comes from the TSFlags field.
274           unsigned TSFlags = I->getInstrDescriptor()->TSFlags;
275           switch (TSFlags & ARMII::AddrModeMask) {
276           default: 
277             // Constant pool entries can reach anything.
278             if (I->getOpcode() == ARM::CONSTPOOL_ENTRY)
279               continue;
280             assert(0 && "Unknown addressing mode for CP reference!");
281           case ARMII::AddrMode1: // AM1: 8 bits << 2
282             MaxOffs = 1 << (8+2);   // Taking the address of a CP entry.
283             break;
284           case ARMII::AddrMode2:
285             MaxOffs = 1 << 12;   // +-offset_12
286             break;
287           case ARMII::AddrMode3:
288             MaxOffs = 1 << 8;   // +-offset_8
289             break;
290             // addrmode4 has no immediate offset.
291           case ARMII::AddrMode5:
292             MaxOffs = 1 << (8+2);   // +-(offset_8*4)
293             break;
294           case ARMII::AddrModeT1:
295             MaxOffs = 1 << 5;
296             break;
297           case ARMII::AddrModeT2:
298             MaxOffs = 1 << (5+1);
299             break;
300           case ARMII::AddrModeT4:
301             MaxOffs = 1 << (5+2);
302             break;
303           case ARMII::AddrModeTs:
304             MaxOffs = 1 << (8+2);
305             break;
306           }
307           
308           // Remember that this is a user of a CP entry.
309           MachineInstr *CPEMI =CPEMIs[I->getOperand(op).getConstantPoolIndex()];
310           CPUsers.push_back(CPUser(I, CPEMI, MaxOffs));
311           
312           // Instructions can only use one CP entry, don't bother scanning the
313           // rest of the operands.
314           break;
315         }
316     }
317     BBSizes.push_back(MBBSize);
318   }
319 }
320
321 /// FIXME: Works around a gcc miscompilation with -fstrict-aliasing
322 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
323                                 unsigned JTI) DISABLE_INLINE;
324 static unsigned getNumJTEntries(const std::vector<MachineJumpTableEntry> &JT,
325                                 unsigned JTI) {
326   return JT[JTI].MBBs.size();
327 }
328
329 /// GetInstSize - Return the size of the specified MachineInstr.
330 ///
331 unsigned ARMConstantIslands::GetInstSize(MachineInstr *MI) const {
332   // Basic size info comes from the TSFlags field.
333   unsigned TSFlags = MI->getInstrDescriptor()->TSFlags;
334   
335   switch ((TSFlags & ARMII::SizeMask) >> ARMII::SizeShift) {
336   default:
337     // If this machine instr is an inline asm, measure it.
338     if (MI->getOpcode() == ARM::INLINEASM)
339       return TAI->getInlineAsmLength(MI->getOperand(0).getSymbolName());
340     assert(0 && "Unknown or unset size field for instr!");
341     break;
342   case ARMII::Size8Bytes: return 8;          // Arm instruction x 2.
343   case ARMII::Size4Bytes: return 4;          // Arm instruction.
344   case ARMII::Size2Bytes: return 2;          // Thumb instruction.
345   case ARMII::SizeSpecial: {
346     switch (MI->getOpcode()) {
347     case ARM::CONSTPOOL_ENTRY:
348       // If this machine instr is a constant pool entry, its size is recorded as
349       // operand #2.
350       return MI->getOperand(2).getImm();
351     case ARM::BR_JTr:
352     case ARM::BR_JTm:
353     case ARM::BR_JTadd: {
354       // These are jumptable branches, i.e. a branch followed by an inlined
355       // jumptable. The size is 4 + 4 * number of entries.
356       unsigned JTI = MI->getOperand(MI->getNumOperands()-2).getJumpTableIndex();
357       const MachineFunction *MF = MI->getParent()->getParent();
358       MachineJumpTableInfo *MJTI = MF->getJumpTableInfo();
359       const std::vector<MachineJumpTableEntry> &JT = MJTI->getJumpTables();
360       assert(JTI < JT.size());
361       return getNumJTEntries(JT, JTI) * 4 + 4;
362     }
363     default:
364       // Otherwise, pseudo-instruction sizes are zero.
365       return 0;
366     }
367   }
368   }
369 }
370
371 /// GetOffsetOf - Return the current offset of the specified machine instruction
372 /// from the start of the function.  This offset changes as stuff is moved
373 /// around inside the function.
374 unsigned ARMConstantIslands::GetOffsetOf(MachineInstr *MI) const {
375   MachineBasicBlock *MBB = MI->getParent();
376   
377   // The offset is composed of two things: the sum of the sizes of all MBB's
378   // before this instruction's block, and the offset from the start of the block
379   // it is in.
380   unsigned Offset = 0;
381   
382   // Sum block sizes before MBB.
383   for (unsigned BB = 0, e = MBB->getNumber(); BB != e; ++BB)
384     Offset += BBSizes[BB];
385
386   // Sum instructions before MI in MBB.
387   for (MachineBasicBlock::iterator I = MBB->begin(); ; ++I) {
388     assert(I != MBB->end() && "Didn't find MI in its own basic block?");
389     if (&*I == MI) return Offset;
390     Offset += GetInstSize(I);
391   }
392 }
393
394 /// GetOffsetOf - Return the current offset of the specified machine BB
395 /// from the start of the function.  This offset changes as stuff is moved
396 /// around inside the function.
397 unsigned ARMConstantIslands::GetOffsetOf(MachineBasicBlock *MBB) const {
398   // Sum block sizes before MBB.
399   unsigned Offset = 0;  
400   for (unsigned BB = 0, e = MBB->getNumber(); BB != e; ++BB)
401     Offset += BBSizes[BB];
402
403   return Offset;
404 }
405
406 /// CompareMBBNumbers - Little predicate function to sort the WaterList by MBB
407 /// ID.
408 static bool CompareMBBNumbers(const MachineBasicBlock *LHS,
409                               const MachineBasicBlock *RHS) {
410   return LHS->getNumber() < RHS->getNumber();
411 }
412
413 /// UpdateForInsertedWaterBlock - When a block is newly inserted into the
414 /// machine function, it upsets all of the block numbers.  Renumber the blocks
415 /// and update the arrays that parallel this numbering.
416 void ARMConstantIslands::UpdateForInsertedWaterBlock(MachineBasicBlock *NewBB) {
417   // Renumber the MBB's to keep them consequtive.
418   NewBB->getParent()->RenumberBlocks(NewBB);
419   
420   // Insert a size into BBSizes to align it properly with the (newly
421   // renumbered) block numbers.
422   BBSizes.insert(BBSizes.begin()+NewBB->getNumber(), 0);
423   
424   // Next, update WaterList.  Specifically, we need to add NewMBB as having 
425   // available water after it.
426   std::vector<MachineBasicBlock*>::iterator IP =
427     std::lower_bound(WaterList.begin(), WaterList.end(), NewBB,
428                      CompareMBBNumbers);
429   WaterList.insert(IP, NewBB);
430 }
431
432
433 /// Split the basic block containing MI into two blocks, which are joined by
434 /// an unconditional branch.  Update datastructures and renumber blocks to
435 /// account for this change.
436 void ARMConstantIslands::SplitBlockBeforeInstr(MachineInstr *MI) {
437   MachineBasicBlock *OrigBB = MI->getParent();
438   const ARMFunctionInfo *AFI = OrigBB->getParent()->getInfo<ARMFunctionInfo>();
439   bool isThumb = AFI->isThumbFunction();
440
441   // Create a new MBB for the code after the OrigBB.
442   MachineBasicBlock *NewBB = new MachineBasicBlock(OrigBB->getBasicBlock());
443   MachineFunction::iterator MBBI = OrigBB; ++MBBI;
444   OrigBB->getParent()->getBasicBlockList().insert(MBBI, NewBB);
445   
446   // Splice the instructions starting with MI over to NewBB.
447   NewBB->splice(NewBB->end(), OrigBB, MI, OrigBB->end());
448   
449   // Add an unconditional branch from OrigBB to NewBB.
450   BuildMI(OrigBB, TII->get(isThumb ? ARM::tB : ARM::B)).addMBB(NewBB);
451   NumSplit++;
452   
453   // Update the CFG.  All succs of OrigBB are now succs of NewBB.
454   while (!OrigBB->succ_empty()) {
455     MachineBasicBlock *Succ = *OrigBB->succ_begin();
456     OrigBB->removeSuccessor(Succ);
457     NewBB->addSuccessor(Succ);
458     
459     // This pass should be run after register allocation, so there should be no
460     // PHI nodes to update.
461     assert((Succ->empty() || Succ->begin()->getOpcode() != TargetInstrInfo::PHI)
462            && "PHI nodes should be eliminated by now!");
463   }
464   
465   // OrigBB branches to NewBB.
466   OrigBB->addSuccessor(NewBB);
467   
468   // Update internal data structures to account for the newly inserted MBB.
469   UpdateForInsertedWaterBlock(NewBB);
470   
471   // Figure out how large the first NewMBB is.
472   unsigned NewBBSize = 0;
473   for (MachineBasicBlock::iterator I = NewBB->begin(), E = NewBB->end();
474        I != E; ++I)
475     NewBBSize += GetInstSize(I);
476   
477   // Set the size of NewBB in BBSizes.
478   BBSizes[NewBB->getNumber()] = NewBBSize;
479   
480   // We removed instructions from UserMBB, subtract that off from its size.
481   // Add 2 or 4 to the block to count the unconditional branch we added to it.
482   BBSizes[OrigBB->getNumber()] -= NewBBSize - (isThumb ? 2 : 4);
483 }
484
485 /// HandleConstantPoolUser - Analyze the specified user, checking to see if it
486 /// is out-of-range.  If so, pick it up the constant pool value and move it some
487 /// place in-range.
488 bool ARMConstantIslands::HandleConstantPoolUser(MachineFunction &Fn, CPUser &U){
489   MachineInstr *UserMI = U.MI;
490   MachineInstr *CPEMI  = U.CPEMI;
491
492   unsigned UserOffset = GetOffsetOf(UserMI);
493   unsigned CPEOffset  = GetOffsetOf(CPEMI);
494   
495   DEBUG(std::cerr << "User of CPE#" << CPEMI->getOperand(0).getImm()
496                   << " max delta=" << U.MaxDisp
497                   << " at offset " << int(UserOffset-CPEOffset) << "\t"
498                   << *UserMI);
499
500   // Check to see if the CPE is already in-range.
501   if (UserOffset < CPEOffset) {
502     // User before the CPE.
503     if (CPEOffset-UserOffset <= U.MaxDisp)
504       return false;
505   } else {
506     if (UserOffset-CPEOffset <= U.MaxDisp)
507       return false;
508   }
509   
510  
511   // Solution guaranteed to work: split the user's MBB right before the user and
512   // insert a clone the CPE into the newly created water.
513   
514   // If the user isn't at the start of its MBB, or if there is a fall-through
515   // into the user's MBB, split the MBB before the User.
516   MachineBasicBlock *UserMBB = UserMI->getParent();
517   if (&UserMBB->front() != UserMI ||
518       UserMBB == &Fn.front() || // entry MBB of function.
519       BBHasFallthrough(prior(MachineFunction::iterator(UserMBB)))) {
520     // TODO: Search for the best place to split the code.  In practice, using
521     // loop nesting information to insert these guys outside of loops would be
522     // sufficient.    
523     SplitBlockBeforeInstr(UserMI);
524     
525     // UserMI's BB may have changed.
526     UserMBB = UserMI->getParent();
527   }
528   
529   // Okay, we know we can put an island before UserMBB now, do it!
530   MachineBasicBlock *NewIsland = new MachineBasicBlock();
531   Fn.getBasicBlockList().insert(UserMBB, NewIsland);
532
533   // Update internal data structures to account for the newly inserted MBB.
534   UpdateForInsertedWaterBlock(NewIsland);
535
536   // Now that we have an island to add the CPE to, clone the original CPE and
537   // add it to the island.
538   unsigned ID  = NextUID++;
539   unsigned CPI = CPEMI->getOperand(1).getConstantPoolIndex();
540   unsigned Size = CPEMI->getOperand(2).getImm();
541   
542   // Build a new CPE for this user.
543   U.CPEMI = BuildMI(NewIsland, TII->get(ARM::CONSTPOOL_ENTRY))
544                 .addImm(ID).addConstantPoolIndex(CPI).addImm(Size);
545   
546   // Increase the size of the island block to account for the new entry.
547   BBSizes[NewIsland->getNumber()] += Size;
548   
549   // Finally, change the CPI in the instruction operand to be ID.
550   for (unsigned i = 0, e = UserMI->getNumOperands(); i != e; ++i)
551     if (UserMI->getOperand(i).isConstantPoolIndex()) {
552       UserMI->getOperand(i).setConstantPoolIndex(ID);
553       break;
554     }
555       
556   DEBUG(std::cerr << "  Moved CPE to #" << ID << " CPI=" << CPI << "\t"
557                   << *UserMI);
558   
559       
560   return true;
561 }
562
563 bool
564 ARMConstantIslands::ShortenImmediateBranch(MachineFunction &Fn, ImmBranch &Br) {
565   MachineInstr *MI = Br.MI;
566   MachineBasicBlock *DestBB = MI->getOperand(0).getMachineBasicBlock();
567
568   unsigned BrOffset   = GetOffsetOf(MI);
569   unsigned DestOffset = GetOffsetOf(DestBB);
570
571   // Check to see if the destination BB is in range.
572   if (BrOffset < DestOffset) {
573     if (DestOffset - BrOffset < Br.MaxDisp)
574       return false;
575   } else {
576     if (BrOffset - DestOffset <= Br.MaxDisp)
577       return false;
578   }
579
580   if (!Br.isCond) {
581     // Unconditional branch. We have to insert a branch somewhere to perform
582     // a two level branch (branch to branch). FIXME: not yet implemented.
583     assert(false && "Can't handle unconditional branch yet!");
584     return false;
585   }
586
587   // Otherwise, add a unconditional branch to the destination and 
588   // invert the branch condition to jump over it:
589   // blt L1
590   // =>
591   // bge L2
592   // b   L1
593   // L2:
594   ARMCC::CondCodes CC = (ARMCC::CondCodes)MI->getOperand(1).getImmedValue();
595   CC = ARMCC::getOppositeCondition(CC);
596
597   // If the branch is at the end of its MBB and that has a fall-through block,
598   // direct the updated conditional branch to the fall-through block. Otherwise,
599   // split the MBB before the next instruction.
600   MachineBasicBlock *MBB = MI->getParent();
601   if (&MBB->back() != MI || !BBHasFallthrough(MBB))
602     SplitBlockBeforeInstr(MI);
603   MachineBasicBlock *NextBB = next(MachineFunction::iterator(MBB));
604
605   // Insert a unconditional branch and replace the conditional branch.
606   // Also update the ImmBranch as well as adding a new entry for the new branch.
607   BuildMI(MBB, TII->get(MI->getOpcode())).addMBB(NextBB).addImm((unsigned)CC);
608   Br.MI = &MBB->back();
609   BuildMI(MBB, TII->get(Br.UncondBr)).addMBB(DestBB);
610   unsigned MaxDisp = (Br.UncondBr == ARM::tB) ? (1<<10)*2 : (1<<23)*4;
611   ImmBranches.push_back(ImmBranch(&MBB->back(), false, Br.UncondBr, MaxDisp));
612   MI->eraseFromParent();
613
614   // Increase the size of MBB to account for the new unconditional branch.
615   BBSizes[MBB->getNumber()] += GetInstSize(&MBB->back());
616   return true;
617 }