float comparison to double 'zero' constant can just be a float 'zero.'
authorJim Grosbach <grosbach@apple.com>
Fri, 30 Sep 2011 18:45:50 +0000 (18:45 +0000)
committerJim Grosbach <grosbach@apple.com>
Fri, 30 Sep 2011 18:45:50 +0000 (18:45 +0000)
InstCombine was incorrectly considering the conversion of the constant
zero to be unsafe.

We want to transform:
define float @bar(float %x) nounwind readnone optsize ssp {
  %conv = fpext float %x to double
  %cmp = fcmp olt double %conv, 0.000000e+00
  %conv1 = zext i1 %cmp to i32
  %conv2 = sitofp i32 %conv1 to float
  ret float %conv2
}

Into:
define float @bar(float %x) nounwind readnone optsize ssp {
  %cmp = fcmp olt float %x, 0.000000e+00   ; <---- This
  %conv1 = zext i1 %cmp to i32
  %conv2 = sitofp i32 %conv1 to float
  ret float %conv2
}

rdar://10215914

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@140869 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/InstCombine/InstCombineCompares.cpp
test/Transforms/InstCombine/fcmp.ll

index d2faab521522876a8afbe1df33f9be7373c07834..26432d2eed5e10b4edfa1c90719cbabec63e8574 100644 (file)
@@ -2837,10 +2837,13 @@ Instruction *InstCombiner::visitFCmpInst(FCmpInst &I) {
         APFloat F = RHSF->getValueAPF();
         F.convert(*Sem, APFloat::rmNearestTiesToEven, &Lossy);
 
-        // Avoid lossy conversions and denormals.
+        // Avoid lossy conversions and denormals. Zero is a special case
+        // that's OK to convert.
+        F.clearSign();
         if (!Lossy &&
-            F.compare(APFloat::getSmallestNormalized(*Sem)) !=
-                                                           APFloat::cmpLessThan)
+            ((F.compare(APFloat::getSmallestNormalized(*Sem)) !=
+                 APFloat::cmpLessThan) || F.isZero()))
+
           return new FCmpInst(I.getPredicate(), LHSExt->getOperand(0),
                               ConstantFP::get(RHSC->getContext(), F));
         break;
index 2eb4f058692d9e11fcd39ef26297be7493affb90..d08cbf574a231e76ea021ac75db76858a7e53415 100644 (file)
@@ -58,3 +58,14 @@ define i1 @test7(float %x) nounwind readnone ssp noredzone {
 ; CHECK: @test7
 ; CHECK-NEXT: fpext float %x to ppc_fp128
 }
+
+define float @test8(float %x) nounwind readnone optsize ssp {
+  %conv = fpext float %x to double
+  %cmp = fcmp olt double %conv, 0.000000e+00
+  %conv1 = zext i1 %cmp to i32
+  %conv2 = sitofp i32 %conv1 to float
+  ret float %conv2
+; Float comparison to zero shouldn't cast to double.
+; CHECK: @test8
+; CHECK-NEXT: fcmp olt float %x, 0.000000e+00
+}