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" 21*0fca6ea1SDimitry Andric #include "llvm/CodeGen/LiveRegUnits.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 6804eeddc0SDimitry Andric SystemZElimCompare() : MachineFunctionPass(ID) { 6904eeddc0SDimitry 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 10604eeddc0SDimitry Andric INITIALIZE_PASS(SystemZElimCompare, DEBUG_TYPE, 10704eeddc0SDimitry Andric "SystemZ Comparison Elimination", false, false) 10804eeddc0SDimitry 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 if (MI.getOperand(1).getReg() == Reg) 1190b57cec5SDimitry Andric return true; 1200b57cec5SDimitry Andric } 1210b57cec5SDimitry Andric 1220b57cec5SDimitry Andric return false; 1230b57cec5SDimitry Andric } 1240b57cec5SDimitry Andric 1250b57cec5SDimitry Andric // Return true if any CC result of MI would (perhaps after conversion) 1260b57cec5SDimitry Andric // reflect the value of Reg. 1270b57cec5SDimitry Andric static bool resultTests(MachineInstr &MI, unsigned Reg) { 1280b57cec5SDimitry Andric if (MI.getNumOperands() > 0 && MI.getOperand(0).isReg() && 1290b57cec5SDimitry Andric MI.getOperand(0).isDef() && MI.getOperand(0).getReg() == Reg) 1300b57cec5SDimitry Andric return true; 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric return (preservesValueOf(MI, Reg)); 1330b57cec5SDimitry Andric } 1340b57cec5SDimitry Andric 1350b57cec5SDimitry Andric // Describe the references to Reg or any of its aliases in MI. 1360b57cec5SDimitry Andric Reference SystemZElimCompare::getRegReferences(MachineInstr &MI, unsigned Reg) { 1370b57cec5SDimitry Andric Reference Ref; 1380b57cec5SDimitry Andric if (MI.isDebugInstr()) 1390b57cec5SDimitry Andric return Ref; 1400b57cec5SDimitry Andric 1414824e7fdSDimitry Andric for (const MachineOperand &MO : MI.operands()) { 1420b57cec5SDimitry Andric if (MO.isReg()) { 1438bcb0991SDimitry Andric if (Register MOReg = MO.getReg()) { 1440b57cec5SDimitry Andric if (TRI->regsOverlap(MOReg, Reg)) { 1450b57cec5SDimitry Andric if (MO.isUse()) 1460b57cec5SDimitry Andric Ref.Use = true; 1470b57cec5SDimitry Andric else if (MO.isDef()) 1480b57cec5SDimitry Andric Ref.Def = true; 1490b57cec5SDimitry Andric } 1500b57cec5SDimitry Andric } 1510b57cec5SDimitry Andric } 1520b57cec5SDimitry Andric } 1530b57cec5SDimitry Andric return Ref; 1540b57cec5SDimitry Andric } 1550b57cec5SDimitry Andric 1560b57cec5SDimitry Andric // Return true if this is a load and test which can be optimized the 1570b57cec5SDimitry Andric // same way as compare instruction. 1580b57cec5SDimitry Andric static bool isLoadAndTestAsCmp(MachineInstr &MI) { 1590b57cec5SDimitry Andric // If we during isel used a load-and-test as a compare with 0, the 1600b57cec5SDimitry Andric // def operand is dead. 1610b57cec5SDimitry Andric return (MI.getOpcode() == SystemZ::LTEBR || 1620b57cec5SDimitry Andric MI.getOpcode() == SystemZ::LTDBR || 1630b57cec5SDimitry Andric MI.getOpcode() == SystemZ::LTXBR) && 1640b57cec5SDimitry Andric MI.getOperand(0).isDead(); 1650b57cec5SDimitry Andric } 1660b57cec5SDimitry Andric 1670b57cec5SDimitry Andric // Return the source register of Compare, which is the unknown value 1680b57cec5SDimitry Andric // being tested. 1690b57cec5SDimitry Andric static unsigned getCompareSourceReg(MachineInstr &Compare) { 1700b57cec5SDimitry Andric unsigned reg = 0; 1710b57cec5SDimitry Andric if (Compare.isCompare()) 1720b57cec5SDimitry Andric reg = Compare.getOperand(0).getReg(); 1730b57cec5SDimitry Andric else if (isLoadAndTestAsCmp(Compare)) 1740b57cec5SDimitry Andric reg = Compare.getOperand(1).getReg(); 1750b57cec5SDimitry Andric assert(reg); 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric return reg; 1780b57cec5SDimitry Andric } 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric // Compare compares the result of MI against zero. If MI is an addition 1810b57cec5SDimitry Andric // of -1 and if CCUsers is a single branch on nonzero, eliminate the addition 1820b57cec5SDimitry Andric // and convert the branch to a BRCT(G) or BRCTH. Return true on success. 1830b57cec5SDimitry Andric bool SystemZElimCompare::convertToBRCT( 1840b57cec5SDimitry Andric MachineInstr &MI, MachineInstr &Compare, 1850b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers) { 1860b57cec5SDimitry Andric // Check whether we have an addition of -1. 1870b57cec5SDimitry Andric unsigned Opcode = MI.getOpcode(); 1880b57cec5SDimitry Andric unsigned BRCT; 1890b57cec5SDimitry Andric if (Opcode == SystemZ::AHI) 1900b57cec5SDimitry Andric BRCT = SystemZ::BRCT; 1910b57cec5SDimitry Andric else if (Opcode == SystemZ::AGHI) 1920b57cec5SDimitry Andric BRCT = SystemZ::BRCTG; 1930b57cec5SDimitry Andric else if (Opcode == SystemZ::AIH) 1940b57cec5SDimitry Andric BRCT = SystemZ::BRCTH; 1950b57cec5SDimitry Andric else 1960b57cec5SDimitry Andric return false; 1970b57cec5SDimitry Andric if (MI.getOperand(2).getImm() != -1) 1980b57cec5SDimitry Andric return false; 1990b57cec5SDimitry Andric 2000b57cec5SDimitry Andric // Check whether we have a single JLH. 2010b57cec5SDimitry Andric if (CCUsers.size() != 1) 2020b57cec5SDimitry Andric return false; 2030b57cec5SDimitry Andric MachineInstr *Branch = CCUsers[0]; 2040b57cec5SDimitry Andric if (Branch->getOpcode() != SystemZ::BRC || 2050b57cec5SDimitry Andric Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP || 2060b57cec5SDimitry Andric Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_NE) 2070b57cec5SDimitry Andric return false; 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric // We already know that there are no references to the register between 2100b57cec5SDimitry Andric // MI and Compare. Make sure that there are also no references between 2110b57cec5SDimitry Andric // Compare and Branch. 2120b57cec5SDimitry Andric unsigned SrcReg = getCompareSourceReg(Compare); 2130b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; 2140b57cec5SDimitry Andric for (++MBBI; MBBI != MBBE; ++MBBI) 2150b57cec5SDimitry Andric if (getRegReferences(*MBBI, SrcReg)) 2160b57cec5SDimitry Andric return false; 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric // The transformation is OK. Rebuild Branch as a BRCT(G) or BRCTH. 2190b57cec5SDimitry Andric MachineOperand Target(Branch->getOperand(2)); 2200b57cec5SDimitry Andric while (Branch->getNumOperands()) 22181ad6265SDimitry Andric Branch->removeOperand(0); 2220b57cec5SDimitry Andric Branch->setDesc(TII->get(BRCT)); 2230b57cec5SDimitry Andric MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch); 2240b57cec5SDimitry Andric MIB.add(MI.getOperand(0)).add(MI.getOperand(1)).add(Target); 2250b57cec5SDimitry Andric // Add a CC def to BRCT(G), since we may have to split them again if the 2260b57cec5SDimitry Andric // branch displacement overflows. BRCTH has a 32-bit displacement, so 2270b57cec5SDimitry Andric // this is not necessary there. 2280b57cec5SDimitry Andric if (BRCT != SystemZ::BRCTH) 2290b57cec5SDimitry Andric MIB.addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead); 2300b57cec5SDimitry Andric MI.eraseFromParent(); 2310b57cec5SDimitry Andric return true; 2320b57cec5SDimitry Andric } 2330b57cec5SDimitry Andric 2340b57cec5SDimitry Andric // Compare compares the result of MI against zero. If MI is a suitable load 2350b57cec5SDimitry Andric // instruction and if CCUsers is a single conditional trap on zero, eliminate 2360b57cec5SDimitry Andric // the load and convert the branch to a load-and-trap. Return true on success. 2370b57cec5SDimitry Andric bool SystemZElimCompare::convertToLoadAndTrap( 2380b57cec5SDimitry Andric MachineInstr &MI, MachineInstr &Compare, 2390b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers) { 2400b57cec5SDimitry Andric unsigned LATOpcode = TII->getLoadAndTrap(MI.getOpcode()); 2410b57cec5SDimitry Andric if (!LATOpcode) 2420b57cec5SDimitry Andric return false; 2430b57cec5SDimitry Andric 2440b57cec5SDimitry Andric // Check whether we have a single CondTrap that traps on zero. 2450b57cec5SDimitry Andric if (CCUsers.size() != 1) 2460b57cec5SDimitry Andric return false; 2470b57cec5SDimitry Andric MachineInstr *Branch = CCUsers[0]; 2480b57cec5SDimitry Andric if (Branch->getOpcode() != SystemZ::CondTrap || 2490b57cec5SDimitry Andric Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP || 2500b57cec5SDimitry Andric Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_EQ) 2510b57cec5SDimitry Andric return false; 2520b57cec5SDimitry Andric 2530b57cec5SDimitry Andric // We already know that there are no references to the register between 2540b57cec5SDimitry Andric // MI and Compare. Make sure that there are also no references between 2550b57cec5SDimitry Andric // Compare and Branch. 2560b57cec5SDimitry Andric unsigned SrcReg = getCompareSourceReg(Compare); 2570b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; 2580b57cec5SDimitry Andric for (++MBBI; MBBI != MBBE; ++MBBI) 2590b57cec5SDimitry Andric if (getRegReferences(*MBBI, SrcReg)) 2600b57cec5SDimitry Andric return false; 2610b57cec5SDimitry Andric 2620b57cec5SDimitry Andric // The transformation is OK. Rebuild Branch as a load-and-trap. 2630b57cec5SDimitry Andric while (Branch->getNumOperands()) 26481ad6265SDimitry Andric Branch->removeOperand(0); 2650b57cec5SDimitry Andric Branch->setDesc(TII->get(LATOpcode)); 2660b57cec5SDimitry Andric MachineInstrBuilder(*Branch->getParent()->getParent(), Branch) 2670b57cec5SDimitry Andric .add(MI.getOperand(0)) 2680b57cec5SDimitry Andric .add(MI.getOperand(1)) 2690b57cec5SDimitry Andric .add(MI.getOperand(2)) 2700b57cec5SDimitry Andric .add(MI.getOperand(3)); 2710b57cec5SDimitry Andric MI.eraseFromParent(); 2720b57cec5SDimitry Andric return true; 2730b57cec5SDimitry Andric } 2740b57cec5SDimitry Andric 2750b57cec5SDimitry Andric // If MI is a load instruction, try to convert it into a LOAD AND TEST. 2760b57cec5SDimitry Andric // Return true on success. 2770b57cec5SDimitry Andric bool SystemZElimCompare::convertToLoadAndTest( 2780b57cec5SDimitry Andric MachineInstr &MI, MachineInstr &Compare, 2790b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers) { 2800b57cec5SDimitry Andric 2810b57cec5SDimitry Andric // Try to adjust CC masks for the LOAD AND TEST opcode that could replace MI. 2820b57cec5SDimitry Andric unsigned Opcode = TII->getLoadAndTest(MI.getOpcode()); 2830b57cec5SDimitry Andric if (!Opcode || !adjustCCMasksForInstr(MI, Compare, CCUsers, Opcode)) 2840b57cec5SDimitry Andric return false; 2850b57cec5SDimitry Andric 2860b57cec5SDimitry Andric // Rebuild to get the CC operand in the right place. 2870b57cec5SDimitry Andric auto MIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(Opcode)); 2880b57cec5SDimitry Andric for (const auto &MO : MI.operands()) 2890b57cec5SDimitry Andric MIB.add(MO); 2900b57cec5SDimitry Andric MIB.setMemRefs(MI.memoperands()); 2910b57cec5SDimitry Andric MI.eraseFromParent(); 2920b57cec5SDimitry Andric 293480093f4SDimitry Andric // Mark instruction as not raising an FP exception if applicable. We already 294480093f4SDimitry Andric // verified earlier that this move is valid. 295480093f4SDimitry Andric if (!Compare.mayRaiseFPException()) 296480093f4SDimitry Andric MIB.setMIFlag(MachineInstr::MIFlag::NoFPExcept); 297480093f4SDimitry Andric 2980b57cec5SDimitry Andric return true; 2990b57cec5SDimitry Andric } 3000b57cec5SDimitry Andric 301480093f4SDimitry Andric // See if MI is an instruction with an equivalent "logical" opcode that can 302480093f4SDimitry Andric // be used and replace MI. This is useful for EQ/NE comparisons where the 303480093f4SDimitry Andric // "nsw" flag is missing since the "logical" opcode always sets CC to reflect 304480093f4SDimitry Andric // the result being zero or non-zero. 305480093f4SDimitry Andric bool SystemZElimCompare::convertToLogical( 306480093f4SDimitry Andric MachineInstr &MI, MachineInstr &Compare, 307480093f4SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers) { 308480093f4SDimitry Andric 309480093f4SDimitry Andric unsigned ConvOpc = 0; 310480093f4SDimitry Andric switch (MI.getOpcode()) { 311480093f4SDimitry Andric case SystemZ::AR: ConvOpc = SystemZ::ALR; break; 312480093f4SDimitry Andric case SystemZ::ARK: ConvOpc = SystemZ::ALRK; break; 313480093f4SDimitry Andric case SystemZ::AGR: ConvOpc = SystemZ::ALGR; break; 314480093f4SDimitry Andric case SystemZ::AGRK: ConvOpc = SystemZ::ALGRK; break; 315480093f4SDimitry Andric case SystemZ::A: ConvOpc = SystemZ::AL; break; 316480093f4SDimitry Andric case SystemZ::AY: ConvOpc = SystemZ::ALY; break; 317480093f4SDimitry Andric case SystemZ::AG: ConvOpc = SystemZ::ALG; break; 318480093f4SDimitry Andric default: break; 319480093f4SDimitry Andric } 320480093f4SDimitry Andric if (!ConvOpc || !adjustCCMasksForInstr(MI, Compare, CCUsers, ConvOpc)) 321480093f4SDimitry Andric return false; 322480093f4SDimitry Andric 323480093f4SDimitry Andric // Operands should be identical, so just change the opcode and remove the 324480093f4SDimitry Andric // dead flag on CC. 325480093f4SDimitry Andric MI.setDesc(TII->get(ConvOpc)); 326480093f4SDimitry Andric MI.clearRegisterDeads(SystemZ::CC); 327480093f4SDimitry Andric return true; 328480093f4SDimitry Andric } 329480093f4SDimitry Andric 330480093f4SDimitry Andric #ifndef NDEBUG 331480093f4SDimitry Andric static bool isAddWithImmediate(unsigned Opcode) { 332480093f4SDimitry Andric switch(Opcode) { 333480093f4SDimitry Andric case SystemZ::AHI: 334480093f4SDimitry Andric case SystemZ::AHIK: 335480093f4SDimitry Andric case SystemZ::AGHI: 336480093f4SDimitry Andric case SystemZ::AGHIK: 337480093f4SDimitry Andric case SystemZ::AFI: 338480093f4SDimitry Andric case SystemZ::AIH: 339480093f4SDimitry Andric case SystemZ::AGFI: 340480093f4SDimitry Andric return true; 341480093f4SDimitry Andric default: break; 342480093f4SDimitry Andric } 343480093f4SDimitry Andric return false; 344480093f4SDimitry Andric } 345480093f4SDimitry Andric #endif 346480093f4SDimitry Andric 3470b57cec5SDimitry Andric // The CC users in CCUsers are testing the result of a comparison of some 3480b57cec5SDimitry Andric // value X against zero and we know that any CC value produced by MI would 3490b57cec5SDimitry Andric // also reflect the value of X. ConvOpc may be used to pass the transfomed 3500b57cec5SDimitry Andric // opcode MI will have if this succeeds. Try to adjust CCUsers so that they 3510b57cec5SDimitry Andric // test the result of MI directly, returning true on success. Leave 3520b57cec5SDimitry Andric // everything unchanged on failure. 3530b57cec5SDimitry Andric bool SystemZElimCompare::adjustCCMasksForInstr( 3540b57cec5SDimitry Andric MachineInstr &MI, MachineInstr &Compare, 3550b57cec5SDimitry Andric SmallVectorImpl<MachineInstr *> &CCUsers, 3560b57cec5SDimitry Andric unsigned ConvOpc) { 357480093f4SDimitry Andric unsigned CompareFlags = Compare.getDesc().TSFlags; 358480093f4SDimitry Andric unsigned CompareCCValues = SystemZII::getCCValues(CompareFlags); 3590b57cec5SDimitry Andric int Opcode = (ConvOpc ? ConvOpc : MI.getOpcode()); 3600b57cec5SDimitry Andric const MCInstrDesc &Desc = TII->get(Opcode); 3610b57cec5SDimitry Andric unsigned MIFlags = Desc.TSFlags; 3620b57cec5SDimitry Andric 363480093f4SDimitry Andric // If Compare may raise an FP exception, we can only eliminate it 364480093f4SDimitry Andric // if MI itself would have already raised the exception. 365480093f4SDimitry Andric if (Compare.mayRaiseFPException()) { 366480093f4SDimitry Andric // If the caller will change MI to use ConvOpc, only test whether 367480093f4SDimitry Andric // ConvOpc is suitable; it is on the caller to set the MI flag. 368480093f4SDimitry Andric if (ConvOpc && !Desc.mayRaiseFPException()) 369480093f4SDimitry Andric return false; 370480093f4SDimitry Andric // If the caller will not change MI, we test the MI flag here. 371480093f4SDimitry Andric if (!ConvOpc && !MI.mayRaiseFPException()) 372480093f4SDimitry Andric return false; 373480093f4SDimitry Andric } 3740b57cec5SDimitry Andric 375480093f4SDimitry Andric // See which compare-style condition codes are available. 376480093f4SDimitry Andric unsigned CCValues = SystemZII::getCCValues(MIFlags); 377480093f4SDimitry Andric unsigned ReusableCCMask = CCValues; 3780b57cec5SDimitry Andric // For unsigned comparisons with zero, only equality makes sense. 3790b57cec5SDimitry Andric if (CompareFlags & SystemZII::IsLogical) 3800b57cec5SDimitry Andric ReusableCCMask &= SystemZ::CCMASK_CMP_EQ; 381480093f4SDimitry Andric unsigned OFImplies = 0; 382480093f4SDimitry Andric bool LogicalMI = false; 383480093f4SDimitry Andric bool MIEquivalentToCmp = false; 384480093f4SDimitry Andric if (MI.getFlag(MachineInstr::NoSWrap) && 385480093f4SDimitry Andric (MIFlags & SystemZII::CCIfNoSignedWrap)) { 386480093f4SDimitry Andric // If MI has the NSW flag set in combination with the 387480093f4SDimitry Andric // SystemZII::CCIfNoSignedWrap flag, all CCValues are valid. 388480093f4SDimitry Andric } 389480093f4SDimitry Andric else if ((MIFlags & SystemZII::CCIfNoSignedWrap) && 390480093f4SDimitry Andric MI.getOperand(2).isImm()) { 391480093f4SDimitry Andric // Signed addition of immediate. If adding a positive immediate 392480093f4SDimitry Andric // overflows, the result must be less than zero. If adding a negative 393480093f4SDimitry Andric // immediate overflows, the result must be larger than zero (except in 394480093f4SDimitry Andric // the special case of adding the minimum value of the result range, in 395480093f4SDimitry Andric // which case we cannot predict whether the result is larger than or 396480093f4SDimitry Andric // equal to zero). 397480093f4SDimitry Andric assert(isAddWithImmediate(Opcode) && "Expected an add with immediate."); 398480093f4SDimitry Andric assert(!MI.mayLoadOrStore() && "Expected an immediate term."); 399480093f4SDimitry Andric int64_t RHS = MI.getOperand(2).getImm(); 400480093f4SDimitry Andric if (SystemZ::GRX32BitRegClass.contains(MI.getOperand(0).getReg()) && 401480093f4SDimitry Andric RHS == INT32_MIN) 402480093f4SDimitry Andric return false; 403480093f4SDimitry Andric OFImplies = (RHS > 0 ? SystemZ::CCMASK_CMP_LT : SystemZ::CCMASK_CMP_GT); 404480093f4SDimitry Andric } 405480093f4SDimitry Andric else if ((MIFlags & SystemZII::IsLogical) && CCValues) { 406480093f4SDimitry Andric // Use CCMASK_CMP_EQ to match with CCUsers. On success CCMask:s will be 407480093f4SDimitry Andric // converted to CCMASK_LOGICAL_ZERO or CCMASK_LOGICAL_NONZERO. 408480093f4SDimitry Andric LogicalMI = true; 409480093f4SDimitry Andric ReusableCCMask = SystemZ::CCMASK_CMP_EQ; 410480093f4SDimitry Andric } 411480093f4SDimitry Andric else { 412480093f4SDimitry Andric ReusableCCMask &= SystemZII::getCompareZeroCCMask(MIFlags); 413480093f4SDimitry Andric assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues"); 414480093f4SDimitry Andric MIEquivalentToCmp = 415480093f4SDimitry Andric ReusableCCMask == CCValues && CCValues == CompareCCValues; 416480093f4SDimitry Andric } 4170b57cec5SDimitry Andric if (ReusableCCMask == 0) 4180b57cec5SDimitry Andric return false; 4190b57cec5SDimitry Andric 4200b57cec5SDimitry Andric if (!MIEquivalentToCmp) { 4210b57cec5SDimitry Andric // Now check whether these flags are enough for all users. 4220b57cec5SDimitry Andric SmallVector<MachineOperand *, 4> AlterMasks; 423*0fca6ea1SDimitry Andric for (MachineInstr *CCUserMI : CCUsers) { 4240b57cec5SDimitry Andric // Fail if this isn't a use of CC that we understand. 425480093f4SDimitry Andric unsigned Flags = CCUserMI->getDesc().TSFlags; 4260b57cec5SDimitry Andric unsigned FirstOpNum; 4270b57cec5SDimitry Andric if (Flags & SystemZII::CCMaskFirst) 4280b57cec5SDimitry Andric FirstOpNum = 0; 4290b57cec5SDimitry Andric else if (Flags & SystemZII::CCMaskLast) 430480093f4SDimitry Andric FirstOpNum = CCUserMI->getNumExplicitOperands() - 2; 4310b57cec5SDimitry Andric else 4320b57cec5SDimitry Andric return false; 4330b57cec5SDimitry Andric 4340b57cec5SDimitry Andric // Check whether the instruction predicate treats all CC values 4350b57cec5SDimitry Andric // outside of ReusableCCMask in the same way. In that case it 4360b57cec5SDimitry Andric // doesn't matter what those CC values mean. 437480093f4SDimitry Andric unsigned CCValid = CCUserMI->getOperand(FirstOpNum).getImm(); 438480093f4SDimitry Andric unsigned CCMask = CCUserMI->getOperand(FirstOpNum + 1).getImm(); 439480093f4SDimitry Andric assert(CCValid == CompareCCValues && (CCMask & ~CCValid) == 0 && 440480093f4SDimitry Andric "Corrupt CC operands of CCUser."); 4410b57cec5SDimitry Andric unsigned OutValid = ~ReusableCCMask & CCValid; 4420b57cec5SDimitry Andric unsigned OutMask = ~ReusableCCMask & CCMask; 4430b57cec5SDimitry Andric if (OutMask != 0 && OutMask != OutValid) 4440b57cec5SDimitry Andric return false; 4450b57cec5SDimitry Andric 446480093f4SDimitry Andric AlterMasks.push_back(&CCUserMI->getOperand(FirstOpNum)); 447480093f4SDimitry Andric AlterMasks.push_back(&CCUserMI->getOperand(FirstOpNum + 1)); 4480b57cec5SDimitry Andric } 4490b57cec5SDimitry Andric 4500b57cec5SDimitry Andric // All users are OK. Adjust the masks for MI. 4510b57cec5SDimitry Andric for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) { 4520b57cec5SDimitry Andric AlterMasks[I]->setImm(CCValues); 4530b57cec5SDimitry Andric unsigned CCMask = AlterMasks[I + 1]->getImm(); 454480093f4SDimitry Andric if (LogicalMI) { 455480093f4SDimitry Andric // Translate the CCMask into its "logical" value. 456480093f4SDimitry Andric CCMask = (CCMask == SystemZ::CCMASK_CMP_EQ ? 457480093f4SDimitry Andric SystemZ::CCMASK_LOGICAL_ZERO : SystemZ::CCMASK_LOGICAL_NONZERO); 458480093f4SDimitry Andric CCMask &= CCValues; // Logical subtracts never set CC=0. 459480093f4SDimitry Andric } else { 4600b57cec5SDimitry Andric if (CCMask & ~ReusableCCMask) 461480093f4SDimitry Andric CCMask = (CCMask & ReusableCCMask) | (CCValues & ~ReusableCCMask); 462480093f4SDimitry Andric CCMask |= (CCMask & OFImplies) ? SystemZ::CCMASK_ARITH_OVERFLOW : 0; 463480093f4SDimitry Andric } 464480093f4SDimitry Andric AlterMasks[I + 1]->setImm(CCMask); 4650b57cec5SDimitry Andric } 4660b57cec5SDimitry Andric } 4670b57cec5SDimitry Andric 4680b57cec5SDimitry Andric // CC is now live after MI. 4698bcb0991SDimitry Andric if (!ConvOpc) 4708bcb0991SDimitry Andric MI.clearRegisterDeads(SystemZ::CC); 4710b57cec5SDimitry Andric 4720b57cec5SDimitry Andric // Check if MI lies before Compare. 4730b57cec5SDimitry Andric bool BeforeCmp = false; 4740b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MI, MBBE = MI.getParent()->end(); 4750b57cec5SDimitry Andric for (++MBBI; MBBI != MBBE; ++MBBI) 4760b57cec5SDimitry Andric if (MBBI == Compare) { 4770b57cec5SDimitry Andric BeforeCmp = true; 4780b57cec5SDimitry Andric break; 4790b57cec5SDimitry Andric } 4800b57cec5SDimitry Andric 4810b57cec5SDimitry Andric // Clear any intervening kills of CC. 4820b57cec5SDimitry Andric if (BeforeCmp) { 4830b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MI, MBBE = Compare; 4840b57cec5SDimitry Andric for (++MBBI; MBBI != MBBE; ++MBBI) 4850b57cec5SDimitry Andric MBBI->clearRegisterKills(SystemZ::CC, TRI); 4860b57cec5SDimitry Andric } 4870b57cec5SDimitry Andric 4880b57cec5SDimitry Andric return true; 4890b57cec5SDimitry Andric } 4900b57cec5SDimitry Andric 4910b57cec5SDimitry Andric // Return true if Compare is a comparison against zero. 4920b57cec5SDimitry Andric static bool isCompareZero(MachineInstr &Compare) { 4930b57cec5SDimitry Andric if (isLoadAndTestAsCmp(Compare)) 4940b57cec5SDimitry Andric return true; 4950b57cec5SDimitry Andric return Compare.getNumExplicitOperands() == 2 && 4960b57cec5SDimitry Andric Compare.getOperand(1).isImm() && Compare.getOperand(1).getImm() == 0; 4970b57cec5SDimitry Andric } 4980b57cec5SDimitry Andric 4990b57cec5SDimitry Andric // Try to optimize cases where comparison instruction Compare is testing 5000b57cec5SDimitry Andric // a value against zero. Return true on success and if Compare should be 5010b57cec5SDimitry Andric // deleted as dead. CCUsers is the list of instructions that use the CC 5020b57cec5SDimitry Andric // value produced by Compare. 5030b57cec5SDimitry Andric bool SystemZElimCompare::optimizeCompareZero( 5040b57cec5SDimitry Andric MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) { 5050b57cec5SDimitry Andric if (!isCompareZero(Compare)) 5060b57cec5SDimitry Andric return false; 5070b57cec5SDimitry Andric 5080b57cec5SDimitry Andric // Search back for CC results that are based on the first operand. 5090b57cec5SDimitry Andric unsigned SrcReg = getCompareSourceReg(Compare); 5100b57cec5SDimitry Andric MachineBasicBlock &MBB = *Compare.getParent(); 5110b57cec5SDimitry Andric Reference CCRefs; 5120b57cec5SDimitry Andric Reference SrcRefs; 5130b57cec5SDimitry Andric for (MachineBasicBlock::reverse_iterator MBBI = 5140b57cec5SDimitry Andric std::next(MachineBasicBlock::reverse_iterator(&Compare)), 5150b57cec5SDimitry Andric MBBE = MBB.rend(); MBBI != MBBE;) { 5160b57cec5SDimitry Andric MachineInstr &MI = *MBBI++; 5170b57cec5SDimitry Andric if (resultTests(MI, SrcReg)) { 5180b57cec5SDimitry Andric // Try to remove both MI and Compare by converting a branch to BRCT(G). 5190b57cec5SDimitry Andric // or a load-and-trap instruction. We don't care in this case whether 5200b57cec5SDimitry Andric // CC is modified between MI and Compare. 5210b57cec5SDimitry Andric if (!CCRefs.Use && !SrcRefs) { 5220b57cec5SDimitry Andric if (convertToBRCT(MI, Compare, CCUsers)) { 5230b57cec5SDimitry Andric BranchOnCounts += 1; 5240b57cec5SDimitry Andric return true; 5250b57cec5SDimitry Andric } 5260b57cec5SDimitry Andric if (convertToLoadAndTrap(MI, Compare, CCUsers)) { 5270b57cec5SDimitry Andric LoadAndTraps += 1; 5280b57cec5SDimitry Andric return true; 5290b57cec5SDimitry Andric } 5300b57cec5SDimitry Andric } 5310b57cec5SDimitry Andric // Try to eliminate Compare by reusing a CC result from MI. 5320b57cec5SDimitry Andric if ((!CCRefs && convertToLoadAndTest(MI, Compare, CCUsers)) || 533480093f4SDimitry Andric (!CCRefs.Def && 534480093f4SDimitry Andric (adjustCCMasksForInstr(MI, Compare, CCUsers) || 535480093f4SDimitry Andric convertToLogical(MI, Compare, CCUsers)))) { 5360b57cec5SDimitry Andric EliminatedComparisons += 1; 5370b57cec5SDimitry Andric return true; 5380b57cec5SDimitry Andric } 5390b57cec5SDimitry Andric } 5400b57cec5SDimitry Andric SrcRefs |= getRegReferences(MI, SrcReg); 5410b57cec5SDimitry Andric if (SrcRefs.Def) 5420b57cec5SDimitry Andric break; 5430b57cec5SDimitry Andric CCRefs |= getRegReferences(MI, SystemZ::CC); 5440b57cec5SDimitry Andric if (CCRefs.Use && CCRefs.Def) 5450b57cec5SDimitry Andric break; 546480093f4SDimitry Andric // Eliminating a Compare that may raise an FP exception will move 547480093f4SDimitry Andric // raising the exception to some earlier MI. We cannot do this if 548480093f4SDimitry Andric // there is anything in between that might change exception flags. 549480093f4SDimitry Andric if (Compare.mayRaiseFPException() && 550480093f4SDimitry Andric (MI.isCall() || MI.hasUnmodeledSideEffects())) 551480093f4SDimitry Andric break; 5520b57cec5SDimitry Andric } 5530b57cec5SDimitry Andric 5540b57cec5SDimitry Andric // Also do a forward search to handle cases where an instruction after the 5550b57cec5SDimitry Andric // compare can be converted, like 5567a6dacacSDimitry Andric // CGHI %r0d, 0; %r1d = LGR %r0d => LTGR %r1d, %r0d 557349cc55cSDimitry Andric auto MIRange = llvm::make_range( 558349cc55cSDimitry Andric std::next(MachineBasicBlock::iterator(&Compare)), MBB.end()); 559349cc55cSDimitry Andric for (MachineInstr &MI : llvm::make_early_inc_range(MIRange)) { 5600b57cec5SDimitry Andric if (preservesValueOf(MI, SrcReg)) { 5610b57cec5SDimitry Andric // Try to eliminate Compare by reusing a CC result from MI. 5620b57cec5SDimitry Andric if (convertToLoadAndTest(MI, Compare, CCUsers)) { 5630b57cec5SDimitry Andric EliminatedComparisons += 1; 5640b57cec5SDimitry Andric return true; 5650b57cec5SDimitry Andric } 5660b57cec5SDimitry Andric } 5670b57cec5SDimitry Andric if (getRegReferences(MI, SrcReg).Def) 5680b57cec5SDimitry Andric return false; 5690b57cec5SDimitry Andric if (getRegReferences(MI, SystemZ::CC)) 5700b57cec5SDimitry Andric return false; 5710b57cec5SDimitry Andric } 5720b57cec5SDimitry Andric 5730b57cec5SDimitry Andric return false; 5740b57cec5SDimitry Andric } 5750b57cec5SDimitry Andric 5760b57cec5SDimitry Andric // Try to fuse comparison instruction Compare into a later branch. 5770b57cec5SDimitry Andric // Return true on success and if Compare is therefore redundant. 5780b57cec5SDimitry Andric bool SystemZElimCompare::fuseCompareOperations( 5790b57cec5SDimitry Andric MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) { 5800b57cec5SDimitry Andric // See whether we have a single branch with which to fuse. 5810b57cec5SDimitry Andric if (CCUsers.size() != 1) 5820b57cec5SDimitry Andric return false; 5830b57cec5SDimitry Andric MachineInstr *Branch = CCUsers[0]; 5840b57cec5SDimitry Andric SystemZII::FusedCompareType Type; 5850b57cec5SDimitry Andric switch (Branch->getOpcode()) { 5860b57cec5SDimitry Andric case SystemZ::BRC: 5870b57cec5SDimitry Andric Type = SystemZII::CompareAndBranch; 5880b57cec5SDimitry Andric break; 5890b57cec5SDimitry Andric case SystemZ::CondReturn: 5900b57cec5SDimitry Andric Type = SystemZII::CompareAndReturn; 5910b57cec5SDimitry Andric break; 5920b57cec5SDimitry Andric case SystemZ::CallBCR: 5930b57cec5SDimitry Andric Type = SystemZII::CompareAndSibcall; 5940b57cec5SDimitry Andric break; 5950b57cec5SDimitry Andric case SystemZ::CondTrap: 5960b57cec5SDimitry Andric Type = SystemZII::CompareAndTrap; 5970b57cec5SDimitry Andric break; 5980b57cec5SDimitry Andric default: 5990b57cec5SDimitry Andric return false; 6000b57cec5SDimitry Andric } 6010b57cec5SDimitry Andric 6020b57cec5SDimitry Andric // See whether we have a comparison that can be fused. 6030b57cec5SDimitry Andric unsigned FusedOpcode = 6040b57cec5SDimitry Andric TII->getFusedCompare(Compare.getOpcode(), Type, &Compare); 6050b57cec5SDimitry Andric if (!FusedOpcode) 6060b57cec5SDimitry Andric return false; 6070b57cec5SDimitry Andric 6080b57cec5SDimitry Andric // Make sure that the operands are available at the branch. 6090b57cec5SDimitry Andric // SrcReg2 is the register if the source operand is a register, 6100b57cec5SDimitry Andric // 0 if the source operand is immediate, and the base register 6110b57cec5SDimitry Andric // if the source operand is memory (index is not supported). 6120b57cec5SDimitry Andric Register SrcReg = Compare.getOperand(0).getReg(); 6130b57cec5SDimitry Andric Register SrcReg2 = 6140b57cec5SDimitry Andric Compare.getOperand(1).isReg() ? Compare.getOperand(1).getReg() : Register(); 6150b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch; 6160b57cec5SDimitry Andric for (++MBBI; MBBI != MBBE; ++MBBI) 6170b57cec5SDimitry Andric if (MBBI->modifiesRegister(SrcReg, TRI) || 6180b57cec5SDimitry Andric (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI))) 6190b57cec5SDimitry Andric return false; 6200b57cec5SDimitry Andric 6210b57cec5SDimitry Andric // Read the branch mask, target (if applicable), regmask (if applicable). 6220b57cec5SDimitry Andric MachineOperand CCMask(MBBI->getOperand(1)); 6230b57cec5SDimitry Andric assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 && 6240b57cec5SDimitry Andric "Invalid condition-code mask for integer comparison"); 625e8d8bef9SDimitry Andric // This is only valid for CompareAndBranch and CompareAndSibcall. 6260b57cec5SDimitry Andric MachineOperand Target(MBBI->getOperand( 627e8d8bef9SDimitry Andric (Type == SystemZII::CompareAndBranch || 628e8d8bef9SDimitry Andric Type == SystemZII::CompareAndSibcall) ? 2 : 0)); 6290b57cec5SDimitry Andric const uint32_t *RegMask; 6300b57cec5SDimitry Andric if (Type == SystemZII::CompareAndSibcall) 631e8d8bef9SDimitry Andric RegMask = MBBI->getOperand(3).getRegMask(); 6320b57cec5SDimitry Andric 6330b57cec5SDimitry Andric // Clear out all current operands. 634*0fca6ea1SDimitry Andric int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, TRI, false); 6350b57cec5SDimitry Andric assert(CCUse >= 0 && "BRC/BCR must use CC"); 63681ad6265SDimitry Andric Branch->removeOperand(CCUse); 637e8d8bef9SDimitry Andric // Remove regmask (sibcall). 638e8d8bef9SDimitry Andric if (Type == SystemZII::CompareAndSibcall) 63981ad6265SDimitry Andric Branch->removeOperand(3); 640e8d8bef9SDimitry Andric // Remove target (branch or sibcall). 6410b57cec5SDimitry Andric if (Type == SystemZII::CompareAndBranch || 6420b57cec5SDimitry Andric Type == SystemZII::CompareAndSibcall) 64381ad6265SDimitry Andric Branch->removeOperand(2); 64481ad6265SDimitry Andric Branch->removeOperand(1); 64581ad6265SDimitry Andric Branch->removeOperand(0); 6460b57cec5SDimitry Andric 6470b57cec5SDimitry Andric // Rebuild Branch as a fused compare and branch. 6480b57cec5SDimitry Andric // SrcNOps is the number of MI operands of the compare instruction 6490b57cec5SDimitry Andric // that we need to copy over. 6500b57cec5SDimitry Andric unsigned SrcNOps = 2; 6510b57cec5SDimitry Andric if (FusedOpcode == SystemZ::CLT || FusedOpcode == SystemZ::CLGT) 6520b57cec5SDimitry Andric SrcNOps = 3; 6530b57cec5SDimitry Andric Branch->setDesc(TII->get(FusedOpcode)); 6540b57cec5SDimitry Andric MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch); 6550b57cec5SDimitry Andric for (unsigned I = 0; I < SrcNOps; I++) 6560b57cec5SDimitry Andric MIB.add(Compare.getOperand(I)); 6570b57cec5SDimitry Andric MIB.add(CCMask); 6580b57cec5SDimitry Andric 6590b57cec5SDimitry Andric if (Type == SystemZII::CompareAndBranch) { 6600b57cec5SDimitry Andric // Only conditional branches define CC, as they may be converted back 6610b57cec5SDimitry Andric // to a non-fused branch because of a long displacement. Conditional 6620b57cec5SDimitry Andric // returns don't have that problem. 6630b57cec5SDimitry Andric MIB.add(Target).addReg(SystemZ::CC, 6640b57cec5SDimitry Andric RegState::ImplicitDefine | RegState::Dead); 6650b57cec5SDimitry Andric } 6660b57cec5SDimitry Andric 667e8d8bef9SDimitry Andric if (Type == SystemZII::CompareAndSibcall) { 668e8d8bef9SDimitry Andric MIB.add(Target); 6690b57cec5SDimitry Andric MIB.addRegMask(RegMask); 670e8d8bef9SDimitry Andric } 6710b57cec5SDimitry Andric 6720b57cec5SDimitry Andric // Clear any intervening kills of SrcReg and SrcReg2. 6730b57cec5SDimitry Andric MBBI = Compare; 6740b57cec5SDimitry Andric for (++MBBI; MBBI != MBBE; ++MBBI) { 6750b57cec5SDimitry Andric MBBI->clearRegisterKills(SrcReg, TRI); 6760b57cec5SDimitry Andric if (SrcReg2) 6770b57cec5SDimitry Andric MBBI->clearRegisterKills(SrcReg2, TRI); 6780b57cec5SDimitry Andric } 6790b57cec5SDimitry Andric FusedComparisons += 1; 6800b57cec5SDimitry Andric return true; 6810b57cec5SDimitry Andric } 6820b57cec5SDimitry Andric 6830b57cec5SDimitry Andric // Process all comparison instructions in MBB. Return true if something 6840b57cec5SDimitry Andric // changed. 6850b57cec5SDimitry Andric bool SystemZElimCompare::processBlock(MachineBasicBlock &MBB) { 6860b57cec5SDimitry Andric bool Changed = false; 6870b57cec5SDimitry Andric 6880b57cec5SDimitry Andric // Walk backwards through the block looking for comparisons, recording 6890b57cec5SDimitry Andric // all CC users as we go. The subroutines can delete Compare and 6900b57cec5SDimitry Andric // instructions before it. 691*0fca6ea1SDimitry Andric LiveRegUnits LiveRegs(*TRI); 692480093f4SDimitry Andric LiveRegs.addLiveOuts(MBB); 693*0fca6ea1SDimitry Andric bool CompleteCCUsers = LiveRegs.available(SystemZ::CC); 6940b57cec5SDimitry Andric SmallVector<MachineInstr *, 4> CCUsers; 6950b57cec5SDimitry Andric MachineBasicBlock::iterator MBBI = MBB.end(); 6960b57cec5SDimitry Andric while (MBBI != MBB.begin()) { 6970b57cec5SDimitry Andric MachineInstr &MI = *--MBBI; 6980b57cec5SDimitry Andric if (CompleteCCUsers && (MI.isCompare() || isLoadAndTestAsCmp(MI)) && 6990b57cec5SDimitry Andric (optimizeCompareZero(MI, CCUsers) || 7000b57cec5SDimitry Andric fuseCompareOperations(MI, CCUsers))) { 7010b57cec5SDimitry Andric ++MBBI; 7020b57cec5SDimitry Andric MI.eraseFromParent(); 7030b57cec5SDimitry Andric Changed = true; 7040b57cec5SDimitry Andric CCUsers.clear(); 7050b57cec5SDimitry Andric continue; 7060b57cec5SDimitry Andric } 7070b57cec5SDimitry Andric 708*0fca6ea1SDimitry Andric if (MI.definesRegister(SystemZ::CC, /*TRI=*/nullptr)) { 7090b57cec5SDimitry Andric CCUsers.clear(); 7100b57cec5SDimitry Andric CompleteCCUsers = true; 7110b57cec5SDimitry Andric } 712*0fca6ea1SDimitry Andric if (MI.readsRegister(SystemZ::CC, /*TRI=*/nullptr) && CompleteCCUsers) 7130b57cec5SDimitry Andric CCUsers.push_back(&MI); 7140b57cec5SDimitry Andric } 7150b57cec5SDimitry Andric return Changed; 7160b57cec5SDimitry Andric } 7170b57cec5SDimitry Andric 7180b57cec5SDimitry Andric bool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) { 7190b57cec5SDimitry Andric if (skipFunction(F.getFunction())) 7200b57cec5SDimitry Andric return false; 7210b57cec5SDimitry Andric 72281ad6265SDimitry Andric TII = F.getSubtarget<SystemZSubtarget>().getInstrInfo(); 7230b57cec5SDimitry Andric TRI = &TII->getRegisterInfo(); 7240b57cec5SDimitry Andric 7250b57cec5SDimitry Andric bool Changed = false; 7260b57cec5SDimitry Andric for (auto &MBB : F) 7270b57cec5SDimitry Andric Changed |= processBlock(MBB); 7280b57cec5SDimitry Andric 7290b57cec5SDimitry Andric return Changed; 7300b57cec5SDimitry Andric } 7310b57cec5SDimitry Andric 7320b57cec5SDimitry Andric FunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) { 73304eeddc0SDimitry Andric return new SystemZElimCompare(); 7340b57cec5SDimitry Andric } 735