[InstCombine] eliminate sext and/or trunc if value has enough signbits
authorSanjay Patel <spatel@rotateright.com>
Thu, 3 Jun 2021 17:15:23 +0000 (13:15 -0400)
committerSanjay Patel <spatel@rotateright.com>
Thu, 3 Jun 2021 17:58:19 +0000 (13:58 -0400)
commitb865eead76577b031c6fae5e2490e7be0073f201
tree23e058752ec4247671ce94af673e2b6185dd1a37
parent3e55f55048387a8efef01e445d46fc5cf100285d
[InstCombine] eliminate sext and/or trunc if value has enough signbits

If we have enough signbits in a source value, we can skip an
intermediate cast for a trunc+sext pair:
https://alive2.llvm.org/ce/z/A_mQt-

This is the original problem shown in:
https://llvm.org/PR49543

There's a test that shows we transformed what used to be
a pair of shifts, so that suggests we could add another
ComputeNumSignBits fold starting from a shift.

There does not appear to be any change in compile-time
from the extra analysis:
https://llvm-compile-time-tracker.com/compare.php?from=3d2c9069dcafd0cbb641841aa3dd6e851fb7d760&to=b9513cdf2419704c7bb0c3a02a9ca06aae13d902&stat=instructions

Differential Revision: https://reviews.llvm.org/D103617
llvm/lib/Transforms/InstCombine/InstCombineCasts.cpp
llvm/test/Transforms/InstCombine/sext-of-trunc-nsw.ll