@@ -382,23 +382,23 @@ void llvm::computePeelCount(Loop *L, unsigned LoopSize,
382
382
static void updateBranchWeights (BasicBlock *Header, BranchInst *LatchBR,
383
383
unsigned IterNumber, unsigned AvgIters,
384
384
uint64_t &PeeledHeaderWeight) {
385
+ if (!PeeledHeaderWeight)
386
+ return ;
385
387
// FIXME: Pick a more realistic distribution.
386
388
// Currently the proportion of weight we assign to the fall-through
387
389
// side of the branch drops linearly with the iteration number, and we use
388
390
// 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);
402
402
}
403
403
404
404
// / Initialize the weights.
@@ -430,22 +430,23 @@ static void initBranchWeights(BasicBlock *Header, BranchInst *LatchBR,
430
430
static void fixupBranchWeights (BasicBlock *Header, BranchInst *LatchBR,
431
431
uint64_t ExitWeight, uint64_t CurHeaderWeight) {
432
432
// 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);
449
450
}
450
451
451
452
// / Clones the body of the loop L, putting it between \p InsertTop and \p
0 commit comments