xref: /openbsd-src/gnu/llvm/llvm/lib/Target/PowerPC/PPCReduceCRLogicals.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
109467b48Spatrick //===---- PPCReduceCRLogicals.cpp - Reduce CR Bit Logical operations ------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===---------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This pass aims to reduce the number of logical operations on bits in the CR
1009467b48Spatrick // register. These instructions have a fairly high latency and only a single
1109467b48Spatrick // pipeline at their disposal in modern PPC cores. Furthermore, they have a
1209467b48Spatrick // tendency to occur in fairly small blocks where there's little opportunity
1309467b48Spatrick // to hide the latency between the CR logical operation and its user.
1409467b48Spatrick //
1509467b48Spatrick //===---------------------------------------------------------------------===//
1609467b48Spatrick 
1709467b48Spatrick #include "PPC.h"
1809467b48Spatrick #include "PPCInstrInfo.h"
1909467b48Spatrick #include "PPCTargetMachine.h"
2009467b48Spatrick #include "llvm/ADT/Statistic.h"
2109467b48Spatrick #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
2209467b48Spatrick #include "llvm/CodeGen/MachineDominators.h"
2309467b48Spatrick #include "llvm/CodeGen/MachineFunctionPass.h"
2409467b48Spatrick #include "llvm/CodeGen/MachineInstrBuilder.h"
2509467b48Spatrick #include "llvm/CodeGen/MachineRegisterInfo.h"
2609467b48Spatrick #include "llvm/Config/llvm-config.h"
2709467b48Spatrick #include "llvm/InitializePasses.h"
2809467b48Spatrick #include "llvm/Support/Debug.h"
2909467b48Spatrick 
3009467b48Spatrick using namespace llvm;
3109467b48Spatrick 
3209467b48Spatrick #define DEBUG_TYPE "ppc-reduce-cr-ops"
3309467b48Spatrick 
3409467b48Spatrick STATISTIC(NumContainedSingleUseBinOps,
3509467b48Spatrick           "Number of single-use binary CR logical ops contained in a block");
3609467b48Spatrick STATISTIC(NumToSplitBlocks,
3709467b48Spatrick           "Number of binary CR logical ops that can be used to split blocks");
3809467b48Spatrick STATISTIC(TotalCRLogicals, "Number of CR logical ops.");
3909467b48Spatrick STATISTIC(TotalNullaryCRLogicals,
4009467b48Spatrick           "Number of nullary CR logical ops (CRSET/CRUNSET).");
4109467b48Spatrick STATISTIC(TotalUnaryCRLogicals, "Number of unary CR logical ops.");
4209467b48Spatrick STATISTIC(TotalBinaryCRLogicals, "Number of CR logical ops.");
4309467b48Spatrick STATISTIC(NumBlocksSplitOnBinaryCROp,
4409467b48Spatrick           "Number of blocks split on CR binary logical ops.");
4509467b48Spatrick STATISTIC(NumNotSplitIdenticalOperands,
4609467b48Spatrick           "Number of blocks not split due to operands being identical.");
4709467b48Spatrick STATISTIC(NumNotSplitChainCopies,
4809467b48Spatrick           "Number of blocks not split due to operands being chained copies.");
4909467b48Spatrick STATISTIC(NumNotSplitWrongOpcode,
5009467b48Spatrick           "Number of blocks not split due to the wrong opcode.");
5109467b48Spatrick 
5209467b48Spatrick /// Given a basic block \p Successor that potentially contains PHIs, this
5309467b48Spatrick /// function will look for any incoming values in the PHIs that are supposed to
5409467b48Spatrick /// be coming from \p OrigMBB but whose definition is actually in \p NewMBB.
5509467b48Spatrick /// Any such PHIs will be updated to reflect reality.
updatePHIs(MachineBasicBlock * Successor,MachineBasicBlock * OrigMBB,MachineBasicBlock * NewMBB,MachineRegisterInfo * MRI)5609467b48Spatrick static void updatePHIs(MachineBasicBlock *Successor, MachineBasicBlock *OrigMBB,
5709467b48Spatrick                        MachineBasicBlock *NewMBB, MachineRegisterInfo *MRI) {
5809467b48Spatrick   for (auto &MI : Successor->instrs()) {
5909467b48Spatrick     if (!MI.isPHI())
6009467b48Spatrick       continue;
6109467b48Spatrick     // This is a really ugly-looking loop, but it was pillaged directly from
6209467b48Spatrick     // MachineBasicBlock::transferSuccessorsAndUpdatePHIs().
6309467b48Spatrick     for (unsigned i = 2, e = MI.getNumOperands() + 1; i != e; i += 2) {
6409467b48Spatrick       MachineOperand &MO = MI.getOperand(i);
6509467b48Spatrick       if (MO.getMBB() == OrigMBB) {
6609467b48Spatrick         // Check if the instruction is actually defined in NewMBB.
6709467b48Spatrick         if (MI.getOperand(i - 1).isReg()) {
6809467b48Spatrick           MachineInstr *DefMI = MRI->getVRegDef(MI.getOperand(i - 1).getReg());
6909467b48Spatrick           if (DefMI->getParent() == NewMBB ||
7009467b48Spatrick               !OrigMBB->isSuccessor(Successor)) {
7109467b48Spatrick             MO.setMBB(NewMBB);
7209467b48Spatrick             break;
7309467b48Spatrick           }
7409467b48Spatrick         }
7509467b48Spatrick       }
7609467b48Spatrick     }
7709467b48Spatrick   }
7809467b48Spatrick }
7909467b48Spatrick 
8009467b48Spatrick /// Given a basic block \p Successor that potentially contains PHIs, this
8109467b48Spatrick /// function will look for PHIs that have an incoming value from \p OrigMBB
8209467b48Spatrick /// and will add the same incoming value from \p NewMBB.
8309467b48Spatrick /// NOTE: This should only be used if \p NewMBB is an immediate dominator of
8409467b48Spatrick /// \p OrigMBB.
addIncomingValuesToPHIs(MachineBasicBlock * Successor,MachineBasicBlock * OrigMBB,MachineBasicBlock * NewMBB,MachineRegisterInfo * MRI)8509467b48Spatrick static void addIncomingValuesToPHIs(MachineBasicBlock *Successor,
8609467b48Spatrick                                     MachineBasicBlock *OrigMBB,
8709467b48Spatrick                                     MachineBasicBlock *NewMBB,
8809467b48Spatrick                                     MachineRegisterInfo *MRI) {
8909467b48Spatrick   assert(OrigMBB->isSuccessor(NewMBB) &&
9009467b48Spatrick          "NewMBB must be a successor of OrigMBB");
9109467b48Spatrick   for (auto &MI : Successor->instrs()) {
9209467b48Spatrick     if (!MI.isPHI())
9309467b48Spatrick       continue;
9409467b48Spatrick     // This is a really ugly-looking loop, but it was pillaged directly from
9509467b48Spatrick     // MachineBasicBlock::transferSuccessorsAndUpdatePHIs().
9609467b48Spatrick     for (unsigned i = 2, e = MI.getNumOperands() + 1; i != e; i += 2) {
9709467b48Spatrick       MachineOperand &MO = MI.getOperand(i);
9809467b48Spatrick       if (MO.getMBB() == OrigMBB) {
9909467b48Spatrick         MachineInstrBuilder MIB(*MI.getParent()->getParent(), &MI);
10009467b48Spatrick         MIB.addReg(MI.getOperand(i - 1).getReg()).addMBB(NewMBB);
10109467b48Spatrick         break;
10209467b48Spatrick       }
10309467b48Spatrick     }
10409467b48Spatrick   }
10509467b48Spatrick }
10609467b48Spatrick 
10709467b48Spatrick struct BlockSplitInfo {
10809467b48Spatrick   MachineInstr *OrigBranch;
10909467b48Spatrick   MachineInstr *SplitBefore;
11009467b48Spatrick   MachineInstr *SplitCond;
11109467b48Spatrick   bool InvertNewBranch;
11209467b48Spatrick   bool InvertOrigBranch;
11309467b48Spatrick   bool BranchToFallThrough;
11409467b48Spatrick   const MachineBranchProbabilityInfo *MBPI;
11509467b48Spatrick   MachineInstr *MIToDelete;
11609467b48Spatrick   MachineInstr *NewCond;
allInstrsInSameMBBBlockSplitInfo11709467b48Spatrick   bool allInstrsInSameMBB() {
11809467b48Spatrick     if (!OrigBranch || !SplitBefore || !SplitCond)
11909467b48Spatrick       return false;
12009467b48Spatrick     MachineBasicBlock *MBB = OrigBranch->getParent();
12109467b48Spatrick     if (SplitBefore->getParent() != MBB || SplitCond->getParent() != MBB)
12209467b48Spatrick       return false;
12309467b48Spatrick     if (MIToDelete && MIToDelete->getParent() != MBB)
12409467b48Spatrick       return false;
12509467b48Spatrick     if (NewCond && NewCond->getParent() != MBB)
12609467b48Spatrick       return false;
12709467b48Spatrick     return true;
12809467b48Spatrick   }
12909467b48Spatrick };
13009467b48Spatrick 
13109467b48Spatrick /// Splits a MachineBasicBlock to branch before \p SplitBefore. The original
13209467b48Spatrick /// branch is \p OrigBranch. The target of the new branch can either be the same
13309467b48Spatrick /// as the target of the original branch or the fallthrough successor of the
13409467b48Spatrick /// original block as determined by \p BranchToFallThrough. The branch
13509467b48Spatrick /// conditions will be inverted according to \p InvertNewBranch and
13609467b48Spatrick /// \p InvertOrigBranch. If an instruction that previously fed the branch is to
13709467b48Spatrick /// be deleted, it is provided in \p MIToDelete and \p NewCond will be used as
13809467b48Spatrick /// the branch condition. The branch probabilities will be set if the
13909467b48Spatrick /// MachineBranchProbabilityInfo isn't null.
splitMBB(BlockSplitInfo & BSI)14009467b48Spatrick static bool splitMBB(BlockSplitInfo &BSI) {
14109467b48Spatrick   assert(BSI.allInstrsInSameMBB() &&
14209467b48Spatrick          "All instructions must be in the same block.");
14309467b48Spatrick 
14409467b48Spatrick   MachineBasicBlock *ThisMBB = BSI.OrigBranch->getParent();
14509467b48Spatrick   MachineFunction *MF = ThisMBB->getParent();
14609467b48Spatrick   MachineRegisterInfo *MRI = &MF->getRegInfo();
14709467b48Spatrick   assert(MRI->isSSA() && "Can only do this while the function is in SSA form.");
14809467b48Spatrick   if (ThisMBB->succ_size() != 2) {
14909467b48Spatrick     LLVM_DEBUG(
15009467b48Spatrick         dbgs() << "Don't know how to handle blocks that don't have exactly"
15109467b48Spatrick                << " two successors.\n");
15209467b48Spatrick     return false;
15309467b48Spatrick   }
15409467b48Spatrick 
15509467b48Spatrick   const PPCInstrInfo *TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo();
15609467b48Spatrick   unsigned OrigBROpcode = BSI.OrigBranch->getOpcode();
15709467b48Spatrick   unsigned InvertedOpcode =
15809467b48Spatrick       OrigBROpcode == PPC::BC
15909467b48Spatrick           ? PPC::BCn
16009467b48Spatrick           : OrigBROpcode == PPC::BCn
16109467b48Spatrick                 ? PPC::BC
16209467b48Spatrick                 : OrigBROpcode == PPC::BCLR ? PPC::BCLRn : PPC::BCLR;
16309467b48Spatrick   unsigned NewBROpcode = BSI.InvertNewBranch ? InvertedOpcode : OrigBROpcode;
16409467b48Spatrick   MachineBasicBlock *OrigTarget = BSI.OrigBranch->getOperand(1).getMBB();
16509467b48Spatrick   MachineBasicBlock *OrigFallThrough = OrigTarget == *ThisMBB->succ_begin()
16609467b48Spatrick                                            ? *ThisMBB->succ_rbegin()
16709467b48Spatrick                                            : *ThisMBB->succ_begin();
16809467b48Spatrick   MachineBasicBlock *NewBRTarget =
16909467b48Spatrick       BSI.BranchToFallThrough ? OrigFallThrough : OrigTarget;
17009467b48Spatrick 
17109467b48Spatrick   // It's impossible to know the precise branch probability after the split.
17209467b48Spatrick   // But it still needs to be reasonable, the whole probability to original
17309467b48Spatrick   // targets should not be changed.
17409467b48Spatrick   // After split NewBRTarget will get two incoming edges. Assume P0 is the
17509467b48Spatrick   // original branch probability to NewBRTarget, P1 and P2 are new branch
17609467b48Spatrick   // probabilies to NewBRTarget after split. If the two edge frequencies are
17709467b48Spatrick   // same, then
17809467b48Spatrick   //      F * P1 = F * P0 / 2            ==>  P1 = P0 / 2
17909467b48Spatrick   //      F * (1 - P1) * P2 = F * P1     ==>  P2 = P1 / (1 - P1)
18009467b48Spatrick   BranchProbability ProbToNewTarget, ProbFallThrough;     // Prob for new Br.
18109467b48Spatrick   BranchProbability ProbOrigTarget, ProbOrigFallThrough;  // Prob for orig Br.
18209467b48Spatrick   ProbToNewTarget = ProbFallThrough = BranchProbability::getUnknown();
18309467b48Spatrick   ProbOrigTarget = ProbOrigFallThrough = BranchProbability::getUnknown();
18409467b48Spatrick   if (BSI.MBPI) {
18509467b48Spatrick     if (BSI.BranchToFallThrough) {
18609467b48Spatrick       ProbToNewTarget = BSI.MBPI->getEdgeProbability(ThisMBB, OrigFallThrough) / 2;
18709467b48Spatrick       ProbFallThrough = ProbToNewTarget.getCompl();
18809467b48Spatrick       ProbOrigFallThrough = ProbToNewTarget / ProbToNewTarget.getCompl();
18909467b48Spatrick       ProbOrigTarget = ProbOrigFallThrough.getCompl();
19009467b48Spatrick     } else {
19109467b48Spatrick       ProbToNewTarget = BSI.MBPI->getEdgeProbability(ThisMBB, OrigTarget) / 2;
19209467b48Spatrick       ProbFallThrough = ProbToNewTarget.getCompl();
19309467b48Spatrick       ProbOrigTarget = ProbToNewTarget / ProbToNewTarget.getCompl();
19409467b48Spatrick       ProbOrigFallThrough = ProbOrigTarget.getCompl();
19509467b48Spatrick     }
19609467b48Spatrick   }
19709467b48Spatrick 
19809467b48Spatrick   // Create a new basic block.
19909467b48Spatrick   MachineBasicBlock::iterator InsertPoint = BSI.SplitBefore;
20009467b48Spatrick   const BasicBlock *LLVM_BB = ThisMBB->getBasicBlock();
20109467b48Spatrick   MachineFunction::iterator It = ThisMBB->getIterator();
20209467b48Spatrick   MachineBasicBlock *NewMBB = MF->CreateMachineBasicBlock(LLVM_BB);
20309467b48Spatrick   MF->insert(++It, NewMBB);
20409467b48Spatrick 
20509467b48Spatrick   // Move everything after SplitBefore into the new block.
20609467b48Spatrick   NewMBB->splice(NewMBB->end(), ThisMBB, InsertPoint, ThisMBB->end());
20709467b48Spatrick   NewMBB->transferSuccessors(ThisMBB);
20809467b48Spatrick   if (!ProbOrigTarget.isUnknown()) {
20973471bf0Spatrick     auto MBBI = find(NewMBB->successors(), OrigTarget);
21009467b48Spatrick     NewMBB->setSuccProbability(MBBI, ProbOrigTarget);
21173471bf0Spatrick     MBBI = find(NewMBB->successors(), OrigFallThrough);
21209467b48Spatrick     NewMBB->setSuccProbability(MBBI, ProbOrigFallThrough);
21309467b48Spatrick   }
21409467b48Spatrick 
21509467b48Spatrick   // Add the two successors to ThisMBB.
21609467b48Spatrick   ThisMBB->addSuccessor(NewBRTarget, ProbToNewTarget);
21709467b48Spatrick   ThisMBB->addSuccessor(NewMBB, ProbFallThrough);
21809467b48Spatrick 
21909467b48Spatrick   // Add the branches to ThisMBB.
22009467b48Spatrick   BuildMI(*ThisMBB, ThisMBB->end(), BSI.SplitBefore->getDebugLoc(),
22109467b48Spatrick           TII->get(NewBROpcode))
22209467b48Spatrick       .addReg(BSI.SplitCond->getOperand(0).getReg())
22309467b48Spatrick       .addMBB(NewBRTarget);
22409467b48Spatrick   BuildMI(*ThisMBB, ThisMBB->end(), BSI.SplitBefore->getDebugLoc(),
22509467b48Spatrick           TII->get(PPC::B))
22609467b48Spatrick       .addMBB(NewMBB);
22709467b48Spatrick   if (BSI.MIToDelete)
22809467b48Spatrick     BSI.MIToDelete->eraseFromParent();
22909467b48Spatrick 
23009467b48Spatrick   // Change the condition on the original branch and invert it if requested.
23109467b48Spatrick   auto FirstTerminator = NewMBB->getFirstTerminator();
23209467b48Spatrick   if (BSI.NewCond) {
23309467b48Spatrick     assert(FirstTerminator->getOperand(0).isReg() &&
23409467b48Spatrick            "Can't update condition of unconditional branch.");
23509467b48Spatrick     FirstTerminator->getOperand(0).setReg(BSI.NewCond->getOperand(0).getReg());
23609467b48Spatrick   }
23709467b48Spatrick   if (BSI.InvertOrigBranch)
23809467b48Spatrick     FirstTerminator->setDesc(TII->get(InvertedOpcode));
23909467b48Spatrick 
24009467b48Spatrick   // If any of the PHIs in the successors of NewMBB reference values that
24109467b48Spatrick   // now come from NewMBB, they need to be updated.
24209467b48Spatrick   for (auto *Succ : NewMBB->successors()) {
24309467b48Spatrick     updatePHIs(Succ, ThisMBB, NewMBB, MRI);
24409467b48Spatrick   }
24509467b48Spatrick   addIncomingValuesToPHIs(NewBRTarget, ThisMBB, NewMBB, MRI);
24609467b48Spatrick 
24709467b48Spatrick   LLVM_DEBUG(dbgs() << "After splitting, ThisMBB:\n"; ThisMBB->dump());
24809467b48Spatrick   LLVM_DEBUG(dbgs() << "NewMBB:\n"; NewMBB->dump());
24909467b48Spatrick   LLVM_DEBUG(dbgs() << "New branch-to block:\n"; NewBRTarget->dump());
25009467b48Spatrick   return true;
25109467b48Spatrick }
25209467b48Spatrick 
isBinary(MachineInstr & MI)25309467b48Spatrick static bool isBinary(MachineInstr &MI) {
25409467b48Spatrick   return MI.getNumOperands() == 3;
25509467b48Spatrick }
25609467b48Spatrick 
isNullary(MachineInstr & MI)25709467b48Spatrick static bool isNullary(MachineInstr &MI) {
25809467b48Spatrick   return MI.getNumOperands() == 1;
25909467b48Spatrick }
26009467b48Spatrick 
26109467b48Spatrick /// Given a CR logical operation \p CROp, branch opcode \p BROp as well as
26209467b48Spatrick /// a flag to indicate if the first operand of \p CROp is used as the
26309467b48Spatrick /// SplitBefore operand, determines whether either of the branches are to be
26409467b48Spatrick /// inverted as well as whether the new target should be the original
26509467b48Spatrick /// fall-through block.
26609467b48Spatrick static void
computeBranchTargetAndInversion(unsigned CROp,unsigned BROp,bool UsingDef1,bool & InvertNewBranch,bool & InvertOrigBranch,bool & TargetIsFallThrough)26709467b48Spatrick computeBranchTargetAndInversion(unsigned CROp, unsigned BROp, bool UsingDef1,
26809467b48Spatrick                                 bool &InvertNewBranch, bool &InvertOrigBranch,
26909467b48Spatrick                                 bool &TargetIsFallThrough) {
27009467b48Spatrick   // The conditions under which each of the output operands should be [un]set
27109467b48Spatrick   // can certainly be written much more concisely with just 3 if statements or
27209467b48Spatrick   // ternary expressions. However, this provides a much clearer overview to the
27309467b48Spatrick   // reader as to what is set for each <CROp, BROp, OpUsed> combination.
27409467b48Spatrick   if (BROp == PPC::BC || BROp == PPC::BCLR) {
27509467b48Spatrick     // Regular branches.
27609467b48Spatrick     switch (CROp) {
27709467b48Spatrick     default:
27809467b48Spatrick       llvm_unreachable("Don't know how to handle this CR logical.");
27909467b48Spatrick     case PPC::CROR:
28009467b48Spatrick       InvertNewBranch = false;
28109467b48Spatrick       InvertOrigBranch = false;
28209467b48Spatrick       TargetIsFallThrough = false;
28309467b48Spatrick       return;
28409467b48Spatrick     case PPC::CRAND:
28509467b48Spatrick       InvertNewBranch = true;
28609467b48Spatrick       InvertOrigBranch = false;
28709467b48Spatrick       TargetIsFallThrough = true;
28809467b48Spatrick       return;
28909467b48Spatrick     case PPC::CRNAND:
29009467b48Spatrick       InvertNewBranch = true;
29109467b48Spatrick       InvertOrigBranch = true;
29209467b48Spatrick       TargetIsFallThrough = false;
29309467b48Spatrick       return;
29409467b48Spatrick     case PPC::CRNOR:
29509467b48Spatrick       InvertNewBranch = false;
29609467b48Spatrick       InvertOrigBranch = true;
29709467b48Spatrick       TargetIsFallThrough = true;
29809467b48Spatrick       return;
29909467b48Spatrick     case PPC::CRORC:
30009467b48Spatrick       InvertNewBranch = UsingDef1;
30109467b48Spatrick       InvertOrigBranch = !UsingDef1;
30209467b48Spatrick       TargetIsFallThrough = false;
30309467b48Spatrick       return;
30409467b48Spatrick     case PPC::CRANDC:
30509467b48Spatrick       InvertNewBranch = !UsingDef1;
30609467b48Spatrick       InvertOrigBranch = !UsingDef1;
30709467b48Spatrick       TargetIsFallThrough = true;
30809467b48Spatrick       return;
30909467b48Spatrick     }
31009467b48Spatrick   } else if (BROp == PPC::BCn || BROp == PPC::BCLRn) {
31109467b48Spatrick     // Negated branches.
31209467b48Spatrick     switch (CROp) {
31309467b48Spatrick     default:
31409467b48Spatrick       llvm_unreachable("Don't know how to handle this CR logical.");
31509467b48Spatrick     case PPC::CROR:
31609467b48Spatrick       InvertNewBranch = true;
31709467b48Spatrick       InvertOrigBranch = false;
31809467b48Spatrick       TargetIsFallThrough = true;
31909467b48Spatrick       return;
32009467b48Spatrick     case PPC::CRAND:
32109467b48Spatrick       InvertNewBranch = false;
32209467b48Spatrick       InvertOrigBranch = false;
32309467b48Spatrick       TargetIsFallThrough = false;
32409467b48Spatrick       return;
32509467b48Spatrick     case PPC::CRNAND:
32609467b48Spatrick       InvertNewBranch = false;
32709467b48Spatrick       InvertOrigBranch = true;
32809467b48Spatrick       TargetIsFallThrough = true;
32909467b48Spatrick       return;
33009467b48Spatrick     case PPC::CRNOR:
33109467b48Spatrick       InvertNewBranch = true;
33209467b48Spatrick       InvertOrigBranch = true;
33309467b48Spatrick       TargetIsFallThrough = false;
33409467b48Spatrick       return;
33509467b48Spatrick     case PPC::CRORC:
33609467b48Spatrick       InvertNewBranch = !UsingDef1;
33709467b48Spatrick       InvertOrigBranch = !UsingDef1;
33809467b48Spatrick       TargetIsFallThrough = true;
33909467b48Spatrick       return;
34009467b48Spatrick     case PPC::CRANDC:
34109467b48Spatrick       InvertNewBranch = UsingDef1;
34209467b48Spatrick       InvertOrigBranch = !UsingDef1;
34309467b48Spatrick       TargetIsFallThrough = false;
34409467b48Spatrick       return;
34509467b48Spatrick     }
34609467b48Spatrick   } else
34709467b48Spatrick     llvm_unreachable("Don't know how to handle this branch.");
34809467b48Spatrick }
34909467b48Spatrick 
35009467b48Spatrick namespace {
35109467b48Spatrick 
35209467b48Spatrick class PPCReduceCRLogicals : public MachineFunctionPass {
35309467b48Spatrick 
35409467b48Spatrick public:
35509467b48Spatrick   static char ID;
35609467b48Spatrick   struct CRLogicalOpInfo {
35709467b48Spatrick     MachineInstr *MI;
35809467b48Spatrick     // FIXME: If chains of copies are to be handled, this should be a vector.
35909467b48Spatrick     std::pair<MachineInstr*, MachineInstr*> CopyDefs;
36009467b48Spatrick     std::pair<MachineInstr*, MachineInstr*> TrueDefs;
36109467b48Spatrick     unsigned IsBinary : 1;
36209467b48Spatrick     unsigned IsNullary : 1;
36309467b48Spatrick     unsigned ContainedInBlock : 1;
36409467b48Spatrick     unsigned FeedsISEL : 1;
36509467b48Spatrick     unsigned FeedsBR : 1;
36609467b48Spatrick     unsigned FeedsLogical : 1;
36709467b48Spatrick     unsigned SingleUse : 1;
36809467b48Spatrick     unsigned DefsSingleUse : 1;
36909467b48Spatrick     unsigned SubregDef1;
37009467b48Spatrick     unsigned SubregDef2;
CRLogicalOpInfo__anon8d2f284a0111::PPCReduceCRLogicals::CRLogicalOpInfo37109467b48Spatrick     CRLogicalOpInfo() : MI(nullptr), IsBinary(0), IsNullary(0),
37209467b48Spatrick                         ContainedInBlock(0), FeedsISEL(0), FeedsBR(0),
37309467b48Spatrick                         FeedsLogical(0), SingleUse(0), DefsSingleUse(1),
37409467b48Spatrick                         SubregDef1(0), SubregDef2(0) { }
37509467b48Spatrick     void dump();
37609467b48Spatrick   };
37709467b48Spatrick 
37809467b48Spatrick private:
37909467b48Spatrick   const PPCInstrInfo *TII = nullptr;
38009467b48Spatrick   MachineFunction *MF = nullptr;
38109467b48Spatrick   MachineRegisterInfo *MRI = nullptr;
38209467b48Spatrick   const MachineBranchProbabilityInfo *MBPI = nullptr;
38309467b48Spatrick 
38409467b48Spatrick   // A vector to contain all the CR logical operations
38509467b48Spatrick   SmallVector<CRLogicalOpInfo, 16> AllCRLogicalOps;
38609467b48Spatrick   void initialize(MachineFunction &MFParm);
38709467b48Spatrick   void collectCRLogicals();
38809467b48Spatrick   bool handleCROp(unsigned Idx);
38909467b48Spatrick   bool splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI);
isCRLogical(MachineInstr & MI)39009467b48Spatrick   static bool isCRLogical(MachineInstr &MI) {
39109467b48Spatrick     unsigned Opc = MI.getOpcode();
39209467b48Spatrick     return Opc == PPC::CRAND || Opc == PPC::CRNAND || Opc == PPC::CROR ||
393*d415bd75Srobert            Opc == PPC::CRXOR || Opc == PPC::CRNOR || Opc == PPC::CRNOT ||
394*d415bd75Srobert            Opc == PPC::CREQV || Opc == PPC::CRANDC || Opc == PPC::CRORC ||
395*d415bd75Srobert            Opc == PPC::CRSET || Opc == PPC::CRUNSET || Opc == PPC::CR6SET ||
396*d415bd75Srobert            Opc == PPC::CR6UNSET;
39709467b48Spatrick   }
simplifyCode()39809467b48Spatrick   bool simplifyCode() {
39909467b48Spatrick     bool Changed = false;
40009467b48Spatrick     // Not using a range-based for loop here as the vector may grow while being
40109467b48Spatrick     // operated on.
40209467b48Spatrick     for (unsigned i = 0; i < AllCRLogicalOps.size(); i++)
40309467b48Spatrick       Changed |= handleCROp(i);
40409467b48Spatrick     return Changed;
40509467b48Spatrick   }
40609467b48Spatrick 
40709467b48Spatrick public:
PPCReduceCRLogicals()40809467b48Spatrick   PPCReduceCRLogicals() : MachineFunctionPass(ID) {
40909467b48Spatrick     initializePPCReduceCRLogicalsPass(*PassRegistry::getPassRegistry());
41009467b48Spatrick   }
41109467b48Spatrick 
41209467b48Spatrick   MachineInstr *lookThroughCRCopy(unsigned Reg, unsigned &Subreg,
41309467b48Spatrick                                   MachineInstr *&CpDef);
runOnMachineFunction(MachineFunction & MF)41409467b48Spatrick   bool runOnMachineFunction(MachineFunction &MF) override {
41509467b48Spatrick     if (skipFunction(MF.getFunction()))
41609467b48Spatrick       return false;
41709467b48Spatrick 
41809467b48Spatrick     // If the subtarget doesn't use CR bits, there's nothing to do.
41909467b48Spatrick     const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
42009467b48Spatrick     if (!STI.useCRBits())
42109467b48Spatrick       return false;
42209467b48Spatrick 
42309467b48Spatrick     initialize(MF);
42409467b48Spatrick     collectCRLogicals();
42509467b48Spatrick     return simplifyCode();
42609467b48Spatrick   }
42709467b48Spatrick   CRLogicalOpInfo createCRLogicalOpInfo(MachineInstr &MI);
getAnalysisUsage(AnalysisUsage & AU) const42809467b48Spatrick   void getAnalysisUsage(AnalysisUsage &AU) const override {
42909467b48Spatrick     AU.addRequired<MachineBranchProbabilityInfo>();
43009467b48Spatrick     AU.addRequired<MachineDominatorTree>();
43109467b48Spatrick     MachineFunctionPass::getAnalysisUsage(AU);
43209467b48Spatrick   }
43309467b48Spatrick };
43409467b48Spatrick 
43509467b48Spatrick #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
dump()43609467b48Spatrick LLVM_DUMP_METHOD void PPCReduceCRLogicals::CRLogicalOpInfo::dump() {
43709467b48Spatrick   dbgs() << "CRLogicalOpMI: ";
43809467b48Spatrick   MI->dump();
43909467b48Spatrick   dbgs() << "IsBinary: " << IsBinary << ", FeedsISEL: " << FeedsISEL;
44009467b48Spatrick   dbgs() << ", FeedsBR: " << FeedsBR << ", FeedsLogical: ";
44109467b48Spatrick   dbgs() << FeedsLogical << ", SingleUse: " << SingleUse;
44209467b48Spatrick   dbgs() << ", DefsSingleUse: " << DefsSingleUse;
44309467b48Spatrick   dbgs() << ", SubregDef1: " << SubregDef1 << ", SubregDef2: ";
44409467b48Spatrick   dbgs() << SubregDef2 << ", ContainedInBlock: " << ContainedInBlock;
44509467b48Spatrick   if (!IsNullary) {
44609467b48Spatrick     dbgs() << "\nDefs:\n";
44709467b48Spatrick     TrueDefs.first->dump();
44809467b48Spatrick   }
44909467b48Spatrick   if (IsBinary)
45009467b48Spatrick     TrueDefs.second->dump();
45109467b48Spatrick   dbgs() << "\n";
45209467b48Spatrick   if (CopyDefs.first) {
45309467b48Spatrick     dbgs() << "CopyDef1: ";
45409467b48Spatrick     CopyDefs.first->dump();
45509467b48Spatrick   }
45609467b48Spatrick   if (CopyDefs.second) {
45709467b48Spatrick     dbgs() << "CopyDef2: ";
45809467b48Spatrick     CopyDefs.second->dump();
45909467b48Spatrick   }
46009467b48Spatrick }
46109467b48Spatrick #endif
46209467b48Spatrick 
46309467b48Spatrick PPCReduceCRLogicals::CRLogicalOpInfo
createCRLogicalOpInfo(MachineInstr & MIParam)46409467b48Spatrick PPCReduceCRLogicals::createCRLogicalOpInfo(MachineInstr &MIParam) {
46509467b48Spatrick   CRLogicalOpInfo Ret;
46609467b48Spatrick   Ret.MI = &MIParam;
46709467b48Spatrick   // Get the defs
46809467b48Spatrick   if (isNullary(MIParam)) {
46909467b48Spatrick     Ret.IsNullary = 1;
47009467b48Spatrick     Ret.TrueDefs = std::make_pair(nullptr, nullptr);
47109467b48Spatrick     Ret.CopyDefs = std::make_pair(nullptr, nullptr);
47209467b48Spatrick   } else {
47309467b48Spatrick     MachineInstr *Def1 = lookThroughCRCopy(MIParam.getOperand(1).getReg(),
47409467b48Spatrick                                            Ret.SubregDef1, Ret.CopyDefs.first);
47509467b48Spatrick     assert(Def1 && "Must be able to find a definition of operand 1.");
47609467b48Spatrick     Ret.DefsSingleUse &=
47709467b48Spatrick       MRI->hasOneNonDBGUse(Def1->getOperand(0).getReg());
47809467b48Spatrick     Ret.DefsSingleUse &=
47909467b48Spatrick       MRI->hasOneNonDBGUse(Ret.CopyDefs.first->getOperand(0).getReg());
48009467b48Spatrick     if (isBinary(MIParam)) {
48109467b48Spatrick       Ret.IsBinary = 1;
48209467b48Spatrick       MachineInstr *Def2 = lookThroughCRCopy(MIParam.getOperand(2).getReg(),
48309467b48Spatrick                                              Ret.SubregDef2,
48409467b48Spatrick                                              Ret.CopyDefs.second);
48509467b48Spatrick       assert(Def2 && "Must be able to find a definition of operand 2.");
48609467b48Spatrick       Ret.DefsSingleUse &=
48709467b48Spatrick         MRI->hasOneNonDBGUse(Def2->getOperand(0).getReg());
48809467b48Spatrick       Ret.DefsSingleUse &=
48909467b48Spatrick         MRI->hasOneNonDBGUse(Ret.CopyDefs.second->getOperand(0).getReg());
49009467b48Spatrick       Ret.TrueDefs = std::make_pair(Def1, Def2);
49109467b48Spatrick     } else {
49209467b48Spatrick       Ret.TrueDefs = std::make_pair(Def1, nullptr);
49309467b48Spatrick       Ret.CopyDefs.second = nullptr;
49409467b48Spatrick     }
49509467b48Spatrick   }
49609467b48Spatrick 
49709467b48Spatrick   Ret.ContainedInBlock = 1;
49809467b48Spatrick   // Get the uses
49909467b48Spatrick   for (MachineInstr &UseMI :
50009467b48Spatrick        MRI->use_nodbg_instructions(MIParam.getOperand(0).getReg())) {
50109467b48Spatrick     unsigned Opc = UseMI.getOpcode();
50209467b48Spatrick     if (Opc == PPC::ISEL || Opc == PPC::ISEL8)
50309467b48Spatrick       Ret.FeedsISEL = 1;
50409467b48Spatrick     if (Opc == PPC::BC || Opc == PPC::BCn || Opc == PPC::BCLR ||
50509467b48Spatrick         Opc == PPC::BCLRn)
50609467b48Spatrick       Ret.FeedsBR = 1;
50709467b48Spatrick     Ret.FeedsLogical = isCRLogical(UseMI);
50809467b48Spatrick     if (UseMI.getParent() != MIParam.getParent())
50909467b48Spatrick       Ret.ContainedInBlock = 0;
51009467b48Spatrick   }
51109467b48Spatrick   Ret.SingleUse = MRI->hasOneNonDBGUse(MIParam.getOperand(0).getReg()) ? 1 : 0;
51209467b48Spatrick 
51309467b48Spatrick   // We now know whether all the uses of the CR logical are in the same block.
51409467b48Spatrick   if (!Ret.IsNullary) {
51509467b48Spatrick     Ret.ContainedInBlock &=
51609467b48Spatrick       (MIParam.getParent() == Ret.TrueDefs.first->getParent());
51709467b48Spatrick     if (Ret.IsBinary)
51809467b48Spatrick       Ret.ContainedInBlock &=
51909467b48Spatrick         (MIParam.getParent() == Ret.TrueDefs.second->getParent());
52009467b48Spatrick   }
52109467b48Spatrick   LLVM_DEBUG(Ret.dump());
52209467b48Spatrick   if (Ret.IsBinary && Ret.ContainedInBlock && Ret.SingleUse) {
52309467b48Spatrick     NumContainedSingleUseBinOps++;
52409467b48Spatrick     if (Ret.FeedsBR && Ret.DefsSingleUse)
52509467b48Spatrick       NumToSplitBlocks++;
52609467b48Spatrick   }
52709467b48Spatrick   return Ret;
52809467b48Spatrick }
52909467b48Spatrick 
53009467b48Spatrick /// Looks through a COPY instruction to the actual definition of the CR-bit
53109467b48Spatrick /// register and returns the instruction that defines it.
53209467b48Spatrick /// FIXME: This currently handles what is by-far the most common case:
53309467b48Spatrick /// an instruction that defines a CR field followed by a single copy of a bit
53409467b48Spatrick /// from that field into a virtual register. If chains of copies need to be
53509467b48Spatrick /// handled, this should have a loop until a non-copy instruction is found.
lookThroughCRCopy(unsigned Reg,unsigned & Subreg,MachineInstr * & CpDef)53609467b48Spatrick MachineInstr *PPCReduceCRLogicals::lookThroughCRCopy(unsigned Reg,
53709467b48Spatrick                                                      unsigned &Subreg,
53809467b48Spatrick                                                      MachineInstr *&CpDef) {
53909467b48Spatrick   Subreg = -1;
54009467b48Spatrick   if (!Register::isVirtualRegister(Reg))
54109467b48Spatrick     return nullptr;
54209467b48Spatrick   MachineInstr *Copy = MRI->getVRegDef(Reg);
54309467b48Spatrick   CpDef = Copy;
54409467b48Spatrick   if (!Copy->isCopy())
54509467b48Spatrick     return Copy;
54609467b48Spatrick   Register CopySrc = Copy->getOperand(1).getReg();
54709467b48Spatrick   Subreg = Copy->getOperand(1).getSubReg();
548*d415bd75Srobert   if (!CopySrc.isVirtual()) {
54909467b48Spatrick     const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
55009467b48Spatrick     // Set the Subreg
55109467b48Spatrick     if (CopySrc == PPC::CR0EQ || CopySrc == PPC::CR6EQ)
55209467b48Spatrick       Subreg = PPC::sub_eq;
55309467b48Spatrick     if (CopySrc == PPC::CR0LT || CopySrc == PPC::CR6LT)
55409467b48Spatrick       Subreg = PPC::sub_lt;
55509467b48Spatrick     if (CopySrc == PPC::CR0GT || CopySrc == PPC::CR6GT)
55609467b48Spatrick       Subreg = PPC::sub_gt;
55709467b48Spatrick     if (CopySrc == PPC::CR0UN || CopySrc == PPC::CR6UN)
55809467b48Spatrick       Subreg = PPC::sub_un;
55909467b48Spatrick     // Loop backwards and return the first MI that modifies the physical CR Reg.
56009467b48Spatrick     MachineBasicBlock::iterator Me = Copy, B = Copy->getParent()->begin();
56109467b48Spatrick     while (Me != B)
56209467b48Spatrick       if ((--Me)->modifiesRegister(CopySrc, TRI))
56309467b48Spatrick         return &*Me;
56409467b48Spatrick     return nullptr;
56509467b48Spatrick   }
56609467b48Spatrick   return MRI->getVRegDef(CopySrc);
56709467b48Spatrick }
56809467b48Spatrick 
initialize(MachineFunction & MFParam)56909467b48Spatrick void PPCReduceCRLogicals::initialize(MachineFunction &MFParam) {
57009467b48Spatrick   MF = &MFParam;
57109467b48Spatrick   MRI = &MF->getRegInfo();
57209467b48Spatrick   TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo();
57309467b48Spatrick   MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
57409467b48Spatrick 
57509467b48Spatrick   AllCRLogicalOps.clear();
57609467b48Spatrick }
57709467b48Spatrick 
57809467b48Spatrick /// Contains all the implemented transformations on CR logical operations.
57909467b48Spatrick /// For example, a binary CR logical can be used to split a block on its inputs,
58009467b48Spatrick /// a unary CR logical might be used to change the condition code on a
58109467b48Spatrick /// comparison feeding it. A nullary CR logical might simply be removable
58209467b48Spatrick /// if the user of the bit it [un]sets can be transformed.
handleCROp(unsigned Idx)58309467b48Spatrick bool PPCReduceCRLogicals::handleCROp(unsigned Idx) {
58409467b48Spatrick   // We can definitely split a block on the inputs to a binary CR operation
58509467b48Spatrick   // whose defs and (single) use are within the same block.
58609467b48Spatrick   bool Changed = false;
58709467b48Spatrick   CRLogicalOpInfo CRI = AllCRLogicalOps[Idx];
58809467b48Spatrick   if (CRI.IsBinary && CRI.ContainedInBlock && CRI.SingleUse && CRI.FeedsBR &&
58909467b48Spatrick       CRI.DefsSingleUse) {
59009467b48Spatrick     Changed = splitBlockOnBinaryCROp(CRI);
59109467b48Spatrick     if (Changed)
59209467b48Spatrick       NumBlocksSplitOnBinaryCROp++;
59309467b48Spatrick   }
59409467b48Spatrick   return Changed;
59509467b48Spatrick }
59609467b48Spatrick 
59709467b48Spatrick /// Splits a block that contains a CR-logical operation that feeds a branch
59809467b48Spatrick /// and whose operands are produced within the block.
59909467b48Spatrick /// Example:
60009467b48Spatrick ///    %vr5<def> = CMPDI %vr2, 0; CRRC:%vr5 G8RC:%vr2
60109467b48Spatrick ///    %vr6<def> = COPY %vr5:sub_eq; CRBITRC:%vr6 CRRC:%vr5
60209467b48Spatrick ///    %vr7<def> = CMPDI %vr3, 0; CRRC:%vr7 G8RC:%vr3
60309467b48Spatrick ///    %vr8<def> = COPY %vr7:sub_eq; CRBITRC:%vr8 CRRC:%vr7
60409467b48Spatrick ///    %vr9<def> = CROR %vr6<kill>, %vr8<kill>; CRBITRC:%vr9,%vr6,%vr8
60509467b48Spatrick ///    BC %vr9<kill>, <BB#2>; CRBITRC:%vr9
60609467b48Spatrick /// Becomes:
60709467b48Spatrick ///    %vr5<def> = CMPDI %vr2, 0; CRRC:%vr5 G8RC:%vr2
60809467b48Spatrick ///    %vr6<def> = COPY %vr5:sub_eq; CRBITRC:%vr6 CRRC:%vr5
60909467b48Spatrick ///    BC %vr6<kill>, <BB#2>; CRBITRC:%vr6
61009467b48Spatrick ///
61109467b48Spatrick ///    %vr7<def> = CMPDI %vr3, 0; CRRC:%vr7 G8RC:%vr3
61209467b48Spatrick ///    %vr8<def> = COPY %vr7:sub_eq; CRBITRC:%vr8 CRRC:%vr7
61309467b48Spatrick ///    BC %vr9<kill>, <BB#2>; CRBITRC:%vr9
splitBlockOnBinaryCROp(CRLogicalOpInfo & CRI)61409467b48Spatrick bool PPCReduceCRLogicals::splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI) {
61509467b48Spatrick   if (CRI.CopyDefs.first == CRI.CopyDefs.second) {
61609467b48Spatrick     LLVM_DEBUG(dbgs() << "Unable to split as the two operands are the same\n");
61709467b48Spatrick     NumNotSplitIdenticalOperands++;
61809467b48Spatrick     return false;
61909467b48Spatrick   }
62009467b48Spatrick   if (CRI.TrueDefs.first->isCopy() || CRI.TrueDefs.second->isCopy() ||
62109467b48Spatrick       CRI.TrueDefs.first->isPHI() || CRI.TrueDefs.second->isPHI()) {
62209467b48Spatrick     LLVM_DEBUG(
62309467b48Spatrick         dbgs() << "Unable to split because one of the operands is a PHI or "
62409467b48Spatrick                   "chain of copies.\n");
62509467b48Spatrick     NumNotSplitChainCopies++;
62609467b48Spatrick     return false;
62709467b48Spatrick   }
62809467b48Spatrick   // Note: keep in sync with computeBranchTargetAndInversion().
62909467b48Spatrick   if (CRI.MI->getOpcode() != PPC::CROR &&
63009467b48Spatrick       CRI.MI->getOpcode() != PPC::CRAND &&
63109467b48Spatrick       CRI.MI->getOpcode() != PPC::CRNOR &&
63209467b48Spatrick       CRI.MI->getOpcode() != PPC::CRNAND &&
63309467b48Spatrick       CRI.MI->getOpcode() != PPC::CRORC &&
63409467b48Spatrick       CRI.MI->getOpcode() != PPC::CRANDC) {
63509467b48Spatrick     LLVM_DEBUG(dbgs() << "Unable to split blocks on this opcode.\n");
63609467b48Spatrick     NumNotSplitWrongOpcode++;
63709467b48Spatrick     return false;
63809467b48Spatrick   }
63909467b48Spatrick   LLVM_DEBUG(dbgs() << "Splitting the following CR op:\n"; CRI.dump());
64009467b48Spatrick   MachineBasicBlock::iterator Def1It = CRI.TrueDefs.first;
64109467b48Spatrick   MachineBasicBlock::iterator Def2It = CRI.TrueDefs.second;
64209467b48Spatrick 
64309467b48Spatrick   bool UsingDef1 = false;
64409467b48Spatrick   MachineInstr *SplitBefore = &*Def2It;
64509467b48Spatrick   for (auto E = CRI.MI->getParent()->end(); Def2It != E; ++Def2It) {
64609467b48Spatrick     if (Def1It == Def2It) { // Def2 comes before Def1.
64709467b48Spatrick       SplitBefore = &*Def1It;
64809467b48Spatrick       UsingDef1 = true;
64909467b48Spatrick       break;
65009467b48Spatrick     }
65109467b48Spatrick   }
65209467b48Spatrick 
65309467b48Spatrick   LLVM_DEBUG(dbgs() << "We will split the following block:\n";);
65409467b48Spatrick   LLVM_DEBUG(CRI.MI->getParent()->dump());
65509467b48Spatrick   LLVM_DEBUG(dbgs() << "Before instruction:\n"; SplitBefore->dump());
65609467b48Spatrick 
65709467b48Spatrick   // Get the branch instruction.
65809467b48Spatrick   MachineInstr *Branch =
65909467b48Spatrick     MRI->use_nodbg_begin(CRI.MI->getOperand(0).getReg())->getParent();
66009467b48Spatrick 
66109467b48Spatrick   // We want the new block to have no code in it other than the definition
66209467b48Spatrick   // of the input to the CR logical and the CR logical itself. So we move
66309467b48Spatrick   // those to the bottom of the block (just before the branch). Then we
66409467b48Spatrick   // will split before the CR logical.
66509467b48Spatrick   MachineBasicBlock *MBB = SplitBefore->getParent();
66609467b48Spatrick   auto FirstTerminator = MBB->getFirstTerminator();
66709467b48Spatrick   MachineBasicBlock::iterator FirstInstrToMove =
66809467b48Spatrick     UsingDef1 ? CRI.TrueDefs.first : CRI.TrueDefs.second;
66909467b48Spatrick   MachineBasicBlock::iterator SecondInstrToMove =
67009467b48Spatrick     UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second;
67109467b48Spatrick 
67209467b48Spatrick   // The instructions that need to be moved are not guaranteed to be
67309467b48Spatrick   // contiguous. Move them individually.
67409467b48Spatrick   // FIXME: If one of the operands is a chain of (single use) copies, they
67509467b48Spatrick   // can all be moved and we can still split.
67609467b48Spatrick   MBB->splice(FirstTerminator, MBB, FirstInstrToMove);
67709467b48Spatrick   if (FirstInstrToMove != SecondInstrToMove)
67809467b48Spatrick     MBB->splice(FirstTerminator, MBB, SecondInstrToMove);
67909467b48Spatrick   MBB->splice(FirstTerminator, MBB, CRI.MI);
68009467b48Spatrick 
68109467b48Spatrick   unsigned Opc = CRI.MI->getOpcode();
68209467b48Spatrick   bool InvertOrigBranch, InvertNewBranch, TargetIsFallThrough;
68309467b48Spatrick   computeBranchTargetAndInversion(Opc, Branch->getOpcode(), UsingDef1,
68409467b48Spatrick                                   InvertNewBranch, InvertOrigBranch,
68509467b48Spatrick                                   TargetIsFallThrough);
68609467b48Spatrick   MachineInstr *SplitCond =
68709467b48Spatrick     UsingDef1 ? CRI.CopyDefs.second : CRI.CopyDefs.first;
68809467b48Spatrick   LLVM_DEBUG(dbgs() << "We will " << (InvertNewBranch ? "invert" : "copy"));
68909467b48Spatrick   LLVM_DEBUG(dbgs() << " the original branch and the target is the "
69009467b48Spatrick                     << (TargetIsFallThrough ? "fallthrough block\n"
69109467b48Spatrick                                             : "orig. target block\n"));
69209467b48Spatrick   LLVM_DEBUG(dbgs() << "Original branch instruction: "; Branch->dump());
69309467b48Spatrick   BlockSplitInfo BSI { Branch, SplitBefore, SplitCond, InvertNewBranch,
69409467b48Spatrick     InvertOrigBranch, TargetIsFallThrough, MBPI, CRI.MI,
69509467b48Spatrick     UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second };
69609467b48Spatrick   bool Changed = splitMBB(BSI);
69709467b48Spatrick   // If we've split on a CR logical that is fed by a CR logical,
69809467b48Spatrick   // recompute the source CR logical as it may be usable for splitting.
69909467b48Spatrick   if (Changed) {
70009467b48Spatrick     bool Input1CRlogical =
70109467b48Spatrick       CRI.TrueDefs.first && isCRLogical(*CRI.TrueDefs.first);
70209467b48Spatrick     bool Input2CRlogical =
70309467b48Spatrick       CRI.TrueDefs.second && isCRLogical(*CRI.TrueDefs.second);
70409467b48Spatrick     if (Input1CRlogical)
70509467b48Spatrick       AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.first));
70609467b48Spatrick     if (Input2CRlogical)
70709467b48Spatrick       AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.second));
70809467b48Spatrick   }
70909467b48Spatrick   return Changed;
71009467b48Spatrick }
71109467b48Spatrick 
collectCRLogicals()71209467b48Spatrick void PPCReduceCRLogicals::collectCRLogicals() {
71309467b48Spatrick   for (MachineBasicBlock &MBB : *MF) {
71409467b48Spatrick     for (MachineInstr &MI : MBB) {
71509467b48Spatrick       if (isCRLogical(MI)) {
71609467b48Spatrick         AllCRLogicalOps.push_back(createCRLogicalOpInfo(MI));
71709467b48Spatrick         TotalCRLogicals++;
71809467b48Spatrick         if (AllCRLogicalOps.back().IsNullary)
71909467b48Spatrick           TotalNullaryCRLogicals++;
72009467b48Spatrick         else if (AllCRLogicalOps.back().IsBinary)
72109467b48Spatrick           TotalBinaryCRLogicals++;
72209467b48Spatrick         else
72309467b48Spatrick           TotalUnaryCRLogicals++;
72409467b48Spatrick       }
72509467b48Spatrick     }
72609467b48Spatrick   }
72709467b48Spatrick }
72809467b48Spatrick 
72909467b48Spatrick } // end anonymous namespace
73009467b48Spatrick 
73109467b48Spatrick INITIALIZE_PASS_BEGIN(PPCReduceCRLogicals, DEBUG_TYPE,
73209467b48Spatrick                       "PowerPC Reduce CR logical Operation", false, false)
73309467b48Spatrick INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
73409467b48Spatrick INITIALIZE_PASS_END(PPCReduceCRLogicals, DEBUG_TYPE,
73509467b48Spatrick                     "PowerPC Reduce CR logical Operation", false, false)
73609467b48Spatrick 
73709467b48Spatrick char PPCReduceCRLogicals::ID = 0;
73809467b48Spatrick FunctionPass*
createPPCReduceCRLogicalsPass()73909467b48Spatrick llvm::createPPCReduceCRLogicalsPass() { return new PPCReduceCRLogicals(); }
740