diff --git a/polly/lib/Analysis/ScopInfo.cpp b/polly/lib/Analysis/ScopInfo.cpp index 00da152b92ad..73b74c884999 100644 --- a/polly/lib/Analysis/ScopInfo.cpp +++ b/polly/lib/Analysis/ScopInfo.cpp @@ -1663,6 +1663,15 @@ getDomainForBlock(BasicBlock *BB, DenseMap &DomainMap, return getDomainForBlock(R->getEntry(), DomainMap, RI); } +static bool containsErrorBlock(RegionNode *RN) { + if (!RN->isSubRegion()) + return isErrorBlock(*RN->getNodeAs()); + for (BasicBlock *BB : RN->getNodeAs()->blocks()) + if (isErrorBlock(*BB)) + return true; + return false; +} + void Scop::propagateDomainConstraints(Region *R, LoopInfo &LI, ScopDetection &SD, DominatorTree &DT) { // Iterate over the region R and propagate the domain constrains from the @@ -1751,7 +1760,7 @@ void Scop::propagateDomainConstraints(Region *R, LoopInfo &LI, Domain = isl_set_intersect(Domain, PredDom); // Add assumptions for error blocks. - if (isErrorBlock(*BB)) { + if (containsErrorBlock(RN)) { IsOptimized = true; isl_set *DomPar = isl_set_params(isl_set_copy(Domain)); addAssumption(isl_set_complement(DomPar)); diff --git a/polly/lib/CodeGen/BlockGenerators.cpp b/polly/lib/CodeGen/BlockGenerators.cpp index d0dddb18301b..d7788c11ea7c 100644 --- a/polly/lib/CodeGen/BlockGenerators.cpp +++ b/polly/lib/CodeGen/BlockGenerators.cpp @@ -1032,9 +1032,17 @@ void RegionGenerator::copyStmt(ScopStmt &Stmt, LoopToScevMapT <S, // region control flow by hand after all blocks have been copied. for (BasicBlock *BB : SeenBlocks) { - BranchInst *BI = cast(BB->getTerminator()); - BasicBlock *BBCopy = BlockMap[BB]; + TerminatorInst *TI = BB->getTerminator(); + if (isa(TI)) { + while (!BBCopy->empty()) + BBCopy->begin()->eraseFromParent(); + new UnreachableInst(BBCopy->getContext(), BBCopy); + continue; + } + + BranchInst *BI = cast(TI); + Instruction *BICopy = BBCopy->getTerminator(); ValueMapT &RegionMap = RegionMaps[BBCopy]; diff --git a/polly/test/Isl/CodeGen/error-stmt-in-non-affine-region.ll b/polly/test/Isl/CodeGen/error-stmt-in-non-affine-region.ll new file mode 100644 index 000000000000..20973ad84769 --- /dev/null +++ b/polly/test/Isl/CodeGen/error-stmt-in-non-affine-region.ll @@ -0,0 +1,58 @@ +; RUN: opt %loadPolly -S -polly-codegen -polly-detect-unprofitable -polly-no-early-exit < %s | FileCheck %s +; +; CHECK-LABEL: polly.stmt.if.then: +; CHECK-NEXT: unreachable +; +; void f(int *A, int N) { +; for (int i = 0; i < 1024; i++) +; if (i == N) { +; if (A[i]) +; abort(); +; else +; A[i] = i; +; } +; } +; +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +define void @f(i32* %A, i64 %N) { +entry: + br label %for.cond + +for.cond: ; preds = %for.inc, %entry + %indvars.iv = phi i64 [ %indvars.iv.next, %for.inc ], [ 0, %entry ] + %cmp = icmp slt i64 %indvars.iv, 1024 + br i1 %cmp, label %for.body, label %for.end + +for.body: ; preds = %for.cond + %arrayidx = getelementptr inbounds i32, i32* %A, i64 %indvars.iv + %tmp = load i32, i32* %arrayidx, align 4 + %cmp.outer = icmp eq i64 %indvars.iv, %N + br i1 %cmp.outer, label %if.then.outer, label %for.inc + +if.then.outer: + %tobool = icmp eq i32 %tmp, 0 + br i1 %tobool, label %if.else, label %if.then + +if.then: ; preds = %for.body + call void @abort() + unreachable + +if.else: ; preds = %for.body + %arrayidx2 = getelementptr inbounds i32, i32* %A, i64 %indvars.iv + %tmp1 = trunc i64 %indvars.iv to i32 + store i32 %tmp1, i32* %arrayidx2, align 4 + br label %if.end + +if.end: ; preds = %if.else + br label %for.inc + +for.inc: ; preds = %if.end + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + br label %for.cond + +for.end: ; preds = %for.cond + ret void +} + +declare void @abort()