[polly] r314116 - [ScopInfo] Allow uniform branch conditions

Tobias Grosser via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 25 09:37:15 PDT 2017


Author: grosser
Date: Mon Sep 25 09:37:15 2017
New Revision: 314116

URL: http://llvm.org/viewvc/llvm-project?rev=314116&view=rev
Log:
[ScopInfo] Allow uniform branch conditions

If all but one branch come from an error condition and the incoming value from
this branch is a constant, we can model this branch.

Added:
    polly/trunk/test/ScopInfo/condtion-after-error-block.ll
Modified:
    polly/trunk/include/polly/Support/SCEVValidator.h
    polly/trunk/lib/Analysis/ScopDetection.cpp
    polly/trunk/lib/Analysis/ScopInfo.cpp
    polly/trunk/lib/Support/SCEVValidator.cpp

Modified: polly/trunk/include/polly/Support/SCEVValidator.h
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/include/polly/Support/SCEVValidator.h?rev=314116&r1=314115&r2=314116&view=diff
==============================================================================
--- polly/trunk/include/polly/Support/SCEVValidator.h (original)
+++ polly/trunk/include/polly/Support/SCEVValidator.h Mon Sep 25 09:37:15 2017
@@ -105,6 +105,15 @@ const llvm::SCEV *tryForwardThroughPHI(c
                                        llvm::ScalarEvolution &SE,
                                        llvm::LoopInfo &LI,
                                        const llvm::DominatorTree &DT);
+
+/// Return a unique non-error block incoming value for @p PHI if available.
+///
+/// @param R The region to run our code on.
+/// @param LI The loopinfo tree
+/// @param DT The dominator tree
+llvm::Value *getUniqueNonErrorValue(llvm::PHINode *PHI, llvm::Region *R,
+                                    llvm::LoopInfo &LI,
+                                    const llvm::DominatorTree &DT);
 } // namespace polly
 
 #endif

Modified: polly/trunk/lib/Analysis/ScopDetection.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Analysis/ScopDetection.cpp?rev=314116&r1=314115&r2=314116&view=diff
==============================================================================
--- polly/trunk/lib/Analysis/ScopDetection.cpp (original)
+++ polly/trunk/lib/Analysis/ScopDetection.cpp Mon Sep 25 09:37:15 2017
@@ -588,6 +588,13 @@ bool ScopDetection::isValidBranch(BasicB
     }
   }
 
+  if (auto PHI = dyn_cast<PHINode>(Condition)) {
+    auto *Unique = dyn_cast_or_null<ConstantInt>(
+        getUniqueNonErrorValue(PHI, &Context.CurRegion, LI, DT));
+    if (Unique && (Unique->isZero() || Unique->isOne()))
+      return true;
+  }
+
   // Non constant conditions of branches need to be ICmpInst.
   if (!isa<ICmpInst>(Condition)) {
     if (!IsLoopBranch && AllowNonAffineSubRegions &&

Modified: polly/trunk/lib/Analysis/ScopInfo.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Analysis/ScopInfo.cpp?rev=314116&r1=314115&r2=314116&view=diff
==============================================================================
--- polly/trunk/lib/Analysis/ScopInfo.cpp (original)
+++ polly/trunk/lib/Analysis/ScopInfo.cpp Mon Sep 25 09:37:15 2017
@@ -1541,7 +1541,16 @@ bool buildConditionSets(Scop &S, BasicBl
                         DenseMap<BasicBlock *, isl::set> &InvalidDomainMap,
                         SmallVectorImpl<__isl_give isl_set *> &ConditionSets) {
   isl_set *ConsequenceCondSet = nullptr;
-  if (auto *CCond = dyn_cast<ConstantInt>(Condition)) {
+
+  if (auto *PHI = dyn_cast<PHINode>(Condition)) {
+    auto *Unique = dyn_cast<ConstantInt>(
+        getUniqueNonErrorValue(PHI, &S.getRegion(), *S.getLI(), *S.getDT()));
+
+    if (Unique->isZero())
+      ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain));
+    else
+      ConsequenceCondSet = isl_set_universe(isl_set_get_space(Domain));
+  } else if (auto *CCond = dyn_cast<ConstantInt>(Condition)) {
     if (CCond->isZero())
       ConsequenceCondSet = isl_set_empty(isl_set_get_space(Domain));
     else

Modified: polly/trunk/lib/Support/SCEVValidator.cpp
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/lib/Support/SCEVValidator.cpp?rev=314116&r1=314115&r2=314116&view=diff
==============================================================================
--- polly/trunk/lib/Support/SCEVValidator.cpp (original)
+++ polly/trunk/lib/Support/SCEVValidator.cpp Mon Sep 25 09:37:15 2017
@@ -762,4 +762,18 @@ const SCEV *tryForwardThroughPHI(const S
   return Expr;
 }
 
+Value *getUniqueNonErrorValue(PHINode *PHI, Region *R, LoopInfo &LI,
+                              const DominatorTree &DT) {
+  Value *V = nullptr;
+  for (unsigned i = 0; i < PHI->getNumIncomingValues(); i++) {
+    BasicBlock *BB = PHI->getIncomingBlock(i);
+    if (!isErrorBlock(*BB, *R, LI, DT)) {
+      if (V)
+        return nullptr;
+      V = PHI->getIncomingValue(i);
+    }
+  }
+
+  return V;
+}
 } // namespace polly

Added: polly/trunk/test/ScopInfo/condtion-after-error-block.ll
URL: http://llvm.org/viewvc/llvm-project/polly/trunk/test/ScopInfo/condtion-after-error-block.ll?rev=314116&view=auto
==============================================================================
--- polly/trunk/test/ScopInfo/condtion-after-error-block.ll (added)
+++ polly/trunk/test/ScopInfo/condtion-after-error-block.ll Mon Sep 25 09:37:15 2017
@@ -0,0 +1,68 @@
+; RUN: opt %loadPolly -polly-scops -analyze < %s | FileCheck %s
+
+; Verify that we allow scops containing uniform branch conditions, where all
+; but one incoming block comes from an error condition.
+
+; CHECK:         Statements {
+; CHECK-NEXT:     	Stmt_A
+; CHECK-NEXT:             Domain :=
+; CHECK-NEXT:                 [p] -> { Stmt_A[] };
+; CHECK-NEXT:             Schedule :=
+; CHECK-NEXT:                 [p] -> { Stmt_A[] -> [1, 0] };
+; CHECK-NEXT:             MustWriteAccess :=	[Reduction Type: NONE] [Scalar: 0]
+; CHECK-NEXT:                 [p] -> { Stmt_A[] -> MemRef_X[0] };
+; CHECK-NEXT:     	Stmt_loop
+; CHECK-NEXT:             Domain :=
+; CHECK-NEXT:                 [p] -> { Stmt_loop[i0] : p >= 13 and 0 <= i0 <= 1025 };
+; CHECK-NEXT:             Schedule :=
+; CHECK-NEXT:                 [p] -> { Stmt_loop[i0] -> [0, i0] };
+; CHECK-NEXT:             MustWriteAccess :=	[Reduction Type: NONE] [Scalar: 0]
+; CHECK-NEXT:                 [p] -> { Stmt_loop[i0] -> MemRef_X[0] };
+; CHECK-NEXT:             MustWriteAccess :=	[Reduction Type: NONE] [Scalar: 1]
+; CHECK-NEXT:                 [p] -> { Stmt_loop[i0] -> MemRef_phi__phi[] };
+; CHECK-NEXT:     }
+
+declare void @bar()
+
+define void @foo(float* %X, i64 %p) {
+entry:
+  br label %br
+
+br:
+  %cmp1 = icmp sle i64 %p, 12
+  br i1 %cmp1, label %A, label %br2
+
+br2:
+  %cmp3 = icmp sle i64 %p, 12
+  br i1 %cmp3, label %cond, label %loop
+
+loop:
+  %indvar = phi i64 [0, %br2], [%indvar.next, %loop]
+  %indvar.next = add nsw i64 %indvar, 1
+  store float 41.0, float* %X
+  %cmp2 = icmp sle i64 %indvar, 1024
+  br i1 %cmp2, label %loop, label %merge
+
+cond:
+  br label %cond2
+
+cond2:
+  call void @bar()
+  br label %merge
+
+merge:
+  %phi = phi i1 [false, %cond2], [true, %loop]
+  br i1 %phi, label %A, label %B
+
+A:
+  store float 42.0, float* %X
+  br label %exit
+
+B:
+  call void @bar()
+  store float 41.0, float* %X
+  br label %exit
+
+exit:
+  ret void
+}




More information about the llvm-commits mailing list