Skip to content

Commit db0ae9c

Browse files
committed
[X86][CodeGen] Add base atan2 intrinsic lowering (p4)
This change is part of this proposal: https://discourse.llvm.org/t/rfc-all-the-math-intrinsics/78294 Based on example PR llvm#96222 and fix PR llvm#101268, with some differences due to 2-arg intrinsic and intermediate refactor (RuntimeLibCalls.cpp). - Add llvm.experimental.constrained.atan2 - Intrinsics.td, ConstrainedOps.def, LangRef.rst - Add to ISDOpcodes.h and TargetSelectionDAG.td, connect to intrinsic in BasicTTIImpl.h, and LibFunc_ in SelectionDAGBuilder.cpp, and map generic op in SelectionDAGCompat.td - Update LegalizeDAG.cpp, LegalizeFloatTypes.cpp, LegalizeVectorOps.cpp, and LegalizeVectorTypes.cpp - Update isKnownNeverNaN in SelectionDAG.cpp - Update SelectionDAGDumper.cpp - Update libcalls - RuntimeLibcalls.def, RuntimeLibcalls.cpp, LegalizerHelper.cpp - Update isKnownNeverNaN for generic opcode in GlobalISel/Utils.cpp - TargetLoweringBase.cpp - Expand for vectors, promote f16 - X86ISelLowering.cpp - Expand f80, promote f32 to f64 for MSVC
1 parent b70d327 commit db0ae9c

27 files changed

+594
-5
lines changed

llvm/include/llvm/CodeGen/BasicTTIImpl.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,9 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
19931993
case Intrinsic::atan:
19941994
ISD = ISD::FATAN;
19951995
break;
1996+
case Intrinsic::atan2:
1997+
ISD = ISD::FATAN2;
1998+
break;
19961999
case Intrinsic::sinh:
19972000
ISD = ISD::FSINH;
19982001
break;

llvm/include/llvm/CodeGen/ISDOpcodes.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ enum NodeType {
425425
STRICT_FASIN,
426426
STRICT_FACOS,
427427
STRICT_FATAN,
428+
STRICT_FATAN2,
428429
STRICT_FSINH,
429430
STRICT_FCOSH,
430431
STRICT_FTANH,
@@ -994,6 +995,8 @@ enum NodeType {
994995
FPOWI,
995996
/// FLDEXP - ldexp, inspired by libm (op0 * 2**op1).
996997
FLDEXP,
998+
/// FATAN2 - atan2, inspired by libm.
999+
FATAN2,
9971000

9981001
/// FFREXP - frexp, extract fractional and exponent component of a
9991002
/// floating-point value. Returns the two components as separate return

llvm/include/llvm/IR/ConstrainedOps.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ CMP_INSTRUCTION(FCmp, 2, 0, experimental_constrained_fcmps, FSETCCS
7272
DAG_FUNCTION(acos, 1, 1, experimental_constrained_acos, FACOS)
7373
DAG_FUNCTION(asin, 1, 1, experimental_constrained_asin, FASIN)
7474
DAG_FUNCTION(atan, 1, 1, experimental_constrained_atan, FATAN)
75+
DAG_FUNCTION(atan2, 2, 1, experimental_constrained_atan2, FATAN2)
7576
DAG_FUNCTION(ceil, 1, 0, experimental_constrained_ceil, FCEIL)
7677
DAG_FUNCTION(cos, 1, 1, experimental_constrained_cos, FCOS)
7778
DAG_FUNCTION(cosh, 1, 1, experimental_constrained_cosh, FCOSH)

llvm/include/llvm/IR/Intrinsics.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1226,6 +1226,11 @@ let IntrProperties = [IntrInaccessibleMemOnly, IntrWillReturn, IntrStrictFP] in
12261226
[ LLVMMatchType<0>,
12271227
llvm_metadata_ty,
12281228
llvm_metadata_ty ]>;
1229+
def int_experimental_constrained_atan2 : DefaultAttrsIntrinsic<[ llvm_anyfloat_ty ],
1230+
[ LLVMMatchType<0>,
1231+
LLVMMatchType<0>,
1232+
llvm_metadata_ty,
1233+
llvm_metadata_ty ]>;
12291234
def int_experimental_constrained_sin : DefaultAttrsIntrinsic<[ llvm_anyfloat_ty ],
12301235
[ LLVMMatchType<0>,
12311236
llvm_metadata_ty,

llvm/include/llvm/IR/RuntimeLibcalls.def

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ HANDLE_LIBCALL(ATAN_F64, "atan")
232232
HANDLE_LIBCALL(ATAN_F80, "atanl")
233233
HANDLE_LIBCALL(ATAN_F128,"atanl")
234234
HANDLE_LIBCALL(ATAN_PPCF128, "atanl")
235+
HANDLE_LIBCALL(ATAN2_F32, "atan2f")
236+
HANDLE_LIBCALL(ATAN2_F64, "atan2")
237+
HANDLE_LIBCALL(ATAN2_F80, "atan2l")
238+
HANDLE_LIBCALL(ATAN2_F128,"atan2l")
239+
HANDLE_LIBCALL(ATAN2_PPCF128, "atan2l")
235240
HANDLE_LIBCALL(SINCOS_F32, nullptr)
236241
HANDLE_LIBCALL(SINCOS_F64, nullptr)
237242
HANDLE_LIBCALL(SINCOS_F80, nullptr)

llvm/include/llvm/Target/GlobalISel/SelectionDAGCompat.td

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ def : GINodeEquiv<G_FTAN, ftan>;
154154
def : GINodeEquiv<G_FACOS, facos>;
155155
def : GINodeEquiv<G_FASIN, fasin>;
156156
def : GINodeEquiv<G_FATAN, fatan>;
157+
def : GINodeEquiv<G_FATAN2, fatan2>;
157158
def : GINodeEquiv<G_FCOSH, fcosh>;
158159
def : GINodeEquiv<G_FSINH, fsinh>;
159160
def : GINodeEquiv<G_FTANH, ftanh>;

llvm/include/llvm/Target/TargetSelectionDAG.td

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,7 @@ def ftan : SDNode<"ISD::FTAN" , SDTFPUnaryOp>;
534534
def fasin : SDNode<"ISD::FASIN" , SDTFPUnaryOp>;
535535
def facos : SDNode<"ISD::FACOS" , SDTFPUnaryOp>;
536536
def fatan : SDNode<"ISD::FATAN" , SDTFPUnaryOp>;
537+
def fatan2 : SDNode<"ISD::FATAN2" , SDTFPBinOp>;
537538
def fsinh : SDNode<"ISD::FSINH" , SDTFPUnaryOp>;
538539
def fcosh : SDNode<"ISD::FCOSH" , SDTFPUnaryOp>;
539540
def ftanh : SDNode<"ISD::FTANH" , SDTFPUnaryOp>;
@@ -602,6 +603,8 @@ def strict_facos : SDNode<"ISD::STRICT_FACOS",
602603
SDTFPUnaryOp, [SDNPHasChain]>;
603604
def strict_fatan : SDNode<"ISD::STRICT_FATAN",
604605
SDTFPUnaryOp, [SDNPHasChain]>;
606+
def strict_fatan2 : SDNode<"ISD::STRICT_FATAN2",
607+
SDTFPBinOp, [SDNPHasChain]>;
605608
def strict_fsinh : SDNode<"ISD::STRICT_FSINH",
606609
SDTFPUnaryOp, [SDNPHasChain]>;
607610
def strict_fcosh : SDNode<"ISD::STRICT_FCOSH",
@@ -1588,6 +1591,9 @@ def any_facos : PatFrags<(ops node:$src),
15881591
def any_fatan : PatFrags<(ops node:$src),
15891592
[(strict_fatan node:$src),
15901593
(fatan node:$src)]>;
1594+
def any_fatan2 : PatFrags<(ops node:$src1, node:$src2),
1595+
[(strict_fatan2 node:$src1, node:$src2),
1596+
(fatan2 node:$src1, node:$src2)]>;
15911597
def any_fsinh : PatFrags<(ops node:$src),
15921598
[(strict_fsinh node:$src),
15931599
(fsinh node:$src)]>;

llvm/lib/CodeGen/GlobalISel/LegalizerHelper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,8 @@ static RTLIB::Libcall getRTLibDesc(unsigned Opcode, unsigned Size) {
457457
RTLIBCASE(ACOS_F);
458458
case TargetOpcode::G_FATAN:
459459
RTLIBCASE(ATAN_F);
460+
case TargetOpcode::G_FATAN2:
461+
RTLIBCASE(ATAN2_F);
460462
case TargetOpcode::G_FSINH:
461463
RTLIBCASE(SINH_F);
462464
case TargetOpcode::G_FCOSH:
@@ -1202,6 +1204,7 @@ LegalizerHelper::libcall(MachineInstr &MI, LostDebugLocObserver &LocObserver) {
12021204
case TargetOpcode::G_FACOS:
12031205
case TargetOpcode::G_FASIN:
12041206
case TargetOpcode::G_FATAN:
1207+
case TargetOpcode::G_FATAN2:
12051208
case TargetOpcode::G_FCOSH:
12061209
case TargetOpcode::G_FSINH:
12071210
case TargetOpcode::G_FTANH:
@@ -3122,6 +3125,7 @@ LegalizerHelper::widenScalar(MachineInstr &MI, unsigned TypeIdx, LLT WideTy) {
31223125
case TargetOpcode::G_FACOS:
31233126
case TargetOpcode::G_FASIN:
31243127
case TargetOpcode::G_FATAN:
3128+
case TargetOpcode::G_FATAN2:
31253129
case TargetOpcode::G_FCOSH:
31263130
case TargetOpcode::G_FSINH:
31273131
case TargetOpcode::G_FTANH:

llvm/lib/CodeGen/GlobalISel/Utils.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,6 +828,7 @@ bool llvm::isKnownNeverNaN(Register Val, const MachineRegisterInfo &MRI,
828828
case TargetOpcode::G_FACOS:
829829
case TargetOpcode::G_FASIN:
830830
case TargetOpcode::G_FATAN:
831+
case TargetOpcode::G_FATAN2:
831832
case TargetOpcode::G_FCOSH:
832833
case TargetOpcode::G_FSINH:
833834
case TargetOpcode::G_FTANH:
@@ -1715,6 +1716,7 @@ bool llvm::isPreISelGenericFloatingPointOpcode(unsigned Opc) {
17151716
case TargetOpcode::G_FACOS:
17161717
case TargetOpcode::G_FASIN:
17171718
case TargetOpcode::G_FATAN:
1719+
case TargetOpcode::G_FATAN2:
17181720
case TargetOpcode::G_FCOSH:
17191721
case TargetOpcode::G_FSINH:
17201722
case TargetOpcode::G_FTANH:

llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4599,6 +4599,11 @@ void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
45994599
ExpandFPLibCall(Node, RTLIB::ATAN_F32, RTLIB::ATAN_F64, RTLIB::ATAN_F80,
46004600
RTLIB::ATAN_F128, RTLIB::ATAN_PPCF128, Results);
46014601
break;
4602+
case ISD::FATAN2:
4603+
case ISD::STRICT_FATAN2:
4604+
ExpandFPLibCall(Node, RTLIB::ATAN2_F32, RTLIB::ATAN2_F64, RTLIB::ATAN2_F80,
4605+
RTLIB::ATAN2_F128, RTLIB::ATAN2_PPCF128, Results);
4606+
break;
46024607
case ISD::FSINH:
46034608
case ISD::STRICT_FSINH:
46044609
ExpandFPLibCall(Node, RTLIB::SINH_F32, RTLIB::SINH_F64, RTLIB::SINH_F80,
@@ -5485,6 +5490,7 @@ void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
54855490
case ISD::FMINIMUMNUM:
54865491
case ISD::FMAXIMUMNUM:
54875492
case ISD::FPOW:
5493+
case ISD::FATAN2:
54885494
Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
54895495
Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
54905496
Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2,
@@ -5501,6 +5507,7 @@ void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
55015507
case ISD::STRICT_FMAXNUM:
55025508
case ISD::STRICT_FREM:
55035509
case ISD::STRICT_FPOW:
5510+
case ISD::STRICT_FATAN2:
55045511
Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
55055512
{Node->getOperand(0), Node->getOperand(1)});
55065513
Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},

0 commit comments

Comments
 (0)