+
+ // Look through all the pairs of blocks that have the same hash as this
+ // one, and find the pair that has the largest number of instructions in
+ // common.
+ // Since instructions may get combined later (e.g. single stores into
+ // store multiple) this measure is not particularly accurate.
+ MachineBasicBlock::iterator BBI1, BBI2;
+
+ unsigned FoundI = ~0U, FoundJ = ~0U;
+ unsigned maxCommonTailLength = 0U;
+ for (int i = MergePotentials.size()-1;
+ i != -1 && MergePotentials[i].first == CurHash; --i) {
+ for (int j = i-1;
+ j != -1 && MergePotentials[j].first == CurHash; --j) {
+ MachineBasicBlock::iterator TrialBBI1, TrialBBI2;
+ unsigned CommonTailLen = ComputeCommonTailLength(
+ MergePotentials[i].second,
+ MergePotentials[j].second,
+ TrialBBI1, TrialBBI2);
+ if (CommonTailLen >= minCommonTailLength &&
+ CommonTailLen > maxCommonTailLength) {
+ FoundI = i;
+ FoundJ = j;
+ maxCommonTailLength = CommonTailLen;
+ BBI1 = TrialBBI1;
+ BBI2 = TrialBBI2;
+ }
+ }
+ }
+
+ // If we didn't find any pair that has at least minCommonTailLength
+ // instructions in common, bail out. All entries with this
+ // hash code can go away now.
+ if (FoundI == ~0U) {
+ for (int i = MergePotentials.size()-1;
+ i != -1 && MergePotentials[i].first == CurHash; --i) {
+ // Put the unconditional branch back, if we need one.
+ CurMBB = MergePotentials[i].second;
+ if (SuccBB && CurMBB != PredBB)
+ FixTail(CurMBB, SuccBB, TII);
+ MergePotentials.pop_back();
+ }
+ continue;
+ }
+
+ // Otherwise, move the block(s) to the right position(s). So that
+ // BBI1/2 will be valid, the last must be I and the next-to-last J.
+ if (FoundI != MergePotentials.size()-1)
+ std::swap(MergePotentials[FoundI], *(MergePotentials.end()-1));
+ if (FoundJ != MergePotentials.size()-2)
+ std::swap(MergePotentials[FoundJ], *(MergePotentials.end()-2));
+
+ CurMBB = (MergePotentials.end()-1)->second;
+ MachineBasicBlock *MBB2 = (MergePotentials.end()-2)->second;
+
+ // If neither block is the entire common tail, split the tail of one block
+ // to make it redundant with the other tail. Also, we cannot jump to the
+ // entry block, so if one block is the entry block, split the other one.
+ MachineBasicBlock *Entry = CurMBB->getParent()->begin();
+ if (CurMBB->begin() == BBI1 && CurMBB != Entry)
+ ; // CurMBB is common tail
+ else if (MBB2->begin() == BBI2 && MBB2 != Entry)
+ ; // MBB2 is common tail
+ else {
+ if (0) { // Enable this to disable partial tail merges.
+ MergePotentials.pop_back();
+ continue;
+ }
+
+ // Decide whether we want to split CurMBB or MBB2.
+ if (ShouldSplitFirstBlock(CurMBB, BBI1, MBB2, BBI2, TII, PredBB)) {
+ CurMBB = SplitMBBAt(*CurMBB, BBI1);
+ BBI1 = CurMBB->begin();
+ MergePotentials.back().second = CurMBB;
+ } else {
+ MBB2 = SplitMBBAt(*MBB2, BBI2);
+ BBI2 = MBB2->begin();
+ (MergePotentials.end()-2)->second = MBB2;
+ }
+ }
+
+ if (MBB2->begin() == BBI2 && MBB2 != Entry) {
+ // Hack the end off CurMBB, making it jump to MBBI@ instead.
+ ReplaceTailWithBranchTo(BBI1, MBB2);
+ // This modifies CurMBB, so remove it from the worklist.
+ MergePotentials.pop_back();
+ } else {
+ assert(CurMBB->begin() == BBI1 && CurMBB != Entry &&
+ "Didn't split block correctly?");
+ // Hack the end off MBB2, making it jump to CurMBB instead.
+ ReplaceTailWithBranchTo(BBI2, CurMBB);
+ // This modifies MBB2, so remove it from the worklist.
+ MergePotentials.erase(MergePotentials.end()-2);
+ }
+ MadeChange = true;