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