[PATCH] Constant fold undefined sqrt to undef instead of 0

Matt Arsenault Matthew.Arsenault at amd.com
Tue Dec 17 13:44:13 PST 2013


Constant fold to undef for sqrt < -0.0. This is undefined, but the current behavior is to return 0. It makes more sense to fold to undef so this will fail in a more obvious way.

http://llvm-reviews.chandlerc.com/D2429

Files:
  lib/Analysis/ConstantFolding.cpp
  test/Transforms/InstCombine/fold-calls.ll

Index: lib/Analysis/ConstantFolding.cpp
===================================================================
--- lib/Analysis/ConstantFolding.cpp
+++ lib/Analysis/ConstantFolding.cpp
@@ -1433,10 +1433,11 @@
           return ConstantFoldFP(log10, V, Ty);
         else if (F->getIntrinsicID() == Intrinsic::sqrt &&
                  (Ty->isHalfTy() || Ty->isFloatTy() || Ty->isDoubleTy())) {
-          if (V >= -0.0)
-            return ConstantFoldFP(sqrt, V, Ty);
-          else // Undefined
-            return Constant::getNullValue(Ty);
+
+          if (V < -0.0) // Undefined
+            return UndefValue::get(Ty);
+
+          return ConstantFoldFP(sqrt, V, Ty);
         }
         break;
       case 's':
Index: test/Transforms/InstCombine/fold-calls.ll
===================================================================
--- test/Transforms/InstCombine/fold-calls.ll
+++ test/Transforms/InstCombine/fold-calls.ll
@@ -17,3 +17,21 @@
 }
 
 declare double @sin(double)
+
+
+declare float @llvm.sqrt.f32(float) nounwind readnone
+
+; Undefined for < -0.0, return undef
+define float @undef_sqrt_neg() nounwind readnone {
+; CHECK-LABEL: @undef_sqrt_neg(
+; CHECK-NEXT: ret float undef
+  %result = call float @llvm.sqrt.f32(float -1.0) nounwind readnone
+  ret float %result
+}
+
+define float @def_sqrt_neg0() nounwind readnone {
+; CHECK-LABEL: @def_sqrt_neg0(
+; CHECK-NEXT: ret float -0.0
+  %result = call float @llvm.sqrt.f32(float -0.0) nounwind readnone
+  ret float %result
+}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D2429.1.patch
Type: text/x-patch
Size: 1495 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20131217/41085b6c/attachment.bin>


More information about the llvm-commits mailing list