1 //===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 and by Function. This is used because you
12 // can do certain things with these global objects that you can't do to anything
13 // else. For example, use the address of one as a constant.
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_GLOBALVALUE_H
18 #define LLVM_GLOBALVALUE_H
20 #include "llvm/Constant.h"
27 class GlobalValue : public Constant {
28 GlobalValue(const GlobalValue &); // do not implement
30 /// @brief An enumeration for the kinds of linkage for global values.
32 ExternalLinkage, ///< Externally visible function
33 LinkOnceLinkage, ///< Keep one copy of function when linking (inline)
34 WeakLinkage, ///< Keep one copy of named function when linking (weak)
35 AppendingLinkage, ///< Special purpose, only applies to global arrays
36 InternalLinkage, ///< Rename collisions when linking (static functions)
37 DLLImportLinkage, ///< Function to be imported from DLL
38 DLLExportLinkage, ///< Function to be accessible from DLL
39 ExternalWeakLinkage,///< ExternalWeak linkage description
40 GhostLinkage ///< Stand-in functions for streaming fns from BC files
43 /// @brief An enumeration for the kinds of visibility of global values.
44 enum VisibilityTypes {
45 DefaultVisibility, ///< The GV is visible
46 HiddenVisibility ///< The GV is hidden
50 GlobalValue(const Type *Ty, ValueTy vty, Use *Ops, unsigned NumOps,
51 LinkageTypes linkage, const std::string &name = "")
52 : Constant(Ty, vty, Ops, NumOps), Parent(0),
53 Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
54 if (!name.empty()) setName(name);
58 LinkageTypes Linkage; // The linkage of this global
59 VisibilityTypes Visibility; // The visibility style of this global
60 unsigned Alignment; // Alignment of this symbol, must be power of two
61 std::string Section; // Section to emit this into, empty mean default
64 removeDeadConstantUsers(); // remove any dead constants using this.
67 unsigned getAlignment() const { return Alignment; }
68 void setAlignment(unsigned Align) {
69 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
73 VisibilityTypes getVisibility() const { return Visibility; }
74 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
75 void setVisibility(VisibilityTypes V) { Visibility = V; }
77 bool hasSection() const { return !Section.empty(); }
78 const std::string &getSection() const { return Section; }
79 void setSection(const std::string &S) { Section = S; }
81 /// If the usage is empty (except transitively dead constants), then this
82 /// global value can can be safely deleted since the destructor will
83 /// delete the dead constants as well.
84 /// @brief Determine if the usage of this global value is empty except
85 /// for transitively dead constants.
86 bool use_empty_except_constants();
88 /// getType - Global values are always pointers.
89 inline const PointerType *getType() const {
90 return reinterpret_cast<const PointerType*>(User::getType());
93 bool hasExternalLinkage() const { return Linkage == ExternalLinkage; }
94 bool hasLinkOnceLinkage() const { return Linkage == LinkOnceLinkage; }
95 bool hasWeakLinkage() const { return Linkage == WeakLinkage; }
96 bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
97 bool hasInternalLinkage() const { return Linkage == InternalLinkage; }
98 bool hasDLLImportLinkage() const { return Linkage == DLLImportLinkage; }
99 bool hasDLLExportLinkage() const { return Linkage == DLLExportLinkage; }
100 bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; }
101 void setLinkage(LinkageTypes LT) { Linkage = LT; }
102 LinkageTypes getLinkage() const { return Linkage; }
104 /// hasNotBeenReadFromBytecode - If a module provider is being used to lazily
105 /// stream in functions from disk, this method can be used to check to see if
106 /// the function has been read in yet or not. Unless you are working on the
107 /// JIT or something else that streams stuff in lazily, you don't need to
108 /// worry about this.
109 bool hasNotBeenReadFromBytecode() const { return Linkage == GhostLinkage; }
111 /// Override from Constant class. No GlobalValue's are null values so this
112 /// always returns false.
113 virtual bool isNullValue() const { return false; }
115 /// Override from Constant class.
116 virtual void destroyConstant();
118 /// isDeclaration - Return true if the primary definition of this global
119 /// value is outside of the current translation unit...
120 virtual bool isDeclaration() const = 0;
122 /// getParent - Get the module that this global value is contained inside
124 inline Module *getParent() { return Parent; }
125 inline const Module *getParent() const { return Parent; }
127 /// removeDeadConstantUsers - If there are any dead constant users dangling
128 /// off of this global value, remove them. This method is useful for clients
129 /// that want to check to see if a global is unused, but don't want to deal
130 /// with potentially dead constants hanging off of the globals.
132 /// This method tries to make the global dead. If it detects a user that
133 /// would prevent it from becoming completely dead, it gives up early,
134 /// potentially leaving some dead constant users around.
135 void removeDeadConstantUsers();
137 // Methods for support type inquiry through isa, cast, and dyn_cast:
138 static inline bool classof(const GlobalValue *) { return true; }
139 static inline bool classof(const Value *V) {
140 return V->getValueType() == Value::FunctionVal ||
141 V->getValueType() == Value::GlobalVariableVal;
145 } // End llvm namespace