xref: /openbsd-src/gnu/llvm/llvm/lib/Target/SystemZ/SystemZElimCompare.cpp (revision d415bd752c734aee168c4ee86ff32e8cc249eb16)
109467b48Spatrick //===-- SystemZElimCompare.cpp - Eliminate comparison instructions --------===//
209467b48Spatrick //
309467b48Spatrick // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
409467b48Spatrick // See https://llvm.org/LICENSE.txt for license information.
509467b48Spatrick // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
609467b48Spatrick //
709467b48Spatrick //===----------------------------------------------------------------------===//
809467b48Spatrick //
909467b48Spatrick // This pass:
1009467b48Spatrick // (1) tries to remove compares if CC already contains the required information
1109467b48Spatrick // (2) fuses compares and branches into COMPARE AND BRANCH instructions
1209467b48Spatrick //
1309467b48Spatrick //===----------------------------------------------------------------------===//
1409467b48Spatrick 
1509467b48Spatrick #include "SystemZ.h"
1609467b48Spatrick #include "SystemZInstrInfo.h"
1709467b48Spatrick #include "SystemZTargetMachine.h"
1809467b48Spatrick #include "llvm/ADT/SmallVector.h"
1909467b48Spatrick #include "llvm/ADT/Statistic.h"
2009467b48Spatrick #include "llvm/ADT/StringRef.h"
2109467b48Spatrick #include "llvm/CodeGen/LivePhysRegs.h"
2209467b48Spatrick #include "llvm/CodeGen/MachineBasicBlock.h"
2309467b48Spatrick #include "llvm/CodeGen/MachineFunction.h"
2409467b48Spatrick #include "llvm/CodeGen/MachineFunctionPass.h"
2509467b48Spatrick #include "llvm/CodeGen/MachineInstr.h"
2609467b48Spatrick #include "llvm/CodeGen/MachineInstrBuilder.h"
2709467b48Spatrick #include "llvm/CodeGen/MachineOperand.h"
2809467b48Spatrick #include "llvm/CodeGen/TargetRegisterInfo.h"
2909467b48Spatrick #include "llvm/CodeGen/TargetSubtargetInfo.h"
3009467b48Spatrick #include "llvm/MC/MCInstrDesc.h"
3109467b48Spatrick #include <cassert>
3209467b48Spatrick #include <cstdint>
3309467b48Spatrick 
3409467b48Spatrick using namespace llvm;
3509467b48Spatrick 
3609467b48Spatrick #define DEBUG_TYPE "systemz-elim-compare"
3709467b48Spatrick 
3809467b48Spatrick STATISTIC(BranchOnCounts, "Number of branch-on-count instructions");
3909467b48Spatrick STATISTIC(LoadAndTraps, "Number of load-and-trap instructions");
4009467b48Spatrick STATISTIC(EliminatedComparisons, "Number of eliminated comparisons");
4109467b48Spatrick STATISTIC(FusedComparisons, "Number of fused compare-and-branch instructions");
4209467b48Spatrick 
4309467b48Spatrick namespace {
4409467b48Spatrick 
4509467b48Spatrick // Represents the references to a particular register in one or more
4609467b48Spatrick // instructions.
4709467b48Spatrick struct Reference {
4809467b48Spatrick   Reference() = default;
4909467b48Spatrick 
operator |=__anonc14f1bd80111::Reference5009467b48Spatrick   Reference &operator|=(const Reference &Other) {
5109467b48Spatrick     Def |= Other.Def;
5209467b48Spatrick     Use |= Other.Use;
5309467b48Spatrick     return *this;
5409467b48Spatrick   }
5509467b48Spatrick 
operator bool__anonc14f1bd80111::Reference5609467b48Spatrick   explicit operator bool() const { return Def || Use; }
5709467b48Spatrick 
5809467b48Spatrick   // True if the register is defined or used in some form, either directly or
5909467b48Spatrick   // via a sub- or super-register.
6009467b48Spatrick   bool Def = false;
6109467b48Spatrick   bool Use = false;
6209467b48Spatrick };
6309467b48Spatrick 
6409467b48Spatrick class SystemZElimCompare : public MachineFunctionPass {
6509467b48Spatrick public:
6609467b48Spatrick   static char ID;
6709467b48Spatrick 
SystemZElimCompare()68*d415bd75Srobert   SystemZElimCompare() : MachineFunctionPass(ID) {
69*d415bd75Srobert     initializeSystemZElimComparePass(*PassRegistry::getPassRegistry());
7009467b48Spatrick   }
7109467b48Spatrick 
7209467b48Spatrick   bool processBlock(MachineBasicBlock &MBB);
7309467b48Spatrick   bool runOnMachineFunction(MachineFunction &F) override;
7409467b48Spatrick 
getRequiredProperties() const7509467b48Spatrick   MachineFunctionProperties getRequiredProperties() const override {
7609467b48Spatrick     return MachineFunctionProperties().set(
7709467b48Spatrick         MachineFunctionProperties::Property::NoVRegs);
7809467b48Spatrick   }
7909467b48Spatrick 
8009467b48Spatrick private:
8109467b48Spatrick   Reference getRegReferences(MachineInstr &MI, unsigned Reg);
8209467b48Spatrick   bool convertToBRCT(MachineInstr &MI, MachineInstr &Compare,
8309467b48Spatrick                      SmallVectorImpl<MachineInstr *> &CCUsers);
8409467b48Spatrick   bool convertToLoadAndTrap(MachineInstr &MI, MachineInstr &Compare,
8509467b48Spatrick                             SmallVectorImpl<MachineInstr *> &CCUsers);
8609467b48Spatrick   bool convertToLoadAndTest(MachineInstr &MI, MachineInstr &Compare,
8709467b48Spatrick                             SmallVectorImpl<MachineInstr *> &CCUsers);
8809467b48Spatrick   bool convertToLogical(MachineInstr &MI, MachineInstr &Compare,
8909467b48Spatrick                         SmallVectorImpl<MachineInstr *> &CCUsers);
9009467b48Spatrick   bool adjustCCMasksForInstr(MachineInstr &MI, MachineInstr &Compare,
9109467b48Spatrick                              SmallVectorImpl<MachineInstr *> &CCUsers,
9209467b48Spatrick                              unsigned ConvOpc = 0);
9309467b48Spatrick   bool optimizeCompareZero(MachineInstr &Compare,
9409467b48Spatrick                            SmallVectorImpl<MachineInstr *> &CCUsers);
9509467b48Spatrick   bool fuseCompareOperations(MachineInstr &Compare,
9609467b48Spatrick                              SmallVectorImpl<MachineInstr *> &CCUsers);
9709467b48Spatrick 
9809467b48Spatrick   const SystemZInstrInfo *TII = nullptr;
9909467b48Spatrick   const TargetRegisterInfo *TRI = nullptr;
10009467b48Spatrick };
10109467b48Spatrick 
10209467b48Spatrick char SystemZElimCompare::ID = 0;
10309467b48Spatrick 
10409467b48Spatrick } // end anonymous namespace
10509467b48Spatrick 
106*d415bd75Srobert INITIALIZE_PASS(SystemZElimCompare, DEBUG_TYPE,
107*d415bd75Srobert                 "SystemZ Comparison Elimination", false, false)
108*d415bd75Srobert 
10909467b48Spatrick // Returns true if MI is an instruction whose output equals the value in Reg.
preservesValueOf(MachineInstr & MI,unsigned Reg)11009467b48Spatrick static bool preservesValueOf(MachineInstr &MI, unsigned Reg) {
11109467b48Spatrick   switch (MI.getOpcode()) {
11209467b48Spatrick   case SystemZ::LR:
11309467b48Spatrick   case SystemZ::LGR:
11409467b48Spatrick   case SystemZ::LGFR:
11509467b48Spatrick   case SystemZ::LTR:
11609467b48Spatrick   case SystemZ::LTGR:
11709467b48Spatrick   case SystemZ::LTGFR:
11809467b48Spatrick   case SystemZ::LER:
11909467b48Spatrick   case SystemZ::LDR:
12009467b48Spatrick   case SystemZ::LXR:
12109467b48Spatrick   case SystemZ::LTEBR:
12209467b48Spatrick   case SystemZ::LTDBR:
12309467b48Spatrick   case SystemZ::LTXBR:
12409467b48Spatrick     if (MI.getOperand(1).getReg() == Reg)
12509467b48Spatrick       return true;
12609467b48Spatrick   }
12709467b48Spatrick 
12809467b48Spatrick   return false;
12909467b48Spatrick }
13009467b48Spatrick 
13109467b48Spatrick // Return true if any CC result of MI would (perhaps after conversion)
13209467b48Spatrick // reflect the value of Reg.
resultTests(MachineInstr & MI,unsigned Reg)13309467b48Spatrick static bool resultTests(MachineInstr &MI, unsigned Reg) {
13409467b48Spatrick   if (MI.getNumOperands() > 0 && MI.getOperand(0).isReg() &&
13509467b48Spatrick       MI.getOperand(0).isDef() && MI.getOperand(0).getReg() == Reg)
13609467b48Spatrick     return true;
13709467b48Spatrick 
13809467b48Spatrick   return (preservesValueOf(MI, Reg));
13909467b48Spatrick }
14009467b48Spatrick 
14109467b48Spatrick // Describe the references to Reg or any of its aliases in MI.
getRegReferences(MachineInstr & MI,unsigned Reg)14209467b48Spatrick Reference SystemZElimCompare::getRegReferences(MachineInstr &MI, unsigned Reg) {
14309467b48Spatrick   Reference Ref;
14409467b48Spatrick   if (MI.isDebugInstr())
14509467b48Spatrick     return Ref;
14609467b48Spatrick 
147*d415bd75Srobert   for (const MachineOperand &MO : MI.operands()) {
14809467b48Spatrick     if (MO.isReg()) {
14909467b48Spatrick       if (Register MOReg = MO.getReg()) {
15009467b48Spatrick         if (TRI->regsOverlap(MOReg, Reg)) {
15109467b48Spatrick           if (MO.isUse())
15209467b48Spatrick             Ref.Use = true;
15309467b48Spatrick           else if (MO.isDef())
15409467b48Spatrick             Ref.Def = true;
15509467b48Spatrick         }
15609467b48Spatrick       }
15709467b48Spatrick     }
15809467b48Spatrick   }
15909467b48Spatrick   return Ref;
16009467b48Spatrick }
16109467b48Spatrick 
16209467b48Spatrick // Return true if this is a load and test which can be optimized the
16309467b48Spatrick // same way as compare instruction.
isLoadAndTestAsCmp(MachineInstr & MI)16409467b48Spatrick static bool isLoadAndTestAsCmp(MachineInstr &MI) {
16509467b48Spatrick   // If we during isel used a load-and-test as a compare with 0, the
16609467b48Spatrick   // def operand is dead.
16709467b48Spatrick   return (MI.getOpcode() == SystemZ::LTEBR ||
16809467b48Spatrick           MI.getOpcode() == SystemZ::LTDBR ||
16909467b48Spatrick           MI.getOpcode() == SystemZ::LTXBR) &&
17009467b48Spatrick          MI.getOperand(0).isDead();
17109467b48Spatrick }
17209467b48Spatrick 
17309467b48Spatrick // Return the source register of Compare, which is the unknown value
17409467b48Spatrick // being tested.
getCompareSourceReg(MachineInstr & Compare)17509467b48Spatrick static unsigned getCompareSourceReg(MachineInstr &Compare) {
17609467b48Spatrick   unsigned reg = 0;
17709467b48Spatrick   if (Compare.isCompare())
17809467b48Spatrick     reg = Compare.getOperand(0).getReg();
17909467b48Spatrick   else if (isLoadAndTestAsCmp(Compare))
18009467b48Spatrick     reg = Compare.getOperand(1).getReg();
18109467b48Spatrick   assert(reg);
18209467b48Spatrick 
18309467b48Spatrick   return reg;
18409467b48Spatrick }
18509467b48Spatrick 
18609467b48Spatrick // Compare compares the result of MI against zero.  If MI is an addition
18709467b48Spatrick // of -1 and if CCUsers is a single branch on nonzero, eliminate the addition
18809467b48Spatrick // and convert the branch to a BRCT(G) or BRCTH.  Return true on success.
convertToBRCT(MachineInstr & MI,MachineInstr & Compare,SmallVectorImpl<MachineInstr * > & CCUsers)18909467b48Spatrick bool SystemZElimCompare::convertToBRCT(
19009467b48Spatrick     MachineInstr &MI, MachineInstr &Compare,
19109467b48Spatrick     SmallVectorImpl<MachineInstr *> &CCUsers) {
19209467b48Spatrick   // Check whether we have an addition of -1.
19309467b48Spatrick   unsigned Opcode = MI.getOpcode();
19409467b48Spatrick   unsigned BRCT;
19509467b48Spatrick   if (Opcode == SystemZ::AHI)
19609467b48Spatrick     BRCT = SystemZ::BRCT;
19709467b48Spatrick   else if (Opcode == SystemZ::AGHI)
19809467b48Spatrick     BRCT = SystemZ::BRCTG;
19909467b48Spatrick   else if (Opcode == SystemZ::AIH)
20009467b48Spatrick     BRCT = SystemZ::BRCTH;
20109467b48Spatrick   else
20209467b48Spatrick     return false;
20309467b48Spatrick   if (MI.getOperand(2).getImm() != -1)
20409467b48Spatrick     return false;
20509467b48Spatrick 
20609467b48Spatrick   // Check whether we have a single JLH.
20709467b48Spatrick   if (CCUsers.size() != 1)
20809467b48Spatrick     return false;
20909467b48Spatrick   MachineInstr *Branch = CCUsers[0];
21009467b48Spatrick   if (Branch->getOpcode() != SystemZ::BRC ||
21109467b48Spatrick       Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP ||
21209467b48Spatrick       Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_NE)
21309467b48Spatrick     return false;
21409467b48Spatrick 
21509467b48Spatrick   // We already know that there are no references to the register between
21609467b48Spatrick   // MI and Compare.  Make sure that there are also no references between
21709467b48Spatrick   // Compare and Branch.
21809467b48Spatrick   unsigned SrcReg = getCompareSourceReg(Compare);
21909467b48Spatrick   MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
22009467b48Spatrick   for (++MBBI; MBBI != MBBE; ++MBBI)
22109467b48Spatrick     if (getRegReferences(*MBBI, SrcReg))
22209467b48Spatrick       return false;
22309467b48Spatrick 
22409467b48Spatrick   // The transformation is OK.  Rebuild Branch as a BRCT(G) or BRCTH.
22509467b48Spatrick   MachineOperand Target(Branch->getOperand(2));
22609467b48Spatrick   while (Branch->getNumOperands())
227*d415bd75Srobert     Branch->removeOperand(0);
22809467b48Spatrick   Branch->setDesc(TII->get(BRCT));
22909467b48Spatrick   MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch);
23009467b48Spatrick   MIB.add(MI.getOperand(0)).add(MI.getOperand(1)).add(Target);
23109467b48Spatrick   // Add a CC def to BRCT(G), since we may have to split them again if the
23209467b48Spatrick   // branch displacement overflows.  BRCTH has a 32-bit displacement, so
23309467b48Spatrick   // this is not necessary there.
23409467b48Spatrick   if (BRCT != SystemZ::BRCTH)
23509467b48Spatrick     MIB.addReg(SystemZ::CC, RegState::ImplicitDefine | RegState::Dead);
23609467b48Spatrick   MI.eraseFromParent();
23709467b48Spatrick   return true;
23809467b48Spatrick }
23909467b48Spatrick 
24009467b48Spatrick // Compare compares the result of MI against zero.  If MI is a suitable load
24109467b48Spatrick // instruction and if CCUsers is a single conditional trap on zero, eliminate
24209467b48Spatrick // the load and convert the branch to a load-and-trap.  Return true on success.
convertToLoadAndTrap(MachineInstr & MI,MachineInstr & Compare,SmallVectorImpl<MachineInstr * > & CCUsers)24309467b48Spatrick bool SystemZElimCompare::convertToLoadAndTrap(
24409467b48Spatrick     MachineInstr &MI, MachineInstr &Compare,
24509467b48Spatrick     SmallVectorImpl<MachineInstr *> &CCUsers) {
24609467b48Spatrick   unsigned LATOpcode = TII->getLoadAndTrap(MI.getOpcode());
24709467b48Spatrick   if (!LATOpcode)
24809467b48Spatrick     return false;
24909467b48Spatrick 
25009467b48Spatrick   // Check whether we have a single CondTrap that traps on zero.
25109467b48Spatrick   if (CCUsers.size() != 1)
25209467b48Spatrick     return false;
25309467b48Spatrick   MachineInstr *Branch = CCUsers[0];
25409467b48Spatrick   if (Branch->getOpcode() != SystemZ::CondTrap ||
25509467b48Spatrick       Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP ||
25609467b48Spatrick       Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_EQ)
25709467b48Spatrick     return false;
25809467b48Spatrick 
25909467b48Spatrick   // We already know that there are no references to the register between
26009467b48Spatrick   // MI and Compare.  Make sure that there are also no references between
26109467b48Spatrick   // Compare and Branch.
26209467b48Spatrick   unsigned SrcReg = getCompareSourceReg(Compare);
26309467b48Spatrick   MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
26409467b48Spatrick   for (++MBBI; MBBI != MBBE; ++MBBI)
26509467b48Spatrick     if (getRegReferences(*MBBI, SrcReg))
26609467b48Spatrick       return false;
26709467b48Spatrick 
26809467b48Spatrick   // The transformation is OK.  Rebuild Branch as a load-and-trap.
26909467b48Spatrick   while (Branch->getNumOperands())
270*d415bd75Srobert     Branch->removeOperand(0);
27109467b48Spatrick   Branch->setDesc(TII->get(LATOpcode));
27209467b48Spatrick   MachineInstrBuilder(*Branch->getParent()->getParent(), Branch)
27309467b48Spatrick       .add(MI.getOperand(0))
27409467b48Spatrick       .add(MI.getOperand(1))
27509467b48Spatrick       .add(MI.getOperand(2))
27609467b48Spatrick       .add(MI.getOperand(3));
27709467b48Spatrick   MI.eraseFromParent();
27809467b48Spatrick   return true;
27909467b48Spatrick }
28009467b48Spatrick 
28109467b48Spatrick // If MI is a load instruction, try to convert it into a LOAD AND TEST.
28209467b48Spatrick // Return true on success.
convertToLoadAndTest(MachineInstr & MI,MachineInstr & Compare,SmallVectorImpl<MachineInstr * > & CCUsers)28309467b48Spatrick bool SystemZElimCompare::convertToLoadAndTest(
28409467b48Spatrick     MachineInstr &MI, MachineInstr &Compare,
28509467b48Spatrick     SmallVectorImpl<MachineInstr *> &CCUsers) {
28609467b48Spatrick 
28709467b48Spatrick   // Try to adjust CC masks for the LOAD AND TEST opcode that could replace MI.
28809467b48Spatrick   unsigned Opcode = TII->getLoadAndTest(MI.getOpcode());
28909467b48Spatrick   if (!Opcode || !adjustCCMasksForInstr(MI, Compare, CCUsers, Opcode))
29009467b48Spatrick     return false;
29109467b48Spatrick 
29209467b48Spatrick   // Rebuild to get the CC operand in the right place.
29309467b48Spatrick   auto MIB = BuildMI(*MI.getParent(), MI, MI.getDebugLoc(), TII->get(Opcode));
29409467b48Spatrick   for (const auto &MO : MI.operands())
29509467b48Spatrick     MIB.add(MO);
29609467b48Spatrick   MIB.setMemRefs(MI.memoperands());
29709467b48Spatrick   MI.eraseFromParent();
29809467b48Spatrick 
29909467b48Spatrick   // Mark instruction as not raising an FP exception if applicable.  We already
30009467b48Spatrick   // verified earlier that this move is valid.
30109467b48Spatrick   if (!Compare.mayRaiseFPException())
30209467b48Spatrick     MIB.setMIFlag(MachineInstr::MIFlag::NoFPExcept);
30309467b48Spatrick 
30409467b48Spatrick   return true;
30509467b48Spatrick }
30609467b48Spatrick 
30709467b48Spatrick // See if MI is an instruction with an equivalent "logical" opcode that can
30809467b48Spatrick // be used and replace MI. This is useful for EQ/NE comparisons where the
30909467b48Spatrick // "nsw" flag is missing since the "logical" opcode always sets CC to reflect
31009467b48Spatrick // the result being zero or non-zero.
convertToLogical(MachineInstr & MI,MachineInstr & Compare,SmallVectorImpl<MachineInstr * > & CCUsers)31109467b48Spatrick bool SystemZElimCompare::convertToLogical(
31209467b48Spatrick     MachineInstr &MI, MachineInstr &Compare,
31309467b48Spatrick     SmallVectorImpl<MachineInstr *> &CCUsers) {
31409467b48Spatrick 
31509467b48Spatrick   unsigned ConvOpc = 0;
31609467b48Spatrick   switch (MI.getOpcode()) {
31709467b48Spatrick   case SystemZ::AR:   ConvOpc = SystemZ::ALR;   break;
31809467b48Spatrick   case SystemZ::ARK:  ConvOpc = SystemZ::ALRK;  break;
31909467b48Spatrick   case SystemZ::AGR:  ConvOpc = SystemZ::ALGR;  break;
32009467b48Spatrick   case SystemZ::AGRK: ConvOpc = SystemZ::ALGRK; break;
32109467b48Spatrick   case SystemZ::A:    ConvOpc = SystemZ::AL;    break;
32209467b48Spatrick   case SystemZ::AY:   ConvOpc = SystemZ::ALY;   break;
32309467b48Spatrick   case SystemZ::AG:   ConvOpc = SystemZ::ALG;   break;
32409467b48Spatrick   default: break;
32509467b48Spatrick   }
32609467b48Spatrick   if (!ConvOpc || !adjustCCMasksForInstr(MI, Compare, CCUsers, ConvOpc))
32709467b48Spatrick     return false;
32809467b48Spatrick 
32909467b48Spatrick   // Operands should be identical, so just change the opcode and remove the
33009467b48Spatrick   // dead flag on CC.
33109467b48Spatrick   MI.setDesc(TII->get(ConvOpc));
33209467b48Spatrick   MI.clearRegisterDeads(SystemZ::CC);
33309467b48Spatrick   return true;
33409467b48Spatrick }
33509467b48Spatrick 
33609467b48Spatrick #ifndef NDEBUG
isAddWithImmediate(unsigned Opcode)33709467b48Spatrick static bool isAddWithImmediate(unsigned Opcode) {
33809467b48Spatrick   switch(Opcode) {
33909467b48Spatrick   case SystemZ::AHI:
34009467b48Spatrick   case SystemZ::AHIK:
34109467b48Spatrick   case SystemZ::AGHI:
34209467b48Spatrick   case SystemZ::AGHIK:
34309467b48Spatrick   case SystemZ::AFI:
34409467b48Spatrick   case SystemZ::AIH:
34509467b48Spatrick   case SystemZ::AGFI:
34609467b48Spatrick     return true;
34709467b48Spatrick   default: break;
34809467b48Spatrick   }
34909467b48Spatrick   return false;
35009467b48Spatrick }
35109467b48Spatrick #endif
35209467b48Spatrick 
35309467b48Spatrick // The CC users in CCUsers are testing the result of a comparison of some
35409467b48Spatrick // value X against zero and we know that any CC value produced by MI would
35509467b48Spatrick // also reflect the value of X.  ConvOpc may be used to pass the transfomed
35609467b48Spatrick // opcode MI will have if this succeeds.  Try to adjust CCUsers so that they
35709467b48Spatrick // test the result of MI directly, returning true on success.  Leave
35809467b48Spatrick // everything unchanged on failure.
adjustCCMasksForInstr(MachineInstr & MI,MachineInstr & Compare,SmallVectorImpl<MachineInstr * > & CCUsers,unsigned ConvOpc)35909467b48Spatrick bool SystemZElimCompare::adjustCCMasksForInstr(
36009467b48Spatrick     MachineInstr &MI, MachineInstr &Compare,
36109467b48Spatrick     SmallVectorImpl<MachineInstr *> &CCUsers,
36209467b48Spatrick     unsigned ConvOpc) {
36309467b48Spatrick   unsigned CompareFlags = Compare.getDesc().TSFlags;
36409467b48Spatrick   unsigned CompareCCValues = SystemZII::getCCValues(CompareFlags);
36509467b48Spatrick   int Opcode = (ConvOpc ? ConvOpc : MI.getOpcode());
36609467b48Spatrick   const MCInstrDesc &Desc = TII->get(Opcode);
36709467b48Spatrick   unsigned MIFlags = Desc.TSFlags;
36809467b48Spatrick 
36909467b48Spatrick   // If Compare may raise an FP exception, we can only eliminate it
37009467b48Spatrick   // if MI itself would have already raised the exception.
37109467b48Spatrick   if (Compare.mayRaiseFPException()) {
37209467b48Spatrick     // If the caller will change MI to use ConvOpc, only test whether
37309467b48Spatrick     // ConvOpc is suitable; it is on the caller to set the MI flag.
37409467b48Spatrick     if (ConvOpc && !Desc.mayRaiseFPException())
37509467b48Spatrick       return false;
37609467b48Spatrick     // If the caller will not change MI, we test the MI flag here.
37709467b48Spatrick     if (!ConvOpc && !MI.mayRaiseFPException())
37809467b48Spatrick       return false;
37909467b48Spatrick   }
38009467b48Spatrick 
38109467b48Spatrick   // See which compare-style condition codes are available.
38209467b48Spatrick   unsigned CCValues = SystemZII::getCCValues(MIFlags);
38309467b48Spatrick   unsigned ReusableCCMask = CCValues;
38409467b48Spatrick   // For unsigned comparisons with zero, only equality makes sense.
38509467b48Spatrick   if (CompareFlags & SystemZII::IsLogical)
38609467b48Spatrick     ReusableCCMask &= SystemZ::CCMASK_CMP_EQ;
38709467b48Spatrick   unsigned OFImplies = 0;
38809467b48Spatrick   bool LogicalMI = false;
38909467b48Spatrick   bool MIEquivalentToCmp = false;
39009467b48Spatrick   if (MI.getFlag(MachineInstr::NoSWrap) &&
39109467b48Spatrick       (MIFlags & SystemZII::CCIfNoSignedWrap)) {
39209467b48Spatrick     // If MI has the NSW flag set in combination with the
39309467b48Spatrick     // SystemZII::CCIfNoSignedWrap flag, all CCValues are valid.
39409467b48Spatrick   }
39509467b48Spatrick   else if ((MIFlags & SystemZII::CCIfNoSignedWrap) &&
39609467b48Spatrick            MI.getOperand(2).isImm()) {
39709467b48Spatrick     // Signed addition of immediate. If adding a positive immediate
39809467b48Spatrick     // overflows, the result must be less than zero. If adding a negative
39909467b48Spatrick     // immediate overflows, the result must be larger than zero (except in
40009467b48Spatrick     // the special case of adding the minimum value of the result range, in
40109467b48Spatrick     // which case we cannot predict whether the result is larger than or
40209467b48Spatrick     // equal to zero).
40309467b48Spatrick     assert(isAddWithImmediate(Opcode) && "Expected an add with immediate.");
40409467b48Spatrick     assert(!MI.mayLoadOrStore() && "Expected an immediate term.");
40509467b48Spatrick     int64_t RHS = MI.getOperand(2).getImm();
40609467b48Spatrick     if (SystemZ::GRX32BitRegClass.contains(MI.getOperand(0).getReg()) &&
40709467b48Spatrick         RHS == INT32_MIN)
40809467b48Spatrick       return false;
40909467b48Spatrick     OFImplies = (RHS > 0 ? SystemZ::CCMASK_CMP_LT : SystemZ::CCMASK_CMP_GT);
41009467b48Spatrick   }
41109467b48Spatrick   else if ((MIFlags & SystemZII::IsLogical) && CCValues) {
41209467b48Spatrick     // Use CCMASK_CMP_EQ to match with CCUsers. On success CCMask:s will be
41309467b48Spatrick     // converted to CCMASK_LOGICAL_ZERO or CCMASK_LOGICAL_NONZERO.
41409467b48Spatrick     LogicalMI = true;
41509467b48Spatrick     ReusableCCMask = SystemZ::CCMASK_CMP_EQ;
41609467b48Spatrick   }
41709467b48Spatrick   else {
41809467b48Spatrick     ReusableCCMask &= SystemZII::getCompareZeroCCMask(MIFlags);
41909467b48Spatrick     assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues");
42009467b48Spatrick     MIEquivalentToCmp =
42109467b48Spatrick       ReusableCCMask == CCValues && CCValues == CompareCCValues;
42209467b48Spatrick   }
42309467b48Spatrick   if (ReusableCCMask == 0)
42409467b48Spatrick     return false;
42509467b48Spatrick 
42609467b48Spatrick   if (!MIEquivalentToCmp) {
42709467b48Spatrick     // Now check whether these flags are enough for all users.
42809467b48Spatrick     SmallVector<MachineOperand *, 4> AlterMasks;
42909467b48Spatrick     for (unsigned int I = 0, E = CCUsers.size(); I != E; ++I) {
43009467b48Spatrick       MachineInstr *CCUserMI = CCUsers[I];
43109467b48Spatrick 
43209467b48Spatrick       // Fail if this isn't a use of CC that we understand.
43309467b48Spatrick       unsigned Flags = CCUserMI->getDesc().TSFlags;
43409467b48Spatrick       unsigned FirstOpNum;
43509467b48Spatrick       if (Flags & SystemZII::CCMaskFirst)
43609467b48Spatrick         FirstOpNum = 0;
43709467b48Spatrick       else if (Flags & SystemZII::CCMaskLast)
43809467b48Spatrick         FirstOpNum = CCUserMI->getNumExplicitOperands() - 2;
43909467b48Spatrick       else
44009467b48Spatrick         return false;
44109467b48Spatrick 
44209467b48Spatrick       // Check whether the instruction predicate treats all CC values
44309467b48Spatrick       // outside of ReusableCCMask in the same way.  In that case it
44409467b48Spatrick       // doesn't matter what those CC values mean.
44509467b48Spatrick       unsigned CCValid = CCUserMI->getOperand(FirstOpNum).getImm();
44609467b48Spatrick       unsigned CCMask = CCUserMI->getOperand(FirstOpNum + 1).getImm();
44709467b48Spatrick       assert(CCValid == CompareCCValues && (CCMask & ~CCValid) == 0 &&
44809467b48Spatrick              "Corrupt CC operands of CCUser.");
44909467b48Spatrick       unsigned OutValid = ~ReusableCCMask & CCValid;
45009467b48Spatrick       unsigned OutMask = ~ReusableCCMask & CCMask;
45109467b48Spatrick       if (OutMask != 0 && OutMask != OutValid)
45209467b48Spatrick         return false;
45309467b48Spatrick 
45409467b48Spatrick       AlterMasks.push_back(&CCUserMI->getOperand(FirstOpNum));
45509467b48Spatrick       AlterMasks.push_back(&CCUserMI->getOperand(FirstOpNum + 1));
45609467b48Spatrick     }
45709467b48Spatrick 
45809467b48Spatrick     // All users are OK.  Adjust the masks for MI.
45909467b48Spatrick     for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) {
46009467b48Spatrick       AlterMasks[I]->setImm(CCValues);
46109467b48Spatrick       unsigned CCMask = AlterMasks[I + 1]->getImm();
46209467b48Spatrick       if (LogicalMI) {
46309467b48Spatrick         // Translate the CCMask into its "logical" value.
46409467b48Spatrick         CCMask = (CCMask == SystemZ::CCMASK_CMP_EQ ?
46509467b48Spatrick                   SystemZ::CCMASK_LOGICAL_ZERO : SystemZ::CCMASK_LOGICAL_NONZERO);
46609467b48Spatrick         CCMask &= CCValues; // Logical subtracts never set CC=0.
46709467b48Spatrick       } else {
46809467b48Spatrick         if (CCMask & ~ReusableCCMask)
46909467b48Spatrick           CCMask = (CCMask & ReusableCCMask) | (CCValues & ~ReusableCCMask);
47009467b48Spatrick         CCMask |= (CCMask & OFImplies) ? SystemZ::CCMASK_ARITH_OVERFLOW : 0;
47109467b48Spatrick       }
47209467b48Spatrick       AlterMasks[I + 1]->setImm(CCMask);
47309467b48Spatrick     }
47409467b48Spatrick   }
47509467b48Spatrick 
47609467b48Spatrick   // CC is now live after MI.
47709467b48Spatrick   if (!ConvOpc)
47809467b48Spatrick     MI.clearRegisterDeads(SystemZ::CC);
47909467b48Spatrick 
48009467b48Spatrick   // Check if MI lies before Compare.
48109467b48Spatrick   bool BeforeCmp = false;
48209467b48Spatrick   MachineBasicBlock::iterator MBBI = MI, MBBE = MI.getParent()->end();
48309467b48Spatrick   for (++MBBI; MBBI != MBBE; ++MBBI)
48409467b48Spatrick     if (MBBI == Compare) {
48509467b48Spatrick       BeforeCmp = true;
48609467b48Spatrick       break;
48709467b48Spatrick     }
48809467b48Spatrick 
48909467b48Spatrick   // Clear any intervening kills of CC.
49009467b48Spatrick   if (BeforeCmp) {
49109467b48Spatrick     MachineBasicBlock::iterator MBBI = MI, MBBE = Compare;
49209467b48Spatrick     for (++MBBI; MBBI != MBBE; ++MBBI)
49309467b48Spatrick       MBBI->clearRegisterKills(SystemZ::CC, TRI);
49409467b48Spatrick   }
49509467b48Spatrick 
49609467b48Spatrick   return true;
49709467b48Spatrick }
49809467b48Spatrick 
49909467b48Spatrick // Return true if Compare is a comparison against zero.
isCompareZero(MachineInstr & Compare)50009467b48Spatrick static bool isCompareZero(MachineInstr &Compare) {
50109467b48Spatrick   switch (Compare.getOpcode()) {
50209467b48Spatrick   case SystemZ::LTEBRCompare:
50309467b48Spatrick   case SystemZ::LTDBRCompare:
50409467b48Spatrick   case SystemZ::LTXBRCompare:
50509467b48Spatrick     return true;
50609467b48Spatrick 
50709467b48Spatrick   default:
50809467b48Spatrick     if (isLoadAndTestAsCmp(Compare))
50909467b48Spatrick       return true;
51009467b48Spatrick     return Compare.getNumExplicitOperands() == 2 &&
51109467b48Spatrick            Compare.getOperand(1).isImm() && Compare.getOperand(1).getImm() == 0;
51209467b48Spatrick   }
51309467b48Spatrick }
51409467b48Spatrick 
51509467b48Spatrick // Try to optimize cases where comparison instruction Compare is testing
51609467b48Spatrick // a value against zero.  Return true on success and if Compare should be
51709467b48Spatrick // deleted as dead.  CCUsers is the list of instructions that use the CC
51809467b48Spatrick // value produced by Compare.
optimizeCompareZero(MachineInstr & Compare,SmallVectorImpl<MachineInstr * > & CCUsers)51909467b48Spatrick bool SystemZElimCompare::optimizeCompareZero(
52009467b48Spatrick     MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
52109467b48Spatrick   if (!isCompareZero(Compare))
52209467b48Spatrick     return false;
52309467b48Spatrick 
52409467b48Spatrick   // Search back for CC results that are based on the first operand.
52509467b48Spatrick   unsigned SrcReg = getCompareSourceReg(Compare);
52609467b48Spatrick   MachineBasicBlock &MBB = *Compare.getParent();
52709467b48Spatrick   Reference CCRefs;
52809467b48Spatrick   Reference SrcRefs;
52909467b48Spatrick   for (MachineBasicBlock::reverse_iterator MBBI =
53009467b48Spatrick          std::next(MachineBasicBlock::reverse_iterator(&Compare)),
53109467b48Spatrick          MBBE = MBB.rend(); MBBI != MBBE;) {
53209467b48Spatrick     MachineInstr &MI = *MBBI++;
53309467b48Spatrick     if (resultTests(MI, SrcReg)) {
53409467b48Spatrick       // Try to remove both MI and Compare by converting a branch to BRCT(G).
53509467b48Spatrick       // or a load-and-trap instruction.  We don't care in this case whether
53609467b48Spatrick       // CC is modified between MI and Compare.
53709467b48Spatrick       if (!CCRefs.Use && !SrcRefs) {
53809467b48Spatrick         if (convertToBRCT(MI, Compare, CCUsers)) {
53909467b48Spatrick           BranchOnCounts += 1;
54009467b48Spatrick           return true;
54109467b48Spatrick         }
54209467b48Spatrick         if (convertToLoadAndTrap(MI, Compare, CCUsers)) {
54309467b48Spatrick           LoadAndTraps += 1;
54409467b48Spatrick           return true;
54509467b48Spatrick         }
54609467b48Spatrick       }
54709467b48Spatrick       // Try to eliminate Compare by reusing a CC result from MI.
54809467b48Spatrick       if ((!CCRefs && convertToLoadAndTest(MI, Compare, CCUsers)) ||
54909467b48Spatrick           (!CCRefs.Def &&
55009467b48Spatrick            (adjustCCMasksForInstr(MI, Compare, CCUsers) ||
55109467b48Spatrick             convertToLogical(MI, Compare, CCUsers)))) {
55209467b48Spatrick         EliminatedComparisons += 1;
55309467b48Spatrick         return true;
55409467b48Spatrick       }
55509467b48Spatrick     }
55609467b48Spatrick     SrcRefs |= getRegReferences(MI, SrcReg);
55709467b48Spatrick     if (SrcRefs.Def)
55809467b48Spatrick       break;
55909467b48Spatrick     CCRefs |= getRegReferences(MI, SystemZ::CC);
56009467b48Spatrick     if (CCRefs.Use && CCRefs.Def)
56109467b48Spatrick       break;
56209467b48Spatrick     // Eliminating a Compare that may raise an FP exception will move
56309467b48Spatrick     // raising the exception to some earlier MI.  We cannot do this if
56409467b48Spatrick     // there is anything in between that might change exception flags.
56509467b48Spatrick     if (Compare.mayRaiseFPException() &&
56609467b48Spatrick         (MI.isCall() || MI.hasUnmodeledSideEffects()))
56709467b48Spatrick       break;
56809467b48Spatrick   }
56909467b48Spatrick 
57009467b48Spatrick   // Also do a forward search to handle cases where an instruction after the
57109467b48Spatrick   // compare can be converted, like
57209467b48Spatrick   // LTEBRCompare %f0s, %f0s; %f2s = LER %f0s  =>  LTEBRCompare %f2s, %f0s
573*d415bd75Srobert   auto MIRange = llvm::make_range(
574*d415bd75Srobert       std::next(MachineBasicBlock::iterator(&Compare)), MBB.end());
575*d415bd75Srobert   for (MachineInstr &MI : llvm::make_early_inc_range(MIRange)) {
57609467b48Spatrick     if (preservesValueOf(MI, SrcReg)) {
57709467b48Spatrick       // Try to eliminate Compare by reusing a CC result from MI.
57809467b48Spatrick       if (convertToLoadAndTest(MI, Compare, CCUsers)) {
57909467b48Spatrick         EliminatedComparisons += 1;
58009467b48Spatrick         return true;
58109467b48Spatrick       }
58209467b48Spatrick     }
58309467b48Spatrick     if (getRegReferences(MI, SrcReg).Def)
58409467b48Spatrick       return false;
58509467b48Spatrick     if (getRegReferences(MI, SystemZ::CC))
58609467b48Spatrick       return false;
58709467b48Spatrick   }
58809467b48Spatrick 
58909467b48Spatrick   return false;
59009467b48Spatrick }
59109467b48Spatrick 
59209467b48Spatrick // Try to fuse comparison instruction Compare into a later branch.
59309467b48Spatrick // Return true on success and if Compare is therefore redundant.
fuseCompareOperations(MachineInstr & Compare,SmallVectorImpl<MachineInstr * > & CCUsers)59409467b48Spatrick bool SystemZElimCompare::fuseCompareOperations(
59509467b48Spatrick     MachineInstr &Compare, SmallVectorImpl<MachineInstr *> &CCUsers) {
59609467b48Spatrick   // See whether we have a single branch with which to fuse.
59709467b48Spatrick   if (CCUsers.size() != 1)
59809467b48Spatrick     return false;
59909467b48Spatrick   MachineInstr *Branch = CCUsers[0];
60009467b48Spatrick   SystemZII::FusedCompareType Type;
60109467b48Spatrick   switch (Branch->getOpcode()) {
60209467b48Spatrick   case SystemZ::BRC:
60309467b48Spatrick     Type = SystemZII::CompareAndBranch;
60409467b48Spatrick     break;
60509467b48Spatrick   case SystemZ::CondReturn:
60609467b48Spatrick     Type = SystemZII::CompareAndReturn;
60709467b48Spatrick     break;
60809467b48Spatrick   case SystemZ::CallBCR:
60909467b48Spatrick     Type = SystemZII::CompareAndSibcall;
61009467b48Spatrick     break;
61109467b48Spatrick   case SystemZ::CondTrap:
61209467b48Spatrick     Type = SystemZII::CompareAndTrap;
61309467b48Spatrick     break;
61409467b48Spatrick   default:
61509467b48Spatrick     return false;
61609467b48Spatrick   }
61709467b48Spatrick 
61809467b48Spatrick   // See whether we have a comparison that can be fused.
61909467b48Spatrick   unsigned FusedOpcode =
62009467b48Spatrick       TII->getFusedCompare(Compare.getOpcode(), Type, &Compare);
62109467b48Spatrick   if (!FusedOpcode)
62209467b48Spatrick     return false;
62309467b48Spatrick 
62409467b48Spatrick   // Make sure that the operands are available at the branch.
62509467b48Spatrick   // SrcReg2 is the register if the source operand is a register,
62609467b48Spatrick   // 0 if the source operand is immediate, and the base register
62709467b48Spatrick   // if the source operand is memory (index is not supported).
62809467b48Spatrick   Register SrcReg = Compare.getOperand(0).getReg();
62909467b48Spatrick   Register SrcReg2 =
63009467b48Spatrick     Compare.getOperand(1).isReg() ? Compare.getOperand(1).getReg() : Register();
63109467b48Spatrick   MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
63209467b48Spatrick   for (++MBBI; MBBI != MBBE; ++MBBI)
63309467b48Spatrick     if (MBBI->modifiesRegister(SrcReg, TRI) ||
63409467b48Spatrick         (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI)))
63509467b48Spatrick       return false;
63609467b48Spatrick 
63709467b48Spatrick   // Read the branch mask, target (if applicable), regmask (if applicable).
63809467b48Spatrick   MachineOperand CCMask(MBBI->getOperand(1));
63909467b48Spatrick   assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 &&
64009467b48Spatrick          "Invalid condition-code mask for integer comparison");
64173471bf0Spatrick   // This is only valid for CompareAndBranch and CompareAndSibcall.
64209467b48Spatrick   MachineOperand Target(MBBI->getOperand(
64373471bf0Spatrick     (Type == SystemZII::CompareAndBranch ||
64473471bf0Spatrick      Type == SystemZII::CompareAndSibcall) ? 2 : 0));
64509467b48Spatrick   const uint32_t *RegMask;
64609467b48Spatrick   if (Type == SystemZII::CompareAndSibcall)
64773471bf0Spatrick     RegMask = MBBI->getOperand(3).getRegMask();
64809467b48Spatrick 
64909467b48Spatrick   // Clear out all current operands.
65009467b48Spatrick   int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI);
65109467b48Spatrick   assert(CCUse >= 0 && "BRC/BCR must use CC");
652*d415bd75Srobert   Branch->removeOperand(CCUse);
65373471bf0Spatrick   // Remove regmask (sibcall).
65473471bf0Spatrick   if (Type == SystemZII::CompareAndSibcall)
655*d415bd75Srobert     Branch->removeOperand(3);
65673471bf0Spatrick   // Remove target (branch or sibcall).
65709467b48Spatrick   if (Type == SystemZII::CompareAndBranch ||
65809467b48Spatrick       Type == SystemZII::CompareAndSibcall)
659*d415bd75Srobert     Branch->removeOperand(2);
660*d415bd75Srobert   Branch->removeOperand(1);
661*d415bd75Srobert   Branch->removeOperand(0);
66209467b48Spatrick 
66309467b48Spatrick   // Rebuild Branch as a fused compare and branch.
66409467b48Spatrick   // SrcNOps is the number of MI operands of the compare instruction
66509467b48Spatrick   // that we need to copy over.
66609467b48Spatrick   unsigned SrcNOps = 2;
66709467b48Spatrick   if (FusedOpcode == SystemZ::CLT || FusedOpcode == SystemZ::CLGT)
66809467b48Spatrick     SrcNOps = 3;
66909467b48Spatrick   Branch->setDesc(TII->get(FusedOpcode));
67009467b48Spatrick   MachineInstrBuilder MIB(*Branch->getParent()->getParent(), Branch);
67109467b48Spatrick   for (unsigned I = 0; I < SrcNOps; I++)
67209467b48Spatrick     MIB.add(Compare.getOperand(I));
67309467b48Spatrick   MIB.add(CCMask);
67409467b48Spatrick 
67509467b48Spatrick   if (Type == SystemZII::CompareAndBranch) {
67609467b48Spatrick     // Only conditional branches define CC, as they may be converted back
67709467b48Spatrick     // to a non-fused branch because of a long displacement.  Conditional
67809467b48Spatrick     // returns don't have that problem.
67909467b48Spatrick     MIB.add(Target).addReg(SystemZ::CC,
68009467b48Spatrick                            RegState::ImplicitDefine | RegState::Dead);
68109467b48Spatrick   }
68209467b48Spatrick 
68373471bf0Spatrick   if (Type == SystemZII::CompareAndSibcall) {
68473471bf0Spatrick     MIB.add(Target);
68509467b48Spatrick     MIB.addRegMask(RegMask);
68673471bf0Spatrick   }
68709467b48Spatrick 
68809467b48Spatrick   // Clear any intervening kills of SrcReg and SrcReg2.
68909467b48Spatrick   MBBI = Compare;
69009467b48Spatrick   for (++MBBI; MBBI != MBBE; ++MBBI) {
69109467b48Spatrick     MBBI->clearRegisterKills(SrcReg, TRI);
69209467b48Spatrick     if (SrcReg2)
69309467b48Spatrick       MBBI->clearRegisterKills(SrcReg2, TRI);
69409467b48Spatrick   }
69509467b48Spatrick   FusedComparisons += 1;
69609467b48Spatrick   return true;
69709467b48Spatrick }
69809467b48Spatrick 
69909467b48Spatrick // Process all comparison instructions in MBB.  Return true if something
70009467b48Spatrick // changed.
processBlock(MachineBasicBlock & MBB)70109467b48Spatrick bool SystemZElimCompare::processBlock(MachineBasicBlock &MBB) {
70209467b48Spatrick   bool Changed = false;
70309467b48Spatrick 
70409467b48Spatrick   // Walk backwards through the block looking for comparisons, recording
70509467b48Spatrick   // all CC users as we go.  The subroutines can delete Compare and
70609467b48Spatrick   // instructions before it.
70709467b48Spatrick   LivePhysRegs LiveRegs(*TRI);
70809467b48Spatrick   LiveRegs.addLiveOuts(MBB);
70909467b48Spatrick   bool CompleteCCUsers = !LiveRegs.contains(SystemZ::CC);
71009467b48Spatrick   SmallVector<MachineInstr *, 4> CCUsers;
71109467b48Spatrick   MachineBasicBlock::iterator MBBI = MBB.end();
71209467b48Spatrick   while (MBBI != MBB.begin()) {
71309467b48Spatrick     MachineInstr &MI = *--MBBI;
71409467b48Spatrick     if (CompleteCCUsers && (MI.isCompare() || isLoadAndTestAsCmp(MI)) &&
71509467b48Spatrick         (optimizeCompareZero(MI, CCUsers) ||
71609467b48Spatrick          fuseCompareOperations(MI, CCUsers))) {
71709467b48Spatrick       ++MBBI;
71809467b48Spatrick       MI.eraseFromParent();
71909467b48Spatrick       Changed = true;
72009467b48Spatrick       CCUsers.clear();
72109467b48Spatrick       continue;
72209467b48Spatrick     }
72309467b48Spatrick 
72409467b48Spatrick     if (MI.definesRegister(SystemZ::CC)) {
72509467b48Spatrick       CCUsers.clear();
72609467b48Spatrick       CompleteCCUsers = true;
72709467b48Spatrick     }
72809467b48Spatrick     if (MI.readsRegister(SystemZ::CC) && CompleteCCUsers)
72909467b48Spatrick       CCUsers.push_back(&MI);
73009467b48Spatrick   }
73109467b48Spatrick   return Changed;
73209467b48Spatrick }
73309467b48Spatrick 
runOnMachineFunction(MachineFunction & F)73409467b48Spatrick bool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) {
73509467b48Spatrick   if (skipFunction(F.getFunction()))
73609467b48Spatrick     return false;
73709467b48Spatrick 
738*d415bd75Srobert   TII = F.getSubtarget<SystemZSubtarget>().getInstrInfo();
73909467b48Spatrick   TRI = &TII->getRegisterInfo();
74009467b48Spatrick 
74109467b48Spatrick   bool Changed = false;
74209467b48Spatrick   for (auto &MBB : F)
74309467b48Spatrick     Changed |= processBlock(MBB);
74409467b48Spatrick 
74509467b48Spatrick   return Changed;
74609467b48Spatrick }
74709467b48Spatrick 
createSystemZElimComparePass(SystemZTargetMachine & TM)74809467b48Spatrick FunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) {
749*d415bd75Srobert   return new SystemZElimCompare();
75009467b48Spatrick }
751