[flang-commits] [lldb] [clang] [flang] [clang-tools-extra] [llvm] [IndVars] Add check of loop invariant for trunc instructions (PR #71072)

Markos Horro via flang-commits flang-commits at lists.llvm.org
Tue Nov 7 09:23:18 PST 2023


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 <marcosh at amd.com>
Date: Thu, 2 Nov 2023 15:35:07 +0000
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<PtrToIntInst>(UseInst))
+    // Go further for the bitcast 'prtoint ptr to i64' or if the cast is done
+    // by truncation
+    if ((isa<PtrToIntInst>(UseInst)) || (isa<TruncInst>(UseInst)))
       for (Use &U : UseInst->uses()) {
         Instruction *User = cast<Instruction>(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 000000000000000..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 <marcosh at amd.com>
Date: Tue, 7 Nov 2023 17:20:48 +0000
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:



More information about the flang-commits mailing list