Skip to content

Commit da0f89e

Browse files
Implement generic llvm.canonicalize.f(32|64).
Previously, this intrinsic has been implemented in the X86 backend. As suggested in the comments for the PR that merges that implementation (#106370), this commit replicates that implementation (at least for `f32` and `f64`) in common code. Canonicalization is implemented as multiplication with 1.0.
1 parent 5e21f2b commit da0f89e

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

llvm/lib/CodeGen/SelectionDAG/LegalizeDAG.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3356,6 +3356,23 @@ bool SelectionDAGLegalize::ExpandNode(SDNode *Node) {
33563356
Results.push_back(Op);
33573357
break;
33583358
}
3359+
case ISD::FCANONICALIZE: {
3360+
// This implements llvm.canonicalize.f* by multiplication with 1.0,
3361+
// as suggested in https://llvm.org/docs/LangRef.html#id2335.
3362+
// Get operand x.
3363+
SDValue Operand = Node->getOperand(0);
3364+
// Get fp value type used.
3365+
EVT VT = Operand.getValueType();
3366+
// Produce appropriately-typed 1.0 constant.
3367+
SDValue One = DAG.getConstantFP(1.0, dl, VT);
3368+
// Produce multiplication node x * 1.0.
3369+
SDValue Chain = DAG.getEntryNode();
3370+
SDValue Mul = DAG.getNode(ISD::STRICT_FMUL, dl, {VT, MVT::Other},
3371+
{Chain, Operand, One});
3372+
3373+
Results.push_back(Mul);
3374+
break;
3375+
}
33593376
case ISD::SIGN_EXTEND_INREG: {
33603377
EVT ExtraVT = cast<VTSDNode>(Node->getOperand(1))->getVT();
33613378
EVT VT = Node->getValueType(0);

llvm/lib/CodeGen/TargetLoweringBase.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -870,6 +870,9 @@ void TargetLoweringBase::initActions() {
870870
ISD::FATAN2},
871871
{MVT::f32, MVT::f64, MVT::f128}, Expand);
872872

873+
// Insert custom handling default for llvm.canonicalize.*.
874+
setOperationAction(ISD::FCANONICALIZE, {MVT::f32, MVT::f64}, Expand);
875+
873876
// FIXME: Query RuntimeLibCalls to make the decision.
874877
setOperationAction({ISD::LRINT, ISD::LLRINT, ISD::LROUND, ISD::LLROUND},
875878
{MVT::f32, MVT::f64, MVT::f128}, LibCall);

0 commit comments

Comments
 (0)