1259698Sdim//===-- SystemZElimCompare.cpp - Eliminate comparison instructions --------===//
2259698Sdim//
3259698Sdim//                     The LLVM Compiler Infrastructure
4259698Sdim//
5259698Sdim// This file is distributed under the University of Illinois Open Source
6259698Sdim// License. See LICENSE.TXT for details.
7259698Sdim//
8259698Sdim//===----------------------------------------------------------------------===//
9259698Sdim//
10259698Sdim// This pass:
11259698Sdim// (1) tries to remove compares if CC already contains the required information
12259698Sdim// (2) fuses compares and branches into COMPARE AND BRANCH instructions
13259698Sdim//
14259698Sdim//===----------------------------------------------------------------------===//
15259698Sdim
16259698Sdim#define DEBUG_TYPE "systemz-elim-compare"
17259698Sdim
18259698Sdim#include "SystemZTargetMachine.h"
19259698Sdim#include "llvm/ADT/Statistic.h"
20259698Sdim#include "llvm/CodeGen/MachineFunctionPass.h"
21259698Sdim#include "llvm/CodeGen/MachineInstrBuilder.h"
22259698Sdim#include "llvm/IR/Function.h"
23259698Sdim#include "llvm/Support/CommandLine.h"
24259698Sdim#include "llvm/Support/MathExtras.h"
25259698Sdim#include "llvm/Target/TargetInstrInfo.h"
26259698Sdim#include "llvm/Target/TargetMachine.h"
27259698Sdim#include "llvm/Target/TargetRegisterInfo.h"
28259698Sdim
29259698Sdimusing namespace llvm;
30259698Sdim
31259698SdimSTATISTIC(BranchOnCounts, "Number of branch-on-count instructions");
32259698SdimSTATISTIC(EliminatedComparisons, "Number of eliminated comparisons");
33259698SdimSTATISTIC(FusedComparisons, "Number of fused compare-and-branch instructions");
34259698Sdim
35259698Sdimnamespace {
36259698Sdim  // Represents the references to a particular register in one or more
37259698Sdim  // instructions.
38259698Sdim  struct Reference {
39259698Sdim    Reference()
40259698Sdim      : Def(false), Use(false), IndirectDef(false), IndirectUse(false) {}
41259698Sdim
42259698Sdim    Reference &operator|=(const Reference &Other) {
43259698Sdim      Def |= Other.Def;
44259698Sdim      IndirectDef |= Other.IndirectDef;
45259698Sdim      Use |= Other.Use;
46259698Sdim      IndirectUse |= Other.IndirectUse;
47259698Sdim      return *this;
48259698Sdim    }
49259698Sdim
50259698Sdim    operator bool() const { return Def || Use; }
51259698Sdim
52259698Sdim    // True if the register is defined or used in some form, either directly or
53259698Sdim    // via a sub- or super-register.
54259698Sdim    bool Def;
55259698Sdim    bool Use;
56259698Sdim
57259698Sdim    // True if the register is defined or used indirectly, by a sub- or
58259698Sdim    // super-register.
59259698Sdim    bool IndirectDef;
60259698Sdim    bool IndirectUse;
61259698Sdim  };
62259698Sdim
63259698Sdim  class SystemZElimCompare : public MachineFunctionPass {
64259698Sdim  public:
65259698Sdim    static char ID;
66259698Sdim    SystemZElimCompare(const SystemZTargetMachine &tm)
67259698Sdim      : MachineFunctionPass(ID), TII(0), TRI(0) {}
68259698Sdim
69259698Sdim    virtual const char *getPassName() const {
70259698Sdim      return "SystemZ Comparison Elimination";
71259698Sdim    }
72259698Sdim
73259698Sdim    bool processBlock(MachineBasicBlock *MBB);
74259698Sdim    bool runOnMachineFunction(MachineFunction &F);
75259698Sdim
76259698Sdim  private:
77259698Sdim    Reference getRegReferences(MachineInstr *MI, unsigned Reg);
78259698Sdim    bool convertToBRCT(MachineInstr *MI, MachineInstr *Compare,
79259698Sdim                       SmallVectorImpl<MachineInstr *> &CCUsers);
80259698Sdim    bool convertToLoadAndTest(MachineInstr *MI);
81259698Sdim    bool adjustCCMasksForInstr(MachineInstr *MI, MachineInstr *Compare,
82259698Sdim                               SmallVectorImpl<MachineInstr *> &CCUsers);
83259698Sdim    bool optimizeCompareZero(MachineInstr *Compare,
84259698Sdim                             SmallVectorImpl<MachineInstr *> &CCUsers);
85259698Sdim    bool fuseCompareAndBranch(MachineInstr *Compare,
86259698Sdim                              SmallVectorImpl<MachineInstr *> &CCUsers);
87259698Sdim
88259698Sdim    const SystemZInstrInfo *TII;
89259698Sdim    const TargetRegisterInfo *TRI;
90259698Sdim  };
91259698Sdim
92259698Sdim  char SystemZElimCompare::ID = 0;
93259698Sdim} // end of anonymous namespace
94259698Sdim
95259698SdimFunctionPass *llvm::createSystemZElimComparePass(SystemZTargetMachine &TM) {
96259698Sdim  return new SystemZElimCompare(TM);
97259698Sdim}
98259698Sdim
99259698Sdim// Return true if CC is live out of MBB.
100259698Sdimstatic bool isCCLiveOut(MachineBasicBlock *MBB) {
101259698Sdim  for (MachineBasicBlock::succ_iterator SI = MBB->succ_begin(),
102259698Sdim         SE = MBB->succ_end(); SI != SE; ++SI)
103259698Sdim    if ((*SI)->isLiveIn(SystemZ::CC))
104259698Sdim      return true;
105259698Sdim  return false;
106259698Sdim}
107259698Sdim
108259698Sdim// Return true if any CC result of MI would reflect the value of subreg
109259698Sdim// SubReg of Reg.
110259698Sdimstatic bool resultTests(MachineInstr *MI, unsigned Reg, unsigned SubReg) {
111259698Sdim  if (MI->getNumOperands() > 0 &&
112259698Sdim      MI->getOperand(0).isReg() &&
113259698Sdim      MI->getOperand(0).isDef() &&
114259698Sdim      MI->getOperand(0).getReg() == Reg &&
115259698Sdim      MI->getOperand(0).getSubReg() == SubReg)
116259698Sdim    return true;
117259698Sdim
118259698Sdim  switch (MI->getOpcode()) {
119259698Sdim  case SystemZ::LR:
120259698Sdim  case SystemZ::LGR:
121259698Sdim  case SystemZ::LGFR:
122259698Sdim  case SystemZ::LTR:
123259698Sdim  case SystemZ::LTGR:
124259698Sdim  case SystemZ::LTGFR:
125259698Sdim  case SystemZ::LER:
126259698Sdim  case SystemZ::LDR:
127259698Sdim  case SystemZ::LXR:
128259698Sdim  case SystemZ::LTEBR:
129259698Sdim  case SystemZ::LTDBR:
130259698Sdim  case SystemZ::LTXBR:
131259698Sdim    if (MI->getOperand(1).getReg() == Reg &&
132259698Sdim        MI->getOperand(1).getSubReg() == SubReg)
133259698Sdim      return true;
134259698Sdim  }
135259698Sdim
136259698Sdim  return false;
137259698Sdim}
138259698Sdim
139259698Sdim// Describe the references to Reg in MI, including sub- and super-registers.
140259698SdimReference SystemZElimCompare::getRegReferences(MachineInstr *MI, unsigned Reg) {
141259698Sdim  Reference Ref;
142259698Sdim  for (unsigned I = 0, E = MI->getNumOperands(); I != E; ++I) {
143259698Sdim    const MachineOperand &MO = MI->getOperand(I);
144259698Sdim    if (MO.isReg()) {
145259698Sdim      if (unsigned MOReg = MO.getReg()) {
146259698Sdim        if (MOReg == Reg || TRI->regsOverlap(MOReg, Reg)) {
147259698Sdim          if (MO.isUse()) {
148259698Sdim            Ref.Use = true;
149259698Sdim            Ref.IndirectUse |= (MOReg != Reg);
150259698Sdim          }
151259698Sdim          if (MO.isDef()) {
152259698Sdim            Ref.Def = true;
153259698Sdim            Ref.IndirectDef |= (MOReg != Reg);
154259698Sdim          }
155259698Sdim        }
156259698Sdim      }
157259698Sdim    }
158259698Sdim  }
159259698Sdim  return Ref;
160259698Sdim}
161259698Sdim
162259698Sdim// Compare compares the result of MI against zero.  If MI is an addition
163259698Sdim// of -1 and if CCUsers is a single branch on nonzero, eliminate the addition
164259698Sdim// and convert the branch to a BRCT(G).  Return true on success.
165259698Sdimbool
166259698SdimSystemZElimCompare::convertToBRCT(MachineInstr *MI, MachineInstr *Compare,
167259698Sdim                                  SmallVectorImpl<MachineInstr *> &CCUsers) {
168259698Sdim  // Check whether we have an addition of -1.
169259698Sdim  unsigned Opcode = MI->getOpcode();
170259698Sdim  unsigned BRCT;
171259698Sdim  if (Opcode == SystemZ::AHI)
172259698Sdim    BRCT = SystemZ::BRCT;
173259698Sdim  else if (Opcode == SystemZ::AGHI)
174259698Sdim    BRCT = SystemZ::BRCTG;
175259698Sdim  else
176259698Sdim    return false;
177259698Sdim  if (MI->getOperand(2).getImm() != -1)
178259698Sdim    return false;
179259698Sdim
180259698Sdim  // Check whether we have a single JLH.
181259698Sdim  if (CCUsers.size() != 1)
182259698Sdim    return false;
183259698Sdim  MachineInstr *Branch = CCUsers[0];
184259698Sdim  if (Branch->getOpcode() != SystemZ::BRC ||
185259698Sdim      Branch->getOperand(0).getImm() != SystemZ::CCMASK_ICMP ||
186259698Sdim      Branch->getOperand(1).getImm() != SystemZ::CCMASK_CMP_NE)
187259698Sdim    return false;
188259698Sdim
189259698Sdim  // We already know that there are no references to the register between
190259698Sdim  // MI and Compare.  Make sure that there are also no references between
191259698Sdim  // Compare and Branch.
192259698Sdim  unsigned SrcReg = Compare->getOperand(0).getReg();
193259698Sdim  MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
194259698Sdim  for (++MBBI; MBBI != MBBE; ++MBBI)
195259698Sdim    if (getRegReferences(MBBI, SrcReg))
196259698Sdim      return false;
197259698Sdim
198259698Sdim  // The transformation is OK.  Rebuild Branch as a BRCT(G).
199259698Sdim  MachineOperand Target(Branch->getOperand(2));
200259698Sdim  Branch->RemoveOperand(2);
201259698Sdim  Branch->RemoveOperand(1);
202259698Sdim  Branch->RemoveOperand(0);
203259698Sdim  Branch->setDesc(TII->get(BRCT));
204259698Sdim  MachineInstrBuilder(*Branch->getParent()->getParent(), Branch)
205259698Sdim    .addOperand(MI->getOperand(0))
206259698Sdim    .addOperand(MI->getOperand(1))
207259698Sdim    .addOperand(Target)
208259698Sdim    .addReg(SystemZ::CC, RegState::ImplicitDefine);
209259698Sdim  MI->removeFromParent();
210259698Sdim  return true;
211259698Sdim}
212259698Sdim
213259698Sdim// If MI is a load instruction, try to convert it into a LOAD AND TEST.
214259698Sdim// Return true on success.
215259698Sdimbool SystemZElimCompare::convertToLoadAndTest(MachineInstr *MI) {
216259698Sdim  unsigned Opcode = TII->getLoadAndTest(MI->getOpcode());
217259698Sdim  if (!Opcode)
218259698Sdim    return false;
219259698Sdim
220259698Sdim  MI->setDesc(TII->get(Opcode));
221259698Sdim  MachineInstrBuilder(*MI->getParent()->getParent(), MI)
222259698Sdim    .addReg(SystemZ::CC, RegState::ImplicitDefine);
223259698Sdim  return true;
224259698Sdim}
225259698Sdim
226259698Sdim// The CC users in CCUsers are testing the result of a comparison of some
227259698Sdim// value X against zero and we know that any CC value produced by MI
228259698Sdim// would also reflect the value of X.  Try to adjust CCUsers so that
229259698Sdim// they test the result of MI directly, returning true on success.
230259698Sdim// Leave everything unchanged on failure.
231259698Sdimbool SystemZElimCompare::
232259698SdimadjustCCMasksForInstr(MachineInstr *MI, MachineInstr *Compare,
233259698Sdim                      SmallVectorImpl<MachineInstr *> &CCUsers) {
234259698Sdim  int Opcode = MI->getOpcode();
235259698Sdim  const MCInstrDesc &Desc = TII->get(Opcode);
236259698Sdim  unsigned MIFlags = Desc.TSFlags;
237259698Sdim
238259698Sdim  // See which compare-style condition codes are available.
239259698Sdim  unsigned ReusableCCMask = SystemZII::getCompareZeroCCMask(MIFlags);
240259698Sdim
241259698Sdim  // For unsigned comparisons with zero, only equality makes sense.
242259698Sdim  unsigned CompareFlags = Compare->getDesc().TSFlags;
243259698Sdim  if (CompareFlags & SystemZII::IsLogical)
244259698Sdim    ReusableCCMask &= SystemZ::CCMASK_CMP_EQ;
245259698Sdim
246259698Sdim  if (ReusableCCMask == 0)
247259698Sdim    return false;
248259698Sdim
249259698Sdim  unsigned CCValues = SystemZII::getCCValues(MIFlags);
250259698Sdim  assert((ReusableCCMask & ~CCValues) == 0 && "Invalid CCValues");
251259698Sdim
252259698Sdim  // Now check whether these flags are enough for all users.
253259698Sdim  SmallVector<MachineOperand *, 4> AlterMasks;
254259698Sdim  for (unsigned int I = 0, E = CCUsers.size(); I != E; ++I) {
255259698Sdim    MachineInstr *MI = CCUsers[I];
256259698Sdim
257259698Sdim    // Fail if this isn't a use of CC that we understand.
258259698Sdim    unsigned Flags = MI->getDesc().TSFlags;
259259698Sdim    unsigned FirstOpNum;
260259698Sdim    if (Flags & SystemZII::CCMaskFirst)
261259698Sdim      FirstOpNum = 0;
262259698Sdim    else if (Flags & SystemZII::CCMaskLast)
263259698Sdim      FirstOpNum = MI->getNumExplicitOperands() - 2;
264259698Sdim    else
265259698Sdim      return false;
266259698Sdim
267259698Sdim    // Check whether the instruction predicate treats all CC values
268259698Sdim    // outside of ReusableCCMask in the same way.  In that case it
269259698Sdim    // doesn't matter what those CC values mean.
270259698Sdim    unsigned CCValid = MI->getOperand(FirstOpNum).getImm();
271259698Sdim    unsigned CCMask = MI->getOperand(FirstOpNum + 1).getImm();
272259698Sdim    unsigned OutValid = ~ReusableCCMask & CCValid;
273259698Sdim    unsigned OutMask = ~ReusableCCMask & CCMask;
274259698Sdim    if (OutMask != 0 && OutMask != OutValid)
275259698Sdim      return false;
276259698Sdim
277259698Sdim    AlterMasks.push_back(&MI->getOperand(FirstOpNum));
278259698Sdim    AlterMasks.push_back(&MI->getOperand(FirstOpNum + 1));
279259698Sdim  }
280259698Sdim
281259698Sdim  // All users are OK.  Adjust the masks for MI.
282259698Sdim  for (unsigned I = 0, E = AlterMasks.size(); I != E; I += 2) {
283259698Sdim    AlterMasks[I]->setImm(CCValues);
284259698Sdim    unsigned CCMask = AlterMasks[I + 1]->getImm();
285259698Sdim    if (CCMask & ~ReusableCCMask)
286259698Sdim      AlterMasks[I + 1]->setImm((CCMask & ReusableCCMask) |
287259698Sdim                                (CCValues & ~ReusableCCMask));
288259698Sdim  }
289259698Sdim
290259698Sdim  // CC is now live after MI.
291259698Sdim  int CCDef = MI->findRegisterDefOperandIdx(SystemZ::CC, false, true, TRI);
292259698Sdim  assert(CCDef >= 0 && "Couldn't find CC set");
293259698Sdim  MI->getOperand(CCDef).setIsDead(false);
294259698Sdim
295259698Sdim  // Clear any intervening kills of CC.
296259698Sdim  MachineBasicBlock::iterator MBBI = MI, MBBE = Compare;
297259698Sdim  for (++MBBI; MBBI != MBBE; ++MBBI)
298259698Sdim    MBBI->clearRegisterKills(SystemZ::CC, TRI);
299259698Sdim
300259698Sdim  return true;
301259698Sdim}
302259698Sdim
303259698Sdim// Return true if Compare is a comparison against zero.
304259698Sdimstatic bool isCompareZero(MachineInstr *Compare) {
305259698Sdim  switch (Compare->getOpcode()) {
306259698Sdim  case SystemZ::LTEBRCompare:
307259698Sdim  case SystemZ::LTDBRCompare:
308259698Sdim  case SystemZ::LTXBRCompare:
309259698Sdim    return true;
310259698Sdim
311259698Sdim  default:
312259698Sdim    return (Compare->getNumExplicitOperands() == 2 &&
313259698Sdim            Compare->getOperand(1).isImm() &&
314259698Sdim            Compare->getOperand(1).getImm() == 0);
315259698Sdim  }
316259698Sdim}
317259698Sdim
318259698Sdim// Try to optimize cases where comparison instruction Compare is testing
319259698Sdim// a value against zero.  Return true on success and if Compare should be
320259698Sdim// deleted as dead.  CCUsers is the list of instructions that use the CC
321259698Sdim// value produced by Compare.
322259698Sdimbool SystemZElimCompare::
323259698SdimoptimizeCompareZero(MachineInstr *Compare,
324259698Sdim                    SmallVectorImpl<MachineInstr *> &CCUsers) {
325259698Sdim  if (!isCompareZero(Compare))
326259698Sdim    return false;
327259698Sdim
328259698Sdim  // Search back for CC results that are based on the first operand.
329259698Sdim  unsigned SrcReg = Compare->getOperand(0).getReg();
330259698Sdim  unsigned SrcSubReg = Compare->getOperand(0).getSubReg();
331259698Sdim  MachineBasicBlock *MBB = Compare->getParent();
332259698Sdim  MachineBasicBlock::iterator MBBI = Compare, MBBE = MBB->begin();
333259698Sdim  Reference CCRefs;
334259698Sdim  Reference SrcRefs;
335259698Sdim  while (MBBI != MBBE) {
336259698Sdim    --MBBI;
337259698Sdim    MachineInstr *MI = MBBI;
338259698Sdim    if (resultTests(MI, SrcReg, SrcSubReg)) {
339259698Sdim      // Try to remove both MI and Compare by converting a branch to BRCT(G).
340259698Sdim      // We don't care in this case whether CC is modified between MI and
341259698Sdim      // Compare.
342259698Sdim      if (!CCRefs.Use && !SrcRefs && convertToBRCT(MI, Compare, CCUsers)) {
343259698Sdim        BranchOnCounts += 1;
344259698Sdim        return true;
345259698Sdim      }
346259698Sdim      // Try to eliminate Compare by reusing a CC result from MI.
347259698Sdim      if ((!CCRefs && convertToLoadAndTest(MI)) ||
348259698Sdim          (!CCRefs.Def && adjustCCMasksForInstr(MI, Compare, CCUsers))) {
349259698Sdim        EliminatedComparisons += 1;
350259698Sdim        return true;
351259698Sdim      }
352259698Sdim    }
353259698Sdim    SrcRefs |= getRegReferences(MI, SrcReg);
354259698Sdim    if (SrcRefs.Def)
355259698Sdim      return false;
356259698Sdim    CCRefs |= getRegReferences(MI, SystemZ::CC);
357259698Sdim    if (CCRefs.Use && CCRefs.Def)
358259698Sdim      return false;
359259698Sdim  }
360259698Sdim  return false;
361259698Sdim}
362259698Sdim
363259698Sdim// Try to fuse comparison instruction Compare into a later branch.
364259698Sdim// Return true on success and if Compare is therefore redundant.
365259698Sdimbool SystemZElimCompare::
366259698SdimfuseCompareAndBranch(MachineInstr *Compare,
367259698Sdim                     SmallVectorImpl<MachineInstr *> &CCUsers) {
368259698Sdim  // See whether we have a comparison that can be fused.
369259698Sdim  unsigned FusedOpcode = TII->getCompareAndBranch(Compare->getOpcode(),
370259698Sdim                                                  Compare);
371259698Sdim  if (!FusedOpcode)
372259698Sdim    return false;
373259698Sdim
374259698Sdim  // See whether we have a single branch with which to fuse.
375259698Sdim  if (CCUsers.size() != 1)
376259698Sdim    return false;
377259698Sdim  MachineInstr *Branch = CCUsers[0];
378259698Sdim  if (Branch->getOpcode() != SystemZ::BRC)
379259698Sdim    return false;
380259698Sdim
381259698Sdim  // Make sure that the operands are available at the branch.
382259698Sdim  unsigned SrcReg = Compare->getOperand(0).getReg();
383259698Sdim  unsigned SrcReg2 = (Compare->getOperand(1).isReg() ?
384259698Sdim                      Compare->getOperand(1).getReg() : 0);
385259698Sdim  MachineBasicBlock::iterator MBBI = Compare, MBBE = Branch;
386259698Sdim  for (++MBBI; MBBI != MBBE; ++MBBI)
387259698Sdim    if (MBBI->modifiesRegister(SrcReg, TRI) ||
388259698Sdim        (SrcReg2 && MBBI->modifiesRegister(SrcReg2, TRI)))
389259698Sdim      return false;
390259698Sdim
391259698Sdim  // Read the branch mask and target.
392259698Sdim  MachineOperand CCMask(MBBI->getOperand(1));
393259698Sdim  MachineOperand Target(MBBI->getOperand(2));
394259698Sdim  assert((CCMask.getImm() & ~SystemZ::CCMASK_ICMP) == 0 &&
395259698Sdim         "Invalid condition-code mask for integer comparison");
396259698Sdim
397259698Sdim  // Clear out all current operands.
398259698Sdim  int CCUse = MBBI->findRegisterUseOperandIdx(SystemZ::CC, false, TRI);
399259698Sdim  assert(CCUse >= 0 && "BRC must use CC");
400259698Sdim  Branch->RemoveOperand(CCUse);
401259698Sdim  Branch->RemoveOperand(2);
402259698Sdim  Branch->RemoveOperand(1);
403259698Sdim  Branch->RemoveOperand(0);
404259698Sdim
405259698Sdim  // Rebuild Branch as a fused compare and branch.
406259698Sdim  Branch->setDesc(TII->get(FusedOpcode));
407259698Sdim  MachineInstrBuilder(*Branch->getParent()->getParent(), Branch)
408259698Sdim    .addOperand(Compare->getOperand(0))
409259698Sdim    .addOperand(Compare->getOperand(1))
410259698Sdim    .addOperand(CCMask)
411259698Sdim    .addOperand(Target)
412259698Sdim    .addReg(SystemZ::CC, RegState::ImplicitDefine);
413259698Sdim
414259698Sdim  // Clear any intervening kills of SrcReg and SrcReg2.
415259698Sdim  MBBI = Compare;
416259698Sdim  for (++MBBI; MBBI != MBBE; ++MBBI) {
417259698Sdim    MBBI->clearRegisterKills(SrcReg, TRI);
418259698Sdim    if (SrcReg2)
419259698Sdim      MBBI->clearRegisterKills(SrcReg2, TRI);
420259698Sdim  }
421259698Sdim  FusedComparisons += 1;
422259698Sdim  return true;
423259698Sdim}
424259698Sdim
425259698Sdim// Process all comparison instructions in MBB.  Return true if something
426259698Sdim// changed.
427259698Sdimbool SystemZElimCompare::processBlock(MachineBasicBlock *MBB) {
428259698Sdim  bool Changed = false;
429259698Sdim
430259698Sdim  // Walk backwards through the block looking for comparisons, recording
431259698Sdim  // all CC users as we go.  The subroutines can delete Compare and
432259698Sdim  // instructions before it.
433259698Sdim  bool CompleteCCUsers = !isCCLiveOut(MBB);
434259698Sdim  SmallVector<MachineInstr *, 4> CCUsers;
435259698Sdim  MachineBasicBlock::iterator MBBI = MBB->end();
436259698Sdim  while (MBBI != MBB->begin()) {
437259698Sdim    MachineInstr *MI = --MBBI;
438259698Sdim    if (CompleteCCUsers &&
439259698Sdim        MI->isCompare() &&
440259698Sdim        (optimizeCompareZero(MI, CCUsers) ||
441259698Sdim         fuseCompareAndBranch(MI, CCUsers))) {
442259698Sdim      ++MBBI;
443259698Sdim      MI->removeFromParent();
444259698Sdim      Changed = true;
445259698Sdim      CCUsers.clear();
446259698Sdim      CompleteCCUsers = true;
447259698Sdim      continue;
448259698Sdim    }
449259698Sdim
450259698Sdim    Reference CCRefs(getRegReferences(MI, SystemZ::CC));
451259698Sdim    if (CCRefs.Def) {
452259698Sdim      CCUsers.clear();
453259698Sdim      CompleteCCUsers = !CCRefs.IndirectDef;
454259698Sdim    }
455259698Sdim    if (CompleteCCUsers && CCRefs.Use)
456259698Sdim      CCUsers.push_back(MI);
457259698Sdim  }
458259698Sdim  return Changed;
459259698Sdim}
460259698Sdim
461259698Sdimbool SystemZElimCompare::runOnMachineFunction(MachineFunction &F) {
462259698Sdim  TII = static_cast<const SystemZInstrInfo *>(F.getTarget().getInstrInfo());
463259698Sdim  TRI = &TII->getRegisterInfo();
464259698Sdim
465259698Sdim  bool Changed = false;
466259698Sdim  for (MachineFunction::iterator MFI = F.begin(), MFE = F.end();
467259698Sdim       MFI != MFE; ++MFI)
468259698Sdim    Changed |= processBlock(MFI);
469259698Sdim
470259698Sdim  return Changed;
471259698Sdim}
472