[llvm] r290116 - [LoopVersioning] Require loop-simplify form for loop versioning.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 19 09:13:37 PST 2016


Author: fhahn
Date: Mon Dec 19 11:13:37 2016
New Revision: 290116

URL: http://llvm.org/viewvc/llvm-project?rev=290116&view=rev
Log:
 [LoopVersioning] Require loop-simplify form for loop versioning.

Summary:
Requiring loop-simplify form for loop versioning ensures that the
runtime check block always dominates the exit block.
    
This patch closes #30958 (https://llvm.org/bugs/show_bug.cgi?id=30958).

Reviewers: silviu.baranga, hfinkel, anemet, ashutosh.nema

Subscribers: ashutosh.nema, mzolotukhin, efriedma, hfinkel, llvm-commits

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

Added:
    llvm/trunk/test/Transforms/LoopVersioning/exit-block-dominates-rt-check-block.ll
Modified:
    llvm/trunk/lib/Transforms/Scalar/LoopDistribute.cpp
    llvm/trunk/lib/Transforms/Scalar/LoopLoadElimination.cpp
    llvm/trunk/lib/Transforms/Scalar/LoopVersioningLICM.cpp
    llvm/trunk/lib/Transforms/Utils/LoopVersioning.cpp
    llvm/trunk/test/Transforms/LoopDistribute/diagnostics-with-hotness.ll
    llvm/trunk/test/Transforms/LoopDistribute/diagnostics.ll
    llvm/trunk/test/Transforms/LoopVectorize/diag-with-hotness-info.ll
    llvm/trunk/test/Transforms/LoopVersioning/noalias-version-twice.ll

Modified: llvm/trunk/lib/Transforms/Scalar/LoopDistribute.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopDistribute.cpp?rev=290116&r1=290115&r2=290116&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopDistribute.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopDistribute.cpp Mon Dec 19 11:13:37 2016
@@ -606,11 +606,13 @@ public:
     DEBUG(dbgs() << "\nLDist: In \"" << L->getHeader()->getParent()->getName()
                  << "\" checking " << *L << "\n");
 
-    BasicBlock *PH = L->getLoopPreheader();
-    if (!PH)
-      return fail("NoHeader", "no preheader");
     if (!L->getExitBlock())
       return fail("MultipleExitBlocks", "multiple exit blocks");
+    if (!L->isLoopSimplifyForm())
+      return fail("NotLoopSimplifyForm",
+                  "loop is not in loop-simplify form");
+
+    BasicBlock *PH = L->getLoopPreheader();
 
     // LAA will check that we only have a single exiting block.
     LAI = &GetLAA(*L);

Modified: llvm/trunk/lib/Transforms/Scalar/LoopLoadElimination.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopLoadElimination.cpp?rev=290116&r1=290115&r2=290116&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopLoadElimination.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopLoadElimination.cpp Mon Dec 19 11:13:37 2016
@@ -517,6 +517,11 @@ public:
         return false;
       }
 
+      if (!L->isLoopSimplifyForm()) {
+        DEBUG(dbgs() << "Loop is not is loop-simplify form");
+        return false;
+      }
+
       // Point of no-return, start the transformation.  First, version the loop
       // if necessary.
 

Modified: llvm/trunk/lib/Transforms/Scalar/LoopVersioningLICM.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/LoopVersioningLICM.cpp?rev=290116&r1=290115&r2=290116&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/LoopVersioningLICM.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/LoopVersioningLICM.cpp Mon Dec 19 11:13:37 2016
@@ -218,9 +218,10 @@ private:
 
 /// \brief Check loop structure and confirms it's good for LoopVersioningLICM.
 bool LoopVersioningLICM::legalLoopStructure() {
-  // Loop must have a preheader, if not return false.
-  if (!CurLoop->getLoopPreheader()) {
-    DEBUG(dbgs() << "    loop preheader is missing\n");
+  // Loop must be in loop simplify form.
+  if (!CurLoop->isLoopSimplifyForm()) {
+    DEBUG(
+        dbgs() << "    loop is not in loop-simplify form.\n");
     return false;
   }
   // Loop should be innermost loop, if not return false.
@@ -256,11 +257,6 @@ bool LoopVersioningLICM::legalLoopStruct
     DEBUG(dbgs() << "    loop depth is more then threshold\n");
     return false;
   }
-  // Loop should have a dedicated exit block, if not return false.
-  if (!CurLoop->hasDedicatedExits()) {
-    DEBUG(dbgs() << "    loop does not has dedicated exit blocks\n");
-    return false;
-  }
   // We need to be able to compute the loop trip count in order
   // to generate the bound checks.
   const SCEV *ExitCount = SE->getBackedgeTakenCount(CurLoop);

Modified: llvm/trunk/lib/Transforms/Utils/LoopVersioning.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/LoopVersioning.cpp?rev=290116&r1=290115&r2=290116&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/LoopVersioning.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/LoopVersioning.cpp Mon Dec 19 11:13:37 2016
@@ -36,7 +36,7 @@ LoopVersioning::LoopVersioning(const Loo
     : VersionedLoop(L), NonVersionedLoop(nullptr), LAI(LAI), LI(LI), DT(DT),
       SE(SE) {
   assert(L->getExitBlock() && "No single exit block");
-  assert(L->getLoopPreheader() && "No preheader");
+  assert(L->isLoopSimplifyForm() && "Loop is not in loop-simplify form");
   if (UseLAIChecks) {
     setAliasChecks(LAI.getRuntimePointerChecking()->getChecks());
     setSCEVChecks(LAI.getPSE().getUnionPredicate());
@@ -278,8 +278,8 @@ public:
     bool Changed = false;
     for (Loop *L : Worklist) {
       const LoopAccessInfo &LAI = LAA->getInfo(L);
-      if (LAI.getNumRuntimePointerChecks() ||
-          !LAI.getPSE().getUnionPredicate().isAlwaysTrue()) {
+      if (L->isLoopSimplifyForm() && (LAI.getNumRuntimePointerChecks() ||
+          !LAI.getPSE().getUnionPredicate().isAlwaysTrue())) {
         LoopVersioning LVer(LAI, L, LI, DT, SE);
         LVer.versionLoop();
         LVer.annotateLoopWithNoAlias();

Modified: llvm/trunk/test/Transforms/LoopDistribute/diagnostics-with-hotness.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopDistribute/diagnostics-with-hotness.ll?rev=290116&r1=290115&r2=290116&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/LoopDistribute/diagnostics-with-hotness.ll (original)
+++ llvm/trunk/test/Transforms/LoopDistribute/diagnostics-with-hotness.ll Mon Dec 19 11:13:37 2016
@@ -1,14 +1,14 @@
-; RUN: opt -loop-distribute -S -pass-remarks-missed=loop-distribute \
+; RUN: opt -loop-simplify -loop-distribute -S -pass-remarks-missed=loop-distribute \
 ; RUN:     -pass-remarks-analysis=loop-distribute \
 ; RUN:     -pass-remarks-with-hotness < %s 2>&1 | FileCheck %s --check-prefix=HOTNESS
-; RUN: opt -loop-distribute -S -pass-remarks-missed=loop-distribute \
+; RUN: opt -loop-simplify -loop-distribute -S -pass-remarks-missed=loop-distribute \
 ; RUN:     -pass-remarks-analysis=loop-distribute \
 ; RUN:                                < %s 2>&1 | FileCheck %s --check-prefix=NO_HOTNESS
 
-; RUN: opt -passes='require<aa>,loop-distribute' -S -pass-remarks-missed=loop-distribute \
+; RUN: opt -passes='loop-simplify,require<aa>,loop-distribute' -S -pass-remarks-missed=loop-distribute \
 ; RUN:     -pass-remarks-analysis=loop-distribute \
 ; RUN:     -pass-remarks-with-hotness < %s 2>&1 | FileCheck %s --check-prefix=HOTNESS
-; RUN: opt -passes='require<aa>,loop-distribute' -S -pass-remarks-missed=loop-distribute \
+; RUN: opt -passes='loop-simplify,require<aa>,loop-distribute' -S -pass-remarks-missed=loop-distribute \
 ; RUN:     -pass-remarks-analysis=loop-distribute \
 ; RUN:                                < %s 2>&1 | FileCheck %s --check-prefix=NO_HOTNESS
 

Modified: llvm/trunk/test/Transforms/LoopDistribute/diagnostics.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopDistribute/diagnostics.ll?rev=290116&r1=290115&r2=290116&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/LoopDistribute/diagnostics.ll (original)
+++ llvm/trunk/test/Transforms/LoopDistribute/diagnostics.ll Mon Dec 19 11:13:37 2016
@@ -1,10 +1,13 @@
-; RUN: opt -loop-distribute -S < %s 2>&1 \
+; RUN: opt -loop-simplify -loop-distribute -S < %s 2>&1 \
 ; RUN:     | FileCheck %s --check-prefix=ALWAYS --check-prefix=NO_REMARKS
-; RUN: opt -loop-distribute -S -pass-remarks-missed=loop-distribute < %s 2>&1 \
+; RUN: opt -loop-simplify -loop-distribute -S \
+; RUN:     -pass-remarks-missed=loop-distribute < %s 2>&1 \
 ; RUN:     | FileCheck %s --check-prefix=ALWAYS --check-prefix=MISSED_REMARKS
-; RUN: opt -loop-distribute -S -pass-remarks-analysis=loop-distribute < %s 2>&1 \
+; RUN: opt -loop-simplify -loop-distribute -S \
+; RUN:     -pass-remarks-analysis=loop-distribute < %s 2>&1 \
 ; RUN:     | FileCheck %s --check-prefix=ALWAYS --check-prefix=ANALYSIS_REMARKS
-; RUN: opt -loop-distribute -S -pass-remarks=loop-distribute < %s 2>&1 \
+; RUN: opt -loop-simplify -loop-distribute -S \
+; RUN:     -pass-remarks=loop-distribute < %s 2>&1 \
 ; RUN:     | FileCheck %s --check-prefix=ALWAYS --check-prefix=REMARKS
 
 ; This is the input program:

Modified: llvm/trunk/test/Transforms/LoopVectorize/diag-with-hotness-info.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopVectorize/diag-with-hotness-info.ll?rev=290116&r1=290115&r2=290116&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/LoopVectorize/diag-with-hotness-info.ll (original)
+++ llvm/trunk/test/Transforms/LoopVectorize/diag-with-hotness-info.ll Mon Dec 19 11:13:37 2016
@@ -10,7 +10,8 @@
 ; RUN:     -pass-remarks-with-hotness < %s 2>&1 | \
 ; RUN:     FileCheck -check-prefix=HOTNESS -check-prefix=BOTH %s
 
-; RUN: opt -S -passes=loop-vectorize -pass-remarks-missed=loop-vectorize < %s 2>&1 | \
+; RUN: opt -S -passes=loop-vectorize \
+; RUN:     -pass-remarks-missed=loop-vectorize < %s 2>&1 | \
 ; RUN:     FileCheck -check-prefix=NO_HOTNESS -check-prefix=BOTH %s
 
 

Added: llvm/trunk/test/Transforms/LoopVersioning/exit-block-dominates-rt-check-block.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopVersioning/exit-block-dominates-rt-check-block.ll?rev=290116&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/LoopVersioning/exit-block-dominates-rt-check-block.ll (added)
+++ llvm/trunk/test/Transforms/LoopVersioning/exit-block-dominates-rt-check-block.ll Mon Dec 19 11:13:37 2016
@@ -0,0 +1,38 @@
+; This test ensures loop versioning does not produce an invalid dominator tree
+; if the exit block of the loop (bb0) dominates the runtime check block
+; (bb1 will become the runtime check block).
+
+; RUN: opt -loop-distribute -verify-dom-info -S -o - %s > %t
+; RUN: opt -loop-simplify -loop-distribute -verify-dom-info -S -o - %s > %t
+; RUN: FileCheck --check-prefix CHECK-VERSIONING -input-file %t %s
+
+; RUN: opt -loop-versioning -verify-dom-info -S -o - %s > %t
+; RUN: opt -loop-simplify -loop-versioning -verify-dom-info -S -o - %s > %t
+; RUN: FileCheck --check-prefix CHECK-VERSIONING -input-file %t %s
+
+ at c1 = external global i16
+
+define void @f(i16 %a) {
+  br label %bb0
+
+bb0:
+  br label %bb1
+
+bb1:
+  %tmp1 = load i16, i16* @c1
+  br label %bb2
+
+bb2:
+  %tmp2 = phi i16 [ %tmp1, %bb1 ], [ %tmp3, %bb2 ]
+  %tmp4 = getelementptr inbounds [1 x i32], [1 x i32]* undef, i32 0, i32 4
+  store i32 1, i32* %tmp4
+  %tmp5 = getelementptr inbounds [1 x i32], [1 x i32]* undef, i32 0, i32 9
+  store i32 0, i32* %tmp5
+  %tmp3 = add i16 %tmp2, 1
+  store i16 %tmp2, i16* @c1
+  %tmp6 = icmp sle i16 %tmp3, 0
+  br i1 %tmp6, label %bb2, label %bb0
+}
+
+; Simple check to make sure loop versioning happened.
+; CHECK-VERSIONING: bb2.lver.check:

Modified: llvm/trunk/test/Transforms/LoopVersioning/noalias-version-twice.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/LoopVersioning/noalias-version-twice.ll?rev=290116&r1=290115&r2=290116&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/LoopVersioning/noalias-version-twice.ll (original)
+++ llvm/trunk/test/Transforms/LoopVersioning/noalias-version-twice.ll Mon Dec 19 11:13:37 2016
@@ -1,4 +1,5 @@
-; RUN: opt -basicaa -loop-distribute -scoped-noalias -loop-versioning -S < %s | FileCheck %s
+; RUN: opt -basicaa -loop-distribute -loop-simplify -scoped-noalias \
+; RUN:     -loop-versioning -S < %s | FileCheck %s
 
 ; Test the metadata generated when versioning an already versioned loop.  Here
 ; we invoke loop distribution to perform the first round of versioning.  It




More information about the llvm-commits mailing list