LLVM API Documentation
00001 //===-- llvm/GlobalValue.h - Class to represent a global value --*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file is a common base class of all globally definable objects. As such, 00011 // it is subclassed by GlobalVariable, GlobalAlias and by Function. This is 00012 // used because you can do certain things with these global objects that you 00013 // can't do to anything else. For example, use the address of one as a 00014 // constant. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #ifndef LLVM_GLOBALVALUE_H 00019 #define LLVM_GLOBALVALUE_H 00020 00021 #include "llvm/Constant.h" 00022 00023 namespace llvm { 00024 00025 class PointerType; 00026 class Module; 00027 00028 class GlobalValue : public Constant { 00029 GlobalValue(const GlobalValue &); // do not implement 00030 public: 00031 /// @brief An enumeration for the kinds of linkage for global values. 00032 enum LinkageTypes { 00033 ExternalLinkage = 0,///< Externally visible function 00034 AvailableExternallyLinkage, ///< Available for inspection, not emission. 00035 LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline) 00036 LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent. 00037 WeakAnyLinkage, ///< Keep one copy of named function when linking (weak) 00038 WeakODRLinkage, ///< Same, but only replaced by something equivalent. 00039 AppendingLinkage, ///< Special purpose, only applies to global arrays 00040 InternalLinkage, ///< Rename collisions when linking (static functions). 00041 PrivateLinkage, ///< Like Internal, but omit from symbol table. 00042 LinkerPrivateLinkage, ///< Like Private, but linker removes. 00043 LinkerPrivateWeakLinkage, ///< Like LinkerPrivate, but weak. 00044 LinkerPrivateWeakDefAutoLinkage, ///< Like LinkerPrivateWeak, but possibly 00045 /// hidden. 00046 DLLImportLinkage, ///< Function to be imported from DLL 00047 DLLExportLinkage, ///< Function to be accessible from DLL. 00048 ExternalWeakLinkage,///< ExternalWeak linkage description. 00049 CommonLinkage ///< Tentative definitions. 00050 }; 00051 00052 /// @brief An enumeration for the kinds of visibility of global values. 00053 enum VisibilityTypes { 00054 DefaultVisibility = 0, ///< The GV is visible 00055 HiddenVisibility, ///< The GV is hidden 00056 ProtectedVisibility ///< The GV is protected 00057 }; 00058 00059 protected: 00060 GlobalValue(Type *ty, ValueTy vty, Use *Ops, unsigned NumOps, 00061 LinkageTypes linkage, const Twine &Name) 00062 : Constant(ty, vty, Ops, NumOps), Linkage(linkage), 00063 Visibility(DefaultVisibility), Alignment(0), UnnamedAddr(0), Parent(0) { 00064 setName(Name); 00065 } 00066 00067 // Note: VC++ treats enums as signed, so an extra bit is required to prevent 00068 // Linkage and Visibility from turning into negative values. 00069 LinkageTypes Linkage : 5; // The linkage of this global 00070 unsigned Visibility : 2; // The visibility style of this global 00071 unsigned Alignment : 16; // Alignment of this symbol, must be power of two 00072 unsigned UnnamedAddr : 1; // This value's address is not significant 00073 Module *Parent; // The containing module. 00074 std::string Section; // Section to emit this into, empty mean default 00075 public: 00076 ~GlobalValue() { 00077 removeDeadConstantUsers(); // remove any dead constants using this. 00078 } 00079 00080 unsigned getAlignment() const { 00081 return (1u << Alignment) >> 1; 00082 } 00083 void setAlignment(unsigned Align); 00084 00085 bool hasUnnamedAddr() const { return UnnamedAddr; } 00086 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; } 00087 00088 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); } 00089 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; } 00090 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; } 00091 bool hasProtectedVisibility() const { 00092 return Visibility == ProtectedVisibility; 00093 } 00094 void setVisibility(VisibilityTypes V) { Visibility = V; } 00095 00096 bool hasSection() const { return !Section.empty(); } 00097 const std::string &getSection() const { return Section; } 00098 void setSection(StringRef S) { Section = S; } 00099 00100 /// If the usage is empty (except transitively dead constants), then this 00101 /// global value can be safely deleted since the destructor will 00102 /// delete the dead constants as well. 00103 /// @brief Determine if the usage of this global value is empty except 00104 /// for transitively dead constants. 00105 bool use_empty_except_constants(); 00106 00107 /// getType - Global values are always pointers. 00108 inline PointerType *getType() const { 00109 return reinterpret_cast<PointerType*>(User::getType()); 00110 } 00111 00112 static LinkageTypes getLinkOnceLinkage(bool ODR) { 00113 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage; 00114 } 00115 static LinkageTypes getWeakLinkage(bool ODR) { 00116 return ODR ? WeakODRLinkage : WeakAnyLinkage; 00117 } 00118 00119 static bool isExternalLinkage(LinkageTypes Linkage) { 00120 return Linkage == ExternalLinkage; 00121 } 00122 static bool isAvailableExternallyLinkage(LinkageTypes Linkage) { 00123 return Linkage == AvailableExternallyLinkage; 00124 } 00125 static bool isLinkOnceLinkage(LinkageTypes Linkage) { 00126 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage; 00127 } 00128 static bool isWeakLinkage(LinkageTypes Linkage) { 00129 return Linkage == WeakAnyLinkage || Linkage == WeakODRLinkage; 00130 } 00131 static bool isAppendingLinkage(LinkageTypes Linkage) { 00132 return Linkage == AppendingLinkage; 00133 } 00134 static bool isInternalLinkage(LinkageTypes Linkage) { 00135 return Linkage == InternalLinkage; 00136 } 00137 static bool isPrivateLinkage(LinkageTypes Linkage) { 00138 return Linkage == PrivateLinkage; 00139 } 00140 static bool isLinkerPrivateLinkage(LinkageTypes Linkage) { 00141 return Linkage == LinkerPrivateLinkage; 00142 } 00143 static bool isLinkerPrivateWeakLinkage(LinkageTypes Linkage) { 00144 return Linkage == LinkerPrivateWeakLinkage; 00145 } 00146 static bool isLinkerPrivateWeakDefAutoLinkage(LinkageTypes Linkage) { 00147 return Linkage == LinkerPrivateWeakDefAutoLinkage; 00148 } 00149 static bool isLocalLinkage(LinkageTypes Linkage) { 00150 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage) || 00151 isLinkerPrivateLinkage(Linkage) || isLinkerPrivateWeakLinkage(Linkage) || 00152 isLinkerPrivateWeakDefAutoLinkage(Linkage); 00153 } 00154 static bool isDLLImportLinkage(LinkageTypes Linkage) { 00155 return Linkage == DLLImportLinkage; 00156 } 00157 static bool isDLLExportLinkage(LinkageTypes Linkage) { 00158 return Linkage == DLLExportLinkage; 00159 } 00160 static bool isExternalWeakLinkage(LinkageTypes Linkage) { 00161 return Linkage == ExternalWeakLinkage; 00162 } 00163 static bool isCommonLinkage(LinkageTypes Linkage) { 00164 return Linkage == CommonLinkage; 00165 } 00166 00167 /// mayBeOverridden - Whether the definition of this global may be replaced 00168 /// by something non-equivalent at link time. For example, if a function has 00169 /// weak linkage then the code defining it may be replaced by different code. 00170 static bool mayBeOverridden(LinkageTypes Linkage) { 00171 return Linkage == WeakAnyLinkage || 00172 Linkage == LinkOnceAnyLinkage || 00173 Linkage == CommonLinkage || 00174 Linkage == ExternalWeakLinkage || 00175 Linkage == LinkerPrivateWeakLinkage || 00176 Linkage == LinkerPrivateWeakDefAutoLinkage; 00177 } 00178 00179 /// isWeakForLinker - Whether the definition of this global may be replaced at 00180 /// link time. NB: Using this method outside of the code generators is almost 00181 /// always a mistake: when working at the IR level use mayBeOverridden instead 00182 /// as it knows about ODR semantics. 00183 static bool isWeakForLinker(LinkageTypes Linkage) { 00184 return Linkage == AvailableExternallyLinkage || 00185 Linkage == WeakAnyLinkage || 00186 Linkage == WeakODRLinkage || 00187 Linkage == LinkOnceAnyLinkage || 00188 Linkage == LinkOnceODRLinkage || 00189 Linkage == CommonLinkage || 00190 Linkage == ExternalWeakLinkage || 00191 Linkage == LinkerPrivateWeakLinkage || 00192 Linkage == LinkerPrivateWeakDefAutoLinkage; 00193 } 00194 00195 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); } 00196 bool hasAvailableExternallyLinkage() const { 00197 return isAvailableExternallyLinkage(Linkage); 00198 } 00199 bool hasLinkOnceLinkage() const { 00200 return isLinkOnceLinkage(Linkage); 00201 } 00202 bool hasWeakLinkage() const { 00203 return isWeakLinkage(Linkage); 00204 } 00205 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); } 00206 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); } 00207 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); } 00208 bool hasLinkerPrivateLinkage() const { return isLinkerPrivateLinkage(Linkage); } 00209 bool hasLinkerPrivateWeakLinkage() const { 00210 return isLinkerPrivateWeakLinkage(Linkage); 00211 } 00212 bool hasLinkerPrivateWeakDefAutoLinkage() const { 00213 return isLinkerPrivateWeakDefAutoLinkage(Linkage); 00214 } 00215 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); } 00216 bool hasDLLImportLinkage() const { return isDLLImportLinkage(Linkage); } 00217 bool hasDLLExportLinkage() const { return isDLLExportLinkage(Linkage); } 00218 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); } 00219 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); } 00220 00221 void setLinkage(LinkageTypes LT) { Linkage = LT; } 00222 LinkageTypes getLinkage() const { return Linkage; } 00223 00224 bool mayBeOverridden() const { return mayBeOverridden(Linkage); } 00225 00226 bool isWeakForLinker() const { return isWeakForLinker(Linkage); } 00227 00228 /// copyAttributesFrom - copy all additional attributes (those not needed to 00229 /// create a GlobalValue) from the GlobalValue Src to this one. 00230 virtual void copyAttributesFrom(const GlobalValue *Src); 00231 00232 /// @name Materialization 00233 /// Materialization is used to construct functions only as they're needed. This 00234 /// is useful to reduce memory usage in LLVM or parsing work done by the 00235 /// BitcodeReader to load the Module. 00236 /// @{ 00237 00238 /// isMaterializable - If this function's Module is being lazily streamed in 00239 /// functions from disk or some other source, this method can be used to check 00240 /// to see if the function has been read in yet or not. 00241 bool isMaterializable() const; 00242 00243 /// isDematerializable - Returns true if this function was loaded from a 00244 /// GVMaterializer that's still attached to its Module and that knows how to 00245 /// dematerialize the function. 00246 bool isDematerializable() const; 00247 00248 /// Materialize - make sure this GlobalValue is fully read. If the module is 00249 /// corrupt, this returns true and fills in the optional string with 00250 /// information about the problem. If successful, this returns false. 00251 bool Materialize(std::string *ErrInfo = 0); 00252 00253 /// Dematerialize - If this GlobalValue is read in, and if the GVMaterializer 00254 /// supports it, release the memory for the function, and set it up to be 00255 /// materialized lazily. If !isDematerializable(), this method is a noop. 00256 void Dematerialize(); 00257 00258 /// @} 00259 00260 /// Override from Constant class. 00261 virtual void destroyConstant(); 00262 00263 /// isDeclaration - Return true if the primary definition of this global 00264 /// value is outside of the current translation unit. 00265 bool isDeclaration() const; 00266 00267 /// removeFromParent - This method unlinks 'this' from the containing module, 00268 /// but does not delete it. 00269 virtual void removeFromParent() = 0; 00270 00271 /// eraseFromParent - This method unlinks 'this' from the containing module 00272 /// and deletes it. 00273 virtual void eraseFromParent() = 0; 00274 00275 /// getParent - Get the module that this global value is contained inside 00276 /// of... 00277 inline Module *getParent() { return Parent; } 00278 inline const Module *getParent() const { return Parent; } 00279 00280 // Methods for support type inquiry through isa, cast, and dyn_cast: 00281 static inline bool classof(const GlobalValue *) { return true; } 00282 static inline bool classof(const Value *V) { 00283 return V->getValueID() == Value::FunctionVal || 00284 V->getValueID() == Value::GlobalVariableVal || 00285 V->getValueID() == Value::GlobalAliasVal; 00286 } 00287 }; 00288 00289 } // End llvm namespace 00290 00291 #endif