LLVM API Documentation
00001 //===-- X86FixupLEAs.cpp - use or replace LEA instructions -----------===// 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 defines the pass that finds instructions that can be 00011 // re-written as LEA instructions in order to reduce pipeline delays. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "X86.h" 00016 #include "X86InstrInfo.h" 00017 #include "X86Subtarget.h" 00018 #include "llvm/ADT/Statistic.h" 00019 #include "llvm/CodeGen/LiveVariables.h" 00020 #include "llvm/CodeGen/MachineFunctionPass.h" 00021 #include "llvm/CodeGen/MachineInstrBuilder.h" 00022 #include "llvm/CodeGen/MachineRegisterInfo.h" 00023 #include "llvm/CodeGen/Passes.h" 00024 #include "llvm/Support/Debug.h" 00025 #include "llvm/Support/raw_ostream.h" 00026 #include "llvm/Target/TargetInstrInfo.h" 00027 using namespace llvm; 00028 00029 #define DEBUG_TYPE "x86-fixup-LEAs" 00030 00031 STATISTIC(NumLEAs, "Number of LEA instructions created"); 00032 00033 namespace { 00034 class FixupLEAPass : public MachineFunctionPass { 00035 enum RegUsageState { RU_NotUsed, RU_Write, RU_Read }; 00036 static char ID; 00037 /// \brief Loop over all of the instructions in the basic block 00038 /// replacing applicable instructions with LEA instructions, 00039 /// where appropriate. 00040 bool processBasicBlock(MachineFunction &MF, MachineFunction::iterator MFI); 00041 00042 const char *getPassName() const override { return "X86 LEA Fixup"; } 00043 00044 /// \brief Given a machine register, look for the instruction 00045 /// which writes it in the current basic block. If found, 00046 /// try to replace it with an equivalent LEA instruction. 00047 /// If replacement succeeds, then also process the the newly created 00048 /// instruction. 00049 void seekLEAFixup(MachineOperand &p, MachineBasicBlock::iterator &I, 00050 MachineFunction::iterator MFI); 00051 00052 /// \brief Given a memory access or LEA instruction 00053 /// whose address mode uses a base and/or index register, look for 00054 /// an opportunity to replace the instruction which sets the base or index 00055 /// register with an equivalent LEA instruction. 00056 void processInstruction(MachineBasicBlock::iterator &I, 00057 MachineFunction::iterator MFI); 00058 00059 /// \brief Given a LEA instruction which is unprofitable 00060 /// on Silvermont try to replace it with an equivalent ADD instruction 00061 void processInstructionForSLM(MachineBasicBlock::iterator &I, 00062 MachineFunction::iterator MFI); 00063 00064 /// \brief Determine if an instruction references a machine register 00065 /// and, if so, whether it reads or writes the register. 00066 RegUsageState usesRegister(MachineOperand &p, MachineBasicBlock::iterator I); 00067 00068 /// \brief Step backwards through a basic block, looking 00069 /// for an instruction which writes a register within 00070 /// a maximum of INSTR_DISTANCE_THRESHOLD instruction latency cycles. 00071 MachineBasicBlock::iterator searchBackwards(MachineOperand &p, 00072 MachineBasicBlock::iterator &I, 00073 MachineFunction::iterator MFI); 00074 00075 /// \brief if an instruction can be converted to an 00076 /// equivalent LEA, insert the new instruction into the basic block 00077 /// and return a pointer to it. Otherwise, return zero. 00078 MachineInstr *postRAConvertToLEA(MachineFunction::iterator &MFI, 00079 MachineBasicBlock::iterator &MBBI) const; 00080 00081 public: 00082 FixupLEAPass() : MachineFunctionPass(ID) {} 00083 00084 /// \brief Loop over all of the basic blocks, 00085 /// replacing instructions by equivalent LEA instructions 00086 /// if needed and when possible. 00087 bool runOnMachineFunction(MachineFunction &MF) override; 00088 00089 private: 00090 MachineFunction *MF; 00091 const TargetMachine *TM; 00092 const X86InstrInfo *TII; // Machine instruction info. 00093 }; 00094 char FixupLEAPass::ID = 0; 00095 } 00096 00097 MachineInstr * 00098 FixupLEAPass::postRAConvertToLEA(MachineFunction::iterator &MFI, 00099 MachineBasicBlock::iterator &MBBI) const { 00100 MachineInstr *MI = MBBI; 00101 MachineInstr *NewMI; 00102 switch (MI->getOpcode()) { 00103 case X86::MOV32rr: 00104 case X86::MOV64rr: { 00105 const MachineOperand &Src = MI->getOperand(1); 00106 const MachineOperand &Dest = MI->getOperand(0); 00107 NewMI = BuildMI(*MF, MI->getDebugLoc(), 00108 TII->get(MI->getOpcode() == X86::MOV32rr ? X86::LEA32r 00109 : X86::LEA64r)) 00110 .addOperand(Dest) 00111 .addOperand(Src) 00112 .addImm(1) 00113 .addReg(0) 00114 .addImm(0) 00115 .addReg(0); 00116 MFI->insert(MBBI, NewMI); // Insert the new inst 00117 return NewMI; 00118 } 00119 case X86::ADD64ri32: 00120 case X86::ADD64ri8: 00121 case X86::ADD64ri32_DB: 00122 case X86::ADD64ri8_DB: 00123 case X86::ADD32ri: 00124 case X86::ADD32ri8: 00125 case X86::ADD32ri_DB: 00126 case X86::ADD32ri8_DB: 00127 case X86::ADD16ri: 00128 case X86::ADD16ri8: 00129 case X86::ADD16ri_DB: 00130 case X86::ADD16ri8_DB: 00131 if (!MI->getOperand(2).isImm()) { 00132 // convertToThreeAddress will call getImm() 00133 // which requires isImm() to be true 00134 return nullptr; 00135 } 00136 break; 00137 case X86::ADD16rr: 00138 case X86::ADD16rr_DB: 00139 if (MI->getOperand(1).getReg() != MI->getOperand(2).getReg()) { 00140 // if src1 != src2, then convertToThreeAddress will 00141 // need to create a Virtual register, which we cannot do 00142 // after register allocation. 00143 return nullptr; 00144 } 00145 } 00146 return TII->convertToThreeAddress(MFI, MBBI, nullptr); 00147 } 00148 00149 FunctionPass *llvm::createX86FixupLEAs() { return new FixupLEAPass(); } 00150 00151 bool FixupLEAPass::runOnMachineFunction(MachineFunction &Func) { 00152 MF = &Func; 00153 TM = &Func.getTarget(); 00154 const X86Subtarget &ST = TM->getSubtarget<X86Subtarget>(); 00155 if (!ST.LEAusesAG() && !ST.slowLEA()) 00156 return false; 00157 00158 TII = 00159 static_cast<const X86InstrInfo *>(TM->getSubtargetImpl()->getInstrInfo()); 00160 00161 DEBUG(dbgs() << "Start X86FixupLEAs\n";); 00162 // Process all basic blocks. 00163 for (MachineFunction::iterator I = Func.begin(), E = Func.end(); I != E; ++I) 00164 processBasicBlock(Func, I); 00165 DEBUG(dbgs() << "End X86FixupLEAs\n";); 00166 00167 return true; 00168 } 00169 00170 FixupLEAPass::RegUsageState 00171 FixupLEAPass::usesRegister(MachineOperand &p, MachineBasicBlock::iterator I) { 00172 RegUsageState RegUsage = RU_NotUsed; 00173 MachineInstr *MI = I; 00174 00175 for (unsigned int i = 0; i < MI->getNumOperands(); ++i) { 00176 MachineOperand &opnd = MI->getOperand(i); 00177 if (opnd.isReg() && opnd.getReg() == p.getReg()) { 00178 if (opnd.isDef()) 00179 return RU_Write; 00180 RegUsage = RU_Read; 00181 } 00182 } 00183 return RegUsage; 00184 } 00185 00186 /// getPreviousInstr - Given a reference to an instruction in a basic 00187 /// block, return a reference to the previous instruction in the block, 00188 /// wrapping around to the last instruction of the block if the block 00189 /// branches to itself. 00190 static inline bool getPreviousInstr(MachineBasicBlock::iterator &I, 00191 MachineFunction::iterator MFI) { 00192 if (I == MFI->begin()) { 00193 if (MFI->isPredecessor(MFI)) { 00194 I = --MFI->end(); 00195 return true; 00196 } else 00197 return false; 00198 } 00199 --I; 00200 return true; 00201 } 00202 00203 MachineBasicBlock::iterator 00204 FixupLEAPass::searchBackwards(MachineOperand &p, MachineBasicBlock::iterator &I, 00205 MachineFunction::iterator MFI) { 00206 int InstrDistance = 1; 00207 MachineBasicBlock::iterator CurInst; 00208 static const int INSTR_DISTANCE_THRESHOLD = 5; 00209 00210 CurInst = I; 00211 bool Found; 00212 Found = getPreviousInstr(CurInst, MFI); 00213 while (Found && I != CurInst) { 00214 if (CurInst->isCall() || CurInst->isInlineAsm()) 00215 break; 00216 if (InstrDistance > INSTR_DISTANCE_THRESHOLD) 00217 break; // too far back to make a difference 00218 if (usesRegister(p, CurInst) == RU_Write) { 00219 return CurInst; 00220 } 00221 InstrDistance += TII->getInstrLatency( 00222 TM->getSubtargetImpl()->getInstrItineraryData(), CurInst); 00223 Found = getPreviousInstr(CurInst, MFI); 00224 } 00225 return nullptr; 00226 } 00227 00228 void FixupLEAPass::processInstruction(MachineBasicBlock::iterator &I, 00229 MachineFunction::iterator MFI) { 00230 // Process a load, store, or LEA instruction. 00231 MachineInstr *MI = I; 00232 int opcode = MI->getOpcode(); 00233 const MCInstrDesc &Desc = MI->getDesc(); 00234 int AddrOffset = X86II::getMemoryOperandNo(Desc.TSFlags, opcode); 00235 if (AddrOffset >= 0) { 00236 AddrOffset += X86II::getOperandBias(Desc); 00237 MachineOperand &p = MI->getOperand(AddrOffset + X86::AddrBaseReg); 00238 if (p.isReg() && p.getReg() != X86::ESP) { 00239 seekLEAFixup(p, I, MFI); 00240 } 00241 MachineOperand &q = MI->getOperand(AddrOffset + X86::AddrIndexReg); 00242 if (q.isReg() && q.getReg() != X86::ESP) { 00243 seekLEAFixup(q, I, MFI); 00244 } 00245 } 00246 } 00247 00248 void FixupLEAPass::seekLEAFixup(MachineOperand &p, 00249 MachineBasicBlock::iterator &I, 00250 MachineFunction::iterator MFI) { 00251 MachineBasicBlock::iterator MBI = searchBackwards(p, I, MFI); 00252 if (MBI) { 00253 MachineInstr *NewMI = postRAConvertToLEA(MFI, MBI); 00254 if (NewMI) { 00255 ++NumLEAs; 00256 DEBUG(dbgs() << "FixLEA: Candidate to replace:"; MBI->dump();); 00257 // now to replace with an equivalent LEA... 00258 DEBUG(dbgs() << "FixLEA: Replaced by: "; NewMI->dump();); 00259 MFI->erase(MBI); 00260 MachineBasicBlock::iterator J = 00261 static_cast<MachineBasicBlock::iterator>(NewMI); 00262 processInstruction(J, MFI); 00263 } 00264 } 00265 } 00266 00267 void FixupLEAPass::processInstructionForSLM(MachineBasicBlock::iterator &I, 00268 MachineFunction::iterator MFI) { 00269 MachineInstr *MI = I; 00270 const int opcode = MI->getOpcode(); 00271 if (opcode != X86::LEA16r && opcode != X86::LEA32r && opcode != X86::LEA64r && 00272 opcode != X86::LEA64_32r) 00273 return; 00274 if (MI->getOperand(5).getReg() != 0 || !MI->getOperand(4).isImm() || 00275 !TII->isSafeToClobberEFLAGS(*MFI, I)) 00276 return; 00277 const unsigned DstR = MI->getOperand(0).getReg(); 00278 const unsigned SrcR1 = MI->getOperand(1).getReg(); 00279 const unsigned SrcR2 = MI->getOperand(3).getReg(); 00280 if ((SrcR1 == 0 || SrcR1 != DstR) && (SrcR2 == 0 || SrcR2 != DstR)) 00281 return; 00282 if (MI->getOperand(2).getImm() > 1) 00283 return; 00284 int addrr_opcode, addri_opcode; 00285 switch (opcode) { 00286 case X86::LEA16r: 00287 addrr_opcode = X86::ADD16rr; 00288 addri_opcode = X86::ADD16ri; 00289 break; 00290 case X86::LEA32r: 00291 addrr_opcode = X86::ADD32rr; 00292 addri_opcode = X86::ADD32ri; 00293 break; 00294 case X86::LEA64_32r: 00295 case X86::LEA64r: 00296 addrr_opcode = X86::ADD64rr; 00297 addri_opcode = X86::ADD64ri32; 00298 break; 00299 default: 00300 assert(false && "Unexpected LEA instruction"); 00301 } 00302 DEBUG(dbgs() << "FixLEA: Candidate to replace:"; I->dump();); 00303 DEBUG(dbgs() << "FixLEA: Replaced by: ";); 00304 MachineInstr *NewMI = nullptr; 00305 const MachineOperand &Dst = MI->getOperand(0); 00306 // Make ADD instruction for two registers writing to LEA's destination 00307 if (SrcR1 != 0 && SrcR2 != 0) { 00308 const MachineOperand &Src1 = MI->getOperand(SrcR1 == DstR ? 1 : 3); 00309 const MachineOperand &Src2 = MI->getOperand(SrcR1 == DstR ? 3 : 1); 00310 NewMI = BuildMI(*MF, MI->getDebugLoc(), TII->get(addrr_opcode)) 00311 .addOperand(Dst) 00312 .addOperand(Src1) 00313 .addOperand(Src2); 00314 MFI->insert(I, NewMI); 00315 DEBUG(NewMI->dump();); 00316 } 00317 // Make ADD instruction for immediate 00318 if (MI->getOperand(4).getImm() != 0) { 00319 const MachineOperand &SrcR = MI->getOperand(SrcR1 == DstR ? 1 : 3); 00320 NewMI = BuildMI(*MF, MI->getDebugLoc(), TII->get(addri_opcode)) 00321 .addOperand(Dst) 00322 .addOperand(SrcR) 00323 .addImm(MI->getOperand(4).getImm()); 00324 MFI->insert(I, NewMI); 00325 DEBUG(NewMI->dump();); 00326 } 00327 if (NewMI) { 00328 MFI->erase(I); 00329 I = static_cast<MachineBasicBlock::iterator>(NewMI); 00330 } 00331 } 00332 00333 bool FixupLEAPass::processBasicBlock(MachineFunction &MF, 00334 MachineFunction::iterator MFI) { 00335 00336 for (MachineBasicBlock::iterator I = MFI->begin(); I != MFI->end(); ++I) { 00337 if (TM->getSubtarget<X86Subtarget>().isSLM()) 00338 processInstructionForSLM(I, MFI); 00339 else 00340 processInstruction(I, MFI); 00341 } 00342 return false; 00343 }