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 LinkOnceLinkage, ///< Keep one copy of function when linking (inline)
35 WeakLinkage, ///< Keep one copy of named function when linking (weak)
36 AppendingLinkage, ///< Special purpose, only applies to global arrays
37 InternalLinkage, ///< Rename collisions when linking (static functions)
38 DLLImportLinkage, ///< Function to be imported from DLL
39 DLLExportLinkage, ///< Function to be accessible from DLL
40 ExternalWeakLinkage,///< ExternalWeak linkage description
41 GhostLinkage, ///< Stand-in functions for streaming fns from BC files
42 CommonLinkage ///< Tentative definitions
45 /// @brief An enumeration for the kinds of visibility of global values.
46 enum VisibilityTypes {
47 DefaultVisibility = 0, ///< The GV is visible
48 HiddenVisibility, ///< The GV is hidden
49 ProtectedVisibility ///< The GV is protected
53 GlobalValue(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
54 LinkageTypes linkage, const std::string &name = "")
55 : Constant(ty, vty, Ops, NumOps), Parent(0),
56 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
57 if (!name.empty()) setName(name);
61 // Note: VC++ treats enums as signed, so an extra bit is required to prevent
62 // Linkage and Visibility from turning into negative values.
63 LinkageTypes Linkage : 5; // The linkage of this global
64 unsigned Visibility : 2; // The visibility style of this global
65 unsigned Alignment : 16; // Alignment of this symbol, must be power of two
66 std::string Section; // Section to emit this into, empty mean default
69 removeDeadConstantUsers(); // remove any dead constants using this.
72 unsigned getAlignment() const { return Alignment; }
73 void setAlignment(unsigned Align) {
74 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
78 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
79 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
80 bool hasProtectedVisibility() const {
81 return Visibility == ProtectedVisibility;
83 void setVisibility(VisibilityTypes V) { Visibility = V; }
85 bool hasSection() const { return !Section.empty(); }
86 const std::string &getSection() const { return Section; }
87 void setSection(const std::string &S) { Section = S; }
89 /// If the usage is empty (except transitively dead constants), then this
90 /// global value can can be safely deleted since the destructor will
91 /// delete the dead constants as well.
92 /// @brief Determine if the usage of this global value is empty except
93 /// for transitively dead constants.
94 bool use_empty_except_constants();
96 /// getType - Global values are always pointers.
97 inline const PointerType *getType() const {
98 return reinterpret_cast<const PointerType*>(User::getType());
101 bool hasExternalLinkage() const { return Linkage == ExternalLinkage; }
102 bool hasLinkOnceLinkage() const { return Linkage == LinkOnceLinkage; }
103 bool hasWeakLinkage() const { return Linkage == WeakLinkage; }
104 bool hasCommonLinkage() const { return Linkage == CommonLinkage; }
105 bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
106 bool hasInternalLinkage() const { return Linkage == InternalLinkage; }
107 bool hasDLLImportLinkage() const { return Linkage == DLLImportLinkage; }
108 bool hasDLLExportLinkage() const { return Linkage == DLLExportLinkage; }
109 bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; }
110 void setLinkage(LinkageTypes LT) { Linkage = LT; }
111 LinkageTypes getLinkage() const { return Linkage; }
113 /// copyAttributesFrom - copy all additional attributes (those not needed to
114 /// create a GlobalValue) from the GlobalValue Src to this one.
115 virtual void copyAttributesFrom(const GlobalValue *Src);
117 /// hasNotBeenReadFromBitcode - If a module provider is being used to lazily
118 /// stream in functions from disk, this method can be used to check to see if
119 /// the function has been read in yet or not. Unless you are working on the
120 /// JIT or something else that streams stuff in lazily, you don't need to
121 /// worry about this.
122 bool hasNotBeenReadFromBitcode() const { return Linkage == GhostLinkage; }
124 /// Override from Constant class. No GlobalValue's are null values so this
125 /// always returns false.
126 virtual bool isNullValue() const { return false; }
128 /// Override from Constant class.
129 virtual void destroyConstant();
131 /// isDeclaration - Return true if the primary definition of this global
132 /// value is outside of the current translation unit...
133 virtual bool isDeclaration() const = 0;
135 /// getParent - Get the module that this global value is contained inside
137 inline Module *getParent() { return Parent; }
138 inline const Module *getParent() const { return Parent; }
140 /// removeDeadConstantUsers - If there are any dead constant users dangling
141 /// off of this global value, remove them. This method is useful for clients
142 /// that want to check to see if a global is unused, but don't want to deal
143 /// with potentially dead constants hanging off of the globals.
144 void removeDeadConstantUsers();
146 // Methods for support type inquiry through isa, cast, and dyn_cast:
147 static inline bool classof(const GlobalValue *) { return true; }
148 static inline bool classof(const Value *V) {
149 return V->getValueID() == Value::FunctionVal ||
150 V->getValueID() == Value::GlobalVariableVal ||
151 V->getValueID() == Value::GlobalAliasVal;
155 } // End llvm namespace