Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit b4e0c9b

Browse files
committed
Reapply r198654 "indvars: sink truncates outside the loop."
This doesn't seem to have actually broken anything. It was paranoia on my part. Trying again now that bots are more stable. This is a follow up of the r198338 commit that added truncates for lcssa phi nodes. Sinking the truncates below the phis cleans up the loop and simplifies subsequent analysis within the indvars pass. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@198678 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent a4d123f commit b4e0c9b

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

lib/Transforms/Scalar/IndVarSimplify.cpp

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -883,6 +883,8 @@ const SCEVAddRecExpr *WidenIV::GetWideRecurrence(Instruction *NarrowUse) {
883883
/// This IV user cannot be widen. Replace this use of the original narrow IV
884884
/// with a truncation of the new wide IV to isolate and eliminate the narrow IV.
885885
static void truncateIVUse(NarrowIVDefUse DU, DominatorTree *DT) {
886+
DEBUG(dbgs() << "INDVARS: Truncate IV " << *DU.WideDef
887+
<< " for user " << *DU.NarrowUse << "\n");
886888
IRBuilder<> Builder(getInsertPointForUses(DU.NarrowUse, DU.NarrowDef, DT));
887889
Value *Trunc = Builder.CreateTrunc(DU.WideDef, DU.NarrowDef->getType());
888890
DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, Trunc);
@@ -893,10 +895,27 @@ static void truncateIVUse(NarrowIVDefUse DU, DominatorTree *DT) {
893895
Instruction *WidenIV::WidenIVUse(NarrowIVDefUse DU, SCEVExpander &Rewriter) {
894896

895897
// Stop traversing the def-use chain at inner-loop phis or post-loop phis.
896-
if (isa<PHINode>(DU.NarrowUse) &&
897-
LI->getLoopFor(DU.NarrowUse->getParent()) != L) {
898-
truncateIVUse(DU, DT);
899-
return 0;
898+
if (PHINode *UsePhi = dyn_cast<PHINode>(DU.NarrowUse)) {
899+
if (LI->getLoopFor(UsePhi->getParent()) != L) {
900+
// For LCSSA phis, sink the truncate outside the loop.
901+
// After SimplifyCFG most loop exit targets have a single predecessor.
902+
// Otherwise fall back to a truncate within the loop.
903+
if (UsePhi->getNumOperands() != 1)
904+
truncateIVUse(DU, DT);
905+
else {
906+
PHINode *WidePhi =
907+
PHINode::Create(DU.WideDef->getType(), 1, UsePhi->getName() + ".wide",
908+
UsePhi);
909+
WidePhi->addIncoming(DU.WideDef, UsePhi->getIncomingBlock(0));
910+
IRBuilder<> Builder(WidePhi->getParent()->getFirstInsertionPt());
911+
Value *Trunc = Builder.CreateTrunc(WidePhi, DU.NarrowDef->getType());
912+
UsePhi->replaceAllUsesWith(Trunc);
913+
DeadInsts.push_back(UsePhi);
914+
DEBUG(dbgs() << "INDVARS: Widen lcssa phi " << *UsePhi
915+
<< " to " << *WidePhi << "\n");
916+
}
917+
return 0;
918+
}
900919
}
901920
// Our raison d'etre! Eliminate sign and zero extension.
902921
if (IsSigned ? isa<SExtInst>(DU.NarrowUse) : isa<ZExtInst>(DU.NarrowUse)) {

test/Transforms/IndVarSimplify/iv-widen.ll

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@ target triple = "x86_64-apple-darwin"
77
; Only one phi now.
88
; CHECK: phi
99
; CHECK-NOT: phi
10-
; We now get 2 trunc, one for the gep and one for the lcssa phi.
10+
; One trunc for the gep.
1111
; CHECK: trunc i64 %indvars.iv to i32
12-
; CHECK: trunc i64 %indvars.iv to i32
13-
; CHECK-LABEL: B24:
12+
; One trunc for the dummy() call.
13+
; CHECK-LABEL: exit24:
14+
; CHECK: trunc i64 {{.*}}lcssa.wide to i32
1415
define void @sloop(i32* %a) {
1516
Prologue:
1617
br i1 undef, label %B18, label %B6

0 commit comments

Comments
 (0)