Skip to content

[IndVars] Add check of loop invariant for trunc instructions #71072

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 8, 2023
Merged

[IndVars] Add check of loop invariant for trunc instructions #71072

merged 7 commits into from
Nov 8, 2023

Conversation

markoshorro
Copy link
Contributor

The same idea as in 34d380e, but for truncation instructions.
Improvement for #59633.

@llvmbot
Copy link
Member

llvmbot commented Nov 2, 2023

@llvm/pr-subscribers-llvm-transforms

Author: Markos Horro (markoshorro)

Changes

The same idea as in 34d380e, 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 --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
+}
+

@markoshorro markoshorro removed the request for review from zhanghb97 November 2, 2023 16:12
@vfdff
Copy link
Contributor

vfdff commented Nov 6, 2023

LGTM

@markoshorro markoshorro merged commit 9d2903c into llvm:main Nov 8, 2023
@fhahn
Copy link
Contributor

fhahn commented Dec 19, 2023

Looks like this is causing #75938. Please take a look

@markoshorro
Copy link
Contributor Author

@fhahn yeah, thanks. I'm investigating this.

@fhahn
Copy link
Contributor

fhahn commented Dec 20, 2023

@markoshorro Great thanks. Please revert the patch if you won't be able to fix this today.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants