[llvm] r292078 - Use getLoopLatch in place of isLoopSimplifyForm

Xin Tong via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 15 13:17:52 PST 2017


Author: trentxintong
Date: Sun Jan 15 15:17:52 2017
New Revision: 292078

URL: http://llvm.org/viewvc/llvm-project?rev=292078&view=rev
Log:
Use getLoopLatch in place of isLoopSimplifyForm

Summary:
Use getLoopLatch in place of isLoopSimplifyForm. we do not need
to know whether the loop has a preheader nor dedicated exits.

Reviewers: hfinkel, sanjoy, atrick, mkuper

Subscribers: mzolotukhin, llvm-commits

Differential Revision: https://reviews.llvm.org/D28724

Added:
    llvm/trunk/unittests/Analysis/LoopInfoTest.cpp
Modified:
    llvm/trunk/lib/Analysis/LoopInfo.cpp
    llvm/trunk/unittests/Analysis/CMakeLists.txt

Modified: llvm/trunk/lib/Analysis/LoopInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/LoopInfo.cpp?rev=292078&r1=292077&r2=292078&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/LoopInfo.cpp (original)
+++ llvm/trunk/lib/Analysis/LoopInfo.cpp Sun Jan 15 15:17:52 2017
@@ -211,9 +211,11 @@ bool Loop::isSafeToClone() const {
 
 MDNode *Loop::getLoopID() const {
   MDNode *LoopID = nullptr;
-  if (isLoopSimplifyForm()) {
-    LoopID = getLoopLatch()->getTerminator()->getMetadata(LLVMContext::MD_loop);
+  if (BasicBlock *Latch = getLoopLatch()) {
+    LoopID = Latch->getTerminator()->getMetadata(LLVMContext::MD_loop);
   } else {
+    assert(!getLoopLatch() &&
+           "The loop should have no single latch at this point");
     // Go through each predecessor of the loop header and check the
     // terminator for the metadata.
     BasicBlock *H = getHeader();
@@ -248,11 +250,12 @@ void Loop::setLoopID(MDNode *LoopID) con
   assert(LoopID->getNumOperands() > 0 && "Loop ID needs at least one operand");
   assert(LoopID->getOperand(0) == LoopID && "Loop ID should refer to itself");
 
-  if (isLoopSimplifyForm()) {
-    getLoopLatch()->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
+  if (BasicBlock *Latch = getLoopLatch()) {
+    Latch->getTerminator()->setMetadata(LLVMContext::MD_loop, LoopID);
     return;
   }
 
+  assert(!getLoopLatch() && "The loop should have no single latch at this point");
   BasicBlock *H = getHeader();
   for (BasicBlock *BB : this->blocks()) {
     TerminatorInst *TI = BB->getTerminator();

Modified: llvm/trunk/unittests/Analysis/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Analysis/CMakeLists.txt?rev=292078&r1=292077&r2=292078&view=diff
==============================================================================
--- llvm/trunk/unittests/Analysis/CMakeLists.txt (original)
+++ llvm/trunk/unittests/Analysis/CMakeLists.txt Sun Jan 15 15:17:52 2017
@@ -13,6 +13,7 @@ add_llvm_unittest(AnalysisTests
   CFGTest.cpp
   CGSCCPassManagerTest.cpp
   LazyCallGraphTest.cpp
+  LoopInfoTest.cpp
   MemoryBuiltinsTest.cpp
   ProfileSummaryInfoTest.cpp
   ScalarEvolutionTest.cpp

Added: llvm/trunk/unittests/Analysis/LoopInfoTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Analysis/LoopInfoTest.cpp?rev=292078&view=auto
==============================================================================
--- llvm/trunk/unittests/Analysis/LoopInfoTest.cpp (added)
+++ llvm/trunk/unittests/Analysis/LoopInfoTest.cpp Sun Jan 15 15:17:52 2017
@@ -0,0 +1,77 @@
+//===- LoopInfoTest.cpp - LoopInfo unit tests -----------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+#include "llvm/Analysis/LoopInfo.h"
+#include "llvm/AsmParser/Parser.h"
+#include "llvm/IR/Dominators.h"
+#include "llvm/Support/SourceMgr.h"
+#include "gtest/gtest.h"
+
+using namespace llvm;
+
+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) {
+  const char *ModuleStr =
+         "target datalayout = \"e-m:o-i64:64-f80:128-n8:16:32:64-S128\"\n"
+         "define void @foo(i32 %n) {\n"
+         "entry:\n"
+         "  br i1 undef, label %for.cond, label %for.end\n"
+         "for.cond:\n"
+         "  %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.inc ]\n"
+         "  %cmp = icmp slt i32 %i.0, %n\n"
+         "  br i1 %cmp, label %for.inc, label %for.end\n"
+         "for.inc:\n"
+         "  %inc = add nsw i32 %i.0, 1\n"
+         "  br label %for.cond, !llvm.loop !0\n"
+         "for.end:\n"
+         "  ret void\n"
+         "}\n"
+         "!0 = distinct !{!0, !1}\n"
+         "!1 = !{!\"llvm.loop.distribute.enable\", i1 true}\n";
+  
+  // Parse the module.
+  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);
+
+
+  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());
+
+  // 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);
+}




More information about the llvm-commits mailing list