Skip to content

Commit 9d2903c

Browse files
authored
[IndVars] Add check of loop invariant for trunc instructions (#71072)
The same idea as in 34d380e, but considering truncation instructions. Improvement for #59633.
1 parent 6eb97f0 commit 9d2903c

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

llvm/lib/Transforms/Utils/SimplifyIndVar.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -909,8 +909,9 @@ void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
909909
if (replaceIVUserWithLoopInvariant(UseInst))
910910
continue;
911911

912-
// Go further for the bitcast ''prtoint ptr to i64'
913-
if (isa<PtrToIntInst>(UseInst))
912+
// Go further for the bitcast 'prtoint ptr to i64' or if the cast is done
913+
// by truncation
914+
if ((isa<PtrToIntInst>(UseInst)) || (isa<TruncInst>(UseInst)))
914915
for (Use &U : UseInst->uses()) {
915916
Instruction *User = cast<Instruction>(U.getUser());
916917
if (replaceIVUserWithLoopInvariant(User))

llvm/test/Transforms/IndVarSimplify/X86/eliminate-trunc.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,8 +503,7 @@ define void @test_11() {
503503
; CHECK: bb4:
504504
; CHECK-NEXT: br label [[BB6]]
505505
; CHECK: bb5:
506-
; CHECK-NEXT: [[_TMP24:%.*]] = icmp slt i16 poison, 0
507-
; CHECK-NEXT: br i1 [[_TMP24]], label [[BB5:%.*]], label [[BB5]]
506+
; CHECK-NEXT: br i1 poison, label [[BB5:%.*]], label [[BB5]]
508507
; CHECK: bb6:
509508
; CHECK-NEXT: br i1 false, label [[BB1]], label [[BB7:%.*]]
510509
; CHECK: bb7:
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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(i16 noundef)
5+
6+
; Function Attrs: mustprogress noreturn uwtable
7+
define void @bar(i64 noundef %ptr) {
8+
; CHECK-LABEL: @bar(
9+
; CHECK-NEXT: entry:
10+
; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[PTR:%.*]] to i4
11+
; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[TMP0]] to i16
12+
; CHECK-NEXT: br label [[WHILE_BODY:%.*]]
13+
; CHECK: while.body:
14+
; CHECK-NEXT: tail call void @foo(i16 noundef signext [[TMP1]])
15+
; CHECK-NEXT: br label [[WHILE_BODY]]
16+
;
17+
entry:
18+
br label %while.body
19+
20+
while.body: ; preds = %entry, %while.body
21+
%0 = phi i64 [ %ptr, %entry ], [ %add.ptr, %while.body ]
22+
%1 = trunc i64 %0 to i16
23+
%and = and i16 %1, 15 ; loop invariant
24+
tail call void @foo(i16 noundef signext %and)
25+
%add.ptr = add nsw i64 %0, 16
26+
br label %while.body
27+
}
28+

0 commit comments

Comments
 (0)