This is an archive of the discontinued LLVM Phabricator instance.

Constfold insertelement to undef when index is out-of-bounds
ClosedPublic

Authored by chfast on Apr 27 2015, 12:07 AM.

Details

Summary

This patch adds constant folding of insertelement instruction to undef value when index operand is constant and is not less than vector size or is undef.

InstCombine does not support this case, but I'm happy to add it there also if this change is accepted.

Diff Detail

Event Timeline

chfast updated this revision to Diff 24455.Apr 27 2015, 12:07 AM
chfast retitled this revision from to Constfold insertelement to undef when index is out-of-bounds.
chfast updated this object.
chfast edited the test plan for this revision. (Show Details)
chfast added a reviewer: majnemer.
chfast set the repository for this revision to rL LLVM.
chfast added a subscriber: Unknown Object (MLST).
majnemer added inline comments.Apr 27 2015, 1:08 AM
lib/IR/ConstantFold.cpp
828

It might be nicer to do something like:

uint64_t Idx = CIdx->getZExtValue();

After the above comparison, we know that getZExtValue will succeed. This lets us simplify the loop below a little (i.e. Idx == i vs CIdx->equalsInt(i))

829

No need to initialize the SmallVector. Please use Result.reserve(NumElts); instead.

832–833

This will need to switch to using push_back once you use reserve.

chfast updated this revision to Diff 24458.Apr 27 2015, 2:16 AM
chfast removed rL LLVM as the repository for this revision.

Switched back to push_back driven implementation

majnemer accepted this revision.Apr 27 2015, 2:23 AM
majnemer edited edge metadata.

LGTM

This revision is now accepted and ready to land.Apr 27 2015, 2:23 AM

Thanks for the review.

What do you think about adding "the same" to InstCombine?

InstCombine would work but there is a better place: InstSimplify. It is
preferable to stick things in InstSimplify when a new instruction doesn't
need to be created.

chfast closed this revision.Apr 27 2015, 2:34 AM