Skip to content

Commit 70553da

Browse files
committed
Adding missed optimisation
1 parent 056ad41 commit 70553da

File tree

2 files changed

+41
-5
lines changed

2 files changed

+41
-5
lines changed

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,40 @@ static Value *foldAbsDiff(ICmpInst *Cmp, Value *TVal, Value *FVal,
10731073
return nullptr;
10741074
}
10751075

1076+
// (A % 2 == 0) ? (A/2*2) : B --> (A % 2 == 0) ? A : B
1077+
// (A % 2 == 0) ? (A/2*2) * (A/2*2) : B --> (A % 2 == 0) ? BinOp A, A : B
1078+
static Value *foldSelectWithIcmpEqAndPattern(ICmpInst *Cmp, Value *TVal,
1079+
Value *FVal,
1080+
InstCombiner::BuilderTy &Builder) {
1081+
Value *A;
1082+
ConstantInt *MaskedConstant;
1083+
ICmpInst::Predicate Pred = Cmp->getPredicate();
1084+
1085+
// Checks if the comparison is (A % 2 == 0) and A is not undef.
1086+
if (!(Pred == ICmpInst::ICMP_EQ &&
1087+
match(Cmp->getOperand(0), m_And(m_Value(A), m_SpecificInt(1))) &&
1088+
match(Cmp->getOperand(1), m_SpecificInt(0)) &&
1089+
isGuaranteedNotToBeUndef(A)))
1090+
return nullptr;
1091+
1092+
// Checks if true branch matches (A % 2).
1093+
if (match(TVal,
1094+
m_OneUse(m_And(m_Specific(A), m_ConstantInt(MaskedConstant)))) &&
1095+
MaskedConstant->getValue().getSExtValue() == -2)
1096+
return Builder.CreateSelect(Cmp, A, FVal);
1097+
1098+
// Checks if true branch is (A/2*2) * (A/2*2).
1099+
Value *MulVal;
1100+
if (match(TVal, m_OneUse(m_Mul(m_Value(MulVal), m_Deferred(MulVal)))))
1101+
if (match(MulVal, m_And(m_Specific(A), m_ConstantInt(MaskedConstant))) &&
1102+
MaskedConstant->getValue().getSExtValue() == -2) {
1103+
Value *NewBinop = Builder.CreateMul(A, A);
1104+
return Builder.CreateSelect(Cmp, NewBinop, FVal);
1105+
}
1106+
1107+
return nullptr;
1108+
}
1109+
10761110
/// Fold the following code sequence:
10771111
/// \code
10781112
/// int a = ctlz(x & -x);
@@ -1933,6 +1967,10 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
19331967
if (Value *V = foldAbsDiff(ICI, TrueVal, FalseVal, Builder))
19341968
return replaceInstUsesWith(SI, V);
19351969

1970+
if (Value *V =
1971+
foldSelectWithIcmpEqAndPattern(ICI, TrueVal, FalseVal, Builder))
1972+
return replaceInstUsesWith(SI, V);
1973+
19361974
return Changed ? &SI : nullptr;
19371975
}
19381976

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,9 +1460,8 @@ define i8 @select_icmp_eq_mul_and(i8 noundef %a, i8 %b) {
14601460
; CHECK-LABEL: @select_icmp_eq_mul_and(
14611461
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[A:%.*]], 1
14621462
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[TMP1]], 0
1463-
; CHECK-NEXT: [[DIV7:%.*]] = and i8 [[A]], -2
1464-
; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[DIV7]], [[DIV7]]
1465-
; CHECK-NEXT: [[RETVAL_0:%.*]] = select i1 [[CMP]], i8 [[MUL]], i8 [[B:%.*]]
1463+
; CHECK-NEXT: [[TMP2:%.*]] = mul i8 [[A]], [[A]]
1464+
; CHECK-NEXT: [[RETVAL_0:%.*]] = select i1 [[CMP]], i8 [[TMP2]], i8 [[B:%.*]]
14661465
; CHECK-NEXT: ret i8 [[RETVAL_0]]
14671466
;
14681467
%1 = and i8 %a, 1
@@ -1477,8 +1476,7 @@ define i8 @select_icmp_eq_and(i8 noundef %a, i8 %b) {
14771476
; CHECK-LABEL: @select_icmp_eq_and(
14781477
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[A:%.*]], 1
14791478
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[TMP1]], 0
1480-
; CHECK-NEXT: [[DIV7:%.*]] = and i8 [[A]], -2
1481-
; CHECK-NEXT: [[RETVAL_0:%.*]] = select i1 [[CMP]], i8 [[DIV7]], i8 [[B:%.*]]
1479+
; CHECK-NEXT: [[RETVAL_0:%.*]] = select i1 [[CMP]], i8 [[A]], i8 [[B:%.*]]
14821480
; CHECK-NEXT: ret i8 [[RETVAL_0]]
14831481
;
14841482
%1 = and i8 %a, 1

0 commit comments

Comments
 (0)