Index: lib/Target/Sparc/SparcISelLowering.h =================================================================== --- lib/Target/Sparc/SparcISelLowering.h +++ lib/Target/Sparc/SparcISelLowering.h @@ -192,6 +192,8 @@ SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const; + SDValue LowerBITCAST(SDValue Op, SelectionDAG &DAG) const; + bool ShouldShrinkFPConstant(EVT VT) const override { // Do not shrink FP constpool if VT == MVT::f128. // (ldd, call _Q_fdtoq) is more expensive than two ldds. Index: lib/Target/Sparc/SparcISelLowering.cpp =================================================================== --- lib/Target/Sparc/SparcISelLowering.cpp +++ lib/Target/Sparc/SparcISelLowering.cpp @@ -846,11 +846,6 @@ if (VA.getLocVT() == MVT::f64) { // Move from the float value from float registers into the // integer registers. - - // TODO: The f64 -> v2i32 conversion is super-inefficient for - // constants: it sticks them in the constant pool, then loads - // to a fp register, then stores to temp memory, then loads to - // integer registers. Arg = DAG.getNode(ISD::BITCAST, dl, MVT::v2i32, Arg); } @@ -1841,6 +1836,12 @@ setOperationAction(ISD::FMUL, MVT::f32, Promote); } + // Custom legalize bitcast between f64 and v2i32 + if (!Subtarget->is64Bit()) { + setOperationAction(ISD::BITCAST, MVT::v2i32, Custom); + setOperationAction(ISD::BITCAST, MVT::f64, Custom); + } + setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::Other, Custom); setMinFunctionAlignment(2); @@ -3056,6 +3057,29 @@ } } +SDValue SparcTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const { + SDLoc dl(Op); + SDValue Src = Op->getOperand(0); + MVT VT = Op.getSimpleValueType(); + + assert(VT == MVT::v2i32 || VT == MVT::f64); + assert(Src.getSimpleValueType() == MVT::v2i32 || + Src.getSimpleValueType() == MVT::f64); + + if (LoadSDNode *LD = dyn_cast(Src)) + return DAG.getLoad(VT, dl, LD->getChain(), LD->getBasePtr(), + LD->getMemOperand()); + + if (ConstantFPSDNode *C = dyn_cast(Src)) { + APInt V = C->getValueAPF().bitcastToAPInt(); + SDValue Lo = DAG.getConstant(V.zextOrTrunc(32), dl, MVT::i32); + SDValue Hi = DAG.getConstant(V.lshr(32).zextOrTrunc(32), dl, MVT::i32); + return DAG.getBuildVector(MVT::v2i32, dl, {Hi, Lo}); + } + + return SDValue(); +} + SDValue SparcTargetLowering:: LowerOperation(SDValue Op, SelectionDAG &DAG) const { @@ -3117,6 +3141,7 @@ case ISD::ATOMIC_LOAD: case ISD::ATOMIC_STORE: return LowerATOMIC_LOAD_STORE(Op, DAG); case ISD::INTRINSIC_WO_CHAIN: return LowerINTRINSIC_WO_CHAIN(Op, DAG); + case ISD::BITCAST: return LowerBITCAST(Op, DAG); } } Index: test/CodeGen/SPARC/float-constants.ll =================================================================== --- test/CodeGen/SPARC/float-constants.ll +++ test/CodeGen/SPARC/float-constants.ll @@ -1,14 +1,11 @@ ; RUN: llc < %s -march=sparc | FileCheck %s -; TODO: actually fix the codegen to be optimal. At least we don't -; crash for now, though... - ;; Bitcast should not do a runtime conversion, but rather emit a ;; constant into integer registers directly. ; CHECK-LABEL: bitcast: -; TODO-CHECK: sethi 1049856, %o0 -; TODO-CHECK: sethi 0, %o1 +; CHECK: sethi 1049856, %o0 +; CHECK: mov %g0, %o1 define <2 x i32> @bitcast() { %1 = bitcast double 5.0 to <2 x i32> ret <2 x i32> %1 @@ -18,8 +15,8 @@ ;; registers) ; CHECK-LABEL: test_call -; TODO-CHECK: sethi 1049856, %o0 -; TODO-CHECK: sethi 0, %o1 +; CHECK: sethi 1049856, %o0 +; CHECK: mov %g0, %o1 declare void @a(double) define void @test_call() { call void @a(double 5.0) @@ -32,8 +29,8 @@ ;; due to an earlier broken workaround for this issue.) ; CHECK-LABEL: test_intrins_call -; TODO-CHECK: sethi 1049856, %o0 -; TODO-CHECK: sethi 0, %o1 +; CHECK: sethi %hi(.LCPI2_0), %i0 +; CHECK: ldd [%i0+%lo(.LCPI2_0)], %o0 declare double @llvm.pow.f64(double, double) define double @test_intrins_call() { %1 = call double @llvm.pow.f64(double 2.0, double 2.0)