Skip to content

Commit a1ff427

Browse files
authored
[SandboxIR][NFC] Move intrinsic code to Utils::isMemIntrinsic() (#111019)
This patch moves the intrinsic specific code from Utils::isMemDepCandidate() to a new function: Utils::isMemIntrinsic().
1 parent 45582ed commit a1ff427

File tree

3 files changed

+45
-12
lines changed

3 files changed

+45
-12
lines changed

llvm/include/llvm/SandboxIR/Utils.h

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "llvm/Analysis/ScalarEvolution.h"
1818
#include "llvm/Analysis/ValueTracking.h"
1919
#include "llvm/SandboxIR/Instruction.h"
20+
#include "llvm/SandboxIR/IntrinsicInst.h"
2021
#include <optional>
2122

2223
namespace llvm::sandboxir {
@@ -101,24 +102,27 @@ class Utils {
101102
}
102103

103104
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>());
105+
if (auto *II = dyn_cast<IntrinsicInst>(I)) {
106+
auto IID = II->getIntrinsicID();
107+
return IID == Intrinsic::stackrestore || IID == Intrinsic::stacksave;
108+
}
109+
return false;
110+
}
111+
112+
/// \Returns true if intrinsic \p I touches memory. This is used by the
113+
/// dependency graph.
114+
static bool isMemIntrinsic(IntrinsicInst *II) {
115+
auto IID = II->getIntrinsicID();
116+
return IID != Intrinsic::sideeffect && IID != Intrinsic::pseudoprobe;
109117
}
110118

111119
/// We consider \p I as a Memory Dependency Candidate instruction if it
112120
/// reads/write memory or if it has side-effects. This is used by the
113121
/// dependency graph.
114122
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));
123+
IntrinsicInst *II;
124+
return I->mayReadOrWriteMemory() &&
125+
(!(II = dyn_cast<IntrinsicInst>(I)) || isMemIntrinsic(II));
122126
}
123127
};
124128
} // namespace llvm::sandboxir

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class DGNode {
6161
virtual ~DGNode() = default;
6262
/// \Returns true if this is before \p Other in program order.
6363
bool comesBefore(const DGNode *Other) { return I->comesBefore(Other->I); }
64+
6465
/// \Returns true if \p I is a memory dependency candidate instruction.
6566
static bool isMemDepNodeCandidate(Instruction *I) {
6667
AllocaInst *Alloca;

llvm/unittests/SandboxIR/UtilsTest.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,3 +287,31 @@ define void @foo(i8 %v1, ptr %ptr) {
287287
EXPECT_TRUE(Utils::isMemDepCandidate(CallBar));
288288
EXPECT_FALSE(Utils::isMemDepCandidate(Ret));
289289
}
290+
291+
TEST_F(UtilsTest, Instruction_isMemIntrinsic) {
292+
parseIR(C, R"IR(
293+
declare void @llvm.sideeffect()
294+
declare void @llvm.pseudoprobe(i64)
295+
declare void @llvm.assume(i1)
296+
297+
define void @foo(ptr %ptr, i1 %cond) {
298+
call void @llvm.sideeffect()
299+
call void @llvm.pseudoprobe(i64 42)
300+
call void @llvm.assume(i1 %cond)
301+
ret void
302+
}
303+
)IR");
304+
llvm::Function *LLVMF = &*M->getFunction("foo");
305+
sandboxir::Context Ctx(C);
306+
sandboxir::Function *F = Ctx.createFunction(LLVMF);
307+
auto *BB = &*F->begin();
308+
auto It = BB->begin();
309+
auto *SideEffect = cast<sandboxir::IntrinsicInst>(&*It++);
310+
auto *PseudoProbe = cast<sandboxir::IntrinsicInst>(&*It++);
311+
auto *OtherIntrinsic = cast<sandboxir::IntrinsicInst>(&*It++);
312+
using Utils = sandboxir::Utils;
313+
314+
EXPECT_FALSE(Utils::isMemIntrinsic(SideEffect));
315+
EXPECT_FALSE(Utils::isMemIntrinsic(PseudoProbe));
316+
EXPECT_TRUE(Utils::isMemIntrinsic(OtherIntrinsic));
317+
}

0 commit comments

Comments
 (0)