1; RUN: opt < %s -instcombine -S | FileCheck %s 2 3; Make sure all library calls are eliminated when the input is known positive. 4 5declare float @fabsf(float) 6declare double @fabs(double) 7declare fp128 @fabsl(fp128) 8 9define float @square_fabs_call_f32(float %x) { 10 %mul = fmul float %x, %x 11 %fabsf = tail call float @fabsf(float %mul) 12 ret float %fabsf 13 14; CHECK-LABEL: square_fabs_call_f32( 15; CHECK-NEXT: %mul = fmul float %x, %x 16; CHECK-NEXT: ret float %mul 17} 18 19define double @square_fabs_call_f64(double %x) { 20 %mul = fmul double %x, %x 21 %fabs = tail call double @fabs(double %mul) 22 ret double %fabs 23 24; CHECK-LABEL: square_fabs_call_f64( 25; CHECK-NEXT: %mul = fmul double %x, %x 26; CHECK-NEXT: ret double %mul 27} 28 29define fp128 @square_fabs_call_f128(fp128 %x) { 30 %mul = fmul fp128 %x, %x 31 %fabsl = tail call fp128 @fabsl(fp128 %mul) 32 ret fp128 %fabsl 33 34; CHECK-LABEL: square_fabs_call_f128( 35; CHECK-NEXT: %mul = fmul fp128 %x, %x 36; CHECK-NEXT: ret fp128 %mul 37} 38 39; Make sure all intrinsic calls are eliminated when the input is known positive. 40 41declare float @llvm.fabs.f32(float) 42declare double @llvm.fabs.f64(double) 43declare fp128 @llvm.fabs.f128(fp128) 44declare <4 x float> @llvm.fabs.v4f32(<4 x float>) 45 46define float @square_fabs_intrinsic_f32(float %x) { 47 %mul = fmul float %x, %x 48 %fabsf = tail call float @llvm.fabs.f32(float %mul) 49 ret float %fabsf 50 51; CHECK-LABEL: square_fabs_intrinsic_f32( 52; CHECK-NEXT: %mul = fmul float %x, %x 53; CHECK-NEXT: ret float %mul 54} 55 56define double @square_fabs_intrinsic_f64(double %x) { 57 %mul = fmul double %x, %x 58 %fabs = tail call double @llvm.fabs.f64(double %mul) 59 ret double %fabs 60 61; CHECK-LABEL: square_fabs_intrinsic_f64( 62; CHECK-NEXT: %mul = fmul double %x, %x 63; CHECK-NEXT: ret double %mul 64} 65 66define fp128 @square_fabs_intrinsic_f128(fp128 %x) { 67 %mul = fmul fp128 %x, %x 68 %fabsl = tail call fp128 @llvm.fabs.f128(fp128 %mul) 69 ret fp128 %fabsl 70 71; CHECK-LABEL: square_fabs_intrinsic_f128( 72; CHECK-NEXT: %mul = fmul fp128 %x, %x 73; CHECK-NEXT: ret fp128 %mul 74} 75 76; Shrinking a library call to a smaller type should not be inhibited by nor inhibit the square optimization. 77 78define float @square_fabs_shrink_call1(float %x) { 79 %ext = fpext float %x to double 80 %sq = fmul double %ext, %ext 81 %fabs = call double @fabs(double %sq) 82 %trunc = fptrunc double %fabs to float 83 ret float %trunc 84 85; CHECK-LABEL: square_fabs_shrink_call1( 86; CHECK-NEXT: %trunc = fmul float %x, %x 87; CHECK-NEXT: ret float %trunc 88} 89 90define float @square_fabs_shrink_call2(float %x) { 91 %sq = fmul float %x, %x 92 %ext = fpext float %sq to double 93 %fabs = call double @fabs(double %ext) 94 %trunc = fptrunc double %fabs to float 95 ret float %trunc 96 97; CHECK-LABEL: square_fabs_shrink_call2( 98; CHECK-NEXT: %sq = fmul float %x, %x 99; CHECK-NEXT: ret float %sq 100} 101 102; A scalar fabs op makes the sign bit zero, so masking off all of the other bits means we can return zero. 103 104define i32 @fabs_value_tracking_f32(float %x) { 105 %call = call float @llvm.fabs.f32(float %x) 106 %bc = bitcast float %call to i32 107 %and = and i32 %bc, 2147483648 108 ret i32 %and 109 110; CHECK-LABEL: fabs_value_tracking_f32( 111; CHECK: ret i32 0 112} 113 114; TODO: A vector fabs op makes the sign bits zero, so masking off all of the other bits means we can return zero. 115 116define <4 x i32> @fabs_value_tracking_v4f32(<4 x float> %x) { 117 %call = call <4 x float> @llvm.fabs.v4f32(<4 x float> %x) 118 %bc = bitcast <4 x float> %call to <4 x i32> 119 %and = and <4 x i32> %bc, <i32 2147483648, i32 2147483648, i32 2147483648, i32 2147483648> 120 ret <4 x i32> %and 121 122; CHECK-LABEL: fabs_value_tracking_v4f32( 123; CHECK: ret <4 x i32> %and 124} 125 126