[InstCombine] Fix negative GEP offset evaluation for 32-bit pointers
authorNikita Popov <nikita.ppv@gmail.com>
Wed, 12 Dec 2018 23:19:03 +0000 (23:19 +0000)
committerNikita Popov <nikita.ppv@gmail.com>
Wed, 12 Dec 2018 23:19:03 +0000 (23:19 +0000)
commit36e03ac6ee91366225f446db6437f184de258be6
treea11100974c6cfbedcc61bef71cfccc3bffa9ab1c
parent102008589836db7d5f95fe5efb5dae31754cbb8c
[InstCombine] Fix negative GEP offset evaluation for 32-bit pointers

This fixes https://bugs.llvm.org/show_bug.cgi?id=39908.

The evaluateGEPOffsetExpression() function simplifies GEP offsets for
use in comparisons against zero, basically by converting X*Scale+Offset==0
to X+Offset/Scale==0 if Scale divides Offset. However, before this is done,
Offset is masked down to the pointer size. This results in incorrect
results for negative Offsets, because we basically end up dividing the
32-bit offset *zero* extended to 64-bit bits (rather than sign extended).

Fix this by explicitly sign extending the truncated value.

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

llvm-svn: 348987
llvm/lib/Transforms/InstCombine/InstCombineCompares.cpp
llvm/test/Transforms/InstCombine/pr39908.ll [new file with mode: 0644]