1 ; RUN: opt < %s -instcombine -S | FileCheck %s
3 ; Make sure all library calls are eliminated when the input is known positive.
5 declare float @fabsf(float)
6 declare double @fabs(double)
7 declare fp128 @fabsl(fp128)
9 define float @square_fabs_call_f32(float %x) {
10 %mul = fmul float %x, %x
11 %fabsf = tail call float @fabsf(float %mul)
14 ; CHECK-LABEL: square_fabs_call_f32(
15 ; CHECK-NEXT: %mul = fmul float %x, %x
16 ; CHECK-NEXT: ret float %mul
19 define double @square_fabs_call_f64(double %x) {
20 %mul = fmul double %x, %x
21 %fabs = tail call double @fabs(double %mul)
24 ; CHECK-LABEL: square_fabs_call_f64(
25 ; CHECK-NEXT: %mul = fmul double %x, %x
26 ; CHECK-NEXT: ret double %mul
29 define fp128 @square_fabs_call_f128(fp128 %x) {
30 %mul = fmul fp128 %x, %x
31 %fabsl = tail call fp128 @fabsl(fp128 %mul)
34 ; CHECK-LABEL: square_fabs_call_f128(
35 ; CHECK-NEXT: %mul = fmul fp128 %x, %x
36 ; CHECK-NEXT: ret fp128 %mul
39 ; Make sure all intrinsic calls are eliminated when the input is known positive.
41 declare float @llvm.fabs.f32(float)
42 declare double @llvm.fabs.f64(double)
43 declare fp128 @llvm.fabs.f128(fp128)
44 declare <4 x float> @llvm.fabs.v4f32(<4 x float>)
46 define float @square_fabs_intrinsic_f32(float %x) {
47 %mul = fmul float %x, %x
48 %fabsf = tail call float @llvm.fabs.f32(float %mul)
51 ; CHECK-LABEL: square_fabs_intrinsic_f32(
52 ; CHECK-NEXT: %mul = fmul float %x, %x
53 ; CHECK-NEXT: ret float %mul
56 define double @square_fabs_intrinsic_f64(double %x) {
57 %mul = fmul double %x, %x
58 %fabs = tail call double @llvm.fabs.f64(double %mul)
61 ; CHECK-LABEL: square_fabs_intrinsic_f64(
62 ; CHECK-NEXT: %mul = fmul double %x, %x
63 ; CHECK-NEXT: ret double %mul
66 define fp128 @square_fabs_intrinsic_f128(fp128 %x) {
67 %mul = fmul fp128 %x, %x
68 %fabsl = tail call fp128 @llvm.fabs.f128(fp128 %mul)
71 ; CHECK-LABEL: square_fabs_intrinsic_f128(
72 ; CHECK-NEXT: %mul = fmul fp128 %x, %x
73 ; CHECK-NEXT: ret fp128 %mul
76 ; Shrinking a library call to a smaller type should not be inhibited by nor inhibit the square optimization.
78 define 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
85 ; CHECK-LABEL: square_fabs_shrink_call1(
86 ; CHECK-NEXT: %trunc = fmul float %x, %x
87 ; CHECK-NEXT: ret float %trunc
90 define 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
97 ; CHECK-LABEL: square_fabs_shrink_call2(
98 ; CHECK-NEXT: %sq = fmul float %x, %x
99 ; CHECK-NEXT: ret float %sq
102 ; A scalar fabs op makes the sign bit zero, so masking off all of the other bits means we can return zero.
104 define 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
110 ; CHECK-LABEL: fabs_value_tracking_f32(
114 ; TODO: A vector fabs op makes the sign bits zero, so masking off all of the other bits means we can return zero.
116 define <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>
122 ; CHECK-LABEL: fabs_value_tracking_v4f32(
123 ; CHECK: ret <4 x i32> %and