Significantly improve Atomic.h by pulling in code from libatomic_ops by HP. This...
[oota-llvm.git] / include / llvm / GlobalValue.h
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_GLOBALVALUE_H
19 #define LLVM_GLOBALVALUE_H
20
21 #include "llvm/Constant.h"
22
23 namespace llvm {
24
25 class PointerType;
26 class Module;
27
28 class GlobalValue : public Constant {
29   GlobalValue(const GlobalValue &);             // do not implement
30 public:
31   /// @brief An enumeration for the kinds of linkage for global values.
32   enum LinkageTypes {
33     ExternalLinkage = 0,///< Externally visible function
34     AvailableExternallyLinkage, ///< Available for inspection, not emission.
35     LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline)
36     LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent.
37     WeakAnyLinkage,     ///< Keep one copy of named function when linking (weak)
38     WeakODRLinkage,     ///< Same, but only replaced by something equivalent.
39     AppendingLinkage,   ///< Special purpose, only applies to global arrays
40     InternalLinkage,    ///< Rename collisions when linking (static functions)
41     PrivateLinkage,     ///< Like Internal, but omit from symbol table
42     DLLImportLinkage,   ///< Function to be imported from DLL
43     DLLExportLinkage,   ///< Function to be accessible from DLL
44     ExternalWeakLinkage,///< ExternalWeak linkage description
45     GhostLinkage,       ///< Stand-in functions for streaming fns from BC files
46     CommonLinkage       ///< Tentative definitions
47   };
48
49   /// @brief An enumeration for the kinds of visibility of global values.
50   enum VisibilityTypes {
51     DefaultVisibility = 0,  ///< The GV is visible
52     HiddenVisibility,       ///< The GV is hidden
53     ProtectedVisibility     ///< The GV is protected
54   };
55
56 protected:
57   GlobalValue(const Type *ty, ValueTy vty, Use *Ops, unsigned NumOps,
58               LinkageTypes linkage, const std::string &name = "")
59     : Constant(ty, vty, Ops, NumOps), Parent(0),
60       Linkage(linkage), Visibility(DefaultVisibility), Alignment(0) {
61     if (!name.empty()) setName(name);
62   }
63
64   Module *Parent;
65   // Note: VC++ treats enums as signed, so an extra bit is required to prevent
66   // Linkage and Visibility from turning into negative values.
67   LinkageTypes Linkage : 5;   // The linkage of this global
68   unsigned Visibility : 2;    // The visibility style of this global
69   unsigned Alignment : 16;    // Alignment of this symbol, must be power of two
70   std::string Section;        // Section to emit this into, empty mean default
71 public:
72   ~GlobalValue() {
73     removeDeadConstantUsers();   // remove any dead constants using this.
74   }
75
76   unsigned getAlignment() const { return Alignment; }
77   void setAlignment(unsigned Align) {
78     assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
79     Alignment = Align;
80   }
81
82   VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); }
83   bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; }
84   bool hasProtectedVisibility() const {
85     return Visibility == ProtectedVisibility;
86   }
87   void setVisibility(VisibilityTypes V) { Visibility = V; }
88   
89   bool hasSection() const { return !Section.empty(); }
90   const std::string &getSection() const { return Section; }
91   void setSection(const std::string &S) { Section = S; }
92   
93   /// If the usage is empty (except transitively dead constants), then this
94   /// global value can can be safely deleted since the destructor will
95   /// delete the dead constants as well.
96   /// @brief Determine if the usage of this global value is empty except
97   /// for transitively dead constants.
98   bool use_empty_except_constants();
99
100   /// getType - Global values are always pointers.
101   inline const PointerType *getType() const {
102     return reinterpret_cast<const PointerType*>(User::getType());
103   }
104
105   static LinkageTypes getLinkOnceLinkage(bool ODR) {
106     return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage;
107   }
108   static LinkageTypes getWeakLinkage(bool ODR) {
109     return ODR ? WeakODRLinkage : WeakAnyLinkage;
110   }
111
112   bool hasExternalLinkage() const { return Linkage == ExternalLinkage; }
113   bool hasAvailableExternallyLinkage() const {
114     return Linkage == AvailableExternallyLinkage;
115   }
116   bool hasLinkOnceLinkage() const {
117     return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage;
118   }
119   bool hasWeakLinkage() const {
120     return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage;
121   }
122   bool hasAppendingLinkage() const { return Linkage == AppendingLinkage; }
123   bool hasInternalLinkage() const { return Linkage == InternalLinkage; }
124   bool hasPrivateLinkage() const { return Linkage == PrivateLinkage; }
125   bool hasLocalLinkage() const {
126     return Linkage == InternalLinkage || Linkage == PrivateLinkage ||
127            Linkage == AvailableExternallyLinkage;
128   }
129   bool hasDLLImportLinkage() const { return Linkage == DLLImportLinkage; }
130   bool hasDLLExportLinkage() const { return Linkage == DLLExportLinkage; }
131   bool hasExternalWeakLinkage() const { return Linkage == ExternalWeakLinkage; }
132   bool hasGhostLinkage() const { return Linkage == GhostLinkage; }
133   bool hasCommonLinkage() const { return Linkage == CommonLinkage; }
134
135   void setLinkage(LinkageTypes LT) { Linkage = LT; }
136   LinkageTypes getLinkage() const { return Linkage; }
137
138   /// mayBeOverridden - Whether the definition of this global may be replaced
139   /// by something non-equivalent at link time.  For example, if a function has
140   /// weak linkage then the code defining it may be replaced by different code.
141   bool mayBeOverridden() const {
142     return (Linkage == WeakAnyLinkage ||
143             Linkage == LinkOnceAnyLinkage ||
144             Linkage == CommonLinkage ||
145             Linkage == ExternalWeakLinkage);
146   }
147
148   /// isWeakForLinker - Whether the definition of this global may be replaced at
149   /// link time.
150   bool isWeakForLinker() const {
151     return (Linkage == AvailableExternallyLinkage ||
152             Linkage == WeakAnyLinkage ||
153             Linkage == WeakODRLinkage ||
154             Linkage == LinkOnceAnyLinkage ||
155             Linkage == LinkOnceODRLinkage ||
156             Linkage == CommonLinkage ||
157             Linkage == ExternalWeakLinkage);
158   }
159
160   /// copyAttributesFrom - copy all additional attributes (those not needed to
161   /// create a GlobalValue) from the GlobalValue Src to this one.
162   virtual void copyAttributesFrom(const GlobalValue *Src);
163
164   /// hasNotBeenReadFromBitcode - If a module provider is being used to lazily
165   /// stream in functions from disk, this method can be used to check to see if
166   /// the function has been read in yet or not.  Unless you are working on the
167   /// JIT or something else that streams stuff in lazily, you don't need to
168   /// worry about this.
169   bool hasNotBeenReadFromBitcode() const { return Linkage == GhostLinkage; }
170
171   /// Override from Constant class. No GlobalValue's are null values so this
172   /// always returns false.
173   virtual bool isNullValue() const { return false; }
174
175   /// Override from Constant class.
176   virtual void destroyConstant();
177
178   /// isDeclaration - Return true if the primary definition of this global 
179   /// value is outside of the current translation unit...
180   virtual bool isDeclaration() const = 0;
181
182   /// removeFromParent - This method unlinks 'this' from the containing module,
183   /// but does not delete it.
184   virtual void removeFromParent() = 0;
185
186   /// eraseFromParent - This method unlinks 'this' from the containing module
187   /// and deletes it.
188   virtual void eraseFromParent() = 0;
189
190   /// getParent - Get the module that this global value is contained inside
191   /// of...
192   inline Module *getParent() { return Parent; }
193   inline const Module *getParent() const { return Parent; }
194
195   /// removeDeadConstantUsers - If there are any dead constant users dangling
196   /// off of this global value, remove them.  This method is useful for clients
197   /// that want to check to see if a global is unused, but don't want to deal
198   /// with potentially dead constants hanging off of the globals.
199   void removeDeadConstantUsers() const;
200
201   // Methods for support type inquiry through isa, cast, and dyn_cast:
202   static inline bool classof(const GlobalValue *) { return true; }
203   static inline bool classof(const Value *V) {
204     return V->getValueID() == Value::FunctionVal ||
205            V->getValueID() == Value::GlobalVariableVal ||
206            V->getValueID() == Value::GlobalAliasVal;
207   }
208 };
209
210 } // End llvm namespace
211
212 #endif