[PATCH] D34822: [LVI] Constant-propagate a zero extension of the switch condition value through case edges

Hiroshi Yamauchi via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 20 10:40:42 PDT 2017


yamauchi added a comment.

I think this situation occurs due to the combination of the following two optimizations that shrink/narrow the width of operands.

They succeed narrowing the width of the switch condition operand "op" (%op.0.in in the test) and its address (%p.addr.0.pn in the test) from i32 to i8.

But since "op" still has a use as an i32 (passed to bar()), the zext remains.

Because of the zext, the current case edge value propagation doesn't perform the value propagation (op==93) on the case edge and the elimination of the call to f.

1. lib/Transforms/InstCombine/InstructionCombining.cpp

  Instruction *InstCombiner::visitSwitchInst(SwitchInst &SI) {
    ...
    // Shrink the condition operand if the new type is smaller than the old type.
    // This may produce a non-standard type for the switch, but that's ok because
    // the backend should extend back to a legal type for the target.
    if (NewWidth > 0 && NewWidth < Known.getBitWidth()) {
      IntegerType *Ty = IntegerType::get(SI.getContext(), NewWidth);
      Builder.SetInsertPoint(&SI);
      Value *NewCond = Builder.CreateTrunc(Cond, Ty, "trunc");
      SI.setCondition(NewCond);
  
      for (auto Case : SI.cases()) {
        APInt TruncatedCase = Case.getCaseValue()->getValue().trunc(NewWidth);
        Case.setValue(ConstantInt::get(SI.getContext(), TruncatedCase));
      }
      return &SI;
    }
  
    return nullptr;
  }



2. lib/Transforms/InstCombine/InstCombinePHI.cpp

  Instruction *InstCombiner::FoldPHIArgZextsIntoPHI(PHINode &Phi) {
    ...
    // All incoming values are zexts or constants that are safe to truncate.
    // Create a new phi node of the narrow type, phi together all of the new
    // operands, and zext the result back to the original type.
    PHINode *NewPhi = PHINode::Create(NarrowType, NumIncomingValues,
                                      Phi.getName() + ".shrunk");
    for (unsigned i = 0; i != NumIncomingValues; ++i)
      NewPhi->addIncoming(NewIncoming[i], Phi.getIncomingBlock(i));
  
    InsertNewInstBefore(NewPhi, Phi);
    return CastInst::CreateZExtOrBitCast(NewPhi, Phi.getType());
  }


https://reviews.llvm.org/D34822





More information about the llvm-commits mailing list