Skip to content

Commit 0cb8593

Browse files
michaelselehovronlieb
authored andcommitted
Revert "Recommit "[InstCombine] Expand foldSelectICmpAndOr -> foldSelectICmpAndBinOp to work for more binops" (3rd Try)"
Backup fix for SWDEV-454675 This reverts commit 54ec8bc. Change-Id: Ie75aad19fe459d93377a7d07dab3f06e1bc27bc1
1 parent 301a848 commit 0cb8593

File tree

3 files changed

+80
-133
lines changed

3 files changed

+80
-133
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -689,32 +689,25 @@ static Value *foldSelectICmpLshrAshr(const ICmpInst *IC, Value *TrueVal,
689689
}
690690

691691
/// We want to turn:
692-
/// (select (icmp eq (and X, C1), 0), Y, (BinOp Y, C2))
692+
/// (select (icmp eq (and X, C1), 0), Y, (or Y, C2))
693693
/// into:
694-
/// IF C2 u>= C1
695-
/// (BinOp Y, (shl (and X, C1), C3))
696-
/// ELSE
697-
/// (BinOp Y, (lshr (and X, C1), C3))
694+
/// (or (shl (and X, C1), C3), Y)
698695
/// iff:
699-
/// 0 on the RHS is the identity value (i.e add, xor, shl, etc...)
700696
/// C1 and C2 are both powers of 2
701697
/// where:
702-
/// IF C2 u>= C1
703-
/// C3 = Log(C2) - Log(C1)
704-
/// ELSE
705-
/// C3 = Log(C1) - Log(C2)
698+
/// C3 = Log(C2) - Log(C1)
706699
///
707700
/// This transform handles cases where:
708701
/// 1. The icmp predicate is inverted
709702
/// 2. The select operands are reversed
710703
/// 3. The magnitude of C2 and C1 are flipped
711-
static Value *foldSelectICmpAndBinOp(const ICmpInst *IC, Value *TrueVal,
704+
static Value *foldSelectICmpAndOr(const ICmpInst *IC, Value *TrueVal,
712705
Value *FalseVal,
713706
InstCombiner::BuilderTy &Builder) {
714707
// Only handle integer compares. Also, if this is a vector select, we need a
715708
// vector compare.
716709
if (!TrueVal->getType()->isIntOrIntVectorTy() ||
717-
TrueVal->getType()->isVectorTy() != IC->getType()->isVectorTy())
710+
TrueVal->getType()->isVectorTy() != IC->getType()->isVectorTy())
718711
return nullptr;
719712

720713
Value *CmpLHS = IC->getOperand(0);
@@ -742,29 +735,21 @@ static Value *foldSelectICmpAndBinOp(const ICmpInst *IC, Value *TrueVal,
742735
NeedAnd = true;
743736
}
744737

745-
Value *Y, *V = CmpLHS;
746-
BinaryOperator *BinOp;
738+
Value *Or, *Y, *V = CmpLHS;
747739
const APInt *C2;
748740
bool NeedXor;
749-
if (match(FalseVal, m_BinOp(m_Specific(TrueVal), m_Power2(C2)))) {
741+
if (match(FalseVal, m_Or(m_Specific(TrueVal), m_Power2(C2)))) {
750742
Y = TrueVal;
751-
BinOp = cast<BinaryOperator>(FalseVal);
743+
Or = FalseVal;
752744
NeedXor = Pred == ICmpInst::ICMP_NE;
753-
} else if (match(TrueVal, m_BinOp(m_Specific(FalseVal), m_Power2(C2)))) {
745+
} else if (match(TrueVal, m_Or(m_Specific(FalseVal), m_Power2(C2)))) {
754746
Y = FalseVal;
755-
BinOp = cast<BinaryOperator>(TrueVal);
747+
Or = TrueVal;
756748
NeedXor = Pred == ICmpInst::ICMP_EQ;
757749
} else {
758750
return nullptr;
759751
}
760752

761-
// Check that 0 on RHS is identity value for this binop.
762-
auto *IdentityC =
763-
ConstantExpr::getBinOpIdentity(BinOp->getOpcode(), BinOp->getType(),
764-
/*AllowRHSConstant*/ true);
765-
if (IdentityC == nullptr || !IdentityC->isNullValue())
766-
return nullptr;
767-
768753
unsigned C2Log = C2->logBase2();
769754

770755
bool NeedShift = C1Log != C2Log;
@@ -773,7 +758,7 @@ static Value *foldSelectICmpAndBinOp(const ICmpInst *IC, Value *TrueVal,
773758

774759
// Make sure we don't create more instructions than we save.
775760
if ((NeedShift + NeedXor + NeedZExtTrunc + NeedAnd) >
776-
(IC->hasOneUse() + BinOp->hasOneUse()))
761+
(IC->hasOneUse() + Or->hasOneUse()))
777762
return nullptr;
778763

779764
if (NeedAnd) {
@@ -794,7 +779,7 @@ static Value *foldSelectICmpAndBinOp(const ICmpInst *IC, Value *TrueVal,
794779
if (NeedXor)
795780
V = Builder.CreateXor(V, *C2);
796781

797-
return Builder.CreateBinOp(BinOp->getOpcode(), Y, V);
782+
return Builder.CreateOr(V, Y);
798783
}
799784

800785
/// Canonicalize a set or clear of a masked set of constant bits to
@@ -1792,7 +1777,7 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
17921777
if (Instruction *V = foldSelectZeroOrOnes(ICI, TrueVal, FalseVal, Builder))
17931778
return V;
17941779

1795-
if (Value *V = foldSelectICmpAndBinOp(ICI, TrueVal, FalseVal, Builder))
1780+
if (Value *V = foldSelectICmpAndOr(ICI, TrueVal, FalseVal, Builder))
17961781
return replaceInstUsesWith(SI, V);
17971782

17981783
if (Value *V = foldSelectICmpLshrAshr(ICI, TrueVal, FalseVal, Builder))

0 commit comments

Comments
 (0)