cache result of operator*
[oota-llvm.git] / lib / VMCore / LLVMContextImpl.cpp
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 <algorithm>
16 using namespace llvm;
17
18 LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
19   : TheTrueVal(0), TheFalseVal(0),
20     VoidTy(C, Type::VoidTyID),
21     LabelTy(C, Type::LabelTyID),
22     FloatTy(C, Type::FloatTyID),
23     DoubleTy(C, Type::DoubleTyID),
24     MetadataTy(C, Type::MetadataTyID),
25     X86_FP80Ty(C, Type::X86_FP80TyID),
26     FP128Ty(C, Type::FP128TyID),
27     PPC_FP128Ty(C, Type::PPC_FP128TyID),
28     Int1Ty(C, 1),
29     Int8Ty(C, 8),
30     Int16Ty(C, 16),
31     Int32Ty(C, 32),
32     Int64Ty(C, 64),
33     AlwaysOpaqueTy(new OpaqueType(C)) {
34   InlineAsmDiagHandler = 0;
35   InlineAsmDiagContext = 0;
36       
37   // Make sure the AlwaysOpaqueTy stays alive as long as the Context.
38   AlwaysOpaqueTy->addRef();
39   OpaqueTypes.insert(AlwaysOpaqueTy);
40 }
41
42 namespace {
43 struct DropReferences {
44   // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
45   // is a Constant*.
46   template<typename PairT>
47   void operator()(const PairT &P) {
48     P.second->dropAllReferences();
49   }
50 };
51 }
52
53 LLVMContextImpl::~LLVMContextImpl() {
54   std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
55                 DropReferences());
56   std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
57                 DropReferences());
58   std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
59                 DropReferences());
60   std::for_each(UnionConstants.map_begin(), UnionConstants.map_end(),
61                 DropReferences());
62   std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
63                 DropReferences());
64   ExprConstants.freeConstants();
65   ArrayConstants.freeConstants();
66   StructConstants.freeConstants();
67   UnionConstants.freeConstants();
68   VectorConstants.freeConstants();
69   AggZeroConstants.freeConstants();
70   NullPtrConstants.freeConstants();
71   UndefValueConstants.freeConstants();
72   InlineAsms.freeConstants();
73   for (IntMapTy::iterator I = IntConstants.begin(), E = IntConstants.end(); 
74        I != E; ++I) {
75     delete I->second;
76   }
77   for (FPMapTy::iterator I = FPConstants.begin(), E = FPConstants.end(); 
78        I != E; ++I) {
79     delete I->second;
80   }
81   AlwaysOpaqueTy->dropRef();
82   for (OpaqueTypesTy::iterator I = OpaqueTypes.begin(), E = OpaqueTypes.end();
83        I != E; ++I) {
84     (*I)->AbstractTypeUsers.clear();
85     delete *I;
86   }
87   // Destroy MDNodes.  ~MDNode can move and remove nodes between the MDNodeSet
88   // and the NonUniquedMDNodes sets, so copy the values out first.
89   SmallVector<MDNode*, 8> MDNodes;
90   MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
91   for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
92        I != E; ++I) {
93     MDNodes.push_back(&*I);
94   }
95   MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
96   for (SmallVector<MDNode*, 8>::iterator I = MDNodes.begin(),
97          E = MDNodes.end(); I != E; ++I) {
98     (*I)->destroy();
99   }
100   assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
101          "Destroying all MDNodes didn't empty the Context's sets.");
102   // Destroy MDStrings.
103   for (StringMap<MDString*>::iterator I = MDStringCache.begin(),
104          E = MDStringCache.end(); I != E; ++I) {
105     delete I->second;
106   }
107 }