[PATCH] D129140: [IndVars] Directly use integer induction for FPToSI of float induction.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 5 04:50:12 PDT 2022


fhahn created this revision.
fhahn added reviewers: scanon, reames, nikic, spatel.
Herald added a subscriber: hiraditya.
Herald added a project: All.
fhahn requested review of this revision.
Herald added a project: LLVM.

When promoting a float induction to an integer one, we already proven
that all possible vales will be integers that can be converted back to
floats.

This makes fp -> int conversions of the float induction variable
redundant. Instead of unconditionally replacing all uses of the float
induction with a int -> fp conversion of the integer induction, simplify
fp -> int conversion to the integer induction directly.

Doing this in other passes would be a bit more tricky, as we would need
to prove the restricted range of the integer induction again separately.

Fixes #55505.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129140

Files:
  llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
  llvm/test/Transforms/IndVarSimplify/floating-point-iv.ll


Index: llvm/test/Transforms/IndVarSimplify/floating-point-iv.ll
===================================================================
--- llvm/test/Transforms/IndVarSimplify/floating-point-iv.ll
+++ llvm/test/Transforms/IndVarSimplify/floating-point-iv.ll
@@ -376,8 +376,8 @@
 ; CHECK:       loop:
 ; CHECK-NEXT:    [[FLOAT_IV_INT:%.*]] = phi i32 [ 1000, [[ENTRY:%.*]] ], [ [[FLOAT_IV_NEXT_INT:%.*]], [[LOOP]] ]
 ; CHECK-NEXT:    [[INDVAR_CONV:%.*]] = sitofp i32 [[FLOAT_IV_INT]] to float
-; CHECK-NEXT:    [[CONV:%.*]] = fptosi float [[INDVAR_CONV]] to i32
-; CHECK-NEXT:    [[IDXPROM:%.*]] = sext i32 [[CONV]] to i64
+; CHECK-NEXT:    [[CONV:%.*]] = fptosi float 1.000000e+03 to i32
+; CHECK-NEXT:    [[IDXPROM:%.*]] = sext i32 [[FLOAT_IV_INT]] to i64
 ; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds i32, ptr [[DST:%.*]], i64 [[IDXPROM]]
 ; CHECK-NEXT:    store float [[INDVAR_CONV]], ptr [[ARRAYIDX]], align 4
 ; CHECK-NEXT:    [[FLOAT_IV_NEXT_INT]] = add nsw i32 [[FLOAT_IV_INT]], -1
Index: llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -388,14 +388,24 @@
   // If the FP induction variable still has uses, this is because something else
   // in the loop uses its value.  In order to canonicalize the induction
   // variable, we chose to eliminate the IV and rewrite it in terms of an
-  // int->fp cast.
+  // int->fp cast. fp->int casts of the original induction can be directly
+  // simplified to use the integer induction .
   //
   // We give preference to sitofp over uitofp because it is faster on most
   // platforms.
   if (WeakPH) {
-    Value *Conv = new SIToFPInst(NewPHI, PN->getType(), "indvar.conv",
-                                 &*PN->getParent()->getFirstInsertionPt());
-    PN->replaceAllUsesWith(Conv);
+    Value *Conv = nullptr;
+    for (Use &U : make_early_inc_range(PN->uses())) {
+      if (isa<FPToSIInst>(U.getUser())) {
+        U.getUser()->replaceAllUsesWith(NewPHI);
+        continue;
+      }
+      if (!Conv)
+        Conv = new SIToFPInst(NewPHI, PN->getType(), "indvar.conv",
+                              &*PN->getParent()->getFirstInsertionPt());
+      U.set(Conv);
+    }
+
     RecursivelyDeleteTriviallyDeadInstructions(PN, TLI, MSSAU.get());
   }
   return true;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129140.442262.patch
Type: text/x-patch
Size: 2402 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220705/101a15e3/attachment.bin>


More information about the llvm-commits mailing list