This is an archive of the discontinued LLVM Phabricator instance.

[DAGCombiner] Fix & simplify constant folding of sext/zext.
ClosedPublic

Authored by chfast on Jun 22 2015, 10:22 AM.

Details

Summary

This patch fixes the cases of sext/zext constant folding in DAG combiner where constans do not fit 64 bits. The fix simply removes un$

Diff Detail

Repository
rL LLVM

Event Timeline

chfast updated this revision to Diff 28126.Jun 22 2015, 10:22 AM
chfast retitled this revision from to [DAGCombiner] Fix & simplify constant folding of sext/zext..
chfast updated this object.
chfast edited the test plan for this revision. (Show Details)
chfast added a subscriber: Unknown Object (MLST).

Thanks for finding this! I think the problems you're seeing revolve around the fact that integer build vectors scalar input operands can be of a wider type than the vector type - typically for legalization reasons. So your pcmpeqd regression is because the comparison result (v4i1) actually has scalar inputs of i32 that get implicitly truncated by BUILD_VECTOR.

lib/CodeGen/SelectionDAG/DAGCombiner.cpp
5609 ↗(On Diff #28126)

The operands of an integer build vector may be wider than the vector scalar type. You need to do something like
const APInt C = cast<ConstantSDNode>(Op)->getAPIntValue().trunc(EVTBits);

5611 ↗(On Diff #28126)

I think you need to just drop the 'getZExtValue()' and keep as a APInt:
Elts.push_back(DAG.getConstant(C.shl(ShAmt).ashr(ShAmt), DL, SVT);

5613 ↗(On Diff #28126)

Elts.push_back(DAG.getConstant(C.shl(ShAmt).lshr(ShAmt), DL, SVT);

test/CodeGen/X86/fold-vector-sext-crash2.ll
2 ↗(On Diff #28126)

Missing -mtriple args

chfast added inline comments.Jun 22 2015, 2:37 PM
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
5611 ↗(On Diff #28126)

That's definitely safer change. But isn't it the same?

RKSimon added inline comments.Jun 22 2015, 3:31 PM
lib/CodeGen/SelectionDAG/DAGCombiner.cpp
5609 ↗(On Diff #28126)

Sorry that should have been
const APInt &C = cast<ConstantSDNode>(Op)->getAPIntValue().zextOrTrunc(EVTBits);

The aim is to ensure that the constant starts out as the bitwidth of input vector's scalar size so that the sext/zext below can work correctly.

5611 ↗(On Diff #28126)

Sorry you're right - if you make the zextOrTrunc change above this should work.

chfast updated this revision to Diff 28212.Jun 23 2015, 3:01 AM

Trunc the constant value first as it might be wider than the scalar type. Thanks @RKSimon for the tip.

chfast edited the test plan for this revision. (Show Details)Jun 23 2015, 3:02 AM
chfast updated this revision to Diff 28213.Jun 23 2015, 3:06 AM

Add -march option to the regression test.

Simon, can you check it again?

RKSimon edited edge metadata.Jun 27 2015, 9:26 AM

Please can you add a zext test - you've updated that code path as well.

chfast updated this revision to Diff 28645.Jun 29 2015, 12:50 AM
chfast edited edge metadata.

Extend the test to check zext variant and constant values in output.

RKSimon accepted this revision.Jun 29 2015, 8:02 AM
RKSimon edited edge metadata.

Thanks, LGTM

This revision is now accepted and ready to land.Jun 29 2015, 8:02 AM
This revision was automatically updated to reflect the committed changes.