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_IR_GLOBALVALUE_H 00019 #define LLVM_IR_GLOBALVALUE_H 00020 00021 #include "llvm/IR/Constant.h" 00022 #include "llvm/IR/DerivedTypes.h" 00023 00024 namespace llvm { 00025 00026 class Comdat; 00027 class PointerType; 00028 class Module; 00029 00030 class GlobalValue : public Constant { 00031 GlobalValue(const GlobalValue &) LLVM_DELETED_FUNCTION; 00032 public: 00033 /// @brief An enumeration for the kinds of linkage for global values. 00034 enum LinkageTypes { 00035 ExternalLinkage = 0,///< Externally visible function 00036 AvailableExternallyLinkage, ///< Available for inspection, not emission. 00037 LinkOnceAnyLinkage, ///< Keep one copy of function when linking (inline) 00038 LinkOnceODRLinkage, ///< Same, but only replaced by something equivalent. 00039 WeakAnyLinkage, ///< Keep one copy of named function when linking (weak) 00040 WeakODRLinkage, ///< Same, but only replaced by something equivalent. 00041 AppendingLinkage, ///< Special purpose, only applies to global arrays 00042 InternalLinkage, ///< Rename collisions when linking (static functions). 00043 PrivateLinkage, ///< Like Internal, but omit from symbol table. 00044 ExternalWeakLinkage,///< ExternalWeak linkage description. 00045 CommonLinkage ///< Tentative definitions. 00046 }; 00047 00048 /// @brief An enumeration for the kinds of visibility of global values. 00049 enum VisibilityTypes { 00050 DefaultVisibility = 0, ///< The GV is visible 00051 HiddenVisibility, ///< The GV is hidden 00052 ProtectedVisibility ///< The GV is protected 00053 }; 00054 00055 /// @brief Storage classes of global values for PE targets. 00056 enum DLLStorageClassTypes { 00057 DefaultStorageClass = 0, 00058 DLLImportStorageClass = 1, ///< Function to be imported from DLL 00059 DLLExportStorageClass = 2 ///< Function to be accessible from DLL. 00060 }; 00061 00062 protected: 00063 GlobalValue(Type *Ty, ValueTy VTy, Use *Ops, unsigned NumOps, 00064 LinkageTypes Linkage, const Twine &Name) 00065 : Constant(Ty, VTy, Ops, NumOps), Linkage(Linkage), 00066 Visibility(DefaultVisibility), UnnamedAddr(0), 00067 DllStorageClass(DefaultStorageClass), 00068 ThreadLocal(NotThreadLocal), Parent(nullptr) { 00069 setName(Name); 00070 } 00071 00072 // Note: VC++ treats enums as signed, so an extra bit is required to prevent 00073 // Linkage and Visibility from turning into negative values. 00074 LinkageTypes Linkage : 5; // The linkage of this global 00075 unsigned Visibility : 2; // The visibility style of this global 00076 unsigned UnnamedAddr : 1; // This value's address is not significant 00077 unsigned DllStorageClass : 2; // DLL storage class 00078 00079 unsigned ThreadLocal : 3; // Is this symbol "Thread Local", if so, what is 00080 // the desired model? 00081 00082 private: 00083 // Give subclasses access to what otherwise would be wasted padding. 00084 // (19 + 3 + 2 + 1 + 2 + 5) == 32. 00085 unsigned SubClassData : 19; 00086 protected: 00087 unsigned getGlobalValueSubClassData() const { 00088 return SubClassData; 00089 } 00090 void setGlobalValueSubClassData(unsigned V) { 00091 assert(V < (1 << 19) && "It will not fit"); 00092 SubClassData = V; 00093 } 00094 00095 Module *Parent; // The containing module. 00096 public: 00097 enum ThreadLocalMode { 00098 NotThreadLocal = 0, 00099 GeneralDynamicTLSModel, 00100 LocalDynamicTLSModel, 00101 InitialExecTLSModel, 00102 LocalExecTLSModel 00103 }; 00104 00105 ~GlobalValue() { 00106 removeDeadConstantUsers(); // remove any dead constants using this. 00107 } 00108 00109 unsigned getAlignment() const; 00110 00111 bool hasUnnamedAddr() const { return UnnamedAddr; } 00112 void setUnnamedAddr(bool Val) { UnnamedAddr = Val; } 00113 00114 bool hasComdat() const { return getComdat() != nullptr; } 00115 Comdat *getComdat(); 00116 const Comdat *getComdat() const { 00117 return const_cast<GlobalValue *>(this)->getComdat(); 00118 } 00119 00120 VisibilityTypes getVisibility() const { return VisibilityTypes(Visibility); } 00121 bool hasDefaultVisibility() const { return Visibility == DefaultVisibility; } 00122 bool hasHiddenVisibility() const { return Visibility == HiddenVisibility; } 00123 bool hasProtectedVisibility() const { 00124 return Visibility == ProtectedVisibility; 00125 } 00126 void setVisibility(VisibilityTypes V) { 00127 assert((!hasLocalLinkage() || V == DefaultVisibility) && 00128 "local linkage requires default visibility"); 00129 Visibility = V; 00130 } 00131 00132 /// If the value is "Thread Local", its value isn't shared by the threads. 00133 bool isThreadLocal() const { return getThreadLocalMode() != NotThreadLocal; } 00134 void setThreadLocal(bool Val) { 00135 setThreadLocalMode(Val ? GeneralDynamicTLSModel : NotThreadLocal); 00136 } 00137 void setThreadLocalMode(ThreadLocalMode Val) { 00138 assert(Val == NotThreadLocal || getValueID() != Value::FunctionVal); 00139 ThreadLocal = Val; 00140 } 00141 ThreadLocalMode getThreadLocalMode() const { 00142 return static_cast<ThreadLocalMode>(ThreadLocal); 00143 } 00144 00145 DLLStorageClassTypes getDLLStorageClass() const { 00146 return DLLStorageClassTypes(DllStorageClass); 00147 } 00148 bool hasDLLImportStorageClass() const { 00149 return DllStorageClass == DLLImportStorageClass; 00150 } 00151 bool hasDLLExportStorageClass() const { 00152 return DllStorageClass == DLLExportStorageClass; 00153 } 00154 void setDLLStorageClass(DLLStorageClassTypes C) { DllStorageClass = C; } 00155 00156 bool hasSection() const { return !StringRef(getSection()).empty(); } 00157 // It is unfortunate that we have to use "char *" in here since this is 00158 // always non NULL, but: 00159 // * The C API expects a null terminated string, so we cannot use StringRef. 00160 // * The C API expects us to own it, so we cannot use a std:string. 00161 // * For GlobalAliases we can fail to find the section and we have to 00162 // return "", so we cannot use a "const std::string &". 00163 const char *getSection() const; 00164 00165 /// Global values are always pointers. 00166 inline PointerType *getType() const { 00167 return cast<PointerType>(User::getType()); 00168 } 00169 00170 static LinkageTypes getLinkOnceLinkage(bool ODR) { 00171 return ODR ? LinkOnceODRLinkage : LinkOnceAnyLinkage; 00172 } 00173 static LinkageTypes getWeakLinkage(bool ODR) { 00174 return ODR ? WeakODRLinkage : WeakAnyLinkage; 00175 } 00176 00177 static bool isExternalLinkage(LinkageTypes Linkage) { 00178 return Linkage == ExternalLinkage; 00179 } 00180 static bool isAvailableExternallyLinkage(LinkageTypes Linkage) { 00181 return Linkage == AvailableExternallyLinkage; 00182 } 00183 static bool isLinkOnceODRLinkage(LinkageTypes Linkage) { 00184 return Linkage == LinkOnceODRLinkage; 00185 } 00186 static bool isLinkOnceLinkage(LinkageTypes Linkage) { 00187 return Linkage == LinkOnceAnyLinkage || Linkage == LinkOnceODRLinkage; 00188 } 00189 static bool isWeakAnyLinkage(LinkageTypes Linkage) { 00190 return Linkage == WeakAnyLinkage; 00191 } 00192 static bool isWeakODRLinkage(LinkageTypes Linkage) { 00193 return Linkage == WeakODRLinkage; 00194 } 00195 static bool isWeakLinkage(LinkageTypes Linkage) { 00196 return isWeakAnyLinkage(Linkage) || isWeakODRLinkage(Linkage); 00197 } 00198 static bool isAppendingLinkage(LinkageTypes Linkage) { 00199 return Linkage == AppendingLinkage; 00200 } 00201 static bool isInternalLinkage(LinkageTypes Linkage) { 00202 return Linkage == InternalLinkage; 00203 } 00204 static bool isPrivateLinkage(LinkageTypes Linkage) { 00205 return Linkage == PrivateLinkage; 00206 } 00207 static bool isLocalLinkage(LinkageTypes Linkage) { 00208 return isInternalLinkage(Linkage) || isPrivateLinkage(Linkage); 00209 } 00210 static bool isExternalWeakLinkage(LinkageTypes Linkage) { 00211 return Linkage == ExternalWeakLinkage; 00212 } 00213 static bool isCommonLinkage(LinkageTypes Linkage) { 00214 return Linkage == CommonLinkage; 00215 } 00216 00217 /// Whether the definition of this global may be discarded if it is not used 00218 /// in its compilation unit. 00219 static bool isDiscardableIfUnused(LinkageTypes Linkage) { 00220 return isLinkOnceLinkage(Linkage) || isLocalLinkage(Linkage); 00221 } 00222 00223 /// Whether the definition of this global may be replaced by something 00224 /// non-equivalent at link time. For example, if a function has weak linkage 00225 /// then the code defining it may be replaced by different code. 00226 static bool mayBeOverridden(LinkageTypes Linkage) { 00227 return Linkage == WeakAnyLinkage || Linkage == LinkOnceAnyLinkage || 00228 Linkage == CommonLinkage || Linkage == ExternalWeakLinkage; 00229 } 00230 00231 /// Whether the definition of this global may be replaced at link time. NB: 00232 /// Using this method outside of the code generators is almost always a 00233 /// mistake: when working at the IR level use mayBeOverridden instead as it 00234 /// knows about ODR semantics. 00235 static bool isWeakForLinker(LinkageTypes Linkage) { 00236 return Linkage == AvailableExternallyLinkage || Linkage == WeakAnyLinkage || 00237 Linkage == WeakODRLinkage || Linkage == LinkOnceAnyLinkage || 00238 Linkage == LinkOnceODRLinkage || Linkage == CommonLinkage || 00239 Linkage == ExternalWeakLinkage; 00240 } 00241 00242 bool hasExternalLinkage() const { return isExternalLinkage(Linkage); } 00243 bool hasAvailableExternallyLinkage() const { 00244 return isAvailableExternallyLinkage(Linkage); 00245 } 00246 bool hasLinkOnceLinkage() const { 00247 return isLinkOnceLinkage(Linkage); 00248 } 00249 bool hasLinkOnceODRLinkage() const { return isLinkOnceODRLinkage(Linkage); } 00250 bool hasWeakLinkage() const { 00251 return isWeakLinkage(Linkage); 00252 } 00253 bool hasWeakAnyLinkage() const { 00254 return isWeakAnyLinkage(Linkage); 00255 } 00256 bool hasWeakODRLinkage() const { 00257 return isWeakODRLinkage(Linkage); 00258 } 00259 bool hasAppendingLinkage() const { return isAppendingLinkage(Linkage); } 00260 bool hasInternalLinkage() const { return isInternalLinkage(Linkage); } 00261 bool hasPrivateLinkage() const { return isPrivateLinkage(Linkage); } 00262 bool hasLocalLinkage() const { return isLocalLinkage(Linkage); } 00263 bool hasExternalWeakLinkage() const { return isExternalWeakLinkage(Linkage); } 00264 bool hasCommonLinkage() const { return isCommonLinkage(Linkage); } 00265 00266 void setLinkage(LinkageTypes LT) { 00267 if (isLocalLinkage(LT)) 00268 Visibility = DefaultVisibility; 00269 Linkage = LT; 00270 } 00271 LinkageTypes getLinkage() const { return Linkage; } 00272 00273 bool isDiscardableIfUnused() const { 00274 return isDiscardableIfUnused(Linkage); 00275 } 00276 00277 bool mayBeOverridden() const { return mayBeOverridden(Linkage); } 00278 00279 bool isWeakForLinker() const { return isWeakForLinker(Linkage); } 00280 00281 /// Copy all additional attributes (those not needed to create a GlobalValue) 00282 /// from the GlobalValue Src to this one. 00283 virtual void copyAttributesFrom(const GlobalValue *Src); 00284 00285 /// If special LLVM prefix that is used to inform the asm printer to not emit 00286 /// usual symbol prefix before the symbol name is used then return linkage 00287 /// name after skipping this special LLVM prefix. 00288 static StringRef getRealLinkageName(StringRef Name) { 00289 if (!Name.empty() && Name[0] == '\1') 00290 return Name.substr(1); 00291 return Name; 00292 } 00293 00294 /// @name Materialization 00295 /// Materialization is used to construct functions only as they're needed. This 00296 /// is useful to reduce memory usage in LLVM or parsing work done by the 00297 /// BitcodeReader to load the Module. 00298 /// @{ 00299 00300 /// If this function's Module is being lazily streamed in functions from disk 00301 /// or some other source, this method can be used to check to see if the 00302 /// function has been read in yet or not. 00303 bool isMaterializable() const; 00304 00305 /// Returns true if this function was loaded from a GVMaterializer that's 00306 /// still attached to its Module and that knows how to dematerialize the 00307 /// function. 00308 bool isDematerializable() const; 00309 00310 /// Make sure this GlobalValue is fully read. If the module is corrupt, this 00311 /// returns true and fills in the optional string with information about the 00312 /// problem. If successful, this returns false. 00313 bool Materialize(std::string *ErrInfo = nullptr); 00314 00315 /// If this GlobalValue is read in, and if the GVMaterializer supports it, 00316 /// release the memory for the function, and set it up to be materialized 00317 /// lazily. If !isDematerializable(), this method is a noop. 00318 void Dematerialize(); 00319 00320 /// @} 00321 00322 /// Override from Constant class. 00323 void destroyConstant() override; 00324 00325 /// Return true if the primary definition of this global value is outside of 00326 /// the current translation unit. 00327 bool isDeclaration() const; 00328 00329 /// This method unlinks 'this' from the containing module, but does not delete 00330 /// it. 00331 virtual void removeFromParent() = 0; 00332 00333 /// This method unlinks 'this' from the containing module and deletes it. 00334 virtual void eraseFromParent() = 0; 00335 00336 /// Get the module that this global value is contained inside of... 00337 inline Module *getParent() { return Parent; } 00338 inline const Module *getParent() const { return Parent; } 00339 00340 const DataLayout *getDataLayout() const; 00341 00342 // Methods for support type inquiry through isa, cast, and dyn_cast: 00343 static inline bool classof(const Value *V) { 00344 return V->getValueID() == Value::FunctionVal || 00345 V->getValueID() == Value::GlobalVariableVal || 00346 V->getValueID() == Value::GlobalAliasVal; 00347 } 00348 }; 00349 00350 } // End llvm namespace 00351 00352 #endif