Skip to content

Commit 9b3ffe0

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 82b40fd commit 9b3ffe0

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
@@ -1998,6 +1998,9 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
19981998
case Intrinsic::atan:
19991999
ISD = ISD::FATAN;
20002000
break;
2001+
case Intrinsic::atan2:
2002+
ISD = ISD::FATAN2;
2003+
break;
20012004
case Intrinsic::sinh:
20022005
ISD = ISD::FSINH;
20032006
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
@@ -1235,6 +1235,11 @@ let IntrProperties = [IntrInaccessibleMemOnly, IntrWillReturn, IntrStrictFP] in
12351235
[ LLVMMatchType<0>,
12361236
llvm_metadata_ty,
12371237
llvm_metadata_ty ]>;
1238+
def int_experimental_constrained_atan2 : DefaultAttrsIntrinsic<[ llvm_anyfloat_ty ],
1239+
[ LLVMMatchType<0>,
1240+
LLVMMatchType<0>,
1241+
llvm_metadata_ty,
1242+
llvm_metadata_ty ]>;
12381243
def int_experimental_constrained_sin : DefaultAttrsIntrinsic<[ llvm_anyfloat_ty ],
12391244
[ LLVMMatchType<0>,
12401245
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
@@ -4600,6 +4600,11 @@ void SelectionDAGLegalize::ConvertNodeToLibcall(SDNode *Node) {
46004600
ExpandFPLibCall(Node, RTLIB::ATAN_F32, RTLIB::ATAN_F64, RTLIB::ATAN_F80,
46014601
RTLIB::ATAN_F128, RTLIB::ATAN_PPCF128, Results);
46024602
break;
4603+
case ISD::FATAN2:
4604+
case ISD::STRICT_FATAN2:
4605+
ExpandFPLibCall(Node, RTLIB::ATAN2_F32, RTLIB::ATAN2_F64, RTLIB::ATAN2_F80,
4606+
RTLIB::ATAN2_F128, RTLIB::ATAN2_PPCF128, Results);
4607+
break;
46034608
case ISD::FSINH:
46044609
case ISD::STRICT_FSINH:
46054610
ExpandFPLibCall(Node, RTLIB::SINH_F32, RTLIB::SINH_F64, RTLIB::SINH_F80,
@@ -5486,6 +5491,7 @@ void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
54865491
case ISD::FMINIMUMNUM:
54875492
case ISD::FMAXIMUMNUM:
54885493
case ISD::FPOW:
5494+
case ISD::FATAN2:
54895495
Tmp1 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(0));
54905496
Tmp2 = DAG.getNode(ISD::FP_EXTEND, dl, NVT, Node->getOperand(1));
54915497
Tmp3 = DAG.getNode(Node->getOpcode(), dl, NVT, Tmp1, Tmp2,
@@ -5502,6 +5508,7 @@ void SelectionDAGLegalize::PromoteNode(SDNode *Node) {
55025508
case ISD::STRICT_FMAXNUM:
55035509
case ISD::STRICT_FREM:
55045510
case ISD::STRICT_FPOW:
5511+
case ISD::STRICT_FATAN2:
55055512
Tmp1 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},
55065513
{Node->getOperand(0), Node->getOperand(1)});
55075514
Tmp2 = DAG.getNode(ISD::STRICT_FP_EXTEND, dl, {NVT, MVT::Other},

0 commit comments

Comments
 (0)