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 "PPCInstrInfo.h" 196f590bf8SNemanja Ivanovic #include "PPC.h" 206f590bf8SNemanja Ivanovic #include "PPCTargetMachine.h" 216f590bf8SNemanja Ivanovic #include "llvm/CodeGen/MachineFunctionPass.h" 226f590bf8SNemanja Ivanovic #include "llvm/CodeGen/MachineDominators.h" 236f590bf8SNemanja Ivanovic #include "llvm/Support/Debug.h" 246f590bf8SNemanja Ivanovic #include "llvm/ADT/Statistic.h" 256f590bf8SNemanja Ivanovic 266f590bf8SNemanja Ivanovic using namespace llvm; 276f590bf8SNemanja Ivanovic 286f590bf8SNemanja Ivanovic #define DEBUG_TYPE "ppc-reduce-cr-ops" 296f590bf8SNemanja Ivanovic #include "PPCMachineBasicBlockUtils.h" 306f590bf8SNemanja Ivanovic 316f590bf8SNemanja Ivanovic STATISTIC(NumContainedSingleUseBinOps, 326f590bf8SNemanja Ivanovic "Number of single-use binary CR logical ops contained in a block"); 336f590bf8SNemanja Ivanovic STATISTIC(NumToSplitBlocks, 346f590bf8SNemanja Ivanovic "Number of binary CR logical ops that can be used to split blocks"); 356f590bf8SNemanja Ivanovic STATISTIC(TotalCRLogicals, "Number of CR logical ops."); 366f590bf8SNemanja Ivanovic STATISTIC(TotalNullaryCRLogicals, 376f590bf8SNemanja Ivanovic "Number of nullary CR logical ops (CRSET/CRUNSET)."); 386f590bf8SNemanja Ivanovic STATISTIC(TotalUnaryCRLogicals, "Number of unary CR logical ops."); 396f590bf8SNemanja Ivanovic STATISTIC(TotalBinaryCRLogicals, "Number of CR logical ops."); 406f590bf8SNemanja Ivanovic STATISTIC(NumBlocksSplitOnBinaryCROp, 416f590bf8SNemanja Ivanovic "Number of blocks split on CR binary logical ops."); 426f590bf8SNemanja Ivanovic STATISTIC(NumNotSplitIdenticalOperands, 436f590bf8SNemanja Ivanovic "Number of blocks not split due to operands being identical."); 446f590bf8SNemanja Ivanovic STATISTIC(NumNotSplitChainCopies, 456f590bf8SNemanja Ivanovic "Number of blocks not split due to operands being chained copies."); 466f590bf8SNemanja Ivanovic STATISTIC(NumNotSplitWrongOpcode, 476f590bf8SNemanja Ivanovic "Number of blocks not split due to the wrong opcode."); 486f590bf8SNemanja Ivanovic 496f590bf8SNemanja Ivanovic namespace llvm { 506f590bf8SNemanja Ivanovic void initializePPCReduceCRLogicalsPass(PassRegistry&); 516f590bf8SNemanja Ivanovic } 526f590bf8SNemanja Ivanovic 536f590bf8SNemanja Ivanovic namespace { 546f590bf8SNemanja Ivanovic 556f590bf8SNemanja Ivanovic static bool isBinary(MachineInstr &MI) { 566f590bf8SNemanja Ivanovic return MI.getNumOperands() == 3; 576f590bf8SNemanja Ivanovic } 586f590bf8SNemanja Ivanovic 596f590bf8SNemanja Ivanovic static bool isNullary(MachineInstr &MI) { 606f590bf8SNemanja Ivanovic return MI.getNumOperands() == 1; 616f590bf8SNemanja Ivanovic } 626f590bf8SNemanja Ivanovic 636f590bf8SNemanja Ivanovic /// Given a CR logical operation \p CROp, branch opcode \p BROp as well as 646f590bf8SNemanja Ivanovic /// a flag to indicate if the first operand of \p CROp is used as the 656f590bf8SNemanja Ivanovic /// SplitBefore operand, determines whether either of the branches are to be 666f590bf8SNemanja Ivanovic /// inverted as well as whether the new target should be the original 676f590bf8SNemanja Ivanovic /// fall-through block. 686f590bf8SNemanja Ivanovic static void 696f590bf8SNemanja Ivanovic computeBranchTargetAndInversion(unsigned CROp, unsigned BROp, bool UsingDef1, 706f590bf8SNemanja Ivanovic bool &InvertNewBranch, bool &InvertOrigBranch, 716f590bf8SNemanja Ivanovic bool &TargetIsFallThrough) { 726f590bf8SNemanja Ivanovic // The conditions under which each of the output operands should be [un]set 736f590bf8SNemanja Ivanovic // can certainly be written much more concisely with just 3 if statements or 746f590bf8SNemanja Ivanovic // ternary expressions. However, this provides a much clearer overview to the 756f590bf8SNemanja Ivanovic // reader as to what is set for each <CROp, BROp, OpUsed> combination. 766f590bf8SNemanja Ivanovic if (BROp == PPC::BC || BROp == PPC::BCLR) { 776f590bf8SNemanja Ivanovic // Regular branches. 786f590bf8SNemanja Ivanovic switch (CROp) { 796f590bf8SNemanja Ivanovic default: 806f590bf8SNemanja Ivanovic llvm_unreachable("Don't know how to handle this CR logical."); 816f590bf8SNemanja Ivanovic case PPC::CROR: 826f590bf8SNemanja Ivanovic InvertNewBranch = false; 836f590bf8SNemanja Ivanovic InvertOrigBranch = false; 846f590bf8SNemanja Ivanovic TargetIsFallThrough = false; 856f590bf8SNemanja Ivanovic return; 866f590bf8SNemanja Ivanovic case PPC::CRAND: 876f590bf8SNemanja Ivanovic InvertNewBranch = true; 886f590bf8SNemanja Ivanovic InvertOrigBranch = false; 896f590bf8SNemanja Ivanovic TargetIsFallThrough = true; 906f590bf8SNemanja Ivanovic return; 916f590bf8SNemanja Ivanovic case PPC::CRNAND: 926f590bf8SNemanja Ivanovic InvertNewBranch = true; 936f590bf8SNemanja Ivanovic InvertOrigBranch = true; 946f590bf8SNemanja Ivanovic TargetIsFallThrough = false; 956f590bf8SNemanja Ivanovic return; 966f590bf8SNemanja Ivanovic case PPC::CRNOR: 976f590bf8SNemanja Ivanovic InvertNewBranch = false; 986f590bf8SNemanja Ivanovic InvertOrigBranch = true; 996f590bf8SNemanja Ivanovic TargetIsFallThrough = true; 1006f590bf8SNemanja Ivanovic return; 1016f590bf8SNemanja Ivanovic case PPC::CRORC: 1026f590bf8SNemanja Ivanovic InvertNewBranch = UsingDef1; 1036f590bf8SNemanja Ivanovic InvertOrigBranch = !UsingDef1; 1046f590bf8SNemanja Ivanovic TargetIsFallThrough = false; 1056f590bf8SNemanja Ivanovic return; 1066f590bf8SNemanja Ivanovic case PPC::CRANDC: 1076f590bf8SNemanja Ivanovic InvertNewBranch = !UsingDef1; 1086f590bf8SNemanja Ivanovic InvertOrigBranch = !UsingDef1; 1096f590bf8SNemanja Ivanovic TargetIsFallThrough = true; 1106f590bf8SNemanja Ivanovic return; 1116f590bf8SNemanja Ivanovic } 1126f590bf8SNemanja Ivanovic } else if (BROp == PPC::BCn || BROp == PPC::BCLRn) { 1136f590bf8SNemanja Ivanovic // Negated branches. 1146f590bf8SNemanja Ivanovic switch (CROp) { 1156f590bf8SNemanja Ivanovic default: 1166f590bf8SNemanja Ivanovic llvm_unreachable("Don't know how to handle this CR logical."); 1176f590bf8SNemanja Ivanovic case PPC::CROR: 1186f590bf8SNemanja Ivanovic InvertNewBranch = true; 1196f590bf8SNemanja Ivanovic InvertOrigBranch = false; 1206f590bf8SNemanja Ivanovic TargetIsFallThrough = true; 1216f590bf8SNemanja Ivanovic return; 1226f590bf8SNemanja Ivanovic case PPC::CRAND: 1236f590bf8SNemanja Ivanovic InvertNewBranch = false; 1246f590bf8SNemanja Ivanovic InvertOrigBranch = false; 1256f590bf8SNemanja Ivanovic TargetIsFallThrough = false; 1266f590bf8SNemanja Ivanovic return; 1276f590bf8SNemanja Ivanovic case PPC::CRNAND: 1286f590bf8SNemanja Ivanovic InvertNewBranch = false; 1296f590bf8SNemanja Ivanovic InvertOrigBranch = true; 1306f590bf8SNemanja Ivanovic TargetIsFallThrough = true; 1316f590bf8SNemanja Ivanovic return; 1326f590bf8SNemanja Ivanovic case PPC::CRNOR: 1336f590bf8SNemanja Ivanovic InvertNewBranch = true; 1346f590bf8SNemanja Ivanovic InvertOrigBranch = true; 1356f590bf8SNemanja Ivanovic TargetIsFallThrough = false; 1366f590bf8SNemanja Ivanovic return; 1376f590bf8SNemanja Ivanovic case PPC::CRORC: 1386f590bf8SNemanja Ivanovic InvertNewBranch = !UsingDef1; 1396f590bf8SNemanja Ivanovic InvertOrigBranch = !UsingDef1; 1406f590bf8SNemanja Ivanovic TargetIsFallThrough = true; 1416f590bf8SNemanja Ivanovic return; 1426f590bf8SNemanja Ivanovic case PPC::CRANDC: 1436f590bf8SNemanja Ivanovic InvertNewBranch = UsingDef1; 1446f590bf8SNemanja Ivanovic InvertOrigBranch = !UsingDef1; 1456f590bf8SNemanja Ivanovic TargetIsFallThrough = false; 1466f590bf8SNemanja Ivanovic return; 1476f590bf8SNemanja Ivanovic } 1486f590bf8SNemanja Ivanovic } else 1496f590bf8SNemanja Ivanovic llvm_unreachable("Don't know how to handle this branch."); 1506f590bf8SNemanja Ivanovic } 1516f590bf8SNemanja Ivanovic 1526f590bf8SNemanja Ivanovic class PPCReduceCRLogicals : public MachineFunctionPass { 1536f590bf8SNemanja Ivanovic 1546f590bf8SNemanja Ivanovic public: 1556f590bf8SNemanja Ivanovic static char ID; 1566f590bf8SNemanja Ivanovic struct CRLogicalOpInfo { 1576f590bf8SNemanja Ivanovic MachineInstr *MI; 1586f590bf8SNemanja Ivanovic // FIXME: If chains of copies are to be handled, this should be a vector. 1596f590bf8SNemanja Ivanovic std::pair<MachineInstr*, MachineInstr*> CopyDefs; 1606f590bf8SNemanja Ivanovic std::pair<MachineInstr*, MachineInstr*> TrueDefs; 1616f590bf8SNemanja Ivanovic unsigned IsBinary : 1; 1626f590bf8SNemanja Ivanovic unsigned IsNullary : 1; 1636f590bf8SNemanja Ivanovic unsigned ContainedInBlock : 1; 1646f590bf8SNemanja Ivanovic unsigned FeedsISEL : 1; 1656f590bf8SNemanja Ivanovic unsigned FeedsBR : 1; 1666f590bf8SNemanja Ivanovic unsigned FeedsLogical : 1; 1676f590bf8SNemanja Ivanovic unsigned SingleUse : 1; 1686f590bf8SNemanja Ivanovic unsigned DefsSingleUse : 1; 1696f590bf8SNemanja Ivanovic unsigned SubregDef1; 1706f590bf8SNemanja Ivanovic unsigned SubregDef2; 1716f590bf8SNemanja Ivanovic CRLogicalOpInfo() : MI(nullptr), IsBinary(0), IsNullary(0), 1726f590bf8SNemanja Ivanovic ContainedInBlock(0), FeedsISEL(0), FeedsBR(0), 1736f590bf8SNemanja Ivanovic FeedsLogical(0), SingleUse(0), DefsSingleUse(1), 1746f590bf8SNemanja Ivanovic SubregDef1(0), SubregDef2(0) { } 1756f590bf8SNemanja Ivanovic void dump(); 1766f590bf8SNemanja Ivanovic }; 1776f590bf8SNemanja Ivanovic 1786f590bf8SNemanja Ivanovic private: 1796f590bf8SNemanja Ivanovic const PPCInstrInfo *TII; 1806f590bf8SNemanja Ivanovic MachineFunction *MF; 1816f590bf8SNemanja Ivanovic MachineRegisterInfo *MRI; 1826f590bf8SNemanja Ivanovic const MachineBranchProbabilityInfo *MBPI; 1836f590bf8SNemanja Ivanovic 1846f590bf8SNemanja Ivanovic // A vector to contain all the CR logical operations 1856f590bf8SNemanja Ivanovic std::vector<CRLogicalOpInfo> AllCRLogicalOps; 1866f590bf8SNemanja Ivanovic void initialize(MachineFunction &MFParm); 1876f590bf8SNemanja Ivanovic void collectCRLogicals(); 1886f590bf8SNemanja Ivanovic bool handleCROp(CRLogicalOpInfo &CRI); 1896f590bf8SNemanja Ivanovic bool splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI); 1906f590bf8SNemanja Ivanovic static bool isCRLogical(MachineInstr &MI) { 1916f590bf8SNemanja Ivanovic unsigned Opc = MI.getOpcode(); 1926f590bf8SNemanja Ivanovic return Opc == PPC::CRAND || Opc == PPC::CRNAND || Opc == PPC::CROR || 1936f590bf8SNemanja Ivanovic Opc == PPC::CRXOR || Opc == PPC::CRNOR || Opc == PPC::CREQV || 1946f590bf8SNemanja Ivanovic Opc == PPC::CRANDC || Opc == PPC::CRORC || Opc == PPC::CRSET || 1956f590bf8SNemanja Ivanovic Opc == PPC::CRUNSET || Opc == PPC::CR6SET || Opc == PPC::CR6UNSET; 1966f590bf8SNemanja Ivanovic } 1976f590bf8SNemanja Ivanovic bool simplifyCode() { 1986f590bf8SNemanja Ivanovic bool Changed = false; 1996f590bf8SNemanja Ivanovic // Not using a range-based for loop here as the vector may grow while being 2006f590bf8SNemanja Ivanovic // operated on. 2016f590bf8SNemanja Ivanovic for (unsigned i = 0; i < AllCRLogicalOps.size(); i++) 2026f590bf8SNemanja Ivanovic Changed |= handleCROp(AllCRLogicalOps[i]); 2036f590bf8SNemanja Ivanovic return Changed; 2046f590bf8SNemanja Ivanovic } 2056f590bf8SNemanja Ivanovic 2066f590bf8SNemanja Ivanovic public: 2076f590bf8SNemanja Ivanovic PPCReduceCRLogicals() : MachineFunctionPass(ID) { 2086f590bf8SNemanja Ivanovic initializePPCReduceCRLogicalsPass(*PassRegistry::getPassRegistry()); 2096f590bf8SNemanja Ivanovic } 2106f590bf8SNemanja Ivanovic 2116f590bf8SNemanja Ivanovic MachineInstr *lookThroughCRCopy(unsigned Reg, unsigned &Subreg, 2126f590bf8SNemanja Ivanovic MachineInstr *&CpDef); 2136f590bf8SNemanja Ivanovic bool runOnMachineFunction(MachineFunction &MF) override { 2146f590bf8SNemanja Ivanovic if (skipFunction(*MF.getFunction())) 2156f590bf8SNemanja Ivanovic return false; 2166f590bf8SNemanja Ivanovic 2176f590bf8SNemanja Ivanovic // If the subtarget doesn't use CR bits, there's nothing to do. 2186f590bf8SNemanja Ivanovic const PPCSubtarget &STI = MF.getSubtarget<PPCSubtarget>(); 2196f590bf8SNemanja Ivanovic if (!STI.useCRBits()) 2206f590bf8SNemanja Ivanovic return false; 2216f590bf8SNemanja Ivanovic 2226f590bf8SNemanja Ivanovic initialize(MF); 2236f590bf8SNemanja Ivanovic collectCRLogicals(); 2246f590bf8SNemanja Ivanovic return simplifyCode(); 2256f590bf8SNemanja Ivanovic } 2266f590bf8SNemanja Ivanovic CRLogicalOpInfo createCRLogicalOpInfo(MachineInstr &MI); 2276f590bf8SNemanja Ivanovic void getAnalysisUsage(AnalysisUsage &AU) const override { 2286f590bf8SNemanja Ivanovic AU.addRequired<MachineBranchProbabilityInfo>(); 2296f590bf8SNemanja Ivanovic AU.addRequired<MachineDominatorTree>(); 2306f590bf8SNemanja Ivanovic MachineFunctionPass::getAnalysisUsage(AU); 2316f590bf8SNemanja Ivanovic } 2326f590bf8SNemanja Ivanovic }; 2336f590bf8SNemanja Ivanovic 234*6af75240SNemanja Ivanovic #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 235*6af75240SNemanja Ivanovic LLVM_DUMP_METHOD void PPCReduceCRLogicals::CRLogicalOpInfo::dump() { 2366f590bf8SNemanja Ivanovic dbgs() << "CRLogicalOpMI: "; 2376f590bf8SNemanja Ivanovic MI->dump(); 2386f590bf8SNemanja Ivanovic dbgs() << "IsBinary: " << IsBinary << ", FeedsISEL: " << FeedsISEL; 2396f590bf8SNemanja Ivanovic dbgs() << ", FeedsBR: " << FeedsBR << ", FeedsLogical: "; 2406f590bf8SNemanja Ivanovic dbgs() << FeedsLogical << ", SingleUse: " << SingleUse; 2416f590bf8SNemanja Ivanovic dbgs() << ", DefsSingleUse: " << DefsSingleUse; 2426f590bf8SNemanja Ivanovic dbgs() << ", SubregDef1: " << SubregDef1 << ", SubregDef2: "; 2436f590bf8SNemanja Ivanovic dbgs() << SubregDef2 << ", ContainedInBlock: " << ContainedInBlock; 2446f590bf8SNemanja Ivanovic if (!IsNullary) { 2456f590bf8SNemanja Ivanovic dbgs() << "\nDefs:\n"; 2466f590bf8SNemanja Ivanovic TrueDefs.first->dump(); 2476f590bf8SNemanja Ivanovic } 2486f590bf8SNemanja Ivanovic if (IsBinary) 2496f590bf8SNemanja Ivanovic TrueDefs.second->dump(); 2506f590bf8SNemanja Ivanovic dbgs() << "\n"; 2516f590bf8SNemanja Ivanovic if (CopyDefs.first) { 2526f590bf8SNemanja Ivanovic dbgs() << "CopyDef1: "; 2536f590bf8SNemanja Ivanovic CopyDefs.first->dump(); 2546f590bf8SNemanja Ivanovic } 2556f590bf8SNemanja Ivanovic if (CopyDefs.second) { 2566f590bf8SNemanja Ivanovic dbgs() << "CopyDef2: "; 2576f590bf8SNemanja Ivanovic CopyDefs.second->dump(); 2586f590bf8SNemanja Ivanovic } 2596f590bf8SNemanja Ivanovic } 260*6af75240SNemanja Ivanovic #endif 2616f590bf8SNemanja Ivanovic 2626f590bf8SNemanja Ivanovic PPCReduceCRLogicals::CRLogicalOpInfo 2636f590bf8SNemanja Ivanovic PPCReduceCRLogicals::createCRLogicalOpInfo(MachineInstr &MIParam) { 2646f590bf8SNemanja Ivanovic CRLogicalOpInfo Ret; 2656f590bf8SNemanja Ivanovic Ret.MI = &MIParam; 2666f590bf8SNemanja Ivanovic // Get the defs 2676f590bf8SNemanja Ivanovic if (isNullary(MIParam)) { 2686f590bf8SNemanja Ivanovic Ret.IsNullary = 1; 2696f590bf8SNemanja Ivanovic Ret.TrueDefs = std::make_pair(nullptr, nullptr); 2706f590bf8SNemanja Ivanovic Ret.CopyDefs = std::make_pair(nullptr, nullptr); 2716f590bf8SNemanja Ivanovic } else { 2726f590bf8SNemanja Ivanovic MachineInstr *Def1 = lookThroughCRCopy(MIParam.getOperand(1).getReg(), 2736f590bf8SNemanja Ivanovic Ret.SubregDef1, Ret.CopyDefs.first); 2746f590bf8SNemanja Ivanovic Ret.DefsSingleUse &= 2756f590bf8SNemanja Ivanovic MRI->hasOneNonDBGUse(Def1->getOperand(0).getReg()); 2766f590bf8SNemanja Ivanovic Ret.DefsSingleUse &= 2776f590bf8SNemanja Ivanovic MRI->hasOneNonDBGUse(Ret.CopyDefs.first->getOperand(0).getReg()); 2786f590bf8SNemanja Ivanovic assert(Def1 && "Must be able to find a definition of operand 1."); 2796f590bf8SNemanja Ivanovic if (isBinary(MIParam)) { 2806f590bf8SNemanja Ivanovic Ret.IsBinary = 1; 2816f590bf8SNemanja Ivanovic MachineInstr *Def2 = lookThroughCRCopy(MIParam.getOperand(2).getReg(), 2826f590bf8SNemanja Ivanovic Ret.SubregDef2, 2836f590bf8SNemanja Ivanovic Ret.CopyDefs.second); 2846f590bf8SNemanja Ivanovic Ret.DefsSingleUse &= 2856f590bf8SNemanja Ivanovic MRI->hasOneNonDBGUse(Def2->getOperand(0).getReg()); 2866f590bf8SNemanja Ivanovic Ret.DefsSingleUse &= 2876f590bf8SNemanja Ivanovic MRI->hasOneNonDBGUse(Ret.CopyDefs.second->getOperand(0).getReg()); 2886f590bf8SNemanja Ivanovic assert(Def2 && "Must be able to find a definition of operand 2."); 2896f590bf8SNemanja Ivanovic Ret.TrueDefs = std::make_pair(Def1, Def2); 2906f590bf8SNemanja Ivanovic } else { 2916f590bf8SNemanja Ivanovic Ret.TrueDefs = std::make_pair(Def1, nullptr); 2926f590bf8SNemanja Ivanovic Ret.CopyDefs.second = nullptr; 2936f590bf8SNemanja Ivanovic } 2946f590bf8SNemanja Ivanovic } 2956f590bf8SNemanja Ivanovic 2966f590bf8SNemanja Ivanovic Ret.ContainedInBlock = 1; 2976f590bf8SNemanja Ivanovic // Get the uses 2986f590bf8SNemanja Ivanovic for (MachineInstr &UseMI : 2996f590bf8SNemanja Ivanovic MRI->use_nodbg_instructions(MIParam.getOperand(0).getReg())) { 3006f590bf8SNemanja Ivanovic unsigned Opc = UseMI.getOpcode(); 3016f590bf8SNemanja Ivanovic if (Opc == PPC::ISEL || Opc == PPC::ISEL8) 3026f590bf8SNemanja Ivanovic Ret.FeedsISEL = 1; 3036f590bf8SNemanja Ivanovic if (Opc == PPC::BC || Opc == PPC::BCn || Opc == PPC::BCLR || 3046f590bf8SNemanja Ivanovic Opc == PPC::BCLRn) 3056f590bf8SNemanja Ivanovic Ret.FeedsBR = 1; 3066f590bf8SNemanja Ivanovic Ret.FeedsLogical = isCRLogical(UseMI); 3076f590bf8SNemanja Ivanovic if (UseMI.getParent() != MIParam.getParent()) 3086f590bf8SNemanja Ivanovic Ret.ContainedInBlock = 0; 3096f590bf8SNemanja Ivanovic } 3106f590bf8SNemanja Ivanovic Ret.SingleUse = MRI->hasOneNonDBGUse(MIParam.getOperand(0).getReg()) ? 1 : 0; 3116f590bf8SNemanja Ivanovic 3126f590bf8SNemanja Ivanovic // We now know whether all the uses of the CR logical are in the same block. 3136f590bf8SNemanja Ivanovic if (!Ret.IsNullary) { 3146f590bf8SNemanja Ivanovic Ret.ContainedInBlock &= 3156f590bf8SNemanja Ivanovic (MIParam.getParent() == Ret.TrueDefs.first->getParent()); 3166f590bf8SNemanja Ivanovic if (Ret.IsBinary) 3176f590bf8SNemanja Ivanovic Ret.ContainedInBlock &= 3186f590bf8SNemanja Ivanovic (MIParam.getParent() == Ret.TrueDefs.second->getParent()); 3196f590bf8SNemanja Ivanovic } 3206f590bf8SNemanja Ivanovic DEBUG(Ret.dump()); 3216f590bf8SNemanja Ivanovic if (Ret.IsBinary && Ret.ContainedInBlock && Ret.SingleUse) { 3226f590bf8SNemanja Ivanovic NumContainedSingleUseBinOps++; 3236f590bf8SNemanja Ivanovic if (Ret.FeedsBR && Ret.DefsSingleUse) 3246f590bf8SNemanja Ivanovic NumToSplitBlocks++; 3256f590bf8SNemanja Ivanovic } 3266f590bf8SNemanja Ivanovic return Ret; 3276f590bf8SNemanja Ivanovic } 3286f590bf8SNemanja Ivanovic 3296f590bf8SNemanja Ivanovic /// Looks trhough a COPY instruction to the actual definition of the CR-bit 3306f590bf8SNemanja Ivanovic /// register and returns the instruction that defines it. 3316f590bf8SNemanja Ivanovic /// FIXME: This currently handles what is by-far the most common case: 3326f590bf8SNemanja Ivanovic /// an instruction that defines a CR field followed by a single copy of a bit 3336f590bf8SNemanja Ivanovic /// from that field into a virtual register. If chains of copies need to be 3346f590bf8SNemanja Ivanovic /// handled, this should have a loop until a non-copy instruction is found. 3356f590bf8SNemanja Ivanovic MachineInstr *PPCReduceCRLogicals::lookThroughCRCopy(unsigned Reg, 3366f590bf8SNemanja Ivanovic unsigned &Subreg, 3376f590bf8SNemanja Ivanovic MachineInstr *&CpDef) { 3386f590bf8SNemanja Ivanovic Subreg = -1; 3396f590bf8SNemanja Ivanovic if (!TargetRegisterInfo::isVirtualRegister(Reg)) 3406f590bf8SNemanja Ivanovic return nullptr; 3416f590bf8SNemanja Ivanovic MachineInstr *Copy = MRI->getVRegDef(Reg); 3426f590bf8SNemanja Ivanovic CpDef = Copy; 3436f590bf8SNemanja Ivanovic if (!Copy->isCopy()) 3446f590bf8SNemanja Ivanovic return Copy; 3456f590bf8SNemanja Ivanovic unsigned CopySrc = Copy->getOperand(1).getReg(); 3466f590bf8SNemanja Ivanovic Subreg = Copy->getOperand(1).getSubReg(); 3476f590bf8SNemanja Ivanovic if (!TargetRegisterInfo::isVirtualRegister(CopySrc)) { 3486f590bf8SNemanja Ivanovic const TargetRegisterInfo *TRI = &TII->getRegisterInfo(); 3496f590bf8SNemanja Ivanovic // Set the Subreg 3506f590bf8SNemanja Ivanovic if (CopySrc == PPC::CR0EQ || CopySrc == PPC::CR6EQ) 3516f590bf8SNemanja Ivanovic Subreg = PPC::sub_eq; 3526f590bf8SNemanja Ivanovic if (CopySrc == PPC::CR0LT || CopySrc == PPC::CR6LT) 3536f590bf8SNemanja Ivanovic Subreg = PPC::sub_lt; 3546f590bf8SNemanja Ivanovic if (CopySrc == PPC::CR0GT || CopySrc == PPC::CR6GT) 3556f590bf8SNemanja Ivanovic Subreg = PPC::sub_gt; 3566f590bf8SNemanja Ivanovic if (CopySrc == PPC::CR0UN || CopySrc == PPC::CR6UN) 3576f590bf8SNemanja Ivanovic Subreg = PPC::sub_un; 3586f590bf8SNemanja Ivanovic // Loop backwards and return the first MI that modifies the physical CR Reg. 3596f590bf8SNemanja Ivanovic MachineBasicBlock::iterator Me = Copy, B = Copy->getParent()->begin(); 3606f590bf8SNemanja Ivanovic while (Me != B) 3616f590bf8SNemanja Ivanovic if ((--Me)->modifiesRegister(CopySrc, TRI)) 3626f590bf8SNemanja Ivanovic return &*Me; 3636f590bf8SNemanja Ivanovic return nullptr; 3646f590bf8SNemanja Ivanovic } 3656f590bf8SNemanja Ivanovic return MRI->getVRegDef(CopySrc); 3666f590bf8SNemanja Ivanovic } 3676f590bf8SNemanja Ivanovic 3686f590bf8SNemanja Ivanovic void PPCReduceCRLogicals::initialize(MachineFunction &MFParam) { 3696f590bf8SNemanja Ivanovic MF = &MFParam; 3706f590bf8SNemanja Ivanovic MRI = &MF->getRegInfo(); 3716f590bf8SNemanja Ivanovic TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo(); 3726f590bf8SNemanja Ivanovic MBPI = &getAnalysis<MachineBranchProbabilityInfo>(); 3736f590bf8SNemanja Ivanovic 3746f590bf8SNemanja Ivanovic AllCRLogicalOps.clear(); 3756f590bf8SNemanja Ivanovic } 3766f590bf8SNemanja Ivanovic 3776f590bf8SNemanja Ivanovic /// Contains all the implemented transformations on CR logical operations. 3786f590bf8SNemanja Ivanovic /// For example, a binary CR logical can be used to split a block on its inputs, 3796f590bf8SNemanja Ivanovic /// a unary CR logical might be used to change the condition code on a 3806f590bf8SNemanja Ivanovic /// comparison feeding it. A nullary CR logical might simply be removable 3816f590bf8SNemanja Ivanovic /// if the user of the bit it [un]sets can be transformed. 3826f590bf8SNemanja Ivanovic bool PPCReduceCRLogicals::handleCROp(CRLogicalOpInfo &CRI) { 3836f590bf8SNemanja Ivanovic // We can definitely split a block on the inputs to a binary CR operation 3846f590bf8SNemanja Ivanovic // whose defs and (single) use are within the same block. 3856f590bf8SNemanja Ivanovic bool Changed = false; 3866f590bf8SNemanja Ivanovic if (CRI.IsBinary && CRI.ContainedInBlock && CRI.SingleUse && CRI.FeedsBR && 3876f590bf8SNemanja Ivanovic CRI.DefsSingleUse) { 3886f590bf8SNemanja Ivanovic Changed = splitBlockOnBinaryCROp(CRI); 3896f590bf8SNemanja Ivanovic if (Changed) 3906f590bf8SNemanja Ivanovic NumBlocksSplitOnBinaryCROp++; 3916f590bf8SNemanja Ivanovic } 3926f590bf8SNemanja Ivanovic return Changed; 3936f590bf8SNemanja Ivanovic } 3946f590bf8SNemanja Ivanovic 3956f590bf8SNemanja Ivanovic /// Splits a block that contains a CR-logical operation that feeds a branch 3966f590bf8SNemanja Ivanovic /// and whose operands are produced within the block. 3976f590bf8SNemanja Ivanovic /// Example: 3986f590bf8SNemanja Ivanovic /// %vr5<def> = CMPDI %vr2, 0; CRRC:%vr5 G8RC:%vr2 3996f590bf8SNemanja Ivanovic /// %vr6<def> = COPY %vr5:sub_eq; CRBITRC:%vr6 CRRC:%vr5 4006f590bf8SNemanja Ivanovic /// %vr7<def> = CMPDI %vr3, 0; CRRC:%vr7 G8RC:%vr3 4016f590bf8SNemanja Ivanovic /// %vr8<def> = COPY %vr7:sub_eq; CRBITRC:%vr8 CRRC:%vr7 4026f590bf8SNemanja Ivanovic /// %vr9<def> = CROR %vr6<kill>, %vr8<kill>; CRBITRC:%vr9,%vr6,%vr8 4036f590bf8SNemanja Ivanovic /// BC %vr9<kill>, <BB#2>; CRBITRC:%vr9 4046f590bf8SNemanja Ivanovic /// Becomes: 4056f590bf8SNemanja Ivanovic /// %vr5<def> = CMPDI %vr2, 0; CRRC:%vr5 G8RC:%vr2 4066f590bf8SNemanja Ivanovic /// %vr6<def> = COPY %vr5:sub_eq; CRBITRC:%vr6 CRRC:%vr5 4076f590bf8SNemanja Ivanovic /// BC %vr6<kill>, <BB#2>; CRBITRC:%vr6 4086f590bf8SNemanja Ivanovic /// 4096f590bf8SNemanja Ivanovic /// %vr7<def> = CMPDI %vr3, 0; CRRC:%vr7 G8RC:%vr3 4106f590bf8SNemanja Ivanovic /// %vr8<def> = COPY %vr7:sub_eq; CRBITRC:%vr8 CRRC:%vr7 4116f590bf8SNemanja Ivanovic /// BC %vr9<kill>, <BB#2>; CRBITRC:%vr9 4126f590bf8SNemanja Ivanovic bool PPCReduceCRLogicals::splitBlockOnBinaryCROp(CRLogicalOpInfo &CRI) { 4136f590bf8SNemanja Ivanovic if (CRI.CopyDefs.first == CRI.CopyDefs.second) { 4146f590bf8SNemanja Ivanovic DEBUG(dbgs() << "Unable to split as the two operands are the same\n"); 4156f590bf8SNemanja Ivanovic NumNotSplitIdenticalOperands++; 4166f590bf8SNemanja Ivanovic return false; 4176f590bf8SNemanja Ivanovic } 4186f590bf8SNemanja Ivanovic if (CRI.TrueDefs.first->isCopy() || CRI.TrueDefs.second->isCopy() || 4196f590bf8SNemanja Ivanovic CRI.TrueDefs.first->isPHI() || CRI.TrueDefs.second->isPHI()) { 4206f590bf8SNemanja Ivanovic DEBUG(dbgs() << "Unable to split because one of the operands is a PHI or " 4216f590bf8SNemanja Ivanovic "chain of copies.\n"); 4226f590bf8SNemanja Ivanovic NumNotSplitChainCopies++; 4236f590bf8SNemanja Ivanovic return false; 4246f590bf8SNemanja Ivanovic } 4256f590bf8SNemanja Ivanovic // Note: keep in sync with computeBranchTargetAndInversion(). 4266f590bf8SNemanja Ivanovic if (CRI.MI->getOpcode() != PPC::CROR && 4276f590bf8SNemanja Ivanovic CRI.MI->getOpcode() != PPC::CRAND && 4286f590bf8SNemanja Ivanovic CRI.MI->getOpcode() != PPC::CRNOR && 4296f590bf8SNemanja Ivanovic CRI.MI->getOpcode() != PPC::CRNAND && 4306f590bf8SNemanja Ivanovic CRI.MI->getOpcode() != PPC::CRORC && 4316f590bf8SNemanja Ivanovic CRI.MI->getOpcode() != PPC::CRANDC) { 4326f590bf8SNemanja Ivanovic DEBUG(dbgs() << "Unable to split blocks on this opcode.\n"); 4336f590bf8SNemanja Ivanovic NumNotSplitWrongOpcode++; 4346f590bf8SNemanja Ivanovic return false; 4356f590bf8SNemanja Ivanovic } 4366f590bf8SNemanja Ivanovic DEBUG(dbgs() << "Splitting the following CR op:\n"; CRI.dump()); 4376f590bf8SNemanja Ivanovic MachineBasicBlock::iterator Def1It = CRI.TrueDefs.first; 4386f590bf8SNemanja Ivanovic MachineBasicBlock::iterator Def2It = CRI.TrueDefs.second; 4396f590bf8SNemanja Ivanovic 4406f590bf8SNemanja Ivanovic bool UsingDef1 = false; 4416f590bf8SNemanja Ivanovic MachineInstr *SplitBefore = &*Def2It; 4426f590bf8SNemanja Ivanovic for (auto E = CRI.MI->getParent()->end(); Def2It != E; ++Def2It) { 4436f590bf8SNemanja Ivanovic if (Def1It == Def2It) { // Def2 comes before Def1. 4446f590bf8SNemanja Ivanovic SplitBefore = &*Def1It; 4456f590bf8SNemanja Ivanovic UsingDef1 = true; 4466f590bf8SNemanja Ivanovic break; 4476f590bf8SNemanja Ivanovic } 4486f590bf8SNemanja Ivanovic } 4496f590bf8SNemanja Ivanovic 4506f590bf8SNemanja Ivanovic DEBUG(dbgs() << "We will split the following block:\n";); 4516f590bf8SNemanja Ivanovic DEBUG(CRI.MI->getParent()->dump()); 4526f590bf8SNemanja Ivanovic DEBUG(dbgs() << "Before instruction:\n"; SplitBefore->dump()); 4536f590bf8SNemanja Ivanovic 4546f590bf8SNemanja Ivanovic // Get the branch instruction. 4556f590bf8SNemanja Ivanovic MachineInstr *Branch = 4566f590bf8SNemanja Ivanovic MRI->use_nodbg_begin(CRI.MI->getOperand(0).getReg())->getParent(); 4576f590bf8SNemanja Ivanovic 4586f590bf8SNemanja Ivanovic // We want the new block to have no code in it other than the definition 4596f590bf8SNemanja Ivanovic // of the input to the CR logical and the CR logical itself. So we move 4606f590bf8SNemanja Ivanovic // those to the bottom of the block (just before the branch). Then we 4616f590bf8SNemanja Ivanovic // will split before the CR logical. 4626f590bf8SNemanja Ivanovic MachineBasicBlock *MBB = SplitBefore->getParent(); 4636f590bf8SNemanja Ivanovic auto FirstTerminator = MBB->getFirstTerminator(); 4646f590bf8SNemanja Ivanovic MachineBasicBlock::iterator FirstInstrToMove = 4656f590bf8SNemanja Ivanovic UsingDef1 ? CRI.TrueDefs.first : CRI.TrueDefs.second; 4666f590bf8SNemanja Ivanovic MachineBasicBlock::iterator SecondInstrToMove = 4676f590bf8SNemanja Ivanovic UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second; 4686f590bf8SNemanja Ivanovic 4696f590bf8SNemanja Ivanovic // The instructions that need to be moved are not guaranteed to be 4706f590bf8SNemanja Ivanovic // contiguous. Move them individually. 4716f590bf8SNemanja Ivanovic // FIXME: If one of the operands is a chain of (single use) copies, they 4726f590bf8SNemanja Ivanovic // can all be moved and we can still split. 4736f590bf8SNemanja Ivanovic MBB->splice(FirstTerminator, MBB, FirstInstrToMove); 4746f590bf8SNemanja Ivanovic if (FirstInstrToMove != SecondInstrToMove) 4756f590bf8SNemanja Ivanovic MBB->splice(FirstTerminator, MBB, SecondInstrToMove); 4766f590bf8SNemanja Ivanovic MBB->splice(FirstTerminator, MBB, CRI.MI); 4776f590bf8SNemanja Ivanovic 4786f590bf8SNemanja Ivanovic unsigned Opc = CRI.MI->getOpcode(); 4796f590bf8SNemanja Ivanovic bool InvertOrigBranch, InvertNewBranch, TargetIsFallThrough; 4806f590bf8SNemanja Ivanovic computeBranchTargetAndInversion(Opc, Branch->getOpcode(), UsingDef1, 4816f590bf8SNemanja Ivanovic InvertNewBranch, InvertOrigBranch, 4826f590bf8SNemanja Ivanovic TargetIsFallThrough); 4836f590bf8SNemanja Ivanovic MachineInstr *SplitCond = 4846f590bf8SNemanja Ivanovic UsingDef1 ? CRI.CopyDefs.second : CRI.CopyDefs.first; 4856f590bf8SNemanja Ivanovic DEBUG(dbgs() << "We will " << (InvertNewBranch ? "invert" : "copy")); 4866f590bf8SNemanja Ivanovic DEBUG(dbgs() << " the original branch and the target is the " << 4876f590bf8SNemanja Ivanovic (TargetIsFallThrough ? "fallthrough block\n" : "orig. target block\n")); 4886f590bf8SNemanja Ivanovic DEBUG(dbgs() << "Original branch instruction: "; Branch->dump()); 4896f590bf8SNemanja Ivanovic BlockSplitInfo BSI { Branch, SplitBefore, SplitCond, InvertNewBranch, 4906f590bf8SNemanja Ivanovic InvertOrigBranch, TargetIsFallThrough, MBPI, CRI.MI, 4916f590bf8SNemanja Ivanovic UsingDef1 ? CRI.CopyDefs.first : CRI.CopyDefs.second }; 4926f590bf8SNemanja Ivanovic bool Changed = splitMBB(BSI); 4936f590bf8SNemanja Ivanovic // If we've split on a CR logical that is fed by a CR logical, 4946f590bf8SNemanja Ivanovic // recompute the source CR logical as it may be usable for splitting. 4956f590bf8SNemanja Ivanovic if (Changed) { 4966f590bf8SNemanja Ivanovic bool Input1CRlogical = 4976f590bf8SNemanja Ivanovic CRI.TrueDefs.first && isCRLogical(*CRI.TrueDefs.first); 4986f590bf8SNemanja Ivanovic bool Input2CRlogical = 4996f590bf8SNemanja Ivanovic CRI.TrueDefs.second && isCRLogical(*CRI.TrueDefs.second); 5006f590bf8SNemanja Ivanovic if (Input1CRlogical) 5016f590bf8SNemanja Ivanovic AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.first)); 5026f590bf8SNemanja Ivanovic if (Input2CRlogical) 5036f590bf8SNemanja Ivanovic AllCRLogicalOps.push_back(createCRLogicalOpInfo(*CRI.TrueDefs.second)); 5046f590bf8SNemanja Ivanovic } 5056f590bf8SNemanja Ivanovic return Changed; 5066f590bf8SNemanja Ivanovic } 5076f590bf8SNemanja Ivanovic 5086f590bf8SNemanja Ivanovic void PPCReduceCRLogicals::collectCRLogicals() { 5096f590bf8SNemanja Ivanovic for (MachineBasicBlock &MBB : *MF) { 5106f590bf8SNemanja Ivanovic for (MachineInstr &MI : MBB) { 5116f590bf8SNemanja Ivanovic if (isCRLogical(MI)) { 5126f590bf8SNemanja Ivanovic AllCRLogicalOps.push_back(createCRLogicalOpInfo(MI)); 5136f590bf8SNemanja Ivanovic TotalCRLogicals++; 5146f590bf8SNemanja Ivanovic if (AllCRLogicalOps.back().IsNullary) 5156f590bf8SNemanja Ivanovic TotalNullaryCRLogicals++; 5166f590bf8SNemanja Ivanovic else if (AllCRLogicalOps.back().IsBinary) 5176f590bf8SNemanja Ivanovic TotalBinaryCRLogicals++; 5186f590bf8SNemanja Ivanovic else 5196f590bf8SNemanja Ivanovic TotalUnaryCRLogicals++; 5206f590bf8SNemanja Ivanovic } 5216f590bf8SNemanja Ivanovic } 5226f590bf8SNemanja Ivanovic } 5236f590bf8SNemanja Ivanovic } 5246f590bf8SNemanja Ivanovic 5256f590bf8SNemanja Ivanovic } // end annonymous namespace 5266f590bf8SNemanja Ivanovic 5276f590bf8SNemanja Ivanovic INITIALIZE_PASS_BEGIN(PPCReduceCRLogicals, DEBUG_TYPE, 5286f590bf8SNemanja Ivanovic "PowerPC Reduce CR logical Operation", false, false) 5296f590bf8SNemanja Ivanovic INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 5306f590bf8SNemanja Ivanovic INITIALIZE_PASS_END(PPCReduceCRLogicals, DEBUG_TYPE, 5316f590bf8SNemanja Ivanovic "PowerPC Reduce CR logical Operation", false, false) 5326f590bf8SNemanja Ivanovic 5336f590bf8SNemanja Ivanovic char PPCReduceCRLogicals::ID = 0; 5346f590bf8SNemanja Ivanovic FunctionPass* 5356f590bf8SNemanja Ivanovic llvm::createPPCReduceCRLogicalsPass() { return new PPCReduceCRLogicals(); } 536