[CodeGenPrepare] Delete intrinsic call to llvm.assume to enable more tailcall
authorGuozhi Wei <carrot@google.com>
Tue, 31 Mar 2020 18:55:51 +0000 (11:55 -0700)
committerGuozhi Wei <carrot@google.com>
Tue, 31 Mar 2020 18:55:51 +0000 (11:55 -0700)
commit6d20937c29a1a1d67fc5e8caf8538b4aa5614505
tree91a3c21de2adb099cded1f5560c88a7e795e3fc2
parent08682dcc8631bcbfd68834a7dc352499a0a06af0
[CodeGenPrepare] Delete intrinsic call to llvm.assume to enable more tailcall

The attached test case is simplified from tcmalloc. Both function calls should be optimized as tailcall. But llvm can only optimize the first call. The second call can't be optimized because function dupRetToEnableTailCallOpts failed to duplicate ret into block case2.

There 2 problems blocked the duplication:

  1 Intrinsic call llvm.assume is not handled by dupRetToEnableTailCallOpts.
  2 The control flow is more complex than expected, dupRetToEnableTailCallOpts can only duplicate ret into its predecessor, but here we have an intermediate block between call and ret.

The solutions:

  1 Since CodeGenPrepare is already at the end of LLVM IR phase, we can simply delete the intrinsic call to llvm.assume.
  2 A general solution to the complex control flow is hard, but for this case, after exit2 is duplicated into case1, exit2 is the only successor of exit1 and exit1 is the only predecessor of exit2, so they can be combined through eliminateFallThrough. But this function is called too late, there is no more dupRetToEnableTailCallOpts after it. We can add an earlier call to eliminateFallThrough to solve it.

Differential Revision: https://reviews.llvm.org/D76539
llvm/lib/CodeGen/CodeGenPrepare.cpp
llvm/test/Transforms/CodeGenPrepare/X86/extend-sink-hoist.ll
llvm/test/Transforms/CodeGenPrepare/X86/optimizeSelect-DT.ll
llvm/test/Transforms/CodeGenPrepare/X86/tailcall-assume-xbb.ll [new file with mode: 0644]