diff --git a/llvm/include/llvm/SandboxIR/SandboxIR.h b/llvm/include/llvm/SandboxIR/SandboxIR.h index 24c34466b4415..4593d98b36b16 100644 --- a/llvm/include/llvm/SandboxIR/SandboxIR.h +++ b/llvm/include/llvm/SandboxIR/SandboxIR.h @@ -1558,6 +1558,14 @@ class Instruction : public sandboxir::User { void moveAfter(Instruction *After) { moveBefore(*After->getParent(), std::next(After->getIterator())); } + // TODO: This currently relies on LLVM IR Instruction::comesBefore which is + // can be linear-time. + /// Given an instruction Other in the same basic block as this instruction, + /// return true if this instruction comes before Other. + bool comesBefore(const Instruction *Other) const { + return cast(Val)->comesBefore( + cast(Other->Val)); + } /// \Returns the BasicBlock containing this Instruction, or null if it is /// detached. BasicBlock *getParent() const; diff --git a/llvm/unittests/SandboxIR/SandboxIRTest.cpp b/llvm/unittests/SandboxIR/SandboxIRTest.cpp index b1f3a6c0cf550..638edce1353c6 100644 --- a/llvm/unittests/SandboxIR/SandboxIRTest.cpp +++ b/llvm/unittests/SandboxIR/SandboxIRTest.cpp @@ -1366,6 +1366,10 @@ define void @foo(i8 %v1) { EXPECT_EQ(I0->getNextNode(), I1); EXPECT_EQ(I1->getPrevNode(), I0); + // Check comesBefore(I). + EXPECT_TRUE(I0->comesBefore(I1)); + EXPECT_FALSE(I1->comesBefore(I0)); + // Check moveBefore(BB, It). I1->moveBefore(*BB, BB->begin()); EXPECT_EQ(I1->getPrevNode(), nullptr);