diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp index ae3644183a735..692242fff0825 100644 --- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -909,8 +909,9 @@ void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) { if (replaceIVUserWithLoopInvariant(UseInst)) continue; - // Go further for the bitcast ''prtoint ptr to i64' - if (isa(UseInst)) + // Go further for the bitcast 'prtoint ptr to i64' or if the cast is done + // by truncation + if ((isa(UseInst)) || (isa(UseInst))) for (Use &U : UseInst->uses()) { Instruction *User = cast(U.getUser()); if (replaceIVUserWithLoopInvariant(User)) diff --git a/llvm/test/Transforms/IndVarSimplify/X86/eliminate-trunc.ll b/llvm/test/Transforms/IndVarSimplify/X86/eliminate-trunc.ll index e0c31fdbaa4fb..e16f429b75798 100644 --- a/llvm/test/Transforms/IndVarSimplify/X86/eliminate-trunc.ll +++ b/llvm/test/Transforms/IndVarSimplify/X86/eliminate-trunc.ll @@ -503,8 +503,7 @@ define void @test_11() { ; CHECK: bb4: ; CHECK-NEXT: br label [[BB6]] ; CHECK: bb5: -; CHECK-NEXT: [[_TMP24:%.*]] = icmp slt i16 poison, 0 -; CHECK-NEXT: br i1 [[_TMP24]], label [[BB5:%.*]], label [[BB5]] +; CHECK-NEXT: br i1 poison, label [[BB5:%.*]], label [[BB5]] ; CHECK: bb6: ; CHECK-NEXT: br i1 false, label [[BB1]], label [[BB7:%.*]] ; CHECK: bb7: diff --git a/llvm/test/Transforms/IndVarSimplify/casted-trunc.ll b/llvm/test/Transforms/IndVarSimplify/casted-trunc.ll new file mode 100644 index 0000000000000..ef9db0b5774da --- /dev/null +++ b/llvm/test/Transforms/IndVarSimplify/casted-trunc.ll @@ -0,0 +1,28 @@ +; NOTE: Assertions have been autogenerated by utils/update_test_checks.py +; RUN: opt < %s -passes=indvars -S | FileCheck %s + +declare void @foo(i16 noundef) + +; Function Attrs: mustprogress noreturn uwtable +define void @bar(i64 noundef %ptr) { +; CHECK-LABEL: @bar( +; CHECK-NEXT: entry: +; CHECK-NEXT: [[TMP0:%.*]] = trunc i64 [[PTR:%.*]] to i4 +; CHECK-NEXT: [[TMP1:%.*]] = zext i4 [[TMP0]] to i16 +; CHECK-NEXT: br label [[WHILE_BODY:%.*]] +; CHECK: while.body: +; CHECK-NEXT: tail call void @foo(i16 noundef signext [[TMP1]]) +; CHECK-NEXT: br label [[WHILE_BODY]] +; +entry: + br label %while.body + +while.body: ; preds = %entry, %while.body + %0 = phi i64 [ %ptr, %entry ], [ %add.ptr, %while.body ] + %1 = trunc i64 %0 to i16 + %and = and i16 %1, 15 ; loop invariant + tail call void @foo(i16 noundef signext %and) + %add.ptr = add nsw i64 %0, 16 + br label %while.body +} +