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_GLOBALVALUE_H
19 #define LLVM_GLOBALVALUE_H
21 #include "llvm/Constant.h"
28 class GlobalValue : public Constant {
29 GlobalValue(const GlobalValue &) LLVM_DELETED_FUNCTION;
31 /// @brief An enumeration for the kinds of linkage for global values.
33 ExternalLinkage = 0,///< Externally visible function
34 AvailableExternallyLinkage, ///< Available for inspection, not emission.
35 LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
36 LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
37 LinkOnceODRAutoHideLinkage, ///< Like LinkOnceODRLinkage but addr not taken.
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 DLLImportLinkage, ///< Function to be imported from DLL
46 DLLExportLinkage, ///< Function to be accessible from DLL.
47 ExternalWeakLinkage,///< ExternalWeak linkage description.
48 CommonLinkage ///< Tentative definitions.
51 /// @brief An enumeration for the kinds of visibility of global values.
52 enum VisibilityTypes {
53 DefaultVisibility = 0, ///< The GV is visible
54 HiddenVisibility, ///< The GV is hidden
55 ProtectedVisibility ///< The GV is protected
59 GlobalValue(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
60 LinkageTypes linkage, const Twine &Name)
61 : Constant(ty, vty, Ops, NumOps), Linkage(linkage),
62 Visibility(DefaultVisibility), Alignment(0), UnnamedAddr(0), Parent(0) {
66 // Note: VC++ treats enums as signed, so an extra bit is required to prevent
67 // Linkage and Visibility from turning into negative values.
68 LinkageTypes Linkage : 5; // The linkage of this global
69 unsigned Visibility : 2; // The visibility style of this global
70 unsigned Alignment : 16; // Alignment of this symbol, must be power of two
71 unsigned UnnamedAddr : 1; // This value's address is not significant
72 Module *Parent; // The containing module.
73 std::string Section; // Section to emit this into, empty mean default
76 removeDeadConstantUsers(); // remove any dead constants using this.
79 unsigned getAlignment() const {
80 return (1u << Alignment) >> 1;
82 void setAlignment(unsigned Align);
84 bool hasUnnamedAddr() const { return UnnamedAddr; }
85 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
87 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
88 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
89 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
90 bool hasProtectedVisibility() const {
91 return Visibility == ProtectedVisibility;
93 void setVisibility(VisibilityTypes V) { Visibility = V; }
95 bool hasSection() const { return !Section.empty(); }
96 const std::string &getSection() const { return Section; }
97 void setSection(StringRef S) { Section = S; }
99 /// If the usage is empty (except transitively dead constants), then this
100 /// global value can be safely deleted since the destructor will
101 /// delete the dead constants as well.
102 /// @brief Determine if the usage of this global value is empty except
103 /// for transitively dead constants.
104 bool use_empty_except_constants();
106 /// getType - Global values are always pointers.
107 inline PointerType *getType() const {
108 return reinterpret_cast<PointerType*>(User::getType());
111 static LinkageTypes getLinkOnceLinkage(bool ODR) {
112 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
114 static LinkageTypes getWeakLinkage(bool ODR) {
115 return ODR ? WeakODRLinkage : WeakAnyLinkage;
118 static bool isExternalLinkage(LinkageTypes Linkage) {
119 return Linkage == ExternalLinkage;
121 static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
122 return Linkage == AvailableExternallyLinkage;
124 static bool isLinkOnceLinkage(LinkageTypes Linkage) {
125 return Linkage == LinkOnceAnyLinkage ||
126 Linkage == LinkOnceODRLinkage ||
127 Linkage == LinkOnceODRAutoHideLinkage;
129 static bool isLinkOnceODRAutoHideLinkage(LinkageTypes Linkage) {
130 return Linkage == LinkOnceODRAutoHideLinkage;
132 static bool isWeakLinkage(LinkageTypes Linkage) {
133 return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
135 static bool isAppendingLinkage(LinkageTypes Linkage) {
136 return Linkage == AppendingLinkage;
138 static bool isInternalLinkage(LinkageTypes Linkage) {
139 return Linkage == InternalLinkage;
141 static bool isPrivateLinkage(LinkageTypes Linkage) {
142 return Linkage == PrivateLinkage;
144 static bool isLinkerPrivateLinkage(LinkageTypes Linkage) {
145 return Linkage == LinkerPrivateLinkage;
147 static bool isLinkerPrivateWeakLinkage(LinkageTypes Linkage) {
148 return Linkage == LinkerPrivateWeakLinkage;
150 static bool isLocalLinkage(LinkageTypes Linkage) {
151 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage) ||
152 isLinkerPrivateLinkage(Linkage) || isLinkerPrivateWeakLinkage(Linkage);
154 static bool isDLLImportLinkage(LinkageTypes Linkage) {
155 return Linkage == DLLImportLinkage;
157 static bool isDLLExportLinkage(LinkageTypes Linkage) {
158 return Linkage == DLLExportLinkage;
160 static bool isExternalWeakLinkage(LinkageTypes Linkage) {
161 return Linkage == ExternalWeakLinkage;
163 static bool isCommonLinkage(LinkageTypes Linkage) {
164 return Linkage == CommonLinkage;
167 /// isDiscardableIfUnused - Whether the definition of this global may be
168 /// discarded if it is not used in its compilation unit.
169 static bool isDiscardableIfUnused(LinkageTypes Linkage) {
170 return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
173 /// mayBeOverridden - Whether the definition of this global may be replaced
174 /// by something non-equivalent at link time. For example, if a function has
175 /// weak linkage then the code defining it may be replaced by different code.
176 static bool mayBeOverridden(LinkageTypes Linkage) {
177 return Linkage == WeakAnyLinkage ||
178 Linkage == LinkOnceAnyLinkage ||
179 Linkage == CommonLinkage ||
180 Linkage == ExternalWeakLinkage ||
181 Linkage == LinkerPrivateWeakLinkage;
184 /// isWeakForLinker - Whether the definition of this global may be replaced at
185 /// link time. NB: Using this method outside of the code generators is almost
186 /// always a mistake: when working at the IR level use mayBeOverridden instead
187 /// as it knows about ODR semantics.
188 static bool isWeakForLinker(LinkageTypes Linkage) {
189 return Linkage == AvailableExternallyLinkage ||
190 Linkage == WeakAnyLinkage ||
191 Linkage == WeakODRLinkage ||
192 Linkage == LinkOnceAnyLinkage ||
193 Linkage == LinkOnceODRLinkage ||
194 Linkage == LinkOnceODRAutoHideLinkage ||
195 Linkage == CommonLinkage ||
196 Linkage == ExternalWeakLinkage ||
197 Linkage == LinkerPrivateWeakLinkage;
200 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
201 bool hasAvailableExternallyLinkage() const {
202 return isAvailableExternallyLinkage(Linkage);
204 bool hasLinkOnceLinkage() const {
205 return isLinkOnceLinkage(Linkage);
207 bool hasLinkOnceODRAutoHideLinkage() const {
208 return isLinkOnceODRAutoHideLinkage(Linkage);
210 bool hasWeakLinkage() const {
211 return isWeakLinkage(Linkage);
213 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
214 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
215 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
216 bool hasLinkerPrivateLinkage() const { return isLinkerPrivateLinkage(Linkage); }
217 bool hasLinkerPrivateWeakLinkage() const {
218 return isLinkerPrivateWeakLinkage(Linkage);
220 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
221 bool hasDLLImportLinkage() const { return isDLLImportLinkage(Linkage); }
222 bool hasDLLExportLinkage() const { return isDLLExportLinkage(Linkage); }
223 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
224 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
226 void setLinkage(LinkageTypes LT) { Linkage = LT; }
227 LinkageTypes getLinkage() const { return Linkage; }
229 bool isDiscardableIfUnused() const {
230 return isDiscardableIfUnused(Linkage);
233 bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
235 bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
237 /// copyAttributesFrom - copy all additional attributes (those not needed to
238 /// create a GlobalValue) from the GlobalValue Src to this one.
239 virtual void copyAttributesFrom(const GlobalValue *Src);
241 /// @name Materialization
242 /// Materialization is used to construct functions only as they're needed. This
243 /// is useful to reduce memory usage in LLVM or parsing work done by the
244 /// BitcodeReader to load the Module.
247 /// isMaterializable - If this function's Module is being lazily streamed in
248 /// functions from disk or some other source, this method can be used to check
249 /// to see if the function has been read in yet or not.
250 bool isMaterializable() const;
252 /// isDematerializable - Returns true if this function was loaded from a
253 /// GVMaterializer that's still attached to its Module and that knows how to
254 /// dematerialize the function.
255 bool isDematerializable() const;
257 /// Materialize - make sure this GlobalValue is fully read. If the module is
258 /// corrupt, this returns true and fills in the optional string with
259 /// information about the problem. If successful, this returns false.
260 bool Materialize(std::string *ErrInfo = 0);
262 /// Dematerialize - If this GlobalValue is read in, and if the GVMaterializer
263 /// supports it, release the memory for the function, and set it up to be
264 /// materialized lazily. If !isDematerializable(), this method is a noop.
265 void Dematerialize();
269 /// Override from Constant class.
270 virtual void destroyConstant();
272 /// isDeclaration - Return true if the primary definition of this global
273 /// value is outside of the current translation unit.
274 bool isDeclaration() const;
276 /// removeFromParent - This method unlinks 'this' from the containing module,
277 /// but does not delete it.
278 virtual void removeFromParent() = 0;
280 /// eraseFromParent - This method unlinks 'this' from the containing module
282 virtual void eraseFromParent() = 0;
284 /// getParent - Get the module that this global value is contained inside
286 inline Module *getParent() { return Parent; }
287 inline const Module *getParent() const { return Parent; }
289 // Methods for support type inquiry through isa, cast, and dyn_cast:
290 static inline bool classof(const Value *V) {
291 return V->getValueID() == Value::FunctionVal ||
292 V->getValueID() == Value::GlobalVariableVal ||
293 V->getValueID() == Value::GlobalAliasVal;
297 } // End llvm namespace