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