MachineInstr.h 79 KB

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