This is an archive of the discontinued LLVM Phabricator instance.

[ValueTracking] Teach computeKnownBits and ComputeNumSignBits to look through ExtractElement.
ClosedPublic

Authored by bjope on Sep 27 2016, 12:44 AM.

Details

Summary

The computeKnownBits and ComputeNumSignBits functions in ValueTracking can now do a simple look-through of ExtractElement.

Diff Detail

Event Timeline

bjope updated this revision to Diff 72494.Sep 27 2016, 12:44 AM
bjope retitled this revision from to [ValueTracking] Teach computeKnownBits and ComputeNumSignBits to look through ExtractElement..
bjope updated this object.
bjope added reviewers: majnemer, spatel.
bjope added a subscriber: llvm-commits.
spatel edited edge metadata.Sep 27 2016, 10:26 AM

The idea looks good to me, but we should increase the depth for the recursive call. The reason that depth is not incremented for the ExtractValue case is that computeKnownBitsAddSub / computeKnownBitsMul are helper functions (they are not recursing on themselves), so the depth is incremented internally when calling computeKnownBits.

test/Analysis/ValueTracking/signbits-extract-elt.ll
2

Use -instsimplify instead.

8–10

The test case is overly complicated because something in -instcombine is able to reduce a simplified version of the test. If you change the test to use -instsimplify, then this test will prove that your ValueTracking change is firing:

define i1 @computeKnownBits_look_through_extractelt(<2 x i8> %vecin) {
  %vec = zext <2 x i8> %vecin to <2 x i32>
  %elt1 = extractelement <2 x i32> %vec, i32 1
  %bool = icmp slt i32 %elt1, 0
  ret i1 %bool
}
24–39

We have to dig deeper to find an instsimplify fold that works based on ComputeNumSignBits, but I think this will do it:

define i32 @computeNumSignBits_look_through_extractelt(<2 x i1> %vec) {
  %vec4 = sext <2 x i1> %vec to <2 x i32>
  %elt0 = extractelement <2 x i32> %vec4, i32 0
  %ashr = ashr i32 %elt0, 5  <--- this will disappear after this patch is applied
  ret i32 %ashr
}
bjope added a comment.Sep 28 2016, 1:40 AM

The idea looks good to me, but we should increase the depth for the recursive call. The reason that depth is not incremented for the ExtractValue case is that computeKnownBitsAddSub / computeKnownBitsMul are helper functions (they are not recursing on themselves), so the depth is incremented internally when calling computeKnownBits.

Ok, well, if Depth is about level of recursion then I agree. If it is about "search depth", then I got the feeling that it was about how many "operational" instructions that could be analysed. In the case of ExtractValue it increases Depth by one, when analysing both the ExtractValue and the add/sub/mul operation. So from an LLVM IR perspective it traverses two instructions on the same Depth, and ExtractValue does not count towards Depth.

Anyhow, I'll make sure we increase the Depth when looking through ExtractElement.

BTW, highly appreciate your comments about the testcases. It will be much easier to use -instsimplify for these tests.

bjope updated this revision to Diff 72804.Sep 28 2016, 5:07 AM
bjope edited edge metadata.

Updated accoring to recommendations from Sanjay.

spatel accepted this revision.Sep 28 2016, 6:30 AM
spatel edited edge metadata.

Ok, well, if Depth is about level of recursion then I agree. If it is about "search depth", then I got the feeling that it was about how many "operational" instructions that could be analysed.

Ah, I didn't think of it that way. You can ask on the dev list if you'd like a more authoritative answer. It's fuzzy to me since it's based on a magic '6' presented with no code comments. :)

LGTM - see inline for a couple of nits.

test/Analysis/ValueTracking/signbits-extract-elt.ll
5

typo: now -> know

18–21

A tip (mostly for future reference): you can auto-generate the CHECK lines using the script in utils/update_test_checks.py. The only difference in this case should be that the script will create FileCheck variables for you. That will provide flexibility in case the intermediate variable names change for some unrelated reason.

This revision is now accepted and ready to land.Sep 28 2016, 6:30 AM
bjope updated this revision to Diff 72825.Sep 28 2016, 7:38 AM
bjope edited edge metadata.

Corrected type in test case and auto-generated FileCheck assertions.

We should probably wait for relaxed checks in a clang test case (see https://reviews.llvm.org/D24397 ) before commit.
But I hope someone will help me commit this when that test case has been corrected.

Corrected type in test case and auto-generated FileCheck assertions.

We should probably wait for relaxed checks in a clang test case (see https://reviews.llvm.org/D24397 ) before commit.

No - a clang regression test should not be dependent on the optimizer; usually, it should only be checking IR with -O0 or -Xclang -disable-llvm-optzns.

bjope closed this revision.Oct 6 2016, 5:00 AM

Committed revision 283434