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