Skip to content

Commit a187e3c

Browse files
vporpoSterling-Augustine
authored andcommitted
[SandboxIR][NFC] Move isMemDepCandidate() and isStackSaveOrRestoreIntrinsic() to Utils
1 parent 26839d2 commit a187e3c

File tree

5 files changed

+96
-92
lines changed

5 files changed

+96
-92
lines changed

llvm/include/llvm/SandboxIR/Instruction.h

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -335,26 +335,6 @@ class Instruction : public User {
335335

336336
// TODO: Missing functions.
337337

338-
bool isStackSaveOrRestoreIntrinsic() const {
339-
auto *I = cast<llvm::Instruction>(Val);
340-
return match(I,
341-
PatternMatch::m_Intrinsic<llvm::Intrinsic::stackrestore>()) ||
342-
match(I, PatternMatch::m_Intrinsic<llvm::Intrinsic::stacksave>());
343-
}
344-
345-
/// We consider \p I as a Memory Dependency Candidate instruction if it
346-
/// reads/write memory or if it has side-effects. This is used by the
347-
/// dependency graph.
348-
bool isMemDepCandidate() const {
349-
auto *I = cast<llvm::Instruction>(Val);
350-
return I->mayReadOrWriteMemory() &&
351-
(!isa<llvm::IntrinsicInst>(I) ||
352-
(cast<llvm::IntrinsicInst>(I)->getIntrinsicID() !=
353-
Intrinsic::sideeffect &&
354-
cast<llvm::IntrinsicInst>(I)->getIntrinsicID() !=
355-
Intrinsic::pseudoprobe));
356-
}
357-
358338
#ifndef NDEBUG
359339
void dumpOS(raw_ostream &OS) const override;
360340
#endif

llvm/include/llvm/SandboxIR/Utils.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,27 @@ class Utils {
9999
return false;
100100
return *Diff > 0;
101101
}
102+
103+
static bool isStackSaveOrRestoreIntrinsic(Instruction *I) {
104+
auto *LLVMI = cast<llvm::Instruction>(I->Val);
105+
return match(LLVMI,
106+
PatternMatch::m_Intrinsic<llvm::Intrinsic::stackrestore>()) ||
107+
match(LLVMI,
108+
PatternMatch::m_Intrinsic<llvm::Intrinsic::stacksave>());
109+
}
110+
111+
/// We consider \p I as a Memory Dependency Candidate instruction if it
112+
/// reads/write memory or if it has side-effects. This is used by the
113+
/// dependency graph.
114+
static bool isMemDepCandidate(Instruction *I) {
115+
auto *LLVMI = cast<llvm::Instruction>(I->Val);
116+
return LLVMI->mayReadOrWriteMemory() &&
117+
(!isa<llvm::IntrinsicInst>(LLVMI) ||
118+
(cast<llvm::IntrinsicInst>(LLVMI)->getIntrinsicID() !=
119+
Intrinsic::sideeffect &&
120+
cast<llvm::IntrinsicInst>(LLVMI)->getIntrinsicID() !=
121+
Intrinsic::pseudoprobe));
122+
}
102123
};
103124
} // namespace llvm::sandboxir
104125

llvm/include/llvm/Transforms/Vectorize/SandboxVectorizer/DependencyGraph.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "llvm/ADT/DenseMap.h"
2626
#include "llvm/ADT/iterator_range.h"
2727
#include "llvm/SandboxIR/Instruction.h"
28+
#include "llvm/SandboxIR/Utils.h"
2829
#include "llvm/Transforms/Vectorize/SandboxVectorizer/Interval.h"
2930

3031
namespace llvm::sandboxir {
@@ -63,10 +64,10 @@ class DGNode {
6364
/// \Returns true if \p I is a memory dependency candidate instruction.
6465
static bool isMemDepCandidate(Instruction *I) {
6566
AllocaInst *Alloca;
66-
return I->isMemDepCandidate() ||
67+
return Utils::isMemDepCandidate(I) ||
6768
((Alloca = dyn_cast<AllocaInst>(I)) &&
6869
Alloca->isUsedWithInAlloca()) ||
69-
I->isStackSaveOrRestoreIntrinsic();
70+
Utils::isStackSaveOrRestoreIntrinsic(I);
7071
}
7172

7273
Instruction *getInstruction() const { return I; }

llvm/unittests/SandboxIR/SandboxIRTest.cpp

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,76 +1977,6 @@ define void @foo(i8 %v1, ptr %ptr) {
19771977
}
19781978
}
19791979

1980-
TEST_F(SandboxIRTest, Instruction_isStackSaveOrRestoreIntrinsic) {
1981-
parseIR(C, R"IR(
1982-
declare void @llvm.sideeffect()
1983-
define void @foo(i8 %v1, ptr %ptr) {
1984-
%add = add i8 %v1, %v1
1985-
%stacksave = call ptr @llvm.stacksave()
1986-
call void @llvm.stackrestore(ptr %stacksave)
1987-
call void @llvm.sideeffect()
1988-
ret void
1989-
}
1990-
)IR");
1991-
llvm::Function *LLVMF = &*M->getFunction("foo");
1992-
sandboxir::Context Ctx(C);
1993-
sandboxir::Function *F = Ctx.createFunction(LLVMF);
1994-
auto *BB = &*F->begin();
1995-
auto It = BB->begin();
1996-
auto *Add = cast<sandboxir::BinaryOperator>(&*It++);
1997-
auto *StackSave = cast<sandboxir::CallInst>(&*It++);
1998-
auto *StackRestore = cast<sandboxir::CallInst>(&*It++);
1999-
auto *Other = cast<sandboxir::CallInst>(&*It++);
2000-
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
2001-
2002-
EXPECT_FALSE(Add->isStackSaveOrRestoreIntrinsic());
2003-
EXPECT_TRUE(StackSave->isStackSaveOrRestoreIntrinsic());
2004-
EXPECT_TRUE(StackRestore->isStackSaveOrRestoreIntrinsic());
2005-
EXPECT_FALSE(Other->isStackSaveOrRestoreIntrinsic());
2006-
EXPECT_FALSE(Ret->isStackSaveOrRestoreIntrinsic());
2007-
}
2008-
2009-
TEST_F(SandboxIRTest, Instruction_isMemDepCandidate) {
2010-
parseIR(C, R"IR(
2011-
declare void @llvm.fake.use(...)
2012-
declare void @llvm.sideeffect()
2013-
declare void @llvm.pseudoprobe(i64, i64, i32, i64)
2014-
declare void @bar()
2015-
define void @foo(i8 %v1, ptr %ptr) {
2016-
%add0 = add i8 %v1, %v1
2017-
%ld0 = load i8, ptr %ptr
2018-
store i8 %v1, ptr %ptr
2019-
call void @llvm.sideeffect()
2020-
call void @llvm.pseudoprobe(i64 42, i64 1, i32 0, i64 -1)
2021-
call void @llvm.fake.use(ptr %ptr)
2022-
call void @bar()
2023-
ret void
2024-
}
2025-
)IR");
2026-
llvm::Function *LLVMF = &*M->getFunction("foo");
2027-
sandboxir::Context Ctx(C);
2028-
sandboxir::Function *F = Ctx.createFunction(LLVMF);
2029-
auto *BB = &*F->begin();
2030-
auto It = BB->begin();
2031-
auto *Add0 = cast<sandboxir::BinaryOperator>(&*It++);
2032-
auto *Ld0 = cast<sandboxir::LoadInst>(&*It++);
2033-
auto *St0 = cast<sandboxir::StoreInst>(&*It++);
2034-
auto *SideEffect0 = cast<sandboxir::CallInst>(&*It++);
2035-
auto *PseudoProbe0 = cast<sandboxir::CallInst>(&*It++);
2036-
auto *OtherIntrinsic0 = cast<sandboxir::CallInst>(&*It++);
2037-
auto *CallBar = cast<sandboxir::CallInst>(&*It++);
2038-
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
2039-
2040-
EXPECT_FALSE(Add0->isMemDepCandidate());
2041-
EXPECT_TRUE(Ld0->isMemDepCandidate());
2042-
EXPECT_TRUE(St0->isMemDepCandidate());
2043-
EXPECT_FALSE(SideEffect0->isMemDepCandidate());
2044-
EXPECT_FALSE(PseudoProbe0->isMemDepCandidate());
2045-
EXPECT_TRUE(OtherIntrinsic0->isMemDepCandidate());
2046-
EXPECT_TRUE(CallBar->isMemDepCandidate());
2047-
EXPECT_FALSE(Ret->isMemDepCandidate());
2048-
}
2049-
20501980
TEST_F(SandboxIRTest, VAArgInst) {
20511981
parseIR(C, R"IR(
20521982
define void @foo(ptr %va) {

llvm/unittests/SandboxIR/UtilsTest.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,75 @@ define void @foo(float %arg0, double %arg1, i8 %arg2, i64 %arg3, ptr %arg4) {
173173
EXPECT_EQ(sandboxir::Utils::getNumBits(L2), 8u);
174174
EXPECT_EQ(sandboxir::Utils::getNumBits(L3), 64u);
175175
}
176+
177+
TEST_F(UtilsTest, Instruction_isStackSaveOrRestoreIntrinsic) {
178+
parseIR(C, R"IR(
179+
declare void @llvm.sideeffect()
180+
define void @foo(i8 %v1, ptr %ptr) {
181+
%add = add i8 %v1, %v1
182+
%stacksave = call ptr @llvm.stacksave()
183+
call void @llvm.stackrestore(ptr %stacksave)
184+
call void @llvm.sideeffect()
185+
ret void
186+
}
187+
)IR");
188+
llvm::Function *LLVMF = &*M->getFunction("foo");
189+
sandboxir::Context Ctx(C);
190+
sandboxir::Function *F = Ctx.createFunction(LLVMF);
191+
auto *BB = &*F->begin();
192+
auto It = BB->begin();
193+
auto *Add = cast<sandboxir::BinaryOperator>(&*It++);
194+
auto *StackSave = cast<sandboxir::CallInst>(&*It++);
195+
auto *StackRestore = cast<sandboxir::CallInst>(&*It++);
196+
auto *Other = cast<sandboxir::CallInst>(&*It++);
197+
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
198+
199+
EXPECT_FALSE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(Add));
200+
EXPECT_TRUE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(StackSave));
201+
EXPECT_TRUE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(StackRestore));
202+
EXPECT_FALSE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(Other));
203+
EXPECT_FALSE(sandboxir::Utils::isStackSaveOrRestoreIntrinsic(Ret));
204+
}
205+
206+
TEST_F(UtilsTest, Instruction_isMemDepCandidate) {
207+
parseIR(C, R"IR(
208+
declare void @llvm.fake.use(...)
209+
declare void @llvm.sideeffect()
210+
declare void @llvm.pseudoprobe(i64, i64, i32, i64)
211+
declare void @bar()
212+
define void @foo(i8 %v1, ptr %ptr) {
213+
%add0 = add i8 %v1, %v1
214+
%ld0 = load i8, ptr %ptr
215+
store i8 %v1, ptr %ptr
216+
call void @llvm.sideeffect()
217+
call void @llvm.pseudoprobe(i64 42, i64 1, i32 0, i64 -1)
218+
call void @llvm.fake.use(ptr %ptr)
219+
call void @bar()
220+
ret void
221+
}
222+
)IR");
223+
llvm::Function *LLVMF = &*M->getFunction("foo");
224+
sandboxir::Context Ctx(C);
225+
sandboxir::Function *F = Ctx.createFunction(LLVMF);
226+
auto *BB = &*F->begin();
227+
auto It = BB->begin();
228+
auto *Add0 = cast<sandboxir::BinaryOperator>(&*It++);
229+
auto *Ld0 = cast<sandboxir::LoadInst>(&*It++);
230+
auto *St0 = cast<sandboxir::StoreInst>(&*It++);
231+
auto *SideEffect0 = cast<sandboxir::CallInst>(&*It++);
232+
auto *PseudoProbe0 = cast<sandboxir::CallInst>(&*It++);
233+
auto *OtherIntrinsic0 = cast<sandboxir::CallInst>(&*It++);
234+
auto *CallBar = cast<sandboxir::CallInst>(&*It++);
235+
auto *Ret = cast<sandboxir::ReturnInst>(&*It++);
236+
237+
using Utils = sandboxir::Utils;
238+
239+
EXPECT_FALSE(Utils::isMemDepCandidate(Add0));
240+
EXPECT_TRUE(Utils::isMemDepCandidate(Ld0));
241+
EXPECT_TRUE(Utils::isMemDepCandidate(St0));
242+
EXPECT_FALSE(Utils::isMemDepCandidate(SideEffect0));
243+
EXPECT_FALSE(Utils::isMemDepCandidate(PseudoProbe0));
244+
EXPECT_TRUE(Utils::isMemDepCandidate(OtherIntrinsic0));
245+
EXPECT_TRUE(Utils::isMemDepCandidate(CallBar));
246+
EXPECT_FALSE(Utils::isMemDepCandidate(Ret));
247+
}

0 commit comments

Comments
 (0)