]> granicus.if.org Git - llvm/commit
[Analysis] Don't assume that unsigned overflow can't happen in EmitGEPOffset (PR42699)
authorMikhail Maltsev <mikhail.maltsev@arm.com>
Thu, 17 Oct 2019 08:59:06 +0000 (08:59 +0000)
committerMikhail Maltsev <mikhail.maltsev@arm.com>
Thu, 17 Oct 2019 08:59:06 +0000 (08:59 +0000)
commitfaeea2dc5e4b500b563a86958ded2e6b4b1b8682
treea3c270f74e0b429e51ddac853a15b4e9e2744f9d
parent45311834539ec8d38f9da4794a346b711235d0a2
[Analysis] Don't assume that unsigned overflow can't happen in EmitGEPOffset (PR42699)

Summary:
Currently when computing a GEP offset using the function EmitGEPOffset
for the following instruction

  getelementptr inbounds i32, i32* %p, i64 %offs

we get

  mul nuw i64 %offs, 4

Unfortunately we cannot assume that unsigned wrapping won't happen
here because %offs is allowed to be negative.

Making such assumptions can lead to miscompilations: see the new test
test24_neg_offs in InstCombine/icmp.ll. Without the patch InstCombine
would generate the following comparison:

   icmp eq i64 %offs, 4611686018427387902; 0x3ffffffffffffffe

Whereas the correct value to compare with is -2.

This patch replaces the NUW flag with NSW in the multiplication
instructions generated by EmitGEPOffset and adjusts the test suite.

https://bugs.llvm.org/show_bug.cgi?id=42699

Reviewers: chandlerc, craig.topper, ostannard, lebedev.ri, spatel, efriedma, nlopes, aqjune

Reviewed By: lebedev.ri

Subscribers: reames, lebedev.ri, hiraditya, llvm-commits

Tags: #llvm

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

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@375089 91177308-0d34-0410-b5e6-96231b3b80d8
include/llvm/Analysis/Utils/Local.h
test/Transforms/InstCombine/gep-custom-dl.ll
test/Transforms/InstCombine/getelementptr.ll
test/Transforms/InstCombine/icmp-custom-dl.ll
test/Transforms/InstCombine/icmp.ll
test/Transforms/InstCombine/sub.ll