@@ -430,8 +430,8 @@ static bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L,
430
430
if (!AddRec->isAffine ()) return false ;
431
431
432
432
// If Start contains an SCEVAddRecExpr from a different loop, other than an
433
- // outer loop of the current loop, reject it. SCEV has no concept of operating
434
- // on one loop at a time so don't confuse it with such expressions.
433
+ // outer loop of the current loop, reject it. SCEV has no concept of
434
+ // operating on one loop at a time so don't confuse it with such expressions.
435
435
if (containsAddRecFromDifferentLoop (Start, L))
436
436
return false ;
437
437
@@ -774,14 +774,14 @@ void BasedUser::RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
774
774
// which need not be an immediate predecessor of this PHI. This way we
775
775
// need only one copy of it even if it is referenced multiple times in
776
776
// the PHI. We don't do this when the original expression is inside the
777
- // loop because multiple copies sometimes do useful sinking of code in that
778
- // case(?).
777
+ // loop because multiple copies sometimes do useful sinking of code in
778
+ // that case(?).
779
779
Instruction *OldLoc = dyn_cast<Instruction>(OperandValToReplace);
780
780
if (L->contains (OldLoc->getParent ())) {
781
- // If this is a critical edge, split the edge so that we do not insert the
782
- // code on all predecessor/successor paths. We do this unless this is the
783
- // canonical backedge for this loop, as this can make some inserted code
784
- // be in an illegal position.
781
+ // If this is a critical edge, split the edge so that we do not insert
782
+ // the code on all predecessor/successor paths. We do this unless this
783
+ // is the canonical backedge for this loop, as this can make some
784
+ // inserted code be in an illegal position.
785
785
BasicBlock *PHIPred = PN->getIncomingBlock (i);
786
786
if (e != 1 && PHIPred->getTerminator ()->getNumSuccessors () > 1 &&
787
787
(PN->getParent () != L->getHeader () || !L->contains (PHIPred))) {
@@ -1224,19 +1224,21 @@ bool LoopStrengthReduce::ValidStride(bool HasBaseReg,
1224
1224
return true ;
1225
1225
}
1226
1226
1227
- // / RequiresTypeConversion - Returns true if converting Ty to NewTy is not
1227
+ // / RequiresTypeConversion - Returns true if converting Ty1 to Ty2 is not
1228
1228
// / a nop.
1229
1229
bool LoopStrengthReduce::RequiresTypeConversion (const Type *Ty1,
1230
1230
const Type *Ty2) {
1231
1231
if (Ty1 == Ty2)
1232
1232
return false ;
1233
+ if (Ty1->canLosslesslyBitCastTo (Ty2))
1234
+ return false ;
1233
1235
if (TLI && TLI->isTruncateFree (Ty1, Ty2))
1234
1236
return false ;
1235
- return (!Ty1-> canLosslesslyBitCastTo (Ty2) &&
1236
- !(isa<PointerType>(Ty2) &&
1237
- Ty1 ->canLosslesslyBitCastTo (UIntPtrTy)) &&
1238
- !(isa<PointerType>(Ty1) &&
1239
- Ty2-> canLosslesslyBitCastTo (UIntPtrTy))) ;
1237
+ if (isa<PointerType> (Ty2) && Ty1-> canLosslesslyBitCastTo (UIntPtrTy))
1238
+ return false ;
1239
+ if (isa<PointerType>(Ty1) && Ty2 ->canLosslesslyBitCastTo (UIntPtrTy))
1240
+ return false ;
1241
+ return true ;
1240
1242
}
1241
1243
1242
1244
// / CheckForIVReuse - Returns the multiple if the stride is the multiple
@@ -1661,15 +1663,28 @@ void LoopStrengthReduce::StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
1661
1663
Rewriter.clear ();
1662
1664
1663
1665
// If we are reusing the iv, then it must be multiplied by a constant
1664
- // factor take advantage of addressing mode scale component.
1666
+ // factor to take advantage of the addressing mode scale component.
1665
1667
if (!isa<SCEVConstant>(RewriteFactor) ||
1666
1668
!cast<SCEVConstant>(RewriteFactor)->isZero ()) {
1667
1669
// If we're reusing an IV with a nonzero base (currently this happens
1668
1670
// only when all reuses are outside the loop) subtract that base here.
1669
1671
// The base has been used to initialize the PHI node but we don't want
1670
1672
// it here.
1671
- if (!ReuseIV.Base ->isZero ())
1672
- RewriteExpr = SE->getMinusSCEV (RewriteExpr, ReuseIV.Base );
1673
+ if (!ReuseIV.Base ->isZero ()) {
1674
+ SCEVHandle typedBase = ReuseIV.Base ;
1675
+ if (RewriteExpr->getType ()->getPrimitiveSizeInBits () !=
1676
+ ReuseIV.Base ->getType ()->getPrimitiveSizeInBits ()) {
1677
+ // It's possible the original IV is a larger type than the new IV,
1678
+ // in which case we have to truncate the Base. We checked in
1679
+ // RequiresTypeConversion that this is valid.
1680
+ assert (RewriteExpr->getType ()->getPrimitiveSizeInBits () <
1681
+ ReuseIV.Base ->getType ()->getPrimitiveSizeInBits () &&
1682
+ " Unexpected lengthening conversion!" );
1683
+ typedBase = SE->getTruncateExpr (ReuseIV.Base ,
1684
+ RewriteExpr->getType ());
1685
+ }
1686
+ RewriteExpr = SE->getMinusSCEV (RewriteExpr, typedBase);
1687
+ }
1673
1688
1674
1689
// Multiply old variable, with base removed, by new scale factor.
1675
1690
RewriteExpr = SE->getMulExpr (RewriteFactor,
0 commit comments