xref: /minix3/external/bsd/llvm/dist/llvm/lib/Target/SystemZ/SystemZElimCompare.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===-- SystemZElimCompare.cpp - Eliminate comparison instructions --------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This pass:
11f4a2713aSLionel Sambuc // (1) tries to remove compares if CC already contains the required information
12f4a2713aSLionel Sambuc // (2) fuses compares and branches into COMPARE AND BRANCH instructions
13f4a2713aSLionel Sambuc //
14f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
15f4a2713aSLionel Sambuc 
16f4a2713aSLionel Sambuc #include "SystemZTargetMachine.h"
17f4a2713aSLionel Sambuc #include "llvm/ADT/Statistic.h"
18f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineFunctionPass.h"
19f4a2713aSLionel Sambuc #include "llvm/CodeGen/MachineInstrBuilder.h"
20f4a2713aSLionel Sambuc #include "llvm/IR/Function.h"
21f4a2713aSLionel Sambuc #include "llvm/Support/CommandLine.h"
22f4a2713aSLionel Sambuc #include "llvm/Support/MathExtras.h"
23f4a2713aSLionel Sambuc #include "llvm/Target/TargetInstrInfo.h"
24f4a2713aSLionel Sambuc #include "llvm/Target/TargetMachine.h"
25f4a2713aSLionel Sambuc #include "llvm/Target/TargetRegisterInfo.h"
26f4a2713aSLionel Sambuc 
27f4a2713aSLionel Sambuc using namespace llvm;
28f4a2713aSLionel Sambuc 
29*0a6a1f1dSLionel Sambuc #define DEBUG_TYPE "systemz-elim-compare"
30*0a6a1f1dSLionel Sambuc 
31f4a2713aSLionel Sambuc STATISTIC(BranchOnCounts, "Number of branch-on-count instructions");
32f4a2713aSLionel Sambuc STATISTIC(EliminatedComparisons, "Number of eliminated comparisons");
33f4a2713aSLionel Sambuc STATISTIC(FusedComparisons, "Number of fused compare-and-branch instructions");
34f4a2713aSLionel Sambuc 
35f4a2713aSLionel Sambuc namespace {
36f4a2713aSLionel Sambuc // Represents the references to a particular register in one or more
37f4a2713aSLionel Sambuc // instructions.
38f4a2713aSLionel Sambuc struct Reference {
Reference__anonfc8f98f40111::Reference39f4a2713aSLionel Sambuc   Reference()
40f4a2713aSLionel Sambuc     : Def(false), Use(false), IndirectDef(false), IndirectUse(false) {}
41f4a2713aSLionel Sambuc 
operator |=__anonfc8f98f40111::Reference42f4a2713aSLionel Sambuc   Reference &operator|=(const Reference &Other) {
43f4a2713aSLionel Sambuc     Def |= Other.Def;
44f4a2713aSLionel Sambuc     IndirectDef |= Other.IndirectDef;
45f4a2713aSLionel Sambuc     Use |= Other.Use;
46f4a2713aSLionel Sambuc     IndirectUse |= Other.IndirectUse;
47f4a2713aSLionel Sambuc     return *this;
48f4a2713aSLionel Sambuc   }
49f4a2713aSLionel Sambuc 
operator bool__anonfc8f98f40111::Reference50*0a6a1f1dSLionel Sambuc   LLVM_EXPLICIT operator bool() const { return Def || Use; }
51f4a2713aSLionel Sambuc 
52f4a2713aSLionel Sambuc   // True if the register is defined or used in some form, either directly or
53f4a2713aSLionel Sambuc   // via a sub- or super-register.
54f4a2713aSLionel Sambuc   bool Def;
55f4a2713aSLionel Sambuc   bool Use;
56f4a2713aSLionel Sambuc 
57f4a2713aSLionel Sambuc   // True if the register is defined or used indirectly, by a sub- or
58f4a2713aSLionel Sambuc   // super-register.
59f4a2713aSLionel Sambuc   bool IndirectDef;
60f4a2713aSLionel Sambuc   bool IndirectUse;
61f4a2713aSLionel Sambuc };
62f4a2713aSLionel Sambuc 
63f4a2713aSLionel Sambuc class SystemZElimCompare : public MachineFunctionPass {
64f4a2713aSLionel Sambuc public:
65f4a2713aSLionel Sambuc   static char ID;
SystemZElimCompare(const SystemZTargetMachine & tm)66f4a2713aSLionel Sambuc   SystemZElimCompare(const SystemZTargetMachine &tm)
67*0a6a1f1dSLionel Sambuc     : MachineFunctionPass(ID), TII(nullptr), TRI(nullptr) {}
68f4a2713aSLionel Sambuc 
getPassName() const69*0a6a1f1dSLionel Sambuc   const char *getPassName() const override {
70f4a2713aSLionel Sambuc     return "SystemZ Comparison Elimination";
71f4a2713aSLionel Sambuc   }
72f4a2713aSLionel Sambuc 
73*0a6a1f1dSLionel Sambuc   bool processBlock(MachineBasicBlock &MBB);
74*0a6a1f1dSLionel Sambuc   bool runOnMachineFunction(MachineFunction &F) override;
75f4a2713aSLionel Sambuc 
76f4a2713aSLionel Sambuc private:
77f4a2713aSLionel Sambuc   Reference getRegReferences(MachineInstr *MI, unsigned Reg);
78f4a2713aSLionel Sambuc   bool convertToBRCT(MachineInstr *MI, MachineInstr *Compare,
79f4a2713aSLionel Sambuc                      SmallVectorImpl<MachineInstr *> &CCUsers);
80f4a2713aSLionel Sambuc   bool convertToLoadAndTest(MachineInstr *MI);
81f4a2713aSLionel Sambuc   bool adjustCCMasksForInstr(MachineInstr *MI, MachineInstr *Compare,
82f4a2713aSLionel Sambuc                              SmallVectorImpl<MachineInstr *> &CCUsers);
83f4a2713aSLionel Sambuc   bool optimizeCompareZero(MachineInstr *Compare,
84f4a2713aSLionel Sambuc                            SmallVectorImpl<MachineInstr *> &CCUsers);
85f4a2713aSLionel Sambuc   bool fuseCompareAndBranch(MachineInstr *Compare,
86f4a2713aSLionel Sambuc                             SmallVectorImpl<MachineInstr *> &CCUsers);
87f4a2713aSLionel Sambuc 
88f4a2713aSLionel Sambuc   const SystemZInstrInfo *TII;
89f4a2713aSLionel Sambuc   const TargetRegisterInfo *TRI;
90f4a2713aSLionel Sambuc };
91f4a2713aSLionel Sambuc 
92f4a2713aSLionel Sambuc char SystemZElimCompare::ID = 0;
93*0a6a1f1dSLionel Sambuc } // end anonymous namespace
94f4a2713aSLionel Sambuc 
createSystemZElimComparePass(SystemZTargetMachine & TM)95f4a2713aSLionel Sambuc FunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) {
96f4a2713aSLionel Sambuc   return new SystemZElimCompare(TM);
97f4a2713aSLionel Sambuc }
98f4a2713aSLionel Sambuc 
99f4a2713aSLionel Sambuc // Return true if CC is live out of MBB.
isCCLiveOut(MachineBasicBlock & MBB)100*0a6a1f1dSLionel Sambuc static bool isCCLiveOut(MachineBasicBlock &MBB) {
101*0a6a1f1dSLionel Sambuc   for (auto SI = MBB.succ_begin(), SE = MBB.succ_end(); SI != SE; ++SI)
102f4a2713aSLionel Sambuc     if ((*SI)->isLiveIn(SystemZ::CC))
103f4a2713aSLionel Sambuc       return true;
104f4a2713aSLionel Sambuc   return false;
105f4a2713aSLionel Sambuc }
106f4a2713aSLionel Sambuc 
107f4a2713aSLionel Sambuc // Return true if any CC result of MI would reflect the value of subreg
108f4a2713aSLionel Sambuc // SubReg of Reg.
resultTests(MachineInstr * MI,unsigned Reg,unsigned SubReg)109f4a2713aSLionel Sambuc static bool resultTests(MachineInstr *MI, unsigned Reg, unsigned SubReg) {
110f4a2713aSLionel Sambuc   if (MI->getNumOperands() > 0 &&
111f4a2713aSLionel Sambuc       MI->getOperand(0).isReg() &&
112f4a2713aSLionel Sambuc       MI->getOperand(0).isDef() &&
113f4a2713aSLionel Sambuc       MI->getOperand(0).getReg() == Reg &&
114f4a2713aSLionel Sambuc       MI->getOperand(0).getSubReg() == SubReg)
115f4a2713aSLionel Sambuc     return true;
116f4a2713aSLionel Sambuc 
117f4a2713aSLionel Sambuc   switch (MI->getOpcode()) {
118f4a2713aSLionel Sambuc   case SystemZ::LR:
119f4a2713aSLionel Sambuc   case SystemZ::LGR:
120f4a2713aSLionel Sambuc   case SystemZ::LGFR:
121f4a2713aSLionel Sambuc   case SystemZ::LTR:
122f4a2713aSLionel Sambuc   case SystemZ::LTGR:
123f4a2713aSLionel Sambuc   case SystemZ::LTGFR:
124f4a2713aSLionel Sambuc   case SystemZ::LER:
125f4a2713aSLionel Sambuc   case SystemZ::LDR:
126f4a2713aSLionel Sambuc   case SystemZ::LXR:
127f4a2713aSLionel Sambuc   case SystemZ::LTEBR:
128f4a2713aSLionel Sambuc   case SystemZ::LTDBR:
129f4a2713aSLionel Sambuc   case SystemZ::LTXBR:
130f4a2713aSLionel Sambuc     if (MI->getOperand(1).getReg() == Reg &&
131f4a2713aSLionel Sambuc         MI->getOperand(1).getSubReg() == SubReg)
132f4a2713aSLionel Sambuc       return true;
133f4a2713aSLionel Sambuc   }
134f4a2713aSLionel Sambuc 
135f4a2713aSLionel Sambuc   return false;
136f4a2713aSLionel Sambuc }
137f4a2713aSLionel Sambuc 
138f4a2713aSLionel Sambuc // Describe the references to Reg in MI, including sub- and super-registers.
getRegReferences(MachineInstr * MI,unsigned Reg)139f4a2713aSLionel Sambuc Reference SystemZElimCompare::getRegReferences(MachineInstr *MI, unsigned Reg) {
140f4a2713aSLionel Sambuc   Reference Ref;
141f4a2713aSLionel Sambuc   for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
142f4a2713aSLionel Sambuc     const MachineOperand &MO = MI->getOperand(I);
143f4a2713aSLionel Sambuc     if (MO.isReg()) {
144f4a2713aSLionel Sambuc       if (unsigned MOReg = MO.getReg()) {
145f4a2713aSLionel Sambuc         if (MOReg == Reg || TRI->regsOverlap(MOReg, Reg)) {
146f4a2713aSLionel Sambuc           if (MO.isUse()) {
147f4a2713aSLionel Sambuc             Ref.Use = true;
148f4a2713aSLionel Sambuc             Ref.IndirectUse |= (MOReg != Reg);
149f4a2713aSLionel Sambuc           }
150f4a2713aSLionel Sambuc           if (MO.isDef()) {
151f4a2713aSLionel Sambuc             Ref.Def = true;
152f4a2713aSLionel Sambuc             Ref.IndirectDef |= (MOReg != Reg);
153f4a2713aSLionel Sambuc           }
154f4a2713aSLionel Sambuc         }
155f4a2713aSLionel Sambuc       }
156f4a2713aSLionel Sambuc     }
157f4a2713aSLionel Sambuc   }
158f4a2713aSLionel Sambuc   return Ref;
159f4a2713aSLionel Sambuc }
160f4a2713aSLionel Sambuc 
161f4a2713aSLionel Sambuc // Compare compares the result of MI against zero.  If MI is an addition
162f4a2713aSLionel Sambuc // of -1 and if CCUsers is a single branch on nonzero, eliminate the addition
163f4a2713aSLionel Sambuc // and convert the branch to a BRCT(G).  Return true on success.
164f4a2713aSLionel Sambuc bool
convertToBRCT(MachineInstr * MI,MachineInstr * Compare,SmallVectorImpl<MachineInstr * > & CCUsers)165f4a2713aSLionel Sambuc SystemZElimCompare::convertToBRCT(MachineInstr *MI, MachineInstr *Compare,
166f4a2713aSLionel Sambuc                                   SmallVectorImpl<MachineInstr *> &CCUsers) {
167f4a2713aSLionel Sambuc   // Check whether we have an addition of -1.
168f4a2713aSLionel Sambuc   unsigned Opcode = MI->getOpcode();
169f4a2713aSLionel Sambuc   unsigned BRCT;
170f4a2713aSLionel Sambuc   if (Opcode == SystemZ::AHI)
171f4a2713aSLionel Sambuc     BRCT = SystemZ::BRCT;
172f4a2713aSLionel Sambuc   else if (Opcode == SystemZ::AGHI)
173f4a2713aSLionel Sambuc     BRCT = SystemZ::BRCTG;
174f4a2713aSLionel Sambuc   else
175f4a2713aSLionel Sambuc     return false;
176f4a2713aSLionel Sambuc   if (MI->getOperand(2).getImm() != -1)
177f4a2713aSLionel Sambuc     return false;
178f4a2713aSLionel Sambuc 
179f4a2713aSLionel Sambuc   // Check whether we have a single JLH.
180f4a2713aSLionel Sambuc   if (CCUsers.size() != 1)
181f4a2713aSLionel Sambuc     return false;
182f4a2713aSLionel Sambuc   MachineInstr *Branch = CCUsers[0];
183f4a2713aSLionel Sambuc   if (Branch->getOpcode() != SystemZ::BRC ||
184f4a2713aSLionel Sambuc       Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP ||
185f4a2713aSLionel Sambuc       Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_NE)
186f4a2713aSLionel Sambuc     return false;
187f4a2713aSLionel Sambuc 
188f4a2713aSLionel Sambuc   // We already know that there are no references to the register between
189f4a2713aSLionel Sambuc   // MI and Compare.  Make sure that there are also no references between
190f4a2713aSLionel Sambuc   // Compare and Branch.
191f4a2713aSLionel Sambuc   unsigned SrcReg = Compare->getOperand(0).getReg();
192f4a2713aSLionel Sambuc   MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
193f4a2713aSLionel Sambuc   for (++MBBI; MBBI != MBBE; ++MBBI)
194f4a2713aSLionel Sambuc     if (getRegReferences(MBBI, SrcReg))
195f4a2713aSLionel Sambuc       return false;
196f4a2713aSLionel Sambuc 
197f4a2713aSLionel Sambuc   // The transformation is OK.  Rebuild Branch as a BRCT(G).
198f4a2713aSLionel Sambuc   MachineOperand Target(Branch->getOperand(2));
199f4a2713aSLionel Sambuc   Branch->RemoveOperand(2);
200f4a2713aSLionel Sambuc   Branch->RemoveOperand(1);
201f4a2713aSLionel Sambuc   Branch->RemoveOperand(0);
202f4a2713aSLionel Sambuc   Branch->setDesc(TII->get(BRCT));
203f4a2713aSLionel Sambuc   MachineInstrBuilder(*Branch->getParent()->getParent(), Branch)
204f4a2713aSLionel Sambuc     .addOperand(MI->getOperand(0))
205f4a2713aSLionel Sambuc     .addOperand(MI->getOperand(1))
206f4a2713aSLionel Sambuc     .addOperand(Target)
207f4a2713aSLionel Sambuc     .addReg(SystemZ::CC, RegState::ImplicitDefine);
208f4a2713aSLionel Sambuc   MI->removeFromParent();
209f4a2713aSLionel Sambuc   return true;
210f4a2713aSLionel Sambuc }
211f4a2713aSLionel Sambuc 
212f4a2713aSLionel Sambuc // If MI is a load instruction, try to convert it into a LOAD AND TEST.
213f4a2713aSLionel Sambuc // Return true on success.
convertToLoadAndTest(MachineInstr * MI)214f4a2713aSLionel Sambuc bool SystemZElimCompare::convertToLoadAndTest(MachineInstr *MI) {
215f4a2713aSLionel Sambuc   unsigned Opcode = TII->getLoadAndTest(MI->getOpcode());
216f4a2713aSLionel Sambuc   if (!Opcode)
217f4a2713aSLionel Sambuc     return false;
218f4a2713aSLionel Sambuc 
219f4a2713aSLionel Sambuc   MI->setDesc(TII->get(Opcode));
220f4a2713aSLionel Sambuc   MachineInstrBuilder(*MI->getParent()->getParent(), MI)
221f4a2713aSLionel Sambuc     .addReg(SystemZ::CC, RegState::ImplicitDefine);
222f4a2713aSLionel Sambuc   return true;
223f4a2713aSLionel Sambuc }
224f4a2713aSLionel Sambuc 
225f4a2713aSLionel Sambuc // The CC users in CCUsers are testing the result of a comparison of some
226f4a2713aSLionel Sambuc // value X against zero and we know that any CC value produced by MI
227f4a2713aSLionel Sambuc // would also reflect the value of X.  Try to adjust CCUsers so that
228f4a2713aSLionel Sambuc // they test the result of MI directly, returning true on success.
229f4a2713aSLionel Sambuc // Leave everything unchanged on failure.
230f4a2713aSLionel Sambuc bool SystemZElimCompare::
adjustCCMasksForInstr(MachineInstr * MI,MachineInstr * Compare,SmallVectorImpl<MachineInstr * > & CCUsers)231f4a2713aSLionel Sambuc adjustCCMasksForInstr(MachineInstr *MI, MachineInstr *Compare,
232f4a2713aSLionel Sambuc                       SmallVectorImpl<MachineInstr *> &CCUsers) {
233f4a2713aSLionel Sambuc   int Opcode = MI->getOpcode();
234f4a2713aSLionel Sambuc   const MCInstrDesc &Desc = TII->get(Opcode);
235f4a2713aSLionel Sambuc   unsigned MIFlags = Desc.TSFlags;
236f4a2713aSLionel Sambuc 
237f4a2713aSLionel Sambuc   // See which compare-style condition codes are available.
238f4a2713aSLionel Sambuc   unsigned ReusableCCMask = SystemZII::getCompareZeroCCMask(MIFlags);
239f4a2713aSLionel Sambuc 
240f4a2713aSLionel Sambuc   // For unsigned comparisons with zero, only equality makes sense.
241f4a2713aSLionel Sambuc   unsigned CompareFlags = Compare->getDesc().TSFlags;
242f4a2713aSLionel Sambuc   if (CompareFlags & SystemZII::IsLogical)
243f4a2713aSLionel Sambuc     ReusableCCMask &= SystemZ::CCMASK_CMP_EQ;
244f4a2713aSLionel Sambuc 
245f4a2713aSLionel Sambuc   if (ReusableCCMask == 0)
246f4a2713aSLionel Sambuc     return false;
247f4a2713aSLionel Sambuc 
248f4a2713aSLionel Sambuc   unsigned CCValues = SystemZII::getCCValues(MIFlags);
249f4a2713aSLionel Sambuc   assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues");
250f4a2713aSLionel Sambuc 
251f4a2713aSLionel Sambuc   // Now check whether these flags are enough for all users.
252f4a2713aSLionel Sambuc   SmallVector<MachineOperand *, 4> AlterMasks;
253f4a2713aSLionel Sambuc   for (unsigned int I = 0, E = CCUsers.size(); I != E; ++I) {
254f4a2713aSLionel Sambuc     MachineInstr *MI = CCUsers[I];
255f4a2713aSLionel Sambuc 
256f4a2713aSLionel Sambuc     // Fail if this isn't a use of CC that we understand.
257f4a2713aSLionel Sambuc     unsigned Flags = MI->getDesc().TSFlags;
258f4a2713aSLionel Sambuc     unsigned FirstOpNum;
259f4a2713aSLionel Sambuc     if (Flags & SystemZII::CCMaskFirst)
260f4a2713aSLionel Sambuc       FirstOpNum = 0;
261f4a2713aSLionel Sambuc     else if (Flags & SystemZII::CCMaskLast)
262f4a2713aSLionel Sambuc       FirstOpNum = MI->getNumExplicitOperands() - 2;
263f4a2713aSLionel Sambuc     else
264f4a2713aSLionel Sambuc       return false;
265f4a2713aSLionel Sambuc 
266f4a2713aSLionel Sambuc     // Check whether the instruction predicate treats all CC values
267f4a2713aSLionel Sambuc     // outside of ReusableCCMask in the same way.  In that case it
268f4a2713aSLionel Sambuc     // doesn't matter what those CC values mean.
269f4a2713aSLionel Sambuc     unsigned CCValid = MI->getOperand(FirstOpNum).getImm();
270f4a2713aSLionel Sambuc     unsigned CCMask = MI->getOperand(FirstOpNum + 1).getImm();
271f4a2713aSLionel Sambuc     unsigned OutValid = ~ReusableCCMask & CCValid;
272f4a2713aSLionel Sambuc     unsigned OutMask = ~ReusableCCMask & CCMask;
273f4a2713aSLionel Sambuc     if (OutMask != 0 && OutMask != OutValid)
274f4a2713aSLionel Sambuc       return false;
275f4a2713aSLionel Sambuc 
276f4a2713aSLionel Sambuc     AlterMasks.push_back(&MI->getOperand(FirstOpNum));
277f4a2713aSLionel Sambuc     AlterMasks.push_back(&MI->getOperand(FirstOpNum + 1));
278f4a2713aSLionel Sambuc   }
279f4a2713aSLionel Sambuc 
280f4a2713aSLionel Sambuc   // All users are OK.  Adjust the masks for MI.
281f4a2713aSLionel Sambuc   for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) {
282f4a2713aSLionel Sambuc     AlterMasks[I]->setImm(CCValues);
283f4a2713aSLionel Sambuc     unsigned CCMask = AlterMasks[I + 1]->getImm();
284f4a2713aSLionel Sambuc     if (CCMask & ~ReusableCCMask)
285f4a2713aSLionel Sambuc       AlterMasks[I + 1]->setImm((CCMask & ReusableCCMask) |
286f4a2713aSLionel Sambuc                                 (CCValues & ~ReusableCCMask));
287f4a2713aSLionel Sambuc   }
288f4a2713aSLionel Sambuc 
289f4a2713aSLionel Sambuc   // CC is now live after MI.
290f4a2713aSLionel Sambuc   int CCDef = MI->findRegisterDefOperandIdx(SystemZ::CC, false, true, TRI);
291f4a2713aSLionel Sambuc   assert(CCDef >= 0 && "Couldn't find CC set");
292f4a2713aSLionel Sambuc   MI->getOperand(CCDef).setIsDead(false);
293f4a2713aSLionel Sambuc 
294f4a2713aSLionel Sambuc   // Clear any intervening kills of CC.
295f4a2713aSLionel Sambuc   MachineBasicBlock::iterator MBBI = MI, MBBE = Compare;
296f4a2713aSLionel Sambuc   for (++MBBI; MBBI != MBBE; ++MBBI)
297f4a2713aSLionel Sambuc     MBBI->clearRegisterKills(SystemZ::CC, TRI);
298f4a2713aSLionel Sambuc 
299f4a2713aSLionel Sambuc   return true;
300f4a2713aSLionel Sambuc }
301f4a2713aSLionel Sambuc 
302f4a2713aSLionel Sambuc // Return true if Compare is a comparison against zero.
isCompareZero(MachineInstr * Compare)303f4a2713aSLionel Sambuc static bool isCompareZero(MachineInstr *Compare) {
304f4a2713aSLionel Sambuc   switch (Compare->getOpcode()) {
305f4a2713aSLionel Sambuc   case SystemZ::LTEBRCompare:
306f4a2713aSLionel Sambuc   case SystemZ::LTDBRCompare:
307f4a2713aSLionel Sambuc   case SystemZ::LTXBRCompare:
308f4a2713aSLionel Sambuc     return true;
309f4a2713aSLionel Sambuc 
310f4a2713aSLionel Sambuc   default:
311f4a2713aSLionel Sambuc     return (Compare->getNumExplicitOperands() == 2 &&
312f4a2713aSLionel Sambuc             Compare->getOperand(1).isImm() &&
313f4a2713aSLionel Sambuc             Compare->getOperand(1).getImm() == 0);
314f4a2713aSLionel Sambuc   }
315f4a2713aSLionel Sambuc }
316f4a2713aSLionel Sambuc 
317f4a2713aSLionel Sambuc // Try to optimize cases where comparison instruction Compare is testing
318f4a2713aSLionel Sambuc // a value against zero.  Return true on success and if Compare should be
319f4a2713aSLionel Sambuc // deleted as dead.  CCUsers is the list of instructions that use the CC
320f4a2713aSLionel Sambuc // value produced by Compare.
321f4a2713aSLionel Sambuc bool SystemZElimCompare::
optimizeCompareZero(MachineInstr * Compare,SmallVectorImpl<MachineInstr * > & CCUsers)322f4a2713aSLionel Sambuc optimizeCompareZero(MachineInstr *Compare,
323f4a2713aSLionel Sambuc                     SmallVectorImpl<MachineInstr *> &CCUsers) {
324f4a2713aSLionel Sambuc   if (!isCompareZero(Compare))
325f4a2713aSLionel Sambuc     return false;
326f4a2713aSLionel Sambuc 
327f4a2713aSLionel Sambuc   // Search back for CC results that are based on the first operand.
328f4a2713aSLionel Sambuc   unsigned SrcReg = Compare->getOperand(0).getReg();
329f4a2713aSLionel Sambuc   unsigned SrcSubReg = Compare->getOperand(0).getSubReg();
330*0a6a1f1dSLionel Sambuc   MachineBasicBlock &MBB = *Compare->getParent();
331*0a6a1f1dSLionel Sambuc   MachineBasicBlock::iterator MBBI = Compare, MBBE = MBB.begin();
332f4a2713aSLionel Sambuc   Reference CCRefs;
333f4a2713aSLionel Sambuc   Reference SrcRefs;
334f4a2713aSLionel Sambuc   while (MBBI != MBBE) {
335f4a2713aSLionel Sambuc     --MBBI;
336f4a2713aSLionel Sambuc     MachineInstr *MI = MBBI;
337f4a2713aSLionel Sambuc     if (resultTests(MI, SrcReg, SrcSubReg)) {
338f4a2713aSLionel Sambuc       // Try to remove both MI and Compare by converting a branch to BRCT(G).
339f4a2713aSLionel Sambuc       // We don't care in this case whether CC is modified between MI and
340f4a2713aSLionel Sambuc       // Compare.
341f4a2713aSLionel Sambuc       if (!CCRefs.Use && !SrcRefs && convertToBRCT(MI, Compare, CCUsers)) {
342f4a2713aSLionel Sambuc         BranchOnCounts += 1;
343f4a2713aSLionel Sambuc         return true;
344f4a2713aSLionel Sambuc       }
345f4a2713aSLionel Sambuc       // Try to eliminate Compare by reusing a CC result from MI.
346f4a2713aSLionel Sambuc       if ((!CCRefs && convertToLoadAndTest(MI)) ||
347f4a2713aSLionel Sambuc           (!CCRefs.Def && adjustCCMasksForInstr(MI, Compare, CCUsers))) {
348f4a2713aSLionel Sambuc         EliminatedComparisons += 1;
349f4a2713aSLionel Sambuc         return true;
350f4a2713aSLionel Sambuc       }
351f4a2713aSLionel Sambuc     }
352f4a2713aSLionel Sambuc     SrcRefs |= getRegReferences(MI, SrcReg);
353f4a2713aSLionel Sambuc     if (SrcRefs.Def)
354f4a2713aSLionel Sambuc       return false;
355f4a2713aSLionel Sambuc     CCRefs |= getRegReferences(MI, SystemZ::CC);
356f4a2713aSLionel Sambuc     if (CCRefs.Use && CCRefs.Def)
357f4a2713aSLionel Sambuc       return false;
358f4a2713aSLionel Sambuc   }
359f4a2713aSLionel Sambuc   return false;
360f4a2713aSLionel Sambuc }
361f4a2713aSLionel Sambuc 
362f4a2713aSLionel Sambuc // Try to fuse comparison instruction Compare into a later branch.
363f4a2713aSLionel Sambuc // Return true on success and if Compare is therefore redundant.
364f4a2713aSLionel Sambuc bool SystemZElimCompare::
fuseCompareAndBranch(MachineInstr * Compare,SmallVectorImpl<MachineInstr * > & CCUsers)365f4a2713aSLionel Sambuc fuseCompareAndBranch(MachineInstr *Compare,
366f4a2713aSLionel Sambuc                      SmallVectorImpl<MachineInstr *> &CCUsers) {
367f4a2713aSLionel Sambuc   // See whether we have a comparison that can be fused.
368f4a2713aSLionel Sambuc   unsigned FusedOpcode = TII->getCompareAndBranch(Compare->getOpcode(),
369f4a2713aSLionel Sambuc                                                   Compare);
370f4a2713aSLionel Sambuc   if (!FusedOpcode)
371f4a2713aSLionel Sambuc     return false;
372f4a2713aSLionel Sambuc 
373f4a2713aSLionel Sambuc   // See whether we have a single branch with which to fuse.
374f4a2713aSLionel Sambuc   if (CCUsers.size() != 1)
375f4a2713aSLionel Sambuc     return false;
376f4a2713aSLionel Sambuc   MachineInstr *Branch = CCUsers[0];
377f4a2713aSLionel Sambuc   if (Branch->getOpcode() != SystemZ::BRC)
378f4a2713aSLionel Sambuc     return false;
379f4a2713aSLionel Sambuc 
380f4a2713aSLionel Sambuc   // Make sure that the operands are available at the branch.
381f4a2713aSLionel Sambuc   unsigned SrcReg = Compare->getOperand(0).getReg();
382f4a2713aSLionel Sambuc   unsigned SrcReg2 = (Compare->getOperand(1).isReg() ?
383f4a2713aSLionel Sambuc                       Compare->getOperand(1).getReg() : 0);
384f4a2713aSLionel Sambuc   MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
385f4a2713aSLionel Sambuc   for (++MBBI; MBBI != MBBE; ++MBBI)
386f4a2713aSLionel Sambuc     if (MBBI->modifiesRegister(SrcReg, TRI) ||
387f4a2713aSLionel Sambuc         (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI)))
388f4a2713aSLionel Sambuc       return false;
389f4a2713aSLionel Sambuc 
390f4a2713aSLionel Sambuc   // Read the branch mask and target.
391f4a2713aSLionel Sambuc   MachineOperand CCMask(MBBI->getOperand(1));
392f4a2713aSLionel Sambuc   MachineOperand Target(MBBI->getOperand(2));
393f4a2713aSLionel Sambuc   assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 &&
394f4a2713aSLionel Sambuc          "Invalid condition-code mask for integer comparison");
395f4a2713aSLionel Sambuc 
396f4a2713aSLionel Sambuc   // Clear out all current operands.
397f4a2713aSLionel Sambuc   int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI);
398f4a2713aSLionel Sambuc   assert(CCUse >= 0 && "BRC must use CC");
399f4a2713aSLionel Sambuc   Branch->RemoveOperand(CCUse);
400f4a2713aSLionel Sambuc   Branch->RemoveOperand(2);
401f4a2713aSLionel Sambuc   Branch->RemoveOperand(1);
402f4a2713aSLionel Sambuc   Branch->RemoveOperand(0);
403f4a2713aSLionel Sambuc 
404f4a2713aSLionel Sambuc   // Rebuild Branch as a fused compare and branch.
405f4a2713aSLionel Sambuc   Branch->setDesc(TII->get(FusedOpcode));
406f4a2713aSLionel Sambuc   MachineInstrBuilder(*Branch->getParent()->getParent(), Branch)
407f4a2713aSLionel Sambuc     .addOperand(Compare->getOperand(0))
408f4a2713aSLionel Sambuc     .addOperand(Compare->getOperand(1))
409f4a2713aSLionel Sambuc     .addOperand(CCMask)
410f4a2713aSLionel Sambuc     .addOperand(Target)
411f4a2713aSLionel Sambuc     .addReg(SystemZ::CC, RegState::ImplicitDefine);
412f4a2713aSLionel Sambuc 
413f4a2713aSLionel Sambuc   // Clear any intervening kills of SrcReg and SrcReg2.
414f4a2713aSLionel Sambuc   MBBI = Compare;
415f4a2713aSLionel Sambuc   for (++MBBI; MBBI != MBBE; ++MBBI) {
416f4a2713aSLionel Sambuc     MBBI->clearRegisterKills(SrcReg, TRI);
417f4a2713aSLionel Sambuc     if (SrcReg2)
418f4a2713aSLionel Sambuc       MBBI->clearRegisterKills(SrcReg2, TRI);
419f4a2713aSLionel Sambuc   }
420f4a2713aSLionel Sambuc   FusedComparisons += 1;
421f4a2713aSLionel Sambuc   return true;
422f4a2713aSLionel Sambuc }
423f4a2713aSLionel Sambuc 
424f4a2713aSLionel Sambuc // Process all comparison instructions in MBB.  Return true if something
425f4a2713aSLionel Sambuc // changed.
processBlock(MachineBasicBlock & MBB)426*0a6a1f1dSLionel Sambuc bool SystemZElimCompare::processBlock(MachineBasicBlock &MBB) {
427f4a2713aSLionel Sambuc   bool Changed = false;
428f4a2713aSLionel Sambuc 
429f4a2713aSLionel Sambuc   // Walk backwards through the block looking for comparisons, recording
430f4a2713aSLionel Sambuc   // all CC users as we go.  The subroutines can delete Compare and
431f4a2713aSLionel Sambuc   // instructions before it.
432f4a2713aSLionel Sambuc   bool CompleteCCUsers = !isCCLiveOut(MBB);
433f4a2713aSLionel Sambuc   SmallVector<MachineInstr *, 4> CCUsers;
434*0a6a1f1dSLionel Sambuc   MachineBasicBlock::iterator MBBI = MBB.end();
435*0a6a1f1dSLionel Sambuc   while (MBBI != MBB.begin()) {
436f4a2713aSLionel Sambuc     MachineInstr *MI = --MBBI;
437f4a2713aSLionel Sambuc     if (CompleteCCUsers &&
438f4a2713aSLionel Sambuc         MI->isCompare() &&
439f4a2713aSLionel Sambuc         (optimizeCompareZero(MI, CCUsers) ||
440f4a2713aSLionel Sambuc          fuseCompareAndBranch(MI, CCUsers))) {
441f4a2713aSLionel Sambuc       ++MBBI;
442f4a2713aSLionel Sambuc       MI->removeFromParent();
443f4a2713aSLionel Sambuc       Changed = true;
444f4a2713aSLionel Sambuc       CCUsers.clear();
445f4a2713aSLionel Sambuc       CompleteCCUsers = true;
446f4a2713aSLionel Sambuc       continue;
447f4a2713aSLionel Sambuc     }
448f4a2713aSLionel Sambuc 
449f4a2713aSLionel Sambuc     Reference CCRefs(getRegReferences(MI, SystemZ::CC));
450f4a2713aSLionel Sambuc     if (CCRefs.Def) {
451f4a2713aSLionel Sambuc       CCUsers.clear();
452f4a2713aSLionel Sambuc       CompleteCCUsers = !CCRefs.IndirectDef;
453f4a2713aSLionel Sambuc     }
454f4a2713aSLionel Sambuc     if (CompleteCCUsers && CCRefs.Use)
455f4a2713aSLionel Sambuc       CCUsers.push_back(MI);
456f4a2713aSLionel Sambuc   }
457f4a2713aSLionel Sambuc   return Changed;
458f4a2713aSLionel Sambuc }
459f4a2713aSLionel Sambuc 
runOnMachineFunction(MachineFunction & F)460f4a2713aSLionel Sambuc bool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) {
461*0a6a1f1dSLionel Sambuc   TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
462f4a2713aSLionel Sambuc   TRI = &TII->getRegisterInfo();
463f4a2713aSLionel Sambuc 
464f4a2713aSLionel Sambuc   bool Changed = false;
465*0a6a1f1dSLionel Sambuc   for (auto &MBB : F)
466*0a6a1f1dSLionel Sambuc     Changed |= processBlock(MBB);
467f4a2713aSLionel Sambuc 
468f4a2713aSLionel Sambuc   return Changed;
469f4a2713aSLionel Sambuc }
470