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