TargetInstrInfo.cpp 58 KB

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