blob: 6513965ae7ad88da2eead12b348685b98a3f4bc4 [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"
Diego Novillo7f8af8b2014-05-22 14:19:46 +000017#include "llvm/IR/DiagnosticInfo.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000018#include "llvm/IR/Module.h"
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000019#include <algorithm>
Dan Gohmanb29cda92010-04-15 17:08:50 +000020using namespace llvm;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000021
22LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
Craig Topperc6207612014-04-09 06:08:46 +000023 : TheTrueVal(nullptr), TheFalseVal(nullptr),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000024 VoidTy(C, Type::VoidTyID),
25 LabelTy(C, Type::LabelTyID),
Dan Gohman518cda42011-12-17 00:04:22 +000026 HalfTy(C, Type::HalfTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000027 FloatTy(C, Type::FloatTyID),
28 DoubleTy(C, Type::DoubleTyID),
29 MetadataTy(C, Type::MetadataTyID),
30 X86_FP80Ty(C, Type::X86_FP80TyID),
31 FP128Ty(C, Type::FP128TyID),
32 PPC_FP128Ty(C, Type::PPC_FP128TyID),
Dale Johannesenbaa5d042010-09-10 20:55:01 +000033 X86_MMXTy(C, Type::X86_MMXTyID),
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000034 Int1Ty(C, 1),
35 Int8Ty(C, 8),
36 Int16Ty(C, 16),
37 Int32Ty(C, 32),
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000038 Int64Ty(C, 64) {
Craig Topperc6207612014-04-09 06:08:46 +000039 InlineAsmDiagHandler = nullptr;
40 InlineAsmDiagContext = nullptr;
41 DiagnosticHandler = nullptr;
42 DiagnosticContext = nullptr;
Juergen Ributzka34390c72014-05-16 02:33:15 +000043 YieldCallback = nullptr;
44 YieldOpaqueHandle = nullptr;
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000045 NamedStructTypesUniqueID = 0;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000046}
47
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000048namespace {
49struct DropReferences {
50 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
51 // is a Constant*.
Diego Novillo7f8af8b2014-05-22 14:19:46 +000052 template <typename PairT> void operator()(const PairT &P) {
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000053 P.second->dropAllReferences();
54 }
55};
Talin46e9b442012-02-05 20:54:10 +000056
57// Temporary - drops pair.first instead of second.
58struct DropFirst {
59 // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
60 // is a Constant*.
61 template<typename PairT>
62 void operator()(const PairT &P) {
63 P.first->dropAllReferences();
64 }
65};
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000066}
67
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000068LLVMContextImpl::~LLVMContextImpl() {
David Blaikie4c82a802014-04-21 21:27:19 +000069 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
70 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
71 // the container. Avoid iterators during this operation:
72 while (!OwnedModules.empty())
73 delete *OwnedModules.begin();
Owen Anderson8e89e412010-09-08 18:03:32 +000074
Chris Lattnerc7f9fd42012-01-23 15:20:12 +000075 // Free the constants. This is important to do here to ensure that they are
76 // freed before the LeakDetector is torn down.
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000077 std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
Duncan P. N. Exon Smith317c1392014-08-19 16:39:58 +000078 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000079 std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +000080 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000081 std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +000082 DropFirst());
Jeffrey Yasskina6eedc32010-03-22 05:23:37 +000083 std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
Talin46e9b442012-02-05 20:54:10 +000084 DropFirst());
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000085 ExprConstants.freeConstants();
86 ArrayConstants.freeConstants();
87 StructConstants.freeConstants();
88 VectorConstants.freeConstants();
Chris Lattnerc7f9fd42012-01-23 15:20:12 +000089 DeleteContainerSeconds(CAZConstants);
90 DeleteContainerSeconds(CPNConstants);
91 DeleteContainerSeconds(UVConstants);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000092 InlineAsms.freeConstants();
Nick Lewyckye9bb9a02011-07-12 00:26:08 +000093 DeleteContainerSeconds(IntConstants);
94 DeleteContainerSeconds(FPConstants);
Chris Lattnerb1ed91f2011-07-09 17:41:24 +000095
Chris Lattner3756b912012-01-23 22:57:10 +000096 for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
97 E = CDSConstants.end(); I != E; ++I)
98 delete I->second;
99 CDSConstants.clear();
Bill Wendlinge38b8042012-09-26 21:07:29 +0000100
101 // Destroy attributes.
Bill Wendling4607f4b2012-12-20 01:36:59 +0000102 for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000103 E = AttrsSet.end(); I != E; ) {
Bill Wendling4607f4b2012-12-20 01:36:59 +0000104 FoldingSetIterator<AttributeImpl> Elem = I++;
Benjamin Kramer6bbdf702012-10-14 08:48:40 +0000105 delete &*Elem;
106 }
107
Bill Wendlingf86efb92012-11-20 05:09:20 +0000108 // Destroy attribute lists.
Bill Wendling6848e382012-12-19 22:42:22 +0000109 for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
Bill Wendlingf86efb92012-11-20 05:09:20 +0000110 E = AttrsLists.end(); I != E; ) {
Bill Wendling6848e382012-12-19 22:42:22 +0000111 FoldingSetIterator<AttributeSetImpl> Elem = I++;
Bill Wendlingf86efb92012-11-20 05:09:20 +0000112 delete &*Elem;
113 }
114
Bill Wendling164a4fb2013-01-24 00:14:46 +0000115 // Destroy attribute node lists.
116 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
117 E = AttrsSetNodes.end(); I != E; ) {
118 FoldingSetIterator<AttributeSetNode> Elem = I++;
119 delete &*Elem;
120 }
121
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000122 // Destroy MDNodes. ~MDNode can move and remove nodes between the MDNodeSet
123 // and the NonUniquedMDNodes sets, so copy the values out first.
124 SmallVector<MDNode*, 8> MDNodes;
125 MDNodes.reserve(MDNodeSet.size() + NonUniquedMDNodes.size());
126 for (FoldingSetIterator<MDNode> I = MDNodeSet.begin(), E = MDNodeSet.end();
Chris Lattner68ef6942011-07-13 04:22:39 +0000127 I != E; ++I)
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000128 MDNodes.push_back(&*I);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000129 MDNodes.append(NonUniquedMDNodes.begin(), NonUniquedMDNodes.end());
Dan Gohman060d5ba2010-10-12 00:15:27 +0000130 for (SmallVectorImpl<MDNode *>::iterator I = MDNodes.begin(),
Chris Lattner68ef6942011-07-13 04:22:39 +0000131 E = MDNodes.end(); I != E; ++I)
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000132 (*I)->destroy();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000133 assert(MDNodeSet.empty() && NonUniquedMDNodes.empty() &&
134 "Destroying all MDNodes didn't empty the Context's sets.");
Bill Wendlinge38b8042012-09-26 21:07:29 +0000135
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000136 // Destroy MDStrings.
Nick Lewyckye9bb9a02011-07-12 00:26:08 +0000137 DeleteContainerSeconds(MDStringCache);
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000138}
David Blaikiea379b1812011-12-20 02:50:00 +0000139
140// ConstantsContext anchors
141void UnaryConstantExpr::anchor() { }
142
143void BinaryConstantExpr::anchor() { }
144
145void SelectConstantExpr::anchor() { }
146
147void ExtractElementConstantExpr::anchor() { }
148
149void InsertElementConstantExpr::anchor() { }
150
151void ShuffleVectorConstantExpr::anchor() { }
152
153void ExtractValueConstantExpr::anchor() { }
154
155void InsertValueConstantExpr::anchor() { }
156
157void GetElementPtrConstantExpr::anchor() { }
158
159void CompareConstantExpr::anchor() { }