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"
23 #include <system_error>
31 class GlobalValue
: public Constant
{
32 GlobalValue(const GlobalValue
&) LLVM_DELETED_FUNCTION
;
34 /// @brief An enumeration for the kinds of linkage for global values.
36 ExternalLinkage
= 0,///< Externally visible function
37 AvailableExternallyLinkage
, ///< Available for inspection, not emission.
38 LinkOnceAnyLinkage
, ///< Keep one copy of function when linking (inline)
39 LinkOnceODRLinkage
, ///< Same, but only replaced by something equivalent.
40 WeakAnyLinkage
, ///< Keep one copy of named function when linking (weak)
41 WeakODRLinkage
, ///< Same, but only replaced by something equivalent.
42 AppendingLinkage
, ///< Special purpose, only applies to global arrays
43 InternalLinkage
, ///< Rename collisions when linking (static functions).
44 PrivateLinkage
, ///< Like Internal, but omit from symbol table.
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
), UnnamedAddr(0),
68 DllStorageClass(DefaultStorageClass
),
69 ThreadLocal(NotThreadLocal
), Parent(nullptr) {
73 // Note: VC++ treats enums as signed, so an extra bit is required to prevent
74 // Linkage and Visibility from turning into negative values.
75 LinkageTypes Linkage
: 5; // The linkage of this global
76 unsigned Visibility
: 2; // The visibility style of this global
77 unsigned UnnamedAddr
: 1; // This value's address is not significant
78 unsigned DllStorageClass
: 2; // DLL storage class
80 unsigned ThreadLocal
: 3; // Is this symbol "Thread Local", if so, what is
84 // Give subclasses access to what otherwise would be wasted padding.
85 // (19 + 3 + 2 + 1 + 2 + 5) == 32.
86 unsigned SubClassData
: 19;
88 static const unsigned GlobalValueSubClassDataBits
= 19;
89 unsigned getGlobalValueSubClassData() const {
92 void setGlobalValueSubClassData(unsigned V
) {
93 assert(V
< (1 << 19) && "It will not fit");
97 Module
*Parent
; // The containing module.
99 enum ThreadLocalMode
{
101 GeneralDynamicTLSModel
,
102 LocalDynamicTLSModel
,
108 removeDeadConstantUsers(); // remove any dead constants using this.
111 unsigned getAlignment() const;
113 bool hasUnnamedAddr() const { return UnnamedAddr
; }
114 void setUnnamedAddr(bool Val
) { UnnamedAddr
= Val
; }
116 bool hasComdat() const { return getComdat() != nullptr; }
118 const Comdat
*getComdat() const {
119 return const_cast<GlobalValue
*>(this)->getComdat();
122 VisibilityTypes
getVisibility() const { return VisibilityTypes(Visibility
); }
123 bool hasDefaultVisibility() const { return Visibility
== DefaultVisibility
; }
124 bool hasHiddenVisibility() const { return Visibility
== HiddenVisibility
; }
125 bool hasProtectedVisibility() const {
126 return Visibility
== ProtectedVisibility
;
128 void setVisibility(VisibilityTypes V
) {
129 assert((!hasLocalLinkage() || V
== DefaultVisibility
) &&
130 "local linkage requires default visibility");
134 /// If the value is "Thread Local", its value isn't shared by the threads.
135 bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal
; }
136 void setThreadLocal(bool Val
) {
137 setThreadLocalMode(Val
? GeneralDynamicTLSModel
: NotThreadLocal
);
139 void setThreadLocalMode(ThreadLocalMode Val
) {
140 assert(Val
== NotThreadLocal
|| getValueID() != Value::FunctionVal
);
143 ThreadLocalMode
getThreadLocalMode() const {
144 return static_cast<ThreadLocalMode
>(ThreadLocal
);
147 DLLStorageClassTypes
getDLLStorageClass() const {
148 return DLLStorageClassTypes(DllStorageClass
);
150 bool hasDLLImportStorageClass() const {
151 return DllStorageClass
== DLLImportStorageClass
;
153 bool hasDLLExportStorageClass() const {
154 return DllStorageClass
== DLLExportStorageClass
;
156 void setDLLStorageClass(DLLStorageClassTypes C
) { DllStorageClass
= C
; }
158 bool hasSection() const { return !StringRef(getSection()).empty(); }
159 // It is unfortunate that we have to use "char *" in here since this is
160 // always non NULL, but:
161 // * The C API expects a null terminated string, so we cannot use StringRef.
162 // * The C API expects us to own it, so we cannot use a std:string.
163 // * For GlobalAliases we can fail to find the section and we have to
164 // return "", so we cannot use a "const std::string &".
165 const char *getSection() const;
167 /// Global values are always pointers.
168 inline PointerType
*getType() const {
169 return cast
<PointerType
>(User::getType());
172 static LinkageTypes
getLinkOnceLinkage(bool ODR
) {
173 return ODR
? LinkOnceODRLinkage
: LinkOnceAnyLinkage
;
175 static LinkageTypes
getWeakLinkage(bool ODR
) {
176 return ODR
? WeakODRLinkage
: WeakAnyLinkage
;
179 static bool isExternalLinkage(LinkageTypes Linkage
) {
180 return Linkage
== ExternalLinkage
;
182 static bool isAvailableExternallyLinkage(LinkageTypes Linkage
) {
183 return Linkage
== AvailableExternallyLinkage
;
185 static bool isLinkOnceODRLinkage(LinkageTypes Linkage
) {
186 return Linkage
== LinkOnceODRLinkage
;
188 static bool isLinkOnceLinkage(LinkageTypes Linkage
) {
189 return Linkage
== LinkOnceAnyLinkage
|| Linkage
== LinkOnceODRLinkage
;
191 static bool isWeakAnyLinkage(LinkageTypes Linkage
) {
192 return Linkage
== WeakAnyLinkage
;
194 static bool isWeakODRLinkage(LinkageTypes Linkage
) {
195 return Linkage
== WeakODRLinkage
;
197 static bool isWeakLinkage(LinkageTypes Linkage
) {
198 return isWeakAnyLinkage(Linkage
) || isWeakODRLinkage(Linkage
);
200 static bool isAppendingLinkage(LinkageTypes Linkage
) {
201 return Linkage
== AppendingLinkage
;
203 static bool isInternalLinkage(LinkageTypes Linkage
) {
204 return Linkage
== InternalLinkage
;
206 static bool isPrivateLinkage(LinkageTypes Linkage
) {
207 return Linkage
== PrivateLinkage
;
209 static bool isLocalLinkage(LinkageTypes Linkage
) {
210 return isInternalLinkage(Linkage
) || isPrivateLinkage(Linkage
);
212 static bool isExternalWeakLinkage(LinkageTypes Linkage
) {
213 return Linkage
== ExternalWeakLinkage
;
215 static bool isCommonLinkage(LinkageTypes Linkage
) {
216 return Linkage
== CommonLinkage
;
219 /// Whether the definition of this global may be discarded if it is not used
220 /// in its compilation unit.
221 static bool isDiscardableIfUnused(LinkageTypes Linkage
) {
222 return isLinkOnceLinkage(Linkage
) || isLocalLinkage(Linkage
);
225 /// Whether the definition of this global may be replaced by something
226 /// non-equivalent at link time. For example, if a function has weak linkage
227 /// then the code defining it may be replaced by different code.
228 static bool mayBeOverridden(LinkageTypes Linkage
) {
229 return Linkage
== WeakAnyLinkage
|| Linkage
== LinkOnceAnyLinkage
||
230 Linkage
== CommonLinkage
|| Linkage
== ExternalWeakLinkage
;
233 /// Whether the definition of this global may be replaced at link time. NB:
234 /// Using this method outside of the code generators is almost always a
235 /// mistake: when working at the IR level use mayBeOverridden instead as it
236 /// knows about ODR semantics.
237 static bool isWeakForLinker(LinkageTypes Linkage
) {
238 return Linkage
== AvailableExternallyLinkage
|| Linkage
== WeakAnyLinkage
||
239 Linkage
== WeakODRLinkage
|| Linkage
== LinkOnceAnyLinkage
||
240 Linkage
== LinkOnceODRLinkage
|| Linkage
== CommonLinkage
||
241 Linkage
== ExternalWeakLinkage
;
244 bool hasExternalLinkage() const { return isExternalLinkage(Linkage
); }
245 bool hasAvailableExternallyLinkage() const {
246 return isAvailableExternallyLinkage(Linkage
);
248 bool hasLinkOnceLinkage() const {
249 return isLinkOnceLinkage(Linkage
);
251 bool hasLinkOnceODRLinkage() const { return isLinkOnceODRLinkage(Linkage
); }
252 bool hasWeakLinkage() const {
253 return isWeakLinkage(Linkage
);
255 bool hasWeakAnyLinkage() const {
256 return isWeakAnyLinkage(Linkage
);
258 bool hasWeakODRLinkage() const {
259 return isWeakODRLinkage(Linkage
);
261 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage
); }
262 bool hasInternalLinkage() const { return isInternalLinkage(Linkage
); }
263 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage
); }
264 bool hasLocalLinkage() const { return isLocalLinkage(Linkage
); }
265 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage
); }
266 bool hasCommonLinkage() const { return isCommonLinkage(Linkage
); }
268 void setLinkage(LinkageTypes LT
) {
269 if (isLocalLinkage(LT
))
270 Visibility
= DefaultVisibility
;
273 LinkageTypes
getLinkage() const { return Linkage
; }
275 bool isDiscardableIfUnused() const {
276 return isDiscardableIfUnused(Linkage
);
279 bool mayBeOverridden() const { return mayBeOverridden(Linkage
); }
281 bool isWeakForLinker() const { return isWeakForLinker(Linkage
); }
283 /// Copy all additional attributes (those not needed to create a GlobalValue)
284 /// from the GlobalValue Src to this one.
285 virtual void copyAttributesFrom(const GlobalValue
*Src
);
287 /// If special LLVM prefix that is used to inform the asm printer to not emit
288 /// usual symbol prefix before the symbol name is used then return linkage
289 /// name after skipping this special LLVM prefix.
290 static StringRef
getRealLinkageName(StringRef Name
) {
291 if (!Name
.empty() && Name
[0] == '\1')
292 return Name
.substr(1);
296 /// @name Materialization
297 /// Materialization is used to construct functions only as they're needed. This
298 /// is useful to reduce memory usage in LLVM or parsing work done by the
299 /// BitcodeReader to load the Module.
302 /// If this function's Module is being lazily streamed in functions from disk
303 /// or some other source, this method can be used to check to see if the
304 /// function has been read in yet or not.
305 bool isMaterializable() const;
307 /// Returns true if this function was loaded from a GVMaterializer that's
308 /// still attached to its Module and that knows how to dematerialize the
310 bool isDematerializable() const;
312 /// Make sure this GlobalValue is fully read. If the module is corrupt, this
313 /// returns true and fills in the optional string with information about the
314 /// problem. If successful, this returns false.
315 std::error_code
materialize();
317 /// If this GlobalValue is read in, and if the GVMaterializer supports it,
318 /// release the memory for the function, and set it up to be materialized
319 /// lazily. If !isDematerializable(), this method is a noop.
320 void Dematerialize();
324 /// Override from Constant class.
325 void destroyConstant() override
;
327 /// Return true if the primary definition of this global value is outside of
328 /// the current translation unit.
329 bool isDeclaration() const;
331 bool isDeclarationForLinker() const {
332 if (hasAvailableExternallyLinkage())
335 return isDeclaration();
338 /// This method unlinks 'this' from the containing module, but does not delete
340 virtual void removeFromParent() = 0;
342 /// This method unlinks 'this' from the containing module and deletes it.
343 virtual void eraseFromParent() = 0;
345 /// Get the module that this global value is contained inside of...
346 inline Module
*getParent() { return Parent
; }
347 inline const Module
*getParent() const { return Parent
; }
349 const DataLayout
*getDataLayout() const;
351 // Methods for support type inquiry through isa, cast, and dyn_cast:
352 static inline bool classof(const Value
*V
) {
353 return V
->getValueID() == Value::FunctionVal
||
354 V
->getValueID() == Value::GlobalVariableVal
||
355 V
->getValueID() == Value::GlobalAliasVal
;
359 } // End llvm namespace