1 //===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // 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_IR_GLOBALVALUE_H
19 #define LLVM_IR_GLOBALVALUE_H
21 #include "llvm/IR/Constant.h"
22 #include "llvm/IR/DerivedTypes.h"
29 class GlobalValue : public Constant {
30 GlobalValue(const GlobalValue &) LLVM_DELETED_FUNCTION;
32 /// @brief An enumeration for the kinds of linkage for global values.
34 ExternalLinkage = 0,///< Externally visible function
35 AvailableExternallyLinkage, ///< Available for inspection, not emission.
36 LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
37 LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
38 WeakAnyLinkage, ///< Keep one copy of named function when linking (weak)
39 WeakODRLinkage, ///< Same, but only replaced by something equivalent.
40 AppendingLinkage, ///< Special purpose, only applies to global arrays
41 InternalLinkage, ///< Rename collisions when linking (static functions).
42 PrivateLinkage, ///< Like Internal, but omit from symbol table.
43 LinkerPrivateLinkage, ///< Like Private, but linker removes.
44 LinkerPrivateWeakLinkage, ///< Like LinkerPrivate, but weak.
45 ExternalWeakLinkage,///< ExternalWeak linkage description.
46 CommonLinkage ///< Tentative definitions.
49 /// @brief An enumeration for the kinds of visibility of global values.
50 enum VisibilityTypes {
51 DefaultVisibility = 0, ///< The GV is visible
52 HiddenVisibility, ///< The GV is hidden
53 ProtectedVisibility ///< The GV is protected
56 /// @brief Storage classes of global values for PE targets.
57 enum DLLStorageClassTypes {
58 DefaultStorageClass = 0,
59 DLLImportStorageClass = 1, ///< Function to be imported from DLL
60 DLLExportStorageClass = 2 ///< Function to be accessible from DLL.
64 GlobalValue(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
65 LinkageTypes linkage, const Twine &Name)
66 : Constant(ty, vty, Ops, NumOps), Linkage(linkage),
67 Visibility(DefaultVisibility), Alignment(0), UnnamedAddr(0),
68 DllStorageClass(DefaultStorageClass), Parent(0) {
72 // Note: VC++ treats enums as signed, so an extra bit is required to prevent
73 // Linkage and Visibility from turning into negative values.
74 LinkageTypes Linkage : 5; // The linkage of this global
75 unsigned Visibility : 2; // The visibility style of this global
76 unsigned Alignment : 16; // Alignment of this symbol, must be power of two
77 unsigned UnnamedAddr : 1; // This value's address is not significant
78 unsigned DllStorageClass : 2; // DLL storage class
79 Module *Parent; // The containing module.
80 std::string Section; // Section to emit this into, empty mean default
83 removeDeadConstantUsers(); // remove any dead constants using this.
86 unsigned getAlignment() const {
87 return (1u << Alignment) >> 1;
89 void setAlignment(unsigned Align);
91 bool hasUnnamedAddr() const { return UnnamedAddr; }
92 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
94 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
95 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
96 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
97 bool hasProtectedVisibility() const {
98 return Visibility == ProtectedVisibility;
100 void setVisibility(VisibilityTypes V) { Visibility = V; }
102 DLLStorageClassTypes getDLLStorageClass() const {
103 return DLLStorageClassTypes(DllStorageClass);
105 bool hasDLLImportStorageClass() const {
106 return DllStorageClass == DLLImportStorageClass;
108 bool hasDLLExportStorageClass() const {
109 return DllStorageClass == DLLExportStorageClass;
111 void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; }
113 bool hasSection() const { return !Section.empty(); }
114 const std::string &getSection() const { return Section; }
115 void setSection(StringRef S) { Section = S; }
117 /// If the usage is empty (except transitively dead constants), then this
118 /// global value can be safely deleted since the destructor will
119 /// delete the dead constants as well.
120 /// @brief Determine if the usage of this global value is empty except
121 /// for transitively dead constants.
122 bool use_empty_except_constants();
124 /// getType - Global values are always pointers.
125 inline PointerType *getType() const {
126 return cast<PointerType>(User::getType());
129 static LinkageTypes getLinkOnceLinkage(bool ODR) {
130 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
132 static LinkageTypes getWeakLinkage(bool ODR) {
133 return ODR ? WeakODRLinkage : WeakAnyLinkage;
136 static bool isExternalLinkage(LinkageTypes Linkage) {
137 return Linkage == ExternalLinkage;
139 static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
140 return Linkage == AvailableExternallyLinkage;
142 static bool isLinkOnceLinkage(LinkageTypes Linkage) {
143 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
145 static bool isWeakLinkage(LinkageTypes Linkage) {
146 return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
148 static bool isAppendingLinkage(LinkageTypes Linkage) {
149 return Linkage == AppendingLinkage;
151 static bool isInternalLinkage(LinkageTypes Linkage) {
152 return Linkage == InternalLinkage;
154 static bool isPrivateLinkage(LinkageTypes Linkage) {
155 return Linkage == PrivateLinkage;
157 static bool isLinkerPrivateLinkage(LinkageTypes Linkage) {
158 return Linkage == LinkerPrivateLinkage;
160 static bool isLinkerPrivateWeakLinkage(LinkageTypes Linkage) {
161 return Linkage == LinkerPrivateWeakLinkage;
163 static bool isLocalLinkage(LinkageTypes Linkage) {
164 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage) ||
165 isLinkerPrivateLinkage(Linkage) || isLinkerPrivateWeakLinkage(Linkage);
167 static bool isExternalWeakLinkage(LinkageTypes Linkage) {
168 return Linkage == ExternalWeakLinkage;
170 static bool isCommonLinkage(LinkageTypes Linkage) {
171 return Linkage == CommonLinkage;
174 /// isDiscardableIfUnused - Whether the definition of this global may be
175 /// discarded if it is not used in its compilation unit.
176 static bool isDiscardableIfUnused(LinkageTypes Linkage) {
177 return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
180 /// mayBeOverridden - Whether the definition of this global may be replaced
181 /// by something non-equivalent at link time. For example, if a function has
182 /// weak linkage then the code defining it may be replaced by different code.
183 static bool mayBeOverridden(LinkageTypes Linkage) {
184 return Linkage == WeakAnyLinkage ||
185 Linkage == LinkOnceAnyLinkage ||
186 Linkage == CommonLinkage ||
187 Linkage == ExternalWeakLinkage ||
188 Linkage == LinkerPrivateWeakLinkage;
191 /// isWeakForLinker - Whether the definition of this global may be replaced at
192 /// link time. NB: Using this method outside of the code generators is almost
193 /// always a mistake: when working at the IR level use mayBeOverridden instead
194 /// as it knows about ODR semantics.
195 static bool isWeakForLinker(LinkageTypes Linkage) {
196 return Linkage == AvailableExternallyLinkage ||
197 Linkage == WeakAnyLinkage ||
198 Linkage == WeakODRLinkage ||
199 Linkage == LinkOnceAnyLinkage ||
200 Linkage == LinkOnceODRLinkage ||
201 Linkage == CommonLinkage ||
202 Linkage == ExternalWeakLinkage ||
203 Linkage == LinkerPrivateWeakLinkage;
206 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
207 bool hasAvailableExternallyLinkage() const {
208 return isAvailableExternallyLinkage(Linkage);
210 bool hasLinkOnceLinkage() const {
211 return isLinkOnceLinkage(Linkage);
213 bool hasWeakLinkage() const {
214 return isWeakLinkage(Linkage);
216 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
217 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
218 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
219 bool hasLinkerPrivateLinkage() const { return isLinkerPrivateLinkage(Linkage); }
220 bool hasLinkerPrivateWeakLinkage() const {
221 return isLinkerPrivateWeakLinkage(Linkage);
223 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
224 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
225 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
227 void setLinkage(LinkageTypes LT) { Linkage = LT; }
228 LinkageTypes getLinkage() const { return Linkage; }
230 bool isDiscardableIfUnused() const {
231 return isDiscardableIfUnused(Linkage);
234 bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
236 bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
238 /// copyAttributesFrom - copy all additional attributes (those not needed to
239 /// create a GlobalValue) from the GlobalValue Src to this one.
240 virtual void copyAttributesFrom(const GlobalValue *Src);
242 /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
243 /// printer to not emit usual symbol prefix before the symbol name is used
244 /// then return linkage name after skipping this special LLVM prefix.
245 static StringRef getRealLinkageName(StringRef Name) {
246 if (!Name.empty() && Name[0] == '\1')
247 return Name.substr(1);
251 /// @name Materialization
252 /// Materialization is used to construct functions only as they're needed. This
253 /// is useful to reduce memory usage in LLVM or parsing work done by the
254 /// BitcodeReader to load the Module.
257 /// isMaterializable - If this function's Module is being lazily streamed in
258 /// functions from disk or some other source, this method can be used to check
259 /// to see if the function has been read in yet or not.
260 bool isMaterializable() const;
262 /// isDematerializable - Returns true if this function was loaded from a
263 /// GVMaterializer that's still attached to its Module and that knows how to
264 /// dematerialize the function.
265 bool isDematerializable() const;
267 /// Materialize - make sure this GlobalValue is fully read. If the module is
268 /// corrupt, this returns true and fills in the optional string with
269 /// information about the problem. If successful, this returns false.
270 bool Materialize(std::string *ErrInfo = 0);
272 /// Dematerialize - If this GlobalValue is read in, and if the GVMaterializer
273 /// supports it, release the memory for the function, and set it up to be
274 /// materialized lazily. If !isDematerializable(), this method is a noop.
275 void Dematerialize();
279 /// Override from Constant class.
280 virtual void destroyConstant();
282 /// isDeclaration - Return true if the primary definition of this global
283 /// value is outside of the current translation unit.
284 bool isDeclaration() const;
286 /// removeFromParent - This method unlinks 'this' from the containing module,
287 /// but does not delete it.
288 virtual void removeFromParent() = 0;
290 /// eraseFromParent - This method unlinks 'this' from the containing module
292 virtual void eraseFromParent() = 0;
294 /// getParent - Get the module that this global value is contained inside
296 inline Module *getParent() { return Parent; }
297 inline const Module *getParent() const { return Parent; }
299 // Methods for support type inquiry through isa, cast, and dyn_cast:
300 static inline bool classof(const Value *V) {
301 return V->getValueID() == Value::FunctionVal ||
302 V->getValueID() == Value::GlobalVariableVal ||
303 V->getValueID() == Value::GlobalAliasVal;
307 } // End llvm namespace