1 //===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the opaque LLVMContextImpl. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "LLVMContextImpl.h" 14 #include "llvm/ADT/SetVector.h" 15 #include "llvm/IR/Module.h" 16 #include "llvm/IR/OptBisect.h" 17 #include "llvm/IR/Type.h" 18 #include "llvm/Support/CommandLine.h" 19 #include "llvm/Support/ManagedStatic.h" 20 #include <cassert> 21 #include <utility> 22 23 using namespace llvm; 24 25 static cl::opt<bool> 26 ForceOpaquePointersCL("force-opaque-pointers", 27 cl::desc("Force all pointers to be opaque pointers"), 28 cl::init(false)); 29 30 LLVMContextImpl::LLVMContextImpl(LLVMContext &C) 31 : DiagHandler(std::make_unique<DiagnosticHandler>()), 32 VoidTy(C, Type::VoidTyID), LabelTy(C, Type::LabelTyID), 33 HalfTy(C, Type::HalfTyID), BFloatTy(C, Type::BFloatTyID), 34 FloatTy(C, Type::FloatTyID), DoubleTy(C, Type::DoubleTyID), 35 MetadataTy(C, Type::MetadataTyID), TokenTy(C, Type::TokenTyID), 36 X86_FP80Ty(C, Type::X86_FP80TyID), FP128Ty(C, Type::FP128TyID), 37 PPC_FP128Ty(C, Type::PPC_FP128TyID), X86_MMXTy(C, Type::X86_MMXTyID), 38 X86_AMXTy(C, Type::X86_AMXTyID), Int1Ty(C, 1), Int8Ty(C, 8), 39 Int16Ty(C, 16), Int32Ty(C, 32), Int64Ty(C, 64), Int128Ty(C, 128), 40 ForceOpaquePointers(ForceOpaquePointersCL) {} 41 42 LLVMContextImpl::~LLVMContextImpl() { 43 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor 44 // will call LLVMContextImpl::removeModule, thus invalidating iterators into 45 // the container. Avoid iterators during this operation: 46 while (!OwnedModules.empty()) 47 delete *OwnedModules.begin(); 48 49 #ifndef NDEBUG 50 // Check for metadata references from leaked Values. 51 for (auto &Pair : ValueMetadata) 52 Pair.first->dump(); 53 assert(ValueMetadata.empty() && "Values with metadata have been leaked"); 54 #endif 55 56 // Drop references for MDNodes. Do this before Values get deleted to avoid 57 // unnecessary RAUW when nodes are still unresolved. 58 for (auto *I : DistinctMDNodes) { 59 // We may have DIArgList that were uniqued, and as it has a custom 60 // implementation of dropAllReferences, it needs to be explicitly invoked. 61 if (auto *AL = dyn_cast<DIArgList>(I)) { 62 AL->dropAllReferences(); 63 continue; 64 } 65 I->dropAllReferences(); 66 } 67 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ 68 for (auto *I : CLASS##s) \ 69 I->dropAllReferences(); 70 #include "llvm/IR/Metadata.def" 71 72 // Also drop references that come from the Value bridges. 73 for (auto &Pair : ValuesAsMetadata) 74 Pair.second->dropUsers(); 75 for (auto &Pair : MetadataAsValues) 76 Pair.second->dropUse(); 77 78 // Destroy MDNodes. 79 for (MDNode *I : DistinctMDNodes) 80 I->deleteAsSubclass(); 81 #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \ 82 for (CLASS * I : CLASS##s) \ 83 delete I; 84 #include "llvm/IR/Metadata.def" 85 86 // Free the constants. 87 for (auto *I : ExprConstants) 88 I->dropAllReferences(); 89 for (auto *I : ArrayConstants) 90 I->dropAllReferences(); 91 for (auto *I : StructConstants) 92 I->dropAllReferences(); 93 for (auto *I : VectorConstants) 94 I->dropAllReferences(); 95 ExprConstants.freeConstants(); 96 ArrayConstants.freeConstants(); 97 StructConstants.freeConstants(); 98 VectorConstants.freeConstants(); 99 InlineAsms.freeConstants(); 100 101 CAZConstants.clear(); 102 CPNConstants.clear(); 103 UVConstants.clear(); 104 PVConstants.clear(); 105 IntConstants.clear(); 106 FPConstants.clear(); 107 CDSConstants.clear(); 108 109 // Destroy attribute node lists. 110 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(), 111 E = AttrsSetNodes.end(); I != E; ) { 112 FoldingSetIterator<AttributeSetNode> Elem = I++; 113 delete &*Elem; 114 } 115 116 // Destroy MetadataAsValues. 117 { 118 SmallVector<MetadataAsValue *, 8> MDVs; 119 MDVs.reserve(MetadataAsValues.size()); 120 for (auto &Pair : MetadataAsValues) 121 MDVs.push_back(Pair.second); 122 MetadataAsValues.clear(); 123 for (auto *V : MDVs) 124 delete V; 125 } 126 127 // Destroy ValuesAsMetadata. 128 for (auto &Pair : ValuesAsMetadata) 129 delete Pair.second; 130 } 131 132 void LLVMContextImpl::dropTriviallyDeadConstantArrays() { 133 SmallSetVector<ConstantArray *, 4> WorkList; 134 135 // When ArrayConstants are of substantial size and only a few in them are 136 // dead, starting WorkList with all elements of ArrayConstants can be 137 // wasteful. Instead, starting WorkList with only elements that have empty 138 // uses. 139 for (ConstantArray *C : ArrayConstants) 140 if (C->use_empty()) 141 WorkList.insert(C); 142 143 while (!WorkList.empty()) { 144 ConstantArray *C = WorkList.pop_back_val(); 145 if (C->use_empty()) { 146 for (const Use &Op : C->operands()) { 147 if (auto *COp = dyn_cast<ConstantArray>(Op)) 148 WorkList.insert(COp); 149 } 150 C->destroyConstant(); 151 } 152 } 153 } 154 155 void Module::dropTriviallyDeadConstantArrays() { 156 Context.pImpl->dropTriviallyDeadConstantArrays(); 157 } 158 159 namespace llvm { 160 161 /// Make MDOperand transparent for hashing. 162 /// 163 /// This overload of an implementation detail of the hashing library makes 164 /// MDOperand hash to the same value as a \a Metadata pointer. 165 /// 166 /// Note that overloading \a hash_value() as follows: 167 /// 168 /// \code 169 /// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); } 170 /// \endcode 171 /// 172 /// does not cause MDOperand to be transparent. In particular, a bare pointer 173 /// doesn't get hashed before it's combined, whereas \a MDOperand would. 174 static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); } 175 176 } // end namespace llvm 177 178 unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) { 179 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end()); 180 #ifndef NDEBUG 181 { 182 SmallVector<Metadata *, 8> MDs(drop_begin(N->operands(), Offset)); 183 unsigned RawHash = calculateHash(MDs); 184 assert(Hash == RawHash && 185 "Expected hash of MDOperand to equal hash of Metadata*"); 186 } 187 #endif 188 return Hash; 189 } 190 191 unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) { 192 return hash_combine_range(Ops.begin(), Ops.end()); 193 } 194 195 StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) { 196 uint32_t NewIdx = BundleTagCache.size(); 197 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first); 198 } 199 200 void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const { 201 Tags.resize(BundleTagCache.size()); 202 for (const auto &T : BundleTagCache) 203 Tags[T.second] = T.first(); 204 } 205 206 uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const { 207 auto I = BundleTagCache.find(Tag); 208 assert(I != BundleTagCache.end() && "Unknown tag!"); 209 return I->second; 210 } 211 212 SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) { 213 auto NewSSID = SSC.size(); 214 assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() && 215 "Hit the maximum number of synchronization scopes allowed!"); 216 return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second; 217 } 218 219 void LLVMContextImpl::getSyncScopeNames( 220 SmallVectorImpl<StringRef> &SSNs) const { 221 SSNs.resize(SSC.size()); 222 for (const auto &SSE : SSC) 223 SSNs[SSE.second] = SSE.first(); 224 } 225 226 /// Gets the OptPassGate for this LLVMContextImpl, which defaults to the 227 /// singleton OptBisect if not explicitly set. 228 OptPassGate &LLVMContextImpl::getOptPassGate() const { 229 if (!OPG) 230 OPG = &(*OptBisector); 231 return *OPG; 232 } 233 234 void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) { 235 this->OPG = &OPG; 236 } 237