Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit a143b4a

Browse files
committed
Fold fneg and fabs like multiplications
Given no NaNs and no signed zeroes it folds: (fmul X, (select (fcmp X > 0.0), -1.0, 1.0)) -> (fneg (fabs X)) (fmul X, (select (fcmp X > 0.0), 1.0, -1.0)) -> (fabs X) Differential Revision: https://reviews.llvm.org/D34579 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@306592 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent de55cfe commit a143b4a

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

lib/CodeGen/SelectionDAG/DAGCombiner.cpp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9753,6 +9753,52 @@ SDValue DAGCombiner::visitFMUL(SDNode *N) {
97539753
}
97549754
}
97559755

9756+
// fold (fmul X, (select (fcmp X > 0.0), -1.0, 1.0)) -> (fneg (fabs X))
9757+
// fold (fmul X, (select (fcmp X > 0.0), 1.0, -1.0)) -> (fabs X)
9758+
if (Flags.hasNoNaNs() && Flags.hasNoSignedZeros() &&
9759+
(N0.getOpcode() == ISD::SELECT || N1.getOpcode() == ISD::SELECT) &&
9760+
TLI.isOperationLegal(ISD::FABS, VT)) {
9761+
SDValue Select = N0, X = N1;
9762+
if (Select.getOpcode() != ISD::SELECT)
9763+
std::swap(Select, X);
9764+
9765+
SDValue Cond = Select.getOperand(0);
9766+
auto TrueOpnd = dyn_cast<ConstantFPSDNode>(Select.getOperand(1));
9767+
auto FalseOpnd = dyn_cast<ConstantFPSDNode>(Select.getOperand(2));
9768+
9769+
if (TrueOpnd && FalseOpnd &&
9770+
Cond.getOpcode() == ISD::SETCC && Cond.getOperand(0) == X &&
9771+
isa<ConstantFPSDNode>(Cond.getOperand(1)) &&
9772+
cast<ConstantFPSDNode>(Cond.getOperand(1))->isExactlyValue(0.0)) {
9773+
ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get();
9774+
switch (CC) {
9775+
default: break;
9776+
case ISD::SETOLT:
9777+
case ISD::SETULT:
9778+
case ISD::SETOLE:
9779+
case ISD::SETULE:
9780+
case ISD::SETLT:
9781+
case ISD::SETLE:
9782+
std::swap(TrueOpnd, FalseOpnd);
9783+
// Fall through
9784+
case ISD::SETOGT:
9785+
case ISD::SETUGT:
9786+
case ISD::SETOGE:
9787+
case ISD::SETUGE:
9788+
case ISD::SETGT:
9789+
case ISD::SETGE:
9790+
if (TrueOpnd->isExactlyValue(-1.0) && FalseOpnd->isExactlyValue(1.0) &&
9791+
TLI.isOperationLegal(ISD::FNEG, VT))
9792+
return DAG.getNode(ISD::FNEG, DL, VT,
9793+
DAG.getNode(ISD::FABS, DL, VT, X));
9794+
if (TrueOpnd->isExactlyValue(1.0) && FalseOpnd->isExactlyValue(-1.0))
9795+
return DAG.getNode(ISD::FABS, DL, VT, X);
9796+
9797+
break;
9798+
}
9799+
}
9800+
}
9801+
97569802
// FMUL -> FMA combines:
97579803
if (SDValue Fused = visitFMULForFMADistributiveCombine(N)) {
97589804
AddToWorklist(Fused.getNode());
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
; RUN: llc -march=amdgcn -verify-machineinstrs < %s | FileCheck -check-prefix=GCN %s
2+
3+
; GCN-LABEL: {{^}}fold_mul_neg:
4+
; GCN: load_dword [[V:v[0-9]+]]
5+
; GCN: v_or_b32_e32 [[NEG:v[0-9]]], 0x80000000, [[V]]
6+
; GCN: store_dword [[NEG]]
7+
8+
define amdgpu_kernel void @fold_mul_neg(float addrspace(1)* %arg) {
9+
%tid = tail call i32 @llvm.amdgcn.workitem.id.x()
10+
%gep = getelementptr inbounds float, float addrspace(1)* %arg, i32 %tid
11+
%v = load float, float addrspace(1)* %gep, align 4
12+
%cmp = fcmp fast ogt float %v, 0.000000e+00
13+
%sel = select i1 %cmp, float -1.000000e+00, float 1.000000e+00
14+
%mul = fmul fast float %v, %sel
15+
store float %mul, float addrspace(1)* %gep, align 4
16+
ret void
17+
}
18+
19+
; GCN-LABEL: {{^}}fold_mul_abs:
20+
; GCN: load_dword [[V:v[0-9]+]]
21+
; GCN: v_and_b32_e32 [[ABS:v[0-9]]], 0x7fffffff, [[V]]
22+
; GCN: store_dword [[ABS]]
23+
24+
define amdgpu_kernel void @fold_mul_abs(float addrspace(1)* %arg) {
25+
%tid = tail call i32 @llvm.amdgcn.workitem.id.x()
26+
%gep = getelementptr inbounds float, float addrspace(1)* %arg, i32 %tid
27+
%v = load float, float addrspace(1)* %gep, align 4
28+
%cmp = fcmp fast olt float %v, 0.000000e+00
29+
%sel = select i1 %cmp, float -1.000000e+00, float 1.000000e+00
30+
%mul = fmul fast float %v, %sel
31+
store float %mul, float addrspace(1)* %gep, align 4
32+
ret void
33+
}
34+
35+
declare i32 @llvm.amdgcn.workitem.id.x() #0
36+
37+
attributes #0 = { nounwind readnone speculatable }

0 commit comments

Comments
 (0)