MachineInstr.h 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242
  1. //===-- llvm/CodeGen/MachineInstr.h - MachineInstr class --------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file contains the declaration of the MachineInstr class, which is the
  11. // basic representation for all target dependent machine instructions used by
  12. // the back end.
  13. //
  14. //===----------------------------------------------------------------------===//
  15. #ifndef LLVM_CODEGEN_MACHINEINSTR_H
  16. #define LLVM_CODEGEN_MACHINEINSTR_H
  17. #include "llvm/ADT/ArrayRef.h"
  18. #include "llvm/ADT/DenseMapInfo.h"
  19. #include "llvm/ADT/STLExtras.h"
  20. #include "llvm/ADT/StringRef.h"
  21. #include "llvm/ADT/ilist.h"
  22. #include "llvm/ADT/ilist_node.h"
  23. #include "llvm/ADT/iterator_range.h"
  24. #include "llvm/CodeGen/MachineOperand.h"
  25. #include "llvm/IR/DebugInfo.h"
  26. #include "llvm/IR/DebugLoc.h"
  27. #include "llvm/IR/InlineAsm.h"
  28. #include "llvm/MC/MCInstrDesc.h"
  29. #include "llvm/Support/ArrayRecycler.h"
  30. #include "llvm/Target/TargetOpcodes.h"
  31. namespace llvm {
  32. template <typename T> class SmallVectorImpl;
  33. class AliasAnalysis;
  34. class TargetInstrInfo;
  35. class TargetRegisterClass;
  36. class TargetRegisterInfo;
  37. class MachineFunction;
  38. class MachineMemOperand;
  39. //===----------------------------------------------------------------------===//
  40. /// Representation of each machine instruction.
  41. ///
  42. /// This class isn't a POD type, but it must have a trivial destructor. When a
  43. /// MachineFunction is deleted, all the contained MachineInstrs are deallocated
  44. /// without having their destructor called.
  45. ///
  46. class MachineInstr : public ilist_node<MachineInstr> {
  47. public:
  48. typedef MachineMemOperand **mmo_iterator;
  49. /// Flags to specify different kinds of comments to output in
  50. /// assembly code. These flags carry semantic information not
  51. /// otherwise easily derivable from the IR text.
  52. ///
  53. enum CommentFlag {
  54. ReloadReuse = 0x1
  55. };
  56. enum MIFlag {
  57. NoFlags = 0,
  58. FrameSetup = 1 << 0, // Instruction is used as a part of
  59. // function frame setup code.
  60. BundledPred = 1 << 1, // Instruction has bundled predecessors.
  61. BundledSucc = 1 << 2 // Instruction has bundled successors.
  62. };
  63. private:
  64. const MCInstrDesc *MCID; // Instruction descriptor.
  65. MachineBasicBlock *Parent; // Pointer to the owning basic block.
  66. // Operands are allocated by an ArrayRecycler.
  67. MachineOperand *Operands; // Pointer to the first operand.
  68. unsigned NumOperands; // Number of operands on instruction.
  69. typedef ArrayRecycler<MachineOperand>::Capacity OperandCapacity;
  70. OperandCapacity CapOperands; // Capacity of the Operands array.
  71. uint8_t Flags; // Various bits of additional
  72. // information about machine
  73. // instruction.
  74. uint8_t AsmPrinterFlags; // Various bits of information used by
  75. // the AsmPrinter to emit helpful
  76. // comments. This is *not* semantic
  77. // information. Do not use this for
  78. // anything other than to convey comment
  79. // information to AsmPrinter.
  80. uint8_t NumMemRefs; // Information on memory references.
  81. mmo_iterator MemRefs;
  82. DebugLoc debugLoc; // Source line information.
  83. MachineInstr(const MachineInstr&) = delete;
  84. void operator=(const MachineInstr&) = delete;
  85. // Use MachineFunction::DeleteMachineInstr() instead.
  86. ~MachineInstr() = delete;
  87. // Intrusive list support
  88. friend struct ilist_traits<MachineInstr>;
  89. friend struct ilist_traits<MachineBasicBlock>;
  90. void setParent(MachineBasicBlock *P) { Parent = P; }
  91. /// This constructor creates a copy of the given
  92. /// MachineInstr in the given MachineFunction.
  93. MachineInstr(MachineFunction &, const MachineInstr &);
  94. /// This constructor create a MachineInstr and add the implicit operands.
  95. /// It reserves space for number of operands specified by
  96. /// MCInstrDesc. An explicit DebugLoc is supplied.
  97. MachineInstr(MachineFunction &, const MCInstrDesc &MCID, DebugLoc dl,
  98. bool NoImp = false);
  99. // MachineInstrs are pool-allocated and owned by MachineFunction.
  100. friend class MachineFunction;
  101. public:
  102. const MachineBasicBlock* getParent() const { return Parent; }
  103. MachineBasicBlock* getParent() { return Parent; }
  104. /// Return the asm printer flags bitvector.
  105. uint8_t getAsmPrinterFlags() const { return AsmPrinterFlags; }
  106. /// Clear the AsmPrinter bitvector.
  107. void clearAsmPrinterFlags() { AsmPrinterFlags = 0; }
  108. /// Return whether an AsmPrinter flag is set.
  109. bool getAsmPrinterFlag(CommentFlag Flag) const {
  110. return AsmPrinterFlags & Flag;
  111. }
  112. /// Set a flag for the AsmPrinter.
  113. void setAsmPrinterFlag(CommentFlag Flag) {
  114. AsmPrinterFlags |= (uint8_t)Flag;
  115. }
  116. /// Clear specific AsmPrinter flags.
  117. void clearAsmPrinterFlag(CommentFlag Flag) {
  118. AsmPrinterFlags &= ~Flag;
  119. }
  120. /// Return the MI flags bitvector.
  121. uint8_t getFlags() const {
  122. return Flags;
  123. }
  124. /// Return whether an MI flag is set.
  125. bool getFlag(MIFlag Flag) const {
  126. return Flags & Flag;
  127. }
  128. /// Set a MI flag.
  129. void setFlag(MIFlag Flag) {
  130. Flags |= (uint8_t)Flag;
  131. }
  132. void setFlags(unsigned flags) {
  133. // Filter out the automatically maintained flags.
  134. unsigned Mask = BundledPred | BundledSucc;
  135. Flags = (Flags & Mask) | (flags & ~Mask);
  136. }
  137. /// clearFlag - Clear a MI flag.
  138. void clearFlag(MIFlag Flag) {
  139. Flags &= ~((uint8_t)Flag);
  140. }
  141. /// Return true if MI is in a bundle (but not the first MI in a bundle).
  142. ///
  143. /// A bundle looks like this before it's finalized:
  144. /// ----------------
  145. /// | MI |
  146. /// ----------------
  147. /// |
  148. /// ----------------
  149. /// | MI * |
  150. /// ----------------
  151. /// |
  152. /// ----------------
  153. /// | MI * |
  154. /// ----------------
  155. /// In this case, the first MI starts a bundle but is not inside a bundle, the
  156. /// next 2 MIs are considered "inside" the bundle.
  157. ///
  158. /// After a bundle is finalized, it looks like this:
  159. /// ----------------
  160. /// | Bundle |
  161. /// ----------------
  162. /// |
  163. /// ----------------
  164. /// | MI * |
  165. /// ----------------
  166. /// |
  167. /// ----------------
  168. /// | MI * |
  169. /// ----------------
  170. /// |
  171. /// ----------------
  172. /// | MI * |
  173. /// ----------------
  174. /// The first instruction has the special opcode "BUNDLE". It's not "inside"
  175. /// a bundle, but the next three MIs are.
  176. bool isInsideBundle() const {
  177. return getFlag(BundledPred);
  178. }
  179. /// Return true if this instruction part of a bundle. This is true
  180. /// if either itself or its following instruction is marked "InsideBundle".
  181. bool isBundled() const {
  182. return isBundledWithPred() || isBundledWithSucc();
  183. }
  184. /// Return true if this instruction is part of a bundle, and it is not the
  185. /// first instruction in the bundle.
  186. bool isBundledWithPred() const { return getFlag(BundledPred); }
  187. /// Return true if this instruction is part of a bundle, and it is not the
  188. /// last instruction in the bundle.
  189. bool isBundledWithSucc() const { return getFlag(BundledSucc); }
  190. /// Bundle this instruction with its predecessor. This can be an unbundled
  191. /// instruction, or it can be the first instruction in a bundle.
  192. void bundleWithPred();
  193. /// Bundle this instruction with its successor. This can be an unbundled
  194. /// instruction, or it can be the last instruction in a bundle.
  195. void bundleWithSucc();
  196. /// Break bundle above this instruction.
  197. void unbundleFromPred();
  198. /// Break bundle below this instruction.
  199. void unbundleFromSucc();
  200. /// Returns the debug location id of this MachineInstr.
  201. const DebugLoc &getDebugLoc() const { return debugLoc; }
  202. /// Return the debug variable referenced by
  203. /// this DBG_VALUE instruction.
  204. const DILocalVariable *getDebugVariable() const {
  205. assert(isDebugValue() && "not a DBG_VALUE");
  206. return cast<DILocalVariable>(getOperand(2).getMetadata());
  207. }
  208. /// Return the complex address expression referenced by
  209. /// this DBG_VALUE instruction.
  210. const DIExpression *getDebugExpression() const {
  211. assert(isDebugValue() && "not a DBG_VALUE");
  212. return cast<DIExpression>(getOperand(3).getMetadata());
  213. }
  214. /// Emit an error referring to the source location of this instruction.
  215. /// This should only be used for inline assembly that is somehow
  216. /// impossible to compile. Other errors should have been handled much
  217. /// earlier.
  218. ///
  219. /// If this method returns, the caller should try to recover from the error.
  220. ///
  221. void emitError(StringRef Msg) const;
  222. /// Returns the target instruction descriptor of this MachineInstr.
  223. const MCInstrDesc &getDesc() const { return *MCID; }
  224. /// Returns the opcode of this MachineInstr.
  225. unsigned getOpcode() const { return MCID->Opcode; }
  226. /// Access to explicit operands of the instruction.
  227. ///
  228. unsigned getNumOperands() const { return NumOperands; }
  229. const MachineOperand& getOperand(unsigned i) const {
  230. assert(i < getNumOperands() && "getOperand() out of range!");
  231. return Operands[i];
  232. }
  233. MachineOperand& getOperand(unsigned i) {
  234. assert(i < getNumOperands() && "getOperand() out of range!");
  235. return Operands[i];
  236. }
  237. /// Returns the number of non-implicit operands.
  238. unsigned getNumExplicitOperands() const;
  239. /// iterator/begin/end - Iterate over all operands of a machine instruction.
  240. typedef MachineOperand *mop_iterator;
  241. typedef const MachineOperand *const_mop_iterator;
  242. mop_iterator operands_begin() { return Operands; }
  243. mop_iterator operands_end() { return Operands + NumOperands; }
  244. const_mop_iterator operands_begin() const { return Operands; }
  245. const_mop_iterator operands_end() const { return Operands + NumOperands; }
  246. iterator_range<mop_iterator> operands() {
  247. return iterator_range<mop_iterator>(operands_begin(), operands_end());
  248. }
  249. iterator_range<const_mop_iterator> operands() const {
  250. return iterator_range<const_mop_iterator>(operands_begin(), operands_end());
  251. }
  252. iterator_range<mop_iterator> explicit_operands() {
  253. return iterator_range<mop_iterator>(
  254. operands_begin(), operands_begin() + getNumExplicitOperands());
  255. }
  256. iterator_range<const_mop_iterator> explicit_operands() const {
  257. return iterator_range<const_mop_iterator>(
  258. operands_begin(), operands_begin() + getNumExplicitOperands());
  259. }
  260. iterator_range<mop_iterator> implicit_operands() {
  261. return iterator_range<mop_iterator>(explicit_operands().end(),
  262. operands_end());
  263. }
  264. iterator_range<const_mop_iterator> implicit_operands() const {
  265. return iterator_range<const_mop_iterator>(explicit_operands().end(),
  266. operands_end());
  267. }
  268. iterator_range<mop_iterator> defs() {
  269. return iterator_range<mop_iterator>(
  270. operands_begin(), operands_begin() + getDesc().getNumDefs());
  271. }
  272. iterator_range<const_mop_iterator> defs() const {
  273. return iterator_range<const_mop_iterator>(
  274. operands_begin(), operands_begin() + getDesc().getNumDefs());
  275. }
  276. iterator_range<mop_iterator> uses() {
  277. return iterator_range<mop_iterator>(
  278. operands_begin() + getDesc().getNumDefs(), operands_end());
  279. }
  280. iterator_range<const_mop_iterator> uses() const {
  281. return iterator_range<const_mop_iterator>(
  282. operands_begin() + getDesc().getNumDefs(), operands_end());
  283. }
  284. /// Returns the number of the operand iterator \p I points to.
  285. unsigned getOperandNo(const_mop_iterator I) const {
  286. return I - operands_begin();
  287. }
  288. /// Access to memory operands of the instruction
  289. mmo_iterator memoperands_begin() const { return MemRefs; }
  290. mmo_iterator memoperands_end() const { return MemRefs + NumMemRefs; }
  291. bool memoperands_empty() const { return NumMemRefs == 0; }
  292. iterator_range<mmo_iterator> memoperands() {
  293. return iterator_range<mmo_iterator>(memoperands_begin(), memoperands_end());
  294. }
  295. iterator_range<mmo_iterator> memoperands() const {
  296. return iterator_range<mmo_iterator>(memoperands_begin(), memoperands_end());
  297. }
  298. /// Return true if this instruction has exactly one MachineMemOperand.
  299. bool hasOneMemOperand() const {
  300. return NumMemRefs == 1;
  301. }
  302. /// API for querying MachineInstr properties. They are the same as MCInstrDesc
  303. /// queries but they are bundle aware.
  304. enum QueryType {
  305. IgnoreBundle, // Ignore bundles
  306. AnyInBundle, // Return true if any instruction in bundle has property
  307. AllInBundle // Return true if all instructions in bundle have property
  308. };
  309. /// Return true if the instruction (or in the case of a bundle,
  310. /// the instructions inside the bundle) has the specified property.
  311. /// The first argument is the property being queried.
  312. /// The second argument indicates whether the query should look inside
  313. /// instruction bundles.
  314. bool hasProperty(unsigned MCFlag, QueryType Type = AnyInBundle) const {
  315. // Inline the fast path for unbundled or bundle-internal instructions.
  316. if (Type == IgnoreBundle || !isBundled() || isBundledWithPred())
  317. return getDesc().getFlags() & (1 << MCFlag);
  318. // If this is the first instruction in a bundle, take the slow path.
  319. return hasPropertyInBundle(1 << MCFlag, Type);
  320. }
  321. /// Return true if this instruction can have a variable number of operands.
  322. /// In this case, the variable operands will be after the normal
  323. /// operands but before the implicit definitions and uses (if any are
  324. /// present).
  325. bool isVariadic(QueryType Type = IgnoreBundle) const {
  326. return hasProperty(MCID::Variadic, Type);
  327. }
  328. /// Set if this instruction has an optional definition, e.g.
  329. /// ARM instructions which can set condition code if 's' bit is set.
  330. bool hasOptionalDef(QueryType Type = IgnoreBundle) const {
  331. return hasProperty(MCID::HasOptionalDef, Type);
  332. }
  333. /// Return true if this is a pseudo instruction that doesn't
  334. /// correspond to a real machine instruction.
  335. bool isPseudo(QueryType Type = IgnoreBundle) const {
  336. return hasProperty(MCID::Pseudo, Type);
  337. }
  338. bool isReturn(QueryType Type = AnyInBundle) const {
  339. return hasProperty(MCID::Return, Type);
  340. }
  341. bool isCall(QueryType Type = AnyInBundle) const {
  342. return hasProperty(MCID::Call, Type);
  343. }
  344. /// Returns true if the specified instruction stops control flow
  345. /// from executing the instruction immediately following it. Examples include
  346. /// unconditional branches and return instructions.
  347. bool isBarrier(QueryType Type = AnyInBundle) const {
  348. return hasProperty(MCID::Barrier, Type);
  349. }
  350. /// Returns true if this instruction part of the terminator for a basic block.
  351. /// Typically this is things like return and branch instructions.
  352. ///
  353. /// Various passes use this to insert code into the bottom of a basic block,
  354. /// but before control flow occurs.
  355. bool isTerminator(QueryType Type = AnyInBundle) const {
  356. return hasProperty(MCID::Terminator, Type);
  357. }
  358. /// Returns true if this is a conditional, unconditional, or indirect branch.
  359. /// Predicates below can be used to discriminate between
  360. /// these cases, and the TargetInstrInfo::AnalyzeBranch method can be used to
  361. /// get more information.
  362. bool isBranch(QueryType Type = AnyInBundle) const {
  363. return hasProperty(MCID::Branch, Type);
  364. }
  365. /// Return true if this is an indirect branch, such as a
  366. /// branch through a register.
  367. bool isIndirectBranch(QueryType Type = AnyInBundle) const {
  368. return hasProperty(MCID::IndirectBranch, Type);
  369. }
  370. /// Return true if this is a branch which may fall
  371. /// through to the next instruction or may transfer control flow to some other
  372. /// block. The TargetInstrInfo::AnalyzeBranch method can be used to get more
  373. /// information about this branch.
  374. bool isConditionalBranch(QueryType Type = AnyInBundle) const {
  375. return isBranch(Type) & !isBarrier(Type) & !isIndirectBranch(Type);
  376. }
  377. /// Return true if this is a branch which always
  378. /// transfers control flow to some other block. The
  379. /// TargetInstrInfo::AnalyzeBranch method can be used to get more information
  380. /// about this branch.
  381. bool isUnconditionalBranch(QueryType Type = AnyInBundle) const {
  382. return isBranch(Type) & isBarrier(Type) & !isIndirectBranch(Type);
  383. }
  384. /// Return true if this instruction has a predicate operand that
  385. /// controls execution. It may be set to 'always', or may be set to other
  386. /// values. There are various methods in TargetInstrInfo that can be used to
  387. /// control and modify the predicate in this instruction.
  388. bool isPredicable(QueryType Type = AllInBundle) const {
  389. // If it's a bundle than all bundled instructions must be predicable for this
  390. // to return true.
  391. return hasProperty(MCID::Predicable, Type);
  392. }
  393. /// Return true if this instruction is a comparison.
  394. bool isCompare(QueryType Type = IgnoreBundle) const {
  395. return hasProperty(MCID::Compare, Type);
  396. }
  397. /// Return true if this instruction is a move immediate
  398. /// (including conditional moves) instruction.
  399. bool isMoveImmediate(QueryType Type = IgnoreBundle) const {
  400. return hasProperty(MCID::MoveImm, Type);
  401. }
  402. /// Return true if this instruction is a bitcast instruction.
  403. bool isBitcast(QueryType Type = IgnoreBundle) const {
  404. return hasProperty(MCID::Bitcast, Type);
  405. }
  406. /// Return true if this instruction is a select instruction.
  407. bool isSelect(QueryType Type = IgnoreBundle) const {
  408. return hasProperty(MCID::Select, Type);
  409. }
  410. /// Return true if this instruction cannot be safely duplicated.
  411. /// For example, if the instruction has a unique labels attached
  412. /// to it, duplicating it would cause multiple definition errors.
  413. bool isNotDuplicable(QueryType Type = AnyInBundle) const {
  414. return hasProperty(MCID::NotDuplicable, Type);
  415. }
  416. /// Return true if this instruction is convergent.
  417. /// Convergent instructions can only be moved to locations that are
  418. /// control-equivalent to their initial position.
  419. bool isConvergent(QueryType Type = AnyInBundle) const {
  420. return hasProperty(MCID::Convergent, Type);
  421. }
  422. /// Returns true if the specified instruction has a delay slot
  423. /// which must be filled by the code generator.
  424. bool hasDelaySlot(QueryType Type = AnyInBundle) const {
  425. return hasProperty(MCID::DelaySlot, Type);
  426. }
  427. /// Return true for instructions that can be folded as
  428. /// memory operands in other instructions. The most common use for this
  429. /// is instructions that are simple loads from memory that don't modify
  430. /// the loaded value in any way, but it can also be used for instructions
  431. /// that can be expressed as constant-pool loads, such as V_SETALLONES
  432. /// on x86, to allow them to be folded when it is beneficial.
  433. /// This should only be set on instructions that return a value in their
  434. /// only virtual register definition.
  435. bool canFoldAsLoad(QueryType Type = IgnoreBundle) const {
  436. return hasProperty(MCID::FoldableAsLoad, Type);
  437. }
  438. /// \brief Return true if this instruction behaves
  439. /// the same way as the generic REG_SEQUENCE instructions.
  440. /// E.g., on ARM,
  441. /// dX VMOVDRR rY, rZ
  442. /// is equivalent to
  443. /// dX = REG_SEQUENCE rY, ssub_0, rZ, ssub_1.
  444. ///
  445. /// Note that for the optimizers to be able to take advantage of
  446. /// this property, TargetInstrInfo::getRegSequenceLikeInputs has to be
  447. /// override accordingly.
  448. bool isRegSequenceLike(QueryType Type = IgnoreBundle) const {
  449. return hasProperty(MCID::RegSequence, Type);
  450. }
  451. /// \brief Return true if this instruction behaves
  452. /// the same way as the generic EXTRACT_SUBREG instructions.
  453. /// E.g., on ARM,
  454. /// rX, rY VMOVRRD dZ
  455. /// is equivalent to two EXTRACT_SUBREG:
  456. /// rX = EXTRACT_SUBREG dZ, ssub_0
  457. /// rY = EXTRACT_SUBREG dZ, ssub_1
  458. ///
  459. /// Note that for the optimizers to be able to take advantage of
  460. /// this property, TargetInstrInfo::getExtractSubregLikeInputs has to be
  461. /// override accordingly.
  462. bool isExtractSubregLike(QueryType Type = IgnoreBundle) const {
  463. return hasProperty(MCID::ExtractSubreg, Type);
  464. }
  465. /// \brief Return true if this instruction behaves
  466. /// the same way as the generic INSERT_SUBREG instructions.
  467. /// E.g., on ARM,
  468. /// dX = VSETLNi32 dY, rZ, Imm
  469. /// is equivalent to a INSERT_SUBREG:
  470. /// dX = INSERT_SUBREG dY, rZ, translateImmToSubIdx(Imm)
  471. ///
  472. /// Note that for the optimizers to be able to take advantage of
  473. /// this property, TargetInstrInfo::getInsertSubregLikeInputs has to be
  474. /// override accordingly.
  475. bool isInsertSubregLike(QueryType Type = IgnoreBundle) const {
  476. return hasProperty(MCID::InsertSubreg, Type);
  477. }
  478. //===--------------------------------------------------------------------===//
  479. // Side Effect Analysis
  480. //===--------------------------------------------------------------------===//
  481. /// Return true if this instruction could possibly read memory.
  482. /// Instructions with this flag set are not necessarily simple load
  483. /// instructions, they may load a value and modify it, for example.
  484. bool mayLoad(QueryType Type = AnyInBundle) const {
  485. if (isInlineAsm()) {
  486. unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
  487. if (ExtraInfo & InlineAsm::Extra_MayLoad)
  488. return true;
  489. }
  490. return hasProperty(MCID::MayLoad, Type);
  491. }
  492. /// Return true if this instruction could possibly modify memory.
  493. /// Instructions with this flag set are not necessarily simple store
  494. /// instructions, they may store a modified value based on their operands, or
  495. /// may not actually modify anything, for example.
  496. bool mayStore(QueryType Type = AnyInBundle) const {
  497. if (isInlineAsm()) {
  498. unsigned ExtraInfo = getOperand(InlineAsm::MIOp_ExtraInfo).getImm();
  499. if (ExtraInfo & InlineAsm::Extra_MayStore)
  500. return true;
  501. }
  502. return hasProperty(MCID::MayStore, Type);
  503. }
  504. /// Return true if this instruction could possibly read or modify memory.
  505. bool mayLoadOrStore(QueryType Type = AnyInBundle) const {
  506. return mayLoad(Type) || mayStore(Type);
  507. }
  508. //===--------------------------------------------------------------------===//
  509. // Flags that indicate whether an instruction can be modified by a method.
  510. //===--------------------------------------------------------------------===//
  511. /// Return true if this may be a 2- or 3-address
  512. /// instruction (of the form "X = op Y, Z, ..."), which produces the same
  513. /// result if Y and Z are exchanged. If this flag is set, then the
  514. /// TargetInstrInfo::commuteInstruction method may be used to hack on the
  515. /// instruction.
  516. ///
  517. /// Note that this flag may be set on instructions that are only commutable
  518. /// sometimes. In these cases, the call to commuteInstruction will fail.
  519. /// Also note that some instructions require non-trivial modification to
  520. /// commute them.
  521. bool isCommutable(QueryType Type = IgnoreBundle) const {
  522. return hasProperty(MCID::Commutable, Type);
  523. }
  524. /// Return true if this is a 2-address instruction
  525. /// which can be changed into a 3-address instruction if needed. Doing this
  526. /// transformation can be profitable in the register allocator, because it
  527. /// means that the instruction can use a 2-address form if possible, but
  528. /// degrade into a less efficient form if the source and dest register cannot
  529. /// be assigned to the same register. For example, this allows the x86
  530. /// backend to turn a "shl reg, 3" instruction into an LEA instruction, which
  531. /// is the same speed as the shift but has bigger code size.
  532. ///
  533. /// If this returns true, then the target must implement the
  534. /// TargetInstrInfo::convertToThreeAddress method for this instruction, which
  535. /// is allowed to fail if the transformation isn't valid for this specific
  536. /// instruction (e.g. shl reg, 4 on x86).
  537. ///
  538. bool isConvertibleTo3Addr(QueryType Type = IgnoreBundle) const {
  539. return hasProperty(MCID::ConvertibleTo3Addr, Type);
  540. }
  541. /// Return true if this instruction requires
  542. /// custom insertion support when the DAG scheduler is inserting it into a
  543. /// machine basic block. If this is true for the instruction, it basically
  544. /// means that it is a pseudo instruction used at SelectionDAG time that is
  545. /// expanded out into magic code by the target when MachineInstrs are formed.
  546. ///
  547. /// If this is true, the TargetLoweringInfo::InsertAtEndOfBasicBlock method
  548. /// is used to insert this into the MachineBasicBlock.
  549. bool usesCustomInsertionHook(QueryType Type = IgnoreBundle) const {
  550. return hasProperty(MCID::UsesCustomInserter, Type);
  551. }
  552. /// Return true if this instruction requires *adjustment*
  553. /// after instruction selection by calling a target hook. For example, this
  554. /// can be used to fill in ARM 's' optional operand depending on whether
  555. /// the conditional flag register is used.
  556. bool hasPostISelHook(QueryType Type = IgnoreBundle) const {
  557. return hasProperty(MCID::HasPostISelHook, Type);
  558. }
  559. /// Returns true if this instruction is a candidate for remat.
  560. /// This flag is deprecated, please don't use it anymore. If this
  561. /// flag is set, the isReallyTriviallyReMaterializable() method is called to
  562. /// verify the instruction is really rematable.
  563. bool isRematerializable(QueryType Type = AllInBundle) const {
  564. // It's only possible to re-mat a bundle if all bundled instructions are
  565. // re-materializable.
  566. return hasProperty(MCID::Rematerializable, Type);
  567. }
  568. /// Returns true if this instruction has the same cost (or less) than a move
  569. /// instruction. This is useful during certain types of optimizations
  570. /// (e.g., remat during two-address conversion or machine licm)
  571. /// where we would like to remat or hoist the instruction, but not if it costs
  572. /// more than moving the instruction into the appropriate register. Note, we
  573. /// are not marking copies from and to the same register class with this flag.
  574. bool isAsCheapAsAMove(QueryType Type = AllInBundle) const {
  575. // Only returns true for a bundle if all bundled instructions are cheap.
  576. return hasProperty(MCID::CheapAsAMove, Type);
  577. }
  578. /// Returns true if this instruction source operands
  579. /// have special register allocation requirements that are not captured by the
  580. /// operand register classes. e.g. ARM::STRD's two source registers must be an
  581. /// even / odd pair, ARM::STM registers have to be in ascending order.
  582. /// Post-register allocation passes should not attempt to change allocations
  583. /// for sources of instructions with this flag.
  584. bool hasExtraSrcRegAllocReq(QueryType Type = AnyInBundle) const {
  585. return hasProperty(MCID::ExtraSrcRegAllocReq, Type);
  586. }
  587. /// Returns true if this instruction def operands
  588. /// have special register allocation requirements that are not captured by the
  589. /// operand register classes. e.g. ARM::LDRD's two def registers must be an
  590. /// even / odd pair, ARM::LDM registers have to be in ascending order.
  591. /// Post-register allocation passes should not attempt to change allocations
  592. /// for definitions of instructions with this flag.
  593. bool hasExtraDefRegAllocReq(QueryType Type = AnyInBundle) const {
  594. return hasProperty(MCID::ExtraDefRegAllocReq, Type);
  595. }
  596. enum MICheckType {
  597. CheckDefs, // Check all operands for equality
  598. CheckKillDead, // Check all operands including kill / dead markers
  599. IgnoreDefs, // Ignore all definitions
  600. IgnoreVRegDefs // Ignore virtual register definitions
  601. };
  602. /// Return true if this instruction is identical to (same
  603. /// opcode and same operands as) the specified instruction.
  604. bool isIdenticalTo(const MachineInstr *Other,
  605. MICheckType Check = CheckDefs) const;
  606. /// Unlink 'this' from the containing basic block, and return it without
  607. /// deleting it.
  608. ///
  609. /// This function can not be used on bundled instructions, use
  610. /// removeFromBundle() to remove individual instructions from a bundle.
  611. MachineInstr *removeFromParent();
  612. /// Unlink this instruction from its basic block and return it without
  613. /// deleting it.
  614. ///
  615. /// If the instruction is part of a bundle, the other instructions in the
  616. /// bundle remain bundled.
  617. MachineInstr *removeFromBundle();
  618. /// Unlink 'this' from the containing basic block and delete it.
  619. ///
  620. /// If this instruction is the header of a bundle, the whole bundle is erased.
  621. /// This function can not be used for instructions inside a bundle, use
  622. /// eraseFromBundle() to erase individual bundled instructions.
  623. void eraseFromParent();
  624. /// Unlink 'this' from the containing basic block and delete it.
  625. ///
  626. /// For all definitions mark their uses in DBG_VALUE nodes
  627. /// as undefined. Otherwise like eraseFromParent().
  628. void eraseFromParentAndMarkDBGValuesForRemoval();
  629. /// Unlink 'this' form its basic block and delete it.
  630. ///
  631. /// If the instruction is part of a bundle, the other instructions in the
  632. /// bundle remain bundled.
  633. void eraseFromBundle();
  634. bool isEHLabel() const { return getOpcode() == TargetOpcode::EH_LABEL; }
  635. bool isGCLabel() const { return getOpcode() == TargetOpcode::GC_LABEL; }
  636. /// Returns true if the MachineInstr represents a label.
  637. bool isLabel() const { return isEHLabel() || isGCLabel(); }
  638. bool isCFIInstruction() const {
  639. return getOpcode() == TargetOpcode::CFI_INSTRUCTION;
  640. }
  641. // True if the instruction represents a position in the function.
  642. bool isPosition() const { return isLabel() || isCFIInstruction(); }
  643. bool isDebugValue() const { return getOpcode() == TargetOpcode::DBG_VALUE; }
  644. /// A DBG_VALUE is indirect iff the first operand is a register and
  645. /// the second operand is an immediate.
  646. bool isIndirectDebugValue() const {
  647. return isDebugValue()
  648. && getOperand(0).isReg()
  649. && getOperand(1).isImm();
  650. }
  651. bool isPHI() const { return getOpcode() == TargetOpcode::PHI; }
  652. bool isKill() const { return getOpcode() == TargetOpcode::KILL; }
  653. bool isImplicitDef() const { return getOpcode()==TargetOpcode::IMPLICIT_DEF; }
  654. bool isInlineAsm() const { return getOpcode() == TargetOpcode::INLINEASM; }
  655. bool isMSInlineAsm() const {
  656. return getOpcode() == TargetOpcode::INLINEASM && getInlineAsmDialect();
  657. }
  658. bool isStackAligningInlineAsm() const;
  659. InlineAsm::AsmDialect getInlineAsmDialect() const;
  660. bool isInsertSubreg() const {
  661. return getOpcode() == TargetOpcode::INSERT_SUBREG;
  662. }
  663. bool isSubregToReg() const {
  664. return getOpcode() == TargetOpcode::SUBREG_TO_REG;
  665. }
  666. bool isRegSequence() const {
  667. return getOpcode() == TargetOpcode::REG_SEQUENCE;
  668. }
  669. bool isBundle() const {
  670. return getOpcode() == TargetOpcode::BUNDLE;
  671. }
  672. bool isCopy() const {
  673. return getOpcode() == TargetOpcode::COPY;
  674. }
  675. bool isFullCopy() const {
  676. return isCopy() && !getOperand(0).getSubReg() && !getOperand(1).getSubReg();
  677. }
  678. bool isExtractSubreg() const {
  679. return getOpcode() == TargetOpcode::EXTRACT_SUBREG;
  680. }
  681. /// Return true if the instruction behaves like a copy.
  682. /// This does not include native copy instructions.
  683. bool isCopyLike() const {
  684. return isCopy() || isSubregToReg();
  685. }
  686. /// Return true is the instruction is an identity copy.
  687. bool isIdentityCopy() const {
  688. return isCopy() && getOperand(0).getReg() == getOperand(1).getReg() &&
  689. getOperand(0).getSubReg() == getOperand(1).getSubReg();
  690. }
  691. /// Return true if this is a transient instruction that is
  692. /// either very likely to be eliminated during register allocation (such as
  693. /// copy-like instructions), or if this instruction doesn't have an
  694. /// execution-time cost.
  695. bool isTransient() const {
  696. switch(getOpcode()) {
  697. default: return false;
  698. // Copy-like instructions are usually eliminated during register allocation.
  699. case TargetOpcode::PHI:
  700. case TargetOpcode::COPY:
  701. case TargetOpcode::INSERT_SUBREG:
  702. case TargetOpcode::SUBREG_TO_REG:
  703. case TargetOpcode::REG_SEQUENCE:
  704. // Pseudo-instructions that don't produce any real output.
  705. case TargetOpcode::IMPLICIT_DEF:
  706. case TargetOpcode::KILL:
  707. case TargetOpcode::CFI_INSTRUCTION:
  708. case TargetOpcode::EH_LABEL:
  709. case TargetOpcode::GC_LABEL:
  710. case TargetOpcode::DBG_VALUE:
  711. return true;
  712. }
  713. }
  714. /// Return the number of instructions inside the MI bundle, excluding the
  715. /// bundle header.
  716. ///
  717. /// This is the number of instructions that MachineBasicBlock::iterator
  718. /// skips, 0 for unbundled instructions.
  719. unsigned getBundleSize() const;
  720. /// Return true if the MachineInstr reads the specified register.
  721. /// If TargetRegisterInfo is passed, then it also checks if there
  722. /// is a read of a super-register.
  723. /// This does not count partial redefines of virtual registers as reads:
  724. /// %reg1024:6 = OP.
  725. bool readsRegister(unsigned Reg,
  726. const TargetRegisterInfo *TRI = nullptr) const {
  727. return findRegisterUseOperandIdx(Reg, false, TRI) != -1;
  728. }
  729. /// Return true if the MachineInstr reads the specified virtual register.
  730. /// Take into account that a partial define is a
  731. /// read-modify-write operation.
  732. bool readsVirtualRegister(unsigned Reg) const {
  733. return readsWritesVirtualRegister(Reg).first;
  734. }
  735. /// Return a pair of bools (reads, writes) indicating if this instruction
  736. /// reads or writes Reg. This also considers partial defines.
  737. /// If Ops is not null, all operand indices for Reg are added.
  738. std::pair<bool,bool> readsWritesVirtualRegister(unsigned Reg,
  739. SmallVectorImpl<unsigned> *Ops = nullptr) const;
  740. /// Return true if the MachineInstr kills the specified register.
  741. /// If TargetRegisterInfo is passed, then it also checks if there is
  742. /// a kill of a super-register.
  743. bool killsRegister(unsigned Reg,
  744. const TargetRegisterInfo *TRI = nullptr) const {
  745. return findRegisterUseOperandIdx(Reg, true, TRI) != -1;
  746. }
  747. /// Return true if the MachineInstr fully defines the specified register.
  748. /// If TargetRegisterInfo is passed, then it also checks
  749. /// if there is a def of a super-register.
  750. /// NOTE: It's ignoring subreg indices on virtual registers.
  751. bool definesRegister(unsigned Reg,
  752. const TargetRegisterInfo *TRI = nullptr) const {
  753. return findRegisterDefOperandIdx(Reg, false, false, TRI) != -1;
  754. }
  755. /// Return true if the MachineInstr modifies (fully define or partially
  756. /// define) the specified register.
  757. /// NOTE: It's ignoring subreg indices on virtual registers.
  758. bool modifiesRegister(unsigned Reg, const TargetRegisterInfo *TRI) const {
  759. return findRegisterDefOperandIdx(Reg, false, true, TRI) != -1;
  760. }
  761. /// Returns true if the register is dead in this machine instruction.
  762. /// If TargetRegisterInfo is passed, then it also checks
  763. /// if there is a dead def of a super-register.
  764. bool registerDefIsDead(unsigned Reg,
  765. const TargetRegisterInfo *TRI = nullptr) const {
  766. return findRegisterDefOperandIdx(Reg, true, false, TRI) != -1;
  767. }
  768. /// Returns the operand index that is a use of the specific register or -1
  769. /// if it is not found. It further tightens the search criteria to a use
  770. /// that kills the register if isKill is true.
  771. int findRegisterUseOperandIdx(unsigned Reg, bool isKill = false,
  772. const TargetRegisterInfo *TRI = nullptr) const;
  773. /// Wrapper for findRegisterUseOperandIdx, it returns
  774. /// a pointer to the MachineOperand rather than an index.
  775. MachineOperand *findRegisterUseOperand(unsigned Reg, bool isKill = false,
  776. const TargetRegisterInfo *TRI = nullptr) {
  777. int Idx = findRegisterUseOperandIdx(Reg, isKill, TRI);
  778. return (Idx == -1) ? nullptr : &getOperand(Idx);
  779. }
  780. /// Returns the operand index that is a def of the specified register or
  781. /// -1 if it is not found. If isDead is true, defs that are not dead are
  782. /// skipped. If Overlap is true, then it also looks for defs that merely
  783. /// overlap the specified register. If TargetRegisterInfo is non-null,
  784. /// then it also checks if there is a def of a super-register.
  785. /// This may also return a register mask operand when Overlap is true.
  786. int findRegisterDefOperandIdx(unsigned Reg,
  787. bool isDead = false, bool Overlap = false,
  788. const TargetRegisterInfo *TRI = nullptr) const;
  789. /// Wrapper for findRegisterDefOperandIdx, it returns
  790. /// a pointer to the MachineOperand rather than an index.
  791. MachineOperand *findRegisterDefOperand(unsigned Reg, bool isDead = false,
  792. const TargetRegisterInfo *TRI = nullptr) {
  793. int Idx = findRegisterDefOperandIdx(Reg, isDead, false, TRI);
  794. return (Idx == -1) ? nullptr : &getOperand(Idx);
  795. }
  796. /// Find the index of the first operand in the
  797. /// operand list that is used to represent the predicate. It returns -1 if
  798. /// none is found.
  799. int findFirstPredOperandIdx() const;
  800. /// Find the index of the flag word operand that
  801. /// corresponds to operand OpIdx on an inline asm instruction. Returns -1 if
  802. /// getOperand(OpIdx) does not belong to an inline asm operand group.
  803. ///
  804. /// If GroupNo is not NULL, it will receive the number of the operand group
  805. /// containing OpIdx.
  806. ///
  807. /// The flag operand is an immediate that can be decoded with methods like
  808. /// InlineAsm::hasRegClassConstraint().
  809. ///
  810. int findInlineAsmFlagIdx(unsigned OpIdx, unsigned *GroupNo = nullptr) const;
  811. /// Compute the static register class constraint for operand OpIdx.
  812. /// For normal instructions, this is derived from the MCInstrDesc.
  813. /// For inline assembly it is derived from the flag words.
  814. ///
  815. /// Returns NULL if the static register class constraint cannot be
  816. /// determined.
  817. ///
  818. const TargetRegisterClass*
  819. getRegClassConstraint(unsigned OpIdx,
  820. const TargetInstrInfo *TII,
  821. const TargetRegisterInfo *TRI) const;
  822. /// \brief Applies the constraints (def/use) implied by this MI on \p Reg to
  823. /// the given \p CurRC.
  824. /// If \p ExploreBundle is set and MI is part of a bundle, all the
  825. /// instructions inside the bundle will be taken into account. In other words,
  826. /// this method accumulates all the constraints of the operand of this MI and
  827. /// the related bundle if MI is a bundle or inside a bundle.
  828. ///
  829. /// Returns the register class that satisfies both \p CurRC and the
  830. /// constraints set by MI. Returns NULL if such a register class does not
  831. /// exist.
  832. ///
  833. /// \pre CurRC must not be NULL.
  834. const TargetRegisterClass *getRegClassConstraintEffectForVReg(
  835. unsigned Reg, const TargetRegisterClass *CurRC,
  836. const TargetInstrInfo *TII, const TargetRegisterInfo *TRI,
  837. bool ExploreBundle = false) const;
  838. /// \brief Applies the constraints (def/use) implied by the \p OpIdx operand
  839. /// to the given \p CurRC.
  840. ///
  841. /// Returns the register class that satisfies both \p CurRC and the
  842. /// constraints set by \p OpIdx MI. Returns NULL if such a register class
  843. /// does not exist.
  844. ///
  845. /// \pre CurRC must not be NULL.
  846. /// \pre The operand at \p OpIdx must be a register.
  847. const TargetRegisterClass *
  848. getRegClassConstraintEffect(unsigned OpIdx, const TargetRegisterClass *CurRC,
  849. const TargetInstrInfo *TII,
  850. const TargetRegisterInfo *TRI) const;
  851. /// Add a tie between the register operands at DefIdx and UseIdx.
  852. /// The tie will cause the register allocator to ensure that the two
  853. /// operands are assigned the same physical register.
  854. ///
  855. /// Tied operands are managed automatically for explicit operands in the
  856. /// MCInstrDesc. This method is for exceptional cases like inline asm.
  857. void tieOperands(unsigned DefIdx, unsigned UseIdx);
  858. /// Given the index of a tied register operand, find the
  859. /// operand it is tied to. Defs are tied to uses and vice versa. Returns the
  860. /// index of the tied operand which must exist.
  861. unsigned findTiedOperandIdx(unsigned OpIdx) const;
  862. /// Given the index of a register def operand,
  863. /// check if the register def is tied to a source operand, due to either
  864. /// two-address elimination or inline assembly constraints. Returns the
  865. /// first tied use operand index by reference if UseOpIdx is not null.
  866. bool isRegTiedToUseOperand(unsigned DefOpIdx,
  867. unsigned *UseOpIdx = nullptr) const {
  868. const MachineOperand &MO = getOperand(DefOpIdx);
  869. if (!MO.isReg() || !MO.isDef() || !MO.isTied())
  870. return false;
  871. if (UseOpIdx)
  872. *UseOpIdx = findTiedOperandIdx(DefOpIdx);
  873. return true;
  874. }
  875. /// Return true if the use operand of the specified index is tied to a def
  876. /// operand. It also returns the def operand index by reference if DefOpIdx
  877. /// is not null.
  878. bool isRegTiedToDefOperand(unsigned UseOpIdx,
  879. unsigned *DefOpIdx = nullptr) const {
  880. const MachineOperand &MO = getOperand(UseOpIdx);
  881. if (!MO.isReg() || !MO.isUse() || !MO.isTied())
  882. return false;
  883. if (DefOpIdx)
  884. *DefOpIdx = findTiedOperandIdx(UseOpIdx);
  885. return true;
  886. }
  887. /// Clears kill flags on all operands.
  888. void clearKillInfo();
  889. /// Replace all occurrences of FromReg with ToReg:SubIdx,
  890. /// properly composing subreg indices where necessary.
  891. void substituteRegister(unsigned FromReg, unsigned ToReg, unsigned SubIdx,
  892. const TargetRegisterInfo &RegInfo);
  893. /// We have determined MI kills a register. Look for the
  894. /// operand that uses it and mark it as IsKill. If AddIfNotFound is true,
  895. /// add a implicit operand if it's not found. Returns true if the operand
  896. /// exists / is added.
  897. bool addRegisterKilled(unsigned IncomingReg,
  898. const TargetRegisterInfo *RegInfo,
  899. bool AddIfNotFound = false);
  900. /// Clear all kill flags affecting Reg. If RegInfo is
  901. /// provided, this includes super-register kills.
  902. void clearRegisterKills(unsigned Reg, const TargetRegisterInfo *RegInfo);
  903. /// We have determined MI defined a register without a use.
  904. /// Look for the operand that defines it and mark it as IsDead. If
  905. /// AddIfNotFound is true, add a implicit operand if it's not found. Returns
  906. /// true if the operand exists / is added.
  907. bool addRegisterDead(unsigned Reg, const TargetRegisterInfo *RegInfo,
  908. bool AddIfNotFound = false);
  909. /// Clear all dead flags on operands defining register @p Reg.
  910. void clearRegisterDeads(unsigned Reg);
  911. /// Mark all subregister defs of register @p Reg with the undef flag.
  912. /// This function is used when we determined to have a subregister def in an
  913. /// otherwise undefined super register.
  914. void addRegisterDefReadUndef(unsigned Reg);
  915. /// We have determined MI defines a register. Make sure there is an operand
  916. /// defining Reg.
  917. void addRegisterDefined(unsigned Reg,
  918. const TargetRegisterInfo *RegInfo = nullptr);
  919. /// Mark every physreg used by this instruction as
  920. /// dead except those in the UsedRegs list.
  921. ///
  922. /// On instructions with register mask operands, also add implicit-def
  923. /// operands for all registers in UsedRegs.
  924. void setPhysRegsDeadExcept(ArrayRef<unsigned> UsedRegs,
  925. const TargetRegisterInfo &TRI);
  926. /// Return true if it is safe to move this instruction. If
  927. /// SawStore is set to true, it means that there is a store (or call) between
  928. /// the instruction's location and its intended destination.
  929. bool isSafeToMove(AliasAnalysis *AA, bool &SawStore) const;
  930. /// Return true if this instruction may have an ordered
  931. /// or volatile memory reference, or if the information describing the memory
  932. /// reference is not available. Return false if it is known to have no
  933. /// ordered or volatile memory references.
  934. bool hasOrderedMemoryRef() const;
  935. /// Return true if this instruction is loading from a
  936. /// location whose value is invariant across the function. For example,
  937. /// loading a value from the constant pool or from the argument area of
  938. /// a function if it does not change. This should only return true of *all*
  939. /// loads the instruction does are invariant (if it does multiple loads).
  940. bool isInvariantLoad(AliasAnalysis *AA) const;
  941. /// If the specified instruction is a PHI that always merges together the
  942. /// same virtual register, return the register, otherwise return 0.
  943. unsigned isConstantValuePHI() const;
  944. /// Return true if this instruction has side effects that are not modeled
  945. /// by mayLoad / mayStore, etc.
  946. /// For all instructions, the property is encoded in MCInstrDesc::Flags
  947. /// (see MCInstrDesc::hasUnmodeledSideEffects(). The only exception is
  948. /// INLINEASM instruction, in which case the side effect property is encoded
  949. /// in one of its operands (see InlineAsm::Extra_HasSideEffect).
  950. ///
  951. bool hasUnmodeledSideEffects() const;
  952. /// Return true if all the defs of this instruction are dead.
  953. bool allDefsAreDead() const;
  954. /// Copy implicit register operands from specified
  955. /// instruction to this instruction.
  956. void copyImplicitOps(MachineFunction &MF, const MachineInstr *MI);
  957. //
  958. // Debugging support
  959. //
  960. void print(raw_ostream &OS, bool SkipOpers = false) const;
  961. void print(raw_ostream &OS, ModuleSlotTracker &MST,
  962. bool SkipOpers = false) const;
  963. void dump() const;
  964. //===--------------------------------------------------------------------===//
  965. // Accessors used to build up machine instructions.
  966. /// Add the specified operand to the instruction. If it is an implicit
  967. /// operand, it is added to the end of the operand list. If it is an
  968. /// explicit operand it is added at the end of the explicit operand list
  969. /// (before the first implicit operand).
  970. ///
  971. /// MF must be the machine function that was used to allocate this
  972. /// instruction.
  973. ///
  974. /// MachineInstrBuilder provides a more convenient interface for creating
  975. /// instructions and adding operands.
  976. void addOperand(MachineFunction &MF, const MachineOperand &Op);
  977. /// Add an operand without providing an MF reference. This only works for
  978. /// instructions that are inserted in a basic block.
  979. ///
  980. /// MachineInstrBuilder and the two-argument addOperand(MF, MO) should be
  981. /// preferred.
  982. void addOperand(const MachineOperand &Op);
  983. /// Replace the instruction descriptor (thus opcode) of
  984. /// the current instruction with a new one.
  985. void setDesc(const MCInstrDesc &tid) { MCID = &tid; }
  986. /// Replace current source information with new such.
  987. /// Avoid using this, the constructor argument is preferable.
  988. void setDebugLoc(DebugLoc dl) {
  989. debugLoc = std::move(dl);
  990. assert(debugLoc.hasTrivialDestructor() && "Expected trivial destructor");
  991. }
  992. /// Erase an operand from an instruction, leaving it with one
  993. /// fewer operand than it started with.
  994. void RemoveOperand(unsigned i);
  995. /// Add a MachineMemOperand to the machine instruction.
  996. /// This function should be used only occasionally. The setMemRefs function
  997. /// is the primary method for setting up a MachineInstr's MemRefs list.
  998. void addMemOperand(MachineFunction &MF, MachineMemOperand *MO);
  999. /// Assign this MachineInstr's memory reference descriptor list.
  1000. /// This does not transfer ownership.
  1001. void setMemRefs(mmo_iterator NewMemRefs, mmo_iterator NewMemRefsEnd) {
  1002. MemRefs = NewMemRefs;
  1003. NumMemRefs = uint8_t(NewMemRefsEnd - NewMemRefs);
  1004. assert(NumMemRefs == NewMemRefsEnd - NewMemRefs && "Too many memrefs");
  1005. }
  1006. /// Clear this MachineInstr's memory reference descriptor list.
  1007. void clearMemRefs() {
  1008. MemRefs = nullptr;
  1009. NumMemRefs = 0;
  1010. }
  1011. /// Break any tie involving OpIdx.
  1012. void untieRegOperand(unsigned OpIdx) {
  1013. MachineOperand &MO = getOperand(OpIdx);
  1014. if (MO.isReg() && MO.isTied()) {
  1015. getOperand(findTiedOperandIdx(OpIdx)).TiedTo = 0;
  1016. MO.TiedTo = 0;
  1017. }
  1018. }
  1019. private:
  1020. /// If this instruction is embedded into a MachineFunction, return the
  1021. /// MachineRegisterInfo object for the current function, otherwise
  1022. /// return null.
  1023. MachineRegisterInfo *getRegInfo();
  1024. /// Add all implicit def and use operands to this instruction.
  1025. void addImplicitDefUseOperands(MachineFunction &MF);
  1026. /// Unlink all of the register operands in this instruction from their
  1027. /// respective use lists. This requires that the operands already be on their
  1028. /// use lists.
  1029. void RemoveRegOperandsFromUseLists(MachineRegisterInfo&);
  1030. /// Add all of the register operands in this instruction from their
  1031. /// respective use lists. This requires that the operands not be on their
  1032. /// use lists yet.
  1033. void AddRegOperandsToUseLists(MachineRegisterInfo&);
  1034. /// Slow path for hasProperty when we're dealing with a bundle.
  1035. bool hasPropertyInBundle(unsigned Mask, QueryType Type) const;
  1036. /// \brief Implements the logic of getRegClassConstraintEffectForVReg for the
  1037. /// this MI and the given operand index \p OpIdx.
  1038. /// If the related operand does not constrained Reg, this returns CurRC.
  1039. const TargetRegisterClass *getRegClassConstraintEffectForVRegImpl(
  1040. unsigned OpIdx, unsigned Reg, const TargetRegisterClass *CurRC,
  1041. const TargetInstrInfo *TII, const TargetRegisterInfo *TRI) const;
  1042. };
  1043. /// Special DenseMapInfo traits to compare MachineInstr* by *value* of the
  1044. /// instruction rather than by pointer value.
  1045. /// The hashing and equality testing functions ignore definitions so this is
  1046. /// useful for CSE, etc.
  1047. struct MachineInstrExpressionTrait : DenseMapInfo<MachineInstr*> {
  1048. static inline MachineInstr *getEmptyKey() {
  1049. return nullptr;
  1050. }
  1051. static inline MachineInstr *getTombstoneKey() {
  1052. return reinterpret_cast<MachineInstr*>(-1);
  1053. }
  1054. static unsigned getHashValue(const MachineInstr* const &MI);
  1055. static bool isEqual(const MachineInstr* const &LHS,
  1056. const MachineInstr* const &RHS) {
  1057. if (RHS == getEmptyKey() || RHS == getTombstoneKey() ||
  1058. LHS == getEmptyKey() || LHS == getTombstoneKey())
  1059. return LHS == RHS;
  1060. return LHS->isIdenticalTo(RHS, MachineInstr::IgnoreVRegDefs);
  1061. }
  1062. };
  1063. // //
  1064. ///////////////////////////////////////////////////////////////////////////////
  1065. // Debugging Support
  1066. inline raw_ostream& operator<<(raw_ostream &OS, const MachineInstr &MI) {
  1067. MI.print(OS);
  1068. return OS;
  1069. }
  1070. } // End llvm namespace
  1071. #endif