This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] Improve ComputeNumSignBits to handle Trunc
ClosedPublic

Authored by resistor on Dec 30 2022, 10:13 PM.

Diff Detail

Event Timeline

resistor created this revision.Dec 30 2022, 10:13 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 30 2022, 10:13 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
resistor requested review of this revision.Dec 30 2022, 10:13 PM
Herald added a project: Restricted Project. · View Herald TranscriptDec 30 2022, 10:13 PM
nikic added inline comments.Dec 31 2022, 2:02 AM
llvm/lib/Analysis/ValueTracking.cpp
3315–3325

return 1 here, we don't want to fall through to computeKnownBits (the ComptueNumSignBits will have already used it if possible).

3319

I don't think this is right for vectors. You're taking the size of the full vector here, not of the vector elements. Using U->getOperand(0)->getType()->getScalarSizeInBits() should be fine here (as trunc is only defined on integers, so we don't need fancy DL machinery).

Please also add a vector test.

3320

nit: Omit braces for single-line if.

nikic retitled this revision from Improve ComputeNumSignBits to handle Trunc. to [ValueTracking] Improve ComputeNumSignBits to handle Trunc.Dec 31 2022, 2:06 AM
resistor added inline comments.Jan 1 2023, 8:12 PM
llvm/lib/Analysis/ValueTracking.cpp
3315–3325

I don't think that's the case? We invoked ComputeNumSignBits on the input to the trunc, so we haven't yet had an opportunity to use computeKnownBits results for the output.

resistor updated this revision to Diff 485839.Jan 1 2023, 8:21 PM

Update for test comments and add tests.

resistor marked 2 inline comments as done.Jan 1 2023, 8:22 PM
nikic added inline comments.Jan 2 2023, 1:44 AM
llvm/lib/Analysis/ValueTracking.cpp
3315–3325

Hm, I guess you are right. If the truncated part doesn't have sign bits and the part directly following has more than one known zero/one bits, then the computeKnownBits() call is still useful.

Do we already have a test that fails if it is removed?

llvm/test/Transforms/InstCombine/vector-trunc.ll
25

It would be slightly better to test the values where the behavior flips, so I guess that should be a shift by 17 above and by 16 here.

42

Am I missing something, or is this test the same as the previous one?

resistor updated this revision to Diff 485916.Jan 2 2023, 9:08 PM

Update for feedback

llvm/lib/Analysis/ValueTracking.cpp
3315–3325

No test currently fails, so I've gone ahead and changed it.

nikic accepted this revision.Jan 3 2023, 1:55 AM

LGTM

This revision is now accepted and ready to land.Jan 3 2023, 1:55 AM
This revision was landed with ongoing or failed builds.Jan 3 2023, 2:26 PM
This revision was automatically updated to reflect the committed changes.