Skip to content

Commit 271f32a

Browse files
committed
[DA] Widening SCEV expressions in strong SIV test to prevent overflow
By doubling the size of the expressions in the strong SIV test, no overflow occurs in the multiplication required to check the test.
1 parent c1678e5 commit 271f32a

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

llvm/lib/Analysis/DependenceAnalysis.cpp

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,17 +1668,35 @@ bool DependenceInfo::strongSIVtest(const SCEV *Coeff, const SCEV *SrcConst,
16681668
LLVM_DEBUG(dbgs() << "\t Delta = " << *Delta);
16691669
LLVM_DEBUG(dbgs() << ", " << *Delta->getType() << "\n");
16701670

1671+
TypeSize CoeffSize =
1672+
Coeff->getType()->getScalarType()->getPrimitiveSizeInBits();
1673+
TypeSize SrcSize =
1674+
SrcConst->getType()->getScalarType()->getPrimitiveSizeInBits();
1675+
TypeSize DstSize =
1676+
DstConst->getType()->getScalarType()->getPrimitiveSizeInBits();
1677+
TypeSize WideSize = std::max(CoeffSize, std::max(SrcSize, DstSize)) * 2;
1678+
LLVMContext &Context = CurDstLoop->getHeader()->getParent()->getContext();
1679+
Type *WideTy = IntegerType::get(Context, WideSize);
1680+
const SCEV *WideSrcC = SE->getSignExtendExpr(SrcConst, WideTy);
1681+
const SCEV *WideDstC = SE->getSignExtendExpr(DstConst, WideTy);
1682+
const SCEV *WideDelta = SE->getMinusSCEV(WideSrcC, WideDstC);
1683+
const SCEV *WideCoeff = SE->getSignExtendExpr(Coeff, WideTy);
1684+
16711685
// check that |Delta| < iteration count
1672-
if (const SCEV *UpperBound =
1673-
collectUpperBound(CurSrcLoop, Delta->getType())) {
1674-
LLVM_DEBUG(dbgs() << "\t UpperBound = " << *UpperBound);
1675-
LLVM_DEBUG(dbgs() << ", " << *UpperBound->getType() << "\n");
1676-
const SCEV *AbsDelta =
1677-
SE->isKnownNonNegative(Delta) ? Delta : SE->getNegativeSCEV(Delta);
1678-
const SCEV *AbsCoeff =
1679-
SE->isKnownNonNegative(Coeff) ? Coeff : SE->getNegativeSCEV(Coeff);
1680-
const SCEV *Product = SE->getMulExpr(UpperBound, AbsCoeff);
1681-
if (isKnownPredicate(CmpInst::ICMP_SGT, AbsDelta, Product)) {
1686+
if (const SCEV *WideUpperBound =
1687+
collectUpperBound(CurSrcLoop, WideDelta->getType())) {
1688+
LLVM_DEBUG(dbgs() << "\t WideUpperBound = " << *WideUpperBound);
1689+
LLVM_DEBUG(dbgs() << ", " << *WideUpperBound->getType() << "\n");
1690+
1691+
// FIXME: Use SCEV getAbsExpr function to compute the abstract values
1692+
const SCEV *WideAbsDelta = SE->isKnownNonNegative(WideDelta)
1693+
? WideDelta
1694+
: SE->getNegativeSCEV(WideDelta);
1695+
const SCEV *WideAbsCoeff = SE->isKnownNonNegative(WideCoeff)
1696+
? WideCoeff
1697+
: SE->getNegativeSCEV(WideCoeff);
1698+
const SCEV *WideProduct = SE->getMulExpr(WideUpperBound, WideAbsCoeff);
1699+
if (isKnownPredicate(CmpInst::ICMP_SGT, WideAbsDelta, WideProduct)) {
16821700
// Distance greater than trip count - no dependence
16831701
++StrongSIVindependence;
16841702
++StrongSIVsuccesses;

0 commit comments

Comments
 (0)