MachineInstr.h 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932
  1. //===- llvm/CodeGen/MachineInstr.h - MachineInstr class ---------*- C++ -*-===//
  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 contains the declaration of the MachineInstr class, which is the
  10. // basic representation for all target dependent machine instructions used by
  11. // the back end.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_CODEGEN_MACHINEINSTR_H
  15. #define LLVM_CODEGEN_MACHINEINSTR_H
  16. #include "llvm/ADT/DenseMapInfo.h"
  17. #include "llvm/ADT/PointerSumType.h"
  18. #include "llvm/ADT/SmallSet.h"
  19. #include "llvm/ADT/ilist.h"
  20. #include "llvm/ADT/ilist_node.h"
  21. #include "llvm/ADT/iterator_range.h"
  22. #include "llvm/CodeGen/MachineMemOperand.h"
  23. #include "llvm/CodeGen/MachineOperand.h"
  24. #include "llvm/CodeGen/TargetOpcodes.h"
  25. #include "llvm/IR/DebugLoc.h"
  26. #include "llvm/IR/InlineAsm.h"
  27. #include "llvm/IR/PseudoProbe.h"
  28. #include "llvm/MC/MCInstrDesc.h"
  29. #include "llvm/MC/MCSymbol.h"
  30. #include "llvm/Support/ArrayRecycler.h"
  31. #include "llvm/Support/TrailingObjects.h"
  32. #include <algorithm>
  33. #include <cassert>
  34. #include <cstdint>
  35. #include <utility>
  36. namespace llvm {
  37. class AAResults;
  38. template <typename T> class ArrayRef;
  39. class DIExpression;
  40. class DILocalVariable;
  41. class MachineBasicBlock;
  42. class MachineFunction;
  43. class MachineRegisterInfo;
  44. class ModuleSlotTracker;
  45. class raw_ostream;
  46. template <typename T> class SmallVectorImpl;
  47. class SmallBitVector;
  48. class StringRef;
  49. class TargetInstrInfo;
  50. class TargetRegisterClass;
  51. class TargetRegisterInfo;
  52. //===----------------------------------------------------------------------===//
  53. /// Representation of each machine instruction.
  54. ///
  55. /// This class isn't a POD type, but it must have a trivial destructor. When a
  56. /// MachineFunction is deleted, all the contained MachineInstrs are deallocated
  57. /// without having their destructor called.
  58. ///
  59. class MachineInstr
  60. : public ilist_node_with_parent<MachineInstr, MachineBasicBlock,
  61. ilist_sentinel_tracking<true>> {
  62. public:
  63. using mmo_iterator = ArrayRef<MachineMemOperand *>::iterator;
  64. /// Flags to specify different kinds of comments to output in
  65. /// assembly code. These flags carry semantic information not
  66. /// otherwise easily derivable from the IR text.
  67. ///
  68. enum CommentFlag {
  69. ReloadReuse = 0x1, // higher bits are reserved for target dep comments.
  70. NoSchedComment = 0x2,
  71. TAsmComments = 0x4 // Target Asm comments should start from this value.
  72. };
  73. enum MIFlag {
  74. NoFlags = 0,
  75. FrameSetup = 1 << 0, // Instruction is used as a part of
  76. // function frame setup code.
  77. FrameDestroy = 1 << 1, // Instruction is used as a part of
  78. // function frame destruction code.
  79. BundledPred = 1 << 2, // Instruction has bundled predecessors.
  80. BundledSucc = 1 << 3, // Instruction has bundled successors.
  81. FmNoNans = 1 << 4, // Instruction does not support Fast
  82. // math nan values.
  83. FmNoInfs = 1 << 5, // Instruction does not support Fast
  84. // math infinity values.
  85. FmNsz = 1 << 6, // Instruction is not required to retain
  86. // signed zero values.
  87. FmArcp = 1 << 7, // Instruction supports Fast math
  88. // reciprocal approximations.
  89. FmContract = 1 << 8, // Instruction supports Fast math
  90. // contraction operations like fma.
  91. FmAfn = 1 << 9, // Instruction may map to Fast math
  92. // instrinsic approximation.
  93. FmReassoc = 1 << 10, // Instruction supports Fast math
  94. // reassociation of operand order.
  95. NoUWrap = 1 << 11, // Instruction supports binary operator
  96. // no unsigned wrap.
  97. NoSWrap = 1 << 12, // Instruction supports binary operator
  98. // no signed wrap.
  99. IsExact = 1 << 13, // Instruction supports division is
  100. // known to be exact.
  101. NoFPExcept = 1 << 14, // Instruction does not raise
  102. // floatint-point exceptions.
  103. NoMerge = 1 << 15, // Passes that drop source location info
  104. // (e.g. branch folding) should skip
  105. // this instruction.
  106. };
  107. private:
  108. const MCInstrDesc *MCID; // Instruction descriptor.
  109. MachineBasicBlock *Parent = nullptr; // Pointer to the owning basic block.
  110. // Operands are allocated by an ArrayRecycler.
  111. MachineOperand *Operands = nullptr; // Pointer to the first operand.
  112. unsigned NumOperands = 0; // Number of operands on instruction.
  113. uint16_t Flags = 0; // Various bits of additional
  114. // information about machine
  115. // instruction.
  116. uint8_t AsmPrinterFlags = 0; // Various bits of information used by
  117. // the AsmPrinter to emit helpful
  118. // comments. This is *not* semantic
  119. // information. Do not use this for
  120. // anything other than to convey comment
  121. // information to AsmPrinter.
  122. // OperandCapacity has uint8_t size, so it should be next to AsmPrinterFlags
  123. // to properly pack.
  124. using OperandCapacity = ArrayRecycler<MachineOperand>::Capacity;
  125. OperandCapacity CapOperands; // Capacity of the Operands array.
  126. /// Internal implementation detail class that provides out-of-line storage for
  127. /// extra info used by the machine instruction when this info cannot be stored
  128. /// in-line within the instruction itself.
  129. ///
  130. /// This has to be defined eagerly due to the implementation constraints of
  131. /// `PointerSumType` where it is used.
  132. class ExtraInfo final
  133. : TrailingObjects<ExtraInfo, MachineMemOperand *, MCSymbol *, MDNode *> {
  134. public:
  135. static ExtraInfo *create(BumpPtrAllocator &Allocator,
  136. ArrayRef<MachineMemOperand *> MMOs,
  137. MCSymbol *PreInstrSymbol = nullptr,
  138. MCSymbol *PostInstrSymbol = nullptr,
  139. MDNode *HeapAllocMarker = nullptr) {
  140. bool HasPreInstrSymbol = PreInstrSymbol != nullptr;
  141. bool HasPostInstrSymbol = PostInstrSymbol != nullptr;
  142. bool HasHeapAllocMarker = HeapAllocMarker != nullptr;
  143. auto *Result = new (Allocator.Allocate(
  144. totalSizeToAlloc<MachineMemOperand *, MCSymbol *, MDNode *>(
  145. MMOs.size(), HasPreInstrSymbol + HasPostInstrSymbol,
  146. HasHeapAllocMarker),
  147. alignof(ExtraInfo)))
  148. ExtraInfo(MMOs.size(), HasPreInstrSymbol, HasPostInstrSymbol,
  149. HasHeapAllocMarker);
  150. // Copy the actual data into the trailing objects.
  151. std::copy(MMOs.begin(), MMOs.end(),
  152. Result->getTrailingObjects<MachineMemOperand *>());
  153. if (HasPreInstrSymbol)
  154. Result->getTrailingObjects<MCSymbol *>()[0] = PreInstrSymbol;
  155. if (HasPostInstrSymbol)
  156. Result->getTrailingObjects<MCSymbol *>()[HasPreInstrSymbol] =
  157. PostInstrSymbol;
  158. if (HasHeapAllocMarker)
  159. Result->getTrailingObjects<MDNode *>()[0] = HeapAllocMarker;
  160. return Result;
  161. }
  162. ArrayRef<MachineMemOperand *> getMMOs() const {
  163. return makeArrayRef(getTrailingObjects<MachineMemOperand *>(), NumMMOs);
  164. }
  165. MCSymbol *getPreInstrSymbol() const {
  166. return HasPreInstrSymbol ? getTrailingObjects<MCSymbol *>()[0] : nullptr;
  167. }
  168. MCSymbol *getPostInstrSymbol() const {
  169. return HasPostInstrSymbol
  170. ? getTrailingObjects<MCSymbol *>()[HasPreInstrSymbol]
  171. : nullptr;
  172. }
  173. MDNode *getHeapAllocMarker() const {
  174. return HasHeapAllocMarker ? getTrailingObjects<MDNode *>()[0] : nullptr;
  175. }
  176. private:
  177. friend TrailingObjects;
  178. // Description of the extra info, used to interpret the actual optional
  179. // data appended.
  180. //
  181. // Note that this is not terribly space optimized. This leaves a great deal
  182. // of flexibility to fit more in here later.
  183. const int NumMMOs;
  184. const bool HasPreInstrSymbol;
  185. const bool HasPostInstrSymbol;
  186. const bool HasHeapAllocMarker;
  187. // Implement the `TrailingObjects` internal API.
  188. size_t numTrailingObjects(OverloadToken<MachineMemOperand *>) const {
  189. return NumMMOs;
  190. }
  191. size_t numTrailingObjects(OverloadToken<MCSymbol *>) const {
  192. return HasPreInstrSymbol + HasPostInstrSymbol;
  193. }
  194. size_t numTrailingObjects(OverloadToken<MDNode *>) const {
  195. return HasHeapAllocMarker;
  196. }
  197. // Just a boring constructor to allow us to initialize the sizes. Always use
  198. // the `create` routine above.
  199. ExtraInfo(int NumMMOs, bool HasPreInstrSymbol, bool HasPostInstrSymbol,
  200. bool HasHeapAllocMarker)
  201. : NumMMOs(NumMMOs), HasPreInstrSymbol(HasPreInstrSymbol),
  202. HasPostInstrSymbol(HasPostInstrSymbol),
  203. HasHeapAllocMarker(HasHeapAllocMarker) {}
  204. };
  205. /// Enumeration of the kinds of inline extra info available. It is important
  206. /// that the `MachineMemOperand` inline kind has a tag value of zero to make
  207. /// it accessible as an `ArrayRef`.
  208. enum ExtraInfoInlineKinds {
  209. EIIK_MMO = 0,
  210. EIIK_PreInstrSymbol,
  211. EIIK_PostInstrSymbol,
  212. EIIK_OutOfLine
  213. };
  214. // We store extra information about the instruction here. The common case is
  215. // expected to be nothing or a single pointer (typically a MMO or a symbol).
  216. // We work to optimize this common case by storing it inline here rather than
  217. // requiring a separate allocation, but we fall back to an allocation when
  218. // multiple pointers are needed.
  219. PointerSumType<ExtraInfoInlineKinds,
  220. PointerSumTypeMember<EIIK_MMO, MachineMemOperand *>,
  221. PointerSumTypeMember<EIIK_PreInstrSymbol, MCSymbol *>,
  222. PointerSumTypeMember<EIIK_PostInstrSymbol, MCSymbol *>,
  223. PointerSumTypeMember<EIIK_OutOfLine, ExtraInfo *>>
  224. Info;
  225. DebugLoc debugLoc; // Source line information.
  226. /// Unique instruction number. Used by DBG_INSTR_REFs to refer to the values
  227. /// defined by this instruction.
  228. unsigned DebugInstrNum;
  229. // Intrusive list support
  230. friend struct ilist_traits<MachineInstr>;
  231. friend struct ilist_callback_traits<MachineBasicBlock>;
  232. void setParent(MachineBasicBlock *P) { Parent = P; }
  233. /// This constructor creates a copy of the given
  234. /// MachineInstr in the given MachineFunction.
  235. MachineInstr(MachineFunction &, const MachineInstr &);
  236. /// This constructor create a MachineInstr and add the implicit operands.
  237. /// It reserves space for number of operands specified by
  238. /// MCInstrDesc. An explicit DebugLoc is supplied.
  239. MachineInstr(MachineFunction &, const MCInstrDesc &tid, DebugLoc dl,
  240. bool NoImp = false);
  241. // MachineInstrs are pool-allocated and owned by MachineFunction.
  242. friend class MachineFunction;
  243. void
  244. dumprImpl(const MachineRegisterInfo &MRI, unsigned Depth, unsigned MaxDepth,
  245. SmallPtrSetImpl<const MachineInstr *> &AlreadySeenInstrs) const;
  246. public:
  247. MachineInstr(const MachineInstr &) = delete;
  248. MachineInstr &operator=(const MachineInstr &) = delete;
  249. // Use MachineFunction::DeleteMachineInstr() instead.
  250. ~MachineInstr() = delete;
  251. const MachineBasicBlock* getParent() const { return Parent; }
  252. MachineBasicBlock* getParent() { return Parent; }
  253. /// Move the instruction before \p MovePos.
  254. void moveBefore(MachineInstr *MovePos);
  255. /// Return the function that contains the basic block that this instruction
  256. /// belongs to.
  257. ///
  258. /// Note: this is undefined behaviour if the instruction does not have a
  259. /// parent.
  260. const MachineFunction *getMF() const;
  261. MachineFunction *getMF() {
  262. return const_cast<MachineFunction *>(
  263. static_cast<const MachineInstr *>(this)->getMF());
  264. }
  265. /// Return the asm printer flags bitvector.
  266. uint8_t getAsmPrinterFlags() const { return AsmPrinterFlags; }
  267. /// Clear the AsmPrinter bitvector.
  268. void clearAsmPrinterFlags() { AsmPrinterFlags = 0; }
  269. /// Return whether an AsmPrinter flag is set.
  270. bool getAsmPrinterFlag(CommentFlag Flag) const {
  271. return AsmPrinterFlags & Flag;
  272. }
  273. /// Set a flag for the AsmPrinter.
  274. void setAsmPrinterFlag(uint8_t Flag) {
  275. AsmPrinterFlags |= Flag;
  276. }
  277. /// Clear specific AsmPrinter flags.
  278. void clearAsmPrinterFlag(CommentFlag Flag) {
  279. AsmPrinterFlags &= ~Flag;
  280. }
  281. /// Return the MI flags bitvector.
  282. uint16_t getFlags() const {
  283. return Flags;
  284. }
  285. /// Return whether an MI flag is set.
  286. bool getFlag(MIFlag Flag) const {
  287. return Flags & Flag;
  288. }
  289. /// Set a MI flag.
  290. void setFlag(MIFlag Flag) {
  291. Flags |= (uint16_t)Flag;
  292. }
  293. void setFlags(unsigned flags) {
  294. // Filter out the automatically maintained flags.
  295. unsigned Mask = BundledPred | BundledSucc;
  296. Flags = (Flags & Mask) | (flags & ~Mask);
  297. }
  298. /// clearFlag - Clear a MI flag.
  299. void clearFlag(MIFlag Flag) {
  300. Flags &= ~((uint16_t)Flag);
  301. }
  302. /// Return true if MI is in a bundle (but not the first MI in a bundle).
  303. ///
  304. /// A bundle looks like this before it's finalized:
  305. /// ----------------
  306. /// | MI |
  307. /// ----------------
  308. /// |
  309. /// ----------------
  310. /// | MI * |
  311. /// ----------------
  312. /// |
  313. /// ----------------
  314. /// | MI * |
  315. /// ----------------
  316. /// In this case, the first MI starts a bundle but is not inside a bundle, the
  317. /// next 2 MIs are considered "inside" the bundle.
  318. ///
  319. /// After a bundle is finalized, it looks like this:
  320. /// ----------------
  321. /// | Bundle |
  322. /// ----------------
  323. /// |
  324. /// ----------------
  325. /// | MI * |
  326. /// ----------------
  327. /// |
  328. /// ----------------
  329. /// | MI * |
  330. /// ----------------
  331. /// |
  332. /// ----------------
  333. /// | MI * |
  334. /// ----------------
  335. /// The first instruction has the special opcode "BUNDLE". It's not "inside"
  336. /// a bundle, but the next three MIs are.
  337. bool isInsideBundle() const {
  338. return getFlag(BundledPred);
  339. }
  340. /// Return true if this instruction part of a bundle. This is true
  341. /// if either itself or its following instruction is marked "InsideBundle".
  342. bool isBundled() const {
  343. return isBundledWithPred() || isBundledWithSucc();
  344. }
  345. /// Return true if this instruction is part of a bundle, and it is not the
  346. /// first instruction in the bundle.
  347. bool isBundledWithPred() const { return getFlag(BundledPred); }
  348. /// Return true if this instruction is part of a bundle, and it is not the
  349. /// last instruction in the bundle.
  350. bool isBundledWithSucc() const { return getFlag(BundledSucc); }
  351. /// Bundle this instruction with its predecessor. This can be an unbundled
  352. /// instruction, or it can be the first instruction in a bundle.
  353. void bundleWithPred();
  354. /// Bundle this instruction with its successor. This can be an unbundled
  355. /// instruction, or it can be the last instruction in a bundle.
  356. void bundleWithSucc();
  357. /// Break bundle above this instruction.
  358. void unbundleFromPred();
  359. /// Break bundle below this instruction.
  360. void unbundleFromSucc();
  361. /// Returns the debug location id of this MachineInstr.
  362. const DebugLoc &getDebugLoc() const { return debugLoc; }
  363. /// Return the operand containing the offset to be used if this DBG_VALUE
  364. /// instruction is indirect; will be an invalid register if this value is
  365. /// not indirect, and an immediate with value 0 otherwise.
  366. const MachineOperand &getDebugOffset() const {
  367. assert(isNonListDebugValue() && "not a DBG_VALUE");
  368. return getOperand(1);
  369. }
  370. MachineOperand &getDebugOffset() {
  371. assert(isNonListDebugValue() && "not a DBG_VALUE");
  372. return getOperand(1);
  373. }
  374. /// Return the operand for the debug variable referenced by
  375. /// this DBG_VALUE instruction.
  376. const MachineOperand &getDebugVariableOp() const;
  377. MachineOperand &getDebugVariableOp();
  378. /// Return the debug variable referenced by
  379. /// this DBG_VALUE instruction.
  380. const DILocalVariable *getDebugVariable() const;
  381. /// Return the operand for the complex address expression referenced by
  382. /// this DBG_VALUE instruction.
  383. const MachineOperand &getDebugExpressionOp() const;
  384. MachineOperand &getDebugExpressionOp();
  385. /// Return the complex address expression referenced by
  386. /// this DBG_VALUE instruction.
  387. const DIExpression *getDebugExpression() const;
  388. /// Return the debug label referenced by
  389. /// this DBG_LABEL instruction.
  390. const DILabel *getDebugLabel() const;
  391. /// Fetch the instruction number of this MachineInstr. If it does not have
  392. /// one already, a new and unique number will be assigned.
  393. unsigned getDebugInstrNum();
  394. /// Examine the instruction number of this MachineInstr. May be zero if
  395. /// it hasn't been assigned a number yet.
  396. unsigned peekDebugInstrNum() const { return DebugInstrNum; }
  397. /// Set instruction number of this MachineInstr. Avoid using unless you're
  398. /// deserializing this information.
  399. void setDebugInstrNum(unsigned Num) { DebugInstrNum = Num; }
  400. /// Emit an error referring to the source location of this instruction.
  401. /// This should only be used for inline assembly that is somehow
  402. /// impossible to compile. Other errors should have been handled much
  403. /// earlier.
  404. ///
  405. /// If this method returns, the caller should try to recover from the error.
  406. void emitError(StringRef Msg) const;
  407. /// Returns the target instruction descriptor of this MachineInstr.
  408. const MCInstrDesc &getDesc() const { return *MCID; }
  409. /// Returns the opcode of this MachineInstr.
  410. unsigned getOpcode() const { return MCID->Opcode; }
  411. /// Retuns the total number of operands.
  412. unsigned getNumOperands() const { return NumOperands; }
  413. /// Returns the total number of operands which are debug locations.
  414. unsigned getNumDebugOperands() const {
  415. return std::distance(debug_operands().begin(), debug_operands().end());
  416. }
  417. const MachineOperand& getOperand(unsigned i) const {
  418. assert(i < getNumOperands() && "getOperand() out of range!");
  419. return Operands[i];
  420. }
  421. MachineOperand& getOperand(unsigned i) {
  422. assert(i < getNumOperands() && "getOperand() out of range!");
  423. return Operands[i];
  424. }
  425. MachineOperand &getDebugOperand(unsigned Index) {
  426. assert(Index < getNumDebugOperands() && "getDebugOperand() out of range!");
  427. return *(debug_operands().begin() + Index);
  428. }
  429. const MachineOperand &getDebugOperand(unsigned Index) const {
  430. assert(Index < getNumDebugOperands() && "getDebugOperand() out of range!");
  431. return *(debug_operands().begin() + Index);
  432. }
  433. SmallSet<Register, 4> getUsedDebugRegs() const {
  434. assert(isDebugValue() && "not a DBG_VALUE*");
  435. SmallSet<Register, 4> UsedRegs;
  436. for (auto MO : debug_operands())
  437. if (MO.isReg() && MO.getReg())
  438. UsedRegs.insert(MO.getReg());
  439. return UsedRegs;
  440. }
  441. /// Returns whether this debug value has at least one debug operand with the
  442. /// register \p Reg.
  443. bool hasDebugOperandForReg(Register Reg) const {
  444. return any_of(debug_operands(), [Reg](const MachineOperand &Op) {
  445. return Op.isReg() && Op.getReg() == Reg;
  446. });
  447. }
  448. /// Returns a range of all of the operands that correspond to a debug use of
  449. /// \p Reg.
  450. template <typename Operand, typename Instruction>
  451. static iterator_range<
  452. filter_iterator<Operand *, std::function<bool(Operand &Op)>>>
  453. getDebugOperandsForReg(Instruction *MI, Register Reg) {
  454. std::function<bool(Operand & Op)> OpUsesReg(
  455. [Reg](Operand &Op) { return Op.isReg() && Op.getReg() == Reg; });
  456. return make_filter_range(MI->debug_operands(), OpUsesReg);
  457. }
  458. iterator_range<filter_iterator<const MachineOperand *,
  459. std::function<bool(const MachineOperand &Op)>>>
  460. getDebugOperandsForReg(Register Reg) const {
  461. return MachineInstr::getDebugOperandsForReg<const MachineOperand,
  462. const MachineInstr>(this, Reg);
  463. }
  464. iterator_range<filter_iterator<MachineOperand *,
  465. std::function<bool(MachineOperand &Op)>>>
  466. getDebugOperandsForReg(Register Reg) {
  467. return MachineInstr::getDebugOperandsForReg<MachineOperand, MachineInstr>(
  468. this, Reg);
  469. }
  470. bool isDebugOperand(const MachineOperand *Op) const {
  471. return Op >= adl_begin(debug_operands()) && Op <= adl_end(debug_operands());
  472. }
  473. unsigned getDebugOperandIndex(const MachineOperand *Op) const {
  474. assert(isDebugOperand(Op) && "Expected a debug operand.");
  475. return std::distance(adl_begin(debug_operands()), Op);
  476. }
  477. /// Returns the total number of definitions.
  478. unsigned getNumDefs() const {
  479. return getNumExplicitDefs() + MCID->getNumImplicitDefs();
  480. }
  481. /// Returns true if the instruction has implicit definition.
  482. bool hasImplicitDef() const {
  483. for (unsigned I = getNumExplicitOperands(), E = getNumOperands();
  484. I != E; ++I) {
  485. const MachineOperand &MO = getOperand(I);
  486. if (MO.isDef() && MO.isImplicit())
  487. return true;
  488. }
  489. return false;
  490. }
  491. /// Returns the implicit operands number.
  492. unsigned getNumImplicitOperands() const {
  493. return getNumOperands() - getNumExplicitOperands();
  494. }
  495. /// Return true if operand \p OpIdx is a subregister index.
  496. bool isOperandSubregIdx(unsigned OpIdx) const {
  497. assert(getOperand(OpIdx).getType() == MachineOperand::MO_Immediate &&
  498. "Expected MO_Immediate operand type.");
  499. if (isExtractSubreg() && OpIdx == 2)
  500. return true;
  501. if (isInsertSubreg() && OpIdx == 3)
  502. return true;
  503. if (isRegSequence() && OpIdx > 1 && (OpIdx % 2) == 0)
  504. return true;
  505. if (isSubregToReg() && OpIdx == 3)
  506. return true;
  507. return false;
  508. }
  509. /// Returns the number of non-implicit operands.
  510. unsigned getNumExplicitOperands() const;
  511. /// Returns the number of non-implicit definitions.
  512. unsigned getNumExplicitDefs() const;
  513. /// iterator/begin/end - Iterate over all operands of a machine instruction.
  514. using mop_iterator = MachineOperand *;
  515. using const_mop_iterator = const MachineOperand *;
  516. mop_iterator operands_begin() { return Operands; }
  517. mop_iterator operands_end() { return Operands + NumOperands; }
  518. const_mop_iterator operands_begin() const { return Operands; }
  519. const_mop_iterator operands_end() const { return Operands + NumOperands; }
  520. iterator_range<mop_iterator> operands() {
  521. return make_range(operands_begin(), operands_end());
  522. }
  523. iterator_range<const_mop_iterator> operands() const {
  524. return make_range(operands_begin(), operands_end());
  525. }
  526. iterator_range<mop_iterator> explicit_operands() {
  527. return make_range(operands_begin(),
  528. operands_begin() + getNumExplicitOperands());
  529. }
  530. iterator_range<const_mop_iterator> explicit_operands() const {
  531. return make_range(operands_begin(),
  532. operands_begin() + getNumExplicitOperands());
  533. }
  534. iterator_range<mop_iterator> implicit_operands() {
  535. return make_range(explicit_operands().end(), operands_end());
  536. }
  537. iterator_range<const_mop_iterator> implicit_operands() const {
  538. return make_range(explicit_operands().end(), operands_end());
  539. }
  540. /// Returns a range over all operands that are used to determine the variable
  541. /// location for this DBG_VALUE instruction.
  542. iterator_range<mop_iterator> debug_operands() {
  543. assert(isDebugValue() && "Must be a debug value instruction.");
  544. return isDebugValueList()
  545. ? make_range(operands_begin() + 2, operands_end())
  546. : make_range(operands_begin(), operands_begin() + 1);
  547. }
  548. /// \copydoc debug_operands()
  549. iterator_range<const_mop_iterator> debug_operands() const {
  550. assert(isDebugValue() && "Must be a debug value instruction.");
  551. return isDebugValueList()
  552. ? make_range(operands_begin() + 2, operands_end())
  553. : make_range(operands_begin(), operands_begin() + 1);
  554. }
  555. /// Returns a range over all explicit operands that are register definitions.
  556. /// Implicit definition are not included!
  557. iterator_range<mop_iterator> defs() {
  558. return make_range(operands_begin(),
  559. operands_begin() + getNumExplicitDefs());
  560. }
  561. /// \copydoc defs()
  562. iterator_range<const_mop_iterator> defs() const {
  563. return make_range(operands_begin(),
  564. operands_begin() + getNumExplicitDefs());
  565. }
  566. /// Returns a range that includes all operands that are register uses.
  567. /// This may include unrelated operands which are not register uses.
  568. iterator_range<mop_iterator> uses() {
  569. return make_range(operands_begin() + getNumExplicitDefs(), operands_end());
  570. }
  571. /// \copydoc uses()
  572. iterator_range<const_mop_iterator> uses() const {
  573. return make_range(operands_begin() + getNumExplicitDefs(), operands_end());
  574. }
  575. iterator_range<mop_iterator> explicit_uses() {
  576. return make_range(operands_begin() + getNumExplicitDefs(),
  577. operands_begin() + getNumExplicitOperands());
  578. }
  579. iterator_range<const_mop_iterator> explicit_uses() const {
  580. return make_range(operands_begin() + getNumExplicitDefs(),
  581. operands_begin() + getNumExplicitOperands());
  582. }
  583. /// Returns the number of the operand iterator \p I points to.
  584. unsigned getOperandNo(const_mop_iterator I) const {
  585. return I - operands_begin();
  586. }
  587. /// Access to memory operands of the instruction. If there are none, that does
  588. /// not imply anything about whether the function accesses memory. Instead,
  589. /// the caller must behave conservatively.
  590. ArrayRef<MachineMemOperand *> memoperands() const {
  591. if (!Info)
  592. return {};
  593. if (Info.is<EIIK_MMO>())
  594. return makeArrayRef(Info.getAddrOfZeroTagPointer(), 1);
  595. if (ExtraInfo *EI = Info.get<EIIK_OutOfLine>())
  596. return EI->getMMOs();
  597. return {};
  598. }
  599. /// Access to memory operands of the instruction.
  600. ///
  601. /// If `memoperands_begin() == memoperands_end()`, that does not imply
  602. /// anything about whether the function accesses memory. Instead, the caller
  603. /// must behave conservatively.
  604. mmo_iterator memoperands_begin() const { return memoperands().begin(); }
  605. /// Access to memory operands of the instruction.
  606. ///
  607. /// If `memoperands_begin() == memoperands_end()`, that does not imply
  608. /// anything about whether the function accesses memory. Instead, the caller
  609. /// must behave conservatively.
  610. mmo_iterator memoperands_end() const { return memoperands().end(); }
  611. /// Return true if we don't have any memory operands which described the
  612. /// memory access done by this instruction. If this is true, calling code
  613. /// must be conservative.
  614. bool memoperands_empty() const { return memoperands().empty(); }
  615. /// Return true if this instruction has exactly one MachineMemOperand.
  616. bool hasOneMemOperand() const { return memoperands().size() == 1; }
  617. /// Return the number of memory operands.
  618. unsigned getNumMemOperands() const { return memoperands().size(); }
  619. /// Helper to extract a pre-instruction symbol if one has been added.
  620. MCSymbol *getPreInstrSymbol() const {
  621. if (!Info)
  622. return nullptr;
  623. if (MCSymbol *S = Info.get<EIIK_PreInstrSymbol>())
  624. return S;
  625. if (ExtraInfo *EI = Info.get<EIIK_OutOfLine>())
  626. return EI->getPreInstrSymbol();
  627. return nullptr;
  628. }
  629. /// Helper to extract a post-instruction symbol if one has been added.
  630. MCSymbol *getPostInstrSymbol() const {
  631. if (!Info)
  632. return nullptr;
  633. if (MCSymbol *S = Info.get<EIIK_PostInstrSymbol>())
  634. return S;
  635. if (ExtraInfo *EI = Info.get<EIIK_OutOfLine>())
  636. return EI->getPostInstrSymbol();
  637. return nullptr;
  638. }
  639. /// Helper to extract a heap alloc marker if one has been added.
  640. MDNode *getHeapAllocMarker() const {
  641. if (!Info)
  642. return nullptr;
  643. if (ExtraInfo *EI = Info.get<EIIK_OutOfLine>())
  644. return EI->getHeapAllocMarker();
  645. return nullptr;
  646. }
  647. /// API for querying MachineInstr properties. They are the same as MCInstrDesc
  648. /// queries but they are bundle aware.
  649. enum QueryType {
  650. IgnoreBundle, // Ignore bundles
  651. AnyInBundle, // Return true if any instruction in bundle has property
  652. AllInBundle // Return true if all instructions in bundle have property
  653. };
  654. /// Return true if the instruction (or in the case of a bundle,
  655. /// the instructions inside the bundle) has the specified property.
  656. /// The first argument is the property being queried.
  657. /// The second argument indicates whether the query should look inside
  658. /// instruction bundles.
  659. bool hasProperty(unsigned MCFlag, QueryType Type = AnyInBundle) const {
  660. assert(MCFlag < 64 &&
  661. "MCFlag out of range for bit mask in getFlags/hasPropertyInBundle.");
  662. // Inline the fast path for unbundled or bundle-internal instructions.
  663. if (Type == IgnoreBundle || !isBundled() || isBundledWithPred())
  664. return getDesc().getFlags() & (1ULL << MCFlag);
  665. // If this is the first instruction in a bundle, take the slow path.
  666. return hasPropertyInBundle(1ULL << MCFlag, Type);
  667. }
  668. /// Return true if this is an instruction that should go through the usual
  669. /// legalization steps.
  670. bool isPreISelOpcode(QueryType Type = IgnoreBundle) const {
  671. return hasProperty(MCID::PreISelOpcode, Type);
  672. }
  673. /// Return true if this instruction can have a variable number of operands.
  674. /// In this case, the variable operands will be after the normal
  675. /// operands but before the implicit definitions and uses (if any are
  676. /// present).
  677. bool isVariadic(QueryType Type = IgnoreBundle) const {
  678. return hasProperty(MCID::Variadic, Type);
  679. }
  680. /// Set if this instruction has an optional definition, e.g.
  681. /// ARM instructions which can set condition code if 's' bit is set.
  682. bool hasOptionalDef(QueryType Type = IgnoreBundle) const {
  683. return hasProperty(MCID::HasOptionalDef, Type);
  684. }
  685. /// Return true if this is a pseudo instruction that doesn't
  686. /// correspond to a real machine instruction.
  687. bool isPseudo(QueryType Type = IgnoreBundle) const {
  688. return hasProperty(MCID::Pseudo, Type);
  689. }
  690. bool isReturn(QueryType Type = AnyInBundle) const {
  691. return hasProperty(MCID::Return, Type);
  692. }
  693. /// Return true if this is an instruction that marks the end of an EH scope,
  694. /// i.e., a catchpad or a cleanuppad instruction.
  695. bool isEHScopeReturn(QueryType Type = AnyInBundle) const {
  696. return hasProperty(MCID::EHScopeReturn, Type);
  697. }
  698. bool isCall(QueryType Type = AnyInBundle) const {
  699. return hasProperty(MCID::Call, Type);
  700. }
  701. /// Return true if this is a call instruction that may have an associated
  702. /// call site entry in the debug info.
  703. bool isCandidateForCallSiteEntry(QueryType Type = IgnoreBundle) const;
  704. /// Return true if copying, moving, or erasing this instruction requires
  705. /// updating Call Site Info (see \ref copyCallSiteInfo, \ref moveCallSiteInfo,
  706. /// \ref eraseCallSiteInfo).
  707. bool shouldUpdateCallSiteInfo() const;
  708. /// Returns true if the specified instruction stops control flow
  709. /// from executing the instruction immediately following it. Examples include
  710. /// unconditional branches and return instructions.
  711. bool isBarrier(QueryType Type = AnyInBundle) const {
  712. return hasProperty(MCID::Barrier, Type);
  713. }
  714. /// Returns true if this instruction part of the terminator for a basic block.
  715. /// Typically this is things like return and branch instructions.
  716. ///
  717. /// Various passes use this to insert code into the bottom of a basic block,
  718. /// but before control flow occurs.
  719. bool isTerminator(QueryType Type = AnyInBundle) const {
  720. return hasProperty(MCID::Terminator, Type);
  721. }
  722. /// Returns true if this is a conditional, unconditional, or indirect branch.
  723. /// Predicates below can be used to discriminate between
  724. /// these cases, and the TargetInstrInfo::analyzeBranch method can be used to
  725. /// get more information.
  726. bool isBranch(QueryType Type = AnyInBundle) const {
  727. return hasProperty(MCID::Branch, Type);
  728. }
  729. /// Return true if this is an indirect branch, such as a
  730. /// branch through a register.
  731. bool isIndirectBranch(QueryType Type = AnyInBundle) const {
  732. return hasProperty(MCID::IndirectBranch, Type);
  733. }
  734. /// Return true if this is a branch which may fall
  735. /// through to the next instruction or may transfer control flow to some other
  736. /// block. The TargetInstrInfo::analyzeBranch method can be used to get more
  737. /// information about this branch.
  738. bool isConditionalBranch(QueryType Type = AnyInBundle) const {
  739. return isBranch(Type) && !isBarrier(Type) && !isIndirectBranch(Type);
  740. }
  741. /// Return true if this is a branch which always
  742. /// transfers control flow to some other block. The
  743. /// TargetInstrInfo::analyzeBranch method can be used to get more information
  744. /// about this branch.
  745. bool isUnconditionalBranch(QueryType Type = AnyInBundle) const {
  746. return isBranch(Type) && isBarrier(Type) && !isIndirectBranch(Type);
  747. }
  748. /// Return true if this instruction has a predicate operand that
  749. /// controls execution. It may be set to 'always', or may be set to other
  750. /// values. There are various methods in TargetInstrInfo that can be used to
  751. /// control and modify the predicate in this instruction.
  752. bool isPredicable(QueryType Type = AllInBundle) const {
  753. // If it's a bundle than all bundled instructions must be predicable for this
  754. // to return true.
  755. return hasProperty(MCID::Predicable, Type);
  756. }
  757. /// Return true if this instruction is a comparison.
  758. bool isCompare(QueryType Type = IgnoreBundle) const {
  759. return hasProperty(MCID::Compare, Type);
  760. }
  761. /// Return true if this instruction is a move immediate
  762. /// (including conditional moves) instruction.
  763. bool isMoveImmediate(QueryType Type = IgnoreBundle) const {
  764. return hasProperty(MCID::MoveImm, Type);
  765. }
  766. /// Return true if this instruction is a register move.
  767. /// (including moving values from subreg to reg)
  768. bool isMoveReg(QueryType Type = IgnoreBundle) const {
  769. return hasProperty(MCID::MoveReg, Type);
  770. }
  771. /// Return true if this instruction is a bitcast instruction.
  772. bool isBitcast(QueryType Type = IgnoreBundle) const {
  773. return hasProperty(MCID::Bitcast, Type);
  774. }
  775. /// Return true if this instruction is a select instruction.
  776. bool isSelect(QueryType Type = IgnoreBundle) const {
  777. return hasProperty(MCID::Select, Type);
  778. }
  779. /// Return true if this instruction cannot be safely duplicated.
  780. /// For example, if the instruction has a unique labels attached
  781. /// to it, duplicating it would cause multiple definition errors.
  782. bool isNotDuplicable(QueryType Type = AnyInBundle) const {
  783. return hasProperty(MCID::NotDuplicable, Type);
  784. }
  785. /// Return true if this instruction is convergent.
  786. /// Convergent instructions can not be made control-dependent on any
  787. /// additional values.
  788. bool isConvergent(QueryType Type = AnyInBundle) const {
  789. if (isInlineAsm()) {
  790. unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
  791. if (ExtraInfo & InlineAsm::Extra_IsConvergent)
  792. return true;
  793. }
  794. return hasProperty(MCID::Convergent, Type);
  795. }
  796. /// Returns true if the specified instruction has a delay slot
  797. /// which must be filled by the code generator.
  798. bool hasDelaySlot(QueryType Type = AnyInBundle) const {
  799. return hasProperty(MCID::DelaySlot, Type);
  800. }
  801. /// Return true for instructions that can be folded as
  802. /// memory operands in other instructions. The most common use for this
  803. /// is instructions that are simple loads from memory that don't modify
  804. /// the loaded value in any way, but it can also be used for instructions
  805. /// that can be expressed as constant-pool loads, such as V_SETALLONES
  806. /// on x86, to allow them to be folded when it is beneficial.
  807. /// This should only be set on instructions that return a value in their
  808. /// only virtual register definition.
  809. bool canFoldAsLoad(QueryType Type = IgnoreBundle) const {
  810. return hasProperty(MCID::FoldableAsLoad, Type);
  811. }
  812. /// Return true if this instruction behaves
  813. /// the same way as the generic REG_SEQUENCE instructions.
  814. /// E.g., on ARM,
  815. /// dX VMOVDRR rY, rZ
  816. /// is equivalent to
  817. /// dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1.
  818. ///
  819. /// Note that for the optimizers to be able to take advantage of
  820. /// this property, TargetInstrInfo::getRegSequenceLikeInputs has to be
  821. /// override accordingly.
  822. bool isRegSequenceLike(QueryType Type = IgnoreBundle) const {
  823. return hasProperty(MCID::RegSequence, Type);
  824. }
  825. /// Return true if this instruction behaves
  826. /// the same way as the generic EXTRACT_SUBREG instructions.
  827. /// E.g., on ARM,
  828. /// rX, rY VMOVRRD dZ
  829. /// is equivalent to two EXTRACT_SUBREG:
  830. /// rX = EXTRACT_SUBREG dZ, ssub_0
  831. /// rY = EXTRACT_SUBREG dZ, ssub_1
  832. ///
  833. /// Note that for the optimizers to be able to take advantage of
  834. /// this property, TargetInstrInfo::getExtractSubregLikeInputs has to be
  835. /// override accordingly.
  836. bool isExtractSubregLike(QueryType Type = IgnoreBundle) const {
  837. return hasProperty(MCID::ExtractSubreg, Type);
  838. }
  839. /// Return true if this instruction behaves
  840. /// the same way as the generic INSERT_SUBREG instructions.
  841. /// E.g., on ARM,
  842. /// dX = VSETLNi32 dY, rZ, Imm
  843. /// is equivalent to a INSERT_SUBREG:
  844. /// dX = INSERT_SUBREG dY, rZ, translateImmToSubIdx(Imm)
  845. ///
  846. /// Note that for the optimizers to be able to take advantage of
  847. /// this property, TargetInstrInfo::getInsertSubregLikeInputs has to be
  848. /// override accordingly.
  849. bool isInsertSubregLike(QueryType Type = IgnoreBundle) const {
  850. return hasProperty(MCID::InsertSubreg, Type);
  851. }
  852. //===--------------------------------------------------------------------===//
  853. // Side Effect Analysis
  854. //===--------------------------------------------------------------------===//
  855. /// Return true if this instruction could possibly read memory.
  856. /// Instructions with this flag set are not necessarily simple load
  857. /// instructions, they may load a value and modify it, for example.
  858. bool mayLoad(QueryType Type = AnyInBundle) const {
  859. if (isInlineAsm()) {
  860. unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
  861. if (ExtraInfo & InlineAsm::Extra_MayLoad)
  862. return true;
  863. }
  864. return hasProperty(MCID::MayLoad, Type);
  865. }
  866. /// Return true if this instruction could possibly modify memory.
  867. /// Instructions with this flag set are not necessarily simple store
  868. /// instructions, they may store a modified value based on their operands, or
  869. /// may not actually modify anything, for example.
  870. bool mayStore(QueryType Type = AnyInBundle) const {
  871. if (isInlineAsm()) {
  872. unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
  873. if (ExtraInfo & InlineAsm::Extra_MayStore)
  874. return true;
  875. }
  876. return hasProperty(MCID::MayStore, Type);
  877. }
  878. /// Return true if this instruction could possibly read or modify memory.
  879. bool mayLoadOrStore(QueryType Type = AnyInBundle) const {
  880. return mayLoad(Type) || mayStore(Type);
  881. }
  882. /// Return true if this instruction could possibly raise a floating-point
  883. /// exception. This is the case if the instruction is a floating-point
  884. /// instruction that can in principle raise an exception, as indicated
  885. /// by the MCID::MayRaiseFPException property, *and* at the same time,
  886. /// the instruction is used in a context where we expect floating-point
  887. /// exceptions are not disabled, as indicated by the NoFPExcept MI flag.
  888. bool mayRaiseFPException() const {
  889. return hasProperty(MCID::MayRaiseFPException) &&
  890. !getFlag(MachineInstr::MIFlag::NoFPExcept);
  891. }
  892. //===--------------------------------------------------------------------===//
  893. // Flags that indicate whether an instruction can be modified by a method.
  894. //===--------------------------------------------------------------------===//
  895. /// Return true if this may be a 2- or 3-address
  896. /// instruction (of the form "X = op Y, Z, ..."), which produces the same
  897. /// result if Y and Z are exchanged. If this flag is set, then the
  898. /// TargetInstrInfo::commuteInstruction method may be used to hack on the
  899. /// instruction.
  900. ///
  901. /// Note that this flag may be set on instructions that are only commutable
  902. /// sometimes. In these cases, the call to commuteInstruction will fail.
  903. /// Also note that some instructions require non-trivial modification to
  904. /// commute them.
  905. bool isCommutable(QueryType Type = IgnoreBundle) const {
  906. return hasProperty(MCID::Commutable, Type);
  907. }
  908. /// Return true if this is a 2-address instruction
  909. /// which can be changed into a 3-address instruction if needed. Doing this
  910. /// transformation can be profitable in the register allocator, because it
  911. /// means that the instruction can use a 2-address form if possible, but
  912. /// degrade into a less efficient form if the source and dest register cannot
  913. /// be assigned to the same register. For example, this allows the x86
  914. /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which
  915. /// is the same speed as the shift but has bigger code size.
  916. ///
  917. /// If this returns true, then the target must implement the
  918. /// TargetInstrInfo::convertToThreeAddress method for this instruction, which
  919. /// is allowed to fail if the transformation isn't valid for this specific
  920. /// instruction (e.g. shl reg, 4 on x86).
  921. ///
  922. bool isConvertibleTo3Addr(QueryType Type = IgnoreBundle) const {
  923. return hasProperty(MCID::ConvertibleTo3Addr, Type);
  924. }
  925. /// Return true if this instruction requires
  926. /// custom insertion support when the DAG scheduler is inserting it into a
  927. /// machine basic block. If this is true for the instruction, it basically
  928. /// means that it is a pseudo instruction used at SelectionDAG time that is
  929. /// expanded out into magic code by the target when MachineInstrs are formed.
  930. ///
  931. /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method
  932. /// is used to insert this into the MachineBasicBlock.
  933. bool usesCustomInsertionHook(QueryType Type = IgnoreBundle) const {
  934. return hasProperty(MCID::UsesCustomInserter, Type);
  935. }
  936. /// Return true if this instruction requires *adjustment*
  937. /// after instruction selection by calling a target hook. For example, this
  938. /// can be used to fill in ARM 's' optional operand depending on whether
  939. /// the conditional flag register is used.
  940. bool hasPostISelHook(QueryType Type = IgnoreBundle) const {
  941. return hasProperty(MCID::HasPostISelHook, Type);
  942. }
  943. /// Returns true if this instruction is a candidate for remat.
  944. /// This flag is deprecated, please don't use it anymore. If this
  945. /// flag is set, the isReallyTriviallyReMaterializable() method is called to
  946. /// verify the instruction is really rematable.
  947. bool isRematerializable(QueryType Type = AllInBundle) const {
  948. // It's only possible to re-mat a bundle if all bundled instructions are
  949. // re-materializable.
  950. return hasProperty(MCID::Rematerializable, Type);
  951. }
  952. /// Returns true if this instruction has the same cost (or less) than a move
  953. /// instruction. This is useful during certain types of optimizations
  954. /// (e.g., remat during two-address conversion or machine licm)
  955. /// where we would like to remat or hoist the instruction, but not if it costs
  956. /// more than moving the instruction into the appropriate register. Note, we
  957. /// are not marking copies from and to the same register class with this flag.
  958. bool isAsCheapAsAMove(QueryType Type = AllInBundle) const {
  959. // Only returns true for a bundle if all bundled instructions are cheap.
  960. return hasProperty(MCID::CheapAsAMove, Type);
  961. }
  962. /// Returns true if this instruction source operands
  963. /// have special register allocation requirements that are not captured by the
  964. /// operand register classes. e.g. ARM::STRD's two source registers must be an
  965. /// even / odd pair, ARM::STM registers have to be in ascending order.
  966. /// Post-register allocation passes should not attempt to change allocations
  967. /// for sources of instructions with this flag.
  968. bool hasExtraSrcRegAllocReq(QueryType Type = AnyInBundle) const {
  969. return hasProperty(MCID::ExtraSrcRegAllocReq, Type);
  970. }
  971. /// Returns true if this instruction def operands
  972. /// have special register allocation requirements that are not captured by the
  973. /// operand register classes. e.g. ARM::LDRD's two def registers must be an
  974. /// even / odd pair, ARM::LDM registers have to be in ascending order.
  975. /// Post-register allocation passes should not attempt to change allocations
  976. /// for definitions of instructions with this flag.
  977. bool hasExtraDefRegAllocReq(QueryType Type = AnyInBundle) const {
  978. return hasProperty(MCID::ExtraDefRegAllocReq, Type);
  979. }
  980. enum MICheckType {
  981. CheckDefs, // Check all operands for equality
  982. CheckKillDead, // Check all operands including kill / dead markers
  983. IgnoreDefs, // Ignore all definitions
  984. IgnoreVRegDefs // Ignore virtual register definitions
  985. };
  986. /// Return true if this instruction is identical to \p Other.
  987. /// Two instructions are identical if they have the same opcode and all their
  988. /// operands are identical (with respect to MachineOperand::isIdenticalTo()).
  989. /// Note that this means liveness related flags (dead, undef, kill) do not
  990. /// affect the notion of identical.
  991. bool isIdenticalTo(const MachineInstr &Other,
  992. MICheckType Check = CheckDefs) const;
  993. /// Unlink 'this' from the containing basic block, and return it without
  994. /// deleting it.
  995. ///
  996. /// This function can not be used on bundled instructions, use
  997. /// removeFromBundle() to remove individual instructions from a bundle.
  998. MachineInstr *removeFromParent();
  999. /// Unlink this instruction from its basic block and return it without
  1000. /// deleting it.
  1001. ///
  1002. /// If the instruction is part of a bundle, the other instructions in the
  1003. /// bundle remain bundled.
  1004. MachineInstr *removeFromBundle();
  1005. /// Unlink 'this' from the containing basic block and delete it.
  1006. ///
  1007. /// If this instruction is the header of a bundle, the whole bundle is erased.
  1008. /// This function can not be used for instructions inside a bundle, use
  1009. /// eraseFromBundle() to erase individual bundled instructions.
  1010. void eraseFromParent();
  1011. /// Unlink 'this' from the containing basic block and delete it.
  1012. ///
  1013. /// For all definitions mark their uses in DBG_VALUE nodes
  1014. /// as undefined. Otherwise like eraseFromParent().
  1015. void eraseFromParentAndMarkDBGValuesForRemoval();
  1016. /// Unlink 'this' form its basic block and delete it.
  1017. ///
  1018. /// If the instruction is part of a bundle, the other instructions in the
  1019. /// bundle remain bundled.
  1020. void eraseFromBundle();
  1021. bool isEHLabel() const { return getOpcode() == TargetOpcode::EH_LABEL; }
  1022. bool isGCLabel() const { return getOpcode() == TargetOpcode::GC_LABEL; }
  1023. bool isAnnotationLabel() const {
  1024. return getOpcode() == TargetOpcode::ANNOTATION_LABEL;
  1025. }
  1026. /// Returns true if the MachineInstr represents a label.
  1027. bool isLabel() const {
  1028. return isEHLabel() || isGCLabel() || isAnnotationLabel();
  1029. }
  1030. bool isCFIInstruction() const {
  1031. return getOpcode() == TargetOpcode::CFI_INSTRUCTION;
  1032. }
  1033. bool isPseudoProbe() const {
  1034. return getOpcode() == TargetOpcode::PSEUDO_PROBE;
  1035. }
  1036. // True if the instruction represents a position in the function.
  1037. bool isPosition() const { return isLabel() || isCFIInstruction(); }
  1038. bool isNonListDebugValue() const {
  1039. return getOpcode() == TargetOpcode::DBG_VALUE;
  1040. }
  1041. bool isDebugValueList() const {
  1042. return getOpcode() == TargetOpcode::DBG_VALUE_LIST;
  1043. }
  1044. bool isDebugValue() const {
  1045. return isNonListDebugValue() || isDebugValueList();
  1046. }
  1047. bool isDebugLabel() const { return getOpcode() == TargetOpcode::DBG_LABEL; }
  1048. bool isDebugRef() const { return getOpcode() == TargetOpcode::DBG_INSTR_REF; }
  1049. bool isDebugPHI() const { return getOpcode() == TargetOpcode::DBG_PHI; }
  1050. bool isDebugInstr() const {
  1051. return isDebugValue() || isDebugLabel() || isDebugRef() || isDebugPHI();
  1052. }
  1053. bool isDebugOrPseudoInstr() const {
  1054. return isDebugInstr() || isPseudoProbe();
  1055. }
  1056. bool isDebugOffsetImm() const {
  1057. return isNonListDebugValue() && getDebugOffset().isImm();
  1058. }
  1059. /// A DBG_VALUE is indirect iff the location operand is a register and
  1060. /// the offset operand is an immediate.
  1061. bool isIndirectDebugValue() const {
  1062. return isDebugOffsetImm() && getDebugOperand(0).isReg();
  1063. }
  1064. /// A DBG_VALUE is an entry value iff its debug expression contains the
  1065. /// DW_OP_LLVM_entry_value operation.
  1066. bool isDebugEntryValue() const;
  1067. /// Return true if the instruction is a debug value which describes a part of
  1068. /// a variable as unavailable.
  1069. bool isUndefDebugValue() const {
  1070. if (!isDebugValue())
  1071. return false;
  1072. // If any $noreg locations are given, this DV is undef.
  1073. for (const MachineOperand &Op : debug_operands())
  1074. if (Op.isReg() && !Op.getReg().isValid())
  1075. return true;
  1076. return false;
  1077. }
  1078. bool isPHI() const {
  1079. return getOpcode() == TargetOpcode::PHI ||
  1080. getOpcode() == TargetOpcode::G_PHI;
  1081. }
  1082. bool isKill() const { return getOpcode() == TargetOpcode::KILL; }
  1083. bool isImplicitDef() const { return getOpcode()==TargetOpcode::IMPLICIT_DEF; }
  1084. bool isInlineAsm() const {
  1085. return getOpcode() == TargetOpcode::INLINEASM ||
  1086. getOpcode() == TargetOpcode::INLINEASM_BR;
  1087. }
  1088. /// FIXME: Seems like a layering violation that the AsmDialect, which is X86
  1089. /// specific, be attached to a generic MachineInstr.
  1090. bool isMSInlineAsm() const {
  1091. return isInlineAsm() && getInlineAsmDialect() == InlineAsm::AD_Intel;
  1092. }
  1093. bool isStackAligningInlineAsm() const;
  1094. InlineAsm::AsmDialect getInlineAsmDialect() const;
  1095. bool isInsertSubreg() const {
  1096. return getOpcode() == TargetOpcode::INSERT_SUBREG;
  1097. }
  1098. bool isSubregToReg() const {
  1099. return getOpcode() == TargetOpcode::SUBREG_TO_REG;
  1100. }
  1101. bool isRegSequence() const {
  1102. return getOpcode() == TargetOpcode::REG_SEQUENCE;
  1103. }
  1104. bool isBundle() const {
  1105. return getOpcode() == TargetOpcode::BUNDLE;
  1106. }
  1107. bool isCopy() const {
  1108. return getOpcode() == TargetOpcode::COPY;
  1109. }
  1110. bool isFullCopy() const {
  1111. return isCopy() && !getOperand(0).getSubReg() && !getOperand(1).getSubReg();
  1112. }
  1113. bool isExtractSubreg() const {
  1114. return getOpcode() == TargetOpcode::EXTRACT_SUBREG;
  1115. }
  1116. /// Return true if the instruction behaves like a copy.
  1117. /// This does not include native copy instructions.
  1118. bool isCopyLike() const {
  1119. return isCopy() || isSubregToReg();
  1120. }
  1121. /// Return true is the instruction is an identity copy.
  1122. bool isIdentityCopy() const {
  1123. return isCopy() && getOperand(0).getReg() == getOperand(1).getReg() &&
  1124. getOperand(0).getSubReg() == getOperand(1).getSubReg();
  1125. }
  1126. /// Return true if this instruction doesn't produce any output in the form of
  1127. /// executable instructions.
  1128. bool isMetaInstruction() const {
  1129. switch (getOpcode()) {
  1130. default:
  1131. return false;
  1132. case TargetOpcode::IMPLICIT_DEF:
  1133. case TargetOpcode::KILL:
  1134. case TargetOpcode::CFI_INSTRUCTION:
  1135. case TargetOpcode::EH_LABEL:
  1136. case TargetOpcode::GC_LABEL:
  1137. case TargetOpcode::DBG_VALUE:
  1138. case TargetOpcode::DBG_VALUE_LIST:
  1139. case TargetOpcode::DBG_INSTR_REF:
  1140. case TargetOpcode::DBG_PHI:
  1141. case TargetOpcode::DBG_LABEL:
  1142. case TargetOpcode::LIFETIME_START:
  1143. case TargetOpcode::LIFETIME_END:
  1144. case TargetOpcode::PSEUDO_PROBE:
  1145. return true;
  1146. }
  1147. }
  1148. /// Return true if this is a transient instruction that is either very likely
  1149. /// to be eliminated during register allocation (such as copy-like
  1150. /// instructions), or if this instruction doesn't have an execution-time cost.
  1151. bool isTransient() const {
  1152. switch (getOpcode()) {
  1153. default:
  1154. return isMetaInstruction();
  1155. // Copy-like instructions are usually eliminated during register allocation.
  1156. case TargetOpcode::PHI:
  1157. case TargetOpcode::G_PHI:
  1158. case TargetOpcode::COPY:
  1159. case TargetOpcode::INSERT_SUBREG:
  1160. case TargetOpcode::SUBREG_TO_REG:
  1161. case TargetOpcode::REG_SEQUENCE:
  1162. return true;
  1163. }
  1164. }
  1165. /// Return the number of instructions inside the MI bundle, excluding the
  1166. /// bundle header.
  1167. ///
  1168. /// This is the number of instructions that MachineBasicBlock::iterator
  1169. /// skips, 0 for unbundled instructions.
  1170. unsigned getBundleSize() const;
  1171. /// Return true if the MachineInstr reads the specified register.
  1172. /// If TargetRegisterInfo is passed, then it also checks if there
  1173. /// is a read of a super-register.
  1174. /// This does not count partial redefines of virtual registers as reads:
  1175. /// %reg1024:6 = OP.
  1176. bool readsRegister(Register Reg,
  1177. const TargetRegisterInfo *TRI = nullptr) const {
  1178. return findRegisterUseOperandIdx(Reg, false, TRI) != -1;
  1179. }
  1180. /// Return true if the MachineInstr reads the specified virtual register.
  1181. /// Take into account that a partial define is a
  1182. /// read-modify-write operation.
  1183. bool readsVirtualRegister(Register Reg) const {
  1184. return readsWritesVirtualRegister(Reg).first;
  1185. }
  1186. /// Return a pair of bools (reads, writes) indicating if this instruction
  1187. /// reads or writes Reg. This also considers partial defines.
  1188. /// If Ops is not null, all operand indices for Reg are added.
  1189. std::pair<bool,bool> readsWritesVirtualRegister(Register Reg,
  1190. SmallVectorImpl<unsigned> *Ops = nullptr) const;
  1191. /// Return true if the MachineInstr kills the specified register.
  1192. /// If TargetRegisterInfo is passed, then it also checks if there is
  1193. /// a kill of a super-register.
  1194. bool killsRegister(Register Reg,
  1195. const TargetRegisterInfo *TRI = nullptr) const {
  1196. return findRegisterUseOperandIdx(Reg, true, TRI) != -1;
  1197. }
  1198. /// Return true if the MachineInstr fully defines the specified register.
  1199. /// If TargetRegisterInfo is passed, then it also checks
  1200. /// if there is a def of a super-register.
  1201. /// NOTE: It's ignoring subreg indices on virtual registers.
  1202. bool definesRegister(Register Reg,
  1203. const TargetRegisterInfo *TRI = nullptr) const {
  1204. return findRegisterDefOperandIdx(Reg, false, false, TRI) != -1;
  1205. }
  1206. /// Return true if the MachineInstr modifies (fully define or partially
  1207. /// define) the specified register.
  1208. /// NOTE: It's ignoring subreg indices on virtual registers.
  1209. bool modifiesRegister(Register Reg,
  1210. const TargetRegisterInfo *TRI = nullptr) const {
  1211. return findRegisterDefOperandIdx(Reg, false, true, TRI) != -1;
  1212. }
  1213. /// Returns true if the register is dead in this machine instruction.
  1214. /// If TargetRegisterInfo is passed, then it also checks
  1215. /// if there is a dead def of a super-register.
  1216. bool registerDefIsDead(Register Reg,
  1217. const TargetRegisterInfo *TRI = nullptr) const {
  1218. return findRegisterDefOperandIdx(Reg, true, false, TRI) != -1;
  1219. }
  1220. /// Returns true if the MachineInstr has an implicit-use operand of exactly
  1221. /// the given register (not considering sub/super-registers).
  1222. bool hasRegisterImplicitUseOperand(Register Reg) const;
  1223. /// Returns the operand index that is a use of the specific register or -1
  1224. /// if it is not found. It further tightens the search criteria to a use
  1225. /// that kills the register if isKill is true.
  1226. int findRegisterUseOperandIdx(Register Reg, bool isKill = false,
  1227. const TargetRegisterInfo *TRI = nullptr) const;
  1228. /// Wrapper for findRegisterUseOperandIdx, it returns
  1229. /// a pointer to the MachineOperand rather than an index.
  1230. MachineOperand *findRegisterUseOperand(Register Reg, bool isKill = false,
  1231. const TargetRegisterInfo *TRI = nullptr) {
  1232. int Idx = findRegisterUseOperandIdx(Reg, isKill, TRI);
  1233. return (Idx == -1) ? nullptr : &getOperand(Idx);
  1234. }
  1235. const MachineOperand *findRegisterUseOperand(
  1236. Register Reg, bool isKill = false,
  1237. const TargetRegisterInfo *TRI = nullptr) const {
  1238. return const_cast<MachineInstr *>(this)->
  1239. findRegisterUseOperand(Reg, isKill, TRI);
  1240. }
  1241. /// Returns the operand index that is a def of the specified register or
  1242. /// -1 if it is not found. If isDead is true, defs that are not dead are
  1243. /// skipped. If Overlap is true, then it also looks for defs that merely
  1244. /// overlap the specified register. If TargetRegisterInfo is non-null,
  1245. /// then it also checks if there is a def of a super-register.
  1246. /// This may also return a register mask operand when Overlap is true.
  1247. int findRegisterDefOperandIdx(Register Reg,
  1248. bool isDead = false, bool Overlap = false,
  1249. const TargetRegisterInfo *TRI = nullptr) const;
  1250. /// Wrapper for findRegisterDefOperandIdx, it returns
  1251. /// a pointer to the MachineOperand rather than an index.
  1252. MachineOperand *
  1253. findRegisterDefOperand(Register Reg, bool isDead = false,
  1254. bool Overlap = false,
  1255. const TargetRegisterInfo *TRI = nullptr) {
  1256. int Idx = findRegisterDefOperandIdx(Reg, isDead, Overlap, TRI);
  1257. return (Idx == -1) ? nullptr : &getOperand(Idx);
  1258. }
  1259. const MachineOperand *
  1260. findRegisterDefOperand(Register Reg, bool isDead = false,
  1261. bool Overlap = false,
  1262. const TargetRegisterInfo *TRI = nullptr) const {
  1263. return const_cast<MachineInstr *>(this)->findRegisterDefOperand(
  1264. Reg, isDead, Overlap, TRI);
  1265. }
  1266. /// Find the index of the first operand in the
  1267. /// operand list that is used to represent the predicate. It returns -1 if
  1268. /// none is found.
  1269. int findFirstPredOperandIdx() const;
  1270. /// Find the index of the flag word operand that
  1271. /// corresponds to operand OpIdx on an inline asm instruction. Returns -1 if
  1272. /// getOperand(OpIdx) does not belong to an inline asm operand group.
  1273. ///
  1274. /// If GroupNo is not NULL, it will receive the number of the operand group
  1275. /// containing OpIdx.
  1276. ///
  1277. /// The flag operand is an immediate that can be decoded with methods like
  1278. /// InlineAsm::hasRegClassConstraint().
  1279. int findInlineAsmFlagIdx(unsigned OpIdx, unsigned *GroupNo = nullptr) const;
  1280. /// Compute the static register class constraint for operand OpIdx.
  1281. /// For normal instructions, this is derived from the MCInstrDesc.
  1282. /// For inline assembly it is derived from the flag words.
  1283. ///
  1284. /// Returns NULL if the static register class constraint cannot be
  1285. /// determined.
  1286. const TargetRegisterClass*
  1287. getRegClassConstraint(unsigned OpIdx,
  1288. const TargetInstrInfo *TII,
  1289. const TargetRegisterInfo *TRI) const;
  1290. /// Applies the constraints (def/use) implied by this MI on \p Reg to
  1291. /// the given \p CurRC.
  1292. /// If \p ExploreBundle is set and MI is part of a bundle, all the
  1293. /// instructions inside the bundle will be taken into account. In other words,
  1294. /// this method accumulates all the constraints of the operand of this MI and
  1295. /// the related bundle if MI is a bundle or inside a bundle.
  1296. ///
  1297. /// Returns the register class that satisfies both \p CurRC and the
  1298. /// constraints set by MI. Returns NULL if such a register class does not
  1299. /// exist.
  1300. ///
  1301. /// \pre CurRC must not be NULL.
  1302. const TargetRegisterClass *getRegClassConstraintEffectForVReg(
  1303. Register Reg, const TargetRegisterClass *CurRC,
  1304. const TargetInstrInfo *TII, const TargetRegisterInfo *TRI,
  1305. bool ExploreBundle = false) const;
  1306. /// Applies the constraints (def/use) implied by the \p OpIdx operand
  1307. /// to the given \p CurRC.
  1308. ///
  1309. /// Returns the register class that satisfies both \p CurRC and the
  1310. /// constraints set by \p OpIdx MI. Returns NULL if such a register class
  1311. /// does not exist.
  1312. ///
  1313. /// \pre CurRC must not be NULL.
  1314. /// \pre The operand at \p OpIdx must be a register.
  1315. const TargetRegisterClass *
  1316. getRegClassConstraintEffect(unsigned OpIdx, const TargetRegisterClass *CurRC,
  1317. const TargetInstrInfo *TII,
  1318. const TargetRegisterInfo *TRI) const;
  1319. /// Add a tie between the register operands at DefIdx and UseIdx.
  1320. /// The tie will cause the register allocator to ensure that the two
  1321. /// operands are assigned the same physical register.
  1322. ///
  1323. /// Tied operands are managed automatically for explicit operands in the
  1324. /// MCInstrDesc. This method is for exceptional cases like inline asm.
  1325. void tieOperands(unsigned DefIdx, unsigned UseIdx);
  1326. /// Given the index of a tied register operand, find the
  1327. /// operand it is tied to. Defs are tied to uses and vice versa. Returns the
  1328. /// index of the tied operand which must exist.
  1329. unsigned findTiedOperandIdx(unsigned OpIdx) const;
  1330. /// Given the index of a register def operand,
  1331. /// check if the register def is tied to a source operand, due to either
  1332. /// two-address elimination or inline assembly constraints. Returns the
  1333. /// first tied use operand index by reference if UseOpIdx is not null.
  1334. bool isRegTiedToUseOperand(unsigned DefOpIdx,
  1335. unsigned *UseOpIdx = nullptr) const {
  1336. const MachineOperand &MO = getOperand(DefOpIdx);
  1337. if (!MO.isReg() || !MO.isDef() || !MO.isTied())
  1338. return false;
  1339. if (UseOpIdx)
  1340. *UseOpIdx = findTiedOperandIdx(DefOpIdx);
  1341. return true;
  1342. }
  1343. /// Return true if the use operand of the specified index is tied to a def
  1344. /// operand. It also returns the def operand index by reference if DefOpIdx
  1345. /// is not null.
  1346. bool isRegTiedToDefOperand(unsigned UseOpIdx,
  1347. unsigned *DefOpIdx = nullptr) const {
  1348. const MachineOperand &MO = getOperand(UseOpIdx);
  1349. if (!MO.isReg() || !MO.isUse() || !MO.isTied())
  1350. return false;
  1351. if (DefOpIdx)
  1352. *DefOpIdx = findTiedOperandIdx(UseOpIdx);
  1353. return true;
  1354. }
  1355. /// Clears kill flags on all operands.
  1356. void clearKillInfo();
  1357. /// Replace all occurrences of FromReg with ToReg:SubIdx,
  1358. /// properly composing subreg indices where necessary.
  1359. void substituteRegister(Register FromReg, Register ToReg, unsigned SubIdx,
  1360. const TargetRegisterInfo &RegInfo);
  1361. /// We have determined MI kills a register. Look for the
  1362. /// operand that uses it and mark it as IsKill. If AddIfNotFound is true,
  1363. /// add a implicit operand if it's not found. Returns true if the operand
  1364. /// exists / is added.
  1365. bool addRegisterKilled(Register IncomingReg,
  1366. const TargetRegisterInfo *RegInfo,
  1367. bool AddIfNotFound = false);
  1368. /// Clear all kill flags affecting Reg. If RegInfo is provided, this includes
  1369. /// all aliasing registers.
  1370. void clearRegisterKills(Register Reg, const TargetRegisterInfo *RegInfo);
  1371. /// We have determined MI defined a register without a use.
  1372. /// Look for the operand that defines it and mark it as IsDead. If
  1373. /// AddIfNotFound is true, add a implicit operand if it's not found. Returns
  1374. /// true if the operand exists / is added.
  1375. bool addRegisterDead(Register Reg, const TargetRegisterInfo *RegInfo,
  1376. bool AddIfNotFound = false);
  1377. /// Clear all dead flags on operands defining register @p Reg.
  1378. void clearRegisterDeads(Register Reg);
  1379. /// Mark all subregister defs of register @p Reg with the undef flag.
  1380. /// This function is used when we determined to have a subregister def in an
  1381. /// otherwise undefined super register.
  1382. void setRegisterDefReadUndef(Register Reg, bool IsUndef = true);
  1383. /// We have determined MI defines a register. Make sure there is an operand
  1384. /// defining Reg.
  1385. void addRegisterDefined(Register Reg,
  1386. const TargetRegisterInfo *RegInfo = nullptr);
  1387. /// Mark every physreg used by this instruction as
  1388. /// dead except those in the UsedRegs list.
  1389. ///
  1390. /// On instructions with register mask operands, also add implicit-def
  1391. /// operands for all registers in UsedRegs.
  1392. void setPhysRegsDeadExcept(ArrayRef<Register> UsedRegs,
  1393. const TargetRegisterInfo &TRI);
  1394. /// Return true if it is safe to move this instruction. If
  1395. /// SawStore is set to true, it means that there is a store (or call) between
  1396. /// the instruction's location and its intended destination.
  1397. bool isSafeToMove(AAResults *AA, bool &SawStore) const;
  1398. /// Returns true if this instruction's memory access aliases the memory
  1399. /// access of Other.
  1400. //
  1401. /// Assumes any physical registers used to compute addresses
  1402. /// have the same value for both instructions. Returns false if neither
  1403. /// instruction writes to memory.
  1404. ///
  1405. /// @param AA Optional alias analysis, used to compare memory operands.
  1406. /// @param Other MachineInstr to check aliasing against.
  1407. /// @param UseTBAA Whether to pass TBAA information to alias analysis.
  1408. bool mayAlias(AAResults *AA, const MachineInstr &Other, bool UseTBAA) const;
  1409. /// Return true if this instruction may have an ordered
  1410. /// or volatile memory reference, or if the information describing the memory
  1411. /// reference is not available. Return false if it is known to have no
  1412. /// ordered or volatile memory references.
  1413. bool hasOrderedMemoryRef() const;
  1414. /// Return true if this load instruction never traps and points to a memory
  1415. /// location whose value doesn't change during the execution of this function.
  1416. ///
  1417. /// Examples include loading a value from the constant pool or from the
  1418. /// argument area of a function (if it does not change). If the instruction
  1419. /// does multiple loads, this returns true only if all of the loads are
  1420. /// dereferenceable and invariant.
  1421. bool isDereferenceableInvariantLoad(AAResults *AA) const;
  1422. /// If the specified instruction is a PHI that always merges together the
  1423. /// same virtual register, return the register, otherwise return 0.
  1424. unsigned isConstantValuePHI() const;
  1425. /// Return true if this instruction has side effects that are not modeled
  1426. /// by mayLoad / mayStore, etc.
  1427. /// For all instructions, the property is encoded in MCInstrDesc::Flags
  1428. /// (see MCInstrDesc::hasUnmodeledSideEffects(). The only exception is
  1429. /// INLINEASM instruction, in which case the side effect property is encoded
  1430. /// in one of its operands (see InlineAsm::Extra_HasSideEffect).
  1431. ///
  1432. bool hasUnmodeledSideEffects() const;
  1433. /// Returns true if it is illegal to fold a load across this instruction.
  1434. bool isLoadFoldBarrier() const;
  1435. /// Return true if all the defs of this instruction are dead.
  1436. bool allDefsAreDead() const;
  1437. /// Return a valid size if the instruction is a spill instruction.
  1438. Optional<unsigned> getSpillSize(const TargetInstrInfo *TII) const;
  1439. /// Return a valid size if the instruction is a folded spill instruction.
  1440. Optional<unsigned> getFoldedSpillSize(const TargetInstrInfo *TII) const;
  1441. /// Return a valid size if the instruction is a restore instruction.
  1442. Optional<unsigned> getRestoreSize(const TargetInstrInfo *TII) const;
  1443. /// Return a valid size if the instruction is a folded restore instruction.
  1444. Optional<unsigned>
  1445. getFoldedRestoreSize(const TargetInstrInfo *TII) const;
  1446. /// Copy implicit register operands from specified
  1447. /// instruction to this instruction.
  1448. void copyImplicitOps(MachineFunction &MF, const MachineInstr &MI);
  1449. /// Debugging support
  1450. /// @{
  1451. /// Determine the generic type to be printed (if needed) on uses and defs.
  1452. LLT getTypeToPrint(unsigned OpIdx, SmallBitVector &PrintedTypes,
  1453. const MachineRegisterInfo &MRI) const;
  1454. /// Return true when an instruction has tied register that can't be determined
  1455. /// by the instruction's descriptor. This is useful for MIR printing, to
  1456. /// determine whether we need to print the ties or not.
  1457. bool hasComplexRegisterTies() const;
  1458. /// Print this MI to \p OS.
  1459. /// Don't print information that can be inferred from other instructions if
  1460. /// \p IsStandalone is false. It is usually true when only a fragment of the
  1461. /// function is printed.
  1462. /// Only print the defs and the opcode if \p SkipOpers is true.
  1463. /// Otherwise, also print operands if \p SkipDebugLoc is true.
  1464. /// Otherwise, also print the debug loc, with a terminating newline.
  1465. /// \p TII is used to print the opcode name. If it's not present, but the
  1466. /// MI is in a function, the opcode will be printed using the function's TII.
  1467. void print(raw_ostream &OS, bool IsStandalone = true, bool SkipOpers = false,
  1468. bool SkipDebugLoc = false, bool AddNewLine = true,
  1469. const TargetInstrInfo *TII = nullptr) const;
  1470. void print(raw_ostream &OS, ModuleSlotTracker &MST, bool IsStandalone = true,
  1471. bool SkipOpers = false, bool SkipDebugLoc = false,
  1472. bool AddNewLine = true,
  1473. const TargetInstrInfo *TII = nullptr) const;
  1474. void dump() const;
  1475. /// Print on dbgs() the current instruction and the instructions defining its
  1476. /// operands and so on until we reach \p MaxDepth.
  1477. void dumpr(const MachineRegisterInfo &MRI,
  1478. unsigned MaxDepth = UINT_MAX) const;
  1479. /// @}
  1480. //===--------------------------------------------------------------------===//
  1481. // Accessors used to build up machine instructions.
  1482. /// Add the specified operand to the instruction. If it is an implicit
  1483. /// operand, it is added to the end of the operand list. If it is an
  1484. /// explicit operand it is added at the end of the explicit operand list
  1485. /// (before the first implicit operand).
  1486. ///
  1487. /// MF must be the machine function that was used to allocate this
  1488. /// instruction.
  1489. ///
  1490. /// MachineInstrBuilder provides a more convenient interface for creating
  1491. /// instructions and adding operands.
  1492. void addOperand(MachineFunction &MF, const MachineOperand &Op);
  1493. /// Add an operand without providing an MF reference. This only works for
  1494. /// instructions that are inserted in a basic block.
  1495. ///
  1496. /// MachineInstrBuilder and the two-argument addOperand(MF, MO) should be
  1497. /// preferred.
  1498. void addOperand(const MachineOperand &Op);
  1499. /// Replace the instruction descriptor (thus opcode) of
  1500. /// the current instruction with a new one.
  1501. void setDesc(const MCInstrDesc &tid) { MCID = &tid; }
  1502. /// Replace current source information with new such.
  1503. /// Avoid using this, the constructor argument is preferable.
  1504. void setDebugLoc(DebugLoc dl) {
  1505. debugLoc = std::move(dl);
  1506. assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
  1507. }
  1508. /// Erase an operand from an instruction, leaving it with one
  1509. /// fewer operand than it started with.
  1510. void RemoveOperand(unsigned OpNo);
  1511. /// Clear this MachineInstr's memory reference descriptor list. This resets
  1512. /// the memrefs to their most conservative state. This should be used only
  1513. /// as a last resort since it greatly pessimizes our knowledge of the memory
  1514. /// access performed by the instruction.
  1515. void dropMemRefs(MachineFunction &MF);
  1516. /// Assign this MachineInstr's memory reference descriptor list.
  1517. ///
  1518. /// Unlike other methods, this *will* allocate them into a new array
  1519. /// associated with the provided `MachineFunction`.
  1520. void setMemRefs(MachineFunction &MF, ArrayRef<MachineMemOperand *> MemRefs);
  1521. /// Add a MachineMemOperand to the machine instruction.
  1522. /// This function should be used only occasionally. The setMemRefs function
  1523. /// is the primary method for setting up a MachineInstr's MemRefs list.
  1524. void addMemOperand(MachineFunction &MF, MachineMemOperand *MO);
  1525. /// Clone another MachineInstr's memory reference descriptor list and replace
  1526. /// ours with it.
  1527. ///
  1528. /// Note that `*this` may be the incoming MI!
  1529. ///
  1530. /// Prefer this API whenever possible as it can avoid allocations in common
  1531. /// cases.
  1532. void cloneMemRefs(MachineFunction &MF, const MachineInstr &MI);
  1533. /// Clone the merge of multiple MachineInstrs' memory reference descriptors
  1534. /// list and replace ours with it.
  1535. ///
  1536. /// Note that `*this` may be one of the incoming MIs!
  1537. ///
  1538. /// Prefer this API whenever possible as it can avoid allocations in common
  1539. /// cases.
  1540. void cloneMergedMemRefs(MachineFunction &MF,
  1541. ArrayRef<const MachineInstr *> MIs);
  1542. /// Set a symbol that will be emitted just prior to the instruction itself.
  1543. ///
  1544. /// Setting this to a null pointer will remove any such symbol.
  1545. ///
  1546. /// FIXME: This is not fully implemented yet.
  1547. void setPreInstrSymbol(MachineFunction &MF, MCSymbol *Symbol);
  1548. /// Set a symbol that will be emitted just after the instruction itself.
  1549. ///
  1550. /// Setting this to a null pointer will remove any such symbol.
  1551. ///
  1552. /// FIXME: This is not fully implemented yet.
  1553. void setPostInstrSymbol(MachineFunction &MF, MCSymbol *Symbol);
  1554. /// Clone another MachineInstr's pre- and post- instruction symbols and
  1555. /// replace ours with it.
  1556. void cloneInstrSymbols(MachineFunction &MF, const MachineInstr &MI);
  1557. /// Set a marker on instructions that denotes where we should create and emit
  1558. /// heap alloc site labels. This waits until after instruction selection and
  1559. /// optimizations to create the label, so it should still work if the
  1560. /// instruction is removed or duplicated.
  1561. void setHeapAllocMarker(MachineFunction &MF, MDNode *MD);
  1562. /// Return the MIFlags which represent both MachineInstrs. This
  1563. /// should be used when merging two MachineInstrs into one. This routine does
  1564. /// not modify the MIFlags of this MachineInstr.
  1565. uint16_t mergeFlagsWith(const MachineInstr& Other) const;
  1566. static uint16_t copyFlagsFromInstruction(const Instruction &I);
  1567. /// Copy all flags to MachineInst MIFlags
  1568. void copyIRFlags(const Instruction &I);
  1569. /// Break any tie involving OpIdx.
  1570. void untieRegOperand(unsigned OpIdx) {
  1571. MachineOperand &MO = getOperand(OpIdx);
  1572. if (MO.isReg() && MO.isTied()) {
  1573. getOperand(findTiedOperandIdx(OpIdx)).TiedTo = 0;
  1574. MO.TiedTo = 0;
  1575. }
  1576. }
  1577. /// Add all implicit def and use operands to this instruction.
  1578. void addImplicitDefUseOperands(MachineFunction &MF);
  1579. /// Scan instructions immediately following MI and collect any matching
  1580. /// DBG_VALUEs.
  1581. void collectDebugValues(SmallVectorImpl<MachineInstr *> &DbgValues);
  1582. /// Find all DBG_VALUEs that point to the register def in this instruction
  1583. /// and point them to \p Reg instead.
  1584. void changeDebugValuesDefReg(Register Reg);
  1585. /// Returns the Intrinsic::ID for this instruction.
  1586. /// \pre Must have an intrinsic ID operand.
  1587. unsigned getIntrinsicID() const {
  1588. return getOperand(getNumExplicitDefs()).getIntrinsicID();
  1589. }
  1590. /// Sets all register debug operands in this debug value instruction to be
  1591. /// undef.
  1592. void setDebugValueUndef() {
  1593. assert(isDebugValue() && "Must be a debug value instruction.");
  1594. for (MachineOperand &MO : debug_operands()) {
  1595. if (MO.isReg()) {
  1596. MO.setReg(0);
  1597. MO.setSubReg(0);
  1598. }
  1599. }
  1600. }
  1601. PseudoProbeAttributes getPseudoProbeAttribute() const {
  1602. assert(isPseudoProbe() && "Must be a pseudo probe instruction");
  1603. return (PseudoProbeAttributes)getOperand(3).getImm();
  1604. }
  1605. void addPseudoProbeAttribute(PseudoProbeAttributes Attr) {
  1606. assert(isPseudoProbe() && "Must be a pseudo probe instruction");
  1607. MachineOperand &AttrOperand = getOperand(3);
  1608. AttrOperand.setImm(AttrOperand.getImm() | (uint32_t)Attr);
  1609. }
  1610. private:
  1611. /// If this instruction is embedded into a MachineFunction, return the
  1612. /// MachineRegisterInfo object for the current function, otherwise
  1613. /// return null.
  1614. MachineRegisterInfo *getRegInfo();
  1615. /// Unlink all of the register operands in this instruction from their
  1616. /// respective use lists. This requires that the operands already be on their
  1617. /// use lists.
  1618. void RemoveRegOperandsFromUseLists(MachineRegisterInfo&);
  1619. /// Add all of the register operands in this instruction from their
  1620. /// respective use lists. This requires that the operands not be on their
  1621. /// use lists yet.
  1622. void AddRegOperandsToUseLists(MachineRegisterInfo&);
  1623. /// Slow path for hasProperty when we're dealing with a bundle.
  1624. bool hasPropertyInBundle(uint64_t Mask, QueryType Type) const;
  1625. /// Implements the logic of getRegClassConstraintEffectForVReg for the
  1626. /// this MI and the given operand index \p OpIdx.
  1627. /// If the related operand does not constrained Reg, this returns CurRC.
  1628. const TargetRegisterClass *getRegClassConstraintEffectForVRegImpl(
  1629. unsigned OpIdx, Register Reg, const TargetRegisterClass *CurRC,
  1630. const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const;
  1631. /// Stores extra instruction information inline or allocates as ExtraInfo
  1632. /// based on the number of pointers.
  1633. void setExtraInfo(MachineFunction &MF, ArrayRef<MachineMemOperand *> MMOs,
  1634. MCSymbol *PreInstrSymbol, MCSymbol *PostInstrSymbol,
  1635. MDNode *HeapAllocMarker);
  1636. };
  1637. /// Special DenseMapInfo traits to compare MachineInstr* by *value* of the
  1638. /// instruction rather than by pointer value.
  1639. /// The hashing and equality testing functions ignore definitions so this is
  1640. /// useful for CSE, etc.
  1641. struct MachineInstrExpressionTrait : DenseMapInfo<MachineInstr*> {
  1642. static inline MachineInstr *getEmptyKey() {
  1643. return nullptr;
  1644. }
  1645. static inline MachineInstr *getTombstoneKey() {
  1646. return reinterpret_cast<MachineInstr*>(-1);
  1647. }
  1648. static unsigned getHashValue(const MachineInstr* const &MI);
  1649. static bool isEqual(const MachineInstr* const &LHS,
  1650. const MachineInstr* const &RHS) {
  1651. if (RHS == getEmptyKey() || RHS == getTombstoneKey() ||
  1652. LHS == getEmptyKey() || LHS == getTombstoneKey())
  1653. return LHS == RHS;
  1654. return LHS->isIdenticalTo(*RHS, MachineInstr::IgnoreVRegDefs);
  1655. }
  1656. };
  1657. //===----------------------------------------------------------------------===//
  1658. // Debugging Support
  1659. inline raw_ostream& operator<<(raw_ostream &OS, const MachineInstr &MI) {
  1660. MI.print(OS);
  1661. return OS;
  1662. }
  1663. } // end namespace llvm
  1664. #endif // LLVM_CODEGEN_MACHINEINSTR_H