[PATCH] D129358: [IndVars] Eliminate redundant type cast between unsigned integer and float

Allen zhong via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 8 03:09:08 PDT 2022


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

This patch is similar to D129191 <https://reviews.llvm.org/D129191>, but extend for unsigned integer.


https://reviews.llvm.org/D129358

Files:
  llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
  llvm/test/Transforms/IndVarSimplify/floating-point-small-iv.ll


Index: llvm/test/Transforms/IndVarSimplify/floating-point-small-iv.ll
===================================================================
--- llvm/test/Transforms/IndVarSimplify/floating-point-small-iv.ll
+++ llvm/test/Transforms/IndVarSimplify/floating-point-small-iv.ll
@@ -3,7 +3,7 @@
 
 @array = dso_local global [16777219 x i32] zeroinitializer, align 4
 
-define void @small_const_bound(i32 %index) {
+define void @small_const_bound() {
 ; CHECK-LABEL: @small_const_bound(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
@@ -37,7 +37,7 @@
 }
 
 ; Negative test: The transform is *not* valid because there are too many significant bits
-define void @overflow_masked_const_bound(i32 %index) {
+define void @overflow_masked_const_bound() {
 ; CHECK-LABEL: @overflow_masked_const_bound(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
@@ -73,7 +73,7 @@
 }
 
 ; Negative test: Type mismatch between the integer IV and the fptosi result
-define void @mismatch_type_const(i32 %index) {
+define void @mismatch_type_const() {
 ;
 ; CHECK-LABEL: @mismatch_type_const(
 ; CHECK-NEXT:  entry:
@@ -110,3 +110,36 @@
 cleanup:                                          ; preds = %for.body
   ret void
 }
+
+define void @unsigned_const_bound() {
+; CHECK-LABEL: @unsigned_const_bound(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[FOR_BODY:%.*]]
+; CHECK:       for.body:
+; CHECK-NEXT:    [[IV_INT:%.*]] = phi i32 [ 100, [[ENTRY:%.*]] ], [ [[DEC_INT:%.*]], [[FOR_BODY]] ]
+; CHECK-NEXT:    [[IDXPROM:%.*]] = zext i32 [[IV_INT]] to i64
+; CHECK-NEXT:    [[ARRAYIDX:%.*]] = getelementptr inbounds [16777219 x i32], [16777219 x i32]* @array, i64 0, i64 [[IDXPROM]]
+; CHECK-NEXT:    store i32 [[IV_INT]], i32* [[ARRAYIDX]], align 4
+; CHECK-NEXT:    [[DEC_INT]] = add nsw i32 [[IV_INT]], -1
+; CHECK-NEXT:    [[CMP:%.*]] = icmp ugt i32 [[DEC_INT]], 0
+; CHECK-NEXT:    br i1 [[CMP]], label [[FOR_BODY]], label [[CLEANUP:%.*]]
+; CHECK:       cleanup:
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %for.body
+
+for.body:                                         ; preds = %for.body, %entry
+  %iv.int = phi i32 [ 100, %entry ], [ %dec.int, %for.body ]
+  %indvar.conv = sitofp i32 %iv.int to float
+  %conv = fptoui float %indvar.conv to i32
+  %idxprom = zext i32 %conv to i64
+  %arrayidx = getelementptr inbounds [16777219 x i32], [16777219 x i32]* @array, i64 0, i64 %idxprom
+  store i32 %conv, i32* %arrayidx, align 4
+  %dec.int = add nsw i32 %iv.int, -1
+  %cmp = icmp ugt i32 %dec.int, 0
+  br i1 %cmp, label %for.body, label %cleanup
+
+cleanup:                                          ; preds = %for.body
+  ret void
+}
Index: llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
+++ llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
@@ -685,11 +685,15 @@
   unsigned DestNumSigBits = UseInst->getType()->getFPMantissaWidth();
   if (IVRange.getActiveBits() <= DestNumSigBits) {
     for (User *U : UseInst->users()) {
-      // Match for fptosi of sitofp and with same type.
-      auto *CI = dyn_cast<FPToSIInst>(U);
+      // Match for fptosi/fptoui of sitofp and with same type.
+      auto *CI = dyn_cast<CastInst>(U);
       if (!CI || IVOperand->getType() != CI->getType())
         continue;
 
+      CastInst::CastOps Opcode = CI->getOpcode();
+      if (Opcode != CastInst::FPToSI && Opcode != CastInst::FPToUI)
+        continue;
+
       CI->replaceAllUsesWith(IVOperand);
       DeadInsts.push_back(CI);
       LLVM_DEBUG(dbgs() << "INDVARS: Replace IV user: " << *CI


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129358.443186.patch
Type: text/x-patch
Size: 3630 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220708/95fba4a8/attachment.bin>


More information about the llvm-commits mailing list