@@ -1846,77 +1846,81 @@ const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
1846
1846
if (const SCEVConstant *RHSC = dyn_cast<SCEVConstant>(RHS)) {
1847
1847
if (RHSC->getValue ()->equalsInt (1 ))
1848
1848
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 ());
1893
1879
}
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);
1894
1914
}
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);
1911
1915
}
1912
- }
1913
1916
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
+ }
1920
1924
}
1921
1925
}
1922
1926
@@ -3319,8 +3323,16 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
3319
3323
// Turn shift left of a constant amount into a multiply.
3320
3324
if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand (1 ))) {
3321
3325
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
+
3322
3334
Constant *X = ConstantInt::get (getContext (),
3323
- APInt (BitWidth, 1 ).shl (SA->getLimitedValue (BitWidth )));
3335
+ APInt (BitWidth, 1 ).shl (SA->getZExtValue ( )));
3324
3336
return getMulExpr (getSCEV (U->getOperand (0 )), getSCEV (X));
3325
3337
}
3326
3338
break ;
@@ -3329,28 +3341,43 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
3329
3341
// Turn logical shift right of a constant into a unsigned divide.
3330
3342
if (ConstantInt *SA = dyn_cast<ConstantInt>(U->getOperand (1 ))) {
3331
3343
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
+
3332
3352
Constant *X = ConstantInt::get (getContext (),
3333
- APInt (BitWidth, 1 ).shl (SA->getLimitedValue (BitWidth )));
3353
+ APInt (BitWidth, 1 ).shl (SA->getZExtValue ( )));
3334
3354
return getUDivExpr (getSCEV (U->getOperand (0 )), getSCEV (X));
3335
3355
}
3336
3356
break ;
3337
3357
3338
3358
case Instruction::AShr:
3339
3359
// For a two-shift sext-inreg, use sext(trunc(x)) as the SCEV expression.
3340
3360
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 )))
3342
3362
if (L->getOpcode () == Instruction::Shl &&
3343
3363
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
+
3345
3373
uint64_t Amt = BitWidth - CI->getZExtValue ();
3346
3374
if (Amt == BitWidth)
3347
3375
return getSCEV (L->getOperand (0 )); // shift by zero --> noop
3348
- if (Amt > BitWidth)
3349
- return getIntegerSCEV (0 , U->getType ()); // value is undefined
3350
3376
return
3351
3377
getSignExtendExpr (getTruncateExpr (getSCEV (L->getOperand (0 )),
3352
- IntegerType::get (getContext (), Amt)),
3353
- U->getType ());
3378
+ IntegerType::get (getContext (),
3379
+ Amt)),
3380
+ U->getType ());
3354
3381
}
3355
3382
break ;
3356
3383
0 commit comments