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 LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
35 LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
36 WeakAnyLinkage, ///< Keep one copy of named function when linking (weak)
37 WeakODRLinkage, ///< Same, but only replaced by something equivalent.
38 AppendingLinkage, ///< Special purpose, only applies to global arrays
39 InternalLinkage, ///< Rename collisions when linking (static functions)
40 PrivateLinkage, ///< Like Internal, but omit from symbol table
41 DLLImportLinkage, ///< Function to be imported from DLL
42 DLLExportLinkage, ///< Function to be accessible from DLL
43 ExternalWeakAnyLinkage,///< ExternalWeak linkage description
44 ExternalWeakODRLinkage,///< Same, but only replaced by something equivalent.
45 GhostLinkage, ///< Stand-in functions for streaming fns from BC files
46 CommonAnyLinkage, ///< Tentative definitions
47 CommonODRLinkage ///< Same, but only replaced by something equivalent.
50 /// @brief An enumeration for the kinds of visibility of global values.
51 enum VisibilityTypes {
52 DefaultVisibility = 0, ///< The GV is visible
53 HiddenVisibility, ///< The GV is hidden
54 ProtectedVisibility ///< The GV is protected
58 GlobalValue(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
59 LinkageTypes linkage, const std::string &name = "")
60 : Constant(ty, vty, Ops, NumOps), Parent(0),
61 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
62 if (!name.empty()) setName(name);
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 std::string Section; // Section to emit this into, empty mean default
74 removeDeadConstantUsers(); // remove any dead constants using this.
77 unsigned getAlignment() const { return Alignment; }
78 void setAlignment(unsigned Align) {
79 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
83 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
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(const std::string &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;
112 static LinkageTypes getCommonLinkage(bool ODR) {
113 return ODR ? CommonODRLinkage : CommonAnyLinkage;
115 static LinkageTypes getExternalWeakLinkage(bool ODR) {
116 return ODR ? ExternalWeakODRLinkage : ExternalWeakAnyLinkage;
119 bool hasExternalLinkage() const { return Linkage == ExternalLinkage; }
120 bool hasLinkOnceLinkage() const {
121 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
123 bool hasWeakLinkage() const {
124 return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
126 bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
127 bool hasInternalLinkage() const { return Linkage == InternalLinkage; }
128 bool hasPrivateLinkage() const { return Linkage == PrivateLinkage; }
129 bool hasLocalLinkage() const {
130 return Linkage == InternalLinkage || Linkage == PrivateLinkage;
132 bool hasDLLImportLinkage() const { return Linkage == DLLImportLinkage; }
133 bool hasDLLExportLinkage() const { return Linkage == DLLExportLinkage; }
134 bool hasExternalWeakLinkage() const {
135 return Linkage == ExternalWeakAnyLinkage ||
136 Linkage == ExternalWeakODRLinkage;
138 bool hasGhostLinkage() const { return Linkage == GhostLinkage; }
139 bool hasCommonLinkage() const {
140 return Linkage == CommonAnyLinkage || Linkage == CommonODRLinkage;
143 void setLinkage(LinkageTypes LT) { Linkage = LT; }
144 LinkageTypes getLinkage() const { return Linkage; }
146 /// mayBeOverridden - Whether the definition of this global may be replaced
147 /// by something non-equivalent at link time. For example, if a function has
148 /// weak linkage then the code defining it may be replaced by different code.
149 bool mayBeOverridden() const {
150 return (Linkage == WeakAnyLinkage ||
151 Linkage == LinkOnceAnyLinkage ||
152 Linkage == CommonAnyLinkage ||
153 Linkage == ExternalWeakAnyLinkage);
156 /// isWeakForLinker - Whether the definition of this global may be replaced at
157 /// link time, whether the replacement is equivalent to the original or not.
158 bool isWeakForLinker() const {
159 return (Linkage == WeakAnyLinkage ||
160 Linkage == WeakODRLinkage ||
161 Linkage == LinkOnceAnyLinkage ||
162 Linkage == LinkOnceODRLinkage ||
163 Linkage == CommonAnyLinkage ||
164 Linkage == CommonODRLinkage ||
165 Linkage == ExternalWeakAnyLinkage ||
166 Linkage == ExternalWeakODRLinkage);
169 /// copyAttributesFrom - copy all additional attributes (those not needed to
170 /// create a GlobalValue) from the GlobalValue Src to this one.
171 virtual void copyAttributesFrom(const GlobalValue *Src);
173 /// hasNotBeenReadFromBitcode - If a module provider is being used to lazily
174 /// stream in functions from disk, this method can be used to check to see if
175 /// the function has been read in yet or not. Unless you are working on the
176 /// JIT or something else that streams stuff in lazily, you don't need to
177 /// worry about this.
178 bool hasNotBeenReadFromBitcode() const { return Linkage == GhostLinkage; }
180 /// Override from Constant class. No GlobalValue's are null values so this
181 /// always returns false.
182 virtual bool isNullValue() const { return false; }
184 /// Override from Constant class.
185 virtual void destroyConstant();
187 /// isDeclaration - Return true if the primary definition of this global
188 /// value is outside of the current translation unit...
189 virtual bool isDeclaration() const = 0;
191 /// removeFromParent - This method unlinks 'this' from the containing module,
192 /// but does not delete it.
193 virtual void removeFromParent() = 0;
195 /// eraseFromParent - This method unlinks 'this' from the containing module
197 virtual void eraseFromParent() = 0;
199 /// getParent - Get the module that this global value is contained inside
201 inline Module *getParent() { return Parent; }
202 inline const Module *getParent() const { return Parent; }
204 /// removeDeadConstantUsers - If there are any dead constant users dangling
205 /// off of this global value, remove them. This method is useful for clients
206 /// that want to check to see if a global is unused, but don't want to deal
207 /// with potentially dead constants hanging off of the globals.
208 void removeDeadConstantUsers();
210 // Methods for support type inquiry through isa, cast, and dyn_cast:
211 static inline bool classof(const GlobalValue *) { return true; }
212 static inline bool classof(const Value *V) {
213 return V->getValueID() == Value::FunctionVal ||
214 V->getValueID() == Value::GlobalVariableVal ||
215 V->getValueID() == Value::GlobalAliasVal;
219 } // End llvm namespace