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"
24 #include <system_error>
32 class GlobalValue : public Constant {
33 GlobalValue(const GlobalValue &) LLVM_DELETED_FUNCTION;
35 /// @brief An enumeration for the kinds of linkage for global values.
37 ExternalLinkage = 0,///< Externally visible function
38 AvailableExternallyLinkage, ///< Available for inspection, not emission.
39 LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
40 LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
41 WeakAnyLinkage, ///< Keep one copy of named function when linking (weak)
42 WeakODRLinkage, ///< Same, but only replaced by something equivalent.
43 AppendingLinkage, ///< Special purpose, only applies to global arrays
44 InternalLinkage, ///< Rename collisions when linking (static functions).
45 PrivateLinkage, ///< Like Internal, but omit from symbol table.
46 ExternalWeakLinkage,///< ExternalWeak linkage description.
47 CommonLinkage ///< Tentative definitions.
50 /// @brief An enumeration for the kinds of visibility of global values.
51 enum VisibilityTypes {
52 DefaultVisibility = 0, ///< The GV is visible
53 HiddenVisibility, ///< The GV is hidden
54 ProtectedVisibility ///< The GV is protected
57 /// @brief Storage classes of global values for PE targets.
58 enum DLLStorageClassTypes {
59 DefaultStorageClass = 0,
60 DLLImportStorageClass = 1, ///< Function to be imported from DLL
61 DLLExportStorageClass = 2 ///< Function to be accessible from DLL.
65 GlobalValue(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
66 LinkageTypes Linkage, const Twine &Name)
67 : Constant(Ty, VTy, Ops, NumOps), Linkage(Linkage),
68 Visibility(DefaultVisibility), UnnamedAddr(0),
69 DllStorageClass(DefaultStorageClass),
70 ThreadLocal(NotThreadLocal), Parent(nullptr) {
74 // Note: VC++ treats enums as signed, so an extra bit is required to prevent
75 // Linkage and Visibility from turning into negative values.
76 LinkageTypes Linkage : 5; // The linkage of this global
77 unsigned Visibility : 2; // The visibility style of this global
78 unsigned UnnamedAddr : 1; // This value's address is not significant
79 unsigned DllStorageClass : 2; // DLL storage class
81 unsigned ThreadLocal : 3; // Is this symbol "Thread Local", if so, what is
85 // Give subclasses access to what otherwise would be wasted padding.
86 // (19 + 3 + 2 + 1 + 2 + 5) == 32.
87 unsigned SubClassData : 19;
89 static const unsigned GlobalValueSubClassDataBits = 19;
90 unsigned getGlobalValueSubClassData() const {
93 void setGlobalValueSubClassData(unsigned V) {
94 assert(V < (1 << 19) && "It will not fit");
98 Module *Parent; // The containing module.
100 enum ThreadLocalMode {
102 GeneralDynamicTLSModel,
103 LocalDynamicTLSModel,
109 removeDeadConstantUsers(); // remove any dead constants using this.
112 unsigned getAlignment() const;
114 bool hasUnnamedAddr() const { return UnnamedAddr; }
115 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
117 bool hasComdat() const { return getComdat() != nullptr; }
119 const Comdat *getComdat() const {
120 return const_cast<GlobalValue *>(this)->getComdat();
123 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
124 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
125 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
126 bool hasProtectedVisibility() const {
127 return Visibility == ProtectedVisibility;
129 void setVisibility(VisibilityTypes V) {
130 assert((!hasLocalLinkage() || V == DefaultVisibility) &&
131 "local linkage requires default visibility");
135 /// If the value is "Thread Local", its value isn't shared by the threads.
136 bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal; }
137 void setThreadLocal(bool Val) {
138 setThreadLocalMode(Val ? GeneralDynamicTLSModel : NotThreadLocal);
140 void setThreadLocalMode(ThreadLocalMode Val) {
141 assert(Val == NotThreadLocal || getValueID() != Value::FunctionVal);
144 ThreadLocalMode getThreadLocalMode() const {
145 return static_cast<ThreadLocalMode>(ThreadLocal);
148 DLLStorageClassTypes getDLLStorageClass() const {
149 return DLLStorageClassTypes(DllStorageClass);
151 bool hasDLLImportStorageClass() const {
152 return DllStorageClass == DLLImportStorageClass;
154 bool hasDLLExportStorageClass() const {
155 return DllStorageClass == DLLExportStorageClass;
157 void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; }
159 bool hasSection() const { return !StringRef(getSection()).empty(); }
160 // It is unfortunate that we have to use "char *" in here since this is
161 // always non NULL, but:
162 // * The C API expects a null terminated string, so we cannot use StringRef.
163 // * The C API expects us to own it, so we cannot use a std:string.
164 // * For GlobalAliases we can fail to find the section and we have to
165 // return "", so we cannot use a "const std::string &".
166 const char *getSection() const;
168 /// Global values are always pointers.
169 inline PointerType *getType() const {
170 return cast<PointerType>(User::getType());
173 static LinkageTypes getLinkOnceLinkage(bool ODR) {
174 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
176 static LinkageTypes getWeakLinkage(bool ODR) {
177 return ODR ? WeakODRLinkage : WeakAnyLinkage;
180 static bool isExternalLinkage(LinkageTypes Linkage) {
181 return Linkage == ExternalLinkage;
183 static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
184 return Linkage == AvailableExternallyLinkage;
186 static bool isLinkOnceODRLinkage(LinkageTypes Linkage) {
187 return Linkage == LinkOnceODRLinkage;
189 static bool isLinkOnceLinkage(LinkageTypes Linkage) {
190 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
192 static bool isWeakAnyLinkage(LinkageTypes Linkage) {
193 return Linkage == WeakAnyLinkage;
195 static bool isWeakODRLinkage(LinkageTypes Linkage) {
196 return Linkage == WeakODRLinkage;
198 static bool isWeakLinkage(LinkageTypes Linkage) {
199 return isWeakAnyLinkage(Linkage) || isWeakODRLinkage(Linkage);
201 static bool isAppendingLinkage(LinkageTypes Linkage) {
202 return Linkage == AppendingLinkage;
204 static bool isInternalLinkage(LinkageTypes Linkage) {
205 return Linkage == InternalLinkage;
207 static bool isPrivateLinkage(LinkageTypes Linkage) {
208 return Linkage == PrivateLinkage;
210 static bool isLocalLinkage(LinkageTypes Linkage) {
211 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage);
213 static bool isExternalWeakLinkage(LinkageTypes Linkage) {
214 return Linkage == ExternalWeakLinkage;
216 static bool isCommonLinkage(LinkageTypes Linkage) {
217 return Linkage == CommonLinkage;
220 /// Whether the definition of this global may be discarded if it is not used
221 /// in its compilation unit.
222 static bool isDiscardableIfUnused(LinkageTypes Linkage) {
223 return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
226 /// Whether the definition of this global may be replaced by something
227 /// non-equivalent at link time. For example, if a function has weak linkage
228 /// then the code defining it may be replaced by different code.
229 static bool mayBeOverridden(LinkageTypes Linkage) {
230 return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage ||
231 Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
234 /// Whether the definition of this global may be replaced at link time. NB:
235 /// Using this method outside of the code generators is almost always a
236 /// mistake: when working at the IR level use mayBeOverridden instead as it
237 /// knows about ODR semantics.
238 static bool isWeakForLinker(LinkageTypes Linkage) {
239 return Linkage == AvailableExternallyLinkage || Linkage == WeakAnyLinkage ||
240 Linkage == WeakODRLinkage || Linkage == LinkOnceAnyLinkage ||
241 Linkage == LinkOnceODRLinkage || Linkage == CommonLinkage ||
242 Linkage == ExternalWeakLinkage;
245 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
246 bool hasAvailableExternallyLinkage() const {
247 return isAvailableExternallyLinkage(Linkage);
249 bool hasLinkOnceLinkage() const {
250 return isLinkOnceLinkage(Linkage);
252 bool hasLinkOnceODRLinkage() const { return isLinkOnceODRLinkage(Linkage); }
253 bool hasWeakLinkage() const {
254 return isWeakLinkage(Linkage);
256 bool hasWeakAnyLinkage() const {
257 return isWeakAnyLinkage(Linkage);
259 bool hasWeakODRLinkage() const {
260 return isWeakODRLinkage(Linkage);
262 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
263 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
264 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
265 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
266 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
267 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
269 void setLinkage(LinkageTypes LT) {
270 if (isLocalLinkage(LT))
271 Visibility = DefaultVisibility;
274 LinkageTypes getLinkage() const { return Linkage; }
276 bool isDiscardableIfUnused() const {
277 return isDiscardableIfUnused(Linkage);
280 bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
282 bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
284 /// Copy all additional attributes (those not needed to create a GlobalValue)
285 /// from the GlobalValue Src to this one.
286 virtual void copyAttributesFrom(const GlobalValue *Src);
288 /// If special LLVM prefix that is used to inform the asm printer to not emit
289 /// usual symbol prefix before the symbol name is used then return linkage
290 /// name after skipping this special LLVM prefix.
291 static StringRef getRealLinkageName(StringRef Name) {
292 if (!Name.empty() && Name[0] == '\1')
293 return Name.substr(1);
297 /// @name Materialization
298 /// Materialization is used to construct functions only as they're needed. This
299 /// is useful to reduce memory usage in LLVM or parsing work done by the
300 /// BitcodeReader to load the Module.
303 /// If this function's Module is being lazily streamed in functions from disk
304 /// or some other source, this method can be used to check to see if the
305 /// function has been read in yet or not.
306 bool isMaterializable() const;
308 /// Returns true if this function was loaded from a GVMaterializer that's
309 /// still attached to its Module and that knows how to dematerialize the
311 bool isDematerializable() const;
313 /// Make sure this GlobalValue is fully read. If the module is corrupt, this
314 /// returns true and fills in the optional string with information about the
315 /// problem. If successful, this returns false.
316 std::error_code materialize();
318 /// If this GlobalValue is read in, and if the GVMaterializer supports it,
319 /// release the memory for the function, and set it up to be materialized
320 /// lazily. If !isDematerializable(), this method is a noop.
321 void Dematerialize();
325 /// Override from Constant class.
326 void destroyConstant() override;
328 /// Return true if the primary definition of this global value is outside of
329 /// the current translation unit.
330 bool isDeclaration() const;
332 bool isDeclarationForLinker() const {
333 if (hasAvailableExternallyLinkage())
336 return isDeclaration();
339 /// This method unlinks 'this' from the containing module, but does not delete
341 virtual void removeFromParent() = 0;
343 /// This method unlinks 'this' from the containing module and deletes it.
344 virtual void eraseFromParent() = 0;
346 /// Get the module that this global value is contained inside of...
347 inline Module *getParent() { return Parent; }
348 inline const Module *getParent() const { return Parent; }
350 const DataLayout *getDataLayout() const;
352 // Methods for support type inquiry through isa, cast, and dyn_cast:
353 static inline bool classof(const Value *V) {
354 return V->getValueID() == Value::FunctionVal ||
355 V->getValueID() == Value::GlobalVariableVal ||
356 V->getValueID() == Value::GlobalAliasVal;
360 } // End llvm namespace