+bool BranchFolder::TailMergeBlocks(MachineFunction &MF) {
+
+ if (!EnableTailMerge) return false;
+
+ MadeChange = false;
+
+ // First find blocks with no successors.
+ MergePotentials.clear();
+ for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
+ if (I->succ_empty())
+ MergePotentials.push_back(std::make_pair(HashEndOfMBB(I, 2U), I));
+ }
+ // See if we can do any tail merging on those.
+ if (MergePotentials.size() < TailMergeThreshold)
+ MadeChange |= TryMergeBlocks(NULL, NULL);
+
+ // Look at blocks (IBB) with multiple predecessors (PBB).
+ // We change each predecessor to a canonical form, by
+ // (1) temporarily removing any unconditional branch from the predecessor
+ // to IBB, and
+ // (2) alter conditional branches so they branch to the other block
+ // not IBB; this may require adding back an unconditional branch to IBB
+ // later, where there wasn't one coming in. E.g.
+ // Bcc IBB
+ // fallthrough to QBB
+ // here becomes
+ // Bncc QBB
+ // with a conceptual B to IBB after that, which never actually exists.
+ // With those changes, we see whether the predecessors' tails match,
+ // and merge them if so. We change things out of canonical form and
+ // back to the way they were later in the process. (OptimizeBranches
+ // would undo some of this, but we can't use it, because we'd get into
+ // a compile-time infinite loop repeatedly doing and undoing the same
+ // transformations.)
+
+ for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
+ if (!I->succ_empty() && I->pred_size() >= 2 &&
+ I->pred_size() < TailMergeThreshold) {
+ MachineBasicBlock *IBB = I;
+ MachineBasicBlock *PredBB = prior(I);
+ MergePotentials.clear();
+ for (MachineBasicBlock::pred_iterator P = I->pred_begin(),
+ E2 = I->pred_end();
+ P != E2; ++P) {
+ MachineBasicBlock* PBB = *P;
+ // Skip blocks that loop to themselves, can't tail merge these.
+ if (PBB==IBB)
+ continue;
+ MachineBasicBlock *TBB = 0, *FBB = 0;
+ std::vector<MachineOperand> Cond;
+ if (!TII->AnalyzeBranch(*PBB, TBB, FBB, Cond)) {
+ // Failing case: IBB is the target of a cbr, and
+ // we cannot reverse the branch.
+ std::vector<MachineOperand> NewCond(Cond);
+ if (Cond.size() && TBB==IBB) {
+ if (TII->ReverseBranchCondition(NewCond))
+ continue;
+ // This is the QBB case described above
+ if (!FBB)
+ FBB = next(MachineFunction::iterator(PBB));
+ }
+ // Failing case: the only way IBB can be reached from PBB is via
+ // exception handling. Happens for landing pads. Would be nice
+ // to have a bit in the edge so we didn't have to do all this.
+ if (IBB->isLandingPad()) {
+ MachineFunction::iterator IP = PBB; IP++;
+ MachineBasicBlock* PredNextBB = NULL;
+ if (IP!=MF.end())
+ PredNextBB = IP;
+ if (TBB==NULL) {
+ if (IBB!=PredNextBB) // fallthrough
+ continue;
+ } else if (FBB) {
+ if (TBB!=IBB && FBB!=IBB) // cbr then ubr
+ continue;
+ } else if (Cond.size() == 0) {
+ if (TBB!=IBB) // ubr
+ continue;
+ } else {
+ if (TBB!=IBB && IBB!=PredNextBB) // cbr
+ continue;
+ }
+ }
+ // Remove the unconditional branch at the end, if any.
+ if (TBB && (Cond.size()==0 || FBB)) {
+ TII->RemoveBranch(*PBB);
+ if (Cond.size())
+ // reinsert conditional branch only, for now
+ TII->InsertBranch(*PBB, (TBB==IBB) ? FBB : TBB, 0, NewCond);
+ }
+ MergePotentials.push_back(std::make_pair(HashEndOfMBB(PBB, 1U), *P));
+ }
+ }
+ if (MergePotentials.size() >= 2)
+ MadeChange |= TryMergeBlocks(I, PredBB);
+ // Reinsert an unconditional branch if needed.
+ // The 1 below can be either an original single predecessor, or a result
+ // of removing blocks in TryMergeBlocks.
+ PredBB = prior(I); // this may have been changed in TryMergeBlocks
+ if (MergePotentials.size()==1 &&
+ (MergePotentials.begin())->second != PredBB)
+ FixTail((MergePotentials.begin())->second, I, TII);
+ }
+ }
+ return MadeChange;
+}