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