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