Skip to content

Commit be16691

Browse files
preamestru
authored andcommitted
[RLEV] Pick a correct insert point when incoming instruction is itself a phi node
This fixes #57336. It was exposed by a recent SCEV change, but appears to have been a long standing issue. Note that the whole insert into the loop instead of a split exit edge is slightly contrived to begin with; it's there solely because IndVarSimplify preserves the CFG. Differential Revision: https://reviews.llvm.org/D132571 (cherry picked from commit c37b1a5)
1 parent 11ba13a commit be16691

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

llvm/lib/Transforms/Utils/LoopUtils.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,10 @@ int llvm::rewriteLoopExitValues(Loop *L, LoopInfo *LI, TargetLibraryInfo *TLI,
13941394
// and next SCEV may errneously get smaller cost.
13951395

13961396
// Collect all the candidate PHINodes to be rewritten.
1397-
RewritePhiSet.emplace_back(PN, i, ExitValue, Inst, HighCost);
1397+
Instruction *InsertPt =
1398+
(isa<PHINode>(Inst) || isa<LandingPadInst>(Inst)) ?
1399+
&*Inst->getParent()->getFirstInsertionPt() : Inst;
1400+
RewritePhiSet.emplace_back(PN, i, ExitValue, InsertPt, HighCost);
13981401
}
13991402
}
14001403
}

llvm/test/Transforms/IndVarSimplify/rewrite-loop-exit-value.ll

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,4 +158,42 @@ exit:
158158
ret i32 %phi_indvar
159159
}
160160

161+
define i16 @pr57336(i16 %end, i16 %m) mustprogress {
162+
; CHECK-LABEL: @pr57336(
163+
; CHECK-NEXT: entry:
164+
; CHECK-NEXT: br label [[FOR_BODY:%.*]]
165+
; CHECK: for.body:
166+
; CHECK-NEXT: [[INC8:%.*]] = phi i16 [ [[INC:%.*]], [[FOR_BODY]] ], [ 0, [[ENTRY:%.*]] ]
167+
; CHECK-NEXT: [[INC]] = add nuw nsw i16 [[INC8]], 1
168+
; CHECK-NEXT: [[MUL:%.*]] = mul nsw i16 [[INC8]], [[M:%.*]]
169+
; CHECK-NEXT: [[CMP_NOT:%.*]] = icmp sgt i16 [[MUL]], [[END:%.*]]
170+
; CHECK-NEXT: br i1 [[CMP_NOT]], label [[CRIT_EDGE:%.*]], label [[FOR_BODY]]
171+
; CHECK: crit_edge:
172+
; CHECK-NEXT: [[TMP0:%.*]] = call i16 @llvm.smax.i16(i16 [[END]], i16 -1)
173+
; CHECK-NEXT: [[SMAX:%.*]] = add nsw i16 [[TMP0]], 1
174+
; CHECK-NEXT: [[TMP1:%.*]] = icmp ne i16 [[SMAX]], 0
175+
; CHECK-NEXT: [[UMIN:%.*]] = zext i1 [[TMP1]] to i16
176+
; CHECK-NEXT: [[TMP2:%.*]] = sub nsw i16 [[SMAX]], [[UMIN]]
177+
; CHECK-NEXT: [[UMAX:%.*]] = call i16 @llvm.umax.i16(i16 [[M]], i16 1)
178+
; CHECK-NEXT: [[TMP3:%.*]] = udiv i16 [[TMP2]], [[UMAX]]
179+
; CHECK-NEXT: [[TMP4:%.*]] = add i16 [[TMP3]], [[UMIN]]
180+
; CHECK-NEXT: ret i16 [[TMP4]]
181+
;
182+
entry:
183+
br label %for.body
184+
185+
for.body:
186+
%inc8 = phi i16 [ %inc, %for.body ], [ 0, %entry ]
187+
%inc137 = phi i32 [ %inc1, %for.body ], [ 0, %entry ]
188+
%inc1 = add nsw i32 %inc137, 1
189+
%inc = add nsw i16 %inc8, 1
190+
%mul = mul nsw i16 %m, %inc8
191+
%cmp.not = icmp slt i16 %end, %mul
192+
br i1 %cmp.not, label %crit_edge, label %for.body
193+
194+
crit_edge:
195+
%inc137.lcssa = phi i32 [ %inc137, %for.body ]
196+
%conv = trunc i32 %inc137.lcssa to i16
197+
ret i16 %conv
198+
}
161199

0 commit comments

Comments
 (0)