[PATCH] D109917: [InstCombine] Improve TryToSink for side-effecting calls that would be trivially dead

Philip Reames via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 1 13:56:48 PST 2021


reames added a comment.

Thinking about it a bit, I think there are some interesting cases where we can sink something with an analyzeable side effect.  An example I came up with is a large function with out-params which are unused in the caller.

Here's an attempt at a C example:

  int foo();
  
  extern int test(int *out) __attribute__((noinline));
  int test(int *out) {
    *out = foo();
    return foo();
  }
  
  int wrapper() {
  
    int notdead;
    if (test(&notdead))
      return 0;
  
    int dead;
    int tmp = test(&dead);
    if (notdead)
      return tmp;
    return foo();
  }

Key points in the example:

- foo is used to represent a large *no-throw* block of code with hard to IPO results.
- We have a parameter which is only written.  (Today, we fail to infer this fact from IR oddly.  We should fix that.)
- We have an unused temporary created to pass to said out-param.
- We have two or more callers, some of which *do* use the out-param.

In the IR, we'd look for something like:

- An argmemonly nothrow willreturn function, with one or more write only params.
- A callsite to said function where the memory passed is an alloca used only by lifetime markers and the call.

Such a call site can be sunk to the dataflow uses without changing the visibility of the side effect.

To be careful, this comment is meant to motivate the basic idea, not any particular implementation detail of this patch.


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

https://reviews.llvm.org/D109917



More information about the llvm-commits mailing list