16f590bf8SNemanja Ivanovic //===---- PPCReduceCRLogicals.cpp - Reduce CR Bit Logical operations ------===//
26f590bf8SNemanja Ivanovic //
36f590bf8SNemanja Ivanovic //                     The LLVM Compiler Infrastructure
46f590bf8SNemanja Ivanovic //
56f590bf8SNemanja Ivanovic // This file is distributed under the University of Illinois Open Source
66f590bf8SNemanja Ivanovic // License. See LICENSE.TXT for details.
76f590bf8SNemanja Ivanovic //
86f590bf8SNemanja Ivanovic //===---------------------------------------------------------------------===//
96f590bf8SNemanja Ivanovic //
106f590bf8SNemanja Ivanovic // This pass aims to reduce the number of logical operations on bits in the CR
116f590bf8SNemanja Ivanovic // register. These instructions have a fairly high latency and only a single
126f590bf8SNemanja Ivanovic // pipeline at their disposal in modern PPC cores. Furthermore, they have a
136f590bf8SNemanja Ivanovic // tendency to occur in fairly small blocks where there's little opportunity
146f590bf8SNemanja Ivanovic // to hide the latency between the CR logical operation and its user.
156f590bf8SNemanja Ivanovic //
166f590bf8SNemanja Ivanovic //===---------------------------------------------------------------------===//
176f590bf8SNemanja Ivanovic 
186f590bf8SNemanja Ivanovic #include "PPC.h"
19*d8a6f93aSDavid Blaikie #include "PPCInstrInfo.h"
206f590bf8SNemanja Ivanovic #include "PPCTargetMachine.h"
216f590bf8SNemanja Ivanovic #include "llvm/ADT/Statistic.h"
22*d8a6f93aSDavid Blaikie #include "llvm/CodeGen/MachineBranchProbabilityInfo.h"
23*d8a6f93aSDavid Blaikie #include "llvm/CodeGen/MachineDominators.h"
24*d8a6f93aSDavid Blaikie #include "llvm/CodeGen/MachineFunctionPass.h"
25*d8a6f93aSDavid Blaikie #include "llvm/CodeGen/MachineInstrBuilder.h"
26*d8a6f93aSDavid Blaikie #include "llvm/CodeGen/MachineRegisterInfo.h"
27*d8a6f93aSDavid Blaikie #include "llvm/Support/Debug.h"
286f590bf8SNemanja Ivanovic 
296f590bf8SNemanja Ivanovic using namespace llvm;
306f590bf8SNemanja Ivanovic 
316f590bf8SNemanja Ivanovic #define DEBUG_TYPE "ppc-reduce-cr-ops"
326f590bf8SNemanja Ivanovic 
336f590bf8SNemanja Ivanovic STATISTIC(NumContainedSingleUseBinOps,
346f590bf8SNemanja Ivanovic           "Number of single-use binary CR logical ops contained in a block");
356f590bf8SNemanja Ivanovic STATISTIC(NumToSplitBlocks,
366f590bf8SNemanja Ivanovic           "Number of binary CR logical ops that can be used to split blocks");
376f590bf8SNemanja Ivanovic STATISTIC(TotalCRLogicals, "Number of CR logical ops.");
386f590bf8SNemanja Ivanovic STATISTIC(TotalNullaryCRLogicals,
396f590bf8SNemanja Ivanovic           "Number of nullary CR logical ops (CRSET/CRUNSET).");
406f590bf8SNemanja Ivanovic STATISTIC(TotalUnaryCRLogicals, "Number of unary CR logical ops.");
416f590bf8SNemanja Ivanovic STATISTIC(TotalBinaryCRLogicals, "Number of CR logical ops.");
426f590bf8SNemanja Ivanovic STATISTIC(NumBlocksSplitOnBinaryCROp,
436f590bf8SNemanja Ivanovic           "Number of blocks split on CR binary logical ops.");
446f590bf8SNemanja Ivanovic STATISTIC(NumNotSplitIdenticalOperands,
456f590bf8SNemanja Ivanovic           "Number of blocks not split due to operands being identical.");
466f590bf8SNemanja Ivanovic STATISTIC(NumNotSplitChainCopies,
476f590bf8SNemanja Ivanovic           "Number of blocks not split due to operands being chained copies.");
486f590bf8SNemanja Ivanovic STATISTIC(NumNotSplitWrongOpcode,
496f590bf8SNemanja Ivanovic           "Number of blocks not split due to the wrong opcode.");
506f590bf8SNemanja Ivanovic 
516f590bf8SNemanja Ivanovic namespace llvm {
526f590bf8SNemanja Ivanovic   void initializePPCReduceCRLogicalsPass(PassRegistry&);
536f590bf8SNemanja Ivanovic }
546f590bf8SNemanja Ivanovic 
55*d8a6f93aSDavid Blaikie /// Given a basic block \p Successor that potentially contains PHIs, this
56*d8a6f93aSDavid Blaikie /// function will look for any incoming values in the PHIs that are supposed to
57*d8a6f93aSDavid Blaikie /// be coming from \p OrigMBB but whose definition is actually in \p NewMBB.
58*d8a6f93aSDavid Blaikie /// Any such PHIs will be updated to reflect reality.
59*d8a6f93aSDavid Blaikie static void updatePHIs(MachineBasicBlock *Successor, MachineBasicBlock *OrigMBB,
60*d8a6f93aSDavid Blaikie                        MachineBasicBlock *NewMBB, MachineRegisterInfo *MRI) {
61*d8a6f93aSDavid Blaikie   for (auto &MI : Successor->instrs()) {
62*d8a6f93aSDavid Blaikie     if (!MI.isPHI())
63*d8a6f93aSDavid Blaikie       continue;
64*d8a6f93aSDavid Blaikie     // This is a really ugly-looking loop, but it was pillaged directly from
65*d8a6f93aSDavid Blaikie     // MachineBasicBlock::transferSuccessorsAndUpdatePHIs().
66*d8a6f93aSDavid Blaikie     for (unsigned i = 2, e = MI.getNumOperands() + 1; i != e; i += 2) {
67*d8a6f93aSDavid Blaikie       MachineOperand &MO = MI.getOperand(i);
68*d8a6f93aSDavid Blaikie       if (MO.getMBB() == OrigMBB) {
69*d8a6f93aSDavid Blaikie         // Check if the instruction is actualy defined in NewMBB.
70*d8a6f93aSDavid Blaikie         if (MI.getOperand(i - 1).isReg()) {
71*d8a6f93aSDavid Blaikie           MachineInstr *DefMI = MRI->getVRegDef(MI.getOperand(i - 1).getReg());
72*d8a6f93aSDavid Blaikie           if (DefMI->getParent() == NewMBB ||
73*d8a6f93aSDavid Blaikie               !OrigMBB->isSuccessor(Successor)) {
74*d8a6f93aSDavid Blaikie             MO.setMBB(NewMBB);
75*d8a6f93aSDavid Blaikie             break;
76*d8a6f93aSDavid Blaikie           }
77*d8a6f93aSDavid Blaikie         }
78*d8a6f93aSDavid Blaikie       }
79*d8a6f93aSDavid Blaikie     }
80*d8a6f93aSDavid Blaikie   }
81*d8a6f93aSDavid Blaikie }
82*d8a6f93aSDavid Blaikie 
83*d8a6f93aSDavid Blaikie /// Given a basic block \p Successor that potentially contains PHIs, this
84*d8a6f93aSDavid Blaikie /// function will look for PHIs that have an incoming value from \p OrigMBB
85*d8a6f93aSDavid Blaikie /// and will add the same incoming value from \p NewMBB.
86*d8a6f93aSDavid Blaikie /// NOTE: This should only be used if \p NewMBB is an immediate dominator of
87*d8a6f93aSDavid Blaikie /// \p OrigMBB.
88*d8a6f93aSDavid Blaikie static void addIncomingValuesToPHIs(MachineBasicBlock *Successor,
89*d8a6f93aSDavid Blaikie                                     MachineBasicBlock *OrigMBB,
90*d8a6f93aSDavid Blaikie                                     MachineBasicBlock *NewMBB,
91*d8a6f93aSDavid Blaikie                                     MachineRegisterInfo *MRI) {
92*d8a6f93aSDavid Blaikie   assert(OrigMBB->isSuccessor(NewMBB) &&
93*d8a6f93aSDavid Blaikie          "NewMBB must be a sucessor of OrigMBB");
94*d8a6f93aSDavid Blaikie   for (auto &MI : Successor->instrs()) {
95*d8a6f93aSDavid Blaikie     if (!MI.isPHI())
96*d8a6f93aSDavid Blaikie       continue;
97*d8a6f93aSDavid Blaikie     // This is a really ugly-looking loop, but it was pillaged directly from
98*d8a6f93aSDavid Blaikie     // MachineBasicBlock::transferSuccessorsAndUpdatePHIs().
99*d8a6f93aSDavid Blaikie     for (unsigned i = 2, e = MI.getNumOperands() + 1; i != e; i += 2) {
100*d8a6f93aSDavid Blaikie       MachineOperand &MO = MI.getOperand(i);
101*d8a6f93aSDavid Blaikie       if (MO.getMBB() == OrigMBB) {
102*d8a6f93aSDavid Blaikie         MachineInstrBuilder MIB(*MI.getParent()->getParent(), &MI);
103*d8a6f93aSDavid Blaikie         MIB.addReg(MI.getOperand(i - 1).getReg()).addMBB(NewMBB);
104*d8a6f93aSDavid Blaikie         break;
105*d8a6f93aSDavid Blaikie       }
106*d8a6f93aSDavid Blaikie     }
107*d8a6f93aSDavid Blaikie   }
108*d8a6f93aSDavid Blaikie }
109*d8a6f93aSDavid Blaikie 
110*d8a6f93aSDavid Blaikie struct BlockSplitInfo {
111*d8a6f93aSDavid Blaikie   MachineInstr *OrigBranch;
112*d8a6f93aSDavid Blaikie   MachineInstr *SplitBefore;
113*d8a6f93aSDavid Blaikie   MachineInstr *SplitCond;
114*d8a6f93aSDavid Blaikie   bool InvertNewBranch;
115*d8a6f93aSDavid Blaikie   bool InvertOrigBranch;
116*d8a6f93aSDavid Blaikie   bool BranchToFallThrough;
117*d8a6f93aSDavid Blaikie   const MachineBranchProbabilityInfo *MBPI;
118*d8a6f93aSDavid Blaikie   MachineInstr *MIToDelete;
119*d8a6f93aSDavid Blaikie   MachineInstr *NewCond;
120*d8a6f93aSDavid Blaikie   bool allInstrsInSameMBB() {
121*d8a6f93aSDavid Blaikie     if (!OrigBranch || !SplitBefore || !SplitCond)
122*d8a6f93aSDavid Blaikie       return false;
123*d8a6f93aSDavid Blaikie     MachineBasicBlock *MBB = OrigBranch->getParent();
124*d8a6f93aSDavid Blaikie     if (SplitBefore->getParent() != MBB || SplitCond->getParent() != MBB)
125*d8a6f93aSDavid Blaikie       return false;
126*d8a6f93aSDavid Blaikie     if (MIToDelete && MIToDelete->getParent() != MBB)
127*d8a6f93aSDavid Blaikie       return false;
128*d8a6f93aSDavid Blaikie     if (NewCond && NewCond->getParent() != MBB)
129*d8a6f93aSDavid Blaikie       return false;
130*d8a6f93aSDavid Blaikie     return true;
131*d8a6f93aSDavid Blaikie   }
132*d8a6f93aSDavid Blaikie };
133*d8a6f93aSDavid Blaikie 
134*d8a6f93aSDavid Blaikie /// Splits a MachineBasicBlock to branch before \p SplitBefore. The original
135*d8a6f93aSDavid Blaikie /// branch is \p OrigBranch. The target of the new branch can either be the same
136*d8a6f93aSDavid Blaikie /// as the target of the original branch or the fallthrough successor of the
137*d8a6f93aSDavid Blaikie /// original block as determined by \p BranchToFallThrough. The branch
138*d8a6f93aSDavid Blaikie /// conditions will be inverted according to \p InvertNewBranch and
139*d8a6f93aSDavid Blaikie /// \p InvertOrigBranch. If an instruction that previously fed the branch is to
140*d8a6f93aSDavid Blaikie /// be deleted, it is provided in \p MIToDelete and \p NewCond will be used as
141*d8a6f93aSDavid Blaikie /// the branch condition. The branch probabilities will be set if the
142*d8a6f93aSDavid Blaikie /// MachineBranchProbabilityInfo isn't null.
143*d8a6f93aSDavid Blaikie static bool splitMBB(BlockSplitInfo &BSI) {
144*d8a6f93aSDavid Blaikie   assert(BSI.allInstrsInSameMBB() &&
145*d8a6f93aSDavid Blaikie          "All instructions must be in the same block.");
146*d8a6f93aSDavid Blaikie 
147*d8a6f93aSDavid Blaikie   MachineBasicBlock *ThisMBB = BSI.OrigBranch->getParent();
148*d8a6f93aSDavid Blaikie   MachineFunction *MF = ThisMBB->getParent();
149*d8a6f93aSDavid Blaikie   MachineRegisterInfo *MRI = &MF->getRegInfo();
150*d8a6f93aSDavid Blaikie   assert(MRI->isSSA() && "Can only do this while the function is in SSA form.");
151*d8a6f93aSDavid Blaikie   if (ThisMBB->succ_size() != 2) {
152*d8a6f93aSDavid Blaikie     DEBUG(dbgs() << "Don't know how to handle blocks that don't have exactly"
153*d8a6f93aSDavid Blaikie                  << " two succesors.\n");
154*d8a6f93aSDavid Blaikie     return false;
155*d8a6f93aSDavid Blaikie   }
156*d8a6f93aSDavid Blaikie 
157*d8a6f93aSDavid Blaikie   const PPCInstrInfo *TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo();
158*d8a6f93aSDavid Blaikie   unsigned OrigBROpcode = BSI.OrigBranch->getOpcode();
159*d8a6f93aSDavid Blaikie   unsigned InvertedOpcode =
160*d8a6f93aSDavid Blaikie       OrigBROpcode == PPC::BC
161*d8a6f93aSDavid Blaikie           ? PPC::BCn
162*d8a6f93aSDavid Blaikie           : OrigBROpcode == PPC::BCn
163*d8a6f93aSDavid Blaikie                 ? PPC::BC
164*d8a6f93aSDavid Blaikie                 : OrigBROpcode == PPC::BCLR ? PPC::BCLRn : PPC::BCLR;
165*d8a6f93aSDavid Blaikie   unsigned NewBROpcode = BSI.InvertNewBranch ? InvertedOpcode : OrigBROpcode;
166*d8a6f93aSDavid Blaikie   MachineBasicBlock *OrigTarget = BSI.OrigBranch->getOperand(1).getMBB();
167*d8a6f93aSDavid Blaikie   MachineBasicBlock *OrigFallThrough = OrigTarget == *ThisMBB->succ_begin()
168*d8a6f93aSDavid Blaikie                                            ? *ThisMBB->succ_rbegin()
169*d8a6f93aSDavid Blaikie                                            : *ThisMBB->succ_begin();
170*d8a6f93aSDavid Blaikie   MachineBasicBlock *NewBRTarget =
171*d8a6f93aSDavid Blaikie       BSI.BranchToFallThrough ? OrigFallThrough : OrigTarget;
172*d8a6f93aSDavid Blaikie   BranchProbability ProbToNewTarget =
173*d8a6f93aSDavid Blaikie       !BSI.MBPI ? BranchProbability::getUnknown()
174*d8a6f93aSDavid Blaikie                 : BSI.MBPI->getEdgeProbability(ThisMBB, NewBRTarget);
175*d8a6f93aSDavid Blaikie 
176*d8a6f93aSDavid Blaikie   // Create a new basic block.
177*d8a6f93aSDavid Blaikie   MachineBasicBlock::iterator InsertPoint = BSI.SplitBefore;
178*d8a6f93aSDavid Blaikie   const BasicBlock *LLVM_BB = ThisMBB->getBasicBlock();
179*d8a6f93aSDavid Blaikie   MachineFunction::iterator It = ThisMBB->getIterator();
180*d8a6f93aSDavid Blaikie   MachineBasicBlock *NewMBB = MF->CreateMachineBasicBlock(LLVM_BB);
181*d8a6f93aSDavid Blaikie   MF->insert(++It, NewMBB);
182*d8a6f93aSDavid Blaikie 
183*d8a6f93aSDavid Blaikie   // Move everything after SplitBefore into the new block.
184*d8a6f93aSDavid Blaikie   NewMBB->splice(NewMBB->end(), ThisMBB, InsertPoint, ThisMBB->end());
185*d8a6f93aSDavid Blaikie   NewMBB->transferSuccessors(ThisMBB);
186*d8a6f93aSDavid Blaikie 
187*d8a6f93aSDavid Blaikie   // Add the two successors to ThisMBB. The probabilities come from the
188*d8a6f93aSDavid Blaikie   // existing blocks if available.
189*d8a6f93aSDavid Blaikie   ThisMBB->addSuccessor(NewBRTarget, ProbToNewTarget);
190*d8a6f93aSDavid Blaikie   ThisMBB->addSuccessor(NewMBB, ProbToNewTarget.getCompl());
191*d8a6f93aSDavid Blaikie 
192*d8a6f93aSDavid Blaikie   // Add the branches to ThisMBB.
193*d8a6f93aSDavid Blaikie   BuildMI(*ThisMBB, ThisMBB->end(), BSI.SplitBefore->getDebugLoc(),
194*d8a6f93aSDavid Blaikie           TII->get(NewBROpcode))
195*d8a6f93aSDavid Blaikie       .addReg(BSI.SplitCond->getOperand(0).getReg())
196*d8a6f93aSDavid Blaikie       .addMBB(NewBRTarget);
197*d8a6f93aSDavid Blaikie   BuildMI(*ThisMBB, ThisMBB->end(), BSI.SplitBefore->getDebugLoc(),
198*d8a6f93aSDavid Blaikie           TII->get(PPC::B))
199*d8a6f93aSDavid Blaikie       .addMBB(NewMBB);
200*d8a6f93aSDavid Blaikie   if (BSI.MIToDelete)
201*d8a6f93aSDavid Blaikie     BSI.MIToDelete->eraseFromParent();
202*d8a6f93aSDavid Blaikie 
203*d8a6f93aSDavid Blaikie   // Change the condition on the original branch and invert it if requested.
204*d8a6f93aSDavid Blaikie   auto FirstTerminator = NewMBB->getFirstTerminator();
205*d8a6f93aSDavid Blaikie   if (BSI.NewCond) {
206*d8a6f93aSDavid Blaikie     assert(FirstTerminator->getOperand(0).isReg() &&
207*d8a6f93aSDavid Blaikie            "Can't update condition of unconditional branch.");
208*d8a6f93aSDavid Blaikie     FirstTerminator->getOperand(0).setReg(BSI.NewCond->getOperand(0).getReg());
209*d8a6f93aSDavid Blaikie   }
210*d8a6f93aSDavid Blaikie   if (BSI.InvertOrigBranch)
211*d8a6f93aSDavid Blaikie     FirstTerminator->setDesc(TII->get(InvertedOpcode));
212*d8a6f93aSDavid Blaikie 
213*d8a6f93aSDavid Blaikie   // If any of the PHIs in the successors of NewMBB reference values that
214*d8a6f93aSDavid Blaikie   // now come from NewMBB, they need to be updated.
215*d8a6f93aSDavid Blaikie   for (auto *Succ : NewMBB->successors()) {
216*d8a6f93aSDavid Blaikie     updatePHIs(Succ, ThisMBB, NewMBB, MRI);
217*d8a6f93aSDavid Blaikie   }
218*d8a6f93aSDavid Blaikie   addIncomingValuesToPHIs(NewBRTarget, ThisMBB, NewMBB, MRI);
219*d8a6f93aSDavid Blaikie 
220*d8a6f93aSDavid Blaikie   DEBUG(dbgs() << "After splitting, ThisMBB:\n"; ThisMBB->dump());
221*d8a6f93aSDavid Blaikie   DEBUG(dbgs() << "NewMBB:\n"; NewMBB->dump());
222*d8a6f93aSDavid Blaikie   DEBUG(dbgs() << "New branch-to block:\n"; NewBRTarget->dump());
223*d8a6f93aSDavid Blaikie   return true;
224*d8a6f93aSDavid Blaikie }
2256f590bf8SNemanja Ivanovic 
2266f590bf8SNemanja Ivanovic static bool isBinary(MachineInstr &MI) {
2276f590bf8SNemanja Ivanovic   return MI.getNumOperands() == 3;
2286f590bf8SNemanja Ivanovic }
2296f590bf8SNemanja Ivanovic 
2306f590bf8SNemanja Ivanovic static bool isNullary(MachineInstr &MI) {
2316f590bf8SNemanja Ivanovic   return MI.getNumOperands() == 1;
2326f590bf8SNemanja Ivanovic }
2336f590bf8SNemanja Ivanovic 
2346f590bf8SNemanja Ivanovic /// Given a CR logical operation \p CROp, branch opcode \p BROp as well as
2356f590bf8SNemanja Ivanovic /// a flag to indicate if the first operand of \p CROp is used as the
2366f590bf8SNemanja Ivanovic /// SplitBefore operand, determines whether either of the branches are to be
2376f590bf8SNemanja Ivanovic /// inverted as well as whether the new target should be the original
2386f590bf8SNemanja Ivanovic /// fall-through block.
2396f590bf8SNemanja Ivanovic static void
2406f590bf8SNemanja Ivanovic computeBranchTargetAndInversion(unsigned CROp, unsigned BROp, bool UsingDef1,
2416f590bf8SNemanja Ivanovic                                 bool &InvertNewBranch, bool &InvertOrigBranch,
2426f590bf8SNemanja Ivanovic                                 bool &TargetIsFallThrough) {
2436f590bf8SNemanja Ivanovic   // The conditions under which each of the output operands should be [un]set
2446f590bf8SNemanja Ivanovic   // can certainly be written much more concisely with just 3 if statements or
2456f590bf8SNemanja Ivanovic   // ternary expressions. However, this provides a much clearer overview to the
2466f590bf8SNemanja Ivanovic   // reader as to what is set for each <CROp, BROp, OpUsed> combination.
2476f590bf8SNemanja Ivanovic   if (BROp == PPC::BC || BROp == PPC::BCLR) {
2486f590bf8SNemanja Ivanovic     // Regular branches.
2496f590bf8SNemanja Ivanovic     switch (CROp) {
2506f590bf8SNemanja Ivanovic     default:
2516f590bf8SNemanja Ivanovic       llvm_unreachable("Don't know how to handle this CR logical.");
2526f590bf8SNemanja Ivanovic     case PPC::CROR:
2536f590bf8SNemanja Ivanovic       InvertNewBranch = false;
2546f590bf8SNemanja Ivanovic       InvertOrigBranch = false;
2556f590bf8SNemanja Ivanovic       TargetIsFallThrough = false;
2566f590bf8SNemanja Ivanovic       return;
2576f590bf8SNemanja Ivanovic     case PPC::CRAND:
2586f590bf8SNemanja Ivanovic       InvertNewBranch = true;
2596f590bf8SNemanja Ivanovic       InvertOrigBranch = false;
2606f590bf8SNemanja Ivanovic       TargetIsFallThrough = true;
2616f590bf8SNemanja Ivanovic       return;
2626f590bf8SNemanja Ivanovic     case PPC::CRNAND:
2636f590bf8SNemanja Ivanovic       InvertNewBranch = true;
2646f590bf8SNemanja Ivanovic       InvertOrigBranch = true;
2656f590bf8SNemanja Ivanovic       TargetIsFallThrough = false;
2666f590bf8SNemanja Ivanovic       return;
2676f590bf8SNemanja Ivanovic     case PPC::CRNOR:
2686f590bf8SNemanja Ivanovic       InvertNewBranch = false;
2696f590bf8SNemanja Ivanovic       InvertOrigBranch = true;
2706f590bf8SNemanja Ivanovic       TargetIsFallThrough = true;
2716f590bf8SNemanja Ivanovic       return;
2726f590bf8SNemanja Ivanovic     case PPC::CRORC:
2736f590bf8SNemanja Ivanovic       InvertNewBranch = UsingDef1;
2746f590bf8SNemanja Ivanovic       InvertOrigBranch = !UsingDef1;
2756f590bf8SNemanja Ivanovic       TargetIsFallThrough = false;
2766f590bf8SNemanja Ivanovic       return;
2776f590bf8SNemanja Ivanovic     case PPC::CRANDC:
2786f590bf8SNemanja Ivanovic       InvertNewBranch = !UsingDef1;
2796f590bf8SNemanja Ivanovic       InvertOrigBranch = !UsingDef1;
2806f590bf8SNemanja Ivanovic       TargetIsFallThrough = true;
2816f590bf8SNemanja Ivanovic       return;
2826f590bf8SNemanja Ivanovic     }
2836f590bf8SNemanja Ivanovic   } else if (BROp == PPC::BCn || BROp == PPC::BCLRn) {
2846f590bf8SNemanja Ivanovic     // Negated branches.
2856f590bf8SNemanja Ivanovic     switch (CROp) {
2866f590bf8SNemanja Ivanovic     default:
2876f590bf8SNemanja Ivanovic       llvm_unreachable("Don't know how to handle this CR logical.");
2886f590bf8SNemanja Ivanovic     case PPC::CROR:
2896f590bf8SNemanja Ivanovic       InvertNewBranch = true;
2906f590bf8SNemanja Ivanovic       InvertOrigBranch = false;
2916f590bf8SNemanja Ivanovic       TargetIsFallThrough = true;
2926f590bf8SNemanja Ivanovic       return;
2936f590bf8SNemanja Ivanovic     case PPC::CRAND:
2946f590bf8SNemanja Ivanovic       InvertNewBranch = false;
2956f590bf8SNemanja Ivanovic       InvertOrigBranch = false;
2966f590bf8SNemanja Ivanovic       TargetIsFallThrough = false;
2976f590bf8SNemanja Ivanovic       return;
2986f590bf8SNemanja Ivanovic     case PPC::CRNAND:
2996f590bf8SNemanja Ivanovic       InvertNewBranch = false;
3006f590bf8SNemanja Ivanovic       InvertOrigBranch = true;
3016f590bf8SNemanja Ivanovic       TargetIsFallThrough = true;
3026f590bf8SNemanja Ivanovic       return;
3036f590bf8SNemanja Ivanovic     case PPC::CRNOR:
3046f590bf8SNemanja Ivanovic       InvertNewBranch = true;
3056f590bf8SNemanja Ivanovic       InvertOrigBranch = true;
3066f590bf8SNemanja Ivanovic       TargetIsFallThrough = false;
3076f590bf8SNemanja Ivanovic       return;
3086f590bf8SNemanja Ivanovic     case PPC::CRORC:
3096f590bf8SNemanja Ivanovic       InvertNewBranch = !UsingDef1;
3106f590bf8SNemanja Ivanovic       InvertOrigBranch = !UsingDef1;
3116f590bf8SNemanja Ivanovic       TargetIsFallThrough = true;
3126f590bf8SNemanja Ivanovic       return;
3136f590bf8SNemanja Ivanovic     case PPC::CRANDC:
3146f590bf8SNemanja Ivanovic       InvertNewBranch = UsingDef1;
3156f590bf8SNemanja Ivanovic       InvertOrigBranch = !UsingDef1;
3166f590bf8SNemanja Ivanovic       TargetIsFallThrough = false;
3176f590bf8SNemanja Ivanovic       return;
3186f590bf8SNemanja Ivanovic     }
3196f590bf8SNemanja Ivanovic   } else
3206f590bf8SNemanja Ivanovic     llvm_unreachable("Don't know how to handle this branch.");
3216f590bf8SNemanja Ivanovic }
3226f590bf8SNemanja Ivanovic 
323*d8a6f93aSDavid Blaikie namespace {
324*d8a6f93aSDavid Blaikie 
3256f590bf8SNemanja Ivanovic class PPCReduceCRLogicals : public MachineFunctionPass {
3266f590bf8SNemanja Ivanovic 
3276f590bf8SNemanja Ivanovic public:
3286f590bf8SNemanja Ivanovic   static char ID;
3296f590bf8SNemanja Ivanovic   struct CRLogicalOpInfo {
3306f590bf8SNemanja Ivanovic     MachineInstr *MI;
3316f590bf8SNemanja Ivanovic     // FIXME: If chains of copies are to be handled, this should be a vector.
3326f590bf8SNemanja Ivanovic     std::pair<MachineInstr*, MachineInstr*> CopyDefs;
3336f590bf8SNemanja Ivanovic     std::pair<MachineInstr*, MachineInstr*> TrueDefs;
3346f590bf8SNemanja Ivanovic     unsigned IsBinary : 1;
3356f590bf8SNemanja Ivanovic     unsigned IsNullary : 1;
3366f590bf8SNemanja Ivanovic     unsigned ContainedInBlock : 1;
3376f590bf8SNemanja Ivanovic     unsigned FeedsISEL : 1;
3386f590bf8SNemanja Ivanovic     unsigned FeedsBR : 1;
3396f590bf8SNemanja Ivanovic     unsigned FeedsLogical : 1;
3406f590bf8SNemanja Ivanovic     unsigned SingleUse : 1;
3416f590bf8SNemanja Ivanovic     unsigned DefsSingleUse : 1;
3426f590bf8SNemanja Ivanovic     unsigned SubregDef1;
3436f590bf8SNemanja Ivanovic     unsigned SubregDef2;
3446f590bf8SNemanja Ivanovic     CRLogicalOpInfo() : MI(nullptr), IsBinary(0), IsNullary(0),
3456f590bf8SNemanja Ivanovic                         ContainedInBlock(0), FeedsISEL(0), FeedsBR(0),
3466f590bf8SNemanja Ivanovic                         FeedsLogical(0), SingleUse(0), DefsSingleUse(1),
3476f590bf8SNemanja Ivanovic                         SubregDef1(0), SubregDef2(0) { }
3486f590bf8SNemanja Ivanovic     void dump();
3496f590bf8SNemanja Ivanovic   };
3506f590bf8SNemanja Ivanovic 
3516f590bf8SNemanja Ivanovic private:
3526f590bf8SNemanja Ivanovic   const PPCInstrInfo *TII;
3536f590bf8SNemanja Ivanovic   MachineFunction *MF;
3546f590bf8SNemanja Ivanovic   MachineRegisterInfo *MRI;
3556f590bf8SNemanja Ivanovic   const MachineBranchProbabilityInfo *MBPI;
3566f590bf8SNemanja Ivanovic 
3576f590bf8SNemanja Ivanovic   // A vector to contain all the CR logical operations
3586f590bf8SNemanja Ivanovic   std::vector<CRLogicalOpInfo> AllCRLogicalOps;
3596f590bf8SNemanja Ivanovic   void initialize(MachineFunction &MFParm);
3606f590bf8SNemanja Ivanovic   void collectCRLogicals();
3616f590bf8SNemanja Ivanovic   bool handleCROp(CRLogicalOpInfo &CRI);
3626f590bf8SNemanja Ivanovic   bool splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI);
3636f590bf8SNemanja Ivanovic   static bool isCRLogical(MachineInstr &MI) {
3646f590bf8SNemanja Ivanovic     unsigned Opc = MI.getOpcode();
3656f590bf8SNemanja Ivanovic     return Opc == PPC::CRAND || Opc == PPC::CRNAND || Opc == PPC::CROR ||
3666f590bf8SNemanja Ivanovic       Opc == PPC::CRXOR || Opc == PPC::CRNOR || Opc == PPC::CREQV ||
3676f590bf8SNemanja Ivanovic       Opc == PPC::CRANDC || Opc == PPC::CRORC || Opc == PPC::CRSET ||
3686f590bf8SNemanja Ivanovic       Opc == PPC::CRUNSET || Opc == PPC::CR6SET || Opc == PPC::CR6UNSET;
3696f590bf8SNemanja Ivanovic   }
3706f590bf8SNemanja Ivanovic   bool simplifyCode() {
3716f590bf8SNemanja Ivanovic     bool Changed = false;
3726f590bf8SNemanja Ivanovic     // Not using a range-based for loop here as the vector may grow while being
3736f590bf8SNemanja Ivanovic     // operated on.
3746f590bf8SNemanja Ivanovic     for (unsigned i = 0; i < AllCRLogicalOps.size(); i++)
3756f590bf8SNemanja Ivanovic       Changed |= handleCROp(AllCRLogicalOps[i]);
3766f590bf8SNemanja Ivanovic     return Changed;
3776f590bf8SNemanja Ivanovic   }
3786f590bf8SNemanja Ivanovic 
3796f590bf8SNemanja Ivanovic public:
3806f590bf8SNemanja Ivanovic   PPCReduceCRLogicals() : MachineFunctionPass(ID) {
3816f590bf8SNemanja Ivanovic     initializePPCReduceCRLogicalsPass(*PassRegistry::getPassRegistry());
3826f590bf8SNemanja Ivanovic   }
3836f590bf8SNemanja Ivanovic 
3846f590bf8SNemanja Ivanovic   MachineInstr *lookThroughCRCopy(unsigned Reg, unsigned &Subreg,
3856f590bf8SNemanja Ivanovic                                   MachineInstr *&CpDef);
3866f590bf8SNemanja Ivanovic   bool runOnMachineFunction(MachineFunction &MF) override {
387f1caa283SMatthias Braun     if (skipFunction(MF.getFunction()))
3886f590bf8SNemanja Ivanovic       return false;
3896f590bf8SNemanja Ivanovic 
3906f590bf8SNemanja Ivanovic     // If the subtarget doesn't use CR bits, there's nothing to do.
3916f590bf8SNemanja Ivanovic     const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
3926f590bf8SNemanja Ivanovic     if (!STI.useCRBits())
3936f590bf8SNemanja Ivanovic       return false;
3946f590bf8SNemanja Ivanovic 
3956f590bf8SNemanja Ivanovic     initialize(MF);
3966f590bf8SNemanja Ivanovic     collectCRLogicals();
3976f590bf8SNemanja Ivanovic     return simplifyCode();
3986f590bf8SNemanja Ivanovic   }
3996f590bf8SNemanja Ivanovic   CRLogicalOpInfo createCRLogicalOpInfo(MachineInstr &MI);
4006f590bf8SNemanja Ivanovic   void getAnalysisUsage(AnalysisUsage &AU) const override {
4016f590bf8SNemanja Ivanovic     AU.addRequired<MachineBranchProbabilityInfo>();
4026f590bf8SNemanja Ivanovic     AU.addRequired<MachineDominatorTree>();
4036f590bf8SNemanja Ivanovic     MachineFunctionPass::getAnalysisUsage(AU);
4046f590bf8SNemanja Ivanovic   }
4056f590bf8SNemanja Ivanovic };
4066f590bf8SNemanja Ivanovic 
4076af75240SNemanja Ivanovic #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
4086af75240SNemanja Ivanovic LLVM_DUMP_METHOD void PPCReduceCRLogicals::CRLogicalOpInfo::dump() {
4096f590bf8SNemanja Ivanovic   dbgs() << "CRLogicalOpMI: ";
4106f590bf8SNemanja Ivanovic   MI->dump();
4116f590bf8SNemanja Ivanovic   dbgs() << "IsBinary: " << IsBinary << ", FeedsISEL: " << FeedsISEL;
4126f590bf8SNemanja Ivanovic   dbgs() << ", FeedsBR: " << FeedsBR << ", FeedsLogical: ";
4136f590bf8SNemanja Ivanovic   dbgs() << FeedsLogical << ", SingleUse: " << SingleUse;
4146f590bf8SNemanja Ivanovic   dbgs() << ", DefsSingleUse: " << DefsSingleUse;
4156f590bf8SNemanja Ivanovic   dbgs() << ", SubregDef1: " << SubregDef1 << ", SubregDef2: ";
4166f590bf8SNemanja Ivanovic   dbgs() << SubregDef2 << ", ContainedInBlock: " << ContainedInBlock;
4176f590bf8SNemanja Ivanovic   if (!IsNullary) {
4186f590bf8SNemanja Ivanovic     dbgs() << "\nDefs:\n";
4196f590bf8SNemanja Ivanovic     TrueDefs.first->dump();
4206f590bf8SNemanja Ivanovic   }
4216f590bf8SNemanja Ivanovic   if (IsBinary)
4226f590bf8SNemanja Ivanovic     TrueDefs.second->dump();
4236f590bf8SNemanja Ivanovic   dbgs() << "\n";
4246f590bf8SNemanja Ivanovic   if (CopyDefs.first) {
4256f590bf8SNemanja Ivanovic     dbgs() << "CopyDef1: ";
4266f590bf8SNemanja Ivanovic     CopyDefs.first->dump();
4276f590bf8SNemanja Ivanovic   }
4286f590bf8SNemanja Ivanovic   if (CopyDefs.second) {
4296f590bf8SNemanja Ivanovic     dbgs() << "CopyDef2: ";
4306f590bf8SNemanja Ivanovic     CopyDefs.second->dump();
4316f590bf8SNemanja Ivanovic   }
4326f590bf8SNemanja Ivanovic }
4336af75240SNemanja Ivanovic #endif
4346f590bf8SNemanja Ivanovic 
4356f590bf8SNemanja Ivanovic PPCReduceCRLogicals::CRLogicalOpInfo
4366f590bf8SNemanja Ivanovic PPCReduceCRLogicals::createCRLogicalOpInfo(MachineInstr &MIParam) {
4376f590bf8SNemanja Ivanovic   CRLogicalOpInfo Ret;
4386f590bf8SNemanja Ivanovic   Ret.MI = &MIParam;
4396f590bf8SNemanja Ivanovic   // Get the defs
4406f590bf8SNemanja Ivanovic   if (isNullary(MIParam)) {
4416f590bf8SNemanja Ivanovic     Ret.IsNullary = 1;
4426f590bf8SNemanja Ivanovic     Ret.TrueDefs = std::make_pair(nullptr, nullptr);
4436f590bf8SNemanja Ivanovic     Ret.CopyDefs = std::make_pair(nullptr, nullptr);
4446f590bf8SNemanja Ivanovic   } else {
4456f590bf8SNemanja Ivanovic     MachineInstr *Def1 = lookThroughCRCopy(MIParam.getOperand(1).getReg(),
4466f590bf8SNemanja Ivanovic                                            Ret.SubregDef1, Ret.CopyDefs.first);
4476f590bf8SNemanja Ivanovic     Ret.DefsSingleUse &=
4486f590bf8SNemanja Ivanovic       MRI->hasOneNonDBGUse(Def1->getOperand(0).getReg());
4496f590bf8SNemanja Ivanovic     Ret.DefsSingleUse &=
4506f590bf8SNemanja Ivanovic       MRI->hasOneNonDBGUse(Ret.CopyDefs.first->getOperand(0).getReg());
4516f590bf8SNemanja Ivanovic     assert(Def1 && "Must be able to find a definition of operand 1.");
4526f590bf8SNemanja Ivanovic     if (isBinary(MIParam)) {
4536f590bf8SNemanja Ivanovic       Ret.IsBinary = 1;
4546f590bf8SNemanja Ivanovic       MachineInstr *Def2 = lookThroughCRCopy(MIParam.getOperand(2).getReg(),
4556f590bf8SNemanja Ivanovic                                              Ret.SubregDef2,
4566f590bf8SNemanja Ivanovic                                              Ret.CopyDefs.second);
4576f590bf8SNemanja Ivanovic       Ret.DefsSingleUse &=
4586f590bf8SNemanja Ivanovic         MRI->hasOneNonDBGUse(Def2->getOperand(0).getReg());
4596f590bf8SNemanja Ivanovic       Ret.DefsSingleUse &=
4606f590bf8SNemanja Ivanovic         MRI->hasOneNonDBGUse(Ret.CopyDefs.second->getOperand(0).getReg());
4616f590bf8SNemanja Ivanovic       assert(Def2 && "Must be able to find a definition of operand 2.");
4626f590bf8SNemanja Ivanovic       Ret.TrueDefs = std::make_pair(Def1, Def2);
4636f590bf8SNemanja Ivanovic     } else {
4646f590bf8SNemanja Ivanovic       Ret.TrueDefs = std::make_pair(Def1, nullptr);
4656f590bf8SNemanja Ivanovic       Ret.CopyDefs.second = nullptr;
4666f590bf8SNemanja Ivanovic     }
4676f590bf8SNemanja Ivanovic   }
4686f590bf8SNemanja Ivanovic 
4696f590bf8SNemanja Ivanovic   Ret.ContainedInBlock = 1;
4706f590bf8SNemanja Ivanovic   // Get the uses
4716f590bf8SNemanja Ivanovic   for (MachineInstr &UseMI :
4726f590bf8SNemanja Ivanovic        MRI->use_nodbg_instructions(MIParam.getOperand(0).getReg())) {
4736f590bf8SNemanja Ivanovic     unsigned Opc = UseMI.getOpcode();
4746f590bf8SNemanja Ivanovic     if (Opc == PPC::ISEL || Opc == PPC::ISEL8)
4756f590bf8SNemanja Ivanovic       Ret.FeedsISEL = 1;
4766f590bf8SNemanja Ivanovic     if (Opc == PPC::BC || Opc == PPC::BCn || Opc == PPC::BCLR ||
4776f590bf8SNemanja Ivanovic         Opc == PPC::BCLRn)
4786f590bf8SNemanja Ivanovic       Ret.FeedsBR = 1;
4796f590bf8SNemanja Ivanovic     Ret.FeedsLogical = isCRLogical(UseMI);
4806f590bf8SNemanja Ivanovic     if (UseMI.getParent() != MIParam.getParent())
4816f590bf8SNemanja Ivanovic       Ret.ContainedInBlock = 0;
4826f590bf8SNemanja Ivanovic   }
4836f590bf8SNemanja Ivanovic   Ret.SingleUse = MRI->hasOneNonDBGUse(MIParam.getOperand(0).getReg()) ? 1 : 0;
4846f590bf8SNemanja Ivanovic 
4856f590bf8SNemanja Ivanovic   // We now know whether all the uses of the CR logical are in the same block.
4866f590bf8SNemanja Ivanovic   if (!Ret.IsNullary) {
4876f590bf8SNemanja Ivanovic     Ret.ContainedInBlock &=
4886f590bf8SNemanja Ivanovic       (MIParam.getParent() == Ret.TrueDefs.first->getParent());
4896f590bf8SNemanja Ivanovic     if (Ret.IsBinary)
4906f590bf8SNemanja Ivanovic       Ret.ContainedInBlock &=
4916f590bf8SNemanja Ivanovic         (MIParam.getParent() == Ret.TrueDefs.second->getParent());
4926f590bf8SNemanja Ivanovic   }
4936f590bf8SNemanja Ivanovic   DEBUG(Ret.dump());
4946f590bf8SNemanja Ivanovic   if (Ret.IsBinary && Ret.ContainedInBlock && Ret.SingleUse) {
4956f590bf8SNemanja Ivanovic     NumContainedSingleUseBinOps++;
4966f590bf8SNemanja Ivanovic     if (Ret.FeedsBR && Ret.DefsSingleUse)
4976f590bf8SNemanja Ivanovic       NumToSplitBlocks++;
4986f590bf8SNemanja Ivanovic   }
4996f590bf8SNemanja Ivanovic   return Ret;
5006f590bf8SNemanja Ivanovic }
5016f590bf8SNemanja Ivanovic 
5026f590bf8SNemanja Ivanovic /// Looks trhough a COPY instruction to the actual definition of the CR-bit
5036f590bf8SNemanja Ivanovic /// register and returns the instruction that defines it.
5046f590bf8SNemanja Ivanovic /// FIXME: This currently handles what is by-far the most common case:
5056f590bf8SNemanja Ivanovic /// an instruction that defines a CR field followed by a single copy of a bit
5066f590bf8SNemanja Ivanovic /// from that field into a virtual register. If chains of copies need to be
5076f590bf8SNemanja Ivanovic /// handled, this should have a loop until a non-copy instruction is found.
5086f590bf8SNemanja Ivanovic MachineInstr *PPCReduceCRLogicals::lookThroughCRCopy(unsigned Reg,
5096f590bf8SNemanja Ivanovic                                                      unsigned &Subreg,
5106f590bf8SNemanja Ivanovic                                                      MachineInstr *&CpDef) {
5116f590bf8SNemanja Ivanovic   Subreg = -1;
5126f590bf8SNemanja Ivanovic   if (!TargetRegisterInfo::isVirtualRegister(Reg))
5136f590bf8SNemanja Ivanovic     return nullptr;
5146f590bf8SNemanja Ivanovic   MachineInstr *Copy = MRI->getVRegDef(Reg);
5156f590bf8SNemanja Ivanovic   CpDef = Copy;
5166f590bf8SNemanja Ivanovic   if (!Copy->isCopy())
5176f590bf8SNemanja Ivanovic     return Copy;
5186f590bf8SNemanja Ivanovic   unsigned CopySrc = Copy->getOperand(1).getReg();
5196f590bf8SNemanja Ivanovic   Subreg = Copy->getOperand(1).getSubReg();
5206f590bf8SNemanja Ivanovic   if (!TargetRegisterInfo::isVirtualRegister(CopySrc)) {
5216f590bf8SNemanja Ivanovic     const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
5226f590bf8SNemanja Ivanovic     // Set the Subreg
5236f590bf8SNemanja Ivanovic     if (CopySrc == PPC::CR0EQ || CopySrc == PPC::CR6EQ)
5246f590bf8SNemanja Ivanovic       Subreg = PPC::sub_eq;
5256f590bf8SNemanja Ivanovic     if (CopySrc == PPC::CR0LT || CopySrc == PPC::CR6LT)
5266f590bf8SNemanja Ivanovic       Subreg = PPC::sub_lt;
5276f590bf8SNemanja Ivanovic     if (CopySrc == PPC::CR0GT || CopySrc == PPC::CR6GT)
5286f590bf8SNemanja Ivanovic       Subreg = PPC::sub_gt;
5296f590bf8SNemanja Ivanovic     if (CopySrc == PPC::CR0UN || CopySrc == PPC::CR6UN)
5306f590bf8SNemanja Ivanovic       Subreg = PPC::sub_un;
5316f590bf8SNemanja Ivanovic     // Loop backwards and return the first MI that modifies the physical CR Reg.
5326f590bf8SNemanja Ivanovic     MachineBasicBlock::iterator Me = Copy, B = Copy->getParent()->begin();
5336f590bf8SNemanja Ivanovic     while (Me != B)
5346f590bf8SNemanja Ivanovic       if ((--Me)->modifiesRegister(CopySrc, TRI))
5356f590bf8SNemanja Ivanovic         return &*Me;
5366f590bf8SNemanja Ivanovic     return nullptr;
5376f590bf8SNemanja Ivanovic   }
5386f590bf8SNemanja Ivanovic   return MRI->getVRegDef(CopySrc);
5396f590bf8SNemanja Ivanovic }
5406f590bf8SNemanja Ivanovic 
5416f590bf8SNemanja Ivanovic void PPCReduceCRLogicals::initialize(MachineFunction &MFParam) {
5426f590bf8SNemanja Ivanovic   MF = &MFParam;
5436f590bf8SNemanja Ivanovic   MRI = &MF->getRegInfo();
5446f590bf8SNemanja Ivanovic   TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo();
5456f590bf8SNemanja Ivanovic   MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
5466f590bf8SNemanja Ivanovic 
5476f590bf8SNemanja Ivanovic   AllCRLogicalOps.clear();
5486f590bf8SNemanja Ivanovic }
5496f590bf8SNemanja Ivanovic 
5506f590bf8SNemanja Ivanovic /// Contains all the implemented transformations on CR logical operations.
5516f590bf8SNemanja Ivanovic /// For example, a binary CR logical can be used to split a block on its inputs,
5526f590bf8SNemanja Ivanovic /// a unary CR logical might be used to change the condition code on a
5536f590bf8SNemanja Ivanovic /// comparison feeding it. A nullary CR logical might simply be removable
5546f590bf8SNemanja Ivanovic /// if the user of the bit it [un]sets can be transformed.
5556f590bf8SNemanja Ivanovic bool PPCReduceCRLogicals::handleCROp(CRLogicalOpInfo &CRI) {
5566f590bf8SNemanja Ivanovic   // We can definitely split a block on the inputs to a binary CR operation
5576f590bf8SNemanja Ivanovic   // whose defs and (single) use are within the same block.
5586f590bf8SNemanja Ivanovic   bool Changed = false;
5596f590bf8SNemanja Ivanovic   if (CRI.IsBinary && CRI.ContainedInBlock && CRI.SingleUse && CRI.FeedsBR &&
5606f590bf8SNemanja Ivanovic       CRI.DefsSingleUse) {
5616f590bf8SNemanja Ivanovic     Changed = splitBlockOnBinaryCROp(CRI);
5626f590bf8SNemanja Ivanovic     if (Changed)
5636f590bf8SNemanja Ivanovic       NumBlocksSplitOnBinaryCROp++;
5646f590bf8SNemanja Ivanovic   }
5656f590bf8SNemanja Ivanovic   return Changed;
5666f590bf8SNemanja Ivanovic }
5676f590bf8SNemanja Ivanovic 
5686f590bf8SNemanja Ivanovic /// Splits a block that contains a CR-logical operation that feeds a branch
5696f590bf8SNemanja Ivanovic /// and whose operands are produced within the block.
5706f590bf8SNemanja Ivanovic /// Example:
5716f590bf8SNemanja Ivanovic ///    %vr5<def> = CMPDI %vr2, 0; CRRC:%vr5 G8RC:%vr2
5726f590bf8SNemanja Ivanovic ///    %vr6<def> = COPY %vr5:sub_eq; CRBITRC:%vr6 CRRC:%vr5
5736f590bf8SNemanja Ivanovic ///    %vr7<def> = CMPDI %vr3, 0; CRRC:%vr7 G8RC:%vr3
5746f590bf8SNemanja Ivanovic ///    %vr8<def> = COPY %vr7:sub_eq; CRBITRC:%vr8 CRRC:%vr7
5756f590bf8SNemanja Ivanovic ///    %vr9<def> = CROR %vr6<kill>, %vr8<kill>; CRBITRC:%vr9,%vr6,%vr8
5766f590bf8SNemanja Ivanovic ///    BC %vr9<kill>, <BB#2>; CRBITRC:%vr9
5776f590bf8SNemanja Ivanovic /// Becomes:
5786f590bf8SNemanja Ivanovic ///    %vr5<def> = CMPDI %vr2, 0; CRRC:%vr5 G8RC:%vr2
5796f590bf8SNemanja Ivanovic ///    %vr6<def> = COPY %vr5:sub_eq; CRBITRC:%vr6 CRRC:%vr5
5806f590bf8SNemanja Ivanovic ///    BC %vr6<kill>, <BB#2>; CRBITRC:%vr6
5816f590bf8SNemanja Ivanovic ///
5826f590bf8SNemanja Ivanovic ///    %vr7<def> = CMPDI %vr3, 0; CRRC:%vr7 G8RC:%vr3
5836f590bf8SNemanja Ivanovic ///    %vr8<def> = COPY %vr7:sub_eq; CRBITRC:%vr8 CRRC:%vr7
5846f590bf8SNemanja Ivanovic ///    BC %vr9<kill>, <BB#2>; CRBITRC:%vr9
5856f590bf8SNemanja Ivanovic bool PPCReduceCRLogicals::splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI) {
5866f590bf8SNemanja Ivanovic   if (CRI.CopyDefs.first == CRI.CopyDefs.second) {
5876f590bf8SNemanja Ivanovic     DEBUG(dbgs() << "Unable to split as the two operands are the same\n");
5886f590bf8SNemanja Ivanovic     NumNotSplitIdenticalOperands++;
5896f590bf8SNemanja Ivanovic     return false;
5906f590bf8SNemanja Ivanovic   }
5916f590bf8SNemanja Ivanovic   if (CRI.TrueDefs.first->isCopy() || CRI.TrueDefs.second->isCopy() ||
5926f590bf8SNemanja Ivanovic       CRI.TrueDefs.first->isPHI() || CRI.TrueDefs.second->isPHI()) {
5936f590bf8SNemanja Ivanovic     DEBUG(dbgs() << "Unable to split because one of the operands is a PHI or "
5946f590bf8SNemanja Ivanovic           "chain of copies.\n");
5956f590bf8SNemanja Ivanovic     NumNotSplitChainCopies++;
5966f590bf8SNemanja Ivanovic     return false;
5976f590bf8SNemanja Ivanovic   }
5986f590bf8SNemanja Ivanovic   // Note: keep in sync with computeBranchTargetAndInversion().
5996f590bf8SNemanja Ivanovic   if (CRI.MI->getOpcode() != PPC::CROR &&
6006f590bf8SNemanja Ivanovic       CRI.MI->getOpcode() != PPC::CRAND &&
6016f590bf8SNemanja Ivanovic       CRI.MI->getOpcode() != PPC::CRNOR &&
6026f590bf8SNemanja Ivanovic       CRI.MI->getOpcode() != PPC::CRNAND &&
6036f590bf8SNemanja Ivanovic       CRI.MI->getOpcode() != PPC::CRORC &&
6046f590bf8SNemanja Ivanovic       CRI.MI->getOpcode() != PPC::CRANDC) {
6056f590bf8SNemanja Ivanovic     DEBUG(dbgs() << "Unable to split blocks on this opcode.\n");
6066f590bf8SNemanja Ivanovic     NumNotSplitWrongOpcode++;
6076f590bf8SNemanja Ivanovic     return false;
6086f590bf8SNemanja Ivanovic   }
6096f590bf8SNemanja Ivanovic   DEBUG(dbgs() << "Splitting the following CR op:\n"; CRI.dump());
6106f590bf8SNemanja Ivanovic   MachineBasicBlock::iterator Def1It = CRI.TrueDefs.first;
6116f590bf8SNemanja Ivanovic   MachineBasicBlock::iterator Def2It = CRI.TrueDefs.second;
6126f590bf8SNemanja Ivanovic 
6136f590bf8SNemanja Ivanovic   bool UsingDef1 = false;
6146f590bf8SNemanja Ivanovic   MachineInstr *SplitBefore = &*Def2It;
6156f590bf8SNemanja Ivanovic   for (auto E = CRI.MI->getParent()->end(); Def2It != E; ++Def2It) {
6166f590bf8SNemanja Ivanovic     if (Def1It == Def2It) { // Def2 comes before Def1.
6176f590bf8SNemanja Ivanovic       SplitBefore = &*Def1It;
6186f590bf8SNemanja Ivanovic       UsingDef1 = true;
6196f590bf8SNemanja Ivanovic       break;
6206f590bf8SNemanja Ivanovic     }
6216f590bf8SNemanja Ivanovic   }
6226f590bf8SNemanja Ivanovic 
6236f590bf8SNemanja Ivanovic   DEBUG(dbgs() << "We will split the following block:\n";);
6246f590bf8SNemanja Ivanovic   DEBUG(CRI.MI->getParent()->dump());
6256f590bf8SNemanja Ivanovic   DEBUG(dbgs() << "Before instruction:\n"; SplitBefore->dump());
6266f590bf8SNemanja Ivanovic 
6276f590bf8SNemanja Ivanovic   // Get the branch instruction.
6286f590bf8SNemanja Ivanovic   MachineInstr *Branch =
6296f590bf8SNemanja Ivanovic     MRI->use_nodbg_begin(CRI.MI->getOperand(0).getReg())->getParent();
6306f590bf8SNemanja Ivanovic 
6316f590bf8SNemanja Ivanovic   // We want the new block to have no code in it other than the definition
6326f590bf8SNemanja Ivanovic   // of the input to the CR logical and the CR logical itself. So we move
6336f590bf8SNemanja Ivanovic   // those to the bottom of the block (just before the branch). Then we
6346f590bf8SNemanja Ivanovic   // will split before the CR logical.
6356f590bf8SNemanja Ivanovic   MachineBasicBlock *MBB = SplitBefore->getParent();
6366f590bf8SNemanja Ivanovic   auto FirstTerminator = MBB->getFirstTerminator();
6376f590bf8SNemanja Ivanovic   MachineBasicBlock::iterator FirstInstrToMove =
6386f590bf8SNemanja Ivanovic     UsingDef1 ? CRI.TrueDefs.first : CRI.TrueDefs.second;
6396f590bf8SNemanja Ivanovic   MachineBasicBlock::iterator SecondInstrToMove =
6406f590bf8SNemanja Ivanovic     UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second;
6416f590bf8SNemanja Ivanovic 
6426f590bf8SNemanja Ivanovic   // The instructions that need to be moved are not guaranteed to be
6436f590bf8SNemanja Ivanovic   // contiguous. Move them individually.
6446f590bf8SNemanja Ivanovic   // FIXME: If one of the operands is a chain of (single use) copies, they
6456f590bf8SNemanja Ivanovic   // can all be moved and we can still split.
6466f590bf8SNemanja Ivanovic   MBB->splice(FirstTerminator, MBB, FirstInstrToMove);
6476f590bf8SNemanja Ivanovic   if (FirstInstrToMove != SecondInstrToMove)
6486f590bf8SNemanja Ivanovic     MBB->splice(FirstTerminator, MBB, SecondInstrToMove);
6496f590bf8SNemanja Ivanovic   MBB->splice(FirstTerminator, MBB, CRI.MI);
6506f590bf8SNemanja Ivanovic 
6516f590bf8SNemanja Ivanovic   unsigned Opc = CRI.MI->getOpcode();
6526f590bf8SNemanja Ivanovic   bool InvertOrigBranch, InvertNewBranch, TargetIsFallThrough;
6536f590bf8SNemanja Ivanovic   computeBranchTargetAndInversion(Opc, Branch->getOpcode(), UsingDef1,
6546f590bf8SNemanja Ivanovic                                   InvertNewBranch, InvertOrigBranch,
6556f590bf8SNemanja Ivanovic                                   TargetIsFallThrough);
6566f590bf8SNemanja Ivanovic   MachineInstr *SplitCond =
6576f590bf8SNemanja Ivanovic     UsingDef1 ? CRI.CopyDefs.second : CRI.CopyDefs.first;
6586f590bf8SNemanja Ivanovic   DEBUG(dbgs() << "We will " <<  (InvertNewBranch ? "invert" : "copy"));
6596f590bf8SNemanja Ivanovic   DEBUG(dbgs() << " the original branch and the target is the " <<
6606f590bf8SNemanja Ivanovic         (TargetIsFallThrough ? "fallthrough block\n" : "orig. target block\n"));
6616f590bf8SNemanja Ivanovic   DEBUG(dbgs() << "Original branch instruction: "; Branch->dump());
6626f590bf8SNemanja Ivanovic   BlockSplitInfo BSI { Branch, SplitBefore, SplitCond, InvertNewBranch,
6636f590bf8SNemanja Ivanovic     InvertOrigBranch, TargetIsFallThrough, MBPI, CRI.MI,
6646f590bf8SNemanja Ivanovic     UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second };
6656f590bf8SNemanja Ivanovic   bool Changed = splitMBB(BSI);
6666f590bf8SNemanja Ivanovic   // If we've split on a CR logical that is fed by a CR logical,
6676f590bf8SNemanja Ivanovic   // recompute the source CR logical as it may be usable for splitting.
6686f590bf8SNemanja Ivanovic   if (Changed) {
6696f590bf8SNemanja Ivanovic     bool Input1CRlogical =
6706f590bf8SNemanja Ivanovic       CRI.TrueDefs.first && isCRLogical(*CRI.TrueDefs.first);
6716f590bf8SNemanja Ivanovic     bool Input2CRlogical =
6726f590bf8SNemanja Ivanovic       CRI.TrueDefs.second && isCRLogical(*CRI.TrueDefs.second);
6736f590bf8SNemanja Ivanovic     if (Input1CRlogical)
6746f590bf8SNemanja Ivanovic       AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.first));
6756f590bf8SNemanja Ivanovic     if (Input2CRlogical)
6766f590bf8SNemanja Ivanovic       AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.second));
6776f590bf8SNemanja Ivanovic   }
6786f590bf8SNemanja Ivanovic   return Changed;
6796f590bf8SNemanja Ivanovic }
6806f590bf8SNemanja Ivanovic 
6816f590bf8SNemanja Ivanovic void PPCReduceCRLogicals::collectCRLogicals() {
6826f590bf8SNemanja Ivanovic   for (MachineBasicBlock &MBB : *MF) {
6836f590bf8SNemanja Ivanovic     for (MachineInstr &MI : MBB) {
6846f590bf8SNemanja Ivanovic       if (isCRLogical(MI)) {
6856f590bf8SNemanja Ivanovic         AllCRLogicalOps.push_back(createCRLogicalOpInfo(MI));
6866f590bf8SNemanja Ivanovic         TotalCRLogicals++;
6876f590bf8SNemanja Ivanovic         if (AllCRLogicalOps.back().IsNullary)
6886f590bf8SNemanja Ivanovic           TotalNullaryCRLogicals++;
6896f590bf8SNemanja Ivanovic         else if (AllCRLogicalOps.back().IsBinary)
6906f590bf8SNemanja Ivanovic           TotalBinaryCRLogicals++;
6916f590bf8SNemanja Ivanovic         else
6926f590bf8SNemanja Ivanovic           TotalUnaryCRLogicals++;
6936f590bf8SNemanja Ivanovic       }
6946f590bf8SNemanja Ivanovic     }
6956f590bf8SNemanja Ivanovic   }
6966f590bf8SNemanja Ivanovic }
6976f590bf8SNemanja Ivanovic 
6986f590bf8SNemanja Ivanovic } // end annonymous namespace
6996f590bf8SNemanja Ivanovic 
7006f590bf8SNemanja Ivanovic INITIALIZE_PASS_BEGIN(PPCReduceCRLogicals, DEBUG_TYPE,
7016f590bf8SNemanja Ivanovic                       "PowerPC Reduce CR logical Operation", false, false)
7026f590bf8SNemanja Ivanovic INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
7036f590bf8SNemanja Ivanovic INITIALIZE_PASS_END(PPCReduceCRLogicals, DEBUG_TYPE,
7046f590bf8SNemanja Ivanovic                     "PowerPC Reduce CR logical Operation", false, false)
7056f590bf8SNemanja Ivanovic 
7066f590bf8SNemanja Ivanovic char PPCReduceCRLogicals::ID = 0;
7076f590bf8SNemanja Ivanovic FunctionPass*
7086f590bf8SNemanja Ivanovic llvm::createPPCReduceCRLogicalsPass() { return new PPCReduceCRLogicals(); }
709