10b57cec5SDimitry Andric //===-------------- PPCMIPeephole.cpp - MI Peephole Cleanups -------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===---------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This pass performs peephole optimizations to clean up ugly code 100b57cec5SDimitry Andric // sequences at the MachineInstruction layer. It runs at the end of 110b57cec5SDimitry Andric // the SSA phases, following VSX swap removal. A pass of dead code 120b57cec5SDimitry Andric // elimination follows this one for quick clean-up of any dead 130b57cec5SDimitry Andric // instructions introduced here. Although we could do this as callbacks 140b57cec5SDimitry Andric // from the generic peephole pass, this would have a couple of bad 150b57cec5SDimitry Andric // effects: it might remove optimization opportunities for VSX swap 160b57cec5SDimitry Andric // removal, and it would miss cleanups made possible following VSX 170b57cec5SDimitry Andric // swap removal. 180b57cec5SDimitry Andric // 195f757f3fSDimitry Andric // NOTE: We run the verifier after this pass in Asserts/Debug builds so it 205f757f3fSDimitry Andric // is important to keep the code valid after transformations. 215f757f3fSDimitry Andric // Common causes of errors stem from violating the contract specified 225f757f3fSDimitry Andric // by kill flags. Whenever a transformation changes the live range of 235f757f3fSDimitry Andric // a register, that register should be added to the work list using 245f757f3fSDimitry Andric // addRegToUpdate(RegsToUpdate, <Reg>). Furthermore, if a transformation 255f757f3fSDimitry Andric // is changing the definition of a register (i.e. removing the single 265f757f3fSDimitry Andric // definition of the original vreg), it needs to provide a dummy 275f757f3fSDimitry Andric // definition of that register using addDummyDef(<MBB>, <Reg>). 280b57cec5SDimitry Andric //===---------------------------------------------------------------------===// 290b57cec5SDimitry Andric 30480093f4SDimitry Andric #include "MCTargetDesc/PPCMCTargetDesc.h" 31480093f4SDimitry Andric #include "MCTargetDesc/PPCPredicates.h" 320b57cec5SDimitry Andric #include "PPC.h" 330b57cec5SDimitry Andric #include "PPCInstrBuilder.h" 340b57cec5SDimitry Andric #include "PPCInstrInfo.h" 350b57cec5SDimitry Andric #include "PPCMachineFunctionInfo.h" 360b57cec5SDimitry Andric #include "PPCTargetMachine.h" 370b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 385f757f3fSDimitry Andric #include "llvm/CodeGen/LiveVariables.h" 390b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBlockFrequencyInfo.h" 400b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h" 4181ad6265SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h" 420b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 430b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 44480093f4SDimitry Andric #include "llvm/CodeGen/MachinePostDominators.h" 450b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h" 46480093f4SDimitry Andric #include "llvm/InitializePasses.h" 470b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 48*0fca6ea1SDimitry Andric #include "llvm/Support/DebugCounter.h" 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric using namespace llvm; 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric #define DEBUG_TYPE "ppc-mi-peepholes" 530b57cec5SDimitry Andric 540b57cec5SDimitry Andric STATISTIC(RemoveTOCSave, "Number of TOC saves removed"); 550b57cec5SDimitry Andric STATISTIC(MultiTOCSaves, 560b57cec5SDimitry Andric "Number of functions with multiple TOC saves that must be kept"); 570b57cec5SDimitry Andric STATISTIC(NumTOCSavesInPrologue, "Number of TOC saves placed in the prologue"); 580b57cec5SDimitry Andric STATISTIC(NumEliminatedSExt, "Number of eliminated sign-extensions"); 590b57cec5SDimitry Andric STATISTIC(NumEliminatedZExt, "Number of eliminated zero-extensions"); 600b57cec5SDimitry Andric STATISTIC(NumOptADDLIs, "Number of optimized ADD instruction fed by LI"); 610b57cec5SDimitry Andric STATISTIC(NumConvertedToImmediateForm, 620b57cec5SDimitry Andric "Number of instructions converted to their immediate form"); 630b57cec5SDimitry Andric STATISTIC(NumFunctionsEnteredInMIPeephole, 640b57cec5SDimitry Andric "Number of functions entered in PPC MI Peepholes"); 650b57cec5SDimitry Andric STATISTIC(NumFixedPointIterations, 660b57cec5SDimitry Andric "Number of fixed-point iterations converting reg-reg instructions " 670b57cec5SDimitry Andric "to reg-imm ones"); 680b57cec5SDimitry Andric STATISTIC(NumRotatesCollapsed, 690b57cec5SDimitry Andric "Number of pairs of rotate left, clear left/right collapsed"); 700b57cec5SDimitry Andric STATISTIC(NumEXTSWAndSLDICombined, 710b57cec5SDimitry Andric "Number of pairs of EXTSW and SLDI combined as EXTSWSLI"); 725ffd83dbSDimitry Andric STATISTIC(NumLoadImmZeroFoldedAndRemoved, 735ffd83dbSDimitry Andric "Number of LI(8) reg, 0 that are folded to r0 and removed"); 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric static cl::opt<bool> 760b57cec5SDimitry Andric FixedPointRegToImm("ppc-reg-to-imm-fixed-point", cl::Hidden, cl::init(true), 770b57cec5SDimitry Andric cl::desc("Iterate to a fixed point when attempting to " 780b57cec5SDimitry Andric "convert reg-reg instructions to reg-imm")); 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric static cl::opt<bool> 810b57cec5SDimitry Andric ConvertRegReg("ppc-convert-rr-to-ri", cl::Hidden, cl::init(true), 820b57cec5SDimitry Andric cl::desc("Convert eligible reg+reg instructions to reg+imm")); 830b57cec5SDimitry Andric 840b57cec5SDimitry Andric static cl::opt<bool> 850b57cec5SDimitry Andric EnableSExtElimination("ppc-eliminate-signext", 860b57cec5SDimitry Andric cl::desc("enable elimination of sign-extensions"), 87bdd1243dSDimitry Andric cl::init(true), cl::Hidden); 880b57cec5SDimitry Andric 890b57cec5SDimitry Andric static cl::opt<bool> 900b57cec5SDimitry Andric EnableZExtElimination("ppc-eliminate-zeroext", 910b57cec5SDimitry Andric cl::desc("enable elimination of zero-extensions"), 92bdd1243dSDimitry Andric cl::init(true), cl::Hidden); 930b57cec5SDimitry Andric 94349cc55cSDimitry Andric static cl::opt<bool> 95349cc55cSDimitry Andric EnableTrapOptimization("ppc-opt-conditional-trap", 96349cc55cSDimitry Andric cl::desc("enable optimization of conditional traps"), 97349cc55cSDimitry Andric cl::init(false), cl::Hidden); 98349cc55cSDimitry Andric 99*0fca6ea1SDimitry Andric DEBUG_COUNTER( 100*0fca6ea1SDimitry Andric PeepholeXToICounter, "ppc-xtoi-peephole", 101*0fca6ea1SDimitry Andric "Controls whether PPC reg+reg to reg+imm peephole is performed on a MI"); 102*0fca6ea1SDimitry Andric 103*0fca6ea1SDimitry Andric DEBUG_COUNTER(PeepholePerOpCounter, "ppc-per-op-peephole", 104*0fca6ea1SDimitry Andric "Controls whether PPC per opcode peephole is performed on a MI"); 105*0fca6ea1SDimitry Andric 1060b57cec5SDimitry Andric namespace { 1070b57cec5SDimitry Andric 1080b57cec5SDimitry Andric struct PPCMIPeephole : public MachineFunctionPass { 1090b57cec5SDimitry Andric 1100b57cec5SDimitry Andric static char ID; 1110b57cec5SDimitry Andric const PPCInstrInfo *TII; 1120b57cec5SDimitry Andric MachineFunction *MF; 1130b57cec5SDimitry Andric MachineRegisterInfo *MRI; 1145f757f3fSDimitry Andric LiveVariables *LV; 1150b57cec5SDimitry Andric 1160b57cec5SDimitry Andric PPCMIPeephole() : MachineFunctionPass(ID) { 1170b57cec5SDimitry Andric initializePPCMIPeepholePass(*PassRegistry::getPassRegistry()); 1180b57cec5SDimitry Andric } 1190b57cec5SDimitry Andric 1200b57cec5SDimitry Andric private: 1210b57cec5SDimitry Andric MachineDominatorTree *MDT; 1220b57cec5SDimitry Andric MachinePostDominatorTree *MPDT; 1230b57cec5SDimitry Andric MachineBlockFrequencyInfo *MBFI; 1245f757f3fSDimitry Andric BlockFrequency EntryFreq; 1255f757f3fSDimitry Andric SmallSet<Register, 16> RegsToUpdate; 1260b57cec5SDimitry Andric 1270b57cec5SDimitry Andric // Initialize class variables. 1280b57cec5SDimitry Andric void initialize(MachineFunction &MFParm); 1290b57cec5SDimitry Andric 1300b57cec5SDimitry Andric // Perform peepholes. 13104eeddc0SDimitry Andric bool simplifyCode(); 1320b57cec5SDimitry Andric 1330b57cec5SDimitry Andric // Perform peepholes. 13404eeddc0SDimitry Andric bool eliminateRedundantCompare(); 1350b57cec5SDimitry Andric bool eliminateRedundantTOCSaves(std::map<MachineInstr *, bool> &TOCSaves); 1360b57cec5SDimitry Andric bool combineSEXTAndSHL(MachineInstr &MI, MachineInstr *&ToErase); 1375f757f3fSDimitry Andric bool emitRLDICWhenLoweringJumpTables(MachineInstr &MI, 1385f757f3fSDimitry Andric MachineInstr *&ToErase); 1390b57cec5SDimitry Andric void UpdateTOCSaves(std::map<MachineInstr *, bool> &TOCSaves, 1400b57cec5SDimitry Andric MachineInstr *MI); 1410b57cec5SDimitry Andric 1425f757f3fSDimitry Andric // A number of transformations will eliminate the definition of a register 1435f757f3fSDimitry Andric // as all of its uses will be removed. However, this leaves a register 1445f757f3fSDimitry Andric // without a definition for LiveVariables. Such transformations should 1455f757f3fSDimitry Andric // use this function to provide a dummy definition of the register that 1465f757f3fSDimitry Andric // will simply be removed by DCE. 1475f757f3fSDimitry Andric void addDummyDef(MachineBasicBlock &MBB, MachineInstr *At, Register Reg) { 1485f757f3fSDimitry Andric BuildMI(MBB, At, At->getDebugLoc(), TII->get(PPC::IMPLICIT_DEF), Reg); 1495f757f3fSDimitry Andric } 1505f757f3fSDimitry Andric void addRegToUpdateWithLine(Register Reg, int Line); 1515f757f3fSDimitry Andric void convertUnprimedAccPHIs(const PPCInstrInfo *TII, MachineRegisterInfo *MRI, 1525f757f3fSDimitry Andric SmallVectorImpl<MachineInstr *> &PHIs, 1535f757f3fSDimitry Andric Register Dst); 1545f757f3fSDimitry Andric 1550b57cec5SDimitry Andric public: 1560b57cec5SDimitry Andric 1570b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 158*0fca6ea1SDimitry Andric AU.addRequired<LiveVariablesWrapperPass>(); 159*0fca6ea1SDimitry Andric AU.addRequired<MachineDominatorTreeWrapperPass>(); 160*0fca6ea1SDimitry Andric AU.addRequired<MachinePostDominatorTreeWrapperPass>(); 161*0fca6ea1SDimitry Andric AU.addRequired<MachineBlockFrequencyInfoWrapperPass>(); 162*0fca6ea1SDimitry Andric AU.addPreserved<LiveVariablesWrapperPass>(); 163*0fca6ea1SDimitry Andric AU.addPreserved<MachineDominatorTreeWrapperPass>(); 164*0fca6ea1SDimitry Andric AU.addPreserved<MachinePostDominatorTreeWrapperPass>(); 165*0fca6ea1SDimitry Andric AU.addPreserved<MachineBlockFrequencyInfoWrapperPass>(); 1660b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 1670b57cec5SDimitry Andric } 1680b57cec5SDimitry Andric 1690b57cec5SDimitry Andric // Main entry point for this pass. 1700b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override { 1715ffd83dbSDimitry Andric initialize(MF); 1725ffd83dbSDimitry Andric // At this point, TOC pointer should not be used in a function that uses 1735ffd83dbSDimitry Andric // PC-Relative addressing. 1745ffd83dbSDimitry Andric assert((MF.getRegInfo().use_empty(PPC::X2) || 1755ffd83dbSDimitry Andric !MF.getSubtarget<PPCSubtarget>().isUsingPCRelativeCalls()) && 1765ffd83dbSDimitry Andric "TOC pointer used in a function using PC-Relative addressing!"); 1770b57cec5SDimitry Andric if (skipFunction(MF.getFunction())) 1780b57cec5SDimitry Andric return false; 1795f757f3fSDimitry Andric bool Changed = simplifyCode(); 1805f757f3fSDimitry Andric #ifndef NDEBUG 1815f757f3fSDimitry Andric if (Changed) 1825f757f3fSDimitry Andric MF.verify(this, "Error in PowerPC MI Peephole optimization, compile with " 1835f757f3fSDimitry Andric "-mllvm -disable-ppc-peephole"); 1845f757f3fSDimitry Andric #endif 1855f757f3fSDimitry Andric return Changed; 1860b57cec5SDimitry Andric } 1870b57cec5SDimitry Andric }; 1880b57cec5SDimitry Andric 1895f757f3fSDimitry Andric #define addRegToUpdate(R) addRegToUpdateWithLine(R, __LINE__) 1905f757f3fSDimitry Andric void PPCMIPeephole::addRegToUpdateWithLine(Register Reg, int Line) { 1915f757f3fSDimitry Andric if (!Register::isVirtualRegister(Reg)) 1925f757f3fSDimitry Andric return; 1935f757f3fSDimitry Andric if (RegsToUpdate.insert(Reg).second) 1945f757f3fSDimitry Andric LLVM_DEBUG(dbgs() << "Adding register: " << Register::virtReg2Index(Reg) 1955f757f3fSDimitry Andric << " on line " << Line 1965f757f3fSDimitry Andric << " for re-computation of kill flags\n"); 1975f757f3fSDimitry Andric } 1985f757f3fSDimitry Andric 1990b57cec5SDimitry Andric // Initialize class variables. 2000b57cec5SDimitry Andric void PPCMIPeephole::initialize(MachineFunction &MFParm) { 2010b57cec5SDimitry Andric MF = &MFParm; 2020b57cec5SDimitry Andric MRI = &MF->getRegInfo(); 203*0fca6ea1SDimitry Andric MDT = &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree(); 204*0fca6ea1SDimitry Andric MPDT = &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree(); 205*0fca6ea1SDimitry Andric MBFI = &getAnalysis<MachineBlockFrequencyInfoWrapperPass>().getMBFI(); 206*0fca6ea1SDimitry Andric LV = &getAnalysis<LiveVariablesWrapperPass>().getLV(); 2070b57cec5SDimitry Andric EntryFreq = MBFI->getEntryFreq(); 2080b57cec5SDimitry Andric TII = MF->getSubtarget<PPCSubtarget>().getInstrInfo(); 2095f757f3fSDimitry Andric RegsToUpdate.clear(); 2100b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "*** PowerPC MI peephole pass ***\n\n"); 2110b57cec5SDimitry Andric LLVM_DEBUG(MF->dump()); 2120b57cec5SDimitry Andric } 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric static MachineInstr *getVRegDefOrNull(MachineOperand *Op, 2150b57cec5SDimitry Andric MachineRegisterInfo *MRI) { 2160b57cec5SDimitry Andric assert(Op && "Invalid Operand!"); 2170b57cec5SDimitry Andric if (!Op->isReg()) 2180b57cec5SDimitry Andric return nullptr; 2190b57cec5SDimitry Andric 2208bcb0991SDimitry Andric Register Reg = Op->getReg(); 221bdd1243dSDimitry Andric if (!Reg.isVirtual()) 2220b57cec5SDimitry Andric return nullptr; 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric return MRI->getVRegDef(Reg); 2250b57cec5SDimitry Andric } 2260b57cec5SDimitry Andric 2270b57cec5SDimitry Andric // This function returns number of known zero bits in output of MI 2280b57cec5SDimitry Andric // starting from the most significant bit. 229bdd1243dSDimitry Andric static unsigned getKnownLeadingZeroCount(const unsigned Reg, 230bdd1243dSDimitry Andric const PPCInstrInfo *TII, 231bdd1243dSDimitry Andric const MachineRegisterInfo *MRI) { 232bdd1243dSDimitry Andric MachineInstr *MI = MRI->getVRegDef(Reg); 2330b57cec5SDimitry Andric unsigned Opcode = MI->getOpcode(); 234480093f4SDimitry Andric if (Opcode == PPC::RLDICL || Opcode == PPC::RLDICL_rec || 235480093f4SDimitry Andric Opcode == PPC::RLDCL || Opcode == PPC::RLDCL_rec) 2360b57cec5SDimitry Andric return MI->getOperand(3).getImm(); 2370b57cec5SDimitry Andric 238480093f4SDimitry Andric if ((Opcode == PPC::RLDIC || Opcode == PPC::RLDIC_rec) && 2390b57cec5SDimitry Andric MI->getOperand(3).getImm() <= 63 - MI->getOperand(2).getImm()) 2400b57cec5SDimitry Andric return MI->getOperand(3).getImm(); 2410b57cec5SDimitry Andric 242480093f4SDimitry Andric if ((Opcode == PPC::RLWINM || Opcode == PPC::RLWINM_rec || 243480093f4SDimitry Andric Opcode == PPC::RLWNM || Opcode == PPC::RLWNM_rec || 2440b57cec5SDimitry Andric Opcode == PPC::RLWINM8 || Opcode == PPC::RLWNM8) && 2450b57cec5SDimitry Andric MI->getOperand(3).getImm() <= MI->getOperand(4).getImm()) 2460b57cec5SDimitry Andric return 32 + MI->getOperand(3).getImm(); 2470b57cec5SDimitry Andric 248480093f4SDimitry Andric if (Opcode == PPC::ANDI_rec) { 2490b57cec5SDimitry Andric uint16_t Imm = MI->getOperand(2).getImm(); 25006c3fb27SDimitry Andric return 48 + llvm::countl_zero(Imm); 2510b57cec5SDimitry Andric } 2520b57cec5SDimitry Andric 253480093f4SDimitry Andric if (Opcode == PPC::CNTLZW || Opcode == PPC::CNTLZW_rec || 254480093f4SDimitry Andric Opcode == PPC::CNTTZW || Opcode == PPC::CNTTZW_rec || 2550b57cec5SDimitry Andric Opcode == PPC::CNTLZW8 || Opcode == PPC::CNTTZW8) 2560b57cec5SDimitry Andric // The result ranges from 0 to 32. 2570b57cec5SDimitry Andric return 58; 2580b57cec5SDimitry Andric 259480093f4SDimitry Andric if (Opcode == PPC::CNTLZD || Opcode == PPC::CNTLZD_rec || 260480093f4SDimitry Andric Opcode == PPC::CNTTZD || Opcode == PPC::CNTTZD_rec) 2610b57cec5SDimitry Andric // The result ranges from 0 to 64. 2620b57cec5SDimitry Andric return 57; 2630b57cec5SDimitry Andric 2640b57cec5SDimitry Andric if (Opcode == PPC::LHZ || Opcode == PPC::LHZX || 2650b57cec5SDimitry Andric Opcode == PPC::LHZ8 || Opcode == PPC::LHZX8 || 2660b57cec5SDimitry Andric Opcode == PPC::LHZU || Opcode == PPC::LHZUX || 2670b57cec5SDimitry Andric Opcode == PPC::LHZU8 || Opcode == PPC::LHZUX8) 2680b57cec5SDimitry Andric return 48; 2690b57cec5SDimitry Andric 2700b57cec5SDimitry Andric if (Opcode == PPC::LBZ || Opcode == PPC::LBZX || 2710b57cec5SDimitry Andric Opcode == PPC::LBZ8 || Opcode == PPC::LBZX8 || 2720b57cec5SDimitry Andric Opcode == PPC::LBZU || Opcode == PPC::LBZUX || 2730b57cec5SDimitry Andric Opcode == PPC::LBZU8 || Opcode == PPC::LBZUX8) 2740b57cec5SDimitry Andric return 56; 2750b57cec5SDimitry Andric 27606c3fb27SDimitry Andric if (Opcode == PPC::AND || Opcode == PPC::AND8 || Opcode == PPC::AND_rec || 27706c3fb27SDimitry Andric Opcode == PPC::AND8_rec) 27806c3fb27SDimitry Andric return std::max( 27906c3fb27SDimitry Andric getKnownLeadingZeroCount(MI->getOperand(1).getReg(), TII, MRI), 28006c3fb27SDimitry Andric getKnownLeadingZeroCount(MI->getOperand(2).getReg(), TII, MRI)); 28106c3fb27SDimitry Andric 28206c3fb27SDimitry Andric if (Opcode == PPC::OR || Opcode == PPC::OR8 || Opcode == PPC::XOR || 28306c3fb27SDimitry Andric Opcode == PPC::XOR8 || Opcode == PPC::OR_rec || 28406c3fb27SDimitry Andric Opcode == PPC::OR8_rec || Opcode == PPC::XOR_rec || 28506c3fb27SDimitry Andric Opcode == PPC::XOR8_rec) 28606c3fb27SDimitry Andric return std::min( 28706c3fb27SDimitry Andric getKnownLeadingZeroCount(MI->getOperand(1).getReg(), TII, MRI), 28806c3fb27SDimitry Andric getKnownLeadingZeroCount(MI->getOperand(2).getReg(), TII, MRI)); 28906c3fb27SDimitry Andric 290bdd1243dSDimitry Andric if (TII->isZeroExtended(Reg, MRI)) 2910b57cec5SDimitry Andric return 32; 2920b57cec5SDimitry Andric 2930b57cec5SDimitry Andric return 0; 2940b57cec5SDimitry Andric } 2950b57cec5SDimitry Andric 2960b57cec5SDimitry Andric // This function maintains a map for the pairs <TOC Save Instr, Keep> 2970b57cec5SDimitry Andric // Each time a new TOC save is encountered, it checks if any of the existing 2980b57cec5SDimitry Andric // ones are dominated by the new one. If so, it marks the existing one as 2990b57cec5SDimitry Andric // redundant by setting it's entry in the map as false. It then adds the new 3000b57cec5SDimitry Andric // instruction to the map with either true or false depending on if any 3010b57cec5SDimitry Andric // existing instructions dominated the new one. 3020b57cec5SDimitry Andric void PPCMIPeephole::UpdateTOCSaves( 3030b57cec5SDimitry Andric std::map<MachineInstr *, bool> &TOCSaves, MachineInstr *MI) { 3040b57cec5SDimitry Andric assert(TII->isTOCSaveMI(*MI) && "Expecting a TOC save instruction here"); 305fe6060f1SDimitry Andric // FIXME: Saving TOC in prologue hasn't been implemented well in AIX ABI part, 306fe6060f1SDimitry Andric // here only support it under ELFv2. 307fe6060f1SDimitry Andric if (MF->getSubtarget<PPCSubtarget>().isELFv2ABI()) { 3080b57cec5SDimitry Andric PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>(); 3090b57cec5SDimitry Andric 3100b57cec5SDimitry Andric MachineBasicBlock *Entry = &MF->front(); 3115f757f3fSDimitry Andric BlockFrequency CurrBlockFreq = MBFI->getBlockFreq(MI->getParent()); 3120b57cec5SDimitry Andric 3130b57cec5SDimitry Andric // If the block in which the TOC save resides is in a block that 3140b57cec5SDimitry Andric // post-dominates Entry, or a block that is hotter than entry (keep in mind 3150b57cec5SDimitry Andric // that early MachineLICM has already run so the TOC save won't be hoisted) 3160b57cec5SDimitry Andric // we can just do the save in the prologue. 3170b57cec5SDimitry Andric if (CurrBlockFreq > EntryFreq || MPDT->dominates(MI->getParent(), Entry)) 3180b57cec5SDimitry Andric FI->setMustSaveTOC(true); 3190b57cec5SDimitry Andric 320fe6060f1SDimitry Andric // If we are saving the TOC in the prologue, all the TOC saves can be 321fe6060f1SDimitry Andric // removed from the code. 3220b57cec5SDimitry Andric if (FI->mustSaveTOC()) { 3230b57cec5SDimitry Andric for (auto &TOCSave : TOCSaves) 3240b57cec5SDimitry Andric TOCSave.second = false; 3250b57cec5SDimitry Andric // Add new instruction to map. 3260b57cec5SDimitry Andric TOCSaves[MI] = false; 3270b57cec5SDimitry Andric return; 3280b57cec5SDimitry Andric } 329fe6060f1SDimitry Andric } 3300b57cec5SDimitry Andric 3310b57cec5SDimitry Andric bool Keep = true; 33204eeddc0SDimitry Andric for (auto &I : TOCSaves) { 33304eeddc0SDimitry Andric MachineInstr *CurrInst = I.first; 3340b57cec5SDimitry Andric // If new instruction dominates an existing one, mark existing one as 3350b57cec5SDimitry Andric // redundant. 33604eeddc0SDimitry Andric if (I.second && MDT->dominates(MI, CurrInst)) 33704eeddc0SDimitry Andric I.second = false; 3380b57cec5SDimitry Andric // Check if the new instruction is redundant. 3390b57cec5SDimitry Andric if (MDT->dominates(CurrInst, MI)) { 3400b57cec5SDimitry Andric Keep = false; 3410b57cec5SDimitry Andric break; 3420b57cec5SDimitry Andric } 3430b57cec5SDimitry Andric } 3440b57cec5SDimitry Andric // Add new instruction to map. 3450b57cec5SDimitry Andric TOCSaves[MI] = Keep; 3460b57cec5SDimitry Andric } 3470b57cec5SDimitry Andric 348e8d8bef9SDimitry Andric // This function returns a list of all PHI nodes in the tree starting from 349e8d8bef9SDimitry Andric // the RootPHI node. We perform a BFS traversal to get an ordered list of nodes. 350e8d8bef9SDimitry Andric // The list initially only contains the root PHI. When we visit a PHI node, we 351e8d8bef9SDimitry Andric // add it to the list. We continue to look for other PHI node operands while 352e8d8bef9SDimitry Andric // there are nodes to visit in the list. The function returns false if the 353e8d8bef9SDimitry Andric // optimization cannot be applied on this tree. 354e8d8bef9SDimitry Andric static bool collectUnprimedAccPHIs(MachineRegisterInfo *MRI, 355e8d8bef9SDimitry Andric MachineInstr *RootPHI, 356e8d8bef9SDimitry Andric SmallVectorImpl<MachineInstr *> &PHIs) { 357e8d8bef9SDimitry Andric PHIs.push_back(RootPHI); 358e8d8bef9SDimitry Andric unsigned VisitedIndex = 0; 359e8d8bef9SDimitry Andric while (VisitedIndex < PHIs.size()) { 360e8d8bef9SDimitry Andric MachineInstr *VisitedPHI = PHIs[VisitedIndex]; 361e8d8bef9SDimitry Andric for (unsigned PHIOp = 1, NumOps = VisitedPHI->getNumOperands(); 362e8d8bef9SDimitry Andric PHIOp != NumOps; PHIOp += 2) { 363e8d8bef9SDimitry Andric Register RegOp = VisitedPHI->getOperand(PHIOp).getReg(); 364bdd1243dSDimitry Andric if (!RegOp.isVirtual()) 365e8d8bef9SDimitry Andric return false; 366e8d8bef9SDimitry Andric MachineInstr *Instr = MRI->getVRegDef(RegOp); 367e8d8bef9SDimitry Andric // While collecting the PHI nodes, we check if they can be converted (i.e. 368e8d8bef9SDimitry Andric // all the operands are either copies, implicit defs or PHI nodes). 369e8d8bef9SDimitry Andric unsigned Opcode = Instr->getOpcode(); 370e8d8bef9SDimitry Andric if (Opcode == PPC::COPY) { 371e8d8bef9SDimitry Andric Register Reg = Instr->getOperand(1).getReg(); 372bdd1243dSDimitry Andric if (!Reg.isVirtual() || MRI->getRegClass(Reg) != &PPC::ACCRCRegClass) 373e8d8bef9SDimitry Andric return false; 374e8d8bef9SDimitry Andric } else if (Opcode != PPC::IMPLICIT_DEF && Opcode != PPC::PHI) 375e8d8bef9SDimitry Andric return false; 376e8d8bef9SDimitry Andric // If we detect a cycle in the PHI nodes, we exit. It would be 377e8d8bef9SDimitry Andric // possible to change cycles as well, but that would add a lot 378e8d8bef9SDimitry Andric // of complexity for a case that is unlikely to occur with MMA 379e8d8bef9SDimitry Andric // code. 380e8d8bef9SDimitry Andric if (Opcode != PPC::PHI) 381e8d8bef9SDimitry Andric continue; 382e8d8bef9SDimitry Andric if (llvm::is_contained(PHIs, Instr)) 383e8d8bef9SDimitry Andric return false; 384e8d8bef9SDimitry Andric PHIs.push_back(Instr); 385e8d8bef9SDimitry Andric } 386e8d8bef9SDimitry Andric VisitedIndex++; 387e8d8bef9SDimitry Andric } 388e8d8bef9SDimitry Andric return true; 389e8d8bef9SDimitry Andric } 390e8d8bef9SDimitry Andric 391e8d8bef9SDimitry Andric // This function changes the unprimed accumulator PHI nodes in the PHIs list to 392e8d8bef9SDimitry Andric // primed accumulator PHI nodes. The list is traversed in reverse order to 393e8d8bef9SDimitry Andric // change all the PHI operands of a PHI node before changing the node itself. 394e8d8bef9SDimitry Andric // We keep a map to associate each changed PHI node to its non-changed form. 3955f757f3fSDimitry Andric void PPCMIPeephole::convertUnprimedAccPHIs( 3965f757f3fSDimitry Andric const PPCInstrInfo *TII, MachineRegisterInfo *MRI, 3975f757f3fSDimitry Andric SmallVectorImpl<MachineInstr *> &PHIs, Register Dst) { 398e8d8bef9SDimitry Andric DenseMap<MachineInstr *, MachineInstr *> ChangedPHIMap; 399349cc55cSDimitry Andric for (MachineInstr *PHI : llvm::reverse(PHIs)) { 400e8d8bef9SDimitry Andric SmallVector<std::pair<MachineOperand, MachineOperand>, 4> PHIOps; 401e8d8bef9SDimitry Andric // We check if the current PHI node can be changed by looking at its 402e8d8bef9SDimitry Andric // operands. If all the operands are either copies from primed 403e8d8bef9SDimitry Andric // accumulators, implicit definitions or other unprimed accumulator 404e8d8bef9SDimitry Andric // PHI nodes, we change it. 405e8d8bef9SDimitry Andric for (unsigned PHIOp = 1, NumOps = PHI->getNumOperands(); PHIOp != NumOps; 406e8d8bef9SDimitry Andric PHIOp += 2) { 407e8d8bef9SDimitry Andric Register RegOp = PHI->getOperand(PHIOp).getReg(); 408e8d8bef9SDimitry Andric MachineInstr *PHIInput = MRI->getVRegDef(RegOp); 409e8d8bef9SDimitry Andric unsigned Opcode = PHIInput->getOpcode(); 410e8d8bef9SDimitry Andric assert((Opcode == PPC::COPY || Opcode == PPC::IMPLICIT_DEF || 411e8d8bef9SDimitry Andric Opcode == PPC::PHI) && 412e8d8bef9SDimitry Andric "Unexpected instruction"); 413e8d8bef9SDimitry Andric if (Opcode == PPC::COPY) { 414e8d8bef9SDimitry Andric assert(MRI->getRegClass(PHIInput->getOperand(1).getReg()) == 415e8d8bef9SDimitry Andric &PPC::ACCRCRegClass && 416e8d8bef9SDimitry Andric "Unexpected register class"); 417e8d8bef9SDimitry Andric PHIOps.push_back({PHIInput->getOperand(1), PHI->getOperand(PHIOp + 1)}); 418e8d8bef9SDimitry Andric } else if (Opcode == PPC::IMPLICIT_DEF) { 419e8d8bef9SDimitry Andric Register AccReg = MRI->createVirtualRegister(&PPC::ACCRCRegClass); 420e8d8bef9SDimitry Andric BuildMI(*PHIInput->getParent(), PHIInput, PHIInput->getDebugLoc(), 421e8d8bef9SDimitry Andric TII->get(PPC::IMPLICIT_DEF), AccReg); 422e8d8bef9SDimitry Andric PHIOps.push_back({MachineOperand::CreateReg(AccReg, false), 423e8d8bef9SDimitry Andric PHI->getOperand(PHIOp + 1)}); 424e8d8bef9SDimitry Andric } else if (Opcode == PPC::PHI) { 425e8d8bef9SDimitry Andric // We found a PHI operand. At this point we know this operand 426e8d8bef9SDimitry Andric // has already been changed so we get its associated changed form 427e8d8bef9SDimitry Andric // from the map. 428e8d8bef9SDimitry Andric assert(ChangedPHIMap.count(PHIInput) == 1 && 429e8d8bef9SDimitry Andric "This PHI node should have already been changed."); 430e8d8bef9SDimitry Andric MachineInstr *PrimedAccPHI = ChangedPHIMap.lookup(PHIInput); 431e8d8bef9SDimitry Andric PHIOps.push_back({MachineOperand::CreateReg( 432e8d8bef9SDimitry Andric PrimedAccPHI->getOperand(0).getReg(), false), 433e8d8bef9SDimitry Andric PHI->getOperand(PHIOp + 1)}); 434e8d8bef9SDimitry Andric } 435e8d8bef9SDimitry Andric } 436e8d8bef9SDimitry Andric Register AccReg = Dst; 437e8d8bef9SDimitry Andric // If the PHI node we are changing is the root node, the register it defines 438e8d8bef9SDimitry Andric // will be the destination register of the original copy (of the PHI def). 439e8d8bef9SDimitry Andric // For all other PHI's in the list, we need to create another primed 440e8d8bef9SDimitry Andric // accumulator virtual register as the PHI will no longer define the 441e8d8bef9SDimitry Andric // unprimed accumulator. 442e8d8bef9SDimitry Andric if (PHI != PHIs[0]) 443e8d8bef9SDimitry Andric AccReg = MRI->createVirtualRegister(&PPC::ACCRCRegClass); 444e8d8bef9SDimitry Andric MachineInstrBuilder NewPHI = BuildMI( 445e8d8bef9SDimitry Andric *PHI->getParent(), PHI, PHI->getDebugLoc(), TII->get(PPC::PHI), AccReg); 4465f757f3fSDimitry Andric for (auto RegMBB : PHIOps) { 447e8d8bef9SDimitry Andric NewPHI.add(RegMBB.first).add(RegMBB.second); 4485f757f3fSDimitry Andric if (MRI->isSSA()) 4495f757f3fSDimitry Andric addRegToUpdate(RegMBB.first.getReg()); 4505f757f3fSDimitry Andric } 451*0fca6ea1SDimitry Andric // The liveness of old PHI and new PHI have to be updated. 452*0fca6ea1SDimitry Andric addRegToUpdate(PHI->getOperand(0).getReg()); 453*0fca6ea1SDimitry Andric addRegToUpdate(AccReg); 454e8d8bef9SDimitry Andric ChangedPHIMap[PHI] = NewPHI.getInstr(); 455bdd1243dSDimitry Andric LLVM_DEBUG(dbgs() << "Converting PHI: "); 456bdd1243dSDimitry Andric LLVM_DEBUG(PHI->dump()); 457bdd1243dSDimitry Andric LLVM_DEBUG(dbgs() << "To: "); 458bdd1243dSDimitry Andric LLVM_DEBUG(NewPHI.getInstr()->dump()); 459e8d8bef9SDimitry Andric } 460e8d8bef9SDimitry Andric } 461e8d8bef9SDimitry Andric 4620b57cec5SDimitry Andric // Perform peephole optimizations. 46304eeddc0SDimitry Andric bool PPCMIPeephole::simplifyCode() { 4640b57cec5SDimitry Andric bool Simplified = false; 465349cc55cSDimitry Andric bool TrapOpt = false; 4660b57cec5SDimitry Andric MachineInstr* ToErase = nullptr; 4670b57cec5SDimitry Andric std::map<MachineInstr *, bool> TOCSaves; 4680b57cec5SDimitry Andric const TargetRegisterInfo *TRI = &TII->getRegisterInfo(); 4690b57cec5SDimitry Andric NumFunctionsEnteredInMIPeephole++; 4700b57cec5SDimitry Andric if (ConvertRegReg) { 4710b57cec5SDimitry Andric // Fixed-point conversion of reg/reg instructions fed by load-immediate 4720b57cec5SDimitry Andric // into reg/imm instructions. FIXME: This is expensive, control it with 4730b57cec5SDimitry Andric // an option. 4740b57cec5SDimitry Andric bool SomethingChanged = false; 4750b57cec5SDimitry Andric do { 4760b57cec5SDimitry Andric NumFixedPointIterations++; 4770b57cec5SDimitry Andric SomethingChanged = false; 4780b57cec5SDimitry Andric for (MachineBasicBlock &MBB : *MF) { 4790b57cec5SDimitry Andric for (MachineInstr &MI : MBB) { 4800b57cec5SDimitry Andric if (MI.isDebugInstr()) 4810b57cec5SDimitry Andric continue; 4820b57cec5SDimitry Andric 483*0fca6ea1SDimitry Andric if (!DebugCounter::shouldExecute(PeepholeXToICounter)) 484*0fca6ea1SDimitry Andric continue; 485*0fca6ea1SDimitry Andric 4865f757f3fSDimitry Andric SmallSet<Register, 4> RRToRIRegsToUpdate; 4875f757f3fSDimitry Andric if (!TII->convertToImmediateForm(MI, RRToRIRegsToUpdate)) 4885f757f3fSDimitry Andric continue; 4895f757f3fSDimitry Andric for (Register R : RRToRIRegsToUpdate) 4905f757f3fSDimitry Andric addRegToUpdate(R); 4915f757f3fSDimitry Andric // The updated instruction may now have new register operands. 4925f757f3fSDimitry Andric // Conservatively add them to recompute the flags as well. 4935f757f3fSDimitry Andric for (const MachineOperand &MO : MI.operands()) 4945f757f3fSDimitry Andric if (MO.isReg()) 4955f757f3fSDimitry Andric addRegToUpdate(MO.getReg()); 4960b57cec5SDimitry Andric // We don't erase anything in case the def has other uses. Let DCE 4970b57cec5SDimitry Andric // remove it if it can be removed. 4980b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Converted instruction to imm form: "); 4990b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 5000b57cec5SDimitry Andric NumConvertedToImmediateForm++; 5010b57cec5SDimitry Andric SomethingChanged = true; 5020b57cec5SDimitry Andric Simplified = true; 5030b57cec5SDimitry Andric continue; 5040b57cec5SDimitry Andric } 5050b57cec5SDimitry Andric } 5060b57cec5SDimitry Andric } while (SomethingChanged && FixedPointRegToImm); 5070b57cec5SDimitry Andric } 5080b57cec5SDimitry Andric 5095f757f3fSDimitry Andric // Since we are deleting this instruction, we need to run LiveVariables 5105f757f3fSDimitry Andric // on any of its definitions that are marked as needing an update since 5115f757f3fSDimitry Andric // we can't run LiveVariables on a deleted register. This only needs 5125f757f3fSDimitry Andric // to be done for defs since uses will have their own defining 5135f757f3fSDimitry Andric // instructions so we won't be running LiveVariables on a deleted reg. 5145f757f3fSDimitry Andric auto recomputeLVForDyingInstr = [&]() { 5155f757f3fSDimitry Andric if (RegsToUpdate.empty()) 5165f757f3fSDimitry Andric return; 5175f757f3fSDimitry Andric for (MachineOperand &MO : ToErase->operands()) { 5185f757f3fSDimitry Andric if (!MO.isReg() || !MO.isDef() || !RegsToUpdate.count(MO.getReg())) 5195f757f3fSDimitry Andric continue; 5205f757f3fSDimitry Andric Register RegToUpdate = MO.getReg(); 5215f757f3fSDimitry Andric RegsToUpdate.erase(RegToUpdate); 5225f757f3fSDimitry Andric // If some transformation has introduced an additional definition of 5235f757f3fSDimitry Andric // this register (breaking SSA), we can safely convert this def to 5245f757f3fSDimitry Andric // a def of an invalid register as the instruction is going away. 5255f757f3fSDimitry Andric if (!MRI->getUniqueVRegDef(RegToUpdate)) 5265f757f3fSDimitry Andric MO.setReg(PPC::NoRegister); 5275f757f3fSDimitry Andric LV->recomputeForSingleDefVirtReg(RegToUpdate); 5285f757f3fSDimitry Andric } 5295f757f3fSDimitry Andric }; 5305f757f3fSDimitry Andric 5310b57cec5SDimitry Andric for (MachineBasicBlock &MBB : *MF) { 5320b57cec5SDimitry Andric for (MachineInstr &MI : MBB) { 5330b57cec5SDimitry Andric 5340b57cec5SDimitry Andric // If the previous instruction was marked for elimination, 5350b57cec5SDimitry Andric // remove it now. 5360b57cec5SDimitry Andric if (ToErase) { 537bdd1243dSDimitry Andric LLVM_DEBUG(dbgs() << "Deleting instruction: "); 538bdd1243dSDimitry Andric LLVM_DEBUG(ToErase->dump()); 5395f757f3fSDimitry Andric recomputeLVForDyingInstr(); 5400b57cec5SDimitry Andric ToErase->eraseFromParent(); 5410b57cec5SDimitry Andric ToErase = nullptr; 5420b57cec5SDimitry Andric } 543349cc55cSDimitry Andric // If a conditional trap instruction got optimized to an 544349cc55cSDimitry Andric // unconditional trap, eliminate all the instructions after 545349cc55cSDimitry Andric // the trap. 546349cc55cSDimitry Andric if (EnableTrapOptimization && TrapOpt) { 547349cc55cSDimitry Andric ToErase = &MI; 548349cc55cSDimitry Andric continue; 549349cc55cSDimitry Andric } 5500b57cec5SDimitry Andric 5510b57cec5SDimitry Andric // Ignore debug instructions. 5520b57cec5SDimitry Andric if (MI.isDebugInstr()) 5530b57cec5SDimitry Andric continue; 5540b57cec5SDimitry Andric 555*0fca6ea1SDimitry Andric if (!DebugCounter::shouldExecute(PeepholePerOpCounter)) 556*0fca6ea1SDimitry Andric continue; 557*0fca6ea1SDimitry Andric 5580b57cec5SDimitry Andric // Per-opcode peepholes. 5590b57cec5SDimitry Andric switch (MI.getOpcode()) { 5600b57cec5SDimitry Andric 5610b57cec5SDimitry Andric default: 5620b57cec5SDimitry Andric break; 563e8d8bef9SDimitry Andric case PPC::COPY: { 564e8d8bef9SDimitry Andric Register Src = MI.getOperand(1).getReg(); 565e8d8bef9SDimitry Andric Register Dst = MI.getOperand(0).getReg(); 566bdd1243dSDimitry Andric if (!Src.isVirtual() || !Dst.isVirtual()) 567e8d8bef9SDimitry Andric break; 568e8d8bef9SDimitry Andric if (MRI->getRegClass(Src) != &PPC::UACCRCRegClass || 569e8d8bef9SDimitry Andric MRI->getRegClass(Dst) != &PPC::ACCRCRegClass) 570e8d8bef9SDimitry Andric break; 571e8d8bef9SDimitry Andric 572e8d8bef9SDimitry Andric // We are copying an unprimed accumulator to a primed accumulator. 573e8d8bef9SDimitry Andric // If the input to the copy is a PHI that is fed only by (i) copies in 574e8d8bef9SDimitry Andric // the other direction (ii) implicitly defined unprimed accumulators or 575e8d8bef9SDimitry Andric // (iii) other PHI nodes satisfying (i) and (ii), we can change 576e8d8bef9SDimitry Andric // the PHI to a PHI on primed accumulators (as long as we also change 577e8d8bef9SDimitry Andric // its operands). To detect and change such copies, we first get a list 578e8d8bef9SDimitry Andric // of all the PHI nodes starting from the root PHI node in BFS order. 579e8d8bef9SDimitry Andric // We then visit all these PHI nodes to check if they can be changed to 580e8d8bef9SDimitry Andric // primed accumulator PHI nodes and if so, we change them. 581e8d8bef9SDimitry Andric MachineInstr *RootPHI = MRI->getVRegDef(Src); 582e8d8bef9SDimitry Andric if (RootPHI->getOpcode() != PPC::PHI) 583e8d8bef9SDimitry Andric break; 584e8d8bef9SDimitry Andric 585e8d8bef9SDimitry Andric SmallVector<MachineInstr *, 4> PHIs; 586e8d8bef9SDimitry Andric if (!collectUnprimedAccPHIs(MRI, RootPHI, PHIs)) 587e8d8bef9SDimitry Andric break; 588e8d8bef9SDimitry Andric 589e8d8bef9SDimitry Andric convertUnprimedAccPHIs(TII, MRI, PHIs, Dst); 590e8d8bef9SDimitry Andric 591e8d8bef9SDimitry Andric ToErase = &MI; 592e8d8bef9SDimitry Andric break; 593e8d8bef9SDimitry Andric } 5945ffd83dbSDimitry Andric case PPC::LI: 5955ffd83dbSDimitry Andric case PPC::LI8: { 5965ffd83dbSDimitry Andric // If we are materializing a zero, look for any use operands for which 5975ffd83dbSDimitry Andric // zero means immediate zero. All such operands can be replaced with 5985ffd83dbSDimitry Andric // PPC::ZERO. 5995ffd83dbSDimitry Andric if (!MI.getOperand(1).isImm() || MI.getOperand(1).getImm() != 0) 6005ffd83dbSDimitry Andric break; 60104eeddc0SDimitry Andric Register MIDestReg = MI.getOperand(0).getReg(); 6025f757f3fSDimitry Andric bool Folded = false; 6035ffd83dbSDimitry Andric for (MachineInstr& UseMI : MRI->use_instructions(MIDestReg)) 6045f757f3fSDimitry Andric Folded |= TII->onlyFoldImmediate(UseMI, MI, MIDestReg); 6055ffd83dbSDimitry Andric if (MRI->use_nodbg_empty(MIDestReg)) { 6065ffd83dbSDimitry Andric ++NumLoadImmZeroFoldedAndRemoved; 6075ffd83dbSDimitry Andric ToErase = &MI; 6085ffd83dbSDimitry Andric } 6095f757f3fSDimitry Andric if (Folded) 6105f757f3fSDimitry Andric addRegToUpdate(MIDestReg); 6115f757f3fSDimitry Andric Simplified |= Folded; 6125ffd83dbSDimitry Andric break; 6135ffd83dbSDimitry Andric } 614fe6060f1SDimitry Andric case PPC::STW: 6150b57cec5SDimitry Andric case PPC::STD: { 6160b57cec5SDimitry Andric MachineFrameInfo &MFI = MF->getFrameInfo(); 6170b57cec5SDimitry Andric if (MFI.hasVarSizedObjects() || 618fe6060f1SDimitry Andric (!MF->getSubtarget<PPCSubtarget>().isELFv2ABI() && 619fe6060f1SDimitry Andric !MF->getSubtarget<PPCSubtarget>().isAIXABI())) 6200b57cec5SDimitry Andric break; 6210b57cec5SDimitry Andric // When encountering a TOC save instruction, call UpdateTOCSaves 6220b57cec5SDimitry Andric // to add it to the TOCSaves map and mark any existing TOC saves 6230b57cec5SDimitry Andric // it dominates as redundant. 6240b57cec5SDimitry Andric if (TII->isTOCSaveMI(MI)) 6250b57cec5SDimitry Andric UpdateTOCSaves(TOCSaves, &MI); 6260b57cec5SDimitry Andric break; 6270b57cec5SDimitry Andric } 6280b57cec5SDimitry Andric case PPC::XXPERMDI: { 6290b57cec5SDimitry Andric // Perform simplifications of 2x64 vector swaps and splats. 6300b57cec5SDimitry Andric // A swap is identified by an immediate value of 2, and a splat 6310b57cec5SDimitry Andric // is identified by an immediate value of 0 or 3. 6320b57cec5SDimitry Andric int Immed = MI.getOperand(3).getImm(); 6330b57cec5SDimitry Andric 634480093f4SDimitry Andric if (Immed == 1) 635480093f4SDimitry Andric break; 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andric // For each of these simplifications, we need the two source 6380b57cec5SDimitry Andric // regs to match. Unfortunately, MachineCSE ignores COPY and 6390b57cec5SDimitry Andric // SUBREG_TO_REG, so for example we can see 6400b57cec5SDimitry Andric // XXPERMDI t, SUBREG_TO_REG(s), SUBREG_TO_REG(s), immed. 6410b57cec5SDimitry Andric // We have to look through chains of COPY and SUBREG_TO_REG 6420b57cec5SDimitry Andric // to find the real source values for comparison. 64304eeddc0SDimitry Andric Register TrueReg1 = 6440b57cec5SDimitry Andric TRI->lookThruCopyLike(MI.getOperand(1).getReg(), MRI); 64504eeddc0SDimitry Andric Register TrueReg2 = 6460b57cec5SDimitry Andric TRI->lookThruCopyLike(MI.getOperand(2).getReg(), MRI); 6470b57cec5SDimitry Andric 648bdd1243dSDimitry Andric if (!(TrueReg1 == TrueReg2 && TrueReg1.isVirtual())) 649480093f4SDimitry Andric break; 650480093f4SDimitry Andric 6510b57cec5SDimitry Andric MachineInstr *DefMI = MRI->getVRegDef(TrueReg1); 652480093f4SDimitry Andric 653480093f4SDimitry Andric if (!DefMI) 654480093f4SDimitry Andric break; 655480093f4SDimitry Andric 656480093f4SDimitry Andric unsigned DefOpc = DefMI->getOpcode(); 6570b57cec5SDimitry Andric 6580b57cec5SDimitry Andric // If this is a splat fed by a splatting load, the splat is 6590b57cec5SDimitry Andric // redundant. Replace with a copy. This doesn't happen directly due 6600b57cec5SDimitry Andric // to code in PPCDAGToDAGISel.cpp, but it can happen when converting 6610b57cec5SDimitry Andric // a load of a double to a vector of 64-bit integers. 6620b57cec5SDimitry Andric auto isConversionOfLoadAndSplat = [=]() -> bool { 6630b57cec5SDimitry Andric if (DefOpc != PPC::XVCVDPSXDS && DefOpc != PPC::XVCVDPUXDS) 6640b57cec5SDimitry Andric return false; 66504eeddc0SDimitry Andric Register FeedReg1 = 6660b57cec5SDimitry Andric TRI->lookThruCopyLike(DefMI->getOperand(1).getReg(), MRI); 667bdd1243dSDimitry Andric if (FeedReg1.isVirtual()) { 668480093f4SDimitry Andric MachineInstr *LoadMI = MRI->getVRegDef(FeedReg1); 6690b57cec5SDimitry Andric if (LoadMI && LoadMI->getOpcode() == PPC::LXVDSX) 6700b57cec5SDimitry Andric return true; 6710b57cec5SDimitry Andric } 6720b57cec5SDimitry Andric return false; 6730b57cec5SDimitry Andric }; 674480093f4SDimitry Andric if ((Immed == 0 || Immed == 3) && 675480093f4SDimitry Andric (DefOpc == PPC::LXVDSX || isConversionOfLoadAndSplat())) { 6760b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Optimizing load-and-splat/splat " 6770b57cec5SDimitry Andric "to load-and-splat/copy: "); 6780b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 6790b57cec5SDimitry Andric BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(PPC::COPY), 6800b57cec5SDimitry Andric MI.getOperand(0).getReg()) 6810b57cec5SDimitry Andric .add(MI.getOperand(1)); 6825f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(1).getReg()); 6830b57cec5SDimitry Andric ToErase = &MI; 6840b57cec5SDimitry Andric Simplified = true; 6850b57cec5SDimitry Andric } 6860b57cec5SDimitry Andric 6870b57cec5SDimitry Andric // If this is a splat or a swap fed by another splat, we 6880b57cec5SDimitry Andric // can replace it with a copy. 6890b57cec5SDimitry Andric if (DefOpc == PPC::XXPERMDI) { 69004eeddc0SDimitry Andric Register DefReg1 = DefMI->getOperand(1).getReg(); 69104eeddc0SDimitry Andric Register DefReg2 = DefMI->getOperand(2).getReg(); 692480093f4SDimitry Andric unsigned DefImmed = DefMI->getOperand(3).getImm(); 6930b57cec5SDimitry Andric 694480093f4SDimitry Andric // If the two inputs are not the same register, check to see if 695480093f4SDimitry Andric // they originate from the same virtual register after only 696480093f4SDimitry Andric // copy-like instructions. 697480093f4SDimitry Andric if (DefReg1 != DefReg2) { 69804eeddc0SDimitry Andric Register FeedReg1 = TRI->lookThruCopyLike(DefReg1, MRI); 69904eeddc0SDimitry Andric Register FeedReg2 = TRI->lookThruCopyLike(DefReg2, MRI); 700480093f4SDimitry Andric 701bdd1243dSDimitry Andric if (!(FeedReg1 == FeedReg2 && FeedReg1.isVirtual())) 702480093f4SDimitry Andric break; 703480093f4SDimitry Andric } 704480093f4SDimitry Andric 705480093f4SDimitry Andric if (DefImmed == 0 || DefImmed == 3) { 7060b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Optimizing splat/swap or splat/splat " 7070b57cec5SDimitry Andric "to splat/copy: "); 7080b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 7090b57cec5SDimitry Andric BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(PPC::COPY), 7100b57cec5SDimitry Andric MI.getOperand(0).getReg()) 7110b57cec5SDimitry Andric .add(MI.getOperand(1)); 7125f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(1).getReg()); 7130b57cec5SDimitry Andric ToErase = &MI; 7140b57cec5SDimitry Andric Simplified = true; 7150b57cec5SDimitry Andric } 7160b57cec5SDimitry Andric 7170b57cec5SDimitry Andric // If this is a splat fed by a swap, we can simplify modify 7180b57cec5SDimitry Andric // the splat to splat the other value from the swap's input 7190b57cec5SDimitry Andric // parameter. 720480093f4SDimitry Andric else if ((Immed == 0 || Immed == 3) && DefImmed == 2) { 7210b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Optimizing swap/splat => splat: "); 7220b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 7235f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(1).getReg()); 7245f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(2).getReg()); 725480093f4SDimitry Andric MI.getOperand(1).setReg(DefReg1); 726480093f4SDimitry Andric MI.getOperand(2).setReg(DefReg2); 7270b57cec5SDimitry Andric MI.getOperand(3).setImm(3 - Immed); 7285f757f3fSDimitry Andric addRegToUpdate(DefReg1); 7295f757f3fSDimitry Andric addRegToUpdate(DefReg2); 7300b57cec5SDimitry Andric Simplified = true; 7310b57cec5SDimitry Andric } 7320b57cec5SDimitry Andric 7330b57cec5SDimitry Andric // If this is a swap fed by a swap, we can replace it 7340b57cec5SDimitry Andric // with a copy from the first swap's input. 735480093f4SDimitry Andric else if (Immed == 2 && DefImmed == 2) { 7360b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Optimizing swap/swap => copy: "); 7370b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 7385f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(1).getReg()); 7390b57cec5SDimitry Andric BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(PPC::COPY), 7400b57cec5SDimitry Andric MI.getOperand(0).getReg()) 7410b57cec5SDimitry Andric .add(DefMI->getOperand(1)); 7425f757f3fSDimitry Andric addRegToUpdate(DefMI->getOperand(0).getReg()); 7435f757f3fSDimitry Andric addRegToUpdate(DefMI->getOperand(1).getReg()); 7440b57cec5SDimitry Andric ToErase = &MI; 7450b57cec5SDimitry Andric Simplified = true; 7460b57cec5SDimitry Andric } 747349cc55cSDimitry Andric } else if ((Immed == 0 || Immed == 3 || Immed == 2) && 748349cc55cSDimitry Andric DefOpc == PPC::XXPERMDIs && 7490b57cec5SDimitry Andric (DefMI->getOperand(2).getImm() == 0 || 7500b57cec5SDimitry Andric DefMI->getOperand(2).getImm() == 3)) { 751349cc55cSDimitry Andric ToErase = &MI; 752349cc55cSDimitry Andric Simplified = true; 753349cc55cSDimitry Andric // Swap of a splat, convert to copy. 754349cc55cSDimitry Andric if (Immed == 2) { 755349cc55cSDimitry Andric LLVM_DEBUG(dbgs() << "Optimizing swap(splat) => copy(splat): "); 756349cc55cSDimitry Andric LLVM_DEBUG(MI.dump()); 757349cc55cSDimitry Andric BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(PPC::COPY), 758349cc55cSDimitry Andric MI.getOperand(0).getReg()) 759349cc55cSDimitry Andric .add(MI.getOperand(1)); 7605f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(1).getReg()); 761349cc55cSDimitry Andric break; 762349cc55cSDimitry Andric } 7630b57cec5SDimitry Andric // Splat fed by another splat - switch the output of the first 7640b57cec5SDimitry Andric // and remove the second. 7650b57cec5SDimitry Andric DefMI->getOperand(0).setReg(MI.getOperand(0).getReg()); 7660b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Removing redundant splat: "); 7670b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 76806c3fb27SDimitry Andric } else if (Immed == 2 && 76906c3fb27SDimitry Andric (DefOpc == PPC::VSPLTB || DefOpc == PPC::VSPLTH || 77006c3fb27SDimitry Andric DefOpc == PPC::VSPLTW || DefOpc == PPC::XXSPLTW || 77106c3fb27SDimitry Andric DefOpc == PPC::VSPLTISB || DefOpc == PPC::VSPLTISH || 77206c3fb27SDimitry Andric DefOpc == PPC::VSPLTISW)) { 77306c3fb27SDimitry Andric // Swap of various vector splats, convert to copy. 77406c3fb27SDimitry Andric ToErase = &MI; 77506c3fb27SDimitry Andric Simplified = true; 77606c3fb27SDimitry Andric LLVM_DEBUG(dbgs() << "Optimizing swap(vsplt(is)?[b|h|w]|xxspltw) => " 77706c3fb27SDimitry Andric "copy(vsplt(is)?[b|h|w]|xxspltw): "); 77806c3fb27SDimitry Andric LLVM_DEBUG(MI.dump()); 77906c3fb27SDimitry Andric BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(PPC::COPY), 78006c3fb27SDimitry Andric MI.getOperand(0).getReg()) 78106c3fb27SDimitry Andric .add(MI.getOperand(1)); 7825f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(1).getReg()); 78306c3fb27SDimitry Andric } else if ((Immed == 0 || Immed == 3 || Immed == 2) && 78406c3fb27SDimitry Andric TII->isLoadFromConstantPool(DefMI)) { 78506c3fb27SDimitry Andric const Constant *C = TII->getConstantFromConstantPool(DefMI); 78606c3fb27SDimitry Andric if (C && C->getType()->isVectorTy() && C->getSplatValue()) { 78706c3fb27SDimitry Andric ToErase = &MI; 78806c3fb27SDimitry Andric Simplified = true; 78906c3fb27SDimitry Andric LLVM_DEBUG(dbgs() 79006c3fb27SDimitry Andric << "Optimizing swap(splat pattern from constant-pool) " 79106c3fb27SDimitry Andric "=> copy(splat pattern from constant-pool): "); 79206c3fb27SDimitry Andric LLVM_DEBUG(MI.dump()); 79306c3fb27SDimitry Andric BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(PPC::COPY), 79406c3fb27SDimitry Andric MI.getOperand(0).getReg()) 79506c3fb27SDimitry Andric .add(MI.getOperand(1)); 7965f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(1).getReg()); 79706c3fb27SDimitry Andric } 7980b57cec5SDimitry Andric } 7990b57cec5SDimitry Andric break; 8000b57cec5SDimitry Andric } 8010b57cec5SDimitry Andric case PPC::VSPLTB: 8020b57cec5SDimitry Andric case PPC::VSPLTH: 8030b57cec5SDimitry Andric case PPC::XXSPLTW: { 8040b57cec5SDimitry Andric unsigned MyOpcode = MI.getOpcode(); 8050b57cec5SDimitry Andric unsigned OpNo = MyOpcode == PPC::XXSPLTW ? 1 : 2; 80604eeddc0SDimitry Andric Register TrueReg = 8070b57cec5SDimitry Andric TRI->lookThruCopyLike(MI.getOperand(OpNo).getReg(), MRI); 808bdd1243dSDimitry Andric if (!TrueReg.isVirtual()) 8090b57cec5SDimitry Andric break; 8100b57cec5SDimitry Andric MachineInstr *DefMI = MRI->getVRegDef(TrueReg); 8110b57cec5SDimitry Andric if (!DefMI) 8120b57cec5SDimitry Andric break; 8130b57cec5SDimitry Andric unsigned DefOpcode = DefMI->getOpcode(); 8140b57cec5SDimitry Andric auto isConvertOfSplat = [=]() -> bool { 8150b57cec5SDimitry Andric if (DefOpcode != PPC::XVCVSPSXWS && DefOpcode != PPC::XVCVSPUXWS) 8160b57cec5SDimitry Andric return false; 8178bcb0991SDimitry Andric Register ConvReg = DefMI->getOperand(1).getReg(); 818bdd1243dSDimitry Andric if (!ConvReg.isVirtual()) 8190b57cec5SDimitry Andric return false; 8200b57cec5SDimitry Andric MachineInstr *Splt = MRI->getVRegDef(ConvReg); 8210b57cec5SDimitry Andric return Splt && (Splt->getOpcode() == PPC::LXVWSX || 8220b57cec5SDimitry Andric Splt->getOpcode() == PPC::XXSPLTW); 8230b57cec5SDimitry Andric }; 8240b57cec5SDimitry Andric bool AlreadySplat = (MyOpcode == DefOpcode) || 8250b57cec5SDimitry Andric (MyOpcode == PPC::VSPLTB && DefOpcode == PPC::VSPLTBs) || 8260b57cec5SDimitry Andric (MyOpcode == PPC::VSPLTH && DefOpcode == PPC::VSPLTHs) || 8270b57cec5SDimitry Andric (MyOpcode == PPC::XXSPLTW && DefOpcode == PPC::XXSPLTWs) || 8280b57cec5SDimitry Andric (MyOpcode == PPC::XXSPLTW && DefOpcode == PPC::LXVWSX) || 8290b57cec5SDimitry Andric (MyOpcode == PPC::XXSPLTW && DefOpcode == PPC::MTVSRWS)|| 8300b57cec5SDimitry Andric (MyOpcode == PPC::XXSPLTW && isConvertOfSplat()); 8310b57cec5SDimitry Andric // If the instruction[s] that feed this splat have already splat 8320b57cec5SDimitry Andric // the value, this splat is redundant. 8330b57cec5SDimitry Andric if (AlreadySplat) { 8340b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Changing redundant splat to a copy: "); 8350b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 8360b57cec5SDimitry Andric BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(PPC::COPY), 8370b57cec5SDimitry Andric MI.getOperand(0).getReg()) 8380b57cec5SDimitry Andric .add(MI.getOperand(OpNo)); 8395f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(OpNo).getReg()); 8400b57cec5SDimitry Andric ToErase = &MI; 8410b57cec5SDimitry Andric Simplified = true; 8420b57cec5SDimitry Andric } 8430b57cec5SDimitry Andric // Splat fed by a shift. Usually when we align value to splat into 8440b57cec5SDimitry Andric // vector element zero. 8450b57cec5SDimitry Andric if (DefOpcode == PPC::XXSLDWI) { 8468bcb0991SDimitry Andric Register ShiftRes = DefMI->getOperand(0).getReg(); 8478bcb0991SDimitry Andric Register ShiftOp1 = DefMI->getOperand(1).getReg(); 8488bcb0991SDimitry Andric Register ShiftOp2 = DefMI->getOperand(2).getReg(); 8490b57cec5SDimitry Andric unsigned ShiftImm = DefMI->getOperand(3).getImm(); 850fe6060f1SDimitry Andric unsigned SplatImm = 851fe6060f1SDimitry Andric MI.getOperand(MyOpcode == PPC::XXSPLTW ? 2 : 1).getImm(); 8520b57cec5SDimitry Andric if (ShiftOp1 == ShiftOp2) { 8530b57cec5SDimitry Andric unsigned NewElem = (SplatImm + ShiftImm) & 0x3; 8540b57cec5SDimitry Andric if (MRI->hasOneNonDBGUse(ShiftRes)) { 8550b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Removing redundant shift: "); 8560b57cec5SDimitry Andric LLVM_DEBUG(DefMI->dump()); 8570b57cec5SDimitry Andric ToErase = DefMI; 8580b57cec5SDimitry Andric } 8590b57cec5SDimitry Andric Simplified = true; 8600b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Changing splat immediate from " << SplatImm 8610b57cec5SDimitry Andric << " to " << NewElem << " in instruction: "); 8620b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 8635f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(OpNo).getReg()); 8645f757f3fSDimitry Andric addRegToUpdate(ShiftOp1); 8655f757f3fSDimitry Andric MI.getOperand(OpNo).setReg(ShiftOp1); 8660b57cec5SDimitry Andric MI.getOperand(2).setImm(NewElem); 8670b57cec5SDimitry Andric } 8680b57cec5SDimitry Andric } 8690b57cec5SDimitry Andric break; 8700b57cec5SDimitry Andric } 8710b57cec5SDimitry Andric case PPC::XVCVDPSP: { 8720b57cec5SDimitry Andric // If this is a DP->SP conversion fed by an FRSP, the FRSP is redundant. 87304eeddc0SDimitry Andric Register TrueReg = 8740b57cec5SDimitry Andric TRI->lookThruCopyLike(MI.getOperand(1).getReg(), MRI); 875bdd1243dSDimitry Andric if (!TrueReg.isVirtual()) 8760b57cec5SDimitry Andric break; 8770b57cec5SDimitry Andric MachineInstr *DefMI = MRI->getVRegDef(TrueReg); 8780b57cec5SDimitry Andric 8790b57cec5SDimitry Andric // This can occur when building a vector of single precision or integer 8800b57cec5SDimitry Andric // values. 8810b57cec5SDimitry Andric if (DefMI && DefMI->getOpcode() == PPC::XXPERMDI) { 88204eeddc0SDimitry Andric Register DefsReg1 = 8830b57cec5SDimitry Andric TRI->lookThruCopyLike(DefMI->getOperand(1).getReg(), MRI); 88404eeddc0SDimitry Andric Register DefsReg2 = 8850b57cec5SDimitry Andric TRI->lookThruCopyLike(DefMI->getOperand(2).getReg(), MRI); 886bdd1243dSDimitry Andric if (!DefsReg1.isVirtual() || !DefsReg2.isVirtual()) 8870b57cec5SDimitry Andric break; 8880b57cec5SDimitry Andric MachineInstr *P1 = MRI->getVRegDef(DefsReg1); 8890b57cec5SDimitry Andric MachineInstr *P2 = MRI->getVRegDef(DefsReg2); 8900b57cec5SDimitry Andric 8910b57cec5SDimitry Andric if (!P1 || !P2) 8920b57cec5SDimitry Andric break; 8930b57cec5SDimitry Andric 8945ffd83dbSDimitry Andric // Remove the passed FRSP/XSRSP instruction if it only feeds this MI 8955ffd83dbSDimitry Andric // and set any uses of that FRSP/XSRSP (in this MI) to the source of 8965ffd83dbSDimitry Andric // the FRSP/XSRSP. 8970b57cec5SDimitry Andric auto removeFRSPIfPossible = [&](MachineInstr *RoundInstr) { 8985ffd83dbSDimitry Andric unsigned Opc = RoundInstr->getOpcode(); 8995ffd83dbSDimitry Andric if ((Opc == PPC::FRSP || Opc == PPC::XSRSP) && 9000b57cec5SDimitry Andric MRI->hasOneNonDBGUse(RoundInstr->getOperand(0).getReg())) { 9010b57cec5SDimitry Andric Simplified = true; 9028bcb0991SDimitry Andric Register ConvReg1 = RoundInstr->getOperand(1).getReg(); 9038bcb0991SDimitry Andric Register FRSPDefines = RoundInstr->getOperand(0).getReg(); 904e8d8bef9SDimitry Andric MachineInstr &Use = *(MRI->use_instr_nodbg_begin(FRSPDefines)); 9050b57cec5SDimitry Andric for (int i = 0, e = Use.getNumOperands(); i < e; ++i) 9060b57cec5SDimitry Andric if (Use.getOperand(i).isReg() && 9070b57cec5SDimitry Andric Use.getOperand(i).getReg() == FRSPDefines) 9080b57cec5SDimitry Andric Use.getOperand(i).setReg(ConvReg1); 9095ffd83dbSDimitry Andric LLVM_DEBUG(dbgs() << "Removing redundant FRSP/XSRSP:\n"); 9100b57cec5SDimitry Andric LLVM_DEBUG(RoundInstr->dump()); 9110b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "As it feeds instruction:\n"); 9120b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 9130b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Through instruction:\n"); 9140b57cec5SDimitry Andric LLVM_DEBUG(DefMI->dump()); 9155f757f3fSDimitry Andric addRegToUpdate(ConvReg1); 9165f757f3fSDimitry Andric addRegToUpdate(FRSPDefines); 9175f757f3fSDimitry Andric ToErase = RoundInstr; 9180b57cec5SDimitry Andric } 9190b57cec5SDimitry Andric }; 9200b57cec5SDimitry Andric 9210b57cec5SDimitry Andric // If the input to XVCVDPSP is a vector that was built (even 9220b57cec5SDimitry Andric // partially) out of FRSP's, the FRSP(s) can safely be removed 9230b57cec5SDimitry Andric // since this instruction performs the same operation. 9240b57cec5SDimitry Andric if (P1 != P2) { 9250b57cec5SDimitry Andric removeFRSPIfPossible(P1); 9260b57cec5SDimitry Andric removeFRSPIfPossible(P2); 9270b57cec5SDimitry Andric break; 9280b57cec5SDimitry Andric } 9290b57cec5SDimitry Andric removeFRSPIfPossible(P1); 9300b57cec5SDimitry Andric } 9310b57cec5SDimitry Andric break; 9320b57cec5SDimitry Andric } 9330b57cec5SDimitry Andric case PPC::EXTSH: 9340b57cec5SDimitry Andric case PPC::EXTSH8: 9350b57cec5SDimitry Andric case PPC::EXTSH8_32_64: { 9360b57cec5SDimitry Andric if (!EnableSExtElimination) break; 9378bcb0991SDimitry Andric Register NarrowReg = MI.getOperand(1).getReg(); 938bdd1243dSDimitry Andric if (!NarrowReg.isVirtual()) 9390b57cec5SDimitry Andric break; 9400b57cec5SDimitry Andric 9410b57cec5SDimitry Andric MachineInstr *SrcMI = MRI->getVRegDef(NarrowReg); 942bdd1243dSDimitry Andric unsigned SrcOpcode = SrcMI->getOpcode(); 9430b57cec5SDimitry Andric // If we've used a zero-extending load that we will sign-extend, 9440b57cec5SDimitry Andric // just do a sign-extending load. 945bdd1243dSDimitry Andric if (SrcOpcode == PPC::LHZ || SrcOpcode == PPC::LHZX) { 9460b57cec5SDimitry Andric if (!MRI->hasOneNonDBGUse(SrcMI->getOperand(0).getReg())) 9470b57cec5SDimitry Andric break; 948bdd1243dSDimitry Andric // Determine the new opcode. We need to make sure that if the original 949bdd1243dSDimitry Andric // instruction has a 64 bit opcode we keep using a 64 bit opcode. 950bdd1243dSDimitry Andric // Likewise if the source is X-Form the new opcode should also be 951bdd1243dSDimitry Andric // X-Form. 952bdd1243dSDimitry Andric unsigned Opc = PPC::LHA; 953bdd1243dSDimitry Andric bool SourceIsXForm = SrcOpcode == PPC::LHZX; 954bdd1243dSDimitry Andric bool MIIs64Bit = MI.getOpcode() == PPC::EXTSH8 || 955bdd1243dSDimitry Andric MI.getOpcode() == PPC::EXTSH8_32_64; 956bdd1243dSDimitry Andric 957bdd1243dSDimitry Andric if (SourceIsXForm && MIIs64Bit) 958bdd1243dSDimitry Andric Opc = PPC::LHAX8; 959bdd1243dSDimitry Andric else if (SourceIsXForm && !MIIs64Bit) 960bdd1243dSDimitry Andric Opc = PPC::LHAX; 961bdd1243dSDimitry Andric else if (MIIs64Bit) 962bdd1243dSDimitry Andric Opc = PPC::LHA8; 963bdd1243dSDimitry Andric 9645f757f3fSDimitry Andric addRegToUpdate(NarrowReg); 9655f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(0).getReg()); 9665f757f3fSDimitry Andric 9675f757f3fSDimitry Andric // We are removing a definition of NarrowReg which will cause 9685f757f3fSDimitry Andric // problems in AliveBlocks. Add an implicit def that will be 9695f757f3fSDimitry Andric // removed so that AliveBlocks are updated correctly. 9705f757f3fSDimitry Andric addDummyDef(MBB, &MI, NarrowReg); 9710b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Zero-extending load\n"); 9720b57cec5SDimitry Andric LLVM_DEBUG(SrcMI->dump()); 9730b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "and sign-extension\n"); 9740b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 9750b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "are merged into sign-extending load\n"); 9760b57cec5SDimitry Andric SrcMI->setDesc(TII->get(Opc)); 9770b57cec5SDimitry Andric SrcMI->getOperand(0).setReg(MI.getOperand(0).getReg()); 9780b57cec5SDimitry Andric ToErase = &MI; 9790b57cec5SDimitry Andric Simplified = true; 9800b57cec5SDimitry Andric NumEliminatedSExt++; 9810b57cec5SDimitry Andric } 9820b57cec5SDimitry Andric break; 9830b57cec5SDimitry Andric } 9840b57cec5SDimitry Andric case PPC::EXTSW: 9850b57cec5SDimitry Andric case PPC::EXTSW_32: 9860b57cec5SDimitry Andric case PPC::EXTSW_32_64: { 9870b57cec5SDimitry Andric if (!EnableSExtElimination) break; 9888bcb0991SDimitry Andric Register NarrowReg = MI.getOperand(1).getReg(); 989bdd1243dSDimitry Andric if (!NarrowReg.isVirtual()) 9900b57cec5SDimitry Andric break; 9910b57cec5SDimitry Andric 9920b57cec5SDimitry Andric MachineInstr *SrcMI = MRI->getVRegDef(NarrowReg); 993bdd1243dSDimitry Andric unsigned SrcOpcode = SrcMI->getOpcode(); 9940b57cec5SDimitry Andric // If we've used a zero-extending load that we will sign-extend, 9950b57cec5SDimitry Andric // just do a sign-extending load. 996bdd1243dSDimitry Andric if (SrcOpcode == PPC::LWZ || SrcOpcode == PPC::LWZX) { 9970b57cec5SDimitry Andric if (!MRI->hasOneNonDBGUse(SrcMI->getOperand(0).getReg())) 9980b57cec5SDimitry Andric break; 999bdd1243dSDimitry Andric 1000bdd1243dSDimitry Andric // The transformation from a zero-extending load to a sign-extending 1001bdd1243dSDimitry Andric // load is only legal when the displacement is a multiple of 4. 1002bdd1243dSDimitry Andric // If the displacement is not at least 4 byte aligned, don't perform 1003bdd1243dSDimitry Andric // the transformation. 1004bdd1243dSDimitry Andric bool IsWordAligned = false; 1005bdd1243dSDimitry Andric if (SrcMI->getOperand(1).isGlobal()) { 1006bdd1243dSDimitry Andric const GlobalObject *GO = 1007bdd1243dSDimitry Andric dyn_cast<GlobalObject>(SrcMI->getOperand(1).getGlobal()); 100806c3fb27SDimitry Andric if (GO && GO->getAlign() && *GO->getAlign() >= 4 && 100906c3fb27SDimitry Andric (SrcMI->getOperand(1).getOffset() % 4 == 0)) 1010bdd1243dSDimitry Andric IsWordAligned = true; 1011bdd1243dSDimitry Andric } else if (SrcMI->getOperand(1).isImm()) { 1012bdd1243dSDimitry Andric int64_t Value = SrcMI->getOperand(1).getImm(); 1013bdd1243dSDimitry Andric if (Value % 4 == 0) 1014bdd1243dSDimitry Andric IsWordAligned = true; 1015bdd1243dSDimitry Andric } 1016bdd1243dSDimitry Andric 1017bdd1243dSDimitry Andric // Determine the new opcode. We need to make sure that if the original 1018bdd1243dSDimitry Andric // instruction has a 64 bit opcode we keep using a 64 bit opcode. 1019bdd1243dSDimitry Andric // Likewise if the source is X-Form the new opcode should also be 1020bdd1243dSDimitry Andric // X-Form. 1021bdd1243dSDimitry Andric unsigned Opc = PPC::LWA_32; 1022bdd1243dSDimitry Andric bool SourceIsXForm = SrcOpcode == PPC::LWZX; 1023bdd1243dSDimitry Andric bool MIIs64Bit = MI.getOpcode() == PPC::EXTSW || 1024bdd1243dSDimitry Andric MI.getOpcode() == PPC::EXTSW_32_64; 1025bdd1243dSDimitry Andric 1026bdd1243dSDimitry Andric if (SourceIsXForm && MIIs64Bit) 1027bdd1243dSDimitry Andric Opc = PPC::LWAX; 1028bdd1243dSDimitry Andric else if (SourceIsXForm && !MIIs64Bit) 1029bdd1243dSDimitry Andric Opc = PPC::LWAX_32; 1030bdd1243dSDimitry Andric else if (MIIs64Bit) 1031bdd1243dSDimitry Andric Opc = PPC::LWA; 1032bdd1243dSDimitry Andric 1033bdd1243dSDimitry Andric if (!IsWordAligned && (Opc == PPC::LWA || Opc == PPC::LWA_32)) 1034bdd1243dSDimitry Andric break; 1035bdd1243dSDimitry Andric 10365f757f3fSDimitry Andric addRegToUpdate(NarrowReg); 10375f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(0).getReg()); 10385f757f3fSDimitry Andric 10395f757f3fSDimitry Andric // We are removing a definition of NarrowReg which will cause 10405f757f3fSDimitry Andric // problems in AliveBlocks. Add an implicit def that will be 10415f757f3fSDimitry Andric // removed so that AliveBlocks are updated correctly. 10425f757f3fSDimitry Andric addDummyDef(MBB, &MI, NarrowReg); 10430b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Zero-extending load\n"); 10440b57cec5SDimitry Andric LLVM_DEBUG(SrcMI->dump()); 10450b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "and sign-extension\n"); 10460b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 10470b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "are merged into sign-extending load\n"); 10480b57cec5SDimitry Andric SrcMI->setDesc(TII->get(Opc)); 10490b57cec5SDimitry Andric SrcMI->getOperand(0).setReg(MI.getOperand(0).getReg()); 10500b57cec5SDimitry Andric ToErase = &MI; 10510b57cec5SDimitry Andric Simplified = true; 10520b57cec5SDimitry Andric NumEliminatedSExt++; 10530b57cec5SDimitry Andric } else if (MI.getOpcode() == PPC::EXTSW_32_64 && 1054bdd1243dSDimitry Andric TII->isSignExtended(NarrowReg, MRI)) { 10550b57cec5SDimitry Andric // We can eliminate EXTSW if the input is known to be already 10560b57cec5SDimitry Andric // sign-extended. 10570b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Removing redundant sign-extension\n"); 10588bcb0991SDimitry Andric Register TmpReg = 10590b57cec5SDimitry Andric MF->getRegInfo().createVirtualRegister(&PPC::G8RCRegClass); 10600b57cec5SDimitry Andric BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(PPC::IMPLICIT_DEF), 10610b57cec5SDimitry Andric TmpReg); 10620b57cec5SDimitry Andric BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(PPC::INSERT_SUBREG), 10630b57cec5SDimitry Andric MI.getOperand(0).getReg()) 10640b57cec5SDimitry Andric .addReg(TmpReg) 10650b57cec5SDimitry Andric .addReg(NarrowReg) 10660b57cec5SDimitry Andric .addImm(PPC::sub_32); 10670b57cec5SDimitry Andric ToErase = &MI; 10680b57cec5SDimitry Andric Simplified = true; 10690b57cec5SDimitry Andric NumEliminatedSExt++; 10700b57cec5SDimitry Andric } 10710b57cec5SDimitry Andric break; 10720b57cec5SDimitry Andric } 10730b57cec5SDimitry Andric case PPC::RLDICL: { 10740b57cec5SDimitry Andric // We can eliminate RLDICL (e.g. for zero-extension) 10750b57cec5SDimitry Andric // if all bits to clear are already zero in the input. 10760b57cec5SDimitry Andric // This code assume following code sequence for zero-extension. 10770b57cec5SDimitry Andric // %6 = COPY %5:sub_32; (optional) 10780b57cec5SDimitry Andric // %8 = IMPLICIT_DEF; 10790b57cec5SDimitry Andric // %7<def,tied1> = INSERT_SUBREG %8<tied0>, %6, sub_32; 10800b57cec5SDimitry Andric if (!EnableZExtElimination) break; 10810b57cec5SDimitry Andric 10820b57cec5SDimitry Andric if (MI.getOperand(2).getImm() != 0) 10830b57cec5SDimitry Andric break; 10840b57cec5SDimitry Andric 10858bcb0991SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 1086bdd1243dSDimitry Andric if (!SrcReg.isVirtual()) 10870b57cec5SDimitry Andric break; 10880b57cec5SDimitry Andric 10890b57cec5SDimitry Andric MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); 10900b57cec5SDimitry Andric if (!(SrcMI && SrcMI->getOpcode() == PPC::INSERT_SUBREG && 10910b57cec5SDimitry Andric SrcMI->getOperand(0).isReg() && SrcMI->getOperand(1).isReg())) 10920b57cec5SDimitry Andric break; 10930b57cec5SDimitry Andric 10940b57cec5SDimitry Andric MachineInstr *ImpDefMI, *SubRegMI; 10950b57cec5SDimitry Andric ImpDefMI = MRI->getVRegDef(SrcMI->getOperand(1).getReg()); 10960b57cec5SDimitry Andric SubRegMI = MRI->getVRegDef(SrcMI->getOperand(2).getReg()); 10970b57cec5SDimitry Andric if (ImpDefMI->getOpcode() != PPC::IMPLICIT_DEF) break; 10980b57cec5SDimitry Andric 10990b57cec5SDimitry Andric SrcMI = SubRegMI; 11000b57cec5SDimitry Andric if (SubRegMI->getOpcode() == PPC::COPY) { 11018bcb0991SDimitry Andric Register CopyReg = SubRegMI->getOperand(1).getReg(); 1102bdd1243dSDimitry Andric if (CopyReg.isVirtual()) 11030b57cec5SDimitry Andric SrcMI = MRI->getVRegDef(CopyReg); 11040b57cec5SDimitry Andric } 1105bdd1243dSDimitry Andric if (!SrcMI->getOperand(0).isReg()) 1106bdd1243dSDimitry Andric break; 11070b57cec5SDimitry Andric 1108bdd1243dSDimitry Andric unsigned KnownZeroCount = 1109bdd1243dSDimitry Andric getKnownLeadingZeroCount(SrcMI->getOperand(0).getReg(), TII, MRI); 11100b57cec5SDimitry Andric if (MI.getOperand(3).getImm() <= KnownZeroCount) { 11110b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Removing redundant zero-extension\n"); 11120b57cec5SDimitry Andric BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(PPC::COPY), 11130b57cec5SDimitry Andric MI.getOperand(0).getReg()) 11140b57cec5SDimitry Andric .addReg(SrcReg); 11155f757f3fSDimitry Andric addRegToUpdate(SrcReg); 11160b57cec5SDimitry Andric ToErase = &MI; 11170b57cec5SDimitry Andric Simplified = true; 11180b57cec5SDimitry Andric NumEliminatedZExt++; 11190b57cec5SDimitry Andric } 11200b57cec5SDimitry Andric break; 11210b57cec5SDimitry Andric } 11220b57cec5SDimitry Andric 11230b57cec5SDimitry Andric // TODO: Any instruction that has an immediate form fed only by a PHI 11240b57cec5SDimitry Andric // whose operands are all load immediate can be folded away. We currently 11250b57cec5SDimitry Andric // do this for ADD instructions, but should expand it to arithmetic and 11260b57cec5SDimitry Andric // binary instructions with immediate forms in the future. 11270b57cec5SDimitry Andric case PPC::ADD4: 11280b57cec5SDimitry Andric case PPC::ADD8: { 11290b57cec5SDimitry Andric auto isSingleUsePHI = [&](MachineOperand *PhiOp) { 11300b57cec5SDimitry Andric assert(PhiOp && "Invalid Operand!"); 11310b57cec5SDimitry Andric MachineInstr *DefPhiMI = getVRegDefOrNull(PhiOp, MRI); 11320b57cec5SDimitry Andric 11330b57cec5SDimitry Andric return DefPhiMI && (DefPhiMI->getOpcode() == PPC::PHI) && 11340b57cec5SDimitry Andric MRI->hasOneNonDBGUse(DefPhiMI->getOperand(0).getReg()); 11350b57cec5SDimitry Andric }; 11360b57cec5SDimitry Andric 11370b57cec5SDimitry Andric auto dominatesAllSingleUseLIs = [&](MachineOperand *DominatorOp, 11380b57cec5SDimitry Andric MachineOperand *PhiOp) { 11390b57cec5SDimitry Andric assert(PhiOp && "Invalid Operand!"); 11400b57cec5SDimitry Andric assert(DominatorOp && "Invalid Operand!"); 11410b57cec5SDimitry Andric MachineInstr *DefPhiMI = getVRegDefOrNull(PhiOp, MRI); 11420b57cec5SDimitry Andric MachineInstr *DefDomMI = getVRegDefOrNull(DominatorOp, MRI); 11430b57cec5SDimitry Andric 11440b57cec5SDimitry Andric // Note: the vregs only show up at odd indices position of PHI Node, 11450b57cec5SDimitry Andric // the even indices position save the BB info. 11460b57cec5SDimitry Andric for (unsigned i = 1; i < DefPhiMI->getNumOperands(); i += 2) { 11470b57cec5SDimitry Andric MachineInstr *LiMI = 11480b57cec5SDimitry Andric getVRegDefOrNull(&DefPhiMI->getOperand(i), MRI); 11490b57cec5SDimitry Andric if (!LiMI || 11500b57cec5SDimitry Andric (LiMI->getOpcode() != PPC::LI && LiMI->getOpcode() != PPC::LI8) 11510b57cec5SDimitry Andric || !MRI->hasOneNonDBGUse(LiMI->getOperand(0).getReg()) || 11520b57cec5SDimitry Andric !MDT->dominates(DefDomMI, LiMI)) 11530b57cec5SDimitry Andric return false; 11540b57cec5SDimitry Andric } 11550b57cec5SDimitry Andric 11560b57cec5SDimitry Andric return true; 11570b57cec5SDimitry Andric }; 11580b57cec5SDimitry Andric 11590b57cec5SDimitry Andric MachineOperand Op1 = MI.getOperand(1); 11600b57cec5SDimitry Andric MachineOperand Op2 = MI.getOperand(2); 11610b57cec5SDimitry Andric if (isSingleUsePHI(&Op2) && dominatesAllSingleUseLIs(&Op1, &Op2)) 11620b57cec5SDimitry Andric std::swap(Op1, Op2); 11630b57cec5SDimitry Andric else if (!isSingleUsePHI(&Op1) || !dominatesAllSingleUseLIs(&Op2, &Op1)) 11640b57cec5SDimitry Andric break; // We don't have an ADD fed by LI's that can be transformed 11650b57cec5SDimitry Andric 11660b57cec5SDimitry Andric // Now we know that Op1 is the PHI node and Op2 is the dominator 11678bcb0991SDimitry Andric Register DominatorReg = Op2.getReg(); 11680b57cec5SDimitry Andric 11690b57cec5SDimitry Andric const TargetRegisterClass *TRC = MI.getOpcode() == PPC::ADD8 11700b57cec5SDimitry Andric ? &PPC::G8RC_and_G8RC_NOX0RegClass 11710b57cec5SDimitry Andric : &PPC::GPRC_and_GPRC_NOR0RegClass; 11720b57cec5SDimitry Andric MRI->setRegClass(DominatorReg, TRC); 11730b57cec5SDimitry Andric 11740b57cec5SDimitry Andric // replace LIs with ADDIs 11750b57cec5SDimitry Andric MachineInstr *DefPhiMI = getVRegDefOrNull(&Op1, MRI); 11760b57cec5SDimitry Andric for (unsigned i = 1; i < DefPhiMI->getNumOperands(); i += 2) { 11770b57cec5SDimitry Andric MachineInstr *LiMI = getVRegDefOrNull(&DefPhiMI->getOperand(i), MRI); 11780b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Optimizing LI to ADDI: "); 11790b57cec5SDimitry Andric LLVM_DEBUG(LiMI->dump()); 11800b57cec5SDimitry Andric 11810b57cec5SDimitry Andric // There could be repeated registers in the PHI, e.g: %1 = 11820b57cec5SDimitry Andric // PHI %6, <%bb.2>, %8, <%bb.3>, %8, <%bb.6>; So if we've 11830b57cec5SDimitry Andric // already replaced the def instruction, skip. 11840b57cec5SDimitry Andric if (LiMI->getOpcode() == PPC::ADDI || LiMI->getOpcode() == PPC::ADDI8) 11850b57cec5SDimitry Andric continue; 11860b57cec5SDimitry Andric 11870b57cec5SDimitry Andric assert((LiMI->getOpcode() == PPC::LI || 11880b57cec5SDimitry Andric LiMI->getOpcode() == PPC::LI8) && 11890b57cec5SDimitry Andric "Invalid Opcode!"); 11900b57cec5SDimitry Andric auto LiImm = LiMI->getOperand(1).getImm(); // save the imm of LI 119181ad6265SDimitry Andric LiMI->removeOperand(1); // remove the imm of LI 11920b57cec5SDimitry Andric LiMI->setDesc(TII->get(LiMI->getOpcode() == PPC::LI ? PPC::ADDI 11930b57cec5SDimitry Andric : PPC::ADDI8)); 11940b57cec5SDimitry Andric MachineInstrBuilder(*LiMI->getParent()->getParent(), *LiMI) 11950b57cec5SDimitry Andric .addReg(DominatorReg) 11960b57cec5SDimitry Andric .addImm(LiImm); // restore the imm of LI 11970b57cec5SDimitry Andric LLVM_DEBUG(LiMI->dump()); 11980b57cec5SDimitry Andric } 11990b57cec5SDimitry Andric 12000b57cec5SDimitry Andric // Replace ADD with COPY 12010b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Optimizing ADD to COPY: "); 12020b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 12030b57cec5SDimitry Andric BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(PPC::COPY), 12040b57cec5SDimitry Andric MI.getOperand(0).getReg()) 12050b57cec5SDimitry Andric .add(Op1); 12065f757f3fSDimitry Andric addRegToUpdate(Op1.getReg()); 12075f757f3fSDimitry Andric addRegToUpdate(Op2.getReg()); 12080b57cec5SDimitry Andric ToErase = &MI; 12090b57cec5SDimitry Andric Simplified = true; 12100b57cec5SDimitry Andric NumOptADDLIs++; 12110b57cec5SDimitry Andric break; 12120b57cec5SDimitry Andric } 12130b57cec5SDimitry Andric case PPC::RLDICR: { 12145f757f3fSDimitry Andric Simplified |= emitRLDICWhenLoweringJumpTables(MI, ToErase) || 12150b57cec5SDimitry Andric combineSEXTAndSHL(MI, ToErase); 12160b57cec5SDimitry Andric break; 12170b57cec5SDimitry Andric } 12185f757f3fSDimitry Andric case PPC::ANDI_rec: 12195f757f3fSDimitry Andric case PPC::ANDI8_rec: 12205f757f3fSDimitry Andric case PPC::ANDIS_rec: 12215f757f3fSDimitry Andric case PPC::ANDIS8_rec: { 12225f757f3fSDimitry Andric Register TrueReg = 12235f757f3fSDimitry Andric TRI->lookThruCopyLike(MI.getOperand(1).getReg(), MRI); 12245f757f3fSDimitry Andric if (!TrueReg.isVirtual() || !MRI->hasOneNonDBGUse(TrueReg)) 12255f757f3fSDimitry Andric break; 12265f757f3fSDimitry Andric 12275f757f3fSDimitry Andric MachineInstr *SrcMI = MRI->getVRegDef(TrueReg); 12285f757f3fSDimitry Andric if (!SrcMI) 12295f757f3fSDimitry Andric break; 12305f757f3fSDimitry Andric 12315f757f3fSDimitry Andric unsigned SrcOpCode = SrcMI->getOpcode(); 12325f757f3fSDimitry Andric if (SrcOpCode != PPC::RLDICL && SrcOpCode != PPC::RLDICR) 12335f757f3fSDimitry Andric break; 12345f757f3fSDimitry Andric 12355f757f3fSDimitry Andric Register SrcReg, DstReg; 12365f757f3fSDimitry Andric SrcReg = SrcMI->getOperand(1).getReg(); 12375f757f3fSDimitry Andric DstReg = MI.getOperand(1).getReg(); 12385f757f3fSDimitry Andric const TargetRegisterClass *SrcRC = MRI->getRegClassOrNull(SrcReg); 12395f757f3fSDimitry Andric const TargetRegisterClass *DstRC = MRI->getRegClassOrNull(DstReg); 12405f757f3fSDimitry Andric if (DstRC != SrcRC) 12415f757f3fSDimitry Andric break; 12425f757f3fSDimitry Andric 12435f757f3fSDimitry Andric uint64_t AndImm = MI.getOperand(2).getImm(); 12445f757f3fSDimitry Andric if (MI.getOpcode() == PPC::ANDIS_rec || 12455f757f3fSDimitry Andric MI.getOpcode() == PPC::ANDIS8_rec) 12465f757f3fSDimitry Andric AndImm <<= 16; 12475f757f3fSDimitry Andric uint64_t LZeroAndImm = llvm::countl_zero<uint64_t>(AndImm); 12485f757f3fSDimitry Andric uint64_t RZeroAndImm = llvm::countr_zero<uint64_t>(AndImm); 12495f757f3fSDimitry Andric uint64_t ImmSrc = SrcMI->getOperand(3).getImm(); 12505f757f3fSDimitry Andric 12515f757f3fSDimitry Andric // We can transfer `RLDICL/RLDICR + ANDI_rec/ANDIS_rec` to `ANDI_rec 0` 12525f757f3fSDimitry Andric // if all bits to AND are already zero in the input. 12535f757f3fSDimitry Andric bool PatternResultZero = 12545f757f3fSDimitry Andric (SrcOpCode == PPC::RLDICL && (RZeroAndImm + ImmSrc > 63)) || 12555f757f3fSDimitry Andric (SrcOpCode == PPC::RLDICR && LZeroAndImm > ImmSrc); 12565f757f3fSDimitry Andric 12575f757f3fSDimitry Andric // We can eliminate RLDICL/RLDICR if it's used to clear bits and all 12585f757f3fSDimitry Andric // bits cleared will be ANDed with 0 by ANDI_rec/ANDIS_rec. 12595f757f3fSDimitry Andric bool PatternRemoveRotate = 12605f757f3fSDimitry Andric SrcMI->getOperand(2).getImm() == 0 && 12615f757f3fSDimitry Andric ((SrcOpCode == PPC::RLDICL && LZeroAndImm >= ImmSrc) || 12625f757f3fSDimitry Andric (SrcOpCode == PPC::RLDICR && (RZeroAndImm + ImmSrc > 63))); 12635f757f3fSDimitry Andric 12645f757f3fSDimitry Andric if (!PatternResultZero && !PatternRemoveRotate) 12655f757f3fSDimitry Andric break; 12665f757f3fSDimitry Andric 12675f757f3fSDimitry Andric LLVM_DEBUG(dbgs() << "Combining pair: "); 12685f757f3fSDimitry Andric LLVM_DEBUG(SrcMI->dump()); 12695f757f3fSDimitry Andric LLVM_DEBUG(MI.dump()); 12705f757f3fSDimitry Andric if (PatternResultZero) 12715f757f3fSDimitry Andric MI.getOperand(2).setImm(0); 12725f757f3fSDimitry Andric MI.getOperand(1).setReg(SrcMI->getOperand(1).getReg()); 12735f757f3fSDimitry Andric LLVM_DEBUG(dbgs() << "To: "); 12745f757f3fSDimitry Andric LLVM_DEBUG(MI.dump()); 12755f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(1).getReg()); 12765f757f3fSDimitry Andric addRegToUpdate(SrcMI->getOperand(0).getReg()); 12775f757f3fSDimitry Andric Simplified = true; 12785f757f3fSDimitry Andric break; 12795f757f3fSDimitry Andric } 1280480093f4SDimitry Andric case PPC::RLWINM: 1281480093f4SDimitry Andric case PPC::RLWINM_rec: 1282480093f4SDimitry Andric case PPC::RLWINM8: 1283480093f4SDimitry Andric case PPC::RLWINM8_rec: { 12845f757f3fSDimitry Andric // We might replace operand 1 of the instruction which will 12855f757f3fSDimitry Andric // require we recompute kill flags for it. 12865f757f3fSDimitry Andric Register OrigOp1Reg = MI.getOperand(1).isReg() 12875f757f3fSDimitry Andric ? MI.getOperand(1).getReg() 12885f757f3fSDimitry Andric : PPC::NoRegister; 1289e8d8bef9SDimitry Andric Simplified = TII->combineRLWINM(MI, &ToErase); 12905f757f3fSDimitry Andric if (Simplified) { 12915f757f3fSDimitry Andric addRegToUpdate(OrigOp1Reg); 12925f757f3fSDimitry Andric if (MI.getOperand(1).isReg()) 12935f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(1).getReg()); 1294*0fca6ea1SDimitry Andric if (ToErase && ToErase->getOperand(1).isReg()) 1295*0fca6ea1SDimitry Andric for (auto UseReg : ToErase->explicit_uses()) 1296*0fca6ea1SDimitry Andric if (UseReg.isReg()) 1297*0fca6ea1SDimitry Andric addRegToUpdate(UseReg.getReg()); 1298480093f4SDimitry Andric ++NumRotatesCollapsed; 12995f757f3fSDimitry Andric } 1300480093f4SDimitry Andric break; 1301480093f4SDimitry Andric } 1302349cc55cSDimitry Andric // We will replace TD/TW/TDI/TWI with an unconditional trap if it will 1303349cc55cSDimitry Andric // always trap, we will delete the node if it will never trap. 1304349cc55cSDimitry Andric case PPC::TDI: 1305349cc55cSDimitry Andric case PPC::TWI: 1306349cc55cSDimitry Andric case PPC::TD: 1307349cc55cSDimitry Andric case PPC::TW: { 1308349cc55cSDimitry Andric if (!EnableTrapOptimization) break; 1309349cc55cSDimitry Andric MachineInstr *LiMI1 = getVRegDefOrNull(&MI.getOperand(1), MRI); 1310349cc55cSDimitry Andric MachineInstr *LiMI2 = getVRegDefOrNull(&MI.getOperand(2), MRI); 1311349cc55cSDimitry Andric bool IsOperand2Immediate = MI.getOperand(2).isImm(); 1312349cc55cSDimitry Andric // We can only do the optimization if we can get immediates 1313349cc55cSDimitry Andric // from both operands 1314349cc55cSDimitry Andric if (!(LiMI1 && (LiMI1->getOpcode() == PPC::LI || 1315349cc55cSDimitry Andric LiMI1->getOpcode() == PPC::LI8))) 1316349cc55cSDimitry Andric break; 1317349cc55cSDimitry Andric if (!IsOperand2Immediate && 1318349cc55cSDimitry Andric !(LiMI2 && (LiMI2->getOpcode() == PPC::LI || 1319349cc55cSDimitry Andric LiMI2->getOpcode() == PPC::LI8))) 1320349cc55cSDimitry Andric break; 1321349cc55cSDimitry Andric 1322349cc55cSDimitry Andric auto ImmOperand0 = MI.getOperand(0).getImm(); 1323349cc55cSDimitry Andric auto ImmOperand1 = LiMI1->getOperand(1).getImm(); 1324349cc55cSDimitry Andric auto ImmOperand2 = IsOperand2Immediate ? MI.getOperand(2).getImm() 1325349cc55cSDimitry Andric : LiMI2->getOperand(1).getImm(); 1326349cc55cSDimitry Andric 1327349cc55cSDimitry Andric // We will replace the MI with an unconditional trap if it will always 1328349cc55cSDimitry Andric // trap. 1329349cc55cSDimitry Andric if ((ImmOperand0 == 31) || 1330349cc55cSDimitry Andric ((ImmOperand0 & 0x10) && 1331349cc55cSDimitry Andric ((int64_t)ImmOperand1 < (int64_t)ImmOperand2)) || 1332349cc55cSDimitry Andric ((ImmOperand0 & 0x8) && 1333349cc55cSDimitry Andric ((int64_t)ImmOperand1 > (int64_t)ImmOperand2)) || 1334349cc55cSDimitry Andric ((ImmOperand0 & 0x2) && 1335349cc55cSDimitry Andric ((uint64_t)ImmOperand1 < (uint64_t)ImmOperand2)) || 1336349cc55cSDimitry Andric ((ImmOperand0 & 0x1) && 1337349cc55cSDimitry Andric ((uint64_t)ImmOperand1 > (uint64_t)ImmOperand2)) || 1338349cc55cSDimitry Andric ((ImmOperand0 & 0x4) && (ImmOperand1 == ImmOperand2))) { 1339349cc55cSDimitry Andric BuildMI(MBB, &MI, MI.getDebugLoc(), TII->get(PPC::TRAP)); 1340349cc55cSDimitry Andric TrapOpt = true; 1341349cc55cSDimitry Andric } 1342349cc55cSDimitry Andric // We will delete the MI if it will never trap. 1343349cc55cSDimitry Andric ToErase = &MI; 1344349cc55cSDimitry Andric Simplified = true; 1345349cc55cSDimitry Andric break; 1346349cc55cSDimitry Andric } 13470b57cec5SDimitry Andric } 13480b57cec5SDimitry Andric } 13490b57cec5SDimitry Andric 13500b57cec5SDimitry Andric // If the last instruction was marked for elimination, 13510b57cec5SDimitry Andric // remove it now. 13520b57cec5SDimitry Andric if (ToErase) { 13535f757f3fSDimitry Andric recomputeLVForDyingInstr(); 13540b57cec5SDimitry Andric ToErase->eraseFromParent(); 13550b57cec5SDimitry Andric ToErase = nullptr; 13560b57cec5SDimitry Andric } 1357349cc55cSDimitry Andric // Reset TrapOpt to false at the end of the basic block. 1358349cc55cSDimitry Andric if (EnableTrapOptimization) 1359349cc55cSDimitry Andric TrapOpt = false; 13600b57cec5SDimitry Andric } 13610b57cec5SDimitry Andric 13620b57cec5SDimitry Andric // Eliminate all the TOC save instructions which are redundant. 13630b57cec5SDimitry Andric Simplified |= eliminateRedundantTOCSaves(TOCSaves); 13640b57cec5SDimitry Andric PPCFunctionInfo *FI = MF->getInfo<PPCFunctionInfo>(); 13650b57cec5SDimitry Andric if (FI->mustSaveTOC()) 13660b57cec5SDimitry Andric NumTOCSavesInPrologue++; 13670b57cec5SDimitry Andric 13680b57cec5SDimitry Andric // We try to eliminate redundant compare instruction. 13690b57cec5SDimitry Andric Simplified |= eliminateRedundantCompare(); 13700b57cec5SDimitry Andric 13715f757f3fSDimitry Andric // If we have made any modifications and added any registers to the set of 13725f757f3fSDimitry Andric // registers for which we need to update the kill flags, do so by recomputing 13735f757f3fSDimitry Andric // LiveVariables for those registers. 13745f757f3fSDimitry Andric for (Register Reg : RegsToUpdate) { 13755f757f3fSDimitry Andric if (!MRI->reg_empty(Reg)) 13765f757f3fSDimitry Andric LV->recomputeForSingleDefVirtReg(Reg); 13775f757f3fSDimitry Andric } 13780b57cec5SDimitry Andric return Simplified; 13790b57cec5SDimitry Andric } 13800b57cec5SDimitry Andric 13810b57cec5SDimitry Andric // helper functions for eliminateRedundantCompare 13820b57cec5SDimitry Andric static bool isEqOrNe(MachineInstr *BI) { 13830b57cec5SDimitry Andric PPC::Predicate Pred = (PPC::Predicate)BI->getOperand(0).getImm(); 13840b57cec5SDimitry Andric unsigned PredCond = PPC::getPredicateCondition(Pred); 13850b57cec5SDimitry Andric return (PredCond == PPC::PRED_EQ || PredCond == PPC::PRED_NE); 13860b57cec5SDimitry Andric } 13870b57cec5SDimitry Andric 13880b57cec5SDimitry Andric static bool isSupportedCmpOp(unsigned opCode) { 13890b57cec5SDimitry Andric return (opCode == PPC::CMPLD || opCode == PPC::CMPD || 13900b57cec5SDimitry Andric opCode == PPC::CMPLW || opCode == PPC::CMPW || 13910b57cec5SDimitry Andric opCode == PPC::CMPLDI || opCode == PPC::CMPDI || 13920b57cec5SDimitry Andric opCode == PPC::CMPLWI || opCode == PPC::CMPWI); 13930b57cec5SDimitry Andric } 13940b57cec5SDimitry Andric 13950b57cec5SDimitry Andric static bool is64bitCmpOp(unsigned opCode) { 13960b57cec5SDimitry Andric return (opCode == PPC::CMPLD || opCode == PPC::CMPD || 13970b57cec5SDimitry Andric opCode == PPC::CMPLDI || opCode == PPC::CMPDI); 13980b57cec5SDimitry Andric } 13990b57cec5SDimitry Andric 14000b57cec5SDimitry Andric static bool isSignedCmpOp(unsigned opCode) { 14010b57cec5SDimitry Andric return (opCode == PPC::CMPD || opCode == PPC::CMPW || 14020b57cec5SDimitry Andric opCode == PPC::CMPDI || opCode == PPC::CMPWI); 14030b57cec5SDimitry Andric } 14040b57cec5SDimitry Andric 14050b57cec5SDimitry Andric static unsigned getSignedCmpOpCode(unsigned opCode) { 14060b57cec5SDimitry Andric if (opCode == PPC::CMPLD) return PPC::CMPD; 14070b57cec5SDimitry Andric if (opCode == PPC::CMPLW) return PPC::CMPW; 14080b57cec5SDimitry Andric if (opCode == PPC::CMPLDI) return PPC::CMPDI; 14090b57cec5SDimitry Andric if (opCode == PPC::CMPLWI) return PPC::CMPWI; 14100b57cec5SDimitry Andric return opCode; 14110b57cec5SDimitry Andric } 14120b57cec5SDimitry Andric 14130b57cec5SDimitry Andric // We can decrement immediate x in (GE x) by changing it to (GT x-1) or 14140b57cec5SDimitry Andric // (LT x) to (LE x-1) 14150b57cec5SDimitry Andric static unsigned getPredicateToDecImm(MachineInstr *BI, MachineInstr *CMPI) { 14160b57cec5SDimitry Andric uint64_t Imm = CMPI->getOperand(2).getImm(); 14170b57cec5SDimitry Andric bool SignedCmp = isSignedCmpOp(CMPI->getOpcode()); 14180b57cec5SDimitry Andric if ((!SignedCmp && Imm == 0) || (SignedCmp && Imm == 0x8000)) 14190b57cec5SDimitry Andric return 0; 14200b57cec5SDimitry Andric 14210b57cec5SDimitry Andric PPC::Predicate Pred = (PPC::Predicate)BI->getOperand(0).getImm(); 14220b57cec5SDimitry Andric unsigned PredCond = PPC::getPredicateCondition(Pred); 14230b57cec5SDimitry Andric unsigned PredHint = PPC::getPredicateHint(Pred); 14240b57cec5SDimitry Andric if (PredCond == PPC::PRED_GE) 14250b57cec5SDimitry Andric return PPC::getPredicate(PPC::PRED_GT, PredHint); 14260b57cec5SDimitry Andric if (PredCond == PPC::PRED_LT) 14270b57cec5SDimitry Andric return PPC::getPredicate(PPC::PRED_LE, PredHint); 14280b57cec5SDimitry Andric 14290b57cec5SDimitry Andric return 0; 14300b57cec5SDimitry Andric } 14310b57cec5SDimitry Andric 14320b57cec5SDimitry Andric // We can increment immediate x in (GT x) by changing it to (GE x+1) or 14330b57cec5SDimitry Andric // (LE x) to (LT x+1) 14340b57cec5SDimitry Andric static unsigned getPredicateToIncImm(MachineInstr *BI, MachineInstr *CMPI) { 14350b57cec5SDimitry Andric uint64_t Imm = CMPI->getOperand(2).getImm(); 14360b57cec5SDimitry Andric bool SignedCmp = isSignedCmpOp(CMPI->getOpcode()); 14370b57cec5SDimitry Andric if ((!SignedCmp && Imm == 0xFFFF) || (SignedCmp && Imm == 0x7FFF)) 14380b57cec5SDimitry Andric return 0; 14390b57cec5SDimitry Andric 14400b57cec5SDimitry Andric PPC::Predicate Pred = (PPC::Predicate)BI->getOperand(0).getImm(); 14410b57cec5SDimitry Andric unsigned PredCond = PPC::getPredicateCondition(Pred); 14420b57cec5SDimitry Andric unsigned PredHint = PPC::getPredicateHint(Pred); 14430b57cec5SDimitry Andric if (PredCond == PPC::PRED_GT) 14440b57cec5SDimitry Andric return PPC::getPredicate(PPC::PRED_GE, PredHint); 14450b57cec5SDimitry Andric if (PredCond == PPC::PRED_LE) 14460b57cec5SDimitry Andric return PPC::getPredicate(PPC::PRED_LT, PredHint); 14470b57cec5SDimitry Andric 14480b57cec5SDimitry Andric return 0; 14490b57cec5SDimitry Andric } 14500b57cec5SDimitry Andric 14510b57cec5SDimitry Andric // This takes a Phi node and returns a register value for the specified BB. 14520b57cec5SDimitry Andric static unsigned getIncomingRegForBlock(MachineInstr *Phi, 14530b57cec5SDimitry Andric MachineBasicBlock *MBB) { 14540b57cec5SDimitry Andric for (unsigned I = 2, E = Phi->getNumOperands() + 1; I != E; I += 2) { 14550b57cec5SDimitry Andric MachineOperand &MO = Phi->getOperand(I); 14560b57cec5SDimitry Andric if (MO.getMBB() == MBB) 14570b57cec5SDimitry Andric return Phi->getOperand(I-1).getReg(); 14580b57cec5SDimitry Andric } 14590b57cec5SDimitry Andric llvm_unreachable("invalid src basic block for this Phi node\n"); 14600b57cec5SDimitry Andric return 0; 14610b57cec5SDimitry Andric } 14620b57cec5SDimitry Andric 14630b57cec5SDimitry Andric // This function tracks the source of the register through register copy. 14640b57cec5SDimitry Andric // If BB1 and BB2 are non-NULL, we also track PHI instruction in BB2 14650b57cec5SDimitry Andric // assuming that the control comes from BB1 into BB2. 14660b57cec5SDimitry Andric static unsigned getSrcVReg(unsigned Reg, MachineBasicBlock *BB1, 14670b57cec5SDimitry Andric MachineBasicBlock *BB2, MachineRegisterInfo *MRI) { 14680b57cec5SDimitry Andric unsigned SrcReg = Reg; 146904eeddc0SDimitry Andric while (true) { 14700b57cec5SDimitry Andric unsigned NextReg = SrcReg; 14710b57cec5SDimitry Andric MachineInstr *Inst = MRI->getVRegDef(SrcReg); 14720b57cec5SDimitry Andric if (BB1 && Inst->getOpcode() == PPC::PHI && Inst->getParent() == BB2) { 14730b57cec5SDimitry Andric NextReg = getIncomingRegForBlock(Inst, BB1); 14740b57cec5SDimitry Andric // We track through PHI only once to avoid infinite loop. 14750b57cec5SDimitry Andric BB1 = nullptr; 14760b57cec5SDimitry Andric } 14770b57cec5SDimitry Andric else if (Inst->isFullCopy()) 14780b57cec5SDimitry Andric NextReg = Inst->getOperand(1).getReg(); 14798bcb0991SDimitry Andric if (NextReg == SrcReg || !Register::isVirtualRegister(NextReg)) 14800b57cec5SDimitry Andric break; 14810b57cec5SDimitry Andric SrcReg = NextReg; 14820b57cec5SDimitry Andric } 14830b57cec5SDimitry Andric return SrcReg; 14840b57cec5SDimitry Andric } 14850b57cec5SDimitry Andric 14860b57cec5SDimitry Andric static bool eligibleForCompareElimination(MachineBasicBlock &MBB, 14870b57cec5SDimitry Andric MachineBasicBlock *&PredMBB, 14880b57cec5SDimitry Andric MachineBasicBlock *&MBBtoMoveCmp, 14890b57cec5SDimitry Andric MachineRegisterInfo *MRI) { 14900b57cec5SDimitry Andric 14910b57cec5SDimitry Andric auto isEligibleBB = [&](MachineBasicBlock &BB) { 14920b57cec5SDimitry Andric auto BII = BB.getFirstInstrTerminator(); 14930b57cec5SDimitry Andric // We optimize BBs ending with a conditional branch. 14940b57cec5SDimitry Andric // We check only for BCC here, not BCCLR, because BCCLR 14950b57cec5SDimitry Andric // will be formed only later in the pipeline. 14960b57cec5SDimitry Andric if (BB.succ_size() == 2 && 14970b57cec5SDimitry Andric BII != BB.instr_end() && 14980b57cec5SDimitry Andric (*BII).getOpcode() == PPC::BCC && 14990b57cec5SDimitry Andric (*BII).getOperand(1).isReg()) { 15000b57cec5SDimitry Andric // We optimize only if the condition code is used only by one BCC. 15018bcb0991SDimitry Andric Register CndReg = (*BII).getOperand(1).getReg(); 1502bdd1243dSDimitry Andric if (!CndReg.isVirtual() || !MRI->hasOneNonDBGUse(CndReg)) 15030b57cec5SDimitry Andric return false; 15040b57cec5SDimitry Andric 15050b57cec5SDimitry Andric MachineInstr *CMPI = MRI->getVRegDef(CndReg); 15060b57cec5SDimitry Andric // We assume compare and branch are in the same BB for ease of analysis. 15070b57cec5SDimitry Andric if (CMPI->getParent() != &BB) 15080b57cec5SDimitry Andric return false; 15090b57cec5SDimitry Andric 15100b57cec5SDimitry Andric // We skip this BB if a physical register is used in comparison. 15110b57cec5SDimitry Andric for (MachineOperand &MO : CMPI->operands()) 1512bdd1243dSDimitry Andric if (MO.isReg() && !MO.getReg().isVirtual()) 15130b57cec5SDimitry Andric return false; 15140b57cec5SDimitry Andric 15150b57cec5SDimitry Andric return true; 15160b57cec5SDimitry Andric } 15170b57cec5SDimitry Andric return false; 15180b57cec5SDimitry Andric }; 15190b57cec5SDimitry Andric 15200b57cec5SDimitry Andric // If this BB has more than one successor, we can create a new BB and 15210b57cec5SDimitry Andric // move the compare instruction in the new BB. 15220b57cec5SDimitry Andric // So far, we do not move compare instruction to a BB having multiple 15230b57cec5SDimitry Andric // successors to avoid potentially increasing code size. 15240b57cec5SDimitry Andric auto isEligibleForMoveCmp = [](MachineBasicBlock &BB) { 15250b57cec5SDimitry Andric return BB.succ_size() == 1; 15260b57cec5SDimitry Andric }; 15270b57cec5SDimitry Andric 15280b57cec5SDimitry Andric if (!isEligibleBB(MBB)) 15290b57cec5SDimitry Andric return false; 15300b57cec5SDimitry Andric 15310b57cec5SDimitry Andric unsigned NumPredBBs = MBB.pred_size(); 15320b57cec5SDimitry Andric if (NumPredBBs == 1) { 15330b57cec5SDimitry Andric MachineBasicBlock *TmpMBB = *MBB.pred_begin(); 15340b57cec5SDimitry Andric if (isEligibleBB(*TmpMBB)) { 15350b57cec5SDimitry Andric PredMBB = TmpMBB; 15360b57cec5SDimitry Andric MBBtoMoveCmp = nullptr; 15370b57cec5SDimitry Andric return true; 15380b57cec5SDimitry Andric } 15390b57cec5SDimitry Andric } 15400b57cec5SDimitry Andric else if (NumPredBBs == 2) { 15410b57cec5SDimitry Andric // We check for partially redundant case. 15420b57cec5SDimitry Andric // So far, we support cases with only two predecessors 15430b57cec5SDimitry Andric // to avoid increasing the number of instructions. 15440b57cec5SDimitry Andric MachineBasicBlock::pred_iterator PI = MBB.pred_begin(); 15450b57cec5SDimitry Andric MachineBasicBlock *Pred1MBB = *PI; 15460b57cec5SDimitry Andric MachineBasicBlock *Pred2MBB = *(PI+1); 15470b57cec5SDimitry Andric 15480b57cec5SDimitry Andric if (isEligibleBB(*Pred1MBB) && isEligibleForMoveCmp(*Pred2MBB)) { 15490b57cec5SDimitry Andric // We assume Pred1MBB is the BB containing the compare to be merged and 15500b57cec5SDimitry Andric // Pred2MBB is the BB to which we will append a compare instruction. 155106c3fb27SDimitry Andric // Proceed as is if Pred1MBB is different from MBB. 15520b57cec5SDimitry Andric } 15530b57cec5SDimitry Andric else if (isEligibleBB(*Pred2MBB) && isEligibleForMoveCmp(*Pred1MBB)) { 15540b57cec5SDimitry Andric // We need to swap Pred1MBB and Pred2MBB to canonicalize. 15550b57cec5SDimitry Andric std::swap(Pred1MBB, Pred2MBB); 15560b57cec5SDimitry Andric } 15570b57cec5SDimitry Andric else return false; 15580b57cec5SDimitry Andric 155906c3fb27SDimitry Andric if (Pred1MBB == &MBB) 156006c3fb27SDimitry Andric return false; 156106c3fb27SDimitry Andric 15620b57cec5SDimitry Andric // Here, Pred2MBB is the BB to which we need to append a compare inst. 15630b57cec5SDimitry Andric // We cannot move the compare instruction if operands are not available 15640b57cec5SDimitry Andric // in Pred2MBB (i.e. defined in MBB by an instruction other than PHI). 15650b57cec5SDimitry Andric MachineInstr *BI = &*MBB.getFirstInstrTerminator(); 15660b57cec5SDimitry Andric MachineInstr *CMPI = MRI->getVRegDef(BI->getOperand(1).getReg()); 15670b57cec5SDimitry Andric for (int I = 1; I <= 2; I++) 15680b57cec5SDimitry Andric if (CMPI->getOperand(I).isReg()) { 15690b57cec5SDimitry Andric MachineInstr *Inst = MRI->getVRegDef(CMPI->getOperand(I).getReg()); 15700b57cec5SDimitry Andric if (Inst->getParent() == &MBB && Inst->getOpcode() != PPC::PHI) 15710b57cec5SDimitry Andric return false; 15720b57cec5SDimitry Andric } 15730b57cec5SDimitry Andric 15740b57cec5SDimitry Andric PredMBB = Pred1MBB; 15750b57cec5SDimitry Andric MBBtoMoveCmp = Pred2MBB; 15760b57cec5SDimitry Andric return true; 15770b57cec5SDimitry Andric } 15780b57cec5SDimitry Andric 15790b57cec5SDimitry Andric return false; 15800b57cec5SDimitry Andric } 15810b57cec5SDimitry Andric 15820b57cec5SDimitry Andric // This function will iterate over the input map containing a pair of TOC save 15830b57cec5SDimitry Andric // instruction and a flag. The flag will be set to false if the TOC save is 15840b57cec5SDimitry Andric // proven redundant. This function will erase from the basic block all the TOC 15850b57cec5SDimitry Andric // saves marked as redundant. 15860b57cec5SDimitry Andric bool PPCMIPeephole::eliminateRedundantTOCSaves( 15870b57cec5SDimitry Andric std::map<MachineInstr *, bool> &TOCSaves) { 15880b57cec5SDimitry Andric bool Simplified = false; 15890b57cec5SDimitry Andric int NumKept = 0; 15900b57cec5SDimitry Andric for (auto TOCSave : TOCSaves) { 15910b57cec5SDimitry Andric if (!TOCSave.second) { 15920b57cec5SDimitry Andric TOCSave.first->eraseFromParent(); 15930b57cec5SDimitry Andric RemoveTOCSave++; 15940b57cec5SDimitry Andric Simplified = true; 15950b57cec5SDimitry Andric } else { 15960b57cec5SDimitry Andric NumKept++; 15970b57cec5SDimitry Andric } 15980b57cec5SDimitry Andric } 15990b57cec5SDimitry Andric 16000b57cec5SDimitry Andric if (NumKept > 1) 16010b57cec5SDimitry Andric MultiTOCSaves++; 16020b57cec5SDimitry Andric 16030b57cec5SDimitry Andric return Simplified; 16040b57cec5SDimitry Andric } 16050b57cec5SDimitry Andric 16060b57cec5SDimitry Andric // If multiple conditional branches are executed based on the (essentially) 16070b57cec5SDimitry Andric // same comparison, we merge compare instructions into one and make multiple 16080b57cec5SDimitry Andric // conditional branches on this comparison. 16090b57cec5SDimitry Andric // For example, 16100b57cec5SDimitry Andric // if (a == 0) { ... } 16110b57cec5SDimitry Andric // else if (a < 0) { ... } 16120b57cec5SDimitry Andric // can be executed by one compare and two conditional branches instead of 16130b57cec5SDimitry Andric // two pairs of a compare and a conditional branch. 16140b57cec5SDimitry Andric // 16150b57cec5SDimitry Andric // This method merges two compare instructions in two MBBs and modifies the 16160b57cec5SDimitry Andric // compare and conditional branch instructions if needed. 16170b57cec5SDimitry Andric // For the above example, the input for this pass looks like: 16180b57cec5SDimitry Andric // cmplwi r3, 0 16190b57cec5SDimitry Andric // beq 0, .LBB0_3 16200b57cec5SDimitry Andric // cmpwi r3, -1 16210b57cec5SDimitry Andric // bgt 0, .LBB0_4 16220b57cec5SDimitry Andric // So, before merging two compares, we need to modify these instructions as 16230b57cec5SDimitry Andric // cmpwi r3, 0 ; cmplwi and cmpwi yield same result for beq 16240b57cec5SDimitry Andric // beq 0, .LBB0_3 16250b57cec5SDimitry Andric // cmpwi r3, 0 ; greather than -1 means greater or equal to 0 16260b57cec5SDimitry Andric // bge 0, .LBB0_4 16270b57cec5SDimitry Andric 162804eeddc0SDimitry Andric bool PPCMIPeephole::eliminateRedundantCompare() { 16290b57cec5SDimitry Andric bool Simplified = false; 16300b57cec5SDimitry Andric 16310b57cec5SDimitry Andric for (MachineBasicBlock &MBB2 : *MF) { 16320b57cec5SDimitry Andric MachineBasicBlock *MBB1 = nullptr, *MBBtoMoveCmp = nullptr; 16330b57cec5SDimitry Andric 16340b57cec5SDimitry Andric // For fully redundant case, we select two basic blocks MBB1 and MBB2 16350b57cec5SDimitry Andric // as an optimization target if 16360b57cec5SDimitry Andric // - both MBBs end with a conditional branch, 16370b57cec5SDimitry Andric // - MBB1 is the only predecessor of MBB2, and 16380b57cec5SDimitry Andric // - compare does not take a physical register as a operand in both MBBs. 16390b57cec5SDimitry Andric // In this case, eligibleForCompareElimination sets MBBtoMoveCmp nullptr. 16400b57cec5SDimitry Andric // 16410b57cec5SDimitry Andric // As partially redundant case, we additionally handle if MBB2 has one 16420b57cec5SDimitry Andric // additional predecessor, which has only one successor (MBB2). 16430b57cec5SDimitry Andric // In this case, we move the compare instruction originally in MBB2 into 16440b57cec5SDimitry Andric // MBBtoMoveCmp. This partially redundant case is typically appear by 16450b57cec5SDimitry Andric // compiling a while loop; here, MBBtoMoveCmp is the loop preheader. 16460b57cec5SDimitry Andric // 16470b57cec5SDimitry Andric // Overview of CFG of related basic blocks 16480b57cec5SDimitry Andric // Fully redundant case Partially redundant case 16490b57cec5SDimitry Andric // -------- ---------------- -------- 16500b57cec5SDimitry Andric // | MBB1 | (w/ 2 succ) | MBBtoMoveCmp | | MBB1 | (w/ 2 succ) 16510b57cec5SDimitry Andric // -------- ---------------- -------- 16520b57cec5SDimitry Andric // | \ (w/ 1 succ) \ | \ 16530b57cec5SDimitry Andric // | \ \ | \ 16540b57cec5SDimitry Andric // | \ | 16550b57cec5SDimitry Andric // -------- -------- 16560b57cec5SDimitry Andric // | MBB2 | (w/ 1 pred | MBB2 | (w/ 2 pred 16570b57cec5SDimitry Andric // -------- and 2 succ) -------- and 2 succ) 16580b57cec5SDimitry Andric // | \ | \ 16590b57cec5SDimitry Andric // | \ | \ 16600b57cec5SDimitry Andric // 16610b57cec5SDimitry Andric if (!eligibleForCompareElimination(MBB2, MBB1, MBBtoMoveCmp, MRI)) 16620b57cec5SDimitry Andric continue; 16630b57cec5SDimitry Andric 16640b57cec5SDimitry Andric MachineInstr *BI1 = &*MBB1->getFirstInstrTerminator(); 16650b57cec5SDimitry Andric MachineInstr *CMPI1 = MRI->getVRegDef(BI1->getOperand(1).getReg()); 16660b57cec5SDimitry Andric 16670b57cec5SDimitry Andric MachineInstr *BI2 = &*MBB2.getFirstInstrTerminator(); 16680b57cec5SDimitry Andric MachineInstr *CMPI2 = MRI->getVRegDef(BI2->getOperand(1).getReg()); 16690b57cec5SDimitry Andric bool IsPartiallyRedundant = (MBBtoMoveCmp != nullptr); 16700b57cec5SDimitry Andric 16710b57cec5SDimitry Andric // We cannot optimize an unsupported compare opcode or 1672bdd1243dSDimitry Andric // a mix of 32-bit and 64-bit comparisons 16730b57cec5SDimitry Andric if (!isSupportedCmpOp(CMPI1->getOpcode()) || 16740b57cec5SDimitry Andric !isSupportedCmpOp(CMPI2->getOpcode()) || 16750b57cec5SDimitry Andric is64bitCmpOp(CMPI1->getOpcode()) != is64bitCmpOp(CMPI2->getOpcode())) 16760b57cec5SDimitry Andric continue; 16770b57cec5SDimitry Andric 16780b57cec5SDimitry Andric unsigned NewOpCode = 0; 16790b57cec5SDimitry Andric unsigned NewPredicate1 = 0, NewPredicate2 = 0; 16800b57cec5SDimitry Andric int16_t Imm1 = 0, NewImm1 = 0, Imm2 = 0, NewImm2 = 0; 16810b57cec5SDimitry Andric bool SwapOperands = false; 16820b57cec5SDimitry Andric 16830b57cec5SDimitry Andric if (CMPI1->getOpcode() != CMPI2->getOpcode()) { 16840b57cec5SDimitry Andric // Typically, unsigned comparison is used for equality check, but 16850b57cec5SDimitry Andric // we replace it with a signed comparison if the comparison 16860b57cec5SDimitry Andric // to be merged is a signed comparison. 16870b57cec5SDimitry Andric // In other cases of opcode mismatch, we cannot optimize this. 16880b57cec5SDimitry Andric 16890b57cec5SDimitry Andric // We cannot change opcode when comparing against an immediate 16900b57cec5SDimitry Andric // if the most significant bit of the immediate is one 16910b57cec5SDimitry Andric // due to the difference in sign extension. 16920b57cec5SDimitry Andric auto CmpAgainstImmWithSignBit = [](MachineInstr *I) { 16930b57cec5SDimitry Andric if (!I->getOperand(2).isImm()) 16940b57cec5SDimitry Andric return false; 16950b57cec5SDimitry Andric int16_t Imm = (int16_t)I->getOperand(2).getImm(); 16960b57cec5SDimitry Andric return Imm < 0; 16970b57cec5SDimitry Andric }; 16980b57cec5SDimitry Andric 16990b57cec5SDimitry Andric if (isEqOrNe(BI2) && !CmpAgainstImmWithSignBit(CMPI2) && 17000b57cec5SDimitry Andric CMPI1->getOpcode() == getSignedCmpOpCode(CMPI2->getOpcode())) 17010b57cec5SDimitry Andric NewOpCode = CMPI1->getOpcode(); 17020b57cec5SDimitry Andric else if (isEqOrNe(BI1) && !CmpAgainstImmWithSignBit(CMPI1) && 17030b57cec5SDimitry Andric getSignedCmpOpCode(CMPI1->getOpcode()) == CMPI2->getOpcode()) 17040b57cec5SDimitry Andric NewOpCode = CMPI2->getOpcode(); 17050b57cec5SDimitry Andric else continue; 17060b57cec5SDimitry Andric } 17070b57cec5SDimitry Andric 17080b57cec5SDimitry Andric if (CMPI1->getOperand(2).isReg() && CMPI2->getOperand(2).isReg()) { 17090b57cec5SDimitry Andric // In case of comparisons between two registers, these two registers 17100b57cec5SDimitry Andric // must be same to merge two comparisons. 17110b57cec5SDimitry Andric unsigned Cmp1Operand1 = getSrcVReg(CMPI1->getOperand(1).getReg(), 17120b57cec5SDimitry Andric nullptr, nullptr, MRI); 17130b57cec5SDimitry Andric unsigned Cmp1Operand2 = getSrcVReg(CMPI1->getOperand(2).getReg(), 17140b57cec5SDimitry Andric nullptr, nullptr, MRI); 17150b57cec5SDimitry Andric unsigned Cmp2Operand1 = getSrcVReg(CMPI2->getOperand(1).getReg(), 17160b57cec5SDimitry Andric MBB1, &MBB2, MRI); 17170b57cec5SDimitry Andric unsigned Cmp2Operand2 = getSrcVReg(CMPI2->getOperand(2).getReg(), 17180b57cec5SDimitry Andric MBB1, &MBB2, MRI); 17190b57cec5SDimitry Andric 17200b57cec5SDimitry Andric if (Cmp1Operand1 == Cmp2Operand1 && Cmp1Operand2 == Cmp2Operand2) { 17210b57cec5SDimitry Andric // Same pair of registers in the same order; ready to merge as is. 17220b57cec5SDimitry Andric } 17230b57cec5SDimitry Andric else if (Cmp1Operand1 == Cmp2Operand2 && Cmp1Operand2 == Cmp2Operand1) { 17240b57cec5SDimitry Andric // Same pair of registers in different order. 17250b57cec5SDimitry Andric // We reverse the predicate to merge compare instructions. 17260b57cec5SDimitry Andric PPC::Predicate Pred = (PPC::Predicate)BI2->getOperand(0).getImm(); 17270b57cec5SDimitry Andric NewPredicate2 = (unsigned)PPC::getSwappedPredicate(Pred); 17280b57cec5SDimitry Andric // In case of partial redundancy, we need to swap operands 17290b57cec5SDimitry Andric // in another compare instruction. 17300b57cec5SDimitry Andric SwapOperands = true; 17310b57cec5SDimitry Andric } 17320b57cec5SDimitry Andric else continue; 17330b57cec5SDimitry Andric } 17340b57cec5SDimitry Andric else if (CMPI1->getOperand(2).isImm() && CMPI2->getOperand(2).isImm()) { 17350b57cec5SDimitry Andric // In case of comparisons between a register and an immediate, 17360b57cec5SDimitry Andric // the operand register must be same for two compare instructions. 17370b57cec5SDimitry Andric unsigned Cmp1Operand1 = getSrcVReg(CMPI1->getOperand(1).getReg(), 17380b57cec5SDimitry Andric nullptr, nullptr, MRI); 17390b57cec5SDimitry Andric unsigned Cmp2Operand1 = getSrcVReg(CMPI2->getOperand(1).getReg(), 17400b57cec5SDimitry Andric MBB1, &MBB2, MRI); 17410b57cec5SDimitry Andric if (Cmp1Operand1 != Cmp2Operand1) 17420b57cec5SDimitry Andric continue; 17430b57cec5SDimitry Andric 17440b57cec5SDimitry Andric NewImm1 = Imm1 = (int16_t)CMPI1->getOperand(2).getImm(); 17450b57cec5SDimitry Andric NewImm2 = Imm2 = (int16_t)CMPI2->getOperand(2).getImm(); 17460b57cec5SDimitry Andric 17470b57cec5SDimitry Andric // If immediate are not same, we try to adjust by changing predicate; 17480b57cec5SDimitry Andric // e.g. GT imm means GE (imm+1). 17490b57cec5SDimitry Andric if (Imm1 != Imm2 && (!isEqOrNe(BI2) || !isEqOrNe(BI1))) { 17500b57cec5SDimitry Andric int Diff = Imm1 - Imm2; 17510b57cec5SDimitry Andric if (Diff < -2 || Diff > 2) 17520b57cec5SDimitry Andric continue; 17530b57cec5SDimitry Andric 17540b57cec5SDimitry Andric unsigned PredToInc1 = getPredicateToIncImm(BI1, CMPI1); 17550b57cec5SDimitry Andric unsigned PredToDec1 = getPredicateToDecImm(BI1, CMPI1); 17560b57cec5SDimitry Andric unsigned PredToInc2 = getPredicateToIncImm(BI2, CMPI2); 17570b57cec5SDimitry Andric unsigned PredToDec2 = getPredicateToDecImm(BI2, CMPI2); 17580b57cec5SDimitry Andric if (Diff == 2) { 17590b57cec5SDimitry Andric if (PredToInc2 && PredToDec1) { 17600b57cec5SDimitry Andric NewPredicate2 = PredToInc2; 17610b57cec5SDimitry Andric NewPredicate1 = PredToDec1; 17620b57cec5SDimitry Andric NewImm2++; 17630b57cec5SDimitry Andric NewImm1--; 17640b57cec5SDimitry Andric } 17650b57cec5SDimitry Andric } 17660b57cec5SDimitry Andric else if (Diff == 1) { 17670b57cec5SDimitry Andric if (PredToInc2) { 17680b57cec5SDimitry Andric NewImm2++; 17690b57cec5SDimitry Andric NewPredicate2 = PredToInc2; 17700b57cec5SDimitry Andric } 17710b57cec5SDimitry Andric else if (PredToDec1) { 17720b57cec5SDimitry Andric NewImm1--; 17730b57cec5SDimitry Andric NewPredicate1 = PredToDec1; 17740b57cec5SDimitry Andric } 17750b57cec5SDimitry Andric } 17760b57cec5SDimitry Andric else if (Diff == -1) { 17770b57cec5SDimitry Andric if (PredToDec2) { 17780b57cec5SDimitry Andric NewImm2--; 17790b57cec5SDimitry Andric NewPredicate2 = PredToDec2; 17800b57cec5SDimitry Andric } 17810b57cec5SDimitry Andric else if (PredToInc1) { 17820b57cec5SDimitry Andric NewImm1++; 17830b57cec5SDimitry Andric NewPredicate1 = PredToInc1; 17840b57cec5SDimitry Andric } 17850b57cec5SDimitry Andric } 17860b57cec5SDimitry Andric else if (Diff == -2) { 17870b57cec5SDimitry Andric if (PredToDec2 && PredToInc1) { 17880b57cec5SDimitry Andric NewPredicate2 = PredToDec2; 17890b57cec5SDimitry Andric NewPredicate1 = PredToInc1; 17900b57cec5SDimitry Andric NewImm2--; 17910b57cec5SDimitry Andric NewImm1++; 17920b57cec5SDimitry Andric } 17930b57cec5SDimitry Andric } 17940b57cec5SDimitry Andric } 17950b57cec5SDimitry Andric 17960b57cec5SDimitry Andric // We cannot merge two compares if the immediates are not same. 17970b57cec5SDimitry Andric if (NewImm2 != NewImm1) 17980b57cec5SDimitry Andric continue; 17990b57cec5SDimitry Andric } 18000b57cec5SDimitry Andric 18010b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Optimize two pairs of compare and branch:\n"); 18020b57cec5SDimitry Andric LLVM_DEBUG(CMPI1->dump()); 18030b57cec5SDimitry Andric LLVM_DEBUG(BI1->dump()); 18040b57cec5SDimitry Andric LLVM_DEBUG(CMPI2->dump()); 18050b57cec5SDimitry Andric LLVM_DEBUG(BI2->dump()); 18065f757f3fSDimitry Andric for (const MachineOperand &MO : CMPI1->operands()) 18075f757f3fSDimitry Andric if (MO.isReg()) 18085f757f3fSDimitry Andric addRegToUpdate(MO.getReg()); 18095f757f3fSDimitry Andric for (const MachineOperand &MO : CMPI2->operands()) 18105f757f3fSDimitry Andric if (MO.isReg()) 18115f757f3fSDimitry Andric addRegToUpdate(MO.getReg()); 18120b57cec5SDimitry Andric 18130b57cec5SDimitry Andric // We adjust opcode, predicates and immediate as we determined above. 18140b57cec5SDimitry Andric if (NewOpCode != 0 && NewOpCode != CMPI1->getOpcode()) { 18150b57cec5SDimitry Andric CMPI1->setDesc(TII->get(NewOpCode)); 18160b57cec5SDimitry Andric } 18170b57cec5SDimitry Andric if (NewPredicate1) { 18180b57cec5SDimitry Andric BI1->getOperand(0).setImm(NewPredicate1); 18190b57cec5SDimitry Andric } 18200b57cec5SDimitry Andric if (NewPredicate2) { 18210b57cec5SDimitry Andric BI2->getOperand(0).setImm(NewPredicate2); 18220b57cec5SDimitry Andric } 18230b57cec5SDimitry Andric if (NewImm1 != Imm1) { 18240b57cec5SDimitry Andric CMPI1->getOperand(2).setImm(NewImm1); 18250b57cec5SDimitry Andric } 18260b57cec5SDimitry Andric 18270b57cec5SDimitry Andric if (IsPartiallyRedundant) { 18280b57cec5SDimitry Andric // We touch up the compare instruction in MBB2 and move it to 18290b57cec5SDimitry Andric // a previous BB to handle partially redundant case. 18300b57cec5SDimitry Andric if (SwapOperands) { 18318bcb0991SDimitry Andric Register Op1 = CMPI2->getOperand(1).getReg(); 18328bcb0991SDimitry Andric Register Op2 = CMPI2->getOperand(2).getReg(); 18330b57cec5SDimitry Andric CMPI2->getOperand(1).setReg(Op2); 18340b57cec5SDimitry Andric CMPI2->getOperand(2).setReg(Op1); 18350b57cec5SDimitry Andric } 18360b57cec5SDimitry Andric if (NewImm2 != Imm2) 18370b57cec5SDimitry Andric CMPI2->getOperand(2).setImm(NewImm2); 18380b57cec5SDimitry Andric 18390b57cec5SDimitry Andric for (int I = 1; I <= 2; I++) { 18400b57cec5SDimitry Andric if (CMPI2->getOperand(I).isReg()) { 18410b57cec5SDimitry Andric MachineInstr *Inst = MRI->getVRegDef(CMPI2->getOperand(I).getReg()); 18420b57cec5SDimitry Andric if (Inst->getParent() != &MBB2) 18430b57cec5SDimitry Andric continue; 18440b57cec5SDimitry Andric 18450b57cec5SDimitry Andric assert(Inst->getOpcode() == PPC::PHI && 18460b57cec5SDimitry Andric "We cannot support if an operand comes from this BB."); 18470b57cec5SDimitry Andric unsigned SrcReg = getIncomingRegForBlock(Inst, MBBtoMoveCmp); 18480b57cec5SDimitry Andric CMPI2->getOperand(I).setReg(SrcReg); 18495f757f3fSDimitry Andric addRegToUpdate(SrcReg); 18500b57cec5SDimitry Andric } 18510b57cec5SDimitry Andric } 18520b57cec5SDimitry Andric auto I = MachineBasicBlock::iterator(MBBtoMoveCmp->getFirstTerminator()); 18530b57cec5SDimitry Andric MBBtoMoveCmp->splice(I, &MBB2, MachineBasicBlock::iterator(CMPI2)); 18540b57cec5SDimitry Andric 18550b57cec5SDimitry Andric DebugLoc DL = CMPI2->getDebugLoc(); 18568bcb0991SDimitry Andric Register NewVReg = MRI->createVirtualRegister(&PPC::CRRCRegClass); 18570b57cec5SDimitry Andric BuildMI(MBB2, MBB2.begin(), DL, 18580b57cec5SDimitry Andric TII->get(PPC::PHI), NewVReg) 18590b57cec5SDimitry Andric .addReg(BI1->getOperand(1).getReg()).addMBB(MBB1) 18600b57cec5SDimitry Andric .addReg(BI2->getOperand(1).getReg()).addMBB(MBBtoMoveCmp); 18610b57cec5SDimitry Andric BI2->getOperand(1).setReg(NewVReg); 18625f757f3fSDimitry Andric addRegToUpdate(NewVReg); 18630b57cec5SDimitry Andric } 18640b57cec5SDimitry Andric else { 18650b57cec5SDimitry Andric // We finally eliminate compare instruction in MBB2. 18665f757f3fSDimitry Andric // We do not need to treat CMPI2 specially here in terms of re-computing 18675f757f3fSDimitry Andric // live variables even though it is being deleted because: 18685f757f3fSDimitry Andric // - It defines a register that has a single use (already checked in 18695f757f3fSDimitry Andric // eligibleForCompareElimination()) 18705f757f3fSDimitry Andric // - The only user (BI2) is no longer using it so the register is dead (no 18715f757f3fSDimitry Andric // def, no uses) 18725f757f3fSDimitry Andric // - We do not attempt to recompute live variables for dead registers 18730b57cec5SDimitry Andric BI2->getOperand(1).setReg(BI1->getOperand(1).getReg()); 18740b57cec5SDimitry Andric CMPI2->eraseFromParent(); 18750b57cec5SDimitry Andric } 18760b57cec5SDimitry Andric 18770b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "into a compare and two branches:\n"); 18780b57cec5SDimitry Andric LLVM_DEBUG(CMPI1->dump()); 18790b57cec5SDimitry Andric LLVM_DEBUG(BI1->dump()); 18800b57cec5SDimitry Andric LLVM_DEBUG(BI2->dump()); 18810b57cec5SDimitry Andric if (IsPartiallyRedundant) { 18820b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "The following compare is moved into " 18830b57cec5SDimitry Andric << printMBBReference(*MBBtoMoveCmp) 18840b57cec5SDimitry Andric << " to handle partial redundancy.\n"); 18850b57cec5SDimitry Andric LLVM_DEBUG(CMPI2->dump()); 18860b57cec5SDimitry Andric } 18870b57cec5SDimitry Andric Simplified = true; 18880b57cec5SDimitry Andric } 18890b57cec5SDimitry Andric 18900b57cec5SDimitry Andric return Simplified; 18910b57cec5SDimitry Andric } 18920b57cec5SDimitry Andric 18930b57cec5SDimitry Andric // We miss the opportunity to emit an RLDIC when lowering jump tables 18940b57cec5SDimitry Andric // since ISEL sees only a single basic block. When selecting, the clear 18950b57cec5SDimitry Andric // and shift left will be in different blocks. 18965f757f3fSDimitry Andric bool PPCMIPeephole::emitRLDICWhenLoweringJumpTables(MachineInstr &MI, 18975f757f3fSDimitry Andric MachineInstr *&ToErase) { 18980b57cec5SDimitry Andric if (MI.getOpcode() != PPC::RLDICR) 18990b57cec5SDimitry Andric return false; 19000b57cec5SDimitry Andric 19018bcb0991SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 1902bdd1243dSDimitry Andric if (!SrcReg.isVirtual()) 19030b57cec5SDimitry Andric return false; 19040b57cec5SDimitry Andric 19050b57cec5SDimitry Andric MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); 19060b57cec5SDimitry Andric if (SrcMI->getOpcode() != PPC::RLDICL) 19070b57cec5SDimitry Andric return false; 19080b57cec5SDimitry Andric 19090b57cec5SDimitry Andric MachineOperand MOpSHSrc = SrcMI->getOperand(2); 19100b57cec5SDimitry Andric MachineOperand MOpMBSrc = SrcMI->getOperand(3); 19110b57cec5SDimitry Andric MachineOperand MOpSHMI = MI.getOperand(2); 19120b57cec5SDimitry Andric MachineOperand MOpMEMI = MI.getOperand(3); 19130b57cec5SDimitry Andric if (!(MOpSHSrc.isImm() && MOpMBSrc.isImm() && MOpSHMI.isImm() && 19140b57cec5SDimitry Andric MOpMEMI.isImm())) 19150b57cec5SDimitry Andric return false; 19160b57cec5SDimitry Andric 19170b57cec5SDimitry Andric uint64_t SHSrc = MOpSHSrc.getImm(); 19180b57cec5SDimitry Andric uint64_t MBSrc = MOpMBSrc.getImm(); 19190b57cec5SDimitry Andric uint64_t SHMI = MOpSHMI.getImm(); 19200b57cec5SDimitry Andric uint64_t MEMI = MOpMEMI.getImm(); 19210b57cec5SDimitry Andric uint64_t NewSH = SHSrc + SHMI; 19220b57cec5SDimitry Andric uint64_t NewMB = MBSrc - SHMI; 19230b57cec5SDimitry Andric if (NewMB > 63 || NewSH > 63) 19240b57cec5SDimitry Andric return false; 19250b57cec5SDimitry Andric 19260b57cec5SDimitry Andric // The bits cleared with RLDICL are [0, MBSrc). 19270b57cec5SDimitry Andric // The bits cleared with RLDICR are (MEMI, 63]. 19280b57cec5SDimitry Andric // After the sequence, the bits cleared are: 19290b57cec5SDimitry Andric // [0, MBSrc-SHMI) and (MEMI, 63). 19300b57cec5SDimitry Andric // 19310b57cec5SDimitry Andric // The bits cleared with RLDIC are [0, NewMB) and (63-NewSH, 63]. 19320b57cec5SDimitry Andric if ((63 - NewSH) != MEMI) 19330b57cec5SDimitry Andric return false; 19340b57cec5SDimitry Andric 19350b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Converting pair: "); 19360b57cec5SDimitry Andric LLVM_DEBUG(SrcMI->dump()); 19370b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 19380b57cec5SDimitry Andric 19390b57cec5SDimitry Andric MI.setDesc(TII->get(PPC::RLDIC)); 19400b57cec5SDimitry Andric MI.getOperand(1).setReg(SrcMI->getOperand(1).getReg()); 19410b57cec5SDimitry Andric MI.getOperand(2).setImm(NewSH); 19420b57cec5SDimitry Andric MI.getOperand(3).setImm(NewMB); 19435f757f3fSDimitry Andric addRegToUpdate(MI.getOperand(1).getReg()); 19445f757f3fSDimitry Andric addRegToUpdate(SrcMI->getOperand(0).getReg()); 19450b57cec5SDimitry Andric 19460b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "To: "); 19470b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 19480b57cec5SDimitry Andric NumRotatesCollapsed++; 19495ffd83dbSDimitry Andric // If SrcReg has no non-debug use it's safe to delete its def SrcMI. 19505ffd83dbSDimitry Andric if (MRI->use_nodbg_empty(SrcReg)) { 19515ffd83dbSDimitry Andric assert(!SrcMI->hasImplicitDef() && 19525ffd83dbSDimitry Andric "Not expecting an implicit def with this instr."); 19535f757f3fSDimitry Andric ToErase = SrcMI; 19545ffd83dbSDimitry Andric } 19550b57cec5SDimitry Andric return true; 19560b57cec5SDimitry Andric } 19570b57cec5SDimitry Andric 19580b57cec5SDimitry Andric // For case in LLVM IR 19590b57cec5SDimitry Andric // entry: 19600b57cec5SDimitry Andric // %iconv = sext i32 %index to i64 19610b57cec5SDimitry Andric // br i1 undef label %true, label %false 19620b57cec5SDimitry Andric // true: 19630b57cec5SDimitry Andric // %ptr = getelementptr inbounds i32, i32* null, i64 %iconv 19640b57cec5SDimitry Andric // ... 19650b57cec5SDimitry Andric // PPCISelLowering::combineSHL fails to combine, because sext and shl are in 19660b57cec5SDimitry Andric // different BBs when conducting instruction selection. We can do a peephole 19670b57cec5SDimitry Andric // optimization to combine these two instructions into extswsli after 19680b57cec5SDimitry Andric // instruction selection. 19690b57cec5SDimitry Andric bool PPCMIPeephole::combineSEXTAndSHL(MachineInstr &MI, 19700b57cec5SDimitry Andric MachineInstr *&ToErase) { 19710b57cec5SDimitry Andric if (MI.getOpcode() != PPC::RLDICR) 19720b57cec5SDimitry Andric return false; 19730b57cec5SDimitry Andric 19740b57cec5SDimitry Andric if (!MF->getSubtarget<PPCSubtarget>().isISA3_0()) 19750b57cec5SDimitry Andric return false; 19760b57cec5SDimitry Andric 19770b57cec5SDimitry Andric assert(MI.getNumOperands() == 4 && "RLDICR should have 4 operands"); 19780b57cec5SDimitry Andric 19790b57cec5SDimitry Andric MachineOperand MOpSHMI = MI.getOperand(2); 19800b57cec5SDimitry Andric MachineOperand MOpMEMI = MI.getOperand(3); 19810b57cec5SDimitry Andric if (!(MOpSHMI.isImm() && MOpMEMI.isImm())) 19820b57cec5SDimitry Andric return false; 19830b57cec5SDimitry Andric 19840b57cec5SDimitry Andric uint64_t SHMI = MOpSHMI.getImm(); 19850b57cec5SDimitry Andric uint64_t MEMI = MOpMEMI.getImm(); 19860b57cec5SDimitry Andric if (SHMI + MEMI != 63) 19870b57cec5SDimitry Andric return false; 19880b57cec5SDimitry Andric 19898bcb0991SDimitry Andric Register SrcReg = MI.getOperand(1).getReg(); 1990bdd1243dSDimitry Andric if (!SrcReg.isVirtual()) 19910b57cec5SDimitry Andric return false; 19920b57cec5SDimitry Andric 19930b57cec5SDimitry Andric MachineInstr *SrcMI = MRI->getVRegDef(SrcReg); 19940b57cec5SDimitry Andric if (SrcMI->getOpcode() != PPC::EXTSW && 19950b57cec5SDimitry Andric SrcMI->getOpcode() != PPC::EXTSW_32_64) 19960b57cec5SDimitry Andric return false; 19970b57cec5SDimitry Andric 19980b57cec5SDimitry Andric // If the register defined by extsw has more than one use, combination is not 19990b57cec5SDimitry Andric // needed. 20000b57cec5SDimitry Andric if (!MRI->hasOneNonDBGUse(SrcReg)) 20010b57cec5SDimitry Andric return false; 20020b57cec5SDimitry Andric 20038bcb0991SDimitry Andric assert(SrcMI->getNumOperands() == 2 && "EXTSW should have 2 operands"); 20048bcb0991SDimitry Andric assert(SrcMI->getOperand(1).isReg() && 20058bcb0991SDimitry Andric "EXTSW's second operand should be a register"); 2006bdd1243dSDimitry Andric if (!SrcMI->getOperand(1).getReg().isVirtual()) 20078bcb0991SDimitry Andric return false; 20088bcb0991SDimitry Andric 20090b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "Combining pair: "); 20100b57cec5SDimitry Andric LLVM_DEBUG(SrcMI->dump()); 20110b57cec5SDimitry Andric LLVM_DEBUG(MI.dump()); 20120b57cec5SDimitry Andric 20130b57cec5SDimitry Andric MachineInstr *NewInstr = 20140b57cec5SDimitry Andric BuildMI(*MI.getParent(), &MI, MI.getDebugLoc(), 20150b57cec5SDimitry Andric SrcMI->getOpcode() == PPC::EXTSW ? TII->get(PPC::EXTSWSLI) 20160b57cec5SDimitry Andric : TII->get(PPC::EXTSWSLI_32_64), 20170b57cec5SDimitry Andric MI.getOperand(0).getReg()) 20180b57cec5SDimitry Andric .add(SrcMI->getOperand(1)) 20190b57cec5SDimitry Andric .add(MOpSHMI); 20200b57cec5SDimitry Andric (void)NewInstr; 20210b57cec5SDimitry Andric 20220b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "TO: "); 20230b57cec5SDimitry Andric LLVM_DEBUG(NewInstr->dump()); 20240b57cec5SDimitry Andric ++NumEXTSWAndSLDICombined; 20250b57cec5SDimitry Andric ToErase = &MI; 20265f757f3fSDimitry Andric // SrcMI, which is extsw, is of no use now, but we don't erase it here so we 20275f757f3fSDimitry Andric // can recompute its kill flags. We run DCE immediately after this pass 20285f757f3fSDimitry Andric // to clean up dead instructions such as this. 20295f757f3fSDimitry Andric addRegToUpdate(NewInstr->getOperand(1).getReg()); 20305f757f3fSDimitry Andric addRegToUpdate(SrcMI->getOperand(0).getReg()); 20310b57cec5SDimitry Andric return true; 20320b57cec5SDimitry Andric } 20330b57cec5SDimitry Andric 20340b57cec5SDimitry Andric } // end default namespace 20350b57cec5SDimitry Andric 20360b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(PPCMIPeephole, DEBUG_TYPE, 20370b57cec5SDimitry Andric "PowerPC MI Peephole Optimization", false, false) 2038*0fca6ea1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineBlockFrequencyInfoWrapperPass) 2039*0fca6ea1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachineDominatorTreeWrapperPass) 2040*0fca6ea1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(MachinePostDominatorTreeWrapperPass) 2041*0fca6ea1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveVariablesWrapperPass) 20420b57cec5SDimitry Andric INITIALIZE_PASS_END(PPCMIPeephole, DEBUG_TYPE, 20430b57cec5SDimitry Andric "PowerPC MI Peephole Optimization", false, false) 20440b57cec5SDimitry Andric 20450b57cec5SDimitry Andric char PPCMIPeephole::ID = 0; 20460b57cec5SDimitry Andric FunctionPass* 20470b57cec5SDimitry Andric llvm::createPPCMIPeepholePass() { return new PPCMIPeephole(); } 2048