[SelectionDAG] Enhance SDTCisSameNumEltsAs to work with scalar types and use it on extend/trunc/round operations.

Currently we don't enforce that ISD::ANY_EXTEND, ZERO_EXTEND, SIGN_EXTEND, TRUNC, FP_ROUND, FP_EXTEND have the same number of elements(including scalar) between their input and output. Though we have them documented as such. Up until a few months ago x86 created nodes that violated this rule. That's all been fixed now, and we should enforce the rule going forward.

In order to do this we need to allow SDTCisSameNumEltsAs to support scalar types and not enforce being a vector. If one type is scalar we will force the other type to also be scalar.

Differential Revision: https://reviews.llvm.org/D30878

llvm-svn: 297648
This commit is contained in:
Craig Topper 2017-03-13 17:37:14 +00:00
parent 8f6bdf69a2
commit 13a3af1931
3 changed files with 64 additions and 45 deletions

View File

@ -136,25 +136,25 @@ def SDTIntUnaryOp : SDTypeProfile<1, 1, [ // ctlz
SDTCisSameAs<0, 1>, SDTCisInt<0>
]>;
def SDTIntExtendOp : SDTypeProfile<1, 1, [ // sext, zext, anyext
SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<1, 0>
SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<1, 0>, SDTCisSameNumEltsAs<0, 1>
]>;
def SDTIntTruncOp : SDTypeProfile<1, 1, [ // trunc
SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<0, 1>
SDTCisInt<0>, SDTCisInt<1>, SDTCisOpSmallerThanOp<0, 1>, SDTCisSameNumEltsAs<0, 1>
]>;
def SDTFPUnaryOp : SDTypeProfile<1, 1, [ // fneg, fsqrt, etc
SDTCisSameAs<0, 1>, SDTCisFP<0>
]>;
def SDTFPRoundOp : SDTypeProfile<1, 1, [ // fround
SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<0, 1>
SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<0, 1>, SDTCisSameNumEltsAs<0, 1>
]>;
def SDTFPExtendOp : SDTypeProfile<1, 1, [ // fextend
SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<1, 0>
SDTCisFP<0>, SDTCisFP<1>, SDTCisOpSmallerThanOp<1, 0>, SDTCisSameNumEltsAs<0, 1>
]>;
def SDTIntToFPOp : SDTypeProfile<1, 1, [ // [su]int_to_fp
SDTCisFP<0>, SDTCisInt<1>
SDTCisFP<0>, SDTCisInt<1>, SDTCisSameNumEltsAs<0, 1>
]>;
def SDTFPToIntOp : SDTypeProfile<1, 1, [ // fp_to_[su]int
SDTCisInt<0>, SDTCisFP<1>
SDTCisInt<0>, SDTCisFP<1>, SDTCisSameNumEltsAs<0, 1>
]>;
def SDTExtInreg : SDTypeProfile<1, 2, [ // sext_inreg
SDTCisSameAs<0, 1>, SDTCisInt<0>, SDTCisVT<2, OtherVT>,
@ -174,7 +174,7 @@ def SDTSelect : SDTypeProfile<1, 3, [ // select
]>;
def SDTVSelect : SDTypeProfile<1, 3, [ // vselect
SDTCisInt<1>, SDTCisSameAs<0, 2>, SDTCisSameAs<2, 3>, SDTCisSameNumEltsAs<0, 1>
SDTCisVec<0>, SDTCisInt<1>, SDTCisSameAs<0, 2>, SDTCisSameAs<2, 3>, SDTCisSameNumEltsAs<0, 1>
]>;
def SDTSelectCC : SDTypeProfile<1, 5, [ // select_cc

View File

@ -580,56 +580,74 @@ bool EEVT::TypeSet::EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VTOperand,
return MadeChange;
}
/// EnforceVectorSameNumElts - 'this' is now constrained to
/// be a vector with same num elements as VTOperand.
bool EEVT::TypeSet::EnforceVectorSameNumElts(EEVT::TypeSet &VTOperand,
TreePattern &TP) {
/// EnforceameNumElts - If VTOperand is a scalar, then 'this' is a scalar. If
/// VTOperand is a vector, then 'this' must have the same number of elements.
bool EEVT::TypeSet::EnforceSameNumElts(EEVT::TypeSet &VTOperand,
TreePattern &TP) {
if (TP.hasError())
return false;
// "This" must be a vector and "VTOperand" must be a vector.
bool MadeChange = false;
MadeChange |= EnforceVector(TP);
MadeChange |= VTOperand.EnforceVector(TP);
// If we know one of the vector types, it forces the other type to agree.
if (isCompletelyUnknown())
MadeChange = FillWithPossibleTypes(TP);
if (VTOperand.isCompletelyUnknown())
MadeChange = VTOperand.FillWithPossibleTypes(TP);
// If one contains vectors but the other doesn't pull vectors out.
if (!hasVectorTypes())
MadeChange |= VTOperand.EnforceScalar(TP);
else if (!hasScalarTypes())
MadeChange |= VTOperand.EnforceVector(TP);
if (!VTOperand.hasVectorTypes())
MadeChange |= EnforceScalar(TP);
else if (!VTOperand.hasScalarTypes())
MadeChange |= EnforceVector(TP);
// If one type is a vector, make sure the other has the same element count.
// If this a scalar, then we are already done with the above.
if (isConcrete()) {
MVT IVT = getConcrete();
unsigned NumElems = IVT.getVectorNumElements();
if (IVT.isVector()) {
unsigned NumElems = IVT.getVectorNumElements();
// Only keep types that have same elements as 'this'.
TypeSet InputSet(VTOperand);
// Only keep types that have same elements as 'this'.
TypeSet InputSet(VTOperand);
auto I = remove_if(VTOperand.TypeVec, [NumElems](MVT VVT) {
return VVT.getVectorNumElements() != NumElems;
});
MadeChange |= I != VTOperand.TypeVec.end();
VTOperand.TypeVec.erase(I, VTOperand.TypeVec.end());
auto I = remove_if(VTOperand.TypeVec, [NumElems](MVT VVT) {
return VVT.getVectorNumElements() != NumElems;
});
MadeChange |= I != VTOperand.TypeVec.end();
VTOperand.TypeVec.erase(I, VTOperand.TypeVec.end());
if (VTOperand.TypeVec.empty()) { // FIXME: Really want an SMLoc here!
TP.error("Type inference contradiction found, forcing '" +
InputSet.getName() + "' to have same number elements as '" +
getName() + "'");
return false;
if (VTOperand.TypeVec.empty()) { // FIXME: Really want an SMLoc here!
TP.error("Type inference contradiction found, forcing '" +
InputSet.getName() + "' to have same number elements as '" +
getName() + "'");
return false;
}
}
} else if (VTOperand.isConcrete()) {
MVT IVT = VTOperand.getConcrete();
unsigned NumElems = IVT.getVectorNumElements();
if (IVT.isVector()) {
unsigned NumElems = IVT.getVectorNumElements();
// Only keep types that have same elements as VTOperand.
TypeSet InputSet(*this);
// Only keep types that have same elements as VTOperand.
TypeSet InputSet(*this);
auto I = remove_if(TypeVec, [NumElems](MVT VVT) {
return VVT.getVectorNumElements() != NumElems;
});
MadeChange |= I != TypeVec.end();
TypeVec.erase(I, TypeVec.end());
auto I = remove_if(TypeVec, [NumElems](MVT VVT) {
return VVT.getVectorNumElements() != NumElems;
});
MadeChange |= I != TypeVec.end();
TypeVec.erase(I, TypeVec.end());
if (TypeVec.empty()) { // FIXME: Really want an SMLoc here!
TP.error("Type inference contradiction found, forcing '" +
InputSet.getName() + "' to have same number elements than '" +
VTOperand.getName() + "'");
return false;
if (TypeVec.empty()) { // FIXME: Really want an SMLoc here!
TP.error("Type inference contradiction found, forcing '" +
InputSet.getName() + "' to have same number elements than '" +
VTOperand.getName() + "'");
return false;
}
}
}
@ -1064,7 +1082,7 @@ bool SDTypeConstraint::ApplyTypeConstraint(TreePatternNode *N,
getOperandNum(x.SDTCisSameNumEltsAs_Info.OtherOperandNum,
N, NodeInfo, OResNo);
return OtherNode->getExtType(OResNo).
EnforceVectorSameNumElts(NodeToApply->getExtType(ResNo), TP);
EnforceSameNumElts(NodeToApply->getExtType(ResNo), TP);
}
case SDTCisSameSizeAs: {
unsigned OResNo = 0;

View File

@ -144,9 +144,10 @@ namespace EEVT {
/// be a vector type VT.
bool EnforceVectorSubVectorTypeIs(EEVT::TypeSet &VT, TreePattern &TP);
/// EnforceVectorSameNumElts - 'this' is now constrained to
/// be a vector with same num elements as VT.
bool EnforceVectorSameNumElts(EEVT::TypeSet &VT, TreePattern &TP);
/// EnforceSameNumElts - If VTOperand is a scalar, then 'this' is a scalar.
/// If VTOperand is a vector, then 'this' must have the same number of
/// elements.
bool EnforceSameNumElts(EEVT::TypeSet &VT, TreePattern &TP);
/// EnforceSameSize - 'this' is now constrained to be the same size as VT.
bool EnforceSameSize(EEVT::TypeSet &VT, TreePattern &TP);