LLVM API Documentation
00001 //===- AlphaInstrInfo.cpp - Alpha Instruction Information -------*- 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 the Alpha implementation of the TargetInstrInfo class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "Alpha.h" 00015 #include "AlphaInstrInfo.h" 00016 #include "AlphaMachineFunctionInfo.h" 00017 #include "AlphaGenInstrInfo.inc" 00018 #include "llvm/CodeGen/MachineRegisterInfo.h" 00019 #include "llvm/ADT/STLExtras.h" 00020 #include "llvm/ADT/SmallVector.h" 00021 #include "llvm/CodeGen/MachineInstrBuilder.h" 00022 #include "llvm/Support/ErrorHandling.h" 00023 using namespace llvm; 00024 00025 AlphaInstrInfo::AlphaInstrInfo() 00026 : TargetInstrInfoImpl(AlphaInsts, array_lengthof(AlphaInsts)), 00027 RI(*this) { } 00028 00029 00030 bool AlphaInstrInfo::isMoveInstr(const MachineInstr& MI, 00031 unsigned& sourceReg, unsigned& destReg, 00032 unsigned& SrcSR, unsigned& DstSR) const { 00033 unsigned oc = MI.getOpcode(); 00034 if (oc == Alpha::BISr || 00035 oc == Alpha::CPYSS || 00036 oc == Alpha::CPYST || 00037 oc == Alpha::CPYSSt || 00038 oc == Alpha::CPYSTs) { 00039 // or r1, r2, r2 00040 // cpys(s|t) r1 r2 r2 00041 assert(MI.getNumOperands() >= 3 && 00042 MI.getOperand(0).isReg() && 00043 MI.getOperand(1).isReg() && 00044 MI.getOperand(2).isReg() && 00045 "invalid Alpha BIS instruction!"); 00046 if (MI.getOperand(1).getReg() == MI.getOperand(2).getReg()) { 00047 sourceReg = MI.getOperand(1).getReg(); 00048 destReg = MI.getOperand(0).getReg(); 00049 SrcSR = DstSR = 0; 00050 return true; 00051 } 00052 } 00053 return false; 00054 } 00055 00056 unsigned 00057 AlphaInstrInfo::isLoadFromStackSlot(const MachineInstr *MI, 00058 int &FrameIndex) const { 00059 switch (MI->getOpcode()) { 00060 case Alpha::LDL: 00061 case Alpha::LDQ: 00062 case Alpha::LDBU: 00063 case Alpha::LDWU: 00064 case Alpha::LDS: 00065 case Alpha::LDT: 00066 if (MI->getOperand(1).isFI()) { 00067 FrameIndex = MI->getOperand(1).getIndex(); 00068 return MI->getOperand(0).getReg(); 00069 } 00070 break; 00071 } 00072 return 0; 00073 } 00074 00075 unsigned 00076 AlphaInstrInfo::isStoreToStackSlot(const MachineInstr *MI, 00077 int &FrameIndex) const { 00078 switch (MI->getOpcode()) { 00079 case Alpha::STL: 00080 case Alpha::STQ: 00081 case Alpha::STB: 00082 case Alpha::STW: 00083 case Alpha::STS: 00084 case Alpha::STT: 00085 if (MI->getOperand(1).isFI()) { 00086 FrameIndex = MI->getOperand(1).getIndex(); 00087 return MI->getOperand(0).getReg(); 00088 } 00089 break; 00090 } 00091 return 0; 00092 } 00093 00094 static bool isAlphaIntCondCode(unsigned Opcode) { 00095 switch (Opcode) { 00096 case Alpha::BEQ: 00097 case Alpha::BNE: 00098 case Alpha::BGE: 00099 case Alpha::BGT: 00100 case Alpha::BLE: 00101 case Alpha::BLT: 00102 case Alpha::BLBC: 00103 case Alpha::BLBS: 00104 return true; 00105 default: 00106 return false; 00107 } 00108 } 00109 00110 unsigned AlphaInstrInfo::InsertBranch(MachineBasicBlock &MBB, 00111 MachineBasicBlock *TBB, 00112 MachineBasicBlock *FBB, 00113 const SmallVectorImpl<MachineOperand> &Cond) const { 00114 // FIXME this should probably have a DebugLoc argument 00115 DebugLoc dl = DebugLoc::getUnknownLoc(); 00116 assert(TBB && "InsertBranch must not be told to insert a fallthrough"); 00117 assert((Cond.size() == 2 || Cond.size() == 0) && 00118 "Alpha branch conditions have two components!"); 00119 00120 // One-way branch. 00121 if (FBB == 0) { 00122 if (Cond.empty()) // Unconditional branch 00123 BuildMI(&MBB, dl, get(Alpha::BR)).addMBB(TBB); 00124 else // Conditional branch 00125 if (isAlphaIntCondCode(Cond[0].getImm())) 00126 BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_I)) 00127 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB); 00128 else 00129 BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_F)) 00130 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB); 00131 return 1; 00132 } 00133 00134 // Two-way Conditional Branch. 00135 if (isAlphaIntCondCode(Cond[0].getImm())) 00136 BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_I)) 00137 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB); 00138 else 00139 BuildMI(&MBB, dl, get(Alpha::COND_BRANCH_F)) 00140 .addImm(Cond[0].getImm()).addReg(Cond[1].getReg()).addMBB(TBB); 00141 BuildMI(&MBB, dl, get(Alpha::BR)).addMBB(FBB); 00142 return 2; 00143 } 00144 00145 bool AlphaInstrInfo::copyRegToReg(MachineBasicBlock &MBB, 00146 MachineBasicBlock::iterator MI, 00147 unsigned DestReg, unsigned SrcReg, 00148 const TargetRegisterClass *DestRC, 00149 const TargetRegisterClass *SrcRC) const { 00150 //cerr << "copyRegToReg " << DestReg << " <- " << SrcReg << "\n"; 00151 if (DestRC != SrcRC) { 00152 // Not yet supported! 00153 return false; 00154 } 00155 00156 DebugLoc DL = DebugLoc::getUnknownLoc(); 00157 if (MI != MBB.end()) DL = MI->getDebugLoc(); 00158 00159 if (DestRC == Alpha::GPRCRegisterClass) { 00160 BuildMI(MBB, MI, DL, get(Alpha::BISr), DestReg) 00161 .addReg(SrcReg) 00162 .addReg(SrcReg); 00163 } else if (DestRC == Alpha::F4RCRegisterClass) { 00164 BuildMI(MBB, MI, DL, get(Alpha::CPYSS), DestReg) 00165 .addReg(SrcReg) 00166 .addReg(SrcReg); 00167 } else if (DestRC == Alpha::F8RCRegisterClass) { 00168 BuildMI(MBB, MI, DL, get(Alpha::CPYST), DestReg) 00169 .addReg(SrcReg) 00170 .addReg(SrcReg); 00171 } else { 00172 // Attempt to copy register that is not GPR or FPR 00173 return false; 00174 } 00175 00176 return true; 00177 } 00178 00179 void 00180 AlphaInstrInfo::storeRegToStackSlot(MachineBasicBlock &MBB, 00181 MachineBasicBlock::iterator MI, 00182 unsigned SrcReg, bool isKill, int FrameIdx, 00183 const TargetRegisterClass *RC) const { 00184 //cerr << "Trying to store " << getPrettyName(SrcReg) << " to " 00185 // << FrameIdx << "\n"; 00186 //BuildMI(MBB, MI, Alpha::WTF, 0).addReg(SrcReg); 00187 00188 DebugLoc DL = DebugLoc::getUnknownLoc(); 00189 if (MI != MBB.end()) DL = MI->getDebugLoc(); 00190 00191 if (RC == Alpha::F4RCRegisterClass) 00192 BuildMI(MBB, MI, DL, get(Alpha::STS)) 00193 .addReg(SrcReg, getKillRegState(isKill)) 00194 .addFrameIndex(FrameIdx).addReg(Alpha::F31); 00195 else if (RC == Alpha::F8RCRegisterClass) 00196 BuildMI(MBB, MI, DL, get(Alpha::STT)) 00197 .addReg(SrcReg, getKillRegState(isKill)) 00198 .addFrameIndex(FrameIdx).addReg(Alpha::F31); 00199 else if (RC == Alpha::GPRCRegisterClass) 00200 BuildMI(MBB, MI, DL, get(Alpha::STQ)) 00201 .addReg(SrcReg, getKillRegState(isKill)) 00202 .addFrameIndex(FrameIdx).addReg(Alpha::F31); 00203 else 00204 llvm_unreachable("Unhandled register class"); 00205 } 00206 00207 void 00208 AlphaInstrInfo::loadRegFromStackSlot(MachineBasicBlock &MBB, 00209 MachineBasicBlock::iterator MI, 00210 unsigned DestReg, int FrameIdx, 00211 const TargetRegisterClass *RC) const { 00212 //cerr << "Trying to load " << getPrettyName(DestReg) << " to " 00213 // << FrameIdx << "\n"; 00214 DebugLoc DL = DebugLoc::getUnknownLoc(); 00215 if (MI != MBB.end()) DL = MI->getDebugLoc(); 00216 00217 if (RC == Alpha::F4RCRegisterClass) 00218 BuildMI(MBB, MI, DL, get(Alpha::LDS), DestReg) 00219 .addFrameIndex(FrameIdx).addReg(Alpha::F31); 00220 else if (RC == Alpha::F8RCRegisterClass) 00221 BuildMI(MBB, MI, DL, get(Alpha::LDT), DestReg) 00222 .addFrameIndex(FrameIdx).addReg(Alpha::F31); 00223 else if (RC == Alpha::GPRCRegisterClass) 00224 BuildMI(MBB, MI, DL, get(Alpha::LDQ), DestReg) 00225 .addFrameIndex(FrameIdx).addReg(Alpha::F31); 00226 else 00227 llvm_unreachable("Unhandled register class"); 00228 } 00229 00230 MachineInstr *AlphaInstrInfo::foldMemoryOperandImpl(MachineFunction &MF, 00231 MachineInstr *MI, 00232 const SmallVectorImpl<unsigned> &Ops, 00233 int FrameIndex) const { 00234 if (Ops.size() != 1) return NULL; 00235 00236 // Make sure this is a reg-reg copy. 00237 unsigned Opc = MI->getOpcode(); 00238 00239 MachineInstr *NewMI = NULL; 00240 switch(Opc) { 00241 default: 00242 break; 00243 case Alpha::BISr: 00244 case Alpha::CPYSS: 00245 case Alpha::CPYST: 00246 if (MI->getOperand(1).getReg() == MI->getOperand(2).getReg()) { 00247 if (Ops[0] == 0) { // move -> store 00248 unsigned InReg = MI->getOperand(1).getReg(); 00249 bool isKill = MI->getOperand(1).isKill(); 00250 bool isUndef = MI->getOperand(1).isUndef(); 00251 Opc = (Opc == Alpha::BISr) ? Alpha::STQ : 00252 ((Opc == Alpha::CPYSS) ? Alpha::STS : Alpha::STT); 00253 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc)) 00254 .addReg(InReg, getKillRegState(isKill) | getUndefRegState(isUndef)) 00255 .addFrameIndex(FrameIndex) 00256 .addReg(Alpha::F31); 00257 } else { // load -> move 00258 unsigned OutReg = MI->getOperand(0).getReg(); 00259 bool isDead = MI->getOperand(0).isDead(); 00260 bool isUndef = MI->getOperand(0).isUndef(); 00261 Opc = (Opc == Alpha::BISr) ? Alpha::LDQ : 00262 ((Opc == Alpha::CPYSS) ? Alpha::LDS : Alpha::LDT); 00263 NewMI = BuildMI(MF, MI->getDebugLoc(), get(Opc)) 00264 .addReg(OutReg, RegState::Define | getDeadRegState(isDead) | 00265 getUndefRegState(isUndef)) 00266 .addFrameIndex(FrameIndex) 00267 .addReg(Alpha::F31); 00268 } 00269 } 00270 break; 00271 } 00272 return NewMI; 00273 } 00274 00275 static unsigned AlphaRevCondCode(unsigned Opcode) { 00276 switch (Opcode) { 00277 case Alpha::BEQ: return Alpha::BNE; 00278 case Alpha::BNE: return Alpha::BEQ; 00279 case Alpha::BGE: return Alpha::BLT; 00280 case Alpha::BGT: return Alpha::BLE; 00281 case Alpha::BLE: return Alpha::BGT; 00282 case Alpha::BLT: return Alpha::BGE; 00283 case Alpha::BLBC: return Alpha::BLBS; 00284 case Alpha::BLBS: return Alpha::BLBC; 00285 case Alpha::FBEQ: return Alpha::FBNE; 00286 case Alpha::FBNE: return Alpha::FBEQ; 00287 case Alpha::FBGE: return Alpha::FBLT; 00288 case Alpha::FBGT: return Alpha::FBLE; 00289 case Alpha::FBLE: return Alpha::FBGT; 00290 case Alpha::FBLT: return Alpha::FBGE; 00291 default: 00292 llvm_unreachable("Unknown opcode"); 00293 } 00294 return 0; // Not reached 00295 } 00296 00297 // Branch analysis. 00298 bool AlphaInstrInfo::AnalyzeBranch(MachineBasicBlock &MBB,MachineBasicBlock *&TBB, 00299 MachineBasicBlock *&FBB, 00300 SmallVectorImpl<MachineOperand> &Cond, 00301 bool AllowModify) const { 00302 // If the block has no terminators, it just falls into the block after it. 00303 MachineBasicBlock::iterator I = MBB.end(); 00304 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) 00305 return false; 00306 00307 // Get the last instruction in the block. 00308 MachineInstr *LastInst = I; 00309 00310 // If there is only one terminator instruction, process it. 00311 if (I == MBB.begin() || !isUnpredicatedTerminator(--I)) { 00312 if (LastInst->getOpcode() == Alpha::BR) { 00313 TBB = LastInst->getOperand(0).getMBB(); 00314 return false; 00315 } else if (LastInst->getOpcode() == Alpha::COND_BRANCH_I || 00316 LastInst->getOpcode() == Alpha::COND_BRANCH_F) { 00317 // Block ends with fall-through condbranch. 00318 TBB = LastInst->getOperand(2).getMBB(); 00319 Cond.push_back(LastInst->getOperand(0)); 00320 Cond.push_back(LastInst->getOperand(1)); 00321 return false; 00322 } 00323 // Otherwise, don't know what this is. 00324 return true; 00325 } 00326 00327 // Get the instruction before it if it's a terminator. 00328 MachineInstr *SecondLastInst = I; 00329 00330 // If there are three terminators, we don't know what sort of block this is. 00331 if (SecondLastInst && I != MBB.begin() && 00332 isUnpredicatedTerminator(--I)) 00333 return true; 00334 00335 // If the block ends with Alpha::BR and Alpha::COND_BRANCH_*, handle it. 00336 if ((SecondLastInst->getOpcode() == Alpha::COND_BRANCH_I || 00337 SecondLastInst->getOpcode() == Alpha::COND_BRANCH_F) && 00338 LastInst->getOpcode() == Alpha::BR) { 00339 TBB = SecondLastInst->getOperand(2).getMBB(); 00340 Cond.push_back(SecondLastInst->getOperand(0)); 00341 Cond.push_back(SecondLastInst->getOperand(1)); 00342 FBB = LastInst->getOperand(0).getMBB(); 00343 return false; 00344 } 00345 00346 // If the block ends with two Alpha::BRs, handle it. The second one is not 00347 // executed, so remove it. 00348 if (SecondLastInst->getOpcode() == Alpha::BR && 00349 LastInst->getOpcode() == Alpha::BR) { 00350 TBB = SecondLastInst->getOperand(0).getMBB(); 00351 I = LastInst; 00352 if (AllowModify) 00353 I->eraseFromParent(); 00354 return false; 00355 } 00356 00357 // Otherwise, can't handle this. 00358 return true; 00359 } 00360 00361 unsigned AlphaInstrInfo::RemoveBranch(MachineBasicBlock &MBB) const { 00362 MachineBasicBlock::iterator I = MBB.end(); 00363 if (I == MBB.begin()) return 0; 00364 --I; 00365 if (I->getOpcode() != Alpha::BR && 00366 I->getOpcode() != Alpha::COND_BRANCH_I && 00367 I->getOpcode() != Alpha::COND_BRANCH_F) 00368 return 0; 00369 00370 // Remove the branch. 00371 I->eraseFromParent(); 00372 00373 I = MBB.end(); 00374 00375 if (I == MBB.begin()) return 1; 00376 --I; 00377 if (I->getOpcode() != Alpha::COND_BRANCH_I && 00378 I->getOpcode() != Alpha::COND_BRANCH_F) 00379 return 1; 00380 00381 // Remove the branch. 00382 I->eraseFromParent(); 00383 return 2; 00384 } 00385 00386 void AlphaInstrInfo::insertNoop(MachineBasicBlock &MBB, 00387 MachineBasicBlock::iterator MI) const { 00388 DebugLoc DL = DebugLoc::getUnknownLoc(); 00389 if (MI != MBB.end()) DL = MI->getDebugLoc(); 00390 BuildMI(MBB, MI, DL, get(Alpha::BISr), Alpha::R31) 00391 .addReg(Alpha::R31) 00392 .addReg(Alpha::R31); 00393 } 00394 00395 bool AlphaInstrInfo::BlockHasNoFallThrough(const MachineBasicBlock &MBB) const { 00396 if (MBB.empty()) return false; 00397 00398 switch (MBB.back().getOpcode()) { 00399 case Alpha::RETDAG: // Return. 00400 case Alpha::RETDAGp: 00401 case Alpha::BR: // Uncond branch. 00402 case Alpha::JMP: // Indirect branch. 00403 return true; 00404 default: return false; 00405 } 00406 } 00407 bool AlphaInstrInfo:: 00408 ReverseBranchCondition(SmallVectorImpl<MachineOperand> &Cond) const { 00409 assert(Cond.size() == 2 && "Invalid Alpha branch opcode!"); 00410 Cond[0].setImm(AlphaRevCondCode(Cond[0].getImm())); 00411 return false; 00412 } 00413 00414 /// getGlobalBaseReg - Return a virtual register initialized with the 00415 /// the global base register value. Output instructions required to 00416 /// initialize the register in the function entry block, if necessary. 00417 /// 00418 unsigned AlphaInstrInfo::getGlobalBaseReg(MachineFunction *MF) const { 00419 AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>(); 00420 unsigned GlobalBaseReg = AlphaFI->getGlobalBaseReg(); 00421 if (GlobalBaseReg != 0) 00422 return GlobalBaseReg; 00423 00424 // Insert the set of GlobalBaseReg into the first MBB of the function 00425 MachineBasicBlock &FirstMBB = MF->front(); 00426 MachineBasicBlock::iterator MBBI = FirstMBB.begin(); 00427 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 00428 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 00429 00430 GlobalBaseReg = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass); 00431 bool Ok = TII->copyRegToReg(FirstMBB, MBBI, GlobalBaseReg, Alpha::R29, 00432 &Alpha::GPRCRegClass, &Alpha::GPRCRegClass); 00433 assert(Ok && "Couldn't assign to global base register!"); 00434 Ok = Ok; // Silence warning when assertions are turned off. 00435 RegInfo.addLiveIn(Alpha::R29); 00436 00437 AlphaFI->setGlobalBaseReg(GlobalBaseReg); 00438 return GlobalBaseReg; 00439 } 00440 00441 /// getGlobalRetAddr - Return a virtual register initialized with the 00442 /// the global base register value. Output instructions required to 00443 /// initialize the register in the function entry block, if necessary. 00444 /// 00445 unsigned AlphaInstrInfo::getGlobalRetAddr(MachineFunction *MF) const { 00446 AlphaMachineFunctionInfo *AlphaFI = MF->getInfo<AlphaMachineFunctionInfo>(); 00447 unsigned GlobalRetAddr = AlphaFI->getGlobalRetAddr(); 00448 if (GlobalRetAddr != 0) 00449 return GlobalRetAddr; 00450 00451 // Insert the set of GlobalRetAddr into the first MBB of the function 00452 MachineBasicBlock &FirstMBB = MF->front(); 00453 MachineBasicBlock::iterator MBBI = FirstMBB.begin(); 00454 MachineRegisterInfo &RegInfo = MF->getRegInfo(); 00455 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 00456 00457 GlobalRetAddr = RegInfo.createVirtualRegister(&Alpha::GPRCRegClass); 00458 bool Ok = TII->copyRegToReg(FirstMBB, MBBI, GlobalRetAddr, Alpha::R26, 00459 &Alpha::GPRCRegClass, &Alpha::GPRCRegClass); 00460 assert(Ok && "Couldn't assign to global return address register!"); 00461 Ok = Ok; // Silence warning when assertions are turned off. 00462 RegInfo.addLiveIn(Alpha::R26); 00463 00464 AlphaFI->setGlobalRetAddr(GlobalRetAddr); 00465 return GlobalRetAddr; 00466 }