1 //===- IntervalTest.cpp ---------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/Transforms/Vectorize/SandboxVectorizer/Interval.h" 10 #include "llvm/AsmParser/Parser.h" 11 #include "llvm/SandboxIR/Instruction.h" 12 #include "llvm/Support/SourceMgr.h" 13 #include "gtest/gtest.h" 14 15 using namespace llvm; 16 17 struct IntervalTest : public testing::Test { 18 LLVMContext C; 19 std::unique_ptr<Module> M; 20 21 void parseIR(LLVMContext &C, const char *IR) { 22 SMDiagnostic Err; 23 M = parseAssemblyString(IR, Err, C); 24 if (!M) 25 Err.print("InstrIntervalTest", errs()); 26 } 27 }; 28 29 TEST_F(IntervalTest, Basic) { 30 parseIR(C, R"IR( 31 define void @foo(i8 %v0) { 32 %add0 = add i8 %v0, %v0 33 %add1 = add i8 %v0, %v0 34 %add2 = add i8 %v0, %v0 35 ret void 36 } 37 )IR"); 38 Function &LLVMF = *M->getFunction("foo"); 39 sandboxir::Context Ctx(C); 40 auto &F = *Ctx.createFunction(&LLVMF); 41 auto *BB = &*F.begin(); 42 auto It = BB->begin(); 43 auto *I0 = &*It++; 44 auto *I1 = &*It++; 45 auto *I2 = &*It++; 46 auto *Ret = &*It++; 47 48 sandboxir::Interval<sandboxir::Instruction> Intvl(I0, Ret); 49 #ifndef NDEBUG 50 EXPECT_DEATH(sandboxir::Interval<sandboxir::Instruction>(I1, I0), 51 ".*before.*"); 52 #endif // NDEBUG 53 // Check Interval<sandboxir::Instruction>(ArrayRef), from(), to(). 54 { 55 sandboxir::Interval<sandboxir::Instruction> Intvl( 56 SmallVector<sandboxir::Instruction *>({I0, Ret})); 57 EXPECT_EQ(Intvl.top(), I0); 58 EXPECT_EQ(Intvl.bottom(), Ret); 59 } 60 { 61 sandboxir::Interval<sandboxir::Instruction> Intvl( 62 SmallVector<sandboxir::Instruction *>({Ret, I0})); 63 EXPECT_EQ(Intvl.top(), I0); 64 EXPECT_EQ(Intvl.bottom(), Ret); 65 } 66 { 67 sandboxir::Interval<sandboxir::Instruction> Intvl( 68 SmallVector<sandboxir::Instruction *>({I0, I0})); 69 EXPECT_EQ(Intvl.top(), I0); 70 EXPECT_EQ(Intvl.bottom(), I0); 71 } 72 73 // Check empty(). 74 EXPECT_FALSE(Intvl.empty()); 75 sandboxir::Interval<sandboxir::Instruction> Empty; 76 EXPECT_TRUE(Empty.empty()); 77 sandboxir::Interval<sandboxir::Instruction> One(I0, I0); 78 EXPECT_FALSE(One.empty()); 79 // Check contains(). 80 for (auto &I : *BB) { 81 EXPECT_TRUE(Intvl.contains(&I)); 82 EXPECT_FALSE(Empty.contains(&I)); 83 } 84 EXPECT_FALSE(One.contains(I1)); 85 EXPECT_FALSE(One.contains(I2)); 86 EXPECT_FALSE(One.contains(Ret)); 87 // Check iterator. 88 auto BBIt = BB->begin(); 89 for (auto &I : Intvl) 90 EXPECT_EQ(&I, &*BBIt++); 91 } 92