[llvm] [InstCombine] Missed optimization: Fold (sext(a) & c1) == c2 to (a & c3) == trunc(c2) (PR #112646)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 26 07:11:46 PDT 2024
================
@@ -7687,6 +7689,34 @@ Instruction *InstCombinerImpl::visitICmpInst(ICmpInst &I) {
if (Instruction *Res = foldReductionIdiom(I, Builder, DL))
return Res;
+ {
+ Value *A;
+ const APInt *C1, *C2;
+ ICmpInst::Predicate Pred = I.getPredicate();
+ if (Pred == ICmpInst::ICMP_EQ || Pred == ICmpInst::ICMP_NE) {
+ // sext(a) & c1 == c2 --> a & c3 == trunc(c2)
+ // sext(a) & c1 != c2 --> a & c3 != trunc(c2)
+ if (match(Op0, m_And(m_SExtLike(m_Value(A)), m_APInt(C1))) &&
+ match(Op1, m_APInt(C2))) {
+ Type *InputTy = A->getType();
+ unsigned InputBitWidth = InputTy->getScalarSizeInBits();
+ // c2 must be non-negative at the bitwidth of a.
+ if (C2->ult(APInt::getOneBitSet(C2->getBitWidth(), InputBitWidth))) {
+ // Check if there are 1s in C1 high bits of size InputBitWidth.
+ bool HasLeading1s =
----------------
goldsteinn wrote:
There doesn't seem to be any reason to store this in a variable. Just put the expression in the condition.
https://github.com/llvm/llvm-project/pull/112646
More information about the llvm-commits
mailing list