This is quite interesting. Is there anything preventing the collection of the number of cycles spent in an arbitrary function? It seems this is just a matter of identifying all branches.
I would better go for analyzing not the whole function (all basic blocks of the function) but only the Hyper Blocks (typical hot path through the function). Here is the example of how to do it: https://lwn.net/Articles/680985/ chapter "Hot-path analysis".