blob: 6279bb823dbf23b1e5823713296e992848c319b3 [file] [log] [blame]
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +00001//===-- 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"
Owen Anderson8e89e412010-09-08 18:03:32 +000015#include "llvm/Module.h"
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000016#include "llvm/ADT/STLExtras.h"
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000017#include <algorithm>
Dan Gohmanb29cda92010-04-15 17:08:50 +000018using namespace llvm;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000019
20LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
21 : TheTrueVal(0), TheFalseVal(0),
22 VoidTy(C, Type::VoidTyID),
23 LabelTy(C, Type::LabelTyID),
Dan Gohman518cda42011-12-17 00:04:22 +000024 HalfTy(C, Type::HalfTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000025 FloatTy(C, Type::FloatTyID),
26 DoubleTy(C, Type::DoubleTyID),
27 MetadataTy(C, Type::MetadataTyID),
28 X86_FP80Ty(C, Type::X86_FP80TyID),
29 FP128Ty(C, Type::FP128TyID),
30 PPC_FP128Ty(C, Type::PPC_FP128TyID),
Dale Johannesenbaa5d042010-09-10 20:55:01 +000031 X86_MMXTy(C, Type::X86_MMXTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000032 Int1Ty(C, 1),
33 Int8Ty(C, 8),
34 Int16Ty(C, 16),
35 Int32Ty(C, 32),
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000036 Int64Ty(C, 64) {
Chris Lattner60955d42010-04-06 00:44:45 +000037 InlineAsmDiagHandler = 0;
38 InlineAsmDiagContext = 0;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000039 NamedStructTypesUniqueID = 0;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000040}
41
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000042namespace {
43struct 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};
Talin46e9b442012-02-05 20:54:10 +000051
52// Temporary - drops pair.first instead of second.
53struct DropFirst {
54 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
55 // is a Constant*.
56 template<typename PairT>
57 void operator()(const PairT &P) {
58 P.first->dropAllReferences();
59 }
60};
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000061}
62
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000063LLVMContextImpl::~LLVMContextImpl() {
Owen Anderson8e89e412010-09-08 18:03:32 +000064 // NOTE: We need to delete the contents of OwnedModules, but we have to
65 // duplicate it into a temporary vector, because the destructor of Module
66 // will try to remove itself from OwnedModules set. This would cause
67 // iterator invalidation if we iterated on the set directly.
68 std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end());
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000069 DeleteContainerPointers(Modules);
Owen Anderson8e89e412010-09-08 18:03:32 +000070
Chris Lattnerc7f9fd42012-01-23 15:20:12 +000071 // Free the constants. This is important to do here to ensure that they are
72 // freed before the LeakDetector is torn down.
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000073 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
74 DropReferences());
75 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +000076 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000077 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +000078 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000079 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +000080 DropFirst());
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000081 ExprConstants.freeConstants();
82 ArrayConstants.freeConstants();
83 StructConstants.freeConstants();
84 VectorConstants.freeConstants();
Chris Lattnerc7f9fd42012-01-23 15:20:12 +000085 DeleteContainerSeconds(CAZConstants);
86 DeleteContainerSeconds(CPNConstants);
87 DeleteContainerSeconds(UVConstants);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000088 InlineAsms.freeConstants();
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000089 DeleteContainerSeconds(IntConstants);
90 DeleteContainerSeconds(FPConstants);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000091
Chris Lattner3756b912012-01-23 22:57:10 +000092 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
93 E = CDSConstants.end(); I != E; ++I)
94 delete I->second;
95 CDSConstants.clear();
96
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000097 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
98 // and the NonUniquedMDNodes sets, so copy the values out first.
99 SmallVector<MDNode*, 8> MDNodes;
100 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
101 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
Chris Lattner68ef6942011-07-13 04:22:39 +0000102 I != E; ++I)
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000103 MDNodes.push_back(&*I);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000104 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
Dan Gohman060d5ba2010-10-12 00:15:27 +0000105 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
Chris Lattner68ef6942011-07-13 04:22:39 +0000106 E = MDNodes.end(); I != E; ++I)
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000107 (*I)->destroy();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000108 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
109 "Destroying all MDNodes didn't empty the Context's sets.");
110 // Destroy MDStrings.
Nick Lewyckye9bb9a02011-07-12 00:26:08 +0000111 DeleteContainerSeconds(MDStringCache);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000112}
David Blaikiea379b1812011-12-20 02:50:00 +0000113
114// ConstantsContext anchors
115void UnaryConstantExpr::anchor() { }
116
117void BinaryConstantExpr::anchor() { }
118
119void SelectConstantExpr::anchor() { }
120
121void ExtractElementConstantExpr::anchor() { }
122
123void InsertElementConstantExpr::anchor() { }
124
125void ShuffleVectorConstantExpr::anchor() { }
126
127void ExtractValueConstantExpr::anchor() { }
128
129void InsertValueConstantExpr::anchor() { }
130
131void GetElementPtrConstantExpr::anchor() { }
132
133void CompareConstantExpr::anchor() { }