LLVM API Documentation

MSP430BranchSelector.cpp
Go to the documentation of this file.
00001 //===-- MSP430BranchSelector.cpp - Emit long conditional branches ---------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains a pass that scans a machine function to determine which
00011 // conditional branches need more than 10 bits of displacement to reach their
00012 // target basic block.  It does this in two passes; a calculation of basic block
00013 // positions pass, and a branch pseudo op to machine branch opcode pass.  This
00014 // pass should be run last, just before the assembly printer.
00015 //
00016 //===----------------------------------------------------------------------===//
00017 
00018 #include "MSP430.h"
00019 #include "MSP430InstrInfo.h"
00020 #include "MSP430Subtarget.h"
00021 #include "llvm/ADT/Statistic.h"
00022 #include "llvm/CodeGen/MachineFunctionPass.h"
00023 #include "llvm/CodeGen/MachineInstrBuilder.h"
00024 #include "llvm/Support/MathExtras.h"
00025 #include "llvm/Target/TargetMachine.h"
00026 using namespace llvm;
00027 
00028 #define DEBUG_TYPE "msp430-branch-select"
00029 
00030 STATISTIC(NumExpanded, "Number of branches expanded to long format");
00031 
00032 namespace {
00033   struct MSP430BSel : public MachineFunctionPass {
00034     static char ID;
00035     MSP430BSel() : MachineFunctionPass(ID) {}
00036 
00037     /// BlockSizes - The sizes of the basic blocks in the function.
00038     std::vector<unsigned> BlockSizes;
00039 
00040     bool runOnMachineFunction(MachineFunction &Fn) override;
00041 
00042     const char *getPassName() const override {
00043       return "MSP430 Branch Selector";
00044     }
00045   };
00046   char MSP430BSel::ID = 0;
00047 }
00048 
00049 /// createMSP430BranchSelectionPass - returns an instance of the Branch
00050 /// Selection Pass
00051 ///
00052 FunctionPass *llvm::createMSP430BranchSelectionPass() {
00053   return new MSP430BSel();
00054 }
00055 
00056 bool MSP430BSel::runOnMachineFunction(MachineFunction &Fn) {
00057   const MSP430InstrInfo *TII =
00058       static_cast<const MSP430InstrInfo *>(Fn.getSubtarget().getInstrInfo());
00059   // Give the blocks of the function a dense, in-order, numbering.
00060   Fn.RenumberBlocks();
00061   BlockSizes.resize(Fn.getNumBlockIDs());
00062 
00063   // Measure each MBB and compute a size for the entire function.
00064   unsigned FuncSize = 0;
00065   for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
00066        ++MFI) {
00067     MachineBasicBlock *MBB = MFI;
00068 
00069     unsigned BlockSize = 0;
00070     for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
00071          MBBI != EE; ++MBBI)
00072       BlockSize += TII->GetInstSizeInBytes(MBBI);
00073 
00074     BlockSizes[MBB->getNumber()] = BlockSize;
00075     FuncSize += BlockSize;
00076   }
00077 
00078   // If the entire function is smaller than the displacement of a branch field,
00079   // we know we don't need to shrink any branches in this function.  This is a
00080   // common case.
00081   if (FuncSize < (1 << 9)) {
00082     BlockSizes.clear();
00083     return false;
00084   }
00085 
00086   // For each conditional branch, if the offset to its destination is larger
00087   // than the offset field allows, transform it into a long branch sequence
00088   // like this:
00089   //   short branch:
00090   //     bCC MBB
00091   //   long branch:
00092   //     b!CC $PC+6
00093   //     b MBB
00094   //
00095   bool MadeChange = true;
00096   bool EverMadeChange = false;
00097   while (MadeChange) {
00098     // Iteratively expand branches until we reach a fixed point.
00099     MadeChange = false;
00100 
00101     for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
00102          ++MFI) {
00103       MachineBasicBlock &MBB = *MFI;
00104       unsigned MBBStartOffset = 0;
00105       for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
00106            I != E; ++I) {
00107         if ((I->getOpcode() != MSP430::JCC || I->getOperand(0).isImm()) &&
00108             I->getOpcode() != MSP430::JMP) {
00109           MBBStartOffset += TII->GetInstSizeInBytes(I);
00110           continue;
00111         }
00112 
00113         // Determine the offset from the current branch to the destination
00114         // block.
00115         MachineBasicBlock *Dest = I->getOperand(0).getMBB();
00116 
00117         int BranchSize;
00118         if (Dest->getNumber() <= MBB.getNumber()) {
00119           // If this is a backwards branch, the delta is the offset from the
00120           // start of this block to this branch, plus the sizes of all blocks
00121           // from this block to the dest.
00122           BranchSize = MBBStartOffset;
00123 
00124           for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
00125             BranchSize += BlockSizes[i];
00126         } else {
00127           // Otherwise, add the size of the blocks between this block and the
00128           // dest to the number of bytes left in this block.
00129           BranchSize = -MBBStartOffset;
00130 
00131           for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
00132             BranchSize += BlockSizes[i];
00133         }
00134 
00135         // If this branch is in range, ignore it.
00136         if (isInt<10>(BranchSize)) {
00137           MBBStartOffset += 2;
00138           continue;
00139         }
00140 
00141         // Otherwise, we have to expand it to a long branch.
00142         unsigned NewSize;
00143         MachineInstr *OldBranch = I;
00144         DebugLoc dl = OldBranch->getDebugLoc();
00145 
00146         if (I->getOpcode() == MSP430::JMP) {
00147           NewSize = 4;
00148         } else {
00149           // The BCC operands are:
00150           // 0. MSP430 branch predicate
00151           // 1. Target MBB
00152           SmallVector<MachineOperand, 1> Cond;
00153           Cond.push_back(I->getOperand(1));
00154 
00155           // Jump over the uncond branch inst (i.e. $+6) on opposite condition.
00156           TII->ReverseBranchCondition(Cond);
00157           BuildMI(MBB, I, dl, TII->get(MSP430::JCC))
00158             .addImm(4).addOperand(Cond[0]);
00159 
00160           NewSize = 6;
00161         }
00162         // Uncond branch to the real destination.
00163         I = BuildMI(MBB, I, dl, TII->get(MSP430::Bi)).addMBB(Dest);
00164 
00165         // Remove the old branch from the function.
00166         OldBranch->eraseFromParent();
00167 
00168         // Remember that this instruction is NewSize bytes, increase the size of the
00169         // block by NewSize-2, remember to iterate.
00170         BlockSizes[MBB.getNumber()] += NewSize-2;
00171         MBBStartOffset += NewSize;
00172 
00173         ++NumExpanded;
00174         MadeChange = true;
00175       }
00176     }
00177     EverMadeChange |= MadeChange;
00178   }
00179 
00180   BlockSizes.clear();
00181   return true;
00182 }