[llvm] 34d380e - [IndVars] Add check of loop invariant for indirect use

via llvm-commits llvm-commits at lists.llvm.org
Sat Jun 3 07:33:44 PDT 2023


Author: zhongyunde
Date: 2023-06-03T22:29:09+08:00
New Revision: 34d380e1f63a7e2cdb9ab1e6498f727fcd710a14

URL: https://github.com/llvm/llvm-project/commit/34d380e1f63a7e2cdb9ab1e6498f727fcd710a14
DIFF: https://github.com/llvm/llvm-project/commit/34d380e1f63a7e2cdb9ab1e6498f727fcd710a14.diff

LOG: [IndVars] Add check of loop invariant for indirect use

We usually only check direct use instruction of IV, while the
bitcast of 'ptrtoint ptr to i64' doesn't affect the result, so go
a step further.
Fix https://github.com/llvm/llvm-project/issues/59633.

Reviewed By: markoshorro
Differential Revision: https://reviews.llvm.org/D151877

Added: 
    llvm/test/Transforms/IndVarSimplify/pr59633.ll

Modified: 
    llvm/lib/Transforms/Utils/SimplifyIndVar.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
index 62821f149f444..a28916bc9baf3 100644
--- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
@@ -908,6 +908,14 @@ void SimplifyIndvar::simplifyUsers(PHINode *CurrIV, IVVisitor *V) {
     if (replaceIVUserWithLoopInvariant(UseInst))
       continue;
 
+    // Go further for the bitcast ''prtoint ptr to i64'
+    if (isa<PtrToIntInst>(UseInst))
+      for (Use &U : UseInst->uses()) {
+        Instruction *User = cast<Instruction>(U.getUser());
+        if (replaceIVUserWithLoopInvariant(User))
+          break; // done replacing
+      }
+
     Instruction *IVOperand = UseOper.second;
     for (unsigned N = 0; IVOperand; ++N) {
       assert(N <= Simplified.size() && "runaway iteration");

diff  --git a/llvm/test/Transforms/IndVarSimplify/pr59633.ll b/llvm/test/Transforms/IndVarSimplify/pr59633.ll
new file mode 100644
index 0000000000000..888a45b714dc6
--- /dev/null
+++ b/llvm/test/Transforms/IndVarSimplify/pr59633.ll
@@ -0,0 +1,29 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt < %s -passes=indvars -S | FileCheck %s
+
+declare void @foo(i64 noundef)
+
+; Function Attrs: mustprogress noreturn uwtable
+define void @pr59633(ptr noundef %ptr) {
+; CHECK-LABEL: @pr59633(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[PTR1:%.*]] = ptrtoint ptr [[PTR:%.*]] to i64
+; CHECK-NEXT:    [[TMP0:%.*]] = trunc i64 [[PTR1]] to i4
+; CHECK-NEXT:    [[TMP1:%.*]] = zext i4 [[TMP0]] to i64
+; CHECK-NEXT:    br label [[WHILE_BODY:%.*]]
+; CHECK:       while.body:
+; CHECK-NEXT:    tail call void @foo(i64 noundef [[TMP1]])
+; CHECK-NEXT:    br label [[WHILE_BODY]]
+;
+entry:
+  br label %while.body
+
+while.body:                                       ; preds = %entry, %while.body
+  %ptr.addr.0 = phi ptr [ %ptr, %entry ], [ %add.ptr, %while.body ]
+  %0 = ptrtoint ptr %ptr.addr.0 to i64
+  %and = and i64 %0, 15                           ; loop invariant
+  tail call void @foo(i64 noundef %and)
+  %add.ptr = getelementptr inbounds i8, ptr %ptr.addr.0, i64 16
+  br label %while.body
+}
+


        


More information about the llvm-commits mailing list