-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Conversation
pulling master
merging master
merging master
@llvm/pr-subscribers-llvm-transforms Author: Markos Horro (markoshorro) ChangesThe same idea as in 34d380e, but for truncation instructions. Full diff: https://github.com/llvm/llvm-project/pull/71072.diff 2 Files Affected:
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
+}
+
|
LGTM |
Looks like this is causing #75938. Please take a look |
@fhahn yeah, thanks. I'm investigating this. |
@markoshorro Great thanks. Please revert the patch if you won't be able to fix this today. |
The same idea as in 34d380e, but for truncation instructions.
Improvement for #59633.