Skip to content

Commit 416c3ce

Browse files
authored
[SandboxIR] Implement ConstantExpr (#109491)
This patch implements an empty sandboxir::ConstantExpr class, mirroring llvm::ConstantExpr.
1 parent e093bb9 commit 416c3ce

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

+15
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ class GlobalVariable;
135135
class GlobalAlias;
136136
class NoCFIValue;
137137
class ConstantPtrAuth;
138+
class ConstantExpr;
138139
class Context;
139140
class Function;
140141
class Instruction;
@@ -344,6 +345,7 @@ class Value {
344345
friend class GlobalAlias; // For `Val`.
345346
friend class NoCFIValue; // For `Val`.
346347
friend class ConstantPtrAuth; // For `Val`.
348+
friend class ConstantExpr; // For `Val`.
347349

348350
/// All values point to the context.
349351
Context &Ctx;
@@ -1661,6 +1663,19 @@ class ConstantPtrAuth final : public Constant {
16611663
}
16621664
};
16631665

1666+
class ConstantExpr : public Constant {
1667+
ConstantExpr(llvm::ConstantExpr *C, Context &Ctx)
1668+
: Constant(ClassID::ConstantExpr, C, Ctx) {}
1669+
friend class Context; // For constructor.
1670+
1671+
public:
1672+
/// For isa/dyn_cast.
1673+
static bool classof(const sandboxir::Value *From) {
1674+
return From->getSubclassID() == ClassID::ConstantExpr;
1675+
}
1676+
// TODO: Missing functions.
1677+
};
1678+
16641679
class BlockAddress final : public Constant {
16651680
BlockAddress(llvm::BlockAddress *C, Context &Ctx)
16661681
: Constant(ClassID::BlockAddress, C, Ctx) {}

llvm/include/llvm/SandboxIR/SandboxIRValues.def

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ DEF_CONST(GlobalAlias, GlobalAlias)
4040
DEF_CONST(BlockAddress, BlockAddress)
4141
DEF_CONST(NoCFIValue, NoCFIValue)
4242
DEF_CONST(ConstantPtrAuth, ConstantPtrAuth)
43+
DEF_CONST(ConstantExpr, ConstantExpr)
4344
DEF_CONST(DSOLocalEquivalent, DSOLocalEquivalent)
4445
DEF_CONST(ConstantTokenNone, ConstantTokenNone)
4546

llvm/lib/SandboxIR/SandboxIR.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -2888,6 +2888,10 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
28882888
It->second = std::unique_ptr<ConstantPtrAuth>(
28892889
new ConstantPtrAuth(cast<llvm::ConstantPtrAuth>(C), *this));
28902890
break;
2891+
case llvm::Value::ConstantExprVal:
2892+
It->second = std::unique_ptr<ConstantExpr>(
2893+
new ConstantExpr(cast<llvm::ConstantExpr>(C), *this));
2894+
break;
28912895
default:
28922896
It->second = std::unique_ptr<Constant>(new Constant(C, *this));
28932897
break;

llvm/unittests/SandboxIR/SandboxIRTest.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -1180,6 +1180,24 @@ define ptr @foo() {
11801180
EXPECT_EQ(PtrAuth->getWithSameSchema(&F), PtrAuth);
11811181
}
11821182

1183+
TEST_F(SandboxIRTest, ConstantExpr) {
1184+
parseIR(C, R"IR(
1185+
define i32 @foo() {
1186+
ret i32 ptrtoint (ptr @foo to i32)
1187+
}
1188+
)IR");
1189+
Function &LLVMF = *M->getFunction("foo");
1190+
sandboxir::Context Ctx(C);
1191+
1192+
auto &F = *Ctx.createFunction(&LLVMF);
1193+
auto *BB = &*F.begin();
1194+
auto It = BB->begin();
1195+
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
1196+
// Check classof(), creation.
1197+
[[maybe_unused]] auto *ConstExpr =
1198+
cast<sandboxir::ConstantExpr>(Ret->getReturnValue());
1199+
}
1200+
11831201
TEST_F(SandboxIRTest, BlockAddress) {
11841202
parseIR(C, R"IR(
11851203
define void @foo(ptr %ptr) {

0 commit comments

Comments
 (0)