/// /// This method determines whether the two coalescing candidates can be merged. /// In order to be merged, all instructions must be able to /// 1. Move to the beginning of the SourceRegion.BranchTargetBlock; /// 2. Move to the end of the TargetRegion.BranchBlock. /// Merging involves moving the instructions in the /// TargetRegion.BranchTargetBlock (also SourceRegion.BranchBlock). /// /// This function first try to move instructions from the /// TargetRegion.BranchTargetBlock down, to the beginning of the /// SourceRegion.BranchTargetBlock. This is not possible if any register defined /// in TargetRegion.BranchTargetBlock is used in a PHI node in the /// SourceRegion.BranchTargetBlock. In this case, check whether the statement /// can be moved up, to the end of the TargetRegion.BranchBlock (immediately /// before the branch statement). If it cannot move, then these blocks cannot /// be merged. /// /// Note that there is no analysis for moving instructions past the fall-through /// blocks because they are confirmed to be empty. An assert is thrown if they /// are not. /// /// \param[in] SourceRegion The candidate to move statements from /// \param[in] TargetRegion The candidate to move statements to /// \return true if all instructions in SourceRegion.BranchBlock can be merged /// into a block in TargetRegion, false otherwise. /// bool PPCBranchCoalescing::canMerge(CoalescingCandidateInfo &SourceRegion, CoalescingCandidateInfo &TargetRegion) const { if (!validateCandidates(SourceRegion, TargetRegion)) return false; // Walk through PHI nodes first and see if they force the merge into the // SourceRegion.BranchTargetBlock. for (MachineBasicBlock::iterator I = SourceRegion.BranchBlock->instr_begin(), E = SourceRegion.BranchBlock->getFirstNonPHI(); I != E; ++I) { for (auto &Def : I->defs()) for (auto &Use : MRI->use_instructions(Def.getReg())) { if (Use.isPHI() && Use.getParent() == SourceRegion.BranchTargetBlock) { DEBUG(dbgs() << "PHI " << *I << " defines register used in another " "PHI within branch target block -- can't merge\n"); NumPHINotMoved++; return false; } if (Use.getParent() == SourceRegion.BranchBlock) { DEBUG(dbgs() << "PHI " << *I << " defines register used in this " "block -- all must move down\n"); SourceRegion.MustMoveDown = true; } } } // Walk through the MI to see if they should be merged into // TargetRegion.BranchBlock (up) or SourceRegion.BranchTargetBlock (down) for (MachineBasicBlock::iterator I = SourceRegion.BranchBlock->getFirstNonPHI(), E = SourceRegion.BranchBlock->end(); I != E; ++I) { if (!canMoveToBeginning(*I, *SourceRegion.BranchTargetBlock)) { DEBUG(dbgs() << "Instruction " << *I << " cannot move down - must move up!\n"); SourceRegion.MustMoveUp = true; } if (!canMoveToEnd(*I, *TargetRegion.BranchBlock)) { DEBUG(dbgs() << "Instruction " << *I << " cannot move up - must move down!\n"); SourceRegion.MustMoveDown = true; } } return (SourceRegion.MustMoveUp && SourceRegion.MustMoveDown) ? false : true; }