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 void setAlignment(unsigned Align);
87 bool hasUnnamedAddr() const { return UnnamedAddr; }
88 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
90 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
91 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
92 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
93 bool hasProtectedVisibility() const {
94 return Visibility == ProtectedVisibility;
96 void setVisibility(VisibilityTypes V) { Visibility = V; }
98 DLLStorageClassTypes getDLLStorageClass() const {
99 return DLLStorageClassTypes(DllStorageClass);
101 bool hasDLLImportStorageClass() const {
102 return DllStorageClass == DLLImportStorageClass;
104 bool hasDLLExportStorageClass() const {
105 return DllStorageClass == DLLExportStorageClass;
107 void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; }
109 bool hasSection() const { return !getSection().empty(); }
110 const std::string &getSection() const;
111 void setSection(StringRef S);
113 /// getType - Global values are always pointers.
114 inline PointerType *getType() const {
115 return cast<PointerType>(User::getType());
118 static LinkageTypes getLinkOnceLinkage(bool ODR) {
119 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
121 static LinkageTypes getWeakLinkage(bool ODR) {
122 return ODR ? WeakODRLinkage : WeakAnyLinkage;
125 static bool isExternalLinkage(LinkageTypes Linkage) {
126 return Linkage == ExternalLinkage;
128 static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
129 return Linkage == AvailableExternallyLinkage;
131 static bool isLinkOnceLinkage(LinkageTypes Linkage) {
132 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
134 static bool isWeakLinkage(LinkageTypes Linkage) {
135 return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
137 static bool isAppendingLinkage(LinkageTypes Linkage) {
138 return Linkage == AppendingLinkage;
140 static bool isInternalLinkage(LinkageTypes Linkage) {
141 return Linkage == InternalLinkage;
143 static bool isPrivateLinkage(LinkageTypes Linkage) {
144 return Linkage == PrivateLinkage;
146 static bool isLocalLinkage(LinkageTypes Linkage) {
147 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage);
149 static bool isExternalWeakLinkage(LinkageTypes Linkage) {
150 return Linkage == ExternalWeakLinkage;
152 static bool isCommonLinkage(LinkageTypes Linkage) {
153 return Linkage == CommonLinkage;
156 /// isDiscardableIfUnused - Whether the definition of this global may be
157 /// discarded if it is not used in its compilation unit.
158 static bool isDiscardableIfUnused(LinkageTypes Linkage) {
159 return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
162 /// mayBeOverridden - Whether the definition of this global may be replaced
163 /// by something non-equivalent at link time. For example, if a function has
164 /// weak linkage then the code defining it may be replaced by different code.
165 static bool mayBeOverridden(LinkageTypes Linkage) {
166 return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage ||
167 Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
170 /// isWeakForLinker - Whether the definition of this global may be replaced at
171 /// link time. NB: Using this method outside of the code generators is almost
172 /// always a mistake: when working at the IR level use mayBeOverridden instead
173 /// as it knows about ODR semantics.
174 static bool isWeakForLinker(LinkageTypes Linkage) {
175 return Linkage == AvailableExternallyLinkage || Linkage == WeakAnyLinkage ||
176 Linkage == WeakODRLinkage || Linkage == LinkOnceAnyLinkage ||
177 Linkage == LinkOnceODRLinkage || Linkage == CommonLinkage ||
178 Linkage == ExternalWeakLinkage;
181 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
182 bool hasAvailableExternallyLinkage() const {
183 return isAvailableExternallyLinkage(Linkage);
185 bool hasLinkOnceLinkage() const {
186 return isLinkOnceLinkage(Linkage);
188 bool hasWeakLinkage() const {
189 return isWeakLinkage(Linkage);
191 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
192 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
193 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
194 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
195 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
196 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
198 void setLinkage(LinkageTypes LT) { Linkage = LT; }
199 LinkageTypes getLinkage() const { return Linkage; }
201 bool isDiscardableIfUnused() const {
202 return isDiscardableIfUnused(Linkage);
205 bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
207 bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
209 /// copyAttributesFrom - copy all additional attributes (those not needed to
210 /// create a GlobalValue) from the GlobalValue Src to this one.
211 virtual void copyAttributesFrom(const GlobalValue *Src);
213 /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
214 /// printer to not emit usual symbol prefix before the symbol name is used
215 /// then return linkage name after skipping this special LLVM prefix.
216 static StringRef getRealLinkageName(StringRef Name) {
217 if (!Name.empty() && Name[0] == '\1')
218 return Name.substr(1);
222 /// @name Materialization
223 /// Materialization is used to construct functions only as they're needed. This
224 /// is useful to reduce memory usage in LLVM or parsing work done by the
225 /// BitcodeReader to load the Module.
228 /// isMaterializable - If this function's Module is being lazily streamed in
229 /// functions from disk or some other source, this method can be used to check
230 /// to see if the function has been read in yet or not.
231 bool isMaterializable() const;
233 /// isDematerializable - Returns true if this function was loaded from a
234 /// GVMaterializer that's still attached to its Module and that knows how to
235 /// dematerialize the function.
236 bool isDematerializable() const;
238 /// Materialize - make sure this GlobalValue is fully read. If the module is
239 /// corrupt, this returns true and fills in the optional string with
240 /// information about the problem. If successful, this returns false.
241 bool Materialize(std::string *ErrInfo = nullptr);
243 /// Dematerialize - If this GlobalValue is read in, and if the GVMaterializer
244 /// supports it, release the memory for the function, and set it up to be
245 /// materialized lazily. If !isDematerializable(), this method is a noop.
246 void Dematerialize();
250 /// Override from Constant class.
251 void destroyConstant() override;
253 /// isDeclaration - Return true if the primary definition of this global
254 /// value is outside of the current translation unit.
255 bool isDeclaration() const;
257 /// removeFromParent - This method unlinks 'this' from the containing module,
258 /// but does not delete it.
259 virtual void removeFromParent() = 0;
261 /// eraseFromParent - This method unlinks 'this' from the containing module
263 virtual void eraseFromParent() = 0;
265 /// getParent - Get the module that this global value is contained inside
267 inline Module *getParent() { return Parent; }
268 inline const Module *getParent() const { return Parent; }
270 const DataLayout *getDataLayout() const;
272 // Methods for support type inquiry through isa, cast, and dyn_cast:
273 static inline bool classof(const Value *V) {
274 return V->getValueID() == Value::FunctionVal ||
275 V->getValueID() == Value::GlobalVariableVal ||
276 V->getValueID() == Value::GlobalAliasVal;
280 } // End llvm namespace