TargetInstrInfoImpl.cpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. //===-- TargetInstrInfoImpl.cpp - Target Instruction Information ----------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the TargetInstrInfoImpl class, it just provides default
  11. // implementations of various methods.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Target/TargetInstrInfo.h"
  15. #include "llvm/Target/TargetLowering.h"
  16. #include "llvm/Target/TargetMachine.h"
  17. #include "llvm/Target/TargetRegisterInfo.h"
  18. #include "llvm/ADT/SmallVector.h"
  19. #include "llvm/CodeGen/MachineFrameInfo.h"
  20. #include "llvm/CodeGen/MachineInstr.h"
  21. #include "llvm/CodeGen/MachineInstrBuilder.h"
  22. #include "llvm/CodeGen/MachineMemOperand.h"
  23. #include "llvm/CodeGen/MachineRegisterInfo.h"
  24. #include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
  25. #include "llvm/CodeGen/PseudoSourceValue.h"
  26. #include "llvm/MC/MCInstrItineraries.h"
  27. #include "llvm/Support/CommandLine.h"
  28. #include "llvm/Support/Debug.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. using namespace llvm;
  32. static cl::opt<bool> DisableHazardRecognizer(
  33. "disable-sched-hazard", cl::Hidden, cl::init(false),
  34. cl::desc("Disable hazard detection during preRA scheduling"));
  35. /// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything
  36. /// after it, replacing it with an unconditional branch to NewDest.
  37. void
  38. TargetInstrInfoImpl::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
  39. MachineBasicBlock *NewDest) const {
  40. MachineBasicBlock *MBB = Tail->getParent();
  41. // Remove all the old successors of MBB from the CFG.
  42. while (!MBB->succ_empty())
  43. MBB->removeSuccessor(MBB->succ_begin());
  44. // Remove all the dead instructions from the end of MBB.
  45. MBB->erase(Tail, MBB->end());
  46. // If MBB isn't immediately before MBB, insert a branch to it.
  47. if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(NewDest))
  48. InsertBranch(*MBB, NewDest, 0, SmallVector<MachineOperand, 0>(),
  49. Tail->getDebugLoc());
  50. MBB->addSuccessor(NewDest);
  51. }
  52. // commuteInstruction - The default implementation of this method just exchanges
  53. // the two operands returned by findCommutedOpIndices.
  54. MachineInstr *TargetInstrInfoImpl::commuteInstruction(MachineInstr *MI,
  55. bool NewMI) const {
  56. const MCInstrDesc &MCID = MI->getDesc();
  57. bool HasDef = MCID.getNumDefs();
  58. if (HasDef && !MI->getOperand(0).isReg())
  59. // No idea how to commute this instruction. Target should implement its own.
  60. return 0;
  61. unsigned Idx1, Idx2;
  62. if (!findCommutedOpIndices(MI, Idx1, Idx2)) {
  63. std::string msg;
  64. raw_string_ostream Msg(msg);
  65. Msg << "Don't know how to commute: " << *MI;
  66. report_fatal_error(Msg.str());
  67. }
  68. assert(MI->getOperand(Idx1).isReg() && MI->getOperand(Idx2).isReg() &&
  69. "This only knows how to commute register operands so far");
  70. unsigned Reg0 = HasDef ? MI->getOperand(0).getReg() : 0;
  71. unsigned Reg1 = MI->getOperand(Idx1).getReg();
  72. unsigned Reg2 = MI->getOperand(Idx2).getReg();
  73. unsigned SubReg0 = HasDef ? MI->getOperand(0).getSubReg() : 0;
  74. unsigned SubReg1 = MI->getOperand(Idx1).getSubReg();
  75. unsigned SubReg2 = MI->getOperand(Idx2).getSubReg();
  76. bool Reg1IsKill = MI->getOperand(Idx1).isKill();
  77. bool Reg2IsKill = MI->getOperand(Idx2).isKill();
  78. // If destination is tied to either of the commuted source register, then
  79. // it must be updated.
  80. if (HasDef && Reg0 == Reg1 &&
  81. MI->getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO) == 0) {
  82. Reg2IsKill = false;
  83. Reg0 = Reg2;
  84. SubReg0 = SubReg2;
  85. } else if (HasDef && Reg0 == Reg2 &&
  86. MI->getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO) == 0) {
  87. Reg1IsKill = false;
  88. Reg0 = Reg1;
  89. SubReg0 = SubReg1;
  90. }
  91. if (NewMI) {
  92. // Create a new instruction.
  93. bool Reg0IsDead = HasDef ? MI->getOperand(0).isDead() : false;
  94. MachineFunction &MF = *MI->getParent()->getParent();
  95. if (HasDef)
  96. return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
  97. .addReg(Reg0, RegState::Define | getDeadRegState(Reg0IsDead), SubReg0)
  98. .addReg(Reg2, getKillRegState(Reg2IsKill), SubReg2)
  99. .addReg(Reg1, getKillRegState(Reg1IsKill), SubReg1);
  100. else
  101. return BuildMI(MF, MI->getDebugLoc(), MI->getDesc())
  102. .addReg(Reg2, getKillRegState(Reg2IsKill), SubReg2)
  103. .addReg(Reg1, getKillRegState(Reg1IsKill), SubReg1);
  104. }
  105. if (HasDef) {
  106. MI->getOperand(0).setReg(Reg0);
  107. MI->getOperand(0).setSubReg(SubReg0);
  108. }
  109. MI->getOperand(Idx2).setReg(Reg1);
  110. MI->getOperand(Idx1).setReg(Reg2);
  111. MI->getOperand(Idx2).setSubReg(SubReg1);
  112. MI->getOperand(Idx1).setSubReg(SubReg2);
  113. MI->getOperand(Idx2).setIsKill(Reg1IsKill);
  114. MI->getOperand(Idx1).setIsKill(Reg2IsKill);
  115. return MI;
  116. }
  117. /// findCommutedOpIndices - If specified MI is commutable, return the two
  118. /// operand indices that would swap value. Return true if the instruction
  119. /// is not in a form which this routine understands.
  120. bool TargetInstrInfoImpl::findCommutedOpIndices(MachineInstr *MI,
  121. unsigned &SrcOpIdx1,
  122. unsigned &SrcOpIdx2) const {
  123. assert(!MI->isBundle() &&
  124. "TargetInstrInfoImpl::findCommutedOpIndices() can't handle bundles");
  125. const MCInstrDesc &MCID = MI->getDesc();
  126. if (!MCID.isCommutable())
  127. return false;
  128. // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this
  129. // is not true, then the target must implement this.
  130. SrcOpIdx1 = MCID.getNumDefs();
  131. SrcOpIdx2 = SrcOpIdx1 + 1;
  132. if (!MI->getOperand(SrcOpIdx1).isReg() ||
  133. !MI->getOperand(SrcOpIdx2).isReg())
  134. // No idea.
  135. return false;
  136. return true;
  137. }
  138. bool
  139. TargetInstrInfoImpl::isUnpredicatedTerminator(const MachineInstr *MI) const {
  140. if (!MI->isTerminator()) return false;
  141. // Conditional branch is a special case.
  142. if (MI->isBranch() && !MI->isBarrier())
  143. return true;
  144. if (!MI->isPredicable())
  145. return true;
  146. return !isPredicated(MI);
  147. }
  148. bool TargetInstrInfoImpl::PredicateInstruction(MachineInstr *MI,
  149. const SmallVectorImpl<MachineOperand> &Pred) const {
  150. bool MadeChange = false;
  151. assert(!MI->isBundle() &&
  152. "TargetInstrInfoImpl::PredicateInstruction() can't handle bundles");
  153. const MCInstrDesc &MCID = MI->getDesc();
  154. if (!MI->isPredicable())
  155. return false;
  156. for (unsigned j = 0, i = 0, e = MI->getNumOperands(); i != e; ++i) {
  157. if (MCID.OpInfo[i].isPredicate()) {
  158. MachineOperand &MO = MI->getOperand(i);
  159. if (MO.isReg()) {
  160. MO.setReg(Pred[j].getReg());
  161. MadeChange = true;
  162. } else if (MO.isImm()) {
  163. MO.setImm(Pred[j].getImm());
  164. MadeChange = true;
  165. } else if (MO.isMBB()) {
  166. MO.setMBB(Pred[j].getMBB());
  167. MadeChange = true;
  168. }
  169. ++j;
  170. }
  171. }
  172. return MadeChange;
  173. }
  174. bool TargetInstrInfoImpl::hasLoadFromStackSlot(const MachineInstr *MI,
  175. const MachineMemOperand *&MMO,
  176. int &FrameIndex) const {
  177. for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
  178. oe = MI->memoperands_end();
  179. o != oe;
  180. ++o) {
  181. if ((*o)->isLoad() && (*o)->getValue())
  182. if (const FixedStackPseudoSourceValue *Value =
  183. dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
  184. FrameIndex = Value->getFrameIndex();
  185. MMO = *o;
  186. return true;
  187. }
  188. }
  189. return false;
  190. }
  191. bool TargetInstrInfoImpl::hasStoreToStackSlot(const MachineInstr *MI,
  192. const MachineMemOperand *&MMO,
  193. int &FrameIndex) const {
  194. for (MachineInstr::mmo_iterator o = MI->memoperands_begin(),
  195. oe = MI->memoperands_end();
  196. o != oe;
  197. ++o) {
  198. if ((*o)->isStore() && (*o)->getValue())
  199. if (const FixedStackPseudoSourceValue *Value =
  200. dyn_cast<const FixedStackPseudoSourceValue>((*o)->getValue())) {
  201. FrameIndex = Value->getFrameIndex();
  202. MMO = *o;
  203. return true;
  204. }
  205. }
  206. return false;
  207. }
  208. void TargetInstrInfoImpl::reMaterialize(MachineBasicBlock &MBB,
  209. MachineBasicBlock::iterator I,
  210. unsigned DestReg,
  211. unsigned SubIdx,
  212. const MachineInstr *Orig,
  213. const TargetRegisterInfo &TRI) const {
  214. MachineInstr *MI = MBB.getParent()->CloneMachineInstr(Orig);
  215. MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI);
  216. MBB.insert(I, MI);
  217. }
  218. bool
  219. TargetInstrInfoImpl::produceSameValue(const MachineInstr *MI0,
  220. const MachineInstr *MI1,
  221. const MachineRegisterInfo *MRI) const {
  222. return MI0->isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
  223. }
  224. MachineInstr *TargetInstrInfoImpl::duplicate(MachineInstr *Orig,
  225. MachineFunction &MF) const {
  226. assert(!Orig->isNotDuplicable() &&
  227. "Instruction cannot be duplicated");
  228. return MF.CloneMachineInstr(Orig);
  229. }
  230. // If the COPY instruction in MI can be folded to a stack operation, return
  231. // the register class to use.
  232. static const TargetRegisterClass *canFoldCopy(const MachineInstr *MI,
  233. unsigned FoldIdx) {
  234. assert(MI->isCopy() && "MI must be a COPY instruction");
  235. if (MI->getNumOperands() != 2)
  236. return 0;
  237. assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand");
  238. const MachineOperand &FoldOp = MI->getOperand(FoldIdx);
  239. const MachineOperand &LiveOp = MI->getOperand(1-FoldIdx);
  240. if (FoldOp.getSubReg() || LiveOp.getSubReg())
  241. return 0;
  242. unsigned FoldReg = FoldOp.getReg();
  243. unsigned LiveReg = LiveOp.getReg();
  244. assert(TargetRegisterInfo::isVirtualRegister(FoldReg) &&
  245. "Cannot fold physregs");
  246. const MachineRegisterInfo &MRI = MI->getParent()->getParent()->getRegInfo();
  247. const TargetRegisterClass *RC = MRI.getRegClass(FoldReg);
  248. if (TargetRegisterInfo::isPhysicalRegister(LiveOp.getReg()))
  249. return RC->contains(LiveOp.getReg()) ? RC : 0;
  250. if (RC->hasSubClassEq(MRI.getRegClass(LiveReg)))
  251. return RC;
  252. // FIXME: Allow folding when register classes are memory compatible.
  253. return 0;
  254. }
  255. bool TargetInstrInfoImpl::
  256. canFoldMemoryOperand(const MachineInstr *MI,
  257. const SmallVectorImpl<unsigned> &Ops) const {
  258. return MI->isCopy() && Ops.size() == 1 && canFoldCopy(MI, Ops[0]);
  259. }
  260. /// foldMemoryOperand - Attempt to fold a load or store of the specified stack
  261. /// slot into the specified machine instruction for the specified operand(s).
  262. /// If this is possible, a new instruction is returned with the specified
  263. /// operand folded, otherwise NULL is returned. The client is responsible for
  264. /// removing the old instruction and adding the new one in the instruction
  265. /// stream.
  266. MachineInstr*
  267. TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI,
  268. const SmallVectorImpl<unsigned> &Ops,
  269. int FI) const {
  270. unsigned Flags = 0;
  271. for (unsigned i = 0, e = Ops.size(); i != e; ++i)
  272. if (MI->getOperand(Ops[i]).isDef())
  273. Flags |= MachineMemOperand::MOStore;
  274. else
  275. Flags |= MachineMemOperand::MOLoad;
  276. MachineBasicBlock *MBB = MI->getParent();
  277. assert(MBB && "foldMemoryOperand needs an inserted instruction");
  278. MachineFunction &MF = *MBB->getParent();
  279. // Ask the target to do the actual folding.
  280. if (MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, FI)) {
  281. // Add a memory operand, foldMemoryOperandImpl doesn't do that.
  282. assert((!(Flags & MachineMemOperand::MOStore) ||
  283. NewMI->mayStore()) &&
  284. "Folded a def to a non-store!");
  285. assert((!(Flags & MachineMemOperand::MOLoad) ||
  286. NewMI->mayLoad()) &&
  287. "Folded a use to a non-load!");
  288. const MachineFrameInfo &MFI = *MF.getFrameInfo();
  289. assert(MFI.getObjectOffset(FI) != -1);
  290. MachineMemOperand *MMO =
  291. MF.getMachineMemOperand(MachinePointerInfo::getFixedStack(FI),
  292. Flags, MFI.getObjectSize(FI),
  293. MFI.getObjectAlignment(FI));
  294. NewMI->addMemOperand(MF, MMO);
  295. // FIXME: change foldMemoryOperandImpl semantics to also insert NewMI.
  296. return MBB->insert(MI, NewMI);
  297. }
  298. // Straight COPY may fold as load/store.
  299. if (!MI->isCopy() || Ops.size() != 1)
  300. return 0;
  301. const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]);
  302. if (!RC)
  303. return 0;
  304. const MachineOperand &MO = MI->getOperand(1-Ops[0]);
  305. MachineBasicBlock::iterator Pos = MI;
  306. const TargetRegisterInfo *TRI = MF.getTarget().getRegisterInfo();
  307. if (Flags == MachineMemOperand::MOStore)
  308. storeRegToStackSlot(*MBB, Pos, MO.getReg(), MO.isKill(), FI, RC, TRI);
  309. else
  310. loadRegFromStackSlot(*MBB, Pos, MO.getReg(), FI, RC, TRI);
  311. return --Pos;
  312. }
  313. /// foldMemoryOperand - Same as the previous version except it allows folding
  314. /// of any load and store from / to any address, not just from a specific
  315. /// stack slot.
  316. MachineInstr*
  317. TargetInstrInfo::foldMemoryOperand(MachineBasicBlock::iterator MI,
  318. const SmallVectorImpl<unsigned> &Ops,
  319. MachineInstr* LoadMI) const {
  320. assert(LoadMI->canFoldAsLoad() && "LoadMI isn't foldable!");
  321. #ifndef NDEBUG
  322. for (unsigned i = 0, e = Ops.size(); i != e; ++i)
  323. assert(MI->getOperand(Ops[i]).isUse() && "Folding load into def!");
  324. #endif
  325. MachineBasicBlock &MBB = *MI->getParent();
  326. MachineFunction &MF = *MBB.getParent();
  327. // Ask the target to do the actual folding.
  328. MachineInstr *NewMI = foldMemoryOperandImpl(MF, MI, Ops, LoadMI);
  329. if (!NewMI) return 0;
  330. NewMI = MBB.insert(MI, NewMI);
  331. // Copy the memoperands from the load to the folded instruction.
  332. NewMI->setMemRefs(LoadMI->memoperands_begin(),
  333. LoadMI->memoperands_end());
  334. return NewMI;
  335. }
  336. bool TargetInstrInfo::
  337. isReallyTriviallyReMaterializableGeneric(const MachineInstr *MI,
  338. AliasAnalysis *AA) const {
  339. const MachineFunction &MF = *MI->getParent()->getParent();
  340. const MachineRegisterInfo &MRI = MF.getRegInfo();
  341. const TargetMachine &TM = MF.getTarget();
  342. const TargetInstrInfo &TII = *TM.getInstrInfo();
  343. // Remat clients assume operand 0 is the defined register.
  344. if (!MI->getNumOperands() || !MI->getOperand(0).isReg())
  345. return false;
  346. unsigned DefReg = MI->getOperand(0).getReg();
  347. // A sub-register definition can only be rematerialized if the instruction
  348. // doesn't read the other parts of the register. Otherwise it is really a
  349. // read-modify-write operation on the full virtual register which cannot be
  350. // moved safely.
  351. if (TargetRegisterInfo::isVirtualRegister(DefReg) &&
  352. MI->getOperand(0).getSubReg() && MI->readsVirtualRegister(DefReg))
  353. return false;
  354. // A load from a fixed stack slot can be rematerialized. This may be
  355. // redundant with subsequent checks, but it's target-independent,
  356. // simple, and a common case.
  357. int FrameIdx = 0;
  358. if (TII.isLoadFromStackSlot(MI, FrameIdx) &&
  359. MF.getFrameInfo()->isImmutableObjectIndex(FrameIdx))
  360. return true;
  361. // Avoid instructions obviously unsafe for remat.
  362. if (MI->isNotDuplicable() || MI->mayStore() ||
  363. MI->hasUnmodeledSideEffects())
  364. return false;
  365. // Don't remat inline asm. We have no idea how expensive it is
  366. // even if it's side effect free.
  367. if (MI->isInlineAsm())
  368. return false;
  369. // Avoid instructions which load from potentially varying memory.
  370. if (MI->mayLoad() && !MI->isInvariantLoad(AA))
  371. return false;
  372. // If any of the registers accessed are non-constant, conservatively assume
  373. // the instruction is not rematerializable.
  374. for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
  375. const MachineOperand &MO = MI->getOperand(i);
  376. if (!MO.isReg()) continue;
  377. unsigned Reg = MO.getReg();
  378. if (Reg == 0)
  379. continue;
  380. // Check for a well-behaved physical register.
  381. if (TargetRegisterInfo::isPhysicalRegister(Reg)) {
  382. if (MO.isUse()) {
  383. // If the physreg has no defs anywhere, it's just an ambient register
  384. // and we can freely move its uses. Alternatively, if it's allocatable,
  385. // it could get allocated to something with a def during allocation.
  386. if (!MRI.isConstantPhysReg(Reg, MF))
  387. return false;
  388. } else {
  389. // A physreg def. We can't remat it.
  390. return false;
  391. }
  392. continue;
  393. }
  394. // Only allow one virtual-register def. There may be multiple defs of the
  395. // same virtual register, though.
  396. if (MO.isDef() && Reg != DefReg)
  397. return false;
  398. // Don't allow any virtual-register uses. Rematting an instruction with
  399. // virtual register uses would length the live ranges of the uses, which
  400. // is not necessarily a good idea, certainly not "trivial".
  401. if (MO.isUse())
  402. return false;
  403. }
  404. // Everything checked out.
  405. return true;
  406. }
  407. /// isSchedulingBoundary - Test if the given instruction should be
  408. /// considered a scheduling boundary. This primarily includes labels
  409. /// and terminators.
  410. bool TargetInstrInfoImpl::isSchedulingBoundary(const MachineInstr *MI,
  411. const MachineBasicBlock *MBB,
  412. const MachineFunction &MF) const{
  413. // Terminators and labels can't be scheduled around.
  414. if (MI->isTerminator() || MI->isLabel())
  415. return true;
  416. // Don't attempt to schedule around any instruction that defines
  417. // a stack-oriented pointer, as it's unlikely to be profitable. This
  418. // saves compile time, because it doesn't require every single
  419. // stack slot reference to depend on the instruction that does the
  420. // modification.
  421. const TargetLowering &TLI = *MF.getTarget().getTargetLowering();
  422. if (MI->definesRegister(TLI.getStackPointerRegisterToSaveRestore()))
  423. return true;
  424. return false;
  425. }
  426. // Provide a global flag for disabling the PreRA hazard recognizer that targets
  427. // may choose to honor.
  428. bool TargetInstrInfoImpl::usePreRAHazardRecognizer() const {
  429. return !DisableHazardRecognizer;
  430. }
  431. // Default implementation of CreateTargetRAHazardRecognizer.
  432. ScheduleHazardRecognizer *TargetInstrInfoImpl::
  433. CreateTargetHazardRecognizer(const TargetMachine *TM,
  434. const ScheduleDAG *DAG) const {
  435. // Dummy hazard recognizer allows all instructions to issue.
  436. return new ScheduleHazardRecognizer();
  437. }
  438. // Default implementation of CreateTargetMIHazardRecognizer.
  439. ScheduleHazardRecognizer *TargetInstrInfoImpl::
  440. CreateTargetMIHazardRecognizer(const InstrItineraryData *II,
  441. const ScheduleDAG *DAG) const {
  442. return (ScheduleHazardRecognizer *)
  443. new ScoreboardHazardRecognizer(II, DAG, "misched");
  444. }
  445. // Default implementation of CreateTargetPostRAHazardRecognizer.
  446. ScheduleHazardRecognizer *TargetInstrInfoImpl::
  447. CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
  448. const ScheduleDAG *DAG) const {
  449. return (ScheduleHazardRecognizer *)
  450. new ScoreboardHazardRecognizer(II, DAG, "post-RA-sched");
  451. }
  452. //===----------------------------------------------------------------------===//
  453. // SelectionDAG latency interface.
  454. //===----------------------------------------------------------------------===//
  455. int
  456. TargetInstrInfoImpl::getOperandLatency(const InstrItineraryData *ItinData,
  457. SDNode *DefNode, unsigned DefIdx,
  458. SDNode *UseNode, unsigned UseIdx) const {
  459. if (!ItinData || ItinData->isEmpty())
  460. return -1;
  461. if (!DefNode->isMachineOpcode())
  462. return -1;
  463. unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass();
  464. if (!UseNode->isMachineOpcode())
  465. return ItinData->getOperandCycle(DefClass, DefIdx);
  466. unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass();
  467. return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
  468. }
  469. int TargetInstrInfoImpl::getInstrLatency(const InstrItineraryData *ItinData,
  470. SDNode *N) const {
  471. if (!ItinData || ItinData->isEmpty())
  472. return 1;
  473. if (!N->isMachineOpcode())
  474. return 1;
  475. return ItinData->getStageLatency(get(N->getMachineOpcode()).getSchedClass());
  476. }
  477. //===----------------------------------------------------------------------===//
  478. // MachineInstr latency interface.
  479. //===----------------------------------------------------------------------===//
  480. unsigned
  481. TargetInstrInfoImpl::getNumMicroOps(const InstrItineraryData *ItinData,
  482. const MachineInstr *MI) const {
  483. if (!ItinData || ItinData->isEmpty())
  484. return 1;
  485. unsigned Class = MI->getDesc().getSchedClass();
  486. int UOps = ItinData->Itineraries[Class].NumMicroOps;
  487. if (UOps >= 0)
  488. return UOps;
  489. // The # of u-ops is dynamically determined. The specific target should
  490. // override this function to return the right number.
  491. return 1;
  492. }
  493. /// Return the default expected latency for a def based on it's opcode.
  494. unsigned TargetInstrInfo::defaultDefLatency(const InstrItineraryData *ItinData,
  495. const MachineInstr *DefMI) const {
  496. if (DefMI->mayLoad())
  497. return ItinData->SchedModel->LoadLatency;
  498. if (isHighLatencyDef(DefMI->getOpcode()))
  499. return ItinData->SchedModel->HighLatency;
  500. return 1;
  501. }
  502. unsigned TargetInstrInfoImpl::
  503. getInstrLatency(const InstrItineraryData *ItinData,
  504. const MachineInstr *MI,
  505. unsigned *PredCost) const {
  506. // Default to one cycle for no itinerary. However, an "empty" itinerary may
  507. // still have a MinLatency property, which getStageLatency checks.
  508. if (!ItinData)
  509. return MI->mayLoad() ? 2 : 1;
  510. return ItinData->getStageLatency(MI->getDesc().getSchedClass());
  511. }
  512. bool TargetInstrInfoImpl::hasLowDefLatency(const InstrItineraryData *ItinData,
  513. const MachineInstr *DefMI,
  514. unsigned DefIdx) const {
  515. if (!ItinData || ItinData->isEmpty())
  516. return false;
  517. unsigned DefClass = DefMI->getDesc().getSchedClass();
  518. int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
  519. return (DefCycle != -1 && DefCycle <= 1);
  520. }
  521. /// Both DefMI and UseMI must be valid. By default, call directly to the
  522. /// itinerary. This may be overriden by the target.
  523. int TargetInstrInfoImpl::
  524. getOperandLatency(const InstrItineraryData *ItinData,
  525. const MachineInstr *DefMI, unsigned DefIdx,
  526. const MachineInstr *UseMI, unsigned UseIdx) const {
  527. unsigned DefClass = DefMI->getDesc().getSchedClass();
  528. unsigned UseClass = UseMI->getDesc().getSchedClass();
  529. return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
  530. }
  531. /// If we can determine the operand latency from the def only, without itinerary
  532. /// lookup, do so. Otherwise return -1.
  533. static int computeDefOperandLatency(
  534. const TargetInstrInfo *TII, const InstrItineraryData *ItinData,
  535. const MachineInstr *DefMI, bool FindMin) {
  536. // Let the target hook getInstrLatency handle missing itineraries.
  537. if (!ItinData)
  538. return TII->getInstrLatency(ItinData, DefMI);
  539. // Return a latency based on the itinerary properties and defining instruction
  540. // if possible. Some common subtargets don't require per-operand latency,
  541. // especially for minimum latencies.
  542. if (FindMin) {
  543. // If MinLatency is valid, call getInstrLatency. This uses Stage latency if
  544. // it exists before defaulting to MinLatency.
  545. if (ItinData->SchedModel->MinLatency >= 0)
  546. return TII->getInstrLatency(ItinData, DefMI);
  547. // If MinLatency is invalid, OperandLatency is interpreted as MinLatency.
  548. // For empty itineraries, short-cirtuit the check and default to one cycle.
  549. if (ItinData->isEmpty())
  550. return 1;
  551. }
  552. else if(ItinData->isEmpty())
  553. return TII->defaultDefLatency(ItinData, DefMI);
  554. // ...operand lookup required
  555. return -1;
  556. }
  557. /// computeOperandLatency - Compute and return the latency of the given data
  558. /// dependent def and use when the operand indices are already known.
  559. ///
  560. /// FindMin may be set to get the minimum vs. expected latency.
  561. unsigned TargetInstrInfo::
  562. computeOperandLatency(const InstrItineraryData *ItinData,
  563. const MachineInstr *DefMI, unsigned DefIdx,
  564. const MachineInstr *UseMI, unsigned UseIdx,
  565. bool FindMin) const {
  566. int DefLatency = computeDefOperandLatency(this, ItinData, DefMI, FindMin);
  567. if (DefLatency >= 0)
  568. return DefLatency;
  569. assert(ItinData && !ItinData->isEmpty() && "computeDefOperandLatency fail");
  570. int OperLatency = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, UseIdx);
  571. if (OperLatency >= 0)
  572. return OperLatency;
  573. // No operand latency was found.
  574. unsigned InstrLatency = getInstrLatency(ItinData, DefMI);
  575. // Expected latency is the max of the stage latency and itinerary props.
  576. if (!FindMin)
  577. InstrLatency = std::max(InstrLatency, defaultDefLatency(ItinData, DefMI));
  578. return InstrLatency;
  579. }
  580. /// computeOperandLatency - Compute and return the latency of the given data
  581. /// dependent def and use. DefMI must be a valid def. UseMI may be NULL for an
  582. /// unknown use. Depending on the subtarget's itinerary properties, this may or
  583. /// may not need to call getOperandLatency().
  584. ///
  585. /// FindMin may be set to get the minimum vs. expected latency. Minimum
  586. /// latency is used for scheduling groups, while expected latency is for
  587. /// instruction cost and critical path.
  588. ///
  589. /// For most subtargets, we don't need DefIdx or UseIdx to compute min latency.
  590. /// DefMI must be a valid definition, but UseMI may be NULL for an unknown use.
  591. unsigned TargetInstrInfo::
  592. computeOperandLatency(const InstrItineraryData *ItinData,
  593. const TargetRegisterInfo *TRI,
  594. const MachineInstr *DefMI, const MachineInstr *UseMI,
  595. unsigned Reg, bool FindMin) const {
  596. int DefLatency = computeDefOperandLatency(this, ItinData, DefMI, FindMin);
  597. if (DefLatency >= 0)
  598. return DefLatency;
  599. assert(ItinData && !ItinData->isEmpty() && "computeDefOperandLatency fail");
  600. // Find the definition of the register in the defining instruction.
  601. int DefIdx = DefMI->findRegisterDefOperandIdx(Reg);
  602. if (DefIdx != -1) {
  603. const MachineOperand &MO = DefMI->getOperand(DefIdx);
  604. if (MO.isReg() && MO.isImplicit() &&
  605. DefIdx >= (int)DefMI->getDesc().getNumOperands()) {
  606. // This is an implicit def, getOperandLatency() won't return the correct
  607. // latency. e.g.
  608. // %D6<def>, %D7<def> = VLD1q16 %R2<kill>, 0, ..., %Q3<imp-def>
  609. // %Q1<def> = VMULv8i16 %Q1<kill>, %Q3<kill>, ...
  610. // What we want is to compute latency between def of %D6/%D7 and use of
  611. // %Q3 instead.
  612. unsigned Op2 = DefMI->findRegisterDefOperandIdx(Reg, false, true, TRI);
  613. if (DefMI->getOperand(Op2).isReg())
  614. DefIdx = Op2;
  615. }
  616. // For all uses of the register, calculate the maxmimum latency
  617. int OperLatency = -1;
  618. // UseMI is null, then it must be a scheduling barrier.
  619. if (!UseMI) {
  620. unsigned DefClass = DefMI->getDesc().getSchedClass();
  621. OperLatency = ItinData->getOperandCycle(DefClass, DefIdx);
  622. }
  623. else {
  624. for (unsigned i = 0, e = UseMI->getNumOperands(); i != e; ++i) {
  625. const MachineOperand &MO = UseMI->getOperand(i);
  626. if (!MO.isReg() || !MO.isUse())
  627. continue;
  628. unsigned MOReg = MO.getReg();
  629. if (MOReg != Reg)
  630. continue;
  631. int UseCycle = getOperandLatency(ItinData, DefMI, DefIdx, UseMI, i);
  632. OperLatency = std::max(OperLatency, UseCycle);
  633. }
  634. }
  635. // If we found an operand latency, we're done.
  636. if (OperLatency >= 0)
  637. return OperLatency;
  638. }
  639. // No operand latency was found.
  640. unsigned InstrLatency = getInstrLatency(ItinData, DefMI);
  641. // Expected latency is the max of the stage latency and itinerary props.
  642. if (!FindMin)
  643. InstrLatency = std::max(InstrLatency, defaultDefLatency(ItinData, DefMI));
  644. return InstrLatency;
  645. }