Index: lib/Transforms/Utils/SimplifyCFG.cpp =================================================================== --- lib/Transforms/Utils/SimplifyCFG.cpp +++ lib/Transforms/Utils/SimplifyCFG.cpp @@ -2835,7 +2835,31 @@ PBI->setSuccessor(0, CommonDest); PBI->setSuccessor(1, OtherDest); - // Update branch weight for PBI. + // OtherDest may have phi nodes. If so, add an entry from PBI's + // block that are identical to the entries for BI's block. + AddPredecessorToBlock(OtherDest, PBI->getParent(), BB); + + // We know that the CommonDest already had an edge from PBI to + // it. If it has PHIs though, the PHIs may have different + // entries for BB and PBI's BB. If so, insert a select to make + // them agree. + PHINode *PN; + for (BasicBlock::iterator II = CommonDest->begin(); + (PN = dyn_cast(II)); ++II) { + Value *BIV = PN->getIncomingValueForBlock(BB); + unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent()); + Value *PBIV = PN->getIncomingValue(PBBIdx); + if (BIV != PBIV) { + // Insert a select in PBI to pick the right value. + // Transfer any original profile metadata from the branch to the select. + Value *NV = Builder.CreateSelect(PBICond, PBIV, BIV, + PBIV->getName() + ".mux", PBI); + PN->setIncomingValue(PBBIdx, NV); + } + } + + // Update branch weight for PBI. We do this after the select creation above + // because we want to preserve the original branch weights for the select. uint64_t PredTrueWeight, PredFalseWeight, SuccTrueWeight, SuccFalseWeight; bool PredHasWeights = PBI->extractProfMetadata(PredTrueWeight, PredFalseWeight); @@ -2860,28 +2884,6 @@ .createBranchWeights(NewWeights[0], NewWeights[1])); } - // OtherDest may have phi nodes. If so, add an entry from PBI's - // block that are identical to the entries for BI's block. - AddPredecessorToBlock(OtherDest, PBI->getParent(), BB); - - // We know that the CommonDest already had an edge from PBI to - // it. If it has PHIs though, the PHIs may have different - // entries for BB and PBI's BB. If so, insert a select to make - // them agree. - PHINode *PN; - for (BasicBlock::iterator II = CommonDest->begin(); - (PN = dyn_cast(II)); ++II) { - Value *BIV = PN->getIncomingValueForBlock(BB); - unsigned PBBIdx = PN->getBasicBlockIndex(PBI->getParent()); - Value *PBIV = PN->getIncomingValue(PBBIdx); - if (BIV != PBIV) { - // Insert a select in PBI to pick the right value. - Value *NV = cast - (Builder.CreateSelect(PBICond, PBIV, BIV, PBIV->getName() + ".mux")); - PN->setIncomingValue(PBBIdx, NV); - } - } - DEBUG(dbgs() << "INTO: " << *PBI->getParent()); DEBUG(dbgs() << *PBI->getParent()->getParent()); Index: test/Transforms/SimplifyCFG/preserve-branchweights.ll =================================================================== --- test/Transforms/SimplifyCFG/preserve-branchweights.ll +++ test/Transforms/SimplifyCFG/preserve-branchweights.ll @@ -419,7 +419,7 @@ ; CHECK-LABEL: @SimplifyCondBranchToCondBranch( ; CHECK-NEXT: block1: ; CHECK-NEXT: [[BRMERGE:%.*]] = or i1 %cmpb, %cmpa -; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 %cmpb, i32 0, i32 2 +; CHECK-NEXT: [[DOTMUX:%.*]] = select i1 %cmpb, i32 0, i32 2, !prof !11 ; CHECK-NEXT: [[OUTVAL:%.*]] = select i1 [[BRMERGE]], i32 [[DOTMUX]], i32 1, !prof !12 ; CHECK-NEXT: ret i32 [[OUTVAL]] ;