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" 16ef860a24SChandler Carruth #include "LLVMContextImpl.h" 179fb823bbSChandler Carruth #include "llvm/IR/Constants.h" 18a9298b22SDiego Novillo #include "llvm/IR/DebugLoc.h" 19b4c44d23SQuentin Colombet #include "llvm/IR/DiagnosticInfo.h" 20b4c44d23SQuentin Colombet #include "llvm/IR/DiagnosticPrinter.h" 219fb823bbSChandler Carruth #include "llvm/IR/Instruction.h" 229fb823bbSChandler Carruth #include "llvm/IR/Metadata.h" 23ef860a24SChandler Carruth #include "llvm/Support/ManagedStatic.h" 24ef860a24SChandler Carruth #include "llvm/Support/SourceMgr.h" 25ef860a24SChandler Carruth #include <cctype> 26ef860a24SChandler Carruth using namespace llvm; 27ef860a24SChandler Carruth 28ef860a24SChandler Carruth static ManagedStatic<LLVMContext> GlobalContext; 29ef860a24SChandler Carruth 30ef860a24SChandler Carruth LLVMContext& llvm::getGlobalContext() { 31ef860a24SChandler Carruth return *GlobalContext; 32ef860a24SChandler Carruth } 33ef860a24SChandler Carruth 34ef860a24SChandler Carruth LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { 35ef860a24SChandler Carruth // Create the fixed metadata kinds. This is done in the same order as the 36ef860a24SChandler Carruth // MD_* enum values so that they correspond. 37ef860a24SChandler Carruth 38ef860a24SChandler Carruth // Create the 'dbg' metadata kind. 39ef860a24SChandler Carruth unsigned DbgID = getMDKindID("dbg"); 40ef860a24SChandler Carruth assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID; 41ef860a24SChandler Carruth 42ef860a24SChandler Carruth // Create the 'tbaa' metadata kind. 43ef860a24SChandler Carruth unsigned TBAAID = getMDKindID("tbaa"); 44ef860a24SChandler Carruth assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID; 45ef860a24SChandler Carruth 46ef860a24SChandler Carruth // Create the 'prof' metadata kind. 47ef860a24SChandler Carruth unsigned ProfID = getMDKindID("prof"); 48ef860a24SChandler Carruth assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID; 49ef860a24SChandler Carruth 50ef860a24SChandler Carruth // Create the 'fpmath' metadata kind. 51ef860a24SChandler Carruth unsigned FPAccuracyID = getMDKindID("fpmath"); 52ef860a24SChandler Carruth assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted"); 53ef860a24SChandler Carruth (void)FPAccuracyID; 54ef860a24SChandler Carruth 55ef860a24SChandler Carruth // Create the 'range' metadata kind. 56ef860a24SChandler Carruth unsigned RangeID = getMDKindID("range"); 57ef860a24SChandler Carruth assert(RangeID == MD_range && "range kind id drifted"); 58ef860a24SChandler Carruth (void)RangeID; 59ef860a24SChandler Carruth 60ef860a24SChandler Carruth // Create the 'tbaa.struct' metadata kind. 61ef860a24SChandler Carruth unsigned TBAAStructID = getMDKindID("tbaa.struct"); 62ef860a24SChandler Carruth assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted"); 63ef860a24SChandler Carruth (void)TBAAStructID; 64408bdad5SShuxin Yang 65408bdad5SShuxin Yang // Create the 'invariant.load' metadata kind. 66408bdad5SShuxin Yang unsigned InvariantLdId = getMDKindID("invariant.load"); 67408bdad5SShuxin Yang assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted"); 68408bdad5SShuxin Yang (void)InvariantLdId; 699414665aSHal Finkel 709414665aSHal Finkel // Create the 'alias.scope' metadata kind. 719414665aSHal Finkel unsigned AliasScopeID = getMDKindID("alias.scope"); 729414665aSHal Finkel assert(AliasScopeID == MD_alias_scope && "alias.scope kind id drifted"); 739414665aSHal Finkel (void)AliasScopeID; 749414665aSHal Finkel 759414665aSHal Finkel // Create the 'noalias' metadata kind. 769414665aSHal Finkel unsigned NoAliasID = getMDKindID("noalias"); 779414665aSHal Finkel assert(NoAliasID == MD_noalias && "noalias kind id drifted"); 789414665aSHal Finkel (void)NoAliasID; 795a3f5f75SPhilip Reames 805a3f5f75SPhilip Reames // Create the 'nontemporal' metadata kind. 815a3f5f75SPhilip Reames unsigned NonTemporalID = getMDKindID("nontemporal"); 825a3f5f75SPhilip Reames assert(NonTemporalID == MD_nontemporal && "nontemporal kind id drifted"); 835a3f5f75SPhilip Reames (void)NonTemporalID; 845a3f5f75SPhilip Reames 855a3f5f75SPhilip Reames // Create the 'llvm.mem.parallel_loop_access' metadata kind. 865a3f5f75SPhilip Reames unsigned MemParallelLoopAccessID = getMDKindID("llvm.mem.parallel_loop_access"); 875a3f5f75SPhilip Reames assert(MemParallelLoopAccessID == MD_mem_parallel_loop_access && 885a3f5f75SPhilip Reames "mem_parallel_loop_access kind id drifted"); 895a3f5f75SPhilip Reames (void)MemParallelLoopAccessID; 905a3f5f75SPhilip Reames 915a3f5f75SPhilip Reames // Create the 'nonnull' metadata kind. 925a3f5f75SPhilip Reames unsigned NonNullID = getMDKindID("nonnull"); 935a3f5f75SPhilip Reames assert(NonNullID == MD_nonnull && "nonnull kind id drifted"); 945a3f5f75SPhilip Reames (void)NonNullID; 95f999547dSSanjoy Das 96f999547dSSanjoy Das // Create the 'dereferenceable' metadata kind. 97f999547dSSanjoy Das unsigned DereferenceableID = getMDKindID("dereferenceable"); 98f999547dSSanjoy Das assert(DereferenceableID == MD_dereferenceable && 99f999547dSSanjoy Das "dereferenceable kind id drifted"); 100f999547dSSanjoy Das (void)DereferenceableID; 101f999547dSSanjoy Das 102f999547dSSanjoy Das // Create the 'dereferenceable_or_null' metadata kind. 103f999547dSSanjoy Das unsigned DereferenceableOrNullID = getMDKindID("dereferenceable_or_null"); 104f999547dSSanjoy Das assert(DereferenceableOrNullID == MD_dereferenceable_or_null && 105f999547dSSanjoy Das "dereferenceable_or_null kind id drifted"); 106f999547dSSanjoy Das (void)DereferenceableOrNullID; 10700038784SChen Li 10800038784SChen Li // Create the 'make.implicit' metadata kind. 10900038784SChen Li unsigned MakeImplicitID = getMDKindID("make.implicit"); 11000038784SChen Li assert(MakeImplicitID == MD_make_implicit && 11100038784SChen Li "make.implicit kind id drifted"); 11200038784SChen Li (void)MakeImplicitID; 113*a99ab1f5SSanjay Patel 114*a99ab1f5SSanjay Patel // Create the 'unpredictable' metadata kind. 115*a99ab1f5SSanjay Patel unsigned UnpredictableID = getMDKindID("unpredictable"); 116*a99ab1f5SSanjay Patel assert(UnpredictableID == MD_unpredictable && 117*a99ab1f5SSanjay Patel "unpredictable kind id drifted"); 118*a99ab1f5SSanjay Patel (void)UnpredictableID; 119ef860a24SChandler Carruth } 120ef860a24SChandler Carruth LLVMContext::~LLVMContext() { delete pImpl; } 121ef860a24SChandler Carruth 122ef860a24SChandler Carruth void LLVMContext::addModule(Module *M) { 123ef860a24SChandler Carruth pImpl->OwnedModules.insert(M); 124ef860a24SChandler Carruth } 125ef860a24SChandler Carruth 126ef860a24SChandler Carruth void LLVMContext::removeModule(Module *M) { 127ef860a24SChandler Carruth pImpl->OwnedModules.erase(M); 128ef860a24SChandler Carruth } 129ef860a24SChandler Carruth 130ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 131ef860a24SChandler Carruth // Recoverable Backend Errors 132ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 133ef860a24SChandler Carruth 134a594fab4SBob Wilson void LLVMContext:: 135a594fab4SBob Wilson setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, 136ef860a24SChandler Carruth void *DiagContext) { 137a594fab4SBob Wilson pImpl->InlineAsmDiagHandler = DiagHandler; 138a594fab4SBob Wilson pImpl->InlineAsmDiagContext = DiagContext; 139ef860a24SChandler Carruth } 140ef860a24SChandler Carruth 141a594fab4SBob Wilson /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by 142a594fab4SBob Wilson /// setInlineAsmDiagnosticHandler. 143a594fab4SBob Wilson LLVMContext::InlineAsmDiagHandlerTy 144a594fab4SBob Wilson LLVMContext::getInlineAsmDiagnosticHandler() const { 145a594fab4SBob Wilson return pImpl->InlineAsmDiagHandler; 146ef860a24SChandler Carruth } 147ef860a24SChandler Carruth 148a594fab4SBob Wilson /// getInlineAsmDiagnosticContext - Return the diagnostic context set by 149a594fab4SBob Wilson /// setInlineAsmDiagnosticHandler. 150a594fab4SBob Wilson void *LLVMContext::getInlineAsmDiagnosticContext() const { 151a594fab4SBob Wilson return pImpl->InlineAsmDiagContext; 152ef860a24SChandler Carruth } 153ef860a24SChandler Carruth 154b4c44d23SQuentin Colombet void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler, 15530c9242cSDuncan P. N. Exon Smith void *DiagnosticContext, 15630c9242cSDuncan P. N. Exon Smith bool RespectFilters) { 157b4c44d23SQuentin Colombet pImpl->DiagnosticHandler = DiagnosticHandler; 158b4c44d23SQuentin Colombet pImpl->DiagnosticContext = DiagnosticContext; 15930c9242cSDuncan P. N. Exon Smith pImpl->RespectDiagnosticFilters = RespectFilters; 160b4c44d23SQuentin Colombet } 161b4c44d23SQuentin Colombet 162b4c44d23SQuentin Colombet LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const { 163b4c44d23SQuentin Colombet return pImpl->DiagnosticHandler; 164b4c44d23SQuentin Colombet } 165b4c44d23SQuentin Colombet 166b4c44d23SQuentin Colombet void *LLVMContext::getDiagnosticContext() const { 167b4c44d23SQuentin Colombet return pImpl->DiagnosticContext; 168b4c44d23SQuentin Colombet } 169b4c44d23SQuentin Colombet 17034390c70SJuergen Ributzka void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle) 17134390c70SJuergen Ributzka { 17234390c70SJuergen Ributzka pImpl->YieldCallback = Callback; 17334390c70SJuergen Ributzka pImpl->YieldOpaqueHandle = OpaqueHandle; 17434390c70SJuergen Ributzka } 17534390c70SJuergen Ributzka 17634390c70SJuergen Ributzka void LLVMContext::yield() { 17734390c70SJuergen Ributzka if (pImpl->YieldCallback) 17834390c70SJuergen Ributzka pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle); 17934390c70SJuergen Ributzka } 18034390c70SJuergen Ributzka 181ef860a24SChandler Carruth void LLVMContext::emitError(const Twine &ErrorStr) { 182dec8d55bSQuentin Colombet diagnose(DiagnosticInfoInlineAsm(ErrorStr)); 183ef860a24SChandler Carruth } 184ef860a24SChandler Carruth 185bfb44ef9SBob Wilson void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { 186dec8d55bSQuentin Colombet assert (I && "Invalid instruction"); 187dec8d55bSQuentin Colombet diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr)); 188ef860a24SChandler Carruth } 189ef860a24SChandler Carruth 19030c9242cSDuncan P. N. Exon Smith static bool isDiagnosticEnabled(const DiagnosticInfo &DI) { 1917f8af8bfSDiego Novillo // Optimization remarks are selective. They need to check whether the regexp 1927f8af8bfSDiego Novillo // pattern, passed via one of the -pass-remarks* flags, matches the name of 1937f8af8bfSDiego Novillo // the pass that is emitting the diagnostic. If there is no match, ignore the 1947f8af8bfSDiego Novillo // diagnostic and return. 1957f8af8bfSDiego Novillo switch (DI.getKind()) { 1967f8af8bfSDiego Novillo case llvm::DK_OptimizationRemark: 1970b761a48SDiego Novillo if (!cast<DiagnosticInfoOptimizationRemark>(DI).isEnabled()) 19830c9242cSDuncan P. N. Exon Smith return false; 1997f8af8bfSDiego Novillo break; 2007f8af8bfSDiego Novillo case llvm::DK_OptimizationRemarkMissed: 2010b761a48SDiego Novillo if (!cast<DiagnosticInfoOptimizationRemarkMissed>(DI).isEnabled()) 20230c9242cSDuncan P. N. Exon Smith return false; 2037f8af8bfSDiego Novillo break; 2047f8af8bfSDiego Novillo case llvm::DK_OptimizationRemarkAnalysis: 2050b761a48SDiego Novillo if (!cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI).isEnabled()) 20630c9242cSDuncan P. N. Exon Smith return false; 2077f8af8bfSDiego Novillo break; 208c1a86f58STyler Nowicki case llvm::DK_OptimizationRemarkAnalysisFPCommute: 209c1a86f58STyler Nowicki if (!cast<DiagnosticInfoOptimizationRemarkAnalysisFPCommute>(DI) 210c1a86f58STyler Nowicki .isEnabled()) 211c1a86f58STyler Nowicki return false; 212c1a86f58STyler Nowicki break; 2137f8af8bfSDiego Novillo default: 2147f8af8bfSDiego Novillo break; 2157f8af8bfSDiego Novillo } 21630c9242cSDuncan P. N. Exon Smith return true; 21730c9242cSDuncan P. N. Exon Smith } 21830c9242cSDuncan P. N. Exon Smith 219735c47ecSAlex Lorenz static const char *getDiagnosticMessagePrefix(DiagnosticSeverity Severity) { 220735c47ecSAlex Lorenz switch (Severity) { 221735c47ecSAlex Lorenz case DS_Error: 222735c47ecSAlex Lorenz return "error"; 223735c47ecSAlex Lorenz case DS_Warning: 224735c47ecSAlex Lorenz return "warning"; 225735c47ecSAlex Lorenz case DS_Remark: 226735c47ecSAlex Lorenz return "remark"; 227735c47ecSAlex Lorenz case DS_Note: 228735c47ecSAlex Lorenz return "note"; 229735c47ecSAlex Lorenz } 2305220476aSAaron Ballman llvm_unreachable("Unknown DiagnosticSeverity"); 231735c47ecSAlex Lorenz } 232735c47ecSAlex Lorenz 23330c9242cSDuncan P. N. Exon Smith void LLVMContext::diagnose(const DiagnosticInfo &DI) { 23430c9242cSDuncan P. N. Exon Smith // If there is a report handler, use it. 23530c9242cSDuncan P. N. Exon Smith if (pImpl->DiagnosticHandler) { 23630c9242cSDuncan P. N. Exon Smith if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) 23730c9242cSDuncan P. N. Exon Smith pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext); 23830c9242cSDuncan P. N. Exon Smith return; 23930c9242cSDuncan P. N. Exon Smith } 24030c9242cSDuncan P. N. Exon Smith 24130c9242cSDuncan P. N. Exon Smith if (!isDiagnosticEnabled(DI)) 24230c9242cSDuncan P. N. Exon Smith return; 243df655013SDiego Novillo 244b4c44d23SQuentin Colombet // Otherwise, print the message with a prefix based on the severity. 245735c47ecSAlex Lorenz DiagnosticPrinterRawOStream DP(errs()); 246735c47ecSAlex Lorenz errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": "; 247b4c44d23SQuentin Colombet DI.print(DP); 248735c47ecSAlex Lorenz errs() << "\n"; 249735c47ecSAlex Lorenz if (DI.getSeverity() == DS_Error) 250b4c44d23SQuentin Colombet exit(1); 251b4c44d23SQuentin Colombet } 252b4c44d23SQuentin Colombet 253ef860a24SChandler Carruth void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { 254dec8d55bSQuentin Colombet diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr)); 255ef860a24SChandler Carruth } 256ef860a24SChandler Carruth 257ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 258ef860a24SChandler Carruth // Metadata Kind Uniquing 259ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 260ef860a24SChandler Carruth 2611ac0d2deSFilipe Cabecinhas /// Return a unique non-zero ID for the specified metadata kind. 262ef860a24SChandler Carruth unsigned LLVMContext::getMDKindID(StringRef Name) const { 263ef860a24SChandler Carruth // If this is new, assign it its ID. 2641ac0d2deSFilipe Cabecinhas return pImpl->CustomMDKindNames.insert( 2651ac0d2deSFilipe Cabecinhas std::make_pair( 2661ac0d2deSFilipe Cabecinhas Name, pImpl->CustomMDKindNames.size())) 2675106ce78SDavid Blaikie .first->second; 268ef860a24SChandler Carruth } 269ef860a24SChandler Carruth 2704104337dSSanjay Patel /// getHandlerNames - Populate client-supplied smallvector using custom 271ef860a24SChandler Carruth /// metadata name and ID. 272ef860a24SChandler Carruth void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { 273ef860a24SChandler Carruth Names.resize(pImpl->CustomMDKindNames.size()); 274ef860a24SChandler Carruth for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), 275ef860a24SChandler Carruth E = pImpl->CustomMDKindNames.end(); I != E; ++I) 276ef860a24SChandler Carruth Names[I->second] = I->first(); 277ef860a24SChandler Carruth } 278