Skip to content

Commit acd700a

Browse files
author
Dan Gohman
committed
Don't attempt to analyze values which are obviously undef. This fixes some
assertion failures in extreme cases. llvm-svn: 102042
1 parent c951e6e commit acd700a

File tree

2 files changed

+141
-75
lines changed

2 files changed

+141
-75
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 102 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1846,77 +1846,81 @@ const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
18461846
if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
18471847
if (RHSC->getValue()->equalsInt(1))
18481848
return LHS; // X udiv 1 --> x
1849-
if (RHSC->getValue()->isZero())
1850-
return getIntegerSCEV(0, LHS->getType()); // value is undefined
1851-
1852-
// Determine if the division can be folded into the operands of
1853-
// its operands.
1854-
// TODO: Generalize this to non-constants by using known-bits information.
1855-
const Type *Ty = LHS->getType();
1856-
unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros();
1857-
unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ;
1858-
// For non-power-of-two values, effectively round the value up to the
1859-
// nearest power of two.
1860-
if (!RHSC->getValue()->getValue().isPowerOf2())
1861-
++MaxShiftAmt;
1862-
const IntegerType *ExtTy =
1863-
IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt);
1864-
// {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
1865-
if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
1866-
if (const SCEVConstant *Step =
1867-
dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this)))
1868-
if (!Step->getValue()->getValue()
1869-
.urem(RHSC->getValue()->getValue()) &&
1870-
getZeroExtendExpr(AR, ExtTy) ==
1871-
getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
1872-
getZeroExtendExpr(Step, ExtTy),
1873-
AR->getLoop())) {
1874-
SmallVector<const SCEV *, 4> Operands;
1875-
for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i)
1876-
Operands.push_back(getUDivExpr(AR->getOperand(i), RHS));
1877-
return getAddRecExpr(Operands, AR->getLoop());
1878-
}
1879-
// (A*B)/C --> A*(B/C) if safe and B/C can be folded.
1880-
if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
1881-
SmallVector<const SCEV *, 4> Operands;
1882-
for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i)
1883-
Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy));
1884-
if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
1885-
// Find an operand that's safely divisible.
1886-
for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
1887-
const SCEV *Op = M->getOperand(i);
1888-
const SCEV *Div = getUDivExpr(Op, RHSC);
1889-
if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
1890-
Operands = SmallVector<const SCEV *, 4>(M->op_begin(), M->op_end());
1891-
Operands[i] = Div;
1892-
return getMulExpr(Operands);
1849+
// If the denominator is zero, the result of the udiv is undefined. Don't
1850+
// try to analyze it, because the resolution chosen here may differ from
1851+
// the resolution chosen in other parts of the compiler.
1852+
if (!RHSC->getValue()->isZero()) {
1853+
// Determine if the division can be folded into the operands of
1854+
// its operands.
1855+
// TODO: Generalize this to non-constants by using known-bits information.
1856+
const Type *Ty = LHS->getType();
1857+
unsigned LZ = RHSC->getValue()->getValue().countLeadingZeros();
1858+
unsigned MaxShiftAmt = getTypeSizeInBits(Ty) - LZ;
1859+
// For non-power-of-two values, effectively round the value up to the
1860+
// nearest power of two.
1861+
if (!RHSC->getValue()->getValue().isPowerOf2())
1862+
++MaxShiftAmt;
1863+
const IntegerType *ExtTy =
1864+
IntegerType::get(getContext(), getTypeSizeInBits(Ty) + MaxShiftAmt);
1865+
// {X,+,N}/C --> {X/C,+,N/C} if safe and N/C can be folded.
1866+
if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(LHS))
1867+
if (const SCEVConstant *Step =
1868+
dyn_cast<SCEVConstant>(AR->getStepRecurrence(*this)))
1869+
if (!Step->getValue()->getValue()
1870+
.urem(RHSC->getValue()->getValue()) &&
1871+
getZeroExtendExpr(AR, ExtTy) ==
1872+
getAddRecExpr(getZeroExtendExpr(AR->getStart(), ExtTy),
1873+
getZeroExtendExpr(Step, ExtTy),
1874+
AR->getLoop())) {
1875+
SmallVector<const SCEV *, 4> Operands;
1876+
for (unsigned i = 0, e = AR->getNumOperands(); i != e; ++i)
1877+
Operands.push_back(getUDivExpr(AR->getOperand(i), RHS));
1878+
return getAddRecExpr(Operands, AR->getLoop());
18931879
}
1880+
// (A*B)/C --> A*(B/C) if safe and B/C can be folded.
1881+
if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(LHS)) {
1882+
SmallVector<const SCEV *, 4> Operands;
1883+
for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i)
1884+
Operands.push_back(getZeroExtendExpr(M->getOperand(i), ExtTy));
1885+
if (getZeroExtendExpr(M, ExtTy) == getMulExpr(Operands))
1886+
// Find an operand that's safely divisible.
1887+
for (unsigned i = 0, e = M->getNumOperands(); i != e; ++i) {
1888+
const SCEV *Op = M->getOperand(i);
1889+
const SCEV *Div = getUDivExpr(Op, RHSC);
1890+
if (!isa<SCEVUDivExpr>(Div) && getMulExpr(Div, RHSC) == Op) {
1891+
Operands = SmallVector<const SCEV *, 4>(M->op_begin(),
1892+
M->op_end());
1893+
Operands[i] = Div;
1894+
return getMulExpr(Operands);
1895+
}
1896+
}
1897+
}
1898+
// (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
1899+
if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) {
1900+
SmallVector<const SCEV *, 4> Operands;
1901+
for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i)
1902+
Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy));
1903+
if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
1904+
Operands.clear();
1905+
for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
1906+
const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
1907+
if (isa<SCEVUDivExpr>(Op) ||
1908+
getMulExpr(Op, RHS) != A->getOperand(i))
1909+
break;
1910+
Operands.push_back(Op);
1911+
}
1912+
if (Operands.size() == A->getNumOperands())
1913+
return getAddExpr(Operands);
18941914
}
1895-
}
1896-
// (A+B)/C --> (A/C + B/C) if safe and A/C and B/C can be folded.
1897-
if (const SCEVAddRecExpr *A = dyn_cast<SCEVAddRecExpr>(LHS)) {
1898-
SmallVector<const SCEV *, 4> Operands;
1899-
for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i)
1900-
Operands.push_back(getZeroExtendExpr(A->getOperand(i), ExtTy));
1901-
if (getZeroExtendExpr(A, ExtTy) == getAddExpr(Operands)) {
1902-
Operands.clear();
1903-
for (unsigned i = 0, e = A->getNumOperands(); i != e; ++i) {
1904-
const SCEV *Op = getUDivExpr(A->getOperand(i), RHS);
1905-
if (isa<SCEVUDivExpr>(Op) || getMulExpr(Op, RHS) != A->getOperand(i))
1906-
break;
1907-
Operands.push_back(Op);
1908-
}
1909-
if (Operands.size() == A->getNumOperands())
1910-
return getAddExpr(Operands);
19111915
}
1912-
}
19131916

1914-
// Fold if both operands are constant.
1915-
if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
1916-
Constant *LHSCV = LHSC->getValue();
1917-
Constant *RHSCV = RHSC->getValue();
1918-
return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV,
1919-
RHSCV)));
1917+
// Fold if both operands are constant.
1918+
if (const SCEVConstant *LHSC = dyn_cast<SCEVConstant>(LHS)) {
1919+
Constant *LHSCV = LHSC->getValue();
1920+
Constant *RHSCV = RHSC->getValue();
1921+
return getConstant(cast<ConstantInt>(ConstantExpr::getUDiv(LHSCV,
1922+
RHSCV)));
1923+
}
19201924
}
19211925
}
19221926

@@ -3319,8 +3323,16 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
33193323
// Turn shift left of a constant amount into a multiply.
33203324
if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
33213325
uint32_t BitWidth = cast<IntegerType>(U->getType())->getBitWidth();
3326+
3327+
// If the shift count is not less than the bitwidth, the result of
3328+
// the shift is undefined. Don't try to analyze it, because the
3329+
// resolution chosen here may differ from the resolution chosen in
3330+
// other parts of the compiler.
3331+
if (SA->getValue().uge(BitWidth))
3332+
break;
3333+
33223334
Constant *X = ConstantInt::get(getContext(),
3323-
APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
3335+
APInt(BitWidth, 1).shl(SA->getZExtValue()));
33243336
return getMulExpr(getSCEV(U->getOperand(0)), getSCEV(X));
33253337
}
33263338
break;
@@ -3329,28 +3341,43 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
33293341
// Turn logical shift right of a constant into a unsigned divide.
33303342
if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand(1))) {
33313343
uint32_t BitWidth = cast<IntegerType>(U->getType())->getBitWidth();
3344+
3345+
// If the shift count is not less than the bitwidth, the result of
3346+
// the shift is undefined. Don't try to analyze it, because the
3347+
// resolution chosen here may differ from the resolution chosen in
3348+
// other parts of the compiler.
3349+
if (SA->getValue().uge(BitWidth))
3350+
break;
3351+
33323352
Constant *X = ConstantInt::get(getContext(),
3333-
APInt(BitWidth, 1).shl(SA->getLimitedValue(BitWidth)));
3353+
APInt(BitWidth, 1).shl(SA->getZExtValue()));
33343354
return getUDivExpr(getSCEV(U->getOperand(0)), getSCEV(X));
33353355
}
33363356
break;
33373357

33383358
case Instruction::AShr:
33393359
// For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression.
33403360
if (ConstantInt *CI = dyn_cast<ConstantInt>(U->getOperand(1)))
3341-
if (Instruction *L = dyn_cast<Instruction>(U->getOperand(0)))
3361+
if (Operator *L = dyn_cast<Operator>(U->getOperand(0)))
33423362
if (L->getOpcode() == Instruction::Shl &&
33433363
L->getOperand(1) == U->getOperand(1)) {
3344-
unsigned BitWidth = getTypeSizeInBits(U->getType());
3364+
uint64_t BitWidth = getTypeSizeInBits(U->getType());
3365+
3366+
// If the shift count is not less than the bitwidth, the result of
3367+
// the shift is undefined. Don't try to analyze it, because the
3368+
// resolution chosen here may differ from the resolution chosen in
3369+
// other parts of the compiler.
3370+
if (CI->getValue().uge(BitWidth))
3371+
break;
3372+
33453373
uint64_t Amt = BitWidth - CI->getZExtValue();
33463374
if (Amt == BitWidth)
33473375
return getSCEV(L->getOperand(0)); // shift by zero --> noop
3348-
if (Amt > BitWidth)
3349-
return getIntegerSCEV(0, U->getType()); // value is undefined
33503376
return
33513377
getSignExtendExpr(getTruncateExpr(getSCEV(L->getOperand(0)),
3352-
IntegerType::get(getContext(), Amt)),
3353-
U->getType());
3378+
IntegerType::get(getContext(),
3379+
Amt)),
3380+
U->getType());
33543381
}
33553382
break;
33563383

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; RUN: opt -analyze -scalar-evolution < %s | FileCheck %s
2+
3+
; ScalarEvolution shouldn't attempt to interpret expressions which have
4+
; undefined results.
5+
6+
define void @foo(i64 %x) {
7+
8+
%a = udiv i64 %x, 0
9+
; CHECK: --> (%x /u 0)
10+
11+
%B = shl i64 %x, 64
12+
; CHECK: --> %B
13+
14+
%b = ashr i64 %B, 64
15+
; CHECK: --> %b
16+
17+
%c = lshr i64 %x, 64
18+
; CHECK: --> %c
19+
20+
%d = shl i64 %x, 64
21+
; CHECK: --> %d
22+
23+
%E = shl i64 %x, -1
24+
; CHECK: --> %E
25+
26+
%e = ashr i64 %E, -1
27+
; CHECK: --> %e
28+
29+
%f = lshr i64 %x, -1
30+
; CHECK: --> %f
31+
32+
%g = shl i64 %x, -1
33+
; CHECK: --> %g
34+
35+
%h = bitcast i64 undef to i64
36+
; CHECK: --> undef
37+
38+
ret void
39+
}

0 commit comments

Comments
 (0)