LLVM API Documentation

LLVMContextImpl.cpp
Go to the documentation of this file.
00001 //===-- LLVMContextImpl.cpp - Implement LLVMContextImpl -------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements the opaque LLVMContextImpl.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "LLVMContextImpl.h"
00015 #include "llvm/ADT/STLExtras.h"
00016 #include "llvm/IR/Attributes.h"
00017 #include "llvm/IR/DiagnosticInfo.h"
00018 #include "llvm/IR/Module.h"
00019 #include <algorithm>
00020 using namespace llvm;
00021 
00022 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
00023   : TheTrueVal(nullptr), TheFalseVal(nullptr),
00024     VoidTy(C, Type::VoidTyID),
00025     LabelTy(C, Type::LabelTyID),
00026     HalfTy(C, Type::HalfTyID),
00027     FloatTy(C, Type::FloatTyID),
00028     DoubleTy(C, Type::DoubleTyID),
00029     MetadataTy(C, Type::MetadataTyID),
00030     X86_FP80Ty(C, Type::X86_FP80TyID),
00031     FP128Ty(C, Type::FP128TyID),
00032     PPC_FP128Ty(C, Type::PPC_FP128TyID),
00033     X86_MMXTy(C, Type::X86_MMXTyID),
00034     Int1Ty(C, 1),
00035     Int8Ty(C, 8),
00036     Int16Ty(C, 16),
00037     Int32Ty(C, 32),
00038     Int64Ty(C, 64) {
00039   InlineAsmDiagHandler = nullptr;
00040   InlineAsmDiagContext = nullptr;
00041   DiagnosticHandler = nullptr;
00042   DiagnosticContext = nullptr;
00043   YieldCallback = nullptr;
00044   YieldOpaqueHandle = nullptr;
00045   NamedStructTypesUniqueID = 0;
00046 }
00047 
00048 namespace {
00049 struct DropReferences {
00050   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
00051   // is a Constant*.
00052   template <typename PairT> void operator()(const PairT &P) {
00053     P.second->dropAllReferences();
00054   }
00055 };
00056 
00057 // Temporary - drops pair.first instead of second.
00058 struct DropFirst {
00059   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
00060   // is a Constant*.
00061   template<typename PairT>
00062   void operator()(const PairT &P) {
00063     P.first->dropAllReferences();
00064   }
00065 };
00066 }
00067 
00068 LLVMContextImpl::~LLVMContextImpl() {
00069   // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
00070   // will call LLVMContextImpl::removeModule, thus invalidating iterators into
00071   // the container. Avoid iterators during this operation:
00072   while (!OwnedModules.empty())
00073     delete *OwnedModules.begin();
00074   
00075   // Free the constants.  This is important to do here to ensure that they are
00076   // freed before the LeakDetector is torn down.
00077   std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
00078                 DropFirst());
00079   std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
00080                 DropFirst());
00081   std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
00082                 DropFirst());
00083   std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
00084                 DropFirst());
00085   ExprConstants.freeConstants();
00086   ArrayConstants.freeConstants();
00087   StructConstants.freeConstants();
00088   VectorConstants.freeConstants();
00089   DeleteContainerSeconds(CAZConstants);
00090   DeleteContainerSeconds(CPNConstants);
00091   DeleteContainerSeconds(UVConstants);
00092   InlineAsms.freeConstants();
00093   DeleteContainerSeconds(IntConstants);
00094   DeleteContainerSeconds(FPConstants);
00095   
00096   for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
00097        E = CDSConstants.end(); I != E; ++I)
00098     delete I->second;
00099   CDSConstants.clear();
00100 
00101   // Destroy attributes.
00102   for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
00103          E = AttrsSet.end(); I != E; ) {
00104     FoldingSetIterator<AttributeImpl> Elem = I++;
00105     delete &*Elem;
00106   }
00107 
00108   // Destroy attribute lists.
00109   for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
00110          E = AttrsLists.end(); I != E; ) {
00111     FoldingSetIterator<AttributeSetImpl> Elem = I++;
00112     delete &*Elem;
00113   }
00114 
00115   // Destroy attribute node lists.
00116   for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
00117          E = AttrsSetNodes.end(); I != E; ) {
00118     FoldingSetIterator<AttributeSetNode> Elem = I++;
00119     delete &*Elem;
00120   }
00121 
00122   // Destroy MDNodes.  ~MDNode can move and remove nodes between the MDNodeSet
00123   // and the NonUniquedMDNodes sets, so copy the values out first.
00124   SmallVector<MDNode*, 8> MDNodes;
00125   MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
00126   for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
00127        I != E; ++I)
00128     MDNodes.push_back(&*I);
00129   MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
00130   for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
00131          E = MDNodes.end(); I != E; ++I)
00132     (*I)->destroy();
00133   assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
00134          "Destroying all MDNodes didn't empty the Context's sets.");
00135 
00136   // Destroy MDStrings.
00137   DeleteContainerSeconds(MDStringCache);
00138 }
00139 
00140 // ConstantsContext anchors
00141 void UnaryConstantExpr::anchor() { }
00142 
00143 void BinaryConstantExpr::anchor() { }
00144 
00145 void SelectConstantExpr::anchor() { }
00146 
00147 void ExtractElementConstantExpr::anchor() { }
00148 
00149 void InsertElementConstantExpr::anchor() { }
00150 
00151 void ShuffleVectorConstantExpr::anchor() { }
00152 
00153 void ExtractValueConstantExpr::anchor() { }
00154 
00155 void InsertValueConstantExpr::anchor() { }
00156 
00157 void GetElementPtrConstantExpr::anchor() { }
00158 
00159 void CompareConstantExpr::anchor() { }