1 //===- Constant.cpp - The Constant classes of Sandbox IR ------------------===// 2 // 3 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/SandboxIR/Constant.h" 10 #include "llvm/SandboxIR/Argument.h" 11 #include "llvm/SandboxIR/BasicBlock.h" 12 #include "llvm/SandboxIR/Context.h" 13 #include "llvm/SandboxIR/Function.h" 14 #include "llvm/Support/Compiler.h" 15 16 namespace llvm::sandboxir { 17 18 #ifndef NDEBUG 19 void Constant::dumpOS(raw_ostream &OS) const { 20 dumpCommonPrefix(OS); 21 dumpCommonSuffix(OS); 22 } 23 #endif // NDEBUG 24 25 ConstantInt *ConstantInt::getTrue(Context &Ctx) { 26 auto *LLVMC = llvm::ConstantInt::getTrue(Ctx.LLVMCtx); 27 return cast<ConstantInt>(Ctx.getOrCreateConstant(LLVMC)); 28 } 29 ConstantInt *ConstantInt::getFalse(Context &Ctx) { 30 auto *LLVMC = llvm::ConstantInt::getFalse(Ctx.LLVMCtx); 31 return cast<ConstantInt>(Ctx.getOrCreateConstant(LLVMC)); 32 } 33 ConstantInt *ConstantInt::getBool(Context &Ctx, bool V) { 34 auto *LLVMC = llvm::ConstantInt::getBool(Ctx.LLVMCtx, V); 35 return cast<ConstantInt>(Ctx.getOrCreateConstant(LLVMC)); 36 } 37 Constant *ConstantInt::getTrue(Type *Ty) { 38 auto *LLVMC = llvm::ConstantInt::getTrue(Ty->LLVMTy); 39 return Ty->getContext().getOrCreateConstant(LLVMC); 40 } 41 Constant *ConstantInt::getFalse(Type *Ty) { 42 auto *LLVMC = llvm::ConstantInt::getFalse(Ty->LLVMTy); 43 return Ty->getContext().getOrCreateConstant(LLVMC); 44 } 45 Constant *ConstantInt::getBool(Type *Ty, bool V) { 46 auto *LLVMC = llvm::ConstantInt::getBool(Ty->LLVMTy, V); 47 return Ty->getContext().getOrCreateConstant(LLVMC); 48 } 49 ConstantInt *ConstantInt::get(Type *Ty, uint64_t V, bool IsSigned) { 50 auto *LLVMC = llvm::ConstantInt::get(Ty->LLVMTy, V, IsSigned); 51 return cast<ConstantInt>(Ty->getContext().getOrCreateConstant(LLVMC)); 52 } 53 ConstantInt *ConstantInt::get(IntegerType *Ty, uint64_t V, bool IsSigned) { 54 auto *LLVMC = llvm::ConstantInt::get(Ty->LLVMTy, V, IsSigned); 55 return cast<ConstantInt>(Ty->getContext().getOrCreateConstant(LLVMC)); 56 } 57 ConstantInt *ConstantInt::getSigned(IntegerType *Ty, int64_t V) { 58 auto *LLVMC = 59 llvm::ConstantInt::getSigned(cast<llvm::IntegerType>(Ty->LLVMTy), V); 60 return cast<ConstantInt>(Ty->getContext().getOrCreateConstant(LLVMC)); 61 } 62 Constant *ConstantInt::getSigned(Type *Ty, int64_t V) { 63 auto *LLVMC = llvm::ConstantInt::getSigned(Ty->LLVMTy, V); 64 return Ty->getContext().getOrCreateConstant(LLVMC); 65 } 66 ConstantInt *ConstantInt::get(Context &Ctx, const APInt &V) { 67 auto *LLVMC = llvm::ConstantInt::get(Ctx.LLVMCtx, V); 68 return cast<ConstantInt>(Ctx.getOrCreateConstant(LLVMC)); 69 } 70 ConstantInt *ConstantInt::get(IntegerType *Ty, StringRef Str, uint8_t Radix) { 71 auto *LLVMC = 72 llvm::ConstantInt::get(cast<llvm::IntegerType>(Ty->LLVMTy), Str, Radix); 73 return cast<ConstantInt>(Ty->getContext().getOrCreateConstant(LLVMC)); 74 } 75 Constant *ConstantInt::get(Type *Ty, const APInt &V) { 76 auto *LLVMC = llvm::ConstantInt::get(Ty->LLVMTy, V); 77 return Ty->getContext().getOrCreateConstant(LLVMC); 78 } 79 IntegerType *ConstantInt::getIntegerType() const { 80 auto *LLVMTy = cast<llvm::ConstantInt>(Val)->getIntegerType(); 81 return cast<IntegerType>(Ctx.getType(LLVMTy)); 82 } 83 84 bool ConstantInt::isValueValidForType(Type *Ty, uint64_t V) { 85 return llvm::ConstantInt::isValueValidForType(Ty->LLVMTy, V); 86 } 87 bool ConstantInt::isValueValidForType(Type *Ty, int64_t V) { 88 return llvm::ConstantInt::isValueValidForType(Ty->LLVMTy, V); 89 } 90 91 Constant *ConstantFP::get(Type *Ty, double V) { 92 auto *LLVMC = llvm::ConstantFP::get(Ty->LLVMTy, V); 93 return Ty->getContext().getOrCreateConstant(LLVMC); 94 } 95 96 Constant *ConstantFP::get(Type *Ty, const APFloat &V) { 97 auto *LLVMC = llvm::ConstantFP::get(Ty->LLVMTy, V); 98 return Ty->getContext().getOrCreateConstant(LLVMC); 99 } 100 101 Constant *ConstantFP::get(Type *Ty, StringRef Str) { 102 auto *LLVMC = llvm::ConstantFP::get(Ty->LLVMTy, Str); 103 return Ty->getContext().getOrCreateConstant(LLVMC); 104 } 105 106 ConstantFP *ConstantFP::get(const APFloat &V, Context &Ctx) { 107 auto *LLVMC = llvm::ConstantFP::get(Ctx.LLVMCtx, V); 108 return cast<ConstantFP>(Ctx.getOrCreateConstant(LLVMC)); 109 } 110 111 Constant *ConstantFP::getNaN(Type *Ty, bool Negative, uint64_t Payload) { 112 auto *LLVMC = llvm::ConstantFP::getNaN(Ty->LLVMTy, Negative, Payload); 113 return cast<Constant>(Ty->getContext().getOrCreateConstant(LLVMC)); 114 } 115 Constant *ConstantFP::getQNaN(Type *Ty, bool Negative, APInt *Payload) { 116 auto *LLVMC = llvm::ConstantFP::getQNaN(Ty->LLVMTy, Negative, Payload); 117 return cast<Constant>(Ty->getContext().getOrCreateConstant(LLVMC)); 118 } 119 Constant *ConstantFP::getSNaN(Type *Ty, bool Negative, APInt *Payload) { 120 auto *LLVMC = llvm::ConstantFP::getSNaN(Ty->LLVMTy, Negative, Payload); 121 return cast<Constant>(Ty->getContext().getOrCreateConstant(LLVMC)); 122 } 123 Constant *ConstantFP::getZero(Type *Ty, bool Negative) { 124 auto *LLVMC = llvm::ConstantFP::getZero(Ty->LLVMTy, Negative); 125 return cast<Constant>(Ty->getContext().getOrCreateConstant(LLVMC)); 126 } 127 Constant *ConstantFP::getNegativeZero(Type *Ty) { 128 auto *LLVMC = llvm::ConstantFP::getNegativeZero(Ty->LLVMTy); 129 return cast<Constant>(Ty->getContext().getOrCreateConstant(LLVMC)); 130 } 131 Constant *ConstantFP::getInfinity(Type *Ty, bool Negative) { 132 auto *LLVMC = llvm::ConstantFP::getInfinity(Ty->LLVMTy, Negative); 133 return cast<Constant>(Ty->getContext().getOrCreateConstant(LLVMC)); 134 } 135 bool ConstantFP::isValueValidForType(Type *Ty, const APFloat &V) { 136 return llvm::ConstantFP::isValueValidForType(Ty->LLVMTy, V); 137 } 138 139 Constant *ConstantArray::get(ArrayType *T, ArrayRef<Constant *> V) { 140 auto &Ctx = T->getContext(); 141 SmallVector<llvm::Constant *> LLVMValues; 142 LLVMValues.reserve(V.size()); 143 for (auto *Elm : V) 144 LLVMValues.push_back(cast<llvm::Constant>(Elm->Val)); 145 auto *LLVMC = 146 llvm::ConstantArray::get(cast<llvm::ArrayType>(T->LLVMTy), LLVMValues); 147 return cast<ConstantArray>(Ctx.getOrCreateConstant(LLVMC)); 148 } 149 150 ArrayType *ConstantArray::getType() const { 151 return cast<ArrayType>( 152 Ctx.getType(cast<llvm::ConstantArray>(Val)->getType())); 153 } 154 155 Constant *ConstantStruct::get(StructType *T, ArrayRef<Constant *> V) { 156 auto &Ctx = T->getContext(); 157 SmallVector<llvm::Constant *> LLVMValues; 158 LLVMValues.reserve(V.size()); 159 for (auto *Elm : V) 160 LLVMValues.push_back(cast<llvm::Constant>(Elm->Val)); 161 auto *LLVMC = 162 llvm::ConstantStruct::get(cast<llvm::StructType>(T->LLVMTy), LLVMValues); 163 return cast<ConstantStruct>(Ctx.getOrCreateConstant(LLVMC)); 164 } 165 166 StructType *ConstantStruct::getTypeForElements(Context &Ctx, 167 ArrayRef<Constant *> V, 168 bool Packed) { 169 unsigned VecSize = V.size(); 170 SmallVector<Type *, 16> EltTypes; 171 EltTypes.reserve(VecSize); 172 for (Constant *Elm : V) 173 EltTypes.push_back(Elm->getType()); 174 return StructType::get(Ctx, EltTypes, Packed); 175 } 176 177 ConstantAggregateZero *ConstantAggregateZero::get(Type *Ty) { 178 auto *LLVMC = llvm::ConstantAggregateZero::get(Ty->LLVMTy); 179 return cast<ConstantAggregateZero>( 180 Ty->getContext().getOrCreateConstant(LLVMC)); 181 } 182 183 Constant *ConstantAggregateZero::getSequentialElement() const { 184 return cast<Constant>(Ctx.getValue( 185 cast<llvm::ConstantAggregateZero>(Val)->getSequentialElement())); 186 } 187 Constant *ConstantAggregateZero::getStructElement(unsigned Elt) const { 188 return cast<Constant>(Ctx.getValue( 189 cast<llvm::ConstantAggregateZero>(Val)->getStructElement(Elt))); 190 } 191 Constant *ConstantAggregateZero::getElementValue(Constant *C) const { 192 return cast<Constant>( 193 Ctx.getValue(cast<llvm::ConstantAggregateZero>(Val)->getElementValue( 194 cast<llvm::Constant>(C->Val)))); 195 } 196 Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const { 197 return cast<Constant>(Ctx.getValue( 198 cast<llvm::ConstantAggregateZero>(Val)->getElementValue(Idx))); 199 } 200 201 ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) { 202 auto *LLVMC = 203 llvm::ConstantPointerNull::get(cast<llvm::PointerType>(Ty->LLVMTy)); 204 return cast<ConstantPointerNull>(Ty->getContext().getOrCreateConstant(LLVMC)); 205 } 206 207 PointerType *ConstantPointerNull::getType() const { 208 return cast<PointerType>( 209 Ctx.getType(cast<llvm::ConstantPointerNull>(Val)->getType())); 210 } 211 212 UndefValue *UndefValue::get(Type *T) { 213 auto *LLVMC = llvm::UndefValue::get(T->LLVMTy); 214 return cast<UndefValue>(T->getContext().getOrCreateConstant(LLVMC)); 215 } 216 217 UndefValue *UndefValue::getSequentialElement() const { 218 return cast<UndefValue>(Ctx.getOrCreateConstant( 219 cast<llvm::UndefValue>(Val)->getSequentialElement())); 220 } 221 222 UndefValue *UndefValue::getStructElement(unsigned Elt) const { 223 return cast<UndefValue>(Ctx.getOrCreateConstant( 224 cast<llvm::UndefValue>(Val)->getStructElement(Elt))); 225 } 226 227 UndefValue *UndefValue::getElementValue(Constant *C) const { 228 return cast<UndefValue>( 229 Ctx.getOrCreateConstant(cast<llvm::UndefValue>(Val)->getElementValue( 230 cast<llvm::Constant>(C->Val)))); 231 } 232 233 UndefValue *UndefValue::getElementValue(unsigned Idx) const { 234 return cast<UndefValue>(Ctx.getOrCreateConstant( 235 cast<llvm::UndefValue>(Val)->getElementValue(Idx))); 236 } 237 238 PoisonValue *PoisonValue::get(Type *T) { 239 auto *LLVMC = llvm::PoisonValue::get(T->LLVMTy); 240 return cast<PoisonValue>(T->getContext().getOrCreateConstant(LLVMC)); 241 } 242 243 PoisonValue *PoisonValue::getSequentialElement() const { 244 return cast<PoisonValue>(Ctx.getOrCreateConstant( 245 cast<llvm::PoisonValue>(Val)->getSequentialElement())); 246 } 247 248 PoisonValue *PoisonValue::getStructElement(unsigned Elt) const { 249 return cast<PoisonValue>(Ctx.getOrCreateConstant( 250 cast<llvm::PoisonValue>(Val)->getStructElement(Elt))); 251 } 252 253 PoisonValue *PoisonValue::getElementValue(Constant *C) const { 254 return cast<PoisonValue>( 255 Ctx.getOrCreateConstant(cast<llvm::PoisonValue>(Val)->getElementValue( 256 cast<llvm::Constant>(C->Val)))); 257 } 258 259 PoisonValue *PoisonValue::getElementValue(unsigned Idx) const { 260 return cast<PoisonValue>(Ctx.getOrCreateConstant( 261 cast<llvm::PoisonValue>(Val)->getElementValue(Idx))); 262 } 263 264 void GlobalObject::setAlignment(MaybeAlign Align) { 265 Ctx.getTracker() 266 .emplaceIfTracking< 267 GenericSetter<&GlobalObject::getAlign, &GlobalObject::setAlignment>>( 268 this); 269 cast<llvm::GlobalObject>(Val)->setAlignment(Align); 270 } 271 272 void GlobalObject::setGlobalObjectSubClassData(unsigned V) { 273 Ctx.getTracker() 274 .emplaceIfTracking< 275 GenericSetter<&GlobalObject::getGlobalObjectSubClassData, 276 &GlobalObject::setGlobalObjectSubClassData>>(this); 277 cast<llvm::GlobalObject>(Val)->setGlobalObjectSubClassData(V); 278 } 279 280 void GlobalObject::setSection(StringRef S) { 281 Ctx.getTracker() 282 .emplaceIfTracking< 283 GenericSetter<&GlobalObject::getSection, &GlobalObject::setSection>>( 284 this); 285 cast<llvm::GlobalObject>(Val)->setSection(S); 286 } 287 288 template <typename GlobalT, typename LLVMGlobalT, typename ParentT, 289 typename LLVMParentT> 290 GlobalT &GlobalWithNodeAPI<GlobalT, LLVMGlobalT, ParentT, LLVMParentT>:: 291 LLVMGVToGV::operator()(LLVMGlobalT &LLVMGV) const { 292 return cast<GlobalT>(*Ctx.getValue(&LLVMGV)); 293 } 294 295 // Explicit instantiations. 296 template class GlobalWithNodeAPI<GlobalIFunc, llvm::GlobalIFunc, GlobalObject, 297 llvm::GlobalObject>; 298 template class GlobalWithNodeAPI<Function, llvm::Function, GlobalObject, 299 llvm::GlobalObject>; 300 template class GlobalWithNodeAPI<GlobalVariable, llvm::GlobalVariable, 301 GlobalObject, llvm::GlobalObject>; 302 template class GlobalWithNodeAPI<GlobalAlias, llvm::GlobalAlias, GlobalValue, 303 llvm::GlobalValue>; 304 305 #ifdef _MSC_VER 306 // These are needed for SandboxIRTest when building with LLVM_BUILD_LLVM_DYLIB 307 template LLVM_EXPORT_TEMPLATE GlobalIFunc & 308 GlobalWithNodeAPI<GlobalIFunc, llvm::GlobalIFunc, GlobalObject, 309 llvm::GlobalObject>::LLVMGVToGV::operator()(llvm::GlobalIFunc 310 &LLVMGV) 311 const; 312 template LLVM_EXPORT_TEMPLATE Function & 313 GlobalWithNodeAPI<Function, llvm::Function, GlobalObject, llvm::GlobalObject>:: 314 LLVMGVToGV::operator()(llvm::Function &LLVMGV) const; 315 316 template LLVM_EXPORT_TEMPLATE GlobalVariable &GlobalWithNodeAPI< 317 GlobalVariable, llvm::GlobalVariable, GlobalObject, 318 llvm::GlobalObject>::LLVMGVToGV::operator()(llvm::GlobalVariable &LLVMGV) 319 const; 320 template LLVM_EXPORT_TEMPLATE GlobalAlias & 321 GlobalWithNodeAPI<GlobalAlias, llvm::GlobalAlias, GlobalValue, 322 llvm::GlobalValue>::LLVMGVToGV::operator()(llvm::GlobalAlias 323 &LLVMGV) const; 324 #endif 325 326 void GlobalIFunc::setResolver(Constant *Resolver) { 327 Ctx.getTracker() 328 .emplaceIfTracking< 329 GenericSetter<&GlobalIFunc::getResolver, &GlobalIFunc::setResolver>>( 330 this); 331 cast<llvm::GlobalIFunc>(Val)->setResolver( 332 cast<llvm::Constant>(Resolver->Val)); 333 } 334 335 Constant *GlobalIFunc::getResolver() const { 336 return Ctx.getOrCreateConstant(cast<llvm::GlobalIFunc>(Val)->getResolver()); 337 } 338 339 Function *GlobalIFunc::getResolverFunction() { 340 return cast<Function>(Ctx.getOrCreateConstant( 341 cast<llvm::GlobalIFunc>(Val)->getResolverFunction())); 342 } 343 344 GlobalVariable & 345 GlobalVariable::LLVMGVToGV::operator()(llvm::GlobalVariable &LLVMGV) const { 346 return cast<GlobalVariable>(*Ctx.getValue(&LLVMGV)); 347 } 348 349 Constant *GlobalVariable::getInitializer() const { 350 return Ctx.getOrCreateConstant( 351 cast<llvm::GlobalVariable>(Val)->getInitializer()); 352 } 353 354 void GlobalVariable::setInitializer(Constant *InitVal) { 355 Ctx.getTracker() 356 .emplaceIfTracking<GenericSetter<&GlobalVariable::getInitializer, 357 &GlobalVariable::setInitializer>>(this); 358 cast<llvm::GlobalVariable>(Val)->setInitializer( 359 cast<llvm::Constant>(InitVal->Val)); 360 } 361 362 void GlobalVariable::setConstant(bool V) { 363 Ctx.getTracker() 364 .emplaceIfTracking<GenericSetter<&GlobalVariable::isConstant, 365 &GlobalVariable::setConstant>>(this); 366 cast<llvm::GlobalVariable>(Val)->setConstant(V); 367 } 368 369 void GlobalVariable::setExternallyInitialized(bool V) { 370 Ctx.getTracker() 371 .emplaceIfTracking< 372 GenericSetter<&GlobalVariable::isExternallyInitialized, 373 &GlobalVariable::setExternallyInitialized>>(this); 374 cast<llvm::GlobalVariable>(Val)->setExternallyInitialized(V); 375 } 376 377 void GlobalAlias::setAliasee(Constant *Aliasee) { 378 Ctx.getTracker() 379 .emplaceIfTracking< 380 GenericSetter<&GlobalAlias::getAliasee, &GlobalAlias::setAliasee>>( 381 this); 382 cast<llvm::GlobalAlias>(Val)->setAliasee(cast<llvm::Constant>(Aliasee->Val)); 383 } 384 385 Constant *GlobalAlias::getAliasee() const { 386 return cast<Constant>( 387 Ctx.getOrCreateConstant(cast<llvm::GlobalAlias>(Val)->getAliasee())); 388 } 389 390 const GlobalObject *GlobalAlias::getAliaseeObject() const { 391 return cast<GlobalObject>(Ctx.getOrCreateConstant( 392 cast<llvm::GlobalAlias>(Val)->getAliaseeObject())); 393 } 394 395 void GlobalValue::setUnnamedAddr(UnnamedAddr V) { 396 Ctx.getTracker() 397 .emplaceIfTracking<GenericSetter<&GlobalValue::getUnnamedAddr, 398 &GlobalValue::setUnnamedAddr>>(this); 399 cast<llvm::GlobalValue>(Val)->setUnnamedAddr(V); 400 } 401 402 void GlobalValue::setVisibility(VisibilityTypes V) { 403 Ctx.getTracker() 404 .emplaceIfTracking<GenericSetter<&GlobalValue::getVisibility, 405 &GlobalValue::setVisibility>>(this); 406 cast<llvm::GlobalValue>(Val)->setVisibility(V); 407 } 408 409 NoCFIValue *NoCFIValue::get(GlobalValue *GV) { 410 auto *LLVMC = llvm::NoCFIValue::get(cast<llvm::GlobalValue>(GV->Val)); 411 return cast<NoCFIValue>(GV->getContext().getOrCreateConstant(LLVMC)); 412 } 413 414 GlobalValue *NoCFIValue::getGlobalValue() const { 415 auto *LLVMC = cast<llvm::NoCFIValue>(Val)->getGlobalValue(); 416 return cast<GlobalValue>(Ctx.getOrCreateConstant(LLVMC)); 417 } 418 419 PointerType *NoCFIValue::getType() const { 420 return cast<PointerType>(Ctx.getType(cast<llvm::NoCFIValue>(Val)->getType())); 421 } 422 423 ConstantPtrAuth *ConstantPtrAuth::get(Constant *Ptr, ConstantInt *Key, 424 ConstantInt *Disc, Constant *AddrDisc) { 425 auto *LLVMC = llvm::ConstantPtrAuth::get( 426 cast<llvm::Constant>(Ptr->Val), cast<llvm::ConstantInt>(Key->Val), 427 cast<llvm::ConstantInt>(Disc->Val), cast<llvm::Constant>(AddrDisc->Val)); 428 return cast<ConstantPtrAuth>(Ptr->getContext().getOrCreateConstant(LLVMC)); 429 } 430 431 Constant *ConstantPtrAuth::getPointer() const { 432 return Ctx.getOrCreateConstant( 433 cast<llvm::ConstantPtrAuth>(Val)->getPointer()); 434 } 435 436 ConstantInt *ConstantPtrAuth::getKey() const { 437 return cast<ConstantInt>( 438 Ctx.getOrCreateConstant(cast<llvm::ConstantPtrAuth>(Val)->getKey())); 439 } 440 441 ConstantInt *ConstantPtrAuth::getDiscriminator() const { 442 return cast<ConstantInt>(Ctx.getOrCreateConstant( 443 cast<llvm::ConstantPtrAuth>(Val)->getDiscriminator())); 444 } 445 446 Constant *ConstantPtrAuth::getAddrDiscriminator() const { 447 return Ctx.getOrCreateConstant( 448 cast<llvm::ConstantPtrAuth>(Val)->getAddrDiscriminator()); 449 } 450 451 ConstantPtrAuth *ConstantPtrAuth::getWithSameSchema(Constant *Pointer) const { 452 auto *LLVMC = cast<llvm::ConstantPtrAuth>(Val)->getWithSameSchema( 453 cast<llvm::Constant>(Pointer->Val)); 454 return cast<ConstantPtrAuth>(Ctx.getOrCreateConstant(LLVMC)); 455 } 456 457 BlockAddress *BlockAddress::get(Function *F, BasicBlock *BB) { 458 auto *LLVMC = llvm::BlockAddress::get(cast<llvm::Function>(F->Val), 459 cast<llvm::BasicBlock>(BB->Val)); 460 return cast<BlockAddress>(F->getContext().getOrCreateConstant(LLVMC)); 461 } 462 463 BlockAddress *BlockAddress::get(BasicBlock *BB) { 464 auto *LLVMC = llvm::BlockAddress::get(cast<llvm::BasicBlock>(BB->Val)); 465 return cast<BlockAddress>(BB->getContext().getOrCreateConstant(LLVMC)); 466 } 467 468 BlockAddress *BlockAddress::lookup(const BasicBlock *BB) { 469 auto *LLVMC = llvm::BlockAddress::lookup(cast<llvm::BasicBlock>(BB->Val)); 470 return cast_or_null<BlockAddress>(BB->getContext().getValue(LLVMC)); 471 } 472 473 Function *BlockAddress::getFunction() const { 474 return cast<Function>( 475 Ctx.getValue(cast<llvm::BlockAddress>(Val)->getFunction())); 476 } 477 478 BasicBlock *BlockAddress::getBasicBlock() const { 479 return cast<BasicBlock>( 480 Ctx.getValue(cast<llvm::BlockAddress>(Val)->getBasicBlock())); 481 } 482 483 DSOLocalEquivalent *DSOLocalEquivalent::get(GlobalValue *GV) { 484 auto *LLVMC = llvm::DSOLocalEquivalent::get(cast<llvm::GlobalValue>(GV->Val)); 485 return cast<DSOLocalEquivalent>(GV->getContext().getValue(LLVMC)); 486 } 487 488 GlobalValue *DSOLocalEquivalent::getGlobalValue() const { 489 return cast<GlobalValue>( 490 Ctx.getValue(cast<llvm::DSOLocalEquivalent>(Val)->getGlobalValue())); 491 } 492 493 } // namespace llvm::sandboxir 494