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"
23 #include <system_error>
35 class GlobalValue : public Constant {
36 GlobalValue(const GlobalValue &) = delete;
38 /// @brief An enumeration for the kinds of linkage for global values.
40 ExternalLinkage = 0,///< Externally visible function
41 AvailableExternallyLinkage, ///< Available for inspection, not emission.
42 LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
43 LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
44 WeakAnyLinkage, ///< Keep one copy of named function when linking (weak)
45 WeakODRLinkage, ///< Same, but only replaced by something equivalent.
46 AppendingLinkage, ///< Special purpose, only applies to global arrays
47 InternalLinkage, ///< Rename collisions when linking (static functions).
48 PrivateLinkage, ///< Like Internal, but omit from symbol table.
49 ExternalWeakLinkage,///< ExternalWeak linkage description.
50 CommonLinkage ///< Tentative definitions.
53 /// @brief An enumeration for the kinds of visibility of global values.
54 enum VisibilityTypes {
55 DefaultVisibility = 0, ///< The GV is visible
56 HiddenVisibility, ///< The GV is hidden
57 ProtectedVisibility ///< The GV is protected
60 /// @brief Storage classes of global values for PE targets.
61 enum DLLStorageClassTypes {
62 DefaultStorageClass = 0,
63 DLLImportStorageClass = 1, ///< Function to be imported from DLL
64 DLLExportStorageClass = 2 ///< Function to be accessible from DLL.
68 GlobalValue(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
69 LinkageTypes Linkage, const Twine &Name, unsigned AddressSpace)
70 : Constant(PointerType::get(Ty, AddressSpace), VTy, Ops, NumOps),
71 ValueType(Ty), Linkage(Linkage), Visibility(DefaultVisibility),
72 UnnamedAddr(0), DllStorageClass(DefaultStorageClass),
73 ThreadLocal(NotThreadLocal), IntID((Intrinsic::ID)0U), Parent(nullptr) {
78 // Note: VC++ treats enums as signed, so an extra bit is required to prevent
79 // Linkage and Visibility from turning into negative values.
80 LinkageTypes Linkage : 5; // The linkage of this global
81 unsigned Visibility : 2; // The visibility style of this global
82 unsigned UnnamedAddr : 1; // This value's address is not significant
83 unsigned DllStorageClass : 2; // DLL storage class
85 unsigned ThreadLocal : 3; // Is this symbol "Thread Local", if so, what is
87 static const unsigned GlobalValueSubClassDataBits = 19;
90 // Give subclasses access to what otherwise would be wasted padding.
91 // (19 + 3 + 2 + 1 + 2 + 5) == 32.
92 unsigned SubClassData : GlobalValueSubClassDataBits;
94 friend class Constant;
95 void destroyConstantImpl();
96 Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
99 /// \brief The intrinsic ID for this subclass (which must be a Function).
101 /// This member is defined by this class, but not used for anything.
102 /// Subclasses can use it to store their intrinsic ID, if they have one.
104 /// This is stored here to save space in Function on 64-bit hosts.
107 unsigned getGlobalValueSubClassData() const {
110 void setGlobalValueSubClassData(unsigned V) {
111 assert(V < (1 << GlobalValueSubClassDataBits) && "It will not fit");
115 Module *Parent; // The containing module.
117 enum ThreadLocalMode {
119 GeneralDynamicTLSModel,
120 LocalDynamicTLSModel,
125 ~GlobalValue() override {
126 removeDeadConstantUsers(); // remove any dead constants using this.
129 unsigned getAlignment() const;
131 bool hasUnnamedAddr() const { return UnnamedAddr; }
132 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
134 bool hasComdat() const { return getComdat() != nullptr; }
136 const Comdat *getComdat() const {
137 return const_cast<GlobalValue *>(this)->getComdat();
140 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
141 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
142 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
143 bool hasProtectedVisibility() const {
144 return Visibility == ProtectedVisibility;
146 void setVisibility(VisibilityTypes V) {
147 assert((!hasLocalLinkage() || V == DefaultVisibility) &&
148 "local linkage requires default visibility");
152 /// If the value is "Thread Local", its value isn't shared by the threads.
153 bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal; }
154 void setThreadLocal(bool Val) {
155 setThreadLocalMode(Val ? GeneralDynamicTLSModel : NotThreadLocal);
157 void setThreadLocalMode(ThreadLocalMode Val) {
158 assert(Val == NotThreadLocal || getValueID() != Value::FunctionVal);
161 ThreadLocalMode getThreadLocalMode() const {
162 return static_cast<ThreadLocalMode>(ThreadLocal);
165 DLLStorageClassTypes getDLLStorageClass() const {
166 return DLLStorageClassTypes(DllStorageClass);
168 bool hasDLLImportStorageClass() const {
169 return DllStorageClass == DLLImportStorageClass;
171 bool hasDLLExportStorageClass() const {
172 return DllStorageClass == DLLExportStorageClass;
174 void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; }
176 bool hasSection() const { return !StringRef(getSection()).empty(); }
177 // It is unfortunate that we have to use "char *" in here since this is
178 // always non NULL, but:
179 // * The C API expects a null terminated string, so we cannot use StringRef.
180 // * The C API expects us to own it, so we cannot use a std:string.
181 // * For GlobalAliases we can fail to find the section and we have to
182 // return "", so we cannot use a "const std::string &".
183 const char *getSection() const;
185 /// Global values are always pointers.
186 PointerType *getType() const { return cast<PointerType>(User::getType()); }
188 Type *getValueType() const { return ValueType; }
190 static LinkageTypes getLinkOnceLinkage(bool ODR) {
191 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
193 static LinkageTypes getWeakLinkage(bool ODR) {
194 return ODR ? WeakODRLinkage : WeakAnyLinkage;
197 static bool isExternalLinkage(LinkageTypes Linkage) {
198 return Linkage == ExternalLinkage;
200 static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
201 return Linkage == AvailableExternallyLinkage;
203 static bool isLinkOnceODRLinkage(LinkageTypes Linkage) {
204 return Linkage == LinkOnceODRLinkage;
206 static bool isLinkOnceLinkage(LinkageTypes Linkage) {
207 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
209 static bool isWeakAnyLinkage(LinkageTypes Linkage) {
210 return Linkage == WeakAnyLinkage;
212 static bool isWeakODRLinkage(LinkageTypes Linkage) {
213 return Linkage == WeakODRLinkage;
215 static bool isWeakLinkage(LinkageTypes Linkage) {
216 return isWeakAnyLinkage(Linkage) || isWeakODRLinkage(Linkage);
218 static bool isAppendingLinkage(LinkageTypes Linkage) {
219 return Linkage == AppendingLinkage;
221 static bool isInternalLinkage(LinkageTypes Linkage) {
222 return Linkage == InternalLinkage;
224 static bool isPrivateLinkage(LinkageTypes Linkage) {
225 return Linkage == PrivateLinkage;
227 static bool isLocalLinkage(LinkageTypes Linkage) {
228 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage);
230 static bool isExternalWeakLinkage(LinkageTypes Linkage) {
231 return Linkage == ExternalWeakLinkage;
233 static bool isCommonLinkage(LinkageTypes Linkage) {
234 return Linkage == CommonLinkage;
237 /// Whether the definition of this global may be discarded if it is not used
238 /// in its compilation unit.
239 static bool isDiscardableIfUnused(LinkageTypes Linkage) {
240 return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage) ||
241 isAvailableExternallyLinkage(Linkage);
244 /// Whether the definition of this global may be replaced by something
245 /// non-equivalent at link time. For example, if a function has weak linkage
246 /// then the code defining it may be replaced by different code.
247 static bool mayBeOverridden(LinkageTypes Linkage) {
248 return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage ||
249 Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
252 /// Whether the definition of this global may be replaced at link time. NB:
253 /// Using this method outside of the code generators is almost always a
254 /// mistake: when working at the IR level use mayBeOverridden instead as it
255 /// knows about ODR semantics.
256 static bool isWeakForLinker(LinkageTypes Linkage) {
257 return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage ||
258 Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage ||
259 Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
262 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
263 bool hasAvailableExternallyLinkage() const {
264 return isAvailableExternallyLinkage(Linkage);
266 bool hasLinkOnceLinkage() const {
267 return isLinkOnceLinkage(Linkage);
269 bool hasLinkOnceODRLinkage() const { return isLinkOnceODRLinkage(Linkage); }
270 bool hasWeakLinkage() const {
271 return isWeakLinkage(Linkage);
273 bool hasWeakAnyLinkage() const {
274 return isWeakAnyLinkage(Linkage);
276 bool hasWeakODRLinkage() const {
277 return isWeakODRLinkage(Linkage);
279 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
280 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
281 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
282 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
283 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
284 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
286 void setLinkage(LinkageTypes LT) {
287 if (isLocalLinkage(LT))
288 Visibility = DefaultVisibility;
291 LinkageTypes getLinkage() const { return Linkage; }
293 bool isDiscardableIfUnused() const {
294 return isDiscardableIfUnused(Linkage);
297 bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
299 bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
301 /// Copy all additional attributes (those not needed to create a GlobalValue)
302 /// from the GlobalValue Src to this one.
303 virtual void copyAttributesFrom(const GlobalValue *Src);
305 /// If special LLVM prefix that is used to inform the asm printer to not emit
306 /// usual symbol prefix before the symbol name is used then return linkage
307 /// name after skipping this special LLVM prefix.
308 static StringRef getRealLinkageName(StringRef Name) {
309 if (!Name.empty() && Name[0] == '\1')
310 return Name.substr(1);
314 /// @name Materialization
315 /// Materialization is used to construct functions only as they're needed. This
316 /// is useful to reduce memory usage in LLVM or parsing work done by the
317 /// BitcodeReader to load the Module.
320 /// If this function's Module is being lazily streamed in functions from disk
321 /// or some other source, this method can be used to check to see if the
322 /// function has been read in yet or not.
323 bool isMaterializable() const;
325 /// Returns true if this function was loaded from a GVMaterializer that's
326 /// still attached to its Module and that knows how to dematerialize the
328 bool isDematerializable() const;
330 /// Make sure this GlobalValue is fully read. If the module is corrupt, this
331 /// returns true and fills in the optional string with information about the
332 /// problem. If successful, this returns false.
333 std::error_code materialize();
335 /// If this GlobalValue is read in, and if the GVMaterializer supports it,
336 /// release the memory for the function, and set it up to be materialized
337 /// lazily. If !isDematerializable(), this method is a noop.
338 void dematerialize();
342 /// Return true if the primary definition of this global value is outside of
343 /// the current translation unit.
344 bool isDeclaration() const;
346 bool isDeclarationForLinker() const {
347 if (hasAvailableExternallyLinkage())
350 return isDeclaration();
353 /// Returns true if this global's definition will be the one chosen by the
355 bool isStrongDefinitionForLinker() const {
356 return !(isDeclarationForLinker() || isWeakForLinker());
359 /// This method unlinks 'this' from the containing module, but does not delete
361 virtual void removeFromParent() = 0;
363 /// This method unlinks 'this' from the containing module and deletes it.
364 virtual void eraseFromParent() = 0;
366 /// Get the module that this global value is contained inside of...
367 Module *getParent() { return Parent; }
368 const Module *getParent() const { return Parent; }
370 // Methods for support type inquiry through isa, cast, and dyn_cast:
371 static bool classof(const Value *V) {
372 return V->getValueID() == Value::FunctionVal ||
373 V->getValueID() == Value::GlobalVariableVal ||
374 V->getValueID() == Value::GlobalAliasVal;
378 } // End llvm namespace