10b57cec5SDimitry Andric //===-- CoalesceBranches.cpp - Coalesce blocks with the same condition ---===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric /// 90b57cec5SDimitry Andric /// \file 100b57cec5SDimitry Andric /// Coalesce basic blocks guarded by the same branch condition into a single 110b57cec5SDimitry Andric /// basic block. 120b57cec5SDimitry Andric /// 130b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric #include "PPC.h" 160b57cec5SDimitry Andric #include "llvm/ADT/BitVector.h" 170b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 180b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 190b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 200b57cec5SDimitry Andric #include "llvm/CodeGen/MachinePostDominators.h" 210b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/Passes.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/TargetFrameLowering.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/TargetInstrInfo.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 26480093f4SDimitry Andric #include "llvm/InitializePasses.h" 270b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 280b57cec5SDimitry Andric 290b57cec5SDimitry Andric using namespace llvm; 300b57cec5SDimitry Andric 310b57cec5SDimitry Andric #define DEBUG_TYPE "ppc-branch-coalescing" 320b57cec5SDimitry Andric 330b57cec5SDimitry Andric STATISTIC(NumBlocksCoalesced, "Number of blocks coalesced"); 340b57cec5SDimitry Andric STATISTIC(NumPHINotMoved, "Number of PHI Nodes that cannot be merged"); 350b57cec5SDimitry Andric STATISTIC(NumBlocksNotCoalesced, "Number of blocks not coalesced"); 360b57cec5SDimitry Andric 370b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 380b57cec5SDimitry Andric // PPCBranchCoalescing 390b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 400b57cec5SDimitry Andric /// 410b57cec5SDimitry Andric /// Improve scheduling by coalescing branches that depend on the same condition. 420b57cec5SDimitry Andric /// This pass looks for blocks that are guarded by the same branch condition 430b57cec5SDimitry Andric /// and attempts to merge the blocks together. Such opportunities arise from 440b57cec5SDimitry Andric /// the expansion of select statements in the IR. 450b57cec5SDimitry Andric /// 460b57cec5SDimitry Andric /// This pass does not handle implicit operands on branch statements. In order 470b57cec5SDimitry Andric /// to run on targets that use implicit operands, changes need to be made in the 480b57cec5SDimitry Andric /// canCoalesceBranch and canMerge methods. 490b57cec5SDimitry Andric /// 500b57cec5SDimitry Andric /// Example: the following LLVM IR 510b57cec5SDimitry Andric /// 520b57cec5SDimitry Andric /// %test = icmp eq i32 %x 0 530b57cec5SDimitry Andric /// %tmp1 = select i1 %test, double %a, double 2.000000e-03 540b57cec5SDimitry Andric /// %tmp2 = select i1 %test, double %b, double 5.000000e-03 550b57cec5SDimitry Andric /// 560b57cec5SDimitry Andric /// expands to the following machine code: 570b57cec5SDimitry Andric /// 580b57cec5SDimitry Andric /// %bb.0: derived from LLVM BB %entry 590b57cec5SDimitry Andric /// liveins: %f1 %f3 %x6 600b57cec5SDimitry Andric /// <SNIP1> 610b57cec5SDimitry Andric /// %0 = COPY %f1; F8RC:%0 620b57cec5SDimitry Andric /// %5 = CMPLWI killed %4, 0; CRRC:%5 GPRC:%4 630b57cec5SDimitry Andric /// %8 = LXSDX %zero8, killed %7, implicit %rm; 640b57cec5SDimitry Andric /// mem:LD8[ConstantPool] F8RC:%8 G8RC:%7 650b57cec5SDimitry Andric /// BCC 76, %5, <%bb.2>; CRRC:%5 660b57cec5SDimitry Andric /// Successors according to CFG: %bb.1(?%) %bb.2(?%) 670b57cec5SDimitry Andric /// 680b57cec5SDimitry Andric /// %bb.1: derived from LLVM BB %entry 690b57cec5SDimitry Andric /// Predecessors according to CFG: %bb.0 700b57cec5SDimitry Andric /// Successors according to CFG: %bb.2(?%) 710b57cec5SDimitry Andric /// 720b57cec5SDimitry Andric /// %bb.2: derived from LLVM BB %entry 730b57cec5SDimitry Andric /// Predecessors according to CFG: %bb.0 %bb.1 740b57cec5SDimitry Andric /// %9 = PHI %8, <%bb.1>, %0, <%bb.0>; 750b57cec5SDimitry Andric /// F8RC:%9,%8,%0 760b57cec5SDimitry Andric /// <SNIP2> 770b57cec5SDimitry Andric /// BCC 76, %5, <%bb.4>; CRRC:%5 780b57cec5SDimitry Andric /// Successors according to CFG: %bb.3(?%) %bb.4(?%) 790b57cec5SDimitry Andric /// 800b57cec5SDimitry Andric /// %bb.3: derived from LLVM BB %entry 810b57cec5SDimitry Andric /// Predecessors according to CFG: %bb.2 820b57cec5SDimitry Andric /// Successors according to CFG: %bb.4(?%) 830b57cec5SDimitry Andric /// 840b57cec5SDimitry Andric /// %bb.4: derived from LLVM BB %entry 850b57cec5SDimitry Andric /// Predecessors according to CFG: %bb.2 %bb.3 860b57cec5SDimitry Andric /// %13 = PHI %12, <%bb.3>, %2, <%bb.2>; 870b57cec5SDimitry Andric /// F8RC:%13,%12,%2 880b57cec5SDimitry Andric /// <SNIP3> 890b57cec5SDimitry Andric /// BLR8 implicit %lr8, implicit %rm, implicit %f1 900b57cec5SDimitry Andric /// 910b57cec5SDimitry Andric /// When this pattern is detected, branch coalescing will try to collapse 920b57cec5SDimitry Andric /// it by moving code in %bb.2 to %bb.0 and/or %bb.4 and removing %bb.3. 930b57cec5SDimitry Andric /// 940b57cec5SDimitry Andric /// If all conditions are meet, IR should collapse to: 950b57cec5SDimitry Andric /// 960b57cec5SDimitry Andric /// %bb.0: derived from LLVM BB %entry 970b57cec5SDimitry Andric /// liveins: %f1 %f3 %x6 980b57cec5SDimitry Andric /// <SNIP1> 990b57cec5SDimitry Andric /// %0 = COPY %f1; F8RC:%0 1000b57cec5SDimitry Andric /// %5 = CMPLWI killed %4, 0; CRRC:%5 GPRC:%4 1010b57cec5SDimitry Andric /// %8 = LXSDX %zero8, killed %7, implicit %rm; 1020b57cec5SDimitry Andric /// mem:LD8[ConstantPool] F8RC:%8 G8RC:%7 1030b57cec5SDimitry Andric /// <SNIP2> 1040b57cec5SDimitry Andric /// BCC 76, %5, <%bb.4>; CRRC:%5 1050b57cec5SDimitry Andric /// Successors according to CFG: %bb.1(0x2aaaaaaa / 0x80000000 = 33.33%) 1060b57cec5SDimitry Andric /// %bb.4(0x55555554 / 0x80000000 = 66.67%) 1070b57cec5SDimitry Andric /// 1080b57cec5SDimitry Andric /// %bb.1: derived from LLVM BB %entry 1090b57cec5SDimitry Andric /// Predecessors according to CFG: %bb.0 1100b57cec5SDimitry Andric /// Successors according to CFG: %bb.4(0x40000000 / 0x80000000 = 50.00%) 1110b57cec5SDimitry Andric /// 1120b57cec5SDimitry Andric /// %bb.4: derived from LLVM BB %entry 1130b57cec5SDimitry Andric /// Predecessors according to CFG: %bb.0 %bb.1 1140b57cec5SDimitry Andric /// %9 = PHI %8, <%bb.1>, %0, <%bb.0>; 1150b57cec5SDimitry Andric /// F8RC:%9,%8,%0 1160b57cec5SDimitry Andric /// %13 = PHI %12, <%bb.1>, %2, <%bb.0>; 1170b57cec5SDimitry Andric /// F8RC:%13,%12,%2 1180b57cec5SDimitry Andric /// <SNIP3> 1190b57cec5SDimitry Andric /// BLR8 implicit %lr8, implicit %rm, implicit %f1 1200b57cec5SDimitry Andric /// 1210b57cec5SDimitry Andric /// Branch Coalescing does not split blocks, it moves everything in the same 1220b57cec5SDimitry Andric /// direction ensuring it does not break use/definition semantics. 1230b57cec5SDimitry Andric /// 1240b57cec5SDimitry Andric /// PHI nodes and its corresponding use instructions are moved to its successor 1250b57cec5SDimitry Andric /// block if there are no uses within the successor block PHI nodes. PHI 1260b57cec5SDimitry Andric /// node ordering cannot be assumed. 1270b57cec5SDimitry Andric /// 1280b57cec5SDimitry Andric /// Non-PHI can be moved up to the predecessor basic block or down to the 1290b57cec5SDimitry Andric /// successor basic block following any PHI instructions. Whether it moves 1300b57cec5SDimitry Andric /// up or down depends on whether the register(s) defined in the instructions 1310b57cec5SDimitry Andric /// are used in current block or in any PHI instructions at the beginning of 1320b57cec5SDimitry Andric /// the successor block. 1330b57cec5SDimitry Andric 1340b57cec5SDimitry Andric namespace { 1350b57cec5SDimitry Andric 1360b57cec5SDimitry Andric class PPCBranchCoalescing : public MachineFunctionPass { 1370b57cec5SDimitry Andric struct CoalescingCandidateInfo { 1380b57cec5SDimitry Andric MachineBasicBlock *BranchBlock; // Block containing the branch 1390b57cec5SDimitry Andric MachineBasicBlock *BranchTargetBlock; // Block branched to 1400b57cec5SDimitry Andric MachineBasicBlock *FallThroughBlock; // Fall-through if branch not taken 1410b57cec5SDimitry Andric SmallVector<MachineOperand, 4> Cond; 1420b57cec5SDimitry Andric bool MustMoveDown; 1430b57cec5SDimitry Andric bool MustMoveUp; 1440b57cec5SDimitry Andric 1450b57cec5SDimitry Andric CoalescingCandidateInfo(); 1460b57cec5SDimitry Andric void clear(); 1470b57cec5SDimitry Andric }; 1480b57cec5SDimitry Andric 1490b57cec5SDimitry Andric MachineDominatorTree *MDT; 1500b57cec5SDimitry Andric MachinePostDominatorTree *MPDT; 1510b57cec5SDimitry Andric const TargetInstrInfo *TII; 1520b57cec5SDimitry Andric MachineRegisterInfo *MRI; 1530b57cec5SDimitry Andric 1540b57cec5SDimitry Andric void initialize(MachineFunction &F); 1550b57cec5SDimitry Andric bool canCoalesceBranch(CoalescingCandidateInfo &Cand); 1560b57cec5SDimitry Andric bool identicalOperands(ArrayRef<MachineOperand> OperandList1, 1570b57cec5SDimitry Andric ArrayRef<MachineOperand> OperandList2) const; 1580b57cec5SDimitry Andric bool validateCandidates(CoalescingCandidateInfo &SourceRegion, 1590b57cec5SDimitry Andric CoalescingCandidateInfo &TargetRegion) const; 1600b57cec5SDimitry Andric 1610b57cec5SDimitry Andric public: 1620b57cec5SDimitry Andric static char ID; 1630b57cec5SDimitry Andric 1640b57cec5SDimitry Andric PPCBranchCoalescing() : MachineFunctionPass(ID) { 1650b57cec5SDimitry Andric initializePPCBranchCoalescingPass(*PassRegistry::getPassRegistry()); 1660b57cec5SDimitry Andric } 1670b57cec5SDimitry Andric 1680b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 1690b57cec5SDimitry Andric AU.addRequired<MachineDominatorTree>(); 1700b57cec5SDimitry Andric AU.addRequired<MachinePostDominatorTree>(); 1710b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 1720b57cec5SDimitry Andric } 1730b57cec5SDimitry Andric 1740b57cec5SDimitry Andric StringRef getPassName() const override { return "Branch Coalescing"; } 1750b57cec5SDimitry Andric 1760b57cec5SDimitry Andric bool mergeCandidates(CoalescingCandidateInfo &SourceRegion, 1770b57cec5SDimitry Andric CoalescingCandidateInfo &TargetRegion); 1780b57cec5SDimitry Andric bool canMoveToBeginning(const MachineInstr &MI, 1790b57cec5SDimitry Andric const MachineBasicBlock &MBB) const; 1800b57cec5SDimitry Andric bool canMoveToEnd(const MachineInstr &MI, 1810b57cec5SDimitry Andric const MachineBasicBlock &MBB) const; 1820b57cec5SDimitry Andric bool canMerge(CoalescingCandidateInfo &SourceRegion, 1830b57cec5SDimitry Andric CoalescingCandidateInfo &TargetRegion) const; 1840b57cec5SDimitry Andric void moveAndUpdatePHIs(MachineBasicBlock *SourceRegionMBB, 1850b57cec5SDimitry Andric MachineBasicBlock *TargetRegionMBB); 1860b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override; 1870b57cec5SDimitry Andric }; 1880b57cec5SDimitry Andric } // End anonymous namespace. 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric char PPCBranchCoalescing::ID = 0; 1910b57cec5SDimitry Andric /// createPPCBranchCoalescingPass - returns an instance of the Branch Coalescing 1920b57cec5SDimitry Andric /// Pass 1930b57cec5SDimitry Andric FunctionPass *llvm::createPPCBranchCoalescingPass() { 1940b57cec5SDimitry Andric return new PPCBranchCoalescing(); 1950b57cec5SDimitry Andric } 1960b57cec5SDimitry Andric 1970b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(PPCBranchCoalescing, DEBUG_TYPE, 1980b57cec5SDimitry Andric "Branch Coalescing", false, false) 1990b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 2000b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTree) 2010b57cec5SDimitry Andric INITIALIZE_PASS_END(PPCBranchCoalescing, DEBUG_TYPE, "Branch Coalescing", 2020b57cec5SDimitry Andric false, false) 2030b57cec5SDimitry Andric 2040b57cec5SDimitry Andric PPCBranchCoalescing::CoalescingCandidateInfo::CoalescingCandidateInfo() 2050b57cec5SDimitry Andric : BranchBlock(nullptr), BranchTargetBlock(nullptr), 2060b57cec5SDimitry Andric FallThroughBlock(nullptr), MustMoveDown(false), MustMoveUp(false) {} 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric void PPCBranchCoalescing::CoalescingCandidateInfo::clear() { 2090b57cec5SDimitry Andric BranchBlock = nullptr; 2100b57cec5SDimitry Andric BranchTargetBlock = nullptr; 2110b57cec5SDimitry Andric FallThroughBlock = nullptr; 2120b57cec5SDimitry Andric Cond.clear(); 2130b57cec5SDimitry Andric MustMoveDown = false; 2140b57cec5SDimitry Andric MustMoveUp = false; 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric void PPCBranchCoalescing::initialize(MachineFunction &MF) { 2180b57cec5SDimitry Andric MDT = &getAnalysis<MachineDominatorTree>(); 2190b57cec5SDimitry Andric MPDT = &getAnalysis<MachinePostDominatorTree>(); 2200b57cec5SDimitry Andric TII = MF.getSubtarget().getInstrInfo(); 2210b57cec5SDimitry Andric MRI = &MF.getRegInfo(); 2220b57cec5SDimitry Andric } 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric /// 2250b57cec5SDimitry Andric /// Analyze the branch statement to determine if it can be coalesced. This 2260b57cec5SDimitry Andric /// method analyses the branch statement for the given candidate to determine 2270b57cec5SDimitry Andric /// if it can be coalesced. If the branch can be coalesced, then the 2280b57cec5SDimitry Andric /// BranchTargetBlock and the FallThroughBlock are recorded in the specified 2290b57cec5SDimitry Andric /// Candidate. 2300b57cec5SDimitry Andric /// 2310b57cec5SDimitry Andric ///\param[in,out] Cand The coalescing candidate to analyze 2320b57cec5SDimitry Andric ///\return true if and only if the branch can be coalesced, false otherwise 2330b57cec5SDimitry Andric /// 2340b57cec5SDimitry Andric bool PPCBranchCoalescing::canCoalesceBranch(CoalescingCandidateInfo &Cand) { 2350b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Determine if branch block " 2360b57cec5SDimitry Andric << Cand.BranchBlock->getNumber() << " can be coalesced:"); 2370b57cec5SDimitry Andric MachineBasicBlock *FalseMBB = nullptr; 2380b57cec5SDimitry Andric 2390b57cec5SDimitry Andric if (TII->analyzeBranch(*Cand.BranchBlock, Cand.BranchTargetBlock, FalseMBB, 2400b57cec5SDimitry Andric Cand.Cond)) { 2410b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "TII unable to Analyze Branch - skip\n"); 2420b57cec5SDimitry Andric return false; 2430b57cec5SDimitry Andric } 2440b57cec5SDimitry Andric 2450b57cec5SDimitry Andric for (auto &I : Cand.BranchBlock->terminators()) { 2460b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Looking at terminator : " << I << "\n"); 2470b57cec5SDimitry Andric if (!I.isBranch()) 2480b57cec5SDimitry Andric continue; 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric // The analyzeBranch method does not include any implicit operands. 2510b57cec5SDimitry Andric // This is not an issue on PPC but must be handled on other targets. 2520b57cec5SDimitry Andric // For this pass to be made target-independent, the analyzeBranch API 2530b57cec5SDimitry Andric // need to be updated to support implicit operands and there would 2540b57cec5SDimitry Andric // need to be a way to verify that any implicit operands would not be 2550b57cec5SDimitry Andric // clobbered by merging blocks. This would include identifying the 2560b57cec5SDimitry Andric // implicit operands as well as the basic block they are defined in. 2570b57cec5SDimitry Andric // This could be done by changing the analyzeBranch API to have it also 2580b57cec5SDimitry Andric // record and return the implicit operands and the blocks where they are 2590b57cec5SDimitry Andric // defined. Alternatively, the BranchCoalescing code would need to be 2600b57cec5SDimitry Andric // extended to identify the implicit operands. The analysis in canMerge 2610b57cec5SDimitry Andric // must then be extended to prove that none of the implicit operands are 2620b57cec5SDimitry Andric // changed in the blocks that are combined during coalescing. 2630b57cec5SDimitry Andric if (I.getNumOperands() != I.getNumExplicitOperands()) { 2640b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Terminator contains implicit operands - skip : " 2650b57cec5SDimitry Andric << I << "\n"); 2660b57cec5SDimitry Andric return false; 2670b57cec5SDimitry Andric } 2680b57cec5SDimitry Andric } 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric if (Cand.BranchBlock->isEHPad() || Cand.BranchBlock->hasEHPadSuccessor()) { 2710b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "EH Pad - skip\n"); 2720b57cec5SDimitry Andric return false; 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric 2755ffd83dbSDimitry Andric if (Cand.BranchBlock->mayHaveInlineAsmBr()) { 2765ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Inline Asm Br - skip\n"); 2775ffd83dbSDimitry Andric return false; 2785ffd83dbSDimitry Andric } 2795ffd83dbSDimitry Andric 2800b57cec5SDimitry Andric // For now only consider triangles (i.e, BranchTargetBlock is set, 2810b57cec5SDimitry Andric // FalseMBB is null, and BranchTargetBlock is a successor to BranchBlock) 2820b57cec5SDimitry Andric if (!Cand.BranchTargetBlock || FalseMBB || 2830b57cec5SDimitry Andric !Cand.BranchBlock->isSuccessor(Cand.BranchTargetBlock)) { 2840b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Does not form a triangle - skip\n"); 2850b57cec5SDimitry Andric return false; 2860b57cec5SDimitry Andric } 2870b57cec5SDimitry Andric 2880b57cec5SDimitry Andric // Ensure there are only two successors 2890b57cec5SDimitry Andric if (Cand.BranchBlock->succ_size() != 2) { 2900b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Does not have 2 successors - skip\n"); 2910b57cec5SDimitry Andric return false; 2920b57cec5SDimitry Andric } 2930b57cec5SDimitry Andric 294349cc55cSDimitry Andric // The block must be able to fall through. 2950b57cec5SDimitry Andric assert(Cand.BranchBlock->canFallThrough() && 2960b57cec5SDimitry Andric "Expecting the block to fall through!"); 2970b57cec5SDimitry Andric 2980b57cec5SDimitry Andric // We have already ensured there are exactly two successors to 2990b57cec5SDimitry Andric // BranchBlock and that BranchTargetBlock is a successor to BranchBlock. 3000b57cec5SDimitry Andric // Ensure the single fall though block is empty. 3010b57cec5SDimitry Andric MachineBasicBlock *Succ = 3020b57cec5SDimitry Andric (*Cand.BranchBlock->succ_begin() == Cand.BranchTargetBlock) 3030b57cec5SDimitry Andric ? *Cand.BranchBlock->succ_rbegin() 3040b57cec5SDimitry Andric : *Cand.BranchBlock->succ_begin(); 3050b57cec5SDimitry Andric 3060b57cec5SDimitry Andric assert(Succ && "Expecting a valid fall-through block\n"); 3070b57cec5SDimitry Andric 3080b57cec5SDimitry Andric if (!Succ->empty()) { 3090b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Fall-through block contains code -- skip\n"); 3100b57cec5SDimitry Andric return false; 3110b57cec5SDimitry Andric } 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric if (!Succ->isSuccessor(Cand.BranchTargetBlock)) { 3140b57cec5SDimitry Andric LLVM_DEBUG( 3150b57cec5SDimitry Andric dbgs() 3160b57cec5SDimitry Andric << "Successor of fall through block is not branch taken block\n"); 3170b57cec5SDimitry Andric return false; 3180b57cec5SDimitry Andric } 3190b57cec5SDimitry Andric 3200b57cec5SDimitry Andric Cand.FallThroughBlock = Succ; 3210b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Valid Candidate\n"); 3220b57cec5SDimitry Andric return true; 3230b57cec5SDimitry Andric } 3240b57cec5SDimitry Andric 3250b57cec5SDimitry Andric /// 3260b57cec5SDimitry Andric /// Determine if the two operand lists are identical 3270b57cec5SDimitry Andric /// 3280b57cec5SDimitry Andric /// \param[in] OpList1 operand list 3290b57cec5SDimitry Andric /// \param[in] OpList2 operand list 3300b57cec5SDimitry Andric /// \return true if and only if the operands lists are identical 3310b57cec5SDimitry Andric /// 3320b57cec5SDimitry Andric bool PPCBranchCoalescing::identicalOperands( 3330b57cec5SDimitry Andric ArrayRef<MachineOperand> OpList1, ArrayRef<MachineOperand> OpList2) const { 3340b57cec5SDimitry Andric 3350b57cec5SDimitry Andric if (OpList1.size() != OpList2.size()) { 3360b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Operand list is different size\n"); 3370b57cec5SDimitry Andric return false; 3380b57cec5SDimitry Andric } 3390b57cec5SDimitry Andric 3400b57cec5SDimitry Andric for (unsigned i = 0; i < OpList1.size(); ++i) { 3410b57cec5SDimitry Andric const MachineOperand &Op1 = OpList1[i]; 3420b57cec5SDimitry Andric const MachineOperand &Op2 = OpList2[i]; 3430b57cec5SDimitry Andric 3440b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Op1: " << Op1 << "\n" 3450b57cec5SDimitry Andric << "Op2: " << Op2 << "\n"); 3460b57cec5SDimitry Andric 3470b57cec5SDimitry Andric if (Op1.isIdenticalTo(Op2)) { 3480b57cec5SDimitry Andric // filter out instructions with physical-register uses 349*bdd1243dSDimitry Andric if (Op1.isReg() && Op1.getReg().isPhysical() 3500b57cec5SDimitry Andric // If the physical register is constant then we can assume the value 3510b57cec5SDimitry Andric // has not changed between uses. 3520b57cec5SDimitry Andric && !(Op1.isUse() && MRI->isConstantPhysReg(Op1.getReg()))) { 3530b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "The operands are not provably identical.\n"); 3540b57cec5SDimitry Andric return false; 3550b57cec5SDimitry Andric } 3560b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Op1 and Op2 are identical!\n"); 3570b57cec5SDimitry Andric continue; 3580b57cec5SDimitry Andric } 3590b57cec5SDimitry Andric 3600b57cec5SDimitry Andric // If the operands are not identical, but are registers, check to see if the 3610b57cec5SDimitry Andric // definition of the register produces the same value. If they produce the 3620b57cec5SDimitry Andric // same value, consider them to be identical. 363*bdd1243dSDimitry Andric if (Op1.isReg() && Op2.isReg() && Op1.getReg().isVirtual() && 364*bdd1243dSDimitry Andric Op2.getReg().isVirtual()) { 3650b57cec5SDimitry Andric MachineInstr *Op1Def = MRI->getVRegDef(Op1.getReg()); 3660b57cec5SDimitry Andric MachineInstr *Op2Def = MRI->getVRegDef(Op2.getReg()); 3670b57cec5SDimitry Andric if (TII->produceSameValue(*Op1Def, *Op2Def, MRI)) { 3680b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Op1Def: " << *Op1Def << " and " << *Op2Def 3690b57cec5SDimitry Andric << " produce the same value!\n"); 3700b57cec5SDimitry Andric } else { 3710b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Operands produce different values\n"); 3720b57cec5SDimitry Andric return false; 3730b57cec5SDimitry Andric } 3740b57cec5SDimitry Andric } else { 3750b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "The operands are not provably identical.\n"); 3760b57cec5SDimitry Andric return false; 3770b57cec5SDimitry Andric } 3780b57cec5SDimitry Andric } 3790b57cec5SDimitry Andric 3800b57cec5SDimitry Andric return true; 3810b57cec5SDimitry Andric } 3820b57cec5SDimitry Andric 3830b57cec5SDimitry Andric /// 3840b57cec5SDimitry Andric /// Moves ALL PHI instructions in SourceMBB to beginning of TargetMBB 3850b57cec5SDimitry Andric /// and update them to refer to the new block. PHI node ordering 3860b57cec5SDimitry Andric /// cannot be assumed so it does not matter where the PHI instructions 3870b57cec5SDimitry Andric /// are moved to in TargetMBB. 3880b57cec5SDimitry Andric /// 3890b57cec5SDimitry Andric /// \param[in] SourceMBB block to move PHI instructions from 3900b57cec5SDimitry Andric /// \param[in] TargetMBB block to move PHI instructions to 3910b57cec5SDimitry Andric /// 3920b57cec5SDimitry Andric void PPCBranchCoalescing::moveAndUpdatePHIs(MachineBasicBlock *SourceMBB, 3930b57cec5SDimitry Andric MachineBasicBlock *TargetMBB) { 3940b57cec5SDimitry Andric 3950b57cec5SDimitry Andric MachineBasicBlock::iterator MI = SourceMBB->begin(); 3960b57cec5SDimitry Andric MachineBasicBlock::iterator ME = SourceMBB->getFirstNonPHI(); 3970b57cec5SDimitry Andric 3980b57cec5SDimitry Andric if (MI == ME) { 3990b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "SourceMBB contains no PHI instructions.\n"); 4000b57cec5SDimitry Andric return; 4010b57cec5SDimitry Andric } 4020b57cec5SDimitry Andric 4030b57cec5SDimitry Andric // Update all PHI instructions in SourceMBB and move to top of TargetMBB 4040b57cec5SDimitry Andric for (MachineBasicBlock::iterator Iter = MI; Iter != ME; Iter++) { 4050b57cec5SDimitry Andric MachineInstr &PHIInst = *Iter; 4060b57cec5SDimitry Andric for (unsigned i = 2, e = PHIInst.getNumOperands() + 1; i != e; i += 2) { 4070b57cec5SDimitry Andric MachineOperand &MO = PHIInst.getOperand(i); 4080b57cec5SDimitry Andric if (MO.getMBB() == SourceMBB) 4090b57cec5SDimitry Andric MO.setMBB(TargetMBB); 4100b57cec5SDimitry Andric } 4110b57cec5SDimitry Andric } 4120b57cec5SDimitry Andric TargetMBB->splice(TargetMBB->begin(), SourceMBB, MI, ME); 4130b57cec5SDimitry Andric } 4140b57cec5SDimitry Andric 4150b57cec5SDimitry Andric /// 4160b57cec5SDimitry Andric /// This function checks if MI can be moved to the beginning of the TargetMBB 4170b57cec5SDimitry Andric /// following PHI instructions. A MI instruction can be moved to beginning of 4180b57cec5SDimitry Andric /// the TargetMBB if there are no uses of it within the TargetMBB PHI nodes. 4190b57cec5SDimitry Andric /// 4200b57cec5SDimitry Andric /// \param[in] MI the machine instruction to move. 4210b57cec5SDimitry Andric /// \param[in] TargetMBB the machine basic block to move to 4220b57cec5SDimitry Andric /// \return true if it is safe to move MI to beginning of TargetMBB, 4230b57cec5SDimitry Andric /// false otherwise. 4240b57cec5SDimitry Andric /// 4250b57cec5SDimitry Andric bool PPCBranchCoalescing::canMoveToBeginning(const MachineInstr &MI, 4260b57cec5SDimitry Andric const MachineBasicBlock &TargetMBB 4270b57cec5SDimitry Andric ) const { 4280b57cec5SDimitry Andric 4290b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Checking if " << MI << " can move to beginning of " 4300b57cec5SDimitry Andric << TargetMBB.getNumber() << "\n"); 4310b57cec5SDimitry Andric 4320b57cec5SDimitry Andric for (auto &Def : MI.defs()) { // Looking at Def 4330b57cec5SDimitry Andric for (auto &Use : MRI->use_instructions(Def.getReg())) { 4340b57cec5SDimitry Andric if (Use.isPHI() && Use.getParent() == &TargetMBB) { 4350b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " *** used in a PHI -- cannot move ***\n"); 4360b57cec5SDimitry Andric return false; 4370b57cec5SDimitry Andric } 4380b57cec5SDimitry Andric } 4390b57cec5SDimitry Andric } 4400b57cec5SDimitry Andric 4410b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Safe to move to the beginning.\n"); 4420b57cec5SDimitry Andric return true; 4430b57cec5SDimitry Andric } 4440b57cec5SDimitry Andric 4450b57cec5SDimitry Andric /// 4460b57cec5SDimitry Andric /// This function checks if MI can be moved to the end of the TargetMBB, 4470b57cec5SDimitry Andric /// immediately before the first terminator. A MI instruction can be moved 4480b57cec5SDimitry Andric /// to then end of the TargetMBB if no PHI node defines what MI uses within 4490b57cec5SDimitry Andric /// it's own MBB. 4500b57cec5SDimitry Andric /// 4510b57cec5SDimitry Andric /// \param[in] MI the machine instruction to move. 4520b57cec5SDimitry Andric /// \param[in] TargetMBB the machine basic block to move to 4530b57cec5SDimitry Andric /// \return true if it is safe to move MI to end of TargetMBB, 4540b57cec5SDimitry Andric /// false otherwise. 4550b57cec5SDimitry Andric /// 4560b57cec5SDimitry Andric bool PPCBranchCoalescing::canMoveToEnd(const MachineInstr &MI, 4570b57cec5SDimitry Andric const MachineBasicBlock &TargetMBB 4580b57cec5SDimitry Andric ) const { 4590b57cec5SDimitry Andric 4600b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Checking if " << MI << " can move to end of " 4610b57cec5SDimitry Andric << TargetMBB.getNumber() << "\n"); 4620b57cec5SDimitry Andric 4630b57cec5SDimitry Andric for (auto &Use : MI.uses()) { 464*bdd1243dSDimitry Andric if (Use.isReg() && Use.getReg().isVirtual()) { 4650b57cec5SDimitry Andric MachineInstr *DefInst = MRI->getVRegDef(Use.getReg()); 4660b57cec5SDimitry Andric if (DefInst->isPHI() && DefInst->getParent() == MI.getParent()) { 4670b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " *** Cannot move this instruction ***\n"); 4680b57cec5SDimitry Andric return false; 4690b57cec5SDimitry Andric } else { 4700b57cec5SDimitry Andric LLVM_DEBUG( 4710b57cec5SDimitry Andric dbgs() << " *** def is in another block -- safe to move!\n"); 4720b57cec5SDimitry Andric } 4730b57cec5SDimitry Andric } 4740b57cec5SDimitry Andric } 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << " Safe to move to the end.\n"); 4770b57cec5SDimitry Andric return true; 4780b57cec5SDimitry Andric } 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric /// 4810b57cec5SDimitry Andric /// This method checks to ensure the two coalescing candidates follows the 4820b57cec5SDimitry Andric /// expected pattern required for coalescing. 4830b57cec5SDimitry Andric /// 4840b57cec5SDimitry Andric /// \param[in] SourceRegion The candidate to move statements from 4850b57cec5SDimitry Andric /// \param[in] TargetRegion The candidate to move statements to 4860b57cec5SDimitry Andric /// \return true if all instructions in SourceRegion.BranchBlock can be merged 4870b57cec5SDimitry Andric /// into a block in TargetRegion; false otherwise. 4880b57cec5SDimitry Andric /// 4890b57cec5SDimitry Andric bool PPCBranchCoalescing::validateCandidates( 4900b57cec5SDimitry Andric CoalescingCandidateInfo &SourceRegion, 4910b57cec5SDimitry Andric CoalescingCandidateInfo &TargetRegion) const { 4920b57cec5SDimitry Andric 4930b57cec5SDimitry Andric if (TargetRegion.BranchTargetBlock != SourceRegion.BranchBlock) 4940b57cec5SDimitry Andric llvm_unreachable("Expecting SourceRegion to immediately follow TargetRegion"); 4950b57cec5SDimitry Andric else if (!MDT->dominates(TargetRegion.BranchBlock, SourceRegion.BranchBlock)) 4960b57cec5SDimitry Andric llvm_unreachable("Expecting TargetRegion to dominate SourceRegion"); 4970b57cec5SDimitry Andric else if (!MPDT->dominates(SourceRegion.BranchBlock, TargetRegion.BranchBlock)) 4980b57cec5SDimitry Andric llvm_unreachable("Expecting SourceRegion to post-dominate TargetRegion"); 4990b57cec5SDimitry Andric else if (!TargetRegion.FallThroughBlock->empty() || 5000b57cec5SDimitry Andric !SourceRegion.FallThroughBlock->empty()) 5010b57cec5SDimitry Andric llvm_unreachable("Expecting fall-through blocks to be empty"); 5020b57cec5SDimitry Andric 5030b57cec5SDimitry Andric return true; 5040b57cec5SDimitry Andric } 5050b57cec5SDimitry Andric 5060b57cec5SDimitry Andric /// 5070b57cec5SDimitry Andric /// This method determines whether the two coalescing candidates can be merged. 5080b57cec5SDimitry Andric /// In order to be merged, all instructions must be able to 5090b57cec5SDimitry Andric /// 1. Move to the beginning of the SourceRegion.BranchTargetBlock; 5100b57cec5SDimitry Andric /// 2. Move to the end of the TargetRegion.BranchBlock. 5110b57cec5SDimitry Andric /// Merging involves moving the instructions in the 5120b57cec5SDimitry Andric /// TargetRegion.BranchTargetBlock (also SourceRegion.BranchBlock). 5130b57cec5SDimitry Andric /// 5140b57cec5SDimitry Andric /// This function first try to move instructions from the 5150b57cec5SDimitry Andric /// TargetRegion.BranchTargetBlock down, to the beginning of the 5160b57cec5SDimitry Andric /// SourceRegion.BranchTargetBlock. This is not possible if any register defined 5170b57cec5SDimitry Andric /// in TargetRegion.BranchTargetBlock is used in a PHI node in the 5180b57cec5SDimitry Andric /// SourceRegion.BranchTargetBlock. In this case, check whether the statement 5190b57cec5SDimitry Andric /// can be moved up, to the end of the TargetRegion.BranchBlock (immediately 5200b57cec5SDimitry Andric /// before the branch statement). If it cannot move, then these blocks cannot 5210b57cec5SDimitry Andric /// be merged. 5220b57cec5SDimitry Andric /// 5230b57cec5SDimitry Andric /// Note that there is no analysis for moving instructions past the fall-through 5240b57cec5SDimitry Andric /// blocks because they are confirmed to be empty. An assert is thrown if they 5250b57cec5SDimitry Andric /// are not. 5260b57cec5SDimitry Andric /// 5270b57cec5SDimitry Andric /// \param[in] SourceRegion The candidate to move statements from 5280b57cec5SDimitry Andric /// \param[in] TargetRegion The candidate to move statements to 5290b57cec5SDimitry Andric /// \return true if all instructions in SourceRegion.BranchBlock can be merged 5300b57cec5SDimitry Andric /// into a block in TargetRegion, false otherwise. 5310b57cec5SDimitry Andric /// 5320b57cec5SDimitry Andric bool PPCBranchCoalescing::canMerge(CoalescingCandidateInfo &SourceRegion, 5330b57cec5SDimitry Andric CoalescingCandidateInfo &TargetRegion) const { 5340b57cec5SDimitry Andric if (!validateCandidates(SourceRegion, TargetRegion)) 5350b57cec5SDimitry Andric return false; 5360b57cec5SDimitry Andric 5370b57cec5SDimitry Andric // Walk through PHI nodes first and see if they force the merge into the 5380b57cec5SDimitry Andric // SourceRegion.BranchTargetBlock. 5390b57cec5SDimitry Andric for (MachineBasicBlock::iterator 5400b57cec5SDimitry Andric I = SourceRegion.BranchBlock->instr_begin(), 5410b57cec5SDimitry Andric E = SourceRegion.BranchBlock->getFirstNonPHI(); 5420b57cec5SDimitry Andric I != E; ++I) { 5430b57cec5SDimitry Andric for (auto &Def : I->defs()) 5440b57cec5SDimitry Andric for (auto &Use : MRI->use_instructions(Def.getReg())) { 5450b57cec5SDimitry Andric if (Use.isPHI() && Use.getParent() == SourceRegion.BranchTargetBlock) { 5460b57cec5SDimitry Andric LLVM_DEBUG(dbgs() 5470b57cec5SDimitry Andric << "PHI " << *I 5480b57cec5SDimitry Andric << " defines register used in another " 5490b57cec5SDimitry Andric "PHI within branch target block -- can't merge\n"); 5500b57cec5SDimitry Andric NumPHINotMoved++; 5510b57cec5SDimitry Andric return false; 5520b57cec5SDimitry Andric } 5530b57cec5SDimitry Andric if (Use.getParent() == SourceRegion.BranchBlock) { 5540b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "PHI " << *I 5550b57cec5SDimitry Andric << " defines register used in this " 5560b57cec5SDimitry Andric "block -- all must move down\n"); 5570b57cec5SDimitry Andric SourceRegion.MustMoveDown = true; 5580b57cec5SDimitry Andric } 5590b57cec5SDimitry Andric } 5600b57cec5SDimitry Andric } 5610b57cec5SDimitry Andric 5620b57cec5SDimitry Andric // Walk through the MI to see if they should be merged into 5630b57cec5SDimitry Andric // TargetRegion.BranchBlock (up) or SourceRegion.BranchTargetBlock (down) 5640b57cec5SDimitry Andric for (MachineBasicBlock::iterator 5650b57cec5SDimitry Andric I = SourceRegion.BranchBlock->getFirstNonPHI(), 5660b57cec5SDimitry Andric E = SourceRegion.BranchBlock->end(); 5670b57cec5SDimitry Andric I != E; ++I) { 5680b57cec5SDimitry Andric if (!canMoveToBeginning(*I, *SourceRegion.BranchTargetBlock)) { 5690b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Instruction " << *I 5700b57cec5SDimitry Andric << " cannot move down - must move up!\n"); 5710b57cec5SDimitry Andric SourceRegion.MustMoveUp = true; 5720b57cec5SDimitry Andric } 5730b57cec5SDimitry Andric if (!canMoveToEnd(*I, *TargetRegion.BranchBlock)) { 5740b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Instruction " << *I 5750b57cec5SDimitry Andric << " cannot move up - must move down!\n"); 5760b57cec5SDimitry Andric SourceRegion.MustMoveDown = true; 5770b57cec5SDimitry Andric } 5780b57cec5SDimitry Andric } 5790b57cec5SDimitry Andric 5800b57cec5SDimitry Andric return (SourceRegion.MustMoveUp && SourceRegion.MustMoveDown) ? false : true; 5810b57cec5SDimitry Andric } 5820b57cec5SDimitry Andric 5830b57cec5SDimitry Andric /// Merge the instructions from SourceRegion.BranchBlock, 5840b57cec5SDimitry Andric /// SourceRegion.BranchTargetBlock, and SourceRegion.FallThroughBlock into 5850b57cec5SDimitry Andric /// TargetRegion.BranchBlock, TargetRegion.BranchTargetBlock and 5860b57cec5SDimitry Andric /// TargetRegion.FallThroughBlock respectively. 5870b57cec5SDimitry Andric /// 5880b57cec5SDimitry Andric /// The successors for blocks in TargetRegion will be updated to use the 5890b57cec5SDimitry Andric /// successors from blocks in SourceRegion. Finally, the blocks in SourceRegion 5900b57cec5SDimitry Andric /// will be removed from the function. 5910b57cec5SDimitry Andric /// 5920b57cec5SDimitry Andric /// A region consists of a BranchBlock, a FallThroughBlock, and a 5930b57cec5SDimitry Andric /// BranchTargetBlock. Branch coalesce works on patterns where the 5940b57cec5SDimitry Andric /// TargetRegion's BranchTargetBlock must also be the SourceRegions's 5950b57cec5SDimitry Andric /// BranchBlock. 5960b57cec5SDimitry Andric /// 5970b57cec5SDimitry Andric /// Before mergeCandidates: 5980b57cec5SDimitry Andric /// 5990b57cec5SDimitry Andric /// +---------------------------+ 6000b57cec5SDimitry Andric /// | TargetRegion.BranchBlock | 6010b57cec5SDimitry Andric /// +---------------------------+ 6020b57cec5SDimitry Andric /// / | 6030b57cec5SDimitry Andric /// / +--------------------------------+ 6040b57cec5SDimitry Andric /// | | TargetRegion.FallThroughBlock | 6050b57cec5SDimitry Andric /// \ +--------------------------------+ 6060b57cec5SDimitry Andric /// \ | 6070b57cec5SDimitry Andric /// +----------------------------------+ 6080b57cec5SDimitry Andric /// | TargetRegion.BranchTargetBlock | 6090b57cec5SDimitry Andric /// | SourceRegion.BranchBlock | 6100b57cec5SDimitry Andric /// +----------------------------------+ 6110b57cec5SDimitry Andric /// / | 6120b57cec5SDimitry Andric /// / +--------------------------------+ 6130b57cec5SDimitry Andric /// | | SourceRegion.FallThroughBlock | 6140b57cec5SDimitry Andric /// \ +--------------------------------+ 6150b57cec5SDimitry Andric /// \ | 6160b57cec5SDimitry Andric /// +----------------------------------+ 6170b57cec5SDimitry Andric /// | SourceRegion.BranchTargetBlock | 6180b57cec5SDimitry Andric /// +----------------------------------+ 6190b57cec5SDimitry Andric /// 6200b57cec5SDimitry Andric /// After mergeCandidates: 6210b57cec5SDimitry Andric /// 6220b57cec5SDimitry Andric /// +-----------------------------+ 6230b57cec5SDimitry Andric /// | TargetRegion.BranchBlock | 6240b57cec5SDimitry Andric /// | SourceRegion.BranchBlock | 6250b57cec5SDimitry Andric /// +-----------------------------+ 6260b57cec5SDimitry Andric /// / | 6270b57cec5SDimitry Andric /// / +---------------------------------+ 6280b57cec5SDimitry Andric /// | | TargetRegion.FallThroughBlock | 6290b57cec5SDimitry Andric /// | | SourceRegion.FallThroughBlock | 6300b57cec5SDimitry Andric /// \ +---------------------------------+ 6310b57cec5SDimitry Andric /// \ | 6320b57cec5SDimitry Andric /// +----------------------------------+ 6330b57cec5SDimitry Andric /// | SourceRegion.BranchTargetBlock | 6340b57cec5SDimitry Andric /// +----------------------------------+ 6350b57cec5SDimitry Andric /// 6360b57cec5SDimitry Andric /// \param[in] SourceRegion The candidate to move blocks from 6370b57cec5SDimitry Andric /// \param[in] TargetRegion The candidate to move blocks to 6380b57cec5SDimitry Andric /// 6390b57cec5SDimitry Andric bool PPCBranchCoalescing::mergeCandidates(CoalescingCandidateInfo &SourceRegion, 6400b57cec5SDimitry Andric CoalescingCandidateInfo &TargetRegion) { 6410b57cec5SDimitry Andric 6420b57cec5SDimitry Andric if (SourceRegion.MustMoveUp && SourceRegion.MustMoveDown) { 6430b57cec5SDimitry Andric llvm_unreachable("Cannot have both MustMoveDown and MustMoveUp set!"); 6440b57cec5SDimitry Andric return false; 6450b57cec5SDimitry Andric } 6460b57cec5SDimitry Andric 6470b57cec5SDimitry Andric if (!validateCandidates(SourceRegion, TargetRegion)) 6480b57cec5SDimitry Andric return false; 6490b57cec5SDimitry Andric 6500b57cec5SDimitry Andric // Start the merging process by first handling the BranchBlock. 6510b57cec5SDimitry Andric // Move any PHIs in SourceRegion.BranchBlock down to the branch-taken block 6520b57cec5SDimitry Andric moveAndUpdatePHIs(SourceRegion.BranchBlock, SourceRegion.BranchTargetBlock); 6530b57cec5SDimitry Andric 6540b57cec5SDimitry Andric // Move remaining instructions in SourceRegion.BranchBlock into 6550b57cec5SDimitry Andric // TargetRegion.BranchBlock 6560b57cec5SDimitry Andric MachineBasicBlock::iterator firstInstr = 6570b57cec5SDimitry Andric SourceRegion.BranchBlock->getFirstNonPHI(); 6580b57cec5SDimitry Andric MachineBasicBlock::iterator lastInstr = 6590b57cec5SDimitry Andric SourceRegion.BranchBlock->getFirstTerminator(); 6600b57cec5SDimitry Andric 6610b57cec5SDimitry Andric MachineBasicBlock *Source = SourceRegion.MustMoveDown 6620b57cec5SDimitry Andric ? SourceRegion.BranchTargetBlock 6630b57cec5SDimitry Andric : TargetRegion.BranchBlock; 6640b57cec5SDimitry Andric 6650b57cec5SDimitry Andric MachineBasicBlock::iterator Target = 6660b57cec5SDimitry Andric SourceRegion.MustMoveDown 6670b57cec5SDimitry Andric ? SourceRegion.BranchTargetBlock->getFirstNonPHI() 6680b57cec5SDimitry Andric : TargetRegion.BranchBlock->getFirstTerminator(); 6690b57cec5SDimitry Andric 6700b57cec5SDimitry Andric Source->splice(Target, SourceRegion.BranchBlock, firstInstr, lastInstr); 6710b57cec5SDimitry Andric 6720b57cec5SDimitry Andric // Once PHI and instructions have been moved we need to clean up the 6730b57cec5SDimitry Andric // control flow. 6740b57cec5SDimitry Andric 6750b57cec5SDimitry Andric // Remove SourceRegion.FallThroughBlock before transferring successors of 6760b57cec5SDimitry Andric // SourceRegion.BranchBlock to TargetRegion.BranchBlock. 6770b57cec5SDimitry Andric SourceRegion.BranchBlock->removeSuccessor(SourceRegion.FallThroughBlock); 6780b57cec5SDimitry Andric TargetRegion.BranchBlock->transferSuccessorsAndUpdatePHIs( 6790b57cec5SDimitry Andric SourceRegion.BranchBlock); 6800b57cec5SDimitry Andric // Update branch in TargetRegion.BranchBlock to jump to 6810b57cec5SDimitry Andric // SourceRegion.BranchTargetBlock 6820b57cec5SDimitry Andric // In this case, TargetRegion.BranchTargetBlock == SourceRegion.BranchBlock. 6830b57cec5SDimitry Andric TargetRegion.BranchBlock->ReplaceUsesOfBlockWith( 6840b57cec5SDimitry Andric SourceRegion.BranchBlock, SourceRegion.BranchTargetBlock); 6850b57cec5SDimitry Andric // Remove the branch statement(s) in SourceRegion.BranchBlock 6860b57cec5SDimitry Andric MachineBasicBlock::iterator I = 6870b57cec5SDimitry Andric SourceRegion.BranchBlock->terminators().begin(); 6880b57cec5SDimitry Andric while (I != SourceRegion.BranchBlock->terminators().end()) { 6890b57cec5SDimitry Andric MachineInstr &CurrInst = *I; 6900b57cec5SDimitry Andric ++I; 6910b57cec5SDimitry Andric if (CurrInst.isBranch()) 6920b57cec5SDimitry Andric CurrInst.eraseFromParent(); 6930b57cec5SDimitry Andric } 6940b57cec5SDimitry Andric 6950b57cec5SDimitry Andric // Fall-through block should be empty since this is part of the condition 6960b57cec5SDimitry Andric // to coalesce the branches. 6970b57cec5SDimitry Andric assert(TargetRegion.FallThroughBlock->empty() && 6980b57cec5SDimitry Andric "FallThroughBlocks should be empty!"); 6990b57cec5SDimitry Andric 7000b57cec5SDimitry Andric // Transfer successor information and move PHIs down to the 7010b57cec5SDimitry Andric // branch-taken block. 7020b57cec5SDimitry Andric TargetRegion.FallThroughBlock->transferSuccessorsAndUpdatePHIs( 7030b57cec5SDimitry Andric SourceRegion.FallThroughBlock); 7040b57cec5SDimitry Andric TargetRegion.FallThroughBlock->removeSuccessor(SourceRegion.BranchBlock); 7050b57cec5SDimitry Andric 7060b57cec5SDimitry Andric // Remove the blocks from the function. 7070b57cec5SDimitry Andric assert(SourceRegion.BranchBlock->empty() && 7080b57cec5SDimitry Andric "Expecting branch block to be empty!"); 7090b57cec5SDimitry Andric SourceRegion.BranchBlock->eraseFromParent(); 7100b57cec5SDimitry Andric 7110b57cec5SDimitry Andric assert(SourceRegion.FallThroughBlock->empty() && 7120b57cec5SDimitry Andric "Expecting fall-through block to be empty!\n"); 7130b57cec5SDimitry Andric SourceRegion.FallThroughBlock->eraseFromParent(); 7140b57cec5SDimitry Andric 7150b57cec5SDimitry Andric NumBlocksCoalesced++; 7160b57cec5SDimitry Andric return true; 7170b57cec5SDimitry Andric } 7180b57cec5SDimitry Andric 7190b57cec5SDimitry Andric bool PPCBranchCoalescing::runOnMachineFunction(MachineFunction &MF) { 7200b57cec5SDimitry Andric 7210b57cec5SDimitry Andric if (skipFunction(MF.getFunction()) || MF.empty()) 7220b57cec5SDimitry Andric return false; 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric bool didSomething = false; 7250b57cec5SDimitry Andric 7260b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "******** Branch Coalescing ********\n"); 7270b57cec5SDimitry Andric initialize(MF); 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Function: "; MF.dump(); dbgs() << "\n"); 7300b57cec5SDimitry Andric 7310b57cec5SDimitry Andric CoalescingCandidateInfo Cand1, Cand2; 7320b57cec5SDimitry Andric // Walk over blocks and find candidates to merge 7330b57cec5SDimitry Andric // Continue trying to merge with the first candidate found, as long as merging 7340b57cec5SDimitry Andric // is successfull. 7350b57cec5SDimitry Andric for (MachineBasicBlock &MBB : MF) { 7360b57cec5SDimitry Andric bool MergedCandidates = false; 7370b57cec5SDimitry Andric do { 7380b57cec5SDimitry Andric MergedCandidates = false; 7390b57cec5SDimitry Andric Cand1.clear(); 7400b57cec5SDimitry Andric Cand2.clear(); 7410b57cec5SDimitry Andric 7420b57cec5SDimitry Andric Cand1.BranchBlock = &MBB; 7430b57cec5SDimitry Andric 7440b57cec5SDimitry Andric // If unable to coalesce the branch, then continue to next block 7450b57cec5SDimitry Andric if (!canCoalesceBranch(Cand1)) 7460b57cec5SDimitry Andric break; 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric Cand2.BranchBlock = Cand1.BranchTargetBlock; 7490b57cec5SDimitry Andric if (!canCoalesceBranch(Cand2)) 7500b57cec5SDimitry Andric break; 7510b57cec5SDimitry Andric 7520b57cec5SDimitry Andric // The branch-taken block of the second candidate should post-dominate the 753349cc55cSDimitry Andric // first candidate. 7540b57cec5SDimitry Andric assert(MPDT->dominates(Cand2.BranchTargetBlock, Cand1.BranchBlock) && 7550b57cec5SDimitry Andric "Branch-taken block should post-dominate first candidate"); 7560b57cec5SDimitry Andric 7570b57cec5SDimitry Andric if (!identicalOperands(Cand1.Cond, Cand2.Cond)) { 7580b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Blocks " << Cand1.BranchBlock->getNumber() 7590b57cec5SDimitry Andric << " and " << Cand2.BranchBlock->getNumber() 7600b57cec5SDimitry Andric << " have different branches\n"); 7610b57cec5SDimitry Andric break; 7620b57cec5SDimitry Andric } 7630b57cec5SDimitry Andric if (!canMerge(Cand2, Cand1)) { 7640b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Cannot merge blocks " 7650b57cec5SDimitry Andric << Cand1.BranchBlock->getNumber() << " and " 7660b57cec5SDimitry Andric << Cand2.BranchBlock->getNumber() << "\n"); 7670b57cec5SDimitry Andric NumBlocksNotCoalesced++; 7680b57cec5SDimitry Andric continue; 7690b57cec5SDimitry Andric } 7700b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Merging blocks " << Cand1.BranchBlock->getNumber() 7710b57cec5SDimitry Andric << " and " << Cand1.BranchTargetBlock->getNumber() 7720b57cec5SDimitry Andric << "\n"); 7730b57cec5SDimitry Andric MergedCandidates = mergeCandidates(Cand2, Cand1); 7740b57cec5SDimitry Andric if (MergedCandidates) 7750b57cec5SDimitry Andric didSomething = true; 7760b57cec5SDimitry Andric 7770b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Function after merging: "; MF.dump(); 7780b57cec5SDimitry Andric dbgs() << "\n"); 7790b57cec5SDimitry Andric } while (MergedCandidates); 7800b57cec5SDimitry Andric } 7810b57cec5SDimitry Andric 7820b57cec5SDimitry Andric #ifndef NDEBUG 7830b57cec5SDimitry Andric // Verify MF is still valid after branch coalescing 7840b57cec5SDimitry Andric if (didSomething) 7850b57cec5SDimitry Andric MF.verify(nullptr, "Error in code produced by branch coalescing"); 7860b57cec5SDimitry Andric #endif // NDEBUG 7870b57cec5SDimitry Andric 7880b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Finished Branch Coalescing\n"); 7890b57cec5SDimitry Andric return didSomething; 7900b57cec5SDimitry Andric } 791