Skip to content

Commit 47f5744

Browse files
committed
[PatternMatch] Add a matching helper m_ElementWiseBitCast. NFC.
1 parent 930996e commit 47f5744

File tree

5 files changed

+43
-28
lines changed

5 files changed

+43
-28
lines changed

llvm/include/llvm/IR/PatternMatch.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1711,6 +1711,30 @@ m_BitCast(const OpTy &Op) {
17111711
return CastOperator_match<OpTy, Instruction::BitCast>(Op);
17121712
}
17131713

1714+
template <typename Op_t> struct ElementWiseBitCast_match {
1715+
Op_t Op;
1716+
1717+
ElementWiseBitCast_match(const Op_t &OpMatch) : Op(OpMatch) {}
1718+
1719+
template <typename OpTy> bool match(OpTy *V) {
1720+
if (auto *I = dyn_cast<BitCastInst>(V)) {
1721+
Type *SrcType = I->getSrcTy();
1722+
Type *DstType = I->getType();
1723+
// Make sure the bitcast doesn't change between scalar and vector and
1724+
// doesn't change the number of vector elements.
1725+
if (SrcType->isVectorTy() == DstType->isVectorTy() &&
1726+
SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits())
1727+
return Op.match(I->getOperand(0));
1728+
}
1729+
return false;
1730+
}
1731+
};
1732+
1733+
template <typename OpTy>
1734+
inline ElementWiseBitCast_match<OpTy> m_ElementWiseBitCast(const OpTy &Op) {
1735+
return ElementWiseBitCast_match<OpTy>(Op);
1736+
}
1737+
17141738
/// Matches PtrToInt.
17151739
template <typename OpTy>
17161740
inline CastOperator_match<OpTy, Instruction::PtrToInt>

llvm/lib/Analysis/InstructionSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3034,7 +3034,7 @@ static Value *simplifyICmpWithConstant(CmpInst::Predicate Pred, Value *LHS,
30343034
// floating-point casts:
30353035
// icmp slt (bitcast (uitofp X)), 0 --> false
30363036
// icmp sgt (bitcast (uitofp X)), -1 --> true
3037-
if (match(LHS, m_BitCast(m_UIToFP(m_Value(X))))) {
3037+
if (match(LHS, m_ElementWiseBitCast(m_UIToFP(m_Value(X))))) {
30383038
if (Pred == ICmpInst::ICMP_SLT && match(RHS, m_Zero()))
30393039
return ConstantInt::getFalse(ITy);
30403040
if (Pred == ICmpInst::ICMP_SGT && match(RHS, m_AllOnes()))

llvm/lib/Transforms/InstCombine/InstCombineAndOrXor.cpp

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,14 +2493,12 @@ Instruction *InstCombinerImpl::visitAnd(BinaryOperator &I) {
24932493
// Assumes any IEEE-represented type has the sign bit in the high bit.
24942494
// TODO: Unify with APInt matcher. This version allows undef unlike m_APInt
24952495
Value *CastOp;
2496-
if (match(Op0, m_BitCast(m_Value(CastOp))) &&
2496+
if (match(Op0, m_ElementWiseBitCast(m_Value(CastOp))) &&
24972497
match(Op1, m_MaxSignedValue()) &&
24982498
!Builder.GetInsertBlock()->getParent()->hasFnAttribute(
2499-
Attribute::NoImplicitFloat)) {
2499+
Attribute::NoImplicitFloat)) {
25002500
Type *EltTy = CastOp->getType()->getScalarType();
2501-
if (EltTy->isFloatingPointTy() && EltTy->isIEEE() &&
2502-
EltTy->getPrimitiveSizeInBits() ==
2503-
I.getType()->getScalarType()->getPrimitiveSizeInBits()) {
2501+
if (EltTy->isFloatingPointTy() && EltTy->isIEEE()) {
25042502
Value *FAbs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, CastOp);
25052503
return new BitCastInst(FAbs, I.getType());
25062504
}
@@ -3925,13 +3923,12 @@ Instruction *InstCombinerImpl::visitOr(BinaryOperator &I) {
39253923
// This is generous interpretation of noimplicitfloat, this is not a true
39263924
// floating-point operation.
39273925
Value *CastOp;
3928-
if (match(Op0, m_BitCast(m_Value(CastOp))) && match(Op1, m_SignMask()) &&
3926+
if (match(Op0, m_ElementWiseBitCast(m_Value(CastOp))) &&
3927+
match(Op1, m_SignMask()) &&
39293928
!Builder.GetInsertBlock()->getParent()->hasFnAttribute(
39303929
Attribute::NoImplicitFloat)) {
39313930
Type *EltTy = CastOp->getType()->getScalarType();
3932-
if (EltTy->isFloatingPointTy() && EltTy->isIEEE() &&
3933-
EltTy->getPrimitiveSizeInBits() ==
3934-
I.getType()->getScalarType()->getPrimitiveSizeInBits()) {
3931+
if (EltTy->isFloatingPointTy() && EltTy->isIEEE()) {
39353932
Value *FAbs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, CastOp);
39363933
Value *FNegFAbs = Builder.CreateFNeg(FAbs);
39373934
return new BitCastInst(FNegFAbs, I.getType());
@@ -4701,13 +4698,12 @@ Instruction *InstCombinerImpl::visitXor(BinaryOperator &I) {
47014698
// Assumes any IEEE-represented type has the sign bit in the high bit.
47024699
// TODO: Unify with APInt matcher. This version allows undef unlike m_APInt
47034700
Value *CastOp;
4704-
if (match(Op0, m_BitCast(m_Value(CastOp))) && match(Op1, m_SignMask()) &&
4701+
if (match(Op0, m_ElementWiseBitCast(m_Value(CastOp))) &&
4702+
match(Op1, m_SignMask()) &&
47054703
!Builder.GetInsertBlock()->getParent()->hasFnAttribute(
47064704
Attribute::NoImplicitFloat)) {
47074705
Type *EltTy = CastOp->getType()->getScalarType();
4708-
if (EltTy->isFloatingPointTy() && EltTy->isIEEE() &&
4709-
EltTy->getPrimitiveSizeInBits() ==
4710-
I.getType()->getScalarType()->getPrimitiveSizeInBits()) {
4706+
if (EltTy->isFloatingPointTy() && EltTy->isIEEE()) {
47114707
Value *FNeg = Builder.CreateFNeg(CastOp);
47124708
return new BitCastInst(FNeg, I.getType());
47134709
}

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,15 +1834,10 @@ Instruction *InstCombinerImpl::foldICmpAndConstConst(ICmpInst &Cmp,
18341834
Value *V;
18351835
if (!Cmp.getParent()->getParent()->hasFnAttribute(
18361836
Attribute::NoImplicitFloat) &&
1837-
Cmp.isEquality() && match(X, m_OneUse(m_BitCast(m_Value(V))))) {
1838-
Type *SrcType = V->getType();
1839-
Type *DstType = X->getType();
1840-
Type *FPType = SrcType->getScalarType();
1841-
// Make sure the bitcast doesn't change between scalar and vector and
1842-
// doesn't change the number of vector elements.
1843-
if (SrcType->isVectorTy() == DstType->isVectorTy() &&
1844-
SrcType->getScalarSizeInBits() == DstType->getScalarSizeInBits() &&
1845-
FPType->isIEEELikeFPTy() && C1 == *C2) {
1837+
Cmp.isEquality() &&
1838+
match(X, m_OneUse(m_ElementWiseBitCast(m_Value(V))))) {
1839+
Type *FPType = V->getType()->getScalarType();
1840+
if (FPType->isIEEELikeFPTy() && C1 == *C2) {
18461841
APInt ExponentMask =
18471842
APFloat::getInf(FPType->getFltSemantics()).bitcastToAPInt();
18481843
if (C1 == ExponentMask) {
@@ -7754,9 +7749,7 @@ Instruction *InstCombinerImpl::visitFCmpInst(FCmpInst &I) {
77547749
// Ignore signbit of bitcasted int when comparing equality to FP 0.0:
77557750
// fcmp oeq/une (bitcast X), 0.0 --> (and X, SignMaskC) ==/!= 0
77567751
if (match(Op1, m_PosZeroFP()) &&
7757-
match(Op0, m_OneUse(m_BitCast(m_Value(X)))) &&
7758-
X->getType()->isVectorTy() == OpType->isVectorTy() &&
7759-
X->getType()->getScalarSizeInBits() == OpType->getScalarSizeInBits()) {
7752+
match(Op0, m_OneUse(m_ElementWiseBitCast(m_Value(X))))) {
77607753
ICmpInst::Predicate IntPred = ICmpInst::BAD_ICMP_PREDICATE;
77617754
if (Pred == FCmpInst::FCMP_OEQ)
77627755
IntPred = ICmpInst::ICMP_EQ;

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2382,7 +2382,8 @@ static Instruction *foldSelectToCopysign(SelectInst &Sel,
23822382
const APInt *C;
23832383
bool IsTrueIfSignSet;
23842384
ICmpInst::Predicate Pred;
2385-
if (!match(Cond, m_OneUse(m_ICmp(Pred, m_BitCast(m_Value(X)), m_APInt(C)))) ||
2385+
if (!match(Cond, m_OneUse(m_ICmp(Pred, m_ElementWiseBitCast(m_Value(X)),
2386+
m_APInt(C)))) ||
23862387
!InstCombiner::isSignBitCheck(Pred, *C, IsTrueIfSignSet) ||
23872388
X->getType() != SelType)
23882389
return nullptr;
@@ -2783,7 +2784,8 @@ static Instruction *foldSelectWithFCmpToFabs(SelectInst &SI,
27832784
CmpInst::Predicate Pred;
27842785
const APInt *C;
27852786
bool TrueIfSigned;
2786-
if (!match(CondVal, m_ICmp(Pred, m_BitCast(m_Specific(X)), m_APInt(C))) ||
2787+
if (!match(CondVal,
2788+
m_ICmp(Pred, m_ElementWiseBitCast(m_Specific(X)), m_APInt(C))) ||
27872789
!IC.isSignBitCheck(Pred, *C, TrueIfSigned))
27882790
continue;
27892791
if (!match(TrueVal, m_FNeg(m_Specific(X))))

0 commit comments

Comments
 (0)