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

via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 2 09:12:10 PDT 2023


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-transforms

Author: Markos Horro (markoshorro)

<details>
<summary>Changes</summary>

The same idea as in 34d380e1f63a7e2cdb9ab1e6498f727fcd710a14, but for truncation instructions.
Improvement for #<!-- -->59633.

---
Full diff: https://github.com/llvm/llvm-project/pull/71072.diff


2 Files Affected:

- (modified) llvm/lib/Transforms/Utils/SimplifyIndVar.cpp (+3-2) 
- (added) llvm/test/Transforms/IndVarSimplify/casted-trunc.ll (+28) 


``````````diff
diff --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
index ae3644183a735bc..692242fff082583 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<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
+}
+

``````````

</details>


https://github.com/llvm/llvm-project/pull/71072


More information about the llvm-commits mailing list