diff --git a/lib/Analysis/DependenceAnalysis.cpp b/lib/Analysis/DependenceAnalysis.cpp index f11cce38406..9564cfb2aa4 100644 --- a/lib/Analysis/DependenceAnalysis.cpp +++ b/lib/Analysis/DependenceAnalysis.cpp @@ -3553,6 +3553,16 @@ DependenceInfo::depends(Instruction *Src, Instruction *Dst, const SCEV *DstSCEV = SE->getSCEV(DstPtr); LLVM_DEBUG(dbgs() << " SrcSCEV = " << *SrcSCEV << "\n"); LLVM_DEBUG(dbgs() << " DstSCEV = " << *DstSCEV << "\n"); + if (SE->getPointerBase(SrcSCEV) != SE->getPointerBase(DstSCEV)) { + // If two pointers have different bases, trying to analyze indexes won't + // work; we can't compare them to each other. This can happen, for example, + // if one is produced by an LCSSA PHI node. + // + // We check this upfront so we don't crash in cases where getMinusSCEV() + // returns a SCEVCouldNotCompute. + LLVM_DEBUG(dbgs() << "can't analyze SCEV with different pointer base\n"); + return std::make_unique(Src, Dst); + } Pair[0].Src = SrcSCEV; Pair[0].Dst = DstSCEV; diff --git a/test/Analysis/DependenceAnalysis/lcssa.ll b/test/Analysis/DependenceAnalysis/lcssa.ll new file mode 100644 index 00000000000..a42295cbe30 --- /dev/null +++ b/test/Analysis/DependenceAnalysis/lcssa.ll @@ -0,0 +1,38 @@ +; RUN: opt < %s -disable-output "-passes=print" \ +; RUN: "-aa-pipeline=basic-aa,tbaa" 2>&1 | FileCheck %s +; RUN: opt < %s -analyze -enable-new-pm=0 -basic-aa -tbaa -da | FileCheck %s + +; CHECK: Src: %v = load i32, i32* %arrayidx1, align 4 --> Dst: store i32 %add, i32* %a.lcssa, align 4 +; CHECK-NEXT: da analyze - confused! + +define void @f(i32 *%a, i32 %n, i64 %n2) { +entry: + br label %while.body + +while.body: + %n.addr = phi i32 [ %mul, %while.body ], [ %n, %entry ] + %inc.phi = phi i32 [ %inc, %while.body ], [ 0, %entry ] + %mul = mul i32 %n.addr, 3 + %div = udiv i32 %mul, 2 + %inc = add i32 %inc.phi, 1 + %incdec.ptr = getelementptr inbounds i32, i32* %a, i32 %inc + %cmp.not = icmp eq i32 %div, 1 + br i1 %cmp.not, label %while.end, label %while.body + +while.end: + %a.lcssa = phi i32* [ %incdec.ptr, %while.body ] + br label %for.body + +for.body: + %indvars.iv = phi i64 [ 1, %while.end ], [ %indvars.iv.next, %for.body ] + %arrayidx1 = getelementptr inbounds i32, i32* %a, i64 %indvars.iv + %v = load i32, i32* %arrayidx1, align 4 + %add = add nsw i32 %v, 1 + store i32 %add, i32* %a.lcssa, align 4 + %indvars.iv.next = add nuw nsw i64 %indvars.iv, 1 + %exitcond.not = icmp eq i64 %indvars.iv.next, %n2 + br i1 %exitcond.not, label %ret, label %for.body + +ret: + ret void +}