[llvm] [InstCombine] Fold selection between less than zero and one (PR #69961)

via llvm-commits llvm-commits at lists.llvm.org
Tue Nov 14 07:01:05 PST 2023


elhewaty wrote:

What's wrong with this?
```
CmpInst::Predicate Pred;
  Value *A;
  if (match(CondVal,
            m_ICmp(Pred, m_Value(A),
                   m_SpecificInt(A->getType()->getScalarSizeInBits() - 1))) &&
      match(TrueVal,
            m_LShr(m_Specific(A),
                   m_SpecificInt(A->getType()->getScalarSizeInBits() - 1))) &&
      match(FalseVal, m_One())) {
    Constant *One = ConstantInt::get(SelType, 1);
    auto *SLTZero
      = Builder.CreateICmpSLT(A, ConstantInt::getNullValue(A->getType()));
    auto *Sel = Builder.CreateSelect(CondVal, SLTZero, One);
    Type *Ty = A->getType();
    auto *Zext = Builder.CreateZExt(Sel, Ty);
    return replaceInstUsesWith(SI, Zext);
  }
```

https://github.com/llvm/llvm-project/pull/69961


More information about the llvm-commits mailing list