[ConstantFolding] Handle leading zero-size elements in load folding
authorNikita Popov <nikita.ppv@gmail.com>
Tue, 11 Dec 2018 20:29:16 +0000 (20:29 +0000)
committerNikita Popov <nikita.ppv@gmail.com>
Tue, 11 Dec 2018 20:29:16 +0000 (20:29 +0000)
commit79c994d9767212c97f87846c7cd76e678f889052
treec02cc77be5e3f6c3efeeb152a18253e426cd2666
parentfb3e58920dcc84edb169d122dd064cfcf1eb1349
[ConstantFolding] Handle leading zero-size elements in load folding

Struct types may have leading zero-size elements like [0 x i32], in
which case the "real" element at offset 0 will not necessarily coincide
with the 0th element of the aggregate. ConstantFoldLoadThroughBitcast()
wants to drill down the element at offset 0, but currently always picks
the 0th aggregate element to do so. This patch changes the code to find
the first non-zero-size element instead, for the struct case.

The motivation behind this change is https://github.com/rust-lang/rust/issues/48627.
Rust is fond of emitting [0 x iN] separators between struct elements to
enforce alignment, which prevents constant folding in this particular case.

The additional tests with [4294967295 x [0 x i32]] check that we don't
end up unnecessarily looping over a large number of zero-size elements
of a zero-size array.

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

llvm-svn: 348895
llvm/lib/Analysis/ConstantFolding.cpp
llvm/test/Transforms/ConstProp/loads.ll