LCOV - code coverage report
Current view: top level - lib/IR - LLVMContextImpl.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 91 92 98.9 %
Date: 2018-07-13 00:08:38 Functions: 13 13 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : //===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===//
       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 the opaque LLVMContextImpl.
      11             : //
      12             : //===----------------------------------------------------------------------===//
      13             : 
      14             : #include "LLVMContextImpl.h"
      15             : #include "llvm/IR/Module.h"
      16             : #include "llvm/IR/OptBisect.h"
      17             : #include "llvm/IR/Type.h"
      18             : #include "llvm/Support/ManagedStatic.h"
      19             : #include <cassert>
      20             : #include <utility>
      21             : 
      22             : using namespace llvm;
      23             : 
      24       82100 : LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
      25             :   : DiagHandler(llvm::make_unique<DiagnosticHandler>()),
      26             :     VoidTy(C, Type::VoidTyID),
      27             :     LabelTy(C, Type::LabelTyID),
      28             :     HalfTy(C, Type::HalfTyID),
      29             :     FloatTy(C, Type::FloatTyID),
      30             :     DoubleTy(C, Type::DoubleTyID),
      31             :     MetadataTy(C, Type::MetadataTyID),
      32             :     TokenTy(C, Type::TokenTyID),
      33             :     X86_FP80Ty(C, Type::X86_FP80TyID),
      34             :     FP128Ty(C, Type::FP128TyID),
      35             :     PPC_FP128Ty(C, Type::PPC_FP128TyID),
      36             :     X86_MMXTy(C, Type::X86_MMXTyID),
      37             :     Int1Ty(C, 1),
      38             :     Int8Ty(C, 8),
      39             :     Int16Ty(C, 16),
      40             :     Int32Ty(C, 32),
      41             :     Int64Ty(C, 64),
      42     1970400 :     Int128Ty(C, 128) {}
      43             : 
      44      795310 : LLVMContextImpl::~LLVMContextImpl() {
      45             :   // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
      46             :   // will call LLVMContextImpl::removeModule, thus invalidating iterators into
      47             :   // the container. Avoid iterators during this operation:
      48       79571 :   while (!OwnedModules.empty())
      49          40 :     delete *OwnedModules.begin();
      50             : 
      51             : #ifndef NDEBUG
      52             :   // Check for metadata references from leaked Instructions.
      53             :   for (auto &Pair : InstructionMetadata)
      54             :     Pair.first->dump();
      55             :   assert(InstructionMetadata.empty() &&
      56             :          "Instructions with metadata have been leaked");
      57             : #endif
      58             : 
      59             :   // Drop references for MDNodes.  Do this before Values get deleted to avoid
      60             :   // unnecessary RAUW when nodes are still unresolved.
      61      110029 :   for (auto *I : DistinctMDNodes)
      62       30498 :     I->dropAllReferences();
      63             : #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
      64             :   for (auto *I : CLASS##s)                                                     \
      65             :     I->dropAllReferences();
      66             : #include "llvm/IR/Metadata.def"
      67             : 
      68             :   // Also drop references that come from the Value bridges.
      69      222454 :   for (auto &Pair : ValuesAsMetadata)
      70       63392 :     Pair.second->dropUsers();
      71      170659 :   for (auto &Pair : MetadataAsValues)
      72       11597 :     Pair.second->dropUse();
      73             : 
      74             :   // Destroy MDNodes.
      75      110029 :   for (MDNode *I : DistinctMDNodes)
      76       30498 :     I->deleteAsSubclass();
      77             : #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
      78             :   for (CLASS * I : CLASS##s)                                                   \
      79             :     delete I;
      80             : #include "llvm/IR/Metadata.def"
      81             : 
      82             :   // Free the constants.
      83       83106 :   for (auto *I : ExprConstants)
      84        3575 :     I->dropAllReferences();
      85       79957 :   for (auto *I : ArrayConstants)
      86         426 :     I->dropAllReferences();
      87       81848 :   for (auto *I : StructConstants)
      88        2317 :     I->dropAllReferences();
      89       93027 :   for (auto *I : VectorConstants)
      90       13496 :     I->dropAllReferences();
      91       79531 :   ExprConstants.freeConstants();
      92       79531 :   ArrayConstants.freeConstants();
      93       79531 :   StructConstants.freeConstants();
      94       79531 :   VectorConstants.freeConstants();
      95       79531 :   InlineAsms.freeConstants();
      96             : 
      97       79531 :   CAZConstants.clear();
      98       79531 :   CPNConstants.clear();
      99       79531 :   UVConstants.clear();
     100       79531 :   IntConstants.clear();
     101       79531 :   FPConstants.clear();
     102             : 
     103      227399 :   for (auto &CDSConstant : CDSConstants)
     104       68337 :     delete CDSConstant.second;
     105       79531 :   CDSConstants.clear();
     106             : 
     107             :   // Destroy attributes.
     108      159062 :   for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
     109      404976 :          E = AttrsSet.end(); I != E; ) {
     110             :     FoldingSetIterator<AttributeImpl> Elem = I++;
     111      245914 :     delete &*Elem;
     112             :   }
     113             : 
     114             :   // Destroy attribute lists.
     115      386263 :   for (FoldingSetIterator<AttributeListImpl> I = AttrsLists.begin(),
     116       79531 :                                              E = AttrsLists.end();
     117      306732 :        I != E;) {
     118             :     FoldingSetIterator<AttributeListImpl> Elem = I++;
     119             :     delete &*Elem;
     120             :   }
     121             : 
     122             :   // Destroy attribute node lists.
     123      327027 :   for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
     124      327027 :          E = AttrsSetNodes.end(); I != E; ) {
     125             :     FoldingSetIterator<AttributeSetNode> Elem = I++;
     126             :     delete &*Elem;
     127             :   }
     128             : 
     129             :   // Destroy MetadataAsValues.
     130             :   {
     131             :     SmallVector<MetadataAsValue *, 8> MDVs;
     132       79531 :     MDVs.reserve(MetadataAsValues.size());
     133      170659 :     for (auto &Pair : MetadataAsValues)
     134       11597 :       MDVs.push_back(Pair.second);
     135       79531 :     MetadataAsValues.clear();
     136      102725 :     for (auto *V : MDVs)
     137       11597 :       delete V;
     138             :   }
     139             : 
     140             :   // Destroy ValuesAsMetadata.
     141      159062 :   for (auto &Pair : ValuesAsMetadata)
     142           0 :     delete Pair.second;
     143       79531 : }
     144             : 
     145        1055 : void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
     146             :   bool Changed;
     147        1067 :   do {
     148             :     Changed = false;
     149             : 
     150        1261 :     for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
     151         194 :       auto *C = *I++;
     152         194 :       if (C->use_empty()) {
     153             :         Changed = true;
     154          12 :         C->destroyConstant();
     155             :       }
     156             :     }
     157             :   } while (Changed);
     158        1055 : }
     159             : 
     160        1055 : void Module::dropTriviallyDeadConstantArrays() {
     161        1055 :   Context.pImpl->dropTriviallyDeadConstantArrays();
     162        1055 : }
     163             : 
     164             : namespace llvm {
     165             : 
     166             : /// Make MDOperand transparent for hashing.
     167             : ///
     168             : /// This overload of an implementation detail of the hashing library makes
     169             : /// MDOperand hash to the same value as a \a Metadata pointer.
     170             : ///
     171             : /// Note that overloading \a hash_value() as follows:
     172             : ///
     173             : /// \code
     174             : ///     size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
     175             : /// \endcode
     176             : ///
     177             : /// does not cause MDOperand to be transparent.  In particular, a bare pointer
     178             : /// doesn't get hashed before it's combined, whereas \a MDOperand would.
     179             : static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
     180             : 
     181             : } // end namespace llvm
     182             : 
     183       20212 : unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
     184       40424 :   unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
     185             : #ifndef NDEBUG
     186             :   {
     187             :     SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
     188             :     unsigned RawHash = calculateHash(MDs);
     189             :     assert(Hash == RawHash &&
     190             :            "Expected hash of MDOperand to equal hash of Metadata*");
     191             :   }
     192             : #endif
     193       20212 :   return Hash;
     194             : }
     195             : 
     196      904507 : unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
     197      904507 :   return hash_combine_range(Ops.begin(), Ops.end());
     198             : }
     199             : 
     200      248057 : StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
     201      248057 :   uint32_t NewIdx = BundleTagCache.size();
     202      496114 :   return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
     203             : }
     204             : 
     205        4518 : void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
     206        4518 :   Tags.resize(BundleTagCache.size());
     207       22598 :   for (const auto &T : BundleTagCache)
     208       27124 :     Tags[T.second] = T.first();
     209        4518 : }
     210             : 
     211          70 : uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
     212          70 :   auto I = BundleTagCache.find(Tag);
     213             :   assert(I != BundleTagCache.end() && "Unknown tag!");
     214          70 :   return I->second;
     215             : }
     216             : 
     217      177670 : SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) {
     218      177670 :   auto NewSSID = SSC.size();
     219             :   assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() &&
     220             :          "Hit the maximum number of synchronization scopes allowed!");
     221      355340 :   return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
     222             : }
     223             : 
     224        4640 : void LLVMContextImpl::getSyncScopeNames(
     225             :     SmallVectorImpl<StringRef> &SSNs) const {
     226        4640 :   SSNs.resize(SSC.size());
     227       18869 :   for (const auto &SSE : SSC)
     228       19178 :     SSNs[SSE.second] = SSE.first();
     229        4640 : }
     230             : 
     231             : /// Singleton instance of the OptBisect class.
     232             : ///
     233             : /// This singleton is accessed via the LLVMContext::getOptPassGate() function.
     234             : /// It provides a mechanism to disable passes and individual optimizations at
     235             : /// compile time based on a command line option (-opt-bisect-limit) in order to
     236             : /// perform a bisecting search for optimization-related problems.
     237             : ///
     238             : /// Even if multiple LLVMContext objects are created, they will all return the
     239             : /// same instance of OptBisect in order to provide a single bisect count.  Any
     240             : /// code that uses the OptBisect object should be serialized when bisection is
     241             : /// enabled in order to enable a consistent bisect count.
     242             : static ManagedStatic<OptBisect> OptBisector;
     243             : 
     244     8545872 : OptPassGate &LLVMContextImpl::getOptPassGate() const {
     245     8545872 :   if (!OPG)
     246       28159 :     OPG = &(*OptBisector);
     247     8545872 :   return *OPG;
     248             : }
     249             : 
     250           2 : void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) {
     251           2 :   this->OPG = &OPG;
     252           2 : }

Generated by: LCOV version 1.13