LLVM API Documentation
00001 //===-- SparcInstrInfo.cpp - Sparc Instruction Information ----------------===// 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 the Sparc implementation of the TargetInstrInfo class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "SparcInstrInfo.h" 00015 #include "Sparc.h" 00016 #include "SparcMachineFunctionInfo.h" 00017 #include "SparcSubtarget.h" 00018 #include "llvm/ADT/STLExtras.h" 00019 #include "llvm/ADT/SmallVector.h" 00020 #include "llvm/CodeGen/MachineFrameInfo.h" 00021 #include "llvm/CodeGen/MachineInstrBuilder.h" 00022 #include "llvm/CodeGen/MachineMemOperand.h" 00023 #include "llvm/CodeGen/MachineRegisterInfo.h" 00024 #include "llvm/Support/ErrorHandling.h" 00025 #include "llvm/Support/TargetRegistry.h" 00026 00027 using namespace llvm; 00028 00029 #define GET_INSTRINFO_CTOR_DTOR 00030 #include "SparcGenInstrInfo.inc" 00031 00032 // Pin the vtable to this file. 00033 void SparcInstrInfo::anchor() {} 00034 00035 SparcInstrInfo::SparcInstrInfo(SparcSubtarget &ST) 00036 : SparcGenInstrInfo(SP::ADJCALLSTACKDOWN, SP::ADJCALLSTACKUP), 00037 RI(ST), Subtarget(ST) { 00038 } 00039 00040 /// isLoadFromStackSlot - If the specified machine instruction is a direct 00041 /// load from a stack slot, return the virtual or physical register number of 00042 /// the destination along with the FrameIndex of the loaded stack slot. If 00043 /// not, return 0. This predicate must return 0 if the instruction has 00044 /// any side effects other than loading from the stack slot. 00045 unsigned SparcInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, 00046 int &FrameIndex) const { 00047 if (MI->getOpcode() == SP::LDri || 00048 MI->getOpcode() == SP::LDXri || 00049 MI->getOpcode() == SP::LDFri || 00050 MI->getOpcode() == SP::LDDFri || 00051 MI->getOpcode() == SP::LDQFri) { 00052 if (MI->getOperand(1).isFI() && MI->getOperand(2).isImm() && 00053 MI->getOperand(2).getImm() == 0) { 00054 FrameIndex = MI->getOperand(1).getIndex(); 00055 return MI->getOperand(0).getReg(); 00056 } 00057 } 00058 return 0; 00059 } 00060 00061 /// isStoreToStackSlot - If the specified machine instruction is a direct 00062 /// store to a stack slot, return the virtual or physical register number of 00063 /// the source reg along with the FrameIndex of the loaded stack slot. If 00064 /// not, return 0. This predicate must return 0 if the instruction has 00065 /// any side effects other than storing to the stack slot. 00066 unsigned SparcInstrInfo::isStoreToStackSlot(const MachineInstr *MI, 00067 int &FrameIndex) const { 00068 if (MI->getOpcode() == SP::STri || 00069 MI->getOpcode() == SP::STXri || 00070 MI->getOpcode() == SP::STFri || 00071 MI->getOpcode() == SP::STDFri || 00072 MI->getOpcode() == SP::STQFri) { 00073 if (MI->getOperand(0).isFI() && MI->getOperand(1).isImm() && 00074 MI->getOperand(1).getImm() == 0) { 00075 FrameIndex = MI->getOperand(0).getIndex(); 00076 return MI->getOperand(2).getReg(); 00077 } 00078 } 00079 return 0; 00080 } 00081 00082 static bool IsIntegerCC(unsigned CC) 00083 { 00084 return (CC <= SPCC::ICC_VC); 00085 } 00086 00087 00088 static SPCC::CondCodes GetOppositeBranchCondition(SPCC::CondCodes CC) 00089 { 00090 switch(CC) { 00091 case SPCC::ICC_A: return SPCC::ICC_N; 00092 case SPCC::ICC_N: return SPCC::ICC_A; 00093 case SPCC::ICC_NE: return SPCC::ICC_E; 00094 case SPCC::ICC_E: return SPCC::ICC_NE; 00095 case SPCC::ICC_G: return SPCC::ICC_LE; 00096 case SPCC::ICC_LE: return SPCC::ICC_G; 00097 case SPCC::ICC_GE: return SPCC::ICC_L; 00098 case SPCC::ICC_L: return SPCC::ICC_GE; 00099 case SPCC::ICC_GU: return SPCC::ICC_LEU; 00100 case SPCC::ICC_LEU: return SPCC::ICC_GU; 00101 case SPCC::ICC_CC: return SPCC::ICC_CS; 00102 case SPCC::ICC_CS: return SPCC::ICC_CC; 00103 case SPCC::ICC_POS: return SPCC::ICC_NEG; 00104 case SPCC::ICC_NEG: return SPCC::ICC_POS; 00105 case SPCC::ICC_VC: return SPCC::ICC_VS; 00106 case SPCC::ICC_VS: return SPCC::ICC_VC; 00107 00108 case SPCC::FCC_A: return SPCC::FCC_N; 00109 case SPCC::FCC_N: return SPCC::FCC_A; 00110 case SPCC::FCC_U: return SPCC::FCC_O; 00111 case SPCC::FCC_O: return SPCC::FCC_U; 00112 case SPCC::FCC_G: return SPCC::FCC_ULE; 00113 case SPCC::FCC_LE: return SPCC::FCC_UG; 00114 case SPCC::FCC_UG: return SPCC::FCC_LE; 00115 case SPCC::FCC_ULE: return SPCC::FCC_G; 00116 case SPCC::FCC_L: return SPCC::FCC_UGE; 00117 case SPCC::FCC_GE: return SPCC::FCC_UL; 00118 case SPCC::FCC_UL: return SPCC::FCC_GE; 00119 case SPCC::FCC_UGE: return SPCC::FCC_L; 00120 case SPCC::FCC_LG: return SPCC::FCC_UE; 00121 case SPCC::FCC_UE: return SPCC::FCC_LG; 00122 case SPCC::FCC_NE: return SPCC::FCC_E; 00123 case SPCC::FCC_E: return SPCC::FCC_NE; 00124 } 00125 llvm_unreachable("Invalid cond code"); 00126 } 00127 00128 bool SparcInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB, 00129 MachineBasicBlock *&TBB, 00130 MachineBasicBlock *&FBB, 00131 SmallVectorImpl<MachineOperand> &Cond, 00132 bool AllowModify) const 00133 { 00134 00135 MachineBasicBlock::iterator I = MBB.end(); 00136 MachineBasicBlock::iterator UnCondBrIter = MBB.end(); 00137 while (I != MBB.begin()) { 00138 --I; 00139 00140 if (I->isDebugValue()) 00141 continue; 00142 00143 // When we see a non-terminator, we are done. 00144 if (!isUnpredicatedTerminator(I)) 00145 break; 00146 00147 // Terminator is not a branch. 00148 if (!I->isBranch()) 00149 return true; 00150 00151 // Handle Unconditional branches. 00152 if (I->getOpcode() == SP::BA) { 00153 UnCondBrIter = I; 00154 00155 if (!AllowModify) { 00156 TBB = I->getOperand(0).getMBB(); 00157 continue; 00158 } 00159 00160 while (std::next(I) != MBB.end()) 00161 std::next(I)->eraseFromParent(); 00162 00163 Cond.clear(); 00164 FBB = nullptr; 00165 00166 if (MBB.isLayoutSuccessor(I->getOperand(0).getMBB())) { 00167 TBB = nullptr; 00168 I->eraseFromParent(); 00169 I = MBB.end(); 00170 UnCondBrIter = MBB.end(); 00171 continue; 00172 } 00173 00174 TBB = I->getOperand(0).getMBB(); 00175 continue; 00176 } 00177 00178 unsigned Opcode = I->getOpcode(); 00179 if (Opcode != SP::BCOND && Opcode != SP::FBCOND) 00180 return true; // Unknown Opcode. 00181 00182 SPCC::CondCodes BranchCode = (SPCC::CondCodes)I->getOperand(1).getImm(); 00183 00184 if (Cond.empty()) { 00185 MachineBasicBlock *TargetBB = I->getOperand(0).getMBB(); 00186 if (AllowModify && UnCondBrIter != MBB.end() && 00187 MBB.isLayoutSuccessor(TargetBB)) { 00188 00189 // Transform the code 00190 // 00191 // brCC L1 00192 // ba L2 00193 // L1: 00194 // .. 00195 // L2: 00196 // 00197 // into 00198 // 00199 // brnCC L2 00200 // L1: 00201 // ... 00202 // L2: 00203 // 00204 BranchCode = GetOppositeBranchCondition(BranchCode); 00205 MachineBasicBlock::iterator OldInst = I; 00206 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(Opcode)) 00207 .addMBB(UnCondBrIter->getOperand(0).getMBB()).addImm(BranchCode); 00208 BuildMI(MBB, UnCondBrIter, MBB.findDebugLoc(I), get(SP::BA)) 00209 .addMBB(TargetBB); 00210 00211 OldInst->eraseFromParent(); 00212 UnCondBrIter->eraseFromParent(); 00213 00214 UnCondBrIter = MBB.end(); 00215 I = MBB.end(); 00216 continue; 00217 } 00218 FBB = TBB; 00219 TBB = I->getOperand(0).getMBB(); 00220 Cond.push_back(MachineOperand::CreateImm(BranchCode)); 00221 continue; 00222 } 00223 // FIXME: Handle subsequent conditional branches. 00224 // For now, we can't handle multiple conditional branches. 00225 return true; 00226 } 00227 return false; 00228 } 00229 00230 unsigned 00231 SparcInstrInfo::InsertBranch(MachineBasicBlock &MBB,MachineBasicBlock *TBB, 00232 MachineBasicBlock *FBB, 00233 const SmallVectorImpl<MachineOperand> &Cond, 00234 DebugLoc DL) const { 00235 assert(TBB && "InsertBranch must not be told to insert a fallthrough"); 00236 assert((Cond.size() == 1 || Cond.size() == 0) && 00237 "Sparc branch conditions should have one component!"); 00238 00239 if (Cond.empty()) { 00240 assert(!FBB && "Unconditional branch with multiple successors!"); 00241 BuildMI(&MBB, DL, get(SP::BA)).addMBB(TBB); 00242 return 1; 00243 } 00244 00245 // Conditional branch 00246 unsigned CC = Cond[0].getImm(); 00247 00248 if (IsIntegerCC(CC)) 00249 BuildMI(&MBB, DL, get(SP::BCOND)).addMBB(TBB).addImm(CC); 00250 else 00251 BuildMI(&MBB, DL, get(SP::FBCOND)).addMBB(TBB).addImm(CC); 00252 if (!FBB) 00253 return 1; 00254 00255 BuildMI(&MBB, DL, get(SP::BA)).addMBB(FBB); 00256 return 2; 00257 } 00258 00259 unsigned SparcInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const 00260 { 00261 MachineBasicBlock::iterator I = MBB.end(); 00262 unsigned Count = 0; 00263 while (I != MBB.begin()) { 00264 --I; 00265 00266 if (I->isDebugValue()) 00267 continue; 00268 00269 if (I->getOpcode() != SP::BA 00270 && I->getOpcode() != SP::BCOND 00271 && I->getOpcode() != SP::FBCOND) 00272 break; // Not a branch 00273 00274 I->eraseFromParent(); 00275 I = MBB.end(); 00276 ++Count; 00277 } 00278 return Count; 00279 } 00280 00281 void SparcInstrInfo::copyPhysReg(MachineBasicBlock &MBB, 00282 MachineBasicBlock::iterator I, DebugLoc DL, 00283 unsigned DestReg, unsigned SrcReg, 00284 bool KillSrc) const { 00285 unsigned numSubRegs = 0; 00286 unsigned movOpc = 0; 00287 const unsigned *subRegIdx = nullptr; 00288 00289 const unsigned DFP_FP_SubRegsIdx[] = { SP::sub_even, SP::sub_odd }; 00290 const unsigned QFP_DFP_SubRegsIdx[] = { SP::sub_even64, SP::sub_odd64 }; 00291 const unsigned QFP_FP_SubRegsIdx[] = { SP::sub_even, SP::sub_odd, 00292 SP::sub_odd64_then_sub_even, 00293 SP::sub_odd64_then_sub_odd }; 00294 00295 if (SP::IntRegsRegClass.contains(DestReg, SrcReg)) 00296 BuildMI(MBB, I, DL, get(SP::ORrr), DestReg).addReg(SP::G0) 00297 .addReg(SrcReg, getKillRegState(KillSrc)); 00298 else if (SP::FPRegsRegClass.contains(DestReg, SrcReg)) 00299 BuildMI(MBB, I, DL, get(SP::FMOVS), DestReg) 00300 .addReg(SrcReg, getKillRegState(KillSrc)); 00301 else if (SP::DFPRegsRegClass.contains(DestReg, SrcReg)) { 00302 if (Subtarget.isV9()) { 00303 BuildMI(MBB, I, DL, get(SP::FMOVD), DestReg) 00304 .addReg(SrcReg, getKillRegState(KillSrc)); 00305 } else { 00306 // Use two FMOVS instructions. 00307 subRegIdx = DFP_FP_SubRegsIdx; 00308 numSubRegs = 2; 00309 movOpc = SP::FMOVS; 00310 } 00311 } else if (SP::QFPRegsRegClass.contains(DestReg, SrcReg)) { 00312 if (Subtarget.isV9()) { 00313 if (Subtarget.hasHardQuad()) { 00314 BuildMI(MBB, I, DL, get(SP::FMOVQ), DestReg) 00315 .addReg(SrcReg, getKillRegState(KillSrc)); 00316 } else { 00317 // Use two FMOVD instructions. 00318 subRegIdx = QFP_DFP_SubRegsIdx; 00319 numSubRegs = 2; 00320 movOpc = SP::FMOVD; 00321 } 00322 } else { 00323 // Use four FMOVS instructions. 00324 subRegIdx = QFP_FP_SubRegsIdx; 00325 numSubRegs = 4; 00326 movOpc = SP::FMOVS; 00327 } 00328 } else 00329 llvm_unreachable("Impossible reg-to-reg copy"); 00330 00331 if (numSubRegs == 0 || subRegIdx == nullptr || movOpc == 0) 00332 return; 00333 00334 const TargetRegisterInfo *TRI = &getRegisterInfo(); 00335 MachineInstr *MovMI = nullptr; 00336 00337 for (unsigned i = 0; i != numSubRegs; ++i) { 00338 unsigned Dst = TRI->getSubReg(DestReg, subRegIdx[i]); 00339 unsigned Src = TRI->getSubReg(SrcReg, subRegIdx[i]); 00340 assert(Dst && Src && "Bad sub-register"); 00341 00342 MovMI = BuildMI(MBB, I, DL, get(movOpc), Dst).addReg(Src); 00343 } 00344 // Add implicit super-register defs and kills to the last MovMI. 00345 MovMI->addRegisterDefined(DestReg, TRI); 00346 if (KillSrc) 00347 MovMI->addRegisterKilled(SrcReg, TRI); 00348 } 00349 00350 void SparcInstrInfo:: 00351 storeRegToStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, 00352 unsigned SrcReg, bool isKill, int FI, 00353 const TargetRegisterClass *RC, 00354 const TargetRegisterInfo *TRI) const { 00355 DebugLoc DL; 00356 if (I != MBB.end()) DL = I->getDebugLoc(); 00357 00358 MachineFunction *MF = MBB.getParent(); 00359 const MachineFrameInfo &MFI = *MF->getFrameInfo(); 00360 MachineMemOperand *MMO = 00361 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), 00362 MachineMemOperand::MOStore, 00363 MFI.getObjectSize(FI), 00364 MFI.getObjectAlignment(FI)); 00365 00366 // On the order of operands here: think "[FrameIdx + 0] = SrcReg". 00367 if (RC == &SP::I64RegsRegClass) 00368 BuildMI(MBB, I, DL, get(SP::STXri)).addFrameIndex(FI).addImm(0) 00369 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO); 00370 else if (RC == &SP::IntRegsRegClass) 00371 BuildMI(MBB, I, DL, get(SP::STri)).addFrameIndex(FI).addImm(0) 00372 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO); 00373 else if (RC == &SP::FPRegsRegClass) 00374 BuildMI(MBB, I, DL, get(SP::STFri)).addFrameIndex(FI).addImm(0) 00375 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO); 00376 else if (SP::DFPRegsRegClass.hasSubClassEq(RC)) 00377 BuildMI(MBB, I, DL, get(SP::STDFri)).addFrameIndex(FI).addImm(0) 00378 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO); 00379 else if (SP::QFPRegsRegClass.hasSubClassEq(RC)) 00380 // Use STQFri irrespective of its legality. If STQ is not legal, it will be 00381 // lowered into two STDs in eliminateFrameIndex. 00382 BuildMI(MBB, I, DL, get(SP::STQFri)).addFrameIndex(FI).addImm(0) 00383 .addReg(SrcReg, getKillRegState(isKill)).addMemOperand(MMO); 00384 else 00385 llvm_unreachable("Can't store this register to stack slot"); 00386 } 00387 00388 void SparcInstrInfo:: 00389 loadRegFromStackSlot(MachineBasicBlock &MBB, MachineBasicBlock::iterator I, 00390 unsigned DestReg, int FI, 00391 const TargetRegisterClass *RC, 00392 const TargetRegisterInfo *TRI) const { 00393 DebugLoc DL; 00394 if (I != MBB.end()) DL = I->getDebugLoc(); 00395 00396 MachineFunction *MF = MBB.getParent(); 00397 const MachineFrameInfo &MFI = *MF->getFrameInfo(); 00398 MachineMemOperand *MMO = 00399 MF->getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), 00400 MachineMemOperand::MOLoad, 00401 MFI.getObjectSize(FI), 00402 MFI.getObjectAlignment(FI)); 00403 00404 if (RC == &SP::I64RegsRegClass) 00405 BuildMI(MBB, I, DL, get(SP::LDXri), DestReg).addFrameIndex(FI).addImm(0) 00406 .addMemOperand(MMO); 00407 else if (RC == &SP::IntRegsRegClass) 00408 BuildMI(MBB, I, DL, get(SP::LDri), DestReg).addFrameIndex(FI).addImm(0) 00409 .addMemOperand(MMO); 00410 else if (RC == &SP::FPRegsRegClass) 00411 BuildMI(MBB, I, DL, get(SP::LDFri), DestReg).addFrameIndex(FI).addImm(0) 00412 .addMemOperand(MMO); 00413 else if (SP::DFPRegsRegClass.hasSubClassEq(RC)) 00414 BuildMI(MBB, I, DL, get(SP::LDDFri), DestReg).addFrameIndex(FI).addImm(0) 00415 .addMemOperand(MMO); 00416 else if (SP::QFPRegsRegClass.hasSubClassEq(RC)) 00417 // Use LDQFri irrespective of its legality. If LDQ is not legal, it will be 00418 // lowered into two LDDs in eliminateFrameIndex. 00419 BuildMI(MBB, I, DL, get(SP::LDQFri), DestReg).addFrameIndex(FI).addImm(0) 00420 .addMemOperand(MMO); 00421 else 00422 llvm_unreachable("Can't load this register from stack slot"); 00423 } 00424 00425 unsigned SparcInstrInfo::getGlobalBaseReg(MachineFunction *MF) const 00426 { 00427 SparcMachineFunctionInfo *SparcFI = MF->getInfo<SparcMachineFunctionInfo>(); 00428 unsigned GlobalBaseReg = SparcFI->getGlobalBaseReg(); 00429 if (GlobalBaseReg != 0) 00430 return GlobalBaseReg; 00431 00432 // Insert the set of GlobalBaseReg into the first MBB of the function 00433 MachineBasicBlock &FirstMBB = MF->front(); 00434 MachineBasicBlock::iterator MBBI = FirstMBB.begin(); 00435 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 00436 00437 const TargetRegisterClass *PtrRC = 00438 Subtarget.is64Bit() ? &SP::I64RegsRegClass : &SP::IntRegsRegClass; 00439 GlobalBaseReg = RegInfo.createVirtualRegister(PtrRC); 00440 00441 DebugLoc dl; 00442 00443 BuildMI(FirstMBB, MBBI, dl, get(SP::GETPCX), GlobalBaseReg); 00444 SparcFI->setGlobalBaseReg(GlobalBaseReg); 00445 return GlobalBaseReg; 00446 }