TargetInstrInfo.cpp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259
  1. //===-- TargetInstrInfo.cpp - Target Instruction Information --------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file implements the TargetInstrInfo class.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/CodeGen/TargetInstrInfo.h"
  13. #include "llvm/CodeGen/MachineFrameInfo.h"
  14. #include "llvm/CodeGen/MachineInstrBuilder.h"
  15. #include "llvm/CodeGen/MachineMemOperand.h"
  16. #include "llvm/CodeGen/MachineRegisterInfo.h"
  17. #include "llvm/CodeGen/PseudoSourceValue.h"
  18. #include "llvm/CodeGen/ScoreboardHazardRecognizer.h"
  19. #include "llvm/CodeGen/StackMaps.h"
  20. #include "llvm/CodeGen/TargetFrameLowering.h"
  21. #include "llvm/CodeGen/TargetLowering.h"
  22. #include "llvm/CodeGen/TargetRegisterInfo.h"
  23. #include "llvm/CodeGen/TargetSchedule.h"
  24. #include "llvm/IR/DataLayout.h"
  25. #include "llvm/IR/DebugInfoMetadata.h"
  26. #include "llvm/MC/MCAsmInfo.h"
  27. #include "llvm/MC/MCInstrItineraries.h"
  28. #include "llvm/Support/CommandLine.h"
  29. #include "llvm/Support/ErrorHandling.h"
  30. #include "llvm/Support/raw_ostream.h"
  31. #include "llvm/Target/TargetMachine.h"
  32. #include <cctype>
  33. using namespace llvm;
  34. static cl::opt<bool> DisableHazardRecognizer(
  35. "disable-sched-hazard", cl::Hidden, cl::init(false),
  36. cl::desc("Disable hazard detection during preRA scheduling"));
  37. TargetInstrInfo::~TargetInstrInfo() {
  38. }
  39. const TargetRegisterClass*
  40. TargetInstrInfo::getRegClass(const MCInstrDesc &MCID, unsigned OpNum,
  41. const TargetRegisterInfo *TRI,
  42. const MachineFunction &MF) const {
  43. if (OpNum >= MCID.getNumOperands())
  44. return nullptr;
  45. short RegClass = MCID.OpInfo[OpNum].RegClass;
  46. if (MCID.OpInfo[OpNum].isLookupPtrRegClass())
  47. return TRI->getPointerRegClass(MF, RegClass);
  48. // Instructions like INSERT_SUBREG do not have fixed register classes.
  49. if (RegClass < 0)
  50. return nullptr;
  51. // Otherwise just look it up normally.
  52. return TRI->getRegClass(RegClass);
  53. }
  54. /// insertNoop - Insert a noop into the instruction stream at the specified
  55. /// point.
  56. void TargetInstrInfo::insertNoop(MachineBasicBlock &MBB,
  57. MachineBasicBlock::iterator MI) const {
  58. llvm_unreachable("Target didn't implement insertNoop!");
  59. }
  60. static bool isAsmComment(const char *Str, const MCAsmInfo &MAI) {
  61. return strncmp(Str, MAI.getCommentString().data(),
  62. MAI.getCommentString().size()) == 0;
  63. }
  64. /// Measure the specified inline asm to determine an approximation of its
  65. /// length.
  66. /// Comments (which run till the next SeparatorString or newline) do not
  67. /// count as an instruction.
  68. /// Any other non-whitespace text is considered an instruction, with
  69. /// multiple instructions separated by SeparatorString or newlines.
  70. /// Variable-length instructions are not handled here; this function
  71. /// may be overloaded in the target code to do that.
  72. /// We implement a special case of the .space directive which takes only a
  73. /// single integer argument in base 10 that is the size in bytes. This is a
  74. /// restricted form of the GAS directive in that we only interpret
  75. /// simple--i.e. not a logical or arithmetic expression--size values without
  76. /// the optional fill value. This is primarily used for creating arbitrary
  77. /// sized inline asm blocks for testing purposes.
  78. unsigned TargetInstrInfo::getInlineAsmLength(
  79. const char *Str,
  80. const MCAsmInfo &MAI, const TargetSubtargetInfo *STI) const {
  81. // Count the number of instructions in the asm.
  82. bool AtInsnStart = true;
  83. unsigned Length = 0;
  84. const unsigned MaxInstLength = MAI.getMaxInstLength(STI);
  85. for (; *Str; ++Str) {
  86. if (*Str == '\n' || strncmp(Str, MAI.getSeparatorString(),
  87. strlen(MAI.getSeparatorString())) == 0) {
  88. AtInsnStart = true;
  89. } else if (isAsmComment(Str, MAI)) {
  90. // Stop counting as an instruction after a comment until the next
  91. // separator.
  92. AtInsnStart = false;
  93. }
  94. if (AtInsnStart && !std::isspace(static_cast<unsigned char>(*Str))) {
  95. unsigned AddLength = MaxInstLength;
  96. if (strncmp(Str, ".space", 6) == 0) {
  97. char *EStr;
  98. int SpaceSize;
  99. SpaceSize = strtol(Str + 6, &EStr, 10);
  100. SpaceSize = SpaceSize < 0 ? 0 : SpaceSize;
  101. while (*EStr != '\n' && std::isspace(static_cast<unsigned char>(*EStr)))
  102. ++EStr;
  103. if (*EStr == '\0' || *EStr == '\n' ||
  104. isAsmComment(EStr, MAI)) // Successfully parsed .space argument
  105. AddLength = SpaceSize;
  106. }
  107. Length += AddLength;
  108. AtInsnStart = false;
  109. }
  110. }
  111. return Length;
  112. }
  113. /// ReplaceTailWithBranchTo - Delete the instruction OldInst and everything
  114. /// after it, replacing it with an unconditional branch to NewDest.
  115. void
  116. TargetInstrInfo::ReplaceTailWithBranchTo(MachineBasicBlock::iterator Tail,
  117. MachineBasicBlock *NewDest) const {
  118. MachineBasicBlock *MBB = Tail->getParent();
  119. // Remove all the old successors of MBB from the CFG.
  120. while (!MBB->succ_empty())
  121. MBB->removeSuccessor(MBB->succ_begin());
  122. // Save off the debug loc before erasing the instruction.
  123. DebugLoc DL = Tail->getDebugLoc();
  124. // Update call site info and remove all the dead instructions
  125. // from the end of MBB.
  126. while (Tail != MBB->end()) {
  127. auto MI = Tail++;
  128. if (MI->isCall())
  129. MBB->getParent()->updateCallSiteInfo(&*MI);
  130. MBB->erase(MI);
  131. }
  132. // If MBB isn't immediately before MBB, insert a branch to it.
  133. if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(NewDest))
  134. insertBranch(*MBB, NewDest, nullptr, SmallVector<MachineOperand, 0>(), DL);
  135. MBB->addSuccessor(NewDest);
  136. }
  137. MachineInstr *TargetInstrInfo::commuteInstructionImpl(MachineInstr &MI,
  138. bool NewMI, unsigned Idx1,
  139. unsigned Idx2) const {
  140. const MCInstrDesc &MCID = MI.getDesc();
  141. bool HasDef = MCID.getNumDefs();
  142. if (HasDef && !MI.getOperand(0).isReg())
  143. // No idea how to commute this instruction. Target should implement its own.
  144. return nullptr;
  145. unsigned CommutableOpIdx1 = Idx1; (void)CommutableOpIdx1;
  146. unsigned CommutableOpIdx2 = Idx2; (void)CommutableOpIdx2;
  147. assert(findCommutedOpIndices(MI, CommutableOpIdx1, CommutableOpIdx2) &&
  148. CommutableOpIdx1 == Idx1 && CommutableOpIdx2 == Idx2 &&
  149. "TargetInstrInfo::CommuteInstructionImpl(): not commutable operands.");
  150. assert(MI.getOperand(Idx1).isReg() && MI.getOperand(Idx2).isReg() &&
  151. "This only knows how to commute register operands so far");
  152. Register Reg0 = HasDef ? MI.getOperand(0).getReg() : Register();
  153. Register Reg1 = MI.getOperand(Idx1).getReg();
  154. Register Reg2 = MI.getOperand(Idx2).getReg();
  155. unsigned SubReg0 = HasDef ? MI.getOperand(0).getSubReg() : 0;
  156. unsigned SubReg1 = MI.getOperand(Idx1).getSubReg();
  157. unsigned SubReg2 = MI.getOperand(Idx2).getSubReg();
  158. bool Reg1IsKill = MI.getOperand(Idx1).isKill();
  159. bool Reg2IsKill = MI.getOperand(Idx2).isKill();
  160. bool Reg1IsUndef = MI.getOperand(Idx1).isUndef();
  161. bool Reg2IsUndef = MI.getOperand(Idx2).isUndef();
  162. bool Reg1IsInternal = MI.getOperand(Idx1).isInternalRead();
  163. bool Reg2IsInternal = MI.getOperand(Idx2).isInternalRead();
  164. // Avoid calling isRenamable for virtual registers since we assert that
  165. // renamable property is only queried/set for physical registers.
  166. bool Reg1IsRenamable = Register::isPhysicalRegister(Reg1)
  167. ? MI.getOperand(Idx1).isRenamable()
  168. : false;
  169. bool Reg2IsRenamable = Register::isPhysicalRegister(Reg2)
  170. ? MI.getOperand(Idx2).isRenamable()
  171. : false;
  172. // If destination is tied to either of the commuted source register, then
  173. // it must be updated.
  174. if (HasDef && Reg0 == Reg1 &&
  175. MI.getDesc().getOperandConstraint(Idx1, MCOI::TIED_TO) == 0) {
  176. Reg2IsKill = false;
  177. Reg0 = Reg2;
  178. SubReg0 = SubReg2;
  179. } else if (HasDef && Reg0 == Reg2 &&
  180. MI.getDesc().getOperandConstraint(Idx2, MCOI::TIED_TO) == 0) {
  181. Reg1IsKill = false;
  182. Reg0 = Reg1;
  183. SubReg0 = SubReg1;
  184. }
  185. MachineInstr *CommutedMI = nullptr;
  186. if (NewMI) {
  187. // Create a new instruction.
  188. MachineFunction &MF = *MI.getMF();
  189. CommutedMI = MF.CloneMachineInstr(&MI);
  190. } else {
  191. CommutedMI = &MI;
  192. }
  193. if (HasDef) {
  194. CommutedMI->getOperand(0).setReg(Reg0);
  195. CommutedMI->getOperand(0).setSubReg(SubReg0);
  196. }
  197. CommutedMI->getOperand(Idx2).setReg(Reg1);
  198. CommutedMI->getOperand(Idx1).setReg(Reg2);
  199. CommutedMI->getOperand(Idx2).setSubReg(SubReg1);
  200. CommutedMI->getOperand(Idx1).setSubReg(SubReg2);
  201. CommutedMI->getOperand(Idx2).setIsKill(Reg1IsKill);
  202. CommutedMI->getOperand(Idx1).setIsKill(Reg2IsKill);
  203. CommutedMI->getOperand(Idx2).setIsUndef(Reg1IsUndef);
  204. CommutedMI->getOperand(Idx1).setIsUndef(Reg2IsUndef);
  205. CommutedMI->getOperand(Idx2).setIsInternalRead(Reg1IsInternal);
  206. CommutedMI->getOperand(Idx1).setIsInternalRead(Reg2IsInternal);
  207. // Avoid calling setIsRenamable for virtual registers since we assert that
  208. // renamable property is only queried/set for physical registers.
  209. if (Register::isPhysicalRegister(Reg1))
  210. CommutedMI->getOperand(Idx2).setIsRenamable(Reg1IsRenamable);
  211. if (Register::isPhysicalRegister(Reg2))
  212. CommutedMI->getOperand(Idx1).setIsRenamable(Reg2IsRenamable);
  213. return CommutedMI;
  214. }
  215. MachineInstr *TargetInstrInfo::commuteInstruction(MachineInstr &MI, bool NewMI,
  216. unsigned OpIdx1,
  217. unsigned OpIdx2) const {
  218. // If OpIdx1 or OpIdx2 is not specified, then this method is free to choose
  219. // any commutable operand, which is done in findCommutedOpIndices() method
  220. // called below.
  221. if ((OpIdx1 == CommuteAnyOperandIndex || OpIdx2 == CommuteAnyOperandIndex) &&
  222. !findCommutedOpIndices(MI, OpIdx1, OpIdx2)) {
  223. assert(MI.isCommutable() &&
  224. "Precondition violation: MI must be commutable.");
  225. return nullptr;
  226. }
  227. return commuteInstructionImpl(MI, NewMI, OpIdx1, OpIdx2);
  228. }
  229. bool TargetInstrInfo::fixCommutedOpIndices(unsigned &ResultIdx1,
  230. unsigned &ResultIdx2,
  231. unsigned CommutableOpIdx1,
  232. unsigned CommutableOpIdx2) {
  233. if (ResultIdx1 == CommuteAnyOperandIndex &&
  234. ResultIdx2 == CommuteAnyOperandIndex) {
  235. ResultIdx1 = CommutableOpIdx1;
  236. ResultIdx2 = CommutableOpIdx2;
  237. } else if (ResultIdx1 == CommuteAnyOperandIndex) {
  238. if (ResultIdx2 == CommutableOpIdx1)
  239. ResultIdx1 = CommutableOpIdx2;
  240. else if (ResultIdx2 == CommutableOpIdx2)
  241. ResultIdx1 = CommutableOpIdx1;
  242. else
  243. return false;
  244. } else if (ResultIdx2 == CommuteAnyOperandIndex) {
  245. if (ResultIdx1 == CommutableOpIdx1)
  246. ResultIdx2 = CommutableOpIdx2;
  247. else if (ResultIdx1 == CommutableOpIdx2)
  248. ResultIdx2 = CommutableOpIdx1;
  249. else
  250. return false;
  251. } else
  252. // Check that the result operand indices match the given commutable
  253. // operand indices.
  254. return (ResultIdx1 == CommutableOpIdx1 && ResultIdx2 == CommutableOpIdx2) ||
  255. (ResultIdx1 == CommutableOpIdx2 && ResultIdx2 == CommutableOpIdx1);
  256. return true;
  257. }
  258. bool TargetInstrInfo::findCommutedOpIndices(MachineInstr &MI,
  259. unsigned &SrcOpIdx1,
  260. unsigned &SrcOpIdx2) const {
  261. assert(!MI.isBundle() &&
  262. "TargetInstrInfo::findCommutedOpIndices() can't handle bundles");
  263. const MCInstrDesc &MCID = MI.getDesc();
  264. if (!MCID.isCommutable())
  265. return false;
  266. // This assumes v0 = op v1, v2 and commuting would swap v1 and v2. If this
  267. // is not true, then the target must implement this.
  268. unsigned CommutableOpIdx1 = MCID.getNumDefs();
  269. unsigned CommutableOpIdx2 = CommutableOpIdx1 + 1;
  270. if (!fixCommutedOpIndices(SrcOpIdx1, SrcOpIdx2,
  271. CommutableOpIdx1, CommutableOpIdx2))
  272. return false;
  273. if (!MI.getOperand(SrcOpIdx1).isReg() || !MI.getOperand(SrcOpIdx2).isReg())
  274. // No idea.
  275. return false;
  276. return true;
  277. }
  278. bool TargetInstrInfo::isUnpredicatedTerminator(const MachineInstr &MI) const {
  279. if (!MI.isTerminator()) return false;
  280. // Conditional branch is a special case.
  281. if (MI.isBranch() && !MI.isBarrier())
  282. return true;
  283. if (!MI.isPredicable())
  284. return true;
  285. return !isPredicated(MI);
  286. }
  287. bool TargetInstrInfo::PredicateInstruction(
  288. MachineInstr &MI, ArrayRef<MachineOperand> Pred) const {
  289. bool MadeChange = false;
  290. assert(!MI.isBundle() &&
  291. "TargetInstrInfo::PredicateInstruction() can't handle bundles");
  292. const MCInstrDesc &MCID = MI.getDesc();
  293. if (!MI.isPredicable())
  294. return false;
  295. for (unsigned j = 0, i = 0, e = MI.getNumOperands(); i != e; ++i) {
  296. if (MCID.OpInfo[i].isPredicate()) {
  297. MachineOperand &MO = MI.getOperand(i);
  298. if (MO.isReg()) {
  299. MO.setReg(Pred[j].getReg());
  300. MadeChange = true;
  301. } else if (MO.isImm()) {
  302. MO.setImm(Pred[j].getImm());
  303. MadeChange = true;
  304. } else if (MO.isMBB()) {
  305. MO.setMBB(Pred[j].getMBB());
  306. MadeChange = true;
  307. }
  308. ++j;
  309. }
  310. }
  311. return MadeChange;
  312. }
  313. bool TargetInstrInfo::hasLoadFromStackSlot(
  314. const MachineInstr &MI,
  315. SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
  316. size_t StartSize = Accesses.size();
  317. for (MachineInstr::mmo_iterator o = MI.memoperands_begin(),
  318. oe = MI.memoperands_end();
  319. o != oe; ++o) {
  320. if ((*o)->isLoad() &&
  321. dyn_cast_or_null<FixedStackPseudoSourceValue>((*o)->getPseudoValue()))
  322. Accesses.push_back(*o);
  323. }
  324. return Accesses.size() != StartSize;
  325. }
  326. bool TargetInstrInfo::hasStoreToStackSlot(
  327. const MachineInstr &MI,
  328. SmallVectorImpl<const MachineMemOperand *> &Accesses) const {
  329. size_t StartSize = Accesses.size();
  330. for (MachineInstr::mmo_iterator o = MI.memoperands_begin(),
  331. oe = MI.memoperands_end();
  332. o != oe; ++o) {
  333. if ((*o)->isStore() &&
  334. dyn_cast_or_null<FixedStackPseudoSourceValue>((*o)->getPseudoValue()))
  335. Accesses.push_back(*o);
  336. }
  337. return Accesses.size() != StartSize;
  338. }
  339. bool TargetInstrInfo::getStackSlotRange(const TargetRegisterClass *RC,
  340. unsigned SubIdx, unsigned &Size,
  341. unsigned &Offset,
  342. const MachineFunction &MF) const {
  343. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  344. if (!SubIdx) {
  345. Size = TRI->getSpillSize(*RC);
  346. Offset = 0;
  347. return true;
  348. }
  349. unsigned BitSize = TRI->getSubRegIdxSize(SubIdx);
  350. // Convert bit size to byte size.
  351. if (BitSize % 8)
  352. return false;
  353. int BitOffset = TRI->getSubRegIdxOffset(SubIdx);
  354. if (BitOffset < 0 || BitOffset % 8)
  355. return false;
  356. Size = BitSize /= 8;
  357. Offset = (unsigned)BitOffset / 8;
  358. assert(TRI->getSpillSize(*RC) >= (Offset + Size) && "bad subregister range");
  359. if (!MF.getDataLayout().isLittleEndian()) {
  360. Offset = TRI->getSpillSize(*RC) - (Offset + Size);
  361. }
  362. return true;
  363. }
  364. void TargetInstrInfo::reMaterialize(MachineBasicBlock &MBB,
  365. MachineBasicBlock::iterator I,
  366. unsigned DestReg, unsigned SubIdx,
  367. const MachineInstr &Orig,
  368. const TargetRegisterInfo &TRI) const {
  369. MachineInstr *MI = MBB.getParent()->CloneMachineInstr(&Orig);
  370. MI->substituteRegister(MI->getOperand(0).getReg(), DestReg, SubIdx, TRI);
  371. MBB.insert(I, MI);
  372. }
  373. bool TargetInstrInfo::produceSameValue(const MachineInstr &MI0,
  374. const MachineInstr &MI1,
  375. const MachineRegisterInfo *MRI) const {
  376. return MI0.isIdenticalTo(MI1, MachineInstr::IgnoreVRegDefs);
  377. }
  378. MachineInstr &TargetInstrInfo::duplicate(MachineBasicBlock &MBB,
  379. MachineBasicBlock::iterator InsertBefore, const MachineInstr &Orig) const {
  380. assert(!Orig.isNotDuplicable() && "Instruction cannot be duplicated");
  381. MachineFunction &MF = *MBB.getParent();
  382. return MF.CloneMachineInstrBundle(MBB, InsertBefore, Orig);
  383. }
  384. // If the COPY instruction in MI can be folded to a stack operation, return
  385. // the register class to use.
  386. static const TargetRegisterClass *canFoldCopy(const MachineInstr &MI,
  387. unsigned FoldIdx) {
  388. assert(MI.isCopy() && "MI must be a COPY instruction");
  389. if (MI.getNumOperands() != 2)
  390. return nullptr;
  391. assert(FoldIdx<2 && "FoldIdx refers no nonexistent operand");
  392. const MachineOperand &FoldOp = MI.getOperand(FoldIdx);
  393. const MachineOperand &LiveOp = MI.getOperand(1 - FoldIdx);
  394. if (FoldOp.getSubReg() || LiveOp.getSubReg())
  395. return nullptr;
  396. Register FoldReg = FoldOp.getReg();
  397. Register LiveReg = LiveOp.getReg();
  398. assert(Register::isVirtualRegister(FoldReg) && "Cannot fold physregs");
  399. const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
  400. const TargetRegisterClass *RC = MRI.getRegClass(FoldReg);
  401. if (Register::isPhysicalRegister(LiveOp.getReg()))
  402. return RC->contains(LiveOp.getReg()) ? RC : nullptr;
  403. if (RC->hasSubClassEq(MRI.getRegClass(LiveReg)))
  404. return RC;
  405. // FIXME: Allow folding when register classes are memory compatible.
  406. return nullptr;
  407. }
  408. void TargetInstrInfo::getNoop(MCInst &NopInst) const {
  409. llvm_unreachable("Not implemented");
  410. }
  411. static MachineInstr *foldPatchpoint(MachineFunction &MF, MachineInstr &MI,
  412. ArrayRef<unsigned> Ops, int FrameIndex,
  413. const TargetInstrInfo &TII) {
  414. unsigned StartIdx = 0;
  415. switch (MI.getOpcode()) {
  416. case TargetOpcode::STACKMAP: {
  417. // StackMapLiveValues are foldable
  418. StartIdx = StackMapOpers(&MI).getVarIdx();
  419. break;
  420. }
  421. case TargetOpcode::PATCHPOINT: {
  422. // For PatchPoint, the call args are not foldable (even if reported in the
  423. // stackmap e.g. via anyregcc).
  424. StartIdx = PatchPointOpers(&MI).getVarIdx();
  425. break;
  426. }
  427. case TargetOpcode::STATEPOINT: {
  428. // For statepoints, fold deopt and gc arguments, but not call arguments.
  429. StartIdx = StatepointOpers(&MI).getVarIdx();
  430. break;
  431. }
  432. default:
  433. llvm_unreachable("unexpected stackmap opcode");
  434. }
  435. // Return false if any operands requested for folding are not foldable (not
  436. // part of the stackmap's live values).
  437. for (unsigned Op : Ops) {
  438. if (Op < StartIdx)
  439. return nullptr;
  440. }
  441. MachineInstr *NewMI =
  442. MF.CreateMachineInstr(TII.get(MI.getOpcode()), MI.getDebugLoc(), true);
  443. MachineInstrBuilder MIB(MF, NewMI);
  444. // No need to fold return, the meta data, and function arguments
  445. for (unsigned i = 0; i < StartIdx; ++i)
  446. MIB.add(MI.getOperand(i));
  447. for (unsigned i = StartIdx; i < MI.getNumOperands(); ++i) {
  448. MachineOperand &MO = MI.getOperand(i);
  449. if (is_contained(Ops, i)) {
  450. unsigned SpillSize;
  451. unsigned SpillOffset;
  452. // Compute the spill slot size and offset.
  453. const TargetRegisterClass *RC =
  454. MF.getRegInfo().getRegClass(MO.getReg());
  455. bool Valid =
  456. TII.getStackSlotRange(RC, MO.getSubReg(), SpillSize, SpillOffset, MF);
  457. if (!Valid)
  458. report_fatal_error("cannot spill patchpoint subregister operand");
  459. MIB.addImm(StackMaps::IndirectMemRefOp);
  460. MIB.addImm(SpillSize);
  461. MIB.addFrameIndex(FrameIndex);
  462. MIB.addImm(SpillOffset);
  463. }
  464. else
  465. MIB.add(MO);
  466. }
  467. return NewMI;
  468. }
  469. MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
  470. ArrayRef<unsigned> Ops, int FI,
  471. LiveIntervals *LIS,
  472. VirtRegMap *VRM) const {
  473. auto Flags = MachineMemOperand::MONone;
  474. for (unsigned OpIdx : Ops)
  475. Flags |= MI.getOperand(OpIdx).isDef() ? MachineMemOperand::MOStore
  476. : MachineMemOperand::MOLoad;
  477. MachineBasicBlock *MBB = MI.getParent();
  478. assert(MBB && "foldMemoryOperand needs an inserted instruction");
  479. MachineFunction &MF = *MBB->getParent();
  480. // If we're not folding a load into a subreg, the size of the load is the
  481. // size of the spill slot. But if we are, we need to figure out what the
  482. // actual load size is.
  483. int64_t MemSize = 0;
  484. const MachineFrameInfo &MFI = MF.getFrameInfo();
  485. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  486. if (Flags & MachineMemOperand::MOStore) {
  487. MemSize = MFI.getObjectSize(FI);
  488. } else {
  489. for (unsigned OpIdx : Ops) {
  490. int64_t OpSize = MFI.getObjectSize(FI);
  491. if (auto SubReg = MI.getOperand(OpIdx).getSubReg()) {
  492. unsigned SubRegSize = TRI->getSubRegIdxSize(SubReg);
  493. if (SubRegSize > 0 && !(SubRegSize % 8))
  494. OpSize = SubRegSize / 8;
  495. }
  496. MemSize = std::max(MemSize, OpSize);
  497. }
  498. }
  499. assert(MemSize && "Did not expect a zero-sized stack slot");
  500. MachineInstr *NewMI = nullptr;
  501. if (MI.getOpcode() == TargetOpcode::STACKMAP ||
  502. MI.getOpcode() == TargetOpcode::PATCHPOINT ||
  503. MI.getOpcode() == TargetOpcode::STATEPOINT) {
  504. // Fold stackmap/patchpoint.
  505. NewMI = foldPatchpoint(MF, MI, Ops, FI, *this);
  506. if (NewMI)
  507. MBB->insert(MI, NewMI);
  508. } else {
  509. // Ask the target to do the actual folding.
  510. NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, FI, LIS, VRM);
  511. }
  512. if (NewMI) {
  513. NewMI->setMemRefs(MF, MI.memoperands());
  514. // Add a memory operand, foldMemoryOperandImpl doesn't do that.
  515. assert((!(Flags & MachineMemOperand::MOStore) ||
  516. NewMI->mayStore()) &&
  517. "Folded a def to a non-store!");
  518. assert((!(Flags & MachineMemOperand::MOLoad) ||
  519. NewMI->mayLoad()) &&
  520. "Folded a use to a non-load!");
  521. assert(MFI.getObjectOffset(FI) != -1);
  522. MachineMemOperand *MMO = MF.getMachineMemOperand(
  523. MachinePointerInfo::getFixedStack(MF, FI), Flags, MemSize,
  524. MFI.getObjectAlignment(FI));
  525. NewMI->addMemOperand(MF, MMO);
  526. return NewMI;
  527. }
  528. // Straight COPY may fold as load/store.
  529. if (!MI.isCopy() || Ops.size() != 1)
  530. return nullptr;
  531. const TargetRegisterClass *RC = canFoldCopy(MI, Ops[0]);
  532. if (!RC)
  533. return nullptr;
  534. const MachineOperand &MO = MI.getOperand(1 - Ops[0]);
  535. MachineBasicBlock::iterator Pos = MI;
  536. if (Flags == MachineMemOperand::MOStore)
  537. storeRegToStackSlot(*MBB, Pos, MO.getReg(), MO.isKill(), FI, RC, TRI);
  538. else
  539. loadRegFromStackSlot(*MBB, Pos, MO.getReg(), FI, RC, TRI);
  540. return &*--Pos;
  541. }
  542. MachineInstr *TargetInstrInfo::foldMemoryOperand(MachineInstr &MI,
  543. ArrayRef<unsigned> Ops,
  544. MachineInstr &LoadMI,
  545. LiveIntervals *LIS) const {
  546. assert(LoadMI.canFoldAsLoad() && "LoadMI isn't foldable!");
  547. #ifndef NDEBUG
  548. for (unsigned OpIdx : Ops)
  549. assert(MI.getOperand(OpIdx).isUse() && "Folding load into def!");
  550. #endif
  551. MachineBasicBlock &MBB = *MI.getParent();
  552. MachineFunction &MF = *MBB.getParent();
  553. // Ask the target to do the actual folding.
  554. MachineInstr *NewMI = nullptr;
  555. int FrameIndex = 0;
  556. if ((MI.getOpcode() == TargetOpcode::STACKMAP ||
  557. MI.getOpcode() == TargetOpcode::PATCHPOINT ||
  558. MI.getOpcode() == TargetOpcode::STATEPOINT) &&
  559. isLoadFromStackSlot(LoadMI, FrameIndex)) {
  560. // Fold stackmap/patchpoint.
  561. NewMI = foldPatchpoint(MF, MI, Ops, FrameIndex, *this);
  562. if (NewMI)
  563. NewMI = &*MBB.insert(MI, NewMI);
  564. } else {
  565. // Ask the target to do the actual folding.
  566. NewMI = foldMemoryOperandImpl(MF, MI, Ops, MI, LoadMI, LIS);
  567. }
  568. if (!NewMI)
  569. return nullptr;
  570. // Copy the memoperands from the load to the folded instruction.
  571. if (MI.memoperands_empty()) {
  572. NewMI->setMemRefs(MF, LoadMI.memoperands());
  573. } else {
  574. // Handle the rare case of folding multiple loads.
  575. NewMI->setMemRefs(MF, MI.memoperands());
  576. for (MachineInstr::mmo_iterator I = LoadMI.memoperands_begin(),
  577. E = LoadMI.memoperands_end();
  578. I != E; ++I) {
  579. NewMI->addMemOperand(MF, *I);
  580. }
  581. }
  582. return NewMI;
  583. }
  584. bool TargetInstrInfo::hasReassociableOperands(
  585. const MachineInstr &Inst, const MachineBasicBlock *MBB) const {
  586. const MachineOperand &Op1 = Inst.getOperand(1);
  587. const MachineOperand &Op2 = Inst.getOperand(2);
  588. const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
  589. // We need virtual register definitions for the operands that we will
  590. // reassociate.
  591. MachineInstr *MI1 = nullptr;
  592. MachineInstr *MI2 = nullptr;
  593. if (Op1.isReg() && Register::isVirtualRegister(Op1.getReg()))
  594. MI1 = MRI.getUniqueVRegDef(Op1.getReg());
  595. if (Op2.isReg() && Register::isVirtualRegister(Op2.getReg()))
  596. MI2 = MRI.getUniqueVRegDef(Op2.getReg());
  597. // And they need to be in the trace (otherwise, they won't have a depth).
  598. return MI1 && MI2 && MI1->getParent() == MBB && MI2->getParent() == MBB;
  599. }
  600. bool TargetInstrInfo::hasReassociableSibling(const MachineInstr &Inst,
  601. bool &Commuted) const {
  602. const MachineBasicBlock *MBB = Inst.getParent();
  603. const MachineRegisterInfo &MRI = MBB->getParent()->getRegInfo();
  604. MachineInstr *MI1 = MRI.getUniqueVRegDef(Inst.getOperand(1).getReg());
  605. MachineInstr *MI2 = MRI.getUniqueVRegDef(Inst.getOperand(2).getReg());
  606. unsigned AssocOpcode = Inst.getOpcode();
  607. // If only one operand has the same opcode and it's the second source operand,
  608. // the operands must be commuted.
  609. Commuted = MI1->getOpcode() != AssocOpcode && MI2->getOpcode() == AssocOpcode;
  610. if (Commuted)
  611. std::swap(MI1, MI2);
  612. // 1. The previous instruction must be the same type as Inst.
  613. // 2. The previous instruction must have virtual register definitions for its
  614. // operands in the same basic block as Inst.
  615. // 3. The previous instruction's result must only be used by Inst.
  616. return MI1->getOpcode() == AssocOpcode &&
  617. hasReassociableOperands(*MI1, MBB) &&
  618. MRI.hasOneNonDBGUse(MI1->getOperand(0).getReg());
  619. }
  620. // 1. The operation must be associative and commutative.
  621. // 2. The instruction must have virtual register definitions for its
  622. // operands in the same basic block.
  623. // 3. The instruction must have a reassociable sibling.
  624. bool TargetInstrInfo::isReassociationCandidate(const MachineInstr &Inst,
  625. bool &Commuted) const {
  626. return isAssociativeAndCommutative(Inst) &&
  627. hasReassociableOperands(Inst, Inst.getParent()) &&
  628. hasReassociableSibling(Inst, Commuted);
  629. }
  630. // The concept of the reassociation pass is that these operations can benefit
  631. // from this kind of transformation:
  632. //
  633. // A = ? op ?
  634. // B = A op X (Prev)
  635. // C = B op Y (Root)
  636. // -->
  637. // A = ? op ?
  638. // B = X op Y
  639. // C = A op B
  640. //
  641. // breaking the dependency between A and B, allowing them to be executed in
  642. // parallel (or back-to-back in a pipeline) instead of depending on each other.
  643. // FIXME: This has the potential to be expensive (compile time) while not
  644. // improving the code at all. Some ways to limit the overhead:
  645. // 1. Track successful transforms; bail out if hit rate gets too low.
  646. // 2. Only enable at -O3 or some other non-default optimization level.
  647. // 3. Pre-screen pattern candidates here: if an operand of the previous
  648. // instruction is known to not increase the critical path, then don't match
  649. // that pattern.
  650. bool TargetInstrInfo::getMachineCombinerPatterns(
  651. MachineInstr &Root,
  652. SmallVectorImpl<MachineCombinerPattern> &Patterns) const {
  653. bool Commute;
  654. if (isReassociationCandidate(Root, Commute)) {
  655. // We found a sequence of instructions that may be suitable for a
  656. // reassociation of operands to increase ILP. Specify each commutation
  657. // possibility for the Prev instruction in the sequence and let the
  658. // machine combiner decide if changing the operands is worthwhile.
  659. if (Commute) {
  660. Patterns.push_back(MachineCombinerPattern::REASSOC_AX_YB);
  661. Patterns.push_back(MachineCombinerPattern::REASSOC_XA_YB);
  662. } else {
  663. Patterns.push_back(MachineCombinerPattern::REASSOC_AX_BY);
  664. Patterns.push_back(MachineCombinerPattern::REASSOC_XA_BY);
  665. }
  666. return true;
  667. }
  668. return false;
  669. }
  670. /// Return true when a code sequence can improve loop throughput.
  671. bool
  672. TargetInstrInfo::isThroughputPattern(MachineCombinerPattern Pattern) const {
  673. return false;
  674. }
  675. /// Attempt the reassociation transformation to reduce critical path length.
  676. /// See the above comments before getMachineCombinerPatterns().
  677. void TargetInstrInfo::reassociateOps(
  678. MachineInstr &Root, MachineInstr &Prev,
  679. MachineCombinerPattern Pattern,
  680. SmallVectorImpl<MachineInstr *> &InsInstrs,
  681. SmallVectorImpl<MachineInstr *> &DelInstrs,
  682. DenseMap<unsigned, unsigned> &InstrIdxForVirtReg) const {
  683. MachineFunction *MF = Root.getMF();
  684. MachineRegisterInfo &MRI = MF->getRegInfo();
  685. const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
  686. const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo();
  687. const TargetRegisterClass *RC = Root.getRegClassConstraint(0, TII, TRI);
  688. // This array encodes the operand index for each parameter because the
  689. // operands may be commuted. Each row corresponds to a pattern value,
  690. // and each column specifies the index of A, B, X, Y.
  691. unsigned OpIdx[4][4] = {
  692. { 1, 1, 2, 2 },
  693. { 1, 2, 2, 1 },
  694. { 2, 1, 1, 2 },
  695. { 2, 2, 1, 1 }
  696. };
  697. int Row;
  698. switch (Pattern) {
  699. case MachineCombinerPattern::REASSOC_AX_BY: Row = 0; break;
  700. case MachineCombinerPattern::REASSOC_AX_YB: Row = 1; break;
  701. case MachineCombinerPattern::REASSOC_XA_BY: Row = 2; break;
  702. case MachineCombinerPattern::REASSOC_XA_YB: Row = 3; break;
  703. default: llvm_unreachable("unexpected MachineCombinerPattern");
  704. }
  705. MachineOperand &OpA = Prev.getOperand(OpIdx[Row][0]);
  706. MachineOperand &OpB = Root.getOperand(OpIdx[Row][1]);
  707. MachineOperand &OpX = Prev.getOperand(OpIdx[Row][2]);
  708. MachineOperand &OpY = Root.getOperand(OpIdx[Row][3]);
  709. MachineOperand &OpC = Root.getOperand(0);
  710. Register RegA = OpA.getReg();
  711. Register RegB = OpB.getReg();
  712. Register RegX = OpX.getReg();
  713. Register RegY = OpY.getReg();
  714. Register RegC = OpC.getReg();
  715. if (Register::isVirtualRegister(RegA))
  716. MRI.constrainRegClass(RegA, RC);
  717. if (Register::isVirtualRegister(RegB))
  718. MRI.constrainRegClass(RegB, RC);
  719. if (Register::isVirtualRegister(RegX))
  720. MRI.constrainRegClass(RegX, RC);
  721. if (Register::isVirtualRegister(RegY))
  722. MRI.constrainRegClass(RegY, RC);
  723. if (Register::isVirtualRegister(RegC))
  724. MRI.constrainRegClass(RegC, RC);
  725. // Create a new virtual register for the result of (X op Y) instead of
  726. // recycling RegB because the MachineCombiner's computation of the critical
  727. // path requires a new register definition rather than an existing one.
  728. Register NewVR = MRI.createVirtualRegister(RC);
  729. InstrIdxForVirtReg.insert(std::make_pair(NewVR, 0));
  730. unsigned Opcode = Root.getOpcode();
  731. bool KillA = OpA.isKill();
  732. bool KillX = OpX.isKill();
  733. bool KillY = OpY.isKill();
  734. // Create new instructions for insertion.
  735. MachineInstrBuilder MIB1 =
  736. BuildMI(*MF, Prev.getDebugLoc(), TII->get(Opcode), NewVR)
  737. .addReg(RegX, getKillRegState(KillX))
  738. .addReg(RegY, getKillRegState(KillY));
  739. MachineInstrBuilder MIB2 =
  740. BuildMI(*MF, Root.getDebugLoc(), TII->get(Opcode), RegC)
  741. .addReg(RegA, getKillRegState(KillA))
  742. .addReg(NewVR, getKillRegState(true));
  743. setSpecialOperandAttr(Root, Prev, *MIB1, *MIB2);
  744. // Record new instructions for insertion and old instructions for deletion.
  745. InsInstrs.push_back(MIB1);
  746. InsInstrs.push_back(MIB2);
  747. DelInstrs.push_back(&Prev);
  748. DelInstrs.push_back(&Root);
  749. }
  750. void TargetInstrInfo::genAlternativeCodeSequence(
  751. MachineInstr &Root, MachineCombinerPattern Pattern,
  752. SmallVectorImpl<MachineInstr *> &InsInstrs,
  753. SmallVectorImpl<MachineInstr *> &DelInstrs,
  754. DenseMap<unsigned, unsigned> &InstIdxForVirtReg) const {
  755. MachineRegisterInfo &MRI = Root.getMF()->getRegInfo();
  756. // Select the previous instruction in the sequence based on the input pattern.
  757. MachineInstr *Prev = nullptr;
  758. switch (Pattern) {
  759. case MachineCombinerPattern::REASSOC_AX_BY:
  760. case MachineCombinerPattern::REASSOC_XA_BY:
  761. Prev = MRI.getUniqueVRegDef(Root.getOperand(1).getReg());
  762. break;
  763. case MachineCombinerPattern::REASSOC_AX_YB:
  764. case MachineCombinerPattern::REASSOC_XA_YB:
  765. Prev = MRI.getUniqueVRegDef(Root.getOperand(2).getReg());
  766. break;
  767. default:
  768. break;
  769. }
  770. assert(Prev && "Unknown pattern for machine combiner");
  771. reassociateOps(Root, *Prev, Pattern, InsInstrs, DelInstrs, InstIdxForVirtReg);
  772. }
  773. bool TargetInstrInfo::isReallyTriviallyReMaterializableGeneric(
  774. const MachineInstr &MI, AliasAnalysis *AA) const {
  775. const MachineFunction &MF = *MI.getMF();
  776. const MachineRegisterInfo &MRI = MF.getRegInfo();
  777. // Remat clients assume operand 0 is the defined register.
  778. if (!MI.getNumOperands() || !MI.getOperand(0).isReg())
  779. return false;
  780. Register DefReg = MI.getOperand(0).getReg();
  781. // A sub-register definition can only be rematerialized if the instruction
  782. // doesn't read the other parts of the register. Otherwise it is really a
  783. // read-modify-write operation on the full virtual register which cannot be
  784. // moved safely.
  785. if (Register::isVirtualRegister(DefReg) && MI.getOperand(0).getSubReg() &&
  786. MI.readsVirtualRegister(DefReg))
  787. return false;
  788. // A load from a fixed stack slot can be rematerialized. This may be
  789. // redundant with subsequent checks, but it's target-independent,
  790. // simple, and a common case.
  791. int FrameIdx = 0;
  792. if (isLoadFromStackSlot(MI, FrameIdx) &&
  793. MF.getFrameInfo().isImmutableObjectIndex(FrameIdx))
  794. return true;
  795. // Avoid instructions obviously unsafe for remat.
  796. if (MI.isNotDuplicable() || MI.mayStore() || MI.mayRaiseFPException() ||
  797. MI.hasUnmodeledSideEffects())
  798. return false;
  799. // Don't remat inline asm. We have no idea how expensive it is
  800. // even if it's side effect free.
  801. if (MI.isInlineAsm())
  802. return false;
  803. // Avoid instructions which load from potentially varying memory.
  804. if (MI.mayLoad() && !MI.isDereferenceableInvariantLoad(AA))
  805. return false;
  806. // If any of the registers accessed are non-constant, conservatively assume
  807. // the instruction is not rematerializable.
  808. for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
  809. const MachineOperand &MO = MI.getOperand(i);
  810. if (!MO.isReg()) continue;
  811. Register Reg = MO.getReg();
  812. if (Reg == 0)
  813. continue;
  814. // Check for a well-behaved physical register.
  815. if (Register::isPhysicalRegister(Reg)) {
  816. if (MO.isUse()) {
  817. // If the physreg has no defs anywhere, it's just an ambient register
  818. // and we can freely move its uses. Alternatively, if it's allocatable,
  819. // it could get allocated to something with a def during allocation.
  820. if (!MRI.isConstantPhysReg(Reg))
  821. return false;
  822. } else {
  823. // A physreg def. We can't remat it.
  824. return false;
  825. }
  826. continue;
  827. }
  828. // Only allow one virtual-register def. There may be multiple defs of the
  829. // same virtual register, though.
  830. if (MO.isDef() && Reg != DefReg)
  831. return false;
  832. // Don't allow any virtual-register uses. Rematting an instruction with
  833. // virtual register uses would length the live ranges of the uses, which
  834. // is not necessarily a good idea, certainly not "trivial".
  835. if (MO.isUse())
  836. return false;
  837. }
  838. // Everything checked out.
  839. return true;
  840. }
  841. int TargetInstrInfo::getSPAdjust(const MachineInstr &MI) const {
  842. const MachineFunction *MF = MI.getMF();
  843. const TargetFrameLowering *TFI = MF->getSubtarget().getFrameLowering();
  844. bool StackGrowsDown =
  845. TFI->getStackGrowthDirection() == TargetFrameLowering::StackGrowsDown;
  846. unsigned FrameSetupOpcode = getCallFrameSetupOpcode();
  847. unsigned FrameDestroyOpcode = getCallFrameDestroyOpcode();
  848. if (!isFrameInstr(MI))
  849. return 0;
  850. int SPAdj = TFI->alignSPAdjust(getFrameSize(MI));
  851. if ((!StackGrowsDown && MI.getOpcode() == FrameSetupOpcode) ||
  852. (StackGrowsDown && MI.getOpcode() == FrameDestroyOpcode))
  853. SPAdj = -SPAdj;
  854. return SPAdj;
  855. }
  856. /// isSchedulingBoundary - Test if the given instruction should be
  857. /// considered a scheduling boundary. This primarily includes labels
  858. /// and terminators.
  859. bool TargetInstrInfo::isSchedulingBoundary(const MachineInstr &MI,
  860. const MachineBasicBlock *MBB,
  861. const MachineFunction &MF) const {
  862. // Terminators and labels can't be scheduled around.
  863. if (MI.isTerminator() || MI.isPosition())
  864. return true;
  865. // Don't attempt to schedule around any instruction that defines
  866. // a stack-oriented pointer, as it's unlikely to be profitable. This
  867. // saves compile time, because it doesn't require every single
  868. // stack slot reference to depend on the instruction that does the
  869. // modification.
  870. const TargetLowering &TLI = *MF.getSubtarget().getTargetLowering();
  871. const TargetRegisterInfo *TRI = MF.getSubtarget().getRegisterInfo();
  872. return MI.modifiesRegister(TLI.getStackPointerRegisterToSaveRestore(), TRI);
  873. }
  874. // Provide a global flag for disabling the PreRA hazard recognizer that targets
  875. // may choose to honor.
  876. bool TargetInstrInfo::usePreRAHazardRecognizer() const {
  877. return !DisableHazardRecognizer;
  878. }
  879. // Default implementation of CreateTargetRAHazardRecognizer.
  880. ScheduleHazardRecognizer *TargetInstrInfo::
  881. CreateTargetHazardRecognizer(const TargetSubtargetInfo *STI,
  882. const ScheduleDAG *DAG) const {
  883. // Dummy hazard recognizer allows all instructions to issue.
  884. return new ScheduleHazardRecognizer();
  885. }
  886. // Default implementation of CreateTargetMIHazardRecognizer.
  887. ScheduleHazardRecognizer *TargetInstrInfo::
  888. CreateTargetMIHazardRecognizer(const InstrItineraryData *II,
  889. const ScheduleDAG *DAG) const {
  890. return (ScheduleHazardRecognizer *)
  891. new ScoreboardHazardRecognizer(II, DAG, "machine-scheduler");
  892. }
  893. // Default implementation of CreateTargetPostRAHazardRecognizer.
  894. ScheduleHazardRecognizer *TargetInstrInfo::
  895. CreateTargetPostRAHazardRecognizer(const InstrItineraryData *II,
  896. const ScheduleDAG *DAG) const {
  897. return (ScheduleHazardRecognizer *)
  898. new ScoreboardHazardRecognizer(II, DAG, "post-RA-sched");
  899. }
  900. //===----------------------------------------------------------------------===//
  901. // SelectionDAG latency interface.
  902. //===----------------------------------------------------------------------===//
  903. int
  904. TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
  905. SDNode *DefNode, unsigned DefIdx,
  906. SDNode *UseNode, unsigned UseIdx) const {
  907. if (!ItinData || ItinData->isEmpty())
  908. return -1;
  909. if (!DefNode->isMachineOpcode())
  910. return -1;
  911. unsigned DefClass = get(DefNode->getMachineOpcode()).getSchedClass();
  912. if (!UseNode->isMachineOpcode())
  913. return ItinData->getOperandCycle(DefClass, DefIdx);
  914. unsigned UseClass = get(UseNode->getMachineOpcode()).getSchedClass();
  915. return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
  916. }
  917. int TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
  918. SDNode *N) const {
  919. if (!ItinData || ItinData->isEmpty())
  920. return 1;
  921. if (!N->isMachineOpcode())
  922. return 1;
  923. return ItinData->getStageLatency(get(N->getMachineOpcode()).getSchedClass());
  924. }
  925. //===----------------------------------------------------------------------===//
  926. // MachineInstr latency interface.
  927. //===----------------------------------------------------------------------===//
  928. unsigned TargetInstrInfo::getNumMicroOps(const InstrItineraryData *ItinData,
  929. const MachineInstr &MI) const {
  930. if (!ItinData || ItinData->isEmpty())
  931. return 1;
  932. unsigned Class = MI.getDesc().getSchedClass();
  933. int UOps = ItinData->Itineraries[Class].NumMicroOps;
  934. if (UOps >= 0)
  935. return UOps;
  936. // The # of u-ops is dynamically determined. The specific target should
  937. // override this function to return the right number.
  938. return 1;
  939. }
  940. /// Return the default expected latency for a def based on it's opcode.
  941. unsigned TargetInstrInfo::defaultDefLatency(const MCSchedModel &SchedModel,
  942. const MachineInstr &DefMI) const {
  943. if (DefMI.isTransient())
  944. return 0;
  945. if (DefMI.mayLoad())
  946. return SchedModel.LoadLatency;
  947. if (isHighLatencyDef(DefMI.getOpcode()))
  948. return SchedModel.HighLatency;
  949. return 1;
  950. }
  951. unsigned TargetInstrInfo::getPredicationCost(const MachineInstr &) const {
  952. return 0;
  953. }
  954. unsigned TargetInstrInfo::getInstrLatency(const InstrItineraryData *ItinData,
  955. const MachineInstr &MI,
  956. unsigned *PredCost) const {
  957. // Default to one cycle for no itinerary. However, an "empty" itinerary may
  958. // still have a MinLatency property, which getStageLatency checks.
  959. if (!ItinData)
  960. return MI.mayLoad() ? 2 : 1;
  961. return ItinData->getStageLatency(MI.getDesc().getSchedClass());
  962. }
  963. bool TargetInstrInfo::hasLowDefLatency(const TargetSchedModel &SchedModel,
  964. const MachineInstr &DefMI,
  965. unsigned DefIdx) const {
  966. const InstrItineraryData *ItinData = SchedModel.getInstrItineraries();
  967. if (!ItinData || ItinData->isEmpty())
  968. return false;
  969. unsigned DefClass = DefMI.getDesc().getSchedClass();
  970. int DefCycle = ItinData->getOperandCycle(DefClass, DefIdx);
  971. return (DefCycle != -1 && DefCycle <= 1);
  972. }
  973. Optional<ParamLoadedValue>
  974. TargetInstrInfo::describeLoadedValue(const MachineInstr &MI) const {
  975. const MachineFunction *MF = MI.getMF();
  976. const MachineOperand *Op = nullptr;
  977. DIExpression *Expr = DIExpression::get(MF->getFunction().getContext(), {});;
  978. const MachineOperand *SrcRegOp, *DestRegOp;
  979. if (isCopyInstr(MI, SrcRegOp, DestRegOp)) {
  980. Op = SrcRegOp;
  981. return ParamLoadedValue(*Op, Expr);
  982. } else if (MI.isMoveImmediate()) {
  983. Op = &MI.getOperand(1);
  984. return ParamLoadedValue(*Op, Expr);
  985. } else if (MI.hasOneMemOperand()) {
  986. int64_t Offset;
  987. const auto &TRI = MF->getSubtarget().getRegisterInfo();
  988. const auto &TII = MF->getSubtarget().getInstrInfo();
  989. const MachineOperand *BaseOp;
  990. if (!TII->getMemOperandWithOffset(MI, BaseOp, Offset, TRI))
  991. return None;
  992. Expr = DIExpression::prepend(Expr, DIExpression::DerefAfter, Offset);
  993. Op = BaseOp;
  994. return ParamLoadedValue(*Op, Expr);
  995. }
  996. return None;
  997. }
  998. /// Both DefMI and UseMI must be valid. By default, call directly to the
  999. /// itinerary. This may be overriden by the target.
  1000. int TargetInstrInfo::getOperandLatency(const InstrItineraryData *ItinData,
  1001. const MachineInstr &DefMI,
  1002. unsigned DefIdx,
  1003. const MachineInstr &UseMI,
  1004. unsigned UseIdx) const {
  1005. unsigned DefClass = DefMI.getDesc().getSchedClass();
  1006. unsigned UseClass = UseMI.getDesc().getSchedClass();
  1007. return ItinData->getOperandLatency(DefClass, DefIdx, UseClass, UseIdx);
  1008. }
  1009. /// If we can determine the operand latency from the def only, without itinerary
  1010. /// lookup, do so. Otherwise return -1.
  1011. int TargetInstrInfo::computeDefOperandLatency(
  1012. const InstrItineraryData *ItinData, const MachineInstr &DefMI) const {
  1013. // Let the target hook getInstrLatency handle missing itineraries.
  1014. if (!ItinData)
  1015. return getInstrLatency(ItinData, DefMI);
  1016. if(ItinData->isEmpty())
  1017. return defaultDefLatency(ItinData->SchedModel, DefMI);
  1018. // ...operand lookup required
  1019. return -1;
  1020. }
  1021. bool TargetInstrInfo::getRegSequenceInputs(
  1022. const MachineInstr &MI, unsigned DefIdx,
  1023. SmallVectorImpl<RegSubRegPairAndIdx> &InputRegs) const {
  1024. assert((MI.isRegSequence() ||
  1025. MI.isRegSequenceLike()) && "Instruction do not have the proper type");
  1026. if (!MI.isRegSequence())
  1027. return getRegSequenceLikeInputs(MI, DefIdx, InputRegs);
  1028. // We are looking at:
  1029. // Def = REG_SEQUENCE v0, sub0, v1, sub1, ...
  1030. assert(DefIdx == 0 && "REG_SEQUENCE only has one def");
  1031. for (unsigned OpIdx = 1, EndOpIdx = MI.getNumOperands(); OpIdx != EndOpIdx;
  1032. OpIdx += 2) {
  1033. const MachineOperand &MOReg = MI.getOperand(OpIdx);
  1034. if (MOReg.isUndef())
  1035. continue;
  1036. const MachineOperand &MOSubIdx = MI.getOperand(OpIdx + 1);
  1037. assert(MOSubIdx.isImm() &&
  1038. "One of the subindex of the reg_sequence is not an immediate");
  1039. // Record Reg:SubReg, SubIdx.
  1040. InputRegs.push_back(RegSubRegPairAndIdx(MOReg.getReg(), MOReg.getSubReg(),
  1041. (unsigned)MOSubIdx.getImm()));
  1042. }
  1043. return true;
  1044. }
  1045. bool TargetInstrInfo::getExtractSubregInputs(
  1046. const MachineInstr &MI, unsigned DefIdx,
  1047. RegSubRegPairAndIdx &InputReg) const {
  1048. assert((MI.isExtractSubreg() ||
  1049. MI.isExtractSubregLike()) && "Instruction do not have the proper type");
  1050. if (!MI.isExtractSubreg())
  1051. return getExtractSubregLikeInputs(MI, DefIdx, InputReg);
  1052. // We are looking at:
  1053. // Def = EXTRACT_SUBREG v0.sub1, sub0.
  1054. assert(DefIdx == 0 && "EXTRACT_SUBREG only has one def");
  1055. const MachineOperand &MOReg = MI.getOperand(1);
  1056. if (MOReg.isUndef())
  1057. return false;
  1058. const MachineOperand &MOSubIdx = MI.getOperand(2);
  1059. assert(MOSubIdx.isImm() &&
  1060. "The subindex of the extract_subreg is not an immediate");
  1061. InputReg.Reg = MOReg.getReg();
  1062. InputReg.SubReg = MOReg.getSubReg();
  1063. InputReg.SubIdx = (unsigned)MOSubIdx.getImm();
  1064. return true;
  1065. }
  1066. bool TargetInstrInfo::getInsertSubregInputs(
  1067. const MachineInstr &MI, unsigned DefIdx,
  1068. RegSubRegPair &BaseReg, RegSubRegPairAndIdx &InsertedReg) const {
  1069. assert((MI.isInsertSubreg() ||
  1070. MI.isInsertSubregLike()) && "Instruction do not have the proper type");
  1071. if (!MI.isInsertSubreg())
  1072. return getInsertSubregLikeInputs(MI, DefIdx, BaseReg, InsertedReg);
  1073. // We are looking at:
  1074. // Def = INSERT_SEQUENCE v0, v1, sub0.
  1075. assert(DefIdx == 0 && "INSERT_SUBREG only has one def");
  1076. const MachineOperand &MOBaseReg = MI.getOperand(1);
  1077. const MachineOperand &MOInsertedReg = MI.getOperand(2);
  1078. if (MOInsertedReg.isUndef())
  1079. return false;
  1080. const MachineOperand &MOSubIdx = MI.getOperand(3);
  1081. assert(MOSubIdx.isImm() &&
  1082. "One of the subindex of the reg_sequence is not an immediate");
  1083. BaseReg.Reg = MOBaseReg.getReg();
  1084. BaseReg.SubReg = MOBaseReg.getSubReg();
  1085. InsertedReg.Reg = MOInsertedReg.getReg();
  1086. InsertedReg.SubReg = MOInsertedReg.getSubReg();
  1087. InsertedReg.SubIdx = (unsigned)MOSubIdx.getImm();
  1088. return true;
  1089. }