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

Owen Anderson resistor at mac.com
Tue Dec 17 14:21:06 PST 2013


AFAIK this isn’t valid, because sqrt() can’t return a negative value.

—Owen

On Dec 17, 2013, at 1:44 PM, Matt Arsenault <Matthew.Arsenault at amd.com> wrote:

> 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
> +}
> <D2429.1.patch>_______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits





More information about the llvm-commits mailing list