diff --git a/llvm/lib/Transforms/Scalar/JumpThreading.cpp b/llvm/lib/Transforms/Scalar/JumpThreading.cpp --- a/llvm/lib/Transforms/Scalar/JumpThreading.cpp +++ b/llvm/lib/Transforms/Scalar/JumpThreading.cpp @@ -2759,8 +2759,24 @@ // Create a conditional branch and update PHI nodes. auto *BI = BranchInst::Create(NewBB, BB, SI->getCondition(), Pred); BI->applyMergedLocation(PredTerm->getDebugLoc(), SI->getDebugLoc()); + BI->copyMetadata(*SI, {LLVMContext::MD_prof}); SIUse->setIncomingValue(Idx, SI->getFalseValue()); SIUse->addIncoming(SI->getTrueValue(), NewBB); + // Set the block frequency of NewBB. + if (HasProfileData) { + uint64_t TrueWeight, FalseWeight; + if (extractBranchWeights(*SI, TrueWeight, FalseWeight) && + (TrueWeight + FalseWeight) != 0) { + SmallVector BP; + BP.emplace_back(BranchProbability(TrueWeight, TrueWeight + FalseWeight)); + BP.emplace_back(BranchProbability(FalseWeight, TrueWeight + FalseWeight)); + BPI->setEdgeProbability(Pred, BP); + } + + auto NewBBFreq = + BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, NewBB); + BFI->setBlockFreq(NewBB, NewBBFreq.getFrequency()); + } // The select is now dead. SI->eraseFromParent(); diff --git a/llvm/test/Transforms/JumpThreading/select.ll b/llvm/test/Transforms/JumpThreading/select.ll --- a/llvm/test/Transforms/JumpThreading/select.ll +++ b/llvm/test/Transforms/JumpThreading/select.ll @@ -274,7 +274,7 @@ ; CHECK: cond.false: ; CHECK-NEXT: [[ADD:%.*]] = fadd double [[X]], [[Y]] ; CHECK-NEXT: [[CMP1:%.*]] = fcmp ogt double [[ADD]], 1.000000e+01 -; CHECK-NEXT: br i1 [[CMP1]], label [[COND_END4]], label [[IF_THEN:%.*]] +; CHECK-NEXT: br i1 [[CMP1]], label [[COND_END4]], label [[IF_THEN:%.*]], !prof !0 ; CHECK: cond.end4: ; CHECK-NEXT: [[COND5:%.*]] = phi double [ [[SUB]], [[ENTRY:%.*]] ], [ [[ADD]], [[COND_FALSE]] ] ; CHECK-NEXT: [[CMP6:%.*]] = fcmp oeq double [[COND5]], 0.000000e+00 @@ -293,7 +293,7 @@ cond.false: ; preds = %entry %add = fadd double %x, %y %cmp1 = fcmp ogt double %add, 1.000000e+01 - %add. = select i1 %cmp1, double %add, double 0.000000e+00 + %add. = select i1 %cmp1, double %add, double 0.000000e+00, !prof !0 br label %cond.end4 cond.end4: ; preds = %entry, %cond.false @@ -311,7 +311,7 @@ } -define void @unfold2(i32 %x, i32 %y) nounwind { +define void @unfold2(i32 %x, i32 %y) nounwind !prof !1 { ; CHECK-LABEL: @unfold2( ; CHECK-NEXT: entry: ; CHECK-NEXT: [[SUB:%.*]] = sub nsw i32 [[X:%.*]], [[Y:%.*]] @@ -320,7 +320,7 @@ ; CHECK: cond.false: ; CHECK-NEXT: [[ADD:%.*]] = add nsw i32 [[X]], [[Y]] ; CHECK-NEXT: [[CMP1:%.*]] = icmp sgt i32 [[ADD]], 10 -; CHECK-NEXT: br i1 [[CMP1]], label [[IF_THEN:%.*]], label [[COND_END4:%.*]] +; CHECK-NEXT: br i1 [[CMP1]], label [[IF_THEN:%.*]], label [[COND_END4:%.*]], !prof !0 ; CHECK: cond.end4: ; CHECK-NEXT: [[COND5:%.*]] = phi i32 [ [[ADD]], [[COND_FALSE]] ] ; CHECK-NEXT: [[CMP6:%.*]] = icmp eq i32 [[COND5]], 0 @@ -339,7 +339,7 @@ cond.false: ; preds = %entry %add = add nsw i32 %x, %y %cmp1 = icmp sgt i32 %add, 10 - %add. = select i1 %cmp1, i32 0, i32 %add + %add. = select i1 %cmp1, i32 0, i32 %add, !prof !0 br label %cond.end4 cond.end4: ; preds = %entry, %cond.false @@ -652,3 +652,6 @@ %v1 = select i1 %v, i32 %s, i32 42 ret i32 %v1 } + +!0 = !{!"branch_weights", i32 2146410443, i32 1073205} +!1 = !{!"function_entry_count", i64 1984}