1 //===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file is a common base class of all globally definable objects. As such,
11 // it is subclassed by GlobalVariable, GlobalAlias and by Function. This is
12 // used because you can do certain things with these global objects that you
13 // can't do to anything else. For example, use the address of one as a
16 //===----------------------------------------------------------------------===//
18 #ifndef LLVM_GLOBALVALUE_H
19 #define LLVM_GLOBALVALUE_H
21 #include "llvm/Constant.h"
28 class GlobalValue : public Constant {
29 GlobalValue(const GlobalValue &); // do not implement
31 /// @brief An enumeration for the kinds of linkage for global values.
33 ExternalLinkage = 0,///< Externally visible function
34 LinkOnceLinkage, ///< Keep one copy of function when linking (inline)
35 WeakLinkage, ///< Keep one copy of named function when linking (weak)
36 AppendingLinkage, ///< Special purpose, only applies to global arrays
37 InternalLinkage, ///< Rename collisions when linking (static functions)
38 DLLImportLinkage, ///< Function to be imported from DLL
39 DLLExportLinkage, ///< Function to be accessible from DLL
40 ExternalWeakLinkage,///< ExternalWeak linkage description
41 GhostLinkage ///< Stand-in functions for streaming fns from BC files
44 /// @brief An enumeration for the kinds of visibility of global values.
45 enum VisibilityTypes {
46 DefaultVisibility = 0, ///< The GV is visible
47 HiddenVisibility, ///< The GV is hidden
48 ProtectedVisibility ///< The GV is protected
52 GlobalValue(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
53 LinkageTypes linkage, const std::string &name = "")
54 : Constant(Ty, vty, Ops, NumOps), Parent(0),
55 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
56 if (!name.empty()) setName(name);
60 // Note: VC++ treats enums as signed, so an extra bit is required to prevent
61 // Linkage and Visibility from turning into negative values.
62 LinkageTypes Linkage : 5; // The linkage of this global
63 unsigned Visibility : 2; // The visibility style of this global
64 unsigned Alignment : 16; // Alignment of this symbol, must be power of two
65 std::string Section; // Section to emit this into, empty mean default
67 static void destroyThis(GlobalValue*v) {
68 v->removeDeadConstantUsers(); // remove any dead constants using this.
69 Constant::destroyThis(v);
72 unsigned getAlignment() const { return Alignment; }
73 void setAlignment(unsigned Align) {
74 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
78 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
79 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
80 bool hasProtectedVisibility() const {
81 return Visibility == ProtectedVisibility;
83 void setVisibility(VisibilityTypes V) { Visibility = V; }
85 bool hasSection() const { return !Section.empty(); }
86 const std::string &getSection() const { return Section; }
87 void setSection(const std::string &S) { Section = S; }
89 /// If the usage is empty (except transitively dead constants), then this
90 /// global value can can be safely deleted since the destructor will
91 /// delete the dead constants as well.
92 /// @brief Determine if the usage of this global value is empty except
93 /// for transitively dead constants.
94 bool use_empty_except_constants();
96 /// getType - Global values are always pointers.
97 inline const PointerType *getType() const {
98 return reinterpret_cast<const PointerType*>(User::getType());
101 bool hasExternalLinkage() const { return Linkage == ExternalLinkage; }
102 bool hasLinkOnceLinkage() const { return Linkage == LinkOnceLinkage; }
103 bool hasWeakLinkage() const { return Linkage == WeakLinkage; }
104 bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
105 bool hasInternalLinkage() const { return Linkage == InternalLinkage; }
106 bool hasDLLImportLinkage() const { return Linkage == DLLImportLinkage; }
107 bool hasDLLExportLinkage() const { return Linkage == DLLExportLinkage; }
108 bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; }
109 void setLinkage(LinkageTypes LT) { Linkage = LT; }
110 LinkageTypes getLinkage() const { return Linkage; }
112 /// hasNotBeenReadFromBitcode - If a module provider is being used to lazily
113 /// stream in functions from disk, this method can be used to check to see if
114 /// the function has been read in yet or not. Unless you are working on the
115 /// JIT or something else that streams stuff in lazily, you don't need to
116 /// worry about this.
117 bool hasNotBeenReadFromBitcode() const { return Linkage == GhostLinkage; }
119 /// Override from Constant class. No GlobalValue's are null values so this
120 /// always returns false.
121 virtual bool isNullValue() const { return false; }
123 /// Override from Constant class.
124 virtual void destroyConstant();
126 /// isDeclaration - Return true if the primary definition of this global
127 /// value is outside of the current translation unit...
128 virtual bool isDeclaration() const = 0;
130 /// getParent - Get the module that this global value is contained inside
132 inline Module *getParent() { return Parent; }
133 inline const Module *getParent() const { return Parent; }
135 /// removeDeadConstantUsers - If there are any dead constant users dangling
136 /// off of this global value, remove them. This method is useful for clients
137 /// that want to check to see if a global is unused, but don't want to deal
138 /// with potentially dead constants hanging off of the globals.
139 void removeDeadConstantUsers();
141 // Methods for support type inquiry through isa, cast, and dyn_cast:
142 static inline bool classof(const GlobalValue *) { return true; }
143 static inline bool classof(const Value *V) {
144 return V->getValueID() == Value::FunctionVal ||
145 V->getValueID() == Value::GlobalVariableVal ||
146 V->getValueID() == Value::GlobalAliasVal;
150 } // End llvm namespace