Skip to content

Commit ed13fef

Browse files
committed
[SelectionDAG] Do minnum->minimum at legalization time instead of building time
The SDAGBuilder behavior stems from the days when we didn't have fast math flags available in SDAG. We do now and doing the transformation in the legalizer has the advantage that it also works for vector types. llvm-svn: 364743
1 parent d74f2d0 commit ed13fef

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

llvm/lib/CodeGen/SelectionDAG/SelectionDAGBuilder.cpp

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6016,28 +6016,18 @@ void SelectionDAGBuilder::visitIntrinsicCall(const CallInst &I,
60166016
getValue(I.getArgOperand(0))));
60176017
return;
60186018
}
6019-
case Intrinsic::minnum: {
6020-
auto VT = getValue(I.getArgOperand(0)).getValueType();
6021-
unsigned Opc =
6022-
I.hasNoNaNs() && TLI.isOperationLegalOrCustom(ISD::FMINIMUM, VT)
6023-
? ISD::FMINIMUM
6024-
: ISD::FMINNUM;
6025-
setValue(&I, DAG.getNode(Opc, sdl, VT,
6019+
case Intrinsic::minnum:
6020+
setValue(&I, DAG.getNode(ISD::FMINNUM, sdl,
6021+
getValue(I.getArgOperand(0)).getValueType(),
60266022
getValue(I.getArgOperand(0)),
60276023
getValue(I.getArgOperand(1))));
60286024
return;
6029-
}
6030-
case Intrinsic::maxnum: {
6031-
auto VT = getValue(I.getArgOperand(0)).getValueType();
6032-
unsigned Opc =
6033-
I.hasNoNaNs() && TLI.isOperationLegalOrCustom(ISD::FMAXIMUM, VT)
6034-
? ISD::FMAXIMUM
6035-
: ISD::FMAXNUM;
6036-
setValue(&I, DAG.getNode(Opc, sdl, VT,
6025+
case Intrinsic::maxnum:
6026+
setValue(&I, DAG.getNode(ISD::FMAXNUM, sdl,
6027+
getValue(I.getArgOperand(0)).getValueType(),
60376028
getValue(I.getArgOperand(0)),
60386029
getValue(I.getArgOperand(1))));
60396030
return;
6040-
}
60416031
case Intrinsic::minimum:
60426032
setValue(&I, DAG.getNode(ISD::FMINIMUM, sdl,
60436033
getValue(I.getArgOperand(0)).getValueType(),

llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5098,6 +5098,17 @@ SDValue TargetLowering::expandFMINNUM_FMAXNUM(SDNode *Node,
50985098
return DAG.getNode(NewOp, dl, VT, Quiet0, Quiet1, Node->getFlags());
50995099
}
51005100

5101+
// If the target has FMINIMUM/FMAXIMUM but not FMINNUM/FMAXNUM use that
5102+
// instead if there are no NaNs.
5103+
if (Node->getFlags().hasNoNaNs()) {
5104+
unsigned IEEE2018Op =
5105+
Node->getOpcode() == ISD::FMINNUM ? ISD::FMINIMUM : ISD::FMAXIMUM;
5106+
if (isOperationLegalOrCustom(IEEE2018Op, VT)) {
5107+
return DAG.getNode(IEEE2018Op, dl, VT, Node->getOperand(0),
5108+
Node->getOperand(1), Node->getFlags());
5109+
}
5110+
}
5111+
51015112
return SDValue();
51025113
}
51035114

llvm/test/CodeGen/WebAssembly/f32.ll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ define float @fmin32_intrinsic(float %x, float %y) {
149149
ret float %a
150150
}
151151

152+
; CHECK-LABEL: fminnum32_intrinsic:
153+
; CHECK: f32.min $push0=, $pop{{[0-9]+}}, $pop{{[0-9]+}}{{$}}
154+
; CHECK-NEXT: return $pop0{{$}}
155+
declare float @llvm.minnum.f32(float, float)
156+
define float @fminnum32_intrinsic(float %x, float %y) {
157+
%a = call nnan float @llvm.minnum.f32(float %x, float %y)
158+
ret float %a
159+
}
160+
152161
; CHECK-LABEL: fmax32_intrinsic:
153162
; CHECK: f32.max $push0=, $pop{{[0-9]+}}, $pop{{[0-9]+}}{{$}}
154163
; CHECK-NEXT: return $pop0{{$}}
@@ -158,6 +167,15 @@ define float @fmax32_intrinsic(float %x, float %y) {
158167
ret float %a
159168
}
160169

170+
; CHECK-LABEL: fmaxnum32_intrinsic:
171+
; CHECK: f32.max $push0=, $pop{{[0-9]+}}, $pop{{[0-9]+}}{{$}}
172+
; CHECK-NEXT: return $pop0{{$}}
173+
declare float @llvm.maxnum.f32(float, float)
174+
define float @fmaxnum32_intrinsic(float %x, float %y) {
175+
%a = call nnan float @llvm.maxnum.f32(float %x, float %y)
176+
ret float %a
177+
}
178+
161179
; CHECK-LABEL: fma32:
162180
; CHECK: {{^}} f32.call $push[[LR:[0-9]+]]=, fmaf, $pop{{[0-9]+}}, $pop{{[0-9]+}}, $pop{{[0-9]+}}{{$}}
163181
; CHECK-NEXT: return $pop[[LR]]{{$}}

llvm/test/CodeGen/WebAssembly/simd-arith.ll

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,17 @@ define <4 x float> @min_intrinsic_v4f32(<4 x float> %x, <4 x float> %y) {
10731073
ret <4 x float> %a
10741074
}
10751075

1076+
; CHECK-LABEL: minnum_intrinsic_v4f32:
1077+
; NO-SIMD128-NOT: f32x4
1078+
; SIMD128-NEXT: .functype minnum_intrinsic_v4f32 (v128, v128) -> (v128){{$}}
1079+
; SIMD128-NEXT: f32x4.min $push[[R:[0-9]+]]=, $0, $1{{$}}
1080+
; SIMD128-NEXT: return $pop[[R]]{{$}}
1081+
declare <4 x float> @llvm.minnum.v4f32(<4 x float>, <4 x float>)
1082+
define <4 x float> @minnum_intrinsic_v4f32(<4 x float> %x, <4 x float> %y) {
1083+
%a = call nnan <4 x float> @llvm.minnum.v4f32(<4 x float> %x, <4 x float> %y)
1084+
ret <4 x float> %a
1085+
}
1086+
10761087
; CHECK-LABEL: max_intrinsic_v4f32:
10771088
; NO-SIMD128-NOT: f32x4
10781089
; SIMD128-NEXT: .functype max_intrinsic_v4f32 (v128, v128) -> (v128){{$}}
@@ -1084,6 +1095,17 @@ define <4 x float> @max_intrinsic_v4f32(<4 x float> %x, <4 x float> %y) {
10841095
ret <4 x float> %a
10851096
}
10861097

1098+
; CHECK-LABEL: maxnum_intrinsic_v4f32:
1099+
; NO-SIMD128-NOT: f32x4
1100+
; SIMD128-NEXT: .functype maxnum_intrinsic_v4f32 (v128, v128) -> (v128){{$}}
1101+
; SIMD128-NEXT: f32x4.max $push[[R:[0-9]+]]=, $0, $1{{$}}
1102+
; SIMD128-NEXT: return $pop[[R]]{{$}}
1103+
declare <4 x float> @llvm.maxnum.v4f32(<4 x float>, <4 x float>)
1104+
define <4 x float> @maxnum_intrinsic_v4f32(<4 x float> %x, <4 x float> %y) {
1105+
%a = call nnan <4 x float> @llvm.maxnum.v4f32(<4 x float> %x, <4 x float> %y)
1106+
ret <4 x float> %a
1107+
}
1108+
10871109
; CHECK-LABEL: min_const_intrinsic_v4f32:
10881110
; NO-SIMD128-NOT: f32x4
10891111
; SIMD128-NEXT: .functype min_const_intrinsic_v4f32 () -> (v128){{$}}

0 commit comments

Comments
 (0)