1 //===- Function.cpp - The Function class 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/Function.h" 10 #include "llvm/IR/Value.h" 11 #include "llvm/SandboxIR/Context.h" 12 13 namespace llvm::sandboxir { 14 getFunctionType() const15FunctionType *Function::getFunctionType() const { 16 return cast<FunctionType>( 17 Ctx.getType(cast<llvm::Function>(Val)->getFunctionType())); 18 } 19 setAlignment(MaybeAlign Align)20void Function::setAlignment(MaybeAlign Align) { 21 Ctx.getTracker() 22 .emplaceIfTracking< 23 GenericSetter<&Function::getAlign, &Function::setAlignment>>(this); 24 cast<llvm::Function>(Val)->setAlignment(Align); 25 } 26 27 #ifndef NDEBUG dumpNameAndArgs(raw_ostream & OS) const28void Function::dumpNameAndArgs(raw_ostream &OS) const { 29 auto *F = cast<llvm::Function>(Val); 30 OS << *F->getReturnType() << " @" << F->getName() << "("; 31 interleave( 32 F->args(), 33 [this, &OS](const llvm::Argument &LLVMArg) { 34 auto *SBArg = cast_or_null<Argument>(Ctx.getValue(&LLVMArg)); 35 if (SBArg == nullptr) 36 OS << "NULL"; 37 else 38 SBArg->printAsOperand(OS); 39 }, 40 [&] { OS << ", "; }); 41 OS << ")"; 42 } 43 dumpOS(raw_ostream & OS) const44void Function::dumpOS(raw_ostream &OS) const { 45 dumpNameAndArgs(OS); 46 OS << " {\n"; 47 auto *LLVMF = cast<llvm::Function>(Val); 48 interleave( 49 *LLVMF, 50 [this, &OS](const llvm::BasicBlock &LLVMBB) { 51 auto *BB = cast_or_null<BasicBlock>(Ctx.getValue(&LLVMBB)); 52 if (BB == nullptr) 53 OS << "NULL"; 54 else 55 OS << *BB; 56 }, 57 [&OS] { OS << "\n"; }); 58 OS << "}\n"; 59 } 60 #endif // NDEBUG 61 62 } // namespace llvm::sandboxir 63