Skip to content

Commit f6465c1

Browse files
committed
Adding the missed optimisation
1 parent daa105e commit f6465c1

File tree

5 files changed

+73
-16
lines changed

5 files changed

+73
-16
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ void computeKnownBitsFromRangeMetadata(const MDNode &Ranges, KnownBits &Known);
9494
void computeKnownBitsFromContext(const Value *V, KnownBits &Known,
9595
unsigned Depth, const SimplifyQuery &Q);
9696

97+
void computeKnownBitsFromCond(const Value *V, Value *Cond, KnownBits &Known,
98+
unsigned Depth, const SimplifyQuery &SQ,
99+
bool Invert);
100+
97101
/// Using KnownBits LHS/RHS produce the known bits for logic op (and/xor/or).
98102
KnownBits analyzeKnownBitsFromAndXorOr(const Operator *I,
99103
const KnownBits &KnownLHS,

llvm/include/llvm/Transforms/InstCombine/InstCombiner.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,13 @@ class LLVM_LIBRARY_VISIBILITY InstCombiner {
438438
return llvm::computeKnownBits(V, Depth, SQ.getWithInstruction(CxtI));
439439
}
440440

441+
void computeKnownBitsFromCond(const Value *V, ICmpInst *Cmp, KnownBits &Known,
442+
unsigned Depth, const Instruction *CxtI,
443+
bool Invert) const {
444+
llvm::computeKnownBitsFromCond(V, Cmp, Known, Depth,
445+
SQ.getWithInstruction(CxtI), Invert);
446+
}
447+
441448
bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero = false,
442449
unsigned Depth = 0,
443450
const Instruction *CxtI = nullptr) {

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -747,9 +747,9 @@ static void computeKnownBitsFromICmpCond(const Value *V, ICmpInst *Cmp,
747747
computeKnownBitsFromCmp(V, Pred, LHS, RHS, Known, SQ);
748748
}
749749

750-
static void computeKnownBitsFromCond(const Value *V, Value *Cond,
751-
KnownBits &Known, unsigned Depth,
752-
const SimplifyQuery &SQ, bool Invert) {
750+
void llvm::computeKnownBitsFromCond(const Value *V, Value *Cond,
751+
KnownBits &Known, unsigned Depth,
752+
const SimplifyQuery &SQ, bool Invert) {
753753
Value *A, *B;
754754
if (Depth < MaxAnalysisRecursionDepth &&
755755
match(Cond, m_LogicalOp(m_Value(A), m_Value(B)))) {

llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp

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

1076+
// When the lsb of cond is 0:
1077+
// cond ? A & -2 : B --> cond ? A : B
1078+
// cond ? BinOp (A & -2), (A & -2) : B --> cond ? BinOp A, A : B
1079+
static Value *foldSelectWithIcmpEqAndPattern(ICmpInst *Cmp, Value *TVal,
1080+
Value *FVal,
1081+
InstCombiner::BuilderTy &Builder,
1082+
SelectInst &SI,
1083+
InstCombinerImpl &IC) {
1084+
auto matchesAndPattern = [](ICmpInst *Cmp,
1085+
Value *TVal, Value *&A,
1086+
SelectInst &SI,
1087+
InstCombinerImpl &IC) -> bool {
1088+
ConstantInt *MaskedConstant;
1089+
1090+
// Check if TVal matches the pattern 'A & -2'
1091+
if (match(TVal, m_c_And(m_Value(A), m_ConstantInt(MaskedConstant))) &&
1092+
MaskedConstant->getValue().getSExtValue() == -2 &&
1093+
isGuaranteedNotToBeUndef(A)) {
1094+
KnownBits Known;
1095+
Known = IC.computeKnownBits(A, 0, &SI);
1096+
IC.computeKnownBitsFromCond(A, Cmp, Known, 0, &SI, false);
1097+
if (Known.Zero[0])
1098+
return true;
1099+
}
1100+
return false;
1101+
};
1102+
1103+
Value *A;
1104+
1105+
// Checks if true branch matches the pattern 'A % 2'.
1106+
if (matchesAndPattern(Cmp, TVal, A, SI, IC))
1107+
return Builder.CreateSelect(Cmp, A, FVal);
1108+
1109+
// Checks if true branch matches nested 'A % 2' within a binary operation.
1110+
1111+
Value *MulVal;
1112+
if (match(TVal, m_OneUse(m_BinOp(m_Value(MulVal), m_Deferred(MulVal)))))
1113+
if (matchesAndPattern(Cmp, MulVal, A, SI, IC)) {
1114+
// Use replaceInInstruction to substitute `and i8 %a, -2` with `%a`
1115+
if (IC.replaceInInstruction(TVal, MulVal, A, 0)) {
1116+
return &SI;
1117+
}
1118+
}
1119+
1120+
return nullptr;
1121+
}
1122+
10761123
/// Fold the following code sequence:
10771124
/// \code
10781125
/// int a = ctlz(x & -x);
@@ -1951,6 +1998,10 @@ Instruction *InstCombinerImpl::foldSelectInstWithICmp(SelectInst &SI,
19511998
if (Value *V = foldAbsDiff(ICI, TrueVal, FalseVal, Builder))
19521999
return replaceInstUsesWith(SI, V);
19532000

2001+
if (Value *V = foldSelectWithIcmpEqAndPattern(ICI, TrueVal, FalseVal, Builder,
2002+
SI, *this))
2003+
return replaceInstUsesWith(SI, V);
2004+
19542005
return Changed ? &SI : nullptr;
19552006
}
19562007

llvm/test/Transforms/InstCombine/select.ll

Lines changed: 8 additions & 13 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,9 +1476,8 @@ define i8 @select_icmp_eq_shl_and(i8 noundef %a, i8 %b) {
14771476
; CHECK-LABEL: @select_icmp_eq_shl_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: [[SHL:%.*]] = shl i8 [[DIV7]], [[DIV7]]
1482-
; CHECK-NEXT: [[RETVAL_0:%.*]] = select i1 [[CMP]], i8 [[SHL]], i8 [[B:%.*]]
1479+
; CHECK-NEXT: [[TMP2:%.*]] = shl i8 [[A]], [[A]]
1480+
; CHECK-NEXT: [[RETVAL_0:%.*]] = select i1 [[CMP]], i8 [[TMP2]], i8 [[B:%.*]]
14831481
; CHECK-NEXT: ret i8 [[RETVAL_0]]
14841482
;
14851483
%1 = and i8 %a, 1
@@ -1494,8 +1492,7 @@ define i8 @select_icmp_eq_and(i8 noundef %a, i8 %b) {
14941492
; CHECK-LABEL: @select_icmp_eq_and(
14951493
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[A:%.*]], 1
14961494
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[TMP1]], 0
1497-
; CHECK-NEXT: [[DIV7:%.*]] = and i8 [[A]], -2
1498-
; CHECK-NEXT: [[RETVAL_0:%.*]] = select i1 [[CMP]], i8 [[DIV7]], i8 [[B:%.*]]
1495+
; CHECK-NEXT: [[RETVAL_0:%.*]] = select i1 [[CMP]], i8 [[A]], i8 [[B:%.*]]
14991496
; CHECK-NEXT: ret i8 [[RETVAL_0]]
15001497
;
15011498
%1 = and i8 %a, 1
@@ -1510,9 +1507,8 @@ define i8 @select_icmp_eq_mul_and_undef(i8 %a, i8 %b) {
15101507
; CHECK-LABEL: @select_icmp_eq_mul_and_undef(
15111508
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[A:%.*]], 1
15121509
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[TMP1]], 0
1513-
; CHECK-NEXT: [[DIV7:%.*]] = and i8 [[A]], -2
1514-
; CHECK-NEXT: [[MUL:%.*]] = mul i8 [[DIV7]], [[DIV7]]
1515-
; CHECK-NEXT: [[RETVAL_0:%.*]] = select i1 [[CMP]], i8 [[MUL]], i8 [[B:%.*]]
1510+
; CHECK-NEXT: [[TMP2:%.*]] = mul i8 [[A]], [[A]]
1511+
; CHECK-NEXT: [[RETVAL_0:%.*]] = select i1 [[CMP]], i8 [[TMP2]], i8 [[B:%.*]]
15161512
; CHECK-NEXT: ret i8 [[RETVAL_0]]
15171513
;
15181514
%1 = and i8 %a, 1
@@ -1528,8 +1524,7 @@ define i8 @select_icmp_eq_and_undef(i8 %a, i8 %b) {
15281524
; CHECK-LABEL: @select_icmp_eq_and_undef(
15291525
; CHECK-NEXT: [[TMP1:%.*]] = and i8 [[A:%.*]], 1
15301526
; CHECK-NEXT: [[CMP:%.*]] = icmp eq i8 [[TMP1]], 0
1531-
; CHECK-NEXT: [[DIV7:%.*]] = and i8 [[A]], -2
1532-
; CHECK-NEXT: [[RETVAL_0:%.*]] = select i1 [[CMP]], i8 [[DIV7]], i8 [[B:%.*]]
1527+
; CHECK-NEXT: [[RETVAL_0:%.*]] = select i1 [[CMP]], i8 [[A]], i8 [[B:%.*]]
15331528
; CHECK-NEXT: ret i8 [[RETVAL_0]]
15341529
;
15351530
%1 = and i8 %a, 1

0 commit comments

Comments
 (0)