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