This is an archive of the discontinued LLVM Phabricator instance.

[PowerPC] Check hot loop exit edge in PPCCTRLoops
ClosedPublic

Authored by inouehrs on Jan 29 2018, 4:47 AM.

Details

Summary

PPCCTRLoops transform loops using mtctr/bdnz instructions if loop trip count is known and big enough to compensate for the cost of mtctr.
But if there is a loop exit edge which is known to be frequently taken (by builtin_expect or by PGO), we should not transform the loop to avoid the cost of mtctr instruction. Here is an example of a loop with hot exit edge:

for (unsigned i = 0; i < TripCount; i++) {
  // do something
  if (__builtin_expect(check(), 1))
    break;
  // do something
}

Diff Detail

Event Timeline

inouehrs created this revision.Jan 29 2018, 4:47 AM
inouehrs edited the summary of this revision. (Show Details)Jan 29 2018, 7:02 AM
inouehrs updated this revision to Diff 132101.Jan 30 2018, 11:07 PM
  • simplified the implementation

This seems like a good thing to do. Would you be able to provide some benchmark data to show the impact?

lib/Target/PowerPC/PPCCTRLoops.cpp
533

Is there a reason this isn't a range for loop? Also, we typically use I for instructions or iterators over them. Seems like BB might be a clearer name here.

test/CodeGen/PowerPC/ctrloops-hot-exit.ll
23

Is it possible to also construct a test case where the true branch is the exit branch?

inouehrs updated this revision to Diff 132322.Jan 31 2018, 8:44 PM
inouehrs marked 2 inline comments as done.Jan 31 2018, 8:50 PM

This seems like a good thing to do. Would you be able to provide some benchmark data to show the impact?

In a micro benchmark with a small loop always exit at the first iteration due to a hot exit branch, this patch gives about 20% performance improvements. I am testing SPEC benchmarks with PGO.

lib/Target/PowerPC/PPCCTRLoops.cpp
533

Thanks. This is much clearer.
I used iterator to be consistent with other loops in this method.

test/CodeGen/PowerPC/ctrloops-hot-exit.ll
23

I added variations where the true branch is for the exit branch.

nemanjai accepted this revision.Feb 1 2018, 1:33 PM

LGTM.

This revision is now accepted and ready to land.Feb 1 2018, 1:33 PM
This revision was automatically updated to reflect the committed changes.
inouehrs marked 2 inline comments as done.