[llvm] r341095 - [InstCombine] Expand the simplification of pow() into exp2()
Evandro Menezes via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 30 12:04:51 PDT 2018
Author: evandro
Date: Thu Aug 30 12:04:51 2018
New Revision: 341095
URL: http://llvm.org/viewvc/llvm-project?rev=341095&view=rev
Log:
[InstCombine] Expand the simplification of pow() into exp2()
Generalize the simplification of `pow(2.0, y)` to `pow(2.0 ** n, y)` for all
scalar and vector types.
This improvement helps some benchmarks in SPEC CPU2000 and CPU2006, such as
252.eon, 447.dealII, 453.povray. Otherwise, no significant regressions on
x86-64 or A64.
Differential revision: https://reviews.llvm.org/D49273
Modified:
llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
llvm/trunk/test/Transforms/InstCombine/pow-1.ll
Modified: llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=341095&r1=341094&r2=341095&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Thu Aug 30 12:04:51 2018
@@ -13,6 +13,7 @@
//===----------------------------------------------------------------------===//
#include "llvm/Transforms/Utils/SimplifyLibCalls.h"
+#include "llvm/ADT/APSInt.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/Triple.h"
@@ -1183,12 +1184,13 @@ static Value *getPow(Value *InnerChain[3
}
/// Use exp{,2}(x * y) for pow(exp{,2}(x), y);
-/// exp2(x) for pow(2.0, x); exp10(x) for pow(10.0, x).
+/// exp2(n * x) for pow(2.0 ** n, x); exp10(x) for pow(10.0, x).
Value *LibCallSimplifier::replacePowWithExp(CallInst *Pow, IRBuilder<> &B) {
Value *Base = Pow->getArgOperand(0), *Expo = Pow->getArgOperand(1);
AttributeList Attrs = Pow->getCalledFunction()->getAttributes();
Module *Mod = Pow->getModule();
Type *Ty = Pow->getType();
+ bool Ignored;
// Evaluate special cases related to a nested function as the base.
@@ -1249,10 +1251,30 @@ Value *LibCallSimplifier::replacePowWith
// Evaluate special cases related to a constant base.
- // pow(2.0, x) -> exp2(x)
- if (match(Base, m_SpecificFP(2.0))) {
- Value *Exp2 = Intrinsic::getDeclaration(Mod, Intrinsic::exp2, Ty);
- return B.CreateCall(Exp2, Expo, "exp2");
+ const APFloat *BaseF;
+ if (!match(Pow->getArgOperand(0), m_APFloat(BaseF)))
+ return nullptr;
+
+ // pow(2.0 ** n, x) -> exp2(n * x)
+ if (hasUnaryFloatFn(TLI, Ty, LibFunc_exp2, LibFunc_exp2f, LibFunc_exp2l)) {
+ APFloat BaseR = APFloat(1.0);
+ BaseR.convert(BaseF->getSemantics(), APFloat::rmTowardZero, &Ignored);
+ BaseR = BaseR / *BaseF;
+ bool IsInteger = BaseF->isInteger(),
+ IsReciprocal = BaseR.isInteger();
+ const APFloat *NF = IsReciprocal ? &BaseR : BaseF;
+ APSInt NI(64, false);
+ if ((IsInteger || IsReciprocal) &&
+ !NF->convertToInteger(NI, APFloat::rmTowardZero, &Ignored) &&
+ NI > 1 && NI.isPowerOf2()) {
+ double N = NI.logBase2() * (IsReciprocal ? -1.0 : 1.0);
+ Value *FMul = B.CreateFMul(Expo, ConstantFP::get(Ty, N), "mul");
+ if (Pow->doesNotAccessMemory())
+ return B.CreateCall(Intrinsic::getDeclaration(Mod, Intrinsic::exp2, Ty),
+ FMul, "exp2");
+ else
+ return emitUnaryFloatFnCall(FMul, TLI->getName(LibFunc_exp2), B, Attrs);
+ }
}
// pow(10.0, x) -> exp10(x)
Modified: llvm/trunk/test/Transforms/InstCombine/pow-1.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/pow-1.ll?rev=341095&r1=341094&r2=341095&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/pow-1.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/pow-1.ll Thu Aug 30 12:04:51 2018
@@ -1,4 +1,3 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; Test that the pow library call simplifier works correctly.
;
; RUN: opt -instcombine -S < %s | FileCheck %s --check-prefixes=ANY
@@ -10,12 +9,14 @@
; RUN: opt -instcombine -S < %s -mtriple=arm-apple-tvos9.0 | FileCheck %s --check-prefixes=ANY,CHECK-EXP10
; RUN: opt -instcombine -S < %s -mtriple=arm-apple-watchos2.0 | FileCheck %s --check-prefixes=ANY,CHECK-EXP10
; rdar://7251832
+; RUN: opt -instcombine -S < %s -mtriple=x86_64-pc-windows-msvc | FileCheck %s --check-prefixes=CHECK-WIN
; NOTE: The readonly attribute on the pow call should be preserved
; in the cases below where pow is transformed into another function call.
declare float @powf(float, float) nounwind readonly
declare double @pow(double, double) nounwind readonly
+declare double @llvm.pow.f64(double, double)
declare <2 x float> @llvm.pow.v2f32(<2 x float>, <2 x float>) nounwind readonly
declare <2 x double> @llvm.pow.v2f64(<2 x double>, <2 x double>) nounwind readonly
@@ -57,18 +58,26 @@ define <2 x double> @test_simplify2v(<2
define float @test_simplify3(float %x) {
; ANY-LABEL: @test_simplify3(
-; ANY-NEXT: [[EXP2:%.*]] = call float @llvm.exp2.f32(float [[X:%.*]])
-; ANY-NEXT: ret float [[EXP2]]
+; ANY-NEXT: [[EXP2F:%.*]] = call float @exp2f(float [[X:%.*]]) [[NUW_RO:#[0-9]+]]
+; ANY-NEXT: ret float [[EXP2F]]
+;
+; CHECK-WIN-LABEL: @test_simplify3(
+; CHECK-WIN-NEXT: [[POW:%.*]] = call float @powf(float 2.000000e+00, float [[X:%.*]])
+; CHECK-WIN-NEXT: ret float [[POW]]
;
%retval = call float @powf(float 2.0, float %x)
ret float %retval
}
-; TODO: Should result in exp2(-2.0 * x).
define double @test_simplify3n(double %x) {
; ANY-LABEL: @test_simplify3n(
-; ANY-NEXT: [[RETVAL:%.*]] = call double @pow(double 2.500000e-01, double [[X:%.*]])
-; ANY-NEXT: ret double [[RETVAL]]
+; ANY-NEXT: [[MUL:%.*]] = fmul double [[X:%.*]], -2.000000e+00
+; ANY-NEXT: [[EXP2:%.*]] = call double @exp2(double [[MUL]]) [[NUW_RO]]
+; ANY-NEXT: ret double [[EXP2]]
+;
+; CHECK-WIN-LABEL: @test_simplify3n(
+; CHECK-WIN-NEXT: [[POW:%.*]] = call double @pow(double 2.500000e-01, double [[X:%.*]])
+; CHECK-WIN-NEXT: ret double [[POW]]
;
%retval = call double @pow(double 0.25, double %x)
ret double %retval
@@ -79,15 +88,19 @@ define <2 x float> @test_simplify3v(<2 x
; ANY-NEXT: [[EXP2:%.*]] = call <2 x float> @llvm.exp2.v2f32(<2 x float> [[X:%.*]])
; ANY-NEXT: ret <2 x float> [[EXP2]]
;
+; CHECK-WIN-LABEL: @test_simplify3v(
+; CHECK-WIN-NEXT: [[POW:%.*]] = call <2 x float> @llvm.pow.v2f32(<2 x float> <float 2.000000e+00, float 2.000000e+00>, <2 x float> [[X:%.*]])
+; CHECK-WIN-NEXT: ret <2 x float> [[POW]]
+;
%retval = call <2 x float> @llvm.pow.v2f32(<2 x float> <float 2.0, float 2.0>, <2 x float> %x)
ret <2 x float> %retval
}
-; TODO: Should result in exp2(2.0 * x).
define <2 x double> @test_simplify3vn(<2 x double> %x) {
; ANY-LABEL: @test_simplify3vn(
-; ANY-NEXT: [[RETVAL:%.*]] = call <2 x double> @llvm.pow.v2f64(<2 x double> <double 4.000000e+00, double 4.000000e+00>, <2 x double> [[X:%.*]])
-; ANY-NEXT: ret <2 x double> [[RETVAL]]
+; ANY-NEXT: [[MUL:%.*]] = fmul <2 x double> [[X:%.*]], <double 2.000000e+00, double 2.000000e+00>
+; ANY-NEXT: [[EXP2:%.*]] = call <2 x double> @llvm.exp2.v2f64(<2 x double> [[MUL]])
+; ANY-NEXT: ret <2 x double> [[EXP2]]
;
%retval = call <2 x double> @llvm.pow.v2f64(<2 x double> <double 4.0, double 4.0>, <2 x double> %x)
ret <2 x double> %retval
@@ -95,18 +108,26 @@ define <2 x double> @test_simplify3vn(<2
define double @test_simplify4(double %x) {
; ANY-LABEL: @test_simplify4(
-; ANY-NEXT: [[EXP2:%.*]] = call double @llvm.exp2.f64(double [[X:%.*]])
+; ANY-NEXT: [[EXP2:%.*]] = call double @exp2(double [[X:%.*]]) [[NUW_RO]]
; ANY-NEXT: ret double [[EXP2]]
;
+; CHECK-WIN-LABEL: @test_simplify4(
+; CHECK-WIN-NEXT: [[POW:%.*]] = call double @pow(double 2.000000e+00, double [[X:%.*]])
+; CHECK-WIN-NEXT: ret double [[POW]]
+;
%retval = call double @pow(double 2.0, double %x)
ret double %retval
}
-; TODO: Should result in exp2f(3.0 * x).
define float @test_simplify4n(float %x) {
; ANY-LABEL: @test_simplify4n(
-; ANY-NEXT: [[RETVAL:%.*]] = call float @powf(float 8.000000e+00, float [[X:%.*]])
-; ANY-NEXT: ret float [[RETVAL]]
+; ANY-NEXT: [[MUL:%.*]] = fmul float [[X:%.*]], 3.000000e+00
+; ANY-NEXT: [[EXP2F:%.*]] = call float @exp2f(float [[MUL]]) [[NUW_RO]]
+; ANY-NEXT: ret float [[EXP2F]]
+;
+; CHECK-WIN-LABEL: @test_simplify4n(
+; CHECK-WIN-NEXT: [[POW:%.*]] = call float @powf(float 8.000000e+00, float [[X:%.*]])
+; CHECK-WIN-NEXT: ret float [[POW]]
;
%retval = call float @powf(float 8.0, float %x)
ret float %retval
@@ -117,15 +138,23 @@ define <2 x double> @test_simplify4v(<2
; ANY-NEXT: [[EXP2:%.*]] = call <2 x double> @llvm.exp2.v2f64(<2 x double> [[X:%.*]])
; ANY-NEXT: ret <2 x double> [[EXP2]]
;
+; CHECK-WIN-LABEL: @test_simplify4v(
+; CHECK-WIN-NEXT: [[POW:%.*]] = call <2 x double> @llvm.pow.v2f64(<2 x double> <double 2.000000e+00, double 2.000000e+00>, <2 x double> [[X:%.*]])
+; CHECK-WIN-NEXT: ret <2 x double> [[POW]]
+;
%retval = call <2 x double> @llvm.pow.v2f64(<2 x double> <double 2.0, double 2.0>, <2 x double> %x)
ret <2 x double> %retval
}
-; TODO: Should result in exp2f(-x).
define <2 x float> @test_simplify4vn(<2 x float> %x) {
; ANY-LABEL: @test_simplify4vn(
-; ANY-NEXT: [[RETVAL:%.*]] = call <2 x float> @llvm.pow.v2f32(<2 x float> <float 5.000000e-01, float 5.000000e-01>, <2 x float> [[X:%.*]])
-; ANY-NEXT: ret <2 x float> [[RETVAL]]
+; ANY-NEXT: [[MUL:%.*]] = fsub <2 x float> <float -0.000000e+00, float -0.000000e+00>, [[X:%.*]]
+; ANY-NEXT: [[EXP2:%.*]] = call <2 x float> @llvm.exp2.v2f32(<2 x float> [[MUL]])
+; ANY-NEXT: ret <2 x float> [[EXP2]]
+;
+; CHECK-WIN-LABEL: @test_simplify4vn(
+; CHECK-WIN-NEXT: [[POW:%.*]] = call <2 x float> @llvm.pow.v2f32(<2 x float> <float 5.000000e-01, float 5.000000e-01>, <2 x float> [[X:%.*]])
+; CHECK-WIN-NEXT: ret <2 x float> [[POW]]
;
%retval = call <2 x float> @llvm.pow.v2f32(<2 x float> <float 0.5, float 0.5>, <2 x float> %x)
ret <2 x float> %retval
@@ -169,7 +198,7 @@ define <2 x double> @test_simplify6v(<2
define float @test_simplify7(float %x) {
; ANY-LABEL: @test_simplify7(
-; ANY-NEXT: [[SQRTF:%.*]] = call float @sqrtf(float [[X:%.*]]) [[NUW_RO:#[0-9]+]]
+; ANY-NEXT: [[SQRTF:%.*]] = call float @sqrtf(float [[X:%.*]]) [[NUW_RO]]
; ANY-NEXT: [[ABS:%.*]] = call float @llvm.fabs.f32(float [[SQRTF]])
; ANY-NEXT: [[ISINF:%.*]] = fcmp oeq float [[X]], 0xFFF0000000000000
; ANY-NEXT: [[TMP1:%.*]] = select i1 [[ISINF]], float 0x7FF0000000000000, float [[ABS]]
@@ -330,7 +359,6 @@ define <2 x double> @pow_neg1_double_fas
ret <2 x double> %r
}
-declare double @llvm.pow.f64(double %Val, double %Power)
define double @test_simplify17(double %x) {
; ANY-LABEL: @test_simplify17(
; ANY-NEXT: [[SQRT:%.*]] = call double @llvm.sqrt.f64(double [[X:%.*]])
More information about the llvm-commits
mailing list