1 //===-- TargetInstrInfo.cpp - Target Instruction Information --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the TargetInstrInfo class.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Target/TargetInstrInfo.h"
15 #include "llvm/CodeGen/MachineFrameInfo.h"
16 #include "llvm/CodeGen/MachineInstrBuilder.h"
17 #include "llvm/CodeGen/MachineMemOperand.h"
18 #include "llvm/CodeGen/MachineRegisterInfo.h"
19 #include "llvm/CodeGen/PseudoSourceValue.h"
20 #include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
21 #include "llvm/CodeGen/StackMaps.h"
22 #include "llvm/IR/DataLayout.h"
23 #include "llvm/MC/MCAsmInfo.h"
24 #include "llvm/MC/MCInstrItineraries.h"
25 #include "llvm/Support/CommandLine.h"
26 #include "llvm/Support/ErrorHandling.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Target/TargetLowering.h"
29 #include "llvm/Target/TargetMachine.h"
30 #include "llvm/Target/TargetRegisterInfo.h"
34 static cl::opt<bool> DisableHazardRecognizer(
35 "disable-sched-hazard", cl::Hidden, cl::init(false),
36 cl::desc("Disable hazard detection during preRA scheduling"));
38 TargetInstrInfo::~TargetInstrInfo() {
41 const TargetRegisterClass*
42 TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
43 const TargetRegisterInfo *TRI,
44 const MachineFunction &MF) const {
45 if (OpNum >= MCID.getNumOperands())
48 short RegClass = MCID.OpInfo[OpNum].RegClass;
49 if (MCID.OpInfo[OpNum].isLookupPtrRegClass())
50 return TRI->getPointerRegClass(MF, RegClass);
52 // Instructions like INSERT_SUBREG do not have fixed register classes.
56 // Otherwise just look it up normally.
57 return TRI->getRegClass(RegClass);
60 /// insertNoop - Insert a noop into the instruction stream at the specified
62 void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
63 MachineBasicBlock::iterator MI) const {
64 llvm_unreachable("Target didn't implement insertNoop!");
67 /// Measure the specified inline asm to determine an approximation of its
69 /// Comments (which run till the next SeparatorString or newline) do not
70 /// count as an instruction.
71 /// Any other non-whitespace text is considered an instruction, with
72 /// multiple instructions separated by SeparatorString or newlines.
73 /// Variable-length instructions are not handled here; this function
74 /// may be overloaded in the target code to do that.
75 unsigned TargetInstrInfo::getInlineAsmLength(const char *Str,
76 const MCAsmInfo &MAI) const {
79 // Count the number of instructions in the asm.
80 bool atInsnStart = true;
83 if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
84 strlen(MAI.getSeparatorString())) == 0)
86 if (atInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
87 Length += MAI.getMaxInstLength();
90 if (atInsnStart && strncmp(Str, MAI.getCommentString(),
91 strlen(MAI.getCommentString())) == 0)
98 /// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything
99 /// after it, replacing it with an unconditional branch to NewDest.
101 TargetInstrInfo::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
102 MachineBasicBlock *NewDest) const {
103 MachineBasicBlock *MBB = Tail->getParent();
105 // Remove all the old successors of MBB from the CFG.
106 while (!MBB->succ_empty())
107 MBB->removeSuccessor(MBB->succ_begin());
109 // Remove all the dead instructions from the end of MBB.
110 MBB->erase(Tail, MBB->end());
112 // If MBB isn't immediately before MBB, insert a branch to it.
113 if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(NewDest))
114 InsertBranch(*MBB, NewDest, 0, SmallVector<MachineOperand, 0>(),
115 Tail->getDebugLoc());
116 MBB->addSuccessor(NewDest);
119 // commuteInstruction - The default implementation of this method just exchanges
120 // the two operands returned by findCommutedOpIndices.
121 MachineInstr *TargetInstrInfo::commuteInstruction(MachineInstr *MI,
123 const MCInstrDesc &MCID = MI->getDesc();
124 bool HasDef = MCID.getNumDefs();
125 if (HasDef && !MI->getOperand(0).isReg())
126 // No idea how to commute this instruction. Target should implement its own.
129 if (!findCommutedOpIndices(MI, Idx1, Idx2)) {
131 raw_string_ostream Msg(msg);
132 Msg << "Don't know how to commute: " << *MI;
133 report_fatal_error(Msg.str());
136 assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() &&
137 "This only knows how to commute register operands so far");
138 unsigned Reg0 = HasDef ? MI->getOperand(0).getReg() : 0;
139 unsigned Reg1 = MI->getOperand(Idx1).getReg();
140 unsigned Reg2 = MI->getOperand(Idx2).getReg();
141 unsigned SubReg0 = HasDef ? MI->getOperand(0).getSubReg() : 0;
142 unsigned SubReg1 = MI->getOperand(Idx1).getSubReg();
143 unsigned SubReg2 = MI->getOperand(Idx2).getSubReg();
144 bool Reg1IsKill = MI->getOperand(Idx1).isKill();
145 bool Reg2IsKill = MI->getOperand(Idx2).isKill();
146 // If destination is tied to either of the commuted source register, then
147 // it must be updated.
148 if (HasDef && Reg0 == Reg1 &&
149 MI->getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO) == 0) {
153 } else if (HasDef && Reg0 == Reg2 &&
154 MI->getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO) == 0) {
161 // Create a new instruction.
162 MachineFunction &MF = *MI->getParent()->getParent();
163 MI = MF.CloneMachineInstr(MI);
167 MI->getOperand(0).setReg(Reg0);
168 MI->getOperand(0).setSubReg(SubReg0);
170 MI->getOperand(Idx2).setReg(Reg1);
171 MI->getOperand(Idx1).setReg(Reg2);
172 MI->getOperand(Idx2).setSubReg(SubReg1);
173 MI->getOperand(Idx1).setSubReg(SubReg2);
174 MI->getOperand(Idx2).setIsKill(Reg1IsKill);
175 MI->getOperand(Idx1).setIsKill(Reg2IsKill);
179 /// findCommutedOpIndices - If specified MI is commutable, return the two
180 /// operand indices that would swap value. Return true if the instruction
181 /// is not in a form which this routine understands.
182 bool TargetInstrInfo::findCommutedOpIndices(MachineInstr *MI,
184 unsigned &SrcOpIdx2) const {
185 assert(!MI->isBundle() &&
186 "TargetInstrInfo::findCommutedOpIndices() can't handle bundles");
188 const MCInstrDesc &MCID = MI->getDesc();
189 if (!MCID.isCommutable())
191 // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this
192 // is not true, then the target must implement this.
193 SrcOpIdx1 = MCID.getNumDefs();
194 SrcOpIdx2 = SrcOpIdx1 + 1;
195 if (!MI->getOperand(SrcOpIdx1).isReg() ||
196 !MI->getOperand(SrcOpIdx2).isReg())
204 TargetInstrInfo::isUnpredicatedTerminator(const MachineInstr *MI) const {
205 if (!MI->isTerminator()) return false;
207 // Conditional branch is a special case.
208 if (MI->isBranch() && !MI->isBarrier())
210 if (!MI->isPredicable())
212 return !isPredicated(MI);
216 bool TargetInstrInfo::PredicateInstruction(MachineInstr *MI,
217 const SmallVectorImpl<MachineOperand> &Pred) const {
218 bool MadeChange = false;
220 assert(!MI->isBundle() &&
221 "TargetInstrInfo::PredicateInstruction() can't handle bundles");
223 const MCInstrDesc &MCID = MI->getDesc();
224 if (!MI->isPredicable())
227 for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) {
228 if (MCID.OpInfo[i].isPredicate()) {
229 MachineOperand &MO = MI->getOperand(i);
231 MO.setReg(Pred[j].getReg());
233 } else if (MO.isImm()) {
234 MO.setImm(Pred[j].getImm());
236 } else if (MO.isMBB()) {
237 MO.setMBB(Pred[j].getMBB());
246 bool TargetInstrInfo::hasLoadFromStackSlot(const MachineInstr *MI,
247 const MachineMemOperand *&MMO,
248 int &FrameIndex) const {
249 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
250 oe = MI->memoperands_end();
253 if ((*o)->isLoad() && (*o)->getValue())
254 if (const FixedStackPseudoSourceValue *Value =
255 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
256 FrameIndex = Value->getFrameIndex();
264 bool TargetInstrInfo::hasStoreToStackSlot(const MachineInstr *MI,
265 const MachineMemOperand *&MMO,
266 int &FrameIndex) const {
267 for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
268 oe = MI->memoperands_end();
271 if ((*o)->isStore() && (*o)->getValue())
272 if (const FixedStackPseudoSourceValue *Value =
273 dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
274 FrameIndex = Value->getFrameIndex();
282 bool TargetInstrInfo::getStackSlotRange(const TargetRegisterClass *RC,
283 unsigned SubIdx, unsigned &Size,
285 const TargetMachine *TM) const {
287 Size = RC->getSize();
291 unsigned BitSize = TM->getRegisterInfo()->getSubRegIdxSize(SubIdx);
292 // Convert bit size to byte size to be consistent with
293 // MCRegisterClass::getSize().
297 int BitOffset = TM->getRegisterInfo()->getSubRegIdxOffset(SubIdx);
298 if (BitOffset < 0 || BitOffset % 8)
302 Offset = (unsigned)BitOffset / 8;
304 assert(RC->getSize() >= (Offset + Size) && "bad subregister range");
306 if (!TM->getDataLayout()->isLittleEndian()) {
307 Offset = RC->getSize() - (Offset + Size);
312 void TargetInstrInfo::reMaterialize(MachineBasicBlock &MBB,
313 MachineBasicBlock::iterator I,
316 const MachineInstr *Orig,
317 const TargetRegisterInfo &TRI) const {
318 MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
319 MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI);
324 TargetInstrInfo::produceSameValue(const MachineInstr *MI0,
325 const MachineInstr *MI1,
326 const MachineRegisterInfo *MRI) const {
327 return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
330 MachineInstr *TargetInstrInfo::duplicate(MachineInstr *Orig,
331 MachineFunction &MF) const {
332 assert(!Orig->isNotDuplicable() &&
333 "Instruction cannot be duplicated");
334 return MF.CloneMachineInstr(Orig);
337 // If the COPY instruction in MI can be folded to a stack operation, return
338 // the register class to use.
339 static const TargetRegisterClass *canFoldCopy(const MachineInstr *MI,
341 assert(MI->isCopy() && "MI must be a COPY instruction");
342 if (MI->getNumOperands() != 2)
344 assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand");
346 const MachineOperand &FoldOp = MI->getOperand(FoldIdx);
347 const MachineOperand &LiveOp = MI->getOperand(1-FoldIdx);
349 if (FoldOp.getSubReg() || LiveOp.getSubReg())
352 unsigned FoldReg = FoldOp.getReg();
353 unsigned LiveReg = LiveOp.getReg();
355 assert(TargetRegisterInfo::isVirtualRegister(FoldReg) &&
356 "Cannot fold physregs");
358 const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
359 const TargetRegisterClass *RC = MRI.getRegClass(FoldReg);
361 if (TargetRegisterInfo::isPhysicalRegister(LiveOp.getReg()))
362 return RC->contains(LiveOp.getReg()) ? RC : 0;
364 if (RC->hasSubClassEq(MRI.getRegClass(LiveReg)))
367 // FIXME: Allow folding when register classes are memory compatible.
371 bool TargetInstrInfo::
372 canFoldMemoryOperand(const MachineInstr *MI,
373 const SmallVectorImpl<unsigned> &Ops) const {
374 return MI->isCopy() && Ops.size() == 1 && canFoldCopy(MI, Ops[0]);
377 static MachineInstr* foldPatchpoint(MachineFunction &MF,
379 const SmallVectorImpl<unsigned> &Ops,
381 const TargetInstrInfo &TII) {
382 unsigned StartIdx = 0;
383 switch (MI->getOpcode()) {
384 case TargetOpcode::STACKMAP:
385 StartIdx = 2; // Skip ID, nShadowBytes.
387 case TargetOpcode::PATCHPOINT: {
388 // For PatchPoint, the call args are not foldable.
389 PatchPointOpers opers(MI);
390 StartIdx = opers.getVarIdx();
394 llvm_unreachable("unexpected stackmap opcode");
397 // Return false if any operands requested for folding are not foldable (not
398 // part of the stackmap's live values).
399 for (SmallVectorImpl<unsigned>::const_iterator I = Ops.begin(), E = Ops.end();
405 MachineInstr *NewMI =
406 MF.CreateMachineInstr(TII.get(MI->getOpcode()), MI->getDebugLoc(), true);
407 MachineInstrBuilder MIB(MF, NewMI);
409 // No need to fold return, the meta data, and function arguments
410 for (unsigned i = 0; i < StartIdx; ++i)
411 MIB.addOperand(MI->getOperand(i));
413 for (unsigned i = StartIdx; i < MI->getNumOperands(); ++i) {
414 MachineOperand &MO = MI->getOperand(i);
415 if (std::find(Ops.begin(), Ops.end(), i) != Ops.end()) {
417 unsigned SpillOffset;
418 // Compute the spill slot size and offset.
419 const TargetRegisterClass *RC =
420 MF.getRegInfo().getRegClass(MO.getReg());
421 bool Valid = TII.getStackSlotRange(RC, MO.getSubReg(), SpillSize,
422 SpillOffset, &MF.getTarget());
424 report_fatal_error("cannot spill patchpoint subregister operand");
425 MIB.addImm(StackMaps::IndirectMemRefOp);
426 MIB.addImm(SpillSize);
427 MIB.addFrameIndex(FrameIndex);
428 MIB.addImm(SpillOffset);
436 /// foldMemoryOperand - Attempt to fold a load or store of the specified stack
437 /// slot into the specified machine instruction for the specified operand(s).
438 /// If this is possible, a new instruction is returned with the specified
439 /// operand folded, otherwise NULL is returned. The client is responsible for
440 /// removing the old instruction and adding the new one in the instruction
443 TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI,
444 const SmallVectorImpl<unsigned> &Ops,
447 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
448 if (MI->getOperand(Ops[i]).isDef())
449 Flags |= MachineMemOperand::MOStore;
451 Flags |= MachineMemOperand::MOLoad;
453 MachineBasicBlock *MBB = MI->getParent();
454 assert(MBB && "foldMemoryOperand needs an inserted instruction");
455 MachineFunction &MF = *MBB->getParent();
457 MachineInstr *NewMI = 0;
459 if (MI->getOpcode() == TargetOpcode::STACKMAP ||
460 MI->getOpcode() == TargetOpcode::PATCHPOINT) {
461 // Fold stackmap/patchpoint.
462 NewMI = foldPatchpoint(MF, MI, Ops, FI, *this);
464 // Ask the target to do the actual folding.
465 NewMI =foldMemoryOperandImpl(MF, MI, Ops, FI);
469 NewMI->setMemRefs(MI->memoperands_begin(), MI->memoperands_end());
470 // Add a memory operand, foldMemoryOperandImpl doesn't do that.
471 assert((!(Flags & MachineMemOperand::MOStore) ||
472 NewMI->mayStore()) &&
473 "Folded a def to a non-store!");
474 assert((!(Flags & MachineMemOperand::MOLoad) ||
476 "Folded a use to a non-load!");
477 const MachineFrameInfo &MFI = *MF.getFrameInfo();
478 assert(MFI.getObjectOffset(FI) != -1);
479 MachineMemOperand *MMO =
480 MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
481 Flags, MFI.getObjectSize(FI),
482 MFI.getObjectAlignment(FI));
483 NewMI->addMemOperand(MF, MMO);
485 // FIXME: change foldMemoryOperandImpl semantics to also insert NewMI.
486 return MBB->insert(MI, NewMI);
489 // Straight COPY may fold as load/store.
490 if (!MI->isCopy() || Ops.size() != 1)
493 const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]);
497 const MachineOperand &MO = MI->getOperand(1-Ops[0]);
498 MachineBasicBlock::iterator Pos = MI;
499 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
501 if (Flags == MachineMemOperand::MOStore)
502 storeRegToStackSlot(*MBB, Pos, MO.getReg(), MO.isKill(), FI, RC, TRI);
504 loadRegFromStackSlot(*MBB, Pos, MO.getReg(), FI, RC, TRI);
508 /// foldMemoryOperand - Same as the previous version except it allows folding
509 /// of any load and store from / to any address, not just from a specific
512 TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI,
513 const SmallVectorImpl<unsigned> &Ops,
514 MachineInstr* LoadMI) const {
515 assert(LoadMI->canFoldAsLoad() && "LoadMI isn't foldable!");
517 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
518 assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!");
520 MachineBasicBlock &MBB = *MI->getParent();
521 MachineFunction &MF = *MBB.getParent();
523 // Ask the target to do the actual folding.
524 MachineInstr *NewMI = 0;
527 if ((MI->getOpcode() == TargetOpcode::STACKMAP ||
528 MI->getOpcode() == TargetOpcode::PATCHPOINT) &&
529 isLoadFromStackSlot(LoadMI, FrameIndex)) {
530 // Fold stackmap/patchpoint.
531 NewMI = foldPatchpoint(MF, MI, Ops, FrameIndex, *this);
533 // Ask the target to do the actual folding.
534 NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI);
537 if (!NewMI) return 0;
539 NewMI = MBB.insert(MI, NewMI);
541 // Copy the memoperands from the load to the folded instruction.
542 if (MI->memoperands_empty()) {
543 NewMI->setMemRefs(LoadMI->memoperands_begin(),
544 LoadMI->memoperands_end());
547 // Handle the rare case of folding multiple loads.
548 NewMI->setMemRefs(MI->memoperands_begin(),
549 MI->memoperands_end());
550 for (MachineInstr::mmo_iterator I = LoadMI->memoperands_begin(),
551 E = LoadMI->memoperands_end(); I != E; ++I) {
552 NewMI->addMemOperand(MF, *I);
558 bool TargetInstrInfo::
559 isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI,
560 AliasAnalysis *AA) const {
561 const MachineFunction &MF = *MI->getParent()->getParent();
562 const MachineRegisterInfo &MRI = MF.getRegInfo();
563 const TargetMachine &TM = MF.getTarget();
564 const TargetInstrInfo &TII = *TM.getInstrInfo();
566 // Remat clients assume operand 0 is the defined register.
567 if (!MI->getNumOperands() || !MI->getOperand(0).isReg())
569 unsigned DefReg = MI->getOperand(0).getReg();
571 // A sub-register definition can only be rematerialized if the instruction
572 // doesn't read the other parts of the register. Otherwise it is really a
573 // read-modify-write operation on the full virtual register which cannot be
575 if (TargetRegisterInfo::isVirtualRegister(DefReg) &&
576 MI->getOperand(0).getSubReg() && MI->readsVirtualRegister(DefReg))
579 // A load from a fixed stack slot can be rematerialized. This may be
580 // redundant with subsequent checks, but it's target-independent,
581 // simple, and a common case.
583 if (TII.isLoadFromStackSlot(MI, FrameIdx) &&
584 MF.getFrameInfo()->isImmutableObjectIndex(FrameIdx))
587 // Avoid instructions obviously unsafe for remat.
588 if (MI->isNotDuplicable() || MI->mayStore() ||
589 MI->hasUnmodeledSideEffects())
592 // Don't remat inline asm. We have no idea how expensive it is
593 // even if it's side effect free.
594 if (MI->isInlineAsm())
597 // Avoid instructions which load from potentially varying memory.
598 if (MI->mayLoad() && !MI->isInvariantLoad(AA))
601 // If any of the registers accessed are non-constant, conservatively assume
602 // the instruction is not rematerializable.
603 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
604 const MachineOperand &MO = MI->getOperand(i);
605 if (!MO.isReg()) continue;
606 unsigned Reg = MO.getReg();
610 // Check for a well-behaved physical register.
611 if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
613 // If the physreg has no defs anywhere, it's just an ambient register
614 // and we can freely move its uses. Alternatively, if it's allocatable,
615 // it could get allocated to something with a def during allocation.
616 if (!MRI.isConstantPhysReg(Reg, MF))
619 // A physreg def. We can't remat it.
625 // Only allow one virtual-register def. There may be multiple defs of the
626 // same virtual register, though.
627 if (MO.isDef() && Reg != DefReg)
630 // Don't allow any virtual-register uses. Rematting an instruction with
631 // virtual register uses would length the live ranges of the uses, which
632 // is not necessarily a good idea, certainly not "trivial".
637 // Everything checked out.
641 /// isSchedulingBoundary - Test if the given instruction should be
642 /// considered a scheduling boundary. This primarily includes labels
644 bool TargetInstrInfo::isSchedulingBoundary(const MachineInstr *MI,
645 const MachineBasicBlock *MBB,
646 const MachineFunction &MF) const {
647 // Terminators and labels can't be scheduled around.
648 if (MI->isTerminator() || MI->isLabel())
651 // Don't attempt to schedule around any instruction that defines
652 // a stack-oriented pointer, as it's unlikely to be profitable. This
653 // saves compile time, because it doesn't require every single
654 // stack slot reference to depend on the instruction that does the
656 const TargetLowering &TLI = *MF.getTarget().getTargetLowering();
657 const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
658 if (MI->modifiesRegister(TLI.getStackPointerRegisterToSaveRestore(), TRI))
664 // Provide a global flag for disabling the PreRA hazard recognizer that targets
665 // may choose to honor.
666 bool TargetInstrInfo::usePreRAHazardRecognizer() const {
667 return !DisableHazardRecognizer;
670 // Default implementation of CreateTargetRAHazardRecognizer.
671 ScheduleHazardRecognizer *TargetInstrInfo::
672 CreateTargetHazardRecognizer(const TargetMachine *TM,
673 const ScheduleDAG *DAG) const {
674 // Dummy hazard recognizer allows all instructions to issue.
675 return new ScheduleHazardRecognizer();
678 // Default implementation of CreateTargetMIHazardRecognizer.
679 ScheduleHazardRecognizer *TargetInstrInfo::
680 CreateTargetMIHazardRecognizer(const InstrItineraryData *II,
681 const ScheduleDAG *DAG) const {
682 return (ScheduleHazardRecognizer *)
683 new ScoreboardHazardRecognizer(II, DAG, "misched");
686 // Default implementation of CreateTargetPostRAHazardRecognizer.
687 ScheduleHazardRecognizer *TargetInstrInfo::
688 CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
689 const ScheduleDAG *DAG) const {
690 return (ScheduleHazardRecognizer *)
691 new ScoreboardHazardRecognizer(II, DAG, "post-RA-sched");
694 //===----------------------------------------------------------------------===//
695 // SelectionDAG latency interface.
696 //===----------------------------------------------------------------------===//
699 TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
700 SDNode *DefNode, unsigned DefIdx,
701 SDNode *UseNode, unsigned UseIdx) const {
702 if (!ItinData || ItinData->isEmpty())
705 if (!DefNode->isMachineOpcode())
708 unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass();
709 if (!UseNode->isMachineOpcode())
710 return ItinData->getOperandCycle(DefClass, DefIdx);
711 unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass();
712 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
715 int TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
717 if (!ItinData || ItinData->isEmpty())
720 if (!N->isMachineOpcode())
723 return ItinData->getStageLatency(get(N->getMachineOpcode()).getSchedClass());
726 //===----------------------------------------------------------------------===//
727 // MachineInstr latency interface.
728 //===----------------------------------------------------------------------===//
731 TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
732 const MachineInstr *MI) const {
733 if (!ItinData || ItinData->isEmpty())
736 unsigned Class = MI->getDesc().getSchedClass();
737 int UOps = ItinData->Itineraries[Class].NumMicroOps;
741 // The # of u-ops is dynamically determined. The specific target should
742 // override this function to return the right number.
746 /// Return the default expected latency for a def based on it's opcode.
747 unsigned TargetInstrInfo::defaultDefLatency(const MCSchedModel *SchedModel,
748 const MachineInstr *DefMI) const {
749 if (DefMI->isTransient())
751 if (DefMI->mayLoad())
752 return SchedModel->LoadLatency;
753 if (isHighLatencyDef(DefMI->getOpcode()))
754 return SchedModel->HighLatency;
758 unsigned TargetInstrInfo::getPredicationCost(const MachineInstr *) const {
762 unsigned TargetInstrInfo::
763 getInstrLatency(const InstrItineraryData *ItinData,
764 const MachineInstr *MI,
765 unsigned *PredCost) const {
766 // Default to one cycle for no itinerary. However, an "empty" itinerary may
767 // still have a MinLatency property, which getStageLatency checks.
769 return MI->mayLoad() ? 2 : 1;
771 return ItinData->getStageLatency(MI->getDesc().getSchedClass());
774 bool TargetInstrInfo::hasLowDefLatency(const InstrItineraryData *ItinData,
775 const MachineInstr *DefMI,
776 unsigned DefIdx) const {
777 if (!ItinData || ItinData->isEmpty())
780 unsigned DefClass = DefMI->getDesc().getSchedClass();
781 int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
782 return (DefCycle != -1 && DefCycle <= 1);
785 /// Both DefMI and UseMI must be valid. By default, call directly to the
786 /// itinerary. This may be overriden by the target.
787 int TargetInstrInfo::
788 getOperandLatency(const InstrItineraryData *ItinData,
789 const MachineInstr *DefMI, unsigned DefIdx,
790 const MachineInstr *UseMI, unsigned UseIdx) const {
791 unsigned DefClass = DefMI->getDesc().getSchedClass();
792 unsigned UseClass = UseMI->getDesc().getSchedClass();
793 return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
796 /// If we can determine the operand latency from the def only, without itinerary
797 /// lookup, do so. Otherwise return -1.
798 int TargetInstrInfo::computeDefOperandLatency(
799 const InstrItineraryData *ItinData,
800 const MachineInstr *DefMI) const {
802 // Let the target hook getInstrLatency handle missing itineraries.
804 return getInstrLatency(ItinData, DefMI);
806 if(ItinData->isEmpty())
807 return defaultDefLatency(ItinData->SchedModel, DefMI);
809 // ...operand lookup required
813 /// computeOperandLatency - Compute and return the latency of the given data
814 /// dependent def and use when the operand indices are already known. UseMI may
815 /// be NULL for an unknown use.
817 /// FindMin may be set to get the minimum vs. expected latency. Minimum
818 /// latency is used for scheduling groups, while expected latency is for
819 /// instruction cost and critical path.
821 /// Depending on the subtarget's itinerary properties, this may or may not need
822 /// to call getOperandLatency(). For most subtargets, we don't need DefIdx or
823 /// UseIdx to compute min latency.
824 unsigned TargetInstrInfo::
825 computeOperandLatency(const InstrItineraryData *ItinData,
826 const MachineInstr *DefMI, unsigned DefIdx,
827 const MachineInstr *UseMI, unsigned UseIdx) const {
829 int DefLatency = computeDefOperandLatency(ItinData, DefMI);
833 assert(ItinData && !ItinData->isEmpty() && "computeDefOperandLatency fail");
837 OperLatency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
839 unsigned DefClass = DefMI->getDesc().getSchedClass();
840 OperLatency = ItinData->getOperandCycle(DefClass, DefIdx);
842 if (OperLatency >= 0)
845 // No operand latency was found.
846 unsigned InstrLatency = getInstrLatency(ItinData, DefMI);
848 // Expected latency is the max of the stage latency and itinerary props.
849 InstrLatency = std::max(InstrLatency,
850 defaultDefLatency(ItinData->SchedModel, DefMI));