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

Allen zhong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 1 04:50:49 PDT 2023


Allen updated this revision to Diff 527366.

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D151877/new/

https://reviews.llvm.org/D151877

Files:
  llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
  llvm/test/Transforms/IndVarSimplify/pr59633.ll


Index: llvm/test/Transforms/IndVarSimplify/pr59633.ll
===================================================================
--- /dev/null
+++ 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
+}
+
Index: llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
+++ llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
@@ -908,6 +908,14 @@
     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");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151877.527366.patch
Type: text/x-patch
Size: 2045 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230601/ff154269/attachment.bin>


More information about the llvm-commits mailing list