Set the estimated trip count metadata (and optional latch weights) for L.

Synopsis

Declared in <llvm/Transforms/Utils/LoopUtils.h>

bool
setLoopEstimatedTripCount(
    Loop* L,
    unsigned int EstimatedTripCount,
    std::optional<unsigned int> EstimatedLoopInvocationWeight = std::nullopt);

Description

Set llvm.loop.estimated_trip_count with the value EstimatedTripCount in the loop metadata of L. Return false if the implementation is unable to handle the loop form of L (e.g., L must have a latch block that controls the loop exit). Otherwise, return true.

In addition, if EstimatedLoopInvocationWeight

  • Set the branch weight metadata of L to reflect that L has an estimated EstimatedTripCount iterations and has *EstimatedLoopInvocationWeight exit weight through the loop's latch.

  • If EstimatedTripCount is zero, set the backedge weight to 0 and exit edge to 1. The EstimatedTripCount is relative to the original loop entry, but the branch weights are encoding the probabilities of the true/false edges. The latter cannot validly be 0‐0, because if the control flow arrived here, one of the branches must be taken. Moreover, BranchProbabilityInfo treats 0‐0 branch weights as if they were 1‐1. Assuming accurate profile information, a 0 EstimatedTripCount should correspond to a very low, or 0, BFI for the loop body. This should mean that the BPI info leading to the loop also gives a very low, or 0, probability to arriving there. If that probability is not exactly 0, 0‐0 branch weights would raise the BFI of the loop (as it would really be treated as 1‐1). With the 0‐1 (i.e. 100% exit) encoding, the BFI stays as low as the rest of the CFG's BPI dictates.

TODO: Eventually, once all passes have migrated away from setting branch weights to indicate estimated trip counts, this function will drop the EstimatedLoopInvocationWeight parameter.

Return Value

True if the estimated trip count was set; false if unsupported.

Parameters

Name

Description

L

The loop whose estimated trip count is set.

EstimatedTripCount

Estimated iterations to store on the loop.

EstimatedLoopInvocationWeight

Optional latch exit weight used to rebuild branch weights.

Created with MrDocs