Skip to content

Commit 7792a83

Browse files
committed
Merge 64177 from mainline.
Fix PR 3471, and some cleanups. llvm-svn: 64517
1 parent a65abd2 commit 7792a83

File tree

2 files changed

+65
-17
lines changed

2 files changed

+65
-17
lines changed

llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -430,8 +430,8 @@ static bool getSCEVStartAndStride(const SCEVHandle &SH, Loop *L,
430430
if (!AddRec->isAffine()) return false;
431431

432432
// 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.
435435
if (containsAddRecFromDifferentLoop(Start, L))
436436
return false;
437437

@@ -774,14 +774,14 @@ void BasedUser::RewriteInstructionToUseNewBase(const SCEVHandle &NewBase,
774774
// which need not be an immediate predecessor of this PHI. This way we
775775
// need only one copy of it even if it is referenced multiple times in
776776
// 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(?).
779779
Instruction *OldLoc = dyn_cast<Instruction>(OperandValToReplace);
780780
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.
785785
BasicBlock *PHIPred = PN->getIncomingBlock(i);
786786
if (e != 1 && PHIPred->getTerminator()->getNumSuccessors() > 1 &&
787787
(PN->getParent() != L->getHeader() || !L->contains(PHIPred))) {
@@ -1224,19 +1224,21 @@ bool LoopStrengthReduce::ValidStride(bool HasBaseReg,
12241224
return true;
12251225
}
12261226

1227-
/// RequiresTypeConversion - Returns true if converting Ty to NewTy is not
1227+
/// RequiresTypeConversion - Returns true if converting Ty1 to Ty2 is not
12281228
/// a nop.
12291229
bool LoopStrengthReduce::RequiresTypeConversion(const Type *Ty1,
12301230
const Type *Ty2) {
12311231
if (Ty1 == Ty2)
12321232
return false;
1233+
if (Ty1->canLosslesslyBitCastTo(Ty2))
1234+
return false;
12331235
if (TLI && TLI->isTruncateFree(Ty1, Ty2))
12341236
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;
12401242
}
12411243

12421244
/// CheckForIVReuse - Returns the multiple if the stride is the multiple
@@ -1661,15 +1663,28 @@ void LoopStrengthReduce::StrengthReduceStridedIVUsers(const SCEVHandle &Stride,
16611663
Rewriter.clear();
16621664

16631665
// 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.
16651667
if (!isa<SCEVConstant>(RewriteFactor) ||
16661668
!cast<SCEVConstant>(RewriteFactor)->isZero()) {
16671669
// If we're reusing an IV with a nonzero base (currently this happens
16681670
// only when all reuses are outside the loop) subtract that base here.
16691671
// The base has been used to initialize the PHI node but we don't want
16701672
// 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+
}
16731688

16741689
// Multiply old variable, with base removed, by new scale factor.
16751690
RewriteExpr = SE->getMulExpr(RewriteFactor,
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; RUN: llvm-as < %s | llc
2+
; This used to crash.
3+
; ModuleID = 'bugpoint-reduced-simplified.bc'
4+
target datalayout ="e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
5+
target triple = "x86_64-unknown-linux-gnu"
6+
7+
define void @parse_number(i8* nocapture %p) nounwind {
8+
entry:
9+
%shift.0 = select i1 false, i32 4, i32 2 ; <i32> [#uses=1]
10+
br label %bb47
11+
12+
bb47: ; preds = %bb47, %entry
13+
br i1 false, label %bb54, label %bb47
14+
15+
bb54: ; preds = %bb47
16+
br i1 false, label %bb56, label %bb66
17+
18+
bb56: ; preds = %bb62, %bb54
19+
%p_addr.0.pn.rec = phi i64 [ %p_addr.6.rec, %bb62 ], [ 0, %bb54 ] ; <i64> [#uses=2]
20+
%ch.6.in.in = phi i8* [ %p_addr.6, %bb62 ], [ null, %bb54 ] ; <i8*> [#uses=0]
21+
%indvar202 = trunc i64 %p_addr.0.pn.rec to i32 ; <i32>[#uses=1]
22+
%frac_bits.0 = mul i32 %indvar202, %shift.0 ; <i32>[#uses=1]
23+
%p_addr.6.rec = add i64 %p_addr.0.pn.rec, 1 ; <i64>[#uses=2]
24+
%p_addr.6 = getelementptr i8* null, i64 %p_addr.6.rec ; <i8*>[#uses=1]
25+
br i1 false, label %bb66, label %bb62
26+
27+
bb62: ; preds = %bb56
28+
br label %bb56
29+
30+
bb66: ; preds = %bb56, %bb54
31+
%frac_bits.1 = phi i32 [ 0, %bb54 ], [ %frac_bits.0, %bb56 ] ; <i32> [#uses=0]
32+
unreachable
33+
}

0 commit comments

Comments
 (0)