@@ -331,7 +331,7 @@ static void buildPartialUnswitchConditionalBranch(
331331static void buildPartialInvariantUnswitchConditionalBranch (
332332 BasicBlock &BB, ArrayRef<Value *> ToDuplicate, bool Direction,
333333 BasicBlock &UnswitchedSucc, BasicBlock &NormalSucc, Loop &L,
334- MemorySSAUpdater *MSSAU) {
334+ MemorySSAUpdater *MSSAU, const BranchInst &OriginalBranch ) {
335335 ValueToValueMapTy VMap;
336336 for (auto *Val : reverse (ToDuplicate)) {
337337 Instruction *Inst = cast<Instruction>(Val);
@@ -371,8 +371,17 @@ static void buildPartialInvariantUnswitchConditionalBranch(
371371 IRBuilder<> IRB (&BB);
372372 IRB.SetCurrentDebugLocation (DebugLoc::getCompilerGenerated ());
373373 Value *Cond = VMap[ToDuplicate[0 ]];
374- IRB.CreateCondBr (Cond, Direction ? &UnswitchedSucc : &NormalSucc,
375- Direction ? &NormalSucc : &UnswitchedSucc);
374+ auto *ProfData =
375+ !ProfcheckDisableMetadataFixes &&
376+ ToDuplicate[0 ] == skipTrivialSelect (OriginalBranch.getCondition ())
377+ ? OriginalBranch.getMetadata (LLVMContext::MD_prof)
378+ : nullptr ;
379+ auto *BR =
380+ IRB.CreateCondBr (Cond, Direction ? &UnswitchedSucc : &NormalSucc,
381+ Direction ? &NormalSucc : &UnswitchedSucc, ProfData);
382+ if (!ProfData)
383+ setExplicitlyUnknownBranchWeightsIfProfiled (
384+ *BR, *BR->getParent ()->getParent (), DEBUG_TYPE);
376385}
377386
378387// / Rewrite the PHI nodes in an unswitched loop exit basic block.
@@ -2509,7 +2518,7 @@ static void unswitchNontrivialInvariants(
25092518 // the branch in the split block.
25102519 if (PartiallyInvariant)
25112520 buildPartialInvariantUnswitchConditionalBranch (
2512- *SplitBB, Invariants, Direction, *ClonedPH, *LoopPH, L, MSSAU);
2521+ *SplitBB, Invariants, Direction, *ClonedPH, *LoopPH, L, MSSAU, *BI );
25132522 else {
25142523 buildPartialUnswitchConditionalBranch (
25152524 *SplitBB, Invariants, Direction, *ClonedPH, *LoopPH,
0 commit comments