Skip to content

Commit d057a97

Browse files
committed
[LoopPeel] Handle non-local instructions/arguments when updating exit values
1 parent 7e1fa09 commit d057a97

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

llvm/lib/Transforms/Utils/LoopPeel.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1257,7 +1257,11 @@ bool llvm::peelLoop(Loop *L, unsigned PeelCount, bool PeelLast, LoopInfo *LI,
12571257
// Now adjust users of the original exit values by replacing them with the
12581258
// exit value from the peeled iteration and remove them.
12591259
for (const auto &[P, E] : ExitValues) {
1260-
P->replaceAllUsesWith(isa<Constant>(E) ? E : &*VMap.lookup(E));
1260+
Instruction *ExitInst = dyn_cast<Instruction>(E);
1261+
if (ExitInst && L->contains(ExitInst))
1262+
P->replaceAllUsesWith(&*VMap[ExitInst]);
1263+
else
1264+
P->replaceAllUsesWith(E);
12611265
P->eraseFromParent();
12621266
}
12631267
formLCSSA(*L, DT, LI, SE);

llvm/test/Transforms/LoopUnroll/unroll-and-peel-last-iteration.ll

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,47 @@ exit:
7171
ret i32 %1
7272
}
7373

74+
; Test case for https://github.com/llvm/llvm-project/issues/142895.
75+
76+
define i32 @pr142895_exit_value_is_arg(i32 %arg) {
77+
entry:
78+
br label %for.cond.preheader
79+
80+
for.cond.preheader:
81+
%indvar = phi i32 [ 0, %entry ], [ %inc, %for.cond.preheader ]
82+
%cmp1 = icmp eq i32 %indvar, 32
83+
%sel = select i1 %cmp1, i32 0, i32 0
84+
%sub = sub i32 0, 0
85+
%xor = xor i32 0, 0
86+
%inc = add i32 %indvar, 1
87+
%exitcond = icmp ne i32 %inc, 33
88+
br i1 %exitcond, label %for.cond.preheader, label %for.cond.cleanup
89+
90+
for.cond.cleanup:
91+
%exit.lcssa = phi i32 [ %arg, %for.cond.preheader ]
92+
ret i32 %exit.lcssa
93+
}
94+
95+
define i32 @pr142895_exit_value_is_inst(i32 %arg) {
96+
entry:
97+
%mul = mul i32 %arg, 7
98+
br label %for.cond.preheader
99+
100+
for.cond.preheader:
101+
%indvar = phi i32 [ 0, %entry ], [ %inc, %for.cond.preheader ]
102+
%cmp1 = icmp eq i32 %indvar, 32
103+
%sel = select i1 %cmp1, i32 0, i32 0
104+
%sub = sub i32 0, 0
105+
%xor = xor i32 0, 0
106+
%inc = add i32 %indvar, 1
107+
%exitcond = icmp ne i32 %inc, 33
108+
br i1 %exitcond, label %for.cond.preheader, label %for.cond.cleanup
109+
110+
for.cond.cleanup:
111+
%exit.lcssa = phi i32 [ %mul, %for.cond.preheader ]
112+
ret i32 %exit.lcssa
113+
}
114+
74115
declare void @foo(i32)
75116
;.
76117
; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]]}

0 commit comments

Comments
 (0)