Skip to content

Commit 950bb68

Browse files
authored
[SandboxIR] Implement ConstantPointerNull (#107320)
This patch implements sandboxir::ConstantPointerNull mirroring llvm::ConstantPointerNull.
1 parent 7c4eb60 commit 950bb68

File tree

4 files changed

+74
-0
lines changed

4 files changed

+74
-0
lines changed

llvm/include/llvm/SandboxIR/SandboxIR.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class BasicBlock;
121121
class ConstantInt;
122122
class ConstantFP;
123123
class ConstantAggregateZero;
124+
class ConstantPointerNull;
124125
class Context;
125126
class Function;
126127
class Instruction;
@@ -318,6 +319,7 @@ class Value {
318319
friend class ConstantArray; // For `Val`.
319320
friend class ConstantStruct; // For `Val`.
320321
friend class ConstantAggregateZero; // For `Val`.
322+
friend class ConstantPointerNull; // For `Val`.
321323

322324
/// All values point to the context.
323325
Context &Ctx;
@@ -987,6 +989,35 @@ class ConstantAggregateZero final : public Constant {
987989
#endif
988990
};
989991

992+
// TODO: Inherit from ConstantData.
993+
class ConstantPointerNull final : public Constant {
994+
ConstantPointerNull(llvm::ConstantPointerNull *C, Context &Ctx)
995+
: Constant(ClassID::ConstantPointerNull, C, Ctx) {}
996+
friend class Context; // For constructor.
997+
998+
public:
999+
static ConstantPointerNull *get(PointerType *Ty);
1000+
1001+
PointerType *getType() const;
1002+
1003+
/// For isa/dyn_cast.
1004+
static bool classof(const sandboxir::Value *From) {
1005+
return From->getSubclassID() == ClassID::ConstantPointerNull;
1006+
}
1007+
unsigned getUseOperandNo(const Use &Use) const final {
1008+
llvm_unreachable("ConstantPointerNull has no operands!");
1009+
}
1010+
#ifndef NDEBUG
1011+
void verify() const override {
1012+
assert(isa<llvm::ConstantPointerNull>(Val) && "Expected a CPNull!");
1013+
}
1014+
void dumpOS(raw_ostream &OS) const override {
1015+
dumpCommonPrefix(OS);
1016+
dumpCommonSuffix(OS);
1017+
}
1018+
#endif
1019+
};
1020+
9901021
/// Iterator for `Instruction`s in a `BasicBlock.
9911022
/// \Returns an sandboxir::Instruction & when derereferenced.
9921023
class BBIterator {

llvm/include/llvm/SandboxIR/SandboxIRValues.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ DEF_CONST(ConstantArray, ConstantArray)
3131
DEF_CONST(ConstantStruct, ConstantStruct)
3232
DEF_CONST(ConstantVector, ConstantVector)
3333
DEF_CONST(ConstantAggregateZero, ConstantAggregateZero)
34+
DEF_CONST(ConstantPointerNull, ConstantPointerNull)
3435

3536
#ifndef DEF_INSTR
3637
#define DEF_INSTR(ID, OPCODE, CLASS)

llvm/lib/SandboxIR/SandboxIR.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,6 +2426,17 @@ Constant *ConstantAggregateZero::getElementValue(unsigned Idx) const {
24262426
cast<llvm::ConstantAggregateZero>(Val)->getElementValue(Idx)));
24272427
}
24282428

2429+
ConstantPointerNull *ConstantPointerNull::get(PointerType *Ty) {
2430+
auto *LLVMC =
2431+
llvm::ConstantPointerNull::get(cast<llvm::PointerType>(Ty->LLVMTy));
2432+
return cast<ConstantPointerNull>(Ty->getContext().getOrCreateConstant(LLVMC));
2433+
}
2434+
2435+
PointerType *ConstantPointerNull::getType() const {
2436+
return cast<PointerType>(
2437+
Ctx.getType(cast<llvm::ConstantPointerNull>(Val)->getType()));
2438+
}
2439+
24292440
FunctionType *Function::getFunctionType() const {
24302441
return cast<FunctionType>(
24312442
Ctx.getType(cast<llvm::Function>(Val)->getFunctionType()));
@@ -2535,6 +2546,10 @@ Value *Context::getOrCreateValueInternal(llvm::Value *LLVMV, llvm::User *U) {
25352546
}
25362547
return Ret;
25372548
}
2549+
case llvm::Value::ConstantPointerNullVal:
2550+
It->second = std::unique_ptr<ConstantPointerNull>(
2551+
new ConstantPointerNull(cast<llvm::ConstantPointerNull>(C), *this));
2552+
return It->second.get();
25382553
case llvm::Value::ConstantArrayVal:
25392554
It->second = std::unique_ptr<ConstantArray>(
25402555
new ConstantArray(cast<llvm::ConstantArray>(C), *this));

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,33 @@ define void @foo(ptr %ptr, {i32, i8} %v1, <2 x i8> %v2) {
589589
EXPECT_EQ(NewVectorCAZ->getElementCount(), ElementCount::getFixed(4));
590590
}
591591

592+
TEST_F(SandboxIRTest, ConstantPointerNull) {
593+
parseIR(C, R"IR(
594+
define ptr @foo() {
595+
ret ptr null
596+
}
597+
)IR");
598+
Function &LLVMF = *M->getFunction("foo");
599+
sandboxir::Context Ctx(C);
600+
601+
auto &F = *Ctx.createFunction(&LLVMF);
602+
auto &BB = *F.begin();
603+
auto It = BB.begin();
604+
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
605+
// Check classof() and creation.
606+
auto *CPNull = cast<sandboxir::ConstantPointerNull>(Ret->getReturnValue());
607+
// Check get().
608+
auto *NewCPNull =
609+
sandboxir::ConstantPointerNull::get(sandboxir::PointerType::get(Ctx, 0u));
610+
EXPECT_EQ(NewCPNull, CPNull);
611+
auto *NewCPNull2 =
612+
sandboxir::ConstantPointerNull::get(sandboxir::PointerType::get(Ctx, 1u));
613+
EXPECT_NE(NewCPNull2, CPNull);
614+
// Check getType().
615+
EXPECT_EQ(CPNull->getType(), sandboxir::PointerType::get(Ctx, 0u));
616+
EXPECT_EQ(NewCPNull2->getType(), sandboxir::PointerType::get(Ctx, 1u));
617+
}
618+
592619
TEST_F(SandboxIRTest, Use) {
593620
parseIR(C, R"IR(
594621
define i32 @foo(i32 %v0, i32 %v1) {

0 commit comments

Comments
 (0)