blob: ebff9d3a51f69631d38e93e05e6f84691a2d5d84 [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"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/ADT/STLExtras.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000016#include "llvm/IR/Attributes.h"
17#include "llvm/IR/Module.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) {
Bob Wilsona594fab2013-02-11 05:37:07 +000038 InlineAsmDiagHandler = 0;
39 InlineAsmDiagContext = 0;
Quentin Colombetb4c44d22013-12-17 17:47:22 +000040 DiagnosticHandler = 0;
41 DiagnosticContext = 0;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000042 NamedStructTypesUniqueID = 0;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000043}
44
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000045namespace {
46struct DropReferences {
47 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
48 // is a Constant*.
49 template<typename PairT>
50 void operator()(const PairT &P) {
51 P.second->dropAllReferences();
52 }
53};
Talin46e9b442012-02-05 20:54:10 +000054
55// Temporary - drops pair.first instead of second.
56struct DropFirst {
57 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
58 // is a Constant*.
59 template<typename PairT>
60 void operator()(const PairT &P) {
61 P.first->dropAllReferences();
62 }
63};
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000064}
65
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000066LLVMContextImpl::~LLVMContextImpl() {
Owen Anderson8e89e412010-09-08 18:03:32 +000067 // NOTE: We need to delete the contents of OwnedModules, but we have to
68 // duplicate it into a temporary vector, because the destructor of Module
69 // will try to remove itself from OwnedModules set. This would cause
70 // iterator invalidation if we iterated on the set directly.
71 std::vector<Module*> Modules(OwnedModules.begin(), OwnedModules.end());
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000072 DeleteContainerPointers(Modules);
Owen Anderson8e89e412010-09-08 18:03:32 +000073
Chris Lattnerc7f9fd42012-01-23 15:20:12 +000074 // Free the constants. This is important to do here to ensure that they are
75 // freed before the LeakDetector is torn down.
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000076 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
77 DropReferences());
78 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +000079 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000080 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +000081 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000082 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +000083 DropFirst());
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000084 ExprConstants.freeConstants();
85 ArrayConstants.freeConstants();
86 StructConstants.freeConstants();
87 VectorConstants.freeConstants();
Chris Lattnerc7f9fd42012-01-23 15:20:12 +000088 DeleteContainerSeconds(CAZConstants);
89 DeleteContainerSeconds(CPNConstants);
90 DeleteContainerSeconds(UVConstants);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000091 InlineAsms.freeConstants();
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000092 DeleteContainerSeconds(IntConstants);
93 DeleteContainerSeconds(FPConstants);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000094
Chris Lattner3756b912012-01-23 22:57:10 +000095 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
96 E = CDSConstants.end(); I != E; ++I)
97 delete I->second;
98 CDSConstants.clear();
Bill Wendlinge38b8042012-09-26 21:07:29 +000099
100 // Destroy attributes.
Bill Wendling4607f4b2012-12-20 01:36:59 +0000101 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000102 E = AttrsSet.end(); I != E; ) {
Bill Wendling4607f4b2012-12-20 01:36:59 +0000103 FoldingSetIterator<AttributeImpl> Elem = I++;
Benjamin Kramer6bbdf702012-10-14 08:48:40 +0000104 delete &*Elem;
105 }
106
Bill Wendlingf86efb92012-11-20 05:09:20 +0000107 // Destroy attribute lists.
Bill Wendling6848e382012-12-19 22:42:22 +0000108 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000109 E = AttrsLists.end(); I != E; ) {
Bill Wendling6848e382012-12-19 22:42:22 +0000110 FoldingSetIterator<AttributeSetImpl> Elem = I++;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000111 delete &*Elem;
112 }
113
Bill Wendling164a4fb2013-01-24 00:14:46 +0000114 // Destroy attribute node lists.
115 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
116 E = AttrsSetNodes.end(); I != E; ) {
117 FoldingSetIterator<AttributeSetNode> Elem = I++;
118 delete &*Elem;
119 }
120
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000121 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
122 // and the NonUniquedMDNodes sets, so copy the values out first.
123 SmallVector<MDNode*, 8> MDNodes;
124 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
125 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
Chris Lattner68ef6942011-07-13 04:22:39 +0000126 I != E; ++I)
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000127 MDNodes.push_back(&*I);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000128 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
Dan Gohman060d5ba2010-10-12 00:15:27 +0000129 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
Chris Lattner68ef6942011-07-13 04:22:39 +0000130 E = MDNodes.end(); I != E; ++I)
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000131 (*I)->destroy();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000132 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
133 "Destroying all MDNodes didn't empty the Context's sets.");
Bill Wendlinge38b8042012-09-26 21:07:29 +0000134
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000135 // Destroy MDStrings.
Nick Lewyckye9bb9a02011-07-12 00:26:08 +0000136 DeleteContainerSeconds(MDStringCache);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000137}
David Blaikiea379b1812011-12-20 02:50:00 +0000138
139// ConstantsContext anchors
140void UnaryConstantExpr::anchor() { }
141
142void BinaryConstantExpr::anchor() { }
143
144void SelectConstantExpr::anchor() { }
145
146void ExtractElementConstantExpr::anchor() { }
147
148void InsertElementConstantExpr::anchor() { }
149
150void ShuffleVectorConstantExpr::anchor() { }
151
152void ExtractValueConstantExpr::anchor() { }
153
154void InsertValueConstantExpr::anchor() { }
155
156void GetElementPtrConstantExpr::anchor() { }
157
158void CompareConstantExpr::anchor() { }