Teach the constant folder to look through bitcast constant expressions
authorChandler Carruth <chandlerc@gmail.com>
Thu, 15 May 2014 09:56:28 +0000 (09:56 +0000)
committerChandler Carruth <chandlerc@gmail.com>
Thu, 15 May 2014 09:56:28 +0000 (09:56 +0000)
commitca323cf916fc7e2bb227e253c15d456f3f3ca3eb
treec639ccfa9b37c30ad695c0d0181070637eaf2218
parent9cfa5cf7ae1c61a8a1a26d56650ae23746180452
Teach the constant folder to look through bitcast constant expressions
much more effectively when trying to constant fold a load of a constant.
Previously, we only handled bitcasts by trying to find a totally generic
byte representation of the constant and use that. Now, we look through
the bitcast to see what constant we might fold the load into, and then
try to form a constant expression cast of the found value that would be
equivalent to loading the value.

You might wonder why on earth this actually matters. Well, turns out
that the Itanium ABI causes us to create a single array for a vtable
where the first elements are virtual base offsets, followed by the
virtual function pointers. Because the array is homogenous the element
type is consistently i8* and we inttoptr the virtual base offsets into
the initial elements.

Then constructors bitcast these pointers to i64 pointers prior to
loading them. Boom, no more constant folding of virtual base offsets.
This is the first fix to LLVM to address the *insane* performance Eric
Niebler discovered with Clang on his range comprehensions[1]. There is
more to come though, this doesn't *really* fix the problem fully.

[1]: http://ericniebler.com/2014/04/27/range-comprehensions/

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@208856 91177308-0d34-0410-b5e6-96231b3b80d8
lib/Analysis/ConstantFolding.cpp
test/Transforms/ConstProp/loads.ll