LLVM API Documentation
00001 //===-- MipsLongBranch.cpp - Emit long 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 pass expands a branch or jump instruction into a long branch if its 00011 // offset is too large to fit into its immediate field. 00012 // 00013 // FIXME: Fix pc-region jump instructions which cross 256MB segment boundaries. 00014 //===----------------------------------------------------------------------===// 00015 00016 #include "Mips.h" 00017 #include "MCTargetDesc/MipsBaseInfo.h" 00018 #include "MCTargetDesc/MipsMCNaCl.h" 00019 #include "MipsMachineFunction.h" 00020 #include "MipsTargetMachine.h" 00021 #include "llvm/ADT/Statistic.h" 00022 #include "llvm/CodeGen/MachineFunctionPass.h" 00023 #include "llvm/CodeGen/MachineInstrBuilder.h" 00024 #include "llvm/IR/Function.h" 00025 #include "llvm/Support/CommandLine.h" 00026 #include "llvm/Support/MathExtras.h" 00027 #include "llvm/Target/TargetInstrInfo.h" 00028 #include "llvm/Target/TargetMachine.h" 00029 #include "llvm/Target/TargetRegisterInfo.h" 00030 00031 using namespace llvm; 00032 00033 #define DEBUG_TYPE "mips-long-branch" 00034 00035 STATISTIC(LongBranches, "Number of long branches."); 00036 00037 static cl::opt<bool> SkipLongBranch( 00038 "skip-mips-long-branch", 00039 cl::init(false), 00040 cl::desc("MIPS: Skip long branch pass."), 00041 cl::Hidden); 00042 00043 static cl::opt<bool> ForceLongBranch( 00044 "force-mips-long-branch", 00045 cl::init(false), 00046 cl::desc("MIPS: Expand all branches to long format."), 00047 cl::Hidden); 00048 00049 namespace { 00050 typedef MachineBasicBlock::iterator Iter; 00051 typedef MachineBasicBlock::reverse_iterator ReverseIter; 00052 00053 struct MBBInfo { 00054 uint64_t Size, Address; 00055 bool HasLongBranch; 00056 MachineInstr *Br; 00057 00058 MBBInfo() : Size(0), HasLongBranch(false), Br(nullptr) {} 00059 }; 00060 00061 class MipsLongBranch : public MachineFunctionPass { 00062 00063 public: 00064 static char ID; 00065 MipsLongBranch(TargetMachine &tm) 00066 : MachineFunctionPass(ID), TM(tm), 00067 IsPIC(TM.getRelocationModel() == Reloc::PIC_), 00068 ABI(TM.getSubtarget<MipsSubtarget>().getTargetABI()), 00069 LongBranchSeqSize(!IsPIC ? 2 : (ABI == MipsSubtarget::N64 ? 10 : 00070 (!TM.getSubtarget<MipsSubtarget>().isTargetNaCl() ? 9 : 10))) {} 00071 00072 const char *getPassName() const override { 00073 return "Mips Long Branch"; 00074 } 00075 00076 bool runOnMachineFunction(MachineFunction &F) override; 00077 00078 private: 00079 void splitMBB(MachineBasicBlock *MBB); 00080 void initMBBInfo(); 00081 int64_t computeOffset(const MachineInstr *Br); 00082 void replaceBranch(MachineBasicBlock &MBB, Iter Br, DebugLoc DL, 00083 MachineBasicBlock *MBBOpnd); 00084 void expandToLongBranch(MBBInfo &Info); 00085 00086 const TargetMachine &TM; 00087 MachineFunction *MF; 00088 SmallVector<MBBInfo, 16> MBBInfos; 00089 bool IsPIC; 00090 unsigned ABI; 00091 unsigned LongBranchSeqSize; 00092 }; 00093 00094 char MipsLongBranch::ID = 0; 00095 } // end of anonymous namespace 00096 00097 /// createMipsLongBranchPass - Returns a pass that converts branches to long 00098 /// branches. 00099 FunctionPass *llvm::createMipsLongBranchPass(MipsTargetMachine &tm) { 00100 return new MipsLongBranch(tm); 00101 } 00102 00103 /// Iterate over list of Br's operands and search for a MachineBasicBlock 00104 /// operand. 00105 static MachineBasicBlock *getTargetMBB(const MachineInstr &Br) { 00106 for (unsigned I = 0, E = Br.getDesc().getNumOperands(); I < E; ++I) { 00107 const MachineOperand &MO = Br.getOperand(I); 00108 00109 if (MO.isMBB()) 00110 return MO.getMBB(); 00111 } 00112 00113 assert(false && "This instruction does not have an MBB operand."); 00114 return nullptr; 00115 } 00116 00117 // Traverse the list of instructions backwards until a non-debug instruction is 00118 // found or it reaches E. 00119 static ReverseIter getNonDebugInstr(ReverseIter B, ReverseIter E) { 00120 for (; B != E; ++B) 00121 if (!B->isDebugValue()) 00122 return B; 00123 00124 return E; 00125 } 00126 00127 // Split MBB if it has two direct jumps/branches. 00128 void MipsLongBranch::splitMBB(MachineBasicBlock *MBB) { 00129 ReverseIter End = MBB->rend(); 00130 ReverseIter LastBr = getNonDebugInstr(MBB->rbegin(), End); 00131 00132 // Return if MBB has no branch instructions. 00133 if ((LastBr == End) || 00134 (!LastBr->isConditionalBranch() && !LastBr->isUnconditionalBranch())) 00135 return; 00136 00137 ReverseIter FirstBr = getNonDebugInstr(std::next(LastBr), End); 00138 00139 // MBB has only one branch instruction if FirstBr is not a branch 00140 // instruction. 00141 if ((FirstBr == End) || 00142 (!FirstBr->isConditionalBranch() && !FirstBr->isUnconditionalBranch())) 00143 return; 00144 00145 assert(!FirstBr->isIndirectBranch() && "Unexpected indirect branch found."); 00146 00147 // Create a new MBB. Move instructions in MBB to the newly created MBB. 00148 MachineBasicBlock *NewMBB = 00149 MF->CreateMachineBasicBlock(MBB->getBasicBlock()); 00150 00151 // Insert NewMBB and fix control flow. 00152 MachineBasicBlock *Tgt = getTargetMBB(*FirstBr); 00153 NewMBB->transferSuccessors(MBB); 00154 NewMBB->removeSuccessor(Tgt); 00155 MBB->addSuccessor(NewMBB); 00156 MBB->addSuccessor(Tgt); 00157 MF->insert(std::next(MachineFunction::iterator(MBB)), NewMBB); 00158 00159 NewMBB->splice(NewMBB->end(), MBB, (++LastBr).base(), MBB->end()); 00160 } 00161 00162 // Fill MBBInfos. 00163 void MipsLongBranch::initMBBInfo() { 00164 // Split the MBBs if they have two branches. Each basic block should have at 00165 // most one branch after this loop is executed. 00166 for (MachineFunction::iterator I = MF->begin(), E = MF->end(); I != E;) 00167 splitMBB(I++); 00168 00169 MF->RenumberBlocks(); 00170 MBBInfos.clear(); 00171 MBBInfos.resize(MF->size()); 00172 00173 const MipsInstrInfo *TII = 00174 static_cast<const MipsInstrInfo *>(TM.getSubtargetImpl()->getInstrInfo()); 00175 for (unsigned I = 0, E = MBBInfos.size(); I < E; ++I) { 00176 MachineBasicBlock *MBB = MF->getBlockNumbered(I); 00177 00178 // Compute size of MBB. 00179 for (MachineBasicBlock::instr_iterator MI = MBB->instr_begin(); 00180 MI != MBB->instr_end(); ++MI) 00181 MBBInfos[I].Size += TII->GetInstSizeInBytes(&*MI); 00182 00183 // Search for MBB's branch instruction. 00184 ReverseIter End = MBB->rend(); 00185 ReverseIter Br = getNonDebugInstr(MBB->rbegin(), End); 00186 00187 if ((Br != End) && !Br->isIndirectBranch() && 00188 (Br->isConditionalBranch() || 00189 (Br->isUnconditionalBranch() && 00190 TM.getRelocationModel() == Reloc::PIC_))) 00191 MBBInfos[I].Br = (++Br).base(); 00192 } 00193 } 00194 00195 // Compute offset of branch in number of bytes. 00196 int64_t MipsLongBranch::computeOffset(const MachineInstr *Br) { 00197 int64_t Offset = 0; 00198 int ThisMBB = Br->getParent()->getNumber(); 00199 int TargetMBB = getTargetMBB(*Br)->getNumber(); 00200 00201 // Compute offset of a forward branch. 00202 if (ThisMBB < TargetMBB) { 00203 for (int N = ThisMBB + 1; N < TargetMBB; ++N) 00204 Offset += MBBInfos[N].Size; 00205 00206 return Offset + 4; 00207 } 00208 00209 // Compute offset of a backward branch. 00210 for (int N = ThisMBB; N >= TargetMBB; --N) 00211 Offset += MBBInfos[N].Size; 00212 00213 return -Offset + 4; 00214 } 00215 00216 // Replace Br with a branch which has the opposite condition code and a 00217 // MachineBasicBlock operand MBBOpnd. 00218 void MipsLongBranch::replaceBranch(MachineBasicBlock &MBB, Iter Br, 00219 DebugLoc DL, MachineBasicBlock *MBBOpnd) { 00220 const MipsInstrInfo *TII = 00221 static_cast<const MipsInstrInfo *>(TM.getSubtargetImpl()->getInstrInfo()); 00222 unsigned NewOpc = TII->getOppositeBranchOpc(Br->getOpcode()); 00223 const MCInstrDesc &NewDesc = TII->get(NewOpc); 00224 00225 MachineInstrBuilder MIB = BuildMI(MBB, Br, DL, NewDesc); 00226 00227 for (unsigned I = 0, E = Br->getDesc().getNumOperands(); I < E; ++I) { 00228 MachineOperand &MO = Br->getOperand(I); 00229 00230 if (!MO.isReg()) { 00231 assert(MO.isMBB() && "MBB operand expected."); 00232 break; 00233 } 00234 00235 MIB.addReg(MO.getReg()); 00236 } 00237 00238 MIB.addMBB(MBBOpnd); 00239 00240 // Bundle the instruction in the delay slot to the newly created branch 00241 // and erase the original branch. 00242 assert(Br->isBundledWithSucc()); 00243 MachineBasicBlock::instr_iterator II(Br); 00244 MIBundleBuilder(&*MIB).append((++II)->removeFromBundle()); 00245 Br->eraseFromParent(); 00246 } 00247 00248 // Expand branch instructions to long branches. 00249 void MipsLongBranch::expandToLongBranch(MBBInfo &I) { 00250 MachineBasicBlock::iterator Pos; 00251 MachineBasicBlock *MBB = I.Br->getParent(), *TgtMBB = getTargetMBB(*I.Br); 00252 DebugLoc DL = I.Br->getDebugLoc(); 00253 const BasicBlock *BB = MBB->getBasicBlock(); 00254 MachineFunction::iterator FallThroughMBB = ++MachineFunction::iterator(MBB); 00255 MachineBasicBlock *LongBrMBB = MF->CreateMachineBasicBlock(BB); 00256 00257 const MipsInstrInfo *TII = 00258 static_cast<const MipsInstrInfo *>(TM.getSubtargetImpl()->getInstrInfo()); 00259 00260 MF->insert(FallThroughMBB, LongBrMBB); 00261 MBB->removeSuccessor(TgtMBB); 00262 MBB->addSuccessor(LongBrMBB); 00263 00264 if (IsPIC) { 00265 MachineBasicBlock *BalTgtMBB = MF->CreateMachineBasicBlock(BB); 00266 MF->insert(FallThroughMBB, BalTgtMBB); 00267 LongBrMBB->addSuccessor(BalTgtMBB); 00268 BalTgtMBB->addSuccessor(TgtMBB); 00269 00270 // We must select between the MIPS32r6/MIPS64r6 BAL (which is a normal 00271 // instruction) and the pre-MIPS32r6/MIPS64r6 definition (which is an 00272 // pseudo-instruction wrapping BGEZAL). 00273 00274 const MipsSubtarget &Subtarget = TM.getSubtarget<MipsSubtarget>(); 00275 unsigned BalOp = Subtarget.hasMips32r6() ? Mips::BAL : Mips::BAL_BR; 00276 00277 if (ABI != MipsSubtarget::N64) { 00278 // $longbr: 00279 // addiu $sp, $sp, -8 00280 // sw $ra, 0($sp) 00281 // lui $at, %hi($tgt - $baltgt) 00282 // bal $baltgt 00283 // addiu $at, $at, %lo($tgt - $baltgt) 00284 // $baltgt: 00285 // addu $at, $ra, $at 00286 // lw $ra, 0($sp) 00287 // jr $at 00288 // addiu $sp, $sp, 8 00289 // $fallthrough: 00290 // 00291 00292 Pos = LongBrMBB->begin(); 00293 00294 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP) 00295 .addReg(Mips::SP).addImm(-8); 00296 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SW)).addReg(Mips::RA) 00297 .addReg(Mips::SP).addImm(0); 00298 00299 // LUi and ADDiu instructions create 32-bit offset of the target basic 00300 // block from the target of BAL instruction. We cannot use immediate 00301 // value for this offset because it cannot be determined accurately when 00302 // the program has inline assembly statements. We therefore use the 00303 // relocation expressions %hi($tgt-$baltgt) and %lo($tgt-$baltgt) which 00304 // are resolved during the fixup, so the values will always be correct. 00305 // 00306 // Since we cannot create %hi($tgt-$baltgt) and %lo($tgt-$baltgt) 00307 // expressions at this point (it is possible only at the MC layer), 00308 // we replace LUi and ADDiu with pseudo instructions 00309 // LONG_BRANCH_LUi and LONG_BRANCH_ADDiu, and add both basic 00310 // blocks as operands to these instructions. When lowering these pseudo 00311 // instructions to LUi and ADDiu in the MC layer, we will create 00312 // %hi($tgt-$baltgt) and %lo($tgt-$baltgt) expressions and add them as 00313 // operands to lowered instructions. 00314 00315 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_LUi), Mips::AT) 00316 .addMBB(TgtMBB).addMBB(BalTgtMBB); 00317 MIBundleBuilder(*LongBrMBB, Pos) 00318 .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB)) 00319 .append(BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_ADDiu), Mips::AT) 00320 .addReg(Mips::AT) 00321 .addMBB(TgtMBB) 00322 .addMBB(BalTgtMBB)); 00323 00324 Pos = BalTgtMBB->begin(); 00325 00326 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDu), Mips::AT) 00327 .addReg(Mips::RA).addReg(Mips::AT); 00328 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LW), Mips::RA) 00329 .addReg(Mips::SP).addImm(0); 00330 00331 if (!TM.getSubtarget<MipsSubtarget>().isTargetNaCl()) { 00332 MIBundleBuilder(*BalTgtMBB, Pos) 00333 .append(BuildMI(*MF, DL, TII->get(Mips::JR)).addReg(Mips::AT)) 00334 .append(BuildMI(*MF, DL, TII->get(Mips::ADDiu), Mips::SP) 00335 .addReg(Mips::SP).addImm(8)); 00336 } else { 00337 // In NaCl, modifying the sp is not allowed in branch delay slot. 00338 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::ADDiu), Mips::SP) 00339 .addReg(Mips::SP).addImm(8); 00340 00341 MIBundleBuilder(*BalTgtMBB, Pos) 00342 .append(BuildMI(*MF, DL, TII->get(Mips::JR)).addReg(Mips::AT)) 00343 .append(BuildMI(*MF, DL, TII->get(Mips::NOP))); 00344 00345 // Bundle-align the target of indirect branch JR. 00346 TgtMBB->setAlignment(MIPS_NACL_BUNDLE_ALIGN); 00347 } 00348 } else { 00349 // $longbr: 00350 // daddiu $sp, $sp, -16 00351 // sd $ra, 0($sp) 00352 // daddiu $at, $zero, %hi($tgt - $baltgt) 00353 // dsll $at, $at, 16 00354 // bal $baltgt 00355 // daddiu $at, $at, %lo($tgt - $baltgt) 00356 // $baltgt: 00357 // daddu $at, $ra, $at 00358 // ld $ra, 0($sp) 00359 // jr64 $at 00360 // daddiu $sp, $sp, 16 00361 // $fallthrough: 00362 // 00363 00364 // We assume the branch is within-function, and that offset is within 00365 // +/- 2GB. High 32 bits will therefore always be zero. 00366 00367 // Note that this will work even if the offset is negative, because 00368 // of the +1 modification that's added in that case. For example, if the 00369 // offset is -1MB (0xFFFFFFFFFFF00000), the computation for %higher is 00370 // 00371 // 0xFFFFFFFFFFF00000 + 0x80008000 = 0x000000007FF08000 00372 // 00373 // and the bits [47:32] are zero. For %highest 00374 // 00375 // 0xFFFFFFFFFFF00000 + 0x800080008000 = 0x000080007FF08000 00376 // 00377 // and the bits [63:48] are zero. 00378 00379 Pos = LongBrMBB->begin(); 00380 00381 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DADDiu), Mips::SP_64) 00382 .addReg(Mips::SP_64).addImm(-16); 00383 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::SD)).addReg(Mips::RA_64) 00384 .addReg(Mips::SP_64).addImm(0); 00385 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::LONG_BRANCH_DADDiu), 00386 Mips::AT_64).addReg(Mips::ZERO_64) 00387 .addMBB(TgtMBB, MipsII::MO_ABS_HI).addMBB(BalTgtMBB); 00388 BuildMI(*LongBrMBB, Pos, DL, TII->get(Mips::DSLL), Mips::AT_64) 00389 .addReg(Mips::AT_64).addImm(16); 00390 00391 MIBundleBuilder(*LongBrMBB, Pos) 00392 .append(BuildMI(*MF, DL, TII->get(BalOp)).addMBB(BalTgtMBB)) 00393 .append( 00394 BuildMI(*MF, DL, TII->get(Mips::LONG_BRANCH_DADDiu), Mips::AT_64) 00395 .addReg(Mips::AT_64) 00396 .addMBB(TgtMBB, MipsII::MO_ABS_LO) 00397 .addMBB(BalTgtMBB)); 00398 00399 Pos = BalTgtMBB->begin(); 00400 00401 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::DADDu), Mips::AT_64) 00402 .addReg(Mips::RA_64).addReg(Mips::AT_64); 00403 BuildMI(*BalTgtMBB, Pos, DL, TII->get(Mips::LD), Mips::RA_64) 00404 .addReg(Mips::SP_64).addImm(0); 00405 00406 MIBundleBuilder(*BalTgtMBB, Pos) 00407 .append(BuildMI(*MF, DL, TII->get(Mips::JR64)).addReg(Mips::AT_64)) 00408 .append(BuildMI(*MF, DL, TII->get(Mips::DADDiu), Mips::SP_64) 00409 .addReg(Mips::SP_64).addImm(16)); 00410 } 00411 00412 assert(LongBrMBB->size() + BalTgtMBB->size() == LongBranchSeqSize); 00413 } else { 00414 // $longbr: 00415 // j $tgt 00416 // nop 00417 // $fallthrough: 00418 // 00419 Pos = LongBrMBB->begin(); 00420 LongBrMBB->addSuccessor(TgtMBB); 00421 MIBundleBuilder(*LongBrMBB, Pos) 00422 .append(BuildMI(*MF, DL, TII->get(Mips::J)).addMBB(TgtMBB)) 00423 .append(BuildMI(*MF, DL, TII->get(Mips::NOP))); 00424 00425 assert(LongBrMBB->size() == LongBranchSeqSize); 00426 } 00427 00428 if (I.Br->isUnconditionalBranch()) { 00429 // Change branch destination. 00430 assert(I.Br->getDesc().getNumOperands() == 1); 00431 I.Br->RemoveOperand(0); 00432 I.Br->addOperand(MachineOperand::CreateMBB(LongBrMBB)); 00433 } else 00434 // Change branch destination and reverse condition. 00435 replaceBranch(*MBB, I.Br, DL, FallThroughMBB); 00436 } 00437 00438 static void emitGPDisp(MachineFunction &F, const MipsInstrInfo *TII) { 00439 MachineBasicBlock &MBB = F.front(); 00440 MachineBasicBlock::iterator I = MBB.begin(); 00441 DebugLoc DL = MBB.findDebugLoc(MBB.begin()); 00442 BuildMI(MBB, I, DL, TII->get(Mips::LUi), Mips::V0) 00443 .addExternalSymbol("_gp_disp", MipsII::MO_ABS_HI); 00444 BuildMI(MBB, I, DL, TII->get(Mips::ADDiu), Mips::V0) 00445 .addReg(Mips::V0).addExternalSymbol("_gp_disp", MipsII::MO_ABS_LO); 00446 MBB.removeLiveIn(Mips::V0); 00447 } 00448 00449 bool MipsLongBranch::runOnMachineFunction(MachineFunction &F) { 00450 const MipsInstrInfo *TII = 00451 static_cast<const MipsInstrInfo *>(TM.getSubtargetImpl()->getInstrInfo()); 00452 00453 const MipsSubtarget &STI = TM.getSubtarget<MipsSubtarget>(); 00454 if (STI.inMips16Mode() || !STI.enableLongBranchPass()) 00455 return false; 00456 if ((TM.getRelocationModel() == Reloc::PIC_) && 00457 TM.getSubtarget<MipsSubtarget>().isABI_O32() && 00458 F.getInfo<MipsFunctionInfo>()->globalBaseRegSet()) 00459 emitGPDisp(F, TII); 00460 00461 if (SkipLongBranch) 00462 return true; 00463 00464 MF = &F; 00465 initMBBInfo(); 00466 00467 SmallVectorImpl<MBBInfo>::iterator I, E = MBBInfos.end(); 00468 bool EverMadeChange = false, MadeChange = true; 00469 00470 while (MadeChange) { 00471 MadeChange = false; 00472 00473 for (I = MBBInfos.begin(); I != E; ++I) { 00474 // Skip if this MBB doesn't have a branch or the branch has already been 00475 // converted to a long branch. 00476 if (!I->Br || I->HasLongBranch) 00477 continue; 00478 00479 int ShVal = TM.getSubtarget<MipsSubtarget>().inMicroMipsMode() ? 2 : 4; 00480 int64_t Offset = computeOffset(I->Br) / ShVal; 00481 00482 if (TM.getSubtarget<MipsSubtarget>().isTargetNaCl()) { 00483 // The offset calculation does not include sandboxing instructions 00484 // that will be added later in the MC layer. Since at this point we 00485 // don't know the exact amount of code that "sandboxing" will add, we 00486 // conservatively estimate that code will not grow more than 100%. 00487 Offset *= 2; 00488 } 00489 00490 // Check if offset fits into 16-bit immediate field of branches. 00491 if (!ForceLongBranch && isInt<16>(Offset)) 00492 continue; 00493 00494 I->HasLongBranch = true; 00495 I->Size += LongBranchSeqSize * 4; 00496 ++LongBranches; 00497 EverMadeChange = MadeChange = true; 00498 } 00499 } 00500 00501 if (!EverMadeChange) 00502 return true; 00503 00504 // Compute basic block addresses. 00505 if (TM.getRelocationModel() == Reloc::PIC_) { 00506 uint64_t Address = 0; 00507 00508 for (I = MBBInfos.begin(); I != E; Address += I->Size, ++I) 00509 I->Address = Address; 00510 } 00511 00512 // Do the expansion. 00513 for (I = MBBInfos.begin(); I != E; ++I) 00514 if (I->HasLongBranch) 00515 expandToLongBranch(*I); 00516 00517 MF->RenumberBlocks(); 00518 00519 return true; 00520 }