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; 95*f999547dSSanjoy Das 96*f999547dSSanjoy Das // Create the 'dereferenceable' metadata kind. 97*f999547dSSanjoy Das unsigned DereferenceableID = getMDKindID("dereferenceable"); 98*f999547dSSanjoy Das assert(DereferenceableID == MD_dereferenceable && 99*f999547dSSanjoy Das "dereferenceable kind id drifted"); 100*f999547dSSanjoy Das (void)DereferenceableID; 101*f999547dSSanjoy Das 102*f999547dSSanjoy Das // Create the 'dereferenceable_or_null' metadata kind. 103*f999547dSSanjoy Das unsigned DereferenceableOrNullID = getMDKindID("dereferenceable_or_null"); 104*f999547dSSanjoy Das assert(DereferenceableOrNullID == MD_dereferenceable_or_null && 105*f999547dSSanjoy Das "dereferenceable_or_null kind id drifted"); 106*f999547dSSanjoy Das (void)DereferenceableOrNullID; 107ef860a24SChandler Carruth } 108ef860a24SChandler Carruth LLVMContext::~LLVMContext() { delete pImpl; } 109ef860a24SChandler Carruth 110ef860a24SChandler Carruth void LLVMContext::addModule(Module *M) { 111ef860a24SChandler Carruth pImpl->OwnedModules.insert(M); 112ef860a24SChandler Carruth } 113ef860a24SChandler Carruth 114ef860a24SChandler Carruth void LLVMContext::removeModule(Module *M) { 115ef860a24SChandler Carruth pImpl->OwnedModules.erase(M); 116ef860a24SChandler Carruth } 117ef860a24SChandler Carruth 118ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 119ef860a24SChandler Carruth // Recoverable Backend Errors 120ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 121ef860a24SChandler Carruth 122a594fab4SBob Wilson void LLVMContext:: 123a594fab4SBob Wilson setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, 124ef860a24SChandler Carruth void *DiagContext) { 125a594fab4SBob Wilson pImpl->InlineAsmDiagHandler = DiagHandler; 126a594fab4SBob Wilson pImpl->InlineAsmDiagContext = DiagContext; 127ef860a24SChandler Carruth } 128ef860a24SChandler Carruth 129a594fab4SBob Wilson /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by 130a594fab4SBob Wilson /// setInlineAsmDiagnosticHandler. 131a594fab4SBob Wilson LLVMContext::InlineAsmDiagHandlerTy 132a594fab4SBob Wilson LLVMContext::getInlineAsmDiagnosticHandler() const { 133a594fab4SBob Wilson return pImpl->InlineAsmDiagHandler; 134ef860a24SChandler Carruth } 135ef860a24SChandler Carruth 136a594fab4SBob Wilson /// getInlineAsmDiagnosticContext - Return the diagnostic context set by 137a594fab4SBob Wilson /// setInlineAsmDiagnosticHandler. 138a594fab4SBob Wilson void *LLVMContext::getInlineAsmDiagnosticContext() const { 139a594fab4SBob Wilson return pImpl->InlineAsmDiagContext; 140ef860a24SChandler Carruth } 141ef860a24SChandler Carruth 142b4c44d23SQuentin Colombet void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler, 14330c9242cSDuncan P. N. Exon Smith void *DiagnosticContext, 14430c9242cSDuncan P. N. Exon Smith bool RespectFilters) { 145b4c44d23SQuentin Colombet pImpl->DiagnosticHandler = DiagnosticHandler; 146b4c44d23SQuentin Colombet pImpl->DiagnosticContext = DiagnosticContext; 14730c9242cSDuncan P. N. Exon Smith pImpl->RespectDiagnosticFilters = RespectFilters; 148b4c44d23SQuentin Colombet } 149b4c44d23SQuentin Colombet 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. 1837f8af8bfSDiego Novillo switch (DI.getKind()) { 1847f8af8bfSDiego Novillo case llvm::DK_OptimizationRemark: 1850b761a48SDiego Novillo if (!cast<DiagnosticInfoOptimizationRemark>(DI).isEnabled()) 18630c9242cSDuncan P. N. Exon Smith return false; 1877f8af8bfSDiego Novillo break; 1887f8af8bfSDiego Novillo case llvm::DK_OptimizationRemarkMissed: 1890b761a48SDiego Novillo if (!cast<DiagnosticInfoOptimizationRemarkMissed>(DI).isEnabled()) 19030c9242cSDuncan P. N. Exon Smith return false; 1917f8af8bfSDiego Novillo break; 1927f8af8bfSDiego Novillo case llvm::DK_OptimizationRemarkAnalysis: 1930b761a48SDiego Novillo if (!cast<DiagnosticInfoOptimizationRemarkAnalysis>(DI).isEnabled()) 19430c9242cSDuncan P. N. Exon Smith return false; 1957f8af8bfSDiego Novillo break; 1967f8af8bfSDiego Novillo default: 1977f8af8bfSDiego Novillo break; 1987f8af8bfSDiego Novillo } 19930c9242cSDuncan P. N. Exon Smith return true; 20030c9242cSDuncan P. N. Exon Smith } 20130c9242cSDuncan P. N. Exon Smith 20230c9242cSDuncan P. N. Exon Smith void LLVMContext::diagnose(const DiagnosticInfo &DI) { 20330c9242cSDuncan P. N. Exon Smith // If there is a report handler, use it. 20430c9242cSDuncan P. N. Exon Smith if (pImpl->DiagnosticHandler) { 20530c9242cSDuncan P. N. Exon Smith if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) 20630c9242cSDuncan P. N. Exon Smith pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext); 20730c9242cSDuncan P. N. Exon Smith return; 20830c9242cSDuncan P. N. Exon Smith } 20930c9242cSDuncan P. N. Exon Smith 21030c9242cSDuncan P. N. Exon Smith if (!isDiagnosticEnabled(DI)) 21130c9242cSDuncan P. N. Exon Smith return; 212df655013SDiego Novillo 213b4c44d23SQuentin Colombet // Otherwise, print the message with a prefix based on the severity. 214e69170a1SAlp Toker std::string MsgStorage; 215e69170a1SAlp Toker raw_string_ostream Stream(MsgStorage); 216e69170a1SAlp Toker DiagnosticPrinterRawOStream DP(Stream); 217b4c44d23SQuentin Colombet DI.print(DP); 218e69170a1SAlp Toker Stream.flush(); 219b4c44d23SQuentin Colombet switch (DI.getSeverity()) { 220b4c44d23SQuentin Colombet case DS_Error: 221e69170a1SAlp Toker errs() << "error: " << MsgStorage << "\n"; 222b4c44d23SQuentin Colombet exit(1); 223b4c44d23SQuentin Colombet case DS_Warning: 224e69170a1SAlp Toker errs() << "warning: " << MsgStorage << "\n"; 225b4c44d23SQuentin Colombet break; 226e8d4c9a2STobias Grosser case DS_Remark: 227e69170a1SAlp Toker errs() << "remark: " << MsgStorage << "\n"; 228e8d4c9a2STobias Grosser break; 229b4c44d23SQuentin Colombet case DS_Note: 230e69170a1SAlp Toker errs() << "note: " << MsgStorage << "\n"; 231b4c44d23SQuentin Colombet break; 232b4c44d23SQuentin Colombet } 233b4c44d23SQuentin Colombet } 234b4c44d23SQuentin Colombet 235ef860a24SChandler Carruth void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { 236dec8d55bSQuentin Colombet diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr)); 237ef860a24SChandler Carruth } 238ef860a24SChandler Carruth 239ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 240ef860a24SChandler Carruth // Metadata Kind Uniquing 241ef860a24SChandler Carruth //===----------------------------------------------------------------------===// 242ef860a24SChandler Carruth 243ef860a24SChandler Carruth /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. 244ef860a24SChandler Carruth unsigned LLVMContext::getMDKindID(StringRef Name) const { 245dde76ff0SNick Lewycky assert(!std::isdigit(Name.front()) && 246dde76ff0SNick Lewycky "Named metadata may not start with a digit"); 247ef860a24SChandler Carruth 248ef860a24SChandler Carruth // If this is new, assign it its ID. 2495106ce78SDavid Blaikie return pImpl->CustomMDKindNames.insert(std::make_pair( 2505106ce78SDavid Blaikie Name, 2515106ce78SDavid Blaikie pImpl->CustomMDKindNames.size())) 2525106ce78SDavid Blaikie .first->second; 253ef860a24SChandler Carruth } 254ef860a24SChandler Carruth 255ef860a24SChandler Carruth /// getHandlerNames - Populate client supplied smallvector using custome 256ef860a24SChandler Carruth /// metadata name and ID. 257ef860a24SChandler Carruth void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { 258ef860a24SChandler Carruth Names.resize(pImpl->CustomMDKindNames.size()); 259ef860a24SChandler Carruth for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), 260ef860a24SChandler Carruth E = pImpl->CustomMDKindNames.end(); I != E; ++I) 261ef860a24SChandler Carruth Names[I->second] = I->first(); 262ef860a24SChandler Carruth } 263