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"
30 class GlobalValue : public Constant {
31 GlobalValue(const GlobalValue &) LLVM_DELETED_FUNCTION;
33 /// @brief An enumeration for the kinds of linkage for global values.
35 ExternalLinkage = 0,///< Externally visible function
36 AvailableExternallyLinkage, ///< Available for inspection, not emission.
37 LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
38 LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
39 WeakAnyLinkage, ///< Keep one copy of named function when linking (weak)
40 WeakODRLinkage, ///< Same, but only replaced by something equivalent.
41 AppendingLinkage, ///< Special purpose, only applies to global arrays
42 InternalLinkage, ///< Rename collisions when linking (static functions).
43 PrivateLinkage, ///< Like Internal, but omit from symbol table.
44 ExternalWeakLinkage,///< ExternalWeak linkage description.
45 CommonLinkage ///< Tentative definitions.
48 /// @brief An enumeration for the kinds of visibility of global values.
49 enum VisibilityTypes {
50 DefaultVisibility = 0, ///< The GV is visible
51 HiddenVisibility, ///< The GV is hidden
52 ProtectedVisibility ///< The GV is protected
55 /// @brief Storage classes of global values for PE targets.
56 enum DLLStorageClassTypes {
57 DefaultStorageClass = 0,
58 DLLImportStorageClass = 1, ///< Function to be imported from DLL
59 DLLExportStorageClass = 2 ///< Function to be accessible from DLL.
63 GlobalValue(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
64 LinkageTypes Linkage, const Twine &Name)
65 : Constant(Ty, VTy, Ops, NumOps), Linkage(Linkage),
66 Visibility(DefaultVisibility), UnnamedAddr(0),
67 DllStorageClass(DefaultStorageClass),
68 ThreadLocal(NotThreadLocal), Parent(nullptr) {
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 UnnamedAddr : 1; // This value's address is not significant
77 unsigned DllStorageClass : 2; // DLL storage class
79 unsigned ThreadLocal : 3; // Is this symbol "Thread Local", if so, what is
83 // Give subclasses access to what otherwise would be wasted padding.
84 // (19 + 3 + 2 + 1 + 2 + 5) == 32.
85 unsigned SubClassData : 19;
87 static const unsigned GlobalValueSubClassDataBits = 19;
88 unsigned getGlobalValueSubClassData() const {
91 void setGlobalValueSubClassData(unsigned V) {
92 assert(V < (1 << 19) && "It will not fit");
96 Module *Parent; // The containing module.
98 enum ThreadLocalMode {
100 GeneralDynamicTLSModel,
101 LocalDynamicTLSModel,
107 removeDeadConstantUsers(); // remove any dead constants using this.
110 unsigned getAlignment() const;
112 bool hasUnnamedAddr() const { return UnnamedAddr; }
113 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
115 bool hasComdat() const { return getComdat() != nullptr; }
117 const Comdat *getComdat() const {
118 return const_cast<GlobalValue *>(this)->getComdat();
121 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
122 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
123 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
124 bool hasProtectedVisibility() const {
125 return Visibility == ProtectedVisibility;
127 void setVisibility(VisibilityTypes V) {
128 assert((!hasLocalLinkage() || V == DefaultVisibility) &&
129 "local linkage requires default visibility");
133 /// If the value is "Thread Local", its value isn't shared by the threads.
134 bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal; }
135 void setThreadLocal(bool Val) {
136 setThreadLocalMode(Val ? GeneralDynamicTLSModel : NotThreadLocal);
138 void setThreadLocalMode(ThreadLocalMode Val) {
139 assert(Val == NotThreadLocal || getValueID() != Value::FunctionVal);
142 ThreadLocalMode getThreadLocalMode() const {
143 return static_cast<ThreadLocalMode>(ThreadLocal);
146 DLLStorageClassTypes getDLLStorageClass() const {
147 return DLLStorageClassTypes(DllStorageClass);
149 bool hasDLLImportStorageClass() const {
150 return DllStorageClass == DLLImportStorageClass;
152 bool hasDLLExportStorageClass() const {
153 return DllStorageClass == DLLExportStorageClass;
155 void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; }
157 bool hasSection() const { return !StringRef(getSection()).empty(); }
158 // It is unfortunate that we have to use "char *" in here since this is
159 // always non NULL, but:
160 // * The C API expects a null terminated string, so we cannot use StringRef.
161 // * The C API expects us to own it, so we cannot use a std:string.
162 // * For GlobalAliases we can fail to find the section and we have to
163 // return "", so we cannot use a "const std::string &".
164 const char *getSection() const;
166 /// Global values are always pointers.
167 inline PointerType *getType() const {
168 return cast<PointerType>(User::getType());
171 static LinkageTypes getLinkOnceLinkage(bool ODR) {
172 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
174 static LinkageTypes getWeakLinkage(bool ODR) {
175 return ODR ? WeakODRLinkage : WeakAnyLinkage;
178 static bool isExternalLinkage(LinkageTypes Linkage) {
179 return Linkage == ExternalLinkage;
181 static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
182 return Linkage == AvailableExternallyLinkage;
184 static bool isLinkOnceODRLinkage(LinkageTypes Linkage) {
185 return Linkage == LinkOnceODRLinkage;
187 static bool isLinkOnceLinkage(LinkageTypes Linkage) {
188 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
190 static bool isWeakAnyLinkage(LinkageTypes Linkage) {
191 return Linkage == WeakAnyLinkage;
193 static bool isWeakODRLinkage(LinkageTypes Linkage) {
194 return Linkage == WeakODRLinkage;
196 static bool isWeakLinkage(LinkageTypes Linkage) {
197 return isWeakAnyLinkage(Linkage) || isWeakODRLinkage(Linkage);
199 static bool isAppendingLinkage(LinkageTypes Linkage) {
200 return Linkage == AppendingLinkage;
202 static bool isInternalLinkage(LinkageTypes Linkage) {
203 return Linkage == InternalLinkage;
205 static bool isPrivateLinkage(LinkageTypes Linkage) {
206 return Linkage == PrivateLinkage;
208 static bool isLocalLinkage(LinkageTypes Linkage) {
209 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage);
211 static bool isExternalWeakLinkage(LinkageTypes Linkage) {
212 return Linkage == ExternalWeakLinkage;
214 static bool isCommonLinkage(LinkageTypes Linkage) {
215 return Linkage == CommonLinkage;
218 /// Whether the definition of this global may be discarded if it is not used
219 /// in its compilation unit.
220 static bool isDiscardableIfUnused(LinkageTypes Linkage) {
221 return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
224 /// Whether the definition of this global may be replaced by something
225 /// non-equivalent at link time. For example, if a function has weak linkage
226 /// then the code defining it may be replaced by different code.
227 static bool mayBeOverridden(LinkageTypes Linkage) {
228 return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage ||
229 Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
232 /// Whether the definition of this global may be replaced at link time. NB:
233 /// Using this method outside of the code generators is almost always a
234 /// mistake: when working at the IR level use mayBeOverridden instead as it
235 /// knows about ODR semantics.
236 static bool isWeakForLinker(LinkageTypes Linkage) {
237 return Linkage == AvailableExternallyLinkage || Linkage == WeakAnyLinkage ||
238 Linkage == WeakODRLinkage || Linkage == LinkOnceAnyLinkage ||
239 Linkage == LinkOnceODRLinkage || Linkage == CommonLinkage ||
240 Linkage == ExternalWeakLinkage;
243 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
244 bool hasAvailableExternallyLinkage() const {
245 return isAvailableExternallyLinkage(Linkage);
247 bool hasLinkOnceLinkage() const {
248 return isLinkOnceLinkage(Linkage);
250 bool hasLinkOnceODRLinkage() const { return isLinkOnceODRLinkage(Linkage); }
251 bool hasWeakLinkage() const {
252 return isWeakLinkage(Linkage);
254 bool hasWeakAnyLinkage() const {
255 return isWeakAnyLinkage(Linkage);
257 bool hasWeakODRLinkage() const {
258 return isWeakODRLinkage(Linkage);
260 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
261 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
262 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
263 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
264 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
265 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
267 void setLinkage(LinkageTypes LT) {
268 if (isLocalLinkage(LT))
269 Visibility = DefaultVisibility;
272 LinkageTypes getLinkage() const { return Linkage; }
274 bool isDiscardableIfUnused() const {
275 return isDiscardableIfUnused(Linkage);
278 bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
280 bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
282 /// Copy all additional attributes (those not needed to create a GlobalValue)
283 /// from the GlobalValue Src to this one.
284 virtual void copyAttributesFrom(const GlobalValue *Src);
286 /// If special LLVM prefix that is used to inform the asm printer to not emit
287 /// usual symbol prefix before the symbol name is used then return linkage
288 /// name after skipping this special LLVM prefix.
289 static StringRef getRealLinkageName(StringRef Name) {
290 if (!Name.empty() && Name[0] == '\1')
291 return Name.substr(1);
295 /// @name Materialization
296 /// Materialization is used to construct functions only as they're needed. This
297 /// is useful to reduce memory usage in LLVM or parsing work done by the
298 /// BitcodeReader to load the Module.
301 /// If this function's Module is being lazily streamed in functions from disk
302 /// or some other source, this method can be used to check to see if the
303 /// function has been read in yet or not.
304 bool isMaterializable() const;
306 /// Returns true if this function was loaded from a GVMaterializer that's
307 /// still attached to its Module and that knows how to dematerialize the
309 bool isDematerializable() const;
311 /// Make sure this GlobalValue is fully read. If the module is corrupt, this
312 /// returns true and fills in the optional string with information about the
313 /// problem. If successful, this returns false.
314 bool Materialize(std::string *ErrInfo = nullptr);
316 /// If this GlobalValue is read in, and if the GVMaterializer supports it,
317 /// release the memory for the function, and set it up to be materialized
318 /// lazily. If !isDematerializable(), this method is a noop.
319 void Dematerialize();
323 /// Override from Constant class.
324 void destroyConstant() override;
326 /// Return true if the primary definition of this global value is outside of
327 /// the current translation unit.
328 bool isDeclaration() const;
330 bool isDeclarationForLinker() const {
331 if (hasAvailableExternallyLinkage())
334 return isDeclaration();
337 /// This method unlinks 'this' from the containing module, but does not delete
339 virtual void removeFromParent() = 0;
341 /// This method unlinks 'this' from the containing module and deletes it.
342 virtual void eraseFromParent() = 0;
344 /// Get the module that this global value is contained inside of...
345 inline Module *getParent() { return Parent; }
346 inline const Module *getParent() const { return Parent; }
348 const DataLayout *getDataLayout() const;
350 // Methods for support type inquiry through isa, cast, and dyn_cast:
351 static inline bool classof(const Value *V) {
352 return V->getValueID() == Value::FunctionVal ||
353 V->getValueID() == Value::GlobalVariableVal ||
354 V->getValueID() == Value::GlobalAliasVal;
358 } // End llvm namespace