LLVM API Documentation

PPCBranchSelector.cpp
Go to the documentation of this file.
00001 //===-- PPCBranchSelector.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 16 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 "PPC.h"
00019 #include "MCTargetDesc/PPCPredicates.h"
00020 #include "PPCInstrBuilder.h"
00021 #include "PPCInstrInfo.h"
00022 #include "llvm/ADT/Statistic.h"
00023 #include "llvm/CodeGen/MachineFunctionPass.h"
00024 #include "llvm/Support/MathExtras.h"
00025 #include "llvm/Target/TargetMachine.h"
00026 #include "llvm/Target/TargetSubtargetInfo.h"
00027 using namespace llvm;
00028 
00029 #define DEBUG_TYPE "ppc-branch-select"
00030 
00031 STATISTIC(NumExpanded, "Number of branches expanded to long format");
00032 
00033 namespace llvm {
00034   void initializePPCBSelPass(PassRegistry&);
00035 }
00036 
00037 namespace {
00038   struct PPCBSel : public MachineFunctionPass {
00039     static char ID;
00040     PPCBSel() : MachineFunctionPass(ID) {
00041       initializePPCBSelPass(*PassRegistry::getPassRegistry());
00042     }
00043 
00044     /// BlockSizes - The sizes of the basic blocks in the function.
00045     std::vector<unsigned> BlockSizes;
00046 
00047     bool runOnMachineFunction(MachineFunction &Fn) override;
00048 
00049     const char *getPassName() const override {
00050       return "PowerPC Branch Selector";
00051     }
00052   };
00053   char PPCBSel::ID = 0;
00054 }
00055 
00056 INITIALIZE_PASS(PPCBSel, "ppc-branch-select", "PowerPC Branch Selector",
00057                 false, false)
00058 
00059 /// createPPCBranchSelectionPass - returns an instance of the Branch Selection
00060 /// Pass
00061 ///
00062 FunctionPass *llvm::createPPCBranchSelectionPass() {
00063   return new PPCBSel();
00064 }
00065 
00066 bool PPCBSel::runOnMachineFunction(MachineFunction &Fn) {
00067   const PPCInstrInfo *TII =
00068       static_cast<const PPCInstrInfo *>(Fn.getSubtarget().getInstrInfo());
00069   // Give the blocks of the function a dense, in-order, numbering.
00070   Fn.RenumberBlocks();
00071   BlockSizes.resize(Fn.getNumBlockIDs());
00072 
00073   // Measure each MBB and compute a size for the entire function.
00074   unsigned FuncSize = 0;
00075   for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
00076        ++MFI) {
00077     MachineBasicBlock *MBB = MFI;
00078 
00079     unsigned BlockSize = 0;
00080     for (MachineBasicBlock::iterator MBBI = MBB->begin(), EE = MBB->end();
00081          MBBI != EE; ++MBBI)
00082       BlockSize += TII->GetInstSizeInBytes(MBBI);
00083     
00084     BlockSizes[MBB->getNumber()] = BlockSize;
00085     FuncSize += BlockSize;
00086   }
00087   
00088   // If the entire function is smaller than the displacement of a branch field,
00089   // we know we don't need to shrink any branches in this function.  This is a
00090   // common case.
00091   if (FuncSize < (1 << 15)) {
00092     BlockSizes.clear();
00093     return false;
00094   }
00095   
00096   // For each conditional branch, if the offset to its destination is larger
00097   // than the offset field allows, transform it into a long branch sequence
00098   // like this:
00099   //   short branch:
00100   //     bCC MBB
00101   //   long branch:
00102   //     b!CC $PC+8
00103   //     b MBB
00104   //
00105   bool MadeChange = true;
00106   bool EverMadeChange = false;
00107   while (MadeChange) {
00108     // Iteratively expand branches until we reach a fixed point.
00109     MadeChange = false;
00110   
00111     for (MachineFunction::iterator MFI = Fn.begin(), E = Fn.end(); MFI != E;
00112          ++MFI) {
00113       MachineBasicBlock &MBB = *MFI;
00114       unsigned MBBStartOffset = 0;
00115       for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
00116            I != E; ++I) {
00117         MachineBasicBlock *Dest = nullptr;
00118         if (I->getOpcode() == PPC::BCC && !I->getOperand(2).isImm())
00119           Dest = I->getOperand(2).getMBB();
00120         else if ((I->getOpcode() == PPC::BC || I->getOpcode() == PPC::BCn) &&
00121                  !I->getOperand(1).isImm())
00122           Dest = I->getOperand(1).getMBB();
00123         else if ((I->getOpcode() == PPC::BDNZ8 || I->getOpcode() == PPC::BDNZ ||
00124                   I->getOpcode() == PPC::BDZ8  || I->getOpcode() == PPC::BDZ) &&
00125                  !I->getOperand(0).isImm())
00126           Dest = I->getOperand(0).getMBB();
00127 
00128         if (!Dest) {
00129           MBBStartOffset += TII->GetInstSizeInBytes(I);
00130           continue;
00131         }
00132         
00133         // Determine the offset from the current branch to the destination
00134         // block.
00135         int BranchSize;
00136         if (Dest->getNumber() <= MBB.getNumber()) {
00137           // If this is a backwards branch, the delta is the offset from the
00138           // start of this block to this branch, plus the sizes of all blocks
00139           // from this block to the dest.
00140           BranchSize = MBBStartOffset;
00141           
00142           for (unsigned i = Dest->getNumber(), e = MBB.getNumber(); i != e; ++i)
00143             BranchSize += BlockSizes[i];
00144         } else {
00145           // Otherwise, add the size of the blocks between this block and the
00146           // dest to the number of bytes left in this block.
00147           BranchSize = -MBBStartOffset;
00148 
00149           for (unsigned i = MBB.getNumber(), e = Dest->getNumber(); i != e; ++i)
00150             BranchSize += BlockSizes[i];
00151         }
00152 
00153         // If this branch is in range, ignore it.
00154         if (isInt<16>(BranchSize)) {
00155           MBBStartOffset += 4;
00156           continue;
00157         }
00158 
00159         // Otherwise, we have to expand it to a long branch.
00160         MachineInstr *OldBranch = I;
00161         DebugLoc dl = OldBranch->getDebugLoc();
00162  
00163         if (I->getOpcode() == PPC::BCC) {
00164           // The BCC operands are:
00165           // 0. PPC branch predicate
00166           // 1. CR register
00167           // 2. Target MBB
00168           PPC::Predicate Pred = (PPC::Predicate)I->getOperand(0).getImm();
00169           unsigned CRReg = I->getOperand(1).getReg();
00170        
00171           // Jump over the uncond branch inst (i.e. $PC+8) on opposite condition.
00172           BuildMI(MBB, I, dl, TII->get(PPC::BCC))
00173             .addImm(PPC::InvertPredicate(Pred)).addReg(CRReg).addImm(2);
00174         } else if (I->getOpcode() == PPC::BC) {
00175           unsigned CRBit = I->getOperand(0).getReg();
00176           BuildMI(MBB, I, dl, TII->get(PPC::BCn)).addReg(CRBit).addImm(2);
00177         } else if (I->getOpcode() == PPC::BCn) {
00178           unsigned CRBit = I->getOperand(0).getReg();
00179           BuildMI(MBB, I, dl, TII->get(PPC::BC)).addReg(CRBit).addImm(2);
00180         } else if (I->getOpcode() == PPC::BDNZ) {
00181           BuildMI(MBB, I, dl, TII->get(PPC::BDZ)).addImm(2);
00182         } else if (I->getOpcode() == PPC::BDNZ8) {
00183           BuildMI(MBB, I, dl, TII->get(PPC::BDZ8)).addImm(2);
00184         } else if (I->getOpcode() == PPC::BDZ) {
00185           BuildMI(MBB, I, dl, TII->get(PPC::BDNZ)).addImm(2);
00186         } else if (I->getOpcode() == PPC::BDZ8) {
00187           BuildMI(MBB, I, dl, TII->get(PPC::BDNZ8)).addImm(2);
00188         } else {
00189            llvm_unreachable("Unhandled branch type!");
00190         }
00191         
00192         // Uncond branch to the real destination.
00193         I = BuildMI(MBB, I, dl, TII->get(PPC::B)).addMBB(Dest);
00194 
00195         // Remove the old branch from the function.
00196         OldBranch->eraseFromParent();
00197         
00198         // Remember that this instruction is 8-bytes, increase the size of the
00199         // block by 4, remember to iterate.
00200         BlockSizes[MBB.getNumber()] += 4;
00201         MBBStartOffset += 8;
00202         ++NumExpanded;
00203         MadeChange = true;
00204       }
00205     }
00206     EverMadeChange |= MadeChange;
00207   }
00208   
00209   BlockSizes.clear();
00210   return true;
00211 }
00212