blob: a86363b632a35f80ffa1c09885d4c79d315f1d70 [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"
Bill Wendlinge38b8042012-09-26 21:07:29 +000015#include "llvm/Attributes.h"
Owen Anderson8e89e412010-09-08 18:03:32 +000016#include "llvm/Module.h"
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000017#include "llvm/ADT/STLExtras.h"
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000018#include <algorithm>
Dan Gohmanb29cda92010-04-15 17:08:50 +000019using namespace llvm;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000020
21LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
22 : TheTrueVal(0), TheFalseVal(0),
23 VoidTy(C, Type::VoidTyID),
24 LabelTy(C, Type::LabelTyID),
Dan Gohman518cda42011-12-17 00:04:22 +000025 HalfTy(C, Type::HalfTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000026 FloatTy(C, Type::FloatTyID),
27 DoubleTy(C, Type::DoubleTyID),
28 MetadataTy(C, Type::MetadataTyID),
29 X86_FP80Ty(C, Type::X86_FP80TyID),
30 FP128Ty(C, Type::FP128TyID),
31 PPC_FP128Ty(C, Type::PPC_FP128TyID),
Dale Johannesenbaa5d042010-09-10 20:55:01 +000032 X86_MMXTy(C, Type::X86_MMXTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000033 Int1Ty(C, 1),
34 Int8Ty(C, 8),
35 Int16Ty(C, 16),
36 Int32Ty(C, 32),
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000037 Int64Ty(C, 64) {
Chris Lattner60955d42010-04-06 00:44:45 +000038 InlineAsmDiagHandler = 0;
39 InlineAsmDiagContext = 0;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000040 NamedStructTypesUniqueID = 0;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000041}
42
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000043namespace {
44struct DropReferences {
45 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
46 // is a Constant*.
47 template<typename PairT>
48 void operator()(const PairT &P) {
49 P.second->dropAllReferences();
50 }
51};
Talin46e9b442012-02-05 20:54:10 +000052
53// Temporary - drops pair.first instead of second.
54struct DropFirst {
55 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
56 // is a Constant*.
57 template<typename PairT>
58 void operator()(const PairT &P) {
59 P.first->dropAllReferences();
60 }
61};
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000062}
63
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000064LLVMContextImpl::~LLVMContextImpl() {
Owen Anderson8e89e412010-09-08 18:03:32 +000065 // NOTE: We need to delete the contents of OwnedModules, but we have to
66 // duplicate it into a temporary vector, because the destructor of Module
67 // will try to remove itself from OwnedModules set. This would cause
68 // iterator invalidation if we iterated on the set directly.
69 std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end());
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000070 DeleteContainerPointers(Modules);
Owen Anderson8e89e412010-09-08 18:03:32 +000071
Chris Lattnerc7f9fd42012-01-23 15:20:12 +000072 // Free the constants. This is important to do here to ensure that they are
73 // freed before the LeakDetector is torn down.
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000074 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
75 DropReferences());
76 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +000077 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000078 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +000079 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000080 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +000081 DropFirst());
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000082 ExprConstants.freeConstants();
83 ArrayConstants.freeConstants();
84 StructConstants.freeConstants();
85 VectorConstants.freeConstants();
Chris Lattnerc7f9fd42012-01-23 15:20:12 +000086 DeleteContainerSeconds(CAZConstants);
87 DeleteContainerSeconds(CPNConstants);
88 DeleteContainerSeconds(UVConstants);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000089 InlineAsms.freeConstants();
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000090 DeleteContainerSeconds(IntConstants);
91 DeleteContainerSeconds(FPConstants);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000092
Chris Lattner3756b912012-01-23 22:57:10 +000093 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
94 E = CDSConstants.end(); I != E; ++I)
95 delete I->second;
96 CDSConstants.clear();
Bill Wendlinge38b8042012-09-26 21:07:29 +000097
98 // Destroy attributes.
99 for (FoldingSetIterator<AttributesImpl> I = AttrsSet.begin(),
100 E = AttrsSet.end(); I != E; ++I)
101 delete &*I;
Chris Lattner3756b912012-01-23 22:57:10 +0000102
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000103 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
104 // and the NonUniquedMDNodes sets, so copy the values out first.
105 SmallVector<MDNode*, 8> MDNodes;
106 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
107 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
Chris Lattner68ef6942011-07-13 04:22:39 +0000108 I != E; ++I)
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000109 MDNodes.push_back(&*I);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000110 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
Dan Gohman060d5ba2010-10-12 00:15:27 +0000111 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
Chris Lattner68ef6942011-07-13 04:22:39 +0000112 E = MDNodes.end(); I != E; ++I)
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000113 (*I)->destroy();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000114 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
115 "Destroying all MDNodes didn't empty the Context's sets.");
Bill Wendlinge38b8042012-09-26 21:07:29 +0000116
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000117 // Destroy MDStrings.
Nick Lewyckye9bb9a02011-07-12 00:26:08 +0000118 DeleteContainerSeconds(MDStringCache);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000119}
David Blaikiea379b1812011-12-20 02:50:00 +0000120
121// ConstantsContext anchors
122void UnaryConstantExpr::anchor() { }
123
124void BinaryConstantExpr::anchor() { }
125
126void SelectConstantExpr::anchor() { }
127
128void ExtractElementConstantExpr::anchor() { }
129
130void InsertElementConstantExpr::anchor() { }
131
132void ShuffleVectorConstantExpr::anchor() { }
133
134void ExtractValueConstantExpr::anchor() { }
135
136void InsertValueConstantExpr::anchor() { }
137
138void GetElementPtrConstantExpr::anchor() { }
139
140void CompareConstantExpr::anchor() { }