[PATCH] D84916: [SimpleLoopUnswitch] Drop make.implicit metadata in case of non-trivial unswitching

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 30 01:36:55 PDT 2020


mkazantsev created this revision.
mkazantsev added reviewers: asbirlea, aqjune, skatkov, fhahn, DaniilSuchkov.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.
mkazantsev requested review of this revision.

Non-trivial unswitching simply moves terminator being unswitch from the loop
up to the switch block. It also preserves all metadata that was there. It might not
be a correct thing to do for `make.implicit` metadata. Consider case:

  for (...) {
    cond = // computed in loop
    if (cond) return X;
    if (p == null) throw_npe(); !make implicit
  }

Before the unswitching, if `p` is null and we reach this check, we are guaranteed
to go to `throw_npe()` block. Now we unswitch on `p == null` condition:

  if (p == null) !make implicit {
    for (...) {
      if (cond) return X;
      throw_npe()
    }
  } else {
    for (...) {
      if (cond) return X;
    }
  }

Now, following `true` branch of `p == null` does not always lead us to
`throw_npe()` because the loop has side exit. Now, if we run ImplicitNullCheck
pass on this code, it may end up making the unswitch condition implicit. This may
lead us to turning normal path to `return X` into signal-throwing path, which is
not efficient.

Note that this does not happen during trivial unswitch: it guarantees that we do not
have side exits before condition being unswitched.

This patch fixes this situation by unconditional dropping of `make.implicit` metadata
when we perform non-trivial unswitch. We could preserve it if we could prove that the
condition always executes. This can be done as a follow-up.


https://reviews.llvm.org/D84916

Files:
  llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp
  llvm/test/Transforms/SimpleLoopUnswitch/implicit-null-checks.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D84916.281822.patch
Type: text/x-patch
Size: 9316 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200730/442d57a5/attachment.bin>


More information about the llvm-commits mailing list