Index: lib/CodeGen/SelectionDAG/DAGCombiner.cpp =================================================================== --- lib/CodeGen/SelectionDAG/DAGCombiner.cpp +++ lib/CodeGen/SelectionDAG/DAGCombiner.cpp @@ -6804,8 +6804,14 @@ // fold vector ops if (VT.isVector()) { + // This just handles C1 * C2 for vectors. Other vector folds are below. SDValue FoldedVOp = SimplifyVBinOp(N); - if (FoldedVOp.getNode()) return FoldedVOp; + if (FoldedVOp.getNode()) + return FoldedVOp; + // Canonicalize possible vector constant to RHS. + if (N0.getOpcode() == ISD::BUILD_VECTOR && + N1.getOpcode() != ISD::BUILD_VECTOR) + return DAG.getNode(N->getOpcode(), SDLoc(N), VT, N1, N0); } // fold (fmul c1, c2) -> c1*c2 @@ -6824,13 +6830,19 @@ // fold (fmul A, 0) -> 0 if (N1CFP && N1CFP->getValueAPF().isZero()) return N1; - + // fold (fmul (fmul x, c1), c2) -> (fmul x, (fmul c1, c2)) - if (N1CFP && N0.getOpcode() == ISD::FMUL && - N0.getNode()->hasOneUse() && isConstOrConstSplatFP(N0.getOperand(1))) { - SDLoc SL(N); - SDValue MulConsts = DAG.getNode(ISD::FMUL, SL, VT, N0.getOperand(1), N1); - return DAG.getNode(ISD::FMUL, SL, VT, N0.getOperand(0), MulConsts); + if (N0.getOpcode() == ISD::FMUL && N0.getNode()->hasOneUse()) { + // Fold scalars or any vector constants (not just splats). + SDValue N01 = N0.getOperand(1); + auto *BV1 = dyn_cast(N1); + auto *BV01 = dyn_cast(N01); + if ((N1CFP && isConstOrConstSplatFP(N01)) || + (BV1 && BV01 && BV1->isConstant() && BV01->isConstant())) { + SDLoc SL(N); + SDValue MulConsts = DAG.getNode(ISD::FMUL, SL, VT, N01, N1); + return DAG.getNode(ISD::FMUL, SL, VT, N0.getOperand(0), MulConsts); + } } // fold (fmul (fadd x, x), c) -> (fmul x, (fmul 2.0, c)) @@ -11058,7 +11070,7 @@ SDValue RHS = N->getOperand(1); SDValue Shuffle = XformToShuffleWithZero(N); if (Shuffle.getNode()) return Shuffle; - + // If the LHS and RHS are BUILD_VECTOR nodes, see if we can constant fold // this operation. if (LHS.getOpcode() == ISD::BUILD_VECTOR && Index: test/CodeGen/X86/fmul-combines.ll =================================================================== --- test/CodeGen/X86/fmul-combines.ll +++ test/CodeGen/X86/fmul-combines.ll @@ -55,6 +55,36 @@ ret <4 x float> %z } +; We should be able to pre-multiply the two constant vectors. +; CHECK: ## float 5.000000e+00 +; CHECK: ## float 1.200000e+01 +; CHECK: ## float 2.100000e+01 +; CHECK: ## float 3.200000e+01 +; CHECK-LABEL: fmul_v4f32_two_consts_no_splat: +; CHECK: mulps +; CHECK-NOT: mulps +; CHECK-NEXT: ret +define <4 x float> @fmul_v4f32_two_consts_no_splat(<4 x float> %x) #0 { + %y = fmul <4 x float> %x, + %z = fmul <4 x float> %y, + ret <4 x float> %z +} + +; Same as above, but reverse operands to make sure non-canonical form is also handled. +; CHECK: ## float 5.000000e+00 +; CHECK: ## float 1.200000e+01 +; CHECK: ## float 2.100000e+01 +; CHECK: ## float 3.200000e+01 +; CHECK-LABEL: fmul_v4f32_two_consts_no_splat_non_canonical: +; CHECK: mulps +; CHECK-NOT: mulps +; CHECK-NEXT: ret +define <4 x float> @fmul_v4f32_two_consts_no_splat_non_canonical(<4 x float> %x) #0 { + %y = fmul <4 x float> , %x + %z = fmul <4 x float> , %y + ret <4 x float> %z +} + ; CHECK-LABEL: fmul_c2_c4_f32: ; CHECK-NOT: addss ; CHECK: mulss