@@ -33594,7 +33594,7 @@ static SDValue combineShuffleOfConcatUndef(SDNode *N, SelectionDAG &DAG,
33594
33594
}
33595
33595
33596
33596
/// Eliminate a redundant shuffle of a horizontal math op.
33597
- static SDValue foldShuffleOfHorizOp(SDNode *N) {
33597
+ static SDValue foldShuffleOfHorizOp(SDNode *N, SelectionDAG &DAG ) {
33598
33598
unsigned Opcode = N->getOpcode();
33599
33599
if (Opcode != X86ISD::MOVDDUP && Opcode != X86ISD::VBROADCAST)
33600
33600
if (Opcode != ISD::VECTOR_SHUFFLE || !N->getOperand(1).isUndef())
@@ -33625,6 +33625,25 @@ static SDValue foldShuffleOfHorizOp(SDNode *N) {
33625
33625
HOp.getOperand(0) != HOp.getOperand(1))
33626
33626
return SDValue();
33627
33627
33628
+ // The shuffle that we are eliminating may have allowed the horizontal op to
33629
+ // have an undemanded (undefined) operand. Duplicate the other (defined)
33630
+ // operand to ensure that the results are defined across all lanes without the
33631
+ // shuffle.
33632
+ auto updateHOp = [](SDValue HorizOp, SelectionDAG &DAG) {
33633
+ SDValue X;
33634
+ if (HorizOp.getOperand(0).isUndef()) {
33635
+ assert(!HorizOp.getOperand(1).isUndef() && "Not expecting foldable h-op");
33636
+ X = HorizOp.getOperand(1);
33637
+ } else if (HorizOp.getOperand(1).isUndef()) {
33638
+ assert(!HorizOp.getOperand(0).isUndef() && "Not expecting foldable h-op");
33639
+ X = HorizOp.getOperand(0);
33640
+ } else {
33641
+ return HorizOp;
33642
+ }
33643
+ return DAG.getNode(HorizOp.getOpcode(), SDLoc(HorizOp),
33644
+ HorizOp.getValueType(), X, X);
33645
+ };
33646
+
33628
33647
// When the operands of a horizontal math op are identical, the low half of
33629
33648
// the result is the same as the high half. If a target shuffle is also
33630
33649
// replicating low and high halves, we don't need the shuffle.
@@ -33635,7 +33654,7 @@ static SDValue foldShuffleOfHorizOp(SDNode *N) {
33635
33654
assert((HOp.getValueType() == MVT::v2f64 ||
33636
33655
HOp.getValueType() == MVT::v4f64) && HOp.getValueType() == VT &&
33637
33656
"Unexpected type for h-op");
33638
- return HOp;
33657
+ return updateHOp( HOp, DAG) ;
33639
33658
}
33640
33659
return SDValue();
33641
33660
}
@@ -33649,14 +33668,14 @@ static SDValue foldShuffleOfHorizOp(SDNode *N) {
33649
33668
(isTargetShuffleEquivalent(Mask, {0, 0}) ||
33650
33669
isTargetShuffleEquivalent(Mask, {0, 1, 0, 1}) ||
33651
33670
isTargetShuffleEquivalent(Mask, {0, 1, 2, 3, 0, 1, 2, 3})))
33652
- return HOp;
33671
+ return updateHOp( HOp, DAG) ;
33653
33672
33654
33673
if (HOp.getValueSizeInBits() == 256 &&
33655
33674
(isTargetShuffleEquivalent(Mask, {0, 0, 2, 2}) ||
33656
33675
isTargetShuffleEquivalent(Mask, {0, 1, 0, 1, 4, 5, 4, 5}) ||
33657
33676
isTargetShuffleEquivalent(
33658
33677
Mask, {0, 1, 2, 3, 0, 1, 2, 3, 8, 9, 10, 11, 8, 9, 10, 11})))
33659
- return HOp;
33678
+ return updateHOp( HOp, DAG) ;
33660
33679
33661
33680
return SDValue();
33662
33681
}
@@ -33710,7 +33729,7 @@ static SDValue combineShuffle(SDNode *N, SelectionDAG &DAG,
33710
33729
if (SDValue AddSub = combineShuffleToAddSubOrFMAddSub(N, Subtarget, DAG))
33711
33730
return AddSub;
33712
33731
33713
- if (SDValue HAddSub = foldShuffleOfHorizOp(N))
33732
+ if (SDValue HAddSub = foldShuffleOfHorizOp(N, DAG ))
33714
33733
return HAddSub;
33715
33734
}
33716
33735
0 commit comments