[PATCH] D134743: [SimplifyCFG] Allow SimplifyCFG sinking to merge tail call kinds

Eduard Zingerman via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 27 08:21:47 PDT 2022


eddyz87 created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
eddyz87 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This commit modifies `SimplifyCFGPass` to allow sinking of calls that
differ in tail call kind. Specifically if tail call kinds for sink
candidates are `TCK_Tail` and `TCK_None` it is safe to sink the call
and assign `TCK_None` as a kind for the merged instruction.

Could be illustrated by the following example:

  c
  extern void escape(void*);
  extern int magic(void);
  extern void tailfn(void);
  
  void foofn() {
    int x;
    if (magic()) {
      tailfn();
    } else {
      escape(&x);
      tailfn();
    }
  }

Could be rewritten as:

  c
  void foofn() {
    int x;
    if (!magic())
      escape(&x);
    tailfn();
  }

W/o this commit such transformation does not happen, because
`TailCallElimPass` computes different tail call kinds for two `tailfn`
calls:

  entry:
    %x = alloca i32, align 4
    %call = tail call i32 @magic()
    %tobool.not = icmp eq i32 %call, 0
    br i1 %tobool.not, label %if.else, label %if.then
  
  if.then:
    tail call void @tailfn()
    br label %if.end
  
  if.else:
    call void @escape(ptr noundef nonnull %x)
    call void @tailfn()
    br label %if.end

When `SimplifyCFGPass` considers what instructions to sink to a common
successor it uses the function `Instruction::isSameOperationAs` to
identify sink candidates. However this function returns false if call
instructions have different values of tail call kind flag.

This commit extends `Instruction::isSameOperationAs` with a flag to
ignore tail call kind and extends `SimplifyCFGPass` with logic to
compute a common tail call kind for a merged instruction.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134743

Files:
  llvm/include/llvm/IR/Instruction.h
  llvm/lib/IR/Instruction.cpp
  llvm/lib/Transforms/Utils/SimplifyCFG.cpp
  llvm/test/Transforms/SimplifyCFG/sink-tail-call-kind.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134743.463241.patch
Type: text/x-patch
Size: 10541 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220927/179a26f9/attachment.bin>


More information about the llvm-commits mailing list