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.

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