LLVMContextImpl.cpp revision 288943
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 "llvm/ADT/STLExtras.h"
16#include "llvm/IR/Attributes.h"
17#include "llvm/IR/DiagnosticInfo.h"
18#include "llvm/IR/Module.h"
19#include <algorithm>
20using namespace llvm;
21
22LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
23  : TheTrueVal(nullptr), TheFalseVal(nullptr),
24    VoidTy(C, Type::VoidTyID),
25    LabelTy(C, Type::LabelTyID),
26    HalfTy(C, Type::HalfTyID),
27    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),
33    X86_MMXTy(C, Type::X86_MMXTyID),
34    Int1Ty(C, 1),
35    Int8Ty(C, 8),
36    Int16Ty(C, 16),
37    Int32Ty(C, 32),
38    Int64Ty(C, 64),
39    Int128Ty(C, 128) {
40  InlineAsmDiagHandler = nullptr;
41  InlineAsmDiagContext = nullptr;
42  DiagnosticHandler = nullptr;
43  DiagnosticContext = nullptr;
44  RespectDiagnosticFilters = false;
45  YieldCallback = nullptr;
46  YieldOpaqueHandle = nullptr;
47  NamedStructTypesUniqueID = 0;
48}
49
50namespace {
51struct DropReferences {
52  // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
53  // is a Constant*.
54  template <typename PairT> void operator()(const PairT &P) {
55    P.second->dropAllReferences();
56  }
57};
58
59// Temporary - drops pair.first instead of second.
60struct DropFirst {
61  // Takes the value_type of a ConstantUniqueMap's internal map, whose 'second'
62  // is a Constant*.
63  template<typename PairT>
64  void operator()(const PairT &P) {
65    P.first->dropAllReferences();
66  }
67};
68}
69
70LLVMContextImpl::~LLVMContextImpl() {
71  // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
72  // will call LLVMContextImpl::removeModule, thus invalidating iterators into
73  // the container. Avoid iterators during this operation:
74  while (!OwnedModules.empty())
75    delete *OwnedModules.begin();
76
77  // Drop references for MDNodes.  Do this before Values get deleted to avoid
78  // unnecessary RAUW when nodes are still unresolved.
79  for (auto *I : DistinctMDNodes)
80    I->dropAllReferences();
81#define HANDLE_MDNODE_LEAF(CLASS)                                              \
82  for (auto *I : CLASS##s)                                                     \
83    I->dropAllReferences();
84#include "llvm/IR/Metadata.def"
85
86  // Also drop references that come from the Value bridges.
87  for (auto &Pair : ValuesAsMetadata)
88    Pair.second->dropUsers();
89  for (auto &Pair : MetadataAsValues)
90    Pair.second->dropUse();
91
92  // Destroy MDNodes.
93  for (MDNode *I : DistinctMDNodes)
94    I->deleteAsSubclass();
95#define HANDLE_MDNODE_LEAF(CLASS)                                              \
96  for (CLASS *I : CLASS##s)                                                    \
97    delete I;
98#include "llvm/IR/Metadata.def"
99
100  // Free the constants.
101  std::for_each(ExprConstants.map_begin(), ExprConstants.map_end(),
102                DropFirst());
103  std::for_each(ArrayConstants.map_begin(), ArrayConstants.map_end(),
104                DropFirst());
105  std::for_each(StructConstants.map_begin(), StructConstants.map_end(),
106                DropFirst());
107  std::for_each(VectorConstants.map_begin(), VectorConstants.map_end(),
108                DropFirst());
109  ExprConstants.freeConstants();
110  ArrayConstants.freeConstants();
111  StructConstants.freeConstants();
112  VectorConstants.freeConstants();
113  DeleteContainerSeconds(CAZConstants);
114  DeleteContainerSeconds(CPNConstants);
115  DeleteContainerSeconds(UVConstants);
116  InlineAsms.freeConstants();
117  DeleteContainerSeconds(IntConstants);
118  DeleteContainerSeconds(FPConstants);
119
120  for (StringMap<ConstantDataSequential*>::iterator I = CDSConstants.begin(),
121       E = CDSConstants.end(); I != E; ++I)
122    delete I->second;
123  CDSConstants.clear();
124
125  // Destroy attributes.
126  for (FoldingSetIterator<AttributeImpl> I = AttrsSet.begin(),
127         E = AttrsSet.end(); I != E; ) {
128    FoldingSetIterator<AttributeImpl> Elem = I++;
129    delete &*Elem;
130  }
131
132  // Destroy attribute lists.
133  for (FoldingSetIterator<AttributeSetImpl> I = AttrsLists.begin(),
134         E = AttrsLists.end(); I != E; ) {
135    FoldingSetIterator<AttributeSetImpl> Elem = I++;
136    delete &*Elem;
137  }
138
139  // Destroy attribute node lists.
140  for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
141         E = AttrsSetNodes.end(); I != E; ) {
142    FoldingSetIterator<AttributeSetNode> Elem = I++;
143    delete &*Elem;
144  }
145
146  // Destroy MetadataAsValues.
147  {
148    SmallVector<MetadataAsValue *, 8> MDVs;
149    MDVs.reserve(MetadataAsValues.size());
150    for (auto &Pair : MetadataAsValues)
151      MDVs.push_back(Pair.second);
152    MetadataAsValues.clear();
153    for (auto *V : MDVs)
154      delete V;
155  }
156
157  // Destroy ValuesAsMetadata.
158  for (auto &Pair : ValuesAsMetadata)
159    delete Pair.second;
160
161  // Destroy MDStrings.
162  MDStringCache.clear();
163}
164
165void LLVMContextImpl::dropTriviallyDeadConstantArrays() {
166  bool Changed;
167  do {
168    Changed = false;
169
170    for (auto I = ArrayConstants.map_begin(), E = ArrayConstants.map_end();
171         I != E; ) {
172      auto *C = I->first;
173      I++;
174      if (C->use_empty()) {
175        Changed = true;
176        C->destroyConstant();
177      }
178    }
179
180  } while (Changed);
181}
182
183void Module::dropTriviallyDeadConstantArrays() {
184  Context.pImpl->dropTriviallyDeadConstantArrays();
185}
186
187namespace llvm {
188/// \brief Make MDOperand transparent for hashing.
189///
190/// This overload of an implementation detail of the hashing library makes
191/// MDOperand hash to the same value as a \a Metadata pointer.
192///
193/// Note that overloading \a hash_value() as follows:
194///
195/// \code
196///     size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
197/// \endcode
198///
199/// does not cause MDOperand to be transparent.  In particular, a bare pointer
200/// doesn't get hashed before it's combined, whereas \a MDOperand would.
201static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
202}
203
204unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
205  unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
206#ifndef NDEBUG
207  {
208    SmallVector<Metadata *, 8> MDs(N->op_begin() + Offset, N->op_end());
209    unsigned RawHash = calculateHash(MDs);
210    assert(Hash == RawHash &&
211           "Expected hash of MDOperand to equal hash of Metadata*");
212  }
213#endif
214  return Hash;
215}
216
217unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
218  return hash_combine_range(Ops.begin(), Ops.end());
219}
220
221// ConstantsContext anchors
222void UnaryConstantExpr::anchor() { }
223
224void BinaryConstantExpr::anchor() { }
225
226void SelectConstantExpr::anchor() { }
227
228void ExtractElementConstantExpr::anchor() { }
229
230void InsertElementConstantExpr::anchor() { }
231
232void ShuffleVectorConstantExpr::anchor() { }
233
234void ExtractValueConstantExpr::anchor() { }
235
236void InsertValueConstantExpr::anchor() { }
237
238void GetElementPtrConstantExpr::anchor() { }
239
240void CompareConstantExpr::anchor() { }
241
242