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 ExternalWeakLinkage,///< ExternalWeak linkage description.
44 CommonLinkage ///< Tentative definitions.
47 /// @brief An enumeration for the kinds of visibility of global values.
48 enum VisibilityTypes {
49 DefaultVisibility = 0, ///< The GV is visible
50 HiddenVisibility, ///< The GV is hidden
51 ProtectedVisibility ///< The GV is protected
54 /// @brief Storage classes of global values for PE targets.
55 enum DLLStorageClassTypes {
56 DefaultStorageClass = 0,
57 DLLImportStorageClass = 1, ///< Function to be imported from DLL
58 DLLExportStorageClass = 2 ///< Function to be accessible from DLL.
62 GlobalValue(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
63 LinkageTypes linkage, const Twine &Name)
64 : Constant(ty, vty, Ops, NumOps), Linkage(linkage),
65 Visibility(DefaultVisibility), Alignment(0), UnnamedAddr(0),
66 DllStorageClass(DefaultStorageClass), Parent(nullptr) {
70 // Note: VC++ treats enums as signed, so an extra bit is required to prevent
71 // Linkage and Visibility from turning into negative values.
72 LinkageTypes Linkage : 5; // The linkage of this global
73 unsigned Visibility : 2; // The visibility style of this global
74 unsigned Alignment : 16; // Alignment of this symbol, must be power of two
75 unsigned UnnamedAddr : 1; // This value's address is not significant
76 unsigned DllStorageClass : 2; // DLL storage class
77 Module *Parent; // The containing module.
78 std::string Section; // Section to emit this into, empty mean default
81 removeDeadConstantUsers(); // remove any dead constants using this.
84 unsigned getAlignment() const {
85 return (1u << Alignment) >> 1;
87 void setAlignment(unsigned Align);
89 bool hasUnnamedAddr() const { return UnnamedAddr; }
90 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
92 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
93 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
94 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
95 bool hasProtectedVisibility() const {
96 return Visibility == ProtectedVisibility;
98 void setVisibility(VisibilityTypes V) { Visibility = V; }
100 DLLStorageClassTypes getDLLStorageClass() const {
101 return DLLStorageClassTypes(DllStorageClass);
103 bool hasDLLImportStorageClass() const {
104 return DllStorageClass == DLLImportStorageClass;
106 bool hasDLLExportStorageClass() const {
107 return DllStorageClass == DLLExportStorageClass;
109 void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; }
111 bool hasSection() const { return !Section.empty(); }
112 const std::string &getSection() const { return Section; }
113 void setSection(StringRef S);
115 /// getType - Global values are always pointers.
116 inline PointerType *getType() const {
117 return cast<PointerType>(User::getType());
120 static LinkageTypes getLinkOnceLinkage(bool ODR) {
121 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
123 static LinkageTypes getWeakLinkage(bool ODR) {
124 return ODR ? WeakODRLinkage : WeakAnyLinkage;
127 static bool isExternalLinkage(LinkageTypes Linkage) {
128 return Linkage == ExternalLinkage;
130 static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
131 return Linkage == AvailableExternallyLinkage;
133 static bool isLinkOnceLinkage(LinkageTypes Linkage) {
134 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
136 static bool isWeakLinkage(LinkageTypes Linkage) {
137 return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
139 static bool isAppendingLinkage(LinkageTypes Linkage) {
140 return Linkage == AppendingLinkage;
142 static bool isInternalLinkage(LinkageTypes Linkage) {
143 return Linkage == InternalLinkage;
145 static bool isPrivateLinkage(LinkageTypes Linkage) {
146 return Linkage == PrivateLinkage;
148 static bool isLocalLinkage(LinkageTypes Linkage) {
149 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage);
151 static bool isExternalWeakLinkage(LinkageTypes Linkage) {
152 return Linkage == ExternalWeakLinkage;
154 static bool isCommonLinkage(LinkageTypes Linkage) {
155 return Linkage == CommonLinkage;
158 /// isDiscardableIfUnused - Whether the definition of this global may be
159 /// discarded if it is not used in its compilation unit.
160 static bool isDiscardableIfUnused(LinkageTypes Linkage) {
161 return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
164 /// mayBeOverridden - Whether the definition of this global may be replaced
165 /// by something non-equivalent at link time. For example, if a function has
166 /// weak linkage then the code defining it may be replaced by different code.
167 static bool mayBeOverridden(LinkageTypes Linkage) {
168 return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage ||
169 Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
172 /// isWeakForLinker - Whether the definition of this global may be replaced at
173 /// link time. NB: Using this method outside of the code generators is almost
174 /// always a mistake: when working at the IR level use mayBeOverridden instead
175 /// as it knows about ODR semantics.
176 static bool isWeakForLinker(LinkageTypes Linkage) {
177 return Linkage == AvailableExternallyLinkage || Linkage == WeakAnyLinkage ||
178 Linkage == WeakODRLinkage || Linkage == LinkOnceAnyLinkage ||
179 Linkage == LinkOnceODRLinkage || Linkage == CommonLinkage ||
180 Linkage == ExternalWeakLinkage;
183 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
184 bool hasAvailableExternallyLinkage() const {
185 return isAvailableExternallyLinkage(Linkage);
187 bool hasLinkOnceLinkage() const {
188 return isLinkOnceLinkage(Linkage);
190 bool hasWeakLinkage() const {
191 return isWeakLinkage(Linkage);
193 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
194 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
195 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
196 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
197 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
198 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
200 void setLinkage(LinkageTypes LT) { Linkage = LT; }
201 LinkageTypes getLinkage() const { return Linkage; }
203 bool isDiscardableIfUnused() const {
204 return isDiscardableIfUnused(Linkage);
207 bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
209 bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
211 /// copyAttributesFrom - copy all additional attributes (those not needed to
212 /// create a GlobalValue) from the GlobalValue Src to this one.
213 virtual void copyAttributesFrom(const GlobalValue *Src);
215 /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
216 /// printer to not emit usual symbol prefix before the symbol name is used
217 /// then return linkage name after skipping this special LLVM prefix.
218 static StringRef getRealLinkageName(StringRef Name) {
219 if (!Name.empty() && Name[0] == '\1')
220 return Name.substr(1);
224 /// @name Materialization
225 /// Materialization is used to construct functions only as they're needed. This
226 /// is useful to reduce memory usage in LLVM or parsing work done by the
227 /// BitcodeReader to load the Module.
230 /// isMaterializable - If this function's Module is being lazily streamed in
231 /// functions from disk or some other source, this method can be used to check
232 /// to see if the function has been read in yet or not.
233 bool isMaterializable() const;
235 /// isDematerializable - Returns true if this function was loaded from a
236 /// GVMaterializer that's still attached to its Module and that knows how to
237 /// dematerialize the function.
238 bool isDematerializable() const;
240 /// Materialize - make sure this GlobalValue is fully read. If the module is
241 /// corrupt, this returns true and fills in the optional string with
242 /// information about the problem. If successful, this returns false.
243 bool Materialize(std::string *ErrInfo = nullptr);
245 /// Dematerialize - If this GlobalValue is read in, and if the GVMaterializer
246 /// supports it, release the memory for the function, and set it up to be
247 /// materialized lazily. If !isDematerializable(), this method is a noop.
248 void Dematerialize();
252 /// Override from Constant class.
253 void destroyConstant() override;
255 /// isDeclaration - Return true if the primary definition of this global
256 /// value is outside of the current translation unit.
257 bool isDeclaration() const;
259 /// removeFromParent - This method unlinks 'this' from the containing module,
260 /// but does not delete it.
261 virtual void removeFromParent() = 0;
263 /// eraseFromParent - This method unlinks 'this' from the containing module
265 virtual void eraseFromParent() = 0;
267 /// getParent - Get the module that this global value is contained inside
269 inline Module *getParent() { return Parent; }
270 inline const Module *getParent() const { return Parent; }
272 const DataLayout *getDataLayout() const;
274 // Methods for support type inquiry through isa, cast, and dyn_cast:
275 static inline bool classof(const Value *V) {
276 return V->getValueID() == Value::FunctionVal ||
277 V->getValueID() == Value::GlobalVariableVal ||
278 V->getValueID() == Value::GlobalAliasVal;
282 } // End llvm namespace