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 LinkerPrivateLinkage, ///< Like Private, but linker removes.
44 LinkerPrivateWeakLinkage, ///< Like LinkerPrivate, but weak.
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), Alignment(0), UnnamedAddr(0),
68 DllStorageClass(DefaultStorageClass), Parent(0) {
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 Alignment : 16; // Alignment of this symbol, must be power of two
77 unsigned UnnamedAddr : 1; // This value's address is not significant
78 unsigned DllStorageClass : 2; // DLL storage class
79 Module *Parent; // The containing module.
80 std::string Section; // Section to emit this into, empty mean default
83 removeDeadConstantUsers(); // remove any dead constants using this.
86 unsigned getAlignment() const {
87 return (1u << Alignment) >> 1;
89 void setAlignment(unsigned Align);
91 bool hasUnnamedAddr() const { return UnnamedAddr; }
92 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
94 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
95 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
96 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
97 bool hasProtectedVisibility() const {
98 return Visibility == ProtectedVisibility;
100 void setVisibility(VisibilityTypes V) { Visibility = V; }
102 DLLStorageClassTypes getDLLStorageClass() const {
103 return DLLStorageClassTypes(DllStorageClass);
105 bool hasDLLImportStorageClass() const {
106 return DllStorageClass == DLLImportStorageClass;
108 bool hasDLLExportStorageClass() const {
109 return DllStorageClass == DLLExportStorageClass;
111 void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; }
113 bool hasSection() const { return !Section.empty(); }
114 const std::string &getSection() const { return Section; }
115 void setSection(StringRef S) {
116 assert((getValueID() != Value::GlobalAliasVal || S.empty()) &&
117 "GlobalAlias should not have a section!");
121 /// If the usage is empty (except transitively dead constants), then this
122 /// global value can be safely deleted since the destructor will
123 /// delete the dead constants as well.
124 /// @brief Determine if the usage of this global value is empty except
125 /// for transitively dead constants.
126 bool use_empty_except_constants();
128 /// getType - Global values are always pointers.
129 inline PointerType *getType() const {
130 return cast<PointerType>(User::getType());
133 static LinkageTypes getLinkOnceLinkage(bool ODR) {
134 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
136 static LinkageTypes getWeakLinkage(bool ODR) {
137 return ODR ? WeakODRLinkage : WeakAnyLinkage;
140 static bool isExternalLinkage(LinkageTypes Linkage) {
141 return Linkage == ExternalLinkage;
143 static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
144 return Linkage == AvailableExternallyLinkage;
146 static bool isLinkOnceLinkage(LinkageTypes Linkage) {
147 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
149 static bool isWeakLinkage(LinkageTypes Linkage) {
150 return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
152 static bool isAppendingLinkage(LinkageTypes Linkage) {
153 return Linkage == AppendingLinkage;
155 static bool isInternalLinkage(LinkageTypes Linkage) {
156 return Linkage == InternalLinkage;
158 static bool isPrivateLinkage(LinkageTypes Linkage) {
159 return Linkage == PrivateLinkage;
161 static bool isLinkerPrivateLinkage(LinkageTypes Linkage) {
162 return Linkage == LinkerPrivateLinkage;
164 static bool isLinkerPrivateWeakLinkage(LinkageTypes Linkage) {
165 return Linkage == LinkerPrivateWeakLinkage;
167 static bool isLocalLinkage(LinkageTypes Linkage) {
168 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage) ||
169 isLinkerPrivateLinkage(Linkage) || isLinkerPrivateWeakLinkage(Linkage);
171 static bool isExternalWeakLinkage(LinkageTypes Linkage) {
172 return Linkage == ExternalWeakLinkage;
174 static bool isCommonLinkage(LinkageTypes Linkage) {
175 return Linkage == CommonLinkage;
178 /// isDiscardableIfUnused - Whether the definition of this global may be
179 /// discarded if it is not used in its compilation unit.
180 static bool isDiscardableIfUnused(LinkageTypes Linkage) {
181 return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
184 /// mayBeOverridden - Whether the definition of this global may be replaced
185 /// by something non-equivalent at link time. For example, if a function has
186 /// weak linkage then the code defining it may be replaced by different code.
187 static bool mayBeOverridden(LinkageTypes Linkage) {
188 return Linkage == WeakAnyLinkage ||
189 Linkage == LinkOnceAnyLinkage ||
190 Linkage == CommonLinkage ||
191 Linkage == ExternalWeakLinkage ||
192 Linkage == LinkerPrivateWeakLinkage;
195 /// isWeakForLinker - Whether the definition of this global may be replaced at
196 /// link time. NB: Using this method outside of the code generators is almost
197 /// always a mistake: when working at the IR level use mayBeOverridden instead
198 /// as it knows about ODR semantics.
199 static bool isWeakForLinker(LinkageTypes Linkage) {
200 return Linkage == AvailableExternallyLinkage ||
201 Linkage == WeakAnyLinkage ||
202 Linkage == WeakODRLinkage ||
203 Linkage == LinkOnceAnyLinkage ||
204 Linkage == LinkOnceODRLinkage ||
205 Linkage == CommonLinkage ||
206 Linkage == ExternalWeakLinkage ||
207 Linkage == LinkerPrivateWeakLinkage;
210 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
211 bool hasAvailableExternallyLinkage() const {
212 return isAvailableExternallyLinkage(Linkage);
214 bool hasLinkOnceLinkage() const {
215 return isLinkOnceLinkage(Linkage);
217 bool hasWeakLinkage() const {
218 return isWeakLinkage(Linkage);
220 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
221 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
222 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
223 bool hasLinkerPrivateLinkage() const { return isLinkerPrivateLinkage(Linkage); }
224 bool hasLinkerPrivateWeakLinkage() const {
225 return isLinkerPrivateWeakLinkage(Linkage);
227 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
228 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
229 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
231 void setLinkage(LinkageTypes LT) { Linkage = LT; }
232 LinkageTypes getLinkage() const { return Linkage; }
234 bool isDiscardableIfUnused() const {
235 return isDiscardableIfUnused(Linkage);
238 bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
240 bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
242 /// copyAttributesFrom - copy all additional attributes (those not needed to
243 /// create a GlobalValue) from the GlobalValue Src to this one.
244 virtual void copyAttributesFrom(const GlobalValue *Src);
246 /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
247 /// printer to not emit usual symbol prefix before the symbol name is used
248 /// then return linkage name after skipping this special LLVM prefix.
249 static StringRef getRealLinkageName(StringRef Name) {
250 if (!Name.empty() && Name[0] == '\1')
251 return Name.substr(1);
255 /// @name Materialization
256 /// Materialization is used to construct functions only as they're needed. This
257 /// is useful to reduce memory usage in LLVM or parsing work done by the
258 /// BitcodeReader to load the Module.
261 /// isMaterializable - If this function's Module is being lazily streamed in
262 /// functions from disk or some other source, this method can be used to check
263 /// to see if the function has been read in yet or not.
264 bool isMaterializable() const;
266 /// isDematerializable - Returns true if this function was loaded from a
267 /// GVMaterializer that's still attached to its Module and that knows how to
268 /// dematerialize the function.
269 bool isDematerializable() const;
271 /// Materialize - make sure this GlobalValue is fully read. If the module is
272 /// corrupt, this returns true and fills in the optional string with
273 /// information about the problem. If successful, this returns false.
274 bool Materialize(std::string *ErrInfo = 0);
276 /// Dematerialize - If this GlobalValue is read in, and if the GVMaterializer
277 /// supports it, release the memory for the function, and set it up to be
278 /// materialized lazily. If !isDematerializable(), this method is a noop.
279 void Dematerialize();
283 /// Override from Constant class.
284 virtual void destroyConstant();
286 /// isDeclaration - Return true if the primary definition of this global
287 /// value is outside of the current translation unit.
288 bool isDeclaration() const;
290 /// removeFromParent - This method unlinks 'this' from the containing module,
291 /// but does not delete it.
292 virtual void removeFromParent() = 0;
294 /// eraseFromParent - This method unlinks 'this' from the containing module
296 virtual void eraseFromParent() = 0;
298 /// getParent - Get the module that this global value is contained inside
300 inline Module *getParent() { return Parent; }
301 inline const Module *getParent() const { return Parent; }
303 // Methods for support type inquiry through isa, cast, and dyn_cast:
304 static inline bool classof(const Value *V) {
305 return V->getValueID() == Value::FunctionVal ||
306 V->getValueID() == Value::GlobalVariableVal ||
307 V->getValueID() == Value::GlobalAliasVal;
311 } // End llvm namespace