1*6f590bf8SNemanja Ivanovic //===---- PPCReduceCRLogicals.cpp - Reduce CR Bit Logical operations ------===//
2*6f590bf8SNemanja Ivanovic //
3*6f590bf8SNemanja Ivanovic //                     The LLVM Compiler Infrastructure
4*6f590bf8SNemanja Ivanovic //
5*6f590bf8SNemanja Ivanovic // This file is distributed under the University of Illinois Open Source
6*6f590bf8SNemanja Ivanovic // License. See LICENSE.TXT for details.
7*6f590bf8SNemanja Ivanovic //
8*6f590bf8SNemanja Ivanovic //===---------------------------------------------------------------------===//
9*6f590bf8SNemanja Ivanovic //
10*6f590bf8SNemanja Ivanovic // This pass aims to reduce the number of logical operations on bits in the CR
11*6f590bf8SNemanja Ivanovic // register. These instructions have a fairly high latency and only a single
12*6f590bf8SNemanja Ivanovic // pipeline at their disposal in modern PPC cores. Furthermore, they have a
13*6f590bf8SNemanja Ivanovic // tendency to occur in fairly small blocks where there's little opportunity
14*6f590bf8SNemanja Ivanovic // to hide the latency between the CR logical operation and its user.
15*6f590bf8SNemanja Ivanovic //
16*6f590bf8SNemanja Ivanovic //===---------------------------------------------------------------------===//
17*6f590bf8SNemanja Ivanovic 
18*6f590bf8SNemanja Ivanovic #include "PPCInstrInfo.h"
19*6f590bf8SNemanja Ivanovic #include "PPC.h"
20*6f590bf8SNemanja Ivanovic #include "PPCTargetMachine.h"
21*6f590bf8SNemanja Ivanovic #include "llvm/CodeGen/MachineFunctionPass.h"
22*6f590bf8SNemanja Ivanovic #include "llvm/CodeGen/MachineDominators.h"
23*6f590bf8SNemanja Ivanovic #include "llvm/Support/Debug.h"
24*6f590bf8SNemanja Ivanovic #include "llvm/ADT/Statistic.h"
25*6f590bf8SNemanja Ivanovic 
26*6f590bf8SNemanja Ivanovic using namespace llvm;
27*6f590bf8SNemanja Ivanovic 
28*6f590bf8SNemanja Ivanovic #define DEBUG_TYPE "ppc-reduce-cr-ops"
29*6f590bf8SNemanja Ivanovic #include "PPCMachineBasicBlockUtils.h"
30*6f590bf8SNemanja Ivanovic 
31*6f590bf8SNemanja Ivanovic STATISTIC(NumContainedSingleUseBinOps,
32*6f590bf8SNemanja Ivanovic           "Number of single-use binary CR logical ops contained in a block");
33*6f590bf8SNemanja Ivanovic STATISTIC(NumToSplitBlocks,
34*6f590bf8SNemanja Ivanovic           "Number of binary CR logical ops that can be used to split blocks");
35*6f590bf8SNemanja Ivanovic STATISTIC(TotalCRLogicals, "Number of CR logical ops.");
36*6f590bf8SNemanja Ivanovic STATISTIC(TotalNullaryCRLogicals,
37*6f590bf8SNemanja Ivanovic           "Number of nullary CR logical ops (CRSET/CRUNSET).");
38*6f590bf8SNemanja Ivanovic STATISTIC(TotalUnaryCRLogicals, "Number of unary CR logical ops.");
39*6f590bf8SNemanja Ivanovic STATISTIC(TotalBinaryCRLogicals, "Number of CR logical ops.");
40*6f590bf8SNemanja Ivanovic STATISTIC(NumBlocksSplitOnBinaryCROp,
41*6f590bf8SNemanja Ivanovic           "Number of blocks split on CR binary logical ops.");
42*6f590bf8SNemanja Ivanovic STATISTIC(NumNotSplitIdenticalOperands,
43*6f590bf8SNemanja Ivanovic           "Number of blocks not split due to operands being identical.");
44*6f590bf8SNemanja Ivanovic STATISTIC(NumNotSplitChainCopies,
45*6f590bf8SNemanja Ivanovic           "Number of blocks not split due to operands being chained copies.");
46*6f590bf8SNemanja Ivanovic STATISTIC(NumNotSplitWrongOpcode,
47*6f590bf8SNemanja Ivanovic           "Number of blocks not split due to the wrong opcode.");
48*6f590bf8SNemanja Ivanovic 
49*6f590bf8SNemanja Ivanovic namespace llvm {
50*6f590bf8SNemanja Ivanovic   void initializePPCReduceCRLogicalsPass(PassRegistry&);
51*6f590bf8SNemanja Ivanovic }
52*6f590bf8SNemanja Ivanovic 
53*6f590bf8SNemanja Ivanovic namespace {
54*6f590bf8SNemanja Ivanovic 
55*6f590bf8SNemanja Ivanovic static bool isBinary(MachineInstr &MI) {
56*6f590bf8SNemanja Ivanovic   return MI.getNumOperands() == 3;
57*6f590bf8SNemanja Ivanovic }
58*6f590bf8SNemanja Ivanovic 
59*6f590bf8SNemanja Ivanovic static bool isNullary(MachineInstr &MI) {
60*6f590bf8SNemanja Ivanovic   return MI.getNumOperands() == 1;
61*6f590bf8SNemanja Ivanovic }
62*6f590bf8SNemanja Ivanovic 
63*6f590bf8SNemanja Ivanovic /// Given a CR logical operation \p CROp, branch opcode \p BROp as well as
64*6f590bf8SNemanja Ivanovic /// a flag to indicate if the first operand of \p CROp is used as the
65*6f590bf8SNemanja Ivanovic /// SplitBefore operand, determines whether either of the branches are to be
66*6f590bf8SNemanja Ivanovic /// inverted as well as whether the new target should be the original
67*6f590bf8SNemanja Ivanovic /// fall-through block.
68*6f590bf8SNemanja Ivanovic static void
69*6f590bf8SNemanja Ivanovic computeBranchTargetAndInversion(unsigned CROp, unsigned BROp, bool UsingDef1,
70*6f590bf8SNemanja Ivanovic                                 bool &InvertNewBranch, bool &InvertOrigBranch,
71*6f590bf8SNemanja Ivanovic                                 bool &TargetIsFallThrough) {
72*6f590bf8SNemanja Ivanovic   // The conditions under which each of the output operands should be [un]set
73*6f590bf8SNemanja Ivanovic   // can certainly be written much more concisely with just 3 if statements or
74*6f590bf8SNemanja Ivanovic   // ternary expressions. However, this provides a much clearer overview to the
75*6f590bf8SNemanja Ivanovic   // reader as to what is set for each <CROp, BROp, OpUsed> combination.
76*6f590bf8SNemanja Ivanovic   if (BROp == PPC::BC || BROp == PPC::BCLR) {
77*6f590bf8SNemanja Ivanovic     // Regular branches.
78*6f590bf8SNemanja Ivanovic     switch (CROp) {
79*6f590bf8SNemanja Ivanovic     default:
80*6f590bf8SNemanja Ivanovic       llvm_unreachable("Don't know how to handle this CR logical.");
81*6f590bf8SNemanja Ivanovic     case PPC::CROR:
82*6f590bf8SNemanja Ivanovic       InvertNewBranch = false;
83*6f590bf8SNemanja Ivanovic       InvertOrigBranch = false;
84*6f590bf8SNemanja Ivanovic       TargetIsFallThrough = false;
85*6f590bf8SNemanja Ivanovic       return;
86*6f590bf8SNemanja Ivanovic     case PPC::CRAND:
87*6f590bf8SNemanja Ivanovic       InvertNewBranch = true;
88*6f590bf8SNemanja Ivanovic       InvertOrigBranch = false;
89*6f590bf8SNemanja Ivanovic       TargetIsFallThrough = true;
90*6f590bf8SNemanja Ivanovic       return;
91*6f590bf8SNemanja Ivanovic     case PPC::CRNAND:
92*6f590bf8SNemanja Ivanovic       InvertNewBranch = true;
93*6f590bf8SNemanja Ivanovic       InvertOrigBranch = true;
94*6f590bf8SNemanja Ivanovic       TargetIsFallThrough = false;
95*6f590bf8SNemanja Ivanovic       return;
96*6f590bf8SNemanja Ivanovic     case PPC::CRNOR:
97*6f590bf8SNemanja Ivanovic       InvertNewBranch = false;
98*6f590bf8SNemanja Ivanovic       InvertOrigBranch = true;
99*6f590bf8SNemanja Ivanovic       TargetIsFallThrough = true;
100*6f590bf8SNemanja Ivanovic       return;
101*6f590bf8SNemanja Ivanovic     case PPC::CRORC:
102*6f590bf8SNemanja Ivanovic       InvertNewBranch = UsingDef1;
103*6f590bf8SNemanja Ivanovic       InvertOrigBranch = !UsingDef1;
104*6f590bf8SNemanja Ivanovic       TargetIsFallThrough = false;
105*6f590bf8SNemanja Ivanovic       return;
106*6f590bf8SNemanja Ivanovic     case PPC::CRANDC:
107*6f590bf8SNemanja Ivanovic       InvertNewBranch = !UsingDef1;
108*6f590bf8SNemanja Ivanovic       InvertOrigBranch = !UsingDef1;
109*6f590bf8SNemanja Ivanovic       TargetIsFallThrough = true;
110*6f590bf8SNemanja Ivanovic       return;
111*6f590bf8SNemanja Ivanovic     }
112*6f590bf8SNemanja Ivanovic   } else if (BROp == PPC::BCn || BROp == PPC::BCLRn) {
113*6f590bf8SNemanja Ivanovic     // Negated branches.
114*6f590bf8SNemanja Ivanovic     switch (CROp) {
115*6f590bf8SNemanja Ivanovic     default:
116*6f590bf8SNemanja Ivanovic       llvm_unreachable("Don't know how to handle this CR logical.");
117*6f590bf8SNemanja Ivanovic     case PPC::CROR:
118*6f590bf8SNemanja Ivanovic       InvertNewBranch = true;
119*6f590bf8SNemanja Ivanovic       InvertOrigBranch = false;
120*6f590bf8SNemanja Ivanovic       TargetIsFallThrough = true;
121*6f590bf8SNemanja Ivanovic       return;
122*6f590bf8SNemanja Ivanovic     case PPC::CRAND:
123*6f590bf8SNemanja Ivanovic       InvertNewBranch = false;
124*6f590bf8SNemanja Ivanovic       InvertOrigBranch = false;
125*6f590bf8SNemanja Ivanovic       TargetIsFallThrough = false;
126*6f590bf8SNemanja Ivanovic       return;
127*6f590bf8SNemanja Ivanovic     case PPC::CRNAND:
128*6f590bf8SNemanja Ivanovic       InvertNewBranch = false;
129*6f590bf8SNemanja Ivanovic       InvertOrigBranch = true;
130*6f590bf8SNemanja Ivanovic       TargetIsFallThrough = true;
131*6f590bf8SNemanja Ivanovic       return;
132*6f590bf8SNemanja Ivanovic     case PPC::CRNOR:
133*6f590bf8SNemanja Ivanovic       InvertNewBranch = true;
134*6f590bf8SNemanja Ivanovic       InvertOrigBranch = true;
135*6f590bf8SNemanja Ivanovic       TargetIsFallThrough = false;
136*6f590bf8SNemanja Ivanovic       return;
137*6f590bf8SNemanja Ivanovic     case PPC::CRORC:
138*6f590bf8SNemanja Ivanovic       InvertNewBranch = !UsingDef1;
139*6f590bf8SNemanja Ivanovic       InvertOrigBranch = !UsingDef1;
140*6f590bf8SNemanja Ivanovic       TargetIsFallThrough = true;
141*6f590bf8SNemanja Ivanovic       return;
142*6f590bf8SNemanja Ivanovic     case PPC::CRANDC:
143*6f590bf8SNemanja Ivanovic       InvertNewBranch = UsingDef1;
144*6f590bf8SNemanja Ivanovic       InvertOrigBranch = !UsingDef1;
145*6f590bf8SNemanja Ivanovic       TargetIsFallThrough = false;
146*6f590bf8SNemanja Ivanovic       return;
147*6f590bf8SNemanja Ivanovic     }
148*6f590bf8SNemanja Ivanovic   } else
149*6f590bf8SNemanja Ivanovic     llvm_unreachable("Don't know how to handle this branch.");
150*6f590bf8SNemanja Ivanovic }
151*6f590bf8SNemanja Ivanovic 
152*6f590bf8SNemanja Ivanovic class PPCReduceCRLogicals : public MachineFunctionPass {
153*6f590bf8SNemanja Ivanovic 
154*6f590bf8SNemanja Ivanovic public:
155*6f590bf8SNemanja Ivanovic   static char ID;
156*6f590bf8SNemanja Ivanovic   struct CRLogicalOpInfo {
157*6f590bf8SNemanja Ivanovic     MachineInstr *MI;
158*6f590bf8SNemanja Ivanovic     // FIXME: If chains of copies are to be handled, this should be a vector.
159*6f590bf8SNemanja Ivanovic     std::pair<MachineInstr*, MachineInstr*> CopyDefs;
160*6f590bf8SNemanja Ivanovic     std::pair<MachineInstr*, MachineInstr*> TrueDefs;
161*6f590bf8SNemanja Ivanovic     unsigned IsBinary : 1;
162*6f590bf8SNemanja Ivanovic     unsigned IsNullary : 1;
163*6f590bf8SNemanja Ivanovic     unsigned ContainedInBlock : 1;
164*6f590bf8SNemanja Ivanovic     unsigned FeedsISEL : 1;
165*6f590bf8SNemanja Ivanovic     unsigned FeedsBR : 1;
166*6f590bf8SNemanja Ivanovic     unsigned FeedsLogical : 1;
167*6f590bf8SNemanja Ivanovic     unsigned SingleUse : 1;
168*6f590bf8SNemanja Ivanovic     unsigned DefsSingleUse : 1;
169*6f590bf8SNemanja Ivanovic     unsigned SubregDef1;
170*6f590bf8SNemanja Ivanovic     unsigned SubregDef2;
171*6f590bf8SNemanja Ivanovic     CRLogicalOpInfo() : MI(nullptr), IsBinary(0), IsNullary(0),
172*6f590bf8SNemanja Ivanovic                         ContainedInBlock(0), FeedsISEL(0), FeedsBR(0),
173*6f590bf8SNemanja Ivanovic                         FeedsLogical(0), SingleUse(0), DefsSingleUse(1),
174*6f590bf8SNemanja Ivanovic                         SubregDef1(0), SubregDef2(0) { }
175*6f590bf8SNemanja Ivanovic     void dump();
176*6f590bf8SNemanja Ivanovic   };
177*6f590bf8SNemanja Ivanovic 
178*6f590bf8SNemanja Ivanovic private:
179*6f590bf8SNemanja Ivanovic   const PPCInstrInfo *TII;
180*6f590bf8SNemanja Ivanovic   MachineFunction *MF;
181*6f590bf8SNemanja Ivanovic   MachineRegisterInfo *MRI;
182*6f590bf8SNemanja Ivanovic   const MachineBranchProbabilityInfo *MBPI;
183*6f590bf8SNemanja Ivanovic 
184*6f590bf8SNemanja Ivanovic   // A vector to contain all the CR logical operations
185*6f590bf8SNemanja Ivanovic   std::vector<CRLogicalOpInfo> AllCRLogicalOps;
186*6f590bf8SNemanja Ivanovic   void initialize(MachineFunction &MFParm);
187*6f590bf8SNemanja Ivanovic   void collectCRLogicals();
188*6f590bf8SNemanja Ivanovic   bool handleCROp(CRLogicalOpInfo &CRI);
189*6f590bf8SNemanja Ivanovic   bool splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI);
190*6f590bf8SNemanja Ivanovic   static bool isCRLogical(MachineInstr &MI) {
191*6f590bf8SNemanja Ivanovic     unsigned Opc = MI.getOpcode();
192*6f590bf8SNemanja Ivanovic     return Opc == PPC::CRAND || Opc == PPC::CRNAND || Opc == PPC::CROR ||
193*6f590bf8SNemanja Ivanovic       Opc == PPC::CRXOR || Opc == PPC::CRNOR || Opc == PPC::CREQV ||
194*6f590bf8SNemanja Ivanovic       Opc == PPC::CRANDC || Opc == PPC::CRORC || Opc == PPC::CRSET ||
195*6f590bf8SNemanja Ivanovic       Opc == PPC::CRUNSET || Opc == PPC::CR6SET || Opc == PPC::CR6UNSET;
196*6f590bf8SNemanja Ivanovic   }
197*6f590bf8SNemanja Ivanovic   bool simplifyCode() {
198*6f590bf8SNemanja Ivanovic     bool Changed = false;
199*6f590bf8SNemanja Ivanovic     // Not using a range-based for loop here as the vector may grow while being
200*6f590bf8SNemanja Ivanovic     // operated on.
201*6f590bf8SNemanja Ivanovic     for (unsigned i = 0; i < AllCRLogicalOps.size(); i++)
202*6f590bf8SNemanja Ivanovic       Changed |= handleCROp(AllCRLogicalOps[i]);
203*6f590bf8SNemanja Ivanovic     return Changed;
204*6f590bf8SNemanja Ivanovic   }
205*6f590bf8SNemanja Ivanovic 
206*6f590bf8SNemanja Ivanovic public:
207*6f590bf8SNemanja Ivanovic   PPCReduceCRLogicals() : MachineFunctionPass(ID) {
208*6f590bf8SNemanja Ivanovic     initializePPCReduceCRLogicalsPass(*PassRegistry::getPassRegistry());
209*6f590bf8SNemanja Ivanovic   }
210*6f590bf8SNemanja Ivanovic 
211*6f590bf8SNemanja Ivanovic   MachineInstr *lookThroughCRCopy(unsigned Reg, unsigned &Subreg,
212*6f590bf8SNemanja Ivanovic                                   MachineInstr *&CpDef);
213*6f590bf8SNemanja Ivanovic   bool runOnMachineFunction(MachineFunction &MF) override {
214*6f590bf8SNemanja Ivanovic     if (skipFunction(*MF.getFunction()))
215*6f590bf8SNemanja Ivanovic       return false;
216*6f590bf8SNemanja Ivanovic 
217*6f590bf8SNemanja Ivanovic     // If the subtarget doesn't use CR bits, there's nothing to do.
218*6f590bf8SNemanja Ivanovic     const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>();
219*6f590bf8SNemanja Ivanovic     if (!STI.useCRBits())
220*6f590bf8SNemanja Ivanovic       return false;
221*6f590bf8SNemanja Ivanovic 
222*6f590bf8SNemanja Ivanovic     initialize(MF);
223*6f590bf8SNemanja Ivanovic     collectCRLogicals();
224*6f590bf8SNemanja Ivanovic     return simplifyCode();
225*6f590bf8SNemanja Ivanovic   }
226*6f590bf8SNemanja Ivanovic   CRLogicalOpInfo createCRLogicalOpInfo(MachineInstr &MI);
227*6f590bf8SNemanja Ivanovic   void getAnalysisUsage(AnalysisUsage &AU) const override {
228*6f590bf8SNemanja Ivanovic     AU.addRequired<MachineBranchProbabilityInfo>();
229*6f590bf8SNemanja Ivanovic     AU.addRequired<MachineDominatorTree>();
230*6f590bf8SNemanja Ivanovic     MachineFunctionPass::getAnalysisUsage(AU);
231*6f590bf8SNemanja Ivanovic   }
232*6f590bf8SNemanja Ivanovic };
233*6f590bf8SNemanja Ivanovic 
234*6f590bf8SNemanja Ivanovic void PPCReduceCRLogicals::CRLogicalOpInfo::dump() {
235*6f590bf8SNemanja Ivanovic   dbgs() << "CRLogicalOpMI: ";
236*6f590bf8SNemanja Ivanovic   MI->dump();
237*6f590bf8SNemanja Ivanovic   dbgs() << "IsBinary: " << IsBinary << ", FeedsISEL: " << FeedsISEL;
238*6f590bf8SNemanja Ivanovic   dbgs() << ", FeedsBR: " << FeedsBR << ", FeedsLogical: ";
239*6f590bf8SNemanja Ivanovic   dbgs() << FeedsLogical << ", SingleUse: " << SingleUse;
240*6f590bf8SNemanja Ivanovic   dbgs() << ", DefsSingleUse: " << DefsSingleUse;
241*6f590bf8SNemanja Ivanovic   dbgs() << ", SubregDef1: " << SubregDef1 << ", SubregDef2: ";
242*6f590bf8SNemanja Ivanovic   dbgs() << SubregDef2 << ", ContainedInBlock: " << ContainedInBlock;
243*6f590bf8SNemanja Ivanovic   if (!IsNullary) {
244*6f590bf8SNemanja Ivanovic     dbgs() << "\nDefs:\n";
245*6f590bf8SNemanja Ivanovic     TrueDefs.first->dump();
246*6f590bf8SNemanja Ivanovic   }
247*6f590bf8SNemanja Ivanovic   if (IsBinary)
248*6f590bf8SNemanja Ivanovic     TrueDefs.second->dump();
249*6f590bf8SNemanja Ivanovic   dbgs() << "\n";
250*6f590bf8SNemanja Ivanovic   if (CopyDefs.first) {
251*6f590bf8SNemanja Ivanovic     dbgs() << "CopyDef1: ";
252*6f590bf8SNemanja Ivanovic     CopyDefs.first->dump();
253*6f590bf8SNemanja Ivanovic   }
254*6f590bf8SNemanja Ivanovic   if (CopyDefs.second) {
255*6f590bf8SNemanja Ivanovic     dbgs() << "CopyDef2: ";
256*6f590bf8SNemanja Ivanovic     CopyDefs.second->dump();
257*6f590bf8SNemanja Ivanovic   }
258*6f590bf8SNemanja Ivanovic }
259*6f590bf8SNemanja Ivanovic 
260*6f590bf8SNemanja Ivanovic PPCReduceCRLogicals::CRLogicalOpInfo
261*6f590bf8SNemanja Ivanovic PPCReduceCRLogicals::createCRLogicalOpInfo(MachineInstr &MIParam) {
262*6f590bf8SNemanja Ivanovic   CRLogicalOpInfo Ret;
263*6f590bf8SNemanja Ivanovic   Ret.MI = &MIParam;
264*6f590bf8SNemanja Ivanovic   // Get the defs
265*6f590bf8SNemanja Ivanovic   if (isNullary(MIParam)) {
266*6f590bf8SNemanja Ivanovic     Ret.IsNullary = 1;
267*6f590bf8SNemanja Ivanovic     Ret.TrueDefs = std::make_pair(nullptr, nullptr);
268*6f590bf8SNemanja Ivanovic     Ret.CopyDefs = std::make_pair(nullptr, nullptr);
269*6f590bf8SNemanja Ivanovic   } else {
270*6f590bf8SNemanja Ivanovic     MachineInstr *Def1 = lookThroughCRCopy(MIParam.getOperand(1).getReg(),
271*6f590bf8SNemanja Ivanovic                                            Ret.SubregDef1, Ret.CopyDefs.first);
272*6f590bf8SNemanja Ivanovic     Ret.DefsSingleUse &=
273*6f590bf8SNemanja Ivanovic       MRI->hasOneNonDBGUse(Def1->getOperand(0).getReg());
274*6f590bf8SNemanja Ivanovic     Ret.DefsSingleUse &=
275*6f590bf8SNemanja Ivanovic       MRI->hasOneNonDBGUse(Ret.CopyDefs.first->getOperand(0).getReg());
276*6f590bf8SNemanja Ivanovic     assert(Def1 && "Must be able to find a definition of operand 1.");
277*6f590bf8SNemanja Ivanovic     if (isBinary(MIParam)) {
278*6f590bf8SNemanja Ivanovic       Ret.IsBinary = 1;
279*6f590bf8SNemanja Ivanovic       MachineInstr *Def2 = lookThroughCRCopy(MIParam.getOperand(2).getReg(),
280*6f590bf8SNemanja Ivanovic                                              Ret.SubregDef2,
281*6f590bf8SNemanja Ivanovic                                              Ret.CopyDefs.second);
282*6f590bf8SNemanja Ivanovic       Ret.DefsSingleUse &=
283*6f590bf8SNemanja Ivanovic         MRI->hasOneNonDBGUse(Def2->getOperand(0).getReg());
284*6f590bf8SNemanja Ivanovic       Ret.DefsSingleUse &=
285*6f590bf8SNemanja Ivanovic         MRI->hasOneNonDBGUse(Ret.CopyDefs.second->getOperand(0).getReg());
286*6f590bf8SNemanja Ivanovic       assert(Def2 && "Must be able to find a definition of operand 2.");
287*6f590bf8SNemanja Ivanovic       Ret.TrueDefs = std::make_pair(Def1, Def2);
288*6f590bf8SNemanja Ivanovic     } else {
289*6f590bf8SNemanja Ivanovic       Ret.TrueDefs = std::make_pair(Def1, nullptr);
290*6f590bf8SNemanja Ivanovic       Ret.CopyDefs.second = nullptr;
291*6f590bf8SNemanja Ivanovic     }
292*6f590bf8SNemanja Ivanovic   }
293*6f590bf8SNemanja Ivanovic 
294*6f590bf8SNemanja Ivanovic   Ret.ContainedInBlock = 1;
295*6f590bf8SNemanja Ivanovic   // Get the uses
296*6f590bf8SNemanja Ivanovic   for (MachineInstr &UseMI :
297*6f590bf8SNemanja Ivanovic        MRI->use_nodbg_instructions(MIParam.getOperand(0).getReg())) {
298*6f590bf8SNemanja Ivanovic     unsigned Opc = UseMI.getOpcode();
299*6f590bf8SNemanja Ivanovic     if (Opc == PPC::ISEL || Opc == PPC::ISEL8)
300*6f590bf8SNemanja Ivanovic       Ret.FeedsISEL = 1;
301*6f590bf8SNemanja Ivanovic     if (Opc == PPC::BC || Opc == PPC::BCn || Opc == PPC::BCLR ||
302*6f590bf8SNemanja Ivanovic         Opc == PPC::BCLRn)
303*6f590bf8SNemanja Ivanovic       Ret.FeedsBR = 1;
304*6f590bf8SNemanja Ivanovic     Ret.FeedsLogical = isCRLogical(UseMI);
305*6f590bf8SNemanja Ivanovic     if (UseMI.getParent() != MIParam.getParent())
306*6f590bf8SNemanja Ivanovic       Ret.ContainedInBlock = 0;
307*6f590bf8SNemanja Ivanovic   }
308*6f590bf8SNemanja Ivanovic   Ret.SingleUse = MRI->hasOneNonDBGUse(MIParam.getOperand(0).getReg()) ? 1 : 0;
309*6f590bf8SNemanja Ivanovic 
310*6f590bf8SNemanja Ivanovic   // We now know whether all the uses of the CR logical are in the same block.
311*6f590bf8SNemanja Ivanovic   if (!Ret.IsNullary) {
312*6f590bf8SNemanja Ivanovic     Ret.ContainedInBlock &=
313*6f590bf8SNemanja Ivanovic       (MIParam.getParent() == Ret.TrueDefs.first->getParent());
314*6f590bf8SNemanja Ivanovic     if (Ret.IsBinary)
315*6f590bf8SNemanja Ivanovic       Ret.ContainedInBlock &=
316*6f590bf8SNemanja Ivanovic         (MIParam.getParent() == Ret.TrueDefs.second->getParent());
317*6f590bf8SNemanja Ivanovic   }
318*6f590bf8SNemanja Ivanovic   DEBUG(Ret.dump());
319*6f590bf8SNemanja Ivanovic   if (Ret.IsBinary && Ret.ContainedInBlock && Ret.SingleUse) {
320*6f590bf8SNemanja Ivanovic     NumContainedSingleUseBinOps++;
321*6f590bf8SNemanja Ivanovic     if (Ret.FeedsBR && Ret.DefsSingleUse)
322*6f590bf8SNemanja Ivanovic       NumToSplitBlocks++;
323*6f590bf8SNemanja Ivanovic   }
324*6f590bf8SNemanja Ivanovic   return Ret;
325*6f590bf8SNemanja Ivanovic }
326*6f590bf8SNemanja Ivanovic 
327*6f590bf8SNemanja Ivanovic /// Looks trhough a COPY instruction to the actual definition of the CR-bit
328*6f590bf8SNemanja Ivanovic /// register and returns the instruction that defines it.
329*6f590bf8SNemanja Ivanovic /// FIXME: This currently handles what is by-far the most common case:
330*6f590bf8SNemanja Ivanovic /// an instruction that defines a CR field followed by a single copy of a bit
331*6f590bf8SNemanja Ivanovic /// from that field into a virtual register. If chains of copies need to be
332*6f590bf8SNemanja Ivanovic /// handled, this should have a loop until a non-copy instruction is found.
333*6f590bf8SNemanja Ivanovic MachineInstr *PPCReduceCRLogicals::lookThroughCRCopy(unsigned Reg,
334*6f590bf8SNemanja Ivanovic                                                      unsigned &Subreg,
335*6f590bf8SNemanja Ivanovic                                                      MachineInstr *&CpDef) {
336*6f590bf8SNemanja Ivanovic   Subreg = -1;
337*6f590bf8SNemanja Ivanovic   if (!TargetRegisterInfo::isVirtualRegister(Reg))
338*6f590bf8SNemanja Ivanovic     return nullptr;
339*6f590bf8SNemanja Ivanovic   MachineInstr *Copy = MRI->getVRegDef(Reg);
340*6f590bf8SNemanja Ivanovic   CpDef = Copy;
341*6f590bf8SNemanja Ivanovic   if (!Copy->isCopy())
342*6f590bf8SNemanja Ivanovic     return Copy;
343*6f590bf8SNemanja Ivanovic   unsigned CopySrc = Copy->getOperand(1).getReg();
344*6f590bf8SNemanja Ivanovic   Subreg = Copy->getOperand(1).getSubReg();
345*6f590bf8SNemanja Ivanovic   if (!TargetRegisterInfo::isVirtualRegister(CopySrc)) {
346*6f590bf8SNemanja Ivanovic     const TargetRegisterInfo *TRI = &TII->getRegisterInfo();
347*6f590bf8SNemanja Ivanovic     // Set the Subreg
348*6f590bf8SNemanja Ivanovic     if (CopySrc == PPC::CR0EQ || CopySrc == PPC::CR6EQ)
349*6f590bf8SNemanja Ivanovic       Subreg = PPC::sub_eq;
350*6f590bf8SNemanja Ivanovic     if (CopySrc == PPC::CR0LT || CopySrc == PPC::CR6LT)
351*6f590bf8SNemanja Ivanovic       Subreg = PPC::sub_lt;
352*6f590bf8SNemanja Ivanovic     if (CopySrc == PPC::CR0GT || CopySrc == PPC::CR6GT)
353*6f590bf8SNemanja Ivanovic       Subreg = PPC::sub_gt;
354*6f590bf8SNemanja Ivanovic     if (CopySrc == PPC::CR0UN || CopySrc == PPC::CR6UN)
355*6f590bf8SNemanja Ivanovic       Subreg = PPC::sub_un;
356*6f590bf8SNemanja Ivanovic     // Loop backwards and return the first MI that modifies the physical CR Reg.
357*6f590bf8SNemanja Ivanovic     MachineBasicBlock::iterator Me = Copy, B = Copy->getParent()->begin();
358*6f590bf8SNemanja Ivanovic     while (Me != B)
359*6f590bf8SNemanja Ivanovic       if ((--Me)->modifiesRegister(CopySrc, TRI))
360*6f590bf8SNemanja Ivanovic         return &*Me;
361*6f590bf8SNemanja Ivanovic     return nullptr;
362*6f590bf8SNemanja Ivanovic   }
363*6f590bf8SNemanja Ivanovic   return MRI->getVRegDef(CopySrc);
364*6f590bf8SNemanja Ivanovic }
365*6f590bf8SNemanja Ivanovic 
366*6f590bf8SNemanja Ivanovic void PPCReduceCRLogicals::initialize(MachineFunction &MFParam) {
367*6f590bf8SNemanja Ivanovic   MF = &MFParam;
368*6f590bf8SNemanja Ivanovic   MRI = &MF->getRegInfo();
369*6f590bf8SNemanja Ivanovic   TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo();
370*6f590bf8SNemanja Ivanovic   MBPI = &getAnalysis<MachineBranchProbabilityInfo>();
371*6f590bf8SNemanja Ivanovic 
372*6f590bf8SNemanja Ivanovic   AllCRLogicalOps.clear();
373*6f590bf8SNemanja Ivanovic }
374*6f590bf8SNemanja Ivanovic 
375*6f590bf8SNemanja Ivanovic /// Contains all the implemented transformations on CR logical operations.
376*6f590bf8SNemanja Ivanovic /// For example, a binary CR logical can be used to split a block on its inputs,
377*6f590bf8SNemanja Ivanovic /// a unary CR logical might be used to change the condition code on a
378*6f590bf8SNemanja Ivanovic /// comparison feeding it. A nullary CR logical might simply be removable
379*6f590bf8SNemanja Ivanovic /// if the user of the bit it [un]sets can be transformed.
380*6f590bf8SNemanja Ivanovic bool PPCReduceCRLogicals::handleCROp(CRLogicalOpInfo &CRI) {
381*6f590bf8SNemanja Ivanovic   // We can definitely split a block on the inputs to a binary CR operation
382*6f590bf8SNemanja Ivanovic   // whose defs and (single) use are within the same block.
383*6f590bf8SNemanja Ivanovic   bool Changed = false;
384*6f590bf8SNemanja Ivanovic   if (CRI.IsBinary && CRI.ContainedInBlock && CRI.SingleUse && CRI.FeedsBR &&
385*6f590bf8SNemanja Ivanovic       CRI.DefsSingleUse) {
386*6f590bf8SNemanja Ivanovic     Changed = splitBlockOnBinaryCROp(CRI);
387*6f590bf8SNemanja Ivanovic     if (Changed)
388*6f590bf8SNemanja Ivanovic       NumBlocksSplitOnBinaryCROp++;
389*6f590bf8SNemanja Ivanovic   }
390*6f590bf8SNemanja Ivanovic   return Changed;
391*6f590bf8SNemanja Ivanovic }
392*6f590bf8SNemanja Ivanovic 
393*6f590bf8SNemanja Ivanovic /// Splits a block that contains a CR-logical operation that feeds a branch
394*6f590bf8SNemanja Ivanovic /// and whose operands are produced within the block.
395*6f590bf8SNemanja Ivanovic /// Example:
396*6f590bf8SNemanja Ivanovic ///    %vr5<def> = CMPDI %vr2, 0; CRRC:%vr5 G8RC:%vr2
397*6f590bf8SNemanja Ivanovic ///    %vr6<def> = COPY %vr5:sub_eq; CRBITRC:%vr6 CRRC:%vr5
398*6f590bf8SNemanja Ivanovic ///    %vr7<def> = CMPDI %vr3, 0; CRRC:%vr7 G8RC:%vr3
399*6f590bf8SNemanja Ivanovic ///    %vr8<def> = COPY %vr7:sub_eq; CRBITRC:%vr8 CRRC:%vr7
400*6f590bf8SNemanja Ivanovic ///    %vr9<def> = CROR %vr6<kill>, %vr8<kill>; CRBITRC:%vr9,%vr6,%vr8
401*6f590bf8SNemanja Ivanovic ///    BC %vr9<kill>, <BB#2>; CRBITRC:%vr9
402*6f590bf8SNemanja Ivanovic /// Becomes:
403*6f590bf8SNemanja Ivanovic ///    %vr5<def> = CMPDI %vr2, 0; CRRC:%vr5 G8RC:%vr2
404*6f590bf8SNemanja Ivanovic ///    %vr6<def> = COPY %vr5:sub_eq; CRBITRC:%vr6 CRRC:%vr5
405*6f590bf8SNemanja Ivanovic ///    BC %vr6<kill>, <BB#2>; CRBITRC:%vr6
406*6f590bf8SNemanja Ivanovic ///
407*6f590bf8SNemanja Ivanovic ///    %vr7<def> = CMPDI %vr3, 0; CRRC:%vr7 G8RC:%vr3
408*6f590bf8SNemanja Ivanovic ///    %vr8<def> = COPY %vr7:sub_eq; CRBITRC:%vr8 CRRC:%vr7
409*6f590bf8SNemanja Ivanovic ///    BC %vr9<kill>, <BB#2>; CRBITRC:%vr9
410*6f590bf8SNemanja Ivanovic bool PPCReduceCRLogicals::splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI) {
411*6f590bf8SNemanja Ivanovic   if (CRI.CopyDefs.first == CRI.CopyDefs.second) {
412*6f590bf8SNemanja Ivanovic     DEBUG(dbgs() << "Unable to split as the two operands are the same\n");
413*6f590bf8SNemanja Ivanovic     NumNotSplitIdenticalOperands++;
414*6f590bf8SNemanja Ivanovic     return false;
415*6f590bf8SNemanja Ivanovic   }
416*6f590bf8SNemanja Ivanovic   if (CRI.TrueDefs.first->isCopy() || CRI.TrueDefs.second->isCopy() ||
417*6f590bf8SNemanja Ivanovic       CRI.TrueDefs.first->isPHI() || CRI.TrueDefs.second->isPHI()) {
418*6f590bf8SNemanja Ivanovic     DEBUG(dbgs() << "Unable to split because one of the operands is a PHI or "
419*6f590bf8SNemanja Ivanovic           "chain of copies.\n");
420*6f590bf8SNemanja Ivanovic     NumNotSplitChainCopies++;
421*6f590bf8SNemanja Ivanovic     return false;
422*6f590bf8SNemanja Ivanovic   }
423*6f590bf8SNemanja Ivanovic   // Note: keep in sync with computeBranchTargetAndInversion().
424*6f590bf8SNemanja Ivanovic   if (CRI.MI->getOpcode() != PPC::CROR &&
425*6f590bf8SNemanja Ivanovic       CRI.MI->getOpcode() != PPC::CRAND &&
426*6f590bf8SNemanja Ivanovic       CRI.MI->getOpcode() != PPC::CRNOR &&
427*6f590bf8SNemanja Ivanovic       CRI.MI->getOpcode() != PPC::CRNAND &&
428*6f590bf8SNemanja Ivanovic       CRI.MI->getOpcode() != PPC::CRORC &&
429*6f590bf8SNemanja Ivanovic       CRI.MI->getOpcode() != PPC::CRANDC) {
430*6f590bf8SNemanja Ivanovic     DEBUG(dbgs() << "Unable to split blocks on this opcode.\n");
431*6f590bf8SNemanja Ivanovic     NumNotSplitWrongOpcode++;
432*6f590bf8SNemanja Ivanovic     return false;
433*6f590bf8SNemanja Ivanovic   }
434*6f590bf8SNemanja Ivanovic   DEBUG(dbgs() << "Splitting the following CR op:\n"; CRI.dump());
435*6f590bf8SNemanja Ivanovic   MachineBasicBlock::iterator Def1It = CRI.TrueDefs.first;
436*6f590bf8SNemanja Ivanovic   MachineBasicBlock::iterator Def2It = CRI.TrueDefs.second;
437*6f590bf8SNemanja Ivanovic 
438*6f590bf8SNemanja Ivanovic   bool UsingDef1 = false;
439*6f590bf8SNemanja Ivanovic   MachineInstr *SplitBefore = &*Def2It;
440*6f590bf8SNemanja Ivanovic   for (auto E = CRI.MI->getParent()->end(); Def2It != E; ++Def2It) {
441*6f590bf8SNemanja Ivanovic     if (Def1It == Def2It) { // Def2 comes before Def1.
442*6f590bf8SNemanja Ivanovic       SplitBefore = &*Def1It;
443*6f590bf8SNemanja Ivanovic       UsingDef1 = true;
444*6f590bf8SNemanja Ivanovic       break;
445*6f590bf8SNemanja Ivanovic     }
446*6f590bf8SNemanja Ivanovic   }
447*6f590bf8SNemanja Ivanovic 
448*6f590bf8SNemanja Ivanovic   DEBUG(dbgs() << "We will split the following block:\n";);
449*6f590bf8SNemanja Ivanovic   DEBUG(CRI.MI->getParent()->dump());
450*6f590bf8SNemanja Ivanovic   DEBUG(dbgs() << "Before instruction:\n"; SplitBefore->dump());
451*6f590bf8SNemanja Ivanovic 
452*6f590bf8SNemanja Ivanovic   // Get the branch instruction.
453*6f590bf8SNemanja Ivanovic   MachineInstr *Branch =
454*6f590bf8SNemanja Ivanovic     MRI->use_nodbg_begin(CRI.MI->getOperand(0).getReg())->getParent();
455*6f590bf8SNemanja Ivanovic 
456*6f590bf8SNemanja Ivanovic   // We want the new block to have no code in it other than the definition
457*6f590bf8SNemanja Ivanovic   // of the input to the CR logical and the CR logical itself. So we move
458*6f590bf8SNemanja Ivanovic   // those to the bottom of the block (just before the branch). Then we
459*6f590bf8SNemanja Ivanovic   // will split before the CR logical.
460*6f590bf8SNemanja Ivanovic   MachineBasicBlock *MBB = SplitBefore->getParent();
461*6f590bf8SNemanja Ivanovic   auto FirstTerminator = MBB->getFirstTerminator();
462*6f590bf8SNemanja Ivanovic   MachineBasicBlock::iterator FirstInstrToMove =
463*6f590bf8SNemanja Ivanovic     UsingDef1 ? CRI.TrueDefs.first : CRI.TrueDefs.second;
464*6f590bf8SNemanja Ivanovic   MachineBasicBlock::iterator SecondInstrToMove =
465*6f590bf8SNemanja Ivanovic     UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second;
466*6f590bf8SNemanja Ivanovic 
467*6f590bf8SNemanja Ivanovic   // The instructions that need to be moved are not guaranteed to be
468*6f590bf8SNemanja Ivanovic   // contiguous. Move them individually.
469*6f590bf8SNemanja Ivanovic   // FIXME: If one of the operands is a chain of (single use) copies, they
470*6f590bf8SNemanja Ivanovic   // can all be moved and we can still split.
471*6f590bf8SNemanja Ivanovic   MBB->splice(FirstTerminator, MBB, FirstInstrToMove);
472*6f590bf8SNemanja Ivanovic   if (FirstInstrToMove != SecondInstrToMove)
473*6f590bf8SNemanja Ivanovic     MBB->splice(FirstTerminator, MBB, SecondInstrToMove);
474*6f590bf8SNemanja Ivanovic   MBB->splice(FirstTerminator, MBB, CRI.MI);
475*6f590bf8SNemanja Ivanovic 
476*6f590bf8SNemanja Ivanovic   unsigned Opc = CRI.MI->getOpcode();
477*6f590bf8SNemanja Ivanovic   bool InvertOrigBranch, InvertNewBranch, TargetIsFallThrough;
478*6f590bf8SNemanja Ivanovic   computeBranchTargetAndInversion(Opc, Branch->getOpcode(), UsingDef1,
479*6f590bf8SNemanja Ivanovic                                   InvertNewBranch, InvertOrigBranch,
480*6f590bf8SNemanja Ivanovic                                   TargetIsFallThrough);
481*6f590bf8SNemanja Ivanovic   MachineInstr *SplitCond =
482*6f590bf8SNemanja Ivanovic     UsingDef1 ? CRI.CopyDefs.second : CRI.CopyDefs.first;
483*6f590bf8SNemanja Ivanovic   DEBUG(dbgs() << "We will " <<  (InvertNewBranch ? "invert" : "copy"));
484*6f590bf8SNemanja Ivanovic   DEBUG(dbgs() << " the original branch and the target is the " <<
485*6f590bf8SNemanja Ivanovic         (TargetIsFallThrough ? "fallthrough block\n" : "orig. target block\n"));
486*6f590bf8SNemanja Ivanovic   DEBUG(dbgs() << "Original branch instruction: "; Branch->dump());
487*6f590bf8SNemanja Ivanovic   BlockSplitInfo BSI { Branch, SplitBefore, SplitCond, InvertNewBranch,
488*6f590bf8SNemanja Ivanovic     InvertOrigBranch, TargetIsFallThrough, MBPI, CRI.MI,
489*6f590bf8SNemanja Ivanovic     UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second };
490*6f590bf8SNemanja Ivanovic   bool Changed = splitMBB(BSI);
491*6f590bf8SNemanja Ivanovic   // If we've split on a CR logical that is fed by a CR logical,
492*6f590bf8SNemanja Ivanovic   // recompute the source CR logical as it may be usable for splitting.
493*6f590bf8SNemanja Ivanovic   if (Changed) {
494*6f590bf8SNemanja Ivanovic     bool Input1CRlogical =
495*6f590bf8SNemanja Ivanovic       CRI.TrueDefs.first && isCRLogical(*CRI.TrueDefs.first);
496*6f590bf8SNemanja Ivanovic     bool Input2CRlogical =
497*6f590bf8SNemanja Ivanovic       CRI.TrueDefs.second && isCRLogical(*CRI.TrueDefs.second);
498*6f590bf8SNemanja Ivanovic     if (Input1CRlogical)
499*6f590bf8SNemanja Ivanovic       AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.first));
500*6f590bf8SNemanja Ivanovic     if (Input2CRlogical)
501*6f590bf8SNemanja Ivanovic       AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.second));
502*6f590bf8SNemanja Ivanovic   }
503*6f590bf8SNemanja Ivanovic   return Changed;
504*6f590bf8SNemanja Ivanovic }
505*6f590bf8SNemanja Ivanovic 
506*6f590bf8SNemanja Ivanovic void PPCReduceCRLogicals::collectCRLogicals() {
507*6f590bf8SNemanja Ivanovic   for (MachineBasicBlock &MBB : *MF) {
508*6f590bf8SNemanja Ivanovic     for (MachineInstr &MI : MBB) {
509*6f590bf8SNemanja Ivanovic       if (isCRLogical(MI)) {
510*6f590bf8SNemanja Ivanovic         AllCRLogicalOps.push_back(createCRLogicalOpInfo(MI));
511*6f590bf8SNemanja Ivanovic         TotalCRLogicals++;
512*6f590bf8SNemanja Ivanovic         if (AllCRLogicalOps.back().IsNullary)
513*6f590bf8SNemanja Ivanovic           TotalNullaryCRLogicals++;
514*6f590bf8SNemanja Ivanovic         else if (AllCRLogicalOps.back().IsBinary)
515*6f590bf8SNemanja Ivanovic           TotalBinaryCRLogicals++;
516*6f590bf8SNemanja Ivanovic         else
517*6f590bf8SNemanja Ivanovic           TotalUnaryCRLogicals++;
518*6f590bf8SNemanja Ivanovic       }
519*6f590bf8SNemanja Ivanovic     }
520*6f590bf8SNemanja Ivanovic   }
521*6f590bf8SNemanja Ivanovic }
522*6f590bf8SNemanja Ivanovic 
523*6f590bf8SNemanja Ivanovic } // end annonymous namespace
524*6f590bf8SNemanja Ivanovic 
525*6f590bf8SNemanja Ivanovic INITIALIZE_PASS_BEGIN(PPCReduceCRLogicals, DEBUG_TYPE,
526*6f590bf8SNemanja Ivanovic                       "PowerPC Reduce CR logical Operation", false, false)
527*6f590bf8SNemanja Ivanovic INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree)
528*6f590bf8SNemanja Ivanovic INITIALIZE_PASS_END(PPCReduceCRLogicals, DEBUG_TYPE,
529*6f590bf8SNemanja Ivanovic                     "PowerPC Reduce CR logical Operation", false, false)
530*6f590bf8SNemanja Ivanovic 
531*6f590bf8SNemanja Ivanovic char PPCReduceCRLogicals::ID = 0;
532*6f590bf8SNemanja Ivanovic FunctionPass*
533*6f590bf8SNemanja Ivanovic llvm::createPPCReduceCRLogicalsPass() { return new PPCReduceCRLogicals(); }
534