Skip to content

Commit b6231f5

Browse files
authored
[DA] Add overflow check in ExactSIV (#157086)
This patch adds an overflow check to the `exactSIVtest` function to fix the issue demonstrated in the test case added in #157085. This patch only fixes one of the routines. To fully resolve the test case, the other functions need to be addressed as well.
1 parent a05e8d5 commit b6231f5

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

llvm/lib/Analysis/DependenceAnalysis.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,15 @@ const SCEVConstant *DependenceInfo::collectConstantUpperBound(const Loop *L,
11701170
return nullptr;
11711171
}
11721172

1173+
/// Returns \p A - \p B if it guaranteed not to signed wrap. Otherwise returns
1174+
/// nullptr. \p A and \p B must have the same integer type.
1175+
static const SCEV *minusSCEVNoSignedOverflow(const SCEV *A, const SCEV *B,
1176+
ScalarEvolution &SE) {
1177+
if (SE.willNotOverflow(Instruction::Sub, /*Signed=*/true, A, B))
1178+
return SE.getMinusSCEV(A, B);
1179+
return nullptr;
1180+
}
1181+
11731182
// testZIV -
11741183
// When we have a pair of subscripts of the form [c1] and [c2],
11751184
// where c1 and c2 are both loop invariant, we attack it using
@@ -1626,7 +1635,9 @@ bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
16261635
assert(0 < Level && Level <= CommonLevels && "Level out of range");
16271636
Level--;
16281637
Result.Consistent = false;
1629-
const SCEV *Delta = SE->getMinusSCEV(DstConst, SrcConst);
1638+
const SCEV *Delta = minusSCEVNoSignedOverflow(DstConst, SrcConst, *SE);
1639+
if (!Delta)
1640+
return false;
16301641
LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta << "\n");
16311642
NewConstraint.setLine(SrcCoeff, SE->getNegativeSCEV(DstCoeff), Delta,
16321643
CurLoop);
@@ -1716,6 +1727,7 @@ bool DependenceInfo::exactSIVtest(const SCEV *SrcCoeff, const SCEV *DstCoeff,
17161727
// explore directions
17171728
unsigned NewDirection = Dependence::DVEntry::NONE;
17181729
APInt LowerDistance, UpperDistance;
1730+
// TODO: Overflow check may be needed.
17191731
if (TA.sgt(TB)) {
17201732
LowerDistance = (TY - TX) + (TA - TB) * TL;
17211733
UpperDistance = (TY - TX) + (TA - TB) * TU;

llvm/test/Analysis/DependenceAnalysis/ExactSIV.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ define void @exact14(ptr %A) {
841841
; CHECK-SIV-ONLY-NEXT: Src: store i8 0, ptr %idx.0, align 1 --> Dst: store i8 0, ptr %idx.0, align 1
842842
; CHECK-SIV-ONLY-NEXT: da analyze - none!
843843
; CHECK-SIV-ONLY-NEXT: Src: store i8 0, ptr %idx.0, align 1 --> Dst: store i8 1, ptr %idx.1, align 1
844-
; CHECK-SIV-ONLY-NEXT: da analyze - none!
844+
; CHECK-SIV-ONLY-NEXT: da analyze - output [*|<]!
845845
; CHECK-SIV-ONLY-NEXT: Src: store i8 1, ptr %idx.1, align 1 --> Dst: store i8 1, ptr %idx.1, align 1
846846
; CHECK-SIV-ONLY-NEXT: da analyze - none!
847847
;

0 commit comments

Comments
 (0)