This is an archive of the discontinued LLVM Phabricator instance.

[IR] Use a binary search in DataLayout::getAlignmentInfo
ClosedPublic

Authored by craig.topper on Mar 21 2017, 8:21 PM.

Details

Summary

We currently do a linear scan through all of the Alignments array entries anytime getAlignmentInfo is called. I noticed while profiling compile time on a -O2 opt run that this function can be called quite frequently and was showing about as about 1% of the time in callgrind.

This patch puts the Alignments array into a sorted order by type and then by bitwidth. We can then do a binary search. And use the sorted nature to handle the special cases for INTEGER_ALIGN. Some of this is modeled after the sorting/searching we do for pointers already.

This reduced the time spent in this routine by about 2/3 in the one compilation I was looking at.

We could maybe improve this more by using a DenseMap to cache the results, but just sorting was easy and didn't require extra data structure. And I think it made the integer handling simpler.

Diff Detail

Repository
rL LLVM

Event Timeline

craig.topper created this revision.Mar 21 2017, 8:21 PM
arsenm accepted this revision.Mar 21 2017, 8:42 PM
arsenm added a subscriber: arsenm.

LGTM

This revision is now accepted and ready to land.Mar 21 2017, 8:42 PM
mehdi_amini edited edge metadata.Mar 21 2017, 8:46 PM

It is not clear to me why an associative container instead of a vector wouldn't be even easier to use?

include/llvm/IR/DataLayout.h
123 ↗(On Diff #92590)

Document that it is sorted by construction.

lib/IR/DataLayout.cpp
443 ↗(On Diff #92590)

Here as well: a reference to the fact that we insert in the right position to keep the vector sorted.

This revision was automatically updated to reflect the committed changes.
davide edited edge metadata.Mar 22 2017, 11:34 PM

lg, thanks for the fix.