Skip to content

Commit 5103910

Browse files
authored
[SandboxIR] Add more functions to sandboxir:Instruction class. (#110050)
The getter functions simply turn around and call the LLVM counterparts. This is fine until we don't add new sandbox IR opcodes. At that point, we may have to explicitly check if the behavior is different.
1 parent e7d68c9 commit 5103910

File tree

2 files changed

+69
-3
lines changed

2 files changed

+69
-3
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1942,7 +1942,13 @@ class Instruction : public sandboxir::User {
19421942
/// state to allow for new SandboxIR-specific instructions.
19431943
Opcode getOpcode() const { return Opc; }
19441944

1945-
// TODO: Missing function getOpcodeName().
1945+
const char *getOpcodeName() const { return getOpcodeName(Opc); }
1946+
1947+
// Note that these functions below are calling into llvm::Instruction.
1948+
// A sandbox IR instruction could introduce a new opcode that could change the
1949+
// behavior of one of these functions. It is better that these functions are
1950+
// only added as needed and new sandbox IR instructions must explicitly check
1951+
// if any of these functions could have a different behavior.
19461952

19471953
bool isTerminator() const {
19481954
return cast<llvm::Instruction>(Val)->isTerminator();
@@ -1954,6 +1960,41 @@ class Instruction : public sandboxir::User {
19541960
}
19551961
bool isShift() const { return cast<llvm::Instruction>(Val)->isShift(); }
19561962
bool isCast() const { return cast<llvm::Instruction>(Val)->isCast(); }
1963+
bool isFuncletPad() const {
1964+
return cast<llvm::Instruction>(Val)->isFuncletPad();
1965+
}
1966+
bool isSpecialTerminator() const {
1967+
return cast<llvm::Instruction>(Val)->isSpecialTerminator();
1968+
}
1969+
bool isOnlyUserOfAnyOperand() const {
1970+
return cast<llvm::Instruction>(Val)->isOnlyUserOfAnyOperand();
1971+
}
1972+
bool isLogicalShift() const {
1973+
return cast<llvm::Instruction>(Val)->isLogicalShift();
1974+
}
1975+
1976+
//===--------------------------------------------------------------------===//
1977+
// Metadata manipulation.
1978+
//===--------------------------------------------------------------------===//
1979+
1980+
/// Return true if the instruction has any metadata attached to it.
1981+
bool hasMetadata() const {
1982+
return cast<llvm::Instruction>(Val)->hasMetadata();
1983+
}
1984+
1985+
/// Return true if this instruction has metadata attached to it other than a
1986+
/// debug location.
1987+
bool hasMetadataOtherThanDebugLoc() const {
1988+
return cast<llvm::Instruction>(Val)->hasMetadataOtherThanDebugLoc();
1989+
}
1990+
1991+
/// Return true if this instruction has the given type of metadata attached.
1992+
bool hasMetadata(unsigned KindID) const {
1993+
return cast<llvm::Instruction>(Val)->hasMetadata(KindID);
1994+
}
1995+
1996+
// TODO: Implement getMetadata and getAllMetadata after sandboxir::MDNode is
1997+
// available.
19571998

19581999
// TODO: More missing functions
19592000

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,9 +1828,12 @@ define void @foo(i8 %v1, ptr %ptr) {
18281828
%atomicrmw = atomicrmw add ptr %ptr, i8 %v1 acquire
18291829
%udiv = udiv i8 %ld0, %v1
18301830
%urem = urem i8 %ld0, %v1
1831-
call void @foo()
1832-
ret void
1831+
call void @foo(), !dbg !1
1832+
ret void, !tbaa !2
18331833
}
1834+
1835+
!1 = !{}
1836+
!2 = !{}
18341837
)IR");
18351838
llvm::Function *LLVMF = &*M->getFunction("foo");
18361839
llvm::BasicBlock *LLVMBB1 = getBasicBlockByName(*LLVMF, "bb1");
@@ -1864,6 +1867,15 @@ define void @foo(i8 %v1, ptr %ptr) {
18641867
EXPECT_EQ(I1->getOpcode(), sandboxir::Instruction::Opcode::Sub);
18651868
EXPECT_EQ(Ret->getOpcode(), sandboxir::Instruction::Opcode::Ret);
18661869

1870+
// Check getOpcodeName().
1871+
EXPECT_EQ(I0->getOpcodeName(), "Add");
1872+
EXPECT_EQ(I1->getOpcodeName(), "Sub");
1873+
EXPECT_EQ(Ret->getOpcodeName(), "Ret");
1874+
1875+
EXPECT_EQ(sandboxir::Instruction::getOpcodeName(
1876+
sandboxir::Instruction::Opcode::Alloca),
1877+
"Alloca");
1878+
18671879
// Check moveBefore(I).
18681880
I1->moveBefore(I0);
18691881
EXPECT_EQ(I0->getPrevNode(), I1);
@@ -1932,6 +1944,19 @@ define void @foo(i8 %v1, ptr %ptr) {
19321944
EXPECT_EQ(LLVMI.isShift(), I.isShift());
19331945
// Check isCast().
19341946
EXPECT_EQ(LLVMI.isCast(), I.isCast());
1947+
// Check isFuncletPad().
1948+
EXPECT_EQ(LLVMI.isFuncletPad(), I.isFuncletPad());
1949+
// Check isSpecialTerminator().
1950+
EXPECT_EQ(LLVMI.isSpecialTerminator(), I.isSpecialTerminator());
1951+
// Check isOnlyUserOfAnyOperand().
1952+
EXPECT_EQ(LLVMI.isOnlyUserOfAnyOperand(), I.isOnlyUserOfAnyOperand());
1953+
// Check isLogicalShift().
1954+
EXPECT_EQ(LLVMI.isLogicalShift(), I.isLogicalShift());
1955+
// Check hasMetadata().
1956+
EXPECT_EQ(LLVMI.hasMetadata(), I.hasMetadata());
1957+
// Check hasMetadataOtherThanDebugLoc().
1958+
EXPECT_EQ(LLVMI.hasMetadataOtherThanDebugLoc(),
1959+
I.hasMetadataOtherThanDebugLoc());
19351960
// Check isAssociative().
19361961
EXPECT_EQ(LLVMI.isAssociative(), I.isAssociative());
19371962
// Check isCommutative().

0 commit comments

Comments
 (0)