[WebAssembly] Fix a bug in removing unnecessary branches
authorHeejin Ahn <aheejin@gmail.com>
Mon, 25 May 2020 08:34:43 +0000 (01:34 -0700)
committerHeejin Ahn <aheejin@gmail.com>
Fri, 29 May 2020 02:44:27 +0000 (19:44 -0700)
commit3fe6ea4641b20c3406e2ef10c0f3782788585030
tree5569f942074ad26264278fbcad84ccedad4d7a6a
parent59ba12994c07d03ac3b628c05c45a834774f9b17
[WebAssembly] Fix a bug in removing unnecessary branches

Summary:
One of the things `removeUnnecessaryInstrs()` in CFGStackify does is to
remove an unnecessary unconditinal branch before an EH pad. When there
is an unconditional branch right before a catch instruction and it
branches to the end of `end_try` marker, we don't need the branch,
because it there is no exception, the control flow transfers to
that point anyway.
```
bb0:
  try
    ...
    br bb2      <- Not necessary
bb1:
  catch
    ...
bb2:
  end
```

This applies when we have a conditional branch followed by an
unconditional one, in which case we should only remove the unconditional
branch. For example:
```
bb0:
  try
    ...
    br_if someplace_else
    br bb2                 <- Not necessary
bb1:
  catch
    ...
bb2:
  end
```

But `TargetInstrInfo::removeBranch` we used removed all existing
branches when there are multiple ones. This patch fixes it by only
deleting the last (= unconditional) branch manually.

Also fixes some `preds` comments in the test file.

Reviewers: dschuff

Subscribers: sbc100, jgravelle-google, hiraditya, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D80572
llvm/lib/Target/WebAssembly/WebAssemblyCFGStackify.cpp
llvm/test/CodeGen/WebAssembly/cfg-stackify-eh.ll