This is an archive of the discontinued LLVM Phabricator instance.

[ConstantFold] Fix incorrect index rewrites for GEPs
ClosedPublic

Authored by anemet on May 25 2016, 10:37 PM.

Details

Summary

If an index for a vector or array type is out-of-range GEP constant
folding tries to factor it into preceding dimensions. The code however
does not consider addressing of structure field padding which should not
qualify as out-of-range index.

As demonstrated by the testcase, this can occur if the indexing
performed on a vector type and the preceding index is an array type.

SROA generates GEPs for example involving padding bytes as it slices an
alloca.

My fix disables this folding if the element type is a vector type. I
believe that this is the only way we can end up with padding. (We have
no access to DataLayout so I am not sure if there is actual robust way
of actually checking the presence of padding.)

Diff Detail

Event Timeline

anemet updated this revision to Diff 58568.May 25 2016, 10:37 PM
anemet retitled this revision from to [ConstantFold] Fix incorrect index rewrites for GEPs.
anemet updated this object.
anemet added a reviewer: majnemer.
anemet added subscribers: Gerolf, llvm-commits.
majnemer accepted this revision.May 25 2016, 11:18 PM
majnemer edited edge metadata.

LGTM

lib/IR/ConstantFold.cpp
2196–2197

These conditions can now be merged.

2198

cast<SequentialType> can now be cast<ArrayType>

This revision is now accepted and ready to land.May 25 2016, 11:18 PM
anemet added inline comments.May 26 2016, 12:13 AM
lib/IR/ConstantFold.cpp
2196–2197

Thanks! I'll do this in a separate commit because clang-format creates a big churn.

This revision was automatically updated to reflect the committed changes.