This is an archive of the discontinued LLVM Phabricator instance.

[ConstProp] Handle insertelement constants
ClosedPublic

Authored by aeubanks on Aug 12 2020, 4:39 PM.

Details

Summary

Previously ConstantFoldExtractElementInstruction() would only work with
insertelement instructions, not contants. This properly handles
insertelement constants as well.

Diff Detail

Event Timeline

aeubanks created this revision.Aug 12 2020, 4:39 PM
Herald added a project: Restricted Project. · View Herald TranscriptAug 12 2020, 4:39 PM
aeubanks requested review of this revision.Aug 12 2020, 4:39 PM

I think I'd prefer to keep this code in ConstantFoldExtractElementInstruction, rather than extend getAggregateElement() this way; I think it's unintuitive to be doing constant folding in getAggregateElement().

llvm/lib/IR/Constants.cpp
421 ↗(On Diff #285212)

Can we also fold in the case where the indexes are known not-equal?

aeubanks updated this revision to Diff 285232.Aug 12 2020, 6:16 PM

Move code to ConstantFoldExtractElementInstruction()

I think I'd prefer to keep this code in ConstantFoldExtractElementInstruction, rather than extend getAggregateElement() this way; I think it's unintuitive to be doing constant folding in getAggregateElement().

Done.

llvm/lib/IR/Constants.cpp
421 ↗(On Diff #285212)

Do you mean recursively search the vector operand?

efriedma added inline comments.Aug 13 2020, 12:04 PM
llvm/lib/IR/ConstantFold.cpp
859

CE->getOperand(1) should return a Constant*; you don't need to dyn_cast<> it.

llvm/lib/IR/Constants.cpp
421 ↗(On Diff #285212)

Sort of. I was thinking of something like return ConstantExpr::getExtractElement(IE->getOperand(0), CIdx);.

efriedma added inline comments.Aug 13 2020, 12:11 PM
llvm/lib/IR/ConstantFold.cpp
858

Oh, also, please avoid using getZExtValue() on constants that don't have a known width; it asserts the number is less than 2^64. Unfortunately, there isn't any convenient way to do this comparison at the moment; I guess you could use APSInt::isSameValue.

aeubanks updated this revision to Diff 285494.Aug 13 2020, 2:09 PM
aeubanks marked an inline comment as done.

Recurse when index is not equal
Don't cast getOperand() to Constant*
Use APInt::eq()

aeubanks marked an inline comment as done.Aug 13 2020, 2:09 PM
aeubanks added inline comments.
llvm/lib/IR/ConstantFold.cpp
858

Is APInt::eq() good?

efriedma added inline comments.Aug 13 2020, 2:17 PM
llvm/lib/IR/ConstantFold.cpp
858

IDIdx and CIdx might not have the same bitwidth, I think; the index of an insertelement/extractelement can have any type. APInt::eq asserts if the widths aren't equal.

aeubanks updated this revision to Diff 285501.Aug 13 2020, 2:45 PM

Use APSInt::isSameValue()

llvm/lib/IR/ConstantFold.cpp
858

Ah yes, I changed one of the types in the new test and repro'd. Now using APSInt::isSameValue().

This revision is now accepted and ready to land.Aug 13 2020, 3:47 PM
This revision was automatically updated to reflect the committed changes.