Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 609f4dd

Browse files
committed
[SimplifyCFG] propagate branch metadata when creating select
Unlike earlier similar fixes, we need to recalculate the branch weights in this case. Differential Revision: http://reviews.llvm.org/D19674 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@268550 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 00ed93a commit 609f4dd

File tree

2 files changed

+67
-22
lines changed

2 files changed

+67
-22
lines changed

lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -848,7 +848,7 @@ static void GetBranchWeights(TerminatorInst *TI,
848848
}
849849
}
850850

851-
/// Keep halving the weights until all can fit in uint32_t.
851+
/// Scale each weight so they all fit in uint32_t.
852852
static void FitWeights(MutableArrayRef<uint64_t> Weights) {
853853
uint64_t Max = *std::max_element(Weights.begin(), Weights.end());
854854
if (Max > UINT_MAX) {
@@ -2840,28 +2840,27 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
28402840
PBI->setSuccessor(1, OtherDest);
28412841

28422842
// Update branch weight for PBI.
2843+
MDBuilder MDB(BI->getContext());
28432844
uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight;
2844-
bool PredHasWeights =
2845-
PBI->extractProfMetadata(PredTrueWeight, PredFalseWeight);
2846-
bool SuccHasWeights =
2847-
BI->extractProfMetadata(SuccTrueWeight, SuccFalseWeight);
2848-
if (PredHasWeights && SuccHasWeights) {
2849-
uint64_t PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
2850-
uint64_t PredOther = PBIOp ?PredTrueWeight : PredFalseWeight;
2851-
uint64_t SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
2852-
uint64_t SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
2845+
uint64_t PredCommon, PredOther, SuccCommon, SuccOther;
2846+
bool HasWeights = PBI->extractProfMetadata(PredTrueWeight, PredFalseWeight);
2847+
if (HasWeights)
2848+
HasWeights = BI->extractProfMetadata(SuccTrueWeight, SuccFalseWeight);
2849+
if (HasWeights) {
2850+
PredCommon = PBIOp ? PredFalseWeight : PredTrueWeight;
2851+
PredOther = PBIOp ? PredTrueWeight : PredFalseWeight;
2852+
SuccCommon = BIOp ? SuccFalseWeight : SuccTrueWeight;
2853+
SuccOther = BIOp ? SuccTrueWeight : SuccFalseWeight;
28532854
// The weight to CommonDest should be PredCommon * SuccTotal +
28542855
// PredOther * SuccCommon.
28552856
// The weight to OtherDest should be PredOther * SuccOther.
28562857
uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther) +
28572858
PredOther * SuccCommon,
28582859
PredOther * SuccOther};
2859-
// Halve the weights if any of them cannot fit in an uint32_t
28602860
FitWeights(NewWeights);
28612861

28622862
PBI->setMetadata(LLVMContext::MD_prof,
2863-
MDBuilder(BI->getContext())
2864-
.createBranchWeights(NewWeights[0], NewWeights[1]));
2863+
MDB.createBranchWeights(NewWeights[0], NewWeights[1]));
28652864
}
28662865

28672866
// OtherDest may have phi nodes. If so, add an entry from PBI's
@@ -2880,9 +2879,24 @@ static bool SimplifyCondBranchToCondBranch(BranchInst *PBI, BranchInst *BI,
28802879
Value *PBIV = PN->getIncomingValue(PBBIdx);
28812880
if (BIV != PBIV) {
28822881
// Insert a select in PBI to pick the right value.
2883-
Value *NV = cast<SelectInst>
2882+
SelectInst *NV = cast<SelectInst>
28842883
(Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName() + ".mux"));
28852884
PN->setIncomingValue(PBBIdx, NV);
2885+
// Although the select has the same condition as PBI, the original branch
2886+
// weights for PBI do not apply to the new select because the select's
2887+
// 'logical' edges are incoming edges of the phi that is eliminated, not
2888+
// the outgoing edges of PBI.
2889+
if (HasWeights) {
2890+
// The weight to PredCommonDest should be PredCommon * SuccTotal.
2891+
// The weight to PredOtherDest should be PredOther * SuccCommon.
2892+
uint64_t NewWeights[2] = {PredCommon * (SuccCommon + SuccOther),
2893+
PredOther * SuccCommon};
2894+
2895+
FitWeights(NewWeights);
2896+
2897+
NV->setMetadata(LLVMContext::MD_prof,
2898+
MDB.createBranchWeights(NewWeights[0], NewWeights[1]));
2899+
}
28862900
}
28872901
}
28882902

test/Transforms/SimplifyCFG/preserve-branchweights.ll

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -412,22 +412,48 @@ return:
412412
ret i32 %retval.0
413413
}
414414

415-
; The 1st select should have branch weights equal to the 1st branch.
416-
; The 2nd select should have freshly calculated branch weights.
415+
; The selects should have freshly calculated branch weights.
417416

418417
define i32 @SimplifyCondBranchToCondBranch(i1 %cmpa, i1 %cmpb) {
419418
; CHECK-LABEL: @SimplifyCondBranchToCondBranch(
420419
; CHECK-NEXT: block1:
421-
; CHECK-NEXT: [[BRMERGE:%.*]] = or i1 %cmpb, %cmpa
422-
; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 %cmpb, i32 0, i32 2
423-
; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof !12
420+
; CHECK-NEXT: [[BRMERGE:%.*]] = or i1 %cmpa, %cmpb
421+
; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 %cmpa, i32 0, i32 2, !prof !12
422+
; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof !13
424423
; CHECK-NEXT: ret i32 [[OUTVAL]]
425424
;
426425
block1:
427-
br i1 %cmpb, label %block3, label %block2, !prof !0
426+
br i1 %cmpa, label %block3, label %block2, !prof !13
428427

429428
block2:
430-
br i1 %cmpa, label %block3, label %exit, !prof !2
429+
br i1 %cmpb, label %block3, label %exit, !prof !14
430+
431+
block3:
432+
%cowval = phi i32 [ 2, %block2 ], [ 0, %block1 ]
433+
br label %exit
434+
435+
exit:
436+
%outval = phi i32 [ %cowval, %block3 ], [ 1, %block2 ]
437+
ret i32 %outval
438+
}
439+
440+
; Swap the operands of the compares to verify that the weights update correctly.
441+
442+
define i32 @SimplifyCondBranchToCondBranchSwap(i1 %cmpa, i1 %cmpb) {
443+
; CHECK-LABEL: @SimplifyCondBranchToCondBranchSwap(
444+
; CHECK-NEXT: block1:
445+
; CHECK-NEXT: [[CMPA_NOT:%.*]] = xor i1 %cmpa, true
446+
; CHECK-NEXT: [[CMPB_NOT:%.*]] = xor i1 %cmpb, true
447+
; CHECK-NEXT: [[BRMERGE:%.*]] = or i1 [[CMPA_NOT]], [[CMPB_NOT]]
448+
; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 [[CMPA_NOT]], i32 0, i32 2, !prof !14
449+
; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof !15
450+
; CHECK-NEXT: ret i32 [[OUTVAL]]
451+
;
452+
block1:
453+
br i1 %cmpa, label %block2, label %block3, !prof !13
454+
455+
block2:
456+
br i1 %cmpb, label %exit, label %block3, !prof !14
431457

432458
block3:
433459
%cowval = phi i32 [ 2, %block2 ], [ 0, %block1 ]
@@ -452,6 +478,8 @@ exit:
452478
!10 = !{!"branch_weights", i32 672646, i32 21604207}
453479
!11 = !{!"branch_weights", i32 6960, i32 21597248}
454480
!12 = !{!"these_are_not_the_branch_weights_you_are_looking_for", i32 3, i32 5}
481+
!13 = !{!"branch_weights", i32 2, i32 3}
482+
!14 = !{!"branch_weights", i32 4, i32 7}
455483

456484
; CHECK: !0 = !{!"branch_weights", i32 5, i32 11}
457485
; CHECK: !1 = !{!"branch_weights", i32 1, i32 5}
@@ -467,5 +495,8 @@ exit:
467495
;; treat the weight as an unsigned integer.
468496
; CHECK: !10 = !{!"branch_weights", i32 112017436, i32 -735157296}
469497
; CHECK: !11 = !{!"branch_weights", i32 3, i32 5}
470-
; CHECK: !12 = !{!"branch_weights", i32 14, i32 10}
498+
; CHECK: !12 = !{!"branch_weights", i32 22, i32 12}
499+
; CHECK: !13 = !{!"branch_weights", i32 34, i32 21}
500+
; CHECK: !14 = !{!"branch_weights", i32 33, i32 14}
501+
; CHECK: !15 = !{!"branch_weights", i32 47, i32 8}
471502

0 commit comments

Comments
 (0)