[Lldb-commits] [llvm] [lldb] [clang-tools-extra] [flang] [clang] [IndVars] Add check of loop invariant for trunc instructions (PR #71072)
markoshorro wrote: @fhahn yeah, thanks. I'm investigating this. https://github.com/llvm/llvm-project/pull/71072 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [clang-tools-extra] [llvm] [clang] [lldb] [flang] [IndVars] Add check of loop invariant for trunc instructions (PR #71072)
https://github.com/markoshorro ready_for_review https://github.com/llvm/llvm-project/pull/71072 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [llvm] [clang] [clang-tools-extra] [lldb] [flang] [IndVars] Add check of loop invariant for trunc instructions (PR #71072)
https://github.com/markoshorro created https://github.com/llvm/llvm-project/pull/71072 The same idea as in 34d380e1f63a7e2cdb9ab1e6498f727fcd710a14, but for truncation instructions. Improvement for #59633. >From 0c5299adb30888aa0dfd7c3106547a69606d5ab1 Mon Sep 17 00:00:00 2001 From: Marcos Horro Date: Thu, 2 Nov 2023 15:35:07 + Subject: [PATCH] [IndVars] Truncation also considered as bitcast optimization --- llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | 5 ++-- .../Transforms/IndVarSimplify/casted-trunc.ll | 28 +++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 llvm/test/Transforms/IndVarSimplify/casted-trunc.ll diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp index a23ac41acaa58aa..740f726cb06b148 100644 --- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -908,8 +908,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/casted-trunc.ll b/llvm/test/Transforms/IndVarSimplify/casted-trunc.ll new file mode 100644 index 000..ef9db0b5774dada --- /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 +} + ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [llvm] [clang-tools-extra] [flang] [lldb] [clang] [IndVars] Add check of loop invariant for trunc instructions (PR #71072)
@@ -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) markoshorro wrote: Having something like: `declare void @foo(i2 noundef) entry: %ptr.addr.0 = ptrtoint ptr %ptr to i64 br label %while.body while.body: ; preds = %entry, %while.body %0 = phi i64 [ %ptr.addr.0, %entry ], [ %add.ptr, %while.body ] %1 = trunc i64 %0 to i2 %and = and i2 %1, 15 ; loop invariant tail call void @foo(i2 noundef signext %and) %add.ptr = add nsw i64 %0, 16 br label %while.body }` With this change, the IR generated is the following: `declare void @foo(i2 noundef) define void @bar(ptr noundef %ptr) { entry: %ptr.addr.0 = ptrtoint ptr %ptr to i64 %0 = trunc i64 %ptr.addr.0 to i2 br label %while.body while.body: ; preds = %while.body, %entry %and = and i2 %0, -1 tail call void @foo(i2 noundef signext %and) br label %while.body }` This is valid to me, when you say that `trunc` can affect the result, do you have any example in mind? Thanks! https://github.com/llvm/llvm-project/pull/71072 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [llvm] [clang-tools-extra] [flang] [lldb] [clang] [IndVars] Add check of loop invariant for trunc instructions (PR #71072)
https://github.com/markoshorro edited https://github.com/llvm/llvm-project/pull/71072 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [clang] [flang] [llvm] [clang-tools-extra] [lldb] [IndVars] Add check of loop invariant for trunc instructions (PR #71072)
@@ -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) markoshorro wrote: What I've also observed from the regression is that we have one test failing: `test_11` in `Transforms/IndVarSimplify/X86/eliminate-trunc.ll`. In concrete, in the function the statement failing is: ```ll define void @test_11() { ... bb5: ; preds = %bb5, %bb5 %_tmp24 = icmp slt i16 %_tmp15, 0 br i1 %_tmp24, label %bb5, label %bb5 ... } ``` which instead of the expected (as it is currently in the test): ```ll ; CHECK: bb5: ; CHECK-NEXT:[[_TMP24:%.*]] = icmp slt i16 poison, 0 ; CHECK-NEXT:br i1 [[_TMP24]], label [[BB5:%.*]], label [[BB5]] ``` It gets simplified to: ```ll bb5: ; preds = %bb5, %bb5 br i1 poison, label %bb5, label %bb5 ``` I'm assuming this simplification is correct, but I don't understand why this is happening with this change. https://github.com/llvm/llvm-project/pull/71072 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [lldb] [clang-tools-extra] [clang] [llvm] [flang] [IndVars] Add check of loop invariant for trunc instructions (PR #71072)
https://github.com/markoshorro edited https://github.com/llvm/llvm-project/pull/71072 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [lldb] [clang] [flang] [clang-tools-extra] [llvm] [IndVars] Add check of loop invariant for trunc instructions (PR #71072)
https://github.com/markoshorro updated https://github.com/llvm/llvm-project/pull/71072 >From 0c5299adb30888aa0dfd7c3106547a69606d5ab1 Mon Sep 17 00:00:00 2001 From: Marcos Horro Date: Thu, 2 Nov 2023 15:35:07 + Subject: [PATCH 1/2] [IndVars] Truncation also considered as bitcast optimization --- llvm/lib/Transforms/Utils/SimplifyIndVar.cpp | 5 ++-- .../Transforms/IndVarSimplify/casted-trunc.ll | 28 +++ 2 files changed, 31 insertions(+), 2 deletions(-) create mode 100644 llvm/test/Transforms/IndVarSimplify/casted-trunc.ll diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp index a23ac41acaa58aa..740f726cb06b148 100644 --- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp +++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp @@ -908,8 +908,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/casted-trunc.ll b/llvm/test/Transforms/IndVarSimplify/casted-trunc.ll new file mode 100644 index 000..ef9db0b5774dada --- /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 +} + >From 6586977812ed9e5f9f99fecf2d0881e788fe06ea Mon Sep 17 00:00:00 2001 From: Marcos Horro Date: Tue, 7 Nov 2023 17:20:48 + Subject: [PATCH 2/2] Fixing test on X86 --- llvm/test/Transforms/IndVarSimplify/X86/eliminate-trunc.ll | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/llvm/test/Transforms/IndVarSimplify/X86/eliminate-trunc.ll b/llvm/test/Transforms/IndVarSimplify/X86/eliminate-trunc.ll index e0c31fdbaa4fb62..e16f429b757987b 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: ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits
[Lldb-commits] [clang-tools-extra] [clang] [llvm] [lldb] [flang] [IndVars] Add check of loop invariant for trunc instructions (PR #71072)
https://github.com/markoshorro closed https://github.com/llvm/llvm-project/pull/71072 ___ lldb-commits mailing list lldb-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/lldb-commits