TargetInstrInfo.cpp 53 KB

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