LLVM API Documentation
00001 //===-- TargetInstrInfo.cpp - Target 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 implements the TargetInstrInfo class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Target/TargetInstrInfo.h" 00015 #include "llvm/CodeGen/MachineFrameInfo.h" 00016 #include "llvm/CodeGen/MachineMemOperand.h" 00017 #include "llvm/CodeGen/MachineRegisterInfo.h" 00018 #include "llvm/CodeGen/PseudoSourceValue.h" 00019 #include "llvm/CodeGen/ScoreboardHazardRecognizer.h" 00020 #include "llvm/MC/MCAsmInfo.h" 00021 #include "llvm/MC/MCInstrItineraries.h" 00022 #include "llvm/Support/CommandLine.h" 00023 #include "llvm/Support/ErrorHandling.h" 00024 #include "llvm/Support/raw_ostream.h" 00025 #include "llvm/Target/TargetLowering.h" 00026 #include "llvm/Target/TargetMachine.h" 00027 #include "llvm/Target/TargetRegisterInfo.h" 00028 #include <cctype> 00029 using namespace llvm; 00030 00031 static cl::opt<bool> DisableHazardRecognizer( 00032 "disable-sched-hazard", cl::Hidden, cl::init(false), 00033 cl::desc("Disable hazard detection during preRA scheduling")); 00034 00035 TargetInstrInfo::~TargetInstrInfo() { 00036 } 00037 00038 const TargetRegisterClass* 00039 TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum, 00040 const TargetRegisterInfo *TRI, 00041 const MachineFunction &MF) const { 00042 if (OpNum >= MCID.getNumOperands()) 00043 return 0; 00044 00045 short RegClass = MCID.OpInfo[OpNum].RegClass; 00046 if (MCID.OpInfo[OpNum].isLookupPtrRegClass()) 00047 return TRI->getPointerRegClass(MF, RegClass); 00048 00049 // Instructions like INSERT_SUBREG do not have fixed register classes. 00050 if (RegClass < 0) 00051 return 0; 00052 00053 // Otherwise just look it up normally. 00054 return TRI->getRegClass(RegClass); 00055 } 00056 00057 /// insertNoop - Insert a noop into the instruction stream at the specified 00058 /// point. 00059 void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB, 00060 MachineBasicBlock::iterator MI) const { 00061 llvm_unreachable("Target didn't implement insertNoop!"); 00062 } 00063 00064 /// Measure the specified inline asm to determine an approximation of its 00065 /// length. 00066 /// Comments (which run till the next SeparatorString or newline) do not 00067 /// count as an instruction. 00068 /// Any other non-whitespace text is considered an instruction, with 00069 /// multiple instructions separated by SeparatorString or newlines. 00070 /// Variable-length instructions are not handled here; this function 00071 /// may be overloaded in the target code to do that. 00072 unsigned TargetInstrInfo::getInlineAsmLength(const char *Str, 00073 const MCAsmInfo &MAI) const { 00074 00075 00076 // Count the number of instructions in the asm. 00077 bool atInsnStart = true; 00078 unsigned Length = 0; 00079 for (; *Str; ++Str) { 00080 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(), 00081 strlen(MAI.getSeparatorString())) == 0) 00082 atInsnStart = true; 00083 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) { 00084 Length += MAI.getMaxInstLength(); 00085 atInsnStart = false; 00086 } 00087 if (atInsnStart && strncmp(Str, MAI.getCommentString(), 00088 strlen(MAI.getCommentString())) == 0) 00089 atInsnStart = false; 00090 } 00091 00092 return Length; 00093 } 00094 00095 /// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything 00096 /// after it, replacing it with an unconditional branch to NewDest. 00097 void 00098 TargetInstrInfo::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail, 00099 MachineBasicBlock *NewDest) const { 00100 MachineBasicBlock *MBB = Tail->getParent(); 00101 00102 // Remove all the old successors of MBB from the CFG. 00103 while (!MBB->succ_empty()) 00104 MBB->removeSuccessor(MBB->succ_begin()); 00105 00106 // Remove all the dead instructions from the end of MBB. 00107 MBB->erase(Tail, MBB->end()); 00108 00109 // If MBB isn't immediately before MBB, insert a branch to it. 00110 if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(NewDest)) 00111 InsertBranch(*MBB, NewDest, 0, SmallVector<MachineOperand, 0>(), 00112 Tail->getDebugLoc()); 00113 MBB->addSuccessor(NewDest); 00114 } 00115 00116 // commuteInstruction - The default implementation of this method just exchanges 00117 // the two operands returned by findCommutedOpIndices. 00118 MachineInstr *TargetInstrInfo::commuteInstruction(MachineInstr *MI, 00119 bool NewMI) const { 00120 const MCInstrDesc &MCID = MI->getDesc(); 00121 bool HasDef = MCID.getNumDefs(); 00122 if (HasDef && !MI->getOperand(0).isReg()) 00123 // No idea how to commute this instruction. Target should implement its own. 00124 return 0; 00125 unsigned Idx1, Idx2; 00126 if (!findCommutedOpIndices(MI, Idx1, Idx2)) { 00127 std::string msg; 00128 raw_string_ostream Msg(msg); 00129 Msg << "Don't know how to commute: " << *MI; 00130 report_fatal_error(Msg.str()); 00131 } 00132 00133 assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() && 00134 "This only knows how to commute register operands so far"); 00135 unsigned Reg0 = HasDef ? MI->getOperand(0).getReg() : 0; 00136 unsigned Reg1 = MI->getOperand(Idx1).getReg(); 00137 unsigned Reg2 = MI->getOperand(Idx2).getReg(); 00138 unsigned SubReg0 = HasDef ? MI->getOperand(0).getSubReg() : 0; 00139 unsigned SubReg1 = MI->getOperand(Idx1).getSubReg(); 00140 unsigned SubReg2 = MI->getOperand(Idx2).getSubReg(); 00141 bool Reg1IsKill = MI->getOperand(Idx1).isKill(); 00142 bool Reg2IsKill = MI->getOperand(Idx2).isKill(); 00143 // If destination is tied to either of the commuted source register, then 00144 // it must be updated. 00145 if (HasDef && Reg0 == Reg1 && 00146 MI->getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO) == 0) { 00147 Reg2IsKill = false; 00148 Reg0 = Reg2; 00149 SubReg0 = SubReg2; 00150 } else if (HasDef && Reg0 == Reg2 && 00151 MI->getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO) == 0) { 00152 Reg1IsKill = false; 00153 Reg0 = Reg1; 00154 SubReg0 = SubReg1; 00155 } 00156 00157 if (NewMI) { 00158 // Create a new instruction. 00159 MachineFunction &MF = *MI->getParent()->getParent(); 00160 MI = MF.CloneMachineInstr(MI); 00161 } 00162 00163 if (HasDef) { 00164 MI->getOperand(0).setReg(Reg0); 00165 MI->getOperand(0).setSubReg(SubReg0); 00166 } 00167 MI->getOperand(Idx2).setReg(Reg1); 00168 MI->getOperand(Idx1).setReg(Reg2); 00169 MI->getOperand(Idx2).setSubReg(SubReg1); 00170 MI->getOperand(Idx1).setSubReg(SubReg2); 00171 MI->getOperand(Idx2).setIsKill(Reg1IsKill); 00172 MI->getOperand(Idx1).setIsKill(Reg2IsKill); 00173 return MI; 00174 } 00175 00176 /// findCommutedOpIndices - If specified MI is commutable, return the two 00177 /// operand indices that would swap value. Return true if the instruction 00178 /// is not in a form which this routine understands. 00179 bool TargetInstrInfo::findCommutedOpIndices(MachineInstr *MI, 00180 unsigned &SrcOpIdx1, 00181 unsigned &SrcOpIdx2) const { 00182 assert(!MI->isBundle() && 00183 "TargetInstrInfo::findCommutedOpIndices() can't handle bundles"); 00184 00185 const MCInstrDesc &MCID = MI->getDesc(); 00186 if (!MCID.isCommutable()) 00187 return false; 00188 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this 00189 // is not true, then the target must implement this. 00190 SrcOpIdx1 = MCID.getNumDefs(); 00191 SrcOpIdx2 = SrcOpIdx1 + 1; 00192 if (!MI->getOperand(SrcOpIdx1).isReg() || 00193 !MI->getOperand(SrcOpIdx2).isReg()) 00194 // No idea. 00195 return false; 00196 return true; 00197 } 00198 00199 00200 bool 00201 TargetInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const { 00202 if (!MI->isTerminator()) return false; 00203 00204 // Conditional branch is a special case. 00205 if (MI->isBranch() && !MI->isBarrier()) 00206 return true; 00207 if (!MI->isPredicable()) 00208 return true; 00209 return !isPredicated(MI); 00210 } 00211 00212 00213 bool TargetInstrInfo::PredicateInstruction(MachineInstr *MI, 00214 const SmallVectorImpl<MachineOperand> &Pred) const { 00215 bool MadeChange = false; 00216 00217 assert(!MI->isBundle() && 00218 "TargetInstrInfo::PredicateInstruction() can't handle bundles"); 00219 00220 const MCInstrDesc &MCID = MI->getDesc(); 00221 if (!MI->isPredicable()) 00222 return false; 00223 00224 for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) { 00225 if (MCID.OpInfo[i].isPredicate()) { 00226 MachineOperand &MO = MI->getOperand(i); 00227 if (MO.isReg()) { 00228 MO.setReg(Pred[j].getReg()); 00229 MadeChange = true; 00230 } else if (MO.isImm()) { 00231 MO.setImm(Pred[j].getImm()); 00232 MadeChange = true; 00233 } else if (MO.isMBB()) { 00234 MO.setMBB(Pred[j].getMBB()); 00235 MadeChange = true; 00236 } 00237 ++j; 00238 } 00239 } 00240 return MadeChange; 00241 } 00242 00243 bool TargetInstrInfo::hasLoadFromStackSlot(const MachineInstr *MI, 00244 const MachineMemOperand *&MMO, 00245 int &FrameIndex) const { 00246 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(), 00247 oe = MI->memoperands_end(); 00248 o != oe; 00249 ++o) { 00250 if ((*o)->isLoad() && (*o)->getValue()) 00251 if (const FixedStackPseudoSourceValue *Value = 00252 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) { 00253 FrameIndex = Value->getFrameIndex(); 00254 MMO = *o; 00255 return true; 00256 } 00257 } 00258 return false; 00259 } 00260 00261 bool TargetInstrInfo::hasStoreToStackSlot(const MachineInstr *MI, 00262 const MachineMemOperand *&MMO, 00263 int &FrameIndex) const { 00264 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(), 00265 oe = MI->memoperands_end(); 00266 o != oe; 00267 ++o) { 00268 if ((*o)->isStore() && (*o)->getValue()) 00269 if (const FixedStackPseudoSourceValue *Value = 00270 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) { 00271 FrameIndex = Value->getFrameIndex(); 00272 MMO = *o; 00273 return true; 00274 } 00275 } 00276 return false; 00277 } 00278 00279 void TargetInstrInfo::reMaterialize(MachineBasicBlock &MBB, 00280 MachineBasicBlock::iterator I, 00281 unsigned DestReg, 00282 unsigned SubIdx, 00283 const MachineInstr *Orig, 00284 const TargetRegisterInfo &TRI) const { 00285 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig); 00286 MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI); 00287 MBB.insert(I, MI); 00288 } 00289 00290 bool 00291 TargetInstrInfo::produceSameValue(const MachineInstr *MI0, 00292 const MachineInstr *MI1, 00293 const MachineRegisterInfo *MRI) const { 00294 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs); 00295 } 00296 00297 MachineInstr *TargetInstrInfo::duplicate(MachineInstr *Orig, 00298 MachineFunction &MF) const { 00299 assert(!Orig->isNotDuplicable() && 00300 "Instruction cannot be duplicated"); 00301 return MF.CloneMachineInstr(Orig); 00302 } 00303 00304 // If the COPY instruction in MI can be folded to a stack operation, return 00305 // the register class to use. 00306 static const TargetRegisterClass *canFoldCopy(const MachineInstr *MI, 00307 unsigned FoldIdx) { 00308 assert(MI->isCopy() && "MI must be a COPY instruction"); 00309 if (MI->getNumOperands() != 2) 00310 return 0; 00311 assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand"); 00312 00313 const MachineOperand &FoldOp = MI->getOperand(FoldIdx); 00314 const MachineOperand &LiveOp = MI->getOperand(1-FoldIdx); 00315 00316 if (FoldOp.getSubReg() || LiveOp.getSubReg()) 00317 return 0; 00318 00319 unsigned FoldReg = FoldOp.getReg(); 00320 unsigned LiveReg = LiveOp.getReg(); 00321 00322 assert(TargetRegisterInfo::isVirtualRegister(FoldReg) && 00323 "Cannot fold physregs"); 00324 00325 const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo(); 00326 const TargetRegisterClass *RC = MRI.getRegClass(FoldReg); 00327 00328 if (TargetRegisterInfo::isPhysicalRegister(LiveOp.getReg())) 00329 return RC->contains(LiveOp.getReg()) ? RC : 0; 00330 00331 if (RC->hasSubClassEq(MRI.getRegClass(LiveReg))) 00332 return RC; 00333 00334 // FIXME: Allow folding when register classes are memory compatible. 00335 return 0; 00336 } 00337 00338 bool TargetInstrInfo:: 00339 canFoldMemoryOperand(const MachineInstr *MI, 00340 const SmallVectorImpl<unsigned> &Ops) const { 00341 return MI->isCopy() && Ops.size() == 1 && canFoldCopy(MI, Ops[0]); 00342 } 00343 00344 /// foldMemoryOperand - Attempt to fold a load or store of the specified stack 00345 /// slot into the specified machine instruction for the specified operand(s). 00346 /// If this is possible, a new instruction is returned with the specified 00347 /// operand folded, otherwise NULL is returned. The client is responsible for 00348 /// removing the old instruction and adding the new one in the instruction 00349 /// stream. 00350 MachineInstr* 00351 TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI, 00352 const SmallVectorImpl<unsigned> &Ops, 00353 int FI) const { 00354 unsigned Flags = 0; 00355 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 00356 if (MI->getOperand(Ops[i]).isDef()) 00357 Flags |= MachineMemOperand::MOStore; 00358 else 00359 Flags |= MachineMemOperand::MOLoad; 00360 00361 MachineBasicBlock *MBB = MI->getParent(); 00362 assert(MBB && "foldMemoryOperand needs an inserted instruction"); 00363 MachineFunction &MF = *MBB->getParent(); 00364 00365 // Ask the target to do the actual folding. 00366 if (MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FI)) { 00367 // Add a memory operand, foldMemoryOperandImpl doesn't do that. 00368 assert((!(Flags & MachineMemOperand::MOStore) || 00369 NewMI->mayStore()) && 00370 "Folded a def to a non-store!"); 00371 assert((!(Flags & MachineMemOperand::MOLoad) || 00372 NewMI->mayLoad()) && 00373 "Folded a use to a non-load!"); 00374 const MachineFrameInfo &MFI = *MF.getFrameInfo(); 00375 assert(MFI.getObjectOffset(FI) != -1); 00376 MachineMemOperand *MMO = 00377 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI), 00378 Flags, MFI.getObjectSize(FI), 00379 MFI.getObjectAlignment(FI)); 00380 NewMI->addMemOperand(MF, MMO); 00381 00382 // FIXME: change foldMemoryOperandImpl semantics to also insert NewMI. 00383 return MBB->insert(MI, NewMI); 00384 } 00385 00386 // Straight COPY may fold as load/store. 00387 if (!MI->isCopy() || Ops.size() != 1) 00388 return 0; 00389 00390 const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]); 00391 if (!RC) 00392 return 0; 00393 00394 const MachineOperand &MO = MI->getOperand(1-Ops[0]); 00395 MachineBasicBlock::iterator Pos = MI; 00396 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo(); 00397 00398 if (Flags == MachineMemOperand::MOStore) 00399 storeRegToStackSlot(*MBB, Pos, MO.getReg(), MO.isKill(), FI, RC, TRI); 00400 else 00401 loadRegFromStackSlot(*MBB, Pos, MO.getReg(), FI, RC, TRI); 00402 return --Pos; 00403 } 00404 00405 /// foldMemoryOperand - Same as the previous version except it allows folding 00406 /// of any load and store from / to any address, not just from a specific 00407 /// stack slot. 00408 MachineInstr* 00409 TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI, 00410 const SmallVectorImpl<unsigned> &Ops, 00411 MachineInstr* LoadMI) const { 00412 assert(LoadMI->canFoldAsLoad() && "LoadMI isn't foldable!"); 00413 #ifndef NDEBUG 00414 for (unsigned i = 0, e = Ops.size(); i != e; ++i) 00415 assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!"); 00416 #endif 00417 MachineBasicBlock &MBB = *MI->getParent(); 00418 MachineFunction &MF = *MBB.getParent(); 00419 00420 // Ask the target to do the actual folding. 00421 MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI); 00422 if (!NewMI) return 0; 00423 00424 NewMI = MBB.insert(MI, NewMI); 00425 00426 // Copy the memoperands from the load to the folded instruction. 00427 NewMI->setMemRefs(LoadMI->memoperands_begin(), 00428 LoadMI->memoperands_end()); 00429 00430 return NewMI; 00431 } 00432 00433 bool TargetInstrInfo:: 00434 isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI, 00435 AliasAnalysis *AA) const { 00436 const MachineFunction &MF = *MI->getParent()->getParent(); 00437 const MachineRegisterInfo &MRI = MF.getRegInfo(); 00438 const TargetMachine &TM = MF.getTarget(); 00439 const TargetInstrInfo &TII = *TM.getInstrInfo(); 00440 00441 // Remat clients assume operand 0 is the defined register. 00442 if (!MI->getNumOperands() || !MI->getOperand(0).isReg()) 00443 return false; 00444 unsigned DefReg = MI->getOperand(0).getReg(); 00445 00446 // A sub-register definition can only be rematerialized if the instruction 00447 // doesn't read the other parts of the register. Otherwise it is really a 00448 // read-modify-write operation on the full virtual register which cannot be 00449 // moved safely. 00450 if (TargetRegisterInfo::isVirtualRegister(DefReg) && 00451 MI->getOperand(0).getSubReg() && MI->readsVirtualRegister(DefReg)) 00452 return false; 00453 00454 // A load from a fixed stack slot can be rematerialized. This may be 00455 // redundant with subsequent checks, but it's target-independent, 00456 // simple, and a common case. 00457 int FrameIdx = 0; 00458 if (TII.isLoadFromStackSlot(MI, FrameIdx) && 00459 MF.getFrameInfo()->isImmutableObjectIndex(FrameIdx)) 00460 return true; 00461 00462 // Avoid instructions obviously unsafe for remat. 00463 if (MI->isNotDuplicable() || MI->mayStore() || 00464 MI->hasUnmodeledSideEffects()) 00465 return false; 00466 00467 // Don't remat inline asm. We have no idea how expensive it is 00468 // even if it's side effect free. 00469 if (MI->isInlineAsm()) 00470 return false; 00471 00472 // Avoid instructions which load from potentially varying memory. 00473 if (MI->mayLoad() && !MI->isInvariantLoad(AA)) 00474 return false; 00475 00476 // If any of the registers accessed are non-constant, conservatively assume 00477 // the instruction is not rematerializable. 00478 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00479 const MachineOperand &MO = MI->getOperand(i); 00480 if (!MO.isReg()) continue; 00481 unsigned Reg = MO.getReg(); 00482 if (Reg == 0) 00483 continue; 00484 00485 // Check for a well-behaved physical register. 00486 if (TargetRegisterInfo::isPhysicalRegister(Reg)) { 00487 if (MO.isUse()) { 00488 // If the physreg has no defs anywhere, it's just an ambient register 00489 // and we can freely move its uses. Alternatively, if it's allocatable, 00490 // it could get allocated to something with a def during allocation. 00491 if (!MRI.isConstantPhysReg(Reg, MF)) 00492 return false; 00493 } else { 00494 // A physreg def. We can't remat it. 00495 return false; 00496 } 00497 continue; 00498 } 00499 00500 // Only allow one virtual-register def. There may be multiple defs of the 00501 // same virtual register, though. 00502 if (MO.isDef() && Reg != DefReg) 00503 return false; 00504 00505 // Don't allow any virtual-register uses. Rematting an instruction with 00506 // virtual register uses would length the live ranges of the uses, which 00507 // is not necessarily a good idea, certainly not "trivial". 00508 if (MO.isUse()) 00509 return false; 00510 } 00511 00512 // Everything checked out. 00513 return true; 00514 } 00515 00516 /// isSchedulingBoundary - Test if the given instruction should be 00517 /// considered a scheduling boundary. This primarily includes labels 00518 /// and terminators. 00519 bool TargetInstrInfo::isSchedulingBoundary(const MachineInstr *MI, 00520 const MachineBasicBlock *MBB, 00521 const MachineFunction &MF) const { 00522 // Terminators and labels can't be scheduled around. 00523 if (MI->isTerminator() || MI->isLabel()) 00524 return true; 00525 00526 // Don't attempt to schedule around any instruction that defines 00527 // a stack-oriented pointer, as it's unlikely to be profitable. This 00528 // saves compile time, because it doesn't require every single 00529 // stack slot reference to depend on the instruction that does the 00530 // modification. 00531 const TargetLowering &TLI = *MF.getTarget().getTargetLowering(); 00532 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo(); 00533 if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore(), TRI)) 00534 return true; 00535 00536 return false; 00537 } 00538 00539 // Provide a global flag for disabling the PreRA hazard recognizer that targets 00540 // may choose to honor. 00541 bool TargetInstrInfo::usePreRAHazardRecognizer() const { 00542 return !DisableHazardRecognizer; 00543 } 00544 00545 // Default implementation of CreateTargetRAHazardRecognizer. 00546 ScheduleHazardRecognizer *TargetInstrInfo:: 00547 CreateTargetHazardRecognizer(const TargetMachine *TM, 00548 const ScheduleDAG *DAG) const { 00549 // Dummy hazard recognizer allows all instructions to issue. 00550 return new ScheduleHazardRecognizer(); 00551 } 00552 00553 // Default implementation of CreateTargetMIHazardRecognizer. 00554 ScheduleHazardRecognizer *TargetInstrInfo:: 00555 CreateTargetMIHazardRecognizer(const InstrItineraryData *II, 00556 const ScheduleDAG *DAG) const { 00557 return (ScheduleHazardRecognizer *) 00558 new ScoreboardHazardRecognizer(II, DAG, "misched"); 00559 } 00560 00561 // Default implementation of CreateTargetPostRAHazardRecognizer. 00562 ScheduleHazardRecognizer *TargetInstrInfo:: 00563 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II, 00564 const ScheduleDAG *DAG) const { 00565 return (ScheduleHazardRecognizer *) 00566 new ScoreboardHazardRecognizer(II, DAG, "post-RA-sched"); 00567 } 00568 00569 //===----------------------------------------------------------------------===// 00570 // SelectionDAG latency interface. 00571 //===----------------------------------------------------------------------===// 00572 00573 int 00574 TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData, 00575 SDNode *DefNode, unsigned DefIdx, 00576 SDNode *UseNode, unsigned UseIdx) const { 00577 if (!ItinData || ItinData->isEmpty()) 00578 return -1; 00579 00580 if (!DefNode->isMachineOpcode()) 00581 return -1; 00582 00583 unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass(); 00584 if (!UseNode->isMachineOpcode()) 00585 return ItinData->getOperandCycle(DefClass, DefIdx); 00586 unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass(); 00587 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx); 00588 } 00589 00590 int TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData, 00591 SDNode *N) const { 00592 if (!ItinData || ItinData->isEmpty()) 00593 return 1; 00594 00595 if (!N->isMachineOpcode()) 00596 return 1; 00597 00598 return ItinData->getStageLatency(get(N->getMachineOpcode()).getSchedClass()); 00599 } 00600 00601 //===----------------------------------------------------------------------===// 00602 // MachineInstr latency interface. 00603 //===----------------------------------------------------------------------===// 00604 00605 unsigned 00606 TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData, 00607 const MachineInstr *MI) const { 00608 if (!ItinData || ItinData->isEmpty()) 00609 return 1; 00610 00611 unsigned Class = MI->getDesc().getSchedClass(); 00612 int UOps = ItinData->Itineraries[Class].NumMicroOps; 00613 if (UOps >= 0) 00614 return UOps; 00615 00616 // The # of u-ops is dynamically determined. The specific target should 00617 // override this function to return the right number. 00618 return 1; 00619 } 00620 00621 /// Return the default expected latency for a def based on it's opcode. 00622 unsigned TargetInstrInfo::defaultDefLatency(const MCSchedModel *SchedModel, 00623 const MachineInstr *DefMI) const { 00624 if (DefMI->isTransient()) 00625 return 0; 00626 if (DefMI->mayLoad()) 00627 return SchedModel->LoadLatency; 00628 if (isHighLatencyDef(DefMI->getOpcode())) 00629 return SchedModel->HighLatency; 00630 return 1; 00631 } 00632 00633 unsigned TargetInstrInfo:: 00634 getInstrLatency(const InstrItineraryData *ItinData, 00635 const MachineInstr *MI, 00636 unsigned *PredCost) const { 00637 // Default to one cycle for no itinerary. However, an "empty" itinerary may 00638 // still have a MinLatency property, which getStageLatency checks. 00639 if (!ItinData) 00640 return MI->mayLoad() ? 2 : 1; 00641 00642 return ItinData->getStageLatency(MI->getDesc().getSchedClass()); 00643 } 00644 00645 bool TargetInstrInfo::hasLowDefLatency(const InstrItineraryData *ItinData, 00646 const MachineInstr *DefMI, 00647 unsigned DefIdx) const { 00648 if (!ItinData || ItinData->isEmpty()) 00649 return false; 00650 00651 unsigned DefClass = DefMI->getDesc().getSchedClass(); 00652 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx); 00653 return (DefCycle != -1 && DefCycle <= 1); 00654 } 00655 00656 /// Both DefMI and UseMI must be valid. By default, call directly to the 00657 /// itinerary. This may be overriden by the target. 00658 int TargetInstrInfo:: 00659 getOperandLatency(const InstrItineraryData *ItinData, 00660 const MachineInstr *DefMI, unsigned DefIdx, 00661 const MachineInstr *UseMI, unsigned UseIdx) const { 00662 unsigned DefClass = DefMI->getDesc().getSchedClass(); 00663 unsigned UseClass = UseMI->getDesc().getSchedClass(); 00664 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx); 00665 } 00666 00667 /// If we can determine the operand latency from the def only, without itinerary 00668 /// lookup, do so. Otherwise return -1. 00669 int TargetInstrInfo::computeDefOperandLatency( 00670 const InstrItineraryData *ItinData, 00671 const MachineInstr *DefMI, bool FindMin) const { 00672 00673 // Let the target hook getInstrLatency handle missing itineraries. 00674 if (!ItinData) 00675 return getInstrLatency(ItinData, DefMI); 00676 00677 // Return a latency based on the itinerary properties and defining instruction 00678 // if possible. Some common subtargets don't require per-operand latency, 00679 // especially for minimum latencies. 00680 if (FindMin) { 00681 // If MinLatency is valid, call getInstrLatency. This uses Stage latency if 00682 // it exists before defaulting to MinLatency. 00683 if (ItinData->SchedModel->MinLatency >= 0) 00684 return getInstrLatency(ItinData, DefMI); 00685 00686 // If MinLatency is invalid, OperandLatency is interpreted as MinLatency. 00687 // For empty itineraries, short-cirtuit the check and default to one cycle. 00688 if (ItinData->isEmpty()) 00689 return 1; 00690 } 00691 else if(ItinData->isEmpty()) 00692 return defaultDefLatency(ItinData->SchedModel, DefMI); 00693 00694 // ...operand lookup required 00695 return -1; 00696 } 00697 00698 /// computeOperandLatency - Compute and return the latency of the given data 00699 /// dependent def and use when the operand indices are already known. UseMI may 00700 /// be NULL for an unknown use. 00701 /// 00702 /// FindMin may be set to get the minimum vs. expected latency. Minimum 00703 /// latency is used for scheduling groups, while expected latency is for 00704 /// instruction cost and critical path. 00705 /// 00706 /// Depending on the subtarget's itinerary properties, this may or may not need 00707 /// to call getOperandLatency(). For most subtargets, we don't need DefIdx or 00708 /// UseIdx to compute min latency. 00709 unsigned TargetInstrInfo:: 00710 computeOperandLatency(const InstrItineraryData *ItinData, 00711 const MachineInstr *DefMI, unsigned DefIdx, 00712 const MachineInstr *UseMI, unsigned UseIdx, 00713 bool FindMin) const { 00714 00715 int DefLatency = computeDefOperandLatency(ItinData, DefMI, FindMin); 00716 if (DefLatency >= 0) 00717 return DefLatency; 00718 00719 assert(ItinData && !ItinData->isEmpty() && "computeDefOperandLatency fail"); 00720 00721 int OperLatency = 0; 00722 if (UseMI) 00723 OperLatency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx); 00724 else { 00725 unsigned DefClass = DefMI->getDesc().getSchedClass(); 00726 OperLatency = ItinData->getOperandCycle(DefClass, DefIdx); 00727 } 00728 if (OperLatency >= 0) 00729 return OperLatency; 00730 00731 // No operand latency was found. 00732 unsigned InstrLatency = getInstrLatency(ItinData, DefMI); 00733 00734 // Expected latency is the max of the stage latency and itinerary props. 00735 if (!FindMin) 00736 InstrLatency = std::max(InstrLatency, 00737 defaultDefLatency(ItinData->SchedModel, DefMI)); 00738 return InstrLatency; 00739 }