]> granicus.if.org Git - llvm/commit
[Legalizer] Fix fp-to-uint to fp-tosint promotion assertion.
authorTim Shen <timshen91@gmail.com>
Wed, 4 Jan 2017 22:11:42 +0000 (22:11 +0000)
committerTim Shen <timshen91@gmail.com>
Wed, 4 Jan 2017 22:11:42 +0000 (22:11 +0000)
commit9f1fb82287ea7dbdf15c1b337f9941d1080263a0
tree8ec185fda9d49a2872cb79d1c2e221471cc1a106
parent4e9e4f277ad254e02a0cff33c61cd827e600da62
[Legalizer] Fix fp-to-uint to fp-tosint promotion assertion.

Summary:
When promoting fp-to-uint16 to fp-to-sint32, the result is actually zero
extended. For example, given double 65534.0, without legalization:

  fp-to-uint16: 65534.0 -> 0xfffe

With the legalization:

  fp-to-sint32: 65534.0 -> 0x0000fffe

Without this patch, legalization wrongly emits a signed extend assertion,
which is consumed by later icmp instruction, and cause miscompile.

Note that the floating point value must be in [0, 65535), otherwise the
behavior is undefined.

This patch reverts r279223 behavior and adds more tests and
documentations.

In PR29041's context, James Molloy mentioned that:

  We don't need to mask because conversion from float->uint8_t is
  undefined if the integer part of the float value is not representable in
  uint8_t. Therefore we can assume this doesn't happen!

which is totally true and good, because fptoui is documented clearly to
have undefined behavior when overflow/underflow happens. We should take
the advantage of this behavior so that we can save unnecessary mask
instructions.

Reviewers: jmolloy, nadav, echristo, kbarton

Subscribers: mehdi_amini, nemanjai, llvm-commits

Differential Revision: https://reviews.llvm.org/D28284

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@291015 91177308-0d34-0410-b5e6-96231b3b80d8
lib/CodeGen/SelectionDAG/LegalizeIntegerTypes.cpp
test/CodeGen/AArch64/fptouint-i8-zext.ll
test/CodeGen/PowerPC/fp64-to-int16.ll [new file with mode: 0644]