xref: /llvm-project/llvm/lib/SandboxIR/Constant.cpp (revision a9050525954cbe11d45b415e2248d9e25e004bfe)
1049fc920Svporpo //===- Constant.cpp - The Constant classes of Sandbox IR ------------------===//
2049fc920Svporpo //
3049fc920Svporpo // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4049fc920Svporpo // See https://llvm.org/LICENSE.txt for license information.
5049fc920Svporpo // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6049fc920Svporpo //
7049fc920Svporpo //===----------------------------------------------------------------------===//
8049fc920Svporpo 
9049fc920Svporpo #include "llvm/SandboxIR/Constant.h"
10491375ccSvporpo #include "llvm/SandboxIR/Argument.h"
11491375ccSvporpo #include "llvm/SandboxIR/BasicBlock.h"
12049fc920Svporpo #include "llvm/SandboxIR/Context.h"
13e22b07e7Svporpo #include "llvm/SandboxIR/Function.h"
14*a9050525SThomas Fransham #include "llvm/Support/Compiler.h"
15049fc920Svporpo 
16049fc920Svporpo namespace llvm::sandboxir {
17049fc920Svporpo 
18049fc920Svporpo #ifndef NDEBUG
19049fc920Svporpo void Constant::dumpOS(raw_ostream &OS) const {
20049fc920Svporpo   dumpCommonPrefix(OS);
21049fc920Svporpo   dumpCommonSuffix(OS);
22049fc920Svporpo }
23049fc920Svporpo #endif // NDEBUG
24049fc920Svporpo 
25049fc920Svporpo ConstantInt *ConstantInt::getTrue(Context &Ctx) {
26049fc920Svporpo   auto *LLVMC = llvm::ConstantInt::getTrue(Ctx.LLVMCtx);
27049fc920Svporpo   return cast<ConstantInt>(Ctx.getOrCreateConstant(LLVMC));
28049fc920Svporpo }
29049fc920Svporpo ConstantInt *ConstantInt::getFalse(Context &Ctx) {
30049fc920Svporpo   auto *LLVMC = llvm::ConstantInt::getFalse(Ctx.LLVMCtx);
31049fc920Svporpo   return cast<ConstantInt>(Ctx.getOrCreateConstant(LLVMC));
32049fc920Svporpo }
33049fc920Svporpo ConstantInt *ConstantInt::getBool(Context &Ctx, bool V) {
34049fc920Svporpo   auto *LLVMC = llvm::ConstantInt::getBool(Ctx.LLVMCtx, V);
35049fc920Svporpo   return cast<ConstantInt>(Ctx.getOrCreateConstant(LLVMC));
36049fc920Svporpo }
37049fc920Svporpo Constant *ConstantInt::getTrue(Type *Ty) {
38049fc920Svporpo   auto *LLVMC = llvm::ConstantInt::getTrue(Ty->LLVMTy);
39049fc920Svporpo   return Ty->getContext().getOrCreateConstant(LLVMC);
40049fc920Svporpo }
41049fc920Svporpo Constant *ConstantInt::getFalse(Type *Ty) {
42049fc920Svporpo   auto *LLVMC = llvm::ConstantInt::getFalse(Ty->LLVMTy);
43049fc920Svporpo   return Ty->getContext().getOrCreateConstant(LLVMC);
44049fc920Svporpo }
45049fc920Svporpo Constant *ConstantInt::getBool(Type *Ty, bool V) {
46049fc920Svporpo   auto *LLVMC = llvm::ConstantInt::getBool(Ty->LLVMTy, V);
47049fc920Svporpo   return Ty->getContext().getOrCreateConstant(LLVMC);
48049fc920Svporpo }
49049fc920Svporpo ConstantInt *ConstantInt::get(Type *Ty, uint64_t V, bool IsSigned) {
50049fc920Svporpo   auto *LLVMC = llvm::ConstantInt::get(Ty->LLVMTy, V, IsSigned);
51049fc920Svporpo   return cast<ConstantInt>(Ty->getContext().getOrCreateConstant(LLVMC));
52049fc920Svporpo }
53049fc920Svporpo ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool IsSigned) {
54049fc920Svporpo   auto *LLVMC = llvm::ConstantInt::get(Ty->LLVMTy, V, IsSigned);
55049fc920Svporpo   return cast<ConstantInt>(Ty->getContext().getOrCreateConstant(LLVMC));
56049fc920Svporpo }
57049fc920Svporpo ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) {
58049fc920Svporpo   auto *LLVMC =
59049fc920Svporpo       llvm::ConstantInt::getSigned(cast<llvm::IntegerType>(Ty->LLVMTy), V);
60049fc920Svporpo   return cast<ConstantInt>(Ty->getContext().getOrCreateConstant(LLVMC));
61049fc920Svporpo }
62049fc920Svporpo Constant *ConstantInt::getSigned(Type *Ty, int64_t V) {
63049fc920Svporpo   auto *LLVMC = llvm::ConstantInt::getSigned(Ty->LLVMTy, V);
64049fc920Svporpo   return Ty->getContext().getOrCreateConstant(LLVMC);
65049fc920Svporpo }
66049fc920Svporpo ConstantInt *ConstantInt::get(Context &Ctx, const APInt &V) {
67049fc920Svporpo   auto *LLVMC = llvm::ConstantInt::get(Ctx.LLVMCtx, V);
68049fc920Svporpo   return cast<ConstantInt>(Ctx.getOrCreateConstant(LLVMC));
69049fc920Svporpo }
70049fc920Svporpo ConstantInt *ConstantInt::get(IntegerType *Ty, StringRef Str, uint8_t Radix) {
71049fc920Svporpo   auto *LLVMC =
72049fc920Svporpo       llvm::ConstantInt::get(cast<llvm::IntegerType>(Ty->LLVMTy), Str, Radix);
73049fc920Svporpo   return cast<ConstantInt>(Ty->getContext().getOrCreateConstant(LLVMC));
74049fc920Svporpo }
75049fc920Svporpo Constant *ConstantInt::get(Type *Ty, const APInt &V) {
76049fc920Svporpo   auto *LLVMC = llvm::ConstantInt::get(Ty->LLVMTy, V);
77049fc920Svporpo   return Ty->getContext().getOrCreateConstant(LLVMC);
78049fc920Svporpo }
79049fc920Svporpo IntegerType *ConstantInt::getIntegerType() const {
80049fc920Svporpo   auto *LLVMTy = cast<llvm::ConstantInt>(Val)->getIntegerType();
81049fc920Svporpo   return cast<IntegerType>(Ctx.getType(LLVMTy));
82049fc920Svporpo }
83049fc920Svporpo 
84049fc920Svporpo bool ConstantInt::isValueValidForType(Type *Ty, uint64_t V) {
85049fc920Svporpo   return llvm::ConstantInt::isValueValidForType(Ty->LLVMTy, V);
86049fc920Svporpo }
87049fc920Svporpo bool ConstantInt::isValueValidForType(Type *Ty, int64_t V) {
88049fc920Svporpo   return llvm::ConstantInt::isValueValidForType(Ty->LLVMTy, V);
89049fc920Svporpo }
90049fc920Svporpo 
91049fc920Svporpo Constant *ConstantFP::get(Type *Ty, double V) {
92049fc920Svporpo   auto *LLVMC = llvm::ConstantFP::get(Ty->LLVMTy, V);
93049fc920Svporpo   return Ty->getContext().getOrCreateConstant(LLVMC);
94049fc920Svporpo }
95049fc920Svporpo 
96049fc920Svporpo Constant *ConstantFP::get(Type *Ty, const APFloat &V) {
97049fc920Svporpo   auto *LLVMC = llvm::ConstantFP::get(Ty->LLVMTy, V);
98049fc920Svporpo   return Ty->getContext().getOrCreateConstant(LLVMC);
99049fc920Svporpo }
100049fc920Svporpo 
101049fc920Svporpo Constant *ConstantFP::get(Type *Ty, StringRef Str) {
102049fc920Svporpo   auto *LLVMC = llvm::ConstantFP::get(Ty->LLVMTy, Str);
103049fc920Svporpo   return Ty->getContext().getOrCreateConstant(LLVMC);
104049fc920Svporpo }
105049fc920Svporpo 
106049fc920Svporpo ConstantFP *ConstantFP::get(const APFloat &V, Context &Ctx) {
107049fc920Svporpo   auto *LLVMC = llvm::ConstantFP::get(Ctx.LLVMCtx, V);
108049fc920Svporpo   return cast<ConstantFP>(Ctx.getOrCreateConstant(LLVMC));
109049fc920Svporpo }
110049fc920Svporpo 
111049fc920Svporpo Constant *ConstantFP::getNaN(Type *Ty, bool Negative, uint64_t Payload) {
112049fc920Svporpo   auto *LLVMC = llvm::ConstantFP::getNaN(Ty->LLVMTy, Negative, Payload);
113049fc920Svporpo   return cast<Constant>(Ty->getContext().getOrCreateConstant(LLVMC));
114049fc920Svporpo }
115049fc920Svporpo Constant *ConstantFP::getQNaN(Type *Ty, bool Negative, APInt *Payload) {
116049fc920Svporpo   auto *LLVMC = llvm::ConstantFP::getQNaN(Ty->LLVMTy, Negative, Payload);
117049fc920Svporpo   return cast<Constant>(Ty->getContext().getOrCreateConstant(LLVMC));
118049fc920Svporpo }
119049fc920Svporpo Constant *ConstantFP::getSNaN(Type *Ty, bool Negative, APInt *Payload) {
120049fc920Svporpo   auto *LLVMC = llvm::ConstantFP::getSNaN(Ty->LLVMTy, Negative, Payload);
121049fc920Svporpo   return cast<Constant>(Ty->getContext().getOrCreateConstant(LLVMC));
122049fc920Svporpo }
123049fc920Svporpo Constant *ConstantFP::getZero(Type *Ty, bool Negative) {
124049fc920Svporpo   auto *LLVMC = llvm::ConstantFP::getZero(Ty->LLVMTy, Negative);
125049fc920Svporpo   return cast<Constant>(Ty->getContext().getOrCreateConstant(LLVMC));
126049fc920Svporpo }
127049fc920Svporpo Constant *ConstantFP::getNegativeZero(Type *Ty) {
128049fc920Svporpo   auto *LLVMC = llvm::ConstantFP::getNegativeZero(Ty->LLVMTy);
129049fc920Svporpo   return cast<Constant>(Ty->getContext().getOrCreateConstant(LLVMC));
130049fc920Svporpo }
131049fc920Svporpo Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) {
132049fc920Svporpo   auto *LLVMC = llvm::ConstantFP::getInfinity(Ty->LLVMTy, Negative);
133049fc920Svporpo   return cast<Constant>(Ty->getContext().getOrCreateConstant(LLVMC));
134049fc920Svporpo }
135049fc920Svporpo bool ConstantFP::isValueValidForType(Type *Ty, const APFloat &V) {
136049fc920Svporpo   return llvm::ConstantFP::isValueValidForType(Ty->LLVMTy, V);
137049fc920Svporpo }
138049fc920Svporpo 
139049fc920Svporpo Constant *ConstantArray::get(ArrayType *T, ArrayRef<Constant *> V) {
140049fc920Svporpo   auto &Ctx = T->getContext();
141049fc920Svporpo   SmallVector<llvm::Constant *> LLVMValues;
142049fc920Svporpo   LLVMValues.reserve(V.size());
143049fc920Svporpo   for (auto *Elm : V)
144049fc920Svporpo     LLVMValues.push_back(cast<llvm::Constant>(Elm->Val));
145049fc920Svporpo   auto *LLVMC =
146049fc920Svporpo       llvm::ConstantArray::get(cast<llvm::ArrayType>(T->LLVMTy), LLVMValues);
147049fc920Svporpo   return cast<ConstantArray>(Ctx.getOrCreateConstant(LLVMC));
148049fc920Svporpo }
149049fc920Svporpo 
150049fc920Svporpo ArrayType *ConstantArray::getType() const {
151049fc920Svporpo   return cast<ArrayType>(
152049fc920Svporpo       Ctx.getType(cast<llvm::ConstantArray>(Val)->getType()));
153049fc920Svporpo }
154049fc920Svporpo 
155049fc920Svporpo Constant *ConstantStruct::get(StructType *T, ArrayRef<Constant *> V) {
156049fc920Svporpo   auto &Ctx = T->getContext();
157049fc920Svporpo   SmallVector<llvm::Constant *> LLVMValues;
158049fc920Svporpo   LLVMValues.reserve(V.size());
159049fc920Svporpo   for (auto *Elm : V)
160049fc920Svporpo     LLVMValues.push_back(cast<llvm::Constant>(Elm->Val));
161049fc920Svporpo   auto *LLVMC =
162049fc920Svporpo       llvm::ConstantStruct::get(cast<llvm::StructType>(T->LLVMTy), LLVMValues);
163049fc920Svporpo   return cast<ConstantStruct>(Ctx.getOrCreateConstant(LLVMC));
164049fc920Svporpo }
165049fc920Svporpo 
166049fc920Svporpo StructType *ConstantStruct::getTypeForElements(Context &Ctx,
167049fc920Svporpo                                                ArrayRef<Constant *> V,
168049fc920Svporpo                                                bool Packed) {
169049fc920Svporpo   unsigned VecSize = V.size();
170049fc920Svporpo   SmallVector<Type *, 16> EltTypes;
171049fc920Svporpo   EltTypes.reserve(VecSize);
172049fc920Svporpo   for (Constant *Elm : V)
173049fc920Svporpo     EltTypes.push_back(Elm->getType());
174049fc920Svporpo   return StructType::get(Ctx, EltTypes, Packed);
175049fc920Svporpo }
176049fc920Svporpo 
177049fc920Svporpo ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) {
178049fc920Svporpo   auto *LLVMC = llvm::ConstantAggregateZero::get(Ty->LLVMTy);
179049fc920Svporpo   return cast<ConstantAggregateZero>(
180049fc920Svporpo       Ty->getContext().getOrCreateConstant(LLVMC));
181049fc920Svporpo }
182049fc920Svporpo 
183049fc920Svporpo Constant *ConstantAggregateZero::getSequentialElement() const {
184049fc920Svporpo   return cast<Constant>(Ctx.getValue(
185049fc920Svporpo       cast<llvm::ConstantAggregateZero>(Val)->getSequentialElement()));
186049fc920Svporpo }
187049fc920Svporpo Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const {
188049fc920Svporpo   return cast<Constant>(Ctx.getValue(
189049fc920Svporpo       cast<llvm::ConstantAggregateZero>(Val)->getStructElement(Elt)));
190049fc920Svporpo }
191049fc920Svporpo Constant *ConstantAggregateZero::getElementValue(Constant *C) const {
192049fc920Svporpo   return cast<Constant>(
193049fc920Svporpo       Ctx.getValue(cast<llvm::ConstantAggregateZero>(Val)->getElementValue(
194049fc920Svporpo           cast<llvm::Constant>(C->Val))));
195049fc920Svporpo }
196049fc920Svporpo Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
197049fc920Svporpo   return cast<Constant>(Ctx.getValue(
198049fc920Svporpo       cast<llvm::ConstantAggregateZero>(Val)->getElementValue(Idx)));
199049fc920Svporpo }
200049fc920Svporpo 
201049fc920Svporpo ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
202049fc920Svporpo   auto *LLVMC =
203049fc920Svporpo       llvm::ConstantPointerNull::get(cast<llvm::PointerType>(Ty->LLVMTy));
204049fc920Svporpo   return cast<ConstantPointerNull>(Ty->getContext().getOrCreateConstant(LLVMC));
205049fc920Svporpo }
206049fc920Svporpo 
207049fc920Svporpo PointerType *ConstantPointerNull::getType() const {
208049fc920Svporpo   return cast<PointerType>(
209049fc920Svporpo       Ctx.getType(cast<llvm::ConstantPointerNull>(Val)->getType()));
210049fc920Svporpo }
211049fc920Svporpo 
212049fc920Svporpo UndefValue *UndefValue::get(Type *T) {
213049fc920Svporpo   auto *LLVMC = llvm::UndefValue::get(T->LLVMTy);
214049fc920Svporpo   return cast<UndefValue>(T->getContext().getOrCreateConstant(LLVMC));
215049fc920Svporpo }
216049fc920Svporpo 
217049fc920Svporpo UndefValue *UndefValue::getSequentialElement() const {
218049fc920Svporpo   return cast<UndefValue>(Ctx.getOrCreateConstant(
219049fc920Svporpo       cast<llvm::UndefValue>(Val)->getSequentialElement()));
220049fc920Svporpo }
221049fc920Svporpo 
222049fc920Svporpo UndefValue *UndefValue::getStructElement(unsigned Elt) const {
223049fc920Svporpo   return cast<UndefValue>(Ctx.getOrCreateConstant(
224049fc920Svporpo       cast<llvm::UndefValue>(Val)->getStructElement(Elt)));
225049fc920Svporpo }
226049fc920Svporpo 
227049fc920Svporpo UndefValue *UndefValue::getElementValue(Constant *C) const {
228049fc920Svporpo   return cast<UndefValue>(
229049fc920Svporpo       Ctx.getOrCreateConstant(cast<llvm::UndefValue>(Val)->getElementValue(
230049fc920Svporpo           cast<llvm::Constant>(C->Val))));
231049fc920Svporpo }
232049fc920Svporpo 
233049fc920Svporpo UndefValue *UndefValue::getElementValue(unsigned Idx) const {
234049fc920Svporpo   return cast<UndefValue>(Ctx.getOrCreateConstant(
235049fc920Svporpo       cast<llvm::UndefValue>(Val)->getElementValue(Idx)));
236049fc920Svporpo }
237049fc920Svporpo 
238049fc920Svporpo PoisonValue *PoisonValue::get(Type *T) {
239049fc920Svporpo   auto *LLVMC = llvm::PoisonValue::get(T->LLVMTy);
240049fc920Svporpo   return cast<PoisonValue>(T->getContext().getOrCreateConstant(LLVMC));
241049fc920Svporpo }
242049fc920Svporpo 
243049fc920Svporpo PoisonValue *PoisonValue::getSequentialElement() const {
244049fc920Svporpo   return cast<PoisonValue>(Ctx.getOrCreateConstant(
245049fc920Svporpo       cast<llvm::PoisonValue>(Val)->getSequentialElement()));
246049fc920Svporpo }
247049fc920Svporpo 
248049fc920Svporpo PoisonValue *PoisonValue::getStructElement(unsigned Elt) const {
249049fc920Svporpo   return cast<PoisonValue>(Ctx.getOrCreateConstant(
250049fc920Svporpo       cast<llvm::PoisonValue>(Val)->getStructElement(Elt)));
251049fc920Svporpo }
252049fc920Svporpo 
253049fc920Svporpo PoisonValue *PoisonValue::getElementValue(Constant *C) const {
254049fc920Svporpo   return cast<PoisonValue>(
255049fc920Svporpo       Ctx.getOrCreateConstant(cast<llvm::PoisonValue>(Val)->getElementValue(
256049fc920Svporpo           cast<llvm::Constant>(C->Val))));
257049fc920Svporpo }
258049fc920Svporpo 
259049fc920Svporpo PoisonValue *PoisonValue::getElementValue(unsigned Idx) const {
260049fc920Svporpo   return cast<PoisonValue>(Ctx.getOrCreateConstant(
261049fc920Svporpo       cast<llvm::PoisonValue>(Val)->getElementValue(Idx)));
262049fc920Svporpo }
263049fc920Svporpo 
264049fc920Svporpo void GlobalObject::setAlignment(MaybeAlign Align) {
265049fc920Svporpo   Ctx.getTracker()
266049fc920Svporpo       .emplaceIfTracking<
267049fc920Svporpo           GenericSetter<&GlobalObject::getAlign, &GlobalObject::setAlignment>>(
268049fc920Svporpo           this);
269049fc920Svporpo   cast<llvm::GlobalObject>(Val)->setAlignment(Align);
270049fc920Svporpo }
271049fc920Svporpo 
272049fc920Svporpo void GlobalObject::setGlobalObjectSubClassData(unsigned V) {
273049fc920Svporpo   Ctx.getTracker()
274049fc920Svporpo       .emplaceIfTracking<
275049fc920Svporpo           GenericSetter<&GlobalObject::getGlobalObjectSubClassData,
276049fc920Svporpo                         &GlobalObject::setGlobalObjectSubClassData>>(this);
277049fc920Svporpo   cast<llvm::GlobalObject>(Val)->setGlobalObjectSubClassData(V);
278049fc920Svporpo }
279049fc920Svporpo 
280049fc920Svporpo void GlobalObject::setSection(StringRef S) {
281049fc920Svporpo   Ctx.getTracker()
282049fc920Svporpo       .emplaceIfTracking<
283049fc920Svporpo           GenericSetter<&GlobalObject::getSection, &GlobalObject::setSection>>(
284049fc920Svporpo           this);
285049fc920Svporpo   cast<llvm::GlobalObject>(Val)->setSection(S);
286049fc920Svporpo }
287049fc920Svporpo 
288049fc920Svporpo template <typename GlobalT, typename LLVMGlobalT, typename ParentT,
289049fc920Svporpo           typename LLVMParentT>
290049fc920Svporpo GlobalT &GlobalWithNodeAPI<GlobalT, LLVMGlobalT, ParentT, LLVMParentT>::
291049fc920Svporpo     LLVMGVToGV::operator()(LLVMGlobalT &LLVMGV) const {
292049fc920Svporpo   return cast<GlobalT>(*Ctx.getValue(&LLVMGV));
293049fc920Svporpo }
294049fc920Svporpo 
295049fc920Svporpo // Explicit instantiations.
296049fc920Svporpo template class GlobalWithNodeAPI<GlobalIFunc, llvm::GlobalIFunc, GlobalObject,
297049fc920Svporpo                                  llvm::GlobalObject>;
298049fc920Svporpo template class GlobalWithNodeAPI<Function, llvm::Function, GlobalObject,
299049fc920Svporpo                                  llvm::GlobalObject>;
300049fc920Svporpo template class GlobalWithNodeAPI<GlobalVariable, llvm::GlobalVariable,
301049fc920Svporpo                                  GlobalObject, llvm::GlobalObject>;
302049fc920Svporpo template class GlobalWithNodeAPI<GlobalAlias, llvm::GlobalAlias, GlobalValue,
303049fc920Svporpo                                  llvm::GlobalValue>;
304049fc920Svporpo 
305*a9050525SThomas Fransham #ifdef _MSC_VER
306*a9050525SThomas Fransham // These are needed for SandboxIRTest when building with LLVM_BUILD_LLVM_DYLIB
307*a9050525SThomas Fransham template LLVM_EXPORT_TEMPLATE GlobalIFunc &
308*a9050525SThomas Fransham GlobalWithNodeAPI<GlobalIFunc, llvm::GlobalIFunc, GlobalObject,
309*a9050525SThomas Fransham                   llvm::GlobalObject>::LLVMGVToGV::operator()(llvm::GlobalIFunc
310*a9050525SThomas Fransham                                                                   &LLVMGV)
311*a9050525SThomas Fransham     const;
312*a9050525SThomas Fransham template LLVM_EXPORT_TEMPLATE Function &
313*a9050525SThomas Fransham GlobalWithNodeAPI<Function, llvm::Function, GlobalObject, llvm::GlobalObject>::
314*a9050525SThomas Fransham     LLVMGVToGV::operator()(llvm::Function &LLVMGV) const;
315*a9050525SThomas Fransham 
316*a9050525SThomas Fransham template LLVM_EXPORT_TEMPLATE GlobalVariable &GlobalWithNodeAPI<
317*a9050525SThomas Fransham     GlobalVariable, llvm::GlobalVariable, GlobalObject,
318*a9050525SThomas Fransham     llvm::GlobalObject>::LLVMGVToGV::operator()(llvm::GlobalVariable &LLVMGV)
319*a9050525SThomas Fransham     const;
320*a9050525SThomas Fransham template LLVM_EXPORT_TEMPLATE GlobalAlias &
321*a9050525SThomas Fransham GlobalWithNodeAPI<GlobalAlias, llvm::GlobalAlias, GlobalValue,
322*a9050525SThomas Fransham                   llvm::GlobalValue>::LLVMGVToGV::operator()(llvm::GlobalAlias
323*a9050525SThomas Fransham                                                                  &LLVMGV) const;
324*a9050525SThomas Fransham #endif
325*a9050525SThomas Fransham 
326049fc920Svporpo void GlobalIFunc::setResolver(Constant *Resolver) {
327049fc920Svporpo   Ctx.getTracker()
328049fc920Svporpo       .emplaceIfTracking<
329049fc920Svporpo           GenericSetter<&GlobalIFunc::getResolver, &GlobalIFunc::setResolver>>(
330049fc920Svporpo           this);
331049fc920Svporpo   cast<llvm::GlobalIFunc>(Val)->setResolver(
332049fc920Svporpo       cast<llvm::Constant>(Resolver->Val));
333049fc920Svporpo }
334049fc920Svporpo 
335049fc920Svporpo Constant *GlobalIFunc::getResolver() const {
336049fc920Svporpo   return Ctx.getOrCreateConstant(cast<llvm::GlobalIFunc>(Val)->getResolver());
337049fc920Svporpo }
338049fc920Svporpo 
339049fc920Svporpo Function *GlobalIFunc::getResolverFunction() {
340049fc920Svporpo   return cast<Function>(Ctx.getOrCreateConstant(
341049fc920Svporpo       cast<llvm::GlobalIFunc>(Val)->getResolverFunction()));
342049fc920Svporpo }
343049fc920Svporpo 
344049fc920Svporpo GlobalVariable &
345049fc920Svporpo GlobalVariable::LLVMGVToGV::operator()(llvm::GlobalVariable &LLVMGV) const {
346049fc920Svporpo   return cast<GlobalVariable>(*Ctx.getValue(&LLVMGV));
347049fc920Svporpo }
348049fc920Svporpo 
349049fc920Svporpo Constant *GlobalVariable::getInitializer() const {
350049fc920Svporpo   return Ctx.getOrCreateConstant(
351049fc920Svporpo       cast<llvm::GlobalVariable>(Val)->getInitializer());
352049fc920Svporpo }
353049fc920Svporpo 
354049fc920Svporpo void GlobalVariable::setInitializer(Constant *InitVal) {
355049fc920Svporpo   Ctx.getTracker()
356049fc920Svporpo       .emplaceIfTracking<GenericSetter<&GlobalVariable::getInitializer,
357049fc920Svporpo                                        &GlobalVariable::setInitializer>>(this);
358049fc920Svporpo   cast<llvm::GlobalVariable>(Val)->setInitializer(
359049fc920Svporpo       cast<llvm::Constant>(InitVal->Val));
360049fc920Svporpo }
361049fc920Svporpo 
362049fc920Svporpo void GlobalVariable::setConstant(bool V) {
363049fc920Svporpo   Ctx.getTracker()
364049fc920Svporpo       .emplaceIfTracking<GenericSetter<&GlobalVariable::isConstant,
365049fc920Svporpo                                        &GlobalVariable::setConstant>>(this);
366049fc920Svporpo   cast<llvm::GlobalVariable>(Val)->setConstant(V);
367049fc920Svporpo }
368049fc920Svporpo 
369049fc920Svporpo void GlobalVariable::setExternallyInitialized(bool V) {
370049fc920Svporpo   Ctx.getTracker()
371049fc920Svporpo       .emplaceIfTracking<
372049fc920Svporpo           GenericSetter<&GlobalVariable::isExternallyInitialized,
373049fc920Svporpo                         &GlobalVariable::setExternallyInitialized>>(this);
374049fc920Svporpo   cast<llvm::GlobalVariable>(Val)->setExternallyInitialized(V);
375049fc920Svporpo }
376049fc920Svporpo 
377049fc920Svporpo void GlobalAlias::setAliasee(Constant *Aliasee) {
378049fc920Svporpo   Ctx.getTracker()
379049fc920Svporpo       .emplaceIfTracking<
380049fc920Svporpo           GenericSetter<&GlobalAlias::getAliasee, &GlobalAlias::setAliasee>>(
381049fc920Svporpo           this);
382049fc920Svporpo   cast<llvm::GlobalAlias>(Val)->setAliasee(cast<llvm::Constant>(Aliasee->Val));
383049fc920Svporpo }
384049fc920Svporpo 
385049fc920Svporpo Constant *GlobalAlias::getAliasee() const {
386049fc920Svporpo   return cast<Constant>(
387049fc920Svporpo       Ctx.getOrCreateConstant(cast<llvm::GlobalAlias>(Val)->getAliasee()));
388049fc920Svporpo }
389049fc920Svporpo 
390049fc920Svporpo const GlobalObject *GlobalAlias::getAliaseeObject() const {
391049fc920Svporpo   return cast<GlobalObject>(Ctx.getOrCreateConstant(
392049fc920Svporpo       cast<llvm::GlobalAlias>(Val)->getAliaseeObject()));
393049fc920Svporpo }
394049fc920Svporpo 
395049fc920Svporpo void GlobalValue::setUnnamedAddr(UnnamedAddr V) {
396049fc920Svporpo   Ctx.getTracker()
397049fc920Svporpo       .emplaceIfTracking<GenericSetter<&GlobalValue::getUnnamedAddr,
398049fc920Svporpo                                        &GlobalValue::setUnnamedAddr>>(this);
399049fc920Svporpo   cast<llvm::GlobalValue>(Val)->setUnnamedAddr(V);
400049fc920Svporpo }
401049fc920Svporpo 
402049fc920Svporpo void GlobalValue::setVisibility(VisibilityTypes V) {
403049fc920Svporpo   Ctx.getTracker()
404049fc920Svporpo       .emplaceIfTracking<GenericSetter<&GlobalValue::getVisibility,
405049fc920Svporpo                                        &GlobalValue::setVisibility>>(this);
406049fc920Svporpo   cast<llvm::GlobalValue>(Val)->setVisibility(V);
407049fc920Svporpo }
408049fc920Svporpo 
409049fc920Svporpo NoCFIValue *NoCFIValue::get(GlobalValue *GV) {
410049fc920Svporpo   auto *LLVMC = llvm::NoCFIValue::get(cast<llvm::GlobalValue>(GV->Val));
411049fc920Svporpo   return cast<NoCFIValue>(GV->getContext().getOrCreateConstant(LLVMC));
412049fc920Svporpo }
413049fc920Svporpo 
414049fc920Svporpo GlobalValue *NoCFIValue::getGlobalValue() const {
415049fc920Svporpo   auto *LLVMC = cast<llvm::NoCFIValue>(Val)->getGlobalValue();
416049fc920Svporpo   return cast<GlobalValue>(Ctx.getOrCreateConstant(LLVMC));
417049fc920Svporpo }
418049fc920Svporpo 
419049fc920Svporpo PointerType *NoCFIValue::getType() const {
420049fc920Svporpo   return cast<PointerType>(Ctx.getType(cast<llvm::NoCFIValue>(Val)->getType()));
421049fc920Svporpo }
422049fc920Svporpo 
423049fc920Svporpo ConstantPtrAuth *ConstantPtrAuth::get(Constant *Ptr, ConstantInt *Key,
424049fc920Svporpo                                       ConstantInt *Disc, Constant *AddrDisc) {
425049fc920Svporpo   auto *LLVMC = llvm::ConstantPtrAuth::get(
426049fc920Svporpo       cast<llvm::Constant>(Ptr->Val), cast<llvm::ConstantInt>(Key->Val),
427049fc920Svporpo       cast<llvm::ConstantInt>(Disc->Val), cast<llvm::Constant>(AddrDisc->Val));
428049fc920Svporpo   return cast<ConstantPtrAuth>(Ptr->getContext().getOrCreateConstant(LLVMC));
429049fc920Svporpo }
430049fc920Svporpo 
431049fc920Svporpo Constant *ConstantPtrAuth::getPointer() const {
432049fc920Svporpo   return Ctx.getOrCreateConstant(
433049fc920Svporpo       cast<llvm::ConstantPtrAuth>(Val)->getPointer());
434049fc920Svporpo }
435049fc920Svporpo 
436049fc920Svporpo ConstantInt *ConstantPtrAuth::getKey() const {
437049fc920Svporpo   return cast<ConstantInt>(
438049fc920Svporpo       Ctx.getOrCreateConstant(cast<llvm::ConstantPtrAuth>(Val)->getKey()));
439049fc920Svporpo }
440049fc920Svporpo 
441049fc920Svporpo ConstantInt *ConstantPtrAuth::getDiscriminator() const {
442049fc920Svporpo   return cast<ConstantInt>(Ctx.getOrCreateConstant(
443049fc920Svporpo       cast<llvm::ConstantPtrAuth>(Val)->getDiscriminator()));
444049fc920Svporpo }
445049fc920Svporpo 
446049fc920Svporpo Constant *ConstantPtrAuth::getAddrDiscriminator() const {
447049fc920Svporpo   return Ctx.getOrCreateConstant(
448049fc920Svporpo       cast<llvm::ConstantPtrAuth>(Val)->getAddrDiscriminator());
449049fc920Svporpo }
450049fc920Svporpo 
451049fc920Svporpo ConstantPtrAuth *ConstantPtrAuth::getWithSameSchema(Constant *Pointer) const {
452049fc920Svporpo   auto *LLVMC = cast<llvm::ConstantPtrAuth>(Val)->getWithSameSchema(
453049fc920Svporpo       cast<llvm::Constant>(Pointer->Val));
454049fc920Svporpo   return cast<ConstantPtrAuth>(Ctx.getOrCreateConstant(LLVMC));
455049fc920Svporpo }
456049fc920Svporpo 
457049fc920Svporpo BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) {
458049fc920Svporpo   auto *LLVMC = llvm::BlockAddress::get(cast<llvm::Function>(F->Val),
459049fc920Svporpo                                         cast<llvm::BasicBlock>(BB->Val));
460049fc920Svporpo   return cast<BlockAddress>(F->getContext().getOrCreateConstant(LLVMC));
461049fc920Svporpo }
462049fc920Svporpo 
463049fc920Svporpo BlockAddress *BlockAddress::get(BasicBlock *BB) {
464049fc920Svporpo   auto *LLVMC = llvm::BlockAddress::get(cast<llvm::BasicBlock>(BB->Val));
465049fc920Svporpo   return cast<BlockAddress>(BB->getContext().getOrCreateConstant(LLVMC));
466049fc920Svporpo }
467049fc920Svporpo 
468049fc920Svporpo BlockAddress *BlockAddress::lookup(const BasicBlock *BB) {
469049fc920Svporpo   auto *LLVMC = llvm::BlockAddress::lookup(cast<llvm::BasicBlock>(BB->Val));
470049fc920Svporpo   return cast_or_null<BlockAddress>(BB->getContext().getValue(LLVMC));
471049fc920Svporpo }
472049fc920Svporpo 
473049fc920Svporpo Function *BlockAddress::getFunction() const {
474049fc920Svporpo   return cast<Function>(
475049fc920Svporpo       Ctx.getValue(cast<llvm::BlockAddress>(Val)->getFunction()));
476049fc920Svporpo }
477049fc920Svporpo 
478049fc920Svporpo BasicBlock *BlockAddress::getBasicBlock() const {
479049fc920Svporpo   return cast<BasicBlock>(
480049fc920Svporpo       Ctx.getValue(cast<llvm::BlockAddress>(Val)->getBasicBlock()));
481049fc920Svporpo }
482049fc920Svporpo 
483049fc920Svporpo DSOLocalEquivalent *DSOLocalEquivalent::get(GlobalValue *GV) {
484049fc920Svporpo   auto *LLVMC = llvm::DSOLocalEquivalent::get(cast<llvm::GlobalValue>(GV->Val));
485049fc920Svporpo   return cast<DSOLocalEquivalent>(GV->getContext().getValue(LLVMC));
486049fc920Svporpo }
487049fc920Svporpo 
488049fc920Svporpo GlobalValue *DSOLocalEquivalent::getGlobalValue() const {
489049fc920Svporpo   return cast<GlobalValue>(
490049fc920Svporpo       Ctx.getValue(cast<llvm::DSOLocalEquivalent>(Val)->getGlobalValue()));
491049fc920Svporpo }
492049fc920Svporpo 
493049fc920Svporpo } // namespace llvm::sandboxir
494