[SCEVValidator] Fix loop exit values considered affine.
authorMichael Kruse <llvm@meinersbur.de>
Thu, 3 Mar 2016 22:10:52 +0000 (22:10 +0000)
committerMichael Kruse <llvm@meinersbur.de>
Thu, 3 Mar 2016 22:10:52 +0000 (22:10 +0000)
Index calculations can use the last value that come out of a loop.
Ideally, ScalarEvolution can compute that exit value directly without
depending on the loop induction variable, but not in all cases.

This changes isAffine to not consider such loop exit values as affine to
avoid that SCEVExpander adds uses of the original loop induction
variable.

This fix is analogous to r262404 that applies to general uses of loop
exit values instead of index expressions and loop bouds as in this
patch.

This reduces the number of LNT test-suite fails with
-polly-position=before-vectorizer -polly-unprofitable
from 10 to 8.

llvm-svn: 262665

polly/lib/Support/SCEVValidator.cpp
polly/test/ScopDetect/index_from_unpredictable_loop.ll [new file with mode: 0644]

index 2413990..0bece55 100644 (file)
@@ -267,7 +267,14 @@ public:
     if (!Recurrence.isValid())
       return Recurrence;
 
-    if (R->contains(Expr->getLoop())) {
+    auto *L = Expr->getLoop();
+    if (R->contains(L) && (!Scope || !L->contains(Scope))) {
+      DEBUG(dbgs() << "INVALID: AddRec out of a loop whose exit value is not "
+                      "synthesizable");
+      return ValidatorResult(SCEVType::INVALID);
+    }
+
+    if (R->contains(L)) {
       if (Recurrence.isINT()) {
         ValidatorResult Result(SCEVType::IV);
         Result.addParamsFrom(Start);
diff --git a/polly/test/ScopDetect/index_from_unpredictable_loop.ll b/polly/test/ScopDetect/index_from_unpredictable_loop.ll
new file mode 100644 (file)
index 0000000..fad5939
--- /dev/null
@@ -0,0 +1,41 @@
+; RUN: opt %loadPolly -polly-scops -analyze < %s | FileCheck %s --check-prefix=AFFINE
+; RUN: opt %loadPolly -polly-scops -polly-allow-nonaffine -analyze < %s | FileCheck %s --check-prefix=NONAFFINE
+
+; The loop for.body => for.inc has an unpredictable iteration count could due to
+; the undef start value that it is compared to. Therefore the array element
+; %arrayidx101 that depends on that exit value cannot be affine.
+; Derived from test-suite/MultiSource/Benchmarks/BitBench/uuencode/uuencode.c
+
+define void @encode_line(i8* nocapture readonly %input, i32 %octets) {
+entry:
+  br i1 undef, label %for.body, label %for.end
+
+for.body:
+  %indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 0, %entry ]
+  %octets.addr.02 = phi i32 [ undef, %for.inc ], [ %octets, %entry ]
+  br i1 false, label %for.inc, label %if.else
+
+if.else:
+  %cond = icmp eq i32 %octets.addr.02, 2
+  br i1 %cond, label %if.then84, label %for.end
+
+if.then84:
+  %0 = add nsw i64 %indvars.iv, 1
+  %arrayidx101 = getelementptr inbounds i8, i8* %input, i64 %0
+  %1 = load i8, i8* %arrayidx101, align 1
+  br label %for.end
+
+for.inc:
+  %cmp = icmp sgt i32 %octets.addr.02, 3
+  %indvars.iv.next = add nsw i64 %indvars.iv, 3
+  br i1 %cmp, label %for.body, label %for.end
+
+for.end:
+  ret void
+}
+
+
+; AFFINE-NOT: Function: encode_line
+
+; NONAFFINE:      Function: encode_line
+; NONAFFINE-NEXT: Region: %for.body---%for.end