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 DLLImportLinkage, ///< Function to be imported from DLL
44 DLLExportLinkage, ///< Function to be accessible from DLL.
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
57 GlobalValue(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
58 LinkageTypes linkage, const Twine &Name)
59 : Constant(ty, vty, Ops, NumOps), Parent(0),
60 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
65 // Note: VC++ treats enums as signed, so an extra bit is required to prevent
66 // Linkage and Visibility from turning into negative values.
67 LinkageTypes Linkage : 5; // The linkage of this global
68 unsigned Visibility : 2; // The visibility style of this global
69 unsigned Alignment : 16; // Alignment of this symbol, must be power of two
70 std::string Section; // Section to emit this into, empty mean default
73 removeDeadConstantUsers(); // remove any dead constants using this.
76 unsigned getAlignment() const { return Alignment; }
77 void setAlignment(unsigned Align) {
78 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
82 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
83 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
84 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
85 bool hasProtectedVisibility() const {
86 return Visibility == ProtectedVisibility;
88 void setVisibility(VisibilityTypes V) { Visibility = V; }
90 bool hasSection() const { return !Section.empty(); }
91 const std::string &getSection() const { return Section; }
92 void setSection(StringRef S) { Section = S; }
94 /// If the usage is empty (except transitively dead constants), then this
95 /// global value can can be safely deleted since the destructor will
96 /// delete the dead constants as well.
97 /// @brief Determine if the usage of this global value is empty except
98 /// for transitively dead constants.
99 bool use_empty_except_constants();
101 /// getType - Global values are always pointers.
102 inline const PointerType *getType() const {
103 return reinterpret_cast<const PointerType*>(User::getType());
106 static LinkageTypes getLinkOnceLinkage(bool ODR) {
107 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
109 static LinkageTypes getWeakLinkage(bool ODR) {
110 return ODR ? WeakODRLinkage : WeakAnyLinkage;
113 bool hasExternalLinkage() const { return Linkage == ExternalLinkage; }
114 bool hasAvailableExternallyLinkage() const {
115 return Linkage == AvailableExternallyLinkage;
117 bool hasLinkOnceLinkage() const {
118 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
120 bool hasWeakLinkage() const {
121 return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
123 bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
124 bool hasInternalLinkage() const { return Linkage == InternalLinkage; }
125 bool hasPrivateLinkage() const { return Linkage == PrivateLinkage; }
126 bool hasLinkerPrivateLinkage() const { return Linkage==LinkerPrivateLinkage; }
127 bool hasLocalLinkage() const {
128 return hasInternalLinkage() || hasPrivateLinkage() ||
129 hasLinkerPrivateLinkage();
131 bool hasDLLImportLinkage() const { return Linkage == DLLImportLinkage; }
132 bool hasDLLExportLinkage() const { return Linkage == DLLExportLinkage; }
133 bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; }
134 bool hasCommonLinkage() const { return Linkage == CommonLinkage; }
136 void setLinkage(LinkageTypes LT) { Linkage = LT; }
137 LinkageTypes getLinkage() const { return Linkage; }
139 /// mayBeOverridden - Whether the definition of this global may be replaced
140 /// by something non-equivalent at link time. For example, if a function has
141 /// weak linkage then the code defining it may be replaced by different code.
142 bool mayBeOverridden() const {
143 return (Linkage == WeakAnyLinkage ||
144 Linkage == LinkOnceAnyLinkage ||
145 Linkage == CommonLinkage ||
146 Linkage == ExternalWeakLinkage);
149 /// isWeakForLinker - Whether the definition of this global may be replaced at
151 bool isWeakForLinker() const {
152 return (Linkage == AvailableExternallyLinkage ||
153 Linkage == WeakAnyLinkage ||
154 Linkage == WeakODRLinkage ||
155 Linkage == LinkOnceAnyLinkage ||
156 Linkage == LinkOnceODRLinkage ||
157 Linkage == CommonLinkage ||
158 Linkage == ExternalWeakLinkage);
161 /// copyAttributesFrom - copy all additional attributes (those not needed to
162 /// create a GlobalValue) from the GlobalValue Src to this one.
163 virtual void copyAttributesFrom(const GlobalValue *Src);
165 /// @name Materialization
166 /// Materialization is used to construct functions only as they're needed. This
167 /// is useful to reduce memory usage in LLVM or parsing work done by the
168 /// BitcodeReader to load the Module.
171 /// isMaterializable - If this function's Module is being lazily streamed in
172 /// functions from disk or some other source, this method can be used to check
173 /// to see if the function has been read in yet or not.
174 bool isMaterializable() const;
176 /// isDematerializable - Returns true if this function was loaded from a
177 /// GVMaterializer that's still attached to its Module and that knows how to
178 /// dematerialize the function.
179 bool isDematerializable() const;
181 /// Materialize - make sure this GlobalValue is fully read. If the module is
182 /// corrupt, this returns true and fills in the optional string with
183 /// information about the problem. If successful, this returns false.
184 bool Materialize(std::string *ErrInfo = 0);
186 /// Dematerialize - If this GlobalValue is read in, and if the GVMaterializer
187 /// supports it, release the memory for the function, and set it up to be
188 /// materialized lazily. If !isDematerializable(), this method is a noop.
189 void Dematerialize();
193 /// Override from Constant class. No GlobalValue's are null values so this
194 /// always returns false.
195 virtual bool isNullValue() const { return false; }
197 /// Override from Constant class.
198 virtual void destroyConstant();
200 /// isDeclaration - Return true if the primary definition of this global
201 /// value is outside of the current translation unit...
202 virtual bool isDeclaration() const = 0;
204 /// removeFromParent - This method unlinks 'this' from the containing module,
205 /// but does not delete it.
206 virtual void removeFromParent() = 0;
208 /// eraseFromParent - This method unlinks 'this' from the containing module
210 virtual void eraseFromParent() = 0;
212 /// getParent - Get the module that this global value is contained inside
214 inline Module *getParent() { return Parent; }
215 inline const Module *getParent() const { return Parent; }
217 /// removeDeadConstantUsers - If there are any dead constant users dangling
218 /// off of this global value, remove them. This method is useful for clients
219 /// that want to check to see if a global is unused, but don't want to deal
220 /// with potentially dead constants hanging off of the globals.
221 void removeDeadConstantUsers() const;
223 // Methods for support type inquiry through isa, cast, and dyn_cast:
224 static inline bool classof(const GlobalValue *) { return true; }
225 static inline bool classof(const Value *V) {
226 return V->getValueID() == Value::FunctionVal ||
227 V->getValueID() == Value::GlobalVariableVal ||
228 V->getValueID() == Value::GlobalAliasVal;
232 } // End llvm namespace