This is an archive of the discontinued LLVM Phabricator instance.

Don't pretend to have probability information if the result of a bitwise and is compared against 0
ClosedPublic

Authored by djasper on Mar 9 2015, 6:28 AM.

Details

Reviewers
chandlerc
Summary

The code that originally made me discover this is:

if ((a & 0x1) == 0x1) {
  ..
}

In this case we don't actually have any branch probability information and should not assume to have any. LLVM transforms this into:

%and = and i32 %a, 1
%tobool = icmp eq i32 %and, 0

So, in this case, the result of a bitwise and is compared against 0. Again, we don't really have information. We could make this check more narrow, e.g. by testing whether the other and-operand is 1 or whether it has a single bit set.

Diff Detail

Event Timeline

djasper updated this revision to Diff 21481.Mar 9 2015, 6:28 AM
djasper retitled this revision from to Don't pretend to have probability information if the result of a bitwise and is compared against 0.
djasper updated this object.
djasper edited the test plan for this revision. (Show Details)
djasper added a reviewer: chandlerc.
djasper added a subscriber: Unknown Object (MLST).
chandlerc edited edge metadata.Mar 29 2015, 9:18 PM

This definitely seems like a good patch. A couple of high-level thoughts:

  1. You can test this directly by printing the analysis. I think we have a few tests like that.
  1. You should comment what is going on here and why.
  1. I feel like you need to handle more cases - != 0 at least, and maybe a few others maybe with a few other math operations?
lib/Analysis/BranchProbabilityInfo.cpp
385

If you need braces, just put them around the entire case.

387

I would use o two-level if with a condition expression. I find the && trick awkward.

djasper updated this revision to Diff 22878.Mar 30 2015, 5:20 AM
djasper updated this object.
djasper edited edge metadata.
  • Addressed review comments
  • Extended change to look at more comparisons
  • Narrowed check to only trigger on single bit bitmasks
  • Added specific branch probability tests

All test changes now seem correct.

Ping?

lib/Analysis/BranchProbabilityInfo.cpp
385

Done.

chandlerc accepted this revision.Apr 14 2015, 7:46 AM
chandlerc edited edge metadata.

This looks fine.

I feel like it should also be handling explicit 'i1' values as well, but that can be a separate patch.

lib/Analysis/BranchProbabilityInfo.cpp
383–390

I don't think these braces are useful, I'd omit all of them.

This revision is now accepted and ready to land.Apr 14 2015, 7:46 AM
djasper closed this revision.Apr 16 2015, 12:55 AM

Finally submitted in r234979.