GlobalValue.h revision 263508
1//===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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
14// constant.
15//
16//===----------------------------------------------------------------------===//
17
18#ifndef LLVM_IR_GLOBALVALUE_H
19#define LLVM_IR_GLOBALVALUE_H
20
21#include "llvm/IR/Constant.h"
22#include "llvm/IR/DerivedTypes.h"
23
24namespace llvm {
25
26class PointerType;
27class Module;
28
29class GlobalValue : public Constant {
30  GlobalValue(const GlobalValue &) LLVM_DELETED_FUNCTION;
31public:
32  /// @brief An enumeration for the kinds of linkage for global values.
33  enum LinkageTypes {
34    ExternalLinkage = 0,///< Externally visible function
35    AvailableExternallyLinkage, ///< Available for inspection, not emission.
36    LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
37    LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
38    WeakAnyLinkage,     ///< Keep one copy of named function when linking (weak)
39    WeakODRLinkage,     ///< Same, but only replaced by something equivalent.
40    AppendingLinkage,   ///< Special purpose, only applies to global arrays
41    InternalLinkage,    ///< Rename collisions when linking (static functions).
42    PrivateLinkage,     ///< Like Internal, but omit from symbol table.
43    LinkerPrivateLinkage, ///< Like Private, but linker removes.
44    LinkerPrivateWeakLinkage, ///< Like LinkerPrivate, but weak.
45    DLLImportLinkage,   ///< Function to be imported from DLL
46    DLLExportLinkage,   ///< Function to be accessible from DLL.
47    ExternalWeakLinkage,///< ExternalWeak linkage description.
48    CommonLinkage       ///< Tentative definitions.
49  };
50
51  /// @brief An enumeration for the kinds of visibility of global values.
52  enum VisibilityTypes {
53    DefaultVisibility = 0,  ///< The GV is visible
54    HiddenVisibility,       ///< The GV is hidden
55    ProtectedVisibility     ///< The GV is protected
56  };
57
58protected:
59  GlobalValue(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
60              LinkageTypes linkage, const Twine &Name)
61    : Constant(ty, vty, Ops, NumOps), Linkage(linkage),
62      Visibility(DefaultVisibility), Alignment(0), UnnamedAddr(0), Parent(0) {
63    setName(Name);
64  }
65
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  unsigned UnnamedAddr : 1;   // This value's address is not significant
72  Module *Parent;             // The containing module.
73  std::string Section;        // Section to emit this into, empty mean default
74public:
75  ~GlobalValue() {
76    removeDeadConstantUsers();   // remove any dead constants using this.
77  }
78
79  unsigned getAlignment() const {
80    return (1u << Alignment) >> 1;
81  }
82  void setAlignment(unsigned Align);
83
84  bool hasUnnamedAddr() const { return UnnamedAddr; }
85  void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
86
87  VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
88  bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; }
89  bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
90  bool hasProtectedVisibility() const {
91    return Visibility == ProtectedVisibility;
92  }
93  void setVisibility(VisibilityTypes V) { Visibility = V; }
94
95  bool hasSection() const { return !Section.empty(); }
96  const std::string &getSection() const { return Section; }
97  void setSection(StringRef S) { Section = S; }
98
99  /// If the usage is empty (except transitively dead constants), then this
100  /// global value can be safely deleted since the destructor will
101  /// delete the dead constants as well.
102  /// @brief Determine if the usage of this global value is empty except
103  /// for transitively dead constants.
104  bool use_empty_except_constants();
105
106  /// getType - Global values are always pointers.
107  inline PointerType *getType() const {
108    return cast<PointerType>(User::getType());
109  }
110
111  static LinkageTypes getLinkOnceLinkage(bool ODR) {
112    return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
113  }
114  static LinkageTypes getWeakLinkage(bool ODR) {
115    return ODR ? WeakODRLinkage : WeakAnyLinkage;
116  }
117
118  static bool isExternalLinkage(LinkageTypes Linkage) {
119    return Linkage == ExternalLinkage;
120  }
121  static bool isAvailableExternallyLinkage(LinkageTypes Linkage) {
122    return Linkage == AvailableExternallyLinkage;
123  }
124  static bool isLinkOnceLinkage(LinkageTypes Linkage) {
125    return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
126  }
127  static bool isWeakLinkage(LinkageTypes Linkage) {
128    return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
129  }
130  static bool isAppendingLinkage(LinkageTypes Linkage) {
131    return Linkage == AppendingLinkage;
132  }
133  static bool isInternalLinkage(LinkageTypes Linkage) {
134    return Linkage == InternalLinkage;
135  }
136  static bool isPrivateLinkage(LinkageTypes Linkage) {
137    return Linkage == PrivateLinkage;
138  }
139  static bool isLinkerPrivateLinkage(LinkageTypes Linkage) {
140    return Linkage == LinkerPrivateLinkage;
141  }
142  static bool isLinkerPrivateWeakLinkage(LinkageTypes Linkage) {
143    return Linkage == LinkerPrivateWeakLinkage;
144  }
145  static bool isLocalLinkage(LinkageTypes Linkage) {
146    return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage) ||
147      isLinkerPrivateLinkage(Linkage) || isLinkerPrivateWeakLinkage(Linkage);
148  }
149  static bool isDLLImportLinkage(LinkageTypes Linkage) {
150    return Linkage == DLLImportLinkage;
151  }
152  static bool isDLLExportLinkage(LinkageTypes Linkage) {
153    return Linkage == DLLExportLinkage;
154  }
155  static bool isExternalWeakLinkage(LinkageTypes Linkage) {
156    return Linkage == ExternalWeakLinkage;
157  }
158  static bool isCommonLinkage(LinkageTypes Linkage) {
159    return Linkage == CommonLinkage;
160  }
161
162  /// isDiscardableIfUnused - Whether the definition of this global may be
163  /// discarded if it is not used in its compilation unit.
164  static bool isDiscardableIfUnused(LinkageTypes Linkage) {
165    return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
166  }
167
168  /// mayBeOverridden - Whether the definition of this global may be replaced
169  /// by something non-equivalent at link time.  For example, if a function has
170  /// weak linkage then the code defining it may be replaced by different code.
171  static bool mayBeOverridden(LinkageTypes Linkage) {
172    return Linkage == WeakAnyLinkage ||
173           Linkage == LinkOnceAnyLinkage ||
174           Linkage == CommonLinkage ||
175           Linkage == ExternalWeakLinkage ||
176           Linkage == LinkerPrivateWeakLinkage;
177  }
178
179  /// isWeakForLinker - Whether the definition of this global may be replaced at
180  /// link time.  NB: Using this method outside of the code generators is almost
181  /// always a mistake: when working at the IR level use mayBeOverridden instead
182  /// as it knows about ODR semantics.
183  static bool isWeakForLinker(LinkageTypes Linkage)  {
184    return Linkage == AvailableExternallyLinkage ||
185           Linkage == WeakAnyLinkage ||
186           Linkage == WeakODRLinkage ||
187           Linkage == LinkOnceAnyLinkage ||
188           Linkage == LinkOnceODRLinkage ||
189           Linkage == CommonLinkage ||
190           Linkage == ExternalWeakLinkage ||
191           Linkage == LinkerPrivateWeakLinkage;
192  }
193
194  bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
195  bool hasAvailableExternallyLinkage() const {
196    return isAvailableExternallyLinkage(Linkage);
197  }
198  bool hasLinkOnceLinkage() const {
199    return isLinkOnceLinkage(Linkage);
200  }
201  bool hasWeakLinkage() const {
202    return isWeakLinkage(Linkage);
203  }
204  bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); }
205  bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
206  bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
207  bool hasLinkerPrivateLinkage() const { return isLinkerPrivateLinkage(Linkage); }
208  bool hasLinkerPrivateWeakLinkage() const {
209    return isLinkerPrivateWeakLinkage(Linkage);
210  }
211  bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
212  bool hasDLLImportLinkage() const { return isDLLImportLinkage(Linkage); }
213  bool hasDLLExportLinkage() const { return isDLLExportLinkage(Linkage); }
214  bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); }
215  bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
216
217  void setLinkage(LinkageTypes LT) { Linkage = LT; }
218  LinkageTypes getLinkage() const { return Linkage; }
219
220  bool isDiscardableIfUnused() const {
221    return isDiscardableIfUnused(Linkage);
222  }
223
224  bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
225
226  bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
227
228  /// copyAttributesFrom - copy all additional attributes (those not needed to
229  /// create a GlobalValue) from the GlobalValue Src to this one.
230  virtual void copyAttributesFrom(const GlobalValue *Src);
231
232  /// getRealLinkageName - If special LLVM prefix that is used to inform the asm
233  /// printer to not emit usual symbol prefix before the symbol name is used
234  /// then return linkage name after skipping this special LLVM prefix.
235  static StringRef getRealLinkageName(StringRef Name) {
236    if (!Name.empty() && Name[0] == '\1')
237      return Name.substr(1);
238    return Name;
239  }
240
241/// @name Materialization
242/// Materialization is used to construct functions only as they're needed. This
243/// is useful to reduce memory usage in LLVM or parsing work done by the
244/// BitcodeReader to load the Module.
245/// @{
246
247  /// isMaterializable - If this function's Module is being lazily streamed in
248  /// functions from disk or some other source, this method can be used to check
249  /// to see if the function has been read in yet or not.
250  bool isMaterializable() const;
251
252  /// isDematerializable - Returns true if this function was loaded from a
253  /// GVMaterializer that's still attached to its Module and that knows how to
254  /// dematerialize the function.
255  bool isDematerializable() const;
256
257  /// Materialize - make sure this GlobalValue is fully read.  If the module is
258  /// corrupt, this returns true and fills in the optional string with
259  /// information about the problem.  If successful, this returns false.
260  bool Materialize(std::string *ErrInfo = 0);
261
262  /// Dematerialize - If this GlobalValue is read in, and if the GVMaterializer
263  /// supports it, release the memory for the function, and set it up to be
264  /// materialized lazily.  If !isDematerializable(), this method is a noop.
265  void Dematerialize();
266
267/// @}
268
269  /// Override from Constant class.
270  virtual void destroyConstant();
271
272  /// isDeclaration - Return true if the primary definition of this global
273  /// value is outside of the current translation unit.
274  bool isDeclaration() const;
275
276  /// removeFromParent - This method unlinks 'this' from the containing module,
277  /// but does not delete it.
278  virtual void removeFromParent() = 0;
279
280  /// eraseFromParent - This method unlinks 'this' from the containing module
281  /// and deletes it.
282  virtual void eraseFromParent() = 0;
283
284  /// getParent - Get the module that this global value is contained inside
285  /// of...
286  inline Module *getParent() { return Parent; }
287  inline const Module *getParent() const { return Parent; }
288
289  // Methods for support type inquiry through isa, cast, and dyn_cast:
290  static inline bool classof(const Value *V) {
291    return V->getValueID() == Value::FunctionVal ||
292           V->getValueID() == Value::GlobalVariableVal ||
293           V->getValueID() == Value::GlobalAliasVal;
294  }
295};
296
297} // End llvm namespace
298
299#endif
300