LLVM  3.7.0
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 #include <system_error>
24 
25 namespace llvm {
26 
27 class Comdat;
28 class PointerType;
29 class Module;
30 
31 namespace Intrinsic {
32  enum ID : unsigned;
33 }
34 
35 class GlobalValue : public Constant {
36  GlobalValue(const GlobalValue &) = delete;
37 public:
38  /// @brief An enumeration for the kinds of linkage for global values.
39  enum LinkageTypes {
40  ExternalLinkage = 0,///< Externally visible function
41  AvailableExternallyLinkage, ///< Available for inspection, not emission.
42  LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
43  LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
44  WeakAnyLinkage, ///< Keep one copy of named function when linking (weak)
45  WeakODRLinkage, ///< Same, but only replaced by something equivalent.
46  AppendingLinkage, ///< Special purpose, only applies to global arrays
47  InternalLinkage, ///< Rename collisions when linking (static functions).
48  PrivateLinkage, ///< Like Internal, but omit from symbol table.
49  ExternalWeakLinkage,///< ExternalWeak linkage description.
50  CommonLinkage ///< Tentative definitions.
51  };
52 
53  /// @brief An enumeration for the kinds of visibility of global values.
55  DefaultVisibility = 0, ///< The GV is visible
56  HiddenVisibility, ///< The GV is hidden
57  ProtectedVisibility ///< The GV is protected
58  };
59 
60  /// @brief Storage classes of global values for PE targets.
63  DLLImportStorageClass = 1, ///< Function to be imported from DLL
64  DLLExportStorageClass = 2 ///< Function to be accessible from DLL.
65  };
66 
67 protected:
68  GlobalValue(PointerType *Ty, ValueTy VTy, Use *Ops, unsigned NumOps,
70  : Constant(Ty, VTy, Ops, NumOps), Linkage(Linkage),
73  ThreadLocal(NotThreadLocal), IntID((Intrinsic::ID)0U), Parent(nullptr) {
74  setName(Name);
75  }
76 
77  // Note: VC++ treats enums as signed, so an extra bit is required to prevent
78  // Linkage and Visibility from turning into negative values.
79  LinkageTypes Linkage : 5; // The linkage of this global
80  unsigned Visibility : 2; // The visibility style of this global
81  unsigned UnnamedAddr : 1; // This value's address is not significant
82  unsigned DllStorageClass : 2; // DLL storage class
83 
84  unsigned ThreadLocal : 3; // Is this symbol "Thread Local", if so, what is
85  // the desired model?
86  static const unsigned GlobalValueSubClassDataBits = 19;
87 
88 private:
89  // Give subclasses access to what otherwise would be wasted padding.
90  // (19 + 3 + 2 + 1 + 2 + 5) == 32.
91  unsigned SubClassData : GlobalValueSubClassDataBits;
92 
93  friend class Constant;
94  void destroyConstantImpl();
95  Value *handleOperandChangeImpl(Value *From, Value *To, Use *U);
96 
97 protected:
98  /// \brief The intrinsic ID for this subclass (which must be a Function).
99  ///
100  /// This member is defined by this class, but not used for anything.
101  /// Subclasses can use it to store their intrinsic ID, if they have one.
102  ///
103  /// This is stored here to save space in Function on 64-bit hosts.
105 
106  unsigned getGlobalValueSubClassData() const {
107  return SubClassData;
108  }
109  void setGlobalValueSubClassData(unsigned V) {
110  assert(V < (1 << GlobalValueSubClassDataBits) && "It will not fit");
111  SubClassData = V;
112  }
113 
114  Module *Parent; // The containing module.
115 public:
122  };
123 
124  ~GlobalValue() override {
125  removeDeadConstantUsers(); // remove any dead constants using this.
126  }
127 
128  unsigned getAlignment() const;
129 
130  bool hasUnnamedAddr() const { return UnnamedAddr; }
131  void setUnnamedAddr(bool Val) { UnnamedAddr = Val; }
132 
133  bool hasComdat() const { return getComdat() != nullptr; }
134  Comdat *getComdat();
135  const Comdat *getComdat() const {
136  return const_cast<GlobalValue *>(this)->getComdat();
137  }
138 
141  bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
142  bool hasProtectedVisibility() const {
144  }
146  assert((!hasLocalLinkage() || V == DefaultVisibility) &&
147  "local linkage requires default visibility");
148  Visibility = V;
149  }
150 
151  /// If the value is "Thread Local", its value isn't shared by the threads.
152  bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal; }
153  void setThreadLocal(bool Val) {
155  }
157  assert(Val == NotThreadLocal || getValueID() != Value::FunctionVal);
158  ThreadLocal = Val;
159  }
161  return static_cast<ThreadLocalMode>(ThreadLocal);
162  }
163 
166  }
169  }
172  }
174 
175  bool hasSection() const { return !StringRef(getSection()).empty(); }
176  // It is unfortunate that we have to use "char *" in here since this is
177  // always non NULL, but:
178  // * The C API expects a null terminated string, so we cannot use StringRef.
179  // * The C API expects us to own it, so we cannot use a std:string.
180  // * For GlobalAliases we can fail to find the section and we have to
181  // return "", so we cannot use a "const std::string &".
182  const char *getSection() const;
183 
184  /// Global values are always pointers.
185  PointerType *getType() const { return cast<PointerType>(User::getType()); }
186 
187  Type *getValueType() const { return getType()->getElementType(); }
188 
189  static LinkageTypes getLinkOnceLinkage(bool ODR) {
190  return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
191  }
192  static LinkageTypes getWeakLinkage(bool ODR) {
193  return ODR ? WeakODRLinkage : WeakAnyLinkage;
194  }
195 
197  return Linkage == ExternalLinkage;
198  }
200  return Linkage == AvailableExternallyLinkage;
201  }
203  return Linkage == LinkOnceODRLinkage;
204  }
206  return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
207  }
209  return Linkage == WeakAnyLinkage;
210  }
212  return Linkage == WeakODRLinkage;
213  }
215  return isWeakAnyLinkage(Linkage) || isWeakODRLinkage(Linkage);
216  }
218  return Linkage == AppendingLinkage;
219  }
221  return Linkage == InternalLinkage;
222  }
224  return Linkage == PrivateLinkage;
225  }
227  return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage);
228  }
230  return Linkage == ExternalWeakLinkage;
231  }
233  return Linkage == CommonLinkage;
234  }
235 
236  /// Whether the definition of this global may be discarded if it is not used
237  /// in its compilation unit.
239  return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage);
240  }
241 
242  /// Whether the definition of this global may be replaced by something
243  /// non-equivalent at link time. For example, if a function has weak linkage
244  /// then the code defining it may be replaced by different code.
246  return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage ||
247  Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
248  }
249 
250  /// Whether the definition of this global may be replaced at link time. NB:
251  /// Using this method outside of the code generators is almost always a
252  /// mistake: when working at the IR level use mayBeOverridden instead as it
253  /// knows about ODR semantics.
255  return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage ||
256  Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage ||
257  Linkage == CommonLinkage || Linkage == ExternalWeakLinkage;
258  }
259 
260  bool hasExternalLinkage() const { return isExternalLinkage(Linkage); }
263  }
264  bool hasLinkOnceLinkage() const {
265  return isLinkOnceLinkage(Linkage);
266  }
268  bool hasWeakLinkage() const {
269  return isWeakLinkage(Linkage);
270  }
271  bool hasWeakAnyLinkage() const {
272  return isWeakAnyLinkage(Linkage);
273  }
274  bool hasWeakODRLinkage() const {
275  return isWeakODRLinkage(Linkage);
276  }
278  bool hasInternalLinkage() const { return isInternalLinkage(Linkage); }
279  bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); }
280  bool hasLocalLinkage() const { return isLocalLinkage(Linkage); }
282  bool hasCommonLinkage() const { return isCommonLinkage(Linkage); }
283 
285  if (isLocalLinkage(LT))
287  Linkage = LT;
288  }
289  LinkageTypes getLinkage() const { return Linkage; }
290 
291  bool isDiscardableIfUnused() const {
293  }
294 
295  bool mayBeOverridden() const { return mayBeOverridden(Linkage); }
296 
297  bool isWeakForLinker() const { return isWeakForLinker(Linkage); }
298 
299  /// Copy all additional attributes (those not needed to create a GlobalValue)
300  /// from the GlobalValue Src to this one.
301  virtual void copyAttributesFrom(const GlobalValue *Src);
302 
303  /// If special LLVM prefix that is used to inform the asm printer to not emit
304  /// usual symbol prefix before the symbol name is used then return linkage
305  /// name after skipping this special LLVM prefix.
307  if (!Name.empty() && Name[0] == '\1')
308  return Name.substr(1);
309  return Name;
310  }
311 
312 /// @name Materialization
313 /// Materialization is used to construct functions only as they're needed. This
314 /// is useful to reduce memory usage in LLVM or parsing work done by the
315 /// BitcodeReader to load the Module.
316 /// @{
317 
318  /// If this function's Module is being lazily streamed in functions from disk
319  /// or some other source, this method can be used to check to see if the
320  /// function has been read in yet or not.
321  bool isMaterializable() const;
322 
323  /// Returns true if this function was loaded from a GVMaterializer that's
324  /// still attached to its Module and that knows how to dematerialize the
325  /// function.
326  bool isDematerializable() const;
327 
328  /// Make sure this GlobalValue is fully read. If the module is corrupt, this
329  /// returns true and fills in the optional string with information about the
330  /// problem. If successful, this returns false.
331  std::error_code materialize();
332 
333  /// If this GlobalValue is read in, and if the GVMaterializer supports it,
334  /// release the memory for the function, and set it up to be materialized
335  /// lazily. If !isDematerializable(), this method is a noop.
336  void dematerialize();
337 
338 /// @}
339 
340  /// Return true if the primary definition of this global value is outside of
341  /// the current translation unit.
342  bool isDeclaration() const;
343 
344  bool isDeclarationForLinker() const {
346  return true;
347 
348  return isDeclaration();
349  }
350 
351  /// Returns true if this global's definition will be the one chosen by the
352  /// linker.
354  return !(isDeclarationForLinker() || isWeakForLinker());
355  }
356 
357  /// This method unlinks 'this' from the containing module, but does not delete
358  /// it.
359  virtual void removeFromParent() = 0;
360 
361  /// This method unlinks 'this' from the containing module and deletes it.
362  virtual void eraseFromParent() = 0;
363 
364  /// Get the module that this global value is contained inside of...
365  Module *getParent() { return Parent; }
366  const Module *getParent() const { return Parent; }
367 
368  // Methods for support type inquiry through isa, cast, and dyn_cast:
369  static bool classof(const Value *V) {
370  return V->getValueID() == Value::FunctionVal ||
371  V->getValueID() == Value::GlobalVariableVal ||
372  V->getValueID() == Value::GlobalAliasVal;
373  }
374 };
375 
376 } // End llvm namespace
377 
378 #endif
void setVisibility(VisibilityTypes V)
Definition: GlobalValue.h:145
LinkageTypes getLinkage() const
Definition: GlobalValue.h:289
~GlobalValue() override
Definition: GlobalValue.h:124
Special purpose, only applies to global arrays.
Definition: GlobalValue.h:46
VisibilityTypes getVisibility() const
Definition: GlobalValue.h:139
virtual void copyAttributesFrom(const GlobalValue *Src)
Copy all additional attributes (those not needed to create a GlobalValue) from the GlobalValue Src to...
Definition: Globals.cpp:57
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:45
ThreadLocal - A class used to abstract thread-local storage.
Definition: ThreadLocal.h:46
static bool isExternalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:196
Available for inspection, not emission.
Definition: GlobalValue.h:41
Type * getValueType() const
Definition: GlobalValue.h:187
bool hasAppendingLinkage() const
Definition: GlobalValue.h:277
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:405
Like Internal, but omit from symbol table.
Definition: GlobalValue.h:48
Externally visible function.
Definition: GlobalValue.h:40
static bool isWeakAnyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:208
bool hasAvailableExternallyLinkage() const
Definition: GlobalValue.h:261
static bool isCommonLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:232
const char * getSection() const
Definition: Globals.cpp:106
Tentative definitions.
Definition: GlobalValue.h:50
static bool isLocalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:226
static bool isLinkOnceLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:205
bool hasCommonLinkage() const
Definition: GlobalValue.h:282
bool hasDLLExportStorageClass() const
Definition: GlobalValue.h:170
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:79
bool hasDefaultVisibility() const
Definition: GlobalValue.h:140
bool hasSection() const
Definition: GlobalValue.h:175
A Use represents the edge between a Value definition and its users.
Definition: Use.h:69
bool hasWeakODRLinkage() const
Definition: GlobalValue.h:274
bool hasInternalLinkage() const
Definition: GlobalValue.h:278
GlobalValue(PointerType *Ty, ValueTy VTy, Use *Ops, unsigned NumOps, LinkageTypes Linkage, const Twine &Name)
Definition: GlobalValue.h:68
void setThreadLocalMode(ThreadLocalMode Val)
Definition: GlobalValue.h:156
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:250
bool isStrongDefinitionForLinker() const
Returns true if this global's definition will be the one chosen by the linker.
Definition: GlobalValue.h:353
void setDLLStorageClass(DLLStorageClassTypes C)
Definition: GlobalValue.h:173
bool hasPrivateLinkage() const
Definition: GlobalValue.h:279
static bool isLinkOnceODRLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:202
static bool isPrivateLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:223
LinkageTypes Linkage
Definition: GlobalValue.h:79
VisibilityTypes
An enumeration for the kinds of visibility of global values.
Definition: GlobalValue.h:54
unsigned DllStorageClass
Definition: GlobalValue.h:82
bool isMaterializable() const
If this function's Module is being lazily streamed in functions from disk or some other source...
Definition: Globals.cpp:30
static bool isAppendingLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:217
bool isDiscardableIfUnused() const
Definition: GlobalValue.h:291
Type * getElementType() const
Definition: DerivedTypes.h:323
PointerType - Class to represent pointers.
Definition: DerivedTypes.h:449
bool isWeakForLinker() const
Definition: GlobalValue.h:297
static bool isWeakForLinker(LinkageTypes Linkage)
Whether the definition of this global may be replaced at link time.
Definition: GlobalValue.h:254
ExternalWeak linkage description.
Definition: GlobalValue.h:49
Same, but only replaced by something equivalent.
Definition: GlobalValue.h:43
static bool isInternalLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:220
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
static bool isWeakODRLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:211
static bool mayBeOverridden(LinkageTypes Linkage)
Whether the definition of this global may be replaced by something non-equivalent at link time...
Definition: GlobalValue.h:245
static bool classof(const Value *V)
Definition: GlobalValue.h:369
This is an important base class in LLVM.
Definition: Constant.h:41
bool hasHiddenVisibility() const
Definition: GlobalValue.h:141
bool isDematerializable() const
Returns true if this function was loaded from a GVMaterializer that's still attached to its Module an...
Definition: Globals.cpp:35
unsigned getValueID() const
Return an ID for the concrete type of this object.
Definition: Value.h:362
static bool isDiscardableIfUnused(LinkageTypes Linkage)
Whether the definition of this global may be discarded if it is not used in its compilation unit...
Definition: GlobalValue.h:238
DLLStorageClassTypes
Storage classes of global values for PE targets.
Definition: GlobalValue.h:61
Intrinsic::ID IntID
The intrinsic ID for this subclass (which must be a Function).
Definition: GlobalValue.h:104
const Module * getParent() const
Definition: GlobalValue.h:366
bool hasWeakAnyLinkage() const
Definition: GlobalValue.h:271
bool isThreadLocal() const
If the value is "Thread Local", its value isn't shared by the threads.
Definition: GlobalValue.h:152
bool hasWeakLinkage() const
Definition: GlobalValue.h:268
static LinkageTypes getWeakLinkage(bool ODR)
Definition: GlobalValue.h:192
bool hasExternalWeakLinkage() const
Definition: GlobalValue.h:281
bool hasExternalLinkage() const
Definition: GlobalValue.h:260
bool hasDLLImportStorageClass() const
Definition: GlobalValue.h:167
Comdat * getComdat()
Definition: Globals.cpp:116
virtual void removeFromParent()=0
This method unlinks 'this' from the containing module, but does not delete it.
static bool isWeakLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:214
Keep one copy of function when linking (inline)
Definition: GlobalValue.h:42
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:222
void setUnnamedAddr(bool Val)
Definition: GlobalValue.h:131
static bool isAvailableExternallyLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:199
ValueTy
Concrete subclass of this.
Definition: Value.h:343
void setLinkage(LinkageTypes LT)
Definition: GlobalValue.h:284
static bool isExternalWeakLinkage(LinkageTypes Linkage)
Definition: GlobalValue.h:229
LinkageTypes
An enumeration for the kinds of linkage for global values.
Definition: GlobalValue.h:39
void setGlobalValueSubClassData(unsigned V)
Definition: GlobalValue.h:109
unsigned ThreadLocal
Definition: GlobalValue.h:84
unsigned UnnamedAddr
Definition: GlobalValue.h:81
const Comdat * getComdat() const
Definition: GlobalValue.h:135
static const unsigned GlobalValueSubClassDataBits
Definition: GlobalValue.h:86
ThreadLocalMode getThreadLocalMode() const
Definition: GlobalValue.h:160
bool hasLinkOnceLinkage() const
Definition: GlobalValue.h:264
unsigned getGlobalValueSubClassData() const
Definition: GlobalValue.h:106
static StringRef getRealLinkageName(StringRef Name)
If special LLVM prefix that is used to inform the asm printer to not emit usual symbol prefix before ...
Definition: GlobalValue.h:306
Function to be accessible from DLL.
Definition: GlobalValue.h:64
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:185
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:128
bool hasComdat() const
Definition: GlobalValue.h:133
bool hasLinkOnceODRLinkage() const
Definition: GlobalValue.h:267
unsigned Visibility
Definition: GlobalValue.h:80
bool hasProtectedVisibility() const
Definition: GlobalValue.h:142
Keep one copy of named function when linking (weak)
Definition: GlobalValue.h:44
Rename collisions when linking (static functions).
Definition: GlobalValue.h:47
Function to be imported from DLL.
Definition: GlobalValue.h:63
bool hasLocalLinkage() const
Definition: GlobalValue.h:280
unsigned getAlignment() const
Definition: Globals.cpp:63
void removeDeadConstantUsers() const
removeDeadConstantUsers - If there are any dead constant users dangling off of this constant...
Definition: Constants.cpp:487
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:365
LLVM Value Representation.
Definition: Value.h:69
bool hasUnnamedAddr() const
Definition: GlobalValue.h:130
static LinkageTypes getLinkOnceLinkage(bool ODR)
Definition: GlobalValue.h:189
void dematerialize()
If this GlobalValue is read in, and if the GVMaterializer supports it, release the memory for the fun...
Definition: Globals.cpp:41
void setThreadLocal(bool Val)
Definition: GlobalValue.h:153
bool mayBeOverridden() const
Definition: GlobalValue.h:295
virtual void eraseFromParent()=0
This method unlinks 'this' from the containing module and deletes it.
DLLStorageClassTypes getDLLStorageClass() const
Definition: GlobalValue.h:164
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
std::error_code materialize()
Make sure this GlobalValue is fully read.
Definition: Globals.cpp:38
bool isDeclarationForLinker() const
Definition: GlobalValue.h:344
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:110