Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1076 lines
44 KiB

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