Skip to content

Commit 7088b46

Browse files
committed
[InstCombine] Fold ceil(X >> C) == 0 -> X == 0
1 parent 8d56b3a commit 7088b46

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "llvm/Analysis/InstructionSimplify.h"
2121
#include "llvm/Analysis/Utils/Local.h"
2222
#include "llvm/Analysis/VectorUtils.h"
23+
#include "llvm/IR/CmpPredicate.h"
2324
#include "llvm/IR/ConstantRange.h"
2425
#include "llvm/IR/DataLayout.h"
2526
#include "llvm/IR/InstrTypes.h"
@@ -1298,6 +1299,23 @@ Instruction *InstCombinerImpl::foldICmpWithZero(ICmpInst &Cmp) {
12981299
// eq/ne (mul X, Y)) with (icmp eq/ne X/Y) and if X/Y is known non-zero that
12991300
// will fold to a constant elsewhere.
13001301
}
1302+
1303+
// (X >> C) + ((X & ((1 << C) - 1)) != 0) == 0 -> X == 0
1304+
if (Pred == ICmpInst::ICMP_EQ) {
1305+
Value *X;
1306+
const APInt *C1, *C2;
1307+
CmpPredicate PredNE;
1308+
if (match(Cmp.getOperand(0),
1309+
m_OneUse(
1310+
m_Add(m_LShr(m_Value(X), m_APInt(C1)),
1311+
m_ZExt(m_ICmp(PredNE, m_And(m_Deferred(X), m_APInt(C2)),
1312+
m_Zero()))))) &&
1313+
PredNE == CmpInst::ICMP_NE &&
1314+
*C2 == APInt::getLowBitsSet(C2->getBitWidth(), C1->getZExtValue()))
1315+
return new ICmpInst(ICmpInst::ICMP_EQ, X,
1316+
ConstantInt::getNullValue(X->getType()));
1317+
}
1318+
13011319
return nullptr;
13021320
}
13031321

0 commit comments

Comments
 (0)