10b57cec5SDimitry Andric //===-- SystemZElimCompare.cpp - Eliminate comparison instructions --------===// 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: 100b57cec5SDimitry Andric // (1) tries to remove compares if CC already contains the required information 110b57cec5SDimitry Andric // (2) fuses compares and branches into COMPARE AND BRANCH instructions 120b57cec5SDimitry Andric // 130b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 140b57cec5SDimitry Andric 150b57cec5SDimitry Andric #include "SystemZ.h" 160b57cec5SDimitry Andric #include "SystemZInstrInfo.h" 170b57cec5SDimitry Andric #include "SystemZTargetMachine.h" 180b57cec5SDimitry Andric #include "llvm/ADT/SmallVector.h" 190b57cec5SDimitry Andric #include "llvm/ADT/Statistic.h" 200b57cec5SDimitry Andric #include "llvm/ADT/StringRef.h" 21480093f4SDimitry Andric #include "llvm/CodeGen/LivePhysRegs.h" 220b57cec5SDimitry Andric #include "llvm/CodeGen/MachineBasicBlock.h" 230b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h" 240b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 250b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstr.h" 260b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineOperand.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/TargetRegisterInfo.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/TargetSubtargetInfo.h" 300b57cec5SDimitry Andric #include "llvm/MC/MCInstrDesc.h" 310b57cec5SDimitry Andric #include <cassert> 320b57cec5SDimitry Andric #include <cstdint> 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric using namespace llvm; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric #define DEBUG_TYPE "systemz-elim-compare" 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric STATISTIC(BranchOnCounts, "Number of branch-on-count instructions"); 390b57cec5SDimitry Andric STATISTIC(LoadAndTraps, "Number of load-and-trap instructions"); 400b57cec5SDimitry Andric STATISTIC(EliminatedComparisons, "Number of eliminated comparisons"); 410b57cec5SDimitry Andric STATISTIC(FusedComparisons, "Number of fused compare-and-branch instructions"); 420b57cec5SDimitry Andric 430b57cec5SDimitry Andric namespace { 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric // Represents the references to a particular register in one or more 460b57cec5SDimitry Andric // instructions. 470b57cec5SDimitry Andric struct Reference { 480b57cec5SDimitry Andric Reference() = default; 490b57cec5SDimitry Andric 500b57cec5SDimitry Andric Reference &operator|=(const Reference &Other) { 510b57cec5SDimitry Andric Def |= Other.Def; 520b57cec5SDimitry Andric Use |= Other.Use; 530b57cec5SDimitry Andric return *this; 540b57cec5SDimitry Andric } 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric explicit operator bool() const { return Def || Use; } 570b57cec5SDimitry Andric 580b57cec5SDimitry Andric // True if the register is defined or used in some form, either directly or 590b57cec5SDimitry Andric // via a sub- or super-register. 600b57cec5SDimitry Andric bool Def = false; 610b57cec5SDimitry Andric bool Use = false; 620b57cec5SDimitry Andric }; 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric class SystemZElimCompare : public MachineFunctionPass { 650b57cec5SDimitry Andric public: 660b57cec5SDimitry Andric static char ID; 670b57cec5SDimitry Andric 68*04eeddc0SDimitry Andric SystemZElimCompare() : MachineFunctionPass(ID) { 69*04eeddc0SDimitry Andric initializeSystemZElimComparePass(*PassRegistry::getPassRegistry()); 700b57cec5SDimitry Andric } 710b57cec5SDimitry Andric 720b57cec5SDimitry Andric bool processBlock(MachineBasicBlock &MBB); 730b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &F) override; 740b57cec5SDimitry Andric 750b57cec5SDimitry Andric MachineFunctionProperties getRequiredProperties() const override { 760b57cec5SDimitry Andric return MachineFunctionProperties().set( 770b57cec5SDimitry Andric MachineFunctionProperties::Property::NoVRegs); 780b57cec5SDimitry Andric } 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric private: 810b57cec5SDimitry Andric Reference getRegReferences(MachineInstr &MI, unsigned Reg); 820b57cec5SDimitry Andric bool convertToBRCT(MachineInstr &MI, MachineInstr &Compare, 830b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers); 840b57cec5SDimitry Andric bool convertToLoadAndTrap(MachineInstr &MI, MachineInstr &Compare, 850b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers); 860b57cec5SDimitry Andric bool convertToLoadAndTest(MachineInstr &MI, MachineInstr &Compare, 870b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers); 88480093f4SDimitry Andric bool convertToLogical(MachineInstr &MI, MachineInstr &Compare, 89480093f4SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers); 900b57cec5SDimitry Andric bool adjustCCMasksForInstr(MachineInstr &MI, MachineInstr &Compare, 910b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers, 920b57cec5SDimitry Andric unsigned ConvOpc = 0); 930b57cec5SDimitry Andric bool optimizeCompareZero(MachineInstr &Compare, 940b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers); 950b57cec5SDimitry Andric bool fuseCompareOperations(MachineInstr &Compare, 960b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers); 970b57cec5SDimitry Andric 980b57cec5SDimitry Andric const SystemZInstrInfo *TII = nullptr; 990b57cec5SDimitry Andric const TargetRegisterInfo *TRI = nullptr; 1000b57cec5SDimitry Andric }; 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric char SystemZElimCompare::ID = 0; 1030b57cec5SDimitry Andric 1040b57cec5SDimitry Andric } // end anonymous namespace 1050b57cec5SDimitry Andric 106*04eeddc0SDimitry Andric INITIALIZE_PASS(SystemZElimCompare, DEBUG_TYPE, 107*04eeddc0SDimitry Andric "SystemZ Comparison Elimination", false, false) 108*04eeddc0SDimitry Andric 1090b57cec5SDimitry Andric // Returns true if MI is an instruction whose output equals the value in Reg. 1100b57cec5SDimitry Andric static bool preservesValueOf(MachineInstr &MI, unsigned Reg) { 1110b57cec5SDimitry Andric switch (MI.getOpcode()) { 1120b57cec5SDimitry Andric case SystemZ::LR: 1130b57cec5SDimitry Andric case SystemZ::LGR: 1140b57cec5SDimitry Andric case SystemZ::LGFR: 1150b57cec5SDimitry Andric case SystemZ::LTR: 1160b57cec5SDimitry Andric case SystemZ::LTGR: 1170b57cec5SDimitry Andric case SystemZ::LTGFR: 1180b57cec5SDimitry Andric case SystemZ::LER: 1190b57cec5SDimitry Andric case SystemZ::LDR: 1200b57cec5SDimitry Andric case SystemZ::LXR: 1210b57cec5SDimitry Andric case SystemZ::LTEBR: 1220b57cec5SDimitry Andric case SystemZ::LTDBR: 1230b57cec5SDimitry Andric case SystemZ::LTXBR: 1240b57cec5SDimitry Andric if (MI.getOperand(1).getReg() == Reg) 1250b57cec5SDimitry Andric return true; 1260b57cec5SDimitry Andric } 1270b57cec5SDimitry Andric 1280b57cec5SDimitry Andric return false; 1290b57cec5SDimitry Andric } 1300b57cec5SDimitry Andric 1310b57cec5SDimitry Andric // Return true if any CC result of MI would (perhaps after conversion) 1320b57cec5SDimitry Andric // reflect the value of Reg. 1330b57cec5SDimitry Andric static bool resultTests(MachineInstr &MI, unsigned Reg) { 1340b57cec5SDimitry Andric if (MI.getNumOperands() > 0 && MI.getOperand(0).isReg() && 1350b57cec5SDimitry Andric MI.getOperand(0).isDef() && MI.getOperand(0).getReg() == Reg) 1360b57cec5SDimitry Andric return true; 1370b57cec5SDimitry Andric 1380b57cec5SDimitry Andric return (preservesValueOf(MI, Reg)); 1390b57cec5SDimitry Andric } 1400b57cec5SDimitry Andric 1410b57cec5SDimitry Andric // Describe the references to Reg or any of its aliases in MI. 1420b57cec5SDimitry Andric Reference SystemZElimCompare::getRegReferences(MachineInstr &MI, unsigned Reg) { 1430b57cec5SDimitry Andric Reference Ref; 1440b57cec5SDimitry Andric if (MI.isDebugInstr()) 1450b57cec5SDimitry Andric return Ref; 1460b57cec5SDimitry Andric 1474824e7fdSDimitry Andric for (const MachineOperand &MO : MI.operands()) { 1480b57cec5SDimitry Andric if (MO.isReg()) { 1498bcb0991SDimitry Andric if (Register MOReg = MO.getReg()) { 1500b57cec5SDimitry Andric if (TRI->regsOverlap(MOReg, Reg)) { 1510b57cec5SDimitry Andric if (MO.isUse()) 1520b57cec5SDimitry Andric Ref.Use = true; 1530b57cec5SDimitry Andric else if (MO.isDef()) 1540b57cec5SDimitry Andric Ref.Def = true; 1550b57cec5SDimitry Andric } 1560b57cec5SDimitry Andric } 1570b57cec5SDimitry Andric } 1580b57cec5SDimitry Andric } 1590b57cec5SDimitry Andric return Ref; 1600b57cec5SDimitry Andric } 1610b57cec5SDimitry Andric 1620b57cec5SDimitry Andric // Return true if this is a load and test which can be optimized the 1630b57cec5SDimitry Andric // same way as compare instruction. 1640b57cec5SDimitry Andric static bool isLoadAndTestAsCmp(MachineInstr &MI) { 1650b57cec5SDimitry Andric // If we during isel used a load-and-test as a compare with 0, the 1660b57cec5SDimitry Andric // def operand is dead. 1670b57cec5SDimitry Andric return (MI.getOpcode() == SystemZ::LTEBR || 1680b57cec5SDimitry Andric MI.getOpcode() == SystemZ::LTDBR || 1690b57cec5SDimitry Andric MI.getOpcode() == SystemZ::LTXBR) && 1700b57cec5SDimitry Andric MI.getOperand(0).isDead(); 1710b57cec5SDimitry Andric } 1720b57cec5SDimitry Andric 1730b57cec5SDimitry Andric // Return the source register of Compare, which is the unknown value 1740b57cec5SDimitry Andric // being tested. 1750b57cec5SDimitry Andric static unsigned getCompareSourceReg(MachineInstr &Compare) { 1760b57cec5SDimitry Andric unsigned reg = 0; 1770b57cec5SDimitry Andric if (Compare.isCompare()) 1780b57cec5SDimitry Andric reg = Compare.getOperand(0).getReg(); 1790b57cec5SDimitry Andric else if (isLoadAndTestAsCmp(Compare)) 1800b57cec5SDimitry Andric reg = Compare.getOperand(1).getReg(); 1810b57cec5SDimitry Andric assert(reg); 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric return reg; 1840b57cec5SDimitry Andric } 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric // Compare compares the result of MI against zero. If MI is an addition 1870b57cec5SDimitry Andric // of -1 and if CCUsers is a single branch on nonzero, eliminate the addition 1880b57cec5SDimitry Andric // and convert the branch to a BRCT(G) or BRCTH. Return true on success. 1890b57cec5SDimitry Andric bool SystemZElimCompare::convertToBRCT( 1900b57cec5SDimitry Andric MachineInstr &MI, MachineInstr &Compare, 1910b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers) { 1920b57cec5SDimitry Andric // Check whether we have an addition of -1. 1930b57cec5SDimitry Andric unsigned Opcode = MI.getOpcode(); 1940b57cec5SDimitry Andric unsigned BRCT; 1950b57cec5SDimitry Andric if (Opcode == SystemZ::AHI) 1960b57cec5SDimitry Andric BRCT = SystemZ::BRCT; 1970b57cec5SDimitry Andric else if (Opcode == SystemZ::AGHI) 1980b57cec5SDimitry Andric BRCT = SystemZ::BRCTG; 1990b57cec5SDimitry Andric else if (Opcode == SystemZ::AIH) 2000b57cec5SDimitry Andric BRCT = SystemZ::BRCTH; 2010b57cec5SDimitry Andric else 2020b57cec5SDimitry Andric return false; 2030b57cec5SDimitry Andric if (MI.getOperand(2).getImm() != -1) 2040b57cec5SDimitry Andric return false; 2050b57cec5SDimitry Andric 2060b57cec5SDimitry Andric // Check whether we have a single JLH. 2070b57cec5SDimitry Andric if (CCUsers.size() != 1) 2080b57cec5SDimitry Andric return false; 2090b57cec5SDimitry Andric MachineInstr *Branch = CCUsers[0]; 2100b57cec5SDimitry Andric if (Branch->getOpcode() != SystemZ::BRC || 2110b57cec5SDimitry Andric Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP || 2120b57cec5SDimitry Andric Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_NE) 2130b57cec5SDimitry Andric return false; 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric // We already know that there are no references to the register between 2160b57cec5SDimitry Andric // MI and Compare. Make sure that there are also no references between 2170b57cec5SDimitry Andric // Compare and Branch. 2180b57cec5SDimitry Andric unsigned SrcReg = getCompareSourceReg(Compare); 2190b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; 2200b57cec5SDimitry Andric for (++MBBI; MBBI != MBBE; ++MBBI) 2210b57cec5SDimitry Andric if (getRegReferences(*MBBI, SrcReg)) 2220b57cec5SDimitry Andric return false; 2230b57cec5SDimitry Andric 2240b57cec5SDimitry Andric // The transformation is OK. Rebuild Branch as a BRCT(G) or BRCTH. 2250b57cec5SDimitry Andric MachineOperand Target(Branch->getOperand(2)); 2260b57cec5SDimitry Andric while (Branch->getNumOperands()) 2270b57cec5SDimitry Andric Branch->RemoveOperand(0); 2280b57cec5SDimitry Andric Branch->setDesc(TII->get(BRCT)); 2290b57cec5SDimitry Andric MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch); 2300b57cec5SDimitry Andric MIB.add(MI.getOperand(0)).add(MI.getOperand(1)).add(Target); 2310b57cec5SDimitry Andric // Add a CC def to BRCT(G), since we may have to split them again if the 2320b57cec5SDimitry Andric // branch displacement overflows. BRCTH has a 32-bit displacement, so 2330b57cec5SDimitry Andric // this is not necessary there. 2340b57cec5SDimitry Andric if (BRCT != SystemZ::BRCTH) 2350b57cec5SDimitry Andric MIB.addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead); 2360b57cec5SDimitry Andric MI.eraseFromParent(); 2370b57cec5SDimitry Andric return true; 2380b57cec5SDimitry Andric } 2390b57cec5SDimitry Andric 2400b57cec5SDimitry Andric // Compare compares the result of MI against zero. If MI is a suitable load 2410b57cec5SDimitry Andric // instruction and if CCUsers is a single conditional trap on zero, eliminate 2420b57cec5SDimitry Andric // the load and convert the branch to a load-and-trap. Return true on success. 2430b57cec5SDimitry Andric bool SystemZElimCompare::convertToLoadAndTrap( 2440b57cec5SDimitry Andric MachineInstr &MI, MachineInstr &Compare, 2450b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers) { 2460b57cec5SDimitry Andric unsigned LATOpcode = TII->getLoadAndTrap(MI.getOpcode()); 2470b57cec5SDimitry Andric if (!LATOpcode) 2480b57cec5SDimitry Andric return false; 2490b57cec5SDimitry Andric 2500b57cec5SDimitry Andric // Check whether we have a single CondTrap that traps on zero. 2510b57cec5SDimitry Andric if (CCUsers.size() != 1) 2520b57cec5SDimitry Andric return false; 2530b57cec5SDimitry Andric MachineInstr *Branch = CCUsers[0]; 2540b57cec5SDimitry Andric if (Branch->getOpcode() != SystemZ::CondTrap || 2550b57cec5SDimitry Andric Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP || 2560b57cec5SDimitry Andric Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_EQ) 2570b57cec5SDimitry Andric return false; 2580b57cec5SDimitry Andric 2590b57cec5SDimitry Andric // We already know that there are no references to the register between 2600b57cec5SDimitry Andric // MI and Compare. Make sure that there are also no references between 2610b57cec5SDimitry Andric // Compare and Branch. 2620b57cec5SDimitry Andric unsigned SrcReg = getCompareSourceReg(Compare); 2630b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; 2640b57cec5SDimitry Andric for (++MBBI; MBBI != MBBE; ++MBBI) 2650b57cec5SDimitry Andric if (getRegReferences(*MBBI, SrcReg)) 2660b57cec5SDimitry Andric return false; 2670b57cec5SDimitry Andric 2680b57cec5SDimitry Andric // The transformation is OK. Rebuild Branch as a load-and-trap. 2690b57cec5SDimitry Andric while (Branch->getNumOperands()) 2700b57cec5SDimitry Andric Branch->RemoveOperand(0); 2710b57cec5SDimitry Andric Branch->setDesc(TII->get(LATOpcode)); 2720b57cec5SDimitry Andric MachineInstrBuilder(*Branch->getParent()->getParent(), Branch) 2730b57cec5SDimitry Andric .add(MI.getOperand(0)) 2740b57cec5SDimitry Andric .add(MI.getOperand(1)) 2750b57cec5SDimitry Andric .add(MI.getOperand(2)) 2760b57cec5SDimitry Andric .add(MI.getOperand(3)); 2770b57cec5SDimitry Andric MI.eraseFromParent(); 2780b57cec5SDimitry Andric return true; 2790b57cec5SDimitry Andric } 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric // If MI is a load instruction, try to convert it into a LOAD AND TEST. 2820b57cec5SDimitry Andric // Return true on success. 2830b57cec5SDimitry Andric bool SystemZElimCompare::convertToLoadAndTest( 2840b57cec5SDimitry Andric MachineInstr &MI, MachineInstr &Compare, 2850b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers) { 2860b57cec5SDimitry Andric 2870b57cec5SDimitry Andric // Try to adjust CC masks for the LOAD AND TEST opcode that could replace MI. 2880b57cec5SDimitry Andric unsigned Opcode = TII->getLoadAndTest(MI.getOpcode()); 2890b57cec5SDimitry Andric if (!Opcode || !adjustCCMasksForInstr(MI, Compare, CCUsers, Opcode)) 2900b57cec5SDimitry Andric return false; 2910b57cec5SDimitry Andric 2920b57cec5SDimitry Andric // Rebuild to get the CC operand in the right place. 2930b57cec5SDimitry Andric auto MIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(Opcode)); 2940b57cec5SDimitry Andric for (const auto &MO : MI.operands()) 2950b57cec5SDimitry Andric MIB.add(MO); 2960b57cec5SDimitry Andric MIB.setMemRefs(MI.memoperands()); 2970b57cec5SDimitry Andric MI.eraseFromParent(); 2980b57cec5SDimitry Andric 299480093f4SDimitry Andric // Mark instruction as not raising an FP exception if applicable. We already 300480093f4SDimitry Andric // verified earlier that this move is valid. 301480093f4SDimitry Andric if (!Compare.mayRaiseFPException()) 302480093f4SDimitry Andric MIB.setMIFlag(MachineInstr::MIFlag::NoFPExcept); 303480093f4SDimitry Andric 3040b57cec5SDimitry Andric return true; 3050b57cec5SDimitry Andric } 3060b57cec5SDimitry Andric 307480093f4SDimitry Andric // See if MI is an instruction with an equivalent "logical" opcode that can 308480093f4SDimitry Andric // be used and replace MI. This is useful for EQ/NE comparisons where the 309480093f4SDimitry Andric // "nsw" flag is missing since the "logical" opcode always sets CC to reflect 310480093f4SDimitry Andric // the result being zero or non-zero. 311480093f4SDimitry Andric bool SystemZElimCompare::convertToLogical( 312480093f4SDimitry Andric MachineInstr &MI, MachineInstr &Compare, 313480093f4SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers) { 314480093f4SDimitry Andric 315480093f4SDimitry Andric unsigned ConvOpc = 0; 316480093f4SDimitry Andric switch (MI.getOpcode()) { 317480093f4SDimitry Andric case SystemZ::AR: ConvOpc = SystemZ::ALR; break; 318480093f4SDimitry Andric case SystemZ::ARK: ConvOpc = SystemZ::ALRK; break; 319480093f4SDimitry Andric case SystemZ::AGR: ConvOpc = SystemZ::ALGR; break; 320480093f4SDimitry Andric case SystemZ::AGRK: ConvOpc = SystemZ::ALGRK; break; 321480093f4SDimitry Andric case SystemZ::A: ConvOpc = SystemZ::AL; break; 322480093f4SDimitry Andric case SystemZ::AY: ConvOpc = SystemZ::ALY; break; 323480093f4SDimitry Andric case SystemZ::AG: ConvOpc = SystemZ::ALG; break; 324480093f4SDimitry Andric default: break; 325480093f4SDimitry Andric } 326480093f4SDimitry Andric if (!ConvOpc || !adjustCCMasksForInstr(MI, Compare, CCUsers, ConvOpc)) 327480093f4SDimitry Andric return false; 328480093f4SDimitry Andric 329480093f4SDimitry Andric // Operands should be identical, so just change the opcode and remove the 330480093f4SDimitry Andric // dead flag on CC. 331480093f4SDimitry Andric MI.setDesc(TII->get(ConvOpc)); 332480093f4SDimitry Andric MI.clearRegisterDeads(SystemZ::CC); 333480093f4SDimitry Andric return true; 334480093f4SDimitry Andric } 335480093f4SDimitry Andric 336480093f4SDimitry Andric #ifndef NDEBUG 337480093f4SDimitry Andric static bool isAddWithImmediate(unsigned Opcode) { 338480093f4SDimitry Andric switch(Opcode) { 339480093f4SDimitry Andric case SystemZ::AHI: 340480093f4SDimitry Andric case SystemZ::AHIK: 341480093f4SDimitry Andric case SystemZ::AGHI: 342480093f4SDimitry Andric case SystemZ::AGHIK: 343480093f4SDimitry Andric case SystemZ::AFI: 344480093f4SDimitry Andric case SystemZ::AIH: 345480093f4SDimitry Andric case SystemZ::AGFI: 346480093f4SDimitry Andric return true; 347480093f4SDimitry Andric default: break; 348480093f4SDimitry Andric } 349480093f4SDimitry Andric return false; 350480093f4SDimitry Andric } 351480093f4SDimitry Andric #endif 352480093f4SDimitry Andric 3530b57cec5SDimitry Andric // The CC users in CCUsers are testing the result of a comparison of some 3540b57cec5SDimitry Andric // value X against zero and we know that any CC value produced by MI would 3550b57cec5SDimitry Andric // also reflect the value of X. ConvOpc may be used to pass the transfomed 3560b57cec5SDimitry Andric // opcode MI will have if this succeeds. Try to adjust CCUsers so that they 3570b57cec5SDimitry Andric // test the result of MI directly, returning true on success. Leave 3580b57cec5SDimitry Andric // everything unchanged on failure. 3590b57cec5SDimitry Andric bool SystemZElimCompare::adjustCCMasksForInstr( 3600b57cec5SDimitry Andric MachineInstr &MI, MachineInstr &Compare, 3610b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers, 3620b57cec5SDimitry Andric unsigned ConvOpc) { 363480093f4SDimitry Andric unsigned CompareFlags = Compare.getDesc().TSFlags; 364480093f4SDimitry Andric unsigned CompareCCValues = SystemZII::getCCValues(CompareFlags); 3650b57cec5SDimitry Andric int Opcode = (ConvOpc ? ConvOpc : MI.getOpcode()); 3660b57cec5SDimitry Andric const MCInstrDesc &Desc = TII->get(Opcode); 3670b57cec5SDimitry Andric unsigned MIFlags = Desc.TSFlags; 3680b57cec5SDimitry Andric 369480093f4SDimitry Andric // If Compare may raise an FP exception, we can only eliminate it 370480093f4SDimitry Andric // if MI itself would have already raised the exception. 371480093f4SDimitry Andric if (Compare.mayRaiseFPException()) { 372480093f4SDimitry Andric // If the caller will change MI to use ConvOpc, only test whether 373480093f4SDimitry Andric // ConvOpc is suitable; it is on the caller to set the MI flag. 374480093f4SDimitry Andric if (ConvOpc && !Desc.mayRaiseFPException()) 375480093f4SDimitry Andric return false; 376480093f4SDimitry Andric // If the caller will not change MI, we test the MI flag here. 377480093f4SDimitry Andric if (!ConvOpc && !MI.mayRaiseFPException()) 378480093f4SDimitry Andric return false; 379480093f4SDimitry Andric } 3800b57cec5SDimitry Andric 381480093f4SDimitry Andric // See which compare-style condition codes are available. 382480093f4SDimitry Andric unsigned CCValues = SystemZII::getCCValues(MIFlags); 383480093f4SDimitry Andric unsigned ReusableCCMask = CCValues; 3840b57cec5SDimitry Andric // For unsigned comparisons with zero, only equality makes sense. 3850b57cec5SDimitry Andric if (CompareFlags & SystemZII::IsLogical) 3860b57cec5SDimitry Andric ReusableCCMask &= SystemZ::CCMASK_CMP_EQ; 387480093f4SDimitry Andric unsigned OFImplies = 0; 388480093f4SDimitry Andric bool LogicalMI = false; 389480093f4SDimitry Andric bool MIEquivalentToCmp = false; 390480093f4SDimitry Andric if (MI.getFlag(MachineInstr::NoSWrap) && 391480093f4SDimitry Andric (MIFlags & SystemZII::CCIfNoSignedWrap)) { 392480093f4SDimitry Andric // If MI has the NSW flag set in combination with the 393480093f4SDimitry Andric // SystemZII::CCIfNoSignedWrap flag, all CCValues are valid. 394480093f4SDimitry Andric } 395480093f4SDimitry Andric else if ((MIFlags & SystemZII::CCIfNoSignedWrap) && 396480093f4SDimitry Andric MI.getOperand(2).isImm()) { 397480093f4SDimitry Andric // Signed addition of immediate. If adding a positive immediate 398480093f4SDimitry Andric // overflows, the result must be less than zero. If adding a negative 399480093f4SDimitry Andric // immediate overflows, the result must be larger than zero (except in 400480093f4SDimitry Andric // the special case of adding the minimum value of the result range, in 401480093f4SDimitry Andric // which case we cannot predict whether the result is larger than or 402480093f4SDimitry Andric // equal to zero). 403480093f4SDimitry Andric assert(isAddWithImmediate(Opcode) && "Expected an add with immediate."); 404480093f4SDimitry Andric assert(!MI.mayLoadOrStore() && "Expected an immediate term."); 405480093f4SDimitry Andric int64_t RHS = MI.getOperand(2).getImm(); 406480093f4SDimitry Andric if (SystemZ::GRX32BitRegClass.contains(MI.getOperand(0).getReg()) && 407480093f4SDimitry Andric RHS == INT32_MIN) 408480093f4SDimitry Andric return false; 409480093f4SDimitry Andric OFImplies = (RHS > 0 ? SystemZ::CCMASK_CMP_LT : SystemZ::CCMASK_CMP_GT); 410480093f4SDimitry Andric } 411480093f4SDimitry Andric else if ((MIFlags & SystemZII::IsLogical) && CCValues) { 412480093f4SDimitry Andric // Use CCMASK_CMP_EQ to match with CCUsers. On success CCMask:s will be 413480093f4SDimitry Andric // converted to CCMASK_LOGICAL_ZERO or CCMASK_LOGICAL_NONZERO. 414480093f4SDimitry Andric LogicalMI = true; 415480093f4SDimitry Andric ReusableCCMask = SystemZ::CCMASK_CMP_EQ; 416480093f4SDimitry Andric } 417480093f4SDimitry Andric else { 418480093f4SDimitry Andric ReusableCCMask &= SystemZII::getCompareZeroCCMask(MIFlags); 419480093f4SDimitry Andric assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues"); 420480093f4SDimitry Andric MIEquivalentToCmp = 421480093f4SDimitry Andric ReusableCCMask == CCValues && CCValues == CompareCCValues; 422480093f4SDimitry Andric } 4230b57cec5SDimitry Andric if (ReusableCCMask == 0) 4240b57cec5SDimitry Andric return false; 4250b57cec5SDimitry Andric 4260b57cec5SDimitry Andric if (!MIEquivalentToCmp) { 4270b57cec5SDimitry Andric // Now check whether these flags are enough for all users. 4280b57cec5SDimitry Andric SmallVector<MachineOperand *, 4> AlterMasks; 4290b57cec5SDimitry Andric for (unsigned int I = 0, E = CCUsers.size(); I != E; ++I) { 430480093f4SDimitry Andric MachineInstr *CCUserMI = CCUsers[I]; 4310b57cec5SDimitry Andric 4320b57cec5SDimitry Andric // Fail if this isn't a use of CC that we understand. 433480093f4SDimitry Andric unsigned Flags = CCUserMI->getDesc().TSFlags; 4340b57cec5SDimitry Andric unsigned FirstOpNum; 4350b57cec5SDimitry Andric if (Flags & SystemZII::CCMaskFirst) 4360b57cec5SDimitry Andric FirstOpNum = 0; 4370b57cec5SDimitry Andric else if (Flags & SystemZII::CCMaskLast) 438480093f4SDimitry Andric FirstOpNum = CCUserMI->getNumExplicitOperands() - 2; 4390b57cec5SDimitry Andric else 4400b57cec5SDimitry Andric return false; 4410b57cec5SDimitry Andric 4420b57cec5SDimitry Andric // Check whether the instruction predicate treats all CC values 4430b57cec5SDimitry Andric // outside of ReusableCCMask in the same way. In that case it 4440b57cec5SDimitry Andric // doesn't matter what those CC values mean. 445480093f4SDimitry Andric unsigned CCValid = CCUserMI->getOperand(FirstOpNum).getImm(); 446480093f4SDimitry Andric unsigned CCMask = CCUserMI->getOperand(FirstOpNum + 1).getImm(); 447480093f4SDimitry Andric assert(CCValid == CompareCCValues && (CCMask & ~CCValid) == 0 && 448480093f4SDimitry Andric "Corrupt CC operands of CCUser."); 4490b57cec5SDimitry Andric unsigned OutValid = ~ReusableCCMask & CCValid; 4500b57cec5SDimitry Andric unsigned OutMask = ~ReusableCCMask & CCMask; 4510b57cec5SDimitry Andric if (OutMask != 0 && OutMask != OutValid) 4520b57cec5SDimitry Andric return false; 4530b57cec5SDimitry Andric 454480093f4SDimitry Andric AlterMasks.push_back(&CCUserMI->getOperand(FirstOpNum)); 455480093f4SDimitry Andric AlterMasks.push_back(&CCUserMI->getOperand(FirstOpNum + 1)); 4560b57cec5SDimitry Andric } 4570b57cec5SDimitry Andric 4580b57cec5SDimitry Andric // All users are OK. Adjust the masks for MI. 4590b57cec5SDimitry Andric for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) { 4600b57cec5SDimitry Andric AlterMasks[I]->setImm(CCValues); 4610b57cec5SDimitry Andric unsigned CCMask = AlterMasks[I + 1]->getImm(); 462480093f4SDimitry Andric if (LogicalMI) { 463480093f4SDimitry Andric // Translate the CCMask into its "logical" value. 464480093f4SDimitry Andric CCMask = (CCMask == SystemZ::CCMASK_CMP_EQ ? 465480093f4SDimitry Andric SystemZ::CCMASK_LOGICAL_ZERO : SystemZ::CCMASK_LOGICAL_NONZERO); 466480093f4SDimitry Andric CCMask &= CCValues; // Logical subtracts never set CC=0. 467480093f4SDimitry Andric } else { 4680b57cec5SDimitry Andric if (CCMask & ~ReusableCCMask) 469480093f4SDimitry Andric CCMask = (CCMask & ReusableCCMask) | (CCValues & ~ReusableCCMask); 470480093f4SDimitry Andric CCMask |= (CCMask & OFImplies) ? SystemZ::CCMASK_ARITH_OVERFLOW : 0; 471480093f4SDimitry Andric } 472480093f4SDimitry Andric AlterMasks[I + 1]->setImm(CCMask); 4730b57cec5SDimitry Andric } 4740b57cec5SDimitry Andric } 4750b57cec5SDimitry Andric 4760b57cec5SDimitry Andric // CC is now live after MI. 4778bcb0991SDimitry Andric if (!ConvOpc) 4788bcb0991SDimitry Andric MI.clearRegisterDeads(SystemZ::CC); 4790b57cec5SDimitry Andric 4800b57cec5SDimitry Andric // Check if MI lies before Compare. 4810b57cec5SDimitry Andric bool BeforeCmp = false; 4820b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MI, MBBE = MI.getParent()->end(); 4830b57cec5SDimitry Andric for (++MBBI; MBBI != MBBE; ++MBBI) 4840b57cec5SDimitry Andric if (MBBI == Compare) { 4850b57cec5SDimitry Andric BeforeCmp = true; 4860b57cec5SDimitry Andric break; 4870b57cec5SDimitry Andric } 4880b57cec5SDimitry Andric 4890b57cec5SDimitry Andric // Clear any intervening kills of CC. 4900b57cec5SDimitry Andric if (BeforeCmp) { 4910b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MI, MBBE = Compare; 4920b57cec5SDimitry Andric for (++MBBI; MBBI != MBBE; ++MBBI) 4930b57cec5SDimitry Andric MBBI->clearRegisterKills(SystemZ::CC, TRI); 4940b57cec5SDimitry Andric } 4950b57cec5SDimitry Andric 4960b57cec5SDimitry Andric return true; 4970b57cec5SDimitry Andric } 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric // Return true if Compare is a comparison against zero. 5000b57cec5SDimitry Andric static bool isCompareZero(MachineInstr &Compare) { 5010b57cec5SDimitry Andric switch (Compare.getOpcode()) { 5020b57cec5SDimitry Andric case SystemZ::LTEBRCompare: 5030b57cec5SDimitry Andric case SystemZ::LTDBRCompare: 5040b57cec5SDimitry Andric case SystemZ::LTXBRCompare: 5050b57cec5SDimitry Andric return true; 5060b57cec5SDimitry Andric 5070b57cec5SDimitry Andric default: 5080b57cec5SDimitry Andric if (isLoadAndTestAsCmp(Compare)) 5090b57cec5SDimitry Andric return true; 5100b57cec5SDimitry Andric return Compare.getNumExplicitOperands() == 2 && 5110b57cec5SDimitry Andric Compare.getOperand(1).isImm() && Compare.getOperand(1).getImm() == 0; 5120b57cec5SDimitry Andric } 5130b57cec5SDimitry Andric } 5140b57cec5SDimitry Andric 5150b57cec5SDimitry Andric // Try to optimize cases where comparison instruction Compare is testing 5160b57cec5SDimitry Andric // a value against zero. Return true on success and if Compare should be 5170b57cec5SDimitry Andric // deleted as dead. CCUsers is the list of instructions that use the CC 5180b57cec5SDimitry Andric // value produced by Compare. 5190b57cec5SDimitry Andric bool SystemZElimCompare::optimizeCompareZero( 5200b57cec5SDimitry Andric MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) { 5210b57cec5SDimitry Andric if (!isCompareZero(Compare)) 5220b57cec5SDimitry Andric return false; 5230b57cec5SDimitry Andric 5240b57cec5SDimitry Andric // Search back for CC results that are based on the first operand. 5250b57cec5SDimitry Andric unsigned SrcReg = getCompareSourceReg(Compare); 5260b57cec5SDimitry Andric MachineBasicBlock &MBB = *Compare.getParent(); 5270b57cec5SDimitry Andric Reference CCRefs; 5280b57cec5SDimitry Andric Reference SrcRefs; 5290b57cec5SDimitry Andric for (MachineBasicBlock::reverse_iterator MBBI = 5300b57cec5SDimitry Andric std::next(MachineBasicBlock::reverse_iterator(&Compare)), 5310b57cec5SDimitry Andric MBBE = MBB.rend(); MBBI != MBBE;) { 5320b57cec5SDimitry Andric MachineInstr &MI = *MBBI++; 5330b57cec5SDimitry Andric if (resultTests(MI, SrcReg)) { 5340b57cec5SDimitry Andric // Try to remove both MI and Compare by converting a branch to BRCT(G). 5350b57cec5SDimitry Andric // or a load-and-trap instruction. We don't care in this case whether 5360b57cec5SDimitry Andric // CC is modified between MI and Compare. 5370b57cec5SDimitry Andric if (!CCRefs.Use && !SrcRefs) { 5380b57cec5SDimitry Andric if (convertToBRCT(MI, Compare, CCUsers)) { 5390b57cec5SDimitry Andric BranchOnCounts += 1; 5400b57cec5SDimitry Andric return true; 5410b57cec5SDimitry Andric } 5420b57cec5SDimitry Andric if (convertToLoadAndTrap(MI, Compare, CCUsers)) { 5430b57cec5SDimitry Andric LoadAndTraps += 1; 5440b57cec5SDimitry Andric return true; 5450b57cec5SDimitry Andric } 5460b57cec5SDimitry Andric } 5470b57cec5SDimitry Andric // Try to eliminate Compare by reusing a CC result from MI. 5480b57cec5SDimitry Andric if ((!CCRefs && convertToLoadAndTest(MI, Compare, CCUsers)) || 549480093f4SDimitry Andric (!CCRefs.Def && 550480093f4SDimitry Andric (adjustCCMasksForInstr(MI, Compare, CCUsers) || 551480093f4SDimitry Andric convertToLogical(MI, Compare, CCUsers)))) { 5520b57cec5SDimitry Andric EliminatedComparisons += 1; 5530b57cec5SDimitry Andric return true; 5540b57cec5SDimitry Andric } 5550b57cec5SDimitry Andric } 5560b57cec5SDimitry Andric SrcRefs |= getRegReferences(MI, SrcReg); 5570b57cec5SDimitry Andric if (SrcRefs.Def) 5580b57cec5SDimitry Andric break; 5590b57cec5SDimitry Andric CCRefs |= getRegReferences(MI, SystemZ::CC); 5600b57cec5SDimitry Andric if (CCRefs.Use && CCRefs.Def) 5610b57cec5SDimitry Andric break; 562480093f4SDimitry Andric // Eliminating a Compare that may raise an FP exception will move 563480093f4SDimitry Andric // raising the exception to some earlier MI. We cannot do this if 564480093f4SDimitry Andric // there is anything in between that might change exception flags. 565480093f4SDimitry Andric if (Compare.mayRaiseFPException() && 566480093f4SDimitry Andric (MI.isCall() || MI.hasUnmodeledSideEffects())) 567480093f4SDimitry Andric break; 5680b57cec5SDimitry Andric } 5690b57cec5SDimitry Andric 5700b57cec5SDimitry Andric // Also do a forward search to handle cases where an instruction after the 5710b57cec5SDimitry Andric // compare can be converted, like 5720b57cec5SDimitry Andric // LTEBRCompare %f0s, %f0s; %f2s = LER %f0s => LTEBRCompare %f2s, %f0s 573349cc55cSDimitry Andric auto MIRange = llvm::make_range( 574349cc55cSDimitry Andric std::next(MachineBasicBlock::iterator(&Compare)), MBB.end()); 575349cc55cSDimitry Andric for (MachineInstr &MI : llvm::make_early_inc_range(MIRange)) { 5760b57cec5SDimitry Andric if (preservesValueOf(MI, SrcReg)) { 5770b57cec5SDimitry Andric // Try to eliminate Compare by reusing a CC result from MI. 5780b57cec5SDimitry Andric if (convertToLoadAndTest(MI, Compare, CCUsers)) { 5790b57cec5SDimitry Andric EliminatedComparisons += 1; 5800b57cec5SDimitry Andric return true; 5810b57cec5SDimitry Andric } 5820b57cec5SDimitry Andric } 5830b57cec5SDimitry Andric if (getRegReferences(MI, SrcReg).Def) 5840b57cec5SDimitry Andric return false; 5850b57cec5SDimitry Andric if (getRegReferences(MI, SystemZ::CC)) 5860b57cec5SDimitry Andric return false; 5870b57cec5SDimitry Andric } 5880b57cec5SDimitry Andric 5890b57cec5SDimitry Andric return false; 5900b57cec5SDimitry Andric } 5910b57cec5SDimitry Andric 5920b57cec5SDimitry Andric // Try to fuse comparison instruction Compare into a later branch. 5930b57cec5SDimitry Andric // Return true on success and if Compare is therefore redundant. 5940b57cec5SDimitry Andric bool SystemZElimCompare::fuseCompareOperations( 5950b57cec5SDimitry Andric MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) { 5960b57cec5SDimitry Andric // See whether we have a single branch with which to fuse. 5970b57cec5SDimitry Andric if (CCUsers.size() != 1) 5980b57cec5SDimitry Andric return false; 5990b57cec5SDimitry Andric MachineInstr *Branch = CCUsers[0]; 6000b57cec5SDimitry Andric SystemZII::FusedCompareType Type; 6010b57cec5SDimitry Andric switch (Branch->getOpcode()) { 6020b57cec5SDimitry Andric case SystemZ::BRC: 6030b57cec5SDimitry Andric Type = SystemZII::CompareAndBranch; 6040b57cec5SDimitry Andric break; 6050b57cec5SDimitry Andric case SystemZ::CondReturn: 6060b57cec5SDimitry Andric Type = SystemZII::CompareAndReturn; 6070b57cec5SDimitry Andric break; 6080b57cec5SDimitry Andric case SystemZ::CallBCR: 6090b57cec5SDimitry Andric Type = SystemZII::CompareAndSibcall; 6100b57cec5SDimitry Andric break; 6110b57cec5SDimitry Andric case SystemZ::CondTrap: 6120b57cec5SDimitry Andric Type = SystemZII::CompareAndTrap; 6130b57cec5SDimitry Andric break; 6140b57cec5SDimitry Andric default: 6150b57cec5SDimitry Andric return false; 6160b57cec5SDimitry Andric } 6170b57cec5SDimitry Andric 6180b57cec5SDimitry Andric // See whether we have a comparison that can be fused. 6190b57cec5SDimitry Andric unsigned FusedOpcode = 6200b57cec5SDimitry Andric TII->getFusedCompare(Compare.getOpcode(), Type, &Compare); 6210b57cec5SDimitry Andric if (!FusedOpcode) 6220b57cec5SDimitry Andric return false; 6230b57cec5SDimitry Andric 6240b57cec5SDimitry Andric // Make sure that the operands are available at the branch. 6250b57cec5SDimitry Andric // SrcReg2 is the register if the source operand is a register, 6260b57cec5SDimitry Andric // 0 if the source operand is immediate, and the base register 6270b57cec5SDimitry Andric // if the source operand is memory (index is not supported). 6280b57cec5SDimitry Andric Register SrcReg = Compare.getOperand(0).getReg(); 6290b57cec5SDimitry Andric Register SrcReg2 = 6300b57cec5SDimitry Andric Compare.getOperand(1).isReg() ? Compare.getOperand(1).getReg() : Register(); 6310b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; 6320b57cec5SDimitry Andric for (++MBBI; MBBI != MBBE; ++MBBI) 6330b57cec5SDimitry Andric if (MBBI->modifiesRegister(SrcReg, TRI) || 6340b57cec5SDimitry Andric (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI))) 6350b57cec5SDimitry Andric return false; 6360b57cec5SDimitry Andric 6370b57cec5SDimitry Andric // Read the branch mask, target (if applicable), regmask (if applicable). 6380b57cec5SDimitry Andric MachineOperand CCMask(MBBI->getOperand(1)); 6390b57cec5SDimitry Andric assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 && 6400b57cec5SDimitry Andric "Invalid condition-code mask for integer comparison"); 641e8d8bef9SDimitry Andric // This is only valid for CompareAndBranch and CompareAndSibcall. 6420b57cec5SDimitry Andric MachineOperand Target(MBBI->getOperand( 643e8d8bef9SDimitry Andric (Type == SystemZII::CompareAndBranch || 644e8d8bef9SDimitry Andric Type == SystemZII::CompareAndSibcall) ? 2 : 0)); 6450b57cec5SDimitry Andric const uint32_t *RegMask; 6460b57cec5SDimitry Andric if (Type == SystemZII::CompareAndSibcall) 647e8d8bef9SDimitry Andric RegMask = MBBI->getOperand(3).getRegMask(); 6480b57cec5SDimitry Andric 6490b57cec5SDimitry Andric // Clear out all current operands. 6500b57cec5SDimitry Andric int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI); 6510b57cec5SDimitry Andric assert(CCUse >= 0 && "BRC/BCR must use CC"); 6520b57cec5SDimitry Andric Branch->RemoveOperand(CCUse); 653e8d8bef9SDimitry Andric // Remove regmask (sibcall). 654e8d8bef9SDimitry Andric if (Type == SystemZII::CompareAndSibcall) 655e8d8bef9SDimitry Andric Branch->RemoveOperand(3); 656e8d8bef9SDimitry Andric // Remove target (branch or sibcall). 6570b57cec5SDimitry Andric if (Type == SystemZII::CompareAndBranch || 6580b57cec5SDimitry Andric Type == SystemZII::CompareAndSibcall) 6590b57cec5SDimitry Andric Branch->RemoveOperand(2); 6600b57cec5SDimitry Andric Branch->RemoveOperand(1); 6610b57cec5SDimitry Andric Branch->RemoveOperand(0); 6620b57cec5SDimitry Andric 6630b57cec5SDimitry Andric // Rebuild Branch as a fused compare and branch. 6640b57cec5SDimitry Andric // SrcNOps is the number of MI operands of the compare instruction 6650b57cec5SDimitry Andric // that we need to copy over. 6660b57cec5SDimitry Andric unsigned SrcNOps = 2; 6670b57cec5SDimitry Andric if (FusedOpcode == SystemZ::CLT || FusedOpcode == SystemZ::CLGT) 6680b57cec5SDimitry Andric SrcNOps = 3; 6690b57cec5SDimitry Andric Branch->setDesc(TII->get(FusedOpcode)); 6700b57cec5SDimitry Andric MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch); 6710b57cec5SDimitry Andric for (unsigned I = 0; I < SrcNOps; I++) 6720b57cec5SDimitry Andric MIB.add(Compare.getOperand(I)); 6730b57cec5SDimitry Andric MIB.add(CCMask); 6740b57cec5SDimitry Andric 6750b57cec5SDimitry Andric if (Type == SystemZII::CompareAndBranch) { 6760b57cec5SDimitry Andric // Only conditional branches define CC, as they may be converted back 6770b57cec5SDimitry Andric // to a non-fused branch because of a long displacement. Conditional 6780b57cec5SDimitry Andric // returns don't have that problem. 6790b57cec5SDimitry Andric MIB.add(Target).addReg(SystemZ::CC, 6800b57cec5SDimitry Andric RegState::ImplicitDefine | RegState::Dead); 6810b57cec5SDimitry Andric } 6820b57cec5SDimitry Andric 683e8d8bef9SDimitry Andric if (Type == SystemZII::CompareAndSibcall) { 684e8d8bef9SDimitry Andric MIB.add(Target); 6850b57cec5SDimitry Andric MIB.addRegMask(RegMask); 686e8d8bef9SDimitry Andric } 6870b57cec5SDimitry Andric 6880b57cec5SDimitry Andric // Clear any intervening kills of SrcReg and SrcReg2. 6890b57cec5SDimitry Andric MBBI = Compare; 6900b57cec5SDimitry Andric for (++MBBI; MBBI != MBBE; ++MBBI) { 6910b57cec5SDimitry Andric MBBI->clearRegisterKills(SrcReg, TRI); 6920b57cec5SDimitry Andric if (SrcReg2) 6930b57cec5SDimitry Andric MBBI->clearRegisterKills(SrcReg2, TRI); 6940b57cec5SDimitry Andric } 6950b57cec5SDimitry Andric FusedComparisons += 1; 6960b57cec5SDimitry Andric return true; 6970b57cec5SDimitry Andric } 6980b57cec5SDimitry Andric 6990b57cec5SDimitry Andric // Process all comparison instructions in MBB. Return true if something 7000b57cec5SDimitry Andric // changed. 7010b57cec5SDimitry Andric bool SystemZElimCompare::processBlock(MachineBasicBlock &MBB) { 7020b57cec5SDimitry Andric bool Changed = false; 7030b57cec5SDimitry Andric 7040b57cec5SDimitry Andric // Walk backwards through the block looking for comparisons, recording 7050b57cec5SDimitry Andric // all CC users as we go. The subroutines can delete Compare and 7060b57cec5SDimitry Andric // instructions before it. 707480093f4SDimitry Andric LivePhysRegs LiveRegs(*TRI); 708480093f4SDimitry Andric LiveRegs.addLiveOuts(MBB); 709480093f4SDimitry Andric bool CompleteCCUsers = !LiveRegs.contains(SystemZ::CC); 7100b57cec5SDimitry Andric SmallVector<MachineInstr *, 4> CCUsers; 7110b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.end(); 7120b57cec5SDimitry Andric while (MBBI != MBB.begin()) { 7130b57cec5SDimitry Andric MachineInstr &MI = *--MBBI; 7140b57cec5SDimitry Andric if (CompleteCCUsers && (MI.isCompare() || isLoadAndTestAsCmp(MI)) && 7150b57cec5SDimitry Andric (optimizeCompareZero(MI, CCUsers) || 7160b57cec5SDimitry Andric fuseCompareOperations(MI, CCUsers))) { 7170b57cec5SDimitry Andric ++MBBI; 7180b57cec5SDimitry Andric MI.eraseFromParent(); 7190b57cec5SDimitry Andric Changed = true; 7200b57cec5SDimitry Andric CCUsers.clear(); 7210b57cec5SDimitry Andric continue; 7220b57cec5SDimitry Andric } 7230b57cec5SDimitry Andric 7240b57cec5SDimitry Andric if (MI.definesRegister(SystemZ::CC)) { 7250b57cec5SDimitry Andric CCUsers.clear(); 7260b57cec5SDimitry Andric CompleteCCUsers = true; 7270b57cec5SDimitry Andric } 7280b57cec5SDimitry Andric if (MI.readsRegister(SystemZ::CC) && CompleteCCUsers) 7290b57cec5SDimitry Andric CCUsers.push_back(&MI); 7300b57cec5SDimitry Andric } 7310b57cec5SDimitry Andric return Changed; 7320b57cec5SDimitry Andric } 7330b57cec5SDimitry Andric 7340b57cec5SDimitry Andric bool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) { 7350b57cec5SDimitry Andric if (skipFunction(F.getFunction())) 7360b57cec5SDimitry Andric return false; 7370b57cec5SDimitry Andric 7380b57cec5SDimitry Andric TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo()); 7390b57cec5SDimitry Andric TRI = &TII->getRegisterInfo(); 7400b57cec5SDimitry Andric 7410b57cec5SDimitry Andric bool Changed = false; 7420b57cec5SDimitry Andric for (auto &MBB : F) 7430b57cec5SDimitry Andric Changed |= processBlock(MBB); 7440b57cec5SDimitry Andric 7450b57cec5SDimitry Andric return Changed; 7460b57cec5SDimitry Andric } 7470b57cec5SDimitry Andric 7480b57cec5SDimitry Andric FunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) { 749*04eeddc0SDimitry Andric return new SystemZElimCompare(); 7500b57cec5SDimitry Andric } 751