LCOV - code coverage report
Current view: top level - lib/IR - LLVMContext.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 130 132 98.5 %
Date: 2017-09-14 15:23:50 Functions: 38 38 100.0 %
Legend: Lines: hit not hit

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

Generated by: LCOV version 1.13