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::setDiagnosticsHotnessRequested(bool Requested) { 129 pImpl->DiagnosticsHotnessRequested = Requested; 130 } 131 bool LLVMContext::getDiagnosticsHotnessRequested() const { 132 return pImpl->DiagnosticsHotnessRequested; 133 } 134 135 void LLVMContext::setDiagnosticsHotnessThreshold(uint64_t Threshold) { 136 pImpl->DiagnosticsHotnessThreshold = Threshold; 137 } 138 uint64_t LLVMContext::getDiagnosticsHotnessThreshold() const { 139 return pImpl->DiagnosticsHotnessThreshold; 140 } 141 142 yaml::Output *LLVMContext::getDiagnosticsOutputFile() { 143 return pImpl->DiagnosticsOutputFile.get(); 144 } 145 146 void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) { 147 pImpl->DiagnosticsOutputFile = std::move(F); 148 } 149 150 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const { 151 return pImpl->DiagnosticHandler; 152 } 153 154 void *LLVMContext::getDiagnosticContext() const { 155 return pImpl->DiagnosticContext; 156 } 157 158 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle) 159 { 160 pImpl->YieldCallback = Callback; 161 pImpl->YieldOpaqueHandle = OpaqueHandle; 162 } 163 164 void LLVMContext::yield() { 165 if (pImpl->YieldCallback) 166 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle); 167 } 168 169 void LLVMContext::emitError(const Twine &ErrorStr) { 170 diagnose(DiagnosticInfoInlineAsm(ErrorStr)); 171 } 172 173 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { 174 assert (I && "Invalid instruction"); 175 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr)); 176 } 177 178 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) { 179 // Optimization remarks are selective. They need to check whether the regexp 180 // pattern, passed via one of the -pass-remarks* flags, matches the name of 181 // the pass that is emitting the diagnostic. If there is no match, ignore the 182 // diagnostic and return. 183 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) 184 return Remark->isEnabled(); 185 186 return true; 187 } 188 189 const char * 190 LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) { 191 switch (Severity) { 192 case DS_Error: 193 return "error"; 194 case DS_Warning: 195 return "warning"; 196 case DS_Remark: 197 return "remark"; 198 case DS_Note: 199 return "note"; 200 } 201 llvm_unreachable("Unknown DiagnosticSeverity"); 202 } 203 204 void LLVMContext::diagnose(const DiagnosticInfo &DI) { 205 // If there is a report handler, use it. 206 if (pImpl->DiagnosticHandler) { 207 if (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) 208 pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext); 209 return; 210 } 211 212 if (!isDiagnosticEnabled(DI)) 213 return; 214 215 // Otherwise, print the message with a prefix based on the severity. 216 DiagnosticPrinterRawOStream DP(errs()); 217 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": "; 218 DI.print(DP); 219 errs() << "\n"; 220 if (DI.getSeverity() == DS_Error) 221 exit(1); 222 } 223 224 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { 225 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr)); 226 } 227 228 //===----------------------------------------------------------------------===// 229 // Metadata Kind Uniquing 230 //===----------------------------------------------------------------------===// 231 232 /// Return a unique non-zero ID for the specified metadata kind. 233 unsigned LLVMContext::getMDKindID(StringRef Name) const { 234 // If this is new, assign it its ID. 235 return pImpl->CustomMDKindNames.insert( 236 std::make_pair( 237 Name, pImpl->CustomMDKindNames.size())) 238 .first->second; 239 } 240 241 /// getHandlerNames - Populate client-supplied smallvector using custom 242 /// metadata name and ID. 243 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { 244 Names.resize(pImpl->CustomMDKindNames.size()); 245 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), 246 E = pImpl->CustomMDKindNames.end(); I != E; ++I) 247 Names[I->second] = I->first(); 248 } 249 250 void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const { 251 pImpl->getOperandBundleTags(Tags); 252 } 253 254 uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const { 255 return pImpl->getOperandBundleTagID(Tag); 256 } 257 258 void LLVMContext::setGC(const Function &Fn, std::string GCName) { 259 auto It = pImpl->GCNames.find(&Fn); 260 261 if (It == pImpl->GCNames.end()) { 262 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName))); 263 return; 264 } 265 It->second = std::move(GCName); 266 } 267 268 const std::string &LLVMContext::getGC(const Function &Fn) { 269 return pImpl->GCNames[&Fn]; 270 } 271 272 void LLVMContext::deleteGC(const Function &Fn) { 273 pImpl->GCNames.erase(&Fn); 274 } 275 276 bool LLVMContext::shouldDiscardValueNames() const { 277 return pImpl->DiscardValueNames; 278 } 279 280 bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; } 281 282 void LLVMContext::enableDebugTypeODRUniquing() { 283 if (pImpl->DITypeMap) 284 return; 285 286 pImpl->DITypeMap.emplace(); 287 } 288 289 void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); } 290 291 void LLVMContext::setDiscardValueNames(bool Discard) { 292 pImpl->DiscardValueNames = Discard; 293 } 294 295 OptBisect &LLVMContext::getOptBisect() { 296 return pImpl->getOptBisect(); 297 } 298