This is an archive of the discontinued LLVM Phabricator instance.

[BPI] Don't assume that strcmp returning >0 is more likely than <0
ClosedPublic

Authored by john.brawn on Jun 6 2017, 4:57 AM.

Details

Summary

The zero heuristic assumes that integers are more likely positive than negative, but this also has the effect of assuming that strcmp return values are more likely positive than negative. Given that for nonzero strcmp return values it's the ordering of arguments that determines the sign of the result there's no reason to assume that's true.

Fix this by inspecting the LHS of the compare and using TargetLibraryInfo to decide if it's strcmp-like, and if so only assume that nonzero is more likely than zero i.e. strings are more often different than the same. This causes a slight code generation change in the spec2006 benchmark 403.gcc, but with no noticeable performance impact. The intent of this patch is to allow better optimisation of dhrystone on Cortex-M cpus, but currently it won't as there are also some changes that need to be made to if-conversion.

Diff Detail

Repository
rL LLVM

Event Timeline

john.brawn created this revision.Jun 6 2017, 4:57 AM
chandlerc edited edge metadata.

Generally makes sense, but maybe check the intuition of some other folks who've been hacking on static heuristics recently... Also, a small simplifying (I hope) comment inline.

include/llvm/Analysis/BranchProbabilityInfo.h
48

Is all of the support for null TLI necessary? It seems like we should always be able to provide it..

john.brawn added inline comments.Jun 6 2017, 5:42 AM
include/llvm/Analysis/BranchProbabilityInfo.h
48

BranchProbabilityInfo is created directly (without using the pass manager) in ModuleSummaryAnalysis, OptimizationDiagnosticInfo, PartialInlining, and PGOInstrumentation, and would require some extra plumbing to have a TargetLibraryInfo available at the point it is created. Or alternatively I could create TargetLibraryInfo directly, except that's messy as there's no simple way to do that.

skatkov edited edge metadata.Jun 6 2017, 9:39 AM

Looks reasonable to me.
I would rename the name of the test to something related to string compare instead of just call.

davidxl added inline comments.Jun 6 2017, 9:46 AM
lib/Analysis/BranchProbabilityInfo.cpp
494

memcmp should be handled similarly here.

john.brawn updated this revision to Diff 101740.Jun 7 2017, 8:09 AM

Handle memcmp in the same way, and adjust the name of the test.

davidxl accepted this revision.Jun 7 2017, 9:36 AM

lgtm

This revision is now accepted and ready to land.Jun 7 2017, 9:36 AM
This revision was automatically updated to reflect the committed changes.