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/ADT/SmallVector.h" 18 #include "llvm/ADT/StringMap.h" 19 #include "llvm/ADT/StringRef.h" 20 #include "llvm/ADT/Twine.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 std::pair<unsigned, StringRef> MDKinds[] = { 39 {MD_dbg, "dbg"}, 40 {MD_tbaa, "tbaa"}, 41 {MD_prof, "prof"}, 42 {MD_fpmath, "fpmath"}, 43 {MD_range, "range"}, 44 {MD_tbaa_struct, "tbaa.struct"}, 45 {MD_invariant_load, "invariant.load"}, 46 {MD_alias_scope, "alias.scope"}, 47 {MD_noalias, "noalias"}, 48 {MD_nontemporal, "nontemporal"}, 49 {MD_mem_parallel_loop_access, "llvm.mem.parallel_loop_access"}, 50 {MD_nonnull, "nonnull"}, 51 {MD_dereferenceable, "dereferenceable"}, 52 {MD_dereferenceable_or_null, "dereferenceable_or_null"}, 53 {MD_make_implicit, "make.implicit"}, 54 {MD_unpredictable, "unpredictable"}, 55 {MD_invariant_group, "invariant.group"}, 56 {MD_align, "align"}, 57 {MD_loop, "llvm.loop"}, 58 {MD_type, "type"}, 59 {MD_section_prefix, "section_prefix"}, 60 {MD_absolute_symbol, "absolute_symbol"}, 61 {MD_associated, "associated"}, 62 }; 63 64 for (auto &MDKind : MDKinds) { 65 unsigned ID = getMDKindID(MDKind.second); 66 assert(ID == MDKind.first && "metadata kind id drifted"); 67 (void)ID; 68 } 69 70 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt"); 71 assert(DeoptEntry->second == LLVMContext::OB_deopt && 72 "deopt operand bundle id drifted!"); 73 (void)DeoptEntry; 74 75 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet"); 76 assert(FuncletEntry->second == LLVMContext::OB_funclet && 77 "funclet operand bundle id drifted!"); 78 (void)FuncletEntry; 79 80 auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition"); 81 assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition && 82 "gc-transition operand bundle id drifted!"); 83 (void)GCTransitionEntry; 84 } 85 86 LLVMContext::~LLVMContext() { delete pImpl; } 87 88 void LLVMContext::addModule(Module *M) { 89 pImpl->OwnedModules.insert(M); 90 } 91 92 void LLVMContext::removeModule(Module *M) { 93 pImpl->OwnedModules.erase(M); 94 } 95 96 //===----------------------------------------------------------------------===// 97 // Recoverable Backend Errors 98 //===----------------------------------------------------------------------===// 99 100 void LLVMContext:: 101 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, 102 void *DiagContext) { 103 pImpl->InlineAsmDiagHandler = DiagHandler; 104 pImpl->InlineAsmDiagContext = DiagContext; 105 } 106 107 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by 108 /// setInlineAsmDiagnosticHandler. 109 LLVMContext::InlineAsmDiagHandlerTy 110 LLVMContext::getInlineAsmDiagnosticHandler() const { 111 return pImpl->InlineAsmDiagHandler; 112 } 113 114 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by 115 /// setInlineAsmDiagnosticHandler. 116 void *LLVMContext::getInlineAsmDiagnosticContext() const { 117 return pImpl->InlineAsmDiagContext; 118 } 119 120 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler, 121 void *DiagnosticContext, 122 bool RespectFilters) { 123 pImpl->DiagnosticHandler = DiagnosticHandler; 124 pImpl->DiagnosticContext = DiagnosticContext; 125 pImpl->RespectDiagnosticFilters = RespectFilters; 126 } 127 128 void LLVMContext::setDiagnosticHotnessRequested(bool Requested) { 129 pImpl->DiagnosticHotnessRequested = Requested; 130 } 131 bool LLVMContext::getDiagnosticHotnessRequested() const { 132 return pImpl->DiagnosticHotnessRequested; 133 } 134 135 yaml::Output *LLVMContext::getDiagnosticsOutputFile() { 136 return pImpl->DiagnosticsOutputFile.get(); 137 } 138 139 void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) { 140 pImpl->DiagnosticsOutputFile = std::move(F); 141 } 142 143 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const { 144 return pImpl->DiagnosticHandler; 145 } 146 147 void *LLVMContext::getDiagnosticContext() const { 148 return pImpl->DiagnosticContext; 149 } 150 151 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle) 152 { 153 pImpl->YieldCallback = Callback; 154 pImpl->YieldOpaqueHandle = OpaqueHandle; 155 } 156 157 void LLVMContext::yield() { 158 if (pImpl->YieldCallback) 159 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle); 160 } 161 162 void LLVMContext::emitError(const Twine &ErrorStr) { 163 diagnose(DiagnosticInfoInlineAsm(ErrorStr)); 164 } 165 166 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { 167 assert (I && "Invalid instruction"); 168 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr)); 169 } 170 171 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) { 172 // Optimization remarks are selective. They need to check whether the regexp 173 // pattern, passed via one of the -pass-remarks* flags, matches the name of 174 // the pass that is emitting the diagnostic. If there is no match, ignore the 175 // diagnostic and return. 176 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) 177 return Remark->isEnabled(); 178 179 return true; 180 } 181 182 const char * 183 LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) { 184 switch (Severity) { 185 case DS_Error: 186 return "error"; 187 case DS_Warning: 188 return "warning"; 189 case DS_Remark: 190 return "remark"; 191 case DS_Note: 192 return "note"; 193 } 194 llvm_unreachable("Unknown DiagnosticSeverity"); 195 } 196 197 void LLVMContext::diagnose(const DiagnosticInfo &DI) { 198 // If there is a report handler, use it. 199 if (pImpl->DiagnosticHandler) { 200 if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) 201 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext); 202 return; 203 } 204 205 if (!isDiagnosticEnabled(DI)) 206 return; 207 208 // Otherwise, print the message with a prefix based on the severity. 209 DiagnosticPrinterRawOStream DP(errs()); 210 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": "; 211 DI.print(DP); 212 errs() << "\n"; 213 if (DI.getSeverity() == DS_Error) 214 exit(1); 215 } 216 217 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { 218 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr)); 219 } 220 221 //===----------------------------------------------------------------------===// 222 // Metadata Kind Uniquing 223 //===----------------------------------------------------------------------===// 224 225 /// Return a unique non-zero ID for the specified metadata kind. 226 unsigned LLVMContext::getMDKindID(StringRef Name) const { 227 // If this is new, assign it its ID. 228 return pImpl->CustomMDKindNames.insert( 229 std::make_pair( 230 Name, pImpl->CustomMDKindNames.size())) 231 .first->second; 232 } 233 234 /// getHandlerNames - Populate client-supplied smallvector using custom 235 /// metadata name and ID. 236 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { 237 Names.resize(pImpl->CustomMDKindNames.size()); 238 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), 239 E = pImpl->CustomMDKindNames.end(); I != E; ++I) 240 Names[I->second] = I->first(); 241 } 242 243 void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const { 244 pImpl->getOperandBundleTags(Tags); 245 } 246 247 uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const { 248 return pImpl->getOperandBundleTagID(Tag); 249 } 250 251 void LLVMContext::setGC(const Function &Fn, std::string GCName) { 252 auto It = pImpl->GCNames.find(&Fn); 253 254 if (It == pImpl->GCNames.end()) { 255 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName))); 256 return; 257 } 258 It->second = std::move(GCName); 259 } 260 261 const std::string &LLVMContext::getGC(const Function &Fn) { 262 return pImpl->GCNames[&Fn]; 263 } 264 265 void LLVMContext::deleteGC(const Function &Fn) { 266 pImpl->GCNames.erase(&Fn); 267 } 268 269 bool LLVMContext::shouldDiscardValueNames() const { 270 return pImpl->DiscardValueNames; 271 } 272 273 bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; } 274 275 void LLVMContext::enableDebugTypeODRUniquing() { 276 if (pImpl->DITypeMap) 277 return; 278 279 pImpl->DITypeMap.emplace(); 280 } 281 282 void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); } 283 284 void LLVMContext::setDiscardValueNames(bool Discard) { 285 pImpl->DiscardValueNames = Discard; 286 } 287 288 OptBisect &LLVMContext::getOptBisect() { 289 return pImpl->getOptBisect(); 290 } 291