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.

408 lines
15 KiB

  1. //===-- llvm/Instruction.h - Instruction class definition -------*- 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 Instruction class, which is the
  11. // base class for all of the LLVM instructions.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_INSTRUCTION_H
  15. #define LLVM_INSTRUCTION_H
  16. #include "llvm/User.h"
  17. #include "llvm/ADT/ilist_node.h"
  18. #include "llvm/Support/DebugLoc.h"
  19. namespace llvm {
  20. class LLVMContext;
  21. class MDNode;
  22. template<typename ValueSubClass, typename ItemParentClass>
  23. class SymbolTableListTraits;
  24. class Instruction : public User, public ilist_node<Instruction> {
  25. void operator=(const Instruction &) LLVM_DELETED_FUNCTION;
  26. Instruction(const Instruction &) LLVM_DELETED_FUNCTION;
  27. BasicBlock *Parent;
  28. DebugLoc DbgLoc; // 'dbg' Metadata cache.
  29. enum {
  30. /// HasMetadataBit - This is a bit stored in the SubClassData field which
  31. /// indicates whether this instruction has metadata attached to it or not.
  32. HasMetadataBit = 1 << 15
  33. };
  34. public:
  35. // Out of line virtual method, so the vtable, etc has a home.
  36. ~Instruction();
  37. /// use_back - Specialize the methods defined in Value, as we know that an
  38. /// instruction can only be used by other instructions.
  39. Instruction *use_back() { return cast<Instruction>(*use_begin());}
  40. const Instruction *use_back() const { return cast<Instruction>(*use_begin());}
  41. inline const BasicBlock *getParent() const { return Parent; }
  42. inline BasicBlock *getParent() { return Parent; }
  43. /// removeFromParent - This method unlinks 'this' from the containing basic
  44. /// block, but does not delete it.
  45. ///
  46. void removeFromParent();
  47. /// eraseFromParent - This method unlinks 'this' from the containing basic
  48. /// block and deletes it.
  49. ///
  50. void eraseFromParent();
  51. /// insertBefore - Insert an unlinked instructions into a basic block
  52. /// immediately before the specified instruction.
  53. void insertBefore(Instruction *InsertPos);
  54. /// insertAfter - Insert an unlinked instructions into a basic block
  55. /// immediately after the specified instruction.
  56. void insertAfter(Instruction *InsertPos);
  57. /// moveBefore - Unlink this instruction from its current basic block and
  58. /// insert it into the basic block that MovePos lives in, right before
  59. /// MovePos.
  60. void moveBefore(Instruction *MovePos);
  61. //===--------------------------------------------------------------------===//
  62. // Subclass classification.
  63. //===--------------------------------------------------------------------===//
  64. /// getOpcode() returns a member of one of the enums like Instruction::Add.
  65. unsigned getOpcode() const { return getValueID() - InstructionVal; }
  66. const char *getOpcodeName() const { return getOpcodeName(getOpcode()); }
  67. bool isTerminator() const { return isTerminator(getOpcode()); }
  68. bool isBinaryOp() const { return isBinaryOp(getOpcode()); }
  69. bool isShift() { return isShift(getOpcode()); }
  70. bool isCast() const { return isCast(getOpcode()); }
  71. static const char* getOpcodeName(unsigned OpCode);
  72. static inline bool isTerminator(unsigned OpCode) {
  73. return OpCode >= TermOpsBegin && OpCode < TermOpsEnd;
  74. }
  75. static inline bool isBinaryOp(unsigned Opcode) {
  76. return Opcode >= BinaryOpsBegin && Opcode < BinaryOpsEnd;
  77. }
  78. /// @brief Determine if the Opcode is one of the shift instructions.
  79. static inline bool isShift(unsigned Opcode) {
  80. return Opcode >= Shl && Opcode <= AShr;
  81. }
  82. /// isLogicalShift - Return true if this is a logical shift left or a logical
  83. /// shift right.
  84. inline bool isLogicalShift() const {
  85. return getOpcode() == Shl || getOpcode() == LShr;
  86. }
  87. /// isArithmeticShift - Return true if this is an arithmetic shift right.
  88. inline bool isArithmeticShift() const {
  89. return getOpcode() == AShr;
  90. }
  91. /// @brief Determine if the OpCode is one of the CastInst instructions.
  92. static inline bool isCast(unsigned OpCode) {
  93. return OpCode >= CastOpsBegin && OpCode < CastOpsEnd;
  94. }
  95. //===--------------------------------------------------------------------===//
  96. // Metadata manipulation.
  97. //===--------------------------------------------------------------------===//
  98. /// hasMetadata() - Return true if this instruction has any metadata attached
  99. /// to it.
  100. bool hasMetadata() const {
  101. return !DbgLoc.isUnknown() || hasMetadataHashEntry();
  102. }
  103. /// hasMetadataOtherThanDebugLoc - Return true if this instruction has
  104. /// metadata attached to it other than a debug location.
  105. bool hasMetadataOtherThanDebugLoc() const {
  106. return hasMetadataHashEntry();
  107. }
  108. /// getMetadata - Get the metadata of given kind attached to this Instruction.
  109. /// If the metadata is not found then return null.
  110. MDNode *getMetadata(unsigned KindID) const {
  111. if (!hasMetadata()) return 0;
  112. return getMetadataImpl(KindID);
  113. }
  114. /// getMetadata - Get the metadata of given kind attached to this Instruction.
  115. /// If the metadata is not found then return null.
  116. MDNode *getMetadata(StringRef Kind) const {
  117. if (!hasMetadata()) return 0;
  118. return getMetadataImpl(Kind);
  119. }
  120. /// getAllMetadata - Get all metadata attached to this Instruction. The first
  121. /// element of each pair returned is the KindID, the second element is the
  122. /// metadata value. This list is returned sorted by the KindID.
  123. void getAllMetadata(SmallVectorImpl<std::pair<unsigned, MDNode*> > &MDs)const{
  124. if (hasMetadata())
  125. getAllMetadataImpl(MDs);
  126. }
  127. /// getAllMetadataOtherThanDebugLoc - This does the same thing as
  128. /// getAllMetadata, except that it filters out the debug location.
  129. void getAllMetadataOtherThanDebugLoc(SmallVectorImpl<std::pair<unsigned,
  130. MDNode*> > &MDs) const {
  131. if (hasMetadataOtherThanDebugLoc())
  132. getAllMetadataOtherThanDebugLocImpl(MDs);
  133. }
  134. /// setMetadata - Set the metadata of the specified kind to the specified
  135. /// node. This updates/replaces metadata if already present, or removes it if
  136. /// Node is null.
  137. void setMetadata(unsigned KindID, MDNode *Node);
  138. void setMetadata(StringRef Kind, MDNode *Node);
  139. /// setDebugLoc - Set the debug location information for this instruction.
  140. void setDebugLoc(const DebugLoc &Loc) { DbgLoc = Loc; }
  141. /// getDebugLoc - Return the debug location for this node as a DebugLoc.
  142. const DebugLoc &getDebugLoc() const { return DbgLoc; }
  143. private:
  144. /// hasMetadataHashEntry - Return true if we have an entry in the on-the-side
  145. /// metadata hash.
  146. bool hasMetadataHashEntry() const {
  147. return (getSubclassDataFromValue() & HasMetadataBit) != 0;
  148. }
  149. // These are all implemented in Metadata.cpp.
  150. MDNode *getMetadataImpl(unsigned KindID) const;
  151. MDNode *getMetadataImpl(StringRef Kind) const;
  152. void getAllMetadataImpl(SmallVectorImpl<std::pair<unsigned,MDNode*> > &)const;
  153. void getAllMetadataOtherThanDebugLocImpl(SmallVectorImpl<std::pair<unsigned,
  154. MDNode*> > &) const;
  155. void clearMetadataHashEntries();
  156. public:
  157. //===--------------------------------------------------------------------===//
  158. // Predicates and helper methods.
  159. //===--------------------------------------------------------------------===//
  160. /// isAssociative - Return true if the instruction is associative:
  161. ///
  162. /// Associative operators satisfy: x op (y op z) === (x op y) op z
  163. ///
  164. /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
  165. ///
  166. bool isAssociative() const { return isAssociative(getOpcode()); }
  167. static bool isAssociative(unsigned op);
  168. /// isCommutative - Return true if the instruction is commutative:
  169. ///
  170. /// Commutative operators satisfy: (x op y) === (y op x)
  171. ///
  172. /// In LLVM, these are the associative operators, plus SetEQ and SetNE, when
  173. /// applied to any type.
  174. ///
  175. bool isCommutative() const { return isCommutative(getOpcode()); }
  176. static bool isCommutative(unsigned op);
  177. /// isIdempotent - Return true if the instruction is idempotent:
  178. ///
  179. /// Idempotent operators satisfy: x op x === x
  180. ///
  181. /// In LLVM, the And and Or operators are idempotent.
  182. ///
  183. bool isIdempotent() const { return isIdempotent(getOpcode()); }
  184. static bool isIdempotent(unsigned op);
  185. /// isNilpotent - Return true if the instruction is nilpotent:
  186. ///
  187. /// Nilpotent operators satisfy: x op x === Id,
  188. ///
  189. /// where Id is the identity for the operator, i.e. a constant such that
  190. /// x op Id === x and Id op x === x for all x.
  191. ///
  192. /// In LLVM, the Xor operator is nilpotent.
  193. ///
  194. bool isNilpotent() const { return isNilpotent(getOpcode()); }
  195. static bool isNilpotent(unsigned op);
  196. /// mayWriteToMemory - Return true if this instruction may modify memory.
  197. ///
  198. bool mayWriteToMemory() const;
  199. /// mayReadFromMemory - Return true if this instruction may read memory.
  200. ///
  201. bool mayReadFromMemory() const;
  202. /// mayReadOrWriteMemory - Return true if this instruction may read or
  203. /// write memory.
  204. ///
  205. bool mayReadOrWriteMemory() const {
  206. return mayReadFromMemory() || mayWriteToMemory();
  207. }
  208. /// mayThrow - Return true if this instruction may throw an exception.
  209. ///
  210. bool mayThrow() const;
  211. /// mayHaveSideEffects - Return true if the instruction may have side effects.
  212. ///
  213. /// Note that this does not consider malloc and alloca to have side
  214. /// effects because the newly allocated memory is completely invisible to
  215. /// instructions which don't used the returned value. For cases where this
  216. /// matters, isSafeToSpeculativelyExecute may be more appropriate.
  217. bool mayHaveSideEffects() const {
  218. return mayWriteToMemory() || mayThrow();
  219. }
  220. /// clone() - Create a copy of 'this' instruction that is identical in all
  221. /// ways except the following:
  222. /// * The instruction has no parent
  223. /// * The instruction has no name
  224. ///
  225. Instruction *clone() const;
  226. /// isIdenticalTo - Return true if the specified instruction is exactly
  227. /// identical to the current one. This means that all operands match and any
  228. /// extra information (e.g. load is volatile) agree.
  229. bool isIdenticalTo(const Instruction *I) const;
  230. /// isIdenticalToWhenDefined - This is like isIdenticalTo, except that it
  231. /// ignores the SubclassOptionalData flags, which specify conditions
  232. /// under which the instruction's result is undefined.
  233. bool isIdenticalToWhenDefined(const Instruction *I) const;
  234. /// When checking for operation equivalence (using isSameOperationAs) it is
  235. /// sometimes useful to ignore certain attributes.
  236. enum OperationEquivalenceFlags {
  237. /// Check for equivalence ignoring load/store alignment.
  238. CompareIgnoringAlignment = 1<<0,
  239. /// Check for equivalence treating a type and a vector of that type
  240. /// as equivalent.
  241. CompareUsingScalarTypes = 1<<1
  242. };
  243. /// This function determines if the specified instruction executes the same
  244. /// operation as the current one. This means that the opcodes, type, operand
  245. /// types and any other factors affecting the operation must be the same. This
  246. /// is similar to isIdenticalTo except the operands themselves don't have to
  247. /// be identical.
  248. /// @returns true if the specified instruction is the same operation as
  249. /// the current one.
  250. /// @brief Determine if one instruction is the same operation as another.
  251. bool isSameOperationAs(const Instruction *I, unsigned flags = 0) const;
  252. /// isUsedOutsideOfBlock - Return true if there are any uses of this
  253. /// instruction in blocks other than the specified block. Note that PHI nodes
  254. /// are considered to evaluate their operands in the corresponding predecessor
  255. /// block.
  256. bool isUsedOutsideOfBlock(const BasicBlock *BB) const;
  257. /// Methods for support type inquiry through isa, cast, and dyn_cast:
  258. static inline bool classof(const Instruction *) { return true; }
  259. static inline bool classof(const Value *V) {
  260. return V->getValueID() >= Value::InstructionVal;
  261. }
  262. //----------------------------------------------------------------------
  263. // Exported enumerations.
  264. //
  265. enum TermOps { // These terminate basic blocks
  266. #define FIRST_TERM_INST(N) TermOpsBegin = N,
  267. #define HANDLE_TERM_INST(N, OPC, CLASS) OPC = N,
  268. #define LAST_TERM_INST(N) TermOpsEnd = N+1
  269. #include "llvm/Instruction.def"
  270. };
  271. enum BinaryOps {
  272. #define FIRST_BINARY_INST(N) BinaryOpsBegin = N,
  273. #define HANDLE_BINARY_INST(N, OPC, CLASS) OPC = N,
  274. #define LAST_BINARY_INST(N) BinaryOpsEnd = N+1
  275. #include "llvm/Instruction.def"
  276. };
  277. enum MemoryOps {
  278. #define FIRST_MEMORY_INST(N) MemoryOpsBegin = N,
  279. #define HANDLE_MEMORY_INST(N, OPC, CLASS) OPC = N,
  280. #define LAST_MEMORY_INST(N) MemoryOpsEnd = N+1
  281. #include "llvm/Instruction.def"
  282. };
  283. enum CastOps {
  284. #define FIRST_CAST_INST(N) CastOpsBegin = N,
  285. #define HANDLE_CAST_INST(N, OPC, CLASS) OPC = N,
  286. #define LAST_CAST_INST(N) CastOpsEnd = N+1
  287. #include "llvm/Instruction.def"
  288. };
  289. enum OtherOps {
  290. #define FIRST_OTHER_INST(N) OtherOpsBegin = N,
  291. #define HANDLE_OTHER_INST(N, OPC, CLASS) OPC = N,
  292. #define LAST_OTHER_INST(N) OtherOpsEnd = N+1
  293. #include "llvm/Instruction.def"
  294. };
  295. private:
  296. // Shadow Value::setValueSubclassData with a private forwarding method so that
  297. // subclasses cannot accidentally use it.
  298. void setValueSubclassData(unsigned short D) {
  299. Value::setValueSubclassData(D);
  300. }
  301. unsigned short getSubclassDataFromValue() const {
  302. return Value::getSubclassDataFromValue();
  303. }
  304. void setHasMetadataHashEntry(bool V) {
  305. setValueSubclassData((getSubclassDataFromValue() & ~HasMetadataBit) |
  306. (V ? HasMetadataBit : 0));
  307. }
  308. friend class SymbolTableListTraits<Instruction, BasicBlock>;
  309. void setParent(BasicBlock *P);
  310. protected:
  311. // Instruction subclasses can stick up to 15 bits of stuff into the
  312. // SubclassData field of instruction with these members.
  313. // Verify that only the low 15 bits are used.
  314. void setInstructionSubclassData(unsigned short D) {
  315. assert((D & HasMetadataBit) == 0 && "Out of range value put into field");
  316. setValueSubclassData((getSubclassDataFromValue() & HasMetadataBit) | D);
  317. }
  318. unsigned getSubclassDataFromInstruction() const {
  319. return getSubclassDataFromValue() & ~HasMetadataBit;
  320. }
  321. Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
  322. Instruction *InsertBefore = 0);
  323. Instruction(Type *Ty, unsigned iType, Use *Ops, unsigned NumOps,
  324. BasicBlock *InsertAtEnd);
  325. virtual Instruction *clone_impl() const = 0;
  326. };
  327. // Instruction* is only 4-byte aligned.
  328. template<>
  329. class PointerLikeTypeTraits<Instruction*> {
  330. typedef Instruction* PT;
  331. public:
  332. static inline void *getAsVoidPointer(PT P) { return P; }
  333. static inline PT getFromVoidPointer(void *P) {
  334. return static_cast<PT>(P);
  335. }
  336. enum { NumLowBitsAvailable = 2 };
  337. };
  338. } // End llvm namespace
  339. #endif