[x86] Revert r212324 which was too aggressive w.r.t. allowing undef
lanes in vector splats.
The core problem here is that undef lanes can't *unilaterally* be
considered to contribute to splats. Their handling needs to be more
cautious. There is also a reported failure of the nightly testers
(thanks Tobias!) that may well stem from the same core issue. I'm going
to fix this theoretical issue, factor the APIs a bit better, and then
verify that I don't see anything bad with Tobias's reduction from the
test suite before recommitting.
Original commit message for r212324:
[x86] Generalize BuildVectorSDNode::getConstantSplatValue to work for
any constant, constant FP, or undef splat and to tolerate any undef
lanes in a splat, then replace all uses of isSplatVector in X86's
lowering with it.
This fixes issues where undef lanes in an otherwise splat vector would
prevent the splat logic from firing. It is a touch more awkward to use
this interface, but it is much more accurate. Suggestions for better
interface structuring welcome.
With this fix, the code generated with the widening legalization
strategy for widen_cast-4.ll is *dramatically* improved as the special
lowering strategies for a v16i8 SRA kick in even though the high lanes
are undef.
We also get a slightly different choice for broadcasting an aligned
memory location, and use vpshufd instead of vbroadcastss. This looks
like a minor win for pipelining and domain crossing, but a minor loss
for the number of micro-ops. I suspect its a wash, but folks can
easily tweak the lowering if they want.
llvm-svn: 212475
diff --git a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
index 9a91dcc..7198203 100644
--- a/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp
@@ -654,12 +654,13 @@
if (ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N))
return CN;
- if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N))
- if (SDValue Splat = BV->getConstantSplatValue())
- if (auto *CN = dyn_cast<ConstantSDNode>(Splat))
- // BuildVectors can truncate their operands. Ignore that case here.
- if (CN->getValueType(0) == N.getValueType().getScalarType())
- return CN;
+ if (BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N)) {
+ ConstantSDNode *CN = BV->getConstantSplatValue();
+
+ // BuildVectors can truncate their operands. Ignore that case here.
+ if (CN && CN->getValueType(0) == N.getValueType().getScalarType())
+ return CN;
+ }
return nullptr;
}
diff --git a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
index fb7d1b1..3a8a5f9 100644
--- a/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
@@ -6603,28 +6603,16 @@
return true;
}
-SDValue BuildVectorSDNode::getConstantSplatValue() const {
- SDValue Splatted;
- for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
- SDValue Op = getOperand(i);
- if (Op.getOpcode() == ISD::UNDEF)
- continue;
- if (Op.getOpcode() != ISD::Constant && Op.getOpcode() != ISD::ConstantFP)
- return SDValue();
+ConstantSDNode *BuildVectorSDNode::getConstantSplatValue() const {
+ SDValue Op0 = getOperand(0);
+ if (Op0.getOpcode() != ISD::Constant)
+ return nullptr;
- if (!Splatted)
- Splatted = Op;
- else if (Splatted != Op)
- return SDValue();
- }
+ for (unsigned i = 1, e = getNumOperands(); i != e; ++i)
+ if (getOperand(i) != Op0)
+ return nullptr;
- if (!Splatted) {
- assert(getOperand(0).getOpcode() == ISD::UNDEF &&
- "Can only have a splat without a constant for all undefs.");
- return getOperand(0);
- }
-
- return Splatted;
+ return cast<ConstantSDNode>(Op0);
}
bool BuildVectorSDNode::isConstant() const {
diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
index 1b3e428..ad91d4a 100644
--- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp
@@ -1152,15 +1152,14 @@
bool IsVec = false;
const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
- if (!CN)
- if (auto *BV = dyn_cast<BuildVectorSDNode>(N))
- if (SDValue Splat = BV->getConstantSplatValue())
- if (auto *SplatCN = dyn_cast<ConstantSDNode>(Splat)) {
- IsVec = true;
- CN = SplatCN;
- }
- if (!CN)
- return false;
+ if (!CN) {
+ const BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N);
+ if (!BV)
+ return false;
+
+ IsVec = true;
+ CN = BV->getConstantSplatValue();
+ }
switch (getBooleanContents(IsVec)) {
case UndefinedBooleanContent:
@@ -1180,15 +1179,14 @@
bool IsVec = false;
const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
- if (!CN)
- if (auto *BV = dyn_cast<BuildVectorSDNode>(N))
- if (SDValue Splat = BV->getConstantSplatValue())
- if (auto *SplatCN = dyn_cast<ConstantSDNode>(Splat)) {
- IsVec = true;
- CN = SplatCN;
- }
- if (!CN)
- return false;
+ if (!CN) {
+ const BuildVectorSDNode *BV = dyn_cast<BuildVectorSDNode>(N);
+ if (!BV)
+ return false;
+
+ IsVec = true;
+ CN = BV->getConstantSplatValue();
+ }
if (getBooleanContents(IsVec) == UndefinedBooleanContent)
return !CN->getAPIntValue()[0];