LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
GlobalValue.h
Go to the documentation of this file.
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 
24 namespace llvm {
25 
26 class PointerType;
27 class Module;
28 
29 class GlobalValue : public Constant {
31 public:
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.
53  DefaultVisibility = 0, ///< The GV is visible
54  HiddenVisibility, ///< The GV is hidden
55  ProtectedVisibility ///< The GV is protected
56  };
57 
58 protected:
59  GlobalValue(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
60  LinkageTypes linkage, const Twine &Name)
61  : Constant(ty, vty, Ops, NumOps), Linkage(linkage),
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
74 public:
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 
89  bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
90  bool hasProtectedVisibility() const {
92  }
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.
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 
119  return Linkage == ExternalLinkage;
120  }
122  return Linkage == AvailableExternallyLinkage;
123  }
125  return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
126  }
128  return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
129  }
131  return Linkage == AppendingLinkage;
132  }
134  return Linkage == InternalLinkage;
135  }
137  return Linkage == PrivateLinkage;
138  }
140  return Linkage == LinkerPrivateLinkage;
141  }
143  return Linkage == LinkerPrivateWeakLinkage;
144  }
146  return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage) ||
148  }
150  return Linkage == DLLImportLinkage;
151  }
153  return Linkage == DLLExportLinkage;
154  }
156  return Linkage == ExternalWeakLinkage;
157  }
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.
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.
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.
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); }
197  }
198  bool hasLinkOnceLinkage() const {
199  return isLinkOnceLinkage(Linkage);
200  }
201  bool hasWeakLinkage() const {
202  return isWeakLinkage(Linkage);
203  }
205  bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
206  bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
210  }
211  bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
215  bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
216 
218  LinkageTypes getLinkage() const { return Linkage; }
219 
220  bool isDiscardableIfUnused() const {
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.
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 ||
294  }
295 };
296 
297 } // End llvm namespace
298 
299 #endif
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:93
LinkageTypes getLinkage() const
Definition: GlobalValue.h:218
Like Private, but linker removes.
Definition: GlobalValue.h:43
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:40
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:87
virtual void copyAttributesFrom(const GlobalValue *Src)
Definition: Globals.cpp:51
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
unsigned getAlignment() const
Definition: GlobalValue.h:79
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:39
static bool isExternalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:118
Available for inspection, not emission.
Definition: GlobalValue.h:35
bool use_empty_except_constants()
Determine if the usage of this global value is empty except for transitively dead constants...
bool hasAppendingLinkage() const
Definition: GlobalValue.h:204
StringRef substr(size_t Start, size_t N=npos) const
Definition: StringRef.h:392
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:42
Externally visible function.
Definition: GlobalValue.h:34
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:195
static bool isCommonLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:158
void setSection(StringRef S)
Definition: GlobalValue.h:97
void Dematerialize()
Definition: Globals.cpp:39
Tentative definitions.
Definition: GlobalValue.h:48
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:145
static bool isLinkOnceLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:124
bool hasCommonLinkage() const
Definition: GlobalValue.h:215
bool hasDefaultVisibility() const
Definition: GlobalValue.h:88
bool hasSection() const
Definition: GlobalValue.h:95
Definition: Use.h:60
bool hasLinkerPrivateWeakLinkage() const
Definition: GlobalValue.h:208
bool hasInternalLinkage() const
Definition: GlobalValue.h:205
void setName(const Twine &Name)
Definition: Value.cpp:175
bool hasPrivateLinkage() const
Definition: GlobalValue.h:206
static bool isPrivateLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:136
LinkageTypes Linkage
Definition: GlobalValue.h:68
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:52
Function to be imported from DLL.
Definition: GlobalValue.h:45
bool isMaterializable() const
Definition: Globals.cpp:30
static bool isAppendingLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:130
bool isDiscardableIfUnused() const
Definition: GlobalValue.h:220
bool isWeakForLinker() const
Definition: GlobalValue.h:226
static bool isWeakForLinker(LinkageTypes Linkage)
Definition: GlobalValue.h:183
ExternalWeak linkage description.
Definition: GlobalValue.h:47
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:37
static bool isInternalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:133
unsigned Alignment
Definition: GlobalValue.h:70
static bool mayBeOverridden(LinkageTypes Linkage)
Definition: GlobalValue.h:171
static bool classof(const Value *V)
Definition: GlobalValue.h:290
LLVM Constant Representation.
Definition: Constant.h:41
bool hasHiddenVisibility() const
Definition: GlobalValue.h:89
bool hasDLLExportLinkage() const
Definition: GlobalValue.h:213
bool isDematerializable() const
Definition: Globals.cpp:33
unsigned getValueID() const
Definition: Value.h:233
bool hasLinkerPrivateLinkage() const
Definition: GlobalValue.h:207
static bool isDiscardableIfUnused(LinkageTypes Linkage)
Definition: GlobalValue.h:164
const Module * getParent() const
Definition: GlobalValue.h:287
Function to be accessible from DLL.
Definition: GlobalValue.h:46
const std::string & getSection() const
Definition: GlobalValue.h:96
bool hasWeakLinkage() const
Definition: GlobalValue.h:201
static LinkageTypes getWeakLinkage(bool ODR)
Definition: GlobalValue.h:114
bool hasExternalWeakLinkage() const
Definition: GlobalValue.h:214
bool hasExternalLinkage() const
Definition: GlobalValue.h:194
virtual void removeFromParent()=0
static bool isWeakLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:127
void setAlignment(unsigned Align)
Definition: Globals.cpp:58
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:36
Type * getType() const
Definition: Value.h:111
void setUnnamedAddr(bool Val)
Definition: GlobalValue.h:85
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:121
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:217
static bool isExternalWeakLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:155
#define LLVM_DELETED_FUNCTION
Definition: Compiler.h:137
static bool isLinkerPrivateWeakLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:142
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:33
unsigned UnnamedAddr
Definition: GlobalValue.h:71
static cl::opt< AlignMode > Align(cl::desc("Load/store alignment support"), cl::Hidden, cl::init(DefaultAlign), cl::values(clEnumValN(DefaultAlign,"arm-default-align","Generate unaligned accesses only on hardware/OS ""combinations that are known to support them"), clEnumValN(StrictAlign,"arm-strict-align","Disallow all unaligned memory accesses"), clEnumValN(NoStrictAlign,"arm-no-strict-align","Allow unaligned memory accesses"), clEnumValEnd))
Like LinkerPrivate, but weak.
Definition: GlobalValue.h:44
static bool isDLLExportLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:152
bool hasLinkOnceLinkage() const
Definition: GlobalValue.h:198
virtual void destroyConstant()
Override from Constant class.
Definition: Globals.cpp:45
static StringRef getRealLinkageName(StringRef Name)
Definition: GlobalValue.h:235
PointerType * getType() const
getType - Global values are always pointers.
Definition: GlobalValue.h:107
bool isDeclaration() const
Definition: Globals.cpp:66
unsigned Visibility
Definition: GlobalValue.h:69
bool hasProtectedVisibility() const
Definition: GlobalValue.h:90
static bool isDLLImportLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:149
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:38
static bool isLinkerPrivateLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:139
Rename collisions when linking (static functions).
Definition: GlobalValue.h:41
bool hasLocalLinkage() const
Definition: GlobalValue.h:211
void removeDeadConstantUsers() const
Definition: Constants.cpp:395
Module * getParent()
Definition: GlobalValue.h:286
LLVM Value Representation.
Definition: Value.h:66
bool hasUnnamedAddr() const
Definition: GlobalValue.h:84
static LinkageTypes getLinkOnceLinkage(bool ODR)
Definition: GlobalValue.h:111
GlobalValue(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps, LinkageTypes linkage, const Twine &Name)
Definition: GlobalValue.h:59
bool mayBeOverridden() const
Definition: GlobalValue.h:224
virtual void eraseFromParent()=0
bool hasDLLImportLinkage() const
Definition: GlobalValue.h:212
bool Materialize(std::string *ErrInfo=0)
Definition: Globals.cpp:36
std::string Section
Definition: GlobalValue.h:73
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110