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
44 /// @brief An enumeration for the kinds of visibility of global values.
45 enum VisibilityTypes {
46 DefaultVisibility = 0, ///< The GV is visible
47 HiddenVisibility, ///< The GV is hidden
48 ProtectedVisibility ///< The GV is protected
52 GlobalValue(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
53 LinkageTypes linkage, const std::string &name = "")
54 : Constant(Ty, vty, Ops, NumOps), Parent(0),
55 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
56 if (!name.empty()) setName(name);
60 // Note: VC++ treats enums as signed, so an extra bit is required to prevent
61 // Linkage and Visibility from turning into negative values.
62 LinkageTypes Linkage : 5; // The linkage of this global
63 unsigned Visibility : 2; // The visibility style of this global
64 unsigned Alignment : 16; // Alignment of this symbol, must be power of two
65 std::string Section; // Section to emit this into, empty mean default
68 removeDeadConstantUsers(); // remove any dead constants using this.
71 unsigned getAlignment() const { return Alignment; }
72 void setAlignment(unsigned Align) {
73 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
77 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
78 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
79 bool hasProtectedVisibility() const {
80 return Visibility == ProtectedVisibility;
82 void setVisibility(VisibilityTypes V) { Visibility = V; }
84 bool hasSection() const { return !Section.empty(); }
85 const std::string &getSection() const { return Section; }
86 void setSection(const std::string &S) { Section = S; }
88 /// If the usage is empty (except transitively dead constants), then this
89 /// global value can can be safely deleted since the destructor will
90 /// delete the dead constants as well.
91 /// @brief Determine if the usage of this global value is empty except
92 /// for transitively dead constants.
93 bool use_empty_except_constants();
95 /// getType - Global values are always pointers.
96 inline const PointerType *getType() const {
97 return reinterpret_cast<const PointerType*>(User::getType());
100 bool hasExternalLinkage() const { return Linkage == ExternalLinkage; }
101 bool hasLinkOnceLinkage() const { return Linkage == LinkOnceLinkage; }
102 bool hasWeakLinkage() const { return Linkage == WeakLinkage; }
103 bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
104 bool hasInternalLinkage() const { return Linkage == InternalLinkage; }
105 bool hasDLLImportLinkage() const { return Linkage == DLLImportLinkage; }
106 bool hasDLLExportLinkage() const { return Linkage == DLLExportLinkage; }
107 bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; }
108 void setLinkage(LinkageTypes LT) { Linkage = LT; }
109 LinkageTypes getLinkage() const { return Linkage; }
111 /// hasNotBeenReadFromBitcode - If a module provider is being used to lazily
112 /// stream in functions from disk, this method can be used to check to see if
113 /// the function has been read in yet or not. Unless you are working on the
114 /// JIT or something else that streams stuff in lazily, you don't need to
115 /// worry about this.
116 bool hasNotBeenReadFromBitcode() const { return Linkage == GhostLinkage; }
118 /// Override from Constant class. No GlobalValue's are null values so this
119 /// always returns false.
120 virtual bool isNullValue() const { return false; }
122 /// Override from Constant class.
123 virtual void destroyConstant();
125 /// isDeclaration - Return true if the primary definition of this global
126 /// value is outside of the current translation unit...
127 virtual bool isDeclaration() const = 0;
129 /// getParent - Get the module that this global value is contained inside
131 inline Module *getParent() { return Parent; }
132 inline const Module *getParent() const { return Parent; }
134 /// removeDeadConstantUsers - If there are any dead constant users dangling
135 /// off of this global value, remove them. This method is useful for clients
136 /// that want to check to see if a global is unused, but don't want to deal
137 /// with potentially dead constants hanging off of the globals.
138 void removeDeadConstantUsers();
140 // Methods for support type inquiry through isa, cast, and dyn_cast:
141 static inline bool classof(const GlobalValue *) { return true; }
142 static inline bool classof(const Value *V) {
143 return V->getValueID() == Value::FunctionVal ||
144 V->getValueID() == Value::GlobalVariableVal ||
145 V->getValueID() == Value::GlobalAliasVal;
149 } // End llvm namespace