Skip to content

Commit df61153

Browse files
1997alirezaDebadri Basak
authored andcommitted
[DA] Check for overflow in strong SIV test (llvm#164704)
Rely on the product of `UpperBound` and `AbsCoeff` only if SCEV can prove that there is no overflow. Also the same about the result of the subtraction of `DstConst` from `SrcConst` to calculate `Delta`.
1 parent 6ce315a commit df61153

File tree

3 files changed

+90
-17
lines changed

3 files changed

+90
-17
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: 72 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 5
22
; RUN: opt < %s -disable-output "-passes=print<da>" -aa-pipeline=basic-aa 2>&1 \
3-
; RUN: | FileCheck %s
3+
; RUN: | FileCheck %s --check-prefixes=CHECK,CHECK-ALL
4+
; RUN: opt < %s -disable-output "-passes=print<da>" -aa-pipeline=basic-aa -da-enable-dependence-test=strong-siv 2>&1 \
5+
; RUN: | FileCheck %s --check-prefixes=CHECK,CHECK-STRONG-SIV
46

57
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128"
68
target triple = "x86_64-apple-macosx10.6.0"
@@ -423,19 +425,33 @@ for.end: ; preds = %for.body
423425
;; *B++ = A[i + 2*n];
424426

425427
define void @strong9(ptr %A, ptr %B, i64 %n) nounwind uwtable ssp {
426-
; CHECK-LABEL: 'strong9'
427-
; CHECK-NEXT: Src: store i32 %conv, ptr %arrayidx, align 4 --> Dst: store i32 %conv, ptr %arrayidx, align 4
428-
; CHECK-NEXT: da analyze - none!
429-
; CHECK-NEXT: Src: store i32 %conv, ptr %arrayidx, align 4 --> Dst: %0 = load i32, ptr %arrayidx2, align 4
430-
; CHECK-NEXT: da analyze - none!
431-
; CHECK-NEXT: Src: store i32 %conv, ptr %arrayidx, align 4 --> Dst: store i32 %0, ptr %B.addr.02, align 4
432-
; CHECK-NEXT: da analyze - confused!
433-
; CHECK-NEXT: Src: %0 = load i32, ptr %arrayidx2, align 4 --> Dst: %0 = load i32, ptr %arrayidx2, align 4
434-
; CHECK-NEXT: da analyze - none!
435-
; CHECK-NEXT: Src: %0 = load i32, ptr %arrayidx2, align 4 --> Dst: store i32 %0, ptr %B.addr.02, align 4
436-
; CHECK-NEXT: da analyze - confused!
437-
; CHECK-NEXT: Src: store i32 %0, ptr %B.addr.02, align 4 --> Dst: store i32 %0, ptr %B.addr.02, align 4
438-
; CHECK-NEXT: da analyze - none!
428+
; CHECK-ALL-LABEL: 'strong9'
429+
; CHECK-ALL-NEXT: Src: store i32 %conv, ptr %arrayidx, align 4 --> Dst: store i32 %conv, ptr %arrayidx, align 4
430+
; CHECK-ALL-NEXT: da analyze - none!
431+
; CHECK-ALL-NEXT: Src: store i32 %conv, ptr %arrayidx, align 4 --> Dst: %0 = load i32, ptr %arrayidx2, align 4
432+
; CHECK-ALL-NEXT: da analyze - none!
433+
; CHECK-ALL-NEXT: Src: store i32 %conv, ptr %arrayidx, align 4 --> Dst: store i32 %0, ptr %B.addr.02, align 4
434+
; CHECK-ALL-NEXT: da analyze - confused!
435+
; CHECK-ALL-NEXT: Src: %0 = load i32, ptr %arrayidx2, align 4 --> Dst: %0 = load i32, ptr %arrayidx2, align 4
436+
; CHECK-ALL-NEXT: da analyze - none!
437+
; CHECK-ALL-NEXT: Src: %0 = load i32, ptr %arrayidx2, align 4 --> Dst: store i32 %0, ptr %B.addr.02, align 4
438+
; CHECK-ALL-NEXT: da analyze - confused!
439+
; CHECK-ALL-NEXT: Src: store i32 %0, ptr %B.addr.02, align 4 --> Dst: store i32 %0, ptr %B.addr.02, align 4
440+
; CHECK-ALL-NEXT: da analyze - none!
441+
;
442+
; CHECK-STRONG-SIV-LABEL: 'strong9'
443+
; CHECK-STRONG-SIV-NEXT: Src: store i32 %conv, ptr %arrayidx, align 4 --> Dst: store i32 %conv, ptr %arrayidx, align 4
444+
; CHECK-STRONG-SIV-NEXT: da analyze - none!
445+
; CHECK-STRONG-SIV-NEXT: Src: store i32 %conv, ptr %arrayidx, align 4 --> Dst: %0 = load i32, ptr %arrayidx2, align 4
446+
; CHECK-STRONG-SIV-NEXT: da analyze - flow [*|<]!
447+
; CHECK-STRONG-SIV-NEXT: Src: store i32 %conv, ptr %arrayidx, align 4 --> Dst: store i32 %0, ptr %B.addr.02, align 4
448+
; CHECK-STRONG-SIV-NEXT: da analyze - confused!
449+
; CHECK-STRONG-SIV-NEXT: Src: %0 = load i32, ptr %arrayidx2, align 4 --> Dst: %0 = load i32, ptr %arrayidx2, align 4
450+
; CHECK-STRONG-SIV-NEXT: da analyze - none!
451+
; CHECK-STRONG-SIV-NEXT: Src: %0 = load i32, ptr %arrayidx2, align 4 --> Dst: store i32 %0, ptr %B.addr.02, align 4
452+
; CHECK-STRONG-SIV-NEXT: da analyze - confused!
453+
; CHECK-STRONG-SIV-NEXT: Src: store i32 %0, ptr %B.addr.02, align 4 --> Dst: store i32 %0, ptr %B.addr.02, align 4
454+
; CHECK-STRONG-SIV-NEXT: da analyze - none!
439455
;
440456
entry:
441457
%cmp1 = icmp eq i64 %n, 0
@@ -512,3 +528,45 @@ for.body: ; preds = %entry, %for.body
512528
for.end: ; preds = %for.body
513529
ret void
514530
}
531+
532+
533+
;; for (long unsigned i = 0; i < 9223372036854775806; i++)
534+
;; for (long unsigned j = 0; j < 2147483640; j++)
535+
;; if (i < 3000000000)
536+
;; A[i] = 0;
537+
;
538+
; FIXME: DependenceAnalysis fails to detect the dependency between A[i] and
539+
; itself, while Strong SIV has been able to prove it.
540+
define void @strong11(ptr %A) nounwind uwtable ssp {
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]!
548+
;
549+
entry:
550+
br label %for.cond1.preheader
551+
552+
for.cond1.preheader: ; preds = %entry, %for.cond.cleanup3
553+
%i.017 = phi i64 [ 0, %entry ], [ %inc8, %for.cond.cleanup3 ]
554+
%cmp5 = icmp samesign ult i64 %i.017, 3000000000
555+
%arrayidx = getelementptr inbounds nuw i32, ptr %A, i64 %i.017
556+
br i1 %cmp5, label %for.body4.us, label %for.cond.cleanup3
557+
558+
for.body4.us: ; preds = %for.cond1.preheader, %for.body4.us
559+
%j.016.us = phi i64 [ %inc.us, %for.body4.us ], [ 0, %for.cond1.preheader ]
560+
store i32 0, ptr %arrayidx, align 4
561+
%inc.us = add nuw nsw i64 %j.016.us, 1
562+
%exitcond.not = icmp eq i64 %inc.us, 2147483640
563+
br i1 %exitcond.not, label %for.cond.cleanup3, label %for.body4.us
564+
565+
for.cond.cleanup: ; preds = %for.cond.cleanup3
566+
ret void
567+
568+
for.cond.cleanup3: ; preds = %for.body4.us, %for.cond1.preheader
569+
%inc8 = add nuw nsw i64 %i.017, 1
570+
%exitcond19.not = icmp eq i64 %inc8, 9223372036854775806
571+
br i1 %exitcond19.not, label %for.cond.cleanup, label %for.cond1.preheader
572+
}

0 commit comments

Comments
 (0)