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 {MD_callees, "callees"}, 63 }; 64 65 for (auto &MDKind : MDKinds) { 66 unsigned ID = getMDKindID(MDKind.second); 67 assert(ID == MDKind.first && "metadata kind id drifted"); 68 (void)ID; 69 } 70 71 auto *DeoptEntry = pImpl->getOrInsertBundleTag("deopt"); 72 assert(DeoptEntry->second == LLVMContext::OB_deopt && 73 "deopt operand bundle id drifted!"); 74 (void)DeoptEntry; 75 76 auto *FuncletEntry = pImpl->getOrInsertBundleTag("funclet"); 77 assert(FuncletEntry->second == LLVMContext::OB_funclet && 78 "funclet operand bundle id drifted!"); 79 (void)FuncletEntry; 80 81 auto *GCTransitionEntry = pImpl->getOrInsertBundleTag("gc-transition"); 82 assert(GCTransitionEntry->second == LLVMContext::OB_gc_transition && 83 "gc-transition operand bundle id drifted!"); 84 (void)GCTransitionEntry; 85 86 SyncScope::ID SingleThreadSSID = 87 pImpl->getOrInsertSyncScopeID("singlethread"); 88 assert(SingleThreadSSID == SyncScope::SingleThread && 89 "singlethread synchronization scope ID drifted!"); 90 (void)SingleThreadSSID; 91 92 SyncScope::ID SystemSSID = 93 pImpl->getOrInsertSyncScopeID(""); 94 assert(SystemSSID == SyncScope::System && 95 "system synchronization scope ID drifted!"); 96 (void)SystemSSID; 97 } 98 99 LLVMContext::~LLVMContext() { delete pImpl; } 100 101 void LLVMContext::addModule(Module *M) { 102 pImpl->OwnedModules.insert(M); 103 } 104 105 void LLVMContext::removeModule(Module *M) { 106 pImpl->OwnedModules.erase(M); 107 } 108 109 //===----------------------------------------------------------------------===// 110 // Recoverable Backend Errors 111 //===----------------------------------------------------------------------===// 112 113 void LLVMContext:: 114 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler, 115 void *DiagContext) { 116 pImpl->InlineAsmDiagHandler = DiagHandler; 117 pImpl->InlineAsmDiagContext = DiagContext; 118 } 119 120 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by 121 /// setInlineAsmDiagnosticHandler. 122 LLVMContext::InlineAsmDiagHandlerTy 123 LLVMContext::getInlineAsmDiagnosticHandler() const { 124 return pImpl->InlineAsmDiagHandler; 125 } 126 127 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by 128 /// setInlineAsmDiagnosticHandler. 129 void *LLVMContext::getInlineAsmDiagnosticContext() const { 130 return pImpl->InlineAsmDiagContext; 131 } 132 133 void LLVMContext::setDiagnosticHandlerCallBack( 134 DiagnosticHandler::DiagnosticHandlerTy DiagnosticHandler, 135 void *DiagnosticContext, bool RespectFilters) { 136 pImpl->DiagHandler->DiagHandlerCallback = DiagnosticHandler; 137 pImpl->DiagHandler->DiagnosticContext = DiagnosticContext; 138 pImpl->RespectDiagnosticFilters = RespectFilters; 139 } 140 141 void LLVMContext::setDiagnosticHandler(std::unique_ptr<DiagnosticHandler> &&DH, 142 bool RespectFilters) { 143 pImpl->DiagHandler = std::move(DH); 144 pImpl->RespectDiagnosticFilters = RespectFilters; 145 } 146 147 void LLVMContext::setDiagnosticsHotnessRequested(bool Requested) { 148 pImpl->DiagnosticsHotnessRequested = Requested; 149 } 150 bool LLVMContext::getDiagnosticsHotnessRequested() const { 151 return pImpl->DiagnosticsHotnessRequested; 152 } 153 154 void LLVMContext::setDiagnosticsHotnessThreshold(uint64_t Threshold) { 155 pImpl->DiagnosticsHotnessThreshold = Threshold; 156 } 157 uint64_t LLVMContext::getDiagnosticsHotnessThreshold() const { 158 return pImpl->DiagnosticsHotnessThreshold; 159 } 160 161 yaml::Output *LLVMContext::getDiagnosticsOutputFile() { 162 return pImpl->DiagnosticsOutputFile.get(); 163 } 164 165 void LLVMContext::setDiagnosticsOutputFile(std::unique_ptr<yaml::Output> F) { 166 pImpl->DiagnosticsOutputFile = std::move(F); 167 } 168 169 DiagnosticHandler::DiagnosticHandlerTy 170 LLVMContext::getDiagnosticHandlerCallBack() const { 171 return pImpl->DiagHandler->DiagHandlerCallback; 172 } 173 174 void *LLVMContext::getDiagnosticContext() const { 175 return pImpl->DiagHandler->DiagnosticContext; 176 } 177 178 void LLVMContext::setYieldCallback(YieldCallbackTy Callback, void *OpaqueHandle) 179 { 180 pImpl->YieldCallback = Callback; 181 pImpl->YieldOpaqueHandle = OpaqueHandle; 182 } 183 184 void LLVMContext::yield() { 185 if (pImpl->YieldCallback) 186 pImpl->YieldCallback(this, pImpl->YieldOpaqueHandle); 187 } 188 189 void LLVMContext::emitError(const Twine &ErrorStr) { 190 diagnose(DiagnosticInfoInlineAsm(ErrorStr)); 191 } 192 193 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) { 194 assert (I && "Invalid instruction"); 195 diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr)); 196 } 197 198 static bool isDiagnosticEnabled(const DiagnosticInfo &DI) { 199 // Optimization remarks are selective. They need to check whether the regexp 200 // pattern, passed via one of the -pass-remarks* flags, matches the name of 201 // the pass that is emitting the diagnostic. If there is no match, ignore the 202 // diagnostic and return. 203 // 204 // Also noisy remarks are only enabled if we have hotness information to sort 205 // them. 206 if (auto *Remark = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) 207 return Remark->isEnabled() && 208 (!Remark->isVerbose() || Remark->getHotness()); 209 210 return true; 211 } 212 213 const char * 214 LLVMContext::getDiagnosticMessagePrefix(DiagnosticSeverity Severity) { 215 switch (Severity) { 216 case DS_Error: 217 return "error"; 218 case DS_Warning: 219 return "warning"; 220 case DS_Remark: 221 return "remark"; 222 case DS_Note: 223 return "note"; 224 } 225 llvm_unreachable("Unknown DiagnosticSeverity"); 226 } 227 228 void LLVMContext::diagnose(const DiagnosticInfo &DI) { 229 if (auto *OptDiagBase = dyn_cast<DiagnosticInfoOptimizationBase>(&DI)) { 230 yaml::Output *Out = getDiagnosticsOutputFile(); 231 if (Out) { 232 // For remarks the << operator takes a reference to a pointer. 233 auto *P = const_cast<DiagnosticInfoOptimizationBase *>(OptDiagBase); 234 *Out << P; 235 } 236 } 237 // If there is a report handler, use it. 238 if (pImpl->DiagHandler && 239 (!pImpl->RespectDiagnosticFilters || isDiagnosticEnabled(DI)) && 240 pImpl->DiagHandler->handleDiagnostics(DI)) 241 return; 242 243 if (!isDiagnosticEnabled(DI)) 244 return; 245 246 // Otherwise, print the message with a prefix based on the severity. 247 DiagnosticPrinterRawOStream DP(errs()); 248 errs() << getDiagnosticMessagePrefix(DI.getSeverity()) << ": "; 249 DI.print(DP); 250 errs() << "\n"; 251 if (DI.getSeverity() == DS_Error) 252 exit(1); 253 } 254 255 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) { 256 diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr)); 257 } 258 259 //===----------------------------------------------------------------------===// 260 // Metadata Kind Uniquing 261 //===----------------------------------------------------------------------===// 262 263 /// Return a unique non-zero ID for the specified metadata kind. 264 unsigned LLVMContext::getMDKindID(StringRef Name) const { 265 // If this is new, assign it its ID. 266 return pImpl->CustomMDKindNames.insert( 267 std::make_pair( 268 Name, pImpl->CustomMDKindNames.size())) 269 .first->second; 270 } 271 272 /// getHandlerNames - Populate client-supplied smallvector using custom 273 /// metadata name and ID. 274 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { 275 Names.resize(pImpl->CustomMDKindNames.size()); 276 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), 277 E = pImpl->CustomMDKindNames.end(); I != E; ++I) 278 Names[I->second] = I->first(); 279 } 280 281 void LLVMContext::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const { 282 pImpl->getOperandBundleTags(Tags); 283 } 284 285 uint32_t LLVMContext::getOperandBundleTagID(StringRef Tag) const { 286 return pImpl->getOperandBundleTagID(Tag); 287 } 288 289 SyncScope::ID LLVMContext::getOrInsertSyncScopeID(StringRef SSN) { 290 return pImpl->getOrInsertSyncScopeID(SSN); 291 } 292 293 void LLVMContext::getSyncScopeNames(SmallVectorImpl<StringRef> &SSNs) const { 294 pImpl->getSyncScopeNames(SSNs); 295 } 296 297 void LLVMContext::setGC(const Function &Fn, std::string GCName) { 298 auto It = pImpl->GCNames.find(&Fn); 299 300 if (It == pImpl->GCNames.end()) { 301 pImpl->GCNames.insert(std::make_pair(&Fn, std::move(GCName))); 302 return; 303 } 304 It->second = std::move(GCName); 305 } 306 307 const std::string &LLVMContext::getGC(const Function &Fn) { 308 return pImpl->GCNames[&Fn]; 309 } 310 311 void LLVMContext::deleteGC(const Function &Fn) { 312 pImpl->GCNames.erase(&Fn); 313 } 314 315 bool LLVMContext::shouldDiscardValueNames() const { 316 return pImpl->DiscardValueNames; 317 } 318 319 bool LLVMContext::isODRUniquingDebugTypes() const { return !!pImpl->DITypeMap; } 320 321 void LLVMContext::enableDebugTypeODRUniquing() { 322 if (pImpl->DITypeMap) 323 return; 324 325 pImpl->DITypeMap.emplace(); 326 } 327 328 void LLVMContext::disableDebugTypeODRUniquing() { pImpl->DITypeMap.reset(); } 329 330 void LLVMContext::setDiscardValueNames(bool Discard) { 331 pImpl->DiscardValueNames = Discard; 332 } 333 334 OptBisect &LLVMContext::getOptBisect() { 335 return pImpl->getOptBisect(); 336 } 337 338 const DiagnosticHandler *LLVMContext::getDiagHandlerPtr() const { 339 return pImpl->DiagHandler.get(); 340 } 341 342 std::unique_ptr<DiagnosticHandler> LLVMContext::getDiagnosticHandler() { 343 return std::move(pImpl->DiagHandler); 344 } 345