1ef860a24SChandler Carruth //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===// 2ef860a24SChandler Carruth // 3ef860a24SChandler Carruth // The LLVM Compiler Infrastructure 4ef860a24SChandler Carruth // 5ef860a24SChandler Carruth // This file is distributed under the University of Illinois Open Source 6ef860a24SChandler Carruth // License. See LICENSE.TXT for details. 7ef860a24SChandler Carruth // 8ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 9ef860a24SChandler Carruth // 10ef860a24SChandler Carruth // This file implements LLVMContext, as a wrapper around the opaque 11ef860a24SChandler Carruth // class LLVMContextImpl. 12ef860a24SChandler Carruth // 13ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 14ef860a24SChandler Carruth 159fb823bbSChandler Carruth #include "llvm/IR/LLVMContext.h" 166bda14b3SChandler Carruth #include "LLVMContextImpl.h" 175354a8aaSEugene Zelenko #include "llvm/ADT/SmallVector.h" 185354a8aaSEugene Zelenko #include "llvm/ADT/StringMap.h" 195354a8aaSEugene Zelenko #include "llvm/ADT/StringRef.h" 205354a8aaSEugene Zelenko #include "llvm/ADT/Twine.h" 21b4c44d23SQuentin Colombet #include "llvm/IR/DiagnosticInfo.h" 22b4c44d23SQuentin Colombet #include "llvm/IR/DiagnosticPrinter.h" 239fb823bbSChandler Carruth #include "llvm/IR/Metadata.h" 245354a8aaSEugene Zelenko #include "llvm/IR/Module.h" 255354a8aaSEugene Zelenko #include "llvm/Support/Casting.h" 265354a8aaSEugene Zelenko #include "llvm/Support/ErrorHandling.h" 275354a8aaSEugene Zelenko #include "llvm/Support/raw_ostream.h" 285354a8aaSEugene Zelenko #include <cassert> 295354a8aaSEugene Zelenko #include <cstdlib> 305354a8aaSEugene Zelenko #include <string> 315354a8aaSEugene Zelenko #include <utility> 325354a8aaSEugene Zelenko 33ef860a24SChandler Carruth using namespace llvm; 34ef860a24SChandler Carruth 35ef860a24SChandler Carruth LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { 36ef860a24SChandler Carruth // Create the fixed metadata kinds. This is done in the same order as the 37ef860a24SChandler Carruth // MD_* enum values so that they correspond. 386f0b4f2eSPeter Collingbourne std::pair<unsigned, StringRef> MDKinds[] = { 396f0b4f2eSPeter Collingbourne {MD_dbg, "dbg"}, 406f0b4f2eSPeter Collingbourne {MD_tbaa, "tbaa"}, 416f0b4f2eSPeter Collingbourne {MD_prof, "prof"}, 426f0b4f2eSPeter Collingbourne {MD_fpmath, "fpmath"}, 436f0b4f2eSPeter Collingbourne {MD_range, "range"}, 446f0b4f2eSPeter Collingbourne {MD_tbaa_struct, "tbaa.struct"}, 456f0b4f2eSPeter Collingbourne {MD_invariant_load, "invariant.load"}, 466f0b4f2eSPeter Collingbourne {MD_alias_scope, "alias.scope"}, 476f0b4f2eSPeter Collingbourne {MD_noalias, "noalias"}, 486f0b4f2eSPeter Collingbourne {MD_nontemporal, "nontemporal"}, 496f0b4f2eSPeter Collingbourne {MD_mem_parallel_loop_access, "llvm.mem.parallel_loop_access"}, 506f0b4f2eSPeter Collingbourne {MD_nonnull, "nonnull"}, 516f0b4f2eSPeter Collingbourne {MD_dereferenceable, "dereferenceable"}, 526f0b4f2eSPeter Collingbourne {MD_dereferenceable_or_null, "dereferenceable_or_null"}, 536f0b4f2eSPeter Collingbourne {MD_make_implicit, "make.implicit"}, 546f0b4f2eSPeter Collingbourne {MD_unpredictable, "unpredictable"}, 556f0b4f2eSPeter Collingbourne {MD_invariant_group, "invariant.group"}, 566f0b4f2eSPeter Collingbourne {MD_align, "align"}, 576f0b4f2eSPeter Collingbourne {MD_loop, "llvm.loop"}, 586f0b4f2eSPeter Collingbourne {MD_type, "type"}, 596f0b4f2eSPeter Collingbourne {MD_section_prefix, "section_prefix"}, 60235c275bSPeter Collingbourne {MD_absolute_symbol, "absolute_symbol"}, 6151c962f7SEvgeniy Stepanov {MD_associated, "associated"}, 626f0b4f2eSPeter Collingbourne }; 63ef860a24SChandler Carruth 646f0b4f2eSPeter Collingbourne for (auto &MDKind : MDKinds) { 656f0b4f2eSPeter Collingbourne unsigned ID = getMDKindID(MDKind.second); 666f0b4f2eSPeter Collingbourne assert(ID == MDKind.first && "metadata kind id drifted"); 676f0b4f2eSPeter Collingbourne (void)ID; 686f0b4f2eSPeter Collingbourne } 69302b69c9SDehao Chen 70cdafd849SSanjoy Das auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt"); 71cdafd849SSanjoy Das assert(DeoptEntry->second == LLVMContext::OB_deopt && 72cdafd849SSanjoy Das "deopt operand bundle id drifted!"); 73cdafd849SSanjoy Das (void)DeoptEntry; 743bb88c02SDavid Majnemer 753bb88c02SDavid Majnemer auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet"); 763bb88c02SDavid Majnemer assert(FuncletEntry->second == LLVMContext::OB_funclet && 773bb88c02SDavid Majnemer "funclet operand bundle id drifted!"); 783bb88c02SDavid Majnemer (void)FuncletEntry; 79a34ce95bSSanjoy Das 80a34ce95bSSanjoy Das auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition"); 81a34ce95bSSanjoy Das assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition && 82a34ce95bSSanjoy Das "gc-transition operand bundle id drifted!"); 83a34ce95bSSanjoy Das (void)GCTransitionEntry; 84ef860a24SChandler Carruth } 855354a8aaSEugene Zelenko 86ef860a24SChandler Carruth LLVMContext::~LLVMContext() { delete pImpl; } 87ef860a24SChandler Carruth 88ef860a24SChandler Carruth void LLVMContext::addModule(Module *M) { 89ef860a24SChandler Carruth pImpl->OwnedModules.insert(M); 90ef860a24SChandler Carruth } 91ef860a24SChandler Carruth 92ef860a24SChandler Carruth void LLVMContext::removeModule(Module *M) { 93ef860a24SChandler Carruth pImpl->OwnedModules.erase(M); 94ef860a24SChandler Carruth } 95ef860a24SChandler Carruth 96ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 97ef860a24SChandler Carruth // Recoverable Backend Errors 98ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 99ef860a24SChandler Carruth 100a594fab4SBob Wilson void LLVMContext:: 101a594fab4SBob Wilson setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, 102ef860a24SChandler Carruth void *DiagContext) { 103a594fab4SBob Wilson pImpl->InlineAsmDiagHandler = DiagHandler; 104a594fab4SBob Wilson pImpl->InlineAsmDiagContext = DiagContext; 105ef860a24SChandler Carruth } 106ef860a24SChandler Carruth 107a594fab4SBob Wilson /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by 108a594fab4SBob Wilson /// setInlineAsmDiagnosticHandler. 109a594fab4SBob Wilson LLVMContext::InlineAsmDiagHandlerTy 110a594fab4SBob Wilson LLVMContext::getInlineAsmDiagnosticHandler() const { 111a594fab4SBob Wilson return pImpl->InlineAsmDiagHandler; 112ef860a24SChandler Carruth } 113ef860a24SChandler Carruth 114a594fab4SBob Wilson /// getInlineAsmDiagnosticContext - Return the diagnostic context set by 115a594fab4SBob Wilson /// setInlineAsmDiagnosticHandler. 116a594fab4SBob Wilson void *LLVMContext::getInlineAsmDiagnosticContext() const { 117a594fab4SBob Wilson return pImpl->InlineAsmDiagContext; 118ef860a24SChandler Carruth } 119ef860a24SChandler Carruth 120b4c44d23SQuentin Colombet void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler, 12130c9242cSDuncan P. N. Exon Smith void *DiagnosticContext, 12230c9242cSDuncan P. N. Exon Smith bool RespectFilters) { 123b4c44d23SQuentin Colombet pImpl->DiagnosticHandler = DiagnosticHandler; 124b4c44d23SQuentin Colombet pImpl->DiagnosticContext = DiagnosticContext; 12530c9242cSDuncan P. N. Exon Smith pImpl->RespectDiagnosticFilters = RespectFilters; 126b4c44d23SQuentin Colombet } 127b4c44d23SQuentin Colombet 12844e5f6c4SBrian Gesiak void LLVMContext::setDiagnosticsHotnessRequested(bool Requested) { 12944e5f6c4SBrian Gesiak pImpl->DiagnosticsHotnessRequested = Requested; 13044e5f6c4SBrian Gesiak } 13144e5f6c4SBrian Gesiak bool LLVMContext::getDiagnosticsHotnessRequested() const { 13244e5f6c4SBrian Gesiak return pImpl->DiagnosticsHotnessRequested; 133aad81608SAdam Nemet } 134aad81608SAdam Nemet 135*4ef3daafSBrian Gesiak void LLVMContext::setDiagnosticsHotnessThreshold(uint64_t Threshold) { 136*4ef3daafSBrian Gesiak pImpl->DiagnosticsHotnessThreshold = Threshold; 137*4ef3daafSBrian Gesiak } 138*4ef3daafSBrian Gesiak uint64_t LLVMContext::getDiagnosticsHotnessThreshold() const { 139*4ef3daafSBrian Gesiak return pImpl->DiagnosticsHotnessThreshold; 140*4ef3daafSBrian Gesiak } 141*4ef3daafSBrian Gesiak 142a62b7e1aSAdam Nemet yaml::Output *LLVMContext::getDiagnosticsOutputFile() { 143a62b7e1aSAdam Nemet return pImpl->DiagnosticsOutputFile.get(); 144a62b7e1aSAdam Nemet } 145a62b7e1aSAdam Nemet 1466f408368SMehdi Amini void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) { 1476f408368SMehdi Amini pImpl->DiagnosticsOutputFile = std::move(F); 148a62b7e1aSAdam Nemet } 149a62b7e1aSAdam Nemet 150b4c44d23SQuentin Colombet LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const { 151b4c44d23SQuentin Colombet return pImpl->DiagnosticHandler; 152b4c44d23SQuentin Colombet } 153b4c44d23SQuentin Colombet 154b4c44d23SQuentin Colombet void *LLVMContext::getDiagnosticContext() const { 155b4c44d23SQuentin Colombet return pImpl->DiagnosticContext; 156b4c44d23SQuentin Colombet } 157b4c44d23SQuentin Colombet 15834390c70SJuergen Ributzka void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle) 15934390c70SJuergen Ributzka { 16034390c70SJuergen Ributzka pImpl->YieldCallback = Callback; 16134390c70SJuergen Ributzka pImpl->YieldOpaqueHandle = OpaqueHandle; 16234390c70SJuergen Ributzka } 16334390c70SJuergen Ributzka 16434390c70SJuergen Ributzka void LLVMContext::yield() { 16534390c70SJuergen Ributzka if (pImpl->YieldCallback) 16634390c70SJuergen Ributzka pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle); 16734390c70SJuergen Ributzka } 16834390c70SJuergen Ributzka 169ef860a24SChandler Carruth void LLVMContext::emitError(const Twine &ErrorStr) { 170dec8d55bSQuentin Colombet diagnose(DiagnosticInfoInlineAsm(ErrorStr)); 171ef860a24SChandler Carruth } 172ef860a24SChandler Carruth 173bfb44ef9SBob Wilson void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { 174dec8d55bSQuentin Colombet assert (I && "Invalid instruction"); 175dec8d55bSQuentin Colombet diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr)); 176ef860a24SChandler Carruth } 177ef860a24SChandler Carruth 17830c9242cSDuncan P. N. Exon Smith static bool isDiagnosticEnabled(const DiagnosticInfo &DI) { 1797f8af8bfSDiego Novillo // Optimization remarks are selective. They need to check whether the regexp 1807f8af8bfSDiego Novillo // pattern, passed via one of the -pass-remarks* flags, matches the name of 1817f8af8bfSDiego Novillo // the pass that is emitting the diagnostic. If there is no match, ignore the 1827f8af8bfSDiego Novillo // diagnostic and return. 183e9148dd6SAkira Hatanaka if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) 184e9148dd6SAkira Hatanaka return Remark->isEnabled(); 185e9148dd6SAkira Hatanaka 18630c9242cSDuncan P. N. Exon Smith return true; 18730c9242cSDuncan P. N. Exon Smith } 18830c9242cSDuncan P. N. Exon Smith 1894b9c0d4dSRenato Golin const char * 1904b9c0d4dSRenato Golin LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) { 191735c47ecSAlex Lorenz switch (Severity) { 192735c47ecSAlex Lorenz case DS_Error: 193735c47ecSAlex Lorenz return "error"; 194735c47ecSAlex Lorenz case DS_Warning: 195735c47ecSAlex Lorenz return "warning"; 196735c47ecSAlex Lorenz case DS_Remark: 197735c47ecSAlex Lorenz return "remark"; 198735c47ecSAlex Lorenz case DS_Note: 199735c47ecSAlex Lorenz return "note"; 200735c47ecSAlex Lorenz } 2015220476aSAaron Ballman llvm_unreachable("Unknown DiagnosticSeverity"); 202735c47ecSAlex Lorenz } 203735c47ecSAlex Lorenz 20430c9242cSDuncan P. N. Exon Smith void LLVMContext::diagnose(const DiagnosticInfo &DI) { 20530c9242cSDuncan P. N. Exon Smith // If there is a report handler, use it. 20630c9242cSDuncan P. N. Exon Smith if (pImpl->DiagnosticHandler) { 20730c9242cSDuncan P. N. Exon Smith if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) 20830c9242cSDuncan P. N. Exon Smith pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext); 20930c9242cSDuncan P. N. Exon Smith return; 21030c9242cSDuncan P. N. Exon Smith } 21130c9242cSDuncan P. N. Exon Smith 21230c9242cSDuncan P. N. Exon Smith if (!isDiagnosticEnabled(DI)) 21330c9242cSDuncan P. N. Exon Smith return; 214df655013SDiego Novillo 215b4c44d23SQuentin Colombet // Otherwise, print the message with a prefix based on the severity. 216735c47ecSAlex Lorenz DiagnosticPrinterRawOStream DP(errs()); 217735c47ecSAlex Lorenz errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": "; 218b4c44d23SQuentin Colombet DI.print(DP); 219735c47ecSAlex Lorenz errs() << "\n"; 220735c47ecSAlex Lorenz if (DI.getSeverity() == DS_Error) 221b4c44d23SQuentin Colombet exit(1); 222b4c44d23SQuentin Colombet } 223b4c44d23SQuentin Colombet 224ef860a24SChandler Carruth void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { 225dec8d55bSQuentin Colombet diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr)); 226ef860a24SChandler Carruth } 227ef860a24SChandler Carruth 228ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 229ef860a24SChandler Carruth // Metadata Kind Uniquing 230ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 231ef860a24SChandler Carruth 2321ac0d2deSFilipe Cabecinhas /// Return a unique non-zero ID for the specified metadata kind. 233ef860a24SChandler Carruth unsigned LLVMContext::getMDKindID(StringRef Name) const { 234ef860a24SChandler Carruth // If this is new, assign it its ID. 2351ac0d2deSFilipe Cabecinhas return pImpl->CustomMDKindNames.insert( 2361ac0d2deSFilipe Cabecinhas std::make_pair( 2371ac0d2deSFilipe Cabecinhas Name, pImpl->CustomMDKindNames.size())) 2385106ce78SDavid Blaikie .first->second; 239ef860a24SChandler Carruth } 240ef860a24SChandler Carruth 2414104337dSSanjay Patel /// getHandlerNames - Populate client-supplied smallvector using custom 242ef860a24SChandler Carruth /// metadata name and ID. 243ef860a24SChandler Carruth void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { 244ef860a24SChandler Carruth Names.resize(pImpl->CustomMDKindNames.size()); 245ef860a24SChandler Carruth for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), 246ef860a24SChandler Carruth E = pImpl->CustomMDKindNames.end(); I != E; ++I) 247ef860a24SChandler Carruth Names[I->second] = I->first(); 248ef860a24SChandler Carruth } 2499303c246SSanjoy Das 2509303c246SSanjoy Das void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const { 2519303c246SSanjoy Das pImpl->getOperandBundleTags(Tags); 2529303c246SSanjoy Das } 2539303c246SSanjoy Das 2549303c246SSanjoy Das uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const { 2559303c246SSanjoy Das return pImpl->getOperandBundleTagID(Tag); 2569303c246SSanjoy Das } 257599ebf27SMehdi Amini 258599ebf27SMehdi Amini void LLVMContext::setGC(const Function &Fn, std::string GCName) { 259599ebf27SMehdi Amini auto It = pImpl->GCNames.find(&Fn); 260599ebf27SMehdi Amini 261599ebf27SMehdi Amini if (It == pImpl->GCNames.end()) { 262599ebf27SMehdi Amini pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName))); 263599ebf27SMehdi Amini return; 264599ebf27SMehdi Amini } 265599ebf27SMehdi Amini It->second = std::move(GCName); 266599ebf27SMehdi Amini } 2675354a8aaSEugene Zelenko 268599ebf27SMehdi Amini const std::string &LLVMContext::getGC(const Function &Fn) { 269599ebf27SMehdi Amini return pImpl->GCNames[&Fn]; 270599ebf27SMehdi Amini } 2715354a8aaSEugene Zelenko 272599ebf27SMehdi Amini void LLVMContext::deleteGC(const Function &Fn) { 273599ebf27SMehdi Amini pImpl->GCNames.erase(&Fn); 274599ebf27SMehdi Amini } 27509b4a8daSMehdi Amini 276e7090155SMehdi Amini bool LLVMContext::shouldDiscardValueNames() const { 277e7090155SMehdi Amini return pImpl->DiscardValueNames; 278e7090155SMehdi Amini } 27909b4a8daSMehdi Amini 280ed8fdb2aSDuncan P. N. Exon Smith bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; } 2815ab2be09SDuncan P. N. Exon Smith 282ed8fdb2aSDuncan P. N. Exon Smith void LLVMContext::enableDebugTypeODRUniquing() { 2835ab2be09SDuncan P. N. Exon Smith if (pImpl->DITypeMap) 2845ab2be09SDuncan P. N. Exon Smith return; 2855ab2be09SDuncan P. N. Exon Smith 286e8b555c6SDuncan P. N. Exon Smith pImpl->DITypeMap.emplace(); 2875ab2be09SDuncan P. N. Exon Smith } 2885ab2be09SDuncan P. N. Exon Smith 289ed8fdb2aSDuncan P. N. Exon Smith void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); } 2905ab2be09SDuncan P. N. Exon Smith 29109b4a8daSMehdi Amini void LLVMContext::setDiscardValueNames(bool Discard) { 29209b4a8daSMehdi Amini pImpl->DiscardValueNames = Discard; 29309b4a8daSMehdi Amini } 294aa641a51SAndrew Kaylor 295aa641a51SAndrew Kaylor OptBisect &LLVMContext::getOptBisect() { 296aa641a51SAndrew Kaylor return pImpl->getOptBisect(); 297aa641a51SAndrew Kaylor } 298