[PATCH] D28778: Use a test fixture for LoopInfoTest.
Xin Tong via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Mon Jan 16 12:08:32 PST 2017
trentxintong created this revision.
trentxintong added reviewers: sanjoy, hfinkel.
trentxintong added a subscriber: llvm-commits.
I am about to add some new tests to the file in a later patch, but all the
tests in the file have to all be using TEST_F or none.
So i am changing this test proactively for later commit.
Plus the later patch will use the same fixture.
https://reviews.llvm.org/D28778
Files:
unittests/Analysis/LoopInfoTest.cpp
Index: unittests/Analysis/LoopInfoTest.cpp
===================================================================
--- unittests/Analysis/LoopInfoTest.cpp
+++ unittests/Analysis/LoopInfoTest.cpp
@@ -15,15 +15,38 @@
using namespace llvm;
+class LoopInfoTest : public testing::Test {
+protected:
+ LoopInfoTest() {}
+
+ std::unique_ptr<DominatorTree> DT;
+ std::unique_ptr<LoopInfo> LI;
+
+ void initializeLoopInfo(Function &F) {
+ // Create the new dominator tree and loop info and free the old ones.
+ DT.reset(new DominatorTree(F));
+ LI.reset(new LoopInfo(*DT));
+ }
+
+ /// Build the loop info for the function and run the Test.
+ void runWithLoopInfo(Module &M, StringRef FuncName,
+ function_ref<void(Function &F, LoopInfo &LI)> Test) {
+ auto *F = M.getFunction(FuncName);
+ ASSERT_NE(F, nullptr) << "Could not find " << FuncName;
+ initializeLoopInfo(*F);
+ Test(*F, *LI);
+ }
+};
+
static std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context,
const char *ModuleStr) {
SMDiagnostic Err;
return parseAssemblyString(ModuleStr, Err, Context);
}
// This tests that for a loop with a single latch, we get the loop id from
// its only latch, even in case the loop may not be in a simplified form.
-TEST(LoopInfoTest, LoopWithSingleLatch) {
+TEST_F(LoopInfoTest, LoopWithSingleLatch) {
const char *ModuleStr =
"target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
"define void @foo(i32 %n) {\n"
@@ -46,31 +69,26 @@
LLVMContext Context;
std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr);
- // Build the dominator tree and loop info.
- DominatorTree DT;
- DT.recalculate(*M->begin());
- LoopInfo LI;
- LI.analyze(DT);
+ runWithLoopInfo(*M, "foo", [&](Function &F, LoopInfo &LI) {
+ Function::iterator FI = F.begin();
+ // First basic block is entry - skip it.
+ BasicBlock *Header = &*(++FI);
+ assert(Header->getName() == "for.cond");
+ Loop *L = LI.getLoopFor(Header);
- Function &F = *M->begin();
- Function::iterator FI = F.begin();
- FI++; // First basic block is entry - skip it.
- BasicBlock *Header = &*FI++;
- assert(Header->getName() == "for.cond");
- Loop *L = LI.getLoopFor(Header);
+ // This loop is not in simplified form.
+ EXPECT_FALSE(L->isLoopSimplifyForm());
- // This loop is not in simplified form.
- EXPECT_FALSE(L->isLoopSimplifyForm());
-
- // Analyze the loop metadata id.
- bool loopIDFoundAndSet = false;
- // Try to get and set the metadata id for the loop.
- if (MDNode *D = L->getLoopID()) {
- L->setLoopID(D);
- loopIDFoundAndSet = true;
- }
+ // Analyze the loop metadata id.
+ bool loopIDFoundAndSet = false;
+ // Try to get and set the metadata id for the loop.
+ if (MDNode *D = L->getLoopID()) {
+ L->setLoopID(D);
+ loopIDFoundAndSet = true;
+ }
- // We must have successfully found and set the loop id in the
- // only latch the loop has.
- EXPECT_TRUE(loopIDFoundAndSet);
+ // We must have successfully found and set the loop id in the
+ // only latch the loop has.
+ EXPECT_TRUE(loopIDFoundAndSet);
+ });
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D28778.84582.patch
Type: text/x-patch
Size: 3194 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170116/200a98d2/attachment.bin>
More information about the llvm-commits
mailing list