Skip to content

Commit 0ffa833

Browse files
author
Serguei Katkov
committed
[LoopInfo] Use early return in branch weight update functions. NFC.
llvm-svn: 366411
1 parent dad1beb commit 0ffa833

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

llvm/lib/Transforms/Utils/LoopUnrollPeel.cpp

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -382,23 +382,23 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
382382
static void updateBranchWeights(BasicBlock *Header, BranchInst *LatchBR,
383383
unsigned IterNumber, unsigned AvgIters,
384384
uint64_t &PeeledHeaderWeight) {
385+
if (!PeeledHeaderWeight)
386+
return;
385387
// FIXME: Pick a more realistic distribution.
386388
// Currently the proportion of weight we assign to the fall-through
387389
// side of the branch drops linearly with the iteration number, and we use
388390
// a 0.9 fudge factor to make the drop-off less sharp...
389-
if (PeeledHeaderWeight) {
390-
uint64_t FallThruWeight =
391-
PeeledHeaderWeight * ((float)(AvgIters - IterNumber) / AvgIters * 0.9);
392-
uint64_t ExitWeight = PeeledHeaderWeight - FallThruWeight;
393-
PeeledHeaderWeight -= ExitWeight;
394-
395-
unsigned HeaderIdx = (LatchBR->getSuccessor(0) == Header ? 0 : 1);
396-
MDBuilder MDB(LatchBR->getContext());
397-
MDNode *WeightNode =
398-
HeaderIdx ? MDB.createBranchWeights(ExitWeight, FallThruWeight)
399-
: MDB.createBranchWeights(FallThruWeight, ExitWeight);
400-
LatchBR->setMetadata(LLVMContext::MD_prof, WeightNode);
401-
}
391+
uint64_t FallThruWeight =
392+
PeeledHeaderWeight * ((float)(AvgIters - IterNumber) / AvgIters * 0.9);
393+
uint64_t ExitWeight = PeeledHeaderWeight - FallThruWeight;
394+
PeeledHeaderWeight -= ExitWeight;
395+
396+
unsigned HeaderIdx = (LatchBR->getSuccessor(0) == Header ? 0 : 1);
397+
MDBuilder MDB(LatchBR->getContext());
398+
MDNode *WeightNode =
399+
HeaderIdx ? MDB.createBranchWeights(ExitWeight, FallThruWeight)
400+
: MDB.createBranchWeights(FallThruWeight, ExitWeight);
401+
LatchBR->setMetadata(LLVMContext::MD_prof, WeightNode);
402402
}
403403

404404
/// Initialize the weights.
@@ -430,22 +430,23 @@ static void initBranchWeights(BasicBlock *Header, BranchInst *LatchBR,
430430
static void fixupBranchWeights(BasicBlock *Header, BranchInst *LatchBR,
431431
uint64_t ExitWeight, uint64_t CurHeaderWeight) {
432432
// Adjust the branch weights on the loop exit.
433-
if (ExitWeight) {
434-
// The backedge count is the difference of current header weight and
435-
// current loop exit weight. If the current header weight is smaller than
436-
// the current loop exit weight, we mark the loop backedge weight as 1.
437-
uint64_t BackEdgeWeight = 0;
438-
if (ExitWeight < CurHeaderWeight)
439-
BackEdgeWeight = CurHeaderWeight - ExitWeight;
440-
else
441-
BackEdgeWeight = 1;
442-
MDBuilder MDB(LatchBR->getContext());
443-
unsigned HeaderIdx = LatchBR->getSuccessor(0) == Header ? 0 : 1;
444-
MDNode *WeightNode =
445-
HeaderIdx ? MDB.createBranchWeights(ExitWeight, BackEdgeWeight)
446-
: MDB.createBranchWeights(BackEdgeWeight, ExitWeight);
447-
LatchBR->setMetadata(LLVMContext::MD_prof, WeightNode);
448-
}
433+
if (!ExitWeight)
434+
return;
435+
436+
// The backedge count is the difference of current header weight and
437+
// current loop exit weight. If the current header weight is smaller than
438+
// the current loop exit weight, we mark the loop backedge weight as 1.
439+
uint64_t BackEdgeWeight = 0;
440+
if (ExitWeight < CurHeaderWeight)
441+
BackEdgeWeight = CurHeaderWeight - ExitWeight;
442+
else
443+
BackEdgeWeight = 1;
444+
MDBuilder MDB(LatchBR->getContext());
445+
unsigned HeaderIdx = LatchBR->getSuccessor(0) == Header ? 0 : 1;
446+
MDNode *WeightNode =
447+
HeaderIdx ? MDB.createBranchWeights(ExitWeight, BackEdgeWeight)
448+
: MDB.createBranchWeights(BackEdgeWeight, ExitWeight);
449+
LatchBR->setMetadata(LLVMContext::MD_prof, WeightNode);
449450
}
450451

451452
/// Clones the body of the loop L, putting it between \p InsertTop and \p

0 commit comments

Comments
 (0)