Skip to content

Commit f0909e3

Browse files
[SandboxIR] Add utility function to find the base Value for Mem instructions (#112030)
1 parent 2bb3d3a commit f0909e3

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

llvm/include/llvm/SandboxIR/Context.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class Context {
6868
}
6969
/// Get or create a sandboxir::Constant from an existing LLVM IR \p LLVMC.
7070
Constant *getOrCreateConstant(llvm::Constant *LLVMC);
71+
friend class Utils; // For getMemoryBase
7172

7273
// Friends for getOrCreateConstant().
7374
#define DEF_CONST(ID, CLASS) friend class CLASS;

llvm/include/llvm/SandboxIR/Utils.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,16 @@ class Utils {
5050
return const_cast<Instruction *>(I);
5151
}
5252

53+
/// \Returns the base Value for load or store instruction \p LSI.
54+
template <typename LoadOrStoreT>
55+
static Value *getMemInstructionBase(const LoadOrStoreT *LSI) {
56+
static_assert(std::is_same_v<LoadOrStoreT, LoadInst> ||
57+
std::is_same_v<LoadOrStoreT, StoreInst>,
58+
"Expected sandboxir::Load or sandboxir::Store!");
59+
return LSI->Ctx.getOrCreateValue(
60+
getUnderlyingObject(LSI->getPointerOperand()->Val));
61+
}
62+
5363
/// \Returns the number of bits required to represent the operands or return
5464
/// value of \p V in \p DL.
5565
static unsigned getNumBits(Value *V, const DataLayout &DL) {

llvm/unittests/SandboxIR/UtilsTest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,35 @@ define void @foo(float %arg0, double %arg1, i8 %arg2, i64 %arg3, ptr %arg4) {
215215
EXPECT_EQ(sandboxir::Utils::getNumBits(L2), 8u);
216216
EXPECT_EQ(sandboxir::Utils::getNumBits(L3), 64u);
217217
}
218+
219+
TEST_F(UtilsTest, GetMemBase) {
220+
parseIR(C, R"IR(
221+
define void @foo(ptr %ptrA, float %val, ptr %ptrB) {
222+
bb:
223+
%gepA0 = getelementptr float, ptr %ptrA, i32 0
224+
%gepA1 = getelementptr float, ptr %ptrA, i32 1
225+
%gepB0 = getelementptr float, ptr %ptrB, i32 0
226+
%gepB1 = getelementptr float, ptr %ptrB, i32 1
227+
store float %val, ptr %gepA0
228+
store float %val, ptr %gepA1
229+
store float %val, ptr %gepB0
230+
store float %val, ptr %gepB1
231+
ret void
232+
}
233+
)IR");
234+
llvm::Function &Foo = *M->getFunction("foo");
235+
sandboxir::Context Ctx(C);
236+
sandboxir::Function *F = Ctx.createFunction(&Foo);
237+
238+
auto It = std::next(F->begin()->begin(), 4);
239+
auto *St0 = cast<sandboxir::StoreInst>(&*It++);
240+
auto *St1 = cast<sandboxir::StoreInst>(&*It++);
241+
auto *St2 = cast<sandboxir::StoreInst>(&*It++);
242+
auto *St3 = cast<sandboxir::StoreInst>(&*It++);
243+
EXPECT_EQ(sandboxir::Utils::getMemInstructionBase(St0),
244+
sandboxir::Utils::getMemInstructionBase(St1));
245+
EXPECT_EQ(sandboxir::Utils::getMemInstructionBase(St2),
246+
sandboxir::Utils::getMemInstructionBase(St3));
247+
EXPECT_NE(sandboxir::Utils::getMemInstructionBase(St0),
248+
sandboxir::Utils::getMemInstructionBase(St3));
249+
}

0 commit comments

Comments
 (0)