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