LLVM API Documentation
00001 //===-- LLVMContextImpl.h - The LLVMContextImpl opaque class ----*- 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 declares LLVMContextImpl, the opaque implementation 00011 // of LLVMContext. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #ifndef LLVM_LLVMCONTEXT_IMPL_H 00016 #define LLVM_LLVMCONTEXT_IMPL_H 00017 00018 #include "AttributeImpl.h" 00019 #include "ConstantsContext.h" 00020 #include "LeaksContext.h" 00021 #include "llvm/ADT/APFloat.h" 00022 #include "llvm/ADT/APInt.h" 00023 #include "llvm/ADT/ArrayRef.h" 00024 #include "llvm/ADT/DenseMap.h" 00025 #include "llvm/ADT/FoldingSet.h" 00026 #include "llvm/ADT/Hashing.h" 00027 #include "llvm/ADT/SmallPtrSet.h" 00028 #include "llvm/ADT/StringMap.h" 00029 #include "llvm/IR/Constants.h" 00030 #include "llvm/IR/DerivedTypes.h" 00031 #include "llvm/IR/LLVMContext.h" 00032 #include "llvm/IR/Metadata.h" 00033 #include "llvm/Support/ValueHandle.h" 00034 #include <vector> 00035 00036 namespace llvm { 00037 00038 class ConstantInt; 00039 class ConstantFP; 00040 class LLVMContext; 00041 class Type; 00042 class Value; 00043 00044 struct DenseMapAPIntKeyInfo { 00045 struct KeyTy { 00046 APInt val; 00047 Type* type; 00048 KeyTy(const APInt& V, Type* Ty) : val(V), type(Ty) {} 00049 bool operator==(const KeyTy& that) const { 00050 return type == that.type && this->val == that.val; 00051 } 00052 bool operator!=(const KeyTy& that) const { 00053 return !this->operator==(that); 00054 } 00055 friend hash_code hash_value(const KeyTy &Key) { 00056 return hash_combine(Key.type, Key.val); 00057 } 00058 }; 00059 static inline KeyTy getEmptyKey() { return KeyTy(APInt(1,0), 0); } 00060 static inline KeyTy getTombstoneKey() { return KeyTy(APInt(1,1), 0); } 00061 static unsigned getHashValue(const KeyTy &Key) { 00062 return static_cast<unsigned>(hash_value(Key)); 00063 } 00064 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { 00065 return LHS == RHS; 00066 } 00067 }; 00068 00069 struct DenseMapAPFloatKeyInfo { 00070 struct KeyTy { 00071 APFloat val; 00072 KeyTy(const APFloat& V) : val(V){} 00073 bool operator==(const KeyTy& that) const { 00074 return this->val.bitwiseIsEqual(that.val); 00075 } 00076 bool operator!=(const KeyTy& that) const { 00077 return !this->operator==(that); 00078 } 00079 friend hash_code hash_value(const KeyTy &Key) { 00080 return hash_combine(Key.val); 00081 } 00082 }; 00083 static inline KeyTy getEmptyKey() { 00084 return KeyTy(APFloat(APFloat::Bogus,1)); 00085 } 00086 static inline KeyTy getTombstoneKey() { 00087 return KeyTy(APFloat(APFloat::Bogus,2)); 00088 } 00089 static unsigned getHashValue(const KeyTy &Key) { 00090 return static_cast<unsigned>(hash_value(Key)); 00091 } 00092 static bool isEqual(const KeyTy &LHS, const KeyTy &RHS) { 00093 return LHS == RHS; 00094 } 00095 }; 00096 00097 struct AnonStructTypeKeyInfo { 00098 struct KeyTy { 00099 ArrayRef<Type*> ETypes; 00100 bool isPacked; 00101 KeyTy(const ArrayRef<Type*>& E, bool P) : 00102 ETypes(E), isPacked(P) {} 00103 KeyTy(const StructType* ST) : 00104 ETypes(ArrayRef<Type*>(ST->element_begin(), ST->element_end())), 00105 isPacked(ST->isPacked()) {} 00106 bool operator==(const KeyTy& that) const { 00107 if (isPacked != that.isPacked) 00108 return false; 00109 if (ETypes != that.ETypes) 00110 return false; 00111 return true; 00112 } 00113 bool operator!=(const KeyTy& that) const { 00114 return !this->operator==(that); 00115 } 00116 }; 00117 static inline StructType* getEmptyKey() { 00118 return DenseMapInfo<StructType*>::getEmptyKey(); 00119 } 00120 static inline StructType* getTombstoneKey() { 00121 return DenseMapInfo<StructType*>::getTombstoneKey(); 00122 } 00123 static unsigned getHashValue(const KeyTy& Key) { 00124 return hash_combine(hash_combine_range(Key.ETypes.begin(), 00125 Key.ETypes.end()), 00126 Key.isPacked); 00127 } 00128 static unsigned getHashValue(const StructType *ST) { 00129 return getHashValue(KeyTy(ST)); 00130 } 00131 static bool isEqual(const KeyTy& LHS, const StructType *RHS) { 00132 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 00133 return false; 00134 return LHS == KeyTy(RHS); 00135 } 00136 static bool isEqual(const StructType *LHS, const StructType *RHS) { 00137 return LHS == RHS; 00138 } 00139 }; 00140 00141 struct FunctionTypeKeyInfo { 00142 struct KeyTy { 00143 const Type *ReturnType; 00144 ArrayRef<Type*> Params; 00145 bool isVarArg; 00146 KeyTy(const Type* R, const ArrayRef<Type*>& P, bool V) : 00147 ReturnType(R), Params(P), isVarArg(V) {} 00148 KeyTy(const FunctionType* FT) : 00149 ReturnType(FT->getReturnType()), 00150 Params(ArrayRef<Type*>(FT->param_begin(), FT->param_end())), 00151 isVarArg(FT->isVarArg()) {} 00152 bool operator==(const KeyTy& that) const { 00153 if (ReturnType != that.ReturnType) 00154 return false; 00155 if (isVarArg != that.isVarArg) 00156 return false; 00157 if (Params != that.Params) 00158 return false; 00159 return true; 00160 } 00161 bool operator!=(const KeyTy& that) const { 00162 return !this->operator==(that); 00163 } 00164 }; 00165 static inline FunctionType* getEmptyKey() { 00166 return DenseMapInfo<FunctionType*>::getEmptyKey(); 00167 } 00168 static inline FunctionType* getTombstoneKey() { 00169 return DenseMapInfo<FunctionType*>::getTombstoneKey(); 00170 } 00171 static unsigned getHashValue(const KeyTy& Key) { 00172 return hash_combine(Key.ReturnType, 00173 hash_combine_range(Key.Params.begin(), 00174 Key.Params.end()), 00175 Key.isVarArg); 00176 } 00177 static unsigned getHashValue(const FunctionType *FT) { 00178 return getHashValue(KeyTy(FT)); 00179 } 00180 static bool isEqual(const KeyTy& LHS, const FunctionType *RHS) { 00181 if (RHS == getEmptyKey() || RHS == getTombstoneKey()) 00182 return false; 00183 return LHS == KeyTy(RHS); 00184 } 00185 static bool isEqual(const FunctionType *LHS, const FunctionType *RHS) { 00186 return LHS == RHS; 00187 } 00188 }; 00189 00190 // Provide a FoldingSetTrait::Equals specialization for MDNode that can use a 00191 // shortcut to avoid comparing all operands. 00192 template<> struct FoldingSetTrait<MDNode> : DefaultFoldingSetTrait<MDNode> { 00193 static bool Equals(const MDNode &X, const FoldingSetNodeID &ID, 00194 unsigned IDHash, FoldingSetNodeID &TempID) { 00195 assert(!X.isNotUniqued() && "Non-uniqued MDNode in FoldingSet?"); 00196 // First, check if the cached hashes match. If they don't we can skip the 00197 // expensive operand walk. 00198 if (X.Hash != IDHash) 00199 return false; 00200 00201 // If they match we have to compare the operands. 00202 X.Profile(TempID); 00203 return TempID == ID; 00204 } 00205 static unsigned ComputeHash(const MDNode &X, FoldingSetNodeID &) { 00206 return X.Hash; // Return cached hash. 00207 } 00208 }; 00209 00210 /// DebugRecVH - This is a CallbackVH used to keep the Scope -> index maps 00211 /// up to date as MDNodes mutate. This class is implemented in DebugLoc.cpp. 00212 class DebugRecVH : public CallbackVH { 00213 /// Ctx - This is the LLVM Context being referenced. 00214 LLVMContextImpl *Ctx; 00215 00216 /// Idx - The index into either ScopeRecordIdx or ScopeInlinedAtRecords that 00217 /// this reference lives in. If this is zero, then it represents a 00218 /// non-canonical entry that has no DenseMap value. This can happen due to 00219 /// RAUW. 00220 int Idx; 00221 public: 00222 DebugRecVH(MDNode *n, LLVMContextImpl *ctx, int idx) 00223 : CallbackVH(n), Ctx(ctx), Idx(idx) {} 00224 00225 MDNode *get() const { 00226 return cast_or_null<MDNode>(getValPtr()); 00227 } 00228 00229 virtual void deleted(); 00230 virtual void allUsesReplacedWith(Value *VNew); 00231 }; 00232 00233 class LLVMContextImpl { 00234 public: 00235 /// OwnedModules - The set of modules instantiated in this context, and which 00236 /// will be automatically deleted if this context is deleted. 00237 SmallPtrSet<Module*, 4> OwnedModules; 00238 00239 LLVMContext::InlineAsmDiagHandlerTy InlineAsmDiagHandler; 00240 void *InlineAsmDiagContext; 00241 00242 typedef DenseMap<DenseMapAPIntKeyInfo::KeyTy, ConstantInt*, 00243 DenseMapAPIntKeyInfo> IntMapTy; 00244 IntMapTy IntConstants; 00245 00246 typedef DenseMap<DenseMapAPFloatKeyInfo::KeyTy, ConstantFP*, 00247 DenseMapAPFloatKeyInfo> FPMapTy; 00248 FPMapTy FPConstants; 00249 00250 FoldingSet<AttributeImpl> AttrsSet; 00251 FoldingSet<AttributeSetImpl> AttrsLists; 00252 FoldingSet<AttributeSetNode> AttrsSetNodes; 00253 00254 StringMap<Value*> MDStringCache; 00255 00256 FoldingSet<MDNode> MDNodeSet; 00257 00258 // MDNodes may be uniqued or not uniqued. When they're not uniqued, they 00259 // aren't in the MDNodeSet, but they're still shared between objects, so no 00260 // one object can destroy them. This set allows us to at least destroy them 00261 // on Context destruction. 00262 SmallPtrSet<MDNode*, 1> NonUniquedMDNodes; 00263 00264 DenseMap<Type*, ConstantAggregateZero*> CAZConstants; 00265 00266 typedef ConstantAggrUniqueMap<ArrayType, ConstantArray> ArrayConstantsTy; 00267 ArrayConstantsTy ArrayConstants; 00268 00269 typedef ConstantAggrUniqueMap<StructType, ConstantStruct> StructConstantsTy; 00270 StructConstantsTy StructConstants; 00271 00272 typedef ConstantAggrUniqueMap<VectorType, ConstantVector> VectorConstantsTy; 00273 VectorConstantsTy VectorConstants; 00274 00275 DenseMap<PointerType*, ConstantPointerNull*> CPNConstants; 00276 00277 DenseMap<Type*, UndefValue*> UVConstants; 00278 00279 StringMap<ConstantDataSequential*> CDSConstants; 00280 00281 00282 DenseMap<std::pair<Function*, BasicBlock*> , BlockAddress*> BlockAddresses; 00283 ConstantUniqueMap<ExprMapKeyType, const ExprMapKeyType&, Type, ConstantExpr> 00284 ExprConstants; 00285 00286 ConstantUniqueMap<InlineAsmKeyType, const InlineAsmKeyType&, PointerType, 00287 InlineAsm> InlineAsms; 00288 00289 ConstantInt *TheTrueVal; 00290 ConstantInt *TheFalseVal; 00291 00292 LeakDetectorImpl<Value> LLVMObjects; 00293 00294 // Basic type instances. 00295 Type VoidTy, LabelTy, HalfTy, FloatTy, DoubleTy, MetadataTy; 00296 Type X86_FP80Ty, FP128Ty, PPC_FP128Ty, X86_MMXTy; 00297 IntegerType Int1Ty, Int8Ty, Int16Ty, Int32Ty, Int64Ty; 00298 00299 00300 /// TypeAllocator - All dynamically allocated types are allocated from this. 00301 /// They live forever until the context is torn down. 00302 BumpPtrAllocator TypeAllocator; 00303 00304 DenseMap<unsigned, IntegerType*> IntegerTypes; 00305 00306 typedef DenseMap<FunctionType*, bool, FunctionTypeKeyInfo> FunctionTypeMap; 00307 FunctionTypeMap FunctionTypes; 00308 typedef DenseMap<StructType*, bool, AnonStructTypeKeyInfo> StructTypeMap; 00309 StructTypeMap AnonStructTypes; 00310 StringMap<StructType*> NamedStructTypes; 00311 unsigned NamedStructTypesUniqueID; 00312 00313 DenseMap<std::pair<Type *, uint64_t>, ArrayType*> ArrayTypes; 00314 DenseMap<std::pair<Type *, unsigned>, VectorType*> VectorTypes; 00315 DenseMap<Type*, PointerType*> PointerTypes; // Pointers in AddrSpace = 0 00316 DenseMap<std::pair<Type*, unsigned>, PointerType*> ASPointerTypes; 00317 00318 00319 /// ValueHandles - This map keeps track of all of the value handles that are 00320 /// watching a Value*. The Value::HasValueHandle bit is used to know 00321 /// whether or not a value has an entry in this map. 00322 typedef DenseMap<Value*, ValueHandleBase*> ValueHandlesTy; 00323 ValueHandlesTy ValueHandles; 00324 00325 /// CustomMDKindNames - Map to hold the metadata string to ID mapping. 00326 StringMap<unsigned> CustomMDKindNames; 00327 00328 typedef std::pair<unsigned, TrackingVH<MDNode> > MDPairTy; 00329 typedef SmallVector<MDPairTy, 2> MDMapTy; 00330 00331 /// MetadataStore - Collection of per-instruction metadata used in this 00332 /// context. 00333 DenseMap<const Instruction *, MDMapTy> MetadataStore; 00334 00335 /// ScopeRecordIdx - This is the index in ScopeRecords for an MDNode scope 00336 /// entry with no "inlined at" element. 00337 DenseMap<MDNode*, int> ScopeRecordIdx; 00338 00339 /// ScopeRecords - These are the actual mdnodes (in a value handle) for an 00340 /// index. The ValueHandle ensures that ScopeRecordIdx stays up to date if 00341 /// the MDNode is RAUW'd. 00342 std::vector<DebugRecVH> ScopeRecords; 00343 00344 /// ScopeInlinedAtIdx - This is the index in ScopeInlinedAtRecords for an 00345 /// scope/inlined-at pair. 00346 DenseMap<std::pair<MDNode*, MDNode*>, int> ScopeInlinedAtIdx; 00347 00348 /// ScopeInlinedAtRecords - These are the actual mdnodes (in value handles) 00349 /// for an index. The ValueHandle ensures that ScopeINlinedAtIdx stays up 00350 /// to date. 00351 std::vector<std::pair<DebugRecVH, DebugRecVH> > ScopeInlinedAtRecords; 00352 00353 /// IntrinsicIDCache - Cache of intrinsic name (string) to numeric ID mappings 00354 /// requested in this context 00355 typedef DenseMap<const Function*, unsigned> IntrinsicIDCacheTy; 00356 IntrinsicIDCacheTy IntrinsicIDCache; 00357 00358 int getOrAddScopeRecordIdxEntry(MDNode *N, int ExistingIdx); 00359 int getOrAddScopeInlinedAtIdxEntry(MDNode *Scope, MDNode *IA,int ExistingIdx); 00360 00361 LLVMContextImpl(LLVMContext &C); 00362 ~LLVMContextImpl(); 00363 }; 00364 00365 } 00366 00367 #endif