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.

416 lines
15 KiB

  1. //===-- llvm/Value.h - Definition of the Value 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 declares the Value class.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #ifndef LLVM_VALUE_H
  14. #define LLVM_VALUE_H
  15. #include "llvm/Use.h"
  16. #include "llvm/Support/Casting.h"
  17. #include "llvm/Support/Compiler.h"
  18. namespace llvm {
  19. class Constant;
  20. class Argument;
  21. class Instruction;
  22. class BasicBlock;
  23. class GlobalValue;
  24. class Function;
  25. class GlobalVariable;
  26. class GlobalAlias;
  27. class InlineAsm;
  28. class ValueSymbolTable;
  29. template<typename ValueTy> class StringMapEntry;
  30. typedef StringMapEntry<Value*> ValueName;
  31. class raw_ostream;
  32. class AssemblyAnnotationWriter;
  33. class ValueHandleBase;
  34. class LLVMContext;
  35. class Twine;
  36. class MDNode;
  37. class Type;
  38. class StringRef;
  39. //===----------------------------------------------------------------------===//
  40. // Value Class
  41. //===----------------------------------------------------------------------===//
  42. /// This is a very important LLVM class. It is the base class of all values
  43. /// computed by a program that may be used as operands to other values. Value is
  44. /// the super class of other important classes such as Instruction and Function.
  45. /// All Values have a Type. Type is not a subclass of Value. Some values can
  46. /// have a name and they belong to some Module. Setting the name on the Value
  47. /// automatically updates the module's symbol table.
  48. ///
  49. /// Every value has a "use list" that keeps track of which other Values are
  50. /// using this Value. A Value can also have an arbitrary number of ValueHandle
  51. /// objects that watch it and listen to RAUW and Destroy events. See
  52. /// llvm/Support/ValueHandle.h for details.
  53. ///
  54. /// @brief LLVM Value Representation
  55. class Value {
  56. const unsigned char SubclassID; // Subclass identifier (for isa/dyn_cast)
  57. unsigned char HasValueHandle : 1; // Has a ValueHandle pointing to this?
  58. protected:
  59. /// SubclassOptionalData - This member is similar to SubclassData, however it
  60. /// is for holding information which may be used to aid optimization, but
  61. /// which may be cleared to zero without affecting conservative
  62. /// interpretation.
  63. unsigned char SubclassOptionalData : 7;
  64. private:
  65. /// SubclassData - This member is defined by this class, but is not used for
  66. /// anything. Subclasses can use it to hold whatever state they find useful.
  67. /// This field is initialized to zero by the ctor.
  68. unsigned short SubclassData;
  69. Type *VTy;
  70. Use *UseList;
  71. friend class ValueSymbolTable; // Allow ValueSymbolTable to directly mod Name.
  72. friend class ValueHandleBase;
  73. ValueName *Name;
  74. void operator=(const Value &) LLVM_DELETED_FUNCTION;
  75. Value(const Value &) LLVM_DELETED_FUNCTION;
  76. protected:
  77. /// printCustom - Value subclasses can override this to implement custom
  78. /// printing behavior.
  79. virtual void printCustom(raw_ostream &O) const;
  80. Value(Type *Ty, unsigned scid);
  81. public:
  82. virtual ~Value();
  83. /// dump - Support for debugging, callable in GDB: V->dump()
  84. //
  85. void dump() const;
  86. /// print - Implement operator<< on Value.
  87. ///
  88. void print(raw_ostream &O, AssemblyAnnotationWriter *AAW = 0) const;
  89. /// All values are typed, get the type of this value.
  90. ///
  91. Type *getType() const { return VTy; }
  92. /// All values hold a context through their type.
  93. LLVMContext &getContext() const;
  94. // All values can potentially be named.
  95. bool hasName() const { return Name != 0 && SubclassID != MDStringVal; }
  96. ValueName *getValueName() const { return Name; }
  97. void setValueName(ValueName *VN) { Name = VN; }
  98. /// getName() - Return a constant reference to the value's name. This is cheap
  99. /// and guaranteed to return the same reference as long as the value is not
  100. /// modified.
  101. StringRef getName() const;
  102. /// setName() - Change the name of the value, choosing a new unique name if
  103. /// the provided name is taken.
  104. ///
  105. /// \param Name The new name; or "" if the value's name should be removed.
  106. void setName(const Twine &Name);
  107. /// takeName - transfer the name from V to this value, setting V's name to
  108. /// empty. It is an error to call V->takeName(V).
  109. void takeName(Value *V);
  110. /// replaceAllUsesWith - Go through the uses list for this definition and make
  111. /// each use point to "V" instead of "this". After this completes, 'this's
  112. /// use list is guaranteed to be empty.
  113. ///
  114. void replaceAllUsesWith(Value *V);
  115. //----------------------------------------------------------------------
  116. // Methods for handling the chain of uses of this Value.
  117. //
  118. typedef value_use_iterator<User> use_iterator;
  119. typedef value_use_iterator<const User> const_use_iterator;
  120. bool use_empty() const { return UseList == 0; }
  121. use_iterator use_begin() { return use_iterator(UseList); }
  122. const_use_iterator use_begin() const { return const_use_iterator(UseList); }
  123. use_iterator use_end() { return use_iterator(0); }
  124. const_use_iterator use_end() const { return const_use_iterator(0); }
  125. User *use_back() { return *use_begin(); }
  126. const User *use_back() const { return *use_begin(); }
  127. /// hasOneUse - Return true if there is exactly one user of this value. This
  128. /// is specialized because it is a common request and does not require
  129. /// traversing the whole use list.
  130. ///
  131. bool hasOneUse() const {
  132. const_use_iterator I = use_begin(), E = use_end();
  133. if (I == E) return false;
  134. return ++I == E;
  135. }
  136. /// hasNUses - Return true if this Value has exactly N users.
  137. ///
  138. bool hasNUses(unsigned N) const;
  139. /// hasNUsesOrMore - Return true if this value has N users or more. This is
  140. /// logically equivalent to getNumUses() >= N.
  141. ///
  142. bool hasNUsesOrMore(unsigned N) const;
  143. bool isUsedInBasicBlock(const BasicBlock *BB) const;
  144. /// getNumUses - This method computes the number of uses of this Value. This
  145. /// is a linear time operation. Use hasOneUse, hasNUses, or hasNUsesOrMore
  146. /// to check for specific values.
  147. unsigned getNumUses() const;
  148. /// addUse - This method should only be used by the Use class.
  149. ///
  150. void addUse(Use &U) { U.addToList(&UseList); }
  151. /// An enumeration for keeping track of the concrete subclass of Value that
  152. /// is actually instantiated. Values of this enumeration are kept in the
  153. /// Value classes SubclassID field. They are used for concrete type
  154. /// identification.
  155. enum ValueTy {
  156. ArgumentVal, // This is an instance of Argument
  157. BasicBlockVal, // This is an instance of BasicBlock
  158. FunctionVal, // This is an instance of Function
  159. GlobalAliasVal, // This is an instance of GlobalAlias
  160. GlobalVariableVal, // This is an instance of GlobalVariable
  161. UndefValueVal, // This is an instance of UndefValue
  162. BlockAddressVal, // This is an instance of BlockAddress
  163. ConstantExprVal, // This is an instance of ConstantExpr
  164. ConstantAggregateZeroVal, // This is an instance of ConstantAggregateZero
  165. ConstantDataArrayVal, // This is an instance of ConstantDataArray
  166. ConstantDataVectorVal, // This is an instance of ConstantDataVector
  167. ConstantIntVal, // This is an instance of ConstantInt
  168. ConstantFPVal, // This is an instance of ConstantFP
  169. ConstantArrayVal, // This is an instance of ConstantArray
  170. ConstantStructVal, // This is an instance of ConstantStruct
  171. ConstantVectorVal, // This is an instance of ConstantVector
  172. ConstantPointerNullVal, // This is an instance of ConstantPointerNull
  173. MDNodeVal, // This is an instance of MDNode
  174. MDStringVal, // This is an instance of MDString
  175. InlineAsmVal, // This is an instance of InlineAsm
  176. PseudoSourceValueVal, // This is an instance of PseudoSourceValue
  177. FixedStackPseudoSourceValueVal, // This is an instance of
  178. // FixedStackPseudoSourceValue
  179. InstructionVal, // This is an instance of Instruction
  180. // Enum values starting at InstructionVal are used for Instructions;
  181. // don't add new values here!
  182. // Markers:
  183. ConstantFirstVal = FunctionVal,
  184. ConstantLastVal = ConstantPointerNullVal
  185. };
  186. /// getValueID - Return an ID for the concrete type of this object. This is
  187. /// used to implement the classof checks. This should not be used for any
  188. /// other purpose, as the values may change as LLVM evolves. Also, note that
  189. /// for instructions, the Instruction's opcode is added to InstructionVal. So
  190. /// this means three things:
  191. /// # there is no value with code InstructionVal (no opcode==0).
  192. /// # there are more possible values for the value type than in ValueTy enum.
  193. /// # the InstructionVal enumerator must be the highest valued enumerator in
  194. /// the ValueTy enum.
  195. unsigned getValueID() const {
  196. return SubclassID;
  197. }
  198. /// getRawSubclassOptionalData - Return the raw optional flags value
  199. /// contained in this value. This should only be used when testing two
  200. /// Values for equivalence.
  201. unsigned getRawSubclassOptionalData() const {
  202. return SubclassOptionalData;
  203. }
  204. /// clearSubclassOptionalData - Clear the optional flags contained in
  205. /// this value.
  206. void clearSubclassOptionalData() {
  207. SubclassOptionalData = 0;
  208. }
  209. /// hasSameSubclassOptionalData - Test whether the optional flags contained
  210. /// in this value are equal to the optional flags in the given value.
  211. bool hasSameSubclassOptionalData(const Value *V) const {
  212. return SubclassOptionalData == V->SubclassOptionalData;
  213. }
  214. /// intersectOptionalDataWith - Clear any optional flags in this value
  215. /// that are not also set in the given value.
  216. void intersectOptionalDataWith(const Value *V) {
  217. SubclassOptionalData &= V->SubclassOptionalData;
  218. }
  219. /// hasValueHandle - Return true if there is a value handle associated with
  220. /// this value.
  221. bool hasValueHandle() const { return HasValueHandle; }
  222. // Methods for support type inquiry through isa, cast, and dyn_cast:
  223. static inline bool classof(const Value *) {
  224. return true; // Values are always values.
  225. }
  226. /// stripPointerCasts - This method strips off any unneeded pointer casts and
  227. /// all-zero GEPs from the specified value, returning the original uncasted
  228. /// value. If this is called on a non-pointer value, it returns 'this'.
  229. Value *stripPointerCasts();
  230. const Value *stripPointerCasts() const {
  231. return const_cast<Value*>(this)->stripPointerCasts();
  232. }
  233. /// stripInBoundsConstantOffsets - This method strips off unneeded pointer casts and
  234. /// all-constant GEPs from the specified value, returning the original
  235. /// pointer value. If this is called on a non-pointer value, it returns
  236. /// 'this'.
  237. Value *stripInBoundsConstantOffsets();
  238. const Value *stripInBoundsConstantOffsets() const {
  239. return const_cast<Value*>(this)->stripInBoundsConstantOffsets();
  240. }
  241. /// stripInBoundsOffsets - This method strips off unneeded pointer casts and
  242. /// any in-bounds Offsets from the specified value, returning the original
  243. /// pointer value. If this is called on a non-pointer value, it returns
  244. /// 'this'.
  245. Value *stripInBoundsOffsets();
  246. const Value *stripInBoundsOffsets() const {
  247. return const_cast<Value*>(this)->stripInBoundsOffsets();
  248. }
  249. /// isDereferenceablePointer - Test if this value is always a pointer to
  250. /// allocated and suitably aligned memory for a simple load or store.
  251. bool isDereferenceablePointer() const;
  252. /// DoPHITranslation - If this value is a PHI node with CurBB as its parent,
  253. /// return the value in the PHI node corresponding to PredBB. If not, return
  254. /// ourself. This is useful if you want to know the value something has in a
  255. /// predecessor block.
  256. Value *DoPHITranslation(const BasicBlock *CurBB, const BasicBlock *PredBB);
  257. const Value *DoPHITranslation(const BasicBlock *CurBB,
  258. const BasicBlock *PredBB) const{
  259. return const_cast<Value*>(this)->DoPHITranslation(CurBB, PredBB);
  260. }
  261. /// MaximumAlignment - This is the greatest alignment value supported by
  262. /// load, store, and alloca instructions, and global values.
  263. static const unsigned MaximumAlignment = 1u << 29;
  264. /// mutateType - Mutate the type of this Value to be of the specified type.
  265. /// Note that this is an extremely dangerous operation which can create
  266. /// completely invalid IR very easily. It is strongly recommended that you
  267. /// recreate IR objects with the right types instead of mutating them in
  268. /// place.
  269. void mutateType(Type *Ty) {
  270. VTy = Ty;
  271. }
  272. protected:
  273. unsigned short getSubclassDataFromValue() const { return SubclassData; }
  274. void setValueSubclassData(unsigned short D) { SubclassData = D; }
  275. };
  276. inline raw_ostream &operator<<(raw_ostream &OS, const Value &V) {
  277. V.print(OS);
  278. return OS;
  279. }
  280. void Use::set(Value *V) {
  281. if (Val) removeFromList();
  282. Val = V;
  283. if (V) V->addUse(*this);
  284. }
  285. // isa - Provide some specializations of isa so that we don't have to include
  286. // the subtype header files to test to see if the value is a subclass...
  287. //
  288. template <> struct isa_impl<Constant, Value> {
  289. static inline bool doit(const Value &Val) {
  290. return Val.getValueID() >= Value::ConstantFirstVal &&
  291. Val.getValueID() <= Value::ConstantLastVal;
  292. }
  293. };
  294. template <> struct isa_impl<Argument, Value> {
  295. static inline bool doit (const Value &Val) {
  296. return Val.getValueID() == Value::ArgumentVal;
  297. }
  298. };
  299. template <> struct isa_impl<InlineAsm, Value> {
  300. static inline bool doit(const Value &Val) {
  301. return Val.getValueID() == Value::InlineAsmVal;
  302. }
  303. };
  304. template <> struct isa_impl<Instruction, Value> {
  305. static inline bool doit(const Value &Val) {
  306. return Val.getValueID() >= Value::InstructionVal;
  307. }
  308. };
  309. template <> struct isa_impl<BasicBlock, Value> {
  310. static inline bool doit(const Value &Val) {
  311. return Val.getValueID() == Value::BasicBlockVal;
  312. }
  313. };
  314. template <> struct isa_impl<Function, Value> {
  315. static inline bool doit(const Value &Val) {
  316. return Val.getValueID() == Value::FunctionVal;
  317. }
  318. };
  319. template <> struct isa_impl<GlobalVariable, Value> {
  320. static inline bool doit(const Value &Val) {
  321. return Val.getValueID() == Value::GlobalVariableVal;
  322. }
  323. };
  324. template <> struct isa_impl<GlobalAlias, Value> {
  325. static inline bool doit(const Value &Val) {
  326. return Val.getValueID() == Value::GlobalAliasVal;
  327. }
  328. };
  329. template <> struct isa_impl<GlobalValue, Value> {
  330. static inline bool doit(const Value &Val) {
  331. return isa<GlobalVariable>(Val) || isa<Function>(Val) ||
  332. isa<GlobalAlias>(Val);
  333. }
  334. };
  335. template <> struct isa_impl<MDNode, Value> {
  336. static inline bool doit(const Value &Val) {
  337. return Val.getValueID() == Value::MDNodeVal;
  338. }
  339. };
  340. // Value* is only 4-byte aligned.
  341. template<>
  342. class PointerLikeTypeTraits<Value*> {
  343. typedef Value* PT;
  344. public:
  345. static inline void *getAsVoidPointer(PT P) { return P; }
  346. static inline PT getFromVoidPointer(void *P) {
  347. return static_cast<PT>(P);
  348. }
  349. enum { NumLowBitsAvailable = 2 };
  350. };
  351. } // End llvm namespace
  352. #endif