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 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 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 || Linkage == LinkOnceODRLinkage;
127 static bool isWeakLinkage(LinkageTypes Linkage) {
128 return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
130 static bool isAppendingLinkage(LinkageTypes Linkage) {
131 return Linkage == AppendingLinkage;
133 static bool isInternalLinkage(LinkageTypes Linkage) {
134 return Linkage == InternalLinkage;
136 static bool isPrivateLinkage(LinkageTypes Linkage) {
137 return Linkage == PrivateLinkage;
139 static bool isLinkerPrivateLinkage(LinkageTypes Linkage) {
140 return Linkage == LinkerPrivateLinkage;
142 static bool isLinkerPrivateWeakLinkage(LinkageTypes Linkage) {
143 return Linkage == LinkerPrivateWeakLinkage;
145 static bool isLocalLinkage(LinkageTypes Linkage) {
146 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage) ||
147 isLinkerPrivateLinkage(Linkage) || isLinkerPrivateWeakLinkage(Linkage);
149 static bool isDLLImportLinkage(LinkageTypes Linkage) {
150 return Linkage == DLLImportLinkage;
152 static bool isDLLExportLinkage(LinkageTypes Linkage) {
153 return Linkage == DLLExportLinkage;
155 static bool isExternalWeakLinkage(LinkageTypes Linkage) {
156 return Linkage == ExternalWeakLinkage;
158 static bool isCommonLinkage(LinkageTypes Linkage) {
159 return Linkage == CommonLinkage;
162 /// isDiscardableIfUnused - Whether the definition of this global may be
163 /// discarded if it is not used in its compilation unit.
164 static bool isDiscardableIfUnused(LinkageTypes Linkage) {
165 return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
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;
179 /// isWeakForLinker - Whether the definition of this global may be replaced at
180 /// link time. NB: Using this method outside of the code generators is almost
181 /// always a mistake: when working at the IR level use mayBeOverridden instead
182 /// as it knows about ODR semantics.
183 static bool isWeakForLinker(LinkageTypes Linkage) {
184 return Linkage == AvailableExternallyLinkage ||
185 Linkage == WeakAnyLinkage ||
186 Linkage == WeakODRLinkage ||
187 Linkage == LinkOnceAnyLinkage ||
188 Linkage == LinkOnceODRLinkage ||
189 Linkage == CommonLinkage ||
190 Linkage == ExternalWeakLinkage ||
191 Linkage == LinkerPrivateWeakLinkage;
194 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
195 bool hasAvailableExternallyLinkage() const {
196 return isAvailableExternallyLinkage(Linkage);
198 bool hasLinkOnceLinkage() const {
199 return isLinkOnceLinkage(Linkage);
201 bool hasWeakLinkage() const {
202 return isWeakLinkage(Linkage);
204 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
205 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
206 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
207 bool hasLinkerPrivateLinkage() const { return isLinkerPrivateLinkage(Linkage); }
208 bool hasLinkerPrivateWeakLinkage() const {
209 return isLinkerPrivateWeakLinkage(Linkage);
211 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
212 bool hasDLLImportLinkage() const { return isDLLImportLinkage(Linkage); }
213 bool hasDLLExportLinkage() const { return isDLLExportLinkage(Linkage); }
214 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
215 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
217 void setLinkage(LinkageTypes LT) { Linkage = LT; }
218 LinkageTypes getLinkage() const { return Linkage; }
220 bool isDiscardableIfUnused() const {
221 return isDiscardableIfUnused(Linkage);
224 bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
226 bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
228 /// copyAttributesFrom - copy all additional attributes (those not needed to
229 /// create a GlobalValue) from the GlobalValue Src to this one.
230 virtual void copyAttributesFrom(const GlobalValue *Src);
232 /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
233 /// printer to not emit usual symbol prefix before the symbol name is used
234 /// then return linkage name after skipping this special LLVM prefix.
235 static StringRef getRealLinkageName(StringRef Name) {
236 if (!Name.empty() && Name[0] == '\1')
237 return Name.substr(1);
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