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-06-17 00:07:59 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       49118 : 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     1178832 :     Int128Ty(C, 128) {}
      43             : 
      44      459690 : 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       46009 :   while (!OwnedModules.empty())
      49          40 :     delete *OwnedModules.begin();
      50             : 
      51             :   // Drop references for MDNodes.  Do this before Values get deleted to avoid
      52             :   // unnecessary RAUW when nodes are still unresolved.
      53       76394 :   for (auto *I : DistinctMDNodes)
      54       30425 :     I->dropAllReferences();
      55             : #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
      56             :   for (auto *I : CLASS##s)                                                     \
      57             :     I->dropAllReferences();
      58             : #include "llvm/IR/Metadata.def"
      59             : 
      60             :   // Also drop references that come from the Value bridges.
      61      155228 :   for (auto &Pair : ValuesAsMetadata)
      62       63290 :     Pair.second->dropUsers();
      63      103114 :   for (auto &Pair : MetadataAsValues)
      64       11176 :     Pair.second->dropUse();
      65             : 
      66             :   // Destroy MDNodes.
      67       76394 :   for (MDNode *I : DistinctMDNodes)
      68       30425 :     I->deleteAsSubclass();
      69             : #define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS)                                    \
      70             :   for (CLASS * I : CLASS##s)                                                   \
      71             :     delete I;
      72             : #include "llvm/IR/Metadata.def"
      73             : 
      74             :   // Free the constants.
      75       49534 :   for (auto *I : ExprConstants)
      76        3565 :     I->dropAllReferences();
      77       46394 :   for (auto *I : ArrayConstants)
      78         425 :     I->dropAllReferences();
      79       48273 :   for (auto *I : StructConstants)
      80        2304 :     I->dropAllReferences();
      81       59230 :   for (auto *I : VectorConstants)
      82       13261 :     I->dropAllReferences();
      83       45969 :   ExprConstants.freeConstants();
      84       45969 :   ArrayConstants.freeConstants();
      85       45969 :   StructConstants.freeConstants();
      86       45969 :   VectorConstants.freeConstants();
      87       45969 :   InlineAsms.freeConstants();
      88             : 
      89       45969 :   CAZConstants.clear();
      90       45969 :   CPNConstants.clear();
      91       45969 :   UVConstants.clear();
      92       45969 :   IntConstants.clear();
      93       45969 :   FPConstants.clear();
      94             : 
      95      159195 :   for (auto &CDSConstant : CDSConstants)
      96       67257 :     delete CDSConstant.second;
      97       45969 :   CDSConstants.clear();
      98             : 
      99             :   // Destroy attributes.
     100       91938 :   for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
     101      328316 :          E = AttrsSet.end(); I != E; ) {
     102             :     FoldingSetIterator<AttributeImpl> Elem = I++;
     103      236378 :     delete &*Elem;
     104             :   }
     105             : 
     106             :   // Destroy attribute lists.
     107      306543 :   for (FoldingSetIterator<AttributeListImpl> I = AttrsLists.begin(),
     108       45969 :                                              E = AttrsLists.end();
     109      260574 :        I != E;) {
     110             :     FoldingSetIterator<AttributeListImpl> Elem = I++;
     111             :     delete &*Elem;
     112             :   }
     113             : 
     114             :   // Destroy attribute node lists.
     115      248396 :   for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
     116      248396 :          E = AttrsSetNodes.end(); I != E; ) {
     117             :     FoldingSetIterator<AttributeSetNode> Elem = I++;
     118             :     delete &*Elem;
     119             :   }
     120             : 
     121             :   // Destroy MetadataAsValues.
     122             :   {
     123             :     SmallVector<MetadataAsValue *, 8> MDVs;
     124       45969 :     MDVs.reserve(MetadataAsValues.size());
     125      103114 :     for (auto &Pair : MetadataAsValues)
     126       11176 :       MDVs.push_back(Pair.second);
     127       45969 :     MetadataAsValues.clear();
     128       68321 :     for (auto *V : MDVs)
     129       11176 :       delete V;
     130             :   }
     131             : 
     132             :   // Destroy ValuesAsMetadata.
     133       91938 :   for (auto &Pair : ValuesAsMetadata)
     134           0 :     delete Pair.second;
     135       45969 : }
     136             : 
     137        1047 : void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
     138             :   bool Changed;
     139        1059 :   do {
     140             :     Changed = false;
     141             : 
     142        1255 :     for (auto I = ArrayConstants.begin(), E = ArrayConstants.end(); I != E;) {
     143         196 :       auto *C = *I++;
     144         196 :       if (C->use_empty()) {
     145             :         Changed = true;
     146          12 :         C->destroyConstant();
     147             :       }
     148             :     }
     149             :   } while (Changed);
     150        1047 : }
     151             : 
     152        1047 : void Module::dropTriviallyDeadConstantArrays() {
     153        1047 :   Context.pImpl->dropTriviallyDeadConstantArrays();
     154        1047 : }
     155             : 
     156             : namespace llvm {
     157             : 
     158             : /// Make MDOperand transparent for hashing.
     159             : ///
     160             : /// This overload of an implementation detail of the hashing library makes
     161             : /// MDOperand hash to the same value as a \a Metadata pointer.
     162             : ///
     163             : /// Note that overloading \a hash_value() as follows:
     164             : ///
     165             : /// \code
     166             : ///     size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
     167             : /// \endcode
     168             : ///
     169             : /// does not cause MDOperand to be transparent.  In particular, a bare pointer
     170             : /// doesn't get hashed before it's combined, whereas \a MDOperand would.
     171             : static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
     172             : 
     173             : } // end namespace llvm
     174             : 
     175       20198 : unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
     176       40396 :   unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
     177             : #ifndef NDEBUG
     178             :   {
     179             :     SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
     180             :     unsigned RawHash = calculateHash(MDs);
     181             :     assert(Hash == RawHash &&
     182             :            "Expected hash of MDOperand to equal hash of Metadata*");
     183             :   }
     184             : #endif
     185       20198 :   return Hash;
     186             : }
     187             : 
     188      908539 : unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
     189      908539 :   return hash_combine_range(Ops.begin(), Ops.end());
     190             : }
     191             : 
     192      149059 : StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
     193      149059 :   uint32_t NewIdx = BundleTagCache.size();
     194      298118 :   return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
     195             : }
     196             : 
     197        4481 : void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
     198        4481 :   Tags.resize(BundleTagCache.size());
     199       22413 :   for (const auto &T : BundleTagCache)
     200       26902 :     Tags[T.second] = T.first();
     201        4481 : }
     202             : 
     203          70 : uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
     204          70 :   auto I = BundleTagCache.find(Tag);
     205             :   assert(I != BundleTagCache.end() && "Unknown tag!");
     206          70 :   return I->second;
     207             : }
     208             : 
     209      111751 : SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) {
     210      111751 :   auto NewSSID = SSC.size();
     211             :   assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() &&
     212             :          "Hit the maximum number of synchronization scopes allowed!");
     213      223502 :   return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
     214             : }
     215             : 
     216        4602 : void LLVMContextImpl::getSyncScopeNames(
     217             :     SmallVectorImpl<StringRef> &SSNs) const {
     218        4602 :   SSNs.resize(SSC.size());
     219       18717 :   for (const auto &SSE : SSC)
     220       19026 :     SSNs[SSE.second] = SSE.first();
     221        4602 : }
     222             : 
     223             : /// Singleton instance of the OptBisect class.
     224             : ///
     225             : /// This singleton is accessed via the LLVMContext::getOptPassGate() function.
     226             : /// It provides a mechanism to disable passes and individual optimizations at
     227             : /// compile time based on a command line option (-opt-bisect-limit) in order to
     228             : /// perform a bisecting search for optimization-related problems.
     229             : ///
     230             : /// Even if multiple LLVMContext objects are created, they will all return the
     231             : /// same instance of OptBisect in order to provide a single bisect count.  Any
     232             : /// code that uses the OptBisect object should be serialized when bisection is
     233             : /// enabled in order to enable a consistent bisect count.
     234             : static ManagedStatic<OptBisect> OptBisector;
     235             : 
     236     8453312 : OptPassGate &LLVMContextImpl::getOptPassGate() const {
     237     8453312 :   if (!OPG)
     238       28560 :     OPG = &(*OptBisector);
     239     8453312 :   return *OPG;
     240             : }
     241             : 
     242           2 : void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) {
     243           2 :   this->OPG = &OPG;
     244           2 : }

Generated by: LCOV version 1.13