1; Test that the pow library call simplifier works correctly. 2; 3; RUN: opt < %s -instcombine -S | FileCheck %s 4; RUN: opt -instcombine -S < %s -mtriple=x86_64-apple-macosx10.9 | FileCheck %s --check-prefix=CHECK-EXP10 5; RUN: opt -instcombine -S < %s -mtriple=arm-apple-ios7.0 | FileCheck %s --check-prefix=CHECK-EXP10 6; RUN: opt -instcombine -S < %s -mtriple=x86_64-apple-macosx10.8 | FileCheck %s --check-prefix=CHECK-NO-EXP10 7; RUN: opt -instcombine -S < %s -mtriple=arm-apple-ios6.0 | FileCheck %s --check-prefix=CHECK-NO-EXP10 8; RUN: opt -instcombine -S < %s -mtriple=x86_64-netbsd | FileCheck %s --check-prefix=CHECK-NO-EXP10 9; RUN: opt -instcombine -S < %s -mtriple=arm-apple-tvos9.0 | FileCheck %s --check-prefix=CHECK-EXP10 10; RUN: opt -instcombine -S < %s -mtriple=arm-apple-watchos2.0 | FileCheck %s --check-prefix=CHECK-EXP10 11; rdar://7251832 12 13; NOTE: The readonly attribute on the pow call should be preserved 14; in the cases below where pow is transformed into another function call. 15 16declare float @powf(float, float) nounwind readonly 17declare double @pow(double, double) nounwind readonly 18 19; Check pow(1.0, x) -> 1.0. 20 21define float @test_simplify1(float %x) { 22; CHECK-LABEL: @test_simplify1( 23 %retval = call float @powf(float 1.0, float %x) 24 ret float %retval 25; CHECK-NEXT: ret float 1.000000e+00 26} 27 28define double @test_simplify2(double %x) { 29; CHECK-LABEL: @test_simplify2( 30 %retval = call double @pow(double 1.0, double %x) 31 ret double %retval 32; CHECK-NEXT: ret double 1.000000e+00 33} 34 35; Check pow(2.0, x) -> exp2(x). 36 37define float @test_simplify3(float %x) { 38; CHECK-LABEL: @test_simplify3( 39 %retval = call float @powf(float 2.0, float %x) 40; CHECK-NEXT: [[EXP2F:%[a-z0-9]+]] = call float @exp2f(float %x) [[NUW_RO:#[0-9]+]] 41 ret float %retval 42; CHECK-NEXT: ret float [[EXP2F]] 43} 44 45define double @test_simplify4(double %x) { 46; CHECK-LABEL: @test_simplify4( 47 %retval = call double @pow(double 2.0, double %x) 48; CHECK-NEXT: [[EXP2:%[a-z0-9]+]] = call double @exp2(double %x) [[NUW_RO]] 49 ret double %retval 50; CHECK-NEXT: ret double [[EXP2]] 51} 52 53; Check pow(x, 0.0) -> 1.0. 54 55define float @test_simplify5(float %x) { 56; CHECK-LABEL: @test_simplify5( 57 %retval = call float @powf(float %x, float 0.0) 58 ret float %retval 59; CHECK-NEXT: ret float 1.000000e+00 60} 61 62define double @test_simplify6(double %x) { 63; CHECK-LABEL: @test_simplify6( 64 %retval = call double @pow(double %x, double 0.0) 65 ret double %retval 66; CHECK-NEXT: ret double 1.000000e+00 67} 68 69; Check pow(x, 0.5) -> fabs(sqrt(x)), where x != -infinity. 70 71define float @test_simplify7(float %x) { 72; CHECK-LABEL: @test_simplify7( 73 %retval = call float @powf(float %x, float 0.5) 74; CHECK-NEXT: [[SQRTF:%[a-z0-9]+]] = call float @sqrtf(float %x) [[NUW_RO]] 75; CHECK-NEXT: [[FABSF:%[a-z0-9]+]] = call float @fabsf(float [[SQRTF]]) [[NUW_RO]] 76; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq float %x, 0xFFF0000000000000 77; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], float 0x7FF0000000000000, float [[FABSF]] 78 ret float %retval 79; CHECK-NEXT: ret float [[SELECT]] 80} 81 82define double @test_simplify8(double %x) { 83; CHECK-LABEL: @test_simplify8( 84 %retval = call double @pow(double %x, double 0.5) 85; CHECK-NEXT: [[SQRT:%[a-z0-9]+]] = call double @sqrt(double %x) [[NUW_RO]] 86; CHECK-NEXT: [[FABS:%[a-z0-9]+]] = call double @fabs(double [[SQRT]]) [[NUW_RO]] 87; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq double %x, 0xFFF0000000000000 88; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], double 0x7FF0000000000000, double [[FABS]] 89 ret double %retval 90; CHECK-NEXT: ret double [[SELECT]] 91} 92 93; Check pow(-infinity, 0.5) -> +infinity. 94 95define float @test_simplify9(float %x) { 96; CHECK-LABEL: @test_simplify9( 97 %retval = call float @powf(float 0xFFF0000000000000, float 0.5) 98 ret float %retval 99; CHECK-NEXT: ret float 0x7FF0000000000000 100} 101 102define double @test_simplify10(double %x) { 103; CHECK-LABEL: @test_simplify10( 104 %retval = call double @pow(double 0xFFF0000000000000, double 0.5) 105 ret double %retval 106; CHECK-NEXT: ret double 0x7FF0000000000000 107} 108 109; Check pow(x, 1.0) -> x. 110 111define float @test_simplify11(float %x) { 112; CHECK-LABEL: @test_simplify11( 113 %retval = call float @powf(float %x, float 1.0) 114 ret float %retval 115; CHECK-NEXT: ret float %x 116} 117 118define double @test_simplify12(double %x) { 119; CHECK-LABEL: @test_simplify12( 120 %retval = call double @pow(double %x, double 1.0) 121 ret double %retval 122; CHECK-NEXT: ret double %x 123} 124 125; Check pow(x, 2.0) -> x*x. 126 127define float @test_simplify13(float %x) { 128; CHECK-LABEL: @test_simplify13( 129 %retval = call float @powf(float %x, float 2.0) 130; CHECK-NEXT: [[SQUARE:%[a-z0-9]+]] = fmul float %x, %x 131 ret float %retval 132; CHECK-NEXT: ret float [[SQUARE]] 133} 134 135define double @test_simplify14(double %x) { 136; CHECK-LABEL: @test_simplify14( 137 %retval = call double @pow(double %x, double 2.0) 138; CHECK-NEXT: [[SQUARE:%[a-z0-9]+]] = fmul double %x, %x 139 ret double %retval 140; CHECK-NEXT: ret double [[SQUARE]] 141} 142 143; Check pow(x, -1.0) -> 1.0/x. 144 145define float @test_simplify15(float %x) { 146; CHECK-LABEL: @test_simplify15( 147 %retval = call float @powf(float %x, float -1.0) 148; CHECK-NEXT: [[RECIPROCAL:%[a-z0-9]+]] = fdiv float 1.000000e+00, %x 149 ret float %retval 150; CHECK-NEXT: ret float [[RECIPROCAL]] 151} 152 153define double @test_simplify16(double %x) { 154; CHECK-LABEL: @test_simplify16( 155 %retval = call double @pow(double %x, double -1.0) 156; CHECK-NEXT: [[RECIPROCAL:%[a-z0-9]+]] = fdiv double 1.000000e+00, %x 157 ret double %retval 158; CHECK-NEXT: ret double [[RECIPROCAL]] 159} 160 161declare double @llvm.pow.f64(double %Val, double %Power) 162define double @test_simplify17(double %x) { 163; CHECK-LABEL: @test_simplify17( 164 %retval = call double @llvm.pow.f64(double %x, double 0.5) 165; CHECK-NEXT: [[SQRT:%[a-z0-9]+]] = call double @sqrt(double %x) 166; CHECK-NEXT: [[FABS:%[a-z0-9]+]] = call double @fabs(double [[SQRT]]) 167; CHECK-NEXT: [[FCMP:%[a-z0-9]+]] = fcmp oeq double %x, 0xFFF0000000000000 168; CHECK-NEXT: [[SELECT:%[a-z0-9]+]] = select i1 [[FCMP]], double 0x7FF0000000000000, double [[FABS]] 169 ret double %retval 170; CHECK-NEXT: ret double [[SELECT]] 171} 172 173; Check pow(10.0, x) -> __exp10(x) on OS X 10.9+ and iOS 7.0+. 174 175define float @test_simplify18(float %x) { 176; CHECK-LABEL: @test_simplify18( 177 %retval = call float @powf(float 10.0, float %x) 178; CHECK-EXP10: [[EXP10F:%[_a-z0-9]+]] = call float @__exp10f(float %x) [[NUW_RO:#[0-9]+]] 179 ret float %retval 180; CHECK-EXP10: ret float [[EXP10F]] 181; CHECK-NO-EXP10: call float @powf 182} 183 184define double @test_simplify19(double %x) { 185; CHECK-LABEL: @test_simplify19( 186 %retval = call double @pow(double 10.0, double %x) 187; CHECK-EXP10: [[EXP10:%[_a-z0-9]+]] = call double @__exp10(double %x) [[NUW_RO]] 188 ret double %retval 189; CHECK-EXP10: ret double [[EXP10]] 190; CHECK-NO-EXP10: call double @pow 191} 192 193; CHECK: attributes [[NUW_RO]] = { nounwind readonly } 194 195