This is Work In Progress.
I'm posting here to solicit feedback.
One motivation for doing this is to improve the loop versioning currently done by LoopVectorizer (LoopVectorizePass::processLoop).
Description:
For certain loops, (TBD what kind), we would like to profile the exact trip count and the
frequency of that trip count. In other words, we want to value profile an expression that
represents the trip count of a loop, whenever it is computable.
The instrumentation point is the loop pre-header, and the value profile (VP) metadata (MD)
is appended to the llvm.loop MD which sits on the branch instruction of the latch block.
In order to find or create a loop pre-header, we run the LoopSimplifyPass pass in the
pipeline for the new pass manager.
The logic in LoopInfoPlugin in ValueProfilePlugins.inc determines the instrumentation
point (needed during the -fprofile-generate step) and what to associate the MD (needed
during the -fprofile-use step).
Instead of having the plugin decide the exact instruction for associating the MD to, we allow
the plugin to select a Loop to associate it to, and then in PGOUseFunc::annotateValueSites
in PGOInstrumentation.cpp we call setLoopTripCount(MD) on the Loop object.
This way, the Loop object maintains control (set/get) over what MD is associated to the
loop id MD node (a.k.a. the !llvm.loop MD).