From 9fcc92cadfacadf4faa53da1da92bd0c4657b7ab Mon Sep 17 00:00:00 2001 From: Vasileios Porpodas Date: Mon, 23 Sep 2024 12:04:49 -0700 Subject: [PATCH] [SandboxIR] Implement a few Instruction member functions This patch implements some of the missing member functions of sandboxir::Instruction. --- llvm/include/llvm/SandboxIR/SandboxIR.h | 55 ++++++++++++++++++++++ llvm/lib/SandboxIR/SandboxIR.cpp | 4 ++ llvm/unittests/SandboxIR/SandboxIRTest.cpp | 54 ++++++++++++++++++++- 3 files changed, 111 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h index 4e751a75196ce1..84c32aeb0451f5 100644 --- a/llvm/include/llvm/SandboxIR/SandboxIR.h +++ b/llvm/include/llvm/SandboxIR/SandboxIR.h @@ -2057,6 +2057,61 @@ class Instruction : public sandboxir::User { /// LangRef.html for the meaning of these flags. void copyFastMathFlags(FastMathFlags FMF); + bool isAssociative() const { + return cast(Val)->isAssociative(); + } + + bool isCommutative() const { + return cast(Val)->isCommutative(); + } + + bool isIdempotent() const { + return cast(Val)->isIdempotent(); + } + + bool isNilpotent() const { + return cast(Val)->isNilpotent(); + } + + bool mayWriteToMemory() const { + return cast(Val)->mayWriteToMemory(); + } + + bool mayReadFromMemory() const { + return cast(Val)->mayReadFromMemory(); + } + bool mayReadOrWriteMemory() const { + return cast(Val)->mayReadOrWriteMemory(); + } + + bool isAtomic() const { return cast(Val)->isAtomic(); } + + bool hasAtomicLoad() const { + return cast(Val)->hasAtomicLoad(); + } + + bool hasAtomicStore() const { + return cast(Val)->hasAtomicStore(); + } + + bool isVolatile() const { return cast(Val)->isVolatile(); } + + Type *getAccessType() const; + + bool mayThrow(bool IncludePhaseOneUnwind = false) const { + return cast(Val)->mayThrow(IncludePhaseOneUnwind); + } + + bool isFenceLike() const { + return cast(Val)->isFenceLike(); + } + + bool mayHaveSideEffects() const { + return cast(Val)->mayHaveSideEffects(); + } + + // TODO: Missing functions. + bool isStackSaveOrRestoreIntrinsic() const { auto *I = cast(Val); return match(I, diff --git a/llvm/lib/SandboxIR/SandboxIR.cpp b/llvm/lib/SandboxIR/SandboxIR.cpp index b1ed72be1030d6..124c1bf92ca7fc 100644 --- a/llvm/lib/SandboxIR/SandboxIR.cpp +++ b/llvm/lib/SandboxIR/SandboxIR.cpp @@ -569,6 +569,10 @@ void Instruction::copyFastMathFlags(FastMathFlags FMF) { cast(Val)->copyFastMathFlags(FMF); } +Type *Instruction::getAccessType() const { + return Ctx.getType(cast(Val)->getAccessType()); +} + void Instruction::setHasApproxFunc(bool B) { Ctx.getTracker() .emplaceIfTrackinggetFunction("foo"); + llvm::BasicBlock *LLVMBB1 = getBasicBlockByName(*LLVMF, "bb1"); sandboxir::Context Ctx(C); sandboxir::Function *F = Ctx.createFunction(LLVMF); auto *Arg = F->getArg(0); - auto *BB = &*F->begin(); + auto *BB = cast( + Ctx.getValue(getBasicBlockByName(*LLVMF, "bb0"))); auto It = BB->begin(); auto *I0 = &*It++; auto *I1 = &*It++; @@ -1844,6 +1858,42 @@ define void @foo(i8 %v1) { I1->eraseFromParent(); EXPECT_EQ(I0->getNumUses(), 0u); EXPECT_EQ(I0->getNextNode(), Ret); + + for (auto &LLVMI : *LLVMBB1) { + auto &I = cast(*Ctx.getValue(&LLVMI)); + // Check isAssociative(). + EXPECT_EQ(LLVMI.isAssociative(), I.isAssociative()); + // Check isCommutative(). + EXPECT_EQ(LLVMI.isCommutative(), I.isCommutative()); + // Check isIdempotent(). + EXPECT_EQ(LLVMI.isIdempotent(), I.isIdempotent()); + // Check isNilpotent(). + EXPECT_EQ(LLVMI.isNilpotent(), I.isNilpotent()); + // Check mayWriteToMemory(). + EXPECT_EQ(LLVMI.mayWriteToMemory(), I.mayWriteToMemory()); + // Check mayReadFromMemory(). + EXPECT_EQ(LLVMI.mayReadFromMemory(), I.mayReadFromMemory()); + // Check mayReadOrWriteMemory(). + EXPECT_EQ(LLVMI.mayReadOrWriteMemory(), I.mayReadOrWriteMemory()); + // Check isAtomic(). + EXPECT_EQ(LLVMI.isAtomic(), I.isAtomic()); + if (I.isAtomic()) { + // Check hasAtomicLoad(). + EXPECT_EQ(LLVMI.hasAtomicLoad(), I.hasAtomicLoad()); + // Check hasAtomicStore(). + EXPECT_EQ(LLVMI.hasAtomicStore(), I.hasAtomicStore()); + } + // Check isVolatile(). + EXPECT_EQ(LLVMI.isVolatile(), I.isVolatile()); + // Check getAccessType(). + EXPECT_EQ(Ctx.getType(LLVMI.getAccessType()), I.getAccessType()); + // Check mayThrow(). + EXPECT_EQ(LLVMI.mayThrow(), I.mayThrow()); + // Check isFenceLike(). + EXPECT_EQ(LLVMI.isFenceLike(), I.isFenceLike()); + // Check mayHaveSideEffects(). + EXPECT_EQ(LLVMI.mayHaveSideEffects(), I.mayHaveSideEffects()); + } } TEST_F(SandboxIRTest, Instruction_isStackSaveOrRestoreIntrinsic) {