@@ -1618,27 +1618,39 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
16181618
16191619 auto *TI = BB->getTerminator ();
16201620
1621- SmallVector<BasicBlock *> SuccessorBlocks;
1622- for (auto *Succ : successors (BB))
1623- SuccessorBlocks.push_back (Succ);
1621+ SmallVector<BasicBlock *, 8 > SuccessorBBs;
1622+ for (auto *Succ : successors (BB)) {
1623+ BasicBlock::iterator SuccItr = Succ->begin ();
1624+ // If we find an unreachable instruction at the beginning of a basic block,
1625+ // we can still hoist instructions from the rest of the basic blocks.
1626+ if (isa<UnreachableInst>(*SuccItr))
1627+ continue ;
1628+ SuccessorBBs.push_back (Succ);
1629+ }
16241630
1625- // Sort successor blocks based on the number of instructions.
1626- // This is because we always want to iterate over instructions
1627- // of the smallest block.
1628- llvm::stable_sort (SuccessorBlocks, [](BasicBlock *BB1, BasicBlock *BB2) {
1629- return BB1->sizeWithoutDebug () < BB2->sizeWithoutDebug ();
1630- });
1631+ // Find the smallest BB because we always want to iterate over instructions
1632+ // of the smallest Successor.
1633+ auto *SmallestBB = *std::min_element (SuccessorBBs.begin (), SuccessorBBs.end (),
1634+ [](BasicBlock *BB1, BasicBlock *BB2) {
1635+ return BB1->size () < BB2->size ();
1636+ });
1637+ std::iter_swap (
1638+ SuccessorBBs.begin (),
1639+ std::find (SuccessorBBs.begin (), SuccessorBBs.end (), SmallestBB));
16311640
16321641 // The second of pair is a SkipFlags bitmask.
16331642 using SuccIterPair = std::pair<BasicBlock::iterator, unsigned >;
16341643 SmallVector<SuccIterPair, 8 > SuccIterPairs;
1635- for (auto *Succ : SuccessorBlocks ) {
1644+ for (auto *Succ : SuccessorBBs ) {
16361645 BasicBlock::iterator SuccItr = Succ->begin ();
16371646 if (isa<PHINode>(*SuccItr))
16381647 return false ;
16391648 SuccIterPairs.push_back (SuccIterPair (SuccItr, 0 ));
16401649 }
16411650
1651+ if (SuccIterPairs.size () < 2 )
1652+ return false ;
1653+
16421654 // Check if only hoisting terminators is allowed. This does not add new
16431655 // instructions to the hoist location.
16441656 if (EqTermsOnly) {
@@ -1656,14 +1668,6 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
16561668 // many instructions we skip, serving as a compilation time control as well as
16571669 // preventing excessive increase of life ranges.
16581670 unsigned NumSkipped = 0 ;
1659- // If we find an unreachable instruction at the beginning of a basic block, we
1660- // can still hoist instructions from the rest of the basic blocks.
1661- if (SuccIterPairs.size () > 2 ) {
1662- erase_if (SuccIterPairs,
1663- [](const auto &Pair) { return isa<UnreachableInst>(Pair.first ); });
1664- if (SuccIterPairs.size () < 2 )
1665- return false ;
1666- }
16671671
16681672 bool Changed = false ;
16691673 auto *SuccIterPairBegin = SuccIterPairs.begin ();
@@ -1697,14 +1701,25 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
16971701 auto OtherSuccIterPairRange =
16981702 iterator_range (SuccIterPairBegin, SuccIterPairs.end ());
16991703 Instruction *I1 = &*BB1ItrPair.first ;
1704+
1705+ // Skip debug info if it is not identical.
1706+ bool IdenticalDebugs = all_of (OtherSuccIterRange, [I1](auto &Iter) {
1707+ Instruction *I2 = &*Iter;
1708+ return I1->isIdenticalToWhenDefined (I2);
1709+ });
1710+ if (!IdenticalDebugs) {
1711+ while (isa<DbgInfoIntrinsic>(I1))
1712+ I1 = &*++BB1ItrPair.first ;
1713+ }
1714+
17001715 bool HasIdenticalInst = true ;
17011716
17021717 // Check if there are identical instructions in all other successors
17031718 for (auto &map : OtherSuccessorsHash) {
17041719 Instruction *I2 = map[getHash (I1)].first ;
17051720 // We might face with same hash values for different instructions.
17061721 // If that happens, ignore the instruction.
1707- if (!I2 || !I1->isIdenticalTo (I2)) {
1722+ if (!I2 || !I1->isIdenticalToWhenDefined (I2)) {
17081723 HasIdenticalInst = false ;
17091724 break ;
17101725 }
@@ -1720,7 +1735,7 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
17201735 SuccIterPair.second |= skippedInstrFlags (I);
17211736 }
17221737 }
1723- NumSkipped++ ;
1738+ ++NumSkipped ;
17241739 if (I1->isTerminator ())
17251740 return Changed;
17261741 ++BB1ItrPair.first ;
@@ -1733,7 +1748,7 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
17331748 OtherInsts.push_back (&*(SuccIterPair.first ));
17341749 } else {
17351750 for (auto &map : OtherSuccessorsHash)
1736- OtherInstrs .push_back (map[getHash (I1)].first );
1751+ OtherInsts .push_back (map[getHash (I1)].first );
17371752 }
17381753
17391754 // If we are hoisting the terminator instruction, don't move one (making a
@@ -1810,13 +1825,38 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
18101825 for (auto &map : OtherSuccessorsHash) {
18111826 Instruction *I2 = map[getHash (I1)].first ;
18121827 assert (I2 != I1);
1813- if (!I2->use_empty ())
1828+ // Update hashcode of all instructions using I2
1829+ if (!I2->use_empty ()) {
1830+ SmallVector<llvm::hash_code, 8 > PrevHashCodes;
1831+ SmallVector<llvm::Instruction *, 8 > PrevUsers;
1832+ // Once the uses of I1 are replaced, the hash value computed for
1833+ // those users are not valid anymore so we gather users and then
1834+ // recompute the hash codes for them. We need to do this only for
1835+ // the instructions located in the same block as I2 because we
1836+ // initially only hashed those instructions.
1837+ for (auto *user : I2->users ()) {
1838+ if (auto *I = dyn_cast<Instruction>(user)) {
1839+ if (I->getParent () != I2->getParent ())
1840+ continue ;
1841+ PrevHashCodes.push_back (getHash (I));
1842+ PrevUsers.push_back (I);
1843+ }
1844+ }
18141845 I2->replaceAllUsesWith (I1);
1846+ unsigned index = 0 ;
1847+ for (auto &PrevHash : PrevHashCodes) {
1848+ auto NewHash = getHash (PrevUsers[index]);
1849+ map.insert ({NewHash, map[PrevHash]});
1850+ map.erase (PrevHash);
1851+ index++;
1852+ }
1853+ }
18151854 I1->andIRFlags (I2);
18161855 combineMetadataForCSE (I1, I2, true );
18171856 // I1 and I2 are being combined into a single instruction. Its debug
18181857 // location is the merged locations of the original instructions.
18191858 I1->applyMergedLocation (I1->getDebugLoc (), I2->getDebugLoc ());
1859+ map.erase (getHash (I1));
18201860 I2->eraseFromParent ();
18211861 }
18221862 }
@@ -1832,10 +1872,11 @@ bool SimplifyCFGOpt::hoistCommonCodeFromSuccessors(BasicBlock *BB,
18321872 // We are about to skip over a pair of non-identical instructions. Record
18331873 // if any have characteristics that would prevent reordering instructions
18341874 // across them.
1875+ BB1ItrPair.first ++;
18351876 SkipFlagsBB1 |= skippedInstrFlags (I1);
18361877 if (SameLevelHoist) {
18371878 for (auto &SuccIterPair : OtherSuccIterPairRange) { // update flags
1838- Instruction *I = &*SuccIterPair.first ;
1879+ Instruction *I = &*SuccIterPair.first ++ ;
18391880 SuccIterPair.second |= skippedInstrFlags (I);
18401881 }
18411882 }
@@ -1857,11 +1898,8 @@ bool SimplifyCFGOpt::hoistSuccIdenticalTerminatorToSwitchOrIf(
18571898 // Use only for an if statement.
18581899 auto *I2 = *OtherSuccTIs.begin ();
18591900 auto *BB2 = I2->getParent ();
1860- if (BI) {
1901+ if (BI)
18611902 assert (OtherSuccTIs.size () == 1 );
1862- assert (BI->getSuccessor (0 ) == I1->getParent ());
1863- assert (BI->getSuccessor (1 ) == I2->getParent ());
1864- }
18651903
18661904 // In the case of an if statement, we try to hoist an invoke.
18671905 // FIXME: Can we define a safety predicate for CallBr?
@@ -1881,6 +1919,7 @@ bool SimplifyCFGOpt::hoistSuccIdenticalTerminatorToSwitchOrIf(
18811919 Value *BB2V = PN.getIncomingValueForBlock (OtherSuccTI->getParent ());
18821920 if (BB1V == BB2V)
18831921 continue ;
1922+
18841923 // In the case of an if statement, check for
18851924 // passingValueIsAlwaysUndefined here because we would rather eliminate
18861925 // undefined control flow then converting it to a select.
@@ -1952,16 +1991,20 @@ bool SimplifyCFGOpt::hoistSuccIdenticalTerminatorToSwitchOrIf(
19521991 }
19531992 }
19541993 }
1994+
19551995 SmallVector<DominatorTree::UpdateType, 4 > Updates;
1996+
19561997 // Update any PHI nodes in our new successors.
19571998 for (BasicBlock *Succ : successors (BB1)) {
19581999 AddPredecessorToBlock (Succ, TIParent, BB1);
19592000 if (DTU)
19602001 Updates.push_back ({DominatorTree::Insert, TIParent, Succ});
19612002 }
2003+
19622004 if (DTU)
19632005 for (BasicBlock *Succ : successors (TI))
19642006 Updates.push_back ({DominatorTree::Delete, TIParent, Succ});
2007+
19652008 EraseTerminatorAndDCECond (TI);
19662009 if (DTU)
19672010 DTU->applyUpdates (Updates);
@@ -3713,7 +3756,7 @@ static bool FoldTwoEntryPHINode(PHINode *PN, const TargetTransformInfo &TTI,
37133756 // Change the PHI node into a select instruction.
37143757 Value *TrueVal = PN->getIncomingValueForBlock (IfTrue);
37153758 Value *FalseVal = PN->getIncomingValueForBlock (IfFalse);
3716-
3759+
37173760 Value *Sel = Builder.CreateSelect (IfCond, TrueVal, FalseVal, " " , DomBI);
37183761 PN->replaceAllUsesWith (Sel);
37193762 Sel->takeName (PN);
0 commit comments