Skip to content

Commit 34d380e

Browse files
committed
[IndVars] Add check of loop invariant for indirect use
We usually only check direct use instruction of IV, while the bitcast of 'ptrtoint ptr to i64' doesn't affect the result, so go a step further. Fix #59633. Reviewed By: markoshorro Differential Revision: https://reviews.llvm.org/D151877
1 parent 287f201 commit 34d380e

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

llvm/lib/Transforms/Utils/SimplifyIndVar.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -908,6 +908,14 @@ void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
908908
if (replaceIVUserWithLoopInvariant(UseInst))
909909
continue;
910910

911+
// Go further for the bitcast ''prtoint ptr to i64'
912+
if (isa<PtrToIntInst>(UseInst))
913+
for (Use &U : UseInst->uses()) {
914+
Instruction *User = cast<Instruction>(U.getUser());
915+
if (replaceIVUserWithLoopInvariant(User))
916+
break; // done replacing
917+
}
918+
911919
Instruction *IVOperand = UseOper.second;
912920
for (unsigned N = 0; IVOperand; ++N) {
913921
assert(N <= Simplified.size() && "runaway iteration");
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
2+
; RUN: opt < %s -passes=indvars -S | FileCheck %s
3+
4+
declare void @foo(i64 noundef)
5+
6+
; Function Attrs: mustprogress noreturn uwtable
7+
define void @pr59633(ptr noundef %ptr) {
8+
; CHECK-LABEL: @pr59633(
9+
; CHECK-NEXT: entry:
10+
; CHECK-NEXT: [[PTR1:%.*]] = ptrtoint ptr [[PTR:%.*]] to i64
11+
; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[PTR1]] to i4
12+
; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[TMP0]] to i64
13+
; CHECK-NEXT: br label [[WHILE_BODY:%.*]]
14+
; CHECK: while.body:
15+
; CHECK-NEXT: tail call void @foo(i64 noundef [[TMP1]])
16+
; CHECK-NEXT: br label [[WHILE_BODY]]
17+
;
18+
entry:
19+
br label %while.body
20+
21+
while.body: ; preds = %entry, %while.body
22+
%ptr.addr.0 = phi ptr [ %ptr, %entry ], [ %add.ptr, %while.body ]
23+
%0 = ptrtoint ptr %ptr.addr.0 to i64
24+
%and = and i64 %0, 15 ; loop invariant
25+
tail call void @foo(i64 noundef %and)
26+
%add.ptr = getelementptr inbounds i8, ptr %ptr.addr.0, i64 16
27+
br label %while.body
28+
}
29+

0 commit comments

Comments
 (0)