[InstCombine] Teach the move free before null test opti how to deal with noop casts
authorQuentin Colombet <quentin.colombet@gmail.com>
Tue, 30 Oct 2018 20:51:04 +0000 (20:51 +0000)
committerQuentin Colombet <quentin.colombet@gmail.com>
Tue, 30 Oct 2018 20:51:04 +0000 (20:51 +0000)
commit900678227c3cc42bc01c82eb4f814524e6b79bd0
tree9020f3e05bfc0ad6d57d685078c6855a762c5ecf
parent93a64efcd3358e85ca24da11d6fa765d01dd6e45
[InstCombine] Teach the move free before null test opti how to deal with noop casts

InstCombine features an optimization that essentially replaces:
if (a)
  free(a)
into:
free(a)

Right now, this optimization is gated by the minsize attribute and therefore
we only perform it if we can prove that we are going to be able to eliminate
the branch and the destination block.

However when casts are involved the optimization would fail to apply, because
the optimization was not smart enough to realize that it is possible to also
move the casts away from the destination block and that is harmless to the
performance since they are just noops.
E.g.,
foo(int *a)
if (a)
  free((char*)a)

Wouldn't be optimized by instcombine, because
- We would refuse to hoist the `bitcast i32* %a to i8` in the source block
- We would fail to see that `bitcast i32* %a to i8` and %a are the same value.

This patch fixes both these problems:
- It teaches the pattern matching of the comparison how to look
  through casts.
- It checks that whether the additional instruction in the destination block
  can be hoisted and are harmless performance-wise.
- It hoists all the code of the destination block in the source block.

Differential Revision: D53356

llvm-svn: 345644
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/malloc-free-delete.ll