Skip to content

Commit 93b88df

Browse files
jdenny-ornlDebadri Basak
authored andcommitted
[LoopUnroll] Fix block frequencies for epilogue (llvm#159163)
As another step in issue llvm#135812, this patch fixes block frequencies for partial loop unrolling with an epilogue remainder loop. It does not fully handle the case when the epilogue loop itself is unrolled. That will be handled in the next patch. For the guard and latch of each of the unrolled loop and epilogue loop, this patch sets branch weights derived directly from the original loop latch branch weights. The total frequency of the original loop body, summed across all its occurrences in the unrolled loop and epilogue loop, is the same as in the original loop. This patch also sets `llvm.loop.estimated_trip_count` for the epilogue loop instead of relying on the epilogue's latch branch weights to imply it. This patch fixes branch weights in tests that PR llvm#157754 adversely affected.
1 parent c81e6c8 commit 93b88df

File tree

12 files changed

+467
-62
lines changed

12 files changed

+467
-62
lines changed

llvm/include/llvm/Support/BranchProbability.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ class BranchProbability {
9797
/// \return \c Num divided by \c this.
9898
LLVM_ABI uint64_t scaleByInverse(uint64_t Num) const;
9999

100+
/// Compute pow(Probability, N).
101+
BranchProbability pow(unsigned N) const;
102+
100103
BranchProbability &operator+=(BranchProbability RHS) {
101104
assert(N != UnknownN && RHS.N != UnknownN &&
102105
"Unknown probability cannot participate in arithmetics.");

llvm/include/llvm/Transforms/Utils/LoopUtils.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,40 @@ LLVM_ABI bool setLoopEstimatedTripCount(
365365
Loop *L, unsigned EstimatedTripCount,
366366
std::optional<unsigned> EstimatedLoopInvocationWeight = std::nullopt);
367367

368+
/// Based on branch weight metadata, return either:
369+
/// - An unknown probability if the implementation is unable to handle the loop
370+
/// form of \p L (e.g., \p L must have a latch block that controls the loop
371+
/// exit).
372+
/// - The probability \c P that, at the end of any iteration, the latch of \p L
373+
/// will start another iteration such that `1 - P` is the probability of
374+
/// exiting the loop.
375+
BranchProbability getLoopProbability(Loop *L);
376+
377+
/// Set branch weight metadata for the latch of \p L to indicate that, at the
378+
/// end of any iteration, \p P and `1 - P` are the probabilities of starting
379+
/// another iteration and exiting the loop, respectively. Return false if the
380+
/// implementation is unable to handle the loop form of \p L (e.g., \p L must
381+
/// have a latch block that controls the loop exit). Otherwise, return true.
382+
bool setLoopProbability(Loop *L, BranchProbability P);
383+
384+
/// Based on branch weight metadata, return either:
385+
/// - An unknown probability if the implementation cannot extract the
386+
/// probability (e.g., \p B must have exactly two target labels, so it must be
387+
/// a conditional branch).
388+
/// - The probability \c P that control flows from \p B to its first target
389+
/// label such that `1 - P` is the probability of control flowing to its
390+
/// second target label, or vice-versa if \p ForFirstTarget is false.
391+
BranchProbability getBranchProbability(BranchInst *B, bool ForFirstTarget);
392+
393+
/// Set branch weight metadata for \p B to indicate that \p P and `1 - P` are
394+
/// the probabilities of control flowing to its first and second target labels,
395+
/// respectively, or vice-versa if \p ForFirstTarget is false. Return false if
396+
/// the implementation cannot set the probability (e.g., \p B must have exactly
397+
/// two target labels, so it must be a conditional branch). Otherwise, return
398+
/// true.
399+
bool setBranchProbability(BranchInst *B, BranchProbability P,
400+
bool ForFirstTarget);
401+
368402
/// Check inner loop (L) backedge count is known to be invariant on all
369403
/// iterations of its outer loop. If the loop has no parent, this is trivially
370404
/// true.

llvm/include/llvm/Transforms/Utils/UnrollLoop.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ LLVM_ABI bool UnrollRuntimeLoopRemainder(
9797
LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
9898
const TargetTransformInfo *TTI, bool PreserveLCSSA,
9999
unsigned SCEVExpansionBudget, bool RuntimeUnrollMultiExit,
100-
Loop **ResultLoop = nullptr);
100+
Loop **ResultLoop = nullptr,
101+
std::optional<unsigned> OriginalTripCount = std::nullopt,
102+
BranchProbability OriginalLoopProb = BranchProbability::getUnknown());
101103

102104
LLVM_ABI LoopUnrollResult UnrollAndJamLoop(
103105
Loop *L, unsigned Count, unsigned TripCount, unsigned TripMultiple,

llvm/lib/Support/BranchProbability.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,10 @@ uint64_t BranchProbability::scale(uint64_t Num) const {
111111
uint64_t BranchProbability::scaleByInverse(uint64_t Num) const {
112112
return ::scale<0>(Num, D, N);
113113
}
114+
115+
BranchProbability BranchProbability::pow(unsigned N) const {
116+
BranchProbability Res = BranchProbability::getOne();
117+
for (unsigned I = 0; I < N; ++I)
118+
Res *= *this;
119+
return Res;
120+
}

llvm/lib/Transforms/Utils/LoopUnroll.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,7 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
501501
const bool MaxOrZero = SE->isBackedgeTakenCountMaxOrZero(L);
502502
std::optional<unsigned> OriginalTripCount =
503503
llvm::getLoopEstimatedTripCount(L);
504+
BranchProbability OriginalLoopProb = llvm::getLoopProbability(L);
504505

505506
// Effectively "DCE" unrolled iterations that are beyond the max tripcount
506507
// and will never be executed.
@@ -591,11 +592,11 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
591592
: isEpilogProfitable(L);
592593

593594
if (ULO.Runtime &&
594-
!UnrollRuntimeLoopRemainder(L, ULO.Count, ULO.AllowExpensiveTripCount,
595-
EpilogProfitability, ULO.UnrollRemainder,
596-
ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI,
597-
PreserveLCSSA, ULO.SCEVExpansionBudget,
598-
ULO.RuntimeUnrollMultiExit, RemainderLoop)) {
595+
!UnrollRuntimeLoopRemainder(
596+
L, ULO.Count, ULO.AllowExpensiveTripCount, EpilogProfitability,
597+
ULO.UnrollRemainder, ULO.ForgetAllSCEV, LI, SE, DT, AC, TTI,
598+
PreserveLCSSA, ULO.SCEVExpansionBudget, ULO.RuntimeUnrollMultiExit,
599+
RemainderLoop, OriginalTripCount, OriginalLoopProb)) {
599600
if (ULO.Force)
600601
ULO.Runtime = false;
601602
else {
@@ -1129,13 +1130,13 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
11291130
LI->erase(L);
11301131
// We shouldn't try to use `L` anymore.
11311132
L = nullptr;
1132-
} else if (OriginalTripCount) {
1133+
} else {
11331134
// Update metadata for the loop's branch weights and estimated trip count:
11341135
// - If ULO.Runtime, UnrollRuntimeLoopRemainder sets the guard branch
11351136
// weights, latch branch weights, and estimated trip count of the
11361137
// remainder loop it creates. It also sets the branch weights for the
11371138
// unrolled loop guard it creates. The branch weights for the unrolled
1138-
// loop latch are adjusted below. FIXME: Actually handle ULO.Runtime.
1139+
// loop latch are adjusted below. FIXME: Handle prologue loops.
11391140
// - Otherwise, if unrolled loop iteration latches become unconditional,
11401141
// branch weights are adjusted above. FIXME: Actually handle such
11411142
// unconditional latches.
@@ -1158,10 +1159,17 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
11581159
// the unrolled loop as a whole without considering the branch weights for
11591160
// each unrolled iteration's latch within it, we store the new trip count as
11601161
// separate metadata.
1161-
unsigned NewTripCount = *OriginalTripCount / ULO.Count;
1162-
if (!ULO.Runtime && *OriginalTripCount % ULO.Count)
1163-
NewTripCount += 1;
1164-
setLoopEstimatedTripCount(L, NewTripCount);
1162+
if (!OriginalLoopProb.isUnknown() && ULO.Runtime && EpilogProfitability) {
1163+
// Where p is always the probability of executing at least 1 more
1164+
// iteration, the probability for at least n more iterations is p^n.
1165+
setLoopProbability(L, OriginalLoopProb.pow(ULO.Count));
1166+
}
1167+
if (OriginalTripCount) {
1168+
unsigned NewTripCount = *OriginalTripCount / ULO.Count;
1169+
if (!ULO.Runtime && *OriginalTripCount % ULO.Count)
1170+
++NewTripCount;
1171+
setLoopEstimatedTripCount(L, NewTripCount);
1172+
}
11651173
}
11661174

11671175
// LoopInfo should not be valid, confirm that.

llvm/lib/Transforms/Utils/LoopUnrollRuntime.cpp

Lines changed: 83 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
#include "llvm/Transforms/Utils/LoopUtils.h"
4141
#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
4242
#include "llvm/Transforms/Utils/UnrollLoop.h"
43+
#include <cmath>
4344

4445
using namespace llvm;
4546

@@ -195,6 +196,21 @@ static void ConnectProlog(Loop *L, Value *BECount, unsigned Count,
195196
}
196197
}
197198

199+
/// Assume, due to our position in the remainder loop or its guard, anywhere
200+
/// from 0 to \p N more iterations can possibly execute. Among such cases in
201+
/// the original loop (with loop probability \p OriginalLoopProb), what is the
202+
/// probability of executing at least one more iteration?
203+
static BranchProbability
204+
probOfNextInRemainder(BranchProbability OriginalLoopProb, unsigned N) {
205+
// Each of these variables holds the original loop's probability that the
206+
// number of iterations it will execute is some m in the specified range.
207+
BranchProbability ProbOne = OriginalLoopProb; // 1 <= m
208+
BranchProbability ProbTooMany = ProbOne.pow(N + 1); // N + 1 <= m
209+
BranchProbability ProbNotTooMany = ProbTooMany.getCompl(); // 0 <= m <= N
210+
BranchProbability ProbOneNotTooMany = ProbOne - ProbTooMany; // 1 <= m <= N
211+
return ProbOneNotTooMany / ProbNotTooMany;
212+
}
213+
198214
/// Connect the unrolling epilog code to the original loop.
199215
/// The unrolling epilog code contains code to execute the
200216
/// 'extra' iterations if the run-time trip count modulo the
@@ -221,7 +237,8 @@ static void ConnectEpilog(Loop *L, Value *ModVal, BasicBlock *NewExit,
221237
BasicBlock *EpilogPreHeader, BasicBlock *NewPreHeader,
222238
ValueToValueMapTy &VMap, DominatorTree *DT,
223239
LoopInfo *LI, bool PreserveLCSSA, ScalarEvolution &SE,
224-
unsigned Count, AssumptionCache &AC) {
240+
unsigned Count, AssumptionCache &AC,
241+
BranchProbability OriginalLoopProb) {
225242
BasicBlock *Latch = L->getLoopLatch();
226243
assert(Latch && "Loop must have a latch");
227244
BasicBlock *EpilogLatch = cast<BasicBlock>(VMap[Latch]);
@@ -332,12 +349,19 @@ static void ConnectEpilog(Loop *L, Value *ModVal, BasicBlock *NewExit,
332349
PreserveLCSSA);
333350
// Add the branch to the exit block (around the epilog loop)
334351
MDNode *BranchWeights = nullptr;
335-
if (hasBranchWeightMD(*Latch->getTerminator())) {
352+
if (OriginalLoopProb.isUnknown() &&
353+
hasBranchWeightMD(*Latch->getTerminator())) {
336354
// Assume equal distribution in interval [0, Count).
337355
MDBuilder MDB(B.getContext());
338356
BranchWeights = MDB.createBranchWeights(1, Count - 1);
339357
}
340-
B.CreateCondBr(BrLoopExit, EpilogPreHeader, Exit, BranchWeights);
358+
BranchInst *RemainderLoopGuard =
359+
B.CreateCondBr(BrLoopExit, EpilogPreHeader, Exit, BranchWeights);
360+
if (!OriginalLoopProb.isUnknown()) {
361+
setBranchProbability(RemainderLoopGuard,
362+
probOfNextInRemainder(OriginalLoopProb, Count - 1),
363+
/*ForFirstTarget=*/true);
364+
}
341365
InsertPt->eraseFromParent();
342366
if (DT) {
343367
auto *NewDom = DT->findNearestCommonDominator(Exit, NewExit);
@@ -357,14 +381,15 @@ static void ConnectEpilog(Loop *L, Value *ModVal, BasicBlock *NewExit,
357381
/// The cloned blocks should be inserted between InsertTop and InsertBot.
358382
/// InsertTop should be new preheader, InsertBot new loop exit.
359383
/// Returns the new cloned loop that is created.
360-
static Loop *
361-
CloneLoopBlocks(Loop *L, Value *NewIter, const bool UseEpilogRemainder,
362-
const bool UnrollRemainder,
363-
BasicBlock *InsertTop,
364-
BasicBlock *InsertBot, BasicBlock *Preheader,
384+
static Loop *CloneLoopBlocks(Loop *L, Value *NewIter,
385+
const bool UseEpilogRemainder,
386+
const bool UnrollRemainder, BasicBlock *InsertTop,
387+
BasicBlock *InsertBot, BasicBlock *Preheader,
365388
std::vector<BasicBlock *> &NewBlocks,
366389
LoopBlocksDFS &LoopBlocks, ValueToValueMapTy &VMap,
367-
DominatorTree *DT, LoopInfo *LI, unsigned Count) {
390+
DominatorTree *DT, LoopInfo *LI, unsigned Count,
391+
std::optional<unsigned> OriginalTripCount,
392+
BranchProbability OriginalLoopProb) {
368393
StringRef suffix = UseEpilogRemainder ? "epil" : "prol";
369394
BasicBlock *Header = L->getHeader();
370395
BasicBlock *Latch = L->getLoopLatch();
@@ -419,7 +444,8 @@ CloneLoopBlocks(Loop *L, Value *NewIter, const bool UseEpilogRemainder,
419444
Builder.CreateAdd(NewIdx, One, NewIdx->getName() + ".next");
420445
Value *IdxCmp = Builder.CreateICmpNE(IdxNext, NewIter, NewIdx->getName() + ".cmp");
421446
MDNode *BranchWeights = nullptr;
422-
if (hasBranchWeightMD(*LatchBR)) {
447+
if ((OriginalLoopProb.isUnknown() || !UseEpilogRemainder) &&
448+
hasBranchWeightMD(*LatchBR)) {
423449
uint32_t ExitWeight;
424450
uint32_t BackEdgeWeight;
425451
if (Count >= 3) {
@@ -437,7 +463,29 @@ CloneLoopBlocks(Loop *L, Value *NewIter, const bool UseEpilogRemainder,
437463
MDBuilder MDB(Builder.getContext());
438464
BranchWeights = MDB.createBranchWeights(BackEdgeWeight, ExitWeight);
439465
}
440-
Builder.CreateCondBr(IdxCmp, FirstLoopBB, InsertBot, BranchWeights);
466+
BranchInst *RemainderLoopLatch =
467+
Builder.CreateCondBr(IdxCmp, FirstLoopBB, InsertBot, BranchWeights);
468+
if (!OriginalLoopProb.isUnknown() && UseEpilogRemainder) {
469+
// Compute the total frequency of the original loop body from the
470+
// remainder iterations. Once we've reached them, the first of them
471+
// always executes, so its frequency and probability are 1.
472+
double FreqRemIters = 1;
473+
if (Count > 2) {
474+
BranchProbability ProbReaching = BranchProbability::getOne();
475+
for (unsigned N = Count - 2; N >= 1; --N) {
476+
ProbReaching *= probOfNextInRemainder(OriginalLoopProb, N);
477+
FreqRemIters += double(ProbReaching.getNumerator()) /
478+
ProbReaching.getDenominator();
479+
}
480+
}
481+
// Solve for the loop probability that would produce that frequency.
482+
// Sum(i=0..inf)(Prob^i) = 1/(1-Prob) = FreqRemIters.
483+
double ProbDouble = 1 - 1 / FreqRemIters;
484+
BranchProbability Prob = BranchProbability::getBranchProbability(
485+
std::round(ProbDouble * BranchProbability::getDenominator()),
486+
BranchProbability::getDenominator());
487+
setBranchProbability(RemainderLoopLatch, Prob, /*ForFirstTarget=*/true);
488+
}
441489
NewIdx->addIncoming(Zero, InsertTop);
442490
NewIdx->addIncoming(IdxNext, NewBB);
443491
LatchBR->eraseFromParent();
@@ -461,6 +509,9 @@ CloneLoopBlocks(Loop *L, Value *NewIter, const bool UseEpilogRemainder,
461509
Loop *NewLoop = NewLoops[L];
462510
assert(NewLoop && "L should have been cloned");
463511

512+
if (OriginalTripCount && UseEpilogRemainder)
513+
setLoopEstimatedTripCount(NewLoop, *OriginalTripCount % Count);
514+
464515
// Add unroll disable metadata to disable future unrolling for this loop.
465516
if (!UnrollRemainder)
466517
NewLoop->setLoopAlreadyUnrolled();
@@ -588,7 +639,8 @@ bool llvm::UnrollRuntimeLoopRemainder(
588639
LoopInfo *LI, ScalarEvolution *SE, DominatorTree *DT, AssumptionCache *AC,
589640
const TargetTransformInfo *TTI, bool PreserveLCSSA,
590641
unsigned SCEVExpansionBudget, bool RuntimeUnrollMultiExit,
591-
Loop **ResultLoop) {
642+
Loop **ResultLoop, std::optional<unsigned> OriginalTripCount,
643+
BranchProbability OriginalLoopProb) {
592644
LLVM_DEBUG(dbgs() << "Trying runtime unrolling on Loop: \n");
593645
LLVM_DEBUG(L->dump());
594646
LLVM_DEBUG(UseEpilogRemainder ? dbgs() << "Using epilog remainder.\n"
@@ -808,12 +860,23 @@ bool llvm::UnrollRuntimeLoopRemainder(
808860
BasicBlock *UnrollingLoop = UseEpilogRemainder ? NewPreHeader : PrologExit;
809861
// Branch to either remainder (extra iterations) loop or unrolling loop.
810862
MDNode *BranchWeights = nullptr;
811-
if (hasBranchWeightMD(*Latch->getTerminator())) {
863+
if ((OriginalLoopProb.isUnknown() || !UseEpilogRemainder) &&
864+
hasBranchWeightMD(*Latch->getTerminator())) {
812865
// Assume loop is nearly always entered.
813866
MDBuilder MDB(B.getContext());
814867
BranchWeights = MDB.createBranchWeights(EpilogHeaderWeights);
815868
}
816-
B.CreateCondBr(BranchVal, RemainderLoop, UnrollingLoop, BranchWeights);
869+
BranchInst *UnrollingLoopGuard =
870+
B.CreateCondBr(BranchVal, RemainderLoop, UnrollingLoop, BranchWeights);
871+
if (!OriginalLoopProb.isUnknown() && UseEpilogRemainder) {
872+
// The original loop's first iteration always happens. Compute the
873+
// probability of the original loop executing Count-1 iterations after that
874+
// to complete the first iteration of the unrolled loop.
875+
BranchProbability ProbOne = OriginalLoopProb;
876+
BranchProbability ProbRest = ProbOne.pow(Count - 1);
877+
setBranchProbability(UnrollingLoopGuard, ProbRest,
878+
/*ForFirstTarget=*/false);
879+
}
817880
PreHeaderBR->eraseFromParent();
818881
if (DT) {
819882
if (UseEpilogRemainder)
@@ -840,9 +903,10 @@ bool llvm::UnrollRuntimeLoopRemainder(
840903
// iterations. This function adds the appropriate CFG connections.
841904
BasicBlock *InsertBot = UseEpilogRemainder ? LatchExit : PrologExit;
842905
BasicBlock *InsertTop = UseEpilogRemainder ? EpilogPreHeader : PrologPreHeader;
843-
Loop *remainderLoop = CloneLoopBlocks(
844-
L, ModVal, UseEpilogRemainder, UnrollRemainder, InsertTop, InsertBot,
845-
NewPreHeader, NewBlocks, LoopBlocks, VMap, DT, LI, Count);
906+
Loop *remainderLoop =
907+
CloneLoopBlocks(L, ModVal, UseEpilogRemainder, UnrollRemainder, InsertTop,
908+
InsertBot, NewPreHeader, NewBlocks, LoopBlocks, VMap, DT,
909+
LI, Count, OriginalTripCount, OriginalLoopProb);
846910

847911
// Insert the cloned blocks into the function.
848912
F->splice(InsertBot->getIterator(), F, NewBlocks[0]->getIterator(), F->end());
@@ -941,7 +1005,8 @@ bool llvm::UnrollRuntimeLoopRemainder(
9411005
// Connect the epilog code to the original loop and update the
9421006
// PHI functions.
9431007
ConnectEpilog(L, ModVal, NewExit, LatchExit, PreHeader, EpilogPreHeader,
944-
NewPreHeader, VMap, DT, LI, PreserveLCSSA, *SE, Count, *AC);
1008+
NewPreHeader, VMap, DT, LI, PreserveLCSSA, *SE, Count, *AC,
1009+
OriginalLoopProb);
9451010

9461011
// Update counter in loop for unrolling.
9471012
// Use an incrementing IV. Pre-incr/post-incr is backedge/trip count.

llvm/lib/Transforms/Utils/LoopUtils.cpp

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -962,13 +962,51 @@ bool llvm::setLoopEstimatedTripCount(
962962
if (LatchBranch->getSuccessor(0) != L->getHeader())
963963
std::swap(BackedgeTakenWeight, LatchExitWeight);
964964

965-
MDBuilder MDB(LatchBranch->getContext());
966-
967965
// Set/Update profile metadata.
968-
LatchBranch->setMetadata(
969-
LLVMContext::MD_prof,
970-
MDB.createBranchWeights(BackedgeTakenWeight, LatchExitWeight));
966+
setBranchWeights(*LatchBranch, {BackedgeTakenWeight, LatchExitWeight},
967+
/*IsExpected=*/false);
968+
969+
return true;
970+
}
971+
972+
BranchProbability llvm::getLoopProbability(Loop *L) {
973+
BranchInst *LatchBranch = getExpectedExitLoopLatchBranch(L);
974+
if (!LatchBranch)
975+
return BranchProbability::getUnknown();
976+
bool FirstTargetIsLoop = LatchBranch->getSuccessor(0) == L->getHeader();
977+
return getBranchProbability(LatchBranch, FirstTargetIsLoop);
978+
}
971979

980+
bool llvm::setLoopProbability(Loop *L, BranchProbability P) {
981+
BranchInst *LatchBranch = getExpectedExitLoopLatchBranch(L);
982+
if (!LatchBranch)
983+
return false;
984+
bool FirstTargetIsLoop = LatchBranch->getSuccessor(0) == L->getHeader();
985+
return setBranchProbability(LatchBranch, P, FirstTargetIsLoop);
986+
}
987+
988+
BranchProbability llvm::getBranchProbability(BranchInst *B,
989+
bool ForFirstTarget) {
990+
if (B->getNumSuccessors() != 2)
991+
return BranchProbability::getUnknown();
992+
uint64_t Weight0, Weight1;
993+
if (!extractBranchWeights(*B, Weight0, Weight1))
994+
return BranchProbability::getUnknown();
995+
if (!ForFirstTarget)
996+
std::swap(Weight0, Weight1);
997+
return BranchProbability::getBranchProbability(Weight0, Weight0 + Weight1);
998+
}
999+
1000+
bool llvm::setBranchProbability(BranchInst *B, BranchProbability P,
1001+
bool ForFirstTarget) {
1002+
if (B->getNumSuccessors() != 2)
1003+
return false;
1004+
BranchProbability Prob0 = P;
1005+
BranchProbability Prob1 = P.getCompl();
1006+
if (!ForFirstTarget)
1007+
std::swap(Prob0, Prob1);
1008+
setBranchWeights(*B, {Prob0.getNumerator(), Prob1.getNumerator()},
1009+
/*IsExpected=*/false);
9721010
return true;
9731011
}
9741012

0 commit comments

Comments
 (0)