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/Module.h"
00018 #include <algorithm>
00019 using namespace llvm;
00020 
00021 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
00022   : TheTrueVal(0), TheFalseVal(0),
00023     VoidTy(C, Type::VoidTyID),
00024     LabelTy(C, Type::LabelTyID),
00025     HalfTy(C, Type::HalfTyID),
00026     FloatTy(C, Type::FloatTyID),
00027     DoubleTy(C, Type::DoubleTyID),
00028     MetadataTy(C, Type::MetadataTyID),
00029     X86_FP80Ty(C, Type::X86_FP80TyID),
00030     FP128Ty(C, Type::FP128TyID),
00031     PPC_FP128Ty(C, Type::PPC_FP128TyID),
00032     X86_MMXTy(C, Type::X86_MMXTyID),
00033     Int1Ty(C, 1),
00034     Int8Ty(C, 8),
00035     Int16Ty(C, 16),
00036     Int32Ty(C, 32),
00037     Int64Ty(C, 64) {
00038   InlineAsmDiagHandler = 0;
00039   InlineAsmDiagContext = 0;
00040   NamedStructTypesUniqueID = 0;
00041 }
00042 
00043 namespace {
00044 struct DropReferences {
00045   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
00046   // is a Constant*.
00047   template<typename PairT>
00048   void operator()(const PairT &P) {
00049     P.second->dropAllReferences();
00050   }
00051 };
00052 
00053 // Temporary - drops pair.first instead of second.
00054 struct DropFirst {
00055   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
00056   // is a Constant*.
00057   template<typename PairT>
00058   void operator()(const PairT &P) {
00059     P.first->dropAllReferences();
00060   }
00061 };
00062 }
00063 
00064 LLVMContextImpl::~LLVMContextImpl() {
00065   // NOTE: We need to delete the contents of OwnedModules, but we have to
00066   // duplicate it into a temporary vector, because the destructor of Module
00067   // will try to remove itself from OwnedModules set.  This would cause
00068   // iterator invalidation if we iterated on the set directly.
00069   std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end());
00070   DeleteContainerPointers(Modules);
00071   
00072   // Free the constants.  This is important to do here to ensure that they are
00073   // freed before the LeakDetector is torn down.
00074   std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
00075                 DropReferences());
00076   std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
00077                 DropFirst());
00078   std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
00079                 DropFirst());
00080   std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
00081                 DropFirst());
00082   ExprConstants.freeConstants();
00083   ArrayConstants.freeConstants();
00084   StructConstants.freeConstants();
00085   VectorConstants.freeConstants();
00086   DeleteContainerSeconds(CAZConstants);
00087   DeleteContainerSeconds(CPNConstants);
00088   DeleteContainerSeconds(UVConstants);
00089   InlineAsms.freeConstants();
00090   DeleteContainerSeconds(IntConstants);
00091   DeleteContainerSeconds(FPConstants);
00092   
00093   for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
00094        E = CDSConstants.end(); I != E; ++I)
00095     delete I->second;
00096   CDSConstants.clear();
00097 
00098   // Destroy attributes.
00099   for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
00100          E = AttrsSet.end(); I != E; ) {
00101     FoldingSetIterator<AttributeImpl> Elem = I++;
00102     delete &*Elem;
00103   }
00104 
00105   // Destroy attribute lists.
00106   for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
00107          E = AttrsLists.end(); I != E; ) {
00108     FoldingSetIterator<AttributeSetImpl> Elem = I++;
00109     delete &*Elem;
00110   }
00111 
00112   // Destroy attribute node lists.
00113   for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
00114          E = AttrsSetNodes.end(); I != E; ) {
00115     FoldingSetIterator<AttributeSetNode> Elem = I++;
00116     delete &*Elem;
00117   }
00118 
00119   // Destroy MDNodes.  ~MDNode can move and remove nodes between the MDNodeSet
00120   // and the NonUniquedMDNodes sets, so copy the values out first.
00121   SmallVector<MDNode*, 8> MDNodes;
00122   MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
00123   for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
00124        I != E; ++I)
00125     MDNodes.push_back(&*I);
00126   MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
00127   for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
00128          E = MDNodes.end(); I != E; ++I)
00129     (*I)->destroy();
00130   assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
00131          "Destroying all MDNodes didn't empty the Context's sets.");
00132 
00133   // Destroy MDStrings.
00134   DeleteContainerSeconds(MDStringCache);
00135 }
00136 
00137 // ConstantsContext anchors
00138 void UnaryConstantExpr::anchor() { }
00139 
00140 void BinaryConstantExpr::anchor() { }
00141 
00142 void SelectConstantExpr::anchor() { }
00143 
00144 void ExtractElementConstantExpr::anchor() { }
00145 
00146 void InsertElementConstantExpr::anchor() { }
00147 
00148 void ShuffleVectorConstantExpr::anchor() { }
00149 
00150 void ExtractValueConstantExpr::anchor() { }
00151 
00152 void InsertValueConstantExpr::anchor() { }
00153 
00154 void GetElementPtrConstantExpr::anchor() { }
00155 
00156 void CompareConstantExpr::anchor() { }