1 //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===// 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 implements LLVMContext, as a wrapper around the opaque 11 // class LLVMContextImpl. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "llvm/IR/LLVMContext.h" 16 #include "LLVMContextImpl.h" 17 #include "llvm/IR/Constants.h" 18 #include "llvm/IR/DebugLoc.h" 19 #include "llvm/IR/DiagnosticInfo.h" 20 #include "llvm/IR/DiagnosticPrinter.h" 21 #include "llvm/IR/Instruction.h" 22 #include "llvm/IR/Metadata.h" 23 #include "llvm/Support/ManagedStatic.h" 24 #include "llvm/Support/SourceMgr.h" 25 #include <cctype> 26 using namespace llvm; 27 28 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { 29 // Create the fixed metadata kinds. This is done in the same order as the 30 // MD_* enum values so that they correspond. 31 32 // Create the 'dbg' metadata kind. 33 unsigned DbgID = getMDKindID("dbg"); 34 assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID; 35 36 // Create the 'tbaa' metadata kind. 37 unsigned TBAAID = getMDKindID("tbaa"); 38 assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID; 39 40 // Create the 'prof' metadata kind. 41 unsigned ProfID = getMDKindID("prof"); 42 assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID; 43 44 // Create the 'fpmath' metadata kind. 45 unsigned FPAccuracyID = getMDKindID("fpmath"); 46 assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted"); 47 (void)FPAccuracyID; 48 49 // Create the 'range' metadata kind. 50 unsigned RangeID = getMDKindID("range"); 51 assert(RangeID == MD_range && "range kind id drifted"); 52 (void)RangeID; 53 54 // Create the 'tbaa.struct' metadata kind. 55 unsigned TBAAStructID = getMDKindID("tbaa.struct"); 56 assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted"); 57 (void)TBAAStructID; 58 59 // Create the 'invariant.load' metadata kind. 60 unsigned InvariantLdId = getMDKindID("invariant.load"); 61 assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted"); 62 (void)InvariantLdId; 63 64 // Create the 'alias.scope' metadata kind. 65 unsigned AliasScopeID = getMDKindID("alias.scope"); 66 assert(AliasScopeID == MD_alias_scope && "alias.scope kind id drifted"); 67 (void)AliasScopeID; 68 69 // Create the 'noalias' metadata kind. 70 unsigned NoAliasID = getMDKindID("noalias"); 71 assert(NoAliasID == MD_noalias && "noalias kind id drifted"); 72 (void)NoAliasID; 73 74 // Create the 'nontemporal' metadata kind. 75 unsigned NonTemporalID = getMDKindID("nontemporal"); 76 assert(NonTemporalID == MD_nontemporal && "nontemporal kind id drifted"); 77 (void)NonTemporalID; 78 79 // Create the 'llvm.mem.parallel_loop_access' metadata kind. 80 unsigned MemParallelLoopAccessID = getMDKindID("llvm.mem.parallel_loop_access"); 81 assert(MemParallelLoopAccessID == MD_mem_parallel_loop_access && 82 "mem_parallel_loop_access kind id drifted"); 83 (void)MemParallelLoopAccessID; 84 85 // Create the 'nonnull' metadata kind. 86 unsigned NonNullID = getMDKindID("nonnull"); 87 assert(NonNullID == MD_nonnull && "nonnull kind id drifted"); 88 (void)NonNullID; 89 90 // Create the 'dereferenceable' metadata kind. 91 unsigned DereferenceableID = getMDKindID("dereferenceable"); 92 assert(DereferenceableID == MD_dereferenceable && 93 "dereferenceable kind id drifted"); 94 (void)DereferenceableID; 95 96 // Create the 'dereferenceable_or_null' metadata kind. 97 unsigned DereferenceableOrNullID = getMDKindID("dereferenceable_or_null"); 98 assert(DereferenceableOrNullID == MD_dereferenceable_or_null && 99 "dereferenceable_or_null kind id drifted"); 100 (void)DereferenceableOrNullID; 101 102 // Create the 'make.implicit' metadata kind. 103 unsigned MakeImplicitID = getMDKindID("make.implicit"); 104 assert(MakeImplicitID == MD_make_implicit && 105 "make.implicit kind id drifted"); 106 (void)MakeImplicitID; 107 108 // Create the 'unpredictable' metadata kind. 109 unsigned UnpredictableID = getMDKindID("unpredictable"); 110 assert(UnpredictableID == MD_unpredictable && 111 "unpredictable kind id drifted"); 112 (void)UnpredictableID; 113 114 // Create the 'invariant.group' metadata kind. 115 unsigned InvariantGroupId = getMDKindID("invariant.group"); 116 assert(InvariantGroupId == MD_invariant_group && 117 "invariant.group kind id drifted"); 118 (void)InvariantGroupId; 119 120 // Create the 'align' metadata kind. 121 unsigned AlignID = getMDKindID("align"); 122 assert(AlignID == MD_align && "align kind id drifted"); 123 (void)AlignID; 124 125 // Create the 'llvm.loop' metadata kind. 126 unsigned LoopID = getMDKindID("llvm.loop"); 127 assert(LoopID == MD_loop && "llvm.loop kind id drifted"); 128 (void)LoopID; 129 130 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt"); 131 assert(DeoptEntry->second == LLVMContext::OB_deopt && 132 "deopt operand bundle id drifted!"); 133 (void)DeoptEntry; 134 135 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet"); 136 assert(FuncletEntry->second == LLVMContext::OB_funclet && 137 "funclet operand bundle id drifted!"); 138 (void)FuncletEntry; 139 140 auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition"); 141 assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition && 142 "gc-transition operand bundle id drifted!"); 143 (void)GCTransitionEntry; 144 } 145 LLVMContext::~LLVMContext() { delete pImpl; } 146 147 void LLVMContext::addModule(Module *M) { 148 pImpl->OwnedModules.insert(M); 149 } 150 151 void LLVMContext::removeModule(Module *M) { 152 pImpl->OwnedModules.erase(M); 153 } 154 155 //===----------------------------------------------------------------------===// 156 // Recoverable Backend Errors 157 //===----------------------------------------------------------------------===// 158 159 void LLVMContext:: 160 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, 161 void *DiagContext) { 162 pImpl->InlineAsmDiagHandler = DiagHandler; 163 pImpl->InlineAsmDiagContext = DiagContext; 164 } 165 166 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by 167 /// setInlineAsmDiagnosticHandler. 168 LLVMContext::InlineAsmDiagHandlerTy 169 LLVMContext::getInlineAsmDiagnosticHandler() const { 170 return pImpl->InlineAsmDiagHandler; 171 } 172 173 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by 174 /// setInlineAsmDiagnosticHandler. 175 void *LLVMContext::getInlineAsmDiagnosticContext() const { 176 return pImpl->InlineAsmDiagContext; 177 } 178 179 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler, 180 void *DiagnosticContext, 181 bool RespectFilters) { 182 pImpl->DiagnosticHandler = DiagnosticHandler; 183 pImpl->DiagnosticContext = DiagnosticContext; 184 pImpl->RespectDiagnosticFilters = RespectFilters; 185 } 186 187 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const { 188 return pImpl->DiagnosticHandler; 189 } 190 191 void *LLVMContext::getDiagnosticContext() const { 192 return pImpl->DiagnosticContext; 193 } 194 195 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle) 196 { 197 pImpl->YieldCallback = Callback; 198 pImpl->YieldOpaqueHandle = OpaqueHandle; 199 } 200 201 void LLVMContext::yield() { 202 if (pImpl->YieldCallback) 203 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle); 204 } 205 206 void LLVMContext::emitError(const Twine &ErrorStr) { 207 diagnose(DiagnosticInfoInlineAsm(ErrorStr)); 208 } 209 210 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { 211 assert (I && "Invalid instruction"); 212 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr)); 213 } 214 215 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) { 216 // Optimization remarks are selective. They need to check whether the regexp 217 // pattern, passed via one of the -pass-remarks* flags, matches the name of 218 // the pass that is emitting the diagnostic. If there is no match, ignore the 219 // diagnostic and return. 220 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) 221 return Remark->isEnabled(); 222 223 return true; 224 } 225 226 static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity) { 227 switch (Severity) { 228 case DS_Error: 229 return "error"; 230 case DS_Warning: 231 return "warning"; 232 case DS_Remark: 233 return "remark"; 234 case DS_Note: 235 return "note"; 236 } 237 llvm_unreachable("Unknown DiagnosticSeverity"); 238 } 239 240 void LLVMContext::diagnose(const DiagnosticInfo &DI) { 241 // If there is a report handler, use it. 242 if (pImpl->DiagnosticHandler) { 243 if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) 244 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext); 245 return; 246 } 247 248 if (!isDiagnosticEnabled(DI)) 249 return; 250 251 // Otherwise, print the message with a prefix based on the severity. 252 DiagnosticPrinterRawOStream DP(errs()); 253 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": "; 254 DI.print(DP); 255 errs() << "\n"; 256 if (DI.getSeverity() == DS_Error) 257 exit(1); 258 } 259 260 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { 261 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr)); 262 } 263 264 //===----------------------------------------------------------------------===// 265 // Metadata Kind Uniquing 266 //===----------------------------------------------------------------------===// 267 268 /// Return a unique non-zero ID for the specified metadata kind. 269 unsigned LLVMContext::getMDKindID(StringRef Name) const { 270 // If this is new, assign it its ID. 271 return pImpl->CustomMDKindNames.insert( 272 std::make_pair( 273 Name, pImpl->CustomMDKindNames.size())) 274 .first->second; 275 } 276 277 /// getHandlerNames - Populate client-supplied smallvector using custom 278 /// metadata name and ID. 279 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { 280 Names.resize(pImpl->CustomMDKindNames.size()); 281 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), 282 E = pImpl->CustomMDKindNames.end(); I != E; ++I) 283 Names[I->second] = I->first(); 284 } 285 286 void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const { 287 pImpl->getOperandBundleTags(Tags); 288 } 289 290 uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const { 291 return pImpl->getOperandBundleTagID(Tag); 292 } 293 294 void LLVMContext::setGC(const Function &Fn, std::string GCName) { 295 auto It = pImpl->GCNames.find(&Fn); 296 297 if (It == pImpl->GCNames.end()) { 298 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName))); 299 return; 300 } 301 It->second = std::move(GCName); 302 } 303 const std::string &LLVMContext::getGC(const Function &Fn) { 304 return pImpl->GCNames[&Fn]; 305 } 306 void LLVMContext::deleteGC(const Function &Fn) { 307 pImpl->GCNames.erase(&Fn); 308 } 309 310 bool LLVMContext::shouldDiscardValueNames() const { 311 return pImpl->DiscardValueNames; 312 } 313 314 bool LLVMContext::hasDITypeMap() const { return !!pImpl->DITypeMap; } 315 316 void LLVMContext::ensureDITypeMap() { 317 if (pImpl->DITypeMap) 318 return; 319 320 pImpl->DITypeMap = llvm::make_unique<DenseMap<const MDString *, DIType *>>(); 321 } 322 323 void LLVMContext::destroyDITypeMap() { pImpl->DITypeMap.reset(); } 324 325 DIType **LLVMContext::getOrInsertDITypeMapping(const MDString &S) { 326 if (!hasDITypeMap()) 327 return nullptr; 328 return &(*pImpl->DITypeMap)[&S]; 329 } 330 331 void LLVMContext::setDiscardValueNames(bool Discard) { 332 pImpl->DiscardValueNames = Discard; 333 } 334