[PATCH] D79258: [AutoFDO] Handling must tail calls in indirect call promotion

Hongtao Yu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat May 2 21:43:46 PDT 2020


hoyFB added a comment.

In D79258#2016598 <https://reviews.llvm.org/D79258#2016598>, @davidxl wrote:

> Can the code be refactored such that the splitting and cloning part of the code are mostly shared?


Yes, I tried that as my first attempt. The change looks like blow and placed closed to the end of the function. Much of the diversion is from cloning the return statement and the bitcast, and also the removal of the merge block. Either way works but I like the current version a bit more. What do you think?

  if (OrigInst->isMustTailCall()) {
      auto Next = &(*MergeBlock->begin());
      Value *NewRetVal = NewInst;
      // Move the optional bitcast.
      if (auto *BitCast = dyn_cast_or_null<BitCastInst>(Next)) {
        assert(BitCast->getOperand(0) == OrigInst &&
               "bitcast following musttail call must use the call");
        Next = BitCast->getNextNode();
        auto NewBitCast = BitCast->clone();
        NewBitCast->replaceUsesOfWith(OrigInst, NewInst);
        BitCast->moveBefore(ElseTerm);
        NewBitCast->insertBefore(ThenTerm);
        NewRetVal = NewBitCast;
      }
      // Move return following a musttail call as well.
      ReturnInst *Ret = dyn_cast_or_null<ReturnInst>(Next);
      assert(Ret && "musttail call must precede a ret with an optional bitcast");
      if (!Ret->getReturnValue()) {
        NewRetVal = nullptr;
      }
      auto NewRet = Ret->clone();
      if (Ret->getReturnValue())
        NewRet->replaceUsesOfWith(Ret->getReturnValue(), NewRetVal);
      Ret->moveBefore(ElseTerm);
      NewRet->insertBefore(ThenTerm);
      // Invoke instructions are terminating, so we don't need the terminator
      // instructions that were just created.
      ThenTerm->eraseFromParent();
      ElseTerm->eraseFromParent();
      if (MergeBlock->empty())
        MergeBlock->eraseFromParent();
      return *NewInst;
    }




Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D79258





More information about the llvm-commits mailing list