1 //===- LoopInfoTest.cpp - LoopInfo unit tests -----------------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Analysis/LoopInfo.h" 11 #include "llvm/AsmParser/Parser.h" 12 #include "llvm/IR/Dominators.h" 13 #include "llvm/Support/SourceMgr.h" 14 #include "gtest/gtest.h" 15 16 using namespace llvm; 17 18 static std::unique_ptr<Module> makeLLVMModule(LLVMContext &Context, 19 const char *ModuleStr) { 20 SMDiagnostic Err; 21 return parseAssemblyString(ModuleStr, Err, Context); 22 } 23 24 // This tests that for a loop with a single latch, we get the loop id from 25 // its only latch, even in case the loop may not be in a simplified form. 26 TEST(LoopInfoTest, LoopWithSingleLatch) { 27 const char *ModuleStr = 28 "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n" 29 "define void @foo(i32 %n) {\n" 30 "entry:\n" 31 " br i1 undef, label %for.cond, label %for.end\n" 32 "for.cond:\n" 33 " %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]\n" 34 " %cmp = icmp slt i32 %i.0, %n\n" 35 " br i1 %cmp, label %for.inc, label %for.end\n" 36 "for.inc:\n" 37 " %inc = add nsw i32 %i.0, 1\n" 38 " br label %for.cond, !llvm.loop !0\n" 39 "for.end:\n" 40 " ret void\n" 41 "}\n" 42 "!0 = distinct !{!0, !1}\n" 43 "!1 = !{!\"llvm.loop.distribute.enable\", i1 true}\n"; 44 45 // Parse the module. 46 LLVMContext Context; 47 std::unique_ptr<Module> M = makeLLVMModule(Context, ModuleStr); 48 49 // Build the dominator tree and loop info. 50 DominatorTree DT; 51 DT.recalculate(*M->begin()); 52 LoopInfo LI; 53 LI.analyze(DT); 54 55 56 Function &F = *M->begin(); 57 Function::iterator FI = F.begin(); 58 FI++; // First basic block is entry - skip it. 59 BasicBlock *Header = &*FI++; 60 assert(Header->getName() == "for.cond"); 61 Loop *L = LI.getLoopFor(Header); 62 63 // This loop is not in simplified form. 64 EXPECT_FALSE(L->isLoopSimplifyForm()); 65 66 // Analyze the loop metadata id. 67 bool loopIDFoundAndSet = false; 68 // Try to get and set the metadata id for the loop. 69 if (MDNode *D = L->getLoopID()) { 70 L->setLoopID(D); 71 loopIDFoundAndSet = true; 72 } 73 74 // We must have successfully found and set the loop id in the 75 // only latch the loop has. 76 EXPECT_TRUE(loopIDFoundAndSet); 77 } 78