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