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

Markos Horro via cfe-commits cfe-commits at lists.llvm.org
Thu Nov 2 09:09:47 PDT 2023


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 <marcosh at amd.com>
Date: Thu, 2 Nov 2023 15:35:07 +0000
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<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
+}
+



More information about the cfe-commits mailing list