Skip to content

Commit 656be5a

Browse files
committed
[DA] Check for overflow in strong SIV test
1 parent 351a064 commit 656be5a

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

llvm/lib/Analysis/DependenceAnalysis.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,6 +1587,15 @@ static const SCEV *minusSCEVNoSignedOverflow(const SCEV *A, const SCEV *B,
15871587
return nullptr;
15881588
}
15891589

1590+
/// Returns \p A * \p B if it guaranteed not to signed wrap. Otherwise returns
1591+
/// nullptr. \p A and \p B must have the same integer type.
1592+
static const SCEV *mulSCEVNoSignedOverflow(const SCEV *A, const SCEV *B,
1593+
ScalarEvolution &SE) {
1594+
if (SE.willNotOverflow(Instruction::Mul, /*Signed=*/true, A, B))
1595+
return SE.getMulExpr(A, B);
1596+
return nullptr;
1597+
}
1598+
15901599
/// Returns the absolute value of \p A. In the context of dependence analysis,
15911600
/// we need an absolute value in a mathematical sense. If \p A is the signed
15921601
/// minimum value, we cannot represent it unless extending the original type.
@@ -1686,7 +1695,11 @@ bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
16861695
assert(0 < Level && Level <= CommonLevels && "level out of range");
16871696
Level--;
16881697

1689-
const SCEV *Delta = SE->getMinusSCEV(SrcConst, DstConst);
1698+
const SCEV *Delta = minusSCEVNoSignedOverflow(SrcConst, DstConst, *SE);
1699+
if (!Delta) {
1700+
Result.Consistent = false;
1701+
return false;
1702+
}
16901703
LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta);
16911704
LLVM_DEBUG(dbgs() << ", " << *Delta->getType() << "\n");
16921705

@@ -1702,7 +1715,9 @@ bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
17021715
const SCEV *AbsCoeff = absSCEVNoSignedOverflow(Coeff, *SE);
17031716
if (!AbsDelta || !AbsCoeff)
17041717
return false;
1705-
const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff);
1718+
const SCEV *Product = mulSCEVNoSignedOverflow(UpperBound, AbsCoeff, *SE);
1719+
if (!Product)
1720+
return false;
17061721
return isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product);
17071722
}();
17081723
if (IsDeltaLarge) {

llvm/test/Analysis/DependenceAnalysis/SimpleSIVNoValidityCheck.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ define void @t3(i64 %n, i64 %m, i64 %lb, ptr %a) {
210210
; CHECK-NEXT: Src: %2 = load i32, ptr %arrayidx6, align 4 --> Dst: %2 = load i32, ptr %arrayidx6, align 4
211211
; CHECK-NEXT: da analyze - none!
212212
; CHECK-NEXT: Src: %2 = load i32, ptr %arrayidx6, align 4 --> Dst: store i32 %2, ptr %arrayidx8, align 4
213-
; CHECK-NEXT: da analyze - consistent anti [1 -2]!
213+
; CHECK-NEXT: da analyze - anti [1 *]!
214214
; CHECK-NEXT: Src: store i32 %2, ptr %arrayidx8, align 4 --> Dst: store i32 %2, ptr %arrayidx8, align 4
215215
; CHECK-NEXT: da analyze - none!
216216
;

llvm/test/Analysis/DependenceAnalysis/StrongSIV.ll

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -535,12 +535,16 @@ for.end: ; preds = %for.body
535535
;; if (i < 3000000000)
536536
;; A[i] = 0;
537537
;
538-
; FIXME: DependenceAnalysis currently detects no dependency between A[i] and
539-
; itself, but it does exist.
538+
; FIXME: DependenceAnalysis fails to detect the dependency between A[i] and
539+
; itself, while Strong SIV has been able to prove it.
540540
define void @strong11(ptr %A) nounwind uwtable ssp {
541-
; CHECK-LABEL: 'strong11'
542-
; CHECK-NEXT: Src: store i32 0, ptr %arrayidx, align 4 --> Dst: store i32 0, ptr %arrayidx, align 4
543-
; CHECK-NEXT: da analyze - none!
541+
; CHECK-ALL-LABEL: 'strong11'
542+
; CHECK-ALL-NEXT: Src: store i32 0, ptr %arrayidx, align 4 --> Dst: store i32 0, ptr %arrayidx, align 4
543+
; CHECK-ALL-NEXT: da analyze - none!
544+
;
545+
; CHECK-STRONG-SIV-LABEL: 'strong11'
546+
; CHECK-STRONG-SIV-NEXT: Src: store i32 0, ptr %arrayidx, align 4 --> Dst: store i32 0, ptr %arrayidx, align 4
547+
; CHECK-STRONG-SIV-NEXT: da analyze - consistent output [0 S]!
544548
;
545549
entry:
546550
br label %for.cond1.preheader

0 commit comments

Comments
 (0)