[PATCH] D109807: [InstCombine] Narrow type of logical operation chains in certain cases

Usman Nadeem via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 20 19:25:11 PDT 2021


mnadeem added a comment.

In D109807#3008300 <https://reviews.llvm.org/D109807#3008300>, @spatel wrote:

> I think I looked at this as a problem for -reassociate a long time ago. It makes changes on larger patterns like:
> https://alive2.llvm.org/ce/z/UD56tJ
> ...but misses the shorter sequences.
>
> (and this patch would not help match that example in -instcombine alone IIUC)
>
> I think this patch also misses cases where we have a logic op with a wide constant:
> https://alive2.llvm.org/ce/z/Ls8hXZ
>
> Do you plan to extend it to deal with those patterns too?

I shouldn't have given this revision a general title, I was only targeting a specific case i.e. `extend(X) | (extend(Y) | Z)`

But your first case can probably be handled in instcombine like this (I havent tested this though):

  // %conv1 = sext i4 %c to i6
  // %conv2 = sext i4 %d to i6
  // %or1 = or i6 %conv1, %a
  // %or2 = or i6 %conv2, %b
  // %or3 = or i6 %or1, %or2
  // to
  // %conv1 = sext i4 %c to i6
  // %conv2 = sext i4 %d to i6
  // %or1 = or i6 %conv1, %conv2 --> will later be converted to extend (or i4 ...)
  // %or2 = or i6 %a, %b
  // %or3 = or i6 %or1, %or2
  Instruction *InstCombinerImpl::reassosiateNestedExtendedBitwiseLogic(BinaryOperator &I) {
    auto LogicOpc = I.getOpcode();
    assert(I.isBitwiseLogicOp() && "Unexpected opcode for bitwise logic instcombine");
  
    auto *Op0 = dyn_cast<BinaryOperator>(I.getOperand(0));
    auto *Op1 = dyn_cast<BinaryOperator>(I.getOperand(1));
    if (!Op0 || !Op1)
      return nullptr;
    if (Op0->getOpcode() != Op1->getOpcode() || Op0->getOpcode() != LogicOpc)
      return nullptr;
    if (match(Inst, 
      m_Or(
        m_OneUse(m_c_BinOp(m_CombineAnd(m_ZExtOrSExt(m_Value()), m_Value(A)), m_Value(B))), 
        m_OneUse(m_c_BinOp(m_CombineAnd(m_ZExtOrSExt(m_Value()), m_Value(C)), m_Value(D)))))) {
      Value *OpWithExtends = Builder.CreateBinOp(LogicOpc, A, Z);
      Value *OpWithOutExtends = Builder.CreateBinOp(LogicOpc, B, D);
      return BinaryOperator::Create(LogicOpc, OpWithExtends , OpWithOutExtends);
    }
    return nullptr;
  }


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D109807/new/

https://reviews.llvm.org/D109807



More information about the llvm-commits mailing list