Path: blob/main/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCBranchCoalescing.cpp
35269 views
//===-- CoalesceBranches.cpp - Coalesce blocks with the same condition ---===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//7///8/// \file9/// Coalesce basic blocks guarded by the same branch condition into a single10/// basic block.11///12//===----------------------------------------------------------------------===//1314#include "PPC.h"15#include "llvm/ADT/Statistic.h"16#include "llvm/CodeGen/MachineDominators.h"17#include "llvm/CodeGen/MachineFunctionPass.h"18#include "llvm/CodeGen/MachinePostDominators.h"19#include "llvm/CodeGen/MachineRegisterInfo.h"20#include "llvm/CodeGen/Passes.h"21#include "llvm/CodeGen/TargetFrameLowering.h"22#include "llvm/CodeGen/TargetInstrInfo.h"23#include "llvm/CodeGen/TargetSubtargetInfo.h"24#include "llvm/InitializePasses.h"25#include "llvm/Support/Debug.h"2627using namespace llvm;2829#define DEBUG_TYPE "ppc-branch-coalescing"3031STATISTIC(NumBlocksCoalesced, "Number of blocks coalesced");32STATISTIC(NumPHINotMoved, "Number of PHI Nodes that cannot be merged");33STATISTIC(NumBlocksNotCoalesced, "Number of blocks not coalesced");3435//===----------------------------------------------------------------------===//36// PPCBranchCoalescing37//===----------------------------------------------------------------------===//38///39/// Improve scheduling by coalescing branches that depend on the same condition.40/// This pass looks for blocks that are guarded by the same branch condition41/// and attempts to merge the blocks together. Such opportunities arise from42/// the expansion of select statements in the IR.43///44/// This pass does not handle implicit operands on branch statements. In order45/// to run on targets that use implicit operands, changes need to be made in the46/// canCoalesceBranch and canMerge methods.47///48/// Example: the following LLVM IR49///50/// %test = icmp eq i32 %x 051/// %tmp1 = select i1 %test, double %a, double 2.000000e-0352/// %tmp2 = select i1 %test, double %b, double 5.000000e-0353///54/// expands to the following machine code:55///56/// %bb.0: derived from LLVM BB %entry57/// liveins: %f1 %f3 %x658/// <SNIP1>59/// %0 = COPY %f1; F8RC:%060/// %5 = CMPLWI killed %4, 0; CRRC:%5 GPRC:%461/// %8 = LXSDX %zero8, killed %7, implicit %rm;62/// mem:LD8[ConstantPool] F8RC:%8 G8RC:%763/// BCC 76, %5, <%bb.2>; CRRC:%564/// Successors according to CFG: %bb.1(?%) %bb.2(?%)65///66/// %bb.1: derived from LLVM BB %entry67/// Predecessors according to CFG: %bb.068/// Successors according to CFG: %bb.2(?%)69///70/// %bb.2: derived from LLVM BB %entry71/// Predecessors according to CFG: %bb.0 %bb.172/// %9 = PHI %8, <%bb.1>, %0, <%bb.0>;73/// F8RC:%9,%8,%074/// <SNIP2>75/// BCC 76, %5, <%bb.4>; CRRC:%576/// Successors according to CFG: %bb.3(?%) %bb.4(?%)77///78/// %bb.3: derived from LLVM BB %entry79/// Predecessors according to CFG: %bb.280/// Successors according to CFG: %bb.4(?%)81///82/// %bb.4: derived from LLVM BB %entry83/// Predecessors according to CFG: %bb.2 %bb.384/// %13 = PHI %12, <%bb.3>, %2, <%bb.2>;85/// F8RC:%13,%12,%286/// <SNIP3>87/// BLR8 implicit %lr8, implicit %rm, implicit %f188///89/// When this pattern is detected, branch coalescing will try to collapse90/// it by moving code in %bb.2 to %bb.0 and/or %bb.4 and removing %bb.3.91///92/// If all conditions are meet, IR should collapse to:93///94/// %bb.0: derived from LLVM BB %entry95/// liveins: %f1 %f3 %x696/// <SNIP1>97/// %0 = COPY %f1; F8RC:%098/// %5 = CMPLWI killed %4, 0; CRRC:%5 GPRC:%499/// %8 = LXSDX %zero8, killed %7, implicit %rm;100/// mem:LD8[ConstantPool] F8RC:%8 G8RC:%7101/// <SNIP2>102/// BCC 76, %5, <%bb.4>; CRRC:%5103/// Successors according to CFG: %bb.1(0x2aaaaaaa / 0x80000000 = 33.33%)104/// %bb.4(0x55555554 / 0x80000000 = 66.67%)105///106/// %bb.1: derived from LLVM BB %entry107/// Predecessors according to CFG: %bb.0108/// Successors according to CFG: %bb.4(0x40000000 / 0x80000000 = 50.00%)109///110/// %bb.4: derived from LLVM BB %entry111/// Predecessors according to CFG: %bb.0 %bb.1112/// %9 = PHI %8, <%bb.1>, %0, <%bb.0>;113/// F8RC:%9,%8,%0114/// %13 = PHI %12, <%bb.1>, %2, <%bb.0>;115/// F8RC:%13,%12,%2116/// <SNIP3>117/// BLR8 implicit %lr8, implicit %rm, implicit %f1118///119/// Branch Coalescing does not split blocks, it moves everything in the same120/// direction ensuring it does not break use/definition semantics.121///122/// PHI nodes and its corresponding use instructions are moved to its successor123/// block if there are no uses within the successor block PHI nodes. PHI124/// node ordering cannot be assumed.125///126/// Non-PHI can be moved up to the predecessor basic block or down to the127/// successor basic block following any PHI instructions. Whether it moves128/// up or down depends on whether the register(s) defined in the instructions129/// are used in current block or in any PHI instructions at the beginning of130/// the successor block.131132namespace {133134class PPCBranchCoalescing : public MachineFunctionPass {135struct CoalescingCandidateInfo {136MachineBasicBlock *BranchBlock; // Block containing the branch137MachineBasicBlock *BranchTargetBlock; // Block branched to138MachineBasicBlock *FallThroughBlock; // Fall-through if branch not taken139SmallVector<MachineOperand, 4> Cond;140bool MustMoveDown;141bool MustMoveUp;142143CoalescingCandidateInfo();144void clear();145};146147MachineDominatorTree *MDT;148MachinePostDominatorTree *MPDT;149const TargetInstrInfo *TII;150MachineRegisterInfo *MRI;151152void initialize(MachineFunction &F);153bool canCoalesceBranch(CoalescingCandidateInfo &Cand);154bool identicalOperands(ArrayRef<MachineOperand> OperandList1,155ArrayRef<MachineOperand> OperandList2) const;156bool validateCandidates(CoalescingCandidateInfo &SourceRegion,157CoalescingCandidateInfo &TargetRegion) const;158159public:160static char ID;161162PPCBranchCoalescing() : MachineFunctionPass(ID) {163initializePPCBranchCoalescingPass(*PassRegistry::getPassRegistry());164}165166void getAnalysisUsage(AnalysisUsage &AU) const override {167AU.addRequired<MachineDominatorTreeWrapperPass>();168AU.addRequired<MachinePostDominatorTreeWrapperPass>();169MachineFunctionPass::getAnalysisUsage(AU);170}171172StringRef getPassName() const override { return "Branch Coalescing"; }173174bool mergeCandidates(CoalescingCandidateInfo &SourceRegion,175CoalescingCandidateInfo &TargetRegion);176bool canMoveToBeginning(const MachineInstr &MI,177const MachineBasicBlock &MBB) const;178bool canMoveToEnd(const MachineInstr &MI,179const MachineBasicBlock &MBB) const;180bool canMerge(CoalescingCandidateInfo &SourceRegion,181CoalescingCandidateInfo &TargetRegion) const;182void moveAndUpdatePHIs(MachineBasicBlock *SourceRegionMBB,183MachineBasicBlock *TargetRegionMBB);184bool runOnMachineFunction(MachineFunction &MF) override;185};186} // End anonymous namespace.187188char PPCBranchCoalescing::ID = 0;189/// createPPCBranchCoalescingPass - returns an instance of the Branch Coalescing190/// Pass191FunctionPass *llvm::createPPCBranchCoalescingPass() {192return new PPCBranchCoalescing();193}194195INITIALIZE_PASS_BEGIN(PPCBranchCoalescing, DEBUG_TYPE,196"Branch Coalescing", false, false)197INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass)198INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTreeWrapperPass)199INITIALIZE_PASS_END(PPCBranchCoalescing, DEBUG_TYPE, "Branch Coalescing",200false, false)201202PPCBranchCoalescing::CoalescingCandidateInfo::CoalescingCandidateInfo()203: BranchBlock(nullptr), BranchTargetBlock(nullptr),204FallThroughBlock(nullptr), MustMoveDown(false), MustMoveUp(false) {}205206void PPCBranchCoalescing::CoalescingCandidateInfo::clear() {207BranchBlock = nullptr;208BranchTargetBlock = nullptr;209FallThroughBlock = nullptr;210Cond.clear();211MustMoveDown = false;212MustMoveUp = false;213}214215void PPCBranchCoalescing::initialize(MachineFunction &MF) {216MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();217MPDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();218TII = MF.getSubtarget().getInstrInfo();219MRI = &MF.getRegInfo();220}221222///223/// Analyze the branch statement to determine if it can be coalesced. This224/// method analyses the branch statement for the given candidate to determine225/// if it can be coalesced. If the branch can be coalesced, then the226/// BranchTargetBlock and the FallThroughBlock are recorded in the specified227/// Candidate.228///229///\param[in,out] Cand The coalescing candidate to analyze230///\return true if and only if the branch can be coalesced, false otherwise231///232bool PPCBranchCoalescing::canCoalesceBranch(CoalescingCandidateInfo &Cand) {233LLVM_DEBUG(dbgs() << "Determine if branch block "234<< Cand.BranchBlock->getNumber() << " can be coalesced:");235MachineBasicBlock *FalseMBB = nullptr;236237if (TII->analyzeBranch(*Cand.BranchBlock, Cand.BranchTargetBlock, FalseMBB,238Cand.Cond)) {239LLVM_DEBUG(dbgs() << "TII unable to Analyze Branch - skip\n");240return false;241}242243for (auto &I : Cand.BranchBlock->terminators()) {244LLVM_DEBUG(dbgs() << "Looking at terminator : " << I << "\n");245if (!I.isBranch())246continue;247248// The analyzeBranch method does not include any implicit operands.249// This is not an issue on PPC but must be handled on other targets.250// For this pass to be made target-independent, the analyzeBranch API251// need to be updated to support implicit operands and there would252// need to be a way to verify that any implicit operands would not be253// clobbered by merging blocks. This would include identifying the254// implicit operands as well as the basic block they are defined in.255// This could be done by changing the analyzeBranch API to have it also256// record and return the implicit operands and the blocks where they are257// defined. Alternatively, the BranchCoalescing code would need to be258// extended to identify the implicit operands. The analysis in canMerge259// must then be extended to prove that none of the implicit operands are260// changed in the blocks that are combined during coalescing.261if (I.getNumOperands() != I.getNumExplicitOperands()) {262LLVM_DEBUG(dbgs() << "Terminator contains implicit operands - skip : "263<< I << "\n");264return false;265}266}267268if (Cand.BranchBlock->isEHPad() || Cand.BranchBlock->hasEHPadSuccessor()) {269LLVM_DEBUG(dbgs() << "EH Pad - skip\n");270return false;271}272273if (Cand.BranchBlock->mayHaveInlineAsmBr()) {274LLVM_DEBUG(dbgs() << "Inline Asm Br - skip\n");275return false;276}277278// For now only consider triangles (i.e, BranchTargetBlock is set,279// FalseMBB is null, and BranchTargetBlock is a successor to BranchBlock)280if (!Cand.BranchTargetBlock || FalseMBB ||281!Cand.BranchBlock->isSuccessor(Cand.BranchTargetBlock)) {282LLVM_DEBUG(dbgs() << "Does not form a triangle - skip\n");283return false;284}285286// Ensure there are only two successors287if (Cand.BranchBlock->succ_size() != 2) {288LLVM_DEBUG(dbgs() << "Does not have 2 successors - skip\n");289return false;290}291292// The block must be able to fall through.293assert(Cand.BranchBlock->canFallThrough() &&294"Expecting the block to fall through!");295296// We have already ensured there are exactly two successors to297// BranchBlock and that BranchTargetBlock is a successor to BranchBlock.298// Ensure the single fall though block is empty.299MachineBasicBlock *Succ =300(*Cand.BranchBlock->succ_begin() == Cand.BranchTargetBlock)301? *Cand.BranchBlock->succ_rbegin()302: *Cand.BranchBlock->succ_begin();303304assert(Succ && "Expecting a valid fall-through block\n");305306if (!Succ->empty()) {307LLVM_DEBUG(dbgs() << "Fall-through block contains code -- skip\n");308return false;309}310311if (!Succ->isSuccessor(Cand.BranchTargetBlock)) {312LLVM_DEBUG(313dbgs()314<< "Successor of fall through block is not branch taken block\n");315return false;316}317318Cand.FallThroughBlock = Succ;319LLVM_DEBUG(dbgs() << "Valid Candidate\n");320return true;321}322323///324/// Determine if the two operand lists are identical325///326/// \param[in] OpList1 operand list327/// \param[in] OpList2 operand list328/// \return true if and only if the operands lists are identical329///330bool PPCBranchCoalescing::identicalOperands(331ArrayRef<MachineOperand> OpList1, ArrayRef<MachineOperand> OpList2) const {332333if (OpList1.size() != OpList2.size()) {334LLVM_DEBUG(dbgs() << "Operand list is different size\n");335return false;336}337338for (unsigned i = 0; i < OpList1.size(); ++i) {339const MachineOperand &Op1 = OpList1[i];340const MachineOperand &Op2 = OpList2[i];341342LLVM_DEBUG(dbgs() << "Op1: " << Op1 << "\n"343<< "Op2: " << Op2 << "\n");344345if (Op1.isIdenticalTo(Op2)) {346// filter out instructions with physical-register uses347if (Op1.isReg() && Op1.getReg().isPhysical()348// If the physical register is constant then we can assume the value349// has not changed between uses.350&& !(Op1.isUse() && MRI->isConstantPhysReg(Op1.getReg()))) {351LLVM_DEBUG(dbgs() << "The operands are not provably identical.\n");352return false;353}354LLVM_DEBUG(dbgs() << "Op1 and Op2 are identical!\n");355continue;356}357358// If the operands are not identical, but are registers, check to see if the359// definition of the register produces the same value. If they produce the360// same value, consider them to be identical.361if (Op1.isReg() && Op2.isReg() && Op1.getReg().isVirtual() &&362Op2.getReg().isVirtual()) {363MachineInstr *Op1Def = MRI->getVRegDef(Op1.getReg());364MachineInstr *Op2Def = MRI->getVRegDef(Op2.getReg());365if (TII->produceSameValue(*Op1Def, *Op2Def, MRI)) {366LLVM_DEBUG(dbgs() << "Op1Def: " << *Op1Def << " and " << *Op2Def367<< " produce the same value!\n");368} else {369LLVM_DEBUG(dbgs() << "Operands produce different values\n");370return false;371}372} else {373LLVM_DEBUG(dbgs() << "The operands are not provably identical.\n");374return false;375}376}377378return true;379}380381///382/// Moves ALL PHI instructions in SourceMBB to beginning of TargetMBB383/// and update them to refer to the new block. PHI node ordering384/// cannot be assumed so it does not matter where the PHI instructions385/// are moved to in TargetMBB.386///387/// \param[in] SourceMBB block to move PHI instructions from388/// \param[in] TargetMBB block to move PHI instructions to389///390void PPCBranchCoalescing::moveAndUpdatePHIs(MachineBasicBlock *SourceMBB,391MachineBasicBlock *TargetMBB) {392393MachineBasicBlock::iterator MI = SourceMBB->begin();394MachineBasicBlock::iterator ME = SourceMBB->getFirstNonPHI();395396if (MI == ME) {397LLVM_DEBUG(dbgs() << "SourceMBB contains no PHI instructions.\n");398return;399}400401// Update all PHI instructions in SourceMBB and move to top of TargetMBB402for (MachineBasicBlock::iterator Iter = MI; Iter != ME; Iter++) {403MachineInstr &PHIInst = *Iter;404for (unsigned i = 2, e = PHIInst.getNumOperands() + 1; i != e; i += 2) {405MachineOperand &MO = PHIInst.getOperand(i);406if (MO.getMBB() == SourceMBB)407MO.setMBB(TargetMBB);408}409}410TargetMBB->splice(TargetMBB->begin(), SourceMBB, MI, ME);411}412413///414/// This function checks if MI can be moved to the beginning of the TargetMBB415/// following PHI instructions. A MI instruction can be moved to beginning of416/// the TargetMBB if there are no uses of it within the TargetMBB PHI nodes.417///418/// \param[in] MI the machine instruction to move.419/// \param[in] TargetMBB the machine basic block to move to420/// \return true if it is safe to move MI to beginning of TargetMBB,421/// false otherwise.422///423bool PPCBranchCoalescing::canMoveToBeginning(const MachineInstr &MI,424const MachineBasicBlock &TargetMBB425) const {426427LLVM_DEBUG(dbgs() << "Checking if " << MI << " can move to beginning of "428<< TargetMBB.getNumber() << "\n");429430for (auto &Def : MI.defs()) { // Looking at Def431for (auto &Use : MRI->use_instructions(Def.getReg())) {432if (Use.isPHI() && Use.getParent() == &TargetMBB) {433LLVM_DEBUG(dbgs() << " *** used in a PHI -- cannot move ***\n");434return false;435}436}437}438439LLVM_DEBUG(dbgs() << " Safe to move to the beginning.\n");440return true;441}442443///444/// This function checks if MI can be moved to the end of the TargetMBB,445/// immediately before the first terminator. A MI instruction can be moved446/// to then end of the TargetMBB if no PHI node defines what MI uses within447/// it's own MBB.448///449/// \param[in] MI the machine instruction to move.450/// \param[in] TargetMBB the machine basic block to move to451/// \return true if it is safe to move MI to end of TargetMBB,452/// false otherwise.453///454bool PPCBranchCoalescing::canMoveToEnd(const MachineInstr &MI,455const MachineBasicBlock &TargetMBB456) const {457458LLVM_DEBUG(dbgs() << "Checking if " << MI << " can move to end of "459<< TargetMBB.getNumber() << "\n");460461for (auto &Use : MI.uses()) {462if (Use.isReg() && Use.getReg().isVirtual()) {463MachineInstr *DefInst = MRI->getVRegDef(Use.getReg());464if (DefInst->isPHI() && DefInst->getParent() == MI.getParent()) {465LLVM_DEBUG(dbgs() << " *** Cannot move this instruction ***\n");466return false;467} else {468LLVM_DEBUG(469dbgs() << " *** def is in another block -- safe to move!\n");470}471}472}473474LLVM_DEBUG(dbgs() << " Safe to move to the end.\n");475return true;476}477478///479/// This method checks to ensure the two coalescing candidates follows the480/// expected pattern required for coalescing.481///482/// \param[in] SourceRegion The candidate to move statements from483/// \param[in] TargetRegion The candidate to move statements to484/// \return true if all instructions in SourceRegion.BranchBlock can be merged485/// into a block in TargetRegion; false otherwise.486///487bool PPCBranchCoalescing::validateCandidates(488CoalescingCandidateInfo &SourceRegion,489CoalescingCandidateInfo &TargetRegion) const {490491if (TargetRegion.BranchTargetBlock != SourceRegion.BranchBlock)492llvm_unreachable("Expecting SourceRegion to immediately follow TargetRegion");493else if (!MDT->dominates(TargetRegion.BranchBlock, SourceRegion.BranchBlock))494llvm_unreachable("Expecting TargetRegion to dominate SourceRegion");495else if (!MPDT->dominates(SourceRegion.BranchBlock, TargetRegion.BranchBlock))496llvm_unreachable("Expecting SourceRegion to post-dominate TargetRegion");497else if (!TargetRegion.FallThroughBlock->empty() ||498!SourceRegion.FallThroughBlock->empty())499llvm_unreachable("Expecting fall-through blocks to be empty");500501return true;502}503504///505/// This method determines whether the two coalescing candidates can be merged.506/// In order to be merged, all instructions must be able to507/// 1. Move to the beginning of the SourceRegion.BranchTargetBlock;508/// 2. Move to the end of the TargetRegion.BranchBlock.509/// Merging involves moving the instructions in the510/// TargetRegion.BranchTargetBlock (also SourceRegion.BranchBlock).511///512/// This function first try to move instructions from the513/// TargetRegion.BranchTargetBlock down, to the beginning of the514/// SourceRegion.BranchTargetBlock. This is not possible if any register defined515/// in TargetRegion.BranchTargetBlock is used in a PHI node in the516/// SourceRegion.BranchTargetBlock. In this case, check whether the statement517/// can be moved up, to the end of the TargetRegion.BranchBlock (immediately518/// before the branch statement). If it cannot move, then these blocks cannot519/// be merged.520///521/// Note that there is no analysis for moving instructions past the fall-through522/// blocks because they are confirmed to be empty. An assert is thrown if they523/// are not.524///525/// \param[in] SourceRegion The candidate to move statements from526/// \param[in] TargetRegion The candidate to move statements to527/// \return true if all instructions in SourceRegion.BranchBlock can be merged528/// into a block in TargetRegion, false otherwise.529///530bool PPCBranchCoalescing::canMerge(CoalescingCandidateInfo &SourceRegion,531CoalescingCandidateInfo &TargetRegion) const {532if (!validateCandidates(SourceRegion, TargetRegion))533return false;534535// Walk through PHI nodes first and see if they force the merge into the536// SourceRegion.BranchTargetBlock.537for (MachineBasicBlock::iterator538I = SourceRegion.BranchBlock->instr_begin(),539E = SourceRegion.BranchBlock->getFirstNonPHI();540I != E; ++I) {541for (auto &Def : I->defs())542for (auto &Use : MRI->use_instructions(Def.getReg())) {543if (Use.isPHI() && Use.getParent() == SourceRegion.BranchTargetBlock) {544LLVM_DEBUG(dbgs()545<< "PHI " << *I546<< " defines register used in another "547"PHI within branch target block -- can't merge\n");548NumPHINotMoved++;549return false;550}551if (Use.getParent() == SourceRegion.BranchBlock) {552LLVM_DEBUG(dbgs() << "PHI " << *I553<< " defines register used in this "554"block -- all must move down\n");555SourceRegion.MustMoveDown = true;556}557}558}559560// Walk through the MI to see if they should be merged into561// TargetRegion.BranchBlock (up) or SourceRegion.BranchTargetBlock (down)562for (MachineBasicBlock::iterator563I = SourceRegion.BranchBlock->getFirstNonPHI(),564E = SourceRegion.BranchBlock->end();565I != E; ++I) {566if (!canMoveToBeginning(*I, *SourceRegion.BranchTargetBlock)) {567LLVM_DEBUG(dbgs() << "Instruction " << *I568<< " cannot move down - must move up!\n");569SourceRegion.MustMoveUp = true;570}571if (!canMoveToEnd(*I, *TargetRegion.BranchBlock)) {572LLVM_DEBUG(dbgs() << "Instruction " << *I573<< " cannot move up - must move down!\n");574SourceRegion.MustMoveDown = true;575}576}577578return (SourceRegion.MustMoveUp && SourceRegion.MustMoveDown) ? false : true;579}580581/// Merge the instructions from SourceRegion.BranchBlock,582/// SourceRegion.BranchTargetBlock, and SourceRegion.FallThroughBlock into583/// TargetRegion.BranchBlock, TargetRegion.BranchTargetBlock and584/// TargetRegion.FallThroughBlock respectively.585///586/// The successors for blocks in TargetRegion will be updated to use the587/// successors from blocks in SourceRegion. Finally, the blocks in SourceRegion588/// will be removed from the function.589///590/// A region consists of a BranchBlock, a FallThroughBlock, and a591/// BranchTargetBlock. Branch coalesce works on patterns where the592/// TargetRegion's BranchTargetBlock must also be the SourceRegions's593/// BranchBlock.594///595/// Before mergeCandidates:596///597/// +---------------------------+598/// | TargetRegion.BranchBlock |599/// +---------------------------+600/// / |601/// / +--------------------------------+602/// | | TargetRegion.FallThroughBlock |603/// \ +--------------------------------+604/// \ |605/// +----------------------------------+606/// | TargetRegion.BranchTargetBlock |607/// | SourceRegion.BranchBlock |608/// +----------------------------------+609/// / |610/// / +--------------------------------+611/// | | SourceRegion.FallThroughBlock |612/// \ +--------------------------------+613/// \ |614/// +----------------------------------+615/// | SourceRegion.BranchTargetBlock |616/// +----------------------------------+617///618/// After mergeCandidates:619///620/// +-----------------------------+621/// | TargetRegion.BranchBlock |622/// | SourceRegion.BranchBlock |623/// +-----------------------------+624/// / |625/// / +---------------------------------+626/// | | TargetRegion.FallThroughBlock |627/// | | SourceRegion.FallThroughBlock |628/// \ +---------------------------------+629/// \ |630/// +----------------------------------+631/// | SourceRegion.BranchTargetBlock |632/// +----------------------------------+633///634/// \param[in] SourceRegion The candidate to move blocks from635/// \param[in] TargetRegion The candidate to move blocks to636///637bool PPCBranchCoalescing::mergeCandidates(CoalescingCandidateInfo &SourceRegion,638CoalescingCandidateInfo &TargetRegion) {639640if (SourceRegion.MustMoveUp && SourceRegion.MustMoveDown) {641llvm_unreachable("Cannot have both MustMoveDown and MustMoveUp set!");642return false;643}644645if (!validateCandidates(SourceRegion, TargetRegion))646return false;647648// Start the merging process by first handling the BranchBlock.649// Move any PHIs in SourceRegion.BranchBlock down to the branch-taken block650moveAndUpdatePHIs(SourceRegion.BranchBlock, SourceRegion.BranchTargetBlock);651652// Move remaining instructions in SourceRegion.BranchBlock into653// TargetRegion.BranchBlock654MachineBasicBlock::iterator firstInstr =655SourceRegion.BranchBlock->getFirstNonPHI();656MachineBasicBlock::iterator lastInstr =657SourceRegion.BranchBlock->getFirstTerminator();658659MachineBasicBlock *Source = SourceRegion.MustMoveDown660? SourceRegion.BranchTargetBlock661: TargetRegion.BranchBlock;662663MachineBasicBlock::iterator Target =664SourceRegion.MustMoveDown665? SourceRegion.BranchTargetBlock->getFirstNonPHI()666: TargetRegion.BranchBlock->getFirstTerminator();667668Source->splice(Target, SourceRegion.BranchBlock, firstInstr, lastInstr);669670// Once PHI and instructions have been moved we need to clean up the671// control flow.672673// Remove SourceRegion.FallThroughBlock before transferring successors of674// SourceRegion.BranchBlock to TargetRegion.BranchBlock.675SourceRegion.BranchBlock->removeSuccessor(SourceRegion.FallThroughBlock);676TargetRegion.BranchBlock->transferSuccessorsAndUpdatePHIs(677SourceRegion.BranchBlock);678// Update branch in TargetRegion.BranchBlock to jump to679// SourceRegion.BranchTargetBlock680// In this case, TargetRegion.BranchTargetBlock == SourceRegion.BranchBlock.681TargetRegion.BranchBlock->ReplaceUsesOfBlockWith(682SourceRegion.BranchBlock, SourceRegion.BranchTargetBlock);683// Remove the branch statement(s) in SourceRegion.BranchBlock684MachineBasicBlock::iterator I =685SourceRegion.BranchBlock->terminators().begin();686while (I != SourceRegion.BranchBlock->terminators().end()) {687MachineInstr &CurrInst = *I;688++I;689if (CurrInst.isBranch())690CurrInst.eraseFromParent();691}692693// Fall-through block should be empty since this is part of the condition694// to coalesce the branches.695assert(TargetRegion.FallThroughBlock->empty() &&696"FallThroughBlocks should be empty!");697698// Transfer successor information and move PHIs down to the699// branch-taken block.700TargetRegion.FallThroughBlock->transferSuccessorsAndUpdatePHIs(701SourceRegion.FallThroughBlock);702TargetRegion.FallThroughBlock->removeSuccessor(SourceRegion.BranchBlock);703TargetRegion.FallThroughBlock->normalizeSuccProbs();704705// Remove the blocks from the function.706assert(SourceRegion.BranchBlock->empty() &&707"Expecting branch block to be empty!");708SourceRegion.BranchBlock->eraseFromParent();709710assert(SourceRegion.FallThroughBlock->empty() &&711"Expecting fall-through block to be empty!\n");712SourceRegion.FallThroughBlock->eraseFromParent();713714NumBlocksCoalesced++;715return true;716}717718bool PPCBranchCoalescing::runOnMachineFunction(MachineFunction &MF) {719720if (skipFunction(MF.getFunction()) || MF.empty())721return false;722723bool didSomething = false;724725LLVM_DEBUG(dbgs() << "******** Branch Coalescing ********\n");726initialize(MF);727728LLVM_DEBUG(dbgs() << "Function: "; MF.dump(); dbgs() << "\n");729730CoalescingCandidateInfo Cand1, Cand2;731// Walk over blocks and find candidates to merge732// Continue trying to merge with the first candidate found, as long as merging733// is successfull.734for (MachineBasicBlock &MBB : MF) {735bool MergedCandidates = false;736do {737MergedCandidates = false;738Cand1.clear();739Cand2.clear();740741Cand1.BranchBlock = &MBB;742743// If unable to coalesce the branch, then continue to next block744if (!canCoalesceBranch(Cand1))745break;746747Cand2.BranchBlock = Cand1.BranchTargetBlock;748if (!canCoalesceBranch(Cand2))749break;750751// The branch-taken block of the second candidate should post-dominate the752// first candidate.753assert(MPDT->dominates(Cand2.BranchTargetBlock, Cand1.BranchBlock) &&754"Branch-taken block should post-dominate first candidate");755756if (!identicalOperands(Cand1.Cond, Cand2.Cond)) {757LLVM_DEBUG(dbgs() << "Blocks " << Cand1.BranchBlock->getNumber()758<< " and " << Cand2.BranchBlock->getNumber()759<< " have different branches\n");760break;761}762if (!canMerge(Cand2, Cand1)) {763LLVM_DEBUG(dbgs() << "Cannot merge blocks "764<< Cand1.BranchBlock->getNumber() << " and "765<< Cand2.BranchBlock->getNumber() << "\n");766NumBlocksNotCoalesced++;767continue;768}769LLVM_DEBUG(dbgs() << "Merging blocks " << Cand1.BranchBlock->getNumber()770<< " and " << Cand1.BranchTargetBlock->getNumber()771<< "\n");772MergedCandidates = mergeCandidates(Cand2, Cand1);773if (MergedCandidates)774didSomething = true;775776LLVM_DEBUG(dbgs() << "Function after merging: "; MF.dump();777dbgs() << "\n");778} while (MergedCandidates);779}780781#ifndef NDEBUG782// Verify MF is still valid after branch coalescing783if (didSomething)784MF.verify(nullptr, "Error in code produced by branch coalescing");785#endif // NDEBUG786787LLVM_DEBUG(dbgs() << "Finished Branch Coalescing\n");788return didSomething;789}790791792