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 &); // do not implement
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 WeakAnyLinkage, ///< Keep one copy of named function when linking (weak)
38 WeakODRLinkage, ///< Same, but only replaced by something equivalent.
39 AppendingLinkage, ///< Special purpose, only applies to global arrays
40 InternalLinkage, ///< Rename collisions when linking (static functions).
41 PrivateLinkage, ///< Like Internal, but omit from symbol table.
42 LinkerPrivateLinkage, ///< Like Private, but linker removes.
43 LinkerPrivateWeakLinkage, ///< Like LinkerPrivate, but weak.
44 LinkerPrivateWeakDefAutoLinkage, ///< Like LinkerPrivateWeak, but possibly
46 DLLImportLinkage, ///< Function to be imported from DLL
47 DLLExportLinkage, ///< Function to be accessible from DLL.
48 ExternalWeakLinkage,///< ExternalWeak linkage description.
49 CommonLinkage ///< Tentative definitions.
52 /// @brief An enumeration for the kinds of visibility of global values.
53 enum VisibilityTypes {
54 DefaultVisibility = 0, ///< The GV is visible
55 HiddenVisibility, ///< The GV is hidden
56 ProtectedVisibility ///< The GV is protected
60 GlobalValue(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
61 LinkageTypes linkage, const Twine &Name)
62 : Constant(ty, vty, Ops, NumOps), Parent(0),
63 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0),
69 // Note: VC++ treats enums as signed, so an extra bit is required to prevent
70 // Linkage and Visibility from turning into negative values.
71 LinkageTypes Linkage : 5; // The linkage of this global
72 unsigned Visibility : 2; // The visibility style of this global
73 unsigned Alignment : 16; // Alignment of this symbol, must be power of two
74 unsigned UnnamedAddr : 1; // This value's address is not significant
75 std::string Section; // Section to emit this into, empty mean default
78 removeDeadConstantUsers(); // remove any dead constants using this.
81 unsigned getAlignment() const {
82 return (1u << Alignment) >> 1;
84 void setAlignment(unsigned Align);
86 bool hasUnnamedAddr() const { return UnnamedAddr; }
87 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
89 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
90 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
91 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
92 bool hasProtectedVisibility() const {
93 return Visibility == ProtectedVisibility;
95 void setVisibility(VisibilityTypes V) { Visibility = V; }
97 bool hasSection() const { return !Section.empty(); }
98 const std::string &getSection() const { return Section; }
99 void setSection(StringRef S) { Section = S; }
101 /// If the usage is empty (except transitively dead constants), then this
102 /// global value can be safely deleted since the destructor will
103 /// delete the dead constants as well.
104 /// @brief Determine if the usage of this global value is empty except
105 /// for transitively dead constants.
106 bool use_empty_except_constants();
108 /// getType - Global values are always pointers.
109 inline const PointerType *getType() const {
110 return reinterpret_cast<const PointerType*>(User::getType());
113 static LinkageTypes getLinkOnceLinkage(bool ODR) {
114 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
116 static LinkageTypes getWeakLinkage(bool ODR) {
117 return ODR ? WeakODRLinkage : WeakAnyLinkage;
120 static bool isExternalLinkage(LinkageTypes Linkage) {
121 return Linkage == ExternalLinkage;
123 static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
124 return Linkage == AvailableExternallyLinkage;
126 static bool isLinkOnceLinkage(LinkageTypes Linkage) {
127 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
129 static bool isWeakLinkage(LinkageTypes Linkage) {
130 return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
132 static bool isAppendingLinkage(LinkageTypes Linkage) {
133 return Linkage == AppendingLinkage;
135 static bool isInternalLinkage(LinkageTypes Linkage) {
136 return Linkage == InternalLinkage;
138 static bool isPrivateLinkage(LinkageTypes Linkage) {
139 return Linkage == PrivateLinkage;
141 static bool isLinkerPrivateLinkage(LinkageTypes Linkage) {
142 return Linkage == LinkerPrivateLinkage;
144 static bool isLinkerPrivateWeakLinkage(LinkageTypes Linkage) {
145 return Linkage == LinkerPrivateWeakLinkage;
147 static bool isLinkerPrivateWeakDefAutoLinkage(LinkageTypes Linkage) {
148 return Linkage == LinkerPrivateWeakDefAutoLinkage;
150 static bool isLocalLinkage(LinkageTypes Linkage) {
151 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage) ||
152 isLinkerPrivateLinkage(Linkage) || isLinkerPrivateWeakLinkage(Linkage) ||
153 isLinkerPrivateWeakDefAutoLinkage(Linkage);
155 static bool isDLLImportLinkage(LinkageTypes Linkage) {
156 return Linkage == DLLImportLinkage;
158 static bool isDLLExportLinkage(LinkageTypes Linkage) {
159 return Linkage == DLLExportLinkage;
161 static bool isExternalWeakLinkage(LinkageTypes Linkage) {
162 return Linkage == ExternalWeakLinkage;
164 static bool isCommonLinkage(LinkageTypes Linkage) {
165 return Linkage == CommonLinkage;
168 /// mayBeOverridden - Whether the definition of this global may be replaced
169 /// by something non-equivalent at link time. For example, if a function has
170 /// weak linkage then the code defining it may be replaced by different code.
171 static bool mayBeOverridden(LinkageTypes Linkage) {
172 return Linkage == WeakAnyLinkage ||
173 Linkage == LinkOnceAnyLinkage ||
174 Linkage == CommonLinkage ||
175 Linkage == ExternalWeakLinkage ||
176 Linkage == LinkerPrivateWeakLinkage ||
177 Linkage == LinkerPrivateWeakDefAutoLinkage;
180 /// isWeakForLinker - Whether the definition of this global may be replaced at
181 /// link time. NB: Using this method outside of the code generators is almost
182 /// always a mistake: when working at the IR level use mayBeOverridden instead
183 /// as it knows about ODR semantics.
184 static bool isWeakForLinker(LinkageTypes Linkage) {
185 return Linkage == AvailableExternallyLinkage ||
186 Linkage == WeakAnyLinkage ||
187 Linkage == WeakODRLinkage ||
188 Linkage == LinkOnceAnyLinkage ||
189 Linkage == LinkOnceODRLinkage ||
190 Linkage == CommonLinkage ||
191 Linkage == ExternalWeakLinkage ||
192 Linkage == LinkerPrivateWeakLinkage ||
193 Linkage == LinkerPrivateWeakDefAutoLinkage;
196 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
197 bool hasAvailableExternallyLinkage() const {
198 return isAvailableExternallyLinkage(Linkage);
200 bool hasLinkOnceLinkage() const {
201 return isLinkOnceLinkage(Linkage);
203 bool hasWeakLinkage() const {
204 return isWeakLinkage(Linkage);
206 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
207 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
208 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
209 bool hasLinkerPrivateLinkage() const { return isLinkerPrivateLinkage(Linkage); }
210 bool hasLinkerPrivateWeakLinkage() const {
211 return isLinkerPrivateWeakLinkage(Linkage);
213 bool hasLinkerPrivateWeakDefAutoLinkage() const {
214 return isLinkerPrivateWeakDefAutoLinkage(Linkage);
216 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
217 bool hasDLLImportLinkage() const { return isDLLImportLinkage(Linkage); }
218 bool hasDLLExportLinkage() const { return isDLLExportLinkage(Linkage); }
219 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
220 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
222 void setLinkage(LinkageTypes LT) { Linkage = LT; }
223 LinkageTypes getLinkage() const { return Linkage; }
225 bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
227 bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
229 /// copyAttributesFrom - copy all additional attributes (those not needed to
230 /// create a GlobalValue) from the GlobalValue Src to this one.
231 virtual void copyAttributesFrom(const GlobalValue *Src);
233 /// @name Materialization
234 /// Materialization is used to construct functions only as they're needed. This
235 /// is useful to reduce memory usage in LLVM or parsing work done by the
236 /// BitcodeReader to load the Module.
239 /// isMaterializable - If this function's Module is being lazily streamed in
240 /// functions from disk or some other source, this method can be used to check
241 /// to see if the function has been read in yet or not.
242 bool isMaterializable() const;
244 /// isDematerializable - Returns true if this function was loaded from a
245 /// GVMaterializer that's still attached to its Module and that knows how to
246 /// dematerialize the function.
247 bool isDematerializable() const;
249 /// Materialize - make sure this GlobalValue is fully read. If the module is
250 /// corrupt, this returns true and fills in the optional string with
251 /// information about the problem. If successful, this returns false.
252 bool Materialize(std::string *ErrInfo = 0);
254 /// Dematerialize - If this GlobalValue is read in, and if the GVMaterializer
255 /// supports it, release the memory for the function, and set it up to be
256 /// materialized lazily. If !isDematerializable(), this method is a noop.
257 void Dematerialize();
261 /// Override from Constant class. No GlobalValue's are null values so this
262 /// always returns false.
263 virtual bool isNullValue() const { return false; }
265 /// Override from Constant class.
266 virtual void destroyConstant();
268 /// isDeclaration - Return true if the primary definition of this global
269 /// value is outside of the current translation unit...
270 virtual bool isDeclaration() const = 0;
272 /// removeFromParent - This method unlinks 'this' from the containing module,
273 /// but does not delete it.
274 virtual void removeFromParent() = 0;
276 /// eraseFromParent - This method unlinks 'this' from the containing module
278 virtual void eraseFromParent() = 0;
280 /// getParent - Get the module that this global value is contained inside
282 inline Module *getParent() { return Parent; }
283 inline const Module *getParent() const { return Parent; }
285 /// removeDeadConstantUsers - If there are any dead constant users dangling
286 /// off of this global value, remove them. This method is useful for clients
287 /// that want to check to see if a global is unused, but don't want to deal
288 /// with potentially dead constants hanging off of the globals.
289 void removeDeadConstantUsers() const;
291 // Methods for support type inquiry through isa, cast, and dyn_cast:
292 static inline bool classof(const GlobalValue *) { return true; }
293 static inline bool classof(const Value *V) {
294 return V->getValueID() == Value::FunctionVal ||
295 V->getValueID() == Value::GlobalVariableVal ||
296 V->getValueID() == Value::GlobalAliasVal;
300 } // End llvm namespace