blob: dfeabb4cbc7c141017f7152aa0cc6718cbe808c6 [file] [log] [blame]
Eugene Zelenkode6cce22017-06-19 22:05:08 +00001//===- LLVMContextImpl.cpp - Implement LLVMContextImpl --------------------===//
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the opaque LLVMContextImpl.
10//
11//===----------------------------------------------------------------------===//
12
13#include "LLVMContextImpl.h"
serge-sans-paillee188aae2022-01-31 22:35:07 +010014#include "AttributeImpl.h"
Benjamin Kramer81f385b2020-01-21 15:54:48 +010015#include "llvm/ADT/SetVector.h"
serge-sans-paillee188aae2022-01-31 22:35:07 +010016#include "llvm/ADT/StringMapEntry.h"
17#include "llvm/ADT/iterator.h"
serge-sans-paillee188aae2022-01-31 22:35:07 +010018#include "llvm/IR/DiagnosticHandler.h"
19#include "llvm/IR/LLVMRemarkStreamer.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000020#include "llvm/IR/Module.h"
Andrew Kayloraa641a52016-04-22 22:06:11 +000021#include "llvm/IR/OptBisect.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000022#include "llvm/IR/Type.h"
serge-sans-paillee188aae2022-01-31 22:35:07 +010023#include "llvm/IR/Use.h"
24#include "llvm/IR/User.h"
25#include "llvm/Remarks/RemarkStreamer.h"
serge-sans-paillee188aae2022-01-31 22:35:07 +010026#include "llvm/Support/Compiler.h"
27#include "llvm/Support/ErrorHandling.h"
Eugene Zelenkode6cce22017-06-19 22:05:08 +000028#include <cassert>
29#include <utility>
30
Dan Gohmanb29cda92010-04-15 17:08:50 +000031using namespace llvm;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000032
33LLVMContextImpl::LLVMContextImpl(LLVMContext &C)
Arthur Eubanks4c8174f2021-06-24 08:21:24 -070034 : DiagHandler(std::make_unique<DiagnosticHandler>()),
35 VoidTy(C, Type::VoidTyID), LabelTy(C, Type::LabelTyID),
36 HalfTy(C, Type::HalfTyID), BFloatTy(C, Type::BFloatTyID),
37 FloatTy(C, Type::FloatTyID), DoubleTy(C, Type::DoubleTyID),
38 MetadataTy(C, Type::MetadataTyID), TokenTy(C, Type::TokenTyID),
39 X86_FP80Ty(C, Type::X86_FP80TyID), FP128Ty(C, Type::FP128TyID),
James Y Knightdfeb3992024-07-25 09:19:22 -040040 PPC_FP128Ty(C, Type::PPC_FP128TyID), X86_AMXTy(C, Type::X86_AMXTyID),
41 Int1Ty(C, 1), Int8Ty(C, 8), Int16Ty(C, 16), Int32Ty(C, 32),
42 Int64Ty(C, 64), Int128Ty(C, 128) {}
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +000043
44LLVMContextImpl::~LLVMContextImpl() {
Jeremy Morsef1b0a542023-11-08 14:58:34 +000045#ifndef NDEBUG
46 // Check that any variable location records that fell off the end of a block
47 // when it's terminator was removed were eventually replaced. This assertion
Stephen Tozerffd08c72024-03-19 20:07:07 +000048 // firing indicates that DbgVariableRecords went missing during the lifetime
49 // of the LLVMContext.
Stephen Tozer360da832024-03-13 16:39:35 +000050 assert(TrailingDbgRecords.empty() && "DbgRecords in blocks not cleaned");
Jeremy Morsef1b0a542023-11-08 14:58:34 +000051#endif
52
David Blaikie4c82a802014-04-21 21:27:19 +000053 // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
54 // will call LLVMContextImpl::removeModule, thus invalidating iterators into
55 // the container. Avoid iterators during this operation:
56 while (!OwnedModules.empty())
57 delete *OwnedModules.begin();
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000058
Vedant Kumar69ee62c2018-06-29 20:13:13 +000059#ifndef NDEBUG
Serge Pavlov7975b8c2019-09-14 00:21:24 +070060 // Check for metadata references from leaked Values.
61 for (auto &Pair : ValueMetadata)
Vedant Kumar69ee62c2018-06-29 20:13:13 +000062 Pair.first->dump();
Serge Pavlov7975b8c2019-09-14 00:21:24 +070063 assert(ValueMetadata.empty() && "Values with metadata have been leaked");
Vedant Kumar69ee62c2018-06-29 20:13:13 +000064#endif
65
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000066 // Drop references for MDNodes. Do this before Values get deleted to avoid
67 // unnecessary RAUW when nodes are still unresolved.
Stephen Tozerf99a0202023-11-17 17:52:24 +000068 for (auto *I : DistinctMDNodes)
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000069 I->dropAllReferences();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +000070#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000071 for (auto *I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000072 I->dropAllReferences();
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000073#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000074
75 // Also drop references that come from the Value bridges.
76 for (auto &Pair : ValuesAsMetadata)
77 Pair.second->dropUsers();
78 for (auto &Pair : MetadataAsValues)
79 Pair.second->dropUse();
Stephen Tozerf99a0202023-11-17 17:52:24 +000080 // Do not untrack ValueAsMetadata references for DIArgLists, as they have
81 // already been more efficiently untracked above.
82 for (DIArgList *AL : DIArgLists) {
83 AL->dropAllReferences(/* Untrack */ false);
84 delete AL;
85 }
86 DIArgLists.clear();
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000087
88 // Destroy MDNodes.
Duncan P. N. Exon Smith2bc00f42015-01-19 23:13:14 +000089 for (MDNode *I : DistinctMDNodes)
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000090 I->deleteAsSubclass();
Haopeng Liu5ece35d2024-06-21 12:09:00 -070091
92 for (auto *ConstantRangeListAttribute : ConstantRangeListAttributes)
93 ConstantRangeListAttribute->~ConstantRangeListAttributeImpl();
Duncan P. N. Exon Smith55ca9642015-08-03 17:26:41 +000094#define HANDLE_MDNODE_LEAF_UNIQUABLE(CLASS) \
95 for (CLASS * I : CLASS##s) \
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000096 delete I;
Duncan P. N. Exon Smith408f5a22015-01-20 01:18:32 +000097#include "llvm/IR/Metadata.def"
Duncan P. N. Exon Smithe16d5872015-01-14 21:58:17 +000098
Benjamin Kramercb36bec2015-01-22 21:43:01 +000099 // Free the constants.
Duncan P. N. Exon Smithef06d442016-04-06 17:56:08 +0000100 for (auto *I : ExprConstants)
101 I->dropAllReferences();
102 for (auto *I : ArrayConstants)
103 I->dropAllReferences();
104 for (auto *I : StructConstants)
105 I->dropAllReferences();
106 for (auto *I : VectorConstants)
107 I->dropAllReferences();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000108 ExprConstants.freeConstants();
109 ArrayConstants.freeConstants();
110 StructConstants.freeConstants();
111 VectorConstants.freeConstants();
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000112 InlineAsms.freeConstants();
Justin Lebar611c5c22016-10-10 16:26:13 +0000113
114 CAZConstants.clear();
115 CPNConstants.clear();
Joshua Cranmere6b02212022-12-20 11:02:11 -0500116 CTNConstants.clear();
Justin Lebar611c5c22016-10-10 16:26:13 +0000117 UVConstants.clear();
Zhengyang Liu345fccc2020-11-26 00:10:36 -0700118 PVConstants.clear();
Arthur Eubanksc3166752023-02-23 21:16:27 -0800119 IntZeroConstants.clear();
Arthur Eubanks86bdcdf2023-02-23 21:47:03 -0800120 IntOneConstants.clear();
Justin Lebar611c5c22016-10-10 16:26:13 +0000121 IntConstants.clear();
Paul Walkercbb24e12024-02-22 14:07:16 +0000122 IntSplatConstants.clear();
Justin Lebar611c5c22016-10-10 16:26:13 +0000123 FPConstants.clear();
Paul Walkercbb24e12024-02-22 14:07:16 +0000124 FPSplatConstants.clear();
Chris Lattner3756b912012-01-23 22:57:10 +0000125 CDSConstants.clear();
Bill Wendlinge38b8042012-09-26 21:07:29 +0000126
Bill Wendling164a4fb2013-01-24 00:14:46 +0000127 // Destroy attribute node lists.
128 for (FoldingSetIterator<AttributeSetNode> I = AttrsSetNodes.begin(),
129 E = AttrsSetNodes.end(); I != E; ) {
130 FoldingSetIterator<AttributeSetNode> Elem = I++;
131 delete &*Elem;
132 }
133
Duncan P. N. Exon Smith5bf8fef2014-12-09 18:38:53 +0000134 // Destroy MetadataAsValues.
135 {
136 SmallVector<MetadataAsValue *, 8> MDVs;
137 MDVs.reserve(MetadataAsValues.size());
138 for (auto &Pair : MetadataAsValues)
139 MDVs.push_back(Pair.second);
140 MetadataAsValues.clear();
141 for (auto *V : MDVs)
142 delete V;
143 }
144
145 // Destroy ValuesAsMetadata.
146 for (auto &Pair : ValuesAsMetadata)
147 delete Pair.second;
Jeffrey Yasskin4cfb3a72010-03-21 21:17:34 +0000148}
David Blaikiea379b1812011-12-20 02:50:00 +0000149
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000150namespace llvm {
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000151
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000152/// Make MDOperand transparent for hashing.
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000153///
154/// This overload of an implementation detail of the hashing library makes
155/// MDOperand hash to the same value as a \a Metadata pointer.
156///
157/// Note that overloading \a hash_value() as follows:
158///
159/// \code
160/// size_t hash_value(const MDOperand &X) { return hash_value(X.get()); }
161/// \endcode
162///
163/// does not cause MDOperand to be transparent. In particular, a bare pointer
164/// doesn't get hashed before it's combined, whereas \a MDOperand would.
165static const Metadata *get_hashable_data(const MDOperand &X) { return X.get(); }
Eugene Zelenkode6cce22017-06-19 22:05:08 +0000166
167} // end namespace llvm
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000168
Duncan P. N. Exon Smithfed199a2015-01-20 00:01:43 +0000169unsigned MDNodeOpsKey::calculateHash(MDNode *N, unsigned Offset) {
170 unsigned Hash = hash_combine_range(N->op_begin() + Offset, N->op_end());
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000171#ifndef NDEBUG
172 {
Kazu Hirata19aacdb2021-01-16 09:40:53 -0800173 SmallVector<Metadata *, 8> MDs(drop_begin(N->operands(), Offset));
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000174 unsigned RawHash = calculateHash(MDs);
175 assert(Hash == RawHash &&
176 "Expected hash of MDOperand to equal hash of Metadata*");
177 }
178#endif
179 return Hash;
180}
181
182unsigned MDNodeOpsKey::calculateHash(ArrayRef<Metadata *> Ops) {
Kazu Hiratab01e25d2025-04-20 16:36:03 -0700183 return hash_combine_range(Ops);
Duncan P. N. Exon Smith93e983e2015-01-19 22:53:18 +0000184}
185
Sanjoy Das9303c242015-09-24 19:14:18 +0000186StringMapEntry<uint32_t> *LLVMContextImpl::getOrInsertBundleTag(StringRef Tag) {
187 uint32_t NewIdx = BundleTagCache.size();
188 return &*(BundleTagCache.insert(std::make_pair(Tag, NewIdx)).first);
189}
190
191void LLVMContextImpl::getOperandBundleTags(SmallVectorImpl<StringRef> &Tags) const {
192 Tags.resize(BundleTagCache.size());
193 for (const auto &T : BundleTagCache)
194 Tags[T.second] = T.first();
195}
196
197uint32_t LLVMContextImpl::getOperandBundleTagID(StringRef Tag) const {
198 auto I = BundleTagCache.find(Tag);
199 assert(I != BundleTagCache.end() && "Unknown tag!");
200 return I->second;
201}
202
Konstantin Zhuravlyovbb80d3e2017-07-11 22:23:00 +0000203SyncScope::ID LLVMContextImpl::getOrInsertSyncScopeID(StringRef SSN) {
204 auto NewSSID = SSC.size();
205 assert(NewSSID < std::numeric_limits<SyncScope::ID>::max() &&
206 "Hit the maximum number of synchronization scopes allowed!");
207 return SSC.insert(std::make_pair(SSN, SyncScope::ID(NewSSID))).first->second;
208}
209
210void LLVMContextImpl::getSyncScopeNames(
211 SmallVectorImpl<StringRef> &SSNs) const {
212 SSNs.resize(SSC.size());
213 for (const auto &SSE : SSC)
214 SSNs[SSE.second] = SSE.first();
215}
216
gonzalobg0f521932024-09-25 20:13:56 +0200217std::optional<StringRef>
218LLVMContextImpl::getSyncScopeName(SyncScope::ID Id) const {
219 for (const auto &SSE : SSC) {
220 if (SSE.second != Id)
221 continue;
222 return SSE.first();
223 }
224 return std::nullopt;
225}
226
Samuel Eubanks47dbee62020-12-20 13:47:46 -0800227/// Gets the OptPassGate for this LLVMContextImpl, which defaults to the
228/// singleton OptBisect if not explicitly set.
Fedor Sergeevd29884c2018-04-05 10:29:37 +0000229OptPassGate &LLVMContextImpl::getOptPassGate() const {
230 if (!OPG)
Evgeniy Brevnov721f9752022-11-01 13:42:07 +0700231 OPG = &getGlobalPassGate();
Fedor Sergeevd29884c2018-04-05 10:29:37 +0000232 return *OPG;
233}
234
235void LLVMContextImpl::setOptPassGate(OptPassGate& OPG) {
236 this->OPG = &OPG;
Andrew Kayloraa641a52016-04-22 22:06:11 +0000237}