+ }
+ return maxCommonTailLength;
+}
+
+/// RemoveBlocksWithHash - Remove all blocks with hash CurHash from
+/// MergePotentials, restoring branches at ends of blocks as appropriate.
+void BranchFolder::RemoveBlocksWithHash(unsigned CurHash,
+ MachineBasicBlock* SuccBB,
+ MachineBasicBlock* PredBB) {
+ MPIterator CurMPIter, B;
+ for (CurMPIter = prior(MergePotentials.end()), B = MergePotentials.begin();
+ CurMPIter->first==CurHash;
+ --CurMPIter) {
+ // Put the unconditional branch back, if we need one.
+ MachineBasicBlock *CurMBB = CurMPIter->second;
+ if (SuccBB && CurMBB != PredBB)
+ FixTail(CurMBB, SuccBB, TII);
+ if (CurMPIter==B)
+ break;
+ }
+ if (CurMPIter->first!=CurHash)
+ CurMPIter++;
+ MergePotentials.erase(CurMPIter, MergePotentials.end());
+}
+
+/// CreateCommonTailOnlyBlock - None of the blocks to be tail-merged consist
+/// only of the common tail. Create a block that does by splitting one.
+unsigned BranchFolder::CreateCommonTailOnlyBlock(MachineBasicBlock *&PredBB,
+ unsigned maxCommonTailLength) {
+ unsigned i, commonTailIndex;
+ unsigned TimeEstimate = ~0U;
+ for (i=0, commonTailIndex=0; i<SameTails.size(); i++) {
+ // Use PredBB if possible; that doesn't require a new branch.
+ if (SameTails[i].first->second==PredBB) {
+ commonTailIndex = i;
+ break;
+ }
+ // Otherwise, make a (fairly bogus) choice based on estimate of
+ // how long it will take the various blocks to execute.
+ unsigned t = EstimateRuntime(SameTails[i].first->second->begin(),
+ SameTails[i].second);
+ if (t<=TimeEstimate) {
+ TimeEstimate = t;
+ commonTailIndex = i;
+ }
+ }
+
+ MachineBasicBlock::iterator BBI = SameTails[commonTailIndex].second;
+ MachineBasicBlock *MBB = SameTails[commonTailIndex].first->second;
+
+ DOUT << "\nSplitting " << MBB->getNumber() << ", size " <<
+ maxCommonTailLength;
+
+ MachineBasicBlock *newMBB = SplitMBBAt(*MBB, BBI);
+ SameTails[commonTailIndex].first->second = newMBB;
+ SameTails[commonTailIndex].second = newMBB->begin();
+ // If we split PredBB, newMBB is the new predecessor.
+ if (PredBB==MBB)
+ PredBB = newMBB;
+
+ return commonTailIndex;